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/>.
19 #include "qemu/osdep.h"
20 #include "qemu/units.h"
22 #include "exec/helper-proto.h"
23 #include "sysemu/kvm.h"
25 #include "mmu-hash64.h"
26 #include "mmu-hash32.h"
27 #include "exec/exec-all.h"
28 #include "exec/cpu_ldst.h"
30 #include "helper_regs.h"
31 #include "qemu/error-report.h"
32 #include "mmu-book3s-v3.h"
33 #include "mmu-radix64.h"
37 //#define DEBUG_SOFTWARE_TLB
38 //#define DUMP_PAGE_TABLES
39 //#define FLUSH_ALL_TLBS
42 # define LOG_MMU_STATE(cpu) log_cpu_state_mask(CPU_LOG_MMU, (cpu), 0)
44 # define LOG_MMU_STATE(cpu) do { } while (0)
47 #ifdef DEBUG_SOFTWARE_TLB
48 # define LOG_SWTLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
50 # define LOG_SWTLB(...) do { } while (0)
54 # define LOG_BATS(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
56 # define LOG_BATS(...) do { } while (0)
59 /*****************************************************************************/
60 /* PowerPC MMU emulation */
62 /* Context used internally during MMU translations */
63 typedef struct mmu_ctx_t mmu_ctx_t
;
65 hwaddr raddr
; /* Real address */
66 hwaddr eaddr
; /* Effective address */
67 int prot
; /* Protection bits */
68 hwaddr hash
[2]; /* Pagetable hash values */
69 target_ulong ptem
; /* Virtual segment ID | API */
70 int key
; /* Access key */
71 int nx
; /* Non-execute area */
74 /* Common routines used by software and hardware TLBs emulation */
75 static inline int pte_is_valid(target_ulong pte0
)
77 return pte0
& 0x80000000 ? 1 : 0;
80 static inline void pte_invalidate(target_ulong
*pte0
)
85 #define PTE_PTEM_MASK 0x7FFFFFBF
86 #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
88 static int pp_check(int key
, int pp
, int nx
)
92 /* Compute access rights */
115 access
= PAGE_READ
| PAGE_WRITE
;
126 static int check_prot(int prot
, int rw
, int access_type
)
130 if (access_type
== ACCESS_CODE
) {
131 if (prot
& PAGE_EXEC
) {
137 if (prot
& PAGE_WRITE
) {
143 if (prot
& PAGE_READ
) {
153 static inline int ppc6xx_tlb_pte_check(mmu_ctx_t
*ctx
, target_ulong pte0
,
154 target_ulong pte1
, int h
, int rw
, int type
)
156 target_ulong ptem
, mmask
;
157 int access
, ret
, pteh
, ptev
, pp
;
160 /* Check validity and table match */
161 ptev
= pte_is_valid(pte0
);
162 pteh
= (pte0
>> 6) & 1;
163 if (ptev
&& h
== pteh
) {
164 /* Check vsid & api */
165 ptem
= pte0
& PTE_PTEM_MASK
;
166 mmask
= PTE_CHECK_MASK
;
167 pp
= pte1
& 0x00000003;
168 if (ptem
== ctx
->ptem
) {
169 if (ctx
->raddr
!= (hwaddr
)-1ULL) {
170 /* all matches should have equal RPN, WIMG & PP */
171 if ((ctx
->raddr
& mmask
) != (pte1
& mmask
)) {
172 qemu_log_mask(CPU_LOG_MMU
, "Bad RPN/WIMG/PP\n");
176 /* Compute access rights */
177 access
= pp_check(ctx
->key
, pp
, ctx
->nx
);
178 /* Keep the matching PTE informations */
181 ret
= check_prot(ctx
->prot
, rw
, type
);
184 qemu_log_mask(CPU_LOG_MMU
, "PTE access granted !\n");
186 /* Access right violation */
187 qemu_log_mask(CPU_LOG_MMU
, "PTE access rejected\n");
195 static int pte_update_flags(mmu_ctx_t
*ctx
, target_ulong
*pte1p
,
200 /* Update page flags */
201 if (!(*pte1p
& 0x00000100)) {
202 /* Update accessed flag */
203 *pte1p
|= 0x00000100;
206 if (!(*pte1p
& 0x00000080)) {
207 if (rw
== 1 && ret
== 0) {
208 /* Update changed flag */
209 *pte1p
|= 0x00000080;
212 /* Force page fault for first write access */
213 ctx
->prot
&= ~PAGE_WRITE
;
220 /* Software driven TLB helpers */
221 static inline int ppc6xx_tlb_getnum(CPUPPCState
*env
, target_ulong eaddr
,
222 int way
, int is_code
)
226 /* Select TLB num in a way from address */
227 nr
= (eaddr
>> TARGET_PAGE_BITS
) & (env
->tlb_per_way
- 1);
229 nr
+= env
->tlb_per_way
* way
;
230 /* 6xx have separate TLBs for instructions and data */
231 if (is_code
&& env
->id_tlbs
== 1) {
238 static inline void ppc6xx_tlb_invalidate_all(CPUPPCState
*env
)
240 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
244 /* LOG_SWTLB("Invalidate all TLBs\n"); */
245 /* Invalidate all defined software TLB */
247 if (env
->id_tlbs
== 1) {
250 for (nr
= 0; nr
< max
; nr
++) {
251 tlb
= &env
->tlb
.tlb6
[nr
];
252 pte_invalidate(&tlb
->pte0
);
257 static inline void ppc6xx_tlb_invalidate_virt2(CPUPPCState
*env
,
259 int is_code
, int match_epn
)
261 #if !defined(FLUSH_ALL_TLBS)
262 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
266 /* Invalidate ITLB + DTLB, all ways */
267 for (way
= 0; way
< env
->nb_ways
; way
++) {
268 nr
= ppc6xx_tlb_getnum(env
, eaddr
, way
, is_code
);
269 tlb
= &env
->tlb
.tlb6
[nr
];
270 if (pte_is_valid(tlb
->pte0
) && (match_epn
== 0 || eaddr
== tlb
->EPN
)) {
271 LOG_SWTLB("TLB invalidate %d/%d " TARGET_FMT_lx
"\n", nr
,
273 pte_invalidate(&tlb
->pte0
);
274 tlb_flush_page(cs
, tlb
->EPN
);
278 /* XXX: PowerPC specification say this is valid as well */
279 ppc6xx_tlb_invalidate_all(env
);
283 static inline void ppc6xx_tlb_invalidate_virt(CPUPPCState
*env
,
284 target_ulong eaddr
, int is_code
)
286 ppc6xx_tlb_invalidate_virt2(env
, eaddr
, is_code
, 0);
289 static void ppc6xx_tlb_store(CPUPPCState
*env
, target_ulong EPN
, int way
,
290 int is_code
, target_ulong pte0
, target_ulong pte1
)
295 nr
= ppc6xx_tlb_getnum(env
, EPN
, way
, is_code
);
296 tlb
= &env
->tlb
.tlb6
[nr
];
297 LOG_SWTLB("Set TLB %d/%d EPN " TARGET_FMT_lx
" PTE0 " TARGET_FMT_lx
298 " PTE1 " TARGET_FMT_lx
"\n", nr
, env
->nb_tlb
, EPN
, pte0
, pte1
);
299 /* Invalidate any pending reference in QEMU for this virtual address */
300 ppc6xx_tlb_invalidate_virt2(env
, EPN
, is_code
, 1);
304 /* Store last way for LRU mechanism */
308 static inline int ppc6xx_tlb_check(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
309 target_ulong eaddr
, int rw
, int access_type
)
316 ret
= -1; /* No TLB found */
317 for (way
= 0; way
< env
->nb_ways
; way
++) {
318 nr
= ppc6xx_tlb_getnum(env
, eaddr
, way
,
319 access_type
== ACCESS_CODE
? 1 : 0);
320 tlb
= &env
->tlb
.tlb6
[nr
];
321 /* This test "emulates" the PTE index match for hardware TLBs */
322 if ((eaddr
& TARGET_PAGE_MASK
) != tlb
->EPN
) {
323 LOG_SWTLB("TLB %d/%d %s [" TARGET_FMT_lx
" " TARGET_FMT_lx
324 "] <> " TARGET_FMT_lx
"\n", nr
, env
->nb_tlb
,
325 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
326 tlb
->EPN
, tlb
->EPN
+ TARGET_PAGE_SIZE
, eaddr
);
329 LOG_SWTLB("TLB %d/%d %s " TARGET_FMT_lx
" <> " TARGET_FMT_lx
" "
330 TARGET_FMT_lx
" %c %c\n", nr
, env
->nb_tlb
,
331 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
332 tlb
->EPN
, eaddr
, tlb
->pte1
,
333 rw
? 'S' : 'L', access_type
== ACCESS_CODE
? 'I' : 'D');
334 switch (ppc6xx_tlb_pte_check(ctx
, tlb
->pte0
, tlb
->pte1
, 0, rw
, access_type
)) {
336 /* TLB inconsistency */
339 /* Access violation */
349 /* XXX: we should go on looping to check all TLBs consistency
350 * but we can speed-up the whole thing as the
351 * result would be undefined if TLBs are not consistent.
360 LOG_SWTLB("found TLB at addr " TARGET_FMT_plx
" prot=%01x ret=%d\n",
361 ctx
->raddr
& TARGET_PAGE_MASK
, ctx
->prot
, ret
);
362 /* Update page flags */
363 pte_update_flags(ctx
, &env
->tlb
.tlb6
[best
].pte1
, ret
, rw
);
369 /* Perform BAT hit & translation */
370 static inline void bat_size_prot(CPUPPCState
*env
, target_ulong
*blp
,
371 int *validp
, int *protp
, target_ulong
*BATu
,
377 bl
= (*BATu
& 0x00001FFC) << 15;
380 if (((msr_pr
== 0) && (*BATu
& 0x00000002)) ||
381 ((msr_pr
!= 0) && (*BATu
& 0x00000001))) {
383 pp
= *BATl
& 0x00000003;
385 prot
= PAGE_READ
| PAGE_EXEC
;
396 static int get_bat_6xx_tlb(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
397 target_ulong
virtual, int rw
, int type
)
399 target_ulong
*BATlt
, *BATut
, *BATu
, *BATl
;
400 target_ulong BEPIl
, BEPIu
, bl
;
404 LOG_BATS("%s: %cBAT v " TARGET_FMT_lx
"\n", __func__
,
405 type
== ACCESS_CODE
? 'I' : 'D', virtual);
408 BATlt
= env
->IBAT
[1];
409 BATut
= env
->IBAT
[0];
412 BATlt
= env
->DBAT
[1];
413 BATut
= env
->DBAT
[0];
416 for (i
= 0; i
< env
->nb_BATs
; i
++) {
419 BEPIu
= *BATu
& 0xF0000000;
420 BEPIl
= *BATu
& 0x0FFE0000;
421 bat_size_prot(env
, &bl
, &valid
, &prot
, BATu
, BATl
);
422 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx
" BATu " TARGET_FMT_lx
423 " BATl " TARGET_FMT_lx
"\n", __func__
,
424 type
== ACCESS_CODE
? 'I' : 'D', i
, virtual, *BATu
, *BATl
);
425 if ((virtual & 0xF0000000) == BEPIu
&&
426 ((virtual & 0x0FFE0000) & ~bl
) == BEPIl
) {
429 /* Get physical address */
430 ctx
->raddr
= (*BATl
& 0xF0000000) |
431 ((virtual & 0x0FFE0000 & bl
) | (*BATl
& 0x0FFE0000)) |
432 (virtual & 0x0001F000);
433 /* Compute access rights */
435 ret
= check_prot(ctx
->prot
, rw
, type
);
437 LOG_BATS("BAT %d match: r " TARGET_FMT_plx
" prot=%c%c\n",
438 i
, ctx
->raddr
, ctx
->prot
& PAGE_READ
? 'R' : '-',
439 ctx
->prot
& PAGE_WRITE
? 'W' : '-');
446 #if defined(DEBUG_BATS)
447 if (qemu_log_enabled()) {
448 LOG_BATS("no BAT match for " TARGET_FMT_lx
":\n", virtual);
449 for (i
= 0; i
< 4; i
++) {
452 BEPIu
= *BATu
& 0xF0000000;
453 BEPIl
= *BATu
& 0x0FFE0000;
454 bl
= (*BATu
& 0x00001FFC) << 15;
455 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx
" BATu " TARGET_FMT_lx
456 " BATl " TARGET_FMT_lx
"\n\t" TARGET_FMT_lx
" "
457 TARGET_FMT_lx
" " TARGET_FMT_lx
"\n",
458 __func__
, type
== ACCESS_CODE
? 'I' : 'D', i
, virtual,
459 *BATu
, *BATl
, BEPIu
, BEPIl
, bl
);
468 /* Perform segment based translation */
469 static inline int get_segment_6xx_tlb(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
470 target_ulong eaddr
, int rw
, int type
)
472 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
475 int ds
, pr
, target_page_bits
;
477 target_ulong sr
, pgidx
;
482 sr
= env
->sr
[eaddr
>> 28];
483 ctx
->key
= (((sr
& 0x20000000) && (pr
!= 0)) ||
484 ((sr
& 0x40000000) && (pr
== 0))) ? 1 : 0;
485 ds
= sr
& 0x80000000 ? 1 : 0;
486 ctx
->nx
= sr
& 0x10000000 ? 1 : 0;
487 vsid
= sr
& 0x00FFFFFF;
488 target_page_bits
= TARGET_PAGE_BITS
;
489 qemu_log_mask(CPU_LOG_MMU
,
490 "Check segment v=" TARGET_FMT_lx
" %d " TARGET_FMT_lx
491 " nip=" TARGET_FMT_lx
" lr=" TARGET_FMT_lx
492 " ir=%d dr=%d pr=%d %d t=%d\n",
493 eaddr
, (int)(eaddr
>> 28), sr
, env
->nip
, env
->lr
, (int)msr_ir
,
494 (int)msr_dr
, pr
!= 0 ? 1 : 0, rw
, type
);
495 pgidx
= (eaddr
& ~SEGMENT_MASK_256M
) >> target_page_bits
;
497 ctx
->ptem
= (vsid
<< 7) | (pgidx
>> 10);
499 qemu_log_mask(CPU_LOG_MMU
,
500 "pte segment: key=%d ds %d nx %d vsid " TARGET_FMT_lx
"\n",
501 ctx
->key
, ds
, ctx
->nx
, vsid
);
504 /* Check if instruction fetch is allowed, if needed */
505 if (type
!= ACCESS_CODE
|| ctx
->nx
== 0) {
506 /* Page address translation */
507 qemu_log_mask(CPU_LOG_MMU
, "htab_base " TARGET_FMT_plx
508 " htab_mask " TARGET_FMT_plx
509 " hash " TARGET_FMT_plx
"\n",
510 ppc_hash32_hpt_base(cpu
), ppc_hash32_hpt_mask(cpu
), hash
);
512 ctx
->hash
[1] = ~hash
;
514 /* Initialize real address with an invalid value */
515 ctx
->raddr
= (hwaddr
)-1ULL;
516 /* Software TLB search */
517 ret
= ppc6xx_tlb_check(env
, ctx
, eaddr
, rw
, type
);
518 #if defined(DUMP_PAGE_TABLES)
519 if (qemu_loglevel_mask(CPU_LOG_MMU
)) {
520 CPUState
*cs
= ENV_GET_CPU(env
);
522 uint32_t a0
, a1
, a2
, a3
;
524 qemu_log("Page table: " TARGET_FMT_plx
" len " TARGET_FMT_plx
525 "\n", ppc_hash32_hpt_base(cpu
),
526 ppc_hash32_hpt_mask(env
) + 0x80);
527 for (curaddr
= ppc_hash32_hpt_base(cpu
);
528 curaddr
< (ppc_hash32_hpt_base(cpu
)
529 + ppc_hash32_hpt_mask(cpu
) + 0x80);
531 a0
= ldl_phys(cs
->as
, curaddr
);
532 a1
= ldl_phys(cs
->as
, curaddr
+ 4);
533 a2
= ldl_phys(cs
->as
, curaddr
+ 8);
534 a3
= ldl_phys(cs
->as
, curaddr
+ 12);
535 if (a0
!= 0 || a1
!= 0 || a2
!= 0 || a3
!= 0) {
536 qemu_log(TARGET_FMT_plx
": %08x %08x %08x %08x\n",
537 curaddr
, a0
, a1
, a2
, a3
);
543 qemu_log_mask(CPU_LOG_MMU
, "No access allowed\n");
549 qemu_log_mask(CPU_LOG_MMU
, "direct store...\n");
550 /* Direct-store segment : absolutely *BUGGY* for now */
552 /* Direct-store implies a 32-bit MMU.
553 * Check the Segment Register's bus unit ID (BUID).
555 sr
= env
->sr
[eaddr
>> 28];
556 if ((sr
& 0x1FF00000) >> 20 == 0x07f) {
557 /* Memory-forced I/O controller interface access */
558 /* If T=1 and BUID=x'07F', the 601 performs a memory access
559 * to SR[28-31] LA[4-31], bypassing all protection mechanisms.
561 ctx
->raddr
= ((sr
& 0xF) << 28) | (eaddr
& 0x0FFFFFFF);
562 ctx
->prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
568 /* Integer load/store : only access allowed */
571 /* No code fetch is allowed in direct-store areas */
574 /* Floating point load/store */
577 /* lwarx, ldarx or srwcx. */
580 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
581 /* Should make the instruction do no-op.
582 * As it already do no-op, it's quite easy :-)
590 qemu_log_mask(CPU_LOG_MMU
, "ERROR: instruction should not need "
591 "address translation\n");
594 if ((rw
== 1 || ctx
->key
!= 1) && (rw
== 0 || ctx
->key
!= 0)) {
605 /* Generic TLB check function for embedded PowerPC implementations */
606 static int ppcemb_tlb_check(CPUPPCState
*env
, ppcemb_tlb_t
*tlb
,
608 target_ulong address
, uint32_t pid
, int ext
,
613 /* Check valid flag */
614 if (!(tlb
->prot
& PAGE_VALID
)) {
617 mask
= ~(tlb
->size
- 1);
618 LOG_SWTLB("%s: TLB %d address " TARGET_FMT_lx
" PID %u <=> " TARGET_FMT_lx
619 " " TARGET_FMT_lx
" %u %x\n", __func__
, i
, address
, pid
, tlb
->EPN
,
620 mask
, (uint32_t)tlb
->PID
, tlb
->prot
);
622 if (tlb
->PID
!= 0 && tlb
->PID
!= pid
) {
625 /* Check effective address */
626 if ((address
& mask
) != tlb
->EPN
) {
629 *raddrp
= (tlb
->RPN
& mask
) | (address
& ~mask
);
631 /* Extend the physical address to 36 bits */
632 *raddrp
|= (uint64_t)(tlb
->RPN
& 0xF) << 32;
638 /* Generic TLB search function for PowerPC embedded implementations */
639 static int ppcemb_tlb_search(CPUPPCState
*env
, target_ulong address
,
646 /* Default return value is no match */
648 for (i
= 0; i
< env
->nb_tlb
; i
++) {
649 tlb
= &env
->tlb
.tlbe
[i
];
650 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
, pid
, 0, i
) == 0) {
659 /* Helpers specific to PowerPC 40x implementations */
660 static inline void ppc4xx_tlb_invalidate_all(CPUPPCState
*env
)
662 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
666 for (i
= 0; i
< env
->nb_tlb
; i
++) {
667 tlb
= &env
->tlb
.tlbe
[i
];
668 tlb
->prot
&= ~PAGE_VALID
;
673 static int mmu40x_get_physical_address(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
674 target_ulong address
, int rw
,
679 int i
, ret
, zsel
, zpr
, pr
;
682 raddr
= (hwaddr
)-1ULL;
684 for (i
= 0; i
< env
->nb_tlb
; i
++) {
685 tlb
= &env
->tlb
.tlbe
[i
];
686 if (ppcemb_tlb_check(env
, tlb
, &raddr
, address
,
687 env
->spr
[SPR_40x_PID
], 0, i
) < 0) {
690 zsel
= (tlb
->attr
>> 4) & 0xF;
691 zpr
= (env
->spr
[SPR_40x_ZPR
] >> (30 - (2 * zsel
))) & 0x3;
692 LOG_SWTLB("%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
693 __func__
, i
, zsel
, zpr
, rw
, tlb
->attr
);
694 /* Check execute enable bit */
702 /* All accesses granted */
703 ctx
->prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
708 /* Raise Zone protection fault. */
709 env
->spr
[SPR_40x_ESR
] = 1 << 22;
717 /* Check from TLB entry */
718 ctx
->prot
= tlb
->prot
;
719 ret
= check_prot(ctx
->prot
, rw
, access_type
);
721 env
->spr
[SPR_40x_ESR
] = 0;
727 LOG_SWTLB("%s: access granted " TARGET_FMT_lx
" => " TARGET_FMT_plx
728 " %d %d\n", __func__
, address
, ctx
->raddr
, ctx
->prot
,
733 LOG_SWTLB("%s: access refused " TARGET_FMT_lx
" => " TARGET_FMT_plx
734 " %d %d\n", __func__
, address
, raddr
, ctx
->prot
, ret
);
739 void store_40x_sler(CPUPPCState
*env
, uint32_t val
)
741 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
743 /* XXX: TO BE FIXED */
744 if (val
!= 0x00000000) {
745 cpu_abort(CPU(cpu
), "Little-endian regions are not supported by now\n");
747 env
->spr
[SPR_405_SLER
] = val
;
750 static inline int mmubooke_check_tlb(CPUPPCState
*env
, ppcemb_tlb_t
*tlb
,
751 hwaddr
*raddr
, int *prot
,
752 target_ulong address
, int rw
,
753 int access_type
, int i
)
757 if (ppcemb_tlb_check(env
, tlb
, raddr
, address
,
758 env
->spr
[SPR_BOOKE_PID
],
759 !env
->nb_pids
, i
) >= 0) {
763 if (env
->spr
[SPR_BOOKE_PID1
] &&
764 ppcemb_tlb_check(env
, tlb
, raddr
, address
,
765 env
->spr
[SPR_BOOKE_PID1
], 0, i
) >= 0) {
769 if (env
->spr
[SPR_BOOKE_PID2
] &&
770 ppcemb_tlb_check(env
, tlb
, raddr
, address
,
771 env
->spr
[SPR_BOOKE_PID2
], 0, i
) >= 0) {
775 LOG_SWTLB("%s: TLB entry not found\n", __func__
);
781 prot2
= tlb
->prot
& 0xF;
783 prot2
= (tlb
->prot
>> 4) & 0xF;
786 /* Check the address space */
787 if (access_type
== ACCESS_CODE
) {
788 if (msr_ir
!= (tlb
->attr
& 1)) {
789 LOG_SWTLB("%s: AS doesn't match\n", __func__
);
794 if (prot2
& PAGE_EXEC
) {
795 LOG_SWTLB("%s: good TLB!\n", __func__
);
799 LOG_SWTLB("%s: no PAGE_EXEC: %x\n", __func__
, prot2
);
802 if (msr_dr
!= (tlb
->attr
& 1)) {
803 LOG_SWTLB("%s: AS doesn't match\n", __func__
);
808 if ((!rw
&& prot2
& PAGE_READ
) || (rw
&& (prot2
& PAGE_WRITE
))) {
809 LOG_SWTLB("%s: found TLB!\n", __func__
);
813 LOG_SWTLB("%s: PAGE_READ/WRITE doesn't match: %x\n", __func__
, prot2
);
820 static int mmubooke_get_physical_address(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
821 target_ulong address
, int rw
,
829 raddr
= (hwaddr
)-1ULL;
830 for (i
= 0; i
< env
->nb_tlb
; i
++) {
831 tlb
= &env
->tlb
.tlbe
[i
];
832 ret
= mmubooke_check_tlb(env
, tlb
, &raddr
, &ctx
->prot
, address
, rw
,
841 LOG_SWTLB("%s: access granted " TARGET_FMT_lx
" => " TARGET_FMT_plx
842 " %d %d\n", __func__
, address
, ctx
->raddr
, ctx
->prot
,
845 LOG_SWTLB("%s: access refused " TARGET_FMT_lx
" => " TARGET_FMT_plx
846 " %d %d\n", __func__
, address
, raddr
, ctx
->prot
, ret
);
852 static void booke206_flush_tlb(CPUPPCState
*env
, int flags
,
853 const int check_iprot
)
855 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
858 ppcmas_tlb_t
*tlb
= env
->tlb
.tlbm
;
860 for (i
= 0; i
< BOOKE206_MAX_TLBN
; i
++) {
861 if (flags
& (1 << i
)) {
862 tlb_size
= booke206_tlb_size(env
, i
);
863 for (j
= 0; j
< tlb_size
; j
++) {
864 if (!check_iprot
|| !(tlb
[j
].mas1
& MAS1_IPROT
)) {
865 tlb
[j
].mas1
&= ~MAS1_VALID
;
869 tlb
+= booke206_tlb_size(env
, i
);
875 static hwaddr
booke206_tlb_to_page_size(CPUPPCState
*env
,
880 tlbm_size
= (tlb
->mas1
& MAS1_TSIZE_MASK
) >> MAS1_TSIZE_SHIFT
;
882 return 1024ULL << tlbm_size
;
885 /* TLB check function for MAS based SoftTLBs */
886 static int ppcmas_tlb_check(CPUPPCState
*env
, ppcmas_tlb_t
*tlb
,
887 hwaddr
*raddrp
, target_ulong address
,
894 /* In 32bit mode we can only address 32bit EAs */
895 address
= (uint32_t)address
;
898 /* Check valid flag */
899 if (!(tlb
->mas1
& MAS1_VALID
)) {
903 mask
= ~(booke206_tlb_to_page_size(env
, tlb
) - 1);
904 LOG_SWTLB("%s: TLB ADDR=0x" TARGET_FMT_lx
" PID=0x%x MAS1=0x%x MAS2=0x%"
905 PRIx64
" mask=0x%" HWADDR_PRIx
" MAS7_3=0x%" PRIx64
" MAS8=0x%"
906 PRIx32
"\n", __func__
, address
, pid
, tlb
->mas1
, tlb
->mas2
, mask
,
907 tlb
->mas7_3
, tlb
->mas8
);
910 tlb_pid
= (tlb
->mas1
& MAS1_TID_MASK
) >> MAS1_TID_SHIFT
;
911 if (tlb_pid
!= 0 && tlb_pid
!= pid
) {
915 /* Check effective address */
916 if ((address
& mask
) != (tlb
->mas2
& MAS2_EPN_MASK
)) {
921 *raddrp
= (tlb
->mas7_3
& mask
) | (address
& ~mask
);
927 static bool is_epid_mmu(int mmu_idx
)
929 return mmu_idx
== PPC_TLB_EPID_STORE
|| mmu_idx
== PPC_TLB_EPID_LOAD
;
932 static uint32_t mmubooke206_esr(int mmu_idx
, bool rw
)
938 if (is_epid_mmu(mmu_idx
)) {
944 /* Get EPID register given the mmu_idx. If this is regular load,
945 * construct the EPID access bits from current processor state */
947 /* Get the effective AS and PR bits and the PID. The PID is returned only if
948 * EPID load is requested, otherwise the caller must detect the correct EPID.
949 * Return true if valid EPID is returned. */
950 static bool mmubooke206_get_as(CPUPPCState
*env
,
951 int mmu_idx
, uint32_t *epid_out
,
952 bool *as_out
, bool *pr_out
)
954 if (is_epid_mmu(mmu_idx
)) {
956 if (mmu_idx
== PPC_TLB_EPID_STORE
) {
957 epidr
= env
->spr
[SPR_BOOKE_EPSC
];
959 epidr
= env
->spr
[SPR_BOOKE_EPLC
];
961 *epid_out
= (epidr
& EPID_EPID
) >> EPID_EPID_SHIFT
;
962 *as_out
= !!(epidr
& EPID_EAS
);
963 *pr_out
= !!(epidr
& EPID_EPR
);
972 /* Check if the tlb found by hashing really matches */
973 static int mmubooke206_check_tlb(CPUPPCState
*env
, ppcmas_tlb_t
*tlb
,
974 hwaddr
*raddr
, int *prot
,
975 target_ulong address
, int rw
,
976 int access_type
, int mmu_idx
)
982 bool use_epid
= mmubooke206_get_as(env
, mmu_idx
, &epid
, &as
, &pr
);
985 if (ppcmas_tlb_check(env
, tlb
, raddr
, address
,
986 env
->spr
[SPR_BOOKE_PID
]) >= 0) {
990 if (env
->spr
[SPR_BOOKE_PID1
] &&
991 ppcmas_tlb_check(env
, tlb
, raddr
, address
,
992 env
->spr
[SPR_BOOKE_PID1
]) >= 0) {
996 if (env
->spr
[SPR_BOOKE_PID2
] &&
997 ppcmas_tlb_check(env
, tlb
, raddr
, address
,
998 env
->spr
[SPR_BOOKE_PID2
]) >= 0) {
1002 if (ppcmas_tlb_check(env
, tlb
, raddr
, address
, epid
) >= 0) {
1007 LOG_SWTLB("%s: TLB entry not found\n", __func__
);
1013 if (tlb
->mas7_3
& MAS3_UR
) {
1016 if (tlb
->mas7_3
& MAS3_UW
) {
1017 prot2
|= PAGE_WRITE
;
1019 if (tlb
->mas7_3
& MAS3_UX
) {
1023 if (tlb
->mas7_3
& MAS3_SR
) {
1026 if (tlb
->mas7_3
& MAS3_SW
) {
1027 prot2
|= PAGE_WRITE
;
1029 if (tlb
->mas7_3
& MAS3_SX
) {
1034 /* Check the address space and permissions */
1035 if (access_type
== ACCESS_CODE
) {
1036 /* There is no way to fetch code using epid load */
1038 if (msr_ir
!= ((tlb
->mas1
& MAS1_TS
) >> MAS1_TS_SHIFT
)) {
1039 LOG_SWTLB("%s: AS doesn't match\n", __func__
);
1044 if (prot2
& PAGE_EXEC
) {
1045 LOG_SWTLB("%s: good TLB!\n", __func__
);
1049 LOG_SWTLB("%s: no PAGE_EXEC: %x\n", __func__
, prot2
);
1052 if (as
!= ((tlb
->mas1
& MAS1_TS
) >> MAS1_TS_SHIFT
)) {
1053 LOG_SWTLB("%s: AS doesn't match\n", __func__
);
1058 if ((!rw
&& prot2
& PAGE_READ
) || (rw
&& (prot2
& PAGE_WRITE
))) {
1059 LOG_SWTLB("%s: found TLB!\n", __func__
);
1063 LOG_SWTLB("%s: PAGE_READ/WRITE doesn't match: %x\n", __func__
, prot2
);
1070 static int mmubooke206_get_physical_address(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
1071 target_ulong address
, int rw
,
1072 int access_type
, int mmu_idx
)
1079 raddr
= (hwaddr
)-1ULL;
1081 for (i
= 0; i
< BOOKE206_MAX_TLBN
; i
++) {
1082 int ways
= booke206_tlb_ways(env
, i
);
1084 for (j
= 0; j
< ways
; j
++) {
1085 tlb
= booke206_get_tlbm(env
, i
, address
, j
);
1089 ret
= mmubooke206_check_tlb(env
, tlb
, &raddr
, &ctx
->prot
, address
,
1090 rw
, access_type
, mmu_idx
);
1101 LOG_SWTLB("%s: access granted " TARGET_FMT_lx
" => " TARGET_FMT_plx
1102 " %d %d\n", __func__
, address
, ctx
->raddr
, ctx
->prot
,
1105 LOG_SWTLB("%s: access refused " TARGET_FMT_lx
" => " TARGET_FMT_plx
1106 " %d %d\n", __func__
, address
, raddr
, ctx
->prot
, ret
);
1112 static const char *book3e_tsize_to_str
[32] = {
1113 "1K", "2K", "4K", "8K", "16K", "32K", "64K", "128K", "256K", "512K",
1114 "1M", "2M", "4M", "8M", "16M", "32M", "64M", "128M", "256M", "512M",
1115 "1G", "2G", "4G", "8G", "16G", "32G", "64G", "128G", "256G", "512G",
1119 static void mmubooke_dump_mmu(FILE *f
, fprintf_function cpu_fprintf
,
1122 ppcemb_tlb_t
*entry
;
1125 if (kvm_enabled() && !env
->kvm_sw_tlb
) {
1126 cpu_fprintf(f
, "Cannot access KVM TLB\n");
1130 cpu_fprintf(f
, "\nTLB:\n");
1131 cpu_fprintf(f
, "Effective Physical Size PID Prot "
1134 entry
= &env
->tlb
.tlbe
[0];
1135 for (i
= 0; i
< env
->nb_tlb
; i
++, entry
++) {
1138 uint64_t size
= (uint64_t)entry
->size
;
1141 /* Check valid flag */
1142 if (!(entry
->prot
& PAGE_VALID
)) {
1146 mask
= ~(entry
->size
- 1);
1147 ea
= entry
->EPN
& mask
;
1148 pa
= entry
->RPN
& mask
;
1149 /* Extend the physical address to 36 bits */
1150 pa
|= (hwaddr
)(entry
->RPN
& 0xF) << 32;
1151 if (size
>= 1 * MiB
) {
1152 snprintf(size_buf
, sizeof(size_buf
), "%3" PRId64
"M", size
/ MiB
);
1154 snprintf(size_buf
, sizeof(size_buf
), "%3" PRId64
"k", size
/ KiB
);
1156 cpu_fprintf(f
, "0x%016" PRIx64
" 0x%016" PRIx64
" %s %-5u %08x %08x\n",
1157 (uint64_t)ea
, (uint64_t)pa
, size_buf
, (uint32_t)entry
->PID
,
1158 entry
->prot
, entry
->attr
);
1163 static void mmubooke206_dump_one_tlb(FILE *f
, fprintf_function cpu_fprintf
,
1164 CPUPPCState
*env
, int tlbn
, int offset
,
1167 ppcmas_tlb_t
*entry
;
1170 cpu_fprintf(f
, "\nTLB%d:\n", tlbn
);
1171 cpu_fprintf(f
, "Effective Physical Size TID TS SRWX"
1172 " URWX WIMGE U0123\n");
1174 entry
= &env
->tlb
.tlbm
[offset
];
1175 for (i
= 0; i
< tlbsize
; i
++, entry
++) {
1176 hwaddr ea
, pa
, size
;
1179 if (!(entry
->mas1
& MAS1_VALID
)) {
1183 tsize
= (entry
->mas1
& MAS1_TSIZE_MASK
) >> MAS1_TSIZE_SHIFT
;
1184 size
= 1024ULL << tsize
;
1185 ea
= entry
->mas2
& ~(size
- 1);
1186 pa
= entry
->mas7_3
& ~(size
- 1);
1188 cpu_fprintf(f
, "0x%016" PRIx64
" 0x%016" PRIx64
" %4s %-5u %1u S%c%c%c"
1189 "U%c%c%c %c%c%c%c%c U%c%c%c%c\n",
1190 (uint64_t)ea
, (uint64_t)pa
,
1191 book3e_tsize_to_str
[tsize
],
1192 (entry
->mas1
& MAS1_TID_MASK
) >> MAS1_TID_SHIFT
,
1193 (entry
->mas1
& MAS1_TS
) >> MAS1_TS_SHIFT
,
1194 entry
->mas7_3
& MAS3_SR
? 'R' : '-',
1195 entry
->mas7_3
& MAS3_SW
? 'W' : '-',
1196 entry
->mas7_3
& MAS3_SX
? 'X' : '-',
1197 entry
->mas7_3
& MAS3_UR
? 'R' : '-',
1198 entry
->mas7_3
& MAS3_UW
? 'W' : '-',
1199 entry
->mas7_3
& MAS3_UX
? 'X' : '-',
1200 entry
->mas2
& MAS2_W
? 'W' : '-',
1201 entry
->mas2
& MAS2_I
? 'I' : '-',
1202 entry
->mas2
& MAS2_M
? 'M' : '-',
1203 entry
->mas2
& MAS2_G
? 'G' : '-',
1204 entry
->mas2
& MAS2_E
? 'E' : '-',
1205 entry
->mas7_3
& MAS3_U0
? '0' : '-',
1206 entry
->mas7_3
& MAS3_U1
? '1' : '-',
1207 entry
->mas7_3
& MAS3_U2
? '2' : '-',
1208 entry
->mas7_3
& MAS3_U3
? '3' : '-');
1212 static void mmubooke206_dump_mmu(FILE *f
, fprintf_function cpu_fprintf
,
1218 if (kvm_enabled() && !env
->kvm_sw_tlb
) {
1219 cpu_fprintf(f
, "Cannot access KVM TLB\n");
1223 for (i
= 0; i
< BOOKE206_MAX_TLBN
; i
++) {
1224 int size
= booke206_tlb_size(env
, i
);
1230 mmubooke206_dump_one_tlb(f
, cpu_fprintf
, env
, i
, offset
, size
);
1235 static void mmu6xx_dump_BATs(FILE *f
, fprintf_function cpu_fprintf
,
1236 CPUPPCState
*env
, int type
)
1238 target_ulong
*BATlt
, *BATut
, *BATu
, *BATl
;
1239 target_ulong BEPIl
, BEPIu
, bl
;
1244 BATlt
= env
->IBAT
[1];
1245 BATut
= env
->IBAT
[0];
1248 BATlt
= env
->DBAT
[1];
1249 BATut
= env
->DBAT
[0];
1253 for (i
= 0; i
< env
->nb_BATs
; i
++) {
1256 BEPIu
= *BATu
& 0xF0000000;
1257 BEPIl
= *BATu
& 0x0FFE0000;
1258 bl
= (*BATu
& 0x00001FFC) << 15;
1259 cpu_fprintf(f
, "%s BAT%d BATu " TARGET_FMT_lx
1260 " BATl " TARGET_FMT_lx
"\n\t" TARGET_FMT_lx
" "
1261 TARGET_FMT_lx
" " TARGET_FMT_lx
"\n",
1262 type
== ACCESS_CODE
? "code" : "data", i
,
1263 *BATu
, *BATl
, BEPIu
, BEPIl
, bl
);
1267 static void mmu6xx_dump_mmu(FILE *f
, fprintf_function cpu_fprintf
,
1270 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1273 int type
, way
, entry
, i
;
1275 cpu_fprintf(f
, "HTAB base = 0x%"HWADDR_PRIx
"\n", ppc_hash32_hpt_base(cpu
));
1276 cpu_fprintf(f
, "HTAB mask = 0x%"HWADDR_PRIx
"\n", ppc_hash32_hpt_mask(cpu
));
1278 cpu_fprintf(f
, "\nSegment registers:\n");
1279 for (i
= 0; i
< 32; i
++) {
1281 if (sr
& 0x80000000) {
1282 cpu_fprintf(f
, "%02d T=%d Ks=%d Kp=%d BUID=0x%03x "
1283 "CNTLR_SPEC=0x%05x\n", i
,
1284 sr
& 0x80000000 ? 1 : 0, sr
& 0x40000000 ? 1 : 0,
1285 sr
& 0x20000000 ? 1 : 0, (uint32_t)((sr
>> 20) & 0x1FF),
1286 (uint32_t)(sr
& 0xFFFFF));
1288 cpu_fprintf(f
, "%02d T=%d Ks=%d Kp=%d N=%d VSID=0x%06x\n", i
,
1289 sr
& 0x80000000 ? 1 : 0, sr
& 0x40000000 ? 1 : 0,
1290 sr
& 0x20000000 ? 1 : 0, sr
& 0x10000000 ? 1 : 0,
1291 (uint32_t)(sr
& 0x00FFFFFF));
1295 cpu_fprintf(f
, "\nBATs:\n");
1296 mmu6xx_dump_BATs(f
, cpu_fprintf
, env
, ACCESS_INT
);
1297 mmu6xx_dump_BATs(f
, cpu_fprintf
, env
, ACCESS_CODE
);
1299 if (env
->id_tlbs
!= 1) {
1300 cpu_fprintf(f
, "ERROR: 6xx MMU should have separated TLB"
1301 " for code and data\n");
1304 cpu_fprintf(f
, "\nTLBs [EPN EPN + SIZE]\n");
1306 for (type
= 0; type
< 2; type
++) {
1307 for (way
= 0; way
< env
->nb_ways
; way
++) {
1308 for (entry
= env
->nb_tlb
* type
+ env
->tlb_per_way
* way
;
1309 entry
< (env
->nb_tlb
* type
+ env
->tlb_per_way
* (way
+ 1));
1312 tlb
= &env
->tlb
.tlb6
[entry
];
1313 cpu_fprintf(f
, "%s TLB %02d/%02d way:%d %s ["
1314 TARGET_FMT_lx
" " TARGET_FMT_lx
"]\n",
1315 type
? "code" : "data", entry
% env
->nb_tlb
,
1317 pte_is_valid(tlb
->pte0
) ? "valid" : "inval",
1318 tlb
->EPN
, tlb
->EPN
+ TARGET_PAGE_SIZE
);
1324 void dump_mmu(FILE *f
, fprintf_function cpu_fprintf
, CPUPPCState
*env
)
1326 switch (env
->mmu_model
) {
1327 case POWERPC_MMU_BOOKE
:
1328 mmubooke_dump_mmu(f
, cpu_fprintf
, env
);
1330 case POWERPC_MMU_BOOKE206
:
1331 mmubooke206_dump_mmu(f
, cpu_fprintf
, env
);
1333 case POWERPC_MMU_SOFT_6xx
:
1334 case POWERPC_MMU_SOFT_74xx
:
1335 mmu6xx_dump_mmu(f
, cpu_fprintf
, env
);
1337 #if defined(TARGET_PPC64)
1338 case POWERPC_MMU_64B
:
1339 case POWERPC_MMU_2_03
:
1340 case POWERPC_MMU_2_06
:
1341 case POWERPC_MMU_2_07
:
1342 dump_slb(f
, cpu_fprintf
, ppc_env_get_cpu(env
));
1344 case POWERPC_MMU_3_00
:
1345 if (ppc64_radix_guest(ppc_env_get_cpu(env
))) {
1346 /* TODO - Unsupported */
1348 dump_slb(f
, cpu_fprintf
, ppc_env_get_cpu(env
));
1353 qemu_log_mask(LOG_UNIMP
, "%s: unimplemented\n", __func__
);
1357 static inline int check_physical(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
1358 target_ulong eaddr
, int rw
)
1363 ctx
->prot
= PAGE_READ
| PAGE_EXEC
;
1365 switch (env
->mmu_model
) {
1366 case POWERPC_MMU_SOFT_6xx
:
1367 case POWERPC_MMU_SOFT_74xx
:
1368 case POWERPC_MMU_SOFT_4xx
:
1369 case POWERPC_MMU_REAL
:
1370 case POWERPC_MMU_BOOKE
:
1371 ctx
->prot
|= PAGE_WRITE
;
1374 case POWERPC_MMU_SOFT_4xx_Z
:
1375 if (unlikely(msr_pe
!= 0)) {
1376 /* 403 family add some particular protections,
1377 * using PBL/PBU registers for accesses with no translation.
1380 /* Check PLB validity */
1381 (env
->pb
[0] < env
->pb
[1] &&
1382 /* and address in plb area */
1383 eaddr
>= env
->pb
[0] && eaddr
< env
->pb
[1]) ||
1384 (env
->pb
[2] < env
->pb
[3] &&
1385 eaddr
>= env
->pb
[2] && eaddr
< env
->pb
[3]) ? 1 : 0;
1386 if (in_plb
^ msr_px
) {
1387 /* Access in protected area */
1389 /* Access is not allowed */
1393 /* Read-write access is allowed */
1394 ctx
->prot
|= PAGE_WRITE
;
1400 /* Caller's checks mean we should never get here for other models */
1408 static int get_physical_address_wtlb(
1409 CPUPPCState
*env
, mmu_ctx_t
*ctx
,
1410 target_ulong eaddr
, int rw
, int access_type
,
1413 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1415 bool real_mode
= (access_type
== ACCESS_CODE
&& msr_ir
== 0)
1416 || (access_type
!= ACCESS_CODE
&& msr_dr
== 0);
1418 switch (env
->mmu_model
) {
1419 case POWERPC_MMU_SOFT_6xx
:
1420 case POWERPC_MMU_SOFT_74xx
:
1422 ret
= check_physical(env
, ctx
, eaddr
, rw
);
1424 /* Try to find a BAT */
1425 if (env
->nb_BATs
!= 0) {
1426 ret
= get_bat_6xx_tlb(env
, ctx
, eaddr
, rw
, access_type
);
1429 /* We didn't match any BAT entry or don't have BATs */
1430 ret
= get_segment_6xx_tlb(env
, ctx
, eaddr
, rw
, access_type
);
1435 case POWERPC_MMU_SOFT_4xx
:
1436 case POWERPC_MMU_SOFT_4xx_Z
:
1438 ret
= check_physical(env
, ctx
, eaddr
, rw
);
1440 ret
= mmu40x_get_physical_address(env
, ctx
, eaddr
,
1444 case POWERPC_MMU_BOOKE
:
1445 ret
= mmubooke_get_physical_address(env
, ctx
, eaddr
,
1448 case POWERPC_MMU_BOOKE206
:
1449 ret
= mmubooke206_get_physical_address(env
, ctx
, eaddr
, rw
,
1450 access_type
, mmu_idx
);
1452 case POWERPC_MMU_MPC8xx
:
1454 cpu_abort(CPU(cpu
), "MPC8xx MMU model is not implemented\n");
1456 case POWERPC_MMU_REAL
:
1458 ret
= check_physical(env
, ctx
, eaddr
, rw
);
1460 cpu_abort(CPU(cpu
), "PowerPC in real mode do not do any translation\n");
1464 cpu_abort(CPU(cpu
), "Unknown or invalid MMU model\n");
1471 static int get_physical_address(
1472 CPUPPCState
*env
, mmu_ctx_t
*ctx
,
1473 target_ulong eaddr
, int rw
, int access_type
)
1475 return get_physical_address_wtlb(env
, ctx
, eaddr
, rw
, access_type
, 0);
1478 hwaddr
ppc_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
1480 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
1481 CPUPPCState
*env
= &cpu
->env
;
1484 switch (env
->mmu_model
) {
1485 #if defined(TARGET_PPC64)
1486 case POWERPC_MMU_64B
:
1487 case POWERPC_MMU_2_03
:
1488 case POWERPC_MMU_2_06
:
1489 case POWERPC_MMU_2_07
:
1490 return ppc_hash64_get_phys_page_debug(cpu
, addr
);
1491 case POWERPC_MMU_3_00
:
1492 if (ppc64_radix_guest(ppc_env_get_cpu(env
))) {
1493 return ppc_radix64_get_phys_page_debug(cpu
, addr
);
1495 return ppc_hash64_get_phys_page_debug(cpu
, addr
);
1500 case POWERPC_MMU_32B
:
1501 case POWERPC_MMU_601
:
1502 return ppc_hash32_get_phys_page_debug(cpu
, addr
);
1508 if (unlikely(get_physical_address(env
, &ctx
, addr
, 0, ACCESS_INT
) != 0)) {
1510 /* Some MMUs have separate TLBs for code and data. If we only try an
1511 * ACCESS_INT, we may not be able to read instructions mapped by code
1512 * TLBs, so we also try a ACCESS_CODE.
1514 if (unlikely(get_physical_address(env
, &ctx
, addr
, 0,
1515 ACCESS_CODE
) != 0)) {
1520 return ctx
.raddr
& TARGET_PAGE_MASK
;
1523 static void booke206_update_mas_tlb_miss(CPUPPCState
*env
, target_ulong address
,
1524 int rw
, int mmu_idx
)
1528 uint32_t missed_tid
= 0;
1529 bool use_epid
= mmubooke206_get_as(env
, mmu_idx
, &epid
, &as
, &pr
);
1533 env
->spr
[SPR_BOOKE_MAS0
] = env
->spr
[SPR_BOOKE_MAS4
] & MAS4_TLBSELD_MASK
;
1534 env
->spr
[SPR_BOOKE_MAS1
] = env
->spr
[SPR_BOOKE_MAS4
] & MAS4_TSIZED_MASK
;
1535 env
->spr
[SPR_BOOKE_MAS2
] = env
->spr
[SPR_BOOKE_MAS4
] & MAS4_WIMGED_MASK
;
1536 env
->spr
[SPR_BOOKE_MAS3
] = 0;
1537 env
->spr
[SPR_BOOKE_MAS6
] = 0;
1538 env
->spr
[SPR_BOOKE_MAS7
] = 0;
1542 env
->spr
[SPR_BOOKE_MAS1
] |= MAS1_TS
;
1543 env
->spr
[SPR_BOOKE_MAS6
] |= MAS6_SAS
;
1546 env
->spr
[SPR_BOOKE_MAS1
] |= MAS1_VALID
;
1547 env
->spr
[SPR_BOOKE_MAS2
] |= address
& MAS2_EPN_MASK
;
1550 switch (env
->spr
[SPR_BOOKE_MAS4
] & MAS4_TIDSELD_PIDZ
) {
1551 case MAS4_TIDSELD_PID0
:
1552 missed_tid
= env
->spr
[SPR_BOOKE_PID
];
1554 case MAS4_TIDSELD_PID1
:
1555 missed_tid
= env
->spr
[SPR_BOOKE_PID1
];
1557 case MAS4_TIDSELD_PID2
:
1558 missed_tid
= env
->spr
[SPR_BOOKE_PID2
];
1561 env
->spr
[SPR_BOOKE_MAS6
] |= env
->spr
[SPR_BOOKE_PID
] << 16;
1564 env
->spr
[SPR_BOOKE_MAS6
] |= missed_tid
<< 16;
1566 env
->spr
[SPR_BOOKE_MAS1
] |= (missed_tid
<< MAS1_TID_SHIFT
);
1569 /* next victim logic */
1570 env
->spr
[SPR_BOOKE_MAS0
] |= env
->last_way
<< MAS0_ESEL_SHIFT
;
1572 env
->last_way
&= booke206_tlb_ways(env
, 0) - 1;
1573 env
->spr
[SPR_BOOKE_MAS0
] |= env
->last_way
<< MAS0_NV_SHIFT
;
1576 /* Perform address translation */
1577 static int cpu_ppc_handle_mmu_fault(CPUPPCState
*env
, target_ulong address
,
1578 int rw
, int mmu_idx
)
1580 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
1581 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
1589 access_type
= ACCESS_CODE
;
1592 access_type
= env
->access_type
;
1594 ret
= get_physical_address_wtlb(env
, &ctx
, address
, rw
,
1595 access_type
, mmu_idx
);
1597 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
,
1598 ctx
.raddr
& TARGET_PAGE_MASK
, ctx
.prot
,
1599 mmu_idx
, TARGET_PAGE_SIZE
);
1601 } else if (ret
< 0) {
1603 if (access_type
== ACCESS_CODE
) {
1606 /* No matches in page tables or TLB */
1607 switch (env
->mmu_model
) {
1608 case POWERPC_MMU_SOFT_6xx
:
1609 cs
->exception_index
= POWERPC_EXCP_IFTLB
;
1610 env
->error_code
= 1 << 18;
1611 env
->spr
[SPR_IMISS
] = address
;
1612 env
->spr
[SPR_ICMP
] = 0x80000000 | ctx
.ptem
;
1614 case POWERPC_MMU_SOFT_74xx
:
1615 cs
->exception_index
= POWERPC_EXCP_IFTLB
;
1617 case POWERPC_MMU_SOFT_4xx
:
1618 case POWERPC_MMU_SOFT_4xx_Z
:
1619 cs
->exception_index
= POWERPC_EXCP_ITLB
;
1620 env
->error_code
= 0;
1621 env
->spr
[SPR_40x_DEAR
] = address
;
1622 env
->spr
[SPR_40x_ESR
] = 0x00000000;
1624 case POWERPC_MMU_BOOKE206
:
1625 booke206_update_mas_tlb_miss(env
, address
, 2, mmu_idx
);
1627 case POWERPC_MMU_BOOKE
:
1628 cs
->exception_index
= POWERPC_EXCP_ITLB
;
1629 env
->error_code
= 0;
1630 env
->spr
[SPR_BOOKE_DEAR
] = address
;
1631 env
->spr
[SPR_BOOKE_ESR
] = mmubooke206_esr(mmu_idx
, 0);
1633 case POWERPC_MMU_MPC8xx
:
1635 cpu_abort(cs
, "MPC8xx MMU model is not implemented\n");
1637 case POWERPC_MMU_REAL
:
1638 cpu_abort(cs
, "PowerPC in real mode should never raise "
1639 "any MMU exceptions\n");
1642 cpu_abort(cs
, "Unknown or invalid MMU model\n");
1647 /* Access rights violation */
1648 cs
->exception_index
= POWERPC_EXCP_ISI
;
1649 env
->error_code
= 0x08000000;
1652 /* No execute protection violation */
1653 if ((env
->mmu_model
== POWERPC_MMU_BOOKE
) ||
1654 (env
->mmu_model
== POWERPC_MMU_BOOKE206
)) {
1655 env
->spr
[SPR_BOOKE_ESR
] = 0x00000000;
1657 cs
->exception_index
= POWERPC_EXCP_ISI
;
1658 env
->error_code
= 0x10000000;
1661 /* Direct store exception */
1662 /* No code fetch is allowed in direct-store areas */
1663 cs
->exception_index
= POWERPC_EXCP_ISI
;
1664 env
->error_code
= 0x10000000;
1670 /* No matches in page tables or TLB */
1671 switch (env
->mmu_model
) {
1672 case POWERPC_MMU_SOFT_6xx
:
1674 cs
->exception_index
= POWERPC_EXCP_DSTLB
;
1675 env
->error_code
= 1 << 16;
1677 cs
->exception_index
= POWERPC_EXCP_DLTLB
;
1678 env
->error_code
= 0;
1680 env
->spr
[SPR_DMISS
] = address
;
1681 env
->spr
[SPR_DCMP
] = 0x80000000 | ctx
.ptem
;
1683 env
->error_code
|= ctx
.key
<< 19;
1684 env
->spr
[SPR_HASH1
] = ppc_hash32_hpt_base(cpu
) +
1685 get_pteg_offset32(cpu
, ctx
.hash
[0]);
1686 env
->spr
[SPR_HASH2
] = ppc_hash32_hpt_base(cpu
) +
1687 get_pteg_offset32(cpu
, ctx
.hash
[1]);
1689 case POWERPC_MMU_SOFT_74xx
:
1691 cs
->exception_index
= POWERPC_EXCP_DSTLB
;
1693 cs
->exception_index
= POWERPC_EXCP_DLTLB
;
1696 /* Implement LRU algorithm */
1697 env
->error_code
= ctx
.key
<< 19;
1698 env
->spr
[SPR_TLBMISS
] = (address
& ~((target_ulong
)0x3)) |
1699 ((env
->last_way
+ 1) & (env
->nb_ways
- 1));
1700 env
->spr
[SPR_PTEHI
] = 0x80000000 | ctx
.ptem
;
1702 case POWERPC_MMU_SOFT_4xx
:
1703 case POWERPC_MMU_SOFT_4xx_Z
:
1704 cs
->exception_index
= POWERPC_EXCP_DTLB
;
1705 env
->error_code
= 0;
1706 env
->spr
[SPR_40x_DEAR
] = address
;
1708 env
->spr
[SPR_40x_ESR
] = 0x00800000;
1710 env
->spr
[SPR_40x_ESR
] = 0x00000000;
1713 case POWERPC_MMU_MPC8xx
:
1715 cpu_abort(cs
, "MPC8xx MMU model is not implemented\n");
1717 case POWERPC_MMU_BOOKE206
:
1718 booke206_update_mas_tlb_miss(env
, address
, rw
, mmu_idx
);
1720 case POWERPC_MMU_BOOKE
:
1721 cs
->exception_index
= POWERPC_EXCP_DTLB
;
1722 env
->error_code
= 0;
1723 env
->spr
[SPR_BOOKE_DEAR
] = address
;
1724 env
->spr
[SPR_BOOKE_ESR
] = mmubooke206_esr(mmu_idx
, rw
);
1726 case POWERPC_MMU_REAL
:
1727 cpu_abort(cs
, "PowerPC in real mode should never raise "
1728 "any MMU exceptions\n");
1731 cpu_abort(cs
, "Unknown or invalid MMU model\n");
1736 /* Access rights violation */
1737 cs
->exception_index
= POWERPC_EXCP_DSI
;
1738 env
->error_code
= 0;
1739 if (env
->mmu_model
== POWERPC_MMU_SOFT_4xx
1740 || env
->mmu_model
== POWERPC_MMU_SOFT_4xx_Z
) {
1741 env
->spr
[SPR_40x_DEAR
] = address
;
1743 env
->spr
[SPR_40x_ESR
] |= 0x00800000;
1745 } else if ((env
->mmu_model
== POWERPC_MMU_BOOKE
) ||
1746 (env
->mmu_model
== POWERPC_MMU_BOOKE206
)) {
1747 env
->spr
[SPR_BOOKE_DEAR
] = address
;
1748 env
->spr
[SPR_BOOKE_ESR
] = mmubooke206_esr(mmu_idx
, rw
);
1750 env
->spr
[SPR_DAR
] = address
;
1752 env
->spr
[SPR_DSISR
] = 0x0A000000;
1754 env
->spr
[SPR_DSISR
] = 0x08000000;
1759 /* Direct store exception */
1760 switch (access_type
) {
1762 /* Floating point load/store */
1763 cs
->exception_index
= POWERPC_EXCP_ALIGN
;
1764 env
->error_code
= POWERPC_EXCP_ALIGN_FP
;
1765 env
->spr
[SPR_DAR
] = address
;
1768 /* lwarx, ldarx or stwcx. */
1769 cs
->exception_index
= POWERPC_EXCP_DSI
;
1770 env
->error_code
= 0;
1771 env
->spr
[SPR_DAR
] = address
;
1773 env
->spr
[SPR_DSISR
] = 0x06000000;
1775 env
->spr
[SPR_DSISR
] = 0x04000000;
1779 /* eciwx or ecowx */
1780 cs
->exception_index
= POWERPC_EXCP_DSI
;
1781 env
->error_code
= 0;
1782 env
->spr
[SPR_DAR
] = address
;
1784 env
->spr
[SPR_DSISR
] = 0x06100000;
1786 env
->spr
[SPR_DSISR
] = 0x04100000;
1790 printf("DSI: invalid exception (%d)\n", ret
);
1791 cs
->exception_index
= POWERPC_EXCP_PROGRAM
;
1793 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
;
1794 env
->spr
[SPR_DAR
] = address
;
1806 /*****************************************************************************/
1807 /* BATs management */
1808 #if !defined(FLUSH_ALL_TLBS)
1809 static inline void do_invalidate_BAT(CPUPPCState
*env
, target_ulong BATu
,
1812 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
1813 target_ulong base
, end
, page
;
1815 base
= BATu
& ~0x0001FFFF;
1816 end
= base
+ mask
+ 0x00020000;
1817 LOG_BATS("Flush BAT from " TARGET_FMT_lx
" to " TARGET_FMT_lx
" ("
1818 TARGET_FMT_lx
")\n", base
, end
, mask
);
1819 for (page
= base
; page
!= end
; page
+= TARGET_PAGE_SIZE
) {
1820 tlb_flush_page(cs
, page
);
1822 LOG_BATS("Flush done\n");
1826 static inline void dump_store_bat(CPUPPCState
*env
, char ID
, int ul
, int nr
,
1829 LOG_BATS("Set %cBAT%d%c to " TARGET_FMT_lx
" (" TARGET_FMT_lx
")\n", ID
,
1830 nr
, ul
== 0 ? 'u' : 'l', value
, env
->nip
);
1833 void helper_store_ibatu(CPUPPCState
*env
, uint32_t nr
, target_ulong value
)
1836 #if defined(FLUSH_ALL_TLBS)
1837 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1840 dump_store_bat(env
, 'I', 0, nr
, value
);
1841 if (env
->IBAT
[0][nr
] != value
) {
1842 mask
= (value
<< 15) & 0x0FFE0000UL
;
1843 #if !defined(FLUSH_ALL_TLBS)
1844 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1846 /* When storing valid upper BAT, mask BEPI and BRPN
1847 * and invalidate all TLBs covered by this BAT
1849 mask
= (value
<< 15) & 0x0FFE0000UL
;
1850 env
->IBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1851 (value
& ~0x0001FFFFUL
& ~mask
);
1852 env
->IBAT
[1][nr
] = (env
->IBAT
[1][nr
] & 0x0000007B) |
1853 (env
->IBAT
[1][nr
] & ~0x0001FFFF & ~mask
);
1854 #if !defined(FLUSH_ALL_TLBS)
1855 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1857 tlb_flush(CPU(cpu
));
1862 void helper_store_ibatl(CPUPPCState
*env
, uint32_t nr
, target_ulong value
)
1864 dump_store_bat(env
, 'I', 1, nr
, value
);
1865 env
->IBAT
[1][nr
] = value
;
1868 void helper_store_dbatu(CPUPPCState
*env
, uint32_t nr
, target_ulong value
)
1871 #if defined(FLUSH_ALL_TLBS)
1872 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1875 dump_store_bat(env
, 'D', 0, nr
, value
);
1876 if (env
->DBAT
[0][nr
] != value
) {
1877 /* When storing valid upper BAT, mask BEPI and BRPN
1878 * and invalidate all TLBs covered by this BAT
1880 mask
= (value
<< 15) & 0x0FFE0000UL
;
1881 #if !defined(FLUSH_ALL_TLBS)
1882 do_invalidate_BAT(env
, env
->DBAT
[0][nr
], mask
);
1884 mask
= (value
<< 15) & 0x0FFE0000UL
;
1885 env
->DBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1886 (value
& ~0x0001FFFFUL
& ~mask
);
1887 env
->DBAT
[1][nr
] = (env
->DBAT
[1][nr
] & 0x0000007B) |
1888 (env
->DBAT
[1][nr
] & ~0x0001FFFF & ~mask
);
1889 #if !defined(FLUSH_ALL_TLBS)
1890 do_invalidate_BAT(env
, env
->DBAT
[0][nr
], mask
);
1892 tlb_flush(CPU(cpu
));
1897 void helper_store_dbatl(CPUPPCState
*env
, uint32_t nr
, target_ulong value
)
1899 dump_store_bat(env
, 'D', 1, nr
, value
);
1900 env
->DBAT
[1][nr
] = value
;
1903 void helper_store_601_batu(CPUPPCState
*env
, uint32_t nr
, target_ulong value
)
1906 #if defined(FLUSH_ALL_TLBS)
1907 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1911 dump_store_bat(env
, 'I', 0, nr
, value
);
1912 if (env
->IBAT
[0][nr
] != value
) {
1913 #if defined(FLUSH_ALL_TLBS)
1916 mask
= (env
->IBAT
[1][nr
] << 17) & 0x0FFE0000UL
;
1917 if (env
->IBAT
[1][nr
] & 0x40) {
1918 /* Invalidate BAT only if it is valid */
1919 #if !defined(FLUSH_ALL_TLBS)
1920 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1925 /* When storing valid upper BAT, mask BEPI and BRPN
1926 * and invalidate all TLBs covered by this BAT
1928 env
->IBAT
[0][nr
] = (value
& 0x00001FFFUL
) |
1929 (value
& ~0x0001FFFFUL
& ~mask
);
1930 env
->DBAT
[0][nr
] = env
->IBAT
[0][nr
];
1931 if (env
->IBAT
[1][nr
] & 0x40) {
1932 #if !defined(FLUSH_ALL_TLBS)
1933 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1938 #if defined(FLUSH_ALL_TLBS)
1940 tlb_flush(CPU(cpu
));
1946 void helper_store_601_batl(CPUPPCState
*env
, uint32_t nr
, target_ulong value
)
1948 #if !defined(FLUSH_ALL_TLBS)
1951 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1955 dump_store_bat(env
, 'I', 1, nr
, value
);
1956 if (env
->IBAT
[1][nr
] != value
) {
1957 #if defined(FLUSH_ALL_TLBS)
1960 if (env
->IBAT
[1][nr
] & 0x40) {
1961 #if !defined(FLUSH_ALL_TLBS)
1962 mask
= (env
->IBAT
[1][nr
] << 17) & 0x0FFE0000UL
;
1963 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1969 #if !defined(FLUSH_ALL_TLBS)
1970 mask
= (value
<< 17) & 0x0FFE0000UL
;
1971 do_invalidate_BAT(env
, env
->IBAT
[0][nr
], mask
);
1976 env
->IBAT
[1][nr
] = value
;
1977 env
->DBAT
[1][nr
] = value
;
1978 #if defined(FLUSH_ALL_TLBS)
1980 tlb_flush(CPU(cpu
));
1986 /*****************************************************************************/
1987 /* TLB management */
1988 void ppc_tlb_invalidate_all(CPUPPCState
*env
)
1990 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
1992 #if defined(TARGET_PPC64)
1993 if (env
->mmu_model
& POWERPC_MMU_64
) {
1994 env
->tlb_need_flush
= 0;
1995 tlb_flush(CPU(cpu
));
1997 #endif /* defined(TARGET_PPC64) */
1998 switch (env
->mmu_model
) {
1999 case POWERPC_MMU_SOFT_6xx
:
2000 case POWERPC_MMU_SOFT_74xx
:
2001 ppc6xx_tlb_invalidate_all(env
);
2003 case POWERPC_MMU_SOFT_4xx
:
2004 case POWERPC_MMU_SOFT_4xx_Z
:
2005 ppc4xx_tlb_invalidate_all(env
);
2007 case POWERPC_MMU_REAL
:
2008 cpu_abort(CPU(cpu
), "No TLB for PowerPC 4xx in real mode\n");
2010 case POWERPC_MMU_MPC8xx
:
2012 cpu_abort(CPU(cpu
), "MPC8xx MMU model is not implemented\n");
2014 case POWERPC_MMU_BOOKE
:
2015 tlb_flush(CPU(cpu
));
2017 case POWERPC_MMU_BOOKE206
:
2018 booke206_flush_tlb(env
, -1, 0);
2020 case POWERPC_MMU_32B
:
2021 case POWERPC_MMU_601
:
2022 env
->tlb_need_flush
= 0;
2023 tlb_flush(CPU(cpu
));
2027 cpu_abort(CPU(cpu
), "Unknown MMU model %x\n", env
->mmu_model
);
2032 void ppc_tlb_invalidate_one(CPUPPCState
*env
, target_ulong addr
)
2034 #if !defined(FLUSH_ALL_TLBS)
2035 addr
&= TARGET_PAGE_MASK
;
2036 #if defined(TARGET_PPC64)
2037 if (env
->mmu_model
& POWERPC_MMU_64
) {
2038 /* tlbie invalidate TLBs for all segments */
2039 /* XXX: given the fact that there are too many segments to invalidate,
2040 * and we still don't have a tlb_flush_mask(env, n, mask) in QEMU,
2041 * we just invalidate all TLBs
2043 env
->tlb_need_flush
|= TLB_NEED_LOCAL_FLUSH
;
2045 #endif /* defined(TARGET_PPC64) */
2046 switch (env
->mmu_model
) {
2047 case POWERPC_MMU_SOFT_6xx
:
2048 case POWERPC_MMU_SOFT_74xx
:
2049 ppc6xx_tlb_invalidate_virt(env
, addr
, 0);
2050 if (env
->id_tlbs
== 1) {
2051 ppc6xx_tlb_invalidate_virt(env
, addr
, 1);
2054 case POWERPC_MMU_32B
:
2055 case POWERPC_MMU_601
:
2056 /* Actual CPUs invalidate entire congruence classes based on the
2057 * geometry of their TLBs and some OSes take that into account,
2058 * we just mark the TLB to be flushed later (context synchronizing
2059 * event or sync instruction on 32-bit).
2061 env
->tlb_need_flush
|= TLB_NEED_LOCAL_FLUSH
;
2064 /* Should never reach here with other MMU models */
2068 ppc_tlb_invalidate_all(env
);
2072 /*****************************************************************************/
2073 /* Special registers manipulation */
2074 void ppc_store_sdr1(CPUPPCState
*env
, target_ulong value
)
2076 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2077 qemu_log_mask(CPU_LOG_MMU
, "%s: " TARGET_FMT_lx
"\n", __func__
, value
);
2079 #if defined(TARGET_PPC64)
2080 if (env
->mmu_model
& POWERPC_MMU_64
) {
2081 target_ulong sdr_mask
= SDR_64_HTABORG
| SDR_64_HTABSIZE
;
2082 target_ulong htabsize
= value
& SDR_64_HTABSIZE
;
2084 if (value
& ~sdr_mask
) {
2085 error_report("Invalid bits 0x"TARGET_FMT_lx
" set in SDR1",
2089 if (htabsize
> 28) {
2090 error_report("Invalid HTABSIZE 0x" TARGET_FMT_lx
" stored in SDR1",
2095 #endif /* defined(TARGET_PPC64) */
2096 /* FIXME: Should check for valid HTABMASK values in 32-bit case */
2097 env
->spr
[SPR_SDR1
] = value
;
2100 #if defined(TARGET_PPC64)
2101 void ppc_store_ptcr(CPUPPCState
*env
, target_ulong value
)
2103 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2104 target_ulong ptcr_mask
= PTCR_PATB
| PTCR_PATS
;
2105 target_ulong patbsize
= value
& PTCR_PATS
;
2107 qemu_log_mask(CPU_LOG_MMU
, "%s: " TARGET_FMT_lx
"\n", __func__
, value
);
2110 assert(env
->mmu_model
& POWERPC_MMU_3_00
);
2112 if (value
& ~ptcr_mask
) {
2113 error_report("Invalid bits 0x"TARGET_FMT_lx
" set in PTCR",
2114 value
& ~ptcr_mask
);
2118 if (patbsize
> 24) {
2119 error_report("Invalid Partition Table size 0x" TARGET_FMT_lx
2120 " stored in PTCR", patbsize
);
2124 env
->spr
[SPR_PTCR
] = value
;
2127 #endif /* defined(TARGET_PPC64) */
2129 /* Segment registers load and store */
2130 target_ulong
helper_load_sr(CPUPPCState
*env
, target_ulong sr_num
)
2132 #if defined(TARGET_PPC64)
2133 if (env
->mmu_model
& POWERPC_MMU_64
) {
2138 return env
->sr
[sr_num
];
2141 void helper_store_sr(CPUPPCState
*env
, target_ulong srnum
, target_ulong value
)
2143 qemu_log_mask(CPU_LOG_MMU
,
2144 "%s: reg=%d " TARGET_FMT_lx
" " TARGET_FMT_lx
"\n", __func__
,
2145 (int)srnum
, value
, env
->sr
[srnum
]);
2146 #if defined(TARGET_PPC64)
2147 if (env
->mmu_model
& POWERPC_MMU_64
) {
2148 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2149 uint64_t esid
, vsid
;
2152 esid
= ((uint64_t)(srnum
& 0xf) << 28) | SLB_ESID_V
;
2155 vsid
= (value
& 0xfffffff) << 12;
2157 vsid
|= ((value
>> 27) & 0xf) << 8;
2159 ppc_store_slb(cpu
, srnum
, esid
, vsid
);
2162 if (env
->sr
[srnum
] != value
) {
2163 env
->sr
[srnum
] = value
;
2164 /* Invalidating 256MB of virtual memory in 4kB pages is way longer than
2165 flusing the whole TLB. */
2166 #if !defined(FLUSH_ALL_TLBS) && 0
2168 target_ulong page
, end
;
2169 /* Invalidate 256 MB of virtual memory */
2170 page
= (16 << 20) * srnum
;
2171 end
= page
+ (16 << 20);
2172 for (; page
!= end
; page
+= TARGET_PAGE_SIZE
) {
2173 tlb_flush_page(CPU(cpu
), page
);
2177 env
->tlb_need_flush
|= TLB_NEED_LOCAL_FLUSH
;
2182 /* TLB management */
2183 void helper_tlbia(CPUPPCState
*env
)
2185 ppc_tlb_invalidate_all(env
);
2188 void helper_tlbie(CPUPPCState
*env
, target_ulong addr
)
2190 ppc_tlb_invalidate_one(env
, addr
);
2193 void helper_tlbiva(CPUPPCState
*env
, target_ulong addr
)
2195 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2197 /* tlbiva instruction only exists on BookE */
2198 assert(env
->mmu_model
== POWERPC_MMU_BOOKE
);
2200 cpu_abort(CPU(cpu
), "BookE MMU model is not implemented\n");
2203 /* Software driven TLBs management */
2204 /* PowerPC 602/603 software TLB load instructions helpers */
2205 static void do_6xx_tlb(CPUPPCState
*env
, target_ulong new_EPN
, int is_code
)
2207 target_ulong RPN
, CMP
, EPN
;
2210 RPN
= env
->spr
[SPR_RPA
];
2212 CMP
= env
->spr
[SPR_ICMP
];
2213 EPN
= env
->spr
[SPR_IMISS
];
2215 CMP
= env
->spr
[SPR_DCMP
];
2216 EPN
= env
->spr
[SPR_DMISS
];
2218 way
= (env
->spr
[SPR_SRR1
] >> 17) & 1;
2219 (void)EPN
; /* avoid a compiler warning */
2220 LOG_SWTLB("%s: EPN " TARGET_FMT_lx
" " TARGET_FMT_lx
" PTE0 " TARGET_FMT_lx
2221 " PTE1 " TARGET_FMT_lx
" way %d\n", __func__
, new_EPN
, EPN
, CMP
,
2223 /* Store this TLB */
2224 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
2225 way
, is_code
, CMP
, RPN
);
2228 void helper_6xx_tlbd(CPUPPCState
*env
, target_ulong EPN
)
2230 do_6xx_tlb(env
, EPN
, 0);
2233 void helper_6xx_tlbi(CPUPPCState
*env
, target_ulong EPN
)
2235 do_6xx_tlb(env
, EPN
, 1);
2238 /* PowerPC 74xx software TLB load instructions helpers */
2239 static void do_74xx_tlb(CPUPPCState
*env
, target_ulong new_EPN
, int is_code
)
2241 target_ulong RPN
, CMP
, EPN
;
2244 RPN
= env
->spr
[SPR_PTELO
];
2245 CMP
= env
->spr
[SPR_PTEHI
];
2246 EPN
= env
->spr
[SPR_TLBMISS
] & ~0x3;
2247 way
= env
->spr
[SPR_TLBMISS
] & 0x3;
2248 (void)EPN
; /* avoid a compiler warning */
2249 LOG_SWTLB("%s: EPN " TARGET_FMT_lx
" " TARGET_FMT_lx
" PTE0 " TARGET_FMT_lx
2250 " PTE1 " TARGET_FMT_lx
" way %d\n", __func__
, new_EPN
, EPN
, CMP
,
2252 /* Store this TLB */
2253 ppc6xx_tlb_store(env
, (uint32_t)(new_EPN
& TARGET_PAGE_MASK
),
2254 way
, is_code
, CMP
, RPN
);
2257 void helper_74xx_tlbd(CPUPPCState
*env
, target_ulong EPN
)
2259 do_74xx_tlb(env
, EPN
, 0);
2262 void helper_74xx_tlbi(CPUPPCState
*env
, target_ulong EPN
)
2264 do_74xx_tlb(env
, EPN
, 1);
2267 /*****************************************************************************/
2268 /* PowerPC 601 specific instructions (POWER bridge) */
2270 target_ulong
helper_rac(CPUPPCState
*env
, target_ulong addr
)
2274 target_ulong ret
= 0;
2276 /* We don't have to generate many instances of this instruction,
2277 * as rac is supervisor only.
2279 /* XXX: FIX THIS: Pretend we have no BAT */
2280 nb_BATs
= env
->nb_BATs
;
2282 if (get_physical_address(env
, &ctx
, addr
, 0, ACCESS_INT
) == 0) {
2285 env
->nb_BATs
= nb_BATs
;
2289 static inline target_ulong
booke_tlb_to_page_size(int size
)
2291 return 1024 << (2 * size
);
2294 static inline int booke_page_size_to_tlb(target_ulong page_size
)
2298 switch (page_size
) {
2332 #if defined(TARGET_PPC64)
2333 case 0x000100000000ULL
:
2336 case 0x000400000000ULL
:
2339 case 0x001000000000ULL
:
2342 case 0x004000000000ULL
:
2345 case 0x010000000000ULL
:
2357 /* Helpers for 4xx TLB management */
2358 #define PPC4XX_TLB_ENTRY_MASK 0x0000003f /* Mask for 64 TLB entries */
2360 #define PPC4XX_TLBHI_V 0x00000040
2361 #define PPC4XX_TLBHI_E 0x00000020
2362 #define PPC4XX_TLBHI_SIZE_MIN 0
2363 #define PPC4XX_TLBHI_SIZE_MAX 7
2364 #define PPC4XX_TLBHI_SIZE_DEFAULT 1
2365 #define PPC4XX_TLBHI_SIZE_SHIFT 7
2366 #define PPC4XX_TLBHI_SIZE_MASK 0x00000007
2368 #define PPC4XX_TLBLO_EX 0x00000200
2369 #define PPC4XX_TLBLO_WR 0x00000100
2370 #define PPC4XX_TLBLO_ATTR_MASK 0x000000FF
2371 #define PPC4XX_TLBLO_RPN_MASK 0xFFFFFC00
2373 target_ulong
helper_4xx_tlbre_hi(CPUPPCState
*env
, target_ulong entry
)
2379 entry
&= PPC4XX_TLB_ENTRY_MASK
;
2380 tlb
= &env
->tlb
.tlbe
[entry
];
2382 if (tlb
->prot
& PAGE_VALID
) {
2383 ret
|= PPC4XX_TLBHI_V
;
2385 size
= booke_page_size_to_tlb(tlb
->size
);
2386 if (size
< PPC4XX_TLBHI_SIZE_MIN
|| size
> PPC4XX_TLBHI_SIZE_MAX
) {
2387 size
= PPC4XX_TLBHI_SIZE_DEFAULT
;
2389 ret
|= size
<< PPC4XX_TLBHI_SIZE_SHIFT
;
2390 env
->spr
[SPR_40x_PID
] = tlb
->PID
;
2394 target_ulong
helper_4xx_tlbre_lo(CPUPPCState
*env
, target_ulong entry
)
2399 entry
&= PPC4XX_TLB_ENTRY_MASK
;
2400 tlb
= &env
->tlb
.tlbe
[entry
];
2402 if (tlb
->prot
& PAGE_EXEC
) {
2403 ret
|= PPC4XX_TLBLO_EX
;
2405 if (tlb
->prot
& PAGE_WRITE
) {
2406 ret
|= PPC4XX_TLBLO_WR
;
2411 void helper_4xx_tlbwe_hi(CPUPPCState
*env
, target_ulong entry
,
2414 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2415 CPUState
*cs
= CPU(cpu
);
2417 target_ulong page
, end
;
2419 LOG_SWTLB("%s entry %d val " TARGET_FMT_lx
"\n", __func__
, (int)entry
,
2421 entry
&= PPC4XX_TLB_ENTRY_MASK
;
2422 tlb
= &env
->tlb
.tlbe
[entry
];
2423 /* Invalidate previous TLB (if it's valid) */
2424 if (tlb
->prot
& PAGE_VALID
) {
2425 end
= tlb
->EPN
+ tlb
->size
;
2426 LOG_SWTLB("%s: invalidate old TLB %d start " TARGET_FMT_lx
" end "
2427 TARGET_FMT_lx
"\n", __func__
, (int)entry
, tlb
->EPN
, end
);
2428 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
) {
2429 tlb_flush_page(cs
, page
);
2432 tlb
->size
= booke_tlb_to_page_size((val
>> PPC4XX_TLBHI_SIZE_SHIFT
)
2433 & PPC4XX_TLBHI_SIZE_MASK
);
2434 /* We cannot handle TLB size < TARGET_PAGE_SIZE.
2435 * If this ever occurs, we should implement TARGET_PAGE_BITS_VARY
2437 if ((val
& PPC4XX_TLBHI_V
) && tlb
->size
< TARGET_PAGE_SIZE
) {
2438 cpu_abort(cs
, "TLB size " TARGET_FMT_lu
" < %u "
2439 "are not supported (%d)\n"
2440 "Please implement TARGET_PAGE_BITS_VARY\n",
2441 tlb
->size
, TARGET_PAGE_SIZE
, (int)((val
>> 7) & 0x7));
2443 tlb
->EPN
= val
& ~(tlb
->size
- 1);
2444 if (val
& PPC4XX_TLBHI_V
) {
2445 tlb
->prot
|= PAGE_VALID
;
2446 if (val
& PPC4XX_TLBHI_E
) {
2447 /* XXX: TO BE FIXED */
2449 "Little-endian TLB entries are not supported by now\n");
2452 tlb
->prot
&= ~PAGE_VALID
;
2454 tlb
->PID
= env
->spr
[SPR_40x_PID
]; /* PID */
2455 LOG_SWTLB("%s: set up TLB %d RPN " TARGET_FMT_plx
" EPN " TARGET_FMT_lx
2456 " size " TARGET_FMT_lx
" prot %c%c%c%c PID %d\n", __func__
,
2457 (int)entry
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2458 tlb
->prot
& PAGE_READ
? 'r' : '-',
2459 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2460 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2461 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2462 /* Invalidate new TLB (if valid) */
2463 if (tlb
->prot
& PAGE_VALID
) {
2464 end
= tlb
->EPN
+ tlb
->size
;
2465 LOG_SWTLB("%s: invalidate TLB %d start " TARGET_FMT_lx
" end "
2466 TARGET_FMT_lx
"\n", __func__
, (int)entry
, tlb
->EPN
, end
);
2467 for (page
= tlb
->EPN
; page
< end
; page
+= TARGET_PAGE_SIZE
) {
2468 tlb_flush_page(cs
, page
);
2473 void helper_4xx_tlbwe_lo(CPUPPCState
*env
, target_ulong entry
,
2478 LOG_SWTLB("%s entry %i val " TARGET_FMT_lx
"\n", __func__
, (int)entry
,
2480 entry
&= PPC4XX_TLB_ENTRY_MASK
;
2481 tlb
= &env
->tlb
.tlbe
[entry
];
2482 tlb
->attr
= val
& PPC4XX_TLBLO_ATTR_MASK
;
2483 tlb
->RPN
= val
& PPC4XX_TLBLO_RPN_MASK
;
2484 tlb
->prot
= PAGE_READ
;
2485 if (val
& PPC4XX_TLBLO_EX
) {
2486 tlb
->prot
|= PAGE_EXEC
;
2488 if (val
& PPC4XX_TLBLO_WR
) {
2489 tlb
->prot
|= PAGE_WRITE
;
2491 LOG_SWTLB("%s: set up TLB %d RPN " TARGET_FMT_plx
" EPN " TARGET_FMT_lx
2492 " size " TARGET_FMT_lx
" prot %c%c%c%c PID %d\n", __func__
,
2493 (int)entry
, tlb
->RPN
, tlb
->EPN
, tlb
->size
,
2494 tlb
->prot
& PAGE_READ
? 'r' : '-',
2495 tlb
->prot
& PAGE_WRITE
? 'w' : '-',
2496 tlb
->prot
& PAGE_EXEC
? 'x' : '-',
2497 tlb
->prot
& PAGE_VALID
? 'v' : '-', (int)tlb
->PID
);
2500 target_ulong
helper_4xx_tlbsx(CPUPPCState
*env
, target_ulong address
)
2502 return ppcemb_tlb_search(env
, address
, env
->spr
[SPR_40x_PID
]);
2505 /* PowerPC 440 TLB management */
2506 void helper_440_tlbwe(CPUPPCState
*env
, uint32_t word
, target_ulong entry
,
2509 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2511 target_ulong EPN
, RPN
, size
;
2514 LOG_SWTLB("%s word %d entry %d value " TARGET_FMT_lx
"\n",
2515 __func__
, word
, (int)entry
, value
);
2518 tlb
= &env
->tlb
.tlbe
[entry
];
2521 /* Just here to please gcc */
2523 EPN
= value
& 0xFFFFFC00;
2524 if ((tlb
->prot
& PAGE_VALID
) && EPN
!= tlb
->EPN
) {
2528 size
= booke_tlb_to_page_size((value
>> 4) & 0xF);
2529 if ((tlb
->prot
& PAGE_VALID
) && tlb
->size
< size
) {
2534 tlb
->attr
|= (value
>> 8) & 1;
2535 if (value
& 0x200) {
2536 tlb
->prot
|= PAGE_VALID
;
2538 if (tlb
->prot
& PAGE_VALID
) {
2539 tlb
->prot
&= ~PAGE_VALID
;
2543 tlb
->PID
= env
->spr
[SPR_440_MMUCR
] & 0x000000FF;
2544 if (do_flush_tlbs
) {
2545 tlb_flush(CPU(cpu
));
2549 RPN
= value
& 0xFFFFFC0F;
2550 if ((tlb
->prot
& PAGE_VALID
) && tlb
->RPN
!= RPN
) {
2551 tlb_flush(CPU(cpu
));
2556 tlb
->attr
= (tlb
->attr
& 0x1) | (value
& 0x0000FF00);
2557 tlb
->prot
= tlb
->prot
& PAGE_VALID
;
2559 tlb
->prot
|= PAGE_READ
<< 4;
2562 tlb
->prot
|= PAGE_WRITE
<< 4;
2565 tlb
->prot
|= PAGE_EXEC
<< 4;
2568 tlb
->prot
|= PAGE_READ
;
2571 tlb
->prot
|= PAGE_WRITE
;
2574 tlb
->prot
|= PAGE_EXEC
;
2580 target_ulong
helper_440_tlbre(CPUPPCState
*env
, uint32_t word
,
2588 tlb
= &env
->tlb
.tlbe
[entry
];
2591 /* Just here to please gcc */
2594 size
= booke_page_size_to_tlb(tlb
->size
);
2595 if (size
< 0 || size
> 0xF) {
2599 if (tlb
->attr
& 0x1) {
2602 if (tlb
->prot
& PAGE_VALID
) {
2605 env
->spr
[SPR_440_MMUCR
] &= ~0x000000FF;
2606 env
->spr
[SPR_440_MMUCR
] |= tlb
->PID
;
2612 ret
= tlb
->attr
& ~0x1;
2613 if (tlb
->prot
& (PAGE_READ
<< 4)) {
2616 if (tlb
->prot
& (PAGE_WRITE
<< 4)) {
2619 if (tlb
->prot
& (PAGE_EXEC
<< 4)) {
2622 if (tlb
->prot
& PAGE_READ
) {
2625 if (tlb
->prot
& PAGE_WRITE
) {
2628 if (tlb
->prot
& PAGE_EXEC
) {
2636 target_ulong
helper_440_tlbsx(CPUPPCState
*env
, target_ulong address
)
2638 return ppcemb_tlb_search(env
, address
, env
->spr
[SPR_440_MMUCR
] & 0xFF);
2641 /* PowerPC BookE 2.06 TLB management */
2643 static ppcmas_tlb_t
*booke206_cur_tlb(CPUPPCState
*env
)
2645 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2646 uint32_t tlbncfg
= 0;
2647 int esel
= (env
->spr
[SPR_BOOKE_MAS0
] & MAS0_ESEL_MASK
) >> MAS0_ESEL_SHIFT
;
2648 int ea
= (env
->spr
[SPR_BOOKE_MAS2
] & MAS2_EPN_MASK
);
2651 tlb
= (env
->spr
[SPR_BOOKE_MAS0
] & MAS0_TLBSEL_MASK
) >> MAS0_TLBSEL_SHIFT
;
2652 tlbncfg
= env
->spr
[SPR_BOOKE_TLB0CFG
+ tlb
];
2654 if ((tlbncfg
& TLBnCFG_HES
) && (env
->spr
[SPR_BOOKE_MAS0
] & MAS0_HES
)) {
2655 cpu_abort(CPU(cpu
), "we don't support HES yet\n");
2658 return booke206_get_tlbm(env
, tlb
, ea
, esel
);
2661 void helper_booke_setpid(CPUPPCState
*env
, uint32_t pidn
, target_ulong pid
)
2663 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2665 env
->spr
[pidn
] = pid
;
2666 /* changing PIDs mean we're in a different address space now */
2667 tlb_flush(CPU(cpu
));
2670 void helper_booke_set_eplc(CPUPPCState
*env
, target_ulong val
)
2672 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2673 env
->spr
[SPR_BOOKE_EPLC
] = val
& EPID_MASK
;
2674 tlb_flush_by_mmuidx(CPU(cpu
), 1 << PPC_TLB_EPID_LOAD
);
2676 void helper_booke_set_epsc(CPUPPCState
*env
, target_ulong val
)
2678 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2679 env
->spr
[SPR_BOOKE_EPSC
] = val
& EPID_MASK
;
2680 tlb_flush_by_mmuidx(CPU(cpu
), 1 << PPC_TLB_EPID_STORE
);
2683 static inline void flush_page(CPUPPCState
*env
, ppcmas_tlb_t
*tlb
)
2685 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2687 if (booke206_tlb_to_page_size(env
, tlb
) == TARGET_PAGE_SIZE
) {
2688 tlb_flush_page(CPU(cpu
), tlb
->mas2
& MAS2_EPN_MASK
);
2690 tlb_flush(CPU(cpu
));
2694 void helper_booke206_tlbwe(CPUPPCState
*env
)
2696 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2697 uint32_t tlbncfg
, tlbn
;
2699 uint32_t size_tlb
, size_ps
;
2703 switch (env
->spr
[SPR_BOOKE_MAS0
] & MAS0_WQ_MASK
) {
2704 case MAS0_WQ_ALWAYS
:
2705 /* good to go, write that entry */
2708 /* XXX check if reserved */
2713 case MAS0_WQ_CLR_RSRV
:
2714 /* XXX clear entry */
2717 /* no idea what to do */
2721 if (((env
->spr
[SPR_BOOKE_MAS0
] & MAS0_ATSEL
) == MAS0_ATSEL_LRAT
) &&
2723 /* XXX we don't support direct LRAT setting yet */
2724 fprintf(stderr
, "cpu: don't support LRAT setting yet\n");
2728 tlbn
= (env
->spr
[SPR_BOOKE_MAS0
] & MAS0_TLBSEL_MASK
) >> MAS0_TLBSEL_SHIFT
;
2729 tlbncfg
= env
->spr
[SPR_BOOKE_TLB0CFG
+ tlbn
];
2731 tlb
= booke206_cur_tlb(env
);
2734 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
2735 POWERPC_EXCP_INVAL
|
2736 POWERPC_EXCP_INVAL_INVAL
, GETPC());
2739 /* check that we support the targeted size */
2740 size_tlb
= (env
->spr
[SPR_BOOKE_MAS1
] & MAS1_TSIZE_MASK
) >> MAS1_TSIZE_SHIFT
;
2741 size_ps
= booke206_tlbnps(env
, tlbn
);
2742 if ((env
->spr
[SPR_BOOKE_MAS1
] & MAS1_VALID
) && (tlbncfg
& TLBnCFG_AVAIL
) &&
2743 !(size_ps
& (1 << size_tlb
))) {
2744 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
2745 POWERPC_EXCP_INVAL
|
2746 POWERPC_EXCP_INVAL_INVAL
, GETPC());
2750 cpu_abort(CPU(cpu
), "missing HV implementation\n");
2753 if (tlb
->mas1
& MAS1_VALID
) {
2754 /* Invalidate the page in QEMU TLB if it was a valid entry.
2756 * In "PowerPC e500 Core Family Reference Manual, Rev. 1",
2757 * Section "12.4.2 TLB Write Entry (tlbwe) Instruction":
2758 * (https://www.nxp.com/docs/en/reference-manual/E500CORERM.pdf)
2760 * "Note that when an L2 TLB entry is written, it may be displacing an
2761 * already valid entry in the same L2 TLB location (a victim). If a
2762 * valid L1 TLB entry corresponds to the L2 MMU victim entry, that L1
2763 * TLB entry is automatically invalidated." */
2764 flush_page(env
, tlb
);
2767 tlb
->mas7_3
= ((uint64_t)env
->spr
[SPR_BOOKE_MAS7
] << 32) |
2768 env
->spr
[SPR_BOOKE_MAS3
];
2769 tlb
->mas1
= env
->spr
[SPR_BOOKE_MAS1
];
2771 if ((env
->spr
[SPR_MMUCFG
] & MMUCFG_MAVN
) == MMUCFG_MAVN_V2
) {
2772 /* For TLB which has a fixed size TSIZE is ignored with MAV2 */
2773 booke206_fixed_size_tlbn(env
, tlbn
, tlb
);
2775 if (!(tlbncfg
& TLBnCFG_AVAIL
)) {
2776 /* force !AVAIL TLB entries to correct page size */
2777 tlb
->mas1
&= ~MAS1_TSIZE_MASK
;
2778 /* XXX can be configured in MMUCSR0 */
2779 tlb
->mas1
|= (tlbncfg
& TLBnCFG_MINSIZE
) >> 12;
2783 /* Make a mask from TLB size to discard invalid bits in EPN field */
2784 mask
= ~(booke206_tlb_to_page_size(env
, tlb
) - 1);
2785 /* Add a mask for page attributes */
2786 mask
|= MAS2_ACM
| MAS2_VLE
| MAS2_W
| MAS2_I
| MAS2_M
| MAS2_G
| MAS2_E
;
2789 /* Executing a tlbwe instruction in 32-bit mode will set
2790 * bits 0:31 of the TLB EPN field to zero.
2795 tlb
->mas2
= env
->spr
[SPR_BOOKE_MAS2
] & mask
;
2797 if (!(tlbncfg
& TLBnCFG_IPROT
)) {
2798 /* no IPROT supported by TLB */
2799 tlb
->mas1
&= ~MAS1_IPROT
;
2802 flush_page(env
, tlb
);
2805 static inline void booke206_tlb_to_mas(CPUPPCState
*env
, ppcmas_tlb_t
*tlb
)
2807 int tlbn
= booke206_tlbm_to_tlbn(env
, tlb
);
2808 int way
= booke206_tlbm_to_way(env
, tlb
);
2810 env
->spr
[SPR_BOOKE_MAS0
] = tlbn
<< MAS0_TLBSEL_SHIFT
;
2811 env
->spr
[SPR_BOOKE_MAS0
] |= way
<< MAS0_ESEL_SHIFT
;
2812 env
->spr
[SPR_BOOKE_MAS0
] |= env
->last_way
<< MAS0_NV_SHIFT
;
2814 env
->spr
[SPR_BOOKE_MAS1
] = tlb
->mas1
;
2815 env
->spr
[SPR_BOOKE_MAS2
] = tlb
->mas2
;
2816 env
->spr
[SPR_BOOKE_MAS3
] = tlb
->mas7_3
;
2817 env
->spr
[SPR_BOOKE_MAS7
] = tlb
->mas7_3
>> 32;
2820 void helper_booke206_tlbre(CPUPPCState
*env
)
2822 ppcmas_tlb_t
*tlb
= NULL
;
2824 tlb
= booke206_cur_tlb(env
);
2826 env
->spr
[SPR_BOOKE_MAS1
] = 0;
2828 booke206_tlb_to_mas(env
, tlb
);
2832 void helper_booke206_tlbsx(CPUPPCState
*env
, target_ulong address
)
2834 ppcmas_tlb_t
*tlb
= NULL
;
2839 spid
= (env
->spr
[SPR_BOOKE_MAS6
] & MAS6_SPID_MASK
) >> MAS6_SPID_SHIFT
;
2840 sas
= env
->spr
[SPR_BOOKE_MAS6
] & MAS6_SAS
;
2842 for (i
= 0; i
< BOOKE206_MAX_TLBN
; i
++) {
2843 int ways
= booke206_tlb_ways(env
, i
);
2845 for (j
= 0; j
< ways
; j
++) {
2846 tlb
= booke206_get_tlbm(env
, i
, address
, j
);
2852 if (ppcmas_tlb_check(env
, tlb
, &raddr
, address
, spid
)) {
2856 if (sas
!= ((tlb
->mas1
& MAS1_TS
) >> MAS1_TS_SHIFT
)) {
2860 booke206_tlb_to_mas(env
, tlb
);
2865 /* no entry found, fill with defaults */
2866 env
->spr
[SPR_BOOKE_MAS0
] = env
->spr
[SPR_BOOKE_MAS4
] & MAS4_TLBSELD_MASK
;
2867 env
->spr
[SPR_BOOKE_MAS1
] = env
->spr
[SPR_BOOKE_MAS4
] & MAS4_TSIZED_MASK
;
2868 env
->spr
[SPR_BOOKE_MAS2
] = env
->spr
[SPR_BOOKE_MAS4
] & MAS4_WIMGED_MASK
;
2869 env
->spr
[SPR_BOOKE_MAS3
] = 0;
2870 env
->spr
[SPR_BOOKE_MAS7
] = 0;
2872 if (env
->spr
[SPR_BOOKE_MAS6
] & MAS6_SAS
) {
2873 env
->spr
[SPR_BOOKE_MAS1
] |= MAS1_TS
;
2876 env
->spr
[SPR_BOOKE_MAS1
] |= (env
->spr
[SPR_BOOKE_MAS6
] >> 16)
2879 /* next victim logic */
2880 env
->spr
[SPR_BOOKE_MAS0
] |= env
->last_way
<< MAS0_ESEL_SHIFT
;
2882 env
->last_way
&= booke206_tlb_ways(env
, 0) - 1;
2883 env
->spr
[SPR_BOOKE_MAS0
] |= env
->last_way
<< MAS0_NV_SHIFT
;
2886 static inline void booke206_invalidate_ea_tlb(CPUPPCState
*env
, int tlbn
,
2890 int ways
= booke206_tlb_ways(env
, tlbn
);
2893 for (i
= 0; i
< ways
; i
++) {
2894 ppcmas_tlb_t
*tlb
= booke206_get_tlbm(env
, tlbn
, ea
, i
);
2898 mask
= ~(booke206_tlb_to_page_size(env
, tlb
) - 1);
2899 if (((tlb
->mas2
& MAS2_EPN_MASK
) == (ea
& mask
)) &&
2900 !(tlb
->mas1
& MAS1_IPROT
)) {
2901 tlb
->mas1
&= ~MAS1_VALID
;
2906 void helper_booke206_tlbivax(CPUPPCState
*env
, target_ulong address
)
2910 if (address
& 0x4) {
2911 /* flush all entries */
2912 if (address
& 0x8) {
2913 /* flush all of TLB1 */
2914 booke206_flush_tlb(env
, BOOKE206_FLUSH_TLB1
, 1);
2916 /* flush all of TLB0 */
2917 booke206_flush_tlb(env
, BOOKE206_FLUSH_TLB0
, 0);
2922 if (address
& 0x8) {
2923 /* flush TLB1 entries */
2924 booke206_invalidate_ea_tlb(env
, 1, address
);
2929 /* flush TLB0 entries */
2930 booke206_invalidate_ea_tlb(env
, 0, address
);
2932 tlb_flush_page(cs
, address
& MAS2_EPN_MASK
);
2937 void helper_booke206_tlbilx0(CPUPPCState
*env
, target_ulong address
)
2939 /* XXX missing LPID handling */
2940 booke206_flush_tlb(env
, -1, 1);
2943 void helper_booke206_tlbilx1(CPUPPCState
*env
, target_ulong address
)
2945 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2947 int tid
= (env
->spr
[SPR_BOOKE_MAS6
] & MAS6_SPID
);
2948 ppcmas_tlb_t
*tlb
= env
->tlb
.tlbm
;
2951 /* XXX missing LPID handling */
2952 for (i
= 0; i
< BOOKE206_MAX_TLBN
; i
++) {
2953 tlb_size
= booke206_tlb_size(env
, i
);
2954 for (j
= 0; j
< tlb_size
; j
++) {
2955 if (!(tlb
[j
].mas1
& MAS1_IPROT
) &&
2956 ((tlb
[j
].mas1
& MAS1_TID_MASK
) == tid
)) {
2957 tlb
[j
].mas1
&= ~MAS1_VALID
;
2960 tlb
+= booke206_tlb_size(env
, i
);
2962 tlb_flush(CPU(cpu
));
2965 void helper_booke206_tlbilx3(CPUPPCState
*env
, target_ulong address
)
2967 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
2970 int tid
= (env
->spr
[SPR_BOOKE_MAS6
] & MAS6_SPID
);
2971 int pid
= tid
>> MAS6_SPID_SHIFT
;
2972 int sgs
= env
->spr
[SPR_BOOKE_MAS5
] & MAS5_SGS
;
2973 int ind
= (env
->spr
[SPR_BOOKE_MAS6
] & MAS6_SIND
) ? MAS1_IND
: 0;
2974 /* XXX check for unsupported isize and raise an invalid opcode then */
2975 int size
= env
->spr
[SPR_BOOKE_MAS6
] & MAS6_ISIZE_MASK
;
2976 /* XXX implement MAV2 handling */
2979 /* XXX missing LPID handling */
2980 /* flush by pid and ea */
2981 for (i
= 0; i
< BOOKE206_MAX_TLBN
; i
++) {
2982 int ways
= booke206_tlb_ways(env
, i
);
2984 for (j
= 0; j
< ways
; j
++) {
2985 tlb
= booke206_get_tlbm(env
, i
, address
, j
);
2989 if ((ppcmas_tlb_check(env
, tlb
, NULL
, address
, pid
) != 0) ||
2990 (tlb
->mas1
& MAS1_IPROT
) ||
2991 ((tlb
->mas1
& MAS1_IND
) != ind
) ||
2992 ((tlb
->mas8
& MAS8_TGS
) != sgs
)) {
2995 if (mav2
&& ((tlb
->mas1
& MAS1_TSIZE_MASK
) != size
)) {
2996 /* XXX only check when MMUCFG[TWC] || TLBnCFG[HES] */
2999 /* XXX e500mc doesn't match SAS, but other cores might */
3000 tlb
->mas1
&= ~MAS1_VALID
;
3003 tlb_flush(CPU(cpu
));
3006 void helper_booke206_tlbflush(CPUPPCState
*env
, target_ulong type
)
3011 flags
|= BOOKE206_FLUSH_TLB1
;
3015 flags
|= BOOKE206_FLUSH_TLB0
;
3018 booke206_flush_tlb(env
, flags
, 1);
3022 void helper_check_tlb_flush_local(CPUPPCState
*env
)
3024 check_tlb_flush(env
, false);
3027 void helper_check_tlb_flush_global(CPUPPCState
*env
)
3029 check_tlb_flush(env
, true);
3032 /*****************************************************************************/
3034 /* try to fill the TLB and return an exception if error. If retaddr is
3035 NULL, it means that the function was called in C code (i.e. not
3036 from generated code or from helper.c) */
3037 /* XXX: fix it to restore all registers */
3038 void tlb_fill(CPUState
*cs
, target_ulong addr
, int size
,
3039 MMUAccessType access_type
, int mmu_idx
, uintptr_t retaddr
)
3041 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
3042 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cs
);
3043 CPUPPCState
*env
= &cpu
->env
;
3046 if (pcc
->handle_mmu_fault
) {
3047 ret
= pcc
->handle_mmu_fault(cpu
, addr
, access_type
, mmu_idx
);
3049 ret
= cpu_ppc_handle_mmu_fault(env
, addr
, access_type
, mmu_idx
);
3051 if (unlikely(ret
!= 0)) {
3052 raise_exception_err_ra(env
, cs
->exception_index
, env
->error_code
,