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 "qapi/error.h"
23 #include "exec/exec-all.h"
24 #include "exec/helper-proto.h"
25 #include "qemu/error-report.h"
26 #include "sysemu/hw_accel.h"
28 #include "mmu-hash64.h"
31 #include "mmu-book3s-v3.h"
36 # define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
38 # define LOG_SLB(...) do { } while (0)
45 static ppc_slb_t
*slb_lookup(PowerPCCPU
*cpu
, target_ulong eaddr
)
47 CPUPPCState
*env
= &cpu
->env
;
48 uint64_t esid_256M
, esid_1T
;
51 LOG_SLB("%s: eaddr " TARGET_FMT_lx
"\n", __func__
, eaddr
);
53 esid_256M
= (eaddr
& SEGMENT_MASK_256M
) | SLB_ESID_V
;
54 esid_1T
= (eaddr
& SEGMENT_MASK_1T
) | SLB_ESID_V
;
56 for (n
= 0; n
< env
->slb_nr
; n
++) {
57 ppc_slb_t
*slb
= &env
->slb
[n
];
59 LOG_SLB("%s: slot %d %016" PRIx64
" %016"
60 PRIx64
"\n", __func__
, n
, slb
->esid
, slb
->vsid
);
61 /* We check for 1T matches on all MMUs here - if the MMU
62 * doesn't have 1T segment support, we will have prevented 1T
63 * entries from being inserted in the slbmte code. */
64 if (((slb
->esid
== esid_256M
) &&
65 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_256M
))
66 || ((slb
->esid
== esid_1T
) &&
67 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_1T
))) {
75 void dump_slb(FILE *f
, fprintf_function cpu_fprintf
, PowerPCCPU
*cpu
)
77 CPUPPCState
*env
= &cpu
->env
;
81 cpu_synchronize_state(CPU(cpu
));
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
)
99 /* XXX: Warning: slbia never invalidates the first segment */
100 for (n
= 1; n
< env
->slb_nr
; n
++) {
101 ppc_slb_t
*slb
= &env
->slb
[n
];
103 if (slb
->esid
& SLB_ESID_V
) {
104 slb
->esid
&= ~SLB_ESID_V
;
105 /* XXX: given the fact that segment size is 256 MB or 1TB,
106 * and we still don't have a tlb_flush_mask(env, n, mask)
107 * in QEMU, we just invalidate all TLBs
109 env
->tlb_need_flush
|= TLB_NEED_LOCAL_FLUSH
;
114 static void __helper_slbie(CPUPPCState
*env
, target_ulong addr
,
117 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
120 slb
= slb_lookup(cpu
, addr
);
125 if (slb
->esid
& SLB_ESID_V
) {
126 slb
->esid
&= ~SLB_ESID_V
;
128 /* XXX: given the fact that segment size is 256 MB or 1TB,
129 * and we still don't have a tlb_flush_mask(env, n, mask)
130 * in QEMU, we just invalidate all TLBs
132 env
->tlb_need_flush
|=
133 (global
== false ? TLB_NEED_LOCAL_FLUSH
: TLB_NEED_GLOBAL_FLUSH
);
137 void helper_slbie(CPUPPCState
*env
, target_ulong addr
)
139 __helper_slbie(env
, addr
, false);
142 void helper_slbieg(CPUPPCState
*env
, target_ulong addr
)
144 __helper_slbie(env
, addr
, true);
147 int ppc_store_slb(PowerPCCPU
*cpu
, target_ulong slot
,
148 target_ulong esid
, target_ulong vsid
)
150 CPUPPCState
*env
= &cpu
->env
;
151 ppc_slb_t
*slb
= &env
->slb
[slot
];
152 const struct ppc_one_seg_page_size
*sps
= NULL
;
155 if (slot
>= env
->slb_nr
) {
156 return -1; /* Bad slot number */
158 if (esid
& ~(SLB_ESID_ESID
| SLB_ESID_V
)) {
159 return -1; /* Reserved bits set */
161 if (vsid
& (SLB_VSID_B
& ~SLB_VSID_B_1T
)) {
162 return -1; /* Bad segment size */
164 if ((vsid
& SLB_VSID_B
) && !(env
->mmu_model
& POWERPC_MMU_1TSEG
)) {
165 return -1; /* 1T segment on MMU that doesn't support it */
168 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
169 const struct ppc_one_seg_page_size
*sps1
= &env
->sps
.sps
[i
];
171 if (!sps1
->page_shift
) {
175 if ((vsid
& SLB_VSID_LLP_MASK
) == sps1
->slb_enc
) {
182 error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
183 " esid 0x"TARGET_FMT_lx
" vsid 0x"TARGET_FMT_lx
,
192 LOG_SLB("%s: " TARGET_FMT_lu
" " TARGET_FMT_lx
" - " TARGET_FMT_lx
193 " => %016" PRIx64
" %016" PRIx64
"\n", __func__
, slot
, esid
, vsid
,
194 slb
->esid
, slb
->vsid
);
199 static int ppc_load_slb_esid(PowerPCCPU
*cpu
, target_ulong rb
,
202 CPUPPCState
*env
= &cpu
->env
;
203 int slot
= rb
& 0xfff;
204 ppc_slb_t
*slb
= &env
->slb
[slot
];
206 if (slot
>= env
->slb_nr
) {
214 static int ppc_load_slb_vsid(PowerPCCPU
*cpu
, target_ulong rb
,
217 CPUPPCState
*env
= &cpu
->env
;
218 int slot
= rb
& 0xfff;
219 ppc_slb_t
*slb
= &env
->slb
[slot
];
221 if (slot
>= env
->slb_nr
) {
229 static int ppc_find_slb_vsid(PowerPCCPU
*cpu
, target_ulong rb
,
232 CPUPPCState
*env
= &cpu
->env
;
235 if (!msr_is_64bit(env
, env
->msr
)) {
238 slb
= slb_lookup(cpu
, rb
);
240 *rt
= (target_ulong
)-1ul;
247 void helper_store_slb(CPUPPCState
*env
, target_ulong rb
, target_ulong rs
)
249 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
251 if (ppc_store_slb(cpu
, rb
& 0xfff, rb
& ~0xfffULL
, rs
) < 0) {
252 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
253 POWERPC_EXCP_INVAL
, GETPC());
257 target_ulong
helper_load_slb_esid(CPUPPCState
*env
, target_ulong rb
)
259 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
262 if (ppc_load_slb_esid(cpu
, rb
, &rt
) < 0) {
263 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
264 POWERPC_EXCP_INVAL
, GETPC());
269 target_ulong
helper_find_slb_vsid(CPUPPCState
*env
, target_ulong rb
)
271 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
274 if (ppc_find_slb_vsid(cpu
, rb
, &rt
) < 0) {
275 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
276 POWERPC_EXCP_INVAL
, GETPC());
281 target_ulong
helper_load_slb_vsid(CPUPPCState
*env
, target_ulong rb
)
283 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
286 if (ppc_load_slb_vsid(cpu
, rb
, &rt
) < 0) {
287 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
288 POWERPC_EXCP_INVAL
, GETPC());
293 /* Check No-Execute or Guarded Storage */
294 static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU
*cpu
,
295 ppc_hash_pte64_t pte
)
297 /* Exec permissions CANNOT take away read or write permissions */
298 return (pte
.pte1
& HPTE64_R_N
) || (pte
.pte1
& HPTE64_R_G
) ?
299 PAGE_READ
| PAGE_WRITE
: PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
302 /* Check Basic Storage Protection */
303 static int ppc_hash64_pte_prot(PowerPCCPU
*cpu
,
304 ppc_slb_t
*slb
, ppc_hash_pte64_t pte
)
306 CPUPPCState
*env
= &cpu
->env
;
308 /* Some pp bit combinations have undefined behaviour, so default
309 * to no access in those cases */
312 key
= !!(msr_pr
? (slb
->vsid
& SLB_VSID_KP
)
313 : (slb
->vsid
& SLB_VSID_KS
));
314 pp
= (pte
.pte1
& HPTE64_R_PP
) | ((pte
.pte1
& HPTE64_R_PP0
) >> 61);
321 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
326 prot
= PAGE_READ
| PAGE_EXEC
;
337 prot
= PAGE_READ
| PAGE_EXEC
;
341 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
349 /* Check the instruction access permissions specified in the IAMR */
350 static int ppc_hash64_iamr_prot(PowerPCCPU
*cpu
, int key
)
352 CPUPPCState
*env
= &cpu
->env
;
353 int iamr_bits
= (env
->spr
[SPR_IAMR
] >> 2 * (31 - key
)) & 0x3;
356 * An instruction fetch is permitted if the IAMR bit is 0.
357 * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit
358 * can only take away EXEC permissions not READ or WRITE permissions.
359 * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since
360 * EXEC permissions are allowed.
362 return (iamr_bits
& 0x1) ? PAGE_READ
| PAGE_WRITE
:
363 PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
366 static int ppc_hash64_amr_prot(PowerPCCPU
*cpu
, ppc_hash_pte64_t pte
)
368 CPUPPCState
*env
= &cpu
->env
;
370 int prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
372 /* Only recent MMUs implement Virtual Page Class Key Protection */
373 if (!(env
->mmu_model
& POWERPC_MMU_AMR
)) {
377 key
= HPTE64_R_KEY(pte
.pte1
);
378 amrbits
= (env
->spr
[SPR_AMR
] >> 2*(31 - key
)) & 0x3;
380 /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
381 /* env->spr[SPR_AMR]); */
384 * A store is permitted if the AMR bit is 0. Remove write
385 * protection if it is set.
391 * A load is permitted if the AMR bit is 0. Remove read
392 * protection if it is set.
398 switch (env
->mmu_model
) {
400 * MMU version 2.07 and later support IAMR
401 * Check if the IAMR allows the instruction access - it will return
402 * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0
403 * if it does (and prot will be unchanged indicating execution support).
405 case POWERPC_MMU_2_07
:
406 case POWERPC_MMU_3_00
:
407 prot
&= ppc_hash64_iamr_prot(cpu
, key
);
416 const ppc_hash_pte64_t
*ppc_hash64_map_hptes(PowerPCCPU
*cpu
,
419 hwaddr pte_offset
= ptex
* HASH_PTE_SIZE_64
;
420 hwaddr base
= ppc_hash64_hpt_base(cpu
);
421 hwaddr plen
= n
* HASH_PTE_SIZE_64
;
422 const ppc_hash_pte64_t
*hptes
;
425 PPCVirtualHypervisorClass
*vhc
=
426 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
427 return vhc
->map_hptes(cpu
->vhyp
, ptex
, n
);
434 hptes
= address_space_map(CPU(cpu
)->as
, base
+ pte_offset
, &plen
, false);
435 if (plen
< (n
* HASH_PTE_SIZE_64
)) {
436 hw_error("%s: Unable to map all requested HPTEs\n", __func__
);
441 void ppc_hash64_unmap_hptes(PowerPCCPU
*cpu
, const ppc_hash_pte64_t
*hptes
,
445 PPCVirtualHypervisorClass
*vhc
=
446 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
447 vhc
->unmap_hptes(cpu
->vhyp
, hptes
, ptex
, n
);
451 address_space_unmap(CPU(cpu
)->as
, (void *)hptes
, n
* HASH_PTE_SIZE_64
,
452 false, n
* HASH_PTE_SIZE_64
);
455 static unsigned hpte_page_shift(const struct ppc_one_seg_page_size
*sps
,
456 uint64_t pte0
, uint64_t pte1
)
460 if (!(pte0
& HPTE64_V_LARGE
)) {
461 if (sps
->page_shift
!= 12) {
462 /* 4kiB page in a non 4kiB segment */
465 /* Normal 4kiB page */
469 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
470 const struct ppc_one_page_size
*ps
= &sps
->enc
[i
];
473 if (!ps
->page_shift
) {
477 if (ps
->page_shift
== 12) {
478 /* L bit is set so this can't be a 4kiB page */
482 mask
= ((1ULL << ps
->page_shift
) - 1) & HPTE64_R_RPN
;
484 if ((pte1
& mask
) == ((uint64_t)ps
->pte_enc
<< HPTE64_R_RPN_SHIFT
)) {
485 return ps
->page_shift
;
489 return 0; /* Bad page size encoding */
492 static hwaddr
ppc_hash64_pteg_search(PowerPCCPU
*cpu
, hwaddr hash
,
493 const struct ppc_one_seg_page_size
*sps
,
495 ppc_hash_pte64_t
*pte
, unsigned *pshift
)
498 const ppc_hash_pte64_t
*pteg
;
499 target_ulong pte0
, pte1
;
502 ptex
= (hash
& ppc_hash64_hpt_mask(cpu
)) * HPTES_PER_GROUP
;
503 pteg
= ppc_hash64_map_hptes(cpu
, ptex
, HPTES_PER_GROUP
);
507 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
508 pte0
= ppc_hash64_hpte0(cpu
, pteg
, i
);
509 pte1
= ppc_hash64_hpte1(cpu
, pteg
, i
);
511 /* This compares V, B, H (secondary) and the AVPN */
512 if (HPTE64_V_COMPARE(pte0
, ptem
)) {
513 *pshift
= hpte_page_shift(sps
, pte0
, pte1
);
515 * If there is no match, ignore the PTE, it could simply
516 * be for a different segment size encoding and the
517 * architecture specifies we should not match. Linux will
518 * potentially leave behind PTEs for the wrong base page
519 * size when demoting segments.
524 /* We don't do anything with pshift yet as qemu TLB only deals
525 * with 4K pages anyway
529 ppc_hash64_unmap_hptes(cpu
, pteg
, ptex
, HPTES_PER_GROUP
);
533 ppc_hash64_unmap_hptes(cpu
, pteg
, ptex
, HPTES_PER_GROUP
);
535 * We didn't find a valid entry.
540 static hwaddr
ppc_hash64_htab_lookup(PowerPCCPU
*cpu
,
541 ppc_slb_t
*slb
, target_ulong eaddr
,
542 ppc_hash_pte64_t
*pte
, unsigned *pshift
)
544 CPUPPCState
*env
= &cpu
->env
;
546 uint64_t vsid
, epnmask
, epn
, ptem
;
547 const struct ppc_one_seg_page_size
*sps
= slb
->sps
;
549 /* The SLB store path should prevent any bad page size encodings
550 * getting in there, so: */
553 /* If ISL is set in LPCR we need to clamp the page size to 4K */
554 if (env
->spr
[SPR_LPCR
] & LPCR_ISL
) {
555 /* We assume that when using TCG, 4k is first entry of SPS */
556 sps
= &env
->sps
.sps
[0];
557 assert(sps
->page_shift
== 12);
560 epnmask
= ~((1ULL << sps
->page_shift
) - 1);
562 if (slb
->vsid
& SLB_VSID_B
) {
564 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT_1T
;
565 epn
= (eaddr
& ~SEGMENT_MASK_1T
) & epnmask
;
566 hash
= vsid
^ (vsid
<< 25) ^ (epn
>> sps
->page_shift
);
569 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT
;
570 epn
= (eaddr
& ~SEGMENT_MASK_256M
) & epnmask
;
571 hash
= vsid
^ (epn
>> sps
->page_shift
);
573 ptem
= (slb
->vsid
& SLB_VSID_PTEM
) | ((epn
>> 16) & HPTE64_V_AVPN
);
574 ptem
|= HPTE64_V_VALID
;
576 /* Page address translation */
577 qemu_log_mask(CPU_LOG_MMU
,
578 "htab_base " TARGET_FMT_plx
" htab_mask " TARGET_FMT_plx
579 " hash " TARGET_FMT_plx
"\n",
580 ppc_hash64_hpt_base(cpu
), ppc_hash64_hpt_mask(cpu
), hash
);
582 /* Primary PTEG lookup */
583 qemu_log_mask(CPU_LOG_MMU
,
584 "0 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
585 " vsid=" TARGET_FMT_lx
" ptem=" TARGET_FMT_lx
586 " hash=" TARGET_FMT_plx
"\n",
587 ppc_hash64_hpt_base(cpu
), ppc_hash64_hpt_mask(cpu
),
589 ptex
= ppc_hash64_pteg_search(cpu
, hash
, sps
, ptem
, pte
, pshift
);
592 /* Secondary PTEG lookup */
593 ptem
|= HPTE64_V_SECONDARY
;
594 qemu_log_mask(CPU_LOG_MMU
,
595 "1 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
596 " vsid=" TARGET_FMT_lx
" api=" TARGET_FMT_lx
597 " hash=" TARGET_FMT_plx
"\n", ppc_hash64_hpt_base(cpu
),
598 ppc_hash64_hpt_mask(cpu
), vsid
, ptem
, ~hash
);
600 ptex
= ppc_hash64_pteg_search(cpu
, ~hash
, sps
, ptem
, pte
, pshift
);
606 unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU
*cpu
,
607 uint64_t pte0
, uint64_t pte1
)
609 CPUPPCState
*env
= &cpu
->env
;
612 if (!(pte0
& HPTE64_V_LARGE
)) {
617 * The encodings in env->sps need to be carefully chosen so that
618 * this gives an unambiguous result.
620 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
621 const struct ppc_one_seg_page_size
*sps
= &env
->sps
.sps
[i
];
624 if (!sps
->page_shift
) {
628 shift
= hpte_page_shift(sps
, pte0
, pte1
);
637 static void ppc_hash64_set_isi(CPUState
*cs
, CPUPPCState
*env
,
643 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM1
);
645 switch (env
->mmu_model
) {
646 case POWERPC_MMU_3_00
:
647 /* Field deprecated in ISAv3.00 - interrupts always go to hyperv */
651 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM0
);
655 if (vpm
&& !msr_hv
) {
656 cs
->exception_index
= POWERPC_EXCP_HISI
;
658 cs
->exception_index
= POWERPC_EXCP_ISI
;
660 env
->error_code
= error_code
;
663 static void ppc_hash64_set_dsi(CPUState
*cs
, CPUPPCState
*env
, uint64_t dar
,
669 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM1
);
671 switch (env
->mmu_model
) {
672 case POWERPC_MMU_3_00
:
673 /* Field deprecated in ISAv3.00 - interrupts always go to hyperv */
677 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM0
);
681 if (vpm
&& !msr_hv
) {
682 cs
->exception_index
= POWERPC_EXCP_HDSI
;
683 env
->spr
[SPR_HDAR
] = dar
;
684 env
->spr
[SPR_HDSISR
] = dsisr
;
686 cs
->exception_index
= POWERPC_EXCP_DSI
;
687 env
->spr
[SPR_DAR
] = dar
;
688 env
->spr
[SPR_DSISR
] = dsisr
;
694 int ppc_hash64_handle_mmu_fault(PowerPCCPU
*cpu
, vaddr eaddr
,
695 int rwx
, int mmu_idx
)
697 CPUState
*cs
= CPU(cpu
);
698 CPUPPCState
*env
= &cpu
->env
;
702 ppc_hash_pte64_t pte
;
703 int exec_prot
, pp_prot
, amr_prot
, prot
;
705 const int need_prot
[] = {PAGE_READ
, PAGE_WRITE
, PAGE_EXEC
};
708 assert((rwx
== 0) || (rwx
== 1) || (rwx
== 2));
710 /* Note on LPCR usage: 970 uses HID4, but our special variant
711 * of store_spr copies relevant fields into env->spr[SPR_LPCR].
712 * Similarily we filter unimplemented bits when storing into
713 * LPCR depending on the MMU version. This code can thus just
714 * use the LPCR "as-is".
717 /* 1. Handle real mode accesses */
718 if (((rwx
== 2) && (msr_ir
== 0)) || ((rwx
!= 2) && (msr_dr
== 0))) {
719 /* Translation is supposedly "off" */
720 /* In real mode the top 4 effective address bits are (mostly) ignored */
721 raddr
= eaddr
& 0x0FFFFFFFFFFFFFFFULL
;
723 /* In HV mode, add HRMOR if top EA bit is clear */
724 if (msr_hv
|| !env
->has_hv_mode
) {
725 if (!(eaddr
>> 63)) {
726 raddr
|= env
->spr
[SPR_HRMOR
];
729 /* Otherwise, check VPM for RMA vs VRMA */
730 if (env
->spr
[SPR_LPCR
] & LPCR_VPM0
) {
731 slb
= &env
->vrma_slb
;
733 goto skip_slb_search
;
735 /* Not much else to do here */
736 cs
->exception_index
= POWERPC_EXCP_MCHECK
;
739 } else if (raddr
< env
->rmls
) {
740 /* RMA. Check bounds in RMLS */
741 raddr
|= env
->spr
[SPR_RMOR
];
743 /* The access failed, generate the approriate interrupt */
745 ppc_hash64_set_isi(cs
, env
, SRR1_PROTFAULT
);
747 int dsisr
= DSISR_PROTFAULT
;
749 dsisr
|= DSISR_ISSTORE
;
751 ppc_hash64_set_dsi(cs
, env
, eaddr
, dsisr
);
756 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
, raddr
& TARGET_PAGE_MASK
,
757 PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
, mmu_idx
,
762 /* 2. Translation is on, so look up the SLB */
763 slb
= slb_lookup(cpu
, eaddr
);
765 /* No entry found, check if in-memory segment tables are in use */
766 if ((env
->mmu_model
& POWERPC_MMU_V3
) && ppc64_use_proc_tbl(cpu
)) {
767 /* TODO - Unsupported */
768 error_report("Segment Table Support Unimplemented");
771 /* Segment still not found, generate the appropriate interrupt */
773 cs
->exception_index
= POWERPC_EXCP_ISEG
;
776 cs
->exception_index
= POWERPC_EXCP_DSEG
;
778 env
->spr
[SPR_DAR
] = eaddr
;
785 /* 3. Check for segment level no-execute violation */
786 if ((rwx
== 2) && (slb
->vsid
& SLB_VSID_N
)) {
787 ppc_hash64_set_isi(cs
, env
, SRR1_NOEXEC_GUARD
);
791 /* 4. Locate the PTE in the hash table */
792 ptex
= ppc_hash64_htab_lookup(cpu
, slb
, eaddr
, &pte
, &apshift
);
795 ppc_hash64_set_isi(cs
, env
, SRR1_NOPTE
);
797 int dsisr
= DSISR_NOPTE
;
799 dsisr
|= DSISR_ISSTORE
;
801 ppc_hash64_set_dsi(cs
, env
, eaddr
, dsisr
);
805 qemu_log_mask(CPU_LOG_MMU
,
806 "found PTE at index %08" HWADDR_PRIx
"\n", ptex
);
808 /* 5. Check access permissions */
810 exec_prot
= ppc_hash64_pte_noexec_guard(cpu
, pte
);
811 pp_prot
= ppc_hash64_pte_prot(cpu
, slb
, pte
);
812 amr_prot
= ppc_hash64_amr_prot(cpu
, pte
);
813 prot
= exec_prot
& pp_prot
& amr_prot
;
815 if ((need_prot
[rwx
] & ~prot
) != 0) {
816 /* Access right violation */
817 qemu_log_mask(CPU_LOG_MMU
, "PTE access rejected\n");
820 if (PAGE_EXEC
& ~exec_prot
) {
821 srr1
|= SRR1_NOEXEC_GUARD
; /* Access violates noexec or guard */
822 } else if (PAGE_EXEC
& ~pp_prot
) {
823 srr1
|= SRR1_PROTFAULT
; /* Access violates access authority */
825 if (PAGE_EXEC
& ~amr_prot
) {
826 srr1
|= SRR1_IAMR
; /* Access violates virt pg class key prot */
828 ppc_hash64_set_isi(cs
, env
, srr1
);
831 if (need_prot
[rwx
] & ~pp_prot
) {
832 dsisr
|= DSISR_PROTFAULT
;
835 dsisr
|= DSISR_ISSTORE
;
837 if (need_prot
[rwx
] & ~amr_prot
) {
840 ppc_hash64_set_dsi(cs
, env
, eaddr
, dsisr
);
845 qemu_log_mask(CPU_LOG_MMU
, "PTE access granted !\n");
847 /* 6. Update PTE referenced and changed bits if necessary */
849 new_pte1
= pte
.pte1
| HPTE64_R_R
; /* set referenced bit */
851 new_pte1
|= HPTE64_R_C
; /* set changed (dirty) bit */
853 /* Treat the page as read-only for now, so that a later write
854 * will pass through this function again to set the C bit */
858 if (new_pte1
!= pte
.pte1
) {
859 ppc_hash64_store_hpte(cpu
, ptex
, pte
.pte0
, new_pte1
);
862 /* 7. Determine the real address from the PTE */
864 raddr
= deposit64(pte
.pte1
& HPTE64_R_RPN
, 0, apshift
, eaddr
);
866 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
, raddr
& TARGET_PAGE_MASK
,
867 prot
, mmu_idx
, 1ULL << apshift
);
872 hwaddr
ppc_hash64_get_phys_page_debug(PowerPCCPU
*cpu
, target_ulong addr
)
874 CPUPPCState
*env
= &cpu
->env
;
877 ppc_hash_pte64_t pte
;
880 /* Handle real mode */
882 /* In real mode the top 4 effective address bits are ignored */
883 raddr
= addr
& 0x0FFFFFFFFFFFFFFFULL
;
885 /* In HV mode, add HRMOR if top EA bit is clear */
886 if ((msr_hv
|| !env
->has_hv_mode
) && !(addr
>> 63)) {
887 return raddr
| env
->spr
[SPR_HRMOR
];
890 /* Otherwise, check VPM for RMA vs VRMA */
891 if (env
->spr
[SPR_LPCR
] & LPCR_VPM0
) {
892 slb
= &env
->vrma_slb
;
896 } else if (raddr
< env
->rmls
) {
897 /* RMA. Check bounds in RMLS */
898 return raddr
| env
->spr
[SPR_RMOR
];
903 slb
= slb_lookup(cpu
, addr
);
909 ptex
= ppc_hash64_htab_lookup(cpu
, slb
, addr
, &pte
, &apshift
);
914 return deposit64(pte
.pte1
& HPTE64_R_RPN
, 0, apshift
, addr
)
918 void ppc_hash64_store_hpte(PowerPCCPU
*cpu
, hwaddr ptex
,
919 uint64_t pte0
, uint64_t pte1
)
921 hwaddr base
= ppc_hash64_hpt_base(cpu
);
922 hwaddr offset
= ptex
* HASH_PTE_SIZE_64
;
925 PPCVirtualHypervisorClass
*vhc
=
926 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
927 vhc
->store_hpte(cpu
->vhyp
, ptex
, pte0
, pte1
);
931 stq_phys(CPU(cpu
)->as
, base
+ offset
, pte0
);
932 stq_phys(CPU(cpu
)->as
, base
+ offset
+ HASH_PTE_SIZE_64
/ 2, pte1
);
935 void ppc_hash64_tlb_flush_hpte(PowerPCCPU
*cpu
, target_ulong ptex
,
936 target_ulong pte0
, target_ulong pte1
)
939 * XXX: given the fact that there are too many segments to
940 * invalidate, and we still don't have a tlb_flush_mask(env, n,
941 * mask) in QEMU, we just invalidate all TLBs
943 cpu
->env
.tlb_need_flush
= TLB_NEED_GLOBAL_FLUSH
| TLB_NEED_LOCAL_FLUSH
;
946 void ppc_hash64_update_rmls(CPUPPCState
*env
)
948 uint64_t lpcr
= env
->spr
[SPR_LPCR
];
951 * This is the full 4 bits encoding of POWER8. Previous
952 * CPUs only support a subset of these but the filtering
953 * is done when writing LPCR
955 switch ((lpcr
& LPCR_RMLS
) >> LPCR_RMLS_SHIFT
) {
957 env
->rmls
= 0x2000000ull
;
960 env
->rmls
= 0x4000000ull
;
962 case 0x7: /* 128MB */
963 env
->rmls
= 0x8000000ull
;
965 case 0x4: /* 256MB */
966 env
->rmls
= 0x10000000ull
;
969 env
->rmls
= 0x40000000ull
;
972 env
->rmls
= 0x400000000ull
;
975 /* What to do here ??? */
980 void ppc_hash64_update_vrma(CPUPPCState
*env
)
982 const struct ppc_one_seg_page_size
*sps
= NULL
;
983 target_ulong esid
, vsid
, lpcr
;
984 ppc_slb_t
*slb
= &env
->vrma_slb
;
989 slb
->esid
= slb
->vsid
= 0;
992 /* Is VRMA enabled ? */
993 lpcr
= env
->spr
[SPR_LPCR
];
994 if (!(lpcr
& LPCR_VPM0
)) {
998 /* Make one up. Mostly ignore the ESID which will not be
999 * needed for translation
1001 vsid
= SLB_VSID_VRMA
;
1002 vrmasd
= (lpcr
& LPCR_VRMASD
) >> LPCR_VRMASD_SHIFT
;
1003 vsid
|= (vrmasd
<< 4) & (SLB_VSID_L
| SLB_VSID_LP
);
1006 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
1007 const struct ppc_one_seg_page_size
*sps1
= &env
->sps
.sps
[i
];
1009 if (!sps1
->page_shift
) {
1013 if ((vsid
& SLB_VSID_LLP_MASK
) == sps1
->slb_enc
) {
1020 error_report("Bad page size encoding esid 0x"TARGET_FMT_lx
1021 " vsid 0x"TARGET_FMT_lx
, esid
, vsid
);
1030 void helper_store_lpcr(CPUPPCState
*env
, target_ulong val
)
1034 /* Filter out bits */
1035 switch (POWERPC_MMU_VER(env
->mmu_model
)) {
1036 case POWERPC_MMU_VER_64B
: /* 970 */
1040 if (val
& 0x8000000000000000ull
) {
1044 lpcr
|= (0x4ull
<< LPCR_RMLS_SHIFT
);
1046 if (val
& 0x4000000000000000ull
) {
1047 lpcr
|= (0x2ull
<< LPCR_RMLS_SHIFT
);
1049 if (val
& 0x2000000000000000ull
) {
1050 lpcr
|= (0x1ull
<< LPCR_RMLS_SHIFT
);
1052 env
->spr
[SPR_RMOR
] = ((lpcr
>> 41) & 0xffffull
) << 26;
1054 /* XXX We could also write LPID from HID4 here
1055 * but since we don't tag any translation on it
1056 * it doesn't actually matter
1058 /* XXX For proper emulation of 970 we also need
1059 * to dig HRMOR out of HID5
1062 case POWERPC_MMU_VER_2_03
: /* P5p */
1063 lpcr
= val
& (LPCR_RMLS
| LPCR_ILE
|
1064 LPCR_LPES0
| LPCR_LPES1
|
1065 LPCR_RMI
| LPCR_HDICE
);
1067 case POWERPC_MMU_VER_2_06
: /* P7 */
1068 lpcr
= val
& (LPCR_VPM0
| LPCR_VPM1
| LPCR_ISL
| LPCR_DPFD
|
1069 LPCR_VRMASD
| LPCR_RMLS
| LPCR_ILE
|
1070 LPCR_P7_PECE0
| LPCR_P7_PECE1
| LPCR_P7_PECE2
|
1071 LPCR_MER
| LPCR_TC
|
1072 LPCR_LPES0
| LPCR_LPES1
| LPCR_HDICE
);
1074 case POWERPC_MMU_VER_2_07
: /* P8 */
1075 lpcr
= val
& (LPCR_VPM0
| LPCR_VPM1
| LPCR_ISL
| LPCR_KBV
|
1076 LPCR_DPFD
| LPCR_VRMASD
| LPCR_RMLS
| LPCR_ILE
|
1077 LPCR_AIL
| LPCR_ONL
| LPCR_P8_PECE0
| LPCR_P8_PECE1
|
1078 LPCR_P8_PECE2
| LPCR_P8_PECE3
| LPCR_P8_PECE4
|
1079 LPCR_MER
| LPCR_TC
| LPCR_LPES0
| LPCR_HDICE
);
1081 case POWERPC_MMU_VER_3_00
: /* P9 */
1082 lpcr
= val
& (LPCR_VPM1
| LPCR_ISL
| LPCR_KBV
| LPCR_DPFD
|
1083 (LPCR_PECE_U_MASK
& LPCR_HVEE
) | LPCR_ILE
| LPCR_AIL
|
1084 LPCR_UPRT
| LPCR_EVIRT
| LPCR_ONL
|
1085 (LPCR_PECE_L_MASK
& (LPCR_PDEE
| LPCR_HDEE
| LPCR_EEE
|
1086 LPCR_DEE
| LPCR_OEE
)) | LPCR_MER
| LPCR_GTSE
| LPCR_TC
|
1087 LPCR_HEIC
| LPCR_LPES0
| LPCR_HVICE
| LPCR_HDICE
);
1092 env
->spr
[SPR_LPCR
] = lpcr
;
1093 ppc_hash64_update_rmls(env
);
1094 ppc_hash64_update_vrma(env
);