xref: /qemu/target/sparc/helper.c (revision b04d98905400aeb7dd62ce938d7eecddf2816817)
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 
449d893301Sbellard #if defined(CONFIG_USER_ONLY)
459d893301Sbellard 
4622548760Sblueswir1 int cpu_sparc_handle_mmu_fault(CPUState *env1, target_ulong address, int rw,
476ebbf390Sj_mayer                                int mmu_idx, int is_softmmu)
489d893301Sbellard {
49878d3096Sbellard     if (rw & 2)
5022548760Sblueswir1         env1->exception_index = TT_TFAULT;
51878d3096Sbellard     else
5222548760Sblueswir1         env1->exception_index = TT_DFAULT;
539d893301Sbellard     return 1;
549d893301Sbellard }
559d893301Sbellard 
569d893301Sbellard #else
57e8af50a3Sbellard 
583475187dSbellard #ifndef TARGET_SPARC64
5983469015Sbellard /*
6083469015Sbellard  * Sparc V8 Reference MMU (SRMMU)
6183469015Sbellard  */
62e8af50a3Sbellard static const int access_table[8][8] = {
63a764a566Sblueswir1     { 0, 0, 0, 0, 8, 0, 12, 12 },
64a764a566Sblueswir1     { 0, 0, 0, 0, 8, 0, 0, 0 },
65a764a566Sblueswir1     { 8, 8, 0, 0, 0, 8, 12, 12 },
66a764a566Sblueswir1     { 8, 8, 0, 0, 0, 8, 0, 0 },
67a764a566Sblueswir1     { 8, 0, 8, 0, 8, 8, 12, 12 },
68a764a566Sblueswir1     { 8, 0, 8, 0, 8, 0, 8, 0 },
69a764a566Sblueswir1     { 8, 8, 8, 0, 8, 8, 12, 12 },
70a764a566Sblueswir1     { 8, 8, 8, 0, 8, 8, 8, 0 }
71e8af50a3Sbellard };
72e8af50a3Sbellard 
73227671c9Sbellard static const int perm_table[2][8] = {
74227671c9Sbellard     {
75227671c9Sbellard         PAGE_READ,
76227671c9Sbellard         PAGE_READ | PAGE_WRITE,
77227671c9Sbellard         PAGE_READ | PAGE_EXEC,
78227671c9Sbellard         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
79227671c9Sbellard         PAGE_EXEC,
80227671c9Sbellard         PAGE_READ | PAGE_WRITE,
81227671c9Sbellard         PAGE_READ | PAGE_EXEC,
82227671c9Sbellard         PAGE_READ | PAGE_WRITE | PAGE_EXEC
83227671c9Sbellard     },
84227671c9Sbellard     {
85227671c9Sbellard         PAGE_READ,
86227671c9Sbellard         PAGE_READ | PAGE_WRITE,
87227671c9Sbellard         PAGE_READ | PAGE_EXEC,
88227671c9Sbellard         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
89227671c9Sbellard         PAGE_EXEC,
90227671c9Sbellard         PAGE_READ,
91227671c9Sbellard         0,
92227671c9Sbellard         0,
93227671c9Sbellard     }
94e8af50a3Sbellard };
95e8af50a3Sbellard 
96c227f099SAnthony Liguori static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
97c48fcb47Sblueswir1                                 int *prot, int *access_index,
98d4c430a8SPaul Brook                                 target_ulong address, int rw, int mmu_idx,
99d4c430a8SPaul Brook                                 target_ulong *page_size)
100e8af50a3Sbellard {
101e80cfcfcSbellard     int access_perms = 0;
102c227f099SAnthony Liguori     target_phys_addr_t pde_ptr;
103af7bf89bSbellard     uint32_t pde;
1046ebbf390Sj_mayer     int error_code = 0, is_dirty, is_user;
105e80cfcfcSbellard     unsigned long page_offset;
106e8af50a3Sbellard 
1076ebbf390Sj_mayer     is_user = mmu_idx == MMU_USER_IDX;
10840ce0a9aSblueswir1 
109e8af50a3Sbellard     if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
110d4c430a8SPaul Brook         *page_size = TARGET_PAGE_SIZE;
11140ce0a9aSblueswir1         // Boot mode: instruction fetches are taken from PROM
1125578ceabSblueswir1         if (rw == 2 && (env->mmuregs[0] & env->def->mmu_bm)) {
11358a770f3Sblueswir1             *physical = env->prom_addr | (address & 0x7ffffULL);
11440ce0a9aSblueswir1             *prot = PAGE_READ | PAGE_EXEC;
11540ce0a9aSblueswir1             return 0;
11640ce0a9aSblueswir1         }
117e80cfcfcSbellard         *physical = address;
118227671c9Sbellard         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
119e80cfcfcSbellard         return 0;
120e8af50a3Sbellard     }
121e8af50a3Sbellard 
1227483750dSbellard     *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user? 0 : 1);
1235dcb6b91Sblueswir1     *physical = 0xffffffffffff0000ULL;
1247483750dSbellard 
125e8af50a3Sbellard     /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
126e8af50a3Sbellard     /* Context base + context number */
1273deaeab7Sblueswir1     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
12849be8030Sbellard     pde = ldl_phys(pde_ptr);
129e8af50a3Sbellard 
130e8af50a3Sbellard     /* Ctx pde */
131e8af50a3Sbellard     switch (pde & PTE_ENTRYTYPE_MASK) {
132e80cfcfcSbellard     default:
133e8af50a3Sbellard     case 0: /* Invalid */
1347483750dSbellard         return 1 << 2;
135e80cfcfcSbellard     case 2: /* L0 PTE, maybe should not happen? */
136e8af50a3Sbellard     case 3: /* Reserved */
1377483750dSbellard         return 4 << 2;
138e80cfcfcSbellard     case 1: /* L0 PDE */
139e80cfcfcSbellard         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
14049be8030Sbellard         pde = ldl_phys(pde_ptr);
141e80cfcfcSbellard 
142e80cfcfcSbellard         switch (pde & PTE_ENTRYTYPE_MASK) {
143e80cfcfcSbellard         default:
144e80cfcfcSbellard         case 0: /* Invalid */
1457483750dSbellard             return (1 << 8) | (1 << 2);
146e80cfcfcSbellard         case 3: /* Reserved */
1477483750dSbellard             return (1 << 8) | (4 << 2);
148e8af50a3Sbellard         case 1: /* L1 PDE */
149e80cfcfcSbellard             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
15049be8030Sbellard             pde = ldl_phys(pde_ptr);
151e8af50a3Sbellard 
152e8af50a3Sbellard             switch (pde & PTE_ENTRYTYPE_MASK) {
153e80cfcfcSbellard             default:
154e8af50a3Sbellard             case 0: /* Invalid */
1557483750dSbellard                 return (2 << 8) | (1 << 2);
156e8af50a3Sbellard             case 3: /* Reserved */
1577483750dSbellard                 return (2 << 8) | (4 << 2);
158e8af50a3Sbellard             case 1: /* L2 PDE */
159e80cfcfcSbellard                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
16049be8030Sbellard                 pde = ldl_phys(pde_ptr);
161e8af50a3Sbellard 
162e8af50a3Sbellard                 switch (pde & PTE_ENTRYTYPE_MASK) {
163e80cfcfcSbellard                 default:
164e8af50a3Sbellard                 case 0: /* Invalid */
1657483750dSbellard                     return (3 << 8) | (1 << 2);
166e8af50a3Sbellard                 case 1: /* PDE, should not happen */
167e8af50a3Sbellard                 case 3: /* Reserved */
1687483750dSbellard                     return (3 << 8) | (4 << 2);
169e8af50a3Sbellard                 case 2: /* L3 PTE */
17077f193daSblueswir1                     page_offset = (address & TARGET_PAGE_MASK) &
17177f193daSblueswir1                         (TARGET_PAGE_SIZE - 1);
172e8af50a3Sbellard                 }
173d4c430a8SPaul Brook                 *page_size = TARGET_PAGE_SIZE;
174e8af50a3Sbellard                 break;
175e8af50a3Sbellard             case 2: /* L2 PTE */
176e8af50a3Sbellard                 page_offset = address & 0x3ffff;
177d4c430a8SPaul Brook                 *page_size = 0x40000;
178e8af50a3Sbellard             }
179e8af50a3Sbellard             break;
180e8af50a3Sbellard         case 2: /* L1 PTE */
181e8af50a3Sbellard             page_offset = address & 0xffffff;
182d4c430a8SPaul Brook             *page_size = 0x1000000;
183e8af50a3Sbellard         }
184e8af50a3Sbellard     }
185e8af50a3Sbellard 
186698235aaSArtyom Tarasenko     /* check access */
187698235aaSArtyom Tarasenko     access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
188698235aaSArtyom Tarasenko     error_code = access_table[*access_index][access_perms];
189698235aaSArtyom Tarasenko     if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user))
190698235aaSArtyom Tarasenko         return error_code;
191698235aaSArtyom Tarasenko 
192e8af50a3Sbellard     /* update page modified and dirty bits */
193b769d8feSbellard     is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
194e8af50a3Sbellard     if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
195e8af50a3Sbellard         pde |= PG_ACCESSED_MASK;
196e8af50a3Sbellard         if (is_dirty)
197e8af50a3Sbellard             pde |= PG_MODIFIED_MASK;
19849be8030Sbellard         stl_phys_notdirty(pde_ptr, pde);
199e8af50a3Sbellard     }
200e8af50a3Sbellard 
201e8af50a3Sbellard     /* the page can be put in the TLB */
202227671c9Sbellard     *prot = perm_table[is_user][access_perms];
203227671c9Sbellard     if (!(pde & PG_MODIFIED_MASK)) {
204e8af50a3Sbellard         /* only set write access if already dirty... otherwise wait
205e8af50a3Sbellard            for dirty access */
206227671c9Sbellard         *prot &= ~PAGE_WRITE;
207e8af50a3Sbellard     }
208e8af50a3Sbellard 
209e8af50a3Sbellard     /* Even if large ptes, we map only one 4KB page in the cache to
210e8af50a3Sbellard        avoid filling it too fast */
211c227f099SAnthony Liguori     *physical = ((target_phys_addr_t)(pde & PTE_ADDR_MASK) << 4) + page_offset;
2126f7e9aecSbellard     return error_code;
213e80cfcfcSbellard }
214e80cfcfcSbellard 
215e80cfcfcSbellard /* Perform address translation */
216af7bf89bSbellard int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
2176ebbf390Sj_mayer                               int mmu_idx, int is_softmmu)
218e80cfcfcSbellard {
219c227f099SAnthony Liguori     target_phys_addr_t paddr;
2205dcb6b91Sblueswir1     target_ulong vaddr;
221d4c430a8SPaul Brook     target_ulong page_size;
222d4c430a8SPaul Brook     int error_code = 0, prot, access_index;
223e80cfcfcSbellard 
22477f193daSblueswir1     error_code = get_physical_address(env, &paddr, &prot, &access_index,
225d4c430a8SPaul Brook                                       address, rw, mmu_idx, &page_size);
226e80cfcfcSbellard     if (error_code == 0) {
2279e61bde5Sbellard         vaddr = address & TARGET_PAGE_MASK;
2289e61bde5Sbellard         paddr &= TARGET_PAGE_MASK;
2299e61bde5Sbellard #ifdef DEBUG_MMU
2305dcb6b91Sblueswir1         printf("Translate at " TARGET_FMT_lx " -> " TARGET_FMT_plx ", vaddr "
2315dcb6b91Sblueswir1                TARGET_FMT_lx "\n", address, paddr, vaddr);
2329e61bde5Sbellard #endif
233d4c430a8SPaul Brook         tlb_set_page(env, vaddr, paddr, prot, mmu_idx, page_size);
234d4c430a8SPaul Brook         return 0;
235e80cfcfcSbellard     }
236e8af50a3Sbellard 
237e8af50a3Sbellard     if (env->mmuregs[3]) /* Fault status register */
238e8af50a3Sbellard         env->mmuregs[3] = 1; /* overflow (not read before another fault) */
2397483750dSbellard     env->mmuregs[3] |= (access_index << 5) | error_code | 2;
240e8af50a3Sbellard     env->mmuregs[4] = address; /* Fault address register */
241e8af50a3Sbellard 
242878d3096Sbellard     if ((env->mmuregs[0] & MMU_NF) || env->psret == 0)  {
2436f7e9aecSbellard         // No fault mode: if a mapping is available, just override
2446f7e9aecSbellard         // permissions. If no mapping is available, redirect accesses to
2456f7e9aecSbellard         // neverland. Fake/overridden mappings will be flushed when
2466f7e9aecSbellard         // switching to normal mode.
2477483750dSbellard         vaddr = address & TARGET_PAGE_MASK;
248227671c9Sbellard         prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
249d4c430a8SPaul Brook         tlb_set_page(env, vaddr, paddr, prot, mmu_idx, TARGET_PAGE_SIZE);
250d4c430a8SPaul Brook         return 0;
2517483750dSbellard     } else {
252878d3096Sbellard         if (rw & 2)
253878d3096Sbellard             env->exception_index = TT_TFAULT;
254878d3096Sbellard         else
255878d3096Sbellard             env->exception_index = TT_DFAULT;
256878d3096Sbellard         return 1;
257e8af50a3Sbellard     }
2587483750dSbellard }
25924741ef3Sbellard 
26024741ef3Sbellard target_ulong mmu_probe(CPUState *env, target_ulong address, int mmulev)
26124741ef3Sbellard {
262c227f099SAnthony Liguori     target_phys_addr_t pde_ptr;
26324741ef3Sbellard     uint32_t pde;
26424741ef3Sbellard 
26524741ef3Sbellard     /* Context base + context number */
266c227f099SAnthony Liguori     pde_ptr = (target_phys_addr_t)(env->mmuregs[1] << 4) +
2675dcb6b91Sblueswir1         (env->mmuregs[2] << 2);
26824741ef3Sbellard     pde = ldl_phys(pde_ptr);
26924741ef3Sbellard 
27024741ef3Sbellard     switch (pde & PTE_ENTRYTYPE_MASK) {
27124741ef3Sbellard     default:
27224741ef3Sbellard     case 0: /* Invalid */
27324741ef3Sbellard     case 2: /* PTE, maybe should not happen? */
27424741ef3Sbellard     case 3: /* Reserved */
27524741ef3Sbellard         return 0;
27624741ef3Sbellard     case 1: /* L1 PDE */
27724741ef3Sbellard         if (mmulev == 3)
27824741ef3Sbellard             return pde;
27924741ef3Sbellard         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
28024741ef3Sbellard         pde = ldl_phys(pde_ptr);
28124741ef3Sbellard 
28224741ef3Sbellard         switch (pde & PTE_ENTRYTYPE_MASK) {
28324741ef3Sbellard         default:
28424741ef3Sbellard         case 0: /* Invalid */
28524741ef3Sbellard         case 3: /* Reserved */
28624741ef3Sbellard             return 0;
28724741ef3Sbellard         case 2: /* L1 PTE */
28824741ef3Sbellard             return pde;
28924741ef3Sbellard         case 1: /* L2 PDE */
29024741ef3Sbellard             if (mmulev == 2)
29124741ef3Sbellard                 return pde;
29224741ef3Sbellard             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
29324741ef3Sbellard             pde = ldl_phys(pde_ptr);
29424741ef3Sbellard 
29524741ef3Sbellard             switch (pde & PTE_ENTRYTYPE_MASK) {
29624741ef3Sbellard             default:
29724741ef3Sbellard             case 0: /* Invalid */
29824741ef3Sbellard             case 3: /* Reserved */
29924741ef3Sbellard                 return 0;
30024741ef3Sbellard             case 2: /* L2 PTE */
30124741ef3Sbellard                 return pde;
30224741ef3Sbellard             case 1: /* L3 PDE */
30324741ef3Sbellard                 if (mmulev == 1)
30424741ef3Sbellard                     return pde;
30524741ef3Sbellard                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
30624741ef3Sbellard                 pde = ldl_phys(pde_ptr);
30724741ef3Sbellard 
30824741ef3Sbellard                 switch (pde & PTE_ENTRYTYPE_MASK) {
30924741ef3Sbellard                 default:
31024741ef3Sbellard                 case 0: /* Invalid */
31124741ef3Sbellard                 case 1: /* PDE, should not happen */
31224741ef3Sbellard                 case 3: /* Reserved */
31324741ef3Sbellard                     return 0;
31424741ef3Sbellard                 case 2: /* L3 PTE */
31524741ef3Sbellard                     return pde;
31624741ef3Sbellard                 }
31724741ef3Sbellard             }
31824741ef3Sbellard         }
31924741ef3Sbellard     }
32024741ef3Sbellard     return 0;
32124741ef3Sbellard }
32224741ef3Sbellard 
323d41160a3SBlue Swirl void dump_mmu(FILE *f, fprintf_function cpu_fprintf, CPUState *env)
32424741ef3Sbellard {
32524741ef3Sbellard     target_ulong va, va1, va2;
32624741ef3Sbellard     unsigned int n, m, o;
327c227f099SAnthony Liguori     target_phys_addr_t pde_ptr, pa;
32824741ef3Sbellard     uint32_t pde;
32924741ef3Sbellard 
33024741ef3Sbellard     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
33124741ef3Sbellard     pde = ldl_phys(pde_ptr);
332d41160a3SBlue Swirl     (*cpu_fprintf)(f, "Root ptr: " TARGET_FMT_plx ", ctx: %d\n",
333c227f099SAnthony Liguori                    (target_phys_addr_t)env->mmuregs[1] << 4, env->mmuregs[2]);
33424741ef3Sbellard     for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
3355dcb6b91Sblueswir1         pde = mmu_probe(env, va, 2);
3365dcb6b91Sblueswir1         if (pde) {
33724741ef3Sbellard             pa = cpu_get_phys_page_debug(env, va);
338d41160a3SBlue Swirl             (*cpu_fprintf)(f, "VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
3395dcb6b91Sblueswir1                            " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
34024741ef3Sbellard             for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
3415dcb6b91Sblueswir1                 pde = mmu_probe(env, va1, 1);
3425dcb6b91Sblueswir1                 if (pde) {
34324741ef3Sbellard                     pa = cpu_get_phys_page_debug(env, va1);
344d41160a3SBlue Swirl                     (*cpu_fprintf)(f, " VA: " TARGET_FMT_lx ", PA: "
345d41160a3SBlue Swirl                                    TARGET_FMT_plx " PDE: " TARGET_FMT_lx "\n",
346d41160a3SBlue Swirl                                    va1, pa, pde);
34724741ef3Sbellard                     for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
3485dcb6b91Sblueswir1                         pde = mmu_probe(env, va2, 0);
3495dcb6b91Sblueswir1                         if (pde) {
35024741ef3Sbellard                             pa = cpu_get_phys_page_debug(env, va2);
351d41160a3SBlue Swirl                             (*cpu_fprintf)(f, "  VA: " TARGET_FMT_lx ", PA: "
352d41160a3SBlue Swirl                                            TARGET_FMT_plx " PTE: "
353d41160a3SBlue Swirl                                            TARGET_FMT_lx "\n",
3545dcb6b91Sblueswir1                                            va2, pa, pde);
35524741ef3Sbellard                         }
35624741ef3Sbellard                     }
35724741ef3Sbellard                 }
35824741ef3Sbellard             }
35924741ef3Sbellard         }
36024741ef3Sbellard     }
36124741ef3Sbellard }
36224741ef3Sbellard 
36324741ef3Sbellard #else /* !TARGET_SPARC64 */
364e8807b14SIgor Kovalenko 
365e8807b14SIgor Kovalenko // 41 bit physical address space
366c227f099SAnthony Liguori static inline target_phys_addr_t ultrasparc_truncate_physical(uint64_t x)
367e8807b14SIgor Kovalenko {
368e8807b14SIgor Kovalenko     return x & 0x1ffffffffffULL;
369e8807b14SIgor Kovalenko }
370e8807b14SIgor Kovalenko 
37183469015Sbellard /*
37283469015Sbellard  * UltraSparc IIi I/DMMUs
37383469015Sbellard  */
3743475187dSbellard 
375536ba015SIgor Kovalenko // Returns true if TTE tag is valid and matches virtual address value in context
376536ba015SIgor Kovalenko // requires virtual address mask value calculated from TTE entry size
3776e8e7d4cSIgor Kovalenko static inline int ultrasparc_tag_match(SparcTLBEntry *tlb,
378536ba015SIgor Kovalenko                                        uint64_t address, uint64_t context,
379299b520cSIgor V. Kovalenko                                        target_phys_addr_t *physical)
380536ba015SIgor Kovalenko {
381536ba015SIgor Kovalenko     uint64_t mask;
382536ba015SIgor Kovalenko 
3836e8e7d4cSIgor Kovalenko     switch ((tlb->tte >> 61) & 3) {
3843475187dSbellard     default:
38583469015Sbellard     case 0x0: // 8k
3863475187dSbellard         mask = 0xffffffffffffe000ULL;
3873475187dSbellard         break;
38883469015Sbellard     case 0x1: // 64k
3893475187dSbellard         mask = 0xffffffffffff0000ULL;
3903475187dSbellard         break;
39183469015Sbellard     case 0x2: // 512k
3923475187dSbellard         mask = 0xfffffffffff80000ULL;
3933475187dSbellard         break;
39483469015Sbellard     case 0x3: // 4M
3953475187dSbellard         mask = 0xffffffffffc00000ULL;
3963475187dSbellard         break;
3973475187dSbellard     }
398536ba015SIgor Kovalenko 
399536ba015SIgor Kovalenko     // valid, context match, virtual address match?
400f707726eSIgor Kovalenko     if (TTE_IS_VALID(tlb->tte) &&
401299b520cSIgor V. Kovalenko         (TTE_IS_GLOBAL(tlb->tte) || tlb_compare_context(tlb, context))
4022a90358fSBlue Swirl         && compare_masked(address, tlb->tag, mask))
403536ba015SIgor Kovalenko     {
404536ba015SIgor Kovalenko         // decode physical address
4056e8e7d4cSIgor Kovalenko         *physical = ((tlb->tte & mask) | (address & ~mask)) & 0x1ffffffe000ULL;
406536ba015SIgor Kovalenko         return 1;
407536ba015SIgor Kovalenko     }
408536ba015SIgor Kovalenko 
409536ba015SIgor Kovalenko     return 0;
410536ba015SIgor Kovalenko }
411536ba015SIgor Kovalenko 
412536ba015SIgor Kovalenko static int get_physical_address_data(CPUState *env,
413c227f099SAnthony Liguori                                      target_phys_addr_t *physical, int *prot,
4142065061eSIgor V. Kovalenko                                      target_ulong address, int rw, int mmu_idx)
415536ba015SIgor Kovalenko {
416536ba015SIgor Kovalenko     unsigned int i;
417536ba015SIgor Kovalenko     uint64_t context;
418536ba015SIgor Kovalenko 
4192065061eSIgor V. Kovalenko     int is_user = (mmu_idx == MMU_USER_IDX ||
4202065061eSIgor V. Kovalenko                    mmu_idx == MMU_USER_SECONDARY_IDX);
4212065061eSIgor V. Kovalenko 
422536ba015SIgor Kovalenko     if ((env->lsu & DMMU_E) == 0) { /* DMMU disabled */
423536ba015SIgor Kovalenko         *physical = ultrasparc_truncate_physical(address);
424536ba015SIgor Kovalenko         *prot = PAGE_READ | PAGE_WRITE;
425536ba015SIgor Kovalenko         return 0;
426536ba015SIgor Kovalenko     }
427536ba015SIgor Kovalenko 
4282065061eSIgor V. Kovalenko     switch(mmu_idx) {
4292065061eSIgor V. Kovalenko     case MMU_USER_IDX:
4302065061eSIgor V. Kovalenko     case MMU_KERNEL_IDX:
4316e8e7d4cSIgor Kovalenko         context = env->dmmu.mmu_primary_context & 0x1fff;
4322065061eSIgor V. Kovalenko         break;
4332065061eSIgor V. Kovalenko     case MMU_USER_SECONDARY_IDX:
4342065061eSIgor V. Kovalenko     case MMU_KERNEL_SECONDARY_IDX:
4352065061eSIgor V. Kovalenko         context = env->dmmu.mmu_secondary_context & 0x1fff;
4362065061eSIgor V. Kovalenko         break;
4372065061eSIgor V. Kovalenko     case MMU_NUCLEUS_IDX:
43844505216SBlue Swirl     default:
439299b520cSIgor V. Kovalenko         context = 0;
4402065061eSIgor V. Kovalenko         break;
441299b520cSIgor V. Kovalenko     }
442536ba015SIgor Kovalenko 
443536ba015SIgor Kovalenko     for (i = 0; i < 64; i++) {
444afdf8109Sblueswir1         // ctx match, vaddr match, valid?
445b8e9fc06SIgor V. Kovalenko         if (ultrasparc_tag_match(&env->dtlb[i], address, context, physical)) {
446b8e9fc06SIgor V. Kovalenko 
4476e8e7d4cSIgor Kovalenko             uint8_t fault_type = 0;
4486e8e7d4cSIgor Kovalenko 
449b8e9fc06SIgor V. Kovalenko             // access ok?
4506e8e7d4cSIgor Kovalenko             if ((env->dtlb[i].tte & 0x4) && is_user) {
4516e8e7d4cSIgor Kovalenko                 fault_type |= 1; /* privilege violation */
452b8e9fc06SIgor V. Kovalenko                 env->exception_index = TT_DFAULT;
453b8e9fc06SIgor V. Kovalenko 
454b8e9fc06SIgor V. Kovalenko                 DPRINTF_MMU("DFAULT at %" PRIx64 " context %" PRIx64
455b8e9fc06SIgor V. Kovalenko                             " mmu_idx=%d tl=%d\n",
456b8e9fc06SIgor V. Kovalenko                             address, context, mmu_idx, env->tl);
457b8e9fc06SIgor V. Kovalenko             } else if (!(env->dtlb[i].tte & 0x2) && (rw == 1)) {
458b8e9fc06SIgor V. Kovalenko                 env->exception_index = TT_DPROT;
459b8e9fc06SIgor V. Kovalenko 
460b8e9fc06SIgor V. Kovalenko                 DPRINTF_MMU("DPROT at %" PRIx64 " context %" PRIx64
461b8e9fc06SIgor V. Kovalenko                             " mmu_idx=%d tl=%d\n",
462b8e9fc06SIgor V. Kovalenko                             address, context, mmu_idx, env->tl);
463b8e9fc06SIgor V. Kovalenko             } else {
464b8e9fc06SIgor V. Kovalenko                 *prot = PAGE_READ;
465b8e9fc06SIgor V. Kovalenko                 if (env->dtlb[i].tte & 0x2)
466b8e9fc06SIgor V. Kovalenko                     *prot |= PAGE_WRITE;
467b8e9fc06SIgor V. Kovalenko 
468b8e9fc06SIgor V. Kovalenko                 TTE_SET_USED(env->dtlb[i].tte);
469b8e9fc06SIgor V. Kovalenko 
470b8e9fc06SIgor V. Kovalenko                 return 0;
4716e8e7d4cSIgor Kovalenko             }
4726e8e7d4cSIgor Kovalenko 
4736e8e7d4cSIgor Kovalenko             if (env->dmmu.sfsr & 1) /* Fault status register */
4746e8e7d4cSIgor Kovalenko                 env->dmmu.sfsr = 2; /* overflow (not read before
47577f193daSblueswir1                                              another fault) */
4766e8e7d4cSIgor Kovalenko 
4776e8e7d4cSIgor Kovalenko             env->dmmu.sfsr |= (is_user << 3) | ((rw == 1) << 2) | 1;
4786e8e7d4cSIgor Kovalenko 
4796e8e7d4cSIgor Kovalenko             env->dmmu.sfsr |= (fault_type << 7);
4806e8e7d4cSIgor Kovalenko 
4816e8e7d4cSIgor Kovalenko             env->dmmu.sfar = address; /* Fault address register */
4829168b3a5SIgor V. Kovalenko 
4839168b3a5SIgor V. Kovalenko             env->dmmu.tag_access = (address & ~0x1fffULL) | context;
4849168b3a5SIgor V. Kovalenko 
4853475187dSbellard             return 1;
4863475187dSbellard         }
4873475187dSbellard     }
488b8e9fc06SIgor V. Kovalenko 
489b8e9fc06SIgor V. Kovalenko     DPRINTF_MMU("DMISS at %" PRIx64 " context %" PRIx64 "\n",
490b8e9fc06SIgor V. Kovalenko                 address, context);
491b8e9fc06SIgor V. Kovalenko 
4926e8e7d4cSIgor Kovalenko     env->dmmu.tag_access = (address & ~0x1fffULL) | context;
49383469015Sbellard     env->exception_index = TT_DMISS;
4943475187dSbellard     return 1;
4953475187dSbellard }
4963475187dSbellard 
49777f193daSblueswir1 static int get_physical_address_code(CPUState *env,
498c227f099SAnthony Liguori                                      target_phys_addr_t *physical, int *prot,
4992065061eSIgor V. Kovalenko                                      target_ulong address, int mmu_idx)
5003475187dSbellard {
5013475187dSbellard     unsigned int i;
502536ba015SIgor Kovalenko     uint64_t context;
5033475187dSbellard 
5042065061eSIgor V. Kovalenko     int is_user = (mmu_idx == MMU_USER_IDX ||
5052065061eSIgor V. Kovalenko                    mmu_idx == MMU_USER_SECONDARY_IDX);
5062065061eSIgor V. Kovalenko 
507e8807b14SIgor Kovalenko     if ((env->lsu & IMMU_E) == 0 || (env->pstate & PS_RED) != 0) {
508e8807b14SIgor Kovalenko         /* IMMU disabled */
509e8807b14SIgor Kovalenko         *physical = ultrasparc_truncate_physical(address);
510227671c9Sbellard         *prot = PAGE_EXEC;
5113475187dSbellard         return 0;
5123475187dSbellard     }
51383469015Sbellard 
514299b520cSIgor V. Kovalenko     if (env->tl == 0) {
5152065061eSIgor V. Kovalenko         /* PRIMARY context */
5166e8e7d4cSIgor Kovalenko         context = env->dmmu.mmu_primary_context & 0x1fff;
517299b520cSIgor V. Kovalenko     } else {
5182065061eSIgor V. Kovalenko         /* NUCLEUS context */
519299b520cSIgor V. Kovalenko         context = 0;
520299b520cSIgor V. Kovalenko     }
521536ba015SIgor Kovalenko 
5223475187dSbellard     for (i = 0; i < 64; i++) {
523afdf8109Sblueswir1         // ctx match, vaddr match, valid?
5246e8e7d4cSIgor Kovalenko         if (ultrasparc_tag_match(&env->itlb[i],
525299b520cSIgor V. Kovalenko                                  address, context, physical)) {
526afdf8109Sblueswir1             // access ok?
5276e8e7d4cSIgor Kovalenko             if ((env->itlb[i].tte & 0x4) && is_user) {
5286e8e7d4cSIgor Kovalenko                 if (env->immu.sfsr) /* Fault status register */
5296e8e7d4cSIgor Kovalenko                     env->immu.sfsr = 2; /* overflow (not read before
53077f193daSblueswir1                                              another fault) */
5316e8e7d4cSIgor Kovalenko                 env->immu.sfsr |= (is_user << 3) | 1;
5323475187dSbellard                 env->exception_index = TT_TFAULT;
533b8e9fc06SIgor V. Kovalenko 
5349168b3a5SIgor V. Kovalenko                 env->immu.tag_access = (address & ~0x1fffULL) | context;
5359168b3a5SIgor V. Kovalenko 
536b8e9fc06SIgor V. Kovalenko                 DPRINTF_MMU("TFAULT at %" PRIx64 " context %" PRIx64 "\n",
537b8e9fc06SIgor V. Kovalenko                             address, context);
538b8e9fc06SIgor V. Kovalenko 
5393475187dSbellard                 return 1;
5403475187dSbellard             }
541227671c9Sbellard             *prot = PAGE_EXEC;
542f707726eSIgor Kovalenko             TTE_SET_USED(env->itlb[i].tte);
5433475187dSbellard             return 0;
5443475187dSbellard         }
5453475187dSbellard     }
546b8e9fc06SIgor V. Kovalenko 
547b8e9fc06SIgor V. Kovalenko     DPRINTF_MMU("TMISS at %" PRIx64 " context %" PRIx64 "\n",
548b8e9fc06SIgor V. Kovalenko                 address, context);
549b8e9fc06SIgor V. Kovalenko 
5507ab463cbSBlue Swirl     /* Context is stored in DMMU (dmmuregs[1]) also for IMMU */
5516e8e7d4cSIgor Kovalenko     env->immu.tag_access = (address & ~0x1fffULL) | context;
55283469015Sbellard     env->exception_index = TT_TMISS;
5533475187dSbellard     return 1;
5543475187dSbellard }
5553475187dSbellard 
556c227f099SAnthony Liguori static int get_physical_address(CPUState *env, target_phys_addr_t *physical,
557c48fcb47Sblueswir1                                 int *prot, int *access_index,
558d4c430a8SPaul Brook                                 target_ulong address, int rw, int mmu_idx,
559d4c430a8SPaul Brook                                 target_ulong *page_size)
5603475187dSbellard {
561d4c430a8SPaul Brook     /* ??? We treat everything as a small page, then explicitly flush
562d4c430a8SPaul Brook        everything when an entry is evicted.  */
563d4c430a8SPaul Brook     *page_size = TARGET_PAGE_SIZE;
5649fd1ae3aSIgor V. Kovalenko 
5659fd1ae3aSIgor V. Kovalenko #if defined (DEBUG_MMU)
5669fd1ae3aSIgor V. Kovalenko     /* safety net to catch wrong softmmu index use from dynamic code */
5679fd1ae3aSIgor V. Kovalenko     if (env->tl > 0 && mmu_idx != MMU_NUCLEUS_IDX) {
5689fd1ae3aSIgor V. Kovalenko         DPRINTF_MMU("get_physical_address %s tl=%d mmu_idx=%d"
5699fd1ae3aSIgor V. Kovalenko                     " primary context=%" PRIx64
5709fd1ae3aSIgor V. Kovalenko                     " secondary context=%" PRIx64
5719fd1ae3aSIgor V. Kovalenko                 " address=%" PRIx64
5729fd1ae3aSIgor V. Kovalenko                 "\n",
5739fd1ae3aSIgor V. Kovalenko                 (rw == 2 ? "CODE" : "DATA"),
5749fd1ae3aSIgor V. Kovalenko                 env->tl, mmu_idx,
5759fd1ae3aSIgor V. Kovalenko                 env->dmmu.mmu_primary_context,
5769fd1ae3aSIgor V. Kovalenko                 env->dmmu.mmu_secondary_context,
5779fd1ae3aSIgor V. Kovalenko                 address);
5789fd1ae3aSIgor V. Kovalenko     }
5799fd1ae3aSIgor V. Kovalenko #endif
5809fd1ae3aSIgor V. Kovalenko 
5813475187dSbellard     if (rw == 2)
58222548760Sblueswir1         return get_physical_address_code(env, physical, prot, address,
5832065061eSIgor V. Kovalenko                                          mmu_idx);
5843475187dSbellard     else
58522548760Sblueswir1         return get_physical_address_data(env, physical, prot, address, rw,
5862065061eSIgor V. Kovalenko                                          mmu_idx);
5873475187dSbellard }
5883475187dSbellard 
5893475187dSbellard /* Perform address translation */
5903475187dSbellard int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
5916ebbf390Sj_mayer                               int mmu_idx, int is_softmmu)
5923475187dSbellard {
59383469015Sbellard     target_ulong virt_addr, vaddr;
594c227f099SAnthony Liguori     target_phys_addr_t paddr;
595d4c430a8SPaul Brook     target_ulong page_size;
596d4c430a8SPaul Brook     int error_code = 0, prot, access_index;
5973475187dSbellard 
59877f193daSblueswir1     error_code = get_physical_address(env, &paddr, &prot, &access_index,
599d4c430a8SPaul Brook                                       address, rw, mmu_idx, &page_size);
6003475187dSbellard     if (error_code == 0) {
6013475187dSbellard         virt_addr = address & TARGET_PAGE_MASK;
60277f193daSblueswir1         vaddr = virt_addr + ((address & TARGET_PAGE_MASK) &
60377f193daSblueswir1                              (TARGET_PAGE_SIZE - 1));
604b8e9fc06SIgor V. Kovalenko 
605b8e9fc06SIgor V. Kovalenko         DPRINTF_MMU("Translate at %" PRIx64 " -> %" PRIx64 ","
606b8e9fc06SIgor V. Kovalenko                     " vaddr %" PRIx64
607b8e9fc06SIgor V. Kovalenko                     " mmu_idx=%d"
608b8e9fc06SIgor V. Kovalenko                     " tl=%d"
609b8e9fc06SIgor V. Kovalenko                     " primary context=%" PRIx64
610b8e9fc06SIgor V. Kovalenko                     " secondary context=%" PRIx64
611b8e9fc06SIgor V. Kovalenko                     "\n",
612b8e9fc06SIgor V. Kovalenko                     address, paddr, vaddr, mmu_idx, env->tl,
613b8e9fc06SIgor V. Kovalenko                     env->dmmu.mmu_primary_context,
614b8e9fc06SIgor V. Kovalenko                     env->dmmu.mmu_secondary_context);
615b8e9fc06SIgor V. Kovalenko 
616d4c430a8SPaul Brook         tlb_set_page(env, vaddr, paddr, prot, mmu_idx, page_size);
617d4c430a8SPaul Brook         return 0;
6183475187dSbellard     }
6193475187dSbellard     // XXX
6203475187dSbellard     return 1;
6213475187dSbellard }
6223475187dSbellard 
623d41160a3SBlue Swirl void dump_mmu(FILE *f, fprintf_function cpu_fprintf, CPUState *env)
62483469015Sbellard {
62583469015Sbellard     unsigned int i;
62683469015Sbellard     const char *mask;
62783469015Sbellard 
628d41160a3SBlue Swirl     (*cpu_fprintf)(f, "MMU contexts: Primary: %" PRId64 ", Secondary: %"
629d41160a3SBlue Swirl                    PRId64 "\n",
630d41160a3SBlue Swirl                    env->dmmu.mmu_primary_context,
631d41160a3SBlue Swirl                    env->dmmu.mmu_secondary_context);
63283469015Sbellard     if ((env->lsu & DMMU_E) == 0) {
633d41160a3SBlue Swirl         (*cpu_fprintf)(f, "DMMU disabled\n");
63483469015Sbellard     } else {
635d41160a3SBlue Swirl         (*cpu_fprintf)(f, "DMMU dump\n");
63683469015Sbellard         for (i = 0; i < 64; i++) {
63731a68d57SBlue Swirl             switch ((env->dtlb[i].tte >> 61) & 3) {
63883469015Sbellard             default:
63983469015Sbellard             case 0x0:
64083469015Sbellard                 mask = "  8k";
64183469015Sbellard                 break;
64283469015Sbellard             case 0x1:
64383469015Sbellard                 mask = " 64k";
64483469015Sbellard                 break;
64583469015Sbellard             case 0x2:
64683469015Sbellard                 mask = "512k";
64783469015Sbellard                 break;
64883469015Sbellard             case 0x3:
64983469015Sbellard                 mask = "  4M";
65083469015Sbellard                 break;
65183469015Sbellard             }
65231a68d57SBlue Swirl             if ((env->dtlb[i].tte & 0x8000000000000000ULL) != 0) {
653d41160a3SBlue Swirl                 (*cpu_fprintf)(f, "[%02u] VA: %" PRIx64 ", PA: %" PRIx64
6542a90358fSBlue Swirl                                ", %s, %s, %s, %s, ctx %" PRId64 " %s\n",
6556e8e7d4cSIgor Kovalenko                                i,
65631a68d57SBlue Swirl                                env->dtlb[i].tag & (uint64_t)~0x1fffULL,
65731a68d57SBlue Swirl                                env->dtlb[i].tte & (uint64_t)0x1ffffffe000ULL,
65883469015Sbellard                                mask,
65931a68d57SBlue Swirl                                env->dtlb[i].tte & 0x4? "priv": "user",
66031a68d57SBlue Swirl                                env->dtlb[i].tte & 0x2? "RW": "RO",
66131a68d57SBlue Swirl                                env->dtlb[i].tte & 0x40? "locked": "unlocked",
6622a90358fSBlue Swirl                                env->dtlb[i].tag & (uint64_t)0x1fffULL,
663d41160a3SBlue Swirl                                TTE_IS_GLOBAL(env->dtlb[i].tte)?
664d41160a3SBlue Swirl                                "global" : "local");
66583469015Sbellard             }
66683469015Sbellard         }
66783469015Sbellard     }
66883469015Sbellard     if ((env->lsu & IMMU_E) == 0) {
669d41160a3SBlue Swirl         (*cpu_fprintf)(f, "IMMU disabled\n");
67083469015Sbellard     } else {
671d41160a3SBlue Swirl         (*cpu_fprintf)(f, "IMMU dump\n");
67283469015Sbellard         for (i = 0; i < 64; i++) {
67331a68d57SBlue Swirl             switch ((env->itlb[i].tte >> 61) & 3) {
67483469015Sbellard             default:
67583469015Sbellard             case 0x0:
67683469015Sbellard                 mask = "  8k";
67783469015Sbellard                 break;
67883469015Sbellard             case 0x1:
67983469015Sbellard                 mask = " 64k";
68083469015Sbellard                 break;
68183469015Sbellard             case 0x2:
68283469015Sbellard                 mask = "512k";
68383469015Sbellard                 break;
68483469015Sbellard             case 0x3:
68583469015Sbellard                 mask = "  4M";
68683469015Sbellard                 break;
68783469015Sbellard             }
68831a68d57SBlue Swirl             if ((env->itlb[i].tte & 0x8000000000000000ULL) != 0) {
689d41160a3SBlue Swirl                 (*cpu_fprintf)(f, "[%02u] VA: %" PRIx64 ", PA: %" PRIx64
6902a90358fSBlue Swirl                                ", %s, %s, %s, ctx %" PRId64 " %s\n",
6916e8e7d4cSIgor Kovalenko                                i,
6926e8e7d4cSIgor Kovalenko                                env->itlb[i].tag & (uint64_t)~0x1fffULL,
69331a68d57SBlue Swirl                                env->itlb[i].tte & (uint64_t)0x1ffffffe000ULL,
69483469015Sbellard                                mask,
69531a68d57SBlue Swirl                                env->itlb[i].tte & 0x4? "priv": "user",
69631a68d57SBlue Swirl                                env->itlb[i].tte & 0x40? "locked": "unlocked",
6972a90358fSBlue Swirl                                env->itlb[i].tag & (uint64_t)0x1fffULL,
698d41160a3SBlue Swirl                                TTE_IS_GLOBAL(env->itlb[i].tte)?
699d41160a3SBlue Swirl                                "global" : "local");
70083469015Sbellard             }
70183469015Sbellard         }
70283469015Sbellard     }
70383469015Sbellard }
70424741ef3Sbellard 
70524741ef3Sbellard #endif /* TARGET_SPARC64 */
70624741ef3Sbellard #endif /* !CONFIG_USER_ONLY */
70724741ef3Sbellard 
708c48fcb47Sblueswir1 
7094fcc562bSPaul Brook #if !defined(CONFIG_USER_ONLY)
7102065061eSIgor V. Kovalenko target_phys_addr_t cpu_get_phys_page_nofault(CPUState *env, target_ulong addr,
7112065061eSIgor V. Kovalenko                                            int mmu_idx)
712c48fcb47Sblueswir1 {
713c227f099SAnthony Liguori     target_phys_addr_t phys_addr;
714d4c430a8SPaul Brook     target_ulong page_size;
715c48fcb47Sblueswir1     int prot, access_index;
716c48fcb47Sblueswir1 
717c48fcb47Sblueswir1     if (get_physical_address(env, &phys_addr, &prot, &access_index, addr, 2,
7182065061eSIgor V. Kovalenko                              mmu_idx, &page_size) != 0)
719c48fcb47Sblueswir1         if (get_physical_address(env, &phys_addr, &prot, &access_index, addr,
7202065061eSIgor V. Kovalenko                                  0, mmu_idx, &page_size) != 0)
721c48fcb47Sblueswir1             return -1;
722c48fcb47Sblueswir1     if (cpu_get_physical_page_desc(phys_addr) == IO_MEM_UNASSIGNED)
723c48fcb47Sblueswir1         return -1;
724c48fcb47Sblueswir1     return phys_addr;
725c48fcb47Sblueswir1 }
7262065061eSIgor V. Kovalenko 
7272065061eSIgor V. Kovalenko target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
7282065061eSIgor V. Kovalenko {
7299fd1ae3aSIgor V. Kovalenko     return cpu_get_phys_page_nofault(env, addr, cpu_mmu_index(env));
7302065061eSIgor V. Kovalenko }
731c48fcb47Sblueswir1 #endif
732c48fcb47Sblueswir1 
733c48fcb47Sblueswir1 void cpu_reset(CPUSPARCState *env)
734c48fcb47Sblueswir1 {
735eca1bdf4Saliguori     if (qemu_loglevel_mask(CPU_LOG_RESET)) {
736eca1bdf4Saliguori         qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
737eca1bdf4Saliguori         log_cpu_state(env, 0);
738eca1bdf4Saliguori     }
739eca1bdf4Saliguori 
740c48fcb47Sblueswir1     tlb_flush(env, 1);
741c48fcb47Sblueswir1     env->cwp = 0;
7425210977aSIgor Kovalenko #ifndef TARGET_SPARC64
743c48fcb47Sblueswir1     env->wim = 1;
7445210977aSIgor Kovalenko #endif
745c48fcb47Sblueswir1     env->regwptr = env->regbase + (env->cwp * 16);
7466b743278SBlue Swirl     CC_OP = CC_OP_FLAGS;
747c48fcb47Sblueswir1 #if defined(CONFIG_USER_ONLY)
748c48fcb47Sblueswir1 #ifdef TARGET_SPARC64
7491a14026eSblueswir1     env->cleanwin = env->nwindows - 2;
7501a14026eSblueswir1     env->cansave = env->nwindows - 2;
751c48fcb47Sblueswir1     env->pstate = PS_RMO | PS_PEF | PS_IE;
752c48fcb47Sblueswir1     env->asi = 0x82; // Primary no-fault
753c48fcb47Sblueswir1 #endif
754c48fcb47Sblueswir1 #else
7555210977aSIgor Kovalenko #if !defined(TARGET_SPARC64)
756c48fcb47Sblueswir1     env->psret = 0;
757c48fcb47Sblueswir1     env->psrs = 1;
758c48fcb47Sblueswir1     env->psrps = 1;
7592aae2b8eSIgor V. Kovalenko #endif
760c48fcb47Sblueswir1 #ifdef TARGET_SPARC64
7618194f35aSIgor Kovalenko     env->pstate = PS_PRIV|PS_RED|PS_PEF|PS_AG;
7622aae2b8eSIgor V. Kovalenko     env->hpstate = cpu_has_hypervisor(env) ? HS_PRIV : 0;
7638194f35aSIgor Kovalenko     env->tl = env->maxtl;
7648194f35aSIgor Kovalenko     cpu_tsptr(env)->tt = TT_POWER_ON_RESET;
765415fc906Sblueswir1     env->lsu = 0;
766c48fcb47Sblueswir1 #else
767c48fcb47Sblueswir1     env->mmuregs[0] &= ~(MMU_E | MMU_NF);
7685578ceabSblueswir1     env->mmuregs[0] |= env->def->mmu_bm;
769c48fcb47Sblueswir1 #endif
770e87231d4Sblueswir1     env->pc = 0;
771c48fcb47Sblueswir1     env->npc = env->pc + 4;
772c48fcb47Sblueswir1 #endif
773b04d9890SFabien Chouteau     env->cache_control = 0;
774c48fcb47Sblueswir1 }
775c48fcb47Sblueswir1 
77664a88d5dSblueswir1 static int cpu_sparc_register(CPUSPARCState *env, const char *cpu_model)
777c48fcb47Sblueswir1 {
77864a88d5dSblueswir1     sparc_def_t def1, *def = &def1;
779c48fcb47Sblueswir1 
78064a88d5dSblueswir1     if (cpu_sparc_find_by_name(def, cpu_model) < 0)
78164a88d5dSblueswir1         return -1;
782c48fcb47Sblueswir1 
7835578ceabSblueswir1     env->def = qemu_mallocz(sizeof(*def));
7845578ceabSblueswir1     memcpy(env->def, def, sizeof(*def));
7855578ceabSblueswir1 #if defined(CONFIG_USER_ONLY)
7865578ceabSblueswir1     if ((env->def->features & CPU_FEATURE_FLOAT))
7875578ceabSblueswir1         env->def->features |= CPU_FEATURE_FLOAT128;
7885578ceabSblueswir1 #endif
789c48fcb47Sblueswir1     env->cpu_model_str = cpu_model;
790c48fcb47Sblueswir1     env->version = def->iu_version;
791c48fcb47Sblueswir1     env->fsr = def->fpu_version;
7921a14026eSblueswir1     env->nwindows = def->nwindows;
793c48fcb47Sblueswir1 #if !defined(TARGET_SPARC64)
794c48fcb47Sblueswir1     env->mmuregs[0] |= def->mmu_version;
795c48fcb47Sblueswir1     cpu_sparc_set_id(env, 0);
796963262deSblueswir1     env->mxccregs[7] |= def->mxcc_version;
7971a14026eSblueswir1 #else
798fb79ceb9Sblueswir1     env->mmu_version = def->mmu_version;
799c19148bdSblueswir1     env->maxtl = def->maxtl;
800c19148bdSblueswir1     env->version |= def->maxtl << 8;
8011a14026eSblueswir1     env->version |= def->nwindows - 1;
802c48fcb47Sblueswir1 #endif
80364a88d5dSblueswir1     return 0;
80464a88d5dSblueswir1 }
80564a88d5dSblueswir1 
80664a88d5dSblueswir1 static void cpu_sparc_close(CPUSPARCState *env)
80764a88d5dSblueswir1 {
8085578ceabSblueswir1     free(env->def);
80964a88d5dSblueswir1     free(env);
81064a88d5dSblueswir1 }
81164a88d5dSblueswir1 
81264a88d5dSblueswir1 CPUSPARCState *cpu_sparc_init(const char *cpu_model)
81364a88d5dSblueswir1 {
81464a88d5dSblueswir1     CPUSPARCState *env;
81564a88d5dSblueswir1 
81664a88d5dSblueswir1     env = qemu_mallocz(sizeof(CPUSPARCState));
81764a88d5dSblueswir1     cpu_exec_init(env);
818c48fcb47Sblueswir1 
819c48fcb47Sblueswir1     gen_intermediate_code_init(env);
820c48fcb47Sblueswir1 
82164a88d5dSblueswir1     if (cpu_sparc_register(env, cpu_model) < 0) {
82264a88d5dSblueswir1         cpu_sparc_close(env);
82364a88d5dSblueswir1         return NULL;
82464a88d5dSblueswir1     }
8250bf46a40Saliguori     qemu_init_vcpu(env);
826c48fcb47Sblueswir1 
827c48fcb47Sblueswir1     return env;
828c48fcb47Sblueswir1 }
829c48fcb47Sblueswir1 
830c48fcb47Sblueswir1 void cpu_sparc_set_id(CPUSPARCState *env, unsigned int cpu)
831c48fcb47Sblueswir1 {
832c48fcb47Sblueswir1 #if !defined(TARGET_SPARC64)
833c48fcb47Sblueswir1     env->mxccregs[7] = ((cpu + 8) & 0xf) << 24;
834c48fcb47Sblueswir1 #endif
835c48fcb47Sblueswir1 }
836c48fcb47Sblueswir1 
837c48fcb47Sblueswir1 static const sparc_def_t sparc_defs[] = {
838c48fcb47Sblueswir1 #ifdef TARGET_SPARC64
839c48fcb47Sblueswir1     {
840c48fcb47Sblueswir1         .name = "Fujitsu Sparc64",
841c19148bdSblueswir1         .iu_version = ((0x04ULL << 48) | (0x02ULL << 32) | (0ULL << 24)),
842c48fcb47Sblueswir1         .fpu_version = 0x00000000,
843fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
8441a14026eSblueswir1         .nwindows = 4,
845c19148bdSblueswir1         .maxtl = 4,
84664a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
847c48fcb47Sblueswir1     },
848c48fcb47Sblueswir1     {
849c48fcb47Sblueswir1         .name = "Fujitsu Sparc64 III",
850c19148bdSblueswir1         .iu_version = ((0x04ULL << 48) | (0x03ULL << 32) | (0ULL << 24)),
851c48fcb47Sblueswir1         .fpu_version = 0x00000000,
852fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
8531a14026eSblueswir1         .nwindows = 5,
854c19148bdSblueswir1         .maxtl = 4,
85564a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
856c48fcb47Sblueswir1     },
857c48fcb47Sblueswir1     {
858c48fcb47Sblueswir1         .name = "Fujitsu Sparc64 IV",
859c19148bdSblueswir1         .iu_version = ((0x04ULL << 48) | (0x04ULL << 32) | (0ULL << 24)),
860c48fcb47Sblueswir1         .fpu_version = 0x00000000,
861fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
8621a14026eSblueswir1         .nwindows = 8,
863c19148bdSblueswir1         .maxtl = 5,
86464a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
865c48fcb47Sblueswir1     },
866c48fcb47Sblueswir1     {
867c48fcb47Sblueswir1         .name = "Fujitsu Sparc64 V",
868c19148bdSblueswir1         .iu_version = ((0x04ULL << 48) | (0x05ULL << 32) | (0x51ULL << 24)),
869c48fcb47Sblueswir1         .fpu_version = 0x00000000,
870fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
8711a14026eSblueswir1         .nwindows = 8,
872c19148bdSblueswir1         .maxtl = 5,
87364a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
874c48fcb47Sblueswir1     },
875c48fcb47Sblueswir1     {
876c48fcb47Sblueswir1         .name = "TI UltraSparc I",
877c19148bdSblueswir1         .iu_version = ((0x17ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
878c48fcb47Sblueswir1         .fpu_version = 0x00000000,
879fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
8801a14026eSblueswir1         .nwindows = 8,
881c19148bdSblueswir1         .maxtl = 5,
88264a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
883c48fcb47Sblueswir1     },
884c48fcb47Sblueswir1     {
885c48fcb47Sblueswir1         .name = "TI UltraSparc II",
886c19148bdSblueswir1         .iu_version = ((0x17ULL << 48) | (0x11ULL << 32) | (0x20ULL << 24)),
887c48fcb47Sblueswir1         .fpu_version = 0x00000000,
888fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
8891a14026eSblueswir1         .nwindows = 8,
890c19148bdSblueswir1         .maxtl = 5,
89164a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
892c48fcb47Sblueswir1     },
893c48fcb47Sblueswir1     {
894c48fcb47Sblueswir1         .name = "TI UltraSparc IIi",
895c19148bdSblueswir1         .iu_version = ((0x17ULL << 48) | (0x12ULL << 32) | (0x91ULL << 24)),
896c48fcb47Sblueswir1         .fpu_version = 0x00000000,
897fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
8981a14026eSblueswir1         .nwindows = 8,
899c19148bdSblueswir1         .maxtl = 5,
90064a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
901c48fcb47Sblueswir1     },
902c48fcb47Sblueswir1     {
903c48fcb47Sblueswir1         .name = "TI UltraSparc IIe",
904c19148bdSblueswir1         .iu_version = ((0x17ULL << 48) | (0x13ULL << 32) | (0x14ULL << 24)),
905c48fcb47Sblueswir1         .fpu_version = 0x00000000,
906fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
9071a14026eSblueswir1         .nwindows = 8,
908c19148bdSblueswir1         .maxtl = 5,
90964a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
910c48fcb47Sblueswir1     },
911c48fcb47Sblueswir1     {
912c48fcb47Sblueswir1         .name = "Sun UltraSparc III",
913c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x14ULL << 32) | (0x34ULL << 24)),
914c48fcb47Sblueswir1         .fpu_version = 0x00000000,
915fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
9161a14026eSblueswir1         .nwindows = 8,
917c19148bdSblueswir1         .maxtl = 5,
91864a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
919c48fcb47Sblueswir1     },
920c48fcb47Sblueswir1     {
921c48fcb47Sblueswir1         .name = "Sun UltraSparc III Cu",
922c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x15ULL << 32) | (0x41ULL << 24)),
923c48fcb47Sblueswir1         .fpu_version = 0x00000000,
924fb79ceb9Sblueswir1         .mmu_version = mmu_us_3,
9251a14026eSblueswir1         .nwindows = 8,
926c19148bdSblueswir1         .maxtl = 5,
92764a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
928c48fcb47Sblueswir1     },
929c48fcb47Sblueswir1     {
930c48fcb47Sblueswir1         .name = "Sun UltraSparc IIIi",
931c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x16ULL << 32) | (0x34ULL << 24)),
932c48fcb47Sblueswir1         .fpu_version = 0x00000000,
933fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
9341a14026eSblueswir1         .nwindows = 8,
935c19148bdSblueswir1         .maxtl = 5,
93664a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
937c48fcb47Sblueswir1     },
938c48fcb47Sblueswir1     {
939c48fcb47Sblueswir1         .name = "Sun UltraSparc IV",
940c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x18ULL << 32) | (0x31ULL << 24)),
941c48fcb47Sblueswir1         .fpu_version = 0x00000000,
942fb79ceb9Sblueswir1         .mmu_version = mmu_us_4,
9431a14026eSblueswir1         .nwindows = 8,
944c19148bdSblueswir1         .maxtl = 5,
94564a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
946c48fcb47Sblueswir1     },
947c48fcb47Sblueswir1     {
948c48fcb47Sblueswir1         .name = "Sun UltraSparc IV+",
949c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x19ULL << 32) | (0x22ULL << 24)),
950c48fcb47Sblueswir1         .fpu_version = 0x00000000,
951fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
9521a14026eSblueswir1         .nwindows = 8,
953c19148bdSblueswir1         .maxtl = 5,
954fb79ceb9Sblueswir1         .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_CMT,
955c48fcb47Sblueswir1     },
956c48fcb47Sblueswir1     {
957c48fcb47Sblueswir1         .name = "Sun UltraSparc IIIi+",
958c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x22ULL << 32) | (0ULL << 24)),
959c48fcb47Sblueswir1         .fpu_version = 0x00000000,
960fb79ceb9Sblueswir1         .mmu_version = mmu_us_3,
9611a14026eSblueswir1         .nwindows = 8,
962c19148bdSblueswir1         .maxtl = 5,
96364a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
964c48fcb47Sblueswir1     },
965c48fcb47Sblueswir1     {
966c7ba218dSblueswir1         .name = "Sun UltraSparc T1",
967c7ba218dSblueswir1         // defined in sparc_ifu_fdp.v and ctu.h
968c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x23ULL << 32) | (0x02ULL << 24)),
969c7ba218dSblueswir1         .fpu_version = 0x00000000,
970c7ba218dSblueswir1         .mmu_version = mmu_sun4v,
971c7ba218dSblueswir1         .nwindows = 8,
972c19148bdSblueswir1         .maxtl = 6,
973c7ba218dSblueswir1         .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_HYPV | CPU_FEATURE_CMT
974c7ba218dSblueswir1         | CPU_FEATURE_GL,
975c7ba218dSblueswir1     },
976c7ba218dSblueswir1     {
977c7ba218dSblueswir1         .name = "Sun UltraSparc T2",
978c7ba218dSblueswir1         // defined in tlu_asi_ctl.v and n2_revid_cust.v
979c19148bdSblueswir1         .iu_version = ((0x3eULL << 48) | (0x24ULL << 32) | (0x02ULL << 24)),
980c7ba218dSblueswir1         .fpu_version = 0x00000000,
981c7ba218dSblueswir1         .mmu_version = mmu_sun4v,
982c7ba218dSblueswir1         .nwindows = 8,
983c19148bdSblueswir1         .maxtl = 6,
984c7ba218dSblueswir1         .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_HYPV | CPU_FEATURE_CMT
985c7ba218dSblueswir1         | CPU_FEATURE_GL,
986c7ba218dSblueswir1     },
987c7ba218dSblueswir1     {
988c48fcb47Sblueswir1         .name = "NEC UltraSparc I",
989c19148bdSblueswir1         .iu_version = ((0x22ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
990c48fcb47Sblueswir1         .fpu_version = 0x00000000,
991fb79ceb9Sblueswir1         .mmu_version = mmu_us_12,
9921a14026eSblueswir1         .nwindows = 8,
993c19148bdSblueswir1         .maxtl = 5,
99464a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
995c48fcb47Sblueswir1     },
996c48fcb47Sblueswir1 #else
997c48fcb47Sblueswir1     {
998c48fcb47Sblueswir1         .name = "Fujitsu MB86900",
999c48fcb47Sblueswir1         .iu_version = 0x00 << 24, /* Impl 0, ver 0 */
1000c48fcb47Sblueswir1         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1001c48fcb47Sblueswir1         .mmu_version = 0x00 << 24, /* Impl 0, ver 0 */
1002c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1003c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1004c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1005c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1006c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
10071a14026eSblueswir1         .nwindows = 7,
1008e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_FSMULD,
1009c48fcb47Sblueswir1     },
1010c48fcb47Sblueswir1     {
1011c48fcb47Sblueswir1         .name = "Fujitsu MB86904",
1012c48fcb47Sblueswir1         .iu_version = 0x04 << 24, /* Impl 0, ver 4 */
1013c48fcb47Sblueswir1         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1014c48fcb47Sblueswir1         .mmu_version = 0x04 << 24, /* Impl 0, ver 4 */
1015c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1016c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x00ffffc0,
1017c48fcb47Sblueswir1         .mmu_cxr_mask = 0x000000ff,
1018c48fcb47Sblueswir1         .mmu_sfsr_mask = 0x00016fff,
1019c48fcb47Sblueswir1         .mmu_trcr_mask = 0x00ffffff,
10201a14026eSblueswir1         .nwindows = 8,
102164a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1022c48fcb47Sblueswir1     },
1023c48fcb47Sblueswir1     {
1024c48fcb47Sblueswir1         .name = "Fujitsu MB86907",
1025c48fcb47Sblueswir1         .iu_version = 0x05 << 24, /* Impl 0, ver 5 */
1026c48fcb47Sblueswir1         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1027c48fcb47Sblueswir1         .mmu_version = 0x05 << 24, /* Impl 0, ver 5 */
1028c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1029c48fcb47Sblueswir1         .mmu_ctpr_mask = 0xffffffc0,
1030c48fcb47Sblueswir1         .mmu_cxr_mask = 0x000000ff,
1031c48fcb47Sblueswir1         .mmu_sfsr_mask = 0x00016fff,
1032c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
10331a14026eSblueswir1         .nwindows = 8,
103464a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1035c48fcb47Sblueswir1     },
1036c48fcb47Sblueswir1     {
1037c48fcb47Sblueswir1         .name = "LSI L64811",
1038c48fcb47Sblueswir1         .iu_version = 0x10 << 24, /* Impl 1, ver 0 */
1039c48fcb47Sblueswir1         .fpu_version = 1 << 17, /* FPU version 1 (LSI L64814) */
1040c48fcb47Sblueswir1         .mmu_version = 0x10 << 24,
1041c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1042c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1043c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1044c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1045c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
10461a14026eSblueswir1         .nwindows = 8,
1047e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
1048e30b4678Sblueswir1         CPU_FEATURE_FSMULD,
1049c48fcb47Sblueswir1     },
1050c48fcb47Sblueswir1     {
1051c48fcb47Sblueswir1         .name = "Cypress CY7C601",
1052c48fcb47Sblueswir1         .iu_version = 0x11 << 24, /* Impl 1, ver 1 */
1053c48fcb47Sblueswir1         .fpu_version = 3 << 17, /* FPU version 3 (Cypress CY7C602) */
1054c48fcb47Sblueswir1         .mmu_version = 0x10 << 24,
1055c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1056c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1057c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1058c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1059c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
10601a14026eSblueswir1         .nwindows = 8,
1061e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
1062e30b4678Sblueswir1         CPU_FEATURE_FSMULD,
1063c48fcb47Sblueswir1     },
1064c48fcb47Sblueswir1     {
1065c48fcb47Sblueswir1         .name = "Cypress CY7C611",
1066c48fcb47Sblueswir1         .iu_version = 0x13 << 24, /* Impl 1, ver 3 */
1067c48fcb47Sblueswir1         .fpu_version = 3 << 17, /* FPU version 3 (Cypress CY7C602) */
1068c48fcb47Sblueswir1         .mmu_version = 0x10 << 24,
1069c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1070c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1071c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1072c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1073c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
10741a14026eSblueswir1         .nwindows = 8,
1075e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
1076e30b4678Sblueswir1         CPU_FEATURE_FSMULD,
1077c48fcb47Sblueswir1     },
1078c48fcb47Sblueswir1     {
1079c48fcb47Sblueswir1         .name = "TI MicroSparc I",
1080c48fcb47Sblueswir1         .iu_version = 0x41000000,
1081c48fcb47Sblueswir1         .fpu_version = 4 << 17,
1082c48fcb47Sblueswir1         .mmu_version = 0x41000000,
1083c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1084c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1085c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1086c48fcb47Sblueswir1         .mmu_sfsr_mask = 0x00016fff,
1087c48fcb47Sblueswir1         .mmu_trcr_mask = 0x0000003f,
10881a14026eSblueswir1         .nwindows = 7,
1089e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_MUL |
1090e30b4678Sblueswir1         CPU_FEATURE_DIV | CPU_FEATURE_FLUSH | CPU_FEATURE_FSQRT |
1091e30b4678Sblueswir1         CPU_FEATURE_FMUL,
1092c48fcb47Sblueswir1     },
1093c48fcb47Sblueswir1     {
1094c48fcb47Sblueswir1         .name = "TI MicroSparc II",
1095c48fcb47Sblueswir1         .iu_version = 0x42000000,
1096c48fcb47Sblueswir1         .fpu_version = 4 << 17,
1097c48fcb47Sblueswir1         .mmu_version = 0x02000000,
1098c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1099c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x00ffffc0,
1100c48fcb47Sblueswir1         .mmu_cxr_mask = 0x000000ff,
1101c48fcb47Sblueswir1         .mmu_sfsr_mask = 0x00016fff,
1102c48fcb47Sblueswir1         .mmu_trcr_mask = 0x00ffffff,
11031a14026eSblueswir1         .nwindows = 8,
110464a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1105c48fcb47Sblueswir1     },
1106c48fcb47Sblueswir1     {
1107c48fcb47Sblueswir1         .name = "TI MicroSparc IIep",
1108c48fcb47Sblueswir1         .iu_version = 0x42000000,
1109c48fcb47Sblueswir1         .fpu_version = 4 << 17,
1110c48fcb47Sblueswir1         .mmu_version = 0x04000000,
1111c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1112c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x00ffffc0,
1113c48fcb47Sblueswir1         .mmu_cxr_mask = 0x000000ff,
1114c48fcb47Sblueswir1         .mmu_sfsr_mask = 0x00016bff,
1115c48fcb47Sblueswir1         .mmu_trcr_mask = 0x00ffffff,
11161a14026eSblueswir1         .nwindows = 8,
111764a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1118c48fcb47Sblueswir1     },
1119c48fcb47Sblueswir1     {
1120b5154bdeSblueswir1         .name = "TI SuperSparc 40", // STP1020NPGA
1121963262deSblueswir1         .iu_version = 0x41000000, // SuperSPARC 2.x
1122b5154bdeSblueswir1         .fpu_version = 0 << 17,
1123963262deSblueswir1         .mmu_version = 0x00000800, // SuperSPARC 2.x, no MXCC
1124b5154bdeSblueswir1         .mmu_bm = 0x00002000,
1125b5154bdeSblueswir1         .mmu_ctpr_mask = 0xffffffc0,
1126b5154bdeSblueswir1         .mmu_cxr_mask = 0x0000ffff,
1127b5154bdeSblueswir1         .mmu_sfsr_mask = 0xffffffff,
1128b5154bdeSblueswir1         .mmu_trcr_mask = 0xffffffff,
11291a14026eSblueswir1         .nwindows = 8,
1130b5154bdeSblueswir1         .features = CPU_DEFAULT_FEATURES,
1131b5154bdeSblueswir1     },
1132b5154bdeSblueswir1     {
1133b5154bdeSblueswir1         .name = "TI SuperSparc 50", // STP1020PGA
1134963262deSblueswir1         .iu_version = 0x40000000, // SuperSPARC 3.x
1135b5154bdeSblueswir1         .fpu_version = 0 << 17,
1136963262deSblueswir1         .mmu_version = 0x01000800, // SuperSPARC 3.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     {
1146c48fcb47Sblueswir1         .name = "TI SuperSparc 51",
1147963262deSblueswir1         .iu_version = 0x40000000, // SuperSPARC 3.x
1148c48fcb47Sblueswir1         .fpu_version = 0 << 17,
1149963262deSblueswir1         .mmu_version = 0x01000000, // SuperSPARC 3.x, MXCC
1150c48fcb47Sblueswir1         .mmu_bm = 0x00002000,
1151c48fcb47Sblueswir1         .mmu_ctpr_mask = 0xffffffc0,
1152c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000ffff,
1153c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1154c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
1155963262deSblueswir1         .mxcc_version = 0x00000104,
11561a14026eSblueswir1         .nwindows = 8,
115764a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1158c48fcb47Sblueswir1     },
1159c48fcb47Sblueswir1     {
1160b5154bdeSblueswir1         .name = "TI SuperSparc 60", // STP1020APGA
1161963262deSblueswir1         .iu_version = 0x40000000, // SuperSPARC 3.x
1162b5154bdeSblueswir1         .fpu_version = 0 << 17,
1163963262deSblueswir1         .mmu_version = 0x01000800, // SuperSPARC 3.x, no MXCC
1164b5154bdeSblueswir1         .mmu_bm = 0x00002000,
1165b5154bdeSblueswir1         .mmu_ctpr_mask = 0xffffffc0,
1166b5154bdeSblueswir1         .mmu_cxr_mask = 0x0000ffff,
1167b5154bdeSblueswir1         .mmu_sfsr_mask = 0xffffffff,
1168b5154bdeSblueswir1         .mmu_trcr_mask = 0xffffffff,
11691a14026eSblueswir1         .nwindows = 8,
1170b5154bdeSblueswir1         .features = CPU_DEFAULT_FEATURES,
1171b5154bdeSblueswir1     },
1172b5154bdeSblueswir1     {
1173c48fcb47Sblueswir1         .name = "TI SuperSparc 61",
1174963262deSblueswir1         .iu_version = 0x44000000, // SuperSPARC 3.x
1175c48fcb47Sblueswir1         .fpu_version = 0 << 17,
1176963262deSblueswir1         .mmu_version = 0x01000000, // SuperSPARC 3.x, MXCC
1177c48fcb47Sblueswir1         .mmu_bm = 0x00002000,
1178c48fcb47Sblueswir1         .mmu_ctpr_mask = 0xffffffc0,
1179c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000ffff,
1180c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1181c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
1182963262deSblueswir1         .mxcc_version = 0x00000104,
1183963262deSblueswir1         .nwindows = 8,
1184963262deSblueswir1         .features = CPU_DEFAULT_FEATURES,
1185963262deSblueswir1     },
1186963262deSblueswir1     {
1187963262deSblueswir1         .name = "TI SuperSparc II",
1188963262deSblueswir1         .iu_version = 0x40000000, // SuperSPARC II 1.x
1189963262deSblueswir1         .fpu_version = 0 << 17,
1190963262deSblueswir1         .mmu_version = 0x08000000, // SuperSPARC II 1.x, MXCC
1191963262deSblueswir1         .mmu_bm = 0x00002000,
1192963262deSblueswir1         .mmu_ctpr_mask = 0xffffffc0,
1193963262deSblueswir1         .mmu_cxr_mask = 0x0000ffff,
1194963262deSblueswir1         .mmu_sfsr_mask = 0xffffffff,
1195963262deSblueswir1         .mmu_trcr_mask = 0xffffffff,
1196963262deSblueswir1         .mxcc_version = 0x00000104,
11971a14026eSblueswir1         .nwindows = 8,
119864a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1199c48fcb47Sblueswir1     },
1200c48fcb47Sblueswir1     {
1201c48fcb47Sblueswir1         .name = "Ross RT625",
1202c48fcb47Sblueswir1         .iu_version = 0x1e000000,
1203c48fcb47Sblueswir1         .fpu_version = 1 << 17,
1204c48fcb47Sblueswir1         .mmu_version = 0x1e000000,
1205c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1206c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1207c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1208c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1209c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
12101a14026eSblueswir1         .nwindows = 8,
121164a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1212c48fcb47Sblueswir1     },
1213c48fcb47Sblueswir1     {
1214c48fcb47Sblueswir1         .name = "Ross RT620",
1215c48fcb47Sblueswir1         .iu_version = 0x1f000000,
1216c48fcb47Sblueswir1         .fpu_version = 1 << 17,
1217c48fcb47Sblueswir1         .mmu_version = 0x1f000000,
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 = "BIT B5010",
1228c48fcb47Sblueswir1         .iu_version = 0x20000000,
1229c48fcb47Sblueswir1         .fpu_version = 0 << 17, /* B5010/B5110/B5120/B5210 */
1230c48fcb47Sblueswir1         .mmu_version = 0x20000000,
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,
1237e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_FSQRT |
1238e30b4678Sblueswir1         CPU_FEATURE_FSMULD,
1239c48fcb47Sblueswir1     },
1240c48fcb47Sblueswir1     {
1241c48fcb47Sblueswir1         .name = "Matsushita MN10501",
1242c48fcb47Sblueswir1         .iu_version = 0x50000000,
1243c48fcb47Sblueswir1         .fpu_version = 0 << 17,
1244c48fcb47Sblueswir1         .mmu_version = 0x50000000,
1245c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1246c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1247c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1248c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1249c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
12501a14026eSblueswir1         .nwindows = 8,
1251e30b4678Sblueswir1         .features = CPU_FEATURE_FLOAT | CPU_FEATURE_MUL | CPU_FEATURE_FSQRT |
1252e30b4678Sblueswir1         CPU_FEATURE_FSMULD,
1253c48fcb47Sblueswir1     },
1254c48fcb47Sblueswir1     {
1255c48fcb47Sblueswir1         .name = "Weitek W8601",
1256c48fcb47Sblueswir1         .iu_version = 0x90 << 24, /* Impl 9, ver 0 */
1257c48fcb47Sblueswir1         .fpu_version = 3 << 17, /* FPU version 3 (Weitek WTL3170/2) */
1258c48fcb47Sblueswir1         .mmu_version = 0x10 << 24,
1259c48fcb47Sblueswir1         .mmu_bm = 0x00004000,
1260c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1261c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1262c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1263c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
12641a14026eSblueswir1         .nwindows = 8,
126564a88d5dSblueswir1         .features = CPU_DEFAULT_FEATURES,
1266c48fcb47Sblueswir1     },
1267c48fcb47Sblueswir1     {
1268c48fcb47Sblueswir1         .name = "LEON2",
1269c48fcb47Sblueswir1         .iu_version = 0xf2000000,
1270c48fcb47Sblueswir1         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1271c48fcb47Sblueswir1         .mmu_version = 0xf2000000,
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,
1278b04d9890SFabien Chouteau         .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_TA0_SHUTDOWN,
1279c48fcb47Sblueswir1     },
1280c48fcb47Sblueswir1     {
1281c48fcb47Sblueswir1         .name = "LEON3",
1282c48fcb47Sblueswir1         .iu_version = 0xf3000000,
1283c48fcb47Sblueswir1         .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
1284c48fcb47Sblueswir1         .mmu_version = 0xf3000000,
1285b04d9890SFabien Chouteau         .mmu_bm = 0x00000000,
1286c48fcb47Sblueswir1         .mmu_ctpr_mask = 0x007ffff0,
1287c48fcb47Sblueswir1         .mmu_cxr_mask = 0x0000003f,
1288c48fcb47Sblueswir1         .mmu_sfsr_mask = 0xffffffff,
1289c48fcb47Sblueswir1         .mmu_trcr_mask = 0xffffffff,
12901a14026eSblueswir1         .nwindows = 8,
1291b04d9890SFabien Chouteau         .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_TA0_SHUTDOWN,
1292c48fcb47Sblueswir1     },
1293c48fcb47Sblueswir1 #endif
1294c48fcb47Sblueswir1 };
1295c48fcb47Sblueswir1 
129664a88d5dSblueswir1 static const char * const feature_name[] = {
129764a88d5dSblueswir1     "float",
129864a88d5dSblueswir1     "float128",
129964a88d5dSblueswir1     "swap",
130064a88d5dSblueswir1     "mul",
130164a88d5dSblueswir1     "div",
130264a88d5dSblueswir1     "flush",
130364a88d5dSblueswir1     "fsqrt",
130464a88d5dSblueswir1     "fmul",
130564a88d5dSblueswir1     "vis1",
130664a88d5dSblueswir1     "vis2",
1307e30b4678Sblueswir1     "fsmuld",
1308fb79ceb9Sblueswir1     "hypv",
1309fb79ceb9Sblueswir1     "cmt",
1310fb79ceb9Sblueswir1     "gl",
131164a88d5dSblueswir1 };
131264a88d5dSblueswir1 
13139a78eeadSStefan Weil static void print_features(FILE *f, fprintf_function cpu_fprintf,
131464a88d5dSblueswir1                            uint32_t features, const char *prefix)
1315c48fcb47Sblueswir1 {
1316c48fcb47Sblueswir1     unsigned int i;
1317c48fcb47Sblueswir1 
131864a88d5dSblueswir1     for (i = 0; i < ARRAY_SIZE(feature_name); i++)
131964a88d5dSblueswir1         if (feature_name[i] && (features & (1 << i))) {
132064a88d5dSblueswir1             if (prefix)
132164a88d5dSblueswir1                 (*cpu_fprintf)(f, "%s", prefix);
132264a88d5dSblueswir1             (*cpu_fprintf)(f, "%s ", feature_name[i]);
132364a88d5dSblueswir1         }
132464a88d5dSblueswir1 }
132564a88d5dSblueswir1 
132664a88d5dSblueswir1 static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features)
132764a88d5dSblueswir1 {
132864a88d5dSblueswir1     unsigned int i;
132964a88d5dSblueswir1 
133064a88d5dSblueswir1     for (i = 0; i < ARRAY_SIZE(feature_name); i++)
133164a88d5dSblueswir1         if (feature_name[i] && !strcmp(flagname, feature_name[i])) {
133264a88d5dSblueswir1             *features |= 1 << i;
133364a88d5dSblueswir1             return;
133464a88d5dSblueswir1         }
133564a88d5dSblueswir1     fprintf(stderr, "CPU feature %s not found\n", flagname);
133664a88d5dSblueswir1 }
133764a88d5dSblueswir1 
133822548760Sblueswir1 static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model)
133964a88d5dSblueswir1 {
134064a88d5dSblueswir1     unsigned int i;
134164a88d5dSblueswir1     const sparc_def_t *def = NULL;
134264a88d5dSblueswir1     char *s = strdup(cpu_model);
134364a88d5dSblueswir1     char *featurestr, *name = strtok(s, ",");
134464a88d5dSblueswir1     uint32_t plus_features = 0;
134564a88d5dSblueswir1     uint32_t minus_features = 0;
13460bfcd599SBlue Swirl     uint64_t iu_version;
13471a14026eSblueswir1     uint32_t fpu_version, mmu_version, nwindows;
134864a88d5dSblueswir1 
1349b1503cdaSmalc     for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
1350c48fcb47Sblueswir1         if (strcasecmp(name, sparc_defs[i].name) == 0) {
135164a88d5dSblueswir1             def = &sparc_defs[i];
1352c48fcb47Sblueswir1         }
1353c48fcb47Sblueswir1     }
135464a88d5dSblueswir1     if (!def)
135564a88d5dSblueswir1         goto error;
135664a88d5dSblueswir1     memcpy(cpu_def, def, sizeof(*def));
135764a88d5dSblueswir1 
135864a88d5dSblueswir1     featurestr = strtok(NULL, ",");
135964a88d5dSblueswir1     while (featurestr) {
136064a88d5dSblueswir1         char *val;
136164a88d5dSblueswir1 
136264a88d5dSblueswir1         if (featurestr[0] == '+') {
136364a88d5dSblueswir1             add_flagname_to_bitmaps(featurestr + 1, &plus_features);
136464a88d5dSblueswir1         } else if (featurestr[0] == '-') {
136564a88d5dSblueswir1             add_flagname_to_bitmaps(featurestr + 1, &minus_features);
136664a88d5dSblueswir1         } else if ((val = strchr(featurestr, '='))) {
136764a88d5dSblueswir1             *val = 0; val++;
136864a88d5dSblueswir1             if (!strcmp(featurestr, "iu_version")) {
136964a88d5dSblueswir1                 char *err;
137064a88d5dSblueswir1 
137164a88d5dSblueswir1                 iu_version = strtoll(val, &err, 0);
137264a88d5dSblueswir1                 if (!*val || *err) {
137364a88d5dSblueswir1                     fprintf(stderr, "bad numerical value %s\n", val);
137464a88d5dSblueswir1                     goto error;
137564a88d5dSblueswir1                 }
137664a88d5dSblueswir1                 cpu_def->iu_version = iu_version;
137764a88d5dSblueswir1 #ifdef DEBUG_FEATURES
13780bfcd599SBlue Swirl                 fprintf(stderr, "iu_version %" PRIx64 "\n", iu_version);
137964a88d5dSblueswir1 #endif
138064a88d5dSblueswir1             } else if (!strcmp(featurestr, "fpu_version")) {
138164a88d5dSblueswir1                 char *err;
138264a88d5dSblueswir1 
138364a88d5dSblueswir1                 fpu_version = strtol(val, &err, 0);
138464a88d5dSblueswir1                 if (!*val || *err) {
138564a88d5dSblueswir1                     fprintf(stderr, "bad numerical value %s\n", val);
138664a88d5dSblueswir1                     goto error;
138764a88d5dSblueswir1                 }
138864a88d5dSblueswir1                 cpu_def->fpu_version = fpu_version;
138964a88d5dSblueswir1 #ifdef DEBUG_FEATURES
13900bf9e31aSBlue Swirl                 fprintf(stderr, "fpu_version %x\n", fpu_version);
139164a88d5dSblueswir1 #endif
139264a88d5dSblueswir1             } else if (!strcmp(featurestr, "mmu_version")) {
139364a88d5dSblueswir1                 char *err;
139464a88d5dSblueswir1 
139564a88d5dSblueswir1                 mmu_version = strtol(val, &err, 0);
139664a88d5dSblueswir1                 if (!*val || *err) {
139764a88d5dSblueswir1                     fprintf(stderr, "bad numerical value %s\n", val);
139864a88d5dSblueswir1                     goto error;
139964a88d5dSblueswir1                 }
140064a88d5dSblueswir1                 cpu_def->mmu_version = mmu_version;
140164a88d5dSblueswir1 #ifdef DEBUG_FEATURES
14020bf9e31aSBlue Swirl                 fprintf(stderr, "mmu_version %x\n", mmu_version);
140364a88d5dSblueswir1 #endif
14041a14026eSblueswir1             } else if (!strcmp(featurestr, "nwindows")) {
14051a14026eSblueswir1                 char *err;
14061a14026eSblueswir1 
14071a14026eSblueswir1                 nwindows = strtol(val, &err, 0);
14081a14026eSblueswir1                 if (!*val || *err || nwindows > MAX_NWINDOWS ||
14091a14026eSblueswir1                     nwindows < MIN_NWINDOWS) {
14101a14026eSblueswir1                     fprintf(stderr, "bad numerical value %s\n", val);
14111a14026eSblueswir1                     goto error;
14121a14026eSblueswir1                 }
14131a14026eSblueswir1                 cpu_def->nwindows = nwindows;
14141a14026eSblueswir1 #ifdef DEBUG_FEATURES
14151a14026eSblueswir1                 fprintf(stderr, "nwindows %d\n", nwindows);
14161a14026eSblueswir1 #endif
141764a88d5dSblueswir1             } else {
141864a88d5dSblueswir1                 fprintf(stderr, "unrecognized feature %s\n", featurestr);
141964a88d5dSblueswir1                 goto error;
142064a88d5dSblueswir1             }
142164a88d5dSblueswir1         } else {
142277f193daSblueswir1             fprintf(stderr, "feature string `%s' not in format "
142377f193daSblueswir1                     "(+feature|-feature|feature=xyz)\n", featurestr);
142464a88d5dSblueswir1             goto error;
142564a88d5dSblueswir1         }
142664a88d5dSblueswir1         featurestr = strtok(NULL, ",");
142764a88d5dSblueswir1     }
142864a88d5dSblueswir1     cpu_def->features |= plus_features;
142964a88d5dSblueswir1     cpu_def->features &= ~minus_features;
143064a88d5dSblueswir1 #ifdef DEBUG_FEATURES
143164a88d5dSblueswir1     print_features(stderr, fprintf, cpu_def->features, NULL);
143264a88d5dSblueswir1 #endif
143364a88d5dSblueswir1     free(s);
143464a88d5dSblueswir1     return 0;
143564a88d5dSblueswir1 
143664a88d5dSblueswir1  error:
143764a88d5dSblueswir1     free(s);
143864a88d5dSblueswir1     return -1;
1439c48fcb47Sblueswir1 }
1440c48fcb47Sblueswir1 
14419a78eeadSStefan Weil void sparc_cpu_list(FILE *f, fprintf_function cpu_fprintf)
1442c48fcb47Sblueswir1 {
1443c48fcb47Sblueswir1     unsigned int i;
1444c48fcb47Sblueswir1 
1445b1503cdaSmalc     for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
14461a14026eSblueswir1         (*cpu_fprintf)(f, "Sparc %16s IU " TARGET_FMT_lx " FPU %08x MMU %08x NWINS %d ",
1447c48fcb47Sblueswir1                        sparc_defs[i].name,
1448c48fcb47Sblueswir1                        sparc_defs[i].iu_version,
1449c48fcb47Sblueswir1                        sparc_defs[i].fpu_version,
14501a14026eSblueswir1                        sparc_defs[i].mmu_version,
14511a14026eSblueswir1                        sparc_defs[i].nwindows);
145277f193daSblueswir1         print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES &
145377f193daSblueswir1                        ~sparc_defs[i].features, "-");
145477f193daSblueswir1         print_features(f, cpu_fprintf, ~CPU_DEFAULT_FEATURES &
145577f193daSblueswir1                        sparc_defs[i].features, "+");
145664a88d5dSblueswir1         (*cpu_fprintf)(f, "\n");
1457c48fcb47Sblueswir1     }
1458f76981b1Sblueswir1     (*cpu_fprintf)(f, "Default CPU feature flags (use '-' to remove): ");
1459f76981b1Sblueswir1     print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES, NULL);
146064a88d5dSblueswir1     (*cpu_fprintf)(f, "\n");
1461f76981b1Sblueswir1     (*cpu_fprintf)(f, "Available CPU feature flags (use '+' to add): ");
1462f76981b1Sblueswir1     print_features(f, cpu_fprintf, ~CPU_DEFAULT_FEATURES, NULL);
1463f76981b1Sblueswir1     (*cpu_fprintf)(f, "\n");
1464f76981b1Sblueswir1     (*cpu_fprintf)(f, "Numerical features (use '=' to set): iu_version "
1465f76981b1Sblueswir1                    "fpu_version mmu_version nwindows\n");
1466c48fcb47Sblueswir1 }
1467c48fcb47Sblueswir1 
14689a78eeadSStefan Weil static void cpu_print_cc(FILE *f, fprintf_function cpu_fprintf,
146943bb98bfSBlue Swirl                          uint32_t cc)
147043bb98bfSBlue Swirl {
147143bb98bfSBlue Swirl     cpu_fprintf(f, "%c%c%c%c", cc & PSR_NEG? 'N' : '-',
147243bb98bfSBlue Swirl                 cc & PSR_ZERO? 'Z' : '-', cc & PSR_OVF? 'V' : '-',
147343bb98bfSBlue Swirl                 cc & PSR_CARRY? 'C' : '-');
147443bb98bfSBlue Swirl }
147543bb98bfSBlue Swirl 
147643bb98bfSBlue Swirl #ifdef TARGET_SPARC64
147743bb98bfSBlue Swirl #define REGS_PER_LINE 4
147843bb98bfSBlue Swirl #else
147943bb98bfSBlue Swirl #define REGS_PER_LINE 8
148043bb98bfSBlue Swirl #endif
148143bb98bfSBlue Swirl 
14829a78eeadSStefan Weil void cpu_dump_state(CPUState *env, FILE *f, fprintf_function cpu_fprintf,
1483c48fcb47Sblueswir1                     int flags)
1484c48fcb47Sblueswir1 {
1485c48fcb47Sblueswir1     int i, x;
1486c48fcb47Sblueswir1 
148777f193daSblueswir1     cpu_fprintf(f, "pc: " TARGET_FMT_lx "  npc: " TARGET_FMT_lx "\n", env->pc,
148877f193daSblueswir1                 env->npc);
1489c48fcb47Sblueswir1     cpu_fprintf(f, "General Registers:\n");
149043bb98bfSBlue Swirl 
149143bb98bfSBlue Swirl     for (i = 0; i < 8; i++) {
149243bb98bfSBlue Swirl         if (i % REGS_PER_LINE == 0) {
149343bb98bfSBlue Swirl             cpu_fprintf(f, "%%g%d-%d:", i, i + REGS_PER_LINE - 1);
149443bb98bfSBlue Swirl         }
149543bb98bfSBlue Swirl         cpu_fprintf(f, " " TARGET_FMT_lx, env->gregs[i]);
149643bb98bfSBlue Swirl         if (i % REGS_PER_LINE == REGS_PER_LINE - 1) {
1497c48fcb47Sblueswir1             cpu_fprintf(f, "\n");
1498c48fcb47Sblueswir1         }
149943bb98bfSBlue Swirl     }
150043bb98bfSBlue Swirl     cpu_fprintf(f, "\nCurrent Register Window:\n");
150143bb98bfSBlue Swirl     for (x = 0; x < 3; x++) {
150243bb98bfSBlue Swirl         for (i = 0; i < 8; i++) {
150343bb98bfSBlue Swirl             if (i % REGS_PER_LINE == 0) {
150443bb98bfSBlue Swirl                 cpu_fprintf(f, "%%%c%d-%d: ",
150543bb98bfSBlue Swirl                             x == 0 ? 'o' : (x == 1 ? 'l' : 'i'),
150643bb98bfSBlue Swirl                             i, i + REGS_PER_LINE - 1);
150743bb98bfSBlue Swirl             }
150843bb98bfSBlue Swirl             cpu_fprintf(f, TARGET_FMT_lx " ", env->regwptr[i + x * 8]);
150943bb98bfSBlue Swirl             if (i % REGS_PER_LINE == REGS_PER_LINE - 1) {
151043bb98bfSBlue Swirl                 cpu_fprintf(f, "\n");
151143bb98bfSBlue Swirl             }
151243bb98bfSBlue Swirl         }
151343bb98bfSBlue Swirl     }
1514c48fcb47Sblueswir1     cpu_fprintf(f, "\nFloating Point Registers:\n");
151543bb98bfSBlue Swirl     for (i = 0; i < TARGET_FPREGS; i++) {
1516c48fcb47Sblueswir1         if ((i & 3) == 0)
1517c48fcb47Sblueswir1             cpu_fprintf(f, "%%f%02d:", i);
1518a37ee56cSblueswir1         cpu_fprintf(f, " %016f", *(float *)&env->fpr[i]);
1519c48fcb47Sblueswir1         if ((i & 3) == 3)
1520c48fcb47Sblueswir1             cpu_fprintf(f, "\n");
1521c48fcb47Sblueswir1     }
1522c48fcb47Sblueswir1 #ifdef TARGET_SPARC64
152343bb98bfSBlue Swirl     cpu_fprintf(f, "pstate: %08x ccr: %02x (icc: ", env->pstate,
1524113c6106SStefan Weil                 (unsigned)cpu_get_ccr(env));
15255a834bb4SBlue Swirl     cpu_print_cc(f, cpu_fprintf, cpu_get_ccr(env) << PSR_CARRY_SHIFT);
152643bb98bfSBlue Swirl     cpu_fprintf(f, " xcc: ");
15275a834bb4SBlue Swirl     cpu_print_cc(f, cpu_fprintf, cpu_get_ccr(env) << (PSR_CARRY_SHIFT - 4));
152843bb98bfSBlue Swirl     cpu_fprintf(f, ") asi: %02x tl: %d pil: %x\n", env->asi, env->tl,
152943bb98bfSBlue Swirl                 env->psrpil);
153043bb98bfSBlue Swirl     cpu_fprintf(f, "cansave: %d canrestore: %d otherwin: %d wstate: %d "
153143bb98bfSBlue Swirl                 "cleanwin: %d cwp: %d\n",
1532c48fcb47Sblueswir1                 env->cansave, env->canrestore, env->otherwin, env->wstate,
15331a14026eSblueswir1                 env->cleanwin, env->nwindows - 1 - env->cwp);
153443bb98bfSBlue Swirl     cpu_fprintf(f, "fsr: " TARGET_FMT_lx " y: " TARGET_FMT_lx " fprs: "
153543bb98bfSBlue Swirl                 TARGET_FMT_lx "\n", env->fsr, env->y, env->fprs);
1536c48fcb47Sblueswir1 #else
15375a834bb4SBlue Swirl     cpu_fprintf(f, "psr: %08x (icc: ", cpu_get_psr(env));
15385a834bb4SBlue Swirl     cpu_print_cc(f, cpu_fprintf, cpu_get_psr(env));
153943bb98bfSBlue Swirl     cpu_fprintf(f, " SPE: %c%c%c) wim: %08x\n", env->psrs? 'S' : '-',
154043bb98bfSBlue Swirl                 env->psrps? 'P' : '-', env->psret? 'E' : '-',
154143bb98bfSBlue Swirl                 env->wim);
154243bb98bfSBlue Swirl     cpu_fprintf(f, "fsr: " TARGET_FMT_lx " y: " TARGET_FMT_lx "\n",
154343bb98bfSBlue Swirl                 env->fsr, env->y);
1544c48fcb47Sblueswir1 #endif
1545c48fcb47Sblueswir1 }
1546