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" 2474781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h" 25fcf5ef2aSThomas Huth #include "qemu/error-report.h" 26fad866daSMarkus Armbruster #include "qemu/qemu-print.h" 27b3946626SVincent Palatin #include "sysemu/hw_accel.h" 28fcf5ef2aSThomas Huth #include "kvm_ppc.h" 29fcf5ef2aSThomas Huth #include "mmu-hash64.h" 30fcf5ef2aSThomas Huth #include "exec/log.h" 317222b94aSDavid Gibson #include "hw/hw.h" 32182357dbSRichard Henderson #include "internal.h" 33b2899495SSuraj Jitindar Singh #include "mmu-book3s-v3.h" 34f03de3b4SRichard Henderson #include "helper_regs.h" 35fcf5ef2aSThomas Huth 362b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 372b44e219SBruno Larsen (billionai) #include "exec/helper-proto.h" 382b44e219SBruno Larsen (billionai) #endif 392b44e219SBruno Larsen (billionai) 40d75cbae8SDavid Gibson /* #define DEBUG_SLB */ 41fcf5ef2aSThomas Huth 42fcf5ef2aSThomas Huth #ifdef DEBUG_SLB 43fcf5ef2aSThomas Huth # define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__) 44fcf5ef2aSThomas Huth #else 45fcf5ef2aSThomas Huth # define LOG_SLB(...) do { } while (0) 46fcf5ef2aSThomas Huth #endif 47fcf5ef2aSThomas Huth 48fcf5ef2aSThomas Huth /* 49fcf5ef2aSThomas Huth * SLB handling 50fcf5ef2aSThomas Huth */ 51fcf5ef2aSThomas Huth 52fcf5ef2aSThomas Huth static ppc_slb_t *slb_lookup(PowerPCCPU *cpu, target_ulong eaddr) 53fcf5ef2aSThomas Huth { 54fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 55fcf5ef2aSThomas Huth uint64_t esid_256M, esid_1T; 56fcf5ef2aSThomas Huth int n; 57fcf5ef2aSThomas Huth 58fcf5ef2aSThomas Huth LOG_SLB("%s: eaddr " TARGET_FMT_lx "\n", __func__, eaddr); 59fcf5ef2aSThomas Huth 60fcf5ef2aSThomas Huth esid_256M = (eaddr & SEGMENT_MASK_256M) | SLB_ESID_V; 61fcf5ef2aSThomas Huth esid_1T = (eaddr & SEGMENT_MASK_1T) | SLB_ESID_V; 62fcf5ef2aSThomas Huth 6367d7d66fSDavid Gibson for (n = 0; n < cpu->hash64_opts->slb_size; n++) { 64fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[n]; 65fcf5ef2aSThomas Huth 66fcf5ef2aSThomas Huth LOG_SLB("%s: slot %d %016" PRIx64 " %016" 67fcf5ef2aSThomas Huth PRIx64 "\n", __func__, n, slb->esid, slb->vsid); 68d75cbae8SDavid Gibson /* 69d75cbae8SDavid Gibson * We check for 1T matches on all MMUs here - if the MMU 70fcf5ef2aSThomas Huth * doesn't have 1T segment support, we will have prevented 1T 71d75cbae8SDavid Gibson * entries from being inserted in the slbmte code. 72d75cbae8SDavid Gibson */ 73fcf5ef2aSThomas Huth if (((slb->esid == esid_256M) && 74fcf5ef2aSThomas Huth ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_256M)) 75fcf5ef2aSThomas Huth || ((slb->esid == esid_1T) && 76fcf5ef2aSThomas Huth ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_1T))) { 77fcf5ef2aSThomas Huth return slb; 78fcf5ef2aSThomas Huth } 79fcf5ef2aSThomas Huth } 80fcf5ef2aSThomas Huth 81fcf5ef2aSThomas Huth return NULL; 82fcf5ef2aSThomas Huth } 83fcf5ef2aSThomas Huth 84fad866daSMarkus Armbruster void dump_slb(PowerPCCPU *cpu) 85fcf5ef2aSThomas Huth { 86fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 87fcf5ef2aSThomas Huth int i; 88fcf5ef2aSThomas Huth uint64_t slbe, slbv; 89fcf5ef2aSThomas Huth 90fcf5ef2aSThomas Huth cpu_synchronize_state(CPU(cpu)); 91fcf5ef2aSThomas Huth 92fad866daSMarkus Armbruster qemu_printf("SLB\tESID\t\t\tVSID\n"); 9367d7d66fSDavid Gibson for (i = 0; i < cpu->hash64_opts->slb_size; i++) { 94fcf5ef2aSThomas Huth slbe = env->slb[i].esid; 95fcf5ef2aSThomas Huth slbv = env->slb[i].vsid; 96fcf5ef2aSThomas Huth if (slbe == 0 && slbv == 0) { 97fcf5ef2aSThomas Huth continue; 98fcf5ef2aSThomas Huth } 99fad866daSMarkus Armbruster qemu_printf("%d\t0x%016" PRIx64 "\t0x%016" PRIx64 "\n", 100fcf5ef2aSThomas Huth i, slbe, slbv); 101fcf5ef2aSThomas Huth } 102fcf5ef2aSThomas Huth } 103fcf5ef2aSThomas Huth 1042b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 1052bfcb7a3SLucas Coutinho void helper_SLBIA(CPUPPCState *env, uint32_t ih) 106fcf5ef2aSThomas Huth { 107db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 1080418bf78SNicholas Piggin int starting_entry; 109fcf5ef2aSThomas Huth int n; 110fcf5ef2aSThomas Huth 111f9e3e1a3SNicholas Piggin /* 112f9e3e1a3SNicholas Piggin * slbia must always flush all TLB (which is equivalent to ERAT in ppc 113f9e3e1a3SNicholas Piggin * architecture). Matching on SLB_ESID_V is not good enough, because slbmte 114f9e3e1a3SNicholas Piggin * can overwrite a valid SLB without flushing its lookaside information. 115f9e3e1a3SNicholas Piggin * 116f9e3e1a3SNicholas Piggin * It would be possible to keep the TLB in synch with the SLB by flushing 117f9e3e1a3SNicholas Piggin * when a valid entry is overwritten by slbmte, and therefore slbia would 118f9e3e1a3SNicholas Piggin * not have to flush unless it evicts a valid SLB entry. However it is 119f9e3e1a3SNicholas Piggin * expected that slbmte is more common than slbia, and slbia is usually 120f9e3e1a3SNicholas Piggin * going to evict valid SLB entries, so that tradeoff is unlikely to be a 121f9e3e1a3SNicholas Piggin * good one. 1220418bf78SNicholas Piggin * 1230418bf78SNicholas Piggin * ISA v2.05 introduced IH field with values 0,1,2,6. These all invalidate 1240418bf78SNicholas Piggin * the same SLB entries (everything but entry 0), but differ in what 1250418bf78SNicholas Piggin * "lookaside information" is invalidated. TCG can ignore this and flush 1260418bf78SNicholas Piggin * everything. 1270418bf78SNicholas Piggin * 1280418bf78SNicholas Piggin * ISA v3.0 introduced additional values 3,4,7, which change what SLBs are 1290418bf78SNicholas Piggin * invalidated. 130f9e3e1a3SNicholas Piggin */ 131f9e3e1a3SNicholas Piggin 1320418bf78SNicholas Piggin env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH; 1330418bf78SNicholas Piggin 1340418bf78SNicholas Piggin starting_entry = 1; /* default for IH=0,1,2,6 */ 1350418bf78SNicholas Piggin 1360418bf78SNicholas Piggin if (env->mmu_model == POWERPC_MMU_3_00) { 1370418bf78SNicholas Piggin switch (ih) { 1380418bf78SNicholas Piggin case 0x7: 1390418bf78SNicholas Piggin /* invalidate no SLBs, but all lookaside information */ 1400418bf78SNicholas Piggin return; 1410418bf78SNicholas Piggin 1420418bf78SNicholas Piggin case 0x3: 1430418bf78SNicholas Piggin case 0x4: 1440418bf78SNicholas Piggin /* also considers SLB entry 0 */ 1450418bf78SNicholas Piggin starting_entry = 0; 1460418bf78SNicholas Piggin break; 1470418bf78SNicholas Piggin 1480418bf78SNicholas Piggin case 0x5: 1490418bf78SNicholas Piggin /* treat undefined values as ih==0, and warn */ 1500418bf78SNicholas Piggin qemu_log_mask(LOG_GUEST_ERROR, 1510418bf78SNicholas Piggin "slbia undefined IH field %u.\n", ih); 1520418bf78SNicholas Piggin break; 1530418bf78SNicholas Piggin 1540418bf78SNicholas Piggin default: 1550418bf78SNicholas Piggin /* 0,1,2,6 */ 1560418bf78SNicholas Piggin break; 1570418bf78SNicholas Piggin } 1580418bf78SNicholas Piggin } 1590418bf78SNicholas Piggin 1600418bf78SNicholas Piggin for (n = starting_entry; n < cpu->hash64_opts->slb_size; n++) { 161fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[n]; 162fcf5ef2aSThomas Huth 1630418bf78SNicholas Piggin if (!(slb->esid & SLB_ESID_V)) { 1640418bf78SNicholas Piggin continue; 1650418bf78SNicholas Piggin } 1660418bf78SNicholas Piggin if (env->mmu_model == POWERPC_MMU_3_00) { 1670418bf78SNicholas Piggin if (ih == 0x3 && (slb->vsid & SLB_VSID_C) == 0) { 1680418bf78SNicholas Piggin /* preserves entries with a class value of 0 */ 1690418bf78SNicholas Piggin continue; 170f9e3e1a3SNicholas Piggin } 171f9e3e1a3SNicholas Piggin } 172f9e3e1a3SNicholas Piggin 1730418bf78SNicholas Piggin slb->esid &= ~SLB_ESID_V; 1740418bf78SNicholas Piggin } 175fcf5ef2aSThomas Huth } 176fcf5ef2aSThomas Huth 177491a2553SLucas Coutinho #if defined(TARGET_PPC64) 178491a2553SLucas Coutinho void helper_SLBIAG(CPUPPCState *env, target_ulong rs, uint32_t l) 179491a2553SLucas Coutinho { 180491a2553SLucas Coutinho PowerPCCPU *cpu = env_archcpu(env); 181491a2553SLucas Coutinho int n; 182491a2553SLucas Coutinho 183491a2553SLucas Coutinho /* 184491a2553SLucas Coutinho * slbiag must always flush all TLB (which is equivalent to ERAT in ppc 185491a2553SLucas Coutinho * architecture). Matching on SLB_ESID_V is not good enough, because slbmte 186491a2553SLucas Coutinho * can overwrite a valid SLB without flushing its lookaside information. 187491a2553SLucas Coutinho * 188491a2553SLucas Coutinho * It would be possible to keep the TLB in synch with the SLB by flushing 189491a2553SLucas Coutinho * when a valid entry is overwritten by slbmte, and therefore slbiag would 190491a2553SLucas Coutinho * not have to flush unless it evicts a valid SLB entry. However it is 191491a2553SLucas Coutinho * expected that slbmte is more common than slbiag, and slbiag is usually 192491a2553SLucas Coutinho * going to evict valid SLB entries, so that tradeoff is unlikely to be a 193491a2553SLucas Coutinho * good one. 194491a2553SLucas Coutinho */ 195491a2553SLucas Coutinho env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH; 196491a2553SLucas Coutinho 197491a2553SLucas Coutinho for (n = 0; n < cpu->hash64_opts->slb_size; n++) { 198491a2553SLucas Coutinho ppc_slb_t *slb = &env->slb[n]; 199491a2553SLucas Coutinho slb->esid &= ~SLB_ESID_V; 200491a2553SLucas Coutinho } 201491a2553SLucas Coutinho } 202491a2553SLucas Coutinho #endif 203491a2553SLucas Coutinho 204a63f1dfcSNikunj A Dadhania static void __helper_slbie(CPUPPCState *env, target_ulong addr, 205a63f1dfcSNikunj A Dadhania target_ulong global) 206fcf5ef2aSThomas Huth { 207db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 208fcf5ef2aSThomas Huth ppc_slb_t *slb; 209fcf5ef2aSThomas Huth 210fcf5ef2aSThomas Huth slb = slb_lookup(cpu, addr); 211fcf5ef2aSThomas Huth if (!slb) { 212fcf5ef2aSThomas Huth return; 213fcf5ef2aSThomas Huth } 214fcf5ef2aSThomas Huth 215fcf5ef2aSThomas Huth if (slb->esid & SLB_ESID_V) { 216fcf5ef2aSThomas Huth slb->esid &= ~SLB_ESID_V; 217fcf5ef2aSThomas Huth 218d75cbae8SDavid Gibson /* 219d75cbae8SDavid Gibson * XXX: given the fact that segment size is 256 MB or 1TB, 220fcf5ef2aSThomas Huth * and we still don't have a tlb_flush_mask(env, n, mask) 221fcf5ef2aSThomas Huth * in QEMU, we just invalidate all TLBs 222fcf5ef2aSThomas Huth */ 223a63f1dfcSNikunj A Dadhania env->tlb_need_flush |= 224a63f1dfcSNikunj A Dadhania (global == false ? TLB_NEED_LOCAL_FLUSH : TLB_NEED_GLOBAL_FLUSH); 225fcf5ef2aSThomas Huth } 226fcf5ef2aSThomas Huth } 227fcf5ef2aSThomas Huth 22843507e47SLucas Coutinho void helper_SLBIE(CPUPPCState *env, target_ulong addr) 229a63f1dfcSNikunj A Dadhania { 230a63f1dfcSNikunj A Dadhania __helper_slbie(env, addr, false); 231a63f1dfcSNikunj A Dadhania } 232a63f1dfcSNikunj A Dadhania 233a1b05c06SLucas Coutinho void helper_SLBIEG(CPUPPCState *env, target_ulong addr) 234a63f1dfcSNikunj A Dadhania { 235a63f1dfcSNikunj A Dadhania __helper_slbie(env, addr, true); 236a63f1dfcSNikunj A Dadhania } 2372b44e219SBruno Larsen (billionai) #endif 238a63f1dfcSNikunj A Dadhania 239fcf5ef2aSThomas Huth int ppc_store_slb(PowerPCCPU *cpu, target_ulong slot, 240fcf5ef2aSThomas Huth target_ulong esid, target_ulong vsid) 241fcf5ef2aSThomas Huth { 242fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 243fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 244b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = NULL; 245fcf5ef2aSThomas Huth int i; 246fcf5ef2aSThomas Huth 24767d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 248fcf5ef2aSThomas Huth return -1; /* Bad slot number */ 249fcf5ef2aSThomas Huth } 250fcf5ef2aSThomas Huth if (esid & ~(SLB_ESID_ESID | SLB_ESID_V)) { 251fcf5ef2aSThomas Huth return -1; /* Reserved bits set */ 252fcf5ef2aSThomas Huth } 253fcf5ef2aSThomas Huth if (vsid & (SLB_VSID_B & ~SLB_VSID_B_1T)) { 254fcf5ef2aSThomas Huth return -1; /* Bad segment size */ 255fcf5ef2aSThomas Huth } 25658969eeeSDavid Gibson if ((vsid & SLB_VSID_B) && !(ppc_hash64_has(cpu, PPC_HASH64_1TSEG))) { 257fcf5ef2aSThomas Huth return -1; /* 1T segment on MMU that doesn't support it */ 258fcf5ef2aSThomas Huth } 259fcf5ef2aSThomas Huth 260fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 261b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i]; 262fcf5ef2aSThomas Huth 263fcf5ef2aSThomas Huth if (!sps1->page_shift) { 264fcf5ef2aSThomas Huth break; 265fcf5ef2aSThomas Huth } 266fcf5ef2aSThomas Huth 267fcf5ef2aSThomas Huth if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) { 268fcf5ef2aSThomas Huth sps = sps1; 269fcf5ef2aSThomas Huth break; 270fcf5ef2aSThomas Huth } 271fcf5ef2aSThomas Huth } 272fcf5ef2aSThomas Huth 273fcf5ef2aSThomas Huth if (!sps) { 274fcf5ef2aSThomas Huth error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu 275fcf5ef2aSThomas Huth " esid 0x"TARGET_FMT_lx" vsid 0x"TARGET_FMT_lx, 276fcf5ef2aSThomas Huth slot, esid, vsid); 277fcf5ef2aSThomas Huth return -1; 278fcf5ef2aSThomas Huth } 279fcf5ef2aSThomas Huth 280fcf5ef2aSThomas Huth slb->esid = esid; 281fcf5ef2aSThomas Huth slb->vsid = vsid; 282fcf5ef2aSThomas Huth slb->sps = sps; 283fcf5ef2aSThomas Huth 28476134d48SSuraj Jitindar Singh LOG_SLB("%s: " TARGET_FMT_lu " " TARGET_FMT_lx " - " TARGET_FMT_lx 28576134d48SSuraj Jitindar Singh " => %016" PRIx64 " %016" PRIx64 "\n", __func__, slot, esid, vsid, 286fcf5ef2aSThomas Huth slb->esid, slb->vsid); 287fcf5ef2aSThomas Huth 288fcf5ef2aSThomas Huth return 0; 289fcf5ef2aSThomas Huth } 290fcf5ef2aSThomas Huth 2912b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 292fcf5ef2aSThomas Huth static int ppc_load_slb_esid(PowerPCCPU *cpu, target_ulong rb, 293fcf5ef2aSThomas Huth target_ulong *rt) 294fcf5ef2aSThomas Huth { 295fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 296fcf5ef2aSThomas Huth int slot = rb & 0xfff; 297fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 298fcf5ef2aSThomas Huth 29967d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 300fcf5ef2aSThomas Huth return -1; 301fcf5ef2aSThomas Huth } 302fcf5ef2aSThomas Huth 303fcf5ef2aSThomas Huth *rt = slb->esid; 304fcf5ef2aSThomas Huth return 0; 305fcf5ef2aSThomas Huth } 306fcf5ef2aSThomas Huth 307fcf5ef2aSThomas Huth static int ppc_load_slb_vsid(PowerPCCPU *cpu, target_ulong rb, 308fcf5ef2aSThomas Huth target_ulong *rt) 309fcf5ef2aSThomas Huth { 310fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 311fcf5ef2aSThomas Huth int slot = rb & 0xfff; 312fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 313fcf5ef2aSThomas Huth 31467d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 315fcf5ef2aSThomas Huth return -1; 316fcf5ef2aSThomas Huth } 317fcf5ef2aSThomas Huth 318fcf5ef2aSThomas Huth *rt = slb->vsid; 319fcf5ef2aSThomas Huth return 0; 320fcf5ef2aSThomas Huth } 321fcf5ef2aSThomas Huth 322fcf5ef2aSThomas Huth static int ppc_find_slb_vsid(PowerPCCPU *cpu, target_ulong rb, 323fcf5ef2aSThomas Huth target_ulong *rt) 324fcf5ef2aSThomas Huth { 325fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 326fcf5ef2aSThomas Huth ppc_slb_t *slb; 327fcf5ef2aSThomas Huth 328fcf5ef2aSThomas Huth if (!msr_is_64bit(env, env->msr)) { 329fcf5ef2aSThomas Huth rb &= 0xffffffff; 330fcf5ef2aSThomas Huth } 331fcf5ef2aSThomas Huth slb = slb_lookup(cpu, rb); 332fcf5ef2aSThomas Huth if (slb == NULL) { 333fcf5ef2aSThomas Huth *rt = (target_ulong)-1ul; 334fcf5ef2aSThomas Huth } else { 335fcf5ef2aSThomas Huth *rt = slb->vsid; 336fcf5ef2aSThomas Huth } 337fcf5ef2aSThomas Huth return 0; 338fcf5ef2aSThomas Huth } 339fcf5ef2aSThomas Huth 3400b0ba40fSLucas Coutinho void helper_SLBMTE(CPUPPCState *env, target_ulong rb, target_ulong rs) 341fcf5ef2aSThomas Huth { 342db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 343fcf5ef2aSThomas Huth 344fcf5ef2aSThomas Huth if (ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs) < 0) { 345fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 346fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 347fcf5ef2aSThomas Huth } 348fcf5ef2aSThomas Huth } 349fcf5ef2aSThomas Huth 35041b60e46SLucas Coutinho target_ulong helper_SLBMFEE(CPUPPCState *env, target_ulong rb) 351fcf5ef2aSThomas Huth { 352db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 353fcf5ef2aSThomas Huth target_ulong rt = 0; 354fcf5ef2aSThomas Huth 355fcf5ef2aSThomas Huth if (ppc_load_slb_esid(cpu, rb, &rt) < 0) { 356fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 357fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 358fcf5ef2aSThomas Huth } 359fcf5ef2aSThomas Huth return rt; 360fcf5ef2aSThomas Huth } 361fcf5ef2aSThomas Huth 36226d02c9dSLucas Coutinho target_ulong helper_SLBFEE(CPUPPCState *env, target_ulong rb) 363fcf5ef2aSThomas Huth { 364db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 365fcf5ef2aSThomas Huth target_ulong rt = 0; 366fcf5ef2aSThomas Huth 367fcf5ef2aSThomas Huth if (ppc_find_slb_vsid(cpu, rb, &rt) < 0) { 368fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 369fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 370fcf5ef2aSThomas Huth } 371fcf5ef2aSThomas Huth return rt; 372fcf5ef2aSThomas Huth } 373fcf5ef2aSThomas Huth 37474a15384SLucas Coutinho target_ulong helper_SLBMFEV(CPUPPCState *env, target_ulong rb) 375fcf5ef2aSThomas Huth { 376db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 377fcf5ef2aSThomas Huth target_ulong rt = 0; 378fcf5ef2aSThomas Huth 379fcf5ef2aSThomas Huth if (ppc_load_slb_vsid(cpu, rb, &rt) < 0) { 380fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 381fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 382fcf5ef2aSThomas Huth } 383fcf5ef2aSThomas Huth return rt; 384fcf5ef2aSThomas Huth } 3852b44e219SBruno Larsen (billionai) #endif 386fcf5ef2aSThomas Huth 38707a68f99SSuraj Jitindar Singh /* Check No-Execute or Guarded Storage */ 38807a68f99SSuraj Jitindar Singh static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU *cpu, 38907a68f99SSuraj Jitindar Singh ppc_hash_pte64_t pte) 39007a68f99SSuraj Jitindar Singh { 39107a68f99SSuraj Jitindar Singh /* Exec permissions CANNOT take away read or write permissions */ 39207a68f99SSuraj Jitindar Singh return (pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G) ? 39307a68f99SSuraj Jitindar Singh PAGE_READ | PAGE_WRITE : PAGE_READ | PAGE_WRITE | PAGE_EXEC; 39407a68f99SSuraj Jitindar Singh } 39507a68f99SSuraj Jitindar Singh 39607a68f99SSuraj Jitindar Singh /* Check Basic Storage Protection */ 39703695a98SBruno Larsen (billionai) static int ppc_hash64_pte_prot(int mmu_idx, 398fcf5ef2aSThomas Huth ppc_slb_t *slb, ppc_hash_pte64_t pte) 399fcf5ef2aSThomas Huth { 400fcf5ef2aSThomas Huth unsigned pp, key; 401d75cbae8SDavid Gibson /* 402d75cbae8SDavid Gibson * Some pp bit combinations have undefined behaviour, so default 403d75cbae8SDavid Gibson * to no access in those cases 404d75cbae8SDavid Gibson */ 405fcf5ef2aSThomas Huth int prot = 0; 406fcf5ef2aSThomas Huth 40703695a98SBruno Larsen (billionai) key = !!(mmuidx_pr(mmu_idx) ? (slb->vsid & SLB_VSID_KP) 408fcf5ef2aSThomas Huth : (slb->vsid & SLB_VSID_KS)); 409fcf5ef2aSThomas Huth pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61); 410fcf5ef2aSThomas Huth 411fcf5ef2aSThomas Huth if (key == 0) { 412fcf5ef2aSThomas Huth switch (pp) { 413fcf5ef2aSThomas Huth case 0x0: 414fcf5ef2aSThomas Huth case 0x1: 415fcf5ef2aSThomas Huth case 0x2: 416347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 417fcf5ef2aSThomas Huth break; 418fcf5ef2aSThomas Huth 419fcf5ef2aSThomas Huth case 0x3: 420fcf5ef2aSThomas Huth case 0x6: 421347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_EXEC; 422fcf5ef2aSThomas Huth break; 423fcf5ef2aSThomas Huth } 424fcf5ef2aSThomas Huth } else { 425fcf5ef2aSThomas Huth switch (pp) { 426fcf5ef2aSThomas Huth case 0x0: 427fcf5ef2aSThomas Huth case 0x6: 428fcf5ef2aSThomas Huth break; 429fcf5ef2aSThomas Huth 430fcf5ef2aSThomas Huth case 0x1: 431fcf5ef2aSThomas Huth case 0x3: 432347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_EXEC; 433fcf5ef2aSThomas Huth break; 434fcf5ef2aSThomas Huth 435fcf5ef2aSThomas Huth case 0x2: 436347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 437fcf5ef2aSThomas Huth break; 438fcf5ef2aSThomas Huth } 439fcf5ef2aSThomas Huth } 440fcf5ef2aSThomas Huth 441fcf5ef2aSThomas Huth return prot; 442fcf5ef2aSThomas Huth } 443fcf5ef2aSThomas Huth 444a6152b52SSuraj Jitindar Singh /* Check the instruction access permissions specified in the IAMR */ 445a6152b52SSuraj Jitindar Singh static int ppc_hash64_iamr_prot(PowerPCCPU *cpu, int key) 446a6152b52SSuraj Jitindar Singh { 447a6152b52SSuraj Jitindar Singh CPUPPCState *env = &cpu->env; 448a6152b52SSuraj Jitindar Singh int iamr_bits = (env->spr[SPR_IAMR] >> 2 * (31 - key)) & 0x3; 449a6152b52SSuraj Jitindar Singh 450a6152b52SSuraj Jitindar Singh /* 451a6152b52SSuraj Jitindar Singh * An instruction fetch is permitted if the IAMR bit is 0. 452a6152b52SSuraj Jitindar Singh * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit 453a6152b52SSuraj Jitindar Singh * can only take away EXEC permissions not READ or WRITE permissions. 454a6152b52SSuraj Jitindar Singh * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since 455a6152b52SSuraj Jitindar Singh * EXEC permissions are allowed. 456a6152b52SSuraj Jitindar Singh */ 457a6152b52SSuraj Jitindar Singh return (iamr_bits & 0x1) ? PAGE_READ | PAGE_WRITE : 458a6152b52SSuraj Jitindar Singh PAGE_READ | PAGE_WRITE | PAGE_EXEC; 459a6152b52SSuraj Jitindar Singh } 460a6152b52SSuraj Jitindar Singh 461fcf5ef2aSThomas Huth static int ppc_hash64_amr_prot(PowerPCCPU *cpu, ppc_hash_pte64_t pte) 462fcf5ef2aSThomas Huth { 463fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 464fcf5ef2aSThomas Huth int key, amrbits; 465fcf5ef2aSThomas Huth int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 466fcf5ef2aSThomas Huth 467fcf5ef2aSThomas Huth /* Only recent MMUs implement Virtual Page Class Key Protection */ 46858969eeeSDavid Gibson if (!ppc_hash64_has(cpu, PPC_HASH64_AMR)) { 469fcf5ef2aSThomas Huth return prot; 470fcf5ef2aSThomas Huth } 471fcf5ef2aSThomas Huth 472fcf5ef2aSThomas Huth key = HPTE64_R_KEY(pte.pte1); 473fcf5ef2aSThomas Huth amrbits = (env->spr[SPR_AMR] >> 2 * (31 - key)) & 0x3; 474fcf5ef2aSThomas Huth 475fcf5ef2aSThomas Huth /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */ 476fcf5ef2aSThomas Huth /* env->spr[SPR_AMR]); */ 477fcf5ef2aSThomas Huth 478fcf5ef2aSThomas Huth /* 479fcf5ef2aSThomas Huth * A store is permitted if the AMR bit is 0. Remove write 480fcf5ef2aSThomas Huth * protection if it is set. 481fcf5ef2aSThomas Huth */ 482fcf5ef2aSThomas Huth if (amrbits & 0x2) { 483fcf5ef2aSThomas Huth prot &= ~PAGE_WRITE; 484fcf5ef2aSThomas Huth } 485fcf5ef2aSThomas Huth /* 486fcf5ef2aSThomas Huth * A load is permitted if the AMR bit is 0. Remove read 487fcf5ef2aSThomas Huth * protection if it is set. 488fcf5ef2aSThomas Huth */ 489fcf5ef2aSThomas Huth if (amrbits & 0x1) { 490fcf5ef2aSThomas Huth prot &= ~PAGE_READ; 491fcf5ef2aSThomas Huth } 492fcf5ef2aSThomas Huth 493a6152b52SSuraj Jitindar Singh switch (env->mmu_model) { 494a6152b52SSuraj Jitindar Singh /* 495a6152b52SSuraj Jitindar Singh * MMU version 2.07 and later support IAMR 496a6152b52SSuraj Jitindar Singh * Check if the IAMR allows the instruction access - it will return 497a6152b52SSuraj Jitindar Singh * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0 498a6152b52SSuraj Jitindar Singh * if it does (and prot will be unchanged indicating execution support). 499a6152b52SSuraj Jitindar Singh */ 500a6152b52SSuraj Jitindar Singh case POWERPC_MMU_2_07: 501a6152b52SSuraj Jitindar Singh case POWERPC_MMU_3_00: 502a6152b52SSuraj Jitindar Singh prot &= ppc_hash64_iamr_prot(cpu, key); 503a6152b52SSuraj Jitindar Singh break; 504a6152b52SSuraj Jitindar Singh default: 505a6152b52SSuraj Jitindar Singh break; 506a6152b52SSuraj Jitindar Singh } 507a6152b52SSuraj Jitindar Singh 508fcf5ef2aSThomas Huth return prot; 509fcf5ef2aSThomas Huth } 510fcf5ef2aSThomas Huth 511*14a43ab3SBALATON Zoltan static hwaddr ppc_hash64_hpt_base(PowerPCCPU *cpu) 512*14a43ab3SBALATON Zoltan { 513*14a43ab3SBALATON Zoltan uint64_t base; 514*14a43ab3SBALATON Zoltan 515*14a43ab3SBALATON Zoltan if (cpu->vhyp) { 516*14a43ab3SBALATON Zoltan return 0; 517*14a43ab3SBALATON Zoltan } 518*14a43ab3SBALATON Zoltan if (cpu->env.mmu_model == POWERPC_MMU_3_00) { 519*14a43ab3SBALATON Zoltan ppc_v3_pate_t pate; 520*14a43ab3SBALATON Zoltan 521*14a43ab3SBALATON Zoltan if (!ppc64_v3_get_pate(cpu, cpu->env.spr[SPR_LPIDR], &pate)) { 522*14a43ab3SBALATON Zoltan return 0; 523*14a43ab3SBALATON Zoltan } 524*14a43ab3SBALATON Zoltan base = pate.dw0; 525*14a43ab3SBALATON Zoltan } else { 526*14a43ab3SBALATON Zoltan base = cpu->env.spr[SPR_SDR1]; 527*14a43ab3SBALATON Zoltan } 528*14a43ab3SBALATON Zoltan return base & SDR_64_HTABORG; 529*14a43ab3SBALATON Zoltan } 530*14a43ab3SBALATON Zoltan 531*14a43ab3SBALATON Zoltan static hwaddr ppc_hash64_hpt_mask(PowerPCCPU *cpu) 532*14a43ab3SBALATON Zoltan { 533*14a43ab3SBALATON Zoltan uint64_t base; 534*14a43ab3SBALATON Zoltan 535*14a43ab3SBALATON Zoltan if (cpu->vhyp) { 536*14a43ab3SBALATON Zoltan return cpu->vhyp_class->hpt_mask(cpu->vhyp); 537*14a43ab3SBALATON Zoltan } 538*14a43ab3SBALATON Zoltan if (cpu->env.mmu_model == POWERPC_MMU_3_00) { 539*14a43ab3SBALATON Zoltan ppc_v3_pate_t pate; 540*14a43ab3SBALATON Zoltan 541*14a43ab3SBALATON Zoltan if (!ppc64_v3_get_pate(cpu, cpu->env.spr[SPR_LPIDR], &pate)) { 542*14a43ab3SBALATON Zoltan return 0; 543*14a43ab3SBALATON Zoltan } 544*14a43ab3SBALATON Zoltan base = pate.dw0; 545*14a43ab3SBALATON Zoltan } else { 546*14a43ab3SBALATON Zoltan base = cpu->env.spr[SPR_SDR1]; 547*14a43ab3SBALATON Zoltan } 548*14a43ab3SBALATON Zoltan return (1ULL << ((base & SDR_64_HTABSIZE) + 18 - 7)) - 1; 549*14a43ab3SBALATON Zoltan } 550*14a43ab3SBALATON Zoltan 5517222b94aSDavid Gibson const ppc_hash_pte64_t *ppc_hash64_map_hptes(PowerPCCPU *cpu, 5527222b94aSDavid Gibson hwaddr ptex, int n) 553fcf5ef2aSThomas Huth { 5547222b94aSDavid Gibson hwaddr pte_offset = ptex * HASH_PTE_SIZE_64; 5553367c62fSBenjamin Herrenschmidt hwaddr base; 5567222b94aSDavid Gibson hwaddr plen = n * HASH_PTE_SIZE_64; 557e57ca75cSDavid Gibson const ppc_hash_pte64_t *hptes; 558e57ca75cSDavid Gibson 559e57ca75cSDavid Gibson if (cpu->vhyp) { 560c700b5e1SNicholas Piggin return cpu->vhyp_class->map_hptes(cpu->vhyp, ptex, n); 561e57ca75cSDavid Gibson } 5623367c62fSBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 563e57ca75cSDavid Gibson 564e57ca75cSDavid Gibson if (!base) { 565e57ca75cSDavid Gibson return NULL; 566e57ca75cSDavid Gibson } 567e57ca75cSDavid Gibson 568f26404fbSPeter Maydell hptes = address_space_map(CPU(cpu)->as, base + pte_offset, &plen, false, 569f26404fbSPeter Maydell MEMTXATTRS_UNSPECIFIED); 5707222b94aSDavid Gibson if (plen < (n * HASH_PTE_SIZE_64)) { 5717222b94aSDavid Gibson hw_error("%s: Unable to map all requested HPTEs\n", __func__); 572fcf5ef2aSThomas Huth } 5737222b94aSDavid Gibson return hptes; 574fcf5ef2aSThomas Huth } 575fcf5ef2aSThomas Huth 5767222b94aSDavid Gibson void ppc_hash64_unmap_hptes(PowerPCCPU *cpu, const ppc_hash_pte64_t *hptes, 5777222b94aSDavid Gibson hwaddr ptex, int n) 578fcf5ef2aSThomas Huth { 579e57ca75cSDavid Gibson if (cpu->vhyp) { 580c700b5e1SNicholas Piggin cpu->vhyp_class->unmap_hptes(cpu->vhyp, hptes, ptex, n); 581e57ca75cSDavid Gibson return; 582e57ca75cSDavid Gibson } 583e57ca75cSDavid Gibson 5847222b94aSDavid Gibson address_space_unmap(CPU(cpu)->as, (void *)hptes, n * HASH_PTE_SIZE_64, 5857222b94aSDavid Gibson false, n * HASH_PTE_SIZE_64); 586fcf5ef2aSThomas Huth } 587fcf5ef2aSThomas Huth 588*14a43ab3SBALATON Zoltan bool ppc_hash64_valid_ptex(PowerPCCPU *cpu, target_ulong ptex) 589*14a43ab3SBALATON Zoltan { 590*14a43ab3SBALATON Zoltan /* hash value/pteg group index is normalized by HPT mask */ 591*14a43ab3SBALATON Zoltan if (((ptex & ~7ULL) / HPTES_PER_GROUP) & ~ppc_hash64_hpt_mask(cpu)) { 592*14a43ab3SBALATON Zoltan return false; 593*14a43ab3SBALATON Zoltan } 594*14a43ab3SBALATON Zoltan return true; 595*14a43ab3SBALATON Zoltan } 596*14a43ab3SBALATON Zoltan 597b07c59f7SDavid Gibson static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes *sps, 598fcf5ef2aSThomas Huth uint64_t pte0, uint64_t pte1) 599fcf5ef2aSThomas Huth { 600fcf5ef2aSThomas Huth int i; 601fcf5ef2aSThomas Huth 602fcf5ef2aSThomas Huth if (!(pte0 & HPTE64_V_LARGE)) { 603fcf5ef2aSThomas Huth if (sps->page_shift != 12) { 604fcf5ef2aSThomas Huth /* 4kiB page in a non 4kiB segment */ 605fcf5ef2aSThomas Huth return 0; 606fcf5ef2aSThomas Huth } 607fcf5ef2aSThomas Huth /* Normal 4kiB page */ 608fcf5ef2aSThomas Huth return 12; 609fcf5ef2aSThomas Huth } 610fcf5ef2aSThomas Huth 611fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 612b07c59f7SDavid Gibson const PPCHash64PageSize *ps = &sps->enc[i]; 613fcf5ef2aSThomas Huth uint64_t mask; 614fcf5ef2aSThomas Huth 615fcf5ef2aSThomas Huth if (!ps->page_shift) { 616fcf5ef2aSThomas Huth break; 617fcf5ef2aSThomas Huth } 618fcf5ef2aSThomas Huth 619fcf5ef2aSThomas Huth if (ps->page_shift == 12) { 620fcf5ef2aSThomas Huth /* L bit is set so this can't be a 4kiB page */ 621fcf5ef2aSThomas Huth continue; 622fcf5ef2aSThomas Huth } 623fcf5ef2aSThomas Huth 624fcf5ef2aSThomas Huth mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN; 625fcf5ef2aSThomas Huth 626fcf5ef2aSThomas Huth if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) { 627fcf5ef2aSThomas Huth return ps->page_shift; 628fcf5ef2aSThomas Huth } 629fcf5ef2aSThomas Huth } 630fcf5ef2aSThomas Huth 631fcf5ef2aSThomas Huth return 0; /* Bad page size encoding */ 632fcf5ef2aSThomas Huth } 633fcf5ef2aSThomas Huth 63434525595SBenjamin Herrenschmidt static void ppc64_v3_new_to_old_hpte(target_ulong *pte0, target_ulong *pte1) 63534525595SBenjamin Herrenschmidt { 63634525595SBenjamin Herrenschmidt /* Insert B into pte0 */ 63734525595SBenjamin Herrenschmidt *pte0 = (*pte0 & HPTE64_V_COMMON_BITS) | 63834525595SBenjamin Herrenschmidt ((*pte1 & HPTE64_R_3_0_SSIZE_MASK) << 63934525595SBenjamin Herrenschmidt (HPTE64_V_SSIZE_SHIFT - HPTE64_R_3_0_SSIZE_SHIFT)); 64034525595SBenjamin Herrenschmidt 64134525595SBenjamin Herrenschmidt /* Remove B from pte1 */ 64234525595SBenjamin Herrenschmidt *pte1 = *pte1 & ~HPTE64_R_3_0_SSIZE_MASK; 64334525595SBenjamin Herrenschmidt } 64434525595SBenjamin Herrenschmidt 64534525595SBenjamin Herrenschmidt 646fcf5ef2aSThomas Huth static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash, 647b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps, 648fcf5ef2aSThomas Huth target_ulong ptem, 649fcf5ef2aSThomas Huth ppc_hash_pte64_t *pte, unsigned *pshift) 650fcf5ef2aSThomas Huth { 651fcf5ef2aSThomas Huth int i; 6527222b94aSDavid Gibson const ppc_hash_pte64_t *pteg; 653fcf5ef2aSThomas Huth target_ulong pte0, pte1; 6547222b94aSDavid Gibson target_ulong ptex; 655fcf5ef2aSThomas Huth 65636778660SDavid Gibson ptex = (hash & ppc_hash64_hpt_mask(cpu)) * HPTES_PER_GROUP; 6577222b94aSDavid Gibson pteg = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP); 6587222b94aSDavid Gibson if (!pteg) { 659fcf5ef2aSThomas Huth return -1; 660fcf5ef2aSThomas Huth } 661fcf5ef2aSThomas Huth for (i = 0; i < HPTES_PER_GROUP; i++) { 6627222b94aSDavid Gibson pte0 = ppc_hash64_hpte0(cpu, pteg, i); 6633054b0caSBenjamin Herrenschmidt /* 6643054b0caSBenjamin Herrenschmidt * pte0 contains the valid bit and must be read before pte1, 6653054b0caSBenjamin Herrenschmidt * otherwise we might see an old pte1 with a new valid bit and 6663054b0caSBenjamin Herrenschmidt * thus an inconsistent hpte value 6673054b0caSBenjamin Herrenschmidt */ 6683054b0caSBenjamin Herrenschmidt smp_rmb(); 6697222b94aSDavid Gibson pte1 = ppc_hash64_hpte1(cpu, pteg, i); 670fcf5ef2aSThomas Huth 67134525595SBenjamin Herrenschmidt /* Convert format if necessary */ 67234525595SBenjamin Herrenschmidt if (cpu->env.mmu_model == POWERPC_MMU_3_00 && !cpu->vhyp) { 67334525595SBenjamin Herrenschmidt ppc64_v3_new_to_old_hpte(&pte0, &pte1); 67434525595SBenjamin Herrenschmidt } 67534525595SBenjamin Herrenschmidt 676fcf5ef2aSThomas Huth /* This compares V, B, H (secondary) and the AVPN */ 677fcf5ef2aSThomas Huth if (HPTE64_V_COMPARE(pte0, ptem)) { 678fcf5ef2aSThomas Huth *pshift = hpte_page_shift(sps, pte0, pte1); 679fcf5ef2aSThomas Huth /* 680fcf5ef2aSThomas Huth * If there is no match, ignore the PTE, it could simply 681fcf5ef2aSThomas Huth * be for a different segment size encoding and the 682fcf5ef2aSThomas Huth * architecture specifies we should not match. Linux will 683fcf5ef2aSThomas Huth * potentially leave behind PTEs for the wrong base page 684fcf5ef2aSThomas Huth * size when demoting segments. 685fcf5ef2aSThomas Huth */ 686fcf5ef2aSThomas Huth if (*pshift == 0) { 687fcf5ef2aSThomas Huth continue; 688fcf5ef2aSThomas Huth } 689d75cbae8SDavid Gibson /* 690d75cbae8SDavid Gibson * We don't do anything with pshift yet as qemu TLB only 691d75cbae8SDavid Gibson * deals with 4K pages anyway 692fcf5ef2aSThomas Huth */ 693fcf5ef2aSThomas Huth pte->pte0 = pte0; 694fcf5ef2aSThomas Huth pte->pte1 = pte1; 6957222b94aSDavid Gibson ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP); 6967222b94aSDavid Gibson return ptex + i; 697fcf5ef2aSThomas Huth } 698fcf5ef2aSThomas Huth } 6997222b94aSDavid Gibson ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP); 700fcf5ef2aSThomas Huth /* 701fcf5ef2aSThomas Huth * We didn't find a valid entry. 702fcf5ef2aSThomas Huth */ 703fcf5ef2aSThomas Huth return -1; 704fcf5ef2aSThomas Huth } 705fcf5ef2aSThomas Huth 706fcf5ef2aSThomas Huth static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu, 707fcf5ef2aSThomas Huth ppc_slb_t *slb, target_ulong eaddr, 708fcf5ef2aSThomas Huth ppc_hash_pte64_t *pte, unsigned *pshift) 709fcf5ef2aSThomas Huth { 710fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 7117222b94aSDavid Gibson hwaddr hash, ptex; 712fcf5ef2aSThomas Huth uint64_t vsid, epnmask, epn, ptem; 713b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = slb->sps; 714fcf5ef2aSThomas Huth 715d75cbae8SDavid Gibson /* 716d75cbae8SDavid Gibson * The SLB store path should prevent any bad page size encodings 717d75cbae8SDavid Gibson * getting in there, so: 718d75cbae8SDavid Gibson */ 719fcf5ef2aSThomas Huth assert(sps); 720fcf5ef2aSThomas Huth 721fcf5ef2aSThomas Huth /* If ISL is set in LPCR we need to clamp the page size to 4K */ 722fcf5ef2aSThomas Huth if (env->spr[SPR_LPCR] & LPCR_ISL) { 723fcf5ef2aSThomas Huth /* We assume that when using TCG, 4k is first entry of SPS */ 724b07c59f7SDavid Gibson sps = &cpu->hash64_opts->sps[0]; 725fcf5ef2aSThomas Huth assert(sps->page_shift == 12); 726fcf5ef2aSThomas Huth } 727fcf5ef2aSThomas Huth 728fcf5ef2aSThomas Huth epnmask = ~((1ULL << sps->page_shift) - 1); 729fcf5ef2aSThomas Huth 730fcf5ef2aSThomas Huth if (slb->vsid & SLB_VSID_B) { 731fcf5ef2aSThomas Huth /* 1TB segment */ 732fcf5ef2aSThomas Huth vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T; 733fcf5ef2aSThomas Huth epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask; 734fcf5ef2aSThomas Huth hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift); 735fcf5ef2aSThomas Huth } else { 736fcf5ef2aSThomas Huth /* 256M segment */ 737fcf5ef2aSThomas Huth vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT; 738fcf5ef2aSThomas Huth epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask; 739fcf5ef2aSThomas Huth hash = vsid ^ (epn >> sps->page_shift); 740fcf5ef2aSThomas Huth } 741fcf5ef2aSThomas Huth ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN); 742fcf5ef2aSThomas Huth ptem |= HPTE64_V_VALID; 743fcf5ef2aSThomas Huth 744fcf5ef2aSThomas Huth /* Page address translation */ 745fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 746883f2c59SPhilippe Mathieu-Daudé "htab_base " HWADDR_FMT_plx " htab_mask " HWADDR_FMT_plx 747883f2c59SPhilippe Mathieu-Daudé " hash " HWADDR_FMT_plx "\n", 74836778660SDavid Gibson ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), hash); 749fcf5ef2aSThomas Huth 750fcf5ef2aSThomas Huth /* Primary PTEG lookup */ 751fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 752883f2c59SPhilippe Mathieu-Daudé "0 htab=" HWADDR_FMT_plx "/" HWADDR_FMT_plx 753fcf5ef2aSThomas Huth " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx 754883f2c59SPhilippe Mathieu-Daudé " hash=" HWADDR_FMT_plx "\n", 75536778660SDavid Gibson ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), 75636778660SDavid Gibson vsid, ptem, hash); 7577222b94aSDavid Gibson ptex = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift); 758fcf5ef2aSThomas Huth 7597222b94aSDavid Gibson if (ptex == -1) { 760fcf5ef2aSThomas Huth /* Secondary PTEG lookup */ 761fcf5ef2aSThomas Huth ptem |= HPTE64_V_SECONDARY; 762fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 763883f2c59SPhilippe Mathieu-Daudé "1 htab=" HWADDR_FMT_plx "/" HWADDR_FMT_plx 764fcf5ef2aSThomas Huth " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx 765883f2c59SPhilippe Mathieu-Daudé " hash=" HWADDR_FMT_plx "\n", ppc_hash64_hpt_base(cpu), 76636778660SDavid Gibson ppc_hash64_hpt_mask(cpu), vsid, ptem, ~hash); 767fcf5ef2aSThomas Huth 7687222b94aSDavid Gibson ptex = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift); 769fcf5ef2aSThomas Huth } 770fcf5ef2aSThomas Huth 7717222b94aSDavid Gibson return ptex; 772fcf5ef2aSThomas Huth } 773fcf5ef2aSThomas Huth 774fcf5ef2aSThomas Huth unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu, 775fcf5ef2aSThomas Huth uint64_t pte0, uint64_t pte1) 776fcf5ef2aSThomas Huth { 777fcf5ef2aSThomas Huth int i; 778fcf5ef2aSThomas Huth 779fcf5ef2aSThomas Huth if (!(pte0 & HPTE64_V_LARGE)) { 780fcf5ef2aSThomas Huth return 12; 781fcf5ef2aSThomas Huth } 782fcf5ef2aSThomas Huth 783fcf5ef2aSThomas Huth /* 784fcf5ef2aSThomas Huth * The encodings in env->sps need to be carefully chosen so that 785fcf5ef2aSThomas Huth * this gives an unambiguous result. 786fcf5ef2aSThomas Huth */ 787fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 788b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i]; 789fcf5ef2aSThomas Huth unsigned shift; 790fcf5ef2aSThomas Huth 791fcf5ef2aSThomas Huth if (!sps->page_shift) { 792fcf5ef2aSThomas Huth break; 793fcf5ef2aSThomas Huth } 794fcf5ef2aSThomas Huth 795fcf5ef2aSThomas Huth shift = hpte_page_shift(sps, pte0, pte1); 796fcf5ef2aSThomas Huth if (shift) { 797fcf5ef2aSThomas Huth return shift; 798fcf5ef2aSThomas Huth } 799fcf5ef2aSThomas Huth } 800fcf5ef2aSThomas Huth 801fcf5ef2aSThomas Huth return 0; 802fcf5ef2aSThomas Huth } 803fcf5ef2aSThomas Huth 8041b99e029SDavid Gibson static bool ppc_hash64_use_vrma(CPUPPCState *env) 8051b99e029SDavid Gibson { 8061b99e029SDavid Gibson switch (env->mmu_model) { 8071b99e029SDavid Gibson case POWERPC_MMU_3_00: 8081b99e029SDavid Gibson /* 8091b99e029SDavid Gibson * ISAv3.0 (POWER9) always uses VRMA, the VPM0 field and RMOR 8101b99e029SDavid Gibson * register no longer exist 8111b99e029SDavid Gibson */ 8121b99e029SDavid Gibson return true; 8131b99e029SDavid Gibson 8141b99e029SDavid Gibson default: 8151b99e029SDavid Gibson return !!(env->spr[SPR_LPCR] & LPCR_VPM0); 8161b99e029SDavid Gibson } 8171b99e029SDavid Gibson } 8181b99e029SDavid Gibson 8199201af09SNicholas Piggin static void ppc_hash64_set_isi(CPUState *cs, int mmu_idx, uint64_t slb_vsid, 8209201af09SNicholas Piggin uint64_t error_code) 821fcf5ef2aSThomas Huth { 8228fe08facSDavid Gibson CPUPPCState *env = &POWERPC_CPU(cs)->env; 823fcf5ef2aSThomas Huth bool vpm; 824fcf5ef2aSThomas Huth 82503695a98SBruno Larsen (billionai) if (!mmuidx_real(mmu_idx)) { 826fcf5ef2aSThomas Huth vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1); 827fcf5ef2aSThomas Huth } else { 8281b99e029SDavid Gibson vpm = ppc_hash64_use_vrma(env); 829fcf5ef2aSThomas Huth } 83003695a98SBruno Larsen (billionai) if (vpm && !mmuidx_hv(mmu_idx)) { 831fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_HISI; 8329201af09SNicholas Piggin env->spr[SPR_ASDR] = slb_vsid; 833fcf5ef2aSThomas Huth } else { 834fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_ISI; 835fcf5ef2aSThomas Huth } 836fcf5ef2aSThomas Huth env->error_code = error_code; 837fcf5ef2aSThomas Huth } 838fcf5ef2aSThomas Huth 8399201af09SNicholas Piggin static void ppc_hash64_set_dsi(CPUState *cs, int mmu_idx, uint64_t slb_vsid, 8409201af09SNicholas Piggin uint64_t dar, uint64_t dsisr) 841fcf5ef2aSThomas Huth { 8428fe08facSDavid Gibson CPUPPCState *env = &POWERPC_CPU(cs)->env; 843fcf5ef2aSThomas Huth bool vpm; 844fcf5ef2aSThomas Huth 84503695a98SBruno Larsen (billionai) if (!mmuidx_real(mmu_idx)) { 846fcf5ef2aSThomas Huth vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1); 847fcf5ef2aSThomas Huth } else { 8481b99e029SDavid Gibson vpm = ppc_hash64_use_vrma(env); 849fcf5ef2aSThomas Huth } 85003695a98SBruno Larsen (billionai) if (vpm && !mmuidx_hv(mmu_idx)) { 851fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_HDSI; 852fcf5ef2aSThomas Huth env->spr[SPR_HDAR] = dar; 853fcf5ef2aSThomas Huth env->spr[SPR_HDSISR] = dsisr; 8549201af09SNicholas Piggin env->spr[SPR_ASDR] = slb_vsid; 855fcf5ef2aSThomas Huth } else { 856fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_DSI; 857fcf5ef2aSThomas Huth env->spr[SPR_DAR] = dar; 858fcf5ef2aSThomas Huth env->spr[SPR_DSISR] = dsisr; 859fcf5ef2aSThomas Huth } 860fcf5ef2aSThomas Huth env->error_code = 0; 861fcf5ef2aSThomas Huth } 862fcf5ef2aSThomas Huth 863fcf5ef2aSThomas Huth 864a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_r(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1) 865a2dd4e83SBenjamin Herrenschmidt { 8667bf00dfbSLeandro Lupori hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + HPTE64_DW1_R; 867a2dd4e83SBenjamin Herrenschmidt 868a2dd4e83SBenjamin Herrenschmidt if (cpu->vhyp) { 869c700b5e1SNicholas Piggin cpu->vhyp_class->hpte_set_r(cpu->vhyp, ptex, pte1); 870a2dd4e83SBenjamin Herrenschmidt return; 871a2dd4e83SBenjamin Herrenschmidt } 872a2dd4e83SBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 873a2dd4e83SBenjamin Herrenschmidt 874a2dd4e83SBenjamin Herrenschmidt 875a2dd4e83SBenjamin Herrenschmidt /* The HW performs a non-atomic byte update */ 876a2dd4e83SBenjamin Herrenschmidt stb_phys(CPU(cpu)->as, base + offset, ((pte1 >> 8) & 0xff) | 0x01); 877a2dd4e83SBenjamin Herrenschmidt } 878a2dd4e83SBenjamin Herrenschmidt 879a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_c(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1) 880a2dd4e83SBenjamin Herrenschmidt { 8817bf00dfbSLeandro Lupori hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + HPTE64_DW1_C; 882a2dd4e83SBenjamin Herrenschmidt 883a2dd4e83SBenjamin Herrenschmidt if (cpu->vhyp) { 884c700b5e1SNicholas Piggin cpu->vhyp_class->hpte_set_c(cpu->vhyp, ptex, pte1); 885a2dd4e83SBenjamin Herrenschmidt return; 886a2dd4e83SBenjamin Herrenschmidt } 887a2dd4e83SBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 888a2dd4e83SBenjamin Herrenschmidt 889a2dd4e83SBenjamin Herrenschmidt /* The HW performs a non-atomic byte update */ 890a2dd4e83SBenjamin Herrenschmidt stb_phys(CPU(cpu)->as, base + offset, (pte1 & 0xff) | 0x80); 891a2dd4e83SBenjamin Herrenschmidt } 892a2dd4e83SBenjamin Herrenschmidt 893a864a6b3SDavid Gibson static target_ulong rmls_limit(PowerPCCPU *cpu) 894a864a6b3SDavid Gibson { 895a864a6b3SDavid Gibson CPUPPCState *env = &cpu->env; 896a864a6b3SDavid Gibson /* 897d37b40daSDavid Gibson * In theory the meanings of RMLS values are implementation 898d37b40daSDavid Gibson * dependent. In practice, this seems to have been the set from 899d37b40daSDavid Gibson * POWER4+..POWER8, and RMLS is no longer supported in POWER9. 900a864a6b3SDavid Gibson * 901a864a6b3SDavid Gibson * Unsupported values mean the OS has shot itself in the 902a864a6b3SDavid Gibson * foot. Return a 0-sized RMA in this case, which we expect 903a864a6b3SDavid Gibson * to trigger an immediate DSI or ISI 904a864a6b3SDavid Gibson */ 905a864a6b3SDavid Gibson static const target_ulong rma_sizes[16] = { 906d37b40daSDavid Gibson [0] = 256 * GiB, 907a864a6b3SDavid Gibson [1] = 16 * GiB, 908a864a6b3SDavid Gibson [2] = 1 * GiB, 909a864a6b3SDavid Gibson [3] = 64 * MiB, 910a864a6b3SDavid Gibson [4] = 256 * MiB, 911a864a6b3SDavid Gibson [7] = 128 * MiB, 912a864a6b3SDavid Gibson [8] = 32 * MiB, 913a864a6b3SDavid Gibson }; 914a864a6b3SDavid Gibson target_ulong rmls = (env->spr[SPR_LPCR] & LPCR_RMLS) >> LPCR_RMLS_SHIFT; 915a864a6b3SDavid Gibson 916a864a6b3SDavid Gibson return rma_sizes[rmls]; 917a864a6b3SDavid Gibson } 918a864a6b3SDavid Gibson 9190e2a3ec3SNicholas Piggin /* Return the LLP in SLB_VSID format */ 9200e2a3ec3SNicholas Piggin static uint64_t get_vrma_llp(PowerPCCPU *cpu) 9214c24a87fSDavid Gibson { 9224c24a87fSDavid Gibson CPUPPCState *env = &cpu->env; 9230e2a3ec3SNicholas Piggin uint64_t llp; 9240e2a3ec3SNicholas Piggin 9250e2a3ec3SNicholas Piggin if (env->mmu_model == POWERPC_MMU_3_00) { 9260e2a3ec3SNicholas Piggin ppc_v3_pate_t pate; 9270e2a3ec3SNicholas Piggin uint64_t ps, l, lp; 9280e2a3ec3SNicholas Piggin 9290e2a3ec3SNicholas Piggin /* 9300e2a3ec3SNicholas Piggin * ISA v3.0 removes the LPCR[VRMASD] field and puts the VRMA base 9310e2a3ec3SNicholas Piggin * page size (L||LP equivalent) in the PS field in the HPT partition 9320e2a3ec3SNicholas Piggin * table entry. 9330e2a3ec3SNicholas Piggin */ 9340e2a3ec3SNicholas Piggin if (!ppc64_v3_get_pate(cpu, cpu->env.spr[SPR_LPIDR], &pate)) { 9350e2a3ec3SNicholas Piggin error_report("Bad VRMA with no partition table entry"); 9360e2a3ec3SNicholas Piggin return 0; 9370e2a3ec3SNicholas Piggin } 9380e2a3ec3SNicholas Piggin ps = PATE0_GET_PS(pate.dw0); 9390e2a3ec3SNicholas Piggin /* PS has L||LP in 3 consecutive bits, put them into SLB LLP format */ 9400e2a3ec3SNicholas Piggin l = (ps >> 2) & 0x1; 9410e2a3ec3SNicholas Piggin lp = ps & 0x3; 9420e2a3ec3SNicholas Piggin llp = (l << SLB_VSID_L_SHIFT) | (lp << SLB_VSID_LP_SHIFT); 9430e2a3ec3SNicholas Piggin 9440e2a3ec3SNicholas Piggin } else { 9450e2a3ec3SNicholas Piggin uint64_t lpcr = env->spr[SPR_LPCR]; 9460e2a3ec3SNicholas Piggin target_ulong vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT; 9470e2a3ec3SNicholas Piggin 9480e2a3ec3SNicholas Piggin /* VRMASD LLP matches SLB format, just shift and mask it */ 9490e2a3ec3SNicholas Piggin llp = (vrmasd << SLB_VSID_LP_SHIFT) & SLB_VSID_LLP_MASK; 9500e2a3ec3SNicholas Piggin } 9510e2a3ec3SNicholas Piggin 9520e2a3ec3SNicholas Piggin return llp; 9530e2a3ec3SNicholas Piggin } 9540e2a3ec3SNicholas Piggin 9550e2a3ec3SNicholas Piggin static int build_vrma_slbe(PowerPCCPU *cpu, ppc_slb_t *slb) 9560e2a3ec3SNicholas Piggin { 9570e2a3ec3SNicholas Piggin uint64_t llp = get_vrma_llp(cpu); 9580e2a3ec3SNicholas Piggin target_ulong vsid = SLB_VSID_VRMA | llp; 9594c24a87fSDavid Gibson int i; 9604c24a87fSDavid Gibson 9614c24a87fSDavid Gibson for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 9624c24a87fSDavid Gibson const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i]; 9634c24a87fSDavid Gibson 9644c24a87fSDavid Gibson if (!sps->page_shift) { 9654c24a87fSDavid Gibson break; 9664c24a87fSDavid Gibson } 9674c24a87fSDavid Gibson 9684c24a87fSDavid Gibson if ((vsid & SLB_VSID_LLP_MASK) == sps->slb_enc) { 9694c24a87fSDavid Gibson slb->esid = SLB_ESID_V; 9704c24a87fSDavid Gibson slb->vsid = vsid; 9714c24a87fSDavid Gibson slb->sps = sps; 9724c24a87fSDavid Gibson return 0; 9734c24a87fSDavid Gibson } 9744c24a87fSDavid Gibson } 9754c24a87fSDavid Gibson 9760e2a3ec3SNicholas Piggin error_report("Bad VRMA page size encoding 0x" TARGET_FMT_lx, llp); 9774c24a87fSDavid Gibson 9784c24a87fSDavid Gibson return -1; 9794c24a87fSDavid Gibson } 9804c24a87fSDavid Gibson 98151806b54SRichard Henderson bool ppc_hash64_xlate(PowerPCCPU *cpu, vaddr eaddr, MMUAccessType access_type, 98203695a98SBruno Larsen (billionai) hwaddr *raddrp, int *psizep, int *protp, int mmu_idx, 9831a8c647bSRichard Henderson bool guest_visible) 984fcf5ef2aSThomas Huth { 985fcf5ef2aSThomas Huth CPUState *cs = CPU(cpu); 986fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 9874c24a87fSDavid Gibson ppc_slb_t vrma_slbe; 988fcf5ef2aSThomas Huth ppc_slb_t *slb; 989fcf5ef2aSThomas Huth unsigned apshift; 9907222b94aSDavid Gibson hwaddr ptex; 991fcf5ef2aSThomas Huth ppc_hash_pte64_t pte; 99207a68f99SSuraj Jitindar Singh int exec_prot, pp_prot, amr_prot, prot; 993182357dbSRichard Henderson int need_prot; 994fcf5ef2aSThomas Huth hwaddr raddr; 995fcf5ef2aSThomas Huth 996d75cbae8SDavid Gibson /* 997d75cbae8SDavid Gibson * Note on LPCR usage: 970 uses HID4, but our special variant of 998d75cbae8SDavid Gibson * store_spr copies relevant fields into env->spr[SPR_LPCR]. 999136fbf65Szhaolichang * Similarly we filter unimplemented bits when storing into LPCR 1000d75cbae8SDavid Gibson * depending on the MMU version. This code can thus just use the 1001d75cbae8SDavid Gibson * LPCR "as-is". 1002fcf5ef2aSThomas Huth */ 1003fcf5ef2aSThomas Huth 1004fcf5ef2aSThomas Huth /* 1. Handle real mode accesses */ 100503695a98SBruno Larsen (billionai) if (mmuidx_real(mmu_idx)) { 1006d75cbae8SDavid Gibson /* 1007d75cbae8SDavid Gibson * Translation is supposedly "off", but in real mode the top 4 1008d75cbae8SDavid Gibson * effective address bits are (mostly) ignored 1009d75cbae8SDavid Gibson */ 1010fcf5ef2aSThomas Huth raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL; 1011fcf5ef2aSThomas Huth 1012682c1dfbSDavid Gibson if (cpu->vhyp) { 1013682c1dfbSDavid Gibson /* 1014682c1dfbSDavid Gibson * In virtual hypervisor mode, there's nothing to do: 1015682c1dfbSDavid Gibson * EA == GPA == qemu guest address 1016682c1dfbSDavid Gibson */ 101703695a98SBruno Larsen (billionai) } else if (mmuidx_hv(mmu_idx) || !env->has_hv_mode) { 1018fcf5ef2aSThomas Huth /* In HV mode, add HRMOR if top EA bit is clear */ 1019fcf5ef2aSThomas Huth if (!(eaddr >> 63)) { 1020fcf5ef2aSThomas Huth raddr |= env->spr[SPR_HRMOR]; 1021fcf5ef2aSThomas Huth } 10221b99e029SDavid Gibson } else if (ppc_hash64_use_vrma(env)) { 1023682c1dfbSDavid Gibson /* Emulated VRMA mode */ 10244c24a87fSDavid Gibson slb = &vrma_slbe; 10254c24a87fSDavid Gibson if (build_vrma_slbe(cpu, slb) != 0) { 1026682c1dfbSDavid Gibson /* Invalid VRMA setup, machine check */ 10271a8c647bSRichard Henderson if (guest_visible) { 1028fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_MCHECK; 1029fcf5ef2aSThomas Huth env->error_code = 0; 10301a8c647bSRichard Henderson } 10311a8c647bSRichard Henderson return false; 1032682c1dfbSDavid Gibson } 1033682c1dfbSDavid Gibson 1034682c1dfbSDavid Gibson goto skip_slb_search; 1035fcf5ef2aSThomas Huth } else { 10363a56a55cSDavid Gibson target_ulong limit = rmls_limit(cpu); 10373a56a55cSDavid Gibson 1038682c1dfbSDavid Gibson /* Emulated old-style RMO mode, bounds check against RMLS */ 10393a56a55cSDavid Gibson if (raddr >= limit) { 10401a8c647bSRichard Henderson if (!guest_visible) { 10411a8c647bSRichard Henderson return false; 10421a8c647bSRichard Henderson } 104359dec5bfSRichard Henderson switch (access_type) { 104459dec5bfSRichard Henderson case MMU_INST_FETCH: 10459201af09SNicholas Piggin ppc_hash64_set_isi(cs, mmu_idx, 0, SRR1_PROTFAULT); 104659dec5bfSRichard Henderson break; 104759dec5bfSRichard Henderson case MMU_DATA_LOAD: 10489201af09SNicholas Piggin ppc_hash64_set_dsi(cs, mmu_idx, 0, eaddr, DSISR_PROTFAULT); 104959dec5bfSRichard Henderson break; 105059dec5bfSRichard Henderson case MMU_DATA_STORE: 10519201af09SNicholas Piggin ppc_hash64_set_dsi(cs, mmu_idx, 0, eaddr, 105259dec5bfSRichard Henderson DSISR_PROTFAULT | DSISR_ISSTORE); 105359dec5bfSRichard Henderson break; 105459dec5bfSRichard Henderson default: 105559dec5bfSRichard Henderson g_assert_not_reached(); 1056fcf5ef2aSThomas Huth } 10571a8c647bSRichard Henderson return false; 1058fcf5ef2aSThomas Huth } 1059682c1dfbSDavid Gibson 1060682c1dfbSDavid Gibson raddr |= env->spr[SPR_RMOR]; 1061fcf5ef2aSThomas Huth } 10621a8c647bSRichard Henderson 10631a8c647bSRichard Henderson *raddrp = raddr; 10641a8c647bSRichard Henderson *protp = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 10651a8c647bSRichard Henderson *psizep = TARGET_PAGE_BITS; 10661a8c647bSRichard Henderson return true; 1067fcf5ef2aSThomas Huth } 1068fcf5ef2aSThomas Huth 1069fcf5ef2aSThomas Huth /* 2. Translation is on, so look up the SLB */ 1070fcf5ef2aSThomas Huth slb = slb_lookup(cpu, eaddr); 1071fcf5ef2aSThomas Huth if (!slb) { 1072b2899495SSuraj Jitindar Singh /* No entry found, check if in-memory segment tables are in use */ 1073ca79b3b7SDavid Gibson if (ppc64_use_proc_tbl(cpu)) { 1074b2899495SSuraj Jitindar Singh /* TODO - Unsupported */ 1075b2899495SSuraj Jitindar Singh error_report("Segment Table Support Unimplemented"); 1076b2899495SSuraj Jitindar Singh exit(1); 1077b2899495SSuraj Jitindar Singh } 1078b2899495SSuraj Jitindar Singh /* Segment still not found, generate the appropriate interrupt */ 10791a8c647bSRichard Henderson if (!guest_visible) { 10801a8c647bSRichard Henderson return false; 10811a8c647bSRichard Henderson } 108259dec5bfSRichard Henderson switch (access_type) { 108359dec5bfSRichard Henderson case MMU_INST_FETCH: 1084fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_ISEG; 1085fcf5ef2aSThomas Huth env->error_code = 0; 108659dec5bfSRichard Henderson break; 108759dec5bfSRichard Henderson case MMU_DATA_LOAD: 108859dec5bfSRichard Henderson case MMU_DATA_STORE: 1089fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_DSEG; 1090fcf5ef2aSThomas Huth env->error_code = 0; 1091fcf5ef2aSThomas Huth env->spr[SPR_DAR] = eaddr; 109259dec5bfSRichard Henderson break; 109359dec5bfSRichard Henderson default: 109459dec5bfSRichard Henderson g_assert_not_reached(); 1095fcf5ef2aSThomas Huth } 10961a8c647bSRichard Henderson return false; 1097fcf5ef2aSThomas Huth } 1098fcf5ef2aSThomas Huth 1099fcf5ef2aSThomas Huth skip_slb_search: 1100fcf5ef2aSThomas Huth 1101fcf5ef2aSThomas Huth /* 3. Check for segment level no-execute violation */ 110259dec5bfSRichard Henderson if (access_type == MMU_INST_FETCH && (slb->vsid & SLB_VSID_N)) { 11031a8c647bSRichard Henderson if (guest_visible) { 11049201af09SNicholas Piggin ppc_hash64_set_isi(cs, mmu_idx, slb->vsid, SRR1_NOEXEC_GUARD); 11051a8c647bSRichard Henderson } 11061a8c647bSRichard Henderson return false; 1107fcf5ef2aSThomas Huth } 1108fcf5ef2aSThomas Huth 1109fcf5ef2aSThomas Huth /* 4. Locate the PTE in the hash table */ 11107222b94aSDavid Gibson ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift); 11117222b94aSDavid Gibson if (ptex == -1) { 11121a8c647bSRichard Henderson if (!guest_visible) { 11131a8c647bSRichard Henderson return false; 11141a8c647bSRichard Henderson } 111559dec5bfSRichard Henderson switch (access_type) { 111659dec5bfSRichard Henderson case MMU_INST_FETCH: 11179201af09SNicholas Piggin ppc_hash64_set_isi(cs, mmu_idx, slb->vsid, SRR1_NOPTE); 111859dec5bfSRichard Henderson break; 111959dec5bfSRichard Henderson case MMU_DATA_LOAD: 11209201af09SNicholas Piggin ppc_hash64_set_dsi(cs, mmu_idx, slb->vsid, eaddr, DSISR_NOPTE); 112159dec5bfSRichard Henderson break; 112259dec5bfSRichard Henderson case MMU_DATA_STORE: 11239201af09SNicholas Piggin ppc_hash64_set_dsi(cs, mmu_idx, slb->vsid, eaddr, 11249201af09SNicholas Piggin DSISR_NOPTE | DSISR_ISSTORE); 112559dec5bfSRichard Henderson break; 112659dec5bfSRichard Henderson default: 112759dec5bfSRichard Henderson g_assert_not_reached(); 1128fcf5ef2aSThomas Huth } 11291a8c647bSRichard Henderson return false; 1130fcf5ef2aSThomas Huth } 1131fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 11327222b94aSDavid Gibson "found PTE at index %08" HWADDR_PRIx "\n", ptex); 1133fcf5ef2aSThomas Huth 1134fcf5ef2aSThomas Huth /* 5. Check access permissions */ 1135fcf5ef2aSThomas Huth 113607a68f99SSuraj Jitindar Singh exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte); 113703695a98SBruno Larsen (billionai) pp_prot = ppc_hash64_pte_prot(mmu_idx, slb, pte); 1138fcf5ef2aSThomas Huth amr_prot = ppc_hash64_amr_prot(cpu, pte); 113907a68f99SSuraj Jitindar Singh prot = exec_prot & pp_prot & amr_prot; 1140fcf5ef2aSThomas Huth 1141cd1038ecSBALATON Zoltan need_prot = check_prot_access_type(PAGE_RWX, access_type); 1142182357dbSRichard Henderson if (need_prot & ~prot) { 1143fcf5ef2aSThomas Huth /* Access right violation */ 1144fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n"); 11451a8c647bSRichard Henderson if (!guest_visible) { 11461a8c647bSRichard Henderson return false; 11471a8c647bSRichard Henderson } 114859dec5bfSRichard Henderson if (access_type == MMU_INST_FETCH) { 1149a6152b52SSuraj Jitindar Singh int srr1 = 0; 115007a68f99SSuraj Jitindar Singh if (PAGE_EXEC & ~exec_prot) { 115107a68f99SSuraj Jitindar Singh srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */ 115207a68f99SSuraj Jitindar Singh } else if (PAGE_EXEC & ~pp_prot) { 1153a6152b52SSuraj Jitindar Singh srr1 |= SRR1_PROTFAULT; /* Access violates access authority */ 1154a6152b52SSuraj Jitindar Singh } 1155a6152b52SSuraj Jitindar Singh if (PAGE_EXEC & ~amr_prot) { 1156a6152b52SSuraj Jitindar Singh srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */ 1157a6152b52SSuraj Jitindar Singh } 11589201af09SNicholas Piggin ppc_hash64_set_isi(cs, mmu_idx, slb->vsid, srr1); 1159fcf5ef2aSThomas Huth } else { 1160da82c73aSSuraj Jitindar Singh int dsisr = 0; 1161182357dbSRichard Henderson if (need_prot & ~pp_prot) { 1162da82c73aSSuraj Jitindar Singh dsisr |= DSISR_PROTFAULT; 1163fcf5ef2aSThomas Huth } 116459dec5bfSRichard Henderson if (access_type == MMU_DATA_STORE) { 1165da82c73aSSuraj Jitindar Singh dsisr |= DSISR_ISSTORE; 1166fcf5ef2aSThomas Huth } 1167182357dbSRichard Henderson if (need_prot & ~amr_prot) { 1168da82c73aSSuraj Jitindar Singh dsisr |= DSISR_AMR; 1169fcf5ef2aSThomas Huth } 11709201af09SNicholas Piggin ppc_hash64_set_dsi(cs, mmu_idx, slb->vsid, eaddr, dsisr); 1171fcf5ef2aSThomas Huth } 11721a8c647bSRichard Henderson return false; 1173fcf5ef2aSThomas Huth } 1174fcf5ef2aSThomas Huth 1175fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n"); 1176fcf5ef2aSThomas Huth 1177fcf5ef2aSThomas Huth /* 6. Update PTE referenced and changed bits if necessary */ 1178fcf5ef2aSThomas Huth 1179a2dd4e83SBenjamin Herrenschmidt if (!(pte.pte1 & HPTE64_R_R)) { 1180a2dd4e83SBenjamin Herrenschmidt ppc_hash64_set_r(cpu, ptex, pte.pte1); 1181a2dd4e83SBenjamin Herrenschmidt } 1182a2dd4e83SBenjamin Herrenschmidt if (!(pte.pte1 & HPTE64_R_C)) { 118359dec5bfSRichard Henderson if (access_type == MMU_DATA_STORE) { 1184a2dd4e83SBenjamin Herrenschmidt ppc_hash64_set_c(cpu, ptex, pte.pte1); 1185fcf5ef2aSThomas Huth } else { 1186d75cbae8SDavid Gibson /* 1187d75cbae8SDavid Gibson * Treat the page as read-only for now, so that a later write 1188d75cbae8SDavid Gibson * will pass through this function again to set the C bit 1189d75cbae8SDavid Gibson */ 1190fcf5ef2aSThomas Huth prot &= ~PAGE_WRITE; 1191fcf5ef2aSThomas Huth } 1192fcf5ef2aSThomas Huth } 1193fcf5ef2aSThomas Huth 1194fcf5ef2aSThomas Huth /* 7. Determine the real address from the PTE */ 1195fcf5ef2aSThomas Huth 11961a8c647bSRichard Henderson *raddrp = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr); 11971a8c647bSRichard Henderson *protp = prot; 11981a8c647bSRichard Henderson *psizep = apshift; 11991a8c647bSRichard Henderson return true; 12001a8c647bSRichard Henderson } 12011a8c647bSRichard Henderson 12027222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex, 1203fcf5ef2aSThomas Huth target_ulong pte0, target_ulong pte1) 1204fcf5ef2aSThomas Huth { 1205fcf5ef2aSThomas Huth /* 1206fcf5ef2aSThomas Huth * XXX: given the fact that there are too many segments to 1207fcf5ef2aSThomas Huth * invalidate, and we still don't have a tlb_flush_mask(env, n, 1208fcf5ef2aSThomas Huth * mask) in QEMU, we just invalidate all TLBs 1209fcf5ef2aSThomas Huth */ 1210fcf5ef2aSThomas Huth cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH; 1211fcf5ef2aSThomas Huth } 1212fcf5ef2aSThomas Huth 12132b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 12145ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val) 12155ad55315SDavid Gibson { 1216db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 12175ad55315SDavid Gibson 12185ad55315SDavid Gibson ppc_store_lpcr(cpu, val); 12195ad55315SDavid Gibson } 12202b44e219SBruno Larsen (billionai) #endif 12215ad55315SDavid Gibson 1222a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu) 1223a059471dSDavid Gibson { 1224a059471dSDavid Gibson CPUPPCState *env = &cpu->env; 1225a059471dSDavid Gibson PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 1226a059471dSDavid Gibson 122721e405f1SDavid Gibson if (!pcc->hash64_opts) { 1228d57d72a8SGreg Kurz assert(!mmu_is_64bit(env->mmu_model)); 122921e405f1SDavid Gibson return; 123021e405f1SDavid Gibson } 123121e405f1SDavid Gibson 123240fed8c1SPhilippe Mathieu-Daudé cpu->hash64_opts = g_memdup2(pcc->hash64_opts, sizeof(*cpu->hash64_opts)); 123321e405f1SDavid Gibson } 123421e405f1SDavid Gibson 123521e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu) 123621e405f1SDavid Gibson { 123721e405f1SDavid Gibson g_free(cpu->hash64_opts); 123821e405f1SDavid Gibson } 123921e405f1SDavid Gibson 124021e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = { 124158969eeeSDavid Gibson .flags = 0, 124267d7d66fSDavid Gibson .slb_size = 64, 1243a059471dSDavid Gibson .sps = { 1244a059471dSDavid Gibson { .page_shift = 12, /* 4K */ 1245a059471dSDavid Gibson .slb_enc = 0, 1246a059471dSDavid Gibson .enc = { { .page_shift = 12, .pte_enc = 0 } } 1247a059471dSDavid Gibson }, 1248a059471dSDavid Gibson { .page_shift = 24, /* 16M */ 1249a059471dSDavid Gibson .slb_enc = 0x100, 1250a059471dSDavid Gibson .enc = { { .page_shift = 24, .pte_enc = 0 } } 1251a059471dSDavid Gibson }, 1252a059471dSDavid Gibson }, 1253a059471dSDavid Gibson }; 1254b07c59f7SDavid Gibson 1255b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = { 125626cd35b8SDavid Gibson .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE, 125767d7d66fSDavid Gibson .slb_size = 32, 1258b07c59f7SDavid Gibson .sps = { 1259b07c59f7SDavid Gibson { 1260b07c59f7SDavid Gibson .page_shift = 12, /* 4K */ 1261b07c59f7SDavid Gibson .slb_enc = 0, 1262b07c59f7SDavid Gibson .enc = { { .page_shift = 12, .pte_enc = 0 }, 1263b07c59f7SDavid Gibson { .page_shift = 16, .pte_enc = 0x7 }, 1264b07c59f7SDavid Gibson { .page_shift = 24, .pte_enc = 0x38 }, }, 1265b07c59f7SDavid Gibson }, 1266b07c59f7SDavid Gibson { 1267b07c59f7SDavid Gibson .page_shift = 16, /* 64K */ 1268b07c59f7SDavid Gibson .slb_enc = SLB_VSID_64K, 1269b07c59f7SDavid Gibson .enc = { { .page_shift = 16, .pte_enc = 0x1 }, 1270b07c59f7SDavid Gibson { .page_shift = 24, .pte_enc = 0x8 }, }, 1271b07c59f7SDavid Gibson }, 1272b07c59f7SDavid Gibson { 1273b07c59f7SDavid Gibson .page_shift = 24, /* 16M */ 1274b07c59f7SDavid Gibson .slb_enc = SLB_VSID_16M, 1275b07c59f7SDavid Gibson .enc = { { .page_shift = 24, .pte_enc = 0 }, }, 1276b07c59f7SDavid Gibson }, 1277b07c59f7SDavid Gibson { 1278b07c59f7SDavid Gibson .page_shift = 34, /* 16G */ 1279b07c59f7SDavid Gibson .slb_enc = SLB_VSID_16G, 1280b07c59f7SDavid Gibson .enc = { { .page_shift = 34, .pte_enc = 0x3 }, }, 1281b07c59f7SDavid Gibson }, 1282b07c59f7SDavid Gibson } 1283b07c59f7SDavid Gibson }; 128427f00f0aSDavid Gibson 128527f00f0aSDavid Gibson 1286