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.1 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 "qemu/error-report.h"
25 #include "qemu/qemu-print.h"
26 #include "sysemu/hw_accel.h"
28 #include "mmu-hash64.h"
32 #include "mmu-book3s-v3.h"
33 #include "helper_regs.h"
36 #include "exec/helper-proto.h"
39 /* #define DEBUG_SLB */
42 # define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
44 # define LOG_SLB(...) do { } while (0)
51 static ppc_slb_t
*slb_lookup(PowerPCCPU
*cpu
, target_ulong eaddr
)
53 CPUPPCState
*env
= &cpu
->env
;
54 uint64_t esid_256M
, esid_1T
;
57 LOG_SLB("%s: eaddr " TARGET_FMT_lx
"\n", __func__
, eaddr
);
59 esid_256M
= (eaddr
& SEGMENT_MASK_256M
) | SLB_ESID_V
;
60 esid_1T
= (eaddr
& SEGMENT_MASK_1T
) | SLB_ESID_V
;
62 for (n
= 0; n
< cpu
->hash64_opts
->slb_size
; n
++) {
63 ppc_slb_t
*slb
= &env
->slb
[n
];
65 LOG_SLB("%s: slot %d %016" PRIx64
" %016"
66 PRIx64
"\n", __func__
, n
, slb
->esid
, slb
->vsid
);
68 * We check for 1T matches on all MMUs here - if the MMU
69 * doesn't have 1T segment support, we will have prevented 1T
70 * entries from being inserted in the slbmte code.
72 if (((slb
->esid
== esid_256M
) &&
73 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_256M
))
74 || ((slb
->esid
== esid_1T
) &&
75 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_1T
))) {
83 void dump_slb(PowerPCCPU
*cpu
)
85 CPUPPCState
*env
= &cpu
->env
;
89 cpu_synchronize_state(CPU(cpu
));
91 qemu_printf("SLB\tESID\t\t\tVSID\n");
92 for (i
= 0; i
< cpu
->hash64_opts
->slb_size
; i
++) {
93 slbe
= env
->slb
[i
].esid
;
94 slbv
= env
->slb
[i
].vsid
;
95 if (slbe
== 0 && slbv
== 0) {
98 qemu_printf("%d\t0x%016" PRIx64
"\t0x%016" PRIx64
"\n",
104 void helper_slbia(CPUPPCState
*env
, uint32_t ih
)
106 PowerPCCPU
*cpu
= env_archcpu(env
);
111 * slbia must always flush all TLB (which is equivalent to ERAT in ppc
112 * architecture). Matching on SLB_ESID_V is not good enough, because slbmte
113 * can overwrite a valid SLB without flushing its lookaside information.
115 * It would be possible to keep the TLB in synch with the SLB by flushing
116 * when a valid entry is overwritten by slbmte, and therefore slbia would
117 * not have to flush unless it evicts a valid SLB entry. However it is
118 * expected that slbmte is more common than slbia, and slbia is usually
119 * going to evict valid SLB entries, so that tradeoff is unlikely to be a
122 * ISA v2.05 introduced IH field with values 0,1,2,6. These all invalidate
123 * the same SLB entries (everything but entry 0), but differ in what
124 * "lookaside information" is invalidated. TCG can ignore this and flush
127 * ISA v3.0 introduced additional values 3,4,7, which change what SLBs are
131 env
->tlb_need_flush
|= TLB_NEED_LOCAL_FLUSH
;
133 starting_entry
= 1; /* default for IH=0,1,2,6 */
135 if (env
->mmu_model
== POWERPC_MMU_3_00
) {
138 /* invalidate no SLBs, but all lookaside information */
143 /* also considers SLB entry 0 */
148 /* treat undefined values as ih==0, and warn */
149 qemu_log_mask(LOG_GUEST_ERROR
,
150 "slbia undefined IH field %u.\n", ih
);
159 for (n
= starting_entry
; n
< cpu
->hash64_opts
->slb_size
; n
++) {
160 ppc_slb_t
*slb
= &env
->slb
[n
];
162 if (!(slb
->esid
& SLB_ESID_V
)) {
165 if (env
->mmu_model
== POWERPC_MMU_3_00
) {
166 if (ih
== 0x3 && (slb
->vsid
& SLB_VSID_C
) == 0) {
167 /* preserves entries with a class value of 0 */
172 slb
->esid
&= ~SLB_ESID_V
;
176 static void __helper_slbie(CPUPPCState
*env
, target_ulong addr
,
179 PowerPCCPU
*cpu
= env_archcpu(env
);
182 slb
= slb_lookup(cpu
, addr
);
187 if (slb
->esid
& SLB_ESID_V
) {
188 slb
->esid
&= ~SLB_ESID_V
;
191 * XXX: given the fact that segment size is 256 MB or 1TB,
192 * and we still don't have a tlb_flush_mask(env, n, mask)
193 * in QEMU, we just invalidate all TLBs
195 env
->tlb_need_flush
|=
196 (global
== false ? TLB_NEED_LOCAL_FLUSH
: TLB_NEED_GLOBAL_FLUSH
);
200 void helper_slbie(CPUPPCState
*env
, target_ulong addr
)
202 __helper_slbie(env
, addr
, false);
205 void helper_slbieg(CPUPPCState
*env
, target_ulong addr
)
207 __helper_slbie(env
, addr
, true);
211 int ppc_store_slb(PowerPCCPU
*cpu
, target_ulong slot
,
212 target_ulong esid
, target_ulong vsid
)
214 CPUPPCState
*env
= &cpu
->env
;
215 ppc_slb_t
*slb
= &env
->slb
[slot
];
216 const PPCHash64SegmentPageSizes
*sps
= NULL
;
219 if (slot
>= cpu
->hash64_opts
->slb_size
) {
220 return -1; /* Bad slot number */
222 if (esid
& ~(SLB_ESID_ESID
| SLB_ESID_V
)) {
223 return -1; /* Reserved bits set */
225 if (vsid
& (SLB_VSID_B
& ~SLB_VSID_B_1T
)) {
226 return -1; /* Bad segment size */
228 if ((vsid
& SLB_VSID_B
) && !(ppc_hash64_has(cpu
, PPC_HASH64_1TSEG
))) {
229 return -1; /* 1T segment on MMU that doesn't support it */
232 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
233 const PPCHash64SegmentPageSizes
*sps1
= &cpu
->hash64_opts
->sps
[i
];
235 if (!sps1
->page_shift
) {
239 if ((vsid
& SLB_VSID_LLP_MASK
) == sps1
->slb_enc
) {
246 error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
247 " esid 0x"TARGET_FMT_lx
" vsid 0x"TARGET_FMT_lx
,
256 LOG_SLB("%s: " TARGET_FMT_lu
" " TARGET_FMT_lx
" - " TARGET_FMT_lx
257 " => %016" PRIx64
" %016" PRIx64
"\n", __func__
, slot
, esid
, vsid
,
258 slb
->esid
, slb
->vsid
);
264 static int ppc_load_slb_esid(PowerPCCPU
*cpu
, target_ulong rb
,
267 CPUPPCState
*env
= &cpu
->env
;
268 int slot
= rb
& 0xfff;
269 ppc_slb_t
*slb
= &env
->slb
[slot
];
271 if (slot
>= cpu
->hash64_opts
->slb_size
) {
279 static int ppc_load_slb_vsid(PowerPCCPU
*cpu
, target_ulong rb
,
282 CPUPPCState
*env
= &cpu
->env
;
283 int slot
= rb
& 0xfff;
284 ppc_slb_t
*slb
= &env
->slb
[slot
];
286 if (slot
>= cpu
->hash64_opts
->slb_size
) {
294 static int ppc_find_slb_vsid(PowerPCCPU
*cpu
, target_ulong rb
,
297 CPUPPCState
*env
= &cpu
->env
;
300 if (!msr_is_64bit(env
, env
->msr
)) {
303 slb
= slb_lookup(cpu
, rb
);
305 *rt
= (target_ulong
)-1ul;
312 void helper_store_slb(CPUPPCState
*env
, target_ulong rb
, target_ulong rs
)
314 PowerPCCPU
*cpu
= env_archcpu(env
);
316 if (ppc_store_slb(cpu
, rb
& 0xfff, rb
& ~0xfffULL
, rs
) < 0) {
317 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
318 POWERPC_EXCP_INVAL
, GETPC());
322 target_ulong
helper_load_slb_esid(CPUPPCState
*env
, target_ulong rb
)
324 PowerPCCPU
*cpu
= env_archcpu(env
);
327 if (ppc_load_slb_esid(cpu
, rb
, &rt
) < 0) {
328 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
329 POWERPC_EXCP_INVAL
, GETPC());
334 target_ulong
helper_find_slb_vsid(CPUPPCState
*env
, target_ulong rb
)
336 PowerPCCPU
*cpu
= env_archcpu(env
);
339 if (ppc_find_slb_vsid(cpu
, rb
, &rt
) < 0) {
340 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
341 POWERPC_EXCP_INVAL
, GETPC());
346 target_ulong
helper_load_slb_vsid(CPUPPCState
*env
, target_ulong rb
)
348 PowerPCCPU
*cpu
= env_archcpu(env
);
351 if (ppc_load_slb_vsid(cpu
, rb
, &rt
) < 0) {
352 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
353 POWERPC_EXCP_INVAL
, GETPC());
359 /* Check No-Execute or Guarded Storage */
360 static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU
*cpu
,
361 ppc_hash_pte64_t pte
)
363 /* Exec permissions CANNOT take away read or write permissions */
364 return (pte
.pte1
& HPTE64_R_N
) || (pte
.pte1
& HPTE64_R_G
) ?
365 PAGE_READ
| PAGE_WRITE
: PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
368 /* Check Basic Storage Protection */
369 static int ppc_hash64_pte_prot(int mmu_idx
,
370 ppc_slb_t
*slb
, ppc_hash_pte64_t pte
)
374 * Some pp bit combinations have undefined behaviour, so default
375 * to no access in those cases
379 key
= !!(mmuidx_pr(mmu_idx
) ? (slb
->vsid
& SLB_VSID_KP
)
380 : (slb
->vsid
& SLB_VSID_KS
));
381 pp
= (pte
.pte1
& HPTE64_R_PP
) | ((pte
.pte1
& HPTE64_R_PP0
) >> 61);
388 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
393 prot
= PAGE_READ
| PAGE_EXEC
;
404 prot
= PAGE_READ
| PAGE_EXEC
;
408 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
416 /* Check the instruction access permissions specified in the IAMR */
417 static int ppc_hash64_iamr_prot(PowerPCCPU
*cpu
, int key
)
419 CPUPPCState
*env
= &cpu
->env
;
420 int iamr_bits
= (env
->spr
[SPR_IAMR
] >> 2 * (31 - key
)) & 0x3;
423 * An instruction fetch is permitted if the IAMR bit is 0.
424 * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit
425 * can only take away EXEC permissions not READ or WRITE permissions.
426 * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since
427 * EXEC permissions are allowed.
429 return (iamr_bits
& 0x1) ? PAGE_READ
| PAGE_WRITE
:
430 PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
433 static int ppc_hash64_amr_prot(PowerPCCPU
*cpu
, ppc_hash_pte64_t pte
)
435 CPUPPCState
*env
= &cpu
->env
;
437 int prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
439 /* Only recent MMUs implement Virtual Page Class Key Protection */
440 if (!ppc_hash64_has(cpu
, PPC_HASH64_AMR
)) {
444 key
= HPTE64_R_KEY(pte
.pte1
);
445 amrbits
= (env
->spr
[SPR_AMR
] >> 2 * (31 - key
)) & 0x3;
447 /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
448 /* env->spr[SPR_AMR]); */
451 * A store is permitted if the AMR bit is 0. Remove write
452 * protection if it is set.
458 * A load is permitted if the AMR bit is 0. Remove read
459 * protection if it is set.
465 switch (env
->mmu_model
) {
467 * MMU version 2.07 and later support IAMR
468 * Check if the IAMR allows the instruction access - it will return
469 * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0
470 * if it does (and prot will be unchanged indicating execution support).
472 case POWERPC_MMU_2_07
:
473 case POWERPC_MMU_3_00
:
474 prot
&= ppc_hash64_iamr_prot(cpu
, key
);
483 const ppc_hash_pte64_t
*ppc_hash64_map_hptes(PowerPCCPU
*cpu
,
486 hwaddr pte_offset
= ptex
* HASH_PTE_SIZE_64
;
488 hwaddr plen
= n
* HASH_PTE_SIZE_64
;
489 const ppc_hash_pte64_t
*hptes
;
492 PPCVirtualHypervisorClass
*vhc
=
493 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
494 return vhc
->map_hptes(cpu
->vhyp
, ptex
, n
);
496 base
= ppc_hash64_hpt_base(cpu
);
502 hptes
= address_space_map(CPU(cpu
)->as
, base
+ pte_offset
, &plen
, false,
503 MEMTXATTRS_UNSPECIFIED
);
504 if (plen
< (n
* HASH_PTE_SIZE_64
)) {
505 hw_error("%s: Unable to map all requested HPTEs\n", __func__
);
510 void ppc_hash64_unmap_hptes(PowerPCCPU
*cpu
, const ppc_hash_pte64_t
*hptes
,
514 PPCVirtualHypervisorClass
*vhc
=
515 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
516 vhc
->unmap_hptes(cpu
->vhyp
, hptes
, ptex
, n
);
520 address_space_unmap(CPU(cpu
)->as
, (void *)hptes
, n
* HASH_PTE_SIZE_64
,
521 false, n
* HASH_PTE_SIZE_64
);
524 static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes
*sps
,
525 uint64_t pte0
, uint64_t pte1
)
529 if (!(pte0
& HPTE64_V_LARGE
)) {
530 if (sps
->page_shift
!= 12) {
531 /* 4kiB page in a non 4kiB segment */
534 /* Normal 4kiB page */
538 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
539 const PPCHash64PageSize
*ps
= &sps
->enc
[i
];
542 if (!ps
->page_shift
) {
546 if (ps
->page_shift
== 12) {
547 /* L bit is set so this can't be a 4kiB page */
551 mask
= ((1ULL << ps
->page_shift
) - 1) & HPTE64_R_RPN
;
553 if ((pte1
& mask
) == ((uint64_t)ps
->pte_enc
<< HPTE64_R_RPN_SHIFT
)) {
554 return ps
->page_shift
;
558 return 0; /* Bad page size encoding */
561 static void ppc64_v3_new_to_old_hpte(target_ulong
*pte0
, target_ulong
*pte1
)
563 /* Insert B into pte0 */
564 *pte0
= (*pte0
& HPTE64_V_COMMON_BITS
) |
565 ((*pte1
& HPTE64_R_3_0_SSIZE_MASK
) <<
566 (HPTE64_V_SSIZE_SHIFT
- HPTE64_R_3_0_SSIZE_SHIFT
));
568 /* Remove B from pte1 */
569 *pte1
= *pte1
& ~HPTE64_R_3_0_SSIZE_MASK
;
573 static hwaddr
ppc_hash64_pteg_search(PowerPCCPU
*cpu
, hwaddr hash
,
574 const PPCHash64SegmentPageSizes
*sps
,
576 ppc_hash_pte64_t
*pte
, unsigned *pshift
)
579 const ppc_hash_pte64_t
*pteg
;
580 target_ulong pte0
, pte1
;
583 ptex
= (hash
& ppc_hash64_hpt_mask(cpu
)) * HPTES_PER_GROUP
;
584 pteg
= ppc_hash64_map_hptes(cpu
, ptex
, HPTES_PER_GROUP
);
588 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
589 pte0
= ppc_hash64_hpte0(cpu
, pteg
, i
);
591 * pte0 contains the valid bit and must be read before pte1,
592 * otherwise we might see an old pte1 with a new valid bit and
593 * thus an inconsistent hpte value
596 pte1
= ppc_hash64_hpte1(cpu
, pteg
, i
);
598 /* Convert format if necessary */
599 if (cpu
->env
.mmu_model
== POWERPC_MMU_3_00
&& !cpu
->vhyp
) {
600 ppc64_v3_new_to_old_hpte(&pte0
, &pte1
);
603 /* This compares V, B, H (secondary) and the AVPN */
604 if (HPTE64_V_COMPARE(pte0
, ptem
)) {
605 *pshift
= hpte_page_shift(sps
, pte0
, pte1
);
607 * If there is no match, ignore the PTE, it could simply
608 * be for a different segment size encoding and the
609 * architecture specifies we should not match. Linux will
610 * potentially leave behind PTEs for the wrong base page
611 * size when demoting segments.
617 * We don't do anything with pshift yet as qemu TLB only
618 * deals with 4K pages anyway
622 ppc_hash64_unmap_hptes(cpu
, pteg
, ptex
, HPTES_PER_GROUP
);
626 ppc_hash64_unmap_hptes(cpu
, pteg
, ptex
, HPTES_PER_GROUP
);
628 * We didn't find a valid entry.
633 static hwaddr
ppc_hash64_htab_lookup(PowerPCCPU
*cpu
,
634 ppc_slb_t
*slb
, target_ulong eaddr
,
635 ppc_hash_pte64_t
*pte
, unsigned *pshift
)
637 CPUPPCState
*env
= &cpu
->env
;
639 uint64_t vsid
, epnmask
, epn
, ptem
;
640 const PPCHash64SegmentPageSizes
*sps
= slb
->sps
;
643 * The SLB store path should prevent any bad page size encodings
644 * getting in there, so:
648 /* If ISL is set in LPCR we need to clamp the page size to 4K */
649 if (env
->spr
[SPR_LPCR
] & LPCR_ISL
) {
650 /* We assume that when using TCG, 4k is first entry of SPS */
651 sps
= &cpu
->hash64_opts
->sps
[0];
652 assert(sps
->page_shift
== 12);
655 epnmask
= ~((1ULL << sps
->page_shift
) - 1);
657 if (slb
->vsid
& SLB_VSID_B
) {
659 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT_1T
;
660 epn
= (eaddr
& ~SEGMENT_MASK_1T
) & epnmask
;
661 hash
= vsid
^ (vsid
<< 25) ^ (epn
>> sps
->page_shift
);
664 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT
;
665 epn
= (eaddr
& ~SEGMENT_MASK_256M
) & epnmask
;
666 hash
= vsid
^ (epn
>> sps
->page_shift
);
668 ptem
= (slb
->vsid
& SLB_VSID_PTEM
) | ((epn
>> 16) & HPTE64_V_AVPN
);
669 ptem
|= HPTE64_V_VALID
;
671 /* Page address translation */
672 qemu_log_mask(CPU_LOG_MMU
,
673 "htab_base " TARGET_FMT_plx
" htab_mask " TARGET_FMT_plx
674 " hash " TARGET_FMT_plx
"\n",
675 ppc_hash64_hpt_base(cpu
), ppc_hash64_hpt_mask(cpu
), hash
);
677 /* Primary PTEG lookup */
678 qemu_log_mask(CPU_LOG_MMU
,
679 "0 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
680 " vsid=" TARGET_FMT_lx
" ptem=" TARGET_FMT_lx
681 " hash=" TARGET_FMT_plx
"\n",
682 ppc_hash64_hpt_base(cpu
), ppc_hash64_hpt_mask(cpu
),
684 ptex
= ppc_hash64_pteg_search(cpu
, hash
, sps
, ptem
, pte
, pshift
);
687 /* Secondary PTEG lookup */
688 ptem
|= HPTE64_V_SECONDARY
;
689 qemu_log_mask(CPU_LOG_MMU
,
690 "1 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
691 " vsid=" TARGET_FMT_lx
" api=" TARGET_FMT_lx
692 " hash=" TARGET_FMT_plx
"\n", ppc_hash64_hpt_base(cpu
),
693 ppc_hash64_hpt_mask(cpu
), vsid
, ptem
, ~hash
);
695 ptex
= ppc_hash64_pteg_search(cpu
, ~hash
, sps
, ptem
, pte
, pshift
);
701 unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU
*cpu
,
702 uint64_t pte0
, uint64_t pte1
)
706 if (!(pte0
& HPTE64_V_LARGE
)) {
711 * The encodings in env->sps need to be carefully chosen so that
712 * this gives an unambiguous result.
714 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
715 const PPCHash64SegmentPageSizes
*sps
= &cpu
->hash64_opts
->sps
[i
];
718 if (!sps
->page_shift
) {
722 shift
= hpte_page_shift(sps
, pte0
, pte1
);
731 static bool ppc_hash64_use_vrma(CPUPPCState
*env
)
733 switch (env
->mmu_model
) {
734 case POWERPC_MMU_3_00
:
736 * ISAv3.0 (POWER9) always uses VRMA, the VPM0 field and RMOR
737 * register no longer exist
742 return !!(env
->spr
[SPR_LPCR
] & LPCR_VPM0
);
746 static void ppc_hash64_set_isi(CPUState
*cs
, int mmu_idx
, uint64_t error_code
)
748 CPUPPCState
*env
= &POWERPC_CPU(cs
)->env
;
751 if (!mmuidx_real(mmu_idx
)) {
752 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM1
);
754 vpm
= ppc_hash64_use_vrma(env
);
756 if (vpm
&& !mmuidx_hv(mmu_idx
)) {
757 cs
->exception_index
= POWERPC_EXCP_HISI
;
759 cs
->exception_index
= POWERPC_EXCP_ISI
;
761 env
->error_code
= error_code
;
764 static void ppc_hash64_set_dsi(CPUState
*cs
, int mmu_idx
, uint64_t dar
, uint64_t dsisr
)
766 CPUPPCState
*env
= &POWERPC_CPU(cs
)->env
;
769 if (!mmuidx_real(mmu_idx
)) {
770 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM1
);
772 vpm
= ppc_hash64_use_vrma(env
);
774 if (vpm
&& !mmuidx_hv(mmu_idx
)) {
775 cs
->exception_index
= POWERPC_EXCP_HDSI
;
776 env
->spr
[SPR_HDAR
] = dar
;
777 env
->spr
[SPR_HDSISR
] = dsisr
;
779 cs
->exception_index
= POWERPC_EXCP_DSI
;
780 env
->spr
[SPR_DAR
] = dar
;
781 env
->spr
[SPR_DSISR
] = dsisr
;
787 static void ppc_hash64_set_r(PowerPCCPU
*cpu
, hwaddr ptex
, uint64_t pte1
)
789 hwaddr base
, offset
= ptex
* HASH_PTE_SIZE_64
+ HPTE64_DW1_R
;
792 PPCVirtualHypervisorClass
*vhc
=
793 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
794 vhc
->hpte_set_r(cpu
->vhyp
, ptex
, pte1
);
797 base
= ppc_hash64_hpt_base(cpu
);
800 /* The HW performs a non-atomic byte update */
801 stb_phys(CPU(cpu
)->as
, base
+ offset
, ((pte1
>> 8) & 0xff) | 0x01);
804 static void ppc_hash64_set_c(PowerPCCPU
*cpu
, hwaddr ptex
, uint64_t pte1
)
806 hwaddr base
, offset
= ptex
* HASH_PTE_SIZE_64
+ HPTE64_DW1_C
;
809 PPCVirtualHypervisorClass
*vhc
=
810 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
811 vhc
->hpte_set_c(cpu
->vhyp
, ptex
, pte1
);
814 base
= ppc_hash64_hpt_base(cpu
);
816 /* The HW performs a non-atomic byte update */
817 stb_phys(CPU(cpu
)->as
, base
+ offset
, (pte1
& 0xff) | 0x80);
820 static target_ulong
rmls_limit(PowerPCCPU
*cpu
)
822 CPUPPCState
*env
= &cpu
->env
;
824 * In theory the meanings of RMLS values are implementation
825 * dependent. In practice, this seems to have been the set from
826 * POWER4+..POWER8, and RMLS is no longer supported in POWER9.
828 * Unsupported values mean the OS has shot itself in the
829 * foot. Return a 0-sized RMA in this case, which we expect
830 * to trigger an immediate DSI or ISI
832 static const target_ulong rma_sizes
[16] = {
841 target_ulong rmls
= (env
->spr
[SPR_LPCR
] & LPCR_RMLS
) >> LPCR_RMLS_SHIFT
;
843 return rma_sizes
[rmls
];
846 static int build_vrma_slbe(PowerPCCPU
*cpu
, ppc_slb_t
*slb
)
848 CPUPPCState
*env
= &cpu
->env
;
849 target_ulong lpcr
= env
->spr
[SPR_LPCR
];
850 uint32_t vrmasd
= (lpcr
& LPCR_VRMASD
) >> LPCR_VRMASD_SHIFT
;
851 target_ulong vsid
= SLB_VSID_VRMA
| ((vrmasd
<< 4) & SLB_VSID_LLP_MASK
);
854 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
855 const PPCHash64SegmentPageSizes
*sps
= &cpu
->hash64_opts
->sps
[i
];
857 if (!sps
->page_shift
) {
861 if ((vsid
& SLB_VSID_LLP_MASK
) == sps
->slb_enc
) {
862 slb
->esid
= SLB_ESID_V
;
869 error_report("Bad page size encoding in LPCR[VRMASD]; LPCR=0x"
870 TARGET_FMT_lx
, lpcr
);
875 bool ppc_hash64_xlate(PowerPCCPU
*cpu
, vaddr eaddr
, MMUAccessType access_type
,
876 hwaddr
*raddrp
, int *psizep
, int *protp
, int mmu_idx
,
879 CPUState
*cs
= CPU(cpu
);
880 CPUPPCState
*env
= &cpu
->env
;
885 ppc_hash_pte64_t pte
;
886 int exec_prot
, pp_prot
, amr_prot
, prot
;
891 * Note on LPCR usage: 970 uses HID4, but our special variant of
892 * store_spr copies relevant fields into env->spr[SPR_LPCR].
893 * Similarly we filter unimplemented bits when storing into LPCR
894 * depending on the MMU version. This code can thus just use the
898 /* 1. Handle real mode accesses */
899 if (mmuidx_real(mmu_idx
)) {
901 * Translation is supposedly "off", but in real mode the top 4
902 * effective address bits are (mostly) ignored
904 raddr
= eaddr
& 0x0FFFFFFFFFFFFFFFULL
;
908 * In virtual hypervisor mode, there's nothing to do:
909 * EA == GPA == qemu guest address
911 } else if (mmuidx_hv(mmu_idx
) || !env
->has_hv_mode
) {
912 /* In HV mode, add HRMOR if top EA bit is clear */
913 if (!(eaddr
>> 63)) {
914 raddr
|= env
->spr
[SPR_HRMOR
];
916 } else if (ppc_hash64_use_vrma(env
)) {
917 /* Emulated VRMA mode */
919 if (build_vrma_slbe(cpu
, slb
) != 0) {
920 /* Invalid VRMA setup, machine check */
922 cs
->exception_index
= POWERPC_EXCP_MCHECK
;
928 goto skip_slb_search
;
930 target_ulong limit
= rmls_limit(cpu
);
932 /* Emulated old-style RMO mode, bounds check against RMLS */
933 if (raddr
>= limit
) {
934 if (!guest_visible
) {
937 switch (access_type
) {
939 ppc_hash64_set_isi(cs
, mmu_idx
, SRR1_PROTFAULT
);
942 ppc_hash64_set_dsi(cs
, mmu_idx
, eaddr
, DSISR_PROTFAULT
);
945 ppc_hash64_set_dsi(cs
, mmu_idx
, eaddr
,
946 DSISR_PROTFAULT
| DSISR_ISSTORE
);
949 g_assert_not_reached();
954 raddr
|= env
->spr
[SPR_RMOR
];
958 *protp
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
959 *psizep
= TARGET_PAGE_BITS
;
963 /* 2. Translation is on, so look up the SLB */
964 slb
= slb_lookup(cpu
, eaddr
);
966 /* No entry found, check if in-memory segment tables are in use */
967 if (ppc64_use_proc_tbl(cpu
)) {
968 /* TODO - Unsupported */
969 error_report("Segment Table Support Unimplemented");
972 /* Segment still not found, generate the appropriate interrupt */
973 if (!guest_visible
) {
976 switch (access_type
) {
978 cs
->exception_index
= POWERPC_EXCP_ISEG
;
983 cs
->exception_index
= POWERPC_EXCP_DSEG
;
985 env
->spr
[SPR_DAR
] = eaddr
;
988 g_assert_not_reached();
995 /* 3. Check for segment level no-execute violation */
996 if (access_type
== MMU_INST_FETCH
&& (slb
->vsid
& SLB_VSID_N
)) {
998 ppc_hash64_set_isi(cs
, mmu_idx
, SRR1_NOEXEC_GUARD
);
1003 /* 4. Locate the PTE in the hash table */
1004 ptex
= ppc_hash64_htab_lookup(cpu
, slb
, eaddr
, &pte
, &apshift
);
1006 if (!guest_visible
) {
1009 switch (access_type
) {
1010 case MMU_INST_FETCH
:
1011 ppc_hash64_set_isi(cs
, mmu_idx
, SRR1_NOPTE
);
1014 ppc_hash64_set_dsi(cs
, mmu_idx
, eaddr
, DSISR_NOPTE
);
1016 case MMU_DATA_STORE
:
1017 ppc_hash64_set_dsi(cs
, mmu_idx
, eaddr
, DSISR_NOPTE
| DSISR_ISSTORE
);
1020 g_assert_not_reached();
1024 qemu_log_mask(CPU_LOG_MMU
,
1025 "found PTE at index %08" HWADDR_PRIx
"\n", ptex
);
1027 /* 5. Check access permissions */
1029 exec_prot
= ppc_hash64_pte_noexec_guard(cpu
, pte
);
1030 pp_prot
= ppc_hash64_pte_prot(mmu_idx
, slb
, pte
);
1031 amr_prot
= ppc_hash64_amr_prot(cpu
, pte
);
1032 prot
= exec_prot
& pp_prot
& amr_prot
;
1034 need_prot
= prot_for_access_type(access_type
);
1035 if (need_prot
& ~prot
) {
1036 /* Access right violation */
1037 qemu_log_mask(CPU_LOG_MMU
, "PTE access rejected\n");
1038 if (!guest_visible
) {
1041 if (access_type
== MMU_INST_FETCH
) {
1043 if (PAGE_EXEC
& ~exec_prot
) {
1044 srr1
|= SRR1_NOEXEC_GUARD
; /* Access violates noexec or guard */
1045 } else if (PAGE_EXEC
& ~pp_prot
) {
1046 srr1
|= SRR1_PROTFAULT
; /* Access violates access authority */
1048 if (PAGE_EXEC
& ~amr_prot
) {
1049 srr1
|= SRR1_IAMR
; /* Access violates virt pg class key prot */
1051 ppc_hash64_set_isi(cs
, mmu_idx
, srr1
);
1054 if (need_prot
& ~pp_prot
) {
1055 dsisr
|= DSISR_PROTFAULT
;
1057 if (access_type
== MMU_DATA_STORE
) {
1058 dsisr
|= DSISR_ISSTORE
;
1060 if (need_prot
& ~amr_prot
) {
1063 ppc_hash64_set_dsi(cs
, mmu_idx
, eaddr
, dsisr
);
1068 qemu_log_mask(CPU_LOG_MMU
, "PTE access granted !\n");
1070 /* 6. Update PTE referenced and changed bits if necessary */
1072 if (!(pte
.pte1
& HPTE64_R_R
)) {
1073 ppc_hash64_set_r(cpu
, ptex
, pte
.pte1
);
1075 if (!(pte
.pte1
& HPTE64_R_C
)) {
1076 if (access_type
== MMU_DATA_STORE
) {
1077 ppc_hash64_set_c(cpu
, ptex
, pte
.pte1
);
1080 * Treat the page as read-only for now, so that a later write
1081 * will pass through this function again to set the C bit
1083 prot
&= ~PAGE_WRITE
;
1087 /* 7. Determine the real address from the PTE */
1089 *raddrp
= deposit64(pte
.pte1
& HPTE64_R_RPN
, 0, apshift
, eaddr
);
1095 void ppc_hash64_tlb_flush_hpte(PowerPCCPU
*cpu
, target_ulong ptex
,
1096 target_ulong pte0
, target_ulong pte1
)
1099 * XXX: given the fact that there are too many segments to
1100 * invalidate, and we still don't have a tlb_flush_mask(env, n,
1101 * mask) in QEMU, we just invalidate all TLBs
1103 cpu
->env
.tlb_need_flush
= TLB_NEED_GLOBAL_FLUSH
| TLB_NEED_LOCAL_FLUSH
;
1107 void helper_store_lpcr(CPUPPCState
*env
, target_ulong val
)
1109 PowerPCCPU
*cpu
= env_archcpu(env
);
1111 ppc_store_lpcr(cpu
, val
);
1115 void ppc_hash64_init(PowerPCCPU
*cpu
)
1117 CPUPPCState
*env
= &cpu
->env
;
1118 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
1120 if (!pcc
->hash64_opts
) {
1121 assert(!mmu_is_64bit(env
->mmu_model
));
1125 cpu
->hash64_opts
= g_memdup(pcc
->hash64_opts
, sizeof(*cpu
->hash64_opts
));
1128 void ppc_hash64_finalize(PowerPCCPU
*cpu
)
1130 g_free(cpu
->hash64_opts
);
1133 const PPCHash64Options ppc_hash64_opts_basic
= {
1137 { .page_shift
= 12, /* 4K */
1139 .enc
= { { .page_shift
= 12, .pte_enc
= 0 } }
1141 { .page_shift
= 24, /* 16M */
1143 .enc
= { { .page_shift
= 24, .pte_enc
= 0 } }
1148 const PPCHash64Options ppc_hash64_opts_POWER7
= {
1149 .flags
= PPC_HASH64_1TSEG
| PPC_HASH64_AMR
| PPC_HASH64_CI_LARGEPAGE
,
1153 .page_shift
= 12, /* 4K */
1155 .enc
= { { .page_shift
= 12, .pte_enc
= 0 },
1156 { .page_shift
= 16, .pte_enc
= 0x7 },
1157 { .page_shift
= 24, .pte_enc
= 0x38 }, },
1160 .page_shift
= 16, /* 64K */
1161 .slb_enc
= SLB_VSID_64K
,
1162 .enc
= { { .page_shift
= 16, .pte_enc
= 0x1 },
1163 { .page_shift
= 24, .pte_enc
= 0x8 }, },
1166 .page_shift
= 24, /* 16M */
1167 .slb_enc
= SLB_VSID_16M
,
1168 .enc
= { { .page_shift
= 24, .pte_enc
= 0 }, },
1171 .page_shift
= 34, /* 16G */
1172 .slb_enc
= SLB_VSID_16G
,
1173 .enc
= { { .page_shift
= 34, .pte_enc
= 0x3 }, },