helper.c 45.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 */

B
blueswir1 已提交
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
};

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

114
    is_user = mmu_idx == MMU_USER_IDX;
115
    virt_addr = address & TARGET_PAGE_MASK;
B
blueswir1 已提交
116

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

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

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

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

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

B
blueswir1 已提交
159 160 161 162 163 164 165 166
            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 已提交
167
                pde = ldl_phys(pde_ptr);
168

B
blueswir1 已提交
169 170 171 172 173 174 175 176 177
                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 */
                    virt_addr = address & TARGET_PAGE_MASK;
B
blueswir1 已提交
178 179
                    page_offset = (address & TARGET_PAGE_MASK) &
                        (TARGET_PAGE_SIZE - 1);
B
blueswir1 已提交
180 181 182 183 184 185 186 187 188 189 190
                }
                break;
            case 2: /* L2 PTE */
                virt_addr = address & ~0x3ffff;
                page_offset = address & 0x3ffff;
            }
            break;
        case 2: /* L1 PTE */
            virt_addr = address & ~0xffffff;
            page_offset = address & 0xffffff;
        }
191 192 193
    }

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

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

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

/* Perform address translation */
222
int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
223
                              int mmu_idx, int is_softmmu)
B
bellard 已提交
224
{
225
    target_phys_addr_t paddr;
226
    target_ulong vaddr;
B
bellard 已提交
227
    int error_code = 0, prot, ret = 0, access_index;
228

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

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

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

target_ulong mmu_probe(CPUState *env, target_ulong address, int mmulev)
{
    target_phys_addr_t pde_ptr;
    uint32_t pde;

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

B
blueswir1 已提交
287 288 289 290 291 292 293 294 295 296 297
        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);
298 299
            pde = ldl_phys(pde_ptr);

B
blueswir1 已提交
300 301 302 303 304 305 306 307 308 309 310
            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);
311 312
                pde = ldl_phys(pde_ptr);

B
blueswir1 已提交
313 314 315 316 317 318 319 320 321 322 323
                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;
                }
            }
        }
324 325 326 327 328 329 330
    }
    return 0;
}

#ifdef DEBUG_MMU
void dump_mmu(CPUState *env)
{
331 332 333
    target_ulong va, va1, va2;
    unsigned int n, m, o;
    target_phys_addr_t pde_ptr, pa;
334 335 336 337 338
    uint32_t pde;

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

#else /* !TARGET_SPARC64 */
371 372 373 374 375 376 377

// 41 bit physical address space
static inline target_phys_addr_t ultrasparc_truncate_physical(uint64_t x)
{
    return x & 0x1ffffffffffULL;
}

B
bellard 已提交
378 379 380
/*
 * UltraSparc IIi I/DMMUs
 */
381 382 383 384 385 386 387 388

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
389
static inline int ultrasparc_tag_match(SparcTLBEntry *tlb,
390 391 392 393 394
                                       uint64_t address, uint64_t context,
                                       target_phys_addr_t *physical)
{
    uint64_t mask;

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

    return 0;
}

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

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

437
    context = env->dmmu.mmu_primary_context & 0x1fff;
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)
        ) {
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 471 472
                *prot |= PAGE_WRITE;
            return 0;
        }
B
bellard 已提交
473
    }
B
bellard 已提交
474
#ifdef DEBUG_MMU
B
bellard 已提交
475
    printf("DMISS at 0x%" PRIx64 "\n", address);
B
bellard 已提交
476
#endif
477
    env->dmmu.tag_access = (address & ~0x1fffULL) | context;
B
bellard 已提交
478
    env->exception_index = TT_DMISS;
B
bellard 已提交
479 480 481
    return 1;
}

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

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

496
    context = env->dmmu.mmu_primary_context & 0x1fff;
497

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

528 529 530
static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
                                int *prot, int *access_index,
                                target_ulong address, int rw, int mmu_idx)
B
bellard 已提交
531
{
532 533
    int is_user = mmu_idx == MMU_USER_IDX;

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

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

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

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

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

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

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

#if defined(CONFIG_USER_ONLY)
target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
{
    return addr;
}

#else
target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
{
    target_phys_addr_t phys_addr;
    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 685 686
    env->regwptr = env->regbase + (env->cwp * 16);
#if defined(CONFIG_USER_ONLY)
#ifdef TARGET_SPARC64
687 688
    env->cleanwin = env->nwindows - 2;
    env->cansave = env->nwindows - 2;
689 690 691 692
    env->pstate = PS_RMO | PS_PEF | PS_IE;
    env->asi = 0x82; // Primary no-fault
#endif
#else
693
#if !defined(TARGET_SPARC64)
694
    env->psret = 0;
695
#endif
696 697
    env->psrs = 1;
    env->psrps = 1;
698
    CC_OP = CC_OP_FLAGS;
699 700 701
#ifdef TARGET_SPARC64
    env->pstate = PS_PRIV;
    env->hpstate = HS_PRIV;
702
    env->tsptr = &env->ts[env->tl & MAXTL_MASK];
B
blueswir1 已提交
703
    env->lsu = 0;
704 705
#else
    env->mmuregs[0] &= ~(MMU_E | MMU_NF);
706
    env->mmuregs[0] |= env->def->mmu_bm;
707
#endif
B
blueswir1 已提交
708
    env->pc = 0;
709 710 711 712
    env->npc = env->pc + 4;
#endif
}

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

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

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

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

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

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

    gen_intermediate_code_init(env);

B
blueswir1 已提交
758 759 760 761
    if (cpu_sparc_register(env, cpu_model) < 0) {
        cpu_sparc_close(env);
        return NULL;
    }
762
    cpu_reset(env);
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 1407 1408 1409 1410 1411 1412
}

void cpu_dump_state(CPUState *env, FILE *f,
                    int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                    int flags)
{
    int i, x;

B
blueswir1 已提交
1413 1414
    cpu_fprintf(f, "pc: " TARGET_FMT_lx "  npc: " TARGET_FMT_lx "\n", env->pc,
                env->npc);
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
    cpu_fprintf(f, "General Registers:\n");
    for (i = 0; i < 4; i++)
        cpu_fprintf(f, "%%g%c: " TARGET_FMT_lx "\t", i + '0', env->gregs[i]);
    cpu_fprintf(f, "\n");
    for (; i < 8; i++)
        cpu_fprintf(f, "%%g%c: " TARGET_FMT_lx "\t", i + '0', env->gregs[i]);
    cpu_fprintf(f, "\nCurrent Register Window:\n");
    for (x = 0; x < 3; x++) {
        for (i = 0; i < 4; i++)
            cpu_fprintf(f, "%%%c%d: " TARGET_FMT_lx "\t",
                    (x == 0 ? 'o' : (x == 1 ? 'l' : 'i')), i,
                    env->regwptr[i + x * 8]);
        cpu_fprintf(f, "\n");
        for (; i < 8; i++)
            cpu_fprintf(f, "%%%c%d: " TARGET_FMT_lx "\t",
                    (x == 0 ? 'o' : x == 1 ? 'l' : 'i'), i,
                    env->regwptr[i + x * 8]);
        cpu_fprintf(f, "\n");
    }
    cpu_fprintf(f, "\nFloating Point Registers:\n");
    for (i = 0; i < 32; i++) {
        if ((i & 3) == 0)
            cpu_fprintf(f, "%%f%02d:", i);
1438
        cpu_fprintf(f, " %016f", *(float *)&env->fpr[i]);
1439 1440 1441 1442 1443 1444
        if ((i & 3) == 3)
            cpu_fprintf(f, "\n");
    }
#ifdef TARGET_SPARC64
    cpu_fprintf(f, "pstate: 0x%08x ccr: 0x%02x asi: 0x%02x tl: %d fprs: %d\n",
                env->pstate, GET_CCR(env), env->asi, env->tl, env->fprs);
B
blueswir1 已提交
1445 1446
    cpu_fprintf(f, "cansave: %d canrestore: %d otherwin: %d wstate %d "
                "cleanwin %d cwp %d\n",
1447
                env->cansave, env->canrestore, env->otherwin, env->wstate,
1448
                env->cleanwin, env->nwindows - 1 - env->cwp);
1449
#else
1450 1451 1452

#define GET_FLAG(a,b) ((env->psr & a)?b:'-')

B
blueswir1 已提交
1453 1454 1455 1456 1457
    cpu_fprintf(f, "psr: 0x%08x -> %c%c%c%c %c%c%c wim: 0x%08x\n",
                GET_PSR(env), GET_FLAG(PSR_ZERO, 'Z'), GET_FLAG(PSR_OVF, 'V'),
                GET_FLAG(PSR_NEG, 'N'), GET_FLAG(PSR_CARRY, 'C'),
                env->psrs?'S':'-', env->psrps?'P':'-',
                env->psret?'E':'-', env->wim);
1458
#endif
1459
    cpu_fprintf(f, "fsr: 0x%08x\n", env->fsr);
1460
}