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"
22 #include "exec/exec-all.h"
23 #include "exec/helper-proto.h"
24 #include "qemu/error-report.h"
25 #include "qemu/qemu-print.h"
26 #include "sysemu/hw_accel.h"
28 #include "mmu-hash64.h"
31 #include "mmu-book3s-v3.h"
33 /* #define DEBUG_SLB */
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
< cpu
->hash64_opts
->slb_size
; 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
);
62 * We check for 1T matches on all MMUs here - if the MMU
63 * doesn't have 1T segment support, we will have prevented 1T
64 * entries from being inserted in the slbmte code.
66 if (((slb
->esid
== esid_256M
) &&
67 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_256M
))
68 || ((slb
->esid
== esid_1T
) &&
69 ((slb
->vsid
& SLB_VSID_B
) == SLB_VSID_B_1T
))) {
77 void dump_slb(PowerPCCPU
*cpu
)
79 CPUPPCState
*env
= &cpu
->env
;
83 cpu_synchronize_state(CPU(cpu
));
85 qemu_printf("SLB\tESID\t\t\tVSID\n");
86 for (i
= 0; i
< cpu
->hash64_opts
->slb_size
; i
++) {
87 slbe
= env
->slb
[i
].esid
;
88 slbv
= env
->slb
[i
].vsid
;
89 if (slbe
== 0 && slbv
== 0) {
92 qemu_printf("%d\t0x%016" PRIx64
"\t0x%016" PRIx64
"\n",
97 void helper_slbia(CPUPPCState
*env
)
99 PowerPCCPU
*cpu
= env_archcpu(env
);
102 /* XXX: Warning: slbia never invalidates the first segment */
103 for (n
= 1; n
< cpu
->hash64_opts
->slb_size
; n
++) {
104 ppc_slb_t
*slb
= &env
->slb
[n
];
106 if (slb
->esid
& SLB_ESID_V
) {
107 slb
->esid
&= ~SLB_ESID_V
;
109 * XXX: given the fact that segment size is 256 MB or 1TB,
110 * and we still don't have a tlb_flush_mask(env, n, mask)
111 * in QEMU, we just invalidate all TLBs
113 env
->tlb_need_flush
|= TLB_NEED_LOCAL_FLUSH
;
118 static void __helper_slbie(CPUPPCState
*env
, target_ulong addr
,
121 PowerPCCPU
*cpu
= env_archcpu(env
);
124 slb
= slb_lookup(cpu
, addr
);
129 if (slb
->esid
& SLB_ESID_V
) {
130 slb
->esid
&= ~SLB_ESID_V
;
133 * XXX: given the fact that segment size is 256 MB or 1TB,
134 * and we still don't have a tlb_flush_mask(env, n, mask)
135 * in QEMU, we just invalidate all TLBs
137 env
->tlb_need_flush
|=
138 (global
== false ? TLB_NEED_LOCAL_FLUSH
: TLB_NEED_GLOBAL_FLUSH
);
142 void helper_slbie(CPUPPCState
*env
, target_ulong addr
)
144 __helper_slbie(env
, addr
, false);
147 void helper_slbieg(CPUPPCState
*env
, target_ulong addr
)
149 __helper_slbie(env
, addr
, true);
152 int ppc_store_slb(PowerPCCPU
*cpu
, target_ulong slot
,
153 target_ulong esid
, target_ulong vsid
)
155 CPUPPCState
*env
= &cpu
->env
;
156 ppc_slb_t
*slb
= &env
->slb
[slot
];
157 const PPCHash64SegmentPageSizes
*sps
= NULL
;
160 if (slot
>= cpu
->hash64_opts
->slb_size
) {
161 return -1; /* Bad slot number */
163 if (esid
& ~(SLB_ESID_ESID
| SLB_ESID_V
)) {
164 return -1; /* Reserved bits set */
166 if (vsid
& (SLB_VSID_B
& ~SLB_VSID_B_1T
)) {
167 return -1; /* Bad segment size */
169 if ((vsid
& SLB_VSID_B
) && !(ppc_hash64_has(cpu
, PPC_HASH64_1TSEG
))) {
170 return -1; /* 1T segment on MMU that doesn't support it */
173 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
174 const PPCHash64SegmentPageSizes
*sps1
= &cpu
->hash64_opts
->sps
[i
];
176 if (!sps1
->page_shift
) {
180 if ((vsid
& SLB_VSID_LLP_MASK
) == sps1
->slb_enc
) {
187 error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
188 " esid 0x"TARGET_FMT_lx
" vsid 0x"TARGET_FMT_lx
,
197 LOG_SLB("%s: " TARGET_FMT_lu
" " TARGET_FMT_lx
" - " TARGET_FMT_lx
198 " => %016" PRIx64
" %016" PRIx64
"\n", __func__
, slot
, esid
, vsid
,
199 slb
->esid
, slb
->vsid
);
204 static int ppc_load_slb_esid(PowerPCCPU
*cpu
, target_ulong rb
,
207 CPUPPCState
*env
= &cpu
->env
;
208 int slot
= rb
& 0xfff;
209 ppc_slb_t
*slb
= &env
->slb
[slot
];
211 if (slot
>= cpu
->hash64_opts
->slb_size
) {
219 static int ppc_load_slb_vsid(PowerPCCPU
*cpu
, target_ulong rb
,
222 CPUPPCState
*env
= &cpu
->env
;
223 int slot
= rb
& 0xfff;
224 ppc_slb_t
*slb
= &env
->slb
[slot
];
226 if (slot
>= cpu
->hash64_opts
->slb_size
) {
234 static int ppc_find_slb_vsid(PowerPCCPU
*cpu
, target_ulong rb
,
237 CPUPPCState
*env
= &cpu
->env
;
240 if (!msr_is_64bit(env
, env
->msr
)) {
243 slb
= slb_lookup(cpu
, rb
);
245 *rt
= (target_ulong
)-1ul;
252 void helper_store_slb(CPUPPCState
*env
, target_ulong rb
, target_ulong rs
)
254 PowerPCCPU
*cpu
= env_archcpu(env
);
256 if (ppc_store_slb(cpu
, rb
& 0xfff, rb
& ~0xfffULL
, rs
) < 0) {
257 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
258 POWERPC_EXCP_INVAL
, GETPC());
262 target_ulong
helper_load_slb_esid(CPUPPCState
*env
, target_ulong rb
)
264 PowerPCCPU
*cpu
= env_archcpu(env
);
267 if (ppc_load_slb_esid(cpu
, rb
, &rt
) < 0) {
268 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
269 POWERPC_EXCP_INVAL
, GETPC());
274 target_ulong
helper_find_slb_vsid(CPUPPCState
*env
, target_ulong rb
)
276 PowerPCCPU
*cpu
= env_archcpu(env
);
279 if (ppc_find_slb_vsid(cpu
, rb
, &rt
) < 0) {
280 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
281 POWERPC_EXCP_INVAL
, GETPC());
286 target_ulong
helper_load_slb_vsid(CPUPPCState
*env
, target_ulong rb
)
288 PowerPCCPU
*cpu
= env_archcpu(env
);
291 if (ppc_load_slb_vsid(cpu
, rb
, &rt
) < 0) {
292 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
293 POWERPC_EXCP_INVAL
, GETPC());
298 /* Check No-Execute or Guarded Storage */
299 static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU
*cpu
,
300 ppc_hash_pte64_t pte
)
302 /* Exec permissions CANNOT take away read or write permissions */
303 return (pte
.pte1
& HPTE64_R_N
) || (pte
.pte1
& HPTE64_R_G
) ?
304 PAGE_READ
| PAGE_WRITE
: PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
307 /* Check Basic Storage Protection */
308 static int ppc_hash64_pte_prot(PowerPCCPU
*cpu
,
309 ppc_slb_t
*slb
, ppc_hash_pte64_t pte
)
311 CPUPPCState
*env
= &cpu
->env
;
314 * Some pp bit combinations have undefined behaviour, so default
315 * to no access in those cases
319 key
= !!(msr_pr
? (slb
->vsid
& SLB_VSID_KP
)
320 : (slb
->vsid
& SLB_VSID_KS
));
321 pp
= (pte
.pte1
& HPTE64_R_PP
) | ((pte
.pte1
& HPTE64_R_PP0
) >> 61);
328 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
333 prot
= PAGE_READ
| PAGE_EXEC
;
344 prot
= PAGE_READ
| PAGE_EXEC
;
348 prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
356 /* Check the instruction access permissions specified in the IAMR */
357 static int ppc_hash64_iamr_prot(PowerPCCPU
*cpu
, int key
)
359 CPUPPCState
*env
= &cpu
->env
;
360 int iamr_bits
= (env
->spr
[SPR_IAMR
] >> 2 * (31 - key
)) & 0x3;
363 * An instruction fetch is permitted if the IAMR bit is 0.
364 * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit
365 * can only take away EXEC permissions not READ or WRITE permissions.
366 * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since
367 * EXEC permissions are allowed.
369 return (iamr_bits
& 0x1) ? PAGE_READ
| PAGE_WRITE
:
370 PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
373 static int ppc_hash64_amr_prot(PowerPCCPU
*cpu
, ppc_hash_pte64_t pte
)
375 CPUPPCState
*env
= &cpu
->env
;
377 int prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
379 /* Only recent MMUs implement Virtual Page Class Key Protection */
380 if (!ppc_hash64_has(cpu
, PPC_HASH64_AMR
)) {
384 key
= HPTE64_R_KEY(pte
.pte1
);
385 amrbits
= (env
->spr
[SPR_AMR
] >> 2 * (31 - key
)) & 0x3;
387 /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
388 /* env->spr[SPR_AMR]); */
391 * A store is permitted if the AMR bit is 0. Remove write
392 * protection if it is set.
398 * A load is permitted if the AMR bit is 0. Remove read
399 * protection if it is set.
405 switch (env
->mmu_model
) {
407 * MMU version 2.07 and later support IAMR
408 * Check if the IAMR allows the instruction access - it will return
409 * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0
410 * if it does (and prot will be unchanged indicating execution support).
412 case POWERPC_MMU_2_07
:
413 case POWERPC_MMU_3_00
:
414 prot
&= ppc_hash64_iamr_prot(cpu
, key
);
423 const ppc_hash_pte64_t
*ppc_hash64_map_hptes(PowerPCCPU
*cpu
,
426 hwaddr pte_offset
= ptex
* HASH_PTE_SIZE_64
;
428 hwaddr plen
= n
* HASH_PTE_SIZE_64
;
429 const ppc_hash_pte64_t
*hptes
;
432 PPCVirtualHypervisorClass
*vhc
=
433 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
434 return vhc
->map_hptes(cpu
->vhyp
, ptex
, n
);
436 base
= ppc_hash64_hpt_base(cpu
);
442 hptes
= address_space_map(CPU(cpu
)->as
, base
+ pte_offset
, &plen
, false,
443 MEMTXATTRS_UNSPECIFIED
);
444 if (plen
< (n
* HASH_PTE_SIZE_64
)) {
445 hw_error("%s: Unable to map all requested HPTEs\n", __func__
);
450 void ppc_hash64_unmap_hptes(PowerPCCPU
*cpu
, const ppc_hash_pte64_t
*hptes
,
454 PPCVirtualHypervisorClass
*vhc
=
455 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
456 vhc
->unmap_hptes(cpu
->vhyp
, hptes
, ptex
, n
);
460 address_space_unmap(CPU(cpu
)->as
, (void *)hptes
, n
* HASH_PTE_SIZE_64
,
461 false, n
* HASH_PTE_SIZE_64
);
464 static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes
*sps
,
465 uint64_t pte0
, uint64_t pte1
)
469 if (!(pte0
& HPTE64_V_LARGE
)) {
470 if (sps
->page_shift
!= 12) {
471 /* 4kiB page in a non 4kiB segment */
474 /* Normal 4kiB page */
478 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
479 const PPCHash64PageSize
*ps
= &sps
->enc
[i
];
482 if (!ps
->page_shift
) {
486 if (ps
->page_shift
== 12) {
487 /* L bit is set so this can't be a 4kiB page */
491 mask
= ((1ULL << ps
->page_shift
) - 1) & HPTE64_R_RPN
;
493 if ((pte1
& mask
) == ((uint64_t)ps
->pte_enc
<< HPTE64_R_RPN_SHIFT
)) {
494 return ps
->page_shift
;
498 return 0; /* Bad page size encoding */
501 static void ppc64_v3_new_to_old_hpte(target_ulong
*pte0
, target_ulong
*pte1
)
503 /* Insert B into pte0 */
504 *pte0
= (*pte0
& HPTE64_V_COMMON_BITS
) |
505 ((*pte1
& HPTE64_R_3_0_SSIZE_MASK
) <<
506 (HPTE64_V_SSIZE_SHIFT
- HPTE64_R_3_0_SSIZE_SHIFT
));
508 /* Remove B from pte1 */
509 *pte1
= *pte1
& ~HPTE64_R_3_0_SSIZE_MASK
;
513 static hwaddr
ppc_hash64_pteg_search(PowerPCCPU
*cpu
, hwaddr hash
,
514 const PPCHash64SegmentPageSizes
*sps
,
516 ppc_hash_pte64_t
*pte
, unsigned *pshift
)
519 const ppc_hash_pte64_t
*pteg
;
520 target_ulong pte0
, pte1
;
523 ptex
= (hash
& ppc_hash64_hpt_mask(cpu
)) * HPTES_PER_GROUP
;
524 pteg
= ppc_hash64_map_hptes(cpu
, ptex
, HPTES_PER_GROUP
);
528 for (i
= 0; i
< HPTES_PER_GROUP
; i
++) {
529 pte0
= ppc_hash64_hpte0(cpu
, pteg
, i
);
531 * pte0 contains the valid bit and must be read before pte1,
532 * otherwise we might see an old pte1 with a new valid bit and
533 * thus an inconsistent hpte value
536 pte1
= ppc_hash64_hpte1(cpu
, pteg
, i
);
538 /* Convert format if necessary */
539 if (cpu
->env
.mmu_model
== POWERPC_MMU_3_00
&& !cpu
->vhyp
) {
540 ppc64_v3_new_to_old_hpte(&pte0
, &pte1
);
543 /* This compares V, B, H (secondary) and the AVPN */
544 if (HPTE64_V_COMPARE(pte0
, ptem
)) {
545 *pshift
= hpte_page_shift(sps
, pte0
, pte1
);
547 * If there is no match, ignore the PTE, it could simply
548 * be for a different segment size encoding and the
549 * architecture specifies we should not match. Linux will
550 * potentially leave behind PTEs for the wrong base page
551 * size when demoting segments.
557 * We don't do anything with pshift yet as qemu TLB only
558 * deals with 4K pages anyway
562 ppc_hash64_unmap_hptes(cpu
, pteg
, ptex
, HPTES_PER_GROUP
);
566 ppc_hash64_unmap_hptes(cpu
, pteg
, ptex
, HPTES_PER_GROUP
);
568 * We didn't find a valid entry.
573 static hwaddr
ppc_hash64_htab_lookup(PowerPCCPU
*cpu
,
574 ppc_slb_t
*slb
, target_ulong eaddr
,
575 ppc_hash_pte64_t
*pte
, unsigned *pshift
)
577 CPUPPCState
*env
= &cpu
->env
;
579 uint64_t vsid
, epnmask
, epn
, ptem
;
580 const PPCHash64SegmentPageSizes
*sps
= slb
->sps
;
583 * The SLB store path should prevent any bad page size encodings
584 * getting in there, so:
588 /* If ISL is set in LPCR we need to clamp the page size to 4K */
589 if (env
->spr
[SPR_LPCR
] & LPCR_ISL
) {
590 /* We assume that when using TCG, 4k is first entry of SPS */
591 sps
= &cpu
->hash64_opts
->sps
[0];
592 assert(sps
->page_shift
== 12);
595 epnmask
= ~((1ULL << sps
->page_shift
) - 1);
597 if (slb
->vsid
& SLB_VSID_B
) {
599 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT_1T
;
600 epn
= (eaddr
& ~SEGMENT_MASK_1T
) & epnmask
;
601 hash
= vsid
^ (vsid
<< 25) ^ (epn
>> sps
->page_shift
);
604 vsid
= (slb
->vsid
& SLB_VSID_VSID
) >> SLB_VSID_SHIFT
;
605 epn
= (eaddr
& ~SEGMENT_MASK_256M
) & epnmask
;
606 hash
= vsid
^ (epn
>> sps
->page_shift
);
608 ptem
= (slb
->vsid
& SLB_VSID_PTEM
) | ((epn
>> 16) & HPTE64_V_AVPN
);
609 ptem
|= HPTE64_V_VALID
;
611 /* Page address translation */
612 qemu_log_mask(CPU_LOG_MMU
,
613 "htab_base " TARGET_FMT_plx
" htab_mask " TARGET_FMT_plx
614 " hash " TARGET_FMT_plx
"\n",
615 ppc_hash64_hpt_base(cpu
), ppc_hash64_hpt_mask(cpu
), hash
);
617 /* Primary PTEG lookup */
618 qemu_log_mask(CPU_LOG_MMU
,
619 "0 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
620 " vsid=" TARGET_FMT_lx
" ptem=" TARGET_FMT_lx
621 " hash=" TARGET_FMT_plx
"\n",
622 ppc_hash64_hpt_base(cpu
), ppc_hash64_hpt_mask(cpu
),
624 ptex
= ppc_hash64_pteg_search(cpu
, hash
, sps
, ptem
, pte
, pshift
);
627 /* Secondary PTEG lookup */
628 ptem
|= HPTE64_V_SECONDARY
;
629 qemu_log_mask(CPU_LOG_MMU
,
630 "1 htab=" TARGET_FMT_plx
"/" TARGET_FMT_plx
631 " vsid=" TARGET_FMT_lx
" api=" TARGET_FMT_lx
632 " hash=" TARGET_FMT_plx
"\n", ppc_hash64_hpt_base(cpu
),
633 ppc_hash64_hpt_mask(cpu
), vsid
, ptem
, ~hash
);
635 ptex
= ppc_hash64_pteg_search(cpu
, ~hash
, sps
, ptem
, pte
, pshift
);
641 unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU
*cpu
,
642 uint64_t pte0
, uint64_t pte1
)
646 if (!(pte0
& HPTE64_V_LARGE
)) {
651 * The encodings in env->sps need to be carefully chosen so that
652 * this gives an unambiguous result.
654 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
655 const PPCHash64SegmentPageSizes
*sps
= &cpu
->hash64_opts
->sps
[i
];
658 if (!sps
->page_shift
) {
662 shift
= hpte_page_shift(sps
, pte0
, pte1
);
671 static void ppc_hash64_set_isi(CPUState
*cs
, uint64_t error_code
)
673 CPUPPCState
*env
= &POWERPC_CPU(cs
)->env
;
677 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM1
);
679 switch (env
->mmu_model
) {
680 case POWERPC_MMU_3_00
:
681 /* Field deprecated in ISAv3.00 - interrupts always go to hyperv */
685 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM0
);
689 if (vpm
&& !msr_hv
) {
690 cs
->exception_index
= POWERPC_EXCP_HISI
;
692 cs
->exception_index
= POWERPC_EXCP_ISI
;
694 env
->error_code
= error_code
;
697 static void ppc_hash64_set_dsi(CPUState
*cs
, uint64_t dar
, uint64_t dsisr
)
699 CPUPPCState
*env
= &POWERPC_CPU(cs
)->env
;
703 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM1
);
705 switch (env
->mmu_model
) {
706 case POWERPC_MMU_3_00
:
707 /* Field deprecated in ISAv3.00 - interrupts always go to hyperv */
711 vpm
= !!(env
->spr
[SPR_LPCR
] & LPCR_VPM0
);
715 if (vpm
&& !msr_hv
) {
716 cs
->exception_index
= POWERPC_EXCP_HDSI
;
717 env
->spr
[SPR_HDAR
] = dar
;
718 env
->spr
[SPR_HDSISR
] = dsisr
;
720 cs
->exception_index
= POWERPC_EXCP_DSI
;
721 env
->spr
[SPR_DAR
] = dar
;
722 env
->spr
[SPR_DSISR
] = dsisr
;
728 static void ppc_hash64_set_r(PowerPCCPU
*cpu
, hwaddr ptex
, uint64_t pte1
)
730 hwaddr base
, offset
= ptex
* HASH_PTE_SIZE_64
+ 16;
733 PPCVirtualHypervisorClass
*vhc
=
734 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
735 vhc
->hpte_set_r(cpu
->vhyp
, ptex
, pte1
);
738 base
= ppc_hash64_hpt_base(cpu
);
741 /* The HW performs a non-atomic byte update */
742 stb_phys(CPU(cpu
)->as
, base
+ offset
, ((pte1
>> 8) & 0xff) | 0x01);
745 static void ppc_hash64_set_c(PowerPCCPU
*cpu
, hwaddr ptex
, uint64_t pte1
)
747 hwaddr base
, offset
= ptex
* HASH_PTE_SIZE_64
+ 15;
750 PPCVirtualHypervisorClass
*vhc
=
751 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
752 vhc
->hpte_set_c(cpu
->vhyp
, ptex
, pte1
);
755 base
= ppc_hash64_hpt_base(cpu
);
757 /* The HW performs a non-atomic byte update */
758 stb_phys(CPU(cpu
)->as
, base
+ offset
, (pte1
& 0xff) | 0x80);
761 int ppc_hash64_handle_mmu_fault(PowerPCCPU
*cpu
, vaddr eaddr
,
762 int rwx
, int mmu_idx
)
764 CPUState
*cs
= CPU(cpu
);
765 CPUPPCState
*env
= &cpu
->env
;
769 ppc_hash_pte64_t pte
;
770 int exec_prot
, pp_prot
, amr_prot
, prot
;
771 const int need_prot
[] = {PAGE_READ
, PAGE_WRITE
, PAGE_EXEC
};
774 assert((rwx
== 0) || (rwx
== 1) || (rwx
== 2));
777 * Note on LPCR usage: 970 uses HID4, but our special variant of
778 * store_spr copies relevant fields into env->spr[SPR_LPCR].
779 * Similarily we filter unimplemented bits when storing into LPCR
780 * depending on the MMU version. This code can thus just use the
784 /* 1. Handle real mode accesses */
785 if (((rwx
== 2) && (msr_ir
== 0)) || ((rwx
!= 2) && (msr_dr
== 0))) {
787 * Translation is supposedly "off", but in real mode the top 4
788 * effective address bits are (mostly) ignored
790 raddr
= eaddr
& 0x0FFFFFFFFFFFFFFFULL
;
792 /* In HV mode, add HRMOR if top EA bit is clear */
793 if (msr_hv
|| !env
->has_hv_mode
) {
794 if (!(eaddr
>> 63)) {
795 raddr
|= env
->spr
[SPR_HRMOR
];
798 /* Otherwise, check VPM for RMA vs VRMA */
799 if (env
->spr
[SPR_LPCR
] & LPCR_VPM0
) {
800 slb
= &env
->vrma_slb
;
802 goto skip_slb_search
;
804 /* Not much else to do here */
805 cs
->exception_index
= POWERPC_EXCP_MCHECK
;
808 } else if (raddr
< env
->rmls
) {
809 /* RMA. Check bounds in RMLS */
810 raddr
|= env
->spr
[SPR_RMOR
];
812 /* The access failed, generate the approriate interrupt */
814 ppc_hash64_set_isi(cs
, SRR1_PROTFAULT
);
816 int dsisr
= DSISR_PROTFAULT
;
818 dsisr
|= DSISR_ISSTORE
;
820 ppc_hash64_set_dsi(cs
, eaddr
, dsisr
);
825 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
, raddr
& TARGET_PAGE_MASK
,
826 PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
, mmu_idx
,
831 /* 2. Translation is on, so look up the SLB */
832 slb
= slb_lookup(cpu
, eaddr
);
834 /* No entry found, check if in-memory segment tables are in use */
835 if (ppc64_use_proc_tbl(cpu
)) {
836 /* TODO - Unsupported */
837 error_report("Segment Table Support Unimplemented");
840 /* Segment still not found, generate the appropriate interrupt */
842 cs
->exception_index
= POWERPC_EXCP_ISEG
;
845 cs
->exception_index
= POWERPC_EXCP_DSEG
;
847 env
->spr
[SPR_DAR
] = eaddr
;
854 /* 3. Check for segment level no-execute violation */
855 if ((rwx
== 2) && (slb
->vsid
& SLB_VSID_N
)) {
856 ppc_hash64_set_isi(cs
, SRR1_NOEXEC_GUARD
);
860 /* 4. Locate the PTE in the hash table */
861 ptex
= ppc_hash64_htab_lookup(cpu
, slb
, eaddr
, &pte
, &apshift
);
864 ppc_hash64_set_isi(cs
, SRR1_NOPTE
);
866 int dsisr
= DSISR_NOPTE
;
868 dsisr
|= DSISR_ISSTORE
;
870 ppc_hash64_set_dsi(cs
, eaddr
, dsisr
);
874 qemu_log_mask(CPU_LOG_MMU
,
875 "found PTE at index %08" HWADDR_PRIx
"\n", ptex
);
877 /* 5. Check access permissions */
879 exec_prot
= ppc_hash64_pte_noexec_guard(cpu
, pte
);
880 pp_prot
= ppc_hash64_pte_prot(cpu
, slb
, pte
);
881 amr_prot
= ppc_hash64_amr_prot(cpu
, pte
);
882 prot
= exec_prot
& pp_prot
& amr_prot
;
884 if ((need_prot
[rwx
] & ~prot
) != 0) {
885 /* Access right violation */
886 qemu_log_mask(CPU_LOG_MMU
, "PTE access rejected\n");
889 if (PAGE_EXEC
& ~exec_prot
) {
890 srr1
|= SRR1_NOEXEC_GUARD
; /* Access violates noexec or guard */
891 } else if (PAGE_EXEC
& ~pp_prot
) {
892 srr1
|= SRR1_PROTFAULT
; /* Access violates access authority */
894 if (PAGE_EXEC
& ~amr_prot
) {
895 srr1
|= SRR1_IAMR
; /* Access violates virt pg class key prot */
897 ppc_hash64_set_isi(cs
, srr1
);
900 if (need_prot
[rwx
] & ~pp_prot
) {
901 dsisr
|= DSISR_PROTFAULT
;
904 dsisr
|= DSISR_ISSTORE
;
906 if (need_prot
[rwx
] & ~amr_prot
) {
909 ppc_hash64_set_dsi(cs
, eaddr
, dsisr
);
914 qemu_log_mask(CPU_LOG_MMU
, "PTE access granted !\n");
916 /* 6. Update PTE referenced and changed bits if necessary */
918 if (!(pte
.pte1
& HPTE64_R_R
)) {
919 ppc_hash64_set_r(cpu
, ptex
, pte
.pte1
);
921 if (!(pte
.pte1
& HPTE64_R_C
)) {
923 ppc_hash64_set_c(cpu
, ptex
, pte
.pte1
);
926 * Treat the page as read-only for now, so that a later write
927 * will pass through this function again to set the C bit
933 /* 7. Determine the real address from the PTE */
935 raddr
= deposit64(pte
.pte1
& HPTE64_R_RPN
, 0, apshift
, eaddr
);
937 tlb_set_page(cs
, eaddr
& TARGET_PAGE_MASK
, raddr
& TARGET_PAGE_MASK
,
938 prot
, mmu_idx
, 1ULL << apshift
);
943 hwaddr
ppc_hash64_get_phys_page_debug(PowerPCCPU
*cpu
, target_ulong addr
)
945 CPUPPCState
*env
= &cpu
->env
;
948 ppc_hash_pte64_t pte
;
951 /* Handle real mode */
953 /* In real mode the top 4 effective address bits are ignored */
954 raddr
= addr
& 0x0FFFFFFFFFFFFFFFULL
;
956 /* In HV mode, add HRMOR if top EA bit is clear */
957 if ((msr_hv
|| !env
->has_hv_mode
) && !(addr
>> 63)) {
958 return raddr
| env
->spr
[SPR_HRMOR
];
961 /* Otherwise, check VPM for RMA vs VRMA */
962 if (env
->spr
[SPR_LPCR
] & LPCR_VPM0
) {
963 slb
= &env
->vrma_slb
;
967 } else if (raddr
< env
->rmls
) {
968 /* RMA. Check bounds in RMLS */
969 return raddr
| env
->spr
[SPR_RMOR
];
974 slb
= slb_lookup(cpu
, addr
);
980 ptex
= ppc_hash64_htab_lookup(cpu
, slb
, addr
, &pte
, &apshift
);
985 return deposit64(pte
.pte1
& HPTE64_R_RPN
, 0, apshift
, addr
)
989 void ppc_hash64_tlb_flush_hpte(PowerPCCPU
*cpu
, target_ulong ptex
,
990 target_ulong pte0
, target_ulong pte1
)
993 * XXX: given the fact that there are too many segments to
994 * invalidate, and we still don't have a tlb_flush_mask(env, n,
995 * mask) in QEMU, we just invalidate all TLBs
997 cpu
->env
.tlb_need_flush
= TLB_NEED_GLOBAL_FLUSH
| TLB_NEED_LOCAL_FLUSH
;
1000 static void ppc_hash64_update_rmls(PowerPCCPU
*cpu
)
1002 CPUPPCState
*env
= &cpu
->env
;
1003 uint64_t lpcr
= env
->spr
[SPR_LPCR
];
1006 * This is the full 4 bits encoding of POWER8. Previous
1007 * CPUs only support a subset of these but the filtering
1008 * is done when writing LPCR
1010 switch ((lpcr
& LPCR_RMLS
) >> LPCR_RMLS_SHIFT
) {
1011 case 0x8: /* 32MB */
1012 env
->rmls
= 0x2000000ull
;
1014 case 0x3: /* 64MB */
1015 env
->rmls
= 0x4000000ull
;
1017 case 0x7: /* 128MB */
1018 env
->rmls
= 0x8000000ull
;
1020 case 0x4: /* 256MB */
1021 env
->rmls
= 0x10000000ull
;
1024 env
->rmls
= 0x40000000ull
;
1026 case 0x1: /* 16GB */
1027 env
->rmls
= 0x400000000ull
;
1030 /* What to do here ??? */
1035 static void ppc_hash64_update_vrma(PowerPCCPU
*cpu
)
1037 CPUPPCState
*env
= &cpu
->env
;
1038 const PPCHash64SegmentPageSizes
*sps
= NULL
;
1039 target_ulong esid
, vsid
, lpcr
;
1040 ppc_slb_t
*slb
= &env
->vrma_slb
;
1044 /* First clear it */
1045 slb
->esid
= slb
->vsid
= 0;
1048 /* Is VRMA enabled ? */
1049 lpcr
= env
->spr
[SPR_LPCR
];
1050 if (!(lpcr
& LPCR_VPM0
)) {
1055 * Make one up. Mostly ignore the ESID which will not be needed
1058 vsid
= SLB_VSID_VRMA
;
1059 vrmasd
= (lpcr
& LPCR_VRMASD
) >> LPCR_VRMASD_SHIFT
;
1060 vsid
|= (vrmasd
<< 4) & (SLB_VSID_L
| SLB_VSID_LP
);
1063 for (i
= 0; i
< PPC_PAGE_SIZES_MAX_SZ
; i
++) {
1064 const PPCHash64SegmentPageSizes
*sps1
= &cpu
->hash64_opts
->sps
[i
];
1066 if (!sps1
->page_shift
) {
1070 if ((vsid
& SLB_VSID_LLP_MASK
) == sps1
->slb_enc
) {
1077 error_report("Bad page size encoding esid 0x"TARGET_FMT_lx
1078 " vsid 0x"TARGET_FMT_lx
, esid
, vsid
);
1087 void ppc_store_lpcr(PowerPCCPU
*cpu
, target_ulong val
)
1089 CPUPPCState
*env
= &cpu
->env
;
1092 /* Filter out bits */
1093 switch (env
->mmu_model
) {
1094 case POWERPC_MMU_64B
: /* 970 */
1098 if (val
& 0x8000000000000000ull
) {
1102 lpcr
|= (0x4ull
<< LPCR_RMLS_SHIFT
);
1104 if (val
& 0x4000000000000000ull
) {
1105 lpcr
|= (0x2ull
<< LPCR_RMLS_SHIFT
);
1107 if (val
& 0x2000000000000000ull
) {
1108 lpcr
|= (0x1ull
<< LPCR_RMLS_SHIFT
);
1110 env
->spr
[SPR_RMOR
] = ((lpcr
>> 41) & 0xffffull
) << 26;
1113 * XXX We could also write LPID from HID4 here
1114 * but since we don't tag any translation on it
1115 * it doesn't actually matter
1117 * XXX For proper emulation of 970 we also need
1118 * to dig HRMOR out of HID5
1121 case POWERPC_MMU_2_03
: /* P5p */
1122 lpcr
= val
& (LPCR_RMLS
| LPCR_ILE
|
1123 LPCR_LPES0
| LPCR_LPES1
|
1124 LPCR_RMI
| LPCR_HDICE
);
1126 case POWERPC_MMU_2_06
: /* P7 */
1127 lpcr
= val
& (LPCR_VPM0
| LPCR_VPM1
| LPCR_ISL
| LPCR_DPFD
|
1128 LPCR_VRMASD
| LPCR_RMLS
| LPCR_ILE
|
1129 LPCR_P7_PECE0
| LPCR_P7_PECE1
| LPCR_P7_PECE2
|
1130 LPCR_MER
| LPCR_TC
|
1131 LPCR_LPES0
| LPCR_LPES1
| LPCR_HDICE
);
1133 case POWERPC_MMU_2_07
: /* P8 */
1134 lpcr
= val
& (LPCR_VPM0
| LPCR_VPM1
| LPCR_ISL
| LPCR_KBV
|
1135 LPCR_DPFD
| LPCR_VRMASD
| LPCR_RMLS
| LPCR_ILE
|
1136 LPCR_AIL
| LPCR_ONL
| LPCR_P8_PECE0
| LPCR_P8_PECE1
|
1137 LPCR_P8_PECE2
| LPCR_P8_PECE3
| LPCR_P8_PECE4
|
1138 LPCR_MER
| LPCR_TC
| LPCR_LPES0
| LPCR_HDICE
);
1140 case POWERPC_MMU_3_00
: /* P9 */
1141 lpcr
= val
& (LPCR_VPM1
| LPCR_ISL
| LPCR_KBV
| LPCR_DPFD
|
1142 (LPCR_PECE_U_MASK
& LPCR_HVEE
) | LPCR_ILE
| LPCR_AIL
|
1143 LPCR_UPRT
| LPCR_EVIRT
| LPCR_ONL
| LPCR_HR
| LPCR_LD
|
1144 (LPCR_PECE_L_MASK
& (LPCR_PDEE
| LPCR_HDEE
| LPCR_EEE
|
1145 LPCR_DEE
| LPCR_OEE
)) | LPCR_MER
| LPCR_GTSE
| LPCR_TC
|
1146 LPCR_HEIC
| LPCR_LPES0
| LPCR_HVICE
| LPCR_HDICE
);
1148 * If we have a virtual hypervisor, we need to bring back RMLS. It
1149 * doesn't exist on an actual P9 but that's all we know how to
1150 * configure with softmmu at the moment
1153 lpcr
|= (val
& LPCR_RMLS
);
1159 env
->spr
[SPR_LPCR
] = lpcr
;
1160 ppc_hash64_update_rmls(cpu
);
1161 ppc_hash64_update_vrma(cpu
);
1164 void helper_store_lpcr(CPUPPCState
*env
, target_ulong val
)
1166 PowerPCCPU
*cpu
= env_archcpu(env
);
1168 ppc_store_lpcr(cpu
, val
);
1171 void ppc_hash64_init(PowerPCCPU
*cpu
)
1173 CPUPPCState
*env
= &cpu
->env
;
1174 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
1176 if (!pcc
->hash64_opts
) {
1177 assert(!(env
->mmu_model
& POWERPC_MMU_64
));
1181 cpu
->hash64_opts
= g_memdup(pcc
->hash64_opts
, sizeof(*cpu
->hash64_opts
));
1184 void ppc_hash64_finalize(PowerPCCPU
*cpu
)
1186 g_free(cpu
->hash64_opts
);
1189 const PPCHash64Options ppc_hash64_opts_basic
= {
1193 { .page_shift
= 12, /* 4K */
1195 .enc
= { { .page_shift
= 12, .pte_enc
= 0 } }
1197 { .page_shift
= 24, /* 16M */
1199 .enc
= { { .page_shift
= 24, .pte_enc
= 0 } }
1204 const PPCHash64Options ppc_hash64_opts_POWER7
= {
1205 .flags
= PPC_HASH64_1TSEG
| PPC_HASH64_AMR
| PPC_HASH64_CI_LARGEPAGE
,
1209 .page_shift
= 12, /* 4K */
1211 .enc
= { { .page_shift
= 12, .pte_enc
= 0 },
1212 { .page_shift
= 16, .pte_enc
= 0x7 },
1213 { .page_shift
= 24, .pte_enc
= 0x38 }, },
1216 .page_shift
= 16, /* 64K */
1217 .slb_enc
= SLB_VSID_64K
,
1218 .enc
= { { .page_shift
= 16, .pte_enc
= 0x1 },
1219 { .page_shift
= 24, .pte_enc
= 0x8 }, },
1222 .page_shift
= 24, /* 16M */
1223 .slb_enc
= SLB_VSID_16M
,
1224 .enc
= { { .page_shift
= 24, .pte_enc
= 0 }, },
1227 .page_shift
= 34, /* 16G */
1228 .slb_enc
= SLB_VSID_16G
,
1229 .enc
= { { .page_shift
= 34, .pte_enc
= 0x3 }, },
1234 void ppc_hash64_filter_pagesizes(PowerPCCPU
*cpu
,
1235 bool (*cb
)(void *, uint32_t, uint32_t),
1238 PPCHash64Options
*opts
= cpu
->hash64_opts
;
1241 bool ci_largepage
= false;
1246 for (i
= 0; i
< ARRAY_SIZE(opts
->sps
); i
++) {
1247 PPCHash64SegmentPageSizes
*sps
= &opts
->sps
[i
];
1253 if (!sps
->page_shift
) {
1257 for (j
= 0; j
< ARRAY_SIZE(sps
->enc
); j
++) {
1258 PPCHash64PageSize
*ps
= &sps
->enc
[j
];
1261 if (!ps
->page_shift
) {
1265 if (cb(opaque
, sps
->page_shift
, ps
->page_shift
)) {
1266 if (ps
->page_shift
>= 16) {
1267 ci_largepage
= true;
1269 sps
->enc
[m
++] = *ps
;
1273 /* Clear rest of the row */
1274 for (j
= m
; j
< ARRAY_SIZE(sps
->enc
); j
++) {
1275 memset(&sps
->enc
[j
], 0, sizeof(sps
->enc
[j
]));
1283 /* Clear the rest of the table */
1284 for (i
= n
; i
< ARRAY_SIZE(opts
->sps
); i
++) {
1285 memset(&opts
->sps
[i
], 0, sizeof(opts
->sps
[i
]));
1288 if (!ci_largepage
) {
1289 opts
->flags
&= ~PPC_HASH64_CI_LARGEPAGE
;