]> git.proxmox.com Git - qemu.git/blame - target-sparc/mmu_helper.c
exec: Change cpu_memory_rw_debug() argument to CPUState
[qemu.git] / target-sparc / mmu_helper.c
CommitLineData
163fa5ca
BS
1/*
2 * Sparc MMU helpers
3 *
4 * Copyright (c) 2003-2005 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "cpu.h"
ec0ceb17 21#include "trace.h"
022c62cb 22#include "exec/address-spaces.h"
163fa5ca
BS
23
24/* Sparc MMU emulation */
25
26#if defined(CONFIG_USER_ONLY)
27
c5f9864e 28int cpu_sparc_handle_mmu_fault(CPUSPARCState *env1, target_ulong address, int rw,
163fa5ca
BS
29 int mmu_idx)
30{
31 if (rw & 2) {
32 env1->exception_index = TT_TFAULT;
33 } else {
34 env1->exception_index = TT_DFAULT;
35 }
36 return 1;
37}
38
39#else
40
41#ifndef TARGET_SPARC64
42/*
43 * Sparc V8 Reference MMU (SRMMU)
44 */
45static const int access_table[8][8] = {
46 { 0, 0, 0, 0, 8, 0, 12, 12 },
47 { 0, 0, 0, 0, 8, 0, 0, 0 },
48 { 8, 8, 0, 0, 0, 8, 12, 12 },
49 { 8, 8, 0, 0, 0, 8, 0, 0 },
50 { 8, 0, 8, 0, 8, 8, 12, 12 },
51 { 8, 0, 8, 0, 8, 0, 8, 0 },
52 { 8, 8, 8, 0, 8, 8, 12, 12 },
53 { 8, 8, 8, 0, 8, 8, 8, 0 }
54};
55
56static const int perm_table[2][8] = {
57 {
58 PAGE_READ,
59 PAGE_READ | PAGE_WRITE,
60 PAGE_READ | PAGE_EXEC,
61 PAGE_READ | PAGE_WRITE | PAGE_EXEC,
62 PAGE_EXEC,
63 PAGE_READ | PAGE_WRITE,
64 PAGE_READ | PAGE_EXEC,
65 PAGE_READ | PAGE_WRITE | PAGE_EXEC
66 },
67 {
68 PAGE_READ,
69 PAGE_READ | PAGE_WRITE,
70 PAGE_READ | PAGE_EXEC,
71 PAGE_READ | PAGE_WRITE | PAGE_EXEC,
72 PAGE_EXEC,
73 PAGE_READ,
74 0,
75 0,
76 }
77};
78
a8170e5e 79static int get_physical_address(CPUSPARCState *env, hwaddr *physical,
163fa5ca
BS
80 int *prot, int *access_index,
81 target_ulong address, int rw, int mmu_idx,
82 target_ulong *page_size)
83{
84 int access_perms = 0;
a8170e5e 85 hwaddr pde_ptr;
163fa5ca
BS
86 uint32_t pde;
87 int error_code = 0, is_dirty, is_user;
88 unsigned long page_offset;
89
90 is_user = mmu_idx == MMU_USER_IDX;
91
92 if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
93 *page_size = TARGET_PAGE_SIZE;
94 /* Boot mode: instruction fetches are taken from PROM */
95 if (rw == 2 && (env->mmuregs[0] & env->def->mmu_bm)) {
96 *physical = env->prom_addr | (address & 0x7ffffULL);
97 *prot = PAGE_READ | PAGE_EXEC;
98 return 0;
99 }
100 *physical = address;
101 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
102 return 0;
103 }
104
105 *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user ? 0 : 1);
106 *physical = 0xffffffffffff0000ULL;
107
108 /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
109 /* Context base + context number */
110 pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
111 pde = ldl_phys(pde_ptr);
112
113 /* Ctx pde */
114 switch (pde & PTE_ENTRYTYPE_MASK) {
115 default:
116 case 0: /* Invalid */
117 return 1 << 2;
118 case 2: /* L0 PTE, maybe should not happen? */
119 case 3: /* Reserved */
120 return 4 << 2;
121 case 1: /* L0 PDE */
122 pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
123 pde = ldl_phys(pde_ptr);
124
125 switch (pde & PTE_ENTRYTYPE_MASK) {
126 default:
127 case 0: /* Invalid */
128 return (1 << 8) | (1 << 2);
129 case 3: /* Reserved */
130 return (1 << 8) | (4 << 2);
131 case 1: /* L1 PDE */
132 pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
133 pde = ldl_phys(pde_ptr);
134
135 switch (pde & PTE_ENTRYTYPE_MASK) {
136 default:
137 case 0: /* Invalid */
138 return (2 << 8) | (1 << 2);
139 case 3: /* Reserved */
140 return (2 << 8) | (4 << 2);
141 case 1: /* L2 PDE */
142 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
143 pde = ldl_phys(pde_ptr);
144
145 switch (pde & PTE_ENTRYTYPE_MASK) {
146 default:
147 case 0: /* Invalid */
148 return (3 << 8) | (1 << 2);
149 case 1: /* PDE, should not happen */
150 case 3: /* Reserved */
151 return (3 << 8) | (4 << 2);
152 case 2: /* L3 PTE */
1658dd32 153 page_offset = 0;
163fa5ca
BS
154 }
155 *page_size = TARGET_PAGE_SIZE;
156 break;
157 case 2: /* L2 PTE */
1658dd32 158 page_offset = address & 0x3f000;
163fa5ca
BS
159 *page_size = 0x40000;
160 }
161 break;
162 case 2: /* L1 PTE */
1658dd32 163 page_offset = address & 0xfff000;
163fa5ca
BS
164 *page_size = 0x1000000;
165 }
166 }
167
168 /* check access */
169 access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
170 error_code = access_table[*access_index][access_perms];
171 if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user)) {
172 return error_code;
173 }
174
175 /* update page modified and dirty bits */
176 is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
177 if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
178 pde |= PG_ACCESSED_MASK;
179 if (is_dirty) {
180 pde |= PG_MODIFIED_MASK;
181 }
182 stl_phys_notdirty(pde_ptr, pde);
183 }
184
185 /* the page can be put in the TLB */
186 *prot = perm_table[is_user][access_perms];
187 if (!(pde & PG_MODIFIED_MASK)) {
188 /* only set write access if already dirty... otherwise wait
189 for dirty access */
190 *prot &= ~PAGE_WRITE;
191 }
192
193 /* Even if large ptes, we map only one 4KB page in the cache to
194 avoid filling it too fast */
a8170e5e 195 *physical = ((hwaddr)(pde & PTE_ADDR_MASK) << 4) + page_offset;
163fa5ca
BS
196 return error_code;
197}
198
199/* Perform address translation */
c5f9864e 200int cpu_sparc_handle_mmu_fault(CPUSPARCState *env, target_ulong address, int rw,
163fa5ca
BS
201 int mmu_idx)
202{
a8170e5e 203 hwaddr paddr;
163fa5ca
BS
204 target_ulong vaddr;
205 target_ulong page_size;
206 int error_code = 0, prot, access_index;
207
1658dd32 208 address &= TARGET_PAGE_MASK;
163fa5ca
BS
209 error_code = get_physical_address(env, &paddr, &prot, &access_index,
210 address, rw, mmu_idx, &page_size);
1658dd32 211 vaddr = address;
163fa5ca 212 if (error_code == 0) {
163fa5ca
BS
213#ifdef DEBUG_MMU
214 printf("Translate at " TARGET_FMT_lx " -> " TARGET_FMT_plx ", vaddr "
215 TARGET_FMT_lx "\n", address, paddr, vaddr);
216#endif
217 tlb_set_page(env, vaddr, paddr, prot, mmu_idx, page_size);
218 return 0;
219 }
220
221 if (env->mmuregs[3]) { /* Fault status register */
222 env->mmuregs[3] = 1; /* overflow (not read before another fault) */
223 }
224 env->mmuregs[3] |= (access_index << 5) | error_code | 2;
225 env->mmuregs[4] = address; /* Fault address register */
226
227 if ((env->mmuregs[0] & MMU_NF) || env->psret == 0) {
228 /* No fault mode: if a mapping is available, just override
229 permissions. If no mapping is available, redirect accesses to
230 neverland. Fake/overridden mappings will be flushed when
231 switching to normal mode. */
163fa5ca
BS
232 prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
233 tlb_set_page(env, vaddr, paddr, prot, mmu_idx, TARGET_PAGE_SIZE);
234 return 0;
235 } else {
236 if (rw & 2) {
237 env->exception_index = TT_TFAULT;
238 } else {
239 env->exception_index = TT_DFAULT;
240 }
241 return 1;
242 }
243}
244
c5f9864e 245target_ulong mmu_probe(CPUSPARCState *env, target_ulong address, int mmulev)
163fa5ca 246{
a8170e5e 247 hwaddr pde_ptr;
163fa5ca
BS
248 uint32_t pde;
249
250 /* Context base + context number */
a8170e5e 251 pde_ptr = (hwaddr)(env->mmuregs[1] << 4) +
163fa5ca
BS
252 (env->mmuregs[2] << 2);
253 pde = ldl_phys(pde_ptr);
254
255 switch (pde & PTE_ENTRYTYPE_MASK) {
256 default:
257 case 0: /* Invalid */
258 case 2: /* PTE, maybe should not happen? */
259 case 3: /* Reserved */
260 return 0;
261 case 1: /* L1 PDE */
262 if (mmulev == 3) {
263 return pde;
264 }
265 pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
266 pde = ldl_phys(pde_ptr);
267
268 switch (pde & PTE_ENTRYTYPE_MASK) {
269 default:
270 case 0: /* Invalid */
271 case 3: /* Reserved */
272 return 0;
273 case 2: /* L1 PTE */
274 return pde;
275 case 1: /* L2 PDE */
276 if (mmulev == 2) {
277 return pde;
278 }
279 pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
280 pde = ldl_phys(pde_ptr);
281
282 switch (pde & PTE_ENTRYTYPE_MASK) {
283 default:
284 case 0: /* Invalid */
285 case 3: /* Reserved */
286 return 0;
287 case 2: /* L2 PTE */
288 return pde;
289 case 1: /* L3 PDE */
290 if (mmulev == 1) {
291 return pde;
292 }
293 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
294 pde = ldl_phys(pde_ptr);
295
296 switch (pde & PTE_ENTRYTYPE_MASK) {
297 default:
298 case 0: /* Invalid */
299 case 1: /* PDE, should not happen */
300 case 3: /* Reserved */
301 return 0;
302 case 2: /* L3 PTE */
303 return pde;
304 }
305 }
306 }
307 }
308 return 0;
309}
310
c5f9864e 311void dump_mmu(FILE *f, fprintf_function cpu_fprintf, CPUSPARCState *env)
163fa5ca 312{
00b941e5 313 CPUState *cs = CPU(sparc_env_get_cpu(env));
163fa5ca
BS
314 target_ulong va, va1, va2;
315 unsigned int n, m, o;
a8170e5e 316 hwaddr pde_ptr, pa;
163fa5ca
BS
317 uint32_t pde;
318
319 pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
320 pde = ldl_phys(pde_ptr);
321 (*cpu_fprintf)(f, "Root ptr: " TARGET_FMT_plx ", ctx: %d\n",
a8170e5e 322 (hwaddr)env->mmuregs[1] << 4, env->mmuregs[2]);
163fa5ca
BS
323 for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
324 pde = mmu_probe(env, va, 2);
325 if (pde) {
00b941e5 326 pa = cpu_get_phys_page_debug(cs, va);
163fa5ca
BS
327 (*cpu_fprintf)(f, "VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
328 " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
329 for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
330 pde = mmu_probe(env, va1, 1);
331 if (pde) {
00b941e5 332 pa = cpu_get_phys_page_debug(cs, va1);
163fa5ca
BS
333 (*cpu_fprintf)(f, " VA: " TARGET_FMT_lx ", PA: "
334 TARGET_FMT_plx " PDE: " TARGET_FMT_lx "\n",
335 va1, pa, pde);
336 for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
337 pde = mmu_probe(env, va2, 0);
338 if (pde) {
00b941e5 339 pa = cpu_get_phys_page_debug(cs, va2);
163fa5ca
BS
340 (*cpu_fprintf)(f, " VA: " TARGET_FMT_lx ", PA: "
341 TARGET_FMT_plx " PTE: "
342 TARGET_FMT_lx "\n",
343 va2, pa, pde);
344 }
345 }
346 }
347 }
348 }
349 }
350}
351
352/* Gdb expects all registers windows to be flushed in ram. This function handles
353 * reads (and only reads) in stack frames as if windows were flushed. We assume
354 * that the sparc ABI is followed.
355 */
c5f9864e 356int target_memory_rw_debug(CPUSPARCState *env, target_ulong addr,
163fa5ca
BS
357 uint8_t *buf, int len, int is_write)
358{
f17ec444 359 CPUState *cs = CPU(sparc_env_get_cpu(env));
163fa5ca
BS
360 int i;
361 int len1;
362 int cwp = env->cwp;
363
364 if (!is_write) {
365 for (i = 0; i < env->nwindows; i++) {
366 int off;
367 target_ulong fp = env->regbase[cwp * 16 + 22];
368
369 /* Assume fp == 0 means end of frame. */
370 if (fp == 0) {
371 break;
372 }
373
374 cwp = cpu_cwp_inc(env, cwp + 1);
375
376 /* Invalid window ? */
377 if (env->wim & (1 << cwp)) {
378 break;
379 }
380
381 /* According to the ABI, the stack is growing downward. */
382 if (addr + len < fp) {
383 break;
384 }
385
386 /* Not in this frame. */
387 if (addr > fp + 64) {
388 continue;
389 }
390
391 /* Handle access before this window. */
392 if (addr < fp) {
393 len1 = fp - addr;
f17ec444 394 if (cpu_memory_rw_debug(cs, addr, buf, len1, is_write) != 0) {
163fa5ca
BS
395 return -1;
396 }
397 addr += len1;
398 len -= len1;
399 buf += len1;
400 }
401
402 /* Access byte per byte to registers. Not very efficient but speed
403 * is not critical.
404 */
405 off = addr - fp;
406 len1 = 64 - off;
407
408 if (len1 > len) {
409 len1 = len;
410 }
411
412 for (; len1; len1--) {
413 int reg = cwp * 16 + 8 + (off >> 2);
414 union {
415 uint32_t v;
416 uint8_t c[4];
417 } u;
418 u.v = cpu_to_be32(env->regbase[reg]);
419 *buf++ = u.c[off & 3];
420 addr++;
421 len--;
422 off++;
423 }
424
425 if (len == 0) {
426 return 0;
427 }
428 }
429 }
f17ec444 430 return cpu_memory_rw_debug(cs, addr, buf, len, is_write);
163fa5ca
BS
431}
432
433#else /* !TARGET_SPARC64 */
434
435/* 41 bit physical address space */
a8170e5e 436static inline hwaddr ultrasparc_truncate_physical(uint64_t x)
163fa5ca
BS
437{
438 return x & 0x1ffffffffffULL;
439}
440
441/*
442 * UltraSparc IIi I/DMMUs
443 */
444
445/* Returns true if TTE tag is valid and matches virtual address value
446 in context requires virtual address mask value calculated from TTE
447 entry size */
448static inline int ultrasparc_tag_match(SparcTLBEntry *tlb,
449 uint64_t address, uint64_t context,
a8170e5e 450 hwaddr *physical)
163fa5ca
BS
451{
452 uint64_t mask;
453
454 switch (TTE_PGSIZE(tlb->tte)) {
455 default:
456 case 0x0: /* 8k */
457 mask = 0xffffffffffffe000ULL;
458 break;
459 case 0x1: /* 64k */
460 mask = 0xffffffffffff0000ULL;
461 break;
462 case 0x2: /* 512k */
463 mask = 0xfffffffffff80000ULL;
464 break;
465 case 0x3: /* 4M */
466 mask = 0xffffffffffc00000ULL;
467 break;
468 }
469
470 /* valid, context match, virtual address match? */
471 if (TTE_IS_VALID(tlb->tte) &&
472 (TTE_IS_GLOBAL(tlb->tte) || tlb_compare_context(tlb, context))
473 && compare_masked(address, tlb->tag, mask)) {
474 /* decode physical address */
475 *physical = ((tlb->tte & mask) | (address & ~mask)) & 0x1ffffffe000ULL;
476 return 1;
477 }
478
479 return 0;
480}
481
c5f9864e 482static int get_physical_address_data(CPUSPARCState *env,
a8170e5e 483 hwaddr *physical, int *prot,
163fa5ca
BS
484 target_ulong address, int rw, int mmu_idx)
485{
486 unsigned int i;
487 uint64_t context;
488 uint64_t sfsr = 0;
489
490 int is_user = (mmu_idx == MMU_USER_IDX ||
491 mmu_idx == MMU_USER_SECONDARY_IDX);
492
493 if ((env->lsu & DMMU_E) == 0) { /* DMMU disabled */
494 *physical = ultrasparc_truncate_physical(address);
495 *prot = PAGE_READ | PAGE_WRITE;
496 return 0;
497 }
498
499 switch (mmu_idx) {
500 case MMU_USER_IDX:
501 case MMU_KERNEL_IDX:
502 context = env->dmmu.mmu_primary_context & 0x1fff;
503 sfsr |= SFSR_CT_PRIMARY;
504 break;
505 case MMU_USER_SECONDARY_IDX:
506 case MMU_KERNEL_SECONDARY_IDX:
507 context = env->dmmu.mmu_secondary_context & 0x1fff;
508 sfsr |= SFSR_CT_SECONDARY;
509 break;
510 case MMU_NUCLEUS_IDX:
511 sfsr |= SFSR_CT_NUCLEUS;
512 /* FALLTHRU */
513 default:
514 context = 0;
515 break;
516 }
517
518 if (rw == 1) {
519 sfsr |= SFSR_WRITE_BIT;
520 } else if (rw == 4) {
521 sfsr |= SFSR_NF_BIT;
522 }
523
524 for (i = 0; i < 64; i++) {
525 /* ctx match, vaddr match, valid? */
526 if (ultrasparc_tag_match(&env->dtlb[i], address, context, physical)) {
527 int do_fault = 0;
528
529 /* access ok? */
530 /* multiple bits in SFSR.FT may be set on TT_DFAULT */
531 if (TTE_IS_PRIV(env->dtlb[i].tte) && is_user) {
532 do_fault = 1;
533 sfsr |= SFSR_FT_PRIV_BIT; /* privilege violation */
ec0ceb17 534 trace_mmu_helper_dfault(address, context, mmu_idx, env->tl);
163fa5ca
BS
535 }
536 if (rw == 4) {
537 if (TTE_IS_SIDEEFFECT(env->dtlb[i].tte)) {
538 do_fault = 1;
539 sfsr |= SFSR_FT_NF_E_BIT;
540 }
541 } else {
542 if (TTE_IS_NFO(env->dtlb[i].tte)) {
543 do_fault = 1;
544 sfsr |= SFSR_FT_NFO_BIT;
545 }
546 }
547
548 if (do_fault) {
549 /* faults above are reported with TT_DFAULT. */
550 env->exception_index = TT_DFAULT;
551 } else if (!TTE_IS_W_OK(env->dtlb[i].tte) && (rw == 1)) {
552 do_fault = 1;
553 env->exception_index = TT_DPROT;
554
ec0ceb17 555 trace_mmu_helper_dprot(address, context, mmu_idx, env->tl);
163fa5ca
BS
556 }
557
558 if (!do_fault) {
559 *prot = PAGE_READ;
560 if (TTE_IS_W_OK(env->dtlb[i].tte)) {
561 *prot |= PAGE_WRITE;
562 }
563
564 TTE_SET_USED(env->dtlb[i].tte);
565
566 return 0;
567 }
568
569 if (env->dmmu.sfsr & SFSR_VALID_BIT) { /* Fault status register */
570 sfsr |= SFSR_OW_BIT; /* overflow (not read before
571 another fault) */
572 }
573
574 if (env->pstate & PS_PRIV) {
575 sfsr |= SFSR_PR_BIT;
576 }
577
578 /* FIXME: ASI field in SFSR must be set */
579 env->dmmu.sfsr = sfsr | SFSR_VALID_BIT;
580
581 env->dmmu.sfar = address; /* Fault address register */
582
583 env->dmmu.tag_access = (address & ~0x1fffULL) | context;
584
585 return 1;
586 }
587 }
588
ec0ceb17 589 trace_mmu_helper_dmiss(address, context);
163fa5ca
BS
590
591 /*
592 * On MMU misses:
593 * - UltraSPARC IIi: SFSR and SFAR unmodified
594 * - JPS1: SFAR updated and some fields of SFSR updated
595 */
596 env->dmmu.tag_access = (address & ~0x1fffULL) | context;
597 env->exception_index = TT_DMISS;
598 return 1;
599}
600
c5f9864e 601static int get_physical_address_code(CPUSPARCState *env,
a8170e5e 602 hwaddr *physical, int *prot,
163fa5ca
BS
603 target_ulong address, int mmu_idx)
604{
605 unsigned int i;
606 uint64_t context;
607
608 int is_user = (mmu_idx == MMU_USER_IDX ||
609 mmu_idx == MMU_USER_SECONDARY_IDX);
610
611 if ((env->lsu & IMMU_E) == 0 || (env->pstate & PS_RED) != 0) {
612 /* IMMU disabled */
613 *physical = ultrasparc_truncate_physical(address);
614 *prot = PAGE_EXEC;
615 return 0;
616 }
617
618 if (env->tl == 0) {
619 /* PRIMARY context */
620 context = env->dmmu.mmu_primary_context & 0x1fff;
621 } else {
622 /* NUCLEUS context */
623 context = 0;
624 }
625
626 for (i = 0; i < 64; i++) {
627 /* ctx match, vaddr match, valid? */
628 if (ultrasparc_tag_match(&env->itlb[i],
629 address, context, physical)) {
630 /* access ok? */
631 if (TTE_IS_PRIV(env->itlb[i].tte) && is_user) {
632 /* Fault status register */
633 if (env->immu.sfsr & SFSR_VALID_BIT) {
634 env->immu.sfsr = SFSR_OW_BIT; /* overflow (not read before
635 another fault) */
636 } else {
637 env->immu.sfsr = 0;
638 }
639 if (env->pstate & PS_PRIV) {
640 env->immu.sfsr |= SFSR_PR_BIT;
641 }
642 if (env->tl > 0) {
643 env->immu.sfsr |= SFSR_CT_NUCLEUS;
644 }
645
646 /* FIXME: ASI field in SFSR must be set */
647 env->immu.sfsr |= SFSR_FT_PRIV_BIT | SFSR_VALID_BIT;
648 env->exception_index = TT_TFAULT;
649
650 env->immu.tag_access = (address & ~0x1fffULL) | context;
651
ec0ceb17 652 trace_mmu_helper_tfault(address, context);
163fa5ca
BS
653
654 return 1;
655 }
656 *prot = PAGE_EXEC;
657 TTE_SET_USED(env->itlb[i].tte);
658 return 0;
659 }
660 }
661
ec0ceb17 662 trace_mmu_helper_tmiss(address, context);
163fa5ca
BS
663
664 /* Context is stored in DMMU (dmmuregs[1]) also for IMMU */
665 env->immu.tag_access = (address & ~0x1fffULL) | context;
666 env->exception_index = TT_TMISS;
667 return 1;
668}
669
a8170e5e 670static int get_physical_address(CPUSPARCState *env, hwaddr *physical,
163fa5ca
BS
671 int *prot, int *access_index,
672 target_ulong address, int rw, int mmu_idx,
673 target_ulong *page_size)
674{
675 /* ??? We treat everything as a small page, then explicitly flush
676 everything when an entry is evicted. */
677 *page_size = TARGET_PAGE_SIZE;
678
163fa5ca
BS
679 /* safety net to catch wrong softmmu index use from dynamic code */
680 if (env->tl > 0 && mmu_idx != MMU_NUCLEUS_IDX) {
ec0ceb17
BS
681 if (rw == 2) {
682 trace_mmu_helper_get_phys_addr_code(env->tl, mmu_idx,
683 env->dmmu.mmu_primary_context,
684 env->dmmu.mmu_secondary_context,
685 address);
686 } else {
687 trace_mmu_helper_get_phys_addr_data(env->tl, mmu_idx,
688 env->dmmu.mmu_primary_context,
689 env->dmmu.mmu_secondary_context,
690 address);
691 }
163fa5ca 692 }
163fa5ca
BS
693
694 if (rw == 2) {
695 return get_physical_address_code(env, physical, prot, address,
696 mmu_idx);
697 } else {
698 return get_physical_address_data(env, physical, prot, address, rw,
699 mmu_idx);
700 }
701}
702
703/* Perform address translation */
c5f9864e 704int cpu_sparc_handle_mmu_fault(CPUSPARCState *env, target_ulong address, int rw,
163fa5ca
BS
705 int mmu_idx)
706{
1658dd32 707 target_ulong vaddr;
a8170e5e 708 hwaddr paddr;
163fa5ca
BS
709 target_ulong page_size;
710 int error_code = 0, prot, access_index;
711
1658dd32 712 address &= TARGET_PAGE_MASK;
163fa5ca
BS
713 error_code = get_physical_address(env, &paddr, &prot, &access_index,
714 address, rw, mmu_idx, &page_size);
715 if (error_code == 0) {
1658dd32 716 vaddr = address;
163fa5ca 717
ec0ceb17
BS
718 trace_mmu_helper_mmu_fault(address, paddr, mmu_idx, env->tl,
719 env->dmmu.mmu_primary_context,
720 env->dmmu.mmu_secondary_context);
163fa5ca
BS
721
722 tlb_set_page(env, vaddr, paddr, prot, mmu_idx, page_size);
723 return 0;
724 }
725 /* XXX */
726 return 1;
727}
728
c5f9864e 729void dump_mmu(FILE *f, fprintf_function cpu_fprintf, CPUSPARCState *env)
163fa5ca
BS
730{
731 unsigned int i;
732 const char *mask;
733
734 (*cpu_fprintf)(f, "MMU contexts: Primary: %" PRId64 ", Secondary: %"
735 PRId64 "\n",
736 env->dmmu.mmu_primary_context,
737 env->dmmu.mmu_secondary_context);
738 if ((env->lsu & DMMU_E) == 0) {
739 (*cpu_fprintf)(f, "DMMU disabled\n");
740 } else {
741 (*cpu_fprintf)(f, "DMMU dump\n");
742 for (i = 0; i < 64; i++) {
743 switch (TTE_PGSIZE(env->dtlb[i].tte)) {
744 default:
745 case 0x0:
746 mask = " 8k";
747 break;
748 case 0x1:
749 mask = " 64k";
750 break;
751 case 0x2:
752 mask = "512k";
753 break;
754 case 0x3:
755 mask = " 4M";
756 break;
757 }
758 if (TTE_IS_VALID(env->dtlb[i].tte)) {
759 (*cpu_fprintf)(f, "[%02u] VA: %" PRIx64 ", PA: %llx"
760 ", %s, %s, %s, %s, ctx %" PRId64 " %s\n",
761 i,
762 env->dtlb[i].tag & (uint64_t)~0x1fffULL,
763 TTE_PA(env->dtlb[i].tte),
764 mask,
765 TTE_IS_PRIV(env->dtlb[i].tte) ? "priv" : "user",
766 TTE_IS_W_OK(env->dtlb[i].tte) ? "RW" : "RO",
767 TTE_IS_LOCKED(env->dtlb[i].tte) ?
768 "locked" : "unlocked",
769 env->dtlb[i].tag & (uint64_t)0x1fffULL,
770 TTE_IS_GLOBAL(env->dtlb[i].tte) ?
771 "global" : "local");
772 }
773 }
774 }
775 if ((env->lsu & IMMU_E) == 0) {
776 (*cpu_fprintf)(f, "IMMU disabled\n");
777 } else {
778 (*cpu_fprintf)(f, "IMMU dump\n");
779 for (i = 0; i < 64; i++) {
780 switch (TTE_PGSIZE(env->itlb[i].tte)) {
781 default:
782 case 0x0:
783 mask = " 8k";
784 break;
785 case 0x1:
786 mask = " 64k";
787 break;
788 case 0x2:
789 mask = "512k";
790 break;
791 case 0x3:
792 mask = " 4M";
793 break;
794 }
795 if (TTE_IS_VALID(env->itlb[i].tte)) {
796 (*cpu_fprintf)(f, "[%02u] VA: %" PRIx64 ", PA: %llx"
797 ", %s, %s, %s, ctx %" PRId64 " %s\n",
798 i,
799 env->itlb[i].tag & (uint64_t)~0x1fffULL,
800 TTE_PA(env->itlb[i].tte),
801 mask,
802 TTE_IS_PRIV(env->itlb[i].tte) ? "priv" : "user",
803 TTE_IS_LOCKED(env->itlb[i].tte) ?
804 "locked" : "unlocked",
805 env->itlb[i].tag & (uint64_t)0x1fffULL,
806 TTE_IS_GLOBAL(env->itlb[i].tte) ?
807 "global" : "local");
808 }
809 }
810 }
811}
812
813#endif /* TARGET_SPARC64 */
814
a8170e5e 815static int cpu_sparc_get_phys_page(CPUSPARCState *env, hwaddr *phys,
163fa5ca
BS
816 target_ulong addr, int rw, int mmu_idx)
817{
818 target_ulong page_size;
819 int prot, access_index;
820
821 return get_physical_address(env, phys, &prot, &access_index, addr, rw,
822 mmu_idx, &page_size);
823}
824
825#if defined(TARGET_SPARC64)
a8170e5e 826hwaddr cpu_get_phys_page_nofault(CPUSPARCState *env, target_ulong addr,
163fa5ca
BS
827 int mmu_idx)
828{
a8170e5e 829 hwaddr phys_addr;
163fa5ca
BS
830
831 if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 4, mmu_idx) != 0) {
832 return -1;
833 }
834 return phys_addr;
835}
836#endif
837
00b941e5 838hwaddr sparc_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
163fa5ca 839{
00b941e5
AF
840 SPARCCPU *cpu = SPARC_CPU(cs);
841 CPUSPARCState *env = &cpu->env;
a8170e5e 842 hwaddr phys_addr;
163fa5ca 843 int mmu_idx = cpu_mmu_index(env);
cc4aa830 844 MemoryRegionSection section;
163fa5ca
BS
845
846 if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 2, mmu_idx) != 0) {
847 if (cpu_sparc_get_phys_page(env, &phys_addr, addr, 0, mmu_idx) != 0) {
848 return -1;
849 }
850 }
cc4aa830 851 section = memory_region_find(get_system_memory(), phys_addr, 1);
dfde4e6e 852 memory_region_unref(section.mr);
052e87b0 853 if (!int128_nz(section.size)) {
163fa5ca
BS
854 return -1;
855 }
856 return phys_addr;
857}
858#endif