helper.c 56.6 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
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

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

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

32 33 34 35 36 37 38
#ifdef DEBUG_MMU
#define DPRINTF_MMU(fmt, ...) \
    do { printf("MMU: " fmt , ## __VA_ARGS__); } while (0)
#else
#define DPRINTF_MMU(fmt, ...) do {} while (0)
#endif

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

41 42
/* Sparc MMU emulation */

43
#if defined(CONFIG_USER_ONLY)
B
bellard 已提交
44

B
blueswir1 已提交
45
int cpu_sparc_handle_mmu_fault(CPUState *env1, target_ulong address, int rw,
46
                               int mmu_idx, int is_softmmu)
B
bellard 已提交
47
{
B
bellard 已提交
48
    if (rw & 2)
B
blueswir1 已提交
49
        env1->exception_index = TT_TFAULT;
B
bellard 已提交
50
    else
B
blueswir1 已提交
51
        env1->exception_index = TT_DFAULT;
B
bellard 已提交
52 53 54 55
    return 1;
}

#else
56

B
bellard 已提交
57
#ifndef TARGET_SPARC64
B
bellard 已提交
58 59 60
/*
 * Sparc V8 Reference MMU (SRMMU)
 */
61
static const int access_table[8][8] = {
62 63 64 65 66 67 68 69
    { 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 }
70 71
};

B
bellard 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
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,
    }
93 94
};

A
Anthony Liguori 已提交
95
static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
96
                                int *prot, int *access_index,
P
Paul Brook 已提交
97 98
                                target_ulong address, int rw, int mmu_idx,
                                target_ulong *page_size)
99
{
B
bellard 已提交
100
    int access_perms = 0;
A
Anthony Liguori 已提交
101
    target_phys_addr_t pde_ptr;
102
    uint32_t pde;
103
    int error_code = 0, is_dirty, is_user;
B
bellard 已提交
104
    unsigned long page_offset;
105

106
    is_user = mmu_idx == MMU_USER_IDX;
B
blueswir1 已提交
107

108
    if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
P
Paul Brook 已提交
109
        *page_size = TARGET_PAGE_SIZE;
B
blueswir1 已提交
110
        // Boot mode: instruction fetches are taken from PROM
111
        if (rw == 2 && (env->mmuregs[0] & env->def->mmu_bm)) {
B
blueswir1 已提交
112
            *physical = env->prom_addr | (address & 0x7ffffULL);
B
blueswir1 已提交
113 114 115
            *prot = PAGE_READ | PAGE_EXEC;
            return 0;
        }
B
blueswir1 已提交
116
        *physical = address;
B
bellard 已提交
117
        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
B
bellard 已提交
118
        return 0;
119 120
    }

B
bellard 已提交
121
    *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user? 0 : 1);
122
    *physical = 0xffffffffffff0000ULL;
B
bellard 已提交
123

124 125
    /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
    /* Context base + context number */
126
    pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
B
bellard 已提交
127
    pde = ldl_phys(pde_ptr);
128 129 130

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

B
blueswir1 已提交
141 142 143 144 145 146 147 148
        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 已提交
149
            pde = ldl_phys(pde_ptr);
150

B
blueswir1 已提交
151 152 153 154 155 156 157 158
            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 已提交
159
                pde = ldl_phys(pde_ptr);
160

B
blueswir1 已提交
161 162 163 164 165 166 167 168
                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 已提交
169 170
                    page_offset = (address & TARGET_PAGE_MASK) &
                        (TARGET_PAGE_SIZE - 1);
B
blueswir1 已提交
171
                }
P
Paul Brook 已提交
172
                *page_size = TARGET_PAGE_SIZE;
B
blueswir1 已提交
173 174 175
                break;
            case 2: /* L2 PTE */
                page_offset = address & 0x3ffff;
P
Paul Brook 已提交
176
                *page_size = 0x40000;
B
blueswir1 已提交
177 178 179 180
            }
            break;
        case 2: /* L1 PTE */
            page_offset = address & 0xffffff;
P
Paul Brook 已提交
181
            *page_size = 0x1000000;
B
blueswir1 已提交
182
        }
183 184
    }

185 186 187 188 189 190
    /* check access */
    access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
    error_code = access_table[*access_index][access_perms];
    if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user))
        return error_code;

191
    /* update page modified and dirty bits */
B
bellard 已提交
192
    is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
193
    if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
B
blueswir1 已提交
194 195 196
        pde |= PG_ACCESSED_MASK;
        if (is_dirty)
            pde |= PG_MODIFIED_MASK;
B
bellard 已提交
197
        stl_phys_notdirty(pde_ptr, pde);
198 199 200
    }

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

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

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

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

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

B
bellard 已提交
241
    if ((env->mmuregs[0] & MMU_NF) || env->psret == 0)  {
B
bellard 已提交
242 243 244 245
        // 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 已提交
246
        vaddr = address & TARGET_PAGE_MASK;
B
bellard 已提交
247
        prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
P
Paul Brook 已提交
248 249
        tlb_set_page(env, vaddr, paddr, prot, mmu_idx, TARGET_PAGE_SIZE);
        return 0;
B
bellard 已提交
250 251 252 253 254 255
    } else {
        if (rw & 2)
            env->exception_index = TT_TFAULT;
        else
            env->exception_index = TT_DFAULT;
        return 1;
B
bellard 已提交
256
    }
257
}
258 259 260

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

    /* Context base + context number */
A
Anthony Liguori 已提交
265
    pde_ptr = (target_phys_addr_t)(env->mmuregs[1] << 4) +
266
        (env->mmuregs[2] << 2);
267 268 269 270 271 272 273
    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 已提交
274
        return 0;
275
    case 1: /* L1 PDE */
B
blueswir1 已提交
276 277 278
        if (mmulev == 3)
            return pde;
        pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
279 280
        pde = ldl_phys(pde_ptr);

B
blueswir1 已提交
281 282 283 284 285 286 287 288 289 290 291
        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);
292 293
            pde = ldl_phys(pde_ptr);

B
blueswir1 已提交
294 295 296 297 298 299 300 301 302 303 304
            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);
305 306
                pde = ldl_phys(pde_ptr);

B
blueswir1 已提交
307 308 309 310 311 312 313 314 315 316 317
                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;
                }
            }
        }
318 319 320 321
    }
    return 0;
}

322
void dump_mmu(FILE *f, fprintf_function cpu_fprintf, CPUState *env)
323
{
324 325
    target_ulong va, va1, va2;
    unsigned int n, m, o;
A
Anthony Liguori 已提交
326
    target_phys_addr_t pde_ptr, pa;
327 328 329 330
    uint32_t pde;

    pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
    pde = ldl_phys(pde_ptr);
331 332
    (*cpu_fprintf)(f, "Root ptr: " TARGET_FMT_plx ", ctx: %d\n",
                   (target_phys_addr_t)env->mmuregs[1] << 4, env->mmuregs[2]);
333
    for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
B
blueswir1 已提交
334 335 336
        pde = mmu_probe(env, va, 2);
        if (pde) {
            pa = cpu_get_phys_page_debug(env, va);
337 338
            (*cpu_fprintf)(f, "VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
                           " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
B
blueswir1 已提交
339 340 341 342
            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);
343 344 345
                    (*cpu_fprintf)(f, " VA: " TARGET_FMT_lx ", PA: "
                                   TARGET_FMT_plx " PDE: " TARGET_FMT_lx "\n",
                                   va1, pa, pde);
B
blueswir1 已提交
346 347 348 349
                    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);
350 351 352 353
                            (*cpu_fprintf)(f, "  VA: " TARGET_FMT_lx ", PA: "
                                           TARGET_FMT_plx " PTE: "
                                           TARGET_FMT_lx "\n",
                                           va2, pa, pde);
B
blueswir1 已提交
354 355 356 357 358
                        }
                    }
                }
            }
        }
359 360 361 362
    }
}

#else /* !TARGET_SPARC64 */
363 364

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

B
bellard 已提交
370 371 372
/*
 * UltraSparc IIi I/DMMUs
 */
373 374 375

// Returns true if TTE tag is valid and matches virtual address value in context
// requires virtual address mask value calculated from TTE entry size
376
static inline int ultrasparc_tag_match(SparcTLBEntry *tlb,
377
                                       uint64_t address, uint64_t context,
378
                                       target_phys_addr_t *physical)
379 380 381
{
    uint64_t mask;

382
    switch ((tlb->tte >> 61) & 3) {
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    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?
399
    if (TTE_IS_VALID(tlb->tte) &&
400
        (TTE_IS_GLOBAL(tlb->tte) || tlb_compare_context(tlb, context))
401
        && compare_masked(address, tlb->tag, mask))
402 403
    {
        // decode physical address
404
        *physical = ((tlb->tte & mask) | (address & ~mask)) & 0x1ffffffe000ULL;
405 406 407 408 409 410
        return 1;
    }

    return 0;
}

B
blueswir1 已提交
411
static int get_physical_address_data(CPUState *env,
A
Anthony Liguori 已提交
412
                                     target_phys_addr_t *physical, int *prot,
413
                                     target_ulong address, int rw, int mmu_idx)
B
bellard 已提交
414 415
{
    unsigned int i;
416
    uint64_t context;
B
bellard 已提交
417

418 419 420
    int is_user = (mmu_idx == MMU_USER_IDX ||
                   mmu_idx == MMU_USER_SECONDARY_IDX);

B
bellard 已提交
421
    if ((env->lsu & DMMU_E) == 0) { /* DMMU disabled */
422
        *physical = ultrasparc_truncate_physical(address);
B
blueswir1 已提交
423
        *prot = PAGE_READ | PAGE_WRITE;
B
bellard 已提交
424 425 426
        return 0;
    }

427 428 429
    switch(mmu_idx) {
    case MMU_USER_IDX:
    case MMU_KERNEL_IDX:
430
        context = env->dmmu.mmu_primary_context & 0x1fff;
431 432 433 434 435 436
        break;
    case MMU_USER_SECONDARY_IDX:
    case MMU_KERNEL_SECONDARY_IDX:
        context = env->dmmu.mmu_secondary_context & 0x1fff;
        break;
    case MMU_NUCLEUS_IDX:
B
Blue Swirl 已提交
437
    default:
438
        context = 0;
439
        break;
440
    }
441

B
bellard 已提交
442
    for (i = 0; i < 64; i++) {
B
blueswir1 已提交
443
        // ctx match, vaddr match, valid?
444 445 446 447
        if (ultrasparc_tag_match(&env->dtlb[i], address, context, physical)) {

            uint8_t fault_type = 0;

B
blueswir1 已提交
448
            // access ok?
449 450 451
            if ((env->dtlb[i].tte & 0x4) && is_user) {
                fault_type |= 1; /* privilege violation */
                env->exception_index = TT_DFAULT;
452

453 454 455 456 457
                DPRINTF_MMU("DFAULT at %" PRIx64 " context %" PRIx64
                            " mmu_idx=%d tl=%d\n",
                            address, context, mmu_idx, env->tl);
            } else if (!(env->dtlb[i].tte & 0x2) && (rw == 1)) {
                env->exception_index = TT_DPROT;
458

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
                DPRINTF_MMU("DPROT at %" PRIx64 " context %" PRIx64
                            " mmu_idx=%d tl=%d\n",
                            address, context, mmu_idx, env->tl);
            } else {
                *prot = PAGE_READ;
                if (env->dtlb[i].tte & 0x2)
                    *prot |= PAGE_WRITE;

                TTE_SET_USED(env->dtlb[i].tte);

                return 0;
            }

            if (env->dmmu.sfsr & 1) /* Fault status register */
                env->dmmu.sfsr = 2; /* overflow (not read before
B
blueswir1 已提交
474
                                             another fault) */
475

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

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

480
            env->dmmu.sfar = address; /* Fault address register */
481 482 483

            env->dmmu.tag_access = (address & ~0x1fffULL) | context;

484
            return 1;
B
blueswir1 已提交
485
        }
B
bellard 已提交
486
    }
487 488 489 490

    DPRINTF_MMU("DMISS at %" PRIx64 " context %" PRIx64 "\n",
                address, context);

491
    env->dmmu.tag_access = (address & ~0x1fffULL) | context;
B
bellard 已提交
492
    env->exception_index = TT_DMISS;
B
bellard 已提交
493 494 495
    return 1;
}

B
blueswir1 已提交
496
static int get_physical_address_code(CPUState *env,
A
Anthony Liguori 已提交
497
                                     target_phys_addr_t *physical, int *prot,
498
                                     target_ulong address, int mmu_idx)
B
bellard 已提交
499 500
{
    unsigned int i;
501
    uint64_t context;
B
bellard 已提交
502

503 504 505
    int is_user = (mmu_idx == MMU_USER_IDX ||
                   mmu_idx == MMU_USER_SECONDARY_IDX);

506 507 508
    if ((env->lsu & IMMU_E) == 0 || (env->pstate & PS_RED) != 0) {
        /* IMMU disabled */
        *physical = ultrasparc_truncate_physical(address);
B
blueswir1 已提交
509
        *prot = PAGE_EXEC;
B
bellard 已提交
510 511
        return 0;
    }
B
bellard 已提交
512

513
    if (env->tl == 0) {
514
        /* PRIMARY context */
515 516
        context = env->dmmu.mmu_primary_context & 0x1fff;
    } else {
517
        /* NUCLEUS context */
518 519
        context = 0;
    }
520

B
bellard 已提交
521
    for (i = 0; i < 64; i++) {
B
blueswir1 已提交
522
        // ctx match, vaddr match, valid?
523
        if (ultrasparc_tag_match(&env->itlb[i],
524
                                 address, context, physical)) {
B
blueswir1 已提交
525
            // access ok?
526 527 528
            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 已提交
529
                                             another fault) */
530
                env->immu.sfsr |= (is_user << 3) | 1;
B
blueswir1 已提交
531
                env->exception_index = TT_TFAULT;
532

533 534
                env->immu.tag_access = (address & ~0x1fffULL) | context;

535 536 537
                DPRINTF_MMU("TFAULT at %" PRIx64 " context %" PRIx64 "\n",
                            address, context);

B
blueswir1 已提交
538 539 540
                return 1;
            }
            *prot = PAGE_EXEC;
541
            TTE_SET_USED(env->itlb[i].tte);
B
blueswir1 已提交
542 543
            return 0;
        }
B
bellard 已提交
544
    }
545 546 547 548

    DPRINTF_MMU("TMISS at %" PRIx64 " context %" PRIx64 "\n",
                address, context);

B
Blue Swirl 已提交
549
    /* Context is stored in DMMU (dmmuregs[1]) also for IMMU */
550
    env->immu.tag_access = (address & ~0x1fffULL) | context;
B
bellard 已提交
551
    env->exception_index = TT_TMISS;
B
bellard 已提交
552 553 554
    return 1;
}

A
Anthony Liguori 已提交
555
static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
556
                                int *prot, int *access_index,
P
Paul Brook 已提交
557 558
                                target_ulong address, int rw, int mmu_idx,
                                target_ulong *page_size)
B
bellard 已提交
559
{
P
Paul Brook 已提交
560 561 562
    /* ??? We treat everything as a small page, then explicitly flush
       everything when an entry is evicted.  */
    *page_size = TARGET_PAGE_SIZE;
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

#if defined (DEBUG_MMU)
    /* safety net to catch wrong softmmu index use from dynamic code */
    if (env->tl > 0 && mmu_idx != MMU_NUCLEUS_IDX) {
        DPRINTF_MMU("get_physical_address %s tl=%d mmu_idx=%d"
                    " primary context=%" PRIx64
                    " secondary context=%" PRIx64
                " address=%" PRIx64
                "\n",
                (rw == 2 ? "CODE" : "DATA"),
                env->tl, mmu_idx,
                env->dmmu.mmu_primary_context,
                env->dmmu.mmu_secondary_context,
                address);
    }
#endif

B
bellard 已提交
580
    if (rw == 2)
B
blueswir1 已提交
581
        return get_physical_address_code(env, physical, prot, address,
582
                                         mmu_idx);
B
bellard 已提交
583
    else
B
blueswir1 已提交
584
        return get_physical_address_data(env, physical, prot, address, rw,
585
                                         mmu_idx);
B
bellard 已提交
586 587 588 589
}

/* Perform address translation */
int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
590
                              int mmu_idx, int is_softmmu)
B
bellard 已提交
591
{
B
bellard 已提交
592
    target_ulong virt_addr, vaddr;
A
Anthony Liguori 已提交
593
    target_phys_addr_t paddr;
P
Paul Brook 已提交
594 595
    target_ulong page_size;
    int error_code = 0, prot, access_index;
B
bellard 已提交
596

B
blueswir1 已提交
597
    error_code = get_physical_address(env, &paddr, &prot, &access_index,
P
Paul Brook 已提交
598
                                      address, rw, mmu_idx, &page_size);
B
bellard 已提交
599
    if (error_code == 0) {
B
blueswir1 已提交
600
        virt_addr = address & TARGET_PAGE_MASK;
B
blueswir1 已提交
601 602
        vaddr = virt_addr + ((address & TARGET_PAGE_MASK) &
                             (TARGET_PAGE_SIZE - 1));
603 604 605 606 607 608 609 610 611 612 613 614

        DPRINTF_MMU("Translate at %" PRIx64 " -> %" PRIx64 ","
                    " vaddr %" PRIx64
                    " mmu_idx=%d"
                    " tl=%d"
                    " primary context=%" PRIx64
                    " secondary context=%" PRIx64
                    "\n",
                    address, paddr, vaddr, mmu_idx, env->tl,
                    env->dmmu.mmu_primary_context,
                    env->dmmu.mmu_secondary_context);

P
Paul Brook 已提交
615 616
        tlb_set_page(env, vaddr, paddr, prot, mmu_idx, page_size);
        return 0;
B
bellard 已提交
617 618 619 620 621
    }
    // XXX
    return 1;
}

622
void dump_mmu(FILE *f, fprintf_function cpu_fprintf, CPUState *env)
B
bellard 已提交
623 624 625 626
{
    unsigned int i;
    const char *mask;

627 628 629 630
    (*cpu_fprintf)(f, "MMU contexts: Primary: %" PRId64 ", Secondary: %"
                   PRId64 "\n",
                   env->dmmu.mmu_primary_context,
                   env->dmmu.mmu_secondary_context);
B
bellard 已提交
631
    if ((env->lsu & DMMU_E) == 0) {
632
        (*cpu_fprintf)(f, "DMMU disabled\n");
B
bellard 已提交
633
    } else {
634
        (*cpu_fprintf)(f, "DMMU dump\n");
B
blueswir1 已提交
635
        for (i = 0; i < 64; i++) {
636
            switch ((env->dtlb[i].tte >> 61) & 3) {
B
blueswir1 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650
            default:
            case 0x0:
                mask = "  8k";
                break;
            case 0x1:
                mask = " 64k";
                break;
            case 0x2:
                mask = "512k";
                break;
            case 0x3:
                mask = "  4M";
                break;
            }
651
            if ((env->dtlb[i].tte & 0x8000000000000000ULL) != 0) {
652 653 654 655 656 657 658 659 660 661 662 663
                (*cpu_fprintf)(f, "[%02u] VA: %" PRIx64 ", PA: %" PRIx64
                               ", %s, %s, %s, %s, ctx %" PRId64 " %s\n",
                               i,
                               env->dtlb[i].tag & (uint64_t)~0x1fffULL,
                               env->dtlb[i].tte & (uint64_t)0x1ffffffe000ULL,
                               mask,
                               env->dtlb[i].tte & 0x4? "priv": "user",
                               env->dtlb[i].tte & 0x2? "RW": "RO",
                               env->dtlb[i].tte & 0x40? "locked": "unlocked",
                               env->dtlb[i].tag & (uint64_t)0x1fffULL,
                               TTE_IS_GLOBAL(env->dtlb[i].tte)?
                               "global" : "local");
B
blueswir1 已提交
664 665
            }
        }
B
bellard 已提交
666 667
    }
    if ((env->lsu & IMMU_E) == 0) {
668
        (*cpu_fprintf)(f, "IMMU disabled\n");
B
bellard 已提交
669
    } else {
670
        (*cpu_fprintf)(f, "IMMU dump\n");
B
blueswir1 已提交
671
        for (i = 0; i < 64; i++) {
672
            switch ((env->itlb[i].tte >> 61) & 3) {
B
blueswir1 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686
            default:
            case 0x0:
                mask = "  8k";
                break;
            case 0x1:
                mask = " 64k";
                break;
            case 0x2:
                mask = "512k";
                break;
            case 0x3:
                mask = "  4M";
                break;
            }
687
            if ((env->itlb[i].tte & 0x8000000000000000ULL) != 0) {
688 689 690 691 692 693 694 695 696 697 698
                (*cpu_fprintf)(f, "[%02u] VA: %" PRIx64 ", PA: %" PRIx64
                               ", %s, %s, %s, ctx %" PRId64 " %s\n",
                               i,
                               env->itlb[i].tag & (uint64_t)~0x1fffULL,
                               env->itlb[i].tte & (uint64_t)0x1ffffffe000ULL,
                               mask,
                               env->itlb[i].tte & 0x4? "priv": "user",
                               env->itlb[i].tte & 0x40? "locked": "unlocked",
                               env->itlb[i].tag & (uint64_t)0x1fffULL,
                               TTE_IS_GLOBAL(env->itlb[i].tte)?
                               "global" : "local");
B
blueswir1 已提交
699 700
            }
        }
B
bellard 已提交
701 702
    }
}
703 704 705 706

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

707

708
#if !defined(CONFIG_USER_ONLY)
709 710
target_phys_addr_t cpu_get_phys_page_nofault(CPUState *env, target_ulong addr,
                                           int mmu_idx)
711
{
A
Anthony Liguori 已提交
712
    target_phys_addr_t phys_addr;
P
Paul Brook 已提交
713
    target_ulong page_size;
714 715 716
    int prot, access_index;

    if (get_physical_address(env, &phys_addr, &prot, &access_index, addr, 2,
717
                             mmu_idx, &page_size) != 0)
718
        if (get_physical_address(env, &phys_addr, &prot, &access_index, addr,
719
                                 0, mmu_idx, &page_size) != 0)
720 721 722 723 724
            return -1;
    if (cpu_get_physical_page_desc(phys_addr) == IO_MEM_UNASSIGNED)
        return -1;
    return phys_addr;
}
725 726 727

target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
{
728
    return cpu_get_phys_page_nofault(env, addr, cpu_mmu_index(env));
729
}
730 731
#endif

732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
#ifdef TARGET_SPARC64
#ifdef DEBUG_PCALL
static const char * const excp_names[0x80] = {
    [TT_TFAULT] = "Instruction Access Fault",
    [TT_TMISS] = "Instruction Access MMU Miss",
    [TT_CODE_ACCESS] = "Instruction Access Error",
    [TT_ILL_INSN] = "Illegal Instruction",
    [TT_PRIV_INSN] = "Privileged Instruction",
    [TT_NFPU_INSN] = "FPU Disabled",
    [TT_FP_EXCP] = "FPU Exception",
    [TT_TOVF] = "Tag Overflow",
    [TT_CLRWIN] = "Clean Windows",
    [TT_DIV_ZERO] = "Division By Zero",
    [TT_DFAULT] = "Data Access Fault",
    [TT_DMISS] = "Data Access MMU Miss",
    [TT_DATA_ACCESS] = "Data Access Error",
    [TT_DPROT] = "Data Protection Error",
    [TT_UNALIGNED] = "Unaligned Memory Access",
    [TT_PRIV_ACT] = "Privileged Action",
    [TT_EXTINT | 0x1] = "External Interrupt 1",
    [TT_EXTINT | 0x2] = "External Interrupt 2",
    [TT_EXTINT | 0x3] = "External Interrupt 3",
    [TT_EXTINT | 0x4] = "External Interrupt 4",
    [TT_EXTINT | 0x5] = "External Interrupt 5",
    [TT_EXTINT | 0x6] = "External Interrupt 6",
    [TT_EXTINT | 0x7] = "External Interrupt 7",
    [TT_EXTINT | 0x8] = "External Interrupt 8",
    [TT_EXTINT | 0x9] = "External Interrupt 9",
    [TT_EXTINT | 0xa] = "External Interrupt 10",
    [TT_EXTINT | 0xb] = "External Interrupt 11",
    [TT_EXTINT | 0xc] = "External Interrupt 12",
    [TT_EXTINT | 0xd] = "External Interrupt 13",
    [TT_EXTINT | 0xe] = "External Interrupt 14",
    [TT_EXTINT | 0xf] = "External Interrupt 15",
};
#endif

void do_interrupt(CPUState *env)
{
    int intno = env->exception_index;
    trap_state *tsptr;

#ifdef DEBUG_PCALL
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
        static int count;
        const char *name;

        if (intno < 0 || intno >= 0x180) {
            name = "Unknown";
        } else if (intno >= 0x100) {
            name = "Trap Instruction";
        } else if (intno >= 0xc0) {
            name = "Window Fill";
        } else if (intno >= 0x80) {
            name = "Window Spill";
        } else {
            name = excp_names[intno];
            if (!name) {
                name = "Unknown";
            }
        }

        qemu_log("%6d: %s (v=%04x) pc=%016" PRIx64 " npc=%016" PRIx64
                " SP=%016" PRIx64 "\n",
                count, name, intno,
                env->pc,
                env->npc, env->regwptr[6]);
        log_cpu_state(env, 0);
#if 0
        {
            int i;
            uint8_t *ptr;

            qemu_log("       code=");
            ptr = (uint8_t *)env->pc;
            for (i = 0; i < 16; i++) {
                qemu_log(" %02x", ldub(ptr + i));
            }
            qemu_log("\n");
        }
#endif
        count++;
    }
#endif
#if !defined(CONFIG_USER_ONLY)
    if (env->tl >= env->maxtl) {
        cpu_abort(env, "Trap 0x%04x while trap level (%d) >= MAXTL (%d),"
                  " Error state", env->exception_index, env->tl, env->maxtl);
        return;
    }
#endif
    if (env->tl < env->maxtl - 1) {
        env->tl++;
    } else {
        env->pstate |= PS_RED;
        if (env->tl < env->maxtl) {
            env->tl++;
        }
    }
    tsptr = cpu_tsptr(env);

    tsptr->tstate = (cpu_get_ccr(env) << 32) |
        ((env->asi & 0xff) << 24) | ((env->pstate & 0xf3f) << 8) |
        cpu_get_cwp64(env);
    tsptr->tpc = env->pc;
    tsptr->tnpc = env->npc;
    tsptr->tt = intno;

    switch (intno) {
    case TT_IVEC:
        cpu_change_pstate(env, PS_PEF | PS_PRIV | PS_IG);
        break;
    case TT_TFAULT:
    case TT_DFAULT:
    case TT_TMISS ... TT_TMISS + 3:
    case TT_DMISS ... TT_DMISS + 3:
    case TT_DPROT ... TT_DPROT + 3:
        cpu_change_pstate(env, PS_PEF | PS_PRIV | PS_MG);
        break;
    default:
        cpu_change_pstate(env, PS_PEF | PS_PRIV | PS_AG);
        break;
    }

    if (intno == TT_CLRWIN) {
        cpu_set_cwp(env, cpu_cwp_dec(env, env->cwp - 1));
    } else if ((intno & 0x1c0) == TT_SPILL) {
        cpu_set_cwp(env, cpu_cwp_dec(env, env->cwp - env->cansave - 2));
    } else if ((intno & 0x1c0) == TT_FILL) {
        cpu_set_cwp(env, cpu_cwp_inc(env, env->cwp + 1));
    }
    env->tbr &= ~0x7fffULL;
    env->tbr |= ((env->tl > 1) ? 1 << 14 : 0) | (intno << 5);
    env->pc = env->tbr;
    env->npc = env->pc + 4;
    env->exception_index = -1;
}
#else
#ifdef DEBUG_PCALL
static const char * const excp_names[0x80] = {
    [TT_TFAULT] = "Instruction Access Fault",
    [TT_ILL_INSN] = "Illegal Instruction",
    [TT_PRIV_INSN] = "Privileged Instruction",
    [TT_NFPU_INSN] = "FPU Disabled",
    [TT_WIN_OVF] = "Window Overflow",
    [TT_WIN_UNF] = "Window Underflow",
    [TT_UNALIGNED] = "Unaligned Memory Access",
    [TT_FP_EXCP] = "FPU Exception",
    [TT_DFAULT] = "Data Access Fault",
    [TT_TOVF] = "Tag Overflow",
    [TT_EXTINT | 0x1] = "External Interrupt 1",
    [TT_EXTINT | 0x2] = "External Interrupt 2",
    [TT_EXTINT | 0x3] = "External Interrupt 3",
    [TT_EXTINT | 0x4] = "External Interrupt 4",
    [TT_EXTINT | 0x5] = "External Interrupt 5",
    [TT_EXTINT | 0x6] = "External Interrupt 6",
    [TT_EXTINT | 0x7] = "External Interrupt 7",
    [TT_EXTINT | 0x8] = "External Interrupt 8",
    [TT_EXTINT | 0x9] = "External Interrupt 9",
    [TT_EXTINT | 0xa] = "External Interrupt 10",
    [TT_EXTINT | 0xb] = "External Interrupt 11",
    [TT_EXTINT | 0xc] = "External Interrupt 12",
    [TT_EXTINT | 0xd] = "External Interrupt 13",
    [TT_EXTINT | 0xe] = "External Interrupt 14",
    [TT_EXTINT | 0xf] = "External Interrupt 15",
    [TT_TOVF] = "Tag Overflow",
    [TT_CODE_ACCESS] = "Instruction Access Error",
    [TT_DATA_ACCESS] = "Data Access Error",
    [TT_DIV_ZERO] = "Division By Zero",
    [TT_NCP_INSN] = "Coprocessor Disabled",
};
#endif

void do_interrupt(CPUState *env)
{
    int cwp, intno = env->exception_index;

#ifdef DEBUG_PCALL
    if (qemu_loglevel_mask(CPU_LOG_INT)) {
        static int count;
        const char *name;

        if (intno < 0 || intno >= 0x100) {
            name = "Unknown";
        } else if (intno >= 0x80) {
            name = "Trap Instruction";
        } else {
            name = excp_names[intno];
            if (!name) {
                name = "Unknown";
            }
        }

        qemu_log("%6d: %s (v=%02x) pc=%08x npc=%08x SP=%08x\n",
                count, name, intno,
                env->pc,
                env->npc, env->regwptr[6]);
        log_cpu_state(env, 0);
#if 0
        {
            int i;
            uint8_t *ptr;

            qemu_log("       code=");
            ptr = (uint8_t *)env->pc;
            for (i = 0; i < 16; i++) {
                qemu_log(" %02x", ldub(ptr + i));
            }
            qemu_log("\n");
        }
#endif
        count++;
    }
#endif
#if !defined(CONFIG_USER_ONLY)
    if (env->psret == 0) {
        cpu_abort(env, "Trap 0x%02x while interrupts disabled, Error state",
                  env->exception_index);
        return;
    }
#endif
    env->psret = 0;
    cwp = cpu_cwp_dec(env, env->cwp - 1);
    cpu_set_cwp(env, cwp);
    env->regwptr[9] = env->pc;
    env->regwptr[10] = env->npc;
    env->psrps = env->psrs;
    env->psrs = 1;
    env->tbr = (env->tbr & TBR_BASE_MASK) | (intno << 4);
    env->pc = env->tbr;
    env->npc = env->pc + 4;
    env->exception_index = -1;

#if !defined(CONFIG_USER_ONLY)
    /* IRQ acknowledgment */
    if ((intno & ~15) == TT_EXTINT && env->qemu_irq_ack != NULL) {
        env->qemu_irq_ack(env->irq_manager, intno);
    }
#endif
}
#endif

974 975
void cpu_reset(CPUSPARCState *env)
{
A
aliguori 已提交
976 977 978 979 980
    if (qemu_loglevel_mask(CPU_LOG_RESET)) {
        qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
        log_cpu_state(env, 0);
    }

981 982
    tlb_flush(env, 1);
    env->cwp = 0;
983
#ifndef TARGET_SPARC64
984
    env->wim = 1;
985
#endif
986
    env->regwptr = env->regbase + (env->cwp * 16);
987
    CC_OP = CC_OP_FLAGS;
988 989
#if defined(CONFIG_USER_ONLY)
#ifdef TARGET_SPARC64
990 991
    env->cleanwin = env->nwindows - 2;
    env->cansave = env->nwindows - 2;
992 993 994 995
    env->pstate = PS_RMO | PS_PEF | PS_IE;
    env->asi = 0x82; // Primary no-fault
#endif
#else
996
#if !defined(TARGET_SPARC64)
997 998 999
    env->psret = 0;
    env->psrs = 1;
    env->psrps = 1;
1000
#endif
1001
#ifdef TARGET_SPARC64
1002
    env->pstate = PS_PRIV|PS_RED|PS_PEF|PS_AG;
1003
    env->hpstate = cpu_has_hypervisor(env) ? HS_PRIV : 0;
1004 1005
    env->tl = env->maxtl;
    cpu_tsptr(env)->tt = TT_POWER_ON_RESET;
B
blueswir1 已提交
1006
    env->lsu = 0;
1007 1008
#else
    env->mmuregs[0] &= ~(MMU_E | MMU_NF);
1009
    env->mmuregs[0] |= env->def->mmu_bm;
1010
#endif
B
blueswir1 已提交
1011
    env->pc = 0;
1012 1013
    env->npc = env->pc + 4;
#endif
F
Fabien Chouteau 已提交
1014
    env->cache_control = 0;
1015 1016
}

B
blueswir1 已提交
1017
static int cpu_sparc_register(CPUSPARCState *env, const char *cpu_model)
1018
{
B
blueswir1 已提交
1019
    sparc_def_t def1, *def = &def1;
1020

B
blueswir1 已提交
1021 1022
    if (cpu_sparc_find_by_name(def, cpu_model) < 0)
        return -1;
1023

1024 1025 1026 1027 1028 1029
    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
1030 1031 1032
    env->cpu_model_str = cpu_model;
    env->version = def->iu_version;
    env->fsr = def->fpu_version;
1033
    env->nwindows = def->nwindows;
1034 1035 1036
#if !defined(TARGET_SPARC64)
    env->mmuregs[0] |= def->mmu_version;
    cpu_sparc_set_id(env, 0);
1037
    env->mxccregs[7] |= def->mxcc_version;
1038
#else
1039
    env->mmu_version = def->mmu_version;
1040 1041
    env->maxtl = def->maxtl;
    env->version |= def->maxtl << 8;
1042
    env->version |= def->nwindows - 1;
1043
#endif
B
blueswir1 已提交
1044 1045 1046 1047 1048
    return 0;
}

static void cpu_sparc_close(CPUSPARCState *env)
{
1049
    free(env->def);
B
blueswir1 已提交
1050 1051 1052 1053 1054 1055 1056 1057 1058
    free(env);
}

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

    env = qemu_mallocz(sizeof(CPUSPARCState));
    cpu_exec_init(env);
1059 1060 1061

    gen_intermediate_code_init(env);

B
blueswir1 已提交
1062 1063 1064 1065
    if (cpu_sparc_register(env, cpu_model) < 0) {
        cpu_sparc_close(env);
        return NULL;
    }
1066
    qemu_init_vcpu(env);
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

    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",
1082
        .iu_version = ((0x04ULL << 48) | (0x02ULL << 32) | (0ULL << 24)),
1083
        .fpu_version = 0x00000000,
1084
        .mmu_version = mmu_us_12,
1085
        .nwindows = 4,
1086
        .maxtl = 4,
B
blueswir1 已提交
1087
        .features = CPU_DEFAULT_FEATURES,
1088 1089 1090
    },
    {
        .name = "Fujitsu Sparc64 III",
1091
        .iu_version = ((0x04ULL << 48) | (0x03ULL << 32) | (0ULL << 24)),
1092
        .fpu_version = 0x00000000,
1093
        .mmu_version = mmu_us_12,
1094
        .nwindows = 5,
1095
        .maxtl = 4,
B
blueswir1 已提交
1096
        .features = CPU_DEFAULT_FEATURES,
1097 1098 1099
    },
    {
        .name = "Fujitsu Sparc64 IV",
1100
        .iu_version = ((0x04ULL << 48) | (0x04ULL << 32) | (0ULL << 24)),
1101
        .fpu_version = 0x00000000,
1102
        .mmu_version = mmu_us_12,
1103
        .nwindows = 8,
1104
        .maxtl = 5,
B
blueswir1 已提交
1105
        .features = CPU_DEFAULT_FEATURES,
1106 1107 1108
    },
    {
        .name = "Fujitsu Sparc64 V",
1109
        .iu_version = ((0x04ULL << 48) | (0x05ULL << 32) | (0x51ULL << 24)),
1110
        .fpu_version = 0x00000000,
1111
        .mmu_version = mmu_us_12,
1112
        .nwindows = 8,
1113
        .maxtl = 5,
B
blueswir1 已提交
1114
        .features = CPU_DEFAULT_FEATURES,
1115 1116 1117
    },
    {
        .name = "TI UltraSparc I",
1118
        .iu_version = ((0x17ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
1119
        .fpu_version = 0x00000000,
1120
        .mmu_version = mmu_us_12,
1121
        .nwindows = 8,
1122
        .maxtl = 5,
B
blueswir1 已提交
1123
        .features = CPU_DEFAULT_FEATURES,
1124 1125 1126
    },
    {
        .name = "TI UltraSparc II",
1127
        .iu_version = ((0x17ULL << 48) | (0x11ULL << 32) | (0x20ULL << 24)),
1128
        .fpu_version = 0x00000000,
1129
        .mmu_version = mmu_us_12,
1130
        .nwindows = 8,
1131
        .maxtl = 5,
B
blueswir1 已提交
1132
        .features = CPU_DEFAULT_FEATURES,
1133 1134 1135
    },
    {
        .name = "TI UltraSparc IIi",
1136
        .iu_version = ((0x17ULL << 48) | (0x12ULL << 32) | (0x91ULL << 24)),
1137
        .fpu_version = 0x00000000,
1138
        .mmu_version = mmu_us_12,
1139
        .nwindows = 8,
1140
        .maxtl = 5,
B
blueswir1 已提交
1141
        .features = CPU_DEFAULT_FEATURES,
1142 1143 1144
    },
    {
        .name = "TI UltraSparc IIe",
1145
        .iu_version = ((0x17ULL << 48) | (0x13ULL << 32) | (0x14ULL << 24)),
1146
        .fpu_version = 0x00000000,
1147
        .mmu_version = mmu_us_12,
1148
        .nwindows = 8,
1149
        .maxtl = 5,
B
blueswir1 已提交
1150
        .features = CPU_DEFAULT_FEATURES,
1151 1152 1153
    },
    {
        .name = "Sun UltraSparc III",
1154
        .iu_version = ((0x3eULL << 48) | (0x14ULL << 32) | (0x34ULL << 24)),
1155
        .fpu_version = 0x00000000,
1156
        .mmu_version = mmu_us_12,
1157
        .nwindows = 8,
1158
        .maxtl = 5,
B
blueswir1 已提交
1159
        .features = CPU_DEFAULT_FEATURES,
1160 1161 1162
    },
    {
        .name = "Sun UltraSparc III Cu",
1163
        .iu_version = ((0x3eULL << 48) | (0x15ULL << 32) | (0x41ULL << 24)),
1164
        .fpu_version = 0x00000000,
1165
        .mmu_version = mmu_us_3,
1166
        .nwindows = 8,
1167
        .maxtl = 5,
B
blueswir1 已提交
1168
        .features = CPU_DEFAULT_FEATURES,
1169 1170 1171
    },
    {
        .name = "Sun UltraSparc IIIi",
1172
        .iu_version = ((0x3eULL << 48) | (0x16ULL << 32) | (0x34ULL << 24)),
1173
        .fpu_version = 0x00000000,
1174
        .mmu_version = mmu_us_12,
1175
        .nwindows = 8,
1176
        .maxtl = 5,
B
blueswir1 已提交
1177
        .features = CPU_DEFAULT_FEATURES,
1178 1179 1180
    },
    {
        .name = "Sun UltraSparc IV",
1181
        .iu_version = ((0x3eULL << 48) | (0x18ULL << 32) | (0x31ULL << 24)),
1182
        .fpu_version = 0x00000000,
1183
        .mmu_version = mmu_us_4,
1184
        .nwindows = 8,
1185
        .maxtl = 5,
B
blueswir1 已提交
1186
        .features = CPU_DEFAULT_FEATURES,
1187 1188 1189
    },
    {
        .name = "Sun UltraSparc IV+",
1190
        .iu_version = ((0x3eULL << 48) | (0x19ULL << 32) | (0x22ULL << 24)),
1191
        .fpu_version = 0x00000000,
1192
        .mmu_version = mmu_us_12,
1193
        .nwindows = 8,
1194
        .maxtl = 5,
1195
        .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_CMT,
1196 1197 1198
    },
    {
        .name = "Sun UltraSparc IIIi+",
1199
        .iu_version = ((0x3eULL << 48) | (0x22ULL << 32) | (0ULL << 24)),
1200
        .fpu_version = 0x00000000,
1201
        .mmu_version = mmu_us_3,
1202
        .nwindows = 8,
1203
        .maxtl = 5,
B
blueswir1 已提交
1204
        .features = CPU_DEFAULT_FEATURES,
1205
    },
1206 1207 1208
    {
        .name = "Sun UltraSparc T1",
        // defined in sparc_ifu_fdp.v and ctu.h
1209
        .iu_version = ((0x3eULL << 48) | (0x23ULL << 32) | (0x02ULL << 24)),
1210 1211 1212
        .fpu_version = 0x00000000,
        .mmu_version = mmu_sun4v,
        .nwindows = 8,
1213
        .maxtl = 6,
1214 1215 1216 1217 1218 1219
        .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
1220
        .iu_version = ((0x3eULL << 48) | (0x24ULL << 32) | (0x02ULL << 24)),
1221 1222 1223
        .fpu_version = 0x00000000,
        .mmu_version = mmu_sun4v,
        .nwindows = 8,
1224
        .maxtl = 6,
1225 1226 1227
        .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_HYPV | CPU_FEATURE_CMT
        | CPU_FEATURE_GL,
    },
1228 1229
    {
        .name = "NEC UltraSparc I",
1230
        .iu_version = ((0x22ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
1231
        .fpu_version = 0x00000000,
1232
        .mmu_version = mmu_us_12,
1233
        .nwindows = 8,
1234
        .maxtl = 5,
B
blueswir1 已提交
1235
        .features = CPU_DEFAULT_FEATURES,
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
    },
#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,
1248
        .nwindows = 7,
B
blueswir1 已提交
1249
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_FSMULD,
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
    },
    {
        .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,
1261
        .nwindows = 8,
B
blueswir1 已提交
1262
        .features = CPU_DEFAULT_FEATURES,
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
    },
    {
        .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,
1274
        .nwindows = 8,
B
blueswir1 已提交
1275
        .features = CPU_DEFAULT_FEATURES,
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
    },
    {
        .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,
1287
        .nwindows = 8,
B
blueswir1 已提交
1288 1289
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
        CPU_FEATURE_FSMULD,
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
    },
    {
        .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,
1301
        .nwindows = 8,
B
blueswir1 已提交
1302 1303
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
        CPU_FEATURE_FSMULD,
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
    },
    {
        .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,
1315
        .nwindows = 8,
B
blueswir1 已提交
1316 1317
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
        CPU_FEATURE_FSMULD,
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
    },
    {
        .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,
1329
        .nwindows = 7,
B
blueswir1 已提交
1330 1331 1332
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_MUL |
        CPU_FEATURE_DIV | CPU_FEATURE_FLUSH | CPU_FEATURE_FSQRT |
        CPU_FEATURE_FMUL,
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
    },
    {
        .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,
1344
        .nwindows = 8,
B
blueswir1 已提交
1345
        .features = CPU_DEFAULT_FEATURES,
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
    },
    {
        .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,
1357
        .nwindows = 8,
B
blueswir1 已提交
1358
        .features = CPU_DEFAULT_FEATURES,
1359
    },
B
blueswir1 已提交
1360 1361
    {
        .name = "TI SuperSparc 40", // STP1020NPGA
1362
        .iu_version = 0x41000000, // SuperSPARC 2.x
B
blueswir1 已提交
1363
        .fpu_version = 0 << 17,
1364
        .mmu_version = 0x00000800, // SuperSPARC 2.x, no MXCC
B
blueswir1 已提交
1365 1366 1367 1368 1369
        .mmu_bm = 0x00002000,
        .mmu_ctpr_mask = 0xffffffc0,
        .mmu_cxr_mask = 0x0000ffff,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1370
        .nwindows = 8,
B
blueswir1 已提交
1371 1372 1373 1374
        .features = CPU_DEFAULT_FEATURES,
    },
    {
        .name = "TI SuperSparc 50", // STP1020PGA
1375
        .iu_version = 0x40000000, // SuperSPARC 3.x
B
blueswir1 已提交
1376
        .fpu_version = 0 << 17,
1377
        .mmu_version = 0x01000800, // SuperSPARC 3.x, no MXCC
B
blueswir1 已提交
1378 1379 1380 1381 1382
        .mmu_bm = 0x00002000,
        .mmu_ctpr_mask = 0xffffffc0,
        .mmu_cxr_mask = 0x0000ffff,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1383
        .nwindows = 8,
B
blueswir1 已提交
1384 1385
        .features = CPU_DEFAULT_FEATURES,
    },
1386 1387
    {
        .name = "TI SuperSparc 51",
1388
        .iu_version = 0x40000000, // SuperSPARC 3.x
1389
        .fpu_version = 0 << 17,
1390
        .mmu_version = 0x01000000, // SuperSPARC 3.x, MXCC
1391 1392 1393 1394 1395
        .mmu_bm = 0x00002000,
        .mmu_ctpr_mask = 0xffffffc0,
        .mmu_cxr_mask = 0x0000ffff,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1396
        .mxcc_version = 0x00000104,
1397
        .nwindows = 8,
B
blueswir1 已提交
1398
        .features = CPU_DEFAULT_FEATURES,
1399
    },
B
blueswir1 已提交
1400 1401
    {
        .name = "TI SuperSparc 60", // STP1020APGA
1402
        .iu_version = 0x40000000, // SuperSPARC 3.x
B
blueswir1 已提交
1403
        .fpu_version = 0 << 17,
1404
        .mmu_version = 0x01000800, // SuperSPARC 3.x, no MXCC
B
blueswir1 已提交
1405 1406 1407 1408 1409
        .mmu_bm = 0x00002000,
        .mmu_ctpr_mask = 0xffffffc0,
        .mmu_cxr_mask = 0x0000ffff,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1410
        .nwindows = 8,
B
blueswir1 已提交
1411 1412
        .features = CPU_DEFAULT_FEATURES,
    },
1413 1414
    {
        .name = "TI SuperSparc 61",
1415
        .iu_version = 0x44000000, // SuperSPARC 3.x
1416
        .fpu_version = 0 << 17,
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
        .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
1432 1433 1434 1435 1436
        .mmu_bm = 0x00002000,
        .mmu_ctpr_mask = 0xffffffc0,
        .mmu_cxr_mask = 0x0000ffff,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1437
        .mxcc_version = 0x00000104,
1438
        .nwindows = 8,
B
blueswir1 已提交
1439
        .features = CPU_DEFAULT_FEATURES,
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
    },
    {
        .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,
1451
        .nwindows = 8,
B
blueswir1 已提交
1452
        .features = CPU_DEFAULT_FEATURES,
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
    },
    {
        .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,
1464
        .nwindows = 8,
B
blueswir1 已提交
1465
        .features = CPU_DEFAULT_FEATURES,
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
    },
    {
        .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,
1477
        .nwindows = 8,
B
blueswir1 已提交
1478 1479
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
        CPU_FEATURE_FSMULD,
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
    },
    {
        .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,
1491
        .nwindows = 8,
B
blueswir1 已提交
1492 1493
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_MUL | CPU_FEATURE_FSQRT |
        CPU_FEATURE_FSMULD,
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
    },
    {
        .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,
1505
        .nwindows = 8,
B
blueswir1 已提交
1506
        .features = CPU_DEFAULT_FEATURES,
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
    },
    {
        .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,
1518
        .nwindows = 8,
F
Fabien Chouteau 已提交
1519
        .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_TA0_SHUTDOWN,
1520 1521 1522 1523 1524 1525
    },
    {
        .name = "LEON3",
        .iu_version = 0xf3000000,
        .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
        .mmu_version = 0xf3000000,
F
Fabien Chouteau 已提交
1526
        .mmu_bm = 0x00000000,
1527 1528 1529 1530
        .mmu_ctpr_mask = 0x007ffff0,
        .mmu_cxr_mask = 0x0000003f,
        .mmu_sfsr_mask = 0xffffffff,
        .mmu_trcr_mask = 0xffffffff,
1531
        .nwindows = 8,
1532
        .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_TA0_SHUTDOWN |
F
Fabien Chouteau 已提交
1533
        CPU_FEATURE_ASR17 | CPU_FEATURE_CACHE_CTRL,
1534 1535 1536 1537
    },
#endif
};

B
blueswir1 已提交
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
static const char * const feature_name[] = {
    "float",
    "float128",
    "swap",
    "mul",
    "div",
    "flush",
    "fsqrt",
    "fmul",
    "vis1",
    "vis2",
B
blueswir1 已提交
1549
    "fsmuld",
1550 1551 1552
    "hypv",
    "cmt",
    "gl",
B
blueswir1 已提交
1553 1554
};

1555
static void print_features(FILE *f, fprintf_function cpu_fprintf,
B
blueswir1 已提交
1556
                           uint32_t features, const char *prefix)
1557 1558 1559
{
    unsigned int i;

B
blueswir1 已提交
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
    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 已提交
1580
static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model)
B
blueswir1 已提交
1581 1582 1583 1584 1585 1586 1587
{
    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;
B
Blue Swirl 已提交
1588
    uint64_t iu_version;
1589
    uint32_t fpu_version, mmu_version, nwindows;
B
blueswir1 已提交
1590

1591
    for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
1592
        if (strcasecmp(name, sparc_defs[i].name) == 0) {
B
blueswir1 已提交
1593
            def = &sparc_defs[i];
1594 1595
        }
    }
B
blueswir1 已提交
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
    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
B
Blue Swirl 已提交
1620
                fprintf(stderr, "iu_version %" PRIx64 "\n", iu_version);
B
blueswir1 已提交
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
#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
1632
                fprintf(stderr, "fpu_version %x\n", fpu_version);
B
blueswir1 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
#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
1644
                fprintf(stderr, "mmu_version %x\n", mmu_version);
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
#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 已提交
1658 1659 1660 1661 1662 1663
#endif
            } else {
                fprintf(stderr, "unrecognized feature %s\n", featurestr);
                goto error;
            }
        } else {
B
blueswir1 已提交
1664 1665
            fprintf(stderr, "feature string `%s' not in format "
                    "(+feature|-feature|feature=xyz)\n", featurestr);
B
blueswir1 已提交
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
            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;
1681 1682
}

1683
void sparc_cpu_list(FILE *f, fprintf_function cpu_fprintf)
1684 1685 1686
{
    unsigned int i;

1687
    for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
1688
        (*cpu_fprintf)(f, "Sparc %16s IU " TARGET_FMT_lx " FPU %08x MMU %08x NWINS %d ",
1689 1690 1691
                       sparc_defs[i].name,
                       sparc_defs[i].iu_version,
                       sparc_defs[i].fpu_version,
1692 1693
                       sparc_defs[i].mmu_version,
                       sparc_defs[i].nwindows);
B
blueswir1 已提交
1694 1695 1696 1697
        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 已提交
1698
        (*cpu_fprintf)(f, "\n");
1699
    }
1700 1701
    (*cpu_fprintf)(f, "Default CPU feature flags (use '-' to remove): ");
    print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES, NULL);
B
blueswir1 已提交
1702
    (*cpu_fprintf)(f, "\n");
1703 1704 1705 1706 1707
    (*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");
1708 1709
}

1710
static void cpu_print_cc(FILE *f, fprintf_function cpu_fprintf,
B
Blue Swirl 已提交
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
                         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

1724
void cpu_dump_state(CPUState *env, FILE *f, fprintf_function cpu_fprintf,
1725 1726 1727 1728
                    int flags)
{
    int i, x;

B
blueswir1 已提交
1729 1730
    cpu_fprintf(f, "pc: " TARGET_FMT_lx "  npc: " TARGET_FMT_lx "\n", env->pc,
                env->npc);
1731
    cpu_fprintf(f, "General Registers:\n");
B
Blue Swirl 已提交
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741

    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");
        }
    }
1742 1743
    cpu_fprintf(f, "\nCurrent Register Window:\n");
    for (x = 0; x < 3; x++) {
B
Blue Swirl 已提交
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
        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");
            }
        }
1755 1756
    }
    cpu_fprintf(f, "\nFloating Point Registers:\n");
B
Blue Swirl 已提交
1757
    for (i = 0; i < TARGET_FPREGS; i++) {
1758 1759
        if ((i & 3) == 0)
            cpu_fprintf(f, "%%f%02d:", i);
1760
        cpu_fprintf(f, " %016f", *(float *)&env->fpr[i]);
1761 1762 1763 1764
        if ((i & 3) == 3)
            cpu_fprintf(f, "\n");
    }
#ifdef TARGET_SPARC64
B
Blue Swirl 已提交
1765
    cpu_fprintf(f, "pstate: %08x ccr: %02x (icc: ", env->pstate,
1766
                (unsigned)cpu_get_ccr(env));
1767
    cpu_print_cc(f, cpu_fprintf, cpu_get_ccr(env) << PSR_CARRY_SHIFT);
B
Blue Swirl 已提交
1768
    cpu_fprintf(f, " xcc: ");
1769
    cpu_print_cc(f, cpu_fprintf, cpu_get_ccr(env) << (PSR_CARRY_SHIFT - 4));
B
Blue Swirl 已提交
1770 1771 1772 1773
    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",
1774
                env->cansave, env->canrestore, env->otherwin, env->wstate,
1775
                env->cleanwin, env->nwindows - 1 - env->cwp);
B
Blue Swirl 已提交
1776 1777
    cpu_fprintf(f, "fsr: " TARGET_FMT_lx " y: " TARGET_FMT_lx " fprs: "
                TARGET_FMT_lx "\n", env->fsr, env->y, env->fprs);
1778
#else
1779 1780
    cpu_fprintf(f, "psr: %08x (icc: ", cpu_get_psr(env));
    cpu_print_cc(f, cpu_fprintf, cpu_get_psr(env));
B
Blue Swirl 已提交
1781 1782 1783 1784 1785
    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);
1786 1787
#endif
}