1ad71ed68SBlue Swirl /* 2ad71ed68SBlue Swirl * PowerPC exception emulation helpers for QEMU. 3ad71ed68SBlue Swirl * 4ad71ed68SBlue Swirl * Copyright (c) 2003-2007 Jocelyn Mayer 5ad71ed68SBlue Swirl * 6ad71ed68SBlue Swirl * This library is free software; you can redistribute it and/or 7ad71ed68SBlue Swirl * modify it under the terms of the GNU Lesser General Public 8ad71ed68SBlue Swirl * License as published by the Free Software Foundation; either 96bd039cdSChetan Pant * version 2.1 of the License, or (at your option) any later version. 10ad71ed68SBlue Swirl * 11ad71ed68SBlue Swirl * This library is distributed in the hope that it will be useful, 12ad71ed68SBlue Swirl * but WITHOUT ANY WARRANTY; without even the implied warranty of 13ad71ed68SBlue Swirl * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14ad71ed68SBlue Swirl * Lesser General Public License for more details. 15ad71ed68SBlue Swirl * 16ad71ed68SBlue Swirl * You should have received a copy of the GNU Lesser General Public 17ad71ed68SBlue Swirl * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18ad71ed68SBlue Swirl */ 190d75590dSPeter Maydell #include "qemu/osdep.h" 20f1c29ebcSThomas Huth #include "qemu/main-loop.h" 21cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h" 22ad71ed68SBlue Swirl #include "cpu.h" 2363c91552SPaolo Bonzini #include "exec/exec-all.h" 240f3110faSRichard Henderson #include "internal.h" 25ad71ed68SBlue Swirl #include "helper_regs.h" 267b694df6SMatheus Ferst #include "hw/ppc/ppc.h" 27ad71ed68SBlue Swirl 282eb1ef73SCédric Le Goater #include "trace.h" 292eb1ef73SCédric Le Goater 302b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 312b44e219SBruno Larsen (billionai) #include "exec/helper-proto.h" 322b44e219SBruno Larsen (billionai) #include "exec/cpu_ldst.h" 332b44e219SBruno Larsen (billionai) #endif 342b44e219SBruno Larsen (billionai) 35c79c73f6SBlue Swirl /*****************************************************************************/ 36c79c73f6SBlue Swirl /* Exception processing */ 37f725245cSPhilippe Mathieu-Daudé #if !defined(CONFIG_USER_ONLY) 3897a8ea5aSAndreas Färber 396789f23bSCédric Le Goater static const char *powerpc_excp_name(int excp) 406789f23bSCédric Le Goater { 416789f23bSCédric Le Goater switch (excp) { 426789f23bSCédric Le Goater case POWERPC_EXCP_CRITICAL: return "CRITICAL"; 436789f23bSCédric Le Goater case POWERPC_EXCP_MCHECK: return "MCHECK"; 446789f23bSCédric Le Goater case POWERPC_EXCP_DSI: return "DSI"; 456789f23bSCédric Le Goater case POWERPC_EXCP_ISI: return "ISI"; 466789f23bSCédric Le Goater case POWERPC_EXCP_EXTERNAL: return "EXTERNAL"; 476789f23bSCédric Le Goater case POWERPC_EXCP_ALIGN: return "ALIGN"; 486789f23bSCédric Le Goater case POWERPC_EXCP_PROGRAM: return "PROGRAM"; 496789f23bSCédric Le Goater case POWERPC_EXCP_FPU: return "FPU"; 506789f23bSCédric Le Goater case POWERPC_EXCP_SYSCALL: return "SYSCALL"; 516789f23bSCédric Le Goater case POWERPC_EXCP_APU: return "APU"; 526789f23bSCédric Le Goater case POWERPC_EXCP_DECR: return "DECR"; 536789f23bSCédric Le Goater case POWERPC_EXCP_FIT: return "FIT"; 546789f23bSCédric Le Goater case POWERPC_EXCP_WDT: return "WDT"; 556789f23bSCédric Le Goater case POWERPC_EXCP_DTLB: return "DTLB"; 566789f23bSCédric Le Goater case POWERPC_EXCP_ITLB: return "ITLB"; 576789f23bSCédric Le Goater case POWERPC_EXCP_DEBUG: return "DEBUG"; 586789f23bSCédric Le Goater case POWERPC_EXCP_SPEU: return "SPEU"; 596789f23bSCédric Le Goater case POWERPC_EXCP_EFPDI: return "EFPDI"; 606789f23bSCédric Le Goater case POWERPC_EXCP_EFPRI: return "EFPRI"; 616789f23bSCédric Le Goater case POWERPC_EXCP_EPERFM: return "EPERFM"; 626789f23bSCédric Le Goater case POWERPC_EXCP_DOORI: return "DOORI"; 636789f23bSCédric Le Goater case POWERPC_EXCP_DOORCI: return "DOORCI"; 646789f23bSCédric Le Goater case POWERPC_EXCP_GDOORI: return "GDOORI"; 656789f23bSCédric Le Goater case POWERPC_EXCP_GDOORCI: return "GDOORCI"; 666789f23bSCédric Le Goater case POWERPC_EXCP_HYPPRIV: return "HYPPRIV"; 676789f23bSCédric Le Goater case POWERPC_EXCP_RESET: return "RESET"; 686789f23bSCédric Le Goater case POWERPC_EXCP_DSEG: return "DSEG"; 696789f23bSCédric Le Goater case POWERPC_EXCP_ISEG: return "ISEG"; 706789f23bSCédric Le Goater case POWERPC_EXCP_HDECR: return "HDECR"; 716789f23bSCédric Le Goater case POWERPC_EXCP_TRACE: return "TRACE"; 726789f23bSCédric Le Goater case POWERPC_EXCP_HDSI: return "HDSI"; 736789f23bSCédric Le Goater case POWERPC_EXCP_HISI: return "HISI"; 746789f23bSCédric Le Goater case POWERPC_EXCP_HDSEG: return "HDSEG"; 756789f23bSCédric Le Goater case POWERPC_EXCP_HISEG: return "HISEG"; 766789f23bSCédric Le Goater case POWERPC_EXCP_VPU: return "VPU"; 776789f23bSCédric Le Goater case POWERPC_EXCP_PIT: return "PIT"; 786789f23bSCédric Le Goater case POWERPC_EXCP_EMUL: return "EMUL"; 796789f23bSCédric Le Goater case POWERPC_EXCP_IFTLB: return "IFTLB"; 806789f23bSCédric Le Goater case POWERPC_EXCP_DLTLB: return "DLTLB"; 816789f23bSCédric Le Goater case POWERPC_EXCP_DSTLB: return "DSTLB"; 826789f23bSCédric Le Goater case POWERPC_EXCP_FPA: return "FPA"; 836789f23bSCédric Le Goater case POWERPC_EXCP_DABR: return "DABR"; 846789f23bSCédric Le Goater case POWERPC_EXCP_IABR: return "IABR"; 856789f23bSCédric Le Goater case POWERPC_EXCP_SMI: return "SMI"; 866789f23bSCédric Le Goater case POWERPC_EXCP_PERFM: return "PERFM"; 876789f23bSCédric Le Goater case POWERPC_EXCP_THERM: return "THERM"; 886789f23bSCédric Le Goater case POWERPC_EXCP_VPUA: return "VPUA"; 896789f23bSCédric Le Goater case POWERPC_EXCP_SOFTP: return "SOFTP"; 906789f23bSCédric Le Goater case POWERPC_EXCP_MAINT: return "MAINT"; 916789f23bSCédric Le Goater case POWERPC_EXCP_MEXTBR: return "MEXTBR"; 926789f23bSCédric Le Goater case POWERPC_EXCP_NMEXTBR: return "NMEXTBR"; 936789f23bSCédric Le Goater case POWERPC_EXCP_ITLBE: return "ITLBE"; 946789f23bSCédric Le Goater case POWERPC_EXCP_DTLBE: return "DTLBE"; 956789f23bSCédric Le Goater case POWERPC_EXCP_VSXU: return "VSXU"; 966789f23bSCédric Le Goater case POWERPC_EXCP_FU: return "FU"; 976789f23bSCédric Le Goater case POWERPC_EXCP_HV_EMU: return "HV_EMU"; 986789f23bSCédric Le Goater case POWERPC_EXCP_HV_MAINT: return "HV_MAINT"; 996789f23bSCédric Le Goater case POWERPC_EXCP_HV_FU: return "HV_FU"; 1006789f23bSCédric Le Goater case POWERPC_EXCP_SDOOR: return "SDOOR"; 1016789f23bSCédric Le Goater case POWERPC_EXCP_SDOOR_HV: return "SDOOR_HV"; 1026789f23bSCédric Le Goater case POWERPC_EXCP_HVIRT: return "HVIRT"; 1036789f23bSCédric Le Goater case POWERPC_EXCP_SYSCALL_VECTORED: return "SYSCALL_VECTORED"; 1046789f23bSCédric Le Goater default: 1056789f23bSCédric Le Goater g_assert_not_reached(); 1066789f23bSCédric Le Goater } 1076789f23bSCédric Le Goater } 1086789f23bSCédric Le Goater 10962e79ef9SCédric Le Goater static void dump_syscall(CPUPPCState *env) 110c79c73f6SBlue Swirl { 1116dc6b557SNicholas Piggin qemu_log_mask(CPU_LOG_INT, "syscall r0=%016" PRIx64 1126dc6b557SNicholas Piggin " r3=%016" PRIx64 " r4=%016" PRIx64 " r5=%016" PRIx64 1136dc6b557SNicholas Piggin " r6=%016" PRIx64 " r7=%016" PRIx64 " r8=%016" PRIx64 114c79c73f6SBlue Swirl " nip=" TARGET_FMT_lx "\n", 115c79c73f6SBlue Swirl ppc_dump_gpr(env, 0), ppc_dump_gpr(env, 3), 116c79c73f6SBlue Swirl ppc_dump_gpr(env, 4), ppc_dump_gpr(env, 5), 1176dc6b557SNicholas Piggin ppc_dump_gpr(env, 6), ppc_dump_gpr(env, 7), 1186dc6b557SNicholas Piggin ppc_dump_gpr(env, 8), env->nip); 1196dc6b557SNicholas Piggin } 1206dc6b557SNicholas Piggin 12162e79ef9SCédric Le Goater static void dump_hcall(CPUPPCState *env) 1226dc6b557SNicholas Piggin { 1236dc6b557SNicholas Piggin qemu_log_mask(CPU_LOG_INT, "hypercall r3=%016" PRIx64 1246dc6b557SNicholas Piggin " r4=%016" PRIx64 " r5=%016" PRIx64 " r6=%016" PRIx64 1256dc6b557SNicholas Piggin " r7=%016" PRIx64 " r8=%016" PRIx64 " r9=%016" PRIx64 1266dc6b557SNicholas Piggin " r10=%016" PRIx64 " r11=%016" PRIx64 " r12=%016" PRIx64 1276dc6b557SNicholas Piggin " nip=" TARGET_FMT_lx "\n", 1286dc6b557SNicholas Piggin ppc_dump_gpr(env, 3), ppc_dump_gpr(env, 4), 1296dc6b557SNicholas Piggin ppc_dump_gpr(env, 5), ppc_dump_gpr(env, 6), 1306dc6b557SNicholas Piggin ppc_dump_gpr(env, 7), ppc_dump_gpr(env, 8), 1316dc6b557SNicholas Piggin ppc_dump_gpr(env, 9), ppc_dump_gpr(env, 10), 1326dc6b557SNicholas Piggin ppc_dump_gpr(env, 11), ppc_dump_gpr(env, 12), 1336dc6b557SNicholas Piggin env->nip); 134c79c73f6SBlue Swirl } 135c79c73f6SBlue Swirl 136e4e27df7SFabiano Rosas static void ppc_excp_debug_sw_tlb(CPUPPCState *env, int excp) 137e4e27df7SFabiano Rosas { 138e4e27df7SFabiano Rosas const char *es; 139e4e27df7SFabiano Rosas target_ulong *miss, *cmp; 140e4e27df7SFabiano Rosas int en; 141e4e27df7SFabiano Rosas 1422e089eceSFabiano Rosas if (!qemu_loglevel_mask(CPU_LOG_MMU)) { 143e4e27df7SFabiano Rosas return; 144e4e27df7SFabiano Rosas } 145e4e27df7SFabiano Rosas 146e4e27df7SFabiano Rosas if (excp == POWERPC_EXCP_IFTLB) { 147e4e27df7SFabiano Rosas es = "I"; 148e4e27df7SFabiano Rosas en = 'I'; 149e4e27df7SFabiano Rosas miss = &env->spr[SPR_IMISS]; 150e4e27df7SFabiano Rosas cmp = &env->spr[SPR_ICMP]; 151e4e27df7SFabiano Rosas } else { 152e4e27df7SFabiano Rosas if (excp == POWERPC_EXCP_DLTLB) { 153e4e27df7SFabiano Rosas es = "DL"; 154e4e27df7SFabiano Rosas } else { 155e4e27df7SFabiano Rosas es = "DS"; 156e4e27df7SFabiano Rosas } 157e4e27df7SFabiano Rosas en = 'D'; 158e4e27df7SFabiano Rosas miss = &env->spr[SPR_DMISS]; 159e4e27df7SFabiano Rosas cmp = &env->spr[SPR_DCMP]; 160e4e27df7SFabiano Rosas } 161e4e27df7SFabiano Rosas qemu_log("6xx %sTLB miss: %cM " TARGET_FMT_lx " %cC " 162e4e27df7SFabiano Rosas TARGET_FMT_lx " H1 " TARGET_FMT_lx " H2 " 163e4e27df7SFabiano Rosas TARGET_FMT_lx " %08x\n", es, en, *miss, en, *cmp, 164e4e27df7SFabiano Rosas env->spr[SPR_HASH1], env->spr[SPR_HASH2], 165e4e27df7SFabiano Rosas env->error_code); 166e4e27df7SFabiano Rosas } 167e4e27df7SFabiano Rosas 16828091374SFabiano Rosas #if defined(TARGET_PPC64) 169dead760bSBenjamin Herrenschmidt static int powerpc_reset_wakeup(CPUState *cs, CPUPPCState *env, int excp, 170dead760bSBenjamin Herrenschmidt target_ulong *msr) 171dead760bSBenjamin Herrenschmidt { 172dead760bSBenjamin Herrenschmidt /* We no longer are in a PM state */ 1731e7fd61dSBenjamin Herrenschmidt env->resume_as_sreset = false; 174dead760bSBenjamin Herrenschmidt 175dead760bSBenjamin Herrenschmidt /* Pretend to be returning from doze always as we don't lose state */ 1760911a60cSLeonardo Bras *msr |= SRR1_WS_NOLOSS; 177dead760bSBenjamin Herrenschmidt 178dead760bSBenjamin Herrenschmidt /* Machine checks are sent normally */ 179dead760bSBenjamin Herrenschmidt if (excp == POWERPC_EXCP_MCHECK) { 180dead760bSBenjamin Herrenschmidt return excp; 181dead760bSBenjamin Herrenschmidt } 182dead760bSBenjamin Herrenschmidt switch (excp) { 183dead760bSBenjamin Herrenschmidt case POWERPC_EXCP_RESET: 1840911a60cSLeonardo Bras *msr |= SRR1_WAKERESET; 185dead760bSBenjamin Herrenschmidt break; 186dead760bSBenjamin Herrenschmidt case POWERPC_EXCP_EXTERNAL: 1870911a60cSLeonardo Bras *msr |= SRR1_WAKEEE; 188dead760bSBenjamin Herrenschmidt break; 189dead760bSBenjamin Herrenschmidt case POWERPC_EXCP_DECR: 1900911a60cSLeonardo Bras *msr |= SRR1_WAKEDEC; 191dead760bSBenjamin Herrenschmidt break; 192dead760bSBenjamin Herrenschmidt case POWERPC_EXCP_SDOOR: 1930911a60cSLeonardo Bras *msr |= SRR1_WAKEDBELL; 194dead760bSBenjamin Herrenschmidt break; 195dead760bSBenjamin Herrenschmidt case POWERPC_EXCP_SDOOR_HV: 1960911a60cSLeonardo Bras *msr |= SRR1_WAKEHDBELL; 197dead760bSBenjamin Herrenschmidt break; 198dead760bSBenjamin Herrenschmidt case POWERPC_EXCP_HV_MAINT: 1990911a60cSLeonardo Bras *msr |= SRR1_WAKEHMI; 200dead760bSBenjamin Herrenschmidt break; 201d8ce5fd6SBenjamin Herrenschmidt case POWERPC_EXCP_HVIRT: 2020911a60cSLeonardo Bras *msr |= SRR1_WAKEHVI; 203d8ce5fd6SBenjamin Herrenschmidt break; 204dead760bSBenjamin Herrenschmidt default: 205dead760bSBenjamin Herrenschmidt cpu_abort(cs, "Unsupported exception %d in Power Save mode\n", 206dead760bSBenjamin Herrenschmidt excp); 207dead760bSBenjamin Herrenschmidt } 208dead760bSBenjamin Herrenschmidt return POWERPC_EXCP_RESET; 209dead760bSBenjamin Herrenschmidt } 210dead760bSBenjamin Herrenschmidt 2118b7e6b07SNicholas Piggin /* 2128b7e6b07SNicholas Piggin * AIL - Alternate Interrupt Location, a mode that allows interrupts to be 2138b7e6b07SNicholas Piggin * taken with the MMU on, and which uses an alternate location (e.g., so the 2148b7e6b07SNicholas Piggin * kernel/hv can map the vectors there with an effective address). 2158b7e6b07SNicholas Piggin * 2168b7e6b07SNicholas Piggin * An interrupt is considered to be taken "with AIL" or "AIL applies" if they 2178b7e6b07SNicholas Piggin * are delivered in this way. AIL requires the LPCR to be set to enable this 2188b7e6b07SNicholas Piggin * mode, and then a number of conditions have to be true for AIL to apply. 2198b7e6b07SNicholas Piggin * 2208b7e6b07SNicholas Piggin * First of all, SRESET, MCE, and HMI are always delivered without AIL, because 2218b7e6b07SNicholas Piggin * they specifically want to be in real mode (e.g., the MCE might be signaling 2228b7e6b07SNicholas Piggin * a SLB multi-hit which requires SLB flush before the MMU can be enabled). 2238b7e6b07SNicholas Piggin * 2248b7e6b07SNicholas Piggin * After that, behaviour depends on the current MSR[IR], MSR[DR], MSR[HV], 2258b7e6b07SNicholas Piggin * whether or not the interrupt changes MSR[HV] from 0 to 1, and the current 2268b7e6b07SNicholas Piggin * radix mode (LPCR[HR]). 2278b7e6b07SNicholas Piggin * 2288b7e6b07SNicholas Piggin * POWER8, POWER9 with LPCR[HR]=0 2298b7e6b07SNicholas Piggin * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL | 2308b7e6b07SNicholas Piggin * +-----------+-------------+---------+-------------+-----+ 2318b7e6b07SNicholas Piggin * | a | 00/01/10 | x | x | 0 | 2328b7e6b07SNicholas Piggin * | a | 11 | 0 | 1 | 0 | 2338b7e6b07SNicholas Piggin * | a | 11 | 1 | 1 | a | 2348b7e6b07SNicholas Piggin * | a | 11 | 0 | 0 | a | 2358b7e6b07SNicholas Piggin * +-------------------------------------------------------+ 2368b7e6b07SNicholas Piggin * 2378b7e6b07SNicholas Piggin * POWER9 with LPCR[HR]=1 2388b7e6b07SNicholas Piggin * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL | 2398b7e6b07SNicholas Piggin * +-----------+-------------+---------+-------------+-----+ 2408b7e6b07SNicholas Piggin * | a | 00/01/10 | x | x | 0 | 2418b7e6b07SNicholas Piggin * | a | 11 | x | x | a | 2428b7e6b07SNicholas Piggin * +-------------------------------------------------------+ 2438b7e6b07SNicholas Piggin * 2448b7e6b07SNicholas Piggin * The difference with POWER9 being that MSR[HV] 0->1 interrupts can be sent to 245526cdce7SNicholas Piggin * the hypervisor in AIL mode if the guest is radix. This is good for 246526cdce7SNicholas Piggin * performance but allows the guest to influence the AIL of hypervisor 247526cdce7SNicholas Piggin * interrupts using its MSR, and also the hypervisor must disallow guest 248526cdce7SNicholas Piggin * interrupts (MSR[HV] 0->0) from using AIL if the hypervisor does not want to 249526cdce7SNicholas Piggin * use AIL for its MSR[HV] 0->1 interrupts. 250526cdce7SNicholas Piggin * 251526cdce7SNicholas Piggin * POWER10 addresses those issues with a new LPCR[HAIL] bit that is applied to 252526cdce7SNicholas Piggin * interrupts that begin execution with MSR[HV]=1 (so both MSR[HV] 0->1 and 253526cdce7SNicholas Piggin * MSR[HV] 1->1). 254526cdce7SNicholas Piggin * 255526cdce7SNicholas Piggin * HAIL=1 is equivalent to AIL=3, for interrupts delivered with MSR[HV]=1. 256526cdce7SNicholas Piggin * 257526cdce7SNicholas Piggin * POWER10 behaviour is 258526cdce7SNicholas Piggin * | LPCR[AIL] | LPCR[HAIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL | 259526cdce7SNicholas Piggin * +-----------+------------+-------------+---------+-------------+-----+ 260526cdce7SNicholas Piggin * | a | h | 00/01/10 | 0 | 0 | 0 | 261526cdce7SNicholas Piggin * | a | h | 11 | 0 | 0 | a | 262526cdce7SNicholas Piggin * | a | h | x | 0 | 1 | h | 263526cdce7SNicholas Piggin * | a | h | 00/01/10 | 1 | 1 | 0 | 264526cdce7SNicholas Piggin * | a | h | 11 | 1 | 1 | h | 265526cdce7SNicholas Piggin * +--------------------------------------------------------------------+ 2668b7e6b07SNicholas Piggin */ 26710895ab6SFabiano Rosas static void ppc_excp_apply_ail(PowerPCCPU *cpu, int excp, target_ulong msr, 26810895ab6SFabiano Rosas target_ulong *new_msr, target_ulong *vector) 2692586a4d7SFabiano Rosas { 27010895ab6SFabiano Rosas PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 2718b7e6b07SNicholas Piggin CPUPPCState *env = &cpu->env; 2728b7e6b07SNicholas Piggin bool mmu_all_on = ((msr >> MSR_IR) & 1) && ((msr >> MSR_DR) & 1); 2738b7e6b07SNicholas Piggin bool hv_escalation = !(msr & MSR_HVB) && (*new_msr & MSR_HVB); 2748b7e6b07SNicholas Piggin int ail = 0; 2752586a4d7SFabiano Rosas 2768b7e6b07SNicholas Piggin if (excp == POWERPC_EXCP_MCHECK || 2778b7e6b07SNicholas Piggin excp == POWERPC_EXCP_RESET || 2788b7e6b07SNicholas Piggin excp == POWERPC_EXCP_HV_MAINT) { 2798b7e6b07SNicholas Piggin /* SRESET, MCE, HMI never apply AIL */ 2808b7e6b07SNicholas Piggin return; 2812586a4d7SFabiano Rosas } 2822586a4d7SFabiano Rosas 28310895ab6SFabiano Rosas if (!(pcc->lpcr_mask & LPCR_AIL)) { 28410895ab6SFabiano Rosas /* This CPU does not have AIL */ 28510895ab6SFabiano Rosas return; 28610895ab6SFabiano Rosas } 28710895ab6SFabiano Rosas 28810895ab6SFabiano Rosas /* P8 & P9 */ 28910895ab6SFabiano Rosas if (!(pcc->lpcr_mask & LPCR_HAIL)) { 2908b7e6b07SNicholas Piggin if (!mmu_all_on) { 2918b7e6b07SNicholas Piggin /* AIL only works if MSR[IR] and MSR[DR] are both enabled. */ 2928b7e6b07SNicholas Piggin return; 2938b7e6b07SNicholas Piggin } 2948b7e6b07SNicholas Piggin if (hv_escalation && !(env->spr[SPR_LPCR] & LPCR_HR)) { 2958b7e6b07SNicholas Piggin /* 2968b7e6b07SNicholas Piggin * AIL does not work if there is a MSR[HV] 0->1 transition and the 2978b7e6b07SNicholas Piggin * partition is in HPT mode. For radix guests, such interrupts are 2988b7e6b07SNicholas Piggin * allowed to be delivered to the hypervisor in ail mode. 2998b7e6b07SNicholas Piggin */ 3008b7e6b07SNicholas Piggin return; 3018b7e6b07SNicholas Piggin } 3028b7e6b07SNicholas Piggin 3038b7e6b07SNicholas Piggin ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT; 3048b7e6b07SNicholas Piggin if (ail == 0) { 3058b7e6b07SNicholas Piggin return; 3068b7e6b07SNicholas Piggin } 3078b7e6b07SNicholas Piggin if (ail == 1) { 3088b7e6b07SNicholas Piggin /* AIL=1 is reserved, treat it like AIL=0 */ 3098b7e6b07SNicholas Piggin return; 3108b7e6b07SNicholas Piggin } 311526cdce7SNicholas Piggin 31210895ab6SFabiano Rosas /* P10 and up */ 31310895ab6SFabiano Rosas } else { 314526cdce7SNicholas Piggin if (!mmu_all_on && !hv_escalation) { 315526cdce7SNicholas Piggin /* 316526cdce7SNicholas Piggin * AIL works for HV interrupts even with guest MSR[IR/DR] disabled. 317526cdce7SNicholas Piggin * Guest->guest and HV->HV interrupts do require MMU on. 318526cdce7SNicholas Piggin */ 319526cdce7SNicholas Piggin return; 320526cdce7SNicholas Piggin } 321526cdce7SNicholas Piggin 322526cdce7SNicholas Piggin if (*new_msr & MSR_HVB) { 323526cdce7SNicholas Piggin if (!(env->spr[SPR_LPCR] & LPCR_HAIL)) { 324526cdce7SNicholas Piggin /* HV interrupts depend on LPCR[HAIL] */ 325526cdce7SNicholas Piggin return; 326526cdce7SNicholas Piggin } 327526cdce7SNicholas Piggin ail = 3; /* HAIL=1 gives AIL=3 behaviour for HV interrupts */ 328526cdce7SNicholas Piggin } else { 329526cdce7SNicholas Piggin ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT; 330526cdce7SNicholas Piggin } 331526cdce7SNicholas Piggin if (ail == 0) { 332526cdce7SNicholas Piggin return; 333526cdce7SNicholas Piggin } 334526cdce7SNicholas Piggin if (ail == 1 || ail == 2) { 335526cdce7SNicholas Piggin /* AIL=1 and AIL=2 are reserved, treat them like AIL=0 */ 336526cdce7SNicholas Piggin return; 337526cdce7SNicholas Piggin } 3388b7e6b07SNicholas Piggin } 3398b7e6b07SNicholas Piggin 3408b7e6b07SNicholas Piggin /* 3418b7e6b07SNicholas Piggin * AIL applies, so the new MSR gets IR and DR set, and an offset applied 3428b7e6b07SNicholas Piggin * to the new IP. 3438b7e6b07SNicholas Piggin */ 3448b7e6b07SNicholas Piggin *new_msr |= (1 << MSR_IR) | (1 << MSR_DR); 3458b7e6b07SNicholas Piggin 3468b7e6b07SNicholas Piggin if (excp != POWERPC_EXCP_SYSCALL_VECTORED) { 3478b7e6b07SNicholas Piggin if (ail == 2) { 3488b7e6b07SNicholas Piggin *vector |= 0x0000000000018000ull; 3498b7e6b07SNicholas Piggin } else if (ail == 3) { 3508b7e6b07SNicholas Piggin *vector |= 0xc000000000004000ull; 3518b7e6b07SNicholas Piggin } 3528b7e6b07SNicholas Piggin } else { 3538b7e6b07SNicholas Piggin /* 3548b7e6b07SNicholas Piggin * scv AIL is a little different. AIL=2 does not change the address, 3558b7e6b07SNicholas Piggin * only the MSR. AIL=3 replaces the 0x17000 base with 0xc...3000. 3568b7e6b07SNicholas Piggin */ 3578b7e6b07SNicholas Piggin if (ail == 3) { 3588b7e6b07SNicholas Piggin *vector &= ~0x0000000000017000ull; /* Un-apply the base offset */ 3598b7e6b07SNicholas Piggin *vector |= 0xc000000000003000ull; /* Apply scv's AIL=3 offset */ 3608b7e6b07SNicholas Piggin } 3618b7e6b07SNicholas Piggin } 3622586a4d7SFabiano Rosas } 36328091374SFabiano Rosas #endif 364dead760bSBenjamin Herrenschmidt 3653680e994SNicholas Piggin static void powerpc_reset_excp_state(PowerPCCPU *cpu) 366ad77c6caSNicholas Piggin { 367ad77c6caSNicholas Piggin CPUState *cs = CPU(cpu); 368ad77c6caSNicholas Piggin CPUPPCState *env = &cpu->env; 369ad77c6caSNicholas Piggin 3703680e994SNicholas Piggin /* Reset exception state */ 3713680e994SNicholas Piggin cs->exception_index = POWERPC_EXCP_NONE; 3723680e994SNicholas Piggin env->error_code = 0; 3733680e994SNicholas Piggin } 3743680e994SNicholas Piggin 3753680e994SNicholas Piggin static void powerpc_set_excp_state(PowerPCCPU *cpu, target_ulong vector, 3763680e994SNicholas Piggin target_ulong msr) 3773680e994SNicholas Piggin { 3783680e994SNicholas Piggin CPUPPCState *env = &cpu->env; 3793680e994SNicholas Piggin 380fce9fbafSFabiano Rosas assert((msr & env->msr_mask) == msr); 381fce9fbafSFabiano Rosas 382ad77c6caSNicholas Piggin /* 383ad77c6caSNicholas Piggin * We don't use hreg_store_msr here as already have treated any 384ad77c6caSNicholas Piggin * special case that could occur. Just store MSR and update hflags 385ad77c6caSNicholas Piggin * 386ad77c6caSNicholas Piggin * Note: We *MUST* not use hreg_store_msr() as-is anyway because it 387ad77c6caSNicholas Piggin * will prevent setting of the HV bit which some exceptions might need 388ad77c6caSNicholas Piggin * to do. 389ad77c6caSNicholas Piggin */ 3903680e994SNicholas Piggin env->nip = vector; 391fce9fbafSFabiano Rosas env->msr = msr; 392ad77c6caSNicholas Piggin hreg_compute_hflags(env); 3932fdedcbcSMatheus Ferst ppc_maybe_interrupt(env); 394ad77c6caSNicholas Piggin 3953680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 396ad77c6caSNicholas Piggin 397ad77c6caSNicholas Piggin /* 398ad77c6caSNicholas Piggin * Any interrupt is context synchronizing, check if TCG TLB needs 399ad77c6caSNicholas Piggin * a delayed flush on ppc64 400ad77c6caSNicholas Piggin */ 401ad77c6caSNicholas Piggin check_tlb_flush(env, false); 4023680e994SNicholas Piggin 4033680e994SNicholas Piggin /* Reset the reservation */ 4043680e994SNicholas Piggin env->reserve_addr = -1; 405ad77c6caSNicholas Piggin } 406ad77c6caSNicholas Piggin 407e808c2edSFabiano Rosas static void powerpc_excp_40x(PowerPCCPU *cpu, int excp) 408e808c2edSFabiano Rosas { 409e808c2edSFabiano Rosas CPUState *cs = CPU(cpu); 410e808c2edSFabiano Rosas CPUPPCState *env = &cpu->env; 411e808c2edSFabiano Rosas target_ulong msr, new_msr, vector; 4128428cdb2SFabiano Rosas int srr0, srr1; 413e808c2edSFabiano Rosas 414e808c2edSFabiano Rosas /* new srr1 value excluding must-be-zero bits */ 415e808c2edSFabiano Rosas msr = env->msr & ~0x783f0000ULL; 416e808c2edSFabiano Rosas 417e808c2edSFabiano Rosas /* 418495fc7ffSFabiano Rosas * new interrupt handler msr preserves existing ME unless 419495fc7ffSFabiano Rosas * explicitly overriden. 420e808c2edSFabiano Rosas */ 421495fc7ffSFabiano Rosas new_msr = env->msr & (((target_ulong)1 << MSR_ME)); 422e808c2edSFabiano Rosas 423e808c2edSFabiano Rosas /* target registers */ 424e808c2edSFabiano Rosas srr0 = SPR_SRR0; 425e808c2edSFabiano Rosas srr1 = SPR_SRR1; 426e808c2edSFabiano Rosas 427e808c2edSFabiano Rosas /* 428e808c2edSFabiano Rosas * Hypervisor emulation assistance interrupt only exists on server 429495fc7ffSFabiano Rosas * arch 2.05 server or later. 430e808c2edSFabiano Rosas */ 431495fc7ffSFabiano Rosas if (excp == POWERPC_EXCP_HV_EMU) { 432e808c2edSFabiano Rosas excp = POWERPC_EXCP_PROGRAM; 433e808c2edSFabiano Rosas } 434e808c2edSFabiano Rosas 435e808c2edSFabiano Rosas vector = env->excp_vectors[excp]; 436e808c2edSFabiano Rosas if (vector == (target_ulong)-1ULL) { 437e808c2edSFabiano Rosas cpu_abort(cs, "Raised an exception without defined vector %d\n", 438e808c2edSFabiano Rosas excp); 439e808c2edSFabiano Rosas } 440e808c2edSFabiano Rosas 441e808c2edSFabiano Rosas vector |= env->excp_prefix; 442e808c2edSFabiano Rosas 443e808c2edSFabiano Rosas switch (excp) { 444e808c2edSFabiano Rosas case POWERPC_EXCP_CRITICAL: /* Critical input */ 445e808c2edSFabiano Rosas srr0 = SPR_40x_SRR2; 446e808c2edSFabiano Rosas srr1 = SPR_40x_SRR3; 447e808c2edSFabiano Rosas break; 448e808c2edSFabiano Rosas case POWERPC_EXCP_MCHECK: /* Machine check exception */ 449c354d858SVíctor Colombo if (!FIELD_EX64(env->msr, MSR, ME)) { 450e808c2edSFabiano Rosas /* 451e808c2edSFabiano Rosas * Machine check exception is not enabled. Enter 452e808c2edSFabiano Rosas * checkstop state. 453e808c2edSFabiano Rosas */ 454e808c2edSFabiano Rosas fprintf(stderr, "Machine check while not allowed. " 455e808c2edSFabiano Rosas "Entering checkstop state\n"); 456e808c2edSFabiano Rosas if (qemu_log_separate()) { 457e808c2edSFabiano Rosas qemu_log("Machine check while not allowed. " 458e808c2edSFabiano Rosas "Entering checkstop state\n"); 459e808c2edSFabiano Rosas } 460e808c2edSFabiano Rosas cs->halted = 1; 461e808c2edSFabiano Rosas cpu_interrupt_exittb(cs); 462e808c2edSFabiano Rosas } 463e808c2edSFabiano Rosas 464e808c2edSFabiano Rosas /* machine check exceptions don't have ME set */ 465e808c2edSFabiano Rosas new_msr &= ~((target_ulong)1 << MSR_ME); 466e808c2edSFabiano Rosas 467e808c2edSFabiano Rosas srr0 = SPR_40x_SRR2; 468e808c2edSFabiano Rosas srr1 = SPR_40x_SRR3; 469e808c2edSFabiano Rosas break; 470e808c2edSFabiano Rosas case POWERPC_EXCP_DSI: /* Data storage exception */ 471f9911e1eSFabiano Rosas trace_ppc_excp_dsi(env->spr[SPR_40x_ESR], env->spr[SPR_40x_DEAR]); 472e808c2edSFabiano Rosas break; 473e808c2edSFabiano Rosas case POWERPC_EXCP_ISI: /* Instruction storage exception */ 474e808c2edSFabiano Rosas trace_ppc_excp_isi(msr, env->nip); 475e808c2edSFabiano Rosas break; 476e808c2edSFabiano Rosas case POWERPC_EXCP_EXTERNAL: /* External input */ 477e808c2edSFabiano Rosas break; 478e808c2edSFabiano Rosas case POWERPC_EXCP_ALIGN: /* Alignment exception */ 479e808c2edSFabiano Rosas break; 480e808c2edSFabiano Rosas case POWERPC_EXCP_PROGRAM: /* Program exception */ 481e808c2edSFabiano Rosas switch (env->error_code & ~0xF) { 482e808c2edSFabiano Rosas case POWERPC_EXCP_FP: 483da806a6cSVíctor Colombo if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) { 484e808c2edSFabiano Rosas trace_ppc_excp_fp_ignore(); 4853680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 486e808c2edSFabiano Rosas return; 487e808c2edSFabiano Rosas } 48864e62cfbSFabiano Rosas env->spr[SPR_40x_ESR] = ESR_FP; 489e808c2edSFabiano Rosas break; 490e808c2edSFabiano Rosas case POWERPC_EXCP_INVAL: 491e808c2edSFabiano Rosas trace_ppc_excp_inval(env->nip); 49264e62cfbSFabiano Rosas env->spr[SPR_40x_ESR] = ESR_PIL; 493e808c2edSFabiano Rosas break; 494e808c2edSFabiano Rosas case POWERPC_EXCP_PRIV: 49564e62cfbSFabiano Rosas env->spr[SPR_40x_ESR] = ESR_PPR; 496e808c2edSFabiano Rosas break; 497e808c2edSFabiano Rosas case POWERPC_EXCP_TRAP: 49864e62cfbSFabiano Rosas env->spr[SPR_40x_ESR] = ESR_PTR; 499e808c2edSFabiano Rosas break; 500e808c2edSFabiano Rosas default: 501e808c2edSFabiano Rosas cpu_abort(cs, "Invalid program exception %d. Aborting\n", 502e808c2edSFabiano Rosas env->error_code); 503e808c2edSFabiano Rosas break; 504e808c2edSFabiano Rosas } 505e808c2edSFabiano Rosas break; 506e808c2edSFabiano Rosas case POWERPC_EXCP_SYSCALL: /* System call exception */ 507e808c2edSFabiano Rosas dump_syscall(env); 508e808c2edSFabiano Rosas 509e808c2edSFabiano Rosas /* 510e808c2edSFabiano Rosas * We need to correct the NIP which in this case is supposed 511e808c2edSFabiano Rosas * to point to the next instruction 512e808c2edSFabiano Rosas */ 513e808c2edSFabiano Rosas env->nip += 4; 514e808c2edSFabiano Rosas break; 515e808c2edSFabiano Rosas case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */ 516e808c2edSFabiano Rosas trace_ppc_excp_print("FIT"); 517e808c2edSFabiano Rosas break; 518e808c2edSFabiano Rosas case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */ 519e808c2edSFabiano Rosas trace_ppc_excp_print("WDT"); 520e808c2edSFabiano Rosas break; 521e808c2edSFabiano Rosas case POWERPC_EXCP_DTLB: /* Data TLB error */ 522e808c2edSFabiano Rosas case POWERPC_EXCP_ITLB: /* Instruction TLB error */ 523e808c2edSFabiano Rosas break; 524e808c2edSFabiano Rosas case POWERPC_EXCP_PIT: /* Programmable interval timer interrupt */ 525e808c2edSFabiano Rosas trace_ppc_excp_print("PIT"); 526e808c2edSFabiano Rosas break; 5274d8ac1d1SFabiano Rosas case POWERPC_EXCP_DEBUG: /* Debug interrupt */ 5284d8ac1d1SFabiano Rosas cpu_abort(cs, "%s exception not implemented\n", 5294d8ac1d1SFabiano Rosas powerpc_excp_name(excp)); 5304d8ac1d1SFabiano Rosas break; 531e808c2edSFabiano Rosas default: 532e808c2edSFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 533e808c2edSFabiano Rosas break; 534e808c2edSFabiano Rosas } 535e808c2edSFabiano Rosas 536e808c2edSFabiano Rosas /* Save PC */ 537e808c2edSFabiano Rosas env->spr[srr0] = env->nip; 538e808c2edSFabiano Rosas 539e808c2edSFabiano Rosas /* Save MSR */ 540e808c2edSFabiano Rosas env->spr[srr1] = msr; 541e808c2edSFabiano Rosas 542e808c2edSFabiano Rosas powerpc_set_excp_state(cpu, vector, new_msr); 543e808c2edSFabiano Rosas } 544e808c2edSFabiano Rosas 54558d178fbSFabiano Rosas static void powerpc_excp_6xx(PowerPCCPU *cpu, int excp) 54658d178fbSFabiano Rosas { 54758d178fbSFabiano Rosas CPUState *cs = CPU(cpu); 54858d178fbSFabiano Rosas CPUPPCState *env = &cpu->env; 54958d178fbSFabiano Rosas target_ulong msr, new_msr, vector; 55058d178fbSFabiano Rosas 55158d178fbSFabiano Rosas /* new srr1 value excluding must-be-zero bits */ 55258d178fbSFabiano Rosas msr = env->msr & ~0x783f0000ULL; 55358d178fbSFabiano Rosas 55458d178fbSFabiano Rosas /* 555082d783bSFabiano Rosas * new interrupt handler msr preserves existing ME unless 55658d178fbSFabiano Rosas * explicitly overriden 55758d178fbSFabiano Rosas */ 558082d783bSFabiano Rosas new_msr = env->msr & ((target_ulong)1 << MSR_ME); 55958d178fbSFabiano Rosas 56058d178fbSFabiano Rosas /* 56158d178fbSFabiano Rosas * Hypervisor emulation assistance interrupt only exists on server 562082d783bSFabiano Rosas * arch 2.05 server or later. 56358d178fbSFabiano Rosas */ 564082d783bSFabiano Rosas if (excp == POWERPC_EXCP_HV_EMU) { 56558d178fbSFabiano Rosas excp = POWERPC_EXCP_PROGRAM; 56658d178fbSFabiano Rosas } 56758d178fbSFabiano Rosas 56858d178fbSFabiano Rosas vector = env->excp_vectors[excp]; 56958d178fbSFabiano Rosas if (vector == (target_ulong)-1ULL) { 57058d178fbSFabiano Rosas cpu_abort(cs, "Raised an exception without defined vector %d\n", 57158d178fbSFabiano Rosas excp); 57258d178fbSFabiano Rosas } 57358d178fbSFabiano Rosas 57458d178fbSFabiano Rosas vector |= env->excp_prefix; 57558d178fbSFabiano Rosas 57658d178fbSFabiano Rosas switch (excp) { 57758d178fbSFabiano Rosas case POWERPC_EXCP_CRITICAL: /* Critical input */ 57858d178fbSFabiano Rosas break; 57958d178fbSFabiano Rosas case POWERPC_EXCP_MCHECK: /* Machine check exception */ 580c354d858SVíctor Colombo if (!FIELD_EX64(env->msr, MSR, ME)) { 58158d178fbSFabiano Rosas /* 58258d178fbSFabiano Rosas * Machine check exception is not enabled. Enter 58358d178fbSFabiano Rosas * checkstop state. 58458d178fbSFabiano Rosas */ 58558d178fbSFabiano Rosas fprintf(stderr, "Machine check while not allowed. " 58658d178fbSFabiano Rosas "Entering checkstop state\n"); 58758d178fbSFabiano Rosas if (qemu_log_separate()) { 58858d178fbSFabiano Rosas qemu_log("Machine check while not allowed. " 58958d178fbSFabiano Rosas "Entering checkstop state\n"); 59058d178fbSFabiano Rosas } 59158d178fbSFabiano Rosas cs->halted = 1; 59258d178fbSFabiano Rosas cpu_interrupt_exittb(cs); 59358d178fbSFabiano Rosas } 59458d178fbSFabiano Rosas 59558d178fbSFabiano Rosas /* machine check exceptions don't have ME set */ 59658d178fbSFabiano Rosas new_msr &= ~((target_ulong)1 << MSR_ME); 59758d178fbSFabiano Rosas 59858d178fbSFabiano Rosas break; 59958d178fbSFabiano Rosas case POWERPC_EXCP_DSI: /* Data storage exception */ 60058d178fbSFabiano Rosas trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]); 60158d178fbSFabiano Rosas break; 60258d178fbSFabiano Rosas case POWERPC_EXCP_ISI: /* Instruction storage exception */ 60358d178fbSFabiano Rosas trace_ppc_excp_isi(msr, env->nip); 60458d178fbSFabiano Rosas msr |= env->error_code; 60558d178fbSFabiano Rosas break; 60658d178fbSFabiano Rosas case POWERPC_EXCP_EXTERNAL: /* External input */ 60758d178fbSFabiano Rosas break; 60858d178fbSFabiano Rosas case POWERPC_EXCP_ALIGN: /* Alignment exception */ 60958d178fbSFabiano Rosas /* Get rS/rD and rA from faulting opcode */ 61058d178fbSFabiano Rosas /* 61158d178fbSFabiano Rosas * Note: the opcode fields will not be set properly for a 61258d178fbSFabiano Rosas * direct store load/store, but nobody cares as nobody 61358d178fbSFabiano Rosas * actually uses direct store segments. 61458d178fbSFabiano Rosas */ 61558d178fbSFabiano Rosas env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16; 61658d178fbSFabiano Rosas break; 61758d178fbSFabiano Rosas case POWERPC_EXCP_PROGRAM: /* Program exception */ 61858d178fbSFabiano Rosas switch (env->error_code & ~0xF) { 61958d178fbSFabiano Rosas case POWERPC_EXCP_FP: 620da806a6cSVíctor Colombo if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) { 62158d178fbSFabiano Rosas trace_ppc_excp_fp_ignore(); 6223680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 62358d178fbSFabiano Rosas return; 62458d178fbSFabiano Rosas } 62558d178fbSFabiano Rosas 62658d178fbSFabiano Rosas /* 62758d178fbSFabiano Rosas * FP exceptions always have NIP pointing to the faulting 62858d178fbSFabiano Rosas * instruction, so always use store_next and claim we are 62958d178fbSFabiano Rosas * precise in the MSR. 63058d178fbSFabiano Rosas */ 63158d178fbSFabiano Rosas msr |= 0x00100000; 63258d178fbSFabiano Rosas break; 63358d178fbSFabiano Rosas case POWERPC_EXCP_INVAL: 63458d178fbSFabiano Rosas trace_ppc_excp_inval(env->nip); 63558d178fbSFabiano Rosas msr |= 0x00080000; 63658d178fbSFabiano Rosas break; 63758d178fbSFabiano Rosas case POWERPC_EXCP_PRIV: 63858d178fbSFabiano Rosas msr |= 0x00040000; 63958d178fbSFabiano Rosas break; 64058d178fbSFabiano Rosas case POWERPC_EXCP_TRAP: 64158d178fbSFabiano Rosas msr |= 0x00020000; 64258d178fbSFabiano Rosas break; 64358d178fbSFabiano Rosas default: 64458d178fbSFabiano Rosas /* Should never occur */ 64558d178fbSFabiano Rosas cpu_abort(cs, "Invalid program exception %d. Aborting\n", 64658d178fbSFabiano Rosas env->error_code); 64758d178fbSFabiano Rosas break; 64858d178fbSFabiano Rosas } 64958d178fbSFabiano Rosas break; 65058d178fbSFabiano Rosas case POWERPC_EXCP_SYSCALL: /* System call exception */ 65158d178fbSFabiano Rosas dump_syscall(env); 65258d178fbSFabiano Rosas 65358d178fbSFabiano Rosas /* 65458d178fbSFabiano Rosas * We need to correct the NIP which in this case is supposed 65558d178fbSFabiano Rosas * to point to the next instruction 65658d178fbSFabiano Rosas */ 65758d178fbSFabiano Rosas env->nip += 4; 65858d178fbSFabiano Rosas break; 65958d178fbSFabiano Rosas case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */ 66058d178fbSFabiano Rosas case POWERPC_EXCP_DECR: /* Decrementer exception */ 66158d178fbSFabiano Rosas break; 66258d178fbSFabiano Rosas case POWERPC_EXCP_DTLB: /* Data TLB error */ 66358d178fbSFabiano Rosas case POWERPC_EXCP_ITLB: /* Instruction TLB error */ 66458d178fbSFabiano Rosas break; 66558d178fbSFabiano Rosas case POWERPC_EXCP_RESET: /* System reset exception */ 6668e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 66758d178fbSFabiano Rosas cpu_abort(cs, "Trying to deliver power-saving system reset " 66858d178fbSFabiano Rosas "exception %d with no HV support\n", excp); 66958d178fbSFabiano Rosas } 67058d178fbSFabiano Rosas break; 67158d178fbSFabiano Rosas case POWERPC_EXCP_TRACE: /* Trace exception */ 67258d178fbSFabiano Rosas break; 67358d178fbSFabiano Rosas case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */ 67458d178fbSFabiano Rosas case POWERPC_EXCP_DLTLB: /* Data load TLB miss */ 67558d178fbSFabiano Rosas case POWERPC_EXCP_DSTLB: /* Data store TLB miss */ 67658d178fbSFabiano Rosas /* Swap temporary saved registers with GPRs */ 67758d178fbSFabiano Rosas if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) { 67858d178fbSFabiano Rosas new_msr |= (target_ulong)1 << MSR_TGPR; 67958d178fbSFabiano Rosas hreg_swap_gpr_tgpr(env); 68058d178fbSFabiano Rosas } 6818f8c7932SFabiano Rosas 68258d178fbSFabiano Rosas ppc_excp_debug_sw_tlb(env, excp); 68358d178fbSFabiano Rosas 68458d178fbSFabiano Rosas msr |= env->crf[0] << 28; 68558d178fbSFabiano Rosas msr |= env->error_code; /* key, D/I, S/L bits */ 68658d178fbSFabiano Rosas /* Set way using a LRU mechanism */ 68758d178fbSFabiano Rosas msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17; 68858d178fbSFabiano Rosas break; 68958d178fbSFabiano Rosas case POWERPC_EXCP_FPA: /* Floating-point assist exception */ 69058d178fbSFabiano Rosas case POWERPC_EXCP_DABR: /* Data address breakpoint */ 69158d178fbSFabiano Rosas case POWERPC_EXCP_IABR: /* Instruction address breakpoint */ 69258d178fbSFabiano Rosas case POWERPC_EXCP_SMI: /* System management interrupt */ 69358d178fbSFabiano Rosas case POWERPC_EXCP_MEXTBR: /* Maskable external breakpoint */ 69458d178fbSFabiano Rosas case POWERPC_EXCP_NMEXTBR: /* Non maskable external breakpoint */ 69558d178fbSFabiano Rosas cpu_abort(cs, "%s exception not implemented\n", 69658d178fbSFabiano Rosas powerpc_excp_name(excp)); 69758d178fbSFabiano Rosas break; 69858d178fbSFabiano Rosas default: 69958d178fbSFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 70058d178fbSFabiano Rosas break; 70158d178fbSFabiano Rosas } 70258d178fbSFabiano Rosas 70358d178fbSFabiano Rosas /* 70458d178fbSFabiano Rosas * Sort out endianness of interrupt, this differs depending on the 70558d178fbSFabiano Rosas * CPU, the HV mode, etc... 70658d178fbSFabiano Rosas */ 70758d178fbSFabiano Rosas if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) { 70858d178fbSFabiano Rosas new_msr |= (target_ulong)1 << MSR_LE; 70958d178fbSFabiano Rosas } 71058d178fbSFabiano Rosas 71158d178fbSFabiano Rosas /* Save PC */ 712c50eaed1SFabiano Rosas env->spr[SPR_SRR0] = env->nip; 71358d178fbSFabiano Rosas 71458d178fbSFabiano Rosas /* Save MSR */ 715c50eaed1SFabiano Rosas env->spr[SPR_SRR1] = msr; 71658d178fbSFabiano Rosas 71758d178fbSFabiano Rosas powerpc_set_excp_state(cpu, vector, new_msr); 71858d178fbSFabiano Rosas } 71958d178fbSFabiano Rosas 720ccfca2fcSFabiano Rosas static void powerpc_excp_7xx(PowerPCCPU *cpu, int excp) 721ccfca2fcSFabiano Rosas { 722ccfca2fcSFabiano Rosas CPUState *cs = CPU(cpu); 723ccfca2fcSFabiano Rosas CPUPPCState *env = &cpu->env; 724ccfca2fcSFabiano Rosas target_ulong msr, new_msr, vector; 725ccfca2fcSFabiano Rosas 726ccfca2fcSFabiano Rosas /* new srr1 value excluding must-be-zero bits */ 727ccfca2fcSFabiano Rosas msr = env->msr & ~0x783f0000ULL; 728ccfca2fcSFabiano Rosas 729ccfca2fcSFabiano Rosas /* 73093848d6aSFabiano Rosas * new interrupt handler msr preserves existing ME unless 731ccfca2fcSFabiano Rosas * explicitly overriden 732ccfca2fcSFabiano Rosas */ 73393848d6aSFabiano Rosas new_msr = env->msr & ((target_ulong)1 << MSR_ME); 734ccfca2fcSFabiano Rosas 735ccfca2fcSFabiano Rosas /* 736ccfca2fcSFabiano Rosas * Hypervisor emulation assistance interrupt only exists on server 73793848d6aSFabiano Rosas * arch 2.05 server or later. 738ccfca2fcSFabiano Rosas */ 73993848d6aSFabiano Rosas if (excp == POWERPC_EXCP_HV_EMU) { 740ccfca2fcSFabiano Rosas excp = POWERPC_EXCP_PROGRAM; 741ccfca2fcSFabiano Rosas } 742ccfca2fcSFabiano Rosas 743ccfca2fcSFabiano Rosas vector = env->excp_vectors[excp]; 744ccfca2fcSFabiano Rosas if (vector == (target_ulong)-1ULL) { 745ccfca2fcSFabiano Rosas cpu_abort(cs, "Raised an exception without defined vector %d\n", 746ccfca2fcSFabiano Rosas excp); 747ccfca2fcSFabiano Rosas } 748ccfca2fcSFabiano Rosas 749ccfca2fcSFabiano Rosas vector |= env->excp_prefix; 750ccfca2fcSFabiano Rosas 751ccfca2fcSFabiano Rosas switch (excp) { 752ccfca2fcSFabiano Rosas case POWERPC_EXCP_MCHECK: /* Machine check exception */ 753c354d858SVíctor Colombo if (!FIELD_EX64(env->msr, MSR, ME)) { 754ccfca2fcSFabiano Rosas /* 755ccfca2fcSFabiano Rosas * Machine check exception is not enabled. Enter 756ccfca2fcSFabiano Rosas * checkstop state. 757ccfca2fcSFabiano Rosas */ 758ccfca2fcSFabiano Rosas fprintf(stderr, "Machine check while not allowed. " 759ccfca2fcSFabiano Rosas "Entering checkstop state\n"); 760ccfca2fcSFabiano Rosas if (qemu_log_separate()) { 761ccfca2fcSFabiano Rosas qemu_log("Machine check while not allowed. " 762ccfca2fcSFabiano Rosas "Entering checkstop state\n"); 763ccfca2fcSFabiano Rosas } 764ccfca2fcSFabiano Rosas cs->halted = 1; 765ccfca2fcSFabiano Rosas cpu_interrupt_exittb(cs); 766ccfca2fcSFabiano Rosas } 767ccfca2fcSFabiano Rosas 768ccfca2fcSFabiano Rosas /* machine check exceptions don't have ME set */ 769ccfca2fcSFabiano Rosas new_msr &= ~((target_ulong)1 << MSR_ME); 770ccfca2fcSFabiano Rosas 771ccfca2fcSFabiano Rosas break; 772ccfca2fcSFabiano Rosas case POWERPC_EXCP_DSI: /* Data storage exception */ 773ccfca2fcSFabiano Rosas trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]); 774ccfca2fcSFabiano Rosas break; 775ccfca2fcSFabiano Rosas case POWERPC_EXCP_ISI: /* Instruction storage exception */ 776ccfca2fcSFabiano Rosas trace_ppc_excp_isi(msr, env->nip); 777ccfca2fcSFabiano Rosas msr |= env->error_code; 778ccfca2fcSFabiano Rosas break; 779ccfca2fcSFabiano Rosas case POWERPC_EXCP_EXTERNAL: /* External input */ 780ccfca2fcSFabiano Rosas break; 781ccfca2fcSFabiano Rosas case POWERPC_EXCP_ALIGN: /* Alignment exception */ 782ccfca2fcSFabiano Rosas /* Get rS/rD and rA from faulting opcode */ 783ccfca2fcSFabiano Rosas /* 784ccfca2fcSFabiano Rosas * Note: the opcode fields will not be set properly for a 785ccfca2fcSFabiano Rosas * direct store load/store, but nobody cares as nobody 786ccfca2fcSFabiano Rosas * actually uses direct store segments. 787ccfca2fcSFabiano Rosas */ 788ccfca2fcSFabiano Rosas env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16; 789ccfca2fcSFabiano Rosas break; 790ccfca2fcSFabiano Rosas case POWERPC_EXCP_PROGRAM: /* Program exception */ 791ccfca2fcSFabiano Rosas switch (env->error_code & ~0xF) { 792ccfca2fcSFabiano Rosas case POWERPC_EXCP_FP: 793da806a6cSVíctor Colombo if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) { 794ccfca2fcSFabiano Rosas trace_ppc_excp_fp_ignore(); 7953680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 796ccfca2fcSFabiano Rosas return; 797ccfca2fcSFabiano Rosas } 798ccfca2fcSFabiano Rosas 799ccfca2fcSFabiano Rosas /* 800ccfca2fcSFabiano Rosas * FP exceptions always have NIP pointing to the faulting 801ccfca2fcSFabiano Rosas * instruction, so always use store_next and claim we are 802ccfca2fcSFabiano Rosas * precise in the MSR. 803ccfca2fcSFabiano Rosas */ 804ccfca2fcSFabiano Rosas msr |= 0x00100000; 805ccfca2fcSFabiano Rosas break; 806ccfca2fcSFabiano Rosas case POWERPC_EXCP_INVAL: 807ccfca2fcSFabiano Rosas trace_ppc_excp_inval(env->nip); 808ccfca2fcSFabiano Rosas msr |= 0x00080000; 809ccfca2fcSFabiano Rosas break; 810ccfca2fcSFabiano Rosas case POWERPC_EXCP_PRIV: 811ccfca2fcSFabiano Rosas msr |= 0x00040000; 812ccfca2fcSFabiano Rosas break; 813ccfca2fcSFabiano Rosas case POWERPC_EXCP_TRAP: 814ccfca2fcSFabiano Rosas msr |= 0x00020000; 815ccfca2fcSFabiano Rosas break; 816ccfca2fcSFabiano Rosas default: 817ccfca2fcSFabiano Rosas /* Should never occur */ 818ccfca2fcSFabiano Rosas cpu_abort(cs, "Invalid program exception %d. Aborting\n", 819ccfca2fcSFabiano Rosas env->error_code); 820ccfca2fcSFabiano Rosas break; 821ccfca2fcSFabiano Rosas } 822ccfca2fcSFabiano Rosas break; 823ccfca2fcSFabiano Rosas case POWERPC_EXCP_SYSCALL: /* System call exception */ 8243b578635SFabiano Rosas { 8253b578635SFabiano Rosas int lev = env->error_code; 826ccfca2fcSFabiano Rosas 8273b578635SFabiano Rosas if (lev == 1 && cpu->vhyp) { 828ccfca2fcSFabiano Rosas dump_hcall(env); 829ccfca2fcSFabiano Rosas } else { 830ccfca2fcSFabiano Rosas dump_syscall(env); 831ccfca2fcSFabiano Rosas } 832ccfca2fcSFabiano Rosas 833ccfca2fcSFabiano Rosas /* 834ccfca2fcSFabiano Rosas * We need to correct the NIP which in this case is supposed 835ccfca2fcSFabiano Rosas * to point to the next instruction 836ccfca2fcSFabiano Rosas */ 837ccfca2fcSFabiano Rosas env->nip += 4; 838ccfca2fcSFabiano Rosas 8393b578635SFabiano Rosas /* 8403b578635SFabiano Rosas * The Virtual Open Firmware (VOF) relies on the 'sc 1' 8413b578635SFabiano Rosas * instruction to communicate with QEMU. The pegasos2 machine 8423b578635SFabiano Rosas * uses VOF and the 7xx CPUs, so although the 7xx don't have 8433b578635SFabiano Rosas * HV mode, we need to keep hypercall support. 8443b578635SFabiano Rosas */ 8453b578635SFabiano Rosas if (lev == 1 && cpu->vhyp) { 846ccfca2fcSFabiano Rosas PPCVirtualHypervisorClass *vhc = 847ccfca2fcSFabiano Rosas PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 848ccfca2fcSFabiano Rosas vhc->hypercall(cpu->vhyp, cpu); 849ccfca2fcSFabiano Rosas return; 850ccfca2fcSFabiano Rosas } 8513b578635SFabiano Rosas 852ccfca2fcSFabiano Rosas break; 8533b578635SFabiano Rosas } 854ccfca2fcSFabiano Rosas case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */ 855ccfca2fcSFabiano Rosas case POWERPC_EXCP_DECR: /* Decrementer exception */ 856ccfca2fcSFabiano Rosas break; 857ccfca2fcSFabiano Rosas case POWERPC_EXCP_RESET: /* System reset exception */ 8588e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 859ccfca2fcSFabiano Rosas cpu_abort(cs, "Trying to deliver power-saving system reset " 860ccfca2fcSFabiano Rosas "exception %d with no HV support\n", excp); 861ccfca2fcSFabiano Rosas } 862ccfca2fcSFabiano Rosas break; 863ccfca2fcSFabiano Rosas case POWERPC_EXCP_TRACE: /* Trace exception */ 864ccfca2fcSFabiano Rosas break; 865ccfca2fcSFabiano Rosas case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */ 866ccfca2fcSFabiano Rosas case POWERPC_EXCP_DLTLB: /* Data load TLB miss */ 867ccfca2fcSFabiano Rosas case POWERPC_EXCP_DSTLB: /* Data store TLB miss */ 868ccfca2fcSFabiano Rosas ppc_excp_debug_sw_tlb(env, excp); 869ccfca2fcSFabiano Rosas 870ccfca2fcSFabiano Rosas msr |= env->crf[0] << 28; 871ccfca2fcSFabiano Rosas msr |= env->error_code; /* key, D/I, S/L bits */ 872ccfca2fcSFabiano Rosas /* Set way using a LRU mechanism */ 873ccfca2fcSFabiano Rosas msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17; 8747df40c54SFabiano Rosas 875ccfca2fcSFabiano Rosas break; 876ccfca2fcSFabiano Rosas case POWERPC_EXCP_IABR: /* Instruction address breakpoint */ 877ccfca2fcSFabiano Rosas case POWERPC_EXCP_SMI: /* System management interrupt */ 878ccfca2fcSFabiano Rosas case POWERPC_EXCP_THERM: /* Thermal interrupt */ 879ccfca2fcSFabiano Rosas case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */ 880ccfca2fcSFabiano Rosas cpu_abort(cs, "%s exception not implemented\n", 881ccfca2fcSFabiano Rosas powerpc_excp_name(excp)); 882ccfca2fcSFabiano Rosas break; 883ccfca2fcSFabiano Rosas default: 884ccfca2fcSFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 885ccfca2fcSFabiano Rosas break; 886ccfca2fcSFabiano Rosas } 887ccfca2fcSFabiano Rosas 888ccfca2fcSFabiano Rosas /* 889ccfca2fcSFabiano Rosas * Sort out endianness of interrupt, this differs depending on the 890ccfca2fcSFabiano Rosas * CPU, the HV mode, etc... 891ccfca2fcSFabiano Rosas */ 892ccfca2fcSFabiano Rosas if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) { 893ccfca2fcSFabiano Rosas new_msr |= (target_ulong)1 << MSR_LE; 894ccfca2fcSFabiano Rosas } 895ccfca2fcSFabiano Rosas 896ccfca2fcSFabiano Rosas /* Save PC */ 897fe4b5c4cSFabiano Rosas env->spr[SPR_SRR0] = env->nip; 898ccfca2fcSFabiano Rosas 899ccfca2fcSFabiano Rosas /* Save MSR */ 900fe4b5c4cSFabiano Rosas env->spr[SPR_SRR1] = msr; 901ccfca2fcSFabiano Rosas 902ccfca2fcSFabiano Rosas powerpc_set_excp_state(cpu, vector, new_msr); 903ccfca2fcSFabiano Rosas } 904ccfca2fcSFabiano Rosas 90552926b0dSFabiano Rosas static void powerpc_excp_74xx(PowerPCCPU *cpu, int excp) 90652926b0dSFabiano Rosas { 90752926b0dSFabiano Rosas CPUState *cs = CPU(cpu); 90852926b0dSFabiano Rosas CPUPPCState *env = &cpu->env; 90952926b0dSFabiano Rosas target_ulong msr, new_msr, vector; 91052926b0dSFabiano Rosas 91152926b0dSFabiano Rosas /* new srr1 value excluding must-be-zero bits */ 91252926b0dSFabiano Rosas msr = env->msr & ~0x783f0000ULL; 91352926b0dSFabiano Rosas 91452926b0dSFabiano Rosas /* 9151f6faf8bSFabiano Rosas * new interrupt handler msr preserves existing ME unless 91652926b0dSFabiano Rosas * explicitly overriden 91752926b0dSFabiano Rosas */ 9181f6faf8bSFabiano Rosas new_msr = env->msr & ((target_ulong)1 << MSR_ME); 91952926b0dSFabiano Rosas 92052926b0dSFabiano Rosas /* 92152926b0dSFabiano Rosas * Hypervisor emulation assistance interrupt only exists on server 9221f6faf8bSFabiano Rosas * arch 2.05 server or later. 92352926b0dSFabiano Rosas */ 9241f6faf8bSFabiano Rosas if (excp == POWERPC_EXCP_HV_EMU) { 92552926b0dSFabiano Rosas excp = POWERPC_EXCP_PROGRAM; 92652926b0dSFabiano Rosas } 92752926b0dSFabiano Rosas 92852926b0dSFabiano Rosas vector = env->excp_vectors[excp]; 92952926b0dSFabiano Rosas if (vector == (target_ulong)-1ULL) { 93052926b0dSFabiano Rosas cpu_abort(cs, "Raised an exception without defined vector %d\n", 93152926b0dSFabiano Rosas excp); 93252926b0dSFabiano Rosas } 93352926b0dSFabiano Rosas 93452926b0dSFabiano Rosas vector |= env->excp_prefix; 93552926b0dSFabiano Rosas 93652926b0dSFabiano Rosas switch (excp) { 93752926b0dSFabiano Rosas case POWERPC_EXCP_MCHECK: /* Machine check exception */ 938c354d858SVíctor Colombo if (!FIELD_EX64(env->msr, MSR, ME)) { 93952926b0dSFabiano Rosas /* 94052926b0dSFabiano Rosas * Machine check exception is not enabled. Enter 94152926b0dSFabiano Rosas * checkstop state. 94252926b0dSFabiano Rosas */ 94352926b0dSFabiano Rosas fprintf(stderr, "Machine check while not allowed. " 94452926b0dSFabiano Rosas "Entering checkstop state\n"); 94552926b0dSFabiano Rosas if (qemu_log_separate()) { 94652926b0dSFabiano Rosas qemu_log("Machine check while not allowed. " 94752926b0dSFabiano Rosas "Entering checkstop state\n"); 94852926b0dSFabiano Rosas } 94952926b0dSFabiano Rosas cs->halted = 1; 95052926b0dSFabiano Rosas cpu_interrupt_exittb(cs); 95152926b0dSFabiano Rosas } 95252926b0dSFabiano Rosas 95352926b0dSFabiano Rosas /* machine check exceptions don't have ME set */ 95452926b0dSFabiano Rosas new_msr &= ~((target_ulong)1 << MSR_ME); 95552926b0dSFabiano Rosas 95652926b0dSFabiano Rosas break; 95752926b0dSFabiano Rosas case POWERPC_EXCP_DSI: /* Data storage exception */ 95852926b0dSFabiano Rosas trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]); 95952926b0dSFabiano Rosas break; 96052926b0dSFabiano Rosas case POWERPC_EXCP_ISI: /* Instruction storage exception */ 96152926b0dSFabiano Rosas trace_ppc_excp_isi(msr, env->nip); 96252926b0dSFabiano Rosas msr |= env->error_code; 96352926b0dSFabiano Rosas break; 96452926b0dSFabiano Rosas case POWERPC_EXCP_EXTERNAL: /* External input */ 96552926b0dSFabiano Rosas break; 96652926b0dSFabiano Rosas case POWERPC_EXCP_ALIGN: /* Alignment exception */ 96752926b0dSFabiano Rosas /* Get rS/rD and rA from faulting opcode */ 96852926b0dSFabiano Rosas /* 96952926b0dSFabiano Rosas * Note: the opcode fields will not be set properly for a 97052926b0dSFabiano Rosas * direct store load/store, but nobody cares as nobody 97152926b0dSFabiano Rosas * actually uses direct store segments. 97252926b0dSFabiano Rosas */ 97352926b0dSFabiano Rosas env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16; 97452926b0dSFabiano Rosas break; 97552926b0dSFabiano Rosas case POWERPC_EXCP_PROGRAM: /* Program exception */ 97652926b0dSFabiano Rosas switch (env->error_code & ~0xF) { 97752926b0dSFabiano Rosas case POWERPC_EXCP_FP: 978da806a6cSVíctor Colombo if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) { 97952926b0dSFabiano Rosas trace_ppc_excp_fp_ignore(); 9803680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 98152926b0dSFabiano Rosas return; 98252926b0dSFabiano Rosas } 98352926b0dSFabiano Rosas 98452926b0dSFabiano Rosas /* 98552926b0dSFabiano Rosas * FP exceptions always have NIP pointing to the faulting 98652926b0dSFabiano Rosas * instruction, so always use store_next and claim we are 98752926b0dSFabiano Rosas * precise in the MSR. 98852926b0dSFabiano Rosas */ 98952926b0dSFabiano Rosas msr |= 0x00100000; 99052926b0dSFabiano Rosas break; 99152926b0dSFabiano Rosas case POWERPC_EXCP_INVAL: 99252926b0dSFabiano Rosas trace_ppc_excp_inval(env->nip); 99352926b0dSFabiano Rosas msr |= 0x00080000; 99452926b0dSFabiano Rosas break; 99552926b0dSFabiano Rosas case POWERPC_EXCP_PRIV: 99652926b0dSFabiano Rosas msr |= 0x00040000; 99752926b0dSFabiano Rosas break; 99852926b0dSFabiano Rosas case POWERPC_EXCP_TRAP: 99952926b0dSFabiano Rosas msr |= 0x00020000; 100052926b0dSFabiano Rosas break; 100152926b0dSFabiano Rosas default: 100252926b0dSFabiano Rosas /* Should never occur */ 100352926b0dSFabiano Rosas cpu_abort(cs, "Invalid program exception %d. Aborting\n", 100452926b0dSFabiano Rosas env->error_code); 100552926b0dSFabiano Rosas break; 100652926b0dSFabiano Rosas } 100752926b0dSFabiano Rosas break; 100852926b0dSFabiano Rosas case POWERPC_EXCP_SYSCALL: /* System call exception */ 1009bca2c6d9SFabiano Rosas { 1010bca2c6d9SFabiano Rosas int lev = env->error_code; 101152926b0dSFabiano Rosas 101252926b0dSFabiano Rosas if ((lev == 1) && cpu->vhyp) { 101352926b0dSFabiano Rosas dump_hcall(env); 101452926b0dSFabiano Rosas } else { 101552926b0dSFabiano Rosas dump_syscall(env); 101652926b0dSFabiano Rosas } 101752926b0dSFabiano Rosas 101852926b0dSFabiano Rosas /* 101952926b0dSFabiano Rosas * We need to correct the NIP which in this case is supposed 102052926b0dSFabiano Rosas * to point to the next instruction 102152926b0dSFabiano Rosas */ 102252926b0dSFabiano Rosas env->nip += 4; 102352926b0dSFabiano Rosas 1024bca2c6d9SFabiano Rosas /* 1025bca2c6d9SFabiano Rosas * The Virtual Open Firmware (VOF) relies on the 'sc 1' 1026bca2c6d9SFabiano Rosas * instruction to communicate with QEMU. The pegasos2 machine 1027bca2c6d9SFabiano Rosas * uses VOF and the 74xx CPUs, so although the 74xx don't have 1028bca2c6d9SFabiano Rosas * HV mode, we need to keep hypercall support. 1029bca2c6d9SFabiano Rosas */ 103052926b0dSFabiano Rosas if ((lev == 1) && cpu->vhyp) { 103152926b0dSFabiano Rosas PPCVirtualHypervisorClass *vhc = 103252926b0dSFabiano Rosas PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 103352926b0dSFabiano Rosas vhc->hypercall(cpu->vhyp, cpu); 103452926b0dSFabiano Rosas return; 103552926b0dSFabiano Rosas } 1036bca2c6d9SFabiano Rosas 103752926b0dSFabiano Rosas break; 1038bca2c6d9SFabiano Rosas } 103952926b0dSFabiano Rosas case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */ 104052926b0dSFabiano Rosas case POWERPC_EXCP_DECR: /* Decrementer exception */ 104152926b0dSFabiano Rosas break; 104252926b0dSFabiano Rosas case POWERPC_EXCP_RESET: /* System reset exception */ 10438e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 104452926b0dSFabiano Rosas cpu_abort(cs, "Trying to deliver power-saving system reset " 104552926b0dSFabiano Rosas "exception %d with no HV support\n", excp); 104652926b0dSFabiano Rosas } 104752926b0dSFabiano Rosas break; 104852926b0dSFabiano Rosas case POWERPC_EXCP_TRACE: /* Trace exception */ 104952926b0dSFabiano Rosas break; 105052926b0dSFabiano Rosas case POWERPC_EXCP_VPU: /* Vector unavailable exception */ 105152926b0dSFabiano Rosas break; 105252926b0dSFabiano Rosas case POWERPC_EXCP_IABR: /* Instruction address breakpoint */ 105352926b0dSFabiano Rosas case POWERPC_EXCP_SMI: /* System management interrupt */ 105452926b0dSFabiano Rosas case POWERPC_EXCP_THERM: /* Thermal interrupt */ 105552926b0dSFabiano Rosas case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */ 105652926b0dSFabiano Rosas case POWERPC_EXCP_VPUA: /* Vector assist exception */ 105752926b0dSFabiano Rosas cpu_abort(cs, "%s exception not implemented\n", 105852926b0dSFabiano Rosas powerpc_excp_name(excp)); 105952926b0dSFabiano Rosas break; 106052926b0dSFabiano Rosas default: 106152926b0dSFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 106252926b0dSFabiano Rosas break; 106352926b0dSFabiano Rosas } 106452926b0dSFabiano Rosas 106552926b0dSFabiano Rosas /* 106652926b0dSFabiano Rosas * Sort out endianness of interrupt, this differs depending on the 106752926b0dSFabiano Rosas * CPU, the HV mode, etc... 106852926b0dSFabiano Rosas */ 106952926b0dSFabiano Rosas if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) { 107052926b0dSFabiano Rosas new_msr |= (target_ulong)1 << MSR_LE; 107152926b0dSFabiano Rosas } 107252926b0dSFabiano Rosas 107352926b0dSFabiano Rosas /* Save PC */ 1074f82db777SFabiano Rosas env->spr[SPR_SRR0] = env->nip; 107552926b0dSFabiano Rosas 107652926b0dSFabiano Rosas /* Save MSR */ 1077f82db777SFabiano Rosas env->spr[SPR_SRR1] = msr; 107852926b0dSFabiano Rosas 107952926b0dSFabiano Rosas powerpc_set_excp_state(cpu, vector, new_msr); 108052926b0dSFabiano Rosas } 108152926b0dSFabiano Rosas 1082180952ceSFabiano Rosas static void powerpc_excp_booke(PowerPCCPU *cpu, int excp) 1083180952ceSFabiano Rosas { 1084180952ceSFabiano Rosas CPUState *cs = CPU(cpu); 1085180952ceSFabiano Rosas CPUPPCState *env = &cpu->env; 1086180952ceSFabiano Rosas target_ulong msr, new_msr, vector; 1087904e8428SFabiano Rosas int srr0, srr1; 1088180952ceSFabiano Rosas 1089180952ceSFabiano Rosas msr = env->msr; 1090180952ceSFabiano Rosas 1091180952ceSFabiano Rosas /* 10929dc20cc3SFabiano Rosas * new interrupt handler msr preserves existing ME unless 1093180952ceSFabiano Rosas * explicitly overriden 1094180952ceSFabiano Rosas */ 10959dc20cc3SFabiano Rosas new_msr = env->msr & ((target_ulong)1 << MSR_ME); 1096180952ceSFabiano Rosas 1097180952ceSFabiano Rosas /* target registers */ 1098180952ceSFabiano Rosas srr0 = SPR_SRR0; 1099180952ceSFabiano Rosas srr1 = SPR_SRR1; 1100180952ceSFabiano Rosas 1101180952ceSFabiano Rosas /* 1102180952ceSFabiano Rosas * Hypervisor emulation assistance interrupt only exists on server 11039dc20cc3SFabiano Rosas * arch 2.05 server or later. 1104180952ceSFabiano Rosas */ 11059dc20cc3SFabiano Rosas if (excp == POWERPC_EXCP_HV_EMU) { 1106180952ceSFabiano Rosas excp = POWERPC_EXCP_PROGRAM; 1107180952ceSFabiano Rosas } 1108180952ceSFabiano Rosas 1109180952ceSFabiano Rosas #ifdef TARGET_PPC64 1110180952ceSFabiano Rosas /* 1111180952ceSFabiano Rosas * SPEU and VPU share the same IVOR but they exist in different 1112180952ceSFabiano Rosas * processors. SPEU is e500v1/2 only and VPU is e6500 only. 1113180952ceSFabiano Rosas */ 11149dc20cc3SFabiano Rosas if (excp == POWERPC_EXCP_VPU) { 1115180952ceSFabiano Rosas excp = POWERPC_EXCP_SPEU; 1116180952ceSFabiano Rosas } 1117180952ceSFabiano Rosas #endif 1118180952ceSFabiano Rosas 1119180952ceSFabiano Rosas vector = env->excp_vectors[excp]; 1120180952ceSFabiano Rosas if (vector == (target_ulong)-1ULL) { 1121180952ceSFabiano Rosas cpu_abort(cs, "Raised an exception without defined vector %d\n", 1122180952ceSFabiano Rosas excp); 1123180952ceSFabiano Rosas } 1124180952ceSFabiano Rosas 1125180952ceSFabiano Rosas vector |= env->excp_prefix; 1126180952ceSFabiano Rosas 1127180952ceSFabiano Rosas switch (excp) { 1128180952ceSFabiano Rosas case POWERPC_EXCP_CRITICAL: /* Critical input */ 1129180952ceSFabiano Rosas srr0 = SPR_BOOKE_CSRR0; 1130180952ceSFabiano Rosas srr1 = SPR_BOOKE_CSRR1; 1131180952ceSFabiano Rosas break; 1132180952ceSFabiano Rosas case POWERPC_EXCP_MCHECK: /* Machine check exception */ 1133c354d858SVíctor Colombo if (!FIELD_EX64(env->msr, MSR, ME)) { 1134180952ceSFabiano Rosas /* 1135180952ceSFabiano Rosas * Machine check exception is not enabled. Enter 1136180952ceSFabiano Rosas * checkstop state. 1137180952ceSFabiano Rosas */ 1138180952ceSFabiano Rosas fprintf(stderr, "Machine check while not allowed. " 1139180952ceSFabiano Rosas "Entering checkstop state\n"); 1140180952ceSFabiano Rosas if (qemu_log_separate()) { 1141180952ceSFabiano Rosas qemu_log("Machine check while not allowed. " 1142180952ceSFabiano Rosas "Entering checkstop state\n"); 1143180952ceSFabiano Rosas } 1144180952ceSFabiano Rosas cs->halted = 1; 1145180952ceSFabiano Rosas cpu_interrupt_exittb(cs); 1146180952ceSFabiano Rosas } 1147180952ceSFabiano Rosas 1148180952ceSFabiano Rosas /* machine check exceptions don't have ME set */ 1149180952ceSFabiano Rosas new_msr &= ~((target_ulong)1 << MSR_ME); 1150180952ceSFabiano Rosas 1151180952ceSFabiano Rosas /* FIXME: choose one or the other based on CPU type */ 1152180952ceSFabiano Rosas srr0 = SPR_BOOKE_MCSRR0; 1153180952ceSFabiano Rosas srr1 = SPR_BOOKE_MCSRR1; 1154180952ceSFabiano Rosas 1155180952ceSFabiano Rosas env->spr[SPR_BOOKE_CSRR0] = env->nip; 1156180952ceSFabiano Rosas env->spr[SPR_BOOKE_CSRR1] = msr; 1157db403211SFabiano Rosas 1158180952ceSFabiano Rosas break; 1159180952ceSFabiano Rosas case POWERPC_EXCP_DSI: /* Data storage exception */ 1160afdbc869SFabiano Rosas trace_ppc_excp_dsi(env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]); 1161180952ceSFabiano Rosas break; 1162180952ceSFabiano Rosas case POWERPC_EXCP_ISI: /* Instruction storage exception */ 1163180952ceSFabiano Rosas trace_ppc_excp_isi(msr, env->nip); 1164180952ceSFabiano Rosas break; 1165180952ceSFabiano Rosas case POWERPC_EXCP_EXTERNAL: /* External input */ 1166180952ceSFabiano Rosas if (env->mpic_proxy) { 1167180952ceSFabiano Rosas /* IACK the IRQ on delivery */ 1168180952ceSFabiano Rosas env->spr[SPR_BOOKE_EPR] = ldl_phys(cs->as, env->mpic_iack); 1169180952ceSFabiano Rosas } 1170180952ceSFabiano Rosas break; 1171180952ceSFabiano Rosas case POWERPC_EXCP_ALIGN: /* Alignment exception */ 1172180952ceSFabiano Rosas break; 1173180952ceSFabiano Rosas case POWERPC_EXCP_PROGRAM: /* Program exception */ 1174180952ceSFabiano Rosas switch (env->error_code & ~0xF) { 1175180952ceSFabiano Rosas case POWERPC_EXCP_FP: 1176da806a6cSVíctor Colombo if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) { 1177180952ceSFabiano Rosas trace_ppc_excp_fp_ignore(); 11783680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 1179180952ceSFabiano Rosas return; 1180180952ceSFabiano Rosas } 1181180952ceSFabiano Rosas 1182180952ceSFabiano Rosas /* 1183180952ceSFabiano Rosas * FP exceptions always have NIP pointing to the faulting 1184180952ceSFabiano Rosas * instruction, so always use store_next and claim we are 1185180952ceSFabiano Rosas * precise in the MSR. 1186180952ceSFabiano Rosas */ 1187180952ceSFabiano Rosas msr |= 0x00100000; 1188180952ceSFabiano Rosas env->spr[SPR_BOOKE_ESR] = ESR_FP; 1189180952ceSFabiano Rosas break; 1190180952ceSFabiano Rosas case POWERPC_EXCP_INVAL: 1191180952ceSFabiano Rosas trace_ppc_excp_inval(env->nip); 1192180952ceSFabiano Rosas msr |= 0x00080000; 1193180952ceSFabiano Rosas env->spr[SPR_BOOKE_ESR] = ESR_PIL; 1194180952ceSFabiano Rosas break; 1195180952ceSFabiano Rosas case POWERPC_EXCP_PRIV: 1196180952ceSFabiano Rosas msr |= 0x00040000; 1197180952ceSFabiano Rosas env->spr[SPR_BOOKE_ESR] = ESR_PPR; 1198180952ceSFabiano Rosas break; 1199180952ceSFabiano Rosas case POWERPC_EXCP_TRAP: 1200180952ceSFabiano Rosas msr |= 0x00020000; 1201180952ceSFabiano Rosas env->spr[SPR_BOOKE_ESR] = ESR_PTR; 1202180952ceSFabiano Rosas break; 1203180952ceSFabiano Rosas default: 1204180952ceSFabiano Rosas /* Should never occur */ 1205180952ceSFabiano Rosas cpu_abort(cs, "Invalid program exception %d. Aborting\n", 1206180952ceSFabiano Rosas env->error_code); 1207180952ceSFabiano Rosas break; 1208180952ceSFabiano Rosas } 1209180952ceSFabiano Rosas break; 1210180952ceSFabiano Rosas case POWERPC_EXCP_SYSCALL: /* System call exception */ 1211180952ceSFabiano Rosas dump_syscall(env); 1212180952ceSFabiano Rosas 1213180952ceSFabiano Rosas /* 1214180952ceSFabiano Rosas * We need to correct the NIP which in this case is supposed 1215180952ceSFabiano Rosas * to point to the next instruction 1216180952ceSFabiano Rosas */ 1217180952ceSFabiano Rosas env->nip += 4; 1218180952ceSFabiano Rosas break; 1219180952ceSFabiano Rosas case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */ 1220180952ceSFabiano Rosas case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */ 1221180952ceSFabiano Rosas case POWERPC_EXCP_DECR: /* Decrementer exception */ 1222180952ceSFabiano Rosas break; 1223180952ceSFabiano Rosas case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */ 1224180952ceSFabiano Rosas /* FIT on 4xx */ 1225180952ceSFabiano Rosas trace_ppc_excp_print("FIT"); 1226180952ceSFabiano Rosas break; 1227180952ceSFabiano Rosas case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */ 1228180952ceSFabiano Rosas trace_ppc_excp_print("WDT"); 1229180952ceSFabiano Rosas srr0 = SPR_BOOKE_CSRR0; 1230180952ceSFabiano Rosas srr1 = SPR_BOOKE_CSRR1; 1231180952ceSFabiano Rosas break; 1232180952ceSFabiano Rosas case POWERPC_EXCP_DTLB: /* Data TLB error */ 1233180952ceSFabiano Rosas case POWERPC_EXCP_ITLB: /* Instruction TLB error */ 1234180952ceSFabiano Rosas break; 1235180952ceSFabiano Rosas case POWERPC_EXCP_DEBUG: /* Debug interrupt */ 1236180952ceSFabiano Rosas if (env->flags & POWERPC_FLAG_DE) { 1237180952ceSFabiano Rosas /* FIXME: choose one or the other based on CPU type */ 1238180952ceSFabiano Rosas srr0 = SPR_BOOKE_DSRR0; 1239180952ceSFabiano Rosas srr1 = SPR_BOOKE_DSRR1; 1240180952ceSFabiano Rosas 1241180952ceSFabiano Rosas env->spr[SPR_BOOKE_CSRR0] = env->nip; 1242180952ceSFabiano Rosas env->spr[SPR_BOOKE_CSRR1] = msr; 1243180952ceSFabiano Rosas 1244180952ceSFabiano Rosas /* DBSR already modified by caller */ 1245180952ceSFabiano Rosas } else { 1246180952ceSFabiano Rosas cpu_abort(cs, "Debug exception triggered on unsupported model\n"); 1247180952ceSFabiano Rosas } 1248180952ceSFabiano Rosas break; 1249180952ceSFabiano Rosas case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavailable/VPU */ 1250180952ceSFabiano Rosas env->spr[SPR_BOOKE_ESR] = ESR_SPV; 1251180952ceSFabiano Rosas break; 12529364df26SNicholas Piggin case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */ 12539364df26SNicholas Piggin break; 12549364df26SNicholas Piggin case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */ 12559364df26SNicholas Piggin srr0 = SPR_BOOKE_CSRR0; 12569364df26SNicholas Piggin srr1 = SPR_BOOKE_CSRR1; 12579364df26SNicholas Piggin break; 1258180952ceSFabiano Rosas case POWERPC_EXCP_RESET: /* System reset exception */ 12598e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 1260180952ceSFabiano Rosas cpu_abort(cs, "Trying to deliver power-saving system reset " 1261180952ceSFabiano Rosas "exception %d with no HV support\n", excp); 1262180952ceSFabiano Rosas } 1263180952ceSFabiano Rosas break; 1264180952ceSFabiano Rosas case POWERPC_EXCP_EFPDI: /* Embedded floating-point data interrupt */ 1265180952ceSFabiano Rosas case POWERPC_EXCP_EFPRI: /* Embedded floating-point round interrupt */ 1266180952ceSFabiano Rosas cpu_abort(cs, "%s exception not implemented\n", 1267180952ceSFabiano Rosas powerpc_excp_name(excp)); 1268180952ceSFabiano Rosas break; 1269180952ceSFabiano Rosas default: 1270180952ceSFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 1271180952ceSFabiano Rosas break; 1272180952ceSFabiano Rosas } 1273180952ceSFabiano Rosas 1274180952ceSFabiano Rosas #if defined(TARGET_PPC64) 1275180952ceSFabiano Rosas if (env->spr[SPR_BOOKE_EPCR] & EPCR_ICM) { 1276180952ceSFabiano Rosas /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */ 1277180952ceSFabiano Rosas new_msr |= (target_ulong)1 << MSR_CM; 1278180952ceSFabiano Rosas } else { 1279180952ceSFabiano Rosas vector = (uint32_t)vector; 1280180952ceSFabiano Rosas } 1281180952ceSFabiano Rosas #endif 1282180952ceSFabiano Rosas 1283180952ceSFabiano Rosas /* Save PC */ 1284180952ceSFabiano Rosas env->spr[srr0] = env->nip; 1285180952ceSFabiano Rosas 1286180952ceSFabiano Rosas /* Save MSR */ 1287180952ceSFabiano Rosas env->spr[srr1] = msr; 1288180952ceSFabiano Rosas 1289180952ceSFabiano Rosas powerpc_set_excp_state(cpu, vector, new_msr); 1290180952ceSFabiano Rosas } 1291180952ceSFabiano Rosas 12927cebc5dbSNicholas Piggin /* 12937cebc5dbSNicholas Piggin * When running a nested HV guest under vhyp, external interrupts are 12947cebc5dbSNicholas Piggin * delivered as HVIRT. 12957cebc5dbSNicholas Piggin */ 12967cebc5dbSNicholas Piggin static bool books_vhyp_promotes_external_to_hvirt(PowerPCCPU *cpu) 12977cebc5dbSNicholas Piggin { 12987cebc5dbSNicholas Piggin if (cpu->vhyp) { 12997cebc5dbSNicholas Piggin return vhyp_cpu_in_nested(cpu); 13007cebc5dbSNicholas Piggin } 13017cebc5dbSNicholas Piggin return false; 13027cebc5dbSNicholas Piggin } 13037cebc5dbSNicholas Piggin 130430c4e426SFabiano Rosas #ifdef TARGET_PPC64 13054c6cf6b2SNicholas Piggin /* 13064c6cf6b2SNicholas Piggin * When running under vhyp, hcalls are always intercepted and sent to the 13074c6cf6b2SNicholas Piggin * vhc->hypercall handler. 13084c6cf6b2SNicholas Piggin */ 13094c6cf6b2SNicholas Piggin static bool books_vhyp_handles_hcall(PowerPCCPU *cpu) 13104c6cf6b2SNicholas Piggin { 13114c6cf6b2SNicholas Piggin if (cpu->vhyp) { 13127cebc5dbSNicholas Piggin return !vhyp_cpu_in_nested(cpu); 13137cebc5dbSNicholas Piggin } 13147cebc5dbSNicholas Piggin return false; 13157cebc5dbSNicholas Piggin } 13167cebc5dbSNicholas Piggin 13177cebc5dbSNicholas Piggin /* 13187cebc5dbSNicholas Piggin * When running a nested KVM HV guest under vhyp, HV exceptions are not 13197cebc5dbSNicholas Piggin * delivered to the guest (because there is no concept of HV support), but 13207cebc5dbSNicholas Piggin * rather they are sent tothe vhyp to exit from the L2 back to the L1 and 13217cebc5dbSNicholas Piggin * return from the H_ENTER_NESTED hypercall. 13227cebc5dbSNicholas Piggin */ 13237cebc5dbSNicholas Piggin static bool books_vhyp_handles_hv_excp(PowerPCCPU *cpu) 13247cebc5dbSNicholas Piggin { 13257cebc5dbSNicholas Piggin if (cpu->vhyp) { 13267cebc5dbSNicholas Piggin return vhyp_cpu_in_nested(cpu); 13274c6cf6b2SNicholas Piggin } 13284c6cf6b2SNicholas Piggin return false; 13294c6cf6b2SNicholas Piggin } 13304c6cf6b2SNicholas Piggin 13319f338e4dSFabiano Rosas static void powerpc_excp_books(PowerPCCPU *cpu, int excp) 13329f338e4dSFabiano Rosas { 13339f338e4dSFabiano Rosas CPUState *cs = CPU(cpu); 13349f338e4dSFabiano Rosas CPUPPCState *env = &cpu->env; 13359f338e4dSFabiano Rosas target_ulong msr, new_msr, vector; 13369f338e4dSFabiano Rosas int srr0, srr1, lev = -1; 13379f338e4dSFabiano Rosas 13389f338e4dSFabiano Rosas /* new srr1 value excluding must-be-zero bits */ 13399f338e4dSFabiano Rosas msr = env->msr & ~0x783f0000ULL; 13409f338e4dSFabiano Rosas 13419f338e4dSFabiano Rosas /* 13429f338e4dSFabiano Rosas * new interrupt handler msr preserves existing HV and ME unless 13439f338e4dSFabiano Rosas * explicitly overriden 13449f338e4dSFabiano Rosas */ 13459f338e4dSFabiano Rosas new_msr = env->msr & (((target_ulong)1 << MSR_ME) | MSR_HVB); 13469f338e4dSFabiano Rosas 13479f338e4dSFabiano Rosas /* target registers */ 13489f338e4dSFabiano Rosas srr0 = SPR_SRR0; 13499f338e4dSFabiano Rosas srr1 = SPR_SRR1; 13509f338e4dSFabiano Rosas 13519f338e4dSFabiano Rosas /* 13529f338e4dSFabiano Rosas * check for special resume at 0x100 from doze/nap/sleep/winkle on 13539f338e4dSFabiano Rosas * P7/P8/P9 13549f338e4dSFabiano Rosas */ 13559f338e4dSFabiano Rosas if (env->resume_as_sreset) { 13569f338e4dSFabiano Rosas excp = powerpc_reset_wakeup(cs, env, excp, &msr); 13579f338e4dSFabiano Rosas } 13589f338e4dSFabiano Rosas 13599f338e4dSFabiano Rosas /* 136030c4e426SFabiano Rosas * We don't want to generate a Hypervisor Emulation Assistance 13616c242e79SNicholas Piggin * Interrupt if we don't have HVB in msr_mask (PAPR mode), 13626c242e79SNicholas Piggin * unless running a nested-hv guest, in which case the L1 13636c242e79SNicholas Piggin * kernel wants the interrupt. 13649f338e4dSFabiano Rosas */ 13656c242e79SNicholas Piggin if (excp == POWERPC_EXCP_HV_EMU && !(env->msr_mask & MSR_HVB) && 13666c242e79SNicholas Piggin !books_vhyp_handles_hv_excp(cpu)) { 13679f338e4dSFabiano Rosas excp = POWERPC_EXCP_PROGRAM; 13689f338e4dSFabiano Rosas } 13699f338e4dSFabiano Rosas 13709f338e4dSFabiano Rosas vector = env->excp_vectors[excp]; 13719f338e4dSFabiano Rosas if (vector == (target_ulong)-1ULL) { 13729f338e4dSFabiano Rosas cpu_abort(cs, "Raised an exception without defined vector %d\n", 13739f338e4dSFabiano Rosas excp); 13749f338e4dSFabiano Rosas } 13759f338e4dSFabiano Rosas 13769f338e4dSFabiano Rosas vector |= env->excp_prefix; 13779f338e4dSFabiano Rosas 13789f338e4dSFabiano Rosas switch (excp) { 13799f338e4dSFabiano Rosas case POWERPC_EXCP_MCHECK: /* Machine check exception */ 1380c354d858SVíctor Colombo if (!FIELD_EX64(env->msr, MSR, ME)) { 13819f338e4dSFabiano Rosas /* 13829f338e4dSFabiano Rosas * Machine check exception is not enabled. Enter 13839f338e4dSFabiano Rosas * checkstop state. 13849f338e4dSFabiano Rosas */ 13859f338e4dSFabiano Rosas fprintf(stderr, "Machine check while not allowed. " 13869f338e4dSFabiano Rosas "Entering checkstop state\n"); 13879f338e4dSFabiano Rosas if (qemu_log_separate()) { 13889f338e4dSFabiano Rosas qemu_log("Machine check while not allowed. " 13899f338e4dSFabiano Rosas "Entering checkstop state\n"); 13909f338e4dSFabiano Rosas } 13919f338e4dSFabiano Rosas cs->halted = 1; 13929f338e4dSFabiano Rosas cpu_interrupt_exittb(cs); 13939f338e4dSFabiano Rosas } 13949f338e4dSFabiano Rosas if (env->msr_mask & MSR_HVB) { 13959f338e4dSFabiano Rosas /* 13969f338e4dSFabiano Rosas * ISA specifies HV, but can be delivered to guest with HV 13979f338e4dSFabiano Rosas * clear (e.g., see FWNMI in PAPR). 13989f338e4dSFabiano Rosas */ 13999f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 14009f338e4dSFabiano Rosas } 14019f338e4dSFabiano Rosas 14029f338e4dSFabiano Rosas /* machine check exceptions don't have ME set */ 14039f338e4dSFabiano Rosas new_msr &= ~((target_ulong)1 << MSR_ME); 14049f338e4dSFabiano Rosas 14059f338e4dSFabiano Rosas break; 14069f338e4dSFabiano Rosas case POWERPC_EXCP_DSI: /* Data storage exception */ 14079f338e4dSFabiano Rosas trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]); 14089f338e4dSFabiano Rosas break; 14099f338e4dSFabiano Rosas case POWERPC_EXCP_ISI: /* Instruction storage exception */ 14109f338e4dSFabiano Rosas trace_ppc_excp_isi(msr, env->nip); 14119f338e4dSFabiano Rosas msr |= env->error_code; 14129f338e4dSFabiano Rosas break; 14139f338e4dSFabiano Rosas case POWERPC_EXCP_EXTERNAL: /* External input */ 14149f338e4dSFabiano Rosas { 14159f338e4dSFabiano Rosas bool lpes0; 14169f338e4dSFabiano Rosas 14179f338e4dSFabiano Rosas /* 141867baff77SFabiano Rosas * LPES0 is only taken into consideration if we support HV 141967baff77SFabiano Rosas * mode for this CPU. 14209f338e4dSFabiano Rosas */ 142167baff77SFabiano Rosas if (!env->has_hv_mode) { 142267baff77SFabiano Rosas break; 14239f338e4dSFabiano Rosas } 14249f338e4dSFabiano Rosas 142567baff77SFabiano Rosas lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 142667baff77SFabiano Rosas 14279f338e4dSFabiano Rosas if (!lpes0) { 14289f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 14299f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 14309f338e4dSFabiano Rosas srr0 = SPR_HSRR0; 14319f338e4dSFabiano Rosas srr1 = SPR_HSRR1; 14329f338e4dSFabiano Rosas } 143367baff77SFabiano Rosas 14349f338e4dSFabiano Rosas break; 14359f338e4dSFabiano Rosas } 14369f338e4dSFabiano Rosas case POWERPC_EXCP_ALIGN: /* Alignment exception */ 14374ee5d281SNicholas Piggin /* Optional DSISR update was removed from ISA v3.0 */ 14384ee5d281SNicholas Piggin if (!(env->insns_flags2 & PPC2_ISA300)) { 14399f338e4dSFabiano Rosas /* Get rS/rD and rA from faulting opcode */ 14409f338e4dSFabiano Rosas /* 14419f338e4dSFabiano Rosas * Note: the opcode fields will not be set properly for a 14429f338e4dSFabiano Rosas * direct store load/store, but nobody cares as nobody 14439f338e4dSFabiano Rosas * actually uses direct store segments. 14449f338e4dSFabiano Rosas */ 14459f338e4dSFabiano Rosas env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16; 14464ee5d281SNicholas Piggin } 14479f338e4dSFabiano Rosas break; 14489f338e4dSFabiano Rosas case POWERPC_EXCP_PROGRAM: /* Program exception */ 14499f338e4dSFabiano Rosas switch (env->error_code & ~0xF) { 14509f338e4dSFabiano Rosas case POWERPC_EXCP_FP: 1451da806a6cSVíctor Colombo if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) { 14529f338e4dSFabiano Rosas trace_ppc_excp_fp_ignore(); 14533680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 14549f338e4dSFabiano Rosas return; 14559f338e4dSFabiano Rosas } 14569f338e4dSFabiano Rosas 14579f338e4dSFabiano Rosas /* 14589f338e4dSFabiano Rosas * FP exceptions always have NIP pointing to the faulting 14599f338e4dSFabiano Rosas * instruction, so always use store_next and claim we are 14609f338e4dSFabiano Rosas * precise in the MSR. 14619f338e4dSFabiano Rosas */ 14629f338e4dSFabiano Rosas msr |= 0x00100000; 14639f338e4dSFabiano Rosas break; 14649f338e4dSFabiano Rosas case POWERPC_EXCP_INVAL: 14659f338e4dSFabiano Rosas trace_ppc_excp_inval(env->nip); 14669f338e4dSFabiano Rosas msr |= 0x00080000; 14679f338e4dSFabiano Rosas break; 14689f338e4dSFabiano Rosas case POWERPC_EXCP_PRIV: 14699f338e4dSFabiano Rosas msr |= 0x00040000; 14709f338e4dSFabiano Rosas break; 14719f338e4dSFabiano Rosas case POWERPC_EXCP_TRAP: 14729f338e4dSFabiano Rosas msr |= 0x00020000; 14739f338e4dSFabiano Rosas break; 14749f338e4dSFabiano Rosas default: 14759f338e4dSFabiano Rosas /* Should never occur */ 14769f338e4dSFabiano Rosas cpu_abort(cs, "Invalid program exception %d. Aborting\n", 14779f338e4dSFabiano Rosas env->error_code); 14789f338e4dSFabiano Rosas break; 14799f338e4dSFabiano Rosas } 14809f338e4dSFabiano Rosas break; 14819f338e4dSFabiano Rosas case POWERPC_EXCP_SYSCALL: /* System call exception */ 14829f338e4dSFabiano Rosas lev = env->error_code; 14839f338e4dSFabiano Rosas 14849f338e4dSFabiano Rosas if ((lev == 1) && cpu->vhyp) { 14859f338e4dSFabiano Rosas dump_hcall(env); 14869f338e4dSFabiano Rosas } else { 14879f338e4dSFabiano Rosas dump_syscall(env); 14889f338e4dSFabiano Rosas } 14899f338e4dSFabiano Rosas 14909f338e4dSFabiano Rosas /* 14919f338e4dSFabiano Rosas * We need to correct the NIP which in this case is supposed 14929f338e4dSFabiano Rosas * to point to the next instruction 14939f338e4dSFabiano Rosas */ 14949f338e4dSFabiano Rosas env->nip += 4; 14959f338e4dSFabiano Rosas 14969f338e4dSFabiano Rosas /* "PAPR mode" built-in hypercall emulation */ 14974c6cf6b2SNicholas Piggin if ((lev == 1) && books_vhyp_handles_hcall(cpu)) { 14989f338e4dSFabiano Rosas PPCVirtualHypervisorClass *vhc = 14999f338e4dSFabiano Rosas PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 15009f338e4dSFabiano Rosas vhc->hypercall(cpu->vhyp, cpu); 15019f338e4dSFabiano Rosas return; 15029f338e4dSFabiano Rosas } 15039f338e4dSFabiano Rosas if (lev == 1) { 15049f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 15059f338e4dSFabiano Rosas } 15069f338e4dSFabiano Rosas break; 15079f338e4dSFabiano Rosas case POWERPC_EXCP_SYSCALL_VECTORED: /* scv exception */ 15089f338e4dSFabiano Rosas lev = env->error_code; 15099f338e4dSFabiano Rosas dump_syscall(env); 15109f338e4dSFabiano Rosas env->nip += 4; 15119f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_EE); 15129f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 15139f338e4dSFabiano Rosas 15149f338e4dSFabiano Rosas vector += lev * 0x20; 15159f338e4dSFabiano Rosas 15169f338e4dSFabiano Rosas env->lr = env->nip; 15179f338e4dSFabiano Rosas env->ctr = msr; 15189f338e4dSFabiano Rosas break; 15199f338e4dSFabiano Rosas case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */ 15209f338e4dSFabiano Rosas case POWERPC_EXCP_DECR: /* Decrementer exception */ 15219f338e4dSFabiano Rosas break; 15229f338e4dSFabiano Rosas case POWERPC_EXCP_RESET: /* System reset exception */ 15239f338e4dSFabiano Rosas /* A power-saving exception sets ME, otherwise it is unchanged */ 15248e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 15259f338e4dSFabiano Rosas /* indicate that we resumed from power save mode */ 15269f338e4dSFabiano Rosas msr |= 0x10000; 15279f338e4dSFabiano Rosas new_msr |= ((target_ulong)1 << MSR_ME); 15289f338e4dSFabiano Rosas } 15299f338e4dSFabiano Rosas if (env->msr_mask & MSR_HVB) { 15309f338e4dSFabiano Rosas /* 15319f338e4dSFabiano Rosas * ISA specifies HV, but can be delivered to guest with HV 15329f338e4dSFabiano Rosas * clear (e.g., see FWNMI in PAPR, NMI injection in QEMU). 15339f338e4dSFabiano Rosas */ 15349f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 15359f338e4dSFabiano Rosas } else { 15368e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 15379f338e4dSFabiano Rosas cpu_abort(cs, "Trying to deliver power-saving system reset " 15389f338e4dSFabiano Rosas "exception %d with no HV support\n", excp); 15399f338e4dSFabiano Rosas } 15409f338e4dSFabiano Rosas } 15419f338e4dSFabiano Rosas break; 15429f338e4dSFabiano Rosas case POWERPC_EXCP_DSEG: /* Data segment exception */ 15439f338e4dSFabiano Rosas case POWERPC_EXCP_ISEG: /* Instruction segment exception */ 15449f338e4dSFabiano Rosas case POWERPC_EXCP_TRACE: /* Trace exception */ 1545fd7abfabSNicholas Piggin case POWERPC_EXCP_SDOOR: /* Doorbell interrupt */ 1546*c29b0704SNicholas Piggin case POWERPC_EXCP_PERFM: /* Performance monitor interrupt */ 15479f338e4dSFabiano Rosas break; 15489f338e4dSFabiano Rosas case POWERPC_EXCP_HISI: /* Hypervisor instruction storage exception */ 15499f338e4dSFabiano Rosas msr |= env->error_code; 15509f338e4dSFabiano Rosas /* fall through */ 15519f338e4dSFabiano Rosas case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */ 15529f338e4dSFabiano Rosas case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */ 15539f338e4dSFabiano Rosas case POWERPC_EXCP_SDOOR_HV: /* Hypervisor Doorbell interrupt */ 15549f338e4dSFabiano Rosas case POWERPC_EXCP_HV_EMU: 15559f338e4dSFabiano Rosas case POWERPC_EXCP_HVIRT: /* Hypervisor virtualization */ 15569f338e4dSFabiano Rosas srr0 = SPR_HSRR0; 15579f338e4dSFabiano Rosas srr1 = SPR_HSRR1; 15589f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 15599f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 15609f338e4dSFabiano Rosas break; 15619f338e4dSFabiano Rosas case POWERPC_EXCP_VPU: /* Vector unavailable exception */ 15629f338e4dSFabiano Rosas case POWERPC_EXCP_VSXU: /* VSX unavailable exception */ 15639f338e4dSFabiano Rosas case POWERPC_EXCP_FU: /* Facility unavailable exception */ 15649f338e4dSFabiano Rosas env->spr[SPR_FSCR] |= ((target_ulong)env->error_code << 56); 15659f338e4dSFabiano Rosas break; 15669f338e4dSFabiano Rosas case POWERPC_EXCP_HV_FU: /* Hypervisor Facility Unavailable Exception */ 15679f338e4dSFabiano Rosas env->spr[SPR_HFSCR] |= ((target_ulong)env->error_code << FSCR_IC_POS); 15689f338e4dSFabiano Rosas srr0 = SPR_HSRR0; 15699f338e4dSFabiano Rosas srr1 = SPR_HSRR1; 15709f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 15719f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 15729f338e4dSFabiano Rosas break; 1573cb76bbc4SDaniel Henrique Barboza case POWERPC_EXCP_PERFM_EBB: /* Performance Monitor EBB Exception */ 1574cb76bbc4SDaniel Henrique Barboza case POWERPC_EXCP_EXTERNAL_EBB: /* External EBB Exception */ 1575cb76bbc4SDaniel Henrique Barboza env->spr[SPR_BESCR] &= ~BESCR_GE; 1576cb76bbc4SDaniel Henrique Barboza 1577cb76bbc4SDaniel Henrique Barboza /* 1578cb76bbc4SDaniel Henrique Barboza * Save NIP for rfebb insn in SPR_EBBRR. Next nip is 1579cb76bbc4SDaniel Henrique Barboza * stored in the EBB Handler SPR_EBBHR. 1580cb76bbc4SDaniel Henrique Barboza */ 1581cb76bbc4SDaniel Henrique Barboza env->spr[SPR_EBBRR] = env->nip; 1582cb76bbc4SDaniel Henrique Barboza powerpc_set_excp_state(cpu, env->spr[SPR_EBBHR], env->msr); 1583cb76bbc4SDaniel Henrique Barboza 1584cb76bbc4SDaniel Henrique Barboza /* 1585cb76bbc4SDaniel Henrique Barboza * This exception is handled in userspace. No need to proceed. 1586cb76bbc4SDaniel Henrique Barboza */ 1587cb76bbc4SDaniel Henrique Barboza return; 15889f338e4dSFabiano Rosas case POWERPC_EXCP_THERM: /* Thermal interrupt */ 15899f338e4dSFabiano Rosas case POWERPC_EXCP_VPUA: /* Vector assist exception */ 15909f338e4dSFabiano Rosas case POWERPC_EXCP_MAINT: /* Maintenance exception */ 159130c4e426SFabiano Rosas case POWERPC_EXCP_HV_MAINT: /* Hypervisor Maintenance exception */ 15929f338e4dSFabiano Rosas cpu_abort(cs, "%s exception not implemented\n", 15939f338e4dSFabiano Rosas powerpc_excp_name(excp)); 15949f338e4dSFabiano Rosas break; 15959f338e4dSFabiano Rosas default: 15969f338e4dSFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 15979f338e4dSFabiano Rosas break; 15989f338e4dSFabiano Rosas } 15999f338e4dSFabiano Rosas 16009f338e4dSFabiano Rosas /* 16019f338e4dSFabiano Rosas * Sort out endianness of interrupt, this differs depending on the 16029f338e4dSFabiano Rosas * CPU, the HV mode, etc... 16039f338e4dSFabiano Rosas */ 16049f338e4dSFabiano Rosas if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) { 16059f338e4dSFabiano Rosas new_msr |= (target_ulong)1 << MSR_LE; 16069f338e4dSFabiano Rosas } 16079f338e4dSFabiano Rosas 16089f338e4dSFabiano Rosas new_msr |= (target_ulong)1 << MSR_SF; 16099f338e4dSFabiano Rosas 16109f338e4dSFabiano Rosas if (excp != POWERPC_EXCP_SYSCALL_VECTORED) { 16119f338e4dSFabiano Rosas /* Save PC */ 16129f338e4dSFabiano Rosas env->spr[srr0] = env->nip; 16139f338e4dSFabiano Rosas 16149f338e4dSFabiano Rosas /* Save MSR */ 16159f338e4dSFabiano Rosas env->spr[srr1] = msr; 16169f338e4dSFabiano Rosas } 16179f338e4dSFabiano Rosas 16187cebc5dbSNicholas Piggin if ((new_msr & MSR_HVB) && books_vhyp_handles_hv_excp(cpu)) { 16197cebc5dbSNicholas Piggin PPCVirtualHypervisorClass *vhc = 16207cebc5dbSNicholas Piggin PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 16217cebc5dbSNicholas Piggin /* Deliver interrupt to L1 by returning from the H_ENTER_NESTED call */ 16227cebc5dbSNicholas Piggin vhc->deliver_hv_excp(cpu, excp); 16237cebc5dbSNicholas Piggin 16247cebc5dbSNicholas Piggin powerpc_reset_excp_state(cpu); 16257cebc5dbSNicholas Piggin 16267cebc5dbSNicholas Piggin } else { 16277cebc5dbSNicholas Piggin /* Sanity check */ 16287cebc5dbSNicholas Piggin if (!(env->msr_mask & MSR_HVB) && srr0 == SPR_HSRR0) { 16297cebc5dbSNicholas Piggin cpu_abort(cs, "Trying to deliver HV exception (HSRR) %d with " 16307cebc5dbSNicholas Piggin "no HV support\n", excp); 16317cebc5dbSNicholas Piggin } 16327cebc5dbSNicholas Piggin 16339f338e4dSFabiano Rosas /* This can update new_msr and vector if AIL applies */ 163410895ab6SFabiano Rosas ppc_excp_apply_ail(cpu, excp, msr, &new_msr, &vector); 16359f338e4dSFabiano Rosas 16369f338e4dSFabiano Rosas powerpc_set_excp_state(cpu, vector, new_msr); 16379f338e4dSFabiano Rosas } 16387cebc5dbSNicholas Piggin } 163930c4e426SFabiano Rosas #else 164030c4e426SFabiano Rosas static inline void powerpc_excp_books(PowerPCCPU *cpu, int excp) 164130c4e426SFabiano Rosas { 164230c4e426SFabiano Rosas g_assert_not_reached(); 164330c4e426SFabiano Rosas } 164430c4e426SFabiano Rosas #endif 16459f338e4dSFabiano Rosas 1646dc88dd0aSFabiano Rosas static void powerpc_excp(PowerPCCPU *cpu, int excp) 1647dc88dd0aSFabiano Rosas { 1648c6eaac89SFabiano Rosas CPUState *cs = CPU(cpu); 1649dc88dd0aSFabiano Rosas CPUPPCState *env = &cpu->env; 1650dc88dd0aSFabiano Rosas 1651c6eaac89SFabiano Rosas if (excp <= POWERPC_EXCP_NONE || excp >= POWERPC_EXCP_NB) { 1652c6eaac89SFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 1653c6eaac89SFabiano Rosas } 1654c6eaac89SFabiano Rosas 1655c6eaac89SFabiano Rosas qemu_log_mask(CPU_LOG_INT, "Raise exception at " TARGET_FMT_lx 1656c6eaac89SFabiano Rosas " => %s (%d) error=%02x\n", env->nip, powerpc_excp_name(excp), 1657c6eaac89SFabiano Rosas excp, env->error_code); 1658c6eaac89SFabiano Rosas 1659dc88dd0aSFabiano Rosas switch (env->excp_model) { 1660e808c2edSFabiano Rosas case POWERPC_EXCP_40x: 1661e808c2edSFabiano Rosas powerpc_excp_40x(cpu, excp); 1662e808c2edSFabiano Rosas break; 166358d178fbSFabiano Rosas case POWERPC_EXCP_6xx: 166458d178fbSFabiano Rosas powerpc_excp_6xx(cpu, excp); 166558d178fbSFabiano Rosas break; 1666ccfca2fcSFabiano Rosas case POWERPC_EXCP_7xx: 1667ccfca2fcSFabiano Rosas powerpc_excp_7xx(cpu, excp); 1668ccfca2fcSFabiano Rosas break; 166952926b0dSFabiano Rosas case POWERPC_EXCP_74xx: 167052926b0dSFabiano Rosas powerpc_excp_74xx(cpu, excp); 167152926b0dSFabiano Rosas break; 1672180952ceSFabiano Rosas case POWERPC_EXCP_BOOKE: 1673180952ceSFabiano Rosas powerpc_excp_booke(cpu, excp); 1674180952ceSFabiano Rosas break; 16759f338e4dSFabiano Rosas case POWERPC_EXCP_970: 16769f338e4dSFabiano Rosas case POWERPC_EXCP_POWER7: 16779f338e4dSFabiano Rosas case POWERPC_EXCP_POWER8: 16789f338e4dSFabiano Rosas case POWERPC_EXCP_POWER9: 16799f338e4dSFabiano Rosas case POWERPC_EXCP_POWER10: 16809f338e4dSFabiano Rosas powerpc_excp_books(cpu, excp); 16819f338e4dSFabiano Rosas break; 1682dc88dd0aSFabiano Rosas default: 168328091374SFabiano Rosas g_assert_not_reached(); 1684dc88dd0aSFabiano Rosas } 1685dc88dd0aSFabiano Rosas } 1686dc88dd0aSFabiano Rosas 168797a8ea5aSAndreas Färber void ppc_cpu_do_interrupt(CPUState *cs) 1688c79c73f6SBlue Swirl { 168997a8ea5aSAndreas Färber PowerPCCPU *cpu = POWERPC_CPU(cs); 16905c26a5b3SAndreas Färber 169193130c84SFabiano Rosas powerpc_excp(cpu, cs->exception_index); 1692c79c73f6SBlue Swirl } 1693c79c73f6SBlue Swirl 16942dfecf01SMatheus Ferst #if defined(TARGET_PPC64) 1695c8e1de2eSMatheus Ferst #define P7_UNUSED_INTERRUPTS \ 1696c8e1de2eSMatheus Ferst (PPC_INTERRUPT_RESET | PPC_INTERRUPT_HVIRT | PPC_INTERRUPT_CEXT | \ 1697c8e1de2eSMatheus Ferst PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT | \ 1698c8e1de2eSMatheus Ferst PPC_INTERRUPT_PIT | PPC_INTERRUPT_DOORBELL | PPC_INTERRUPT_HDOORBELL | \ 1699c8e1de2eSMatheus Ferst PPC_INTERRUPT_THERM | PPC_INTERRUPT_EBB) 1700c8e1de2eSMatheus Ferst 17019c713713SMatheus Ferst static int p7_interrupt_powersave(CPUPPCState *env) 17029c713713SMatheus Ferst { 17039c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_EXT) && 17049c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P7_PECE0)) { 17059c713713SMatheus Ferst return PPC_INTERRUPT_EXT; 17069c713713SMatheus Ferst } 17079c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DECR) && 17089c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P7_PECE1)) { 17099c713713SMatheus Ferst return PPC_INTERRUPT_DECR; 17109c713713SMatheus Ferst } 17119c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_MCK) && 17129c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) { 17139c713713SMatheus Ferst return PPC_INTERRUPT_MCK; 17149c713713SMatheus Ferst } 17159c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HMI) && 17169c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) { 17179c713713SMatheus Ferst return PPC_INTERRUPT_HMI; 17189c713713SMatheus Ferst } 17199c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_RESET) { 17209c713713SMatheus Ferst return PPC_INTERRUPT_RESET; 17219c713713SMatheus Ferst } 17229c713713SMatheus Ferst return 0; 17239c713713SMatheus Ferst } 17249c713713SMatheus Ferst 1725bf303fb3SMatheus Ferst static int p7_next_unmasked_interrupt(CPUPPCState *env) 1726bf303fb3SMatheus Ferst { 1727022b7128SMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 1728022b7128SMatheus Ferst CPUState *cs = CPU(cpu); 1729022b7128SMatheus Ferst /* Ignore MSR[EE] when coming out of some power management states */ 1730022b7128SMatheus Ferst bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset; 1731bf303fb3SMatheus Ferst 1732c8e1de2eSMatheus Ferst assert((env->pending_interrupts & P7_UNUSED_INTERRUPTS) == 0); 1733c8e1de2eSMatheus Ferst 1734022b7128SMatheus Ferst if (cs->halted) { 1735022b7128SMatheus Ferst /* LPCR[PECE] controls which interrupts can exit power-saving mode */ 1736022b7128SMatheus Ferst return p7_interrupt_powersave(env); 1737022b7128SMatheus Ferst } 1738022b7128SMatheus Ferst 1739bf303fb3SMatheus Ferst /* Machine check exception */ 1740bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 1741bf303fb3SMatheus Ferst return PPC_INTERRUPT_MCK; 1742bf303fb3SMatheus Ferst } 1743bf303fb3SMatheus Ferst 1744bf303fb3SMatheus Ferst /* Hypervisor decrementer exception */ 1745bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDECR) { 1746bf303fb3SMatheus Ferst /* LPCR will be clear when not supported so this will work */ 1747bf303fb3SMatheus Ferst bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE); 1748022b7128SMatheus Ferst if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) { 1749bf303fb3SMatheus Ferst /* HDEC clears on delivery */ 1750bf303fb3SMatheus Ferst return PPC_INTERRUPT_HDECR; 1751bf303fb3SMatheus Ferst } 1752bf303fb3SMatheus Ferst } 1753bf303fb3SMatheus Ferst 1754bf303fb3SMatheus Ferst /* External interrupt can ignore MSR:EE under some circumstances */ 1755bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EXT) { 1756bf303fb3SMatheus Ferst bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 1757bf303fb3SMatheus Ferst bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 1758bf303fb3SMatheus Ferst /* HEIC blocks delivery to the hypervisor */ 1759022b7128SMatheus Ferst if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) && 1760bf303fb3SMatheus Ferst !FIELD_EX64(env->msr, MSR, PR))) || 1761bf303fb3SMatheus Ferst (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) { 1762bf303fb3SMatheus Ferst return PPC_INTERRUPT_EXT; 1763bf303fb3SMatheus Ferst } 1764bf303fb3SMatheus Ferst } 1765022b7128SMatheus Ferst if (msr_ee != 0) { 1766bf303fb3SMatheus Ferst /* Decrementer exception */ 1767bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DECR) { 1768bf303fb3SMatheus Ferst return PPC_INTERRUPT_DECR; 1769bf303fb3SMatheus Ferst } 1770bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PERFM) { 1771bf303fb3SMatheus Ferst return PPC_INTERRUPT_PERFM; 1772bf303fb3SMatheus Ferst } 1773bf303fb3SMatheus Ferst } 1774bf303fb3SMatheus Ferst 1775bf303fb3SMatheus Ferst return 0; 1776bf303fb3SMatheus Ferst } 1777bf303fb3SMatheus Ferst 1778f6194fddSMatheus Ferst #define P8_UNUSED_INTERRUPTS \ 1779f6194fddSMatheus Ferst (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_HVIRT | \ 1780f6194fddSMatheus Ferst PPC_INTERRUPT_CEXT | PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | \ 1781f6194fddSMatheus Ferst PPC_INTERRUPT_FIT | PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM) 1782f6194fddSMatheus Ferst 17839c713713SMatheus Ferst static int p8_interrupt_powersave(CPUPPCState *env) 17849c713713SMatheus Ferst { 17859c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_EXT) && 17869c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE2)) { 17879c713713SMatheus Ferst return PPC_INTERRUPT_EXT; 17889c713713SMatheus Ferst } 17899c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DECR) && 17909c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE3)) { 17919c713713SMatheus Ferst return PPC_INTERRUPT_DECR; 17929c713713SMatheus Ferst } 17939c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_MCK) && 17949c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) { 17959c713713SMatheus Ferst return PPC_INTERRUPT_MCK; 17969c713713SMatheus Ferst } 17979c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HMI) && 17989c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) { 17999c713713SMatheus Ferst return PPC_INTERRUPT_HMI; 18009c713713SMatheus Ferst } 18019c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) && 18029c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE0)) { 18039c713713SMatheus Ferst return PPC_INTERRUPT_DOORBELL; 18049c713713SMatheus Ferst } 18059c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) && 18069c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE1)) { 18079c713713SMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 18089c713713SMatheus Ferst } 18099c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_RESET) { 18109c713713SMatheus Ferst return PPC_INTERRUPT_RESET; 18119c713713SMatheus Ferst } 18129c713713SMatheus Ferst return 0; 18139c713713SMatheus Ferst } 18149c713713SMatheus Ferst 1815a9899d42SMatheus Ferst static int p8_next_unmasked_interrupt(CPUPPCState *env) 1816a9899d42SMatheus Ferst { 181764a9b5eeSMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 181864a9b5eeSMatheus Ferst CPUState *cs = CPU(cpu); 181964a9b5eeSMatheus Ferst /* Ignore MSR[EE] when coming out of some power management states */ 182064a9b5eeSMatheus Ferst bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset; 1821a9899d42SMatheus Ferst 1822f6194fddSMatheus Ferst assert((env->pending_interrupts & P8_UNUSED_INTERRUPTS) == 0); 1823f6194fddSMatheus Ferst 182464a9b5eeSMatheus Ferst if (cs->halted) { 182564a9b5eeSMatheus Ferst /* LPCR[PECE] controls which interrupts can exit power-saving mode */ 182664a9b5eeSMatheus Ferst return p8_interrupt_powersave(env); 182764a9b5eeSMatheus Ferst } 182864a9b5eeSMatheus Ferst 1829a9899d42SMatheus Ferst /* Machine check exception */ 1830a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 1831a9899d42SMatheus Ferst return PPC_INTERRUPT_MCK; 1832a9899d42SMatheus Ferst } 1833a9899d42SMatheus Ferst 1834a9899d42SMatheus Ferst /* Hypervisor decrementer exception */ 1835a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDECR) { 1836a9899d42SMatheus Ferst /* LPCR will be clear when not supported so this will work */ 1837a9899d42SMatheus Ferst bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE); 183864a9b5eeSMatheus Ferst if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) { 1839a9899d42SMatheus Ferst /* HDEC clears on delivery */ 1840a9899d42SMatheus Ferst return PPC_INTERRUPT_HDECR; 1841a9899d42SMatheus Ferst } 1842a9899d42SMatheus Ferst } 1843a9899d42SMatheus Ferst 1844a9899d42SMatheus Ferst /* External interrupt can ignore MSR:EE under some circumstances */ 1845a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EXT) { 1846a9899d42SMatheus Ferst bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 1847a9899d42SMatheus Ferst bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 1848a9899d42SMatheus Ferst /* HEIC blocks delivery to the hypervisor */ 184964a9b5eeSMatheus Ferst if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) && 1850a9899d42SMatheus Ferst !FIELD_EX64(env->msr, MSR, PR))) || 1851a9899d42SMatheus Ferst (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) { 1852a9899d42SMatheus Ferst return PPC_INTERRUPT_EXT; 1853a9899d42SMatheus Ferst } 1854a9899d42SMatheus Ferst } 185564a9b5eeSMatheus Ferst if (msr_ee != 0) { 1856a9899d42SMatheus Ferst /* Decrementer exception */ 1857a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DECR) { 1858a9899d42SMatheus Ferst return PPC_INTERRUPT_DECR; 1859a9899d42SMatheus Ferst } 1860a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 1861a9899d42SMatheus Ferst return PPC_INTERRUPT_DOORBELL; 1862a9899d42SMatheus Ferst } 1863a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) { 1864a9899d42SMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 1865a9899d42SMatheus Ferst } 1866a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PERFM) { 1867a9899d42SMatheus Ferst return PPC_INTERRUPT_PERFM; 1868a9899d42SMatheus Ferst } 1869a9899d42SMatheus Ferst /* EBB exception */ 1870a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EBB) { 1871a9899d42SMatheus Ferst /* 1872a9899d42SMatheus Ferst * EBB exception must be taken in problem state and 1873a9899d42SMatheus Ferst * with BESCR_GE set. 1874a9899d42SMatheus Ferst */ 1875a9899d42SMatheus Ferst if (FIELD_EX64(env->msr, MSR, PR) && 1876a9899d42SMatheus Ferst (env->spr[SPR_BESCR] & BESCR_GE)) { 1877a9899d42SMatheus Ferst return PPC_INTERRUPT_EBB; 1878a9899d42SMatheus Ferst } 1879a9899d42SMatheus Ferst } 1880a9899d42SMatheus Ferst } 1881a9899d42SMatheus Ferst 1882a9899d42SMatheus Ferst return 0; 1883a9899d42SMatheus Ferst } 1884a9899d42SMatheus Ferst 1885b00e9a2fSMatheus Ferst #define P9_UNUSED_INTERRUPTS \ 1886b00e9a2fSMatheus Ferst (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT | \ 1887b00e9a2fSMatheus Ferst PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT | \ 1888b00e9a2fSMatheus Ferst PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM) 1889b00e9a2fSMatheus Ferst 18909c713713SMatheus Ferst static int p9_interrupt_powersave(CPUPPCState *env) 18919c713713SMatheus Ferst { 18929c713713SMatheus Ferst /* External Exception */ 18939c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_EXT) && 18949c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_EEE)) { 18959c713713SMatheus Ferst bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 18969c713713SMatheus Ferst if (!heic || !FIELD_EX64_HV(env->msr) || 18979c713713SMatheus Ferst FIELD_EX64(env->msr, MSR, PR)) { 18989c713713SMatheus Ferst return PPC_INTERRUPT_EXT; 18999c713713SMatheus Ferst } 19009c713713SMatheus Ferst } 19019c713713SMatheus Ferst /* Decrementer Exception */ 19029c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DECR) && 19039c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_DEE)) { 19049c713713SMatheus Ferst return PPC_INTERRUPT_DECR; 19059c713713SMatheus Ferst } 19069c713713SMatheus Ferst /* Machine Check or Hypervisor Maintenance Exception */ 19079c713713SMatheus Ferst if (env->spr[SPR_LPCR] & LPCR_OEE) { 19089c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 19099c713713SMatheus Ferst return PPC_INTERRUPT_MCK; 19109c713713SMatheus Ferst } 19119c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HMI) { 19129c713713SMatheus Ferst return PPC_INTERRUPT_HMI; 19139c713713SMatheus Ferst } 19149c713713SMatheus Ferst } 19159c713713SMatheus Ferst /* Privileged Doorbell Exception */ 19169c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) && 19179c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_PDEE)) { 19189c713713SMatheus Ferst return PPC_INTERRUPT_DOORBELL; 19199c713713SMatheus Ferst } 19209c713713SMatheus Ferst /* Hypervisor Doorbell Exception */ 19219c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) && 19229c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_HDEE)) { 19239c713713SMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 19249c713713SMatheus Ferst } 19259c713713SMatheus Ferst /* Hypervisor virtualization exception */ 19269c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HVIRT) && 19279c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_HVEE)) { 19289c713713SMatheus Ferst return PPC_INTERRUPT_HVIRT; 19299c713713SMatheus Ferst } 19309c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_RESET) { 19319c713713SMatheus Ferst return PPC_INTERRUPT_RESET; 19329c713713SMatheus Ferst } 19339c713713SMatheus Ferst return 0; 19349c713713SMatheus Ferst } 19359c713713SMatheus Ferst 19362dfecf01SMatheus Ferst static int p9_next_unmasked_interrupt(CPUPPCState *env) 19372dfecf01SMatheus Ferst { 193827796411SMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 193927796411SMatheus Ferst CPUState *cs = CPU(cpu); 194027796411SMatheus Ferst /* Ignore MSR[EE] when coming out of some power management states */ 194127796411SMatheus Ferst bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset; 19422dfecf01SMatheus Ferst 1943b00e9a2fSMatheus Ferst assert((env->pending_interrupts & P9_UNUSED_INTERRUPTS) == 0); 1944b00e9a2fSMatheus Ferst 194527796411SMatheus Ferst if (cs->halted) { 194627796411SMatheus Ferst if (env->spr[SPR_PSSCR] & PSSCR_EC) { 194727796411SMatheus Ferst /* 194827796411SMatheus Ferst * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can 194927796411SMatheus Ferst * wakeup the processor 195027796411SMatheus Ferst */ 195127796411SMatheus Ferst return p9_interrupt_powersave(env); 195227796411SMatheus Ferst } else { 195327796411SMatheus Ferst /* 195427796411SMatheus Ferst * When it's clear, any system-caused exception exits power-saving 195527796411SMatheus Ferst * mode, even the ones that gate on MSR[EE]. 195627796411SMatheus Ferst */ 195727796411SMatheus Ferst msr_ee = true; 195827796411SMatheus Ferst } 195927796411SMatheus Ferst } 196027796411SMatheus Ferst 19612dfecf01SMatheus Ferst /* Machine check exception */ 19622dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 19632dfecf01SMatheus Ferst return PPC_INTERRUPT_MCK; 19642dfecf01SMatheus Ferst } 19652dfecf01SMatheus Ferst 19662dfecf01SMatheus Ferst /* Hypervisor decrementer exception */ 19672dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDECR) { 19682dfecf01SMatheus Ferst /* LPCR will be clear when not supported so this will work */ 19692dfecf01SMatheus Ferst bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE); 197027796411SMatheus Ferst if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) { 19712dfecf01SMatheus Ferst /* HDEC clears on delivery */ 19722dfecf01SMatheus Ferst return PPC_INTERRUPT_HDECR; 19732dfecf01SMatheus Ferst } 19742dfecf01SMatheus Ferst } 19752dfecf01SMatheus Ferst 19762dfecf01SMatheus Ferst /* Hypervisor virtualization interrupt */ 19772dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) { 19782dfecf01SMatheus Ferst /* LPCR will be clear when not supported so this will work */ 19792dfecf01SMatheus Ferst bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE); 198027796411SMatheus Ferst if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hvice) { 19812dfecf01SMatheus Ferst return PPC_INTERRUPT_HVIRT; 19822dfecf01SMatheus Ferst } 19832dfecf01SMatheus Ferst } 19842dfecf01SMatheus Ferst 19852dfecf01SMatheus Ferst /* External interrupt can ignore MSR:EE under some circumstances */ 19862dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EXT) { 19872dfecf01SMatheus Ferst bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 19882dfecf01SMatheus Ferst bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 19892dfecf01SMatheus Ferst /* HEIC blocks delivery to the hypervisor */ 199027796411SMatheus Ferst if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) && 19912dfecf01SMatheus Ferst !FIELD_EX64(env->msr, MSR, PR))) || 19922dfecf01SMatheus Ferst (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) { 19932dfecf01SMatheus Ferst return PPC_INTERRUPT_EXT; 19942dfecf01SMatheus Ferst } 19952dfecf01SMatheus Ferst } 199627796411SMatheus Ferst if (msr_ee != 0) { 19972dfecf01SMatheus Ferst /* Decrementer exception */ 19982dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DECR) { 19992dfecf01SMatheus Ferst return PPC_INTERRUPT_DECR; 20002dfecf01SMatheus Ferst } 20012dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 20022dfecf01SMatheus Ferst return PPC_INTERRUPT_DOORBELL; 20032dfecf01SMatheus Ferst } 20042dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) { 20052dfecf01SMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 20062dfecf01SMatheus Ferst } 20072dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PERFM) { 20082dfecf01SMatheus Ferst return PPC_INTERRUPT_PERFM; 20092dfecf01SMatheus Ferst } 20102dfecf01SMatheus Ferst /* EBB exception */ 20112dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EBB) { 20122dfecf01SMatheus Ferst /* 20132dfecf01SMatheus Ferst * EBB exception must be taken in problem state and 20142dfecf01SMatheus Ferst * with BESCR_GE set. 20152dfecf01SMatheus Ferst */ 20162dfecf01SMatheus Ferst if (FIELD_EX64(env->msr, MSR, PR) && 20172dfecf01SMatheus Ferst (env->spr[SPR_BESCR] & BESCR_GE)) { 20182dfecf01SMatheus Ferst return PPC_INTERRUPT_EBB; 20192dfecf01SMatheus Ferst } 20202dfecf01SMatheus Ferst } 20212dfecf01SMatheus Ferst } 20222dfecf01SMatheus Ferst 20232dfecf01SMatheus Ferst return 0; 20242dfecf01SMatheus Ferst } 20252dfecf01SMatheus Ferst #endif 20262dfecf01SMatheus Ferst 2027ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt_generic(CPUPPCState *env) 2028c79c73f6SBlue Swirl { 20293621e2c9SBenjamin Herrenschmidt bool async_deliver; 2030259186a7SAndreas Färber 2031c79c73f6SBlue Swirl /* External reset */ 2032f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_RESET) { 2033de76b85cSMatheus Ferst return PPC_INTERRUPT_RESET; 2034c79c73f6SBlue Swirl } 2035c79c73f6SBlue Swirl /* Machine check exception */ 2036f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 2037de76b85cSMatheus Ferst return PPC_INTERRUPT_MCK; 2038c79c73f6SBlue Swirl } 2039c79c73f6SBlue Swirl #if 0 /* TODO */ 2040c79c73f6SBlue Swirl /* External debug exception */ 2041f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) { 2042de76b85cSMatheus Ferst return PPC_INTERRUPT_DEBUG; 2043c79c73f6SBlue Swirl } 2044c79c73f6SBlue Swirl #endif 20453621e2c9SBenjamin Herrenschmidt 20463621e2c9SBenjamin Herrenschmidt /* 20473621e2c9SBenjamin Herrenschmidt * For interrupts that gate on MSR:EE, we need to do something a 20483621e2c9SBenjamin Herrenschmidt * bit more subtle, as we need to let them through even when EE is 20493621e2c9SBenjamin Herrenschmidt * clear when coming out of some power management states (in order 20503621e2c9SBenjamin Herrenschmidt * for them to become a 0x100). 20513621e2c9SBenjamin Herrenschmidt */ 20520939b8f8SVíctor Colombo async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset; 20533621e2c9SBenjamin Herrenschmidt 2054c79c73f6SBlue Swirl /* Hypervisor decrementer exception */ 2055f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDECR) { 20564b236b62SBenjamin Herrenschmidt /* LPCR will be clear when not supported so this will work */ 20574b236b62SBenjamin Herrenschmidt bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE); 20589de754d3SVíctor Colombo if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) { 20594b236b62SBenjamin Herrenschmidt /* HDEC clears on delivery */ 2060de76b85cSMatheus Ferst return PPC_INTERRUPT_HDECR; 2061c79c73f6SBlue Swirl } 2062c79c73f6SBlue Swirl } 2063d8ce5fd6SBenjamin Herrenschmidt 2064d8ce5fd6SBenjamin Herrenschmidt /* Hypervisor virtualization interrupt */ 2065f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) { 2066d8ce5fd6SBenjamin Herrenschmidt /* LPCR will be clear when not supported so this will work */ 2067d8ce5fd6SBenjamin Herrenschmidt bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE); 20689de754d3SVíctor Colombo if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) { 2069de76b85cSMatheus Ferst return PPC_INTERRUPT_HVIRT; 2070d8ce5fd6SBenjamin Herrenschmidt } 2071d8ce5fd6SBenjamin Herrenschmidt } 2072d8ce5fd6SBenjamin Herrenschmidt 2073d8ce5fd6SBenjamin Herrenschmidt /* External interrupt can ignore MSR:EE under some circumstances */ 2074f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EXT) { 2075d1dbe37cSBenjamin Herrenschmidt bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 20766eebe6dcSBenjamin Herrenschmidt bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 20776eebe6dcSBenjamin Herrenschmidt /* HEIC blocks delivery to the hypervisor */ 20789de754d3SVíctor Colombo if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) && 2079d41ccf6eSVíctor Colombo !FIELD_EX64(env->msr, MSR, PR))) || 20809de754d3SVíctor Colombo (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) { 2081de76b85cSMatheus Ferst return PPC_INTERRUPT_EXT; 2082d1dbe37cSBenjamin Herrenschmidt } 2083d1dbe37cSBenjamin Herrenschmidt } 2084acc861c2SVíctor Colombo if (FIELD_EX64(env->msr, MSR, CE)) { 2085c79c73f6SBlue Swirl /* External critical interrupt */ 2086f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_CEXT) { 2087de76b85cSMatheus Ferst return PPC_INTERRUPT_CEXT; 2088c79c73f6SBlue Swirl } 2089c79c73f6SBlue Swirl } 20903621e2c9SBenjamin Herrenschmidt if (async_deliver != 0) { 2091c79c73f6SBlue Swirl /* Watchdog timer on embedded PowerPC */ 2092f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_WDT) { 2093de76b85cSMatheus Ferst return PPC_INTERRUPT_WDT; 2094c79c73f6SBlue Swirl } 2095f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) { 2096de76b85cSMatheus Ferst return PPC_INTERRUPT_CDOORBELL; 2097c79c73f6SBlue Swirl } 2098c79c73f6SBlue Swirl /* Fixed interval timer on embedded PowerPC */ 2099f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_FIT) { 2100de76b85cSMatheus Ferst return PPC_INTERRUPT_FIT; 2101c79c73f6SBlue Swirl } 2102c79c73f6SBlue Swirl /* Programmable interval timer on embedded PowerPC */ 2103f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PIT) { 2104de76b85cSMatheus Ferst return PPC_INTERRUPT_PIT; 2105c79c73f6SBlue Swirl } 2106c79c73f6SBlue Swirl /* Decrementer exception */ 2107f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DECR) { 2108de76b85cSMatheus Ferst return PPC_INTERRUPT_DECR; 2109c79c73f6SBlue Swirl } 2110f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 2111de76b85cSMatheus Ferst return PPC_INTERRUPT_DOORBELL; 2112c79c73f6SBlue Swirl } 2113f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) { 2114de76b85cSMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 21157af1e7b0SCédric Le Goater } 2116f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PERFM) { 2117de76b85cSMatheus Ferst return PPC_INTERRUPT_PERFM; 2118c79c73f6SBlue Swirl } 2119c79c73f6SBlue Swirl /* Thermal interrupt */ 2120f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_THERM) { 2121de76b85cSMatheus Ferst return PPC_INTERRUPT_THERM; 2122c79c73f6SBlue Swirl } 2123cb76bbc4SDaniel Henrique Barboza /* EBB exception */ 2124f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EBB) { 2125cb76bbc4SDaniel Henrique Barboza /* 2126cb76bbc4SDaniel Henrique Barboza * EBB exception must be taken in problem state and 2127cb76bbc4SDaniel Henrique Barboza * with BESCR_GE set. 2128cb76bbc4SDaniel Henrique Barboza */ 2129d41ccf6eSVíctor Colombo if (FIELD_EX64(env->msr, MSR, PR) && 2130d41ccf6eSVíctor Colombo (env->spr[SPR_BESCR] & BESCR_GE)) { 2131de76b85cSMatheus Ferst return PPC_INTERRUPT_EBB; 2132de76b85cSMatheus Ferst } 2133de76b85cSMatheus Ferst } 2134de76b85cSMatheus Ferst } 2135cb76bbc4SDaniel Henrique Barboza 2136de76b85cSMatheus Ferst return 0; 2137de76b85cSMatheus Ferst } 2138de76b85cSMatheus Ferst 2139ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt(CPUPPCState *env) 2140ba2898f7SMatheus Ferst { 2141ba2898f7SMatheus Ferst switch (env->excp_model) { 21422dfecf01SMatheus Ferst #if defined(TARGET_PPC64) 2143bf303fb3SMatheus Ferst case POWERPC_EXCP_POWER7: 2144bf303fb3SMatheus Ferst return p7_next_unmasked_interrupt(env); 2145a9899d42SMatheus Ferst case POWERPC_EXCP_POWER8: 2146a9899d42SMatheus Ferst return p8_next_unmasked_interrupt(env); 21472dfecf01SMatheus Ferst case POWERPC_EXCP_POWER9: 21482dfecf01SMatheus Ferst case POWERPC_EXCP_POWER10: 21492dfecf01SMatheus Ferst return p9_next_unmasked_interrupt(env); 21502dfecf01SMatheus Ferst #endif 2151ba2898f7SMatheus Ferst default: 2152ba2898f7SMatheus Ferst return ppc_next_unmasked_interrupt_generic(env); 2153ba2898f7SMatheus Ferst } 2154ba2898f7SMatheus Ferst } 2155ba2898f7SMatheus Ferst 21562fdedcbcSMatheus Ferst /* 21572fdedcbcSMatheus Ferst * Sets CPU_INTERRUPT_HARD if there is at least one unmasked interrupt to be 21582fdedcbcSMatheus Ferst * delivered and clears CPU_INTERRUPT_HARD otherwise. 21592fdedcbcSMatheus Ferst * 21602fdedcbcSMatheus Ferst * This method is called by ppc_set_interrupt when an interrupt is raised or 21612fdedcbcSMatheus Ferst * lowered, and should also be called whenever an interrupt masking condition 21622fdedcbcSMatheus Ferst * is changed, e.g.: 21632fdedcbcSMatheus Ferst * - When relevant bits of MSR are altered, like EE, HV, PR, etc.; 21642fdedcbcSMatheus Ferst * - When relevant bits of LPCR are altered, like PECE, HDICE, HVICE, etc.; 21652fdedcbcSMatheus Ferst * - When PSSCR[EC] or env->resume_as_sreset are changed; 21662fdedcbcSMatheus Ferst * - When cs->halted is changed and the CPU has a different interrupt masking 21672fdedcbcSMatheus Ferst * logic in power-saving mode (e.g., POWER7/8/9/10); 21682fdedcbcSMatheus Ferst */ 21692fdedcbcSMatheus Ferst void ppc_maybe_interrupt(CPUPPCState *env) 21702fdedcbcSMatheus Ferst { 21712fdedcbcSMatheus Ferst CPUState *cs = env_cpu(env); 21722fc4f9f3SRichard Henderson QEMU_IOTHREAD_LOCK_GUARD(); 21732fdedcbcSMatheus Ferst 21742fdedcbcSMatheus Ferst if (ppc_next_unmasked_interrupt(env)) { 21752fdedcbcSMatheus Ferst cpu_interrupt(cs, CPU_INTERRUPT_HARD); 21762fdedcbcSMatheus Ferst } else { 21772fdedcbcSMatheus Ferst cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); 21782fdedcbcSMatheus Ferst } 21792fdedcbcSMatheus Ferst } 21802fdedcbcSMatheus Ferst 21813654e238SMatheus Ferst #if defined(TARGET_PPC64) 2182d93a4856SMatheus Ferst static void p7_deliver_interrupt(CPUPPCState *env, int interrupt) 2183d93a4856SMatheus Ferst { 2184d93a4856SMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 2185d93a4856SMatheus Ferst CPUState *cs = env_cpu(env); 2186d93a4856SMatheus Ferst 2187d93a4856SMatheus Ferst switch (interrupt) { 2188d93a4856SMatheus Ferst case PPC_INTERRUPT_MCK: /* Machine check exception */ 2189d93a4856SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_MCK; 2190d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_MCHECK); 2191d93a4856SMatheus Ferst break; 2192d93a4856SMatheus Ferst 2193d93a4856SMatheus Ferst case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */ 2194d93a4856SMatheus Ferst /* HDEC clears on delivery */ 2195d93a4856SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDECR; 2196d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HDECR); 2197d93a4856SMatheus Ferst break; 2198d93a4856SMatheus Ferst 2199d93a4856SMatheus Ferst case PPC_INTERRUPT_EXT: 2200d93a4856SMatheus Ferst if (books_vhyp_promotes_external_to_hvirt(cpu)) { 2201d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 2202d93a4856SMatheus Ferst } else { 2203d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL); 2204d93a4856SMatheus Ferst } 2205d93a4856SMatheus Ferst break; 2206d93a4856SMatheus Ferst 2207d93a4856SMatheus Ferst case PPC_INTERRUPT_DECR: /* Decrementer exception */ 2208d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DECR); 2209d93a4856SMatheus Ferst break; 2210d93a4856SMatheus Ferst case PPC_INTERRUPT_PERFM: 2211d93a4856SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PERFM; 2212d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM); 2213d93a4856SMatheus Ferst break; 2214d93a4856SMatheus Ferst case 0: 2215d93a4856SMatheus Ferst /* 2216d93a4856SMatheus Ferst * This is a bug ! It means that has_work took us out of halt without 2217d93a4856SMatheus Ferst * anything to deliver while in a PM state that requires getting 2218d93a4856SMatheus Ferst * out via a 0x100 2219d93a4856SMatheus Ferst * 2220d93a4856SMatheus Ferst * This means we will incorrectly execute past the power management 2221d93a4856SMatheus Ferst * instruction instead of triggering a reset. 2222d93a4856SMatheus Ferst * 2223d93a4856SMatheus Ferst * It generally means a discrepancy between the wakeup conditions in the 2224d93a4856SMatheus Ferst * processor has_work implementation and the logic in this function. 2225d93a4856SMatheus Ferst */ 2226d93a4856SMatheus Ferst assert(!env->resume_as_sreset); 2227d93a4856SMatheus Ferst break; 2228d93a4856SMatheus Ferst default: 2229d93a4856SMatheus Ferst cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt); 2230d93a4856SMatheus Ferst } 2231d93a4856SMatheus Ferst } 2232d93a4856SMatheus Ferst 22336527e757SMatheus Ferst static void p8_deliver_interrupt(CPUPPCState *env, int interrupt) 22346527e757SMatheus Ferst { 22356527e757SMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 22366527e757SMatheus Ferst CPUState *cs = env_cpu(env); 22376527e757SMatheus Ferst 22386527e757SMatheus Ferst switch (interrupt) { 22396527e757SMatheus Ferst case PPC_INTERRUPT_MCK: /* Machine check exception */ 22406527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_MCK; 22416527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_MCHECK); 22426527e757SMatheus Ferst break; 22436527e757SMatheus Ferst 22446527e757SMatheus Ferst case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */ 22456527e757SMatheus Ferst /* HDEC clears on delivery */ 22466527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDECR; 22476527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HDECR); 22486527e757SMatheus Ferst break; 22496527e757SMatheus Ferst 22506527e757SMatheus Ferst case PPC_INTERRUPT_EXT: 22516527e757SMatheus Ferst if (books_vhyp_promotes_external_to_hvirt(cpu)) { 22526527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 22536527e757SMatheus Ferst } else { 22546527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL); 22556527e757SMatheus Ferst } 22566527e757SMatheus Ferst break; 22576527e757SMatheus Ferst 22586527e757SMatheus Ferst case PPC_INTERRUPT_DECR: /* Decrementer exception */ 22596527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DECR); 22606527e757SMatheus Ferst break; 22616527e757SMatheus Ferst case PPC_INTERRUPT_DOORBELL: 22626527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL; 22636527e757SMatheus Ferst if (is_book3s_arch2x(env)) { 22646527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR); 22656527e757SMatheus Ferst } else { 22666527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DOORI); 22676527e757SMatheus Ferst } 22686527e757SMatheus Ferst break; 22696527e757SMatheus Ferst case PPC_INTERRUPT_HDOORBELL: 22706527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL; 22716527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV); 22726527e757SMatheus Ferst break; 22736527e757SMatheus Ferst case PPC_INTERRUPT_PERFM: 22746527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PERFM; 22756527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM); 22766527e757SMatheus Ferst break; 22776527e757SMatheus Ferst case PPC_INTERRUPT_EBB: /* EBB exception */ 22786527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_EBB; 22796527e757SMatheus Ferst if (env->spr[SPR_BESCR] & BESCR_PMEO) { 22806527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB); 22816527e757SMatheus Ferst } else if (env->spr[SPR_BESCR] & BESCR_EEO) { 22826527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB); 22836527e757SMatheus Ferst } 22846527e757SMatheus Ferst break; 22856527e757SMatheus Ferst case 0: 22866527e757SMatheus Ferst /* 22876527e757SMatheus Ferst * This is a bug ! It means that has_work took us out of halt without 22886527e757SMatheus Ferst * anything to deliver while in a PM state that requires getting 22896527e757SMatheus Ferst * out via a 0x100 22906527e757SMatheus Ferst * 22916527e757SMatheus Ferst * This means we will incorrectly execute past the power management 22926527e757SMatheus Ferst * instruction instead of triggering a reset. 22936527e757SMatheus Ferst * 22946527e757SMatheus Ferst * It generally means a discrepancy between the wakeup conditions in the 22956527e757SMatheus Ferst * processor has_work implementation and the logic in this function. 22966527e757SMatheus Ferst */ 22976527e757SMatheus Ferst assert(!env->resume_as_sreset); 22986527e757SMatheus Ferst break; 22996527e757SMatheus Ferst default: 23006527e757SMatheus Ferst cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt); 23016527e757SMatheus Ferst } 23026527e757SMatheus Ferst } 23036527e757SMatheus Ferst 23043654e238SMatheus Ferst static void p9_deliver_interrupt(CPUPPCState *env, int interrupt) 23053654e238SMatheus Ferst { 23063654e238SMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 23073654e238SMatheus Ferst CPUState *cs = env_cpu(env); 23083654e238SMatheus Ferst 230927796411SMatheus Ferst if (cs->halted && !(env->spr[SPR_PSSCR] & PSSCR_EC) && 231027796411SMatheus Ferst !FIELD_EX64(env->msr, MSR, EE)) { 231127796411SMatheus Ferst /* 231227796411SMatheus Ferst * A pending interrupt took us out of power-saving, but MSR[EE] says 231327796411SMatheus Ferst * that we should return to NIP+4 instead of delivering it. 231427796411SMatheus Ferst */ 231527796411SMatheus Ferst return; 231627796411SMatheus Ferst } 231727796411SMatheus Ferst 23183654e238SMatheus Ferst switch (interrupt) { 23193654e238SMatheus Ferst case PPC_INTERRUPT_MCK: /* Machine check exception */ 23203654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_MCK; 23213654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_MCHECK); 23223654e238SMatheus Ferst break; 23233654e238SMatheus Ferst 23243654e238SMatheus Ferst case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */ 23253654e238SMatheus Ferst /* HDEC clears on delivery */ 23263654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDECR; 23273654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HDECR); 23283654e238SMatheus Ferst break; 23293654e238SMatheus Ferst case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */ 23303654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 23313654e238SMatheus Ferst break; 23323654e238SMatheus Ferst 23333654e238SMatheus Ferst case PPC_INTERRUPT_EXT: 23343654e238SMatheus Ferst if (books_vhyp_promotes_external_to_hvirt(cpu)) { 23353654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 23363654e238SMatheus Ferst } else { 23373654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL); 23383654e238SMatheus Ferst } 23393654e238SMatheus Ferst break; 23403654e238SMatheus Ferst 23413654e238SMatheus Ferst case PPC_INTERRUPT_DECR: /* Decrementer exception */ 23423654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DECR); 23433654e238SMatheus Ferst break; 23443654e238SMatheus Ferst case PPC_INTERRUPT_DOORBELL: 23453654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL; 23463654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR); 23473654e238SMatheus Ferst break; 23483654e238SMatheus Ferst case PPC_INTERRUPT_HDOORBELL: 23493654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL; 23503654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV); 23513654e238SMatheus Ferst break; 23523654e238SMatheus Ferst case PPC_INTERRUPT_PERFM: 23533654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PERFM; 23543654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM); 23553654e238SMatheus Ferst break; 23563654e238SMatheus Ferst case PPC_INTERRUPT_EBB: /* EBB exception */ 23573654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_EBB; 23583654e238SMatheus Ferst if (env->spr[SPR_BESCR] & BESCR_PMEO) { 23593654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB); 23603654e238SMatheus Ferst } else if (env->spr[SPR_BESCR] & BESCR_EEO) { 23613654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB); 23623654e238SMatheus Ferst } 23633654e238SMatheus Ferst break; 23643654e238SMatheus Ferst case 0: 23653654e238SMatheus Ferst /* 23663654e238SMatheus Ferst * This is a bug ! It means that has_work took us out of halt without 23673654e238SMatheus Ferst * anything to deliver while in a PM state that requires getting 23683654e238SMatheus Ferst * out via a 0x100 23693654e238SMatheus Ferst * 23703654e238SMatheus Ferst * This means we will incorrectly execute past the power management 23713654e238SMatheus Ferst * instruction instead of triggering a reset. 23723654e238SMatheus Ferst * 23733654e238SMatheus Ferst * It generally means a discrepancy between the wakeup conditions in the 23743654e238SMatheus Ferst * processor has_work implementation and the logic in this function. 23753654e238SMatheus Ferst */ 23763654e238SMatheus Ferst assert(!env->resume_as_sreset); 23773654e238SMatheus Ferst break; 23783654e238SMatheus Ferst default: 23793654e238SMatheus Ferst cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt); 23803654e238SMatheus Ferst } 23813654e238SMatheus Ferst } 23823654e238SMatheus Ferst #endif 23833654e238SMatheus Ferst 2384ba2898f7SMatheus Ferst static void ppc_deliver_interrupt_generic(CPUPPCState *env, int interrupt) 2385de76b85cSMatheus Ferst { 2386de76b85cSMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 2387de76b85cSMatheus Ferst CPUState *cs = env_cpu(env); 2388de76b85cSMatheus Ferst 2389de76b85cSMatheus Ferst switch (interrupt) { 2390de76b85cSMatheus Ferst case PPC_INTERRUPT_RESET: /* External reset */ 2391de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_RESET; 2392de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_RESET); 2393de76b85cSMatheus Ferst break; 2394de76b85cSMatheus Ferst case PPC_INTERRUPT_MCK: /* Machine check exception */ 2395de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_MCK; 2396de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_MCHECK); 2397de76b85cSMatheus Ferst break; 2398de76b85cSMatheus Ferst 2399de76b85cSMatheus Ferst case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */ 2400de76b85cSMatheus Ferst /* HDEC clears on delivery */ 2401de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDECR; 2402de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HDECR); 2403de76b85cSMatheus Ferst break; 2404de76b85cSMatheus Ferst case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */ 2405de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 2406de76b85cSMatheus Ferst break; 2407de76b85cSMatheus Ferst 2408de76b85cSMatheus Ferst case PPC_INTERRUPT_EXT: 2409de76b85cSMatheus Ferst if (books_vhyp_promotes_external_to_hvirt(cpu)) { 2410de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 2411de76b85cSMatheus Ferst } else { 2412de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL); 2413de76b85cSMatheus Ferst } 2414de76b85cSMatheus Ferst break; 2415de76b85cSMatheus Ferst case PPC_INTERRUPT_CEXT: /* External critical interrupt */ 2416de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_CRITICAL); 2417de76b85cSMatheus Ferst break; 2418de76b85cSMatheus Ferst 2419de76b85cSMatheus Ferst case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */ 2420de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_WDT; 2421de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_WDT); 2422de76b85cSMatheus Ferst break; 2423de76b85cSMatheus Ferst case PPC_INTERRUPT_CDOORBELL: 2424de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL; 2425de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DOORCI); 2426de76b85cSMatheus Ferst break; 2427de76b85cSMatheus Ferst case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */ 2428de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_FIT; 2429de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_FIT); 2430de76b85cSMatheus Ferst break; 2431de76b85cSMatheus Ferst case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */ 2432de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PIT; 2433de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PIT); 2434de76b85cSMatheus Ferst break; 2435de76b85cSMatheus Ferst case PPC_INTERRUPT_DECR: /* Decrementer exception */ 2436de76b85cSMatheus Ferst if (ppc_decr_clear_on_delivery(env)) { 2437de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_DECR; 2438de76b85cSMatheus Ferst } 2439de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DECR); 2440de76b85cSMatheus Ferst break; 2441de76b85cSMatheus Ferst case PPC_INTERRUPT_DOORBELL: 2442de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL; 2443de76b85cSMatheus Ferst if (is_book3s_arch2x(env)) { 2444de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR); 2445de76b85cSMatheus Ferst } else { 2446de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DOORI); 2447de76b85cSMatheus Ferst } 2448de76b85cSMatheus Ferst break; 2449de76b85cSMatheus Ferst case PPC_INTERRUPT_HDOORBELL: 2450de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL; 2451de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV); 2452de76b85cSMatheus Ferst break; 2453de76b85cSMatheus Ferst case PPC_INTERRUPT_PERFM: 2454de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PERFM; 2455de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM); 2456de76b85cSMatheus Ferst break; 2457de76b85cSMatheus Ferst case PPC_INTERRUPT_THERM: /* Thermal interrupt */ 2458de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_THERM; 2459de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_THERM); 2460de76b85cSMatheus Ferst break; 2461de76b85cSMatheus Ferst case PPC_INTERRUPT_EBB: /* EBB exception */ 2462de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_EBB; 2463cb76bbc4SDaniel Henrique Barboza if (env->spr[SPR_BESCR] & BESCR_PMEO) { 2464cb76bbc4SDaniel Henrique Barboza powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB); 2465cb76bbc4SDaniel Henrique Barboza } else if (env->spr[SPR_BESCR] & BESCR_EEO) { 2466cb76bbc4SDaniel Henrique Barboza powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB); 2467cb76bbc4SDaniel Henrique Barboza } 2468de76b85cSMatheus Ferst break; 2469de76b85cSMatheus Ferst case 0: 2470f8154fd2SBenjamin Herrenschmidt /* 2471f8154fd2SBenjamin Herrenschmidt * This is a bug ! It means that has_work took us out of halt without 2472f8154fd2SBenjamin Herrenschmidt * anything to deliver while in a PM state that requires getting 2473f8154fd2SBenjamin Herrenschmidt * out via a 0x100 2474f8154fd2SBenjamin Herrenschmidt * 2475f8154fd2SBenjamin Herrenschmidt * This means we will incorrectly execute past the power management 2476f8154fd2SBenjamin Herrenschmidt * instruction instead of triggering a reset. 2477f8154fd2SBenjamin Herrenschmidt * 2478136fbf65Szhaolichang * It generally means a discrepancy between the wakeup conditions in the 2479f8154fd2SBenjamin Herrenschmidt * processor has_work implementation and the logic in this function. 2480f8154fd2SBenjamin Herrenschmidt */ 2481de76b85cSMatheus Ferst assert(!env->resume_as_sreset); 2482de76b85cSMatheus Ferst break; 2483de76b85cSMatheus Ferst default: 2484de76b85cSMatheus Ferst cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt); 2485f8154fd2SBenjamin Herrenschmidt } 2486c79c73f6SBlue Swirl } 248734316482SAlexey Kardashevskiy 2488ba2898f7SMatheus Ferst static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt) 2489ba2898f7SMatheus Ferst { 2490ba2898f7SMatheus Ferst switch (env->excp_model) { 24913654e238SMatheus Ferst #if defined(TARGET_PPC64) 2492d93a4856SMatheus Ferst case POWERPC_EXCP_POWER7: 2493d93a4856SMatheus Ferst p7_deliver_interrupt(env, interrupt); 2494d93a4856SMatheus Ferst break; 24956527e757SMatheus Ferst case POWERPC_EXCP_POWER8: 24966527e757SMatheus Ferst p8_deliver_interrupt(env, interrupt); 24976527e757SMatheus Ferst break; 24983654e238SMatheus Ferst case POWERPC_EXCP_POWER9: 24993654e238SMatheus Ferst case POWERPC_EXCP_POWER10: 25003654e238SMatheus Ferst p9_deliver_interrupt(env, interrupt); 25013654e238SMatheus Ferst break; 25023654e238SMatheus Ferst #endif 2503ba2898f7SMatheus Ferst default: 2504ba2898f7SMatheus Ferst ppc_deliver_interrupt_generic(env, interrupt); 2505ba2898f7SMatheus Ferst } 2506ba2898f7SMatheus Ferst } 2507ba2898f7SMatheus Ferst 2508b5b7f391SNicholas Piggin void ppc_cpu_do_system_reset(CPUState *cs) 250934316482SAlexey Kardashevskiy { 251034316482SAlexey Kardashevskiy PowerPCCPU *cpu = POWERPC_CPU(cs); 251134316482SAlexey Kardashevskiy 251293130c84SFabiano Rosas powerpc_excp(cpu, POWERPC_EXCP_RESET); 251334316482SAlexey Kardashevskiy } 2514ad77c6caSNicholas Piggin 2515ad77c6caSNicholas Piggin void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector) 2516ad77c6caSNicholas Piggin { 2517ad77c6caSNicholas Piggin PowerPCCPU *cpu = POWERPC_CPU(cs); 2518ad77c6caSNicholas Piggin CPUPPCState *env = &cpu->env; 2519ad77c6caSNicholas Piggin target_ulong msr = 0; 2520ad77c6caSNicholas Piggin 2521ad77c6caSNicholas Piggin /* 2522ad77c6caSNicholas Piggin * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already 2523ad77c6caSNicholas Piggin * been set by KVM. 2524ad77c6caSNicholas Piggin */ 2525ad77c6caSNicholas Piggin msr = (1ULL << MSR_ME); 2526ad77c6caSNicholas Piggin msr |= env->msr & (1ULL << MSR_SF); 2527516fc103SFabiano Rosas if (ppc_interrupts_little_endian(cpu, false)) { 2528ad77c6caSNicholas Piggin msr |= (1ULL << MSR_LE); 2529ad77c6caSNicholas Piggin } 2530ad77c6caSNicholas Piggin 25317cebc5dbSNicholas Piggin /* Anything for nested required here? MSR[HV] bit? */ 25327cebc5dbSNicholas Piggin 2533ad77c6caSNicholas Piggin powerpc_set_excp_state(cpu, vector, msr); 2534ad77c6caSNicholas Piggin } 2535c79c73f6SBlue Swirl 2536458dd766SRichard Henderson bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request) 2537458dd766SRichard Henderson { 2538458dd766SRichard Henderson PowerPCCPU *cpu = POWERPC_CPU(cs); 2539458dd766SRichard Henderson CPUPPCState *env = &cpu->env; 2540de76b85cSMatheus Ferst int interrupt; 2541458dd766SRichard Henderson 2542de76b85cSMatheus Ferst if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) { 2543de76b85cSMatheus Ferst return false; 2544de76b85cSMatheus Ferst } 2545de76b85cSMatheus Ferst 2546de76b85cSMatheus Ferst interrupt = ppc_next_unmasked_interrupt(env); 2547de76b85cSMatheus Ferst if (interrupt == 0) { 2548de76b85cSMatheus Ferst return false; 2549de76b85cSMatheus Ferst } 2550de76b85cSMatheus Ferst 2551de76b85cSMatheus Ferst ppc_deliver_interrupt(env, interrupt); 2552458dd766SRichard Henderson if (env->pending_interrupts == 0) { 2553de76b85cSMatheus Ferst cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); 2554458dd766SRichard Henderson } 2555458dd766SRichard Henderson return true; 2556458dd766SRichard Henderson } 2557458dd766SRichard Henderson 2558f725245cSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */ 2559f725245cSPhilippe Mathieu-Daudé 2560ad71ed68SBlue Swirl /*****************************************************************************/ 2561ad71ed68SBlue Swirl /* Exceptions processing helpers */ 2562ad71ed68SBlue Swirl 2563db789c6cSBenjamin Herrenschmidt void raise_exception_err_ra(CPUPPCState *env, uint32_t exception, 2564db789c6cSBenjamin Herrenschmidt uint32_t error_code, uintptr_t raddr) 2565ad71ed68SBlue Swirl { 2566db70b311SRichard Henderson CPUState *cs = env_cpu(env); 256727103424SAndreas Färber 256827103424SAndreas Färber cs->exception_index = exception; 2569ad71ed68SBlue Swirl env->error_code = error_code; 2570db789c6cSBenjamin Herrenschmidt cpu_loop_exit_restore(cs, raddr); 2571db789c6cSBenjamin Herrenschmidt } 2572db789c6cSBenjamin Herrenschmidt 2573db789c6cSBenjamin Herrenschmidt void raise_exception_err(CPUPPCState *env, uint32_t exception, 2574db789c6cSBenjamin Herrenschmidt uint32_t error_code) 2575db789c6cSBenjamin Herrenschmidt { 2576db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, error_code, 0); 2577db789c6cSBenjamin Herrenschmidt } 2578db789c6cSBenjamin Herrenschmidt 2579db789c6cSBenjamin Herrenschmidt void raise_exception(CPUPPCState *env, uint32_t exception) 2580db789c6cSBenjamin Herrenschmidt { 2581db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, 0, 0); 2582db789c6cSBenjamin Herrenschmidt } 2583db789c6cSBenjamin Herrenschmidt 2584db789c6cSBenjamin Herrenschmidt void raise_exception_ra(CPUPPCState *env, uint32_t exception, 2585db789c6cSBenjamin Herrenschmidt uintptr_t raddr) 2586db789c6cSBenjamin Herrenschmidt { 2587db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, 0, raddr); 2588db789c6cSBenjamin Herrenschmidt } 2589db789c6cSBenjamin Herrenschmidt 25902b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 2591db789c6cSBenjamin Herrenschmidt void helper_raise_exception_err(CPUPPCState *env, uint32_t exception, 2592db789c6cSBenjamin Herrenschmidt uint32_t error_code) 2593db789c6cSBenjamin Herrenschmidt { 2594db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, error_code, 0); 2595ad71ed68SBlue Swirl } 2596ad71ed68SBlue Swirl 2597e5f17ac6SBlue Swirl void helper_raise_exception(CPUPPCState *env, uint32_t exception) 2598ad71ed68SBlue Swirl { 2599db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, 0, 0); 2600ad71ed68SBlue Swirl } 26012b44e219SBruno Larsen (billionai) #endif 2602ad71ed68SBlue Swirl 2603ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY) 26042b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 2605e5f17ac6SBlue Swirl void helper_store_msr(CPUPPCState *env, target_ulong val) 2606ad71ed68SBlue Swirl { 2607db789c6cSBenjamin Herrenschmidt uint32_t excp = hreg_store_msr(env, val, 0); 2608259186a7SAndreas Färber 2609db789c6cSBenjamin Herrenschmidt if (excp != 0) { 2610db70b311SRichard Henderson CPUState *cs = env_cpu(env); 2611044897efSRichard Purdie cpu_interrupt_exittb(cs); 2612db789c6cSBenjamin Herrenschmidt raise_exception(env, excp); 2613ad71ed68SBlue Swirl } 2614ad71ed68SBlue Swirl } 2615ad71ed68SBlue Swirl 26162fdedcbcSMatheus Ferst void helper_ppc_maybe_interrupt(CPUPPCState *env) 26172fdedcbcSMatheus Ferst { 26182fdedcbcSMatheus Ferst ppc_maybe_interrupt(env); 26192fdedcbcSMatheus Ferst } 26202fdedcbcSMatheus Ferst 26217778a575SBenjamin Herrenschmidt #if defined(TARGET_PPC64) 2622f43520e5SRichard Henderson void helper_scv(CPUPPCState *env, uint32_t lev) 2623f43520e5SRichard Henderson { 2624f43520e5SRichard Henderson if (env->spr[SPR_FSCR] & (1ull << FSCR_SCV)) { 2625f43520e5SRichard Henderson raise_exception_err(env, POWERPC_EXCP_SYSCALL_VECTORED, lev); 2626f43520e5SRichard Henderson } else { 2627f43520e5SRichard Henderson raise_exception_err(env, POWERPC_EXCP_FU, FSCR_IC_SCV); 2628f43520e5SRichard Henderson } 2629f43520e5SRichard Henderson } 2630f43520e5SRichard Henderson 263107e4804fSCédric Le Goater void helper_pminsn(CPUPPCState *env, uint32_t insn) 26327778a575SBenjamin Herrenschmidt { 26337778a575SBenjamin Herrenschmidt CPUState *cs; 26347778a575SBenjamin Herrenschmidt 2635db70b311SRichard Henderson cs = env_cpu(env); 26367778a575SBenjamin Herrenschmidt cs->halted = 1; 26377778a575SBenjamin Herrenschmidt 26383621e2c9SBenjamin Herrenschmidt /* Condition for waking up at 0x100 */ 26391e7fd61dSBenjamin Herrenschmidt env->resume_as_sreset = (insn != PPC_PM_STOP) || 264021c0d66aSBenjamin Herrenschmidt (env->spr[SPR_PSSCR] & PSSCR_EC); 26412fdedcbcSMatheus Ferst 26422fdedcbcSMatheus Ferst ppc_maybe_interrupt(env); 26437778a575SBenjamin Herrenschmidt } 26447778a575SBenjamin Herrenschmidt #endif /* defined(TARGET_PPC64) */ 26457778a575SBenjamin Herrenschmidt 264662e79ef9SCédric Le Goater static void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr) 2647ad71ed68SBlue Swirl { 2648db70b311SRichard Henderson CPUState *cs = env_cpu(env); 2649259186a7SAndreas Färber 2650a2e71b28SBenjamin Herrenschmidt /* MSR:POW cannot be set by any form of rfi */ 2651a2e71b28SBenjamin Herrenschmidt msr &= ~(1ULL << MSR_POW); 2652a2e71b28SBenjamin Herrenschmidt 26535aad0457SChristophe Leroy /* MSR:TGPR cannot be set by any form of rfi */ 26545aad0457SChristophe Leroy if (env->flags & POWERPC_FLAG_TGPR) 26555aad0457SChristophe Leroy msr &= ~(1ULL << MSR_TGPR); 26565aad0457SChristophe Leroy 2657ad71ed68SBlue Swirl #if defined(TARGET_PPC64) 2658a2e71b28SBenjamin Herrenschmidt /* Switching to 32-bit ? Crop the nip */ 2659a2e71b28SBenjamin Herrenschmidt if (!msr_is_64bit(env, msr)) { 2660ad71ed68SBlue Swirl nip = (uint32_t)nip; 2661ad71ed68SBlue Swirl } 2662ad71ed68SBlue Swirl #else 2663ad71ed68SBlue Swirl nip = (uint32_t)nip; 2664ad71ed68SBlue Swirl #endif 2665ad71ed68SBlue Swirl /* XXX: beware: this is false if VLE is supported */ 2666ad71ed68SBlue Swirl env->nip = nip & ~((target_ulong)0x00000003); 2667ad71ed68SBlue Swirl hreg_store_msr(env, msr, 1); 26682eb1ef73SCédric Le Goater trace_ppc_excp_rfi(env->nip, env->msr); 266947733729SDavid Gibson /* 267047733729SDavid Gibson * No need to raise an exception here, as rfi is always the last 267147733729SDavid Gibson * insn of a TB 2672ad71ed68SBlue Swirl */ 2673044897efSRichard Purdie cpu_interrupt_exittb(cs); 2674a8b73734SNikunj A Dadhania /* Reset the reservation */ 2675a8b73734SNikunj A Dadhania env->reserve_addr = -1; 2676a8b73734SNikunj A Dadhania 2677cd0c6f47SBenjamin Herrenschmidt /* Context synchronizing: check if TCG TLB needs flush */ 2678e3cffe6fSNikunj A Dadhania check_tlb_flush(env, false); 2679ad71ed68SBlue Swirl } 2680ad71ed68SBlue Swirl 2681e5f17ac6SBlue Swirl void helper_rfi(CPUPPCState *env) 2682ad71ed68SBlue Swirl { 2683a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful); 2684a1bb7384SScott Wood } 2685ad71ed68SBlue Swirl 2686ad71ed68SBlue Swirl #if defined(TARGET_PPC64) 2687e5f17ac6SBlue Swirl void helper_rfid(CPUPPCState *env) 2688ad71ed68SBlue Swirl { 268947733729SDavid Gibson /* 2690136fbf65Szhaolichang * The architecture defines a number of rules for which bits can 269147733729SDavid Gibson * change but in practice, we handle this in hreg_store_msr() 2692a2e71b28SBenjamin Herrenschmidt * which will be called by do_rfi(), so there is no need to filter 2693a2e71b28SBenjamin Herrenschmidt * here 2694a2e71b28SBenjamin Herrenschmidt */ 2695a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]); 2696ad71ed68SBlue Swirl } 2697ad71ed68SBlue Swirl 26983c89b8d6SNicholas Piggin void helper_rfscv(CPUPPCState *env) 26993c89b8d6SNicholas Piggin { 27003c89b8d6SNicholas Piggin do_rfi(env, env->lr, env->ctr); 27013c89b8d6SNicholas Piggin } 27023c89b8d6SNicholas Piggin 2703e5f17ac6SBlue Swirl void helper_hrfid(CPUPPCState *env) 2704ad71ed68SBlue Swirl { 2705a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]); 2706ad71ed68SBlue Swirl } 2707ad71ed68SBlue Swirl #endif 2708ad71ed68SBlue Swirl 27091f26c751SDaniel Henrique Barboza #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY) 27101f26c751SDaniel Henrique Barboza void helper_rfebb(CPUPPCState *env, target_ulong s) 27111f26c751SDaniel Henrique Barboza { 27121f26c751SDaniel Henrique Barboza target_ulong msr = env->msr; 27131f26c751SDaniel Henrique Barboza 27141f26c751SDaniel Henrique Barboza /* 27151f26c751SDaniel Henrique Barboza * Handling of BESCR bits 32:33 according to PowerISA v3.1: 27161f26c751SDaniel Henrique Barboza * 27171f26c751SDaniel Henrique Barboza * "If BESCR 32:33 != 0b00 the instruction is treated as if 27181f26c751SDaniel Henrique Barboza * the instruction form were invalid." 27191f26c751SDaniel Henrique Barboza */ 27201f26c751SDaniel Henrique Barboza if (env->spr[SPR_BESCR] & BESCR_INVALID) { 27211f26c751SDaniel Henrique Barboza raise_exception_err(env, POWERPC_EXCP_PROGRAM, 27221f26c751SDaniel Henrique Barboza POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL); 27231f26c751SDaniel Henrique Barboza } 27241f26c751SDaniel Henrique Barboza 27251f26c751SDaniel Henrique Barboza env->nip = env->spr[SPR_EBBRR]; 27261f26c751SDaniel Henrique Barboza 27271f26c751SDaniel Henrique Barboza /* Switching to 32-bit ? Crop the nip */ 27281f26c751SDaniel Henrique Barboza if (!msr_is_64bit(env, msr)) { 27291f26c751SDaniel Henrique Barboza env->nip = (uint32_t)env->spr[SPR_EBBRR]; 27301f26c751SDaniel Henrique Barboza } 27311f26c751SDaniel Henrique Barboza 27321f26c751SDaniel Henrique Barboza if (s) { 27331f26c751SDaniel Henrique Barboza env->spr[SPR_BESCR] |= BESCR_GE; 27341f26c751SDaniel Henrique Barboza } else { 27351f26c751SDaniel Henrique Barboza env->spr[SPR_BESCR] &= ~BESCR_GE; 27361f26c751SDaniel Henrique Barboza } 27371f26c751SDaniel Henrique Barboza } 2738d3412df2SDaniel Henrique Barboza 2739d3412df2SDaniel Henrique Barboza /* 2740d3412df2SDaniel Henrique Barboza * Triggers or queues an 'ebb_excp' EBB exception. All checks 2741d3412df2SDaniel Henrique Barboza * but FSCR, HFSCR and msr_pr must be done beforehand. 2742d3412df2SDaniel Henrique Barboza * 2743d3412df2SDaniel Henrique Barboza * PowerISA v3.1 isn't clear about whether an EBB should be 2744d3412df2SDaniel Henrique Barboza * postponed or cancelled if the EBB facility is unavailable. 2745d3412df2SDaniel Henrique Barboza * Our assumption here is that the EBB is cancelled if both 2746d3412df2SDaniel Henrique Barboza * FSCR and HFSCR EBB facilities aren't available. 2747d3412df2SDaniel Henrique Barboza */ 2748d3412df2SDaniel Henrique Barboza static void do_ebb(CPUPPCState *env, int ebb_excp) 2749d3412df2SDaniel Henrique Barboza { 2750d3412df2SDaniel Henrique Barboza PowerPCCPU *cpu = env_archcpu(env); 2751d3412df2SDaniel Henrique Barboza 2752d3412df2SDaniel Henrique Barboza /* 2753d3412df2SDaniel Henrique Barboza * FSCR_EBB and FSCR_IC_EBB are the same bits used with 2754d3412df2SDaniel Henrique Barboza * HFSCR. 2755d3412df2SDaniel Henrique Barboza */ 2756d3412df2SDaniel Henrique Barboza helper_fscr_facility_check(env, FSCR_EBB, 0, FSCR_IC_EBB); 2757d3412df2SDaniel Henrique Barboza helper_hfscr_facility_check(env, FSCR_EBB, "EBB", FSCR_IC_EBB); 2758d3412df2SDaniel Henrique Barboza 2759d3412df2SDaniel Henrique Barboza if (ebb_excp == POWERPC_EXCP_PERFM_EBB) { 2760d3412df2SDaniel Henrique Barboza env->spr[SPR_BESCR] |= BESCR_PMEO; 2761d3412df2SDaniel Henrique Barboza } else if (ebb_excp == POWERPC_EXCP_EXTERNAL_EBB) { 2762d3412df2SDaniel Henrique Barboza env->spr[SPR_BESCR] |= BESCR_EEO; 2763d3412df2SDaniel Henrique Barboza } 2764d3412df2SDaniel Henrique Barboza 2765d41ccf6eSVíctor Colombo if (FIELD_EX64(env->msr, MSR, PR)) { 2766d3412df2SDaniel Henrique Barboza powerpc_excp(cpu, ebb_excp); 2767d3412df2SDaniel Henrique Barboza } else { 27687b694df6SMatheus Ferst ppc_set_irq(cpu, PPC_INTERRUPT_EBB, 1); 2769d3412df2SDaniel Henrique Barboza } 2770d3412df2SDaniel Henrique Barboza } 2771d3412df2SDaniel Henrique Barboza 2772d3412df2SDaniel Henrique Barboza void raise_ebb_perfm_exception(CPUPPCState *env) 2773d3412df2SDaniel Henrique Barboza { 2774d3412df2SDaniel Henrique Barboza bool perfm_ebb_enabled = env->spr[SPR_POWER_MMCR0] & MMCR0_EBE && 2775d3412df2SDaniel Henrique Barboza env->spr[SPR_BESCR] & BESCR_PME && 2776d3412df2SDaniel Henrique Barboza env->spr[SPR_BESCR] & BESCR_GE; 2777d3412df2SDaniel Henrique Barboza 2778d3412df2SDaniel Henrique Barboza if (!perfm_ebb_enabled) { 2779d3412df2SDaniel Henrique Barboza return; 2780d3412df2SDaniel Henrique Barboza } 2781d3412df2SDaniel Henrique Barboza 2782d3412df2SDaniel Henrique Barboza do_ebb(env, POWERPC_EXCP_PERFM_EBB); 2783d3412df2SDaniel Henrique Barboza } 27841f26c751SDaniel Henrique Barboza #endif 27851f26c751SDaniel Henrique Barboza 2786ad71ed68SBlue Swirl /*****************************************************************************/ 2787ad71ed68SBlue Swirl /* Embedded PowerPC specific helpers */ 2788e5f17ac6SBlue Swirl void helper_40x_rfci(CPUPPCState *env) 2789ad71ed68SBlue Swirl { 2790a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]); 2791ad71ed68SBlue Swirl } 2792ad71ed68SBlue Swirl 2793e5f17ac6SBlue Swirl void helper_rfci(CPUPPCState *env) 2794ad71ed68SBlue Swirl { 2795a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]); 2796ad71ed68SBlue Swirl } 2797ad71ed68SBlue Swirl 2798e5f17ac6SBlue Swirl void helper_rfdi(CPUPPCState *env) 2799ad71ed68SBlue Swirl { 2800a1bb7384SScott Wood /* FIXME: choose CSRR1 or DSRR1 based on cpu type */ 2801a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]); 2802ad71ed68SBlue Swirl } 2803ad71ed68SBlue Swirl 2804e5f17ac6SBlue Swirl void helper_rfmci(CPUPPCState *env) 2805ad71ed68SBlue Swirl { 2806a1bb7384SScott Wood /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */ 2807a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]); 2808ad71ed68SBlue Swirl } 28092b44e219SBruno Larsen (billionai) #endif /* CONFIG_TCG */ 28102b44e219SBruno Larsen (billionai) #endif /* !defined(CONFIG_USER_ONLY) */ 2811ad71ed68SBlue Swirl 28122b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 2813e5f17ac6SBlue Swirl void helper_tw(CPUPPCState *env, target_ulong arg1, target_ulong arg2, 2814e5f17ac6SBlue Swirl uint32_t flags) 2815ad71ed68SBlue Swirl { 2816ad71ed68SBlue Swirl if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) || 2817ad71ed68SBlue Swirl ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) || 2818ad71ed68SBlue Swirl ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) || 2819ad71ed68SBlue Swirl ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) || 2820ad71ed68SBlue Swirl ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) { 282172073dccSBenjamin Herrenschmidt raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 282272073dccSBenjamin Herrenschmidt POWERPC_EXCP_TRAP, GETPC()); 2823ad71ed68SBlue Swirl } 2824ad71ed68SBlue Swirl } 2825ad71ed68SBlue Swirl 2826ad71ed68SBlue Swirl #if defined(TARGET_PPC64) 2827e5f17ac6SBlue Swirl void helper_td(CPUPPCState *env, target_ulong arg1, target_ulong arg2, 2828e5f17ac6SBlue Swirl uint32_t flags) 2829ad71ed68SBlue Swirl { 2830ad71ed68SBlue Swirl if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) || 2831ad71ed68SBlue Swirl ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) || 2832ad71ed68SBlue Swirl ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) || 2833ad71ed68SBlue Swirl ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) || 2834ad71ed68SBlue Swirl ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) { 283572073dccSBenjamin Herrenschmidt raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 283672073dccSBenjamin Herrenschmidt POWERPC_EXCP_TRAP, GETPC()); 2837ad71ed68SBlue Swirl } 2838ad71ed68SBlue Swirl } 2839ad71ed68SBlue Swirl #endif 28402b44e219SBruno Larsen (billionai) #endif 2841ad71ed68SBlue Swirl 2842049b4ad6SVaibhav Jain #ifdef CONFIG_TCG 2843670f1da3SVíctor Colombo static uint32_t helper_SIMON_LIKE_32_64(uint32_t x, uint64_t key, uint32_t lane) 2844670f1da3SVíctor Colombo { 2845670f1da3SVíctor Colombo const uint16_t c = 0xfffc; 2846670f1da3SVíctor Colombo const uint64_t z0 = 0xfa2561cdf44ac398ULL; 2847670f1da3SVíctor Colombo uint16_t z = 0, temp; 2848670f1da3SVíctor Colombo uint16_t k[32], eff_k[32], xleft[33], xright[33], fxleft[32]; 2849670f1da3SVíctor Colombo 2850670f1da3SVíctor Colombo for (int i = 3; i >= 0; i--) { 2851670f1da3SVíctor Colombo k[i] = key & 0xffff; 2852670f1da3SVíctor Colombo key >>= 16; 2853670f1da3SVíctor Colombo } 2854670f1da3SVíctor Colombo xleft[0] = x & 0xffff; 2855670f1da3SVíctor Colombo xright[0] = (x >> 16) & 0xffff; 2856670f1da3SVíctor Colombo 2857670f1da3SVíctor Colombo for (int i = 0; i < 28; i++) { 2858670f1da3SVíctor Colombo z = (z0 >> (63 - i)) & 1; 2859670f1da3SVíctor Colombo temp = ror16(k[i + 3], 3) ^ k[i + 1]; 2860670f1da3SVíctor Colombo k[i + 4] = c ^ z ^ k[i] ^ temp ^ ror16(temp, 1); 2861670f1da3SVíctor Colombo } 2862670f1da3SVíctor Colombo 2863670f1da3SVíctor Colombo for (int i = 0; i < 8; i++) { 2864670f1da3SVíctor Colombo eff_k[4 * i + 0] = k[4 * i + ((0 + lane) % 4)]; 2865670f1da3SVíctor Colombo eff_k[4 * i + 1] = k[4 * i + ((1 + lane) % 4)]; 2866670f1da3SVíctor Colombo eff_k[4 * i + 2] = k[4 * i + ((2 + lane) % 4)]; 2867670f1da3SVíctor Colombo eff_k[4 * i + 3] = k[4 * i + ((3 + lane) % 4)]; 2868670f1da3SVíctor Colombo } 2869670f1da3SVíctor Colombo 2870670f1da3SVíctor Colombo for (int i = 0; i < 32; i++) { 2871670f1da3SVíctor Colombo fxleft[i] = (rol16(xleft[i], 1) & 2872670f1da3SVíctor Colombo rol16(xleft[i], 8)) ^ rol16(xleft[i], 2); 2873670f1da3SVíctor Colombo xleft[i + 1] = xright[i] ^ fxleft[i] ^ eff_k[i]; 2874670f1da3SVíctor Colombo xright[i + 1] = xleft[i]; 2875670f1da3SVíctor Colombo } 2876670f1da3SVíctor Colombo 2877670f1da3SVíctor Colombo return (((uint32_t)xright[32]) << 16) | xleft[32]; 2878670f1da3SVíctor Colombo } 2879670f1da3SVíctor Colombo 2880670f1da3SVíctor Colombo static uint64_t hash_digest(uint64_t ra, uint64_t rb, uint64_t key) 2881670f1da3SVíctor Colombo { 2882670f1da3SVíctor Colombo uint64_t stage0_h = 0ULL, stage0_l = 0ULL; 2883670f1da3SVíctor Colombo uint64_t stage1_h, stage1_l; 2884670f1da3SVíctor Colombo 2885670f1da3SVíctor Colombo for (int i = 0; i < 4; i++) { 2886670f1da3SVíctor Colombo stage0_h |= ror64(rb & 0xff, 8 * (2 * i + 1)); 2887670f1da3SVíctor Colombo stage0_h |= ((ra >> 32) & 0xff) << (8 * 2 * i); 2888670f1da3SVíctor Colombo stage0_l |= ror64((rb >> 32) & 0xff, 8 * (2 * i + 1)); 2889670f1da3SVíctor Colombo stage0_l |= (ra & 0xff) << (8 * 2 * i); 2890670f1da3SVíctor Colombo rb >>= 8; 2891670f1da3SVíctor Colombo ra >>= 8; 2892670f1da3SVíctor Colombo } 2893670f1da3SVíctor Colombo 2894670f1da3SVíctor Colombo stage1_h = (uint64_t)helper_SIMON_LIKE_32_64(stage0_h >> 32, key, 0) << 32; 2895670f1da3SVíctor Colombo stage1_h |= helper_SIMON_LIKE_32_64(stage0_h, key, 1); 2896670f1da3SVíctor Colombo stage1_l = (uint64_t)helper_SIMON_LIKE_32_64(stage0_l >> 32, key, 2) << 32; 2897670f1da3SVíctor Colombo stage1_l |= helper_SIMON_LIKE_32_64(stage0_l, key, 3); 2898670f1da3SVíctor Colombo 2899670f1da3SVíctor Colombo return stage1_h ^ stage1_l; 2900670f1da3SVíctor Colombo } 2901670f1da3SVíctor Colombo 29024091fabfSNicholas Miehlbradt static void do_hash(CPUPPCState *env, target_ulong ea, target_ulong ra, 29034091fabfSNicholas Miehlbradt target_ulong rb, uint64_t key, bool store) 29044091fabfSNicholas Miehlbradt { 29054091fabfSNicholas Miehlbradt uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash; 29064091fabfSNicholas Miehlbradt 29074091fabfSNicholas Miehlbradt if (store) { 29084091fabfSNicholas Miehlbradt cpu_stq_data_ra(env, ea, calculated_hash, GETPC()); 29094091fabfSNicholas Miehlbradt } else { 29104091fabfSNicholas Miehlbradt loaded_hash = cpu_ldq_data_ra(env, ea, GETPC()); 29114091fabfSNicholas Miehlbradt if (loaded_hash != calculated_hash) { 29124091fabfSNicholas Miehlbradt raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 29134091fabfSNicholas Miehlbradt POWERPC_EXCP_TRAP, GETPC()); 29144091fabfSNicholas Miehlbradt } 29154091fabfSNicholas Miehlbradt } 29164091fabfSNicholas Miehlbradt } 29174091fabfSNicholas Miehlbradt 2918670f1da3SVíctor Colombo #include "qemu/guest-random.h" 2919670f1da3SVíctor Colombo 29204091fabfSNicholas Miehlbradt #ifdef TARGET_PPC64 29214091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect) \ 2922670f1da3SVíctor Colombo void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra, \ 2923670f1da3SVíctor Colombo target_ulong rb) \ 2924670f1da3SVíctor Colombo { \ 29254091fabfSNicholas Miehlbradt if (env->msr & R_MSR_PR_MASK) { \ 29264091fabfSNicholas Miehlbradt if (!(env->spr[SPR_DEXCR] & R_DEXCR_PRO_##dexcr_aspect##_MASK || \ 29274091fabfSNicholas Miehlbradt env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK)) \ 29284091fabfSNicholas Miehlbradt return; \ 29294091fabfSNicholas Miehlbradt } else if (!(env->msr & R_MSR_HV_MASK)) { \ 29304091fabfSNicholas Miehlbradt if (!(env->spr[SPR_DEXCR] & R_DEXCR_PNH_##dexcr_aspect##_MASK || \ 29314091fabfSNicholas Miehlbradt env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK)) \ 29324091fabfSNicholas Miehlbradt return; \ 29334091fabfSNicholas Miehlbradt } else if (!(env->msr & R_MSR_S_MASK)) { \ 29344091fabfSNicholas Miehlbradt if (!(env->spr[SPR_HDEXCR] & R_HDEXCR_HNU_##dexcr_aspect##_MASK)) \ 29354091fabfSNicholas Miehlbradt return; \ 29364091fabfSNicholas Miehlbradt } \ 2937670f1da3SVíctor Colombo \ 29384091fabfSNicholas Miehlbradt do_hash(env, ea, ra, rb, key, store); \ 2939670f1da3SVíctor Colombo } 29404091fabfSNicholas Miehlbradt #else 29414091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect) \ 29424091fabfSNicholas Miehlbradt void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra, \ 29434091fabfSNicholas Miehlbradt target_ulong rb) \ 29444091fabfSNicholas Miehlbradt { \ 29454091fabfSNicholas Miehlbradt do_hash(env, ea, ra, rb, key, store); \ 29464091fabfSNicholas Miehlbradt } 29474091fabfSNicholas Miehlbradt #endif /* TARGET_PPC64 */ 2948670f1da3SVíctor Colombo 29494091fabfSNicholas Miehlbradt HELPER_HASH(HASHST, env->spr[SPR_HASHKEYR], true, NPHIE) 29504091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHK, env->spr[SPR_HASHKEYR], false, NPHIE) 29514091fabfSNicholas Miehlbradt HELPER_HASH(HASHSTP, env->spr[SPR_HASHPKEYR], true, PHIE) 29524091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHKP, env->spr[SPR_HASHPKEYR], false, PHIE) 2953049b4ad6SVaibhav Jain #endif /* CONFIG_TCG */ 2954670f1da3SVíctor Colombo 2955ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY) 2956ad71ed68SBlue Swirl 29572b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 2958ad71ed68SBlue Swirl 2959ad71ed68SBlue Swirl /* Embedded.Processor Control */ 2960ad71ed68SBlue Swirl static int dbell2irq(target_ulong rb) 2961ad71ed68SBlue Swirl { 2962ad71ed68SBlue Swirl int msg = rb & DBELL_TYPE_MASK; 2963ad71ed68SBlue Swirl int irq = -1; 2964ad71ed68SBlue Swirl 2965ad71ed68SBlue Swirl switch (msg) { 2966ad71ed68SBlue Swirl case DBELL_TYPE_DBELL: 2967ad71ed68SBlue Swirl irq = PPC_INTERRUPT_DOORBELL; 2968ad71ed68SBlue Swirl break; 2969ad71ed68SBlue Swirl case DBELL_TYPE_DBELL_CRIT: 2970ad71ed68SBlue Swirl irq = PPC_INTERRUPT_CDOORBELL; 2971ad71ed68SBlue Swirl break; 2972ad71ed68SBlue Swirl case DBELL_TYPE_G_DBELL: 2973ad71ed68SBlue Swirl case DBELL_TYPE_G_DBELL_CRIT: 2974ad71ed68SBlue Swirl case DBELL_TYPE_G_DBELL_MC: 2975ad71ed68SBlue Swirl /* XXX implement */ 2976ad71ed68SBlue Swirl default: 2977ad71ed68SBlue Swirl break; 2978ad71ed68SBlue Swirl } 2979ad71ed68SBlue Swirl 2980ad71ed68SBlue Swirl return irq; 2981ad71ed68SBlue Swirl } 2982ad71ed68SBlue Swirl 2983e5f17ac6SBlue Swirl void helper_msgclr(CPUPPCState *env, target_ulong rb) 2984ad71ed68SBlue Swirl { 2985ad71ed68SBlue Swirl int irq = dbell2irq(rb); 2986ad71ed68SBlue Swirl 2987ad71ed68SBlue Swirl if (irq < 0) { 2988ad71ed68SBlue Swirl return; 2989ad71ed68SBlue Swirl } 2990ad71ed68SBlue Swirl 29917b694df6SMatheus Ferst ppc_set_irq(env_archcpu(env), irq, 0); 2992ad71ed68SBlue Swirl } 2993ad71ed68SBlue Swirl 2994ad71ed68SBlue Swirl void helper_msgsnd(target_ulong rb) 2995ad71ed68SBlue Swirl { 2996ad71ed68SBlue Swirl int irq = dbell2irq(rb); 2997ad71ed68SBlue Swirl int pir = rb & DBELL_PIRTAG_MASK; 2998182735efSAndreas Färber CPUState *cs; 2999ad71ed68SBlue Swirl 3000ad71ed68SBlue Swirl if (irq < 0) { 3001ad71ed68SBlue Swirl return; 3002ad71ed68SBlue Swirl } 3003ad71ed68SBlue Swirl 3004f1c29ebcSThomas Huth qemu_mutex_lock_iothread(); 3005bdc44640SAndreas Färber CPU_FOREACH(cs) { 3006182735efSAndreas Färber PowerPCCPU *cpu = POWERPC_CPU(cs); 3007182735efSAndreas Färber CPUPPCState *cenv = &cpu->env; 3008182735efSAndreas Färber 3009ad71ed68SBlue Swirl if ((rb & DBELL_BRDCAST) || (cenv->spr[SPR_BOOKE_PIR] == pir)) { 30107b694df6SMatheus Ferst ppc_set_irq(cpu, irq, 1); 3011ad71ed68SBlue Swirl } 3012ad71ed68SBlue Swirl } 3013f1c29ebcSThomas Huth qemu_mutex_unlock_iothread(); 3014ad71ed68SBlue Swirl } 30157af1e7b0SCédric Le Goater 30167af1e7b0SCédric Le Goater /* Server Processor Control */ 30177af1e7b0SCédric Le Goater 30185ba7ba1dSCédric Le Goater static bool dbell_type_server(target_ulong rb) 30195ba7ba1dSCédric Le Goater { 302047733729SDavid Gibson /* 302147733729SDavid Gibson * A Directed Hypervisor Doorbell message is sent only if the 30227af1e7b0SCédric Le Goater * message type is 5. All other types are reserved and the 302347733729SDavid Gibson * instruction is a no-op 302447733729SDavid Gibson */ 30255ba7ba1dSCédric Le Goater return (rb & DBELL_TYPE_MASK) == DBELL_TYPE_DBELL_SERVER; 30267af1e7b0SCédric Le Goater } 30277af1e7b0SCédric Le Goater 30287af1e7b0SCédric Le Goater void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb) 30297af1e7b0SCédric Le Goater { 30305ba7ba1dSCédric Le Goater if (!dbell_type_server(rb)) { 30317af1e7b0SCédric Le Goater return; 30327af1e7b0SCédric Le Goater } 30337af1e7b0SCédric Le Goater 30347b694df6SMatheus Ferst ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0); 30357af1e7b0SCédric Le Goater } 30367af1e7b0SCédric Le Goater 30375ba7ba1dSCédric Le Goater static void book3s_msgsnd_common(int pir, int irq) 30387af1e7b0SCédric Le Goater { 30397af1e7b0SCédric Le Goater CPUState *cs; 30407af1e7b0SCédric Le Goater 30417af1e7b0SCédric Le Goater qemu_mutex_lock_iothread(); 30427af1e7b0SCédric Le Goater CPU_FOREACH(cs) { 30437af1e7b0SCédric Le Goater PowerPCCPU *cpu = POWERPC_CPU(cs); 30447af1e7b0SCédric Le Goater CPUPPCState *cenv = &cpu->env; 30457af1e7b0SCédric Le Goater 30467af1e7b0SCédric Le Goater /* TODO: broadcast message to all threads of the same processor */ 30477af1e7b0SCédric Le Goater if (cenv->spr_cb[SPR_PIR].default_value == pir) { 30487b694df6SMatheus Ferst ppc_set_irq(cpu, irq, 1); 30497af1e7b0SCédric Le Goater } 30507af1e7b0SCédric Le Goater } 30517af1e7b0SCédric Le Goater qemu_mutex_unlock_iothread(); 30527af1e7b0SCédric Le Goater } 30535ba7ba1dSCédric Le Goater 30545ba7ba1dSCédric Le Goater void helper_book3s_msgsnd(target_ulong rb) 30555ba7ba1dSCédric Le Goater { 30565ba7ba1dSCédric Le Goater int pir = rb & DBELL_PROCIDTAG_MASK; 30575ba7ba1dSCédric Le Goater 30585ba7ba1dSCédric Le Goater if (!dbell_type_server(rb)) { 30595ba7ba1dSCédric Le Goater return; 30605ba7ba1dSCédric Le Goater } 30615ba7ba1dSCédric Le Goater 30625ba7ba1dSCédric Le Goater book3s_msgsnd_common(pir, PPC_INTERRUPT_HDOORBELL); 30635ba7ba1dSCédric Le Goater } 30645ba7ba1dSCédric Le Goater 30655ba7ba1dSCédric Le Goater #if defined(TARGET_PPC64) 30665ba7ba1dSCédric Le Goater void helper_book3s_msgclrp(CPUPPCState *env, target_ulong rb) 30675ba7ba1dSCédric Le Goater { 3068493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "msgclrp", HFSCR_IC_MSGP); 3069493028d8SCédric Le Goater 30705ba7ba1dSCédric Le Goater if (!dbell_type_server(rb)) { 30715ba7ba1dSCédric Le Goater return; 30725ba7ba1dSCédric Le Goater } 30735ba7ba1dSCédric Le Goater 30742e985555SNicholas Piggin ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_DOORBELL, 0); 30755ba7ba1dSCédric Le Goater } 30765ba7ba1dSCédric Le Goater 30775ba7ba1dSCédric Le Goater /* 30785ba7ba1dSCédric Le Goater * sends a message to other threads that are on the same 30795ba7ba1dSCédric Le Goater * multi-threaded processor 30805ba7ba1dSCédric Le Goater */ 30815ba7ba1dSCédric Le Goater void helper_book3s_msgsndp(CPUPPCState *env, target_ulong rb) 30825ba7ba1dSCédric Le Goater { 30835ba7ba1dSCédric Le Goater int pir = env->spr_cb[SPR_PIR].default_value; 30845ba7ba1dSCédric Le Goater 3085493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "msgsndp", HFSCR_IC_MSGP); 3086493028d8SCédric Le Goater 30875ba7ba1dSCédric Le Goater if (!dbell_type_server(rb)) { 30885ba7ba1dSCédric Le Goater return; 30895ba7ba1dSCédric Le Goater } 30905ba7ba1dSCédric Le Goater 30915ba7ba1dSCédric Le Goater /* TODO: TCG supports only one thread */ 30925ba7ba1dSCédric Le Goater 30935ba7ba1dSCédric Le Goater book3s_msgsnd_common(pir, PPC_INTERRUPT_DOORBELL); 30945ba7ba1dSCédric Le Goater } 3095996473e4SRichard Henderson #endif /* TARGET_PPC64 */ 30960f3110faSRichard Henderson 30970f3110faSRichard Henderson void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr, 30980f3110faSRichard Henderson MMUAccessType access_type, 30990f3110faSRichard Henderson int mmu_idx, uintptr_t retaddr) 31000f3110faSRichard Henderson { 31010f3110faSRichard Henderson CPUPPCState *env = cs->env_ptr; 310229c4a336SFabiano Rosas uint32_t insn; 310329c4a336SFabiano Rosas 310429c4a336SFabiano Rosas /* Restore state and reload the insn we executed, for filling in DSISR. */ 31053d419a4dSRichard Henderson cpu_restore_state(cs, retaddr); 310629c4a336SFabiano Rosas insn = cpu_ldl_code(env, env->nip); 31070f3110faSRichard Henderson 3108a7e3af13SRichard Henderson switch (env->mmu_model) { 3109a7e3af13SRichard Henderson case POWERPC_MMU_SOFT_4xx: 3110a7e3af13SRichard Henderson env->spr[SPR_40x_DEAR] = vaddr; 3111a7e3af13SRichard Henderson break; 3112a7e3af13SRichard Henderson case POWERPC_MMU_BOOKE: 3113a7e3af13SRichard Henderson case POWERPC_MMU_BOOKE206: 3114a7e3af13SRichard Henderson env->spr[SPR_BOOKE_DEAR] = vaddr; 3115a7e3af13SRichard Henderson break; 3116a7e3af13SRichard Henderson default: 3117a7e3af13SRichard Henderson env->spr[SPR_DAR] = vaddr; 3118a7e3af13SRichard Henderson break; 3119a7e3af13SRichard Henderson } 3120a7e3af13SRichard Henderson 31210f3110faSRichard Henderson cs->exception_index = POWERPC_EXCP_ALIGN; 312229c4a336SFabiano Rosas env->error_code = insn & 0x03FF0000; 312329c4a336SFabiano Rosas cpu_loop_exit(cs); 31240f3110faSRichard Henderson } 3125996473e4SRichard Henderson #endif /* CONFIG_TCG */ 3126996473e4SRichard Henderson #endif /* !CONFIG_USER_ONLY */ 3127