helper.c 46.8 KB
Newer Older
1 2
/*
 *  sparc helpers
3
 *
B
bellard 已提交
4
 *  Copyright (c) 2003-2005 Fabrice Bellard
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19 20 21 22 23 24 25 26 27
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <signal.h>

#include "cpu.h"
#include "exec-all.h"
28
#include "qemu-common.h"
29

B
bellard 已提交
30
//#define DEBUG_MMU
B
blueswir1 已提交
31
//#define DEBUG_FEATURES
32

B
blueswir1 已提交
33
static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model);
34

35 36 37 38
/* Sparc MMU emulation */

/* thread support */

A
Anthony Liguori 已提交
39
static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
40 41 42 43 44 45 46 47 48 49 50

void cpu_lock(void)
{
    spin_lock(&global_cpu_lock);
}

void cpu_unlock(void)
{
    spin_unlock(&global_cpu_lock);
}

51
#if defined(CONFIG_USER_ONLY)
B
bellard 已提交
52

B
blueswir1 已提交
53
int cpu_sparc_handle_mmu_fault(CPUState *env1, target_ulong address, int rw,
54
                               int mmu_idx, int is_softmmu)
B
bellard 已提交
55
{
B
bellard 已提交
56
    if (rw & 2)
B
blueswir1 已提交
57
        env1->exception_index = TT_TFAULT;
B
bellard 已提交
58
    else
B
blueswir1 已提交
59
        env1->exception_index = TT_DFAULT;
B
bellard 已提交
60 61 62 63
    return 1;
}

#else
64

B
bellard 已提交
65
#ifndef TARGET_SPARC64
B
bellard 已提交
66 67 68
/*
 * Sparc V8 Reference MMU (SRMMU)
 */
69
static const int access_table[8][8] = {
70 71 72 73 74 75 76 77
    { 0, 0, 0, 0, 8, 0, 12, 12 },
    { 0, 0, 0, 0, 8, 0, 0, 0 },
    { 8, 8, 0, 0, 0, 8, 12, 12 },
    { 8, 8, 0, 0, 0, 8, 0, 0 },
    { 8, 0, 8, 0, 8, 8, 12, 12 },
    { 8, 0, 8, 0, 8, 0, 8, 0 },
    { 8, 8, 8, 0, 8, 8, 12, 12 },
    { 8, 8, 8, 0, 8, 8, 8, 0 }
78 79
};

B
bellard 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static const int perm_table[2][8] = {
    {
        PAGE_READ,
        PAGE_READ | PAGE_WRITE,
        PAGE_READ | PAGE_EXEC,
        PAGE_READ | PAGE_WRITE | PAGE_EXEC,
        PAGE_EXEC,
        PAGE_READ | PAGE_WRITE,
        PAGE_READ | PAGE_EXEC,
        PAGE_READ | PAGE_WRITE | PAGE_EXEC
    },
    {
        PAGE_READ,
        PAGE_READ | PAGE_WRITE,
        PAGE_READ | PAGE_EXEC,
        PAGE_READ | PAGE_WRITE | PAGE_EXEC,
        PAGE_EXEC,
        PAGE_READ,
        0,
        0,
    }
101 102
};

A
Anthony Liguori 已提交
103
static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
104 105
                                int *prot, int *access_index,
                                target_ulong address, int rw, int mmu_idx)
106
{
B
bellard 已提交
107
    int access_perms = 0;
A
Anthony Liguori 已提交
108
    target_phys_addr_t pde_ptr;
109
    uint32_t pde;
110
    int error_code = 0, is_dirty, is_user;
B
bellard 已提交
111
    unsigned long page_offset;
112

113
    is_user = mmu_idx == MMU_USER_IDX;
B
blueswir1 已提交
114

115
    if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
B
blueswir1 已提交
116
        // Boot mode: instruction fetches are taken from PROM
117
        if (rw == 2 && (env->mmuregs[0] & env->def->mmu_bm)) {
B
blueswir1 已提交
118
            *physical = env->prom_addr | (address & 0x7ffffULL);
B
blueswir1 已提交
119 120 121
            *prot = PAGE_READ | PAGE_EXEC;
            return 0;
        }
B
blueswir1 已提交
122
        *physical = address;
B
bellard 已提交
123
        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
B
bellard 已提交
124
        return 0;
125 126
    }

B
bellard 已提交
127
    *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user? 0 : 1);
128
    *physical = 0xffffffffffff0000ULL;
B
bellard 已提交
129

130 131
    /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
    /* Context base + context number */
132
    pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
B
bellard 已提交
133
    pde = ldl_phys(pde_ptr);
134 135 136

    /* Ctx pde */
    switch (pde & PTE_ENTRYTYPE_MASK) {
B
bellard 已提交
137
    default:
138
    case 0: /* Invalid */
B
blueswir1 已提交
139
        return 1 << 2;
B
bellard 已提交
140
    case 2: /* L0 PTE, maybe should not happen? */
141
    case 3: /* Reserved */
B
bellard 已提交
142
        return 4 << 2;
B
bellard 已提交
143
    case 1: /* L0 PDE */
B
blueswir1 已提交
144
        pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
B
bellard 已提交
145
        pde = ldl_phys(pde_ptr);
146

B
blueswir1 已提交
147 148 149 150 151 152 153 154
        switch (pde & PTE_ENTRYTYPE_MASK) {
        default:
        case 0: /* Invalid */
            return (1 << 8) | (1 << 2);
        case 3: /* Reserved */
            return (1 << 8) | (4 << 2);
        case 1: /* L1 PDE */
            pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
B
bellard 已提交
155
            pde = ldl_phys(pde_ptr);
156

B
blueswir1 已提交
157 158 159 160 161 162 163 164
            switch (pde & PTE_ENTRYTYPE_MASK) {
            default:
            case 0: /* Invalid */
                return (2 << 8) | (1 << 2);
            case 3: /* Reserved */
                return (2 << 8) | (4 << 2);
            case 1: /* L2 PDE */
                pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
B
bellard 已提交
165
                pde = ldl_phys(pde_ptr);
166

B
blueswir1 已提交
167 168 169 170 171 172 173 174
                switch (pde & PTE_ENTRYTYPE_MASK) {
                default:
                case 0: /* Invalid */
                    return (3 << 8) | (1 << 2);
                case 1: /* PDE, should not happen */
                case 3: /* Reserved */
                    return (3 << 8) | (4 << 2);
                case 2: /* L3 PTE */
B
blueswir1 已提交
175 176
                    page_offset = (address & TARGET_PAGE_MASK) &
                        (TARGET_PAGE_SIZE - 1);
B
blueswir1 已提交
177 178 179 180 181 182 183 184 185
                }
                break;
            case 2: /* L2 PTE */
                page_offset = address & 0x3ffff;
            }
            break;
        case 2: /* L1 PTE */
            page_offset = address & 0xffffff;
        }
186 187 188
    }

    /* update page modified and dirty bits */
B
bellard 已提交
189
    is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
190
    if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
B
blueswir1 已提交
191 192 193
        pde |= PG_ACCESSED_MASK;
        if (is_dirty)
            pde |= PG_MODIFIED_MASK;
B
bellard 已提交
194
        stl_phys_notdirty(pde_ptr, pde);
195 196 197
    }
    /* check access */
    access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
B
bellard 已提交
198
    error_code = access_table[*access_index][access_perms];
B
bellard 已提交
199
    if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user))
B
blueswir1 已提交
200
        return error_code;
201 202

    /* the page can be put in the TLB */
B
bellard 已提交
203 204
    *prot = perm_table[is_user][access_perms];
    if (!(pde & PG_MODIFIED_MASK)) {
205 206
        /* only set write access if already dirty... otherwise wait
           for dirty access */
B
bellard 已提交
207
        *prot &= ~PAGE_WRITE;
208 209 210 211
    }

    /* Even if large ptes, we map only one 4KB page in the cache to
       avoid filling it too fast */
A
Anthony Liguori 已提交
212
    *physical = ((target_phys_addr_t)(pde & PTE_ADDR_MASK) << 4) + page_offset;
B
bellard 已提交
213
    return error_code;
B
bellard 已提交
214 215 216
}

/* Perform address translation */
217
int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
218
                              int mmu_idx, int is_softmmu)
B
bellard 已提交
219
{
A
Anthony Liguori 已提交
220
    target_phys_addr_t paddr;
221
    target_ulong vaddr;
B
bellard 已提交
222
    int error_code = 0, prot, ret = 0, access_index;
223

B
blueswir1 已提交
224 225
    error_code = get_physical_address(env, &paddr, &prot, &access_index,
                                      address, rw, mmu_idx);
B
bellard 已提交
226
    if (error_code == 0) {
B
blueswir1 已提交
227 228
        vaddr = address & TARGET_PAGE_MASK;
        paddr &= TARGET_PAGE_MASK;
B
bellard 已提交
229
#ifdef DEBUG_MMU
B
blueswir1 已提交
230
        printf("Translate at " TARGET_FMT_lx " -> " TARGET_FMT_plx ", vaddr "
231
               TARGET_FMT_lx "\n", address, paddr, vaddr);
B
bellard 已提交
232
#endif
233
        ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
B
blueswir1 已提交
234
        return ret;
B
bellard 已提交
235
    }
236 237

    if (env->mmuregs[3]) /* Fault status register */
B
blueswir1 已提交
238
        env->mmuregs[3] = 1; /* overflow (not read before another fault) */
B
bellard 已提交
239
    env->mmuregs[3] |= (access_index << 5) | error_code | 2;
240 241
    env->mmuregs[4] = address; /* Fault address register */

B
bellard 已提交
242
    if ((env->mmuregs[0] & MMU_NF) || env->psret == 0)  {
B
bellard 已提交
243 244 245 246
        // No fault mode: if a mapping is available, just override
        // permissions. If no mapping is available, redirect accesses to
        // neverland. Fake/overridden mappings will be flushed when
        // switching to normal mode.
B
blueswir1 已提交
247
        vaddr = address & TARGET_PAGE_MASK;
B
bellard 已提交
248
        prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
249
        ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
B
blueswir1 已提交
250
        return ret;
B
bellard 已提交
251 252 253 254 255 256
    } else {
        if (rw & 2)
            env->exception_index = TT_TFAULT;
        else
            env->exception_index = TT_DFAULT;
        return 1;
B
bellard 已提交
257
    }
258
}
259 260 261

target_ulong mmu_probe(CPUState *env, target_ulong address, int mmulev)
{
A
Anthony Liguori 已提交
262
    target_phys_addr_t pde_ptr;
263 264 265
    uint32_t pde;

    /* Context base + context number */
A
Anthony Liguori 已提交
266
    pde_ptr = (target_phys_addr_t)(env->mmuregs[1] << 4) +
267
        (env->mmuregs[2] << 2);
268 269 270 271 272 273 274
    pde = ldl_phys(pde_ptr);

    switch (pde & PTE_ENTRYTYPE_MASK) {
    default:
    case 0: /* Invalid */
    case 2: /* PTE, maybe should not happen? */
    case 3: /* Reserved */
B
blueswir1 已提交
275
        return 0;
276
    case 1: /* L1 PDE */
B
blueswir1 已提交
277 278 279
        if (mmulev == 3)
            return pde;
        pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
280 281
        pde = ldl_phys(pde_ptr);

B
blueswir1 已提交
282 283 284 285 286 287 288 289 290 291 292
        switch (pde & PTE_ENTRYTYPE_MASK) {
        default:
        case 0: /* Invalid */
        case 3: /* Reserved */
            return 0;
        case 2: /* L1 PTE */
            return pde;
        case 1: /* L2 PDE */
            if (mmulev == 2)
                return pde;
            pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
293 294
            pde = ldl_phys(pde_ptr);

B
blueswir1 已提交
295 296 297 298 299 300 301 302 303 304 305
            switch (pde & PTE_ENTRYTYPE_MASK) {
            default:
            case 0: /* Invalid */
            case 3: /* Reserved */
                return 0;
            case 2: /* L2 PTE */
                return pde;
            case 1: /* L3 PDE */
                if (mmulev == 1)
                    return pde;
                pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
306 307
                pde = ldl_phys(pde_ptr);

B
blueswir1 已提交
308 309 310 311 312 313 314 315 316 317 318
                switch (pde & PTE_ENTRYTYPE_MASK) {
                default:
                case 0: /* Invalid */
                case 1: /* PDE, should not happen */
                case 3: /* Reserved */
                    return 0;
                case 2: /* L3 PTE */
                    return pde;
                }
            }
        }
319 320 321 322 323 324 325
    }
    return 0;
}

#ifdef DEBUG_MMU
void dump_mmu(CPUState *env)
{
326 327
    target_ulong va, va1, va2;
    unsigned int n, m, o;
A
Anthony Liguori 已提交
328
    target_phys_addr_t pde_ptr, pa;
329 330 331 332 333
    uint32_t pde;

    printf("MMU dump:\n");
    pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
    pde = ldl_phys(pde_ptr);
334
    printf("Root ptr: " TARGET_FMT_plx ", ctx: %d\n",
A
Anthony Liguori 已提交
335
           (target_phys_addr_t)env->mmuregs[1] << 4, env->mmuregs[2]);
336
    for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
B
blueswir1 已提交
337 338 339 340
        pde = mmu_probe(env, va, 2);
        if (pde) {
            pa = cpu_get_phys_page_debug(env, va);
            printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
341
                   " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
B
blueswir1 已提交
342 343 344 345 346
            for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
                pde = mmu_probe(env, va1, 1);
                if (pde) {
                    pa = cpu_get_phys_page_debug(env, va1);
                    printf(" VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
347
                           " PDE: " TARGET_FMT_lx "\n", va1, pa, pde);
B
blueswir1 已提交
348 349 350 351 352
                    for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
                        pde = mmu_probe(env, va2, 0);
                        if (pde) {
                            pa = cpu_get_phys_page_debug(env, va2);
                            printf("  VA: " TARGET_FMT_lx ", PA: "
353 354
                                   TARGET_FMT_plx " PTE: " TARGET_FMT_lx "\n",
                                   va2, pa, pde);
B
blueswir1 已提交
355 356 357 358 359
                        }
                    }
                }
            }
        }
360 361 362 363 364 365
    }
    printf("MMU dump ends\n");
}
#endif /* DEBUG_MMU */

#else /* !TARGET_SPARC64 */
366 367

// 41 bit physical address space
A
Anthony Liguori 已提交
368
static inline target_phys_addr_t ultrasparc_truncate_physical(uint64_t x)
369 370 371 372
{
    return x & 0x1ffffffffffULL;
}

B
bellard 已提交
373 374 375
/*
 * UltraSparc IIi I/DMMUs
 */
376 377 378 379 380 381 382 383

static inline int compare_masked(uint64_t x, uint64_t y, uint64_t mask)
{
    return (x & mask) == (y & mask);
}

// Returns true if TTE tag is valid and matches virtual address value in context
// requires virtual address mask value calculated from TTE entry size
384
static inline int ultrasparc_tag_match(SparcTLBEntry *tlb,
385
                                       uint64_t address, uint64_t context,
386 387
                                       target_phys_addr_t *physical,
                                       int is_nucleus)
388 389 390
{
    uint64_t mask;

391
    switch ((tlb->tte >> 61) & 3) {
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
    default:
    case 0x0: // 8k
        mask = 0xffffffffffffe000ULL;
        break;
    case 0x1: // 64k
        mask = 0xffffffffffff0000ULL;
        break;
    case 0x2: // 512k
        mask = 0xfffffffffff80000ULL;
        break;
    case 0x3: // 4M
        mask = 0xffffffffffc00000ULL;
        break;
    }

    // valid, context match, virtual address match?
408
    if (TTE_IS_VALID(tlb->tte) &&
409 410 411
        ((is_nucleus && compare_masked(0, tlb->tag, 0x1fff))
         || TTE_IS_GLOBAL(tlb->tte) || compare_masked(context, tlb->tag, 0x1fff))
        && compare_masked(address, tlb->tag, mask))
412 413
    {
        // decode physical address
414
        *physical = ((tlb->tte & mask) | (address & ~mask)) & 0x1ffffffe000ULL;
415 416 417 418 419 420
        return 1;
    }

    return 0;
}

B
blueswir1 已提交
421
static int get_physical_address_data(CPUState *env,
A
Anthony Liguori 已提交
422
                                     target_phys_addr_t *physical, int *prot,
B
blueswir1 已提交
423
                                     target_ulong address, int rw, int is_user)
B
bellard 已提交
424 425
{
    unsigned int i;
426
    uint64_t context;
427
    int is_nucleus;
B
bellard 已提交
428 429

    if ((env->lsu & DMMU_E) == 0) { /* DMMU disabled */
430
        *physical = ultrasparc_truncate_physical(address);
B
blueswir1 已提交
431
        *prot = PAGE_READ | PAGE_WRITE;
B
bellard 已提交
432 433 434
        return 0;
    }

435
    context = env->dmmu.mmu_primary_context & 0x1fff;
436
    is_nucleus = env->tl > 0;
437

B
bellard 已提交
438
    for (i = 0; i < 64; i++) {
B
blueswir1 已提交
439
        // ctx match, vaddr match, valid?
440
        if (ultrasparc_tag_match(&env->dtlb[i],
441 442
                                 address, context, physical,
                                 is_nucleus)) {
B
blueswir1 已提交
443
            // access ok?
444 445 446 447 448 449 450 451 452 453
            if (((env->dtlb[i].tte & 0x4) && is_user) ||
                (!(env->dtlb[i].tte & 0x2) && (rw == 1))) {
                uint8_t fault_type = 0;

                if ((env->dtlb[i].tte & 0x4) && is_user) {
                    fault_type |= 1; /* privilege violation */
                }

                if (env->dmmu.sfsr & 1) /* Fault status register */
                    env->dmmu.sfsr = 2; /* overflow (not read before
B
blueswir1 已提交
454
                                             another fault) */
455 456 457 458 459 460

                env->dmmu.sfsr |= (is_user << 3) | ((rw == 1) << 2) | 1;

                env->dmmu.sfsr |= (fault_type << 7);

                env->dmmu.sfar = address; /* Fault address register */
B
blueswir1 已提交
461
                env->exception_index = TT_DFAULT;
B
bellard 已提交
462
#ifdef DEBUG_MMU
B
blueswir1 已提交
463
                printf("DFAULT at 0x%" PRIx64 "\n", address);
B
bellard 已提交
464
#endif
B
blueswir1 已提交
465 466 467
                return 1;
            }
            *prot = PAGE_READ;
468
            if (env->dtlb[i].tte & 0x2)
B
blueswir1 已提交
469
                *prot |= PAGE_WRITE;
470
            TTE_SET_USED(env->dtlb[i].tte);
B
blueswir1 已提交
471 472
            return 0;
        }
B
bellard 已提交
473
    }
B
bellard 已提交
474
#ifdef DEBUG_MMU
B
bellard 已提交
475
    printf("DMISS at 0x%" PRIx64 "\n", address);
B
bellard 已提交
476
#endif
477
    env->dmmu.tag_access = (address & ~0x1fffULL) | context;
B
bellard 已提交
478
    env->exception_index = TT_DMISS;
B
bellard 已提交
479 480 481
    return 1;
}

B
blueswir1 已提交
482
static int get_physical_address_code(CPUState *env,
A
Anthony Liguori 已提交
483
                                     target_phys_addr_t *physical, int *prot,
B
blueswir1 已提交
484
                                     target_ulong address, int is_user)
B
bellard 已提交
485 486
{
    unsigned int i;
487
    uint64_t context;
488
    int is_nucleus;
B
bellard 已提交
489

490 491 492
    if ((env->lsu & IMMU_E) == 0 || (env->pstate & PS_RED) != 0) {
        /* IMMU disabled */
        *physical = ultrasparc_truncate_physical(address);
B
blueswir1 已提交
493
        *prot = PAGE_EXEC;
B
bellard 已提交
494 495
        return 0;
    }
B
bellard 已提交
496

497
    context = env->dmmu.mmu_primary_context & 0x1fff;
498
    is_nucleus = env->tl > 0;
499

B
bellard 已提交
500
    for (i = 0; i < 64; i++) {
B
blueswir1 已提交
501
        // ctx match, vaddr match, valid?
502
        if (ultrasparc_tag_match(&env->itlb[i],
503 504
                                 address, context, physical,
                                 is_nucleus)) {
B
blueswir1 已提交
505
            // access ok?
506 507 508
            if ((env->itlb[i].tte & 0x4) && is_user) {
                if (env->immu.sfsr) /* Fault status register */
                    env->immu.sfsr = 2; /* overflow (not read before
B
blueswir1 已提交
509
                                             another fault) */
510
                env->immu.sfsr |= (is_user << 3) | 1;
B
blueswir1 已提交
511
                env->exception_index = TT_TFAULT;
B
bellard 已提交
512
#ifdef DEBUG_MMU
B
blueswir1 已提交
513
                printf("TFAULT at 0x%" PRIx64 "\n", address);
B
bellard 已提交
514
#endif
B
blueswir1 已提交
515 516 517
                return 1;
            }
            *prot = PAGE_EXEC;
518
            TTE_SET_USED(env->itlb[i].tte);
B
blueswir1 已提交
519 520
            return 0;
        }
B
bellard 已提交
521
    }
B
bellard 已提交
522
#ifdef DEBUG_MMU
B
bellard 已提交
523
    printf("TMISS at 0x%" PRIx64 "\n", address);
B
bellard 已提交
524
#endif
B
Blue Swirl 已提交
525
    /* Context is stored in DMMU (dmmuregs[1]) also for IMMU */
526
    env->immu.tag_access = (address & ~0x1fffULL) | context;
B
bellard 已提交
527
    env->exception_index = TT_TMISS;
B
bellard 已提交
528 529 530
    return 1;
}

A
Anthony Liguori 已提交
531
static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
532 533
                                int *prot, int *access_index,
                                target_ulong address, int rw, int mmu_idx)
B
bellard 已提交
534
{
535 536
    int is_user = mmu_idx == MMU_USER_IDX;

B
bellard 已提交
537
    if (rw == 2)
B
blueswir1 已提交
538 539
        return get_physical_address_code(env, physical, prot, address,
                                         is_user);
B
bellard 已提交
540
    else
B
blueswir1 已提交
541 542
        return get_physical_address_data(env, physical, prot, address, rw,
                                         is_user);
B
bellard 已提交
543 544 545 546
}

/* Perform address translation */
int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
547
                              int mmu_idx, int is_softmmu)
B
bellard 已提交
548
{
B
bellard 已提交
549
    target_ulong virt_addr, vaddr;
A
Anthony Liguori 已提交
550
    target_phys_addr_t paddr;
B
bellard 已提交
551 552
    int error_code = 0, prot, ret = 0, access_index;

B
blueswir1 已提交
553 554
    error_code = get_physical_address(env, &paddr, &prot, &access_index,
                                      address, rw, mmu_idx);
B
bellard 已提交
555
    if (error_code == 0) {
B
blueswir1 已提交
556
        virt_addr = address & TARGET_PAGE_MASK;
B
blueswir1 已提交
557 558
        vaddr = virt_addr + ((address & TARGET_PAGE_MASK) &
                             (TARGET_PAGE_SIZE - 1));
B
bellard 已提交
559
#ifdef DEBUG_MMU
B
blueswir1 已提交
560 561
        printf("Translate at 0x%" PRIx64 " -> 0x%" PRIx64 ", vaddr 0x%" PRIx64
               "\n", address, paddr, vaddr);
B
bellard 已提交
562
#endif
563
        ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
B
blueswir1 已提交
564
        return ret;
B
bellard 已提交
565 566 567 568 569
    }
    // XXX
    return 1;
}

B
bellard 已提交
570 571 572 573 574 575
#ifdef DEBUG_MMU
void dump_mmu(CPUState *env)
{
    unsigned int i;
    const char *mask;

B
blueswir1 已提交
576
    printf("MMU contexts: Primary: %" PRId64 ", Secondary: %" PRId64 "\n",
577
           env->dmmu.mmu_primary_context, env->dmmu.mmu_secondary_context);
B
bellard 已提交
578
    if ((env->lsu & DMMU_E) == 0) {
B
blueswir1 已提交
579
        printf("DMMU disabled\n");
B
bellard 已提交
580
    } else {
B
blueswir1 已提交
581 582
        printf("DMMU dump:\n");
        for (i = 0; i < 64; i++) {
583
            switch ((env->dtlb[i].tte >> 61) & 3) {
B
blueswir1 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597
            default:
            case 0x0:
                mask = "  8k";
                break;
            case 0x1:
                mask = " 64k";
                break;
            case 0x2:
                mask = "512k";
                break;
            case 0x3:
                mask = "  4M";
                break;
            }
598 599
            if ((env->dtlb[i].tte & 0x8000000000000000ULL) != 0) {
                printf("[%02u] VA: %" PRIx64 ", PA: %" PRIx64
600
                       ", %s, %s, %s, %s, ctx %" PRId64 " %s\n",
601
                       i,
602 603
                       env->dtlb[i].tag & (uint64_t)~0x1fffULL,
                       env->dtlb[i].tte & (uint64_t)0x1ffffffe000ULL,
B
blueswir1 已提交
604
                       mask,
605 606 607
                       env->dtlb[i].tte & 0x4? "priv": "user",
                       env->dtlb[i].tte & 0x2? "RW": "RO",
                       env->dtlb[i].tte & 0x40? "locked": "unlocked",
608 609
                       env->dtlb[i].tag & (uint64_t)0x1fffULL,
                       TTE_IS_GLOBAL(env->dtlb[i].tag)? "global" : "local");
B
blueswir1 已提交
610 611
            }
        }
B
bellard 已提交
612 613
    }
    if ((env->lsu & IMMU_E) == 0) {
B
blueswir1 已提交
614
        printf("IMMU disabled\n");
B
bellard 已提交
615
    } else {
B
blueswir1 已提交
616 617
        printf("IMMU dump:\n");
        for (i = 0; i < 64; i++) {
618
            switch ((env->itlb[i].tte >> 61) & 3) {
B
blueswir1 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632
            default:
            case 0x0:
                mask = "  8k";
                break;
            case 0x1:
                mask = " 64k";
                break;
            case 0x2:
                mask = "512k";
                break;
            case 0x3:
                mask = "  4M";
                break;
            }
633 634
            if ((env->itlb[i].tte & 0x8000000000000000ULL) != 0) {
                printf("[%02u] VA: %" PRIx64 ", PA: %" PRIx64
635
                       ", %s, %s, %s, ctx %" PRId64 " %s\n",
636 637
                       i,
                       env->itlb[i].tag & (uint64_t)~0x1fffULL,
638
                       env->itlb[i].tte & (uint64_t)0x1ffffffe000ULL,
B
blueswir1 已提交
639
                       mask,
640 641
                       env->itlb[i].tte & 0x4? "priv": "user",
                       env->itlb[i].tte & 0x40? "locked": "unlocked",
642 643
                       env->itlb[i].tag & (uint64_t)0x1fffULL,
                       TTE_IS_GLOBAL(env->itlb[i].tag)? "global" : "local");
B
blueswir1 已提交
644 645
            }
        }
B
bellard 已提交
646 647
    }
}
648 649 650 651 652
#endif /* DEBUG_MMU */

#endif /* TARGET_SPARC64 */
#endif /* !CONFIG_USER_ONLY */

653 654

#if defined(CONFIG_USER_ONLY)
A
Anthony Liguori 已提交
655
target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
656 657 658 659 660
{
    return addr;
}

#else
A
Anthony Liguori 已提交
661
target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
662
{
A
Anthony Liguori 已提交
663
    target_phys_addr_t phys_addr;
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
    int prot, access_index;

    if (get_physical_address(env, &phys_addr, &prot, &access_index, addr, 2,
                             MMU_KERNEL_IDX) != 0)
        if (get_physical_address(env, &phys_addr, &prot, &access_index, addr,
                                 0, MMU_KERNEL_IDX) != 0)
            return -1;
    if (cpu_get_physical_page_desc(phys_addr) == IO_MEM_UNASSIGNED)
        return -1;
    return phys_addr;
}
#endif

void cpu_reset(CPUSPARCState *env)
{
A
aliguori 已提交
679 680 681 682 683
    if (qemu_loglevel_mask(CPU_LOG_RESET)) {
        qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
        log_cpu_state(env, 0);
    }

684 685
    tlb_flush(env, 1);
    env->cwp = 0;
686
#ifndef TARGET_SPARC64
687
    env->wim = 1;
688
#endif
689
    env->regwptr = env->regbase + (env->cwp * 16);
690
    CC_OP = CC_OP_FLAGS;
691 692
#if defined(CONFIG_USER_ONLY)
#ifdef TARGET_SPARC64
693 694
    env->cleanwin = env->nwindows - 2;
    env->cansave = env->nwindows - 2;
695 696 697 698
    env->pstate = PS_RMO | PS_PEF | PS_IE;
    env->asi = 0x82; // Primary no-fault
#endif
#else
699
#if !defined(TARGET_SPARC64)
700
    env->psret = 0;
701
#endif
702 703 704
    env->psrs = 1;
    env->psrps = 1;
#ifdef TARGET_SPARC64
705
    env->pstate = PS_PRIV|PS_RED|PS_PEF|PS_AG;
706
    env->hpstate = HS_PRIV;
707 708
    env->tl = env->maxtl;
    cpu_tsptr(env)->tt = TT_POWER_ON_RESET;
B
blueswir1 已提交
709
    env->lsu = 0;
710 711
#else
    env->mmuregs[0] &= ~(MMU_E | MMU_NF);
712
    env->mmuregs[0] |= env->def->mmu_bm;
713
#endif
B
blueswir1 已提交
714
    env->pc = 0;
715 716 717 718
    env->npc = env->pc + 4;
#endif
}

B
blueswir1 已提交
719
static int cpu_sparc_register(CPUSPARCState *env, const char *cpu_model)
720
{
B
blueswir1 已提交
721
    sparc_def_t def1, *def = &def1;
722

B
blueswir1 已提交
723 724
    if (cpu_sparc_find_by_name(def, cpu_model) < 0)
        return -1;
725

726 727 728 729 730 731
    env->def = qemu_mallocz(sizeof(*def));
    memcpy(env->def, def, sizeof(*def));
#if defined(CONFIG_USER_ONLY)
    if ((env->def->features & CPU_FEATURE_FLOAT))
        env->def->features |= CPU_FEATURE_FLOAT128;
#endif
732 733 734
    env->cpu_model_str = cpu_model;
    env->version = def->iu_version;
    env->fsr = def->fpu_version;
735
    env->nwindows = def->nwindows;
736 737 738
#if !defined(TARGET_SPARC64)
    env->mmuregs[0] |= def->mmu_version;
    cpu_sparc_set_id(env, 0);
739
    env->mxccregs[7] |= def->mxcc_version;
740
#else
741
    env->mmu_version = def->mmu_version;
742 743
    env->maxtl = def->maxtl;
    env->version |= def->maxtl << 8;
744
    env->version |= def->nwindows - 1;
745
#endif
B
blueswir1 已提交
746 747 748 749 750
    return 0;
}

static void cpu_sparc_close(CPUSPARCState *env)
{
751
    free(env->def);
B
blueswir1 已提交
752 753 754 755 756 757 758 759 760
    free(env);
}

CPUSPARCState *cpu_sparc_init(const char *cpu_model)
{
    CPUSPARCState *env;

    env = qemu_mallocz(sizeof(CPUSPARCState));
    cpu_exec_init(env);
761 762 763

    gen_intermediate_code_init(env);

B
blueswir1 已提交
764 765 766 767
    if (cpu_sparc_register(env, cpu_model) < 0) {
        cpu_sparc_close(env);
        return NULL;
    }
768
    qemu_init_vcpu(env);
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783

    return env;
}

void cpu_sparc_set_id(CPUSPARCState *env, unsigned int cpu)
{
#if !defined(TARGET_SPARC64)
    env->mxccregs[7] = ((cpu + 8) & 0xf) << 24;
#endif
}

static const sparc_def_t sparc_defs[] = {
#ifdef TARGET_SPARC64
    {
        .name = "Fujitsu Sparc64",
784
        .iu_version = ((0x04ULL << 48) | (0x02ULL << 32) | (0ULL << 24)),
785
        .fpu_version = 0x00000000,
786
        .mmu_version = mmu_us_12,
787
        .nwindows = 4,
788
        .maxtl = 4,
B
blueswir1 已提交
789
        .features = CPU_DEFAULT_FEATURES,
790 791 792
    },
    {
        .name = "Fujitsu Sparc64 III",
793
        .iu_version = ((0x04ULL << 48) | (0x03ULL << 32) | (0ULL << 24)),
794
        .fpu_version = 0x00000000,
795
        .mmu_version = mmu_us_12,
796
        .nwindows = 5,
797
        .maxtl = 4,
B
blueswir1 已提交
798
        .features = CPU_DEFAULT_FEATURES,
799 800 801
    },
    {
        .name = "Fujitsu Sparc64 IV",
802
        .iu_version = ((0x04ULL << 48) | (0x04ULL << 32) | (0ULL << 24)),
803
        .fpu_version = 0x00000000,
804
        .mmu_version = mmu_us_12,
805
        .nwindows = 8,
806
        .maxtl = 5,
B
blueswir1 已提交
807
        .features = CPU_DEFAULT_FEATURES,
808 809 810
    },
    {
        .name = "Fujitsu Sparc64 V",
811
        .iu_version = ((0x04ULL << 48) | (0x05ULL << 32) | (0x51ULL << 24)),
812
        .fpu_version = 0x00000000,
813
        .mmu_version = mmu_us_12,
814
        .nwindows = 8,
815
        .maxtl = 5,
B
blueswir1 已提交
816
        .features = CPU_DEFAULT_FEATURES,
817 818 819
    },
    {
        .name = "TI UltraSparc I",
820
        .iu_version = ((0x17ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
821
        .fpu_version = 0x00000000,
822
        .mmu_version = mmu_us_12,
823
        .nwindows = 8,
824
        .maxtl = 5,
B
blueswir1 已提交
825
        .features = CPU_DEFAULT_FEATURES,
826 827 828
    },
    {
        .name = "TI UltraSparc II",
829
        .iu_version = ((0x17ULL << 48) | (0x11ULL << 32) | (0x20ULL << 24)),
830
        .fpu_version = 0x00000000,
831
        .mmu_version = mmu_us_12,
832
        .nwindows = 8,
833
        .maxtl = 5,
B
blueswir1 已提交
834
        .features = CPU_DEFAULT_FEATURES,
835 836 837
    },
    {
        .name = "TI UltraSparc IIi",
838
        .iu_version = ((0x17ULL << 48) | (0x12ULL << 32) | (0x91ULL << 24)),
839
        .fpu_version = 0x00000000,
840
        .mmu_version = mmu_us_12,
841
        .nwindows = 8,
842
        .maxtl = 5,
B
blueswir1 已提交
843
        .features = CPU_DEFAULT_FEATURES,
844 845 846
    },
    {
        .name = "TI UltraSparc IIe",
847
        .iu_version = ((0x17ULL << 48) | (0x13ULL << 32) | (0x14ULL << 24)),
848
        .fpu_version = 0x00000000,
849
        .mmu_version = mmu_us_12,
850
        .nwindows = 8,
851
        .maxtl = 5,
B
blueswir1 已提交
852
        .features = CPU_DEFAULT_FEATURES,
853 854 855
    },
    {
        .name = "Sun UltraSparc III",
856
        .iu_version = ((0x3eULL << 48) | (0x14ULL << 32) | (0x34ULL << 24)),
857
        .fpu_version = 0x00000000,
858
        .mmu_version = mmu_us_12,
859
        .nwindows = 8,
860
        .maxtl = 5,
B
blueswir1 已提交
861
        .features = CPU_DEFAULT_FEATURES,
862 863 864
    },
    {
        .name = "Sun UltraSparc III Cu",
865
        .iu_version = ((0x3eULL << 48) | (0x15ULL << 32) | (0x41ULL << 24)),
866
        .fpu_version = 0x00000000,
867
        .mmu_version = mmu_us_3,
868
        .nwindows = 8,
869
        .maxtl = 5,
B
blueswir1 已提交
870
        .features = CPU_DEFAULT_FEATURES,
871 872 873
    },
    {
        .name = "Sun UltraSparc IIIi",
874
        .iu_version = ((0x3eULL << 48) | (0x16ULL << 32) | (0x34ULL << 24)),
875
        .fpu_version = 0x00000000,
876
        .mmu_version = mmu_us_12,
877
        .nwindows = 8,
878
        .maxtl = 5,
B
blueswir1 已提交
879
        .features = CPU_DEFAULT_FEATURES,
880 881 882
    },
    {
        .name = "Sun UltraSparc IV",
883
        .iu_version = ((0x3eULL << 48) | (0x18ULL << 32) | (0x31ULL << 24)),
884
        .fpu_version = 0x00000000,
885
        .mmu_version = mmu_us_4,
886
        .nwindows = 8,
887
        .maxtl = 5,
B
blueswir1 已提交
888
        .features = CPU_DEFAULT_FEATURES,
889 890 891
    },
    {
        .name = "Sun UltraSparc IV+",
892
        .iu_version = ((0x3eULL << 48) | (0x19ULL << 32) | (0x22ULL << 24)),
893
        .fpu_version = 0x00000000,
894
        .mmu_version = mmu_us_12,
895
        .nwindows = 8,
896
        .maxtl = 5,
897
        .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_CMT,
898 899 900
    },
    {
        .name = "Sun UltraSparc IIIi+",
901
        .iu_version = ((0x3eULL << 48) | (0x22ULL << 32) | (0ULL << 24)),
902
        .fpu_version = 0x00000000,
903
        .mmu_version = mmu_us_3,
904
        .nwindows = 8,
905
        .maxtl = 5,
B
blueswir1 已提交
906
        .features = CPU_DEFAULT_FEATURES,
907
    },
908 909 910
    {
        .name = "Sun UltraSparc T1",
        // defined in sparc_ifu_fdp.v and ctu.h
911
        .iu_version = ((0x3eULL << 48) | (0x23ULL << 32) | (0x02ULL << 24)),
912 913 914
        .fpu_version = 0x00000000,
        .mmu_version = mmu_sun4v,
        .nwindows = 8,
915
        .maxtl = 6,
916 917 918 919 920 921
        .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_HYPV | CPU_FEATURE_CMT
        | CPU_FEATURE_GL,
    },
    {
        .name = "Sun UltraSparc T2",
        // defined in tlu_asi_ctl.v and n2_revid_cust.v
922
        .iu_version = ((0x3eULL << 48) | (0x24ULL << 32) | (0x02ULL << 24)),
923 924 925
        .fpu_version = 0x00000000,
        .mmu_version = mmu_sun4v,
        .nwindows = 8,
926
        .maxtl = 6,
927 928 929
        .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_HYPV | CPU_FEATURE_CMT
        | CPU_FEATURE_GL,
    },
930 931
    {
        .name = "NEC UltraSparc I",
932
        .iu_version = ((0x22ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
933
        .fpu_version = 0x00000000,
934
        .mmu_version = mmu_us_12,
935
        .nwindows = 8,
936
        .maxtl = 5,
B
blueswir1 已提交
937
        .features = CPU_DEFAULT_FEATURES,
938 939 940 941 942 943 944 945 946 947 948 949
    },
#else
    {
        .name = "Fujitsu MB86900",
        .iu_version = 0x00 << 24, /* Impl 0, ver 0 */
        .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
        .mmu_version = 0x00 << 24, /* Impl 0, ver 0 */
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x007ffff0,
        .mmu_cxr_mask = 0x0000003f,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
950
        .nwindows = 7,
B
blueswir1 已提交
951
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_FSMULD,
952 953 954 955 956 957 958 959 960 961 962
    },
    {
        .name = "Fujitsu MB86904",
        .iu_version = 0x04 << 24, /* Impl 0, ver 4 */
        .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
        .mmu_version = 0x04 << 24, /* Impl 0, ver 4 */
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x00ffffc0,
        .mmu_cxr_mask = 0x000000ff,
        .mmu_sfsr_mask = 0x00016fff,
        .mmu_trcr_mask = 0x00ffffff,
963
        .nwindows = 8,
B
blueswir1 已提交
964
        .features = CPU_DEFAULT_FEATURES,
965 966 967 968 969 970 971 972 973 974 975
    },
    {
        .name = "Fujitsu MB86907",
        .iu_version = 0x05 << 24, /* Impl 0, ver 5 */
        .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
        .mmu_version = 0x05 << 24, /* Impl 0, ver 5 */
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0xffffffc0,
        .mmu_cxr_mask = 0x000000ff,
        .mmu_sfsr_mask = 0x00016fff,
        .mmu_trcr_mask = 0xffffffff,
976
        .nwindows = 8,
B
blueswir1 已提交
977
        .features = CPU_DEFAULT_FEATURES,
978 979 980 981 982 983 984 985 986 987 988
    },
    {
        .name = "LSI L64811",
        .iu_version = 0x10 << 24, /* Impl 1, ver 0 */
        .fpu_version = 1 << 17, /* FPU version 1 (LSI L64814) */
        .mmu_version = 0x10 << 24,
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x007ffff0,
        .mmu_cxr_mask = 0x0000003f,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
989
        .nwindows = 8,
B
blueswir1 已提交
990 991
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
        CPU_FEATURE_FSMULD,
992 993 994 995 996 997 998 999 1000 1001 1002
    },
    {
        .name = "Cypress CY7C601",
        .iu_version = 0x11 << 24, /* Impl 1, ver 1 */
        .fpu_version = 3 << 17, /* FPU version 3 (Cypress CY7C602) */
        .mmu_version = 0x10 << 24,
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x007ffff0,
        .mmu_cxr_mask = 0x0000003f,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1003
        .nwindows = 8,
B
blueswir1 已提交
1004 1005
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
        CPU_FEATURE_FSMULD,
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
    },
    {
        .name = "Cypress CY7C611",
        .iu_version = 0x13 << 24, /* Impl 1, ver 3 */
        .fpu_version = 3 << 17, /* FPU version 3 (Cypress CY7C602) */
        .mmu_version = 0x10 << 24,
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x007ffff0,
        .mmu_cxr_mask = 0x0000003f,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1017
        .nwindows = 8,
B
blueswir1 已提交
1018 1019
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
        CPU_FEATURE_FSMULD,
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
    },
    {
        .name = "TI MicroSparc I",
        .iu_version = 0x41000000,
        .fpu_version = 4 << 17,
        .mmu_version = 0x41000000,
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x007ffff0,
        .mmu_cxr_mask = 0x0000003f,
        .mmu_sfsr_mask = 0x00016fff,
        .mmu_trcr_mask = 0x0000003f,
1031
        .nwindows = 7,
B
blueswir1 已提交
1032 1033 1034
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_MUL |
        CPU_FEATURE_DIV | CPU_FEATURE_FLUSH | CPU_FEATURE_FSQRT |
        CPU_FEATURE_FMUL,
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
    },
    {
        .name = "TI MicroSparc II",
        .iu_version = 0x42000000,
        .fpu_version = 4 << 17,
        .mmu_version = 0x02000000,
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x00ffffc0,
        .mmu_cxr_mask = 0x000000ff,
        .mmu_sfsr_mask = 0x00016fff,
        .mmu_trcr_mask = 0x00ffffff,
1046
        .nwindows = 8,
B
blueswir1 已提交
1047
        .features = CPU_DEFAULT_FEATURES,
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
    },
    {
        .name = "TI MicroSparc IIep",
        .iu_version = 0x42000000,
        .fpu_version = 4 << 17,
        .mmu_version = 0x04000000,
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x00ffffc0,
        .mmu_cxr_mask = 0x000000ff,
        .mmu_sfsr_mask = 0x00016bff,
        .mmu_trcr_mask = 0x00ffffff,
1059
        .nwindows = 8,
B
blueswir1 已提交
1060
        .features = CPU_DEFAULT_FEATURES,
1061
    },
B
blueswir1 已提交
1062 1063
    {
        .name = "TI SuperSparc 40", // STP1020NPGA
1064
        .iu_version = 0x41000000, // SuperSPARC 2.x
B
blueswir1 已提交
1065
        .fpu_version = 0 << 17,
1066
        .mmu_version = 0x00000800, // SuperSPARC 2.x, no MXCC
B
blueswir1 已提交
1067 1068 1069 1070 1071
        .mmu_bm = 0x00002000,
        .mmu_ctpr_mask = 0xffffffc0,
        .mmu_cxr_mask = 0x0000ffff,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1072
        .nwindows = 8,
B
blueswir1 已提交
1073 1074 1075 1076
        .features = CPU_DEFAULT_FEATURES,
    },
    {
        .name = "TI SuperSparc 50", // STP1020PGA
1077
        .iu_version = 0x40000000, // SuperSPARC 3.x
B
blueswir1 已提交
1078
        .fpu_version = 0 << 17,
1079
        .mmu_version = 0x01000800, // SuperSPARC 3.x, no MXCC
B
blueswir1 已提交
1080 1081 1082 1083 1084
        .mmu_bm = 0x00002000,
        .mmu_ctpr_mask = 0xffffffc0,
        .mmu_cxr_mask = 0x0000ffff,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1085
        .nwindows = 8,
B
blueswir1 已提交
1086 1087
        .features = CPU_DEFAULT_FEATURES,
    },
1088 1089
    {
        .name = "TI SuperSparc 51",
1090
        .iu_version = 0x40000000, // SuperSPARC 3.x
1091
        .fpu_version = 0 << 17,
1092
        .mmu_version = 0x01000000, // SuperSPARC 3.x, MXCC
1093 1094 1095 1096 1097
        .mmu_bm = 0x00002000,
        .mmu_ctpr_mask = 0xffffffc0,
        .mmu_cxr_mask = 0x0000ffff,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1098
        .mxcc_version = 0x00000104,
1099
        .nwindows = 8,
B
blueswir1 已提交
1100
        .features = CPU_DEFAULT_FEATURES,
1101
    },
B
blueswir1 已提交
1102 1103
    {
        .name = "TI SuperSparc 60", // STP1020APGA
1104
        .iu_version = 0x40000000, // SuperSPARC 3.x
B
blueswir1 已提交
1105
        .fpu_version = 0 << 17,
1106
        .mmu_version = 0x01000800, // SuperSPARC 3.x, no MXCC
B
blueswir1 已提交
1107 1108 1109 1110 1111
        .mmu_bm = 0x00002000,
        .mmu_ctpr_mask = 0xffffffc0,
        .mmu_cxr_mask = 0x0000ffff,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1112
        .nwindows = 8,
B
blueswir1 已提交
1113 1114
        .features = CPU_DEFAULT_FEATURES,
    },
1115 1116
    {
        .name = "TI SuperSparc 61",
1117
        .iu_version = 0x44000000, // SuperSPARC 3.x
1118
        .fpu_version = 0 << 17,
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
        .mmu_version = 0x01000000, // SuperSPARC 3.x, MXCC
        .mmu_bm = 0x00002000,
        .mmu_ctpr_mask = 0xffffffc0,
        .mmu_cxr_mask = 0x0000ffff,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
        .mxcc_version = 0x00000104,
        .nwindows = 8,
        .features = CPU_DEFAULT_FEATURES,
    },
    {
        .name = "TI SuperSparc II",
        .iu_version = 0x40000000, // SuperSPARC II 1.x
        .fpu_version = 0 << 17,
        .mmu_version = 0x08000000, // SuperSPARC II 1.x, MXCC
1134 1135 1136 1137 1138
        .mmu_bm = 0x00002000,
        .mmu_ctpr_mask = 0xffffffc0,
        .mmu_cxr_mask = 0x0000ffff,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1139
        .mxcc_version = 0x00000104,
1140
        .nwindows = 8,
B
blueswir1 已提交
1141
        .features = CPU_DEFAULT_FEATURES,
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
    },
    {
        .name = "Ross RT625",
        .iu_version = 0x1e000000,
        .fpu_version = 1 << 17,
        .mmu_version = 0x1e000000,
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x007ffff0,
        .mmu_cxr_mask = 0x0000003f,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1153
        .nwindows = 8,
B
blueswir1 已提交
1154
        .features = CPU_DEFAULT_FEATURES,
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
    },
    {
        .name = "Ross RT620",
        .iu_version = 0x1f000000,
        .fpu_version = 1 << 17,
        .mmu_version = 0x1f000000,
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x007ffff0,
        .mmu_cxr_mask = 0x0000003f,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1166
        .nwindows = 8,
B
blueswir1 已提交
1167
        .features = CPU_DEFAULT_FEATURES,
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
    },
    {
        .name = "BIT B5010",
        .iu_version = 0x20000000,
        .fpu_version = 0 << 17, /* B5010/B5110/B5120/B5210 */
        .mmu_version = 0x20000000,
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x007ffff0,
        .mmu_cxr_mask = 0x0000003f,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1179
        .nwindows = 8,
B
blueswir1 已提交
1180 1181
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
        CPU_FEATURE_FSMULD,
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
    },
    {
        .name = "Matsushita MN10501",
        .iu_version = 0x50000000,
        .fpu_version = 0 << 17,
        .mmu_version = 0x50000000,
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x007ffff0,
        .mmu_cxr_mask = 0x0000003f,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1193
        .nwindows = 8,
B
blueswir1 已提交
1194 1195
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_MUL | CPU_FEATURE_FSQRT |
        CPU_FEATURE_FSMULD,
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
    },
    {
        .name = "Weitek W8601",
        .iu_version = 0x90 << 24, /* Impl 9, ver 0 */
        .fpu_version = 3 << 17, /* FPU version 3 (Weitek WTL3170/2) */
        .mmu_version = 0x10 << 24,
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x007ffff0,
        .mmu_cxr_mask = 0x0000003f,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1207
        .nwindows = 8,
B
blueswir1 已提交
1208
        .features = CPU_DEFAULT_FEATURES,
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
    },
    {
        .name = "LEON2",
        .iu_version = 0xf2000000,
        .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
        .mmu_version = 0xf2000000,
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x007ffff0,
        .mmu_cxr_mask = 0x0000003f,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1220
        .nwindows = 8,
B
blueswir1 已提交
1221
        .features = CPU_DEFAULT_FEATURES,
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
    },
    {
        .name = "LEON3",
        .iu_version = 0xf3000000,
        .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
        .mmu_version = 0xf3000000,
        .mmu_bm = 0x00004000,
        .mmu_ctpr_mask = 0x007ffff0,
        .mmu_cxr_mask = 0x0000003f,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1233
        .nwindows = 8,
B
blueswir1 已提交
1234
        .features = CPU_DEFAULT_FEATURES,
1235 1236 1237 1238
    },
#endif
};

B
blueswir1 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
static const char * const feature_name[] = {
    "float",
    "float128",
    "swap",
    "mul",
    "div",
    "flush",
    "fsqrt",
    "fmul",
    "vis1",
    "vis2",
B
blueswir1 已提交
1250
    "fsmuld",
1251 1252 1253
    "hypv",
    "cmt",
    "gl",
B
blueswir1 已提交
1254 1255 1256 1257 1258
};

static void print_features(FILE *f,
                           int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                           uint32_t features, const char *prefix)
1259 1260 1261
{
    unsigned int i;

B
blueswir1 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
    for (i = 0; i < ARRAY_SIZE(feature_name); i++)
        if (feature_name[i] && (features & (1 << i))) {
            if (prefix)
                (*cpu_fprintf)(f, "%s", prefix);
            (*cpu_fprintf)(f, "%s ", feature_name[i]);
        }
}

static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features)
{
    unsigned int i;

    for (i = 0; i < ARRAY_SIZE(feature_name); i++)
        if (feature_name[i] && !strcmp(flagname, feature_name[i])) {
            *features |= 1 << i;
            return;
        }
    fprintf(stderr, "CPU feature %s not found\n", flagname);
}

B
blueswir1 已提交
1282
static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model)
B
blueswir1 已提交
1283 1284 1285 1286 1287 1288 1289 1290
{
    unsigned int i;
    const sparc_def_t *def = NULL;
    char *s = strdup(cpu_model);
    char *featurestr, *name = strtok(s, ",");
    uint32_t plus_features = 0;
    uint32_t minus_features = 0;
    long long iu_version;
1291
    uint32_t fpu_version, mmu_version, nwindows;
B
blueswir1 已提交
1292

1293
    for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
1294
        if (strcasecmp(name, sparc_defs[i].name) == 0) {
B
blueswir1 已提交
1295
            def = &sparc_defs[i];
1296 1297
        }
    }
B
blueswir1 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
    if (!def)
        goto error;
    memcpy(cpu_def, def, sizeof(*def));

    featurestr = strtok(NULL, ",");
    while (featurestr) {
        char *val;

        if (featurestr[0] == '+') {
            add_flagname_to_bitmaps(featurestr + 1, &plus_features);
        } else if (featurestr[0] == '-') {
            add_flagname_to_bitmaps(featurestr + 1, &minus_features);
        } else if ((val = strchr(featurestr, '='))) {
            *val = 0; val++;
            if (!strcmp(featurestr, "iu_version")) {
                char *err;

                iu_version = strtoll(val, &err, 0);
                if (!*val || *err) {
                    fprintf(stderr, "bad numerical value %s\n", val);
                    goto error;
                }
                cpu_def->iu_version = iu_version;
#ifdef DEBUG_FEATURES
                fprintf(stderr, "iu_version %llx\n", iu_version);
#endif
            } else if (!strcmp(featurestr, "fpu_version")) {
                char *err;

                fpu_version = strtol(val, &err, 0);
                if (!*val || *err) {
                    fprintf(stderr, "bad numerical value %s\n", val);
                    goto error;
                }
                cpu_def->fpu_version = fpu_version;
#ifdef DEBUG_FEATURES
1334
                fprintf(stderr, "fpu_version %x\n", fpu_version);
B
blueswir1 已提交
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
#endif
            } else if (!strcmp(featurestr, "mmu_version")) {
                char *err;

                mmu_version = strtol(val, &err, 0);
                if (!*val || *err) {
                    fprintf(stderr, "bad numerical value %s\n", val);
                    goto error;
                }
                cpu_def->mmu_version = mmu_version;
#ifdef DEBUG_FEATURES
1346
                fprintf(stderr, "mmu_version %x\n", mmu_version);
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
#endif
            } else if (!strcmp(featurestr, "nwindows")) {
                char *err;

                nwindows = strtol(val, &err, 0);
                if (!*val || *err || nwindows > MAX_NWINDOWS ||
                    nwindows < MIN_NWINDOWS) {
                    fprintf(stderr, "bad numerical value %s\n", val);
                    goto error;
                }
                cpu_def->nwindows = nwindows;
#ifdef DEBUG_FEATURES
                fprintf(stderr, "nwindows %d\n", nwindows);
B
blueswir1 已提交
1360 1361 1362 1363 1364 1365
#endif
            } else {
                fprintf(stderr, "unrecognized feature %s\n", featurestr);
                goto error;
            }
        } else {
B
blueswir1 已提交
1366 1367
            fprintf(stderr, "feature string `%s' not in format "
                    "(+feature|-feature|feature=xyz)\n", featurestr);
B
blueswir1 已提交
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
            goto error;
        }
        featurestr = strtok(NULL, ",");
    }
    cpu_def->features |= plus_features;
    cpu_def->features &= ~minus_features;
#ifdef DEBUG_FEATURES
    print_features(stderr, fprintf, cpu_def->features, NULL);
#endif
    free(s);
    return 0;

 error:
    free(s);
    return -1;
1383 1384
}

B
blueswir1 已提交
1385
void sparc_cpu_list(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
1386 1387 1388
{
    unsigned int i;

1389
    for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
1390
        (*cpu_fprintf)(f, "Sparc %16s IU " TARGET_FMT_lx " FPU %08x MMU %08x NWINS %d ",
1391 1392 1393
                       sparc_defs[i].name,
                       sparc_defs[i].iu_version,
                       sparc_defs[i].fpu_version,
1394 1395
                       sparc_defs[i].mmu_version,
                       sparc_defs[i].nwindows);
B
blueswir1 已提交
1396 1397 1398 1399
        print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES &
                       ~sparc_defs[i].features, "-");
        print_features(f, cpu_fprintf, ~CPU_DEFAULT_FEATURES &
                       sparc_defs[i].features, "+");
B
blueswir1 已提交
1400
        (*cpu_fprintf)(f, "\n");
1401
    }
1402 1403
    (*cpu_fprintf)(f, "Default CPU feature flags (use '-' to remove): ");
    print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES, NULL);
B
blueswir1 已提交
1404
    (*cpu_fprintf)(f, "\n");
1405 1406 1407 1408 1409
    (*cpu_fprintf)(f, "Available CPU feature flags (use '+' to add): ");
    print_features(f, cpu_fprintf, ~CPU_DEFAULT_FEATURES, NULL);
    (*cpu_fprintf)(f, "\n");
    (*cpu_fprintf)(f, "Numerical features (use '=' to set): iu_version "
                   "fpu_version mmu_version nwindows\n");
1410 1411
}

B
Blue Swirl 已提交
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
static void cpu_print_cc(FILE *f,
                         int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                         uint32_t cc)
{
    cpu_fprintf(f, "%c%c%c%c", cc & PSR_NEG? 'N' : '-',
                cc & PSR_ZERO? 'Z' : '-', cc & PSR_OVF? 'V' : '-',
                cc & PSR_CARRY? 'C' : '-');
}

#ifdef TARGET_SPARC64
#define REGS_PER_LINE 4
#else
#define REGS_PER_LINE 8
#endif

1427 1428 1429 1430 1431 1432
void cpu_dump_state(CPUState *env, FILE *f,
                    int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                    int flags)
{
    int i, x;

B
blueswir1 已提交
1433 1434
    cpu_fprintf(f, "pc: " TARGET_FMT_lx "  npc: " TARGET_FMT_lx "\n", env->pc,
                env->npc);
1435
    cpu_fprintf(f, "General Registers:\n");
B
Blue Swirl 已提交
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445

    for (i = 0; i < 8; i++) {
        if (i % REGS_PER_LINE == 0) {
            cpu_fprintf(f, "%%g%d-%d:", i, i + REGS_PER_LINE - 1);
        }
        cpu_fprintf(f, " " TARGET_FMT_lx, env->gregs[i]);
        if (i % REGS_PER_LINE == REGS_PER_LINE - 1) {
            cpu_fprintf(f, "\n");
        }
    }
1446 1447
    cpu_fprintf(f, "\nCurrent Register Window:\n");
    for (x = 0; x < 3; x++) {
B
Blue Swirl 已提交
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
        for (i = 0; i < 8; i++) {
            if (i % REGS_PER_LINE == 0) {
                cpu_fprintf(f, "%%%c%d-%d: ",
                            x == 0 ? 'o' : (x == 1 ? 'l' : 'i'),
                            i, i + REGS_PER_LINE - 1);
            }
            cpu_fprintf(f, TARGET_FMT_lx " ", env->regwptr[i + x * 8]);
            if (i % REGS_PER_LINE == REGS_PER_LINE - 1) {
                cpu_fprintf(f, "\n");
            }
        }
1459 1460
    }
    cpu_fprintf(f, "\nFloating Point Registers:\n");
B
Blue Swirl 已提交
1461
    for (i = 0; i < TARGET_FPREGS; i++) {
1462 1463
        if ((i & 3) == 0)
            cpu_fprintf(f, "%%f%02d:", i);
1464
        cpu_fprintf(f, " %016f", *(float *)&env->fpr[i]);
1465 1466 1467 1468
        if ((i & 3) == 3)
            cpu_fprintf(f, "\n");
    }
#ifdef TARGET_SPARC64
B
Blue Swirl 已提交
1469 1470 1471 1472 1473 1474 1475 1476 1477
    cpu_fprintf(f, "pstate: %08x ccr: %02x (icc: ", env->pstate,
                GET_CCR(env));
    cpu_print_cc(f, cpu_fprintf, GET_CCR(env) << PSR_CARRY_SHIFT);
    cpu_fprintf(f, " xcc: ");
    cpu_print_cc(f, cpu_fprintf, GET_CCR(env) << (PSR_CARRY_SHIFT - 4));
    cpu_fprintf(f, ") asi: %02x tl: %d pil: %x\n", env->asi, env->tl,
                env->psrpil);
    cpu_fprintf(f, "cansave: %d canrestore: %d otherwin: %d wstate: %d "
                "cleanwin: %d cwp: %d\n",
1478
                env->cansave, env->canrestore, env->otherwin, env->wstate,
1479
                env->cleanwin, env->nwindows - 1 - env->cwp);
B
Blue Swirl 已提交
1480 1481
    cpu_fprintf(f, "fsr: " TARGET_FMT_lx " y: " TARGET_FMT_lx " fprs: "
                TARGET_FMT_lx "\n", env->fsr, env->y, env->fprs);
1482
#else
B
Blue Swirl 已提交
1483 1484 1485 1486 1487 1488 1489
    cpu_fprintf(f, "psr: %08x (icc: ", GET_PSR(env));
    cpu_print_cc(f, cpu_fprintf, GET_PSR(env));
    cpu_fprintf(f, " SPE: %c%c%c) wim: %08x\n", env->psrs? 'S' : '-',
                env->psrps? 'P' : '-', env->psret? 'E' : '-',
                env->wim);
    cpu_fprintf(f, "fsr: " TARGET_FMT_lx " y: " TARGET_FMT_lx "\n",
                env->fsr, env->y);
1490 1491
#endif
}