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 143155a7fa34SNicholas Piggin msr |= env->error_code; 14329f338e4dSFabiano Rosas break; 143355a7fa34SNicholas Piggin 14349f338e4dSFabiano Rosas case POWERPC_EXCP_DSI: /* Data storage exception */ 14359f338e4dSFabiano Rosas trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]); 14369f338e4dSFabiano Rosas break; 14379f338e4dSFabiano Rosas case POWERPC_EXCP_ISI: /* Instruction storage exception */ 14389f338e4dSFabiano Rosas trace_ppc_excp_isi(msr, env->nip); 14399f338e4dSFabiano Rosas msr |= env->error_code; 14409f338e4dSFabiano Rosas break; 14419f338e4dSFabiano Rosas case POWERPC_EXCP_EXTERNAL: /* External input */ 14429f338e4dSFabiano Rosas { 14439f338e4dSFabiano Rosas bool lpes0; 14449f338e4dSFabiano Rosas 14459f338e4dSFabiano Rosas /* 144667baff77SFabiano Rosas * LPES0 is only taken into consideration if we support HV 144767baff77SFabiano Rosas * mode for this CPU. 14489f338e4dSFabiano Rosas */ 144967baff77SFabiano Rosas if (!env->has_hv_mode) { 145067baff77SFabiano Rosas break; 14519f338e4dSFabiano Rosas } 14529f338e4dSFabiano Rosas 145367baff77SFabiano Rosas lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 145467baff77SFabiano Rosas 14559f338e4dSFabiano Rosas if (!lpes0) { 14569f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 14579f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 14589f338e4dSFabiano Rosas srr0 = SPR_HSRR0; 14599f338e4dSFabiano Rosas srr1 = SPR_HSRR1; 14609f338e4dSFabiano Rosas } 146167baff77SFabiano Rosas 14629f338e4dSFabiano Rosas break; 14639f338e4dSFabiano Rosas } 14649f338e4dSFabiano Rosas case POWERPC_EXCP_ALIGN: /* Alignment exception */ 14654ee5d281SNicholas Piggin /* Optional DSISR update was removed from ISA v3.0 */ 14664ee5d281SNicholas Piggin if (!(env->insns_flags2 & PPC2_ISA300)) { 14679f338e4dSFabiano Rosas /* Get rS/rD and rA from faulting opcode */ 14689f338e4dSFabiano Rosas /* 14699f338e4dSFabiano Rosas * Note: the opcode fields will not be set properly for a 14709f338e4dSFabiano Rosas * direct store load/store, but nobody cares as nobody 14719f338e4dSFabiano Rosas * actually uses direct store segments. 14729f338e4dSFabiano Rosas */ 14739f338e4dSFabiano Rosas env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16; 14744ee5d281SNicholas Piggin } 14759f338e4dSFabiano Rosas break; 14769f338e4dSFabiano Rosas case POWERPC_EXCP_PROGRAM: /* Program exception */ 14779f338e4dSFabiano Rosas switch (env->error_code & ~0xF) { 14789f338e4dSFabiano Rosas case POWERPC_EXCP_FP: 1479da806a6cSVíctor Colombo if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) { 14809f338e4dSFabiano Rosas trace_ppc_excp_fp_ignore(); 14813680e994SNicholas Piggin powerpc_reset_excp_state(cpu); 14829f338e4dSFabiano Rosas return; 14839f338e4dSFabiano Rosas } 14849f338e4dSFabiano Rosas 14859f338e4dSFabiano Rosas /* 14869f338e4dSFabiano Rosas * FP exceptions always have NIP pointing to the faulting 14879f338e4dSFabiano Rosas * instruction, so always use store_next and claim we are 14889f338e4dSFabiano Rosas * precise in the MSR. 14899f338e4dSFabiano Rosas */ 14909f338e4dSFabiano Rosas msr |= 0x00100000; 14919f338e4dSFabiano Rosas break; 14929f338e4dSFabiano Rosas case POWERPC_EXCP_INVAL: 14939f338e4dSFabiano Rosas trace_ppc_excp_inval(env->nip); 14949f338e4dSFabiano Rosas msr |= 0x00080000; 14959f338e4dSFabiano Rosas break; 14969f338e4dSFabiano Rosas case POWERPC_EXCP_PRIV: 14979f338e4dSFabiano Rosas msr |= 0x00040000; 14989f338e4dSFabiano Rosas break; 14999f338e4dSFabiano Rosas case POWERPC_EXCP_TRAP: 15009f338e4dSFabiano Rosas msr |= 0x00020000; 15019f338e4dSFabiano Rosas break; 15029f338e4dSFabiano Rosas default: 15039f338e4dSFabiano Rosas /* Should never occur */ 15049f338e4dSFabiano Rosas cpu_abort(cs, "Invalid program exception %d. Aborting\n", 15059f338e4dSFabiano Rosas env->error_code); 15069f338e4dSFabiano Rosas break; 15079f338e4dSFabiano Rosas } 15089f338e4dSFabiano Rosas break; 15099f338e4dSFabiano Rosas case POWERPC_EXCP_SYSCALL: /* System call exception */ 15109f338e4dSFabiano Rosas lev = env->error_code; 15119f338e4dSFabiano Rosas 15122306c606SBALATON Zoltan if (lev == 1 && cpu->vhyp) { 15139f338e4dSFabiano Rosas dump_hcall(env); 15149f338e4dSFabiano Rosas } else { 15159f338e4dSFabiano Rosas dump_syscall(env); 15169f338e4dSFabiano Rosas } 15179f338e4dSFabiano Rosas 15189f338e4dSFabiano Rosas /* 15199f338e4dSFabiano Rosas * We need to correct the NIP which in this case is supposed 15209f338e4dSFabiano Rosas * to point to the next instruction 15219f338e4dSFabiano Rosas */ 15229f338e4dSFabiano Rosas env->nip += 4; 15239f338e4dSFabiano Rosas 15249f338e4dSFabiano Rosas /* "PAPR mode" built-in hypercall emulation */ 15252306c606SBALATON Zoltan if (lev == 1 && books_vhyp_handles_hcall(cpu)) { 15269f338e4dSFabiano Rosas PPCVirtualHypervisorClass *vhc = 15279f338e4dSFabiano Rosas PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 15289f338e4dSFabiano Rosas vhc->hypercall(cpu->vhyp, cpu); 15299f338e4dSFabiano Rosas return; 15309f338e4dSFabiano Rosas } 1531eb701f30SNicholas Piggin if (env->insns_flags2 & PPC2_ISA310) { 1532eb701f30SNicholas Piggin /* ISAv3.1 puts LEV into SRR1 */ 1533eb701f30SNicholas Piggin msr |= lev << 20; 1534eb701f30SNicholas Piggin } 15359f338e4dSFabiano Rosas if (lev == 1) { 15369f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 15379f338e4dSFabiano Rosas } 15389f338e4dSFabiano Rosas break; 15399f338e4dSFabiano Rosas case POWERPC_EXCP_SYSCALL_VECTORED: /* scv exception */ 15409f338e4dSFabiano Rosas lev = env->error_code; 15419f338e4dSFabiano Rosas dump_syscall(env); 15429f338e4dSFabiano Rosas env->nip += 4; 15439f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_EE); 15449f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 15459f338e4dSFabiano Rosas 15469f338e4dSFabiano Rosas vector += lev * 0x20; 15479f338e4dSFabiano Rosas 15489f338e4dSFabiano Rosas env->lr = env->nip; 15499f338e4dSFabiano Rosas env->ctr = msr; 15509f338e4dSFabiano Rosas break; 15519f338e4dSFabiano Rosas case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */ 15529f338e4dSFabiano Rosas case POWERPC_EXCP_DECR: /* Decrementer exception */ 15539f338e4dSFabiano Rosas break; 15549f338e4dSFabiano Rosas case POWERPC_EXCP_RESET: /* System reset exception */ 15559f338e4dSFabiano Rosas /* A power-saving exception sets ME, otherwise it is unchanged */ 15568e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 15579f338e4dSFabiano Rosas /* indicate that we resumed from power save mode */ 15589f338e4dSFabiano Rosas msr |= 0x10000; 15599f338e4dSFabiano Rosas new_msr |= ((target_ulong)1 << MSR_ME); 15609f338e4dSFabiano Rosas } 15619f338e4dSFabiano Rosas if (env->msr_mask & MSR_HVB) { 15629f338e4dSFabiano Rosas /* 15639f338e4dSFabiano Rosas * ISA specifies HV, but can be delivered to guest with HV 15649f338e4dSFabiano Rosas * clear (e.g., see FWNMI in PAPR, NMI injection in QEMU). 15659f338e4dSFabiano Rosas */ 15669f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 15679f338e4dSFabiano Rosas } else { 15688e54ad65SVíctor Colombo if (FIELD_EX64(env->msr, MSR, POW)) { 15699f338e4dSFabiano Rosas cpu_abort(cs, "Trying to deliver power-saving system reset " 15709f338e4dSFabiano Rosas "exception %d with no HV support\n", excp); 15719f338e4dSFabiano Rosas } 15729f338e4dSFabiano Rosas } 15739f338e4dSFabiano Rosas break; 157414895384SNicholas Piggin case POWERPC_EXCP_TRACE: /* Trace exception */ 157514895384SNicholas Piggin msr |= env->error_code; 157614895384SNicholas Piggin /* fall through */ 15779f338e4dSFabiano Rosas case POWERPC_EXCP_DSEG: /* Data segment exception */ 15789f338e4dSFabiano Rosas case POWERPC_EXCP_ISEG: /* Instruction segment exception */ 1579fd7abfabSNicholas Piggin case POWERPC_EXCP_SDOOR: /* Doorbell interrupt */ 1580c29b0704SNicholas Piggin case POWERPC_EXCP_PERFM: /* Performance monitor interrupt */ 15819f338e4dSFabiano Rosas break; 15829f338e4dSFabiano Rosas case POWERPC_EXCP_HISI: /* Hypervisor instruction storage exception */ 15839f338e4dSFabiano Rosas msr |= env->error_code; 15849f338e4dSFabiano Rosas /* fall through */ 15859f338e4dSFabiano Rosas case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */ 15869f338e4dSFabiano Rosas case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */ 15879f338e4dSFabiano Rosas case POWERPC_EXCP_SDOOR_HV: /* Hypervisor Doorbell interrupt */ 15889f338e4dSFabiano Rosas case POWERPC_EXCP_HVIRT: /* Hypervisor virtualization */ 15899f338e4dSFabiano Rosas srr0 = SPR_HSRR0; 15909f338e4dSFabiano Rosas srr1 = SPR_HSRR1; 15919f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 15929f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 15939f338e4dSFabiano Rosas break; 1594a3c020d8SNicholas Piggin #ifdef CONFIG_TCG 1595a3c020d8SNicholas Piggin case POWERPC_EXCP_HV_EMU: { 1596a3c020d8SNicholas Piggin uint32_t insn = ppc_ldl_code(env, env->nip); 1597a3c020d8SNicholas Piggin env->spr[SPR_HEIR] = insn; 1598a3c020d8SNicholas Piggin if (is_prefix_insn(env, insn)) { 1599a3c020d8SNicholas Piggin uint32_t insn2 = ppc_ldl_code(env, env->nip + 4); 1600a3c020d8SNicholas Piggin env->spr[SPR_HEIR] <<= 32; 1601a3c020d8SNicholas Piggin env->spr[SPR_HEIR] |= insn2; 1602a3c020d8SNicholas Piggin } 1603a3c020d8SNicholas Piggin srr0 = SPR_HSRR0; 1604a3c020d8SNicholas Piggin srr1 = SPR_HSRR1; 1605a3c020d8SNicholas Piggin new_msr |= (target_ulong)MSR_HVB; 1606a3c020d8SNicholas Piggin new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 1607a3c020d8SNicholas Piggin break; 1608a3c020d8SNicholas Piggin } 1609a3c020d8SNicholas Piggin #endif 16109f338e4dSFabiano Rosas case POWERPC_EXCP_VPU: /* Vector unavailable exception */ 16119f338e4dSFabiano Rosas case POWERPC_EXCP_VSXU: /* VSX unavailable exception */ 16129f338e4dSFabiano Rosas case POWERPC_EXCP_FU: /* Facility unavailable exception */ 16139f338e4dSFabiano Rosas env->spr[SPR_FSCR] |= ((target_ulong)env->error_code << 56); 16149f338e4dSFabiano Rosas break; 16159f338e4dSFabiano Rosas case POWERPC_EXCP_HV_FU: /* Hypervisor Facility Unavailable Exception */ 16169f338e4dSFabiano Rosas env->spr[SPR_HFSCR] |= ((target_ulong)env->error_code << FSCR_IC_POS); 16179f338e4dSFabiano Rosas srr0 = SPR_HSRR0; 16189f338e4dSFabiano Rosas srr1 = SPR_HSRR1; 16199f338e4dSFabiano Rosas new_msr |= (target_ulong)MSR_HVB; 16209f338e4dSFabiano Rosas new_msr |= env->msr & ((target_ulong)1 << MSR_RI); 16219f338e4dSFabiano Rosas break; 1622cb76bbc4SDaniel Henrique Barboza case POWERPC_EXCP_PERFM_EBB: /* Performance Monitor EBB Exception */ 1623cb76bbc4SDaniel Henrique Barboza case POWERPC_EXCP_EXTERNAL_EBB: /* External EBB Exception */ 1624cb76bbc4SDaniel Henrique Barboza env->spr[SPR_BESCR] &= ~BESCR_GE; 1625cb76bbc4SDaniel Henrique Barboza 1626cb76bbc4SDaniel Henrique Barboza /* 1627cb76bbc4SDaniel Henrique Barboza * Save NIP for rfebb insn in SPR_EBBRR. Next nip is 1628cb76bbc4SDaniel Henrique Barboza * stored in the EBB Handler SPR_EBBHR. 1629cb76bbc4SDaniel Henrique Barboza */ 1630cb76bbc4SDaniel Henrique Barboza env->spr[SPR_EBBRR] = env->nip; 1631cb76bbc4SDaniel Henrique Barboza powerpc_set_excp_state(cpu, env->spr[SPR_EBBHR], env->msr); 1632cb76bbc4SDaniel Henrique Barboza 1633cb76bbc4SDaniel Henrique Barboza /* 1634cb76bbc4SDaniel Henrique Barboza * This exception is handled in userspace. No need to proceed. 1635cb76bbc4SDaniel Henrique Barboza */ 1636cb76bbc4SDaniel Henrique Barboza return; 16379f338e4dSFabiano Rosas case POWERPC_EXCP_THERM: /* Thermal interrupt */ 16389f338e4dSFabiano Rosas case POWERPC_EXCP_VPUA: /* Vector assist exception */ 16399f338e4dSFabiano Rosas case POWERPC_EXCP_MAINT: /* Maintenance exception */ 164030c4e426SFabiano Rosas case POWERPC_EXCP_HV_MAINT: /* Hypervisor Maintenance exception */ 16419f338e4dSFabiano Rosas cpu_abort(cs, "%s exception not implemented\n", 16429f338e4dSFabiano Rosas powerpc_excp_name(excp)); 16439f338e4dSFabiano Rosas break; 16449f338e4dSFabiano Rosas default: 16459f338e4dSFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 16469f338e4dSFabiano Rosas break; 16479f338e4dSFabiano Rosas } 16489f338e4dSFabiano Rosas 16499f338e4dSFabiano Rosas /* 16509f338e4dSFabiano Rosas * Sort out endianness of interrupt, this differs depending on the 16519f338e4dSFabiano Rosas * CPU, the HV mode, etc... 16529f338e4dSFabiano Rosas */ 16539f338e4dSFabiano Rosas if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) { 16549f338e4dSFabiano Rosas new_msr |= (target_ulong)1 << MSR_LE; 16559f338e4dSFabiano Rosas } 16569f338e4dSFabiano Rosas 16579f338e4dSFabiano Rosas new_msr |= (target_ulong)1 << MSR_SF; 16589f338e4dSFabiano Rosas 16599f338e4dSFabiano Rosas if (excp != POWERPC_EXCP_SYSCALL_VECTORED) { 16609f338e4dSFabiano Rosas /* Save PC */ 16619f338e4dSFabiano Rosas env->spr[srr0] = env->nip; 16629f338e4dSFabiano Rosas 16639f338e4dSFabiano Rosas /* Save MSR */ 16649f338e4dSFabiano Rosas env->spr[srr1] = msr; 16659f338e4dSFabiano Rosas } 16669f338e4dSFabiano Rosas 16677cebc5dbSNicholas Piggin if ((new_msr & MSR_HVB) && books_vhyp_handles_hv_excp(cpu)) { 16687cebc5dbSNicholas Piggin PPCVirtualHypervisorClass *vhc = 16697cebc5dbSNicholas Piggin PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp); 16707cebc5dbSNicholas Piggin /* Deliver interrupt to L1 by returning from the H_ENTER_NESTED call */ 16717cebc5dbSNicholas Piggin vhc->deliver_hv_excp(cpu, excp); 16727cebc5dbSNicholas Piggin 16737cebc5dbSNicholas Piggin powerpc_reset_excp_state(cpu); 16747cebc5dbSNicholas Piggin 16757cebc5dbSNicholas Piggin } else { 16767cebc5dbSNicholas Piggin /* Sanity check */ 16777cebc5dbSNicholas Piggin if (!(env->msr_mask & MSR_HVB) && srr0 == SPR_HSRR0) { 16787cebc5dbSNicholas Piggin cpu_abort(cs, "Trying to deliver HV exception (HSRR) %d with " 16797cebc5dbSNicholas Piggin "no HV support\n", excp); 16807cebc5dbSNicholas Piggin } 16817cebc5dbSNicholas Piggin 16829f338e4dSFabiano Rosas /* This can update new_msr and vector if AIL applies */ 168310895ab6SFabiano Rosas ppc_excp_apply_ail(cpu, excp, msr, &new_msr, &vector); 16849f338e4dSFabiano Rosas 16859f338e4dSFabiano Rosas powerpc_set_excp_state(cpu, vector, new_msr); 16869f338e4dSFabiano Rosas } 16877cebc5dbSNicholas Piggin } 168830c4e426SFabiano Rosas #else 168930c4e426SFabiano Rosas static inline void powerpc_excp_books(PowerPCCPU *cpu, int excp) 169030c4e426SFabiano Rosas { 169130c4e426SFabiano Rosas g_assert_not_reached(); 169230c4e426SFabiano Rosas } 169330c4e426SFabiano Rosas #endif 16949f338e4dSFabiano Rosas 1695dc88dd0aSFabiano Rosas static void powerpc_excp(PowerPCCPU *cpu, int excp) 1696dc88dd0aSFabiano Rosas { 1697c6eaac89SFabiano Rosas CPUState *cs = CPU(cpu); 1698dc88dd0aSFabiano Rosas CPUPPCState *env = &cpu->env; 1699dc88dd0aSFabiano Rosas 1700c6eaac89SFabiano Rosas if (excp <= POWERPC_EXCP_NONE || excp >= POWERPC_EXCP_NB) { 1701c6eaac89SFabiano Rosas cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp); 1702c6eaac89SFabiano Rosas } 1703c6eaac89SFabiano Rosas 1704c6eaac89SFabiano Rosas qemu_log_mask(CPU_LOG_INT, "Raise exception at " TARGET_FMT_lx 1705c6eaac89SFabiano Rosas " => %s (%d) error=%02x\n", env->nip, powerpc_excp_name(excp), 1706c6eaac89SFabiano Rosas excp, env->error_code); 17078a15cceeSBALATON Zoltan env->excp_stats[excp]++; 1708c6eaac89SFabiano Rosas 1709dc88dd0aSFabiano Rosas switch (env->excp_model) { 1710e808c2edSFabiano Rosas case POWERPC_EXCP_40x: 1711e808c2edSFabiano Rosas powerpc_excp_40x(cpu, excp); 1712e808c2edSFabiano Rosas break; 171358d178fbSFabiano Rosas case POWERPC_EXCP_6xx: 171458d178fbSFabiano Rosas powerpc_excp_6xx(cpu, excp); 171558d178fbSFabiano Rosas break; 1716ccfca2fcSFabiano Rosas case POWERPC_EXCP_7xx: 1717ccfca2fcSFabiano Rosas powerpc_excp_7xx(cpu, excp); 1718ccfca2fcSFabiano Rosas break; 171952926b0dSFabiano Rosas case POWERPC_EXCP_74xx: 172052926b0dSFabiano Rosas powerpc_excp_74xx(cpu, excp); 172152926b0dSFabiano Rosas break; 1722180952ceSFabiano Rosas case POWERPC_EXCP_BOOKE: 1723180952ceSFabiano Rosas powerpc_excp_booke(cpu, excp); 1724180952ceSFabiano Rosas break; 17259f338e4dSFabiano Rosas case POWERPC_EXCP_970: 17269f338e4dSFabiano Rosas case POWERPC_EXCP_POWER7: 17279f338e4dSFabiano Rosas case POWERPC_EXCP_POWER8: 17289f338e4dSFabiano Rosas case POWERPC_EXCP_POWER9: 17299f338e4dSFabiano Rosas case POWERPC_EXCP_POWER10: 17309f338e4dSFabiano Rosas powerpc_excp_books(cpu, excp); 17319f338e4dSFabiano Rosas break; 1732dc88dd0aSFabiano Rosas default: 173328091374SFabiano Rosas g_assert_not_reached(); 1734dc88dd0aSFabiano Rosas } 1735dc88dd0aSFabiano Rosas } 1736dc88dd0aSFabiano Rosas 173797a8ea5aSAndreas Färber void ppc_cpu_do_interrupt(CPUState *cs) 1738c79c73f6SBlue Swirl { 173997a8ea5aSAndreas Färber PowerPCCPU *cpu = POWERPC_CPU(cs); 17405c26a5b3SAndreas Färber 174193130c84SFabiano Rosas powerpc_excp(cpu, cs->exception_index); 1742c79c73f6SBlue Swirl } 1743c79c73f6SBlue Swirl 17442dfecf01SMatheus Ferst #if defined(TARGET_PPC64) 1745c8e1de2eSMatheus Ferst #define P7_UNUSED_INTERRUPTS \ 1746c8e1de2eSMatheus Ferst (PPC_INTERRUPT_RESET | PPC_INTERRUPT_HVIRT | PPC_INTERRUPT_CEXT | \ 1747c8e1de2eSMatheus Ferst PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT | \ 1748c8e1de2eSMatheus Ferst PPC_INTERRUPT_PIT | PPC_INTERRUPT_DOORBELL | PPC_INTERRUPT_HDOORBELL | \ 1749c8e1de2eSMatheus Ferst PPC_INTERRUPT_THERM | PPC_INTERRUPT_EBB) 1750c8e1de2eSMatheus Ferst 17519c713713SMatheus Ferst static int p7_interrupt_powersave(CPUPPCState *env) 17529c713713SMatheus Ferst { 17539c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_EXT) && 17549c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P7_PECE0)) { 17559c713713SMatheus Ferst return PPC_INTERRUPT_EXT; 17569c713713SMatheus Ferst } 17579c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DECR) && 17589c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P7_PECE1)) { 17599c713713SMatheus Ferst return PPC_INTERRUPT_DECR; 17609c713713SMatheus Ferst } 17619c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_MCK) && 17629c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) { 17639c713713SMatheus Ferst return PPC_INTERRUPT_MCK; 17649c713713SMatheus Ferst } 17659c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HMI) && 17669c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) { 17679c713713SMatheus Ferst return PPC_INTERRUPT_HMI; 17689c713713SMatheus Ferst } 17699c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_RESET) { 17709c713713SMatheus Ferst return PPC_INTERRUPT_RESET; 17719c713713SMatheus Ferst } 17729c713713SMatheus Ferst return 0; 17739c713713SMatheus Ferst } 17749c713713SMatheus Ferst 1775bf303fb3SMatheus Ferst static int p7_next_unmasked_interrupt(CPUPPCState *env) 1776bf303fb3SMatheus Ferst { 17774e6b7db2SBALATON Zoltan CPUState *cs = env_cpu(env); 17784e6b7db2SBALATON Zoltan 1779022b7128SMatheus Ferst /* Ignore MSR[EE] when coming out of some power management states */ 1780022b7128SMatheus Ferst bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset; 1781bf303fb3SMatheus Ferst 1782c8e1de2eSMatheus Ferst assert((env->pending_interrupts & P7_UNUSED_INTERRUPTS) == 0); 1783c8e1de2eSMatheus Ferst 1784022b7128SMatheus Ferst if (cs->halted) { 1785022b7128SMatheus Ferst /* LPCR[PECE] controls which interrupts can exit power-saving mode */ 1786022b7128SMatheus Ferst return p7_interrupt_powersave(env); 1787022b7128SMatheus Ferst } 1788022b7128SMatheus Ferst 1789bf303fb3SMatheus Ferst /* Machine check exception */ 1790bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 1791bf303fb3SMatheus Ferst return PPC_INTERRUPT_MCK; 1792bf303fb3SMatheus Ferst } 1793bf303fb3SMatheus Ferst 1794bf303fb3SMatheus Ferst /* Hypervisor decrementer exception */ 1795bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDECR) { 1796bf303fb3SMatheus Ferst /* LPCR will be clear when not supported so this will work */ 1797bf303fb3SMatheus Ferst bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE); 1798022b7128SMatheus Ferst if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) { 1799bf303fb3SMatheus Ferst /* HDEC clears on delivery */ 1800bf303fb3SMatheus Ferst return PPC_INTERRUPT_HDECR; 1801bf303fb3SMatheus Ferst } 1802bf303fb3SMatheus Ferst } 1803bf303fb3SMatheus Ferst 1804bf303fb3SMatheus Ferst /* External interrupt can ignore MSR:EE under some circumstances */ 1805bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EXT) { 1806bf303fb3SMatheus Ferst bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 1807bf303fb3SMatheus Ferst bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 1808bf303fb3SMatheus Ferst /* HEIC blocks delivery to the hypervisor */ 1809022b7128SMatheus Ferst if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) && 1810bf303fb3SMatheus Ferst !FIELD_EX64(env->msr, MSR, PR))) || 1811bf303fb3SMatheus Ferst (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) { 1812bf303fb3SMatheus Ferst return PPC_INTERRUPT_EXT; 1813bf303fb3SMatheus Ferst } 1814bf303fb3SMatheus Ferst } 1815022b7128SMatheus Ferst if (msr_ee != 0) { 1816bf303fb3SMatheus Ferst /* Decrementer exception */ 1817bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DECR) { 1818bf303fb3SMatheus Ferst return PPC_INTERRUPT_DECR; 1819bf303fb3SMatheus Ferst } 1820bf303fb3SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PERFM) { 1821bf303fb3SMatheus Ferst return PPC_INTERRUPT_PERFM; 1822bf303fb3SMatheus Ferst } 1823bf303fb3SMatheus Ferst } 1824bf303fb3SMatheus Ferst 1825bf303fb3SMatheus Ferst return 0; 1826bf303fb3SMatheus Ferst } 1827bf303fb3SMatheus Ferst 1828f6194fddSMatheus Ferst #define P8_UNUSED_INTERRUPTS \ 1829f6194fddSMatheus Ferst (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_HVIRT | \ 1830f6194fddSMatheus Ferst PPC_INTERRUPT_CEXT | PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | \ 1831f6194fddSMatheus Ferst PPC_INTERRUPT_FIT | PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM) 1832f6194fddSMatheus Ferst 18339c713713SMatheus Ferst static int p8_interrupt_powersave(CPUPPCState *env) 18349c713713SMatheus Ferst { 18359c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_EXT) && 18369c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE2)) { 18379c713713SMatheus Ferst return PPC_INTERRUPT_EXT; 18389c713713SMatheus Ferst } 18399c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DECR) && 18409c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE3)) { 18419c713713SMatheus Ferst return PPC_INTERRUPT_DECR; 18429c713713SMatheus Ferst } 18439c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_MCK) && 18449c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) { 18459c713713SMatheus Ferst return PPC_INTERRUPT_MCK; 18469c713713SMatheus Ferst } 18479c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HMI) && 18489c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) { 18499c713713SMatheus Ferst return PPC_INTERRUPT_HMI; 18509c713713SMatheus Ferst } 18519c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) && 18529c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE0)) { 18539c713713SMatheus Ferst return PPC_INTERRUPT_DOORBELL; 18549c713713SMatheus Ferst } 18559c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) && 18569c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_P8_PECE1)) { 18579c713713SMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 18589c713713SMatheus Ferst } 18599c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_RESET) { 18609c713713SMatheus Ferst return PPC_INTERRUPT_RESET; 18619c713713SMatheus Ferst } 18629c713713SMatheus Ferst return 0; 18639c713713SMatheus Ferst } 18649c713713SMatheus Ferst 1865a9899d42SMatheus Ferst static int p8_next_unmasked_interrupt(CPUPPCState *env) 1866a9899d42SMatheus Ferst { 18674e6b7db2SBALATON Zoltan CPUState *cs = env_cpu(env); 18684e6b7db2SBALATON Zoltan 186964a9b5eeSMatheus Ferst /* Ignore MSR[EE] when coming out of some power management states */ 187064a9b5eeSMatheus Ferst bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset; 1871a9899d42SMatheus Ferst 1872f6194fddSMatheus Ferst assert((env->pending_interrupts & P8_UNUSED_INTERRUPTS) == 0); 1873f6194fddSMatheus Ferst 187464a9b5eeSMatheus Ferst if (cs->halted) { 187564a9b5eeSMatheus Ferst /* LPCR[PECE] controls which interrupts can exit power-saving mode */ 187664a9b5eeSMatheus Ferst return p8_interrupt_powersave(env); 187764a9b5eeSMatheus Ferst } 187864a9b5eeSMatheus Ferst 1879a9899d42SMatheus Ferst /* Machine check exception */ 1880a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 1881a9899d42SMatheus Ferst return PPC_INTERRUPT_MCK; 1882a9899d42SMatheus Ferst } 1883a9899d42SMatheus Ferst 1884a9899d42SMatheus Ferst /* Hypervisor decrementer exception */ 1885a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDECR) { 1886a9899d42SMatheus Ferst /* LPCR will be clear when not supported so this will work */ 1887a9899d42SMatheus Ferst bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE); 188864a9b5eeSMatheus Ferst if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) { 1889a9899d42SMatheus Ferst /* HDEC clears on delivery */ 1890a9899d42SMatheus Ferst return PPC_INTERRUPT_HDECR; 1891a9899d42SMatheus Ferst } 1892a9899d42SMatheus Ferst } 1893a9899d42SMatheus Ferst 1894a9899d42SMatheus Ferst /* External interrupt can ignore MSR:EE under some circumstances */ 1895a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EXT) { 1896a9899d42SMatheus Ferst bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 1897a9899d42SMatheus Ferst bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 1898a9899d42SMatheus Ferst /* HEIC blocks delivery to the hypervisor */ 189964a9b5eeSMatheus Ferst if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) && 1900a9899d42SMatheus Ferst !FIELD_EX64(env->msr, MSR, PR))) || 1901a9899d42SMatheus Ferst (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) { 1902a9899d42SMatheus Ferst return PPC_INTERRUPT_EXT; 1903a9899d42SMatheus Ferst } 1904a9899d42SMatheus Ferst } 190564a9b5eeSMatheus Ferst if (msr_ee != 0) { 1906a9899d42SMatheus Ferst /* Decrementer exception */ 1907a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DECR) { 1908a9899d42SMatheus Ferst return PPC_INTERRUPT_DECR; 1909a9899d42SMatheus Ferst } 1910a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 1911a9899d42SMatheus Ferst return PPC_INTERRUPT_DOORBELL; 1912a9899d42SMatheus Ferst } 1913a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) { 1914a9899d42SMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 1915a9899d42SMatheus Ferst } 1916a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PERFM) { 1917a9899d42SMatheus Ferst return PPC_INTERRUPT_PERFM; 1918a9899d42SMatheus Ferst } 1919a9899d42SMatheus Ferst /* EBB exception */ 1920a9899d42SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EBB) { 1921a9899d42SMatheus Ferst /* 1922a9899d42SMatheus Ferst * EBB exception must be taken in problem state and 1923a9899d42SMatheus Ferst * with BESCR_GE set. 1924a9899d42SMatheus Ferst */ 1925a9899d42SMatheus Ferst if (FIELD_EX64(env->msr, MSR, PR) && 1926a9899d42SMatheus Ferst (env->spr[SPR_BESCR] & BESCR_GE)) { 1927a9899d42SMatheus Ferst return PPC_INTERRUPT_EBB; 1928a9899d42SMatheus Ferst } 1929a9899d42SMatheus Ferst } 1930a9899d42SMatheus Ferst } 1931a9899d42SMatheus Ferst 1932a9899d42SMatheus Ferst return 0; 1933a9899d42SMatheus Ferst } 1934a9899d42SMatheus Ferst 1935b00e9a2fSMatheus Ferst #define P9_UNUSED_INTERRUPTS \ 1936b00e9a2fSMatheus Ferst (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT | \ 1937b00e9a2fSMatheus Ferst PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT | \ 1938b00e9a2fSMatheus Ferst PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM) 1939b00e9a2fSMatheus Ferst 19409c713713SMatheus Ferst static int p9_interrupt_powersave(CPUPPCState *env) 19419c713713SMatheus Ferst { 19429c713713SMatheus Ferst /* External Exception */ 19439c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_EXT) && 19449c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_EEE)) { 19459c713713SMatheus Ferst bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 19469c713713SMatheus Ferst if (!heic || !FIELD_EX64_HV(env->msr) || 19479c713713SMatheus Ferst FIELD_EX64(env->msr, MSR, PR)) { 19489c713713SMatheus Ferst return PPC_INTERRUPT_EXT; 19499c713713SMatheus Ferst } 19509c713713SMatheus Ferst } 19519c713713SMatheus Ferst /* Decrementer Exception */ 19529c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DECR) && 19539c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_DEE)) { 19549c713713SMatheus Ferst return PPC_INTERRUPT_DECR; 19559c713713SMatheus Ferst } 19569c713713SMatheus Ferst /* Machine Check or Hypervisor Maintenance Exception */ 19579c713713SMatheus Ferst if (env->spr[SPR_LPCR] & LPCR_OEE) { 19589c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 19599c713713SMatheus Ferst return PPC_INTERRUPT_MCK; 19609c713713SMatheus Ferst } 19619c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HMI) { 19629c713713SMatheus Ferst return PPC_INTERRUPT_HMI; 19639c713713SMatheus Ferst } 19649c713713SMatheus Ferst } 19659c713713SMatheus Ferst /* Privileged Doorbell Exception */ 19669c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) && 19679c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_PDEE)) { 19689c713713SMatheus Ferst return PPC_INTERRUPT_DOORBELL; 19699c713713SMatheus Ferst } 19709c713713SMatheus Ferst /* Hypervisor Doorbell Exception */ 19719c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) && 19729c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_HDEE)) { 19739c713713SMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 19749c713713SMatheus Ferst } 19759c713713SMatheus Ferst /* Hypervisor virtualization exception */ 19769c713713SMatheus Ferst if ((env->pending_interrupts & PPC_INTERRUPT_HVIRT) && 19779c713713SMatheus Ferst (env->spr[SPR_LPCR] & LPCR_HVEE)) { 19789c713713SMatheus Ferst return PPC_INTERRUPT_HVIRT; 19799c713713SMatheus Ferst } 19809c713713SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_RESET) { 19819c713713SMatheus Ferst return PPC_INTERRUPT_RESET; 19829c713713SMatheus Ferst } 19839c713713SMatheus Ferst return 0; 19849c713713SMatheus Ferst } 19859c713713SMatheus Ferst 19862dfecf01SMatheus Ferst static int p9_next_unmasked_interrupt(CPUPPCState *env) 19872dfecf01SMatheus Ferst { 19884e6b7db2SBALATON Zoltan CPUState *cs = env_cpu(env); 19894e6b7db2SBALATON Zoltan 199027796411SMatheus Ferst /* Ignore MSR[EE] when coming out of some power management states */ 199127796411SMatheus Ferst bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset; 19922dfecf01SMatheus Ferst 1993b00e9a2fSMatheus Ferst assert((env->pending_interrupts & P9_UNUSED_INTERRUPTS) == 0); 1994b00e9a2fSMatheus Ferst 199527796411SMatheus Ferst if (cs->halted) { 199627796411SMatheus Ferst if (env->spr[SPR_PSSCR] & PSSCR_EC) { 199727796411SMatheus Ferst /* 199827796411SMatheus Ferst * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can 199927796411SMatheus Ferst * wakeup the processor 200027796411SMatheus Ferst */ 200127796411SMatheus Ferst return p9_interrupt_powersave(env); 200227796411SMatheus Ferst } else { 200327796411SMatheus Ferst /* 200427796411SMatheus Ferst * When it's clear, any system-caused exception exits power-saving 200527796411SMatheus Ferst * mode, even the ones that gate on MSR[EE]. 200627796411SMatheus Ferst */ 200727796411SMatheus Ferst msr_ee = true; 200827796411SMatheus Ferst } 200927796411SMatheus Ferst } 201027796411SMatheus Ferst 20112dfecf01SMatheus Ferst /* Machine check exception */ 20122dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 20132dfecf01SMatheus Ferst return PPC_INTERRUPT_MCK; 20142dfecf01SMatheus Ferst } 20152dfecf01SMatheus Ferst 20162dfecf01SMatheus Ferst /* Hypervisor decrementer exception */ 20172dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDECR) { 20182dfecf01SMatheus Ferst /* LPCR will be clear when not supported so this will work */ 20192dfecf01SMatheus Ferst bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE); 202027796411SMatheus Ferst if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) { 20212dfecf01SMatheus Ferst /* HDEC clears on delivery */ 20222dfecf01SMatheus Ferst return PPC_INTERRUPT_HDECR; 20232dfecf01SMatheus Ferst } 20242dfecf01SMatheus Ferst } 20252dfecf01SMatheus Ferst 20262dfecf01SMatheus Ferst /* Hypervisor virtualization interrupt */ 20272dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) { 20282dfecf01SMatheus Ferst /* LPCR will be clear when not supported so this will work */ 20292dfecf01SMatheus Ferst bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE); 203027796411SMatheus Ferst if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hvice) { 20312dfecf01SMatheus Ferst return PPC_INTERRUPT_HVIRT; 20322dfecf01SMatheus Ferst } 20332dfecf01SMatheus Ferst } 20342dfecf01SMatheus Ferst 20352dfecf01SMatheus Ferst /* External interrupt can ignore MSR:EE under some circumstances */ 20362dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EXT) { 20372dfecf01SMatheus Ferst bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 20382dfecf01SMatheus Ferst bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 20392dfecf01SMatheus Ferst /* HEIC blocks delivery to the hypervisor */ 204027796411SMatheus Ferst if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) && 20412dfecf01SMatheus Ferst !FIELD_EX64(env->msr, MSR, PR))) || 20422dfecf01SMatheus Ferst (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) { 20432dfecf01SMatheus Ferst return PPC_INTERRUPT_EXT; 20442dfecf01SMatheus Ferst } 20452dfecf01SMatheus Ferst } 204627796411SMatheus Ferst if (msr_ee != 0) { 20472dfecf01SMatheus Ferst /* Decrementer exception */ 20482dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DECR) { 20492dfecf01SMatheus Ferst return PPC_INTERRUPT_DECR; 20502dfecf01SMatheus Ferst } 20512dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 20522dfecf01SMatheus Ferst return PPC_INTERRUPT_DOORBELL; 20532dfecf01SMatheus Ferst } 20542dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) { 20552dfecf01SMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 20562dfecf01SMatheus Ferst } 20572dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PERFM) { 20582dfecf01SMatheus Ferst return PPC_INTERRUPT_PERFM; 20592dfecf01SMatheus Ferst } 20602dfecf01SMatheus Ferst /* EBB exception */ 20612dfecf01SMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EBB) { 20622dfecf01SMatheus Ferst /* 20632dfecf01SMatheus Ferst * EBB exception must be taken in problem state and 20642dfecf01SMatheus Ferst * with BESCR_GE set. 20652dfecf01SMatheus Ferst */ 20662dfecf01SMatheus Ferst if (FIELD_EX64(env->msr, MSR, PR) && 20672dfecf01SMatheus Ferst (env->spr[SPR_BESCR] & BESCR_GE)) { 20682dfecf01SMatheus Ferst return PPC_INTERRUPT_EBB; 20692dfecf01SMatheus Ferst } 20702dfecf01SMatheus Ferst } 20712dfecf01SMatheus Ferst } 20722dfecf01SMatheus Ferst 20732dfecf01SMatheus Ferst return 0; 20742dfecf01SMatheus Ferst } 20752dfecf01SMatheus Ferst #endif 20762dfecf01SMatheus Ferst 2077ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt_generic(CPUPPCState *env) 2078c79c73f6SBlue Swirl { 20793621e2c9SBenjamin Herrenschmidt bool async_deliver; 2080259186a7SAndreas Färber 2081c79c73f6SBlue Swirl /* External reset */ 2082f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_RESET) { 2083de76b85cSMatheus Ferst return PPC_INTERRUPT_RESET; 2084c79c73f6SBlue Swirl } 2085c79c73f6SBlue Swirl /* Machine check exception */ 2086f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_MCK) { 2087de76b85cSMatheus Ferst return PPC_INTERRUPT_MCK; 2088c79c73f6SBlue Swirl } 2089c79c73f6SBlue Swirl #if 0 /* TODO */ 2090c79c73f6SBlue Swirl /* External debug exception */ 2091f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) { 2092de76b85cSMatheus Ferst return PPC_INTERRUPT_DEBUG; 2093c79c73f6SBlue Swirl } 2094c79c73f6SBlue Swirl #endif 20953621e2c9SBenjamin Herrenschmidt 20963621e2c9SBenjamin Herrenschmidt /* 20973621e2c9SBenjamin Herrenschmidt * For interrupts that gate on MSR:EE, we need to do something a 20983621e2c9SBenjamin Herrenschmidt * bit more subtle, as we need to let them through even when EE is 20993621e2c9SBenjamin Herrenschmidt * clear when coming out of some power management states (in order 21003621e2c9SBenjamin Herrenschmidt * for them to become a 0x100). 21013621e2c9SBenjamin Herrenschmidt */ 21020939b8f8SVíctor Colombo async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset; 21033621e2c9SBenjamin Herrenschmidt 2104c79c73f6SBlue Swirl /* Hypervisor decrementer exception */ 2105f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDECR) { 21064b236b62SBenjamin Herrenschmidt /* LPCR will be clear when not supported so this will work */ 21074b236b62SBenjamin Herrenschmidt bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE); 21089de754d3SVíctor Colombo if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) { 21094b236b62SBenjamin Herrenschmidt /* HDEC clears on delivery */ 2110de76b85cSMatheus Ferst return PPC_INTERRUPT_HDECR; 2111c79c73f6SBlue Swirl } 2112c79c73f6SBlue Swirl } 2113d8ce5fd6SBenjamin Herrenschmidt 2114d8ce5fd6SBenjamin Herrenschmidt /* Hypervisor virtualization interrupt */ 2115f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) { 2116d8ce5fd6SBenjamin Herrenschmidt /* LPCR will be clear when not supported so this will work */ 2117d8ce5fd6SBenjamin Herrenschmidt bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE); 21189de754d3SVíctor Colombo if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) { 2119de76b85cSMatheus Ferst return PPC_INTERRUPT_HVIRT; 2120d8ce5fd6SBenjamin Herrenschmidt } 2121d8ce5fd6SBenjamin Herrenschmidt } 2122d8ce5fd6SBenjamin Herrenschmidt 2123d8ce5fd6SBenjamin Herrenschmidt /* External interrupt can ignore MSR:EE under some circumstances */ 2124f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EXT) { 2125d1dbe37cSBenjamin Herrenschmidt bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0); 21266eebe6dcSBenjamin Herrenschmidt bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC); 21276eebe6dcSBenjamin Herrenschmidt /* HEIC blocks delivery to the hypervisor */ 21289de754d3SVíctor Colombo if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) && 2129d41ccf6eSVíctor Colombo !FIELD_EX64(env->msr, MSR, PR))) || 21309de754d3SVíctor Colombo (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) { 2131de76b85cSMatheus Ferst return PPC_INTERRUPT_EXT; 2132d1dbe37cSBenjamin Herrenschmidt } 2133d1dbe37cSBenjamin Herrenschmidt } 2134acc861c2SVíctor Colombo if (FIELD_EX64(env->msr, MSR, CE)) { 2135c79c73f6SBlue Swirl /* External critical interrupt */ 2136f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_CEXT) { 2137de76b85cSMatheus Ferst return PPC_INTERRUPT_CEXT; 2138c79c73f6SBlue Swirl } 2139c79c73f6SBlue Swirl } 21403621e2c9SBenjamin Herrenschmidt if (async_deliver != 0) { 2141c79c73f6SBlue Swirl /* Watchdog timer on embedded PowerPC */ 2142f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_WDT) { 2143de76b85cSMatheus Ferst return PPC_INTERRUPT_WDT; 2144c79c73f6SBlue Swirl } 2145f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) { 2146de76b85cSMatheus Ferst return PPC_INTERRUPT_CDOORBELL; 2147c79c73f6SBlue Swirl } 2148c79c73f6SBlue Swirl /* Fixed interval timer on embedded PowerPC */ 2149f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_FIT) { 2150de76b85cSMatheus Ferst return PPC_INTERRUPT_FIT; 2151c79c73f6SBlue Swirl } 2152c79c73f6SBlue Swirl /* Programmable interval timer on embedded PowerPC */ 2153f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PIT) { 2154de76b85cSMatheus Ferst return PPC_INTERRUPT_PIT; 2155c79c73f6SBlue Swirl } 2156c79c73f6SBlue Swirl /* Decrementer exception */ 2157f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DECR) { 2158de76b85cSMatheus Ferst return PPC_INTERRUPT_DECR; 2159c79c73f6SBlue Swirl } 2160f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) { 2161de76b85cSMatheus Ferst return PPC_INTERRUPT_DOORBELL; 2162c79c73f6SBlue Swirl } 2163f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) { 2164de76b85cSMatheus Ferst return PPC_INTERRUPT_HDOORBELL; 21657af1e7b0SCédric Le Goater } 2166f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_PERFM) { 2167de76b85cSMatheus Ferst return PPC_INTERRUPT_PERFM; 2168c79c73f6SBlue Swirl } 2169c79c73f6SBlue Swirl /* Thermal interrupt */ 2170f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_THERM) { 2171de76b85cSMatheus Ferst return PPC_INTERRUPT_THERM; 2172c79c73f6SBlue Swirl } 2173cb76bbc4SDaniel Henrique Barboza /* EBB exception */ 2174f003109fSMatheus Ferst if (env->pending_interrupts & PPC_INTERRUPT_EBB) { 2175cb76bbc4SDaniel Henrique Barboza /* 2176cb76bbc4SDaniel Henrique Barboza * EBB exception must be taken in problem state and 2177cb76bbc4SDaniel Henrique Barboza * with BESCR_GE set. 2178cb76bbc4SDaniel Henrique Barboza */ 2179d41ccf6eSVíctor Colombo if (FIELD_EX64(env->msr, MSR, PR) && 2180d41ccf6eSVíctor Colombo (env->spr[SPR_BESCR] & BESCR_GE)) { 2181de76b85cSMatheus Ferst return PPC_INTERRUPT_EBB; 2182de76b85cSMatheus Ferst } 2183de76b85cSMatheus Ferst } 2184de76b85cSMatheus Ferst } 2185cb76bbc4SDaniel Henrique Barboza 2186de76b85cSMatheus Ferst return 0; 2187de76b85cSMatheus Ferst } 2188de76b85cSMatheus Ferst 2189ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt(CPUPPCState *env) 2190ba2898f7SMatheus Ferst { 2191ba2898f7SMatheus Ferst switch (env->excp_model) { 21922dfecf01SMatheus Ferst #if defined(TARGET_PPC64) 2193bf303fb3SMatheus Ferst case POWERPC_EXCP_POWER7: 2194bf303fb3SMatheus Ferst return p7_next_unmasked_interrupt(env); 2195a9899d42SMatheus Ferst case POWERPC_EXCP_POWER8: 2196a9899d42SMatheus Ferst return p8_next_unmasked_interrupt(env); 21972dfecf01SMatheus Ferst case POWERPC_EXCP_POWER9: 21982dfecf01SMatheus Ferst case POWERPC_EXCP_POWER10: 21992dfecf01SMatheus Ferst return p9_next_unmasked_interrupt(env); 22002dfecf01SMatheus Ferst #endif 2201ba2898f7SMatheus Ferst default: 2202ba2898f7SMatheus Ferst return ppc_next_unmasked_interrupt_generic(env); 2203ba2898f7SMatheus Ferst } 2204ba2898f7SMatheus Ferst } 2205ba2898f7SMatheus Ferst 22062fdedcbcSMatheus Ferst /* 22072fdedcbcSMatheus Ferst * Sets CPU_INTERRUPT_HARD if there is at least one unmasked interrupt to be 22082fdedcbcSMatheus Ferst * delivered and clears CPU_INTERRUPT_HARD otherwise. 22092fdedcbcSMatheus Ferst * 22102fdedcbcSMatheus Ferst * This method is called by ppc_set_interrupt when an interrupt is raised or 22112fdedcbcSMatheus Ferst * lowered, and should also be called whenever an interrupt masking condition 22122fdedcbcSMatheus Ferst * is changed, e.g.: 22132fdedcbcSMatheus Ferst * - When relevant bits of MSR are altered, like EE, HV, PR, etc.; 22142fdedcbcSMatheus Ferst * - When relevant bits of LPCR are altered, like PECE, HDICE, HVICE, etc.; 22152fdedcbcSMatheus Ferst * - When PSSCR[EC] or env->resume_as_sreset are changed; 22162fdedcbcSMatheus Ferst * - When cs->halted is changed and the CPU has a different interrupt masking 22172fdedcbcSMatheus Ferst * logic in power-saving mode (e.g., POWER7/8/9/10); 22182fdedcbcSMatheus Ferst */ 22192fdedcbcSMatheus Ferst void ppc_maybe_interrupt(CPUPPCState *env) 22202fdedcbcSMatheus Ferst { 22212fdedcbcSMatheus Ferst CPUState *cs = env_cpu(env); 22222fc4f9f3SRichard Henderson QEMU_IOTHREAD_LOCK_GUARD(); 22232fdedcbcSMatheus Ferst 22242fdedcbcSMatheus Ferst if (ppc_next_unmasked_interrupt(env)) { 22252fdedcbcSMatheus Ferst cpu_interrupt(cs, CPU_INTERRUPT_HARD); 22262fdedcbcSMatheus Ferst } else { 22272fdedcbcSMatheus Ferst cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); 22282fdedcbcSMatheus Ferst } 22292fdedcbcSMatheus Ferst } 22302fdedcbcSMatheus Ferst 22313654e238SMatheus Ferst #if defined(TARGET_PPC64) 2232d93a4856SMatheus Ferst static void p7_deliver_interrupt(CPUPPCState *env, int interrupt) 2233d93a4856SMatheus Ferst { 2234d93a4856SMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 2235d93a4856SMatheus Ferst CPUState *cs = env_cpu(env); 2236d93a4856SMatheus Ferst 2237d93a4856SMatheus Ferst switch (interrupt) { 2238d93a4856SMatheus Ferst case PPC_INTERRUPT_MCK: /* Machine check exception */ 2239d93a4856SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_MCK; 2240d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_MCHECK); 2241d93a4856SMatheus Ferst break; 2242d93a4856SMatheus Ferst 2243d93a4856SMatheus Ferst case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */ 2244d93a4856SMatheus Ferst /* HDEC clears on delivery */ 2245d93a4856SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDECR; 2246d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HDECR); 2247d93a4856SMatheus Ferst break; 2248d93a4856SMatheus Ferst 2249d93a4856SMatheus Ferst case PPC_INTERRUPT_EXT: 2250d93a4856SMatheus Ferst if (books_vhyp_promotes_external_to_hvirt(cpu)) { 2251d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 2252d93a4856SMatheus Ferst } else { 2253d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL); 2254d93a4856SMatheus Ferst } 2255d93a4856SMatheus Ferst break; 2256d93a4856SMatheus Ferst 2257d93a4856SMatheus Ferst case PPC_INTERRUPT_DECR: /* Decrementer exception */ 2258d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DECR); 2259d93a4856SMatheus Ferst break; 2260d93a4856SMatheus Ferst case PPC_INTERRUPT_PERFM: 2261d93a4856SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PERFM; 2262d93a4856SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM); 2263d93a4856SMatheus Ferst break; 2264d93a4856SMatheus Ferst case 0: 2265d93a4856SMatheus Ferst /* 2266d93a4856SMatheus Ferst * This is a bug ! It means that has_work took us out of halt without 2267d93a4856SMatheus Ferst * anything to deliver while in a PM state that requires getting 2268d93a4856SMatheus Ferst * out via a 0x100 2269d93a4856SMatheus Ferst * 2270d93a4856SMatheus Ferst * This means we will incorrectly execute past the power management 2271d93a4856SMatheus Ferst * instruction instead of triggering a reset. 2272d93a4856SMatheus Ferst * 2273d93a4856SMatheus Ferst * It generally means a discrepancy between the wakeup conditions in the 2274d93a4856SMatheus Ferst * processor has_work implementation and the logic in this function. 2275d93a4856SMatheus Ferst */ 2276d93a4856SMatheus Ferst assert(!env->resume_as_sreset); 2277d93a4856SMatheus Ferst break; 2278d93a4856SMatheus Ferst default: 2279d93a4856SMatheus Ferst cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt); 2280d93a4856SMatheus Ferst } 2281d93a4856SMatheus Ferst } 2282d93a4856SMatheus Ferst 22836527e757SMatheus Ferst static void p8_deliver_interrupt(CPUPPCState *env, int interrupt) 22846527e757SMatheus Ferst { 22856527e757SMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 22866527e757SMatheus Ferst CPUState *cs = env_cpu(env); 22876527e757SMatheus Ferst 22886527e757SMatheus Ferst switch (interrupt) { 22896527e757SMatheus Ferst case PPC_INTERRUPT_MCK: /* Machine check exception */ 22906527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_MCK; 22916527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_MCHECK); 22926527e757SMatheus Ferst break; 22936527e757SMatheus Ferst 22946527e757SMatheus Ferst case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */ 22956527e757SMatheus Ferst /* HDEC clears on delivery */ 22966527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDECR; 22976527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HDECR); 22986527e757SMatheus Ferst break; 22996527e757SMatheus Ferst 23006527e757SMatheus Ferst case PPC_INTERRUPT_EXT: 23016527e757SMatheus Ferst if (books_vhyp_promotes_external_to_hvirt(cpu)) { 23026527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 23036527e757SMatheus Ferst } else { 23046527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL); 23056527e757SMatheus Ferst } 23066527e757SMatheus Ferst break; 23076527e757SMatheus Ferst 23086527e757SMatheus Ferst case PPC_INTERRUPT_DECR: /* Decrementer exception */ 23096527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DECR); 23106527e757SMatheus Ferst break; 23116527e757SMatheus Ferst case PPC_INTERRUPT_DOORBELL: 23126527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL; 23136527e757SMatheus Ferst if (is_book3s_arch2x(env)) { 23146527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR); 23156527e757SMatheus Ferst } else { 23166527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DOORI); 23176527e757SMatheus Ferst } 23186527e757SMatheus Ferst break; 23196527e757SMatheus Ferst case PPC_INTERRUPT_HDOORBELL: 23206527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL; 23216527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV); 23226527e757SMatheus Ferst break; 23236527e757SMatheus Ferst case PPC_INTERRUPT_PERFM: 23246527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PERFM; 23256527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM); 23266527e757SMatheus Ferst break; 23276527e757SMatheus Ferst case PPC_INTERRUPT_EBB: /* EBB exception */ 23286527e757SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_EBB; 23296527e757SMatheus Ferst if (env->spr[SPR_BESCR] & BESCR_PMEO) { 23306527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB); 23316527e757SMatheus Ferst } else if (env->spr[SPR_BESCR] & BESCR_EEO) { 23326527e757SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB); 23336527e757SMatheus Ferst } 23346527e757SMatheus Ferst break; 23356527e757SMatheus Ferst case 0: 23366527e757SMatheus Ferst /* 23376527e757SMatheus Ferst * This is a bug ! It means that has_work took us out of halt without 23386527e757SMatheus Ferst * anything to deliver while in a PM state that requires getting 23396527e757SMatheus Ferst * out via a 0x100 23406527e757SMatheus Ferst * 23416527e757SMatheus Ferst * This means we will incorrectly execute past the power management 23426527e757SMatheus Ferst * instruction instead of triggering a reset. 23436527e757SMatheus Ferst * 23446527e757SMatheus Ferst * It generally means a discrepancy between the wakeup conditions in the 23456527e757SMatheus Ferst * processor has_work implementation and the logic in this function. 23466527e757SMatheus Ferst */ 23476527e757SMatheus Ferst assert(!env->resume_as_sreset); 23486527e757SMatheus Ferst break; 23496527e757SMatheus Ferst default: 23506527e757SMatheus Ferst cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt); 23516527e757SMatheus Ferst } 23526527e757SMatheus Ferst } 23536527e757SMatheus Ferst 23543654e238SMatheus Ferst static void p9_deliver_interrupt(CPUPPCState *env, int interrupt) 23553654e238SMatheus Ferst { 23563654e238SMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 23573654e238SMatheus Ferst CPUState *cs = env_cpu(env); 23583654e238SMatheus Ferst 235927796411SMatheus Ferst if (cs->halted && !(env->spr[SPR_PSSCR] & PSSCR_EC) && 236027796411SMatheus Ferst !FIELD_EX64(env->msr, MSR, EE)) { 236127796411SMatheus Ferst /* 236227796411SMatheus Ferst * A pending interrupt took us out of power-saving, but MSR[EE] says 236327796411SMatheus Ferst * that we should return to NIP+4 instead of delivering it. 236427796411SMatheus Ferst */ 236527796411SMatheus Ferst return; 236627796411SMatheus Ferst } 236727796411SMatheus Ferst 23683654e238SMatheus Ferst switch (interrupt) { 23693654e238SMatheus Ferst case PPC_INTERRUPT_MCK: /* Machine check exception */ 23703654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_MCK; 23713654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_MCHECK); 23723654e238SMatheus Ferst break; 23733654e238SMatheus Ferst 23743654e238SMatheus Ferst case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */ 23753654e238SMatheus Ferst /* HDEC clears on delivery */ 23763654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDECR; 23773654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HDECR); 23783654e238SMatheus Ferst break; 23793654e238SMatheus Ferst case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */ 23803654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 23813654e238SMatheus Ferst break; 23823654e238SMatheus Ferst 23833654e238SMatheus Ferst case PPC_INTERRUPT_EXT: 23843654e238SMatheus Ferst if (books_vhyp_promotes_external_to_hvirt(cpu)) { 23853654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 23863654e238SMatheus Ferst } else { 23873654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL); 23883654e238SMatheus Ferst } 23893654e238SMatheus Ferst break; 23903654e238SMatheus Ferst 23913654e238SMatheus Ferst case PPC_INTERRUPT_DECR: /* Decrementer exception */ 23923654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DECR); 23933654e238SMatheus Ferst break; 23943654e238SMatheus Ferst case PPC_INTERRUPT_DOORBELL: 23953654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL; 23963654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR); 23973654e238SMatheus Ferst break; 23983654e238SMatheus Ferst case PPC_INTERRUPT_HDOORBELL: 23993654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL; 24003654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV); 24013654e238SMatheus Ferst break; 24023654e238SMatheus Ferst case PPC_INTERRUPT_PERFM: 24033654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PERFM; 24043654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM); 24053654e238SMatheus Ferst break; 24063654e238SMatheus Ferst case PPC_INTERRUPT_EBB: /* EBB exception */ 24073654e238SMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_EBB; 24083654e238SMatheus Ferst if (env->spr[SPR_BESCR] & BESCR_PMEO) { 24093654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB); 24103654e238SMatheus Ferst } else if (env->spr[SPR_BESCR] & BESCR_EEO) { 24113654e238SMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB); 24123654e238SMatheus Ferst } 24133654e238SMatheus Ferst break; 24143654e238SMatheus Ferst case 0: 24153654e238SMatheus Ferst /* 24163654e238SMatheus Ferst * This is a bug ! It means that has_work took us out of halt without 24173654e238SMatheus Ferst * anything to deliver while in a PM state that requires getting 24183654e238SMatheus Ferst * out via a 0x100 24193654e238SMatheus Ferst * 24203654e238SMatheus Ferst * This means we will incorrectly execute past the power management 24213654e238SMatheus Ferst * instruction instead of triggering a reset. 24223654e238SMatheus Ferst * 24233654e238SMatheus Ferst * It generally means a discrepancy between the wakeup conditions in the 24243654e238SMatheus Ferst * processor has_work implementation and the logic in this function. 24253654e238SMatheus Ferst */ 24263654e238SMatheus Ferst assert(!env->resume_as_sreset); 24273654e238SMatheus Ferst break; 24283654e238SMatheus Ferst default: 24293654e238SMatheus Ferst cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt); 24303654e238SMatheus Ferst } 24313654e238SMatheus Ferst } 24323654e238SMatheus Ferst #endif 24333654e238SMatheus Ferst 2434ba2898f7SMatheus Ferst static void ppc_deliver_interrupt_generic(CPUPPCState *env, int interrupt) 2435de76b85cSMatheus Ferst { 2436de76b85cSMatheus Ferst PowerPCCPU *cpu = env_archcpu(env); 2437de76b85cSMatheus Ferst CPUState *cs = env_cpu(env); 2438de76b85cSMatheus Ferst 2439de76b85cSMatheus Ferst switch (interrupt) { 2440de76b85cSMatheus Ferst case PPC_INTERRUPT_RESET: /* External reset */ 2441de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_RESET; 2442de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_RESET); 2443de76b85cSMatheus Ferst break; 2444de76b85cSMatheus Ferst case PPC_INTERRUPT_MCK: /* Machine check exception */ 2445de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_MCK; 2446de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_MCHECK); 2447de76b85cSMatheus Ferst break; 2448de76b85cSMatheus Ferst 2449de76b85cSMatheus Ferst case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */ 2450de76b85cSMatheus Ferst /* HDEC clears on delivery */ 2451de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDECR; 2452de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HDECR); 2453de76b85cSMatheus Ferst break; 2454de76b85cSMatheus Ferst case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */ 2455de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 2456de76b85cSMatheus Ferst break; 2457de76b85cSMatheus Ferst 2458de76b85cSMatheus Ferst case PPC_INTERRUPT_EXT: 2459de76b85cSMatheus Ferst if (books_vhyp_promotes_external_to_hvirt(cpu)) { 2460de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_HVIRT); 2461de76b85cSMatheus Ferst } else { 2462de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL); 2463de76b85cSMatheus Ferst } 2464de76b85cSMatheus Ferst break; 2465de76b85cSMatheus Ferst case PPC_INTERRUPT_CEXT: /* External critical interrupt */ 2466de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_CRITICAL); 2467de76b85cSMatheus Ferst break; 2468de76b85cSMatheus Ferst 2469de76b85cSMatheus Ferst case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */ 2470de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_WDT; 2471de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_WDT); 2472de76b85cSMatheus Ferst break; 2473de76b85cSMatheus Ferst case PPC_INTERRUPT_CDOORBELL: 2474de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL; 2475de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DOORCI); 2476de76b85cSMatheus Ferst break; 2477de76b85cSMatheus Ferst case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */ 2478de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_FIT; 2479de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_FIT); 2480de76b85cSMatheus Ferst break; 2481de76b85cSMatheus Ferst case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */ 2482de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PIT; 2483de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PIT); 2484de76b85cSMatheus Ferst break; 2485de76b85cSMatheus Ferst case PPC_INTERRUPT_DECR: /* Decrementer exception */ 2486de76b85cSMatheus Ferst if (ppc_decr_clear_on_delivery(env)) { 2487de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_DECR; 2488de76b85cSMatheus Ferst } 2489de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DECR); 2490de76b85cSMatheus Ferst break; 2491de76b85cSMatheus Ferst case PPC_INTERRUPT_DOORBELL: 2492de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL; 2493de76b85cSMatheus Ferst if (is_book3s_arch2x(env)) { 2494de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR); 2495de76b85cSMatheus Ferst } else { 2496de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_DOORI); 2497de76b85cSMatheus Ferst } 2498de76b85cSMatheus Ferst break; 2499de76b85cSMatheus Ferst case PPC_INTERRUPT_HDOORBELL: 2500de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL; 2501de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV); 2502de76b85cSMatheus Ferst break; 2503de76b85cSMatheus Ferst case PPC_INTERRUPT_PERFM: 2504de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_PERFM; 2505de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_PERFM); 2506de76b85cSMatheus Ferst break; 2507de76b85cSMatheus Ferst case PPC_INTERRUPT_THERM: /* Thermal interrupt */ 2508de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_THERM; 2509de76b85cSMatheus Ferst powerpc_excp(cpu, POWERPC_EXCP_THERM); 2510de76b85cSMatheus Ferst break; 2511de76b85cSMatheus Ferst case PPC_INTERRUPT_EBB: /* EBB exception */ 2512de76b85cSMatheus Ferst env->pending_interrupts &= ~PPC_INTERRUPT_EBB; 2513cb76bbc4SDaniel Henrique Barboza if (env->spr[SPR_BESCR] & BESCR_PMEO) { 2514cb76bbc4SDaniel Henrique Barboza powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB); 2515cb76bbc4SDaniel Henrique Barboza } else if (env->spr[SPR_BESCR] & BESCR_EEO) { 2516cb76bbc4SDaniel Henrique Barboza powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB); 2517cb76bbc4SDaniel Henrique Barboza } 2518de76b85cSMatheus Ferst break; 2519de76b85cSMatheus Ferst case 0: 2520f8154fd2SBenjamin Herrenschmidt /* 2521f8154fd2SBenjamin Herrenschmidt * This is a bug ! It means that has_work took us out of halt without 2522f8154fd2SBenjamin Herrenschmidt * anything to deliver while in a PM state that requires getting 2523f8154fd2SBenjamin Herrenschmidt * out via a 0x100 2524f8154fd2SBenjamin Herrenschmidt * 2525f8154fd2SBenjamin Herrenschmidt * This means we will incorrectly execute past the power management 2526f8154fd2SBenjamin Herrenschmidt * instruction instead of triggering a reset. 2527f8154fd2SBenjamin Herrenschmidt * 2528136fbf65Szhaolichang * It generally means a discrepancy between the wakeup conditions in the 2529f8154fd2SBenjamin Herrenschmidt * processor has_work implementation and the logic in this function. 2530f8154fd2SBenjamin Herrenschmidt */ 2531de76b85cSMatheus Ferst assert(!env->resume_as_sreset); 2532de76b85cSMatheus Ferst break; 2533de76b85cSMatheus Ferst default: 2534de76b85cSMatheus Ferst cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt); 2535f8154fd2SBenjamin Herrenschmidt } 2536c79c73f6SBlue Swirl } 253734316482SAlexey Kardashevskiy 2538ba2898f7SMatheus Ferst static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt) 2539ba2898f7SMatheus Ferst { 2540ba2898f7SMatheus Ferst switch (env->excp_model) { 25413654e238SMatheus Ferst #if defined(TARGET_PPC64) 2542d93a4856SMatheus Ferst case POWERPC_EXCP_POWER7: 2543d93a4856SMatheus Ferst p7_deliver_interrupt(env, interrupt); 2544d93a4856SMatheus Ferst break; 25456527e757SMatheus Ferst case POWERPC_EXCP_POWER8: 25466527e757SMatheus Ferst p8_deliver_interrupt(env, interrupt); 25476527e757SMatheus Ferst break; 25483654e238SMatheus Ferst case POWERPC_EXCP_POWER9: 25493654e238SMatheus Ferst case POWERPC_EXCP_POWER10: 25503654e238SMatheus Ferst p9_deliver_interrupt(env, interrupt); 25513654e238SMatheus Ferst break; 25523654e238SMatheus Ferst #endif 2553ba2898f7SMatheus Ferst default: 2554ba2898f7SMatheus Ferst ppc_deliver_interrupt_generic(env, interrupt); 2555ba2898f7SMatheus Ferst } 2556ba2898f7SMatheus Ferst } 2557ba2898f7SMatheus Ferst 2558b5b7f391SNicholas Piggin void ppc_cpu_do_system_reset(CPUState *cs) 255934316482SAlexey Kardashevskiy { 256034316482SAlexey Kardashevskiy PowerPCCPU *cpu = POWERPC_CPU(cs); 256134316482SAlexey Kardashevskiy 256293130c84SFabiano Rosas powerpc_excp(cpu, POWERPC_EXCP_RESET); 256334316482SAlexey Kardashevskiy } 2564ad77c6caSNicholas Piggin 2565ad77c6caSNicholas Piggin void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector) 2566ad77c6caSNicholas Piggin { 2567ad77c6caSNicholas Piggin PowerPCCPU *cpu = POWERPC_CPU(cs); 2568ad77c6caSNicholas Piggin CPUPPCState *env = &cpu->env; 2569ad77c6caSNicholas Piggin target_ulong msr = 0; 2570ad77c6caSNicholas Piggin 2571ad77c6caSNicholas Piggin /* 2572ad77c6caSNicholas Piggin * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already 2573ad77c6caSNicholas Piggin * been set by KVM. 2574ad77c6caSNicholas Piggin */ 2575ad77c6caSNicholas Piggin msr = (1ULL << MSR_ME); 2576ad77c6caSNicholas Piggin msr |= env->msr & (1ULL << MSR_SF); 2577516fc103SFabiano Rosas if (ppc_interrupts_little_endian(cpu, false)) { 2578ad77c6caSNicholas Piggin msr |= (1ULL << MSR_LE); 2579ad77c6caSNicholas Piggin } 2580ad77c6caSNicholas Piggin 25817cebc5dbSNicholas Piggin /* Anything for nested required here? MSR[HV] bit? */ 25827cebc5dbSNicholas Piggin 2583ad77c6caSNicholas Piggin powerpc_set_excp_state(cpu, vector, msr); 2584ad77c6caSNicholas Piggin } 2585c79c73f6SBlue Swirl 2586458dd766SRichard Henderson bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request) 2587458dd766SRichard Henderson { 2588458dd766SRichard Henderson PowerPCCPU *cpu = POWERPC_CPU(cs); 2589458dd766SRichard Henderson CPUPPCState *env = &cpu->env; 2590de76b85cSMatheus Ferst int interrupt; 2591458dd766SRichard Henderson 2592de76b85cSMatheus Ferst if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) { 2593de76b85cSMatheus Ferst return false; 2594de76b85cSMatheus Ferst } 2595de76b85cSMatheus Ferst 2596de76b85cSMatheus Ferst interrupt = ppc_next_unmasked_interrupt(env); 2597de76b85cSMatheus Ferst if (interrupt == 0) { 2598de76b85cSMatheus Ferst return false; 2599de76b85cSMatheus Ferst } 2600de76b85cSMatheus Ferst 2601de76b85cSMatheus Ferst ppc_deliver_interrupt(env, interrupt); 2602458dd766SRichard Henderson if (env->pending_interrupts == 0) { 2603de76b85cSMatheus Ferst cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); 2604458dd766SRichard Henderson } 2605458dd766SRichard Henderson return true; 2606458dd766SRichard Henderson } 2607458dd766SRichard Henderson 2608f725245cSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */ 2609f725245cSPhilippe Mathieu-Daudé 2610ad71ed68SBlue Swirl /*****************************************************************************/ 2611ad71ed68SBlue Swirl /* Exceptions processing helpers */ 2612ad71ed68SBlue Swirl 2613db789c6cSBenjamin Herrenschmidt void raise_exception_err_ra(CPUPPCState *env, uint32_t exception, 2614db789c6cSBenjamin Herrenschmidt uint32_t error_code, uintptr_t raddr) 2615ad71ed68SBlue Swirl { 2616db70b311SRichard Henderson CPUState *cs = env_cpu(env); 261727103424SAndreas Färber 261827103424SAndreas Färber cs->exception_index = exception; 2619ad71ed68SBlue Swirl env->error_code = error_code; 2620db789c6cSBenjamin Herrenschmidt cpu_loop_exit_restore(cs, raddr); 2621db789c6cSBenjamin Herrenschmidt } 2622db789c6cSBenjamin Herrenschmidt 2623db789c6cSBenjamin Herrenschmidt void raise_exception_err(CPUPPCState *env, uint32_t exception, 2624db789c6cSBenjamin Herrenschmidt uint32_t error_code) 2625db789c6cSBenjamin Herrenschmidt { 2626db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, error_code, 0); 2627db789c6cSBenjamin Herrenschmidt } 2628db789c6cSBenjamin Herrenschmidt 2629db789c6cSBenjamin Herrenschmidt void raise_exception(CPUPPCState *env, uint32_t exception) 2630db789c6cSBenjamin Herrenschmidt { 2631db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, 0, 0); 2632db789c6cSBenjamin Herrenschmidt } 2633db789c6cSBenjamin Herrenschmidt 2634db789c6cSBenjamin Herrenschmidt void raise_exception_ra(CPUPPCState *env, uint32_t exception, 2635db789c6cSBenjamin Herrenschmidt uintptr_t raddr) 2636db789c6cSBenjamin Herrenschmidt { 2637db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, 0, raddr); 2638db789c6cSBenjamin Herrenschmidt } 2639db789c6cSBenjamin Herrenschmidt 26402b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 2641db789c6cSBenjamin Herrenschmidt void helper_raise_exception_err(CPUPPCState *env, uint32_t exception, 2642db789c6cSBenjamin Herrenschmidt uint32_t error_code) 2643db789c6cSBenjamin Herrenschmidt { 2644db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, error_code, 0); 2645ad71ed68SBlue Swirl } 2646ad71ed68SBlue Swirl 2647e5f17ac6SBlue Swirl void helper_raise_exception(CPUPPCState *env, uint32_t exception) 2648ad71ed68SBlue Swirl { 2649db789c6cSBenjamin Herrenschmidt raise_exception_err_ra(env, exception, 0, 0); 2650ad71ed68SBlue Swirl } 26512b44e219SBruno Larsen (billionai) #endif 2652ad71ed68SBlue Swirl 2653ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY) 26542b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 2655e5f17ac6SBlue Swirl void helper_store_msr(CPUPPCState *env, target_ulong val) 2656ad71ed68SBlue Swirl { 2657db789c6cSBenjamin Herrenschmidt uint32_t excp = hreg_store_msr(env, val, 0); 2658259186a7SAndreas Färber 2659db789c6cSBenjamin Herrenschmidt if (excp != 0) { 26600661329aSBALATON Zoltan cpu_interrupt_exittb(env_cpu(env)); 2661db789c6cSBenjamin Herrenschmidt raise_exception(env, excp); 2662ad71ed68SBlue Swirl } 2663ad71ed68SBlue Swirl } 2664ad71ed68SBlue Swirl 26652fdedcbcSMatheus Ferst void helper_ppc_maybe_interrupt(CPUPPCState *env) 26662fdedcbcSMatheus Ferst { 26672fdedcbcSMatheus Ferst ppc_maybe_interrupt(env); 26682fdedcbcSMatheus Ferst } 26692fdedcbcSMatheus Ferst 26707778a575SBenjamin Herrenschmidt #if defined(TARGET_PPC64) 2671f43520e5SRichard Henderson void helper_scv(CPUPPCState *env, uint32_t lev) 2672f43520e5SRichard Henderson { 2673f43520e5SRichard Henderson if (env->spr[SPR_FSCR] & (1ull << FSCR_SCV)) { 2674f43520e5SRichard Henderson raise_exception_err(env, POWERPC_EXCP_SYSCALL_VECTORED, lev); 2675f43520e5SRichard Henderson } else { 2676f43520e5SRichard Henderson raise_exception_err(env, POWERPC_EXCP_FU, FSCR_IC_SCV); 2677f43520e5SRichard Henderson } 2678f43520e5SRichard Henderson } 2679f43520e5SRichard Henderson 268007e4804fSCédric Le Goater void helper_pminsn(CPUPPCState *env, uint32_t insn) 26817778a575SBenjamin Herrenschmidt { 26824e6b7db2SBALATON Zoltan CPUState *cs = env_cpu(env); 26837778a575SBenjamin Herrenschmidt 26847778a575SBenjamin Herrenschmidt cs->halted = 1; 26857778a575SBenjamin Herrenschmidt 26863621e2c9SBenjamin Herrenschmidt /* Condition for waking up at 0x100 */ 26871e7fd61dSBenjamin Herrenschmidt env->resume_as_sreset = (insn != PPC_PM_STOP) || 268821c0d66aSBenjamin Herrenschmidt (env->spr[SPR_PSSCR] & PSSCR_EC); 26892fdedcbcSMatheus Ferst 26909915dac4SNicholas Piggin /* HDECR is not to wake from PM state, it may have already fired */ 26919915dac4SNicholas Piggin if (env->resume_as_sreset) { 26929915dac4SNicholas Piggin PowerPCCPU *cpu = env_archcpu(env); 26939915dac4SNicholas Piggin ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 0); 26949915dac4SNicholas Piggin } 26959915dac4SNicholas Piggin 26962fdedcbcSMatheus Ferst ppc_maybe_interrupt(env); 26977778a575SBenjamin Herrenschmidt } 26987778a575SBenjamin Herrenschmidt #endif /* defined(TARGET_PPC64) */ 26997778a575SBenjamin Herrenschmidt 270062e79ef9SCédric Le Goater static void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr) 2701ad71ed68SBlue Swirl { 2702a2e71b28SBenjamin Herrenschmidt /* MSR:POW cannot be set by any form of rfi */ 2703a2e71b28SBenjamin Herrenschmidt msr &= ~(1ULL << MSR_POW); 2704a2e71b28SBenjamin Herrenschmidt 27055aad0457SChristophe Leroy /* MSR:TGPR cannot be set by any form of rfi */ 27065aad0457SChristophe Leroy if (env->flags & POWERPC_FLAG_TGPR) 27075aad0457SChristophe Leroy msr &= ~(1ULL << MSR_TGPR); 27085aad0457SChristophe Leroy 2709ad71ed68SBlue Swirl #if defined(TARGET_PPC64) 2710a2e71b28SBenjamin Herrenschmidt /* Switching to 32-bit ? Crop the nip */ 2711a2e71b28SBenjamin Herrenschmidt if (!msr_is_64bit(env, msr)) { 2712ad71ed68SBlue Swirl nip = (uint32_t)nip; 2713ad71ed68SBlue Swirl } 2714ad71ed68SBlue Swirl #else 2715ad71ed68SBlue Swirl nip = (uint32_t)nip; 2716ad71ed68SBlue Swirl #endif 2717ad71ed68SBlue Swirl /* XXX: beware: this is false if VLE is supported */ 2718ad71ed68SBlue Swirl env->nip = nip & ~((target_ulong)0x00000003); 2719ad71ed68SBlue Swirl hreg_store_msr(env, msr, 1); 27202eb1ef73SCédric Le Goater trace_ppc_excp_rfi(env->nip, env->msr); 272147733729SDavid Gibson /* 272247733729SDavid Gibson * No need to raise an exception here, as rfi is always the last 272347733729SDavid Gibson * insn of a TB 2724ad71ed68SBlue Swirl */ 27250661329aSBALATON Zoltan cpu_interrupt_exittb(env_cpu(env)); 2726a8b73734SNikunj A Dadhania /* Reset the reservation */ 2727a8b73734SNikunj A Dadhania env->reserve_addr = -1; 2728a8b73734SNikunj A Dadhania 2729cd0c6f47SBenjamin Herrenschmidt /* Context synchronizing: check if TCG TLB needs flush */ 2730e3cffe6fSNikunj A Dadhania check_tlb_flush(env, false); 2731ad71ed68SBlue Swirl } 2732ad71ed68SBlue Swirl 2733e5f17ac6SBlue Swirl void helper_rfi(CPUPPCState *env) 2734ad71ed68SBlue Swirl { 2735a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful); 2736a1bb7384SScott Wood } 2737ad71ed68SBlue Swirl 2738ad71ed68SBlue Swirl #if defined(TARGET_PPC64) 2739e5f17ac6SBlue Swirl void helper_rfid(CPUPPCState *env) 2740ad71ed68SBlue Swirl { 274147733729SDavid Gibson /* 2742136fbf65Szhaolichang * The architecture defines a number of rules for which bits can 274347733729SDavid Gibson * change but in practice, we handle this in hreg_store_msr() 2744a2e71b28SBenjamin Herrenschmidt * which will be called by do_rfi(), so there is no need to filter 2745a2e71b28SBenjamin Herrenschmidt * here 2746a2e71b28SBenjamin Herrenschmidt */ 2747a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]); 2748ad71ed68SBlue Swirl } 2749ad71ed68SBlue Swirl 27503c89b8d6SNicholas Piggin void helper_rfscv(CPUPPCState *env) 27513c89b8d6SNicholas Piggin { 27523c89b8d6SNicholas Piggin do_rfi(env, env->lr, env->ctr); 27533c89b8d6SNicholas Piggin } 27543c89b8d6SNicholas Piggin 2755e5f17ac6SBlue Swirl void helper_hrfid(CPUPPCState *env) 2756ad71ed68SBlue Swirl { 2757a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]); 2758ad71ed68SBlue Swirl } 2759ad71ed68SBlue Swirl #endif 2760ad71ed68SBlue Swirl 27611f26c751SDaniel Henrique Barboza #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY) 27621f26c751SDaniel Henrique Barboza void helper_rfebb(CPUPPCState *env, target_ulong s) 27631f26c751SDaniel Henrique Barboza { 27641f26c751SDaniel Henrique Barboza target_ulong msr = env->msr; 27651f26c751SDaniel Henrique Barboza 27661f26c751SDaniel Henrique Barboza /* 27671f26c751SDaniel Henrique Barboza * Handling of BESCR bits 32:33 according to PowerISA v3.1: 27681f26c751SDaniel Henrique Barboza * 27691f26c751SDaniel Henrique Barboza * "If BESCR 32:33 != 0b00 the instruction is treated as if 27701f26c751SDaniel Henrique Barboza * the instruction form were invalid." 27711f26c751SDaniel Henrique Barboza */ 27721f26c751SDaniel Henrique Barboza if (env->spr[SPR_BESCR] & BESCR_INVALID) { 27731f26c751SDaniel Henrique Barboza raise_exception_err(env, POWERPC_EXCP_PROGRAM, 27741f26c751SDaniel Henrique Barboza POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL); 27751f26c751SDaniel Henrique Barboza } 27761f26c751SDaniel Henrique Barboza 27771f26c751SDaniel Henrique Barboza env->nip = env->spr[SPR_EBBRR]; 27781f26c751SDaniel Henrique Barboza 27791f26c751SDaniel Henrique Barboza /* Switching to 32-bit ? Crop the nip */ 27801f26c751SDaniel Henrique Barboza if (!msr_is_64bit(env, msr)) { 27811f26c751SDaniel Henrique Barboza env->nip = (uint32_t)env->spr[SPR_EBBRR]; 27821f26c751SDaniel Henrique Barboza } 27831f26c751SDaniel Henrique Barboza 27841f26c751SDaniel Henrique Barboza if (s) { 27851f26c751SDaniel Henrique Barboza env->spr[SPR_BESCR] |= BESCR_GE; 27861f26c751SDaniel Henrique Barboza } else { 27871f26c751SDaniel Henrique Barboza env->spr[SPR_BESCR] &= ~BESCR_GE; 27881f26c751SDaniel Henrique Barboza } 27891f26c751SDaniel Henrique Barboza } 2790d3412df2SDaniel Henrique Barboza 2791d3412df2SDaniel Henrique Barboza /* 2792d3412df2SDaniel Henrique Barboza * Triggers or queues an 'ebb_excp' EBB exception. All checks 2793d3412df2SDaniel Henrique Barboza * but FSCR, HFSCR and msr_pr must be done beforehand. 2794d3412df2SDaniel Henrique Barboza * 2795d3412df2SDaniel Henrique Barboza * PowerISA v3.1 isn't clear about whether an EBB should be 2796d3412df2SDaniel Henrique Barboza * postponed or cancelled if the EBB facility is unavailable. 2797d3412df2SDaniel Henrique Barboza * Our assumption here is that the EBB is cancelled if both 2798d3412df2SDaniel Henrique Barboza * FSCR and HFSCR EBB facilities aren't available. 2799d3412df2SDaniel Henrique Barboza */ 2800d3412df2SDaniel Henrique Barboza static void do_ebb(CPUPPCState *env, int ebb_excp) 2801d3412df2SDaniel Henrique Barboza { 2802d3412df2SDaniel Henrique Barboza PowerPCCPU *cpu = env_archcpu(env); 2803d3412df2SDaniel Henrique Barboza 2804d3412df2SDaniel Henrique Barboza /* 2805d3412df2SDaniel Henrique Barboza * FSCR_EBB and FSCR_IC_EBB are the same bits used with 2806d3412df2SDaniel Henrique Barboza * HFSCR. 2807d3412df2SDaniel Henrique Barboza */ 2808d3412df2SDaniel Henrique Barboza helper_fscr_facility_check(env, FSCR_EBB, 0, FSCR_IC_EBB); 2809d3412df2SDaniel Henrique Barboza helper_hfscr_facility_check(env, FSCR_EBB, "EBB", FSCR_IC_EBB); 2810d3412df2SDaniel Henrique Barboza 2811d3412df2SDaniel Henrique Barboza if (ebb_excp == POWERPC_EXCP_PERFM_EBB) { 2812d3412df2SDaniel Henrique Barboza env->spr[SPR_BESCR] |= BESCR_PMEO; 2813d3412df2SDaniel Henrique Barboza } else if (ebb_excp == POWERPC_EXCP_EXTERNAL_EBB) { 2814d3412df2SDaniel Henrique Barboza env->spr[SPR_BESCR] |= BESCR_EEO; 2815d3412df2SDaniel Henrique Barboza } 2816d3412df2SDaniel Henrique Barboza 2817d41ccf6eSVíctor Colombo if (FIELD_EX64(env->msr, MSR, PR)) { 2818d3412df2SDaniel Henrique Barboza powerpc_excp(cpu, ebb_excp); 2819d3412df2SDaniel Henrique Barboza } else { 28207b694df6SMatheus Ferst ppc_set_irq(cpu, PPC_INTERRUPT_EBB, 1); 2821d3412df2SDaniel Henrique Barboza } 2822d3412df2SDaniel Henrique Barboza } 2823d3412df2SDaniel Henrique Barboza 2824d3412df2SDaniel Henrique Barboza void raise_ebb_perfm_exception(CPUPPCState *env) 2825d3412df2SDaniel Henrique Barboza { 2826d3412df2SDaniel Henrique Barboza bool perfm_ebb_enabled = env->spr[SPR_POWER_MMCR0] & MMCR0_EBE && 2827d3412df2SDaniel Henrique Barboza env->spr[SPR_BESCR] & BESCR_PME && 2828d3412df2SDaniel Henrique Barboza env->spr[SPR_BESCR] & BESCR_GE; 2829d3412df2SDaniel Henrique Barboza 2830d3412df2SDaniel Henrique Barboza if (!perfm_ebb_enabled) { 2831d3412df2SDaniel Henrique Barboza return; 2832d3412df2SDaniel Henrique Barboza } 2833d3412df2SDaniel Henrique Barboza 2834d3412df2SDaniel Henrique Barboza do_ebb(env, POWERPC_EXCP_PERFM_EBB); 2835d3412df2SDaniel Henrique Barboza } 28361f26c751SDaniel Henrique Barboza #endif 28371f26c751SDaniel Henrique Barboza 2838ad71ed68SBlue Swirl /*****************************************************************************/ 2839ad71ed68SBlue Swirl /* Embedded PowerPC specific helpers */ 2840e5f17ac6SBlue Swirl void helper_40x_rfci(CPUPPCState *env) 2841ad71ed68SBlue Swirl { 2842a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]); 2843ad71ed68SBlue Swirl } 2844ad71ed68SBlue Swirl 2845e5f17ac6SBlue Swirl void helper_rfci(CPUPPCState *env) 2846ad71ed68SBlue Swirl { 2847a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]); 2848ad71ed68SBlue Swirl } 2849ad71ed68SBlue Swirl 2850e5f17ac6SBlue Swirl void helper_rfdi(CPUPPCState *env) 2851ad71ed68SBlue Swirl { 2852a1bb7384SScott Wood /* FIXME: choose CSRR1 or DSRR1 based on cpu type */ 2853a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]); 2854ad71ed68SBlue Swirl } 2855ad71ed68SBlue Swirl 2856e5f17ac6SBlue Swirl void helper_rfmci(CPUPPCState *env) 2857ad71ed68SBlue Swirl { 2858a1bb7384SScott Wood /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */ 2859a2e71b28SBenjamin Herrenschmidt do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]); 2860ad71ed68SBlue Swirl } 28612b44e219SBruno Larsen (billionai) #endif /* CONFIG_TCG */ 28622b44e219SBruno Larsen (billionai) #endif /* !defined(CONFIG_USER_ONLY) */ 2863ad71ed68SBlue Swirl 28642b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 2865e5f17ac6SBlue Swirl void helper_tw(CPUPPCState *env, target_ulong arg1, target_ulong arg2, 2866e5f17ac6SBlue Swirl uint32_t flags) 2867ad71ed68SBlue Swirl { 2868ad71ed68SBlue Swirl if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) || 2869ad71ed68SBlue Swirl ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) || 2870ad71ed68SBlue Swirl ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) || 2871ad71ed68SBlue Swirl ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) || 2872ad71ed68SBlue Swirl ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) { 287372073dccSBenjamin Herrenschmidt raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 287472073dccSBenjamin Herrenschmidt POWERPC_EXCP_TRAP, GETPC()); 2875ad71ed68SBlue Swirl } 2876ad71ed68SBlue Swirl } 2877ad71ed68SBlue Swirl 2878ad71ed68SBlue Swirl #if defined(TARGET_PPC64) 2879e5f17ac6SBlue Swirl void helper_td(CPUPPCState *env, target_ulong arg1, target_ulong arg2, 2880e5f17ac6SBlue Swirl uint32_t flags) 2881ad71ed68SBlue Swirl { 2882ad71ed68SBlue Swirl if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) || 2883ad71ed68SBlue Swirl ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) || 2884ad71ed68SBlue Swirl ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) || 2885ad71ed68SBlue Swirl ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) || 2886ad71ed68SBlue Swirl ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) { 288772073dccSBenjamin Herrenschmidt raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 288872073dccSBenjamin Herrenschmidt POWERPC_EXCP_TRAP, GETPC()); 2889ad71ed68SBlue Swirl } 2890ad71ed68SBlue Swirl } 2891ad71ed68SBlue Swirl #endif 28922b44e219SBruno Larsen (billionai) #endif 2893ad71ed68SBlue Swirl 2894049b4ad6SVaibhav Jain #ifdef CONFIG_TCG 2895670f1da3SVíctor Colombo static uint32_t helper_SIMON_LIKE_32_64(uint32_t x, uint64_t key, uint32_t lane) 2896670f1da3SVíctor Colombo { 2897670f1da3SVíctor Colombo const uint16_t c = 0xfffc; 2898670f1da3SVíctor Colombo const uint64_t z0 = 0xfa2561cdf44ac398ULL; 2899670f1da3SVíctor Colombo uint16_t z = 0, temp; 2900670f1da3SVíctor Colombo uint16_t k[32], eff_k[32], xleft[33], xright[33], fxleft[32]; 2901670f1da3SVíctor Colombo 2902670f1da3SVíctor Colombo for (int i = 3; i >= 0; i--) { 2903670f1da3SVíctor Colombo k[i] = key & 0xffff; 2904670f1da3SVíctor Colombo key >>= 16; 2905670f1da3SVíctor Colombo } 2906670f1da3SVíctor Colombo xleft[0] = x & 0xffff; 2907670f1da3SVíctor Colombo xright[0] = (x >> 16) & 0xffff; 2908670f1da3SVíctor Colombo 2909670f1da3SVíctor Colombo for (int i = 0; i < 28; i++) { 2910670f1da3SVíctor Colombo z = (z0 >> (63 - i)) & 1; 2911670f1da3SVíctor Colombo temp = ror16(k[i + 3], 3) ^ k[i + 1]; 2912670f1da3SVíctor Colombo k[i + 4] = c ^ z ^ k[i] ^ temp ^ ror16(temp, 1); 2913670f1da3SVíctor Colombo } 2914670f1da3SVíctor Colombo 2915670f1da3SVíctor Colombo for (int i = 0; i < 8; i++) { 2916670f1da3SVíctor Colombo eff_k[4 * i + 0] = k[4 * i + ((0 + lane) % 4)]; 2917670f1da3SVíctor Colombo eff_k[4 * i + 1] = k[4 * i + ((1 + lane) % 4)]; 2918670f1da3SVíctor Colombo eff_k[4 * i + 2] = k[4 * i + ((2 + lane) % 4)]; 2919670f1da3SVíctor Colombo eff_k[4 * i + 3] = k[4 * i + ((3 + lane) % 4)]; 2920670f1da3SVíctor Colombo } 2921670f1da3SVíctor Colombo 2922670f1da3SVíctor Colombo for (int i = 0; i < 32; i++) { 2923670f1da3SVíctor Colombo fxleft[i] = (rol16(xleft[i], 1) & 2924670f1da3SVíctor Colombo rol16(xleft[i], 8)) ^ rol16(xleft[i], 2); 2925670f1da3SVíctor Colombo xleft[i + 1] = xright[i] ^ fxleft[i] ^ eff_k[i]; 2926670f1da3SVíctor Colombo xright[i + 1] = xleft[i]; 2927670f1da3SVíctor Colombo } 2928670f1da3SVíctor Colombo 2929670f1da3SVíctor Colombo return (((uint32_t)xright[32]) << 16) | xleft[32]; 2930670f1da3SVíctor Colombo } 2931670f1da3SVíctor Colombo 2932670f1da3SVíctor Colombo static uint64_t hash_digest(uint64_t ra, uint64_t rb, uint64_t key) 2933670f1da3SVíctor Colombo { 2934670f1da3SVíctor Colombo uint64_t stage0_h = 0ULL, stage0_l = 0ULL; 2935670f1da3SVíctor Colombo uint64_t stage1_h, stage1_l; 2936670f1da3SVíctor Colombo 2937670f1da3SVíctor Colombo for (int i = 0; i < 4; i++) { 2938670f1da3SVíctor Colombo stage0_h |= ror64(rb & 0xff, 8 * (2 * i + 1)); 2939670f1da3SVíctor Colombo stage0_h |= ((ra >> 32) & 0xff) << (8 * 2 * i); 2940670f1da3SVíctor Colombo stage0_l |= ror64((rb >> 32) & 0xff, 8 * (2 * i + 1)); 2941670f1da3SVíctor Colombo stage0_l |= (ra & 0xff) << (8 * 2 * i); 2942670f1da3SVíctor Colombo rb >>= 8; 2943670f1da3SVíctor Colombo ra >>= 8; 2944670f1da3SVíctor Colombo } 2945670f1da3SVíctor Colombo 2946670f1da3SVíctor Colombo stage1_h = (uint64_t)helper_SIMON_LIKE_32_64(stage0_h >> 32, key, 0) << 32; 2947670f1da3SVíctor Colombo stage1_h |= helper_SIMON_LIKE_32_64(stage0_h, key, 1); 2948670f1da3SVíctor Colombo stage1_l = (uint64_t)helper_SIMON_LIKE_32_64(stage0_l >> 32, key, 2) << 32; 2949670f1da3SVíctor Colombo stage1_l |= helper_SIMON_LIKE_32_64(stage0_l, key, 3); 2950670f1da3SVíctor Colombo 2951670f1da3SVíctor Colombo return stage1_h ^ stage1_l; 2952670f1da3SVíctor Colombo } 2953670f1da3SVíctor Colombo 29544091fabfSNicholas Miehlbradt static void do_hash(CPUPPCState *env, target_ulong ea, target_ulong ra, 29554091fabfSNicholas Miehlbradt target_ulong rb, uint64_t key, bool store) 29564091fabfSNicholas Miehlbradt { 29574091fabfSNicholas Miehlbradt uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash; 29584091fabfSNicholas Miehlbradt 29594091fabfSNicholas Miehlbradt if (store) { 29604091fabfSNicholas Miehlbradt cpu_stq_data_ra(env, ea, calculated_hash, GETPC()); 29614091fabfSNicholas Miehlbradt } else { 29624091fabfSNicholas Miehlbradt loaded_hash = cpu_ldq_data_ra(env, ea, GETPC()); 29634091fabfSNicholas Miehlbradt if (loaded_hash != calculated_hash) { 29644091fabfSNicholas Miehlbradt raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM, 29654091fabfSNicholas Miehlbradt POWERPC_EXCP_TRAP, GETPC()); 29664091fabfSNicholas Miehlbradt } 29674091fabfSNicholas Miehlbradt } 29684091fabfSNicholas Miehlbradt } 29694091fabfSNicholas Miehlbradt 2970670f1da3SVíctor Colombo #include "qemu/guest-random.h" 2971670f1da3SVíctor Colombo 29724091fabfSNicholas Miehlbradt #ifdef TARGET_PPC64 29734091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect) \ 2974670f1da3SVíctor Colombo void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra, \ 2975670f1da3SVíctor Colombo target_ulong rb) \ 2976670f1da3SVíctor Colombo { \ 29774091fabfSNicholas Miehlbradt if (env->msr & R_MSR_PR_MASK) { \ 29784091fabfSNicholas Miehlbradt if (!(env->spr[SPR_DEXCR] & R_DEXCR_PRO_##dexcr_aspect##_MASK || \ 29794091fabfSNicholas Miehlbradt env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK)) \ 29804091fabfSNicholas Miehlbradt return; \ 29814091fabfSNicholas Miehlbradt } else if (!(env->msr & R_MSR_HV_MASK)) { \ 29824091fabfSNicholas Miehlbradt if (!(env->spr[SPR_DEXCR] & R_DEXCR_PNH_##dexcr_aspect##_MASK || \ 29834091fabfSNicholas Miehlbradt env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK)) \ 29844091fabfSNicholas Miehlbradt return; \ 29854091fabfSNicholas Miehlbradt } else if (!(env->msr & R_MSR_S_MASK)) { \ 29864091fabfSNicholas Miehlbradt if (!(env->spr[SPR_HDEXCR] & R_HDEXCR_HNU_##dexcr_aspect##_MASK)) \ 29874091fabfSNicholas Miehlbradt return; \ 29884091fabfSNicholas Miehlbradt } \ 2989670f1da3SVíctor Colombo \ 29904091fabfSNicholas Miehlbradt do_hash(env, ea, ra, rb, key, store); \ 2991670f1da3SVíctor Colombo } 29924091fabfSNicholas Miehlbradt #else 29934091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect) \ 29944091fabfSNicholas Miehlbradt void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra, \ 29954091fabfSNicholas Miehlbradt target_ulong rb) \ 29964091fabfSNicholas Miehlbradt { \ 29974091fabfSNicholas Miehlbradt do_hash(env, ea, ra, rb, key, store); \ 29984091fabfSNicholas Miehlbradt } 29994091fabfSNicholas Miehlbradt #endif /* TARGET_PPC64 */ 3000670f1da3SVíctor Colombo 30014091fabfSNicholas Miehlbradt HELPER_HASH(HASHST, env->spr[SPR_HASHKEYR], true, NPHIE) 30024091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHK, env->spr[SPR_HASHKEYR], false, NPHIE) 30034091fabfSNicholas Miehlbradt HELPER_HASH(HASHSTP, env->spr[SPR_HASHPKEYR], true, PHIE) 30044091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHKP, env->spr[SPR_HASHPKEYR], false, PHIE) 3005049b4ad6SVaibhav Jain #endif /* CONFIG_TCG */ 3006670f1da3SVíctor Colombo 3007ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY) 3008ad71ed68SBlue Swirl 30092b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG 3010ad71ed68SBlue Swirl 3011ad71ed68SBlue Swirl /* Embedded.Processor Control */ 3012ad71ed68SBlue Swirl static int dbell2irq(target_ulong rb) 3013ad71ed68SBlue Swirl { 3014ad71ed68SBlue Swirl int msg = rb & DBELL_TYPE_MASK; 3015ad71ed68SBlue Swirl int irq = -1; 3016ad71ed68SBlue Swirl 3017ad71ed68SBlue Swirl switch (msg) { 3018ad71ed68SBlue Swirl case DBELL_TYPE_DBELL: 3019ad71ed68SBlue Swirl irq = PPC_INTERRUPT_DOORBELL; 3020ad71ed68SBlue Swirl break; 3021ad71ed68SBlue Swirl case DBELL_TYPE_DBELL_CRIT: 3022ad71ed68SBlue Swirl irq = PPC_INTERRUPT_CDOORBELL; 3023ad71ed68SBlue Swirl break; 3024ad71ed68SBlue Swirl case DBELL_TYPE_G_DBELL: 3025ad71ed68SBlue Swirl case DBELL_TYPE_G_DBELL_CRIT: 3026ad71ed68SBlue Swirl case DBELL_TYPE_G_DBELL_MC: 3027ad71ed68SBlue Swirl /* XXX implement */ 3028ad71ed68SBlue Swirl default: 3029ad71ed68SBlue Swirl break; 3030ad71ed68SBlue Swirl } 3031ad71ed68SBlue Swirl 3032ad71ed68SBlue Swirl return irq; 3033ad71ed68SBlue Swirl } 3034ad71ed68SBlue Swirl 3035e5f17ac6SBlue Swirl void helper_msgclr(CPUPPCState *env, target_ulong rb) 3036ad71ed68SBlue Swirl { 3037ad71ed68SBlue Swirl int irq = dbell2irq(rb); 3038ad71ed68SBlue Swirl 3039ad71ed68SBlue Swirl if (irq < 0) { 3040ad71ed68SBlue Swirl return; 3041ad71ed68SBlue Swirl } 3042ad71ed68SBlue Swirl 30437b694df6SMatheus Ferst ppc_set_irq(env_archcpu(env), irq, 0); 3044ad71ed68SBlue Swirl } 3045ad71ed68SBlue Swirl 3046ad71ed68SBlue Swirl void helper_msgsnd(target_ulong rb) 3047ad71ed68SBlue Swirl { 3048ad71ed68SBlue Swirl int irq = dbell2irq(rb); 3049ad71ed68SBlue Swirl int pir = rb & DBELL_PIRTAG_MASK; 3050182735efSAndreas Färber CPUState *cs; 3051ad71ed68SBlue Swirl 3052ad71ed68SBlue Swirl if (irq < 0) { 3053ad71ed68SBlue Swirl return; 3054ad71ed68SBlue Swirl } 3055ad71ed68SBlue Swirl 3056f1c29ebcSThomas Huth qemu_mutex_lock_iothread(); 3057bdc44640SAndreas Färber CPU_FOREACH(cs) { 3058182735efSAndreas Färber PowerPCCPU *cpu = POWERPC_CPU(cs); 3059182735efSAndreas Färber CPUPPCState *cenv = &cpu->env; 3060182735efSAndreas Färber 3061ad71ed68SBlue Swirl if ((rb & DBELL_BRDCAST) || (cenv->spr[SPR_BOOKE_PIR] == pir)) { 30627b694df6SMatheus Ferst ppc_set_irq(cpu, irq, 1); 3063ad71ed68SBlue Swirl } 3064ad71ed68SBlue Swirl } 3065f1c29ebcSThomas Huth qemu_mutex_unlock_iothread(); 3066ad71ed68SBlue Swirl } 30677af1e7b0SCédric Le Goater 30687af1e7b0SCédric Le Goater /* Server Processor Control */ 30697af1e7b0SCédric Le Goater 30705ba7ba1dSCédric Le Goater static bool dbell_type_server(target_ulong rb) 30715ba7ba1dSCédric Le Goater { 307247733729SDavid Gibson /* 307347733729SDavid Gibson * A Directed Hypervisor Doorbell message is sent only if the 30747af1e7b0SCédric Le Goater * message type is 5. All other types are reserved and the 307547733729SDavid Gibson * instruction is a no-op 307647733729SDavid Gibson */ 30775ba7ba1dSCédric Le Goater return (rb & DBELL_TYPE_MASK) == DBELL_TYPE_DBELL_SERVER; 30787af1e7b0SCédric Le Goater } 30797af1e7b0SCédric Le Goater 30807af1e7b0SCédric Le Goater void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb) 30817af1e7b0SCédric Le Goater { 30825ba7ba1dSCédric Le Goater if (!dbell_type_server(rb)) { 30837af1e7b0SCédric Le Goater return; 30847af1e7b0SCédric Le Goater } 30857af1e7b0SCédric Le Goater 30867b694df6SMatheus Ferst ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0); 30877af1e7b0SCédric Le Goater } 30887af1e7b0SCédric Le Goater 30895ba7ba1dSCédric Le Goater static void book3s_msgsnd_common(int pir, int irq) 30907af1e7b0SCédric Le Goater { 30917af1e7b0SCédric Le Goater CPUState *cs; 30927af1e7b0SCédric Le Goater 30937af1e7b0SCédric Le Goater qemu_mutex_lock_iothread(); 30947af1e7b0SCédric Le Goater CPU_FOREACH(cs) { 30957af1e7b0SCédric Le Goater PowerPCCPU *cpu = POWERPC_CPU(cs); 30967af1e7b0SCédric Le Goater CPUPPCState *cenv = &cpu->env; 30977af1e7b0SCédric Le Goater 30987af1e7b0SCédric Le Goater /* TODO: broadcast message to all threads of the same processor */ 30997af1e7b0SCédric Le Goater if (cenv->spr_cb[SPR_PIR].default_value == pir) { 31007b694df6SMatheus Ferst ppc_set_irq(cpu, irq, 1); 31017af1e7b0SCédric Le Goater } 31027af1e7b0SCédric Le Goater } 31037af1e7b0SCédric Le Goater qemu_mutex_unlock_iothread(); 31047af1e7b0SCédric Le Goater } 31055ba7ba1dSCédric Le Goater 31065ba7ba1dSCédric Le Goater void helper_book3s_msgsnd(target_ulong rb) 31075ba7ba1dSCédric Le Goater { 31085ba7ba1dSCédric Le Goater int pir = rb & DBELL_PROCIDTAG_MASK; 31095ba7ba1dSCédric Le Goater 31105ba7ba1dSCédric Le Goater if (!dbell_type_server(rb)) { 31115ba7ba1dSCédric Le Goater return; 31125ba7ba1dSCédric Le Goater } 31135ba7ba1dSCédric Le Goater 31145ba7ba1dSCédric Le Goater book3s_msgsnd_common(pir, PPC_INTERRUPT_HDOORBELL); 31155ba7ba1dSCédric Le Goater } 31165ba7ba1dSCédric Le Goater 31175ba7ba1dSCédric Le Goater #if defined(TARGET_PPC64) 31185ba7ba1dSCédric Le Goater void helper_book3s_msgclrp(CPUPPCState *env, target_ulong rb) 31195ba7ba1dSCédric Le Goater { 3120493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "msgclrp", HFSCR_IC_MSGP); 3121493028d8SCédric Le Goater 31225ba7ba1dSCédric Le Goater if (!dbell_type_server(rb)) { 31235ba7ba1dSCédric Le Goater return; 31245ba7ba1dSCédric Le Goater } 31255ba7ba1dSCédric Le Goater 31262e985555SNicholas Piggin ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_DOORBELL, 0); 31275ba7ba1dSCédric Le Goater } 31285ba7ba1dSCédric Le Goater 31295ba7ba1dSCédric Le Goater /* 3130d24e80b2SNicholas Piggin * sends a message to another thread on the same 31315ba7ba1dSCédric Le Goater * multi-threaded processor 31325ba7ba1dSCédric Le Goater */ 31335ba7ba1dSCédric Le Goater void helper_book3s_msgsndp(CPUPPCState *env, target_ulong rb) 31345ba7ba1dSCédric Le Goater { 3135d24e80b2SNicholas Piggin CPUState *cs = env_cpu(env); 3136d24e80b2SNicholas Piggin PowerPCCPU *cpu = POWERPC_CPU(cs); 3137d24e80b2SNicholas Piggin CPUState *ccs; 3138d24e80b2SNicholas Piggin uint32_t nr_threads = cs->nr_threads; 3139d24e80b2SNicholas Piggin int ttir = rb & PPC_BITMASK(57, 63); 31405ba7ba1dSCédric Le Goater 3141493028d8SCédric Le Goater helper_hfscr_facility_check(env, HFSCR_MSGP, "msgsndp", HFSCR_IC_MSGP); 3142493028d8SCédric Le Goater 31433401ea3cSNicholas Piggin if (!(env->flags & POWERPC_FLAG_SMT_1LPAR)) { 31443401ea3cSNicholas Piggin nr_threads = 1; /* msgsndp behaves as 1-thread in LPAR-per-thread mode*/ 31453401ea3cSNicholas Piggin } 31463401ea3cSNicholas Piggin 3147d24e80b2SNicholas Piggin if (!dbell_type_server(rb) || ttir >= nr_threads) { 31485ba7ba1dSCédric Le Goater return; 31495ba7ba1dSCédric Le Goater } 31505ba7ba1dSCédric Le Goater 3151d24e80b2SNicholas Piggin if (nr_threads == 1) { 3152d24e80b2SNicholas Piggin ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, 1); 3153d24e80b2SNicholas Piggin return; 3154d24e80b2SNicholas Piggin } 31555ba7ba1dSCédric Le Goater 3156d24e80b2SNicholas Piggin /* Does iothread need to be locked for walking CPU list? */ 3157d24e80b2SNicholas Piggin qemu_mutex_lock_iothread(); 3158d24e80b2SNicholas Piggin THREAD_SIBLING_FOREACH(cs, ccs) { 3159d24e80b2SNicholas Piggin PowerPCCPU *ccpu = POWERPC_CPU(ccs); 3160d24e80b2SNicholas Piggin uint32_t thread_id = ppc_cpu_tir(ccpu); 3161d24e80b2SNicholas Piggin 3162d24e80b2SNicholas Piggin if (ttir == thread_id) { 3163d24e80b2SNicholas Piggin ppc_set_irq(ccpu, PPC_INTERRUPT_DOORBELL, 1); 3164d24e80b2SNicholas Piggin qemu_mutex_unlock_iothread(); 3165d24e80b2SNicholas Piggin return; 3166d24e80b2SNicholas Piggin } 3167d24e80b2SNicholas Piggin } 3168d24e80b2SNicholas Piggin 3169d24e80b2SNicholas Piggin g_assert_not_reached(); 31705ba7ba1dSCédric Le Goater } 3171996473e4SRichard Henderson #endif /* TARGET_PPC64 */ 31720f3110faSRichard Henderson 317314895384SNicholas Piggin /* Single-step tracing */ 317414895384SNicholas Piggin void helper_book3s_trace(CPUPPCState *env, target_ulong prev_ip) 317514895384SNicholas Piggin { 317614895384SNicholas Piggin uint32_t error_code = 0; 317714895384SNicholas Piggin if (env->insns_flags2 & PPC2_ISA207S) { 317814895384SNicholas Piggin /* Load/store reporting, SRR1[35, 36] and SDAR, are not implemented. */ 317914895384SNicholas Piggin env->spr[SPR_POWER_SIAR] = prev_ip; 318014895384SNicholas Piggin error_code = PPC_BIT(33); 318114895384SNicholas Piggin } 318214895384SNicholas Piggin raise_exception_err(env, POWERPC_EXCP_TRACE, error_code); 318314895384SNicholas Piggin } 318414895384SNicholas Piggin 31850f3110faSRichard Henderson void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr, 31860f3110faSRichard Henderson MMUAccessType access_type, 31870f3110faSRichard Henderson int mmu_idx, uintptr_t retaddr) 31880f3110faSRichard Henderson { 31890f3110faSRichard Henderson CPUPPCState *env = cs->env_ptr; 319029c4a336SFabiano Rosas uint32_t insn; 319129c4a336SFabiano Rosas 319229c4a336SFabiano Rosas /* Restore state and reload the insn we executed, for filling in DSISR. */ 31933d419a4dSRichard Henderson cpu_restore_state(cs, retaddr); 3194888050cfSNicholas Piggin insn = ppc_ldl_code(env, env->nip); 31950f3110faSRichard Henderson 3196a7e3af13SRichard Henderson switch (env->mmu_model) { 3197a7e3af13SRichard Henderson case POWERPC_MMU_SOFT_4xx: 3198a7e3af13SRichard Henderson env->spr[SPR_40x_DEAR] = vaddr; 3199a7e3af13SRichard Henderson break; 3200a7e3af13SRichard Henderson case POWERPC_MMU_BOOKE: 3201a7e3af13SRichard Henderson case POWERPC_MMU_BOOKE206: 3202a7e3af13SRichard Henderson env->spr[SPR_BOOKE_DEAR] = vaddr; 3203a7e3af13SRichard Henderson break; 3204a7e3af13SRichard Henderson default: 3205a7e3af13SRichard Henderson env->spr[SPR_DAR] = vaddr; 3206a7e3af13SRichard Henderson break; 3207a7e3af13SRichard Henderson } 3208a7e3af13SRichard Henderson 32090f3110faSRichard Henderson cs->exception_index = POWERPC_EXCP_ALIGN; 321029c4a336SFabiano Rosas env->error_code = insn & 0x03FF0000; 321129c4a336SFabiano Rosas cpu_loop_exit(cs); 32120f3110faSRichard Henderson } 321355a7fa34SNicholas Piggin 321455a7fa34SNicholas Piggin void ppc_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr, 321555a7fa34SNicholas Piggin vaddr vaddr, unsigned size, 321655a7fa34SNicholas Piggin MMUAccessType access_type, 321755a7fa34SNicholas Piggin int mmu_idx, MemTxAttrs attrs, 321855a7fa34SNicholas Piggin MemTxResult response, uintptr_t retaddr) 321955a7fa34SNicholas Piggin { 322055a7fa34SNicholas Piggin CPUPPCState *env = cs->env_ptr; 322155a7fa34SNicholas Piggin 322255a7fa34SNicholas Piggin switch (env->excp_model) { 322355a7fa34SNicholas Piggin #if defined(TARGET_PPC64) 322455a7fa34SNicholas Piggin case POWERPC_EXCP_POWER9: 322555a7fa34SNicholas Piggin case POWERPC_EXCP_POWER10: 322655a7fa34SNicholas Piggin /* 322755a7fa34SNicholas Piggin * Machine check codes can be found in processor User Manual or 322855a7fa34SNicholas Piggin * Linux or skiboot source. 322955a7fa34SNicholas Piggin */ 323055a7fa34SNicholas Piggin if (access_type == MMU_DATA_LOAD) { 323155a7fa34SNicholas Piggin env->spr[SPR_DAR] = vaddr; 323255a7fa34SNicholas Piggin env->spr[SPR_DSISR] = PPC_BIT(57); 323355a7fa34SNicholas Piggin env->error_code = PPC_BIT(42); 323455a7fa34SNicholas Piggin 323555a7fa34SNicholas Piggin } else if (access_type == MMU_DATA_STORE) { 323655a7fa34SNicholas Piggin /* 323755a7fa34SNicholas Piggin * MCE for stores in POWER is asynchronous so hardware does 323855a7fa34SNicholas Piggin * not set DAR, but QEMU can do better. 323955a7fa34SNicholas Piggin */ 324055a7fa34SNicholas Piggin env->spr[SPR_DAR] = vaddr; 324155a7fa34SNicholas Piggin env->error_code = PPC_BIT(36) | PPC_BIT(43) | PPC_BIT(45); 324255a7fa34SNicholas Piggin env->error_code |= PPC_BIT(42); 324355a7fa34SNicholas Piggin 324455a7fa34SNicholas Piggin } else { /* Fetch */ 324555a7fa34SNicholas Piggin env->error_code = PPC_BIT(36) | PPC_BIT(44) | PPC_BIT(45); 324655a7fa34SNicholas Piggin } 324755a7fa34SNicholas Piggin break; 324855a7fa34SNicholas Piggin #endif 324955a7fa34SNicholas Piggin default: 325055a7fa34SNicholas Piggin /* 325155a7fa34SNicholas Piggin * TODO: Check behaviour for other CPUs, for now do nothing. 325255a7fa34SNicholas Piggin * Could add a basic MCE even if real hardware ignores. 325355a7fa34SNicholas Piggin */ 325455a7fa34SNicholas Piggin return; 325555a7fa34SNicholas Piggin } 325655a7fa34SNicholas Piggin 325755a7fa34SNicholas Piggin cs->exception_index = POWERPC_EXCP_MCHECK; 325855a7fa34SNicholas Piggin cpu_loop_exit_restore(cs, retaddr); 325955a7fa34SNicholas Piggin } 326014192307SNicholas Piggin 326114192307SNicholas Piggin void ppc_cpu_debug_excp_handler(CPUState *cs) 326214192307SNicholas Piggin { 326314192307SNicholas Piggin #if defined(TARGET_PPC64) 326414192307SNicholas Piggin CPUPPCState *env = cs->env_ptr; 326514192307SNicholas Piggin 326614192307SNicholas Piggin if (env->insns_flags2 & PPC2_ISA207S) { 3267*d5ee641cSNicholas Piggin if (cs->watchpoint_hit) { 3268*d5ee641cSNicholas Piggin if (cs->watchpoint_hit->flags & BP_CPU) { 3269*d5ee641cSNicholas Piggin env->spr[SPR_DAR] = cs->watchpoint_hit->hitaddr; 3270*d5ee641cSNicholas Piggin env->spr[SPR_DSISR] = PPC_BIT(41); 3271*d5ee641cSNicholas Piggin cs->watchpoint_hit = NULL; 3272*d5ee641cSNicholas Piggin raise_exception(env, POWERPC_EXCP_DSI); 3273*d5ee641cSNicholas Piggin } 3274*d5ee641cSNicholas Piggin cs->watchpoint_hit = NULL; 3275*d5ee641cSNicholas Piggin } else if (cpu_breakpoint_test(cs, env->nip, BP_CPU)) { 327614192307SNicholas Piggin raise_exception_err(env, POWERPC_EXCP_TRACE, 327714192307SNicholas Piggin PPC_BIT(33) | PPC_BIT(43)); 327814192307SNicholas Piggin } 327914192307SNicholas Piggin } 328014192307SNicholas Piggin #endif 328114192307SNicholas Piggin } 328214192307SNicholas Piggin 328314192307SNicholas Piggin bool ppc_cpu_debug_check_breakpoint(CPUState *cs) 328414192307SNicholas Piggin { 328514192307SNicholas Piggin #if defined(TARGET_PPC64) 328614192307SNicholas Piggin CPUPPCState *env = cs->env_ptr; 328714192307SNicholas Piggin 328814192307SNicholas Piggin if (env->insns_flags2 & PPC2_ISA207S) { 328914192307SNicholas Piggin target_ulong priv; 329014192307SNicholas Piggin 329114192307SNicholas Piggin priv = env->spr[SPR_CIABR] & PPC_BITMASK(62, 63); 329214192307SNicholas Piggin switch (priv) { 329314192307SNicholas Piggin case 0x1: /* problem */ 329414192307SNicholas Piggin return env->msr & ((target_ulong)1 << MSR_PR); 329514192307SNicholas Piggin case 0x2: /* supervisor */ 329614192307SNicholas Piggin return (!(env->msr & ((target_ulong)1 << MSR_PR)) && 329714192307SNicholas Piggin !(env->msr & ((target_ulong)1 << MSR_HV))); 329814192307SNicholas Piggin case 0x3: /* hypervisor */ 329914192307SNicholas Piggin return (!(env->msr & ((target_ulong)1 << MSR_PR)) && 330014192307SNicholas Piggin (env->msr & ((target_ulong)1 << MSR_HV))); 330114192307SNicholas Piggin default: 330214192307SNicholas Piggin g_assert_not_reached(); 330314192307SNicholas Piggin } 330414192307SNicholas Piggin } 330514192307SNicholas Piggin #endif 330614192307SNicholas Piggin 330714192307SNicholas Piggin return false; 330814192307SNicholas Piggin } 330914192307SNicholas Piggin 3310*d5ee641cSNicholas Piggin bool ppc_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp) 3311*d5ee641cSNicholas Piggin { 3312*d5ee641cSNicholas Piggin #if defined(TARGET_PPC64) 3313*d5ee641cSNicholas Piggin CPUPPCState *env = cs->env_ptr; 3314*d5ee641cSNicholas Piggin 3315*d5ee641cSNicholas Piggin if (env->insns_flags2 & PPC2_ISA207S) { 3316*d5ee641cSNicholas Piggin if (wp == env->dawr0_watchpoint) { 3317*d5ee641cSNicholas Piggin uint32_t dawrx = env->spr[SPR_DAWRX0]; 3318*d5ee641cSNicholas Piggin bool wt = extract32(dawrx, PPC_BIT_NR(59), 1); 3319*d5ee641cSNicholas Piggin bool wti = extract32(dawrx, PPC_BIT_NR(60), 1); 3320*d5ee641cSNicholas Piggin bool hv = extract32(dawrx, PPC_BIT_NR(61), 1); 3321*d5ee641cSNicholas Piggin bool sv = extract32(dawrx, PPC_BIT_NR(62), 1); 3322*d5ee641cSNicholas Piggin bool pr = extract32(dawrx, PPC_BIT_NR(62), 1); 3323*d5ee641cSNicholas Piggin 3324*d5ee641cSNicholas Piggin if ((env->msr & ((target_ulong)1 << MSR_PR)) && !pr) { 3325*d5ee641cSNicholas Piggin return false; 3326*d5ee641cSNicholas Piggin } else if ((env->msr & ((target_ulong)1 << MSR_HV)) && !hv) { 3327*d5ee641cSNicholas Piggin return false; 3328*d5ee641cSNicholas Piggin } else if (!sv) { 3329*d5ee641cSNicholas Piggin return false; 3330*d5ee641cSNicholas Piggin } 3331*d5ee641cSNicholas Piggin 3332*d5ee641cSNicholas Piggin if (!wti) { 3333*d5ee641cSNicholas Piggin if (env->msr & ((target_ulong)1 << MSR_DR)) { 3334*d5ee641cSNicholas Piggin if (!wt) { 3335*d5ee641cSNicholas Piggin return false; 3336*d5ee641cSNicholas Piggin } 3337*d5ee641cSNicholas Piggin } else { 3338*d5ee641cSNicholas Piggin if (wt) { 3339*d5ee641cSNicholas Piggin return false; 3340*d5ee641cSNicholas Piggin } 3341*d5ee641cSNicholas Piggin } 3342*d5ee641cSNicholas Piggin } 3343*d5ee641cSNicholas Piggin 3344*d5ee641cSNicholas Piggin return true; 3345*d5ee641cSNicholas Piggin } 3346*d5ee641cSNicholas Piggin } 3347*d5ee641cSNicholas Piggin #endif 3348*d5ee641cSNicholas Piggin 3349*d5ee641cSNicholas Piggin return false; 3350*d5ee641cSNicholas Piggin } 3351*d5ee641cSNicholas Piggin 3352996473e4SRichard Henderson #endif /* CONFIG_TCG */ 3353996473e4SRichard Henderson #endif /* !CONFIG_USER_ONLY */ 3354