2 * PowerPC MMU, TLB, SLB and BAT emulation helpers for QEMU.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
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, see <http://www.gnu.org/licenses/>.
20 #include "exec/helper-proto.h"
21 #include "sysemu/kvm.h"
23 #include "mmu-hash64.h"
24 #include "mmu-hash32.h"
25 #include "exec/cpu_ldst.h"
29 //#define DEBUG_SOFTWARE_TLB
30 //#define DUMP_PAGE_TABLES
31 //#define FLUSH_ALL_TLBS
34 # define LOG_MMU_STATE(cpu) log_cpu_state_mask(CPU_LOG_MMU, (cpu), 0)
36 # define LOG_MMU_STATE(cpu) do { } while (0)
39 #ifdef DEBUG_SOFTWARE_TLB
40 # define LOG_SWTLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
42 # define LOG_SWTLB(...) do { } while (0)
46 # define LOG_BATS(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
48 # define LOG_BATS(...) do { } while (0)
51 /*****************************************************************************/
52 /* PowerPC MMU emulation */
54 /* Context used internally during MMU translations */
55 typedef struct mmu_ctx_t mmu_ctx_t
;
57 hwaddr raddr
; /* Real address */
58 hwaddr eaddr
; /* Effective address */
59 int prot
; /* Protection bits */
60 hwaddr hash
[2]; /* Pagetable hash values */
61 target_ulong ptem
; /* Virtual segment ID | API */
62 int key
; /* Access key */
63 int nx
; /* Non-execute area */
66 /* Common routines used by software and hardware TLBs emulation */
67 static inline int pte_is_valid(target_ulong pte0
)
69 return pte0
& 0x80000000 ? 1 : 0;
72 static inline void pte_invalidate(target_ulong
*pte0
)
77 #define PTE_PTEM_MASK 0x7FFFFFBF
78 #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
80 static int pp_check(int key
, int pp
, int nx
)
84 /* Compute access rights */
107 access
= PAGE_READ
| PAGE_WRITE
;
118 static int check_prot(int prot
, int rw
, int access_type
)
122 if (access_type
== ACCESS_CODE
) {
123 if (prot
& PAGE_EXEC
) {
129 if (prot
& PAGE_WRITE
) {
135 if (prot
& PAGE_READ
) {
145 static inline int ppc6xx_tlb_pte_check(mmu_ctx_t
*ctx
, target_ulong pte0
,
146 target_ulong pte1
, int h
, int rw
, int type
)
148 target_ulong ptem
, mmask
;
149 int access
, ret
, pteh
, ptev
, pp
;
152 /* Check validity and table match */
153 ptev
= pte_is_valid(pte0
);
154 pteh
= (pte0
>> 6) & 1;
155 if (ptev
&& h
== pteh
) {
156 /* Check vsid & api */
157 ptem
= pte0
& PTE_PTEM_MASK
;
158 mmask
= PTE_CHECK_MASK
;
159 pp
= pte1
& 0x00000003;
160 if (ptem
== ctx
->ptem
) {
161 if (ctx
->raddr
!= (hwaddr
)-1ULL) {
162 /* all matches should have equal RPN, WIMG & PP */
163 if ((ctx
->raddr
& mmask
) != (pte1
& mmask
)) {
164 qemu_log_mask(CPU_LOG_MMU
, "Bad RPN/WIMG/PP\n");
168 /* Compute access rights */
169 access
= pp_check(ctx
->key
, pp
, ctx
->nx
);
170 /* Keep the matching PTE informations */
173 ret
= check_prot(ctx
->prot
, rw
, type
);
176 qemu_log_mask(CPU_LOG_MMU
, "PTE access granted !\n");
178 /* Access right violation */
179 qemu_log_mask(CPU_LOG_MMU
, "PTE access rejected\n");
187 static int pte_update_flags(mmu_ctx_t
*ctx
, target_ulong
*pte1p
,
192 /* Update page flags */
193 if (!(*pte1p
& 0x00000100)) {
194 /* Update accessed flag */
195 *pte1p
|= 0x00000100;
198 if (!(*pte1p
& 0x00000080)) {
199 if (rw
== 1 && ret
== 0) {
200 /* Update changed flag */
201 *pte1p
|= 0x00000080;
204 /* Force page fault for first write access */
205 ctx
->prot
&= ~PAGE_WRITE
;
212 /* Software driven TLB helpers */
213 static inline int ppc6xx_tlb_getnum(CPUPPCState
*env
, target_ulong eaddr
,
214 int way
, int is_code
)
218 /* Select TLB num in a way from address */
219 nr
= (eaddr
>> TARGET_PAGE_BITS
) & (env
->tlb_per_way
- 1);
221 nr
+= env
->tlb_per_way
* way
;
222 /* 6xx have separate TLBs for instructions and data */
223 if (is_code
&& env
->id_tlbs
== 1) {
230 static inline void ppc6xx_tlb_invalidate_all(CPUPPCState
*env
)
232 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
236 /* LOG_SWTLB("Invalidate all TLBs\n"); */
237 /* Invalidate all defined software TLB */
239 if (env
->id_tlbs
== 1) {
242 for (nr
= 0; nr
< max
; nr
++) {
243 tlb
= &env
->tlb
.tlb6
[nr
];
244 pte_invalidate(&tlb
->pte0
);
246 tlb_flush(CPU(cpu
), 1);
249 static inline void ppc6xx_tlb_invalidate_virt2(CPUPPCState
*env
,
251 int is_code
, int match_epn
)
253 #if !defined(FLUSH_ALL_TLBS)
254 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
258 /* Invalidate ITLB + DTLB, all ways */
259 for (way
= 0; way
< env
->nb_ways
; way
++) {
260 nr
= ppc6xx_tlb_getnum(env
, eaddr
, way
, is_code
);
261 tlb
= &env
->tlb
.tlb6
[nr
];
262 if (pte_is_valid(tlb
->pte0
) && (match_epn
== 0 || eaddr
== tlb
->EPN
)) {
263 LOG_SWTLB("TLB invalidate %d/%d " TARGET_FMT_lx
"\n", nr
,
265 pte_invalidate(&tlb
->pte0
);
266 tlb_flush_page(cs
, tlb
->EPN
);
270 /* XXX: PowerPC specification say this is valid as well */
271 ppc6xx_tlb_invalidate_all(env
);
275 static inline void ppc6xx_tlb_invalidate_virt(CPUPPCState
*env
,
276 target_ulong eaddr
, int is_code
)
278 ppc6xx_tlb_invalidate_virt2(env
, eaddr
, is_code
, 0);
281 static void ppc6xx_tlb_store(CPUPPCState
*env
, target_ulong EPN
, int way
,
282 int is_code
, target_ulong pte0
, target_ulong pte1
)
287 nr
= ppc6xx_tlb_getnum(env
, EPN
, way
, is_code
);
288 tlb
= &env
->tlb
.tlb6
[nr
];
289 LOG_SWTLB("Set TLB %d/%d EPN " TARGET_FMT_lx
" PTE0 " TARGET_FMT_lx
290 " PTE1 " TARGET_FMT_lx
"\n", nr
, env
->nb_tlb
, EPN
, pte0
, pte1
);
291 /* Invalidate any pending reference in QEMU for this virtual address */
292 ppc6xx_tlb_invalidate_virt2(env
, EPN
, is_code
, 1);
296 /* Store last way for LRU mechanism */
300 static inline int ppc6xx_tlb_check(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
301 target_ulong eaddr
, int rw
, int access_type
)
308 ret
= -1; /* No TLB found */
309 for (way
= 0; way
< env
->nb_ways
; way
++) {
310 nr
= ppc6xx_tlb_getnum(env
, eaddr
, way
,
311 access_type
== ACCESS_CODE
? 1 : 0);
312 tlb
= &env
->tlb
.tlb6
[nr
];
313 /* This test "emulates" the PTE index match for hardware TLBs */
314 if ((eaddr
& TARGET_PAGE_MASK
) != tlb
->EPN
) {
315 LOG_SWTLB("TLB %d/%d %s [" TARGET_FMT_lx
" " TARGET_FMT_lx
316 "] <> " TARGET_FMT_lx
"\n", nr
, env
->nb_tlb
,
317 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
318 tlb
->EPN
, tlb
->EPN
+ TARGET_PAGE_SIZE
, eaddr
);
321 LOG_SWTLB("TLB %d/%d %s " TARGET_FMT_lx
" <> " TARGET_FMT_lx
" "
322 TARGET_FMT_lx
" %c %c\n", nr
, env
->nb_tlb
,
323 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
324 tlb
->EPN
, eaddr
, tlb
->pte1
,
325 rw
? 'S' : 'L', access_type
== ACCESS_CODE
? 'I' : 'D');
326 switch (ppc6xx_tlb_pte_check(ctx
, tlb
->pte0
, tlb
->pte1
, 0, rw
, access_type
)) {
328 /* TLB inconsistency */
331 /* Access violation */
341 /* XXX: we should go on looping to check all TLBs consistency
342 * but we can speed-up the whole thing as the
343 * result would be undefined if TLBs are not consistent.
352 LOG_SWTLB("found TLB at addr " TARGET_FMT_plx
" prot=%01x ret=%d\n",
353 ctx
->raddr
& TARGET_PAGE_MASK
, ctx
->prot
, ret
);
354 /* Update page flags */
355 pte_update_flags(ctx
, &env
->tlb
.tlb6
[best
].pte1
, ret
, rw
);
361 /* Perform BAT hit & translation */
362 static inline void bat_size_prot(CPUPPCState
*env
, target_ulong
*blp
,
363 int *validp
, int *protp
, target_ulong
*BATu
,
369 bl
= (*BATu
& 0x00001FFC) << 15;
372 if (((msr_pr
== 0) && (*BATu
& 0x00000002)) ||
373 ((msr_pr
!= 0) && (*BATu
& 0x00000001))) {
375 pp
= *BATl
& 0x00000003;
377 prot
= PAGE_READ
| PAGE_EXEC
;
388 static int get_bat_6xx_tlb(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
389 target_ulong
virtual, int rw
, int type
)
391 target_ulong
*BATlt
, *BATut
, *BATu
, *BATl
;
392 target_ulong BEPIl
, BEPIu
, bl
;
396 LOG_BATS("%s: %cBAT v " TARGET_FMT_lx
"\n", __func__
,
397 type
== ACCESS_CODE
? 'I' : 'D', virtual);
400 BATlt
= env
->IBAT
[1];
401 BATut
= env
->IBAT
[0];
404 BATlt
= env
->DBAT
[1];
405 BATut
= env
->DBAT
[0];
408 for (i
= 0; i
< env
->nb_BATs
; i
++) {
411 BEPIu
= *BATu
& 0xF0000000;
412 BEPIl
= *BATu
& 0x0FFE0000;
413 bat_size_prot(env
, &bl
, &valid
, &prot
, BATu
, BATl
);
414 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx
" BATu " TARGET_FMT_lx
415 " BATl " TARGET_FMT_lx
"\n", __func__
,
416 type
== ACCESS_CODE
? 'I' : 'D', i
, virtual, *BATu
, *BATl
);
417 if ((virtual & 0xF0000000) == BEPIu
&&
418 ((virtual & 0x0FFE0000) & ~bl
) == BEPIl
) {
421 /* Get physical address */
422 ctx
->raddr
= (*BATl
& 0xF0000000) |
423 ((virtual & 0x0FFE0000 & bl
) | (*BATl
& 0x0FFE0000)) |
424 (virtual & 0x0001F000);
425 /* Compute access rights */
427 ret
= check_prot(ctx
->prot
, rw
, type
);
429 LOG_BATS("BAT %d match: r " TARGET_FMT_plx
" prot=%c%c\n",
430 i
, ctx
->raddr
, ctx
->prot
& PAGE_READ
? 'R' : '-',
431 ctx
->prot
& PAGE_WRITE
? 'W' : '-');
438 #if defined(DEBUG_BATS)
439 if (qemu_log_enabled()) {
440 LOG_BATS("no BAT match for " TARGET_FMT_lx
":\n", virtual);
441 for (i
= 0; i
< 4; i
++) {
444 BEPIu
= *BATu
& 0xF0000000;
445 BEPIl
= *BATu
& 0x0FFE0000;
446 bl
= (*BATu
& 0x00001FFC) << 15;
447 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx
" BATu " TARGET_FMT_lx
448 " BATl " TARGET_FMT_lx
"\n\t" TARGET_FMT_lx
" "
449 TARGET_FMT_lx
" " TARGET_FMT_lx
"\n",
450 __func__
, type
== ACCESS_CODE
? 'I' : 'D', i
, virtual,
451 *BATu
, *BATl
, BEPIu
, BEPIl
, bl
);
460 /* Perform segment based translation */
461 static inline int get_segment_6xx_tlb(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
462 target_ulong eaddr
, int rw
, int type
)
466 int ds
, pr
, target_page_bits
;
468 target_ulong sr
, pgidx
;
473 sr
= env
->sr
[eaddr
>> 28];
474 ctx
->key
= (((sr
& 0x20000000) && (pr
!= 0)) ||
475 ((sr
& 0x40000000) && (pr
== 0))) ? 1 : 0;
476 ds
= sr
& 0x80000000 ? 1 : 0;
477 ctx
->nx
= sr
& 0x10000000 ? 1 : 0;
478 vsid
= sr
& 0x00FFFFFF;
479 target_page_bits
= TARGET_PAGE_BITS
;
480 qemu_log_mask(CPU_LOG_MMU
,
481 "Check segment v=" TARGET_FMT_lx
" %d " TARGET_FMT_lx
482 " nip=" TARGET_FMT_lx
" lr=" TARGET_FMT_lx
483 " ir=%d dr=%d pr=%d %d t=%d\n",
484 eaddr
, (int)(eaddr
>> 28), sr
, env
->nip
, env
->lr
, (int)msr_ir
,
485 (int)msr_dr
, pr
!= 0 ? 1 : 0, rw
, type
);
486 pgidx
= (eaddr
& ~SEGMENT_MASK_256M
) >> target_page_bits
;
488 ctx
->ptem
= (vsid
<< 7) | (pgidx
>> 10);
490 qemu_log_mask(CPU_LOG_MMU
,
491 "pte segment: key=%d ds %d nx %d vsid " TARGET_FMT_lx
"\n",
492 ctx
->key
, ds
, ctx
->nx
, vsid
);
495 /* Check if instruction fetch is allowed, if needed */
496 if (type
!= ACCESS_CODE
|| ctx
->nx
== 0) {
497 /* Page address translation */
498 qemu_log_mask(CPU_LOG_MMU
, "htab_base " TARGET_FMT_plx
499 " htab_mask " TARGET_FMT_plx
500 " hash " TARGET_FMT_plx
"\n",
501 env
->htab_base
, env
->htab_mask
, hash
);
503 ctx
->hash
[1] = ~hash
;
505 /* Initialize real address with an invalid value */
506 ctx
->raddr
= (hwaddr
)-1ULL;
507 /* Software TLB search */
508 ret
= ppc6xx_tlb_check(env
, ctx
, eaddr
, rw
, type
);
509 #if defined(DUMP_PAGE_TABLES)
510 if (qemu_log_mask(CPU_LOG_MMU
)) {
512 uint32_t a0
, a1
, a2
, a3
;
514 qemu_log("Page table: " TARGET_FMT_plx
" len " TARGET_FMT_plx
515 "\n", sdr
, mask
+ 0x80);
516 for (curaddr
= sdr
; curaddr
< (sdr
+ mask
+ 0x80);
518 a0
= ldl_phys(curaddr
);
519 a1
= ldl_phys(curaddr
+ 4);
520 a2
= ldl_phys(curaddr
+ 8);
521 a3
= ldl_phys(curaddr
+ 12);
522 if (a0
!= 0 || a1
!= 0 || a2
!= 0 || a3
!= 0) {
523 qemu_log(TARGET_FMT_plx
": %08x %08x %08x %08x\n",
524 curaddr
, a0
, a1
, a2
, a3
);
530 qemu_log_mask(CPU_LOG_MMU
, "No access allowed\n");
536 qemu_log_mask(CPU_LOG_MMU
, "direct store...\n");
537 /* Direct-store segment : absolutely *BUGGY* for now */
539 /* Direct-store implies a 32-bit MMU.
540 * Check the Segment Register's bus unit ID (BUID).
542 sr
= env
->sr
[eaddr
>> 28];
543 if ((sr
& 0x1FF00000) >> 20 == 0x07f) {
544 /* Memory-forced I/O controller interface access */
545 /* If T=1 and BUID=x'07F', the 601 performs a memory access
546 * to SR[28-31] LA[4-31], bypassing all protection mechanisms.
548 ctx
->raddr
= ((sr
& 0xF) << 28) | (eaddr
& 0x0FFFFFFF);
549 ctx
->prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
555 /* Integer load/store : only access allowed */
558 /* No code fetch is allowed in direct-store areas */
561 /* Floating point load/store */
564 /* lwarx, ldarx or srwcx. */
567 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
568 /* Should make the instruction do no-op.
569 * As it already do no-op, it's quite easy :-)
577 qemu_log_mask(CPU_LOG_MMU
, "ERROR: instruction should not need "
578 "address translation\n");
581 if ((rw
== 1 || ctx
->key
!= 1) && (rw
== 0 || ctx
->key
!= 0)) {
592 /* Generic TLB check function for embedded PowerPC implementations */
593 static int ppcemb_tlb_check(CPUPPCState
*env
, ppcemb_tlb_t
*tlb
,
595 target_ulong address
, uint32_t pid
, int ext
,
600 /* Check valid flag */
601 if (!(tlb
->prot
& PAGE_VALID
)) {
604 mask
= ~(tlb
->size
- 1);
605 LOG_SWTLB("%s: TLB %d address " TARGET_FMT_lx
" PID %u <=> " TARGET_FMT_lx
606 " " TARGET_FMT_lx
" %u %x\n", __func__
, i
, address
, pid
, tlb
->EPN
,
607 mask
, (uint32_t)tlb
->PID
, tlb
->prot
);
609 if (tlb
->PID
!= 0 && tlb
->PID
!= pid
) {
612 /* Check effective address */
613 if ((address
& mask
) != tlb
->EPN
) {
616 *raddrp
= (tlb
->RPN
& mask
) | (address
& ~mask
);
618 /* Extend the physical address to 36 bits */
619 *raddrp
|= (uint64_t)(tlb
->RPN
& 0xF) << 32;
625 /* Generic TLB search function for PowerPC embedded implementations */
626 static int ppcemb_tlb_search(CPUPPCState
*env
, target_ulong address
,
633 /* Default return value is no match */
635 for (i
= 0; i
< env
->nb_tlb
; i
++) {
636 tlb
= &env
->tlb
.tlbe
[i
];
637 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
, pid
, 0, i
) == 0) {
646 /* Helpers specific to PowerPC 40x implementations */
647 static inline void ppc4xx_tlb_invalidate_all(CPUPPCState
*env
)
649 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
653 for (i
= 0; i
< env
->nb_tlb
; i
++) {
654 tlb
= &env
->tlb
.tlbe
[i
];
655 tlb
->prot
&= ~PAGE_VALID
;
657 tlb_flush(CPU(cpu
), 1);
660 static inline void ppc4xx_tlb_invalidate_virt(CPUPPCState
*env
,
661 target_ulong eaddr
, uint32_t pid
)
663 #if !defined(FLUSH_ALL_TLBS)
664 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
667 target_ulong page
, end
;
670 for (i
= 0; i
< env
->nb_tlb
; i
++) {
671 tlb
= &env
->tlb
.tlbe
[i
];
672 if (ppcemb_tlb_check(env
, tlb
, &raddr
, eaddr
, pid
, 0, i
) == 0) {
673 end
= tlb
->EPN
+ tlb
->size
;
674 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
) {
675 tlb_flush_page(cs
, page
);
677 tlb
->prot
&= ~PAGE_VALID
;
682 ppc4xx_tlb_invalidate_all(env
);
686 static int mmu40x_get_physical_address(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
687 target_ulong address
, int rw
,
692 int i
, ret
, zsel
, zpr
, pr
;
695 raddr
= (hwaddr
)-1ULL;
697 for (i
= 0; i
< env
->nb_tlb
; i
++) {
698 tlb
= &env
->tlb
.tlbe
[i
];
699 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
,
700 env
->spr
[SPR_40x_PID
], 0, i
) < 0) {
703 zsel
= (tlb
->attr
>> 4) & 0xF;
704 zpr
= (env
->spr
[SPR_40x_ZPR
] >> (30 - (2 * zsel
))) & 0x3;
705 LOG_SWTLB("%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
706 __func__
, i
, zsel
, zpr
, rw
, tlb
->attr
);
707 /* Check execute enable bit */
715 /* All accesses granted */
716 ctx
->prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
721 /* Raise Zone protection fault. */
722 env
->spr
[SPR_40x_ESR
] = 1 << 22;
730 /* Check from TLB entry */
731 ctx
->prot
= tlb
->prot
;
732 ret
= check_prot(ctx
->prot
, rw
, access_type
);
734 env
->spr
[SPR_40x_ESR
] = 0;
740 LOG_SWTLB("%s: access granted " TARGET_FMT_lx
" => " TARGET_FMT_plx
741 " %d %d\n", __func__
, address
, ctx
->raddr
, ctx
->prot
,
746 LOG_SWTLB("%s: access refused " TARGET_FMT_lx
" => " TARGET_FMT_plx
747 " %d %d\n", __func__
, address
, raddr
, ctx
->prot
, ret
);
752 void store_40x_sler(CPUPPCState
*env
, uint32_t val
)
754 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
756 /* XXX: TO BE FIXED */
757 if (val
!= 0x00000000) {
758 cpu_abort(CPU(cpu
), "Little-endian regions are not supported by now\n");
760 env
->spr
[SPR_405_SLER
] = val
;
763 static inline int mmubooke_check_tlb(CPUPPCState
*env
, ppcemb_tlb_t
*tlb
,
764 hwaddr
*raddr
, int *prot
,
765 target_ulong address
, int rw
,
766 int access_type
, int i
)
770 if (ppcemb_tlb_check(env
, tlb
, raddr
, address
,
771 env
->spr
[SPR_BOOKE_PID
],
772 !env
->nb_pids
, i
) >= 0) {
776 if (env
->spr
[SPR_BOOKE_PID1
] &&
777 ppcemb_tlb_check(env
, tlb
, raddr
, address
,
778 env
->spr
[SPR_BOOKE_PID1
], 0, i
) >= 0) {
782 if (env
->spr
[SPR_BOOKE_PID2
] &&
783 ppcemb_tlb_check(env
, tlb
, raddr
, address
,
784 env
->spr
[SPR_BOOKE_PID2
], 0, i
) >= 0) {
788 LOG_SWTLB("%s: TLB entry not found\n", __func__
);
794 prot2
= tlb
->prot
& 0xF;
796 prot2
= (tlb
->prot
>> 4) & 0xF;
799 /* Check the address space */
800 if (access_type
== ACCESS_CODE
) {
801 if (msr_ir
!= (tlb
->attr
& 1)) {
802 LOG_SWTLB("%s: AS doesn't match\n", __func__
);
807 if (prot2
& PAGE_EXEC
) {
808 LOG_SWTLB("%s: good TLB!\n", __func__
);
812 LOG_SWTLB("%s: no PAGE_EXEC: %x\n", __func__
, prot2
);
815 if (msr_dr
!= (tlb
->attr
& 1)) {
816 LOG_SWTLB("%s: AS doesn't match\n", __func__
);
821 if ((!rw
&& prot2
& PAGE_READ
) || (rw
&& (prot2
& PAGE_WRITE
))) {
822 LOG_SWTLB("%s: found TLB!\n", __func__
);
826 LOG_SWTLB("%s: PAGE_READ/WRITE doesn't match: %x\n", __func__
, prot2
);
833 static int mmubooke_get_physical_address(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
834 target_ulong address
, int rw
,
842 raddr
= (hwaddr
)-1ULL;
843 for (i
= 0; i
< env
->nb_tlb
; i
++) {
844 tlb
= &env
->tlb
.tlbe
[i
];
845 ret
= mmubooke_check_tlb(env
, tlb
, &raddr
, &ctx
->prot
, address
, rw
,
854 LOG_SWTLB("%s: access granted " TARGET_FMT_lx
" => " TARGET_FMT_plx
855 " %d %d\n", __func__
, address
, ctx
->raddr
, ctx
->prot
,
858 LOG_SWTLB("%s: access refused " TARGET_FMT_lx
" => " TARGET_FMT_plx
859 " %d %d\n", __func__
, address
, raddr
, ctx
->prot
, ret
);
865 static void booke206_flush_tlb(CPUPPCState
*env
, int flags
,
866 const int check_iprot
)
868 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
871 ppcmas_tlb_t
*tlb
= env
->tlb
.tlbm
;
873 for (i
= 0; i
< BOOKE206_MAX_TLBN
; i
++) {
874 if (flags
& (1 << i
)) {
875 tlb_size
= booke206_tlb_size(env
, i
);
876 for (j
= 0; j
< tlb_size
; j
++) {
877 if (!check_iprot
|| !(tlb
[j
].mas1
& MAS1_IPROT
)) {
878 tlb
[j
].mas1
&= ~MAS1_VALID
;
882 tlb
+= booke206_tlb_size(env
, i
);
885 tlb_flush(CPU(cpu
), 1);
888 static hwaddr
booke206_tlb_to_page_size(CPUPPCState
*env
,
893 tlbm_size
= (tlb
->mas1
& MAS1_TSIZE_MASK
) >> MAS1_TSIZE_SHIFT
;
895 return 1024ULL << tlbm_size
;
898 /* TLB check function for MAS based SoftTLBs */
899 static int ppcmas_tlb_check(CPUPPCState
*env
, ppcmas_tlb_t
*tlb
,
900 hwaddr
*raddrp
, target_ulong address
,
907 /* In 32bit mode we can only address 32bit EAs */
908 address
= (uint32_t)address
;
911 /* Check valid flag */
912 if (!(tlb
->mas1
& MAS1_VALID
)) {
916 mask
= ~(booke206_tlb_to_page_size(env
, tlb
) - 1);
917 LOG_SWTLB("%s: TLB ADDR=0x" TARGET_FMT_lx
" PID=0x%x MAS1=0x%x MAS2=0x%"
918 PRIx64
" mask=0x" TARGET_FMT_lx
" MAS7_3=0x%" PRIx64
" MAS8=%x\n",
919 __func__
, address
, pid
, tlb
->mas1
, tlb
->mas2
, mask
, tlb
->mas7_3
,
923 tlb_pid
= (tlb
->mas1
& MAS1_TID_MASK
) >> MAS1_TID_SHIFT
;
924 if (tlb_pid
!= 0 && tlb_pid
!= pid
) {
928 /* Check effective address */
929 if ((address
& mask
) != (tlb
->mas2
& MAS2_EPN_MASK
)) {
934 *raddrp
= (tlb
->mas7_3
& mask
) | (address
& ~mask
);
940 static int mmubooke206_check_tlb(CPUPPCState
*env
, ppcmas_tlb_t
*tlb
,
941 hwaddr
*raddr
, int *prot
,
942 target_ulong address
, int rw
,
948 if (ppcmas_tlb_check(env
, tlb
, raddr
, address
,
949 env
->spr
[SPR_BOOKE_PID
]) >= 0) {
953 if (env
->spr
[SPR_BOOKE_PID1
] &&
954 ppcmas_tlb_check(env
, tlb
, raddr
, address
,
955 env
->spr
[SPR_BOOKE_PID1
]) >= 0) {
959 if (env
->spr
[SPR_BOOKE_PID2
] &&
960 ppcmas_tlb_check(env
, tlb
, raddr
, address
,
961 env
->spr
[SPR_BOOKE_PID2
]) >= 0) {
965 LOG_SWTLB("%s: TLB entry not found\n", __func__
);
971 if (tlb
->mas7_3
& MAS3_UR
) {
974 if (tlb
->mas7_3
& MAS3_UW
) {
977 if (tlb
->mas7_3
& MAS3_UX
) {
981 if (tlb
->mas7_3
& MAS3_SR
) {
984 if (tlb
->mas7_3
& MAS3_SW
) {
987 if (tlb
->mas7_3
& MAS3_SX
) {
992 /* Check the address space and permissions */
993 if (access_type
== ACCESS_CODE
) {
994 if (msr_ir
!= ((tlb
->mas1
& MAS1_TS
) >> MAS1_TS_SHIFT
)) {
995 LOG_SWTLB("%s: AS doesn't match\n", __func__
);
1000 if (prot2
& PAGE_EXEC
) {
1001 LOG_SWTLB("%s: good TLB!\n", __func__
);
1005 LOG_SWTLB("%s: no PAGE_EXEC: %x\n", __func__
, prot2
);
1008 if (msr_dr
!= ((tlb
->mas1
& MAS1_TS
) >> MAS1_TS_SHIFT
)) {
1009 LOG_SWTLB("%s: AS doesn't match\n", __func__
);
1014 if ((!rw
&& prot2
& PAGE_READ
) || (rw
&& (prot2
& PAGE_WRITE
))) {
1015 LOG_SWTLB("%s: found TLB!\n", __func__
);
1019 LOG_SWTLB("%s: PAGE_READ/WRITE doesn't match: %x\n", __func__
, prot2
);
1026 static int mmubooke206_get_physical_address(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
1027 target_ulong address
, int rw
,
1035 raddr
= (hwaddr
)-1ULL;
1037 for (i
= 0; i
< BOOKE206_MAX_TLBN
; i
++) {
1038 int ways
= booke206_tlb_ways(env
, i
);
1040 for (j
= 0; j
< ways
; j
++) {
1041 tlb
= booke206_get_tlbm(env
, i
, address
, j
);
1045 ret
= mmubooke206_check_tlb(env
, tlb
, &raddr
, &ctx
->prot
, address
,
1057 LOG_SWTLB("%s: access granted " TARGET_FMT_lx
" => " TARGET_FMT_plx
1058 " %d %d\n", __func__
, address
, ctx
->raddr
, ctx
->prot
,
1061 LOG_SWTLB("%s: access refused " TARGET_FMT_lx
" => " TARGET_FMT_plx
1062 " %d %d\n", __func__
, address
, raddr
, ctx
->prot
, ret
);
1068 static const char *book3e_tsize_to_str
[32] = {
1069 "1K", "2K", "4K", "8K", "16K", "32K", "64K", "128K", "256K", "512K",
1070 "1M", "2M", "4M", "8M", "16M", "32M", "64M", "128M", "256M", "512M",
1071 "1G", "2G", "4G", "8G", "16G", "32G", "64G", "128G", "256G", "512G",
1075 static void mmubooke_dump_mmu(FILE *f
, fprintf_function cpu_fprintf
,
1078 ppcemb_tlb_t
*entry
;
1081 if (kvm_enabled() && !env
->kvm_sw_tlb
) {
1082 cpu_fprintf(f
, "Cannot access KVM TLB\n");
1086 cpu_fprintf(f
, "\nTLB:\n");
1087 cpu_fprintf(f
, "Effective Physical Size PID Prot "
1090 entry
= &env
->tlb
.tlbe
[0];
1091 for (i
= 0; i
< env
->nb_tlb
; i
++, entry
++) {
1094 uint64_t size
= (uint64_t)entry
->size
;
1097 /* Check valid flag */
1098 if (!(entry
->prot
& PAGE_VALID
)) {
1102 mask
= ~(entry
->size
- 1);
1103 ea
= entry
->EPN
& mask
;
1104 pa
= entry
->RPN
& mask
;
1105 /* Extend the physical address to 36 bits */
1106 pa
|= (hwaddr
)(entry
->RPN
& 0xF) << 32;
1109 snprintf(size_buf
, sizeof(size_buf
), "%3" PRId64
"M", size
/ 1024);
1111 snprintf(size_buf
, sizeof(size_buf
), "%3" PRId64
"k", size
);
1113 cpu_fprintf(f
, "0x%016" PRIx64
" 0x%016" PRIx64
" %s %-5u %08x %08x\n",
1114 (uint64_t)ea
, (uint64_t)pa
, size_buf
, (uint32_t)entry
->PID
,
1115 entry
->prot
, entry
->attr
);
1120 static void mmubooke206_dump_one_tlb(FILE *f
, fprintf_function cpu_fprintf
,
1121 CPUPPCState
*env
, int tlbn
, int offset
,
1124 ppcmas_tlb_t
*entry
;
1127 cpu_fprintf(f
, "\nTLB%d:\n", tlbn
);
1128 cpu_fprintf(f
, "Effective Physical Size TID TS SRWX"
1129 " URWX WIMGE U0123\n");
1131 entry
= &env
->tlb
.tlbm
[offset
];
1132 for (i
= 0; i
< tlbsize
; i
++, entry
++) {
1133 hwaddr ea
, pa
, size
;
1136 if (!(entry
->mas1
& MAS1_VALID
)) {
1140 tsize
= (entry
->mas1
& MAS1_TSIZE_MASK
) >> MAS1_TSIZE_SHIFT
;
1141 size
= 1024ULL << tsize
;
1142 ea
= entry
->mas2
& ~(size
- 1);
1143 pa
= entry
->mas7_3
& ~(size
- 1);
1145 cpu_fprintf(f
, "0x%016" PRIx64
" 0x%016" PRIx64
" %4s %-5u %1u S%c%c%c"
1146 "U%c%c%c %c%c%c%c%c U%c%c%c%c\n",
1147 (uint64_t)ea
, (uint64_t)pa
,
1148 book3e_tsize_to_str
[tsize
],
1149 (entry
->mas1
& MAS1_TID_MASK
) >> MAS1_TID_SHIFT
,
1150 (entry
->mas1
& MAS1_TS
) >> MAS1_TS_SHIFT
,
1151 entry
->mas7_3
& MAS3_SR
? 'R' : '-',
1152 entry
->mas7_3
& MAS3_SW
? 'W' : '-',
1153 entry
->mas7_3
& MAS3_SX
? 'X' : '-',
1154 entry
->mas7_3
& MAS3_UR
? 'R' : '-',
1155 entry
->mas7_3
& MAS3_UW
? 'W' : '-',
1156 entry
->mas7_3
& MAS3_UX
? 'X' : '-',
1157 entry
->mas2
& MAS2_W
? 'W' : '-',
1158 entry
->mas2
& MAS2_I
? 'I' : '-',
1159 entry
->mas2
& MAS2_M
? 'M' : '-',
1160 entry
->mas2
& MAS2_G
? 'G' : '-',
1161 entry
->mas2
& MAS2_E
? 'E' : '-',
1162 entry
->mas7_3
& MAS3_U0
? '0' : '-',
1163 entry
->mas7_3
& MAS3_U1
? '1' : '-',
1164 entry
->mas7_3
& MAS3_U2
? '2' : '-',
1165 entry
->mas7_3
& MAS3_U3
? '3' : '-');
1169 static void mmubooke206_dump_mmu(FILE *f
, fprintf_function cpu_fprintf
,
1175 if (kvm_enabled() && !env
->kvm_sw_tlb
) {
1176 cpu_fprintf(f
, "Cannot access KVM TLB\n");
1180 for (i
= 0; i
< BOOKE206_MAX_TLBN
; i
++) {
1181 int size
= booke206_tlb_size(env
, i
);
1187 mmubooke206_dump_one_tlb(f
, cpu_fprintf
, env
, i
, offset
, size
);
1192 static void mmu6xx_dump_BATs(FILE *f
, fprintf_function cpu_fprintf
,
1193 CPUPPCState
*env
, int type
)
1195 target_ulong
*BATlt
, *BATut
, *BATu
, *BATl
;
1196 target_ulong BEPIl
, BEPIu
, bl
;
1201 BATlt
= env
->IBAT
[1];
1202 BATut
= env
->IBAT
[0];
1205 BATlt
= env
->DBAT
[1];
1206 BATut
= env
->DBAT
[0];
1210 for (i
= 0; i
< env
->nb_BATs
; i
++) {
1213 BEPIu
= *BATu
& 0xF0000000;
1214 BEPIl
= *BATu
& 0x0FFE0000;
1215 bl
= (*BATu
& 0x00001FFC) << 15;
1216 cpu_fprintf(f
, "%s BAT%d BATu " TARGET_FMT_lx
1217 " BATl " TARGET_FMT_lx
"\n\t" TARGET_FMT_lx
" "
1218 TARGET_FMT_lx
" " TARGET_FMT_lx
"\n",
1219 type
== ACCESS_CODE
? "code" : "data", i
,
1220 *BATu
, *BATl
, BEPIu
, BEPIl
, bl
);
1224 static void mmu6xx_dump_mmu(FILE *f
, fprintf_function cpu_fprintf
,
1229 int type
, way
, entry
, i
;
1231 cpu_fprintf(f
, "HTAB base = 0x%"HWADDR_PRIx
"\n", env
->htab_base
);
1232 cpu_fprintf(f
, "HTAB mask = 0x%"HWADDR_PRIx
"\n", env
->htab_mask
);
1234 cpu_fprintf(f
, "\nSegment registers:\n");
1235 for (i
= 0; i
< 32; i
++) {
1237 if (sr
& 0x80000000) {
1238 cpu_fprintf(f
, "%02d T=%d Ks=%d Kp=%d BUID=0x%03x "
1239 "CNTLR_SPEC=0x%05x\n", i
,
1240 sr
& 0x80000000 ? 1 : 0, sr
& 0x40000000 ? 1 : 0,
1241 sr
& 0x20000000 ? 1 : 0, (uint32_t)((sr
>> 20) & 0x1FF),
1242 (uint32_t)(sr
& 0xFFFFF));
1244 cpu_fprintf(f
, "%02d T=%d Ks=%d Kp=%d N=%d VSID=0x%06x\n", i
,
1245 sr
& 0x80000000 ? 1 : 0, sr
& 0x40000000 ? 1 : 0,
1246 sr
& 0x20000000 ? 1 : 0, sr
& 0x10000000 ? 1 : 0,
1247 (uint32_t)(sr
& 0x00FFFFFF));
1251 cpu_fprintf(f
, "\nBATs:\n");
1252 mmu6xx_dump_BATs(f
, cpu_fprintf
, env
, ACCESS_INT
);
1253 mmu6xx_dump_BATs(f
, cpu_fprintf
, env
, ACCESS_CODE
);
1255 if (env
->id_tlbs
!= 1) {
1256 cpu_fprintf(f
, "ERROR: 6xx MMU should have separated TLB"
1257 " for code and data\n");
1260 cpu_fprintf(f
, "\nTLBs [EPN EPN + SIZE]\n");
1262 for (type
= 0; type
< 2; type
++) {
1263 for (way
= 0; way
< env
->nb_ways
; way
++) {
1264 for (entry
= env
->nb_tlb
* type
+ env
->tlb_per_way
* way
;
1265 entry
< (env
->nb_tlb
* type
+ env
->tlb_per_way
* (way
+ 1));
1268 tlb
= &env
->tlb
.tlb6
[entry
];
1269 cpu_fprintf(f
, "%s TLB %02d/%02d way:%d %s ["
1270 TARGET_FMT_lx
" " TARGET_FMT_lx
"]\n",
1271 type
? "code" : "data", entry
% env
->nb_tlb
,
1273 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
1274 tlb
->EPN
, tlb
->EPN
+ TARGET_PAGE_SIZE
);
1280 void dump_mmu(FILE *f
, fprintf_function cpu_fprintf
, CPUPPCState
*env
)
1282 switch (env
->mmu_model
) {
1283 case POWERPC_MMU_BOOKE
:
1284 mmubooke_dump_mmu(f
, cpu_fprintf
, env
);
1286 case POWERPC_MMU_BOOKE206
:
1287 mmubooke206_dump_mmu(f
, cpu_fprintf
, env
);
1289 case POWERPC_MMU_SOFT_6xx
:
1290 case POWERPC_MMU_SOFT_74xx
:
1291 mmu6xx_dump_mmu(f
, cpu_fprintf
, env
);
1293 #if defined(TARGET_PPC64)
1294 case POWERPC_MMU_64B
:
1295 case POWERPC_MMU_2_03
:
1296 case POWERPC_MMU_2_06
:
1297 case POWERPC_MMU_2_06a
:
1298 case POWERPC_MMU_2_07
:
1299 case POWERPC_MMU_2_07a
:
1300 dump_slb(f
, cpu_fprintf
, env
);
1304 qemu_log_mask(LOG_UNIMP
, "%s: unimplemented\n", __func__
);
1308 static inline int check_physical(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
1309 target_ulong eaddr
, int rw
)
1314 ctx
->prot
= PAGE_READ
| PAGE_EXEC
;
1316 switch (env
->mmu_model
) {
1317 case POWERPC_MMU_SOFT_6xx
:
1318 case POWERPC_MMU_SOFT_74xx
:
1319 case POWERPC_MMU_SOFT_4xx
:
1320 case POWERPC_MMU_REAL
:
1321 case POWERPC_MMU_BOOKE
:
1322 ctx
->prot
|= PAGE_WRITE
;
1325 case POWERPC_MMU_SOFT_4xx_Z
:
1326 if (unlikely(msr_pe
!= 0)) {
1327 /* 403 family add some particular protections,
1328 * using PBL/PBU registers for accesses with no translation.
1331 /* Check PLB validity */
1332 (env
->pb
[0] < env
->pb
[1] &&
1333 /* and address in plb area */
1334 eaddr
>= env
->pb
[0] && eaddr
< env
->pb
[1]) ||
1335 (env
->pb
[2] < env
->pb
[3] &&
1336 eaddr
>= env
->pb
[2] && eaddr
< env
->pb
[3]) ? 1 : 0;
1337 if (in_plb
^ msr_px
) {
1338 /* Access in protected area */
1340 /* Access is not allowed */
1344 /* Read-write access is allowed */
1345 ctx
->prot
|= PAGE_WRITE
;
1351 /* Caller's checks mean we should never get here for other models */
1359 static int get_physical_address(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
1360 target_ulong eaddr
, int rw
, int access_type
)
1362 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1364 bool real_mode
= (access_type
== ACCESS_CODE
&& msr_ir
== 0)
1365 || (access_type
!= ACCESS_CODE
&& msr_dr
== 0);
1368 qemu_log("%s\n", __func__
);
1371 switch (env
->mmu_model
) {
1372 case POWERPC_MMU_SOFT_6xx
:
1373 case POWERPC_MMU_SOFT_74xx
:
1375 ret
= check_physical(env
, ctx
, eaddr
, rw
);
1377 /* Try to find a BAT */
1378 if (env
->nb_BATs
!= 0) {
1379 ret
= get_bat_6xx_tlb(env
, ctx
, eaddr
, rw
, access_type
);
1382 /* We didn't match any BAT entry or don't have BATs */
1383 ret
= get_segment_6xx_tlb(env
, ctx
, eaddr
, rw
, access_type
);
1388 case POWERPC_MMU_SOFT_4xx
:
1389 case POWERPC_MMU_SOFT_4xx_Z
:
1391 ret
= check_physical(env
, ctx
, eaddr
, rw
);
1393 ret
= mmu40x_get_physical_address(env
, ctx
, eaddr
,
1397 case POWERPC_MMU_BOOKE
:
1398 ret
= mmubooke_get_physical_address(env
, ctx
, eaddr
,
1401 case POWERPC_MMU_BOOKE206
:
1402 ret
= mmubooke206_get_physical_address(env
, ctx
, eaddr
, rw
,
1405 case POWERPC_MMU_MPC8xx
:
1407 cpu_abort(CPU(cpu
), "MPC8xx MMU model is not implemented\n");
1409 case POWERPC_MMU_REAL
:
1411 ret
= check_physical(env
, ctx
, eaddr
, rw
);
1413 cpu_abort(CPU(cpu
), "PowerPC in real mode do not do any translation\n");
1417 cpu_abort(CPU(cpu
), "Unknown or invalid MMU model\n");
1421 qemu_log("%s address " TARGET_FMT_lx
" => %d " TARGET_FMT_plx
"\n",
1422 __func__
, eaddr
, ret
, ctx
->raddr
);
1428 hwaddr
ppc_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
1430 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
1431 CPUPPCState
*env
= &cpu
->env
;
1434 switch (env
->mmu_model
) {
1435 #if defined(TARGET_PPC64)
1436 case POWERPC_MMU_64B
:
1437 case POWERPC_MMU_2_03
:
1438 case POWERPC_MMU_2_06
:
1439 case POWERPC_MMU_2_06a
:
1440 case POWERPC_MMU_2_07
:
1441 case POWERPC_MMU_2_07a
:
1442 return ppc_hash64_get_phys_page_debug(env
, addr
);
1445 case POWERPC_MMU_32B
:
1446 case POWERPC_MMU_601
:
1447 return ppc_hash32_get_phys_page_debug(env
, addr
);
1453 if (unlikely(get_physical_address(env
, &ctx
, addr
, 0, ACCESS_INT
) != 0)) {
1455 /* Some MMUs have separate TLBs for code and data. If we only try an
1456 * ACCESS_INT, we may not be able to read instructions mapped by code
1457 * TLBs, so we also try a ACCESS_CODE.
1459 if (unlikely(get_physical_address(env
, &ctx
, addr
, 0,
1460 ACCESS_CODE
) != 0)) {
1465 return ctx
.raddr
& TARGET_PAGE_MASK
;
1468 static void booke206_update_mas_tlb_miss(CPUPPCState
*env
, target_ulong address
,
1471 env
->spr
[SPR_BOOKE_MAS0
] = env
->spr
[SPR_BOOKE_MAS4
] & MAS4_TLBSELD_MASK
;
1472 env
->spr
[SPR_BOOKE_MAS1
] = env
->spr
[SPR_BOOKE_MAS4
] & MAS4_TSIZED_MASK
;
1473 env
->spr
[SPR_BOOKE_MAS2
] = env
->spr
[SPR_BOOKE_MAS4
] & MAS4_WIMGED_MASK
;
1474 env
->spr
[SPR_BOOKE_MAS3
] = 0;
1475 env
->spr
[SPR_BOOKE_MAS6
] = 0;
1476 env
->spr
[SPR_BOOKE_MAS7
] = 0;
1479 if (((rw
== 2) && msr_ir
) || ((rw
!= 2) && msr_dr
)) {
1480 env
->spr
[SPR_BOOKE_MAS1
] |= MAS1_TS
;
1481 env
->spr
[SPR_BOOKE_MAS6
] |= MAS6_SAS
;
1484 env
->spr
[SPR_BOOKE_MAS1
] |= MAS1_VALID
;
1485 env
->spr
[SPR_BOOKE_MAS2
] |= address
& MAS2_EPN_MASK
;
1487 switch (env
->spr
[SPR_BOOKE_MAS4
] & MAS4_TIDSELD_PIDZ
) {
1488 case MAS4_TIDSELD_PID0
:
1489 env
->spr
[SPR_BOOKE_MAS1
] |= env
->spr
[SPR_BOOKE_PID
] << MAS1_TID_SHIFT
;
1491 case MAS4_TIDSELD_PID1
:
1492 env
->spr
[SPR_BOOKE_MAS1
] |= env
->spr
[SPR_BOOKE_PID1
] << MAS1_TID_SHIFT
;
1494 case MAS4_TIDSELD_PID2
:
1495 env
->spr
[SPR_BOOKE_MAS1
] |= env
->spr
[SPR_BOOKE_PID2
] << MAS1_TID_SHIFT
;
1499 env
->spr
[SPR_BOOKE_MAS6
] |= env
->spr
[SPR_BOOKE_PID
] << 16;
1501 /* next victim logic */
1502 env
->spr
[SPR_BOOKE_MAS0
] |= env
->last_way
<< MAS0_ESEL_SHIFT
;
1504 env
->last_way
&= booke206_tlb_ways(env
, 0) - 1;
1505 env
->spr
[SPR_BOOKE_MAS0
] |= env
->last_way
<< MAS0_NV_SHIFT
;
1508 /* Perform address translation */
1509 static int cpu_ppc_handle_mmu_fault(CPUPPCState
*env
, target_ulong address
,
1510 int rw
, int mmu_idx
)
1512 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
1520 access_type
= ACCESS_CODE
;
1523 access_type
= env
->access_type
;
1525 ret
= get_physical_address(env
, &ctx
, address
, rw
, access_type
);
1527 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
,
1528 ctx
.raddr
& TARGET_PAGE_MASK
, ctx
.prot
,
1529 mmu_idx
, TARGET_PAGE_SIZE
);
1531 } else if (ret
< 0) {
1533 if (access_type
== ACCESS_CODE
) {
1536 /* No matches in page tables or TLB */
1537 switch (env
->mmu_model
) {
1538 case POWERPC_MMU_SOFT_6xx
:
1539 cs
->exception_index
= POWERPC_EXCP_IFTLB
;
1540 env
->error_code
= 1 << 18;
1541 env
->spr
[SPR_IMISS
] = address
;
1542 env
->spr
[SPR_ICMP
] = 0x80000000 | ctx
.ptem
;
1544 case POWERPC_MMU_SOFT_74xx
:
1545 cs
->exception_index
= POWERPC_EXCP_IFTLB
;
1547 case POWERPC_MMU_SOFT_4xx
:
1548 case POWERPC_MMU_SOFT_4xx_Z
:
1549 cs
->exception_index
= POWERPC_EXCP_ITLB
;
1550 env
->error_code
= 0;
1551 env
->spr
[SPR_40x_DEAR
] = address
;
1552 env
->spr
[SPR_40x_ESR
] = 0x00000000;
1554 case POWERPC_MMU_BOOKE206
:
1555 booke206_update_mas_tlb_miss(env
, address
, rw
);
1557 case POWERPC_MMU_BOOKE
:
1558 cs
->exception_index
= POWERPC_EXCP_ITLB
;
1559 env
->error_code
= 0;
1560 env
->spr
[SPR_BOOKE_DEAR
] = address
;
1562 case POWERPC_MMU_MPC8xx
:
1564 cpu_abort(cs
, "MPC8xx MMU model is not implemented\n");
1566 case POWERPC_MMU_REAL
:
1567 cpu_abort(cs
, "PowerPC in real mode should never raise "
1568 "any MMU exceptions\n");
1571 cpu_abort(cs
, "Unknown or invalid MMU model\n");
1576 /* Access rights violation */
1577 cs
->exception_index
= POWERPC_EXCP_ISI
;
1578 env
->error_code
= 0x08000000;
1581 /* No execute protection violation */
1582 if ((env
->mmu_model
== POWERPC_MMU_BOOKE
) ||
1583 (env
->mmu_model
== POWERPC_MMU_BOOKE206
)) {
1584 env
->spr
[SPR_BOOKE_ESR
] = 0x00000000;
1586 cs
->exception_index
= POWERPC_EXCP_ISI
;
1587 env
->error_code
= 0x10000000;
1590 /* Direct store exception */
1591 /* No code fetch is allowed in direct-store areas */
1592 cs
->exception_index
= POWERPC_EXCP_ISI
;
1593 env
->error_code
= 0x10000000;
1599 /* No matches in page tables or TLB */
1600 switch (env
->mmu_model
) {
1601 case POWERPC_MMU_SOFT_6xx
:
1603 cs
->exception_index
= POWERPC_EXCP_DSTLB
;
1604 env
->error_code
= 1 << 16;
1606 cs
->exception_index
= POWERPC_EXCP_DLTLB
;
1607 env
->error_code
= 0;
1609 env
->spr
[SPR_DMISS
] = address
;
1610 env
->spr
[SPR_DCMP
] = 0x80000000 | ctx
.ptem
;
1612 env
->error_code
|= ctx
.key
<< 19;
1613 env
->spr
[SPR_HASH1
] = env
->htab_base
+
1614 get_pteg_offset32(env
, ctx
.hash
[0]);
1615 env
->spr
[SPR_HASH2
] = env
->htab_base
+
1616 get_pteg_offset32(env
, ctx
.hash
[1]);
1618 case POWERPC_MMU_SOFT_74xx
:
1620 cs
->exception_index
= POWERPC_EXCP_DSTLB
;
1622 cs
->exception_index
= POWERPC_EXCP_DLTLB
;
1625 /* Implement LRU algorithm */
1626 env
->error_code
= ctx
.key
<< 19;
1627 env
->spr
[SPR_TLBMISS
] = (address
& ~((target_ulong
)0x3)) |
1628 ((env
->last_way
+ 1) & (env
->nb_ways
- 1));
1629 env
->spr
[SPR_PTEHI
] = 0x80000000 | ctx
.ptem
;
1631 case POWERPC_MMU_SOFT_4xx
:
1632 case POWERPC_MMU_SOFT_4xx_Z
:
1633 cs
->exception_index
= POWERPC_EXCP_DTLB
;
1634 env
->error_code
= 0;
1635 env
->spr
[SPR_40x_DEAR
] = address
;
1637 env
->spr
[SPR_40x_ESR
] = 0x00800000;
1639 env
->spr
[SPR_40x_ESR
] = 0x00000000;
1642 case POWERPC_MMU_MPC8xx
:
1644 cpu_abort(cs
, "MPC8xx MMU model is not implemented\n");
1646 case POWERPC_MMU_BOOKE206
:
1647 booke206_update_mas_tlb_miss(env
, address
, rw
);
1649 case POWERPC_MMU_BOOKE
:
1650 cs
->exception_index
= POWERPC_EXCP_DTLB
;
1651 env
->error_code
= 0;
1652 env
->spr
[SPR_BOOKE_DEAR
] = address
;
1653 env
->spr
[SPR_BOOKE_ESR
] = rw
? ESR_ST
: 0;
1655 case POWERPC_MMU_REAL
:
1656 cpu_abort(cs
, "PowerPC in real mode should never raise "
1657 "any MMU exceptions\n");
1660 cpu_abort(cs
, "Unknown or invalid MMU model\n");
1665 /* Access rights violation */
1666 cs
->exception_index
= POWERPC_EXCP_DSI
;
1667 env
->error_code
= 0;
1668 if (env
->mmu_model
== POWERPC_MMU_SOFT_4xx
1669 || env
->mmu_model
== POWERPC_MMU_SOFT_4xx_Z
) {
1670 env
->spr
[SPR_40x_DEAR
] = address
;
1672 env
->spr
[SPR_40x_ESR
] |= 0x00800000;
1674 } else if ((env
->mmu_model
== POWERPC_MMU_BOOKE
) ||
1675 (env
->mmu_model
== POWERPC_MMU_BOOKE206
)) {
1676 env
->spr
[SPR_BOOKE_DEAR
] = address
;
1677 env
->spr
[SPR_BOOKE_ESR
] = rw
? ESR_ST
: 0;
1679 env
->spr
[SPR_DAR
] = address
;
1681 env
->spr
[SPR_DSISR
] = 0x0A000000;
1683 env
->spr
[SPR_DSISR
] = 0x08000000;
1688 /* Direct store exception */
1689 switch (access_type
) {
1691 /* Floating point load/store */
1692 cs
->exception_index
= POWERPC_EXCP_ALIGN
;
1693 env
->error_code
= POWERPC_EXCP_ALIGN_FP
;
1694 env
->spr
[SPR_DAR
] = address
;
1697 /* lwarx, ldarx or stwcx. */
1698 cs
->exception_index
= POWERPC_EXCP_DSI
;
1699 env
->error_code
= 0;
1700 env
->spr
[SPR_DAR
] = address
;
1702 env
->spr
[SPR_DSISR
] = 0x06000000;
1704 env
->spr
[SPR_DSISR
] = 0x04000000;
1708 /* eciwx or ecowx */
1709 cs
->exception_index
= POWERPC_EXCP_DSI
;
1710 env
->error_code
= 0;
1711 env
->spr
[SPR_DAR
] = address
;
1713 env
->spr
[SPR_DSISR
] = 0x06100000;
1715 env
->spr
[SPR_DSISR
] = 0x04100000;
1719 printf("DSI: invalid exception (%d)\n", ret
);
1720 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
1722 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
;
1723 env
->spr
[SPR_DAR
] = address
;
1730 printf("%s: set exception to %d %02x\n", __func__
,
1731 cs
->exception
, env
->error_code
);
1739 /*****************************************************************************/
1740 /* BATs management */
1741 #if !defined(FLUSH_ALL_TLBS)
1742 static inline void do_invalidate_BAT(CPUPPCState
*env
, target_ulong BATu
,
1745 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
1746 target_ulong base
, end
, page
;
1748 base
= BATu
& ~0x0001FFFF;
1749 end
= base
+ mask
+ 0x00020000;
1750 LOG_BATS("Flush BAT from " TARGET_FMT_lx
" to " TARGET_FMT_lx
" ("
1751 TARGET_FMT_lx
")\n", base
, end
, mask
);
1752 for (page
= base
; page
!= end
; page
+= TARGET_PAGE_SIZE
) {
1753 tlb_flush_page(cs
, page
);
1755 LOG_BATS("Flush done\n");
1759 static inline void dump_store_bat(CPUPPCState
*env
, char ID
, int ul
, int nr
,
1762 LOG_BATS("Set %cBAT%d%c to " TARGET_FMT_lx
" (" TARGET_FMT_lx
")\n", ID
,
1763 nr
, ul
== 0 ? 'u' : 'l', value
, env
->nip
);
1766 void helper_store_ibatu(CPUPPCState
*env
, uint32_t nr
, target_ulong value
)
1770 dump_store_bat(env
, 'I', 0, nr
, value
);
1771 if (env
->IBAT
[0][nr
] != value
) {
1772 mask
= (value
<< 15) & 0x0FFE0000UL
;
1773 #if !defined(FLUSH_ALL_TLBS)
1774 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1776 /* When storing valid upper BAT, mask BEPI and BRPN
1777 * and invalidate all TLBs covered by this BAT
1779 mask
= (value
<< 15) & 0x0FFE0000UL
;
1780 env
->IBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1781 (value
& ~0x0001FFFFUL
& ~mask
);
1782 env
->IBAT
[1][nr
] = (env
->IBAT
[1][nr
] & 0x0000007B) |
1783 (env
->IBAT
[1][nr
] & ~0x0001FFFF & ~mask
);
1784 #if !defined(FLUSH_ALL_TLBS)
1785 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1792 void helper_store_ibatl(CPUPPCState
*env
, uint32_t nr
, target_ulong value
)
1794 dump_store_bat(env
, 'I', 1, nr
, value
);
1795 env
->IBAT
[1][nr
] = value
;
1798 void helper_store_dbatu(CPUPPCState
*env
, uint32_t nr
, target_ulong value
)
1802 dump_store_bat(env
, 'D', 0, nr
, value
);
1803 if (env
->DBAT
[0][nr
] != value
) {
1804 /* When storing valid upper BAT, mask BEPI and BRPN
1805 * and invalidate all TLBs covered by this BAT
1807 mask
= (value
<< 15) & 0x0FFE0000UL
;
1808 #if !defined(FLUSH_ALL_TLBS)
1809 do_invalidate_BAT(env
, env
->DBAT
[0][nr
], mask
);
1811 mask
= (value
<< 15) & 0x0FFE0000UL
;
1812 env
->DBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1813 (value
& ~0x0001FFFFUL
& ~mask
);
1814 env
->DBAT
[1][nr
] = (env
->DBAT
[1][nr
] & 0x0000007B) |
1815 (env
->DBAT
[1][nr
] & ~0x0001FFFF & ~mask
);
1816 #if !defined(FLUSH_ALL_TLBS)
1817 do_invalidate_BAT(env
, env
->DBAT
[0][nr
], mask
);
1824 void helper_store_dbatl(CPUPPCState
*env
, uint32_t nr
, target_ulong value
)
1826 dump_store_bat(env
, 'D', 1, nr
, value
);
1827 env
->DBAT
[1][nr
] = value
;
1830 void helper_store_601_batu(CPUPPCState
*env
, uint32_t nr
, target_ulong value
)
1833 #if defined(FLUSH_ALL_TLBS)
1837 dump_store_bat(env
, 'I', 0, nr
, value
);
1838 if (env
->IBAT
[0][nr
] != value
) {
1839 #if defined(FLUSH_ALL_TLBS)
1842 mask
= (env
->IBAT
[1][nr
] << 17) & 0x0FFE0000UL
;
1843 if (env
->IBAT
[1][nr
] & 0x40) {
1844 /* Invalidate BAT only if it is valid */
1845 #if !defined(FLUSH_ALL_TLBS)
1846 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1851 /* When storing valid upper BAT, mask BEPI and BRPN
1852 * and invalidate all TLBs covered by this BAT
1854 env
->IBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1855 (value
& ~0x0001FFFFUL
& ~mask
);
1856 env
->DBAT
[0][nr
] = env
->IBAT
[0][nr
];
1857 if (env
->IBAT
[1][nr
] & 0x40) {
1858 #if !defined(FLUSH_ALL_TLBS)
1859 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1864 #if defined(FLUSH_ALL_TLBS)
1872 void helper_store_601_batl(CPUPPCState
*env
, uint32_t nr
, target_ulong value
)
1874 #if !defined(FLUSH_ALL_TLBS)
1880 dump_store_bat(env
, 'I', 1, nr
, value
);
1881 if (env
->IBAT
[1][nr
] != value
) {
1882 #if defined(FLUSH_ALL_TLBS)
1885 if (env
->IBAT
[1][nr
] & 0x40) {
1886 #if !defined(FLUSH_ALL_TLBS)
1887 mask
= (env
->IBAT
[1][nr
] << 17) & 0x0FFE0000UL
;
1888 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1894 #if !defined(FLUSH_ALL_TLBS)
1895 mask
= (value
<< 17) & 0x0FFE0000UL
;
1896 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1901 env
->IBAT
[1][nr
] = value
;
1902 env
->DBAT
[1][nr
] = value
;
1903 #if defined(FLUSH_ALL_TLBS)
1911 /*****************************************************************************/
1912 /* TLB management */
1913 void ppc_tlb_invalidate_all(CPUPPCState
*env
)
1915 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1917 switch (env
->mmu_model
) {
1918 case POWERPC_MMU_SOFT_6xx
:
1919 case POWERPC_MMU_SOFT_74xx
:
1920 ppc6xx_tlb_invalidate_all(env
);
1922 case POWERPC_MMU_SOFT_4xx
:
1923 case POWERPC_MMU_SOFT_4xx_Z
:
1924 ppc4xx_tlb_invalidate_all(env
);
1926 case POWERPC_MMU_REAL
:
1927 cpu_abort(CPU(cpu
), "No TLB for PowerPC 4xx in real mode\n");
1929 case POWERPC_MMU_MPC8xx
:
1931 cpu_abort(CPU(cpu
), "MPC8xx MMU model is not implemented\n");
1933 case POWERPC_MMU_BOOKE
:
1934 tlb_flush(CPU(cpu
), 1);
1936 case POWERPC_MMU_BOOKE206
:
1937 booke206_flush_tlb(env
, -1, 0);
1939 case POWERPC_MMU_32B
:
1940 case POWERPC_MMU_601
:
1941 #if defined(TARGET_PPC64)
1942 case POWERPC_MMU_64B
:
1943 case POWERPC_MMU_2_03
:
1944 case POWERPC_MMU_2_06
:
1945 case POWERPC_MMU_2_06a
:
1946 case POWERPC_MMU_2_07
:
1947 case POWERPC_MMU_2_07a
:
1948 #endif /* defined(TARGET_PPC64) */
1949 tlb_flush(CPU(cpu
), 1);
1953 cpu_abort(CPU(cpu
), "Unknown MMU model\n");
1958 void ppc_tlb_invalidate_one(CPUPPCState
*env
, target_ulong addr
)
1960 #if !defined(FLUSH_ALL_TLBS)
1961 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1964 addr
&= TARGET_PAGE_MASK
;
1965 switch (env
->mmu_model
) {
1966 case POWERPC_MMU_SOFT_6xx
:
1967 case POWERPC_MMU_SOFT_74xx
:
1968 ppc6xx_tlb_invalidate_virt(env
, addr
, 0);
1969 if (env
->id_tlbs
== 1) {
1970 ppc6xx_tlb_invalidate_virt(env
, addr
, 1);
1973 case POWERPC_MMU_SOFT_4xx
:
1974 case POWERPC_MMU_SOFT_4xx_Z
:
1975 ppc4xx_tlb_invalidate_virt(env
, addr
, env
->spr
[SPR_40x_PID
]);
1977 case POWERPC_MMU_REAL
:
1978 cpu_abort(CPU(cpu
), "No TLB for PowerPC 4xx in real mode\n");
1980 case POWERPC_MMU_MPC8xx
:
1982 cpu_abort(CPU(cpu
), "MPC8xx MMU model is not implemented\n");
1984 case POWERPC_MMU_BOOKE
:
1986 cpu_abort(CPU(cpu
), "BookE MMU model is not implemented\n");
1988 case POWERPC_MMU_BOOKE206
:
1990 cpu_abort(CPU(cpu
), "BookE 2.06 MMU model is not implemented\n");
1992 case POWERPC_MMU_32B
:
1993 case POWERPC_MMU_601
:
1994 /* tlbie invalidate TLBs for all segments */
1995 addr
&= ~((target_ulong
)-1ULL << 28);
1997 /* XXX: this case should be optimized,
1998 * giving a mask to tlb_flush_page
2000 tlb_flush_page(cs
, addr
| (0x0 << 28));
2001 tlb_flush_page(cs
, addr
| (0x1 << 28));
2002 tlb_flush_page(cs
, addr
| (0x2 << 28));
2003 tlb_flush_page(cs
, addr
| (0x3 << 28));
2004 tlb_flush_page(cs
, addr
| (0x4 << 28));
2005 tlb_flush_page(cs
, addr
| (0x5 << 28));
2006 tlb_flush_page(cs
, addr
| (0x6 << 28));
2007 tlb_flush_page(cs
, addr
| (0x7 << 28));
2008 tlb_flush_page(cs
, addr
| (0x8 << 28));
2009 tlb_flush_page(cs
, addr
| (0x9 << 28));
2010 tlb_flush_page(cs
, addr
| (0xA << 28));
2011 tlb_flush_page(cs
, addr
| (0xB << 28));
2012 tlb_flush_page(cs
, addr
| (0xC << 28));
2013 tlb_flush_page(cs
, addr
| (0xD << 28));
2014 tlb_flush_page(cs
, addr
| (0xE << 28));
2015 tlb_flush_page(cs
, addr
| (0xF << 28));
2017 #if defined(TARGET_PPC64)
2018 case POWERPC_MMU_64B
:
2019 case POWERPC_MMU_2_03
:
2020 case POWERPC_MMU_2_06
:
2021 case POWERPC_MMU_2_06a
:
2022 case POWERPC_MMU_2_07
:
2023 case POWERPC_MMU_2_07a
:
2024 /* tlbie invalidate TLBs for all segments */
2025 /* XXX: given the fact that there are too many segments to invalidate,
2026 * and we still don't have a tlb_flush_mask(env, n, mask) in QEMU,
2027 * we just invalidate all TLBs
2029 tlb_flush(CPU(cpu
), 1);
2031 #endif /* defined(TARGET_PPC64) */
2034 cpu_abort(CPU(cpu
), "Unknown MMU model\n");
2038 ppc_tlb_invalidate_all(env
);
2042 /*****************************************************************************/
2043 /* Special registers manipulation */
2044 void ppc_store_sdr1(CPUPPCState
*env
, target_ulong value
)
2046 qemu_log_mask(CPU_LOG_MMU
, "%s: " TARGET_FMT_lx
"\n", __func__
, value
);
2047 assert(!env
->external_htab
);
2048 env
->spr
[SPR_SDR1
] = value
;
2049 #if defined(TARGET_PPC64)
2050 if (env
->mmu_model
& POWERPC_MMU_64
) {
2051 target_ulong htabsize
= value
& SDR_64_HTABSIZE
;
2053 if (htabsize
> 28) {
2054 fprintf(stderr
, "Invalid HTABSIZE 0x" TARGET_FMT_lx
2055 " stored in SDR1\n", htabsize
);
2058 env
->htab_mask
= (1ULL << (htabsize
+ 18 - 7)) - 1;
2059 env
->htab_base
= value
& SDR_64_HTABORG
;
2061 #endif /* defined(TARGET_PPC64) */
2063 /* FIXME: Should check for valid HTABMASK values */
2064 env
->htab_mask
= ((value
& SDR_32_HTABMASK
) << 16) | 0xFFFF;
2065 env
->htab_base
= value
& SDR_32_HTABORG
;
2069 /* Segment registers load and store */
2070 target_ulong
helper_load_sr(CPUPPCState
*env
, target_ulong sr_num
)
2072 #if defined(TARGET_PPC64)
2073 if (env
->mmu_model
& POWERPC_MMU_64
) {
2078 return env
->sr
[sr_num
];
2081 void helper_store_sr(CPUPPCState
*env
, target_ulong srnum
, target_ulong value
)
2083 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2085 qemu_log_mask(CPU_LOG_MMU
,
2086 "%s: reg=%d " TARGET_FMT_lx
" " TARGET_FMT_lx
"\n", __func__
,
2087 (int)srnum
, value
, env
->sr
[srnum
]);
2088 #if defined(TARGET_PPC64)
2089 if (env
->mmu_model
& POWERPC_MMU_64
) {
2090 uint64_t rb
= 0, rs
= 0;
2093 rb
|= ((uint32_t)srnum
& 0xf) << 28;
2094 /* Set the valid bit */
2097 rb
|= (uint32_t)srnum
;
2100 rs
|= (value
& 0xfffffff) << 12;
2102 rs
|= ((value
>> 27) & 0xf) << 8;
2104 ppc_store_slb(env
, rb
, rs
);
2107 if (env
->sr
[srnum
] != value
) {
2108 env
->sr
[srnum
] = value
;
2109 /* Invalidating 256MB of virtual memory in 4kB pages is way longer than
2110 flusing the whole TLB. */
2111 #if !defined(FLUSH_ALL_TLBS) && 0
2113 target_ulong page
, end
;
2114 /* Invalidate 256 MB of virtual memory */
2115 page
= (16 << 20) * srnum
;
2116 end
= page
+ (16 << 20);
2117 for (; page
!= end
; page
+= TARGET_PAGE_SIZE
) {
2118 tlb_flush_page(CPU(cpu
), page
);
2122 tlb_flush(CPU(cpu
), 1);
2127 /* TLB management */
2128 void helper_tlbia(CPUPPCState
*env
)
2130 ppc_tlb_invalidate_all(env
);
2133 void helper_tlbie(CPUPPCState
*env
, target_ulong addr
)
2135 ppc_tlb_invalidate_one(env
, addr
);
2138 /* Software driven TLBs management */
2139 /* PowerPC 602/603 software TLB load instructions helpers */
2140 static void do_6xx_tlb(CPUPPCState
*env
, target_ulong new_EPN
, int is_code
)
2142 target_ulong RPN
, CMP
, EPN
;
2145 RPN
= env
->spr
[SPR_RPA
];
2147 CMP
= env
->spr
[SPR_ICMP
];
2148 EPN
= env
->spr
[SPR_IMISS
];
2150 CMP
= env
->spr
[SPR_DCMP
];
2151 EPN
= env
->spr
[SPR_DMISS
];
2153 way
= (env
->spr
[SPR_SRR1
] >> 17) & 1;
2154 (void)EPN
; /* avoid a compiler warning */
2155 LOG_SWTLB("%s: EPN " TARGET_FMT_lx
" " TARGET_FMT_lx
" PTE0 " TARGET_FMT_lx
2156 " PTE1 " TARGET_FMT_lx
" way %d\n", __func__
, new_EPN
, EPN
, CMP
,
2158 /* Store this TLB */
2159 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
2160 way
, is_code
, CMP
, RPN
);
2163 void helper_6xx_tlbd(CPUPPCState
*env
, target_ulong EPN
)
2165 do_6xx_tlb(env
, EPN
, 0);
2168 void helper_6xx_tlbi(CPUPPCState
*env
, target_ulong EPN
)
2170 do_6xx_tlb(env
, EPN
, 1);
2173 /* PowerPC 74xx software TLB load instructions helpers */
2174 static void do_74xx_tlb(CPUPPCState
*env
, target_ulong new_EPN
, int is_code
)
2176 target_ulong RPN
, CMP
, EPN
;
2179 RPN
= env
->spr
[SPR_PTELO
];
2180 CMP
= env
->spr
[SPR_PTEHI
];
2181 EPN
= env
->spr
[SPR_TLBMISS
] & ~0x3;
2182 way
= env
->spr
[SPR_TLBMISS
] & 0x3;
2183 (void)EPN
; /* avoid a compiler warning */
2184 LOG_SWTLB("%s: EPN " TARGET_FMT_lx
" " TARGET_FMT_lx
" PTE0 " TARGET_FMT_lx
2185 " PTE1 " TARGET_FMT_lx
" way %d\n", __func__
, new_EPN
, EPN
, CMP
,
2187 /* Store this TLB */
2188 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
2189 way
, is_code
, CMP
, RPN
);
2192 void helper_74xx_tlbd(CPUPPCState
*env
, target_ulong EPN
)
2194 do_74xx_tlb(env
, EPN
, 0);
2197 void helper_74xx_tlbi(CPUPPCState
*env
, target_ulong EPN
)
2199 do_74xx_tlb(env
, EPN
, 1);
2202 /*****************************************************************************/
2203 /* PowerPC 601 specific instructions (POWER bridge) */
2205 target_ulong
helper_rac(CPUPPCState
*env
, target_ulong addr
)
2209 target_ulong ret
= 0;
2211 /* We don't have to generate many instances of this instruction,
2212 * as rac is supervisor only.
2214 /* XXX: FIX THIS: Pretend we have no BAT */
2215 nb_BATs
= env
->nb_BATs
;
2217 if (get_physical_address(env
, &ctx
, addr
, 0, ACCESS_INT
) == 0) {
2220 env
->nb_BATs
= nb_BATs
;
2224 static inline target_ulong
booke_tlb_to_page_size(int size
)
2226 return 1024 << (2 * size
);
2229 static inline int booke_page_size_to_tlb(target_ulong page_size
)
2233 switch (page_size
) {
2267 #if defined(TARGET_PPC64)
2268 case 0x000100000000ULL
:
2271 case 0x000400000000ULL
:
2274 case 0x001000000000ULL
:
2277 case 0x004000000000ULL
:
2280 case 0x010000000000ULL
:
2292 /* Helpers for 4xx TLB management */
2293 #define PPC4XX_TLB_ENTRY_MASK 0x0000003f /* Mask for 64 TLB entries */
2295 #define PPC4XX_TLBHI_V 0x00000040
2296 #define PPC4XX_TLBHI_E 0x00000020
2297 #define PPC4XX_TLBHI_SIZE_MIN 0
2298 #define PPC4XX_TLBHI_SIZE_MAX 7
2299 #define PPC4XX_TLBHI_SIZE_DEFAULT 1
2300 #define PPC4XX_TLBHI_SIZE_SHIFT 7
2301 #define PPC4XX_TLBHI_SIZE_MASK 0x00000007
2303 #define PPC4XX_TLBLO_EX 0x00000200
2304 #define PPC4XX_TLBLO_WR 0x00000100
2305 #define PPC4XX_TLBLO_ATTR_MASK 0x000000FF
2306 #define PPC4XX_TLBLO_RPN_MASK 0xFFFFFC00
2308 target_ulong
helper_4xx_tlbre_hi(CPUPPCState
*env
, target_ulong entry
)
2314 entry
&= PPC4XX_TLB_ENTRY_MASK
;
2315 tlb
= &env
->tlb
.tlbe
[entry
];
2317 if (tlb
->prot
& PAGE_VALID
) {
2318 ret
|= PPC4XX_TLBHI_V
;
2320 size
= booke_page_size_to_tlb(tlb
->size
);
2321 if (size
< PPC4XX_TLBHI_SIZE_MIN
|| size
> PPC4XX_TLBHI_SIZE_MAX
) {
2322 size
= PPC4XX_TLBHI_SIZE_DEFAULT
;
2324 ret
|= size
<< PPC4XX_TLBHI_SIZE_SHIFT
;
2325 env
->spr
[SPR_40x_PID
] = tlb
->PID
;
2329 target_ulong
helper_4xx_tlbre_lo(CPUPPCState
*env
, target_ulong entry
)
2334 entry
&= PPC4XX_TLB_ENTRY_MASK
;
2335 tlb
= &env
->tlb
.tlbe
[entry
];
2337 if (tlb
->prot
& PAGE_EXEC
) {
2338 ret
|= PPC4XX_TLBLO_EX
;
2340 if (tlb
->prot
& PAGE_WRITE
) {
2341 ret
|= PPC4XX_TLBLO_WR
;
2346 void helper_4xx_tlbwe_hi(CPUPPCState
*env
, target_ulong entry
,
2349 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2350 CPUState
*cs
= CPU(cpu
);
2352 target_ulong page
, end
;
2354 LOG_SWTLB("%s entry %d val " TARGET_FMT_lx
"\n", __func__
, (int)entry
,
2356 entry
&= PPC4XX_TLB_ENTRY_MASK
;
2357 tlb
= &env
->tlb
.tlbe
[entry
];
2358 /* Invalidate previous TLB (if it's valid) */
2359 if (tlb
->prot
& PAGE_VALID
) {
2360 end
= tlb
->EPN
+ tlb
->size
;
2361 LOG_SWTLB("%s: invalidate old TLB %d start " TARGET_FMT_lx
" end "
2362 TARGET_FMT_lx
"\n", __func__
, (int)entry
, tlb
->EPN
, end
);
2363 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
) {
2364 tlb_flush_page(cs
, page
);
2367 tlb
->size
= booke_tlb_to_page_size((val
>> PPC4XX_TLBHI_SIZE_SHIFT
)
2368 & PPC4XX_TLBHI_SIZE_MASK
);
2369 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2370 * If this ever occurs, one should use the ppcemb target instead
2371 * of the ppc or ppc64 one
2373 if ((val
& PPC4XX_TLBHI_V
) && tlb
->size
< TARGET_PAGE_SIZE
) {
2374 cpu_abort(cs
, "TLB size " TARGET_FMT_lu
" < %u "
2375 "are not supported (%d)\n",
2376 tlb
->size
, TARGET_PAGE_SIZE
, (int)((val
>> 7) & 0x7));
2378 tlb
->EPN
= val
& ~(tlb
->size
- 1);
2379 if (val
& PPC4XX_TLBHI_V
) {
2380 tlb
->prot
|= PAGE_VALID
;
2381 if (val
& PPC4XX_TLBHI_E
) {
2382 /* XXX: TO BE FIXED */
2384 "Little-endian TLB entries are not supported by now\n");
2387 tlb
->prot
&= ~PAGE_VALID
;
2389 tlb
->PID
= env
->spr
[SPR_40x_PID
]; /* PID */
2390 LOG_SWTLB("%s: set up TLB %d RPN " TARGET_FMT_plx
" EPN " TARGET_FMT_lx
2391 " size " TARGET_FMT_lx
" prot %c%c%c%c PID %d\n", __func__
,
2392 (int)entry
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2393 tlb
->prot
& PAGE_READ
? 'r' : '-',
2394 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2395 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2396 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2397 /* Invalidate new TLB (if valid) */
2398 if (tlb
->prot
& PAGE_VALID
) {
2399 end
= tlb
->EPN
+ tlb
->size
;
2400 LOG_SWTLB("%s: invalidate TLB %d start " TARGET_FMT_lx
" end "
2401 TARGET_FMT_lx
"\n", __func__
, (int)entry
, tlb
->EPN
, end
);
2402 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
) {
2403 tlb_flush_page(cs
, page
);
2408 void helper_4xx_tlbwe_lo(CPUPPCState
*env
, target_ulong entry
,
2413 LOG_SWTLB("%s entry %i val " TARGET_FMT_lx
"\n", __func__
, (int)entry
,
2415 entry
&= PPC4XX_TLB_ENTRY_MASK
;
2416 tlb
= &env
->tlb
.tlbe
[entry
];
2417 tlb
->attr
= val
& PPC4XX_TLBLO_ATTR_MASK
;
2418 tlb
->RPN
= val
& PPC4XX_TLBLO_RPN_MASK
;
2419 tlb
->prot
= PAGE_READ
;
2420 if (val
& PPC4XX_TLBLO_EX
) {
2421 tlb
->prot
|= PAGE_EXEC
;
2423 if (val
& PPC4XX_TLBLO_WR
) {
2424 tlb
->prot
|= PAGE_WRITE
;
2426 LOG_SWTLB("%s: set up TLB %d RPN " TARGET_FMT_plx
" EPN " TARGET_FMT_lx
2427 " size " TARGET_FMT_lx
" prot %c%c%c%c PID %d\n", __func__
,
2428 (int)entry
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2429 tlb
->prot
& PAGE_READ
? 'r' : '-',
2430 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2431 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2432 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2435 target_ulong
helper_4xx_tlbsx(CPUPPCState
*env
, target_ulong address
)
2437 return ppcemb_tlb_search(env
, address
, env
->spr
[SPR_40x_PID
]);
2440 /* PowerPC 440 TLB management */
2441 void helper_440_tlbwe(CPUPPCState
*env
, uint32_t word
, target_ulong entry
,
2444 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2446 target_ulong EPN
, RPN
, size
;
2449 LOG_SWTLB("%s word %d entry %d value " TARGET_FMT_lx
"\n",
2450 __func__
, word
, (int)entry
, value
);
2453 tlb
= &env
->tlb
.tlbe
[entry
];
2456 /* Just here to please gcc */
2458 EPN
= value
& 0xFFFFFC00;
2459 if ((tlb
->prot
& PAGE_VALID
) && EPN
!= tlb
->EPN
) {
2463 size
= booke_tlb_to_page_size((value
>> 4) & 0xF);
2464 if ((tlb
->prot
& PAGE_VALID
) && tlb
->size
< size
) {
2469 tlb
->attr
|= (value
>> 8) & 1;
2470 if (value
& 0x200) {
2471 tlb
->prot
|= PAGE_VALID
;
2473 if (tlb
->prot
& PAGE_VALID
) {
2474 tlb
->prot
&= ~PAGE_VALID
;
2478 tlb
->PID
= env
->spr
[SPR_440_MMUCR
] & 0x000000FF;
2479 if (do_flush_tlbs
) {
2480 tlb_flush(CPU(cpu
), 1);
2484 RPN
= value
& 0xFFFFFC0F;
2485 if ((tlb
->prot
& PAGE_VALID
) && tlb
->RPN
!= RPN
) {
2486 tlb_flush(CPU(cpu
), 1);
2491 tlb
->attr
= (tlb
->attr
& 0x1) | (value
& 0x0000FF00);
2492 tlb
->prot
= tlb
->prot
& PAGE_VALID
;
2494 tlb
->prot
|= PAGE_READ
<< 4;
2497 tlb
->prot
|= PAGE_WRITE
<< 4;
2500 tlb
->prot
|= PAGE_EXEC
<< 4;
2503 tlb
->prot
|= PAGE_READ
;
2506 tlb
->prot
|= PAGE_WRITE
;
2509 tlb
->prot
|= PAGE_EXEC
;
2515 target_ulong
helper_440_tlbre(CPUPPCState
*env
, uint32_t word
,
2523 tlb
= &env
->tlb
.tlbe
[entry
];
2526 /* Just here to please gcc */
2529 size
= booke_page_size_to_tlb(tlb
->size
);
2530 if (size
< 0 || size
> 0xF) {
2534 if (tlb
->attr
& 0x1) {
2537 if (tlb
->prot
& PAGE_VALID
) {
2540 env
->spr
[SPR_440_MMUCR
] &= ~0x000000FF;
2541 env
->spr
[SPR_440_MMUCR
] |= tlb
->PID
;
2547 ret
= tlb
->attr
& ~0x1;
2548 if (tlb
->prot
& (PAGE_READ
<< 4)) {
2551 if (tlb
->prot
& (PAGE_WRITE
<< 4)) {
2554 if (tlb
->prot
& (PAGE_EXEC
<< 4)) {
2557 if (tlb
->prot
& PAGE_READ
) {
2560 if (tlb
->prot
& PAGE_WRITE
) {
2563 if (tlb
->prot
& PAGE_EXEC
) {
2571 target_ulong
helper_440_tlbsx(CPUPPCState
*env
, target_ulong address
)
2573 return ppcemb_tlb_search(env
, address
, env
->spr
[SPR_440_MMUCR
] & 0xFF);
2576 /* PowerPC BookE 2.06 TLB management */
2578 static ppcmas_tlb_t
*booke206_cur_tlb(CPUPPCState
*env
)
2580 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2581 uint32_t tlbncfg
= 0;
2582 int esel
= (env
->spr
[SPR_BOOKE_MAS0
] & MAS0_ESEL_MASK
) >> MAS0_ESEL_SHIFT
;
2583 int ea
= (env
->spr
[SPR_BOOKE_MAS2
] & MAS2_EPN_MASK
);
2586 tlb
= (env
->spr
[SPR_BOOKE_MAS0
] & MAS0_TLBSEL_MASK
) >> MAS0_TLBSEL_SHIFT
;
2587 tlbncfg
= env
->spr
[SPR_BOOKE_TLB0CFG
+ tlb
];
2589 if ((tlbncfg
& TLBnCFG_HES
) && (env
->spr
[SPR_BOOKE_MAS0
] & MAS0_HES
)) {
2590 cpu_abort(CPU(cpu
), "we don't support HES yet\n");
2593 return booke206_get_tlbm(env
, tlb
, ea
, esel
);
2596 void helper_booke_setpid(CPUPPCState
*env
, uint32_t pidn
, target_ulong pid
)
2598 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2600 env
->spr
[pidn
] = pid
;
2601 /* changing PIDs mean we're in a different address space now */
2602 tlb_flush(CPU(cpu
), 1);
2605 void helper_booke206_tlbwe(CPUPPCState
*env
)
2607 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2608 uint32_t tlbncfg
, tlbn
;
2610 uint32_t size_tlb
, size_ps
;
2614 switch (env
->spr
[SPR_BOOKE_MAS0
] & MAS0_WQ_MASK
) {
2615 case MAS0_WQ_ALWAYS
:
2616 /* good to go, write that entry */
2619 /* XXX check if reserved */
2624 case MAS0_WQ_CLR_RSRV
:
2625 /* XXX clear entry */
2628 /* no idea what to do */
2632 if (((env
->spr
[SPR_BOOKE_MAS0
] & MAS0_ATSEL
) == MAS0_ATSEL_LRAT
) &&
2634 /* XXX we don't support direct LRAT setting yet */
2635 fprintf(stderr
, "cpu: don't support LRAT setting yet\n");
2639 tlbn
= (env
->spr
[SPR_BOOKE_MAS0
] & MAS0_TLBSEL_MASK
) >> MAS0_TLBSEL_SHIFT
;
2640 tlbncfg
= env
->spr
[SPR_BOOKE_TLB0CFG
+ tlbn
];
2642 tlb
= booke206_cur_tlb(env
);
2645 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
2646 POWERPC_EXCP_INVAL
|
2647 POWERPC_EXCP_INVAL_INVAL
);
2650 /* check that we support the targeted size */
2651 size_tlb
= (env
->spr
[SPR_BOOKE_MAS1
] & MAS1_TSIZE_MASK
) >> MAS1_TSIZE_SHIFT
;
2652 size_ps
= booke206_tlbnps(env
, tlbn
);
2653 if ((env
->spr
[SPR_BOOKE_MAS1
] & MAS1_VALID
) && (tlbncfg
& TLBnCFG_AVAIL
) &&
2654 !(size_ps
& (1 << size_tlb
))) {
2655 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
2656 POWERPC_EXCP_INVAL
|
2657 POWERPC_EXCP_INVAL_INVAL
);
2661 cpu_abort(CPU(cpu
), "missing HV implementation\n");
2663 tlb
->mas7_3
= ((uint64_t)env
->spr
[SPR_BOOKE_MAS7
] << 32) |
2664 env
->spr
[SPR_BOOKE_MAS3
];
2665 tlb
->mas1
= env
->spr
[SPR_BOOKE_MAS1
];
2668 if (!(tlbncfg
& TLBnCFG_AVAIL
)) {
2669 /* force !AVAIL TLB entries to correct page size */
2670 tlb
->mas1
&= ~MAS1_TSIZE_MASK
;
2671 /* XXX can be configured in MMUCSR0 */
2672 tlb
->mas1
|= (tlbncfg
& TLBnCFG_MINSIZE
) >> 12;
2675 /* Make a mask from TLB size to discard invalid bits in EPN field */
2676 mask
= ~(booke206_tlb_to_page_size(env
, tlb
) - 1);
2677 /* Add a mask for page attributes */
2678 mask
|= MAS2_ACM
| MAS2_VLE
| MAS2_W
| MAS2_I
| MAS2_M
| MAS2_G
| MAS2_E
;
2681 /* Executing a tlbwe instruction in 32-bit mode will set
2682 * bits 0:31 of the TLB EPN field to zero.
2687 tlb
->mas2
= env
->spr
[SPR_BOOKE_MAS2
] & mask
;
2689 if (!(tlbncfg
& TLBnCFG_IPROT
)) {
2690 /* no IPROT supported by TLB */
2691 tlb
->mas1
&= ~MAS1_IPROT
;
2694 if (booke206_tlb_to_page_size(env
, tlb
) == TARGET_PAGE_SIZE
) {
2695 tlb_flush_page(CPU(cpu
), tlb
->mas2
& MAS2_EPN_MASK
);
2697 tlb_flush(CPU(cpu
), 1);
2701 static inline void booke206_tlb_to_mas(CPUPPCState
*env
, ppcmas_tlb_t
*tlb
)
2703 int tlbn
= booke206_tlbm_to_tlbn(env
, tlb
);
2704 int way
= booke206_tlbm_to_way(env
, tlb
);
2706 env
->spr
[SPR_BOOKE_MAS0
] = tlbn
<< MAS0_TLBSEL_SHIFT
;
2707 env
->spr
[SPR_BOOKE_MAS0
] |= way
<< MAS0_ESEL_SHIFT
;
2708 env
->spr
[SPR_BOOKE_MAS0
] |= env
->last_way
<< MAS0_NV_SHIFT
;
2710 env
->spr
[SPR_BOOKE_MAS1
] = tlb
->mas1
;
2711 env
->spr
[SPR_BOOKE_MAS2
] = tlb
->mas2
;
2712 env
->spr
[SPR_BOOKE_MAS3
] = tlb
->mas7_3
;
2713 env
->spr
[SPR_BOOKE_MAS7
] = tlb
->mas7_3
>> 32;
2716 void helper_booke206_tlbre(CPUPPCState
*env
)
2718 ppcmas_tlb_t
*tlb
= NULL
;
2720 tlb
= booke206_cur_tlb(env
);
2722 env
->spr
[SPR_BOOKE_MAS1
] = 0;
2724 booke206_tlb_to_mas(env
, tlb
);
2728 void helper_booke206_tlbsx(CPUPPCState
*env
, target_ulong address
)
2730 ppcmas_tlb_t
*tlb
= NULL
;
2735 spid
= (env
->spr
[SPR_BOOKE_MAS6
] & MAS6_SPID_MASK
) >> MAS6_SPID_SHIFT
;
2736 sas
= env
->spr
[SPR_BOOKE_MAS6
] & MAS6_SAS
;
2738 for (i
= 0; i
< BOOKE206_MAX_TLBN
; i
++) {
2739 int ways
= booke206_tlb_ways(env
, i
);
2741 for (j
= 0; j
< ways
; j
++) {
2742 tlb
= booke206_get_tlbm(env
, i
, address
, j
);
2748 if (ppcmas_tlb_check(env
, tlb
, &raddr
, address
, spid
)) {
2752 if (sas
!= ((tlb
->mas1
& MAS1_TS
) >> MAS1_TS_SHIFT
)) {
2756 booke206_tlb_to_mas(env
, tlb
);
2761 /* no entry found, fill with defaults */
2762 env
->spr
[SPR_BOOKE_MAS0
] = env
->spr
[SPR_BOOKE_MAS4
] & MAS4_TLBSELD_MASK
;
2763 env
->spr
[SPR_BOOKE_MAS1
] = env
->spr
[SPR_BOOKE_MAS4
] & MAS4_TSIZED_MASK
;
2764 env
->spr
[SPR_BOOKE_MAS2
] = env
->spr
[SPR_BOOKE_MAS4
] & MAS4_WIMGED_MASK
;
2765 env
->spr
[SPR_BOOKE_MAS3
] = 0;
2766 env
->spr
[SPR_BOOKE_MAS7
] = 0;
2768 if (env
->spr
[SPR_BOOKE_MAS6
] & MAS6_SAS
) {
2769 env
->spr
[SPR_BOOKE_MAS1
] |= MAS1_TS
;
2772 env
->spr
[SPR_BOOKE_MAS1
] |= (env
->spr
[SPR_BOOKE_MAS6
] >> 16)
2775 /* next victim logic */
2776 env
->spr
[SPR_BOOKE_MAS0
] |= env
->last_way
<< MAS0_ESEL_SHIFT
;
2778 env
->last_way
&= booke206_tlb_ways(env
, 0) - 1;
2779 env
->spr
[SPR_BOOKE_MAS0
] |= env
->last_way
<< MAS0_NV_SHIFT
;
2782 static inline void booke206_invalidate_ea_tlb(CPUPPCState
*env
, int tlbn
,
2786 int ways
= booke206_tlb_ways(env
, tlbn
);
2789 for (i
= 0; i
< ways
; i
++) {
2790 ppcmas_tlb_t
*tlb
= booke206_get_tlbm(env
, tlbn
, ea
, i
);
2794 mask
= ~(booke206_tlb_to_page_size(env
, tlb
) - 1);
2795 if (((tlb
->mas2
& MAS2_EPN_MASK
) == (ea
& mask
)) &&
2796 !(tlb
->mas1
& MAS1_IPROT
)) {
2797 tlb
->mas1
&= ~MAS1_VALID
;
2802 void helper_booke206_tlbivax(CPUPPCState
*env
, target_ulong address
)
2804 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2806 if (address
& 0x4) {
2807 /* flush all entries */
2808 if (address
& 0x8) {
2809 /* flush all of TLB1 */
2810 booke206_flush_tlb(env
, BOOKE206_FLUSH_TLB1
, 1);
2812 /* flush all of TLB0 */
2813 booke206_flush_tlb(env
, BOOKE206_FLUSH_TLB0
, 0);
2818 if (address
& 0x8) {
2819 /* flush TLB1 entries */
2820 booke206_invalidate_ea_tlb(env
, 1, address
);
2821 tlb_flush(CPU(cpu
), 1);
2823 /* flush TLB0 entries */
2824 booke206_invalidate_ea_tlb(env
, 0, address
);
2825 tlb_flush_page(CPU(cpu
), address
& MAS2_EPN_MASK
);
2829 void helper_booke206_tlbilx0(CPUPPCState
*env
, target_ulong address
)
2831 /* XXX missing LPID handling */
2832 booke206_flush_tlb(env
, -1, 1);
2835 void helper_booke206_tlbilx1(CPUPPCState
*env
, target_ulong address
)
2837 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2839 int tid
= (env
->spr
[SPR_BOOKE_MAS6
] & MAS6_SPID
);
2840 ppcmas_tlb_t
*tlb
= env
->tlb
.tlbm
;
2843 /* XXX missing LPID handling */
2844 for (i
= 0; i
< BOOKE206_MAX_TLBN
; i
++) {
2845 tlb_size
= booke206_tlb_size(env
, i
);
2846 for (j
= 0; j
< tlb_size
; j
++) {
2847 if (!(tlb
[j
].mas1
& MAS1_IPROT
) &&
2848 ((tlb
[j
].mas1
& MAS1_TID_MASK
) == tid
)) {
2849 tlb
[j
].mas1
&= ~MAS1_VALID
;
2852 tlb
+= booke206_tlb_size(env
, i
);
2854 tlb_flush(CPU(cpu
), 1);
2857 void helper_booke206_tlbilx3(CPUPPCState
*env
, target_ulong address
)
2859 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2862 int tid
= (env
->spr
[SPR_BOOKE_MAS6
] & MAS6_SPID
);
2863 int pid
= tid
>> MAS6_SPID_SHIFT
;
2864 int sgs
= env
->spr
[SPR_BOOKE_MAS5
] & MAS5_SGS
;
2865 int ind
= (env
->spr
[SPR_BOOKE_MAS6
] & MAS6_SIND
) ? MAS1_IND
: 0;
2866 /* XXX check for unsupported isize and raise an invalid opcode then */
2867 int size
= env
->spr
[SPR_BOOKE_MAS6
] & MAS6_ISIZE_MASK
;
2868 /* XXX implement MAV2 handling */
2871 /* XXX missing LPID handling */
2872 /* flush by pid and ea */
2873 for (i
= 0; i
< BOOKE206_MAX_TLBN
; i
++) {
2874 int ways
= booke206_tlb_ways(env
, i
);
2876 for (j
= 0; j
< ways
; j
++) {
2877 tlb
= booke206_get_tlbm(env
, i
, address
, j
);
2881 if ((ppcmas_tlb_check(env
, tlb
, NULL
, address
, pid
) != 0) ||
2882 (tlb
->mas1
& MAS1_IPROT
) ||
2883 ((tlb
->mas1
& MAS1_IND
) != ind
) ||
2884 ((tlb
->mas8
& MAS8_TGS
) != sgs
)) {
2887 if (mav2
&& ((tlb
->mas1
& MAS1_TSIZE_MASK
) != size
)) {
2888 /* XXX only check when MMUCFG[TWC] || TLBnCFG[HES] */
2891 /* XXX e500mc doesn't match SAS, but other cores might */
2892 tlb
->mas1
&= ~MAS1_VALID
;
2895 tlb_flush(CPU(cpu
), 1);
2898 void helper_booke206_tlbflush(CPUPPCState
*env
, target_ulong type
)
2903 flags
|= BOOKE206_FLUSH_TLB1
;
2907 flags
|= BOOKE206_FLUSH_TLB0
;
2910 booke206_flush_tlb(env
, flags
, 1);
2914 /*****************************************************************************/
2916 /* try to fill the TLB and return an exception if error. If retaddr is
2917 NULL, it means that the function was called in C code (i.e. not
2918 from generated code or from helper.c) */
2919 /* XXX: fix it to restore all registers */
2920 void tlb_fill(CPUState
*cs
, target_ulong addr
, int is_write
, int mmu_idx
,
2923 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
2924 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cs
);
2925 CPUPPCState
*env
= &cpu
->env
;
2928 if (pcc
->handle_mmu_fault
) {
2929 ret
= pcc
->handle_mmu_fault(cpu
, addr
, is_write
, mmu_idx
);
2931 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
);
2933 if (unlikely(ret
!= 0)) {
2934 if (likely(retaddr
)) {
2935 /* now we have a real cpu fault */
2936 cpu_restore_state(cs
, retaddr
);
2938 helper_raise_exception_err(env
, cs
->exception_index
, env
->error_code
);