helper.c 46.7 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 189 190 191 192 193
    /* 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;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#else /* !TARGET_SPARC64 */
367 368

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

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

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
385
static inline int ultrasparc_tag_match(SparcTLBEntry *tlb,
386
                                       uint64_t address, uint64_t context,
387 388
                                       target_phys_addr_t *physical,
                                       int is_nucleus)
389 390 391
{
    uint64_t mask;

392
    switch ((tlb->tte >> 61) & 3) {
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
    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?
409
    if (TTE_IS_VALID(tlb->tte) &&
410 411 412
        ((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))
413 414
    {
        // decode physical address
415
        *physical = ((tlb->tte & mask) | (address & ~mask)) & 0x1ffffffe000ULL;
416 417 418 419 420 421
        return 1;
    }

    return 0;
}

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

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

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

B
bellard 已提交
439
    for (i = 0; i < 64; i++) {
B
blueswir1 已提交
440
        // ctx match, vaddr match, valid?
441
        if (ultrasparc_tag_match(&env->dtlb[i],
442 443
                                 address, context, physical,
                                 is_nucleus)) {
B
blueswir1 已提交
444
            // access ok?
445 446 447 448 449 450 451 452 453 454
            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 已提交
455
                                             another fault) */
456 457 458 459 460 461

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

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

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

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

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

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

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

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

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

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

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

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

654

655
#if !defined(CONFIG_USER_ONLY)
A
Anthony Liguori 已提交
656
target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
657
{
A
Anthony Liguori 已提交
658
    target_phys_addr_t phys_addr;
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    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 已提交
674 675 676 677 678
    if (qemu_loglevel_mask(CPU_LOG_RESET)) {
        qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
        log_cpu_state(env, 0);
    }

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

B
blueswir1 已提交
714
static int cpu_sparc_register(CPUSPARCState *env, const char *cpu_model)
715
{
B
blueswir1 已提交
716
    sparc_def_t def1, *def = &def1;
717

B
blueswir1 已提交
718 719
    if (cpu_sparc_find_by_name(def, cpu_model) < 0)
        return -1;
720

721 722 723 724 725 726
    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
727 728 729
    env->cpu_model_str = cpu_model;
    env->version = def->iu_version;
    env->fsr = def->fpu_version;
730
    env->nwindows = def->nwindows;
731 732 733
#if !defined(TARGET_SPARC64)
    env->mmuregs[0] |= def->mmu_version;
    cpu_sparc_set_id(env, 0);
734
    env->mxccregs[7] |= def->mxcc_version;
735
#else
736
    env->mmu_version = def->mmu_version;
737 738
    env->maxtl = def->maxtl;
    env->version |= def->maxtl << 8;
739
    env->version |= def->nwindows - 1;
740
#endif
B
blueswir1 已提交
741 742 743 744 745
    return 0;
}

static void cpu_sparc_close(CPUSPARCState *env)
{
746
    free(env->def);
B
blueswir1 已提交
747 748 749 750 751 752 753 754 755
    free(env);
}

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

    env = qemu_mallocz(sizeof(CPUSPARCState));
    cpu_exec_init(env);
756 757 758

    gen_intermediate_code_init(env);

B
blueswir1 已提交
759 760 761 762
    if (cpu_sparc_register(env, cpu_model) < 0) {
        cpu_sparc_close(env);
        return NULL;
    }
763
    qemu_init_vcpu(env);
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778

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

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

static void print_features(FILE *f,
                           int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                           uint32_t features, const char *prefix)
1254 1255 1256
{
    unsigned int i;

B
blueswir1 已提交
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
    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 已提交
1277
static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model)
B
blueswir1 已提交
1278 1279 1280 1281 1282 1283 1284 1285
{
    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;
1286
    uint32_t fpu_version, mmu_version, nwindows;
B
blueswir1 已提交
1287

1288
    for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
1289
        if (strcasecmp(name, sparc_defs[i].name) == 0) {
B
blueswir1 已提交
1290
            def = &sparc_defs[i];
1291 1292
        }
    }
B
blueswir1 已提交
1293 1294 1295 1296 1297 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
    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
1329
                fprintf(stderr, "fpu_version %x\n", fpu_version);
B
blueswir1 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
#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
1341
                fprintf(stderr, "mmu_version %x\n", mmu_version);
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
#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 已提交
1355 1356 1357 1358 1359 1360
#endif
            } else {
                fprintf(stderr, "unrecognized feature %s\n", featurestr);
                goto error;
            }
        } else {
B
blueswir1 已提交
1361 1362
            fprintf(stderr, "feature string `%s' not in format "
                    "(+feature|-feature|feature=xyz)\n", featurestr);
B
blueswir1 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
            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;
1378 1379
}

B
blueswir1 已提交
1380
void sparc_cpu_list(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
1381 1382 1383
{
    unsigned int i;

1384
    for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
1385
        (*cpu_fprintf)(f, "Sparc %16s IU " TARGET_FMT_lx " FPU %08x MMU %08x NWINS %d ",
1386 1387 1388
                       sparc_defs[i].name,
                       sparc_defs[i].iu_version,
                       sparc_defs[i].fpu_version,
1389 1390
                       sparc_defs[i].mmu_version,
                       sparc_defs[i].nwindows);
B
blueswir1 已提交
1391 1392 1393 1394
        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 已提交
1395
        (*cpu_fprintf)(f, "\n");
1396
    }
1397 1398
    (*cpu_fprintf)(f, "Default CPU feature flags (use '-' to remove): ");
    print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES, NULL);
B
blueswir1 已提交
1399
    (*cpu_fprintf)(f, "\n");
1400 1401 1402 1403 1404
    (*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");
1405 1406
}

B
Blue Swirl 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
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

1422 1423 1424 1425 1426 1427
void cpu_dump_state(CPUState *env, FILE *f,
                    int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                    int flags)
{
    int i, x;

B
blueswir1 已提交
1428 1429
    cpu_fprintf(f, "pc: " TARGET_FMT_lx "  npc: " TARGET_FMT_lx "\n", env->pc,
                env->npc);
1430
    cpu_fprintf(f, "General Registers:\n");
B
Blue Swirl 已提交
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440

    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");
        }
    }
1441 1442
    cpu_fprintf(f, "\nCurrent Register Window:\n");
    for (x = 0; x < 3; x++) {
B
Blue Swirl 已提交
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
        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");
            }
        }
1454 1455
    }
    cpu_fprintf(f, "\nFloating Point Registers:\n");
B
Blue Swirl 已提交
1456
    for (i = 0; i < TARGET_FPREGS; i++) {
1457 1458
        if ((i & 3) == 0)
            cpu_fprintf(f, "%%f%02d:", i);
1459
        cpu_fprintf(f, " %016f", *(float *)&env->fpr[i]);
1460 1461 1462 1463
        if ((i & 3) == 3)
            cpu_fprintf(f, "\n");
    }
#ifdef TARGET_SPARC64
B
Blue Swirl 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472
    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",
1473
                env->cansave, env->canrestore, env->otherwin, env->wstate,
1474
                env->cleanwin, env->nwindows - 1 - env->cwp);
B
Blue Swirl 已提交
1475 1476
    cpu_fprintf(f, "fsr: " TARGET_FMT_lx " y: " TARGET_FMT_lx " fprs: "
                TARGET_FMT_lx "\n", env->fsr, env->y, env->fprs);
1477
#else
B
Blue Swirl 已提交
1478 1479 1480 1481 1482 1483 1484
    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);
1485 1486
#endif
}