1fcf5ef2aSThomas Huth /* 2fcf5ef2aSThomas Huth * PowerPC MMU, TLB, SLB and BAT emulation helpers for QEMU. 3fcf5ef2aSThomas Huth * 4fcf5ef2aSThomas Huth * Copyright (c) 2003-2007 Jocelyn Mayer 5fcf5ef2aSThomas Huth * Copyright (c) 2013 David Gibson, IBM Corporation 6fcf5ef2aSThomas Huth * 7fcf5ef2aSThomas Huth * This library is free software; you can redistribute it and/or 8fcf5ef2aSThomas Huth * modify it under the terms of the GNU Lesser General Public 9fcf5ef2aSThomas Huth * License as published by the Free Software Foundation; either 106bd039cdSChetan Pant * version 2.1 of the License, or (at your option) any later version. 11fcf5ef2aSThomas Huth * 12fcf5ef2aSThomas Huth * This library is distributed in the hope that it will be useful, 13fcf5ef2aSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of 14fcf5ef2aSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15fcf5ef2aSThomas Huth * Lesser General Public License for more details. 16fcf5ef2aSThomas Huth * 17fcf5ef2aSThomas Huth * You should have received a copy of the GNU Lesser General Public 18fcf5ef2aSThomas Huth * License along with this library; if not, see <http://www.gnu.org/licenses/>. 19fcf5ef2aSThomas Huth */ 20fcf5ef2aSThomas Huth #include "qemu/osdep.h" 21a864a6b3SDavid Gibson #include "qemu/units.h" 22fcf5ef2aSThomas Huth #include "cpu.h" 23fcf5ef2aSThomas Huth #include "exec/exec-all.h" 24fcf5ef2aSThomas Huth #include "qemu/error-report.h" 25fad866daSMarkus Armbruster #include "qemu/qemu-print.h" 26b3946626SVincent Palatin #include "sysemu/hw_accel.h" 27fcf5ef2aSThomas Huth #include "kvm_ppc.h" 28fcf5ef2aSThomas Huth #include "mmu-hash64.h" 29fcf5ef2aSThomas Huth #include "exec/log.h" 307222b94aSDavid Gibson #include "hw/hw.h" 31182357dbSRichard Henderson #include "internal.h" 32b2899495SSuraj Jitindar Singh #include "mmu-book3s-v3.h" 33f03de3b4SRichard Henderson #include "helper_regs.h" 34fcf5ef2aSThomas Huth 352b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 362b44e219SBruno Larsen (billionai) #include "exec/helper-proto.h" 372b44e219SBruno Larsen (billionai) #endif 382b44e219SBruno Larsen (billionai) 39d75cbae8SDavid Gibson /* #define DEBUG_SLB */ 40fcf5ef2aSThomas Huth 41fcf5ef2aSThomas Huth #ifdef DEBUG_SLB 42fcf5ef2aSThomas Huth # define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__) 43fcf5ef2aSThomas Huth #else 44fcf5ef2aSThomas Huth # define LOG_SLB(...) do { } while (0) 45fcf5ef2aSThomas Huth #endif 46fcf5ef2aSThomas Huth 47fcf5ef2aSThomas Huth /* 48fcf5ef2aSThomas Huth * SLB handling 49fcf5ef2aSThomas Huth */ 50fcf5ef2aSThomas Huth 51fcf5ef2aSThomas Huth static ppc_slb_t *slb_lookup(PowerPCCPU *cpu, target_ulong eaddr) 52fcf5ef2aSThomas Huth { 53fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 54fcf5ef2aSThomas Huth uint64_t esid_256M, esid_1T; 55fcf5ef2aSThomas Huth int n; 56fcf5ef2aSThomas Huth 57fcf5ef2aSThomas Huth LOG_SLB("%s: eaddr " TARGET_FMT_lx "\n", __func__, eaddr); 58fcf5ef2aSThomas Huth 59fcf5ef2aSThomas Huth esid_256M = (eaddr & SEGMENT_MASK_256M) | SLB_ESID_V; 60fcf5ef2aSThomas Huth esid_1T = (eaddr & SEGMENT_MASK_1T) | SLB_ESID_V; 61fcf5ef2aSThomas Huth 6267d7d66fSDavid Gibson for (n = 0; n < cpu->hash64_opts->slb_size; n++) { 63fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[n]; 64fcf5ef2aSThomas Huth 65fcf5ef2aSThomas Huth LOG_SLB("%s: slot %d %016" PRIx64 " %016" 66fcf5ef2aSThomas Huth PRIx64 "\n", __func__, n, slb->esid, slb->vsid); 67d75cbae8SDavid Gibson /* 68d75cbae8SDavid Gibson * We check for 1T matches on all MMUs here - if the MMU 69fcf5ef2aSThomas Huth * doesn't have 1T segment support, we will have prevented 1T 70d75cbae8SDavid Gibson * entries from being inserted in the slbmte code. 71d75cbae8SDavid Gibson */ 72fcf5ef2aSThomas Huth if (((slb->esid == esid_256M) && 73fcf5ef2aSThomas Huth ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_256M)) 74fcf5ef2aSThomas Huth || ((slb->esid == esid_1T) && 75fcf5ef2aSThomas Huth ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_1T))) { 76fcf5ef2aSThomas Huth return slb; 77fcf5ef2aSThomas Huth } 78fcf5ef2aSThomas Huth } 79fcf5ef2aSThomas Huth 80fcf5ef2aSThomas Huth return NULL; 81fcf5ef2aSThomas Huth } 82fcf5ef2aSThomas Huth 83fad866daSMarkus Armbruster void dump_slb(PowerPCCPU *cpu) 84fcf5ef2aSThomas Huth { 85fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 86fcf5ef2aSThomas Huth int i; 87fcf5ef2aSThomas Huth uint64_t slbe, slbv; 88fcf5ef2aSThomas Huth 89fcf5ef2aSThomas Huth cpu_synchronize_state(CPU(cpu)); 90fcf5ef2aSThomas Huth 91fad866daSMarkus Armbruster qemu_printf("SLB\tESID\t\t\tVSID\n"); 9267d7d66fSDavid Gibson for (i = 0; i < cpu->hash64_opts->slb_size; i++) { 93fcf5ef2aSThomas Huth slbe = env->slb[i].esid; 94fcf5ef2aSThomas Huth slbv = env->slb[i].vsid; 95fcf5ef2aSThomas Huth if (slbe == 0 && slbv == 0) { 96fcf5ef2aSThomas Huth continue; 97fcf5ef2aSThomas Huth } 98fad866daSMarkus Armbruster qemu_printf("%d\t0x%016" PRIx64 "\t0x%016" PRIx64 "\n", 99fcf5ef2aSThomas Huth i, slbe, slbv); 100fcf5ef2aSThomas Huth } 101fcf5ef2aSThomas Huth } 102fcf5ef2aSThomas Huth 1032b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 1040418bf78SNicholas Piggin void helper_slbia(CPUPPCState *env, uint32_t ih) 105fcf5ef2aSThomas Huth { 106db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 1070418bf78SNicholas Piggin int starting_entry; 108fcf5ef2aSThomas Huth int n; 109fcf5ef2aSThomas Huth 110f9e3e1a3SNicholas Piggin /* 111f9e3e1a3SNicholas Piggin * slbia must always flush all TLB (which is equivalent to ERAT in ppc 112f9e3e1a3SNicholas Piggin * architecture). Matching on SLB_ESID_V is not good enough, because slbmte 113f9e3e1a3SNicholas Piggin * can overwrite a valid SLB without flushing its lookaside information. 114f9e3e1a3SNicholas Piggin * 115f9e3e1a3SNicholas Piggin * It would be possible to keep the TLB in synch with the SLB by flushing 116f9e3e1a3SNicholas Piggin * when a valid entry is overwritten by slbmte, and therefore slbia would 117f9e3e1a3SNicholas Piggin * not have to flush unless it evicts a valid SLB entry. However it is 118f9e3e1a3SNicholas Piggin * expected that slbmte is more common than slbia, and slbia is usually 119f9e3e1a3SNicholas Piggin * going to evict valid SLB entries, so that tradeoff is unlikely to be a 120f9e3e1a3SNicholas Piggin * good one. 1210418bf78SNicholas Piggin * 1220418bf78SNicholas Piggin * ISA v2.05 introduced IH field with values 0,1,2,6. These all invalidate 1230418bf78SNicholas Piggin * the same SLB entries (everything but entry 0), but differ in what 1240418bf78SNicholas Piggin * "lookaside information" is invalidated. TCG can ignore this and flush 1250418bf78SNicholas Piggin * everything. 1260418bf78SNicholas Piggin * 1270418bf78SNicholas Piggin * ISA v3.0 introduced additional values 3,4,7, which change what SLBs are 1280418bf78SNicholas Piggin * invalidated. 129f9e3e1a3SNicholas Piggin */ 130f9e3e1a3SNicholas Piggin 1310418bf78SNicholas Piggin env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH; 1320418bf78SNicholas Piggin 1330418bf78SNicholas Piggin starting_entry = 1; /* default for IH=0,1,2,6 */ 1340418bf78SNicholas Piggin 1350418bf78SNicholas Piggin if (env->mmu_model == POWERPC_MMU_3_00) { 1360418bf78SNicholas Piggin switch (ih) { 1370418bf78SNicholas Piggin case 0x7: 1380418bf78SNicholas Piggin /* invalidate no SLBs, but all lookaside information */ 1390418bf78SNicholas Piggin return; 1400418bf78SNicholas Piggin 1410418bf78SNicholas Piggin case 0x3: 1420418bf78SNicholas Piggin case 0x4: 1430418bf78SNicholas Piggin /* also considers SLB entry 0 */ 1440418bf78SNicholas Piggin starting_entry = 0; 1450418bf78SNicholas Piggin break; 1460418bf78SNicholas Piggin 1470418bf78SNicholas Piggin case 0x5: 1480418bf78SNicholas Piggin /* treat undefined values as ih==0, and warn */ 1490418bf78SNicholas Piggin qemu_log_mask(LOG_GUEST_ERROR, 1500418bf78SNicholas Piggin "slbia undefined IH field %u.\n", ih); 1510418bf78SNicholas Piggin break; 1520418bf78SNicholas Piggin 1530418bf78SNicholas Piggin default: 1540418bf78SNicholas Piggin /* 0,1,2,6 */ 1550418bf78SNicholas Piggin break; 1560418bf78SNicholas Piggin } 1570418bf78SNicholas Piggin } 1580418bf78SNicholas Piggin 1590418bf78SNicholas Piggin for (n = starting_entry; n < cpu->hash64_opts->slb_size; n++) { 160fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[n]; 161fcf5ef2aSThomas Huth 1620418bf78SNicholas Piggin if (!(slb->esid & SLB_ESID_V)) { 1630418bf78SNicholas Piggin continue; 1640418bf78SNicholas Piggin } 1650418bf78SNicholas Piggin if (env->mmu_model == POWERPC_MMU_3_00) { 1660418bf78SNicholas Piggin if (ih == 0x3 && (slb->vsid & SLB_VSID_C) == 0) { 1670418bf78SNicholas Piggin /* preserves entries with a class value of 0 */ 1680418bf78SNicholas Piggin continue; 169f9e3e1a3SNicholas Piggin } 170f9e3e1a3SNicholas Piggin } 171f9e3e1a3SNicholas Piggin 1720418bf78SNicholas Piggin slb->esid &= ~SLB_ESID_V; 1730418bf78SNicholas Piggin } 174fcf5ef2aSThomas Huth } 175fcf5ef2aSThomas Huth 176a63f1dfcSNikunj A Dadhania static void __helper_slbie(CPUPPCState *env, target_ulong addr, 177a63f1dfcSNikunj A Dadhania target_ulong global) 178fcf5ef2aSThomas Huth { 179db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 180fcf5ef2aSThomas Huth ppc_slb_t *slb; 181fcf5ef2aSThomas Huth 182fcf5ef2aSThomas Huth slb = slb_lookup(cpu, addr); 183fcf5ef2aSThomas Huth if (!slb) { 184fcf5ef2aSThomas Huth return; 185fcf5ef2aSThomas Huth } 186fcf5ef2aSThomas Huth 187fcf5ef2aSThomas Huth if (slb->esid & SLB_ESID_V) { 188fcf5ef2aSThomas Huth slb->esid &= ~SLB_ESID_V; 189fcf5ef2aSThomas Huth 190d75cbae8SDavid Gibson /* 191d75cbae8SDavid Gibson * XXX: given the fact that segment size is 256 MB or 1TB, 192fcf5ef2aSThomas Huth * and we still don't have a tlb_flush_mask(env, n, mask) 193fcf5ef2aSThomas Huth * in QEMU, we just invalidate all TLBs 194fcf5ef2aSThomas Huth */ 195a63f1dfcSNikunj A Dadhania env->tlb_need_flush |= 196a63f1dfcSNikunj A Dadhania (global == false ? TLB_NEED_LOCAL_FLUSH : TLB_NEED_GLOBAL_FLUSH); 197fcf5ef2aSThomas Huth } 198fcf5ef2aSThomas Huth } 199fcf5ef2aSThomas Huth 200a63f1dfcSNikunj A Dadhania void helper_slbie(CPUPPCState *env, target_ulong addr) 201a63f1dfcSNikunj A Dadhania { 202a63f1dfcSNikunj A Dadhania __helper_slbie(env, addr, false); 203a63f1dfcSNikunj A Dadhania } 204a63f1dfcSNikunj A Dadhania 205a63f1dfcSNikunj A Dadhania void helper_slbieg(CPUPPCState *env, target_ulong addr) 206a63f1dfcSNikunj A Dadhania { 207a63f1dfcSNikunj A Dadhania __helper_slbie(env, addr, true); 208a63f1dfcSNikunj A Dadhania } 2092b44e219SBruno Larsen (billionai) #endif 210a63f1dfcSNikunj A Dadhania 211fcf5ef2aSThomas Huth int ppc_store_slb(PowerPCCPU *cpu, target_ulong slot, 212fcf5ef2aSThomas Huth target_ulong esid, target_ulong vsid) 213fcf5ef2aSThomas Huth { 214fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 215fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 216b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = NULL; 217fcf5ef2aSThomas Huth int i; 218fcf5ef2aSThomas Huth 21967d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 220fcf5ef2aSThomas Huth return -1; /* Bad slot number */ 221fcf5ef2aSThomas Huth } 222fcf5ef2aSThomas Huth if (esid & ~(SLB_ESID_ESID | SLB_ESID_V)) { 223fcf5ef2aSThomas Huth return -1; /* Reserved bits set */ 224fcf5ef2aSThomas Huth } 225fcf5ef2aSThomas Huth if (vsid & (SLB_VSID_B & ~SLB_VSID_B_1T)) { 226fcf5ef2aSThomas Huth return -1; /* Bad segment size */ 227fcf5ef2aSThomas Huth } 22858969eeeSDavid Gibson if ((vsid & SLB_VSID_B) && !(ppc_hash64_has(cpu, PPC_HASH64_1TSEG))) { 229fcf5ef2aSThomas Huth return -1; /* 1T segment on MMU that doesn't support it */ 230fcf5ef2aSThomas Huth } 231fcf5ef2aSThomas Huth 232fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 233b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i]; 234fcf5ef2aSThomas Huth 235fcf5ef2aSThomas Huth if (!sps1->page_shift) { 236fcf5ef2aSThomas Huth break; 237fcf5ef2aSThomas Huth } 238fcf5ef2aSThomas Huth 239fcf5ef2aSThomas Huth if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) { 240fcf5ef2aSThomas Huth sps = sps1; 241fcf5ef2aSThomas Huth break; 242fcf5ef2aSThomas Huth } 243fcf5ef2aSThomas Huth } 244fcf5ef2aSThomas Huth 245fcf5ef2aSThomas Huth if (!sps) { 246fcf5ef2aSThomas Huth error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu 247fcf5ef2aSThomas Huth " esid 0x"TARGET_FMT_lx" vsid 0x"TARGET_FMT_lx, 248fcf5ef2aSThomas Huth slot, esid, vsid); 249fcf5ef2aSThomas Huth return -1; 250fcf5ef2aSThomas Huth } 251fcf5ef2aSThomas Huth 252fcf5ef2aSThomas Huth slb->esid = esid; 253fcf5ef2aSThomas Huth slb->vsid = vsid; 254fcf5ef2aSThomas Huth slb->sps = sps; 255fcf5ef2aSThomas Huth 25676134d48SSuraj Jitindar Singh LOG_SLB("%s: " TARGET_FMT_lu " " TARGET_FMT_lx " - " TARGET_FMT_lx 25776134d48SSuraj Jitindar Singh " => %016" PRIx64 " %016" PRIx64 "\n", __func__, slot, esid, vsid, 258fcf5ef2aSThomas Huth slb->esid, slb->vsid); 259fcf5ef2aSThomas Huth 260fcf5ef2aSThomas Huth return 0; 261fcf5ef2aSThomas Huth } 262fcf5ef2aSThomas Huth 2632b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 264fcf5ef2aSThomas Huth static int ppc_load_slb_esid(PowerPCCPU *cpu, target_ulong rb, 265fcf5ef2aSThomas Huth target_ulong *rt) 266fcf5ef2aSThomas Huth { 267fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 268fcf5ef2aSThomas Huth int slot = rb & 0xfff; 269fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 270fcf5ef2aSThomas Huth 27167d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 272fcf5ef2aSThomas Huth return -1; 273fcf5ef2aSThomas Huth } 274fcf5ef2aSThomas Huth 275fcf5ef2aSThomas Huth *rt = slb->esid; 276fcf5ef2aSThomas Huth return 0; 277fcf5ef2aSThomas Huth } 278fcf5ef2aSThomas Huth 279fcf5ef2aSThomas Huth static int ppc_load_slb_vsid(PowerPCCPU *cpu, target_ulong rb, 280fcf5ef2aSThomas Huth target_ulong *rt) 281fcf5ef2aSThomas Huth { 282fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 283fcf5ef2aSThomas Huth int slot = rb & 0xfff; 284fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 285fcf5ef2aSThomas Huth 28667d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 287fcf5ef2aSThomas Huth return -1; 288fcf5ef2aSThomas Huth } 289fcf5ef2aSThomas Huth 290fcf5ef2aSThomas Huth *rt = slb->vsid; 291fcf5ef2aSThomas Huth return 0; 292fcf5ef2aSThomas Huth } 293fcf5ef2aSThomas Huth 294fcf5ef2aSThomas Huth static int ppc_find_slb_vsid(PowerPCCPU *cpu, target_ulong rb, 295fcf5ef2aSThomas Huth target_ulong *rt) 296fcf5ef2aSThomas Huth { 297fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 298fcf5ef2aSThomas Huth ppc_slb_t *slb; 299fcf5ef2aSThomas Huth 300fcf5ef2aSThomas Huth if (!msr_is_64bit(env, env->msr)) { 301fcf5ef2aSThomas Huth rb &= 0xffffffff; 302fcf5ef2aSThomas Huth } 303fcf5ef2aSThomas Huth slb = slb_lookup(cpu, rb); 304fcf5ef2aSThomas Huth if (slb == NULL) { 305fcf5ef2aSThomas Huth *rt = (target_ulong)-1ul; 306fcf5ef2aSThomas Huth } else { 307fcf5ef2aSThomas Huth *rt = slb->vsid; 308fcf5ef2aSThomas Huth } 309fcf5ef2aSThomas Huth return 0; 310fcf5ef2aSThomas Huth } 311fcf5ef2aSThomas Huth 312fcf5ef2aSThomas Huth void helper_store_slb(CPUPPCState *env, target_ulong rb, target_ulong rs) 313fcf5ef2aSThomas Huth { 314db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 315fcf5ef2aSThomas Huth 316fcf5ef2aSThomas Huth if (ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs) < 0) { 317fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 318fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 319fcf5ef2aSThomas Huth } 320fcf5ef2aSThomas Huth } 321fcf5ef2aSThomas Huth 322fcf5ef2aSThomas Huth target_ulong helper_load_slb_esid(CPUPPCState *env, target_ulong rb) 323fcf5ef2aSThomas Huth { 324db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 325fcf5ef2aSThomas Huth target_ulong rt = 0; 326fcf5ef2aSThomas Huth 327fcf5ef2aSThomas Huth if (ppc_load_slb_esid(cpu, rb, &rt) < 0) { 328fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 329fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 330fcf5ef2aSThomas Huth } 331fcf5ef2aSThomas Huth return rt; 332fcf5ef2aSThomas Huth } 333fcf5ef2aSThomas Huth 334fcf5ef2aSThomas Huth target_ulong helper_find_slb_vsid(CPUPPCState *env, target_ulong rb) 335fcf5ef2aSThomas Huth { 336db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 337fcf5ef2aSThomas Huth target_ulong rt = 0; 338fcf5ef2aSThomas Huth 339fcf5ef2aSThomas Huth if (ppc_find_slb_vsid(cpu, rb, &rt) < 0) { 340fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 341fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 342fcf5ef2aSThomas Huth } 343fcf5ef2aSThomas Huth return rt; 344fcf5ef2aSThomas Huth } 345fcf5ef2aSThomas Huth 346fcf5ef2aSThomas Huth target_ulong helper_load_slb_vsid(CPUPPCState *env, target_ulong rb) 347fcf5ef2aSThomas Huth { 348db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 349fcf5ef2aSThomas Huth target_ulong rt = 0; 350fcf5ef2aSThomas Huth 351fcf5ef2aSThomas Huth if (ppc_load_slb_vsid(cpu, rb, &rt) < 0) { 352fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 353fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 354fcf5ef2aSThomas Huth } 355fcf5ef2aSThomas Huth return rt; 356fcf5ef2aSThomas Huth } 3572b44e219SBruno Larsen (billionai) #endif 358fcf5ef2aSThomas Huth 35907a68f99SSuraj Jitindar Singh /* Check No-Execute or Guarded Storage */ 36007a68f99SSuraj Jitindar Singh static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU *cpu, 36107a68f99SSuraj Jitindar Singh ppc_hash_pte64_t pte) 36207a68f99SSuraj Jitindar Singh { 36307a68f99SSuraj Jitindar Singh /* Exec permissions CANNOT take away read or write permissions */ 36407a68f99SSuraj Jitindar Singh return (pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G) ? 36507a68f99SSuraj Jitindar Singh PAGE_READ | PAGE_WRITE : PAGE_READ | PAGE_WRITE | PAGE_EXEC; 36607a68f99SSuraj Jitindar Singh } 36707a68f99SSuraj Jitindar Singh 36807a68f99SSuraj Jitindar Singh /* Check Basic Storage Protection */ 369fcf5ef2aSThomas Huth static int ppc_hash64_pte_prot(PowerPCCPU *cpu, 370fcf5ef2aSThomas Huth ppc_slb_t *slb, ppc_hash_pte64_t pte) 371fcf5ef2aSThomas Huth { 372fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 373fcf5ef2aSThomas Huth unsigned pp, key; 374d75cbae8SDavid Gibson /* 375d75cbae8SDavid Gibson * Some pp bit combinations have undefined behaviour, so default 376d75cbae8SDavid Gibson * to no access in those cases 377d75cbae8SDavid Gibson */ 378fcf5ef2aSThomas Huth int prot = 0; 379fcf5ef2aSThomas Huth 380fcf5ef2aSThomas Huth key = !!(msr_pr ? (slb->vsid & SLB_VSID_KP) 381fcf5ef2aSThomas Huth : (slb->vsid & SLB_VSID_KS)); 382fcf5ef2aSThomas Huth pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61); 383fcf5ef2aSThomas Huth 384fcf5ef2aSThomas Huth if (key == 0) { 385fcf5ef2aSThomas Huth switch (pp) { 386fcf5ef2aSThomas Huth case 0x0: 387fcf5ef2aSThomas Huth case 0x1: 388fcf5ef2aSThomas Huth case 0x2: 389347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 390fcf5ef2aSThomas Huth break; 391fcf5ef2aSThomas Huth 392fcf5ef2aSThomas Huth case 0x3: 393fcf5ef2aSThomas Huth case 0x6: 394347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_EXEC; 395fcf5ef2aSThomas Huth break; 396fcf5ef2aSThomas Huth } 397fcf5ef2aSThomas Huth } else { 398fcf5ef2aSThomas Huth switch (pp) { 399fcf5ef2aSThomas Huth case 0x0: 400fcf5ef2aSThomas Huth case 0x6: 401fcf5ef2aSThomas Huth break; 402fcf5ef2aSThomas Huth 403fcf5ef2aSThomas Huth case 0x1: 404fcf5ef2aSThomas Huth case 0x3: 405347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_EXEC; 406fcf5ef2aSThomas Huth break; 407fcf5ef2aSThomas Huth 408fcf5ef2aSThomas Huth case 0x2: 409347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 410fcf5ef2aSThomas Huth break; 411fcf5ef2aSThomas Huth } 412fcf5ef2aSThomas Huth } 413fcf5ef2aSThomas Huth 414fcf5ef2aSThomas Huth return prot; 415fcf5ef2aSThomas Huth } 416fcf5ef2aSThomas Huth 417a6152b52SSuraj Jitindar Singh /* Check the instruction access permissions specified in the IAMR */ 418a6152b52SSuraj Jitindar Singh static int ppc_hash64_iamr_prot(PowerPCCPU *cpu, int key) 419a6152b52SSuraj Jitindar Singh { 420a6152b52SSuraj Jitindar Singh CPUPPCState *env = &cpu->env; 421a6152b52SSuraj Jitindar Singh int iamr_bits = (env->spr[SPR_IAMR] >> 2 * (31 - key)) & 0x3; 422a6152b52SSuraj Jitindar Singh 423a6152b52SSuraj Jitindar Singh /* 424a6152b52SSuraj Jitindar Singh * An instruction fetch is permitted if the IAMR bit is 0. 425a6152b52SSuraj Jitindar Singh * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit 426a6152b52SSuraj Jitindar Singh * can only take away EXEC permissions not READ or WRITE permissions. 427a6152b52SSuraj Jitindar Singh * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since 428a6152b52SSuraj Jitindar Singh * EXEC permissions are allowed. 429a6152b52SSuraj Jitindar Singh */ 430a6152b52SSuraj Jitindar Singh return (iamr_bits & 0x1) ? PAGE_READ | PAGE_WRITE : 431a6152b52SSuraj Jitindar Singh PAGE_READ | PAGE_WRITE | PAGE_EXEC; 432a6152b52SSuraj Jitindar Singh } 433a6152b52SSuraj Jitindar Singh 434fcf5ef2aSThomas Huth static int ppc_hash64_amr_prot(PowerPCCPU *cpu, ppc_hash_pte64_t pte) 435fcf5ef2aSThomas Huth { 436fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 437fcf5ef2aSThomas Huth int key, amrbits; 438fcf5ef2aSThomas Huth int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 439fcf5ef2aSThomas Huth 440fcf5ef2aSThomas Huth /* Only recent MMUs implement Virtual Page Class Key Protection */ 44158969eeeSDavid Gibson if (!ppc_hash64_has(cpu, PPC_HASH64_AMR)) { 442fcf5ef2aSThomas Huth return prot; 443fcf5ef2aSThomas Huth } 444fcf5ef2aSThomas Huth 445fcf5ef2aSThomas Huth key = HPTE64_R_KEY(pte.pte1); 446fcf5ef2aSThomas Huth amrbits = (env->spr[SPR_AMR] >> 2 * (31 - key)) & 0x3; 447fcf5ef2aSThomas Huth 448fcf5ef2aSThomas Huth /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */ 449fcf5ef2aSThomas Huth /* env->spr[SPR_AMR]); */ 450fcf5ef2aSThomas Huth 451fcf5ef2aSThomas Huth /* 452fcf5ef2aSThomas Huth * A store is permitted if the AMR bit is 0. Remove write 453fcf5ef2aSThomas Huth * protection if it is set. 454fcf5ef2aSThomas Huth */ 455fcf5ef2aSThomas Huth if (amrbits & 0x2) { 456fcf5ef2aSThomas Huth prot &= ~PAGE_WRITE; 457fcf5ef2aSThomas Huth } 458fcf5ef2aSThomas Huth /* 459fcf5ef2aSThomas Huth * A load is permitted if the AMR bit is 0. Remove read 460fcf5ef2aSThomas Huth * protection if it is set. 461fcf5ef2aSThomas Huth */ 462fcf5ef2aSThomas Huth if (amrbits & 0x1) { 463fcf5ef2aSThomas Huth prot &= ~PAGE_READ; 464fcf5ef2aSThomas Huth } 465fcf5ef2aSThomas Huth 466a6152b52SSuraj Jitindar Singh switch (env->mmu_model) { 467a6152b52SSuraj Jitindar Singh /* 468a6152b52SSuraj Jitindar Singh * MMU version 2.07 and later support IAMR 469a6152b52SSuraj Jitindar Singh * Check if the IAMR allows the instruction access - it will return 470a6152b52SSuraj Jitindar Singh * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0 471a6152b52SSuraj Jitindar Singh * if it does (and prot will be unchanged indicating execution support). 472a6152b52SSuraj Jitindar Singh */ 473a6152b52SSuraj Jitindar Singh case POWERPC_MMU_2_07: 474a6152b52SSuraj Jitindar Singh case POWERPC_MMU_3_00: 475a6152b52SSuraj Jitindar Singh prot &= ppc_hash64_iamr_prot(cpu, key); 476a6152b52SSuraj Jitindar Singh break; 477a6152b52SSuraj Jitindar Singh default: 478a6152b52SSuraj Jitindar Singh break; 479a6152b52SSuraj Jitindar Singh } 480a6152b52SSuraj Jitindar Singh 481fcf5ef2aSThomas Huth return prot; 482fcf5ef2aSThomas Huth } 483fcf5ef2aSThomas Huth 4847222b94aSDavid Gibson const ppc_hash_pte64_t *ppc_hash64_map_hptes(PowerPCCPU *cpu, 4857222b94aSDavid Gibson hwaddr ptex, int n) 486fcf5ef2aSThomas Huth { 4877222b94aSDavid Gibson hwaddr pte_offset = ptex * HASH_PTE_SIZE_64; 4883367c62fSBenjamin Herrenschmidt hwaddr base; 4897222b94aSDavid Gibson hwaddr plen = n * HASH_PTE_SIZE_64; 490e57ca75cSDavid Gibson const ppc_hash_pte64_t *hptes; 491e57ca75cSDavid Gibson 492e57ca75cSDavid Gibson if (cpu->vhyp) { 493e57ca75cSDavid Gibson PPCVirtualHypervisorClass *vhc = 494e57ca75cSDavid Gibson PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 495e57ca75cSDavid Gibson return vhc->map_hptes(cpu->vhyp, ptex, n); 496e57ca75cSDavid Gibson } 4973367c62fSBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 498e57ca75cSDavid Gibson 499e57ca75cSDavid Gibson if (!base) { 500e57ca75cSDavid Gibson return NULL; 501e57ca75cSDavid Gibson } 502e57ca75cSDavid Gibson 503f26404fbSPeter Maydell hptes = address_space_map(CPU(cpu)->as, base + pte_offset, &plen, false, 504f26404fbSPeter Maydell MEMTXATTRS_UNSPECIFIED); 5057222b94aSDavid Gibson if (plen < (n * HASH_PTE_SIZE_64)) { 5067222b94aSDavid Gibson hw_error("%s: Unable to map all requested HPTEs\n", __func__); 507fcf5ef2aSThomas Huth } 5087222b94aSDavid Gibson return hptes; 509fcf5ef2aSThomas Huth } 510fcf5ef2aSThomas Huth 5117222b94aSDavid Gibson void ppc_hash64_unmap_hptes(PowerPCCPU *cpu, const ppc_hash_pte64_t *hptes, 5127222b94aSDavid Gibson hwaddr ptex, int n) 513fcf5ef2aSThomas Huth { 514e57ca75cSDavid Gibson if (cpu->vhyp) { 515e57ca75cSDavid Gibson PPCVirtualHypervisorClass *vhc = 516e57ca75cSDavid Gibson PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 517e57ca75cSDavid Gibson vhc->unmap_hptes(cpu->vhyp, hptes, ptex, n); 518e57ca75cSDavid Gibson return; 519e57ca75cSDavid Gibson } 520e57ca75cSDavid Gibson 5217222b94aSDavid Gibson address_space_unmap(CPU(cpu)->as, (void *)hptes, n * HASH_PTE_SIZE_64, 5227222b94aSDavid Gibson false, n * HASH_PTE_SIZE_64); 523fcf5ef2aSThomas Huth } 524fcf5ef2aSThomas Huth 525b07c59f7SDavid Gibson static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes *sps, 526fcf5ef2aSThomas Huth uint64_t pte0, uint64_t pte1) 527fcf5ef2aSThomas Huth { 528fcf5ef2aSThomas Huth int i; 529fcf5ef2aSThomas Huth 530fcf5ef2aSThomas Huth if (!(pte0 & HPTE64_V_LARGE)) { 531fcf5ef2aSThomas Huth if (sps->page_shift != 12) { 532fcf5ef2aSThomas Huth /* 4kiB page in a non 4kiB segment */ 533fcf5ef2aSThomas Huth return 0; 534fcf5ef2aSThomas Huth } 535fcf5ef2aSThomas Huth /* Normal 4kiB page */ 536fcf5ef2aSThomas Huth return 12; 537fcf5ef2aSThomas Huth } 538fcf5ef2aSThomas Huth 539fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 540b07c59f7SDavid Gibson const PPCHash64PageSize *ps = &sps->enc[i]; 541fcf5ef2aSThomas Huth uint64_t mask; 542fcf5ef2aSThomas Huth 543fcf5ef2aSThomas Huth if (!ps->page_shift) { 544fcf5ef2aSThomas Huth break; 545fcf5ef2aSThomas Huth } 546fcf5ef2aSThomas Huth 547fcf5ef2aSThomas Huth if (ps->page_shift == 12) { 548fcf5ef2aSThomas Huth /* L bit is set so this can't be a 4kiB page */ 549fcf5ef2aSThomas Huth continue; 550fcf5ef2aSThomas Huth } 551fcf5ef2aSThomas Huth 552fcf5ef2aSThomas Huth mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN; 553fcf5ef2aSThomas Huth 554fcf5ef2aSThomas Huth if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) { 555fcf5ef2aSThomas Huth return ps->page_shift; 556fcf5ef2aSThomas Huth } 557fcf5ef2aSThomas Huth } 558fcf5ef2aSThomas Huth 559fcf5ef2aSThomas Huth return 0; /* Bad page size encoding */ 560fcf5ef2aSThomas Huth } 561fcf5ef2aSThomas Huth 56234525595SBenjamin Herrenschmidt static void ppc64_v3_new_to_old_hpte(target_ulong *pte0, target_ulong *pte1) 56334525595SBenjamin Herrenschmidt { 56434525595SBenjamin Herrenschmidt /* Insert B into pte0 */ 56534525595SBenjamin Herrenschmidt *pte0 = (*pte0 & HPTE64_V_COMMON_BITS) | 56634525595SBenjamin Herrenschmidt ((*pte1 & HPTE64_R_3_0_SSIZE_MASK) << 56734525595SBenjamin Herrenschmidt (HPTE64_V_SSIZE_SHIFT - HPTE64_R_3_0_SSIZE_SHIFT)); 56834525595SBenjamin Herrenschmidt 56934525595SBenjamin Herrenschmidt /* Remove B from pte1 */ 57034525595SBenjamin Herrenschmidt *pte1 = *pte1 & ~HPTE64_R_3_0_SSIZE_MASK; 57134525595SBenjamin Herrenschmidt } 57234525595SBenjamin Herrenschmidt 57334525595SBenjamin Herrenschmidt 574fcf5ef2aSThomas Huth static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash, 575b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps, 576fcf5ef2aSThomas Huth target_ulong ptem, 577fcf5ef2aSThomas Huth ppc_hash_pte64_t *pte, unsigned *pshift) 578fcf5ef2aSThomas Huth { 579fcf5ef2aSThomas Huth int i; 5807222b94aSDavid Gibson const ppc_hash_pte64_t *pteg; 581fcf5ef2aSThomas Huth target_ulong pte0, pte1; 5827222b94aSDavid Gibson target_ulong ptex; 583fcf5ef2aSThomas Huth 58436778660SDavid Gibson ptex = (hash & ppc_hash64_hpt_mask(cpu)) * HPTES_PER_GROUP; 5857222b94aSDavid Gibson pteg = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP); 5867222b94aSDavid Gibson if (!pteg) { 587fcf5ef2aSThomas Huth return -1; 588fcf5ef2aSThomas Huth } 589fcf5ef2aSThomas Huth for (i = 0; i < HPTES_PER_GROUP; i++) { 5907222b94aSDavid Gibson pte0 = ppc_hash64_hpte0(cpu, pteg, i); 5913054b0caSBenjamin Herrenschmidt /* 5923054b0caSBenjamin Herrenschmidt * pte0 contains the valid bit and must be read before pte1, 5933054b0caSBenjamin Herrenschmidt * otherwise we might see an old pte1 with a new valid bit and 5943054b0caSBenjamin Herrenschmidt * thus an inconsistent hpte value 5953054b0caSBenjamin Herrenschmidt */ 5963054b0caSBenjamin Herrenschmidt smp_rmb(); 5977222b94aSDavid Gibson pte1 = ppc_hash64_hpte1(cpu, pteg, i); 598fcf5ef2aSThomas Huth 59934525595SBenjamin Herrenschmidt /* Convert format if necessary */ 60034525595SBenjamin Herrenschmidt if (cpu->env.mmu_model == POWERPC_MMU_3_00 && !cpu->vhyp) { 60134525595SBenjamin Herrenschmidt ppc64_v3_new_to_old_hpte(&pte0, &pte1); 60234525595SBenjamin Herrenschmidt } 60334525595SBenjamin Herrenschmidt 604fcf5ef2aSThomas Huth /* This compares V, B, H (secondary) and the AVPN */ 605fcf5ef2aSThomas Huth if (HPTE64_V_COMPARE(pte0, ptem)) { 606fcf5ef2aSThomas Huth *pshift = hpte_page_shift(sps, pte0, pte1); 607fcf5ef2aSThomas Huth /* 608fcf5ef2aSThomas Huth * If there is no match, ignore the PTE, it could simply 609fcf5ef2aSThomas Huth * be for a different segment size encoding and the 610fcf5ef2aSThomas Huth * architecture specifies we should not match. Linux will 611fcf5ef2aSThomas Huth * potentially leave behind PTEs for the wrong base page 612fcf5ef2aSThomas Huth * size when demoting segments. 613fcf5ef2aSThomas Huth */ 614fcf5ef2aSThomas Huth if (*pshift == 0) { 615fcf5ef2aSThomas Huth continue; 616fcf5ef2aSThomas Huth } 617d75cbae8SDavid Gibson /* 618d75cbae8SDavid Gibson * We don't do anything with pshift yet as qemu TLB only 619d75cbae8SDavid Gibson * deals with 4K pages anyway 620fcf5ef2aSThomas Huth */ 621fcf5ef2aSThomas Huth pte->pte0 = pte0; 622fcf5ef2aSThomas Huth pte->pte1 = pte1; 6237222b94aSDavid Gibson ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP); 6247222b94aSDavid Gibson return ptex + i; 625fcf5ef2aSThomas Huth } 626fcf5ef2aSThomas Huth } 6277222b94aSDavid Gibson ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP); 628fcf5ef2aSThomas Huth /* 629fcf5ef2aSThomas Huth * We didn't find a valid entry. 630fcf5ef2aSThomas Huth */ 631fcf5ef2aSThomas Huth return -1; 632fcf5ef2aSThomas Huth } 633fcf5ef2aSThomas Huth 634fcf5ef2aSThomas Huth static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu, 635fcf5ef2aSThomas Huth ppc_slb_t *slb, target_ulong eaddr, 636fcf5ef2aSThomas Huth ppc_hash_pte64_t *pte, unsigned *pshift) 637fcf5ef2aSThomas Huth { 638fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 6397222b94aSDavid Gibson hwaddr hash, ptex; 640fcf5ef2aSThomas Huth uint64_t vsid, epnmask, epn, ptem; 641b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = slb->sps; 642fcf5ef2aSThomas Huth 643d75cbae8SDavid Gibson /* 644d75cbae8SDavid Gibson * The SLB store path should prevent any bad page size encodings 645d75cbae8SDavid Gibson * getting in there, so: 646d75cbae8SDavid Gibson */ 647fcf5ef2aSThomas Huth assert(sps); 648fcf5ef2aSThomas Huth 649fcf5ef2aSThomas Huth /* If ISL is set in LPCR we need to clamp the page size to 4K */ 650fcf5ef2aSThomas Huth if (env->spr[SPR_LPCR] & LPCR_ISL) { 651fcf5ef2aSThomas Huth /* We assume that when using TCG, 4k is first entry of SPS */ 652b07c59f7SDavid Gibson sps = &cpu->hash64_opts->sps[0]; 653fcf5ef2aSThomas Huth assert(sps->page_shift == 12); 654fcf5ef2aSThomas Huth } 655fcf5ef2aSThomas Huth 656fcf5ef2aSThomas Huth epnmask = ~((1ULL << sps->page_shift) - 1); 657fcf5ef2aSThomas Huth 658fcf5ef2aSThomas Huth if (slb->vsid & SLB_VSID_B) { 659fcf5ef2aSThomas Huth /* 1TB segment */ 660fcf5ef2aSThomas Huth vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T; 661fcf5ef2aSThomas Huth epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask; 662fcf5ef2aSThomas Huth hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift); 663fcf5ef2aSThomas Huth } else { 664fcf5ef2aSThomas Huth /* 256M segment */ 665fcf5ef2aSThomas Huth vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT; 666fcf5ef2aSThomas Huth epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask; 667fcf5ef2aSThomas Huth hash = vsid ^ (epn >> sps->page_shift); 668fcf5ef2aSThomas Huth } 669fcf5ef2aSThomas Huth ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN); 670fcf5ef2aSThomas Huth ptem |= HPTE64_V_VALID; 671fcf5ef2aSThomas Huth 672fcf5ef2aSThomas Huth /* Page address translation */ 673fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 674fcf5ef2aSThomas Huth "htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx 675fcf5ef2aSThomas Huth " hash " TARGET_FMT_plx "\n", 67636778660SDavid Gibson ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), hash); 677fcf5ef2aSThomas Huth 678fcf5ef2aSThomas Huth /* Primary PTEG lookup */ 679fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 680fcf5ef2aSThomas Huth "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx 681fcf5ef2aSThomas Huth " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx 682fcf5ef2aSThomas Huth " hash=" TARGET_FMT_plx "\n", 68336778660SDavid Gibson ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), 68436778660SDavid Gibson vsid, ptem, hash); 6857222b94aSDavid Gibson ptex = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift); 686fcf5ef2aSThomas Huth 6877222b94aSDavid Gibson if (ptex == -1) { 688fcf5ef2aSThomas Huth /* Secondary PTEG lookup */ 689fcf5ef2aSThomas Huth ptem |= HPTE64_V_SECONDARY; 690fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 691fcf5ef2aSThomas Huth "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx 692fcf5ef2aSThomas Huth " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx 69336778660SDavid Gibson " hash=" TARGET_FMT_plx "\n", ppc_hash64_hpt_base(cpu), 69436778660SDavid Gibson ppc_hash64_hpt_mask(cpu), vsid, ptem, ~hash); 695fcf5ef2aSThomas Huth 6967222b94aSDavid Gibson ptex = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift); 697fcf5ef2aSThomas Huth } 698fcf5ef2aSThomas Huth 6997222b94aSDavid Gibson return ptex; 700fcf5ef2aSThomas Huth } 701fcf5ef2aSThomas Huth 702fcf5ef2aSThomas Huth unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu, 703fcf5ef2aSThomas Huth uint64_t pte0, uint64_t pte1) 704fcf5ef2aSThomas Huth { 705fcf5ef2aSThomas Huth int i; 706fcf5ef2aSThomas Huth 707fcf5ef2aSThomas Huth if (!(pte0 & HPTE64_V_LARGE)) { 708fcf5ef2aSThomas Huth return 12; 709fcf5ef2aSThomas Huth } 710fcf5ef2aSThomas Huth 711fcf5ef2aSThomas Huth /* 712fcf5ef2aSThomas Huth * The encodings in env->sps need to be carefully chosen so that 713fcf5ef2aSThomas Huth * this gives an unambiguous result. 714fcf5ef2aSThomas Huth */ 715fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 716b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i]; 717fcf5ef2aSThomas Huth unsigned shift; 718fcf5ef2aSThomas Huth 719fcf5ef2aSThomas Huth if (!sps->page_shift) { 720fcf5ef2aSThomas Huth break; 721fcf5ef2aSThomas Huth } 722fcf5ef2aSThomas Huth 723fcf5ef2aSThomas Huth shift = hpte_page_shift(sps, pte0, pte1); 724fcf5ef2aSThomas Huth if (shift) { 725fcf5ef2aSThomas Huth return shift; 726fcf5ef2aSThomas Huth } 727fcf5ef2aSThomas Huth } 728fcf5ef2aSThomas Huth 729fcf5ef2aSThomas Huth return 0; 730fcf5ef2aSThomas Huth } 731fcf5ef2aSThomas Huth 7321b99e029SDavid Gibson static bool ppc_hash64_use_vrma(CPUPPCState *env) 7331b99e029SDavid Gibson { 7341b99e029SDavid Gibson switch (env->mmu_model) { 7351b99e029SDavid Gibson case POWERPC_MMU_3_00: 7361b99e029SDavid Gibson /* 7371b99e029SDavid Gibson * ISAv3.0 (POWER9) always uses VRMA, the VPM0 field and RMOR 7381b99e029SDavid Gibson * register no longer exist 7391b99e029SDavid Gibson */ 7401b99e029SDavid Gibson return true; 7411b99e029SDavid Gibson 7421b99e029SDavid Gibson default: 7431b99e029SDavid Gibson return !!(env->spr[SPR_LPCR] & LPCR_VPM0); 7441b99e029SDavid Gibson } 7451b99e029SDavid Gibson } 7461b99e029SDavid Gibson 7478fe08facSDavid Gibson static void ppc_hash64_set_isi(CPUState *cs, uint64_t error_code) 748fcf5ef2aSThomas Huth { 7498fe08facSDavid Gibson CPUPPCState *env = &POWERPC_CPU(cs)->env; 750fcf5ef2aSThomas Huth bool vpm; 751fcf5ef2aSThomas Huth 752fcf5ef2aSThomas Huth if (msr_ir) { 753fcf5ef2aSThomas Huth vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1); 754fcf5ef2aSThomas Huth } else { 7551b99e029SDavid Gibson vpm = ppc_hash64_use_vrma(env); 756fcf5ef2aSThomas Huth } 757fcf5ef2aSThomas Huth if (vpm && !msr_hv) { 758fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_HISI; 759fcf5ef2aSThomas Huth } else { 760fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_ISI; 761fcf5ef2aSThomas Huth } 762fcf5ef2aSThomas Huth env->error_code = error_code; 763fcf5ef2aSThomas Huth } 764fcf5ef2aSThomas Huth 7658fe08facSDavid Gibson static void ppc_hash64_set_dsi(CPUState *cs, uint64_t dar, uint64_t dsisr) 766fcf5ef2aSThomas Huth { 7678fe08facSDavid Gibson CPUPPCState *env = &POWERPC_CPU(cs)->env; 768fcf5ef2aSThomas Huth bool vpm; 769fcf5ef2aSThomas Huth 770fcf5ef2aSThomas Huth if (msr_dr) { 771fcf5ef2aSThomas Huth vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1); 772fcf5ef2aSThomas Huth } else { 7731b99e029SDavid Gibson vpm = ppc_hash64_use_vrma(env); 774fcf5ef2aSThomas Huth } 775fcf5ef2aSThomas Huth if (vpm && !msr_hv) { 776fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_HDSI; 777fcf5ef2aSThomas Huth env->spr[SPR_HDAR] = dar; 778fcf5ef2aSThomas Huth env->spr[SPR_HDSISR] = dsisr; 779fcf5ef2aSThomas Huth } else { 780fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_DSI; 781fcf5ef2aSThomas Huth env->spr[SPR_DAR] = dar; 782fcf5ef2aSThomas Huth env->spr[SPR_DSISR] = dsisr; 783fcf5ef2aSThomas Huth } 784fcf5ef2aSThomas Huth env->error_code = 0; 785fcf5ef2aSThomas Huth } 786fcf5ef2aSThomas Huth 787fcf5ef2aSThomas Huth 788a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_r(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1) 789a2dd4e83SBenjamin Herrenschmidt { 790a2dd4e83SBenjamin Herrenschmidt hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + 16; 791a2dd4e83SBenjamin Herrenschmidt 792a2dd4e83SBenjamin Herrenschmidt if (cpu->vhyp) { 793a2dd4e83SBenjamin Herrenschmidt PPCVirtualHypervisorClass *vhc = 794a2dd4e83SBenjamin Herrenschmidt PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 795a2dd4e83SBenjamin Herrenschmidt vhc->hpte_set_r(cpu->vhyp, ptex, pte1); 796a2dd4e83SBenjamin Herrenschmidt return; 797a2dd4e83SBenjamin Herrenschmidt } 798a2dd4e83SBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 799a2dd4e83SBenjamin Herrenschmidt 800a2dd4e83SBenjamin Herrenschmidt 801a2dd4e83SBenjamin Herrenschmidt /* The HW performs a non-atomic byte update */ 802a2dd4e83SBenjamin Herrenschmidt stb_phys(CPU(cpu)->as, base + offset, ((pte1 >> 8) & 0xff) | 0x01); 803a2dd4e83SBenjamin Herrenschmidt } 804a2dd4e83SBenjamin Herrenschmidt 805a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_c(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1) 806a2dd4e83SBenjamin Herrenschmidt { 807a2dd4e83SBenjamin Herrenschmidt hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + 15; 808a2dd4e83SBenjamin Herrenschmidt 809a2dd4e83SBenjamin Herrenschmidt if (cpu->vhyp) { 810a2dd4e83SBenjamin Herrenschmidt PPCVirtualHypervisorClass *vhc = 811a2dd4e83SBenjamin Herrenschmidt PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 812a2dd4e83SBenjamin Herrenschmidt vhc->hpte_set_c(cpu->vhyp, ptex, pte1); 813a2dd4e83SBenjamin Herrenschmidt return; 814a2dd4e83SBenjamin Herrenschmidt } 815a2dd4e83SBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 816a2dd4e83SBenjamin Herrenschmidt 817a2dd4e83SBenjamin Herrenschmidt /* The HW performs a non-atomic byte update */ 818a2dd4e83SBenjamin Herrenschmidt stb_phys(CPU(cpu)->as, base + offset, (pte1 & 0xff) | 0x80); 819a2dd4e83SBenjamin Herrenschmidt } 820a2dd4e83SBenjamin Herrenschmidt 821a864a6b3SDavid Gibson static target_ulong rmls_limit(PowerPCCPU *cpu) 822a864a6b3SDavid Gibson { 823a864a6b3SDavid Gibson CPUPPCState *env = &cpu->env; 824a864a6b3SDavid Gibson /* 825d37b40daSDavid Gibson * In theory the meanings of RMLS values are implementation 826d37b40daSDavid Gibson * dependent. In practice, this seems to have been the set from 827d37b40daSDavid Gibson * POWER4+..POWER8, and RMLS is no longer supported in POWER9. 828a864a6b3SDavid Gibson * 829a864a6b3SDavid Gibson * Unsupported values mean the OS has shot itself in the 830a864a6b3SDavid Gibson * foot. Return a 0-sized RMA in this case, which we expect 831a864a6b3SDavid Gibson * to trigger an immediate DSI or ISI 832a864a6b3SDavid Gibson */ 833a864a6b3SDavid Gibson static const target_ulong rma_sizes[16] = { 834d37b40daSDavid Gibson [0] = 256 * GiB, 835a864a6b3SDavid Gibson [1] = 16 * GiB, 836a864a6b3SDavid Gibson [2] = 1 * GiB, 837a864a6b3SDavid Gibson [3] = 64 * MiB, 838a864a6b3SDavid Gibson [4] = 256 * MiB, 839a864a6b3SDavid Gibson [7] = 128 * MiB, 840a864a6b3SDavid Gibson [8] = 32 * MiB, 841a864a6b3SDavid Gibson }; 842a864a6b3SDavid Gibson target_ulong rmls = (env->spr[SPR_LPCR] & LPCR_RMLS) >> LPCR_RMLS_SHIFT; 843a864a6b3SDavid Gibson 844a864a6b3SDavid Gibson return rma_sizes[rmls]; 845a864a6b3SDavid Gibson } 846a864a6b3SDavid Gibson 8474c24a87fSDavid Gibson static int build_vrma_slbe(PowerPCCPU *cpu, ppc_slb_t *slb) 8484c24a87fSDavid Gibson { 8494c24a87fSDavid Gibson CPUPPCState *env = &cpu->env; 8504c24a87fSDavid Gibson target_ulong lpcr = env->spr[SPR_LPCR]; 8514c24a87fSDavid Gibson uint32_t vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT; 8524c24a87fSDavid Gibson target_ulong vsid = SLB_VSID_VRMA | ((vrmasd << 4) & SLB_VSID_LLP_MASK); 8534c24a87fSDavid Gibson int i; 8544c24a87fSDavid Gibson 8554c24a87fSDavid Gibson for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 8564c24a87fSDavid Gibson const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i]; 8574c24a87fSDavid Gibson 8584c24a87fSDavid Gibson if (!sps->page_shift) { 8594c24a87fSDavid Gibson break; 8604c24a87fSDavid Gibson } 8614c24a87fSDavid Gibson 8624c24a87fSDavid Gibson if ((vsid & SLB_VSID_LLP_MASK) == sps->slb_enc) { 8634c24a87fSDavid Gibson slb->esid = SLB_ESID_V; 8644c24a87fSDavid Gibson slb->vsid = vsid; 8654c24a87fSDavid Gibson slb->sps = sps; 8664c24a87fSDavid Gibson return 0; 8674c24a87fSDavid Gibson } 8684c24a87fSDavid Gibson } 8694c24a87fSDavid Gibson 8704c24a87fSDavid Gibson error_report("Bad page size encoding in LPCR[VRMASD]; LPCR=0x" 871ff5b5d5bSMarkus Armbruster TARGET_FMT_lx, lpcr); 8724c24a87fSDavid Gibson 8734c24a87fSDavid Gibson return -1; 8744c24a87fSDavid Gibson } 8754c24a87fSDavid Gibson 876*1a8c647bSRichard Henderson static bool ppc_hash64_xlate(PowerPCCPU *cpu, vaddr eaddr, 877*1a8c647bSRichard Henderson MMUAccessType access_type, 878*1a8c647bSRichard Henderson hwaddr *raddrp, int *psizep, int *protp, 879*1a8c647bSRichard Henderson bool guest_visible) 880fcf5ef2aSThomas Huth { 881fcf5ef2aSThomas Huth CPUState *cs = CPU(cpu); 882fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 8834c24a87fSDavid Gibson ppc_slb_t vrma_slbe; 884fcf5ef2aSThomas Huth ppc_slb_t *slb; 885fcf5ef2aSThomas Huth unsigned apshift; 8867222b94aSDavid Gibson hwaddr ptex; 887fcf5ef2aSThomas Huth ppc_hash_pte64_t pte; 88807a68f99SSuraj Jitindar Singh int exec_prot, pp_prot, amr_prot, prot; 889182357dbSRichard Henderson int need_prot; 890fcf5ef2aSThomas Huth hwaddr raddr; 891fcf5ef2aSThomas Huth 892d75cbae8SDavid Gibson /* 893d75cbae8SDavid Gibson * Note on LPCR usage: 970 uses HID4, but our special variant of 894d75cbae8SDavid Gibson * store_spr copies relevant fields into env->spr[SPR_LPCR]. 895136fbf65Szhaolichang * Similarly we filter unimplemented bits when storing into LPCR 896d75cbae8SDavid Gibson * depending on the MMU version. This code can thus just use the 897d75cbae8SDavid Gibson * LPCR "as-is". 898fcf5ef2aSThomas Huth */ 899fcf5ef2aSThomas Huth 900fcf5ef2aSThomas Huth /* 1. Handle real mode accesses */ 90159dec5bfSRichard Henderson if (access_type == MMU_INST_FETCH ? !msr_ir : !msr_dr) { 902d75cbae8SDavid Gibson /* 903d75cbae8SDavid Gibson * Translation is supposedly "off", but in real mode the top 4 904d75cbae8SDavid Gibson * effective address bits are (mostly) ignored 905d75cbae8SDavid Gibson */ 906fcf5ef2aSThomas Huth raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL; 907fcf5ef2aSThomas Huth 908682c1dfbSDavid Gibson if (cpu->vhyp) { 909682c1dfbSDavid Gibson /* 910682c1dfbSDavid Gibson * In virtual hypervisor mode, there's nothing to do: 911682c1dfbSDavid Gibson * EA == GPA == qemu guest address 912682c1dfbSDavid Gibson */ 913682c1dfbSDavid Gibson } else if (msr_hv || !env->has_hv_mode) { 914fcf5ef2aSThomas Huth /* In HV mode, add HRMOR if top EA bit is clear */ 915fcf5ef2aSThomas Huth if (!(eaddr >> 63)) { 916fcf5ef2aSThomas Huth raddr |= env->spr[SPR_HRMOR]; 917fcf5ef2aSThomas Huth } 9181b99e029SDavid Gibson } else if (ppc_hash64_use_vrma(env)) { 919682c1dfbSDavid Gibson /* Emulated VRMA mode */ 9204c24a87fSDavid Gibson slb = &vrma_slbe; 9214c24a87fSDavid Gibson if (build_vrma_slbe(cpu, slb) != 0) { 922682c1dfbSDavid Gibson /* Invalid VRMA setup, machine check */ 923*1a8c647bSRichard Henderson if (guest_visible) { 924fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_MCHECK; 925fcf5ef2aSThomas Huth env->error_code = 0; 926*1a8c647bSRichard Henderson } 927*1a8c647bSRichard Henderson return false; 928682c1dfbSDavid Gibson } 929682c1dfbSDavid Gibson 930682c1dfbSDavid Gibson goto skip_slb_search; 931fcf5ef2aSThomas Huth } else { 9323a56a55cSDavid Gibson target_ulong limit = rmls_limit(cpu); 9333a56a55cSDavid Gibson 934682c1dfbSDavid Gibson /* Emulated old-style RMO mode, bounds check against RMLS */ 9353a56a55cSDavid Gibson if (raddr >= limit) { 936*1a8c647bSRichard Henderson if (!guest_visible) { 937*1a8c647bSRichard Henderson return false; 938*1a8c647bSRichard Henderson } 93959dec5bfSRichard Henderson switch (access_type) { 94059dec5bfSRichard Henderson case MMU_INST_FETCH: 9418fe08facSDavid Gibson ppc_hash64_set_isi(cs, SRR1_PROTFAULT); 94259dec5bfSRichard Henderson break; 94359dec5bfSRichard Henderson case MMU_DATA_LOAD: 94459dec5bfSRichard Henderson ppc_hash64_set_dsi(cs, eaddr, DSISR_PROTFAULT); 94559dec5bfSRichard Henderson break; 94659dec5bfSRichard Henderson case MMU_DATA_STORE: 94759dec5bfSRichard Henderson ppc_hash64_set_dsi(cs, eaddr, 94859dec5bfSRichard Henderson DSISR_PROTFAULT | DSISR_ISSTORE); 94959dec5bfSRichard Henderson break; 95059dec5bfSRichard Henderson default: 95159dec5bfSRichard Henderson g_assert_not_reached(); 952fcf5ef2aSThomas Huth } 953*1a8c647bSRichard Henderson return false; 954fcf5ef2aSThomas Huth } 955682c1dfbSDavid Gibson 956682c1dfbSDavid Gibson raddr |= env->spr[SPR_RMOR]; 957fcf5ef2aSThomas Huth } 958*1a8c647bSRichard Henderson 959*1a8c647bSRichard Henderson *raddrp = raddr; 960*1a8c647bSRichard Henderson *protp = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 961*1a8c647bSRichard Henderson *psizep = TARGET_PAGE_BITS; 962*1a8c647bSRichard Henderson return true; 963fcf5ef2aSThomas Huth } 964fcf5ef2aSThomas Huth 965fcf5ef2aSThomas Huth /* 2. Translation is on, so look up the SLB */ 966fcf5ef2aSThomas Huth slb = slb_lookup(cpu, eaddr); 967fcf5ef2aSThomas Huth if (!slb) { 968b2899495SSuraj Jitindar Singh /* No entry found, check if in-memory segment tables are in use */ 969ca79b3b7SDavid Gibson if (ppc64_use_proc_tbl(cpu)) { 970b2899495SSuraj Jitindar Singh /* TODO - Unsupported */ 971b2899495SSuraj Jitindar Singh error_report("Segment Table Support Unimplemented"); 972b2899495SSuraj Jitindar Singh exit(1); 973b2899495SSuraj Jitindar Singh } 974b2899495SSuraj Jitindar Singh /* Segment still not found, generate the appropriate interrupt */ 975*1a8c647bSRichard Henderson if (!guest_visible) { 976*1a8c647bSRichard Henderson return false; 977*1a8c647bSRichard Henderson } 97859dec5bfSRichard Henderson switch (access_type) { 97959dec5bfSRichard Henderson case MMU_INST_FETCH: 980fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_ISEG; 981fcf5ef2aSThomas Huth env->error_code = 0; 98259dec5bfSRichard Henderson break; 98359dec5bfSRichard Henderson case MMU_DATA_LOAD: 98459dec5bfSRichard Henderson case MMU_DATA_STORE: 985fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_DSEG; 986fcf5ef2aSThomas Huth env->error_code = 0; 987fcf5ef2aSThomas Huth env->spr[SPR_DAR] = eaddr; 98859dec5bfSRichard Henderson break; 98959dec5bfSRichard Henderson default: 99059dec5bfSRichard Henderson g_assert_not_reached(); 991fcf5ef2aSThomas Huth } 992*1a8c647bSRichard Henderson return false; 993fcf5ef2aSThomas Huth } 994fcf5ef2aSThomas Huth 995fcf5ef2aSThomas Huth skip_slb_search: 996fcf5ef2aSThomas Huth 997fcf5ef2aSThomas Huth /* 3. Check for segment level no-execute violation */ 99859dec5bfSRichard Henderson if (access_type == MMU_INST_FETCH && (slb->vsid & SLB_VSID_N)) { 999*1a8c647bSRichard Henderson if (guest_visible) { 10008fe08facSDavid Gibson ppc_hash64_set_isi(cs, SRR1_NOEXEC_GUARD); 1001*1a8c647bSRichard Henderson } 1002*1a8c647bSRichard Henderson return false; 1003fcf5ef2aSThomas Huth } 1004fcf5ef2aSThomas Huth 1005fcf5ef2aSThomas Huth /* 4. Locate the PTE in the hash table */ 10067222b94aSDavid Gibson ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift); 10077222b94aSDavid Gibson if (ptex == -1) { 1008*1a8c647bSRichard Henderson if (!guest_visible) { 1009*1a8c647bSRichard Henderson return false; 1010*1a8c647bSRichard Henderson } 101159dec5bfSRichard Henderson switch (access_type) { 101259dec5bfSRichard Henderson case MMU_INST_FETCH: 10138fe08facSDavid Gibson ppc_hash64_set_isi(cs, SRR1_NOPTE); 101459dec5bfSRichard Henderson break; 101559dec5bfSRichard Henderson case MMU_DATA_LOAD: 101659dec5bfSRichard Henderson ppc_hash64_set_dsi(cs, eaddr, DSISR_NOPTE); 101759dec5bfSRichard Henderson break; 101859dec5bfSRichard Henderson case MMU_DATA_STORE: 101959dec5bfSRichard Henderson ppc_hash64_set_dsi(cs, eaddr, DSISR_NOPTE | DSISR_ISSTORE); 102059dec5bfSRichard Henderson break; 102159dec5bfSRichard Henderson default: 102259dec5bfSRichard Henderson g_assert_not_reached(); 1023fcf5ef2aSThomas Huth } 1024*1a8c647bSRichard Henderson return false; 1025fcf5ef2aSThomas Huth } 1026fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 10277222b94aSDavid Gibson "found PTE at index %08" HWADDR_PRIx "\n", ptex); 1028fcf5ef2aSThomas Huth 1029fcf5ef2aSThomas Huth /* 5. Check access permissions */ 1030fcf5ef2aSThomas Huth 103107a68f99SSuraj Jitindar Singh exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte); 1032fcf5ef2aSThomas Huth pp_prot = ppc_hash64_pte_prot(cpu, slb, pte); 1033fcf5ef2aSThomas Huth amr_prot = ppc_hash64_amr_prot(cpu, pte); 103407a68f99SSuraj Jitindar Singh prot = exec_prot & pp_prot & amr_prot; 1035fcf5ef2aSThomas Huth 103659dec5bfSRichard Henderson need_prot = prot_for_access_type(access_type); 1037182357dbSRichard Henderson if (need_prot & ~prot) { 1038fcf5ef2aSThomas Huth /* Access right violation */ 1039fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n"); 1040*1a8c647bSRichard Henderson if (!guest_visible) { 1041*1a8c647bSRichard Henderson return false; 1042*1a8c647bSRichard Henderson } 104359dec5bfSRichard Henderson if (access_type == MMU_INST_FETCH) { 1044a6152b52SSuraj Jitindar Singh int srr1 = 0; 104507a68f99SSuraj Jitindar Singh if (PAGE_EXEC & ~exec_prot) { 104607a68f99SSuraj Jitindar Singh srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */ 104707a68f99SSuraj Jitindar Singh } else if (PAGE_EXEC & ~pp_prot) { 1048a6152b52SSuraj Jitindar Singh srr1 |= SRR1_PROTFAULT; /* Access violates access authority */ 1049a6152b52SSuraj Jitindar Singh } 1050a6152b52SSuraj Jitindar Singh if (PAGE_EXEC & ~amr_prot) { 1051a6152b52SSuraj Jitindar Singh srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */ 1052a6152b52SSuraj Jitindar Singh } 10538fe08facSDavid Gibson ppc_hash64_set_isi(cs, srr1); 1054fcf5ef2aSThomas Huth } else { 1055da82c73aSSuraj Jitindar Singh int dsisr = 0; 1056182357dbSRichard Henderson if (need_prot & ~pp_prot) { 1057da82c73aSSuraj Jitindar Singh dsisr |= DSISR_PROTFAULT; 1058fcf5ef2aSThomas Huth } 105959dec5bfSRichard Henderson if (access_type == MMU_DATA_STORE) { 1060da82c73aSSuraj Jitindar Singh dsisr |= DSISR_ISSTORE; 1061fcf5ef2aSThomas Huth } 1062182357dbSRichard Henderson if (need_prot & ~amr_prot) { 1063da82c73aSSuraj Jitindar Singh dsisr |= DSISR_AMR; 1064fcf5ef2aSThomas Huth } 10658fe08facSDavid Gibson ppc_hash64_set_dsi(cs, eaddr, dsisr); 1066fcf5ef2aSThomas Huth } 1067*1a8c647bSRichard Henderson return false; 1068fcf5ef2aSThomas Huth } 1069fcf5ef2aSThomas Huth 1070fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n"); 1071fcf5ef2aSThomas Huth 1072fcf5ef2aSThomas Huth /* 6. Update PTE referenced and changed bits if necessary */ 1073fcf5ef2aSThomas Huth 1074a2dd4e83SBenjamin Herrenschmidt if (!(pte.pte1 & HPTE64_R_R)) { 1075a2dd4e83SBenjamin Herrenschmidt ppc_hash64_set_r(cpu, ptex, pte.pte1); 1076a2dd4e83SBenjamin Herrenschmidt } 1077a2dd4e83SBenjamin Herrenschmidt if (!(pte.pte1 & HPTE64_R_C)) { 107859dec5bfSRichard Henderson if (access_type == MMU_DATA_STORE) { 1079a2dd4e83SBenjamin Herrenschmidt ppc_hash64_set_c(cpu, ptex, pte.pte1); 1080fcf5ef2aSThomas Huth } else { 1081d75cbae8SDavid Gibson /* 1082d75cbae8SDavid Gibson * Treat the page as read-only for now, so that a later write 1083d75cbae8SDavid Gibson * will pass through this function again to set the C bit 1084d75cbae8SDavid Gibson */ 1085fcf5ef2aSThomas Huth prot &= ~PAGE_WRITE; 1086fcf5ef2aSThomas Huth } 1087fcf5ef2aSThomas Huth } 1088fcf5ef2aSThomas Huth 1089fcf5ef2aSThomas Huth /* 7. Determine the real address from the PTE */ 1090fcf5ef2aSThomas Huth 1091*1a8c647bSRichard Henderson *raddrp = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr); 1092*1a8c647bSRichard Henderson *protp = prot; 1093*1a8c647bSRichard Henderson *psizep = apshift; 1094*1a8c647bSRichard Henderson return true; 1095*1a8c647bSRichard Henderson } 1096*1a8c647bSRichard Henderson 1097*1a8c647bSRichard Henderson int ppc_hash64_handle_mmu_fault(PowerPCCPU *cpu, vaddr eaddr, 1098*1a8c647bSRichard Henderson MMUAccessType access_type, int mmu_idx) 1099*1a8c647bSRichard Henderson { 1100*1a8c647bSRichard Henderson CPUState *cs = CPU(cpu); 1101*1a8c647bSRichard Henderson int page_size, prot; 1102*1a8c647bSRichard Henderson hwaddr raddr; 1103*1a8c647bSRichard Henderson 1104*1a8c647bSRichard Henderson if (!ppc_hash64_xlate(cpu, eaddr, access_type, &raddr, 1105*1a8c647bSRichard Henderson &page_size, &prot, true)) { 1106*1a8c647bSRichard Henderson return 1; 1107*1a8c647bSRichard Henderson } 1108fcf5ef2aSThomas Huth 1109fcf5ef2aSThomas Huth tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK, 1110*1a8c647bSRichard Henderson prot, mmu_idx, 1UL << page_size); 1111fcf5ef2aSThomas Huth return 0; 1112fcf5ef2aSThomas Huth } 1113fcf5ef2aSThomas Huth 1114*1a8c647bSRichard Henderson hwaddr ppc_hash64_get_phys_page_debug(PowerPCCPU *cpu, target_ulong eaddr) 1115fcf5ef2aSThomas Huth { 1116*1a8c647bSRichard Henderson int psize, prot; 1117*1a8c647bSRichard Henderson hwaddr raddr; 1118fcf5ef2aSThomas Huth 1119*1a8c647bSRichard Henderson if (!ppc_hash64_xlate(cpu, eaddr, MMU_DATA_LOAD, &raddr, 1120*1a8c647bSRichard Henderson &psize, &prot, false)) { 1121fcf5ef2aSThomas Huth return -1; 1122fcf5ef2aSThomas Huth } 1123fcf5ef2aSThomas Huth 1124*1a8c647bSRichard Henderson return raddr & TARGET_PAGE_MASK; 1125fcf5ef2aSThomas Huth } 1126fcf5ef2aSThomas Huth 11277222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex, 1128fcf5ef2aSThomas Huth target_ulong pte0, target_ulong pte1) 1129fcf5ef2aSThomas Huth { 1130fcf5ef2aSThomas Huth /* 1131fcf5ef2aSThomas Huth * XXX: given the fact that there are too many segments to 1132fcf5ef2aSThomas Huth * invalidate, and we still don't have a tlb_flush_mask(env, n, 1133fcf5ef2aSThomas Huth * mask) in QEMU, we just invalidate all TLBs 1134fcf5ef2aSThomas Huth */ 1135fcf5ef2aSThomas Huth cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH; 1136fcf5ef2aSThomas Huth } 1137fcf5ef2aSThomas Huth 11382b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 11395ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val) 11405ad55315SDavid Gibson { 1141db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 11425ad55315SDavid Gibson 11435ad55315SDavid Gibson ppc_store_lpcr(cpu, val); 11445ad55315SDavid Gibson } 11452b44e219SBruno Larsen (billionai) #endif 11465ad55315SDavid Gibson 1147a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu) 1148a059471dSDavid Gibson { 1149a059471dSDavid Gibson CPUPPCState *env = &cpu->env; 1150a059471dSDavid Gibson PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 1151a059471dSDavid Gibson 115221e405f1SDavid Gibson if (!pcc->hash64_opts) { 1153d57d72a8SGreg Kurz assert(!mmu_is_64bit(env->mmu_model)); 115421e405f1SDavid Gibson return; 115521e405f1SDavid Gibson } 115621e405f1SDavid Gibson 115721e405f1SDavid Gibson cpu->hash64_opts = g_memdup(pcc->hash64_opts, sizeof(*cpu->hash64_opts)); 115821e405f1SDavid Gibson } 115921e405f1SDavid Gibson 116021e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu) 116121e405f1SDavid Gibson { 116221e405f1SDavid Gibson g_free(cpu->hash64_opts); 116321e405f1SDavid Gibson } 116421e405f1SDavid Gibson 116521e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = { 116658969eeeSDavid Gibson .flags = 0, 116767d7d66fSDavid Gibson .slb_size = 64, 1168a059471dSDavid Gibson .sps = { 1169a059471dSDavid Gibson { .page_shift = 12, /* 4K */ 1170a059471dSDavid Gibson .slb_enc = 0, 1171a059471dSDavid Gibson .enc = { { .page_shift = 12, .pte_enc = 0 } } 1172a059471dSDavid Gibson }, 1173a059471dSDavid Gibson { .page_shift = 24, /* 16M */ 1174a059471dSDavid Gibson .slb_enc = 0x100, 1175a059471dSDavid Gibson .enc = { { .page_shift = 24, .pte_enc = 0 } } 1176a059471dSDavid Gibson }, 1177a059471dSDavid Gibson }, 1178a059471dSDavid Gibson }; 1179b07c59f7SDavid Gibson 1180b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = { 118126cd35b8SDavid Gibson .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE, 118267d7d66fSDavid Gibson .slb_size = 32, 1183b07c59f7SDavid Gibson .sps = { 1184b07c59f7SDavid Gibson { 1185b07c59f7SDavid Gibson .page_shift = 12, /* 4K */ 1186b07c59f7SDavid Gibson .slb_enc = 0, 1187b07c59f7SDavid Gibson .enc = { { .page_shift = 12, .pte_enc = 0 }, 1188b07c59f7SDavid Gibson { .page_shift = 16, .pte_enc = 0x7 }, 1189b07c59f7SDavid Gibson { .page_shift = 24, .pte_enc = 0x38 }, }, 1190b07c59f7SDavid Gibson }, 1191b07c59f7SDavid Gibson { 1192b07c59f7SDavid Gibson .page_shift = 16, /* 64K */ 1193b07c59f7SDavid Gibson .slb_enc = SLB_VSID_64K, 1194b07c59f7SDavid Gibson .enc = { { .page_shift = 16, .pte_enc = 0x1 }, 1195b07c59f7SDavid Gibson { .page_shift = 24, .pte_enc = 0x8 }, }, 1196b07c59f7SDavid Gibson }, 1197b07c59f7SDavid Gibson { 1198b07c59f7SDavid Gibson .page_shift = 24, /* 16M */ 1199b07c59f7SDavid Gibson .slb_enc = SLB_VSID_16M, 1200b07c59f7SDavid Gibson .enc = { { .page_shift = 24, .pte_enc = 0 }, }, 1201b07c59f7SDavid Gibson }, 1202b07c59f7SDavid Gibson { 1203b07c59f7SDavid Gibson .page_shift = 34, /* 16G */ 1204b07c59f7SDavid Gibson .slb_enc = SLB_VSID_16G, 1205b07c59f7SDavid Gibson .enc = { { .page_shift = 34, .pte_enc = 0x3 }, }, 1206b07c59f7SDavid Gibson }, 1207b07c59f7SDavid Gibson } 1208b07c59f7SDavid Gibson }; 120927f00f0aSDavid Gibson 121027f00f0aSDavid Gibson 1211