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 10fcf5ef2aSThomas Huth * version 2 of the License, or (at your option) any later version. 11fcf5ef2aSThomas Huth * 12fcf5ef2aSThomas Huth * This library is distributed in the hope that it will be useful, 13fcf5ef2aSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of 14fcf5ef2aSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15fcf5ef2aSThomas Huth * Lesser General Public License for more details. 16fcf5ef2aSThomas Huth * 17fcf5ef2aSThomas Huth * You should have received a copy of the GNU Lesser General Public 18fcf5ef2aSThomas Huth * License along with this library; if not, see <http://www.gnu.org/licenses/>. 19fcf5ef2aSThomas Huth */ 20fcf5ef2aSThomas Huth #include "qemu/osdep.h" 21a864a6b3SDavid Gibson #include "qemu/units.h" 22fcf5ef2aSThomas Huth #include "cpu.h" 23fcf5ef2aSThomas Huth #include "exec/exec-all.h" 24fcf5ef2aSThomas Huth #include "exec/helper-proto.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" 32b2899495SSuraj Jitindar Singh #include "mmu-book3s-v3.h" 33fcf5ef2aSThomas Huth 34d75cbae8SDavid Gibson /* #define DEBUG_SLB */ 35fcf5ef2aSThomas Huth 36fcf5ef2aSThomas Huth #ifdef DEBUG_SLB 37fcf5ef2aSThomas Huth # define LOG_SLB(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__) 38fcf5ef2aSThomas Huth #else 39fcf5ef2aSThomas Huth # define LOG_SLB(...) do { } while (0) 40fcf5ef2aSThomas Huth #endif 41fcf5ef2aSThomas Huth 42fcf5ef2aSThomas Huth /* 43fcf5ef2aSThomas Huth * SLB handling 44fcf5ef2aSThomas Huth */ 45fcf5ef2aSThomas Huth 46fcf5ef2aSThomas Huth static ppc_slb_t *slb_lookup(PowerPCCPU *cpu, target_ulong eaddr) 47fcf5ef2aSThomas Huth { 48fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 49fcf5ef2aSThomas Huth uint64_t esid_256M, esid_1T; 50fcf5ef2aSThomas Huth int n; 51fcf5ef2aSThomas Huth 52fcf5ef2aSThomas Huth LOG_SLB("%s: eaddr " TARGET_FMT_lx "\n", __func__, eaddr); 53fcf5ef2aSThomas Huth 54fcf5ef2aSThomas Huth esid_256M = (eaddr & SEGMENT_MASK_256M) | SLB_ESID_V; 55fcf5ef2aSThomas Huth esid_1T = (eaddr & SEGMENT_MASK_1T) | SLB_ESID_V; 56fcf5ef2aSThomas Huth 5767d7d66fSDavid Gibson for (n = 0; n < cpu->hash64_opts->slb_size; n++) { 58fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[n]; 59fcf5ef2aSThomas Huth 60fcf5ef2aSThomas Huth LOG_SLB("%s: slot %d %016" PRIx64 " %016" 61fcf5ef2aSThomas Huth PRIx64 "\n", __func__, n, slb->esid, slb->vsid); 62d75cbae8SDavid Gibson /* 63d75cbae8SDavid Gibson * We check for 1T matches on all MMUs here - if the MMU 64fcf5ef2aSThomas Huth * doesn't have 1T segment support, we will have prevented 1T 65d75cbae8SDavid Gibson * entries from being inserted in the slbmte code. 66d75cbae8SDavid Gibson */ 67fcf5ef2aSThomas Huth if (((slb->esid == esid_256M) && 68fcf5ef2aSThomas Huth ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_256M)) 69fcf5ef2aSThomas Huth || ((slb->esid == esid_1T) && 70fcf5ef2aSThomas Huth ((slb->vsid & SLB_VSID_B) == SLB_VSID_B_1T))) { 71fcf5ef2aSThomas Huth return slb; 72fcf5ef2aSThomas Huth } 73fcf5ef2aSThomas Huth } 74fcf5ef2aSThomas Huth 75fcf5ef2aSThomas Huth return NULL; 76fcf5ef2aSThomas Huth } 77fcf5ef2aSThomas Huth 78fad866daSMarkus Armbruster void dump_slb(PowerPCCPU *cpu) 79fcf5ef2aSThomas Huth { 80fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 81fcf5ef2aSThomas Huth int i; 82fcf5ef2aSThomas Huth uint64_t slbe, slbv; 83fcf5ef2aSThomas Huth 84fcf5ef2aSThomas Huth cpu_synchronize_state(CPU(cpu)); 85fcf5ef2aSThomas Huth 86fad866daSMarkus Armbruster qemu_printf("SLB\tESID\t\t\tVSID\n"); 8767d7d66fSDavid Gibson for (i = 0; i < cpu->hash64_opts->slb_size; i++) { 88fcf5ef2aSThomas Huth slbe = env->slb[i].esid; 89fcf5ef2aSThomas Huth slbv = env->slb[i].vsid; 90fcf5ef2aSThomas Huth if (slbe == 0 && slbv == 0) { 91fcf5ef2aSThomas Huth continue; 92fcf5ef2aSThomas Huth } 93fad866daSMarkus Armbruster qemu_printf("%d\t0x%016" PRIx64 "\t0x%016" PRIx64 "\n", 94fcf5ef2aSThomas Huth i, slbe, slbv); 95fcf5ef2aSThomas Huth } 96fcf5ef2aSThomas Huth } 97fcf5ef2aSThomas Huth 980418bf78SNicholas Piggin void helper_slbia(CPUPPCState *env, uint32_t ih) 99fcf5ef2aSThomas Huth { 100db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 1010418bf78SNicholas Piggin int starting_entry; 102fcf5ef2aSThomas Huth int n; 103fcf5ef2aSThomas Huth 104f9e3e1a3SNicholas Piggin /* 105f9e3e1a3SNicholas Piggin * slbia must always flush all TLB (which is equivalent to ERAT in ppc 106f9e3e1a3SNicholas Piggin * architecture). Matching on SLB_ESID_V is not good enough, because slbmte 107f9e3e1a3SNicholas Piggin * can overwrite a valid SLB without flushing its lookaside information. 108f9e3e1a3SNicholas Piggin * 109f9e3e1a3SNicholas Piggin * It would be possible to keep the TLB in synch with the SLB by flushing 110f9e3e1a3SNicholas Piggin * when a valid entry is overwritten by slbmte, and therefore slbia would 111f9e3e1a3SNicholas Piggin * not have to flush unless it evicts a valid SLB entry. However it is 112f9e3e1a3SNicholas Piggin * expected that slbmte is more common than slbia, and slbia is usually 113f9e3e1a3SNicholas Piggin * going to evict valid SLB entries, so that tradeoff is unlikely to be a 114f9e3e1a3SNicholas Piggin * good one. 1150418bf78SNicholas Piggin * 1160418bf78SNicholas Piggin * ISA v2.05 introduced IH field with values 0,1,2,6. These all invalidate 1170418bf78SNicholas Piggin * the same SLB entries (everything but entry 0), but differ in what 1180418bf78SNicholas Piggin * "lookaside information" is invalidated. TCG can ignore this and flush 1190418bf78SNicholas Piggin * everything. 1200418bf78SNicholas Piggin * 1210418bf78SNicholas Piggin * ISA v3.0 introduced additional values 3,4,7, which change what SLBs are 1220418bf78SNicholas Piggin * invalidated. 123f9e3e1a3SNicholas Piggin */ 124f9e3e1a3SNicholas Piggin 1250418bf78SNicholas Piggin env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH; 1260418bf78SNicholas Piggin 1270418bf78SNicholas Piggin starting_entry = 1; /* default for IH=0,1,2,6 */ 1280418bf78SNicholas Piggin 1290418bf78SNicholas Piggin if (env->mmu_model == POWERPC_MMU_3_00) { 1300418bf78SNicholas Piggin switch (ih) { 1310418bf78SNicholas Piggin case 0x7: 1320418bf78SNicholas Piggin /* invalidate no SLBs, but all lookaside information */ 1330418bf78SNicholas Piggin return; 1340418bf78SNicholas Piggin 1350418bf78SNicholas Piggin case 0x3: 1360418bf78SNicholas Piggin case 0x4: 1370418bf78SNicholas Piggin /* also considers SLB entry 0 */ 1380418bf78SNicholas Piggin starting_entry = 0; 1390418bf78SNicholas Piggin break; 1400418bf78SNicholas Piggin 1410418bf78SNicholas Piggin case 0x5: 1420418bf78SNicholas Piggin /* treat undefined values as ih==0, and warn */ 1430418bf78SNicholas Piggin qemu_log_mask(LOG_GUEST_ERROR, 1440418bf78SNicholas Piggin "slbia undefined IH field %u.\n", ih); 1450418bf78SNicholas Piggin break; 1460418bf78SNicholas Piggin 1470418bf78SNicholas Piggin default: 1480418bf78SNicholas Piggin /* 0,1,2,6 */ 1490418bf78SNicholas Piggin break; 1500418bf78SNicholas Piggin } 1510418bf78SNicholas Piggin } 1520418bf78SNicholas Piggin 1530418bf78SNicholas Piggin for (n = starting_entry; n < cpu->hash64_opts->slb_size; n++) { 154fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[n]; 155fcf5ef2aSThomas Huth 1560418bf78SNicholas Piggin if (!(slb->esid & SLB_ESID_V)) { 1570418bf78SNicholas Piggin continue; 1580418bf78SNicholas Piggin } 1590418bf78SNicholas Piggin if (env->mmu_model == POWERPC_MMU_3_00) { 1600418bf78SNicholas Piggin if (ih == 0x3 && (slb->vsid & SLB_VSID_C) == 0) { 1610418bf78SNicholas Piggin /* preserves entries with a class value of 0 */ 1620418bf78SNicholas Piggin continue; 163f9e3e1a3SNicholas Piggin } 164f9e3e1a3SNicholas Piggin } 165f9e3e1a3SNicholas Piggin 1660418bf78SNicholas Piggin slb->esid &= ~SLB_ESID_V; 1670418bf78SNicholas Piggin } 168fcf5ef2aSThomas Huth } 169fcf5ef2aSThomas Huth 170a63f1dfcSNikunj A Dadhania static void __helper_slbie(CPUPPCState *env, target_ulong addr, 171a63f1dfcSNikunj A Dadhania target_ulong global) 172fcf5ef2aSThomas Huth { 173db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 174fcf5ef2aSThomas Huth ppc_slb_t *slb; 175fcf5ef2aSThomas Huth 176fcf5ef2aSThomas Huth slb = slb_lookup(cpu, addr); 177fcf5ef2aSThomas Huth if (!slb) { 178fcf5ef2aSThomas Huth return; 179fcf5ef2aSThomas Huth } 180fcf5ef2aSThomas Huth 181fcf5ef2aSThomas Huth if (slb->esid & SLB_ESID_V) { 182fcf5ef2aSThomas Huth slb->esid &= ~SLB_ESID_V; 183fcf5ef2aSThomas Huth 184d75cbae8SDavid Gibson /* 185d75cbae8SDavid Gibson * XXX: given the fact that segment size is 256 MB or 1TB, 186fcf5ef2aSThomas Huth * and we still don't have a tlb_flush_mask(env, n, mask) 187fcf5ef2aSThomas Huth * in QEMU, we just invalidate all TLBs 188fcf5ef2aSThomas Huth */ 189a63f1dfcSNikunj A Dadhania env->tlb_need_flush |= 190a63f1dfcSNikunj A Dadhania (global == false ? TLB_NEED_LOCAL_FLUSH : TLB_NEED_GLOBAL_FLUSH); 191fcf5ef2aSThomas Huth } 192fcf5ef2aSThomas Huth } 193fcf5ef2aSThomas Huth 194a63f1dfcSNikunj A Dadhania void helper_slbie(CPUPPCState *env, target_ulong addr) 195a63f1dfcSNikunj A Dadhania { 196a63f1dfcSNikunj A Dadhania __helper_slbie(env, addr, false); 197a63f1dfcSNikunj A Dadhania } 198a63f1dfcSNikunj A Dadhania 199a63f1dfcSNikunj A Dadhania void helper_slbieg(CPUPPCState *env, target_ulong addr) 200a63f1dfcSNikunj A Dadhania { 201a63f1dfcSNikunj A Dadhania __helper_slbie(env, addr, true); 202a63f1dfcSNikunj A Dadhania } 203a63f1dfcSNikunj A Dadhania 204fcf5ef2aSThomas Huth int ppc_store_slb(PowerPCCPU *cpu, target_ulong slot, 205fcf5ef2aSThomas Huth target_ulong esid, target_ulong vsid) 206fcf5ef2aSThomas Huth { 207fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 208fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 209b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = NULL; 210fcf5ef2aSThomas Huth int i; 211fcf5ef2aSThomas Huth 21267d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 213fcf5ef2aSThomas Huth return -1; /* Bad slot number */ 214fcf5ef2aSThomas Huth } 215fcf5ef2aSThomas Huth if (esid & ~(SLB_ESID_ESID | SLB_ESID_V)) { 216fcf5ef2aSThomas Huth return -1; /* Reserved bits set */ 217fcf5ef2aSThomas Huth } 218fcf5ef2aSThomas Huth if (vsid & (SLB_VSID_B & ~SLB_VSID_B_1T)) { 219fcf5ef2aSThomas Huth return -1; /* Bad segment size */ 220fcf5ef2aSThomas Huth } 22158969eeeSDavid Gibson if ((vsid & SLB_VSID_B) && !(ppc_hash64_has(cpu, PPC_HASH64_1TSEG))) { 222fcf5ef2aSThomas Huth return -1; /* 1T segment on MMU that doesn't support it */ 223fcf5ef2aSThomas Huth } 224fcf5ef2aSThomas Huth 225fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 226b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i]; 227fcf5ef2aSThomas Huth 228fcf5ef2aSThomas Huth if (!sps1->page_shift) { 229fcf5ef2aSThomas Huth break; 230fcf5ef2aSThomas Huth } 231fcf5ef2aSThomas Huth 232fcf5ef2aSThomas Huth if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) { 233fcf5ef2aSThomas Huth sps = sps1; 234fcf5ef2aSThomas Huth break; 235fcf5ef2aSThomas Huth } 236fcf5ef2aSThomas Huth } 237fcf5ef2aSThomas Huth 238fcf5ef2aSThomas Huth if (!sps) { 239fcf5ef2aSThomas Huth error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu 240fcf5ef2aSThomas Huth " esid 0x"TARGET_FMT_lx" vsid 0x"TARGET_FMT_lx, 241fcf5ef2aSThomas Huth slot, esid, vsid); 242fcf5ef2aSThomas Huth return -1; 243fcf5ef2aSThomas Huth } 244fcf5ef2aSThomas Huth 245fcf5ef2aSThomas Huth slb->esid = esid; 246fcf5ef2aSThomas Huth slb->vsid = vsid; 247fcf5ef2aSThomas Huth slb->sps = sps; 248fcf5ef2aSThomas Huth 24976134d48SSuraj Jitindar Singh LOG_SLB("%s: " TARGET_FMT_lu " " TARGET_FMT_lx " - " TARGET_FMT_lx 25076134d48SSuraj Jitindar Singh " => %016" PRIx64 " %016" PRIx64 "\n", __func__, slot, esid, vsid, 251fcf5ef2aSThomas Huth slb->esid, slb->vsid); 252fcf5ef2aSThomas Huth 253fcf5ef2aSThomas Huth return 0; 254fcf5ef2aSThomas Huth } 255fcf5ef2aSThomas Huth 256fcf5ef2aSThomas Huth static int ppc_load_slb_esid(PowerPCCPU *cpu, target_ulong rb, 257fcf5ef2aSThomas Huth target_ulong *rt) 258fcf5ef2aSThomas Huth { 259fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 260fcf5ef2aSThomas Huth int slot = rb & 0xfff; 261fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 262fcf5ef2aSThomas Huth 26367d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 264fcf5ef2aSThomas Huth return -1; 265fcf5ef2aSThomas Huth } 266fcf5ef2aSThomas Huth 267fcf5ef2aSThomas Huth *rt = slb->esid; 268fcf5ef2aSThomas Huth return 0; 269fcf5ef2aSThomas Huth } 270fcf5ef2aSThomas Huth 271fcf5ef2aSThomas Huth static int ppc_load_slb_vsid(PowerPCCPU *cpu, target_ulong rb, 272fcf5ef2aSThomas Huth target_ulong *rt) 273fcf5ef2aSThomas Huth { 274fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 275fcf5ef2aSThomas Huth int slot = rb & 0xfff; 276fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 277fcf5ef2aSThomas Huth 27867d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 279fcf5ef2aSThomas Huth return -1; 280fcf5ef2aSThomas Huth } 281fcf5ef2aSThomas Huth 282fcf5ef2aSThomas Huth *rt = slb->vsid; 283fcf5ef2aSThomas Huth return 0; 284fcf5ef2aSThomas Huth } 285fcf5ef2aSThomas Huth 286fcf5ef2aSThomas Huth static int ppc_find_slb_vsid(PowerPCCPU *cpu, target_ulong rb, 287fcf5ef2aSThomas Huth target_ulong *rt) 288fcf5ef2aSThomas Huth { 289fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 290fcf5ef2aSThomas Huth ppc_slb_t *slb; 291fcf5ef2aSThomas Huth 292fcf5ef2aSThomas Huth if (!msr_is_64bit(env, env->msr)) { 293fcf5ef2aSThomas Huth rb &= 0xffffffff; 294fcf5ef2aSThomas Huth } 295fcf5ef2aSThomas Huth slb = slb_lookup(cpu, rb); 296fcf5ef2aSThomas Huth if (slb == NULL) { 297fcf5ef2aSThomas Huth *rt = (target_ulong)-1ul; 298fcf5ef2aSThomas Huth } else { 299fcf5ef2aSThomas Huth *rt = slb->vsid; 300fcf5ef2aSThomas Huth } 301fcf5ef2aSThomas Huth return 0; 302fcf5ef2aSThomas Huth } 303fcf5ef2aSThomas Huth 304fcf5ef2aSThomas Huth void helper_store_slb(CPUPPCState *env, target_ulong rb, target_ulong rs) 305fcf5ef2aSThomas Huth { 306db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 307fcf5ef2aSThomas Huth 308fcf5ef2aSThomas Huth if (ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs) < 0) { 309fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 310fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 311fcf5ef2aSThomas Huth } 312fcf5ef2aSThomas Huth } 313fcf5ef2aSThomas Huth 314fcf5ef2aSThomas Huth target_ulong helper_load_slb_esid(CPUPPCState *env, target_ulong rb) 315fcf5ef2aSThomas Huth { 316db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 317fcf5ef2aSThomas Huth target_ulong rt = 0; 318fcf5ef2aSThomas Huth 319fcf5ef2aSThomas Huth if (ppc_load_slb_esid(cpu, rb, &rt) < 0) { 320fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 321fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 322fcf5ef2aSThomas Huth } 323fcf5ef2aSThomas Huth return rt; 324fcf5ef2aSThomas Huth } 325fcf5ef2aSThomas Huth 326fcf5ef2aSThomas Huth target_ulong helper_find_slb_vsid(CPUPPCState *env, target_ulong rb) 327fcf5ef2aSThomas Huth { 328db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 329fcf5ef2aSThomas Huth target_ulong rt = 0; 330fcf5ef2aSThomas Huth 331fcf5ef2aSThomas Huth if (ppc_find_slb_vsid(cpu, rb, &rt) < 0) { 332fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 333fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 334fcf5ef2aSThomas Huth } 335fcf5ef2aSThomas Huth return rt; 336fcf5ef2aSThomas Huth } 337fcf5ef2aSThomas Huth 338fcf5ef2aSThomas Huth target_ulong helper_load_slb_vsid(CPUPPCState *env, target_ulong rb) 339fcf5ef2aSThomas Huth { 340db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 341fcf5ef2aSThomas Huth target_ulong rt = 0; 342fcf5ef2aSThomas Huth 343fcf5ef2aSThomas Huth if (ppc_load_slb_vsid(cpu, rb, &rt) < 0) { 344fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 345fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 346fcf5ef2aSThomas Huth } 347fcf5ef2aSThomas Huth return rt; 348fcf5ef2aSThomas Huth } 349fcf5ef2aSThomas Huth 35007a68f99SSuraj Jitindar Singh /* Check No-Execute or Guarded Storage */ 35107a68f99SSuraj Jitindar Singh static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU *cpu, 35207a68f99SSuraj Jitindar Singh ppc_hash_pte64_t pte) 35307a68f99SSuraj Jitindar Singh { 35407a68f99SSuraj Jitindar Singh /* Exec permissions CANNOT take away read or write permissions */ 35507a68f99SSuraj Jitindar Singh return (pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G) ? 35607a68f99SSuraj Jitindar Singh PAGE_READ | PAGE_WRITE : PAGE_READ | PAGE_WRITE | PAGE_EXEC; 35707a68f99SSuraj Jitindar Singh } 35807a68f99SSuraj Jitindar Singh 35907a68f99SSuraj Jitindar Singh /* Check Basic Storage Protection */ 360fcf5ef2aSThomas Huth static int ppc_hash64_pte_prot(PowerPCCPU *cpu, 361fcf5ef2aSThomas Huth ppc_slb_t *slb, ppc_hash_pte64_t pte) 362fcf5ef2aSThomas Huth { 363fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 364fcf5ef2aSThomas Huth unsigned pp, key; 365d75cbae8SDavid Gibson /* 366d75cbae8SDavid Gibson * Some pp bit combinations have undefined behaviour, so default 367d75cbae8SDavid Gibson * to no access in those cases 368d75cbae8SDavid Gibson */ 369fcf5ef2aSThomas Huth int prot = 0; 370fcf5ef2aSThomas Huth 371fcf5ef2aSThomas Huth key = !!(msr_pr ? (slb->vsid & SLB_VSID_KP) 372fcf5ef2aSThomas Huth : (slb->vsid & SLB_VSID_KS)); 373fcf5ef2aSThomas Huth pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61); 374fcf5ef2aSThomas Huth 375fcf5ef2aSThomas Huth if (key == 0) { 376fcf5ef2aSThomas Huth switch (pp) { 377fcf5ef2aSThomas Huth case 0x0: 378fcf5ef2aSThomas Huth case 0x1: 379fcf5ef2aSThomas Huth case 0x2: 380347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 381fcf5ef2aSThomas Huth break; 382fcf5ef2aSThomas Huth 383fcf5ef2aSThomas Huth case 0x3: 384fcf5ef2aSThomas Huth case 0x6: 385347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_EXEC; 386fcf5ef2aSThomas Huth break; 387fcf5ef2aSThomas Huth } 388fcf5ef2aSThomas Huth } else { 389fcf5ef2aSThomas Huth switch (pp) { 390fcf5ef2aSThomas Huth case 0x0: 391fcf5ef2aSThomas Huth case 0x6: 392fcf5ef2aSThomas Huth break; 393fcf5ef2aSThomas Huth 394fcf5ef2aSThomas Huth case 0x1: 395fcf5ef2aSThomas Huth case 0x3: 396347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_EXEC; 397fcf5ef2aSThomas Huth break; 398fcf5ef2aSThomas Huth 399fcf5ef2aSThomas Huth case 0x2: 400347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 401fcf5ef2aSThomas Huth break; 402fcf5ef2aSThomas Huth } 403fcf5ef2aSThomas Huth } 404fcf5ef2aSThomas Huth 405fcf5ef2aSThomas Huth return prot; 406fcf5ef2aSThomas Huth } 407fcf5ef2aSThomas Huth 408a6152b52SSuraj Jitindar Singh /* Check the instruction access permissions specified in the IAMR */ 409a6152b52SSuraj Jitindar Singh static int ppc_hash64_iamr_prot(PowerPCCPU *cpu, int key) 410a6152b52SSuraj Jitindar Singh { 411a6152b52SSuraj Jitindar Singh CPUPPCState *env = &cpu->env; 412a6152b52SSuraj Jitindar Singh int iamr_bits = (env->spr[SPR_IAMR] >> 2 * (31 - key)) & 0x3; 413a6152b52SSuraj Jitindar Singh 414a6152b52SSuraj Jitindar Singh /* 415a6152b52SSuraj Jitindar Singh * An instruction fetch is permitted if the IAMR bit is 0. 416a6152b52SSuraj Jitindar Singh * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit 417a6152b52SSuraj Jitindar Singh * can only take away EXEC permissions not READ or WRITE permissions. 418a6152b52SSuraj Jitindar Singh * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since 419a6152b52SSuraj Jitindar Singh * EXEC permissions are allowed. 420a6152b52SSuraj Jitindar Singh */ 421a6152b52SSuraj Jitindar Singh return (iamr_bits & 0x1) ? PAGE_READ | PAGE_WRITE : 422a6152b52SSuraj Jitindar Singh PAGE_READ | PAGE_WRITE | PAGE_EXEC; 423a6152b52SSuraj Jitindar Singh } 424a6152b52SSuraj Jitindar Singh 425fcf5ef2aSThomas Huth static int ppc_hash64_amr_prot(PowerPCCPU *cpu, ppc_hash_pte64_t pte) 426fcf5ef2aSThomas Huth { 427fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 428fcf5ef2aSThomas Huth int key, amrbits; 429fcf5ef2aSThomas Huth int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 430fcf5ef2aSThomas Huth 431fcf5ef2aSThomas Huth /* Only recent MMUs implement Virtual Page Class Key Protection */ 43258969eeeSDavid Gibson if (!ppc_hash64_has(cpu, PPC_HASH64_AMR)) { 433fcf5ef2aSThomas Huth return prot; 434fcf5ef2aSThomas Huth } 435fcf5ef2aSThomas Huth 436fcf5ef2aSThomas Huth key = HPTE64_R_KEY(pte.pte1); 437fcf5ef2aSThomas Huth amrbits = (env->spr[SPR_AMR] >> 2 * (31 - key)) & 0x3; 438fcf5ef2aSThomas Huth 439fcf5ef2aSThomas Huth /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */ 440fcf5ef2aSThomas Huth /* env->spr[SPR_AMR]); */ 441fcf5ef2aSThomas Huth 442fcf5ef2aSThomas Huth /* 443fcf5ef2aSThomas Huth * A store is permitted if the AMR bit is 0. Remove write 444fcf5ef2aSThomas Huth * protection if it is set. 445fcf5ef2aSThomas Huth */ 446fcf5ef2aSThomas Huth if (amrbits & 0x2) { 447fcf5ef2aSThomas Huth prot &= ~PAGE_WRITE; 448fcf5ef2aSThomas Huth } 449fcf5ef2aSThomas Huth /* 450fcf5ef2aSThomas Huth * A load is permitted if the AMR bit is 0. Remove read 451fcf5ef2aSThomas Huth * protection if it is set. 452fcf5ef2aSThomas Huth */ 453fcf5ef2aSThomas Huth if (amrbits & 0x1) { 454fcf5ef2aSThomas Huth prot &= ~PAGE_READ; 455fcf5ef2aSThomas Huth } 456fcf5ef2aSThomas Huth 457a6152b52SSuraj Jitindar Singh switch (env->mmu_model) { 458a6152b52SSuraj Jitindar Singh /* 459a6152b52SSuraj Jitindar Singh * MMU version 2.07 and later support IAMR 460a6152b52SSuraj Jitindar Singh * Check if the IAMR allows the instruction access - it will return 461a6152b52SSuraj Jitindar Singh * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0 462a6152b52SSuraj Jitindar Singh * if it does (and prot will be unchanged indicating execution support). 463a6152b52SSuraj Jitindar Singh */ 464a6152b52SSuraj Jitindar Singh case POWERPC_MMU_2_07: 465a6152b52SSuraj Jitindar Singh case POWERPC_MMU_3_00: 466a6152b52SSuraj Jitindar Singh prot &= ppc_hash64_iamr_prot(cpu, key); 467a6152b52SSuraj Jitindar Singh break; 468a6152b52SSuraj Jitindar Singh default: 469a6152b52SSuraj Jitindar Singh break; 470a6152b52SSuraj Jitindar Singh } 471a6152b52SSuraj Jitindar Singh 472fcf5ef2aSThomas Huth return prot; 473fcf5ef2aSThomas Huth } 474fcf5ef2aSThomas Huth 4757222b94aSDavid Gibson const ppc_hash_pte64_t *ppc_hash64_map_hptes(PowerPCCPU *cpu, 4767222b94aSDavid Gibson hwaddr ptex, int n) 477fcf5ef2aSThomas Huth { 4787222b94aSDavid Gibson hwaddr pte_offset = ptex * HASH_PTE_SIZE_64; 4793367c62fSBenjamin Herrenschmidt hwaddr base; 4807222b94aSDavid Gibson hwaddr plen = n * HASH_PTE_SIZE_64; 481e57ca75cSDavid Gibson const ppc_hash_pte64_t *hptes; 482e57ca75cSDavid Gibson 483e57ca75cSDavid Gibson if (cpu->vhyp) { 484e57ca75cSDavid Gibson PPCVirtualHypervisorClass *vhc = 485e57ca75cSDavid Gibson PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 486e57ca75cSDavid Gibson return vhc->map_hptes(cpu->vhyp, ptex, n); 487e57ca75cSDavid Gibson } 4883367c62fSBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 489e57ca75cSDavid Gibson 490e57ca75cSDavid Gibson if (!base) { 491e57ca75cSDavid Gibson return NULL; 492e57ca75cSDavid Gibson } 493e57ca75cSDavid Gibson 494f26404fbSPeter Maydell hptes = address_space_map(CPU(cpu)->as, base + pte_offset, &plen, false, 495f26404fbSPeter Maydell MEMTXATTRS_UNSPECIFIED); 4967222b94aSDavid Gibson if (plen < (n * HASH_PTE_SIZE_64)) { 4977222b94aSDavid Gibson hw_error("%s: Unable to map all requested HPTEs\n", __func__); 498fcf5ef2aSThomas Huth } 4997222b94aSDavid Gibson return hptes; 500fcf5ef2aSThomas Huth } 501fcf5ef2aSThomas Huth 5027222b94aSDavid Gibson void ppc_hash64_unmap_hptes(PowerPCCPU *cpu, const ppc_hash_pte64_t *hptes, 5037222b94aSDavid Gibson hwaddr ptex, int n) 504fcf5ef2aSThomas Huth { 505e57ca75cSDavid Gibson if (cpu->vhyp) { 506e57ca75cSDavid Gibson PPCVirtualHypervisorClass *vhc = 507e57ca75cSDavid Gibson PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 508e57ca75cSDavid Gibson vhc->unmap_hptes(cpu->vhyp, hptes, ptex, n); 509e57ca75cSDavid Gibson return; 510e57ca75cSDavid Gibson } 511e57ca75cSDavid Gibson 5127222b94aSDavid Gibson address_space_unmap(CPU(cpu)->as, (void *)hptes, n * HASH_PTE_SIZE_64, 5137222b94aSDavid Gibson false, n * HASH_PTE_SIZE_64); 514fcf5ef2aSThomas Huth } 515fcf5ef2aSThomas Huth 516b07c59f7SDavid Gibson static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes *sps, 517fcf5ef2aSThomas Huth uint64_t pte0, uint64_t pte1) 518fcf5ef2aSThomas Huth { 519fcf5ef2aSThomas Huth int i; 520fcf5ef2aSThomas Huth 521fcf5ef2aSThomas Huth if (!(pte0 & HPTE64_V_LARGE)) { 522fcf5ef2aSThomas Huth if (sps->page_shift != 12) { 523fcf5ef2aSThomas Huth /* 4kiB page in a non 4kiB segment */ 524fcf5ef2aSThomas Huth return 0; 525fcf5ef2aSThomas Huth } 526fcf5ef2aSThomas Huth /* Normal 4kiB page */ 527fcf5ef2aSThomas Huth return 12; 528fcf5ef2aSThomas Huth } 529fcf5ef2aSThomas Huth 530fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 531b07c59f7SDavid Gibson const PPCHash64PageSize *ps = &sps->enc[i]; 532fcf5ef2aSThomas Huth uint64_t mask; 533fcf5ef2aSThomas Huth 534fcf5ef2aSThomas Huth if (!ps->page_shift) { 535fcf5ef2aSThomas Huth break; 536fcf5ef2aSThomas Huth } 537fcf5ef2aSThomas Huth 538fcf5ef2aSThomas Huth if (ps->page_shift == 12) { 539fcf5ef2aSThomas Huth /* L bit is set so this can't be a 4kiB page */ 540fcf5ef2aSThomas Huth continue; 541fcf5ef2aSThomas Huth } 542fcf5ef2aSThomas Huth 543fcf5ef2aSThomas Huth mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN; 544fcf5ef2aSThomas Huth 545fcf5ef2aSThomas Huth if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) { 546fcf5ef2aSThomas Huth return ps->page_shift; 547fcf5ef2aSThomas Huth } 548fcf5ef2aSThomas Huth } 549fcf5ef2aSThomas Huth 550fcf5ef2aSThomas Huth return 0; /* Bad page size encoding */ 551fcf5ef2aSThomas Huth } 552fcf5ef2aSThomas Huth 55334525595SBenjamin Herrenschmidt static void ppc64_v3_new_to_old_hpte(target_ulong *pte0, target_ulong *pte1) 55434525595SBenjamin Herrenschmidt { 55534525595SBenjamin Herrenschmidt /* Insert B into pte0 */ 55634525595SBenjamin Herrenschmidt *pte0 = (*pte0 & HPTE64_V_COMMON_BITS) | 55734525595SBenjamin Herrenschmidt ((*pte1 & HPTE64_R_3_0_SSIZE_MASK) << 55834525595SBenjamin Herrenschmidt (HPTE64_V_SSIZE_SHIFT - HPTE64_R_3_0_SSIZE_SHIFT)); 55934525595SBenjamin Herrenschmidt 56034525595SBenjamin Herrenschmidt /* Remove B from pte1 */ 56134525595SBenjamin Herrenschmidt *pte1 = *pte1 & ~HPTE64_R_3_0_SSIZE_MASK; 56234525595SBenjamin Herrenschmidt } 56334525595SBenjamin Herrenschmidt 56434525595SBenjamin Herrenschmidt 565fcf5ef2aSThomas Huth static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash, 566b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps, 567fcf5ef2aSThomas Huth target_ulong ptem, 568fcf5ef2aSThomas Huth ppc_hash_pte64_t *pte, unsigned *pshift) 569fcf5ef2aSThomas Huth { 570fcf5ef2aSThomas Huth int i; 5717222b94aSDavid Gibson const ppc_hash_pte64_t *pteg; 572fcf5ef2aSThomas Huth target_ulong pte0, pte1; 5737222b94aSDavid Gibson target_ulong ptex; 574fcf5ef2aSThomas Huth 57536778660SDavid Gibson ptex = (hash & ppc_hash64_hpt_mask(cpu)) * HPTES_PER_GROUP; 5767222b94aSDavid Gibson pteg = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP); 5777222b94aSDavid Gibson if (!pteg) { 578fcf5ef2aSThomas Huth return -1; 579fcf5ef2aSThomas Huth } 580fcf5ef2aSThomas Huth for (i = 0; i < HPTES_PER_GROUP; i++) { 5817222b94aSDavid Gibson pte0 = ppc_hash64_hpte0(cpu, pteg, i); 5823054b0caSBenjamin Herrenschmidt /* 5833054b0caSBenjamin Herrenschmidt * pte0 contains the valid bit and must be read before pte1, 5843054b0caSBenjamin Herrenschmidt * otherwise we might see an old pte1 with a new valid bit and 5853054b0caSBenjamin Herrenschmidt * thus an inconsistent hpte value 5863054b0caSBenjamin Herrenschmidt */ 5873054b0caSBenjamin Herrenschmidt smp_rmb(); 5887222b94aSDavid Gibson pte1 = ppc_hash64_hpte1(cpu, pteg, i); 589fcf5ef2aSThomas Huth 59034525595SBenjamin Herrenschmidt /* Convert format if necessary */ 59134525595SBenjamin Herrenschmidt if (cpu->env.mmu_model == POWERPC_MMU_3_00 && !cpu->vhyp) { 59234525595SBenjamin Herrenschmidt ppc64_v3_new_to_old_hpte(&pte0, &pte1); 59334525595SBenjamin Herrenschmidt } 59434525595SBenjamin Herrenschmidt 595fcf5ef2aSThomas Huth /* This compares V, B, H (secondary) and the AVPN */ 596fcf5ef2aSThomas Huth if (HPTE64_V_COMPARE(pte0, ptem)) { 597fcf5ef2aSThomas Huth *pshift = hpte_page_shift(sps, pte0, pte1); 598fcf5ef2aSThomas Huth /* 599fcf5ef2aSThomas Huth * If there is no match, ignore the PTE, it could simply 600fcf5ef2aSThomas Huth * be for a different segment size encoding and the 601fcf5ef2aSThomas Huth * architecture specifies we should not match. Linux will 602fcf5ef2aSThomas Huth * potentially leave behind PTEs for the wrong base page 603fcf5ef2aSThomas Huth * size when demoting segments. 604fcf5ef2aSThomas Huth */ 605fcf5ef2aSThomas Huth if (*pshift == 0) { 606fcf5ef2aSThomas Huth continue; 607fcf5ef2aSThomas Huth } 608d75cbae8SDavid Gibson /* 609d75cbae8SDavid Gibson * We don't do anything with pshift yet as qemu TLB only 610d75cbae8SDavid Gibson * deals with 4K pages anyway 611fcf5ef2aSThomas Huth */ 612fcf5ef2aSThomas Huth pte->pte0 = pte0; 613fcf5ef2aSThomas Huth pte->pte1 = pte1; 6147222b94aSDavid Gibson ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP); 6157222b94aSDavid Gibson return ptex + i; 616fcf5ef2aSThomas Huth } 617fcf5ef2aSThomas Huth } 6187222b94aSDavid Gibson ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP); 619fcf5ef2aSThomas Huth /* 620fcf5ef2aSThomas Huth * We didn't find a valid entry. 621fcf5ef2aSThomas Huth */ 622fcf5ef2aSThomas Huth return -1; 623fcf5ef2aSThomas Huth } 624fcf5ef2aSThomas Huth 625fcf5ef2aSThomas Huth static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu, 626fcf5ef2aSThomas Huth ppc_slb_t *slb, target_ulong eaddr, 627fcf5ef2aSThomas Huth ppc_hash_pte64_t *pte, unsigned *pshift) 628fcf5ef2aSThomas Huth { 629fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 6307222b94aSDavid Gibson hwaddr hash, ptex; 631fcf5ef2aSThomas Huth uint64_t vsid, epnmask, epn, ptem; 632b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = slb->sps; 633fcf5ef2aSThomas Huth 634d75cbae8SDavid Gibson /* 635d75cbae8SDavid Gibson * The SLB store path should prevent any bad page size encodings 636d75cbae8SDavid Gibson * getting in there, so: 637d75cbae8SDavid Gibson */ 638fcf5ef2aSThomas Huth assert(sps); 639fcf5ef2aSThomas Huth 640fcf5ef2aSThomas Huth /* If ISL is set in LPCR we need to clamp the page size to 4K */ 641fcf5ef2aSThomas Huth if (env->spr[SPR_LPCR] & LPCR_ISL) { 642fcf5ef2aSThomas Huth /* We assume that when using TCG, 4k is first entry of SPS */ 643b07c59f7SDavid Gibson sps = &cpu->hash64_opts->sps[0]; 644fcf5ef2aSThomas Huth assert(sps->page_shift == 12); 645fcf5ef2aSThomas Huth } 646fcf5ef2aSThomas Huth 647fcf5ef2aSThomas Huth epnmask = ~((1ULL << sps->page_shift) - 1); 648fcf5ef2aSThomas Huth 649fcf5ef2aSThomas Huth if (slb->vsid & SLB_VSID_B) { 650fcf5ef2aSThomas Huth /* 1TB segment */ 651fcf5ef2aSThomas Huth vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T; 652fcf5ef2aSThomas Huth epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask; 653fcf5ef2aSThomas Huth hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift); 654fcf5ef2aSThomas Huth } else { 655fcf5ef2aSThomas Huth /* 256M segment */ 656fcf5ef2aSThomas Huth vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT; 657fcf5ef2aSThomas Huth epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask; 658fcf5ef2aSThomas Huth hash = vsid ^ (epn >> sps->page_shift); 659fcf5ef2aSThomas Huth } 660fcf5ef2aSThomas Huth ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN); 661fcf5ef2aSThomas Huth ptem |= HPTE64_V_VALID; 662fcf5ef2aSThomas Huth 663fcf5ef2aSThomas Huth /* Page address translation */ 664fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 665fcf5ef2aSThomas Huth "htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx 666fcf5ef2aSThomas Huth " hash " TARGET_FMT_plx "\n", 66736778660SDavid Gibson ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), hash); 668fcf5ef2aSThomas Huth 669fcf5ef2aSThomas Huth /* Primary PTEG lookup */ 670fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 671fcf5ef2aSThomas Huth "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx 672fcf5ef2aSThomas Huth " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx 673fcf5ef2aSThomas Huth " hash=" TARGET_FMT_plx "\n", 67436778660SDavid Gibson ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), 67536778660SDavid Gibson vsid, ptem, hash); 6767222b94aSDavid Gibson ptex = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift); 677fcf5ef2aSThomas Huth 6787222b94aSDavid Gibson if (ptex == -1) { 679fcf5ef2aSThomas Huth /* Secondary PTEG lookup */ 680fcf5ef2aSThomas Huth ptem |= HPTE64_V_SECONDARY; 681fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 682fcf5ef2aSThomas Huth "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx 683fcf5ef2aSThomas Huth " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx 68436778660SDavid Gibson " hash=" TARGET_FMT_plx "\n", ppc_hash64_hpt_base(cpu), 68536778660SDavid Gibson ppc_hash64_hpt_mask(cpu), vsid, ptem, ~hash); 686fcf5ef2aSThomas Huth 6877222b94aSDavid Gibson ptex = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift); 688fcf5ef2aSThomas Huth } 689fcf5ef2aSThomas Huth 6907222b94aSDavid Gibson return ptex; 691fcf5ef2aSThomas Huth } 692fcf5ef2aSThomas Huth 693fcf5ef2aSThomas Huth unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu, 694fcf5ef2aSThomas Huth uint64_t pte0, uint64_t pte1) 695fcf5ef2aSThomas Huth { 696fcf5ef2aSThomas Huth int i; 697fcf5ef2aSThomas Huth 698fcf5ef2aSThomas Huth if (!(pte0 & HPTE64_V_LARGE)) { 699fcf5ef2aSThomas Huth return 12; 700fcf5ef2aSThomas Huth } 701fcf5ef2aSThomas Huth 702fcf5ef2aSThomas Huth /* 703fcf5ef2aSThomas Huth * The encodings in env->sps need to be carefully chosen so that 704fcf5ef2aSThomas Huth * this gives an unambiguous result. 705fcf5ef2aSThomas Huth */ 706fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 707b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i]; 708fcf5ef2aSThomas Huth unsigned shift; 709fcf5ef2aSThomas Huth 710fcf5ef2aSThomas Huth if (!sps->page_shift) { 711fcf5ef2aSThomas Huth break; 712fcf5ef2aSThomas Huth } 713fcf5ef2aSThomas Huth 714fcf5ef2aSThomas Huth shift = hpte_page_shift(sps, pte0, pte1); 715fcf5ef2aSThomas Huth if (shift) { 716fcf5ef2aSThomas Huth return shift; 717fcf5ef2aSThomas Huth } 718fcf5ef2aSThomas Huth } 719fcf5ef2aSThomas Huth 720fcf5ef2aSThomas Huth return 0; 721fcf5ef2aSThomas Huth } 722fcf5ef2aSThomas Huth 7231b99e029SDavid Gibson static bool ppc_hash64_use_vrma(CPUPPCState *env) 7241b99e029SDavid Gibson { 7251b99e029SDavid Gibson switch (env->mmu_model) { 7261b99e029SDavid Gibson case POWERPC_MMU_3_00: 7271b99e029SDavid Gibson /* 7281b99e029SDavid Gibson * ISAv3.0 (POWER9) always uses VRMA, the VPM0 field and RMOR 7291b99e029SDavid Gibson * register no longer exist 7301b99e029SDavid Gibson */ 7311b99e029SDavid Gibson return true; 7321b99e029SDavid Gibson 7331b99e029SDavid Gibson default: 7341b99e029SDavid Gibson return !!(env->spr[SPR_LPCR] & LPCR_VPM0); 7351b99e029SDavid Gibson } 7361b99e029SDavid Gibson } 7371b99e029SDavid Gibson 7388fe08facSDavid Gibson static void ppc_hash64_set_isi(CPUState *cs, uint64_t error_code) 739fcf5ef2aSThomas Huth { 7408fe08facSDavid Gibson CPUPPCState *env = &POWERPC_CPU(cs)->env; 741fcf5ef2aSThomas Huth bool vpm; 742fcf5ef2aSThomas Huth 743fcf5ef2aSThomas Huth if (msr_ir) { 744fcf5ef2aSThomas Huth vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1); 745fcf5ef2aSThomas Huth } else { 7461b99e029SDavid Gibson vpm = ppc_hash64_use_vrma(env); 747fcf5ef2aSThomas Huth } 748fcf5ef2aSThomas Huth if (vpm && !msr_hv) { 749fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_HISI; 750fcf5ef2aSThomas Huth } else { 751fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_ISI; 752fcf5ef2aSThomas Huth } 753fcf5ef2aSThomas Huth env->error_code = error_code; 754fcf5ef2aSThomas Huth } 755fcf5ef2aSThomas Huth 7568fe08facSDavid Gibson static void ppc_hash64_set_dsi(CPUState *cs, uint64_t dar, uint64_t dsisr) 757fcf5ef2aSThomas Huth { 7588fe08facSDavid Gibson CPUPPCState *env = &POWERPC_CPU(cs)->env; 759fcf5ef2aSThomas Huth bool vpm; 760fcf5ef2aSThomas Huth 761fcf5ef2aSThomas Huth if (msr_dr) { 762fcf5ef2aSThomas Huth vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1); 763fcf5ef2aSThomas Huth } else { 7641b99e029SDavid Gibson vpm = ppc_hash64_use_vrma(env); 765fcf5ef2aSThomas Huth } 766fcf5ef2aSThomas Huth if (vpm && !msr_hv) { 767fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_HDSI; 768fcf5ef2aSThomas Huth env->spr[SPR_HDAR] = dar; 769fcf5ef2aSThomas Huth env->spr[SPR_HDSISR] = dsisr; 770fcf5ef2aSThomas Huth } else { 771fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_DSI; 772fcf5ef2aSThomas Huth env->spr[SPR_DAR] = dar; 773fcf5ef2aSThomas Huth env->spr[SPR_DSISR] = dsisr; 774fcf5ef2aSThomas Huth } 775fcf5ef2aSThomas Huth env->error_code = 0; 776fcf5ef2aSThomas Huth } 777fcf5ef2aSThomas Huth 778fcf5ef2aSThomas Huth 779a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_r(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1) 780a2dd4e83SBenjamin Herrenschmidt { 781a2dd4e83SBenjamin Herrenschmidt hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + 16; 782a2dd4e83SBenjamin Herrenschmidt 783a2dd4e83SBenjamin Herrenschmidt if (cpu->vhyp) { 784a2dd4e83SBenjamin Herrenschmidt PPCVirtualHypervisorClass *vhc = 785a2dd4e83SBenjamin Herrenschmidt PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 786a2dd4e83SBenjamin Herrenschmidt vhc->hpte_set_r(cpu->vhyp, ptex, pte1); 787a2dd4e83SBenjamin Herrenschmidt return; 788a2dd4e83SBenjamin Herrenschmidt } 789a2dd4e83SBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 790a2dd4e83SBenjamin Herrenschmidt 791a2dd4e83SBenjamin Herrenschmidt 792a2dd4e83SBenjamin Herrenschmidt /* The HW performs a non-atomic byte update */ 793a2dd4e83SBenjamin Herrenschmidt stb_phys(CPU(cpu)->as, base + offset, ((pte1 >> 8) & 0xff) | 0x01); 794a2dd4e83SBenjamin Herrenschmidt } 795a2dd4e83SBenjamin Herrenschmidt 796a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_c(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1) 797a2dd4e83SBenjamin Herrenschmidt { 798a2dd4e83SBenjamin Herrenschmidt hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + 15; 799a2dd4e83SBenjamin Herrenschmidt 800a2dd4e83SBenjamin Herrenschmidt if (cpu->vhyp) { 801a2dd4e83SBenjamin Herrenschmidt PPCVirtualHypervisorClass *vhc = 802a2dd4e83SBenjamin Herrenschmidt PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 803a2dd4e83SBenjamin Herrenschmidt vhc->hpte_set_c(cpu->vhyp, ptex, pte1); 804a2dd4e83SBenjamin Herrenschmidt return; 805a2dd4e83SBenjamin Herrenschmidt } 806a2dd4e83SBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 807a2dd4e83SBenjamin Herrenschmidt 808a2dd4e83SBenjamin Herrenschmidt /* The HW performs a non-atomic byte update */ 809a2dd4e83SBenjamin Herrenschmidt stb_phys(CPU(cpu)->as, base + offset, (pte1 & 0xff) | 0x80); 810a2dd4e83SBenjamin Herrenschmidt } 811a2dd4e83SBenjamin Herrenschmidt 812a864a6b3SDavid Gibson static target_ulong rmls_limit(PowerPCCPU *cpu) 813a864a6b3SDavid Gibson { 814a864a6b3SDavid Gibson CPUPPCState *env = &cpu->env; 815a864a6b3SDavid Gibson /* 816d37b40daSDavid Gibson * In theory the meanings of RMLS values are implementation 817d37b40daSDavid Gibson * dependent. In practice, this seems to have been the set from 818d37b40daSDavid Gibson * POWER4+..POWER8, and RMLS is no longer supported in POWER9. 819a864a6b3SDavid Gibson * 820a864a6b3SDavid Gibson * Unsupported values mean the OS has shot itself in the 821a864a6b3SDavid Gibson * foot. Return a 0-sized RMA in this case, which we expect 822a864a6b3SDavid Gibson * to trigger an immediate DSI or ISI 823a864a6b3SDavid Gibson */ 824a864a6b3SDavid Gibson static const target_ulong rma_sizes[16] = { 825d37b40daSDavid Gibson [0] = 256 * GiB, 826a864a6b3SDavid Gibson [1] = 16 * GiB, 827a864a6b3SDavid Gibson [2] = 1 * GiB, 828a864a6b3SDavid Gibson [3] = 64 * MiB, 829a864a6b3SDavid Gibson [4] = 256 * MiB, 830a864a6b3SDavid Gibson [7] = 128 * MiB, 831a864a6b3SDavid Gibson [8] = 32 * MiB, 832a864a6b3SDavid Gibson }; 833a864a6b3SDavid Gibson target_ulong rmls = (env->spr[SPR_LPCR] & LPCR_RMLS) >> LPCR_RMLS_SHIFT; 834a864a6b3SDavid Gibson 835a864a6b3SDavid Gibson return rma_sizes[rmls]; 836a864a6b3SDavid Gibson } 837a864a6b3SDavid Gibson 8384c24a87fSDavid Gibson static int build_vrma_slbe(PowerPCCPU *cpu, ppc_slb_t *slb) 8394c24a87fSDavid Gibson { 8404c24a87fSDavid Gibson CPUPPCState *env = &cpu->env; 8414c24a87fSDavid Gibson target_ulong lpcr = env->spr[SPR_LPCR]; 8424c24a87fSDavid Gibson uint32_t vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT; 8434c24a87fSDavid Gibson target_ulong vsid = SLB_VSID_VRMA | ((vrmasd << 4) & SLB_VSID_LLP_MASK); 8444c24a87fSDavid Gibson int i; 8454c24a87fSDavid Gibson 8464c24a87fSDavid Gibson for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 8474c24a87fSDavid Gibson const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i]; 8484c24a87fSDavid Gibson 8494c24a87fSDavid Gibson if (!sps->page_shift) { 8504c24a87fSDavid Gibson break; 8514c24a87fSDavid Gibson } 8524c24a87fSDavid Gibson 8534c24a87fSDavid Gibson if ((vsid & SLB_VSID_LLP_MASK) == sps->slb_enc) { 8544c24a87fSDavid Gibson slb->esid = SLB_ESID_V; 8554c24a87fSDavid Gibson slb->vsid = vsid; 8564c24a87fSDavid Gibson slb->sps = sps; 8574c24a87fSDavid Gibson return 0; 8584c24a87fSDavid Gibson } 8594c24a87fSDavid Gibson } 8604c24a87fSDavid Gibson 8614c24a87fSDavid Gibson error_report("Bad page size encoding in LPCR[VRMASD]; LPCR=0x" 862*ff5b5d5bSMarkus Armbruster TARGET_FMT_lx, lpcr); 8634c24a87fSDavid Gibson 8644c24a87fSDavid Gibson return -1; 8654c24a87fSDavid Gibson } 8664c24a87fSDavid Gibson 867fcf5ef2aSThomas Huth int ppc_hash64_handle_mmu_fault(PowerPCCPU *cpu, vaddr eaddr, 868fcf5ef2aSThomas Huth int rwx, int mmu_idx) 869fcf5ef2aSThomas Huth { 870fcf5ef2aSThomas Huth CPUState *cs = CPU(cpu); 871fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 8724c24a87fSDavid Gibson ppc_slb_t vrma_slbe; 873fcf5ef2aSThomas Huth ppc_slb_t *slb; 874fcf5ef2aSThomas Huth unsigned apshift; 8757222b94aSDavid Gibson hwaddr ptex; 876fcf5ef2aSThomas Huth ppc_hash_pte64_t pte; 87707a68f99SSuraj Jitindar Singh int exec_prot, pp_prot, amr_prot, prot; 878fcf5ef2aSThomas Huth const int need_prot[] = {PAGE_READ, PAGE_WRITE, PAGE_EXEC}; 879fcf5ef2aSThomas Huth hwaddr raddr; 880fcf5ef2aSThomas Huth 881fcf5ef2aSThomas Huth assert((rwx == 0) || (rwx == 1) || (rwx == 2)); 882fcf5ef2aSThomas Huth 883d75cbae8SDavid Gibson /* 884d75cbae8SDavid Gibson * Note on LPCR usage: 970 uses HID4, but our special variant of 885d75cbae8SDavid Gibson * store_spr copies relevant fields into env->spr[SPR_LPCR]. 886d75cbae8SDavid Gibson * Similarily we filter unimplemented bits when storing into LPCR 887d75cbae8SDavid Gibson * depending on the MMU version. This code can thus just use the 888d75cbae8SDavid Gibson * LPCR "as-is". 889fcf5ef2aSThomas Huth */ 890fcf5ef2aSThomas Huth 891fcf5ef2aSThomas Huth /* 1. Handle real mode accesses */ 892fcf5ef2aSThomas Huth if (((rwx == 2) && (msr_ir == 0)) || ((rwx != 2) && (msr_dr == 0))) { 893d75cbae8SDavid Gibson /* 894d75cbae8SDavid Gibson * Translation is supposedly "off", but in real mode the top 4 895d75cbae8SDavid Gibson * effective address bits are (mostly) ignored 896d75cbae8SDavid Gibson */ 897fcf5ef2aSThomas Huth raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL; 898fcf5ef2aSThomas Huth 899682c1dfbSDavid Gibson if (cpu->vhyp) { 900682c1dfbSDavid Gibson /* 901682c1dfbSDavid Gibson * In virtual hypervisor mode, there's nothing to do: 902682c1dfbSDavid Gibson * EA == GPA == qemu guest address 903682c1dfbSDavid Gibson */ 904682c1dfbSDavid Gibson } else if (msr_hv || !env->has_hv_mode) { 905fcf5ef2aSThomas Huth /* In HV mode, add HRMOR if top EA bit is clear */ 906fcf5ef2aSThomas Huth if (!(eaddr >> 63)) { 907fcf5ef2aSThomas Huth raddr |= env->spr[SPR_HRMOR]; 908fcf5ef2aSThomas Huth } 9091b99e029SDavid Gibson } else if (ppc_hash64_use_vrma(env)) { 910682c1dfbSDavid Gibson /* Emulated VRMA mode */ 9114c24a87fSDavid Gibson slb = &vrma_slbe; 9124c24a87fSDavid Gibson if (build_vrma_slbe(cpu, slb) != 0) { 913682c1dfbSDavid Gibson /* Invalid VRMA setup, machine check */ 914fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_MCHECK; 915fcf5ef2aSThomas Huth env->error_code = 0; 916fcf5ef2aSThomas Huth return 1; 917682c1dfbSDavid Gibson } 918682c1dfbSDavid Gibson 919682c1dfbSDavid Gibson goto skip_slb_search; 920fcf5ef2aSThomas Huth } else { 9213a56a55cSDavid Gibson target_ulong limit = rmls_limit(cpu); 9223a56a55cSDavid Gibson 923682c1dfbSDavid Gibson /* Emulated old-style RMO mode, bounds check against RMLS */ 9243a56a55cSDavid Gibson if (raddr >= limit) { 925fcf5ef2aSThomas Huth if (rwx == 2) { 9268fe08facSDavid Gibson ppc_hash64_set_isi(cs, SRR1_PROTFAULT); 927fcf5ef2aSThomas Huth } else { 928da82c73aSSuraj Jitindar Singh int dsisr = DSISR_PROTFAULT; 929fcf5ef2aSThomas Huth if (rwx == 1) { 930da82c73aSSuraj Jitindar Singh dsisr |= DSISR_ISSTORE; 931fcf5ef2aSThomas Huth } 9328fe08facSDavid Gibson ppc_hash64_set_dsi(cs, eaddr, dsisr); 933fcf5ef2aSThomas Huth } 934fcf5ef2aSThomas Huth return 1; 935fcf5ef2aSThomas Huth } 936682c1dfbSDavid Gibson 937682c1dfbSDavid Gibson raddr |= env->spr[SPR_RMOR]; 938fcf5ef2aSThomas Huth } 939fcf5ef2aSThomas Huth tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK, 940fcf5ef2aSThomas Huth PAGE_READ | PAGE_WRITE | PAGE_EXEC, mmu_idx, 941fcf5ef2aSThomas Huth TARGET_PAGE_SIZE); 942fcf5ef2aSThomas Huth return 0; 943fcf5ef2aSThomas Huth } 944fcf5ef2aSThomas Huth 945fcf5ef2aSThomas Huth /* 2. Translation is on, so look up the SLB */ 946fcf5ef2aSThomas Huth slb = slb_lookup(cpu, eaddr); 947fcf5ef2aSThomas Huth if (!slb) { 948b2899495SSuraj Jitindar Singh /* No entry found, check if in-memory segment tables are in use */ 949ca79b3b7SDavid Gibson if (ppc64_use_proc_tbl(cpu)) { 950b2899495SSuraj Jitindar Singh /* TODO - Unsupported */ 951b2899495SSuraj Jitindar Singh error_report("Segment Table Support Unimplemented"); 952b2899495SSuraj Jitindar Singh exit(1); 953b2899495SSuraj Jitindar Singh } 954b2899495SSuraj Jitindar Singh /* Segment still not found, generate the appropriate interrupt */ 955fcf5ef2aSThomas Huth if (rwx == 2) { 956fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_ISEG; 957fcf5ef2aSThomas Huth env->error_code = 0; 958fcf5ef2aSThomas Huth } else { 959fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_DSEG; 960fcf5ef2aSThomas Huth env->error_code = 0; 961fcf5ef2aSThomas Huth env->spr[SPR_DAR] = eaddr; 962fcf5ef2aSThomas Huth } 963fcf5ef2aSThomas Huth return 1; 964fcf5ef2aSThomas Huth } 965fcf5ef2aSThomas Huth 966fcf5ef2aSThomas Huth skip_slb_search: 967fcf5ef2aSThomas Huth 968fcf5ef2aSThomas Huth /* 3. Check for segment level no-execute violation */ 969fcf5ef2aSThomas Huth if ((rwx == 2) && (slb->vsid & SLB_VSID_N)) { 9708fe08facSDavid Gibson ppc_hash64_set_isi(cs, SRR1_NOEXEC_GUARD); 971fcf5ef2aSThomas Huth return 1; 972fcf5ef2aSThomas Huth } 973fcf5ef2aSThomas Huth 974fcf5ef2aSThomas Huth /* 4. Locate the PTE in the hash table */ 9757222b94aSDavid Gibson ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift); 9767222b94aSDavid Gibson if (ptex == -1) { 977fcf5ef2aSThomas Huth if (rwx == 2) { 9788fe08facSDavid Gibson ppc_hash64_set_isi(cs, SRR1_NOPTE); 979fcf5ef2aSThomas Huth } else { 980da82c73aSSuraj Jitindar Singh int dsisr = DSISR_NOPTE; 981fcf5ef2aSThomas Huth if (rwx == 1) { 982da82c73aSSuraj Jitindar Singh dsisr |= DSISR_ISSTORE; 983fcf5ef2aSThomas Huth } 9848fe08facSDavid Gibson ppc_hash64_set_dsi(cs, eaddr, dsisr); 985fcf5ef2aSThomas Huth } 986fcf5ef2aSThomas Huth return 1; 987fcf5ef2aSThomas Huth } 988fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 9897222b94aSDavid Gibson "found PTE at index %08" HWADDR_PRIx "\n", ptex); 990fcf5ef2aSThomas Huth 991fcf5ef2aSThomas Huth /* 5. Check access permissions */ 992fcf5ef2aSThomas Huth 99307a68f99SSuraj Jitindar Singh exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte); 994fcf5ef2aSThomas Huth pp_prot = ppc_hash64_pte_prot(cpu, slb, pte); 995fcf5ef2aSThomas Huth amr_prot = ppc_hash64_amr_prot(cpu, pte); 99607a68f99SSuraj Jitindar Singh prot = exec_prot & pp_prot & amr_prot; 997fcf5ef2aSThomas Huth 998fcf5ef2aSThomas Huth if ((need_prot[rwx] & ~prot) != 0) { 999fcf5ef2aSThomas Huth /* Access right violation */ 1000fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n"); 1001fcf5ef2aSThomas Huth if (rwx == 2) { 1002a6152b52SSuraj Jitindar Singh int srr1 = 0; 100307a68f99SSuraj Jitindar Singh if (PAGE_EXEC & ~exec_prot) { 100407a68f99SSuraj Jitindar Singh srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */ 100507a68f99SSuraj Jitindar Singh } else if (PAGE_EXEC & ~pp_prot) { 1006a6152b52SSuraj Jitindar Singh srr1 |= SRR1_PROTFAULT; /* Access violates access authority */ 1007a6152b52SSuraj Jitindar Singh } 1008a6152b52SSuraj Jitindar Singh if (PAGE_EXEC & ~amr_prot) { 1009a6152b52SSuraj Jitindar Singh srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */ 1010a6152b52SSuraj Jitindar Singh } 10118fe08facSDavid Gibson ppc_hash64_set_isi(cs, srr1); 1012fcf5ef2aSThomas Huth } else { 1013da82c73aSSuraj Jitindar Singh int dsisr = 0; 1014fcf5ef2aSThomas Huth if (need_prot[rwx] & ~pp_prot) { 1015da82c73aSSuraj Jitindar Singh dsisr |= DSISR_PROTFAULT; 1016fcf5ef2aSThomas Huth } 1017fcf5ef2aSThomas Huth if (rwx == 1) { 1018da82c73aSSuraj Jitindar Singh dsisr |= DSISR_ISSTORE; 1019fcf5ef2aSThomas Huth } 1020fcf5ef2aSThomas Huth if (need_prot[rwx] & ~amr_prot) { 1021da82c73aSSuraj Jitindar Singh dsisr |= DSISR_AMR; 1022fcf5ef2aSThomas Huth } 10238fe08facSDavid Gibson ppc_hash64_set_dsi(cs, eaddr, dsisr); 1024fcf5ef2aSThomas Huth } 1025fcf5ef2aSThomas Huth return 1; 1026fcf5ef2aSThomas Huth } 1027fcf5ef2aSThomas Huth 1028fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n"); 1029fcf5ef2aSThomas Huth 1030fcf5ef2aSThomas Huth /* 6. Update PTE referenced and changed bits if necessary */ 1031fcf5ef2aSThomas Huth 1032a2dd4e83SBenjamin Herrenschmidt if (!(pte.pte1 & HPTE64_R_R)) { 1033a2dd4e83SBenjamin Herrenschmidt ppc_hash64_set_r(cpu, ptex, pte.pte1); 1034a2dd4e83SBenjamin Herrenschmidt } 1035a2dd4e83SBenjamin Herrenschmidt if (!(pte.pte1 & HPTE64_R_C)) { 1036fcf5ef2aSThomas Huth if (rwx == 1) { 1037a2dd4e83SBenjamin Herrenschmidt ppc_hash64_set_c(cpu, ptex, pte.pte1); 1038fcf5ef2aSThomas Huth } else { 1039d75cbae8SDavid Gibson /* 1040d75cbae8SDavid Gibson * Treat the page as read-only for now, so that a later write 1041d75cbae8SDavid Gibson * will pass through this function again to set the C bit 1042d75cbae8SDavid Gibson */ 1043fcf5ef2aSThomas Huth prot &= ~PAGE_WRITE; 1044fcf5ef2aSThomas Huth } 1045fcf5ef2aSThomas Huth } 1046fcf5ef2aSThomas Huth 1047fcf5ef2aSThomas Huth /* 7. Determine the real address from the PTE */ 1048fcf5ef2aSThomas Huth 1049fcf5ef2aSThomas Huth raddr = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr); 1050fcf5ef2aSThomas Huth 1051fcf5ef2aSThomas Huth tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK, 1052fcf5ef2aSThomas Huth prot, mmu_idx, 1ULL << apshift); 1053fcf5ef2aSThomas Huth 1054fcf5ef2aSThomas Huth return 0; 1055fcf5ef2aSThomas Huth } 1056fcf5ef2aSThomas Huth 1057fcf5ef2aSThomas Huth hwaddr ppc_hash64_get_phys_page_debug(PowerPCCPU *cpu, target_ulong addr) 1058fcf5ef2aSThomas Huth { 1059fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 10604c24a87fSDavid Gibson ppc_slb_t vrma_slbe; 1061fcf5ef2aSThomas Huth ppc_slb_t *slb; 10627222b94aSDavid Gibson hwaddr ptex, raddr; 1063fcf5ef2aSThomas Huth ppc_hash_pte64_t pte; 1064fcf5ef2aSThomas Huth unsigned apshift; 1065fcf5ef2aSThomas Huth 1066fcf5ef2aSThomas Huth /* Handle real mode */ 1067fcf5ef2aSThomas Huth if (msr_dr == 0) { 1068fcf5ef2aSThomas Huth /* In real mode the top 4 effective address bits are ignored */ 1069fcf5ef2aSThomas Huth raddr = addr & 0x0FFFFFFFFFFFFFFFULL; 1070fcf5ef2aSThomas Huth 1071682c1dfbSDavid Gibson if (cpu->vhyp) { 1072682c1dfbSDavid Gibson /* 1073682c1dfbSDavid Gibson * In virtual hypervisor mode, there's nothing to do: 1074682c1dfbSDavid Gibson * EA == GPA == qemu guest address 1075682c1dfbSDavid Gibson */ 1076682c1dfbSDavid Gibson return raddr; 1077682c1dfbSDavid Gibson } else if ((msr_hv || !env->has_hv_mode) && !(addr >> 63)) { 1078fcf5ef2aSThomas Huth /* In HV mode, add HRMOR if top EA bit is clear */ 1079fcf5ef2aSThomas Huth return raddr | env->spr[SPR_HRMOR]; 10801b99e029SDavid Gibson } else if (ppc_hash64_use_vrma(env)) { 1081682c1dfbSDavid Gibson /* Emulated VRMA mode */ 10824c24a87fSDavid Gibson slb = &vrma_slbe; 10834c24a87fSDavid Gibson if (build_vrma_slbe(cpu, slb) != 0) { 1084fcf5ef2aSThomas Huth return -1; 1085fcf5ef2aSThomas Huth } 1086fcf5ef2aSThomas Huth } else { 10873a56a55cSDavid Gibson target_ulong limit = rmls_limit(cpu); 10883a56a55cSDavid Gibson 1089682c1dfbSDavid Gibson /* Emulated old-style RMO mode, bounds check against RMLS */ 10903a56a55cSDavid Gibson if (raddr >= limit) { 1091fcf5ef2aSThomas Huth return -1; 1092fcf5ef2aSThomas Huth } 1093682c1dfbSDavid Gibson return raddr | env->spr[SPR_RMOR]; 1094682c1dfbSDavid Gibson } 1095fcf5ef2aSThomas Huth } else { 1096fcf5ef2aSThomas Huth slb = slb_lookup(cpu, addr); 1097fcf5ef2aSThomas Huth if (!slb) { 1098fcf5ef2aSThomas Huth return -1; 1099fcf5ef2aSThomas Huth } 1100fcf5ef2aSThomas Huth } 1101fcf5ef2aSThomas Huth 11027222b94aSDavid Gibson ptex = ppc_hash64_htab_lookup(cpu, slb, addr, &pte, &apshift); 11037222b94aSDavid Gibson if (ptex == -1) { 1104fcf5ef2aSThomas Huth return -1; 1105fcf5ef2aSThomas Huth } 1106fcf5ef2aSThomas Huth 1107fcf5ef2aSThomas Huth return deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, addr) 1108fcf5ef2aSThomas Huth & TARGET_PAGE_MASK; 1109fcf5ef2aSThomas Huth } 1110fcf5ef2aSThomas Huth 11117222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex, 1112fcf5ef2aSThomas Huth target_ulong pte0, target_ulong pte1) 1113fcf5ef2aSThomas Huth { 1114fcf5ef2aSThomas Huth /* 1115fcf5ef2aSThomas Huth * XXX: given the fact that there are too many segments to 1116fcf5ef2aSThomas Huth * invalidate, and we still don't have a tlb_flush_mask(env, n, 1117fcf5ef2aSThomas Huth * mask) in QEMU, we just invalidate all TLBs 1118fcf5ef2aSThomas Huth */ 1119fcf5ef2aSThomas Huth cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH; 1120fcf5ef2aSThomas Huth } 1121fcf5ef2aSThomas Huth 11225ad55315SDavid Gibson void ppc_store_lpcr(PowerPCCPU *cpu, target_ulong val) 1123fcf5ef2aSThomas Huth { 1124e232ecccSDavid Gibson PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 11255ad55315SDavid Gibson CPUPPCState *env = &cpu->env; 1126fcf5ef2aSThomas Huth 1127e232ecccSDavid Gibson env->spr[SPR_LPCR] = val & pcc->lpcr_mask; 1128fcf5ef2aSThomas Huth } 1129a059471dSDavid Gibson 11305ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val) 11315ad55315SDavid Gibson { 1132db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 11335ad55315SDavid Gibson 11345ad55315SDavid Gibson ppc_store_lpcr(cpu, val); 11355ad55315SDavid Gibson } 11365ad55315SDavid Gibson 1137a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu) 1138a059471dSDavid Gibson { 1139a059471dSDavid Gibson CPUPPCState *env = &cpu->env; 1140a059471dSDavid Gibson PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 1141a059471dSDavid Gibson 114221e405f1SDavid Gibson if (!pcc->hash64_opts) { 114321e405f1SDavid Gibson assert(!(env->mmu_model & POWERPC_MMU_64)); 114421e405f1SDavid Gibson return; 114521e405f1SDavid Gibson } 114621e405f1SDavid Gibson 114721e405f1SDavid Gibson cpu->hash64_opts = g_memdup(pcc->hash64_opts, sizeof(*cpu->hash64_opts)); 114821e405f1SDavid Gibson } 114921e405f1SDavid Gibson 115021e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu) 115121e405f1SDavid Gibson { 115221e405f1SDavid Gibson g_free(cpu->hash64_opts); 115321e405f1SDavid Gibson } 115421e405f1SDavid Gibson 115521e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = { 115658969eeeSDavid Gibson .flags = 0, 115767d7d66fSDavid Gibson .slb_size = 64, 1158a059471dSDavid Gibson .sps = { 1159a059471dSDavid Gibson { .page_shift = 12, /* 4K */ 1160a059471dSDavid Gibson .slb_enc = 0, 1161a059471dSDavid Gibson .enc = { { .page_shift = 12, .pte_enc = 0 } } 1162a059471dSDavid Gibson }, 1163a059471dSDavid Gibson { .page_shift = 24, /* 16M */ 1164a059471dSDavid Gibson .slb_enc = 0x100, 1165a059471dSDavid Gibson .enc = { { .page_shift = 24, .pte_enc = 0 } } 1166a059471dSDavid Gibson }, 1167a059471dSDavid Gibson }, 1168a059471dSDavid Gibson }; 1169b07c59f7SDavid Gibson 1170b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = { 117126cd35b8SDavid Gibson .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE, 117267d7d66fSDavid Gibson .slb_size = 32, 1173b07c59f7SDavid Gibson .sps = { 1174b07c59f7SDavid Gibson { 1175b07c59f7SDavid Gibson .page_shift = 12, /* 4K */ 1176b07c59f7SDavid Gibson .slb_enc = 0, 1177b07c59f7SDavid Gibson .enc = { { .page_shift = 12, .pte_enc = 0 }, 1178b07c59f7SDavid Gibson { .page_shift = 16, .pte_enc = 0x7 }, 1179b07c59f7SDavid Gibson { .page_shift = 24, .pte_enc = 0x38 }, }, 1180b07c59f7SDavid Gibson }, 1181b07c59f7SDavid Gibson { 1182b07c59f7SDavid Gibson .page_shift = 16, /* 64K */ 1183b07c59f7SDavid Gibson .slb_enc = SLB_VSID_64K, 1184b07c59f7SDavid Gibson .enc = { { .page_shift = 16, .pte_enc = 0x1 }, 1185b07c59f7SDavid Gibson { .page_shift = 24, .pte_enc = 0x8 }, }, 1186b07c59f7SDavid Gibson }, 1187b07c59f7SDavid Gibson { 1188b07c59f7SDavid Gibson .page_shift = 24, /* 16M */ 1189b07c59f7SDavid Gibson .slb_enc = SLB_VSID_16M, 1190b07c59f7SDavid Gibson .enc = { { .page_shift = 24, .pte_enc = 0 }, }, 1191b07c59f7SDavid Gibson }, 1192b07c59f7SDavid Gibson { 1193b07c59f7SDavid Gibson .page_shift = 34, /* 16G */ 1194b07c59f7SDavid Gibson .slb_enc = SLB_VSID_16G, 1195b07c59f7SDavid Gibson .enc = { { .page_shift = 34, .pte_enc = 0x3 }, }, 1196b07c59f7SDavid Gibson }, 1197b07c59f7SDavid Gibson } 1198b07c59f7SDavid Gibson }; 119927f00f0aSDavid Gibson 120027f00f0aSDavid Gibson void ppc_hash64_filter_pagesizes(PowerPCCPU *cpu, 120127f00f0aSDavid Gibson bool (*cb)(void *, uint32_t, uint32_t), 120227f00f0aSDavid Gibson void *opaque) 120327f00f0aSDavid Gibson { 120427f00f0aSDavid Gibson PPCHash64Options *opts = cpu->hash64_opts; 120527f00f0aSDavid Gibson int i; 120627f00f0aSDavid Gibson int n = 0; 120727f00f0aSDavid Gibson bool ci_largepage = false; 120827f00f0aSDavid Gibson 120927f00f0aSDavid Gibson assert(opts); 121027f00f0aSDavid Gibson 121127f00f0aSDavid Gibson n = 0; 121227f00f0aSDavid Gibson for (i = 0; i < ARRAY_SIZE(opts->sps); i++) { 121327f00f0aSDavid Gibson PPCHash64SegmentPageSizes *sps = &opts->sps[i]; 121427f00f0aSDavid Gibson int j; 121527f00f0aSDavid Gibson int m = 0; 121627f00f0aSDavid Gibson 121727f00f0aSDavid Gibson assert(n <= i); 121827f00f0aSDavid Gibson 121927f00f0aSDavid Gibson if (!sps->page_shift) { 122027f00f0aSDavid Gibson break; 122127f00f0aSDavid Gibson } 122227f00f0aSDavid Gibson 122327f00f0aSDavid Gibson for (j = 0; j < ARRAY_SIZE(sps->enc); j++) { 122427f00f0aSDavid Gibson PPCHash64PageSize *ps = &sps->enc[j]; 122527f00f0aSDavid Gibson 122627f00f0aSDavid Gibson assert(m <= j); 122727f00f0aSDavid Gibson if (!ps->page_shift) { 122827f00f0aSDavid Gibson break; 122927f00f0aSDavid Gibson } 123027f00f0aSDavid Gibson 123127f00f0aSDavid Gibson if (cb(opaque, sps->page_shift, ps->page_shift)) { 123227f00f0aSDavid Gibson if (ps->page_shift >= 16) { 123327f00f0aSDavid Gibson ci_largepage = true; 123427f00f0aSDavid Gibson } 123527f00f0aSDavid Gibson sps->enc[m++] = *ps; 123627f00f0aSDavid Gibson } 123727f00f0aSDavid Gibson } 123827f00f0aSDavid Gibson 123927f00f0aSDavid Gibson /* Clear rest of the row */ 124027f00f0aSDavid Gibson for (j = m; j < ARRAY_SIZE(sps->enc); j++) { 124127f00f0aSDavid Gibson memset(&sps->enc[j], 0, sizeof(sps->enc[j])); 124227f00f0aSDavid Gibson } 124327f00f0aSDavid Gibson 124427f00f0aSDavid Gibson if (m) { 124527f00f0aSDavid Gibson n++; 124627f00f0aSDavid Gibson } 124727f00f0aSDavid Gibson } 124827f00f0aSDavid Gibson 124927f00f0aSDavid Gibson /* Clear the rest of the table */ 125027f00f0aSDavid Gibson for (i = n; i < ARRAY_SIZE(opts->sps); i++) { 125127f00f0aSDavid Gibson memset(&opts->sps[i], 0, sizeof(opts->sps[i])); 125227f00f0aSDavid Gibson } 125327f00f0aSDavid Gibson 125427f00f0aSDavid Gibson if (!ci_largepage) { 125527f00f0aSDavid Gibson opts->flags &= ~PPC_HASH64_CI_LARGEPAGE; 125627f00f0aSDavid Gibson } 125727f00f0aSDavid Gibson } 1258