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 98fcf5ef2aSThomas Huth void helper_slbia(CPUPPCState *env) 99fcf5ef2aSThomas Huth { 100db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 101fcf5ef2aSThomas Huth int n; 102fcf5ef2aSThomas Huth 103fcf5ef2aSThomas Huth /* XXX: Warning: slbia never invalidates the first segment */ 10467d7d66fSDavid Gibson for (n = 1; n < cpu->hash64_opts->slb_size; n++) { 105fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[n]; 106fcf5ef2aSThomas Huth 107fcf5ef2aSThomas Huth if (slb->esid & SLB_ESID_V) { 108fcf5ef2aSThomas Huth slb->esid &= ~SLB_ESID_V; 109d75cbae8SDavid Gibson /* 110d75cbae8SDavid Gibson * XXX: given the fact that segment size is 256 MB or 1TB, 111fcf5ef2aSThomas Huth * and we still don't have a tlb_flush_mask(env, n, mask) 112fcf5ef2aSThomas Huth * in QEMU, we just invalidate all TLBs 113fcf5ef2aSThomas Huth */ 114fcf5ef2aSThomas Huth env->tlb_need_flush |= TLB_NEED_LOCAL_FLUSH; 115fcf5ef2aSThomas Huth } 116fcf5ef2aSThomas Huth } 117fcf5ef2aSThomas Huth } 118fcf5ef2aSThomas Huth 119a63f1dfcSNikunj A Dadhania static void __helper_slbie(CPUPPCState *env, target_ulong addr, 120a63f1dfcSNikunj A Dadhania target_ulong global) 121fcf5ef2aSThomas Huth { 122db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 123fcf5ef2aSThomas Huth ppc_slb_t *slb; 124fcf5ef2aSThomas Huth 125fcf5ef2aSThomas Huth slb = slb_lookup(cpu, addr); 126fcf5ef2aSThomas Huth if (!slb) { 127fcf5ef2aSThomas Huth return; 128fcf5ef2aSThomas Huth } 129fcf5ef2aSThomas Huth 130fcf5ef2aSThomas Huth if (slb->esid & SLB_ESID_V) { 131fcf5ef2aSThomas Huth slb->esid &= ~SLB_ESID_V; 132fcf5ef2aSThomas Huth 133d75cbae8SDavid Gibson /* 134d75cbae8SDavid Gibson * XXX: given the fact that segment size is 256 MB or 1TB, 135fcf5ef2aSThomas Huth * and we still don't have a tlb_flush_mask(env, n, mask) 136fcf5ef2aSThomas Huth * in QEMU, we just invalidate all TLBs 137fcf5ef2aSThomas Huth */ 138a63f1dfcSNikunj A Dadhania env->tlb_need_flush |= 139a63f1dfcSNikunj A Dadhania (global == false ? TLB_NEED_LOCAL_FLUSH : TLB_NEED_GLOBAL_FLUSH); 140fcf5ef2aSThomas Huth } 141fcf5ef2aSThomas Huth } 142fcf5ef2aSThomas Huth 143a63f1dfcSNikunj A Dadhania void helper_slbie(CPUPPCState *env, target_ulong addr) 144a63f1dfcSNikunj A Dadhania { 145a63f1dfcSNikunj A Dadhania __helper_slbie(env, addr, false); 146a63f1dfcSNikunj A Dadhania } 147a63f1dfcSNikunj A Dadhania 148a63f1dfcSNikunj A Dadhania void helper_slbieg(CPUPPCState *env, target_ulong addr) 149a63f1dfcSNikunj A Dadhania { 150a63f1dfcSNikunj A Dadhania __helper_slbie(env, addr, true); 151a63f1dfcSNikunj A Dadhania } 152a63f1dfcSNikunj A Dadhania 153fcf5ef2aSThomas Huth int ppc_store_slb(PowerPCCPU *cpu, target_ulong slot, 154fcf5ef2aSThomas Huth target_ulong esid, target_ulong vsid) 155fcf5ef2aSThomas Huth { 156fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 157fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 158b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = NULL; 159fcf5ef2aSThomas Huth int i; 160fcf5ef2aSThomas Huth 16167d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 162fcf5ef2aSThomas Huth return -1; /* Bad slot number */ 163fcf5ef2aSThomas Huth } 164fcf5ef2aSThomas Huth if (esid & ~(SLB_ESID_ESID | SLB_ESID_V)) { 165fcf5ef2aSThomas Huth return -1; /* Reserved bits set */ 166fcf5ef2aSThomas Huth } 167fcf5ef2aSThomas Huth if (vsid & (SLB_VSID_B & ~SLB_VSID_B_1T)) { 168fcf5ef2aSThomas Huth return -1; /* Bad segment size */ 169fcf5ef2aSThomas Huth } 17058969eeeSDavid Gibson if ((vsid & SLB_VSID_B) && !(ppc_hash64_has(cpu, PPC_HASH64_1TSEG))) { 171fcf5ef2aSThomas Huth return -1; /* 1T segment on MMU that doesn't support it */ 172fcf5ef2aSThomas Huth } 173fcf5ef2aSThomas Huth 174fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 175b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i]; 176fcf5ef2aSThomas Huth 177fcf5ef2aSThomas Huth if (!sps1->page_shift) { 178fcf5ef2aSThomas Huth break; 179fcf5ef2aSThomas Huth } 180fcf5ef2aSThomas Huth 181fcf5ef2aSThomas Huth if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) { 182fcf5ef2aSThomas Huth sps = sps1; 183fcf5ef2aSThomas Huth break; 184fcf5ef2aSThomas Huth } 185fcf5ef2aSThomas Huth } 186fcf5ef2aSThomas Huth 187fcf5ef2aSThomas Huth if (!sps) { 188fcf5ef2aSThomas Huth error_report("Bad page size encoding in SLB store: slot "TARGET_FMT_lu 189fcf5ef2aSThomas Huth " esid 0x"TARGET_FMT_lx" vsid 0x"TARGET_FMT_lx, 190fcf5ef2aSThomas Huth slot, esid, vsid); 191fcf5ef2aSThomas Huth return -1; 192fcf5ef2aSThomas Huth } 193fcf5ef2aSThomas Huth 194fcf5ef2aSThomas Huth slb->esid = esid; 195fcf5ef2aSThomas Huth slb->vsid = vsid; 196fcf5ef2aSThomas Huth slb->sps = sps; 197fcf5ef2aSThomas Huth 19876134d48SSuraj Jitindar Singh LOG_SLB("%s: " TARGET_FMT_lu " " TARGET_FMT_lx " - " TARGET_FMT_lx 19976134d48SSuraj Jitindar Singh " => %016" PRIx64 " %016" PRIx64 "\n", __func__, slot, esid, vsid, 200fcf5ef2aSThomas Huth slb->esid, slb->vsid); 201fcf5ef2aSThomas Huth 202fcf5ef2aSThomas Huth return 0; 203fcf5ef2aSThomas Huth } 204fcf5ef2aSThomas Huth 205fcf5ef2aSThomas Huth static int ppc_load_slb_esid(PowerPCCPU *cpu, target_ulong rb, 206fcf5ef2aSThomas Huth target_ulong *rt) 207fcf5ef2aSThomas Huth { 208fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 209fcf5ef2aSThomas Huth int slot = rb & 0xfff; 210fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 211fcf5ef2aSThomas Huth 21267d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 213fcf5ef2aSThomas Huth return -1; 214fcf5ef2aSThomas Huth } 215fcf5ef2aSThomas Huth 216fcf5ef2aSThomas Huth *rt = slb->esid; 217fcf5ef2aSThomas Huth return 0; 218fcf5ef2aSThomas Huth } 219fcf5ef2aSThomas Huth 220fcf5ef2aSThomas Huth static int ppc_load_slb_vsid(PowerPCCPU *cpu, target_ulong rb, 221fcf5ef2aSThomas Huth target_ulong *rt) 222fcf5ef2aSThomas Huth { 223fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 224fcf5ef2aSThomas Huth int slot = rb & 0xfff; 225fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->slb[slot]; 226fcf5ef2aSThomas Huth 22767d7d66fSDavid Gibson if (slot >= cpu->hash64_opts->slb_size) { 228fcf5ef2aSThomas Huth return -1; 229fcf5ef2aSThomas Huth } 230fcf5ef2aSThomas Huth 231fcf5ef2aSThomas Huth *rt = slb->vsid; 232fcf5ef2aSThomas Huth return 0; 233fcf5ef2aSThomas Huth } 234fcf5ef2aSThomas Huth 235fcf5ef2aSThomas Huth static int ppc_find_slb_vsid(PowerPCCPU *cpu, target_ulong rb, 236fcf5ef2aSThomas Huth target_ulong *rt) 237fcf5ef2aSThomas Huth { 238fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 239fcf5ef2aSThomas Huth ppc_slb_t *slb; 240fcf5ef2aSThomas Huth 241fcf5ef2aSThomas Huth if (!msr_is_64bit(env, env->msr)) { 242fcf5ef2aSThomas Huth rb &= 0xffffffff; 243fcf5ef2aSThomas Huth } 244fcf5ef2aSThomas Huth slb = slb_lookup(cpu, rb); 245fcf5ef2aSThomas Huth if (slb == NULL) { 246fcf5ef2aSThomas Huth *rt = (target_ulong)-1ul; 247fcf5ef2aSThomas Huth } else { 248fcf5ef2aSThomas Huth *rt = slb->vsid; 249fcf5ef2aSThomas Huth } 250fcf5ef2aSThomas Huth return 0; 251fcf5ef2aSThomas Huth } 252fcf5ef2aSThomas Huth 253fcf5ef2aSThomas Huth void helper_store_slb(CPUPPCState *env, target_ulong rb, target_ulong rs) 254fcf5ef2aSThomas Huth { 255db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 256fcf5ef2aSThomas Huth 257fcf5ef2aSThomas Huth if (ppc_store_slb(cpu, rb & 0xfff, rb & ~0xfffULL, rs) < 0) { 258fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 259fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 260fcf5ef2aSThomas Huth } 261fcf5ef2aSThomas Huth } 262fcf5ef2aSThomas Huth 263fcf5ef2aSThomas Huth target_ulong helper_load_slb_esid(CPUPPCState *env, target_ulong rb) 264fcf5ef2aSThomas Huth { 265db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 266fcf5ef2aSThomas Huth target_ulong rt = 0; 267fcf5ef2aSThomas Huth 268fcf5ef2aSThomas Huth if (ppc_load_slb_esid(cpu, rb, &rt) < 0) { 269fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 270fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 271fcf5ef2aSThomas Huth } 272fcf5ef2aSThomas Huth return rt; 273fcf5ef2aSThomas Huth } 274fcf5ef2aSThomas Huth 275fcf5ef2aSThomas Huth target_ulong helper_find_slb_vsid(CPUPPCState *env, target_ulong rb) 276fcf5ef2aSThomas Huth { 277db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 278fcf5ef2aSThomas Huth target_ulong rt = 0; 279fcf5ef2aSThomas Huth 280fcf5ef2aSThomas Huth if (ppc_find_slb_vsid(cpu, rb, &rt) < 0) { 281fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 282fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 283fcf5ef2aSThomas Huth } 284fcf5ef2aSThomas Huth return rt; 285fcf5ef2aSThomas Huth } 286fcf5ef2aSThomas Huth 287fcf5ef2aSThomas Huth target_ulong helper_load_slb_vsid(CPUPPCState *env, target_ulong rb) 288fcf5ef2aSThomas Huth { 289db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 290fcf5ef2aSThomas Huth target_ulong rt = 0; 291fcf5ef2aSThomas Huth 292fcf5ef2aSThomas Huth if (ppc_load_slb_vsid(cpu, rb, &rt) < 0) { 293fcf5ef2aSThomas Huth raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 294fcf5ef2aSThomas Huth POWERPC_EXCP_INVAL, GETPC()); 295fcf5ef2aSThomas Huth } 296fcf5ef2aSThomas Huth return rt; 297fcf5ef2aSThomas Huth } 298fcf5ef2aSThomas Huth 29907a68f99SSuraj Jitindar Singh /* Check No-Execute or Guarded Storage */ 30007a68f99SSuraj Jitindar Singh static inline int ppc_hash64_pte_noexec_guard(PowerPCCPU *cpu, 30107a68f99SSuraj Jitindar Singh ppc_hash_pte64_t pte) 30207a68f99SSuraj Jitindar Singh { 30307a68f99SSuraj Jitindar Singh /* Exec permissions CANNOT take away read or write permissions */ 30407a68f99SSuraj Jitindar Singh return (pte.pte1 & HPTE64_R_N) || (pte.pte1 & HPTE64_R_G) ? 30507a68f99SSuraj Jitindar Singh PAGE_READ | PAGE_WRITE : PAGE_READ | PAGE_WRITE | PAGE_EXEC; 30607a68f99SSuraj Jitindar Singh } 30707a68f99SSuraj Jitindar Singh 30807a68f99SSuraj Jitindar Singh /* Check Basic Storage Protection */ 309fcf5ef2aSThomas Huth static int ppc_hash64_pte_prot(PowerPCCPU *cpu, 310fcf5ef2aSThomas Huth ppc_slb_t *slb, ppc_hash_pte64_t pte) 311fcf5ef2aSThomas Huth { 312fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 313fcf5ef2aSThomas Huth unsigned pp, key; 314d75cbae8SDavid Gibson /* 315d75cbae8SDavid Gibson * Some pp bit combinations have undefined behaviour, so default 316d75cbae8SDavid Gibson * to no access in those cases 317d75cbae8SDavid Gibson */ 318fcf5ef2aSThomas Huth int prot = 0; 319fcf5ef2aSThomas Huth 320fcf5ef2aSThomas Huth key = !!(msr_pr ? (slb->vsid & SLB_VSID_KP) 321fcf5ef2aSThomas Huth : (slb->vsid & SLB_VSID_KS)); 322fcf5ef2aSThomas Huth pp = (pte.pte1 & HPTE64_R_PP) | ((pte.pte1 & HPTE64_R_PP0) >> 61); 323fcf5ef2aSThomas Huth 324fcf5ef2aSThomas Huth if (key == 0) { 325fcf5ef2aSThomas Huth switch (pp) { 326fcf5ef2aSThomas Huth case 0x0: 327fcf5ef2aSThomas Huth case 0x1: 328fcf5ef2aSThomas Huth case 0x2: 329347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 330fcf5ef2aSThomas Huth break; 331fcf5ef2aSThomas Huth 332fcf5ef2aSThomas Huth case 0x3: 333fcf5ef2aSThomas Huth case 0x6: 334347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_EXEC; 335fcf5ef2aSThomas Huth break; 336fcf5ef2aSThomas Huth } 337fcf5ef2aSThomas Huth } else { 338fcf5ef2aSThomas Huth switch (pp) { 339fcf5ef2aSThomas Huth case 0x0: 340fcf5ef2aSThomas Huth case 0x6: 341fcf5ef2aSThomas Huth break; 342fcf5ef2aSThomas Huth 343fcf5ef2aSThomas Huth case 0x1: 344fcf5ef2aSThomas Huth case 0x3: 345347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_EXEC; 346fcf5ef2aSThomas Huth break; 347fcf5ef2aSThomas Huth 348fcf5ef2aSThomas Huth case 0x2: 349347a5c73SSuraj Jitindar Singh prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 350fcf5ef2aSThomas Huth break; 351fcf5ef2aSThomas Huth } 352fcf5ef2aSThomas Huth } 353fcf5ef2aSThomas Huth 354fcf5ef2aSThomas Huth return prot; 355fcf5ef2aSThomas Huth } 356fcf5ef2aSThomas Huth 357a6152b52SSuraj Jitindar Singh /* Check the instruction access permissions specified in the IAMR */ 358a6152b52SSuraj Jitindar Singh static int ppc_hash64_iamr_prot(PowerPCCPU *cpu, int key) 359a6152b52SSuraj Jitindar Singh { 360a6152b52SSuraj Jitindar Singh CPUPPCState *env = &cpu->env; 361a6152b52SSuraj Jitindar Singh int iamr_bits = (env->spr[SPR_IAMR] >> 2 * (31 - key)) & 0x3; 362a6152b52SSuraj Jitindar Singh 363a6152b52SSuraj Jitindar Singh /* 364a6152b52SSuraj Jitindar Singh * An instruction fetch is permitted if the IAMR bit is 0. 365a6152b52SSuraj Jitindar Singh * If the bit is set, return PAGE_READ | PAGE_WRITE because this bit 366a6152b52SSuraj Jitindar Singh * can only take away EXEC permissions not READ or WRITE permissions. 367a6152b52SSuraj Jitindar Singh * If bit is cleared return PAGE_READ | PAGE_WRITE | PAGE_EXEC since 368a6152b52SSuraj Jitindar Singh * EXEC permissions are allowed. 369a6152b52SSuraj Jitindar Singh */ 370a6152b52SSuraj Jitindar Singh return (iamr_bits & 0x1) ? PAGE_READ | PAGE_WRITE : 371a6152b52SSuraj Jitindar Singh PAGE_READ | PAGE_WRITE | PAGE_EXEC; 372a6152b52SSuraj Jitindar Singh } 373a6152b52SSuraj Jitindar Singh 374fcf5ef2aSThomas Huth static int ppc_hash64_amr_prot(PowerPCCPU *cpu, ppc_hash_pte64_t pte) 375fcf5ef2aSThomas Huth { 376fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 377fcf5ef2aSThomas Huth int key, amrbits; 378fcf5ef2aSThomas Huth int prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 379fcf5ef2aSThomas Huth 380fcf5ef2aSThomas Huth /* Only recent MMUs implement Virtual Page Class Key Protection */ 38158969eeeSDavid Gibson if (!ppc_hash64_has(cpu, PPC_HASH64_AMR)) { 382fcf5ef2aSThomas Huth return prot; 383fcf5ef2aSThomas Huth } 384fcf5ef2aSThomas Huth 385fcf5ef2aSThomas Huth key = HPTE64_R_KEY(pte.pte1); 386fcf5ef2aSThomas Huth amrbits = (env->spr[SPR_AMR] >> 2 * (31 - key)) & 0x3; 387fcf5ef2aSThomas Huth 388fcf5ef2aSThomas Huth /* fprintf(stderr, "AMR protection: key=%d AMR=0x%" PRIx64 "\n", key, */ 389fcf5ef2aSThomas Huth /* env->spr[SPR_AMR]); */ 390fcf5ef2aSThomas Huth 391fcf5ef2aSThomas Huth /* 392fcf5ef2aSThomas Huth * A store is permitted if the AMR bit is 0. Remove write 393fcf5ef2aSThomas Huth * protection if it is set. 394fcf5ef2aSThomas Huth */ 395fcf5ef2aSThomas Huth if (amrbits & 0x2) { 396fcf5ef2aSThomas Huth prot &= ~PAGE_WRITE; 397fcf5ef2aSThomas Huth } 398fcf5ef2aSThomas Huth /* 399fcf5ef2aSThomas Huth * A load is permitted if the AMR bit is 0. Remove read 400fcf5ef2aSThomas Huth * protection if it is set. 401fcf5ef2aSThomas Huth */ 402fcf5ef2aSThomas Huth if (amrbits & 0x1) { 403fcf5ef2aSThomas Huth prot &= ~PAGE_READ; 404fcf5ef2aSThomas Huth } 405fcf5ef2aSThomas Huth 406a6152b52SSuraj Jitindar Singh switch (env->mmu_model) { 407a6152b52SSuraj Jitindar Singh /* 408a6152b52SSuraj Jitindar Singh * MMU version 2.07 and later support IAMR 409a6152b52SSuraj Jitindar Singh * Check if the IAMR allows the instruction access - it will return 410a6152b52SSuraj Jitindar Singh * PAGE_EXEC if it doesn't (and thus that bit will be cleared) or 0 411a6152b52SSuraj Jitindar Singh * if it does (and prot will be unchanged indicating execution support). 412a6152b52SSuraj Jitindar Singh */ 413a6152b52SSuraj Jitindar Singh case POWERPC_MMU_2_07: 414a6152b52SSuraj Jitindar Singh case POWERPC_MMU_3_00: 415a6152b52SSuraj Jitindar Singh prot &= ppc_hash64_iamr_prot(cpu, key); 416a6152b52SSuraj Jitindar Singh break; 417a6152b52SSuraj Jitindar Singh default: 418a6152b52SSuraj Jitindar Singh break; 419a6152b52SSuraj Jitindar Singh } 420a6152b52SSuraj Jitindar Singh 421fcf5ef2aSThomas Huth return prot; 422fcf5ef2aSThomas Huth } 423fcf5ef2aSThomas Huth 4247222b94aSDavid Gibson const ppc_hash_pte64_t *ppc_hash64_map_hptes(PowerPCCPU *cpu, 4257222b94aSDavid Gibson hwaddr ptex, int n) 426fcf5ef2aSThomas Huth { 4277222b94aSDavid Gibson hwaddr pte_offset = ptex * HASH_PTE_SIZE_64; 4283367c62fSBenjamin Herrenschmidt hwaddr base; 4297222b94aSDavid Gibson hwaddr plen = n * HASH_PTE_SIZE_64; 430e57ca75cSDavid Gibson const ppc_hash_pte64_t *hptes; 431e57ca75cSDavid Gibson 432e57ca75cSDavid Gibson if (cpu->vhyp) { 433e57ca75cSDavid Gibson PPCVirtualHypervisorClass *vhc = 434e57ca75cSDavid Gibson PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 435e57ca75cSDavid Gibson return vhc->map_hptes(cpu->vhyp, ptex, n); 436e57ca75cSDavid Gibson } 4373367c62fSBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 438e57ca75cSDavid Gibson 439e57ca75cSDavid Gibson if (!base) { 440e57ca75cSDavid Gibson return NULL; 441e57ca75cSDavid Gibson } 442e57ca75cSDavid Gibson 443f26404fbSPeter Maydell hptes = address_space_map(CPU(cpu)->as, base + pte_offset, &plen, false, 444f26404fbSPeter Maydell MEMTXATTRS_UNSPECIFIED); 4457222b94aSDavid Gibson if (plen < (n * HASH_PTE_SIZE_64)) { 4467222b94aSDavid Gibson hw_error("%s: Unable to map all requested HPTEs\n", __func__); 447fcf5ef2aSThomas Huth } 4487222b94aSDavid Gibson return hptes; 449fcf5ef2aSThomas Huth } 450fcf5ef2aSThomas Huth 4517222b94aSDavid Gibson void ppc_hash64_unmap_hptes(PowerPCCPU *cpu, const ppc_hash_pte64_t *hptes, 4527222b94aSDavid Gibson hwaddr ptex, int n) 453fcf5ef2aSThomas Huth { 454e57ca75cSDavid Gibson if (cpu->vhyp) { 455e57ca75cSDavid Gibson PPCVirtualHypervisorClass *vhc = 456e57ca75cSDavid Gibson PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 457e57ca75cSDavid Gibson vhc->unmap_hptes(cpu->vhyp, hptes, ptex, n); 458e57ca75cSDavid Gibson return; 459e57ca75cSDavid Gibson } 460e57ca75cSDavid Gibson 4617222b94aSDavid Gibson address_space_unmap(CPU(cpu)->as, (void *)hptes, n * HASH_PTE_SIZE_64, 4627222b94aSDavid Gibson false, n * HASH_PTE_SIZE_64); 463fcf5ef2aSThomas Huth } 464fcf5ef2aSThomas Huth 465b07c59f7SDavid Gibson static unsigned hpte_page_shift(const PPCHash64SegmentPageSizes *sps, 466fcf5ef2aSThomas Huth uint64_t pte0, uint64_t pte1) 467fcf5ef2aSThomas Huth { 468fcf5ef2aSThomas Huth int i; 469fcf5ef2aSThomas Huth 470fcf5ef2aSThomas Huth if (!(pte0 & HPTE64_V_LARGE)) { 471fcf5ef2aSThomas Huth if (sps->page_shift != 12) { 472fcf5ef2aSThomas Huth /* 4kiB page in a non 4kiB segment */ 473fcf5ef2aSThomas Huth return 0; 474fcf5ef2aSThomas Huth } 475fcf5ef2aSThomas Huth /* Normal 4kiB page */ 476fcf5ef2aSThomas Huth return 12; 477fcf5ef2aSThomas Huth } 478fcf5ef2aSThomas Huth 479fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 480b07c59f7SDavid Gibson const PPCHash64PageSize *ps = &sps->enc[i]; 481fcf5ef2aSThomas Huth uint64_t mask; 482fcf5ef2aSThomas Huth 483fcf5ef2aSThomas Huth if (!ps->page_shift) { 484fcf5ef2aSThomas Huth break; 485fcf5ef2aSThomas Huth } 486fcf5ef2aSThomas Huth 487fcf5ef2aSThomas Huth if (ps->page_shift == 12) { 488fcf5ef2aSThomas Huth /* L bit is set so this can't be a 4kiB page */ 489fcf5ef2aSThomas Huth continue; 490fcf5ef2aSThomas Huth } 491fcf5ef2aSThomas Huth 492fcf5ef2aSThomas Huth mask = ((1ULL << ps->page_shift) - 1) & HPTE64_R_RPN; 493fcf5ef2aSThomas Huth 494fcf5ef2aSThomas Huth if ((pte1 & mask) == ((uint64_t)ps->pte_enc << HPTE64_R_RPN_SHIFT)) { 495fcf5ef2aSThomas Huth return ps->page_shift; 496fcf5ef2aSThomas Huth } 497fcf5ef2aSThomas Huth } 498fcf5ef2aSThomas Huth 499fcf5ef2aSThomas Huth return 0; /* Bad page size encoding */ 500fcf5ef2aSThomas Huth } 501fcf5ef2aSThomas Huth 50234525595SBenjamin Herrenschmidt static void ppc64_v3_new_to_old_hpte(target_ulong *pte0, target_ulong *pte1) 50334525595SBenjamin Herrenschmidt { 50434525595SBenjamin Herrenschmidt /* Insert B into pte0 */ 50534525595SBenjamin Herrenschmidt *pte0 = (*pte0 & HPTE64_V_COMMON_BITS) | 50634525595SBenjamin Herrenschmidt ((*pte1 & HPTE64_R_3_0_SSIZE_MASK) << 50734525595SBenjamin Herrenschmidt (HPTE64_V_SSIZE_SHIFT - HPTE64_R_3_0_SSIZE_SHIFT)); 50834525595SBenjamin Herrenschmidt 50934525595SBenjamin Herrenschmidt /* Remove B from pte1 */ 51034525595SBenjamin Herrenschmidt *pte1 = *pte1 & ~HPTE64_R_3_0_SSIZE_MASK; 51134525595SBenjamin Herrenschmidt } 51234525595SBenjamin Herrenschmidt 51334525595SBenjamin Herrenschmidt 514fcf5ef2aSThomas Huth static hwaddr ppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash, 515b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps, 516fcf5ef2aSThomas Huth target_ulong ptem, 517fcf5ef2aSThomas Huth ppc_hash_pte64_t *pte, unsigned *pshift) 518fcf5ef2aSThomas Huth { 519fcf5ef2aSThomas Huth int i; 5207222b94aSDavid Gibson const ppc_hash_pte64_t *pteg; 521fcf5ef2aSThomas Huth target_ulong pte0, pte1; 5227222b94aSDavid Gibson target_ulong ptex; 523fcf5ef2aSThomas Huth 52436778660SDavid Gibson ptex = (hash & ppc_hash64_hpt_mask(cpu)) * HPTES_PER_GROUP; 5257222b94aSDavid Gibson pteg = ppc_hash64_map_hptes(cpu, ptex, HPTES_PER_GROUP); 5267222b94aSDavid Gibson if (!pteg) { 527fcf5ef2aSThomas Huth return -1; 528fcf5ef2aSThomas Huth } 529fcf5ef2aSThomas Huth for (i = 0; i < HPTES_PER_GROUP; i++) { 5307222b94aSDavid Gibson pte0 = ppc_hash64_hpte0(cpu, pteg, i); 5313054b0caSBenjamin Herrenschmidt /* 5323054b0caSBenjamin Herrenschmidt * pte0 contains the valid bit and must be read before pte1, 5333054b0caSBenjamin Herrenschmidt * otherwise we might see an old pte1 with a new valid bit and 5343054b0caSBenjamin Herrenschmidt * thus an inconsistent hpte value 5353054b0caSBenjamin Herrenschmidt */ 5363054b0caSBenjamin Herrenschmidt smp_rmb(); 5377222b94aSDavid Gibson pte1 = ppc_hash64_hpte1(cpu, pteg, i); 538fcf5ef2aSThomas Huth 53934525595SBenjamin Herrenschmidt /* Convert format if necessary */ 54034525595SBenjamin Herrenschmidt if (cpu->env.mmu_model == POWERPC_MMU_3_00 && !cpu->vhyp) { 54134525595SBenjamin Herrenschmidt ppc64_v3_new_to_old_hpte(&pte0, &pte1); 54234525595SBenjamin Herrenschmidt } 54334525595SBenjamin Herrenschmidt 544fcf5ef2aSThomas Huth /* This compares V, B, H (secondary) and the AVPN */ 545fcf5ef2aSThomas Huth if (HPTE64_V_COMPARE(pte0, ptem)) { 546fcf5ef2aSThomas Huth *pshift = hpte_page_shift(sps, pte0, pte1); 547fcf5ef2aSThomas Huth /* 548fcf5ef2aSThomas Huth * If there is no match, ignore the PTE, it could simply 549fcf5ef2aSThomas Huth * be for a different segment size encoding and the 550fcf5ef2aSThomas Huth * architecture specifies we should not match. Linux will 551fcf5ef2aSThomas Huth * potentially leave behind PTEs for the wrong base page 552fcf5ef2aSThomas Huth * size when demoting segments. 553fcf5ef2aSThomas Huth */ 554fcf5ef2aSThomas Huth if (*pshift == 0) { 555fcf5ef2aSThomas Huth continue; 556fcf5ef2aSThomas Huth } 557d75cbae8SDavid Gibson /* 558d75cbae8SDavid Gibson * We don't do anything with pshift yet as qemu TLB only 559d75cbae8SDavid Gibson * deals with 4K pages anyway 560fcf5ef2aSThomas Huth */ 561fcf5ef2aSThomas Huth pte->pte0 = pte0; 562fcf5ef2aSThomas Huth pte->pte1 = pte1; 5637222b94aSDavid Gibson ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP); 5647222b94aSDavid Gibson return ptex + i; 565fcf5ef2aSThomas Huth } 566fcf5ef2aSThomas Huth } 5677222b94aSDavid Gibson ppc_hash64_unmap_hptes(cpu, pteg, ptex, HPTES_PER_GROUP); 568fcf5ef2aSThomas Huth /* 569fcf5ef2aSThomas Huth * We didn't find a valid entry. 570fcf5ef2aSThomas Huth */ 571fcf5ef2aSThomas Huth return -1; 572fcf5ef2aSThomas Huth } 573fcf5ef2aSThomas Huth 574fcf5ef2aSThomas Huth static hwaddr ppc_hash64_htab_lookup(PowerPCCPU *cpu, 575fcf5ef2aSThomas Huth ppc_slb_t *slb, target_ulong eaddr, 576fcf5ef2aSThomas Huth ppc_hash_pte64_t *pte, unsigned *pshift) 577fcf5ef2aSThomas Huth { 578fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 5797222b94aSDavid Gibson hwaddr hash, ptex; 580fcf5ef2aSThomas Huth uint64_t vsid, epnmask, epn, ptem; 581b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = slb->sps; 582fcf5ef2aSThomas Huth 583d75cbae8SDavid Gibson /* 584d75cbae8SDavid Gibson * The SLB store path should prevent any bad page size encodings 585d75cbae8SDavid Gibson * getting in there, so: 586d75cbae8SDavid Gibson */ 587fcf5ef2aSThomas Huth assert(sps); 588fcf5ef2aSThomas Huth 589fcf5ef2aSThomas Huth /* If ISL is set in LPCR we need to clamp the page size to 4K */ 590fcf5ef2aSThomas Huth if (env->spr[SPR_LPCR] & LPCR_ISL) { 591fcf5ef2aSThomas Huth /* We assume that when using TCG, 4k is first entry of SPS */ 592b07c59f7SDavid Gibson sps = &cpu->hash64_opts->sps[0]; 593fcf5ef2aSThomas Huth assert(sps->page_shift == 12); 594fcf5ef2aSThomas Huth } 595fcf5ef2aSThomas Huth 596fcf5ef2aSThomas Huth epnmask = ~((1ULL << sps->page_shift) - 1); 597fcf5ef2aSThomas Huth 598fcf5ef2aSThomas Huth if (slb->vsid & SLB_VSID_B) { 599fcf5ef2aSThomas Huth /* 1TB segment */ 600fcf5ef2aSThomas Huth vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT_1T; 601fcf5ef2aSThomas Huth epn = (eaddr & ~SEGMENT_MASK_1T) & epnmask; 602fcf5ef2aSThomas Huth hash = vsid ^ (vsid << 25) ^ (epn >> sps->page_shift); 603fcf5ef2aSThomas Huth } else { 604fcf5ef2aSThomas Huth /* 256M segment */ 605fcf5ef2aSThomas Huth vsid = (slb->vsid & SLB_VSID_VSID) >> SLB_VSID_SHIFT; 606fcf5ef2aSThomas Huth epn = (eaddr & ~SEGMENT_MASK_256M) & epnmask; 607fcf5ef2aSThomas Huth hash = vsid ^ (epn >> sps->page_shift); 608fcf5ef2aSThomas Huth } 609fcf5ef2aSThomas Huth ptem = (slb->vsid & SLB_VSID_PTEM) | ((epn >> 16) & HPTE64_V_AVPN); 610fcf5ef2aSThomas Huth ptem |= HPTE64_V_VALID; 611fcf5ef2aSThomas Huth 612fcf5ef2aSThomas Huth /* Page address translation */ 613fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 614fcf5ef2aSThomas Huth "htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx 615fcf5ef2aSThomas Huth " hash " TARGET_FMT_plx "\n", 61636778660SDavid Gibson ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), hash); 617fcf5ef2aSThomas Huth 618fcf5ef2aSThomas Huth /* Primary PTEG lookup */ 619fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 620fcf5ef2aSThomas Huth "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx 621fcf5ef2aSThomas Huth " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx 622fcf5ef2aSThomas Huth " hash=" TARGET_FMT_plx "\n", 62336778660SDavid Gibson ppc_hash64_hpt_base(cpu), ppc_hash64_hpt_mask(cpu), 62436778660SDavid Gibson vsid, ptem, hash); 6257222b94aSDavid Gibson ptex = ppc_hash64_pteg_search(cpu, hash, sps, ptem, pte, pshift); 626fcf5ef2aSThomas Huth 6277222b94aSDavid Gibson if (ptex == -1) { 628fcf5ef2aSThomas Huth /* Secondary PTEG lookup */ 629fcf5ef2aSThomas Huth ptem |= HPTE64_V_SECONDARY; 630fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 631fcf5ef2aSThomas Huth "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx 632fcf5ef2aSThomas Huth " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx 63336778660SDavid Gibson " hash=" TARGET_FMT_plx "\n", ppc_hash64_hpt_base(cpu), 63436778660SDavid Gibson ppc_hash64_hpt_mask(cpu), vsid, ptem, ~hash); 635fcf5ef2aSThomas Huth 6367222b94aSDavid Gibson ptex = ppc_hash64_pteg_search(cpu, ~hash, sps, ptem, pte, pshift); 637fcf5ef2aSThomas Huth } 638fcf5ef2aSThomas Huth 6397222b94aSDavid Gibson return ptex; 640fcf5ef2aSThomas Huth } 641fcf5ef2aSThomas Huth 642fcf5ef2aSThomas Huth unsigned ppc_hash64_hpte_page_shift_noslb(PowerPCCPU *cpu, 643fcf5ef2aSThomas Huth uint64_t pte0, uint64_t pte1) 644fcf5ef2aSThomas Huth { 645fcf5ef2aSThomas Huth int i; 646fcf5ef2aSThomas Huth 647fcf5ef2aSThomas Huth if (!(pte0 & HPTE64_V_LARGE)) { 648fcf5ef2aSThomas Huth return 12; 649fcf5ef2aSThomas Huth } 650fcf5ef2aSThomas Huth 651fcf5ef2aSThomas Huth /* 652fcf5ef2aSThomas Huth * The encodings in env->sps need to be carefully chosen so that 653fcf5ef2aSThomas Huth * this gives an unambiguous result. 654fcf5ef2aSThomas Huth */ 655fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 656b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i]; 657fcf5ef2aSThomas Huth unsigned shift; 658fcf5ef2aSThomas Huth 659fcf5ef2aSThomas Huth if (!sps->page_shift) { 660fcf5ef2aSThomas Huth break; 661fcf5ef2aSThomas Huth } 662fcf5ef2aSThomas Huth 663fcf5ef2aSThomas Huth shift = hpte_page_shift(sps, pte0, pte1); 664fcf5ef2aSThomas Huth if (shift) { 665fcf5ef2aSThomas Huth return shift; 666fcf5ef2aSThomas Huth } 667fcf5ef2aSThomas Huth } 668fcf5ef2aSThomas Huth 669fcf5ef2aSThomas Huth return 0; 670fcf5ef2aSThomas Huth } 671fcf5ef2aSThomas Huth 6721b99e029SDavid Gibson static bool ppc_hash64_use_vrma(CPUPPCState *env) 6731b99e029SDavid Gibson { 6741b99e029SDavid Gibson switch (env->mmu_model) { 6751b99e029SDavid Gibson case POWERPC_MMU_3_00: 6761b99e029SDavid Gibson /* 6771b99e029SDavid Gibson * ISAv3.0 (POWER9) always uses VRMA, the VPM0 field and RMOR 6781b99e029SDavid Gibson * register no longer exist 6791b99e029SDavid Gibson */ 6801b99e029SDavid Gibson return true; 6811b99e029SDavid Gibson 6821b99e029SDavid Gibson default: 6831b99e029SDavid Gibson return !!(env->spr[SPR_LPCR] & LPCR_VPM0); 6841b99e029SDavid Gibson } 6851b99e029SDavid Gibson } 6861b99e029SDavid Gibson 6878fe08facSDavid Gibson static void ppc_hash64_set_isi(CPUState *cs, uint64_t error_code) 688fcf5ef2aSThomas Huth { 6898fe08facSDavid Gibson CPUPPCState *env = &POWERPC_CPU(cs)->env; 690fcf5ef2aSThomas Huth bool vpm; 691fcf5ef2aSThomas Huth 692fcf5ef2aSThomas Huth if (msr_ir) { 693fcf5ef2aSThomas Huth vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1); 694fcf5ef2aSThomas Huth } else { 6951b99e029SDavid Gibson vpm = ppc_hash64_use_vrma(env); 696fcf5ef2aSThomas Huth } 697fcf5ef2aSThomas Huth if (vpm && !msr_hv) { 698fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_HISI; 699fcf5ef2aSThomas Huth } else { 700fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_ISI; 701fcf5ef2aSThomas Huth } 702fcf5ef2aSThomas Huth env->error_code = error_code; 703fcf5ef2aSThomas Huth } 704fcf5ef2aSThomas Huth 7058fe08facSDavid Gibson static void ppc_hash64_set_dsi(CPUState *cs, uint64_t dar, uint64_t dsisr) 706fcf5ef2aSThomas Huth { 7078fe08facSDavid Gibson CPUPPCState *env = &POWERPC_CPU(cs)->env; 708fcf5ef2aSThomas Huth bool vpm; 709fcf5ef2aSThomas Huth 710fcf5ef2aSThomas Huth if (msr_dr) { 711fcf5ef2aSThomas Huth vpm = !!(env->spr[SPR_LPCR] & LPCR_VPM1); 712fcf5ef2aSThomas Huth } else { 7131b99e029SDavid Gibson vpm = ppc_hash64_use_vrma(env); 714fcf5ef2aSThomas Huth } 715fcf5ef2aSThomas Huth if (vpm && !msr_hv) { 716fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_HDSI; 717fcf5ef2aSThomas Huth env->spr[SPR_HDAR] = dar; 718fcf5ef2aSThomas Huth env->spr[SPR_HDSISR] = dsisr; 719fcf5ef2aSThomas Huth } else { 720fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_DSI; 721fcf5ef2aSThomas Huth env->spr[SPR_DAR] = dar; 722fcf5ef2aSThomas Huth env->spr[SPR_DSISR] = dsisr; 723fcf5ef2aSThomas Huth } 724fcf5ef2aSThomas Huth env->error_code = 0; 725fcf5ef2aSThomas Huth } 726fcf5ef2aSThomas Huth 727fcf5ef2aSThomas Huth 728a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_r(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1) 729a2dd4e83SBenjamin Herrenschmidt { 730a2dd4e83SBenjamin Herrenschmidt hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + 16; 731a2dd4e83SBenjamin Herrenschmidt 732a2dd4e83SBenjamin Herrenschmidt if (cpu->vhyp) { 733a2dd4e83SBenjamin Herrenschmidt PPCVirtualHypervisorClass *vhc = 734a2dd4e83SBenjamin Herrenschmidt PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 735a2dd4e83SBenjamin Herrenschmidt vhc->hpte_set_r(cpu->vhyp, ptex, pte1); 736a2dd4e83SBenjamin Herrenschmidt return; 737a2dd4e83SBenjamin Herrenschmidt } 738a2dd4e83SBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 739a2dd4e83SBenjamin Herrenschmidt 740a2dd4e83SBenjamin Herrenschmidt 741a2dd4e83SBenjamin Herrenschmidt /* The HW performs a non-atomic byte update */ 742a2dd4e83SBenjamin Herrenschmidt stb_phys(CPU(cpu)->as, base + offset, ((pte1 >> 8) & 0xff) | 0x01); 743a2dd4e83SBenjamin Herrenschmidt } 744a2dd4e83SBenjamin Herrenschmidt 745a2dd4e83SBenjamin Herrenschmidt static void ppc_hash64_set_c(PowerPCCPU *cpu, hwaddr ptex, uint64_t pte1) 746a2dd4e83SBenjamin Herrenschmidt { 747a2dd4e83SBenjamin Herrenschmidt hwaddr base, offset = ptex * HASH_PTE_SIZE_64 + 15; 748a2dd4e83SBenjamin Herrenschmidt 749a2dd4e83SBenjamin Herrenschmidt if (cpu->vhyp) { 750a2dd4e83SBenjamin Herrenschmidt PPCVirtualHypervisorClass *vhc = 751a2dd4e83SBenjamin Herrenschmidt PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 752a2dd4e83SBenjamin Herrenschmidt vhc->hpte_set_c(cpu->vhyp, ptex, pte1); 753a2dd4e83SBenjamin Herrenschmidt return; 754a2dd4e83SBenjamin Herrenschmidt } 755a2dd4e83SBenjamin Herrenschmidt base = ppc_hash64_hpt_base(cpu); 756a2dd4e83SBenjamin Herrenschmidt 757a2dd4e83SBenjamin Herrenschmidt /* The HW performs a non-atomic byte update */ 758a2dd4e83SBenjamin Herrenschmidt stb_phys(CPU(cpu)->as, base + offset, (pte1 & 0xff) | 0x80); 759a2dd4e83SBenjamin Herrenschmidt } 760a2dd4e83SBenjamin Herrenschmidt 761a864a6b3SDavid Gibson static target_ulong rmls_limit(PowerPCCPU *cpu) 762a864a6b3SDavid Gibson { 763a864a6b3SDavid Gibson CPUPPCState *env = &cpu->env; 764a864a6b3SDavid Gibson /* 765d37b40daSDavid Gibson * In theory the meanings of RMLS values are implementation 766d37b40daSDavid Gibson * dependent. In practice, this seems to have been the set from 767d37b40daSDavid Gibson * POWER4+..POWER8, and RMLS is no longer supported in POWER9. 768a864a6b3SDavid Gibson * 769a864a6b3SDavid Gibson * Unsupported values mean the OS has shot itself in the 770a864a6b3SDavid Gibson * foot. Return a 0-sized RMA in this case, which we expect 771a864a6b3SDavid Gibson * to trigger an immediate DSI or ISI 772a864a6b3SDavid Gibson */ 773a864a6b3SDavid Gibson static const target_ulong rma_sizes[16] = { 774d37b40daSDavid Gibson [0] = 256 * GiB, 775a864a6b3SDavid Gibson [1] = 16 * GiB, 776a864a6b3SDavid Gibson [2] = 1 * GiB, 777a864a6b3SDavid Gibson [3] = 64 * MiB, 778a864a6b3SDavid Gibson [4] = 256 * MiB, 779a864a6b3SDavid Gibson [7] = 128 * MiB, 780a864a6b3SDavid Gibson [8] = 32 * MiB, 781a864a6b3SDavid Gibson }; 782a864a6b3SDavid Gibson target_ulong rmls = (env->spr[SPR_LPCR] & LPCR_RMLS) >> LPCR_RMLS_SHIFT; 783a864a6b3SDavid Gibson 784a864a6b3SDavid Gibson return rma_sizes[rmls]; 785a864a6b3SDavid Gibson } 786a864a6b3SDavid Gibson 787*4c24a87fSDavid Gibson static int build_vrma_slbe(PowerPCCPU *cpu, ppc_slb_t *slb) 788*4c24a87fSDavid Gibson { 789*4c24a87fSDavid Gibson CPUPPCState *env = &cpu->env; 790*4c24a87fSDavid Gibson target_ulong lpcr = env->spr[SPR_LPCR]; 791*4c24a87fSDavid Gibson uint32_t vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT; 792*4c24a87fSDavid Gibson target_ulong vsid = SLB_VSID_VRMA | ((vrmasd << 4) & SLB_VSID_LLP_MASK); 793*4c24a87fSDavid Gibson int i; 794*4c24a87fSDavid Gibson 795*4c24a87fSDavid Gibson for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 796*4c24a87fSDavid Gibson const PPCHash64SegmentPageSizes *sps = &cpu->hash64_opts->sps[i]; 797*4c24a87fSDavid Gibson 798*4c24a87fSDavid Gibson if (!sps->page_shift) { 799*4c24a87fSDavid Gibson break; 800*4c24a87fSDavid Gibson } 801*4c24a87fSDavid Gibson 802*4c24a87fSDavid Gibson if ((vsid & SLB_VSID_LLP_MASK) == sps->slb_enc) { 803*4c24a87fSDavid Gibson slb->esid = SLB_ESID_V; 804*4c24a87fSDavid Gibson slb->vsid = vsid; 805*4c24a87fSDavid Gibson slb->sps = sps; 806*4c24a87fSDavid Gibson return 0; 807*4c24a87fSDavid Gibson } 808*4c24a87fSDavid Gibson } 809*4c24a87fSDavid Gibson 810*4c24a87fSDavid Gibson error_report("Bad page size encoding in LPCR[VRMASD]; LPCR=0x" 811*4c24a87fSDavid Gibson TARGET_FMT_lx"\n", lpcr); 812*4c24a87fSDavid Gibson 813*4c24a87fSDavid Gibson return -1; 814*4c24a87fSDavid Gibson } 815*4c24a87fSDavid Gibson 816fcf5ef2aSThomas Huth int ppc_hash64_handle_mmu_fault(PowerPCCPU *cpu, vaddr eaddr, 817fcf5ef2aSThomas Huth int rwx, int mmu_idx) 818fcf5ef2aSThomas Huth { 819fcf5ef2aSThomas Huth CPUState *cs = CPU(cpu); 820fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 821*4c24a87fSDavid Gibson ppc_slb_t vrma_slbe; 822fcf5ef2aSThomas Huth ppc_slb_t *slb; 823fcf5ef2aSThomas Huth unsigned apshift; 8247222b94aSDavid Gibson hwaddr ptex; 825fcf5ef2aSThomas Huth ppc_hash_pte64_t pte; 82607a68f99SSuraj Jitindar Singh int exec_prot, pp_prot, amr_prot, prot; 827fcf5ef2aSThomas Huth const int need_prot[] = {PAGE_READ, PAGE_WRITE, PAGE_EXEC}; 828fcf5ef2aSThomas Huth hwaddr raddr; 829fcf5ef2aSThomas Huth 830fcf5ef2aSThomas Huth assert((rwx == 0) || (rwx == 1) || (rwx == 2)); 831fcf5ef2aSThomas Huth 832d75cbae8SDavid Gibson /* 833d75cbae8SDavid Gibson * Note on LPCR usage: 970 uses HID4, but our special variant of 834d75cbae8SDavid Gibson * store_spr copies relevant fields into env->spr[SPR_LPCR]. 835d75cbae8SDavid Gibson * Similarily we filter unimplemented bits when storing into LPCR 836d75cbae8SDavid Gibson * depending on the MMU version. This code can thus just use the 837d75cbae8SDavid Gibson * LPCR "as-is". 838fcf5ef2aSThomas Huth */ 839fcf5ef2aSThomas Huth 840fcf5ef2aSThomas Huth /* 1. Handle real mode accesses */ 841fcf5ef2aSThomas Huth if (((rwx == 2) && (msr_ir == 0)) || ((rwx != 2) && (msr_dr == 0))) { 842d75cbae8SDavid Gibson /* 843d75cbae8SDavid Gibson * Translation is supposedly "off", but in real mode the top 4 844d75cbae8SDavid Gibson * effective address bits are (mostly) ignored 845d75cbae8SDavid Gibson */ 846fcf5ef2aSThomas Huth raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL; 847fcf5ef2aSThomas Huth 848682c1dfbSDavid Gibson if (cpu->vhyp) { 849682c1dfbSDavid Gibson /* 850682c1dfbSDavid Gibson * In virtual hypervisor mode, there's nothing to do: 851682c1dfbSDavid Gibson * EA == GPA == qemu guest address 852682c1dfbSDavid Gibson */ 853682c1dfbSDavid Gibson } else if (msr_hv || !env->has_hv_mode) { 854fcf5ef2aSThomas Huth /* In HV mode, add HRMOR if top EA bit is clear */ 855fcf5ef2aSThomas Huth if (!(eaddr >> 63)) { 856fcf5ef2aSThomas Huth raddr |= env->spr[SPR_HRMOR]; 857fcf5ef2aSThomas Huth } 8581b99e029SDavid Gibson } else if (ppc_hash64_use_vrma(env)) { 859682c1dfbSDavid Gibson /* Emulated VRMA mode */ 860*4c24a87fSDavid Gibson slb = &vrma_slbe; 861*4c24a87fSDavid Gibson if (build_vrma_slbe(cpu, slb) != 0) { 862682c1dfbSDavid Gibson /* Invalid VRMA setup, machine check */ 863fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_MCHECK; 864fcf5ef2aSThomas Huth env->error_code = 0; 865fcf5ef2aSThomas Huth return 1; 866682c1dfbSDavid Gibson } 867682c1dfbSDavid Gibson 868682c1dfbSDavid Gibson goto skip_slb_search; 869fcf5ef2aSThomas Huth } else { 8703a56a55cSDavid Gibson target_ulong limit = rmls_limit(cpu); 8713a56a55cSDavid Gibson 872682c1dfbSDavid Gibson /* Emulated old-style RMO mode, bounds check against RMLS */ 8733a56a55cSDavid Gibson if (raddr >= limit) { 874fcf5ef2aSThomas Huth if (rwx == 2) { 8758fe08facSDavid Gibson ppc_hash64_set_isi(cs, SRR1_PROTFAULT); 876fcf5ef2aSThomas Huth } else { 877da82c73aSSuraj Jitindar Singh int dsisr = DSISR_PROTFAULT; 878fcf5ef2aSThomas Huth if (rwx == 1) { 879da82c73aSSuraj Jitindar Singh dsisr |= DSISR_ISSTORE; 880fcf5ef2aSThomas Huth } 8818fe08facSDavid Gibson ppc_hash64_set_dsi(cs, eaddr, dsisr); 882fcf5ef2aSThomas Huth } 883fcf5ef2aSThomas Huth return 1; 884fcf5ef2aSThomas Huth } 885682c1dfbSDavid Gibson 886682c1dfbSDavid Gibson raddr |= env->spr[SPR_RMOR]; 887fcf5ef2aSThomas Huth } 888fcf5ef2aSThomas Huth tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK, 889fcf5ef2aSThomas Huth PAGE_READ | PAGE_WRITE | PAGE_EXEC, mmu_idx, 890fcf5ef2aSThomas Huth TARGET_PAGE_SIZE); 891fcf5ef2aSThomas Huth return 0; 892fcf5ef2aSThomas Huth } 893fcf5ef2aSThomas Huth 894fcf5ef2aSThomas Huth /* 2. Translation is on, so look up the SLB */ 895fcf5ef2aSThomas Huth slb = slb_lookup(cpu, eaddr); 896fcf5ef2aSThomas Huth if (!slb) { 897b2899495SSuraj Jitindar Singh /* No entry found, check if in-memory segment tables are in use */ 898ca79b3b7SDavid Gibson if (ppc64_use_proc_tbl(cpu)) { 899b2899495SSuraj Jitindar Singh /* TODO - Unsupported */ 900b2899495SSuraj Jitindar Singh error_report("Segment Table Support Unimplemented"); 901b2899495SSuraj Jitindar Singh exit(1); 902b2899495SSuraj Jitindar Singh } 903b2899495SSuraj Jitindar Singh /* Segment still not found, generate the appropriate interrupt */ 904fcf5ef2aSThomas Huth if (rwx == 2) { 905fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_ISEG; 906fcf5ef2aSThomas Huth env->error_code = 0; 907fcf5ef2aSThomas Huth } else { 908fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_DSEG; 909fcf5ef2aSThomas Huth env->error_code = 0; 910fcf5ef2aSThomas Huth env->spr[SPR_DAR] = eaddr; 911fcf5ef2aSThomas Huth } 912fcf5ef2aSThomas Huth return 1; 913fcf5ef2aSThomas Huth } 914fcf5ef2aSThomas Huth 915fcf5ef2aSThomas Huth skip_slb_search: 916fcf5ef2aSThomas Huth 917fcf5ef2aSThomas Huth /* 3. Check for segment level no-execute violation */ 918fcf5ef2aSThomas Huth if ((rwx == 2) && (slb->vsid & SLB_VSID_N)) { 9198fe08facSDavid Gibson ppc_hash64_set_isi(cs, SRR1_NOEXEC_GUARD); 920fcf5ef2aSThomas Huth return 1; 921fcf5ef2aSThomas Huth } 922fcf5ef2aSThomas Huth 923fcf5ef2aSThomas Huth /* 4. Locate the PTE in the hash table */ 9247222b94aSDavid Gibson ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift); 9257222b94aSDavid Gibson if (ptex == -1) { 926fcf5ef2aSThomas Huth if (rwx == 2) { 9278fe08facSDavid Gibson ppc_hash64_set_isi(cs, SRR1_NOPTE); 928fcf5ef2aSThomas Huth } else { 929da82c73aSSuraj Jitindar Singh int dsisr = DSISR_NOPTE; 930fcf5ef2aSThomas Huth if (rwx == 1) { 931da82c73aSSuraj Jitindar Singh dsisr |= DSISR_ISSTORE; 932fcf5ef2aSThomas Huth } 9338fe08facSDavid Gibson ppc_hash64_set_dsi(cs, eaddr, dsisr); 934fcf5ef2aSThomas Huth } 935fcf5ef2aSThomas Huth return 1; 936fcf5ef2aSThomas Huth } 937fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 9387222b94aSDavid Gibson "found PTE at index %08" HWADDR_PRIx "\n", ptex); 939fcf5ef2aSThomas Huth 940fcf5ef2aSThomas Huth /* 5. Check access permissions */ 941fcf5ef2aSThomas Huth 94207a68f99SSuraj Jitindar Singh exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte); 943fcf5ef2aSThomas Huth pp_prot = ppc_hash64_pte_prot(cpu, slb, pte); 944fcf5ef2aSThomas Huth amr_prot = ppc_hash64_amr_prot(cpu, pte); 94507a68f99SSuraj Jitindar Singh prot = exec_prot & pp_prot & amr_prot; 946fcf5ef2aSThomas Huth 947fcf5ef2aSThomas Huth if ((need_prot[rwx] & ~prot) != 0) { 948fcf5ef2aSThomas Huth /* Access right violation */ 949fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n"); 950fcf5ef2aSThomas Huth if (rwx == 2) { 951a6152b52SSuraj Jitindar Singh int srr1 = 0; 95207a68f99SSuraj Jitindar Singh if (PAGE_EXEC & ~exec_prot) { 95307a68f99SSuraj Jitindar Singh srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */ 95407a68f99SSuraj Jitindar Singh } else if (PAGE_EXEC & ~pp_prot) { 955a6152b52SSuraj Jitindar Singh srr1 |= SRR1_PROTFAULT; /* Access violates access authority */ 956a6152b52SSuraj Jitindar Singh } 957a6152b52SSuraj Jitindar Singh if (PAGE_EXEC & ~amr_prot) { 958a6152b52SSuraj Jitindar Singh srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */ 959a6152b52SSuraj Jitindar Singh } 9608fe08facSDavid Gibson ppc_hash64_set_isi(cs, srr1); 961fcf5ef2aSThomas Huth } else { 962da82c73aSSuraj Jitindar Singh int dsisr = 0; 963fcf5ef2aSThomas Huth if (need_prot[rwx] & ~pp_prot) { 964da82c73aSSuraj Jitindar Singh dsisr |= DSISR_PROTFAULT; 965fcf5ef2aSThomas Huth } 966fcf5ef2aSThomas Huth if (rwx == 1) { 967da82c73aSSuraj Jitindar Singh dsisr |= DSISR_ISSTORE; 968fcf5ef2aSThomas Huth } 969fcf5ef2aSThomas Huth if (need_prot[rwx] & ~amr_prot) { 970da82c73aSSuraj Jitindar Singh dsisr |= DSISR_AMR; 971fcf5ef2aSThomas Huth } 9728fe08facSDavid Gibson ppc_hash64_set_dsi(cs, eaddr, dsisr); 973fcf5ef2aSThomas Huth } 974fcf5ef2aSThomas Huth return 1; 975fcf5ef2aSThomas Huth } 976fcf5ef2aSThomas Huth 977fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n"); 978fcf5ef2aSThomas Huth 979fcf5ef2aSThomas Huth /* 6. Update PTE referenced and changed bits if necessary */ 980fcf5ef2aSThomas Huth 981a2dd4e83SBenjamin Herrenschmidt if (!(pte.pte1 & HPTE64_R_R)) { 982a2dd4e83SBenjamin Herrenschmidt ppc_hash64_set_r(cpu, ptex, pte.pte1); 983a2dd4e83SBenjamin Herrenschmidt } 984a2dd4e83SBenjamin Herrenschmidt if (!(pte.pte1 & HPTE64_R_C)) { 985fcf5ef2aSThomas Huth if (rwx == 1) { 986a2dd4e83SBenjamin Herrenschmidt ppc_hash64_set_c(cpu, ptex, pte.pte1); 987fcf5ef2aSThomas Huth } else { 988d75cbae8SDavid Gibson /* 989d75cbae8SDavid Gibson * Treat the page as read-only for now, so that a later write 990d75cbae8SDavid Gibson * will pass through this function again to set the C bit 991d75cbae8SDavid Gibson */ 992fcf5ef2aSThomas Huth prot &= ~PAGE_WRITE; 993fcf5ef2aSThomas Huth } 994fcf5ef2aSThomas Huth } 995fcf5ef2aSThomas Huth 996fcf5ef2aSThomas Huth /* 7. Determine the real address from the PTE */ 997fcf5ef2aSThomas Huth 998fcf5ef2aSThomas Huth raddr = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr); 999fcf5ef2aSThomas Huth 1000fcf5ef2aSThomas Huth tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK, 1001fcf5ef2aSThomas Huth prot, mmu_idx, 1ULL << apshift); 1002fcf5ef2aSThomas Huth 1003fcf5ef2aSThomas Huth return 0; 1004fcf5ef2aSThomas Huth } 1005fcf5ef2aSThomas Huth 1006fcf5ef2aSThomas Huth hwaddr ppc_hash64_get_phys_page_debug(PowerPCCPU *cpu, target_ulong addr) 1007fcf5ef2aSThomas Huth { 1008fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 1009*4c24a87fSDavid Gibson ppc_slb_t vrma_slbe; 1010fcf5ef2aSThomas Huth ppc_slb_t *slb; 10117222b94aSDavid Gibson hwaddr ptex, raddr; 1012fcf5ef2aSThomas Huth ppc_hash_pte64_t pte; 1013fcf5ef2aSThomas Huth unsigned apshift; 1014fcf5ef2aSThomas Huth 1015fcf5ef2aSThomas Huth /* Handle real mode */ 1016fcf5ef2aSThomas Huth if (msr_dr == 0) { 1017fcf5ef2aSThomas Huth /* In real mode the top 4 effective address bits are ignored */ 1018fcf5ef2aSThomas Huth raddr = addr & 0x0FFFFFFFFFFFFFFFULL; 1019fcf5ef2aSThomas Huth 1020682c1dfbSDavid Gibson if (cpu->vhyp) { 1021682c1dfbSDavid Gibson /* 1022682c1dfbSDavid Gibson * In virtual hypervisor mode, there's nothing to do: 1023682c1dfbSDavid Gibson * EA == GPA == qemu guest address 1024682c1dfbSDavid Gibson */ 1025682c1dfbSDavid Gibson return raddr; 1026682c1dfbSDavid Gibson } else if ((msr_hv || !env->has_hv_mode) && !(addr >> 63)) { 1027fcf5ef2aSThomas Huth /* In HV mode, add HRMOR if top EA bit is clear */ 1028fcf5ef2aSThomas Huth return raddr | env->spr[SPR_HRMOR]; 10291b99e029SDavid Gibson } else if (ppc_hash64_use_vrma(env)) { 1030682c1dfbSDavid Gibson /* Emulated VRMA mode */ 1031*4c24a87fSDavid Gibson slb = &vrma_slbe; 1032*4c24a87fSDavid Gibson if (build_vrma_slbe(cpu, slb) != 0) { 1033fcf5ef2aSThomas Huth return -1; 1034fcf5ef2aSThomas Huth } 1035fcf5ef2aSThomas Huth } else { 10363a56a55cSDavid Gibson target_ulong limit = rmls_limit(cpu); 10373a56a55cSDavid Gibson 1038682c1dfbSDavid Gibson /* Emulated old-style RMO mode, bounds check against RMLS */ 10393a56a55cSDavid Gibson if (raddr >= limit) { 1040fcf5ef2aSThomas Huth return -1; 1041fcf5ef2aSThomas Huth } 1042682c1dfbSDavid Gibson return raddr | env->spr[SPR_RMOR]; 1043682c1dfbSDavid Gibson } 1044fcf5ef2aSThomas Huth } else { 1045fcf5ef2aSThomas Huth slb = slb_lookup(cpu, addr); 1046fcf5ef2aSThomas Huth if (!slb) { 1047fcf5ef2aSThomas Huth return -1; 1048fcf5ef2aSThomas Huth } 1049fcf5ef2aSThomas Huth } 1050fcf5ef2aSThomas Huth 10517222b94aSDavid Gibson ptex = ppc_hash64_htab_lookup(cpu, slb, addr, &pte, &apshift); 10527222b94aSDavid Gibson if (ptex == -1) { 1053fcf5ef2aSThomas Huth return -1; 1054fcf5ef2aSThomas Huth } 1055fcf5ef2aSThomas Huth 1056fcf5ef2aSThomas Huth return deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, addr) 1057fcf5ef2aSThomas Huth & TARGET_PAGE_MASK; 1058fcf5ef2aSThomas Huth } 1059fcf5ef2aSThomas Huth 10607222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex, 1061fcf5ef2aSThomas Huth target_ulong pte0, target_ulong pte1) 1062fcf5ef2aSThomas Huth { 1063fcf5ef2aSThomas Huth /* 1064fcf5ef2aSThomas Huth * XXX: given the fact that there are too many segments to 1065fcf5ef2aSThomas Huth * invalidate, and we still don't have a tlb_flush_mask(env, n, 1066fcf5ef2aSThomas Huth * mask) in QEMU, we just invalidate all TLBs 1067fcf5ef2aSThomas Huth */ 1068fcf5ef2aSThomas Huth cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH; 1069fcf5ef2aSThomas Huth } 1070fcf5ef2aSThomas Huth 10715ad55315SDavid Gibson void ppc_store_lpcr(PowerPCCPU *cpu, target_ulong val) 1072fcf5ef2aSThomas Huth { 1073e232ecccSDavid Gibson PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 10745ad55315SDavid Gibson CPUPPCState *env = &cpu->env; 1075fcf5ef2aSThomas Huth 1076e232ecccSDavid Gibson env->spr[SPR_LPCR] = val & pcc->lpcr_mask; 1077fcf5ef2aSThomas Huth } 1078a059471dSDavid Gibson 10795ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val) 10805ad55315SDavid Gibson { 1081db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 10825ad55315SDavid Gibson 10835ad55315SDavid Gibson ppc_store_lpcr(cpu, val); 10845ad55315SDavid Gibson } 10855ad55315SDavid Gibson 1086a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu) 1087a059471dSDavid Gibson { 1088a059471dSDavid Gibson CPUPPCState *env = &cpu->env; 1089a059471dSDavid Gibson PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 1090a059471dSDavid Gibson 109121e405f1SDavid Gibson if (!pcc->hash64_opts) { 109221e405f1SDavid Gibson assert(!(env->mmu_model & POWERPC_MMU_64)); 109321e405f1SDavid Gibson return; 109421e405f1SDavid Gibson } 109521e405f1SDavid Gibson 109621e405f1SDavid Gibson cpu->hash64_opts = g_memdup(pcc->hash64_opts, sizeof(*cpu->hash64_opts)); 109721e405f1SDavid Gibson } 109821e405f1SDavid Gibson 109921e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu) 110021e405f1SDavid Gibson { 110121e405f1SDavid Gibson g_free(cpu->hash64_opts); 110221e405f1SDavid Gibson } 110321e405f1SDavid Gibson 110421e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = { 110558969eeeSDavid Gibson .flags = 0, 110667d7d66fSDavid Gibson .slb_size = 64, 1107a059471dSDavid Gibson .sps = { 1108a059471dSDavid Gibson { .page_shift = 12, /* 4K */ 1109a059471dSDavid Gibson .slb_enc = 0, 1110a059471dSDavid Gibson .enc = { { .page_shift = 12, .pte_enc = 0 } } 1111a059471dSDavid Gibson }, 1112a059471dSDavid Gibson { .page_shift = 24, /* 16M */ 1113a059471dSDavid Gibson .slb_enc = 0x100, 1114a059471dSDavid Gibson .enc = { { .page_shift = 24, .pte_enc = 0 } } 1115a059471dSDavid Gibson }, 1116a059471dSDavid Gibson }, 1117a059471dSDavid Gibson }; 1118b07c59f7SDavid Gibson 1119b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = { 112026cd35b8SDavid Gibson .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE, 112167d7d66fSDavid Gibson .slb_size = 32, 1122b07c59f7SDavid Gibson .sps = { 1123b07c59f7SDavid Gibson { 1124b07c59f7SDavid Gibson .page_shift = 12, /* 4K */ 1125b07c59f7SDavid Gibson .slb_enc = 0, 1126b07c59f7SDavid Gibson .enc = { { .page_shift = 12, .pte_enc = 0 }, 1127b07c59f7SDavid Gibson { .page_shift = 16, .pte_enc = 0x7 }, 1128b07c59f7SDavid Gibson { .page_shift = 24, .pte_enc = 0x38 }, }, 1129b07c59f7SDavid Gibson }, 1130b07c59f7SDavid Gibson { 1131b07c59f7SDavid Gibson .page_shift = 16, /* 64K */ 1132b07c59f7SDavid Gibson .slb_enc = SLB_VSID_64K, 1133b07c59f7SDavid Gibson .enc = { { .page_shift = 16, .pte_enc = 0x1 }, 1134b07c59f7SDavid Gibson { .page_shift = 24, .pte_enc = 0x8 }, }, 1135b07c59f7SDavid Gibson }, 1136b07c59f7SDavid Gibson { 1137b07c59f7SDavid Gibson .page_shift = 24, /* 16M */ 1138b07c59f7SDavid Gibson .slb_enc = SLB_VSID_16M, 1139b07c59f7SDavid Gibson .enc = { { .page_shift = 24, .pte_enc = 0 }, }, 1140b07c59f7SDavid Gibson }, 1141b07c59f7SDavid Gibson { 1142b07c59f7SDavid Gibson .page_shift = 34, /* 16G */ 1143b07c59f7SDavid Gibson .slb_enc = SLB_VSID_16G, 1144b07c59f7SDavid Gibson .enc = { { .page_shift = 34, .pte_enc = 0x3 }, }, 1145b07c59f7SDavid Gibson }, 1146b07c59f7SDavid Gibson } 1147b07c59f7SDavid Gibson }; 114827f00f0aSDavid Gibson 114927f00f0aSDavid Gibson void ppc_hash64_filter_pagesizes(PowerPCCPU *cpu, 115027f00f0aSDavid Gibson bool (*cb)(void *, uint32_t, uint32_t), 115127f00f0aSDavid Gibson void *opaque) 115227f00f0aSDavid Gibson { 115327f00f0aSDavid Gibson PPCHash64Options *opts = cpu->hash64_opts; 115427f00f0aSDavid Gibson int i; 115527f00f0aSDavid Gibson int n = 0; 115627f00f0aSDavid Gibson bool ci_largepage = false; 115727f00f0aSDavid Gibson 115827f00f0aSDavid Gibson assert(opts); 115927f00f0aSDavid Gibson 116027f00f0aSDavid Gibson n = 0; 116127f00f0aSDavid Gibson for (i = 0; i < ARRAY_SIZE(opts->sps); i++) { 116227f00f0aSDavid Gibson PPCHash64SegmentPageSizes *sps = &opts->sps[i]; 116327f00f0aSDavid Gibson int j; 116427f00f0aSDavid Gibson int m = 0; 116527f00f0aSDavid Gibson 116627f00f0aSDavid Gibson assert(n <= i); 116727f00f0aSDavid Gibson 116827f00f0aSDavid Gibson if (!sps->page_shift) { 116927f00f0aSDavid Gibson break; 117027f00f0aSDavid Gibson } 117127f00f0aSDavid Gibson 117227f00f0aSDavid Gibson for (j = 0; j < ARRAY_SIZE(sps->enc); j++) { 117327f00f0aSDavid Gibson PPCHash64PageSize *ps = &sps->enc[j]; 117427f00f0aSDavid Gibson 117527f00f0aSDavid Gibson assert(m <= j); 117627f00f0aSDavid Gibson if (!ps->page_shift) { 117727f00f0aSDavid Gibson break; 117827f00f0aSDavid Gibson } 117927f00f0aSDavid Gibson 118027f00f0aSDavid Gibson if (cb(opaque, sps->page_shift, ps->page_shift)) { 118127f00f0aSDavid Gibson if (ps->page_shift >= 16) { 118227f00f0aSDavid Gibson ci_largepage = true; 118327f00f0aSDavid Gibson } 118427f00f0aSDavid Gibson sps->enc[m++] = *ps; 118527f00f0aSDavid Gibson } 118627f00f0aSDavid Gibson } 118727f00f0aSDavid Gibson 118827f00f0aSDavid Gibson /* Clear rest of the row */ 118927f00f0aSDavid Gibson for (j = m; j < ARRAY_SIZE(sps->enc); j++) { 119027f00f0aSDavid Gibson memset(&sps->enc[j], 0, sizeof(sps->enc[j])); 119127f00f0aSDavid Gibson } 119227f00f0aSDavid Gibson 119327f00f0aSDavid Gibson if (m) { 119427f00f0aSDavid Gibson n++; 119527f00f0aSDavid Gibson } 119627f00f0aSDavid Gibson } 119727f00f0aSDavid Gibson 119827f00f0aSDavid Gibson /* Clear the rest of the table */ 119927f00f0aSDavid Gibson for (i = n; i < ARRAY_SIZE(opts->sps); i++) { 120027f00f0aSDavid Gibson memset(&opts->sps[i], 0, sizeof(opts->sps[i])); 120127f00f0aSDavid Gibson } 120227f00f0aSDavid Gibson 120327f00f0aSDavid Gibson if (!ci_largepage) { 120427f00f0aSDavid Gibson opts->flags &= ~PPC_HASH64_CI_LARGEPAGE; 120527f00f0aSDavid Gibson } 120627f00f0aSDavid Gibson } 1207