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" 34d741ecffSBALATON Zoltan #include "mmu-books.h" 35f03de3b4SRichard Henderson #include "helper_regs.h" 36fcf5ef2aSThomas Huth 372b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 382b44e219SBruno Larsen (billionai) #include "exec/helper-proto.h" 392b44e219SBruno Larsen (billionai) #endif 402b44e219SBruno Larsen (billionai) 41d75cbae8SDavid Gibson /* #define DEBUG_SLB */ 42fcf5ef2aSThomas Huth 43fcf5ef2aSThomas Huth #ifdef DEBUG_SLB 44fcf5ef2aSThomas Huth # define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__) 45fcf5ef2aSThomas Huth #else 46fcf5ef2aSThomas Huth # define LOG_SLB(...) do { } while (0) 47fcf5ef2aSThomas Huth #endif 48fcf5ef2aSThomas Huth 49fcf5ef2aSThomas Huth /* 50fcf5ef2aSThomas Huth * SLB handling 51fcf5ef2aSThomas Huth */ 52fcf5ef2aSThomas Huth 53fcf5ef2aSThomas Huth static ppc_slb_t *slb_lookup(PowerPCCPU *cpu, target_ulong eaddr) 54fcf5ef2aSThomas Huth { 55fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 56fcf5ef2aSThomas Huth uint64_t esid_256M, esid_1T; 57fcf5ef2aSThomas Huth int n; 58fcf5ef2aSThomas Huth 59fcf5ef2aSThomas Huth LOG_SLB("%s: eaddr " TARGET_FMT_lx "\n", __func__, eaddr); 60fcf5ef2aSThomas Huth 61fcf5ef2aSThomas Huth esid_256M = (eaddr & SEGMENT_MASK_256M) | SLB_ESID_V; 62fcf5ef2aSThomas Huth esid_1T = (eaddr & SEGMENT_MASK_1T) | SLB_ESID_V; 63fcf5ef2aSThomas Huth 6467d7d66fSDavid Gibson for (n = 0; n < cpu->hash64_opts->slb_size; n++) { 65fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[n]; 66fcf5ef2aSThomas Huth 67fcf5ef2aSThomas Huth LOG_SLB("%s: slot %d %016" PRIx64 " %016" 68fcf5ef2aSThomas Huth PRIx64 "\n", __func__, n, slb->esid, slb->vsid); 69d75cbae8SDavid Gibson /* 70d75cbae8SDavid Gibson * We check for 1T matches on all MMUs here - if the MMU 71fcf5ef2aSThomas Huth * doesn't have 1T segment support, we will have prevented 1T 72d75cbae8SDavid Gibson * entries from being inserted in the slbmte code. 73d75cbae8SDavid Gibson */ 74fcf5ef2aSThomas Huth if (((slb->esid == esid_256M) && 75fcf5ef2aSThomas Huth ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_256M)) 76fcf5ef2aSThomas Huth || ((slb->esid == esid_1T) && 77fcf5ef2aSThomas Huth ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_1T))) { 78fcf5ef2aSThomas Huth return slb; 79fcf5ef2aSThomas Huth } 80fcf5ef2aSThomas Huth } 81fcf5ef2aSThomas Huth 82fcf5ef2aSThomas Huth return NULL; 83fcf5ef2aSThomas Huth } 84fcf5ef2aSThomas Huth 85fad866daSMarkus Armbruster void dump_slb(PowerPCCPU *cpu) 86fcf5ef2aSThomas Huth { 87fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 88fcf5ef2aSThomas Huth int i; 89fcf5ef2aSThomas Huth uint64_t slbe, slbv; 90fcf5ef2aSThomas Huth 91fcf5ef2aSThomas Huth cpu_synchronize_state(CPU(cpu)); 92fcf5ef2aSThomas Huth 93fad866daSMarkus Armbruster qemu_printf("SLB\tESID\t\t\tVSID\n"); 9467d7d66fSDavid Gibson for (i = 0; i < cpu->hash64_opts->slb_size; i++) { 95fcf5ef2aSThomas Huth slbe = env->slb[i].esid; 96fcf5ef2aSThomas Huth slbv = env->slb[i].vsid; 97fcf5ef2aSThomas Huth if (slbe == 0 && slbv == 0) { 98fcf5ef2aSThomas Huth continue; 99fcf5ef2aSThomas Huth } 100fad866daSMarkus Armbruster qemu_printf("%d\t0x%016" PRIx64 "\t0x%016" PRIx64 "\n", 101fcf5ef2aSThomas Huth i, slbe, slbv); 102fcf5ef2aSThomas Huth } 103fcf5ef2aSThomas Huth } 104fcf5ef2aSThomas Huth 1052b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 1062bfcb7a3SLucas Coutinho void helper_SLBIA(CPUPPCState *env, uint32_t ih) 107fcf5ef2aSThomas Huth { 108db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 1090418bf78SNicholas Piggin int starting_entry; 110fcf5ef2aSThomas Huth int n; 111fcf5ef2aSThomas Huth 112f9e3e1a3SNicholas Piggin /* 113f9e3e1a3SNicholas Piggin * slbia must always flush all TLB (which is equivalent to ERAT in ppc 114f9e3e1a3SNicholas Piggin * architecture). Matching on SLB_ESID_V is not good enough, because slbmte 115f9e3e1a3SNicholas Piggin * can overwrite a valid SLB without flushing its lookaside information. 116f9e3e1a3SNicholas Piggin * 117f9e3e1a3SNicholas Piggin * It would be possible to keep the TLB in synch with the SLB by flushing 118f9e3e1a3SNicholas Piggin * when a valid entry is overwritten by slbmte, and therefore slbia would 119f9e3e1a3SNicholas Piggin * not have to flush unless it evicts a valid SLB entry. However it is 120f9e3e1a3SNicholas Piggin * expected that slbmte is more common than slbia, and slbia is usually 121f9e3e1a3SNicholas Piggin * going to evict valid SLB entries, so that tradeoff is unlikely to be a 122f9e3e1a3SNicholas Piggin * good one. 1230418bf78SNicholas Piggin * 1240418bf78SNicholas Piggin * ISA v2.05 introduced IH field with values 0,1,2,6. These all invalidate 1250418bf78SNicholas Piggin * the same SLB entries (everything but entry 0), but differ in what 1260418bf78SNicholas Piggin * "lookaside information" is invalidated. TCG can ignore this and flush 1270418bf78SNicholas Piggin * everything. 1280418bf78SNicholas Piggin * 1290418bf78SNicholas Piggin * ISA v3.0 introduced additional values 3,4,7, which change what SLBs are 1300418bf78SNicholas Piggin * invalidated. 131f9e3e1a3SNicholas Piggin */ 132f9e3e1a3SNicholas Piggin 1330418bf78SNicholas Piggin env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH; 1340418bf78SNicholas Piggin 1350418bf78SNicholas Piggin starting_entry = 1; /* default for IH=0,1,2,6 */ 1360418bf78SNicholas Piggin 1370418bf78SNicholas Piggin if (env->mmu_model == POWERPC_MMU_3_00) { 1380418bf78SNicholas Piggin switch (ih) { 1390418bf78SNicholas Piggin case 0x7: 1400418bf78SNicholas Piggin /* invalidate no SLBs, but all lookaside information */ 1410418bf78SNicholas Piggin return; 1420418bf78SNicholas Piggin 1430418bf78SNicholas Piggin case 0x3: 1440418bf78SNicholas Piggin case 0x4: 1450418bf78SNicholas Piggin /* also considers SLB entry 0 */ 1460418bf78SNicholas Piggin starting_entry = 0; 1470418bf78SNicholas Piggin break; 1480418bf78SNicholas Piggin 1490418bf78SNicholas Piggin case 0x5: 1500418bf78SNicholas Piggin /* treat undefined values as ih==0, and warn */ 1510418bf78SNicholas Piggin qemu_log_mask(LOG_GUEST_ERROR, 1520418bf78SNicholas Piggin "slbia undefined IH field %u.\n", ih); 1530418bf78SNicholas Piggin break; 1540418bf78SNicholas Piggin 1550418bf78SNicholas Piggin default: 1560418bf78SNicholas Piggin /* 0,1,2,6 */ 1570418bf78SNicholas Piggin break; 1580418bf78SNicholas Piggin } 1590418bf78SNicholas Piggin } 1600418bf78SNicholas Piggin 1610418bf78SNicholas Piggin for (n = starting_entry; n < cpu->hash64_opts->slb_size; n++) { 162fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[n]; 163fcf5ef2aSThomas Huth 1640418bf78SNicholas Piggin if (!(slb->esid & SLB_ESID_V)) { 1650418bf78SNicholas Piggin continue; 1660418bf78SNicholas Piggin } 1670418bf78SNicholas Piggin if (env->mmu_model == POWERPC_MMU_3_00) { 1680418bf78SNicholas Piggin if (ih == 0x3 && (slb->vsid & SLB_VSID_C) == 0) { 1690418bf78SNicholas Piggin /* preserves entries with a class value of 0 */ 1700418bf78SNicholas Piggin continue; 171f9e3e1a3SNicholas Piggin } 172f9e3e1a3SNicholas Piggin } 173f9e3e1a3SNicholas Piggin 1740418bf78SNicholas Piggin slb->esid &= ~SLB_ESID_V; 1750418bf78SNicholas Piggin } 176fcf5ef2aSThomas Huth } 177fcf5ef2aSThomas Huth 178491a2553SLucas Coutinho #if defined(TARGET_PPC64) 179491a2553SLucas Coutinho void helper_SLBIAG(CPUPPCState *env, target_ulong rs, uint32_t l) 180491a2553SLucas Coutinho { 181491a2553SLucas Coutinho PowerPCCPU *cpu = env_archcpu(env); 182491a2553SLucas Coutinho int n; 183491a2553SLucas Coutinho 184491a2553SLucas Coutinho /* 185491a2553SLucas Coutinho * slbiag must always flush all TLB (which is equivalent to ERAT in ppc 186491a2553SLucas Coutinho * architecture). Matching on SLB_ESID_V is not good enough, because slbmte 187491a2553SLucas Coutinho * can overwrite a valid SLB without flushing its lookaside information. 188491a2553SLucas Coutinho * 189491a2553SLucas Coutinho * It would be possible to keep the TLB in synch with the SLB by flushing 190491a2553SLucas Coutinho * when a valid entry is overwritten by slbmte, and therefore slbiag would 191491a2553SLucas Coutinho * not have to flush unless it evicts a valid SLB entry. However it is 192491a2553SLucas Coutinho * expected that slbmte is more common than slbiag, and slbiag is usually 193491a2553SLucas Coutinho * going to evict valid SLB entries, so that tradeoff is unlikely to be a 194491a2553SLucas Coutinho * good one. 195491a2553SLucas Coutinho */ 196491a2553SLucas Coutinho env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH; 197491a2553SLucas Coutinho 198491a2553SLucas Coutinho for (n = 0; n < cpu->hash64_opts->slb_size; n++) { 199491a2553SLucas Coutinho ppc_slb_t *slb = &env->slb[n]; 200491a2553SLucas Coutinho slb->esid &= ~SLB_ESID_V; 201491a2553SLucas Coutinho } 202491a2553SLucas Coutinho } 203491a2553SLucas Coutinho #endif 204491a2553SLucas Coutinho 205a63f1dfcSNikunj A Dadhania static void __helper_slbie(CPUPPCState *env, target_ulong addr, 206a63f1dfcSNikunj A Dadhania target_ulong global) 207fcf5ef2aSThomas Huth { 208db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 209fcf5ef2aSThomas Huth ppc_slb_t *slb; 210fcf5ef2aSThomas Huth 211fcf5ef2aSThomas Huth slb = slb_lookup(cpu, addr); 212fcf5ef2aSThomas Huth if (!slb) { 213fcf5ef2aSThomas Huth return; 214fcf5ef2aSThomas Huth } 215fcf5ef2aSThomas Huth 216fcf5ef2aSThomas Huth if (slb->esid & SLB_ESID_V) { 217fcf5ef2aSThomas Huth slb->esid &= ~SLB_ESID_V; 218fcf5ef2aSThomas Huth 219d75cbae8SDavid Gibson /* 220d75cbae8SDavid Gibson * XXX: given the fact that segment size is 256 MB or 1TB, 221fcf5ef2aSThomas Huth * and we still don't have a tlb_flush_mask(env, n, mask) 222fcf5ef2aSThomas Huth * in QEMU, we just invalidate all TLBs 223fcf5ef2aSThomas Huth */ 224a63f1dfcSNikunj A Dadhania env->tlb_need_flush |= 225a63f1dfcSNikunj A Dadhania (global == false ? TLB_NEED_LOCAL_FLUSH : TLB_NEED_GLOBAL_FLUSH); 226fcf5ef2aSThomas Huth } 227fcf5ef2aSThomas Huth } 228fcf5ef2aSThomas Huth 22943507e47SLucas Coutinho void helper_SLBIE(CPUPPCState *env, target_ulong addr) 230a63f1dfcSNikunj A Dadhania { 231a63f1dfcSNikunj A Dadhania __helper_slbie(env, addr, false); 232a63f1dfcSNikunj A Dadhania } 233a63f1dfcSNikunj A Dadhania 234a1b05c06SLucas Coutinho void helper_SLBIEG(CPUPPCState *env, target_ulong addr) 235a63f1dfcSNikunj A Dadhania { 236a63f1dfcSNikunj A Dadhania __helper_slbie(env, addr, true); 237a63f1dfcSNikunj A Dadhania } 2382b44e219SBruno Larsen (billionai) #endif 239a63f1dfcSNikunj A Dadhania 240fcf5ef2aSThomas Huth int ppc_store_slb(PowerPCCPU *cpu, target_ulong slot, 241fcf5ef2aSThomas Huth target_ulong esid, target_ulong vsid) 242fcf5ef2aSThomas Huth { 243fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 244fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 245b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = NULL; 246fcf5ef2aSThomas Huth int i; 247fcf5ef2aSThomas Huth 24867d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 249fcf5ef2aSThomas Huth return -1; /* Bad slot number */ 250fcf5ef2aSThomas Huth } 251fcf5ef2aSThomas Huth if (esid & ~(SLB_ESID_ESID | SLB_ESID_V)) { 252fcf5ef2aSThomas Huth return -1; /* Reserved bits set */ 253fcf5ef2aSThomas Huth } 254fcf5ef2aSThomas Huth if (vsid & (SLB_VSID_B & ~SLB_VSID_B_1T)) { 255fcf5ef2aSThomas Huth return -1; /* Bad segment size */ 256fcf5ef2aSThomas Huth } 25758969eeeSDavid Gibson if ((vsid & SLB_VSID_B) && !(ppc_hash64_has(cpu, PPC_HASH64_1TSEG))) { 258fcf5ef2aSThomas Huth return -1; /* 1T segment on MMU that doesn't support it */ 259fcf5ef2aSThomas Huth } 260fcf5ef2aSThomas Huth 261fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 262b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i]; 263fcf5ef2aSThomas Huth 264fcf5ef2aSThomas Huth if (!sps1->page_shift) { 265fcf5ef2aSThomas Huth break; 266fcf5ef2aSThomas Huth } 267fcf5ef2aSThomas Huth 268fcf5ef2aSThomas Huth if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) { 269fcf5ef2aSThomas Huth sps = sps1; 270fcf5ef2aSThomas Huth break; 271fcf5ef2aSThomas Huth } 272fcf5ef2aSThomas Huth } 273fcf5ef2aSThomas Huth 274fcf5ef2aSThomas Huth if (!sps) { 275fcf5ef2aSThomas Huth error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu 276fcf5ef2aSThomas Huth " esid 0x"TARGET_FMT_lx" vsid 0x"TARGET_FMT_lx, 277fcf5ef2aSThomas Huth slot, esid, vsid); 278fcf5ef2aSThomas Huth return -1; 279fcf5ef2aSThomas Huth } 280fcf5ef2aSThomas Huth 281fcf5ef2aSThomas Huth slb->esid = esid; 282fcf5ef2aSThomas Huth slb->vsid = vsid; 283fcf5ef2aSThomas Huth slb->sps = sps; 284fcf5ef2aSThomas Huth 28576134d48SSuraj Jitindar Singh LOG_SLB("%s: " TARGET_FMT_lu " " TARGET_FMT_lx " - " TARGET_FMT_lx 28676134d48SSuraj Jitindar Singh " => %016" PRIx64 " %016" PRIx64 "\n", __func__, slot, esid, vsid, 287fcf5ef2aSThomas Huth slb->esid, slb->vsid); 288fcf5ef2aSThomas Huth 289fcf5ef2aSThomas Huth return 0; 290fcf5ef2aSThomas Huth } 291fcf5ef2aSThomas Huth 2922b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 293fcf5ef2aSThomas Huth static int ppc_load_slb_esid(PowerPCCPU *cpu, target_ulong rb, 294fcf5ef2aSThomas Huth target_ulong *rt) 295fcf5ef2aSThomas Huth { 296fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 297fcf5ef2aSThomas Huth int slot = rb & 0xfff; 298fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 299fcf5ef2aSThomas Huth 30067d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 301fcf5ef2aSThomas Huth return -1; 302fcf5ef2aSThomas Huth } 303fcf5ef2aSThomas Huth 304fcf5ef2aSThomas Huth *rt = slb->esid; 305fcf5ef2aSThomas Huth return 0; 306fcf5ef2aSThomas Huth } 307fcf5ef2aSThomas Huth 308fcf5ef2aSThomas Huth static int ppc_load_slb_vsid(PowerPCCPU *cpu, target_ulong rb, 309fcf5ef2aSThomas Huth target_ulong *rt) 310fcf5ef2aSThomas Huth { 311fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 312fcf5ef2aSThomas Huth int slot = rb & 0xfff; 313fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 314fcf5ef2aSThomas Huth 31567d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 316fcf5ef2aSThomas Huth return -1; 317fcf5ef2aSThomas Huth } 318fcf5ef2aSThomas Huth 319fcf5ef2aSThomas Huth *rt = slb->vsid; 320fcf5ef2aSThomas Huth return 0; 321fcf5ef2aSThomas Huth } 322fcf5ef2aSThomas Huth 323fcf5ef2aSThomas Huth static int ppc_find_slb_vsid(PowerPCCPU *cpu, target_ulong rb, 324fcf5ef2aSThomas Huth target_ulong *rt) 325fcf5ef2aSThomas Huth { 326fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 327fcf5ef2aSThomas Huth ppc_slb_t *slb; 328fcf5ef2aSThomas Huth 329fcf5ef2aSThomas Huth if (!msr_is_64bit(env, env->msr)) { 330fcf5ef2aSThomas Huth rb &= 0xffffffff; 331fcf5ef2aSThomas Huth } 332fcf5ef2aSThomas Huth slb = slb_lookup(cpu, rb); 333fcf5ef2aSThomas Huth if (slb == NULL) { 334fcf5ef2aSThomas Huth *rt = (target_ulong)-1ul; 335fcf5ef2aSThomas Huth } else { 336fcf5ef2aSThomas Huth *rt = slb->vsid; 337fcf5ef2aSThomas Huth } 338fcf5ef2aSThomas Huth return 0; 339fcf5ef2aSThomas Huth } 340fcf5ef2aSThomas Huth 3410b0ba40fSLucas Coutinho void helper_SLBMTE(CPUPPCState *env, target_ulong rb, target_ulong rs) 342fcf5ef2aSThomas Huth { 343db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 344fcf5ef2aSThomas Huth 345fcf5ef2aSThomas Huth if (ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs) < 0) { 346fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 347fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 348fcf5ef2aSThomas Huth } 349fcf5ef2aSThomas Huth } 350fcf5ef2aSThomas Huth 35141b60e46SLucas Coutinho target_ulong helper_SLBMFEE(CPUPPCState *env, target_ulong rb) 352fcf5ef2aSThomas Huth { 353db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 354fcf5ef2aSThomas Huth target_ulong rt = 0; 355fcf5ef2aSThomas Huth 356fcf5ef2aSThomas Huth if (ppc_load_slb_esid(cpu, rb, &rt) < 0) { 357fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 358fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 359fcf5ef2aSThomas Huth } 360fcf5ef2aSThomas Huth return rt; 361fcf5ef2aSThomas Huth } 362fcf5ef2aSThomas Huth 36326d02c9dSLucas Coutinho target_ulong helper_SLBFEE(CPUPPCState *env, target_ulong rb) 364fcf5ef2aSThomas Huth { 365db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 366fcf5ef2aSThomas Huth target_ulong rt = 0; 367fcf5ef2aSThomas Huth 368fcf5ef2aSThomas Huth if (ppc_find_slb_vsid(cpu, rb, &rt) < 0) { 369fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 370fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 371fcf5ef2aSThomas Huth } 372fcf5ef2aSThomas Huth return rt; 373fcf5ef2aSThomas Huth } 374fcf5ef2aSThomas Huth 37574a15384SLucas Coutinho target_ulong helper_SLBMFEV(CPUPPCState *env, target_ulong rb) 376fcf5ef2aSThomas Huth { 377db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 378fcf5ef2aSThomas Huth target_ulong rt = 0; 379fcf5ef2aSThomas Huth 380fcf5ef2aSThomas Huth if (ppc_load_slb_vsid(cpu, rb, &rt) < 0) { 381fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 382fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 383fcf5ef2aSThomas Huth } 384fcf5ef2aSThomas Huth return rt; 385fcf5ef2aSThomas Huth } 3862b44e219SBruno Larsen (billionai) #endif 387fcf5ef2aSThomas Huth 38807a68f99SSuraj Jitindar Singh /* Check No-Execute or Guarded Storage */ 38907a68f99SSuraj Jitindar Singh static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU *cpu, 39007a68f99SSuraj Jitindar Singh ppc_hash_pte64_t pte) 39107a68f99SSuraj Jitindar Singh { 39207a68f99SSuraj Jitindar Singh /* Exec permissions CANNOT take away read or write permissions */ 39307a68f99SSuraj Jitindar Singh return (pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G) ? 39407a68f99SSuraj Jitindar Singh PAGE_READ | PAGE_WRITE : PAGE_READ | PAGE_WRITE | PAGE_EXEC; 39507a68f99SSuraj Jitindar Singh } 39607a68f99SSuraj Jitindar Singh 39707a68f99SSuraj Jitindar Singh /* Check Basic Storage Protection */ 39803695a98SBruno Larsen (billionai) static int ppc_hash64_pte_prot(int mmu_idx, 399fcf5ef2aSThomas Huth ppc_slb_t *slb, ppc_hash_pte64_t pte) 400fcf5ef2aSThomas Huth { 401fcf5ef2aSThomas Huth unsigned pp, key; 402d75cbae8SDavid Gibson /* 403d75cbae8SDavid Gibson * Some pp bit combinations have undefined behaviour, so default 404d75cbae8SDavid Gibson * to no access in those cases 405d75cbae8SDavid Gibson */ 406fcf5ef2aSThomas Huth int prot = 0; 407fcf5ef2aSThomas Huth 40803695a98SBruno Larsen (billionai) key = !!(mmuidx_pr(mmu_idx) ? (slb->vsid & SLB_VSID_KP) 409fcf5ef2aSThomas Huth : (slb->vsid & SLB_VSID_KS)); 410fcf5ef2aSThomas Huth pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61); 411fcf5ef2aSThomas Huth 412fcf5ef2aSThomas Huth if (key == 0) { 413fcf5ef2aSThomas Huth switch (pp) { 414fcf5ef2aSThomas Huth case 0x0: 415fcf5ef2aSThomas Huth case 0x1: 416fcf5ef2aSThomas Huth case 0x2: 417347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 418fcf5ef2aSThomas Huth break; 419fcf5ef2aSThomas Huth 420fcf5ef2aSThomas Huth case 0x3: 421fcf5ef2aSThomas Huth case 0x6: 422347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_EXEC; 423fcf5ef2aSThomas Huth break; 424fcf5ef2aSThomas Huth } 425fcf5ef2aSThomas Huth } else { 426fcf5ef2aSThomas Huth switch (pp) { 427fcf5ef2aSThomas Huth case 0x0: 428fcf5ef2aSThomas Huth case 0x6: 429fcf5ef2aSThomas Huth break; 430fcf5ef2aSThomas Huth 431fcf5ef2aSThomas Huth case 0x1: 432fcf5ef2aSThomas Huth case 0x3: 433347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_EXEC; 434fcf5ef2aSThomas Huth break; 435fcf5ef2aSThomas Huth 436fcf5ef2aSThomas Huth case 0x2: 437347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 438fcf5ef2aSThomas Huth break; 439fcf5ef2aSThomas Huth } 440fcf5ef2aSThomas Huth } 441fcf5ef2aSThomas Huth 442fcf5ef2aSThomas Huth return prot; 443fcf5ef2aSThomas Huth } 444fcf5ef2aSThomas Huth 445a6152b52SSuraj Jitindar Singh /* Check the instruction access permissions specified in the IAMR */ 446a6152b52SSuraj Jitindar Singh static int ppc_hash64_iamr_prot(PowerPCCPU *cpu, int key) 447a6152b52SSuraj Jitindar Singh { 448a6152b52SSuraj Jitindar Singh CPUPPCState *env = &cpu->env; 449a6152b52SSuraj Jitindar Singh int iamr_bits = (env->spr[SPR_IAMR] >> 2 * (31 - key)) & 0x3; 450a6152b52SSuraj Jitindar Singh 451a6152b52SSuraj Jitindar Singh /* 452a6152b52SSuraj Jitindar Singh * An instruction fetch is permitted if the IAMR bit is 0. 453a6152b52SSuraj Jitindar Singh * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit 454a6152b52SSuraj Jitindar Singh * can only take away EXEC permissions not READ or WRITE permissions. 455a6152b52SSuraj Jitindar Singh * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since 456a6152b52SSuraj Jitindar Singh * EXEC permissions are allowed. 457a6152b52SSuraj Jitindar Singh */ 458a6152b52SSuraj Jitindar Singh return (iamr_bits & 0x1) ? PAGE_READ | PAGE_WRITE : 459a6152b52SSuraj Jitindar Singh PAGE_READ | PAGE_WRITE | PAGE_EXEC; 460a6152b52SSuraj Jitindar Singh } 461a6152b52SSuraj Jitindar Singh 462fcf5ef2aSThomas Huth static int ppc_hash64_amr_prot(PowerPCCPU *cpu, ppc_hash_pte64_t pte) 463fcf5ef2aSThomas Huth { 464fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 465fcf5ef2aSThomas Huth int key, amrbits; 466fcf5ef2aSThomas Huth int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 467fcf5ef2aSThomas Huth 468fcf5ef2aSThomas Huth /* Only recent MMUs implement Virtual Page Class Key Protection */ 46958969eeeSDavid Gibson if (!ppc_hash64_has(cpu, PPC_HASH64_AMR)) { 470fcf5ef2aSThomas Huth return prot; 471fcf5ef2aSThomas Huth } 472fcf5ef2aSThomas Huth 473fcf5ef2aSThomas Huth key = HPTE64_R_KEY(pte.pte1); 474fcf5ef2aSThomas Huth amrbits = (env->spr[SPR_AMR] >> 2 * (31 - key)) & 0x3; 475fcf5ef2aSThomas Huth 476fcf5ef2aSThomas Huth /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */ 477fcf5ef2aSThomas Huth /* env->spr[SPR_AMR]); */ 478fcf5ef2aSThomas Huth 479fcf5ef2aSThomas Huth /* 480fcf5ef2aSThomas Huth * A store is permitted if the AMR bit is 0. Remove write 481fcf5ef2aSThomas Huth * protection if it is set. 482fcf5ef2aSThomas Huth */ 483fcf5ef2aSThomas Huth if (amrbits & 0x2) { 484fcf5ef2aSThomas Huth prot &= ~PAGE_WRITE; 485fcf5ef2aSThomas Huth } 486fcf5ef2aSThomas Huth /* 487fcf5ef2aSThomas Huth * A load is permitted if the AMR bit is 0. Remove read 488fcf5ef2aSThomas Huth * protection if it is set. 489fcf5ef2aSThomas Huth */ 490fcf5ef2aSThomas Huth if (amrbits & 0x1) { 491fcf5ef2aSThomas Huth prot &= ~PAGE_READ; 492fcf5ef2aSThomas Huth } 493fcf5ef2aSThomas Huth 494a6152b52SSuraj Jitindar Singh switch (env->mmu_model) { 495a6152b52SSuraj Jitindar Singh /* 496a6152b52SSuraj Jitindar Singh * MMU version 2.07 and later support IAMR 497a6152b52SSuraj Jitindar Singh * Check if the IAMR allows the instruction access - it will return 498a6152b52SSuraj Jitindar Singh * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0 499a6152b52SSuraj Jitindar Singh * if it does (and prot will be unchanged indicating execution support). 500a6152b52SSuraj Jitindar Singh */ 501a6152b52SSuraj Jitindar Singh case POWERPC_MMU_2_07: 502a6152b52SSuraj Jitindar Singh case POWERPC_MMU_3_00: 503a6152b52SSuraj Jitindar Singh prot &= ppc_hash64_iamr_prot(cpu, key); 504a6152b52SSuraj Jitindar Singh break; 505a6152b52SSuraj Jitindar Singh default: 506a6152b52SSuraj Jitindar Singh break; 507a6152b52SSuraj Jitindar Singh } 508a6152b52SSuraj Jitindar Singh 509fcf5ef2aSThomas Huth return prot; 510fcf5ef2aSThomas Huth } 511fcf5ef2aSThomas Huth 51214a43ab3SBALATON Zoltan static hwaddr ppc_hash64_hpt_base(PowerPCCPU *cpu) 51314a43ab3SBALATON Zoltan { 51414a43ab3SBALATON Zoltan uint64_t base; 51514a43ab3SBALATON Zoltan 51614a43ab3SBALATON Zoltan if (cpu->vhyp) { 51714a43ab3SBALATON Zoltan return 0; 51814a43ab3SBALATON Zoltan } 51914a43ab3SBALATON Zoltan if (cpu->env.mmu_model == POWERPC_MMU_3_00) { 52014a43ab3SBALATON Zoltan ppc_v3_pate_t pate; 52114a43ab3SBALATON Zoltan 52214a43ab3SBALATON Zoltan if (!ppc64_v3_get_pate(cpu, cpu->env.spr[SPR_LPIDR], &pate)) { 52314a43ab3SBALATON Zoltan return 0; 52414a43ab3SBALATON Zoltan } 52514a43ab3SBALATON Zoltan base = pate.dw0; 52614a43ab3SBALATON Zoltan } else { 52714a43ab3SBALATON Zoltan base = cpu->env.spr[SPR_SDR1]; 52814a43ab3SBALATON Zoltan } 52914a43ab3SBALATON Zoltan return base & SDR_64_HTABORG; 53014a43ab3SBALATON Zoltan } 53114a43ab3SBALATON Zoltan 53214a43ab3SBALATON Zoltan static hwaddr ppc_hash64_hpt_mask(PowerPCCPU *cpu) 53314a43ab3SBALATON Zoltan { 53414a43ab3SBALATON Zoltan uint64_t base; 53514a43ab3SBALATON Zoltan 53614a43ab3SBALATON Zoltan if (cpu->vhyp) { 53714a43ab3SBALATON Zoltan return cpu->vhyp_class->hpt_mask(cpu->vhyp); 53814a43ab3SBALATON Zoltan } 53914a43ab3SBALATON Zoltan if (cpu->env.mmu_model == POWERPC_MMU_3_00) { 54014a43ab3SBALATON Zoltan ppc_v3_pate_t pate; 54114a43ab3SBALATON Zoltan 54214a43ab3SBALATON Zoltan if (!ppc64_v3_get_pate(cpu, cpu->env.spr[SPR_LPIDR], &pate)) { 54314a43ab3SBALATON Zoltan return 0; 54414a43ab3SBALATON Zoltan } 54514a43ab3SBALATON Zoltan base = pate.dw0; 54614a43ab3SBALATON Zoltan } else { 54714a43ab3SBALATON Zoltan base = cpu->env.spr[SPR_SDR1]; 54814a43ab3SBALATON Zoltan } 54914a43ab3SBALATON Zoltan return (1ULL << ((base & SDR_64_HTABSIZE) + 18 - 7)) - 1; 55014a43ab3SBALATON Zoltan } 55114a43ab3SBALATON Zoltan 5527222b94aSDavid Gibson const ppc_hash_pte64_t *ppc_hash64_map_hptes(PowerPCCPU *cpu, 5537222b94aSDavid Gibson hwaddr ptex, int n) 554fcf5ef2aSThomas Huth { 5557222b94aSDavid Gibson hwaddr pte_offset = ptex * HASH_PTE_SIZE_64; 5563367c62fSBenjamin Herrenschmidt hwaddr base; 5577222b94aSDavid Gibson hwaddr plen = n * HASH_PTE_SIZE_64; 558e57ca75cSDavid Gibson const ppc_hash_pte64_t *hptes; 559e57ca75cSDavid Gibson 560e57ca75cSDavid Gibson if (cpu->vhyp) { 561c700b5e1SNicholas Piggin return cpu->vhyp_class->map_hptes(cpu->vhyp, ptex, n); 562e57ca75cSDavid Gibson } 5633367c62fSBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 564e57ca75cSDavid Gibson 565e57ca75cSDavid Gibson if (!base) { 566e57ca75cSDavid Gibson return NULL; 567e57ca75cSDavid Gibson } 568e57ca75cSDavid Gibson 569f26404fbSPeter Maydell hptes = address_space_map(CPU(cpu)->as, base + pte_offset, &plen, false, 570f26404fbSPeter Maydell MEMTXATTRS_UNSPECIFIED); 5717222b94aSDavid Gibson if (plen < (n * HASH_PTE_SIZE_64)) { 5727222b94aSDavid Gibson hw_error("%s: Unable to map all requested HPTEs\n", __func__); 573fcf5ef2aSThomas Huth } 5747222b94aSDavid Gibson return hptes; 575fcf5ef2aSThomas Huth } 576fcf5ef2aSThomas Huth 5777222b94aSDavid Gibson void ppc_hash64_unmap_hptes(PowerPCCPU *cpu, const ppc_hash_pte64_t *hptes, 5787222b94aSDavid Gibson hwaddr ptex, int n) 579fcf5ef2aSThomas Huth { 580e57ca75cSDavid Gibson if (cpu->vhyp) { 581c700b5e1SNicholas Piggin cpu->vhyp_class->unmap_hptes(cpu->vhyp, hptes, ptex, n); 582e57ca75cSDavid Gibson return; 583e57ca75cSDavid Gibson } 584e57ca75cSDavid Gibson 5857222b94aSDavid Gibson address_space_unmap(CPU(cpu)->as, (void *)hptes, n * HASH_PTE_SIZE_64, 5867222b94aSDavid Gibson false, n * HASH_PTE_SIZE_64); 587fcf5ef2aSThomas Huth } 588fcf5ef2aSThomas Huth 58914a43ab3SBALATON Zoltan bool ppc_hash64_valid_ptex(PowerPCCPU *cpu, target_ulong ptex) 59014a43ab3SBALATON Zoltan { 59114a43ab3SBALATON Zoltan /* hash value/pteg group index is normalized by HPT mask */ 59214a43ab3SBALATON Zoltan if (((ptex & ~7ULL) / HPTES_PER_GROUP) & ~ppc_hash64_hpt_mask(cpu)) { 59314a43ab3SBALATON Zoltan return false; 59414a43ab3SBALATON Zoltan } 59514a43ab3SBALATON Zoltan return true; 59614a43ab3SBALATON Zoltan } 59714a43ab3SBALATON Zoltan 598b07c59f7SDavid Gibson static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes *sps, 599fcf5ef2aSThomas Huth uint64_t pte0, uint64_t pte1) 600fcf5ef2aSThomas Huth { 601fcf5ef2aSThomas Huth int i; 602fcf5ef2aSThomas Huth 603fcf5ef2aSThomas Huth if (!(pte0 & HPTE64_V_LARGE)) { 604fcf5ef2aSThomas Huth if (sps->page_shift != 12) { 605fcf5ef2aSThomas Huth /* 4kiB page in a non 4kiB segment */ 606fcf5ef2aSThomas Huth return 0; 607fcf5ef2aSThomas Huth } 608fcf5ef2aSThomas Huth /* Normal 4kiB page */ 609fcf5ef2aSThomas Huth return 12; 610fcf5ef2aSThomas Huth } 611fcf5ef2aSThomas Huth 612fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 613b07c59f7SDavid Gibson const PPCHash64PageSize *ps = &sps->enc[i]; 614fcf5ef2aSThomas Huth uint64_t mask; 615fcf5ef2aSThomas Huth 616fcf5ef2aSThomas Huth if (!ps->page_shift) { 617fcf5ef2aSThomas Huth break; 618fcf5ef2aSThomas Huth } 619fcf5ef2aSThomas Huth 620fcf5ef2aSThomas Huth if (ps->page_shift == 12) { 621fcf5ef2aSThomas Huth /* L bit is set so this can't be a 4kiB page */ 622fcf5ef2aSThomas Huth continue; 623fcf5ef2aSThomas Huth } 624fcf5ef2aSThomas Huth 625fcf5ef2aSThomas Huth mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN; 626fcf5ef2aSThomas Huth 627fcf5ef2aSThomas Huth if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) { 628fcf5ef2aSThomas Huth return ps->page_shift; 629fcf5ef2aSThomas Huth } 630fcf5ef2aSThomas Huth } 631fcf5ef2aSThomas Huth 632fcf5ef2aSThomas Huth return 0; /* Bad page size encoding */ 633fcf5ef2aSThomas Huth } 634fcf5ef2aSThomas Huth 63534525595SBenjamin Herrenschmidt static void ppc64_v3_new_to_old_hpte(target_ulong *pte0, target_ulong *pte1) 63634525595SBenjamin Herrenschmidt { 63734525595SBenjamin Herrenschmidt /* Insert B into pte0 */ 63834525595SBenjamin Herrenschmidt *pte0 = (*pte0 & HPTE64_V_COMMON_BITS) | 63934525595SBenjamin Herrenschmidt ((*pte1 & HPTE64_R_3_0_SSIZE_MASK) << 64034525595SBenjamin Herrenschmidt (HPTE64_V_SSIZE_SHIFT - HPTE64_R_3_0_SSIZE_SHIFT)); 64134525595SBenjamin Herrenschmidt 64234525595SBenjamin Herrenschmidt /* Remove B from pte1 */ 64334525595SBenjamin Herrenschmidt *pte1 = *pte1 & ~HPTE64_R_3_0_SSIZE_MASK; 64434525595SBenjamin Herrenschmidt } 64534525595SBenjamin Herrenschmidt 64634525595SBenjamin Herrenschmidt 647fcf5ef2aSThomas Huth static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash, 648b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps, 649fcf5ef2aSThomas Huth target_ulong ptem, 650fcf5ef2aSThomas Huth ppc_hash_pte64_t *pte, unsigned *pshift) 651fcf5ef2aSThomas Huth { 652fcf5ef2aSThomas Huth int i; 6537222b94aSDavid Gibson const ppc_hash_pte64_t *pteg; 654fcf5ef2aSThomas Huth target_ulong pte0, pte1; 6557222b94aSDavid Gibson target_ulong ptex; 656fcf5ef2aSThomas Huth 65736778660SDavid Gibson ptex = (hash & ppc_hash64_hpt_mask(cpu)) * HPTES_PER_GROUP; 6587222b94aSDavid Gibson pteg = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP); 6597222b94aSDavid Gibson if (!pteg) { 660fcf5ef2aSThomas Huth return -1; 661fcf5ef2aSThomas Huth } 662fcf5ef2aSThomas Huth for (i = 0; i < HPTES_PER_GROUP; i++) { 6637222b94aSDavid Gibson pte0 = ppc_hash64_hpte0(cpu, pteg, i); 6643054b0caSBenjamin Herrenschmidt /* 6653054b0caSBenjamin Herrenschmidt * pte0 contains the valid bit and must be read before pte1, 6663054b0caSBenjamin Herrenschmidt * otherwise we might see an old pte1 with a new valid bit and 6673054b0caSBenjamin Herrenschmidt * thus an inconsistent hpte value 6683054b0caSBenjamin Herrenschmidt */ 6693054b0caSBenjamin Herrenschmidt smp_rmb(); 6707222b94aSDavid Gibson pte1 = ppc_hash64_hpte1(cpu, pteg, i); 671fcf5ef2aSThomas Huth 67234525595SBenjamin Herrenschmidt /* Convert format if necessary */ 67334525595SBenjamin Herrenschmidt if (cpu->env.mmu_model == POWERPC_MMU_3_00 && !cpu->vhyp) { 67434525595SBenjamin Herrenschmidt ppc64_v3_new_to_old_hpte(&pte0, &pte1); 67534525595SBenjamin Herrenschmidt } 67634525595SBenjamin Herrenschmidt 677fcf5ef2aSThomas Huth /* This compares V, B, H (secondary) and the AVPN */ 678fcf5ef2aSThomas Huth if (HPTE64_V_COMPARE(pte0, ptem)) { 679fcf5ef2aSThomas Huth *pshift = hpte_page_shift(sps, pte0, pte1); 680fcf5ef2aSThomas Huth /* 681fcf5ef2aSThomas Huth * If there is no match, ignore the PTE, it could simply 682fcf5ef2aSThomas Huth * be for a different segment size encoding and the 683fcf5ef2aSThomas Huth * architecture specifies we should not match. Linux will 684fcf5ef2aSThomas Huth * potentially leave behind PTEs for the wrong base page 685fcf5ef2aSThomas Huth * size when demoting segments. 686fcf5ef2aSThomas Huth */ 687fcf5ef2aSThomas Huth if (*pshift == 0) { 688fcf5ef2aSThomas Huth continue; 689fcf5ef2aSThomas Huth } 690d75cbae8SDavid Gibson /* 691d75cbae8SDavid Gibson * We don't do anything with pshift yet as qemu TLB only 692d75cbae8SDavid Gibson * deals with 4K pages anyway 693fcf5ef2aSThomas Huth */ 694fcf5ef2aSThomas Huth pte->pte0 = pte0; 695fcf5ef2aSThomas Huth pte->pte1 = pte1; 6967222b94aSDavid Gibson ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP); 6977222b94aSDavid Gibson return ptex + i; 698fcf5ef2aSThomas Huth } 699fcf5ef2aSThomas Huth } 7007222b94aSDavid Gibson ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP); 701fcf5ef2aSThomas Huth /* 702fcf5ef2aSThomas Huth * We didn't find a valid entry. 703fcf5ef2aSThomas Huth */ 704fcf5ef2aSThomas Huth return -1; 705fcf5ef2aSThomas Huth } 706fcf5ef2aSThomas Huth 707fcf5ef2aSThomas Huth static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu, 708fcf5ef2aSThomas Huth ppc_slb_t *slb, target_ulong eaddr, 709fcf5ef2aSThomas Huth ppc_hash_pte64_t *pte, unsigned *pshift) 710fcf5ef2aSThomas Huth { 711fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 7127222b94aSDavid Gibson hwaddr hash, ptex; 713fcf5ef2aSThomas Huth uint64_t vsid, epnmask, epn, ptem; 714b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = slb->sps; 715fcf5ef2aSThomas Huth 716d75cbae8SDavid Gibson /* 717d75cbae8SDavid Gibson * The SLB store path should prevent any bad page size encodings 718d75cbae8SDavid Gibson * getting in there, so: 719d75cbae8SDavid Gibson */ 720fcf5ef2aSThomas Huth assert(sps); 721fcf5ef2aSThomas Huth 722fcf5ef2aSThomas Huth /* If ISL is set in LPCR we need to clamp the page size to 4K */ 723fcf5ef2aSThomas Huth if (env->spr[SPR_LPCR] & LPCR_ISL) { 724fcf5ef2aSThomas Huth /* We assume that when using TCG, 4k is first entry of SPS */ 725b07c59f7SDavid Gibson sps = &cpu->hash64_opts->sps[0]; 726fcf5ef2aSThomas Huth assert(sps->page_shift == 12); 727fcf5ef2aSThomas Huth } 728fcf5ef2aSThomas Huth 729fcf5ef2aSThomas Huth epnmask = ~((1ULL << sps->page_shift) - 1); 730fcf5ef2aSThomas Huth 731fcf5ef2aSThomas Huth if (slb->vsid & SLB_VSID_B) { 732fcf5ef2aSThomas Huth /* 1TB segment */ 733fcf5ef2aSThomas Huth vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T; 734fcf5ef2aSThomas Huth epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask; 735fcf5ef2aSThomas Huth hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift); 736fcf5ef2aSThomas Huth } else { 737fcf5ef2aSThomas Huth /* 256M segment */ 738fcf5ef2aSThomas Huth vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT; 739fcf5ef2aSThomas Huth epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask; 740fcf5ef2aSThomas Huth hash = vsid ^ (epn >> sps->page_shift); 741fcf5ef2aSThomas Huth } 742fcf5ef2aSThomas Huth ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN); 743fcf5ef2aSThomas Huth ptem |= HPTE64_V_VALID; 744fcf5ef2aSThomas Huth 745fcf5ef2aSThomas Huth /* Page address translation */ 746fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 747883f2c59SPhilippe Mathieu-Daudé "htab_base " HWADDR_FMT_plx " htab_mask " HWADDR_FMT_plx 748883f2c59SPhilippe Mathieu-Daudé " hash " HWADDR_FMT_plx "\n", 74936778660SDavid Gibson ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), hash); 750fcf5ef2aSThomas Huth 751fcf5ef2aSThomas Huth /* Primary PTEG lookup */ 752fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 753883f2c59SPhilippe Mathieu-Daudé "0 htab=" HWADDR_FMT_plx "/" HWADDR_FMT_plx 754fcf5ef2aSThomas Huth " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx 755883f2c59SPhilippe Mathieu-Daudé " hash=" HWADDR_FMT_plx "\n", 75636778660SDavid Gibson ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), 75736778660SDavid Gibson vsid, ptem, hash); 7587222b94aSDavid Gibson ptex = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift); 759fcf5ef2aSThomas Huth 7607222b94aSDavid Gibson if (ptex == -1) { 761fcf5ef2aSThomas Huth /* Secondary PTEG lookup */ 762fcf5ef2aSThomas Huth ptem |= HPTE64_V_SECONDARY; 763fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 764883f2c59SPhilippe Mathieu-Daudé "1 htab=" HWADDR_FMT_plx "/" HWADDR_FMT_plx 765fcf5ef2aSThomas Huth " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx 766883f2c59SPhilippe Mathieu-Daudé " hash=" HWADDR_FMT_plx "\n", ppc_hash64_hpt_base(cpu), 76736778660SDavid Gibson ppc_hash64_hpt_mask(cpu), vsid, ptem, ~hash); 768fcf5ef2aSThomas Huth 7697222b94aSDavid Gibson ptex = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift); 770fcf5ef2aSThomas Huth } 771fcf5ef2aSThomas Huth 7727222b94aSDavid Gibson return ptex; 773fcf5ef2aSThomas Huth } 774fcf5ef2aSThomas Huth 775fcf5ef2aSThomas Huth unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu, 776fcf5ef2aSThomas Huth uint64_t pte0, uint64_t pte1) 777fcf5ef2aSThomas Huth { 778fcf5ef2aSThomas Huth int i; 779fcf5ef2aSThomas Huth 780fcf5ef2aSThomas Huth if (!(pte0 & HPTE64_V_LARGE)) { 781fcf5ef2aSThomas Huth return 12; 782fcf5ef2aSThomas Huth } 783fcf5ef2aSThomas Huth 784fcf5ef2aSThomas Huth /* 785fcf5ef2aSThomas Huth * The encodings in env->sps need to be carefully chosen so that 786fcf5ef2aSThomas Huth * this gives an unambiguous result. 787fcf5ef2aSThomas Huth */ 788fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 789b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i]; 790fcf5ef2aSThomas Huth unsigned shift; 791fcf5ef2aSThomas Huth 792fcf5ef2aSThomas Huth if (!sps->page_shift) { 793fcf5ef2aSThomas Huth break; 794fcf5ef2aSThomas Huth } 795fcf5ef2aSThomas Huth 796fcf5ef2aSThomas Huth shift = hpte_page_shift(sps, pte0, pte1); 797fcf5ef2aSThomas Huth if (shift) { 798fcf5ef2aSThomas Huth return shift; 799fcf5ef2aSThomas Huth } 800fcf5ef2aSThomas Huth } 801fcf5ef2aSThomas Huth 802fcf5ef2aSThomas Huth return 0; 803fcf5ef2aSThomas Huth } 804fcf5ef2aSThomas Huth 8051b99e029SDavid Gibson static bool ppc_hash64_use_vrma(CPUPPCState *env) 8061b99e029SDavid Gibson { 8071b99e029SDavid Gibson switch (env->mmu_model) { 8081b99e029SDavid Gibson case POWERPC_MMU_3_00: 8091b99e029SDavid Gibson /* 8101b99e029SDavid Gibson * ISAv3.0 (POWER9) always uses VRMA, the VPM0 field and RMOR 8111b99e029SDavid Gibson * register no longer exist 8121b99e029SDavid Gibson */ 8131b99e029SDavid Gibson return true; 8141b99e029SDavid Gibson 8151b99e029SDavid Gibson default: 8161b99e029SDavid Gibson return !!(env->spr[SPR_LPCR] & LPCR_VPM0); 8171b99e029SDavid Gibson } 8181b99e029SDavid Gibson } 8191b99e029SDavid Gibson 8209201af09SNicholas Piggin static void ppc_hash64_set_isi(CPUState *cs, int mmu_idx, uint64_t slb_vsid, 8219201af09SNicholas Piggin uint64_t error_code) 822fcf5ef2aSThomas Huth { 8238fe08facSDavid Gibson CPUPPCState *env = &POWERPC_CPU(cs)->env; 824fcf5ef2aSThomas Huth bool vpm; 825fcf5ef2aSThomas Huth 82603695a98SBruno Larsen (billionai) if (!mmuidx_real(mmu_idx)) { 827fcf5ef2aSThomas Huth vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1); 828fcf5ef2aSThomas Huth } else { 8291b99e029SDavid Gibson vpm = ppc_hash64_use_vrma(env); 830fcf5ef2aSThomas Huth } 83103695a98SBruno Larsen (billionai) if (vpm && !mmuidx_hv(mmu_idx)) { 832fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_HISI; 8339201af09SNicholas Piggin env->spr[SPR_ASDR] = slb_vsid; 834fcf5ef2aSThomas Huth } else { 835fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_ISI; 836fcf5ef2aSThomas Huth } 837fcf5ef2aSThomas Huth env->error_code = error_code; 838fcf5ef2aSThomas Huth } 839fcf5ef2aSThomas Huth 8409201af09SNicholas Piggin static void ppc_hash64_set_dsi(CPUState *cs, int mmu_idx, uint64_t slb_vsid, 8419201af09SNicholas Piggin uint64_t dar, uint64_t dsisr) 842fcf5ef2aSThomas Huth { 8438fe08facSDavid Gibson CPUPPCState *env = &POWERPC_CPU(cs)->env; 844fcf5ef2aSThomas Huth bool vpm; 845fcf5ef2aSThomas Huth 84603695a98SBruno Larsen (billionai) if (!mmuidx_real(mmu_idx)) { 847fcf5ef2aSThomas Huth vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1); 848fcf5ef2aSThomas Huth } else { 8491b99e029SDavid Gibson vpm = ppc_hash64_use_vrma(env); 850fcf5ef2aSThomas Huth } 85103695a98SBruno Larsen (billionai) if (vpm && !mmuidx_hv(mmu_idx)) { 852fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_HDSI; 853fcf5ef2aSThomas Huth env->spr[SPR_HDAR] = dar; 854fcf5ef2aSThomas Huth env->spr[SPR_HDSISR] = dsisr; 8559201af09SNicholas Piggin env->spr[SPR_ASDR] = slb_vsid; 856fcf5ef2aSThomas Huth } else { 857fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_DSI; 858fcf5ef2aSThomas Huth env->spr[SPR_DAR] = dar; 859fcf5ef2aSThomas Huth env->spr[SPR_DSISR] = dsisr; 860fcf5ef2aSThomas Huth } 861fcf5ef2aSThomas Huth env->error_code = 0; 862fcf5ef2aSThomas Huth } 863fcf5ef2aSThomas Huth 864fcf5ef2aSThomas Huth 865a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_r(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1) 866a2dd4e83SBenjamin Herrenschmidt { 8677bf00dfbSLeandro Lupori hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + HPTE64_DW1_R; 868a2dd4e83SBenjamin Herrenschmidt 869a2dd4e83SBenjamin Herrenschmidt if (cpu->vhyp) { 870c700b5e1SNicholas Piggin cpu->vhyp_class->hpte_set_r(cpu->vhyp, ptex, pte1); 871a2dd4e83SBenjamin Herrenschmidt return; 872a2dd4e83SBenjamin Herrenschmidt } 873a2dd4e83SBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 874a2dd4e83SBenjamin Herrenschmidt 875a2dd4e83SBenjamin Herrenschmidt 876a2dd4e83SBenjamin Herrenschmidt /* The HW performs a non-atomic byte update */ 877a2dd4e83SBenjamin Herrenschmidt stb_phys(CPU(cpu)->as, base + offset, ((pte1 >> 8) & 0xff) | 0x01); 878a2dd4e83SBenjamin Herrenschmidt } 879a2dd4e83SBenjamin Herrenschmidt 880a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_c(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1) 881a2dd4e83SBenjamin Herrenschmidt { 8827bf00dfbSLeandro Lupori hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + HPTE64_DW1_C; 883a2dd4e83SBenjamin Herrenschmidt 884a2dd4e83SBenjamin Herrenschmidt if (cpu->vhyp) { 885c700b5e1SNicholas Piggin cpu->vhyp_class->hpte_set_c(cpu->vhyp, ptex, pte1); 886a2dd4e83SBenjamin Herrenschmidt return; 887a2dd4e83SBenjamin Herrenschmidt } 888a2dd4e83SBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 889a2dd4e83SBenjamin Herrenschmidt 890a2dd4e83SBenjamin Herrenschmidt /* The HW performs a non-atomic byte update */ 891a2dd4e83SBenjamin Herrenschmidt stb_phys(CPU(cpu)->as, base + offset, (pte1 & 0xff) | 0x80); 892a2dd4e83SBenjamin Herrenschmidt } 893a2dd4e83SBenjamin Herrenschmidt 894a864a6b3SDavid Gibson static target_ulong rmls_limit(PowerPCCPU *cpu) 895a864a6b3SDavid Gibson { 896a864a6b3SDavid Gibson CPUPPCState *env = &cpu->env; 897a864a6b3SDavid Gibson /* 898d37b40daSDavid Gibson * In theory the meanings of RMLS values are implementation 899d37b40daSDavid Gibson * dependent. In practice, this seems to have been the set from 900d37b40daSDavid Gibson * POWER4+..POWER8, and RMLS is no longer supported in POWER9. 901a864a6b3SDavid Gibson * 902a864a6b3SDavid Gibson * Unsupported values mean the OS has shot itself in the 903a864a6b3SDavid Gibson * foot. Return a 0-sized RMA in this case, which we expect 904a864a6b3SDavid Gibson * to trigger an immediate DSI or ISI 905a864a6b3SDavid Gibson */ 906a864a6b3SDavid Gibson static const target_ulong rma_sizes[16] = { 907d37b40daSDavid Gibson [0] = 256 * GiB, 908a864a6b3SDavid Gibson [1] = 16 * GiB, 909a864a6b3SDavid Gibson [2] = 1 * GiB, 910a864a6b3SDavid Gibson [3] = 64 * MiB, 911a864a6b3SDavid Gibson [4] = 256 * MiB, 912a864a6b3SDavid Gibson [7] = 128 * MiB, 913a864a6b3SDavid Gibson [8] = 32 * MiB, 914a864a6b3SDavid Gibson }; 915a864a6b3SDavid Gibson target_ulong rmls = (env->spr[SPR_LPCR] & LPCR_RMLS) >> LPCR_RMLS_SHIFT; 916a864a6b3SDavid Gibson 917a864a6b3SDavid Gibson return rma_sizes[rmls]; 918a864a6b3SDavid Gibson } 919a864a6b3SDavid Gibson 9200e2a3ec3SNicholas Piggin /* Return the LLP in SLB_VSID format */ 9210e2a3ec3SNicholas Piggin static uint64_t get_vrma_llp(PowerPCCPU *cpu) 9224c24a87fSDavid Gibson { 9234c24a87fSDavid Gibson CPUPPCState *env = &cpu->env; 9240e2a3ec3SNicholas Piggin uint64_t llp; 9250e2a3ec3SNicholas Piggin 9260e2a3ec3SNicholas Piggin if (env->mmu_model == POWERPC_MMU_3_00) { 9270e2a3ec3SNicholas Piggin ppc_v3_pate_t pate; 9280e2a3ec3SNicholas Piggin uint64_t ps, l, lp; 9290e2a3ec3SNicholas Piggin 9300e2a3ec3SNicholas Piggin /* 9310e2a3ec3SNicholas Piggin * ISA v3.0 removes the LPCR[VRMASD] field and puts the VRMA base 9320e2a3ec3SNicholas Piggin * page size (L||LP equivalent) in the PS field in the HPT partition 9330e2a3ec3SNicholas Piggin * table entry. 9340e2a3ec3SNicholas Piggin */ 9350e2a3ec3SNicholas Piggin if (!ppc64_v3_get_pate(cpu, cpu->env.spr[SPR_LPIDR], &pate)) { 9360e2a3ec3SNicholas Piggin error_report("Bad VRMA with no partition table entry"); 9370e2a3ec3SNicholas Piggin return 0; 9380e2a3ec3SNicholas Piggin } 9390e2a3ec3SNicholas Piggin ps = PATE0_GET_PS(pate.dw0); 9400e2a3ec3SNicholas Piggin /* PS has L||LP in 3 consecutive bits, put them into SLB LLP format */ 9410e2a3ec3SNicholas Piggin l = (ps >> 2) & 0x1; 9420e2a3ec3SNicholas Piggin lp = ps & 0x3; 9430e2a3ec3SNicholas Piggin llp = (l << SLB_VSID_L_SHIFT) | (lp << SLB_VSID_LP_SHIFT); 9440e2a3ec3SNicholas Piggin 9450e2a3ec3SNicholas Piggin } else { 9460e2a3ec3SNicholas Piggin uint64_t lpcr = env->spr[SPR_LPCR]; 9470e2a3ec3SNicholas Piggin target_ulong vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT; 9480e2a3ec3SNicholas Piggin 9490e2a3ec3SNicholas Piggin /* VRMASD LLP matches SLB format, just shift and mask it */ 9500e2a3ec3SNicholas Piggin llp = (vrmasd << SLB_VSID_LP_SHIFT) & SLB_VSID_LLP_MASK; 9510e2a3ec3SNicholas Piggin } 9520e2a3ec3SNicholas Piggin 9530e2a3ec3SNicholas Piggin return llp; 9540e2a3ec3SNicholas Piggin } 9550e2a3ec3SNicholas Piggin 9560e2a3ec3SNicholas Piggin static int build_vrma_slbe(PowerPCCPU *cpu, ppc_slb_t *slb) 9570e2a3ec3SNicholas Piggin { 9580e2a3ec3SNicholas Piggin uint64_t llp = get_vrma_llp(cpu); 9590e2a3ec3SNicholas Piggin target_ulong vsid = SLB_VSID_VRMA | llp; 9604c24a87fSDavid Gibson int i; 9614c24a87fSDavid Gibson 9624c24a87fSDavid Gibson for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 9634c24a87fSDavid Gibson const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i]; 9644c24a87fSDavid Gibson 9654c24a87fSDavid Gibson if (!sps->page_shift) { 9664c24a87fSDavid Gibson break; 9674c24a87fSDavid Gibson } 9684c24a87fSDavid Gibson 9694c24a87fSDavid Gibson if ((vsid & SLB_VSID_LLP_MASK) == sps->slb_enc) { 9704c24a87fSDavid Gibson slb->esid = SLB_ESID_V; 9714c24a87fSDavid Gibson slb->vsid = vsid; 9724c24a87fSDavid Gibson slb->sps = sps; 9734c24a87fSDavid Gibson return 0; 9744c24a87fSDavid Gibson } 9754c24a87fSDavid Gibson } 9764c24a87fSDavid Gibson 9770e2a3ec3SNicholas Piggin error_report("Bad VRMA page size encoding 0x" TARGET_FMT_lx, llp); 9784c24a87fSDavid Gibson 9794c24a87fSDavid Gibson return -1; 9804c24a87fSDavid Gibson } 9814c24a87fSDavid Gibson 98251806b54SRichard Henderson bool ppc_hash64_xlate(PowerPCCPU *cpu, vaddr eaddr, MMUAccessType access_type, 98303695a98SBruno Larsen (billionai) hwaddr *raddrp, int *psizep, int *protp, int mmu_idx, 9841a8c647bSRichard Henderson bool guest_visible) 985fcf5ef2aSThomas Huth { 986fcf5ef2aSThomas Huth CPUState *cs = CPU(cpu); 987fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 9884c24a87fSDavid Gibson ppc_slb_t vrma_slbe; 989fcf5ef2aSThomas Huth ppc_slb_t *slb; 990fcf5ef2aSThomas Huth unsigned apshift; 9917222b94aSDavid Gibson hwaddr ptex; 992fcf5ef2aSThomas Huth ppc_hash_pte64_t pte; 99307a68f99SSuraj Jitindar Singh int exec_prot, pp_prot, amr_prot, prot; 994182357dbSRichard Henderson int need_prot; 995fcf5ef2aSThomas Huth hwaddr raddr; 996*fdd9cf28SNicholas Piggin bool vrma = false; 997fcf5ef2aSThomas Huth 998d75cbae8SDavid Gibson /* 999d75cbae8SDavid Gibson * Note on LPCR usage: 970 uses HID4, but our special variant of 1000d75cbae8SDavid Gibson * store_spr copies relevant fields into env->spr[SPR_LPCR]. 1001136fbf65Szhaolichang * Similarly we filter unimplemented bits when storing into LPCR 1002d75cbae8SDavid Gibson * depending on the MMU version. This code can thus just use the 1003d75cbae8SDavid Gibson * LPCR "as-is". 1004fcf5ef2aSThomas Huth */ 1005fcf5ef2aSThomas Huth 1006fcf5ef2aSThomas Huth /* 1. Handle real mode accesses */ 100703695a98SBruno Larsen (billionai) if (mmuidx_real(mmu_idx)) { 1008d75cbae8SDavid Gibson /* 1009d75cbae8SDavid Gibson * Translation is supposedly "off", but in real mode the top 4 1010d75cbae8SDavid Gibson * effective address bits are (mostly) ignored 1011d75cbae8SDavid Gibson */ 1012fcf5ef2aSThomas Huth raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL; 1013fcf5ef2aSThomas Huth 1014682c1dfbSDavid Gibson if (cpu->vhyp) { 1015682c1dfbSDavid Gibson /* 1016682c1dfbSDavid Gibson * In virtual hypervisor mode, there's nothing to do: 1017682c1dfbSDavid Gibson * EA == GPA == qemu guest address 1018682c1dfbSDavid Gibson */ 101903695a98SBruno Larsen (billionai) } else if (mmuidx_hv(mmu_idx) || !env->has_hv_mode) { 1020fcf5ef2aSThomas Huth /* In HV mode, add HRMOR if top EA bit is clear */ 1021fcf5ef2aSThomas Huth if (!(eaddr >> 63)) { 1022fcf5ef2aSThomas Huth raddr |= env->spr[SPR_HRMOR]; 1023fcf5ef2aSThomas Huth } 10241b99e029SDavid Gibson } else if (ppc_hash64_use_vrma(env)) { 1025682c1dfbSDavid Gibson /* Emulated VRMA mode */ 1026*fdd9cf28SNicholas Piggin vrma = true; 10274c24a87fSDavid Gibson slb = &vrma_slbe; 10284c24a87fSDavid Gibson if (build_vrma_slbe(cpu, slb) != 0) { 1029682c1dfbSDavid Gibson /* Invalid VRMA setup, machine check */ 10301a8c647bSRichard Henderson if (guest_visible) { 1031fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_MCHECK; 1032fcf5ef2aSThomas Huth env->error_code = 0; 10331a8c647bSRichard Henderson } 10341a8c647bSRichard Henderson return false; 1035682c1dfbSDavid Gibson } 1036682c1dfbSDavid Gibson 1037682c1dfbSDavid Gibson goto skip_slb_search; 1038fcf5ef2aSThomas Huth } else { 10393a56a55cSDavid Gibson target_ulong limit = rmls_limit(cpu); 10403a56a55cSDavid Gibson 1041682c1dfbSDavid Gibson /* Emulated old-style RMO mode, bounds check against RMLS */ 10423a56a55cSDavid Gibson if (raddr >= limit) { 10431a8c647bSRichard Henderson if (!guest_visible) { 10441a8c647bSRichard Henderson return false; 10451a8c647bSRichard Henderson } 104659dec5bfSRichard Henderson switch (access_type) { 104759dec5bfSRichard Henderson case MMU_INST_FETCH: 10489201af09SNicholas Piggin ppc_hash64_set_isi(cs, mmu_idx, 0, SRR1_PROTFAULT); 104959dec5bfSRichard Henderson break; 105059dec5bfSRichard Henderson case MMU_DATA_LOAD: 10519201af09SNicholas Piggin ppc_hash64_set_dsi(cs, mmu_idx, 0, eaddr, DSISR_PROTFAULT); 105259dec5bfSRichard Henderson break; 105359dec5bfSRichard Henderson case MMU_DATA_STORE: 10549201af09SNicholas Piggin ppc_hash64_set_dsi(cs, mmu_idx, 0, eaddr, 105559dec5bfSRichard Henderson DSISR_PROTFAULT | DSISR_ISSTORE); 105659dec5bfSRichard Henderson break; 105759dec5bfSRichard Henderson default: 105859dec5bfSRichard Henderson g_assert_not_reached(); 1059fcf5ef2aSThomas Huth } 10601a8c647bSRichard Henderson return false; 1061fcf5ef2aSThomas Huth } 1062682c1dfbSDavid Gibson 1063682c1dfbSDavid Gibson raddr |= env->spr[SPR_RMOR]; 1064fcf5ef2aSThomas Huth } 10651a8c647bSRichard Henderson 10661a8c647bSRichard Henderson *raddrp = raddr; 10671a8c647bSRichard Henderson *protp = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 10681a8c647bSRichard Henderson *psizep = TARGET_PAGE_BITS; 10691a8c647bSRichard Henderson return true; 1070fcf5ef2aSThomas Huth } 1071fcf5ef2aSThomas Huth 1072fcf5ef2aSThomas Huth /* 2. Translation is on, so look up the SLB */ 1073fcf5ef2aSThomas Huth slb = slb_lookup(cpu, eaddr); 1074fcf5ef2aSThomas Huth if (!slb) { 1075b2899495SSuraj Jitindar Singh /* No entry found, check if in-memory segment tables are in use */ 1076ca79b3b7SDavid Gibson if (ppc64_use_proc_tbl(cpu)) { 1077b2899495SSuraj Jitindar Singh /* TODO - Unsupported */ 1078b2899495SSuraj Jitindar Singh error_report("Segment Table Support Unimplemented"); 1079b2899495SSuraj Jitindar Singh exit(1); 1080b2899495SSuraj Jitindar Singh } 1081b2899495SSuraj Jitindar Singh /* Segment still not found, generate the appropriate interrupt */ 10821a8c647bSRichard Henderson if (!guest_visible) { 10831a8c647bSRichard Henderson return false; 10841a8c647bSRichard Henderson } 108559dec5bfSRichard Henderson switch (access_type) { 108659dec5bfSRichard Henderson case MMU_INST_FETCH: 1087fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_ISEG; 1088fcf5ef2aSThomas Huth env->error_code = 0; 108959dec5bfSRichard Henderson break; 109059dec5bfSRichard Henderson case MMU_DATA_LOAD: 109159dec5bfSRichard Henderson case MMU_DATA_STORE: 1092fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_DSEG; 1093fcf5ef2aSThomas Huth env->error_code = 0; 1094fcf5ef2aSThomas Huth env->spr[SPR_DAR] = eaddr; 109559dec5bfSRichard Henderson break; 109659dec5bfSRichard Henderson default: 109759dec5bfSRichard Henderson g_assert_not_reached(); 1098fcf5ef2aSThomas Huth } 10991a8c647bSRichard Henderson return false; 1100fcf5ef2aSThomas Huth } 1101fcf5ef2aSThomas Huth 1102fcf5ef2aSThomas Huth skip_slb_search: 1103fcf5ef2aSThomas Huth 1104fcf5ef2aSThomas Huth /* 3. Check for segment level no-execute violation */ 110559dec5bfSRichard Henderson if (access_type == MMU_INST_FETCH && (slb->vsid & SLB_VSID_N)) { 11061a8c647bSRichard Henderson if (guest_visible) { 11079201af09SNicholas Piggin ppc_hash64_set_isi(cs, mmu_idx, slb->vsid, SRR1_NOEXEC_GUARD); 11081a8c647bSRichard Henderson } 11091a8c647bSRichard Henderson return false; 1110fcf5ef2aSThomas Huth } 1111fcf5ef2aSThomas Huth 1112fcf5ef2aSThomas Huth /* 4. Locate the PTE in the hash table */ 11137222b94aSDavid Gibson ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift); 11147222b94aSDavid Gibson if (ptex == -1) { 11151a8c647bSRichard Henderson if (!guest_visible) { 11161a8c647bSRichard Henderson return false; 11171a8c647bSRichard Henderson } 111859dec5bfSRichard Henderson switch (access_type) { 111959dec5bfSRichard Henderson case MMU_INST_FETCH: 11209201af09SNicholas Piggin ppc_hash64_set_isi(cs, mmu_idx, slb->vsid, SRR1_NOPTE); 112159dec5bfSRichard Henderson break; 112259dec5bfSRichard Henderson case MMU_DATA_LOAD: 11239201af09SNicholas Piggin ppc_hash64_set_dsi(cs, mmu_idx, slb->vsid, eaddr, DSISR_NOPTE); 112459dec5bfSRichard Henderson break; 112559dec5bfSRichard Henderson case MMU_DATA_STORE: 11269201af09SNicholas Piggin ppc_hash64_set_dsi(cs, mmu_idx, slb->vsid, eaddr, 11279201af09SNicholas Piggin DSISR_NOPTE | DSISR_ISSTORE); 112859dec5bfSRichard Henderson break; 112959dec5bfSRichard Henderson default: 113059dec5bfSRichard Henderson g_assert_not_reached(); 1131fcf5ef2aSThomas Huth } 11321a8c647bSRichard Henderson return false; 1133fcf5ef2aSThomas Huth } 1134fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 11357222b94aSDavid Gibson "found PTE at index %08" HWADDR_PRIx "\n", ptex); 1136fcf5ef2aSThomas Huth 1137fcf5ef2aSThomas Huth /* 5. Check access permissions */ 1138fcf5ef2aSThomas Huth 113907a68f99SSuraj Jitindar Singh exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte); 114003695a98SBruno Larsen (billionai) pp_prot = ppc_hash64_pte_prot(mmu_idx, slb, pte); 1141*fdd9cf28SNicholas Piggin if (vrma) { 1142*fdd9cf28SNicholas Piggin /* VRMA does not check keys */ 1143*fdd9cf28SNicholas Piggin amr_prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 1144*fdd9cf28SNicholas Piggin } else { 1145fcf5ef2aSThomas Huth amr_prot = ppc_hash64_amr_prot(cpu, pte); 1146*fdd9cf28SNicholas Piggin } 114707a68f99SSuraj Jitindar Singh prot = exec_prot & pp_prot & amr_prot; 1148fcf5ef2aSThomas Huth 1149cd1038ecSBALATON Zoltan need_prot = check_prot_access_type(PAGE_RWX, access_type); 1150182357dbSRichard Henderson if (need_prot & ~prot) { 1151fcf5ef2aSThomas Huth /* Access right violation */ 1152fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n"); 11531a8c647bSRichard Henderson if (!guest_visible) { 11541a8c647bSRichard Henderson return false; 11551a8c647bSRichard Henderson } 115659dec5bfSRichard Henderson if (access_type == MMU_INST_FETCH) { 1157a6152b52SSuraj Jitindar Singh int srr1 = 0; 115807a68f99SSuraj Jitindar Singh if (PAGE_EXEC & ~exec_prot) { 115907a68f99SSuraj Jitindar Singh srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */ 116007a68f99SSuraj Jitindar Singh } else if (PAGE_EXEC & ~pp_prot) { 1161a6152b52SSuraj Jitindar Singh srr1 |= SRR1_PROTFAULT; /* Access violates access authority */ 1162a6152b52SSuraj Jitindar Singh } 1163a6152b52SSuraj Jitindar Singh if (PAGE_EXEC & ~amr_prot) { 1164a6152b52SSuraj Jitindar Singh srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */ 1165a6152b52SSuraj Jitindar Singh } 11669201af09SNicholas Piggin ppc_hash64_set_isi(cs, mmu_idx, slb->vsid, srr1); 1167fcf5ef2aSThomas Huth } else { 1168da82c73aSSuraj Jitindar Singh int dsisr = 0; 1169182357dbSRichard Henderson if (need_prot & ~pp_prot) { 1170da82c73aSSuraj Jitindar Singh dsisr |= DSISR_PROTFAULT; 1171fcf5ef2aSThomas Huth } 117259dec5bfSRichard Henderson if (access_type == MMU_DATA_STORE) { 1173da82c73aSSuraj Jitindar Singh dsisr |= DSISR_ISSTORE; 1174fcf5ef2aSThomas Huth } 1175182357dbSRichard Henderson if (need_prot & ~amr_prot) { 1176da82c73aSSuraj Jitindar Singh dsisr |= DSISR_AMR; 1177fcf5ef2aSThomas Huth } 11789201af09SNicholas Piggin ppc_hash64_set_dsi(cs, mmu_idx, slb->vsid, eaddr, dsisr); 1179fcf5ef2aSThomas Huth } 11801a8c647bSRichard Henderson return false; 1181fcf5ef2aSThomas Huth } 1182fcf5ef2aSThomas Huth 1183fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n"); 1184fcf5ef2aSThomas Huth 1185fcf5ef2aSThomas Huth /* 6. Update PTE referenced and changed bits if necessary */ 1186fcf5ef2aSThomas Huth 1187a2dd4e83SBenjamin Herrenschmidt if (!(pte.pte1 & HPTE64_R_R)) { 1188a2dd4e83SBenjamin Herrenschmidt ppc_hash64_set_r(cpu, ptex, pte.pte1); 1189a2dd4e83SBenjamin Herrenschmidt } 1190a2dd4e83SBenjamin Herrenschmidt if (!(pte.pte1 & HPTE64_R_C)) { 119159dec5bfSRichard Henderson if (access_type == MMU_DATA_STORE) { 1192a2dd4e83SBenjamin Herrenschmidt ppc_hash64_set_c(cpu, ptex, pte.pte1); 1193fcf5ef2aSThomas Huth } else { 1194d75cbae8SDavid Gibson /* 1195d75cbae8SDavid Gibson * Treat the page as read-only for now, so that a later write 1196d75cbae8SDavid Gibson * will pass through this function again to set the C bit 1197d75cbae8SDavid Gibson */ 1198fcf5ef2aSThomas Huth prot &= ~PAGE_WRITE; 1199fcf5ef2aSThomas Huth } 1200fcf5ef2aSThomas Huth } 1201fcf5ef2aSThomas Huth 1202fcf5ef2aSThomas Huth /* 7. Determine the real address from the PTE */ 1203fcf5ef2aSThomas Huth 12041a8c647bSRichard Henderson *raddrp = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr); 12051a8c647bSRichard Henderson *protp = prot; 12061a8c647bSRichard Henderson *psizep = apshift; 12071a8c647bSRichard Henderson return true; 12081a8c647bSRichard Henderson } 12091a8c647bSRichard Henderson 12107222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex, 1211fcf5ef2aSThomas Huth target_ulong pte0, target_ulong pte1) 1212fcf5ef2aSThomas Huth { 1213fcf5ef2aSThomas Huth /* 1214fcf5ef2aSThomas Huth * XXX: given the fact that there are too many segments to 1215fcf5ef2aSThomas Huth * invalidate, and we still don't have a tlb_flush_mask(env, n, 1216fcf5ef2aSThomas Huth * mask) in QEMU, we just invalidate all TLBs 1217fcf5ef2aSThomas Huth */ 1218fcf5ef2aSThomas Huth cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH; 1219fcf5ef2aSThomas Huth } 1220fcf5ef2aSThomas Huth 12212b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 12225ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val) 12235ad55315SDavid Gibson { 1224db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 12255ad55315SDavid Gibson 12265ad55315SDavid Gibson ppc_store_lpcr(cpu, val); 12275ad55315SDavid Gibson } 12282b44e219SBruno Larsen (billionai) #endif 12295ad55315SDavid Gibson 1230a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu) 1231a059471dSDavid Gibson { 1232a059471dSDavid Gibson CPUPPCState *env = &cpu->env; 1233a059471dSDavid Gibson PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 1234a059471dSDavid Gibson 123521e405f1SDavid Gibson if (!pcc->hash64_opts) { 1236d57d72a8SGreg Kurz assert(!mmu_is_64bit(env->mmu_model)); 123721e405f1SDavid Gibson return; 123821e405f1SDavid Gibson } 123921e405f1SDavid Gibson 124040fed8c1SPhilippe Mathieu-Daudé cpu->hash64_opts = g_memdup2(pcc->hash64_opts, sizeof(*cpu->hash64_opts)); 124121e405f1SDavid Gibson } 124221e405f1SDavid Gibson 124321e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu) 124421e405f1SDavid Gibson { 124521e405f1SDavid Gibson g_free(cpu->hash64_opts); 124621e405f1SDavid Gibson } 124721e405f1SDavid Gibson 124821e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = { 124958969eeeSDavid Gibson .flags = 0, 125067d7d66fSDavid Gibson .slb_size = 64, 1251a059471dSDavid Gibson .sps = { 1252a059471dSDavid Gibson { .page_shift = 12, /* 4K */ 1253a059471dSDavid Gibson .slb_enc = 0, 1254a059471dSDavid Gibson .enc = { { .page_shift = 12, .pte_enc = 0 } } 1255a059471dSDavid Gibson }, 1256a059471dSDavid Gibson { .page_shift = 24, /* 16M */ 1257a059471dSDavid Gibson .slb_enc = 0x100, 1258a059471dSDavid Gibson .enc = { { .page_shift = 24, .pte_enc = 0 } } 1259a059471dSDavid Gibson }, 1260a059471dSDavid Gibson }, 1261a059471dSDavid Gibson }; 1262b07c59f7SDavid Gibson 1263b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = { 126426cd35b8SDavid Gibson .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE, 126567d7d66fSDavid Gibson .slb_size = 32, 1266b07c59f7SDavid Gibson .sps = { 1267b07c59f7SDavid Gibson { 1268b07c59f7SDavid Gibson .page_shift = 12, /* 4K */ 1269b07c59f7SDavid Gibson .slb_enc = 0, 1270b07c59f7SDavid Gibson .enc = { { .page_shift = 12, .pte_enc = 0 }, 1271b07c59f7SDavid Gibson { .page_shift = 16, .pte_enc = 0x7 }, 1272b07c59f7SDavid Gibson { .page_shift = 24, .pte_enc = 0x38 }, }, 1273b07c59f7SDavid Gibson }, 1274b07c59f7SDavid Gibson { 1275b07c59f7SDavid Gibson .page_shift = 16, /* 64K */ 1276b07c59f7SDavid Gibson .slb_enc = SLB_VSID_64K, 1277b07c59f7SDavid Gibson .enc = { { .page_shift = 16, .pte_enc = 0x1 }, 1278b07c59f7SDavid Gibson { .page_shift = 24, .pte_enc = 0x8 }, }, 1279b07c59f7SDavid Gibson }, 1280b07c59f7SDavid Gibson { 1281b07c59f7SDavid Gibson .page_shift = 24, /* 16M */ 1282b07c59f7SDavid Gibson .slb_enc = SLB_VSID_16M, 1283b07c59f7SDavid Gibson .enc = { { .page_shift = 24, .pte_enc = 0 }, }, 1284b07c59f7SDavid Gibson }, 1285b07c59f7SDavid Gibson { 1286b07c59f7SDavid Gibson .page_shift = 34, /* 16G */ 1287b07c59f7SDavid Gibson .slb_enc = SLB_VSID_16G, 1288b07c59f7SDavid Gibson .enc = { { .page_shift = 34, .pte_enc = 0x3 }, }, 1289b07c59f7SDavid Gibson }, 1290b07c59f7SDavid Gibson } 1291b07c59f7SDavid Gibson }; 129227f00f0aSDavid Gibson 129327f00f0aSDavid Gibson 1294