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