2 * PowerPC MMU, TLB, SLB and BAT emulation helpers for QEMU.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
5 * Copyright (c) 2013 David Gibson, IBM Corporation
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "sysemu/kvm.h"
24 #include "mmu-hash64.h"
30 # define LOG_MMU(...) qemu_log(__VA_ARGS__)
31 # define LOG_MMU_STATE(env) log_cpu_state((env), 0)
33 # define LOG_MMU(...) do { } while (0)
34 # define LOG_MMU_STATE(...) do { } while (0)
38 # define LOG_SLB(...) qemu_log(__VA_ARGS__)
40 # define LOG_SLB(...) do { } while (0)
43 struct mmu_ctx_hash64
{
44 hwaddr raddr
; /* Real address */
45 int prot
; /* Protection bits */
46 hwaddr hash
[2]; /* Pagetable hash values */
47 target_ulong ptem
; /* Virtual segment ID | API */
48 int key
; /* Access key */
55 static ppc_slb_t
*slb_lookup(CPUPPCState
*env
, target_ulong eaddr
)
57 uint64_t esid_256M
, esid_1T
;
60 LOG_SLB("%s: eaddr " TARGET_FMT_lx
"\n", __func__
, eaddr
);
62 esid_256M
= (eaddr
& SEGMENT_MASK_256M
) | SLB_ESID_V
;
63 esid_1T
= (eaddr
& SEGMENT_MASK_1T
) | SLB_ESID_V
;
65 for (n
= 0; n
< env
->slb_nr
; n
++) {
66 ppc_slb_t
*slb
= &env
->slb
[n
];
68 LOG_SLB("%s: slot %d %016" PRIx64
" %016"
69 PRIx64
"\n", __func__
, n
, slb
->esid
, slb
->vsid
);
70 /* We check for 1T matches on all MMUs here - if the MMU
71 * doesn't have 1T segment support, we will have prevented 1T
72 * entries from being inserted in the slbmte code. */
73 if (((slb
->esid
== esid_256M
) &&
74 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_256M
))
75 || ((slb
->esid
== esid_1T
) &&
76 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_1T
))) {
84 void dump_slb(FILE *f
, fprintf_function cpu_fprintf
, CPUPPCState
*env
)
89 cpu_synchronize_state(env
);
91 cpu_fprintf(f
, "SLB\tESID\t\t\tVSID\n");
92 for (i
= 0; i
< env
->slb_nr
; i
++) {
93 slbe
= env
->slb
[i
].esid
;
94 slbv
= env
->slb
[i
].vsid
;
95 if (slbe
== 0 && slbv
== 0) {
98 cpu_fprintf(f
, "%d\t0x%016" PRIx64
"\t0x%016" PRIx64
"\n",
103 void helper_slbia(CPUPPCState
*env
)
105 int n
, do_invalidate
;
108 /* XXX: Warning: slbia never invalidates the first segment */
109 for (n
= 1; n
< env
->slb_nr
; n
++) {
110 ppc_slb_t
*slb
= &env
->slb
[n
];
112 if (slb
->esid
& SLB_ESID_V
) {
113 slb
->esid
&= ~SLB_ESID_V
;
114 /* XXX: given the fact that segment size is 256 MB or 1TB,
115 * and we still don't have a tlb_flush_mask(env, n, mask)
116 * in QEMU, we just invalidate all TLBs
126 void helper_slbie(CPUPPCState
*env
, target_ulong addr
)
130 slb
= slb_lookup(env
, addr
);
135 if (slb
->esid
& SLB_ESID_V
) {
136 slb
->esid
&= ~SLB_ESID_V
;
138 /* XXX: given the fact that segment size is 256 MB or 1TB,
139 * and we still don't have a tlb_flush_mask(env, n, mask)
140 * in QEMU, we just invalidate all TLBs
146 int ppc_store_slb(CPUPPCState
*env
, target_ulong rb
, target_ulong rs
)
148 int slot
= rb
& 0xfff;
149 ppc_slb_t
*slb
= &env
->slb
[slot
];
151 if (rb
& (0x1000 - env
->slb_nr
)) {
152 return -1; /* Reserved bits set or slot too high */
154 if (rs
& (SLB_VSID_B
& ~SLB_VSID_B_1T
)) {
155 return -1; /* Bad segment size */
157 if ((rs
& SLB_VSID_B
) && !(env
->mmu_model
& POWERPC_MMU_1TSEG
)) {
158 return -1; /* 1T segment on MMU that doesn't support it */
161 /* Mask out the slot number as we store the entry */
162 slb
->esid
= rb
& (SLB_ESID_ESID
| SLB_ESID_V
);
165 LOG_SLB("%s: %d " TARGET_FMT_lx
" - " TARGET_FMT_lx
" => %016" PRIx64
166 " %016" PRIx64
"\n", __func__
, slot
, rb
, rs
,
167 slb
->esid
, slb
->vsid
);
172 static int ppc_load_slb_esid(CPUPPCState
*env
, target_ulong rb
,
175 int slot
= rb
& 0xfff;
176 ppc_slb_t
*slb
= &env
->slb
[slot
];
178 if (slot
>= env
->slb_nr
) {
186 static int ppc_load_slb_vsid(CPUPPCState
*env
, target_ulong rb
,
189 int slot
= rb
& 0xfff;
190 ppc_slb_t
*slb
= &env
->slb
[slot
];
192 if (slot
>= env
->slb_nr
) {
200 void helper_store_slb(CPUPPCState
*env
, target_ulong rb
, target_ulong rs
)
202 if (ppc_store_slb(env
, rb
, rs
) < 0) {
203 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
208 target_ulong
helper_load_slb_esid(CPUPPCState
*env
, target_ulong rb
)
212 if (ppc_load_slb_esid(env
, rb
, &rt
) < 0) {
213 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
219 target_ulong
helper_load_slb_vsid(CPUPPCState
*env
, target_ulong rb
)
223 if (ppc_load_slb_vsid(env
, rb
, &rt
) < 0) {
224 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
231 * 64-bit hash table MMU handling
234 #define PTE64_CHECK_MASK (TARGET_PAGE_MASK | 0x7F)
236 static int ppc_hash64_pp_check(int key
, int pp
, bool nx
)
240 /* Compute access rights */
241 /* When pp is 4, 5 or 7, the result is undefined. Set it to noaccess */
248 access
|= PAGE_WRITE
;
266 access
= PAGE_READ
| PAGE_WRITE
;
277 static int ppc_hash64_check_prot(int prot
, int rwx
)
282 if (prot
& PAGE_EXEC
) {
287 } else if (rwx
== 1) {
288 if (prot
& PAGE_WRITE
) {
294 if (prot
& PAGE_READ
) {
304 static int pte64_check(struct mmu_ctx_hash64
*ctx
, target_ulong pte0
,
305 target_ulong pte1
, int h
, int rwx
)
311 /* Check validity and table match */
312 if ((pte0
& HPTE64_V_VALID
) && (h
== !!(pte0
& HPTE64_V_SECONDARY
))) {
315 /* Check vsid & api */
316 mmask
= PTE64_CHECK_MASK
;
317 pp
= (pte1
& HPTE64_R_PP
) | ((pte1
& HPTE64_R_PP0
) >> 61);
318 /* No execute if either noexec or guarded bits set */
319 nx
= (pte1
& HPTE64_R_N
) || (pte1
& HPTE64_R_G
);
320 if (HPTE64_V_COMPARE(pte0
, ctx
->ptem
)) {
321 if (ctx
->raddr
!= (hwaddr
)-1ULL) {
322 /* all matches should have equal RPN, WIMG & PP */
323 if ((ctx
->raddr
& mmask
) != (pte1
& mmask
)) {
324 qemu_log("Bad RPN/WIMG/PP\n");
328 /* Compute access rights */
329 access
= ppc_hash64_pp_check(ctx
->key
, pp
, nx
);
330 /* Keep the matching PTE informations */
333 ret
= ppc_hash64_check_prot(ctx
->prot
, rwx
);
336 LOG_MMU("PTE access granted !\n");
338 /* Access right violation */
339 LOG_MMU("PTE access rejected\n");
347 static int ppc_hash64_pte_update_flags(struct mmu_ctx_hash64
*ctx
,
353 /* Update page flags */
354 if (!(*pte1p
& HPTE64_R_R
)) {
355 /* Update accessed flag */
356 *pte1p
|= HPTE64_R_R
;
359 if (!(*pte1p
& HPTE64_R_C
)) {
360 if (rw
== 1 && ret
== 0) {
361 /* Update changed flag */
362 *pte1p
|= HPTE64_R_C
;
365 /* Force page fault for first write access */
366 ctx
->prot
&= ~PAGE_WRITE
;
373 /* PTE table lookup */
374 static int find_pte64(CPUPPCState
*env
, struct mmu_ctx_hash64
*ctx
,
375 target_ulong eaddr
, int h
, int rwx
, int target_page_bits
)
378 target_ulong pte0
, pte1
;
382 ret
= -1; /* No entry found */
383 pteg_off
= (ctx
->hash
[h
] * HASH_PTEG_SIZE_64
) & env
->htab_mask
;
384 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
385 pte0
= ppc_hash64_load_hpte0(env
, pteg_off
+ i
*HASH_PTE_SIZE_64
);
386 pte1
= ppc_hash64_load_hpte1(env
, pteg_off
+ i
*HASH_PTE_SIZE_64
);
388 r
= pte64_check(ctx
, pte0
, pte1
, h
, rwx
);
389 LOG_MMU("Load pte from %016" HWADDR_PRIx
" => " TARGET_FMT_lx
" "
390 TARGET_FMT_lx
" %d %d %d " TARGET_FMT_lx
"\n",
391 pteg_off
+ (i
* 16), pte0
, pte1
, (int)(pte0
& 1), h
,
392 (int)((pte0
>> 1) & 1), ctx
->ptem
);
395 /* PTE inconsistency */
398 /* Access violation */
408 /* XXX: we should go on looping to check all PTEs consistency
409 * but if we can speed-up the whole thing as the
410 * result would be undefined if PTEs are not consistent.
419 LOG_MMU("found PTE at addr %08" HWADDR_PRIx
" prot=%01x ret=%d\n",
420 ctx
->raddr
, ctx
->prot
, ret
);
421 /* Update page flags */
423 if (ppc_hash64_pte_update_flags(ctx
, &pte1
, ret
, rwx
) == 1) {
424 ppc_hash64_store_hpte1(env
, pteg_off
+ good
* HASH_PTE_SIZE_64
, pte1
);
428 /* We have a TLB that saves 4K pages, so let's
429 * split a huge page to 4k chunks */
430 if (target_page_bits
!= TARGET_PAGE_BITS
) {
431 ctx
->raddr
|= (eaddr
& ((1 << target_page_bits
) - 1))
437 static int ppc_hash64_translate(CPUPPCState
*env
, struct mmu_ctx_hash64
*ctx
,
438 target_ulong eaddr
, int rwx
)
442 int pr
, target_page_bits
;
445 target_ulong pageaddr
;
448 /* 1. Handle real mode accesses */
449 if (((rwx
== 2) && (msr_ir
== 0)) || ((rwx
!= 2) && (msr_dr
== 0))) {
450 /* Translation is off */
451 /* In real mode the top 4 effective address bits are ignored */
452 ctx
->raddr
= eaddr
& 0x0FFFFFFFFFFFFFFFULL
;
453 ctx
->prot
= PAGE_READ
| PAGE_EXEC
| PAGE_WRITE
;
459 LOG_MMU("Check SLBs\n");
460 slb
= slb_lookup(env
, eaddr
);
465 if (slb
->vsid
& SLB_VSID_B
) {
466 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT_1T
;
469 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT
;
473 target_page_bits
= (slb
->vsid
& SLB_VSID_L
)
474 ? TARGET_PAGE_BITS_16M
: TARGET_PAGE_BITS
;
475 ctx
->key
= !!(pr
? (slb
->vsid
& SLB_VSID_KP
)
476 : (slb
->vsid
& SLB_VSID_KS
));
478 pageaddr
= eaddr
& ((1ULL << segment_bits
)
479 - (1ULL << target_page_bits
));
480 if (slb
->vsid
& SLB_VSID_B
) {
481 hash
= vsid
^ (vsid
<< 25) ^ (pageaddr
>> target_page_bits
);
483 hash
= vsid
^ (pageaddr
>> target_page_bits
);
485 /* Only 5 bits of the page index are used in the AVPN */
486 ctx
->ptem
= (slb
->vsid
& SLB_VSID_PTEM
) |
487 ((pageaddr
>> 16) & ((1ULL << segment_bits
) - 0x80));
489 LOG_MMU("pte segment: key=%d nx %d vsid " TARGET_FMT_lx
"\n",
490 ctx
->key
, !!(slb
->vsid
& SLB_VSID_N
), vsid
);
493 /* Check if instruction fetch is allowed, if needed */
494 if (rwx
!= 2 || !(slb
->vsid
& SLB_VSID_N
)) {
495 /* Page address translation */
496 LOG_MMU("htab_base " TARGET_FMT_plx
" htab_mask " TARGET_FMT_plx
497 " hash " TARGET_FMT_plx
"\n",
498 env
->htab_base
, env
->htab_mask
, hash
);
500 ctx
->hash
[1] = ~hash
;
502 /* Initialize real address with an invalid value */
503 ctx
->raddr
= (hwaddr
)-1ULL;
504 LOG_MMU("0 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
505 " vsid=" TARGET_FMT_lx
" ptem=" TARGET_FMT_lx
506 " hash=" TARGET_FMT_plx
"\n",
507 env
->htab_base
, env
->htab_mask
, vsid
, ctx
->ptem
,
509 /* Primary table lookup */
510 ret
= find_pte64(env
, ctx
, eaddr
, 0, rwx
, target_page_bits
);
512 /* Secondary table lookup */
513 LOG_MMU("1 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
514 " vsid=" TARGET_FMT_lx
" api=" TARGET_FMT_lx
515 " hash=" TARGET_FMT_plx
"\n", env
->htab_base
,
516 env
->htab_mask
, vsid
, ctx
->ptem
, ctx
->hash
[1]);
517 ret2
= find_pte64(env
, ctx
, eaddr
, 1, rwx
, target_page_bits
);
523 LOG_MMU("No access allowed\n");
530 hwaddr
ppc_hash64_get_phys_page_debug(CPUPPCState
*env
, target_ulong addr
)
532 struct mmu_ctx_hash64 ctx
;
534 if (unlikely(ppc_hash64_translate(env
, &ctx
, addr
, 0) != 0)) {
538 return ctx
.raddr
& TARGET_PAGE_MASK
;
541 int ppc_hash64_handle_mmu_fault(CPUPPCState
*env
, target_ulong address
, int rwx
,
544 struct mmu_ctx_hash64 ctx
;
547 ret
= ppc_hash64_translate(env
, &ctx
, address
, rwx
);
549 tlb_set_page(env
, address
& TARGET_PAGE_MASK
,
550 ctx
.raddr
& TARGET_PAGE_MASK
, ctx
.prot
,
551 mmu_idx
, TARGET_PAGE_SIZE
);
553 } else if (ret
< 0) {
558 env
->exception_index
= POWERPC_EXCP_ISI
;
559 env
->error_code
= 0x40000000;
562 /* Access rights violation */
563 env
->exception_index
= POWERPC_EXCP_ISI
;
564 env
->error_code
= 0x08000000;
567 /* No execute protection violation */
568 env
->exception_index
= POWERPC_EXCP_ISI
;
569 env
->error_code
= 0x10000000;
572 /* No match in segment table */
573 env
->exception_index
= POWERPC_EXCP_ISEG
;
580 /* No matches in page tables or TLB */
581 env
->exception_index
= POWERPC_EXCP_DSI
;
583 env
->spr
[SPR_DAR
] = address
;
585 env
->spr
[SPR_DSISR
] = 0x42000000;
587 env
->spr
[SPR_DSISR
] = 0x40000000;
591 /* Access rights violation */
592 env
->exception_index
= POWERPC_EXCP_DSI
;
594 env
->spr
[SPR_DAR
] = address
;
596 env
->spr
[SPR_DSISR
] = 0x0A000000;
598 env
->spr
[SPR_DSISR
] = 0x08000000;
602 /* No match in segment table */
603 env
->exception_index
= POWERPC_EXCP_DSEG
;
605 env
->spr
[SPR_DAR
] = address
;
610 printf("%s: set exception to %d %02x\n", __func__
,
611 env
->exception
, env
->error_code
);