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/>.
20 #include "qemu/osdep.h"
21 #include "qemu/units.h"
23 #include "exec/exec-all.h"
24 #include "exec/helper-proto.h"
25 #include "qemu/error-report.h"
26 #include "qemu/qemu-print.h"
27 #include "sysemu/hw_accel.h"
29 #include "mmu-hash64.h"
32 #include "mmu-book3s-v3.h"
34 /* #define DEBUG_SLB */
37 # define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
39 # define LOG_SLB(...) do { } while (0)
46 static ppc_slb_t
*slb_lookup(PowerPCCPU
*cpu
, target_ulong eaddr
)
48 CPUPPCState
*env
= &cpu
->env
;
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
< cpu
->hash64_opts
->slb_size
; 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
);
63 * We check for 1T matches on all MMUs here - if the MMU
64 * doesn't have 1T segment support, we will have prevented 1T
65 * entries from being inserted in the slbmte code.
67 if (((slb
->esid
== esid_256M
) &&
68 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_256M
))
69 || ((slb
->esid
== esid_1T
) &&
70 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_1T
))) {
78 void dump_slb(PowerPCCPU
*cpu
)
80 CPUPPCState
*env
= &cpu
->env
;
84 cpu_synchronize_state(CPU(cpu
));
86 qemu_printf("SLB\tESID\t\t\tVSID\n");
87 for (i
= 0; i
< cpu
->hash64_opts
->slb_size
; i
++) {
88 slbe
= env
->slb
[i
].esid
;
89 slbv
= env
->slb
[i
].vsid
;
90 if (slbe
== 0 && slbv
== 0) {
93 qemu_printf("%d\t0x%016" PRIx64
"\t0x%016" PRIx64
"\n",
98 void helper_slbia(CPUPPCState
*env
, uint32_t ih
)
100 PowerPCCPU
*cpu
= env_archcpu(env
);
105 * slbia must always flush all TLB (which is equivalent to ERAT in ppc
106 * architecture). Matching on SLB_ESID_V is not good enough, because slbmte
107 * can overwrite a valid SLB without flushing its lookaside information.
109 * It would be possible to keep the TLB in synch with the SLB by flushing
110 * when a valid entry is overwritten by slbmte, and therefore slbia would
111 * not have to flush unless it evicts a valid SLB entry. However it is
112 * expected that slbmte is more common than slbia, and slbia is usually
113 * going to evict valid SLB entries, so that tradeoff is unlikely to be a
116 * ISA v2.05 introduced IH field with values 0,1,2,6. These all invalidate
117 * the same SLB entries (everything but entry 0), but differ in what
118 * "lookaside information" is invalidated. TCG can ignore this and flush
121 * ISA v3.0 introduced additional values 3,4,7, which change what SLBs are
125 env
->tlb_need_flush
|= TLB_NEED_LOCAL_FLUSH
;
127 starting_entry
= 1; /* default for IH=0,1,2,6 */
129 if (env
->mmu_model
== POWERPC_MMU_3_00
) {
132 /* invalidate no SLBs, but all lookaside information */
137 /* also considers SLB entry 0 */
142 /* treat undefined values as ih==0, and warn */
143 qemu_log_mask(LOG_GUEST_ERROR
,
144 "slbia undefined IH field %u.\n", ih
);
153 for (n
= starting_entry
; n
< cpu
->hash64_opts
->slb_size
; n
++) {
154 ppc_slb_t
*slb
= &env
->slb
[n
];
156 if (!(slb
->esid
& SLB_ESID_V
)) {
159 if (env
->mmu_model
== POWERPC_MMU_3_00
) {
160 if (ih
== 0x3 && (slb
->vsid
& SLB_VSID_C
) == 0) {
161 /* preserves entries with a class value of 0 */
166 slb
->esid
&= ~SLB_ESID_V
;
170 static void __helper_slbie(CPUPPCState
*env
, target_ulong addr
,
173 PowerPCCPU
*cpu
= env_archcpu(env
);
176 slb
= slb_lookup(cpu
, addr
);
181 if (slb
->esid
& SLB_ESID_V
) {
182 slb
->esid
&= ~SLB_ESID_V
;
185 * XXX: given the fact that segment size is 256 MB or 1TB,
186 * and we still don't have a tlb_flush_mask(env, n, mask)
187 * in QEMU, we just invalidate all TLBs
189 env
->tlb_need_flush
|=
190 (global
== false ? TLB_NEED_LOCAL_FLUSH
: TLB_NEED_GLOBAL_FLUSH
);
194 void helper_slbie(CPUPPCState
*env
, target_ulong addr
)
196 __helper_slbie(env
, addr
, false);
199 void helper_slbieg(CPUPPCState
*env
, target_ulong addr
)
201 __helper_slbie(env
, addr
, true);
204 int ppc_store_slb(PowerPCCPU
*cpu
, target_ulong slot
,
205 target_ulong esid
, target_ulong vsid
)
207 CPUPPCState
*env
= &cpu
->env
;
208 ppc_slb_t
*slb
= &env
->slb
[slot
];
209 const PPCHash64SegmentPageSizes
*sps
= NULL
;
212 if (slot
>= cpu
->hash64_opts
->slb_size
) {
213 return -1; /* Bad slot number */
215 if (esid
& ~(SLB_ESID_ESID
| SLB_ESID_V
)) {
216 return -1; /* Reserved bits set */
218 if (vsid
& (SLB_VSID_B
& ~SLB_VSID_B_1T
)) {
219 return -1; /* Bad segment size */
221 if ((vsid
& SLB_VSID_B
) && !(ppc_hash64_has(cpu
, PPC_HASH64_1TSEG
))) {
222 return -1; /* 1T segment on MMU that doesn't support it */
225 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
226 const PPCHash64SegmentPageSizes
*sps1
= &cpu
->hash64_opts
->sps
[i
];
228 if (!sps1
->page_shift
) {
232 if ((vsid
& SLB_VSID_LLP_MASK
) == sps1
->slb_enc
) {
239 error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
240 " esid 0x"TARGET_FMT_lx
" vsid 0x"TARGET_FMT_lx
,
249 LOG_SLB("%s: " TARGET_FMT_lu
" " TARGET_FMT_lx
" - " TARGET_FMT_lx
250 " => %016" PRIx64
" %016" PRIx64
"\n", __func__
, slot
, esid
, vsid
,
251 slb
->esid
, slb
->vsid
);
256 static int ppc_load_slb_esid(PowerPCCPU
*cpu
, target_ulong rb
,
259 CPUPPCState
*env
= &cpu
->env
;
260 int slot
= rb
& 0xfff;
261 ppc_slb_t
*slb
= &env
->slb
[slot
];
263 if (slot
>= cpu
->hash64_opts
->slb_size
) {
271 static int ppc_load_slb_vsid(PowerPCCPU
*cpu
, target_ulong rb
,
274 CPUPPCState
*env
= &cpu
->env
;
275 int slot
= rb
& 0xfff;
276 ppc_slb_t
*slb
= &env
->slb
[slot
];
278 if (slot
>= cpu
->hash64_opts
->slb_size
) {
286 static int ppc_find_slb_vsid(PowerPCCPU
*cpu
, target_ulong rb
,
289 CPUPPCState
*env
= &cpu
->env
;
292 if (!msr_is_64bit(env
, env
->msr
)) {
295 slb
= slb_lookup(cpu
, rb
);
297 *rt
= (target_ulong
)-1ul;
304 void helper_store_slb(CPUPPCState
*env
, target_ulong rb
, target_ulong rs
)
306 PowerPCCPU
*cpu
= env_archcpu(env
);
308 if (ppc_store_slb(cpu
, rb
& 0xfff, rb
& ~0xfffULL
, rs
) < 0) {
309 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
310 POWERPC_EXCP_INVAL
, GETPC());
314 target_ulong
helper_load_slb_esid(CPUPPCState
*env
, target_ulong rb
)
316 PowerPCCPU
*cpu
= env_archcpu(env
);
319 if (ppc_load_slb_esid(cpu
, rb
, &rt
) < 0) {
320 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
321 POWERPC_EXCP_INVAL
, GETPC());
326 target_ulong
helper_find_slb_vsid(CPUPPCState
*env
, target_ulong rb
)
328 PowerPCCPU
*cpu
= env_archcpu(env
);
331 if (ppc_find_slb_vsid(cpu
, rb
, &rt
) < 0) {
332 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
333 POWERPC_EXCP_INVAL
, GETPC());
338 target_ulong
helper_load_slb_vsid(CPUPPCState
*env
, target_ulong rb
)
340 PowerPCCPU
*cpu
= env_archcpu(env
);
343 if (ppc_load_slb_vsid(cpu
, rb
, &rt
) < 0) {
344 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
345 POWERPC_EXCP_INVAL
, GETPC());
350 /* Check No-Execute or Guarded Storage */
351 static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU
*cpu
,
352 ppc_hash_pte64_t pte
)
354 /* Exec permissions CANNOT take away read or write permissions */
355 return (pte
.pte1
& HPTE64_R_N
) || (pte
.pte1
& HPTE64_R_G
) ?
356 PAGE_READ
| PAGE_WRITE
: PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
359 /* Check Basic Storage Protection */
360 static int ppc_hash64_pte_prot(PowerPCCPU
*cpu
,
361 ppc_slb_t
*slb
, ppc_hash_pte64_t pte
)
363 CPUPPCState
*env
= &cpu
->env
;
366 * Some pp bit combinations have undefined behaviour, so default
367 * to no access in those cases
371 key
= !!(msr_pr
? (slb
->vsid
& SLB_VSID_KP
)
372 : (slb
->vsid
& SLB_VSID_KS
));
373 pp
= (pte
.pte1
& HPTE64_R_PP
) | ((pte
.pte1
& HPTE64_R_PP0
) >> 61);
380 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
385 prot
= PAGE_READ
| PAGE_EXEC
;
396 prot
= PAGE_READ
| PAGE_EXEC
;
400 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
408 /* Check the instruction access permissions specified in the IAMR */
409 static int ppc_hash64_iamr_prot(PowerPCCPU
*cpu
, int key
)
411 CPUPPCState
*env
= &cpu
->env
;
412 int iamr_bits
= (env
->spr
[SPR_IAMR
] >> 2 * (31 - key
)) & 0x3;
415 * An instruction fetch is permitted if the IAMR bit is 0.
416 * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit
417 * can only take away EXEC permissions not READ or WRITE permissions.
418 * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since
419 * EXEC permissions are allowed.
421 return (iamr_bits
& 0x1) ? PAGE_READ
| PAGE_WRITE
:
422 PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
425 static int ppc_hash64_amr_prot(PowerPCCPU
*cpu
, ppc_hash_pte64_t pte
)
427 CPUPPCState
*env
= &cpu
->env
;
429 int prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
431 /* Only recent MMUs implement Virtual Page Class Key Protection */
432 if (!ppc_hash64_has(cpu
, PPC_HASH64_AMR
)) {
436 key
= HPTE64_R_KEY(pte
.pte1
);
437 amrbits
= (env
->spr
[SPR_AMR
] >> 2 * (31 - key
)) & 0x3;
439 /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
440 /* env->spr[SPR_AMR]); */
443 * A store is permitted if the AMR bit is 0. Remove write
444 * protection if it is set.
450 * A load is permitted if the AMR bit is 0. Remove read
451 * protection if it is set.
457 switch (env
->mmu_model
) {
459 * MMU version 2.07 and later support IAMR
460 * Check if the IAMR allows the instruction access - it will return
461 * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0
462 * if it does (and prot will be unchanged indicating execution support).
464 case POWERPC_MMU_2_07
:
465 case POWERPC_MMU_3_00
:
466 prot
&= ppc_hash64_iamr_prot(cpu
, key
);
475 const ppc_hash_pte64_t
*ppc_hash64_map_hptes(PowerPCCPU
*cpu
,
478 hwaddr pte_offset
= ptex
* HASH_PTE_SIZE_64
;
480 hwaddr plen
= n
* HASH_PTE_SIZE_64
;
481 const ppc_hash_pte64_t
*hptes
;
484 PPCVirtualHypervisorClass
*vhc
=
485 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
486 return vhc
->map_hptes(cpu
->vhyp
, ptex
, n
);
488 base
= ppc_hash64_hpt_base(cpu
);
494 hptes
= address_space_map(CPU(cpu
)->as
, base
+ pte_offset
, &plen
, false,
495 MEMTXATTRS_UNSPECIFIED
);
496 if (plen
< (n
* HASH_PTE_SIZE_64
)) {
497 hw_error("%s: Unable to map all requested HPTEs\n", __func__
);
502 void ppc_hash64_unmap_hptes(PowerPCCPU
*cpu
, const ppc_hash_pte64_t
*hptes
,
506 PPCVirtualHypervisorClass
*vhc
=
507 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
508 vhc
->unmap_hptes(cpu
->vhyp
, hptes
, ptex
, n
);
512 address_space_unmap(CPU(cpu
)->as
, (void *)hptes
, n
* HASH_PTE_SIZE_64
,
513 false, n
* HASH_PTE_SIZE_64
);
516 static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes
*sps
,
517 uint64_t pte0
, uint64_t pte1
)
521 if (!(pte0
& HPTE64_V_LARGE
)) {
522 if (sps
->page_shift
!= 12) {
523 /* 4kiB page in a non 4kiB segment */
526 /* Normal 4kiB page */
530 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
531 const PPCHash64PageSize
*ps
= &sps
->enc
[i
];
534 if (!ps
->page_shift
) {
538 if (ps
->page_shift
== 12) {
539 /* L bit is set so this can't be a 4kiB page */
543 mask
= ((1ULL << ps
->page_shift
) - 1) & HPTE64_R_RPN
;
545 if ((pte1
& mask
) == ((uint64_t)ps
->pte_enc
<< HPTE64_R_RPN_SHIFT
)) {
546 return ps
->page_shift
;
550 return 0; /* Bad page size encoding */
553 static void ppc64_v3_new_to_old_hpte(target_ulong
*pte0
, target_ulong
*pte1
)
555 /* Insert B into pte0 */
556 *pte0
= (*pte0
& HPTE64_V_COMMON_BITS
) |
557 ((*pte1
& HPTE64_R_3_0_SSIZE_MASK
) <<
558 (HPTE64_V_SSIZE_SHIFT
- HPTE64_R_3_0_SSIZE_SHIFT
));
560 /* Remove B from pte1 */
561 *pte1
= *pte1
& ~HPTE64_R_3_0_SSIZE_MASK
;
565 static hwaddr
ppc_hash64_pteg_search(PowerPCCPU
*cpu
, hwaddr hash
,
566 const PPCHash64SegmentPageSizes
*sps
,
568 ppc_hash_pte64_t
*pte
, unsigned *pshift
)
571 const ppc_hash_pte64_t
*pteg
;
572 target_ulong pte0
, pte1
;
575 ptex
= (hash
& ppc_hash64_hpt_mask(cpu
)) * HPTES_PER_GROUP
;
576 pteg
= ppc_hash64_map_hptes(cpu
, ptex
, HPTES_PER_GROUP
);
580 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
581 pte0
= ppc_hash64_hpte0(cpu
, pteg
, i
);
583 * pte0 contains the valid bit and must be read before pte1,
584 * otherwise we might see an old pte1 with a new valid bit and
585 * thus an inconsistent hpte value
588 pte1
= ppc_hash64_hpte1(cpu
, pteg
, i
);
590 /* Convert format if necessary */
591 if (cpu
->env
.mmu_model
== POWERPC_MMU_3_00
&& !cpu
->vhyp
) {
592 ppc64_v3_new_to_old_hpte(&pte0
, &pte1
);
595 /* This compares V, B, H (secondary) and the AVPN */
596 if (HPTE64_V_COMPARE(pte0
, ptem
)) {
597 *pshift
= hpte_page_shift(sps
, pte0
, pte1
);
599 * If there is no match, ignore the PTE, it could simply
600 * be for a different segment size encoding and the
601 * architecture specifies we should not match. Linux will
602 * potentially leave behind PTEs for the wrong base page
603 * size when demoting segments.
609 * We don't do anything with pshift yet as qemu TLB only
610 * deals with 4K pages anyway
614 ppc_hash64_unmap_hptes(cpu
, pteg
, ptex
, HPTES_PER_GROUP
);
618 ppc_hash64_unmap_hptes(cpu
, pteg
, ptex
, HPTES_PER_GROUP
);
620 * We didn't find a valid entry.
625 static hwaddr
ppc_hash64_htab_lookup(PowerPCCPU
*cpu
,
626 ppc_slb_t
*slb
, target_ulong eaddr
,
627 ppc_hash_pte64_t
*pte
, unsigned *pshift
)
629 CPUPPCState
*env
= &cpu
->env
;
631 uint64_t vsid
, epnmask
, epn
, ptem
;
632 const PPCHash64SegmentPageSizes
*sps
= slb
->sps
;
635 * The SLB store path should prevent any bad page size encodings
636 * getting in there, so:
640 /* If ISL is set in LPCR we need to clamp the page size to 4K */
641 if (env
->spr
[SPR_LPCR
] & LPCR_ISL
) {
642 /* We assume that when using TCG, 4k is first entry of SPS */
643 sps
= &cpu
->hash64_opts
->sps
[0];
644 assert(sps
->page_shift
== 12);
647 epnmask
= ~((1ULL << sps
->page_shift
) - 1);
649 if (slb
->vsid
& SLB_VSID_B
) {
651 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT_1T
;
652 epn
= (eaddr
& ~SEGMENT_MASK_1T
) & epnmask
;
653 hash
= vsid
^ (vsid
<< 25) ^ (epn
>> sps
->page_shift
);
656 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT
;
657 epn
= (eaddr
& ~SEGMENT_MASK_256M
) & epnmask
;
658 hash
= vsid
^ (epn
>> sps
->page_shift
);
660 ptem
= (slb
->vsid
& SLB_VSID_PTEM
) | ((epn
>> 16) & HPTE64_V_AVPN
);
661 ptem
|= HPTE64_V_VALID
;
663 /* Page address translation */
664 qemu_log_mask(CPU_LOG_MMU
,
665 "htab_base " TARGET_FMT_plx
" htab_mask " TARGET_FMT_plx
666 " hash " TARGET_FMT_plx
"\n",
667 ppc_hash64_hpt_base(cpu
), ppc_hash64_hpt_mask(cpu
), hash
);
669 /* Primary PTEG lookup */
670 qemu_log_mask(CPU_LOG_MMU
,
671 "0 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
672 " vsid=" TARGET_FMT_lx
" ptem=" TARGET_FMT_lx
673 " hash=" TARGET_FMT_plx
"\n",
674 ppc_hash64_hpt_base(cpu
), ppc_hash64_hpt_mask(cpu
),
676 ptex
= ppc_hash64_pteg_search(cpu
, hash
, sps
, ptem
, pte
, pshift
);
679 /* Secondary PTEG lookup */
680 ptem
|= HPTE64_V_SECONDARY
;
681 qemu_log_mask(CPU_LOG_MMU
,
682 "1 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
683 " vsid=" TARGET_FMT_lx
" api=" TARGET_FMT_lx
684 " hash=" TARGET_FMT_plx
"\n", ppc_hash64_hpt_base(cpu
),
685 ppc_hash64_hpt_mask(cpu
), vsid
, ptem
, ~hash
);
687 ptex
= ppc_hash64_pteg_search(cpu
, ~hash
, sps
, ptem
, pte
, pshift
);
693 unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU
*cpu
,
694 uint64_t pte0
, uint64_t pte1
)
698 if (!(pte0
& HPTE64_V_LARGE
)) {
703 * The encodings in env->sps need to be carefully chosen so that
704 * this gives an unambiguous result.
706 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
707 const PPCHash64SegmentPageSizes
*sps
= &cpu
->hash64_opts
->sps
[i
];
710 if (!sps
->page_shift
) {
714 shift
= hpte_page_shift(sps
, pte0
, pte1
);
723 static bool ppc_hash64_use_vrma(CPUPPCState
*env
)
725 switch (env
->mmu_model
) {
726 case POWERPC_MMU_3_00
:
728 * ISAv3.0 (POWER9) always uses VRMA, the VPM0 field and RMOR
729 * register no longer exist
734 return !!(env
->spr
[SPR_LPCR
] & LPCR_VPM0
);
738 static void ppc_hash64_set_isi(CPUState
*cs
, uint64_t error_code
)
740 CPUPPCState
*env
= &POWERPC_CPU(cs
)->env
;
744 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM1
);
746 vpm
= ppc_hash64_use_vrma(env
);
748 if (vpm
&& !msr_hv
) {
749 cs
->exception_index
= POWERPC_EXCP_HISI
;
751 cs
->exception_index
= POWERPC_EXCP_ISI
;
753 env
->error_code
= error_code
;
756 static void ppc_hash64_set_dsi(CPUState
*cs
, uint64_t dar
, uint64_t dsisr
)
758 CPUPPCState
*env
= &POWERPC_CPU(cs
)->env
;
762 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM1
);
764 vpm
= ppc_hash64_use_vrma(env
);
766 if (vpm
&& !msr_hv
) {
767 cs
->exception_index
= POWERPC_EXCP_HDSI
;
768 env
->spr
[SPR_HDAR
] = dar
;
769 env
->spr
[SPR_HDSISR
] = dsisr
;
771 cs
->exception_index
= POWERPC_EXCP_DSI
;
772 env
->spr
[SPR_DAR
] = dar
;
773 env
->spr
[SPR_DSISR
] = dsisr
;
779 static void ppc_hash64_set_r(PowerPCCPU
*cpu
, hwaddr ptex
, uint64_t pte1
)
781 hwaddr base
, offset
= ptex
* HASH_PTE_SIZE_64
+ 16;
784 PPCVirtualHypervisorClass
*vhc
=
785 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
786 vhc
->hpte_set_r(cpu
->vhyp
, ptex
, pte1
);
789 base
= ppc_hash64_hpt_base(cpu
);
792 /* The HW performs a non-atomic byte update */
793 stb_phys(CPU(cpu
)->as
, base
+ offset
, ((pte1
>> 8) & 0xff) | 0x01);
796 static void ppc_hash64_set_c(PowerPCCPU
*cpu
, hwaddr ptex
, uint64_t pte1
)
798 hwaddr base
, offset
= ptex
* HASH_PTE_SIZE_64
+ 15;
801 PPCVirtualHypervisorClass
*vhc
=
802 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
803 vhc
->hpte_set_c(cpu
->vhyp
, ptex
, pte1
);
806 base
= ppc_hash64_hpt_base(cpu
);
808 /* The HW performs a non-atomic byte update */
809 stb_phys(CPU(cpu
)->as
, base
+ offset
, (pte1
& 0xff) | 0x80);
812 static target_ulong
rmls_limit(PowerPCCPU
*cpu
)
814 CPUPPCState
*env
= &cpu
->env
;
816 * In theory the meanings of RMLS values are implementation
817 * dependent. In practice, this seems to have been the set from
818 * POWER4+..POWER8, and RMLS is no longer supported in POWER9.
820 * Unsupported values mean the OS has shot itself in the
821 * foot. Return a 0-sized RMA in this case, which we expect
822 * to trigger an immediate DSI or ISI
824 static const target_ulong rma_sizes
[16] = {
833 target_ulong rmls
= (env
->spr
[SPR_LPCR
] & LPCR_RMLS
) >> LPCR_RMLS_SHIFT
;
835 return rma_sizes
[rmls
];
838 static int build_vrma_slbe(PowerPCCPU
*cpu
, ppc_slb_t
*slb
)
840 CPUPPCState
*env
= &cpu
->env
;
841 target_ulong lpcr
= env
->spr
[SPR_LPCR
];
842 uint32_t vrmasd
= (lpcr
& LPCR_VRMASD
) >> LPCR_VRMASD_SHIFT
;
843 target_ulong vsid
= SLB_VSID_VRMA
| ((vrmasd
<< 4) & SLB_VSID_LLP_MASK
);
846 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
847 const PPCHash64SegmentPageSizes
*sps
= &cpu
->hash64_opts
->sps
[i
];
849 if (!sps
->page_shift
) {
853 if ((vsid
& SLB_VSID_LLP_MASK
) == sps
->slb_enc
) {
854 slb
->esid
= SLB_ESID_V
;
861 error_report("Bad page size encoding in LPCR[VRMASD]; LPCR=0x"
862 TARGET_FMT_lx
, lpcr
);
867 int ppc_hash64_handle_mmu_fault(PowerPCCPU
*cpu
, vaddr eaddr
,
868 int rwx
, int mmu_idx
)
870 CPUState
*cs
= CPU(cpu
);
871 CPUPPCState
*env
= &cpu
->env
;
876 ppc_hash_pte64_t pte
;
877 int exec_prot
, pp_prot
, amr_prot
, prot
;
878 const int need_prot
[] = {PAGE_READ
, PAGE_WRITE
, PAGE_EXEC
};
881 assert((rwx
== 0) || (rwx
== 1) || (rwx
== 2));
884 * Note on LPCR usage: 970 uses HID4, but our special variant of
885 * store_spr copies relevant fields into env->spr[SPR_LPCR].
886 * Similarily we filter unimplemented bits when storing into LPCR
887 * depending on the MMU version. This code can thus just use the
891 /* 1. Handle real mode accesses */
892 if (((rwx
== 2) && (msr_ir
== 0)) || ((rwx
!= 2) && (msr_dr
== 0))) {
894 * Translation is supposedly "off", but in real mode the top 4
895 * effective address bits are (mostly) ignored
897 raddr
= eaddr
& 0x0FFFFFFFFFFFFFFFULL
;
901 * In virtual hypervisor mode, there's nothing to do:
902 * EA == GPA == qemu guest address
904 } else if (msr_hv
|| !env
->has_hv_mode
) {
905 /* In HV mode, add HRMOR if top EA bit is clear */
906 if (!(eaddr
>> 63)) {
907 raddr
|= env
->spr
[SPR_HRMOR
];
909 } else if (ppc_hash64_use_vrma(env
)) {
910 /* Emulated VRMA mode */
912 if (build_vrma_slbe(cpu
, slb
) != 0) {
913 /* Invalid VRMA setup, machine check */
914 cs
->exception_index
= POWERPC_EXCP_MCHECK
;
919 goto skip_slb_search
;
921 target_ulong limit
= rmls_limit(cpu
);
923 /* Emulated old-style RMO mode, bounds check against RMLS */
924 if (raddr
>= limit
) {
926 ppc_hash64_set_isi(cs
, SRR1_PROTFAULT
);
928 int dsisr
= DSISR_PROTFAULT
;
930 dsisr
|= DSISR_ISSTORE
;
932 ppc_hash64_set_dsi(cs
, eaddr
, dsisr
);
937 raddr
|= env
->spr
[SPR_RMOR
];
939 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
, raddr
& TARGET_PAGE_MASK
,
940 PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
, mmu_idx
,
945 /* 2. Translation is on, so look up the SLB */
946 slb
= slb_lookup(cpu
, eaddr
);
948 /* No entry found, check if in-memory segment tables are in use */
949 if (ppc64_use_proc_tbl(cpu
)) {
950 /* TODO - Unsupported */
951 error_report("Segment Table Support Unimplemented");
954 /* Segment still not found, generate the appropriate interrupt */
956 cs
->exception_index
= POWERPC_EXCP_ISEG
;
959 cs
->exception_index
= POWERPC_EXCP_DSEG
;
961 env
->spr
[SPR_DAR
] = eaddr
;
968 /* 3. Check for segment level no-execute violation */
969 if ((rwx
== 2) && (slb
->vsid
& SLB_VSID_N
)) {
970 ppc_hash64_set_isi(cs
, SRR1_NOEXEC_GUARD
);
974 /* 4. Locate the PTE in the hash table */
975 ptex
= ppc_hash64_htab_lookup(cpu
, slb
, eaddr
, &pte
, &apshift
);
978 ppc_hash64_set_isi(cs
, SRR1_NOPTE
);
980 int dsisr
= DSISR_NOPTE
;
982 dsisr
|= DSISR_ISSTORE
;
984 ppc_hash64_set_dsi(cs
, eaddr
, dsisr
);
988 qemu_log_mask(CPU_LOG_MMU
,
989 "found PTE at index %08" HWADDR_PRIx
"\n", ptex
);
991 /* 5. Check access permissions */
993 exec_prot
= ppc_hash64_pte_noexec_guard(cpu
, pte
);
994 pp_prot
= ppc_hash64_pte_prot(cpu
, slb
, pte
);
995 amr_prot
= ppc_hash64_amr_prot(cpu
, pte
);
996 prot
= exec_prot
& pp_prot
& amr_prot
;
998 if ((need_prot
[rwx
] & ~prot
) != 0) {
999 /* Access right violation */
1000 qemu_log_mask(CPU_LOG_MMU
, "PTE access rejected\n");
1003 if (PAGE_EXEC
& ~exec_prot
) {
1004 srr1
|= SRR1_NOEXEC_GUARD
; /* Access violates noexec or guard */
1005 } else if (PAGE_EXEC
& ~pp_prot
) {
1006 srr1
|= SRR1_PROTFAULT
; /* Access violates access authority */
1008 if (PAGE_EXEC
& ~amr_prot
) {
1009 srr1
|= SRR1_IAMR
; /* Access violates virt pg class key prot */
1011 ppc_hash64_set_isi(cs
, srr1
);
1014 if (need_prot
[rwx
] & ~pp_prot
) {
1015 dsisr
|= DSISR_PROTFAULT
;
1018 dsisr
|= DSISR_ISSTORE
;
1020 if (need_prot
[rwx
] & ~amr_prot
) {
1023 ppc_hash64_set_dsi(cs
, eaddr
, dsisr
);
1028 qemu_log_mask(CPU_LOG_MMU
, "PTE access granted !\n");
1030 /* 6. Update PTE referenced and changed bits if necessary */
1032 if (!(pte
.pte1
& HPTE64_R_R
)) {
1033 ppc_hash64_set_r(cpu
, ptex
, pte
.pte1
);
1035 if (!(pte
.pte1
& HPTE64_R_C
)) {
1037 ppc_hash64_set_c(cpu
, ptex
, pte
.pte1
);
1040 * Treat the page as read-only for now, so that a later write
1041 * will pass through this function again to set the C bit
1043 prot
&= ~PAGE_WRITE
;
1047 /* 7. Determine the real address from the PTE */
1049 raddr
= deposit64(pte
.pte1
& HPTE64_R_RPN
, 0, apshift
, eaddr
);
1051 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
, raddr
& TARGET_PAGE_MASK
,
1052 prot
, mmu_idx
, 1ULL << apshift
);
1057 hwaddr
ppc_hash64_get_phys_page_debug(PowerPCCPU
*cpu
, target_ulong addr
)
1059 CPUPPCState
*env
= &cpu
->env
;
1060 ppc_slb_t vrma_slbe
;
1063 ppc_hash_pte64_t pte
;
1066 /* Handle real mode */
1068 /* In real mode the top 4 effective address bits are ignored */
1069 raddr
= addr
& 0x0FFFFFFFFFFFFFFFULL
;
1073 * In virtual hypervisor mode, there's nothing to do:
1074 * EA == GPA == qemu guest address
1077 } else if ((msr_hv
|| !env
->has_hv_mode
) && !(addr
>> 63)) {
1078 /* In HV mode, add HRMOR if top EA bit is clear */
1079 return raddr
| env
->spr
[SPR_HRMOR
];
1080 } else if (ppc_hash64_use_vrma(env
)) {
1081 /* Emulated VRMA mode */
1083 if (build_vrma_slbe(cpu
, slb
) != 0) {
1087 target_ulong limit
= rmls_limit(cpu
);
1089 /* Emulated old-style RMO mode, bounds check against RMLS */
1090 if (raddr
>= limit
) {
1093 return raddr
| env
->spr
[SPR_RMOR
];
1096 slb
= slb_lookup(cpu
, addr
);
1102 ptex
= ppc_hash64_htab_lookup(cpu
, slb
, addr
, &pte
, &apshift
);
1107 return deposit64(pte
.pte1
& HPTE64_R_RPN
, 0, apshift
, addr
)
1111 void ppc_hash64_tlb_flush_hpte(PowerPCCPU
*cpu
, target_ulong ptex
,
1112 target_ulong pte0
, target_ulong pte1
)
1115 * XXX: given the fact that there are too many segments to
1116 * invalidate, and we still don't have a tlb_flush_mask(env, n,
1117 * mask) in QEMU, we just invalidate all TLBs
1119 cpu
->env
.tlb_need_flush
= TLB_NEED_GLOBAL_FLUSH
| TLB_NEED_LOCAL_FLUSH
;
1122 void ppc_store_lpcr(PowerPCCPU
*cpu
, target_ulong val
)
1124 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
1125 CPUPPCState
*env
= &cpu
->env
;
1127 env
->spr
[SPR_LPCR
] = val
& pcc
->lpcr_mask
;
1130 void helper_store_lpcr(CPUPPCState
*env
, target_ulong val
)
1132 PowerPCCPU
*cpu
= env_archcpu(env
);
1134 ppc_store_lpcr(cpu
, val
);
1137 void ppc_hash64_init(PowerPCCPU
*cpu
)
1139 CPUPPCState
*env
= &cpu
->env
;
1140 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
1142 if (!pcc
->hash64_opts
) {
1143 assert(!(env
->mmu_model
& POWERPC_MMU_64
));
1147 cpu
->hash64_opts
= g_memdup(pcc
->hash64_opts
, sizeof(*cpu
->hash64_opts
));
1150 void ppc_hash64_finalize(PowerPCCPU
*cpu
)
1152 g_free(cpu
->hash64_opts
);
1155 const PPCHash64Options ppc_hash64_opts_basic
= {
1159 { .page_shift
= 12, /* 4K */
1161 .enc
= { { .page_shift
= 12, .pte_enc
= 0 } }
1163 { .page_shift
= 24, /* 16M */
1165 .enc
= { { .page_shift
= 24, .pte_enc
= 0 } }
1170 const PPCHash64Options ppc_hash64_opts_POWER7
= {
1171 .flags
= PPC_HASH64_1TSEG
| PPC_HASH64_AMR
| PPC_HASH64_CI_LARGEPAGE
,
1175 .page_shift
= 12, /* 4K */
1177 .enc
= { { .page_shift
= 12, .pte_enc
= 0 },
1178 { .page_shift
= 16, .pte_enc
= 0x7 },
1179 { .page_shift
= 24, .pte_enc
= 0x38 }, },
1182 .page_shift
= 16, /* 64K */
1183 .slb_enc
= SLB_VSID_64K
,
1184 .enc
= { { .page_shift
= 16, .pte_enc
= 0x1 },
1185 { .page_shift
= 24, .pte_enc
= 0x8 }, },
1188 .page_shift
= 24, /* 16M */
1189 .slb_enc
= SLB_VSID_16M
,
1190 .enc
= { { .page_shift
= 24, .pte_enc
= 0 }, },
1193 .page_shift
= 34, /* 16G */
1194 .slb_enc
= SLB_VSID_16G
,
1195 .enc
= { { .page_shift
= 34, .pte_enc
= 0x3 }, },
1200 void ppc_hash64_filter_pagesizes(PowerPCCPU
*cpu
,
1201 bool (*cb
)(void *, uint32_t, uint32_t),
1204 PPCHash64Options
*opts
= cpu
->hash64_opts
;
1207 bool ci_largepage
= false;
1212 for (i
= 0; i
< ARRAY_SIZE(opts
->sps
); i
++) {
1213 PPCHash64SegmentPageSizes
*sps
= &opts
->sps
[i
];
1219 if (!sps
->page_shift
) {
1223 for (j
= 0; j
< ARRAY_SIZE(sps
->enc
); j
++) {
1224 PPCHash64PageSize
*ps
= &sps
->enc
[j
];
1227 if (!ps
->page_shift
) {
1231 if (cb(opaque
, sps
->page_shift
, ps
->page_shift
)) {
1232 if (ps
->page_shift
>= 16) {
1233 ci_largepage
= true;
1235 sps
->enc
[m
++] = *ps
;
1239 /* Clear rest of the row */
1240 for (j
= m
; j
< ARRAY_SIZE(sps
->enc
); j
++) {
1241 memset(&sps
->enc
[j
], 0, sizeof(sps
->enc
[j
]));
1249 /* Clear the rest of the table */
1250 for (i
= n
; i
< ARRAY_SIZE(opts
->sps
); i
++) {
1251 memset(&opts
->sps
[i
], 0, sizeof(opts
->sps
[i
]));
1254 if (!ci_largepage
) {
1255 opts
->flags
&= ~PPC_HASH64_CI_LARGEPAGE
;