4 * Copyright (c) 2003-2005 Fabrice Bellard
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 /* Sparc MMU emulation */
37 spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
41 spin_lock(&global_cpu_lock);
46 spin_unlock(&global_cpu_lock);
49 #if defined(CONFIG_USER_ONLY)
51 int cpu_sparc_handle_mmu_fault(CPUState *env, target_ulong address, int rw,
52 int mmu_idx, int is_softmmu)
55 env->exception_index = TT_TFAULT;
57 env->exception_index = TT_DFAULT;
63 #ifndef TARGET_SPARC64
65 * Sparc V8 Reference MMU (SRMMU)
67 static const int access_table[8][8] = {
68 { 0, 0, 0, 0, 2, 0, 3, 3 },
69 { 0, 0, 0, 0, 2, 0, 0, 0 },
70 { 2, 2, 0, 0, 0, 2, 3, 3 },
71 { 2, 2, 0, 0, 0, 2, 0, 0 },
72 { 2, 0, 2, 0, 2, 2, 3, 3 },
73 { 2, 0, 2, 0, 2, 0, 2, 0 },
74 { 2, 2, 2, 0, 2, 2, 3, 3 },
75 { 2, 2, 2, 0, 2, 2, 2, 0 }
78 static const int perm_table[2][8] = {
81 PAGE_READ | PAGE_WRITE,
82 PAGE_READ | PAGE_EXEC,
83 PAGE_READ | PAGE_WRITE | PAGE_EXEC,
85 PAGE_READ | PAGE_WRITE,
86 PAGE_READ | PAGE_EXEC,
87 PAGE_READ | PAGE_WRITE | PAGE_EXEC
91 PAGE_READ | PAGE_WRITE,
92 PAGE_READ | PAGE_EXEC,
93 PAGE_READ | PAGE_WRITE | PAGE_EXEC,
101 int get_physical_address (CPUState *env, target_phys_addr_t *physical, int *prot,
102 int *access_index, target_ulong address, int rw,
105 int access_perms = 0;
106 target_phys_addr_t pde_ptr;
108 target_ulong virt_addr;
109 int error_code = 0, is_dirty, is_user;
110 unsigned long page_offset;
112 is_user = mmu_idx == MMU_USER_IDX;
113 virt_addr = address & TARGET_PAGE_MASK;
115 if ((env->mmuregs[0] & MMU_E) == 0) { /* MMU disabled */
116 // Boot mode: instruction fetches are taken from PROM
117 if (rw == 2 && (env->mmuregs[0] & env->mmu_bm)) {
118 *physical = 0xff0000000ULL | (address & 0x3ffffULL);
119 *prot = PAGE_READ | PAGE_EXEC;
123 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
127 *access_index = ((rw & 1) << 2) | (rw & 2) | (is_user? 0 : 1);
128 *physical = 0xffffffffffff0000ULL;
130 /* SPARC reference MMU table walk: Context table->L1->L2->PTE */
131 /* Context base + context number */
132 pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
133 pde = ldl_phys(pde_ptr);
136 switch (pde & PTE_ENTRYTYPE_MASK) {
138 case 0: /* Invalid */
140 case 2: /* L0 PTE, maybe should not happen? */
141 case 3: /* Reserved */
144 pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
145 pde = ldl_phys(pde_ptr);
147 switch (pde & PTE_ENTRYTYPE_MASK) {
149 case 0: /* Invalid */
150 return (1 << 8) | (1 << 2);
151 case 3: /* Reserved */
152 return (1 << 8) | (4 << 2);
154 pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
155 pde = ldl_phys(pde_ptr);
157 switch (pde & PTE_ENTRYTYPE_MASK) {
159 case 0: /* Invalid */
160 return (2 << 8) | (1 << 2);
161 case 3: /* Reserved */
162 return (2 << 8) | (4 << 2);
164 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
165 pde = ldl_phys(pde_ptr);
167 switch (pde & PTE_ENTRYTYPE_MASK) {
169 case 0: /* Invalid */
170 return (3 << 8) | (1 << 2);
171 case 1: /* PDE, should not happen */
172 case 3: /* Reserved */
173 return (3 << 8) | (4 << 2);
175 virt_addr = address & TARGET_PAGE_MASK;
176 page_offset = (address & TARGET_PAGE_MASK) & (TARGET_PAGE_SIZE - 1);
180 virt_addr = address & ~0x3ffff;
181 page_offset = address & 0x3ffff;
185 virt_addr = address & ~0xffffff;
186 page_offset = address & 0xffffff;
190 /* update page modified and dirty bits */
191 is_dirty = (rw & 1) && !(pde & PG_MODIFIED_MASK);
192 if (!(pde & PG_ACCESSED_MASK) || is_dirty) {
193 pde |= PG_ACCESSED_MASK;
195 pde |= PG_MODIFIED_MASK;
196 stl_phys_notdirty(pde_ptr, pde);
199 access_perms = (pde & PTE_ACCESS_MASK) >> PTE_ACCESS_SHIFT;
200 error_code = access_table[*access_index][access_perms];
201 if (error_code && !((env->mmuregs[0] & MMU_NF) && is_user))
204 /* the page can be put in the TLB */
205 *prot = perm_table[is_user][access_perms];
206 if (!(pde & PG_MODIFIED_MASK)) {
207 /* only set write access if already dirty... otherwise wait
209 *prot &= ~PAGE_WRITE;
212 /* Even if large ptes, we map only one 4KB page in the cache to
213 avoid filling it too fast */
214 *physical = ((target_phys_addr_t)(pde & PTE_ADDR_MASK) << 4) + page_offset;
218 /* Perform address translation */
219 int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
220 int mmu_idx, int is_softmmu)
222 target_phys_addr_t paddr;
224 int error_code = 0, prot, ret = 0, access_index;
226 error_code = get_physical_address(env, &paddr, &prot, &access_index, address, rw, mmu_idx);
227 if (error_code == 0) {
228 vaddr = address & TARGET_PAGE_MASK;
229 paddr &= TARGET_PAGE_MASK;
231 printf("Translate at " TARGET_FMT_lx " -> " TARGET_FMT_plx ", vaddr "
232 TARGET_FMT_lx "\n", address, paddr, vaddr);
234 ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
238 if (env->mmuregs[3]) /* Fault status register */
239 env->mmuregs[3] = 1; /* overflow (not read before another fault) */
240 env->mmuregs[3] |= (access_index << 5) | error_code | 2;
241 env->mmuregs[4] = address; /* Fault address register */
243 if ((env->mmuregs[0] & MMU_NF) || env->psret == 0) {
244 // No fault mode: if a mapping is available, just override
245 // permissions. If no mapping is available, redirect accesses to
246 // neverland. Fake/overridden mappings will be flushed when
247 // switching to normal mode.
248 vaddr = address & TARGET_PAGE_MASK;
249 prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
250 ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
254 env->exception_index = TT_TFAULT;
256 env->exception_index = TT_DFAULT;
261 target_ulong mmu_probe(CPUState *env, target_ulong address, int mmulev)
263 target_phys_addr_t pde_ptr;
266 /* Context base + context number */
267 pde_ptr = (target_phys_addr_t)(env->mmuregs[1] << 4) +
268 (env->mmuregs[2] << 2);
269 pde = ldl_phys(pde_ptr);
271 switch (pde & PTE_ENTRYTYPE_MASK) {
273 case 0: /* Invalid */
274 case 2: /* PTE, maybe should not happen? */
275 case 3: /* Reserved */
280 pde_ptr = ((address >> 22) & ~3) + ((pde & ~3) << 4);
281 pde = ldl_phys(pde_ptr);
283 switch (pde & PTE_ENTRYTYPE_MASK) {
285 case 0: /* Invalid */
286 case 3: /* Reserved */
293 pde_ptr = ((address & 0xfc0000) >> 16) + ((pde & ~3) << 4);
294 pde = ldl_phys(pde_ptr);
296 switch (pde & PTE_ENTRYTYPE_MASK) {
298 case 0: /* Invalid */
299 case 3: /* Reserved */
306 pde_ptr = ((address & 0x3f000) >> 10) + ((pde & ~3) << 4);
307 pde = ldl_phys(pde_ptr);
309 switch (pde & PTE_ENTRYTYPE_MASK) {
311 case 0: /* Invalid */
312 case 1: /* PDE, should not happen */
313 case 3: /* Reserved */
325 void dump_mmu(CPUState *env)
327 target_ulong va, va1, va2;
328 unsigned int n, m, o;
329 target_phys_addr_t pde_ptr, pa;
332 printf("MMU dump:\n");
333 pde_ptr = (env->mmuregs[1] << 4) + (env->mmuregs[2] << 2);
334 pde = ldl_phys(pde_ptr);
335 printf("Root ptr: " TARGET_FMT_plx ", ctx: %d\n",
336 (target_phys_addr_t)env->mmuregs[1] << 4, env->mmuregs[2]);
337 for (n = 0, va = 0; n < 256; n++, va += 16 * 1024 * 1024) {
338 pde = mmu_probe(env, va, 2);
340 pa = cpu_get_phys_page_debug(env, va);
341 printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
342 " PDE: " TARGET_FMT_lx "\n", va, pa, pde);
343 for (m = 0, va1 = va; m < 64; m++, va1 += 256 * 1024) {
344 pde = mmu_probe(env, va1, 1);
346 pa = cpu_get_phys_page_debug(env, va1);
347 printf(" VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_plx
348 " PDE: " TARGET_FMT_lx "\n", va1, pa, pde);
349 for (o = 0, va2 = va1; o < 64; o++, va2 += 4 * 1024) {
350 pde = mmu_probe(env, va2, 0);
352 pa = cpu_get_phys_page_debug(env, va2);
353 printf(" VA: " TARGET_FMT_lx ", PA: "
354 TARGET_FMT_plx " PTE: " TARGET_FMT_lx "\n",
362 printf("MMU dump ends\n");
364 #endif /* DEBUG_MMU */
366 #else /* !TARGET_SPARC64 */
368 * UltraSparc IIi I/DMMUs
370 static int get_physical_address_data(CPUState *env, target_phys_addr_t *physical, int *prot,
371 int *access_index, target_ulong address, int rw,
377 if ((env->lsu & DMMU_E) == 0) { /* DMMU disabled */
379 *prot = PAGE_READ | PAGE_WRITE;
383 for (i = 0; i < 64; i++) {
384 switch ((env->dtlb_tte[i] >> 61) & 3) {
387 mask = 0xffffffffffffe000ULL;
390 mask = 0xffffffffffff0000ULL;
393 mask = 0xfffffffffff80000ULL;
396 mask = 0xffffffffffc00000ULL;
399 // ctx match, vaddr match?
400 if (env->dmmuregs[1] == (env->dtlb_tag[i] & 0x1fff) &&
401 (address & mask) == (env->dtlb_tag[i] & ~0x1fffULL)) {
403 if ((env->dtlb_tte[i] & 0x8000000000000000ULL) == 0 ||
404 ((env->dtlb_tte[i] & 0x4) && is_user) ||
405 (!(env->dtlb_tte[i] & 0x2) && (rw == 1))) {
406 if (env->dmmuregs[3]) /* Fault status register */
407 env->dmmuregs[3] = 2; /* overflow (not read before another fault) */
408 env->dmmuregs[3] |= (is_user << 3) | ((rw == 1) << 2) | 1;
409 env->dmmuregs[4] = address; /* Fault address register */
410 env->exception_index = TT_DFAULT;
412 printf("DFAULT at 0x%" PRIx64 "\n", address);
416 *physical = (env->dtlb_tte[i] & mask & 0x1fffffff000ULL) + (address & ~mask & 0x1fffffff000ULL);
418 if (env->dtlb_tte[i] & 0x2)
424 printf("DMISS at 0x%" PRIx64 "\n", address);
426 env->exception_index = TT_DMISS;
430 static int get_physical_address_code(CPUState *env, target_phys_addr_t *physical, int *prot,
431 int *access_index, target_ulong address, int rw,
437 if ((env->lsu & IMMU_E) == 0) { /* IMMU disabled */
443 for (i = 0; i < 64; i++) {
444 switch ((env->itlb_tte[i] >> 61) & 3) {
447 mask = 0xffffffffffffe000ULL;
450 mask = 0xffffffffffff0000ULL;
453 mask = 0xfffffffffff80000ULL;
456 mask = 0xffffffffffc00000ULL;
459 // ctx match, vaddr match?
460 if (env->dmmuregs[1] == (env->itlb_tag[i] & 0x1fff) &&
461 (address & mask) == (env->itlb_tag[i] & ~0x1fffULL)) {
463 if ((env->itlb_tte[i] & 0x8000000000000000ULL) == 0 ||
464 ((env->itlb_tte[i] & 0x4) && is_user)) {
465 if (env->immuregs[3]) /* Fault status register */
466 env->immuregs[3] = 2; /* overflow (not read before another fault) */
467 env->immuregs[3] |= (is_user << 3) | 1;
468 env->exception_index = TT_TFAULT;
470 printf("TFAULT at 0x%" PRIx64 "\n", address);
474 *physical = (env->itlb_tte[i] & mask & 0x1fffffff000ULL) + (address & ~mask & 0x1fffffff000ULL);
480 printf("TMISS at 0x%" PRIx64 "\n", address);
482 env->exception_index = TT_TMISS;
486 int get_physical_address(CPUState *env, target_phys_addr_t *physical, int *prot,
487 int *access_index, target_ulong address, int rw,
490 int is_user = mmu_idx == MMU_USER_IDX;
493 return get_physical_address_code(env, physical, prot, access_index, address, rw, is_user);
495 return get_physical_address_data(env, physical, prot, access_index, address, rw, is_user);
498 /* Perform address translation */
499 int cpu_sparc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
500 int mmu_idx, int is_softmmu)
502 target_ulong virt_addr, vaddr;
503 target_phys_addr_t paddr;
504 int error_code = 0, prot, ret = 0, access_index;
506 error_code = get_physical_address(env, &paddr, &prot, &access_index, address, rw, mmu_idx);
507 if (error_code == 0) {
508 virt_addr = address & TARGET_PAGE_MASK;
509 vaddr = virt_addr + ((address & TARGET_PAGE_MASK) & (TARGET_PAGE_SIZE - 1));
511 printf("Translate at 0x%" PRIx64 " -> 0x%" PRIx64 ", vaddr 0x%" PRIx64 "\n", address, paddr, vaddr);
513 ret = tlb_set_page_exec(env, vaddr, paddr, prot, mmu_idx, is_softmmu);
521 void dump_mmu(CPUState *env)
526 printf("MMU contexts: Primary: %" PRId64 ", Secondary: %" PRId64 "\n", env->dmmuregs[1], env->dmmuregs[2]);
527 if ((env->lsu & DMMU_E) == 0) {
528 printf("DMMU disabled\n");
530 printf("DMMU dump:\n");
531 for (i = 0; i < 64; i++) {
532 switch ((env->dtlb_tte[i] >> 61) & 3) {
547 if ((env->dtlb_tte[i] & 0x8000000000000000ULL) != 0) {
548 printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_lx ", %s, %s, %s, %s, ctx %" PRId64 "\n",
549 env->dtlb_tag[i] & ~0x1fffULL,
550 env->dtlb_tte[i] & 0x1ffffffe000ULL,
552 env->dtlb_tte[i] & 0x4? "priv": "user",
553 env->dtlb_tte[i] & 0x2? "RW": "RO",
554 env->dtlb_tte[i] & 0x40? "locked": "unlocked",
555 env->dtlb_tag[i] & 0x1fffULL);
559 if ((env->lsu & IMMU_E) == 0) {
560 printf("IMMU disabled\n");
562 printf("IMMU dump:\n");
563 for (i = 0; i < 64; i++) {
564 switch ((env->itlb_tte[i] >> 61) & 3) {
579 if ((env->itlb_tte[i] & 0x8000000000000000ULL) != 0) {
580 printf("VA: " TARGET_FMT_lx ", PA: " TARGET_FMT_lx ", %s, %s, %s, ctx %" PRId64 "\n",
581 env->itlb_tag[i] & ~0x1fffULL,
582 env->itlb_tte[i] & 0x1ffffffe000ULL,
584 env->itlb_tte[i] & 0x4? "priv": "user",
585 env->itlb_tte[i] & 0x40? "locked": "unlocked",
586 env->itlb_tag[i] & 0x1fffULL);
591 #endif /* DEBUG_MMU */
593 #endif /* TARGET_SPARC64 */
594 #endif /* !CONFIG_USER_ONLY */
596 void memcpy32(target_ulong *dst, const target_ulong *src)