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)
47 static ppc_slb_t
*slb_lookup(CPUPPCState
*env
, target_ulong eaddr
)
49 uint64_t esid_256M
, esid_1T
;
52 LOG_SLB("%s: eaddr " TARGET_FMT_lx
"\n", __func__
, eaddr
);
54 esid_256M
= (eaddr
& SEGMENT_MASK_256M
) | SLB_ESID_V
;
55 esid_1T
= (eaddr
& SEGMENT_MASK_1T
) | SLB_ESID_V
;
57 for (n
= 0; n
< env
->slb_nr
; n
++) {
58 ppc_slb_t
*slb
= &env
->slb
[n
];
60 LOG_SLB("%s: slot %d %016" PRIx64
" %016"
61 PRIx64
"\n", __func__
, n
, slb
->esid
, slb
->vsid
);
62 /* We check for 1T matches on all MMUs here - if the MMU
63 * doesn't have 1T segment support, we will have prevented 1T
64 * entries from being inserted in the slbmte code. */
65 if (((slb
->esid
== esid_256M
) &&
66 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_256M
))
67 || ((slb
->esid
== esid_1T
) &&
68 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_1T
))) {
76 void dump_slb(FILE *f
, fprintf_function cpu_fprintf
, CPUPPCState
*env
)
81 cpu_synchronize_state(env
);
83 cpu_fprintf(f
, "SLB\tESID\t\t\tVSID\n");
84 for (i
= 0; i
< env
->slb_nr
; i
++) {
85 slbe
= env
->slb
[i
].esid
;
86 slbv
= env
->slb
[i
].vsid
;
87 if (slbe
== 0 && slbv
== 0) {
90 cpu_fprintf(f
, "%d\t0x%016" PRIx64
"\t0x%016" PRIx64
"\n",
95 void helper_slbia(CPUPPCState
*env
)
100 /* XXX: Warning: slbia never invalidates the first segment */
101 for (n
= 1; n
< env
->slb_nr
; n
++) {
102 ppc_slb_t
*slb
= &env
->slb
[n
];
104 if (slb
->esid
& SLB_ESID_V
) {
105 slb
->esid
&= ~SLB_ESID_V
;
106 /* XXX: given the fact that segment size is 256 MB or 1TB,
107 * and we still don't have a tlb_flush_mask(env, n, mask)
108 * in QEMU, we just invalidate all TLBs
118 void helper_slbie(CPUPPCState
*env
, target_ulong addr
)
122 slb
= slb_lookup(env
, addr
);
127 if (slb
->esid
& SLB_ESID_V
) {
128 slb
->esid
&= ~SLB_ESID_V
;
130 /* XXX: given the fact that segment size is 256 MB or 1TB,
131 * and we still don't have a tlb_flush_mask(env, n, mask)
132 * in QEMU, we just invalidate all TLBs
138 int ppc_store_slb(CPUPPCState
*env
, target_ulong rb
, target_ulong rs
)
140 int slot
= rb
& 0xfff;
141 ppc_slb_t
*slb
= &env
->slb
[slot
];
143 if (rb
& (0x1000 - env
->slb_nr
)) {
144 return -1; /* Reserved bits set or slot too high */
146 if (rs
& (SLB_VSID_B
& ~SLB_VSID_B_1T
)) {
147 return -1; /* Bad segment size */
149 if ((rs
& SLB_VSID_B
) && !(env
->mmu_model
& POWERPC_MMU_1TSEG
)) {
150 return -1; /* 1T segment on MMU that doesn't support it */
153 /* Mask out the slot number as we store the entry */
154 slb
->esid
= rb
& (SLB_ESID_ESID
| SLB_ESID_V
);
157 LOG_SLB("%s: %d " TARGET_FMT_lx
" - " TARGET_FMT_lx
" => %016" PRIx64
158 " %016" PRIx64
"\n", __func__
, slot
, rb
, rs
,
159 slb
->esid
, slb
->vsid
);
164 static int ppc_load_slb_esid(CPUPPCState
*env
, target_ulong rb
,
167 int slot
= rb
& 0xfff;
168 ppc_slb_t
*slb
= &env
->slb
[slot
];
170 if (slot
>= env
->slb_nr
) {
178 static int ppc_load_slb_vsid(CPUPPCState
*env
, target_ulong rb
,
181 int slot
= rb
& 0xfff;
182 ppc_slb_t
*slb
= &env
->slb
[slot
];
184 if (slot
>= env
->slb_nr
) {
192 void helper_store_slb(CPUPPCState
*env
, target_ulong rb
, target_ulong rs
)
194 if (ppc_store_slb(env
, rb
, rs
) < 0) {
195 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
200 target_ulong
helper_load_slb_esid(CPUPPCState
*env
, target_ulong rb
)
204 if (ppc_load_slb_esid(env
, rb
, &rt
) < 0) {
205 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
211 target_ulong
helper_load_slb_vsid(CPUPPCState
*env
, target_ulong rb
)
215 if (ppc_load_slb_vsid(env
, rb
, &rt
) < 0) {
216 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
223 * 64-bit hash table MMU handling
226 #define PTE64_PTEM_MASK 0xFFFFFFFFFFFFFF80ULL
227 #define PTE64_CHECK_MASK (TARGET_PAGE_MASK | 0x7F)
229 static inline int pte64_is_valid(target_ulong pte0
)
231 return pte0
& 0x0000000000000001ULL
? 1 : 0;
234 static int pte64_check(mmu_ctx_t
*ctx
, target_ulong pte0
,
235 target_ulong pte1
, int h
, int rw
, int type
)
237 target_ulong ptem
, mmask
;
238 int access
, ret
, pteh
, ptev
, pp
;
241 /* Check validity and table match */
242 ptev
= pte64_is_valid(pte0
);
243 pteh
= (pte0
>> 1) & 1;
244 if (ptev
&& h
== pteh
) {
245 /* Check vsid & api */
246 ptem
= pte0
& PTE64_PTEM_MASK
;
247 mmask
= PTE64_CHECK_MASK
;
248 pp
= (pte1
& 0x00000003) | ((pte1
>> 61) & 0x00000004);
249 ctx
->nx
= (pte1
>> 2) & 1; /* No execute bit */
250 ctx
->nx
|= (pte1
>> 3) & 1; /* Guarded bit */
251 if (ptem
== ctx
->ptem
) {
252 if (ctx
->raddr
!= (hwaddr
)-1ULL) {
253 /* all matches should have equal RPN, WIMG & PP */
254 if ((ctx
->raddr
& mmask
) != (pte1
& mmask
)) {
255 qemu_log("Bad RPN/WIMG/PP\n");
259 /* Compute access rights */
260 access
= pp_check(ctx
->key
, pp
, ctx
->nx
);
261 /* Keep the matching PTE informations */
264 ret
= check_prot(ctx
->prot
, rw
, type
);
267 LOG_MMU("PTE access granted !\n");
269 /* Access right violation */
270 LOG_MMU("PTE access rejected\n");
278 /* PTE table lookup */
279 static int find_pte64(CPUPPCState
*env
, mmu_ctx_t
*ctx
, int h
,
280 int rw
, int type
, int target_page_bits
)
283 target_ulong pte0
, pte1
;
287 ret
= -1; /* No entry found */
288 pteg_off
= get_pteg_offset(env
, ctx
->hash
[h
], HASH_PTE_SIZE_64
);
289 for (i
= 0; i
< 8; i
++) {
290 if (env
->external_htab
) {
291 pte0
= ldq_p(env
->external_htab
+ pteg_off
+ (i
* 16));
292 pte1
= ldq_p(env
->external_htab
+ pteg_off
+ (i
* 16) + 8);
294 pte0
= ldq_phys(env
->htab_base
+ pteg_off
+ (i
* 16));
295 pte1
= ldq_phys(env
->htab_base
+ pteg_off
+ (i
* 16) + 8);
298 r
= pte64_check(ctx
, pte0
, pte1
, h
, rw
, type
);
299 LOG_MMU("Load pte from %016" HWADDR_PRIx
" => " TARGET_FMT_lx
" "
300 TARGET_FMT_lx
" %d %d %d " TARGET_FMT_lx
"\n",
301 pteg_off
+ (i
* 16), pte0
, pte1
, (int)(pte0
& 1), h
,
302 (int)((pte0
>> 1) & 1), ctx
->ptem
);
305 /* PTE inconsistency */
308 /* Access violation */
318 /* XXX: we should go on looping to check all PTEs consistency
319 * but if we can speed-up the whole thing as the
320 * result would be undefined if PTEs are not consistent.
329 LOG_MMU("found PTE at addr %08" HWADDR_PRIx
" prot=%01x ret=%d\n",
330 ctx
->raddr
, ctx
->prot
, ret
);
331 /* Update page flags */
333 if (pte_update_flags(ctx
, &pte1
, ret
, rw
) == 1) {
334 if (env
->external_htab
) {
335 stq_p(env
->external_htab
+ pteg_off
+ (good
* 16) + 8,
338 stq_phys_notdirty(env
->htab_base
+ pteg_off
+
339 (good
* 16) + 8, pte1
);
344 /* We have a TLB that saves 4K pages, so let's
345 * split a huge page to 4k chunks */
346 if (target_page_bits
!= TARGET_PAGE_BITS
) {
347 ctx
->raddr
|= (ctx
->eaddr
& ((1 << target_page_bits
) - 1))
353 static int get_segment64(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
354 target_ulong eaddr
, int rw
, int type
)
358 int pr
, target_page_bits
;
364 target_ulong pageaddr
;
367 LOG_MMU("Check SLBs\n");
368 slb
= slb_lookup(env
, eaddr
);
373 if (slb
->vsid
& SLB_VSID_B
) {
374 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT_1T
;
377 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT
;
381 target_page_bits
= (slb
->vsid
& SLB_VSID_L
)
382 ? TARGET_PAGE_BITS_16M
: TARGET_PAGE_BITS
;
383 ctx
->key
= !!(pr
? (slb
->vsid
& SLB_VSID_KP
)
384 : (slb
->vsid
& SLB_VSID_KS
));
385 ctx
->nx
= !!(slb
->vsid
& SLB_VSID_N
);
387 pageaddr
= eaddr
& ((1ULL << segment_bits
)
388 - (1ULL << target_page_bits
));
389 if (slb
->vsid
& SLB_VSID_B
) {
390 hash
= vsid
^ (vsid
<< 25) ^ (pageaddr
>> target_page_bits
);
392 hash
= vsid
^ (pageaddr
>> target_page_bits
);
394 /* Only 5 bits of the page index are used in the AVPN */
395 ctx
->ptem
= (slb
->vsid
& SLB_VSID_PTEM
) |
396 ((pageaddr
>> 16) & ((1ULL << segment_bits
) - 0x80));
398 LOG_MMU("pte segment: key=%d nx %d vsid " TARGET_FMT_lx
"\n",
399 ctx
->key
, ctx
->nx
, vsid
);
402 /* Check if instruction fetch is allowed, if needed */
403 if (type
!= ACCESS_CODE
|| ctx
->nx
== 0) {
404 /* Page address translation */
405 LOG_MMU("htab_base " TARGET_FMT_plx
" htab_mask " TARGET_FMT_plx
406 " hash " TARGET_FMT_plx
"\n",
407 env
->htab_base
, env
->htab_mask
, hash
);
409 ctx
->hash
[1] = ~hash
;
411 /* Initialize real address with an invalid value */
412 ctx
->raddr
= (hwaddr
)-1ULL;
413 LOG_MMU("0 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
414 " vsid=" TARGET_FMT_lx
" ptem=" TARGET_FMT_lx
415 " hash=" TARGET_FMT_plx
"\n",
416 env
->htab_base
, env
->htab_mask
, vsid
, ctx
->ptem
,
418 /* Primary table lookup */
419 ret
= find_pte64(env
, ctx
, 0, rw
, type
, target_page_bits
);
421 /* Secondary table lookup */
422 LOG_MMU("1 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
423 " vsid=" TARGET_FMT_lx
" api=" TARGET_FMT_lx
424 " hash=" TARGET_FMT_plx
"\n", env
->htab_base
,
425 env
->htab_mask
, vsid
, ctx
->ptem
, ctx
->hash
[1]);
426 ret2
= find_pte64(env
, ctx
, 1, rw
, type
, target_page_bits
);
432 LOG_MMU("No access allowed\n");
439 int ppc_hash64_get_physical_address(CPUPPCState
*env
, mmu_ctx_t
*ctx
,
440 target_ulong eaddr
, int rw
, int access_type
)
442 bool real_mode
= (access_type
== ACCESS_CODE
&& msr_ir
== 0)
443 || (access_type
!= ACCESS_CODE
&& msr_dr
== 0);
446 ctx
->raddr
= eaddr
& 0x0FFFFFFFFFFFFFFFULL
;
447 ctx
->prot
= PAGE_READ
| PAGE_EXEC
| PAGE_WRITE
;
450 return get_segment64(env
, ctx
, eaddr
, rw
, access_type
);
454 int ppc_hash64_handle_mmu_fault(CPUPPCState
*env
, target_ulong address
, int rw
,
464 access_type
= ACCESS_CODE
;
467 access_type
= env
->access_type
;
469 ret
= ppc_hash64_get_physical_address(env
, &ctx
, address
, rw
, access_type
);
471 tlb_set_page(env
, address
& TARGET_PAGE_MASK
,
472 ctx
.raddr
& TARGET_PAGE_MASK
, ctx
.prot
,
473 mmu_idx
, TARGET_PAGE_SIZE
);
475 } else if (ret
< 0) {
477 if (access_type
== ACCESS_CODE
) {
480 env
->exception_index
= POWERPC_EXCP_ISI
;
481 env
->error_code
= 0x40000000;
484 /* Access rights violation */
485 env
->exception_index
= POWERPC_EXCP_ISI
;
486 env
->error_code
= 0x08000000;
489 /* No execute protection violation */
490 env
->exception_index
= POWERPC_EXCP_ISI
;
491 env
->error_code
= 0x10000000;
494 /* No match in segment table */
495 env
->exception_index
= POWERPC_EXCP_ISEG
;
502 /* No matches in page tables or TLB */
503 env
->exception_index
= POWERPC_EXCP_DSI
;
505 env
->spr
[SPR_DAR
] = address
;
507 env
->spr
[SPR_DSISR
] = 0x42000000;
509 env
->spr
[SPR_DSISR
] = 0x40000000;
513 /* Access rights violation */
514 env
->exception_index
= POWERPC_EXCP_DSI
;
516 env
->spr
[SPR_DAR
] = address
;
518 env
->spr
[SPR_DSISR
] = 0x0A000000;
520 env
->spr
[SPR_DSISR
] = 0x08000000;
524 /* No match in segment table */
525 env
->exception_index
= POWERPC_EXCP_DSEG
;
527 env
->spr
[SPR_DAR
] = address
;
532 printf("%s: set exception to %d %02x\n", __func__
,
533 env
->exception
, env
->error_code
);