xref: /qemu/target/sparc/mmu_helper.c (revision 00b941e581b5c42645f836ef530705bb76a3e6bb)
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 
20163fa5caSBlue Swirl #include "cpu.h"
21ec0ceb17SBlue Swirl #include "trace.h"
22022c62cbSPaolo Bonzini #include "exec/address-spaces.h"
23163fa5caSBlue Swirl 
24163fa5caSBlue Swirl /* Sparc MMU emulation */
25163fa5caSBlue Swirl 
26163fa5caSBlue Swirl #if defined(CONFIG_USER_ONLY)
27163fa5caSBlue Swirl 
28c5f9864eSAndreas Färber int cpu_sparc_handle_mmu_fault(CPUSPARCState *env1, target_ulong address, int rw,
29163fa5caSBlue Swirl                                int mmu_idx)
30163fa5caSBlue Swirl {
31163fa5caSBlue Swirl     if (rw & 2) {
32163fa5caSBlue Swirl         env1->exception_index = TT_TFAULT;
33163fa5caSBlue Swirl     } else {
34163fa5caSBlue Swirl         env1->exception_index = TT_DFAULT;
35163fa5caSBlue Swirl     }
36163fa5caSBlue Swirl     return 1;
37163fa5caSBlue Swirl }
38163fa5caSBlue Swirl 
39163fa5caSBlue Swirl #else
40163fa5caSBlue Swirl 
41163fa5caSBlue Swirl #ifndef TARGET_SPARC64
42163fa5caSBlue Swirl /*
43163fa5caSBlue Swirl  * Sparc V8 Reference MMU (SRMMU)
44163fa5caSBlue Swirl  */
45163fa5caSBlue Swirl static const int access_table[8][8] = {
46163fa5caSBlue Swirl     { 0, 0, 0, 0, 8, 0, 12, 12 },
47163fa5caSBlue Swirl     { 0, 0, 0, 0, 8, 0, 0, 0 },
48163fa5caSBlue Swirl     { 8, 8, 0, 0, 0, 8, 12, 12 },
49163fa5caSBlue Swirl     { 8, 8, 0, 0, 0, 8, 0, 0 },
50163fa5caSBlue Swirl     { 8, 0, 8, 0, 8, 8, 12, 12 },
51163fa5caSBlue Swirl     { 8, 0, 8, 0, 8, 0, 8, 0 },
52163fa5caSBlue Swirl     { 8, 8, 8, 0, 8, 8, 12, 12 },
53163fa5caSBlue Swirl     { 8, 8, 8, 0, 8, 8, 8, 0 }
54163fa5caSBlue Swirl };
55163fa5caSBlue Swirl 
56163fa5caSBlue Swirl static const int perm_table[2][8] = {
57163fa5caSBlue Swirl     {
58163fa5caSBlue Swirl         PAGE_READ,
59163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE,
60163fa5caSBlue Swirl         PAGE_READ | PAGE_EXEC,
61163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
62163fa5caSBlue Swirl         PAGE_EXEC,
63163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE,
64163fa5caSBlue Swirl         PAGE_READ | PAGE_EXEC,
65163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE | PAGE_EXEC
66163fa5caSBlue Swirl     },
67163fa5caSBlue Swirl     {
68163fa5caSBlue Swirl         PAGE_READ,
69163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE,
70163fa5caSBlue Swirl         PAGE_READ | PAGE_EXEC,
71163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
72163fa5caSBlue Swirl         PAGE_EXEC,
73163fa5caSBlue Swirl         PAGE_READ,
74163fa5caSBlue Swirl         0,
75163fa5caSBlue Swirl         0,
76163fa5caSBlue Swirl     }
77163fa5caSBlue Swirl };
78163fa5caSBlue Swirl 
79a8170e5eSAvi Kivity static int get_physical_address(CPUSPARCState *env, hwaddr *physical,
80163fa5caSBlue Swirl                                 int *prot, int *access_index,
81163fa5caSBlue Swirl                                 target_ulong address, int rw, int mmu_idx,
82163fa5caSBlue Swirl                                 target_ulong *page_size)
83163fa5caSBlue Swirl {
84163fa5caSBlue Swirl     int access_perms = 0;
85a8170e5eSAvi Kivity     hwaddr pde_ptr;
86163fa5caSBlue Swirl     uint32_t pde;
87163fa5caSBlue Swirl     int error_code = 0, is_dirty, is_user;
88163fa5caSBlue Swirl     unsigned long page_offset;
89163fa5caSBlue Swirl 
90163fa5caSBlue Swirl     is_user = mmu_idx == MMU_USER_IDX;
91163fa5caSBlue Swirl 
92163fa5caSBlue Swirl     if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
93163fa5caSBlue Swirl         *page_size = TARGET_PAGE_SIZE;
94163fa5caSBlue Swirl         /* Boot mode: instruction fetches are taken from PROM */
95163fa5caSBlue Swirl         if (rw == 2 && (env->mmuregs[0] & env->def->mmu_bm)) {
96163fa5caSBlue Swirl             *physical = env->prom_addr | (address & 0x7ffffULL);
97163fa5caSBlue Swirl             *prot = PAGE_READ | PAGE_EXEC;
98163fa5caSBlue Swirl             return 0;
99163fa5caSBlue Swirl         }
100163fa5caSBlue Swirl         *physical = address;
101163fa5caSBlue Swirl         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
102163fa5caSBlue Swirl         return 0;
103163fa5caSBlue Swirl     }
104163fa5caSBlue Swirl 
105163fa5caSBlue Swirl     *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user ? 0 : 1);
106163fa5caSBlue Swirl     *physical = 0xffffffffffff0000ULL;
107163fa5caSBlue Swirl 
108163fa5caSBlue Swirl     /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
109163fa5caSBlue Swirl     /* Context base + context number */
110163fa5caSBlue Swirl     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
111163fa5caSBlue Swirl     pde = ldl_phys(pde_ptr);
112163fa5caSBlue Swirl 
113163fa5caSBlue Swirl     /* Ctx pde */
114163fa5caSBlue Swirl     switch (pde & PTE_ENTRYTYPE_MASK) {
115163fa5caSBlue Swirl     default:
116163fa5caSBlue Swirl     case 0: /* Invalid */
117163fa5caSBlue Swirl         return 1 << 2;
118163fa5caSBlue Swirl     case 2: /* L0 PTE, maybe should not happen? */
119163fa5caSBlue Swirl     case 3: /* Reserved */
120163fa5caSBlue Swirl         return 4 << 2;
121163fa5caSBlue Swirl     case 1: /* L0 PDE */
122163fa5caSBlue Swirl         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
123163fa5caSBlue Swirl         pde = ldl_phys(pde_ptr);
124163fa5caSBlue Swirl 
125163fa5caSBlue Swirl         switch (pde & PTE_ENTRYTYPE_MASK) {
126163fa5caSBlue Swirl         default:
127163fa5caSBlue Swirl         case 0: /* Invalid */
128163fa5caSBlue Swirl             return (1 << 8) | (1 << 2);
129163fa5caSBlue Swirl         case 3: /* Reserved */
130163fa5caSBlue Swirl             return (1 << 8) | (4 << 2);
131163fa5caSBlue Swirl         case 1: /* L1 PDE */
132163fa5caSBlue Swirl             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
133163fa5caSBlue Swirl             pde = ldl_phys(pde_ptr);
134163fa5caSBlue Swirl 
135163fa5caSBlue Swirl             switch (pde & PTE_ENTRYTYPE_MASK) {
136163fa5caSBlue Swirl             default:
137163fa5caSBlue Swirl             case 0: /* Invalid */
138163fa5caSBlue Swirl                 return (2 << 8) | (1 << 2);
139163fa5caSBlue Swirl             case 3: /* Reserved */
140163fa5caSBlue Swirl                 return (2 << 8) | (4 << 2);
141163fa5caSBlue Swirl             case 1: /* L2 PDE */
142163fa5caSBlue Swirl                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
143163fa5caSBlue Swirl                 pde = ldl_phys(pde_ptr);
144163fa5caSBlue Swirl 
145163fa5caSBlue Swirl                 switch (pde & PTE_ENTRYTYPE_MASK) {
146163fa5caSBlue Swirl                 default:
147163fa5caSBlue Swirl                 case 0: /* Invalid */
148163fa5caSBlue Swirl                     return (3 << 8) | (1 << 2);
149163fa5caSBlue Swirl                 case 1: /* PDE, should not happen */
150163fa5caSBlue Swirl                 case 3: /* Reserved */
151163fa5caSBlue Swirl                     return (3 << 8) | (4 << 2);
152163fa5caSBlue Swirl                 case 2: /* L3 PTE */
1531658dd32SBlue Swirl                     page_offset = 0;
154163fa5caSBlue Swirl                 }
155163fa5caSBlue Swirl                 *page_size = TARGET_PAGE_SIZE;
156163fa5caSBlue Swirl                 break;
157163fa5caSBlue Swirl             case 2: /* L2 PTE */
1581658dd32SBlue Swirl                 page_offset = address & 0x3f000;
159163fa5caSBlue Swirl                 *page_size = 0x40000;
160163fa5caSBlue Swirl             }
161163fa5caSBlue Swirl             break;
162163fa5caSBlue Swirl         case 2: /* L1 PTE */
1631658dd32SBlue Swirl             page_offset = address & 0xfff000;
164163fa5caSBlue Swirl             *page_size = 0x1000000;
165163fa5caSBlue Swirl         }
166163fa5caSBlue Swirl     }
167163fa5caSBlue Swirl 
168163fa5caSBlue Swirl     /* check access */
169163fa5caSBlue Swirl     access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
170163fa5caSBlue Swirl     error_code = access_table[*access_index][access_perms];
171163fa5caSBlue Swirl     if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user)) {
172163fa5caSBlue Swirl         return error_code;
173163fa5caSBlue Swirl     }
174163fa5caSBlue Swirl 
175163fa5caSBlue Swirl     /* update page modified and dirty bits */
176163fa5caSBlue Swirl     is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
177163fa5caSBlue Swirl     if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
178163fa5caSBlue Swirl         pde |= PG_ACCESSED_MASK;
179163fa5caSBlue Swirl         if (is_dirty) {
180163fa5caSBlue Swirl             pde |= PG_MODIFIED_MASK;
181163fa5caSBlue Swirl         }
182163fa5caSBlue Swirl         stl_phys_notdirty(pde_ptr, pde);
183163fa5caSBlue Swirl     }
184163fa5caSBlue Swirl 
185163fa5caSBlue Swirl     /* the page can be put in the TLB */
186163fa5caSBlue Swirl     *prot = perm_table[is_user][access_perms];
187163fa5caSBlue Swirl     if (!(pde & PG_MODIFIED_MASK)) {
188163fa5caSBlue Swirl         /* only set write access if already dirty... otherwise wait
189163fa5caSBlue Swirl            for dirty access */
190163fa5caSBlue Swirl         *prot &= ~PAGE_WRITE;
191163fa5caSBlue Swirl     }
192163fa5caSBlue Swirl 
193163fa5caSBlue Swirl     /* Even if large ptes, we map only one 4KB page in the cache to
194163fa5caSBlue Swirl        avoid filling it too fast */
195a8170e5eSAvi Kivity     *physical = ((hwaddr)(pde & PTE_ADDR_MASK) << 4) + page_offset;
196163fa5caSBlue Swirl     return error_code;
197163fa5caSBlue Swirl }
198163fa5caSBlue Swirl 
199163fa5caSBlue Swirl /* Perform address translation */
200c5f9864eSAndreas Färber int cpu_sparc_handle_mmu_fault(CPUSPARCState *env, target_ulong address, int rw,
201163fa5caSBlue Swirl                                int mmu_idx)
202163fa5caSBlue Swirl {
203a8170e5eSAvi Kivity     hwaddr paddr;
204163fa5caSBlue Swirl     target_ulong vaddr;
205163fa5caSBlue Swirl     target_ulong page_size;
206163fa5caSBlue Swirl     int error_code = 0, prot, access_index;
207163fa5caSBlue Swirl 
2081658dd32SBlue Swirl     address &= TARGET_PAGE_MASK;
209163fa5caSBlue Swirl     error_code = get_physical_address(env, &paddr, &prot, &access_index,
210163fa5caSBlue Swirl                                       address, rw, mmu_idx, &page_size);
2111658dd32SBlue Swirl     vaddr = address;
212163fa5caSBlue Swirl     if (error_code == 0) {
213163fa5caSBlue Swirl #ifdef DEBUG_MMU
214163fa5caSBlue Swirl         printf("Translate at " TARGET_FMT_lx " -> " TARGET_FMT_plx ", vaddr "
215163fa5caSBlue Swirl                TARGET_FMT_lx "\n", address, paddr, vaddr);
216163fa5caSBlue Swirl #endif
217163fa5caSBlue Swirl         tlb_set_page(env, vaddr, paddr, prot, mmu_idx, page_size);
218163fa5caSBlue Swirl         return 0;
219163fa5caSBlue Swirl     }
220163fa5caSBlue Swirl 
221163fa5caSBlue Swirl     if (env->mmuregs[3]) { /* Fault status register */
222163fa5caSBlue Swirl         env->mmuregs[3] = 1; /* overflow (not read before another fault) */
223163fa5caSBlue Swirl     }
224163fa5caSBlue Swirl     env->mmuregs[3] |= (access_index << 5) | error_code | 2;
225163fa5caSBlue Swirl     env->mmuregs[4] = address; /* Fault address register */
226163fa5caSBlue Swirl 
227163fa5caSBlue Swirl     if ((env->mmuregs[0] & MMU_NF) || env->psret == 0)  {
228163fa5caSBlue Swirl         /* No fault mode: if a mapping is available, just override
229163fa5caSBlue Swirl            permissions. If no mapping is available, redirect accesses to
230163fa5caSBlue Swirl            neverland. Fake/overridden mappings will be flushed when
231163fa5caSBlue Swirl            switching to normal mode. */
232163fa5caSBlue Swirl         prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
233163fa5caSBlue Swirl         tlb_set_page(env, vaddr, paddr, prot, mmu_idx, TARGET_PAGE_SIZE);
234163fa5caSBlue Swirl         return 0;
235163fa5caSBlue Swirl     } else {
236163fa5caSBlue Swirl         if (rw & 2) {
237163fa5caSBlue Swirl             env->exception_index = TT_TFAULT;
238163fa5caSBlue Swirl         } else {
239163fa5caSBlue Swirl             env->exception_index = TT_DFAULT;
240163fa5caSBlue Swirl         }
241163fa5caSBlue Swirl         return 1;
242163fa5caSBlue Swirl     }
243163fa5caSBlue Swirl }
244163fa5caSBlue Swirl 
245c5f9864eSAndreas Färber target_ulong mmu_probe(CPUSPARCState *env, target_ulong address, int mmulev)
246163fa5caSBlue Swirl {
247a8170e5eSAvi Kivity     hwaddr pde_ptr;
248163fa5caSBlue Swirl     uint32_t pde;
249163fa5caSBlue Swirl 
250163fa5caSBlue Swirl     /* Context base + context number */
251a8170e5eSAvi Kivity     pde_ptr = (hwaddr)(env->mmuregs[1] << 4) +
252163fa5caSBlue Swirl         (env->mmuregs[2] << 2);
253163fa5caSBlue Swirl     pde = ldl_phys(pde_ptr);
254163fa5caSBlue Swirl 
255163fa5caSBlue Swirl     switch (pde & PTE_ENTRYTYPE_MASK) {
256163fa5caSBlue Swirl     default:
257163fa5caSBlue Swirl     case 0: /* Invalid */
258163fa5caSBlue Swirl     case 2: /* PTE, maybe should not happen? */
259163fa5caSBlue Swirl     case 3: /* Reserved */
260163fa5caSBlue Swirl         return 0;
261163fa5caSBlue Swirl     case 1: /* L1 PDE */
262163fa5caSBlue Swirl         if (mmulev == 3) {
263163fa5caSBlue Swirl             return pde;
264163fa5caSBlue Swirl         }
265163fa5caSBlue Swirl         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
266163fa5caSBlue Swirl         pde = ldl_phys(pde_ptr);
267163fa5caSBlue Swirl 
268163fa5caSBlue Swirl         switch (pde & PTE_ENTRYTYPE_MASK) {
269163fa5caSBlue Swirl         default:
270163fa5caSBlue Swirl         case 0: /* Invalid */
271163fa5caSBlue Swirl         case 3: /* Reserved */
272163fa5caSBlue Swirl             return 0;
273163fa5caSBlue Swirl         case 2: /* L1 PTE */
274163fa5caSBlue Swirl             return pde;
275163fa5caSBlue Swirl         case 1: /* L2 PDE */
276163fa5caSBlue Swirl             if (mmulev == 2) {
277163fa5caSBlue Swirl                 return pde;
278163fa5caSBlue Swirl             }
279163fa5caSBlue Swirl             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
280163fa5caSBlue Swirl             pde = ldl_phys(pde_ptr);
281163fa5caSBlue Swirl 
282163fa5caSBlue Swirl             switch (pde & PTE_ENTRYTYPE_MASK) {
283163fa5caSBlue Swirl             default:
284163fa5caSBlue Swirl             case 0: /* Invalid */
285163fa5caSBlue Swirl             case 3: /* Reserved */
286163fa5caSBlue Swirl                 return 0;
287163fa5caSBlue Swirl             case 2: /* L2 PTE */
288163fa5caSBlue Swirl                 return pde;
289163fa5caSBlue Swirl             case 1: /* L3 PDE */
290163fa5caSBlue Swirl                 if (mmulev == 1) {
291163fa5caSBlue Swirl                     return pde;
292163fa5caSBlue Swirl                 }
293163fa5caSBlue Swirl                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
294163fa5caSBlue Swirl                 pde = ldl_phys(pde_ptr);
295163fa5caSBlue Swirl 
296163fa5caSBlue Swirl                 switch (pde & PTE_ENTRYTYPE_MASK) {
297163fa5caSBlue Swirl                 default:
298163fa5caSBlue Swirl                 case 0: /* Invalid */
299163fa5caSBlue Swirl                 case 1: /* PDE, should not happen */
300163fa5caSBlue Swirl                 case 3: /* Reserved */
301163fa5caSBlue Swirl                     return 0;
302163fa5caSBlue Swirl                 case 2: /* L3 PTE */
303163fa5caSBlue Swirl                     return pde;
304163fa5caSBlue Swirl                 }
305163fa5caSBlue Swirl             }
306163fa5caSBlue Swirl         }
307163fa5caSBlue Swirl     }
308163fa5caSBlue Swirl     return 0;
309163fa5caSBlue Swirl }
310163fa5caSBlue Swirl 
311c5f9864eSAndreas Färber void dump_mmu(FILE *f, fprintf_function cpu_fprintf, CPUSPARCState *env)
312163fa5caSBlue Swirl {
31300b941e5SAndreas Färber     CPUState *cs = CPU(sparc_env_get_cpu(env));
314163fa5caSBlue Swirl     target_ulong va, va1, va2;
315163fa5caSBlue Swirl     unsigned int n, m, o;
316a8170e5eSAvi Kivity     hwaddr pde_ptr, pa;
317163fa5caSBlue Swirl     uint32_t pde;
318163fa5caSBlue Swirl 
319163fa5caSBlue Swirl     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
320163fa5caSBlue Swirl     pde = ldl_phys(pde_ptr);
321163fa5caSBlue Swirl     (*cpu_fprintf)(f, "Root ptr: " TARGET_FMT_plx ", ctx: %d\n",
322a8170e5eSAvi Kivity                    (hwaddr)env->mmuregs[1] << 4, env->mmuregs[2]);
323163fa5caSBlue Swirl     for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
324163fa5caSBlue Swirl         pde = mmu_probe(env, va, 2);
325163fa5caSBlue Swirl         if (pde) {
32600b941e5SAndreas Färber             pa = cpu_get_phys_page_debug(cs, va);
327163fa5caSBlue Swirl             (*cpu_fprintf)(f, "VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
328163fa5caSBlue Swirl                            " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
329163fa5caSBlue Swirl             for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
330163fa5caSBlue Swirl                 pde = mmu_probe(env, va1, 1);
331163fa5caSBlue Swirl                 if (pde) {
33200b941e5SAndreas Färber                     pa = cpu_get_phys_page_debug(cs, va1);
333163fa5caSBlue Swirl                     (*cpu_fprintf)(f, " VA: " TARGET_FMT_lx ", PA: "
334163fa5caSBlue Swirl                                    TARGET_FMT_plx " PDE: " TARGET_FMT_lx "\n",
335163fa5caSBlue Swirl                                    va1, pa, pde);
336163fa5caSBlue Swirl                     for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
337163fa5caSBlue Swirl                         pde = mmu_probe(env, va2, 0);
338163fa5caSBlue Swirl                         if (pde) {
33900b941e5SAndreas Färber                             pa = cpu_get_phys_page_debug(cs, va2);
340163fa5caSBlue Swirl                             (*cpu_fprintf)(f, "  VA: " TARGET_FMT_lx ", PA: "
341163fa5caSBlue Swirl                                            TARGET_FMT_plx " PTE: "
342163fa5caSBlue Swirl                                            TARGET_FMT_lx "\n",
343163fa5caSBlue Swirl                                            va2, pa, pde);
344163fa5caSBlue Swirl                         }
345163fa5caSBlue Swirl                     }
346163fa5caSBlue Swirl                 }
347163fa5caSBlue Swirl             }
348163fa5caSBlue Swirl         }
349163fa5caSBlue Swirl     }
350163fa5caSBlue Swirl }
351163fa5caSBlue Swirl 
352163fa5caSBlue Swirl /* Gdb expects all registers windows to be flushed in ram. This function handles
353163fa5caSBlue Swirl  * reads (and only reads) in stack frames as if windows were flushed. We assume
354163fa5caSBlue Swirl  * that the sparc ABI is followed.
355163fa5caSBlue Swirl  */
356c5f9864eSAndreas Färber int target_memory_rw_debug(CPUSPARCState *env, target_ulong addr,
357163fa5caSBlue Swirl                            uint8_t *buf, int len, int is_write)
358163fa5caSBlue Swirl {
359163fa5caSBlue Swirl     int i;
360163fa5caSBlue Swirl     int len1;
361163fa5caSBlue Swirl     int cwp = env->cwp;
362163fa5caSBlue Swirl 
363163fa5caSBlue Swirl     if (!is_write) {
364163fa5caSBlue Swirl         for (i = 0; i < env->nwindows; i++) {
365163fa5caSBlue Swirl             int off;
366163fa5caSBlue Swirl             target_ulong fp = env->regbase[cwp * 16 + 22];
367163fa5caSBlue Swirl 
368163fa5caSBlue Swirl             /* Assume fp == 0 means end of frame.  */
369163fa5caSBlue Swirl             if (fp == 0) {
370163fa5caSBlue Swirl                 break;
371163fa5caSBlue Swirl             }
372163fa5caSBlue Swirl 
373163fa5caSBlue Swirl             cwp = cpu_cwp_inc(env, cwp + 1);
374163fa5caSBlue Swirl 
375163fa5caSBlue Swirl             /* Invalid window ? */
376163fa5caSBlue Swirl             if (env->wim & (1 << cwp)) {
377163fa5caSBlue Swirl                 break;
378163fa5caSBlue Swirl             }
379163fa5caSBlue Swirl 
380163fa5caSBlue Swirl             /* According to the ABI, the stack is growing downward.  */
381163fa5caSBlue Swirl             if (addr + len < fp) {
382163fa5caSBlue Swirl                 break;
383163fa5caSBlue Swirl             }
384163fa5caSBlue Swirl 
385163fa5caSBlue Swirl             /* Not in this frame.  */
386163fa5caSBlue Swirl             if (addr > fp + 64) {
387163fa5caSBlue Swirl                 continue;
388163fa5caSBlue Swirl             }
389163fa5caSBlue Swirl 
390163fa5caSBlue Swirl             /* Handle access before this window.  */
391163fa5caSBlue Swirl             if (addr < fp) {
392163fa5caSBlue Swirl                 len1 = fp - addr;
393163fa5caSBlue Swirl                 if (cpu_memory_rw_debug(env, addr, buf, len1, is_write) != 0) {
394163fa5caSBlue Swirl                     return -1;
395163fa5caSBlue Swirl                 }
396163fa5caSBlue Swirl                 addr += len1;
397163fa5caSBlue Swirl                 len -= len1;
398163fa5caSBlue Swirl                 buf += len1;
399163fa5caSBlue Swirl             }
400163fa5caSBlue Swirl 
401163fa5caSBlue Swirl             /* Access byte per byte to registers. Not very efficient but speed
402163fa5caSBlue Swirl              * is not critical.
403163fa5caSBlue Swirl              */
404163fa5caSBlue Swirl             off = addr - fp;
405163fa5caSBlue Swirl             len1 = 64 - off;
406163fa5caSBlue Swirl 
407163fa5caSBlue Swirl             if (len1 > len) {
408163fa5caSBlue Swirl                 len1 = len;
409163fa5caSBlue Swirl             }
410163fa5caSBlue Swirl 
411163fa5caSBlue Swirl             for (; len1; len1--) {
412163fa5caSBlue Swirl                 int reg = cwp * 16 + 8 + (off >> 2);
413163fa5caSBlue Swirl                 union {
414163fa5caSBlue Swirl                     uint32_t v;
415163fa5caSBlue Swirl                     uint8_t c[4];
416163fa5caSBlue Swirl                 } u;
417163fa5caSBlue Swirl                 u.v = cpu_to_be32(env->regbase[reg]);
418163fa5caSBlue Swirl                 *buf++ = u.c[off & 3];
419163fa5caSBlue Swirl                 addr++;
420163fa5caSBlue Swirl                 len--;
421163fa5caSBlue Swirl                 off++;
422163fa5caSBlue Swirl             }
423163fa5caSBlue Swirl 
424163fa5caSBlue Swirl             if (len == 0) {
425163fa5caSBlue Swirl                 return 0;
426163fa5caSBlue Swirl             }
427163fa5caSBlue Swirl         }
428163fa5caSBlue Swirl     }
429163fa5caSBlue Swirl     return cpu_memory_rw_debug(env, addr, buf, len, is_write);
430163fa5caSBlue Swirl }
431163fa5caSBlue Swirl 
432163fa5caSBlue Swirl #else /* !TARGET_SPARC64 */
433163fa5caSBlue Swirl 
434163fa5caSBlue Swirl /* 41 bit physical address space */
435a8170e5eSAvi Kivity static inline hwaddr ultrasparc_truncate_physical(uint64_t x)
436163fa5caSBlue Swirl {
437163fa5caSBlue Swirl     return x & 0x1ffffffffffULL;
438163fa5caSBlue Swirl }
439163fa5caSBlue Swirl 
440163fa5caSBlue Swirl /*
441163fa5caSBlue Swirl  * UltraSparc IIi I/DMMUs
442163fa5caSBlue Swirl  */
443163fa5caSBlue Swirl 
444163fa5caSBlue Swirl /* Returns true if TTE tag is valid and matches virtual address value
445163fa5caSBlue Swirl    in context requires virtual address mask value calculated from TTE
446163fa5caSBlue Swirl    entry size */
447163fa5caSBlue Swirl static inline int ultrasparc_tag_match(SparcTLBEntry *tlb,
448163fa5caSBlue Swirl                                        uint64_t address, uint64_t context,
449a8170e5eSAvi Kivity                                        hwaddr *physical)
450163fa5caSBlue Swirl {
451163fa5caSBlue Swirl     uint64_t mask;
452163fa5caSBlue Swirl 
453163fa5caSBlue Swirl     switch (TTE_PGSIZE(tlb->tte)) {
454163fa5caSBlue Swirl     default:
455163fa5caSBlue Swirl     case 0x0: /* 8k */
456163fa5caSBlue Swirl         mask = 0xffffffffffffe000ULL;
457163fa5caSBlue Swirl         break;
458163fa5caSBlue Swirl     case 0x1: /* 64k */
459163fa5caSBlue Swirl         mask = 0xffffffffffff0000ULL;
460163fa5caSBlue Swirl         break;
461163fa5caSBlue Swirl     case 0x2: /* 512k */
462163fa5caSBlue Swirl         mask = 0xfffffffffff80000ULL;
463163fa5caSBlue Swirl         break;
464163fa5caSBlue Swirl     case 0x3: /* 4M */
465163fa5caSBlue Swirl         mask = 0xffffffffffc00000ULL;
466163fa5caSBlue Swirl         break;
467163fa5caSBlue Swirl     }
468163fa5caSBlue Swirl 
469163fa5caSBlue Swirl     /* valid, context match, virtual address match? */
470163fa5caSBlue Swirl     if (TTE_IS_VALID(tlb->tte) &&
471163fa5caSBlue Swirl         (TTE_IS_GLOBAL(tlb->tte) || tlb_compare_context(tlb, context))
472163fa5caSBlue Swirl         && compare_masked(address, tlb->tag, mask)) {
473163fa5caSBlue Swirl         /* decode physical address */
474163fa5caSBlue Swirl         *physical = ((tlb->tte & mask) | (address & ~mask)) & 0x1ffffffe000ULL;
475163fa5caSBlue Swirl         return 1;
476163fa5caSBlue Swirl     }
477163fa5caSBlue Swirl 
478163fa5caSBlue Swirl     return 0;
479163fa5caSBlue Swirl }
480163fa5caSBlue Swirl 
481c5f9864eSAndreas Färber static int get_physical_address_data(CPUSPARCState *env,
482a8170e5eSAvi Kivity                                      hwaddr *physical, int *prot,
483163fa5caSBlue Swirl                                      target_ulong address, int rw, int mmu_idx)
484163fa5caSBlue Swirl {
485163fa5caSBlue Swirl     unsigned int i;
486163fa5caSBlue Swirl     uint64_t context;
487163fa5caSBlue Swirl     uint64_t sfsr = 0;
488163fa5caSBlue Swirl 
489163fa5caSBlue Swirl     int is_user = (mmu_idx == MMU_USER_IDX ||
490163fa5caSBlue Swirl                    mmu_idx == MMU_USER_SECONDARY_IDX);
491163fa5caSBlue Swirl 
492163fa5caSBlue Swirl     if ((env->lsu & DMMU_E) == 0) { /* DMMU disabled */
493163fa5caSBlue Swirl         *physical = ultrasparc_truncate_physical(address);
494163fa5caSBlue Swirl         *prot = PAGE_READ | PAGE_WRITE;
495163fa5caSBlue Swirl         return 0;
496163fa5caSBlue Swirl     }
497163fa5caSBlue Swirl 
498163fa5caSBlue Swirl     switch (mmu_idx) {
499163fa5caSBlue Swirl     case MMU_USER_IDX:
500163fa5caSBlue Swirl     case MMU_KERNEL_IDX:
501163fa5caSBlue Swirl         context = env->dmmu.mmu_primary_context & 0x1fff;
502163fa5caSBlue Swirl         sfsr |= SFSR_CT_PRIMARY;
503163fa5caSBlue Swirl         break;
504163fa5caSBlue Swirl     case MMU_USER_SECONDARY_IDX:
505163fa5caSBlue Swirl     case MMU_KERNEL_SECONDARY_IDX:
506163fa5caSBlue Swirl         context = env->dmmu.mmu_secondary_context & 0x1fff;
507163fa5caSBlue Swirl         sfsr |= SFSR_CT_SECONDARY;
508163fa5caSBlue Swirl         break;
509163fa5caSBlue Swirl     case MMU_NUCLEUS_IDX:
510163fa5caSBlue Swirl         sfsr |= SFSR_CT_NUCLEUS;
511163fa5caSBlue Swirl         /* FALLTHRU */
512163fa5caSBlue Swirl     default:
513163fa5caSBlue Swirl         context = 0;
514163fa5caSBlue Swirl         break;
515163fa5caSBlue Swirl     }
516163fa5caSBlue Swirl 
517163fa5caSBlue Swirl     if (rw == 1) {
518163fa5caSBlue Swirl         sfsr |= SFSR_WRITE_BIT;
519163fa5caSBlue Swirl     } else if (rw == 4) {
520163fa5caSBlue Swirl         sfsr |= SFSR_NF_BIT;
521163fa5caSBlue Swirl     }
522163fa5caSBlue Swirl 
523163fa5caSBlue Swirl     for (i = 0; i < 64; i++) {
524163fa5caSBlue Swirl         /* ctx match, vaddr match, valid? */
525163fa5caSBlue Swirl         if (ultrasparc_tag_match(&env->dtlb[i], address, context, physical)) {
526163fa5caSBlue Swirl             int do_fault = 0;
527163fa5caSBlue Swirl 
528163fa5caSBlue Swirl             /* access ok? */
529163fa5caSBlue Swirl             /* multiple bits in SFSR.FT may be set on TT_DFAULT */
530163fa5caSBlue Swirl             if (TTE_IS_PRIV(env->dtlb[i].tte) && is_user) {
531163fa5caSBlue Swirl                 do_fault = 1;
532163fa5caSBlue Swirl                 sfsr |= SFSR_FT_PRIV_BIT; /* privilege violation */
533ec0ceb17SBlue Swirl                 trace_mmu_helper_dfault(address, context, mmu_idx, env->tl);
534163fa5caSBlue Swirl             }
535163fa5caSBlue Swirl             if (rw == 4) {
536163fa5caSBlue Swirl                 if (TTE_IS_SIDEEFFECT(env->dtlb[i].tte)) {
537163fa5caSBlue Swirl                     do_fault = 1;
538163fa5caSBlue Swirl                     sfsr |= SFSR_FT_NF_E_BIT;
539163fa5caSBlue Swirl                 }
540163fa5caSBlue Swirl             } else {
541163fa5caSBlue Swirl                 if (TTE_IS_NFO(env->dtlb[i].tte)) {
542163fa5caSBlue Swirl                     do_fault = 1;
543163fa5caSBlue Swirl                     sfsr |= SFSR_FT_NFO_BIT;
544163fa5caSBlue Swirl                 }
545163fa5caSBlue Swirl             }
546163fa5caSBlue Swirl 
547163fa5caSBlue Swirl             if (do_fault) {
548163fa5caSBlue Swirl                 /* faults above are reported with TT_DFAULT. */
549163fa5caSBlue Swirl                 env->exception_index = TT_DFAULT;
550163fa5caSBlue Swirl             } else if (!TTE_IS_W_OK(env->dtlb[i].tte) && (rw == 1)) {
551163fa5caSBlue Swirl                 do_fault = 1;
552163fa5caSBlue Swirl                 env->exception_index = TT_DPROT;
553163fa5caSBlue Swirl 
554ec0ceb17SBlue Swirl                 trace_mmu_helper_dprot(address, context, mmu_idx, env->tl);
555163fa5caSBlue Swirl             }
556163fa5caSBlue Swirl 
557163fa5caSBlue Swirl             if (!do_fault) {
558163fa5caSBlue Swirl                 *prot = PAGE_READ;
559163fa5caSBlue Swirl                 if (TTE_IS_W_OK(env->dtlb[i].tte)) {
560163fa5caSBlue Swirl                     *prot |= PAGE_WRITE;
561163fa5caSBlue Swirl                 }
562163fa5caSBlue Swirl 
563163fa5caSBlue Swirl                 TTE_SET_USED(env->dtlb[i].tte);
564163fa5caSBlue Swirl 
565163fa5caSBlue Swirl                 return 0;
566163fa5caSBlue Swirl             }
567163fa5caSBlue Swirl 
568163fa5caSBlue Swirl             if (env->dmmu.sfsr & SFSR_VALID_BIT) { /* Fault status register */
569163fa5caSBlue Swirl                 sfsr |= SFSR_OW_BIT; /* overflow (not read before
570163fa5caSBlue Swirl                                         another fault) */
571163fa5caSBlue Swirl             }
572163fa5caSBlue Swirl 
573163fa5caSBlue Swirl             if (env->pstate & PS_PRIV) {
574163fa5caSBlue Swirl                 sfsr |= SFSR_PR_BIT;
575163fa5caSBlue Swirl             }
576163fa5caSBlue Swirl 
577163fa5caSBlue Swirl             /* FIXME: ASI field in SFSR must be set */
578163fa5caSBlue Swirl             env->dmmu.sfsr = sfsr | SFSR_VALID_BIT;
579163fa5caSBlue Swirl 
580163fa5caSBlue Swirl             env->dmmu.sfar = address; /* Fault address register */
581163fa5caSBlue Swirl 
582163fa5caSBlue Swirl             env->dmmu.tag_access = (address & ~0x1fffULL) | context;
583163fa5caSBlue Swirl 
584163fa5caSBlue Swirl             return 1;
585163fa5caSBlue Swirl         }
586163fa5caSBlue Swirl     }
587163fa5caSBlue Swirl 
588ec0ceb17SBlue Swirl     trace_mmu_helper_dmiss(address, context);
589163fa5caSBlue Swirl 
590163fa5caSBlue Swirl     /*
591163fa5caSBlue Swirl      * On MMU misses:
592163fa5caSBlue Swirl      * - UltraSPARC IIi: SFSR and SFAR unmodified
593163fa5caSBlue Swirl      * - JPS1: SFAR updated and some fields of SFSR updated
594163fa5caSBlue Swirl      */
595163fa5caSBlue Swirl     env->dmmu.tag_access = (address & ~0x1fffULL) | context;
596163fa5caSBlue Swirl     env->exception_index = TT_DMISS;
597163fa5caSBlue Swirl     return 1;
598163fa5caSBlue Swirl }
599163fa5caSBlue Swirl 
600c5f9864eSAndreas Färber static int get_physical_address_code(CPUSPARCState *env,
601a8170e5eSAvi Kivity                                      hwaddr *physical, int *prot,
602163fa5caSBlue Swirl                                      target_ulong address, int mmu_idx)
603163fa5caSBlue Swirl {
604163fa5caSBlue Swirl     unsigned int i;
605163fa5caSBlue Swirl     uint64_t context;
606163fa5caSBlue Swirl 
607163fa5caSBlue Swirl     int is_user = (mmu_idx == MMU_USER_IDX ||
608163fa5caSBlue Swirl                    mmu_idx == MMU_USER_SECONDARY_IDX);
609163fa5caSBlue Swirl 
610163fa5caSBlue Swirl     if ((env->lsu & IMMU_E) == 0 || (env->pstate & PS_RED) != 0) {
611163fa5caSBlue Swirl         /* IMMU disabled */
612163fa5caSBlue Swirl         *physical = ultrasparc_truncate_physical(address);
613163fa5caSBlue Swirl         *prot = PAGE_EXEC;
614163fa5caSBlue Swirl         return 0;
615163fa5caSBlue Swirl     }
616163fa5caSBlue Swirl 
617163fa5caSBlue Swirl     if (env->tl == 0) {
618163fa5caSBlue Swirl         /* PRIMARY context */
619163fa5caSBlue Swirl         context = env->dmmu.mmu_primary_context & 0x1fff;
620163fa5caSBlue Swirl     } else {
621163fa5caSBlue Swirl         /* NUCLEUS context */
622163fa5caSBlue Swirl         context = 0;
623163fa5caSBlue Swirl     }
624163fa5caSBlue Swirl 
625163fa5caSBlue Swirl     for (i = 0; i < 64; i++) {
626163fa5caSBlue Swirl         /* ctx match, vaddr match, valid? */
627163fa5caSBlue Swirl         if (ultrasparc_tag_match(&env->itlb[i],
628163fa5caSBlue Swirl                                  address, context, physical)) {
629163fa5caSBlue Swirl             /* access ok? */
630163fa5caSBlue Swirl             if (TTE_IS_PRIV(env->itlb[i].tte) && is_user) {
631163fa5caSBlue Swirl                 /* Fault status register */
632163fa5caSBlue Swirl                 if (env->immu.sfsr & SFSR_VALID_BIT) {
633163fa5caSBlue Swirl                     env->immu.sfsr = SFSR_OW_BIT; /* overflow (not read before
634163fa5caSBlue Swirl                                                      another fault) */
635163fa5caSBlue Swirl                 } else {
636163fa5caSBlue Swirl                     env->immu.sfsr = 0;
637163fa5caSBlue Swirl                 }
638163fa5caSBlue Swirl                 if (env->pstate & PS_PRIV) {
639163fa5caSBlue Swirl                     env->immu.sfsr |= SFSR_PR_BIT;
640163fa5caSBlue Swirl                 }
641163fa5caSBlue Swirl                 if (env->tl > 0) {
642163fa5caSBlue Swirl                     env->immu.sfsr |= SFSR_CT_NUCLEUS;
643163fa5caSBlue Swirl                 }
644163fa5caSBlue Swirl 
645163fa5caSBlue Swirl                 /* FIXME: ASI field in SFSR must be set */
646163fa5caSBlue Swirl                 env->immu.sfsr |= SFSR_FT_PRIV_BIT | SFSR_VALID_BIT;
647163fa5caSBlue Swirl                 env->exception_index = TT_TFAULT;
648163fa5caSBlue Swirl 
649163fa5caSBlue Swirl                 env->immu.tag_access = (address & ~0x1fffULL) | context;
650163fa5caSBlue Swirl 
651ec0ceb17SBlue Swirl                 trace_mmu_helper_tfault(address, context);
652163fa5caSBlue Swirl 
653163fa5caSBlue Swirl                 return 1;
654163fa5caSBlue Swirl             }
655163fa5caSBlue Swirl             *prot = PAGE_EXEC;
656163fa5caSBlue Swirl             TTE_SET_USED(env->itlb[i].tte);
657163fa5caSBlue Swirl             return 0;
658163fa5caSBlue Swirl         }
659163fa5caSBlue Swirl     }
660163fa5caSBlue Swirl 
661ec0ceb17SBlue Swirl     trace_mmu_helper_tmiss(address, context);
662163fa5caSBlue Swirl 
663163fa5caSBlue Swirl     /* Context is stored in DMMU (dmmuregs[1]) also for IMMU */
664163fa5caSBlue Swirl     env->immu.tag_access = (address & ~0x1fffULL) | context;
665163fa5caSBlue Swirl     env->exception_index = TT_TMISS;
666163fa5caSBlue Swirl     return 1;
667163fa5caSBlue Swirl }
668163fa5caSBlue Swirl 
669a8170e5eSAvi Kivity static int get_physical_address(CPUSPARCState *env, hwaddr *physical,
670163fa5caSBlue Swirl                                 int *prot, int *access_index,
671163fa5caSBlue Swirl                                 target_ulong address, int rw, int mmu_idx,
672163fa5caSBlue Swirl                                 target_ulong *page_size)
673163fa5caSBlue Swirl {
674163fa5caSBlue Swirl     /* ??? We treat everything as a small page, then explicitly flush
675163fa5caSBlue Swirl        everything when an entry is evicted.  */
676163fa5caSBlue Swirl     *page_size = TARGET_PAGE_SIZE;
677163fa5caSBlue Swirl 
678163fa5caSBlue Swirl     /* safety net to catch wrong softmmu index use from dynamic code */
679163fa5caSBlue Swirl     if (env->tl > 0 && mmu_idx != MMU_NUCLEUS_IDX) {
680ec0ceb17SBlue Swirl         if (rw == 2) {
681ec0ceb17SBlue Swirl             trace_mmu_helper_get_phys_addr_code(env->tl, mmu_idx,
682ec0ceb17SBlue Swirl                                                 env->dmmu.mmu_primary_context,
683ec0ceb17SBlue Swirl                                                 env->dmmu.mmu_secondary_context,
684ec0ceb17SBlue Swirl                                                 address);
685ec0ceb17SBlue Swirl         } else {
686ec0ceb17SBlue Swirl             trace_mmu_helper_get_phys_addr_data(env->tl, mmu_idx,
687163fa5caSBlue Swirl                                                 env->dmmu.mmu_primary_context,
688163fa5caSBlue Swirl                                                 env->dmmu.mmu_secondary_context,
689163fa5caSBlue Swirl                                                 address);
690163fa5caSBlue Swirl         }
691ec0ceb17SBlue Swirl     }
692163fa5caSBlue Swirl 
693163fa5caSBlue Swirl     if (rw == 2) {
694163fa5caSBlue Swirl         return get_physical_address_code(env, physical, prot, address,
695163fa5caSBlue Swirl                                          mmu_idx);
696163fa5caSBlue Swirl     } else {
697163fa5caSBlue Swirl         return get_physical_address_data(env, physical, prot, address, rw,
698163fa5caSBlue Swirl                                          mmu_idx);
699163fa5caSBlue Swirl     }
700163fa5caSBlue Swirl }
701163fa5caSBlue Swirl 
702163fa5caSBlue Swirl /* Perform address translation */
703c5f9864eSAndreas Färber int cpu_sparc_handle_mmu_fault(CPUSPARCState *env, target_ulong address, int rw,
704163fa5caSBlue Swirl                                int mmu_idx)
705163fa5caSBlue Swirl {
7061658dd32SBlue Swirl     target_ulong vaddr;
707a8170e5eSAvi Kivity     hwaddr paddr;
708163fa5caSBlue Swirl     target_ulong page_size;
709163fa5caSBlue Swirl     int error_code = 0, prot, access_index;
710163fa5caSBlue Swirl 
7111658dd32SBlue Swirl     address &= TARGET_PAGE_MASK;
712163fa5caSBlue Swirl     error_code = get_physical_address(env, &paddr, &prot, &access_index,
713163fa5caSBlue Swirl                                       address, rw, mmu_idx, &page_size);
714163fa5caSBlue Swirl     if (error_code == 0) {
7151658dd32SBlue Swirl         vaddr = address;
716163fa5caSBlue Swirl 
717ec0ceb17SBlue Swirl         trace_mmu_helper_mmu_fault(address, paddr, mmu_idx, env->tl,
718163fa5caSBlue Swirl                                    env->dmmu.mmu_primary_context,
719163fa5caSBlue Swirl                                    env->dmmu.mmu_secondary_context);
720163fa5caSBlue Swirl 
721163fa5caSBlue Swirl         tlb_set_page(env, vaddr, paddr, prot, mmu_idx, page_size);
722163fa5caSBlue Swirl         return 0;
723163fa5caSBlue Swirl     }
724163fa5caSBlue Swirl     /* XXX */
725163fa5caSBlue Swirl     return 1;
726163fa5caSBlue Swirl }
727163fa5caSBlue Swirl 
728c5f9864eSAndreas Färber void dump_mmu(FILE *f, fprintf_function cpu_fprintf, CPUSPARCState *env)
729163fa5caSBlue Swirl {
730163fa5caSBlue Swirl     unsigned int i;
731163fa5caSBlue Swirl     const char *mask;
732163fa5caSBlue Swirl 
733163fa5caSBlue Swirl     (*cpu_fprintf)(f, "MMU contexts: Primary: %" PRId64 ", Secondary: %"
734163fa5caSBlue Swirl                    PRId64 "\n",
735163fa5caSBlue Swirl                    env->dmmu.mmu_primary_context,
736163fa5caSBlue Swirl                    env->dmmu.mmu_secondary_context);
737163fa5caSBlue Swirl     if ((env->lsu & DMMU_E) == 0) {
738163fa5caSBlue Swirl         (*cpu_fprintf)(f, "DMMU disabled\n");
739163fa5caSBlue Swirl     } else {
740163fa5caSBlue Swirl         (*cpu_fprintf)(f, "DMMU dump\n");
741163fa5caSBlue Swirl         for (i = 0; i < 64; i++) {
742163fa5caSBlue Swirl             switch (TTE_PGSIZE(env->dtlb[i].tte)) {
743163fa5caSBlue Swirl             default:
744163fa5caSBlue Swirl             case 0x0:
745163fa5caSBlue Swirl                 mask = "  8k";
746163fa5caSBlue Swirl                 break;
747163fa5caSBlue Swirl             case 0x1:
748163fa5caSBlue Swirl                 mask = " 64k";
749163fa5caSBlue Swirl                 break;
750163fa5caSBlue Swirl             case 0x2:
751163fa5caSBlue Swirl                 mask = "512k";
752163fa5caSBlue Swirl                 break;
753163fa5caSBlue Swirl             case 0x3:
754163fa5caSBlue Swirl                 mask = "  4M";
755163fa5caSBlue Swirl                 break;
756163fa5caSBlue Swirl             }
757163fa5caSBlue Swirl             if (TTE_IS_VALID(env->dtlb[i].tte)) {
758163fa5caSBlue Swirl                 (*cpu_fprintf)(f, "[%02u] VA: %" PRIx64 ", PA: %llx"
759163fa5caSBlue Swirl                                ", %s, %s, %s, %s, ctx %" PRId64 " %s\n",
760163fa5caSBlue Swirl                                i,
761163fa5caSBlue Swirl                                env->dtlb[i].tag & (uint64_t)~0x1fffULL,
762163fa5caSBlue Swirl                                TTE_PA(env->dtlb[i].tte),
763163fa5caSBlue Swirl                                mask,
764163fa5caSBlue Swirl                                TTE_IS_PRIV(env->dtlb[i].tte) ? "priv" : "user",
765163fa5caSBlue Swirl                                TTE_IS_W_OK(env->dtlb[i].tte) ? "RW" : "RO",
766163fa5caSBlue Swirl                                TTE_IS_LOCKED(env->dtlb[i].tte) ?
767163fa5caSBlue Swirl                                "locked" : "unlocked",
768163fa5caSBlue Swirl                                env->dtlb[i].tag & (uint64_t)0x1fffULL,
769163fa5caSBlue Swirl                                TTE_IS_GLOBAL(env->dtlb[i].tte) ?
770163fa5caSBlue Swirl                                "global" : "local");
771163fa5caSBlue Swirl             }
772163fa5caSBlue Swirl         }
773163fa5caSBlue Swirl     }
774163fa5caSBlue Swirl     if ((env->lsu & IMMU_E) == 0) {
775163fa5caSBlue Swirl         (*cpu_fprintf)(f, "IMMU disabled\n");
776163fa5caSBlue Swirl     } else {
777163fa5caSBlue Swirl         (*cpu_fprintf)(f, "IMMU dump\n");
778163fa5caSBlue Swirl         for (i = 0; i < 64; i++) {
779163fa5caSBlue Swirl             switch (TTE_PGSIZE(env->itlb[i].tte)) {
780163fa5caSBlue Swirl             default:
781163fa5caSBlue Swirl             case 0x0:
782163fa5caSBlue Swirl                 mask = "  8k";
783163fa5caSBlue Swirl                 break;
784163fa5caSBlue Swirl             case 0x1:
785163fa5caSBlue Swirl                 mask = " 64k";
786163fa5caSBlue Swirl                 break;
787163fa5caSBlue Swirl             case 0x2:
788163fa5caSBlue Swirl                 mask = "512k";
789163fa5caSBlue Swirl                 break;
790163fa5caSBlue Swirl             case 0x3:
791163fa5caSBlue Swirl                 mask = "  4M";
792163fa5caSBlue Swirl                 break;
793163fa5caSBlue Swirl             }
794163fa5caSBlue Swirl             if (TTE_IS_VALID(env->itlb[i].tte)) {
795163fa5caSBlue Swirl                 (*cpu_fprintf)(f, "[%02u] VA: %" PRIx64 ", PA: %llx"
796163fa5caSBlue Swirl                                ", %s, %s, %s, ctx %" PRId64 " %s\n",
797163fa5caSBlue Swirl                                i,
798163fa5caSBlue Swirl                                env->itlb[i].tag & (uint64_t)~0x1fffULL,
799163fa5caSBlue Swirl                                TTE_PA(env->itlb[i].tte),
800163fa5caSBlue Swirl                                mask,
801163fa5caSBlue Swirl                                TTE_IS_PRIV(env->itlb[i].tte) ? "priv" : "user",
802163fa5caSBlue Swirl                                TTE_IS_LOCKED(env->itlb[i].tte) ?
803163fa5caSBlue Swirl                                "locked" : "unlocked",
804163fa5caSBlue Swirl                                env->itlb[i].tag & (uint64_t)0x1fffULL,
805163fa5caSBlue Swirl                                TTE_IS_GLOBAL(env->itlb[i].tte) ?
806163fa5caSBlue Swirl                                "global" : "local");
807163fa5caSBlue Swirl             }
808163fa5caSBlue Swirl         }
809163fa5caSBlue Swirl     }
810163fa5caSBlue Swirl }
811163fa5caSBlue Swirl 
812163fa5caSBlue Swirl #endif /* TARGET_SPARC64 */
813163fa5caSBlue Swirl 
814a8170e5eSAvi Kivity static int cpu_sparc_get_phys_page(CPUSPARCState *env, hwaddr *phys,
815163fa5caSBlue Swirl                                    target_ulong addr, int rw, int mmu_idx)
816163fa5caSBlue Swirl {
817163fa5caSBlue Swirl     target_ulong page_size;
818163fa5caSBlue Swirl     int prot, access_index;
819163fa5caSBlue Swirl 
820163fa5caSBlue Swirl     return get_physical_address(env, phys, &prot, &access_index, addr, rw,
821163fa5caSBlue Swirl                                 mmu_idx, &page_size);
822163fa5caSBlue Swirl }
823163fa5caSBlue Swirl 
824163fa5caSBlue Swirl #if defined(TARGET_SPARC64)
825a8170e5eSAvi Kivity hwaddr cpu_get_phys_page_nofault(CPUSPARCState *env, target_ulong addr,
826163fa5caSBlue Swirl                                            int mmu_idx)
827163fa5caSBlue Swirl {
828a8170e5eSAvi Kivity     hwaddr phys_addr;
829163fa5caSBlue Swirl 
830163fa5caSBlue Swirl     if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 4, mmu_idx) != 0) {
831163fa5caSBlue Swirl         return -1;
832163fa5caSBlue Swirl     }
833163fa5caSBlue Swirl     return phys_addr;
834163fa5caSBlue Swirl }
835163fa5caSBlue Swirl #endif
836163fa5caSBlue Swirl 
83700b941e5SAndreas Färber hwaddr sparc_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
838163fa5caSBlue Swirl {
83900b941e5SAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
84000b941e5SAndreas Färber     CPUSPARCState *env = &cpu->env;
841a8170e5eSAvi Kivity     hwaddr phys_addr;
842163fa5caSBlue Swirl     int mmu_idx = cpu_mmu_index(env);
843cc4aa830SAvi Kivity     MemoryRegionSection section;
844163fa5caSBlue Swirl 
845163fa5caSBlue Swirl     if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 2, mmu_idx) != 0) {
846163fa5caSBlue Swirl         if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 0, mmu_idx) != 0) {
847163fa5caSBlue Swirl             return -1;
848163fa5caSBlue Swirl         }
849163fa5caSBlue Swirl     }
850cc4aa830SAvi Kivity     section = memory_region_find(get_system_memory(), phys_addr, 1);
851dfde4e6eSPaolo Bonzini     memory_region_unref(section.mr);
852052e87b0SPaolo Bonzini     if (!int128_nz(section.size)) {
853163fa5caSBlue Swirl         return -1;
854163fa5caSBlue Swirl     }
855163fa5caSBlue Swirl     return phys_addr;
856163fa5caSBlue Swirl }
857163fa5caSBlue Swirl #endif
858