xref: /qemu/target/sparc/helper.c (revision 9a78eead0c74333a394c0f7bbfc4423ac746fcd5)
1e8af50a3Sbellard /*
2e8af50a3Sbellard  *  sparc helpers
3e8af50a3Sbellard  *
483469015Sbellard  *  Copyright (c) 2003-2005 Fabrice Bellard
5e8af50a3Sbellard  *
6e8af50a3Sbellard  * This library is free software; you can redistribute it and/or
7e8af50a3Sbellard  * modify it under the terms of the GNU Lesser General Public
8e8af50a3Sbellard  * License as published by the Free Software Foundation; either
9e8af50a3Sbellard  * version 2 of the License, or (at your option) any later version.
10e8af50a3Sbellard  *
11e8af50a3Sbellard  * This library is distributed in the hope that it will be useful,
12e8af50a3Sbellard  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13e8af50a3Sbellard  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14e8af50a3Sbellard  * Lesser General Public License for more details.
15e8af50a3Sbellard  *
16e8af50a3Sbellard  * You should have received a copy of the GNU Lesser General Public
178167ee88SBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18e8af50a3Sbellard  */
19ee5bbe38Sbellard #include <stdarg.h>
20ee5bbe38Sbellard #include <stdlib.h>
21ee5bbe38Sbellard #include <stdio.h>
22ee5bbe38Sbellard #include <string.h>
23ee5bbe38Sbellard #include <inttypes.h>
24ee5bbe38Sbellard #include <signal.h>
25ee5bbe38Sbellard 
26ee5bbe38Sbellard #include "cpu.h"
27ee5bbe38Sbellard #include "exec-all.h"
28ca10f867Saurel32 #include "qemu-common.h"
29e8af50a3Sbellard 
30e80cfcfcSbellard //#define DEBUG_MMU
3164a88d5dSblueswir1 //#define DEBUG_FEATURES
32e8af50a3Sbellard 
33b8e9fc06SIgor V. Kovalenko #ifdef DEBUG_MMU
34b8e9fc06SIgor V. Kovalenko #define DPRINTF_MMU(fmt, ...) \
35b8e9fc06SIgor V. Kovalenko     do { printf("MMU: " fmt , ## __VA_ARGS__); } while (0)
36b8e9fc06SIgor V. Kovalenko #else
37b8e9fc06SIgor V. Kovalenko #define DPRINTF_MMU(fmt, ...) do {} while (0)
38b8e9fc06SIgor V. Kovalenko #endif
39b8e9fc06SIgor V. Kovalenko 
4022548760Sblueswir1 static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model);
41c48fcb47Sblueswir1 
42e8af50a3Sbellard /* Sparc MMU emulation */
43e8af50a3Sbellard 
44e8af50a3Sbellard /* thread support */
45e8af50a3Sbellard 
46c227f099SAnthony Liguori static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
47e8af50a3Sbellard 
48e8af50a3Sbellard void cpu_lock(void)
49e8af50a3Sbellard {
50e8af50a3Sbellard     spin_lock(&global_cpu_lock);
51e8af50a3Sbellard }
52e8af50a3Sbellard 
53e8af50a3Sbellard void cpu_unlock(void)
54e8af50a3Sbellard {
55e8af50a3Sbellard     spin_unlock(&global_cpu_lock);
56e8af50a3Sbellard }
57e8af50a3Sbellard 
589d893301Sbellard #if defined(CONFIG_USER_ONLY)
599d893301Sbellard 
6022548760Sblueswir1 int cpu_sparc_handle_mmu_fault(CPUState *env1, target_ulong address, int rw,
616ebbf390Sj_mayer                                int mmu_idx, int is_softmmu)
629d893301Sbellard {
63878d3096Sbellard     if (rw & 2)
6422548760Sblueswir1         env1->exception_index = TT_TFAULT;
65878d3096Sbellard     else
6622548760Sblueswir1         env1->exception_index = TT_DFAULT;
679d893301Sbellard     return 1;
689d893301Sbellard }
699d893301Sbellard 
709d893301Sbellard #else
71e8af50a3Sbellard 
723475187dSbellard #ifndef TARGET_SPARC64
7383469015Sbellard /*
7483469015Sbellard  * Sparc V8 Reference MMU (SRMMU)
7583469015Sbellard  */
76e8af50a3Sbellard static const int access_table[8][8] = {
77a764a566Sblueswir1     { 0, 0, 0, 0, 8, 0, 12, 12 },
78a764a566Sblueswir1     { 0, 0, 0, 0, 8, 0, 0, 0 },
79a764a566Sblueswir1     { 8, 8, 0, 0, 0, 8, 12, 12 },
80a764a566Sblueswir1     { 8, 8, 0, 0, 0, 8, 0, 0 },
81a764a566Sblueswir1     { 8, 0, 8, 0, 8, 8, 12, 12 },
82a764a566Sblueswir1     { 8, 0, 8, 0, 8, 0, 8, 0 },
83a764a566Sblueswir1     { 8, 8, 8, 0, 8, 8, 12, 12 },
84a764a566Sblueswir1     { 8, 8, 8, 0, 8, 8, 8, 0 }
85e8af50a3Sbellard };
86e8af50a3Sbellard 
87227671c9Sbellard static const int perm_table[2][8] = {
88227671c9Sbellard     {
89227671c9Sbellard         PAGE_READ,
90227671c9Sbellard         PAGE_READ | PAGE_WRITE,
91227671c9Sbellard         PAGE_READ | PAGE_EXEC,
92227671c9Sbellard         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
93227671c9Sbellard         PAGE_EXEC,
94227671c9Sbellard         PAGE_READ | PAGE_WRITE,
95227671c9Sbellard         PAGE_READ | PAGE_EXEC,
96227671c9Sbellard         PAGE_READ | PAGE_WRITE | PAGE_EXEC
97227671c9Sbellard     },
98227671c9Sbellard     {
99227671c9Sbellard         PAGE_READ,
100227671c9Sbellard         PAGE_READ | PAGE_WRITE,
101227671c9Sbellard         PAGE_READ | PAGE_EXEC,
102227671c9Sbellard         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
103227671c9Sbellard         PAGE_EXEC,
104227671c9Sbellard         PAGE_READ,
105227671c9Sbellard         0,
106227671c9Sbellard         0,
107227671c9Sbellard     }
108e8af50a3Sbellard };
109e8af50a3Sbellard 
110c227f099SAnthony Liguori static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
111c48fcb47Sblueswir1                                 int *prot, int *access_index,
112d4c430a8SPaul Brook                                 target_ulong address, int rw, int mmu_idx,
113d4c430a8SPaul Brook                                 target_ulong *page_size)
114e8af50a3Sbellard {
115e80cfcfcSbellard     int access_perms = 0;
116c227f099SAnthony Liguori     target_phys_addr_t pde_ptr;
117af7bf89bSbellard     uint32_t pde;
1186ebbf390Sj_mayer     int error_code = 0, is_dirty, is_user;
119e80cfcfcSbellard     unsigned long page_offset;
120e8af50a3Sbellard 
1216ebbf390Sj_mayer     is_user = mmu_idx == MMU_USER_IDX;
12240ce0a9aSblueswir1 
123e8af50a3Sbellard     if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
124d4c430a8SPaul Brook         *page_size = TARGET_PAGE_SIZE;
12540ce0a9aSblueswir1         // Boot mode: instruction fetches are taken from PROM
1265578ceabSblueswir1         if (rw == 2 && (env->mmuregs[0] & env->def->mmu_bm)) {
12758a770f3Sblueswir1             *physical = env->prom_addr | (address & 0x7ffffULL);
12840ce0a9aSblueswir1             *prot = PAGE_READ | PAGE_EXEC;
12940ce0a9aSblueswir1             return 0;
13040ce0a9aSblueswir1         }
131e80cfcfcSbellard         *physical = address;
132227671c9Sbellard         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
133e80cfcfcSbellard         return 0;
134e8af50a3Sbellard     }
135e8af50a3Sbellard 
1367483750dSbellard     *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user? 0 : 1);
1375dcb6b91Sblueswir1     *physical = 0xffffffffffff0000ULL;
1387483750dSbellard 
139e8af50a3Sbellard     /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
140e8af50a3Sbellard     /* Context base + context number */
1413deaeab7Sblueswir1     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
14249be8030Sbellard     pde = ldl_phys(pde_ptr);
143e8af50a3Sbellard 
144e8af50a3Sbellard     /* Ctx pde */
145e8af50a3Sbellard     switch (pde & PTE_ENTRYTYPE_MASK) {
146e80cfcfcSbellard     default:
147e8af50a3Sbellard     case 0: /* Invalid */
1487483750dSbellard         return 1 << 2;
149e80cfcfcSbellard     case 2: /* L0 PTE, maybe should not happen? */
150e8af50a3Sbellard     case 3: /* Reserved */
1517483750dSbellard         return 4 << 2;
152e80cfcfcSbellard     case 1: /* L0 PDE */
153e80cfcfcSbellard         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
15449be8030Sbellard         pde = ldl_phys(pde_ptr);
155e80cfcfcSbellard 
156e80cfcfcSbellard         switch (pde & PTE_ENTRYTYPE_MASK) {
157e80cfcfcSbellard         default:
158e80cfcfcSbellard         case 0: /* Invalid */
1597483750dSbellard             return (1 << 8) | (1 << 2);
160e80cfcfcSbellard         case 3: /* Reserved */
1617483750dSbellard             return (1 << 8) | (4 << 2);
162e8af50a3Sbellard         case 1: /* L1 PDE */
163e80cfcfcSbellard             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
16449be8030Sbellard             pde = ldl_phys(pde_ptr);
165e8af50a3Sbellard 
166e8af50a3Sbellard             switch (pde & PTE_ENTRYTYPE_MASK) {
167e80cfcfcSbellard             default:
168e8af50a3Sbellard             case 0: /* Invalid */
1697483750dSbellard                 return (2 << 8) | (1 << 2);
170e8af50a3Sbellard             case 3: /* Reserved */
1717483750dSbellard                 return (2 << 8) | (4 << 2);
172e8af50a3Sbellard             case 1: /* L2 PDE */
173e80cfcfcSbellard                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
17449be8030Sbellard                 pde = ldl_phys(pde_ptr);
175e8af50a3Sbellard 
176e8af50a3Sbellard                 switch (pde & PTE_ENTRYTYPE_MASK) {
177e80cfcfcSbellard                 default:
178e8af50a3Sbellard                 case 0: /* Invalid */
1797483750dSbellard                     return (3 << 8) | (1 << 2);
180e8af50a3Sbellard                 case 1: /* PDE, should not happen */
181e8af50a3Sbellard                 case 3: /* Reserved */
1827483750dSbellard                     return (3 << 8) | (4 << 2);
183e8af50a3Sbellard                 case 2: /* L3 PTE */
18477f193daSblueswir1                     page_offset = (address & TARGET_PAGE_MASK) &
18577f193daSblueswir1                         (TARGET_PAGE_SIZE - 1);
186e8af50a3Sbellard                 }
187d4c430a8SPaul Brook                 *page_size = TARGET_PAGE_SIZE;
188e8af50a3Sbellard                 break;
189e8af50a3Sbellard             case 2: /* L2 PTE */
190e8af50a3Sbellard                 page_offset = address & 0x3ffff;
191d4c430a8SPaul Brook                 *page_size = 0x40000;
192e8af50a3Sbellard             }
193e8af50a3Sbellard             break;
194e8af50a3Sbellard         case 2: /* L1 PTE */
195e8af50a3Sbellard             page_offset = address & 0xffffff;
196d4c430a8SPaul Brook             *page_size = 0x1000000;
197e8af50a3Sbellard         }
198e8af50a3Sbellard     }
199e8af50a3Sbellard 
200698235aaSArtyom Tarasenko     /* check access */
201698235aaSArtyom Tarasenko     access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
202698235aaSArtyom Tarasenko     error_code = access_table[*access_index][access_perms];
203698235aaSArtyom Tarasenko     if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user))
204698235aaSArtyom Tarasenko         return error_code;
205698235aaSArtyom Tarasenko 
206e8af50a3Sbellard     /* update page modified and dirty bits */
207b769d8feSbellard     is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
208e8af50a3Sbellard     if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
209e8af50a3Sbellard         pde |= PG_ACCESSED_MASK;
210e8af50a3Sbellard         if (is_dirty)
211e8af50a3Sbellard             pde |= PG_MODIFIED_MASK;
21249be8030Sbellard         stl_phys_notdirty(pde_ptr, pde);
213e8af50a3Sbellard     }
214e8af50a3Sbellard 
215e8af50a3Sbellard     /* the page can be put in the TLB */
216227671c9Sbellard     *prot = perm_table[is_user][access_perms];
217227671c9Sbellard     if (!(pde & PG_MODIFIED_MASK)) {
218e8af50a3Sbellard         /* only set write access if already dirty... otherwise wait
219e8af50a3Sbellard            for dirty access */
220227671c9Sbellard         *prot &= ~PAGE_WRITE;
221e8af50a3Sbellard     }
222e8af50a3Sbellard 
223e8af50a3Sbellard     /* Even if large ptes, we map only one 4KB page in the cache to
224e8af50a3Sbellard        avoid filling it too fast */
225c227f099SAnthony Liguori     *physical = ((target_phys_addr_t)(pde & PTE_ADDR_MASK) << 4) + page_offset;
2266f7e9aecSbellard     return error_code;
227e80cfcfcSbellard }
228e80cfcfcSbellard 
229e80cfcfcSbellard /* Perform address translation */
230af7bf89bSbellard int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
2316ebbf390Sj_mayer                               int mmu_idx, int is_softmmu)
232e80cfcfcSbellard {
233c227f099SAnthony Liguori     target_phys_addr_t paddr;
2345dcb6b91Sblueswir1     target_ulong vaddr;
235d4c430a8SPaul Brook     target_ulong page_size;
236d4c430a8SPaul Brook     int error_code = 0, prot, access_index;
237e80cfcfcSbellard 
23877f193daSblueswir1     error_code = get_physical_address(env, &paddr, &prot, &access_index,
239d4c430a8SPaul Brook                                       address, rw, mmu_idx, &page_size);
240e80cfcfcSbellard     if (error_code == 0) {
2419e61bde5Sbellard         vaddr = address & TARGET_PAGE_MASK;
2429e61bde5Sbellard         paddr &= TARGET_PAGE_MASK;
2439e61bde5Sbellard #ifdef DEBUG_MMU
2445dcb6b91Sblueswir1         printf("Translate at " TARGET_FMT_lx " -> " TARGET_FMT_plx ", vaddr "
2455dcb6b91Sblueswir1                TARGET_FMT_lx "\n", address, paddr, vaddr);
2469e61bde5Sbellard #endif
247d4c430a8SPaul Brook         tlb_set_page(env, vaddr, paddr, prot, mmu_idx, page_size);
248d4c430a8SPaul Brook         return 0;
249e80cfcfcSbellard     }
250e8af50a3Sbellard 
251e8af50a3Sbellard     if (env->mmuregs[3]) /* Fault status register */
252e8af50a3Sbellard         env->mmuregs[3] = 1; /* overflow (not read before another fault) */
2537483750dSbellard     env->mmuregs[3] |= (access_index << 5) | error_code | 2;
254e8af50a3Sbellard     env->mmuregs[4] = address; /* Fault address register */
255e8af50a3Sbellard 
256878d3096Sbellard     if ((env->mmuregs[0] & MMU_NF) || env->psret == 0)  {
2576f7e9aecSbellard         // No fault mode: if a mapping is available, just override
2586f7e9aecSbellard         // permissions. If no mapping is available, redirect accesses to
2596f7e9aecSbellard         // neverland. Fake/overridden mappings will be flushed when
2606f7e9aecSbellard         // switching to normal mode.
2617483750dSbellard         vaddr = address & TARGET_PAGE_MASK;
262227671c9Sbellard         prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
263d4c430a8SPaul Brook         tlb_set_page(env, vaddr, paddr, prot, mmu_idx, TARGET_PAGE_SIZE);
264d4c430a8SPaul Brook         return 0;
2657483750dSbellard     } else {
266878d3096Sbellard         if (rw & 2)
267878d3096Sbellard             env->exception_index = TT_TFAULT;
268878d3096Sbellard         else
269878d3096Sbellard             env->exception_index = TT_DFAULT;
270878d3096Sbellard         return 1;
271e8af50a3Sbellard     }
2727483750dSbellard }
27324741ef3Sbellard 
27424741ef3Sbellard target_ulong mmu_probe(CPUState *env, target_ulong address, int mmulev)
27524741ef3Sbellard {
276c227f099SAnthony Liguori     target_phys_addr_t pde_ptr;
27724741ef3Sbellard     uint32_t pde;
27824741ef3Sbellard 
27924741ef3Sbellard     /* Context base + context number */
280c227f099SAnthony Liguori     pde_ptr = (target_phys_addr_t)(env->mmuregs[1] << 4) +
2815dcb6b91Sblueswir1         (env->mmuregs[2] << 2);
28224741ef3Sbellard     pde = ldl_phys(pde_ptr);
28324741ef3Sbellard 
28424741ef3Sbellard     switch (pde & PTE_ENTRYTYPE_MASK) {
28524741ef3Sbellard     default:
28624741ef3Sbellard     case 0: /* Invalid */
28724741ef3Sbellard     case 2: /* PTE, maybe should not happen? */
28824741ef3Sbellard     case 3: /* Reserved */
28924741ef3Sbellard         return 0;
29024741ef3Sbellard     case 1: /* L1 PDE */
29124741ef3Sbellard         if (mmulev == 3)
29224741ef3Sbellard             return pde;
29324741ef3Sbellard         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
29424741ef3Sbellard         pde = ldl_phys(pde_ptr);
29524741ef3Sbellard 
29624741ef3Sbellard         switch (pde & PTE_ENTRYTYPE_MASK) {
29724741ef3Sbellard         default:
29824741ef3Sbellard         case 0: /* Invalid */
29924741ef3Sbellard         case 3: /* Reserved */
30024741ef3Sbellard             return 0;
30124741ef3Sbellard         case 2: /* L1 PTE */
30224741ef3Sbellard             return pde;
30324741ef3Sbellard         case 1: /* L2 PDE */
30424741ef3Sbellard             if (mmulev == 2)
30524741ef3Sbellard                 return pde;
30624741ef3Sbellard             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
30724741ef3Sbellard             pde = ldl_phys(pde_ptr);
30824741ef3Sbellard 
30924741ef3Sbellard             switch (pde & PTE_ENTRYTYPE_MASK) {
31024741ef3Sbellard             default:
31124741ef3Sbellard             case 0: /* Invalid */
31224741ef3Sbellard             case 3: /* Reserved */
31324741ef3Sbellard                 return 0;
31424741ef3Sbellard             case 2: /* L2 PTE */
31524741ef3Sbellard                 return pde;
31624741ef3Sbellard             case 1: /* L3 PDE */
31724741ef3Sbellard                 if (mmulev == 1)
31824741ef3Sbellard                     return pde;
31924741ef3Sbellard                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
32024741ef3Sbellard                 pde = ldl_phys(pde_ptr);
32124741ef3Sbellard 
32224741ef3Sbellard                 switch (pde & PTE_ENTRYTYPE_MASK) {
32324741ef3Sbellard                 default:
32424741ef3Sbellard                 case 0: /* Invalid */
32524741ef3Sbellard                 case 1: /* PDE, should not happen */
32624741ef3Sbellard                 case 3: /* Reserved */
32724741ef3Sbellard                     return 0;
32824741ef3Sbellard                 case 2: /* L3 PTE */
32924741ef3Sbellard                     return pde;
33024741ef3Sbellard                 }
33124741ef3Sbellard             }
33224741ef3Sbellard         }
33324741ef3Sbellard     }
33424741ef3Sbellard     return 0;
33524741ef3Sbellard }
33624741ef3Sbellard 
33724741ef3Sbellard #ifdef DEBUG_MMU
33824741ef3Sbellard void dump_mmu(CPUState *env)
33924741ef3Sbellard {
34024741ef3Sbellard     target_ulong va, va1, va2;
34124741ef3Sbellard     unsigned int n, m, o;
342c227f099SAnthony Liguori     target_phys_addr_t pde_ptr, pa;
34324741ef3Sbellard     uint32_t pde;
34424741ef3Sbellard 
34524741ef3Sbellard     printf("MMU dump:\n");
34624741ef3Sbellard     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
34724741ef3Sbellard     pde = ldl_phys(pde_ptr);
3485dcb6b91Sblueswir1     printf("Root ptr: " TARGET_FMT_plx ", ctx: %d\n",
349c227f099SAnthony Liguori            (target_phys_addr_t)env->mmuregs[1] << 4, env->mmuregs[2]);
35024741ef3Sbellard     for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
3515dcb6b91Sblueswir1         pde = mmu_probe(env, va, 2);
3525dcb6b91Sblueswir1         if (pde) {
35324741ef3Sbellard             pa = cpu_get_phys_page_debug(env, va);
3545dcb6b91Sblueswir1             printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
3555dcb6b91Sblueswir1                    " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
35624741ef3Sbellard             for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
3575dcb6b91Sblueswir1                 pde = mmu_probe(env, va1, 1);
3585dcb6b91Sblueswir1                 if (pde) {
35924741ef3Sbellard                     pa = cpu_get_phys_page_debug(env, va1);
3605dcb6b91Sblueswir1                     printf(" VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
3615dcb6b91Sblueswir1                            " PDE: " TARGET_FMT_lx "\n", va1, pa, pde);
36224741ef3Sbellard                     for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
3635dcb6b91Sblueswir1                         pde = mmu_probe(env, va2, 0);
3645dcb6b91Sblueswir1                         if (pde) {
36524741ef3Sbellard                             pa = cpu_get_phys_page_debug(env, va2);
3665dcb6b91Sblueswir1                             printf("  VA: " TARGET_FMT_lx ", PA: "
3675dcb6b91Sblueswir1                                    TARGET_FMT_plx " PTE: " TARGET_FMT_lx "\n",
3685dcb6b91Sblueswir1                                    va2, pa, pde);
36924741ef3Sbellard                         }
37024741ef3Sbellard                     }
37124741ef3Sbellard                 }
37224741ef3Sbellard             }
37324741ef3Sbellard         }
37424741ef3Sbellard     }
37524741ef3Sbellard     printf("MMU dump ends\n");
37624741ef3Sbellard }
37724741ef3Sbellard #endif /* DEBUG_MMU */
37824741ef3Sbellard 
37924741ef3Sbellard #else /* !TARGET_SPARC64 */
380e8807b14SIgor Kovalenko 
381e8807b14SIgor Kovalenko // 41 bit physical address space
382c227f099SAnthony Liguori static inline target_phys_addr_t ultrasparc_truncate_physical(uint64_t x)
383e8807b14SIgor Kovalenko {
384e8807b14SIgor Kovalenko     return x & 0x1ffffffffffULL;
385e8807b14SIgor Kovalenko }
386e8807b14SIgor Kovalenko 
38783469015Sbellard /*
38883469015Sbellard  * UltraSparc IIi I/DMMUs
38983469015Sbellard  */
3903475187dSbellard 
391536ba015SIgor Kovalenko // Returns true if TTE tag is valid and matches virtual address value in context
392536ba015SIgor Kovalenko // requires virtual address mask value calculated from TTE entry size
3936e8e7d4cSIgor Kovalenko static inline int ultrasparc_tag_match(SparcTLBEntry *tlb,
394536ba015SIgor Kovalenko                                        uint64_t address, uint64_t context,
395299b520cSIgor V. Kovalenko                                        target_phys_addr_t *physical)
396536ba015SIgor Kovalenko {
397536ba015SIgor Kovalenko     uint64_t mask;
398536ba015SIgor Kovalenko 
3996e8e7d4cSIgor Kovalenko     switch ((tlb->tte >> 61) & 3) {
4003475187dSbellard     default:
40183469015Sbellard     case 0x0: // 8k
4023475187dSbellard         mask = 0xffffffffffffe000ULL;
4033475187dSbellard         break;
40483469015Sbellard     case 0x1: // 64k
4053475187dSbellard         mask = 0xffffffffffff0000ULL;
4063475187dSbellard         break;
40783469015Sbellard     case 0x2: // 512k
4083475187dSbellard         mask = 0xfffffffffff80000ULL;
4093475187dSbellard         break;
41083469015Sbellard     case 0x3: // 4M
4113475187dSbellard         mask = 0xffffffffffc00000ULL;
4123475187dSbellard         break;
4133475187dSbellard     }
414536ba015SIgor Kovalenko 
415536ba015SIgor Kovalenko     // valid, context match, virtual address match?
416f707726eSIgor Kovalenko     if (TTE_IS_VALID(tlb->tte) &&
417299b520cSIgor V. Kovalenko         (TTE_IS_GLOBAL(tlb->tte) || tlb_compare_context(tlb, context))
4182a90358fSBlue Swirl         && compare_masked(address, tlb->tag, mask))
419536ba015SIgor Kovalenko     {
420536ba015SIgor Kovalenko         // decode physical address
4216e8e7d4cSIgor Kovalenko         *physical = ((tlb->tte & mask) | (address & ~mask)) & 0x1ffffffe000ULL;
422536ba015SIgor Kovalenko         return 1;
423536ba015SIgor Kovalenko     }
424536ba015SIgor Kovalenko 
425536ba015SIgor Kovalenko     return 0;
426536ba015SIgor Kovalenko }
427536ba015SIgor Kovalenko 
428536ba015SIgor Kovalenko static int get_physical_address_data(CPUState *env,
429c227f099SAnthony Liguori                                      target_phys_addr_t *physical, int *prot,
4302065061eSIgor V. Kovalenko                                      target_ulong address, int rw, int mmu_idx)
431536ba015SIgor Kovalenko {
432536ba015SIgor Kovalenko     unsigned int i;
433536ba015SIgor Kovalenko     uint64_t context;
434536ba015SIgor Kovalenko 
4352065061eSIgor V. Kovalenko     int is_user = (mmu_idx == MMU_USER_IDX ||
4362065061eSIgor V. Kovalenko                    mmu_idx == MMU_USER_SECONDARY_IDX);
4372065061eSIgor V. Kovalenko 
438536ba015SIgor Kovalenko     if ((env->lsu & DMMU_E) == 0) { /* DMMU disabled */
439536ba015SIgor Kovalenko         *physical = ultrasparc_truncate_physical(address);
440536ba015SIgor Kovalenko         *prot = PAGE_READ | PAGE_WRITE;
441536ba015SIgor Kovalenko         return 0;
442536ba015SIgor Kovalenko     }
443536ba015SIgor Kovalenko 
4442065061eSIgor V. Kovalenko     switch(mmu_idx) {
4452065061eSIgor V. Kovalenko     case MMU_USER_IDX:
4462065061eSIgor V. Kovalenko     case MMU_KERNEL_IDX:
4476e8e7d4cSIgor Kovalenko         context = env->dmmu.mmu_primary_context & 0x1fff;
4482065061eSIgor V. Kovalenko         break;
4492065061eSIgor V. Kovalenko     case MMU_USER_SECONDARY_IDX:
4502065061eSIgor V. Kovalenko     case MMU_KERNEL_SECONDARY_IDX:
4512065061eSIgor V. Kovalenko         context = env->dmmu.mmu_secondary_context & 0x1fff;
4522065061eSIgor V. Kovalenko         break;
4532065061eSIgor V. Kovalenko     case MMU_NUCLEUS_IDX:
45444505216SBlue Swirl     default:
455299b520cSIgor V. Kovalenko         context = 0;
4562065061eSIgor V. Kovalenko         break;
457299b520cSIgor V. Kovalenko     }
458536ba015SIgor Kovalenko 
459536ba015SIgor Kovalenko     for (i = 0; i < 64; i++) {
460afdf8109Sblueswir1         // ctx match, vaddr match, valid?
461b8e9fc06SIgor V. Kovalenko         if (ultrasparc_tag_match(&env->dtlb[i], address, context, physical)) {
462b8e9fc06SIgor V. Kovalenko 
4636e8e7d4cSIgor Kovalenko             uint8_t fault_type = 0;
4646e8e7d4cSIgor Kovalenko 
465b8e9fc06SIgor V. Kovalenko             // access ok?
4666e8e7d4cSIgor Kovalenko             if ((env->dtlb[i].tte & 0x4) && is_user) {
4676e8e7d4cSIgor Kovalenko                 fault_type |= 1; /* privilege violation */
468b8e9fc06SIgor V. Kovalenko                 env->exception_index = TT_DFAULT;
469b8e9fc06SIgor V. Kovalenko 
470b8e9fc06SIgor V. Kovalenko                 DPRINTF_MMU("DFAULT at %" PRIx64 " context %" PRIx64
471b8e9fc06SIgor V. Kovalenko                             " mmu_idx=%d tl=%d\n",
472b8e9fc06SIgor V. Kovalenko                             address, context, mmu_idx, env->tl);
473b8e9fc06SIgor V. Kovalenko             } else if (!(env->dtlb[i].tte & 0x2) && (rw == 1)) {
474b8e9fc06SIgor V. Kovalenko                 env->exception_index = TT_DPROT;
475b8e9fc06SIgor V. Kovalenko 
476b8e9fc06SIgor V. Kovalenko                 DPRINTF_MMU("DPROT at %" PRIx64 " context %" PRIx64
477b8e9fc06SIgor V. Kovalenko                             " mmu_idx=%d tl=%d\n",
478b8e9fc06SIgor V. Kovalenko                             address, context, mmu_idx, env->tl);
479b8e9fc06SIgor V. Kovalenko             } else {
480b8e9fc06SIgor V. Kovalenko                 *prot = PAGE_READ;
481b8e9fc06SIgor V. Kovalenko                 if (env->dtlb[i].tte & 0x2)
482b8e9fc06SIgor V. Kovalenko                     *prot |= PAGE_WRITE;
483b8e9fc06SIgor V. Kovalenko 
484b8e9fc06SIgor V. Kovalenko                 TTE_SET_USED(env->dtlb[i].tte);
485b8e9fc06SIgor V. Kovalenko 
486b8e9fc06SIgor V. Kovalenko                 return 0;
4876e8e7d4cSIgor Kovalenko             }
4886e8e7d4cSIgor Kovalenko 
4896e8e7d4cSIgor Kovalenko             if (env->dmmu.sfsr & 1) /* Fault status register */
4906e8e7d4cSIgor Kovalenko                 env->dmmu.sfsr = 2; /* overflow (not read before
49177f193daSblueswir1                                              another fault) */
4926e8e7d4cSIgor Kovalenko 
4936e8e7d4cSIgor Kovalenko             env->dmmu.sfsr |= (is_user << 3) | ((rw == 1) << 2) | 1;
4946e8e7d4cSIgor Kovalenko 
4956e8e7d4cSIgor Kovalenko             env->dmmu.sfsr |= (fault_type << 7);
4966e8e7d4cSIgor Kovalenko 
4976e8e7d4cSIgor Kovalenko             env->dmmu.sfar = address; /* Fault address register */
4989168b3a5SIgor V. Kovalenko 
4999168b3a5SIgor V. Kovalenko             env->dmmu.tag_access = (address & ~0x1fffULL) | context;
5009168b3a5SIgor V. Kovalenko 
5013475187dSbellard             return 1;
5023475187dSbellard         }
5033475187dSbellard     }
504b8e9fc06SIgor V. Kovalenko 
505b8e9fc06SIgor V. Kovalenko     DPRINTF_MMU("DMISS at %" PRIx64 " context %" PRIx64 "\n",
506b8e9fc06SIgor V. Kovalenko                 address, context);
507b8e9fc06SIgor V. Kovalenko 
5086e8e7d4cSIgor Kovalenko     env->dmmu.tag_access = (address & ~0x1fffULL) | context;
50983469015Sbellard     env->exception_index = TT_DMISS;
5103475187dSbellard     return 1;
5113475187dSbellard }
5123475187dSbellard 
51377f193daSblueswir1 static int get_physical_address_code(CPUState *env,
514c227f099SAnthony Liguori                                      target_phys_addr_t *physical, int *prot,
5152065061eSIgor V. Kovalenko                                      target_ulong address, int mmu_idx)
5163475187dSbellard {
5173475187dSbellard     unsigned int i;
518536ba015SIgor Kovalenko     uint64_t context;
5193475187dSbellard 
5202065061eSIgor V. Kovalenko     int is_user = (mmu_idx == MMU_USER_IDX ||
5212065061eSIgor V. Kovalenko                    mmu_idx == MMU_USER_SECONDARY_IDX);
5222065061eSIgor V. Kovalenko 
523e8807b14SIgor Kovalenko     if ((env->lsu & IMMU_E) == 0 || (env->pstate & PS_RED) != 0) {
524e8807b14SIgor Kovalenko         /* IMMU disabled */
525e8807b14SIgor Kovalenko         *physical = ultrasparc_truncate_physical(address);
526227671c9Sbellard         *prot = PAGE_EXEC;
5273475187dSbellard         return 0;
5283475187dSbellard     }
52983469015Sbellard 
530299b520cSIgor V. Kovalenko     if (env->tl == 0) {
5312065061eSIgor V. Kovalenko         /* PRIMARY context */
5326e8e7d4cSIgor Kovalenko         context = env->dmmu.mmu_primary_context & 0x1fff;
533299b520cSIgor V. Kovalenko     } else {
5342065061eSIgor V. Kovalenko         /* NUCLEUS context */
535299b520cSIgor V. Kovalenko         context = 0;
536299b520cSIgor V. Kovalenko     }
537536ba015SIgor Kovalenko 
5383475187dSbellard     for (i = 0; i < 64; i++) {
539afdf8109Sblueswir1         // ctx match, vaddr match, valid?
5406e8e7d4cSIgor Kovalenko         if (ultrasparc_tag_match(&env->itlb[i],
541299b520cSIgor V. Kovalenko                                  address, context, physical)) {
542afdf8109Sblueswir1             // access ok?
5436e8e7d4cSIgor Kovalenko             if ((env->itlb[i].tte & 0x4) && is_user) {
5446e8e7d4cSIgor Kovalenko                 if (env->immu.sfsr) /* Fault status register */
5456e8e7d4cSIgor Kovalenko                     env->immu.sfsr = 2; /* overflow (not read before
54677f193daSblueswir1                                              another fault) */
5476e8e7d4cSIgor Kovalenko                 env->immu.sfsr |= (is_user << 3) | 1;
5483475187dSbellard                 env->exception_index = TT_TFAULT;
549b8e9fc06SIgor V. Kovalenko 
5509168b3a5SIgor V. Kovalenko                 env->immu.tag_access = (address & ~0x1fffULL) | context;
5519168b3a5SIgor V. Kovalenko 
552b8e9fc06SIgor V. Kovalenko                 DPRINTF_MMU("TFAULT at %" PRIx64 " context %" PRIx64 "\n",
553b8e9fc06SIgor V. Kovalenko                             address, context);
554b8e9fc06SIgor V. Kovalenko 
5553475187dSbellard                 return 1;
5563475187dSbellard             }
557227671c9Sbellard             *prot = PAGE_EXEC;
558f707726eSIgor Kovalenko             TTE_SET_USED(env->itlb[i].tte);
5593475187dSbellard             return 0;
5603475187dSbellard         }
5613475187dSbellard     }
562b8e9fc06SIgor V. Kovalenko 
563b8e9fc06SIgor V. Kovalenko     DPRINTF_MMU("TMISS at %" PRIx64 " context %" PRIx64 "\n",
564b8e9fc06SIgor V. Kovalenko                 address, context);
565b8e9fc06SIgor V. Kovalenko 
5667ab463cbSBlue Swirl     /* Context is stored in DMMU (dmmuregs[1]) also for IMMU */
5676e8e7d4cSIgor Kovalenko     env->immu.tag_access = (address & ~0x1fffULL) | context;
56883469015Sbellard     env->exception_index = TT_TMISS;
5693475187dSbellard     return 1;
5703475187dSbellard }
5713475187dSbellard 
572c227f099SAnthony Liguori static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
573c48fcb47Sblueswir1                                 int *prot, int *access_index,
574d4c430a8SPaul Brook                                 target_ulong address, int rw, int mmu_idx,
575d4c430a8SPaul Brook                                 target_ulong *page_size)
5763475187dSbellard {
577d4c430a8SPaul Brook     /* ??? We treat everything as a small page, then explicitly flush
578d4c430a8SPaul Brook        everything when an entry is evicted.  */
579d4c430a8SPaul Brook     *page_size = TARGET_PAGE_SIZE;
5809fd1ae3aSIgor V. Kovalenko 
5819fd1ae3aSIgor V. Kovalenko #if defined (DEBUG_MMU)
5829fd1ae3aSIgor V. Kovalenko     /* safety net to catch wrong softmmu index use from dynamic code */
5839fd1ae3aSIgor V. Kovalenko     if (env->tl > 0 && mmu_idx != MMU_NUCLEUS_IDX) {
5849fd1ae3aSIgor V. Kovalenko         DPRINTF_MMU("get_physical_address %s tl=%d mmu_idx=%d"
5859fd1ae3aSIgor V. Kovalenko                     " primary context=%" PRIx64
5869fd1ae3aSIgor V. Kovalenko                     " secondary context=%" PRIx64
5879fd1ae3aSIgor V. Kovalenko                 " address=%" PRIx64
5889fd1ae3aSIgor V. Kovalenko                 "\n",
5899fd1ae3aSIgor V. Kovalenko                 (rw == 2 ? "CODE" : "DATA"),
5909fd1ae3aSIgor V. Kovalenko                 env->tl, mmu_idx,
5919fd1ae3aSIgor V. Kovalenko                 env->dmmu.mmu_primary_context,
5929fd1ae3aSIgor V. Kovalenko                 env->dmmu.mmu_secondary_context,
5939fd1ae3aSIgor V. Kovalenko                 address);
5949fd1ae3aSIgor V. Kovalenko     }
5959fd1ae3aSIgor V. Kovalenko #endif
5969fd1ae3aSIgor V. Kovalenko 
5973475187dSbellard     if (rw == 2)
59822548760Sblueswir1         return get_physical_address_code(env, physical, prot, address,
5992065061eSIgor V. Kovalenko                                          mmu_idx);
6003475187dSbellard     else
60122548760Sblueswir1         return get_physical_address_data(env, physical, prot, address, rw,
6022065061eSIgor V. Kovalenko                                          mmu_idx);
6033475187dSbellard }
6043475187dSbellard 
6053475187dSbellard /* Perform address translation */
6063475187dSbellard int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
6076ebbf390Sj_mayer                               int mmu_idx, int is_softmmu)
6083475187dSbellard {
60983469015Sbellard     target_ulong virt_addr, vaddr;
610c227f099SAnthony Liguori     target_phys_addr_t paddr;
611d4c430a8SPaul Brook     target_ulong page_size;
612d4c430a8SPaul Brook     int error_code = 0, prot, access_index;
6133475187dSbellard 
61477f193daSblueswir1     error_code = get_physical_address(env, &paddr, &prot, &access_index,
615d4c430a8SPaul Brook                                       address, rw, mmu_idx, &page_size);
6163475187dSbellard     if (error_code == 0) {
6173475187dSbellard         virt_addr = address & TARGET_PAGE_MASK;
61877f193daSblueswir1         vaddr = virt_addr + ((address & TARGET_PAGE_MASK) &
61977f193daSblueswir1                              (TARGET_PAGE_SIZE - 1));
620b8e9fc06SIgor V. Kovalenko 
621b8e9fc06SIgor V. Kovalenko         DPRINTF_MMU("Translate at %" PRIx64 " -> %" PRIx64 ","
622b8e9fc06SIgor V. Kovalenko                     " vaddr %" PRIx64
623b8e9fc06SIgor V. Kovalenko                     " mmu_idx=%d"
624b8e9fc06SIgor V. Kovalenko                     " tl=%d"
625b8e9fc06SIgor V. Kovalenko                     " primary context=%" PRIx64
626b8e9fc06SIgor V. Kovalenko                     " secondary context=%" PRIx64
627b8e9fc06SIgor V. Kovalenko                     "\n",
628b8e9fc06SIgor V. Kovalenko                     address, paddr, vaddr, mmu_idx, env->tl,
629b8e9fc06SIgor V. Kovalenko                     env->dmmu.mmu_primary_context,
630b8e9fc06SIgor V. Kovalenko                     env->dmmu.mmu_secondary_context);
631b8e9fc06SIgor V. Kovalenko 
632d4c430a8SPaul Brook         tlb_set_page(env, vaddr, paddr, prot, mmu_idx, page_size);
633d4c430a8SPaul Brook         return 0;
6343475187dSbellard     }
6353475187dSbellard     // XXX
6363475187dSbellard     return 1;
6373475187dSbellard }
6383475187dSbellard 
63983469015Sbellard #ifdef DEBUG_MMU
64083469015Sbellard void dump_mmu(CPUState *env)
64183469015Sbellard {
64283469015Sbellard     unsigned int i;
64383469015Sbellard     const char *mask;
64483469015Sbellard 
64577f193daSblueswir1     printf("MMU contexts: Primary: %" PRId64 ", Secondary: %" PRId64 "\n",
6466e8e7d4cSIgor Kovalenko            env->dmmu.mmu_primary_context, env->dmmu.mmu_secondary_context);
64783469015Sbellard     if ((env->lsu & DMMU_E) == 0) {
64883469015Sbellard         printf("DMMU disabled\n");
64983469015Sbellard     } else {
65083469015Sbellard         printf("DMMU dump:\n");
65183469015Sbellard         for (i = 0; i < 64; i++) {
65231a68d57SBlue Swirl             switch ((env->dtlb[i].tte >> 61) & 3) {
65383469015Sbellard             default:
65483469015Sbellard             case 0x0:
65583469015Sbellard                 mask = "  8k";
65683469015Sbellard                 break;
65783469015Sbellard             case 0x1:
65883469015Sbellard                 mask = " 64k";
65983469015Sbellard                 break;
66083469015Sbellard             case 0x2:
66183469015Sbellard                 mask = "512k";
66283469015Sbellard                 break;
66383469015Sbellard             case 0x3:
66483469015Sbellard                 mask = "  4M";
66583469015Sbellard                 break;
66683469015Sbellard             }
66731a68d57SBlue Swirl             if ((env->dtlb[i].tte & 0x8000000000000000ULL) != 0) {
66831a68d57SBlue Swirl                 printf("[%02u] VA: %" PRIx64 ", PA: %" PRIx64
6692a90358fSBlue Swirl                        ", %s, %s, %s, %s, ctx %" PRId64 " %s\n",
6706e8e7d4cSIgor Kovalenko                        i,
67131a68d57SBlue Swirl                        env->dtlb[i].tag & (uint64_t)~0x1fffULL,
67231a68d57SBlue Swirl                        env->dtlb[i].tte & (uint64_t)0x1ffffffe000ULL,
67383469015Sbellard                        mask,
67431a68d57SBlue Swirl                        env->dtlb[i].tte & 0x4? "priv": "user",
67531a68d57SBlue Swirl                        env->dtlb[i].tte & 0x2? "RW": "RO",
67631a68d57SBlue Swirl                        env->dtlb[i].tte & 0x40? "locked": "unlocked",
6772a90358fSBlue Swirl                        env->dtlb[i].tag & (uint64_t)0x1fffULL,
678e2129586SIgor V. Kovalenko                        TTE_IS_GLOBAL(env->dtlb[i].tte)? "global" : "local");
67983469015Sbellard             }
68083469015Sbellard         }
68183469015Sbellard     }
68283469015Sbellard     if ((env->lsu & IMMU_E) == 0) {
68383469015Sbellard         printf("IMMU disabled\n");
68483469015Sbellard     } else {
68583469015Sbellard         printf("IMMU dump:\n");
68683469015Sbellard         for (i = 0; i < 64; i++) {
68731a68d57SBlue Swirl             switch ((env->itlb[i].tte >> 61) & 3) {
68883469015Sbellard             default:
68983469015Sbellard             case 0x0:
69083469015Sbellard                 mask = "  8k";
69183469015Sbellard                 break;
69283469015Sbellard             case 0x1:
69383469015Sbellard                 mask = " 64k";
69483469015Sbellard                 break;
69583469015Sbellard             case 0x2:
69683469015Sbellard                 mask = "512k";
69783469015Sbellard                 break;
69883469015Sbellard             case 0x3:
69983469015Sbellard                 mask = "  4M";
70083469015Sbellard                 break;
70183469015Sbellard             }
70231a68d57SBlue Swirl             if ((env->itlb[i].tte & 0x8000000000000000ULL) != 0) {
70331a68d57SBlue Swirl                 printf("[%02u] VA: %" PRIx64 ", PA: %" PRIx64
7042a90358fSBlue Swirl                        ", %s, %s, %s, ctx %" PRId64 " %s\n",
7056e8e7d4cSIgor Kovalenko                        i,
7066e8e7d4cSIgor Kovalenko                        env->itlb[i].tag & (uint64_t)~0x1fffULL,
70731a68d57SBlue Swirl                        env->itlb[i].tte & (uint64_t)0x1ffffffe000ULL,
70883469015Sbellard                        mask,
70931a68d57SBlue Swirl                        env->itlb[i].tte & 0x4? "priv": "user",
71031a68d57SBlue Swirl                        env->itlb[i].tte & 0x40? "locked": "unlocked",
7112a90358fSBlue Swirl                        env->itlb[i].tag & (uint64_t)0x1fffULL,
712e2129586SIgor V. Kovalenko                        TTE_IS_GLOBAL(env->itlb[i].tte)? "global" : "local");
71383469015Sbellard             }
71483469015Sbellard         }
71583469015Sbellard     }
71683469015Sbellard }
71724741ef3Sbellard #endif /* DEBUG_MMU */
71824741ef3Sbellard 
71924741ef3Sbellard #endif /* TARGET_SPARC64 */
72024741ef3Sbellard #endif /* !CONFIG_USER_ONLY */
72124741ef3Sbellard 
722c48fcb47Sblueswir1 
7234fcc562bSPaul Brook #if !defined(CONFIG_USER_ONLY)
7242065061eSIgor V. Kovalenko target_phys_addr_t cpu_get_phys_page_nofault(CPUState *env, target_ulong addr,
7252065061eSIgor V. Kovalenko                                            int mmu_idx)
726c48fcb47Sblueswir1 {
727c227f099SAnthony Liguori     target_phys_addr_t phys_addr;
728d4c430a8SPaul Brook     target_ulong page_size;
729c48fcb47Sblueswir1     int prot, access_index;
730c48fcb47Sblueswir1 
731c48fcb47Sblueswir1     if (get_physical_address(env, &phys_addr, &prot, &access_index, addr, 2,
7322065061eSIgor V. Kovalenko                              mmu_idx, &page_size) != 0)
733c48fcb47Sblueswir1         if (get_physical_address(env, &phys_addr, &prot, &access_index, addr,
7342065061eSIgor V. Kovalenko                                  0, mmu_idx, &page_size) != 0)
735c48fcb47Sblueswir1             return -1;
736c48fcb47Sblueswir1     if (cpu_get_physical_page_desc(phys_addr) == IO_MEM_UNASSIGNED)
737c48fcb47Sblueswir1         return -1;
738c48fcb47Sblueswir1     return phys_addr;
739c48fcb47Sblueswir1 }
7402065061eSIgor V. Kovalenko 
7412065061eSIgor V. Kovalenko target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
7422065061eSIgor V. Kovalenko {
7439fd1ae3aSIgor V. Kovalenko     return cpu_get_phys_page_nofault(env, addr, cpu_mmu_index(env));
7442065061eSIgor V. Kovalenko }
745c48fcb47Sblueswir1 #endif
746c48fcb47Sblueswir1 
747c48fcb47Sblueswir1 void cpu_reset(CPUSPARCState *env)
748c48fcb47Sblueswir1 {
749eca1bdf4Saliguori     if (qemu_loglevel_mask(CPU_LOG_RESET)) {
750eca1bdf4Saliguori         qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
751eca1bdf4Saliguori         log_cpu_state(env, 0);
752eca1bdf4Saliguori     }
753eca1bdf4Saliguori 
754c48fcb47Sblueswir1     tlb_flush(env, 1);
755c48fcb47Sblueswir1     env->cwp = 0;
7565210977aSIgor Kovalenko #ifndef TARGET_SPARC64
757c48fcb47Sblueswir1     env->wim = 1;
7585210977aSIgor Kovalenko #endif
759c48fcb47Sblueswir1     env->regwptr = env->regbase + (env->cwp * 16);
7606b743278SBlue Swirl     CC_OP = CC_OP_FLAGS;
761c48fcb47Sblueswir1 #if defined(CONFIG_USER_ONLY)
762c48fcb47Sblueswir1 #ifdef TARGET_SPARC64
7631a14026eSblueswir1     env->cleanwin = env->nwindows - 2;
7641a14026eSblueswir1     env->cansave = env->nwindows - 2;
765c48fcb47Sblueswir1     env->pstate = PS_RMO | PS_PEF | PS_IE;
766c48fcb47Sblueswir1     env->asi = 0x82; // Primary no-fault
767c48fcb47Sblueswir1 #endif
768c48fcb47Sblueswir1 #else
7695210977aSIgor Kovalenko #if !defined(TARGET_SPARC64)
770c48fcb47Sblueswir1     env->psret = 0;
771c48fcb47Sblueswir1     env->psrs = 1;
772c48fcb47Sblueswir1     env->psrps = 1;
7732aae2b8eSIgor V. Kovalenko #endif
774c48fcb47Sblueswir1 #ifdef TARGET_SPARC64
7758194f35aSIgor Kovalenko     env->pstate = PS_PRIV|PS_RED|PS_PEF|PS_AG;
7762aae2b8eSIgor V. Kovalenko     env->hpstate = cpu_has_hypervisor(env) ? HS_PRIV : 0;
7778194f35aSIgor Kovalenko     env->tl = env->maxtl;
7788194f35aSIgor Kovalenko     cpu_tsptr(env)->tt = TT_POWER_ON_RESET;
779415fc906Sblueswir1     env->lsu = 0;
780c48fcb47Sblueswir1 #else
781c48fcb47Sblueswir1     env->mmuregs[0] &= ~(MMU_E | MMU_NF);
7825578ceabSblueswir1     env->mmuregs[0] |= env->def->mmu_bm;
783c48fcb47Sblueswir1 #endif
784e87231d4Sblueswir1     env->pc = 0;
785c48fcb47Sblueswir1     env->npc = env->pc + 4;
786c48fcb47Sblueswir1 #endif
787c48fcb47Sblueswir1 }
788c48fcb47Sblueswir1 
78964a88d5dSblueswir1 static int cpu_sparc_register(CPUSPARCState *env, const char *cpu_model)
790c48fcb47Sblueswir1 {
79164a88d5dSblueswir1     sparc_def_t def1, *def = &def1;
792c48fcb47Sblueswir1 
79364a88d5dSblueswir1     if (cpu_sparc_find_by_name(def, cpu_model) < 0)
79464a88d5dSblueswir1         return -1;
795c48fcb47Sblueswir1 
7965578ceabSblueswir1     env->def = qemu_mallocz(sizeof(*def));
7975578ceabSblueswir1     memcpy(env->def, def, sizeof(*def));
7985578ceabSblueswir1 #if defined(CONFIG_USER_ONLY)
7995578ceabSblueswir1     if ((env->def->features & CPU_FEATURE_FLOAT))
8005578ceabSblueswir1         env->def->features |= CPU_FEATURE_FLOAT128;
8015578ceabSblueswir1 #endif
802c48fcb47Sblueswir1     env->cpu_model_str = cpu_model;
803c48fcb47Sblueswir1     env->version = def->iu_version;
804c48fcb47Sblueswir1     env->fsr = def->fpu_version;
8051a14026eSblueswir1     env->nwindows = def->nwindows;
806c48fcb47Sblueswir1 #if !defined(TARGET_SPARC64)
807c48fcb47Sblueswir1     env->mmuregs[0] |= def->mmu_version;
808c48fcb47Sblueswir1     cpu_sparc_set_id(env, 0);
809963262deSblueswir1     env->mxccregs[7] |= def->mxcc_version;
8101a14026eSblueswir1 #else
811fb79ceb9Sblueswir1     env->mmu_version = def->mmu_version;
812c19148bdSblueswir1     env->maxtl = def->maxtl;
813c19148bdSblueswir1     env->version |= def->maxtl << 8;
8141a14026eSblueswir1     env->version |= def->nwindows - 1;
815c48fcb47Sblueswir1 #endif
81664a88d5dSblueswir1     return 0;
81764a88d5dSblueswir1 }
81864a88d5dSblueswir1 
81964a88d5dSblueswir1 static void cpu_sparc_close(CPUSPARCState *env)
82064a88d5dSblueswir1 {
8215578ceabSblueswir1     free(env->def);
82264a88d5dSblueswir1     free(env);
82364a88d5dSblueswir1 }
82464a88d5dSblueswir1 
82564a88d5dSblueswir1 CPUSPARCState *cpu_sparc_init(const char *cpu_model)
82664a88d5dSblueswir1 {
82764a88d5dSblueswir1     CPUSPARCState *env;
82864a88d5dSblueswir1 
82964a88d5dSblueswir1     env = qemu_mallocz(sizeof(CPUSPARCState));
83064a88d5dSblueswir1     cpu_exec_init(env);
831c48fcb47Sblueswir1 
832c48fcb47Sblueswir1     gen_intermediate_code_init(env);
833c48fcb47Sblueswir1 
83464a88d5dSblueswir1     if (cpu_sparc_register(env, cpu_model) < 0) {
83564a88d5dSblueswir1         cpu_sparc_close(env);
83664a88d5dSblueswir1         return NULL;
83764a88d5dSblueswir1     }
8380bf46a40Saliguori     qemu_init_vcpu(env);
839c48fcb47Sblueswir1 
840c48fcb47Sblueswir1     return env;
841c48fcb47Sblueswir1 }
842c48fcb47Sblueswir1 
843c48fcb47Sblueswir1 void cpu_sparc_set_id(CPUSPARCState *env, unsigned int cpu)
844c48fcb47Sblueswir1 {
845c48fcb47Sblueswir1 #if !defined(TARGET_SPARC64)
846c48fcb47Sblueswir1     env->mxccregs[7] = ((cpu + 8) & 0xf) << 24;
847c48fcb47Sblueswir1 #endif
848c48fcb47Sblueswir1 }
849c48fcb47Sblueswir1 
850c48fcb47Sblueswir1 static const sparc_def_t sparc_defs[] = {
851c48fcb47Sblueswir1 #ifdef TARGET_SPARC64
852c48fcb47Sblueswir1     {
853c48fcb47Sblueswir1         .name = "Fujitsu Sparc64",
854c19148bdSblueswir1         .iu_version = ((0x04ULL << 48) | (0x02ULL << 32) | (0ULL << 24)),
855c48fcb47Sblueswir1         .fpu_version = 0x00000000,
856fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
8571a14026eSblueswir1         .nwindows = 4,
858c19148bdSblueswir1         .maxtl = 4,
85964a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
860c48fcb47Sblueswir1     },
861c48fcb47Sblueswir1     {
862c48fcb47Sblueswir1         .name = "Fujitsu Sparc64 III",
863c19148bdSblueswir1         .iu_version = ((0x04ULL << 48) | (0x03ULL << 32) | (0ULL << 24)),
864c48fcb47Sblueswir1         .fpu_version = 0x00000000,
865fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
8661a14026eSblueswir1         .nwindows = 5,
867c19148bdSblueswir1         .maxtl = 4,
86864a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
869c48fcb47Sblueswir1     },
870c48fcb47Sblueswir1     {
871c48fcb47Sblueswir1         .name = "Fujitsu Sparc64 IV",
872c19148bdSblueswir1         .iu_version = ((0x04ULL << 48) | (0x04ULL << 32) | (0ULL << 24)),
873c48fcb47Sblueswir1         .fpu_version = 0x00000000,
874fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
8751a14026eSblueswir1         .nwindows = 8,
876c19148bdSblueswir1         .maxtl = 5,
87764a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
878c48fcb47Sblueswir1     },
879c48fcb47Sblueswir1     {
880c48fcb47Sblueswir1         .name = "Fujitsu Sparc64 V",
881c19148bdSblueswir1         .iu_version = ((0x04ULL << 48) | (0x05ULL << 32) | (0x51ULL << 24)),
882c48fcb47Sblueswir1         .fpu_version = 0x00000000,
883fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
8841a14026eSblueswir1         .nwindows = 8,
885c19148bdSblueswir1         .maxtl = 5,
88664a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
887c48fcb47Sblueswir1     },
888c48fcb47Sblueswir1     {
889c48fcb47Sblueswir1         .name = "TI UltraSparc I",
890c19148bdSblueswir1         .iu_version = ((0x17ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
891c48fcb47Sblueswir1         .fpu_version = 0x00000000,
892fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
8931a14026eSblueswir1         .nwindows = 8,
894c19148bdSblueswir1         .maxtl = 5,
89564a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
896c48fcb47Sblueswir1     },
897c48fcb47Sblueswir1     {
898c48fcb47Sblueswir1         .name = "TI UltraSparc II",
899c19148bdSblueswir1         .iu_version = ((0x17ULL << 48) | (0x11ULL << 32) | (0x20ULL << 24)),
900c48fcb47Sblueswir1         .fpu_version = 0x00000000,
901fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
9021a14026eSblueswir1         .nwindows = 8,
903c19148bdSblueswir1         .maxtl = 5,
90464a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
905c48fcb47Sblueswir1     },
906c48fcb47Sblueswir1     {
907c48fcb47Sblueswir1         .name = "TI UltraSparc IIi",
908c19148bdSblueswir1         .iu_version = ((0x17ULL << 48) | (0x12ULL << 32) | (0x91ULL << 24)),
909c48fcb47Sblueswir1         .fpu_version = 0x00000000,
910fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
9111a14026eSblueswir1         .nwindows = 8,
912c19148bdSblueswir1         .maxtl = 5,
91364a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
914c48fcb47Sblueswir1     },
915c48fcb47Sblueswir1     {
916c48fcb47Sblueswir1         .name = "TI UltraSparc IIe",
917c19148bdSblueswir1         .iu_version = ((0x17ULL << 48) | (0x13ULL << 32) | (0x14ULL << 24)),
918c48fcb47Sblueswir1         .fpu_version = 0x00000000,
919fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
9201a14026eSblueswir1         .nwindows = 8,
921c19148bdSblueswir1         .maxtl = 5,
92264a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
923c48fcb47Sblueswir1     },
924c48fcb47Sblueswir1     {
925c48fcb47Sblueswir1         .name = "Sun UltraSparc III",
926c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x14ULL << 32) | (0x34ULL << 24)),
927c48fcb47Sblueswir1         .fpu_version = 0x00000000,
928fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
9291a14026eSblueswir1         .nwindows = 8,
930c19148bdSblueswir1         .maxtl = 5,
93164a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
932c48fcb47Sblueswir1     },
933c48fcb47Sblueswir1     {
934c48fcb47Sblueswir1         .name = "Sun UltraSparc III Cu",
935c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x15ULL << 32) | (0x41ULL << 24)),
936c48fcb47Sblueswir1         .fpu_version = 0x00000000,
937fb79ceb9Sblueswir1         .mmu_version = mmu_us_3,
9381a14026eSblueswir1         .nwindows = 8,
939c19148bdSblueswir1         .maxtl = 5,
94064a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
941c48fcb47Sblueswir1     },
942c48fcb47Sblueswir1     {
943c48fcb47Sblueswir1         .name = "Sun UltraSparc IIIi",
944c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x16ULL << 32) | (0x34ULL << 24)),
945c48fcb47Sblueswir1         .fpu_version = 0x00000000,
946fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
9471a14026eSblueswir1         .nwindows = 8,
948c19148bdSblueswir1         .maxtl = 5,
94964a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
950c48fcb47Sblueswir1     },
951c48fcb47Sblueswir1     {
952c48fcb47Sblueswir1         .name = "Sun UltraSparc IV",
953c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x18ULL << 32) | (0x31ULL << 24)),
954c48fcb47Sblueswir1         .fpu_version = 0x00000000,
955fb79ceb9Sblueswir1         .mmu_version = mmu_us_4,
9561a14026eSblueswir1         .nwindows = 8,
957c19148bdSblueswir1         .maxtl = 5,
95864a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
959c48fcb47Sblueswir1     },
960c48fcb47Sblueswir1     {
961c48fcb47Sblueswir1         .name = "Sun UltraSparc IV+",
962c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x19ULL << 32) | (0x22ULL << 24)),
963c48fcb47Sblueswir1         .fpu_version = 0x00000000,
964fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
9651a14026eSblueswir1         .nwindows = 8,
966c19148bdSblueswir1         .maxtl = 5,
967fb79ceb9Sblueswir1         .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_CMT,
968c48fcb47Sblueswir1     },
969c48fcb47Sblueswir1     {
970c48fcb47Sblueswir1         .name = "Sun UltraSparc IIIi+",
971c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x22ULL << 32) | (0ULL << 24)),
972c48fcb47Sblueswir1         .fpu_version = 0x00000000,
973fb79ceb9Sblueswir1         .mmu_version = mmu_us_3,
9741a14026eSblueswir1         .nwindows = 8,
975c19148bdSblueswir1         .maxtl = 5,
97664a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
977c48fcb47Sblueswir1     },
978c48fcb47Sblueswir1     {
979c7ba218dSblueswir1         .name = "Sun UltraSparc T1",
980c7ba218dSblueswir1         // defined in sparc_ifu_fdp.v and ctu.h
981c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x23ULL << 32) | (0x02ULL << 24)),
982c7ba218dSblueswir1         .fpu_version = 0x00000000,
983c7ba218dSblueswir1         .mmu_version = mmu_sun4v,
984c7ba218dSblueswir1         .nwindows = 8,
985c19148bdSblueswir1         .maxtl = 6,
986c7ba218dSblueswir1         .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_HYPV | CPU_FEATURE_CMT
987c7ba218dSblueswir1         | CPU_FEATURE_GL,
988c7ba218dSblueswir1     },
989c7ba218dSblueswir1     {
990c7ba218dSblueswir1         .name = "Sun UltraSparc T2",
991c7ba218dSblueswir1         // defined in tlu_asi_ctl.v and n2_revid_cust.v
992c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x24ULL << 32) | (0x02ULL << 24)),
993c7ba218dSblueswir1         .fpu_version = 0x00000000,
994c7ba218dSblueswir1         .mmu_version = mmu_sun4v,
995c7ba218dSblueswir1         .nwindows = 8,
996c19148bdSblueswir1         .maxtl = 6,
997c7ba218dSblueswir1         .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_HYPV | CPU_FEATURE_CMT
998c7ba218dSblueswir1         | CPU_FEATURE_GL,
999c7ba218dSblueswir1     },
1000c7ba218dSblueswir1     {
1001c48fcb47Sblueswir1         .name = "NEC UltraSparc I",
1002c19148bdSblueswir1         .iu_version = ((0x22ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
1003c48fcb47Sblueswir1         .fpu_version = 0x00000000,
1004fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
10051a14026eSblueswir1         .nwindows = 8,
1006c19148bdSblueswir1         .maxtl = 5,
100764a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1008c48fcb47Sblueswir1     },
1009c48fcb47Sblueswir1 #else
1010c48fcb47Sblueswir1     {
1011c48fcb47Sblueswir1         .name = "Fujitsu MB86900",
1012c48fcb47Sblueswir1         .iu_version = 0x00 << 24, /* Impl 0, ver 0 */
1013c48fcb47Sblueswir1         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1014c48fcb47Sblueswir1         .mmu_version = 0x00 << 24, /* Impl 0, ver 0 */
1015c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1016c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1017c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1018c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1019c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
10201a14026eSblueswir1         .nwindows = 7,
1021e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_FSMULD,
1022c48fcb47Sblueswir1     },
1023c48fcb47Sblueswir1     {
1024c48fcb47Sblueswir1         .name = "Fujitsu MB86904",
1025c48fcb47Sblueswir1         .iu_version = 0x04 << 24, /* Impl 0, ver 4 */
1026c48fcb47Sblueswir1         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1027c48fcb47Sblueswir1         .mmu_version = 0x04 << 24, /* Impl 0, ver 4 */
1028c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1029c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x00ffffc0,
1030c48fcb47Sblueswir1         .mmu_cxr_mask = 0x000000ff,
1031c48fcb47Sblueswir1         .mmu_sfsr_mask = 0x00016fff,
1032c48fcb47Sblueswir1         .mmu_trcr_mask = 0x00ffffff,
10331a14026eSblueswir1         .nwindows = 8,
103464a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1035c48fcb47Sblueswir1     },
1036c48fcb47Sblueswir1     {
1037c48fcb47Sblueswir1         .name = "Fujitsu MB86907",
1038c48fcb47Sblueswir1         .iu_version = 0x05 << 24, /* Impl 0, ver 5 */
1039c48fcb47Sblueswir1         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1040c48fcb47Sblueswir1         .mmu_version = 0x05 << 24, /* Impl 0, ver 5 */
1041c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1042c48fcb47Sblueswir1         .mmu_ctpr_mask = 0xffffffc0,
1043c48fcb47Sblueswir1         .mmu_cxr_mask = 0x000000ff,
1044c48fcb47Sblueswir1         .mmu_sfsr_mask = 0x00016fff,
1045c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
10461a14026eSblueswir1         .nwindows = 8,
104764a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1048c48fcb47Sblueswir1     },
1049c48fcb47Sblueswir1     {
1050c48fcb47Sblueswir1         .name = "LSI L64811",
1051c48fcb47Sblueswir1         .iu_version = 0x10 << 24, /* Impl 1, ver 0 */
1052c48fcb47Sblueswir1         .fpu_version = 1 << 17, /* FPU version 1 (LSI L64814) */
1053c48fcb47Sblueswir1         .mmu_version = 0x10 << 24,
1054c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1055c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1056c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1057c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1058c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
10591a14026eSblueswir1         .nwindows = 8,
1060e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
1061e30b4678Sblueswir1         CPU_FEATURE_FSMULD,
1062c48fcb47Sblueswir1     },
1063c48fcb47Sblueswir1     {
1064c48fcb47Sblueswir1         .name = "Cypress CY7C601",
1065c48fcb47Sblueswir1         .iu_version = 0x11 << 24, /* Impl 1, ver 1 */
1066c48fcb47Sblueswir1         .fpu_version = 3 << 17, /* FPU version 3 (Cypress CY7C602) */
1067c48fcb47Sblueswir1         .mmu_version = 0x10 << 24,
1068c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1069c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1070c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1071c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1072c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
10731a14026eSblueswir1         .nwindows = 8,
1074e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
1075e30b4678Sblueswir1         CPU_FEATURE_FSMULD,
1076c48fcb47Sblueswir1     },
1077c48fcb47Sblueswir1     {
1078c48fcb47Sblueswir1         .name = "Cypress CY7C611",
1079c48fcb47Sblueswir1         .iu_version = 0x13 << 24, /* Impl 1, ver 3 */
1080c48fcb47Sblueswir1         .fpu_version = 3 << 17, /* FPU version 3 (Cypress CY7C602) */
1081c48fcb47Sblueswir1         .mmu_version = 0x10 << 24,
1082c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1083c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1084c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1085c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1086c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
10871a14026eSblueswir1         .nwindows = 8,
1088e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
1089e30b4678Sblueswir1         CPU_FEATURE_FSMULD,
1090c48fcb47Sblueswir1     },
1091c48fcb47Sblueswir1     {
1092c48fcb47Sblueswir1         .name = "TI MicroSparc I",
1093c48fcb47Sblueswir1         .iu_version = 0x41000000,
1094c48fcb47Sblueswir1         .fpu_version = 4 << 17,
1095c48fcb47Sblueswir1         .mmu_version = 0x41000000,
1096c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1097c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1098c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1099c48fcb47Sblueswir1         .mmu_sfsr_mask = 0x00016fff,
1100c48fcb47Sblueswir1         .mmu_trcr_mask = 0x0000003f,
11011a14026eSblueswir1         .nwindows = 7,
1102e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_MUL |
1103e30b4678Sblueswir1         CPU_FEATURE_DIV | CPU_FEATURE_FLUSH | CPU_FEATURE_FSQRT |
1104e30b4678Sblueswir1         CPU_FEATURE_FMUL,
1105c48fcb47Sblueswir1     },
1106c48fcb47Sblueswir1     {
1107c48fcb47Sblueswir1         .name = "TI MicroSparc II",
1108c48fcb47Sblueswir1         .iu_version = 0x42000000,
1109c48fcb47Sblueswir1         .fpu_version = 4 << 17,
1110c48fcb47Sblueswir1         .mmu_version = 0x02000000,
1111c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1112c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x00ffffc0,
1113c48fcb47Sblueswir1         .mmu_cxr_mask = 0x000000ff,
1114c48fcb47Sblueswir1         .mmu_sfsr_mask = 0x00016fff,
1115c48fcb47Sblueswir1         .mmu_trcr_mask = 0x00ffffff,
11161a14026eSblueswir1         .nwindows = 8,
111764a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1118c48fcb47Sblueswir1     },
1119c48fcb47Sblueswir1     {
1120c48fcb47Sblueswir1         .name = "TI MicroSparc IIep",
1121c48fcb47Sblueswir1         .iu_version = 0x42000000,
1122c48fcb47Sblueswir1         .fpu_version = 4 << 17,
1123c48fcb47Sblueswir1         .mmu_version = 0x04000000,
1124c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1125c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x00ffffc0,
1126c48fcb47Sblueswir1         .mmu_cxr_mask = 0x000000ff,
1127c48fcb47Sblueswir1         .mmu_sfsr_mask = 0x00016bff,
1128c48fcb47Sblueswir1         .mmu_trcr_mask = 0x00ffffff,
11291a14026eSblueswir1         .nwindows = 8,
113064a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1131c48fcb47Sblueswir1     },
1132c48fcb47Sblueswir1     {
1133b5154bdeSblueswir1         .name = "TI SuperSparc 40", // STP1020NPGA
1134963262deSblueswir1         .iu_version = 0x41000000, // SuperSPARC 2.x
1135b5154bdeSblueswir1         .fpu_version = 0 << 17,
1136963262deSblueswir1         .mmu_version = 0x00000800, // SuperSPARC 2.x, no MXCC
1137b5154bdeSblueswir1         .mmu_bm = 0x00002000,
1138b5154bdeSblueswir1         .mmu_ctpr_mask = 0xffffffc0,
1139b5154bdeSblueswir1         .mmu_cxr_mask = 0x0000ffff,
1140b5154bdeSblueswir1         .mmu_sfsr_mask = 0xffffffff,
1141b5154bdeSblueswir1         .mmu_trcr_mask = 0xffffffff,
11421a14026eSblueswir1         .nwindows = 8,
1143b5154bdeSblueswir1         .features = CPU_DEFAULT_FEATURES,
1144b5154bdeSblueswir1     },
1145b5154bdeSblueswir1     {
1146b5154bdeSblueswir1         .name = "TI SuperSparc 50", // STP1020PGA
1147963262deSblueswir1         .iu_version = 0x40000000, // SuperSPARC 3.x
1148b5154bdeSblueswir1         .fpu_version = 0 << 17,
1149963262deSblueswir1         .mmu_version = 0x01000800, // SuperSPARC 3.x, no MXCC
1150b5154bdeSblueswir1         .mmu_bm = 0x00002000,
1151b5154bdeSblueswir1         .mmu_ctpr_mask = 0xffffffc0,
1152b5154bdeSblueswir1         .mmu_cxr_mask = 0x0000ffff,
1153b5154bdeSblueswir1         .mmu_sfsr_mask = 0xffffffff,
1154b5154bdeSblueswir1         .mmu_trcr_mask = 0xffffffff,
11551a14026eSblueswir1         .nwindows = 8,
1156b5154bdeSblueswir1         .features = CPU_DEFAULT_FEATURES,
1157b5154bdeSblueswir1     },
1158b5154bdeSblueswir1     {
1159c48fcb47Sblueswir1         .name = "TI SuperSparc 51",
1160963262deSblueswir1         .iu_version = 0x40000000, // SuperSPARC 3.x
1161c48fcb47Sblueswir1         .fpu_version = 0 << 17,
1162963262deSblueswir1         .mmu_version = 0x01000000, // SuperSPARC 3.x, MXCC
1163c48fcb47Sblueswir1         .mmu_bm = 0x00002000,
1164c48fcb47Sblueswir1         .mmu_ctpr_mask = 0xffffffc0,
1165c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000ffff,
1166c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1167c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
1168963262deSblueswir1         .mxcc_version = 0x00000104,
11691a14026eSblueswir1         .nwindows = 8,
117064a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1171c48fcb47Sblueswir1     },
1172c48fcb47Sblueswir1     {
1173b5154bdeSblueswir1         .name = "TI SuperSparc 60", // STP1020APGA
1174963262deSblueswir1         .iu_version = 0x40000000, // SuperSPARC 3.x
1175b5154bdeSblueswir1         .fpu_version = 0 << 17,
1176963262deSblueswir1         .mmu_version = 0x01000800, // SuperSPARC 3.x, no MXCC
1177b5154bdeSblueswir1         .mmu_bm = 0x00002000,
1178b5154bdeSblueswir1         .mmu_ctpr_mask = 0xffffffc0,
1179b5154bdeSblueswir1         .mmu_cxr_mask = 0x0000ffff,
1180b5154bdeSblueswir1         .mmu_sfsr_mask = 0xffffffff,
1181b5154bdeSblueswir1         .mmu_trcr_mask = 0xffffffff,
11821a14026eSblueswir1         .nwindows = 8,
1183b5154bdeSblueswir1         .features = CPU_DEFAULT_FEATURES,
1184b5154bdeSblueswir1     },
1185b5154bdeSblueswir1     {
1186c48fcb47Sblueswir1         .name = "TI SuperSparc 61",
1187963262deSblueswir1         .iu_version = 0x44000000, // SuperSPARC 3.x
1188c48fcb47Sblueswir1         .fpu_version = 0 << 17,
1189963262deSblueswir1         .mmu_version = 0x01000000, // SuperSPARC 3.x, MXCC
1190c48fcb47Sblueswir1         .mmu_bm = 0x00002000,
1191c48fcb47Sblueswir1         .mmu_ctpr_mask = 0xffffffc0,
1192c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000ffff,
1193c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1194c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
1195963262deSblueswir1         .mxcc_version = 0x00000104,
1196963262deSblueswir1         .nwindows = 8,
1197963262deSblueswir1         .features = CPU_DEFAULT_FEATURES,
1198963262deSblueswir1     },
1199963262deSblueswir1     {
1200963262deSblueswir1         .name = "TI SuperSparc II",
1201963262deSblueswir1         .iu_version = 0x40000000, // SuperSPARC II 1.x
1202963262deSblueswir1         .fpu_version = 0 << 17,
1203963262deSblueswir1         .mmu_version = 0x08000000, // SuperSPARC II 1.x, MXCC
1204963262deSblueswir1         .mmu_bm = 0x00002000,
1205963262deSblueswir1         .mmu_ctpr_mask = 0xffffffc0,
1206963262deSblueswir1         .mmu_cxr_mask = 0x0000ffff,
1207963262deSblueswir1         .mmu_sfsr_mask = 0xffffffff,
1208963262deSblueswir1         .mmu_trcr_mask = 0xffffffff,
1209963262deSblueswir1         .mxcc_version = 0x00000104,
12101a14026eSblueswir1         .nwindows = 8,
121164a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1212c48fcb47Sblueswir1     },
1213c48fcb47Sblueswir1     {
1214c48fcb47Sblueswir1         .name = "Ross RT625",
1215c48fcb47Sblueswir1         .iu_version = 0x1e000000,
1216c48fcb47Sblueswir1         .fpu_version = 1 << 17,
1217c48fcb47Sblueswir1         .mmu_version = 0x1e000000,
1218c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1219c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1220c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1221c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1222c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
12231a14026eSblueswir1         .nwindows = 8,
122464a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1225c48fcb47Sblueswir1     },
1226c48fcb47Sblueswir1     {
1227c48fcb47Sblueswir1         .name = "Ross RT620",
1228c48fcb47Sblueswir1         .iu_version = 0x1f000000,
1229c48fcb47Sblueswir1         .fpu_version = 1 << 17,
1230c48fcb47Sblueswir1         .mmu_version = 0x1f000000,
1231c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1232c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1233c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1234c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1235c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
12361a14026eSblueswir1         .nwindows = 8,
123764a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1238c48fcb47Sblueswir1     },
1239c48fcb47Sblueswir1     {
1240c48fcb47Sblueswir1         .name = "BIT B5010",
1241c48fcb47Sblueswir1         .iu_version = 0x20000000,
1242c48fcb47Sblueswir1         .fpu_version = 0 << 17, /* B5010/B5110/B5120/B5210 */
1243c48fcb47Sblueswir1         .mmu_version = 0x20000000,
1244c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1245c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1246c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1247c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1248c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
12491a14026eSblueswir1         .nwindows = 8,
1250e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
1251e30b4678Sblueswir1         CPU_FEATURE_FSMULD,
1252c48fcb47Sblueswir1     },
1253c48fcb47Sblueswir1     {
1254c48fcb47Sblueswir1         .name = "Matsushita MN10501",
1255c48fcb47Sblueswir1         .iu_version = 0x50000000,
1256c48fcb47Sblueswir1         .fpu_version = 0 << 17,
1257c48fcb47Sblueswir1         .mmu_version = 0x50000000,
1258c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1259c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1260c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1261c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1262c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
12631a14026eSblueswir1         .nwindows = 8,
1264e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_MUL | CPU_FEATURE_FSQRT |
1265e30b4678Sblueswir1         CPU_FEATURE_FSMULD,
1266c48fcb47Sblueswir1     },
1267c48fcb47Sblueswir1     {
1268c48fcb47Sblueswir1         .name = "Weitek W8601",
1269c48fcb47Sblueswir1         .iu_version = 0x90 << 24, /* Impl 9, ver 0 */
1270c48fcb47Sblueswir1         .fpu_version = 3 << 17, /* FPU version 3 (Weitek WTL3170/2) */
1271c48fcb47Sblueswir1         .mmu_version = 0x10 << 24,
1272c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1273c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1274c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1275c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1276c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
12771a14026eSblueswir1         .nwindows = 8,
127864a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1279c48fcb47Sblueswir1     },
1280c48fcb47Sblueswir1     {
1281c48fcb47Sblueswir1         .name = "LEON2",
1282c48fcb47Sblueswir1         .iu_version = 0xf2000000,
1283c48fcb47Sblueswir1         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1284c48fcb47Sblueswir1         .mmu_version = 0xf2000000,
1285c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1286c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1287c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1288c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1289c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
12901a14026eSblueswir1         .nwindows = 8,
129164a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1292c48fcb47Sblueswir1     },
1293c48fcb47Sblueswir1     {
1294c48fcb47Sblueswir1         .name = "LEON3",
1295c48fcb47Sblueswir1         .iu_version = 0xf3000000,
1296c48fcb47Sblueswir1         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1297c48fcb47Sblueswir1         .mmu_version = 0xf3000000,
1298c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1299c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1300c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1301c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1302c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
13031a14026eSblueswir1         .nwindows = 8,
130464a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1305c48fcb47Sblueswir1     },
1306c48fcb47Sblueswir1 #endif
1307c48fcb47Sblueswir1 };
1308c48fcb47Sblueswir1 
130964a88d5dSblueswir1 static const char * const feature_name[] = {
131064a88d5dSblueswir1     "float",
131164a88d5dSblueswir1     "float128",
131264a88d5dSblueswir1     "swap",
131364a88d5dSblueswir1     "mul",
131464a88d5dSblueswir1     "div",
131564a88d5dSblueswir1     "flush",
131664a88d5dSblueswir1     "fsqrt",
131764a88d5dSblueswir1     "fmul",
131864a88d5dSblueswir1     "vis1",
131964a88d5dSblueswir1     "vis2",
1320e30b4678Sblueswir1     "fsmuld",
1321fb79ceb9Sblueswir1     "hypv",
1322fb79ceb9Sblueswir1     "cmt",
1323fb79ceb9Sblueswir1     "gl",
132464a88d5dSblueswir1 };
132564a88d5dSblueswir1 
13269a78eeadSStefan Weil static void print_features(FILE *f, fprintf_function cpu_fprintf,
132764a88d5dSblueswir1                            uint32_t features, const char *prefix)
1328c48fcb47Sblueswir1 {
1329c48fcb47Sblueswir1     unsigned int i;
1330c48fcb47Sblueswir1 
133164a88d5dSblueswir1     for (i = 0; i < ARRAY_SIZE(feature_name); i++)
133264a88d5dSblueswir1         if (feature_name[i] && (features & (1 << i))) {
133364a88d5dSblueswir1             if (prefix)
133464a88d5dSblueswir1                 (*cpu_fprintf)(f, "%s", prefix);
133564a88d5dSblueswir1             (*cpu_fprintf)(f, "%s ", feature_name[i]);
133664a88d5dSblueswir1         }
133764a88d5dSblueswir1 }
133864a88d5dSblueswir1 
133964a88d5dSblueswir1 static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features)
134064a88d5dSblueswir1 {
134164a88d5dSblueswir1     unsigned int i;
134264a88d5dSblueswir1 
134364a88d5dSblueswir1     for (i = 0; i < ARRAY_SIZE(feature_name); i++)
134464a88d5dSblueswir1         if (feature_name[i] && !strcmp(flagname, feature_name[i])) {
134564a88d5dSblueswir1             *features |= 1 << i;
134664a88d5dSblueswir1             return;
134764a88d5dSblueswir1         }
134864a88d5dSblueswir1     fprintf(stderr, "CPU feature %s not found\n", flagname);
134964a88d5dSblueswir1 }
135064a88d5dSblueswir1 
135122548760Sblueswir1 static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model)
135264a88d5dSblueswir1 {
135364a88d5dSblueswir1     unsigned int i;
135464a88d5dSblueswir1     const sparc_def_t *def = NULL;
135564a88d5dSblueswir1     char *s = strdup(cpu_model);
135664a88d5dSblueswir1     char *featurestr, *name = strtok(s, ",");
135764a88d5dSblueswir1     uint32_t plus_features = 0;
135864a88d5dSblueswir1     uint32_t minus_features = 0;
13590bfcd599SBlue Swirl     uint64_t iu_version;
13601a14026eSblueswir1     uint32_t fpu_version, mmu_version, nwindows;
136164a88d5dSblueswir1 
1362b1503cdaSmalc     for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
1363c48fcb47Sblueswir1         if (strcasecmp(name, sparc_defs[i].name) == 0) {
136464a88d5dSblueswir1             def = &sparc_defs[i];
1365c48fcb47Sblueswir1         }
1366c48fcb47Sblueswir1     }
136764a88d5dSblueswir1     if (!def)
136864a88d5dSblueswir1         goto error;
136964a88d5dSblueswir1     memcpy(cpu_def, def, sizeof(*def));
137064a88d5dSblueswir1 
137164a88d5dSblueswir1     featurestr = strtok(NULL, ",");
137264a88d5dSblueswir1     while (featurestr) {
137364a88d5dSblueswir1         char *val;
137464a88d5dSblueswir1 
137564a88d5dSblueswir1         if (featurestr[0] == '+') {
137664a88d5dSblueswir1             add_flagname_to_bitmaps(featurestr + 1, &plus_features);
137764a88d5dSblueswir1         } else if (featurestr[0] == '-') {
137864a88d5dSblueswir1             add_flagname_to_bitmaps(featurestr + 1, &minus_features);
137964a88d5dSblueswir1         } else if ((val = strchr(featurestr, '='))) {
138064a88d5dSblueswir1             *val = 0; val++;
138164a88d5dSblueswir1             if (!strcmp(featurestr, "iu_version")) {
138264a88d5dSblueswir1                 char *err;
138364a88d5dSblueswir1 
138464a88d5dSblueswir1                 iu_version = strtoll(val, &err, 0);
138564a88d5dSblueswir1                 if (!*val || *err) {
138664a88d5dSblueswir1                     fprintf(stderr, "bad numerical value %s\n", val);
138764a88d5dSblueswir1                     goto error;
138864a88d5dSblueswir1                 }
138964a88d5dSblueswir1                 cpu_def->iu_version = iu_version;
139064a88d5dSblueswir1 #ifdef DEBUG_FEATURES
13910bfcd599SBlue Swirl                 fprintf(stderr, "iu_version %" PRIx64 "\n", iu_version);
139264a88d5dSblueswir1 #endif
139364a88d5dSblueswir1             } else if (!strcmp(featurestr, "fpu_version")) {
139464a88d5dSblueswir1                 char *err;
139564a88d5dSblueswir1 
139664a88d5dSblueswir1                 fpu_version = strtol(val, &err, 0);
139764a88d5dSblueswir1                 if (!*val || *err) {
139864a88d5dSblueswir1                     fprintf(stderr, "bad numerical value %s\n", val);
139964a88d5dSblueswir1                     goto error;
140064a88d5dSblueswir1                 }
140164a88d5dSblueswir1                 cpu_def->fpu_version = fpu_version;
140264a88d5dSblueswir1 #ifdef DEBUG_FEATURES
14030bf9e31aSBlue Swirl                 fprintf(stderr, "fpu_version %x\n", fpu_version);
140464a88d5dSblueswir1 #endif
140564a88d5dSblueswir1             } else if (!strcmp(featurestr, "mmu_version")) {
140664a88d5dSblueswir1                 char *err;
140764a88d5dSblueswir1 
140864a88d5dSblueswir1                 mmu_version = strtol(val, &err, 0);
140964a88d5dSblueswir1                 if (!*val || *err) {
141064a88d5dSblueswir1                     fprintf(stderr, "bad numerical value %s\n", val);
141164a88d5dSblueswir1                     goto error;
141264a88d5dSblueswir1                 }
141364a88d5dSblueswir1                 cpu_def->mmu_version = mmu_version;
141464a88d5dSblueswir1 #ifdef DEBUG_FEATURES
14150bf9e31aSBlue Swirl                 fprintf(stderr, "mmu_version %x\n", mmu_version);
141664a88d5dSblueswir1 #endif
14171a14026eSblueswir1             } else if (!strcmp(featurestr, "nwindows")) {
14181a14026eSblueswir1                 char *err;
14191a14026eSblueswir1 
14201a14026eSblueswir1                 nwindows = strtol(val, &err, 0);
14211a14026eSblueswir1                 if (!*val || *err || nwindows > MAX_NWINDOWS ||
14221a14026eSblueswir1                     nwindows < MIN_NWINDOWS) {
14231a14026eSblueswir1                     fprintf(stderr, "bad numerical value %s\n", val);
14241a14026eSblueswir1                     goto error;
14251a14026eSblueswir1                 }
14261a14026eSblueswir1                 cpu_def->nwindows = nwindows;
14271a14026eSblueswir1 #ifdef DEBUG_FEATURES
14281a14026eSblueswir1                 fprintf(stderr, "nwindows %d\n", nwindows);
14291a14026eSblueswir1 #endif
143064a88d5dSblueswir1             } else {
143164a88d5dSblueswir1                 fprintf(stderr, "unrecognized feature %s\n", featurestr);
143264a88d5dSblueswir1                 goto error;
143364a88d5dSblueswir1             }
143464a88d5dSblueswir1         } else {
143577f193daSblueswir1             fprintf(stderr, "feature string `%s' not in format "
143677f193daSblueswir1                     "(+feature|-feature|feature=xyz)\n", featurestr);
143764a88d5dSblueswir1             goto error;
143864a88d5dSblueswir1         }
143964a88d5dSblueswir1         featurestr = strtok(NULL, ",");
144064a88d5dSblueswir1     }
144164a88d5dSblueswir1     cpu_def->features |= plus_features;
144264a88d5dSblueswir1     cpu_def->features &= ~minus_features;
144364a88d5dSblueswir1 #ifdef DEBUG_FEATURES
144464a88d5dSblueswir1     print_features(stderr, fprintf, cpu_def->features, NULL);
144564a88d5dSblueswir1 #endif
144664a88d5dSblueswir1     free(s);
144764a88d5dSblueswir1     return 0;
144864a88d5dSblueswir1 
144964a88d5dSblueswir1  error:
145064a88d5dSblueswir1     free(s);
145164a88d5dSblueswir1     return -1;
1452c48fcb47Sblueswir1 }
1453c48fcb47Sblueswir1 
14549a78eeadSStefan Weil void sparc_cpu_list(FILE *f, fprintf_function cpu_fprintf)
1455c48fcb47Sblueswir1 {
1456c48fcb47Sblueswir1     unsigned int i;
1457c48fcb47Sblueswir1 
1458b1503cdaSmalc     for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
14591a14026eSblueswir1         (*cpu_fprintf)(f, "Sparc %16s IU " TARGET_FMT_lx " FPU %08x MMU %08x NWINS %d ",
1460c48fcb47Sblueswir1                        sparc_defs[i].name,
1461c48fcb47Sblueswir1                        sparc_defs[i].iu_version,
1462c48fcb47Sblueswir1                        sparc_defs[i].fpu_version,
14631a14026eSblueswir1                        sparc_defs[i].mmu_version,
14641a14026eSblueswir1                        sparc_defs[i].nwindows);
146577f193daSblueswir1         print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES &
146677f193daSblueswir1                        ~sparc_defs[i].features, "-");
146777f193daSblueswir1         print_features(f, cpu_fprintf, ~CPU_DEFAULT_FEATURES &
146877f193daSblueswir1                        sparc_defs[i].features, "+");
146964a88d5dSblueswir1         (*cpu_fprintf)(f, "\n");
1470c48fcb47Sblueswir1     }
1471f76981b1Sblueswir1     (*cpu_fprintf)(f, "Default CPU feature flags (use '-' to remove): ");
1472f76981b1Sblueswir1     print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES, NULL);
147364a88d5dSblueswir1     (*cpu_fprintf)(f, "\n");
1474f76981b1Sblueswir1     (*cpu_fprintf)(f, "Available CPU feature flags (use '+' to add): ");
1475f76981b1Sblueswir1     print_features(f, cpu_fprintf, ~CPU_DEFAULT_FEATURES, NULL);
1476f76981b1Sblueswir1     (*cpu_fprintf)(f, "\n");
1477f76981b1Sblueswir1     (*cpu_fprintf)(f, "Numerical features (use '=' to set): iu_version "
1478f76981b1Sblueswir1                    "fpu_version mmu_version nwindows\n");
1479c48fcb47Sblueswir1 }
1480c48fcb47Sblueswir1 
14819a78eeadSStefan Weil static void cpu_print_cc(FILE *f, fprintf_function cpu_fprintf,
148243bb98bfSBlue Swirl                          uint32_t cc)
148343bb98bfSBlue Swirl {
148443bb98bfSBlue Swirl     cpu_fprintf(f, "%c%c%c%c", cc & PSR_NEG? 'N' : '-',
148543bb98bfSBlue Swirl                 cc & PSR_ZERO? 'Z' : '-', cc & PSR_OVF? 'V' : '-',
148643bb98bfSBlue Swirl                 cc & PSR_CARRY? 'C' : '-');
148743bb98bfSBlue Swirl }
148843bb98bfSBlue Swirl 
148943bb98bfSBlue Swirl #ifdef TARGET_SPARC64
149043bb98bfSBlue Swirl #define REGS_PER_LINE 4
149143bb98bfSBlue Swirl #else
149243bb98bfSBlue Swirl #define REGS_PER_LINE 8
149343bb98bfSBlue Swirl #endif
149443bb98bfSBlue Swirl 
14959a78eeadSStefan Weil void cpu_dump_state(CPUState *env, FILE *f, fprintf_function cpu_fprintf,
1496c48fcb47Sblueswir1                     int flags)
1497c48fcb47Sblueswir1 {
1498c48fcb47Sblueswir1     int i, x;
1499c48fcb47Sblueswir1 
150077f193daSblueswir1     cpu_fprintf(f, "pc: " TARGET_FMT_lx "  npc: " TARGET_FMT_lx "\n", env->pc,
150177f193daSblueswir1                 env->npc);
1502c48fcb47Sblueswir1     cpu_fprintf(f, "General Registers:\n");
150343bb98bfSBlue Swirl 
150443bb98bfSBlue Swirl     for (i = 0; i < 8; i++) {
150543bb98bfSBlue Swirl         if (i % REGS_PER_LINE == 0) {
150643bb98bfSBlue Swirl             cpu_fprintf(f, "%%g%d-%d:", i, i + REGS_PER_LINE - 1);
150743bb98bfSBlue Swirl         }
150843bb98bfSBlue Swirl         cpu_fprintf(f, " " TARGET_FMT_lx, env->gregs[i]);
150943bb98bfSBlue Swirl         if (i % REGS_PER_LINE == REGS_PER_LINE - 1) {
1510c48fcb47Sblueswir1             cpu_fprintf(f, "\n");
1511c48fcb47Sblueswir1         }
151243bb98bfSBlue Swirl     }
151343bb98bfSBlue Swirl     cpu_fprintf(f, "\nCurrent Register Window:\n");
151443bb98bfSBlue Swirl     for (x = 0; x < 3; x++) {
151543bb98bfSBlue Swirl         for (i = 0; i < 8; i++) {
151643bb98bfSBlue Swirl             if (i % REGS_PER_LINE == 0) {
151743bb98bfSBlue Swirl                 cpu_fprintf(f, "%%%c%d-%d: ",
151843bb98bfSBlue Swirl                             x == 0 ? 'o' : (x == 1 ? 'l' : 'i'),
151943bb98bfSBlue Swirl                             i, i + REGS_PER_LINE - 1);
152043bb98bfSBlue Swirl             }
152143bb98bfSBlue Swirl             cpu_fprintf(f, TARGET_FMT_lx " ", env->regwptr[i + x * 8]);
152243bb98bfSBlue Swirl             if (i % REGS_PER_LINE == REGS_PER_LINE - 1) {
152343bb98bfSBlue Swirl                 cpu_fprintf(f, "\n");
152443bb98bfSBlue Swirl             }
152543bb98bfSBlue Swirl         }
152643bb98bfSBlue Swirl     }
1527c48fcb47Sblueswir1     cpu_fprintf(f, "\nFloating Point Registers:\n");
152843bb98bfSBlue Swirl     for (i = 0; i < TARGET_FPREGS; i++) {
1529c48fcb47Sblueswir1         if ((i & 3) == 0)
1530c48fcb47Sblueswir1             cpu_fprintf(f, "%%f%02d:", i);
1531a37ee56cSblueswir1         cpu_fprintf(f, " %016f", *(float *)&env->fpr[i]);
1532c48fcb47Sblueswir1         if ((i & 3) == 3)
1533c48fcb47Sblueswir1             cpu_fprintf(f, "\n");
1534c48fcb47Sblueswir1     }
1535c48fcb47Sblueswir1 #ifdef TARGET_SPARC64
153643bb98bfSBlue Swirl     cpu_fprintf(f, "pstate: %08x ccr: %02x (icc: ", env->pstate,
1537113c6106SStefan Weil                 (unsigned)cpu_get_ccr(env));
15385a834bb4SBlue Swirl     cpu_print_cc(f, cpu_fprintf, cpu_get_ccr(env) << PSR_CARRY_SHIFT);
153943bb98bfSBlue Swirl     cpu_fprintf(f, " xcc: ");
15405a834bb4SBlue Swirl     cpu_print_cc(f, cpu_fprintf, cpu_get_ccr(env) << (PSR_CARRY_SHIFT - 4));
154143bb98bfSBlue Swirl     cpu_fprintf(f, ") asi: %02x tl: %d pil: %x\n", env->asi, env->tl,
154243bb98bfSBlue Swirl                 env->psrpil);
154343bb98bfSBlue Swirl     cpu_fprintf(f, "cansave: %d canrestore: %d otherwin: %d wstate: %d "
154443bb98bfSBlue Swirl                 "cleanwin: %d cwp: %d\n",
1545c48fcb47Sblueswir1                 env->cansave, env->canrestore, env->otherwin, env->wstate,
15461a14026eSblueswir1                 env->cleanwin, env->nwindows - 1 - env->cwp);
154743bb98bfSBlue Swirl     cpu_fprintf(f, "fsr: " TARGET_FMT_lx " y: " TARGET_FMT_lx " fprs: "
154843bb98bfSBlue Swirl                 TARGET_FMT_lx "\n", env->fsr, env->y, env->fprs);
1549c48fcb47Sblueswir1 #else
15505a834bb4SBlue Swirl     cpu_fprintf(f, "psr: %08x (icc: ", cpu_get_psr(env));
15515a834bb4SBlue Swirl     cpu_print_cc(f, cpu_fprintf, cpu_get_psr(env));
155243bb98bfSBlue Swirl     cpu_fprintf(f, " SPE: %c%c%c) wim: %08x\n", env->psrs? 'S' : '-',
155343bb98bfSBlue Swirl                 env->psrps? 'P' : '-', env->psret? 'E' : '-',
155443bb98bfSBlue Swirl                 env->wim);
155543bb98bfSBlue Swirl     cpu_fprintf(f, "fsr: " TARGET_FMT_lx " y: " TARGET_FMT_lx "\n",
155643bb98bfSBlue Swirl                 env->fsr, env->y);
1557c48fcb47Sblueswir1 #endif
1558c48fcb47Sblueswir1 }
1559