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"
35 # define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
37 # define LOG_SLB(...) do { } while (0)
44 static ppc_slb_t
*slb_lookup(PowerPCCPU
*cpu
, target_ulong eaddr
)
46 CPUPPCState
*env
= &cpu
->env
;
47 uint64_t esid_256M
, esid_1T
;
50 LOG_SLB("%s: eaddr " TARGET_FMT_lx
"\n", __func__
, eaddr
);
52 esid_256M
= (eaddr
& SEGMENT_MASK_256M
) | SLB_ESID_V
;
53 esid_1T
= (eaddr
& SEGMENT_MASK_1T
) | SLB_ESID_V
;
55 for (n
= 0; n
< env
->slb_nr
; n
++) {
56 ppc_slb_t
*slb
= &env
->slb
[n
];
58 LOG_SLB("%s: slot %d %016" PRIx64
" %016"
59 PRIx64
"\n", __func__
, n
, slb
->esid
, slb
->vsid
);
60 /* We check for 1T matches on all MMUs here - if the MMU
61 * doesn't have 1T segment support, we will have prevented 1T
62 * entries from being inserted in the slbmte code. */
63 if (((slb
->esid
== esid_256M
) &&
64 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_256M
))
65 || ((slb
->esid
== esid_1T
) &&
66 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_1T
))) {
74 void dump_slb(FILE *f
, fprintf_function cpu_fprintf
, PowerPCCPU
*cpu
)
76 CPUPPCState
*env
= &cpu
->env
;
80 cpu_synchronize_state(CPU(cpu
));
82 cpu_fprintf(f
, "SLB\tESID\t\t\tVSID\n");
83 for (i
= 0; i
< env
->slb_nr
; i
++) {
84 slbe
= env
->slb
[i
].esid
;
85 slbv
= env
->slb
[i
].vsid
;
86 if (slbe
== 0 && slbv
== 0) {
89 cpu_fprintf(f
, "%d\t0x%016" PRIx64
"\t0x%016" PRIx64
"\n",
94 void helper_slbia(CPUPPCState
*env
)
98 /* XXX: Warning: slbia never invalidates the first segment */
99 for (n
= 1; n
< env
->slb_nr
; n
++) {
100 ppc_slb_t
*slb
= &env
->slb
[n
];
102 if (slb
->esid
& SLB_ESID_V
) {
103 slb
->esid
&= ~SLB_ESID_V
;
104 /* XXX: given the fact that segment size is 256 MB or 1TB,
105 * and we still don't have a tlb_flush_mask(env, n, mask)
106 * in QEMU, we just invalidate all TLBs
108 env
->tlb_need_flush
|= TLB_NEED_LOCAL_FLUSH
;
113 static void __helper_slbie(CPUPPCState
*env
, target_ulong addr
,
116 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
119 slb
= slb_lookup(cpu
, addr
);
124 if (slb
->esid
& SLB_ESID_V
) {
125 slb
->esid
&= ~SLB_ESID_V
;
127 /* XXX: given the fact that segment size is 256 MB or 1TB,
128 * and we still don't have a tlb_flush_mask(env, n, mask)
129 * in QEMU, we just invalidate all TLBs
131 env
->tlb_need_flush
|=
132 (global
== false ? TLB_NEED_LOCAL_FLUSH
: TLB_NEED_GLOBAL_FLUSH
);
136 void helper_slbie(CPUPPCState
*env
, target_ulong addr
)
138 __helper_slbie(env
, addr
, false);
141 void helper_slbieg(CPUPPCState
*env
, target_ulong addr
)
143 __helper_slbie(env
, addr
, true);
146 int ppc_store_slb(PowerPCCPU
*cpu
, target_ulong slot
,
147 target_ulong esid
, target_ulong vsid
)
149 CPUPPCState
*env
= &cpu
->env
;
150 ppc_slb_t
*slb
= &env
->slb
[slot
];
151 const struct ppc_one_seg_page_size
*sps
= NULL
;
154 if (slot
>= env
->slb_nr
) {
155 return -1; /* Bad slot number */
157 if (esid
& ~(SLB_ESID_ESID
| SLB_ESID_V
)) {
158 return -1; /* Reserved bits set */
160 if (vsid
& (SLB_VSID_B
& ~SLB_VSID_B_1T
)) {
161 return -1; /* Bad segment size */
163 if ((vsid
& SLB_VSID_B
) && !(env
->mmu_model
& POWERPC_MMU_1TSEG
)) {
164 return -1; /* 1T segment on MMU that doesn't support it */
167 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
168 const struct ppc_one_seg_page_size
*sps1
= &env
->sps
.sps
[i
];
170 if (!sps1
->page_shift
) {
174 if ((vsid
& SLB_VSID_LLP_MASK
) == sps1
->slb_enc
) {
181 error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
182 " esid 0x"TARGET_FMT_lx
" vsid 0x"TARGET_FMT_lx
,
191 LOG_SLB("%s: " TARGET_FMT_lu
" " TARGET_FMT_lx
" - " TARGET_FMT_lx
192 " => %016" PRIx64
" %016" PRIx64
"\n", __func__
, slot
, esid
, vsid
,
193 slb
->esid
, slb
->vsid
);
198 static int ppc_load_slb_esid(PowerPCCPU
*cpu
, target_ulong rb
,
201 CPUPPCState
*env
= &cpu
->env
;
202 int slot
= rb
& 0xfff;
203 ppc_slb_t
*slb
= &env
->slb
[slot
];
205 if (slot
>= env
->slb_nr
) {
213 static int ppc_load_slb_vsid(PowerPCCPU
*cpu
, target_ulong rb
,
216 CPUPPCState
*env
= &cpu
->env
;
217 int slot
= rb
& 0xfff;
218 ppc_slb_t
*slb
= &env
->slb
[slot
];
220 if (slot
>= env
->slb_nr
) {
228 static int ppc_find_slb_vsid(PowerPCCPU
*cpu
, target_ulong rb
,
231 CPUPPCState
*env
= &cpu
->env
;
234 if (!msr_is_64bit(env
, env
->msr
)) {
237 slb
= slb_lookup(cpu
, rb
);
239 *rt
= (target_ulong
)-1ul;
246 void helper_store_slb(CPUPPCState
*env
, target_ulong rb
, target_ulong rs
)
248 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
250 if (ppc_store_slb(cpu
, rb
& 0xfff, rb
& ~0xfffULL
, rs
) < 0) {
251 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
252 POWERPC_EXCP_INVAL
, GETPC());
256 target_ulong
helper_load_slb_esid(CPUPPCState
*env
, target_ulong rb
)
258 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
261 if (ppc_load_slb_esid(cpu
, rb
, &rt
) < 0) {
262 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
263 POWERPC_EXCP_INVAL
, GETPC());
268 target_ulong
helper_find_slb_vsid(CPUPPCState
*env
, target_ulong rb
)
270 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
273 if (ppc_find_slb_vsid(cpu
, rb
, &rt
) < 0) {
274 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
275 POWERPC_EXCP_INVAL
, GETPC());
280 target_ulong
helper_load_slb_vsid(CPUPPCState
*env
, target_ulong rb
)
282 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
285 if (ppc_load_slb_vsid(cpu
, rb
, &rt
) < 0) {
286 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
287 POWERPC_EXCP_INVAL
, GETPC());
292 static int ppc_hash64_pte_prot(PowerPCCPU
*cpu
,
293 ppc_slb_t
*slb
, ppc_hash_pte64_t pte
)
295 CPUPPCState
*env
= &cpu
->env
;
297 /* Some pp bit combinations have undefined behaviour, so default
298 * to no access in those cases */
301 key
= !!(msr_pr
? (slb
->vsid
& SLB_VSID_KP
)
302 : (slb
->vsid
& SLB_VSID_KS
));
303 pp
= (pte
.pte1
& HPTE64_R_PP
) | ((pte
.pte1
& HPTE64_R_PP0
) >> 61);
310 prot
= PAGE_READ
| PAGE_WRITE
;
331 prot
= PAGE_READ
| PAGE_WRITE
;
336 /* No execute if either noexec or guarded bits set */
337 if (!(pte
.pte1
& HPTE64_R_N
) || (pte
.pte1
& HPTE64_R_G
)
338 || (slb
->vsid
& SLB_VSID_N
)) {
345 static int ppc_hash64_amr_prot(PowerPCCPU
*cpu
, ppc_hash_pte64_t pte
)
347 CPUPPCState
*env
= &cpu
->env
;
349 int prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
351 /* Only recent MMUs implement Virtual Page Class Key Protection */
352 if (!(env
->mmu_model
& POWERPC_MMU_AMR
)) {
356 key
= HPTE64_R_KEY(pte
.pte1
);
357 amrbits
= (env
->spr
[SPR_AMR
] >> 2*(31 - key
)) & 0x3;
359 /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
360 /* env->spr[SPR_AMR]); */
363 * A store is permitted if the AMR bit is 0. Remove write
364 * protection if it is set.
370 * A load is permitted if the AMR bit is 0. Remove read
371 * protection if it is set.
380 const ppc_hash_pte64_t
*ppc_hash64_map_hptes(PowerPCCPU
*cpu
,
383 hwaddr pte_offset
= ptex
* HASH_PTE_SIZE_64
;
384 hwaddr base
= ppc_hash64_hpt_base(cpu
);
385 hwaddr plen
= n
* HASH_PTE_SIZE_64
;
386 const ppc_hash_pte64_t
*hptes
;
389 PPCVirtualHypervisorClass
*vhc
=
390 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
391 return vhc
->map_hptes(cpu
->vhyp
, ptex
, n
);
398 hptes
= address_space_map(CPU(cpu
)->as
, base
+ pte_offset
, &plen
, false);
399 if (plen
< (n
* HASH_PTE_SIZE_64
)) {
400 hw_error("%s: Unable to map all requested HPTEs\n", __func__
);
405 void ppc_hash64_unmap_hptes(PowerPCCPU
*cpu
, const ppc_hash_pte64_t
*hptes
,
409 PPCVirtualHypervisorClass
*vhc
=
410 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
411 vhc
->unmap_hptes(cpu
->vhyp
, hptes
, ptex
, n
);
415 address_space_unmap(CPU(cpu
)->as
, (void *)hptes
, n
* HASH_PTE_SIZE_64
,
416 false, n
* HASH_PTE_SIZE_64
);
419 static unsigned hpte_page_shift(const struct ppc_one_seg_page_size
*sps
,
420 uint64_t pte0
, uint64_t pte1
)
424 if (!(pte0
& HPTE64_V_LARGE
)) {
425 if (sps
->page_shift
!= 12) {
426 /* 4kiB page in a non 4kiB segment */
429 /* Normal 4kiB page */
433 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
434 const struct ppc_one_page_size
*ps
= &sps
->enc
[i
];
437 if (!ps
->page_shift
) {
441 if (ps
->page_shift
== 12) {
442 /* L bit is set so this can't be a 4kiB page */
446 mask
= ((1ULL << ps
->page_shift
) - 1) & HPTE64_R_RPN
;
448 if ((pte1
& mask
) == ((uint64_t)ps
->pte_enc
<< HPTE64_R_RPN_SHIFT
)) {
449 return ps
->page_shift
;
453 return 0; /* Bad page size encoding */
456 static hwaddr
ppc_hash64_pteg_search(PowerPCCPU
*cpu
, hwaddr hash
,
457 const struct ppc_one_seg_page_size
*sps
,
459 ppc_hash_pte64_t
*pte
, unsigned *pshift
)
462 const ppc_hash_pte64_t
*pteg
;
463 target_ulong pte0
, pte1
;
466 ptex
= (hash
& ppc_hash64_hpt_mask(cpu
)) * HPTES_PER_GROUP
;
467 pteg
= ppc_hash64_map_hptes(cpu
, ptex
, HPTES_PER_GROUP
);
471 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
472 pte0
= ppc_hash64_hpte0(cpu
, pteg
, i
);
473 pte1
= ppc_hash64_hpte1(cpu
, pteg
, i
);
475 /* This compares V, B, H (secondary) and the AVPN */
476 if (HPTE64_V_COMPARE(pte0
, ptem
)) {
477 *pshift
= hpte_page_shift(sps
, pte0
, pte1
);
479 * If there is no match, ignore the PTE, it could simply
480 * be for a different segment size encoding and the
481 * architecture specifies we should not match. Linux will
482 * potentially leave behind PTEs for the wrong base page
483 * size when demoting segments.
488 /* We don't do anything with pshift yet as qemu TLB only deals
489 * with 4K pages anyway
493 ppc_hash64_unmap_hptes(cpu
, pteg
, ptex
, HPTES_PER_GROUP
);
497 ppc_hash64_unmap_hptes(cpu
, pteg
, ptex
, HPTES_PER_GROUP
);
499 * We didn't find a valid entry.
504 static hwaddr
ppc_hash64_htab_lookup(PowerPCCPU
*cpu
,
505 ppc_slb_t
*slb
, target_ulong eaddr
,
506 ppc_hash_pte64_t
*pte
, unsigned *pshift
)
508 CPUPPCState
*env
= &cpu
->env
;
510 uint64_t vsid
, epnmask
, epn
, ptem
;
511 const struct ppc_one_seg_page_size
*sps
= slb
->sps
;
513 /* The SLB store path should prevent any bad page size encodings
514 * getting in there, so: */
517 /* If ISL is set in LPCR we need to clamp the page size to 4K */
518 if (env
->spr
[SPR_LPCR
] & LPCR_ISL
) {
519 /* We assume that when using TCG, 4k is first entry of SPS */
520 sps
= &env
->sps
.sps
[0];
521 assert(sps
->page_shift
== 12);
524 epnmask
= ~((1ULL << sps
->page_shift
) - 1);
526 if (slb
->vsid
& SLB_VSID_B
) {
528 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT_1T
;
529 epn
= (eaddr
& ~SEGMENT_MASK_1T
) & epnmask
;
530 hash
= vsid
^ (vsid
<< 25) ^ (epn
>> sps
->page_shift
);
533 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT
;
534 epn
= (eaddr
& ~SEGMENT_MASK_256M
) & epnmask
;
535 hash
= vsid
^ (epn
>> sps
->page_shift
);
537 ptem
= (slb
->vsid
& SLB_VSID_PTEM
) | ((epn
>> 16) & HPTE64_V_AVPN
);
538 ptem
|= HPTE64_V_VALID
;
540 /* Page address translation */
541 qemu_log_mask(CPU_LOG_MMU
,
542 "htab_base " TARGET_FMT_plx
" htab_mask " TARGET_FMT_plx
543 " hash " TARGET_FMT_plx
"\n",
544 ppc_hash64_hpt_base(cpu
), ppc_hash64_hpt_mask(cpu
), hash
);
546 /* Primary PTEG lookup */
547 qemu_log_mask(CPU_LOG_MMU
,
548 "0 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
549 " vsid=" TARGET_FMT_lx
" ptem=" TARGET_FMT_lx
550 " hash=" TARGET_FMT_plx
"\n",
551 ppc_hash64_hpt_base(cpu
), ppc_hash64_hpt_mask(cpu
),
553 ptex
= ppc_hash64_pteg_search(cpu
, hash
, sps
, ptem
, pte
, pshift
);
556 /* Secondary PTEG lookup */
557 ptem
|= HPTE64_V_SECONDARY
;
558 qemu_log_mask(CPU_LOG_MMU
,
559 "1 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
560 " vsid=" TARGET_FMT_lx
" api=" TARGET_FMT_lx
561 " hash=" TARGET_FMT_plx
"\n", ppc_hash64_hpt_base(cpu
),
562 ppc_hash64_hpt_mask(cpu
), vsid
, ptem
, ~hash
);
564 ptex
= ppc_hash64_pteg_search(cpu
, ~hash
, sps
, ptem
, pte
, pshift
);
570 unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU
*cpu
,
571 uint64_t pte0
, uint64_t pte1
)
573 CPUPPCState
*env
= &cpu
->env
;
576 if (!(pte0
& HPTE64_V_LARGE
)) {
581 * The encodings in env->sps need to be carefully chosen so that
582 * this gives an unambiguous result.
584 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
585 const struct ppc_one_seg_page_size
*sps
= &env
->sps
.sps
[i
];
588 if (!sps
->page_shift
) {
592 shift
= hpte_page_shift(sps
, pte0
, pte1
);
601 static void ppc_hash64_set_isi(CPUState
*cs
, CPUPPCState
*env
,
607 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM1
);
609 switch (env
->mmu_model
) {
610 case POWERPC_MMU_3_00
:
611 /* Field deprecated in ISAv3.00 - interrupts always go to hyperv */
615 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM0
);
619 if (vpm
&& !msr_hv
) {
620 cs
->exception_index
= POWERPC_EXCP_HISI
;
622 cs
->exception_index
= POWERPC_EXCP_ISI
;
624 env
->error_code
= error_code
;
627 static void ppc_hash64_set_dsi(CPUState
*cs
, CPUPPCState
*env
, uint64_t dar
,
633 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM1
);
635 switch (env
->mmu_model
) {
636 case POWERPC_MMU_3_00
:
637 /* Field deprecated in ISAv3.00 - interrupts always go to hyperv */
641 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM0
);
645 if (vpm
&& !msr_hv
) {
646 cs
->exception_index
= POWERPC_EXCP_HDSI
;
647 env
->spr
[SPR_HDAR
] = dar
;
648 env
->spr
[SPR_HDSISR
] = dsisr
;
650 cs
->exception_index
= POWERPC_EXCP_DSI
;
651 env
->spr
[SPR_DAR
] = dar
;
652 env
->spr
[SPR_DSISR
] = dsisr
;
658 int ppc_hash64_handle_mmu_fault(PowerPCCPU
*cpu
, vaddr eaddr
,
659 int rwx
, int mmu_idx
)
661 CPUState
*cs
= CPU(cpu
);
662 CPUPPCState
*env
= &cpu
->env
;
666 ppc_hash_pte64_t pte
;
667 int pp_prot
, amr_prot
, prot
;
668 uint64_t new_pte1
, dsisr
;
669 const int need_prot
[] = {PAGE_READ
, PAGE_WRITE
, PAGE_EXEC
};
672 assert((rwx
== 0) || (rwx
== 1) || (rwx
== 2));
674 /* Note on LPCR usage: 970 uses HID4, but our special variant
675 * of store_spr copies relevant fields into env->spr[SPR_LPCR].
676 * Similarily we filter unimplemented bits when storing into
677 * LPCR depending on the MMU version. This code can thus just
678 * use the LPCR "as-is".
681 /* 1. Handle real mode accesses */
682 if (((rwx
== 2) && (msr_ir
== 0)) || ((rwx
!= 2) && (msr_dr
== 0))) {
683 /* Translation is supposedly "off" */
684 /* In real mode the top 4 effective address bits are (mostly) ignored */
685 raddr
= eaddr
& 0x0FFFFFFFFFFFFFFFULL
;
687 /* In HV mode, add HRMOR if top EA bit is clear */
688 if (msr_hv
|| !env
->has_hv_mode
) {
689 if (!(eaddr
>> 63)) {
690 raddr
|= env
->spr
[SPR_HRMOR
];
693 /* Otherwise, check VPM for RMA vs VRMA */
694 if (env
->spr
[SPR_LPCR
] & LPCR_VPM0
) {
695 slb
= &env
->vrma_slb
;
697 goto skip_slb_search
;
699 /* Not much else to do here */
700 cs
->exception_index
= POWERPC_EXCP_MCHECK
;
703 } else if (raddr
< env
->rmls
) {
704 /* RMA. Check bounds in RMLS */
705 raddr
|= env
->spr
[SPR_RMOR
];
707 /* The access failed, generate the approriate interrupt */
709 ppc_hash64_set_isi(cs
, env
, 0x08000000);
715 ppc_hash64_set_dsi(cs
, env
, eaddr
, dsisr
);
720 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
, raddr
& TARGET_PAGE_MASK
,
721 PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
, mmu_idx
,
726 /* 2. Translation is on, so look up the SLB */
727 slb
= slb_lookup(cpu
, eaddr
);
730 cs
->exception_index
= POWERPC_EXCP_ISEG
;
733 cs
->exception_index
= POWERPC_EXCP_DSEG
;
735 env
->spr
[SPR_DAR
] = eaddr
;
742 /* 3. Check for segment level no-execute violation */
743 if ((rwx
== 2) && (slb
->vsid
& SLB_VSID_N
)) {
744 ppc_hash64_set_isi(cs
, env
, 0x10000000);
748 /* 4. Locate the PTE in the hash table */
749 ptex
= ppc_hash64_htab_lookup(cpu
, slb
, eaddr
, &pte
, &apshift
);
753 ppc_hash64_set_isi(cs
, env
, dsisr
);
758 ppc_hash64_set_dsi(cs
, env
, eaddr
, dsisr
);
762 qemu_log_mask(CPU_LOG_MMU
,
763 "found PTE at index %08" HWADDR_PRIx
"\n", ptex
);
765 /* 5. Check access permissions */
767 pp_prot
= ppc_hash64_pte_prot(cpu
, slb
, pte
);
768 amr_prot
= ppc_hash64_amr_prot(cpu
, pte
);
769 prot
= pp_prot
& amr_prot
;
771 if ((need_prot
[rwx
] & ~prot
) != 0) {
772 /* Access right violation */
773 qemu_log_mask(CPU_LOG_MMU
, "PTE access rejected\n");
775 ppc_hash64_set_isi(cs
, env
, 0x08000000);
778 if (need_prot
[rwx
] & ~pp_prot
) {
784 if (need_prot
[rwx
] & ~amr_prot
) {
787 ppc_hash64_set_dsi(cs
, env
, eaddr
, dsisr
);
792 qemu_log_mask(CPU_LOG_MMU
, "PTE access granted !\n");
794 /* 6. Update PTE referenced and changed bits if necessary */
796 new_pte1
= pte
.pte1
| HPTE64_R_R
; /* set referenced bit */
798 new_pte1
|= HPTE64_R_C
; /* set changed (dirty) bit */
800 /* Treat the page as read-only for now, so that a later write
801 * will pass through this function again to set the C bit */
805 if (new_pte1
!= pte
.pte1
) {
806 ppc_hash64_store_hpte(cpu
, ptex
, pte
.pte0
, new_pte1
);
809 /* 7. Determine the real address from the PTE */
811 raddr
= deposit64(pte
.pte1
& HPTE64_R_RPN
, 0, apshift
, eaddr
);
813 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
, raddr
& TARGET_PAGE_MASK
,
814 prot
, mmu_idx
, 1ULL << apshift
);
819 hwaddr
ppc_hash64_get_phys_page_debug(PowerPCCPU
*cpu
, target_ulong addr
)
821 CPUPPCState
*env
= &cpu
->env
;
824 ppc_hash_pte64_t pte
;
827 /* Handle real mode */
829 /* In real mode the top 4 effective address bits are ignored */
830 raddr
= addr
& 0x0FFFFFFFFFFFFFFFULL
;
832 /* In HV mode, add HRMOR if top EA bit is clear */
833 if ((msr_hv
|| !env
->has_hv_mode
) && !(addr
>> 63)) {
834 return raddr
| env
->spr
[SPR_HRMOR
];
837 /* Otherwise, check VPM for RMA vs VRMA */
838 if (env
->spr
[SPR_LPCR
] & LPCR_VPM0
) {
839 slb
= &env
->vrma_slb
;
843 } else if (raddr
< env
->rmls
) {
844 /* RMA. Check bounds in RMLS */
845 return raddr
| env
->spr
[SPR_RMOR
];
850 slb
= slb_lookup(cpu
, addr
);
856 ptex
= ppc_hash64_htab_lookup(cpu
, slb
, addr
, &pte
, &apshift
);
861 return deposit64(pte
.pte1
& HPTE64_R_RPN
, 0, apshift
, addr
)
865 void ppc_hash64_store_hpte(PowerPCCPU
*cpu
, hwaddr ptex
,
866 uint64_t pte0
, uint64_t pte1
)
868 hwaddr base
= ppc_hash64_hpt_base(cpu
);
869 hwaddr offset
= ptex
* HASH_PTE_SIZE_64
;
872 PPCVirtualHypervisorClass
*vhc
=
873 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
874 vhc
->store_hpte(cpu
->vhyp
, ptex
, pte0
, pte1
);
878 stq_phys(CPU(cpu
)->as
, base
+ offset
, pte0
);
879 stq_phys(CPU(cpu
)->as
, base
+ offset
+ HASH_PTE_SIZE_64
/ 2, pte1
);
882 void ppc_hash64_tlb_flush_hpte(PowerPCCPU
*cpu
, target_ulong ptex
,
883 target_ulong pte0
, target_ulong pte1
)
886 * XXX: given the fact that there are too many segments to
887 * invalidate, and we still don't have a tlb_flush_mask(env, n,
888 * mask) in QEMU, we just invalidate all TLBs
890 cpu
->env
.tlb_need_flush
= TLB_NEED_GLOBAL_FLUSH
| TLB_NEED_LOCAL_FLUSH
;
893 void ppc_hash64_update_rmls(CPUPPCState
*env
)
895 uint64_t lpcr
= env
->spr
[SPR_LPCR
];
898 * This is the full 4 bits encoding of POWER8. Previous
899 * CPUs only support a subset of these but the filtering
900 * is done when writing LPCR
902 switch ((lpcr
& LPCR_RMLS
) >> LPCR_RMLS_SHIFT
) {
904 env
->rmls
= 0x2000000ull
;
907 env
->rmls
= 0x4000000ull
;
909 case 0x7: /* 128MB */
910 env
->rmls
= 0x8000000ull
;
912 case 0x4: /* 256MB */
913 env
->rmls
= 0x10000000ull
;
916 env
->rmls
= 0x40000000ull
;
919 env
->rmls
= 0x400000000ull
;
922 /* What to do here ??? */
927 void ppc_hash64_update_vrma(CPUPPCState
*env
)
929 const struct ppc_one_seg_page_size
*sps
= NULL
;
930 target_ulong esid
, vsid
, lpcr
;
931 ppc_slb_t
*slb
= &env
->vrma_slb
;
936 slb
->esid
= slb
->vsid
= 0;
939 /* Is VRMA enabled ? */
940 lpcr
= env
->spr
[SPR_LPCR
];
941 if (!(lpcr
& LPCR_VPM0
)) {
945 /* Make one up. Mostly ignore the ESID which will not be
946 * needed for translation
948 vsid
= SLB_VSID_VRMA
;
949 vrmasd
= (lpcr
& LPCR_VRMASD
) >> LPCR_VRMASD_SHIFT
;
950 vsid
|= (vrmasd
<< 4) & (SLB_VSID_L
| SLB_VSID_LP
);
953 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
954 const struct ppc_one_seg_page_size
*sps1
= &env
->sps
.sps
[i
];
956 if (!sps1
->page_shift
) {
960 if ((vsid
& SLB_VSID_LLP_MASK
) == sps1
->slb_enc
) {
967 error_report("Bad page size encoding esid 0x"TARGET_FMT_lx
968 " vsid 0x"TARGET_FMT_lx
, esid
, vsid
);
977 void helper_store_lpcr(CPUPPCState
*env
, target_ulong val
)
981 /* Filter out bits */
982 switch (env
->mmu_model
) {
983 case POWERPC_MMU_64B
: /* 970 */
987 if (val
& 0x8000000000000000ull
) {
991 lpcr
|= (0x4ull
<< LPCR_RMLS_SHIFT
);
993 if (val
& 0x4000000000000000ull
) {
994 lpcr
|= (0x2ull
<< LPCR_RMLS_SHIFT
);
996 if (val
& 0x2000000000000000ull
) {
997 lpcr
|= (0x1ull
<< LPCR_RMLS_SHIFT
);
999 env
->spr
[SPR_RMOR
] = ((lpcr
>> 41) & 0xffffull
) << 26;
1001 /* XXX We could also write LPID from HID4 here
1002 * but since we don't tag any translation on it
1003 * it doesn't actually matter
1005 /* XXX For proper emulation of 970 we also need
1006 * to dig HRMOR out of HID5
1009 case POWERPC_MMU_2_03
: /* P5p */
1010 lpcr
= val
& (LPCR_RMLS
| LPCR_ILE
|
1011 LPCR_LPES0
| LPCR_LPES1
|
1012 LPCR_RMI
| LPCR_HDICE
);
1014 case POWERPC_MMU_2_06
: /* P7 */
1015 lpcr
= val
& (LPCR_VPM0
| LPCR_VPM1
| LPCR_ISL
| LPCR_DPFD
|
1016 LPCR_VRMASD
| LPCR_RMLS
| LPCR_ILE
|
1017 LPCR_P7_PECE0
| LPCR_P7_PECE1
| LPCR_P7_PECE2
|
1018 LPCR_MER
| LPCR_TC
|
1019 LPCR_LPES0
| LPCR_LPES1
| LPCR_HDICE
);
1021 case POWERPC_MMU_2_07
: /* P8 */
1022 lpcr
= val
& (LPCR_VPM0
| LPCR_VPM1
| LPCR_ISL
| LPCR_KBV
|
1023 LPCR_DPFD
| LPCR_VRMASD
| LPCR_RMLS
| LPCR_ILE
|
1024 LPCR_AIL
| LPCR_ONL
| LPCR_P8_PECE0
| LPCR_P8_PECE1
|
1025 LPCR_P8_PECE2
| LPCR_P8_PECE3
| LPCR_P8_PECE4
|
1026 LPCR_MER
| LPCR_TC
| LPCR_LPES0
| LPCR_HDICE
);
1028 case POWERPC_MMU_3_00
: /* P9 */
1029 lpcr
= val
& (LPCR_VPM1
| LPCR_ISL
| LPCR_KBV
| LPCR_DPFD
|
1030 (LPCR_PECE_U_MASK
& LPCR_HVEE
) | LPCR_ILE
| LPCR_AIL
|
1031 LPCR_UPRT
| LPCR_EVIRT
| LPCR_ONL
|
1032 (LPCR_PECE_L_MASK
& (LPCR_PDEE
| LPCR_HDEE
| LPCR_EEE
|
1033 LPCR_DEE
| LPCR_OEE
)) | LPCR_MER
| LPCR_GTSE
| LPCR_TC
|
1034 LPCR_HEIC
| LPCR_LPES0
| LPCR_HVICE
| LPCR_HDICE
);
1039 env
->spr
[SPR_LPCR
] = lpcr
;
1040 ppc_hash64_update_rmls(env
);
1041 ppc_hash64_update_vrma(env
);