xref: /qemu/target/sparc/mmu_helper.c (revision a0ff4a879cd3198adb4213653d51a39d053ef2d6)
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
95650b549SChetan Pant  * version 2.1 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"
21cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h"
22163fa5caSBlue Swirl #include "cpu.h"
2363c91552SPaolo Bonzini #include "exec/exec-all.h"
24fad866daSMarkus Armbruster #include "qemu/qemu-print.h"
25ec0ceb17SBlue Swirl #include "trace.h"
26163fa5caSBlue Swirl 
27163fa5caSBlue Swirl /* Sparc MMU emulation */
28163fa5caSBlue Swirl 
29163fa5caSBlue Swirl #ifndef TARGET_SPARC64
30163fa5caSBlue Swirl /*
31163fa5caSBlue Swirl  * Sparc V8 Reference MMU (SRMMU)
32163fa5caSBlue Swirl  */
33163fa5caSBlue Swirl static const int access_table[8][8] = {
34163fa5caSBlue Swirl     { 0, 0, 0, 0, 8, 0, 12, 12 },
35163fa5caSBlue Swirl     { 0, 0, 0, 0, 8, 0, 0, 0 },
36163fa5caSBlue Swirl     { 8, 8, 0, 0, 0, 8, 12, 12 },
37163fa5caSBlue Swirl     { 8, 8, 0, 0, 0, 8, 0, 0 },
38163fa5caSBlue Swirl     { 8, 0, 8, 0, 8, 8, 12, 12 },
39163fa5caSBlue Swirl     { 8, 0, 8, 0, 8, 0, 8, 0 },
40163fa5caSBlue Swirl     { 8, 8, 8, 0, 8, 8, 12, 12 },
41163fa5caSBlue Swirl     { 8, 8, 8, 0, 8, 8, 8, 0 }
42163fa5caSBlue Swirl };
43163fa5caSBlue Swirl 
44163fa5caSBlue Swirl static const int perm_table[2][8] = {
45163fa5caSBlue Swirl     {
46163fa5caSBlue Swirl         PAGE_READ,
47163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE,
48163fa5caSBlue Swirl         PAGE_READ | PAGE_EXEC,
49163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
50163fa5caSBlue Swirl         PAGE_EXEC,
51163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE,
52163fa5caSBlue Swirl         PAGE_READ | PAGE_EXEC,
53163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE | PAGE_EXEC
54163fa5caSBlue Swirl     },
55163fa5caSBlue Swirl     {
56163fa5caSBlue Swirl         PAGE_READ,
57163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE,
58163fa5caSBlue Swirl         PAGE_READ | PAGE_EXEC,
59163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
60163fa5caSBlue Swirl         PAGE_EXEC,
61163fa5caSBlue Swirl         PAGE_READ,
62163fa5caSBlue Swirl         0,
63163fa5caSBlue Swirl         0,
64163fa5caSBlue Swirl     }
65163fa5caSBlue Swirl };
66163fa5caSBlue Swirl 
6771b7794bSRichard Henderson static int get_physical_address(CPUSPARCState *env, CPUTLBEntryFull *full,
6871b7794bSRichard Henderson                                 int *access_index, target_ulong address,
6971b7794bSRichard Henderson                                 int rw, int mmu_idx)
70163fa5caSBlue Swirl {
71163fa5caSBlue Swirl     int access_perms = 0;
72a8170e5eSAvi Kivity     hwaddr pde_ptr;
73163fa5caSBlue Swirl     uint32_t pde;
74163fa5caSBlue Swirl     int error_code = 0, is_dirty, is_user;
75163fa5caSBlue Swirl     unsigned long page_offset;
765a59fbceSRichard Henderson     CPUState *cs = env_cpu(env);
773c818dfcSPeter Maydell     MemTxResult result;
78163fa5caSBlue Swirl 
79163fa5caSBlue Swirl     is_user = mmu_idx == MMU_USER_IDX;
80163fa5caSBlue Swirl 
81af7a06baSRichard Henderson     if (mmu_idx == MMU_PHYS_IDX) {
8271b7794bSRichard Henderson         full->lg_page_size = TARGET_PAGE_BITS;
83163fa5caSBlue Swirl         /* Boot mode: instruction fetches are taken from PROM */
84576e1c4cSIgor Mammedov         if (rw == 2 && (env->mmuregs[0] & env->def.mmu_bm)) {
8571b7794bSRichard Henderson             full->phys_addr = env->prom_addr | (address & 0x7ffffULL);
8671b7794bSRichard Henderson             full->prot = PAGE_READ | PAGE_EXEC;
87163fa5caSBlue Swirl             return 0;
88163fa5caSBlue Swirl         }
8971b7794bSRichard Henderson         full->phys_addr = address;
9071b7794bSRichard Henderson         full->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
91163fa5caSBlue Swirl         return 0;
92163fa5caSBlue Swirl     }
93163fa5caSBlue Swirl 
94163fa5caSBlue Swirl     *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user ? 0 : 1);
9571b7794bSRichard Henderson     full->phys_addr = 0xffffffffffff0000ULL;
96163fa5caSBlue Swirl 
97163fa5caSBlue Swirl     /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
98163fa5caSBlue Swirl     /* Context base + context number */
99163fa5caSBlue Swirl     pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
1003c818dfcSPeter Maydell     pde = address_space_ldl(cs->as, pde_ptr, MEMTXATTRS_UNSPECIFIED, &result);
1013c818dfcSPeter Maydell     if (result != MEMTX_OK) {
1023c818dfcSPeter Maydell         return 4 << 2; /* Translation fault, L = 0 */
1033c818dfcSPeter Maydell     }
104163fa5caSBlue Swirl 
105163fa5caSBlue Swirl     /* Ctx pde */
106163fa5caSBlue Swirl     switch (pde & PTE_ENTRYTYPE_MASK) {
107163fa5caSBlue Swirl     default:
108163fa5caSBlue Swirl     case 0: /* Invalid */
109163fa5caSBlue Swirl         return 1 << 2;
110163fa5caSBlue Swirl     case 2: /* L0 PTE, maybe should not happen? */
111163fa5caSBlue Swirl     case 3: /* Reserved */
112163fa5caSBlue Swirl         return 4 << 2;
113163fa5caSBlue Swirl     case 1: /* L0 PDE */
114163fa5caSBlue Swirl         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
1153c818dfcSPeter Maydell         pde = address_space_ldl(cs->as, pde_ptr,
1163c818dfcSPeter Maydell                                 MEMTXATTRS_UNSPECIFIED, &result);
1173c818dfcSPeter Maydell         if (result != MEMTX_OK) {
1183c818dfcSPeter Maydell             return (1 << 8) | (4 << 2); /* Translation fault, L = 1 */
1193c818dfcSPeter Maydell         }
120163fa5caSBlue Swirl 
121163fa5caSBlue Swirl         switch (pde & PTE_ENTRYTYPE_MASK) {
122163fa5caSBlue Swirl         default:
123163fa5caSBlue Swirl         case 0: /* Invalid */
124163fa5caSBlue Swirl             return (1 << 8) | (1 << 2);
125163fa5caSBlue Swirl         case 3: /* Reserved */
126163fa5caSBlue Swirl             return (1 << 8) | (4 << 2);
127163fa5caSBlue Swirl         case 1: /* L1 PDE */
128163fa5caSBlue Swirl             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
1293c818dfcSPeter Maydell             pde = address_space_ldl(cs->as, pde_ptr,
1303c818dfcSPeter Maydell                                     MEMTXATTRS_UNSPECIFIED, &result);
1313c818dfcSPeter Maydell             if (result != MEMTX_OK) {
1323c818dfcSPeter Maydell                 return (2 << 8) | (4 << 2); /* Translation fault, L = 2 */
1333c818dfcSPeter Maydell             }
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);
1433c818dfcSPeter Maydell                 pde = address_space_ldl(cs->as, pde_ptr,
1443c818dfcSPeter Maydell                                         MEMTXATTRS_UNSPECIFIED, &result);
1453c818dfcSPeter Maydell                 if (result != MEMTX_OK) {
1463c818dfcSPeter Maydell                     return (3 << 8) | (4 << 2); /* Translation fault, L = 3 */
1473c818dfcSPeter Maydell                 }
148163fa5caSBlue Swirl 
149163fa5caSBlue Swirl                 switch (pde & PTE_ENTRYTYPE_MASK) {
150163fa5caSBlue Swirl                 default:
151163fa5caSBlue Swirl                 case 0: /* Invalid */
152163fa5caSBlue Swirl                     return (3 << 8) | (1 << 2);
153163fa5caSBlue Swirl                 case 1: /* PDE, should not happen */
154163fa5caSBlue Swirl                 case 3: /* Reserved */
155163fa5caSBlue Swirl                     return (3 << 8) | (4 << 2);
156163fa5caSBlue Swirl                 case 2: /* L3 PTE */
1571658dd32SBlue Swirl                     page_offset = 0;
158163fa5caSBlue Swirl                 }
15971b7794bSRichard Henderson                 full->lg_page_size = TARGET_PAGE_BITS;
160163fa5caSBlue Swirl                 break;
161163fa5caSBlue Swirl             case 2: /* L2 PTE */
1621658dd32SBlue Swirl                 page_offset = address & 0x3f000;
16371b7794bSRichard Henderson                 full->lg_page_size = 18;
164163fa5caSBlue Swirl             }
165163fa5caSBlue Swirl             break;
166163fa5caSBlue Swirl         case 2: /* L1 PTE */
1671658dd32SBlue Swirl             page_offset = address & 0xfff000;
16871b7794bSRichard Henderson             full->lg_page_size = 24;
16971b7794bSRichard Henderson             break;
170163fa5caSBlue Swirl         }
171163fa5caSBlue Swirl     }
172163fa5caSBlue Swirl 
173163fa5caSBlue Swirl     /* check access */
174163fa5caSBlue Swirl     access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
175163fa5caSBlue Swirl     error_code = access_table[*access_index][access_perms];
176163fa5caSBlue Swirl     if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user)) {
177163fa5caSBlue Swirl         return error_code;
178163fa5caSBlue Swirl     }
179163fa5caSBlue Swirl 
180163fa5caSBlue Swirl     /* update page modified and dirty bits */
181163fa5caSBlue Swirl     is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
182163fa5caSBlue Swirl     if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
183163fa5caSBlue Swirl         pde |= PG_ACCESSED_MASK;
184163fa5caSBlue Swirl         if (is_dirty) {
185163fa5caSBlue Swirl             pde |= PG_MODIFIED_MASK;
186163fa5caSBlue Swirl         }
1872198a121SEdgar E. Iglesias         stl_phys_notdirty(cs->as, pde_ptr, pde);
188163fa5caSBlue Swirl     }
189163fa5caSBlue Swirl 
190163fa5caSBlue Swirl     /* the page can be put in the TLB */
19171b7794bSRichard Henderson     full->prot = perm_table[is_user][access_perms];
192163fa5caSBlue Swirl     if (!(pde & PG_MODIFIED_MASK)) {
193163fa5caSBlue Swirl         /* only set write access if already dirty... otherwise wait
194163fa5caSBlue Swirl            for dirty access */
19571b7794bSRichard Henderson         full->prot &= ~PAGE_WRITE;
196163fa5caSBlue Swirl     }
197163fa5caSBlue Swirl 
198163fa5caSBlue Swirl     /* Even if large ptes, we map only one 4KB page in the cache to
199163fa5caSBlue Swirl        avoid filling it too fast */
20071b7794bSRichard Henderson     full->phys_addr = ((hwaddr)(pde & PTE_ADDR_MASK) << 4) + page_offset;
201163fa5caSBlue Swirl     return error_code;
202163fa5caSBlue Swirl }
203163fa5caSBlue Swirl 
204163fa5caSBlue Swirl /* Perform address translation */
205e84942f2SRichard Henderson bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
206e84942f2SRichard Henderson                         MMUAccessType access_type, int mmu_idx,
207e84942f2SRichard Henderson                         bool probe, uintptr_t retaddr)
208163fa5caSBlue Swirl {
2097510454eSAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
2107510454eSAndreas Färber     CPUSPARCState *env = &cpu->env;
21171b7794bSRichard Henderson     CPUTLBEntryFull full = {};
212163fa5caSBlue Swirl     target_ulong vaddr;
21371b7794bSRichard Henderson     int error_code = 0, access_index;
214163fa5caSBlue Swirl 
215e84942f2SRichard Henderson     /*
216e84942f2SRichard Henderson      * TODO: If we ever need tlb_vaddr_to_host for this target,
217e84942f2SRichard Henderson      * then we must figure out how to manipulate FSR and FAR
218e84942f2SRichard Henderson      * when both MMU_NF and probe are set.  In the meantime,
219e84942f2SRichard Henderson      * do not support this use case.
220e84942f2SRichard Henderson      */
221e84942f2SRichard Henderson     assert(!probe);
222e84942f2SRichard Henderson 
2231658dd32SBlue Swirl     address &= TARGET_PAGE_MASK;
22471b7794bSRichard Henderson     error_code = get_physical_address(env, &full, &access_index,
22571b7794bSRichard Henderson                                       address, access_type, mmu_idx);
2261658dd32SBlue Swirl     vaddr = address;
227e84942f2SRichard Henderson     if (likely(error_code == 0)) {
228339aaf5bSAntony Pavlov         qemu_log_mask(CPU_LOG_MMU,
229e84942f2SRichard Henderson                       "Translate at %" VADDR_PRIx " -> "
230883f2c59SPhilippe Mathieu-Daudé                       HWADDR_FMT_plx ", vaddr " TARGET_FMT_lx "\n",
23171b7794bSRichard Henderson                       address, full.phys_addr, vaddr);
23271b7794bSRichard Henderson         tlb_set_page_full(cs, mmu_idx, vaddr, &full);
233e84942f2SRichard Henderson         return true;
234163fa5caSBlue Swirl     }
235163fa5caSBlue Swirl 
236163fa5caSBlue Swirl     if (env->mmuregs[3]) { /* Fault status register */
237163fa5caSBlue Swirl         env->mmuregs[3] = 1; /* overflow (not read before another fault) */
238163fa5caSBlue Swirl     }
239163fa5caSBlue Swirl     env->mmuregs[3] |= (access_index << 5) | error_code | 2;
240163fa5caSBlue Swirl     env->mmuregs[4] = address; /* Fault address register */
241163fa5caSBlue Swirl 
242163fa5caSBlue Swirl     if ((env->mmuregs[0] & MMU_NF) || env->psret == 0)  {
243163fa5caSBlue Swirl         /* No fault mode: if a mapping is available, just override
244163fa5caSBlue Swirl            permissions. If no mapping is available, redirect accesses to
245163fa5caSBlue Swirl            neverland. Fake/overridden mappings will be flushed when
246163fa5caSBlue Swirl            switching to normal mode. */
24771b7794bSRichard Henderson         full.prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
24871b7794bSRichard Henderson         tlb_set_page_full(cs, mmu_idx, vaddr, &full);
249e84942f2SRichard Henderson         return true;
250163fa5caSBlue Swirl     } else {
251e84942f2SRichard Henderson         if (access_type == MMU_INST_FETCH) {
25227103424SAndreas Färber             cs->exception_index = TT_TFAULT;
253163fa5caSBlue Swirl         } else {
25427103424SAndreas Färber             cs->exception_index = TT_DFAULT;
255163fa5caSBlue Swirl         }
256e84942f2SRichard Henderson         cpu_loop_exit_restore(cs, retaddr);
257163fa5caSBlue Swirl     }
258163fa5caSBlue Swirl }
259163fa5caSBlue Swirl 
260c5f9864eSAndreas Färber target_ulong mmu_probe(CPUSPARCState *env, target_ulong address, int mmulev)
261163fa5caSBlue Swirl {
2625a59fbceSRichard Henderson     CPUState *cs = env_cpu(env);
263a8170e5eSAvi Kivity     hwaddr pde_ptr;
264163fa5caSBlue Swirl     uint32_t pde;
265d86a9ad3SPeter Maydell     MemTxResult result;
266d86a9ad3SPeter Maydell 
267d86a9ad3SPeter Maydell     /*
268d86a9ad3SPeter Maydell      * TODO: MMU probe operations are supposed to set the fault
269d86a9ad3SPeter Maydell      * status registers, but we don't do this.
270d86a9ad3SPeter Maydell      */
271163fa5caSBlue Swirl 
272163fa5caSBlue Swirl     /* Context base + context number */
273a8170e5eSAvi Kivity     pde_ptr = (hwaddr)(env->mmuregs[1] << 4) +
274163fa5caSBlue Swirl         (env->mmuregs[2] << 2);
275d86a9ad3SPeter Maydell     pde = address_space_ldl(cs->as, pde_ptr, MEMTXATTRS_UNSPECIFIED, &result);
276d86a9ad3SPeter Maydell     if (result != MEMTX_OK) {
277d86a9ad3SPeter Maydell         return 0;
278d86a9ad3SPeter Maydell     }
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);
291d86a9ad3SPeter Maydell         pde = address_space_ldl(cs->as, pde_ptr,
292d86a9ad3SPeter Maydell                                 MEMTXATTRS_UNSPECIFIED, &result);
293d86a9ad3SPeter Maydell         if (result != MEMTX_OK) {
294d86a9ad3SPeter Maydell             return 0;
295d86a9ad3SPeter Maydell         }
296163fa5caSBlue Swirl 
297163fa5caSBlue Swirl         switch (pde & PTE_ENTRYTYPE_MASK) {
298163fa5caSBlue Swirl         default:
299163fa5caSBlue Swirl         case 0: /* Invalid */
300163fa5caSBlue Swirl         case 3: /* Reserved */
301163fa5caSBlue Swirl             return 0;
302163fa5caSBlue Swirl         case 2: /* L1 PTE */
303163fa5caSBlue Swirl             return pde;
304163fa5caSBlue Swirl         case 1: /* L2 PDE */
305163fa5caSBlue Swirl             if (mmulev == 2) {
306163fa5caSBlue Swirl                 return pde;
307163fa5caSBlue Swirl             }
308163fa5caSBlue Swirl             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
309d86a9ad3SPeter Maydell             pde = address_space_ldl(cs->as, pde_ptr,
310d86a9ad3SPeter Maydell                                     MEMTXATTRS_UNSPECIFIED, &result);
311d86a9ad3SPeter Maydell             if (result != MEMTX_OK) {
312d86a9ad3SPeter Maydell                 return 0;
313d86a9ad3SPeter Maydell             }
314163fa5caSBlue Swirl 
315163fa5caSBlue Swirl             switch (pde & PTE_ENTRYTYPE_MASK) {
316163fa5caSBlue Swirl             default:
317163fa5caSBlue Swirl             case 0: /* Invalid */
318163fa5caSBlue Swirl             case 3: /* Reserved */
319163fa5caSBlue Swirl                 return 0;
320163fa5caSBlue Swirl             case 2: /* L2 PTE */
321163fa5caSBlue Swirl                 return pde;
322163fa5caSBlue Swirl             case 1: /* L3 PDE */
323163fa5caSBlue Swirl                 if (mmulev == 1) {
324163fa5caSBlue Swirl                     return pde;
325163fa5caSBlue Swirl                 }
326163fa5caSBlue Swirl                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
327d86a9ad3SPeter Maydell                 pde = address_space_ldl(cs->as, pde_ptr,
328d86a9ad3SPeter Maydell                                         MEMTXATTRS_UNSPECIFIED, &result);
329d86a9ad3SPeter Maydell                 if (result != MEMTX_OK) {
330d86a9ad3SPeter Maydell                     return 0;
331d86a9ad3SPeter Maydell                 }
332163fa5caSBlue Swirl 
333163fa5caSBlue Swirl                 switch (pde & PTE_ENTRYTYPE_MASK) {
334163fa5caSBlue Swirl                 default:
335163fa5caSBlue Swirl                 case 0: /* Invalid */
336163fa5caSBlue Swirl                 case 1: /* PDE, should not happen */
337163fa5caSBlue Swirl                 case 3: /* Reserved */
338163fa5caSBlue Swirl                     return 0;
339163fa5caSBlue Swirl                 case 2: /* L3 PTE */
340163fa5caSBlue Swirl                     return pde;
341163fa5caSBlue Swirl                 }
342163fa5caSBlue Swirl             }
343163fa5caSBlue Swirl         }
344163fa5caSBlue Swirl     }
345163fa5caSBlue Swirl     return 0;
346163fa5caSBlue Swirl }
347163fa5caSBlue Swirl 
348fad866daSMarkus Armbruster void dump_mmu(CPUSPARCState *env)
349163fa5caSBlue Swirl {
3505a59fbceSRichard Henderson     CPUState *cs = env_cpu(env);
351163fa5caSBlue Swirl     target_ulong va, va1, va2;
352163fa5caSBlue Swirl     unsigned int n, m, o;
3539dffeec2SPeter Maydell     hwaddr pa;
354163fa5caSBlue Swirl     uint32_t pde;
355163fa5caSBlue Swirl 
356883f2c59SPhilippe Mathieu-Daudé     qemu_printf("Root ptr: " HWADDR_FMT_plx ", ctx: %d\n",
357a8170e5eSAvi Kivity                 (hwaddr)env->mmuregs[1] << 4, env->mmuregs[2]);
358163fa5caSBlue Swirl     for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
359163fa5caSBlue Swirl         pde = mmu_probe(env, va, 2);
360163fa5caSBlue Swirl         if (pde) {
36100b941e5SAndreas Färber             pa = cpu_get_phys_page_debug(cs, va);
362883f2c59SPhilippe Mathieu-Daudé             qemu_printf("VA: " TARGET_FMT_lx ", PA: " HWADDR_FMT_plx
363163fa5caSBlue Swirl                         " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
364163fa5caSBlue Swirl             for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
365163fa5caSBlue Swirl                 pde = mmu_probe(env, va1, 1);
366163fa5caSBlue Swirl                 if (pde) {
36700b941e5SAndreas Färber                     pa = cpu_get_phys_page_debug(cs, va1);
368fad866daSMarkus Armbruster                     qemu_printf(" VA: " TARGET_FMT_lx ", PA: "
369883f2c59SPhilippe Mathieu-Daudé                                 HWADDR_FMT_plx " PDE: " TARGET_FMT_lx "\n",
370163fa5caSBlue Swirl                                 va1, pa, pde);
371163fa5caSBlue Swirl                     for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
372163fa5caSBlue Swirl                         pde = mmu_probe(env, va2, 0);
373163fa5caSBlue Swirl                         if (pde) {
37400b941e5SAndreas Färber                             pa = cpu_get_phys_page_debug(cs, va2);
375fad866daSMarkus Armbruster                             qemu_printf("  VA: " TARGET_FMT_lx ", PA: "
376883f2c59SPhilippe Mathieu-Daudé                                         HWADDR_FMT_plx " PTE: "
377163fa5caSBlue Swirl                                         TARGET_FMT_lx "\n",
378163fa5caSBlue Swirl                                         va2, pa, pde);
379163fa5caSBlue Swirl                         }
380163fa5caSBlue Swirl                     }
381163fa5caSBlue Swirl                 }
382163fa5caSBlue Swirl             }
383163fa5caSBlue Swirl         }
384163fa5caSBlue Swirl     }
385163fa5caSBlue Swirl }
386163fa5caSBlue Swirl 
387163fa5caSBlue Swirl /* Gdb expects all registers windows to be flushed in ram. This function handles
388163fa5caSBlue Swirl  * reads (and only reads) in stack frames as if windows were flushed. We assume
389163fa5caSBlue Swirl  * that the sparc ABI is followed.
390163fa5caSBlue Swirl  */
391f3659eeeSAndreas Färber int sparc_cpu_memory_rw_debug(CPUState *cs, vaddr address,
392f3659eeeSAndreas Färber                               uint8_t *buf, int len, bool is_write)
393163fa5caSBlue Swirl {
394f3659eeeSAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
395f3659eeeSAndreas Färber     CPUSPARCState *env = &cpu->env;
396f3659eeeSAndreas Färber     target_ulong addr = address;
397163fa5caSBlue Swirl     int i;
398163fa5caSBlue Swirl     int len1;
399163fa5caSBlue Swirl     int cwp = env->cwp;
400163fa5caSBlue Swirl 
401163fa5caSBlue Swirl     if (!is_write) {
402163fa5caSBlue Swirl         for (i = 0; i < env->nwindows; i++) {
403163fa5caSBlue Swirl             int off;
404163fa5caSBlue Swirl             target_ulong fp = env->regbase[cwp * 16 + 22];
405163fa5caSBlue Swirl 
406163fa5caSBlue Swirl             /* Assume fp == 0 means end of frame.  */
407163fa5caSBlue Swirl             if (fp == 0) {
408163fa5caSBlue Swirl                 break;
409163fa5caSBlue Swirl             }
410163fa5caSBlue Swirl 
411163fa5caSBlue Swirl             cwp = cpu_cwp_inc(env, cwp + 1);
412163fa5caSBlue Swirl 
413163fa5caSBlue Swirl             /* Invalid window ? */
414163fa5caSBlue Swirl             if (env->wim & (1 << cwp)) {
415163fa5caSBlue Swirl                 break;
416163fa5caSBlue Swirl             }
417163fa5caSBlue Swirl 
418163fa5caSBlue Swirl             /* According to the ABI, the stack is growing downward.  */
419163fa5caSBlue Swirl             if (addr + len < fp) {
420163fa5caSBlue Swirl                 break;
421163fa5caSBlue Swirl             }
422163fa5caSBlue Swirl 
423163fa5caSBlue Swirl             /* Not in this frame.  */
424163fa5caSBlue Swirl             if (addr > fp + 64) {
425163fa5caSBlue Swirl                 continue;
426163fa5caSBlue Swirl             }
427163fa5caSBlue Swirl 
428163fa5caSBlue Swirl             /* Handle access before this window.  */
429163fa5caSBlue Swirl             if (addr < fp) {
430163fa5caSBlue Swirl                 len1 = fp - addr;
431f17ec444SAndreas Färber                 if (cpu_memory_rw_debug(cs, addr, buf, len1, is_write) != 0) {
432163fa5caSBlue Swirl                     return -1;
433163fa5caSBlue Swirl                 }
434163fa5caSBlue Swirl                 addr += len1;
435163fa5caSBlue Swirl                 len -= len1;
436163fa5caSBlue Swirl                 buf += len1;
437163fa5caSBlue Swirl             }
438163fa5caSBlue Swirl 
439163fa5caSBlue Swirl             /* Access byte per byte to registers. Not very efficient but speed
440163fa5caSBlue Swirl              * is not critical.
441163fa5caSBlue Swirl              */
442163fa5caSBlue Swirl             off = addr - fp;
443163fa5caSBlue Swirl             len1 = 64 - off;
444163fa5caSBlue Swirl 
445163fa5caSBlue Swirl             if (len1 > len) {
446163fa5caSBlue Swirl                 len1 = len;
447163fa5caSBlue Swirl             }
448163fa5caSBlue Swirl 
449163fa5caSBlue Swirl             for (; len1; len1--) {
450163fa5caSBlue Swirl                 int reg = cwp * 16 + 8 + (off >> 2);
451163fa5caSBlue Swirl                 union {
452163fa5caSBlue Swirl                     uint32_t v;
453163fa5caSBlue Swirl                     uint8_t c[4];
454163fa5caSBlue Swirl                 } u;
455163fa5caSBlue Swirl                 u.v = cpu_to_be32(env->regbase[reg]);
456163fa5caSBlue Swirl                 *buf++ = u.c[off & 3];
457163fa5caSBlue Swirl                 addr++;
458163fa5caSBlue Swirl                 len--;
459163fa5caSBlue Swirl                 off++;
460163fa5caSBlue Swirl             }
461163fa5caSBlue Swirl 
462163fa5caSBlue Swirl             if (len == 0) {
463163fa5caSBlue Swirl                 return 0;
464163fa5caSBlue Swirl             }
465163fa5caSBlue Swirl         }
466163fa5caSBlue Swirl     }
467f17ec444SAndreas Färber     return cpu_memory_rw_debug(cs, addr, buf, len, is_write);
468163fa5caSBlue Swirl }
469163fa5caSBlue Swirl 
470163fa5caSBlue Swirl #else /* !TARGET_SPARC64 */
471163fa5caSBlue Swirl 
472163fa5caSBlue Swirl /* 41 bit physical address space */
473a8170e5eSAvi Kivity static inline hwaddr ultrasparc_truncate_physical(uint64_t x)
474163fa5caSBlue Swirl {
475163fa5caSBlue Swirl     return x & 0x1ffffffffffULL;
476163fa5caSBlue Swirl }
477163fa5caSBlue Swirl 
478163fa5caSBlue Swirl /*
479163fa5caSBlue Swirl  * UltraSparc IIi I/DMMUs
480163fa5caSBlue Swirl  */
481163fa5caSBlue Swirl 
482163fa5caSBlue Swirl /* Returns true if TTE tag is valid and matches virtual address value
483163fa5caSBlue Swirl    in context requires virtual address mask value calculated from TTE
484163fa5caSBlue Swirl    entry size */
485163fa5caSBlue Swirl static inline int ultrasparc_tag_match(SparcTLBEntry *tlb,
486163fa5caSBlue Swirl                                        uint64_t address, uint64_t context,
487a8170e5eSAvi Kivity                                        hwaddr *physical)
488163fa5caSBlue Swirl {
489913b5f28SArtyom Tarasenko     uint64_t mask = -(8192ULL << 3 * TTE_PGSIZE(tlb->tte));
490163fa5caSBlue Swirl 
491163fa5caSBlue Swirl     /* valid, context match, virtual address match? */
492163fa5caSBlue Swirl     if (TTE_IS_VALID(tlb->tte) &&
493163fa5caSBlue Swirl         (TTE_IS_GLOBAL(tlb->tte) || tlb_compare_context(tlb, context))
494163fa5caSBlue Swirl         && compare_masked(address, tlb->tag, mask)) {
495163fa5caSBlue Swirl         /* decode physical address */
496163fa5caSBlue Swirl         *physical = ((tlb->tte & mask) | (address & ~mask)) & 0x1ffffffe000ULL;
497163fa5caSBlue Swirl         return 1;
498163fa5caSBlue Swirl     }
499163fa5caSBlue Swirl 
500163fa5caSBlue Swirl     return 0;
501163fa5caSBlue Swirl }
502163fa5caSBlue Swirl 
503c0e0c6feSRichard Henderson static uint64_t build_sfsr(CPUSPARCState *env, int mmu_idx, int rw)
504c0e0c6feSRichard Henderson {
505c0e0c6feSRichard Henderson     uint64_t sfsr = SFSR_VALID_BIT;
506c0e0c6feSRichard Henderson 
507c0e0c6feSRichard Henderson     switch (mmu_idx) {
508c0e0c6feSRichard Henderson     case MMU_PHYS_IDX:
509c0e0c6feSRichard Henderson         sfsr |= SFSR_CT_NOTRANS;
510c0e0c6feSRichard Henderson         break;
511c0e0c6feSRichard Henderson     case MMU_USER_IDX:
512c0e0c6feSRichard Henderson     case MMU_KERNEL_IDX:
513c0e0c6feSRichard Henderson         sfsr |= SFSR_CT_PRIMARY;
514c0e0c6feSRichard Henderson         break;
515c0e0c6feSRichard Henderson     case MMU_USER_SECONDARY_IDX:
516c0e0c6feSRichard Henderson     case MMU_KERNEL_SECONDARY_IDX:
517c0e0c6feSRichard Henderson         sfsr |= SFSR_CT_SECONDARY;
518c0e0c6feSRichard Henderson         break;
519c0e0c6feSRichard Henderson     case MMU_NUCLEUS_IDX:
520c0e0c6feSRichard Henderson         sfsr |= SFSR_CT_NUCLEUS;
521c0e0c6feSRichard Henderson         break;
522c0e0c6feSRichard Henderson     default:
523c0e0c6feSRichard Henderson         g_assert_not_reached();
524c0e0c6feSRichard Henderson     }
525c0e0c6feSRichard Henderson 
526c0e0c6feSRichard Henderson     if (rw == 1) {
527c0e0c6feSRichard Henderson         sfsr |= SFSR_WRITE_BIT;
528c0e0c6feSRichard Henderson     } else if (rw == 4) {
529c0e0c6feSRichard Henderson         sfsr |= SFSR_NF_BIT;
530c0e0c6feSRichard Henderson     }
531c0e0c6feSRichard Henderson 
532c0e0c6feSRichard Henderson     if (env->pstate & PS_PRIV) {
533c0e0c6feSRichard Henderson         sfsr |= SFSR_PR_BIT;
534c0e0c6feSRichard Henderson     }
535c0e0c6feSRichard Henderson 
536c0e0c6feSRichard Henderson     if (env->dmmu.sfsr & SFSR_VALID_BIT) { /* Fault status register */
537c0e0c6feSRichard Henderson         sfsr |= SFSR_OW_BIT; /* overflow (not read before another fault) */
538c0e0c6feSRichard Henderson     }
539c0e0c6feSRichard Henderson 
540c0e0c6feSRichard Henderson     /* FIXME: ASI field in SFSR must be set */
541c0e0c6feSRichard Henderson 
542c0e0c6feSRichard Henderson     return sfsr;
543c0e0c6feSRichard Henderson }
544c0e0c6feSRichard Henderson 
54571b7794bSRichard Henderson static int get_physical_address_data(CPUSPARCState *env, CPUTLBEntryFull *full,
546163fa5caSBlue Swirl                                      target_ulong address, int rw, int mmu_idx)
547163fa5caSBlue Swirl {
5485a59fbceSRichard Henderson     CPUState *cs = env_cpu(env);
549163fa5caSBlue Swirl     unsigned int i;
550c0e0c6feSRichard Henderson     uint64_t sfsr;
551163fa5caSBlue Swirl     uint64_t context;
552af7a06baSRichard Henderson     bool is_user = false;
553163fa5caSBlue Swirl 
554c0e0c6feSRichard Henderson     sfsr = build_sfsr(env, mmu_idx, rw);
555c0e0c6feSRichard Henderson 
556163fa5caSBlue Swirl     switch (mmu_idx) {
557af7a06baSRichard Henderson     case MMU_PHYS_IDX:
558af7a06baSRichard Henderson         g_assert_not_reached();
559163fa5caSBlue Swirl     case MMU_USER_IDX:
560af7a06baSRichard Henderson         is_user = true;
561af7a06baSRichard Henderson         /* fallthru */
562163fa5caSBlue Swirl     case MMU_KERNEL_IDX:
563163fa5caSBlue Swirl         context = env->dmmu.mmu_primary_context & 0x1fff;
564163fa5caSBlue Swirl         break;
565163fa5caSBlue Swirl     case MMU_USER_SECONDARY_IDX:
566af7a06baSRichard Henderson         is_user = true;
567af7a06baSRichard Henderson         /* fallthru */
568163fa5caSBlue Swirl     case MMU_KERNEL_SECONDARY_IDX:
569163fa5caSBlue Swirl         context = env->dmmu.mmu_secondary_context & 0x1fff;
570163fa5caSBlue Swirl         break;
571163fa5caSBlue Swirl     default:
572163fa5caSBlue Swirl         context = 0;
573163fa5caSBlue Swirl         break;
574163fa5caSBlue Swirl     }
575163fa5caSBlue Swirl 
576163fa5caSBlue Swirl     for (i = 0; i < 64; i++) {
577163fa5caSBlue Swirl         /* ctx match, vaddr match, valid? */
57871b7794bSRichard Henderson         if (ultrasparc_tag_match(&env->dtlb[i], address, context,
57971b7794bSRichard Henderson                                  &full->phys_addr)) {
580163fa5caSBlue Swirl             int do_fault = 0;
581163fa5caSBlue Swirl 
582ccdb4c55STony Nguyen             if (TTE_IS_IE(env->dtlb[i].tte)) {
583*a0ff4a87SRichard Henderson                 full->tlb_fill_flags |= TLB_BSWAP;
584ccdb4c55STony Nguyen             }
585ccdb4c55STony Nguyen 
586163fa5caSBlue Swirl             /* access ok? */
587163fa5caSBlue Swirl             /* multiple bits in SFSR.FT may be set on TT_DFAULT */
588163fa5caSBlue Swirl             if (TTE_IS_PRIV(env->dtlb[i].tte) && is_user) {
589163fa5caSBlue Swirl                 do_fault = 1;
590163fa5caSBlue Swirl                 sfsr |= SFSR_FT_PRIV_BIT; /* privilege violation */
591ec0ceb17SBlue Swirl                 trace_mmu_helper_dfault(address, context, mmu_idx, env->tl);
592163fa5caSBlue Swirl             }
593163fa5caSBlue Swirl             if (rw == 4) {
594163fa5caSBlue Swirl                 if (TTE_IS_SIDEEFFECT(env->dtlb[i].tte)) {
595163fa5caSBlue Swirl                     do_fault = 1;
596163fa5caSBlue Swirl                     sfsr |= SFSR_FT_NF_E_BIT;
597163fa5caSBlue Swirl                 }
598163fa5caSBlue Swirl             } else {
599163fa5caSBlue Swirl                 if (TTE_IS_NFO(env->dtlb[i].tte)) {
600163fa5caSBlue Swirl                     do_fault = 1;
601163fa5caSBlue Swirl                     sfsr |= SFSR_FT_NFO_BIT;
602163fa5caSBlue Swirl                 }
603163fa5caSBlue Swirl             }
604163fa5caSBlue Swirl 
605163fa5caSBlue Swirl             if (do_fault) {
606163fa5caSBlue Swirl                 /* faults above are reported with TT_DFAULT. */
60727103424SAndreas Färber                 cs->exception_index = TT_DFAULT;
608163fa5caSBlue Swirl             } else if (!TTE_IS_W_OK(env->dtlb[i].tte) && (rw == 1)) {
609163fa5caSBlue Swirl                 do_fault = 1;
61027103424SAndreas Färber                 cs->exception_index = TT_DPROT;
611163fa5caSBlue Swirl 
612ec0ceb17SBlue Swirl                 trace_mmu_helper_dprot(address, context, mmu_idx, env->tl);
613163fa5caSBlue Swirl             }
614163fa5caSBlue Swirl 
615163fa5caSBlue Swirl             if (!do_fault) {
61671b7794bSRichard Henderson                 full->prot = PAGE_READ;
617163fa5caSBlue Swirl                 if (TTE_IS_W_OK(env->dtlb[i].tte)) {
61871b7794bSRichard Henderson                     full->prot |= PAGE_WRITE;
619163fa5caSBlue Swirl                 }
620163fa5caSBlue Swirl 
621163fa5caSBlue Swirl                 TTE_SET_USED(env->dtlb[i].tte);
622163fa5caSBlue Swirl 
623163fa5caSBlue Swirl                 return 0;
624163fa5caSBlue Swirl             }
625163fa5caSBlue Swirl 
626c0e0c6feSRichard Henderson             env->dmmu.sfsr = sfsr;
627163fa5caSBlue Swirl             env->dmmu.sfar = address; /* Fault address register */
628163fa5caSBlue Swirl             env->dmmu.tag_access = (address & ~0x1fffULL) | context;
629163fa5caSBlue Swirl             return 1;
630163fa5caSBlue Swirl         }
631163fa5caSBlue Swirl     }
632163fa5caSBlue Swirl 
633ec0ceb17SBlue Swirl     trace_mmu_helper_dmiss(address, context);
634163fa5caSBlue Swirl 
635163fa5caSBlue Swirl     /*
636163fa5caSBlue Swirl      * On MMU misses:
637163fa5caSBlue Swirl      * - UltraSPARC IIi: SFSR and SFAR unmodified
638163fa5caSBlue Swirl      * - JPS1: SFAR updated and some fields of SFSR updated
639163fa5caSBlue Swirl      */
640163fa5caSBlue Swirl     env->dmmu.tag_access = (address & ~0x1fffULL) | context;
64127103424SAndreas Färber     cs->exception_index = TT_DMISS;
642163fa5caSBlue Swirl     return 1;
643163fa5caSBlue Swirl }
644163fa5caSBlue Swirl 
64571b7794bSRichard Henderson static int get_physical_address_code(CPUSPARCState *env, CPUTLBEntryFull *full,
646163fa5caSBlue Swirl                                      target_ulong address, int mmu_idx)
647163fa5caSBlue Swirl {
6485a59fbceSRichard Henderson     CPUState *cs = env_cpu(env);
649163fa5caSBlue Swirl     unsigned int i;
650163fa5caSBlue Swirl     uint64_t context;
651af7a06baSRichard Henderson     bool is_user = false;
652163fa5caSBlue Swirl 
653af7a06baSRichard Henderson     switch (mmu_idx) {
654af7a06baSRichard Henderson     case MMU_PHYS_IDX:
655af7a06baSRichard Henderson     case MMU_USER_SECONDARY_IDX:
656af7a06baSRichard Henderson     case MMU_KERNEL_SECONDARY_IDX:
657af7a06baSRichard Henderson         g_assert_not_reached();
658af7a06baSRichard Henderson     case MMU_USER_IDX:
659af7a06baSRichard Henderson         is_user = true;
660af7a06baSRichard Henderson         /* fallthru */
661af7a06baSRichard Henderson     case MMU_KERNEL_IDX:
662af7a06baSRichard Henderson         context = env->dmmu.mmu_primary_context & 0x1fff;
663af7a06baSRichard Henderson         break;
664af7a06baSRichard Henderson     default:
665af7a06baSRichard Henderson         context = 0;
666af7a06baSRichard Henderson         break;
667163fa5caSBlue Swirl     }
668163fa5caSBlue Swirl 
669163fa5caSBlue Swirl     if (env->tl == 0) {
670163fa5caSBlue Swirl         /* PRIMARY context */
671163fa5caSBlue Swirl         context = env->dmmu.mmu_primary_context & 0x1fff;
672163fa5caSBlue Swirl     } else {
673163fa5caSBlue Swirl         /* NUCLEUS context */
674163fa5caSBlue Swirl         context = 0;
675163fa5caSBlue Swirl     }
676163fa5caSBlue Swirl 
677163fa5caSBlue Swirl     for (i = 0; i < 64; i++) {
678163fa5caSBlue Swirl         /* ctx match, vaddr match, valid? */
679163fa5caSBlue Swirl         if (ultrasparc_tag_match(&env->itlb[i],
68071b7794bSRichard Henderson                                  address, context, &full->phys_addr)) {
681163fa5caSBlue Swirl             /* access ok? */
682163fa5caSBlue Swirl             if (TTE_IS_PRIV(env->itlb[i].tte) && is_user) {
683163fa5caSBlue Swirl                 /* Fault status register */
684163fa5caSBlue Swirl                 if (env->immu.sfsr & SFSR_VALID_BIT) {
685163fa5caSBlue Swirl                     env->immu.sfsr = SFSR_OW_BIT; /* overflow (not read before
686163fa5caSBlue Swirl                                                      another fault) */
687163fa5caSBlue Swirl                 } else {
688163fa5caSBlue Swirl                     env->immu.sfsr = 0;
689163fa5caSBlue Swirl                 }
690163fa5caSBlue Swirl                 if (env->pstate & PS_PRIV) {
691163fa5caSBlue Swirl                     env->immu.sfsr |= SFSR_PR_BIT;
692163fa5caSBlue Swirl                 }
693163fa5caSBlue Swirl                 if (env->tl > 0) {
694163fa5caSBlue Swirl                     env->immu.sfsr |= SFSR_CT_NUCLEUS;
695163fa5caSBlue Swirl                 }
696163fa5caSBlue Swirl 
697163fa5caSBlue Swirl                 /* FIXME: ASI field in SFSR must be set */
698163fa5caSBlue Swirl                 env->immu.sfsr |= SFSR_FT_PRIV_BIT | SFSR_VALID_BIT;
69927103424SAndreas Färber                 cs->exception_index = TT_TFAULT;
700163fa5caSBlue Swirl 
701163fa5caSBlue Swirl                 env->immu.tag_access = (address & ~0x1fffULL) | context;
702163fa5caSBlue Swirl 
703ec0ceb17SBlue Swirl                 trace_mmu_helper_tfault(address, context);
704163fa5caSBlue Swirl 
705163fa5caSBlue Swirl                 return 1;
706163fa5caSBlue Swirl             }
70771b7794bSRichard Henderson             full->prot = PAGE_EXEC;
708163fa5caSBlue Swirl             TTE_SET_USED(env->itlb[i].tte);
709163fa5caSBlue Swirl             return 0;
710163fa5caSBlue Swirl         }
711163fa5caSBlue Swirl     }
712163fa5caSBlue Swirl 
713ec0ceb17SBlue Swirl     trace_mmu_helper_tmiss(address, context);
714163fa5caSBlue Swirl 
715163fa5caSBlue Swirl     /* Context is stored in DMMU (dmmuregs[1]) also for IMMU */
716163fa5caSBlue Swirl     env->immu.tag_access = (address & ~0x1fffULL) | context;
71727103424SAndreas Färber     cs->exception_index = TT_TMISS;
718163fa5caSBlue Swirl     return 1;
719163fa5caSBlue Swirl }
720163fa5caSBlue Swirl 
72171b7794bSRichard Henderson static int get_physical_address(CPUSPARCState *env, CPUTLBEntryFull *full,
72271b7794bSRichard Henderson                                 int *access_index, target_ulong address,
72371b7794bSRichard Henderson                                 int rw, int mmu_idx)
724163fa5caSBlue Swirl {
725163fa5caSBlue Swirl     /* ??? We treat everything as a small page, then explicitly flush
726163fa5caSBlue Swirl        everything when an entry is evicted.  */
72771b7794bSRichard Henderson     full->lg_page_size = TARGET_PAGE_BITS;
728163fa5caSBlue Swirl 
729163fa5caSBlue Swirl     /* safety net to catch wrong softmmu index use from dynamic code */
730163fa5caSBlue Swirl     if (env->tl > 0 && mmu_idx != MMU_NUCLEUS_IDX) {
731ec0ceb17SBlue Swirl         if (rw == 2) {
732ec0ceb17SBlue Swirl             trace_mmu_helper_get_phys_addr_code(env->tl, mmu_idx,
733ec0ceb17SBlue Swirl                                                 env->dmmu.mmu_primary_context,
734ec0ceb17SBlue Swirl                                                 env->dmmu.mmu_secondary_context,
735ec0ceb17SBlue Swirl                                                 address);
736ec0ceb17SBlue Swirl         } else {
737ec0ceb17SBlue Swirl             trace_mmu_helper_get_phys_addr_data(env->tl, mmu_idx,
738163fa5caSBlue Swirl                                                 env->dmmu.mmu_primary_context,
739163fa5caSBlue Swirl                                                 env->dmmu.mmu_secondary_context,
740163fa5caSBlue Swirl                                                 address);
741163fa5caSBlue Swirl         }
742ec0ceb17SBlue Swirl     }
743163fa5caSBlue Swirl 
744af7a06baSRichard Henderson     if (mmu_idx == MMU_PHYS_IDX) {
74571b7794bSRichard Henderson         full->phys_addr = ultrasparc_truncate_physical(address);
74671b7794bSRichard Henderson         full->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
747af7a06baSRichard Henderson         return 0;
748af7a06baSRichard Henderson     }
749af7a06baSRichard Henderson 
750163fa5caSBlue Swirl     if (rw == 2) {
75171b7794bSRichard Henderson         return get_physical_address_code(env, full, address, mmu_idx);
752163fa5caSBlue Swirl     } else {
75371b7794bSRichard Henderson         return get_physical_address_data(env, full, address, rw, mmu_idx);
754163fa5caSBlue Swirl     }
755163fa5caSBlue Swirl }
756163fa5caSBlue Swirl 
757163fa5caSBlue Swirl /* Perform address translation */
758e84942f2SRichard Henderson bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
759e84942f2SRichard Henderson                         MMUAccessType access_type, int mmu_idx,
760e84942f2SRichard Henderson                         bool probe, uintptr_t retaddr)
761163fa5caSBlue Swirl {
7627510454eSAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
7637510454eSAndreas Färber     CPUSPARCState *env = &cpu->env;
76471b7794bSRichard Henderson     CPUTLBEntryFull full = {};
76571b7794bSRichard Henderson     int error_code = 0, access_index;
766163fa5caSBlue Swirl 
7671658dd32SBlue Swirl     address &= TARGET_PAGE_MASK;
76871b7794bSRichard Henderson     error_code = get_physical_address(env, &full, &access_index,
76971b7794bSRichard Henderson                                       address, access_type, mmu_idx);
770e84942f2SRichard Henderson     if (likely(error_code == 0)) {
77171b7794bSRichard Henderson         trace_mmu_helper_mmu_fault(address, full.phys_addr, mmu_idx, env->tl,
772163fa5caSBlue Swirl                                    env->dmmu.mmu_primary_context,
773163fa5caSBlue Swirl                                    env->dmmu.mmu_secondary_context);
77471b7794bSRichard Henderson         tlb_set_page_full(cs, mmu_idx, address, &full);
775e84942f2SRichard Henderson         return true;
776163fa5caSBlue Swirl     }
777e84942f2SRichard Henderson     if (probe) {
778e84942f2SRichard Henderson         return false;
779e84942f2SRichard Henderson     }
780e84942f2SRichard Henderson     cpu_loop_exit_restore(cs, retaddr);
781163fa5caSBlue Swirl }
782163fa5caSBlue Swirl 
783fad866daSMarkus Armbruster void dump_mmu(CPUSPARCState *env)
784163fa5caSBlue Swirl {
785163fa5caSBlue Swirl     unsigned int i;
786163fa5caSBlue Swirl     const char *mask;
787163fa5caSBlue Swirl 
788fad866daSMarkus Armbruster     qemu_printf("MMU contexts: Primary: %" PRId64 ", Secondary: %"
789163fa5caSBlue Swirl                 PRId64 "\n",
790163fa5caSBlue Swirl                 env->dmmu.mmu_primary_context,
791163fa5caSBlue Swirl                 env->dmmu.mmu_secondary_context);
792fad866daSMarkus Armbruster     qemu_printf("DMMU Tag Access: %" PRIx64 ", TSB Tag Target: %" PRIx64
793d00a2334SArtyom Tarasenko                 "\n", env->dmmu.tag_access, env->dmmu.tsb_tag_target);
794163fa5caSBlue Swirl     if ((env->lsu & DMMU_E) == 0) {
795fad866daSMarkus Armbruster         qemu_printf("DMMU disabled\n");
796163fa5caSBlue Swirl     } else {
797fad866daSMarkus Armbruster         qemu_printf("DMMU dump\n");
798163fa5caSBlue Swirl         for (i = 0; i < 64; i++) {
799163fa5caSBlue Swirl             switch (TTE_PGSIZE(env->dtlb[i].tte)) {
800163fa5caSBlue Swirl             default:
801163fa5caSBlue Swirl             case 0x0:
802163fa5caSBlue Swirl                 mask = "  8k";
803163fa5caSBlue Swirl                 break;
804163fa5caSBlue Swirl             case 0x1:
805163fa5caSBlue Swirl                 mask = " 64k";
806163fa5caSBlue Swirl                 break;
807163fa5caSBlue Swirl             case 0x2:
808163fa5caSBlue Swirl                 mask = "512k";
809163fa5caSBlue Swirl                 break;
810163fa5caSBlue Swirl             case 0x3:
811163fa5caSBlue Swirl                 mask = "  4M";
812163fa5caSBlue Swirl                 break;
813163fa5caSBlue Swirl             }
814163fa5caSBlue Swirl             if (TTE_IS_VALID(env->dtlb[i].tte)) {
815fad866daSMarkus Armbruster                 qemu_printf("[%02u] VA: %" PRIx64 ", PA: %llx"
816ccdb4c55STony Nguyen                             ", %s, %s, %s, %s, ie %s, ctx %" PRId64 " %s\n",
817163fa5caSBlue Swirl                             i,
818163fa5caSBlue Swirl                             env->dtlb[i].tag & (uint64_t)~0x1fffULL,
819163fa5caSBlue Swirl                             TTE_PA(env->dtlb[i].tte),
820163fa5caSBlue Swirl                             mask,
821163fa5caSBlue Swirl                             TTE_IS_PRIV(env->dtlb[i].tte) ? "priv" : "user",
822163fa5caSBlue Swirl                             TTE_IS_W_OK(env->dtlb[i].tte) ? "RW" : "RO",
823163fa5caSBlue Swirl                             TTE_IS_LOCKED(env->dtlb[i].tte) ?
824163fa5caSBlue Swirl                             "locked" : "unlocked",
825ccdb4c55STony Nguyen                             TTE_IS_IE(env->dtlb[i].tte) ?
826ccdb4c55STony Nguyen                             "yes" : "no",
827163fa5caSBlue Swirl                             env->dtlb[i].tag & (uint64_t)0x1fffULL,
828163fa5caSBlue Swirl                             TTE_IS_GLOBAL(env->dtlb[i].tte) ?
829163fa5caSBlue Swirl                             "global" : "local");
830163fa5caSBlue Swirl             }
831163fa5caSBlue Swirl         }
832163fa5caSBlue Swirl     }
833163fa5caSBlue Swirl     if ((env->lsu & IMMU_E) == 0) {
834fad866daSMarkus Armbruster         qemu_printf("IMMU disabled\n");
835163fa5caSBlue Swirl     } else {
836fad866daSMarkus Armbruster         qemu_printf("IMMU dump\n");
837163fa5caSBlue Swirl         for (i = 0; i < 64; i++) {
838163fa5caSBlue Swirl             switch (TTE_PGSIZE(env->itlb[i].tte)) {
839163fa5caSBlue Swirl             default:
840163fa5caSBlue Swirl             case 0x0:
841163fa5caSBlue Swirl                 mask = "  8k";
842163fa5caSBlue Swirl                 break;
843163fa5caSBlue Swirl             case 0x1:
844163fa5caSBlue Swirl                 mask = " 64k";
845163fa5caSBlue Swirl                 break;
846163fa5caSBlue Swirl             case 0x2:
847163fa5caSBlue Swirl                 mask = "512k";
848163fa5caSBlue Swirl                 break;
849163fa5caSBlue Swirl             case 0x3:
850163fa5caSBlue Swirl                 mask = "  4M";
851163fa5caSBlue Swirl                 break;
852163fa5caSBlue Swirl             }
853163fa5caSBlue Swirl             if (TTE_IS_VALID(env->itlb[i].tte)) {
854fad866daSMarkus Armbruster                 qemu_printf("[%02u] VA: %" PRIx64 ", PA: %llx"
855163fa5caSBlue Swirl                             ", %s, %s, %s, ctx %" PRId64 " %s\n",
856163fa5caSBlue Swirl                             i,
857163fa5caSBlue Swirl                             env->itlb[i].tag & (uint64_t)~0x1fffULL,
858163fa5caSBlue Swirl                             TTE_PA(env->itlb[i].tte),
859163fa5caSBlue Swirl                             mask,
860163fa5caSBlue Swirl                             TTE_IS_PRIV(env->itlb[i].tte) ? "priv" : "user",
861163fa5caSBlue Swirl                             TTE_IS_LOCKED(env->itlb[i].tte) ?
862163fa5caSBlue Swirl                             "locked" : "unlocked",
863163fa5caSBlue Swirl                             env->itlb[i].tag & (uint64_t)0x1fffULL,
864163fa5caSBlue Swirl                             TTE_IS_GLOBAL(env->itlb[i].tte) ?
865163fa5caSBlue Swirl                             "global" : "local");
866163fa5caSBlue Swirl             }
867163fa5caSBlue Swirl         }
868163fa5caSBlue Swirl     }
869163fa5caSBlue Swirl }
870163fa5caSBlue Swirl 
871163fa5caSBlue Swirl #endif /* TARGET_SPARC64 */
872163fa5caSBlue Swirl 
873a8170e5eSAvi Kivity static int cpu_sparc_get_phys_page(CPUSPARCState *env, hwaddr *phys,
874163fa5caSBlue Swirl                                    target_ulong addr, int rw, int mmu_idx)
875163fa5caSBlue Swirl {
87671b7794bSRichard Henderson     CPUTLBEntryFull full = {};
87771b7794bSRichard Henderson     int access_index, ret;
878163fa5caSBlue Swirl 
87971b7794bSRichard Henderson     ret = get_physical_address(env, &full, &access_index, addr, rw, mmu_idx);
88071b7794bSRichard Henderson     if (ret == 0) {
88171b7794bSRichard Henderson         *phys = full.phys_addr;
88271b7794bSRichard Henderson     }
88371b7794bSRichard Henderson     return ret;
884163fa5caSBlue Swirl }
885163fa5caSBlue Swirl 
886163fa5caSBlue Swirl #if defined(TARGET_SPARC64)
887a8170e5eSAvi Kivity hwaddr cpu_get_phys_page_nofault(CPUSPARCState *env, target_ulong addr,
888163fa5caSBlue Swirl                                            int mmu_idx)
889163fa5caSBlue Swirl {
890a8170e5eSAvi Kivity     hwaddr phys_addr;
891163fa5caSBlue Swirl 
892163fa5caSBlue Swirl     if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 4, mmu_idx) != 0) {
893163fa5caSBlue Swirl         return -1;
894163fa5caSBlue Swirl     }
895163fa5caSBlue Swirl     return phys_addr;
896163fa5caSBlue Swirl }
897163fa5caSBlue Swirl #endif
898163fa5caSBlue Swirl 
89900b941e5SAndreas Färber hwaddr sparc_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
900163fa5caSBlue Swirl {
90100b941e5SAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
90200b941e5SAndreas Färber     CPUSPARCState *env = &cpu->env;
903a8170e5eSAvi Kivity     hwaddr phys_addr;
9043b916140SRichard Henderson     int mmu_idx = cpu_mmu_index(cs, false);
905163fa5caSBlue Swirl 
906163fa5caSBlue Swirl     if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 2, mmu_idx) != 0) {
907163fa5caSBlue Swirl         if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 0, mmu_idx) != 0) {
908163fa5caSBlue Swirl             return -1;
909163fa5caSBlue Swirl         }
910163fa5caSBlue Swirl     }
911163fa5caSBlue Swirl     return phys_addr;
912163fa5caSBlue Swirl }
913aebe5153SRichard Henderson 
9148905770bSMarc-André Lureau G_NORETURN void sparc_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
915aebe5153SRichard Henderson                                               MMUAccessType access_type,
916aebe5153SRichard Henderson                                               int mmu_idx,
917aebe5153SRichard Henderson                                               uintptr_t retaddr)
918aebe5153SRichard Henderson {
919aebe5153SRichard Henderson     SPARCCPU *cpu = SPARC_CPU(cs);
920aebe5153SRichard Henderson     CPUSPARCState *env = &cpu->env;
921aebe5153SRichard Henderson 
922aebe5153SRichard Henderson #ifdef TARGET_SPARC64
923aebe5153SRichard Henderson     env->dmmu.sfsr = build_sfsr(env, mmu_idx, access_type);
924aebe5153SRichard Henderson     env->dmmu.sfar = addr;
925aebe5153SRichard Henderson #else
926aebe5153SRichard Henderson     env->mmuregs[4] = addr;
927aebe5153SRichard Henderson #endif
928aebe5153SRichard Henderson 
929aebe5153SRichard Henderson     cpu_raise_exception_ra(env, TT_UNALIGNED, retaddr);
930aebe5153SRichard Henderson }
931