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 315a5d3b23SNicholas Piggin #include "sysemu/tcg.h" 322b44e219SBruno Larsen (billionai) #include "exec/helper-proto.h" 332b44e219SBruno Larsen (billionai) #include "exec/cpu_ldst.h" 342b44e219SBruno Larsen (billionai) #endif 352b44e219SBruno Larsen (billionai) 36c79c73f6SBlue Swirl /*****************************************************************************/ 37c79c73f6SBlue Swirl /* Exception processing */ 38f725245cSPhilippe Mathieu-Daudé #if !defined(CONFIG_USER_ONLY) 3997a8ea5aSAndreas Färber 406789f23bSCédric Le Goater static const char *powerpc_excp_name(int excp) 416789f23bSCédric Le Goater { 426789f23bSCédric Le Goater switch (excp) { 436789f23bSCédric Le Goater case POWERPC_EXCP_CRITICAL: return "CRITICAL"; 446789f23bSCédric Le Goater case POWERPC_EXCP_MCHECK: return "MCHECK"; 456789f23bSCédric Le Goater case POWERPC_EXCP_DSI: return "DSI"; 466789f23bSCédric Le Goater case POWERPC_EXCP_ISI: return "ISI"; 476789f23bSCédric Le Goater case POWERPC_EXCP_EXTERNAL: return "EXTERNAL"; 486789f23bSCédric Le Goater case POWERPC_EXCP_ALIGN: return "ALIGN"; 496789f23bSCédric Le Goater case POWERPC_EXCP_PROGRAM: return "PROGRAM"; 506789f23bSCédric Le Goater case POWERPC_EXCP_FPU: return "FPU"; 516789f23bSCédric Le Goater case POWERPC_EXCP_SYSCALL: return "SYSCALL"; 526789f23bSCédric Le Goater case POWERPC_EXCP_APU: return "APU"; 536789f23bSCédric Le Goater case POWERPC_EXCP_DECR: return "DECR"; 546789f23bSCédric Le Goater case POWERPC_EXCP_FIT: return "FIT"; 556789f23bSCédric Le Goater case POWERPC_EXCP_WDT: return "WDT"; 566789f23bSCédric Le Goater case POWERPC_EXCP_DTLB: return "DTLB"; 576789f23bSCédric Le Goater case POWERPC_EXCP_ITLB: return "ITLB"; 586789f23bSCédric Le Goater case POWERPC_EXCP_DEBUG: return "DEBUG"; 596789f23bSCédric Le Goater case POWERPC_EXCP_SPEU: return "SPEU"; 606789f23bSCédric Le Goater case POWERPC_EXCP_EFPDI: return "EFPDI"; 616789f23bSCédric Le Goater case POWERPC_EXCP_EFPRI: return "EFPRI"; 626789f23bSCédric Le Goater case POWERPC_EXCP_EPERFM: return "EPERFM"; 636789f23bSCédric Le Goater case POWERPC_EXCP_DOORI: return "DOORI"; 646789f23bSCédric Le Goater case POWERPC_EXCP_DOORCI: return "DOORCI"; 656789f23bSCédric Le Goater case POWERPC_EXCP_GDOORI: return "GDOORI"; 666789f23bSCédric Le Goater case POWERPC_EXCP_GDOORCI: return "GDOORCI"; 676789f23bSCédric Le Goater case POWERPC_EXCP_HYPPRIV: return "HYPPRIV"; 686789f23bSCédric Le Goater case POWERPC_EXCP_RESET: return "RESET"; 696789f23bSCédric Le Goater case POWERPC_EXCP_DSEG: return "DSEG"; 706789f23bSCédric Le Goater case POWERPC_EXCP_ISEG: return "ISEG"; 716789f23bSCédric Le Goater case POWERPC_EXCP_HDECR: return "HDECR"; 726789f23bSCédric Le Goater case POWERPC_EXCP_TRACE: return "TRACE"; 736789f23bSCédric Le Goater case POWERPC_EXCP_HDSI: return "HDSI"; 746789f23bSCédric Le Goater case POWERPC_EXCP_HISI: return "HISI"; 756789f23bSCédric Le Goater case POWERPC_EXCP_HDSEG: return "HDSEG"; 766789f23bSCédric Le Goater case POWERPC_EXCP_HISEG: return "HISEG"; 776789f23bSCédric Le Goater case POWERPC_EXCP_VPU: return "VPU"; 786789f23bSCédric Le Goater case POWERPC_EXCP_PIT: return "PIT"; 796789f23bSCédric Le Goater case POWERPC_EXCP_EMUL: return "EMUL"; 806789f23bSCédric Le Goater case POWERPC_EXCP_IFTLB: return "IFTLB"; 816789f23bSCédric Le Goater case POWERPC_EXCP_DLTLB: return "DLTLB"; 826789f23bSCédric Le Goater case POWERPC_EXCP_DSTLB: return "DSTLB"; 836789f23bSCédric Le Goater case POWERPC_EXCP_FPA: return "FPA"; 846789f23bSCédric Le Goater case POWERPC_EXCP_DABR: return "DABR"; 856789f23bSCédric Le Goater case POWERPC_EXCP_IABR: return "IABR"; 866789f23bSCédric Le Goater case POWERPC_EXCP_SMI: return "SMI"; 876789f23bSCédric Le Goater case POWERPC_EXCP_PERFM: return "PERFM"; 886789f23bSCédric Le Goater case POWERPC_EXCP_THERM: return "THERM"; 896789f23bSCédric Le Goater case POWERPC_EXCP_VPUA: return "VPUA"; 906789f23bSCédric Le Goater case POWERPC_EXCP_SOFTP: return "SOFTP"; 916789f23bSCédric Le Goater case POWERPC_EXCP_MAINT: return "MAINT"; 926789f23bSCédric Le Goater case POWERPC_EXCP_MEXTBR: return "MEXTBR"; 936789f23bSCédric Le Goater case POWERPC_EXCP_NMEXTBR: return "NMEXTBR"; 946789f23bSCédric Le Goater case POWERPC_EXCP_ITLBE: return "ITLBE"; 956789f23bSCédric Le Goater case POWERPC_EXCP_DTLBE: return "DTLBE"; 966789f23bSCédric Le Goater case POWERPC_EXCP_VSXU: return "VSXU"; 976789f23bSCédric Le Goater case POWERPC_EXCP_FU: return "FU"; 986789f23bSCédric Le Goater case POWERPC_EXCP_HV_EMU: return "HV_EMU"; 996789f23bSCédric Le Goater case POWERPC_EXCP_HV_MAINT: return "HV_MAINT"; 1006789f23bSCédric Le Goater case POWERPC_EXCP_HV_FU: return "HV_FU"; 1016789f23bSCédric Le Goater case POWERPC_EXCP_SDOOR: return "SDOOR"; 1026789f23bSCédric Le Goater case POWERPC_EXCP_SDOOR_HV: return "SDOOR_HV"; 1036789f23bSCédric Le Goater case POWERPC_EXCP_HVIRT: return "HVIRT"; 1046789f23bSCédric Le Goater case POWERPC_EXCP_SYSCALL_VECTORED: return "SYSCALL_VECTORED"; 1056789f23bSCédric Le Goater default: 1066789f23bSCédric Le Goater g_assert_not_reached(); 1076789f23bSCédric Le Goater } 1086789f23bSCédric Le Goater } 1096789f23bSCédric Le Goater 11062e79ef9SCédric Le Goater static void dump_syscall(CPUPPCState *env) 111c79c73f6SBlue Swirl { 1126dc6b557SNicholas Piggin qemu_log_mask(CPU_LOG_INT, "syscall r0=%016" PRIx64 1136dc6b557SNicholas Piggin " r3=%016" PRIx64 " r4=%016" PRIx64 " r5=%016" PRIx64 1146dc6b557SNicholas Piggin " r6=%016" PRIx64 " r7=%016" PRIx64 " r8=%016" PRIx64 115c79c73f6SBlue Swirl " nip=" TARGET_FMT_lx "\n", 116c79c73f6SBlue Swirl ppc_dump_gpr(env, 0), ppc_dump_gpr(env, 3), 117c79c73f6SBlue Swirl ppc_dump_gpr(env, 4), ppc_dump_gpr(env, 5), 1186dc6b557SNicholas Piggin ppc_dump_gpr(env, 6), ppc_dump_gpr(env, 7), 1196dc6b557SNicholas Piggin ppc_dump_gpr(env, 8), env->nip); 1206dc6b557SNicholas Piggin } 1216dc6b557SNicholas Piggin 12262e79ef9SCédric Le Goater static void dump_hcall(CPUPPCState *env) 1236dc6b557SNicholas Piggin { 1246dc6b557SNicholas Piggin qemu_log_mask(CPU_LOG_INT, "hypercall r3=%016" PRIx64 1256dc6b557SNicholas Piggin " r4=%016" PRIx64 " r5=%016" PRIx64 " r6=%016" PRIx64 1266dc6b557SNicholas Piggin " r7=%016" PRIx64 " r8=%016" PRIx64 " r9=%016" PRIx64 1276dc6b557SNicholas Piggin " r10=%016" PRIx64 " r11=%016" PRIx64 " r12=%016" PRIx64 1286dc6b557SNicholas Piggin " nip=" TARGET_FMT_lx "\n", 1296dc6b557SNicholas Piggin ppc_dump_gpr(env, 3), ppc_dump_gpr(env, 4), 1306dc6b557SNicholas Piggin ppc_dump_gpr(env, 5), ppc_dump_gpr(env, 6), 1316dc6b557SNicholas Piggin ppc_dump_gpr(env, 7), ppc_dump_gpr(env, 8), 1326dc6b557SNicholas Piggin ppc_dump_gpr(env, 9), ppc_dump_gpr(env, 10), 1336dc6b557SNicholas Piggin ppc_dump_gpr(env, 11), ppc_dump_gpr(env, 12), 1346dc6b557SNicholas Piggin env->nip); 135c79c73f6SBlue Swirl } 136c79c73f6SBlue Swirl 137888050cfSNicholas Piggin #ifdef CONFIG_TCG 138888050cfSNicholas Piggin /* Return true iff byteswap is needed to load instruction */ 139888050cfSNicholas Piggin static inline bool insn_need_byteswap(CPUArchState *env) 140888050cfSNicholas Piggin { 141888050cfSNicholas Piggin /* SYSTEM builds TARGET_BIG_ENDIAN. Need to swap when MSR[LE] is set */ 142888050cfSNicholas Piggin return !!(env->msr & ((target_ulong)1 << MSR_LE)); 143888050cfSNicholas Piggin } 144888050cfSNicholas Piggin 1455a5d3b23SNicholas Piggin static uint32_t ppc_ldl_code(CPUArchState *env, abi_ptr addr) 146888050cfSNicholas Piggin { 147888050cfSNicholas Piggin uint32_t insn = cpu_ldl_code(env, addr); 148888050cfSNicholas Piggin 149888050cfSNicholas Piggin if (insn_need_byteswap(env)) { 150888050cfSNicholas Piggin insn = bswap32(insn); 151888050cfSNicholas Piggin } 152888050cfSNicholas Piggin 153888050cfSNicholas Piggin return insn; 154888050cfSNicholas Piggin } 155888050cfSNicholas Piggin #endif 156888050cfSNicholas Piggin 157e4e27df7SFabiano Rosas static void ppc_excp_debug_sw_tlb(CPUPPCState *env, int excp) 158e4e27df7SFabiano Rosas { 159e4e27df7SFabiano Rosas const char *es; 160e4e27df7SFabiano Rosas target_ulong *miss, *cmp; 161e4e27df7SFabiano Rosas int en; 162e4e27df7SFabiano Rosas 1632e089eceSFabiano Rosas if (!qemu_loglevel_mask(CPU_LOG_MMU)) { 164e4e27df7SFabiano Rosas return; 165e4e27df7SFabiano Rosas } 166e4e27df7SFabiano Rosas 167e4e27df7SFabiano Rosas if (excp == POWERPC_EXCP_IFTLB) { 168e4e27df7SFabiano Rosas es = "I"; 169e4e27df7SFabiano Rosas en = 'I'; 170e4e27df7SFabiano Rosas miss = &env->spr[SPR_IMISS]; 171e4e27df7SFabiano Rosas cmp = &env->spr[SPR_ICMP]; 172e4e27df7SFabiano Rosas } else { 173e4e27df7SFabiano Rosas if (excp == POWERPC_EXCP_DLTLB) { 174e4e27df7SFabiano Rosas es = "DL"; 175e4e27df7SFabiano Rosas } else { 176e4e27df7SFabiano Rosas es = "DS"; 177e4e27df7SFabiano Rosas } 178e4e27df7SFabiano Rosas en = 'D'; 179e4e27df7SFabiano Rosas miss = &env->spr[SPR_DMISS]; 180e4e27df7SFabiano Rosas cmp = &env->spr[SPR_DCMP]; 181e4e27df7SFabiano Rosas } 182e4e27df7SFabiano Rosas qemu_log("6xx %sTLB miss: %cM " TARGET_FMT_lx " %cC " 183e4e27df7SFabiano Rosas TARGET_FMT_lx " H1 " TARGET_FMT_lx " H2 " 184e4e27df7SFabiano Rosas TARGET_FMT_lx " %08x\n", es, en, *miss, en, *cmp, 185e4e27df7SFabiano Rosas env->spr[SPR_HASH1], env->spr[SPR_HASH2], 186e4e27df7SFabiano Rosas env->error_code); 187e4e27df7SFabiano Rosas } 188e4e27df7SFabiano Rosas 18928091374SFabiano Rosas #if defined(TARGET_PPC64) 1903f88a89dSBALATON Zoltan static int powerpc_reset_wakeup(CPUPPCState *env, int excp, target_ulong *msr) 191dead760bSBenjamin Herrenschmidt { 192dead760bSBenjamin Herrenschmidt /* We no longer are in a PM state */ 1931e7fd61dSBenjamin Herrenschmidt env->resume_as_sreset = false; 194dead760bSBenjamin Herrenschmidt 195dead760bSBenjamin Herrenschmidt /* Pretend to be returning from doze always as we don't lose state */ 1960911a60cSLeonardo Bras *msr |= SRR1_WS_NOLOSS; 197dead760bSBenjamin Herrenschmidt 198dead760bSBenjamin Herrenschmidt /* Machine checks are sent normally */ 199dead760bSBenjamin Herrenschmidt if (excp == POWERPC_EXCP_MCHECK) { 200dead760bSBenjamin Herrenschmidt return excp; 201dead760bSBenjamin Herrenschmidt } 202dead760bSBenjamin Herrenschmidt switch (excp) { 203dead760bSBenjamin Herrenschmidt case POWERPC_EXCP_RESET: 2040911a60cSLeonardo Bras *msr |= SRR1_WAKERESET; 205dead760bSBenjamin Herrenschmidt break; 206dead760bSBenjamin Herrenschmidt case POWERPC_EXCP_EXTERNAL: 2070911a60cSLeonardo Bras *msr |= SRR1_WAKEEE; 208dead760bSBenjamin Herrenschmidt break; 209dead760bSBenjamin Herrenschmidt case POWERPC_EXCP_DECR: 2100911a60cSLeonardo Bras *msr |= SRR1_WAKEDEC; 211dead760bSBenjamin Herrenschmidt break; 212dead760bSBenjamin Herrenschmidt case POWERPC_EXCP_SDOOR: 2130911a60cSLeonardo Bras *msr |= SRR1_WAKEDBELL; 214dead760bSBenjamin Herrenschmidt break; 215dead760bSBenjamin Herrenschmidt case POWERPC_EXCP_SDOOR_HV: 2160911a60cSLeonardo Bras *msr |= SRR1_WAKEHDBELL; 217dead760bSBenjamin Herrenschmidt break; 218dead760bSBenjamin Herrenschmidt case POWERPC_EXCP_HV_MAINT: 2190911a60cSLeonardo Bras *msr |= SRR1_WAKEHMI; 220dead760bSBenjamin Herrenschmidt break; 221d8ce5fd6SBenjamin Herrenschmidt case POWERPC_EXCP_HVIRT: 2220911a60cSLeonardo Bras *msr |= SRR1_WAKEHVI; 223d8ce5fd6SBenjamin Herrenschmidt break; 224dead760bSBenjamin Herrenschmidt default: 2253f88a89dSBALATON Zoltan cpu_abort(env_cpu(env), 2263f88a89dSBALATON Zoltan "Unsupported exception %d in Power Save mode\n", excp); 227dead760bSBenjamin Herrenschmidt } 228dead760bSBenjamin Herrenschmidt return POWERPC_EXCP_RESET; 229dead760bSBenjamin Herrenschmidt } 230dead760bSBenjamin Herrenschmidt 2318b7e6b07SNicholas Piggin /* 2328b7e6b07SNicholas Piggin * AIL - Alternate Interrupt Location, a mode that allows interrupts to be 2338b7e6b07SNicholas Piggin * taken with the MMU on, and which uses an alternate location (e.g., so the 2348b7e6b07SNicholas Piggin * kernel/hv can map the vectors there with an effective address). 2358b7e6b07SNicholas Piggin * 2368b7e6b07SNicholas Piggin * An interrupt is considered to be taken "with AIL" or "AIL applies" if they 2378b7e6b07SNicholas Piggin * are delivered in this way. AIL requires the LPCR to be set to enable this 2388b7e6b07SNicholas Piggin * mode, and then a number of conditions have to be true for AIL to apply. 2398b7e6b07SNicholas Piggin * 2408b7e6b07SNicholas Piggin * First of all, SRESET, MCE, and HMI are always delivered without AIL, because 2418b7e6b07SNicholas Piggin * they specifically want to be in real mode (e.g., the MCE might be signaling 2428b7e6b07SNicholas Piggin * a SLB multi-hit which requires SLB flush before the MMU can be enabled). 2438b7e6b07SNicholas Piggin * 2448b7e6b07SNicholas Piggin * After that, behaviour depends on the current MSR[IR], MSR[DR], MSR[HV], 2458b7e6b07SNicholas Piggin * whether or not the interrupt changes MSR[HV] from 0 to 1, and the current 2468b7e6b07SNicholas Piggin * radix mode (LPCR[HR]). 2478b7e6b07SNicholas Piggin * 2488b7e6b07SNicholas Piggin * POWER8, POWER9 with LPCR[HR]=0 2498b7e6b07SNicholas Piggin * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL | 2508b7e6b07SNicholas Piggin * +-----------+-------------+---------+-------------+-----+ 2518b7e6b07SNicholas Piggin * | a | 00/01/10 | x | x | 0 | 2528b7e6b07SNicholas Piggin * | a | 11 | 0 | 1 | 0 | 2538b7e6b07SNicholas Piggin * | a | 11 | 1 | 1 | a | 2548b7e6b07SNicholas Piggin * | a | 11 | 0 | 0 | a | 2558b7e6b07SNicholas Piggin * +-------------------------------------------------------+ 2568b7e6b07SNicholas Piggin * 2578b7e6b07SNicholas Piggin * POWER9 with LPCR[HR]=1 2588b7e6b07SNicholas Piggin * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL | 2598b7e6b07SNicholas Piggin * +-----------+-------------+---------+-------------+-----+ 2608b7e6b07SNicholas Piggin * | a | 00/01/10 | x | x | 0 | 2618b7e6b07SNicholas Piggin * | a | 11 | x | x | a | 2628b7e6b07SNicholas Piggin * +-------------------------------------------------------+ 2638b7e6b07SNicholas Piggin * 2648b7e6b07SNicholas Piggin * The difference with POWER9 being that MSR[HV] 0->1 interrupts can be sent to 265526cdce7SNicholas Piggin * the hypervisor in AIL mode if the guest is radix. This is good for 266526cdce7SNicholas Piggin * performance but allows the guest to influence the AIL of hypervisor 267526cdce7SNicholas Piggin * interrupts using its MSR, and also the hypervisor must disallow guest 268526cdce7SNicholas Piggin * interrupts (MSR[HV] 0->0) from using AIL if the hypervisor does not want to 269526cdce7SNicholas Piggin * use AIL for its MSR[HV] 0->1 interrupts. 270526cdce7SNicholas Piggin * 271526cdce7SNicholas Piggin * POWER10 addresses those issues with a new LPCR[HAIL] bit that is applied to 272526cdce7SNicholas Piggin * interrupts that begin execution with MSR[HV]=1 (so both MSR[HV] 0->1 and 273526cdce7SNicholas Piggin * MSR[HV] 1->1). 274526cdce7SNicholas Piggin * 275526cdce7SNicholas Piggin * HAIL=1 is equivalent to AIL=3, for interrupts delivered with MSR[HV]=1. 276526cdce7SNicholas Piggin * 277526cdce7SNicholas Piggin * POWER10 behaviour is 278526cdce7SNicholas Piggin * | LPCR[AIL] | LPCR[HAIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL | 279526cdce7SNicholas Piggin * +-----------+------------+-------------+---------+-------------+-----+ 280526cdce7SNicholas Piggin * | a | h | 00/01/10 | 0 | 0 | 0 | 281526cdce7SNicholas Piggin * | a | h | 11 | 0 | 0 | a | 282526cdce7SNicholas Piggin * | a | h | x | 0 | 1 | h | 283526cdce7SNicholas Piggin * | a | h | 00/01/10 | 1 | 1 | 0 | 284526cdce7SNicholas Piggin * | a | h | 11 | 1 | 1 | h | 285526cdce7SNicholas Piggin * +--------------------------------------------------------------------+ 2868b7e6b07SNicholas Piggin */ 28710895ab6SFabiano Rosas static void ppc_excp_apply_ail(PowerPCCPU *cpu, int excp, target_ulong msr, 28810895ab6SFabiano Rosas target_ulong *new_msr, target_ulong *vector) 2892586a4d7SFabiano Rosas { 29010895ab6SFabiano Rosas PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu); 2918b7e6b07SNicholas Piggin CPUPPCState *env = &cpu->env; 2928b7e6b07SNicholas Piggin bool mmu_all_on = ((msr >> MSR_IR) & 1) && ((msr >> MSR_DR) & 1); 2938b7e6b07SNicholas Piggin bool hv_escalation = !(msr & MSR_HVB) && (*new_msr & MSR_HVB); 2948b7e6b07SNicholas Piggin int ail = 0; 2952586a4d7SFabiano Rosas 2968b7e6b07SNicholas Piggin if (excp == POWERPC_EXCP_MCHECK || 2978b7e6b07SNicholas Piggin excp == POWERPC_EXCP_RESET || 2988b7e6b07SNicholas Piggin excp == POWERPC_EXCP_HV_MAINT) { 2998b7e6b07SNicholas Piggin /* SRESET, MCE, HMI never apply AIL */ 3008b7e6b07SNicholas Piggin return; 3012586a4d7SFabiano Rosas } 3022586a4d7SFabiano Rosas 30310895ab6SFabiano Rosas if (!(pcc->lpcr_mask & LPCR_AIL)) { 30410895ab6SFabiano Rosas /* This CPU does not have AIL */ 30510895ab6SFabiano Rosas return; 30610895ab6SFabiano Rosas } 30710895ab6SFabiano Rosas 30810895ab6SFabiano Rosas /* P8 & P9 */ 30910895ab6SFabiano Rosas if (!(pcc->lpcr_mask & LPCR_HAIL)) { 3108b7e6b07SNicholas Piggin if (!mmu_all_on) { 3118b7e6b07SNicholas Piggin /* AIL only works if MSR[IR] and MSR[DR] are both enabled. */ 3128b7e6b07SNicholas Piggin return; 3138b7e6b07SNicholas Piggin } 3148b7e6b07SNicholas Piggin if (hv_escalation && !(env->spr[SPR_LPCR] & LPCR_HR)) { 3158b7e6b07SNicholas Piggin /* 3168b7e6b07SNicholas Piggin * AIL does not work if there is a MSR[HV] 0->1 transition and the 3178b7e6b07SNicholas Piggin * partition is in HPT mode. For radix guests, such interrupts are 3188b7e6b07SNicholas Piggin * allowed to be delivered to the hypervisor in ail mode. 3198b7e6b07SNicholas Piggin */ 3208b7e6b07SNicholas Piggin return; 3218b7e6b07SNicholas Piggin } 3228b7e6b07SNicholas Piggin 3238b7e6b07SNicholas Piggin ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT; 3248b7e6b07SNicholas Piggin if (ail == 0) { 3258b7e6b07SNicholas Piggin return; 3268b7e6b07SNicholas Piggin } 3278b7e6b07SNicholas Piggin if (ail == 1) { 3288b7e6b07SNicholas Piggin /* AIL=1 is reserved, treat it like AIL=0 */ 3298b7e6b07SNicholas Piggin return; 3308b7e6b07SNicholas Piggin } 331526cdce7SNicholas Piggin 33210895ab6SFabiano Rosas /* P10 and up */ 33310895ab6SFabiano Rosas } else { 334526cdce7SNicholas Piggin if (!mmu_all_on && !hv_escalation) { 335526cdce7SNicholas Piggin /* 336526cdce7SNicholas Piggin * AIL works for HV interrupts even with guest MSR[IR/DR] disabled. 337526cdce7SNicholas Piggin * Guest->guest and HV->HV interrupts do require MMU on. 338526cdce7SNicholas Piggin */ 339526cdce7SNicholas Piggin return; 340526cdce7SNicholas Piggin } 341526cdce7SNicholas Piggin 342526cdce7SNicholas Piggin if (*new_msr & MSR_HVB) { 343526cdce7SNicholas Piggin if (!(env->spr[SPR_LPCR] & LPCR_HAIL)) { 344526cdce7SNicholas Piggin /* HV interrupts depend on LPCR[HAIL] */ 345526cdce7SNicholas Piggin return; 346526cdce7SNicholas Piggin } 347526cdce7SNicholas Piggin ail = 3; /* HAIL=1 gives AIL=3 behaviour for HV interrupts */ 348526cdce7SNicholas Piggin } else { 349526cdce7SNicholas Piggin ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT; 350526cdce7SNicholas Piggin } 351526cdce7SNicholas Piggin if (ail == 0) { 352526cdce7SNicholas Piggin return; 353526cdce7SNicholas Piggin } 354526cdce7SNicholas Piggin if (ail == 1 || ail == 2) { 355526cdce7SNicholas Piggin /* AIL=1 and AIL=2 are reserved, treat them like AIL=0 */ 356526cdce7SNicholas Piggin return; 357526cdce7SNicholas Piggin } 3588b7e6b07SNicholas Piggin } 3598b7e6b07SNicholas Piggin 3608b7e6b07SNicholas Piggin /* 3618b7e6b07SNicholas Piggin * AIL applies, so the new MSR gets IR and DR set, and an offset applied 3628b7e6b07SNicholas Piggin * to the new IP. 3638b7e6b07SNicholas Piggin */ 3648b7e6b07SNicholas Piggin *new_msr |= (1 << MSR_IR) | (1 << MSR_DR); 3658b7e6b07SNicholas Piggin 3668b7e6b07SNicholas Piggin if (excp != POWERPC_EXCP_SYSCALL_VECTORED) { 3678b7e6b07SNicholas Piggin if (ail == 2) { 3688b7e6b07SNicholas Piggin *vector |= 0x0000000000018000ull; 3698b7e6b07SNicholas Piggin } else if (ail == 3) { 3708b7e6b07SNicholas Piggin *vector |= 0xc000000000004000ull; 3718b7e6b07SNicholas Piggin } 3728b7e6b07SNicholas Piggin } else { 3738b7e6b07SNicholas Piggin /* 3748b7e6b07SNicholas Piggin * scv AIL is a little different. AIL=2 does not change the address, 3758b7e6b07SNicholas Piggin * only the MSR. AIL=3 replaces the 0x17000 base with 0xc...3000. 3768b7e6b07SNicholas Piggin */ 3778b7e6b07SNicholas Piggin if (ail == 3) { 3788b7e6b07SNicholas Piggin *vector &= ~0x0000000000017000ull; /* Un-apply the base offset */ 3798b7e6b07SNicholas Piggin *vector |= 0xc000000000003000ull; /* Apply scv's AIL=3 offset */ 3808b7e6b07SNicholas Piggin } 3818b7e6b07SNicholas Piggin } 3822586a4d7SFabiano Rosas } 38328091374SFabiano Rosas #endif 384dead760bSBenjamin Herrenschmidt 3853680e994SNicholas Piggin static void powerpc_reset_excp_state(PowerPCCPU *cpu) 386ad77c6caSNicholas Piggin { 387ad77c6caSNicholas Piggin CPUState *cs = CPU(cpu); 388ad77c6caSNicholas Piggin CPUPPCState *env = &cpu->env; 389ad77c6caSNicholas Piggin 3903680e994SNicholas Piggin /* Reset exception state */ 3913680e994SNicholas Piggin cs->exception_index = POWERPC_EXCP_NONE; 3923680e994SNicholas Piggin env->error_code = 0; 3933680e994SNicholas Piggin } 3943680e994SNicholas Piggin 3953680e994SNicholas Piggin static void powerpc_set_excp_state(PowerPCCPU *cpu, target_ulong vector, 3963680e994SNicholas Piggin target_ulong msr) 3973680e994SNicholas Piggin { 3983680e994SNicholas Piggin CPUPPCState *env = &cpu->env; 3993680e994SNicholas Piggin 400fce9fbafSFabiano Rosas assert((msr & env->msr_mask) == msr); 401fce9fbafSFabiano Rosas 402ad77c6caSNicholas Piggin /* 403ad77c6caSNicholas Piggin * We don't use hreg_store_msr here as already have treated any 404ad77c6caSNicholas Piggin * special case that could occur. Just store MSR and update hflags 405ad77c6caSNicholas Piggin * 406ad77c6caSNicholas Piggin * Note: We *MUST* not use hreg_store_msr() as-is anyway because it 407ad77c6caSNicholas Piggin * will prevent setting of the HV bit which some exceptions might need 408ad77c6caSNicholas Piggin * to do. 409ad77c6caSNicholas Piggin */ 4103680e994SNicholas Piggin env->nip = vector; 411fce9fbafSFabiano Rosas env->msr = msr; 412ad77c6caSNicholas Piggin hreg_compute_hflags(env); 4132fdedcbcSMatheus Ferst ppc_maybe_interrupt(env); 414ad77c6caSNicholas Piggin 4153680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 416ad77c6caSNicholas Piggin 417ad77c6caSNicholas Piggin /* 418ad77c6caSNicholas Piggin * Any interrupt is context synchronizing, check if TCG TLB needs 419ad77c6caSNicholas Piggin * a delayed flush on ppc64 420ad77c6caSNicholas Piggin */ 421ad77c6caSNicholas Piggin check_tlb_flush(env, false); 4223680e994SNicholas Piggin 4233680e994SNicholas Piggin /* Reset the reservation */ 4243680e994SNicholas Piggin env->reserve_addr = -1; 425ad77c6caSNicholas Piggin } 426ad77c6caSNicholas Piggin 427819b31b0SBALATON Zoltan static void powerpc_mcheck_checkstop(CPUPPCState *env) 428819b31b0SBALATON Zoltan { 429819b31b0SBALATON Zoltan CPUState *cs = env_cpu(env); 430819b31b0SBALATON Zoltan 431819b31b0SBALATON Zoltan if (FIELD_EX64(env->msr, MSR, ME)) { 432819b31b0SBALATON Zoltan return; 433819b31b0SBALATON Zoltan } 434819b31b0SBALATON Zoltan 435819b31b0SBALATON Zoltan /* Machine check exception is not enabled. Enter checkstop state. */ 436819b31b0SBALATON Zoltan fprintf(stderr, "Machine check while not allowed. " 437819b31b0SBALATON Zoltan "Entering checkstop state\n"); 438819b31b0SBALATON Zoltan if (qemu_log_separate()) { 439819b31b0SBALATON Zoltan qemu_log("Machine check while not allowed. " 440819b31b0SBALATON Zoltan "Entering checkstop state\n"); 441819b31b0SBALATON Zoltan } 442819b31b0SBALATON Zoltan cs->halted = 1; 443819b31b0SBALATON Zoltan cpu_interrupt_exittb(cs); 444819b31b0SBALATON Zoltan } 445819b31b0SBALATON Zoltan 446e808c2edSFabiano Rosas static void powerpc_excp_40x(PowerPCCPU *cpu, int excp) 447e808c2edSFabiano Rosas { 448e808c2edSFabiano Rosas CPUState *cs = CPU(cpu); 449e808c2edSFabiano Rosas CPUPPCState *env = &cpu->env; 450e808c2edSFabiano Rosas target_ulong msr, new_msr, vector; 4518428cdb2SFabiano Rosas int srr0, srr1; 452e808c2edSFabiano Rosas 453e808c2edSFabiano Rosas /* new srr1 value excluding must-be-zero bits */ 454e808c2edSFabiano Rosas msr = env->msr & ~0x783f0000ULL; 455e808c2edSFabiano Rosas 456e808c2edSFabiano Rosas /* 457495fc7ffSFabiano Rosas * new interrupt handler msr preserves existing ME unless 458495fc7ffSFabiano Rosas * explicitly overriden. 459e808c2edSFabiano Rosas */ 460495fc7ffSFabiano Rosas new_msr = env->msr & (((target_ulong)1 << MSR_ME)); 461e808c2edSFabiano Rosas 462e808c2edSFabiano Rosas /* target registers */ 463e808c2edSFabiano Rosas srr0 = SPR_SRR0; 464e808c2edSFabiano Rosas srr1 = SPR_SRR1; 465e808c2edSFabiano Rosas 466e808c2edSFabiano Rosas /* 467e808c2edSFabiano Rosas * Hypervisor emulation assistance interrupt only exists on server 468495fc7ffSFabiano Rosas * arch 2.05 server or later. 469e808c2edSFabiano Rosas */ 470495fc7ffSFabiano Rosas if (excp == POWERPC_EXCP_HV_EMU) { 471e808c2edSFabiano Rosas excp = POWERPC_EXCP_PROGRAM; 472e808c2edSFabiano Rosas } 473e808c2edSFabiano Rosas 474e808c2edSFabiano Rosas vector = env->excp_vectors[excp]; 475e808c2edSFabiano Rosas if (vector == (target_ulong)-1ULL) { 476e808c2edSFabiano Rosas cpu_abort(cs, "Raised an exception without defined vector %d\n", 477e808c2edSFabiano Rosas excp); 478e808c2edSFabiano Rosas } 479e808c2edSFabiano Rosas 480e808c2edSFabiano Rosas vector |= env->excp_prefix; 481e808c2edSFabiano Rosas 482e808c2edSFabiano Rosas switch (excp) { 483e808c2edSFabiano Rosas case POWERPC_EXCP_CRITICAL: /* Critical input */ 484e808c2edSFabiano Rosas srr0 = SPR_40x_SRR2; 485e808c2edSFabiano Rosas srr1 = SPR_40x_SRR3; 486e808c2edSFabiano Rosas break; 487e808c2edSFabiano Rosas case POWERPC_EXCP_MCHECK: /* Machine check exception */ 488819b31b0SBALATON Zoltan powerpc_mcheck_checkstop(env); 489e808c2edSFabiano Rosas /* machine check exceptions don't have ME set */ 490e808c2edSFabiano Rosas new_msr &= ~((target_ulong)1 << MSR_ME); 491e808c2edSFabiano Rosas 492e808c2edSFabiano Rosas srr0 = SPR_40x_SRR2; 493e808c2edSFabiano Rosas srr1 = SPR_40x_SRR3; 494e808c2edSFabiano Rosas break; 495e808c2edSFabiano Rosas case POWERPC_EXCP_DSI: /* Data storage exception */ 496f9911e1eSFabiano Rosas trace_ppc_excp_dsi(env->spr[SPR_40x_ESR], env->spr[SPR_40x_DEAR]); 497e808c2edSFabiano Rosas break; 498e808c2edSFabiano Rosas case POWERPC_EXCP_ISI: /* Instruction storage exception */ 499e808c2edSFabiano Rosas trace_ppc_excp_isi(msr, env->nip); 500e808c2edSFabiano Rosas break; 501e808c2edSFabiano Rosas case POWERPC_EXCP_EXTERNAL: /* External input */ 502e808c2edSFabiano Rosas break; 503e808c2edSFabiano Rosas case POWERPC_EXCP_ALIGN: /* Alignment exception */ 504e808c2edSFabiano Rosas break; 505e808c2edSFabiano Rosas case POWERPC_EXCP_PROGRAM: /* Program exception */ 506e808c2edSFabiano Rosas switch (env->error_code & ~0xF) { 507e808c2edSFabiano Rosas case POWERPC_EXCP_FP: 508da806a6cSVíctor Colombo if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) { 509e808c2edSFabiano Rosas trace_ppc_excp_fp_ignore(); 5103680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 511e808c2edSFabiano Rosas return; 512e808c2edSFabiano Rosas } 51364e62cfbSFabiano Rosas env->spr[SPR_40x_ESR] = ESR_FP; 514e808c2edSFabiano Rosas break; 515e808c2edSFabiano Rosas case POWERPC_EXCP_INVAL: 516e808c2edSFabiano Rosas trace_ppc_excp_inval(env->nip); 51764e62cfbSFabiano Rosas env->spr[SPR_40x_ESR] = ESR_PIL; 518e808c2edSFabiano Rosas break; 519e808c2edSFabiano Rosas case POWERPC_EXCP_PRIV: 52064e62cfbSFabiano Rosas env->spr[SPR_40x_ESR] = ESR_PPR; 521e808c2edSFabiano Rosas break; 522e808c2edSFabiano Rosas case POWERPC_EXCP_TRAP: 52364e62cfbSFabiano Rosas env->spr[SPR_40x_ESR] = ESR_PTR; 524e808c2edSFabiano Rosas break; 525e808c2edSFabiano Rosas default: 526e808c2edSFabiano Rosas cpu_abort(cs, "Invalid program exception %d. Aborting\n", 527e808c2edSFabiano Rosas env->error_code); 528e808c2edSFabiano Rosas break; 529e808c2edSFabiano Rosas } 530e808c2edSFabiano Rosas break; 531e808c2edSFabiano Rosas case POWERPC_EXCP_SYSCALL: /* System call exception */ 532e808c2edSFabiano Rosas dump_syscall(env); 533e808c2edSFabiano Rosas 534e808c2edSFabiano Rosas /* 535e808c2edSFabiano Rosas * We need to correct the NIP which in this case is supposed 536e808c2edSFabiano Rosas * to point to the next instruction 537e808c2edSFabiano Rosas */ 538e808c2edSFabiano Rosas env->nip += 4; 539e808c2edSFabiano Rosas break; 540e808c2edSFabiano Rosas case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */ 541e808c2edSFabiano Rosas trace_ppc_excp_print("FIT"); 542e808c2edSFabiano Rosas break; 543e808c2edSFabiano Rosas case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */ 544e808c2edSFabiano Rosas trace_ppc_excp_print("WDT"); 545e808c2edSFabiano Rosas break; 546e808c2edSFabiano Rosas case POWERPC_EXCP_DTLB: /* Data TLB error */ 547e808c2edSFabiano Rosas case POWERPC_EXCP_ITLB: /* Instruction TLB error */ 548e808c2edSFabiano Rosas break; 549e808c2edSFabiano Rosas case POWERPC_EXCP_PIT: /* Programmable interval timer interrupt */ 550e808c2edSFabiano Rosas trace_ppc_excp_print("PIT"); 551e808c2edSFabiano Rosas break; 5524d8ac1d1SFabiano Rosas case POWERPC_EXCP_DEBUG: /* Debug interrupt */ 5534d8ac1d1SFabiano Rosas cpu_abort(cs, "%s exception not implemented\n", 5544d8ac1d1SFabiano Rosas powerpc_excp_name(excp)); 5554d8ac1d1SFabiano Rosas break; 556e808c2edSFabiano Rosas default: 557e808c2edSFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 558e808c2edSFabiano Rosas break; 559e808c2edSFabiano Rosas } 560e808c2edSFabiano Rosas 561e808c2edSFabiano Rosas /* Save PC */ 562e808c2edSFabiano Rosas env->spr[srr0] = env->nip; 563e808c2edSFabiano Rosas 564e808c2edSFabiano Rosas /* Save MSR */ 565e808c2edSFabiano Rosas env->spr[srr1] = msr; 566e808c2edSFabiano Rosas 567e808c2edSFabiano Rosas powerpc_set_excp_state(cpu, vector, new_msr); 568e808c2edSFabiano Rosas } 569e808c2edSFabiano Rosas 57058d178fbSFabiano Rosas static void powerpc_excp_6xx(PowerPCCPU *cpu, int excp) 57158d178fbSFabiano Rosas { 57258d178fbSFabiano Rosas CPUState *cs = CPU(cpu); 57358d178fbSFabiano Rosas CPUPPCState *env = &cpu->env; 57458d178fbSFabiano Rosas target_ulong msr, new_msr, vector; 57558d178fbSFabiano Rosas 57658d178fbSFabiano Rosas /* new srr1 value excluding must-be-zero bits */ 57758d178fbSFabiano Rosas msr = env->msr & ~0x783f0000ULL; 57858d178fbSFabiano Rosas 57958d178fbSFabiano Rosas /* 580082d783bSFabiano Rosas * new interrupt handler msr preserves existing ME unless 58158d178fbSFabiano Rosas * explicitly overriden 58258d178fbSFabiano Rosas */ 583082d783bSFabiano Rosas new_msr = env->msr & ((target_ulong)1 << MSR_ME); 58458d178fbSFabiano Rosas 58558d178fbSFabiano Rosas /* 58658d178fbSFabiano Rosas * Hypervisor emulation assistance interrupt only exists on server 587082d783bSFabiano Rosas * arch 2.05 server or later. 58858d178fbSFabiano Rosas */ 589082d783bSFabiano Rosas if (excp == POWERPC_EXCP_HV_EMU) { 59058d178fbSFabiano Rosas excp = POWERPC_EXCP_PROGRAM; 59158d178fbSFabiano Rosas } 59258d178fbSFabiano Rosas 59358d178fbSFabiano Rosas vector = env->excp_vectors[excp]; 59458d178fbSFabiano Rosas if (vector == (target_ulong)-1ULL) { 59558d178fbSFabiano Rosas cpu_abort(cs, "Raised an exception without defined vector %d\n", 59658d178fbSFabiano Rosas excp); 59758d178fbSFabiano Rosas } 59858d178fbSFabiano Rosas 59958d178fbSFabiano Rosas vector |= env->excp_prefix; 60058d178fbSFabiano Rosas 60158d178fbSFabiano Rosas switch (excp) { 60258d178fbSFabiano Rosas case POWERPC_EXCP_CRITICAL: /* Critical input */ 60358d178fbSFabiano Rosas break; 60458d178fbSFabiano Rosas case POWERPC_EXCP_MCHECK: /* Machine check exception */ 605819b31b0SBALATON Zoltan powerpc_mcheck_checkstop(env); 60658d178fbSFabiano Rosas /* machine check exceptions don't have ME set */ 60758d178fbSFabiano Rosas new_msr &= ~((target_ulong)1 << MSR_ME); 60858d178fbSFabiano Rosas 60958d178fbSFabiano Rosas break; 61058d178fbSFabiano Rosas case POWERPC_EXCP_DSI: /* Data storage exception */ 61158d178fbSFabiano Rosas trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]); 61258d178fbSFabiano Rosas break; 61358d178fbSFabiano Rosas case POWERPC_EXCP_ISI: /* Instruction storage exception */ 61458d178fbSFabiano Rosas trace_ppc_excp_isi(msr, env->nip); 61558d178fbSFabiano Rosas msr |= env->error_code; 61658d178fbSFabiano Rosas break; 61758d178fbSFabiano Rosas case POWERPC_EXCP_EXTERNAL: /* External input */ 61858d178fbSFabiano Rosas break; 61958d178fbSFabiano Rosas case POWERPC_EXCP_ALIGN: /* Alignment exception */ 62058d178fbSFabiano Rosas /* Get rS/rD and rA from faulting opcode */ 62158d178fbSFabiano Rosas /* 62258d178fbSFabiano Rosas * Note: the opcode fields will not be set properly for a 62358d178fbSFabiano Rosas * direct store load/store, but nobody cares as nobody 62458d178fbSFabiano Rosas * actually uses direct store segments. 62558d178fbSFabiano Rosas */ 62658d178fbSFabiano Rosas env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16; 62758d178fbSFabiano Rosas break; 62858d178fbSFabiano Rosas case POWERPC_EXCP_PROGRAM: /* Program exception */ 62958d178fbSFabiano Rosas switch (env->error_code & ~0xF) { 63058d178fbSFabiano Rosas case POWERPC_EXCP_FP: 631da806a6cSVíctor Colombo if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) { 63258d178fbSFabiano Rosas trace_ppc_excp_fp_ignore(); 6333680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 63458d178fbSFabiano Rosas return; 63558d178fbSFabiano Rosas } 63658d178fbSFabiano Rosas 63758d178fbSFabiano Rosas /* 63858d178fbSFabiano Rosas * FP exceptions always have NIP pointing to the faulting 63958d178fbSFabiano Rosas * instruction, so always use store_next and claim we are 64058d178fbSFabiano Rosas * precise in the MSR. 64158d178fbSFabiano Rosas */ 64258d178fbSFabiano Rosas msr |= 0x00100000; 64358d178fbSFabiano Rosas break; 64458d178fbSFabiano Rosas case POWERPC_EXCP_INVAL: 64558d178fbSFabiano Rosas trace_ppc_excp_inval(env->nip); 64658d178fbSFabiano Rosas msr |= 0x00080000; 64758d178fbSFabiano Rosas break; 64858d178fbSFabiano Rosas case POWERPC_EXCP_PRIV: 64958d178fbSFabiano Rosas msr |= 0x00040000; 65058d178fbSFabiano Rosas break; 65158d178fbSFabiano Rosas case POWERPC_EXCP_TRAP: 65258d178fbSFabiano Rosas msr |= 0x00020000; 65358d178fbSFabiano Rosas break; 65458d178fbSFabiano Rosas default: 65558d178fbSFabiano Rosas /* Should never occur */ 65658d178fbSFabiano Rosas cpu_abort(cs, "Invalid program exception %d. Aborting\n", 65758d178fbSFabiano Rosas env->error_code); 65858d178fbSFabiano Rosas break; 65958d178fbSFabiano Rosas } 66058d178fbSFabiano Rosas break; 66158d178fbSFabiano Rosas case POWERPC_EXCP_SYSCALL: /* System call exception */ 66258d178fbSFabiano Rosas dump_syscall(env); 66358d178fbSFabiano Rosas 66458d178fbSFabiano Rosas /* 66558d178fbSFabiano Rosas * We need to correct the NIP which in this case is supposed 66658d178fbSFabiano Rosas * to point to the next instruction 66758d178fbSFabiano Rosas */ 66858d178fbSFabiano Rosas env->nip += 4; 66958d178fbSFabiano Rosas break; 67058d178fbSFabiano Rosas case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */ 67158d178fbSFabiano Rosas case POWERPC_EXCP_DECR: /* Decrementer exception */ 67258d178fbSFabiano Rosas break; 67358d178fbSFabiano Rosas case POWERPC_EXCP_DTLB: /* Data TLB error */ 67458d178fbSFabiano Rosas case POWERPC_EXCP_ITLB: /* Instruction TLB error */ 67558d178fbSFabiano Rosas break; 67658d178fbSFabiano Rosas case POWERPC_EXCP_RESET: /* System reset exception */ 6778e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 67858d178fbSFabiano Rosas cpu_abort(cs, "Trying to deliver power-saving system reset " 67958d178fbSFabiano Rosas "exception %d with no HV support\n", excp); 68058d178fbSFabiano Rosas } 68158d178fbSFabiano Rosas break; 68258d178fbSFabiano Rosas case POWERPC_EXCP_TRACE: /* Trace exception */ 68358d178fbSFabiano Rosas break; 68458d178fbSFabiano Rosas case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */ 68558d178fbSFabiano Rosas case POWERPC_EXCP_DLTLB: /* Data load TLB miss */ 68658d178fbSFabiano Rosas case POWERPC_EXCP_DSTLB: /* Data store TLB miss */ 68758d178fbSFabiano Rosas /* Swap temporary saved registers with GPRs */ 68858d178fbSFabiano Rosas if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) { 68958d178fbSFabiano Rosas new_msr |= (target_ulong)1 << MSR_TGPR; 69058d178fbSFabiano Rosas hreg_swap_gpr_tgpr(env); 69158d178fbSFabiano Rosas } 6928f8c7932SFabiano Rosas 69358d178fbSFabiano Rosas ppc_excp_debug_sw_tlb(env, excp); 69458d178fbSFabiano Rosas 69558d178fbSFabiano Rosas msr |= env->crf[0] << 28; 69658d178fbSFabiano Rosas msr |= env->error_code; /* key, D/I, S/L bits */ 69758d178fbSFabiano Rosas /* Set way using a LRU mechanism */ 69858d178fbSFabiano Rosas msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17; 69958d178fbSFabiano Rosas break; 70058d178fbSFabiano Rosas case POWERPC_EXCP_FPA: /* Floating-point assist exception */ 70158d178fbSFabiano Rosas case POWERPC_EXCP_DABR: /* Data address breakpoint */ 70258d178fbSFabiano Rosas case POWERPC_EXCP_IABR: /* Instruction address breakpoint */ 70358d178fbSFabiano Rosas case POWERPC_EXCP_SMI: /* System management interrupt */ 70458d178fbSFabiano Rosas case POWERPC_EXCP_MEXTBR: /* Maskable external breakpoint */ 70558d178fbSFabiano Rosas case POWERPC_EXCP_NMEXTBR: /* Non maskable external breakpoint */ 70658d178fbSFabiano Rosas cpu_abort(cs, "%s exception not implemented\n", 70758d178fbSFabiano Rosas powerpc_excp_name(excp)); 70858d178fbSFabiano Rosas break; 70958d178fbSFabiano Rosas default: 71058d178fbSFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 71158d178fbSFabiano Rosas break; 71258d178fbSFabiano Rosas } 71358d178fbSFabiano Rosas 71458d178fbSFabiano Rosas /* 71558d178fbSFabiano Rosas * Sort out endianness of interrupt, this differs depending on the 71658d178fbSFabiano Rosas * CPU, the HV mode, etc... 71758d178fbSFabiano Rosas */ 71858d178fbSFabiano Rosas if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) { 71958d178fbSFabiano Rosas new_msr |= (target_ulong)1 << MSR_LE; 72058d178fbSFabiano Rosas } 72158d178fbSFabiano Rosas 72258d178fbSFabiano Rosas /* Save PC */ 723c50eaed1SFabiano Rosas env->spr[SPR_SRR0] = env->nip; 72458d178fbSFabiano Rosas 72558d178fbSFabiano Rosas /* Save MSR */ 726c50eaed1SFabiano Rosas env->spr[SPR_SRR1] = msr; 72758d178fbSFabiano Rosas 72858d178fbSFabiano Rosas powerpc_set_excp_state(cpu, vector, new_msr); 72958d178fbSFabiano Rosas } 73058d178fbSFabiano Rosas 731ccfca2fcSFabiano Rosas static void powerpc_excp_7xx(PowerPCCPU *cpu, int excp) 732ccfca2fcSFabiano Rosas { 733ccfca2fcSFabiano Rosas CPUState *cs = CPU(cpu); 734ccfca2fcSFabiano Rosas CPUPPCState *env = &cpu->env; 735ccfca2fcSFabiano Rosas target_ulong msr, new_msr, vector; 736ccfca2fcSFabiano Rosas 737ccfca2fcSFabiano Rosas /* new srr1 value excluding must-be-zero bits */ 738ccfca2fcSFabiano Rosas msr = env->msr & ~0x783f0000ULL; 739ccfca2fcSFabiano Rosas 740ccfca2fcSFabiano Rosas /* 74193848d6aSFabiano Rosas * new interrupt handler msr preserves existing ME unless 742ccfca2fcSFabiano Rosas * explicitly overriden 743ccfca2fcSFabiano Rosas */ 74493848d6aSFabiano Rosas new_msr = env->msr & ((target_ulong)1 << MSR_ME); 745ccfca2fcSFabiano Rosas 746ccfca2fcSFabiano Rosas /* 747ccfca2fcSFabiano Rosas * Hypervisor emulation assistance interrupt only exists on server 74893848d6aSFabiano Rosas * arch 2.05 server or later. 749ccfca2fcSFabiano Rosas */ 75093848d6aSFabiano Rosas if (excp == POWERPC_EXCP_HV_EMU) { 751ccfca2fcSFabiano Rosas excp = POWERPC_EXCP_PROGRAM; 752ccfca2fcSFabiano Rosas } 753ccfca2fcSFabiano Rosas 754ccfca2fcSFabiano Rosas vector = env->excp_vectors[excp]; 755ccfca2fcSFabiano Rosas if (vector == (target_ulong)-1ULL) { 756ccfca2fcSFabiano Rosas cpu_abort(cs, "Raised an exception without defined vector %d\n", 757ccfca2fcSFabiano Rosas excp); 758ccfca2fcSFabiano Rosas } 759ccfca2fcSFabiano Rosas 760ccfca2fcSFabiano Rosas vector |= env->excp_prefix; 761ccfca2fcSFabiano Rosas 762ccfca2fcSFabiano Rosas switch (excp) { 763ccfca2fcSFabiano Rosas case POWERPC_EXCP_MCHECK: /* Machine check exception */ 764819b31b0SBALATON Zoltan powerpc_mcheck_checkstop(env); 765ccfca2fcSFabiano Rosas /* machine check exceptions don't have ME set */ 766ccfca2fcSFabiano Rosas new_msr &= ~((target_ulong)1 << MSR_ME); 767ccfca2fcSFabiano Rosas 768ccfca2fcSFabiano Rosas break; 769ccfca2fcSFabiano Rosas case POWERPC_EXCP_DSI: /* Data storage exception */ 770ccfca2fcSFabiano Rosas trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]); 771ccfca2fcSFabiano Rosas break; 772ccfca2fcSFabiano Rosas case POWERPC_EXCP_ISI: /* Instruction storage exception */ 773ccfca2fcSFabiano Rosas trace_ppc_excp_isi(msr, env->nip); 774ccfca2fcSFabiano Rosas msr |= env->error_code; 775ccfca2fcSFabiano Rosas break; 776ccfca2fcSFabiano Rosas case POWERPC_EXCP_EXTERNAL: /* External input */ 777ccfca2fcSFabiano Rosas break; 778ccfca2fcSFabiano Rosas case POWERPC_EXCP_ALIGN: /* Alignment exception */ 779ccfca2fcSFabiano Rosas /* Get rS/rD and rA from faulting opcode */ 780ccfca2fcSFabiano Rosas /* 781ccfca2fcSFabiano Rosas * Note: the opcode fields will not be set properly for a 782ccfca2fcSFabiano Rosas * direct store load/store, but nobody cares as nobody 783ccfca2fcSFabiano Rosas * actually uses direct store segments. 784ccfca2fcSFabiano Rosas */ 785ccfca2fcSFabiano Rosas env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16; 786ccfca2fcSFabiano Rosas break; 787ccfca2fcSFabiano Rosas case POWERPC_EXCP_PROGRAM: /* Program exception */ 788ccfca2fcSFabiano Rosas switch (env->error_code & ~0xF) { 789ccfca2fcSFabiano Rosas case POWERPC_EXCP_FP: 790da806a6cSVíctor Colombo if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) { 791ccfca2fcSFabiano Rosas trace_ppc_excp_fp_ignore(); 7923680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 793ccfca2fcSFabiano Rosas return; 794ccfca2fcSFabiano Rosas } 795ccfca2fcSFabiano Rosas 796ccfca2fcSFabiano Rosas /* 797ccfca2fcSFabiano Rosas * FP exceptions always have NIP pointing to the faulting 798ccfca2fcSFabiano Rosas * instruction, so always use store_next and claim we are 799ccfca2fcSFabiano Rosas * precise in the MSR. 800ccfca2fcSFabiano Rosas */ 801ccfca2fcSFabiano Rosas msr |= 0x00100000; 802ccfca2fcSFabiano Rosas break; 803ccfca2fcSFabiano Rosas case POWERPC_EXCP_INVAL: 804ccfca2fcSFabiano Rosas trace_ppc_excp_inval(env->nip); 805ccfca2fcSFabiano Rosas msr |= 0x00080000; 806ccfca2fcSFabiano Rosas break; 807ccfca2fcSFabiano Rosas case POWERPC_EXCP_PRIV: 808ccfca2fcSFabiano Rosas msr |= 0x00040000; 809ccfca2fcSFabiano Rosas break; 810ccfca2fcSFabiano Rosas case POWERPC_EXCP_TRAP: 811ccfca2fcSFabiano Rosas msr |= 0x00020000; 812ccfca2fcSFabiano Rosas break; 813ccfca2fcSFabiano Rosas default: 814ccfca2fcSFabiano Rosas /* Should never occur */ 815ccfca2fcSFabiano Rosas cpu_abort(cs, "Invalid program exception %d. Aborting\n", 816ccfca2fcSFabiano Rosas env->error_code); 817ccfca2fcSFabiano Rosas break; 818ccfca2fcSFabiano Rosas } 819ccfca2fcSFabiano Rosas break; 820ccfca2fcSFabiano Rosas case POWERPC_EXCP_SYSCALL: /* System call exception */ 8213b578635SFabiano Rosas { 8223b578635SFabiano Rosas int lev = env->error_code; 823ccfca2fcSFabiano Rosas 8243b578635SFabiano Rosas if (lev == 1 && cpu->vhyp) { 825ccfca2fcSFabiano Rosas dump_hcall(env); 826ccfca2fcSFabiano Rosas } else { 827ccfca2fcSFabiano Rosas dump_syscall(env); 828ccfca2fcSFabiano Rosas } 829ccfca2fcSFabiano Rosas 830ccfca2fcSFabiano Rosas /* 831ccfca2fcSFabiano Rosas * We need to correct the NIP which in this case is supposed 832ccfca2fcSFabiano Rosas * to point to the next instruction 833ccfca2fcSFabiano Rosas */ 834ccfca2fcSFabiano Rosas env->nip += 4; 835ccfca2fcSFabiano Rosas 8363b578635SFabiano Rosas /* 8373b578635SFabiano Rosas * The Virtual Open Firmware (VOF) relies on the 'sc 1' 8383b578635SFabiano Rosas * instruction to communicate with QEMU. The pegasos2 machine 8393b578635SFabiano Rosas * uses VOF and the 7xx CPUs, so although the 7xx don't have 8403b578635SFabiano Rosas * HV mode, we need to keep hypercall support. 8413b578635SFabiano Rosas */ 8423b578635SFabiano Rosas if (lev == 1 && cpu->vhyp) { 843ccfca2fcSFabiano Rosas PPCVirtualHypervisorClass *vhc = 844ccfca2fcSFabiano Rosas PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 845ccfca2fcSFabiano Rosas vhc->hypercall(cpu->vhyp, cpu); 846ccfca2fcSFabiano Rosas return; 847ccfca2fcSFabiano Rosas } 8483b578635SFabiano Rosas 849ccfca2fcSFabiano Rosas break; 8503b578635SFabiano Rosas } 851ccfca2fcSFabiano Rosas case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */ 852ccfca2fcSFabiano Rosas case POWERPC_EXCP_DECR: /* Decrementer exception */ 853ccfca2fcSFabiano Rosas break; 854ccfca2fcSFabiano Rosas case POWERPC_EXCP_RESET: /* System reset exception */ 8558e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 856ccfca2fcSFabiano Rosas cpu_abort(cs, "Trying to deliver power-saving system reset " 857ccfca2fcSFabiano Rosas "exception %d with no HV support\n", excp); 858ccfca2fcSFabiano Rosas } 859ccfca2fcSFabiano Rosas break; 860ccfca2fcSFabiano Rosas case POWERPC_EXCP_TRACE: /* Trace exception */ 861ccfca2fcSFabiano Rosas break; 862ccfca2fcSFabiano Rosas case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */ 863ccfca2fcSFabiano Rosas case POWERPC_EXCP_DLTLB: /* Data load TLB miss */ 864ccfca2fcSFabiano Rosas case POWERPC_EXCP_DSTLB: /* Data store TLB miss */ 865ccfca2fcSFabiano Rosas ppc_excp_debug_sw_tlb(env, excp); 866ccfca2fcSFabiano Rosas 867ccfca2fcSFabiano Rosas msr |= env->crf[0] << 28; 868ccfca2fcSFabiano Rosas msr |= env->error_code; /* key, D/I, S/L bits */ 869ccfca2fcSFabiano Rosas /* Set way using a LRU mechanism */ 870ccfca2fcSFabiano Rosas msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17; 8717df40c54SFabiano Rosas 872ccfca2fcSFabiano Rosas break; 873ccfca2fcSFabiano Rosas case POWERPC_EXCP_IABR: /* Instruction address breakpoint */ 874ccfca2fcSFabiano Rosas case POWERPC_EXCP_SMI: /* System management interrupt */ 875ccfca2fcSFabiano Rosas case POWERPC_EXCP_THERM: /* Thermal interrupt */ 876ccfca2fcSFabiano Rosas case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */ 877ccfca2fcSFabiano Rosas cpu_abort(cs, "%s exception not implemented\n", 878ccfca2fcSFabiano Rosas powerpc_excp_name(excp)); 879ccfca2fcSFabiano Rosas break; 880ccfca2fcSFabiano Rosas default: 881ccfca2fcSFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 882ccfca2fcSFabiano Rosas break; 883ccfca2fcSFabiano Rosas } 884ccfca2fcSFabiano Rosas 885ccfca2fcSFabiano Rosas /* 886ccfca2fcSFabiano Rosas * Sort out endianness of interrupt, this differs depending on the 887ccfca2fcSFabiano Rosas * CPU, the HV mode, etc... 888ccfca2fcSFabiano Rosas */ 889ccfca2fcSFabiano Rosas if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) { 890ccfca2fcSFabiano Rosas new_msr |= (target_ulong)1 << MSR_LE; 891ccfca2fcSFabiano Rosas } 892ccfca2fcSFabiano Rosas 893ccfca2fcSFabiano Rosas /* Save PC */ 894fe4b5c4cSFabiano Rosas env->spr[SPR_SRR0] = env->nip; 895ccfca2fcSFabiano Rosas 896ccfca2fcSFabiano Rosas /* Save MSR */ 897fe4b5c4cSFabiano Rosas env->spr[SPR_SRR1] = msr; 898ccfca2fcSFabiano Rosas 899ccfca2fcSFabiano Rosas powerpc_set_excp_state(cpu, vector, new_msr); 900ccfca2fcSFabiano Rosas } 901ccfca2fcSFabiano Rosas 90252926b0dSFabiano Rosas static void powerpc_excp_74xx(PowerPCCPU *cpu, int excp) 90352926b0dSFabiano Rosas { 90452926b0dSFabiano Rosas CPUState *cs = CPU(cpu); 90552926b0dSFabiano Rosas CPUPPCState *env = &cpu->env; 90652926b0dSFabiano Rosas target_ulong msr, new_msr, vector; 90752926b0dSFabiano Rosas 90852926b0dSFabiano Rosas /* new srr1 value excluding must-be-zero bits */ 90952926b0dSFabiano Rosas msr = env->msr & ~0x783f0000ULL; 91052926b0dSFabiano Rosas 91152926b0dSFabiano Rosas /* 9121f6faf8bSFabiano Rosas * new interrupt handler msr preserves existing ME unless 91352926b0dSFabiano Rosas * explicitly overriden 91452926b0dSFabiano Rosas */ 9151f6faf8bSFabiano Rosas new_msr = env->msr & ((target_ulong)1 << MSR_ME); 91652926b0dSFabiano Rosas 91752926b0dSFabiano Rosas /* 91852926b0dSFabiano Rosas * Hypervisor emulation assistance interrupt only exists on server 9191f6faf8bSFabiano Rosas * arch 2.05 server or later. 92052926b0dSFabiano Rosas */ 9211f6faf8bSFabiano Rosas if (excp == POWERPC_EXCP_HV_EMU) { 92252926b0dSFabiano Rosas excp = POWERPC_EXCP_PROGRAM; 92352926b0dSFabiano Rosas } 92452926b0dSFabiano Rosas 92552926b0dSFabiano Rosas vector = env->excp_vectors[excp]; 92652926b0dSFabiano Rosas if (vector == (target_ulong)-1ULL) { 92752926b0dSFabiano Rosas cpu_abort(cs, "Raised an exception without defined vector %d\n", 92852926b0dSFabiano Rosas excp); 92952926b0dSFabiano Rosas } 93052926b0dSFabiano Rosas 93152926b0dSFabiano Rosas vector |= env->excp_prefix; 93252926b0dSFabiano Rosas 93352926b0dSFabiano Rosas switch (excp) { 93452926b0dSFabiano Rosas case POWERPC_EXCP_MCHECK: /* Machine check exception */ 935819b31b0SBALATON Zoltan powerpc_mcheck_checkstop(env); 93652926b0dSFabiano Rosas /* machine check exceptions don't have ME set */ 93752926b0dSFabiano Rosas new_msr &= ~((target_ulong)1 << MSR_ME); 93852926b0dSFabiano Rosas 93952926b0dSFabiano Rosas break; 94052926b0dSFabiano Rosas case POWERPC_EXCP_DSI: /* Data storage exception */ 94152926b0dSFabiano Rosas trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]); 94252926b0dSFabiano Rosas break; 94352926b0dSFabiano Rosas case POWERPC_EXCP_ISI: /* Instruction storage exception */ 94452926b0dSFabiano Rosas trace_ppc_excp_isi(msr, env->nip); 94552926b0dSFabiano Rosas msr |= env->error_code; 94652926b0dSFabiano Rosas break; 94752926b0dSFabiano Rosas case POWERPC_EXCP_EXTERNAL: /* External input */ 94852926b0dSFabiano Rosas break; 94952926b0dSFabiano Rosas case POWERPC_EXCP_ALIGN: /* Alignment exception */ 95052926b0dSFabiano Rosas /* Get rS/rD and rA from faulting opcode */ 95152926b0dSFabiano Rosas /* 95252926b0dSFabiano Rosas * Note: the opcode fields will not be set properly for a 95352926b0dSFabiano Rosas * direct store load/store, but nobody cares as nobody 95452926b0dSFabiano Rosas * actually uses direct store segments. 95552926b0dSFabiano Rosas */ 95652926b0dSFabiano Rosas env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16; 95752926b0dSFabiano Rosas break; 95852926b0dSFabiano Rosas case POWERPC_EXCP_PROGRAM: /* Program exception */ 95952926b0dSFabiano Rosas switch (env->error_code & ~0xF) { 96052926b0dSFabiano Rosas case POWERPC_EXCP_FP: 961da806a6cSVíctor Colombo if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) { 96252926b0dSFabiano Rosas trace_ppc_excp_fp_ignore(); 9633680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 96452926b0dSFabiano Rosas return; 96552926b0dSFabiano Rosas } 96652926b0dSFabiano Rosas 96752926b0dSFabiano Rosas /* 96852926b0dSFabiano Rosas * FP exceptions always have NIP pointing to the faulting 96952926b0dSFabiano Rosas * instruction, so always use store_next and claim we are 97052926b0dSFabiano Rosas * precise in the MSR. 97152926b0dSFabiano Rosas */ 97252926b0dSFabiano Rosas msr |= 0x00100000; 97352926b0dSFabiano Rosas break; 97452926b0dSFabiano Rosas case POWERPC_EXCP_INVAL: 97552926b0dSFabiano Rosas trace_ppc_excp_inval(env->nip); 97652926b0dSFabiano Rosas msr |= 0x00080000; 97752926b0dSFabiano Rosas break; 97852926b0dSFabiano Rosas case POWERPC_EXCP_PRIV: 97952926b0dSFabiano Rosas msr |= 0x00040000; 98052926b0dSFabiano Rosas break; 98152926b0dSFabiano Rosas case POWERPC_EXCP_TRAP: 98252926b0dSFabiano Rosas msr |= 0x00020000; 98352926b0dSFabiano Rosas break; 98452926b0dSFabiano Rosas default: 98552926b0dSFabiano Rosas /* Should never occur */ 98652926b0dSFabiano Rosas cpu_abort(cs, "Invalid program exception %d. Aborting\n", 98752926b0dSFabiano Rosas env->error_code); 98852926b0dSFabiano Rosas break; 98952926b0dSFabiano Rosas } 99052926b0dSFabiano Rosas break; 99152926b0dSFabiano Rosas case POWERPC_EXCP_SYSCALL: /* System call exception */ 992bca2c6d9SFabiano Rosas { 993bca2c6d9SFabiano Rosas int lev = env->error_code; 99452926b0dSFabiano Rosas 9952306c606SBALATON Zoltan if (lev == 1 && cpu->vhyp) { 99652926b0dSFabiano Rosas dump_hcall(env); 99752926b0dSFabiano Rosas } else { 99852926b0dSFabiano Rosas dump_syscall(env); 99952926b0dSFabiano Rosas } 100052926b0dSFabiano Rosas 100152926b0dSFabiano Rosas /* 100252926b0dSFabiano Rosas * We need to correct the NIP which in this case is supposed 100352926b0dSFabiano Rosas * to point to the next instruction 100452926b0dSFabiano Rosas */ 100552926b0dSFabiano Rosas env->nip += 4; 100652926b0dSFabiano Rosas 1007bca2c6d9SFabiano Rosas /* 1008bca2c6d9SFabiano Rosas * The Virtual Open Firmware (VOF) relies on the 'sc 1' 1009bca2c6d9SFabiano Rosas * instruction to communicate with QEMU. The pegasos2 machine 1010bca2c6d9SFabiano Rosas * uses VOF and the 74xx CPUs, so although the 74xx don't have 1011bca2c6d9SFabiano Rosas * HV mode, we need to keep hypercall support. 1012bca2c6d9SFabiano Rosas */ 10132306c606SBALATON Zoltan if (lev == 1 && cpu->vhyp) { 101452926b0dSFabiano Rosas PPCVirtualHypervisorClass *vhc = 101552926b0dSFabiano Rosas PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 101652926b0dSFabiano Rosas vhc->hypercall(cpu->vhyp, cpu); 101752926b0dSFabiano Rosas return; 101852926b0dSFabiano Rosas } 1019bca2c6d9SFabiano Rosas 102052926b0dSFabiano Rosas break; 1021bca2c6d9SFabiano Rosas } 102252926b0dSFabiano Rosas case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */ 102352926b0dSFabiano Rosas case POWERPC_EXCP_DECR: /* Decrementer exception */ 102452926b0dSFabiano Rosas break; 102552926b0dSFabiano Rosas case POWERPC_EXCP_RESET: /* System reset exception */ 10268e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 102752926b0dSFabiano Rosas cpu_abort(cs, "Trying to deliver power-saving system reset " 102852926b0dSFabiano Rosas "exception %d with no HV support\n", excp); 102952926b0dSFabiano Rosas } 103052926b0dSFabiano Rosas break; 103152926b0dSFabiano Rosas case POWERPC_EXCP_TRACE: /* Trace exception */ 103252926b0dSFabiano Rosas break; 103352926b0dSFabiano Rosas case POWERPC_EXCP_VPU: /* Vector unavailable exception */ 103452926b0dSFabiano Rosas break; 103552926b0dSFabiano Rosas case POWERPC_EXCP_IABR: /* Instruction address breakpoint */ 103652926b0dSFabiano Rosas case POWERPC_EXCP_SMI: /* System management interrupt */ 103752926b0dSFabiano Rosas case POWERPC_EXCP_THERM: /* Thermal interrupt */ 103852926b0dSFabiano Rosas case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */ 103952926b0dSFabiano Rosas case POWERPC_EXCP_VPUA: /* Vector assist exception */ 104052926b0dSFabiano Rosas cpu_abort(cs, "%s exception not implemented\n", 104152926b0dSFabiano Rosas powerpc_excp_name(excp)); 104252926b0dSFabiano Rosas break; 104352926b0dSFabiano Rosas default: 104452926b0dSFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 104552926b0dSFabiano Rosas break; 104652926b0dSFabiano Rosas } 104752926b0dSFabiano Rosas 104852926b0dSFabiano Rosas /* 104952926b0dSFabiano Rosas * Sort out endianness of interrupt, this differs depending on the 105052926b0dSFabiano Rosas * CPU, the HV mode, etc... 105152926b0dSFabiano Rosas */ 105252926b0dSFabiano Rosas if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) { 105352926b0dSFabiano Rosas new_msr |= (target_ulong)1 << MSR_LE; 105452926b0dSFabiano Rosas } 105552926b0dSFabiano Rosas 105652926b0dSFabiano Rosas /* Save PC */ 1057f82db777SFabiano Rosas env->spr[SPR_SRR0] = env->nip; 105852926b0dSFabiano Rosas 105952926b0dSFabiano Rosas /* Save MSR */ 1060f82db777SFabiano Rosas env->spr[SPR_SRR1] = msr; 106152926b0dSFabiano Rosas 106252926b0dSFabiano Rosas powerpc_set_excp_state(cpu, vector, new_msr); 106352926b0dSFabiano Rosas } 106452926b0dSFabiano Rosas 1065180952ceSFabiano Rosas static void powerpc_excp_booke(PowerPCCPU *cpu, int excp) 1066180952ceSFabiano Rosas { 1067180952ceSFabiano Rosas CPUState *cs = CPU(cpu); 1068180952ceSFabiano Rosas CPUPPCState *env = &cpu->env; 1069180952ceSFabiano Rosas target_ulong msr, new_msr, vector; 1070904e8428SFabiano Rosas int srr0, srr1; 1071180952ceSFabiano Rosas 1072180952ceSFabiano Rosas msr = env->msr; 1073180952ceSFabiano Rosas 1074180952ceSFabiano Rosas /* 10759dc20cc3SFabiano Rosas * new interrupt handler msr preserves existing ME unless 1076180952ceSFabiano Rosas * explicitly overriden 1077180952ceSFabiano Rosas */ 10789dc20cc3SFabiano Rosas new_msr = env->msr & ((target_ulong)1 << MSR_ME); 1079180952ceSFabiano Rosas 1080180952ceSFabiano Rosas /* target registers */ 1081180952ceSFabiano Rosas srr0 = SPR_SRR0; 1082180952ceSFabiano Rosas srr1 = SPR_SRR1; 1083180952ceSFabiano Rosas 1084180952ceSFabiano Rosas /* 1085180952ceSFabiano Rosas * Hypervisor emulation assistance interrupt only exists on server 10869dc20cc3SFabiano Rosas * arch 2.05 server or later. 1087180952ceSFabiano Rosas */ 10889dc20cc3SFabiano Rosas if (excp == POWERPC_EXCP_HV_EMU) { 1089180952ceSFabiano Rosas excp = POWERPC_EXCP_PROGRAM; 1090180952ceSFabiano Rosas } 1091180952ceSFabiano Rosas 1092180952ceSFabiano Rosas #ifdef TARGET_PPC64 1093180952ceSFabiano Rosas /* 1094180952ceSFabiano Rosas * SPEU and VPU share the same IVOR but they exist in different 1095180952ceSFabiano Rosas * processors. SPEU is e500v1/2 only and VPU is e6500 only. 1096180952ceSFabiano Rosas */ 10979dc20cc3SFabiano Rosas if (excp == POWERPC_EXCP_VPU) { 1098180952ceSFabiano Rosas excp = POWERPC_EXCP_SPEU; 1099180952ceSFabiano Rosas } 1100180952ceSFabiano Rosas #endif 1101180952ceSFabiano Rosas 1102180952ceSFabiano Rosas vector = env->excp_vectors[excp]; 1103180952ceSFabiano Rosas if (vector == (target_ulong)-1ULL) { 1104180952ceSFabiano Rosas cpu_abort(cs, "Raised an exception without defined vector %d\n", 1105180952ceSFabiano Rosas excp); 1106180952ceSFabiano Rosas } 1107180952ceSFabiano Rosas 1108180952ceSFabiano Rosas vector |= env->excp_prefix; 1109180952ceSFabiano Rosas 1110180952ceSFabiano Rosas switch (excp) { 1111180952ceSFabiano Rosas case POWERPC_EXCP_CRITICAL: /* Critical input */ 1112180952ceSFabiano Rosas srr0 = SPR_BOOKE_CSRR0; 1113180952ceSFabiano Rosas srr1 = SPR_BOOKE_CSRR1; 1114180952ceSFabiano Rosas break; 1115180952ceSFabiano Rosas case POWERPC_EXCP_MCHECK: /* Machine check exception */ 1116819b31b0SBALATON Zoltan powerpc_mcheck_checkstop(env); 1117180952ceSFabiano Rosas /* machine check exceptions don't have ME set */ 1118180952ceSFabiano Rosas new_msr &= ~((target_ulong)1 << MSR_ME); 1119180952ceSFabiano Rosas 1120180952ceSFabiano Rosas /* FIXME: choose one or the other based on CPU type */ 1121180952ceSFabiano Rosas srr0 = SPR_BOOKE_MCSRR0; 1122180952ceSFabiano Rosas srr1 = SPR_BOOKE_MCSRR1; 1123180952ceSFabiano Rosas 1124180952ceSFabiano Rosas env->spr[SPR_BOOKE_CSRR0] = env->nip; 1125180952ceSFabiano Rosas env->spr[SPR_BOOKE_CSRR1] = msr; 1126db403211SFabiano Rosas 1127180952ceSFabiano Rosas break; 1128180952ceSFabiano Rosas case POWERPC_EXCP_DSI: /* Data storage exception */ 1129afdbc869SFabiano Rosas trace_ppc_excp_dsi(env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]); 1130180952ceSFabiano Rosas break; 1131180952ceSFabiano Rosas case POWERPC_EXCP_ISI: /* Instruction storage exception */ 1132180952ceSFabiano Rosas trace_ppc_excp_isi(msr, env->nip); 1133180952ceSFabiano Rosas break; 1134180952ceSFabiano Rosas case POWERPC_EXCP_EXTERNAL: /* External input */ 1135180952ceSFabiano Rosas if (env->mpic_proxy) { 1136180952ceSFabiano Rosas /* IACK the IRQ on delivery */ 1137180952ceSFabiano Rosas env->spr[SPR_BOOKE_EPR] = ldl_phys(cs->as, env->mpic_iack); 1138180952ceSFabiano Rosas } 1139180952ceSFabiano Rosas break; 1140180952ceSFabiano Rosas case POWERPC_EXCP_ALIGN: /* Alignment exception */ 1141180952ceSFabiano Rosas break; 1142180952ceSFabiano Rosas case POWERPC_EXCP_PROGRAM: /* Program exception */ 1143180952ceSFabiano Rosas switch (env->error_code & ~0xF) { 1144180952ceSFabiano Rosas case POWERPC_EXCP_FP: 1145da806a6cSVíctor Colombo if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) { 1146180952ceSFabiano Rosas trace_ppc_excp_fp_ignore(); 11473680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 1148180952ceSFabiano Rosas return; 1149180952ceSFabiano Rosas } 1150180952ceSFabiano Rosas 1151180952ceSFabiano Rosas /* 1152180952ceSFabiano Rosas * FP exceptions always have NIP pointing to the faulting 1153180952ceSFabiano Rosas * instruction, so always use store_next and claim we are 1154180952ceSFabiano Rosas * precise in the MSR. 1155180952ceSFabiano Rosas */ 1156180952ceSFabiano Rosas msr |= 0x00100000; 1157180952ceSFabiano Rosas env->spr[SPR_BOOKE_ESR] = ESR_FP; 1158180952ceSFabiano Rosas break; 1159180952ceSFabiano Rosas case POWERPC_EXCP_INVAL: 1160180952ceSFabiano Rosas trace_ppc_excp_inval(env->nip); 1161180952ceSFabiano Rosas msr |= 0x00080000; 1162180952ceSFabiano Rosas env->spr[SPR_BOOKE_ESR] = ESR_PIL; 1163180952ceSFabiano Rosas break; 1164180952ceSFabiano Rosas case POWERPC_EXCP_PRIV: 1165180952ceSFabiano Rosas msr |= 0x00040000; 1166180952ceSFabiano Rosas env->spr[SPR_BOOKE_ESR] = ESR_PPR; 1167180952ceSFabiano Rosas break; 1168180952ceSFabiano Rosas case POWERPC_EXCP_TRAP: 1169180952ceSFabiano Rosas msr |= 0x00020000; 1170180952ceSFabiano Rosas env->spr[SPR_BOOKE_ESR] = ESR_PTR; 1171180952ceSFabiano Rosas break; 1172180952ceSFabiano Rosas default: 1173180952ceSFabiano Rosas /* Should never occur */ 1174180952ceSFabiano Rosas cpu_abort(cs, "Invalid program exception %d. Aborting\n", 1175180952ceSFabiano Rosas env->error_code); 1176180952ceSFabiano Rosas break; 1177180952ceSFabiano Rosas } 1178180952ceSFabiano Rosas break; 1179180952ceSFabiano Rosas case POWERPC_EXCP_SYSCALL: /* System call exception */ 1180180952ceSFabiano Rosas dump_syscall(env); 1181180952ceSFabiano Rosas 1182180952ceSFabiano Rosas /* 1183180952ceSFabiano Rosas * We need to correct the NIP which in this case is supposed 1184180952ceSFabiano Rosas * to point to the next instruction 1185180952ceSFabiano Rosas */ 1186180952ceSFabiano Rosas env->nip += 4; 1187180952ceSFabiano Rosas break; 1188180952ceSFabiano Rosas case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */ 1189180952ceSFabiano Rosas case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */ 1190180952ceSFabiano Rosas case POWERPC_EXCP_DECR: /* Decrementer exception */ 1191180952ceSFabiano Rosas break; 1192180952ceSFabiano Rosas case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */ 1193180952ceSFabiano Rosas /* FIT on 4xx */ 1194180952ceSFabiano Rosas trace_ppc_excp_print("FIT"); 1195180952ceSFabiano Rosas break; 1196180952ceSFabiano Rosas case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */ 1197180952ceSFabiano Rosas trace_ppc_excp_print("WDT"); 1198180952ceSFabiano Rosas srr0 = SPR_BOOKE_CSRR0; 1199180952ceSFabiano Rosas srr1 = SPR_BOOKE_CSRR1; 1200180952ceSFabiano Rosas break; 1201180952ceSFabiano Rosas case POWERPC_EXCP_DTLB: /* Data TLB error */ 1202180952ceSFabiano Rosas case POWERPC_EXCP_ITLB: /* Instruction TLB error */ 1203180952ceSFabiano Rosas break; 1204180952ceSFabiano Rosas case POWERPC_EXCP_DEBUG: /* Debug interrupt */ 1205180952ceSFabiano Rosas if (env->flags & POWERPC_FLAG_DE) { 1206180952ceSFabiano Rosas /* FIXME: choose one or the other based on CPU type */ 1207180952ceSFabiano Rosas srr0 = SPR_BOOKE_DSRR0; 1208180952ceSFabiano Rosas srr1 = SPR_BOOKE_DSRR1; 1209180952ceSFabiano Rosas 1210180952ceSFabiano Rosas env->spr[SPR_BOOKE_CSRR0] = env->nip; 1211180952ceSFabiano Rosas env->spr[SPR_BOOKE_CSRR1] = msr; 1212180952ceSFabiano Rosas 1213180952ceSFabiano Rosas /* DBSR already modified by caller */ 1214180952ceSFabiano Rosas } else { 1215180952ceSFabiano Rosas cpu_abort(cs, "Debug exception triggered on unsupported model\n"); 1216180952ceSFabiano Rosas } 1217180952ceSFabiano Rosas break; 1218180952ceSFabiano Rosas case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavailable/VPU */ 1219180952ceSFabiano Rosas env->spr[SPR_BOOKE_ESR] = ESR_SPV; 1220180952ceSFabiano Rosas break; 12219364df26SNicholas Piggin case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */ 12229364df26SNicholas Piggin break; 12239364df26SNicholas Piggin case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */ 12249364df26SNicholas Piggin srr0 = SPR_BOOKE_CSRR0; 12259364df26SNicholas Piggin srr1 = SPR_BOOKE_CSRR1; 12269364df26SNicholas Piggin break; 1227180952ceSFabiano Rosas case POWERPC_EXCP_RESET: /* System reset exception */ 12288e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 1229180952ceSFabiano Rosas cpu_abort(cs, "Trying to deliver power-saving system reset " 1230180952ceSFabiano Rosas "exception %d with no HV support\n", excp); 1231180952ceSFabiano Rosas } 1232180952ceSFabiano Rosas break; 1233180952ceSFabiano Rosas case POWERPC_EXCP_EFPDI: /* Embedded floating-point data interrupt */ 1234180952ceSFabiano Rosas case POWERPC_EXCP_EFPRI: /* Embedded floating-point round interrupt */ 1235180952ceSFabiano Rosas cpu_abort(cs, "%s exception not implemented\n", 1236180952ceSFabiano Rosas powerpc_excp_name(excp)); 1237180952ceSFabiano Rosas break; 1238180952ceSFabiano Rosas default: 1239180952ceSFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 1240180952ceSFabiano Rosas break; 1241180952ceSFabiano Rosas } 1242180952ceSFabiano Rosas 1243180952ceSFabiano Rosas #if defined(TARGET_PPC64) 1244180952ceSFabiano Rosas if (env->spr[SPR_BOOKE_EPCR] & EPCR_ICM) { 1245180952ceSFabiano Rosas /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */ 1246180952ceSFabiano Rosas new_msr |= (target_ulong)1 << MSR_CM; 1247180952ceSFabiano Rosas } else { 1248180952ceSFabiano Rosas vector = (uint32_t)vector; 1249180952ceSFabiano Rosas } 1250180952ceSFabiano Rosas #endif 1251180952ceSFabiano Rosas 1252180952ceSFabiano Rosas /* Save PC */ 1253180952ceSFabiano Rosas env->spr[srr0] = env->nip; 1254180952ceSFabiano Rosas 1255180952ceSFabiano Rosas /* Save MSR */ 1256180952ceSFabiano Rosas env->spr[srr1] = msr; 1257180952ceSFabiano Rosas 1258180952ceSFabiano Rosas powerpc_set_excp_state(cpu, vector, new_msr); 1259180952ceSFabiano Rosas } 1260180952ceSFabiano Rosas 12617cebc5dbSNicholas Piggin /* 12627cebc5dbSNicholas Piggin * When running a nested HV guest under vhyp, external interrupts are 12637cebc5dbSNicholas Piggin * delivered as HVIRT. 12647cebc5dbSNicholas Piggin */ 12657cebc5dbSNicholas Piggin static bool books_vhyp_promotes_external_to_hvirt(PowerPCCPU *cpu) 12667cebc5dbSNicholas Piggin { 12677cebc5dbSNicholas Piggin if (cpu->vhyp) { 12687cebc5dbSNicholas Piggin return vhyp_cpu_in_nested(cpu); 12697cebc5dbSNicholas Piggin } 12707cebc5dbSNicholas Piggin return false; 12717cebc5dbSNicholas Piggin } 12727cebc5dbSNicholas Piggin 127330c4e426SFabiano Rosas #ifdef TARGET_PPC64 12744c6cf6b2SNicholas Piggin /* 12754c6cf6b2SNicholas Piggin * When running under vhyp, hcalls are always intercepted and sent to the 12764c6cf6b2SNicholas Piggin * vhc->hypercall handler. 12774c6cf6b2SNicholas Piggin */ 12784c6cf6b2SNicholas Piggin static bool books_vhyp_handles_hcall(PowerPCCPU *cpu) 12794c6cf6b2SNicholas Piggin { 12804c6cf6b2SNicholas Piggin if (cpu->vhyp) { 12817cebc5dbSNicholas Piggin return !vhyp_cpu_in_nested(cpu); 12827cebc5dbSNicholas Piggin } 12837cebc5dbSNicholas Piggin return false; 12847cebc5dbSNicholas Piggin } 12857cebc5dbSNicholas Piggin 12867cebc5dbSNicholas Piggin /* 12877cebc5dbSNicholas Piggin * When running a nested KVM HV guest under vhyp, HV exceptions are not 12887cebc5dbSNicholas Piggin * delivered to the guest (because there is no concept of HV support), but 12897cebc5dbSNicholas Piggin * rather they are sent tothe vhyp to exit from the L2 back to the L1 and 12907cebc5dbSNicholas Piggin * return from the H_ENTER_NESTED hypercall. 12917cebc5dbSNicholas Piggin */ 12927cebc5dbSNicholas Piggin static bool books_vhyp_handles_hv_excp(PowerPCCPU *cpu) 12937cebc5dbSNicholas Piggin { 12947cebc5dbSNicholas Piggin if (cpu->vhyp) { 12957cebc5dbSNicholas Piggin return vhyp_cpu_in_nested(cpu); 12964c6cf6b2SNicholas Piggin } 12974c6cf6b2SNicholas Piggin return false; 12984c6cf6b2SNicholas Piggin } 12994c6cf6b2SNicholas Piggin 13005a5d3b23SNicholas Piggin #ifdef CONFIG_TCG 13015a5d3b23SNicholas Piggin static bool is_prefix_insn(CPUPPCState *env, uint32_t insn) 13025a5d3b23SNicholas Piggin { 13035a5d3b23SNicholas Piggin if (!(env->insns_flags2 & PPC2_ISA310)) { 13045a5d3b23SNicholas Piggin return false; 13055a5d3b23SNicholas Piggin } 13065a5d3b23SNicholas Piggin return ((insn & 0xfc000000) == 0x04000000); 13075a5d3b23SNicholas Piggin } 13085a5d3b23SNicholas Piggin 13095a5d3b23SNicholas Piggin static bool is_prefix_insn_excp(PowerPCCPU *cpu, int excp) 13105a5d3b23SNicholas Piggin { 13115a5d3b23SNicholas Piggin CPUPPCState *env = &cpu->env; 13125a5d3b23SNicholas Piggin 13135a5d3b23SNicholas Piggin if (!tcg_enabled()) { 13145a5d3b23SNicholas Piggin /* 13155a5d3b23SNicholas Piggin * This does not load instructions and set the prefix bit correctly 13165a5d3b23SNicholas Piggin * for injected interrupts with KVM. That may have to be discovered 13175a5d3b23SNicholas Piggin * and set by the KVM layer before injecting. 13185a5d3b23SNicholas Piggin */ 13195a5d3b23SNicholas Piggin return false; 13205a5d3b23SNicholas Piggin } 13215a5d3b23SNicholas Piggin 13225a5d3b23SNicholas Piggin switch (excp) { 13235a5d3b23SNicholas Piggin case POWERPC_EXCP_HDSI: 13245a5d3b23SNicholas Piggin /* HDSI PRTABLE_FAULT has the originating access type in error_code */ 13255a5d3b23SNicholas Piggin if ((env->spr[SPR_HDSISR] & DSISR_PRTABLE_FAULT) && 13265a5d3b23SNicholas Piggin (env->error_code == MMU_INST_FETCH)) { 13275a5d3b23SNicholas Piggin /* 13285a5d3b23SNicholas Piggin * Fetch failed due to partition scope translation, so prefix 13295a5d3b23SNicholas Piggin * indication is not relevant (and attempting to load the 13305a5d3b23SNicholas Piggin * instruction at NIP would cause recursive faults with the same 13315a5d3b23SNicholas Piggin * translation). 13325a5d3b23SNicholas Piggin */ 13335a5d3b23SNicholas Piggin break; 13345a5d3b23SNicholas Piggin } 13355a5d3b23SNicholas Piggin /* fall through */ 13365a5d3b23SNicholas Piggin case POWERPC_EXCP_MCHECK: 13375a5d3b23SNicholas Piggin case POWERPC_EXCP_DSI: 13385a5d3b23SNicholas Piggin case POWERPC_EXCP_DSEG: 13395a5d3b23SNicholas Piggin case POWERPC_EXCP_ALIGN: 13405a5d3b23SNicholas Piggin case POWERPC_EXCP_PROGRAM: 13415a5d3b23SNicholas Piggin case POWERPC_EXCP_FPU: 13425a5d3b23SNicholas Piggin case POWERPC_EXCP_TRACE: 13435a5d3b23SNicholas Piggin case POWERPC_EXCP_HV_EMU: 13445a5d3b23SNicholas Piggin case POWERPC_EXCP_VPU: 13455a5d3b23SNicholas Piggin case POWERPC_EXCP_VSXU: 13465a5d3b23SNicholas Piggin case POWERPC_EXCP_FU: 13475a5d3b23SNicholas Piggin case POWERPC_EXCP_HV_FU: { 13485a5d3b23SNicholas Piggin uint32_t insn = ppc_ldl_code(env, env->nip); 13495a5d3b23SNicholas Piggin if (is_prefix_insn(env, insn)) { 13505a5d3b23SNicholas Piggin return true; 13515a5d3b23SNicholas Piggin } 13525a5d3b23SNicholas Piggin break; 13535a5d3b23SNicholas Piggin } 13545a5d3b23SNicholas Piggin default: 13555a5d3b23SNicholas Piggin break; 13565a5d3b23SNicholas Piggin } 13575a5d3b23SNicholas Piggin return false; 13585a5d3b23SNicholas Piggin } 13595a5d3b23SNicholas Piggin #else 13605a5d3b23SNicholas Piggin static bool is_prefix_insn_excp(PowerPCCPU *cpu, int excp) 13615a5d3b23SNicholas Piggin { 13625a5d3b23SNicholas Piggin return false; 13635a5d3b23SNicholas Piggin } 13645a5d3b23SNicholas Piggin #endif 13655a5d3b23SNicholas Piggin 13669f338e4dSFabiano Rosas static void powerpc_excp_books(PowerPCCPU *cpu, int excp) 13679f338e4dSFabiano Rosas { 13689f338e4dSFabiano Rosas CPUState *cs = CPU(cpu); 13699f338e4dSFabiano Rosas CPUPPCState *env = &cpu->env; 13709f338e4dSFabiano Rosas target_ulong msr, new_msr, vector; 13719f338e4dSFabiano Rosas int srr0, srr1, lev = -1; 13729f338e4dSFabiano Rosas 13739f338e4dSFabiano Rosas /* new srr1 value excluding must-be-zero bits */ 13749f338e4dSFabiano Rosas msr = env->msr & ~0x783f0000ULL; 13759f338e4dSFabiano Rosas 13769f338e4dSFabiano Rosas /* 13779f338e4dSFabiano Rosas * new interrupt handler msr preserves existing HV and ME unless 13789f338e4dSFabiano Rosas * explicitly overriden 13799f338e4dSFabiano Rosas */ 13809f338e4dSFabiano Rosas new_msr = env->msr & (((target_ulong)1 << MSR_ME) | MSR_HVB); 13819f338e4dSFabiano Rosas 13829f338e4dSFabiano Rosas /* target registers */ 13839f338e4dSFabiano Rosas srr0 = SPR_SRR0; 13849f338e4dSFabiano Rosas srr1 = SPR_SRR1; 13859f338e4dSFabiano Rosas 13869f338e4dSFabiano Rosas /* 13879f338e4dSFabiano Rosas * check for special resume at 0x100 from doze/nap/sleep/winkle on 13889f338e4dSFabiano Rosas * P7/P8/P9 13899f338e4dSFabiano Rosas */ 13909f338e4dSFabiano Rosas if (env->resume_as_sreset) { 13913f88a89dSBALATON Zoltan excp = powerpc_reset_wakeup(env, excp, &msr); 13929f338e4dSFabiano Rosas } 13939f338e4dSFabiano Rosas 13949f338e4dSFabiano Rosas /* 139530c4e426SFabiano Rosas * We don't want to generate a Hypervisor Emulation Assistance 13966c242e79SNicholas Piggin * Interrupt if we don't have HVB in msr_mask (PAPR mode), 13976c242e79SNicholas Piggin * unless running a nested-hv guest, in which case the L1 13986c242e79SNicholas Piggin * kernel wants the interrupt. 13999f338e4dSFabiano Rosas */ 14006c242e79SNicholas Piggin if (excp == POWERPC_EXCP_HV_EMU && !(env->msr_mask & MSR_HVB) && 14016c242e79SNicholas Piggin !books_vhyp_handles_hv_excp(cpu)) { 14029f338e4dSFabiano Rosas excp = POWERPC_EXCP_PROGRAM; 14039f338e4dSFabiano Rosas } 14049f338e4dSFabiano Rosas 14059f338e4dSFabiano Rosas vector = env->excp_vectors[excp]; 14069f338e4dSFabiano Rosas if (vector == (target_ulong)-1ULL) { 14079f338e4dSFabiano Rosas cpu_abort(cs, "Raised an exception without defined vector %d\n", 14089f338e4dSFabiano Rosas excp); 14099f338e4dSFabiano Rosas } 14109f338e4dSFabiano Rosas 14119f338e4dSFabiano Rosas vector |= env->excp_prefix; 14129f338e4dSFabiano Rosas 14135a5d3b23SNicholas Piggin if (is_prefix_insn_excp(cpu, excp)) { 14145a5d3b23SNicholas Piggin msr |= PPC_BIT(34); 14155a5d3b23SNicholas Piggin } 14165a5d3b23SNicholas Piggin 14179f338e4dSFabiano Rosas switch (excp) { 14189f338e4dSFabiano Rosas case POWERPC_EXCP_MCHECK: /* Machine check exception */ 1419819b31b0SBALATON Zoltan powerpc_mcheck_checkstop(env); 14209f338e4dSFabiano Rosas if (env->msr_mask & MSR_HVB) { 14219f338e4dSFabiano Rosas /* 14229f338e4dSFabiano Rosas * ISA specifies HV, but can be delivered to guest with HV 14239f338e4dSFabiano Rosas * clear (e.g., see FWNMI in PAPR). 14249f338e4dSFabiano Rosas */ 14259f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 14269f338e4dSFabiano Rosas } 14279f338e4dSFabiano Rosas 14289f338e4dSFabiano Rosas /* machine check exceptions don't have ME set */ 14299f338e4dSFabiano Rosas new_msr &= ~((target_ulong)1 << MSR_ME); 14309f338e4dSFabiano Rosas 14319f338e4dSFabiano Rosas break; 14329f338e4dSFabiano Rosas case POWERPC_EXCP_DSI: /* Data storage exception */ 14339f338e4dSFabiano Rosas trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]); 14349f338e4dSFabiano Rosas break; 14359f338e4dSFabiano Rosas case POWERPC_EXCP_ISI: /* Instruction storage exception */ 14369f338e4dSFabiano Rosas trace_ppc_excp_isi(msr, env->nip); 14379f338e4dSFabiano Rosas msr |= env->error_code; 14389f338e4dSFabiano Rosas break; 14399f338e4dSFabiano Rosas case POWERPC_EXCP_EXTERNAL: /* External input */ 14409f338e4dSFabiano Rosas { 14419f338e4dSFabiano Rosas bool lpes0; 14429f338e4dSFabiano Rosas 14439f338e4dSFabiano Rosas /* 144467baff77SFabiano Rosas * LPES0 is only taken into consideration if we support HV 144567baff77SFabiano Rosas * mode for this CPU. 14469f338e4dSFabiano Rosas */ 144767baff77SFabiano Rosas if (!env->has_hv_mode) { 144867baff77SFabiano Rosas break; 14499f338e4dSFabiano Rosas } 14509f338e4dSFabiano Rosas 145167baff77SFabiano Rosas lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 145267baff77SFabiano Rosas 14539f338e4dSFabiano Rosas if (!lpes0) { 14549f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 14559f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 14569f338e4dSFabiano Rosas srr0 = SPR_HSRR0; 14579f338e4dSFabiano Rosas srr1 = SPR_HSRR1; 14589f338e4dSFabiano Rosas } 145967baff77SFabiano Rosas 14609f338e4dSFabiano Rosas break; 14619f338e4dSFabiano Rosas } 14629f338e4dSFabiano Rosas case POWERPC_EXCP_ALIGN: /* Alignment exception */ 14634ee5d281SNicholas Piggin /* Optional DSISR update was removed from ISA v3.0 */ 14644ee5d281SNicholas Piggin if (!(env->insns_flags2 & PPC2_ISA300)) { 14659f338e4dSFabiano Rosas /* Get rS/rD and rA from faulting opcode */ 14669f338e4dSFabiano Rosas /* 14679f338e4dSFabiano Rosas * Note: the opcode fields will not be set properly for a 14689f338e4dSFabiano Rosas * direct store load/store, but nobody cares as nobody 14699f338e4dSFabiano Rosas * actually uses direct store segments. 14709f338e4dSFabiano Rosas */ 14719f338e4dSFabiano Rosas env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16; 14724ee5d281SNicholas Piggin } 14739f338e4dSFabiano Rosas break; 14749f338e4dSFabiano Rosas case POWERPC_EXCP_PROGRAM: /* Program exception */ 14759f338e4dSFabiano Rosas switch (env->error_code & ~0xF) { 14769f338e4dSFabiano Rosas case POWERPC_EXCP_FP: 1477da806a6cSVíctor Colombo if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) { 14789f338e4dSFabiano Rosas trace_ppc_excp_fp_ignore(); 14793680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 14809f338e4dSFabiano Rosas return; 14819f338e4dSFabiano Rosas } 14829f338e4dSFabiano Rosas 14839f338e4dSFabiano Rosas /* 14849f338e4dSFabiano Rosas * FP exceptions always have NIP pointing to the faulting 14859f338e4dSFabiano Rosas * instruction, so always use store_next and claim we are 14869f338e4dSFabiano Rosas * precise in the MSR. 14879f338e4dSFabiano Rosas */ 14889f338e4dSFabiano Rosas msr |= 0x00100000; 14899f338e4dSFabiano Rosas break; 14909f338e4dSFabiano Rosas case POWERPC_EXCP_INVAL: 14919f338e4dSFabiano Rosas trace_ppc_excp_inval(env->nip); 14929f338e4dSFabiano Rosas msr |= 0x00080000; 14939f338e4dSFabiano Rosas break; 14949f338e4dSFabiano Rosas case POWERPC_EXCP_PRIV: 14959f338e4dSFabiano Rosas msr |= 0x00040000; 14969f338e4dSFabiano Rosas break; 14979f338e4dSFabiano Rosas case POWERPC_EXCP_TRAP: 14989f338e4dSFabiano Rosas msr |= 0x00020000; 14999f338e4dSFabiano Rosas break; 15009f338e4dSFabiano Rosas default: 15019f338e4dSFabiano Rosas /* Should never occur */ 15029f338e4dSFabiano Rosas cpu_abort(cs, "Invalid program exception %d. Aborting\n", 15039f338e4dSFabiano Rosas env->error_code); 15049f338e4dSFabiano Rosas break; 15059f338e4dSFabiano Rosas } 15069f338e4dSFabiano Rosas break; 15079f338e4dSFabiano Rosas case POWERPC_EXCP_SYSCALL: /* System call exception */ 15089f338e4dSFabiano Rosas lev = env->error_code; 15099f338e4dSFabiano Rosas 15102306c606SBALATON Zoltan if (lev == 1 && cpu->vhyp) { 15119f338e4dSFabiano Rosas dump_hcall(env); 15129f338e4dSFabiano Rosas } else { 15139f338e4dSFabiano Rosas dump_syscall(env); 15149f338e4dSFabiano Rosas } 15159f338e4dSFabiano Rosas 15169f338e4dSFabiano Rosas /* 15179f338e4dSFabiano Rosas * We need to correct the NIP which in this case is supposed 15189f338e4dSFabiano Rosas * to point to the next instruction 15199f338e4dSFabiano Rosas */ 15209f338e4dSFabiano Rosas env->nip += 4; 15219f338e4dSFabiano Rosas 15229f338e4dSFabiano Rosas /* "PAPR mode" built-in hypercall emulation */ 15232306c606SBALATON Zoltan if (lev == 1 && books_vhyp_handles_hcall(cpu)) { 15249f338e4dSFabiano Rosas PPCVirtualHypervisorClass *vhc = 15259f338e4dSFabiano Rosas PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 15269f338e4dSFabiano Rosas vhc->hypercall(cpu->vhyp, cpu); 15279f338e4dSFabiano Rosas return; 15289f338e4dSFabiano Rosas } 1529eb701f30SNicholas Piggin if (env->insns_flags2 & PPC2_ISA310) { 1530eb701f30SNicholas Piggin /* ISAv3.1 puts LEV into SRR1 */ 1531eb701f30SNicholas Piggin msr |= lev << 20; 1532eb701f30SNicholas Piggin } 15339f338e4dSFabiano Rosas if (lev == 1) { 15349f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 15359f338e4dSFabiano Rosas } 15369f338e4dSFabiano Rosas break; 15379f338e4dSFabiano Rosas case POWERPC_EXCP_SYSCALL_VECTORED: /* scv exception */ 15389f338e4dSFabiano Rosas lev = env->error_code; 15399f338e4dSFabiano Rosas dump_syscall(env); 15409f338e4dSFabiano Rosas env->nip += 4; 15419f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_EE); 15429f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 15439f338e4dSFabiano Rosas 15449f338e4dSFabiano Rosas vector += lev * 0x20; 15459f338e4dSFabiano Rosas 15469f338e4dSFabiano Rosas env->lr = env->nip; 15479f338e4dSFabiano Rosas env->ctr = msr; 15489f338e4dSFabiano Rosas break; 15499f338e4dSFabiano Rosas case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */ 15509f338e4dSFabiano Rosas case POWERPC_EXCP_DECR: /* Decrementer exception */ 15519f338e4dSFabiano Rosas break; 15529f338e4dSFabiano Rosas case POWERPC_EXCP_RESET: /* System reset exception */ 15539f338e4dSFabiano Rosas /* A power-saving exception sets ME, otherwise it is unchanged */ 15548e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 15559f338e4dSFabiano Rosas /* indicate that we resumed from power save mode */ 15569f338e4dSFabiano Rosas msr |= 0x10000; 15579f338e4dSFabiano Rosas new_msr |= ((target_ulong)1 << MSR_ME); 15589f338e4dSFabiano Rosas } 15599f338e4dSFabiano Rosas if (env->msr_mask & MSR_HVB) { 15609f338e4dSFabiano Rosas /* 15619f338e4dSFabiano Rosas * ISA specifies HV, but can be delivered to guest with HV 15629f338e4dSFabiano Rosas * clear (e.g., see FWNMI in PAPR, NMI injection in QEMU). 15639f338e4dSFabiano Rosas */ 15649f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 15659f338e4dSFabiano Rosas } else { 15668e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 15679f338e4dSFabiano Rosas cpu_abort(cs, "Trying to deliver power-saving system reset " 15689f338e4dSFabiano Rosas "exception %d with no HV support\n", excp); 15699f338e4dSFabiano Rosas } 15709f338e4dSFabiano Rosas } 15719f338e4dSFabiano Rosas break; 15729f338e4dSFabiano Rosas case POWERPC_EXCP_DSEG: /* Data segment exception */ 15739f338e4dSFabiano Rosas case POWERPC_EXCP_ISEG: /* Instruction segment exception */ 15749f338e4dSFabiano Rosas case POWERPC_EXCP_TRACE: /* Trace exception */ 1575fd7abfabSNicholas Piggin case POWERPC_EXCP_SDOOR: /* Doorbell interrupt */ 1576c29b0704SNicholas Piggin case POWERPC_EXCP_PERFM: /* Performance monitor interrupt */ 15779f338e4dSFabiano Rosas break; 15789f338e4dSFabiano Rosas case POWERPC_EXCP_HISI: /* Hypervisor instruction storage exception */ 15799f338e4dSFabiano Rosas msr |= env->error_code; 15809f338e4dSFabiano Rosas /* fall through */ 15819f338e4dSFabiano Rosas case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */ 15829f338e4dSFabiano Rosas case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */ 15839f338e4dSFabiano Rosas case POWERPC_EXCP_SDOOR_HV: /* Hypervisor Doorbell interrupt */ 15849f338e4dSFabiano Rosas case POWERPC_EXCP_HVIRT: /* Hypervisor virtualization */ 15859f338e4dSFabiano Rosas srr0 = SPR_HSRR0; 15869f338e4dSFabiano Rosas srr1 = SPR_HSRR1; 15879f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 15889f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 15899f338e4dSFabiano Rosas break; 1590a3c020d8SNicholas Piggin #ifdef CONFIG_TCG 1591a3c020d8SNicholas Piggin case POWERPC_EXCP_HV_EMU: { 1592a3c020d8SNicholas Piggin uint32_t insn = ppc_ldl_code(env, env->nip); 1593a3c020d8SNicholas Piggin env->spr[SPR_HEIR] = insn; 1594a3c020d8SNicholas Piggin if (is_prefix_insn(env, insn)) { 1595a3c020d8SNicholas Piggin uint32_t insn2 = ppc_ldl_code(env, env->nip + 4); 1596a3c020d8SNicholas Piggin env->spr[SPR_HEIR] <<= 32; 1597a3c020d8SNicholas Piggin env->spr[SPR_HEIR] |= insn2; 1598a3c020d8SNicholas Piggin } 1599a3c020d8SNicholas Piggin srr0 = SPR_HSRR0; 1600a3c020d8SNicholas Piggin srr1 = SPR_HSRR1; 1601a3c020d8SNicholas Piggin new_msr |= (target_ulong)MSR_HVB; 1602a3c020d8SNicholas Piggin new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 1603a3c020d8SNicholas Piggin break; 1604a3c020d8SNicholas Piggin } 1605a3c020d8SNicholas Piggin #endif 16069f338e4dSFabiano Rosas case POWERPC_EXCP_VPU: /* Vector unavailable exception */ 16079f338e4dSFabiano Rosas case POWERPC_EXCP_VSXU: /* VSX unavailable exception */ 16089f338e4dSFabiano Rosas case POWERPC_EXCP_FU: /* Facility unavailable exception */ 16099f338e4dSFabiano Rosas env->spr[SPR_FSCR] |= ((target_ulong)env->error_code << 56); 16109f338e4dSFabiano Rosas break; 16119f338e4dSFabiano Rosas case POWERPC_EXCP_HV_FU: /* Hypervisor Facility Unavailable Exception */ 16129f338e4dSFabiano Rosas env->spr[SPR_HFSCR] |= ((target_ulong)env->error_code << FSCR_IC_POS); 16139f338e4dSFabiano Rosas srr0 = SPR_HSRR0; 16149f338e4dSFabiano Rosas srr1 = SPR_HSRR1; 16159f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 16169f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 16179f338e4dSFabiano Rosas break; 1618cb76bbc4SDaniel Henrique Barboza case POWERPC_EXCP_PERFM_EBB: /* Performance Monitor EBB Exception */ 1619cb76bbc4SDaniel Henrique Barboza case POWERPC_EXCP_EXTERNAL_EBB: /* External EBB Exception */ 1620cb76bbc4SDaniel Henrique Barboza env->spr[SPR_BESCR] &= ~BESCR_GE; 1621cb76bbc4SDaniel Henrique Barboza 1622cb76bbc4SDaniel Henrique Barboza /* 1623cb76bbc4SDaniel Henrique Barboza * Save NIP for rfebb insn in SPR_EBBRR. Next nip is 1624cb76bbc4SDaniel Henrique Barboza * stored in the EBB Handler SPR_EBBHR. 1625cb76bbc4SDaniel Henrique Barboza */ 1626cb76bbc4SDaniel Henrique Barboza env->spr[SPR_EBBRR] = env->nip; 1627cb76bbc4SDaniel Henrique Barboza powerpc_set_excp_state(cpu, env->spr[SPR_EBBHR], env->msr); 1628cb76bbc4SDaniel Henrique Barboza 1629cb76bbc4SDaniel Henrique Barboza /* 1630cb76bbc4SDaniel Henrique Barboza * This exception is handled in userspace. No need to proceed. 1631cb76bbc4SDaniel Henrique Barboza */ 1632cb76bbc4SDaniel Henrique Barboza return; 16339f338e4dSFabiano Rosas case POWERPC_EXCP_THERM: /* Thermal interrupt */ 16349f338e4dSFabiano Rosas case POWERPC_EXCP_VPUA: /* Vector assist exception */ 16359f338e4dSFabiano Rosas case POWERPC_EXCP_MAINT: /* Maintenance exception */ 163630c4e426SFabiano Rosas case POWERPC_EXCP_HV_MAINT: /* Hypervisor Maintenance exception */ 16379f338e4dSFabiano Rosas cpu_abort(cs, "%s exception not implemented\n", 16389f338e4dSFabiano Rosas powerpc_excp_name(excp)); 16399f338e4dSFabiano Rosas break; 16409f338e4dSFabiano Rosas default: 16419f338e4dSFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 16429f338e4dSFabiano Rosas break; 16439f338e4dSFabiano Rosas } 16449f338e4dSFabiano Rosas 16459f338e4dSFabiano Rosas /* 16469f338e4dSFabiano Rosas * Sort out endianness of interrupt, this differs depending on the 16479f338e4dSFabiano Rosas * CPU, the HV mode, etc... 16489f338e4dSFabiano Rosas */ 16499f338e4dSFabiano Rosas if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) { 16509f338e4dSFabiano Rosas new_msr |= (target_ulong)1 << MSR_LE; 16519f338e4dSFabiano Rosas } 16529f338e4dSFabiano Rosas 16539f338e4dSFabiano Rosas new_msr |= (target_ulong)1 << MSR_SF; 16549f338e4dSFabiano Rosas 16559f338e4dSFabiano Rosas if (excp != POWERPC_EXCP_SYSCALL_VECTORED) { 16569f338e4dSFabiano Rosas /* Save PC */ 16579f338e4dSFabiano Rosas env->spr[srr0] = env->nip; 16589f338e4dSFabiano Rosas 16599f338e4dSFabiano Rosas /* Save MSR */ 16609f338e4dSFabiano Rosas env->spr[srr1] = msr; 16619f338e4dSFabiano Rosas } 16629f338e4dSFabiano Rosas 16637cebc5dbSNicholas Piggin if ((new_msr & MSR_HVB) && books_vhyp_handles_hv_excp(cpu)) { 16647cebc5dbSNicholas Piggin PPCVirtualHypervisorClass *vhc = 16657cebc5dbSNicholas Piggin PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 16667cebc5dbSNicholas Piggin /* Deliver interrupt to L1 by returning from the H_ENTER_NESTED call */ 16677cebc5dbSNicholas Piggin vhc->deliver_hv_excp(cpu, excp); 16687cebc5dbSNicholas Piggin 16697cebc5dbSNicholas Piggin powerpc_reset_excp_state(cpu); 16707cebc5dbSNicholas Piggin 16717cebc5dbSNicholas Piggin } else { 16727cebc5dbSNicholas Piggin /* Sanity check */ 16737cebc5dbSNicholas Piggin if (!(env->msr_mask & MSR_HVB) && srr0 == SPR_HSRR0) { 16747cebc5dbSNicholas Piggin cpu_abort(cs, "Trying to deliver HV exception (HSRR) %d with " 16757cebc5dbSNicholas Piggin "no HV support\n", excp); 16767cebc5dbSNicholas Piggin } 16777cebc5dbSNicholas Piggin 16789f338e4dSFabiano Rosas /* This can update new_msr and vector if AIL applies */ 167910895ab6SFabiano Rosas ppc_excp_apply_ail(cpu, excp, msr, &new_msr, &vector); 16809f338e4dSFabiano Rosas 16819f338e4dSFabiano Rosas powerpc_set_excp_state(cpu, vector, new_msr); 16829f338e4dSFabiano Rosas } 16837cebc5dbSNicholas Piggin } 168430c4e426SFabiano Rosas #else 168530c4e426SFabiano Rosas static inline void powerpc_excp_books(PowerPCCPU *cpu, int excp) 168630c4e426SFabiano Rosas { 168730c4e426SFabiano Rosas g_assert_not_reached(); 168830c4e426SFabiano Rosas } 168930c4e426SFabiano Rosas #endif 16909f338e4dSFabiano Rosas 1691dc88dd0aSFabiano Rosas static void powerpc_excp(PowerPCCPU *cpu, int excp) 1692dc88dd0aSFabiano Rosas { 1693c6eaac89SFabiano Rosas CPUState *cs = CPU(cpu); 1694dc88dd0aSFabiano Rosas CPUPPCState *env = &cpu->env; 1695dc88dd0aSFabiano Rosas 1696c6eaac89SFabiano Rosas if (excp <= POWERPC_EXCP_NONE || excp >= POWERPC_EXCP_NB) { 1697c6eaac89SFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 1698c6eaac89SFabiano Rosas } 1699c6eaac89SFabiano Rosas 1700c6eaac89SFabiano Rosas qemu_log_mask(CPU_LOG_INT, "Raise exception at " TARGET_FMT_lx 1701c6eaac89SFabiano Rosas " => %s (%d) error=%02x\n", env->nip, powerpc_excp_name(excp), 1702c6eaac89SFabiano Rosas excp, env->error_code); 17038a15cceeSBALATON Zoltan env->excp_stats[excp]++; 1704c6eaac89SFabiano Rosas 1705dc88dd0aSFabiano Rosas switch (env->excp_model) { 1706e808c2edSFabiano Rosas case POWERPC_EXCP_40x: 1707e808c2edSFabiano Rosas powerpc_excp_40x(cpu, excp); 1708e808c2edSFabiano Rosas break; 170958d178fbSFabiano Rosas case POWERPC_EXCP_6xx: 171058d178fbSFabiano Rosas powerpc_excp_6xx(cpu, excp); 171158d178fbSFabiano Rosas break; 1712ccfca2fcSFabiano Rosas case POWERPC_EXCP_7xx: 1713ccfca2fcSFabiano Rosas powerpc_excp_7xx(cpu, excp); 1714ccfca2fcSFabiano Rosas break; 171552926b0dSFabiano Rosas case POWERPC_EXCP_74xx: 171652926b0dSFabiano Rosas powerpc_excp_74xx(cpu, excp); 171752926b0dSFabiano Rosas break; 1718180952ceSFabiano Rosas case POWERPC_EXCP_BOOKE: 1719180952ceSFabiano Rosas powerpc_excp_booke(cpu, excp); 1720180952ceSFabiano Rosas break; 17219f338e4dSFabiano Rosas case POWERPC_EXCP_970: 17229f338e4dSFabiano Rosas case POWERPC_EXCP_POWER7: 17239f338e4dSFabiano Rosas case POWERPC_EXCP_POWER8: 17249f338e4dSFabiano Rosas case POWERPC_EXCP_POWER9: 17259f338e4dSFabiano Rosas case POWERPC_EXCP_POWER10: 17269f338e4dSFabiano Rosas powerpc_excp_books(cpu, excp); 17279f338e4dSFabiano Rosas break; 1728dc88dd0aSFabiano Rosas default: 172928091374SFabiano Rosas g_assert_not_reached(); 1730dc88dd0aSFabiano Rosas } 1731dc88dd0aSFabiano Rosas } 1732dc88dd0aSFabiano Rosas 173397a8ea5aSAndreas Färber void ppc_cpu_do_interrupt(CPUState *cs) 1734c79c73f6SBlue Swirl { 173597a8ea5aSAndreas Färber PowerPCCPU *cpu = POWERPC_CPU(cs); 17365c26a5b3SAndreas Färber 173793130c84SFabiano Rosas powerpc_excp(cpu, cs->exception_index); 1738c79c73f6SBlue Swirl } 1739c79c73f6SBlue Swirl 17402dfecf01SMatheus Ferst #if defined(TARGET_PPC64) 1741c8e1de2eSMatheus Ferst #define P7_UNUSED_INTERRUPTS \ 1742c8e1de2eSMatheus Ferst (PPC_INTERRUPT_RESET | PPC_INTERRUPT_HVIRT | PPC_INTERRUPT_CEXT | \ 1743c8e1de2eSMatheus Ferst PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT | \ 1744c8e1de2eSMatheus Ferst PPC_INTERRUPT_PIT | PPC_INTERRUPT_DOORBELL | PPC_INTERRUPT_HDOORBELL | \ 1745c8e1de2eSMatheus Ferst PPC_INTERRUPT_THERM | PPC_INTERRUPT_EBB) 1746c8e1de2eSMatheus Ferst 17479c713713SMatheus Ferst static int p7_interrupt_powersave(CPUPPCState *env) 17489c713713SMatheus Ferst { 17499c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_EXT) && 17509c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P7_PECE0)) { 17519c713713SMatheus Ferst return PPC_INTERRUPT_EXT; 17529c713713SMatheus Ferst } 17539c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DECR) && 17549c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P7_PECE1)) { 17559c713713SMatheus Ferst return PPC_INTERRUPT_DECR; 17569c713713SMatheus Ferst } 17579c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_MCK) && 17589c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) { 17599c713713SMatheus Ferst return PPC_INTERRUPT_MCK; 17609c713713SMatheus Ferst } 17619c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HMI) && 17629c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) { 17639c713713SMatheus Ferst return PPC_INTERRUPT_HMI; 17649c713713SMatheus Ferst } 17659c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_RESET) { 17669c713713SMatheus Ferst return PPC_INTERRUPT_RESET; 17679c713713SMatheus Ferst } 17689c713713SMatheus Ferst return 0; 17699c713713SMatheus Ferst } 17709c713713SMatheus Ferst 1771bf303fb3SMatheus Ferst static int p7_next_unmasked_interrupt(CPUPPCState *env) 1772bf303fb3SMatheus Ferst { 1773*4e6b7db2SBALATON Zoltan CPUState *cs = env_cpu(env); 1774*4e6b7db2SBALATON Zoltan 1775022b7128SMatheus Ferst /* Ignore MSR[EE] when coming out of some power management states */ 1776022b7128SMatheus Ferst bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset; 1777bf303fb3SMatheus Ferst 1778c8e1de2eSMatheus Ferst assert((env->pending_interrupts & P7_UNUSED_INTERRUPTS) == 0); 1779c8e1de2eSMatheus Ferst 1780022b7128SMatheus Ferst if (cs->halted) { 1781022b7128SMatheus Ferst /* LPCR[PECE] controls which interrupts can exit power-saving mode */ 1782022b7128SMatheus Ferst return p7_interrupt_powersave(env); 1783022b7128SMatheus Ferst } 1784022b7128SMatheus Ferst 1785bf303fb3SMatheus Ferst /* Machine check exception */ 1786bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 1787bf303fb3SMatheus Ferst return PPC_INTERRUPT_MCK; 1788bf303fb3SMatheus Ferst } 1789bf303fb3SMatheus Ferst 1790bf303fb3SMatheus Ferst /* Hypervisor decrementer exception */ 1791bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDECR) { 1792bf303fb3SMatheus Ferst /* LPCR will be clear when not supported so this will work */ 1793bf303fb3SMatheus Ferst bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE); 1794022b7128SMatheus Ferst if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) { 1795bf303fb3SMatheus Ferst /* HDEC clears on delivery */ 1796bf303fb3SMatheus Ferst return PPC_INTERRUPT_HDECR; 1797bf303fb3SMatheus Ferst } 1798bf303fb3SMatheus Ferst } 1799bf303fb3SMatheus Ferst 1800bf303fb3SMatheus Ferst /* External interrupt can ignore MSR:EE under some circumstances */ 1801bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EXT) { 1802bf303fb3SMatheus Ferst bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 1803bf303fb3SMatheus Ferst bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 1804bf303fb3SMatheus Ferst /* HEIC blocks delivery to the hypervisor */ 1805022b7128SMatheus Ferst if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) && 1806bf303fb3SMatheus Ferst !FIELD_EX64(env->msr, MSR, PR))) || 1807bf303fb3SMatheus Ferst (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) { 1808bf303fb3SMatheus Ferst return PPC_INTERRUPT_EXT; 1809bf303fb3SMatheus Ferst } 1810bf303fb3SMatheus Ferst } 1811022b7128SMatheus Ferst if (msr_ee != 0) { 1812bf303fb3SMatheus Ferst /* Decrementer exception */ 1813bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DECR) { 1814bf303fb3SMatheus Ferst return PPC_INTERRUPT_DECR; 1815bf303fb3SMatheus Ferst } 1816bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PERFM) { 1817bf303fb3SMatheus Ferst return PPC_INTERRUPT_PERFM; 1818bf303fb3SMatheus Ferst } 1819bf303fb3SMatheus Ferst } 1820bf303fb3SMatheus Ferst 1821bf303fb3SMatheus Ferst return 0; 1822bf303fb3SMatheus Ferst } 1823bf303fb3SMatheus Ferst 1824f6194fddSMatheus Ferst #define P8_UNUSED_INTERRUPTS \ 1825f6194fddSMatheus Ferst (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_HVIRT | \ 1826f6194fddSMatheus Ferst PPC_INTERRUPT_CEXT | PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | \ 1827f6194fddSMatheus Ferst PPC_INTERRUPT_FIT | PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM) 1828f6194fddSMatheus Ferst 18299c713713SMatheus Ferst static int p8_interrupt_powersave(CPUPPCState *env) 18309c713713SMatheus Ferst { 18319c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_EXT) && 18329c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE2)) { 18339c713713SMatheus Ferst return PPC_INTERRUPT_EXT; 18349c713713SMatheus Ferst } 18359c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DECR) && 18369c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE3)) { 18379c713713SMatheus Ferst return PPC_INTERRUPT_DECR; 18389c713713SMatheus Ferst } 18399c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_MCK) && 18409c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) { 18419c713713SMatheus Ferst return PPC_INTERRUPT_MCK; 18429c713713SMatheus Ferst } 18439c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HMI) && 18449c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) { 18459c713713SMatheus Ferst return PPC_INTERRUPT_HMI; 18469c713713SMatheus Ferst } 18479c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) && 18489c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE0)) { 18499c713713SMatheus Ferst return PPC_INTERRUPT_DOORBELL; 18509c713713SMatheus Ferst } 18519c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) && 18529c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE1)) { 18539c713713SMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 18549c713713SMatheus Ferst } 18559c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_RESET) { 18569c713713SMatheus Ferst return PPC_INTERRUPT_RESET; 18579c713713SMatheus Ferst } 18589c713713SMatheus Ferst return 0; 18599c713713SMatheus Ferst } 18609c713713SMatheus Ferst 1861a9899d42SMatheus Ferst static int p8_next_unmasked_interrupt(CPUPPCState *env) 1862a9899d42SMatheus Ferst { 1863*4e6b7db2SBALATON Zoltan CPUState *cs = env_cpu(env); 1864*4e6b7db2SBALATON Zoltan 186564a9b5eeSMatheus Ferst /* Ignore MSR[EE] when coming out of some power management states */ 186664a9b5eeSMatheus Ferst bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset; 1867a9899d42SMatheus Ferst 1868f6194fddSMatheus Ferst assert((env->pending_interrupts & P8_UNUSED_INTERRUPTS) == 0); 1869f6194fddSMatheus Ferst 187064a9b5eeSMatheus Ferst if (cs->halted) { 187164a9b5eeSMatheus Ferst /* LPCR[PECE] controls which interrupts can exit power-saving mode */ 187264a9b5eeSMatheus Ferst return p8_interrupt_powersave(env); 187364a9b5eeSMatheus Ferst } 187464a9b5eeSMatheus Ferst 1875a9899d42SMatheus Ferst /* Machine check exception */ 1876a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 1877a9899d42SMatheus Ferst return PPC_INTERRUPT_MCK; 1878a9899d42SMatheus Ferst } 1879a9899d42SMatheus Ferst 1880a9899d42SMatheus Ferst /* Hypervisor decrementer exception */ 1881a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDECR) { 1882a9899d42SMatheus Ferst /* LPCR will be clear when not supported so this will work */ 1883a9899d42SMatheus Ferst bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE); 188464a9b5eeSMatheus Ferst if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) { 1885a9899d42SMatheus Ferst /* HDEC clears on delivery */ 1886a9899d42SMatheus Ferst return PPC_INTERRUPT_HDECR; 1887a9899d42SMatheus Ferst } 1888a9899d42SMatheus Ferst } 1889a9899d42SMatheus Ferst 1890a9899d42SMatheus Ferst /* External interrupt can ignore MSR:EE under some circumstances */ 1891a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EXT) { 1892a9899d42SMatheus Ferst bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 1893a9899d42SMatheus Ferst bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 1894a9899d42SMatheus Ferst /* HEIC blocks delivery to the hypervisor */ 189564a9b5eeSMatheus Ferst if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) && 1896a9899d42SMatheus Ferst !FIELD_EX64(env->msr, MSR, PR))) || 1897a9899d42SMatheus Ferst (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) { 1898a9899d42SMatheus Ferst return PPC_INTERRUPT_EXT; 1899a9899d42SMatheus Ferst } 1900a9899d42SMatheus Ferst } 190164a9b5eeSMatheus Ferst if (msr_ee != 0) { 1902a9899d42SMatheus Ferst /* Decrementer exception */ 1903a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DECR) { 1904a9899d42SMatheus Ferst return PPC_INTERRUPT_DECR; 1905a9899d42SMatheus Ferst } 1906a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 1907a9899d42SMatheus Ferst return PPC_INTERRUPT_DOORBELL; 1908a9899d42SMatheus Ferst } 1909a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) { 1910a9899d42SMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 1911a9899d42SMatheus Ferst } 1912a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PERFM) { 1913a9899d42SMatheus Ferst return PPC_INTERRUPT_PERFM; 1914a9899d42SMatheus Ferst } 1915a9899d42SMatheus Ferst /* EBB exception */ 1916a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EBB) { 1917a9899d42SMatheus Ferst /* 1918a9899d42SMatheus Ferst * EBB exception must be taken in problem state and 1919a9899d42SMatheus Ferst * with BESCR_GE set. 1920a9899d42SMatheus Ferst */ 1921a9899d42SMatheus Ferst if (FIELD_EX64(env->msr, MSR, PR) && 1922a9899d42SMatheus Ferst (env->spr[SPR_BESCR] & BESCR_GE)) { 1923a9899d42SMatheus Ferst return PPC_INTERRUPT_EBB; 1924a9899d42SMatheus Ferst } 1925a9899d42SMatheus Ferst } 1926a9899d42SMatheus Ferst } 1927a9899d42SMatheus Ferst 1928a9899d42SMatheus Ferst return 0; 1929a9899d42SMatheus Ferst } 1930a9899d42SMatheus Ferst 1931b00e9a2fSMatheus Ferst #define P9_UNUSED_INTERRUPTS \ 1932b00e9a2fSMatheus Ferst (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT | \ 1933b00e9a2fSMatheus Ferst PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT | \ 1934b00e9a2fSMatheus Ferst PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM) 1935b00e9a2fSMatheus Ferst 19369c713713SMatheus Ferst static int p9_interrupt_powersave(CPUPPCState *env) 19379c713713SMatheus Ferst { 19389c713713SMatheus Ferst /* External Exception */ 19399c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_EXT) && 19409c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_EEE)) { 19419c713713SMatheus Ferst bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 19429c713713SMatheus Ferst if (!heic || !FIELD_EX64_HV(env->msr) || 19439c713713SMatheus Ferst FIELD_EX64(env->msr, MSR, PR)) { 19449c713713SMatheus Ferst return PPC_INTERRUPT_EXT; 19459c713713SMatheus Ferst } 19469c713713SMatheus Ferst } 19479c713713SMatheus Ferst /* Decrementer Exception */ 19489c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DECR) && 19499c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_DEE)) { 19509c713713SMatheus Ferst return PPC_INTERRUPT_DECR; 19519c713713SMatheus Ferst } 19529c713713SMatheus Ferst /* Machine Check or Hypervisor Maintenance Exception */ 19539c713713SMatheus Ferst if (env->spr[SPR_LPCR] & LPCR_OEE) { 19549c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 19559c713713SMatheus Ferst return PPC_INTERRUPT_MCK; 19569c713713SMatheus Ferst } 19579c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HMI) { 19589c713713SMatheus Ferst return PPC_INTERRUPT_HMI; 19599c713713SMatheus Ferst } 19609c713713SMatheus Ferst } 19619c713713SMatheus Ferst /* Privileged Doorbell Exception */ 19629c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) && 19639c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_PDEE)) { 19649c713713SMatheus Ferst return PPC_INTERRUPT_DOORBELL; 19659c713713SMatheus Ferst } 19669c713713SMatheus Ferst /* Hypervisor Doorbell Exception */ 19679c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) && 19689c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_HDEE)) { 19699c713713SMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 19709c713713SMatheus Ferst } 19719c713713SMatheus Ferst /* Hypervisor virtualization exception */ 19729c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HVIRT) && 19739c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_HVEE)) { 19749c713713SMatheus Ferst return PPC_INTERRUPT_HVIRT; 19759c713713SMatheus Ferst } 19769c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_RESET) { 19779c713713SMatheus Ferst return PPC_INTERRUPT_RESET; 19789c713713SMatheus Ferst } 19799c713713SMatheus Ferst return 0; 19809c713713SMatheus Ferst } 19819c713713SMatheus Ferst 19822dfecf01SMatheus Ferst static int p9_next_unmasked_interrupt(CPUPPCState *env) 19832dfecf01SMatheus Ferst { 1984*4e6b7db2SBALATON Zoltan CPUState *cs = env_cpu(env); 1985*4e6b7db2SBALATON Zoltan 198627796411SMatheus Ferst /* Ignore MSR[EE] when coming out of some power management states */ 198727796411SMatheus Ferst bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset; 19882dfecf01SMatheus Ferst 1989b00e9a2fSMatheus Ferst assert((env->pending_interrupts & P9_UNUSED_INTERRUPTS) == 0); 1990b00e9a2fSMatheus Ferst 199127796411SMatheus Ferst if (cs->halted) { 199227796411SMatheus Ferst if (env->spr[SPR_PSSCR] & PSSCR_EC) { 199327796411SMatheus Ferst /* 199427796411SMatheus Ferst * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can 199527796411SMatheus Ferst * wakeup the processor 199627796411SMatheus Ferst */ 199727796411SMatheus Ferst return p9_interrupt_powersave(env); 199827796411SMatheus Ferst } else { 199927796411SMatheus Ferst /* 200027796411SMatheus Ferst * When it's clear, any system-caused exception exits power-saving 200127796411SMatheus Ferst * mode, even the ones that gate on MSR[EE]. 200227796411SMatheus Ferst */ 200327796411SMatheus Ferst msr_ee = true; 200427796411SMatheus Ferst } 200527796411SMatheus Ferst } 200627796411SMatheus Ferst 20072dfecf01SMatheus Ferst /* Machine check exception */ 20082dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 20092dfecf01SMatheus Ferst return PPC_INTERRUPT_MCK; 20102dfecf01SMatheus Ferst } 20112dfecf01SMatheus Ferst 20122dfecf01SMatheus Ferst /* Hypervisor decrementer exception */ 20132dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDECR) { 20142dfecf01SMatheus Ferst /* LPCR will be clear when not supported so this will work */ 20152dfecf01SMatheus Ferst bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE); 201627796411SMatheus Ferst if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) { 20172dfecf01SMatheus Ferst /* HDEC clears on delivery */ 20182dfecf01SMatheus Ferst return PPC_INTERRUPT_HDECR; 20192dfecf01SMatheus Ferst } 20202dfecf01SMatheus Ferst } 20212dfecf01SMatheus Ferst 20222dfecf01SMatheus Ferst /* Hypervisor virtualization interrupt */ 20232dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) { 20242dfecf01SMatheus Ferst /* LPCR will be clear when not supported so this will work */ 20252dfecf01SMatheus Ferst bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE); 202627796411SMatheus Ferst if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hvice) { 20272dfecf01SMatheus Ferst return PPC_INTERRUPT_HVIRT; 20282dfecf01SMatheus Ferst } 20292dfecf01SMatheus Ferst } 20302dfecf01SMatheus Ferst 20312dfecf01SMatheus Ferst /* External interrupt can ignore MSR:EE under some circumstances */ 20322dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EXT) { 20332dfecf01SMatheus Ferst bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 20342dfecf01SMatheus Ferst bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 20352dfecf01SMatheus Ferst /* HEIC blocks delivery to the hypervisor */ 203627796411SMatheus Ferst if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) && 20372dfecf01SMatheus Ferst !FIELD_EX64(env->msr, MSR, PR))) || 20382dfecf01SMatheus Ferst (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) { 20392dfecf01SMatheus Ferst return PPC_INTERRUPT_EXT; 20402dfecf01SMatheus Ferst } 20412dfecf01SMatheus Ferst } 204227796411SMatheus Ferst if (msr_ee != 0) { 20432dfecf01SMatheus Ferst /* Decrementer exception */ 20442dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DECR) { 20452dfecf01SMatheus Ferst return PPC_INTERRUPT_DECR; 20462dfecf01SMatheus Ferst } 20472dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 20482dfecf01SMatheus Ferst return PPC_INTERRUPT_DOORBELL; 20492dfecf01SMatheus Ferst } 20502dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) { 20512dfecf01SMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 20522dfecf01SMatheus Ferst } 20532dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PERFM) { 20542dfecf01SMatheus Ferst return PPC_INTERRUPT_PERFM; 20552dfecf01SMatheus Ferst } 20562dfecf01SMatheus Ferst /* EBB exception */ 20572dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EBB) { 20582dfecf01SMatheus Ferst /* 20592dfecf01SMatheus Ferst * EBB exception must be taken in problem state and 20602dfecf01SMatheus Ferst * with BESCR_GE set. 20612dfecf01SMatheus Ferst */ 20622dfecf01SMatheus Ferst if (FIELD_EX64(env->msr, MSR, PR) && 20632dfecf01SMatheus Ferst (env->spr[SPR_BESCR] & BESCR_GE)) { 20642dfecf01SMatheus Ferst return PPC_INTERRUPT_EBB; 20652dfecf01SMatheus Ferst } 20662dfecf01SMatheus Ferst } 20672dfecf01SMatheus Ferst } 20682dfecf01SMatheus Ferst 20692dfecf01SMatheus Ferst return 0; 20702dfecf01SMatheus Ferst } 20712dfecf01SMatheus Ferst #endif 20722dfecf01SMatheus Ferst 2073ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt_generic(CPUPPCState *env) 2074c79c73f6SBlue Swirl { 20753621e2c9SBenjamin Herrenschmidt bool async_deliver; 2076259186a7SAndreas Färber 2077c79c73f6SBlue Swirl /* External reset */ 2078f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_RESET) { 2079de76b85cSMatheus Ferst return PPC_INTERRUPT_RESET; 2080c79c73f6SBlue Swirl } 2081c79c73f6SBlue Swirl /* Machine check exception */ 2082f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 2083de76b85cSMatheus Ferst return PPC_INTERRUPT_MCK; 2084c79c73f6SBlue Swirl } 2085c79c73f6SBlue Swirl #if 0 /* TODO */ 2086c79c73f6SBlue Swirl /* External debug exception */ 2087f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) { 2088de76b85cSMatheus Ferst return PPC_INTERRUPT_DEBUG; 2089c79c73f6SBlue Swirl } 2090c79c73f6SBlue Swirl #endif 20913621e2c9SBenjamin Herrenschmidt 20923621e2c9SBenjamin Herrenschmidt /* 20933621e2c9SBenjamin Herrenschmidt * For interrupts that gate on MSR:EE, we need to do something a 20943621e2c9SBenjamin Herrenschmidt * bit more subtle, as we need to let them through even when EE is 20953621e2c9SBenjamin Herrenschmidt * clear when coming out of some power management states (in order 20963621e2c9SBenjamin Herrenschmidt * for them to become a 0x100). 20973621e2c9SBenjamin Herrenschmidt */ 20980939b8f8SVíctor Colombo async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset; 20993621e2c9SBenjamin Herrenschmidt 2100c79c73f6SBlue Swirl /* Hypervisor decrementer exception */ 2101f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDECR) { 21024b236b62SBenjamin Herrenschmidt /* LPCR will be clear when not supported so this will work */ 21034b236b62SBenjamin Herrenschmidt bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE); 21049de754d3SVíctor Colombo if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) { 21054b236b62SBenjamin Herrenschmidt /* HDEC clears on delivery */ 2106de76b85cSMatheus Ferst return PPC_INTERRUPT_HDECR; 2107c79c73f6SBlue Swirl } 2108c79c73f6SBlue Swirl } 2109d8ce5fd6SBenjamin Herrenschmidt 2110d8ce5fd6SBenjamin Herrenschmidt /* Hypervisor virtualization interrupt */ 2111f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) { 2112d8ce5fd6SBenjamin Herrenschmidt /* LPCR will be clear when not supported so this will work */ 2113d8ce5fd6SBenjamin Herrenschmidt bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE); 21149de754d3SVíctor Colombo if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) { 2115de76b85cSMatheus Ferst return PPC_INTERRUPT_HVIRT; 2116d8ce5fd6SBenjamin Herrenschmidt } 2117d8ce5fd6SBenjamin Herrenschmidt } 2118d8ce5fd6SBenjamin Herrenschmidt 2119d8ce5fd6SBenjamin Herrenschmidt /* External interrupt can ignore MSR:EE under some circumstances */ 2120f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EXT) { 2121d1dbe37cSBenjamin Herrenschmidt bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 21226eebe6dcSBenjamin Herrenschmidt bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 21236eebe6dcSBenjamin Herrenschmidt /* HEIC blocks delivery to the hypervisor */ 21249de754d3SVíctor Colombo if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) && 2125d41ccf6eSVíctor Colombo !FIELD_EX64(env->msr, MSR, PR))) || 21269de754d3SVíctor Colombo (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) { 2127de76b85cSMatheus Ferst return PPC_INTERRUPT_EXT; 2128d1dbe37cSBenjamin Herrenschmidt } 2129d1dbe37cSBenjamin Herrenschmidt } 2130acc861c2SVíctor Colombo if (FIELD_EX64(env->msr, MSR, CE)) { 2131c79c73f6SBlue Swirl /* External critical interrupt */ 2132f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_CEXT) { 2133de76b85cSMatheus Ferst return PPC_INTERRUPT_CEXT; 2134c79c73f6SBlue Swirl } 2135c79c73f6SBlue Swirl } 21363621e2c9SBenjamin Herrenschmidt if (async_deliver != 0) { 2137c79c73f6SBlue Swirl /* Watchdog timer on embedded PowerPC */ 2138f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_WDT) { 2139de76b85cSMatheus Ferst return PPC_INTERRUPT_WDT; 2140c79c73f6SBlue Swirl } 2141f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) { 2142de76b85cSMatheus Ferst return PPC_INTERRUPT_CDOORBELL; 2143c79c73f6SBlue Swirl } 2144c79c73f6SBlue Swirl /* Fixed interval timer on embedded PowerPC */ 2145f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_FIT) { 2146de76b85cSMatheus Ferst return PPC_INTERRUPT_FIT; 2147c79c73f6SBlue Swirl } 2148c79c73f6SBlue Swirl /* Programmable interval timer on embedded PowerPC */ 2149f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PIT) { 2150de76b85cSMatheus Ferst return PPC_INTERRUPT_PIT; 2151c79c73f6SBlue Swirl } 2152c79c73f6SBlue Swirl /* Decrementer exception */ 2153f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DECR) { 2154de76b85cSMatheus Ferst return PPC_INTERRUPT_DECR; 2155c79c73f6SBlue Swirl } 2156f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 2157de76b85cSMatheus Ferst return PPC_INTERRUPT_DOORBELL; 2158c79c73f6SBlue Swirl } 2159f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) { 2160de76b85cSMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 21617af1e7b0SCédric Le Goater } 2162f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PERFM) { 2163de76b85cSMatheus Ferst return PPC_INTERRUPT_PERFM; 2164c79c73f6SBlue Swirl } 2165c79c73f6SBlue Swirl /* Thermal interrupt */ 2166f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_THERM) { 2167de76b85cSMatheus Ferst return PPC_INTERRUPT_THERM; 2168c79c73f6SBlue Swirl } 2169cb76bbc4SDaniel Henrique Barboza /* EBB exception */ 2170f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EBB) { 2171cb76bbc4SDaniel Henrique Barboza /* 2172cb76bbc4SDaniel Henrique Barboza * EBB exception must be taken in problem state and 2173cb76bbc4SDaniel Henrique Barboza * with BESCR_GE set. 2174cb76bbc4SDaniel Henrique Barboza */ 2175d41ccf6eSVíctor Colombo if (FIELD_EX64(env->msr, MSR, PR) && 2176d41ccf6eSVíctor Colombo (env->spr[SPR_BESCR] & BESCR_GE)) { 2177de76b85cSMatheus Ferst return PPC_INTERRUPT_EBB; 2178de76b85cSMatheus Ferst } 2179de76b85cSMatheus Ferst } 2180de76b85cSMatheus Ferst } 2181cb76bbc4SDaniel Henrique Barboza 2182de76b85cSMatheus Ferst return 0; 2183de76b85cSMatheus Ferst } 2184de76b85cSMatheus Ferst 2185ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt(CPUPPCState *env) 2186ba2898f7SMatheus Ferst { 2187ba2898f7SMatheus Ferst switch (env->excp_model) { 21882dfecf01SMatheus Ferst #if defined(TARGET_PPC64) 2189bf303fb3SMatheus Ferst case POWERPC_EXCP_POWER7: 2190bf303fb3SMatheus Ferst return p7_next_unmasked_interrupt(env); 2191a9899d42SMatheus Ferst case POWERPC_EXCP_POWER8: 2192a9899d42SMatheus Ferst return p8_next_unmasked_interrupt(env); 21932dfecf01SMatheus Ferst case POWERPC_EXCP_POWER9: 21942dfecf01SMatheus Ferst case POWERPC_EXCP_POWER10: 21952dfecf01SMatheus Ferst return p9_next_unmasked_interrupt(env); 21962dfecf01SMatheus Ferst #endif 2197ba2898f7SMatheus Ferst default: 2198ba2898f7SMatheus Ferst return ppc_next_unmasked_interrupt_generic(env); 2199ba2898f7SMatheus Ferst } 2200ba2898f7SMatheus Ferst } 2201ba2898f7SMatheus Ferst 22022fdedcbcSMatheus Ferst /* 22032fdedcbcSMatheus Ferst * Sets CPU_INTERRUPT_HARD if there is at least one unmasked interrupt to be 22042fdedcbcSMatheus Ferst * delivered and clears CPU_INTERRUPT_HARD otherwise. 22052fdedcbcSMatheus Ferst * 22062fdedcbcSMatheus Ferst * This method is called by ppc_set_interrupt when an interrupt is raised or 22072fdedcbcSMatheus Ferst * lowered, and should also be called whenever an interrupt masking condition 22082fdedcbcSMatheus Ferst * is changed, e.g.: 22092fdedcbcSMatheus Ferst * - When relevant bits of MSR are altered, like EE, HV, PR, etc.; 22102fdedcbcSMatheus Ferst * - When relevant bits of LPCR are altered, like PECE, HDICE, HVICE, etc.; 22112fdedcbcSMatheus Ferst * - When PSSCR[EC] or env->resume_as_sreset are changed; 22122fdedcbcSMatheus Ferst * - When cs->halted is changed and the CPU has a different interrupt masking 22132fdedcbcSMatheus Ferst * logic in power-saving mode (e.g., POWER7/8/9/10); 22142fdedcbcSMatheus Ferst */ 22152fdedcbcSMatheus Ferst void ppc_maybe_interrupt(CPUPPCState *env) 22162fdedcbcSMatheus Ferst { 22172fdedcbcSMatheus Ferst CPUState *cs = env_cpu(env); 22182fc4f9f3SRichard Henderson QEMU_IOTHREAD_LOCK_GUARD(); 22192fdedcbcSMatheus Ferst 22202fdedcbcSMatheus Ferst if (ppc_next_unmasked_interrupt(env)) { 22212fdedcbcSMatheus Ferst cpu_interrupt(cs, CPU_INTERRUPT_HARD); 22222fdedcbcSMatheus Ferst } else { 22232fdedcbcSMatheus Ferst cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); 22242fdedcbcSMatheus Ferst } 22252fdedcbcSMatheus Ferst } 22262fdedcbcSMatheus Ferst 22273654e238SMatheus Ferst #if defined(TARGET_PPC64) 2228d93a4856SMatheus Ferst static void p7_deliver_interrupt(CPUPPCState *env, int interrupt) 2229d93a4856SMatheus Ferst { 2230d93a4856SMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 2231d93a4856SMatheus Ferst CPUState *cs = env_cpu(env); 2232d93a4856SMatheus Ferst 2233d93a4856SMatheus Ferst switch (interrupt) { 2234d93a4856SMatheus Ferst case PPC_INTERRUPT_MCK: /* Machine check exception */ 2235d93a4856SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_MCK; 2236d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_MCHECK); 2237d93a4856SMatheus Ferst break; 2238d93a4856SMatheus Ferst 2239d93a4856SMatheus Ferst case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */ 2240d93a4856SMatheus Ferst /* HDEC clears on delivery */ 2241d93a4856SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDECR; 2242d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HDECR); 2243d93a4856SMatheus Ferst break; 2244d93a4856SMatheus Ferst 2245d93a4856SMatheus Ferst case PPC_INTERRUPT_EXT: 2246d93a4856SMatheus Ferst if (books_vhyp_promotes_external_to_hvirt(cpu)) { 2247d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 2248d93a4856SMatheus Ferst } else { 2249d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL); 2250d93a4856SMatheus Ferst } 2251d93a4856SMatheus Ferst break; 2252d93a4856SMatheus Ferst 2253d93a4856SMatheus Ferst case PPC_INTERRUPT_DECR: /* Decrementer exception */ 2254d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DECR); 2255d93a4856SMatheus Ferst break; 2256d93a4856SMatheus Ferst case PPC_INTERRUPT_PERFM: 2257d93a4856SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PERFM; 2258d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM); 2259d93a4856SMatheus Ferst break; 2260d93a4856SMatheus Ferst case 0: 2261d93a4856SMatheus Ferst /* 2262d93a4856SMatheus Ferst * This is a bug ! It means that has_work took us out of halt without 2263d93a4856SMatheus Ferst * anything to deliver while in a PM state that requires getting 2264d93a4856SMatheus Ferst * out via a 0x100 2265d93a4856SMatheus Ferst * 2266d93a4856SMatheus Ferst * This means we will incorrectly execute past the power management 2267d93a4856SMatheus Ferst * instruction instead of triggering a reset. 2268d93a4856SMatheus Ferst * 2269d93a4856SMatheus Ferst * It generally means a discrepancy between the wakeup conditions in the 2270d93a4856SMatheus Ferst * processor has_work implementation and the logic in this function. 2271d93a4856SMatheus Ferst */ 2272d93a4856SMatheus Ferst assert(!env->resume_as_sreset); 2273d93a4856SMatheus Ferst break; 2274d93a4856SMatheus Ferst default: 2275d93a4856SMatheus Ferst cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt); 2276d93a4856SMatheus Ferst } 2277d93a4856SMatheus Ferst } 2278d93a4856SMatheus Ferst 22796527e757SMatheus Ferst static void p8_deliver_interrupt(CPUPPCState *env, int interrupt) 22806527e757SMatheus Ferst { 22816527e757SMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 22826527e757SMatheus Ferst CPUState *cs = env_cpu(env); 22836527e757SMatheus Ferst 22846527e757SMatheus Ferst switch (interrupt) { 22856527e757SMatheus Ferst case PPC_INTERRUPT_MCK: /* Machine check exception */ 22866527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_MCK; 22876527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_MCHECK); 22886527e757SMatheus Ferst break; 22896527e757SMatheus Ferst 22906527e757SMatheus Ferst case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */ 22916527e757SMatheus Ferst /* HDEC clears on delivery */ 22926527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDECR; 22936527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HDECR); 22946527e757SMatheus Ferst break; 22956527e757SMatheus Ferst 22966527e757SMatheus Ferst case PPC_INTERRUPT_EXT: 22976527e757SMatheus Ferst if (books_vhyp_promotes_external_to_hvirt(cpu)) { 22986527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 22996527e757SMatheus Ferst } else { 23006527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL); 23016527e757SMatheus Ferst } 23026527e757SMatheus Ferst break; 23036527e757SMatheus Ferst 23046527e757SMatheus Ferst case PPC_INTERRUPT_DECR: /* Decrementer exception */ 23056527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DECR); 23066527e757SMatheus Ferst break; 23076527e757SMatheus Ferst case PPC_INTERRUPT_DOORBELL: 23086527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL; 23096527e757SMatheus Ferst if (is_book3s_arch2x(env)) { 23106527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR); 23116527e757SMatheus Ferst } else { 23126527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DOORI); 23136527e757SMatheus Ferst } 23146527e757SMatheus Ferst break; 23156527e757SMatheus Ferst case PPC_INTERRUPT_HDOORBELL: 23166527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL; 23176527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV); 23186527e757SMatheus Ferst break; 23196527e757SMatheus Ferst case PPC_INTERRUPT_PERFM: 23206527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PERFM; 23216527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM); 23226527e757SMatheus Ferst break; 23236527e757SMatheus Ferst case PPC_INTERRUPT_EBB: /* EBB exception */ 23246527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_EBB; 23256527e757SMatheus Ferst if (env->spr[SPR_BESCR] & BESCR_PMEO) { 23266527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB); 23276527e757SMatheus Ferst } else if (env->spr[SPR_BESCR] & BESCR_EEO) { 23286527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB); 23296527e757SMatheus Ferst } 23306527e757SMatheus Ferst break; 23316527e757SMatheus Ferst case 0: 23326527e757SMatheus Ferst /* 23336527e757SMatheus Ferst * This is a bug ! It means that has_work took us out of halt without 23346527e757SMatheus Ferst * anything to deliver while in a PM state that requires getting 23356527e757SMatheus Ferst * out via a 0x100 23366527e757SMatheus Ferst * 23376527e757SMatheus Ferst * This means we will incorrectly execute past the power management 23386527e757SMatheus Ferst * instruction instead of triggering a reset. 23396527e757SMatheus Ferst * 23406527e757SMatheus Ferst * It generally means a discrepancy between the wakeup conditions in the 23416527e757SMatheus Ferst * processor has_work implementation and the logic in this function. 23426527e757SMatheus Ferst */ 23436527e757SMatheus Ferst assert(!env->resume_as_sreset); 23446527e757SMatheus Ferst break; 23456527e757SMatheus Ferst default: 23466527e757SMatheus Ferst cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt); 23476527e757SMatheus Ferst } 23486527e757SMatheus Ferst } 23496527e757SMatheus Ferst 23503654e238SMatheus Ferst static void p9_deliver_interrupt(CPUPPCState *env, int interrupt) 23513654e238SMatheus Ferst { 23523654e238SMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 23533654e238SMatheus Ferst CPUState *cs = env_cpu(env); 23543654e238SMatheus Ferst 235527796411SMatheus Ferst if (cs->halted && !(env->spr[SPR_PSSCR] & PSSCR_EC) && 235627796411SMatheus Ferst !FIELD_EX64(env->msr, MSR, EE)) { 235727796411SMatheus Ferst /* 235827796411SMatheus Ferst * A pending interrupt took us out of power-saving, but MSR[EE] says 235927796411SMatheus Ferst * that we should return to NIP+4 instead of delivering it. 236027796411SMatheus Ferst */ 236127796411SMatheus Ferst return; 236227796411SMatheus Ferst } 236327796411SMatheus Ferst 23643654e238SMatheus Ferst switch (interrupt) { 23653654e238SMatheus Ferst case PPC_INTERRUPT_MCK: /* Machine check exception */ 23663654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_MCK; 23673654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_MCHECK); 23683654e238SMatheus Ferst break; 23693654e238SMatheus Ferst 23703654e238SMatheus Ferst case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */ 23713654e238SMatheus Ferst /* HDEC clears on delivery */ 23723654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDECR; 23733654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HDECR); 23743654e238SMatheus Ferst break; 23753654e238SMatheus Ferst case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */ 23763654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 23773654e238SMatheus Ferst break; 23783654e238SMatheus Ferst 23793654e238SMatheus Ferst case PPC_INTERRUPT_EXT: 23803654e238SMatheus Ferst if (books_vhyp_promotes_external_to_hvirt(cpu)) { 23813654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 23823654e238SMatheus Ferst } else { 23833654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL); 23843654e238SMatheus Ferst } 23853654e238SMatheus Ferst break; 23863654e238SMatheus Ferst 23873654e238SMatheus Ferst case PPC_INTERRUPT_DECR: /* Decrementer exception */ 23883654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DECR); 23893654e238SMatheus Ferst break; 23903654e238SMatheus Ferst case PPC_INTERRUPT_DOORBELL: 23913654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL; 23923654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR); 23933654e238SMatheus Ferst break; 23943654e238SMatheus Ferst case PPC_INTERRUPT_HDOORBELL: 23953654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL; 23963654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV); 23973654e238SMatheus Ferst break; 23983654e238SMatheus Ferst case PPC_INTERRUPT_PERFM: 23993654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PERFM; 24003654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM); 24013654e238SMatheus Ferst break; 24023654e238SMatheus Ferst case PPC_INTERRUPT_EBB: /* EBB exception */ 24033654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_EBB; 24043654e238SMatheus Ferst if (env->spr[SPR_BESCR] & BESCR_PMEO) { 24053654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB); 24063654e238SMatheus Ferst } else if (env->spr[SPR_BESCR] & BESCR_EEO) { 24073654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB); 24083654e238SMatheus Ferst } 24093654e238SMatheus Ferst break; 24103654e238SMatheus Ferst case 0: 24113654e238SMatheus Ferst /* 24123654e238SMatheus Ferst * This is a bug ! It means that has_work took us out of halt without 24133654e238SMatheus Ferst * anything to deliver while in a PM state that requires getting 24143654e238SMatheus Ferst * out via a 0x100 24153654e238SMatheus Ferst * 24163654e238SMatheus Ferst * This means we will incorrectly execute past the power management 24173654e238SMatheus Ferst * instruction instead of triggering a reset. 24183654e238SMatheus Ferst * 24193654e238SMatheus Ferst * It generally means a discrepancy between the wakeup conditions in the 24203654e238SMatheus Ferst * processor has_work implementation and the logic in this function. 24213654e238SMatheus Ferst */ 24223654e238SMatheus Ferst assert(!env->resume_as_sreset); 24233654e238SMatheus Ferst break; 24243654e238SMatheus Ferst default: 24253654e238SMatheus Ferst cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt); 24263654e238SMatheus Ferst } 24273654e238SMatheus Ferst } 24283654e238SMatheus Ferst #endif 24293654e238SMatheus Ferst 2430ba2898f7SMatheus Ferst static void ppc_deliver_interrupt_generic(CPUPPCState *env, int interrupt) 2431de76b85cSMatheus Ferst { 2432de76b85cSMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 2433de76b85cSMatheus Ferst CPUState *cs = env_cpu(env); 2434de76b85cSMatheus Ferst 2435de76b85cSMatheus Ferst switch (interrupt) { 2436de76b85cSMatheus Ferst case PPC_INTERRUPT_RESET: /* External reset */ 2437de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_RESET; 2438de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_RESET); 2439de76b85cSMatheus Ferst break; 2440de76b85cSMatheus Ferst case PPC_INTERRUPT_MCK: /* Machine check exception */ 2441de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_MCK; 2442de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_MCHECK); 2443de76b85cSMatheus Ferst break; 2444de76b85cSMatheus Ferst 2445de76b85cSMatheus Ferst case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */ 2446de76b85cSMatheus Ferst /* HDEC clears on delivery */ 2447de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDECR; 2448de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HDECR); 2449de76b85cSMatheus Ferst break; 2450de76b85cSMatheus Ferst case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */ 2451de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 2452de76b85cSMatheus Ferst break; 2453de76b85cSMatheus Ferst 2454de76b85cSMatheus Ferst case PPC_INTERRUPT_EXT: 2455de76b85cSMatheus Ferst if (books_vhyp_promotes_external_to_hvirt(cpu)) { 2456de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 2457de76b85cSMatheus Ferst } else { 2458de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL); 2459de76b85cSMatheus Ferst } 2460de76b85cSMatheus Ferst break; 2461de76b85cSMatheus Ferst case PPC_INTERRUPT_CEXT: /* External critical interrupt */ 2462de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_CRITICAL); 2463de76b85cSMatheus Ferst break; 2464de76b85cSMatheus Ferst 2465de76b85cSMatheus Ferst case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */ 2466de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_WDT; 2467de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_WDT); 2468de76b85cSMatheus Ferst break; 2469de76b85cSMatheus Ferst case PPC_INTERRUPT_CDOORBELL: 2470de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL; 2471de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DOORCI); 2472de76b85cSMatheus Ferst break; 2473de76b85cSMatheus Ferst case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */ 2474de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_FIT; 2475de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_FIT); 2476de76b85cSMatheus Ferst break; 2477de76b85cSMatheus Ferst case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */ 2478de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PIT; 2479de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PIT); 2480de76b85cSMatheus Ferst break; 2481de76b85cSMatheus Ferst case PPC_INTERRUPT_DECR: /* Decrementer exception */ 2482de76b85cSMatheus Ferst if (ppc_decr_clear_on_delivery(env)) { 2483de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_DECR; 2484de76b85cSMatheus Ferst } 2485de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DECR); 2486de76b85cSMatheus Ferst break; 2487de76b85cSMatheus Ferst case PPC_INTERRUPT_DOORBELL: 2488de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL; 2489de76b85cSMatheus Ferst if (is_book3s_arch2x(env)) { 2490de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR); 2491de76b85cSMatheus Ferst } else { 2492de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DOORI); 2493de76b85cSMatheus Ferst } 2494de76b85cSMatheus Ferst break; 2495de76b85cSMatheus Ferst case PPC_INTERRUPT_HDOORBELL: 2496de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL; 2497de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV); 2498de76b85cSMatheus Ferst break; 2499de76b85cSMatheus Ferst case PPC_INTERRUPT_PERFM: 2500de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PERFM; 2501de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM); 2502de76b85cSMatheus Ferst break; 2503de76b85cSMatheus Ferst case PPC_INTERRUPT_THERM: /* Thermal interrupt */ 2504de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_THERM; 2505de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_THERM); 2506de76b85cSMatheus Ferst break; 2507de76b85cSMatheus Ferst case PPC_INTERRUPT_EBB: /* EBB exception */ 2508de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_EBB; 2509cb76bbc4SDaniel Henrique Barboza if (env->spr[SPR_BESCR] & BESCR_PMEO) { 2510cb76bbc4SDaniel Henrique Barboza powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB); 2511cb76bbc4SDaniel Henrique Barboza } else if (env->spr[SPR_BESCR] & BESCR_EEO) { 2512cb76bbc4SDaniel Henrique Barboza powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB); 2513cb76bbc4SDaniel Henrique Barboza } 2514de76b85cSMatheus Ferst break; 2515de76b85cSMatheus Ferst case 0: 2516f8154fd2SBenjamin Herrenschmidt /* 2517f8154fd2SBenjamin Herrenschmidt * This is a bug ! It means that has_work took us out of halt without 2518f8154fd2SBenjamin Herrenschmidt * anything to deliver while in a PM state that requires getting 2519f8154fd2SBenjamin Herrenschmidt * out via a 0x100 2520f8154fd2SBenjamin Herrenschmidt * 2521f8154fd2SBenjamin Herrenschmidt * This means we will incorrectly execute past the power management 2522f8154fd2SBenjamin Herrenschmidt * instruction instead of triggering a reset. 2523f8154fd2SBenjamin Herrenschmidt * 2524136fbf65Szhaolichang * It generally means a discrepancy between the wakeup conditions in the 2525f8154fd2SBenjamin Herrenschmidt * processor has_work implementation and the logic in this function. 2526f8154fd2SBenjamin Herrenschmidt */ 2527de76b85cSMatheus Ferst assert(!env->resume_as_sreset); 2528de76b85cSMatheus Ferst break; 2529de76b85cSMatheus Ferst default: 2530de76b85cSMatheus Ferst cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt); 2531f8154fd2SBenjamin Herrenschmidt } 2532c79c73f6SBlue Swirl } 253334316482SAlexey Kardashevskiy 2534ba2898f7SMatheus Ferst static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt) 2535ba2898f7SMatheus Ferst { 2536ba2898f7SMatheus Ferst switch (env->excp_model) { 25373654e238SMatheus Ferst #if defined(TARGET_PPC64) 2538d93a4856SMatheus Ferst case POWERPC_EXCP_POWER7: 2539d93a4856SMatheus Ferst p7_deliver_interrupt(env, interrupt); 2540d93a4856SMatheus Ferst break; 25416527e757SMatheus Ferst case POWERPC_EXCP_POWER8: 25426527e757SMatheus Ferst p8_deliver_interrupt(env, interrupt); 25436527e757SMatheus Ferst break; 25443654e238SMatheus Ferst case POWERPC_EXCP_POWER9: 25453654e238SMatheus Ferst case POWERPC_EXCP_POWER10: 25463654e238SMatheus Ferst p9_deliver_interrupt(env, interrupt); 25473654e238SMatheus Ferst break; 25483654e238SMatheus Ferst #endif 2549ba2898f7SMatheus Ferst default: 2550ba2898f7SMatheus Ferst ppc_deliver_interrupt_generic(env, interrupt); 2551ba2898f7SMatheus Ferst } 2552ba2898f7SMatheus Ferst } 2553ba2898f7SMatheus Ferst 2554b5b7f391SNicholas Piggin void ppc_cpu_do_system_reset(CPUState *cs) 255534316482SAlexey Kardashevskiy { 255634316482SAlexey Kardashevskiy PowerPCCPU *cpu = POWERPC_CPU(cs); 255734316482SAlexey Kardashevskiy 255893130c84SFabiano Rosas powerpc_excp(cpu, POWERPC_EXCP_RESET); 255934316482SAlexey Kardashevskiy } 2560ad77c6caSNicholas Piggin 2561ad77c6caSNicholas Piggin void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector) 2562ad77c6caSNicholas Piggin { 2563ad77c6caSNicholas Piggin PowerPCCPU *cpu = POWERPC_CPU(cs); 2564ad77c6caSNicholas Piggin CPUPPCState *env = &cpu->env; 2565ad77c6caSNicholas Piggin target_ulong msr = 0; 2566ad77c6caSNicholas Piggin 2567ad77c6caSNicholas Piggin /* 2568ad77c6caSNicholas Piggin * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already 2569ad77c6caSNicholas Piggin * been set by KVM. 2570ad77c6caSNicholas Piggin */ 2571ad77c6caSNicholas Piggin msr = (1ULL << MSR_ME); 2572ad77c6caSNicholas Piggin msr |= env->msr & (1ULL << MSR_SF); 2573516fc103SFabiano Rosas if (ppc_interrupts_little_endian(cpu, false)) { 2574ad77c6caSNicholas Piggin msr |= (1ULL << MSR_LE); 2575ad77c6caSNicholas Piggin } 2576ad77c6caSNicholas Piggin 25777cebc5dbSNicholas Piggin /* Anything for nested required here? MSR[HV] bit? */ 25787cebc5dbSNicholas Piggin 2579ad77c6caSNicholas Piggin powerpc_set_excp_state(cpu, vector, msr); 2580ad77c6caSNicholas Piggin } 2581c79c73f6SBlue Swirl 2582458dd766SRichard Henderson bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request) 2583458dd766SRichard Henderson { 2584458dd766SRichard Henderson PowerPCCPU *cpu = POWERPC_CPU(cs); 2585458dd766SRichard Henderson CPUPPCState *env = &cpu->env; 2586de76b85cSMatheus Ferst int interrupt; 2587458dd766SRichard Henderson 2588de76b85cSMatheus Ferst if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) { 2589de76b85cSMatheus Ferst return false; 2590de76b85cSMatheus Ferst } 2591de76b85cSMatheus Ferst 2592de76b85cSMatheus Ferst interrupt = ppc_next_unmasked_interrupt(env); 2593de76b85cSMatheus Ferst if (interrupt == 0) { 2594de76b85cSMatheus Ferst return false; 2595de76b85cSMatheus Ferst } 2596de76b85cSMatheus Ferst 2597de76b85cSMatheus Ferst ppc_deliver_interrupt(env, interrupt); 2598458dd766SRichard Henderson if (env->pending_interrupts == 0) { 2599de76b85cSMatheus Ferst cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); 2600458dd766SRichard Henderson } 2601458dd766SRichard Henderson return true; 2602458dd766SRichard Henderson } 2603458dd766SRichard Henderson 2604f725245cSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */ 2605f725245cSPhilippe Mathieu-Daudé 2606ad71ed68SBlue Swirl /*****************************************************************************/ 2607ad71ed68SBlue Swirl /* Exceptions processing helpers */ 2608ad71ed68SBlue Swirl 2609db789c6cSBenjamin Herrenschmidt void raise_exception_err_ra(CPUPPCState *env, uint32_t exception, 2610db789c6cSBenjamin Herrenschmidt uint32_t error_code, uintptr_t raddr) 2611ad71ed68SBlue Swirl { 2612db70b311SRichard Henderson CPUState *cs = env_cpu(env); 261327103424SAndreas Färber 261427103424SAndreas Färber cs->exception_index = exception; 2615ad71ed68SBlue Swirl env->error_code = error_code; 2616db789c6cSBenjamin Herrenschmidt cpu_loop_exit_restore(cs, raddr); 2617db789c6cSBenjamin Herrenschmidt } 2618db789c6cSBenjamin Herrenschmidt 2619db789c6cSBenjamin Herrenschmidt void raise_exception_err(CPUPPCState *env, uint32_t exception, 2620db789c6cSBenjamin Herrenschmidt uint32_t error_code) 2621db789c6cSBenjamin Herrenschmidt { 2622db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, error_code, 0); 2623db789c6cSBenjamin Herrenschmidt } 2624db789c6cSBenjamin Herrenschmidt 2625db789c6cSBenjamin Herrenschmidt void raise_exception(CPUPPCState *env, uint32_t exception) 2626db789c6cSBenjamin Herrenschmidt { 2627db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, 0, 0); 2628db789c6cSBenjamin Herrenschmidt } 2629db789c6cSBenjamin Herrenschmidt 2630db789c6cSBenjamin Herrenschmidt void raise_exception_ra(CPUPPCState *env, uint32_t exception, 2631db789c6cSBenjamin Herrenschmidt uintptr_t raddr) 2632db789c6cSBenjamin Herrenschmidt { 2633db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, 0, raddr); 2634db789c6cSBenjamin Herrenschmidt } 2635db789c6cSBenjamin Herrenschmidt 26362b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 2637db789c6cSBenjamin Herrenschmidt void helper_raise_exception_err(CPUPPCState *env, uint32_t exception, 2638db789c6cSBenjamin Herrenschmidt uint32_t error_code) 2639db789c6cSBenjamin Herrenschmidt { 2640db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, error_code, 0); 2641ad71ed68SBlue Swirl } 2642ad71ed68SBlue Swirl 2643e5f17ac6SBlue Swirl void helper_raise_exception(CPUPPCState *env, uint32_t exception) 2644ad71ed68SBlue Swirl { 2645db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, 0, 0); 2646ad71ed68SBlue Swirl } 26472b44e219SBruno Larsen (billionai) #endif 2648ad71ed68SBlue Swirl 2649ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY) 26502b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 2651e5f17ac6SBlue Swirl void helper_store_msr(CPUPPCState *env, target_ulong val) 2652ad71ed68SBlue Swirl { 2653db789c6cSBenjamin Herrenschmidt uint32_t excp = hreg_store_msr(env, val, 0); 2654259186a7SAndreas Färber 2655db789c6cSBenjamin Herrenschmidt if (excp != 0) { 26560661329aSBALATON Zoltan cpu_interrupt_exittb(env_cpu(env)); 2657db789c6cSBenjamin Herrenschmidt raise_exception(env, excp); 2658ad71ed68SBlue Swirl } 2659ad71ed68SBlue Swirl } 2660ad71ed68SBlue Swirl 26612fdedcbcSMatheus Ferst void helper_ppc_maybe_interrupt(CPUPPCState *env) 26622fdedcbcSMatheus Ferst { 26632fdedcbcSMatheus Ferst ppc_maybe_interrupt(env); 26642fdedcbcSMatheus Ferst } 26652fdedcbcSMatheus Ferst 26667778a575SBenjamin Herrenschmidt #if defined(TARGET_PPC64) 2667f43520e5SRichard Henderson void helper_scv(CPUPPCState *env, uint32_t lev) 2668f43520e5SRichard Henderson { 2669f43520e5SRichard Henderson if (env->spr[SPR_FSCR] & (1ull << FSCR_SCV)) { 2670f43520e5SRichard Henderson raise_exception_err(env, POWERPC_EXCP_SYSCALL_VECTORED, lev); 2671f43520e5SRichard Henderson } else { 2672f43520e5SRichard Henderson raise_exception_err(env, POWERPC_EXCP_FU, FSCR_IC_SCV); 2673f43520e5SRichard Henderson } 2674f43520e5SRichard Henderson } 2675f43520e5SRichard Henderson 267607e4804fSCédric Le Goater void helper_pminsn(CPUPPCState *env, uint32_t insn) 26777778a575SBenjamin Herrenschmidt { 2678*4e6b7db2SBALATON Zoltan CPUState *cs = env_cpu(env); 26797778a575SBenjamin Herrenschmidt 26807778a575SBenjamin Herrenschmidt cs->halted = 1; 26817778a575SBenjamin Herrenschmidt 26823621e2c9SBenjamin Herrenschmidt /* Condition for waking up at 0x100 */ 26831e7fd61dSBenjamin Herrenschmidt env->resume_as_sreset = (insn != PPC_PM_STOP) || 268421c0d66aSBenjamin Herrenschmidt (env->spr[SPR_PSSCR] & PSSCR_EC); 26852fdedcbcSMatheus Ferst 26862fdedcbcSMatheus Ferst ppc_maybe_interrupt(env); 26877778a575SBenjamin Herrenschmidt } 26887778a575SBenjamin Herrenschmidt #endif /* defined(TARGET_PPC64) */ 26897778a575SBenjamin Herrenschmidt 269062e79ef9SCédric Le Goater static void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr) 2691ad71ed68SBlue Swirl { 2692a2e71b28SBenjamin Herrenschmidt /* MSR:POW cannot be set by any form of rfi */ 2693a2e71b28SBenjamin Herrenschmidt msr &= ~(1ULL << MSR_POW); 2694a2e71b28SBenjamin Herrenschmidt 26955aad0457SChristophe Leroy /* MSR:TGPR cannot be set by any form of rfi */ 26965aad0457SChristophe Leroy if (env->flags & POWERPC_FLAG_TGPR) 26975aad0457SChristophe Leroy msr &= ~(1ULL << MSR_TGPR); 26985aad0457SChristophe Leroy 2699ad71ed68SBlue Swirl #if defined(TARGET_PPC64) 2700a2e71b28SBenjamin Herrenschmidt /* Switching to 32-bit ? Crop the nip */ 2701a2e71b28SBenjamin Herrenschmidt if (!msr_is_64bit(env, msr)) { 2702ad71ed68SBlue Swirl nip = (uint32_t)nip; 2703ad71ed68SBlue Swirl } 2704ad71ed68SBlue Swirl #else 2705ad71ed68SBlue Swirl nip = (uint32_t)nip; 2706ad71ed68SBlue Swirl #endif 2707ad71ed68SBlue Swirl /* XXX: beware: this is false if VLE is supported */ 2708ad71ed68SBlue Swirl env->nip = nip & ~((target_ulong)0x00000003); 2709ad71ed68SBlue Swirl hreg_store_msr(env, msr, 1); 27102eb1ef73SCédric Le Goater trace_ppc_excp_rfi(env->nip, env->msr); 271147733729SDavid Gibson /* 271247733729SDavid Gibson * No need to raise an exception here, as rfi is always the last 271347733729SDavid Gibson * insn of a TB 2714ad71ed68SBlue Swirl */ 27150661329aSBALATON Zoltan cpu_interrupt_exittb(env_cpu(env)); 2716a8b73734SNikunj A Dadhania /* Reset the reservation */ 2717a8b73734SNikunj A Dadhania env->reserve_addr = -1; 2718a8b73734SNikunj A Dadhania 2719cd0c6f47SBenjamin Herrenschmidt /* Context synchronizing: check if TCG TLB needs flush */ 2720e3cffe6fSNikunj A Dadhania check_tlb_flush(env, false); 2721ad71ed68SBlue Swirl } 2722ad71ed68SBlue Swirl 2723e5f17ac6SBlue Swirl void helper_rfi(CPUPPCState *env) 2724ad71ed68SBlue Swirl { 2725a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful); 2726a1bb7384SScott Wood } 2727ad71ed68SBlue Swirl 2728ad71ed68SBlue Swirl #if defined(TARGET_PPC64) 2729e5f17ac6SBlue Swirl void helper_rfid(CPUPPCState *env) 2730ad71ed68SBlue Swirl { 273147733729SDavid Gibson /* 2732136fbf65Szhaolichang * The architecture defines a number of rules for which bits can 273347733729SDavid Gibson * change but in practice, we handle this in hreg_store_msr() 2734a2e71b28SBenjamin Herrenschmidt * which will be called by do_rfi(), so there is no need to filter 2735a2e71b28SBenjamin Herrenschmidt * here 2736a2e71b28SBenjamin Herrenschmidt */ 2737a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]); 2738ad71ed68SBlue Swirl } 2739ad71ed68SBlue Swirl 27403c89b8d6SNicholas Piggin void helper_rfscv(CPUPPCState *env) 27413c89b8d6SNicholas Piggin { 27423c89b8d6SNicholas Piggin do_rfi(env, env->lr, env->ctr); 27433c89b8d6SNicholas Piggin } 27443c89b8d6SNicholas Piggin 2745e5f17ac6SBlue Swirl void helper_hrfid(CPUPPCState *env) 2746ad71ed68SBlue Swirl { 2747a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]); 2748ad71ed68SBlue Swirl } 2749ad71ed68SBlue Swirl #endif 2750ad71ed68SBlue Swirl 27511f26c751SDaniel Henrique Barboza #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY) 27521f26c751SDaniel Henrique Barboza void helper_rfebb(CPUPPCState *env, target_ulong s) 27531f26c751SDaniel Henrique Barboza { 27541f26c751SDaniel Henrique Barboza target_ulong msr = env->msr; 27551f26c751SDaniel Henrique Barboza 27561f26c751SDaniel Henrique Barboza /* 27571f26c751SDaniel Henrique Barboza * Handling of BESCR bits 32:33 according to PowerISA v3.1: 27581f26c751SDaniel Henrique Barboza * 27591f26c751SDaniel Henrique Barboza * "If BESCR 32:33 != 0b00 the instruction is treated as if 27601f26c751SDaniel Henrique Barboza * the instruction form were invalid." 27611f26c751SDaniel Henrique Barboza */ 27621f26c751SDaniel Henrique Barboza if (env->spr[SPR_BESCR] & BESCR_INVALID) { 27631f26c751SDaniel Henrique Barboza raise_exception_err(env, POWERPC_EXCP_PROGRAM, 27641f26c751SDaniel Henrique Barboza POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL); 27651f26c751SDaniel Henrique Barboza } 27661f26c751SDaniel Henrique Barboza 27671f26c751SDaniel Henrique Barboza env->nip = env->spr[SPR_EBBRR]; 27681f26c751SDaniel Henrique Barboza 27691f26c751SDaniel Henrique Barboza /* Switching to 32-bit ? Crop the nip */ 27701f26c751SDaniel Henrique Barboza if (!msr_is_64bit(env, msr)) { 27711f26c751SDaniel Henrique Barboza env->nip = (uint32_t)env->spr[SPR_EBBRR]; 27721f26c751SDaniel Henrique Barboza } 27731f26c751SDaniel Henrique Barboza 27741f26c751SDaniel Henrique Barboza if (s) { 27751f26c751SDaniel Henrique Barboza env->spr[SPR_BESCR] |= BESCR_GE; 27761f26c751SDaniel Henrique Barboza } else { 27771f26c751SDaniel Henrique Barboza env->spr[SPR_BESCR] &= ~BESCR_GE; 27781f26c751SDaniel Henrique Barboza } 27791f26c751SDaniel Henrique Barboza } 2780d3412df2SDaniel Henrique Barboza 2781d3412df2SDaniel Henrique Barboza /* 2782d3412df2SDaniel Henrique Barboza * Triggers or queues an 'ebb_excp' EBB exception. All checks 2783d3412df2SDaniel Henrique Barboza * but FSCR, HFSCR and msr_pr must be done beforehand. 2784d3412df2SDaniel Henrique Barboza * 2785d3412df2SDaniel Henrique Barboza * PowerISA v3.1 isn't clear about whether an EBB should be 2786d3412df2SDaniel Henrique Barboza * postponed or cancelled if the EBB facility is unavailable. 2787d3412df2SDaniel Henrique Barboza * Our assumption here is that the EBB is cancelled if both 2788d3412df2SDaniel Henrique Barboza * FSCR and HFSCR EBB facilities aren't available. 2789d3412df2SDaniel Henrique Barboza */ 2790d3412df2SDaniel Henrique Barboza static void do_ebb(CPUPPCState *env, int ebb_excp) 2791d3412df2SDaniel Henrique Barboza { 2792d3412df2SDaniel Henrique Barboza PowerPCCPU *cpu = env_archcpu(env); 2793d3412df2SDaniel Henrique Barboza 2794d3412df2SDaniel Henrique Barboza /* 2795d3412df2SDaniel Henrique Barboza * FSCR_EBB and FSCR_IC_EBB are the same bits used with 2796d3412df2SDaniel Henrique Barboza * HFSCR. 2797d3412df2SDaniel Henrique Barboza */ 2798d3412df2SDaniel Henrique Barboza helper_fscr_facility_check(env, FSCR_EBB, 0, FSCR_IC_EBB); 2799d3412df2SDaniel Henrique Barboza helper_hfscr_facility_check(env, FSCR_EBB, "EBB", FSCR_IC_EBB); 2800d3412df2SDaniel Henrique Barboza 2801d3412df2SDaniel Henrique Barboza if (ebb_excp == POWERPC_EXCP_PERFM_EBB) { 2802d3412df2SDaniel Henrique Barboza env->spr[SPR_BESCR] |= BESCR_PMEO; 2803d3412df2SDaniel Henrique Barboza } else if (ebb_excp == POWERPC_EXCP_EXTERNAL_EBB) { 2804d3412df2SDaniel Henrique Barboza env->spr[SPR_BESCR] |= BESCR_EEO; 2805d3412df2SDaniel Henrique Barboza } 2806d3412df2SDaniel Henrique Barboza 2807d41ccf6eSVíctor Colombo if (FIELD_EX64(env->msr, MSR, PR)) { 2808d3412df2SDaniel Henrique Barboza powerpc_excp(cpu, ebb_excp); 2809d3412df2SDaniel Henrique Barboza } else { 28107b694df6SMatheus Ferst ppc_set_irq(cpu, PPC_INTERRUPT_EBB, 1); 2811d3412df2SDaniel Henrique Barboza } 2812d3412df2SDaniel Henrique Barboza } 2813d3412df2SDaniel Henrique Barboza 2814d3412df2SDaniel Henrique Barboza void raise_ebb_perfm_exception(CPUPPCState *env) 2815d3412df2SDaniel Henrique Barboza { 2816d3412df2SDaniel Henrique Barboza bool perfm_ebb_enabled = env->spr[SPR_POWER_MMCR0] & MMCR0_EBE && 2817d3412df2SDaniel Henrique Barboza env->spr[SPR_BESCR] & BESCR_PME && 2818d3412df2SDaniel Henrique Barboza env->spr[SPR_BESCR] & BESCR_GE; 2819d3412df2SDaniel Henrique Barboza 2820d3412df2SDaniel Henrique Barboza if (!perfm_ebb_enabled) { 2821d3412df2SDaniel Henrique Barboza return; 2822d3412df2SDaniel Henrique Barboza } 2823d3412df2SDaniel Henrique Barboza 2824d3412df2SDaniel Henrique Barboza do_ebb(env, POWERPC_EXCP_PERFM_EBB); 2825d3412df2SDaniel Henrique Barboza } 28261f26c751SDaniel Henrique Barboza #endif 28271f26c751SDaniel Henrique Barboza 2828ad71ed68SBlue Swirl /*****************************************************************************/ 2829ad71ed68SBlue Swirl /* Embedded PowerPC specific helpers */ 2830e5f17ac6SBlue Swirl void helper_40x_rfci(CPUPPCState *env) 2831ad71ed68SBlue Swirl { 2832a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]); 2833ad71ed68SBlue Swirl } 2834ad71ed68SBlue Swirl 2835e5f17ac6SBlue Swirl void helper_rfci(CPUPPCState *env) 2836ad71ed68SBlue Swirl { 2837a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]); 2838ad71ed68SBlue Swirl } 2839ad71ed68SBlue Swirl 2840e5f17ac6SBlue Swirl void helper_rfdi(CPUPPCState *env) 2841ad71ed68SBlue Swirl { 2842a1bb7384SScott Wood /* FIXME: choose CSRR1 or DSRR1 based on cpu type */ 2843a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]); 2844ad71ed68SBlue Swirl } 2845ad71ed68SBlue Swirl 2846e5f17ac6SBlue Swirl void helper_rfmci(CPUPPCState *env) 2847ad71ed68SBlue Swirl { 2848a1bb7384SScott Wood /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */ 2849a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]); 2850ad71ed68SBlue Swirl } 28512b44e219SBruno Larsen (billionai) #endif /* CONFIG_TCG */ 28522b44e219SBruno Larsen (billionai) #endif /* !defined(CONFIG_USER_ONLY) */ 2853ad71ed68SBlue Swirl 28542b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 2855e5f17ac6SBlue Swirl void helper_tw(CPUPPCState *env, target_ulong arg1, target_ulong arg2, 2856e5f17ac6SBlue Swirl uint32_t flags) 2857ad71ed68SBlue Swirl { 2858ad71ed68SBlue Swirl if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) || 2859ad71ed68SBlue Swirl ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) || 2860ad71ed68SBlue Swirl ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) || 2861ad71ed68SBlue Swirl ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) || 2862ad71ed68SBlue Swirl ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) { 286372073dccSBenjamin Herrenschmidt raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 286472073dccSBenjamin Herrenschmidt POWERPC_EXCP_TRAP, GETPC()); 2865ad71ed68SBlue Swirl } 2866ad71ed68SBlue Swirl } 2867ad71ed68SBlue Swirl 2868ad71ed68SBlue Swirl #if defined(TARGET_PPC64) 2869e5f17ac6SBlue Swirl void helper_td(CPUPPCState *env, target_ulong arg1, target_ulong arg2, 2870e5f17ac6SBlue Swirl uint32_t flags) 2871ad71ed68SBlue Swirl { 2872ad71ed68SBlue Swirl if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) || 2873ad71ed68SBlue Swirl ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) || 2874ad71ed68SBlue Swirl ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) || 2875ad71ed68SBlue Swirl ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) || 2876ad71ed68SBlue Swirl ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) { 287772073dccSBenjamin Herrenschmidt raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 287872073dccSBenjamin Herrenschmidt POWERPC_EXCP_TRAP, GETPC()); 2879ad71ed68SBlue Swirl } 2880ad71ed68SBlue Swirl } 2881ad71ed68SBlue Swirl #endif 28822b44e219SBruno Larsen (billionai) #endif 2883ad71ed68SBlue Swirl 2884049b4ad6SVaibhav Jain #ifdef CONFIG_TCG 2885670f1da3SVíctor Colombo static uint32_t helper_SIMON_LIKE_32_64(uint32_t x, uint64_t key, uint32_t lane) 2886670f1da3SVíctor Colombo { 2887670f1da3SVíctor Colombo const uint16_t c = 0xfffc; 2888670f1da3SVíctor Colombo const uint64_t z0 = 0xfa2561cdf44ac398ULL; 2889670f1da3SVíctor Colombo uint16_t z = 0, temp; 2890670f1da3SVíctor Colombo uint16_t k[32], eff_k[32], xleft[33], xright[33], fxleft[32]; 2891670f1da3SVíctor Colombo 2892670f1da3SVíctor Colombo for (int i = 3; i >= 0; i--) { 2893670f1da3SVíctor Colombo k[i] = key & 0xffff; 2894670f1da3SVíctor Colombo key >>= 16; 2895670f1da3SVíctor Colombo } 2896670f1da3SVíctor Colombo xleft[0] = x & 0xffff; 2897670f1da3SVíctor Colombo xright[0] = (x >> 16) & 0xffff; 2898670f1da3SVíctor Colombo 2899670f1da3SVíctor Colombo for (int i = 0; i < 28; i++) { 2900670f1da3SVíctor Colombo z = (z0 >> (63 - i)) & 1; 2901670f1da3SVíctor Colombo temp = ror16(k[i + 3], 3) ^ k[i + 1]; 2902670f1da3SVíctor Colombo k[i + 4] = c ^ z ^ k[i] ^ temp ^ ror16(temp, 1); 2903670f1da3SVíctor Colombo } 2904670f1da3SVíctor Colombo 2905670f1da3SVíctor Colombo for (int i = 0; i < 8; i++) { 2906670f1da3SVíctor Colombo eff_k[4 * i + 0] = k[4 * i + ((0 + lane) % 4)]; 2907670f1da3SVíctor Colombo eff_k[4 * i + 1] = k[4 * i + ((1 + lane) % 4)]; 2908670f1da3SVíctor Colombo eff_k[4 * i + 2] = k[4 * i + ((2 + lane) % 4)]; 2909670f1da3SVíctor Colombo eff_k[4 * i + 3] = k[4 * i + ((3 + lane) % 4)]; 2910670f1da3SVíctor Colombo } 2911670f1da3SVíctor Colombo 2912670f1da3SVíctor Colombo for (int i = 0; i < 32; i++) { 2913670f1da3SVíctor Colombo fxleft[i] = (rol16(xleft[i], 1) & 2914670f1da3SVíctor Colombo rol16(xleft[i], 8)) ^ rol16(xleft[i], 2); 2915670f1da3SVíctor Colombo xleft[i + 1] = xright[i] ^ fxleft[i] ^ eff_k[i]; 2916670f1da3SVíctor Colombo xright[i + 1] = xleft[i]; 2917670f1da3SVíctor Colombo } 2918670f1da3SVíctor Colombo 2919670f1da3SVíctor Colombo return (((uint32_t)xright[32]) << 16) | xleft[32]; 2920670f1da3SVíctor Colombo } 2921670f1da3SVíctor Colombo 2922670f1da3SVíctor Colombo static uint64_t hash_digest(uint64_t ra, uint64_t rb, uint64_t key) 2923670f1da3SVíctor Colombo { 2924670f1da3SVíctor Colombo uint64_t stage0_h = 0ULL, stage0_l = 0ULL; 2925670f1da3SVíctor Colombo uint64_t stage1_h, stage1_l; 2926670f1da3SVíctor Colombo 2927670f1da3SVíctor Colombo for (int i = 0; i < 4; i++) { 2928670f1da3SVíctor Colombo stage0_h |= ror64(rb & 0xff, 8 * (2 * i + 1)); 2929670f1da3SVíctor Colombo stage0_h |= ((ra >> 32) & 0xff) << (8 * 2 * i); 2930670f1da3SVíctor Colombo stage0_l |= ror64((rb >> 32) & 0xff, 8 * (2 * i + 1)); 2931670f1da3SVíctor Colombo stage0_l |= (ra & 0xff) << (8 * 2 * i); 2932670f1da3SVíctor Colombo rb >>= 8; 2933670f1da3SVíctor Colombo ra >>= 8; 2934670f1da3SVíctor Colombo } 2935670f1da3SVíctor Colombo 2936670f1da3SVíctor Colombo stage1_h = (uint64_t)helper_SIMON_LIKE_32_64(stage0_h >> 32, key, 0) << 32; 2937670f1da3SVíctor Colombo stage1_h |= helper_SIMON_LIKE_32_64(stage0_h, key, 1); 2938670f1da3SVíctor Colombo stage1_l = (uint64_t)helper_SIMON_LIKE_32_64(stage0_l >> 32, key, 2) << 32; 2939670f1da3SVíctor Colombo stage1_l |= helper_SIMON_LIKE_32_64(stage0_l, key, 3); 2940670f1da3SVíctor Colombo 2941670f1da3SVíctor Colombo return stage1_h ^ stage1_l; 2942670f1da3SVíctor Colombo } 2943670f1da3SVíctor Colombo 29444091fabfSNicholas Miehlbradt static void do_hash(CPUPPCState *env, target_ulong ea, target_ulong ra, 29454091fabfSNicholas Miehlbradt target_ulong rb, uint64_t key, bool store) 29464091fabfSNicholas Miehlbradt { 29474091fabfSNicholas Miehlbradt uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash; 29484091fabfSNicholas Miehlbradt 29494091fabfSNicholas Miehlbradt if (store) { 29504091fabfSNicholas Miehlbradt cpu_stq_data_ra(env, ea, calculated_hash, GETPC()); 29514091fabfSNicholas Miehlbradt } else { 29524091fabfSNicholas Miehlbradt loaded_hash = cpu_ldq_data_ra(env, ea, GETPC()); 29534091fabfSNicholas Miehlbradt if (loaded_hash != calculated_hash) { 29544091fabfSNicholas Miehlbradt raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 29554091fabfSNicholas Miehlbradt POWERPC_EXCP_TRAP, GETPC()); 29564091fabfSNicholas Miehlbradt } 29574091fabfSNicholas Miehlbradt } 29584091fabfSNicholas Miehlbradt } 29594091fabfSNicholas Miehlbradt 2960670f1da3SVíctor Colombo #include "qemu/guest-random.h" 2961670f1da3SVíctor Colombo 29624091fabfSNicholas Miehlbradt #ifdef TARGET_PPC64 29634091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect) \ 2964670f1da3SVíctor Colombo void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra, \ 2965670f1da3SVíctor Colombo target_ulong rb) \ 2966670f1da3SVíctor Colombo { \ 29674091fabfSNicholas Miehlbradt if (env->msr & R_MSR_PR_MASK) { \ 29684091fabfSNicholas Miehlbradt if (!(env->spr[SPR_DEXCR] & R_DEXCR_PRO_##dexcr_aspect##_MASK || \ 29694091fabfSNicholas Miehlbradt env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK)) \ 29704091fabfSNicholas Miehlbradt return; \ 29714091fabfSNicholas Miehlbradt } else if (!(env->msr & R_MSR_HV_MASK)) { \ 29724091fabfSNicholas Miehlbradt if (!(env->spr[SPR_DEXCR] & R_DEXCR_PNH_##dexcr_aspect##_MASK || \ 29734091fabfSNicholas Miehlbradt env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK)) \ 29744091fabfSNicholas Miehlbradt return; \ 29754091fabfSNicholas Miehlbradt } else if (!(env->msr & R_MSR_S_MASK)) { \ 29764091fabfSNicholas Miehlbradt if (!(env->spr[SPR_HDEXCR] & R_HDEXCR_HNU_##dexcr_aspect##_MASK)) \ 29774091fabfSNicholas Miehlbradt return; \ 29784091fabfSNicholas Miehlbradt } \ 2979670f1da3SVíctor Colombo \ 29804091fabfSNicholas Miehlbradt do_hash(env, ea, ra, rb, key, store); \ 2981670f1da3SVíctor Colombo } 29824091fabfSNicholas Miehlbradt #else 29834091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect) \ 29844091fabfSNicholas Miehlbradt void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra, \ 29854091fabfSNicholas Miehlbradt target_ulong rb) \ 29864091fabfSNicholas Miehlbradt { \ 29874091fabfSNicholas Miehlbradt do_hash(env, ea, ra, rb, key, store); \ 29884091fabfSNicholas Miehlbradt } 29894091fabfSNicholas Miehlbradt #endif /* TARGET_PPC64 */ 2990670f1da3SVíctor Colombo 29914091fabfSNicholas Miehlbradt HELPER_HASH(HASHST, env->spr[SPR_HASHKEYR], true, NPHIE) 29924091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHK, env->spr[SPR_HASHKEYR], false, NPHIE) 29934091fabfSNicholas Miehlbradt HELPER_HASH(HASHSTP, env->spr[SPR_HASHPKEYR], true, PHIE) 29944091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHKP, env->spr[SPR_HASHPKEYR], false, PHIE) 2995049b4ad6SVaibhav Jain #endif /* CONFIG_TCG */ 2996670f1da3SVíctor Colombo 2997ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY) 2998ad71ed68SBlue Swirl 29992b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 3000ad71ed68SBlue Swirl 3001ad71ed68SBlue Swirl /* Embedded.Processor Control */ 3002ad71ed68SBlue Swirl static int dbell2irq(target_ulong rb) 3003ad71ed68SBlue Swirl { 3004ad71ed68SBlue Swirl int msg = rb & DBELL_TYPE_MASK; 3005ad71ed68SBlue Swirl int irq = -1; 3006ad71ed68SBlue Swirl 3007ad71ed68SBlue Swirl switch (msg) { 3008ad71ed68SBlue Swirl case DBELL_TYPE_DBELL: 3009ad71ed68SBlue Swirl irq = PPC_INTERRUPT_DOORBELL; 3010ad71ed68SBlue Swirl break; 3011ad71ed68SBlue Swirl case DBELL_TYPE_DBELL_CRIT: 3012ad71ed68SBlue Swirl irq = PPC_INTERRUPT_CDOORBELL; 3013ad71ed68SBlue Swirl break; 3014ad71ed68SBlue Swirl case DBELL_TYPE_G_DBELL: 3015ad71ed68SBlue Swirl case DBELL_TYPE_G_DBELL_CRIT: 3016ad71ed68SBlue Swirl case DBELL_TYPE_G_DBELL_MC: 3017ad71ed68SBlue Swirl /* XXX implement */ 3018ad71ed68SBlue Swirl default: 3019ad71ed68SBlue Swirl break; 3020ad71ed68SBlue Swirl } 3021ad71ed68SBlue Swirl 3022ad71ed68SBlue Swirl return irq; 3023ad71ed68SBlue Swirl } 3024ad71ed68SBlue Swirl 3025e5f17ac6SBlue Swirl void helper_msgclr(CPUPPCState *env, target_ulong rb) 3026ad71ed68SBlue Swirl { 3027ad71ed68SBlue Swirl int irq = dbell2irq(rb); 3028ad71ed68SBlue Swirl 3029ad71ed68SBlue Swirl if (irq < 0) { 3030ad71ed68SBlue Swirl return; 3031ad71ed68SBlue Swirl } 3032ad71ed68SBlue Swirl 30337b694df6SMatheus Ferst ppc_set_irq(env_archcpu(env), irq, 0); 3034ad71ed68SBlue Swirl } 3035ad71ed68SBlue Swirl 3036ad71ed68SBlue Swirl void helper_msgsnd(target_ulong rb) 3037ad71ed68SBlue Swirl { 3038ad71ed68SBlue Swirl int irq = dbell2irq(rb); 3039ad71ed68SBlue Swirl int pir = rb & DBELL_PIRTAG_MASK; 3040182735efSAndreas Färber CPUState *cs; 3041ad71ed68SBlue Swirl 3042ad71ed68SBlue Swirl if (irq < 0) { 3043ad71ed68SBlue Swirl return; 3044ad71ed68SBlue Swirl } 3045ad71ed68SBlue Swirl 3046f1c29ebcSThomas Huth qemu_mutex_lock_iothread(); 3047bdc44640SAndreas Färber CPU_FOREACH(cs) { 3048182735efSAndreas Färber PowerPCCPU *cpu = POWERPC_CPU(cs); 3049182735efSAndreas Färber CPUPPCState *cenv = &cpu->env; 3050182735efSAndreas Färber 3051ad71ed68SBlue Swirl if ((rb & DBELL_BRDCAST) || (cenv->spr[SPR_BOOKE_PIR] == pir)) { 30527b694df6SMatheus Ferst ppc_set_irq(cpu, irq, 1); 3053ad71ed68SBlue Swirl } 3054ad71ed68SBlue Swirl } 3055f1c29ebcSThomas Huth qemu_mutex_unlock_iothread(); 3056ad71ed68SBlue Swirl } 30577af1e7b0SCédric Le Goater 30587af1e7b0SCédric Le Goater /* Server Processor Control */ 30597af1e7b0SCédric Le Goater 30605ba7ba1dSCédric Le Goater static bool dbell_type_server(target_ulong rb) 30615ba7ba1dSCédric Le Goater { 306247733729SDavid Gibson /* 306347733729SDavid Gibson * A Directed Hypervisor Doorbell message is sent only if the 30647af1e7b0SCédric Le Goater * message type is 5. All other types are reserved and the 306547733729SDavid Gibson * instruction is a no-op 306647733729SDavid Gibson */ 30675ba7ba1dSCédric Le Goater return (rb & DBELL_TYPE_MASK) == DBELL_TYPE_DBELL_SERVER; 30687af1e7b0SCédric Le Goater } 30697af1e7b0SCédric Le Goater 30707af1e7b0SCédric Le Goater void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb) 30717af1e7b0SCédric Le Goater { 30725ba7ba1dSCédric Le Goater if (!dbell_type_server(rb)) { 30737af1e7b0SCédric Le Goater return; 30747af1e7b0SCédric Le Goater } 30757af1e7b0SCédric Le Goater 30767b694df6SMatheus Ferst ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0); 30777af1e7b0SCédric Le Goater } 30787af1e7b0SCédric Le Goater 30795ba7ba1dSCédric Le Goater static void book3s_msgsnd_common(int pir, int irq) 30807af1e7b0SCédric Le Goater { 30817af1e7b0SCédric Le Goater CPUState *cs; 30827af1e7b0SCédric Le Goater 30837af1e7b0SCédric Le Goater qemu_mutex_lock_iothread(); 30847af1e7b0SCédric Le Goater CPU_FOREACH(cs) { 30857af1e7b0SCédric Le Goater PowerPCCPU *cpu = POWERPC_CPU(cs); 30867af1e7b0SCédric Le Goater CPUPPCState *cenv = &cpu->env; 30877af1e7b0SCédric Le Goater 30887af1e7b0SCédric Le Goater /* TODO: broadcast message to all threads of the same processor */ 30897af1e7b0SCédric Le Goater if (cenv->spr_cb[SPR_PIR].default_value == pir) { 30907b694df6SMatheus Ferst ppc_set_irq(cpu, irq, 1); 30917af1e7b0SCédric Le Goater } 30927af1e7b0SCédric Le Goater } 30937af1e7b0SCédric Le Goater qemu_mutex_unlock_iothread(); 30947af1e7b0SCédric Le Goater } 30955ba7ba1dSCédric Le Goater 30965ba7ba1dSCédric Le Goater void helper_book3s_msgsnd(target_ulong rb) 30975ba7ba1dSCédric Le Goater { 30985ba7ba1dSCédric Le Goater int pir = rb & DBELL_PROCIDTAG_MASK; 30995ba7ba1dSCédric Le Goater 31005ba7ba1dSCédric Le Goater if (!dbell_type_server(rb)) { 31015ba7ba1dSCédric Le Goater return; 31025ba7ba1dSCédric Le Goater } 31035ba7ba1dSCédric Le Goater 31045ba7ba1dSCédric Le Goater book3s_msgsnd_common(pir, PPC_INTERRUPT_HDOORBELL); 31055ba7ba1dSCédric Le Goater } 31065ba7ba1dSCédric Le Goater 31075ba7ba1dSCédric Le Goater #if defined(TARGET_PPC64) 31085ba7ba1dSCédric Le Goater void helper_book3s_msgclrp(CPUPPCState *env, target_ulong rb) 31095ba7ba1dSCédric Le Goater { 3110493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "msgclrp", HFSCR_IC_MSGP); 3111493028d8SCédric Le Goater 31125ba7ba1dSCédric Le Goater if (!dbell_type_server(rb)) { 31135ba7ba1dSCédric Le Goater return; 31145ba7ba1dSCédric Le Goater } 31155ba7ba1dSCédric Le Goater 31162e985555SNicholas Piggin ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_DOORBELL, 0); 31175ba7ba1dSCédric Le Goater } 31185ba7ba1dSCédric Le Goater 31195ba7ba1dSCédric Le Goater /* 3120d24e80b2SNicholas Piggin * sends a message to another thread on the same 31215ba7ba1dSCédric Le Goater * multi-threaded processor 31225ba7ba1dSCédric Le Goater */ 31235ba7ba1dSCédric Le Goater void helper_book3s_msgsndp(CPUPPCState *env, target_ulong rb) 31245ba7ba1dSCédric Le Goater { 3125d24e80b2SNicholas Piggin CPUState *cs = env_cpu(env); 3126d24e80b2SNicholas Piggin PowerPCCPU *cpu = POWERPC_CPU(cs); 3127d24e80b2SNicholas Piggin CPUState *ccs; 3128d24e80b2SNicholas Piggin uint32_t nr_threads = cs->nr_threads; 3129d24e80b2SNicholas Piggin int ttir = rb & PPC_BITMASK(57, 63); 31305ba7ba1dSCédric Le Goater 3131493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "msgsndp", HFSCR_IC_MSGP); 3132493028d8SCédric Le Goater 3133d24e80b2SNicholas Piggin if (!dbell_type_server(rb) || ttir >= nr_threads) { 31345ba7ba1dSCédric Le Goater return; 31355ba7ba1dSCédric Le Goater } 31365ba7ba1dSCédric Le Goater 3137d24e80b2SNicholas Piggin if (nr_threads == 1) { 3138d24e80b2SNicholas Piggin ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, 1); 3139d24e80b2SNicholas Piggin return; 3140d24e80b2SNicholas Piggin } 31415ba7ba1dSCédric Le Goater 3142d24e80b2SNicholas Piggin /* Does iothread need to be locked for walking CPU list? */ 3143d24e80b2SNicholas Piggin qemu_mutex_lock_iothread(); 3144d24e80b2SNicholas Piggin THREAD_SIBLING_FOREACH(cs, ccs) { 3145d24e80b2SNicholas Piggin PowerPCCPU *ccpu = POWERPC_CPU(ccs); 3146d24e80b2SNicholas Piggin uint32_t thread_id = ppc_cpu_tir(ccpu); 3147d24e80b2SNicholas Piggin 3148d24e80b2SNicholas Piggin if (ttir == thread_id) { 3149d24e80b2SNicholas Piggin ppc_set_irq(ccpu, PPC_INTERRUPT_DOORBELL, 1); 3150d24e80b2SNicholas Piggin qemu_mutex_unlock_iothread(); 3151d24e80b2SNicholas Piggin return; 3152d24e80b2SNicholas Piggin } 3153d24e80b2SNicholas Piggin } 3154d24e80b2SNicholas Piggin 3155d24e80b2SNicholas Piggin g_assert_not_reached(); 31565ba7ba1dSCédric Le Goater } 3157996473e4SRichard Henderson #endif /* TARGET_PPC64 */ 31580f3110faSRichard Henderson 31590f3110faSRichard Henderson void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr, 31600f3110faSRichard Henderson MMUAccessType access_type, 31610f3110faSRichard Henderson int mmu_idx, uintptr_t retaddr) 31620f3110faSRichard Henderson { 31630f3110faSRichard Henderson CPUPPCState *env = cs->env_ptr; 316429c4a336SFabiano Rosas uint32_t insn; 316529c4a336SFabiano Rosas 316629c4a336SFabiano Rosas /* Restore state and reload the insn we executed, for filling in DSISR. */ 31673d419a4dSRichard Henderson cpu_restore_state(cs, retaddr); 3168888050cfSNicholas Piggin insn = ppc_ldl_code(env, env->nip); 31690f3110faSRichard Henderson 3170a7e3af13SRichard Henderson switch (env->mmu_model) { 3171a7e3af13SRichard Henderson case POWERPC_MMU_SOFT_4xx: 3172a7e3af13SRichard Henderson env->spr[SPR_40x_DEAR] = vaddr; 3173a7e3af13SRichard Henderson break; 3174a7e3af13SRichard Henderson case POWERPC_MMU_BOOKE: 3175a7e3af13SRichard Henderson case POWERPC_MMU_BOOKE206: 3176a7e3af13SRichard Henderson env->spr[SPR_BOOKE_DEAR] = vaddr; 3177a7e3af13SRichard Henderson break; 3178a7e3af13SRichard Henderson default: 3179a7e3af13SRichard Henderson env->spr[SPR_DAR] = vaddr; 3180a7e3af13SRichard Henderson break; 3181a7e3af13SRichard Henderson } 3182a7e3af13SRichard Henderson 31830f3110faSRichard Henderson cs->exception_index = POWERPC_EXCP_ALIGN; 318429c4a336SFabiano Rosas env->error_code = insn & 0x03FF0000; 318529c4a336SFabiano Rosas cpu_loop_exit(cs); 31860f3110faSRichard Henderson } 3187996473e4SRichard Henderson #endif /* CONFIG_TCG */ 3188996473e4SRichard Henderson #endif /* !CONFIG_USER_ONLY */ 3189