target/ppc: Introduce prot_for_access_type
[qemu/ar7.git] / target / ppc / mmu-hash64.c
blobd5b70ddc9cf43cf9a0cac033de18090f66a8f2cc
1 /*
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"
22 #include "cpu.h"
23 #include "exec/exec-all.h"
24 #include "exec/helper-proto.h"
25 #include "qemu/error-report.h"
26 #include "qemu/qemu-print.h"
27 #include "sysemu/hw_accel.h"
28 #include "kvm_ppc.h"
29 #include "mmu-hash64.h"
30 #include "exec/log.h"
31 #include "hw/hw.h"
32 #include "internal.h"
33 #include "mmu-book3s-v3.h"
34 #include "helper_regs.h"
36 /* #define DEBUG_SLB */
38 #ifdef DEBUG_SLB
39 # define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
40 #else
41 # define LOG_SLB(...) do { } while (0)
42 #endif
45 * SLB handling
48 static ppc_slb_t *slb_lookup(PowerPCCPU *cpu, target_ulong eaddr)
50 CPUPPCState *env = &cpu->env;
51 uint64_t esid_256M, esid_1T;
52 int n;
54 LOG_SLB("%s: eaddr " TARGET_FMT_lx "\n", __func__, eaddr);
56 esid_256M = (eaddr & SEGMENT_MASK_256M) | SLB_ESID_V;
57 esid_1T = (eaddr & SEGMENT_MASK_1T) | SLB_ESID_V;
59 for (n = 0; n < cpu->hash64_opts->slb_size; n++) {
60 ppc_slb_t *slb = &env->slb[n];
62 LOG_SLB("%s: slot %d %016" PRIx64 " %016"
63 PRIx64 "\n", __func__, n, slb->esid, slb->vsid);
65 * We check for 1T matches on all MMUs here - if the MMU
66 * doesn't have 1T segment support, we will have prevented 1T
67 * entries from being inserted in the slbmte code.
69 if (((slb->esid == esid_256M) &&
70 ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_256M))
71 || ((slb->esid == esid_1T) &&
72 ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_1T))) {
73 return slb;
77 return NULL;
80 void dump_slb(PowerPCCPU *cpu)
82 CPUPPCState *env = &cpu->env;
83 int i;
84 uint64_t slbe, slbv;
86 cpu_synchronize_state(CPU(cpu));
88 qemu_printf("SLB\tESID\t\t\tVSID\n");
89 for (i = 0; i < cpu->hash64_opts->slb_size; i++) {
90 slbe = env->slb[i].esid;
91 slbv = env->slb[i].vsid;
92 if (slbe == 0 && slbv == 0) {
93 continue;
95 qemu_printf("%d\t0x%016" PRIx64 "\t0x%016" PRIx64 "\n",
96 i, slbe, slbv);
100 void helper_slbia(CPUPPCState *env, uint32_t ih)
102 PowerPCCPU *cpu = env_archcpu(env);
103 int starting_entry;
104 int n;
107 * slbia must always flush all TLB (which is equivalent to ERAT in ppc
108 * architecture). Matching on SLB_ESID_V is not good enough, because slbmte
109 * can overwrite a valid SLB without flushing its lookaside information.
111 * It would be possible to keep the TLB in synch with the SLB by flushing
112 * when a valid entry is overwritten by slbmte, and therefore slbia would
113 * not have to flush unless it evicts a valid SLB entry. However it is
114 * expected that slbmte is more common than slbia, and slbia is usually
115 * going to evict valid SLB entries, so that tradeoff is unlikely to be a
116 * good one.
118 * ISA v2.05 introduced IH field with values 0,1,2,6. These all invalidate
119 * the same SLB entries (everything but entry 0), but differ in what
120 * "lookaside information" is invalidated. TCG can ignore this and flush
121 * everything.
123 * ISA v3.0 introduced additional values 3,4,7, which change what SLBs are
124 * invalidated.
127 env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH;
129 starting_entry = 1; /* default for IH=0,1,2,6 */
131 if (env->mmu_model == POWERPC_MMU_3_00) {
132 switch (ih) {
133 case 0x7:
134 /* invalidate no SLBs, but all lookaside information */
135 return;
137 case 0x3:
138 case 0x4:
139 /* also considers SLB entry 0 */
140 starting_entry = 0;
141 break;
143 case 0x5:
144 /* treat undefined values as ih==0, and warn */
145 qemu_log_mask(LOG_GUEST_ERROR,
146 "slbia undefined IH field %u.\n", ih);
147 break;
149 default:
150 /* 0,1,2,6 */
151 break;
155 for (n = starting_entry; n < cpu->hash64_opts->slb_size; n++) {
156 ppc_slb_t *slb = &env->slb[n];
158 if (!(slb->esid & SLB_ESID_V)) {
159 continue;
161 if (env->mmu_model == POWERPC_MMU_3_00) {
162 if (ih == 0x3 && (slb->vsid & SLB_VSID_C) == 0) {
163 /* preserves entries with a class value of 0 */
164 continue;
168 slb->esid &= ~SLB_ESID_V;
172 static void __helper_slbie(CPUPPCState *env, target_ulong addr,
173 target_ulong global)
175 PowerPCCPU *cpu = env_archcpu(env);
176 ppc_slb_t *slb;
178 slb = slb_lookup(cpu, addr);
179 if (!slb) {
180 return;
183 if (slb->esid & SLB_ESID_V) {
184 slb->esid &= ~SLB_ESID_V;
187 * XXX: given the fact that segment size is 256 MB or 1TB,
188 * and we still don't have a tlb_flush_mask(env, n, mask)
189 * in QEMU, we just invalidate all TLBs
191 env->tlb_need_flush |=
192 (global == false ? TLB_NEED_LOCAL_FLUSH : TLB_NEED_GLOBAL_FLUSH);
196 void helper_slbie(CPUPPCState *env, target_ulong addr)
198 __helper_slbie(env, addr, false);
201 void helper_slbieg(CPUPPCState *env, target_ulong addr)
203 __helper_slbie(env, addr, true);
206 int ppc_store_slb(PowerPCCPU *cpu, target_ulong slot,
207 target_ulong esid, target_ulong vsid)
209 CPUPPCState *env = &cpu->env;
210 ppc_slb_t *slb = &env->slb[slot];
211 const PPCHash64SegmentPageSizes *sps = NULL;
212 int i;
214 if (slot >= cpu->hash64_opts->slb_size) {
215 return -1; /* Bad slot number */
217 if (esid & ~(SLB_ESID_ESID | SLB_ESID_V)) {
218 return -1; /* Reserved bits set */
220 if (vsid & (SLB_VSID_B & ~SLB_VSID_B_1T)) {
221 return -1; /* Bad segment size */
223 if ((vsid & SLB_VSID_B) && !(ppc_hash64_has(cpu, PPC_HASH64_1TSEG))) {
224 return -1; /* 1T segment on MMU that doesn't support it */
227 for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
228 const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i];
230 if (!sps1->page_shift) {
231 break;
234 if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) {
235 sps = sps1;
236 break;
240 if (!sps) {
241 error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu
242 " esid 0x"TARGET_FMT_lx" vsid 0x"TARGET_FMT_lx,
243 slot, esid, vsid);
244 return -1;
247 slb->esid = esid;
248 slb->vsid = vsid;
249 slb->sps = sps;
251 LOG_SLB("%s: " TARGET_FMT_lu " " TARGET_FMT_lx " - " TARGET_FMT_lx
252 " => %016" PRIx64 " %016" PRIx64 "\n", __func__, slot, esid, vsid,
253 slb->esid, slb->vsid);
255 return 0;
258 static int ppc_load_slb_esid(PowerPCCPU *cpu, target_ulong rb,
259 target_ulong *rt)
261 CPUPPCState *env = &cpu->env;
262 int slot = rb & 0xfff;
263 ppc_slb_t *slb = &env->slb[slot];
265 if (slot >= cpu->hash64_opts->slb_size) {
266 return -1;
269 *rt = slb->esid;
270 return 0;
273 static int ppc_load_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
274 target_ulong *rt)
276 CPUPPCState *env = &cpu->env;
277 int slot = rb & 0xfff;
278 ppc_slb_t *slb = &env->slb[slot];
280 if (slot >= cpu->hash64_opts->slb_size) {
281 return -1;
284 *rt = slb->vsid;
285 return 0;
288 static int ppc_find_slb_vsid(PowerPCCPU *cpu, target_ulong rb,
289 target_ulong *rt)
291 CPUPPCState *env = &cpu->env;
292 ppc_slb_t *slb;
294 if (!msr_is_64bit(env, env->msr)) {
295 rb &= 0xffffffff;
297 slb = slb_lookup(cpu, rb);
298 if (slb == NULL) {
299 *rt = (target_ulong)-1ul;
300 } else {
301 *rt = slb->vsid;
303 return 0;
306 void helper_store_slb(CPUPPCState *env, target_ulong rb, target_ulong rs)
308 PowerPCCPU *cpu = env_archcpu(env);
310 if (ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs) < 0) {
311 raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
312 POWERPC_EXCP_INVAL, GETPC());
316 target_ulong helper_load_slb_esid(CPUPPCState *env, target_ulong rb)
318 PowerPCCPU *cpu = env_archcpu(env);
319 target_ulong rt = 0;
321 if (ppc_load_slb_esid(cpu, rb, &rt) < 0) {
322 raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
323 POWERPC_EXCP_INVAL, GETPC());
325 return rt;
328 target_ulong helper_find_slb_vsid(CPUPPCState *env, target_ulong rb)
330 PowerPCCPU *cpu = env_archcpu(env);
331 target_ulong rt = 0;
333 if (ppc_find_slb_vsid(cpu, rb, &rt) < 0) {
334 raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
335 POWERPC_EXCP_INVAL, GETPC());
337 return rt;
340 target_ulong helper_load_slb_vsid(CPUPPCState *env, target_ulong rb)
342 PowerPCCPU *cpu = env_archcpu(env);
343 target_ulong rt = 0;
345 if (ppc_load_slb_vsid(cpu, rb, &rt) < 0) {
346 raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
347 POWERPC_EXCP_INVAL, GETPC());
349 return rt;
352 /* Check No-Execute or Guarded Storage */
353 static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU *cpu,
354 ppc_hash_pte64_t pte)
356 /* Exec permissions CANNOT take away read or write permissions */
357 return (pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G) ?
358 PAGE_READ | PAGE_WRITE : PAGE_READ | PAGE_WRITE | PAGE_EXEC;
361 /* Check Basic Storage Protection */
362 static int ppc_hash64_pte_prot(PowerPCCPU *cpu,
363 ppc_slb_t *slb, ppc_hash_pte64_t pte)
365 CPUPPCState *env = &cpu->env;
366 unsigned pp, key;
368 * Some pp bit combinations have undefined behaviour, so default
369 * to no access in those cases
371 int prot = 0;
373 key = !!(msr_pr ? (slb->vsid & SLB_VSID_KP)
374 : (slb->vsid & SLB_VSID_KS));
375 pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61);
377 if (key == 0) {
378 switch (pp) {
379 case 0x0:
380 case 0x1:
381 case 0x2:
382 prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
383 break;
385 case 0x3:
386 case 0x6:
387 prot = PAGE_READ | PAGE_EXEC;
388 break;
390 } else {
391 switch (pp) {
392 case 0x0:
393 case 0x6:
394 break;
396 case 0x1:
397 case 0x3:
398 prot = PAGE_READ | PAGE_EXEC;
399 break;
401 case 0x2:
402 prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
403 break;
407 return prot;
410 /* Check the instruction access permissions specified in the IAMR */
411 static int ppc_hash64_iamr_prot(PowerPCCPU *cpu, int key)
413 CPUPPCState *env = &cpu->env;
414 int iamr_bits = (env->spr[SPR_IAMR] >> 2 * (31 - key)) & 0x3;
417 * An instruction fetch is permitted if the IAMR bit is 0.
418 * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit
419 * can only take away EXEC permissions not READ or WRITE permissions.
420 * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since
421 * EXEC permissions are allowed.
423 return (iamr_bits & 0x1) ? PAGE_READ | PAGE_WRITE :
424 PAGE_READ | PAGE_WRITE | PAGE_EXEC;
427 static int ppc_hash64_amr_prot(PowerPCCPU *cpu, ppc_hash_pte64_t pte)
429 CPUPPCState *env = &cpu->env;
430 int key, amrbits;
431 int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
433 /* Only recent MMUs implement Virtual Page Class Key Protection */
434 if (!ppc_hash64_has(cpu, PPC_HASH64_AMR)) {
435 return prot;
438 key = HPTE64_R_KEY(pte.pte1);
439 amrbits = (env->spr[SPR_AMR] >> 2 * (31 - key)) & 0x3;
441 /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */
442 /* env->spr[SPR_AMR]); */
445 * A store is permitted if the AMR bit is 0. Remove write
446 * protection if it is set.
448 if (amrbits & 0x2) {
449 prot &= ~PAGE_WRITE;
452 * A load is permitted if the AMR bit is 0. Remove read
453 * protection if it is set.
455 if (amrbits & 0x1) {
456 prot &= ~PAGE_READ;
459 switch (env->mmu_model) {
461 * MMU version 2.07 and later support IAMR
462 * Check if the IAMR allows the instruction access - it will return
463 * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0
464 * if it does (and prot will be unchanged indicating execution support).
466 case POWERPC_MMU_2_07:
467 case POWERPC_MMU_3_00:
468 prot &= ppc_hash64_iamr_prot(cpu, key);
469 break;
470 default:
471 break;
474 return prot;
477 const ppc_hash_pte64_t *ppc_hash64_map_hptes(PowerPCCPU *cpu,
478 hwaddr ptex, int n)
480 hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
481 hwaddr base;
482 hwaddr plen = n * HASH_PTE_SIZE_64;
483 const ppc_hash_pte64_t *hptes;
485 if (cpu->vhyp) {
486 PPCVirtualHypervisorClass *vhc =
487 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
488 return vhc->map_hptes(cpu->vhyp, ptex, n);
490 base = ppc_hash64_hpt_base(cpu);
492 if (!base) {
493 return NULL;
496 hptes = address_space_map(CPU(cpu)->as, base + pte_offset, &plen, false,
497 MEMTXATTRS_UNSPECIFIED);
498 if (plen < (n * HASH_PTE_SIZE_64)) {
499 hw_error("%s: Unable to map all requested HPTEs\n", __func__);
501 return hptes;
504 void ppc_hash64_unmap_hptes(PowerPCCPU *cpu, const ppc_hash_pte64_t *hptes,
505 hwaddr ptex, int n)
507 if (cpu->vhyp) {
508 PPCVirtualHypervisorClass *vhc =
509 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
510 vhc->unmap_hptes(cpu->vhyp, hptes, ptex, n);
511 return;
514 address_space_unmap(CPU(cpu)->as, (void *)hptes, n * HASH_PTE_SIZE_64,
515 false, n * HASH_PTE_SIZE_64);
518 static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes *sps,
519 uint64_t pte0, uint64_t pte1)
521 int i;
523 if (!(pte0 & HPTE64_V_LARGE)) {
524 if (sps->page_shift != 12) {
525 /* 4kiB page in a non 4kiB segment */
526 return 0;
528 /* Normal 4kiB page */
529 return 12;
532 for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
533 const PPCHash64PageSize *ps = &sps->enc[i];
534 uint64_t mask;
536 if (!ps->page_shift) {
537 break;
540 if (ps->page_shift == 12) {
541 /* L bit is set so this can't be a 4kiB page */
542 continue;
545 mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN;
547 if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) {
548 return ps->page_shift;
552 return 0; /* Bad page size encoding */
555 static void ppc64_v3_new_to_old_hpte(target_ulong *pte0, target_ulong *pte1)
557 /* Insert B into pte0 */
558 *pte0 = (*pte0 & HPTE64_V_COMMON_BITS) |
559 ((*pte1 & HPTE64_R_3_0_SSIZE_MASK) <<
560 (HPTE64_V_SSIZE_SHIFT - HPTE64_R_3_0_SSIZE_SHIFT));
562 /* Remove B from pte1 */
563 *pte1 = *pte1 & ~HPTE64_R_3_0_SSIZE_MASK;
567 static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash,
568 const PPCHash64SegmentPageSizes *sps,
569 target_ulong ptem,
570 ppc_hash_pte64_t *pte, unsigned *pshift)
572 int i;
573 const ppc_hash_pte64_t *pteg;
574 target_ulong pte0, pte1;
575 target_ulong ptex;
577 ptex = (hash & ppc_hash64_hpt_mask(cpu)) * HPTES_PER_GROUP;
578 pteg = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP);
579 if (!pteg) {
580 return -1;
582 for (i = 0; i < HPTES_PER_GROUP; i++) {
583 pte0 = ppc_hash64_hpte0(cpu, pteg, i);
585 * pte0 contains the valid bit and must be read before pte1,
586 * otherwise we might see an old pte1 with a new valid bit and
587 * thus an inconsistent hpte value
589 smp_rmb();
590 pte1 = ppc_hash64_hpte1(cpu, pteg, i);
592 /* Convert format if necessary */
593 if (cpu->env.mmu_model == POWERPC_MMU_3_00 && !cpu->vhyp) {
594 ppc64_v3_new_to_old_hpte(&pte0, &pte1);
597 /* This compares V, B, H (secondary) and the AVPN */
598 if (HPTE64_V_COMPARE(pte0, ptem)) {
599 *pshift = hpte_page_shift(sps, pte0, pte1);
601 * If there is no match, ignore the PTE, it could simply
602 * be for a different segment size encoding and the
603 * architecture specifies we should not match. Linux will
604 * potentially leave behind PTEs for the wrong base page
605 * size when demoting segments.
607 if (*pshift == 0) {
608 continue;
611 * We don't do anything with pshift yet as qemu TLB only
612 * deals with 4K pages anyway
614 pte->pte0 = pte0;
615 pte->pte1 = pte1;
616 ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
617 return ptex + i;
620 ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP);
622 * We didn't find a valid entry.
624 return -1;
627 static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu,
628 ppc_slb_t *slb, target_ulong eaddr,
629 ppc_hash_pte64_t *pte, unsigned *pshift)
631 CPUPPCState *env = &cpu->env;
632 hwaddr hash, ptex;
633 uint64_t vsid, epnmask, epn, ptem;
634 const PPCHash64SegmentPageSizes *sps = slb->sps;
637 * The SLB store path should prevent any bad page size encodings
638 * getting in there, so:
640 assert(sps);
642 /* If ISL is set in LPCR we need to clamp the page size to 4K */
643 if (env->spr[SPR_LPCR] & LPCR_ISL) {
644 /* We assume that when using TCG, 4k is first entry of SPS */
645 sps = &cpu->hash64_opts->sps[0];
646 assert(sps->page_shift == 12);
649 epnmask = ~((1ULL << sps->page_shift) - 1);
651 if (slb->vsid & SLB_VSID_B) {
652 /* 1TB segment */
653 vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T;
654 epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask;
655 hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift);
656 } else {
657 /* 256M segment */
658 vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT;
659 epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask;
660 hash = vsid ^ (epn >> sps->page_shift);
662 ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN);
663 ptem |= HPTE64_V_VALID;
665 /* Page address translation */
666 qemu_log_mask(CPU_LOG_MMU,
667 "htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx
668 " hash " TARGET_FMT_plx "\n",
669 ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), hash);
671 /* Primary PTEG lookup */
672 qemu_log_mask(CPU_LOG_MMU,
673 "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
674 " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
675 " hash=" TARGET_FMT_plx "\n",
676 ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu),
677 vsid, ptem, hash);
678 ptex = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift);
680 if (ptex == -1) {
681 /* Secondary PTEG lookup */
682 ptem |= HPTE64_V_SECONDARY;
683 qemu_log_mask(CPU_LOG_MMU,
684 "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
685 " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
686 " hash=" TARGET_FMT_plx "\n", ppc_hash64_hpt_base(cpu),
687 ppc_hash64_hpt_mask(cpu), vsid, ptem, ~hash);
689 ptex = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift);
692 return ptex;
695 unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu,
696 uint64_t pte0, uint64_t pte1)
698 int i;
700 if (!(pte0 & HPTE64_V_LARGE)) {
701 return 12;
705 * The encodings in env->sps need to be carefully chosen so that
706 * this gives an unambiguous result.
708 for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
709 const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
710 unsigned shift;
712 if (!sps->page_shift) {
713 break;
716 shift = hpte_page_shift(sps, pte0, pte1);
717 if (shift) {
718 return shift;
722 return 0;
725 static bool ppc_hash64_use_vrma(CPUPPCState *env)
727 switch (env->mmu_model) {
728 case POWERPC_MMU_3_00:
730 * ISAv3.0 (POWER9) always uses VRMA, the VPM0 field and RMOR
731 * register no longer exist
733 return true;
735 default:
736 return !!(env->spr[SPR_LPCR] & LPCR_VPM0);
740 static void ppc_hash64_set_isi(CPUState *cs, uint64_t error_code)
742 CPUPPCState *env = &POWERPC_CPU(cs)->env;
743 bool vpm;
745 if (msr_ir) {
746 vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
747 } else {
748 vpm = ppc_hash64_use_vrma(env);
750 if (vpm && !msr_hv) {
751 cs->exception_index = POWERPC_EXCP_HISI;
752 } else {
753 cs->exception_index = POWERPC_EXCP_ISI;
755 env->error_code = error_code;
758 static void ppc_hash64_set_dsi(CPUState *cs, uint64_t dar, uint64_t dsisr)
760 CPUPPCState *env = &POWERPC_CPU(cs)->env;
761 bool vpm;
763 if (msr_dr) {
764 vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1);
765 } else {
766 vpm = ppc_hash64_use_vrma(env);
768 if (vpm && !msr_hv) {
769 cs->exception_index = POWERPC_EXCP_HDSI;
770 env->spr[SPR_HDAR] = dar;
771 env->spr[SPR_HDSISR] = dsisr;
772 } else {
773 cs->exception_index = POWERPC_EXCP_DSI;
774 env->spr[SPR_DAR] = dar;
775 env->spr[SPR_DSISR] = dsisr;
777 env->error_code = 0;
781 static void ppc_hash64_set_r(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1)
783 hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + 16;
785 if (cpu->vhyp) {
786 PPCVirtualHypervisorClass *vhc =
787 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
788 vhc->hpte_set_r(cpu->vhyp, ptex, pte1);
789 return;
791 base = ppc_hash64_hpt_base(cpu);
794 /* The HW performs a non-atomic byte update */
795 stb_phys(CPU(cpu)->as, base + offset, ((pte1 >> 8) & 0xff) | 0x01);
798 static void ppc_hash64_set_c(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1)
800 hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + 15;
802 if (cpu->vhyp) {
803 PPCVirtualHypervisorClass *vhc =
804 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
805 vhc->hpte_set_c(cpu->vhyp, ptex, pte1);
806 return;
808 base = ppc_hash64_hpt_base(cpu);
810 /* The HW performs a non-atomic byte update */
811 stb_phys(CPU(cpu)->as, base + offset, (pte1 & 0xff) | 0x80);
814 static target_ulong rmls_limit(PowerPCCPU *cpu)
816 CPUPPCState *env = &cpu->env;
818 * In theory the meanings of RMLS values are implementation
819 * dependent. In practice, this seems to have been the set from
820 * POWER4+..POWER8, and RMLS is no longer supported in POWER9.
822 * Unsupported values mean the OS has shot itself in the
823 * foot. Return a 0-sized RMA in this case, which we expect
824 * to trigger an immediate DSI or ISI
826 static const target_ulong rma_sizes[16] = {
827 [0] = 256 * GiB,
828 [1] = 16 * GiB,
829 [2] = 1 * GiB,
830 [3] = 64 * MiB,
831 [4] = 256 * MiB,
832 [7] = 128 * MiB,
833 [8] = 32 * MiB,
835 target_ulong rmls = (env->spr[SPR_LPCR] & LPCR_RMLS) >> LPCR_RMLS_SHIFT;
837 return rma_sizes[rmls];
840 static int build_vrma_slbe(PowerPCCPU *cpu, ppc_slb_t *slb)
842 CPUPPCState *env = &cpu->env;
843 target_ulong lpcr = env->spr[SPR_LPCR];
844 uint32_t vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT;
845 target_ulong vsid = SLB_VSID_VRMA | ((vrmasd << 4) & SLB_VSID_LLP_MASK);
846 int i;
848 for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
849 const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i];
851 if (!sps->page_shift) {
852 break;
855 if ((vsid & SLB_VSID_LLP_MASK) == sps->slb_enc) {
856 slb->esid = SLB_ESID_V;
857 slb->vsid = vsid;
858 slb->sps = sps;
859 return 0;
863 error_report("Bad page size encoding in LPCR[VRMASD]; LPCR=0x"
864 TARGET_FMT_lx, lpcr);
866 return -1;
869 int ppc_hash64_handle_mmu_fault(PowerPCCPU *cpu, vaddr eaddr,
870 int rwx, int mmu_idx)
872 CPUState *cs = CPU(cpu);
873 CPUPPCState *env = &cpu->env;
874 ppc_slb_t vrma_slbe;
875 ppc_slb_t *slb;
876 unsigned apshift;
877 hwaddr ptex;
878 ppc_hash_pte64_t pte;
879 int exec_prot, pp_prot, amr_prot, prot;
880 int need_prot;
881 hwaddr raddr;
883 assert((rwx == 0) || (rwx == 1) || (rwx == 2));
886 * Note on LPCR usage: 970 uses HID4, but our special variant of
887 * store_spr copies relevant fields into env->spr[SPR_LPCR].
888 * Similarly we filter unimplemented bits when storing into LPCR
889 * depending on the MMU version. This code can thus just use the
890 * LPCR "as-is".
893 /* 1. Handle real mode accesses */
894 if (((rwx == 2) && (msr_ir == 0)) || ((rwx != 2) && (msr_dr == 0))) {
896 * Translation is supposedly "off", but in real mode the top 4
897 * effective address bits are (mostly) ignored
899 raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL;
901 if (cpu->vhyp) {
903 * In virtual hypervisor mode, there's nothing to do:
904 * EA == GPA == qemu guest address
906 } else if (msr_hv || !env->has_hv_mode) {
907 /* In HV mode, add HRMOR if top EA bit is clear */
908 if (!(eaddr >> 63)) {
909 raddr |= env->spr[SPR_HRMOR];
911 } else if (ppc_hash64_use_vrma(env)) {
912 /* Emulated VRMA mode */
913 slb = &vrma_slbe;
914 if (build_vrma_slbe(cpu, slb) != 0) {
915 /* Invalid VRMA setup, machine check */
916 cs->exception_index = POWERPC_EXCP_MCHECK;
917 env->error_code = 0;
918 return 1;
921 goto skip_slb_search;
922 } else {
923 target_ulong limit = rmls_limit(cpu);
925 /* Emulated old-style RMO mode, bounds check against RMLS */
926 if (raddr >= limit) {
927 if (rwx == 2) {
928 ppc_hash64_set_isi(cs, SRR1_PROTFAULT);
929 } else {
930 int dsisr = DSISR_PROTFAULT;
931 if (rwx == 1) {
932 dsisr |= DSISR_ISSTORE;
934 ppc_hash64_set_dsi(cs, eaddr, dsisr);
936 return 1;
939 raddr |= env->spr[SPR_RMOR];
941 tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
942 PAGE_READ | PAGE_WRITE | PAGE_EXEC, mmu_idx,
943 TARGET_PAGE_SIZE);
944 return 0;
947 /* 2. Translation is on, so look up the SLB */
948 slb = slb_lookup(cpu, eaddr);
949 if (!slb) {
950 /* No entry found, check if in-memory segment tables are in use */
951 if (ppc64_use_proc_tbl(cpu)) {
952 /* TODO - Unsupported */
953 error_report("Segment Table Support Unimplemented");
954 exit(1);
956 /* Segment still not found, generate the appropriate interrupt */
957 if (rwx == 2) {
958 cs->exception_index = POWERPC_EXCP_ISEG;
959 env->error_code = 0;
960 } else {
961 cs->exception_index = POWERPC_EXCP_DSEG;
962 env->error_code = 0;
963 env->spr[SPR_DAR] = eaddr;
965 return 1;
968 skip_slb_search:
970 /* 3. Check for segment level no-execute violation */
971 if ((rwx == 2) && (slb->vsid & SLB_VSID_N)) {
972 ppc_hash64_set_isi(cs, SRR1_NOEXEC_GUARD);
973 return 1;
976 /* 4. Locate the PTE in the hash table */
977 ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift);
978 if (ptex == -1) {
979 if (rwx == 2) {
980 ppc_hash64_set_isi(cs, SRR1_NOPTE);
981 } else {
982 int dsisr = DSISR_NOPTE;
983 if (rwx == 1) {
984 dsisr |= DSISR_ISSTORE;
986 ppc_hash64_set_dsi(cs, eaddr, dsisr);
988 return 1;
990 qemu_log_mask(CPU_LOG_MMU,
991 "found PTE at index %08" HWADDR_PRIx "\n", ptex);
993 /* 5. Check access permissions */
995 exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte);
996 pp_prot = ppc_hash64_pte_prot(cpu, slb, pte);
997 amr_prot = ppc_hash64_amr_prot(cpu, pte);
998 prot = exec_prot & pp_prot & amr_prot;
1000 need_prot = prot_for_access_type(rwx);
1001 if (need_prot & ~prot) {
1002 /* Access right violation */
1003 qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n");
1004 if (rwx == 2) {
1005 int srr1 = 0;
1006 if (PAGE_EXEC & ~exec_prot) {
1007 srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */
1008 } else if (PAGE_EXEC & ~pp_prot) {
1009 srr1 |= SRR1_PROTFAULT; /* Access violates access authority */
1011 if (PAGE_EXEC & ~amr_prot) {
1012 srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */
1014 ppc_hash64_set_isi(cs, srr1);
1015 } else {
1016 int dsisr = 0;
1017 if (need_prot & ~pp_prot) {
1018 dsisr |= DSISR_PROTFAULT;
1020 if (rwx == 1) {
1021 dsisr |= DSISR_ISSTORE;
1023 if (need_prot & ~amr_prot) {
1024 dsisr |= DSISR_AMR;
1026 ppc_hash64_set_dsi(cs, eaddr, dsisr);
1028 return 1;
1031 qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n");
1033 /* 6. Update PTE referenced and changed bits if necessary */
1035 if (!(pte.pte1 & HPTE64_R_R)) {
1036 ppc_hash64_set_r(cpu, ptex, pte.pte1);
1038 if (!(pte.pte1 & HPTE64_R_C)) {
1039 if (rwx == 1) {
1040 ppc_hash64_set_c(cpu, ptex, pte.pte1);
1041 } else {
1043 * Treat the page as read-only for now, so that a later write
1044 * will pass through this function again to set the C bit
1046 prot &= ~PAGE_WRITE;
1050 /* 7. Determine the real address from the PTE */
1052 raddr = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr);
1054 tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
1055 prot, mmu_idx, 1ULL << apshift);
1057 return 0;
1060 hwaddr ppc_hash64_get_phys_page_debug(PowerPCCPU *cpu, target_ulong addr)
1062 CPUPPCState *env = &cpu->env;
1063 ppc_slb_t vrma_slbe;
1064 ppc_slb_t *slb;
1065 hwaddr ptex, raddr;
1066 ppc_hash_pte64_t pte;
1067 unsigned apshift;
1069 /* Handle real mode */
1070 if (msr_dr == 0) {
1071 /* In real mode the top 4 effective address bits are ignored */
1072 raddr = addr & 0x0FFFFFFFFFFFFFFFULL;
1074 if (cpu->vhyp) {
1076 * In virtual hypervisor mode, there's nothing to do:
1077 * EA == GPA == qemu guest address
1079 return raddr;
1080 } else if ((msr_hv || !env->has_hv_mode) && !(addr >> 63)) {
1081 /* In HV mode, add HRMOR if top EA bit is clear */
1082 return raddr | env->spr[SPR_HRMOR];
1083 } else if (ppc_hash64_use_vrma(env)) {
1084 /* Emulated VRMA mode */
1085 slb = &vrma_slbe;
1086 if (build_vrma_slbe(cpu, slb) != 0) {
1087 return -1;
1089 } else {
1090 target_ulong limit = rmls_limit(cpu);
1092 /* Emulated old-style RMO mode, bounds check against RMLS */
1093 if (raddr >= limit) {
1094 return -1;
1096 return raddr | env->spr[SPR_RMOR];
1098 } else {
1099 slb = slb_lookup(cpu, addr);
1100 if (!slb) {
1101 return -1;
1105 ptex = ppc_hash64_htab_lookup(cpu, slb, addr, &pte, &apshift);
1106 if (ptex == -1) {
1107 return -1;
1110 return deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, addr)
1111 & TARGET_PAGE_MASK;
1114 void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex,
1115 target_ulong pte0, target_ulong pte1)
1118 * XXX: given the fact that there are too many segments to
1119 * invalidate, and we still don't have a tlb_flush_mask(env, n,
1120 * mask) in QEMU, we just invalidate all TLBs
1122 cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH;
1125 void helper_store_lpcr(CPUPPCState *env, target_ulong val)
1127 PowerPCCPU *cpu = env_archcpu(env);
1129 ppc_store_lpcr(cpu, val);
1132 void ppc_hash64_init(PowerPCCPU *cpu)
1134 CPUPPCState *env = &cpu->env;
1135 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
1137 if (!pcc->hash64_opts) {
1138 assert(!mmu_is_64bit(env->mmu_model));
1139 return;
1142 cpu->hash64_opts = g_memdup(pcc->hash64_opts, sizeof(*cpu->hash64_opts));
1145 void ppc_hash64_finalize(PowerPCCPU *cpu)
1147 g_free(cpu->hash64_opts);
1150 const PPCHash64Options ppc_hash64_opts_basic = {
1151 .flags = 0,
1152 .slb_size = 64,
1153 .sps = {
1154 { .page_shift = 12, /* 4K */
1155 .slb_enc = 0,
1156 .enc = { { .page_shift = 12, .pte_enc = 0 } }
1158 { .page_shift = 24, /* 16M */
1159 .slb_enc = 0x100,
1160 .enc = { { .page_shift = 24, .pte_enc = 0 } }
1165 const PPCHash64Options ppc_hash64_opts_POWER7 = {
1166 .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE,
1167 .slb_size = 32,
1168 .sps = {
1170 .page_shift = 12, /* 4K */
1171 .slb_enc = 0,
1172 .enc = { { .page_shift = 12, .pte_enc = 0 },
1173 { .page_shift = 16, .pte_enc = 0x7 },
1174 { .page_shift = 24, .pte_enc = 0x38 }, },
1177 .page_shift = 16, /* 64K */
1178 .slb_enc = SLB_VSID_64K,
1179 .enc = { { .page_shift = 16, .pte_enc = 0x1 },
1180 { .page_shift = 24, .pte_enc = 0x8 }, },
1183 .page_shift = 24, /* 16M */
1184 .slb_enc = SLB_VSID_16M,
1185 .enc = { { .page_shift = 24, .pte_enc = 0 }, },
1188 .page_shift = 34, /* 16G */
1189 .slb_enc = SLB_VSID_16G,
1190 .enc = { { .page_shift = 34, .pte_enc = 0x3 }, },