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 /* 765*d37b40daSDavid Gibson * In theory the meanings of RMLS values are implementation 766*d37b40daSDavid Gibson * dependent. In practice, this seems to have been the set from 767*d37b40daSDavid 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] = { 774*d37b40daSDavid 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 787fcf5ef2aSThomas Huth int ppc_hash64_handle_mmu_fault(PowerPCCPU *cpu, vaddr eaddr, 788fcf5ef2aSThomas Huth int rwx, int mmu_idx) 789fcf5ef2aSThomas Huth { 790fcf5ef2aSThomas Huth CPUState *cs = CPU(cpu); 791fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 792fcf5ef2aSThomas Huth ppc_slb_t *slb; 793fcf5ef2aSThomas Huth unsigned apshift; 7947222b94aSDavid Gibson hwaddr ptex; 795fcf5ef2aSThomas Huth ppc_hash_pte64_t pte; 79607a68f99SSuraj Jitindar Singh int exec_prot, pp_prot, amr_prot, prot; 797fcf5ef2aSThomas Huth const int need_prot[] = {PAGE_READ, PAGE_WRITE, PAGE_EXEC}; 798fcf5ef2aSThomas Huth hwaddr raddr; 799fcf5ef2aSThomas Huth 800fcf5ef2aSThomas Huth assert((rwx == 0) || (rwx == 1) || (rwx == 2)); 801fcf5ef2aSThomas Huth 802d75cbae8SDavid Gibson /* 803d75cbae8SDavid Gibson * Note on LPCR usage: 970 uses HID4, but our special variant of 804d75cbae8SDavid Gibson * store_spr copies relevant fields into env->spr[SPR_LPCR]. 805d75cbae8SDavid Gibson * Similarily we filter unimplemented bits when storing into LPCR 806d75cbae8SDavid Gibson * depending on the MMU version. This code can thus just use the 807d75cbae8SDavid Gibson * LPCR "as-is". 808fcf5ef2aSThomas Huth */ 809fcf5ef2aSThomas Huth 810fcf5ef2aSThomas Huth /* 1. Handle real mode accesses */ 811fcf5ef2aSThomas Huth if (((rwx == 2) && (msr_ir == 0)) || ((rwx != 2) && (msr_dr == 0))) { 812d75cbae8SDavid Gibson /* 813d75cbae8SDavid Gibson * Translation is supposedly "off", but in real mode the top 4 814d75cbae8SDavid Gibson * effective address bits are (mostly) ignored 815d75cbae8SDavid Gibson */ 816fcf5ef2aSThomas Huth raddr = eaddr & 0x0FFFFFFFFFFFFFFFULL; 817fcf5ef2aSThomas Huth 818682c1dfbSDavid Gibson if (cpu->vhyp) { 819682c1dfbSDavid Gibson /* 820682c1dfbSDavid Gibson * In virtual hypervisor mode, there's nothing to do: 821682c1dfbSDavid Gibson * EA == GPA == qemu guest address 822682c1dfbSDavid Gibson */ 823682c1dfbSDavid Gibson } else if (msr_hv || !env->has_hv_mode) { 824fcf5ef2aSThomas Huth /* In HV mode, add HRMOR if top EA bit is clear */ 825fcf5ef2aSThomas Huth if (!(eaddr >> 63)) { 826fcf5ef2aSThomas Huth raddr |= env->spr[SPR_HRMOR]; 827fcf5ef2aSThomas Huth } 8281b99e029SDavid Gibson } else if (ppc_hash64_use_vrma(env)) { 829682c1dfbSDavid Gibson /* Emulated VRMA mode */ 830fcf5ef2aSThomas Huth slb = &env->vrma_slb; 831682c1dfbSDavid Gibson if (!slb->sps) { 832682c1dfbSDavid Gibson /* Invalid VRMA setup, machine check */ 833fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_MCHECK; 834fcf5ef2aSThomas Huth env->error_code = 0; 835fcf5ef2aSThomas Huth return 1; 836682c1dfbSDavid Gibson } 837682c1dfbSDavid Gibson 838682c1dfbSDavid Gibson goto skip_slb_search; 839fcf5ef2aSThomas Huth } else { 840682c1dfbSDavid Gibson /* Emulated old-style RMO mode, bounds check against RMLS */ 841682c1dfbSDavid Gibson if (raddr >= env->rmls) { 842fcf5ef2aSThomas Huth if (rwx == 2) { 8438fe08facSDavid Gibson ppc_hash64_set_isi(cs, SRR1_PROTFAULT); 844fcf5ef2aSThomas Huth } else { 845da82c73aSSuraj Jitindar Singh int dsisr = DSISR_PROTFAULT; 846fcf5ef2aSThomas Huth if (rwx == 1) { 847da82c73aSSuraj Jitindar Singh dsisr |= DSISR_ISSTORE; 848fcf5ef2aSThomas Huth } 8498fe08facSDavid Gibson ppc_hash64_set_dsi(cs, eaddr, dsisr); 850fcf5ef2aSThomas Huth } 851fcf5ef2aSThomas Huth return 1; 852fcf5ef2aSThomas Huth } 853682c1dfbSDavid Gibson 854682c1dfbSDavid Gibson raddr |= env->spr[SPR_RMOR]; 855fcf5ef2aSThomas Huth } 856fcf5ef2aSThomas Huth tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK, 857fcf5ef2aSThomas Huth PAGE_READ | PAGE_WRITE | PAGE_EXEC, mmu_idx, 858fcf5ef2aSThomas Huth TARGET_PAGE_SIZE); 859fcf5ef2aSThomas Huth return 0; 860fcf5ef2aSThomas Huth } 861fcf5ef2aSThomas Huth 862fcf5ef2aSThomas Huth /* 2. Translation is on, so look up the SLB */ 863fcf5ef2aSThomas Huth slb = slb_lookup(cpu, eaddr); 864fcf5ef2aSThomas Huth if (!slb) { 865b2899495SSuraj Jitindar Singh /* No entry found, check if in-memory segment tables are in use */ 866ca79b3b7SDavid Gibson if (ppc64_use_proc_tbl(cpu)) { 867b2899495SSuraj Jitindar Singh /* TODO - Unsupported */ 868b2899495SSuraj Jitindar Singh error_report("Segment Table Support Unimplemented"); 869b2899495SSuraj Jitindar Singh exit(1); 870b2899495SSuraj Jitindar Singh } 871b2899495SSuraj Jitindar Singh /* Segment still not found, generate the appropriate interrupt */ 872fcf5ef2aSThomas Huth if (rwx == 2) { 873fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_ISEG; 874fcf5ef2aSThomas Huth env->error_code = 0; 875fcf5ef2aSThomas Huth } else { 876fcf5ef2aSThomas Huth cs->exception_index = POWERPC_EXCP_DSEG; 877fcf5ef2aSThomas Huth env->error_code = 0; 878fcf5ef2aSThomas Huth env->spr[SPR_DAR] = eaddr; 879fcf5ef2aSThomas Huth } 880fcf5ef2aSThomas Huth return 1; 881fcf5ef2aSThomas Huth } 882fcf5ef2aSThomas Huth 883fcf5ef2aSThomas Huth skip_slb_search: 884fcf5ef2aSThomas Huth 885fcf5ef2aSThomas Huth /* 3. Check for segment level no-execute violation */ 886fcf5ef2aSThomas Huth if ((rwx == 2) && (slb->vsid & SLB_VSID_N)) { 8878fe08facSDavid Gibson ppc_hash64_set_isi(cs, SRR1_NOEXEC_GUARD); 888fcf5ef2aSThomas Huth return 1; 889fcf5ef2aSThomas Huth } 890fcf5ef2aSThomas Huth 891fcf5ef2aSThomas Huth /* 4. Locate the PTE in the hash table */ 8927222b94aSDavid Gibson ptex = ppc_hash64_htab_lookup(cpu, slb, eaddr, &pte, &apshift); 8937222b94aSDavid Gibson if (ptex == -1) { 894fcf5ef2aSThomas Huth if (rwx == 2) { 8958fe08facSDavid Gibson ppc_hash64_set_isi(cs, SRR1_NOPTE); 896fcf5ef2aSThomas Huth } else { 897da82c73aSSuraj Jitindar Singh int dsisr = DSISR_NOPTE; 898fcf5ef2aSThomas Huth if (rwx == 1) { 899da82c73aSSuraj Jitindar Singh dsisr |= DSISR_ISSTORE; 900fcf5ef2aSThomas Huth } 9018fe08facSDavid Gibson ppc_hash64_set_dsi(cs, eaddr, dsisr); 902fcf5ef2aSThomas Huth } 903fcf5ef2aSThomas Huth return 1; 904fcf5ef2aSThomas Huth } 905fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, 9067222b94aSDavid Gibson "found PTE at index %08" HWADDR_PRIx "\n", ptex); 907fcf5ef2aSThomas Huth 908fcf5ef2aSThomas Huth /* 5. Check access permissions */ 909fcf5ef2aSThomas Huth 91007a68f99SSuraj Jitindar Singh exec_prot = ppc_hash64_pte_noexec_guard(cpu, pte); 911fcf5ef2aSThomas Huth pp_prot = ppc_hash64_pte_prot(cpu, slb, pte); 912fcf5ef2aSThomas Huth amr_prot = ppc_hash64_amr_prot(cpu, pte); 91307a68f99SSuraj Jitindar Singh prot = exec_prot & pp_prot & amr_prot; 914fcf5ef2aSThomas Huth 915fcf5ef2aSThomas Huth if ((need_prot[rwx] & ~prot) != 0) { 916fcf5ef2aSThomas Huth /* Access right violation */ 917fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n"); 918fcf5ef2aSThomas Huth if (rwx == 2) { 919a6152b52SSuraj Jitindar Singh int srr1 = 0; 92007a68f99SSuraj Jitindar Singh if (PAGE_EXEC & ~exec_prot) { 92107a68f99SSuraj Jitindar Singh srr1 |= SRR1_NOEXEC_GUARD; /* Access violates noexec or guard */ 92207a68f99SSuraj Jitindar Singh } else if (PAGE_EXEC & ~pp_prot) { 923a6152b52SSuraj Jitindar Singh srr1 |= SRR1_PROTFAULT; /* Access violates access authority */ 924a6152b52SSuraj Jitindar Singh } 925a6152b52SSuraj Jitindar Singh if (PAGE_EXEC & ~amr_prot) { 926a6152b52SSuraj Jitindar Singh srr1 |= SRR1_IAMR; /* Access violates virt pg class key prot */ 927a6152b52SSuraj Jitindar Singh } 9288fe08facSDavid Gibson ppc_hash64_set_isi(cs, srr1); 929fcf5ef2aSThomas Huth } else { 930da82c73aSSuraj Jitindar Singh int dsisr = 0; 931fcf5ef2aSThomas Huth if (need_prot[rwx] & ~pp_prot) { 932da82c73aSSuraj Jitindar Singh dsisr |= DSISR_PROTFAULT; 933fcf5ef2aSThomas Huth } 934fcf5ef2aSThomas Huth if (rwx == 1) { 935da82c73aSSuraj Jitindar Singh dsisr |= DSISR_ISSTORE; 936fcf5ef2aSThomas Huth } 937fcf5ef2aSThomas Huth if (need_prot[rwx] & ~amr_prot) { 938da82c73aSSuraj Jitindar Singh dsisr |= DSISR_AMR; 939fcf5ef2aSThomas Huth } 9408fe08facSDavid Gibson ppc_hash64_set_dsi(cs, eaddr, dsisr); 941fcf5ef2aSThomas Huth } 942fcf5ef2aSThomas Huth return 1; 943fcf5ef2aSThomas Huth } 944fcf5ef2aSThomas Huth 945fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n"); 946fcf5ef2aSThomas Huth 947fcf5ef2aSThomas Huth /* 6. Update PTE referenced and changed bits if necessary */ 948fcf5ef2aSThomas Huth 949a2dd4e83SBenjamin Herrenschmidt if (!(pte.pte1 & HPTE64_R_R)) { 950a2dd4e83SBenjamin Herrenschmidt ppc_hash64_set_r(cpu, ptex, pte.pte1); 951a2dd4e83SBenjamin Herrenschmidt } 952a2dd4e83SBenjamin Herrenschmidt if (!(pte.pte1 & HPTE64_R_C)) { 953fcf5ef2aSThomas Huth if (rwx == 1) { 954a2dd4e83SBenjamin Herrenschmidt ppc_hash64_set_c(cpu, ptex, pte.pte1); 955fcf5ef2aSThomas Huth } else { 956d75cbae8SDavid Gibson /* 957d75cbae8SDavid Gibson * Treat the page as read-only for now, so that a later write 958d75cbae8SDavid Gibson * will pass through this function again to set the C bit 959d75cbae8SDavid Gibson */ 960fcf5ef2aSThomas Huth prot &= ~PAGE_WRITE; 961fcf5ef2aSThomas Huth } 962fcf5ef2aSThomas Huth } 963fcf5ef2aSThomas Huth 964fcf5ef2aSThomas Huth /* 7. Determine the real address from the PTE */ 965fcf5ef2aSThomas Huth 966fcf5ef2aSThomas Huth raddr = deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, eaddr); 967fcf5ef2aSThomas Huth 968fcf5ef2aSThomas Huth tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK, 969fcf5ef2aSThomas Huth prot, mmu_idx, 1ULL << apshift); 970fcf5ef2aSThomas Huth 971fcf5ef2aSThomas Huth return 0; 972fcf5ef2aSThomas Huth } 973fcf5ef2aSThomas Huth 974fcf5ef2aSThomas Huth hwaddr ppc_hash64_get_phys_page_debug(PowerPCCPU *cpu, target_ulong addr) 975fcf5ef2aSThomas Huth { 976fcf5ef2aSThomas Huth CPUPPCState *env = &cpu->env; 977fcf5ef2aSThomas Huth ppc_slb_t *slb; 9787222b94aSDavid Gibson hwaddr ptex, raddr; 979fcf5ef2aSThomas Huth ppc_hash_pte64_t pte; 980fcf5ef2aSThomas Huth unsigned apshift; 981fcf5ef2aSThomas Huth 982fcf5ef2aSThomas Huth /* Handle real mode */ 983fcf5ef2aSThomas Huth if (msr_dr == 0) { 984fcf5ef2aSThomas Huth /* In real mode the top 4 effective address bits are ignored */ 985fcf5ef2aSThomas Huth raddr = addr & 0x0FFFFFFFFFFFFFFFULL; 986fcf5ef2aSThomas Huth 987682c1dfbSDavid Gibson if (cpu->vhyp) { 988682c1dfbSDavid Gibson /* 989682c1dfbSDavid Gibson * In virtual hypervisor mode, there's nothing to do: 990682c1dfbSDavid Gibson * EA == GPA == qemu guest address 991682c1dfbSDavid Gibson */ 992682c1dfbSDavid Gibson return raddr; 993682c1dfbSDavid Gibson } else if ((msr_hv || !env->has_hv_mode) && !(addr >> 63)) { 994fcf5ef2aSThomas Huth /* In HV mode, add HRMOR if top EA bit is clear */ 995fcf5ef2aSThomas Huth return raddr | env->spr[SPR_HRMOR]; 9961b99e029SDavid Gibson } else if (ppc_hash64_use_vrma(env)) { 997682c1dfbSDavid Gibson /* Emulated VRMA mode */ 998fcf5ef2aSThomas Huth slb = &env->vrma_slb; 999fcf5ef2aSThomas Huth if (!slb->sps) { 1000fcf5ef2aSThomas Huth return -1; 1001fcf5ef2aSThomas Huth } 1002fcf5ef2aSThomas Huth } else { 1003682c1dfbSDavid Gibson /* Emulated old-style RMO mode, bounds check against RMLS */ 1004682c1dfbSDavid Gibson if (raddr >= env->rmls) { 1005fcf5ef2aSThomas Huth return -1; 1006fcf5ef2aSThomas Huth } 1007682c1dfbSDavid Gibson return raddr | env->spr[SPR_RMOR]; 1008682c1dfbSDavid Gibson } 1009fcf5ef2aSThomas Huth } else { 1010fcf5ef2aSThomas Huth slb = slb_lookup(cpu, addr); 1011fcf5ef2aSThomas Huth if (!slb) { 1012fcf5ef2aSThomas Huth return -1; 1013fcf5ef2aSThomas Huth } 1014fcf5ef2aSThomas Huth } 1015fcf5ef2aSThomas Huth 10167222b94aSDavid Gibson ptex = ppc_hash64_htab_lookup(cpu, slb, addr, &pte, &apshift); 10177222b94aSDavid Gibson if (ptex == -1) { 1018fcf5ef2aSThomas Huth return -1; 1019fcf5ef2aSThomas Huth } 1020fcf5ef2aSThomas Huth 1021fcf5ef2aSThomas Huth return deposit64(pte.pte1 & HPTE64_R_RPN, 0, apshift, addr) 1022fcf5ef2aSThomas Huth & TARGET_PAGE_MASK; 1023fcf5ef2aSThomas Huth } 1024fcf5ef2aSThomas Huth 10257222b94aSDavid Gibson void ppc_hash64_tlb_flush_hpte(PowerPCCPU *cpu, target_ulong ptex, 1026fcf5ef2aSThomas Huth target_ulong pte0, target_ulong pte1) 1027fcf5ef2aSThomas Huth { 1028fcf5ef2aSThomas Huth /* 1029fcf5ef2aSThomas Huth * XXX: given the fact that there are too many segments to 1030fcf5ef2aSThomas Huth * invalidate, and we still don't have a tlb_flush_mask(env, n, 1031fcf5ef2aSThomas Huth * mask) in QEMU, we just invalidate all TLBs 1032fcf5ef2aSThomas Huth */ 1033fcf5ef2aSThomas Huth cpu->env.tlb_need_flush = TLB_NEED_GLOBAL_FLUSH | TLB_NEED_LOCAL_FLUSH; 1034fcf5ef2aSThomas Huth } 1035fcf5ef2aSThomas Huth 10365ad55315SDavid Gibson static void ppc_hash64_update_vrma(PowerPCCPU *cpu) 1037fcf5ef2aSThomas Huth { 10388fe08facSDavid Gibson CPUPPCState *env = &cpu->env; 1039b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps = NULL; 1040fcf5ef2aSThomas Huth target_ulong esid, vsid, lpcr; 1041fcf5ef2aSThomas Huth ppc_slb_t *slb = &env->vrma_slb; 1042fcf5ef2aSThomas Huth uint32_t vrmasd; 1043fcf5ef2aSThomas Huth int i; 1044fcf5ef2aSThomas Huth 1045fcf5ef2aSThomas Huth /* First clear it */ 1046fcf5ef2aSThomas Huth slb->esid = slb->vsid = 0; 1047fcf5ef2aSThomas Huth slb->sps = NULL; 1048fcf5ef2aSThomas Huth 1049fcf5ef2aSThomas Huth /* Is VRMA enabled ? */ 10501b99e029SDavid Gibson if (!ppc_hash64_use_vrma(env)) { 1051fcf5ef2aSThomas Huth return; 1052fcf5ef2aSThomas Huth } 1053fcf5ef2aSThomas Huth 1054d75cbae8SDavid Gibson /* 1055d75cbae8SDavid Gibson * Make one up. Mostly ignore the ESID which will not be needed 1056d75cbae8SDavid Gibson * for translation 1057fcf5ef2aSThomas Huth */ 10581b99e029SDavid Gibson lpcr = env->spr[SPR_LPCR]; 1059fcf5ef2aSThomas Huth vsid = SLB_VSID_VRMA; 1060fcf5ef2aSThomas Huth vrmasd = (lpcr & LPCR_VRMASD) >> LPCR_VRMASD_SHIFT; 1061fcf5ef2aSThomas Huth vsid |= (vrmasd << 4) & (SLB_VSID_L | SLB_VSID_LP); 1062fcf5ef2aSThomas Huth esid = SLB_ESID_V; 1063fcf5ef2aSThomas Huth 1064fcf5ef2aSThomas Huth for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) { 1065b07c59f7SDavid Gibson const PPCHash64SegmentPageSizes *sps1 = &cpu->hash64_opts->sps[i]; 1066fcf5ef2aSThomas Huth 1067fcf5ef2aSThomas Huth if (!sps1->page_shift) { 1068fcf5ef2aSThomas Huth break; 1069fcf5ef2aSThomas Huth } 1070fcf5ef2aSThomas Huth 1071fcf5ef2aSThomas Huth if ((vsid & SLB_VSID_LLP_MASK) == sps1->slb_enc) { 1072fcf5ef2aSThomas Huth sps = sps1; 1073fcf5ef2aSThomas Huth break; 1074fcf5ef2aSThomas Huth } 1075fcf5ef2aSThomas Huth } 1076fcf5ef2aSThomas Huth 1077fcf5ef2aSThomas Huth if (!sps) { 1078fcf5ef2aSThomas Huth error_report("Bad page size encoding esid 0x"TARGET_FMT_lx 1079fcf5ef2aSThomas Huth " vsid 0x"TARGET_FMT_lx, esid, vsid); 1080fcf5ef2aSThomas Huth return; 1081fcf5ef2aSThomas Huth } 1082fcf5ef2aSThomas Huth 1083fcf5ef2aSThomas Huth slb->vsid = vsid; 1084fcf5ef2aSThomas Huth slb->esid = esid; 1085fcf5ef2aSThomas Huth slb->sps = sps; 1086fcf5ef2aSThomas Huth } 1087fcf5ef2aSThomas Huth 10885ad55315SDavid Gibson void ppc_store_lpcr(PowerPCCPU *cpu, target_ulong val) 1089fcf5ef2aSThomas Huth { 1090e232ecccSDavid Gibson PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 10915ad55315SDavid Gibson CPUPPCState *env = &cpu->env; 1092fcf5ef2aSThomas Huth 1093e232ecccSDavid Gibson env->spr[SPR_LPCR] = val & pcc->lpcr_mask; 1094a864a6b3SDavid Gibson env->rmls = rmls_limit(cpu); 10958fe08facSDavid Gibson ppc_hash64_update_vrma(cpu); 1096fcf5ef2aSThomas Huth } 1097a059471dSDavid Gibson 10985ad55315SDavid Gibson void helper_store_lpcr(CPUPPCState *env, target_ulong val) 10995ad55315SDavid Gibson { 1100db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 11015ad55315SDavid Gibson 11025ad55315SDavid Gibson ppc_store_lpcr(cpu, val); 11035ad55315SDavid Gibson } 11045ad55315SDavid Gibson 1105a059471dSDavid Gibson void ppc_hash64_init(PowerPCCPU *cpu) 1106a059471dSDavid Gibson { 1107a059471dSDavid Gibson CPUPPCState *env = &cpu->env; 1108a059471dSDavid Gibson PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 1109a059471dSDavid Gibson 111021e405f1SDavid Gibson if (!pcc->hash64_opts) { 111121e405f1SDavid Gibson assert(!(env->mmu_model & POWERPC_MMU_64)); 111221e405f1SDavid Gibson return; 111321e405f1SDavid Gibson } 111421e405f1SDavid Gibson 111521e405f1SDavid Gibson cpu->hash64_opts = g_memdup(pcc->hash64_opts, sizeof(*cpu->hash64_opts)); 111621e405f1SDavid Gibson } 111721e405f1SDavid Gibson 111821e405f1SDavid Gibson void ppc_hash64_finalize(PowerPCCPU *cpu) 111921e405f1SDavid Gibson { 112021e405f1SDavid Gibson g_free(cpu->hash64_opts); 112121e405f1SDavid Gibson } 112221e405f1SDavid Gibson 112321e405f1SDavid Gibson const PPCHash64Options ppc_hash64_opts_basic = { 112458969eeeSDavid Gibson .flags = 0, 112567d7d66fSDavid Gibson .slb_size = 64, 1126a059471dSDavid Gibson .sps = { 1127a059471dSDavid Gibson { .page_shift = 12, /* 4K */ 1128a059471dSDavid Gibson .slb_enc = 0, 1129a059471dSDavid Gibson .enc = { { .page_shift = 12, .pte_enc = 0 } } 1130a059471dSDavid Gibson }, 1131a059471dSDavid Gibson { .page_shift = 24, /* 16M */ 1132a059471dSDavid Gibson .slb_enc = 0x100, 1133a059471dSDavid Gibson .enc = { { .page_shift = 24, .pte_enc = 0 } } 1134a059471dSDavid Gibson }, 1135a059471dSDavid Gibson }, 1136a059471dSDavid Gibson }; 1137b07c59f7SDavid Gibson 1138b07c59f7SDavid Gibson const PPCHash64Options ppc_hash64_opts_POWER7 = { 113926cd35b8SDavid Gibson .flags = PPC_HASH64_1TSEG | PPC_HASH64_AMR | PPC_HASH64_CI_LARGEPAGE, 114067d7d66fSDavid Gibson .slb_size = 32, 1141b07c59f7SDavid Gibson .sps = { 1142b07c59f7SDavid Gibson { 1143b07c59f7SDavid Gibson .page_shift = 12, /* 4K */ 1144b07c59f7SDavid Gibson .slb_enc = 0, 1145b07c59f7SDavid Gibson .enc = { { .page_shift = 12, .pte_enc = 0 }, 1146b07c59f7SDavid Gibson { .page_shift = 16, .pte_enc = 0x7 }, 1147b07c59f7SDavid Gibson { .page_shift = 24, .pte_enc = 0x38 }, }, 1148b07c59f7SDavid Gibson }, 1149b07c59f7SDavid Gibson { 1150b07c59f7SDavid Gibson .page_shift = 16, /* 64K */ 1151b07c59f7SDavid Gibson .slb_enc = SLB_VSID_64K, 1152b07c59f7SDavid Gibson .enc = { { .page_shift = 16, .pte_enc = 0x1 }, 1153b07c59f7SDavid Gibson { .page_shift = 24, .pte_enc = 0x8 }, }, 1154b07c59f7SDavid Gibson }, 1155b07c59f7SDavid Gibson { 1156b07c59f7SDavid Gibson .page_shift = 24, /* 16M */ 1157b07c59f7SDavid Gibson .slb_enc = SLB_VSID_16M, 1158b07c59f7SDavid Gibson .enc = { { .page_shift = 24, .pte_enc = 0 }, }, 1159b07c59f7SDavid Gibson }, 1160b07c59f7SDavid Gibson { 1161b07c59f7SDavid Gibson .page_shift = 34, /* 16G */ 1162b07c59f7SDavid Gibson .slb_enc = SLB_VSID_16G, 1163b07c59f7SDavid Gibson .enc = { { .page_shift = 34, .pte_enc = 0x3 }, }, 1164b07c59f7SDavid Gibson }, 1165b07c59f7SDavid Gibson } 1166b07c59f7SDavid Gibson }; 116727f00f0aSDavid Gibson 116827f00f0aSDavid Gibson void ppc_hash64_filter_pagesizes(PowerPCCPU *cpu, 116927f00f0aSDavid Gibson bool (*cb)(void *, uint32_t, uint32_t), 117027f00f0aSDavid Gibson void *opaque) 117127f00f0aSDavid Gibson { 117227f00f0aSDavid Gibson PPCHash64Options *opts = cpu->hash64_opts; 117327f00f0aSDavid Gibson int i; 117427f00f0aSDavid Gibson int n = 0; 117527f00f0aSDavid Gibson bool ci_largepage = false; 117627f00f0aSDavid Gibson 117727f00f0aSDavid Gibson assert(opts); 117827f00f0aSDavid Gibson 117927f00f0aSDavid Gibson n = 0; 118027f00f0aSDavid Gibson for (i = 0; i < ARRAY_SIZE(opts->sps); i++) { 118127f00f0aSDavid Gibson PPCHash64SegmentPageSizes *sps = &opts->sps[i]; 118227f00f0aSDavid Gibson int j; 118327f00f0aSDavid Gibson int m = 0; 118427f00f0aSDavid Gibson 118527f00f0aSDavid Gibson assert(n <= i); 118627f00f0aSDavid Gibson 118727f00f0aSDavid Gibson if (!sps->page_shift) { 118827f00f0aSDavid Gibson break; 118927f00f0aSDavid Gibson } 119027f00f0aSDavid Gibson 119127f00f0aSDavid Gibson for (j = 0; j < ARRAY_SIZE(sps->enc); j++) { 119227f00f0aSDavid Gibson PPCHash64PageSize *ps = &sps->enc[j]; 119327f00f0aSDavid Gibson 119427f00f0aSDavid Gibson assert(m <= j); 119527f00f0aSDavid Gibson if (!ps->page_shift) { 119627f00f0aSDavid Gibson break; 119727f00f0aSDavid Gibson } 119827f00f0aSDavid Gibson 119927f00f0aSDavid Gibson if (cb(opaque, sps->page_shift, ps->page_shift)) { 120027f00f0aSDavid Gibson if (ps->page_shift >= 16) { 120127f00f0aSDavid Gibson ci_largepage = true; 120227f00f0aSDavid Gibson } 120327f00f0aSDavid Gibson sps->enc[m++] = *ps; 120427f00f0aSDavid Gibson } 120527f00f0aSDavid Gibson } 120627f00f0aSDavid Gibson 120727f00f0aSDavid Gibson /* Clear rest of the row */ 120827f00f0aSDavid Gibson for (j = m; j < ARRAY_SIZE(sps->enc); j++) { 120927f00f0aSDavid Gibson memset(&sps->enc[j], 0, sizeof(sps->enc[j])); 121027f00f0aSDavid Gibson } 121127f00f0aSDavid Gibson 121227f00f0aSDavid Gibson if (m) { 121327f00f0aSDavid Gibson n++; 121427f00f0aSDavid Gibson } 121527f00f0aSDavid Gibson } 121627f00f0aSDavid Gibson 121727f00f0aSDavid Gibson /* Clear the rest of the table */ 121827f00f0aSDavid Gibson for (i = n; i < ARRAY_SIZE(opts->sps); i++) { 121927f00f0aSDavid Gibson memset(&opts->sps[i], 0, sizeof(opts->sps[i])); 122027f00f0aSDavid Gibson } 122127f00f0aSDavid Gibson 122227f00f0aSDavid Gibson if (!ci_largepage) { 122327f00f0aSDavid Gibson opts->flags &= ~PPC_HASH64_CI_LARGEPAGE; 122427f00f0aSDavid Gibson } 122527f00f0aSDavid Gibson } 1226