]> Git Repo - qemu.git/blob - target-sparc/helper.c
multiboot: Use signed type for negative error numbers
[qemu.git] / target-sparc / helper.c
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, see <http://www.gnu.org/licenses/>.
18  */
19 #include <stdarg.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <inttypes.h>
24 #include <signal.h>
25
26 #include "cpu.h"
27 #include "exec-all.h"
28 #include "qemu-common.h"
29
30 //#define DEBUG_MMU
31 //#define DEBUG_FEATURES
32
33 static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model);
34
35 /* Sparc MMU emulation */
36
37 /* thread support */
38
39 static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
40
41 void cpu_lock(void)
42 {
43     spin_lock(&global_cpu_lock);
44 }
45
46 void cpu_unlock(void)
47 {
48     spin_unlock(&global_cpu_lock);
49 }
50
51 #if defined(CONFIG_USER_ONLY)
52
53 int cpu_sparc_handle_mmu_fault(CPUState *env1, target_ulong address, int rw,
54                                int mmu_idx, int is_softmmu)
55 {
56     if (rw & 2)
57         env1->exception_index = TT_TFAULT;
58     else
59         env1->exception_index = TT_DFAULT;
60     return 1;
61 }
62
63 #else
64
65 #ifndef TARGET_SPARC64
66 /*
67  * Sparc V8 Reference MMU (SRMMU)
68  */
69 static const int access_table[8][8] = {
70     { 0, 0, 0, 0, 8, 0, 12, 12 },
71     { 0, 0, 0, 0, 8, 0, 0, 0 },
72     { 8, 8, 0, 0, 0, 8, 12, 12 },
73     { 8, 8, 0, 0, 0, 8, 0, 0 },
74     { 8, 0, 8, 0, 8, 8, 12, 12 },
75     { 8, 0, 8, 0, 8, 0, 8, 0 },
76     { 8, 8, 8, 0, 8, 8, 12, 12 },
77     { 8, 8, 8, 0, 8, 8, 8, 0 }
78 };
79
80 static const int perm_table[2][8] = {
81     {
82         PAGE_READ,
83         PAGE_READ | PAGE_WRITE,
84         PAGE_READ | PAGE_EXEC,
85         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
86         PAGE_EXEC,
87         PAGE_READ | PAGE_WRITE,
88         PAGE_READ | PAGE_EXEC,
89         PAGE_READ | PAGE_WRITE | PAGE_EXEC
90     },
91     {
92         PAGE_READ,
93         PAGE_READ | PAGE_WRITE,
94         PAGE_READ | PAGE_EXEC,
95         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
96         PAGE_EXEC,
97         PAGE_READ,
98         0,
99         0,
100     }
101 };
102
103 static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
104                                 int *prot, int *access_index,
105                                 target_ulong address, int rw, int mmu_idx)
106 {
107     int access_perms = 0;
108     target_phys_addr_t pde_ptr;
109     uint32_t pde;
110     target_ulong virt_addr;
111     int error_code = 0, is_dirty, is_user;
112     unsigned long page_offset;
113
114     is_user = mmu_idx == MMU_USER_IDX;
115     virt_addr = address & TARGET_PAGE_MASK;
116
117     if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
118         // Boot mode: instruction fetches are taken from PROM
119         if (rw == 2 && (env->mmuregs[0] & env->def->mmu_bm)) {
120             *physical = env->prom_addr | (address & 0x7ffffULL);
121             *prot = PAGE_READ | PAGE_EXEC;
122             return 0;
123         }
124         *physical = address;
125         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
126         return 0;
127     }
128
129     *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user? 0 : 1);
130     *physical = 0xffffffffffff0000ULL;
131
132     /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
133     /* Context base + context number */
134     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
135     pde = ldl_phys(pde_ptr);
136
137     /* Ctx pde */
138     switch (pde & PTE_ENTRYTYPE_MASK) {
139     default:
140     case 0: /* Invalid */
141         return 1 << 2;
142     case 2: /* L0 PTE, maybe should not happen? */
143     case 3: /* Reserved */
144         return 4 << 2;
145     case 1: /* L0 PDE */
146         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
147         pde = ldl_phys(pde_ptr);
148
149         switch (pde & PTE_ENTRYTYPE_MASK) {
150         default:
151         case 0: /* Invalid */
152             return (1 << 8) | (1 << 2);
153         case 3: /* Reserved */
154             return (1 << 8) | (4 << 2);
155         case 1: /* L1 PDE */
156             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
157             pde = ldl_phys(pde_ptr);
158
159             switch (pde & PTE_ENTRYTYPE_MASK) {
160             default:
161             case 0: /* Invalid */
162                 return (2 << 8) | (1 << 2);
163             case 3: /* Reserved */
164                 return (2 << 8) | (4 << 2);
165             case 1: /* L2 PDE */
166                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
167                 pde = ldl_phys(pde_ptr);
168
169                 switch (pde & PTE_ENTRYTYPE_MASK) {
170                 default:
171                 case 0: /* Invalid */
172                     return (3 << 8) | (1 << 2);
173                 case 1: /* PDE, should not happen */
174                 case 3: /* Reserved */
175                     return (3 << 8) | (4 << 2);
176                 case 2: /* L3 PTE */
177                     virt_addr = address & TARGET_PAGE_MASK;
178                     page_offset = (address & TARGET_PAGE_MASK) &
179                         (TARGET_PAGE_SIZE - 1);
180                 }
181                 break;
182             case 2: /* L2 PTE */
183                 virt_addr = address & ~0x3ffff;
184                 page_offset = address & 0x3ffff;
185             }
186             break;
187         case 2: /* L1 PTE */
188             virt_addr = address & ~0xffffff;
189             page_offset = address & 0xffffff;
190         }
191     }
192
193     /* update page modified and dirty bits */
194     is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
195     if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
196         pde |= PG_ACCESSED_MASK;
197         if (is_dirty)
198             pde |= PG_MODIFIED_MASK;
199         stl_phys_notdirty(pde_ptr, pde);
200     }
201     /* check access */
202     access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
203     error_code = access_table[*access_index][access_perms];
204     if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user))
205         return error_code;
206
207     /* the page can be put in the TLB */
208     *prot = perm_table[is_user][access_perms];
209     if (!(pde & PG_MODIFIED_MASK)) {
210         /* only set write access if already dirty... otherwise wait
211            for dirty access */
212         *prot &= ~PAGE_WRITE;
213     }
214
215     /* Even if large ptes, we map only one 4KB page in the cache to
216        avoid filling it too fast */
217     *physical = ((target_phys_addr_t)(pde & PTE_ADDR_MASK) << 4) + page_offset;
218     return error_code;
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)
224 {
225     target_phys_addr_t paddr;
226     target_ulong vaddr;
227     int error_code = 0, prot, ret = 0, access_index;
228
229     error_code = get_physical_address(env, &paddr, &prot, &access_index,
230                                       address, rw, mmu_idx);
231     if (error_code == 0) {
232         vaddr = address & TARGET_PAGE_MASK;
233         paddr &= TARGET_PAGE_MASK;
234 #ifdef DEBUG_MMU
235         printf("Translate at " TARGET_FMT_lx " -> " TARGET_FMT_plx ", vaddr "
236                TARGET_FMT_lx "\n", address, paddr, vaddr);
237 #endif
238         ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
239         return ret;
240     }
241
242     if (env->mmuregs[3]) /* Fault status register */
243         env->mmuregs[3] = 1; /* overflow (not read before another fault) */
244     env->mmuregs[3] |= (access_index << 5) | error_code | 2;
245     env->mmuregs[4] = address; /* Fault address register */
246
247     if ((env->mmuregs[0] & MMU_NF) || env->psret == 0)  {
248         // No fault mode: if a mapping is available, just override
249         // permissions. If no mapping is available, redirect accesses to
250         // neverland. Fake/overridden mappings will be flushed when
251         // switching to normal mode.
252         vaddr = address & TARGET_PAGE_MASK;
253         prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
254         ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
255         return ret;
256     } else {
257         if (rw & 2)
258             env->exception_index = TT_TFAULT;
259         else
260             env->exception_index = TT_DFAULT;
261         return 1;
262     }
263 }
264
265 target_ulong mmu_probe(CPUState *env, target_ulong address, int mmulev)
266 {
267     target_phys_addr_t pde_ptr;
268     uint32_t pde;
269
270     /* Context base + context number */
271     pde_ptr = (target_phys_addr_t)(env->mmuregs[1] << 4) +
272         (env->mmuregs[2] << 2);
273     pde = ldl_phys(pde_ptr);
274
275     switch (pde & PTE_ENTRYTYPE_MASK) {
276     default:
277     case 0: /* Invalid */
278     case 2: /* PTE, maybe should not happen? */
279     case 3: /* Reserved */
280         return 0;
281     case 1: /* L1 PDE */
282         if (mmulev == 3)
283             return pde;
284         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
285         pde = ldl_phys(pde_ptr);
286
287         switch (pde & PTE_ENTRYTYPE_MASK) {
288         default:
289         case 0: /* Invalid */
290         case 3: /* Reserved */
291             return 0;
292         case 2: /* L1 PTE */
293             return pde;
294         case 1: /* L2 PDE */
295             if (mmulev == 2)
296                 return pde;
297             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
298             pde = ldl_phys(pde_ptr);
299
300             switch (pde & PTE_ENTRYTYPE_MASK) {
301             default:
302             case 0: /* Invalid */
303             case 3: /* Reserved */
304                 return 0;
305             case 2: /* L2 PTE */
306                 return pde;
307             case 1: /* L3 PDE */
308                 if (mmulev == 1)
309                     return pde;
310                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
311                 pde = ldl_phys(pde_ptr);
312
313                 switch (pde & PTE_ENTRYTYPE_MASK) {
314                 default:
315                 case 0: /* Invalid */
316                 case 1: /* PDE, should not happen */
317                 case 3: /* Reserved */
318                     return 0;
319                 case 2: /* L3 PTE */
320                     return pde;
321                 }
322             }
323         }
324     }
325     return 0;
326 }
327
328 #ifdef DEBUG_MMU
329 void dump_mmu(CPUState *env)
330 {
331     target_ulong va, va1, va2;
332     unsigned int n, m, o;
333     target_phys_addr_t pde_ptr, pa;
334     uint32_t pde;
335
336     printf("MMU dump:\n");
337     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
338     pde = ldl_phys(pde_ptr);
339     printf("Root ptr: " TARGET_FMT_plx ", ctx: %d\n",
340            (target_phys_addr_t)env->mmuregs[1] << 4, env->mmuregs[2]);
341     for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
342         pde = mmu_probe(env, va, 2);
343         if (pde) {
344             pa = cpu_get_phys_page_debug(env, va);
345             printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
346                    " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
347             for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
348                 pde = mmu_probe(env, va1, 1);
349                 if (pde) {
350                     pa = cpu_get_phys_page_debug(env, va1);
351                     printf(" VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
352                            " PDE: " TARGET_FMT_lx "\n", va1, pa, pde);
353                     for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
354                         pde = mmu_probe(env, va2, 0);
355                         if (pde) {
356                             pa = cpu_get_phys_page_debug(env, va2);
357                             printf("  VA: " TARGET_FMT_lx ", PA: "
358                                    TARGET_FMT_plx " PTE: " TARGET_FMT_lx "\n",
359                                    va2, pa, pde);
360                         }
361                     }
362                 }
363             }
364         }
365     }
366     printf("MMU dump ends\n");
367 }
368 #endif /* DEBUG_MMU */
369
370 #else /* !TARGET_SPARC64 */
371
372 // 41 bit physical address space
373 static inline target_phys_addr_t ultrasparc_truncate_physical(uint64_t x)
374 {
375     return x & 0x1ffffffffffULL;
376 }
377
378 /*
379  * UltraSparc IIi I/DMMUs
380  */
381
382 static inline int compare_masked(uint64_t x, uint64_t y, uint64_t mask)
383 {
384     return (x & mask) == (y & mask);
385 }
386
387 // Returns true if TTE tag is valid and matches virtual address value in context
388 // requires virtual address mask value calculated from TTE entry size
389 static inline int ultrasparc_tag_match(SparcTLBEntry *tlb,
390                                        uint64_t address, uint64_t context,
391                                        target_phys_addr_t *physical)
392 {
393     uint64_t mask;
394
395     switch ((tlb->tte >> 61) & 3) {
396     default:
397     case 0x0: // 8k
398         mask = 0xffffffffffffe000ULL;
399         break;
400     case 0x1: // 64k
401         mask = 0xffffffffffff0000ULL;
402         break;
403     case 0x2: // 512k
404         mask = 0xfffffffffff80000ULL;
405         break;
406     case 0x3: // 4M
407         mask = 0xffffffffffc00000ULL;
408         break;
409     }
410
411     // valid, context match, virtual address match?
412     if (TTE_IS_VALID(tlb->tte) &&
413             compare_masked(context, tlb->tag, 0x1fff) &&
414             compare_masked(address, tlb->tag, mask))
415     {
416         // decode physical address
417         *physical = ((tlb->tte & mask) | (address & ~mask)) & 0x1ffffffe000ULL;
418         return 1;
419     }
420
421     return 0;
422 }
423
424 static int get_physical_address_data(CPUState *env,
425                                      target_phys_addr_t *physical, int *prot,
426                                      target_ulong address, int rw, int is_user)
427 {
428     unsigned int i;
429     uint64_t context;
430
431     if ((env->lsu & DMMU_E) == 0) { /* DMMU disabled */
432         *physical = ultrasparc_truncate_physical(address);
433         *prot = PAGE_READ | PAGE_WRITE;
434         return 0;
435     }
436
437     context = env->dmmu.mmu_primary_context & 0x1fff;
438
439     for (i = 0; i < 64; i++) {
440         // ctx match, vaddr match, valid?
441         if (ultrasparc_tag_match(&env->dtlb[i],
442                                  address, context, physical)
443         ) {
444             // access ok?
445             if (((env->dtlb[i].tte & 0x4) && is_user) ||
446                 (!(env->dtlb[i].tte & 0x2) && (rw == 1))) {
447                 uint8_t fault_type = 0;
448
449                 if ((env->dtlb[i].tte & 0x4) && is_user) {
450                     fault_type |= 1; /* privilege violation */
451                 }
452
453                 if (env->dmmu.sfsr & 1) /* Fault status register */
454                     env->dmmu.sfsr = 2; /* overflow (not read before
455                                              another fault) */
456
457                 env->dmmu.sfsr |= (is_user << 3) | ((rw == 1) << 2) | 1;
458
459                 env->dmmu.sfsr |= (fault_type << 7);
460
461                 env->dmmu.sfar = address; /* Fault address register */
462                 env->exception_index = TT_DFAULT;
463 #ifdef DEBUG_MMU
464                 printf("DFAULT at 0x%" PRIx64 "\n", address);
465 #endif
466                 return 1;
467             }
468             *prot = PAGE_READ;
469             if (env->dtlb[i].tte & 0x2)
470                 *prot |= PAGE_WRITE;
471             TTE_SET_USED(env->dtlb[i].tte);
472             return 0;
473         }
474     }
475 #ifdef DEBUG_MMU
476     printf("DMISS at 0x%" PRIx64 "\n", address);
477 #endif
478     env->dmmu.tag_access = (address & ~0x1fffULL) | context;
479     env->exception_index = TT_DMISS;
480     return 1;
481 }
482
483 static int get_physical_address_code(CPUState *env,
484                                      target_phys_addr_t *physical, int *prot,
485                                      target_ulong address, int is_user)
486 {
487     unsigned int i;
488     uint64_t context;
489
490     if ((env->lsu & IMMU_E) == 0 || (env->pstate & PS_RED) != 0) {
491         /* IMMU disabled */
492         *physical = ultrasparc_truncate_physical(address);
493         *prot = PAGE_EXEC;
494         return 0;
495     }
496
497     context = env->dmmu.mmu_primary_context & 0x1fff;
498
499     for (i = 0; i < 64; i++) {
500         // ctx match, vaddr match, valid?
501         if (ultrasparc_tag_match(&env->itlb[i],
502                                  address, context, physical)
503         ) {
504             // access ok?
505             if ((env->itlb[i].tte & 0x4) && is_user) {
506                 if (env->immu.sfsr) /* Fault status register */
507                     env->immu.sfsr = 2; /* overflow (not read before
508                                              another fault) */
509                 env->immu.sfsr |= (is_user << 3) | 1;
510                 env->exception_index = TT_TFAULT;
511 #ifdef DEBUG_MMU
512                 printf("TFAULT at 0x%" PRIx64 "\n", address);
513 #endif
514                 return 1;
515             }
516             *prot = PAGE_EXEC;
517             TTE_SET_USED(env->itlb[i].tte);
518             return 0;
519         }
520     }
521 #ifdef DEBUG_MMU
522     printf("TMISS at 0x%" PRIx64 "\n", address);
523 #endif
524     /* Context is stored in DMMU (dmmuregs[1]) also for IMMU */
525     env->immu.tag_access = (address & ~0x1fffULL) | context;
526     env->exception_index = TT_TMISS;
527     return 1;
528 }
529
530 static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
531                                 int *prot, int *access_index,
532                                 target_ulong address, int rw, int mmu_idx)
533 {
534     int is_user = mmu_idx == MMU_USER_IDX;
535
536     if (rw == 2)
537         return get_physical_address_code(env, physical, prot, address,
538                                          is_user);
539     else
540         return get_physical_address_data(env, physical, prot, address, rw,
541                                          is_user);
542 }
543
544 /* Perform address translation */
545 int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
546                               int mmu_idx, int is_softmmu)
547 {
548     target_ulong virt_addr, vaddr;
549     target_phys_addr_t paddr;
550     int error_code = 0, prot, ret = 0, access_index;
551
552     error_code = get_physical_address(env, &paddr, &prot, &access_index,
553                                       address, rw, mmu_idx);
554     if (error_code == 0) {
555         virt_addr = address & TARGET_PAGE_MASK;
556         vaddr = virt_addr + ((address & TARGET_PAGE_MASK) &
557                              (TARGET_PAGE_SIZE - 1));
558 #ifdef DEBUG_MMU
559         printf("Translate at 0x%" PRIx64 " -> 0x%" PRIx64 ", vaddr 0x%" PRIx64
560                "\n", address, paddr, vaddr);
561 #endif
562         ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
563         return ret;
564     }
565     // XXX
566     return 1;
567 }
568
569 #ifdef DEBUG_MMU
570 void dump_mmu(CPUState *env)
571 {
572     unsigned int i;
573     const char *mask;
574
575     printf("MMU contexts: Primary: %" PRId64 ", Secondary: %" PRId64 "\n",
576            env->dmmu.mmu_primary_context, env->dmmu.mmu_secondary_context);
577     if ((env->lsu & DMMU_E) == 0) {
578         printf("DMMU disabled\n");
579     } else {
580         printf("DMMU dump:\n");
581         for (i = 0; i < 64; i++) {
582             switch ((env->dtlb_tte[i] >> 61) & 3) {
583             default:
584             case 0x0:
585                 mask = "  8k";
586                 break;
587             case 0x1:
588                 mask = " 64k";
589                 break;
590             case 0x2:
591                 mask = "512k";
592                 break;
593             case 0x3:
594                 mask = "  4M";
595                 break;
596             }
597             if ((env->dtlb_tte[i] & 0x8000000000000000ULL) != 0) {
598                 printf("[%02u] VA: " PRIx64 ", PA: " PRIx64
599                        ", %s, %s, %s, %s, ctx %" PRId64 "\n",
600                        i,
601                        env->dtlb_tag[i] & (uint64_t)~0x1fffULL,
602                        env->dtlb_tte[i] & (uint64_t)0x1ffffffe000ULL,
603                        mask,
604                        env->dtlb_tte[i] & 0x4? "priv": "user",
605                        env->dtlb_tte[i] & 0x2? "RW": "RO",
606                        env->dtlb_tte[i] & 0x40? "locked": "unlocked",
607                        env->dtlb_tag[i] & (uint64_t)0x1fffULL);
608             }
609         }
610     }
611     if ((env->lsu & IMMU_E) == 0) {
612         printf("IMMU disabled\n");
613     } else {
614         printf("IMMU dump:\n");
615         for (i = 0; i < 64; i++) {
616             switch ((env->itlb_tte[i] >> 61) & 3) {
617             default:
618             case 0x0:
619                 mask = "  8k";
620                 break;
621             case 0x1:
622                 mask = " 64k";
623                 break;
624             case 0x2:
625                 mask = "512k";
626                 break;
627             case 0x3:
628                 mask = "  4M";
629                 break;
630             }
631             if ((env->itlb_tte[i] & 0x8000000000000000ULL) != 0) {
632                 printf("[%02u] VA: " PRIx64 ", PA: " PRIx64
633                        ", %s, %s, %s, ctx %" PRId64 "\n",
634                        i,
635                        env->itlb[i].tag & (uint64_t)~0x1fffULL,
636                        env->itlb_tte[i] & (uint64_t)0x1ffffffe000ULL,
637                        mask,
638                        env->itlb_tte[i] & 0x4? "priv": "user",
639                        env->itlb_tte[i] & 0x40? "locked": "unlocked",
640                        env->itlb[i].tag & (uint64_t)0x1fffULL);
641             }
642         }
643     }
644 }
645 #endif /* DEBUG_MMU */
646
647 #endif /* TARGET_SPARC64 */
648 #endif /* !CONFIG_USER_ONLY */
649
650
651 #if defined(CONFIG_USER_ONLY)
652 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
653 {
654     return addr;
655 }
656
657 #else
658 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
659 {
660     target_phys_addr_t phys_addr;
661     int prot, access_index;
662
663     if (get_physical_address(env, &phys_addr, &prot, &access_index, addr, 2,
664                              MMU_KERNEL_IDX) != 0)
665         if (get_physical_address(env, &phys_addr, &prot, &access_index, addr,
666                                  0, MMU_KERNEL_IDX) != 0)
667             return -1;
668     if (cpu_get_physical_page_desc(phys_addr) == IO_MEM_UNASSIGNED)
669         return -1;
670     return phys_addr;
671 }
672 #endif
673
674 void cpu_reset(CPUSPARCState *env)
675 {
676     if (qemu_loglevel_mask(CPU_LOG_RESET)) {
677         qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
678         log_cpu_state(env, 0);
679     }
680
681     tlb_flush(env, 1);
682     env->cwp = 0;
683 #ifndef TARGET_SPARC64
684     env->wim = 1;
685 #endif
686     env->regwptr = env->regbase + (env->cwp * 16);
687     CC_OP = CC_OP_FLAGS;
688 #if defined(CONFIG_USER_ONLY)
689 #ifdef TARGET_SPARC64
690     env->cleanwin = env->nwindows - 2;
691     env->cansave = env->nwindows - 2;
692     env->pstate = PS_RMO | PS_PEF | PS_IE;
693     env->asi = 0x82; // Primary no-fault
694 #endif
695 #else
696 #if !defined(TARGET_SPARC64)
697     env->psret = 0;
698 #endif
699     env->psrs = 1;
700     env->psrps = 1;
701 #ifdef TARGET_SPARC64
702     env->pstate = PS_PRIV|PS_RED|PS_PEF|PS_AG;
703     env->hpstate = HS_PRIV;
704     env->tl = env->maxtl;
705     cpu_tsptr(env)->tt = TT_POWER_ON_RESET;
706     env->lsu = 0;
707 #else
708     env->mmuregs[0] &= ~(MMU_E | MMU_NF);
709     env->mmuregs[0] |= env->def->mmu_bm;
710 #endif
711     env->pc = 0;
712     env->npc = env->pc + 4;
713 #endif
714 }
715
716 static int cpu_sparc_register(CPUSPARCState *env, const char *cpu_model)
717 {
718     sparc_def_t def1, *def = &def1;
719
720     if (cpu_sparc_find_by_name(def, cpu_model) < 0)
721         return -1;
722
723     env->def = qemu_mallocz(sizeof(*def));
724     memcpy(env->def, def, sizeof(*def));
725 #if defined(CONFIG_USER_ONLY)
726     if ((env->def->features & CPU_FEATURE_FLOAT))
727         env->def->features |= CPU_FEATURE_FLOAT128;
728 #endif
729     env->cpu_model_str = cpu_model;
730     env->version = def->iu_version;
731     env->fsr = def->fpu_version;
732     env->nwindows = def->nwindows;
733 #if !defined(TARGET_SPARC64)
734     env->mmuregs[0] |= def->mmu_version;
735     cpu_sparc_set_id(env, 0);
736     env->mxccregs[7] |= def->mxcc_version;
737 #else
738     env->mmu_version = def->mmu_version;
739     env->maxtl = def->maxtl;
740     env->version |= def->maxtl << 8;
741     env->version |= def->nwindows - 1;
742 #endif
743     return 0;
744 }
745
746 static void cpu_sparc_close(CPUSPARCState *env)
747 {
748     free(env->def);
749     free(env);
750 }
751
752 CPUSPARCState *cpu_sparc_init(const char *cpu_model)
753 {
754     CPUSPARCState *env;
755
756     env = qemu_mallocz(sizeof(CPUSPARCState));
757     cpu_exec_init(env);
758
759     gen_intermediate_code_init(env);
760
761     if (cpu_sparc_register(env, cpu_model) < 0) {
762         cpu_sparc_close(env);
763         return NULL;
764     }
765     qemu_init_vcpu(env);
766
767     return env;
768 }
769
770 void cpu_sparc_set_id(CPUSPARCState *env, unsigned int cpu)
771 {
772 #if !defined(TARGET_SPARC64)
773     env->mxccregs[7] = ((cpu + 8) & 0xf) << 24;
774 #endif
775 }
776
777 static const sparc_def_t sparc_defs[] = {
778 #ifdef TARGET_SPARC64
779     {
780         .name = "Fujitsu Sparc64",
781         .iu_version = ((0x04ULL << 48) | (0x02ULL << 32) | (0ULL << 24)),
782         .fpu_version = 0x00000000,
783         .mmu_version = mmu_us_12,
784         .nwindows = 4,
785         .maxtl = 4,
786         .features = CPU_DEFAULT_FEATURES,
787     },
788     {
789         .name = "Fujitsu Sparc64 III",
790         .iu_version = ((0x04ULL << 48) | (0x03ULL << 32) | (0ULL << 24)),
791         .fpu_version = 0x00000000,
792         .mmu_version = mmu_us_12,
793         .nwindows = 5,
794         .maxtl = 4,
795         .features = CPU_DEFAULT_FEATURES,
796     },
797     {
798         .name = "Fujitsu Sparc64 IV",
799         .iu_version = ((0x04ULL << 48) | (0x04ULL << 32) | (0ULL << 24)),
800         .fpu_version = 0x00000000,
801         .mmu_version = mmu_us_12,
802         .nwindows = 8,
803         .maxtl = 5,
804         .features = CPU_DEFAULT_FEATURES,
805     },
806     {
807         .name = "Fujitsu Sparc64 V",
808         .iu_version = ((0x04ULL << 48) | (0x05ULL << 32) | (0x51ULL << 24)),
809         .fpu_version = 0x00000000,
810         .mmu_version = mmu_us_12,
811         .nwindows = 8,
812         .maxtl = 5,
813         .features = CPU_DEFAULT_FEATURES,
814     },
815     {
816         .name = "TI UltraSparc I",
817         .iu_version = ((0x17ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
818         .fpu_version = 0x00000000,
819         .mmu_version = mmu_us_12,
820         .nwindows = 8,
821         .maxtl = 5,
822         .features = CPU_DEFAULT_FEATURES,
823     },
824     {
825         .name = "TI UltraSparc II",
826         .iu_version = ((0x17ULL << 48) | (0x11ULL << 32) | (0x20ULL << 24)),
827         .fpu_version = 0x00000000,
828         .mmu_version = mmu_us_12,
829         .nwindows = 8,
830         .maxtl = 5,
831         .features = CPU_DEFAULT_FEATURES,
832     },
833     {
834         .name = "TI UltraSparc IIi",
835         .iu_version = ((0x17ULL << 48) | (0x12ULL << 32) | (0x91ULL << 24)),
836         .fpu_version = 0x00000000,
837         .mmu_version = mmu_us_12,
838         .nwindows = 8,
839         .maxtl = 5,
840         .features = CPU_DEFAULT_FEATURES,
841     },
842     {
843         .name = "TI UltraSparc IIe",
844         .iu_version = ((0x17ULL << 48) | (0x13ULL << 32) | (0x14ULL << 24)),
845         .fpu_version = 0x00000000,
846         .mmu_version = mmu_us_12,
847         .nwindows = 8,
848         .maxtl = 5,
849         .features = CPU_DEFAULT_FEATURES,
850     },
851     {
852         .name = "Sun UltraSparc III",
853         .iu_version = ((0x3eULL << 48) | (0x14ULL << 32) | (0x34ULL << 24)),
854         .fpu_version = 0x00000000,
855         .mmu_version = mmu_us_12,
856         .nwindows = 8,
857         .maxtl = 5,
858         .features = CPU_DEFAULT_FEATURES,
859     },
860     {
861         .name = "Sun UltraSparc III Cu",
862         .iu_version = ((0x3eULL << 48) | (0x15ULL << 32) | (0x41ULL << 24)),
863         .fpu_version = 0x00000000,
864         .mmu_version = mmu_us_3,
865         .nwindows = 8,
866         .maxtl = 5,
867         .features = CPU_DEFAULT_FEATURES,
868     },
869     {
870         .name = "Sun UltraSparc IIIi",
871         .iu_version = ((0x3eULL << 48) | (0x16ULL << 32) | (0x34ULL << 24)),
872         .fpu_version = 0x00000000,
873         .mmu_version = mmu_us_12,
874         .nwindows = 8,
875         .maxtl = 5,
876         .features = CPU_DEFAULT_FEATURES,
877     },
878     {
879         .name = "Sun UltraSparc IV",
880         .iu_version = ((0x3eULL << 48) | (0x18ULL << 32) | (0x31ULL << 24)),
881         .fpu_version = 0x00000000,
882         .mmu_version = mmu_us_4,
883         .nwindows = 8,
884         .maxtl = 5,
885         .features = CPU_DEFAULT_FEATURES,
886     },
887     {
888         .name = "Sun UltraSparc IV+",
889         .iu_version = ((0x3eULL << 48) | (0x19ULL << 32) | (0x22ULL << 24)),
890         .fpu_version = 0x00000000,
891         .mmu_version = mmu_us_12,
892         .nwindows = 8,
893         .maxtl = 5,
894         .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_CMT,
895     },
896     {
897         .name = "Sun UltraSparc IIIi+",
898         .iu_version = ((0x3eULL << 48) | (0x22ULL << 32) | (0ULL << 24)),
899         .fpu_version = 0x00000000,
900         .mmu_version = mmu_us_3,
901         .nwindows = 8,
902         .maxtl = 5,
903         .features = CPU_DEFAULT_FEATURES,
904     },
905     {
906         .name = "Sun UltraSparc T1",
907         // defined in sparc_ifu_fdp.v and ctu.h
908         .iu_version = ((0x3eULL << 48) | (0x23ULL << 32) | (0x02ULL << 24)),
909         .fpu_version = 0x00000000,
910         .mmu_version = mmu_sun4v,
911         .nwindows = 8,
912         .maxtl = 6,
913         .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_HYPV | CPU_FEATURE_CMT
914         | CPU_FEATURE_GL,
915     },
916     {
917         .name = "Sun UltraSparc T2",
918         // defined in tlu_asi_ctl.v and n2_revid_cust.v
919         .iu_version = ((0x3eULL << 48) | (0x24ULL << 32) | (0x02ULL << 24)),
920         .fpu_version = 0x00000000,
921         .mmu_version = mmu_sun4v,
922         .nwindows = 8,
923         .maxtl = 6,
924         .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_HYPV | CPU_FEATURE_CMT
925         | CPU_FEATURE_GL,
926     },
927     {
928         .name = "NEC UltraSparc I",
929         .iu_version = ((0x22ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
930         .fpu_version = 0x00000000,
931         .mmu_version = mmu_us_12,
932         .nwindows = 8,
933         .maxtl = 5,
934         .features = CPU_DEFAULT_FEATURES,
935     },
936 #else
937     {
938         .name = "Fujitsu MB86900",
939         .iu_version = 0x00 << 24, /* Impl 0, ver 0 */
940         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
941         .mmu_version = 0x00 << 24, /* Impl 0, ver 0 */
942         .mmu_bm = 0x00004000,
943         .mmu_ctpr_mask = 0x007ffff0,
944         .mmu_cxr_mask = 0x0000003f,
945         .mmu_sfsr_mask = 0xffffffff,
946         .mmu_trcr_mask = 0xffffffff,
947         .nwindows = 7,
948         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_FSMULD,
949     },
950     {
951         .name = "Fujitsu MB86904",
952         .iu_version = 0x04 << 24, /* Impl 0, ver 4 */
953         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
954         .mmu_version = 0x04 << 24, /* Impl 0, ver 4 */
955         .mmu_bm = 0x00004000,
956         .mmu_ctpr_mask = 0x00ffffc0,
957         .mmu_cxr_mask = 0x000000ff,
958         .mmu_sfsr_mask = 0x00016fff,
959         .mmu_trcr_mask = 0x00ffffff,
960         .nwindows = 8,
961         .features = CPU_DEFAULT_FEATURES,
962     },
963     {
964         .name = "Fujitsu MB86907",
965         .iu_version = 0x05 << 24, /* Impl 0, ver 5 */
966         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
967         .mmu_version = 0x05 << 24, /* Impl 0, ver 5 */
968         .mmu_bm = 0x00004000,
969         .mmu_ctpr_mask = 0xffffffc0,
970         .mmu_cxr_mask = 0x000000ff,
971         .mmu_sfsr_mask = 0x00016fff,
972         .mmu_trcr_mask = 0xffffffff,
973         .nwindows = 8,
974         .features = CPU_DEFAULT_FEATURES,
975     },
976     {
977         .name = "LSI L64811",
978         .iu_version = 0x10 << 24, /* Impl 1, ver 0 */
979         .fpu_version = 1 << 17, /* FPU version 1 (LSI L64814) */
980         .mmu_version = 0x10 << 24,
981         .mmu_bm = 0x00004000,
982         .mmu_ctpr_mask = 0x007ffff0,
983         .mmu_cxr_mask = 0x0000003f,
984         .mmu_sfsr_mask = 0xffffffff,
985         .mmu_trcr_mask = 0xffffffff,
986         .nwindows = 8,
987         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
988         CPU_FEATURE_FSMULD,
989     },
990     {
991         .name = "Cypress CY7C601",
992         .iu_version = 0x11 << 24, /* Impl 1, ver 1 */
993         .fpu_version = 3 << 17, /* FPU version 3 (Cypress CY7C602) */
994         .mmu_version = 0x10 << 24,
995         .mmu_bm = 0x00004000,
996         .mmu_ctpr_mask = 0x007ffff0,
997         .mmu_cxr_mask = 0x0000003f,
998         .mmu_sfsr_mask = 0xffffffff,
999         .mmu_trcr_mask = 0xffffffff,
1000         .nwindows = 8,
1001         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
1002         CPU_FEATURE_FSMULD,
1003     },
1004     {
1005         .name = "Cypress CY7C611",
1006         .iu_version = 0x13 << 24, /* Impl 1, ver 3 */
1007         .fpu_version = 3 << 17, /* FPU version 3 (Cypress CY7C602) */
1008         .mmu_version = 0x10 << 24,
1009         .mmu_bm = 0x00004000,
1010         .mmu_ctpr_mask = 0x007ffff0,
1011         .mmu_cxr_mask = 0x0000003f,
1012         .mmu_sfsr_mask = 0xffffffff,
1013         .mmu_trcr_mask = 0xffffffff,
1014         .nwindows = 8,
1015         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
1016         CPU_FEATURE_FSMULD,
1017     },
1018     {
1019         .name = "TI MicroSparc I",
1020         .iu_version = 0x41000000,
1021         .fpu_version = 4 << 17,
1022         .mmu_version = 0x41000000,
1023         .mmu_bm = 0x00004000,
1024         .mmu_ctpr_mask = 0x007ffff0,
1025         .mmu_cxr_mask = 0x0000003f,
1026         .mmu_sfsr_mask = 0x00016fff,
1027         .mmu_trcr_mask = 0x0000003f,
1028         .nwindows = 7,
1029         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_MUL |
1030         CPU_FEATURE_DIV | CPU_FEATURE_FLUSH | CPU_FEATURE_FSQRT |
1031         CPU_FEATURE_FMUL,
1032     },
1033     {
1034         .name = "TI MicroSparc II",
1035         .iu_version = 0x42000000,
1036         .fpu_version = 4 << 17,
1037         .mmu_version = 0x02000000,
1038         .mmu_bm = 0x00004000,
1039         .mmu_ctpr_mask = 0x00ffffc0,
1040         .mmu_cxr_mask = 0x000000ff,
1041         .mmu_sfsr_mask = 0x00016fff,
1042         .mmu_trcr_mask = 0x00ffffff,
1043         .nwindows = 8,
1044         .features = CPU_DEFAULT_FEATURES,
1045     },
1046     {
1047         .name = "TI MicroSparc IIep",
1048         .iu_version = 0x42000000,
1049         .fpu_version = 4 << 17,
1050         .mmu_version = 0x04000000,
1051         .mmu_bm = 0x00004000,
1052         .mmu_ctpr_mask = 0x00ffffc0,
1053         .mmu_cxr_mask = 0x000000ff,
1054         .mmu_sfsr_mask = 0x00016bff,
1055         .mmu_trcr_mask = 0x00ffffff,
1056         .nwindows = 8,
1057         .features = CPU_DEFAULT_FEATURES,
1058     },
1059     {
1060         .name = "TI SuperSparc 40", // STP1020NPGA
1061         .iu_version = 0x41000000, // SuperSPARC 2.x
1062         .fpu_version = 0 << 17,
1063         .mmu_version = 0x00000800, // SuperSPARC 2.x, no MXCC
1064         .mmu_bm = 0x00002000,
1065         .mmu_ctpr_mask = 0xffffffc0,
1066         .mmu_cxr_mask = 0x0000ffff,
1067         .mmu_sfsr_mask = 0xffffffff,
1068         .mmu_trcr_mask = 0xffffffff,
1069         .nwindows = 8,
1070         .features = CPU_DEFAULT_FEATURES,
1071     },
1072     {
1073         .name = "TI SuperSparc 50", // STP1020PGA
1074         .iu_version = 0x40000000, // SuperSPARC 3.x
1075         .fpu_version = 0 << 17,
1076         .mmu_version = 0x01000800, // SuperSPARC 3.x, no MXCC
1077         .mmu_bm = 0x00002000,
1078         .mmu_ctpr_mask = 0xffffffc0,
1079         .mmu_cxr_mask = 0x0000ffff,
1080         .mmu_sfsr_mask = 0xffffffff,
1081         .mmu_trcr_mask = 0xffffffff,
1082         .nwindows = 8,
1083         .features = CPU_DEFAULT_FEATURES,
1084     },
1085     {
1086         .name = "TI SuperSparc 51",
1087         .iu_version = 0x40000000, // SuperSPARC 3.x
1088         .fpu_version = 0 << 17,
1089         .mmu_version = 0x01000000, // SuperSPARC 3.x, MXCC
1090         .mmu_bm = 0x00002000,
1091         .mmu_ctpr_mask = 0xffffffc0,
1092         .mmu_cxr_mask = 0x0000ffff,
1093         .mmu_sfsr_mask = 0xffffffff,
1094         .mmu_trcr_mask = 0xffffffff,
1095         .mxcc_version = 0x00000104,
1096         .nwindows = 8,
1097         .features = CPU_DEFAULT_FEATURES,
1098     },
1099     {
1100         .name = "TI SuperSparc 60", // STP1020APGA
1101         .iu_version = 0x40000000, // SuperSPARC 3.x
1102         .fpu_version = 0 << 17,
1103         .mmu_version = 0x01000800, // SuperSPARC 3.x, no MXCC
1104         .mmu_bm = 0x00002000,
1105         .mmu_ctpr_mask = 0xffffffc0,
1106         .mmu_cxr_mask = 0x0000ffff,
1107         .mmu_sfsr_mask = 0xffffffff,
1108         .mmu_trcr_mask = 0xffffffff,
1109         .nwindows = 8,
1110         .features = CPU_DEFAULT_FEATURES,
1111     },
1112     {
1113         .name = "TI SuperSparc 61",
1114         .iu_version = 0x44000000, // SuperSPARC 3.x
1115         .fpu_version = 0 << 17,
1116         .mmu_version = 0x01000000, // SuperSPARC 3.x, MXCC
1117         .mmu_bm = 0x00002000,
1118         .mmu_ctpr_mask = 0xffffffc0,
1119         .mmu_cxr_mask = 0x0000ffff,
1120         .mmu_sfsr_mask = 0xffffffff,
1121         .mmu_trcr_mask = 0xffffffff,
1122         .mxcc_version = 0x00000104,
1123         .nwindows = 8,
1124         .features = CPU_DEFAULT_FEATURES,
1125     },
1126     {
1127         .name = "TI SuperSparc II",
1128         .iu_version = 0x40000000, // SuperSPARC II 1.x
1129         .fpu_version = 0 << 17,
1130         .mmu_version = 0x08000000, // SuperSPARC II 1.x, MXCC
1131         .mmu_bm = 0x00002000,
1132         .mmu_ctpr_mask = 0xffffffc0,
1133         .mmu_cxr_mask = 0x0000ffff,
1134         .mmu_sfsr_mask = 0xffffffff,
1135         .mmu_trcr_mask = 0xffffffff,
1136         .mxcc_version = 0x00000104,
1137         .nwindows = 8,
1138         .features = CPU_DEFAULT_FEATURES,
1139     },
1140     {
1141         .name = "Ross RT625",
1142         .iu_version = 0x1e000000,
1143         .fpu_version = 1 << 17,
1144         .mmu_version = 0x1e000000,
1145         .mmu_bm = 0x00004000,
1146         .mmu_ctpr_mask = 0x007ffff0,
1147         .mmu_cxr_mask = 0x0000003f,
1148         .mmu_sfsr_mask = 0xffffffff,
1149         .mmu_trcr_mask = 0xffffffff,
1150         .nwindows = 8,
1151         .features = CPU_DEFAULT_FEATURES,
1152     },
1153     {
1154         .name = "Ross RT620",
1155         .iu_version = 0x1f000000,
1156         .fpu_version = 1 << 17,
1157         .mmu_version = 0x1f000000,
1158         .mmu_bm = 0x00004000,
1159         .mmu_ctpr_mask = 0x007ffff0,
1160         .mmu_cxr_mask = 0x0000003f,
1161         .mmu_sfsr_mask = 0xffffffff,
1162         .mmu_trcr_mask = 0xffffffff,
1163         .nwindows = 8,
1164         .features = CPU_DEFAULT_FEATURES,
1165     },
1166     {
1167         .name = "BIT B5010",
1168         .iu_version = 0x20000000,
1169         .fpu_version = 0 << 17, /* B5010/B5110/B5120/B5210 */
1170         .mmu_version = 0x20000000,
1171         .mmu_bm = 0x00004000,
1172         .mmu_ctpr_mask = 0x007ffff0,
1173         .mmu_cxr_mask = 0x0000003f,
1174         .mmu_sfsr_mask = 0xffffffff,
1175         .mmu_trcr_mask = 0xffffffff,
1176         .nwindows = 8,
1177         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
1178         CPU_FEATURE_FSMULD,
1179     },
1180     {
1181         .name = "Matsushita MN10501",
1182         .iu_version = 0x50000000,
1183         .fpu_version = 0 << 17,
1184         .mmu_version = 0x50000000,
1185         .mmu_bm = 0x00004000,
1186         .mmu_ctpr_mask = 0x007ffff0,
1187         .mmu_cxr_mask = 0x0000003f,
1188         .mmu_sfsr_mask = 0xffffffff,
1189         .mmu_trcr_mask = 0xffffffff,
1190         .nwindows = 8,
1191         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_MUL | CPU_FEATURE_FSQRT |
1192         CPU_FEATURE_FSMULD,
1193     },
1194     {
1195         .name = "Weitek W8601",
1196         .iu_version = 0x90 << 24, /* Impl 9, ver 0 */
1197         .fpu_version = 3 << 17, /* FPU version 3 (Weitek WTL3170/2) */
1198         .mmu_version = 0x10 << 24,
1199         .mmu_bm = 0x00004000,
1200         .mmu_ctpr_mask = 0x007ffff0,
1201         .mmu_cxr_mask = 0x0000003f,
1202         .mmu_sfsr_mask = 0xffffffff,
1203         .mmu_trcr_mask = 0xffffffff,
1204         .nwindows = 8,
1205         .features = CPU_DEFAULT_FEATURES,
1206     },
1207     {
1208         .name = "LEON2",
1209         .iu_version = 0xf2000000,
1210         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1211         .mmu_version = 0xf2000000,
1212         .mmu_bm = 0x00004000,
1213         .mmu_ctpr_mask = 0x007ffff0,
1214         .mmu_cxr_mask = 0x0000003f,
1215         .mmu_sfsr_mask = 0xffffffff,
1216         .mmu_trcr_mask = 0xffffffff,
1217         .nwindows = 8,
1218         .features = CPU_DEFAULT_FEATURES,
1219     },
1220     {
1221         .name = "LEON3",
1222         .iu_version = 0xf3000000,
1223         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1224         .mmu_version = 0xf3000000,
1225         .mmu_bm = 0x00004000,
1226         .mmu_ctpr_mask = 0x007ffff0,
1227         .mmu_cxr_mask = 0x0000003f,
1228         .mmu_sfsr_mask = 0xffffffff,
1229         .mmu_trcr_mask = 0xffffffff,
1230         .nwindows = 8,
1231         .features = CPU_DEFAULT_FEATURES,
1232     },
1233 #endif
1234 };
1235
1236 static const char * const feature_name[] = {
1237     "float",
1238     "float128",
1239     "swap",
1240     "mul",
1241     "div",
1242     "flush",
1243     "fsqrt",
1244     "fmul",
1245     "vis1",
1246     "vis2",
1247     "fsmuld",
1248     "hypv",
1249     "cmt",
1250     "gl",
1251 };
1252
1253 static void print_features(FILE *f,
1254                            int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
1255                            uint32_t features, const char *prefix)
1256 {
1257     unsigned int i;
1258
1259     for (i = 0; i < ARRAY_SIZE(feature_name); i++)
1260         if (feature_name[i] && (features & (1 << i))) {
1261             if (prefix)
1262                 (*cpu_fprintf)(f, "%s", prefix);
1263             (*cpu_fprintf)(f, "%s ", feature_name[i]);
1264         }
1265 }
1266
1267 static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features)
1268 {
1269     unsigned int i;
1270
1271     for (i = 0; i < ARRAY_SIZE(feature_name); i++)
1272         if (feature_name[i] && !strcmp(flagname, feature_name[i])) {
1273             *features |= 1 << i;
1274             return;
1275         }
1276     fprintf(stderr, "CPU feature %s not found\n", flagname);
1277 }
1278
1279 static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model)
1280 {
1281     unsigned int i;
1282     const sparc_def_t *def = NULL;
1283     char *s = strdup(cpu_model);
1284     char *featurestr, *name = strtok(s, ",");
1285     uint32_t plus_features = 0;
1286     uint32_t minus_features = 0;
1287     long long iu_version;
1288     uint32_t fpu_version, mmu_version, nwindows;
1289
1290     for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
1291         if (strcasecmp(name, sparc_defs[i].name) == 0) {
1292             def = &sparc_defs[i];
1293         }
1294     }
1295     if (!def)
1296         goto error;
1297     memcpy(cpu_def, def, sizeof(*def));
1298
1299     featurestr = strtok(NULL, ",");
1300     while (featurestr) {
1301         char *val;
1302
1303         if (featurestr[0] == '+') {
1304             add_flagname_to_bitmaps(featurestr + 1, &plus_features);
1305         } else if (featurestr[0] == '-') {
1306             add_flagname_to_bitmaps(featurestr + 1, &minus_features);
1307         } else if ((val = strchr(featurestr, '='))) {
1308             *val = 0; val++;
1309             if (!strcmp(featurestr, "iu_version")) {
1310                 char *err;
1311
1312                 iu_version = strtoll(val, &err, 0);
1313                 if (!*val || *err) {
1314                     fprintf(stderr, "bad numerical value %s\n", val);
1315                     goto error;
1316                 }
1317                 cpu_def->iu_version = iu_version;
1318 #ifdef DEBUG_FEATURES
1319                 fprintf(stderr, "iu_version %llx\n", iu_version);
1320 #endif
1321             } else if (!strcmp(featurestr, "fpu_version")) {
1322                 char *err;
1323
1324                 fpu_version = strtol(val, &err, 0);
1325                 if (!*val || *err) {
1326                     fprintf(stderr, "bad numerical value %s\n", val);
1327                     goto error;
1328                 }
1329                 cpu_def->fpu_version = fpu_version;
1330 #ifdef DEBUG_FEATURES
1331                 fprintf(stderr, "fpu_version %x\n", fpu_version);
1332 #endif
1333             } else if (!strcmp(featurestr, "mmu_version")) {
1334                 char *err;
1335
1336                 mmu_version = strtol(val, &err, 0);
1337                 if (!*val || *err) {
1338                     fprintf(stderr, "bad numerical value %s\n", val);
1339                     goto error;
1340                 }
1341                 cpu_def->mmu_version = mmu_version;
1342 #ifdef DEBUG_FEATURES
1343                 fprintf(stderr, "mmu_version %x\n", mmu_version);
1344 #endif
1345             } else if (!strcmp(featurestr, "nwindows")) {
1346                 char *err;
1347
1348                 nwindows = strtol(val, &err, 0);
1349                 if (!*val || *err || nwindows > MAX_NWINDOWS ||
1350                     nwindows < MIN_NWINDOWS) {
1351                     fprintf(stderr, "bad numerical value %s\n", val);
1352                     goto error;
1353                 }
1354                 cpu_def->nwindows = nwindows;
1355 #ifdef DEBUG_FEATURES
1356                 fprintf(stderr, "nwindows %d\n", nwindows);
1357 #endif
1358             } else {
1359                 fprintf(stderr, "unrecognized feature %s\n", featurestr);
1360                 goto error;
1361             }
1362         } else {
1363             fprintf(stderr, "feature string `%s' not in format "
1364                     "(+feature|-feature|feature=xyz)\n", featurestr);
1365             goto error;
1366         }
1367         featurestr = strtok(NULL, ",");
1368     }
1369     cpu_def->features |= plus_features;
1370     cpu_def->features &= ~minus_features;
1371 #ifdef DEBUG_FEATURES
1372     print_features(stderr, fprintf, cpu_def->features, NULL);
1373 #endif
1374     free(s);
1375     return 0;
1376
1377  error:
1378     free(s);
1379     return -1;
1380 }
1381
1382 void sparc_cpu_list(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
1383 {
1384     unsigned int i;
1385
1386     for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
1387         (*cpu_fprintf)(f, "Sparc %16s IU " TARGET_FMT_lx " FPU %08x MMU %08x NWINS %d ",
1388                        sparc_defs[i].name,
1389                        sparc_defs[i].iu_version,
1390                        sparc_defs[i].fpu_version,
1391                        sparc_defs[i].mmu_version,
1392                        sparc_defs[i].nwindows);
1393         print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES &
1394                        ~sparc_defs[i].features, "-");
1395         print_features(f, cpu_fprintf, ~CPU_DEFAULT_FEATURES &
1396                        sparc_defs[i].features, "+");
1397         (*cpu_fprintf)(f, "\n");
1398     }
1399     (*cpu_fprintf)(f, "Default CPU feature flags (use '-' to remove): ");
1400     print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES, NULL);
1401     (*cpu_fprintf)(f, "\n");
1402     (*cpu_fprintf)(f, "Available CPU feature flags (use '+' to add): ");
1403     print_features(f, cpu_fprintf, ~CPU_DEFAULT_FEATURES, NULL);
1404     (*cpu_fprintf)(f, "\n");
1405     (*cpu_fprintf)(f, "Numerical features (use '=' to set): iu_version "
1406                    "fpu_version mmu_version nwindows\n");
1407 }
1408
1409 void cpu_dump_state(CPUState *env, FILE *f,
1410                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
1411                     int flags)
1412 {
1413     int i, x;
1414
1415     cpu_fprintf(f, "pc: " TARGET_FMT_lx "  npc: " TARGET_FMT_lx "\n", env->pc,
1416                 env->npc);
1417     cpu_fprintf(f, "General Registers:\n");
1418     for (i = 0; i < 4; i++)
1419         cpu_fprintf(f, "%%g%c: " TARGET_FMT_lx "\t", i + '0', env->gregs[i]);
1420     cpu_fprintf(f, "\n");
1421     for (; i < 8; i++)
1422         cpu_fprintf(f, "%%g%c: " TARGET_FMT_lx "\t", i + '0', env->gregs[i]);
1423     cpu_fprintf(f, "\nCurrent Register Window:\n");
1424     for (x = 0; x < 3; x++) {
1425         for (i = 0; i < 4; i++)
1426             cpu_fprintf(f, "%%%c%d: " TARGET_FMT_lx "\t",
1427                     (x == 0 ? 'o' : (x == 1 ? 'l' : 'i')), i,
1428                     env->regwptr[i + x * 8]);
1429         cpu_fprintf(f, "\n");
1430         for (; i < 8; i++)
1431             cpu_fprintf(f, "%%%c%d: " TARGET_FMT_lx "\t",
1432                     (x == 0 ? 'o' : x == 1 ? 'l' : 'i'), i,
1433                     env->regwptr[i + x * 8]);
1434         cpu_fprintf(f, "\n");
1435     }
1436     cpu_fprintf(f, "\nFloating Point Registers:\n");
1437     for (i = 0; i < 32; i++) {
1438         if ((i & 3) == 0)
1439             cpu_fprintf(f, "%%f%02d:", i);
1440         cpu_fprintf(f, " %016f", *(float *)&env->fpr[i]);
1441         if ((i & 3) == 3)
1442             cpu_fprintf(f, "\n");
1443     }
1444 #ifdef TARGET_SPARC64
1445     cpu_fprintf(f, "pstate: 0x%08x ccr: 0x%02x asi: 0x%02x tl: %d fprs: %d\n",
1446                 env->pstate, GET_CCR(env), env->asi, env->tl, env->fprs);
1447     cpu_fprintf(f, "cansave: %d canrestore: %d otherwin: %d wstate %d "
1448                 "cleanwin %d cwp %d\n",
1449                 env->cansave, env->canrestore, env->otherwin, env->wstate,
1450                 env->cleanwin, env->nwindows - 1 - env->cwp);
1451 #else
1452
1453 #define GET_FLAG(a,b) ((env->psr & a)?b:'-')
1454
1455     cpu_fprintf(f, "psr: 0x%08x -> %c%c%c%c %c%c%c wim: 0x%08x\n",
1456                 GET_PSR(env), GET_FLAG(PSR_ZERO, 'Z'), GET_FLAG(PSR_OVF, 'V'),
1457                 GET_FLAG(PSR_NEG, 'N'), GET_FLAG(PSR_CARRY, 'C'),
1458                 env->psrs?'S':'-', env->psrps?'P':'-',
1459                 env->psret?'E':'-', env->wim);
1460 #endif
1461     cpu_fprintf(f, "fsr: 0x%08x\n", env->fsr);
1462 }
This page took 0.10666 seconds and 4 git commands to generate.