Statistics
| Branch: | Revision:

root / target-sparc / helper.c @ 77f193da

History | View | Annotate | Download (41.9 kB)

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

    
28
#include "cpu.h"
29
#include "exec-all.h"
30
#include "qemu-common.h"
31
#include "helper.h"
32

    
33
//#define DEBUG_MMU
34
//#define DEBUG_FEATURES
35

    
36
typedef struct sparc_def_t sparc_def_t;
37

    
38
struct sparc_def_t {
39
    const char *name;
40
    target_ulong iu_version;
41
    uint32_t fpu_version;
42
    uint32_t mmu_version;
43
    uint32_t mmu_bm;
44
    uint32_t mmu_ctpr_mask;
45
    uint32_t mmu_cxr_mask;
46
    uint32_t mmu_sfsr_mask;
47
    uint32_t mmu_trcr_mask;
48
    uint32_t features;
49
};
50

    
51
static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model);
52

    
53
/* Sparc MMU emulation */
54

    
55
/* thread support */
56

    
57
spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
58

    
59
void cpu_lock(void)
60
{
61
    spin_lock(&global_cpu_lock);
62
}
63

    
64
void cpu_unlock(void)
65
{
66
    spin_unlock(&global_cpu_lock);
67
}
68

    
69
#if defined(CONFIG_USER_ONLY)
70

    
71
int cpu_sparc_handle_mmu_fault(CPUState *env1, target_ulong address, int rw,
72
                               int mmu_idx, int is_softmmu)
73
{
74
    if (rw & 2)
75
        env1->exception_index = TT_TFAULT;
76
    else
77
        env1->exception_index = TT_DFAULT;
78
    return 1;
79
}
80

    
81
#else
82

    
83
#ifndef TARGET_SPARC64
84
/*
85
 * Sparc V8 Reference MMU (SRMMU)
86
 */
87
static const int access_table[8][8] = {
88
    { 0, 0, 0, 0, 2, 0, 3, 3 },
89
    { 0, 0, 0, 0, 2, 0, 0, 0 },
90
    { 2, 2, 0, 0, 0, 2, 3, 3 },
91
    { 2, 2, 0, 0, 0, 2, 0, 0 },
92
    { 2, 0, 2, 0, 2, 2, 3, 3 },
93
    { 2, 0, 2, 0, 2, 0, 2, 0 },
94
    { 2, 2, 2, 0, 2, 2, 3, 3 },
95
    { 2, 2, 2, 0, 2, 2, 2, 0 }
96
};
97

    
98
static const int perm_table[2][8] = {
99
    {
100
        PAGE_READ,
101
        PAGE_READ | PAGE_WRITE,
102
        PAGE_READ | PAGE_EXEC,
103
        PAGE_READ | PAGE_WRITE | PAGE_EXEC,
104
        PAGE_EXEC,
105
        PAGE_READ | PAGE_WRITE,
106
        PAGE_READ | PAGE_EXEC,
107
        PAGE_READ | PAGE_WRITE | PAGE_EXEC
108
    },
109
    {
110
        PAGE_READ,
111
        PAGE_READ | PAGE_WRITE,
112
        PAGE_READ | PAGE_EXEC,
113
        PAGE_READ | PAGE_WRITE | PAGE_EXEC,
114
        PAGE_EXEC,
115
        PAGE_READ,
116
        0,
117
        0,
118
    }
119
};
120

    
121
static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
122
                                int *prot, int *access_index,
123
                                target_ulong address, int rw, int mmu_idx)
124
{
125
    int access_perms = 0;
126
    target_phys_addr_t pde_ptr;
127
    uint32_t pde;
128
    target_ulong virt_addr;
129
    int error_code = 0, is_dirty, is_user;
130
    unsigned long page_offset;
131

    
132
    is_user = mmu_idx == MMU_USER_IDX;
133
    virt_addr = address & TARGET_PAGE_MASK;
134

    
135
    if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
136
        // Boot mode: instruction fetches are taken from PROM
137
        if (rw == 2 && (env->mmuregs[0] & env->mmu_bm)) {
138
            *physical = env->prom_addr | (address & 0x7ffffULL);
139
            *prot = PAGE_READ | PAGE_EXEC;
140
            return 0;
141
        }
142
        *physical = address;
143
        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
144
        return 0;
145
    }
146

    
147
    *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user? 0 : 1);
148
    *physical = 0xffffffffffff0000ULL;
149

    
150
    /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
151
    /* Context base + context number */
152
    pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
153
    pde = ldl_phys(pde_ptr);
154

    
155
    /* Ctx pde */
156
    switch (pde & PTE_ENTRYTYPE_MASK) {
157
    default:
158
    case 0: /* Invalid */
159
        return 1 << 2;
160
    case 2: /* L0 PTE, maybe should not happen? */
161
    case 3: /* Reserved */
162
        return 4 << 2;
163
    case 1: /* L0 PDE */
164
        pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
165
        pde = ldl_phys(pde_ptr);
166

    
167
        switch (pde & PTE_ENTRYTYPE_MASK) {
168
        default:
169
        case 0: /* Invalid */
170
            return (1 << 8) | (1 << 2);
171
        case 3: /* Reserved */
172
            return (1 << 8) | (4 << 2);
173
        case 1: /* L1 PDE */
174
            pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
175
            pde = ldl_phys(pde_ptr);
176

    
177
            switch (pde & PTE_ENTRYTYPE_MASK) {
178
            default:
179
            case 0: /* Invalid */
180
                return (2 << 8) | (1 << 2);
181
            case 3: /* Reserved */
182
                return (2 << 8) | (4 << 2);
183
            case 1: /* L2 PDE */
184
                pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
185
                pde = ldl_phys(pde_ptr);
186

    
187
                switch (pde & PTE_ENTRYTYPE_MASK) {
188
                default:
189
                case 0: /* Invalid */
190
                    return (3 << 8) | (1 << 2);
191
                case 1: /* PDE, should not happen */
192
                case 3: /* Reserved */
193
                    return (3 << 8) | (4 << 2);
194
                case 2: /* L3 PTE */
195
                    virt_addr = address & TARGET_PAGE_MASK;
196
                    page_offset = (address & TARGET_PAGE_MASK) &
197
                        (TARGET_PAGE_SIZE - 1);
198
                }
199
                break;
200
            case 2: /* L2 PTE */
201
                virt_addr = address & ~0x3ffff;
202
                page_offset = address & 0x3ffff;
203
            }
204
            break;
205
        case 2: /* L1 PTE */
206
            virt_addr = address & ~0xffffff;
207
            page_offset = address & 0xffffff;
208
        }
209
    }
210

    
211
    /* update page modified and dirty bits */
212
    is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
213
    if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
214
        pde |= PG_ACCESSED_MASK;
215
        if (is_dirty)
216
            pde |= PG_MODIFIED_MASK;
217
        stl_phys_notdirty(pde_ptr, pde);
218
    }
219
    /* check access */
220
    access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
221
    error_code = access_table[*access_index][access_perms];
222
    if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user))
223
        return error_code;
224

    
225
    /* the page can be put in the TLB */
226
    *prot = perm_table[is_user][access_perms];
227
    if (!(pde & PG_MODIFIED_MASK)) {
228
        /* only set write access if already dirty... otherwise wait
229
           for dirty access */
230
        *prot &= ~PAGE_WRITE;
231
    }
232

    
233
    /* Even if large ptes, we map only one 4KB page in the cache to
234
       avoid filling it too fast */
235
    *physical = ((target_phys_addr_t)(pde & PTE_ADDR_MASK) << 4) + page_offset;
236
    return error_code;
237
}
238

    
239
/* Perform address translation */
240
int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
241
                              int mmu_idx, int is_softmmu)
242
{
243
    target_phys_addr_t paddr;
244
    target_ulong vaddr;
245
    int error_code = 0, prot, ret = 0, access_index;
246

    
247
    error_code = get_physical_address(env, &paddr, &prot, &access_index,
248
                                      address, rw, mmu_idx);
249
    if (error_code == 0) {
250
        vaddr = address & TARGET_PAGE_MASK;
251
        paddr &= TARGET_PAGE_MASK;
252
#ifdef DEBUG_MMU
253
        printf("Translate at " TARGET_FMT_lx " -> " TARGET_FMT_plx ", vaddr "
254
               TARGET_FMT_lx "\n", address, paddr, vaddr);
255
#endif
256
        ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
257
        return ret;
258
    }
259

    
260
    if (env->mmuregs[3]) /* Fault status register */
261
        env->mmuregs[3] = 1; /* overflow (not read before another fault) */
262
    env->mmuregs[3] |= (access_index << 5) | error_code | 2;
263
    env->mmuregs[4] = address; /* Fault address register */
264

    
265
    if ((env->mmuregs[0] & MMU_NF) || env->psret == 0)  {
266
        // No fault mode: if a mapping is available, just override
267
        // permissions. If no mapping is available, redirect accesses to
268
        // neverland. Fake/overridden mappings will be flushed when
269
        // switching to normal mode.
270
        vaddr = address & TARGET_PAGE_MASK;
271
        prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
272
        ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
273
        return ret;
274
    } else {
275
        if (rw & 2)
276
            env->exception_index = TT_TFAULT;
277
        else
278
            env->exception_index = TT_DFAULT;
279
        return 1;
280
    }
281
}
282

    
283
target_ulong mmu_probe(CPUState *env, target_ulong address, int mmulev)
284
{
285
    target_phys_addr_t pde_ptr;
286
    uint32_t pde;
287

    
288
    /* Context base + context number */
289
    pde_ptr = (target_phys_addr_t)(env->mmuregs[1] << 4) +
290
        (env->mmuregs[2] << 2);
291
    pde = ldl_phys(pde_ptr);
292

    
293
    switch (pde & PTE_ENTRYTYPE_MASK) {
294
    default:
295
    case 0: /* Invalid */
296
    case 2: /* PTE, maybe should not happen? */
297
    case 3: /* Reserved */
298
        return 0;
299
    case 1: /* L1 PDE */
300
        if (mmulev == 3)
301
            return pde;
302
        pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
303
        pde = ldl_phys(pde_ptr);
304

    
305
        switch (pde & PTE_ENTRYTYPE_MASK) {
306
        default:
307
        case 0: /* Invalid */
308
        case 3: /* Reserved */
309
            return 0;
310
        case 2: /* L1 PTE */
311
            return pde;
312
        case 1: /* L2 PDE */
313
            if (mmulev == 2)
314
                return pde;
315
            pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
316
            pde = ldl_phys(pde_ptr);
317

    
318
            switch (pde & PTE_ENTRYTYPE_MASK) {
319
            default:
320
            case 0: /* Invalid */
321
            case 3: /* Reserved */
322
                return 0;
323
            case 2: /* L2 PTE */
324
                return pde;
325
            case 1: /* L3 PDE */
326
                if (mmulev == 1)
327
                    return pde;
328
                pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
329
                pde = ldl_phys(pde_ptr);
330

    
331
                switch (pde & PTE_ENTRYTYPE_MASK) {
332
                default:
333
                case 0: /* Invalid */
334
                case 1: /* PDE, should not happen */
335
                case 3: /* Reserved */
336
                    return 0;
337
                case 2: /* L3 PTE */
338
                    return pde;
339
                }
340
            }
341
        }
342
    }
343
    return 0;
344
}
345

    
346
#ifdef DEBUG_MMU
347
void dump_mmu(CPUState *env)
348
{
349
    target_ulong va, va1, va2;
350
    unsigned int n, m, o;
351
    target_phys_addr_t pde_ptr, pa;
352
    uint32_t pde;
353

    
354
    printf("MMU dump:\n");
355
    pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
356
    pde = ldl_phys(pde_ptr);
357
    printf("Root ptr: " TARGET_FMT_plx ", ctx: %d\n",
358
           (target_phys_addr_t)env->mmuregs[1] << 4, env->mmuregs[2]);
359
    for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
360
        pde = mmu_probe(env, va, 2);
361
        if (pde) {
362
            pa = cpu_get_phys_page_debug(env, va);
363
            printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
364
                   " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
365
            for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
366
                pde = mmu_probe(env, va1, 1);
367
                if (pde) {
368
                    pa = cpu_get_phys_page_debug(env, va1);
369
                    printf(" VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
370
                           " PDE: " TARGET_FMT_lx "\n", va1, pa, pde);
371
                    for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
372
                        pde = mmu_probe(env, va2, 0);
373
                        if (pde) {
374
                            pa = cpu_get_phys_page_debug(env, va2);
375
                            printf("  VA: " TARGET_FMT_lx ", PA: "
376
                                   TARGET_FMT_plx " PTE: " TARGET_FMT_lx "\n",
377
                                   va2, pa, pde);
378
                        }
379
                    }
380
                }
381
            }
382
        }
383
    }
384
    printf("MMU dump ends\n");
385
}
386
#endif /* DEBUG_MMU */
387

    
388
#else /* !TARGET_SPARC64 */
389
/*
390
 * UltraSparc IIi I/DMMUs
391
 */
392
static int get_physical_address_data(CPUState *env,
393
                                     target_phys_addr_t *physical, int *prot,
394
                                     target_ulong address, int rw, int is_user)
395
{
396
    target_ulong mask;
397
    unsigned int i;
398

    
399
    if ((env->lsu & DMMU_E) == 0) { /* DMMU disabled */
400
        *physical = address;
401
        *prot = PAGE_READ | PAGE_WRITE;
402
        return 0;
403
    }
404

    
405
    for (i = 0; i < 64; i++) {
406
        switch ((env->dtlb_tte[i] >> 61) & 3) {
407
        default:
408
        case 0x0: // 8k
409
            mask = 0xffffffffffffe000ULL;
410
            break;
411
        case 0x1: // 64k
412
            mask = 0xffffffffffff0000ULL;
413
            break;
414
        case 0x2: // 512k
415
            mask = 0xfffffffffff80000ULL;
416
            break;
417
        case 0x3: // 4M
418
            mask = 0xffffffffffc00000ULL;
419
            break;
420
        }
421
        // ctx match, vaddr match?
422
        if (env->dmmuregs[1] == (env->dtlb_tag[i] & 0x1fff) &&
423
            (address & mask) == (env->dtlb_tag[i] & ~0x1fffULL)) {
424
            // valid, access ok?
425
            if ((env->dtlb_tte[i] & 0x8000000000000000ULL) == 0 ||
426
                ((env->dtlb_tte[i] & 0x4) && is_user) ||
427
                (!(env->dtlb_tte[i] & 0x2) && (rw == 1))) {
428
                if (env->dmmuregs[3]) /* Fault status register */
429
                    env->dmmuregs[3] = 2; /* overflow (not read before
430
                                             another fault) */
431
                env->dmmuregs[3] |= (is_user << 3) | ((rw == 1) << 2) | 1;
432
                env->dmmuregs[4] = address; /* Fault address register */
433
                env->exception_index = TT_DFAULT;
434
#ifdef DEBUG_MMU
435
                printf("DFAULT at 0x%" PRIx64 "\n", address);
436
#endif
437
                return 1;
438
            }
439
            *physical = (env->dtlb_tte[i] & mask & 0x1fffffff000ULL) +
440
                (address & ~mask & 0x1fffffff000ULL);
441
            *prot = PAGE_READ;
442
            if (env->dtlb_tte[i] & 0x2)
443
                *prot |= PAGE_WRITE;
444
            return 0;
445
        }
446
    }
447
#ifdef DEBUG_MMU
448
    printf("DMISS at 0x%" PRIx64 "\n", address);
449
#endif
450
    env->exception_index = TT_DMISS;
451
    return 1;
452
}
453

    
454
static int get_physical_address_code(CPUState *env,
455
                                     target_phys_addr_t *physical, int *prot,
456
                                     target_ulong address, int is_user)
457
{
458
    target_ulong mask;
459
    unsigned int i;
460

    
461
    if ((env->lsu & IMMU_E) == 0) { /* IMMU disabled */
462
        *physical = address;
463
        *prot = PAGE_EXEC;
464
        return 0;
465
    }
466

    
467
    for (i = 0; i < 64; i++) {
468
        switch ((env->itlb_tte[i] >> 61) & 3) {
469
        default:
470
        case 0x0: // 8k
471
            mask = 0xffffffffffffe000ULL;
472
            break;
473
        case 0x1: // 64k
474
            mask = 0xffffffffffff0000ULL;
475
            break;
476
        case 0x2: // 512k
477
            mask = 0xfffffffffff80000ULL;
478
            break;
479
        case 0x3: // 4M
480
            mask = 0xffffffffffc00000ULL;
481
                break;
482
        }
483
        // ctx match, vaddr match?
484
        if (env->dmmuregs[1] == (env->itlb_tag[i] & 0x1fff) &&
485
            (address & mask) == (env->itlb_tag[i] & ~0x1fffULL)) {
486
            // valid, access ok?
487
            if ((env->itlb_tte[i] & 0x8000000000000000ULL) == 0 ||
488
                ((env->itlb_tte[i] & 0x4) && is_user)) {
489
                if (env->immuregs[3]) /* Fault status register */
490
                    env->immuregs[3] = 2; /* overflow (not read before
491
                                             another fault) */
492
                env->immuregs[3] |= (is_user << 3) | 1;
493
                env->exception_index = TT_TFAULT;
494
#ifdef DEBUG_MMU
495
                printf("TFAULT at 0x%" PRIx64 "\n", address);
496
#endif
497
                return 1;
498
            }
499
            *physical = (env->itlb_tte[i] & mask & 0x1fffffff000ULL) +
500
                (address & ~mask & 0x1fffffff000ULL);
501
            *prot = PAGE_EXEC;
502
            return 0;
503
        }
504
    }
505
#ifdef DEBUG_MMU
506
    printf("TMISS at 0x%" PRIx64 "\n", address);
507
#endif
508
    env->exception_index = TT_TMISS;
509
    return 1;
510
}
511

    
512
static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
513
                                int *prot, int *access_index,
514
                                target_ulong address, int rw, int mmu_idx)
515
{
516
    int is_user = mmu_idx == MMU_USER_IDX;
517

    
518
    if (rw == 2)
519
        return get_physical_address_code(env, physical, prot, address,
520
                                         is_user);
521
    else
522
        return get_physical_address_data(env, physical, prot, address, rw,
523
                                         is_user);
524
}
525

    
526
/* Perform address translation */
527
int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
528
                              int mmu_idx, int is_softmmu)
529
{
530
    target_ulong virt_addr, vaddr;
531
    target_phys_addr_t paddr;
532
    int error_code = 0, prot, ret = 0, access_index;
533

    
534
    error_code = get_physical_address(env, &paddr, &prot, &access_index,
535
                                      address, rw, mmu_idx);
536
    if (error_code == 0) {
537
        virt_addr = address & TARGET_PAGE_MASK;
538
        vaddr = virt_addr + ((address & TARGET_PAGE_MASK) &
539
                             (TARGET_PAGE_SIZE - 1));
540
#ifdef DEBUG_MMU
541
        printf("Translate at 0x%" PRIx64 " -> 0x%" PRIx64 ", vaddr 0x%" PRIx64
542
               "\n", address, paddr, vaddr);
543
#endif
544
        ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
545
        return ret;
546
    }
547
    // XXX
548
    return 1;
549
}
550

    
551
#ifdef DEBUG_MMU
552
void dump_mmu(CPUState *env)
553
{
554
    unsigned int i;
555
    const char *mask;
556

    
557
    printf("MMU contexts: Primary: %" PRId64 ", Secondary: %" PRId64 "\n",
558
           env->dmmuregs[1], env->dmmuregs[2]);
559
    if ((env->lsu & DMMU_E) == 0) {
560
        printf("DMMU disabled\n");
561
    } else {
562
        printf("DMMU dump:\n");
563
        for (i = 0; i < 64; i++) {
564
            switch ((env->dtlb_tte[i] >> 61) & 3) {
565
            default:
566
            case 0x0:
567
                mask = "  8k";
568
                break;
569
            case 0x1:
570
                mask = " 64k";
571
                break;
572
            case 0x2:
573
                mask = "512k";
574
                break;
575
            case 0x3:
576
                mask = "  4M";
577
                break;
578
            }
579
            if ((env->dtlb_tte[i] & 0x8000000000000000ULL) != 0) {
580
                printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_lx
581
                       ", %s, %s, %s, %s, ctx %" PRId64 "\n",
582
                       env->dtlb_tag[i] & ~0x1fffULL,
583
                       env->dtlb_tte[i] & 0x1ffffffe000ULL,
584
                       mask,
585
                       env->dtlb_tte[i] & 0x4? "priv": "user",
586
                       env->dtlb_tte[i] & 0x2? "RW": "RO",
587
                       env->dtlb_tte[i] & 0x40? "locked": "unlocked",
588
                       env->dtlb_tag[i] & 0x1fffULL);
589
            }
590
        }
591
    }
592
    if ((env->lsu & IMMU_E) == 0) {
593
        printf("IMMU disabled\n");
594
    } else {
595
        printf("IMMU dump:\n");
596
        for (i = 0; i < 64; i++) {
597
            switch ((env->itlb_tte[i] >> 61) & 3) {
598
            default:
599
            case 0x0:
600
                mask = "  8k";
601
                break;
602
            case 0x1:
603
                mask = " 64k";
604
                break;
605
            case 0x2:
606
                mask = "512k";
607
                break;
608
            case 0x3:
609
                mask = "  4M";
610
                break;
611
            }
612
            if ((env->itlb_tte[i] & 0x8000000000000000ULL) != 0) {
613
                printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_lx
614
                       ", %s, %s, %s, ctx %" PRId64 "\n",
615
                       env->itlb_tag[i] & ~0x1fffULL,
616
                       env->itlb_tte[i] & 0x1ffffffe000ULL,
617
                       mask,
618
                       env->itlb_tte[i] & 0x4? "priv": "user",
619
                       env->itlb_tte[i] & 0x40? "locked": "unlocked",
620
                       env->itlb_tag[i] & 0x1fffULL);
621
            }
622
        }
623
    }
624
}
625
#endif /* DEBUG_MMU */
626

    
627
#endif /* TARGET_SPARC64 */
628
#endif /* !CONFIG_USER_ONLY */
629

    
630

    
631
#if defined(CONFIG_USER_ONLY)
632
target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
633
{
634
    return addr;
635
}
636

    
637
#else
638
target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
639
{
640
    target_phys_addr_t phys_addr;
641
    int prot, access_index;
642

    
643
    if (get_physical_address(env, &phys_addr, &prot, &access_index, addr, 2,
644
                             MMU_KERNEL_IDX) != 0)
645
        if (get_physical_address(env, &phys_addr, &prot, &access_index, addr,
646
                                 0, MMU_KERNEL_IDX) != 0)
647
            return -1;
648
    if (cpu_get_physical_page_desc(phys_addr) == IO_MEM_UNASSIGNED)
649
        return -1;
650
    return phys_addr;
651
}
652
#endif
653

    
654
void memcpy32(target_ulong *dst, const target_ulong *src)
655
{
656
    dst[0] = src[0];
657
    dst[1] = src[1];
658
    dst[2] = src[2];
659
    dst[3] = src[3];
660
    dst[4] = src[4];
661
    dst[5] = src[5];
662
    dst[6] = src[6];
663
    dst[7] = src[7];
664
}
665

    
666
void helper_flush(target_ulong addr)
667
{
668
    addr &= ~7;
669
    tb_invalidate_page_range(addr, addr + 8);
670
}
671

    
672
void cpu_reset(CPUSPARCState *env)
673
{
674
    tlb_flush(env, 1);
675
    env->cwp = 0;
676
    env->wim = 1;
677
    env->regwptr = env->regbase + (env->cwp * 16);
678
#if defined(CONFIG_USER_ONLY)
679
    env->user_mode_only = 1;
680
#ifdef TARGET_SPARC64
681
    env->cleanwin = NWINDOWS - 2;
682
    env->cansave = NWINDOWS - 2;
683
    env->pstate = PS_RMO | PS_PEF | PS_IE;
684
    env->asi = 0x82; // Primary no-fault
685
#endif
686
#else
687
    env->psret = 0;
688
    env->psrs = 1;
689
    env->psrps = 1;
690
#ifdef TARGET_SPARC64
691
    env->pstate = PS_PRIV;
692
    env->hpstate = HS_PRIV;
693
    env->pc = 0x1fff0000000ULL;
694
    env->tsptr = &env->ts[env->tl];
695
#else
696
    env->pc = 0;
697
    env->mmuregs[0] &= ~(MMU_E | MMU_NF);
698
    env->mmuregs[0] |= env->mmu_bm;
699
#endif
700
    env->npc = env->pc + 4;
701
#endif
702
}
703

    
704
static int cpu_sparc_register(CPUSPARCState *env, const char *cpu_model)
705
{
706
    sparc_def_t def1, *def = &def1;
707

    
708
    if (cpu_sparc_find_by_name(def, cpu_model) < 0)
709
        return -1;
710

    
711
    env->features = def->features;
712
    env->cpu_model_str = cpu_model;
713
    env->version = def->iu_version;
714
    env->fsr = def->fpu_version;
715
#if !defined(TARGET_SPARC64)
716
    env->mmu_bm = def->mmu_bm;
717
    env->mmu_ctpr_mask = def->mmu_ctpr_mask;
718
    env->mmu_cxr_mask = def->mmu_cxr_mask;
719
    env->mmu_sfsr_mask = def->mmu_sfsr_mask;
720
    env->mmu_trcr_mask = def->mmu_trcr_mask;
721
    env->mmuregs[0] |= def->mmu_version;
722
    cpu_sparc_set_id(env, 0);
723
#endif
724
    return 0;
725
}
726

    
727
static void cpu_sparc_close(CPUSPARCState *env)
728
{
729
    free(env);
730
}
731

    
732
CPUSPARCState *cpu_sparc_init(const char *cpu_model)
733
{
734
    CPUSPARCState *env;
735

    
736
    env = qemu_mallocz(sizeof(CPUSPARCState));
737
    if (!env)
738
        return NULL;
739
    cpu_exec_init(env);
740

    
741
    gen_intermediate_code_init(env);
742

    
743
    if (cpu_sparc_register(env, cpu_model) < 0) {
744
        cpu_sparc_close(env);
745
        return NULL;
746
    }
747
    cpu_reset(env);
748

    
749
    return env;
750
}
751

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

    
759
static const sparc_def_t sparc_defs[] = {
760
#ifdef TARGET_SPARC64
761
    {
762
        .name = "Fujitsu Sparc64",
763
        .iu_version = ((0x04ULL << 48) | (0x02ULL << 32) | (0ULL << 24)
764
                       | (MAXTL << 8) | (NWINDOWS - 1)),
765
        .fpu_version = 0x00000000,
766
        .mmu_version = 0,
767
        .features = CPU_DEFAULT_FEATURES,
768
    },
769
    {
770
        .name = "Fujitsu Sparc64 III",
771
        .iu_version = ((0x04ULL << 48) | (0x03ULL << 32) | (0ULL << 24)
772
                       | (MAXTL << 8) | (NWINDOWS - 1)),
773
        .fpu_version = 0x00000000,
774
        .mmu_version = 0,
775
        .features = CPU_DEFAULT_FEATURES,
776
    },
777
    {
778
        .name = "Fujitsu Sparc64 IV",
779
        .iu_version = ((0x04ULL << 48) | (0x04ULL << 32) | (0ULL << 24)
780
                       | (MAXTL << 8) | (NWINDOWS - 1)),
781
        .fpu_version = 0x00000000,
782
        .mmu_version = 0,
783
        .features = CPU_DEFAULT_FEATURES,
784
    },
785
    {
786
        .name = "Fujitsu Sparc64 V",
787
        .iu_version = ((0x04ULL << 48) | (0x05ULL << 32) | (0x51ULL << 24)
788
                       | (MAXTL << 8) | (NWINDOWS - 1)),
789
        .fpu_version = 0x00000000,
790
        .mmu_version = 0,
791
        .features = CPU_DEFAULT_FEATURES,
792
    },
793
    {
794
        .name = "TI UltraSparc I",
795
        .iu_version = ((0x17ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)
796
                       | (MAXTL << 8) | (NWINDOWS - 1)),
797
        .fpu_version = 0x00000000,
798
        .mmu_version = 0,
799
        .features = CPU_DEFAULT_FEATURES,
800
    },
801
    {
802
        .name = "TI UltraSparc II",
803
        .iu_version = ((0x17ULL << 48) | (0x11ULL << 32) | (0x20ULL << 24)
804
                       | (MAXTL << 8) | (NWINDOWS - 1)),
805
        .fpu_version = 0x00000000,
806
        .mmu_version = 0,
807
        .features = CPU_DEFAULT_FEATURES,
808
    },
809
    {
810
        .name = "TI UltraSparc IIi",
811
        .iu_version = ((0x17ULL << 48) | (0x12ULL << 32) | (0x91ULL << 24)
812
                       | (MAXTL << 8) | (NWINDOWS - 1)),
813
        .fpu_version = 0x00000000,
814
        .mmu_version = 0,
815
        .features = CPU_DEFAULT_FEATURES,
816
    },
817
    {
818
        .name = "TI UltraSparc IIe",
819
        .iu_version = ((0x17ULL << 48) | (0x13ULL << 32) | (0x14ULL << 24)
820
                       | (MAXTL << 8) | (NWINDOWS - 1)),
821
        .fpu_version = 0x00000000,
822
        .mmu_version = 0,
823
        .features = CPU_DEFAULT_FEATURES,
824
    },
825
    {
826
        .name = "Sun UltraSparc III",
827
        .iu_version = ((0x3eULL << 48) | (0x14ULL << 32) | (0x34ULL << 24)
828
                       | (MAXTL << 8) | (NWINDOWS - 1)),
829
        .fpu_version = 0x00000000,
830
        .mmu_version = 0,
831
        .features = CPU_DEFAULT_FEATURES,
832
    },
833
    {
834
        .name = "Sun UltraSparc III Cu",
835
        .iu_version = ((0x3eULL << 48) | (0x15ULL << 32) | (0x41ULL << 24)
836
                       | (MAXTL << 8) | (NWINDOWS - 1)),
837
        .fpu_version = 0x00000000,
838
        .mmu_version = 0,
839
        .features = CPU_DEFAULT_FEATURES,
840
    },
841
    {
842
        .name = "Sun UltraSparc IIIi",
843
        .iu_version = ((0x3eULL << 48) | (0x16ULL << 32) | (0x34ULL << 24)
844
                       | (MAXTL << 8) | (NWINDOWS - 1)),
845
        .fpu_version = 0x00000000,
846
        .mmu_version = 0,
847
        .features = CPU_DEFAULT_FEATURES,
848
    },
849
    {
850
        .name = "Sun UltraSparc IV",
851
        .iu_version = ((0x3eULL << 48) | (0x18ULL << 32) | (0x31ULL << 24)
852
                       | (MAXTL << 8) | (NWINDOWS - 1)),
853
        .fpu_version = 0x00000000,
854
        .mmu_version = 0,
855
        .features = CPU_DEFAULT_FEATURES,
856
    },
857
    {
858
        .name = "Sun UltraSparc IV+",
859
        .iu_version = ((0x3eULL << 48) | (0x19ULL << 32) | (0x22ULL << 24)
860
                       | (MAXTL << 8) | (NWINDOWS - 1)),
861
        .fpu_version = 0x00000000,
862
        .mmu_version = 0,
863
        .features = CPU_DEFAULT_FEATURES,
864
    },
865
    {
866
        .name = "Sun UltraSparc IIIi+",
867
        .iu_version = ((0x3eULL << 48) | (0x22ULL << 32) | (0ULL << 24)
868
                       | (MAXTL << 8) | (NWINDOWS - 1)),
869
        .fpu_version = 0x00000000,
870
        .mmu_version = 0,
871
        .features = CPU_DEFAULT_FEATURES,
872
    },
873
    {
874
        .name = "NEC UltraSparc I",
875
        .iu_version = ((0x22ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)
876
                       | (MAXTL << 8) | (NWINDOWS - 1)),
877
        .fpu_version = 0x00000000,
878
        .mmu_version = 0,
879
        .features = CPU_DEFAULT_FEATURES,
880
    },
881
#else
882
    {
883
        .name = "Fujitsu MB86900",
884
        .iu_version = 0x00 << 24, /* Impl 0, ver 0 */
885
        .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
886
        .mmu_version = 0x00 << 24, /* Impl 0, ver 0 */
887
        .mmu_bm = 0x00004000,
888
        .mmu_ctpr_mask = 0x007ffff0,
889
        .mmu_cxr_mask = 0x0000003f,
890
        .mmu_sfsr_mask = 0xffffffff,
891
        .mmu_trcr_mask = 0xffffffff,
892
        .features = CPU_FEATURE_FLOAT,
893
    },
894
    {
895
        .name = "Fujitsu MB86904",
896
        .iu_version = 0x04 << 24, /* Impl 0, ver 4 */
897
        .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
898
        .mmu_version = 0x04 << 24, /* Impl 0, ver 4 */
899
        .mmu_bm = 0x00004000,
900
        .mmu_ctpr_mask = 0x00ffffc0,
901
        .mmu_cxr_mask = 0x000000ff,
902
        .mmu_sfsr_mask = 0x00016fff,
903
        .mmu_trcr_mask = 0x00ffffff,
904
        .features = CPU_DEFAULT_FEATURES,
905
    },
906
    {
907
        .name = "Fujitsu MB86907",
908
        .iu_version = 0x05 << 24, /* Impl 0, ver 5 */
909
        .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
910
        .mmu_version = 0x05 << 24, /* Impl 0, ver 5 */
911
        .mmu_bm = 0x00004000,
912
        .mmu_ctpr_mask = 0xffffffc0,
913
        .mmu_cxr_mask = 0x000000ff,
914
        .mmu_sfsr_mask = 0x00016fff,
915
        .mmu_trcr_mask = 0xffffffff,
916
        .features = CPU_DEFAULT_FEATURES,
917
    },
918
    {
919
        .name = "LSI L64811",
920
        .iu_version = 0x10 << 24, /* Impl 1, ver 0 */
921
        .fpu_version = 1 << 17, /* FPU version 1 (LSI L64814) */
922
        .mmu_version = 0x10 << 24,
923
        .mmu_bm = 0x00004000,
924
        .mmu_ctpr_mask = 0x007ffff0,
925
        .mmu_cxr_mask = 0x0000003f,
926
        .mmu_sfsr_mask = 0xffffffff,
927
        .mmu_trcr_mask = 0xffffffff,
928
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT,
929
    },
930
    {
931
        .name = "Cypress CY7C601",
932
        .iu_version = 0x11 << 24, /* Impl 1, ver 1 */
933
        .fpu_version = 3 << 17, /* FPU version 3 (Cypress CY7C602) */
934
        .mmu_version = 0x10 << 24,
935
        .mmu_bm = 0x00004000,
936
        .mmu_ctpr_mask = 0x007ffff0,
937
        .mmu_cxr_mask = 0x0000003f,
938
        .mmu_sfsr_mask = 0xffffffff,
939
        .mmu_trcr_mask = 0xffffffff,
940
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT,
941
    },
942
    {
943
        .name = "Cypress CY7C611",
944
        .iu_version = 0x13 << 24, /* Impl 1, ver 3 */
945
        .fpu_version = 3 << 17, /* FPU version 3 (Cypress CY7C602) */
946
        .mmu_version = 0x10 << 24,
947
        .mmu_bm = 0x00004000,
948
        .mmu_ctpr_mask = 0x007ffff0,
949
        .mmu_cxr_mask = 0x0000003f,
950
        .mmu_sfsr_mask = 0xffffffff,
951
        .mmu_trcr_mask = 0xffffffff,
952
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT,
953
    },
954
    {
955
        .name = "TI SuperSparc II",
956
        .iu_version = 0x40000000,
957
        .fpu_version = 0 << 17,
958
        .mmu_version = 0x04000000,
959
        .mmu_bm = 0x00002000,
960
        .mmu_ctpr_mask = 0xffffffc0,
961
        .mmu_cxr_mask = 0x0000ffff,
962
        .mmu_sfsr_mask = 0xffffffff,
963
        .mmu_trcr_mask = 0xffffffff,
964
        .features = CPU_DEFAULT_FEATURES,
965
    },
966
    {
967
        .name = "TI MicroSparc I",
968
        .iu_version = 0x41000000,
969
        .fpu_version = 4 << 17,
970
        .mmu_version = 0x41000000,
971
        .mmu_bm = 0x00004000,
972
        .mmu_ctpr_mask = 0x007ffff0,
973
        .mmu_cxr_mask = 0x0000003f,
974
        .mmu_sfsr_mask = 0x00016fff,
975
        .mmu_trcr_mask = 0x0000003f,
976
        .features = CPU_DEFAULT_FEATURES,
977
    },
978
    {
979
        .name = "TI MicroSparc II",
980
        .iu_version = 0x42000000,
981
        .fpu_version = 4 << 17,
982
        .mmu_version = 0x02000000,
983
        .mmu_bm = 0x00004000,
984
        .mmu_ctpr_mask = 0x00ffffc0,
985
        .mmu_cxr_mask = 0x000000ff,
986
        .mmu_sfsr_mask = 0x00016fff,
987
        .mmu_trcr_mask = 0x00ffffff,
988
        .features = CPU_DEFAULT_FEATURES,
989
    },
990
    {
991
        .name = "TI MicroSparc IIep",
992
        .iu_version = 0x42000000,
993
        .fpu_version = 4 << 17,
994
        .mmu_version = 0x04000000,
995
        .mmu_bm = 0x00004000,
996
        .mmu_ctpr_mask = 0x00ffffc0,
997
        .mmu_cxr_mask = 0x000000ff,
998
        .mmu_sfsr_mask = 0x00016bff,
999
        .mmu_trcr_mask = 0x00ffffff,
1000
        .features = CPU_DEFAULT_FEATURES,
1001
    },
1002
    {
1003
        .name = "TI SuperSparc 51",
1004
        .iu_version = 0x43000000,
1005
        .fpu_version = 0 << 17,
1006
        .mmu_version = 0x04000000,
1007
        .mmu_bm = 0x00002000,
1008
        .mmu_ctpr_mask = 0xffffffc0,
1009
        .mmu_cxr_mask = 0x0000ffff,
1010
        .mmu_sfsr_mask = 0xffffffff,
1011
        .mmu_trcr_mask = 0xffffffff,
1012
        .features = CPU_DEFAULT_FEATURES,
1013
    },
1014
    {
1015
        .name = "TI SuperSparc 61",
1016
        .iu_version = 0x44000000,
1017
        .fpu_version = 0 << 17,
1018
        .mmu_version = 0x04000000,
1019
        .mmu_bm = 0x00002000,
1020
        .mmu_ctpr_mask = 0xffffffc0,
1021
        .mmu_cxr_mask = 0x0000ffff,
1022
        .mmu_sfsr_mask = 0xffffffff,
1023
        .mmu_trcr_mask = 0xffffffff,
1024
        .features = CPU_DEFAULT_FEATURES,
1025
    },
1026
    {
1027
        .name = "Ross RT625",
1028
        .iu_version = 0x1e000000,
1029
        .fpu_version = 1 << 17,
1030
        .mmu_version = 0x1e000000,
1031
        .mmu_bm = 0x00004000,
1032
        .mmu_ctpr_mask = 0x007ffff0,
1033
        .mmu_cxr_mask = 0x0000003f,
1034
        .mmu_sfsr_mask = 0xffffffff,
1035
        .mmu_trcr_mask = 0xffffffff,
1036
        .features = CPU_DEFAULT_FEATURES,
1037
    },
1038
    {
1039
        .name = "Ross RT620",
1040
        .iu_version = 0x1f000000,
1041
        .fpu_version = 1 << 17,
1042
        .mmu_version = 0x1f000000,
1043
        .mmu_bm = 0x00004000,
1044
        .mmu_ctpr_mask = 0x007ffff0,
1045
        .mmu_cxr_mask = 0x0000003f,
1046
        .mmu_sfsr_mask = 0xffffffff,
1047
        .mmu_trcr_mask = 0xffffffff,
1048
        .features = CPU_DEFAULT_FEATURES,
1049
    },
1050
    {
1051
        .name = "BIT B5010",
1052
        .iu_version = 0x20000000,
1053
        .fpu_version = 0 << 17, /* B5010/B5110/B5120/B5210 */
1054
        .mmu_version = 0x20000000,
1055
        .mmu_bm = 0x00004000,
1056
        .mmu_ctpr_mask = 0x007ffff0,
1057
        .mmu_cxr_mask = 0x0000003f,
1058
        .mmu_sfsr_mask = 0xffffffff,
1059
        .mmu_trcr_mask = 0xffffffff,
1060
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT,
1061
    },
1062
    {
1063
        .name = "Matsushita MN10501",
1064
        .iu_version = 0x50000000,
1065
        .fpu_version = 0 << 17,
1066
        .mmu_version = 0x50000000,
1067
        .mmu_bm = 0x00004000,
1068
        .mmu_ctpr_mask = 0x007ffff0,
1069
        .mmu_cxr_mask = 0x0000003f,
1070
        .mmu_sfsr_mask = 0xffffffff,
1071
        .mmu_trcr_mask = 0xffffffff,
1072
        .features = CPU_FEATURE_FLOAT | CPU_FEATURE_MUL | CPU_FEATURE_FSQRT,
1073
    },
1074
    {
1075
        .name = "Weitek W8601",
1076
        .iu_version = 0x90 << 24, /* Impl 9, ver 0 */
1077
        .fpu_version = 3 << 17, /* FPU version 3 (Weitek WTL3170/2) */
1078
        .mmu_version = 0x10 << 24,
1079
        .mmu_bm = 0x00004000,
1080
        .mmu_ctpr_mask = 0x007ffff0,
1081
        .mmu_cxr_mask = 0x0000003f,
1082
        .mmu_sfsr_mask = 0xffffffff,
1083
        .mmu_trcr_mask = 0xffffffff,
1084
        .features = CPU_DEFAULT_FEATURES,
1085
    },
1086
    {
1087
        .name = "LEON2",
1088
        .iu_version = 0xf2000000,
1089
        .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1090
        .mmu_version = 0xf2000000,
1091
        .mmu_bm = 0x00004000,
1092
        .mmu_ctpr_mask = 0x007ffff0,
1093
        .mmu_cxr_mask = 0x0000003f,
1094
        .mmu_sfsr_mask = 0xffffffff,
1095
        .mmu_trcr_mask = 0xffffffff,
1096
        .features = CPU_DEFAULT_FEATURES,
1097
    },
1098
    {
1099
        .name = "LEON3",
1100
        .iu_version = 0xf3000000,
1101
        .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1102
        .mmu_version = 0xf3000000,
1103
        .mmu_bm = 0x00004000,
1104
        .mmu_ctpr_mask = 0x007ffff0,
1105
        .mmu_cxr_mask = 0x0000003f,
1106
        .mmu_sfsr_mask = 0xffffffff,
1107
        .mmu_trcr_mask = 0xffffffff,
1108
        .features = CPU_DEFAULT_FEATURES,
1109
    },
1110
#endif
1111
};
1112

    
1113
static const char * const feature_name[] = {
1114
    "float",
1115
    "float128",
1116
    "swap",
1117
    "mul",
1118
    "div",
1119
    "flush",
1120
    "fsqrt",
1121
    "fmul",
1122
    "vis1",
1123
    "vis2",
1124
};
1125

    
1126
static void print_features(FILE *f,
1127
                           int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
1128
                           uint32_t features, const char *prefix)
1129
{
1130
    unsigned int i;
1131

    
1132
    for (i = 0; i < ARRAY_SIZE(feature_name); i++)
1133
        if (feature_name[i] && (features & (1 << i))) {
1134
            if (prefix)
1135
                (*cpu_fprintf)(f, "%s", prefix);
1136
            (*cpu_fprintf)(f, "%s ", feature_name[i]);
1137
        }
1138
}
1139

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

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

    
1152
static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model)
1153
{
1154
    unsigned int i;
1155
    const sparc_def_t *def = NULL;
1156
    char *s = strdup(cpu_model);
1157
    char *featurestr, *name = strtok(s, ",");
1158
    uint32_t plus_features = 0;
1159
    uint32_t minus_features = 0;
1160
    long long iu_version;
1161
    uint32_t fpu_version, mmu_version;
1162

    
1163
    for (i = 0; i < sizeof(sparc_defs) / sizeof(sparc_def_t); i++) {
1164
        if (strcasecmp(name, sparc_defs[i].name) == 0) {
1165
            def = &sparc_defs[i];
1166
        }
1167
    }
1168
    if (!def)
1169
        goto error;
1170
    memcpy(cpu_def, def, sizeof(*def));
1171

    
1172
    featurestr = strtok(NULL, ",");
1173
    while (featurestr) {
1174
        char *val;
1175

    
1176
        if (featurestr[0] == '+') {
1177
            add_flagname_to_bitmaps(featurestr + 1, &plus_features);
1178
        } else if (featurestr[0] == '-') {
1179
            add_flagname_to_bitmaps(featurestr + 1, &minus_features);
1180
        } else if ((val = strchr(featurestr, '='))) {
1181
            *val = 0; val++;
1182
            if (!strcmp(featurestr, "iu_version")) {
1183
                char *err;
1184

    
1185
                iu_version = strtoll(val, &err, 0);
1186
                if (!*val || *err) {
1187
                    fprintf(stderr, "bad numerical value %s\n", val);
1188
                    goto error;
1189
                }
1190
                cpu_def->iu_version = iu_version;
1191
#ifdef DEBUG_FEATURES
1192
                fprintf(stderr, "iu_version %llx\n", iu_version);
1193
#endif
1194
            } else if (!strcmp(featurestr, "fpu_version")) {
1195
                char *err;
1196

    
1197
                fpu_version = strtol(val, &err, 0);
1198
                if (!*val || *err) {
1199
                    fprintf(stderr, "bad numerical value %s\n", val);
1200
                    goto error;
1201
                }
1202
                cpu_def->fpu_version = fpu_version;
1203
#ifdef DEBUG_FEATURES
1204
                fprintf(stderr, "fpu_version %llx\n", fpu_version);
1205
#endif
1206
            } else if (!strcmp(featurestr, "mmu_version")) {
1207
                char *err;
1208

    
1209
                mmu_version = strtol(val, &err, 0);
1210
                if (!*val || *err) {
1211
                    fprintf(stderr, "bad numerical value %s\n", val);
1212
                    goto error;
1213
                }
1214
                cpu_def->mmu_version = mmu_version;
1215
#ifdef DEBUG_FEATURES
1216
                fprintf(stderr, "mmu_version %llx\n", mmu_version);
1217
#endif
1218
            } else {
1219
                fprintf(stderr, "unrecognized feature %s\n", featurestr);
1220
                goto error;
1221
            }
1222
        } else {
1223
            fprintf(stderr, "feature string `%s' not in format "
1224
                    "(+feature|-feature|feature=xyz)\n", featurestr);
1225
            goto error;
1226
        }
1227
        featurestr = strtok(NULL, ",");
1228
    }
1229
    cpu_def->features |= plus_features;
1230
    cpu_def->features &= ~minus_features;
1231
#ifdef DEBUG_FEATURES
1232
    print_features(stderr, fprintf, cpu_def->features, NULL);
1233
#endif
1234
    free(s);
1235
    return 0;
1236

    
1237
 error:
1238
    free(s);
1239
    return -1;
1240
}
1241

    
1242
void sparc_cpu_list(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
1243
{
1244
    unsigned int i;
1245

    
1246
    for (i = 0; i < sizeof(sparc_defs) / sizeof(sparc_def_t); i++) {
1247
        (*cpu_fprintf)(f, "Sparc %16s IU " TARGET_FMT_lx " FPU %08x MMU %08x ",
1248
                       sparc_defs[i].name,
1249
                       sparc_defs[i].iu_version,
1250
                       sparc_defs[i].fpu_version,
1251
                       sparc_defs[i].mmu_version);
1252
        print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES &
1253
                       ~sparc_defs[i].features, "-");
1254
        print_features(f, cpu_fprintf, ~CPU_DEFAULT_FEATURES &
1255
                       sparc_defs[i].features, "+");
1256
        (*cpu_fprintf)(f, "\n");
1257
    }
1258
    (*cpu_fprintf)(f, "CPU feature flags (+/-): ");
1259
    print_features(f, cpu_fprintf, -1, NULL);
1260
    (*cpu_fprintf)(f, "\n");
1261
    (*cpu_fprintf)(f, "Numerical features (=): iu_version fpu_version "
1262
                   "mmu_version\n");
1263
}
1264

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

    
1267
void cpu_dump_state(CPUState *env, FILE *f,
1268
                    int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
1269
                    int flags)
1270
{
1271
    int i, x;
1272

    
1273
    cpu_fprintf(f, "pc: " TARGET_FMT_lx "  npc: " TARGET_FMT_lx "\n", env->pc,
1274
                env->npc);
1275
    cpu_fprintf(f, "General Registers:\n");
1276
    for (i = 0; i < 4; i++)
1277
        cpu_fprintf(f, "%%g%c: " TARGET_FMT_lx "\t", i + '0', env->gregs[i]);
1278
    cpu_fprintf(f, "\n");
1279
    for (; i < 8; i++)
1280
        cpu_fprintf(f, "%%g%c: " TARGET_FMT_lx "\t", i + '0', env->gregs[i]);
1281
    cpu_fprintf(f, "\nCurrent Register Window:\n");
1282
    for (x = 0; x < 3; x++) {
1283
        for (i = 0; i < 4; i++)
1284
            cpu_fprintf(f, "%%%c%d: " TARGET_FMT_lx "\t",
1285
                    (x == 0 ? 'o' : (x == 1 ? 'l' : 'i')), i,
1286
                    env->regwptr[i + x * 8]);
1287
        cpu_fprintf(f, "\n");
1288
        for (; i < 8; i++)
1289
            cpu_fprintf(f, "%%%c%d: " TARGET_FMT_lx "\t",
1290
                    (x == 0 ? 'o' : x == 1 ? 'l' : 'i'), i,
1291
                    env->regwptr[i + x * 8]);
1292
        cpu_fprintf(f, "\n");
1293
    }
1294
    cpu_fprintf(f, "\nFloating Point Registers:\n");
1295
    for (i = 0; i < 32; i++) {
1296
        if ((i & 3) == 0)
1297
            cpu_fprintf(f, "%%f%02d:", i);
1298
        cpu_fprintf(f, " %016lf", env->fpr[i]);
1299
        if ((i & 3) == 3)
1300
            cpu_fprintf(f, "\n");
1301
    }
1302
#ifdef TARGET_SPARC64
1303
    cpu_fprintf(f, "pstate: 0x%08x ccr: 0x%02x asi: 0x%02x tl: %d fprs: %d\n",
1304
                env->pstate, GET_CCR(env), env->asi, env->tl, env->fprs);
1305
    cpu_fprintf(f, "cansave: %d canrestore: %d otherwin: %d wstate %d "
1306
                "cleanwin %d cwp %d\n",
1307
                env->cansave, env->canrestore, env->otherwin, env->wstate,
1308
                env->cleanwin, NWINDOWS - 1 - env->cwp);
1309
#else
1310
    cpu_fprintf(f, "psr: 0x%08x -> %c%c%c%c %c%c%c wim: 0x%08x\n",
1311
                GET_PSR(env), GET_FLAG(PSR_ZERO, 'Z'), GET_FLAG(PSR_OVF, 'V'),
1312
                GET_FLAG(PSR_NEG, 'N'), GET_FLAG(PSR_CARRY, 'C'),
1313
                env->psrs?'S':'-', env->psrps?'P':'-',
1314
                env->psret?'E':'-', env->wim);
1315
#endif
1316
    cpu_fprintf(f, "fsr: 0x%08x\n", GET_FSR32(env));
1317
}
1318

    
1319
#ifdef TARGET_SPARC64
1320
#if !defined(CONFIG_USER_ONLY)
1321
#include "qemu-common.h"
1322
#include "hw/irq.h"
1323
#include "qemu-timer.h"
1324
#endif
1325

    
1326
void helper_tick_set_count(void *opaque, uint64_t count)
1327
{
1328
#if !defined(CONFIG_USER_ONLY)
1329
    ptimer_set_count(opaque, -count);
1330
#endif
1331
}
1332

    
1333
uint64_t helper_tick_get_count(void *opaque)
1334
{
1335
#if !defined(CONFIG_USER_ONLY)
1336
    return -ptimer_get_count(opaque);
1337
#else
1338
    return 0;
1339
#endif
1340
}
1341

    
1342
void helper_tick_set_limit(void *opaque, uint64_t limit)
1343
{
1344
#if !defined(CONFIG_USER_ONLY)
1345
    ptimer_set_limit(opaque, -limit, 0);
1346
#endif
1347
}
1348
#endif