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 "exec/page-protection.h"
25 #include "qemu/error-report.h"
26 #include "qemu/qemu-print.h"
27 #include "sysemu/hw_accel.h"
29 #include "mmu-hash64.h"
33 #include "mmu-book3s-v3.h"
34 #include "mmu-books.h"
35 #include "helper_regs.h"
38 #include "exec/helper-proto.h"
41 /* #define DEBUG_SLB */
44 # define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
46 # define LOG_SLB(...) do { } while (0)
53 static ppc_slb_t
*slb_lookup(PowerPCCPU
*cpu
, target_ulong eaddr
)
55 CPUPPCState
*env
= &cpu
->env
;
56 uint64_t esid_256M
, esid_1T
;
59 LOG_SLB("%s: eaddr " TARGET_FMT_lx
"\n", __func__
, eaddr
);
61 esid_256M
= (eaddr
& SEGMENT_MASK_256M
) | SLB_ESID_V
;
62 esid_1T
= (eaddr
& SEGMENT_MASK_1T
) | SLB_ESID_V
;
64 for (n
= 0; n
< cpu
->hash64_opts
->slb_size
; n
++) {
65 ppc_slb_t
*slb
= &env
->slb
[n
];
67 LOG_SLB("%s: slot %d %016" PRIx64
" %016"
68 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.
74 if (((slb
->esid
== esid_256M
) &&
75 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_256M
))
76 || ((slb
->esid
== esid_1T
) &&
77 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_1T
))) {
85 void dump_slb(PowerPCCPU
*cpu
)
87 CPUPPCState
*env
= &cpu
->env
;
91 cpu_synchronize_state(CPU(cpu
));
93 qemu_printf("SLB\tESID\t\t\tVSID\n");
94 for (i
= 0; i
< cpu
->hash64_opts
->slb_size
; i
++) {
95 slbe
= env
->slb
[i
].esid
;
96 slbv
= env
->slb
[i
].vsid
;
97 if (slbe
== 0 && slbv
== 0) {
100 qemu_printf("%d\t0x%016" PRIx64
"\t0x%016" PRIx64
"\n",
106 void helper_SLBIA(CPUPPCState
*env
, uint32_t ih
)
108 PowerPCCPU
*cpu
= env_archcpu(env
);
113 * slbia must always flush all TLB (which is equivalent to ERAT in ppc
114 * architecture). Matching on SLB_ESID_V is not good enough, because slbmte
115 * can overwrite a valid SLB without flushing its lookaside information.
117 * It would be possible to keep the TLB in synch with the SLB by flushing
118 * when a valid entry is overwritten by slbmte, and therefore slbia would
119 * not have to flush unless it evicts a valid SLB entry. However it is
120 * expected that slbmte is more common than slbia, and slbia is usually
121 * going to evict valid SLB entries, so that tradeoff is unlikely to be a
124 * ISA v2.05 introduced IH field with values 0,1,2,6. These all invalidate
125 * the same SLB entries (everything but entry 0), but differ in what
126 * "lookaside information" is invalidated. TCG can ignore this and flush
129 * ISA v3.0 introduced additional values 3,4,7, which change what SLBs are
133 env
->tlb_need_flush
|= TLB_NEED_LOCAL_FLUSH
;
135 starting_entry
= 1; /* default for IH=0,1,2,6 */
137 if (env
->mmu_model
== POWERPC_MMU_3_00
) {
140 /* invalidate no SLBs, but all lookaside information */
145 /* also considers SLB entry 0 */
150 /* treat undefined values as ih==0, and warn */
151 qemu_log_mask(LOG_GUEST_ERROR
,
152 "slbia undefined IH field %u.\n", ih
);
161 for (n
= starting_entry
; n
< cpu
->hash64_opts
->slb_size
; n
++) {
162 ppc_slb_t
*slb
= &env
->slb
[n
];
164 if (!(slb
->esid
& SLB_ESID_V
)) {
167 if (env
->mmu_model
== POWERPC_MMU_3_00
) {
168 if (ih
== 0x3 && (slb
->vsid
& SLB_VSID_C
) == 0) {
169 /* preserves entries with a class value of 0 */
174 slb
->esid
&= ~SLB_ESID_V
;
178 #if defined(TARGET_PPC64)
179 void helper_SLBIAG(CPUPPCState
*env
, target_ulong rs
, uint32_t l
)
181 PowerPCCPU
*cpu
= env_archcpu(env
);
185 * slbiag must always flush all TLB (which is equivalent to ERAT in ppc
186 * architecture). Matching on SLB_ESID_V is not good enough, because slbmte
187 * can overwrite a valid SLB without flushing its lookaside information.
189 * It would be possible to keep the TLB in synch with the SLB by flushing
190 * when a valid entry is overwritten by slbmte, and therefore slbiag would
191 * not have to flush unless it evicts a valid SLB entry. However it is
192 * expected that slbmte is more common than slbiag, and slbiag is usually
193 * going to evict valid SLB entries, so that tradeoff is unlikely to be a
196 env
->tlb_need_flush
|= TLB_NEED_LOCAL_FLUSH
;
198 for (n
= 0; n
< cpu
->hash64_opts
->slb_size
; n
++) {
199 ppc_slb_t
*slb
= &env
->slb
[n
];
200 slb
->esid
&= ~SLB_ESID_V
;
205 static void __helper_slbie(CPUPPCState
*env
, target_ulong addr
,
208 PowerPCCPU
*cpu
= env_archcpu(env
);
211 slb
= slb_lookup(cpu
, addr
);
216 if (slb
->esid
& SLB_ESID_V
) {
217 slb
->esid
&= ~SLB_ESID_V
;
220 * XXX: given the fact that segment size is 256 MB or 1TB,
221 * and we still don't have a tlb_flush_mask(env, n, mask)
222 * in QEMU, we just invalidate all TLBs
224 env
->tlb_need_flush
|=
225 (global
== false ? TLB_NEED_LOCAL_FLUSH
: TLB_NEED_GLOBAL_FLUSH
);
229 void helper_SLBIE(CPUPPCState
*env
, target_ulong addr
)
231 __helper_slbie(env
, addr
, false);
234 void helper_SLBIEG(CPUPPCState
*env
, target_ulong addr
)
236 __helper_slbie(env
, addr
, true);
240 int ppc_store_slb(PowerPCCPU
*cpu
, target_ulong slot
,
241 target_ulong esid
, target_ulong vsid
)
243 CPUPPCState
*env
= &cpu
->env
;
244 ppc_slb_t
*slb
= &env
->slb
[slot
];
245 const PPCHash64SegmentPageSizes
*sps
= NULL
;
248 if (slot
>= cpu
->hash64_opts
->slb_size
) {
249 return -1; /* Bad slot number */
251 if (esid
& ~(SLB_ESID_ESID
| SLB_ESID_V
)) {
252 return -1; /* Reserved bits set */
254 if (vsid
& (SLB_VSID_B
& ~SLB_VSID_B_1T
)) {
255 return -1; /* Bad segment size */
257 if ((vsid
& SLB_VSID_B
) && !(ppc_hash64_has(cpu
, PPC_HASH64_1TSEG
))) {
258 return -1; /* 1T segment on MMU that doesn't support it */
261 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
262 const PPCHash64SegmentPageSizes
*sps1
= &cpu
->hash64_opts
->sps
[i
];
264 if (!sps1
->page_shift
) {
268 if ((vsid
& SLB_VSID_LLP_MASK
) == sps1
->slb_enc
) {
275 error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
276 " esid 0x"TARGET_FMT_lx
" vsid 0x"TARGET_FMT_lx
,
285 LOG_SLB("%s: " TARGET_FMT_lu
" " TARGET_FMT_lx
" - " TARGET_FMT_lx
286 " => %016" PRIx64
" %016" PRIx64
"\n", __func__
, slot
, esid
, vsid
,
287 slb
->esid
, slb
->vsid
);
293 static int ppc_load_slb_esid(PowerPCCPU
*cpu
, target_ulong rb
,
296 CPUPPCState
*env
= &cpu
->env
;
297 int slot
= rb
& 0xfff;
298 ppc_slb_t
*slb
= &env
->slb
[slot
];
300 if (slot
>= cpu
->hash64_opts
->slb_size
) {
308 static int ppc_load_slb_vsid(PowerPCCPU
*cpu
, target_ulong rb
,
311 CPUPPCState
*env
= &cpu
->env
;
312 int slot
= rb
& 0xfff;
313 ppc_slb_t
*slb
= &env
->slb
[slot
];
315 if (slot
>= cpu
->hash64_opts
->slb_size
) {
323 static int ppc_find_slb_vsid(PowerPCCPU
*cpu
, target_ulong rb
,
326 CPUPPCState
*env
= &cpu
->env
;
329 if (!msr_is_64bit(env
, env
->msr
)) {
332 slb
= slb_lookup(cpu
, rb
);
334 *rt
= (target_ulong
)-1ul;
341 void helper_SLBMTE(CPUPPCState
*env
, target_ulong rb
, target_ulong rs
)
343 PowerPCCPU
*cpu
= env_archcpu(env
);
345 if (ppc_store_slb(cpu
, rb
& 0xfff, rb
& ~0xfffULL
, rs
) < 0) {
346 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
347 POWERPC_EXCP_INVAL
, GETPC());
351 target_ulong
helper_SLBMFEE(CPUPPCState
*env
, target_ulong rb
)
353 PowerPCCPU
*cpu
= env_archcpu(env
);
356 if (ppc_load_slb_esid(cpu
, rb
, &rt
) < 0) {
357 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
358 POWERPC_EXCP_INVAL
, GETPC());
363 target_ulong
helper_SLBFEE(CPUPPCState
*env
, target_ulong rb
)
365 PowerPCCPU
*cpu
= env_archcpu(env
);
368 if (ppc_find_slb_vsid(cpu
, rb
, &rt
) < 0) {
369 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
370 POWERPC_EXCP_INVAL
, GETPC());
375 target_ulong
helper_SLBMFEV(CPUPPCState
*env
, target_ulong rb
)
377 PowerPCCPU
*cpu
= env_archcpu(env
);
380 if (ppc_load_slb_vsid(cpu
, rb
, &rt
) < 0) {
381 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
382 POWERPC_EXCP_INVAL
, GETPC());
388 /* Check No-Execute or Guarded Storage */
389 static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU
*cpu
,
390 ppc_hash_pte64_t pte
)
392 /* Exec permissions CANNOT take away read or write permissions */
393 return (pte
.pte1
& HPTE64_R_N
) || (pte
.pte1
& HPTE64_R_G
) ?
394 PAGE_READ
| PAGE_WRITE
: PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
397 /* Check Basic Storage Protection */
398 static int ppc_hash64_pte_prot(int mmu_idx
,
399 ppc_slb_t
*slb
, ppc_hash_pte64_t pte
)
403 * Some pp bit combinations have undefined behaviour, so default
404 * to no access in those cases
408 key
= !!(mmuidx_pr(mmu_idx
) ? (slb
->vsid
& SLB_VSID_KP
)
409 : (slb
->vsid
& SLB_VSID_KS
));
410 pp
= (pte
.pte1
& HPTE64_R_PP
) | ((pte
.pte1
& HPTE64_R_PP0
) >> 61);
417 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
422 prot
= PAGE_READ
| PAGE_EXEC
;
433 prot
= PAGE_READ
| PAGE_EXEC
;
437 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
445 /* Check the instruction access permissions specified in the IAMR */
446 static int ppc_hash64_iamr_prot(PowerPCCPU
*cpu
, int key
)
448 CPUPPCState
*env
= &cpu
->env
;
449 int iamr_bits
= (env
->spr
[SPR_IAMR
] >> 2 * (31 - key
)) & 0x3;
452 * An instruction fetch is permitted if the IAMR bit is 0.
453 * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit
454 * can only take away EXEC permissions not READ or WRITE permissions.
455 * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since
456 * EXEC permissions are allowed.
458 return (iamr_bits
& 0x1) ? PAGE_READ
| PAGE_WRITE
:
459 PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
462 static int ppc_hash64_amr_prot(PowerPCCPU
*cpu
, ppc_hash_pte64_t pte
)
464 CPUPPCState
*env
= &cpu
->env
;
466 int prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
468 /* Only recent MMUs implement Virtual Page Class Key Protection */
469 if (!ppc_hash64_has(cpu
, PPC_HASH64_AMR
)) {
473 key
= HPTE64_R_KEY(pte
.pte1
);
474 amrbits
= (env
->spr
[SPR_AMR
] >> 2 * (31 - key
)) & 0x3;
476 /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
477 /* env->spr[SPR_AMR]); */
480 * A store is permitted if the AMR bit is 0. Remove write
481 * protection if it is set.
487 * A load is permitted if the AMR bit is 0. Remove read
488 * protection if it is set.
494 switch (env
->mmu_model
) {
496 * MMU version 2.07 and later support IAMR
497 * Check if the IAMR allows the instruction access - it will return
498 * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0
499 * if it does (and prot will be unchanged indicating execution support).
501 case POWERPC_MMU_2_07
:
502 case POWERPC_MMU_3_00
:
503 prot
&= ppc_hash64_iamr_prot(cpu
, key
);
512 static hwaddr
ppc_hash64_hpt_base(PowerPCCPU
*cpu
)
519 if (cpu
->env
.mmu_model
== POWERPC_MMU_3_00
) {
522 if (!ppc64_v3_get_pate(cpu
, cpu
->env
.spr
[SPR_LPIDR
], &pate
)) {
527 base
= cpu
->env
.spr
[SPR_SDR1
];
529 return base
& SDR_64_HTABORG
;
532 static hwaddr
ppc_hash64_hpt_mask(PowerPCCPU
*cpu
)
537 return cpu
->vhyp_class
->hpt_mask(cpu
->vhyp
);
539 if (cpu
->env
.mmu_model
== POWERPC_MMU_3_00
) {
542 if (!ppc64_v3_get_pate(cpu
, cpu
->env
.spr
[SPR_LPIDR
], &pate
)) {
547 base
= cpu
->env
.spr
[SPR_SDR1
];
549 return (1ULL << ((base
& SDR_64_HTABSIZE
) + 18 - 7)) - 1;
552 const ppc_hash_pte64_t
*ppc_hash64_map_hptes(PowerPCCPU
*cpu
,
555 hwaddr pte_offset
= ptex
* HASH_PTE_SIZE_64
;
557 hwaddr plen
= n
* HASH_PTE_SIZE_64
;
558 const ppc_hash_pte64_t
*hptes
;
561 return cpu
->vhyp_class
->map_hptes(cpu
->vhyp
, ptex
, n
);
563 base
= ppc_hash64_hpt_base(cpu
);
569 hptes
= address_space_map(CPU(cpu
)->as
, base
+ pte_offset
, &plen
, false,
570 MEMTXATTRS_UNSPECIFIED
);
571 if (plen
< (n
* HASH_PTE_SIZE_64
)) {
572 hw_error("%s: Unable to map all requested HPTEs\n", __func__
);
577 void ppc_hash64_unmap_hptes(PowerPCCPU
*cpu
, const ppc_hash_pte64_t
*hptes
,
581 cpu
->vhyp_class
->unmap_hptes(cpu
->vhyp
, hptes
, ptex
, n
);
585 address_space_unmap(CPU(cpu
)->as
, (void *)hptes
, n
* HASH_PTE_SIZE_64
,
586 false, n
* HASH_PTE_SIZE_64
);
589 bool ppc_hash64_valid_ptex(PowerPCCPU
*cpu
, target_ulong ptex
)
591 /* hash value/pteg group index is normalized by HPT mask */
592 if (((ptex
& ~7ULL) / HPTES_PER_GROUP
) & ~ppc_hash64_hpt_mask(cpu
)) {
598 static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes
*sps
,
599 uint64_t pte0
, uint64_t pte1
)
603 if (!(pte0
& HPTE64_V_LARGE
)) {
604 if (sps
->page_shift
!= 12) {
605 /* 4kiB page in a non 4kiB segment */
608 /* Normal 4kiB page */
612 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
613 const PPCHash64PageSize
*ps
= &sps
->enc
[i
];
616 if (!ps
->page_shift
) {
620 if (ps
->page_shift
== 12) {
621 /* L bit is set so this can't be a 4kiB page */
625 mask
= ((1ULL << ps
->page_shift
) - 1) & HPTE64_R_RPN
;
627 if ((pte1
& mask
) == ((uint64_t)ps
->pte_enc
<< HPTE64_R_RPN_SHIFT
)) {
628 return ps
->page_shift
;
632 return 0; /* Bad page size encoding */
635 static void ppc64_v3_new_to_old_hpte(target_ulong
*pte0
, target_ulong
*pte1
)
637 /* Insert B into pte0 */
638 *pte0
= (*pte0
& HPTE64_V_COMMON_BITS
) |
639 ((*pte1
& HPTE64_R_3_0_SSIZE_MASK
) <<
640 (HPTE64_V_SSIZE_SHIFT
- HPTE64_R_3_0_SSIZE_SHIFT
));
642 /* Remove B from pte1 */
643 *pte1
= *pte1
& ~HPTE64_R_3_0_SSIZE_MASK
;
647 static hwaddr
ppc_hash64_pteg_search(PowerPCCPU
*cpu
, hwaddr hash
,
648 const PPCHash64SegmentPageSizes
*sps
,
650 ppc_hash_pte64_t
*pte
, unsigned *pshift
)
653 const ppc_hash_pte64_t
*pteg
;
654 target_ulong pte0
, pte1
;
657 ptex
= (hash
& ppc_hash64_hpt_mask(cpu
)) * HPTES_PER_GROUP
;
658 pteg
= ppc_hash64_map_hptes(cpu
, ptex
, HPTES_PER_GROUP
);
662 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
663 pte0
= ppc_hash64_hpte0(cpu
, pteg
, i
);
665 * pte0 contains the valid bit and must be read before pte1,
666 * otherwise we might see an old pte1 with a new valid bit and
667 * thus an inconsistent hpte value
670 pte1
= ppc_hash64_hpte1(cpu
, pteg
, i
);
672 /* Convert format if necessary */
673 if (cpu
->env
.mmu_model
== POWERPC_MMU_3_00
&& !cpu
->vhyp
) {
674 ppc64_v3_new_to_old_hpte(&pte0
, &pte1
);
677 /* This compares V, B, H (secondary) and the AVPN */
678 if (HPTE64_V_COMPARE(pte0
, ptem
)) {
679 *pshift
= hpte_page_shift(sps
, pte0
, pte1
);
681 * If there is no match, ignore the PTE, it could simply
682 * be for a different segment size encoding and the
683 * architecture specifies we should not match. Linux will
684 * potentially leave behind PTEs for the wrong base page
685 * size when demoting segments.
691 * We don't do anything with pshift yet as qemu TLB only
692 * deals with 4K pages anyway
696 ppc_hash64_unmap_hptes(cpu
, pteg
, ptex
, HPTES_PER_GROUP
);
700 ppc_hash64_unmap_hptes(cpu
, pteg
, ptex
, HPTES_PER_GROUP
);
702 * We didn't find a valid entry.
707 static hwaddr
ppc_hash64_htab_lookup(PowerPCCPU
*cpu
,
708 ppc_slb_t
*slb
, target_ulong eaddr
,
709 ppc_hash_pte64_t
*pte
, unsigned *pshift
)
711 CPUPPCState
*env
= &cpu
->env
;
713 uint64_t vsid
, epnmask
, epn
, ptem
;
714 const PPCHash64SegmentPageSizes
*sps
= slb
->sps
;
717 * The SLB store path should prevent any bad page size encodings
718 * getting in there, so:
722 /* If ISL is set in LPCR we need to clamp the page size to 4K */
723 if (env
->spr
[SPR_LPCR
] & LPCR_ISL
) {
724 /* We assume that when using TCG, 4k is first entry of SPS */
725 sps
= &cpu
->hash64_opts
->sps
[0];
726 assert(sps
->page_shift
== 12);
729 epnmask
= ~((1ULL << sps
->page_shift
) - 1);
731 if (slb
->vsid
& SLB_VSID_B
) {
733 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT_1T
;
734 epn
= (eaddr
& ~SEGMENT_MASK_1T
) & epnmask
;
735 hash
= vsid
^ (vsid
<< 25) ^ (epn
>> sps
->page_shift
);
738 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT
;
739 epn
= (eaddr
& ~SEGMENT_MASK_256M
) & epnmask
;
740 hash
= vsid
^ (epn
>> sps
->page_shift
);
742 ptem
= (slb
->vsid
& SLB_VSID_PTEM
) | ((epn
>> 16) & HPTE64_V_AVPN
);
743 ptem
|= HPTE64_V_VALID
;
745 /* Page address translation */
746 qemu_log_mask(CPU_LOG_MMU
,
747 "htab_base " HWADDR_FMT_plx
" htab_mask " HWADDR_FMT_plx
748 " hash " HWADDR_FMT_plx
"\n",
749 ppc_hash64_hpt_base(cpu
), ppc_hash64_hpt_mask(cpu
), hash
);
751 /* Primary PTEG lookup */
752 qemu_log_mask(CPU_LOG_MMU
,
753 "0 htab=" HWADDR_FMT_plx
"/" HWADDR_FMT_plx
754 " vsid=" TARGET_FMT_lx
" ptem=" TARGET_FMT_lx
755 " hash=" HWADDR_FMT_plx
"\n",
756 ppc_hash64_hpt_base(cpu
), ppc_hash64_hpt_mask(cpu
),
758 ptex
= ppc_hash64_pteg_search(cpu
, hash
, sps
, ptem
, pte
, pshift
);
761 /* Secondary PTEG lookup */
762 ptem
|= HPTE64_V_SECONDARY
;
763 qemu_log_mask(CPU_LOG_MMU
,
764 "1 htab=" HWADDR_FMT_plx
"/" HWADDR_FMT_plx
765 " vsid=" TARGET_FMT_lx
" api=" TARGET_FMT_lx
766 " hash=" HWADDR_FMT_plx
"\n", ppc_hash64_hpt_base(cpu
),
767 ppc_hash64_hpt_mask(cpu
), vsid
, ptem
, ~hash
);
769 ptex
= ppc_hash64_pteg_search(cpu
, ~hash
, sps
, ptem
, pte
, pshift
);
775 unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU
*cpu
,
776 uint64_t pte0
, uint64_t pte1
)
780 if (!(pte0
& HPTE64_V_LARGE
)) {
785 * The encodings in env->sps need to be carefully chosen so that
786 * this gives an unambiguous result.
788 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
789 const PPCHash64SegmentPageSizes
*sps
= &cpu
->hash64_opts
->sps
[i
];
792 if (!sps
->page_shift
) {
796 shift
= hpte_page_shift(sps
, pte0
, pte1
);
805 static bool ppc_hash64_use_vrma(CPUPPCState
*env
)
807 switch (env
->mmu_model
) {
808 case POWERPC_MMU_3_00
:
810 * ISAv3.0 (POWER9) always uses VRMA, the VPM0 field and RMOR
811 * register no longer exist
816 return !!(env
->spr
[SPR_LPCR
] & LPCR_VPM0
);
820 static void ppc_hash64_set_isi(CPUState
*cs
, int mmu_idx
, uint64_t slb_vsid
,
823 CPUPPCState
*env
= &POWERPC_CPU(cs
)->env
;
826 if (!mmuidx_real(mmu_idx
)) {
827 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM1
);
829 vpm
= ppc_hash64_use_vrma(env
);
831 if (vpm
&& !mmuidx_hv(mmu_idx
)) {
832 cs
->exception_index
= POWERPC_EXCP_HISI
;
833 env
->spr
[SPR_ASDR
] = slb_vsid
;
835 cs
->exception_index
= POWERPC_EXCP_ISI
;
837 env
->error_code
= error_code
;
840 static void ppc_hash64_set_dsi(CPUState
*cs
, int mmu_idx
, uint64_t slb_vsid
,
841 uint64_t dar
, uint64_t dsisr
)
843 CPUPPCState
*env
= &POWERPC_CPU(cs
)->env
;
846 if (!mmuidx_real(mmu_idx
)) {
847 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM1
);
849 vpm
= ppc_hash64_use_vrma(env
);
851 if (vpm
&& !mmuidx_hv(mmu_idx
)) {
852 cs
->exception_index
= POWERPC_EXCP_HDSI
;
853 env
->spr
[SPR_HDAR
] = dar
;
854 env
->spr
[SPR_HDSISR
] = dsisr
;
855 env
->spr
[SPR_ASDR
] = slb_vsid
;
857 cs
->exception_index
= POWERPC_EXCP_DSI
;
858 env
->spr
[SPR_DAR
] = dar
;
859 env
->spr
[SPR_DSISR
] = dsisr
;
865 static void ppc_hash64_set_r(PowerPCCPU
*cpu
, hwaddr ptex
, uint64_t pte1
)
867 hwaddr base
, offset
= ptex
* HASH_PTE_SIZE_64
+ HPTE64_DW1_R
;
870 cpu
->vhyp_class
->hpte_set_r(cpu
->vhyp
, ptex
, pte1
);
873 base
= ppc_hash64_hpt_base(cpu
);
876 /* The HW performs a non-atomic byte update */
877 stb_phys(CPU(cpu
)->as
, base
+ offset
, ((pte1
>> 8) & 0xff) | 0x01);
880 static void ppc_hash64_set_c(PowerPCCPU
*cpu
, hwaddr ptex
, uint64_t pte1
)
882 hwaddr base
, offset
= ptex
* HASH_PTE_SIZE_64
+ HPTE64_DW1_C
;
885 cpu
->vhyp_class
->hpte_set_c(cpu
->vhyp
, ptex
, pte1
);
888 base
= ppc_hash64_hpt_base(cpu
);
890 /* The HW performs a non-atomic byte update */
891 stb_phys(CPU(cpu
)->as
, base
+ offset
, (pte1
& 0xff) | 0x80);
894 static target_ulong
rmls_limit(PowerPCCPU
*cpu
)
896 CPUPPCState
*env
= &cpu
->env
;
898 * In theory the meanings of RMLS values are implementation
899 * dependent. In practice, this seems to have been the set from
900 * POWER4+..POWER8, and RMLS is no longer supported in POWER9.
902 * Unsupported values mean the OS has shot itself in the
903 * foot. Return a 0-sized RMA in this case, which we expect
904 * to trigger an immediate DSI or ISI
906 static const target_ulong rma_sizes
[16] = {
915 target_ulong rmls
= (env
->spr
[SPR_LPCR
] & LPCR_RMLS
) >> LPCR_RMLS_SHIFT
;
917 return rma_sizes
[rmls
];
920 /* Return the LLP in SLB_VSID format */
921 static uint64_t get_vrma_llp(PowerPCCPU
*cpu
)
923 CPUPPCState
*env
= &cpu
->env
;
926 if (env
->mmu_model
== POWERPC_MMU_3_00
) {
931 * ISA v3.0 removes the LPCR[VRMASD] field and puts the VRMA base
932 * page size (L||LP equivalent) in the PS field in the HPT partition
935 if (!ppc64_v3_get_pate(cpu
, cpu
->env
.spr
[SPR_LPIDR
], &pate
)) {
936 error_report("Bad VRMA with no partition table entry");
939 ps
= PATE0_GET_PS(pate
.dw0
);
940 /* PS has L||LP in 3 consecutive bits, put them into SLB LLP format */
943 llp
= (l
<< SLB_VSID_L_SHIFT
) | (lp
<< SLB_VSID_LP_SHIFT
);
946 uint64_t lpcr
= env
->spr
[SPR_LPCR
];
947 target_ulong vrmasd
= (lpcr
& LPCR_VRMASD
) >> LPCR_VRMASD_SHIFT
;
949 /* VRMASD LLP matches SLB format, just shift and mask it */
950 llp
= (vrmasd
<< SLB_VSID_LP_SHIFT
) & SLB_VSID_LLP_MASK
;
956 static int build_vrma_slbe(PowerPCCPU
*cpu
, ppc_slb_t
*slb
)
958 uint64_t llp
= get_vrma_llp(cpu
);
959 target_ulong vsid
= SLB_VSID_VRMA
| llp
;
962 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
963 const PPCHash64SegmentPageSizes
*sps
= &cpu
->hash64_opts
->sps
[i
];
965 if (!sps
->page_shift
) {
969 if ((vsid
& SLB_VSID_LLP_MASK
) == sps
->slb_enc
) {
970 slb
->esid
= SLB_ESID_V
;
977 error_report("Bad VRMA page size encoding 0x" TARGET_FMT_lx
, llp
);
982 bool ppc_hash64_xlate(PowerPCCPU
*cpu
, vaddr eaddr
, MMUAccessType access_type
,
983 hwaddr
*raddrp
, int *psizep
, int *protp
, int mmu_idx
,
986 CPUState
*cs
= CPU(cpu
);
987 CPUPPCState
*env
= &cpu
->env
;
992 ppc_hash_pte64_t pte
;
993 int exec_prot
, pp_prot
, amr_prot
, prot
;
998 * Note on LPCR usage: 970 uses HID4, but our special variant of
999 * store_spr copies relevant fields into env->spr[SPR_LPCR].
1000 * Similarly we filter unimplemented bits when storing into LPCR
1001 * depending on the MMU version. This code can thus just use the
1005 /* 1. Handle real mode accesses */
1006 if (mmuidx_real(mmu_idx
)) {
1008 * Translation is supposedly "off", but in real mode the top 4
1009 * effective address bits are (mostly) ignored
1011 raddr
= eaddr
& 0x0FFFFFFFFFFFFFFFULL
;
1015 * In virtual hypervisor mode, there's nothing to do:
1016 * EA == GPA == qemu guest address
1018 } else if (mmuidx_hv(mmu_idx
) || !env
->has_hv_mode
) {
1019 /* In HV mode, add HRMOR if top EA bit is clear */
1020 if (!(eaddr
>> 63)) {
1021 raddr
|= env
->spr
[SPR_HRMOR
];
1023 } else if (ppc_hash64_use_vrma(env
)) {
1024 /* Emulated VRMA mode */
1026 if (build_vrma_slbe(cpu
, slb
) != 0) {
1027 /* Invalid VRMA setup, machine check */
1028 if (guest_visible
) {
1029 cs
->exception_index
= POWERPC_EXCP_MCHECK
;
1030 env
->error_code
= 0;
1035 goto skip_slb_search
;
1037 target_ulong limit
= rmls_limit(cpu
);
1039 /* Emulated old-style RMO mode, bounds check against RMLS */
1040 if (raddr
>= limit
) {
1041 if (!guest_visible
) {
1044 switch (access_type
) {
1045 case MMU_INST_FETCH
:
1046 ppc_hash64_set_isi(cs
, mmu_idx
, 0, SRR1_PROTFAULT
);
1049 ppc_hash64_set_dsi(cs
, mmu_idx
, 0, eaddr
, DSISR_PROTFAULT
);
1051 case MMU_DATA_STORE
:
1052 ppc_hash64_set_dsi(cs
, mmu_idx
, 0, eaddr
,
1053 DSISR_PROTFAULT
| DSISR_ISSTORE
);
1056 g_assert_not_reached();
1061 raddr
|= env
->spr
[SPR_RMOR
];
1065 *protp
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
1066 *psizep
= TARGET_PAGE_BITS
;
1070 /* 2. Translation is on, so look up the SLB */
1071 slb
= slb_lookup(cpu
, eaddr
);
1073 /* No entry found, check if in-memory segment tables are in use */
1074 if (ppc64_use_proc_tbl(cpu
)) {
1075 /* TODO - Unsupported */
1076 error_report("Segment Table Support Unimplemented");
1079 /* Segment still not found, generate the appropriate interrupt */
1080 if (!guest_visible
) {
1083 switch (access_type
) {
1084 case MMU_INST_FETCH
:
1085 cs
->exception_index
= POWERPC_EXCP_ISEG
;
1086 env
->error_code
= 0;
1089 case MMU_DATA_STORE
:
1090 cs
->exception_index
= POWERPC_EXCP_DSEG
;
1091 env
->error_code
= 0;
1092 env
->spr
[SPR_DAR
] = eaddr
;
1095 g_assert_not_reached();
1102 /* 3. Check for segment level no-execute violation */
1103 if (access_type
== MMU_INST_FETCH
&& (slb
->vsid
& SLB_VSID_N
)) {
1104 if (guest_visible
) {
1105 ppc_hash64_set_isi(cs
, mmu_idx
, slb
->vsid
, SRR1_NOEXEC_GUARD
);
1110 /* 4. Locate the PTE in the hash table */
1111 ptex
= ppc_hash64_htab_lookup(cpu
, slb
, eaddr
, &pte
, &apshift
);
1113 if (!guest_visible
) {
1116 switch (access_type
) {
1117 case MMU_INST_FETCH
:
1118 ppc_hash64_set_isi(cs
, mmu_idx
, slb
->vsid
, SRR1_NOPTE
);
1121 ppc_hash64_set_dsi(cs
, mmu_idx
, slb
->vsid
, eaddr
, DSISR_NOPTE
);
1123 case MMU_DATA_STORE
:
1124 ppc_hash64_set_dsi(cs
, mmu_idx
, slb
->vsid
, eaddr
,
1125 DSISR_NOPTE
| DSISR_ISSTORE
);
1128 g_assert_not_reached();
1132 qemu_log_mask(CPU_LOG_MMU
,
1133 "found PTE at index %08" HWADDR_PRIx
"\n", ptex
);
1135 /* 5. Check access permissions */
1137 exec_prot
= ppc_hash64_pte_noexec_guard(cpu
, pte
);
1138 pp_prot
= ppc_hash64_pte_prot(mmu_idx
, slb
, pte
);
1139 amr_prot
= ppc_hash64_amr_prot(cpu
, pte
);
1140 prot
= exec_prot
& pp_prot
& amr_prot
;
1142 need_prot
= check_prot_access_type(PAGE_RWX
, access_type
);
1143 if (need_prot
& ~prot
) {
1144 /* Access right violation */
1145 qemu_log_mask(CPU_LOG_MMU
, "PTE access rejected\n");
1146 if (!guest_visible
) {
1149 if (access_type
== MMU_INST_FETCH
) {
1151 if (PAGE_EXEC
& ~exec_prot
) {
1152 srr1
|= SRR1_NOEXEC_GUARD
; /* Access violates noexec or guard */
1153 } else if (PAGE_EXEC
& ~pp_prot
) {
1154 srr1
|= SRR1_PROTFAULT
; /* Access violates access authority */
1156 if (PAGE_EXEC
& ~amr_prot
) {
1157 srr1
|= SRR1_IAMR
; /* Access violates virt pg class key prot */
1159 ppc_hash64_set_isi(cs
, mmu_idx
, slb
->vsid
, srr1
);
1162 if (need_prot
& ~pp_prot
) {
1163 dsisr
|= DSISR_PROTFAULT
;
1165 if (access_type
== MMU_DATA_STORE
) {
1166 dsisr
|= DSISR_ISSTORE
;
1168 if (need_prot
& ~amr_prot
) {
1171 ppc_hash64_set_dsi(cs
, mmu_idx
, slb
->vsid
, eaddr
, dsisr
);
1176 qemu_log_mask(CPU_LOG_MMU
, "PTE access granted !\n");
1178 /* 6. Update PTE referenced and changed bits if necessary */
1180 if (!(pte
.pte1
& HPTE64_R_R
)) {
1181 ppc_hash64_set_r(cpu
, ptex
, pte
.pte1
);
1183 if (!(pte
.pte1
& HPTE64_R_C
)) {
1184 if (access_type
== MMU_DATA_STORE
) {
1185 ppc_hash64_set_c(cpu
, ptex
, pte
.pte1
);
1188 * Treat the page as read-only for now, so that a later write
1189 * will pass through this function again to set the C bit
1191 prot
&= ~PAGE_WRITE
;
1195 /* 7. Determine the real address from the PTE */
1197 *raddrp
= deposit64(pte
.pte1
& HPTE64_R_RPN
, 0, apshift
, eaddr
);
1203 void ppc_hash64_tlb_flush_hpte(PowerPCCPU
*cpu
, target_ulong ptex
,
1204 target_ulong pte0
, target_ulong pte1
)
1207 * XXX: given the fact that there are too many segments to
1208 * invalidate, and we still don't have a tlb_flush_mask(env, n,
1209 * mask) in QEMU, we just invalidate all TLBs
1211 cpu
->env
.tlb_need_flush
= TLB_NEED_GLOBAL_FLUSH
| TLB_NEED_LOCAL_FLUSH
;
1215 void helper_store_lpcr(CPUPPCState
*env
, target_ulong val
)
1217 PowerPCCPU
*cpu
= env_archcpu(env
);
1219 ppc_store_lpcr(cpu
, val
);
1223 void ppc_hash64_init(PowerPCCPU
*cpu
)
1225 CPUPPCState
*env
= &cpu
->env
;
1226 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
1228 if (!pcc
->hash64_opts
) {
1229 assert(!mmu_is_64bit(env
->mmu_model
));
1233 cpu
->hash64_opts
= g_memdup2(pcc
->hash64_opts
, sizeof(*cpu
->hash64_opts
));
1236 void ppc_hash64_finalize(PowerPCCPU
*cpu
)
1238 g_free(cpu
->hash64_opts
);
1241 const PPCHash64Options ppc_hash64_opts_basic
= {
1245 { .page_shift
= 12, /* 4K */
1247 .enc
= { { .page_shift
= 12, .pte_enc
= 0 } }
1249 { .page_shift
= 24, /* 16M */
1251 .enc
= { { .page_shift
= 24, .pte_enc
= 0 } }
1256 const PPCHash64Options ppc_hash64_opts_POWER7
= {
1257 .flags
= PPC_HASH64_1TSEG
| PPC_HASH64_AMR
| PPC_HASH64_CI_LARGEPAGE
,
1261 .page_shift
= 12, /* 4K */
1263 .enc
= { { .page_shift
= 12, .pte_enc
= 0 },
1264 { .page_shift
= 16, .pte_enc
= 0x7 },
1265 { .page_shift
= 24, .pte_enc
= 0x38 }, },
1268 .page_shift
= 16, /* 64K */
1269 .slb_enc
= SLB_VSID_64K
,
1270 .enc
= { { .page_shift
= 16, .pte_enc
= 0x1 },
1271 { .page_shift
= 24, .pte_enc
= 0x8 }, },
1274 .page_shift
= 24, /* 16M */
1275 .slb_enc
= SLB_VSID_16M
,
1276 .enc
= { { .page_shift
= 24, .pte_enc
= 0 }, },
1279 .page_shift
= 34, /* 16G */
1280 .slb_enc
= SLB_VSID_16G
,
1281 .enc
= { { .page_shift
= 34, .pte_enc
= 0x3 }, },