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 static int ppc_hash64_pp_check(int key
, int pp
, bool nx
)
238 /* Compute access rights */
239 /* When pp is 4, 5 or 7, the result is undefined. Set it to noaccess */
246 access
|= PAGE_WRITE
;
264 access
= PAGE_READ
| PAGE_WRITE
;
275 static int ppc_hash64_check_prot(int prot
, int rwx
)
280 if (prot
& PAGE_EXEC
) {
285 } else if (rwx
== 1) {
286 if (prot
& PAGE_WRITE
) {
292 if (prot
& PAGE_READ
) {
302 static bool pte64_match(target_ulong pte0
, target_ulong pte1
,
303 bool secondary
, target_ulong ptem
)
305 return (pte0
& HPTE64_V_VALID
)
306 && (secondary
== !!(pte0
& HPTE64_V_SECONDARY
))
307 && HPTE64_V_COMPARE(pte0
, ptem
);
310 static int pte64_check(struct mmu_ctx_hash64
*ctx
, target_ulong pte0
,
311 target_ulong pte1
, int rwx
)
316 pp
= (pte1
& HPTE64_R_PP
) | ((pte1
& HPTE64_R_PP0
) >> 61);
317 /* No execute if either noexec or guarded bits set */
318 nx
= (pte1
& HPTE64_R_N
) || (pte1
& HPTE64_R_G
);
319 /* Compute access rights */
320 access
= ppc_hash64_pp_check(ctx
->key
, pp
, nx
);
321 /* Keep the matching PTE informations */
324 ret
= ppc_hash64_check_prot(ctx
->prot
, rwx
);
327 LOG_MMU("PTE access granted !\n");
329 /* Access right violation */
330 LOG_MMU("PTE access rejected\n");
336 static int ppc_hash64_pte_update_flags(struct mmu_ctx_hash64
*ctx
,
342 /* Update page flags */
343 if (!(*pte1p
& HPTE64_R_R
)) {
344 /* Update accessed flag */
345 *pte1p
|= HPTE64_R_R
;
348 if (!(*pte1p
& HPTE64_R_C
)) {
349 if (rw
== 1 && ret
== 0) {
350 /* Update changed flag */
351 *pte1p
|= HPTE64_R_C
;
354 /* Force page fault for first write access */
355 ctx
->prot
&= ~PAGE_WRITE
;
362 /* PTE table lookup */
363 static int find_pte64(CPUPPCState
*env
, struct mmu_ctx_hash64
*ctx
,
364 target_ulong eaddr
, int h
, int rwx
, int target_page_bits
)
367 target_ulong pte0
, pte1
;
371 ret
= -1; /* No entry found */
372 pteg_off
= (ctx
->hash
[h
] * HASH_PTEG_SIZE_64
) & env
->htab_mask
;
373 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
374 pte0
= ppc_hash64_load_hpte0(env
, pteg_off
+ i
*HASH_PTE_SIZE_64
);
375 pte1
= ppc_hash64_load_hpte1(env
, pteg_off
+ i
*HASH_PTE_SIZE_64
);
377 LOG_MMU("Load pte from %016" HWADDR_PRIx
" => " TARGET_FMT_lx
" "
378 TARGET_FMT_lx
" %d %d %d " TARGET_FMT_lx
"\n",
379 pteg_off
+ (i
* 16), pte0
, pte1
, !!(pte0
& HPTE64_V_VALID
),
380 h
, !!(pte0
& HPTE64_V_SECONDARY
), ctx
->ptem
);
382 if (pte64_match(pte0
, pte1
, h
, ctx
->ptem
)) {
388 ret
= pte64_check(ctx
, pte0
, pte1
, rwx
);
389 LOG_MMU("found PTE at addr %08" HWADDR_PRIx
" prot=%01x ret=%d\n",
390 ctx
->raddr
, ctx
->prot
, ret
);
391 /* Update page flags */
393 if (ppc_hash64_pte_update_flags(ctx
, &pte1
, ret
, rwx
) == 1) {
394 ppc_hash64_store_hpte1(env
, pteg_off
+ good
* HASH_PTE_SIZE_64
, pte1
);
398 /* We have a TLB that saves 4K pages, so let's
399 * split a huge page to 4k chunks */
400 if (target_page_bits
!= TARGET_PAGE_BITS
) {
401 ctx
->raddr
|= (eaddr
& ((1 << target_page_bits
) - 1))
407 static int ppc_hash64_translate(CPUPPCState
*env
, struct mmu_ctx_hash64
*ctx
,
408 target_ulong eaddr
, int rwx
)
412 int pr
, target_page_bits
;
415 target_ulong pageaddr
;
418 /* 1. Handle real mode accesses */
419 if (((rwx
== 2) && (msr_ir
== 0)) || ((rwx
!= 2) && (msr_dr
== 0))) {
420 /* Translation is off */
421 /* In real mode the top 4 effective address bits are ignored */
422 ctx
->raddr
= eaddr
& 0x0FFFFFFFFFFFFFFFULL
;
423 ctx
->prot
= PAGE_READ
| PAGE_EXEC
| PAGE_WRITE
;
427 /* 2. Translation is on, so look up the SLB */
428 slb
= slb_lookup(env
, eaddr
);
434 /* 3. Check for segment level no-execute violation */
435 if ((rwx
== 2) && (slb
->vsid
& SLB_VSID_N
)) {
441 if (slb
->vsid
& SLB_VSID_B
) {
442 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT_1T
;
445 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT
;
449 target_page_bits
= (slb
->vsid
& SLB_VSID_L
)
450 ? TARGET_PAGE_BITS_16M
: TARGET_PAGE_BITS
;
451 ctx
->key
= !!(pr
? (slb
->vsid
& SLB_VSID_KP
)
452 : (slb
->vsid
& SLB_VSID_KS
));
454 pageaddr
= eaddr
& ((1ULL << segment_bits
)
455 - (1ULL << target_page_bits
));
456 if (slb
->vsid
& SLB_VSID_B
) {
457 hash
= vsid
^ (vsid
<< 25) ^ (pageaddr
>> target_page_bits
);
459 hash
= vsid
^ (pageaddr
>> target_page_bits
);
461 /* Only 5 bits of the page index are used in the AVPN */
462 ctx
->ptem
= (slb
->vsid
& SLB_VSID_PTEM
) |
463 ((pageaddr
>> 16) & ((1ULL << segment_bits
) - 0x80));
465 LOG_MMU("pte segment: key=%d nx %d vsid " TARGET_FMT_lx
"\n",
466 ctx
->key
, !!(slb
->vsid
& SLB_VSID_N
), vsid
);
469 /* Page address translation */
470 LOG_MMU("htab_base " TARGET_FMT_plx
" htab_mask " TARGET_FMT_plx
471 " hash " TARGET_FMT_plx
"\n",
472 env
->htab_base
, env
->htab_mask
, hash
);
474 ctx
->hash
[1] = ~hash
;
476 LOG_MMU("0 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
477 " vsid=" TARGET_FMT_lx
" ptem=" TARGET_FMT_lx
478 " hash=" TARGET_FMT_plx
"\n",
479 env
->htab_base
, env
->htab_mask
, vsid
, ctx
->ptem
,
481 /* Primary table lookup */
482 ret
= find_pte64(env
, ctx
, eaddr
, 0, rwx
, target_page_bits
);
484 /* Secondary table lookup */
485 LOG_MMU("1 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
486 " vsid=" TARGET_FMT_lx
" api=" TARGET_FMT_lx
487 " hash=" TARGET_FMT_plx
"\n", env
->htab_base
,
488 env
->htab_mask
, vsid
, ctx
->ptem
, ctx
->hash
[1]);
489 ret2
= find_pte64(env
, ctx
, eaddr
, 1, rwx
, target_page_bits
);
498 hwaddr
ppc_hash64_get_phys_page_debug(CPUPPCState
*env
, target_ulong addr
)
500 struct mmu_ctx_hash64 ctx
;
502 if (unlikely(ppc_hash64_translate(env
, &ctx
, addr
, 0) != 0)) {
506 return ctx
.raddr
& TARGET_PAGE_MASK
;
509 int ppc_hash64_handle_mmu_fault(CPUPPCState
*env
, target_ulong address
, int rwx
,
512 struct mmu_ctx_hash64 ctx
;
515 ret
= ppc_hash64_translate(env
, &ctx
, address
, rwx
);
517 tlb_set_page(env
, address
& TARGET_PAGE_MASK
,
518 ctx
.raddr
& TARGET_PAGE_MASK
, ctx
.prot
,
519 mmu_idx
, TARGET_PAGE_SIZE
);
521 } else if (ret
< 0) {
526 env
->exception_index
= POWERPC_EXCP_ISI
;
527 env
->error_code
= 0x40000000;
530 /* Access rights violation */
531 env
->exception_index
= POWERPC_EXCP_ISI
;
532 env
->error_code
= 0x08000000;
535 /* No execute protection violation */
536 env
->exception_index
= POWERPC_EXCP_ISI
;
537 env
->error_code
= 0x10000000;
540 /* No match in segment table */
541 env
->exception_index
= POWERPC_EXCP_ISEG
;
548 /* No matches in page tables or TLB */
549 env
->exception_index
= POWERPC_EXCP_DSI
;
551 env
->spr
[SPR_DAR
] = address
;
553 env
->spr
[SPR_DSISR
] = 0x42000000;
555 env
->spr
[SPR_DSISR
] = 0x40000000;
559 /* Access rights violation */
560 env
->exception_index
= POWERPC_EXCP_DSI
;
562 env
->spr
[SPR_DAR
] = address
;
564 env
->spr
[SPR_DSISR
] = 0x0A000000;
566 env
->spr
[SPR_DSISR
] = 0x08000000;
570 /* No match in segment table */
571 env
->exception_index
= POWERPC_EXCP_DSEG
;
573 env
->spr
[SPR_DAR
] = address
;
578 printf("%s: set exception to %d %02x\n", __func__
,
579 env
->exception
, env
->error_code
);