1901c4eafSBlue Swirl /* 2901c4eafSBlue Swirl * Miscellaneous PowerPC emulation helpers for QEMU. 3901c4eafSBlue Swirl * 4901c4eafSBlue Swirl * Copyright (c) 2003-2007 Jocelyn Mayer 5901c4eafSBlue Swirl * 6901c4eafSBlue Swirl * This library is free software; you can redistribute it and/or 7901c4eafSBlue Swirl * modify it under the terms of the GNU Lesser General Public 8901c4eafSBlue Swirl * License as published by the Free Software Foundation; either 9901c4eafSBlue Swirl * version 2 of the License, or (at your option) any later version. 10901c4eafSBlue Swirl * 11901c4eafSBlue Swirl * This library is distributed in the hope that it will be useful, 12901c4eafSBlue Swirl * but WITHOUT ANY WARRANTY; without even the implied warranty of 13901c4eafSBlue Swirl * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14901c4eafSBlue Swirl * Lesser General Public License for more details. 15901c4eafSBlue Swirl * 16901c4eafSBlue Swirl * You should have received a copy of the GNU Lesser General Public 17901c4eafSBlue Swirl * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18901c4eafSBlue Swirl */ 190d75590dSPeter Maydell #include "qemu/osdep.h" 20901c4eafSBlue Swirl #include "cpu.h" 2163c91552SPaolo Bonzini #include "exec/exec-all.h" 222ef6175aSRichard Henderson #include "exec/helper-proto.h" 236b375544SJoel Stanley #include "qemu/error-report.h" 24901c4eafSBlue Swirl 25901c4eafSBlue Swirl #include "helper_regs.h" 26901c4eafSBlue Swirl 27901c4eafSBlue Swirl /*****************************************************************************/ 28901c4eafSBlue Swirl /* SPR accesses */ 29d523dd00SBlue Swirl void helper_load_dump_spr(CPUPPCState *env, uint32_t sprn) 30901c4eafSBlue Swirl { 31901c4eafSBlue Swirl qemu_log("Read SPR %d %03x => " TARGET_FMT_lx "\n", sprn, sprn, 32901c4eafSBlue Swirl env->spr[sprn]); 33901c4eafSBlue Swirl } 34901c4eafSBlue Swirl 35d523dd00SBlue Swirl void helper_store_dump_spr(CPUPPCState *env, uint32_t sprn) 36901c4eafSBlue Swirl { 37901c4eafSBlue Swirl qemu_log("Write SPR %d %03x <= " TARGET_FMT_lx "\n", sprn, sprn, 38901c4eafSBlue Swirl env->spr[sprn]); 39901c4eafSBlue Swirl } 407019cb3dSAlexey Kardashevskiy 417019cb3dSAlexey Kardashevskiy #ifdef TARGET_PPC64 427019cb3dSAlexey Kardashevskiy static void raise_fu_exception(CPUPPCState *env, uint32_t bit, 4357a2988bSBenjamin Herrenschmidt uint32_t sprn, uint32_t cause, 4457a2988bSBenjamin Herrenschmidt uintptr_t raddr) 457019cb3dSAlexey Kardashevskiy { 467019cb3dSAlexey Kardashevskiy qemu_log("Facility SPR %d is unavailable (SPR FSCR:%d)\n", sprn, bit); 477019cb3dSAlexey Kardashevskiy 487019cb3dSAlexey Kardashevskiy env->spr[SPR_FSCR] &= ~((target_ulong)FSCR_IC_MASK << FSCR_IC_POS); 497019cb3dSAlexey Kardashevskiy cause &= FSCR_IC_MASK; 507019cb3dSAlexey Kardashevskiy env->spr[SPR_FSCR] |= (target_ulong)cause << FSCR_IC_POS; 517019cb3dSAlexey Kardashevskiy 5257a2988bSBenjamin Herrenschmidt raise_exception_err_ra(env, POWERPC_EXCP_FU, 0, raddr); 537019cb3dSAlexey Kardashevskiy } 547019cb3dSAlexey Kardashevskiy #endif 557019cb3dSAlexey Kardashevskiy 567019cb3dSAlexey Kardashevskiy void helper_fscr_facility_check(CPUPPCState *env, uint32_t bit, 577019cb3dSAlexey Kardashevskiy uint32_t sprn, uint32_t cause) 587019cb3dSAlexey Kardashevskiy { 597019cb3dSAlexey Kardashevskiy #ifdef TARGET_PPC64 607019cb3dSAlexey Kardashevskiy if (env->spr[SPR_FSCR] & (1ULL << bit)) { 617019cb3dSAlexey Kardashevskiy /* Facility is enabled, continue */ 627019cb3dSAlexey Kardashevskiy return; 637019cb3dSAlexey Kardashevskiy } 6457a2988bSBenjamin Herrenschmidt raise_fu_exception(env, bit, sprn, cause, GETPC()); 657019cb3dSAlexey Kardashevskiy #endif 667019cb3dSAlexey Kardashevskiy } 677019cb3dSAlexey Kardashevskiy 68cdcdda27SAlexey Kardashevskiy void helper_msr_facility_check(CPUPPCState *env, uint32_t bit, 69cdcdda27SAlexey Kardashevskiy uint32_t sprn, uint32_t cause) 70cdcdda27SAlexey Kardashevskiy { 71cdcdda27SAlexey Kardashevskiy #ifdef TARGET_PPC64 72cdcdda27SAlexey Kardashevskiy if (env->msr & (1ULL << bit)) { 73cdcdda27SAlexey Kardashevskiy /* Facility is enabled, continue */ 74cdcdda27SAlexey Kardashevskiy return; 75cdcdda27SAlexey Kardashevskiy } 7657a2988bSBenjamin Herrenschmidt raise_fu_exception(env, bit, sprn, cause, GETPC()); 77cdcdda27SAlexey Kardashevskiy #endif 78cdcdda27SAlexey Kardashevskiy } 79cdcdda27SAlexey Kardashevskiy 80901c4eafSBlue Swirl #if !defined(CONFIG_USER_ONLY) 81901c4eafSBlue Swirl 82d523dd00SBlue Swirl void helper_store_sdr1(CPUPPCState *env, target_ulong val) 83901c4eafSBlue Swirl { 842828c4cdSMark Cave-Ayland if (env->spr[SPR_SDR1] != val) { 85901c4eafSBlue Swirl ppc_store_sdr1(env, val); 86*db70b311SRichard Henderson tlb_flush(env_cpu(env)); 872828c4cdSMark Cave-Ayland } 88901c4eafSBlue Swirl } 89901c4eafSBlue Swirl 904a7518e0SCédric Le Goater #if defined(TARGET_PPC64) 914a7518e0SCédric Le Goater void helper_store_ptcr(CPUPPCState *env, target_ulong val) 924a7518e0SCédric Le Goater { 934a7518e0SCédric Le Goater if (env->spr[SPR_PTCR] != val) { 944a7518e0SCédric Le Goater ppc_store_ptcr(env, val); 95*db70b311SRichard Henderson tlb_flush(env_cpu(env)); 964a7518e0SCédric Le Goater } 974a7518e0SCédric Le Goater } 986b375544SJoel Stanley 996b375544SJoel Stanley void helper_store_pcr(CPUPPCState *env, target_ulong value) 1006b375544SJoel Stanley { 101*db70b311SRichard Henderson PowerPCCPU *cpu = env_archcpu(env); 1026b375544SJoel Stanley PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 1036b375544SJoel Stanley 1046b375544SJoel Stanley env->spr[SPR_PCR] = value & pcc->pcr_mask; 1056b375544SJoel Stanley } 1064a7518e0SCédric Le Goater #endif /* defined(TARGET_PPC64) */ 1074a7518e0SCédric Le Goater 10831b2b0f8SSuraj Jitindar Singh void helper_store_pidr(CPUPPCState *env, target_ulong val) 10931b2b0f8SSuraj Jitindar Singh { 11031b2b0f8SSuraj Jitindar Singh env->spr[SPR_BOOKS_PID] = val; 111*db70b311SRichard Henderson tlb_flush(env_cpu(env)); 11231b2b0f8SSuraj Jitindar Singh } 11331b2b0f8SSuraj Jitindar Singh 114c4dae9cdSBenjamin Herrenschmidt void helper_store_lpidr(CPUPPCState *env, target_ulong val) 115c4dae9cdSBenjamin Herrenschmidt { 116c4dae9cdSBenjamin Herrenschmidt env->spr[SPR_LPIDR] = val; 117c4dae9cdSBenjamin Herrenschmidt 118c4dae9cdSBenjamin Herrenschmidt /* 119c4dae9cdSBenjamin Herrenschmidt * We need to flush the TLB on LPID changes as we only tag HV vs 120c4dae9cdSBenjamin Herrenschmidt * guest in TCG TLB. Also the quadrants means the HV will 121c4dae9cdSBenjamin Herrenschmidt * potentially access and cache entries for the current LPID as 122c4dae9cdSBenjamin Herrenschmidt * well. 123c4dae9cdSBenjamin Herrenschmidt */ 124*db70b311SRichard Henderson tlb_flush(env_cpu(env)); 125c4dae9cdSBenjamin Herrenschmidt } 126c4dae9cdSBenjamin Herrenschmidt 127d523dd00SBlue Swirl void helper_store_hid0_601(CPUPPCState *env, target_ulong val) 128901c4eafSBlue Swirl { 129901c4eafSBlue Swirl target_ulong hid0; 130901c4eafSBlue Swirl 131901c4eafSBlue Swirl hid0 = env->spr[SPR_HID0]; 132901c4eafSBlue Swirl if ((val ^ hid0) & 0x00000008) { 133901c4eafSBlue Swirl /* Change current endianness */ 134901c4eafSBlue Swirl env->hflags &= ~(1 << MSR_LE); 135901c4eafSBlue Swirl env->hflags_nmsr &= ~(1 << MSR_LE); 136901c4eafSBlue Swirl env->hflags_nmsr |= (1 << MSR_LE) & (((val >> 3) & 1) << MSR_LE); 137901c4eafSBlue Swirl env->hflags |= env->hflags_nmsr; 138901c4eafSBlue Swirl qemu_log("%s: set endianness to %c => " TARGET_FMT_lx "\n", __func__, 139901c4eafSBlue Swirl val & 0x8 ? 'l' : 'b', env->hflags); 140901c4eafSBlue Swirl } 141901c4eafSBlue Swirl env->spr[SPR_HID0] = (uint32_t)val; 142901c4eafSBlue Swirl } 143901c4eafSBlue Swirl 144d523dd00SBlue Swirl void helper_store_403_pbr(CPUPPCState *env, uint32_t num, target_ulong value) 145901c4eafSBlue Swirl { 146901c4eafSBlue Swirl if (likely(env->pb[num] != value)) { 147901c4eafSBlue Swirl env->pb[num] = value; 148901c4eafSBlue Swirl /* Should be optimized */ 149*db70b311SRichard Henderson tlb_flush(env_cpu(env)); 150901c4eafSBlue Swirl } 151901c4eafSBlue Swirl } 152901c4eafSBlue Swirl 153d523dd00SBlue Swirl void helper_store_40x_dbcr0(CPUPPCState *env, target_ulong val) 154901c4eafSBlue Swirl { 155901c4eafSBlue Swirl store_40x_dbcr0(env, val); 156901c4eafSBlue Swirl } 157901c4eafSBlue Swirl 158d523dd00SBlue Swirl void helper_store_40x_sler(CPUPPCState *env, target_ulong val) 159901c4eafSBlue Swirl { 160901c4eafSBlue Swirl store_40x_sler(env, val); 161901c4eafSBlue Swirl } 162901c4eafSBlue Swirl #endif 163901c4eafSBlue Swirl /*****************************************************************************/ 164901c4eafSBlue Swirl /* PowerPC 601 specific instructions (POWER bridge) */ 165901c4eafSBlue Swirl 166d523dd00SBlue Swirl target_ulong helper_clcs(CPUPPCState *env, uint32_t arg) 167901c4eafSBlue Swirl { 168901c4eafSBlue Swirl switch (arg) { 169901c4eafSBlue Swirl case 0x0CUL: 170901c4eafSBlue Swirl /* Instruction cache line size */ 171901c4eafSBlue Swirl return env->icache_line_size; 172901c4eafSBlue Swirl break; 173901c4eafSBlue Swirl case 0x0DUL: 174901c4eafSBlue Swirl /* Data cache line size */ 175901c4eafSBlue Swirl return env->dcache_line_size; 176901c4eafSBlue Swirl break; 177901c4eafSBlue Swirl case 0x0EUL: 178901c4eafSBlue Swirl /* Minimum cache line size */ 179901c4eafSBlue Swirl return (env->icache_line_size < env->dcache_line_size) ? 180901c4eafSBlue Swirl env->icache_line_size : env->dcache_line_size; 181901c4eafSBlue Swirl break; 182901c4eafSBlue Swirl case 0x0FUL: 183901c4eafSBlue Swirl /* Maximum cache line size */ 184901c4eafSBlue Swirl return (env->icache_line_size > env->dcache_line_size) ? 185901c4eafSBlue Swirl env->icache_line_size : env->dcache_line_size; 186901c4eafSBlue Swirl break; 187901c4eafSBlue Swirl default: 188901c4eafSBlue Swirl /* Undefined */ 189901c4eafSBlue Swirl return 0; 190901c4eafSBlue Swirl break; 191901c4eafSBlue Swirl } 192901c4eafSBlue Swirl } 1938555f71dSBlue Swirl 1948555f71dSBlue Swirl /*****************************************************************************/ 1958555f71dSBlue Swirl /* Special registers manipulation */ 1968555f71dSBlue Swirl 1978555f71dSBlue Swirl /* GDBstub can read and write MSR... */ 1988555f71dSBlue Swirl void ppc_store_msr(CPUPPCState *env, target_ulong value) 1998555f71dSBlue Swirl { 2008555f71dSBlue Swirl hreg_store_msr(env, value, 0); 2018555f71dSBlue Swirl } 202f0278900SBenjamin Herrenschmidt 203d81b4327SDavid Gibson /* 204d81b4327SDavid Gibson * This code is lifted from MacOnLinux. It is called whenever THRM1,2 205d81b4327SDavid Gibson * or 3 is read an fixes up the values in such a way that will make 206d81b4327SDavid Gibson * MacOS not hang. These registers exist on some 75x and 74xx 207d81b4327SDavid Gibson * processors. 208f0278900SBenjamin Herrenschmidt */ 209f0278900SBenjamin Herrenschmidt void helper_fixup_thrm(CPUPPCState *env) 210f0278900SBenjamin Herrenschmidt { 211f0278900SBenjamin Herrenschmidt target_ulong v, t; 212f0278900SBenjamin Herrenschmidt int i; 213f0278900SBenjamin Herrenschmidt 214f0278900SBenjamin Herrenschmidt #define THRM1_TIN (1 << 31) 215f0278900SBenjamin Herrenschmidt #define THRM1_TIV (1 << 30) 216f0278900SBenjamin Herrenschmidt #define THRM1_THRES(x) (((x) & 0x7f) << 23) 217f0278900SBenjamin Herrenschmidt #define THRM1_TID (1 << 2) 218f0278900SBenjamin Herrenschmidt #define THRM1_TIE (1 << 1) 219f0278900SBenjamin Herrenschmidt #define THRM1_V (1 << 0) 220f0278900SBenjamin Herrenschmidt #define THRM3_E (1 << 0) 221f0278900SBenjamin Herrenschmidt 222f0278900SBenjamin Herrenschmidt if (!(env->spr[SPR_THRM3] & THRM3_E)) { 223f0278900SBenjamin Herrenschmidt return; 224f0278900SBenjamin Herrenschmidt } 225f0278900SBenjamin Herrenschmidt 226f0278900SBenjamin Herrenschmidt /* Note: Thermal interrupts are unimplemented */ 227f0278900SBenjamin Herrenschmidt for (i = SPR_THRM1; i <= SPR_THRM2; i++) { 228f0278900SBenjamin Herrenschmidt v = env->spr[i]; 229f0278900SBenjamin Herrenschmidt if (!(v & THRM1_V)) { 230f0278900SBenjamin Herrenschmidt continue; 231f0278900SBenjamin Herrenschmidt } 232f0278900SBenjamin Herrenschmidt v |= THRM1_TIV; 233f0278900SBenjamin Herrenschmidt v &= ~THRM1_TIN; 234f0278900SBenjamin Herrenschmidt t = v & THRM1_THRES(127); 235f0278900SBenjamin Herrenschmidt if ((v & THRM1_TID) && t < THRM1_THRES(24)) { 236f0278900SBenjamin Herrenschmidt v |= THRM1_TIN; 237f0278900SBenjamin Herrenschmidt } 238f0278900SBenjamin Herrenschmidt if (!(v & THRM1_TID) && t > THRM1_THRES(24)) { 239f0278900SBenjamin Herrenschmidt v |= THRM1_TIN; 240f0278900SBenjamin Herrenschmidt } 241f0278900SBenjamin Herrenschmidt env->spr[i] = v; 242f0278900SBenjamin Herrenschmidt } 243f0278900SBenjamin Herrenschmidt } 244