xref: /qemu/target/sparc/mmu_helper.c (revision e84942f2ceaa79430414f2cb68d77c044dadca96)
1163fa5caSBlue Swirl /*
2163fa5caSBlue Swirl  *  Sparc MMU helpers
3163fa5caSBlue Swirl  *
4163fa5caSBlue Swirl  *  Copyright (c) 2003-2005 Fabrice Bellard
5163fa5caSBlue Swirl  *
6163fa5caSBlue Swirl  * This library is free software; you can redistribute it and/or
7163fa5caSBlue Swirl  * modify it under the terms of the GNU Lesser General Public
8163fa5caSBlue Swirl  * License as published by the Free Software Foundation; either
9163fa5caSBlue Swirl  * version 2 of the License, or (at your option) any later version.
10163fa5caSBlue Swirl  *
11163fa5caSBlue Swirl  * This library is distributed in the hope that it will be useful,
12163fa5caSBlue Swirl  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13163fa5caSBlue Swirl  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14163fa5caSBlue Swirl  * Lesser General Public License for more details.
15163fa5caSBlue Swirl  *
16163fa5caSBlue Swirl  * You should have received a copy of the GNU Lesser General Public
17163fa5caSBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18163fa5caSBlue Swirl  */
19163fa5caSBlue Swirl 
20db5ebe5fSPeter Maydell #include "qemu/osdep.h"
21163fa5caSBlue Swirl #include "cpu.h"
2263c91552SPaolo Bonzini #include "exec/exec-all.h"
23fad866daSMarkus Armbruster #include "qemu/qemu-print.h"
24ec0ceb17SBlue Swirl #include "trace.h"
25163fa5caSBlue Swirl 
26163fa5caSBlue Swirl /* Sparc MMU emulation */
27163fa5caSBlue Swirl 
28163fa5caSBlue Swirl #if defined(CONFIG_USER_ONLY)
29163fa5caSBlue Swirl 
30*e84942f2SRichard Henderson bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
31*e84942f2SRichard Henderson                         MMUAccessType access_type, int mmu_idx,
32*e84942f2SRichard Henderson                         bool probe, uintptr_t retaddr)
33163fa5caSBlue Swirl {
348d8cb956SPeter Maydell     SPARCCPU *cpu = SPARC_CPU(cs);
358d8cb956SPeter Maydell     CPUSPARCState *env = &cpu->env;
368d8cb956SPeter Maydell 
37*e84942f2SRichard Henderson     if (access_type == MMU_INST_FETCH) {
3827103424SAndreas Färber         cs->exception_index = TT_TFAULT;
39163fa5caSBlue Swirl     } else {
4027103424SAndreas Färber         cs->exception_index = TT_DFAULT;
418d8cb956SPeter Maydell #ifdef TARGET_SPARC64
428d8cb956SPeter Maydell         env->dmmu.mmuregs[4] = address;
438d8cb956SPeter Maydell #else
448d8cb956SPeter Maydell         env->mmuregs[4] = address;
458d8cb956SPeter Maydell #endif
46163fa5caSBlue Swirl     }
47*e84942f2SRichard Henderson     cpu_loop_exit_restore(cs, retaddr);
48163fa5caSBlue Swirl }
49163fa5caSBlue Swirl 
50163fa5caSBlue Swirl #else
51163fa5caSBlue Swirl 
52163fa5caSBlue Swirl #ifndef TARGET_SPARC64
53163fa5caSBlue Swirl /*
54163fa5caSBlue Swirl  * Sparc V8 Reference MMU (SRMMU)
55163fa5caSBlue Swirl  */
56163fa5caSBlue Swirl static const int access_table[8][8] = {
57163fa5caSBlue Swirl     { 0, 0, 0, 0, 8, 0, 12, 12 },
58163fa5caSBlue Swirl     { 0, 0, 0, 0, 8, 0, 0, 0 },
59163fa5caSBlue Swirl     { 8, 8, 0, 0, 0, 8, 12, 12 },
60163fa5caSBlue Swirl     { 8, 8, 0, 0, 0, 8, 0, 0 },
61163fa5caSBlue Swirl     { 8, 0, 8, 0, 8, 8, 12, 12 },
62163fa5caSBlue Swirl     { 8, 0, 8, 0, 8, 0, 8, 0 },
63163fa5caSBlue Swirl     { 8, 8, 8, 0, 8, 8, 12, 12 },
64163fa5caSBlue Swirl     { 8, 8, 8, 0, 8, 8, 8, 0 }
65163fa5caSBlue Swirl };
66163fa5caSBlue Swirl 
67163fa5caSBlue Swirl static const int perm_table[2][8] = {
68163fa5caSBlue Swirl     {
69163fa5caSBlue Swirl         PAGE_READ,
70163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE,
71163fa5caSBlue Swirl         PAGE_READ | PAGE_EXEC,
72163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
73163fa5caSBlue Swirl         PAGE_EXEC,
74163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE,
75163fa5caSBlue Swirl         PAGE_READ | PAGE_EXEC,
76163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE | PAGE_EXEC
77163fa5caSBlue Swirl     },
78163fa5caSBlue Swirl     {
79163fa5caSBlue Swirl         PAGE_READ,
80163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE,
81163fa5caSBlue Swirl         PAGE_READ | PAGE_EXEC,
82163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
83163fa5caSBlue Swirl         PAGE_EXEC,
84163fa5caSBlue Swirl         PAGE_READ,
85163fa5caSBlue Swirl         0,
86163fa5caSBlue Swirl         0,
87163fa5caSBlue Swirl     }
88163fa5caSBlue Swirl };
89163fa5caSBlue Swirl 
90a8170e5eSAvi Kivity static int get_physical_address(CPUSPARCState *env, hwaddr *physical,
91163fa5caSBlue Swirl                                 int *prot, int *access_index,
92163fa5caSBlue Swirl                                 target_ulong address, int rw, int mmu_idx,
93163fa5caSBlue Swirl                                 target_ulong *page_size)
94163fa5caSBlue Swirl {
95163fa5caSBlue Swirl     int access_perms = 0;
96a8170e5eSAvi Kivity     hwaddr pde_ptr;
97163fa5caSBlue Swirl     uint32_t pde;
98163fa5caSBlue Swirl     int error_code = 0, is_dirty, is_user;
99163fa5caSBlue Swirl     unsigned long page_offset;
1002fad1112SAndreas Färber     CPUState *cs = CPU(sparc_env_get_cpu(env));
101163fa5caSBlue Swirl 
102163fa5caSBlue Swirl     is_user = mmu_idx == MMU_USER_IDX;
103163fa5caSBlue Swirl 
104af7a06baSRichard Henderson     if (mmu_idx == MMU_PHYS_IDX) {
105163fa5caSBlue Swirl         *page_size = TARGET_PAGE_SIZE;
106163fa5caSBlue Swirl         /* Boot mode: instruction fetches are taken from PROM */
107576e1c4cSIgor Mammedov         if (rw == 2 && (env->mmuregs[0] & env->def.mmu_bm)) {
108163fa5caSBlue Swirl             *physical = env->prom_addr | (address & 0x7ffffULL);
109163fa5caSBlue Swirl             *prot = PAGE_READ | PAGE_EXEC;
110163fa5caSBlue Swirl             return 0;
111163fa5caSBlue Swirl         }
112163fa5caSBlue Swirl         *physical = address;
113163fa5caSBlue Swirl         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
114163fa5caSBlue Swirl         return 0;
115163fa5caSBlue Swirl     }
116163fa5caSBlue Swirl 
117163fa5caSBlue Swirl     *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user ? 0 : 1);
118163fa5caSBlue Swirl     *physical = 0xffffffffffff0000ULL;
119163fa5caSBlue Swirl 
120163fa5caSBlue Swirl     /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
121163fa5caSBlue Swirl     /* Context base + context number */
122163fa5caSBlue Swirl     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
123fdfba1a2SEdgar E. Iglesias     pde = ldl_phys(cs->as, pde_ptr);
124163fa5caSBlue Swirl 
125163fa5caSBlue Swirl     /* Ctx pde */
126163fa5caSBlue Swirl     switch (pde & PTE_ENTRYTYPE_MASK) {
127163fa5caSBlue Swirl     default:
128163fa5caSBlue Swirl     case 0: /* Invalid */
129163fa5caSBlue Swirl         return 1 << 2;
130163fa5caSBlue Swirl     case 2: /* L0 PTE, maybe should not happen? */
131163fa5caSBlue Swirl     case 3: /* Reserved */
132163fa5caSBlue Swirl         return 4 << 2;
133163fa5caSBlue Swirl     case 1: /* L0 PDE */
134163fa5caSBlue Swirl         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
135fdfba1a2SEdgar E. Iglesias         pde = ldl_phys(cs->as, pde_ptr);
136163fa5caSBlue Swirl 
137163fa5caSBlue Swirl         switch (pde & PTE_ENTRYTYPE_MASK) {
138163fa5caSBlue Swirl         default:
139163fa5caSBlue Swirl         case 0: /* Invalid */
140163fa5caSBlue Swirl             return (1 << 8) | (1 << 2);
141163fa5caSBlue Swirl         case 3: /* Reserved */
142163fa5caSBlue Swirl             return (1 << 8) | (4 << 2);
143163fa5caSBlue Swirl         case 1: /* L1 PDE */
144163fa5caSBlue Swirl             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
145fdfba1a2SEdgar E. Iglesias             pde = ldl_phys(cs->as, pde_ptr);
146163fa5caSBlue Swirl 
147163fa5caSBlue Swirl             switch (pde & PTE_ENTRYTYPE_MASK) {
148163fa5caSBlue Swirl             default:
149163fa5caSBlue Swirl             case 0: /* Invalid */
150163fa5caSBlue Swirl                 return (2 << 8) | (1 << 2);
151163fa5caSBlue Swirl             case 3: /* Reserved */
152163fa5caSBlue Swirl                 return (2 << 8) | (4 << 2);
153163fa5caSBlue Swirl             case 1: /* L2 PDE */
154163fa5caSBlue Swirl                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
155fdfba1a2SEdgar E. Iglesias                 pde = ldl_phys(cs->as, pde_ptr);
156163fa5caSBlue Swirl 
157163fa5caSBlue Swirl                 switch (pde & PTE_ENTRYTYPE_MASK) {
158163fa5caSBlue Swirl                 default:
159163fa5caSBlue Swirl                 case 0: /* Invalid */
160163fa5caSBlue Swirl                     return (3 << 8) | (1 << 2);
161163fa5caSBlue Swirl                 case 1: /* PDE, should not happen */
162163fa5caSBlue Swirl                 case 3: /* Reserved */
163163fa5caSBlue Swirl                     return (3 << 8) | (4 << 2);
164163fa5caSBlue Swirl                 case 2: /* L3 PTE */
1651658dd32SBlue Swirl                     page_offset = 0;
166163fa5caSBlue Swirl                 }
167163fa5caSBlue Swirl                 *page_size = TARGET_PAGE_SIZE;
168163fa5caSBlue Swirl                 break;
169163fa5caSBlue Swirl             case 2: /* L2 PTE */
1701658dd32SBlue Swirl                 page_offset = address & 0x3f000;
171163fa5caSBlue Swirl                 *page_size = 0x40000;
172163fa5caSBlue Swirl             }
173163fa5caSBlue Swirl             break;
174163fa5caSBlue Swirl         case 2: /* L1 PTE */
1751658dd32SBlue Swirl             page_offset = address & 0xfff000;
176163fa5caSBlue Swirl             *page_size = 0x1000000;
177163fa5caSBlue Swirl         }
178163fa5caSBlue Swirl     }
179163fa5caSBlue Swirl 
180163fa5caSBlue Swirl     /* check access */
181163fa5caSBlue Swirl     access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
182163fa5caSBlue Swirl     error_code = access_table[*access_index][access_perms];
183163fa5caSBlue Swirl     if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user)) {
184163fa5caSBlue Swirl         return error_code;
185163fa5caSBlue Swirl     }
186163fa5caSBlue Swirl 
187163fa5caSBlue Swirl     /* update page modified and dirty bits */
188163fa5caSBlue Swirl     is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
189163fa5caSBlue Swirl     if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
190163fa5caSBlue Swirl         pde |= PG_ACCESSED_MASK;
191163fa5caSBlue Swirl         if (is_dirty) {
192163fa5caSBlue Swirl             pde |= PG_MODIFIED_MASK;
193163fa5caSBlue Swirl         }
1942198a121SEdgar E. Iglesias         stl_phys_notdirty(cs->as, pde_ptr, pde);
195163fa5caSBlue Swirl     }
196163fa5caSBlue Swirl 
197163fa5caSBlue Swirl     /* the page can be put in the TLB */
198163fa5caSBlue Swirl     *prot = perm_table[is_user][access_perms];
199163fa5caSBlue Swirl     if (!(pde & PG_MODIFIED_MASK)) {
200163fa5caSBlue Swirl         /* only set write access if already dirty... otherwise wait
201163fa5caSBlue Swirl            for dirty access */
202163fa5caSBlue Swirl         *prot &= ~PAGE_WRITE;
203163fa5caSBlue Swirl     }
204163fa5caSBlue Swirl 
205163fa5caSBlue Swirl     /* Even if large ptes, we map only one 4KB page in the cache to
206163fa5caSBlue Swirl        avoid filling it too fast */
207a8170e5eSAvi Kivity     *physical = ((hwaddr)(pde & PTE_ADDR_MASK) << 4) + page_offset;
208163fa5caSBlue Swirl     return error_code;
209163fa5caSBlue Swirl }
210163fa5caSBlue Swirl 
211163fa5caSBlue Swirl /* Perform address translation */
212*e84942f2SRichard Henderson bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
213*e84942f2SRichard Henderson                         MMUAccessType access_type, int mmu_idx,
214*e84942f2SRichard Henderson                         bool probe, uintptr_t retaddr)
215163fa5caSBlue Swirl {
2167510454eSAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
2177510454eSAndreas Färber     CPUSPARCState *env = &cpu->env;
218a8170e5eSAvi Kivity     hwaddr paddr;
219163fa5caSBlue Swirl     target_ulong vaddr;
220163fa5caSBlue Swirl     target_ulong page_size;
221163fa5caSBlue Swirl     int error_code = 0, prot, access_index;
222163fa5caSBlue Swirl 
223*e84942f2SRichard Henderson     /*
224*e84942f2SRichard Henderson      * TODO: If we ever need tlb_vaddr_to_host for this target,
225*e84942f2SRichard Henderson      * then we must figure out how to manipulate FSR and FAR
226*e84942f2SRichard Henderson      * when both MMU_NF and probe are set.  In the meantime,
227*e84942f2SRichard Henderson      * do not support this use case.
228*e84942f2SRichard Henderson      */
229*e84942f2SRichard Henderson     assert(!probe);
230*e84942f2SRichard Henderson 
2311658dd32SBlue Swirl     address &= TARGET_PAGE_MASK;
232163fa5caSBlue Swirl     error_code = get_physical_address(env, &paddr, &prot, &access_index,
233*e84942f2SRichard Henderson                                       address, access_type,
234*e84942f2SRichard Henderson                                       mmu_idx, &page_size);
2351658dd32SBlue Swirl     vaddr = address;
236*e84942f2SRichard Henderson     if (likely(error_code == 0)) {
237339aaf5bSAntony Pavlov         qemu_log_mask(CPU_LOG_MMU,
238*e84942f2SRichard Henderson                       "Translate at %" VADDR_PRIx " -> "
239*e84942f2SRichard Henderson                       TARGET_FMT_plx ", vaddr " TARGET_FMT_lx "\n",
240*e84942f2SRichard Henderson                       address, paddr, vaddr);
2410c591eb0SAndreas Färber         tlb_set_page(cs, vaddr, paddr, prot, mmu_idx, page_size);
242*e84942f2SRichard Henderson         return true;
243163fa5caSBlue Swirl     }
244163fa5caSBlue Swirl 
245163fa5caSBlue Swirl     if (env->mmuregs[3]) { /* Fault status register */
246163fa5caSBlue Swirl         env->mmuregs[3] = 1; /* overflow (not read before another fault) */
247163fa5caSBlue Swirl     }
248163fa5caSBlue Swirl     env->mmuregs[3] |= (access_index << 5) | error_code | 2;
249163fa5caSBlue Swirl     env->mmuregs[4] = address; /* Fault address register */
250163fa5caSBlue Swirl 
251163fa5caSBlue Swirl     if ((env->mmuregs[0] & MMU_NF) || env->psret == 0)  {
252163fa5caSBlue Swirl         /* No fault mode: if a mapping is available, just override
253163fa5caSBlue Swirl            permissions. If no mapping is available, redirect accesses to
254163fa5caSBlue Swirl            neverland. Fake/overridden mappings will be flushed when
255163fa5caSBlue Swirl            switching to normal mode. */
256163fa5caSBlue Swirl         prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2570c591eb0SAndreas Färber         tlb_set_page(cs, vaddr, paddr, prot, mmu_idx, TARGET_PAGE_SIZE);
258*e84942f2SRichard Henderson         return true;
259163fa5caSBlue Swirl     } else {
260*e84942f2SRichard Henderson         if (access_type == MMU_INST_FETCH) {
26127103424SAndreas Färber             cs->exception_index = TT_TFAULT;
262163fa5caSBlue Swirl         } else {
26327103424SAndreas Färber             cs->exception_index = TT_DFAULT;
264163fa5caSBlue Swirl         }
265*e84942f2SRichard Henderson         cpu_loop_exit_restore(cs, retaddr);
266163fa5caSBlue Swirl     }
267163fa5caSBlue Swirl }
268163fa5caSBlue Swirl 
269c5f9864eSAndreas Färber target_ulong mmu_probe(CPUSPARCState *env, target_ulong address, int mmulev)
270163fa5caSBlue Swirl {
2712fad1112SAndreas Färber     CPUState *cs = CPU(sparc_env_get_cpu(env));
272a8170e5eSAvi Kivity     hwaddr pde_ptr;
273163fa5caSBlue Swirl     uint32_t pde;
274163fa5caSBlue Swirl 
275163fa5caSBlue Swirl     /* Context base + context number */
276a8170e5eSAvi Kivity     pde_ptr = (hwaddr)(env->mmuregs[1] << 4) +
277163fa5caSBlue Swirl         (env->mmuregs[2] << 2);
278fdfba1a2SEdgar E. Iglesias     pde = ldl_phys(cs->as, pde_ptr);
279163fa5caSBlue Swirl 
280163fa5caSBlue Swirl     switch (pde & PTE_ENTRYTYPE_MASK) {
281163fa5caSBlue Swirl     default:
282163fa5caSBlue Swirl     case 0: /* Invalid */
283163fa5caSBlue Swirl     case 2: /* PTE, maybe should not happen? */
284163fa5caSBlue Swirl     case 3: /* Reserved */
285163fa5caSBlue Swirl         return 0;
286163fa5caSBlue Swirl     case 1: /* L1 PDE */
287163fa5caSBlue Swirl         if (mmulev == 3) {
288163fa5caSBlue Swirl             return pde;
289163fa5caSBlue Swirl         }
290163fa5caSBlue Swirl         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
291fdfba1a2SEdgar E. Iglesias         pde = ldl_phys(cs->as, pde_ptr);
292163fa5caSBlue Swirl 
293163fa5caSBlue Swirl         switch (pde & PTE_ENTRYTYPE_MASK) {
294163fa5caSBlue Swirl         default:
295163fa5caSBlue Swirl         case 0: /* Invalid */
296163fa5caSBlue Swirl         case 3: /* Reserved */
297163fa5caSBlue Swirl             return 0;
298163fa5caSBlue Swirl         case 2: /* L1 PTE */
299163fa5caSBlue Swirl             return pde;
300163fa5caSBlue Swirl         case 1: /* L2 PDE */
301163fa5caSBlue Swirl             if (mmulev == 2) {
302163fa5caSBlue Swirl                 return pde;
303163fa5caSBlue Swirl             }
304163fa5caSBlue Swirl             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
305fdfba1a2SEdgar E. Iglesias             pde = ldl_phys(cs->as, pde_ptr);
306163fa5caSBlue Swirl 
307163fa5caSBlue Swirl             switch (pde & PTE_ENTRYTYPE_MASK) {
308163fa5caSBlue Swirl             default:
309163fa5caSBlue Swirl             case 0: /* Invalid */
310163fa5caSBlue Swirl             case 3: /* Reserved */
311163fa5caSBlue Swirl                 return 0;
312163fa5caSBlue Swirl             case 2: /* L2 PTE */
313163fa5caSBlue Swirl                 return pde;
314163fa5caSBlue Swirl             case 1: /* L3 PDE */
315163fa5caSBlue Swirl                 if (mmulev == 1) {
316163fa5caSBlue Swirl                     return pde;
317163fa5caSBlue Swirl                 }
318163fa5caSBlue Swirl                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
319fdfba1a2SEdgar E. Iglesias                 pde = ldl_phys(cs->as, pde_ptr);
320163fa5caSBlue Swirl 
321163fa5caSBlue Swirl                 switch (pde & PTE_ENTRYTYPE_MASK) {
322163fa5caSBlue Swirl                 default:
323163fa5caSBlue Swirl                 case 0: /* Invalid */
324163fa5caSBlue Swirl                 case 1: /* PDE, should not happen */
325163fa5caSBlue Swirl                 case 3: /* Reserved */
326163fa5caSBlue Swirl                     return 0;
327163fa5caSBlue Swirl                 case 2: /* L3 PTE */
328163fa5caSBlue Swirl                     return pde;
329163fa5caSBlue Swirl                 }
330163fa5caSBlue Swirl             }
331163fa5caSBlue Swirl         }
332163fa5caSBlue Swirl     }
333163fa5caSBlue Swirl     return 0;
334163fa5caSBlue Swirl }
335163fa5caSBlue Swirl 
336fad866daSMarkus Armbruster void dump_mmu(CPUSPARCState *env)
337163fa5caSBlue Swirl {
33800b941e5SAndreas Färber     CPUState *cs = CPU(sparc_env_get_cpu(env));
339163fa5caSBlue Swirl     target_ulong va, va1, va2;
340163fa5caSBlue Swirl     unsigned int n, m, o;
341a8170e5eSAvi Kivity     hwaddr pde_ptr, pa;
342163fa5caSBlue Swirl     uint32_t pde;
343163fa5caSBlue Swirl 
344163fa5caSBlue Swirl     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
345fdfba1a2SEdgar E. Iglesias     pde = ldl_phys(cs->as, pde_ptr);
346fad866daSMarkus Armbruster     qemu_printf("Root ptr: " TARGET_FMT_plx ", ctx: %d\n",
347a8170e5eSAvi Kivity                 (hwaddr)env->mmuregs[1] << 4, env->mmuregs[2]);
348163fa5caSBlue Swirl     for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
349163fa5caSBlue Swirl         pde = mmu_probe(env, va, 2);
350163fa5caSBlue Swirl         if (pde) {
35100b941e5SAndreas Färber             pa = cpu_get_phys_page_debug(cs, va);
352fad866daSMarkus Armbruster             qemu_printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
353163fa5caSBlue Swirl                         " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
354163fa5caSBlue Swirl             for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
355163fa5caSBlue Swirl                 pde = mmu_probe(env, va1, 1);
356163fa5caSBlue Swirl                 if (pde) {
35700b941e5SAndreas Färber                     pa = cpu_get_phys_page_debug(cs, va1);
358fad866daSMarkus Armbruster                     qemu_printf(" VA: " TARGET_FMT_lx ", PA: "
359163fa5caSBlue Swirl                                 TARGET_FMT_plx " PDE: " TARGET_FMT_lx "\n",
360163fa5caSBlue Swirl                                 va1, pa, pde);
361163fa5caSBlue Swirl                     for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
362163fa5caSBlue Swirl                         pde = mmu_probe(env, va2, 0);
363163fa5caSBlue Swirl                         if (pde) {
36400b941e5SAndreas Färber                             pa = cpu_get_phys_page_debug(cs, va2);
365fad866daSMarkus Armbruster                             qemu_printf("  VA: " TARGET_FMT_lx ", PA: "
366163fa5caSBlue Swirl                                         TARGET_FMT_plx " PTE: "
367163fa5caSBlue Swirl                                         TARGET_FMT_lx "\n",
368163fa5caSBlue Swirl                                         va2, pa, pde);
369163fa5caSBlue Swirl                         }
370163fa5caSBlue Swirl                     }
371163fa5caSBlue Swirl                 }
372163fa5caSBlue Swirl             }
373163fa5caSBlue Swirl         }
374163fa5caSBlue Swirl     }
375163fa5caSBlue Swirl }
376163fa5caSBlue Swirl 
377163fa5caSBlue Swirl /* Gdb expects all registers windows to be flushed in ram. This function handles
378163fa5caSBlue Swirl  * reads (and only reads) in stack frames as if windows were flushed. We assume
379163fa5caSBlue Swirl  * that the sparc ABI is followed.
380163fa5caSBlue Swirl  */
381f3659eeeSAndreas Färber int sparc_cpu_memory_rw_debug(CPUState *cs, vaddr address,
382f3659eeeSAndreas Färber                               uint8_t *buf, int len, bool is_write)
383163fa5caSBlue Swirl {
384f3659eeeSAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
385f3659eeeSAndreas Färber     CPUSPARCState *env = &cpu->env;
386f3659eeeSAndreas Färber     target_ulong addr = address;
387163fa5caSBlue Swirl     int i;
388163fa5caSBlue Swirl     int len1;
389163fa5caSBlue Swirl     int cwp = env->cwp;
390163fa5caSBlue Swirl 
391163fa5caSBlue Swirl     if (!is_write) {
392163fa5caSBlue Swirl         for (i = 0; i < env->nwindows; i++) {
393163fa5caSBlue Swirl             int off;
394163fa5caSBlue Swirl             target_ulong fp = env->regbase[cwp * 16 + 22];
395163fa5caSBlue Swirl 
396163fa5caSBlue Swirl             /* Assume fp == 0 means end of frame.  */
397163fa5caSBlue Swirl             if (fp == 0) {
398163fa5caSBlue Swirl                 break;
399163fa5caSBlue Swirl             }
400163fa5caSBlue Swirl 
401163fa5caSBlue Swirl             cwp = cpu_cwp_inc(env, cwp + 1);
402163fa5caSBlue Swirl 
403163fa5caSBlue Swirl             /* Invalid window ? */
404163fa5caSBlue Swirl             if (env->wim & (1 << cwp)) {
405163fa5caSBlue Swirl                 break;
406163fa5caSBlue Swirl             }
407163fa5caSBlue Swirl 
408163fa5caSBlue Swirl             /* According to the ABI, the stack is growing downward.  */
409163fa5caSBlue Swirl             if (addr + len < fp) {
410163fa5caSBlue Swirl                 break;
411163fa5caSBlue Swirl             }
412163fa5caSBlue Swirl 
413163fa5caSBlue Swirl             /* Not in this frame.  */
414163fa5caSBlue Swirl             if (addr > fp + 64) {
415163fa5caSBlue Swirl                 continue;
416163fa5caSBlue Swirl             }
417163fa5caSBlue Swirl 
418163fa5caSBlue Swirl             /* Handle access before this window.  */
419163fa5caSBlue Swirl             if (addr < fp) {
420163fa5caSBlue Swirl                 len1 = fp - addr;
421f17ec444SAndreas Färber                 if (cpu_memory_rw_debug(cs, addr, buf, len1, is_write) != 0) {
422163fa5caSBlue Swirl                     return -1;
423163fa5caSBlue Swirl                 }
424163fa5caSBlue Swirl                 addr += len1;
425163fa5caSBlue Swirl                 len -= len1;
426163fa5caSBlue Swirl                 buf += len1;
427163fa5caSBlue Swirl             }
428163fa5caSBlue Swirl 
429163fa5caSBlue Swirl             /* Access byte per byte to registers. Not very efficient but speed
430163fa5caSBlue Swirl              * is not critical.
431163fa5caSBlue Swirl              */
432163fa5caSBlue Swirl             off = addr - fp;
433163fa5caSBlue Swirl             len1 = 64 - off;
434163fa5caSBlue Swirl 
435163fa5caSBlue Swirl             if (len1 > len) {
436163fa5caSBlue Swirl                 len1 = len;
437163fa5caSBlue Swirl             }
438163fa5caSBlue Swirl 
439163fa5caSBlue Swirl             for (; len1; len1--) {
440163fa5caSBlue Swirl                 int reg = cwp * 16 + 8 + (off >> 2);
441163fa5caSBlue Swirl                 union {
442163fa5caSBlue Swirl                     uint32_t v;
443163fa5caSBlue Swirl                     uint8_t c[4];
444163fa5caSBlue Swirl                 } u;
445163fa5caSBlue Swirl                 u.v = cpu_to_be32(env->regbase[reg]);
446163fa5caSBlue Swirl                 *buf++ = u.c[off & 3];
447163fa5caSBlue Swirl                 addr++;
448163fa5caSBlue Swirl                 len--;
449163fa5caSBlue Swirl                 off++;
450163fa5caSBlue Swirl             }
451163fa5caSBlue Swirl 
452163fa5caSBlue Swirl             if (len == 0) {
453163fa5caSBlue Swirl                 return 0;
454163fa5caSBlue Swirl             }
455163fa5caSBlue Swirl         }
456163fa5caSBlue Swirl     }
457f17ec444SAndreas Färber     return cpu_memory_rw_debug(cs, addr, buf, len, is_write);
458163fa5caSBlue Swirl }
459163fa5caSBlue Swirl 
460163fa5caSBlue Swirl #else /* !TARGET_SPARC64 */
461163fa5caSBlue Swirl 
462163fa5caSBlue Swirl /* 41 bit physical address space */
463a8170e5eSAvi Kivity static inline hwaddr ultrasparc_truncate_physical(uint64_t x)
464163fa5caSBlue Swirl {
465163fa5caSBlue Swirl     return x & 0x1ffffffffffULL;
466163fa5caSBlue Swirl }
467163fa5caSBlue Swirl 
468163fa5caSBlue Swirl /*
469163fa5caSBlue Swirl  * UltraSparc IIi I/DMMUs
470163fa5caSBlue Swirl  */
471163fa5caSBlue Swirl 
472163fa5caSBlue Swirl /* Returns true if TTE tag is valid and matches virtual address value
473163fa5caSBlue Swirl    in context requires virtual address mask value calculated from TTE
474163fa5caSBlue Swirl    entry size */
475163fa5caSBlue Swirl static inline int ultrasparc_tag_match(SparcTLBEntry *tlb,
476163fa5caSBlue Swirl                                        uint64_t address, uint64_t context,
477a8170e5eSAvi Kivity                                        hwaddr *physical)
478163fa5caSBlue Swirl {
479913b5f28SArtyom Tarasenko     uint64_t mask = -(8192ULL << 3 * TTE_PGSIZE(tlb->tte));
480163fa5caSBlue Swirl 
481163fa5caSBlue Swirl     /* valid, context match, virtual address match? */
482163fa5caSBlue Swirl     if (TTE_IS_VALID(tlb->tte) &&
483163fa5caSBlue Swirl         (TTE_IS_GLOBAL(tlb->tte) || tlb_compare_context(tlb, context))
484163fa5caSBlue Swirl         && compare_masked(address, tlb->tag, mask)) {
485163fa5caSBlue Swirl         /* decode physical address */
486163fa5caSBlue Swirl         *physical = ((tlb->tte & mask) | (address & ~mask)) & 0x1ffffffe000ULL;
487163fa5caSBlue Swirl         return 1;
488163fa5caSBlue Swirl     }
489163fa5caSBlue Swirl 
490163fa5caSBlue Swirl     return 0;
491163fa5caSBlue Swirl }
492163fa5caSBlue Swirl 
493c5f9864eSAndreas Färber static int get_physical_address_data(CPUSPARCState *env,
494a8170e5eSAvi Kivity                                      hwaddr *physical, int *prot,
495163fa5caSBlue Swirl                                      target_ulong address, int rw, int mmu_idx)
496163fa5caSBlue Swirl {
49727103424SAndreas Färber     CPUState *cs = CPU(sparc_env_get_cpu(env));
498163fa5caSBlue Swirl     unsigned int i;
499163fa5caSBlue Swirl     uint64_t context;
500163fa5caSBlue Swirl     uint64_t sfsr = 0;
501af7a06baSRichard Henderson     bool is_user = false;
502163fa5caSBlue Swirl 
503163fa5caSBlue Swirl     switch (mmu_idx) {
504af7a06baSRichard Henderson     case MMU_PHYS_IDX:
505af7a06baSRichard Henderson         g_assert_not_reached();
506163fa5caSBlue Swirl     case MMU_USER_IDX:
507af7a06baSRichard Henderson         is_user = true;
508af7a06baSRichard Henderson         /* fallthru */
509163fa5caSBlue Swirl     case MMU_KERNEL_IDX:
510163fa5caSBlue Swirl         context = env->dmmu.mmu_primary_context & 0x1fff;
511163fa5caSBlue Swirl         sfsr |= SFSR_CT_PRIMARY;
512163fa5caSBlue Swirl         break;
513163fa5caSBlue Swirl     case MMU_USER_SECONDARY_IDX:
514af7a06baSRichard Henderson         is_user = true;
515af7a06baSRichard Henderson         /* fallthru */
516163fa5caSBlue Swirl     case MMU_KERNEL_SECONDARY_IDX:
517163fa5caSBlue Swirl         context = env->dmmu.mmu_secondary_context & 0x1fff;
518163fa5caSBlue Swirl         sfsr |= SFSR_CT_SECONDARY;
519163fa5caSBlue Swirl         break;
520163fa5caSBlue Swirl     case MMU_NUCLEUS_IDX:
521163fa5caSBlue Swirl         sfsr |= SFSR_CT_NUCLEUS;
522163fa5caSBlue Swirl         /* FALLTHRU */
523163fa5caSBlue Swirl     default:
524163fa5caSBlue Swirl         context = 0;
525163fa5caSBlue Swirl         break;
526163fa5caSBlue Swirl     }
527163fa5caSBlue Swirl 
528163fa5caSBlue Swirl     if (rw == 1) {
529163fa5caSBlue Swirl         sfsr |= SFSR_WRITE_BIT;
530163fa5caSBlue Swirl     } else if (rw == 4) {
531163fa5caSBlue Swirl         sfsr |= SFSR_NF_BIT;
532163fa5caSBlue Swirl     }
533163fa5caSBlue Swirl 
534163fa5caSBlue Swirl     for (i = 0; i < 64; i++) {
535163fa5caSBlue Swirl         /* ctx match, vaddr match, valid? */
536163fa5caSBlue Swirl         if (ultrasparc_tag_match(&env->dtlb[i], address, context, physical)) {
537163fa5caSBlue Swirl             int do_fault = 0;
538163fa5caSBlue Swirl 
539163fa5caSBlue Swirl             /* access ok? */
540163fa5caSBlue Swirl             /* multiple bits in SFSR.FT may be set on TT_DFAULT */
541163fa5caSBlue Swirl             if (TTE_IS_PRIV(env->dtlb[i].tte) && is_user) {
542163fa5caSBlue Swirl                 do_fault = 1;
543163fa5caSBlue Swirl                 sfsr |= SFSR_FT_PRIV_BIT; /* privilege violation */
544ec0ceb17SBlue Swirl                 trace_mmu_helper_dfault(address, context, mmu_idx, env->tl);
545163fa5caSBlue Swirl             }
546163fa5caSBlue Swirl             if (rw == 4) {
547163fa5caSBlue Swirl                 if (TTE_IS_SIDEEFFECT(env->dtlb[i].tte)) {
548163fa5caSBlue Swirl                     do_fault = 1;
549163fa5caSBlue Swirl                     sfsr |= SFSR_FT_NF_E_BIT;
550163fa5caSBlue Swirl                 }
551163fa5caSBlue Swirl             } else {
552163fa5caSBlue Swirl                 if (TTE_IS_NFO(env->dtlb[i].tte)) {
553163fa5caSBlue Swirl                     do_fault = 1;
554163fa5caSBlue Swirl                     sfsr |= SFSR_FT_NFO_BIT;
555163fa5caSBlue Swirl                 }
556163fa5caSBlue Swirl             }
557163fa5caSBlue Swirl 
558163fa5caSBlue Swirl             if (do_fault) {
559163fa5caSBlue Swirl                 /* faults above are reported with TT_DFAULT. */
56027103424SAndreas Färber                 cs->exception_index = TT_DFAULT;
561163fa5caSBlue Swirl             } else if (!TTE_IS_W_OK(env->dtlb[i].tte) && (rw == 1)) {
562163fa5caSBlue Swirl                 do_fault = 1;
56327103424SAndreas Färber                 cs->exception_index = TT_DPROT;
564163fa5caSBlue Swirl 
565ec0ceb17SBlue Swirl                 trace_mmu_helper_dprot(address, context, mmu_idx, env->tl);
566163fa5caSBlue Swirl             }
567163fa5caSBlue Swirl 
568163fa5caSBlue Swirl             if (!do_fault) {
569163fa5caSBlue Swirl                 *prot = PAGE_READ;
570163fa5caSBlue Swirl                 if (TTE_IS_W_OK(env->dtlb[i].tte)) {
571163fa5caSBlue Swirl                     *prot |= PAGE_WRITE;
572163fa5caSBlue Swirl                 }
573163fa5caSBlue Swirl 
574163fa5caSBlue Swirl                 TTE_SET_USED(env->dtlb[i].tte);
575163fa5caSBlue Swirl 
576163fa5caSBlue Swirl                 return 0;
577163fa5caSBlue Swirl             }
578163fa5caSBlue Swirl 
579163fa5caSBlue Swirl             if (env->dmmu.sfsr & SFSR_VALID_BIT) { /* Fault status register */
580163fa5caSBlue Swirl                 sfsr |= SFSR_OW_BIT; /* overflow (not read before
581163fa5caSBlue Swirl                                         another fault) */
582163fa5caSBlue Swirl             }
583163fa5caSBlue Swirl 
584163fa5caSBlue Swirl             if (env->pstate & PS_PRIV) {
585163fa5caSBlue Swirl                 sfsr |= SFSR_PR_BIT;
586163fa5caSBlue Swirl             }
587163fa5caSBlue Swirl 
588163fa5caSBlue Swirl             /* FIXME: ASI field in SFSR must be set */
589163fa5caSBlue Swirl             env->dmmu.sfsr = sfsr | SFSR_VALID_BIT;
590163fa5caSBlue Swirl 
591163fa5caSBlue Swirl             env->dmmu.sfar = address; /* Fault address register */
592163fa5caSBlue Swirl 
593163fa5caSBlue Swirl             env->dmmu.tag_access = (address & ~0x1fffULL) | context;
594163fa5caSBlue Swirl 
595163fa5caSBlue Swirl             return 1;
596163fa5caSBlue Swirl         }
597163fa5caSBlue Swirl     }
598163fa5caSBlue Swirl 
599ec0ceb17SBlue Swirl     trace_mmu_helper_dmiss(address, context);
600163fa5caSBlue Swirl 
601163fa5caSBlue Swirl     /*
602163fa5caSBlue Swirl      * On MMU misses:
603163fa5caSBlue Swirl      * - UltraSPARC IIi: SFSR and SFAR unmodified
604163fa5caSBlue Swirl      * - JPS1: SFAR updated and some fields of SFSR updated
605163fa5caSBlue Swirl      */
606163fa5caSBlue Swirl     env->dmmu.tag_access = (address & ~0x1fffULL) | context;
60727103424SAndreas Färber     cs->exception_index = TT_DMISS;
608163fa5caSBlue Swirl     return 1;
609163fa5caSBlue Swirl }
610163fa5caSBlue Swirl 
611c5f9864eSAndreas Färber static int get_physical_address_code(CPUSPARCState *env,
612a8170e5eSAvi Kivity                                      hwaddr *physical, int *prot,
613163fa5caSBlue Swirl                                      target_ulong address, int mmu_idx)
614163fa5caSBlue Swirl {
61527103424SAndreas Färber     CPUState *cs = CPU(sparc_env_get_cpu(env));
616163fa5caSBlue Swirl     unsigned int i;
617163fa5caSBlue Swirl     uint64_t context;
618af7a06baSRichard Henderson     bool is_user = false;
619163fa5caSBlue Swirl 
620af7a06baSRichard Henderson     switch (mmu_idx) {
621af7a06baSRichard Henderson     case MMU_PHYS_IDX:
622af7a06baSRichard Henderson     case MMU_USER_SECONDARY_IDX:
623af7a06baSRichard Henderson     case MMU_KERNEL_SECONDARY_IDX:
624af7a06baSRichard Henderson         g_assert_not_reached();
625af7a06baSRichard Henderson     case MMU_USER_IDX:
626af7a06baSRichard Henderson         is_user = true;
627af7a06baSRichard Henderson         /* fallthru */
628af7a06baSRichard Henderson     case MMU_KERNEL_IDX:
629af7a06baSRichard Henderson         context = env->dmmu.mmu_primary_context & 0x1fff;
630af7a06baSRichard Henderson         break;
631af7a06baSRichard Henderson     default:
632af7a06baSRichard Henderson         context = 0;
633af7a06baSRichard Henderson         break;
634163fa5caSBlue Swirl     }
635163fa5caSBlue Swirl 
636163fa5caSBlue Swirl     if (env->tl == 0) {
637163fa5caSBlue Swirl         /* PRIMARY context */
638163fa5caSBlue Swirl         context = env->dmmu.mmu_primary_context & 0x1fff;
639163fa5caSBlue Swirl     } else {
640163fa5caSBlue Swirl         /* NUCLEUS context */
641163fa5caSBlue Swirl         context = 0;
642163fa5caSBlue Swirl     }
643163fa5caSBlue Swirl 
644163fa5caSBlue Swirl     for (i = 0; i < 64; i++) {
645163fa5caSBlue Swirl         /* ctx match, vaddr match, valid? */
646163fa5caSBlue Swirl         if (ultrasparc_tag_match(&env->itlb[i],
647163fa5caSBlue Swirl                                  address, context, physical)) {
648163fa5caSBlue Swirl             /* access ok? */
649163fa5caSBlue Swirl             if (TTE_IS_PRIV(env->itlb[i].tte) && is_user) {
650163fa5caSBlue Swirl                 /* Fault status register */
651163fa5caSBlue Swirl                 if (env->immu.sfsr & SFSR_VALID_BIT) {
652163fa5caSBlue Swirl                     env->immu.sfsr = SFSR_OW_BIT; /* overflow (not read before
653163fa5caSBlue Swirl                                                      another fault) */
654163fa5caSBlue Swirl                 } else {
655163fa5caSBlue Swirl                     env->immu.sfsr = 0;
656163fa5caSBlue Swirl                 }
657163fa5caSBlue Swirl                 if (env->pstate & PS_PRIV) {
658163fa5caSBlue Swirl                     env->immu.sfsr |= SFSR_PR_BIT;
659163fa5caSBlue Swirl                 }
660163fa5caSBlue Swirl                 if (env->tl > 0) {
661163fa5caSBlue Swirl                     env->immu.sfsr |= SFSR_CT_NUCLEUS;
662163fa5caSBlue Swirl                 }
663163fa5caSBlue Swirl 
664163fa5caSBlue Swirl                 /* FIXME: ASI field in SFSR must be set */
665163fa5caSBlue Swirl                 env->immu.sfsr |= SFSR_FT_PRIV_BIT | SFSR_VALID_BIT;
66627103424SAndreas Färber                 cs->exception_index = TT_TFAULT;
667163fa5caSBlue Swirl 
668163fa5caSBlue Swirl                 env->immu.tag_access = (address & ~0x1fffULL) | context;
669163fa5caSBlue Swirl 
670ec0ceb17SBlue Swirl                 trace_mmu_helper_tfault(address, context);
671163fa5caSBlue Swirl 
672163fa5caSBlue Swirl                 return 1;
673163fa5caSBlue Swirl             }
674163fa5caSBlue Swirl             *prot = PAGE_EXEC;
675163fa5caSBlue Swirl             TTE_SET_USED(env->itlb[i].tte);
676163fa5caSBlue Swirl             return 0;
677163fa5caSBlue Swirl         }
678163fa5caSBlue Swirl     }
679163fa5caSBlue Swirl 
680ec0ceb17SBlue Swirl     trace_mmu_helper_tmiss(address, context);
681163fa5caSBlue Swirl 
682163fa5caSBlue Swirl     /* Context is stored in DMMU (dmmuregs[1]) also for IMMU */
683163fa5caSBlue Swirl     env->immu.tag_access = (address & ~0x1fffULL) | context;
68427103424SAndreas Färber     cs->exception_index = TT_TMISS;
685163fa5caSBlue Swirl     return 1;
686163fa5caSBlue Swirl }
687163fa5caSBlue Swirl 
688a8170e5eSAvi Kivity static int get_physical_address(CPUSPARCState *env, hwaddr *physical,
689163fa5caSBlue Swirl                                 int *prot, int *access_index,
690163fa5caSBlue Swirl                                 target_ulong address, int rw, int mmu_idx,
691163fa5caSBlue Swirl                                 target_ulong *page_size)
692163fa5caSBlue Swirl {
693163fa5caSBlue Swirl     /* ??? We treat everything as a small page, then explicitly flush
694163fa5caSBlue Swirl        everything when an entry is evicted.  */
695163fa5caSBlue Swirl     *page_size = TARGET_PAGE_SIZE;
696163fa5caSBlue Swirl 
697163fa5caSBlue Swirl     /* safety net to catch wrong softmmu index use from dynamic code */
698163fa5caSBlue Swirl     if (env->tl > 0 && mmu_idx != MMU_NUCLEUS_IDX) {
699ec0ceb17SBlue Swirl         if (rw == 2) {
700ec0ceb17SBlue Swirl             trace_mmu_helper_get_phys_addr_code(env->tl, mmu_idx,
701ec0ceb17SBlue Swirl                                                 env->dmmu.mmu_primary_context,
702ec0ceb17SBlue Swirl                                                 env->dmmu.mmu_secondary_context,
703ec0ceb17SBlue Swirl                                                 address);
704ec0ceb17SBlue Swirl         } else {
705ec0ceb17SBlue Swirl             trace_mmu_helper_get_phys_addr_data(env->tl, mmu_idx,
706163fa5caSBlue Swirl                                                 env->dmmu.mmu_primary_context,
707163fa5caSBlue Swirl                                                 env->dmmu.mmu_secondary_context,
708163fa5caSBlue Swirl                                                 address);
709163fa5caSBlue Swirl         }
710ec0ceb17SBlue Swirl     }
711163fa5caSBlue Swirl 
712af7a06baSRichard Henderson     if (mmu_idx == MMU_PHYS_IDX) {
713af7a06baSRichard Henderson         *physical = ultrasparc_truncate_physical(address);
714af7a06baSRichard Henderson         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
715af7a06baSRichard Henderson         return 0;
716af7a06baSRichard Henderson     }
717af7a06baSRichard Henderson 
718163fa5caSBlue Swirl     if (rw == 2) {
719163fa5caSBlue Swirl         return get_physical_address_code(env, physical, prot, address,
720163fa5caSBlue Swirl                                          mmu_idx);
721163fa5caSBlue Swirl     } else {
722163fa5caSBlue Swirl         return get_physical_address_data(env, physical, prot, address, rw,
723163fa5caSBlue Swirl                                          mmu_idx);
724163fa5caSBlue Swirl     }
725163fa5caSBlue Swirl }
726163fa5caSBlue Swirl 
727163fa5caSBlue Swirl /* Perform address translation */
728*e84942f2SRichard Henderson bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
729*e84942f2SRichard Henderson                         MMUAccessType access_type, int mmu_idx,
730*e84942f2SRichard Henderson                         bool probe, uintptr_t retaddr)
731163fa5caSBlue Swirl {
7327510454eSAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
7337510454eSAndreas Färber     CPUSPARCState *env = &cpu->env;
7341658dd32SBlue Swirl     target_ulong vaddr;
735a8170e5eSAvi Kivity     hwaddr paddr;
736163fa5caSBlue Swirl     target_ulong page_size;
737163fa5caSBlue Swirl     int error_code = 0, prot, access_index;
738163fa5caSBlue Swirl 
7391658dd32SBlue Swirl     address &= TARGET_PAGE_MASK;
740163fa5caSBlue Swirl     error_code = get_physical_address(env, &paddr, &prot, &access_index,
741*e84942f2SRichard Henderson                                       address, access_type,
742*e84942f2SRichard Henderson                                       mmu_idx, &page_size);
743*e84942f2SRichard Henderson     if (likely(error_code == 0)) {
7441658dd32SBlue Swirl         vaddr = address;
745163fa5caSBlue Swirl 
746ec0ceb17SBlue Swirl         trace_mmu_helper_mmu_fault(address, paddr, mmu_idx, env->tl,
747163fa5caSBlue Swirl                                    env->dmmu.mmu_primary_context,
748163fa5caSBlue Swirl                                    env->dmmu.mmu_secondary_context);
749163fa5caSBlue Swirl 
7500c591eb0SAndreas Färber         tlb_set_page(cs, vaddr, paddr, prot, mmu_idx, page_size);
751*e84942f2SRichard Henderson         return true;
752163fa5caSBlue Swirl     }
753*e84942f2SRichard Henderson     if (probe) {
754*e84942f2SRichard Henderson         return false;
755*e84942f2SRichard Henderson     }
756*e84942f2SRichard Henderson     cpu_loop_exit_restore(cs, retaddr);
757163fa5caSBlue Swirl }
758163fa5caSBlue Swirl 
759fad866daSMarkus Armbruster void dump_mmu(CPUSPARCState *env)
760163fa5caSBlue Swirl {
761163fa5caSBlue Swirl     unsigned int i;
762163fa5caSBlue Swirl     const char *mask;
763163fa5caSBlue Swirl 
764fad866daSMarkus Armbruster     qemu_printf("MMU contexts: Primary: %" PRId64 ", Secondary: %"
765163fa5caSBlue Swirl                 PRId64 "\n",
766163fa5caSBlue Swirl                 env->dmmu.mmu_primary_context,
767163fa5caSBlue Swirl                 env->dmmu.mmu_secondary_context);
768fad866daSMarkus Armbruster     qemu_printf("DMMU Tag Access: %" PRIx64 ", TSB Tag Target: %" PRIx64
769d00a2334SArtyom Tarasenko                 "\n", env->dmmu.tag_access, env->dmmu.tsb_tag_target);
770163fa5caSBlue Swirl     if ((env->lsu & DMMU_E) == 0) {
771fad866daSMarkus Armbruster         qemu_printf("DMMU disabled\n");
772163fa5caSBlue Swirl     } else {
773fad866daSMarkus Armbruster         qemu_printf("DMMU dump\n");
774163fa5caSBlue Swirl         for (i = 0; i < 64; i++) {
775163fa5caSBlue Swirl             switch (TTE_PGSIZE(env->dtlb[i].tte)) {
776163fa5caSBlue Swirl             default:
777163fa5caSBlue Swirl             case 0x0:
778163fa5caSBlue Swirl                 mask = "  8k";
779163fa5caSBlue Swirl                 break;
780163fa5caSBlue Swirl             case 0x1:
781163fa5caSBlue Swirl                 mask = " 64k";
782163fa5caSBlue Swirl                 break;
783163fa5caSBlue Swirl             case 0x2:
784163fa5caSBlue Swirl                 mask = "512k";
785163fa5caSBlue Swirl                 break;
786163fa5caSBlue Swirl             case 0x3:
787163fa5caSBlue Swirl                 mask = "  4M";
788163fa5caSBlue Swirl                 break;
789163fa5caSBlue Swirl             }
790163fa5caSBlue Swirl             if (TTE_IS_VALID(env->dtlb[i].tte)) {
791fad866daSMarkus Armbruster                 qemu_printf("[%02u] VA: %" PRIx64 ", PA: %llx"
792163fa5caSBlue Swirl                             ", %s, %s, %s, %s, ctx %" PRId64 " %s\n",
793163fa5caSBlue Swirl                             i,
794163fa5caSBlue Swirl                             env->dtlb[i].tag & (uint64_t)~0x1fffULL,
795163fa5caSBlue Swirl                             TTE_PA(env->dtlb[i].tte),
796163fa5caSBlue Swirl                             mask,
797163fa5caSBlue Swirl                             TTE_IS_PRIV(env->dtlb[i].tte) ? "priv" : "user",
798163fa5caSBlue Swirl                             TTE_IS_W_OK(env->dtlb[i].tte) ? "RW" : "RO",
799163fa5caSBlue Swirl                             TTE_IS_LOCKED(env->dtlb[i].tte) ?
800163fa5caSBlue Swirl                             "locked" : "unlocked",
801163fa5caSBlue Swirl                             env->dtlb[i].tag & (uint64_t)0x1fffULL,
802163fa5caSBlue Swirl                             TTE_IS_GLOBAL(env->dtlb[i].tte) ?
803163fa5caSBlue Swirl                             "global" : "local");
804163fa5caSBlue Swirl             }
805163fa5caSBlue Swirl         }
806163fa5caSBlue Swirl     }
807163fa5caSBlue Swirl     if ((env->lsu & IMMU_E) == 0) {
808fad866daSMarkus Armbruster         qemu_printf("IMMU disabled\n");
809163fa5caSBlue Swirl     } else {
810fad866daSMarkus Armbruster         qemu_printf("IMMU dump\n");
811163fa5caSBlue Swirl         for (i = 0; i < 64; i++) {
812163fa5caSBlue Swirl             switch (TTE_PGSIZE(env->itlb[i].tte)) {
813163fa5caSBlue Swirl             default:
814163fa5caSBlue Swirl             case 0x0:
815163fa5caSBlue Swirl                 mask = "  8k";
816163fa5caSBlue Swirl                 break;
817163fa5caSBlue Swirl             case 0x1:
818163fa5caSBlue Swirl                 mask = " 64k";
819163fa5caSBlue Swirl                 break;
820163fa5caSBlue Swirl             case 0x2:
821163fa5caSBlue Swirl                 mask = "512k";
822163fa5caSBlue Swirl                 break;
823163fa5caSBlue Swirl             case 0x3:
824163fa5caSBlue Swirl                 mask = "  4M";
825163fa5caSBlue Swirl                 break;
826163fa5caSBlue Swirl             }
827163fa5caSBlue Swirl             if (TTE_IS_VALID(env->itlb[i].tte)) {
828fad866daSMarkus Armbruster                 qemu_printf("[%02u] VA: %" PRIx64 ", PA: %llx"
829163fa5caSBlue Swirl                             ", %s, %s, %s, ctx %" PRId64 " %s\n",
830163fa5caSBlue Swirl                             i,
831163fa5caSBlue Swirl                             env->itlb[i].tag & (uint64_t)~0x1fffULL,
832163fa5caSBlue Swirl                             TTE_PA(env->itlb[i].tte),
833163fa5caSBlue Swirl                             mask,
834163fa5caSBlue Swirl                             TTE_IS_PRIV(env->itlb[i].tte) ? "priv" : "user",
835163fa5caSBlue Swirl                             TTE_IS_LOCKED(env->itlb[i].tte) ?
836163fa5caSBlue Swirl                             "locked" : "unlocked",
837163fa5caSBlue Swirl                             env->itlb[i].tag & (uint64_t)0x1fffULL,
838163fa5caSBlue Swirl                             TTE_IS_GLOBAL(env->itlb[i].tte) ?
839163fa5caSBlue Swirl                             "global" : "local");
840163fa5caSBlue Swirl             }
841163fa5caSBlue Swirl         }
842163fa5caSBlue Swirl     }
843163fa5caSBlue Swirl }
844163fa5caSBlue Swirl 
845163fa5caSBlue Swirl #endif /* TARGET_SPARC64 */
846163fa5caSBlue Swirl 
847a8170e5eSAvi Kivity static int cpu_sparc_get_phys_page(CPUSPARCState *env, hwaddr *phys,
848163fa5caSBlue Swirl                                    target_ulong addr, int rw, int mmu_idx)
849163fa5caSBlue Swirl {
850163fa5caSBlue Swirl     target_ulong page_size;
851163fa5caSBlue Swirl     int prot, access_index;
852163fa5caSBlue Swirl 
853163fa5caSBlue Swirl     return get_physical_address(env, phys, &prot, &access_index, addr, rw,
854163fa5caSBlue Swirl                                 mmu_idx, &page_size);
855163fa5caSBlue Swirl }
856163fa5caSBlue Swirl 
857163fa5caSBlue Swirl #if defined(TARGET_SPARC64)
858a8170e5eSAvi Kivity hwaddr cpu_get_phys_page_nofault(CPUSPARCState *env, target_ulong addr,
859163fa5caSBlue Swirl                                            int mmu_idx)
860163fa5caSBlue Swirl {
861a8170e5eSAvi Kivity     hwaddr phys_addr;
862163fa5caSBlue Swirl 
863163fa5caSBlue Swirl     if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 4, mmu_idx) != 0) {
864163fa5caSBlue Swirl         return -1;
865163fa5caSBlue Swirl     }
866163fa5caSBlue Swirl     return phys_addr;
867163fa5caSBlue Swirl }
868163fa5caSBlue Swirl #endif
869163fa5caSBlue Swirl 
87000b941e5SAndreas Färber hwaddr sparc_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
871163fa5caSBlue Swirl {
87200b941e5SAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
87300b941e5SAndreas Färber     CPUSPARCState *env = &cpu->env;
874a8170e5eSAvi Kivity     hwaddr phys_addr;
87597ed5ccdSBenjamin Herrenschmidt     int mmu_idx = cpu_mmu_index(env, false);
876163fa5caSBlue Swirl 
877163fa5caSBlue Swirl     if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 2, mmu_idx) != 0) {
878163fa5caSBlue Swirl         if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 0, mmu_idx) != 0) {
879163fa5caSBlue Swirl             return -1;
880163fa5caSBlue Swirl         }
881163fa5caSBlue Swirl     }
882163fa5caSBlue Swirl     return phys_addr;
883163fa5caSBlue Swirl }
884163fa5caSBlue Swirl #endif
885