xref: /qemu/target/sparc/mmu_helper.c (revision aebe51538b90f76a9085dbdeccd87f295316a1dd)
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"
21163fa5caSBlue Swirl #include "cpu.h"
2263c91552SPaolo Bonzini #include "exec/exec-all.h"
23fad866daSMarkus Armbruster #include "qemu/qemu-print.h"
24ec0ceb17SBlue Swirl #include "trace.h"
25163fa5caSBlue Swirl 
26163fa5caSBlue Swirl /* Sparc MMU emulation */
27163fa5caSBlue Swirl 
28163fa5caSBlue Swirl #ifndef TARGET_SPARC64
29163fa5caSBlue Swirl /*
30163fa5caSBlue Swirl  * Sparc V8 Reference MMU (SRMMU)
31163fa5caSBlue Swirl  */
32163fa5caSBlue Swirl static const int access_table[8][8] = {
33163fa5caSBlue Swirl     { 0, 0, 0, 0, 8, 0, 12, 12 },
34163fa5caSBlue Swirl     { 0, 0, 0, 0, 8, 0, 0, 0 },
35163fa5caSBlue Swirl     { 8, 8, 0, 0, 0, 8, 12, 12 },
36163fa5caSBlue Swirl     { 8, 8, 0, 0, 0, 8, 0, 0 },
37163fa5caSBlue Swirl     { 8, 0, 8, 0, 8, 8, 12, 12 },
38163fa5caSBlue Swirl     { 8, 0, 8, 0, 8, 0, 8, 0 },
39163fa5caSBlue Swirl     { 8, 8, 8, 0, 8, 8, 12, 12 },
40163fa5caSBlue Swirl     { 8, 8, 8, 0, 8, 8, 8, 0 }
41163fa5caSBlue Swirl };
42163fa5caSBlue Swirl 
43163fa5caSBlue Swirl static const int perm_table[2][8] = {
44163fa5caSBlue Swirl     {
45163fa5caSBlue Swirl         PAGE_READ,
46163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE,
47163fa5caSBlue Swirl         PAGE_READ | PAGE_EXEC,
48163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
49163fa5caSBlue Swirl         PAGE_EXEC,
50163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE,
51163fa5caSBlue Swirl         PAGE_READ | PAGE_EXEC,
52163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE | PAGE_EXEC
53163fa5caSBlue Swirl     },
54163fa5caSBlue Swirl     {
55163fa5caSBlue Swirl         PAGE_READ,
56163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE,
57163fa5caSBlue Swirl         PAGE_READ | PAGE_EXEC,
58163fa5caSBlue Swirl         PAGE_READ | PAGE_WRITE | PAGE_EXEC,
59163fa5caSBlue Swirl         PAGE_EXEC,
60163fa5caSBlue Swirl         PAGE_READ,
61163fa5caSBlue Swirl         0,
62163fa5caSBlue Swirl         0,
63163fa5caSBlue Swirl     }
64163fa5caSBlue Swirl };
65163fa5caSBlue Swirl 
66a8170e5eSAvi Kivity static int get_physical_address(CPUSPARCState *env, hwaddr *physical,
679bed46e6STony Nguyen                                 int *prot, int *access_index, MemTxAttrs *attrs,
68163fa5caSBlue Swirl                                 target_ulong address, int rw, int mmu_idx,
69163fa5caSBlue Swirl                                 target_ulong *page_size)
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) {
82163fa5caSBlue Swirl         *page_size = TARGET_PAGE_SIZE;
83163fa5caSBlue Swirl         /* Boot mode: instruction fetches are taken from PROM */
84576e1c4cSIgor Mammedov         if (rw == 2 && (env->mmuregs[0] & env->def.mmu_bm)) {
85163fa5caSBlue Swirl             *physical = env->prom_addr | (address & 0x7ffffULL);
86163fa5caSBlue Swirl             *prot = PAGE_READ | PAGE_EXEC;
87163fa5caSBlue Swirl             return 0;
88163fa5caSBlue Swirl         }
89163fa5caSBlue Swirl         *physical = address;
90163fa5caSBlue Swirl         *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);
95163fa5caSBlue Swirl     *physical = 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                 }
159163fa5caSBlue Swirl                 *page_size = TARGET_PAGE_SIZE;
160163fa5caSBlue Swirl                 break;
161163fa5caSBlue Swirl             case 2: /* L2 PTE */
1621658dd32SBlue Swirl                 page_offset = address & 0x3f000;
163163fa5caSBlue Swirl                 *page_size = 0x40000;
164163fa5caSBlue Swirl             }
165163fa5caSBlue Swirl             break;
166163fa5caSBlue Swirl         case 2: /* L1 PTE */
1671658dd32SBlue Swirl             page_offset = address & 0xfff000;
168163fa5caSBlue Swirl             *page_size = 0x1000000;
169163fa5caSBlue Swirl         }
170163fa5caSBlue Swirl     }
171163fa5caSBlue Swirl 
172163fa5caSBlue Swirl     /* check access */
173163fa5caSBlue Swirl     access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
174163fa5caSBlue Swirl     error_code = access_table[*access_index][access_perms];
175163fa5caSBlue Swirl     if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user)) {
176163fa5caSBlue Swirl         return error_code;
177163fa5caSBlue Swirl     }
178163fa5caSBlue Swirl 
179163fa5caSBlue Swirl     /* update page modified and dirty bits */
180163fa5caSBlue Swirl     is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
181163fa5caSBlue Swirl     if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
182163fa5caSBlue Swirl         pde |= PG_ACCESSED_MASK;
183163fa5caSBlue Swirl         if (is_dirty) {
184163fa5caSBlue Swirl             pde |= PG_MODIFIED_MASK;
185163fa5caSBlue Swirl         }
1862198a121SEdgar E. Iglesias         stl_phys_notdirty(cs->as, pde_ptr, pde);
187163fa5caSBlue Swirl     }
188163fa5caSBlue Swirl 
189163fa5caSBlue Swirl     /* the page can be put in the TLB */
190163fa5caSBlue Swirl     *prot = perm_table[is_user][access_perms];
191163fa5caSBlue Swirl     if (!(pde & PG_MODIFIED_MASK)) {
192163fa5caSBlue Swirl         /* only set write access if already dirty... otherwise wait
193163fa5caSBlue Swirl            for dirty access */
194163fa5caSBlue Swirl         *prot &= ~PAGE_WRITE;
195163fa5caSBlue Swirl     }
196163fa5caSBlue Swirl 
197163fa5caSBlue Swirl     /* Even if large ptes, we map only one 4KB page in the cache to
198163fa5caSBlue Swirl        avoid filling it too fast */
199a8170e5eSAvi Kivity     *physical = ((hwaddr)(pde & PTE_ADDR_MASK) << 4) + page_offset;
200163fa5caSBlue Swirl     return error_code;
201163fa5caSBlue Swirl }
202163fa5caSBlue Swirl 
203163fa5caSBlue Swirl /* Perform address translation */
204e84942f2SRichard Henderson bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
205e84942f2SRichard Henderson                         MMUAccessType access_type, int mmu_idx,
206e84942f2SRichard Henderson                         bool probe, uintptr_t retaddr)
207163fa5caSBlue Swirl {
2087510454eSAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
2097510454eSAndreas Färber     CPUSPARCState *env = &cpu->env;
210a8170e5eSAvi Kivity     hwaddr paddr;
211163fa5caSBlue Swirl     target_ulong vaddr;
212163fa5caSBlue Swirl     target_ulong page_size;
213163fa5caSBlue Swirl     int error_code = 0, prot, access_index;
2149bed46e6STony Nguyen     MemTxAttrs attrs = {};
215163fa5caSBlue Swirl 
216e84942f2SRichard Henderson     /*
217e84942f2SRichard Henderson      * TODO: If we ever need tlb_vaddr_to_host for this target,
218e84942f2SRichard Henderson      * then we must figure out how to manipulate FSR and FAR
219e84942f2SRichard Henderson      * when both MMU_NF and probe are set.  In the meantime,
220e84942f2SRichard Henderson      * do not support this use case.
221e84942f2SRichard Henderson      */
222e84942f2SRichard Henderson     assert(!probe);
223e84942f2SRichard Henderson 
2241658dd32SBlue Swirl     address &= TARGET_PAGE_MASK;
2259bed46e6STony Nguyen     error_code = get_physical_address(env, &paddr, &prot, &access_index, &attrs,
226e84942f2SRichard Henderson                                       address, access_type,
227e84942f2SRichard Henderson                                       mmu_idx, &page_size);
2281658dd32SBlue Swirl     vaddr = address;
229e84942f2SRichard Henderson     if (likely(error_code == 0)) {
230339aaf5bSAntony Pavlov         qemu_log_mask(CPU_LOG_MMU,
231e84942f2SRichard Henderson                       "Translate at %" VADDR_PRIx " -> "
232e84942f2SRichard Henderson                       TARGET_FMT_plx ", vaddr " TARGET_FMT_lx "\n",
233e84942f2SRichard Henderson                       address, paddr, vaddr);
2340c591eb0SAndreas Färber         tlb_set_page(cs, vaddr, paddr, prot, mmu_idx, page_size);
235e84942f2SRichard Henderson         return true;
236163fa5caSBlue Swirl     }
237163fa5caSBlue Swirl 
238163fa5caSBlue Swirl     if (env->mmuregs[3]) { /* Fault status register */
239163fa5caSBlue Swirl         env->mmuregs[3] = 1; /* overflow (not read before another fault) */
240163fa5caSBlue Swirl     }
241163fa5caSBlue Swirl     env->mmuregs[3] |= (access_index << 5) | error_code | 2;
242163fa5caSBlue Swirl     env->mmuregs[4] = address; /* Fault address register */
243163fa5caSBlue Swirl 
244163fa5caSBlue Swirl     if ((env->mmuregs[0] & MMU_NF) || env->psret == 0)  {
245163fa5caSBlue Swirl         /* No fault mode: if a mapping is available, just override
246163fa5caSBlue Swirl            permissions. If no mapping is available, redirect accesses to
247163fa5caSBlue Swirl            neverland. Fake/overridden mappings will be flushed when
248163fa5caSBlue Swirl            switching to normal mode. */
249163fa5caSBlue Swirl         prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
2500c591eb0SAndreas Färber         tlb_set_page(cs, vaddr, paddr, prot, mmu_idx, TARGET_PAGE_SIZE);
251e84942f2SRichard Henderson         return true;
252163fa5caSBlue Swirl     } else {
253e84942f2SRichard Henderson         if (access_type == MMU_INST_FETCH) {
25427103424SAndreas Färber             cs->exception_index = TT_TFAULT;
255163fa5caSBlue Swirl         } else {
25627103424SAndreas Färber             cs->exception_index = TT_DFAULT;
257163fa5caSBlue Swirl         }
258e84942f2SRichard Henderson         cpu_loop_exit_restore(cs, retaddr);
259163fa5caSBlue Swirl     }
260163fa5caSBlue Swirl }
261163fa5caSBlue Swirl 
262c5f9864eSAndreas Färber target_ulong mmu_probe(CPUSPARCState *env, target_ulong address, int mmulev)
263163fa5caSBlue Swirl {
2645a59fbceSRichard Henderson     CPUState *cs = env_cpu(env);
265a8170e5eSAvi Kivity     hwaddr pde_ptr;
266163fa5caSBlue Swirl     uint32_t pde;
267d86a9ad3SPeter Maydell     MemTxResult result;
268d86a9ad3SPeter Maydell 
269d86a9ad3SPeter Maydell     /*
270d86a9ad3SPeter Maydell      * TODO: MMU probe operations are supposed to set the fault
271d86a9ad3SPeter Maydell      * status registers, but we don't do this.
272d86a9ad3SPeter Maydell      */
273163fa5caSBlue Swirl 
274163fa5caSBlue Swirl     /* Context base + context number */
275a8170e5eSAvi Kivity     pde_ptr = (hwaddr)(env->mmuregs[1] << 4) +
276163fa5caSBlue Swirl         (env->mmuregs[2] << 2);
277d86a9ad3SPeter Maydell     pde = address_space_ldl(cs->as, pde_ptr, MEMTXATTRS_UNSPECIFIED, &result);
278d86a9ad3SPeter Maydell     if (result != MEMTX_OK) {
279d86a9ad3SPeter Maydell         return 0;
280d86a9ad3SPeter Maydell     }
281163fa5caSBlue Swirl 
282163fa5caSBlue Swirl     switch (pde & PTE_ENTRYTYPE_MASK) {
283163fa5caSBlue Swirl     default:
284163fa5caSBlue Swirl     case 0: /* Invalid */
285163fa5caSBlue Swirl     case 2: /* PTE, maybe should not happen? */
286163fa5caSBlue Swirl     case 3: /* Reserved */
287163fa5caSBlue Swirl         return 0;
288163fa5caSBlue Swirl     case 1: /* L1 PDE */
289163fa5caSBlue Swirl         if (mmulev == 3) {
290163fa5caSBlue Swirl             return pde;
291163fa5caSBlue Swirl         }
292163fa5caSBlue Swirl         pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
293d86a9ad3SPeter Maydell         pde = address_space_ldl(cs->as, pde_ptr,
294d86a9ad3SPeter Maydell                                 MEMTXATTRS_UNSPECIFIED, &result);
295d86a9ad3SPeter Maydell         if (result != MEMTX_OK) {
296d86a9ad3SPeter Maydell             return 0;
297d86a9ad3SPeter Maydell         }
298163fa5caSBlue Swirl 
299163fa5caSBlue Swirl         switch (pde & PTE_ENTRYTYPE_MASK) {
300163fa5caSBlue Swirl         default:
301163fa5caSBlue Swirl         case 0: /* Invalid */
302163fa5caSBlue Swirl         case 3: /* Reserved */
303163fa5caSBlue Swirl             return 0;
304163fa5caSBlue Swirl         case 2: /* L1 PTE */
305163fa5caSBlue Swirl             return pde;
306163fa5caSBlue Swirl         case 1: /* L2 PDE */
307163fa5caSBlue Swirl             if (mmulev == 2) {
308163fa5caSBlue Swirl                 return pde;
309163fa5caSBlue Swirl             }
310163fa5caSBlue Swirl             pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
311d86a9ad3SPeter Maydell             pde = address_space_ldl(cs->as, pde_ptr,
312d86a9ad3SPeter Maydell                                     MEMTXATTRS_UNSPECIFIED, &result);
313d86a9ad3SPeter Maydell             if (result != MEMTX_OK) {
314d86a9ad3SPeter Maydell                 return 0;
315d86a9ad3SPeter Maydell             }
316163fa5caSBlue Swirl 
317163fa5caSBlue Swirl             switch (pde & PTE_ENTRYTYPE_MASK) {
318163fa5caSBlue Swirl             default:
319163fa5caSBlue Swirl             case 0: /* Invalid */
320163fa5caSBlue Swirl             case 3: /* Reserved */
321163fa5caSBlue Swirl                 return 0;
322163fa5caSBlue Swirl             case 2: /* L2 PTE */
323163fa5caSBlue Swirl                 return pde;
324163fa5caSBlue Swirl             case 1: /* L3 PDE */
325163fa5caSBlue Swirl                 if (mmulev == 1) {
326163fa5caSBlue Swirl                     return pde;
327163fa5caSBlue Swirl                 }
328163fa5caSBlue Swirl                 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
329d86a9ad3SPeter Maydell                 pde = address_space_ldl(cs->as, pde_ptr,
330d86a9ad3SPeter Maydell                                         MEMTXATTRS_UNSPECIFIED, &result);
331d86a9ad3SPeter Maydell                 if (result != MEMTX_OK) {
332d86a9ad3SPeter Maydell                     return 0;
333d86a9ad3SPeter Maydell                 }
334163fa5caSBlue Swirl 
335163fa5caSBlue Swirl                 switch (pde & PTE_ENTRYTYPE_MASK) {
336163fa5caSBlue Swirl                 default:
337163fa5caSBlue Swirl                 case 0: /* Invalid */
338163fa5caSBlue Swirl                 case 1: /* PDE, should not happen */
339163fa5caSBlue Swirl                 case 3: /* Reserved */
340163fa5caSBlue Swirl                     return 0;
341163fa5caSBlue Swirl                 case 2: /* L3 PTE */
342163fa5caSBlue Swirl                     return pde;
343163fa5caSBlue Swirl                 }
344163fa5caSBlue Swirl             }
345163fa5caSBlue Swirl         }
346163fa5caSBlue Swirl     }
347163fa5caSBlue Swirl     return 0;
348163fa5caSBlue Swirl }
349163fa5caSBlue Swirl 
350fad866daSMarkus Armbruster void dump_mmu(CPUSPARCState *env)
351163fa5caSBlue Swirl {
3525a59fbceSRichard Henderson     CPUState *cs = env_cpu(env);
353163fa5caSBlue Swirl     target_ulong va, va1, va2;
354163fa5caSBlue Swirl     unsigned int n, m, o;
3559dffeec2SPeter Maydell     hwaddr pa;
356163fa5caSBlue Swirl     uint32_t pde;
357163fa5caSBlue Swirl 
358fad866daSMarkus Armbruster     qemu_printf("Root ptr: " TARGET_FMT_plx ", ctx: %d\n",
359a8170e5eSAvi Kivity                 (hwaddr)env->mmuregs[1] << 4, env->mmuregs[2]);
360163fa5caSBlue Swirl     for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
361163fa5caSBlue Swirl         pde = mmu_probe(env, va, 2);
362163fa5caSBlue Swirl         if (pde) {
36300b941e5SAndreas Färber             pa = cpu_get_phys_page_debug(cs, va);
364fad866daSMarkus Armbruster             qemu_printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
365163fa5caSBlue Swirl                         " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
366163fa5caSBlue Swirl             for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
367163fa5caSBlue Swirl                 pde = mmu_probe(env, va1, 1);
368163fa5caSBlue Swirl                 if (pde) {
36900b941e5SAndreas Färber                     pa = cpu_get_phys_page_debug(cs, va1);
370fad866daSMarkus Armbruster                     qemu_printf(" VA: " TARGET_FMT_lx ", PA: "
371163fa5caSBlue Swirl                                 TARGET_FMT_plx " PDE: " TARGET_FMT_lx "\n",
372163fa5caSBlue Swirl                                 va1, pa, pde);
373163fa5caSBlue Swirl                     for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
374163fa5caSBlue Swirl                         pde = mmu_probe(env, va2, 0);
375163fa5caSBlue Swirl                         if (pde) {
37600b941e5SAndreas Färber                             pa = cpu_get_phys_page_debug(cs, va2);
377fad866daSMarkus Armbruster                             qemu_printf("  VA: " TARGET_FMT_lx ", PA: "
378163fa5caSBlue Swirl                                         TARGET_FMT_plx " PTE: "
379163fa5caSBlue Swirl                                         TARGET_FMT_lx "\n",
380163fa5caSBlue Swirl                                         va2, pa, pde);
381163fa5caSBlue Swirl                         }
382163fa5caSBlue Swirl                     }
383163fa5caSBlue Swirl                 }
384163fa5caSBlue Swirl             }
385163fa5caSBlue Swirl         }
386163fa5caSBlue Swirl     }
387163fa5caSBlue Swirl }
388163fa5caSBlue Swirl 
389163fa5caSBlue Swirl /* Gdb expects all registers windows to be flushed in ram. This function handles
390163fa5caSBlue Swirl  * reads (and only reads) in stack frames as if windows were flushed. We assume
391163fa5caSBlue Swirl  * that the sparc ABI is followed.
392163fa5caSBlue Swirl  */
393f3659eeeSAndreas Färber int sparc_cpu_memory_rw_debug(CPUState *cs, vaddr address,
394f3659eeeSAndreas Färber                               uint8_t *buf, int len, bool is_write)
395163fa5caSBlue Swirl {
396f3659eeeSAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
397f3659eeeSAndreas Färber     CPUSPARCState *env = &cpu->env;
398f3659eeeSAndreas Färber     target_ulong addr = address;
399163fa5caSBlue Swirl     int i;
400163fa5caSBlue Swirl     int len1;
401163fa5caSBlue Swirl     int cwp = env->cwp;
402163fa5caSBlue Swirl 
403163fa5caSBlue Swirl     if (!is_write) {
404163fa5caSBlue Swirl         for (i = 0; i < env->nwindows; i++) {
405163fa5caSBlue Swirl             int off;
406163fa5caSBlue Swirl             target_ulong fp = env->regbase[cwp * 16 + 22];
407163fa5caSBlue Swirl 
408163fa5caSBlue Swirl             /* Assume fp == 0 means end of frame.  */
409163fa5caSBlue Swirl             if (fp == 0) {
410163fa5caSBlue Swirl                 break;
411163fa5caSBlue Swirl             }
412163fa5caSBlue Swirl 
413163fa5caSBlue Swirl             cwp = cpu_cwp_inc(env, cwp + 1);
414163fa5caSBlue Swirl 
415163fa5caSBlue Swirl             /* Invalid window ? */
416163fa5caSBlue Swirl             if (env->wim & (1 << cwp)) {
417163fa5caSBlue Swirl                 break;
418163fa5caSBlue Swirl             }
419163fa5caSBlue Swirl 
420163fa5caSBlue Swirl             /* According to the ABI, the stack is growing downward.  */
421163fa5caSBlue Swirl             if (addr + len < fp) {
422163fa5caSBlue Swirl                 break;
423163fa5caSBlue Swirl             }
424163fa5caSBlue Swirl 
425163fa5caSBlue Swirl             /* Not in this frame.  */
426163fa5caSBlue Swirl             if (addr > fp + 64) {
427163fa5caSBlue Swirl                 continue;
428163fa5caSBlue Swirl             }
429163fa5caSBlue Swirl 
430163fa5caSBlue Swirl             /* Handle access before this window.  */
431163fa5caSBlue Swirl             if (addr < fp) {
432163fa5caSBlue Swirl                 len1 = fp - addr;
433f17ec444SAndreas Färber                 if (cpu_memory_rw_debug(cs, addr, buf, len1, is_write) != 0) {
434163fa5caSBlue Swirl                     return -1;
435163fa5caSBlue Swirl                 }
436163fa5caSBlue Swirl                 addr += len1;
437163fa5caSBlue Swirl                 len -= len1;
438163fa5caSBlue Swirl                 buf += len1;
439163fa5caSBlue Swirl             }
440163fa5caSBlue Swirl 
441163fa5caSBlue Swirl             /* Access byte per byte to registers. Not very efficient but speed
442163fa5caSBlue Swirl              * is not critical.
443163fa5caSBlue Swirl              */
444163fa5caSBlue Swirl             off = addr - fp;
445163fa5caSBlue Swirl             len1 = 64 - off;
446163fa5caSBlue Swirl 
447163fa5caSBlue Swirl             if (len1 > len) {
448163fa5caSBlue Swirl                 len1 = len;
449163fa5caSBlue Swirl             }
450163fa5caSBlue Swirl 
451163fa5caSBlue Swirl             for (; len1; len1--) {
452163fa5caSBlue Swirl                 int reg = cwp * 16 + 8 + (off >> 2);
453163fa5caSBlue Swirl                 union {
454163fa5caSBlue Swirl                     uint32_t v;
455163fa5caSBlue Swirl                     uint8_t c[4];
456163fa5caSBlue Swirl                 } u;
457163fa5caSBlue Swirl                 u.v = cpu_to_be32(env->regbase[reg]);
458163fa5caSBlue Swirl                 *buf++ = u.c[off & 3];
459163fa5caSBlue Swirl                 addr++;
460163fa5caSBlue Swirl                 len--;
461163fa5caSBlue Swirl                 off++;
462163fa5caSBlue Swirl             }
463163fa5caSBlue Swirl 
464163fa5caSBlue Swirl             if (len == 0) {
465163fa5caSBlue Swirl                 return 0;
466163fa5caSBlue Swirl             }
467163fa5caSBlue Swirl         }
468163fa5caSBlue Swirl     }
469f17ec444SAndreas Färber     return cpu_memory_rw_debug(cs, addr, buf, len, is_write);
470163fa5caSBlue Swirl }
471163fa5caSBlue Swirl 
472163fa5caSBlue Swirl #else /* !TARGET_SPARC64 */
473163fa5caSBlue Swirl 
474163fa5caSBlue Swirl /* 41 bit physical address space */
475a8170e5eSAvi Kivity static inline hwaddr ultrasparc_truncate_physical(uint64_t x)
476163fa5caSBlue Swirl {
477163fa5caSBlue Swirl     return x & 0x1ffffffffffULL;
478163fa5caSBlue Swirl }
479163fa5caSBlue Swirl 
480163fa5caSBlue Swirl /*
481163fa5caSBlue Swirl  * UltraSparc IIi I/DMMUs
482163fa5caSBlue Swirl  */
483163fa5caSBlue Swirl 
484163fa5caSBlue Swirl /* Returns true if TTE tag is valid and matches virtual address value
485163fa5caSBlue Swirl    in context requires virtual address mask value calculated from TTE
486163fa5caSBlue Swirl    entry size */
487163fa5caSBlue Swirl static inline int ultrasparc_tag_match(SparcTLBEntry *tlb,
488163fa5caSBlue Swirl                                        uint64_t address, uint64_t context,
489a8170e5eSAvi Kivity                                        hwaddr *physical)
490163fa5caSBlue Swirl {
491913b5f28SArtyom Tarasenko     uint64_t mask = -(8192ULL << 3 * TTE_PGSIZE(tlb->tte));
492163fa5caSBlue Swirl 
493163fa5caSBlue Swirl     /* valid, context match, virtual address match? */
494163fa5caSBlue Swirl     if (TTE_IS_VALID(tlb->tte) &&
495163fa5caSBlue Swirl         (TTE_IS_GLOBAL(tlb->tte) || tlb_compare_context(tlb, context))
496163fa5caSBlue Swirl         && compare_masked(address, tlb->tag, mask)) {
497163fa5caSBlue Swirl         /* decode physical address */
498163fa5caSBlue Swirl         *physical = ((tlb->tte & mask) | (address & ~mask)) & 0x1ffffffe000ULL;
499163fa5caSBlue Swirl         return 1;
500163fa5caSBlue Swirl     }
501163fa5caSBlue Swirl 
502163fa5caSBlue Swirl     return 0;
503163fa5caSBlue Swirl }
504163fa5caSBlue Swirl 
505c0e0c6feSRichard Henderson static uint64_t build_sfsr(CPUSPARCState *env, int mmu_idx, int rw)
506c0e0c6feSRichard Henderson {
507c0e0c6feSRichard Henderson     uint64_t sfsr = SFSR_VALID_BIT;
508c0e0c6feSRichard Henderson 
509c0e0c6feSRichard Henderson     switch (mmu_idx) {
510c0e0c6feSRichard Henderson     case MMU_PHYS_IDX:
511c0e0c6feSRichard Henderson         sfsr |= SFSR_CT_NOTRANS;
512c0e0c6feSRichard Henderson         break;
513c0e0c6feSRichard Henderson     case MMU_USER_IDX:
514c0e0c6feSRichard Henderson     case MMU_KERNEL_IDX:
515c0e0c6feSRichard Henderson         sfsr |= SFSR_CT_PRIMARY;
516c0e0c6feSRichard Henderson         break;
517c0e0c6feSRichard Henderson     case MMU_USER_SECONDARY_IDX:
518c0e0c6feSRichard Henderson     case MMU_KERNEL_SECONDARY_IDX:
519c0e0c6feSRichard Henderson         sfsr |= SFSR_CT_SECONDARY;
520c0e0c6feSRichard Henderson         break;
521c0e0c6feSRichard Henderson     case MMU_NUCLEUS_IDX:
522c0e0c6feSRichard Henderson         sfsr |= SFSR_CT_NUCLEUS;
523c0e0c6feSRichard Henderson         break;
524c0e0c6feSRichard Henderson     default:
525c0e0c6feSRichard Henderson         g_assert_not_reached();
526c0e0c6feSRichard Henderson     }
527c0e0c6feSRichard Henderson 
528c0e0c6feSRichard Henderson     if (rw == 1) {
529c0e0c6feSRichard Henderson         sfsr |= SFSR_WRITE_BIT;
530c0e0c6feSRichard Henderson     } else if (rw == 4) {
531c0e0c6feSRichard Henderson         sfsr |= SFSR_NF_BIT;
532c0e0c6feSRichard Henderson     }
533c0e0c6feSRichard Henderson 
534c0e0c6feSRichard Henderson     if (env->pstate & PS_PRIV) {
535c0e0c6feSRichard Henderson         sfsr |= SFSR_PR_BIT;
536c0e0c6feSRichard Henderson     }
537c0e0c6feSRichard Henderson 
538c0e0c6feSRichard Henderson     if (env->dmmu.sfsr & SFSR_VALID_BIT) { /* Fault status register */
539c0e0c6feSRichard Henderson         sfsr |= SFSR_OW_BIT; /* overflow (not read before another fault) */
540c0e0c6feSRichard Henderson     }
541c0e0c6feSRichard Henderson 
542c0e0c6feSRichard Henderson     /* FIXME: ASI field in SFSR must be set */
543c0e0c6feSRichard Henderson 
544c0e0c6feSRichard Henderson     return sfsr;
545c0e0c6feSRichard Henderson }
546c0e0c6feSRichard Henderson 
5479bed46e6STony Nguyen static int get_physical_address_data(CPUSPARCState *env, hwaddr *physical,
5489bed46e6STony Nguyen                                      int *prot, MemTxAttrs *attrs,
549163fa5caSBlue Swirl                                      target_ulong address, int rw, int mmu_idx)
550163fa5caSBlue Swirl {
5515a59fbceSRichard Henderson     CPUState *cs = env_cpu(env);
552163fa5caSBlue Swirl     unsigned int i;
553c0e0c6feSRichard Henderson     uint64_t sfsr;
554163fa5caSBlue Swirl     uint64_t context;
555af7a06baSRichard Henderson     bool is_user = false;
556163fa5caSBlue Swirl 
557c0e0c6feSRichard Henderson     sfsr = build_sfsr(env, mmu_idx, rw);
558c0e0c6feSRichard Henderson 
559163fa5caSBlue Swirl     switch (mmu_idx) {
560af7a06baSRichard Henderson     case MMU_PHYS_IDX:
561af7a06baSRichard Henderson         g_assert_not_reached();
562163fa5caSBlue Swirl     case MMU_USER_IDX:
563af7a06baSRichard Henderson         is_user = true;
564af7a06baSRichard Henderson         /* fallthru */
565163fa5caSBlue Swirl     case MMU_KERNEL_IDX:
566163fa5caSBlue Swirl         context = env->dmmu.mmu_primary_context & 0x1fff;
567163fa5caSBlue Swirl         break;
568163fa5caSBlue Swirl     case MMU_USER_SECONDARY_IDX:
569af7a06baSRichard Henderson         is_user = true;
570af7a06baSRichard Henderson         /* fallthru */
571163fa5caSBlue Swirl     case MMU_KERNEL_SECONDARY_IDX:
572163fa5caSBlue Swirl         context = env->dmmu.mmu_secondary_context & 0x1fff;
573163fa5caSBlue Swirl         break;
574163fa5caSBlue Swirl     default:
575163fa5caSBlue Swirl         context = 0;
576163fa5caSBlue Swirl         break;
577163fa5caSBlue Swirl     }
578163fa5caSBlue Swirl 
579163fa5caSBlue Swirl     for (i = 0; i < 64; i++) {
580163fa5caSBlue Swirl         /* ctx match, vaddr match, valid? */
581163fa5caSBlue Swirl         if (ultrasparc_tag_match(&env->dtlb[i], address, context, physical)) {
582163fa5caSBlue Swirl             int do_fault = 0;
583163fa5caSBlue Swirl 
584ccdb4c55STony Nguyen             if (TTE_IS_IE(env->dtlb[i].tte)) {
585ccdb4c55STony Nguyen                 attrs->byte_swap = true;
586ccdb4c55STony Nguyen             }
587ccdb4c55STony Nguyen 
588163fa5caSBlue Swirl             /* access ok? */
589163fa5caSBlue Swirl             /* multiple bits in SFSR.FT may be set on TT_DFAULT */
590163fa5caSBlue Swirl             if (TTE_IS_PRIV(env->dtlb[i].tte) && is_user) {
591163fa5caSBlue Swirl                 do_fault = 1;
592163fa5caSBlue Swirl                 sfsr |= SFSR_FT_PRIV_BIT; /* privilege violation */
593ec0ceb17SBlue Swirl                 trace_mmu_helper_dfault(address, context, mmu_idx, env->tl);
594163fa5caSBlue Swirl             }
595163fa5caSBlue Swirl             if (rw == 4) {
596163fa5caSBlue Swirl                 if (TTE_IS_SIDEEFFECT(env->dtlb[i].tte)) {
597163fa5caSBlue Swirl                     do_fault = 1;
598163fa5caSBlue Swirl                     sfsr |= SFSR_FT_NF_E_BIT;
599163fa5caSBlue Swirl                 }
600163fa5caSBlue Swirl             } else {
601163fa5caSBlue Swirl                 if (TTE_IS_NFO(env->dtlb[i].tte)) {
602163fa5caSBlue Swirl                     do_fault = 1;
603163fa5caSBlue Swirl                     sfsr |= SFSR_FT_NFO_BIT;
604163fa5caSBlue Swirl                 }
605163fa5caSBlue Swirl             }
606163fa5caSBlue Swirl 
607163fa5caSBlue Swirl             if (do_fault) {
608163fa5caSBlue Swirl                 /* faults above are reported with TT_DFAULT. */
60927103424SAndreas Färber                 cs->exception_index = TT_DFAULT;
610163fa5caSBlue Swirl             } else if (!TTE_IS_W_OK(env->dtlb[i].tte) && (rw == 1)) {
611163fa5caSBlue Swirl                 do_fault = 1;
61227103424SAndreas Färber                 cs->exception_index = TT_DPROT;
613163fa5caSBlue Swirl 
614ec0ceb17SBlue Swirl                 trace_mmu_helper_dprot(address, context, mmu_idx, env->tl);
615163fa5caSBlue Swirl             }
616163fa5caSBlue Swirl 
617163fa5caSBlue Swirl             if (!do_fault) {
618163fa5caSBlue Swirl                 *prot = PAGE_READ;
619163fa5caSBlue Swirl                 if (TTE_IS_W_OK(env->dtlb[i].tte)) {
620163fa5caSBlue Swirl                     *prot |= PAGE_WRITE;
621163fa5caSBlue Swirl                 }
622163fa5caSBlue Swirl 
623163fa5caSBlue Swirl                 TTE_SET_USED(env->dtlb[i].tte);
624163fa5caSBlue Swirl 
625163fa5caSBlue Swirl                 return 0;
626163fa5caSBlue Swirl             }
627163fa5caSBlue Swirl 
628c0e0c6feSRichard Henderson             env->dmmu.sfsr = sfsr;
629163fa5caSBlue Swirl             env->dmmu.sfar = address; /* Fault address register */
630163fa5caSBlue Swirl             env->dmmu.tag_access = (address & ~0x1fffULL) | context;
631163fa5caSBlue Swirl             return 1;
632163fa5caSBlue Swirl         }
633163fa5caSBlue Swirl     }
634163fa5caSBlue Swirl 
635ec0ceb17SBlue Swirl     trace_mmu_helper_dmiss(address, context);
636163fa5caSBlue Swirl 
637163fa5caSBlue Swirl     /*
638163fa5caSBlue Swirl      * On MMU misses:
639163fa5caSBlue Swirl      * - UltraSPARC IIi: SFSR and SFAR unmodified
640163fa5caSBlue Swirl      * - JPS1: SFAR updated and some fields of SFSR updated
641163fa5caSBlue Swirl      */
642163fa5caSBlue Swirl     env->dmmu.tag_access = (address & ~0x1fffULL) | context;
64327103424SAndreas Färber     cs->exception_index = TT_DMISS;
644163fa5caSBlue Swirl     return 1;
645163fa5caSBlue Swirl }
646163fa5caSBlue Swirl 
6479bed46e6STony Nguyen static int get_physical_address_code(CPUSPARCState *env, hwaddr *physical,
6489bed46e6STony Nguyen                                      int *prot, MemTxAttrs *attrs,
649163fa5caSBlue Swirl                                      target_ulong address, int mmu_idx)
650163fa5caSBlue Swirl {
6515a59fbceSRichard Henderson     CPUState *cs = env_cpu(env);
652163fa5caSBlue Swirl     unsigned int i;
653163fa5caSBlue Swirl     uint64_t context;
654af7a06baSRichard Henderson     bool is_user = false;
655163fa5caSBlue Swirl 
656af7a06baSRichard Henderson     switch (mmu_idx) {
657af7a06baSRichard Henderson     case MMU_PHYS_IDX:
658af7a06baSRichard Henderson     case MMU_USER_SECONDARY_IDX:
659af7a06baSRichard Henderson     case MMU_KERNEL_SECONDARY_IDX:
660af7a06baSRichard Henderson         g_assert_not_reached();
661af7a06baSRichard Henderson     case MMU_USER_IDX:
662af7a06baSRichard Henderson         is_user = true;
663af7a06baSRichard Henderson         /* fallthru */
664af7a06baSRichard Henderson     case MMU_KERNEL_IDX:
665af7a06baSRichard Henderson         context = env->dmmu.mmu_primary_context & 0x1fff;
666af7a06baSRichard Henderson         break;
667af7a06baSRichard Henderson     default:
668af7a06baSRichard Henderson         context = 0;
669af7a06baSRichard Henderson         break;
670163fa5caSBlue Swirl     }
671163fa5caSBlue Swirl 
672163fa5caSBlue Swirl     if (env->tl == 0) {
673163fa5caSBlue Swirl         /* PRIMARY context */
674163fa5caSBlue Swirl         context = env->dmmu.mmu_primary_context & 0x1fff;
675163fa5caSBlue Swirl     } else {
676163fa5caSBlue Swirl         /* NUCLEUS context */
677163fa5caSBlue Swirl         context = 0;
678163fa5caSBlue Swirl     }
679163fa5caSBlue Swirl 
680163fa5caSBlue Swirl     for (i = 0; i < 64; i++) {
681163fa5caSBlue Swirl         /* ctx match, vaddr match, valid? */
682163fa5caSBlue Swirl         if (ultrasparc_tag_match(&env->itlb[i],
683163fa5caSBlue Swirl                                  address, context, physical)) {
684163fa5caSBlue Swirl             /* access ok? */
685163fa5caSBlue Swirl             if (TTE_IS_PRIV(env->itlb[i].tte) && is_user) {
686163fa5caSBlue Swirl                 /* Fault status register */
687163fa5caSBlue Swirl                 if (env->immu.sfsr & SFSR_VALID_BIT) {
688163fa5caSBlue Swirl                     env->immu.sfsr = SFSR_OW_BIT; /* overflow (not read before
689163fa5caSBlue Swirl                                                      another fault) */
690163fa5caSBlue Swirl                 } else {
691163fa5caSBlue Swirl                     env->immu.sfsr = 0;
692163fa5caSBlue Swirl                 }
693163fa5caSBlue Swirl                 if (env->pstate & PS_PRIV) {
694163fa5caSBlue Swirl                     env->immu.sfsr |= SFSR_PR_BIT;
695163fa5caSBlue Swirl                 }
696163fa5caSBlue Swirl                 if (env->tl > 0) {
697163fa5caSBlue Swirl                     env->immu.sfsr |= SFSR_CT_NUCLEUS;
698163fa5caSBlue Swirl                 }
699163fa5caSBlue Swirl 
700163fa5caSBlue Swirl                 /* FIXME: ASI field in SFSR must be set */
701163fa5caSBlue Swirl                 env->immu.sfsr |= SFSR_FT_PRIV_BIT | SFSR_VALID_BIT;
70227103424SAndreas Färber                 cs->exception_index = TT_TFAULT;
703163fa5caSBlue Swirl 
704163fa5caSBlue Swirl                 env->immu.tag_access = (address & ~0x1fffULL) | context;
705163fa5caSBlue Swirl 
706ec0ceb17SBlue Swirl                 trace_mmu_helper_tfault(address, context);
707163fa5caSBlue Swirl 
708163fa5caSBlue Swirl                 return 1;
709163fa5caSBlue Swirl             }
710163fa5caSBlue Swirl             *prot = PAGE_EXEC;
711163fa5caSBlue Swirl             TTE_SET_USED(env->itlb[i].tte);
712163fa5caSBlue Swirl             return 0;
713163fa5caSBlue Swirl         }
714163fa5caSBlue Swirl     }
715163fa5caSBlue Swirl 
716ec0ceb17SBlue Swirl     trace_mmu_helper_tmiss(address, context);
717163fa5caSBlue Swirl 
718163fa5caSBlue Swirl     /* Context is stored in DMMU (dmmuregs[1]) also for IMMU */
719163fa5caSBlue Swirl     env->immu.tag_access = (address & ~0x1fffULL) | context;
72027103424SAndreas Färber     cs->exception_index = TT_TMISS;
721163fa5caSBlue Swirl     return 1;
722163fa5caSBlue Swirl }
723163fa5caSBlue Swirl 
724a8170e5eSAvi Kivity static int get_physical_address(CPUSPARCState *env, hwaddr *physical,
7259bed46e6STony Nguyen                                 int *prot, int *access_index, MemTxAttrs *attrs,
726163fa5caSBlue Swirl                                 target_ulong address, int rw, int mmu_idx,
727163fa5caSBlue Swirl                                 target_ulong *page_size)
728163fa5caSBlue Swirl {
729163fa5caSBlue Swirl     /* ??? We treat everything as a small page, then explicitly flush
730163fa5caSBlue Swirl        everything when an entry is evicted.  */
731163fa5caSBlue Swirl     *page_size = TARGET_PAGE_SIZE;
732163fa5caSBlue Swirl 
733163fa5caSBlue Swirl     /* safety net to catch wrong softmmu index use from dynamic code */
734163fa5caSBlue Swirl     if (env->tl > 0 && mmu_idx != MMU_NUCLEUS_IDX) {
735ec0ceb17SBlue Swirl         if (rw == 2) {
736ec0ceb17SBlue Swirl             trace_mmu_helper_get_phys_addr_code(env->tl, mmu_idx,
737ec0ceb17SBlue Swirl                                                 env->dmmu.mmu_primary_context,
738ec0ceb17SBlue Swirl                                                 env->dmmu.mmu_secondary_context,
739ec0ceb17SBlue Swirl                                                 address);
740ec0ceb17SBlue Swirl         } else {
741ec0ceb17SBlue Swirl             trace_mmu_helper_get_phys_addr_data(env->tl, mmu_idx,
742163fa5caSBlue Swirl                                                 env->dmmu.mmu_primary_context,
743163fa5caSBlue Swirl                                                 env->dmmu.mmu_secondary_context,
744163fa5caSBlue Swirl                                                 address);
745163fa5caSBlue Swirl         }
746ec0ceb17SBlue Swirl     }
747163fa5caSBlue Swirl 
748af7a06baSRichard Henderson     if (mmu_idx == MMU_PHYS_IDX) {
749af7a06baSRichard Henderson         *physical = ultrasparc_truncate_physical(address);
750af7a06baSRichard Henderson         *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
751af7a06baSRichard Henderson         return 0;
752af7a06baSRichard Henderson     }
753af7a06baSRichard Henderson 
754163fa5caSBlue Swirl     if (rw == 2) {
7559bed46e6STony Nguyen         return get_physical_address_code(env, physical, prot, attrs, address,
756163fa5caSBlue Swirl                                          mmu_idx);
757163fa5caSBlue Swirl     } else {
7589bed46e6STony Nguyen         return get_physical_address_data(env, physical, prot, attrs, address,
7599bed46e6STony Nguyen                                          rw, mmu_idx);
760163fa5caSBlue Swirl     }
761163fa5caSBlue Swirl }
762163fa5caSBlue Swirl 
763163fa5caSBlue Swirl /* Perform address translation */
764e84942f2SRichard Henderson bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
765e84942f2SRichard Henderson                         MMUAccessType access_type, int mmu_idx,
766e84942f2SRichard Henderson                         bool probe, uintptr_t retaddr)
767163fa5caSBlue Swirl {
7687510454eSAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
7697510454eSAndreas Färber     CPUSPARCState *env = &cpu->env;
7701658dd32SBlue Swirl     target_ulong vaddr;
771a8170e5eSAvi Kivity     hwaddr paddr;
772163fa5caSBlue Swirl     target_ulong page_size;
7739bed46e6STony Nguyen     MemTxAttrs attrs = {};
774163fa5caSBlue Swirl     int error_code = 0, prot, access_index;
775163fa5caSBlue Swirl 
7761658dd32SBlue Swirl     address &= TARGET_PAGE_MASK;
7779bed46e6STony Nguyen     error_code = get_physical_address(env, &paddr, &prot, &access_index, &attrs,
778e84942f2SRichard Henderson                                       address, access_type,
779e84942f2SRichard Henderson                                       mmu_idx, &page_size);
780e84942f2SRichard Henderson     if (likely(error_code == 0)) {
7811658dd32SBlue Swirl         vaddr = address;
782163fa5caSBlue Swirl 
783ec0ceb17SBlue Swirl         trace_mmu_helper_mmu_fault(address, paddr, mmu_idx, env->tl,
784163fa5caSBlue Swirl                                    env->dmmu.mmu_primary_context,
785163fa5caSBlue Swirl                                    env->dmmu.mmu_secondary_context);
786163fa5caSBlue Swirl 
7879bed46e6STony Nguyen         tlb_set_page_with_attrs(cs, vaddr, paddr, attrs, prot, mmu_idx,
7889bed46e6STony Nguyen                                 page_size);
789e84942f2SRichard Henderson         return true;
790163fa5caSBlue Swirl     }
791e84942f2SRichard Henderson     if (probe) {
792e84942f2SRichard Henderson         return false;
793e84942f2SRichard Henderson     }
794e84942f2SRichard Henderson     cpu_loop_exit_restore(cs, retaddr);
795163fa5caSBlue Swirl }
796163fa5caSBlue Swirl 
797fad866daSMarkus Armbruster void dump_mmu(CPUSPARCState *env)
798163fa5caSBlue Swirl {
799163fa5caSBlue Swirl     unsigned int i;
800163fa5caSBlue Swirl     const char *mask;
801163fa5caSBlue Swirl 
802fad866daSMarkus Armbruster     qemu_printf("MMU contexts: Primary: %" PRId64 ", Secondary: %"
803163fa5caSBlue Swirl                 PRId64 "\n",
804163fa5caSBlue Swirl                 env->dmmu.mmu_primary_context,
805163fa5caSBlue Swirl                 env->dmmu.mmu_secondary_context);
806fad866daSMarkus Armbruster     qemu_printf("DMMU Tag Access: %" PRIx64 ", TSB Tag Target: %" PRIx64
807d00a2334SArtyom Tarasenko                 "\n", env->dmmu.tag_access, env->dmmu.tsb_tag_target);
808163fa5caSBlue Swirl     if ((env->lsu & DMMU_E) == 0) {
809fad866daSMarkus Armbruster         qemu_printf("DMMU disabled\n");
810163fa5caSBlue Swirl     } else {
811fad866daSMarkus Armbruster         qemu_printf("DMMU dump\n");
812163fa5caSBlue Swirl         for (i = 0; i < 64; i++) {
813163fa5caSBlue Swirl             switch (TTE_PGSIZE(env->dtlb[i].tte)) {
814163fa5caSBlue Swirl             default:
815163fa5caSBlue Swirl             case 0x0:
816163fa5caSBlue Swirl                 mask = "  8k";
817163fa5caSBlue Swirl                 break;
818163fa5caSBlue Swirl             case 0x1:
819163fa5caSBlue Swirl                 mask = " 64k";
820163fa5caSBlue Swirl                 break;
821163fa5caSBlue Swirl             case 0x2:
822163fa5caSBlue Swirl                 mask = "512k";
823163fa5caSBlue Swirl                 break;
824163fa5caSBlue Swirl             case 0x3:
825163fa5caSBlue Swirl                 mask = "  4M";
826163fa5caSBlue Swirl                 break;
827163fa5caSBlue Swirl             }
828163fa5caSBlue Swirl             if (TTE_IS_VALID(env->dtlb[i].tte)) {
829fad866daSMarkus Armbruster                 qemu_printf("[%02u] VA: %" PRIx64 ", PA: %llx"
830ccdb4c55STony Nguyen                             ", %s, %s, %s, %s, ie %s, ctx %" PRId64 " %s\n",
831163fa5caSBlue Swirl                             i,
832163fa5caSBlue Swirl                             env->dtlb[i].tag & (uint64_t)~0x1fffULL,
833163fa5caSBlue Swirl                             TTE_PA(env->dtlb[i].tte),
834163fa5caSBlue Swirl                             mask,
835163fa5caSBlue Swirl                             TTE_IS_PRIV(env->dtlb[i].tte) ? "priv" : "user",
836163fa5caSBlue Swirl                             TTE_IS_W_OK(env->dtlb[i].tte) ? "RW" : "RO",
837163fa5caSBlue Swirl                             TTE_IS_LOCKED(env->dtlb[i].tte) ?
838163fa5caSBlue Swirl                             "locked" : "unlocked",
839ccdb4c55STony Nguyen                             TTE_IS_IE(env->dtlb[i].tte) ?
840ccdb4c55STony Nguyen                             "yes" : "no",
841163fa5caSBlue Swirl                             env->dtlb[i].tag & (uint64_t)0x1fffULL,
842163fa5caSBlue Swirl                             TTE_IS_GLOBAL(env->dtlb[i].tte) ?
843163fa5caSBlue Swirl                             "global" : "local");
844163fa5caSBlue Swirl             }
845163fa5caSBlue Swirl         }
846163fa5caSBlue Swirl     }
847163fa5caSBlue Swirl     if ((env->lsu & IMMU_E) == 0) {
848fad866daSMarkus Armbruster         qemu_printf("IMMU disabled\n");
849163fa5caSBlue Swirl     } else {
850fad866daSMarkus Armbruster         qemu_printf("IMMU dump\n");
851163fa5caSBlue Swirl         for (i = 0; i < 64; i++) {
852163fa5caSBlue Swirl             switch (TTE_PGSIZE(env->itlb[i].tte)) {
853163fa5caSBlue Swirl             default:
854163fa5caSBlue Swirl             case 0x0:
855163fa5caSBlue Swirl                 mask = "  8k";
856163fa5caSBlue Swirl                 break;
857163fa5caSBlue Swirl             case 0x1:
858163fa5caSBlue Swirl                 mask = " 64k";
859163fa5caSBlue Swirl                 break;
860163fa5caSBlue Swirl             case 0x2:
861163fa5caSBlue Swirl                 mask = "512k";
862163fa5caSBlue Swirl                 break;
863163fa5caSBlue Swirl             case 0x3:
864163fa5caSBlue Swirl                 mask = "  4M";
865163fa5caSBlue Swirl                 break;
866163fa5caSBlue Swirl             }
867163fa5caSBlue Swirl             if (TTE_IS_VALID(env->itlb[i].tte)) {
868fad866daSMarkus Armbruster                 qemu_printf("[%02u] VA: %" PRIx64 ", PA: %llx"
869163fa5caSBlue Swirl                             ", %s, %s, %s, ctx %" PRId64 " %s\n",
870163fa5caSBlue Swirl                             i,
871163fa5caSBlue Swirl                             env->itlb[i].tag & (uint64_t)~0x1fffULL,
872163fa5caSBlue Swirl                             TTE_PA(env->itlb[i].tte),
873163fa5caSBlue Swirl                             mask,
874163fa5caSBlue Swirl                             TTE_IS_PRIV(env->itlb[i].tte) ? "priv" : "user",
875163fa5caSBlue Swirl                             TTE_IS_LOCKED(env->itlb[i].tte) ?
876163fa5caSBlue Swirl                             "locked" : "unlocked",
877163fa5caSBlue Swirl                             env->itlb[i].tag & (uint64_t)0x1fffULL,
878163fa5caSBlue Swirl                             TTE_IS_GLOBAL(env->itlb[i].tte) ?
879163fa5caSBlue Swirl                             "global" : "local");
880163fa5caSBlue Swirl             }
881163fa5caSBlue Swirl         }
882163fa5caSBlue Swirl     }
883163fa5caSBlue Swirl }
884163fa5caSBlue Swirl 
885163fa5caSBlue Swirl #endif /* TARGET_SPARC64 */
886163fa5caSBlue Swirl 
887a8170e5eSAvi Kivity static int cpu_sparc_get_phys_page(CPUSPARCState *env, hwaddr *phys,
888163fa5caSBlue Swirl                                    target_ulong addr, int rw, int mmu_idx)
889163fa5caSBlue Swirl {
890163fa5caSBlue Swirl     target_ulong page_size;
891163fa5caSBlue Swirl     int prot, access_index;
8929bed46e6STony Nguyen     MemTxAttrs attrs = {};
893163fa5caSBlue Swirl 
8949bed46e6STony Nguyen     return get_physical_address(env, phys, &prot, &access_index, &attrs, addr,
8959bed46e6STony Nguyen                                 rw, mmu_idx, &page_size);
896163fa5caSBlue Swirl }
897163fa5caSBlue Swirl 
898163fa5caSBlue Swirl #if defined(TARGET_SPARC64)
899a8170e5eSAvi Kivity hwaddr cpu_get_phys_page_nofault(CPUSPARCState *env, target_ulong addr,
900163fa5caSBlue Swirl                                            int mmu_idx)
901163fa5caSBlue Swirl {
902a8170e5eSAvi Kivity     hwaddr phys_addr;
903163fa5caSBlue Swirl 
904163fa5caSBlue Swirl     if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 4, mmu_idx) != 0) {
905163fa5caSBlue Swirl         return -1;
906163fa5caSBlue Swirl     }
907163fa5caSBlue Swirl     return phys_addr;
908163fa5caSBlue Swirl }
909163fa5caSBlue Swirl #endif
910163fa5caSBlue Swirl 
91100b941e5SAndreas Färber hwaddr sparc_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
912163fa5caSBlue Swirl {
91300b941e5SAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
91400b941e5SAndreas Färber     CPUSPARCState *env = &cpu->env;
915a8170e5eSAvi Kivity     hwaddr phys_addr;
91697ed5ccdSBenjamin Herrenschmidt     int mmu_idx = cpu_mmu_index(env, false);
917163fa5caSBlue Swirl 
918163fa5caSBlue Swirl     if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 2, mmu_idx) != 0) {
919163fa5caSBlue Swirl         if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 0, mmu_idx) != 0) {
920163fa5caSBlue Swirl             return -1;
921163fa5caSBlue Swirl         }
922163fa5caSBlue Swirl     }
923163fa5caSBlue Swirl     return phys_addr;
924163fa5caSBlue Swirl }
925*aebe5153SRichard Henderson 
926*aebe5153SRichard Henderson #ifndef CONFIG_USER_ONLY
927*aebe5153SRichard Henderson void QEMU_NORETURN sparc_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
928*aebe5153SRichard Henderson                                                  MMUAccessType access_type,
929*aebe5153SRichard Henderson                                                  int mmu_idx,
930*aebe5153SRichard Henderson                                                  uintptr_t retaddr)
931*aebe5153SRichard Henderson {
932*aebe5153SRichard Henderson     SPARCCPU *cpu = SPARC_CPU(cs);
933*aebe5153SRichard Henderson     CPUSPARCState *env = &cpu->env;
934*aebe5153SRichard Henderson 
935*aebe5153SRichard Henderson #ifdef TARGET_SPARC64
936*aebe5153SRichard Henderson     env->dmmu.sfsr = build_sfsr(env, mmu_idx, access_type);
937*aebe5153SRichard Henderson     env->dmmu.sfar = addr;
938*aebe5153SRichard Henderson #else
939*aebe5153SRichard Henderson     env->mmuregs[4] = addr;
940*aebe5153SRichard Henderson #endif
941*aebe5153SRichard Henderson 
942*aebe5153SRichard Henderson     cpu_raise_exception_ra(env, TT_UNALIGNED, retaddr);
943*aebe5153SRichard Henderson }
944*aebe5153SRichard Henderson #endif /* !CONFIG_USER_ONLY */
945