xref: /qemu/target/ppc/excp_helper.c (revision 7cef6d686309e2792186504ae17cf4f3eb57ef68)
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"
22*342e313dSPierrick Bouvier #include "system/memory.h"
2330de74bdSPhilippe Mathieu-Daudé #include "system/tcg.h"
2432cad1ffSPhilippe Mathieu-Daudé #include "system/system.h"
2532cad1ffSPhilippe Mathieu-Daudé #include "system/runstate.h"
26ad71ed68SBlue Swirl #include "cpu.h"
270f3110faSRichard Henderson #include "internal.h"
28ad71ed68SBlue Swirl #include "helper_regs.h"
297b694df6SMatheus Ferst #include "hw/ppc/ppc.h"
30ad71ed68SBlue Swirl 
312eb1ef73SCédric Le Goater #include "trace.h"
322eb1ef73SCédric Le Goater 
33c79c73f6SBlue Swirl /*****************************************************************************/
34c79c73f6SBlue Swirl /* Exception processing */
35f6c2d68bSBALATON Zoltan #ifndef CONFIG_USER_ONLY
3697a8ea5aSAndreas Färber 
powerpc_excp_name(int excp)376789f23bSCédric Le Goater static const char *powerpc_excp_name(int excp)
386789f23bSCédric Le Goater {
396789f23bSCédric Le Goater     switch (excp) {
406789f23bSCédric Le Goater     case POWERPC_EXCP_CRITICAL: return "CRITICAL";
416789f23bSCédric Le Goater     case POWERPC_EXCP_MCHECK:   return "MCHECK";
426789f23bSCédric Le Goater     case POWERPC_EXCP_DSI:      return "DSI";
436789f23bSCédric Le Goater     case POWERPC_EXCP_ISI:      return "ISI";
446789f23bSCédric Le Goater     case POWERPC_EXCP_EXTERNAL: return "EXTERNAL";
456789f23bSCédric Le Goater     case POWERPC_EXCP_ALIGN:    return "ALIGN";
466789f23bSCédric Le Goater     case POWERPC_EXCP_PROGRAM:  return "PROGRAM";
476789f23bSCédric Le Goater     case POWERPC_EXCP_FPU:      return "FPU";
486789f23bSCédric Le Goater     case POWERPC_EXCP_SYSCALL:  return "SYSCALL";
496789f23bSCédric Le Goater     case POWERPC_EXCP_APU:      return "APU";
506789f23bSCédric Le Goater     case POWERPC_EXCP_DECR:     return "DECR";
516789f23bSCédric Le Goater     case POWERPC_EXCP_FIT:      return "FIT";
526789f23bSCédric Le Goater     case POWERPC_EXCP_WDT:      return "WDT";
536789f23bSCédric Le Goater     case POWERPC_EXCP_DTLB:     return "DTLB";
546789f23bSCédric Le Goater     case POWERPC_EXCP_ITLB:     return "ITLB";
556789f23bSCédric Le Goater     case POWERPC_EXCP_DEBUG:    return "DEBUG";
566789f23bSCédric Le Goater     case POWERPC_EXCP_SPEU:     return "SPEU";
576789f23bSCédric Le Goater     case POWERPC_EXCP_EFPDI:    return "EFPDI";
586789f23bSCédric Le Goater     case POWERPC_EXCP_EFPRI:    return "EFPRI";
596789f23bSCédric Le Goater     case POWERPC_EXCP_EPERFM:   return "EPERFM";
606789f23bSCédric Le Goater     case POWERPC_EXCP_DOORI:    return "DOORI";
616789f23bSCédric Le Goater     case POWERPC_EXCP_DOORCI:   return "DOORCI";
626789f23bSCédric Le Goater     case POWERPC_EXCP_GDOORI:   return "GDOORI";
636789f23bSCédric Le Goater     case POWERPC_EXCP_GDOORCI:  return "GDOORCI";
646789f23bSCédric Le Goater     case POWERPC_EXCP_HYPPRIV:  return "HYPPRIV";
656789f23bSCédric Le Goater     case POWERPC_EXCP_RESET:    return "RESET";
666789f23bSCédric Le Goater     case POWERPC_EXCP_DSEG:     return "DSEG";
676789f23bSCédric Le Goater     case POWERPC_EXCP_ISEG:     return "ISEG";
686789f23bSCédric Le Goater     case POWERPC_EXCP_HDECR:    return "HDECR";
696789f23bSCédric Le Goater     case POWERPC_EXCP_TRACE:    return "TRACE";
706789f23bSCédric Le Goater     case POWERPC_EXCP_HDSI:     return "HDSI";
716789f23bSCédric Le Goater     case POWERPC_EXCP_HISI:     return "HISI";
726789f23bSCédric Le Goater     case POWERPC_EXCP_HDSEG:    return "HDSEG";
736789f23bSCédric Le Goater     case POWERPC_EXCP_HISEG:    return "HISEG";
746789f23bSCédric Le Goater     case POWERPC_EXCP_VPU:      return "VPU";
756789f23bSCédric Le Goater     case POWERPC_EXCP_PIT:      return "PIT";
766789f23bSCédric Le Goater     case POWERPC_EXCP_EMUL:     return "EMUL";
776789f23bSCédric Le Goater     case POWERPC_EXCP_IFTLB:    return "IFTLB";
786789f23bSCédric Le Goater     case POWERPC_EXCP_DLTLB:    return "DLTLB";
796789f23bSCédric Le Goater     case POWERPC_EXCP_DSTLB:    return "DSTLB";
806789f23bSCédric Le Goater     case POWERPC_EXCP_FPA:      return "FPA";
816789f23bSCédric Le Goater     case POWERPC_EXCP_DABR:     return "DABR";
826789f23bSCédric Le Goater     case POWERPC_EXCP_IABR:     return "IABR";
836789f23bSCédric Le Goater     case POWERPC_EXCP_SMI:      return "SMI";
846789f23bSCédric Le Goater     case POWERPC_EXCP_PERFM:    return "PERFM";
856789f23bSCédric Le Goater     case POWERPC_EXCP_THERM:    return "THERM";
866789f23bSCédric Le Goater     case POWERPC_EXCP_VPUA:     return "VPUA";
876789f23bSCédric Le Goater     case POWERPC_EXCP_SOFTP:    return "SOFTP";
886789f23bSCédric Le Goater     case POWERPC_EXCP_MAINT:    return "MAINT";
896789f23bSCédric Le Goater     case POWERPC_EXCP_MEXTBR:   return "MEXTBR";
906789f23bSCédric Le Goater     case POWERPC_EXCP_NMEXTBR:  return "NMEXTBR";
916789f23bSCédric Le Goater     case POWERPC_EXCP_ITLBE:    return "ITLBE";
926789f23bSCédric Le Goater     case POWERPC_EXCP_DTLBE:    return "DTLBE";
936789f23bSCédric Le Goater     case POWERPC_EXCP_VSXU:     return "VSXU";
946789f23bSCédric Le Goater     case POWERPC_EXCP_FU:       return "FU";
956789f23bSCédric Le Goater     case POWERPC_EXCP_HV_EMU:   return "HV_EMU";
966789f23bSCédric Le Goater     case POWERPC_EXCP_HV_MAINT: return "HV_MAINT";
976789f23bSCédric Le Goater     case POWERPC_EXCP_HV_FU:    return "HV_FU";
986789f23bSCédric Le Goater     case POWERPC_EXCP_SDOOR:    return "SDOOR";
996789f23bSCédric Le Goater     case POWERPC_EXCP_SDOOR_HV: return "SDOOR_HV";
1006789f23bSCédric Le Goater     case POWERPC_EXCP_HVIRT:    return "HVIRT";
1016789f23bSCédric Le Goater     case POWERPC_EXCP_SYSCALL_VECTORED: return "SYSCALL_VECTORED";
1026789f23bSCédric Le Goater     default:
1036789f23bSCédric Le Goater         g_assert_not_reached();
1046789f23bSCédric Le Goater     }
1056789f23bSCédric Le Goater }
1066789f23bSCédric Le Goater 
dump_syscall(CPUPPCState * env)10762e79ef9SCédric Le Goater static void dump_syscall(CPUPPCState *env)
108c79c73f6SBlue Swirl {
1096dc6b557SNicholas Piggin     qemu_log_mask(CPU_LOG_INT, "syscall r0=%016" PRIx64
1106dc6b557SNicholas Piggin                   " r3=%016" PRIx64 " r4=%016" PRIx64 " r5=%016" PRIx64
1116dc6b557SNicholas Piggin                   " r6=%016" PRIx64 " r7=%016" PRIx64 " r8=%016" PRIx64
112c79c73f6SBlue Swirl                   " nip=" TARGET_FMT_lx "\n",
113c79c73f6SBlue Swirl                   ppc_dump_gpr(env, 0), ppc_dump_gpr(env, 3),
114c79c73f6SBlue Swirl                   ppc_dump_gpr(env, 4), ppc_dump_gpr(env, 5),
1156dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 6), ppc_dump_gpr(env, 7),
1166dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 8), env->nip);
1176dc6b557SNicholas Piggin }
1186dc6b557SNicholas Piggin 
dump_hcall(CPUPPCState * env)11962e79ef9SCédric Le Goater static void dump_hcall(CPUPPCState *env)
1206dc6b557SNicholas Piggin {
1216dc6b557SNicholas Piggin     qemu_log_mask(CPU_LOG_INT, "hypercall r3=%016" PRIx64
1226dc6b557SNicholas Piggin                   " r4=%016" PRIx64 " r5=%016" PRIx64 " r6=%016" PRIx64
1236dc6b557SNicholas Piggin                   " r7=%016" PRIx64 " r8=%016" PRIx64 " r9=%016" PRIx64
1246dc6b557SNicholas Piggin                   " r10=%016" PRIx64 " r11=%016" PRIx64 " r12=%016" PRIx64
1256dc6b557SNicholas Piggin                   " nip=" TARGET_FMT_lx "\n",
1266dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 3), ppc_dump_gpr(env, 4),
1276dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 5), ppc_dump_gpr(env, 6),
1286dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 7), ppc_dump_gpr(env, 8),
1296dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 9), ppc_dump_gpr(env, 10),
1306dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 11), ppc_dump_gpr(env, 12),
1316dc6b557SNicholas Piggin                   env->nip);
132c79c73f6SBlue Swirl }
133c79c73f6SBlue Swirl 
ppc_excp_debug_sw_tlb(CPUPPCState * env,int excp)134e4e27df7SFabiano Rosas static void ppc_excp_debug_sw_tlb(CPUPPCState *env, int excp)
135e4e27df7SFabiano Rosas {
136e4e27df7SFabiano Rosas     const char *es;
137e4e27df7SFabiano Rosas     target_ulong *miss, *cmp;
138e4e27df7SFabiano Rosas     int en;
139e4e27df7SFabiano Rosas 
1402e089eceSFabiano Rosas     if (!qemu_loglevel_mask(CPU_LOG_MMU)) {
141e4e27df7SFabiano Rosas         return;
142e4e27df7SFabiano Rosas     }
143e4e27df7SFabiano Rosas 
144e4e27df7SFabiano Rosas     if (excp == POWERPC_EXCP_IFTLB) {
145e4e27df7SFabiano Rosas         es = "I";
146e4e27df7SFabiano Rosas         en = 'I';
147e4e27df7SFabiano Rosas         miss = &env->spr[SPR_IMISS];
148e4e27df7SFabiano Rosas         cmp = &env->spr[SPR_ICMP];
149e4e27df7SFabiano Rosas     } else {
150e4e27df7SFabiano Rosas         if (excp == POWERPC_EXCP_DLTLB) {
151e4e27df7SFabiano Rosas             es = "DL";
152e4e27df7SFabiano Rosas         } else {
153e4e27df7SFabiano Rosas             es = "DS";
154e4e27df7SFabiano Rosas         }
155e4e27df7SFabiano Rosas         en = 'D';
156e4e27df7SFabiano Rosas         miss = &env->spr[SPR_DMISS];
157e4e27df7SFabiano Rosas         cmp = &env->spr[SPR_DCMP];
158e4e27df7SFabiano Rosas     }
159e4e27df7SFabiano Rosas     qemu_log("6xx %sTLB miss: %cM " TARGET_FMT_lx " %cC "
160e4e27df7SFabiano Rosas              TARGET_FMT_lx " H1 " TARGET_FMT_lx " H2 "
161e4e27df7SFabiano Rosas              TARGET_FMT_lx " %08x\n", es, en, *miss, en, *cmp,
162e4e27df7SFabiano Rosas              env->spr[SPR_HASH1], env->spr[SPR_HASH2],
163e4e27df7SFabiano Rosas              env->error_code);
164e4e27df7SFabiano Rosas }
165e4e27df7SFabiano Rosas 
166f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
powerpc_reset_wakeup(CPUPPCState * env,int excp,target_ulong * msr)1673f88a89dSBALATON Zoltan static int powerpc_reset_wakeup(CPUPPCState *env, int excp, target_ulong *msr)
168dead760bSBenjamin Herrenschmidt {
169dead760bSBenjamin Herrenschmidt     /* We no longer are in a PM state */
1701e7fd61dSBenjamin Herrenschmidt     env->resume_as_sreset = false;
171dead760bSBenjamin Herrenschmidt 
172dead760bSBenjamin Herrenschmidt     /* Pretend to be returning from doze always as we don't lose state */
1730911a60cSLeonardo Bras     *msr |= SRR1_WS_NOLOSS;
174dead760bSBenjamin Herrenschmidt 
175dead760bSBenjamin Herrenschmidt     /* Machine checks are sent normally */
176dead760bSBenjamin Herrenschmidt     if (excp == POWERPC_EXCP_MCHECK) {
177dead760bSBenjamin Herrenschmidt         return excp;
178dead760bSBenjamin Herrenschmidt     }
179dead760bSBenjamin Herrenschmidt     switch (excp) {
180dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_RESET:
1810911a60cSLeonardo Bras         *msr |= SRR1_WAKERESET;
182dead760bSBenjamin Herrenschmidt         break;
183dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_EXTERNAL:
1840911a60cSLeonardo Bras         *msr |= SRR1_WAKEEE;
185dead760bSBenjamin Herrenschmidt         break;
186dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_DECR:
1870911a60cSLeonardo Bras         *msr |= SRR1_WAKEDEC;
188dead760bSBenjamin Herrenschmidt         break;
189dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_SDOOR:
1900911a60cSLeonardo Bras         *msr |= SRR1_WAKEDBELL;
191dead760bSBenjamin Herrenschmidt         break;
192dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_SDOOR_HV:
1930911a60cSLeonardo Bras         *msr |= SRR1_WAKEHDBELL;
194dead760bSBenjamin Herrenschmidt         break;
195dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_HV_MAINT:
1960911a60cSLeonardo Bras         *msr |= SRR1_WAKEHMI;
197dead760bSBenjamin Herrenschmidt         break;
198d8ce5fd6SBenjamin Herrenschmidt     case POWERPC_EXCP_HVIRT:
1990911a60cSLeonardo Bras         *msr |= SRR1_WAKEHVI;
200d8ce5fd6SBenjamin Herrenschmidt         break;
201dead760bSBenjamin Herrenschmidt     default:
2023f88a89dSBALATON Zoltan         cpu_abort(env_cpu(env),
2033f88a89dSBALATON Zoltan                   "Unsupported exception %d in Power Save mode\n", excp);
204dead760bSBenjamin Herrenschmidt     }
205dead760bSBenjamin Herrenschmidt     return POWERPC_EXCP_RESET;
206dead760bSBenjamin Herrenschmidt }
207dead760bSBenjamin Herrenschmidt 
2088b7e6b07SNicholas Piggin /*
2098b7e6b07SNicholas Piggin  * AIL - Alternate Interrupt Location, a mode that allows interrupts to be
2108b7e6b07SNicholas Piggin  * taken with the MMU on, and which uses an alternate location (e.g., so the
2118b7e6b07SNicholas Piggin  * kernel/hv can map the vectors there with an effective address).
2128b7e6b07SNicholas Piggin  *
2138b7e6b07SNicholas Piggin  * An interrupt is considered to be taken "with AIL" or "AIL applies" if they
2148b7e6b07SNicholas Piggin  * are delivered in this way. AIL requires the LPCR to be set to enable this
2158b7e6b07SNicholas Piggin  * mode, and then a number of conditions have to be true for AIL to apply.
2168b7e6b07SNicholas Piggin  *
2178b7e6b07SNicholas Piggin  * First of all, SRESET, MCE, and HMI are always delivered without AIL, because
2188b7e6b07SNicholas Piggin  * they specifically want to be in real mode (e.g., the MCE might be signaling
2198b7e6b07SNicholas Piggin  * a SLB multi-hit which requires SLB flush before the MMU can be enabled).
2208b7e6b07SNicholas Piggin  *
2218b7e6b07SNicholas Piggin  * After that, behaviour depends on the current MSR[IR], MSR[DR], MSR[HV],
2228b7e6b07SNicholas Piggin  * whether or not the interrupt changes MSR[HV] from 0 to 1, and the current
2238b7e6b07SNicholas Piggin  * radix mode (LPCR[HR]).
2248b7e6b07SNicholas Piggin  *
2258b7e6b07SNicholas Piggin  * POWER8, POWER9 with LPCR[HR]=0
2268b7e6b07SNicholas Piggin  * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
2278b7e6b07SNicholas Piggin  * +-----------+-------------+---------+-------------+-----+
2288b7e6b07SNicholas Piggin  * | a         | 00/01/10    | x       | x           | 0   |
2298b7e6b07SNicholas Piggin  * | a         | 11          | 0       | 1           | 0   |
2308b7e6b07SNicholas Piggin  * | a         | 11          | 1       | 1           | a   |
2318b7e6b07SNicholas Piggin  * | a         | 11          | 0       | 0           | a   |
2328b7e6b07SNicholas Piggin  * +-------------------------------------------------------+
2338b7e6b07SNicholas Piggin  *
2348b7e6b07SNicholas Piggin  * POWER9 with LPCR[HR]=1
2358b7e6b07SNicholas Piggin  * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
2368b7e6b07SNicholas Piggin  * +-----------+-------------+---------+-------------+-----+
2378b7e6b07SNicholas Piggin  * | a         | 00/01/10    | x       | x           | 0   |
2388b7e6b07SNicholas Piggin  * | a         | 11          | x       | x           | a   |
2398b7e6b07SNicholas Piggin  * +-------------------------------------------------------+
2408b7e6b07SNicholas Piggin  *
2418b7e6b07SNicholas Piggin  * The difference with POWER9 being that MSR[HV] 0->1 interrupts can be sent to
242526cdce7SNicholas Piggin  * the hypervisor in AIL mode if the guest is radix. This is good for
243526cdce7SNicholas Piggin  * performance but allows the guest to influence the AIL of hypervisor
244526cdce7SNicholas Piggin  * interrupts using its MSR, and also the hypervisor must disallow guest
245526cdce7SNicholas Piggin  * interrupts (MSR[HV] 0->0) from using AIL if the hypervisor does not want to
246526cdce7SNicholas Piggin  * use AIL for its MSR[HV] 0->1 interrupts.
247526cdce7SNicholas Piggin  *
248526cdce7SNicholas Piggin  * POWER10 addresses those issues with a new LPCR[HAIL] bit that is applied to
249526cdce7SNicholas Piggin  * interrupts that begin execution with MSR[HV]=1 (so both MSR[HV] 0->1 and
250526cdce7SNicholas Piggin  * MSR[HV] 1->1).
251526cdce7SNicholas Piggin  *
252526cdce7SNicholas Piggin  * HAIL=1 is equivalent to AIL=3, for interrupts delivered with MSR[HV]=1.
253526cdce7SNicholas Piggin  *
254526cdce7SNicholas Piggin  * POWER10 behaviour is
255526cdce7SNicholas Piggin  * | LPCR[AIL] | LPCR[HAIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
256526cdce7SNicholas Piggin  * +-----------+------------+-------------+---------+-------------+-----+
257526cdce7SNicholas Piggin  * | a         | h          | 00/01/10    | 0       | 0           | 0   |
258526cdce7SNicholas Piggin  * | a         | h          | 11          | 0       | 0           | a   |
259526cdce7SNicholas Piggin  * | a         | h          | x           | 0       | 1           | h   |
260526cdce7SNicholas Piggin  * | a         | h          | 00/01/10    | 1       | 1           | 0   |
261526cdce7SNicholas Piggin  * | a         | h          | 11          | 1       | 1           | h   |
262526cdce7SNicholas Piggin  * +--------------------------------------------------------------------+
2638b7e6b07SNicholas Piggin  */
ppc_excp_apply_ail(PowerPCCPU * cpu,int excp,target_ulong msr,target_ulong * new_msr,target_ulong * vector)26410895ab6SFabiano Rosas static void ppc_excp_apply_ail(PowerPCCPU *cpu, int excp, target_ulong msr,
26510895ab6SFabiano Rosas                                target_ulong *new_msr, target_ulong *vector)
2662586a4d7SFabiano Rosas {
26710895ab6SFabiano Rosas     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
2688b7e6b07SNicholas Piggin     CPUPPCState *env = &cpu->env;
2698b7e6b07SNicholas Piggin     bool mmu_all_on = ((msr >> MSR_IR) & 1) && ((msr >> MSR_DR) & 1);
2708b7e6b07SNicholas Piggin     bool hv_escalation = !(msr & MSR_HVB) && (*new_msr & MSR_HVB);
2718b7e6b07SNicholas Piggin     int ail = 0;
2722586a4d7SFabiano Rosas 
2738b7e6b07SNicholas Piggin     if (excp == POWERPC_EXCP_MCHECK ||
2748b7e6b07SNicholas Piggin         excp == POWERPC_EXCP_RESET ||
2758b7e6b07SNicholas Piggin         excp == POWERPC_EXCP_HV_MAINT) {
2768b7e6b07SNicholas Piggin         /* SRESET, MCE, HMI never apply AIL */
2778b7e6b07SNicholas Piggin         return;
2782586a4d7SFabiano Rosas     }
2792586a4d7SFabiano Rosas 
28010895ab6SFabiano Rosas     if (!(pcc->lpcr_mask & LPCR_AIL)) {
28110895ab6SFabiano Rosas         /* This CPU does not have AIL */
28210895ab6SFabiano Rosas         return;
28310895ab6SFabiano Rosas     }
28410895ab6SFabiano Rosas 
28510895ab6SFabiano Rosas     /* P8 & P9 */
28610895ab6SFabiano Rosas     if (!(pcc->lpcr_mask & LPCR_HAIL)) {
2878b7e6b07SNicholas Piggin         if (!mmu_all_on) {
2888b7e6b07SNicholas Piggin             /* AIL only works if MSR[IR] and MSR[DR] are both enabled. */
2898b7e6b07SNicholas Piggin             return;
2908b7e6b07SNicholas Piggin         }
2918b7e6b07SNicholas Piggin         if (hv_escalation && !(env->spr[SPR_LPCR] & LPCR_HR)) {
2928b7e6b07SNicholas Piggin             /*
2938b7e6b07SNicholas Piggin              * AIL does not work if there is a MSR[HV] 0->1 transition and the
2948b7e6b07SNicholas Piggin              * partition is in HPT mode. For radix guests, such interrupts are
2958b7e6b07SNicholas Piggin              * allowed to be delivered to the hypervisor in ail mode.
2968b7e6b07SNicholas Piggin              */
2978b7e6b07SNicholas Piggin             return;
2988b7e6b07SNicholas Piggin         }
2998b7e6b07SNicholas Piggin 
3008b7e6b07SNicholas Piggin         ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT;
3018bbf0cffSHarsh Prateek Bora         if (ail == 0 || ail == 1) {
3028b7e6b07SNicholas Piggin             /* AIL=1 is reserved, treat it like AIL=0 */
3038b7e6b07SNicholas Piggin             return;
3048b7e6b07SNicholas Piggin         }
305526cdce7SNicholas Piggin 
30610895ab6SFabiano Rosas     /* P10 and up */
30710895ab6SFabiano Rosas     } else {
308526cdce7SNicholas Piggin         if (!mmu_all_on && !hv_escalation) {
309526cdce7SNicholas Piggin             /*
310526cdce7SNicholas Piggin              * AIL works for HV interrupts even with guest MSR[IR/DR] disabled.
311526cdce7SNicholas Piggin              * Guest->guest and HV->HV interrupts do require MMU on.
312526cdce7SNicholas Piggin              */
313526cdce7SNicholas Piggin             return;
314526cdce7SNicholas Piggin         }
315526cdce7SNicholas Piggin 
316526cdce7SNicholas Piggin         if (*new_msr & MSR_HVB) {
317526cdce7SNicholas Piggin             if (!(env->spr[SPR_LPCR] & LPCR_HAIL)) {
318526cdce7SNicholas Piggin                 /* HV interrupts depend on LPCR[HAIL] */
319526cdce7SNicholas Piggin                 return;
320526cdce7SNicholas Piggin             }
321526cdce7SNicholas Piggin             ail = 3; /* HAIL=1 gives AIL=3 behaviour for HV interrupts */
322526cdce7SNicholas Piggin         } else {
323526cdce7SNicholas Piggin             ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT;
324526cdce7SNicholas Piggin         }
3258bbf0cffSHarsh Prateek Bora         if (ail == 0 || ail == 1 || ail == 2) {
326526cdce7SNicholas Piggin             /* AIL=1 and AIL=2 are reserved, treat them like AIL=0 */
327526cdce7SNicholas Piggin             return;
328526cdce7SNicholas Piggin         }
3298b7e6b07SNicholas Piggin     }
3308b7e6b07SNicholas Piggin 
3318b7e6b07SNicholas Piggin     /*
3328b7e6b07SNicholas Piggin      * AIL applies, so the new MSR gets IR and DR set, and an offset applied
3338b7e6b07SNicholas Piggin      * to the new IP.
3348b7e6b07SNicholas Piggin      */
3358b7e6b07SNicholas Piggin     *new_msr |= (1 << MSR_IR) | (1 << MSR_DR);
3368b7e6b07SNicholas Piggin 
3378b7e6b07SNicholas Piggin     if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
3388b7e6b07SNicholas Piggin         if (ail == 2) {
3398b7e6b07SNicholas Piggin             *vector |= 0x0000000000018000ull;
3408b7e6b07SNicholas Piggin         } else if (ail == 3) {
3418b7e6b07SNicholas Piggin             *vector |= 0xc000000000004000ull;
3428b7e6b07SNicholas Piggin         }
3438b7e6b07SNicholas Piggin     } else {
3448b7e6b07SNicholas Piggin         /*
3458b7e6b07SNicholas Piggin          * scv AIL is a little different. AIL=2 does not change the address,
3468b7e6b07SNicholas Piggin          * only the MSR. AIL=3 replaces the 0x17000 base with 0xc...3000.
3478b7e6b07SNicholas Piggin          */
3488b7e6b07SNicholas Piggin         if (ail == 3) {
3498b7e6b07SNicholas Piggin             *vector &= ~0x0000000000017000ull; /* Un-apply the base offset */
3508b7e6b07SNicholas Piggin             *vector |= 0xc000000000003000ull; /* Apply scv's AIL=3 offset */
3518b7e6b07SNicholas Piggin         }
3528b7e6b07SNicholas Piggin     }
3532586a4d7SFabiano Rosas }
354f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
355dead760bSBenjamin Herrenschmidt 
powerpc_reset_excp_state(PowerPCCPU * cpu)3563680e994SNicholas Piggin static void powerpc_reset_excp_state(PowerPCCPU *cpu)
357ad77c6caSNicholas Piggin {
358ad77c6caSNicholas Piggin     CPUState *cs = CPU(cpu);
359ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
360ad77c6caSNicholas Piggin 
3613680e994SNicholas Piggin     /* Reset exception state */
3623680e994SNicholas Piggin     cs->exception_index = POWERPC_EXCP_NONE;
3633680e994SNicholas Piggin     env->error_code = 0;
3643680e994SNicholas Piggin }
3653680e994SNicholas Piggin 
powerpc_set_excp_state(PowerPCCPU * cpu,target_ulong vector,target_ulong msr)3663680e994SNicholas Piggin static void powerpc_set_excp_state(PowerPCCPU *cpu, target_ulong vector,
3673680e994SNicholas Piggin                                    target_ulong msr)
3683680e994SNicholas Piggin {
3693680e994SNicholas Piggin     CPUPPCState *env = &cpu->env;
3703680e994SNicholas Piggin 
371fce9fbafSFabiano Rosas     assert((msr & env->msr_mask) == msr);
372fce9fbafSFabiano Rosas 
373ad77c6caSNicholas Piggin     /*
374ad77c6caSNicholas Piggin      * We don't use hreg_store_msr here as already have treated any
375ad77c6caSNicholas Piggin      * special case that could occur. Just store MSR and update hflags
376ad77c6caSNicholas Piggin      *
377ab452503SBALATON Zoltan      * Note: We *MUST* not use hreg_store_msr() as-is anyway because it will
378ab452503SBALATON Zoltan      * prevent setting of the HV bit which some exceptions might need to do.
379ad77c6caSNicholas Piggin      */
3803680e994SNicholas Piggin     env->nip = vector;
381fce9fbafSFabiano Rosas     env->msr = msr;
382ad77c6caSNicholas Piggin     hreg_compute_hflags(env);
3832fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
384ad77c6caSNicholas Piggin 
3853680e994SNicholas Piggin     powerpc_reset_excp_state(cpu);
386ad77c6caSNicholas Piggin 
387ad77c6caSNicholas Piggin     /*
388ad77c6caSNicholas Piggin      * Any interrupt is context synchronizing, check if TCG TLB needs
389ad77c6caSNicholas Piggin      * a delayed flush on ppc64
390ad77c6caSNicholas Piggin      */
391ad77c6caSNicholas Piggin     check_tlb_flush(env, false);
3923680e994SNicholas Piggin 
3933680e994SNicholas Piggin     /* Reset the reservation */
3943680e994SNicholas Piggin     env->reserve_addr = -1;
395ad77c6caSNicholas Piggin }
396ad77c6caSNicholas Piggin 
powerpc_mcheck_checkstop(CPUPPCState * env)39745693f94SNicholas Piggin static void powerpc_mcheck_checkstop(CPUPPCState *env)
39845693f94SNicholas Piggin {
39945693f94SNicholas Piggin     /* KVM guests always have MSR[ME] enabled */
40045693f94SNicholas Piggin     if (FIELD_EX64(env->msr, MSR, ME)) {
40145693f94SNicholas Piggin         return;
40245693f94SNicholas Piggin     }
40330de74bdSPhilippe Mathieu-Daudé     assert(tcg_enabled());
40445693f94SNicholas Piggin     powerpc_checkstop(env, "machine check with MSR[ME]=0");
405819b31b0SBALATON Zoltan }
406819b31b0SBALATON Zoltan 
powerpc_excp_40x(PowerPCCPU * cpu,int excp)407e808c2edSFabiano Rosas static void powerpc_excp_40x(PowerPCCPU *cpu, int excp)
408e808c2edSFabiano Rosas {
409e808c2edSFabiano Rosas     CPUPPCState *env = &cpu->env;
410e808c2edSFabiano Rosas     target_ulong msr, new_msr, vector;
411ab452503SBALATON Zoltan     int srr0 = SPR_SRR0, srr1 = SPR_SRR1;
412e808c2edSFabiano Rosas 
413e808c2edSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
414e808c2edSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
415e808c2edSFabiano Rosas 
416ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
417495fc7ffSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME));
418e808c2edSFabiano Rosas 
419ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
420495fc7ffSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
421e808c2edSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
422e808c2edSFabiano Rosas     }
423e808c2edSFabiano Rosas 
424e808c2edSFabiano Rosas     vector = env->excp_vectors[excp];
425e808c2edSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
426bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
427bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
428e808c2edSFabiano Rosas     }
429e808c2edSFabiano Rosas     vector |= env->excp_prefix;
430e808c2edSFabiano Rosas 
431e808c2edSFabiano Rosas     switch (excp) {
432e808c2edSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
433e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
434e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
435e808c2edSFabiano Rosas         break;
436e808c2edSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
437819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
438e808c2edSFabiano Rosas         /* machine check exceptions don't have ME set */
439e808c2edSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
440e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
441e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
442e808c2edSFabiano Rosas         break;
443e808c2edSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
444f9911e1eSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_40x_ESR], env->spr[SPR_40x_DEAR]);
445e808c2edSFabiano Rosas         break;
446e808c2edSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
447e808c2edSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
448e808c2edSFabiano Rosas         break;
449e808c2edSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
450e808c2edSFabiano Rosas         break;
451e808c2edSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
452e808c2edSFabiano Rosas         break;
453e808c2edSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
454e808c2edSFabiano Rosas         switch (env->error_code & ~0xF) {
455e808c2edSFabiano Rosas         case POWERPC_EXCP_FP:
456da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
457e808c2edSFabiano Rosas                 trace_ppc_excp_fp_ignore();
4583680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
459e808c2edSFabiano Rosas                 return;
460e808c2edSFabiano Rosas             }
46164e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_FP;
462e808c2edSFabiano Rosas             break;
463e808c2edSFabiano Rosas         case POWERPC_EXCP_INVAL:
464e808c2edSFabiano Rosas             trace_ppc_excp_inval(env->nip);
46564e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PIL;
466e808c2edSFabiano Rosas             break;
467e808c2edSFabiano Rosas         case POWERPC_EXCP_PRIV:
46864e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PPR;
469e808c2edSFabiano Rosas             break;
470e808c2edSFabiano Rosas         case POWERPC_EXCP_TRAP:
47164e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PTR;
472e808c2edSFabiano Rosas             break;
473e808c2edSFabiano Rosas         default:
474bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
475e808c2edSFabiano Rosas                       env->error_code);
476e808c2edSFabiano Rosas             break;
477e808c2edSFabiano Rosas         }
478e808c2edSFabiano Rosas         break;
479e808c2edSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
480e808c2edSFabiano Rosas         dump_syscall(env);
481e808c2edSFabiano Rosas 
482e808c2edSFabiano Rosas         /*
483e808c2edSFabiano Rosas          * We need to correct the NIP which in this case is supposed
484e808c2edSFabiano Rosas          * to point to the next instruction
485e808c2edSFabiano Rosas          */
486e808c2edSFabiano Rosas         env->nip += 4;
487e808c2edSFabiano Rosas         break;
488e808c2edSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
489e808c2edSFabiano Rosas         trace_ppc_excp_print("FIT");
490e808c2edSFabiano Rosas         break;
491e808c2edSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
492e808c2edSFabiano Rosas         trace_ppc_excp_print("WDT");
493e808c2edSFabiano Rosas         break;
494e808c2edSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
495e808c2edSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
496e808c2edSFabiano Rosas         break;
497e808c2edSFabiano Rosas     case POWERPC_EXCP_PIT:       /* Programmable interval timer interrupt    */
498e808c2edSFabiano Rosas         trace_ppc_excp_print("PIT");
499e808c2edSFabiano Rosas         break;
5004d8ac1d1SFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
501bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
5024d8ac1d1SFabiano Rosas                   powerpc_excp_name(excp));
5034d8ac1d1SFabiano Rosas         break;
504e808c2edSFabiano Rosas     default:
505bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
506bc30c1c6SBALATON Zoltan                   excp);
507e808c2edSFabiano Rosas         break;
508e808c2edSFabiano Rosas     }
509e808c2edSFabiano Rosas 
510e808c2edSFabiano Rosas     env->spr[srr0] = env->nip;
511e808c2edSFabiano Rosas     env->spr[srr1] = msr;
512e808c2edSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
513e808c2edSFabiano Rosas }
514e808c2edSFabiano Rosas 
powerpc_excp_6xx(PowerPCCPU * cpu,int excp)51558d178fbSFabiano Rosas static void powerpc_excp_6xx(PowerPCCPU *cpu, int excp)
51658d178fbSFabiano Rosas {
51758d178fbSFabiano Rosas     CPUPPCState *env = &cpu->env;
51858d178fbSFabiano Rosas     target_ulong msr, new_msr, vector;
51958d178fbSFabiano Rosas 
52058d178fbSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
52158d178fbSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
52258d178fbSFabiano Rosas 
523ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
524082d783bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
52558d178fbSFabiano Rosas 
526ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
527082d783bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
52858d178fbSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
52958d178fbSFabiano Rosas     }
53058d178fbSFabiano Rosas 
53158d178fbSFabiano Rosas     vector = env->excp_vectors[excp];
53258d178fbSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
533bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
534bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
53558d178fbSFabiano Rosas     }
53658d178fbSFabiano Rosas     vector |= env->excp_prefix;
53758d178fbSFabiano Rosas 
53858d178fbSFabiano Rosas     switch (excp) {
53958d178fbSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
54058d178fbSFabiano Rosas         break;
54158d178fbSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
542819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
54358d178fbSFabiano Rosas         /* machine check exceptions don't have ME set */
54458d178fbSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
54558d178fbSFabiano Rosas         break;
54658d178fbSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
54758d178fbSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
54858d178fbSFabiano Rosas         break;
54958d178fbSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
55058d178fbSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
55158d178fbSFabiano Rosas         msr |= env->error_code;
55258d178fbSFabiano Rosas         break;
55358d178fbSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
55458d178fbSFabiano Rosas         break;
55558d178fbSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
55658d178fbSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
55758d178fbSFabiano Rosas         /*
55858d178fbSFabiano Rosas          * Note: the opcode fields will not be set properly for a
55958d178fbSFabiano Rosas          * direct store load/store, but nobody cares as nobody
56058d178fbSFabiano Rosas          * actually uses direct store segments.
56158d178fbSFabiano Rosas          */
56258d178fbSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
56358d178fbSFabiano Rosas         break;
56458d178fbSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
56558d178fbSFabiano Rosas         switch (env->error_code & ~0xF) {
56658d178fbSFabiano Rosas         case POWERPC_EXCP_FP:
567da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
56858d178fbSFabiano Rosas                 trace_ppc_excp_fp_ignore();
5693680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
57058d178fbSFabiano Rosas                 return;
57158d178fbSFabiano Rosas             }
57258d178fbSFabiano Rosas             /*
573ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
574ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
57558d178fbSFabiano Rosas              */
57658d178fbSFabiano Rosas             msr |= 0x00100000;
57758d178fbSFabiano Rosas             break;
57858d178fbSFabiano Rosas         case POWERPC_EXCP_INVAL:
57958d178fbSFabiano Rosas             trace_ppc_excp_inval(env->nip);
58058d178fbSFabiano Rosas             msr |= 0x00080000;
58158d178fbSFabiano Rosas             break;
58258d178fbSFabiano Rosas         case POWERPC_EXCP_PRIV:
58358d178fbSFabiano Rosas             msr |= 0x00040000;
58458d178fbSFabiano Rosas             break;
58558d178fbSFabiano Rosas         case POWERPC_EXCP_TRAP:
58658d178fbSFabiano Rosas             msr |= 0x00020000;
58758d178fbSFabiano Rosas             break;
58858d178fbSFabiano Rosas         default:
58958d178fbSFabiano Rosas             /* Should never occur */
590bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
59158d178fbSFabiano Rosas                       env->error_code);
59258d178fbSFabiano Rosas             break;
59358d178fbSFabiano Rosas         }
59458d178fbSFabiano Rosas         break;
59558d178fbSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
59658d178fbSFabiano Rosas         dump_syscall(env);
59758d178fbSFabiano Rosas 
59858d178fbSFabiano Rosas         /*
59958d178fbSFabiano Rosas          * We need to correct the NIP which in this case is supposed
60058d178fbSFabiano Rosas          * to point to the next instruction
60158d178fbSFabiano Rosas          */
60258d178fbSFabiano Rosas         env->nip += 4;
60358d178fbSFabiano Rosas         break;
60458d178fbSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
60558d178fbSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
60658d178fbSFabiano Rosas         break;
60758d178fbSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
60858d178fbSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
60958d178fbSFabiano Rosas         break;
61058d178fbSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
6118e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
612bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
613bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset exception "
614bc30c1c6SBALATON Zoltan                       "%d with no HV support\n", excp);
61558d178fbSFabiano Rosas         }
61658d178fbSFabiano Rosas         break;
61758d178fbSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
61858d178fbSFabiano Rosas         break;
61958d178fbSFabiano Rosas     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
62058d178fbSFabiano Rosas     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
62158d178fbSFabiano Rosas     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
62258d178fbSFabiano Rosas         /* Swap temporary saved registers with GPRs */
62358d178fbSFabiano Rosas         if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
62458d178fbSFabiano Rosas             new_msr |= (target_ulong)1 << MSR_TGPR;
62558d178fbSFabiano Rosas             hreg_swap_gpr_tgpr(env);
62658d178fbSFabiano Rosas         }
6278f8c7932SFabiano Rosas 
62858d178fbSFabiano Rosas         ppc_excp_debug_sw_tlb(env, excp);
62958d178fbSFabiano Rosas 
63058d178fbSFabiano Rosas         msr |= env->crf[0] << 28;
63158d178fbSFabiano Rosas         msr |= env->error_code; /* key, D/I, S/L bits */
63258d178fbSFabiano Rosas         /* Set way using a LRU mechanism */
63358d178fbSFabiano Rosas         msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
63458d178fbSFabiano Rosas         break;
63558d178fbSFabiano Rosas     case POWERPC_EXCP_FPA:       /* Floating-point assist exception          */
63658d178fbSFabiano Rosas     case POWERPC_EXCP_DABR:      /* Data address breakpoint                  */
63758d178fbSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
63858d178fbSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
63958d178fbSFabiano Rosas     case POWERPC_EXCP_MEXTBR:    /* Maskable external breakpoint             */
64058d178fbSFabiano Rosas     case POWERPC_EXCP_NMEXTBR:   /* Non maskable external breakpoint         */
641bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
64258d178fbSFabiano Rosas                   powerpc_excp_name(excp));
64358d178fbSFabiano Rosas         break;
64458d178fbSFabiano Rosas     default:
645bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
646bc30c1c6SBALATON Zoltan                   excp);
64758d178fbSFabiano Rosas         break;
64858d178fbSFabiano Rosas     }
64958d178fbSFabiano Rosas 
65058d178fbSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
65158d178fbSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
65258d178fbSFabiano Rosas     }
653c50eaed1SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
654c50eaed1SFabiano Rosas     env->spr[SPR_SRR1] = msr;
65558d178fbSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
65658d178fbSFabiano Rosas }
65758d178fbSFabiano Rosas 
powerpc_excp_7xx(PowerPCCPU * cpu,int excp)658ccfca2fcSFabiano Rosas static void powerpc_excp_7xx(PowerPCCPU *cpu, int excp)
659ccfca2fcSFabiano Rosas {
660ccfca2fcSFabiano Rosas     CPUPPCState *env = &cpu->env;
661ccfca2fcSFabiano Rosas     target_ulong msr, new_msr, vector;
662ccfca2fcSFabiano Rosas 
663ccfca2fcSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
664ccfca2fcSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
665ccfca2fcSFabiano Rosas 
666ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
66793848d6aSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
668ccfca2fcSFabiano Rosas 
669ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
67093848d6aSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
671ccfca2fcSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
672ccfca2fcSFabiano Rosas     }
673ccfca2fcSFabiano Rosas 
674ccfca2fcSFabiano Rosas     vector = env->excp_vectors[excp];
675ccfca2fcSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
676bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
677bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
678ccfca2fcSFabiano Rosas     }
679ccfca2fcSFabiano Rosas     vector |= env->excp_prefix;
680ccfca2fcSFabiano Rosas 
681ccfca2fcSFabiano Rosas     switch (excp) {
682ccfca2fcSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
683819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
684ccfca2fcSFabiano Rosas         /* machine check exceptions don't have ME set */
685ccfca2fcSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
686ccfca2fcSFabiano Rosas         break;
687ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
688ccfca2fcSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
689ccfca2fcSFabiano Rosas         break;
690ccfca2fcSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
691ccfca2fcSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
692ccfca2fcSFabiano Rosas         msr |= env->error_code;
693ccfca2fcSFabiano Rosas         break;
694ccfca2fcSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
695ccfca2fcSFabiano Rosas         break;
696ccfca2fcSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
697ccfca2fcSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
698ccfca2fcSFabiano Rosas         /*
699ccfca2fcSFabiano Rosas          * Note: the opcode fields will not be set properly for a
700ccfca2fcSFabiano Rosas          * direct store load/store, but nobody cares as nobody
701ccfca2fcSFabiano Rosas          * actually uses direct store segments.
702ccfca2fcSFabiano Rosas          */
703ccfca2fcSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
704ccfca2fcSFabiano Rosas         break;
705ccfca2fcSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
706ccfca2fcSFabiano Rosas         switch (env->error_code & ~0xF) {
707ccfca2fcSFabiano Rosas         case POWERPC_EXCP_FP:
708da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
709ccfca2fcSFabiano Rosas                 trace_ppc_excp_fp_ignore();
7103680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
711ccfca2fcSFabiano Rosas                 return;
712ccfca2fcSFabiano Rosas             }
713ccfca2fcSFabiano Rosas             /*
714ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
715ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
716ccfca2fcSFabiano Rosas              */
717ccfca2fcSFabiano Rosas             msr |= 0x00100000;
718ccfca2fcSFabiano Rosas             break;
719ccfca2fcSFabiano Rosas         case POWERPC_EXCP_INVAL:
720ccfca2fcSFabiano Rosas             trace_ppc_excp_inval(env->nip);
721ccfca2fcSFabiano Rosas             msr |= 0x00080000;
722ccfca2fcSFabiano Rosas             break;
723ccfca2fcSFabiano Rosas         case POWERPC_EXCP_PRIV:
724ccfca2fcSFabiano Rosas             msr |= 0x00040000;
725ccfca2fcSFabiano Rosas             break;
726ccfca2fcSFabiano Rosas         case POWERPC_EXCP_TRAP:
727ccfca2fcSFabiano Rosas             msr |= 0x00020000;
728ccfca2fcSFabiano Rosas             break;
729ccfca2fcSFabiano Rosas         default:
730ccfca2fcSFabiano Rosas             /* Should never occur */
731bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
732ccfca2fcSFabiano Rosas                       env->error_code);
733ccfca2fcSFabiano Rosas             break;
734ccfca2fcSFabiano Rosas         }
735ccfca2fcSFabiano Rosas         break;
736ccfca2fcSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
7373b578635SFabiano Rosas     {
7383b578635SFabiano Rosas         int lev = env->error_code;
739ccfca2fcSFabiano Rosas 
7403b578635SFabiano Rosas         if (lev == 1 && cpu->vhyp) {
741ccfca2fcSFabiano Rosas             dump_hcall(env);
742ccfca2fcSFabiano Rosas         } else {
743ccfca2fcSFabiano Rosas             dump_syscall(env);
744ccfca2fcSFabiano Rosas         }
745ccfca2fcSFabiano Rosas 
746ccfca2fcSFabiano Rosas         /*
747ccfca2fcSFabiano Rosas          * We need to correct the NIP which in this case is supposed
748ccfca2fcSFabiano Rosas          * to point to the next instruction
749ccfca2fcSFabiano Rosas          */
750ccfca2fcSFabiano Rosas         env->nip += 4;
751ccfca2fcSFabiano Rosas 
7523b578635SFabiano Rosas         /*
7533b578635SFabiano Rosas          * The Virtual Open Firmware (VOF) relies on the 'sc 1'
7543b578635SFabiano Rosas          * instruction to communicate with QEMU. The pegasos2 machine
7553b578635SFabiano Rosas          * uses VOF and the 7xx CPUs, so although the 7xx don't have
7563b578635SFabiano Rosas          * HV mode, we need to keep hypercall support.
7573b578635SFabiano Rosas          */
7583b578635SFabiano Rosas         if (lev == 1 && cpu->vhyp) {
759c700b5e1SNicholas Piggin             cpu->vhyp_class->hypercall(cpu->vhyp, cpu);
7602c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
761ccfca2fcSFabiano Rosas             return;
762ccfca2fcSFabiano Rosas         }
7633b578635SFabiano Rosas 
764ccfca2fcSFabiano Rosas         break;
7653b578635SFabiano Rosas     }
766ccfca2fcSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
767ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
768ccfca2fcSFabiano Rosas         break;
769ccfca2fcSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
7708e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
771bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
772bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset exception "
773bc30c1c6SBALATON Zoltan                       "%d with no HV support\n", excp);
774ccfca2fcSFabiano Rosas         }
775ccfca2fcSFabiano Rosas         break;
776ccfca2fcSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
777ccfca2fcSFabiano Rosas         break;
778ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
779ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
780ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
781ccfca2fcSFabiano Rosas         ppc_excp_debug_sw_tlb(env, excp);
782ccfca2fcSFabiano Rosas         msr |= env->crf[0] << 28;
783ccfca2fcSFabiano Rosas         msr |= env->error_code; /* key, D/I, S/L bits */
784ccfca2fcSFabiano Rosas         /* Set way using a LRU mechanism */
785ccfca2fcSFabiano Rosas         msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
786ccfca2fcSFabiano Rosas         break;
787ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
788ccfca2fcSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
789ccfca2fcSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
790ccfca2fcSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
791bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
792ccfca2fcSFabiano Rosas                   powerpc_excp_name(excp));
793ccfca2fcSFabiano Rosas         break;
794ccfca2fcSFabiano Rosas     default:
795bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
796bc30c1c6SBALATON Zoltan                   excp);
797ccfca2fcSFabiano Rosas         break;
798ccfca2fcSFabiano Rosas     }
799ccfca2fcSFabiano Rosas 
800ccfca2fcSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
801ccfca2fcSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
802ccfca2fcSFabiano Rosas     }
803fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
804fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR1] = msr;
805ccfca2fcSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
806ccfca2fcSFabiano Rosas }
807ccfca2fcSFabiano Rosas 
powerpc_excp_74xx(PowerPCCPU * cpu,int excp)80852926b0dSFabiano Rosas static void powerpc_excp_74xx(PowerPCCPU *cpu, int excp)
80952926b0dSFabiano Rosas {
81052926b0dSFabiano Rosas     CPUPPCState *env = &cpu->env;
81152926b0dSFabiano Rosas     target_ulong msr, new_msr, vector;
81252926b0dSFabiano Rosas 
81352926b0dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
81452926b0dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
81552926b0dSFabiano Rosas 
816ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
8171f6faf8bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
81852926b0dSFabiano Rosas 
819ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
8201f6faf8bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
82152926b0dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
82252926b0dSFabiano Rosas     }
82352926b0dSFabiano Rosas 
82452926b0dSFabiano Rosas     vector = env->excp_vectors[excp];
82552926b0dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
826bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
827bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
82852926b0dSFabiano Rosas     }
82952926b0dSFabiano Rosas     vector |= env->excp_prefix;
83052926b0dSFabiano Rosas 
83152926b0dSFabiano Rosas     switch (excp) {
83252926b0dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
833819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
83452926b0dSFabiano Rosas         /* machine check exceptions don't have ME set */
83552926b0dSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
83652926b0dSFabiano Rosas         break;
83752926b0dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
83852926b0dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
83952926b0dSFabiano Rosas         break;
84052926b0dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
84152926b0dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
84252926b0dSFabiano Rosas         msr |= env->error_code;
84352926b0dSFabiano Rosas         break;
84452926b0dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
84552926b0dSFabiano Rosas         break;
84652926b0dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
84752926b0dSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
84852926b0dSFabiano Rosas         /*
84952926b0dSFabiano Rosas          * Note: the opcode fields will not be set properly for a
85052926b0dSFabiano Rosas          * direct store load/store, but nobody cares as nobody
85152926b0dSFabiano Rosas          * actually uses direct store segments.
85252926b0dSFabiano Rosas          */
85352926b0dSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
85452926b0dSFabiano Rosas         break;
85552926b0dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
85652926b0dSFabiano Rosas         switch (env->error_code & ~0xF) {
85752926b0dSFabiano Rosas         case POWERPC_EXCP_FP:
858da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
85952926b0dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
8603680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
86152926b0dSFabiano Rosas                 return;
86252926b0dSFabiano Rosas             }
86352926b0dSFabiano Rosas             /*
864ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
865ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
86652926b0dSFabiano Rosas              */
86752926b0dSFabiano Rosas             msr |= 0x00100000;
86852926b0dSFabiano Rosas             break;
86952926b0dSFabiano Rosas         case POWERPC_EXCP_INVAL:
87052926b0dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
87152926b0dSFabiano Rosas             msr |= 0x00080000;
87252926b0dSFabiano Rosas             break;
87352926b0dSFabiano Rosas         case POWERPC_EXCP_PRIV:
87452926b0dSFabiano Rosas             msr |= 0x00040000;
87552926b0dSFabiano Rosas             break;
87652926b0dSFabiano Rosas         case POWERPC_EXCP_TRAP:
87752926b0dSFabiano Rosas             msr |= 0x00020000;
87852926b0dSFabiano Rosas             break;
87952926b0dSFabiano Rosas         default:
88052926b0dSFabiano Rosas             /* Should never occur */
881bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
88252926b0dSFabiano Rosas                       env->error_code);
88352926b0dSFabiano Rosas             break;
88452926b0dSFabiano Rosas         }
88552926b0dSFabiano Rosas         break;
88652926b0dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
887bca2c6d9SFabiano Rosas     {
888bca2c6d9SFabiano Rosas         int lev = env->error_code;
88952926b0dSFabiano Rosas 
8902306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
89152926b0dSFabiano Rosas             dump_hcall(env);
89252926b0dSFabiano Rosas         } else {
89352926b0dSFabiano Rosas             dump_syscall(env);
89452926b0dSFabiano Rosas         }
89552926b0dSFabiano Rosas 
89652926b0dSFabiano Rosas         /*
89752926b0dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
89852926b0dSFabiano Rosas          * to point to the next instruction
89952926b0dSFabiano Rosas          */
90052926b0dSFabiano Rosas         env->nip += 4;
90152926b0dSFabiano Rosas 
902bca2c6d9SFabiano Rosas         /*
903bca2c6d9SFabiano Rosas          * The Virtual Open Firmware (VOF) relies on the 'sc 1'
904bca2c6d9SFabiano Rosas          * instruction to communicate with QEMU. The pegasos2 machine
905bca2c6d9SFabiano Rosas          * uses VOF and the 74xx CPUs, so although the 74xx don't have
906bca2c6d9SFabiano Rosas          * HV mode, we need to keep hypercall support.
907bca2c6d9SFabiano Rosas          */
9082306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
909c700b5e1SNicholas Piggin             cpu->vhyp_class->hypercall(cpu->vhyp, cpu);
9102c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
91152926b0dSFabiano Rosas             return;
91252926b0dSFabiano Rosas         }
913bca2c6d9SFabiano Rosas 
91452926b0dSFabiano Rosas         break;
915bca2c6d9SFabiano Rosas     }
91652926b0dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
91752926b0dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
91852926b0dSFabiano Rosas         break;
91952926b0dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
9208e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
921bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
922bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset "
92352926b0dSFabiano Rosas                       "exception %d with no HV support\n", excp);
92452926b0dSFabiano Rosas         }
92552926b0dSFabiano Rosas         break;
92652926b0dSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
92752926b0dSFabiano Rosas         break;
92852926b0dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
92952926b0dSFabiano Rosas         break;
93052926b0dSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
93152926b0dSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
93252926b0dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
93352926b0dSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
93452926b0dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
935bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
93652926b0dSFabiano Rosas                   powerpc_excp_name(excp));
93752926b0dSFabiano Rosas         break;
93852926b0dSFabiano Rosas     default:
939bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
940bc30c1c6SBALATON Zoltan                   excp);
94152926b0dSFabiano Rosas         break;
94252926b0dSFabiano Rosas     }
94352926b0dSFabiano Rosas 
94452926b0dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
94552926b0dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
94652926b0dSFabiano Rosas     }
947f82db777SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
948f82db777SFabiano Rosas     env->spr[SPR_SRR1] = msr;
94952926b0dSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
95052926b0dSFabiano Rosas }
95152926b0dSFabiano Rosas 
powerpc_excp_booke(PowerPCCPU * cpu,int excp)952180952ceSFabiano Rosas static void powerpc_excp_booke(PowerPCCPU *cpu, int excp)
953180952ceSFabiano Rosas {
954180952ceSFabiano Rosas     CPUPPCState *env = &cpu->env;
955180952ceSFabiano Rosas     target_ulong msr, new_msr, vector;
956ab452503SBALATON Zoltan     int srr0 = SPR_SRR0, srr1 = SPR_SRR1;
957180952ceSFabiano Rosas 
958ab452503SBALATON Zoltan     /*
959ab452503SBALATON Zoltan      * Book E does not play games with certain bits of xSRR1 being MSR save
960ab452503SBALATON Zoltan      * bits and others being error status. xSRR1 is the old MSR, period.
961ab452503SBALATON Zoltan      */
962180952ceSFabiano Rosas     msr = env->msr;
963180952ceSFabiano Rosas 
964ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
9659dc20cc3SFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
966180952ceSFabiano Rosas 
967ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
9689dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
969180952ceSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
970180952ceSFabiano Rosas     }
971180952ceSFabiano Rosas 
972180952ceSFabiano Rosas #ifdef TARGET_PPC64
973180952ceSFabiano Rosas     /*
974180952ceSFabiano Rosas      * SPEU and VPU share the same IVOR but they exist in different
975180952ceSFabiano Rosas      * processors. SPEU is e500v1/2 only and VPU is e6500 only.
976180952ceSFabiano Rosas      */
9779dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_VPU) {
978180952ceSFabiano Rosas         excp = POWERPC_EXCP_SPEU;
979180952ceSFabiano Rosas     }
980180952ceSFabiano Rosas #endif
981180952ceSFabiano Rosas 
982180952ceSFabiano Rosas     vector = env->excp_vectors[excp];
983180952ceSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
984bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
985bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
986180952ceSFabiano Rosas     }
987180952ceSFabiano Rosas     vector |= env->excp_prefix;
988180952ceSFabiano Rosas 
989180952ceSFabiano Rosas     switch (excp) {
990180952ceSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
991180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
992180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
993180952ceSFabiano Rosas         break;
994180952ceSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
995819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
996180952ceSFabiano Rosas         /* machine check exceptions don't have ME set */
997180952ceSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
998180952ceSFabiano Rosas 
999180952ceSFabiano Rosas         /* FIXME: choose one or the other based on CPU type */
1000180952ceSFabiano Rosas         srr0 = SPR_BOOKE_MCSRR0;
1001180952ceSFabiano Rosas         srr1 = SPR_BOOKE_MCSRR1;
1002180952ceSFabiano Rosas 
1003180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR0] = env->nip;
1004180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR1] = msr;
1005db403211SFabiano Rosas 
1006180952ceSFabiano Rosas         break;
1007180952ceSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
1008afdbc869SFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
1009180952ceSFabiano Rosas         break;
1010180952ceSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
1011180952ceSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
1012180952ceSFabiano Rosas         break;
1013180952ceSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
1014180952ceSFabiano Rosas         if (env->mpic_proxy) {
1015bc30c1c6SBALATON Zoltan             CPUState *cs = env_cpu(env);
1016180952ceSFabiano Rosas             /* IACK the IRQ on delivery */
1017180952ceSFabiano Rosas             env->spr[SPR_BOOKE_EPR] = ldl_phys(cs->as, env->mpic_iack);
1018180952ceSFabiano Rosas         }
1019180952ceSFabiano Rosas         break;
1020180952ceSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
1021180952ceSFabiano Rosas         break;
1022180952ceSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
1023180952ceSFabiano Rosas         switch (env->error_code & ~0xF) {
1024180952ceSFabiano Rosas         case POWERPC_EXCP_FP:
1025da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
1026180952ceSFabiano Rosas                 trace_ppc_excp_fp_ignore();
10273680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
1028180952ceSFabiano Rosas                 return;
1029180952ceSFabiano Rosas             }
1030180952ceSFabiano Rosas             /*
1031ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
1032ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
1033180952ceSFabiano Rosas              */
1034180952ceSFabiano Rosas             msr |= 0x00100000;
1035180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_FP;
1036180952ceSFabiano Rosas             break;
1037180952ceSFabiano Rosas         case POWERPC_EXCP_INVAL:
1038180952ceSFabiano Rosas             trace_ppc_excp_inval(env->nip);
1039180952ceSFabiano Rosas             msr |= 0x00080000;
1040180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PIL;
1041180952ceSFabiano Rosas             break;
1042180952ceSFabiano Rosas         case POWERPC_EXCP_PRIV:
1043180952ceSFabiano Rosas             msr |= 0x00040000;
1044180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PPR;
1045180952ceSFabiano Rosas             break;
1046180952ceSFabiano Rosas         case POWERPC_EXCP_TRAP:
1047180952ceSFabiano Rosas             msr |= 0x00020000;
1048180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PTR;
1049180952ceSFabiano Rosas             break;
1050180952ceSFabiano Rosas         default:
1051180952ceSFabiano Rosas             /* Should never occur */
1052bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
1053180952ceSFabiano Rosas                       env->error_code);
1054180952ceSFabiano Rosas             break;
1055180952ceSFabiano Rosas         }
1056180952ceSFabiano Rosas         break;
1057180952ceSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
1058180952ceSFabiano Rosas         dump_syscall(env);
1059180952ceSFabiano Rosas 
1060180952ceSFabiano Rosas         /*
1061180952ceSFabiano Rosas          * We need to correct the NIP which in this case is supposed
1062180952ceSFabiano Rosas          * to point to the next instruction
1063180952ceSFabiano Rosas          */
1064180952ceSFabiano Rosas         env->nip += 4;
1065180952ceSFabiano Rosas         break;
1066180952ceSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
1067180952ceSFabiano Rosas     case POWERPC_EXCP_APU:       /* Auxiliary processor unavailable          */
1068180952ceSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
1069180952ceSFabiano Rosas         break;
1070180952ceSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
1071180952ceSFabiano Rosas         /* FIT on 4xx */
1072180952ceSFabiano Rosas         trace_ppc_excp_print("FIT");
1073180952ceSFabiano Rosas         break;
1074180952ceSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
1075180952ceSFabiano Rosas         trace_ppc_excp_print("WDT");
1076180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
1077180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
1078180952ceSFabiano Rosas         break;
1079180952ceSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
1080180952ceSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
1081180952ceSFabiano Rosas         break;
1082180952ceSFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
1083180952ceSFabiano Rosas         if (env->flags & POWERPC_FLAG_DE) {
1084180952ceSFabiano Rosas             /* FIXME: choose one or the other based on CPU type */
1085180952ceSFabiano Rosas             srr0 = SPR_BOOKE_DSRR0;
1086180952ceSFabiano Rosas             srr1 = SPR_BOOKE_DSRR1;
1087180952ceSFabiano Rosas 
1088180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR0] = env->nip;
1089180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR1] = msr;
1090180952ceSFabiano Rosas 
1091180952ceSFabiano Rosas             /* DBSR already modified by caller */
1092180952ceSFabiano Rosas         } else {
1093bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
1094bc30c1c6SBALATON Zoltan                       "Debug exception triggered on unsupported model\n");
1095180952ceSFabiano Rosas         }
1096180952ceSFabiano Rosas         break;
1097180952ceSFabiano Rosas     case POWERPC_EXCP_SPEU:   /* SPE/embedded floating-point unavailable/VPU  */
1098180952ceSFabiano Rosas         env->spr[SPR_BOOKE_ESR] = ESR_SPV;
1099180952ceSFabiano Rosas         break;
11009364df26SNicholas Piggin     case POWERPC_EXCP_DOORI:     /* Embedded doorbell interrupt              */
11019364df26SNicholas Piggin         break;
11029364df26SNicholas Piggin     case POWERPC_EXCP_DOORCI:    /* Embedded doorbell critical interrupt     */
11039364df26SNicholas Piggin         srr0 = SPR_BOOKE_CSRR0;
11049364df26SNicholas Piggin         srr1 = SPR_BOOKE_CSRR1;
11059364df26SNicholas Piggin         break;
1106180952ceSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
11078e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
1108bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
1109bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset "
1110180952ceSFabiano Rosas                       "exception %d with no HV support\n", excp);
1111180952ceSFabiano Rosas         }
1112180952ceSFabiano Rosas         break;
1113180952ceSFabiano Rosas     case POWERPC_EXCP_EFPDI:     /* Embedded floating-point data interrupt   */
1114180952ceSFabiano Rosas     case POWERPC_EXCP_EFPRI:     /* Embedded floating-point round interrupt  */
1115bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
1116180952ceSFabiano Rosas                   powerpc_excp_name(excp));
1117180952ceSFabiano Rosas         break;
1118180952ceSFabiano Rosas     default:
1119bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1120bc30c1c6SBALATON Zoltan                   excp);
1121180952ceSFabiano Rosas         break;
1122180952ceSFabiano Rosas     }
1123180952ceSFabiano Rosas 
1124f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
1125180952ceSFabiano Rosas     if (env->spr[SPR_BOOKE_EPCR] & EPCR_ICM) {
1126180952ceSFabiano Rosas         /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */
1127180952ceSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_CM;
1128180952ceSFabiano Rosas     } else {
1129180952ceSFabiano Rosas         vector = (uint32_t)vector;
1130180952ceSFabiano Rosas     }
1131180952ceSFabiano Rosas #endif
1132180952ceSFabiano Rosas 
1133180952ceSFabiano Rosas     env->spr[srr0] = env->nip;
1134180952ceSFabiano Rosas     env->spr[srr1] = msr;
1135180952ceSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
1136180952ceSFabiano Rosas }
1137180952ceSFabiano Rosas 
11387cebc5dbSNicholas Piggin /*
11397cebc5dbSNicholas Piggin  * When running a nested HV guest under vhyp, external interrupts are
11407cebc5dbSNicholas Piggin  * delivered as HVIRT.
11417cebc5dbSNicholas Piggin  */
books_vhyp_promotes_external_to_hvirt(PowerPCCPU * cpu)11427cebc5dbSNicholas Piggin static bool books_vhyp_promotes_external_to_hvirt(PowerPCCPU *cpu)
11437cebc5dbSNicholas Piggin {
11447cebc5dbSNicholas Piggin     if (cpu->vhyp) {
11457cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
11467cebc5dbSNicholas Piggin     }
11477cebc5dbSNicholas Piggin     return false;
11487cebc5dbSNicholas Piggin }
11497cebc5dbSNicholas Piggin 
115030c4e426SFabiano Rosas #ifdef TARGET_PPC64
11514c6cf6b2SNicholas Piggin /*
11524c6cf6b2SNicholas Piggin  * When running under vhyp, hcalls are always intercepted and sent to the
11534c6cf6b2SNicholas Piggin  * vhc->hypercall handler.
11544c6cf6b2SNicholas Piggin  */
books_vhyp_handles_hcall(PowerPCCPU * cpu)11554c6cf6b2SNicholas Piggin static bool books_vhyp_handles_hcall(PowerPCCPU *cpu)
11564c6cf6b2SNicholas Piggin {
11574c6cf6b2SNicholas Piggin     if (cpu->vhyp) {
11587cebc5dbSNicholas Piggin         return !vhyp_cpu_in_nested(cpu);
11597cebc5dbSNicholas Piggin     }
11607cebc5dbSNicholas Piggin     return false;
11617cebc5dbSNicholas Piggin }
11627cebc5dbSNicholas Piggin 
11637cebc5dbSNicholas Piggin /*
11647cebc5dbSNicholas Piggin  * When running a nested KVM HV guest under vhyp, HV exceptions are not
11657cebc5dbSNicholas Piggin  * delivered to the guest (because there is no concept of HV support), but
11667cebc5dbSNicholas Piggin  * rather they are sent to the vhyp to exit from the L2 back to the L1 and
11677cebc5dbSNicholas Piggin  * return from the H_ENTER_NESTED hypercall.
11687cebc5dbSNicholas Piggin  */
books_vhyp_handles_hv_excp(PowerPCCPU * cpu)11697cebc5dbSNicholas Piggin static bool books_vhyp_handles_hv_excp(PowerPCCPU *cpu)
11707cebc5dbSNicholas Piggin {
11717cebc5dbSNicholas Piggin     if (cpu->vhyp) {
11727cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
11734c6cf6b2SNicholas Piggin     }
11744c6cf6b2SNicholas Piggin     return false;
11754c6cf6b2SNicholas Piggin }
11764c6cf6b2SNicholas Piggin 
11775a5d3b23SNicholas Piggin #ifdef CONFIG_TCG
is_prefix_insn(CPUPPCState * env,uint32_t insn)11785a5d3b23SNicholas Piggin static bool is_prefix_insn(CPUPPCState *env, uint32_t insn)
11795a5d3b23SNicholas Piggin {
11805a5d3b23SNicholas Piggin     if (!(env->insns_flags2 & PPC2_ISA310)) {
11815a5d3b23SNicholas Piggin         return false;
11825a5d3b23SNicholas Piggin     }
11835a5d3b23SNicholas Piggin     return ((insn & 0xfc000000) == 0x04000000);
11845a5d3b23SNicholas Piggin }
11855a5d3b23SNicholas Piggin 
is_prefix_insn_excp(PowerPCCPU * cpu,int excp)11865a5d3b23SNicholas Piggin static bool is_prefix_insn_excp(PowerPCCPU *cpu, int excp)
11875a5d3b23SNicholas Piggin {
11885a5d3b23SNicholas Piggin     CPUPPCState *env = &cpu->env;
11895a5d3b23SNicholas Piggin 
1190c8fd9667SNicholas Piggin     if (!(env->insns_flags2 & PPC2_ISA310)) {
1191c8fd9667SNicholas Piggin         return false;
1192c8fd9667SNicholas Piggin     }
1193c8fd9667SNicholas Piggin 
11945a5d3b23SNicholas Piggin     if (!tcg_enabled()) {
11955a5d3b23SNicholas Piggin         /*
11965a5d3b23SNicholas Piggin          * This does not load instructions and set the prefix bit correctly
11975a5d3b23SNicholas Piggin          * for injected interrupts with KVM. That may have to be discovered
11985a5d3b23SNicholas Piggin          * and set by the KVM layer before injecting.
11995a5d3b23SNicholas Piggin          */
12005a5d3b23SNicholas Piggin         return false;
12015a5d3b23SNicholas Piggin     }
12025a5d3b23SNicholas Piggin 
12035a5d3b23SNicholas Piggin     switch (excp) {
1204c8fd9667SNicholas Piggin     case POWERPC_EXCP_MCHECK:
1205c8fd9667SNicholas Piggin         if (!(env->error_code & PPC_BIT(42))) {
1206c8fd9667SNicholas Piggin             /*
1207c8fd9667SNicholas Piggin              * Fetch attempt caused a machine check, so attempting to fetch
1208c8fd9667SNicholas Piggin              * again would cause a recursive machine check.
1209c8fd9667SNicholas Piggin              */
1210c8fd9667SNicholas Piggin             return false;
1211c8fd9667SNicholas Piggin         }
1212c8fd9667SNicholas Piggin         break;
12135a5d3b23SNicholas Piggin     case POWERPC_EXCP_HDSI:
12145a5d3b23SNicholas Piggin         /* HDSI PRTABLE_FAULT has the originating access type in error_code */
12155a5d3b23SNicholas Piggin         if ((env->spr[SPR_HDSISR] & DSISR_PRTABLE_FAULT) &&
12165a5d3b23SNicholas Piggin             (env->error_code == MMU_INST_FETCH)) {
12175a5d3b23SNicholas Piggin             /*
12185a5d3b23SNicholas Piggin              * Fetch failed due to partition scope translation, so prefix
12195a5d3b23SNicholas Piggin              * indication is not relevant (and attempting to load the
12205a5d3b23SNicholas Piggin              * instruction at NIP would cause recursive faults with the same
12215a5d3b23SNicholas Piggin              * translation).
12225a5d3b23SNicholas Piggin              */
1223c8fd9667SNicholas Piggin             return false;
12245a5d3b23SNicholas Piggin         }
1225c8fd9667SNicholas Piggin         break;
1226c8fd9667SNicholas Piggin 
12275a5d3b23SNicholas Piggin     case POWERPC_EXCP_DSI:
12285a5d3b23SNicholas Piggin     case POWERPC_EXCP_DSEG:
12295a5d3b23SNicholas Piggin     case POWERPC_EXCP_ALIGN:
12305a5d3b23SNicholas Piggin     case POWERPC_EXCP_PROGRAM:
12315a5d3b23SNicholas Piggin     case POWERPC_EXCP_FPU:
12325a5d3b23SNicholas Piggin     case POWERPC_EXCP_TRACE:
12335a5d3b23SNicholas Piggin     case POWERPC_EXCP_HV_EMU:
12345a5d3b23SNicholas Piggin     case POWERPC_EXCP_VPU:
12355a5d3b23SNicholas Piggin     case POWERPC_EXCP_VSXU:
12365a5d3b23SNicholas Piggin     case POWERPC_EXCP_FU:
1237c8fd9667SNicholas Piggin     case POWERPC_EXCP_HV_FU:
12385a5d3b23SNicholas Piggin         break;
12395a5d3b23SNicholas Piggin     default:
12405a5d3b23SNicholas Piggin         return false;
12415a5d3b23SNicholas Piggin     }
1242c8fd9667SNicholas Piggin 
1243c8fd9667SNicholas Piggin     return is_prefix_insn(env, ppc_ldl_code(env, env->nip));
1244c8fd9667SNicholas Piggin }
12455a5d3b23SNicholas Piggin #else
is_prefix_insn_excp(PowerPCCPU * cpu,int excp)12465a5d3b23SNicholas Piggin static bool is_prefix_insn_excp(PowerPCCPU *cpu, int excp)
12475a5d3b23SNicholas Piggin {
12485a5d3b23SNicholas Piggin     return false;
12495a5d3b23SNicholas Piggin }
12505a5d3b23SNicholas Piggin #endif
12515a5d3b23SNicholas Piggin 
powerpc_excp_books(PowerPCCPU * cpu,int excp)12529f338e4dSFabiano Rosas static void powerpc_excp_books(PowerPCCPU *cpu, int excp)
12539f338e4dSFabiano Rosas {
12549f338e4dSFabiano Rosas     CPUPPCState *env = &cpu->env;
12559f338e4dSFabiano Rosas     target_ulong msr, new_msr, vector;
1256ab452503SBALATON Zoltan     int srr0 = SPR_SRR0, srr1 = SPR_SRR1, lev = -1;
12579f338e4dSFabiano Rosas 
12589f338e4dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
12599f338e4dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
12609f338e4dSFabiano Rosas 
12619f338e4dSFabiano Rosas     /*
1262ab452503SBALATON Zoltan      * new interrupt handler msr preserves HV and ME unless explicitly
1263ab452503SBALATON Zoltan      * overridden
12649f338e4dSFabiano Rosas      */
12659f338e4dSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME) | MSR_HVB);
12669f338e4dSFabiano Rosas 
12679f338e4dSFabiano Rosas     /*
12689f338e4dSFabiano Rosas      * check for special resume at 0x100 from doze/nap/sleep/winkle on
12699f338e4dSFabiano Rosas      * P7/P8/P9
12709f338e4dSFabiano Rosas      */
12719f338e4dSFabiano Rosas     if (env->resume_as_sreset) {
12723f88a89dSBALATON Zoltan         excp = powerpc_reset_wakeup(env, excp, &msr);
12739f338e4dSFabiano Rosas     }
12749f338e4dSFabiano Rosas 
12759f338e4dSFabiano Rosas     /*
127630c4e426SFabiano Rosas      * We don't want to generate a Hypervisor Emulation Assistance
12776c242e79SNicholas Piggin      * Interrupt if we don't have HVB in msr_mask (PAPR mode),
12786c242e79SNicholas Piggin      * unless running a nested-hv guest, in which case the L1
12796c242e79SNicholas Piggin      * kernel wants the interrupt.
12809f338e4dSFabiano Rosas      */
12816c242e79SNicholas Piggin     if (excp == POWERPC_EXCP_HV_EMU && !(env->msr_mask & MSR_HVB) &&
12826c242e79SNicholas Piggin             !books_vhyp_handles_hv_excp(cpu)) {
12839f338e4dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
12849f338e4dSFabiano Rosas     }
12859f338e4dSFabiano Rosas 
12869f338e4dSFabiano Rosas     vector = env->excp_vectors[excp];
12879f338e4dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
1288bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
1289bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
12909f338e4dSFabiano Rosas     }
12919f338e4dSFabiano Rosas     vector |= env->excp_prefix;
12929f338e4dSFabiano Rosas 
12935a5d3b23SNicholas Piggin     if (is_prefix_insn_excp(cpu, excp)) {
12945a5d3b23SNicholas Piggin         msr |= PPC_BIT(34);
12955a5d3b23SNicholas Piggin     }
12965a5d3b23SNicholas Piggin 
12979f338e4dSFabiano Rosas     switch (excp) {
12989f338e4dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
1299819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
13009f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
13019f338e4dSFabiano Rosas             /*
13029f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
13039f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR).
13049f338e4dSFabiano Rosas              */
13059f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
130643453161SNicholas Piggin 
130743453161SNicholas Piggin             /* HV machine check exceptions don't have ME set */
13089f338e4dSFabiano Rosas             new_msr &= ~((target_ulong)1 << MSR_ME);
130943453161SNicholas Piggin         }
13109f338e4dSFabiano Rosas 
131155a7fa34SNicholas Piggin         msr |= env->error_code;
13129f338e4dSFabiano Rosas         break;
131355a7fa34SNicholas Piggin 
13149f338e4dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
13159f338e4dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
13169f338e4dSFabiano Rosas         break;
13179f338e4dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
13189f338e4dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
13199f338e4dSFabiano Rosas         msr |= env->error_code;
13209f338e4dSFabiano Rosas         break;
13219f338e4dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
13229f338e4dSFabiano Rosas     {
13239f338e4dSFabiano Rosas         bool lpes0;
13249f338e4dSFabiano Rosas 
1325ab452503SBALATON Zoltan         /* LPES0 is only taken into consideration if we support HV mode */
132667baff77SFabiano Rosas         if (!env->has_hv_mode) {
132767baff77SFabiano Rosas             break;
13289f338e4dSFabiano Rosas         }
132967baff77SFabiano Rosas         lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
13309f338e4dSFabiano Rosas         if (!lpes0) {
13319f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
13329f338e4dSFabiano Rosas             new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
13339f338e4dSFabiano Rosas             srr0 = SPR_HSRR0;
13349f338e4dSFabiano Rosas             srr1 = SPR_HSRR1;
13359f338e4dSFabiano Rosas         }
13369f338e4dSFabiano Rosas         break;
13379f338e4dSFabiano Rosas     }
13389f338e4dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
13394ee5d281SNicholas Piggin         /* Optional DSISR update was removed from ISA v3.0 */
13404ee5d281SNicholas Piggin         if (!(env->insns_flags2 & PPC2_ISA300)) {
13419f338e4dSFabiano Rosas             /* Get rS/rD and rA from faulting opcode */
13429f338e4dSFabiano Rosas             /*
13439f338e4dSFabiano Rosas              * Note: the opcode fields will not be set properly for a
13449f338e4dSFabiano Rosas              * direct store load/store, but nobody cares as nobody
13459f338e4dSFabiano Rosas              * actually uses direct store segments.
13469f338e4dSFabiano Rosas              */
13479f338e4dSFabiano Rosas             env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
13484ee5d281SNicholas Piggin         }
13499f338e4dSFabiano Rosas         break;
13509f338e4dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
13519f338e4dSFabiano Rosas         switch (env->error_code & ~0xF) {
13529f338e4dSFabiano Rosas         case POWERPC_EXCP_FP:
1353da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
13549f338e4dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
13553680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
13569f338e4dSFabiano Rosas                 return;
13579f338e4dSFabiano Rosas             }
13589f338e4dSFabiano Rosas             /*
1359ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
1360ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
13619f338e4dSFabiano Rosas              */
13629f338e4dSFabiano Rosas             msr |= 0x00100000;
13639f338e4dSFabiano Rosas             break;
13649f338e4dSFabiano Rosas         case POWERPC_EXCP_INVAL:
13659f338e4dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
13669f338e4dSFabiano Rosas             msr |= 0x00080000;
13679f338e4dSFabiano Rosas             break;
13689f338e4dSFabiano Rosas         case POWERPC_EXCP_PRIV:
13699f338e4dSFabiano Rosas             msr |= 0x00040000;
13709f338e4dSFabiano Rosas             break;
13719f338e4dSFabiano Rosas         case POWERPC_EXCP_TRAP:
13729f338e4dSFabiano Rosas             msr |= 0x00020000;
13739f338e4dSFabiano Rosas             break;
13749f338e4dSFabiano Rosas         default:
13759f338e4dSFabiano Rosas             /* Should never occur */
1376bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
13779f338e4dSFabiano Rosas                       env->error_code);
13789f338e4dSFabiano Rosas             break;
13799f338e4dSFabiano Rosas         }
13809f338e4dSFabiano Rosas         break;
13819f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
13829f338e4dSFabiano Rosas         lev = env->error_code;
13839f338e4dSFabiano Rosas 
13842306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
13859f338e4dSFabiano Rosas             dump_hcall(env);
13869f338e4dSFabiano Rosas         } else {
13879f338e4dSFabiano Rosas             dump_syscall(env);
13889f338e4dSFabiano Rosas         }
13899f338e4dSFabiano Rosas 
13909f338e4dSFabiano Rosas         /*
13919f338e4dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
13929f338e4dSFabiano Rosas          * to point to the next instruction
13939f338e4dSFabiano Rosas          */
13949f338e4dSFabiano Rosas         env->nip += 4;
13959f338e4dSFabiano Rosas 
13969f338e4dSFabiano Rosas         /* "PAPR mode" built-in hypercall emulation */
13972306c606SBALATON Zoltan         if (lev == 1 && books_vhyp_handles_hcall(cpu)) {
1398c700b5e1SNicholas Piggin             cpu->vhyp_class->hypercall(cpu->vhyp, cpu);
13992c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
14009f338e4dSFabiano Rosas             return;
14019f338e4dSFabiano Rosas         }
1402eb701f30SNicholas Piggin         if (env->insns_flags2 & PPC2_ISA310) {
1403eb701f30SNicholas Piggin             /* ISAv3.1 puts LEV into SRR1 */
1404eb701f30SNicholas Piggin             msr |= lev << 20;
1405eb701f30SNicholas Piggin         }
14069f338e4dSFabiano Rosas         if (lev == 1) {
14079f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14089f338e4dSFabiano Rosas         }
14099f338e4dSFabiano Rosas         break;
14109f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL_VECTORED: /* scv exception                     */
14119f338e4dSFabiano Rosas         lev = env->error_code;
14129f338e4dSFabiano Rosas         dump_syscall(env);
14139f338e4dSFabiano Rosas         env->nip += 4;
14149f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_EE);
14159f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
14169f338e4dSFabiano Rosas 
14179f338e4dSFabiano Rosas         vector += lev * 0x20;
14189f338e4dSFabiano Rosas 
14199f338e4dSFabiano Rosas         env->lr = env->nip;
14209f338e4dSFabiano Rosas         env->ctr = msr;
14219f338e4dSFabiano Rosas         break;
14229f338e4dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
14239f338e4dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
14249f338e4dSFabiano Rosas         break;
14259f338e4dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
14269f338e4dSFabiano Rosas         /* A power-saving exception sets ME, otherwise it is unchanged */
14278e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
14289f338e4dSFabiano Rosas             /* indicate that we resumed from power save mode */
14299f338e4dSFabiano Rosas             msr |= 0x10000;
14309f338e4dSFabiano Rosas             new_msr |= ((target_ulong)1 << MSR_ME);
14319f338e4dSFabiano Rosas         }
14329f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
14339f338e4dSFabiano Rosas             /*
14349f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
14359f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR, NMI injection in QEMU).
14369f338e4dSFabiano Rosas              */
14379f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14389f338e4dSFabiano Rosas         } else {
14398e54ad65SVíctor Colombo             if (FIELD_EX64(env->msr, MSR, POW)) {
1440bc30c1c6SBALATON Zoltan                 cpu_abort(env_cpu(env),
1441bc30c1c6SBALATON Zoltan                           "Trying to deliver power-saving system reset "
14429f338e4dSFabiano Rosas                           "exception %d with no HV support\n", excp);
14439f338e4dSFabiano Rosas             }
14449f338e4dSFabiano Rosas         }
14459f338e4dSFabiano Rosas         break;
144614895384SNicholas Piggin     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
144714895384SNicholas Piggin         msr |= env->error_code;
144814895384SNicholas Piggin         /* fall through */
14499f338e4dSFabiano Rosas     case POWERPC_EXCP_DSEG:      /* Data segment exception                   */
14509f338e4dSFabiano Rosas     case POWERPC_EXCP_ISEG:      /* Instruction segment exception            */
1451fd7abfabSNicholas Piggin     case POWERPC_EXCP_SDOOR:     /* Doorbell interrupt                       */
1452c29b0704SNicholas Piggin     case POWERPC_EXCP_PERFM:     /* Performance monitor interrupt            */
14539f338e4dSFabiano Rosas         break;
14549f338e4dSFabiano Rosas     case POWERPC_EXCP_HISI:      /* Hypervisor instruction storage exception */
14559f338e4dSFabiano Rosas         msr |= env->error_code;
14569f338e4dSFabiano Rosas         /* fall through */
14579f338e4dSFabiano Rosas     case POWERPC_EXCP_HDECR:     /* Hypervisor decrementer exception         */
14589f338e4dSFabiano Rosas     case POWERPC_EXCP_HDSI:      /* Hypervisor data storage exception        */
14599f338e4dSFabiano Rosas     case POWERPC_EXCP_SDOOR_HV:  /* Hypervisor Doorbell interrupt            */
14609f338e4dSFabiano Rosas     case POWERPC_EXCP_HVIRT:     /* Hypervisor virtualization                */
14619f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
14629f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
14639f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
14649f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
14659f338e4dSFabiano Rosas         break;
1466a3c020d8SNicholas Piggin #ifdef CONFIG_TCG
1467a3c020d8SNicholas Piggin     case POWERPC_EXCP_HV_EMU: {
1468a3c020d8SNicholas Piggin         uint32_t insn = ppc_ldl_code(env, env->nip);
1469a3c020d8SNicholas Piggin         env->spr[SPR_HEIR] = insn;
1470a3c020d8SNicholas Piggin         if (is_prefix_insn(env, insn)) {
1471a3c020d8SNicholas Piggin             uint32_t insn2 = ppc_ldl_code(env, env->nip + 4);
1472a3c020d8SNicholas Piggin             env->spr[SPR_HEIR] <<= 32;
1473a3c020d8SNicholas Piggin             env->spr[SPR_HEIR] |= insn2;
1474a3c020d8SNicholas Piggin         }
1475a3c020d8SNicholas Piggin         srr0 = SPR_HSRR0;
1476a3c020d8SNicholas Piggin         srr1 = SPR_HSRR1;
1477a3c020d8SNicholas Piggin         new_msr |= (target_ulong)MSR_HVB;
1478a3c020d8SNicholas Piggin         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
1479a3c020d8SNicholas Piggin         break;
1480a3c020d8SNicholas Piggin     }
1481a3c020d8SNicholas Piggin #endif
14829f338e4dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
14839f338e4dSFabiano Rosas     case POWERPC_EXCP_VSXU:       /* VSX unavailable exception               */
14849f338e4dSFabiano Rosas     case POWERPC_EXCP_FU:         /* Facility unavailable exception          */
14859f338e4dSFabiano Rosas         env->spr[SPR_FSCR] |= ((target_ulong)env->error_code << 56);
14869f338e4dSFabiano Rosas         break;
14879f338e4dSFabiano Rosas     case POWERPC_EXCP_HV_FU:     /* Hypervisor Facility Unavailable Exception */
14889f338e4dSFabiano Rosas         env->spr[SPR_HFSCR] |= ((target_ulong)env->error_code << FSCR_IC_POS);
14899f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
14909f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
14919f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
14929f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
14939f338e4dSFabiano Rosas         break;
1494cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_PERFM_EBB:        /* Performance Monitor EBB Exception  */
1495cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_EXTERNAL_EBB:     /* External EBB Exception             */
1496cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
1497cb76bbc4SDaniel Henrique Barboza 
1498cb76bbc4SDaniel Henrique Barboza         /*
1499cb76bbc4SDaniel Henrique Barboza          * Save NIP for rfebb insn in SPR_EBBRR. Next nip is
1500cb76bbc4SDaniel Henrique Barboza          * stored in the EBB Handler SPR_EBBHR.
1501cb76bbc4SDaniel Henrique Barboza          */
1502cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_EBBRR] = env->nip;
1503cb76bbc4SDaniel Henrique Barboza         powerpc_set_excp_state(cpu, env->spr[SPR_EBBHR], env->msr);
1504cb76bbc4SDaniel Henrique Barboza 
1505cb76bbc4SDaniel Henrique Barboza         /*
1506cb76bbc4SDaniel Henrique Barboza          * This exception is handled in userspace. No need to proceed.
1507cb76bbc4SDaniel Henrique Barboza          */
1508cb76bbc4SDaniel Henrique Barboza         return;
15099f338e4dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
15109f338e4dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
15119f338e4dSFabiano Rosas     case POWERPC_EXCP_MAINT:     /* Maintenance exception                    */
151230c4e426SFabiano Rosas     case POWERPC_EXCP_HV_MAINT:  /* Hypervisor Maintenance exception         */
1513bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
15149f338e4dSFabiano Rosas                   powerpc_excp_name(excp));
15159f338e4dSFabiano Rosas         break;
15169f338e4dSFabiano Rosas     default:
1517bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1518bc30c1c6SBALATON Zoltan                   excp);
15199f338e4dSFabiano Rosas         break;
15209f338e4dSFabiano Rosas     }
15219f338e4dSFabiano Rosas 
15229f338e4dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
15239f338e4dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
15249f338e4dSFabiano Rosas     }
15259f338e4dSFabiano Rosas     new_msr |= (target_ulong)1 << MSR_SF;
15269f338e4dSFabiano Rosas 
15279f338e4dSFabiano Rosas     if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
15289f338e4dSFabiano Rosas         env->spr[srr0] = env->nip;
15299f338e4dSFabiano Rosas         env->spr[srr1] = msr;
15309f338e4dSFabiano Rosas     }
15319f338e4dSFabiano Rosas 
15327cebc5dbSNicholas Piggin     if ((new_msr & MSR_HVB) && books_vhyp_handles_hv_excp(cpu)) {
15337cebc5dbSNicholas Piggin         /* Deliver interrupt to L1 by returning from the H_ENTER_NESTED call */
1534c700b5e1SNicholas Piggin         cpu->vhyp_class->deliver_hv_excp(cpu, excp);
15357cebc5dbSNicholas Piggin         powerpc_reset_excp_state(cpu);
15367cebc5dbSNicholas Piggin     } else {
15377cebc5dbSNicholas Piggin         /* Sanity check */
15387cebc5dbSNicholas Piggin         if (!(env->msr_mask & MSR_HVB) && srr0 == SPR_HSRR0) {
1539bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Trying to deliver HV exception (HSRR) %d "
1540bc30c1c6SBALATON Zoltan                       "with no HV support\n", excp);
15417cebc5dbSNicholas Piggin         }
15429f338e4dSFabiano Rosas         /* This can update new_msr and vector if AIL applies */
154310895ab6SFabiano Rosas         ppc_excp_apply_ail(cpu, excp, msr, &new_msr, &vector);
15449f338e4dSFabiano Rosas         powerpc_set_excp_state(cpu, vector, new_msr);
15459f338e4dSFabiano Rosas     }
15467cebc5dbSNicholas Piggin }
154730c4e426SFabiano Rosas #else
powerpc_excp_books(PowerPCCPU * cpu,int excp)154830c4e426SFabiano Rosas static inline void powerpc_excp_books(PowerPCCPU *cpu, int excp)
154930c4e426SFabiano Rosas {
155030c4e426SFabiano Rosas     g_assert_not_reached();
155130c4e426SFabiano Rosas }
1552f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
15539f338e4dSFabiano Rosas 
powerpc_excp(PowerPCCPU * cpu,int excp)155492c787deSPhilippe Mathieu-Daudé void powerpc_excp(PowerPCCPU *cpu, int excp)
1555dc88dd0aSFabiano Rosas {
1556dc88dd0aSFabiano Rosas     CPUPPCState *env = &cpu->env;
1557dc88dd0aSFabiano Rosas 
1558c6eaac89SFabiano Rosas     if (excp <= POWERPC_EXCP_NONE || excp >= POWERPC_EXCP_NB) {
1559bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1560bc30c1c6SBALATON Zoltan                   excp);
1561c6eaac89SFabiano Rosas     }
1562c6eaac89SFabiano Rosas 
1563c6eaac89SFabiano Rosas     qemu_log_mask(CPU_LOG_INT, "Raise exception at " TARGET_FMT_lx
1564c6eaac89SFabiano Rosas                   " => %s (%d) error=%02x\n", env->nip, powerpc_excp_name(excp),
1565c6eaac89SFabiano Rosas                   excp, env->error_code);
15668a15cceeSBALATON Zoltan     env->excp_stats[excp]++;
1567c6eaac89SFabiano Rosas 
1568dc88dd0aSFabiano Rosas     switch (env->excp_model) {
1569e808c2edSFabiano Rosas     case POWERPC_EXCP_40x:
1570e808c2edSFabiano Rosas         powerpc_excp_40x(cpu, excp);
1571e808c2edSFabiano Rosas         break;
157258d178fbSFabiano Rosas     case POWERPC_EXCP_6xx:
157358d178fbSFabiano Rosas         powerpc_excp_6xx(cpu, excp);
157458d178fbSFabiano Rosas         break;
1575ccfca2fcSFabiano Rosas     case POWERPC_EXCP_7xx:
1576ccfca2fcSFabiano Rosas         powerpc_excp_7xx(cpu, excp);
1577ccfca2fcSFabiano Rosas         break;
157852926b0dSFabiano Rosas     case POWERPC_EXCP_74xx:
157952926b0dSFabiano Rosas         powerpc_excp_74xx(cpu, excp);
158052926b0dSFabiano Rosas         break;
1581180952ceSFabiano Rosas     case POWERPC_EXCP_BOOKE:
1582180952ceSFabiano Rosas         powerpc_excp_booke(cpu, excp);
1583180952ceSFabiano Rosas         break;
15849f338e4dSFabiano Rosas     case POWERPC_EXCP_970:
15859f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER7:
15869f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER8:
15879f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER9:
15889f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER10:
1589c0d96407SAditya Gupta     case POWERPC_EXCP_POWER11:
15909f338e4dSFabiano Rosas         powerpc_excp_books(cpu, excp);
15919f338e4dSFabiano Rosas         break;
1592dc88dd0aSFabiano Rosas     default:
159328091374SFabiano Rosas         g_assert_not_reached();
1594dc88dd0aSFabiano Rosas     }
1595dc88dd0aSFabiano Rosas }
1596dc88dd0aSFabiano Rosas 
ppc_cpu_do_interrupt(CPUState * cs)159797a8ea5aSAndreas Färber void ppc_cpu_do_interrupt(CPUState *cs)
1598c79c73f6SBlue Swirl {
159997a8ea5aSAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
16005c26a5b3SAndreas Färber 
160193130c84SFabiano Rosas     powerpc_excp(cpu, cs->exception_index);
1602c79c73f6SBlue Swirl }
1603c79c73f6SBlue Swirl 
1604f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
1605c8e1de2eSMatheus Ferst #define P7_UNUSED_INTERRUPTS \
1606c8e1de2eSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_HVIRT | PPC_INTERRUPT_CEXT |       \
1607c8e1de2eSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |      \
1608c8e1de2eSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_DOORBELL | PPC_INTERRUPT_HDOORBELL | \
1609c8e1de2eSMatheus Ferst      PPC_INTERRUPT_THERM | PPC_INTERRUPT_EBB)
1610c8e1de2eSMatheus Ferst 
p7_interrupt_powersave(uint32_t pending_interrupts,target_ulong lpcr)1611085cc364SHarsh Prateek Bora static int p7_interrupt_powersave(uint32_t pending_interrupts,
1612085cc364SHarsh Prateek Bora                                   target_ulong lpcr)
16139c713713SMatheus Ferst {
1614085cc364SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_EXT) &&
1615085cc364SHarsh Prateek Bora         (lpcr & LPCR_P7_PECE0)) {
16169c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
16179c713713SMatheus Ferst     }
1618085cc364SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DECR) &&
1619085cc364SHarsh Prateek Bora         (lpcr & LPCR_P7_PECE1)) {
16209c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
16219c713713SMatheus Ferst     }
1622085cc364SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_MCK) &&
1623085cc364SHarsh Prateek Bora         (lpcr & LPCR_P7_PECE2)) {
16249c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
16259c713713SMatheus Ferst     }
1626085cc364SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HMI) &&
1627085cc364SHarsh Prateek Bora         (lpcr & LPCR_P7_PECE2)) {
16289c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
16299c713713SMatheus Ferst     }
1630085cc364SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_RESET) {
16319c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
16329c713713SMatheus Ferst     }
16339c713713SMatheus Ferst     return 0;
16349c713713SMatheus Ferst }
16359c713713SMatheus Ferst 
p7_next_unmasked_interrupt(CPUPPCState * env,uint32_t pending_interrupts,target_ulong lpcr)1636085cc364SHarsh Prateek Bora static int p7_next_unmasked_interrupt(CPUPPCState *env,
1637085cc364SHarsh Prateek Bora                                       uint32_t pending_interrupts,
1638085cc364SHarsh Prateek Bora                                       target_ulong lpcr)
1639bf303fb3SMatheus Ferst {
16404e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
16414e6b7db2SBALATON Zoltan 
1642022b7128SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
1643022b7128SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1644bf303fb3SMatheus Ferst 
1645085cc364SHarsh Prateek Bora     assert((pending_interrupts & P7_UNUSED_INTERRUPTS) == 0);
1646c8e1de2eSMatheus Ferst 
1647022b7128SMatheus Ferst     if (cs->halted) {
1648022b7128SMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
1649085cc364SHarsh Prateek Bora         return p7_interrupt_powersave(pending_interrupts, lpcr);
1650022b7128SMatheus Ferst     }
1651022b7128SMatheus Ferst 
1652bf303fb3SMatheus Ferst     /* Machine check exception */
1653085cc364SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_MCK) {
1654bf303fb3SMatheus Ferst         return PPC_INTERRUPT_MCK;
1655bf303fb3SMatheus Ferst     }
1656bf303fb3SMatheus Ferst 
1657bf303fb3SMatheus Ferst     /* Hypervisor decrementer exception */
1658085cc364SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HDECR) {
1659bf303fb3SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1660bf303fb3SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
1661022b7128SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1662bf303fb3SMatheus Ferst             /* HDEC clears on delivery */
1663bf303fb3SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1664bf303fb3SMatheus Ferst         }
1665bf303fb3SMatheus Ferst     }
1666bf303fb3SMatheus Ferst 
1667bf303fb3SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1668085cc364SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_EXT) {
1669085cc364SHarsh Prateek Bora         bool lpes0 = !!(lpcr & LPCR_LPES0);
1670085cc364SHarsh Prateek Bora         bool heic = !!(lpcr & LPCR_HEIC);
1671bf303fb3SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
1672022b7128SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1673bf303fb3SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1674bf303fb3SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1675bf303fb3SMatheus Ferst             return PPC_INTERRUPT_EXT;
1676bf303fb3SMatheus Ferst         }
1677bf303fb3SMatheus Ferst     }
1678022b7128SMatheus Ferst     if (msr_ee != 0) {
1679bf303fb3SMatheus Ferst         /* Decrementer exception */
1680085cc364SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DECR) {
1681bf303fb3SMatheus Ferst             return PPC_INTERRUPT_DECR;
1682bf303fb3SMatheus Ferst         }
1683085cc364SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_PERFM) {
1684bf303fb3SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1685bf303fb3SMatheus Ferst         }
1686bf303fb3SMatheus Ferst     }
1687bf303fb3SMatheus Ferst 
1688bf303fb3SMatheus Ferst     return 0;
1689bf303fb3SMatheus Ferst }
1690bf303fb3SMatheus Ferst 
1691f6194fddSMatheus Ferst #define P8_UNUSED_INTERRUPTS \
1692f6194fddSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_HVIRT |  \
1693f6194fddSMatheus Ferst     PPC_INTERRUPT_CEXT | PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL |  \
1694f6194fddSMatheus Ferst     PPC_INTERRUPT_FIT | PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1695f6194fddSMatheus Ferst 
p8_interrupt_powersave(uint32_t pending_interrupts,target_ulong lpcr)169637e62398SHarsh Prateek Bora static int p8_interrupt_powersave(uint32_t pending_interrupts,
169737e62398SHarsh Prateek Bora                                   target_ulong lpcr)
16989c713713SMatheus Ferst {
169937e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_EXT) &&
170037e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE2)) {
17019c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
17029c713713SMatheus Ferst     }
170337e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DECR) &&
170437e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE3)) {
17059c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
17069c713713SMatheus Ferst     }
170737e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_MCK) &&
170837e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE4)) {
17099c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
17109c713713SMatheus Ferst     }
171137e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HMI) &&
171237e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE4)) {
17139c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
17149c713713SMatheus Ferst     }
171537e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
171637e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE0)) {
17179c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
17189c713713SMatheus Ferst     }
171937e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
172037e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE1)) {
17219c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
17229c713713SMatheus Ferst     }
172337e62398SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_RESET) {
17249c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
17259c713713SMatheus Ferst     }
17269c713713SMatheus Ferst     return 0;
17279c713713SMatheus Ferst }
17289c713713SMatheus Ferst 
p8_next_unmasked_interrupt(CPUPPCState * env,uint32_t pending_interrupts,target_ulong lpcr)172937e62398SHarsh Prateek Bora static int p8_next_unmasked_interrupt(CPUPPCState *env,
173037e62398SHarsh Prateek Bora                                       uint32_t pending_interrupts,
173137e62398SHarsh Prateek Bora                                       target_ulong lpcr)
1732a9899d42SMatheus Ferst {
17334e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
17344e6b7db2SBALATON Zoltan 
173564a9b5eeSMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
173664a9b5eeSMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1737a9899d42SMatheus Ferst 
1738f6194fddSMatheus Ferst     assert((env->pending_interrupts & P8_UNUSED_INTERRUPTS) == 0);
1739f6194fddSMatheus Ferst 
174064a9b5eeSMatheus Ferst     if (cs->halted) {
174164a9b5eeSMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
174237e62398SHarsh Prateek Bora         return p8_interrupt_powersave(pending_interrupts, lpcr);
174364a9b5eeSMatheus Ferst     }
174464a9b5eeSMatheus Ferst 
1745a9899d42SMatheus Ferst     /* Machine check exception */
174637e62398SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_MCK) {
1747a9899d42SMatheus Ferst         return PPC_INTERRUPT_MCK;
1748a9899d42SMatheus Ferst     }
1749a9899d42SMatheus Ferst 
1750a9899d42SMatheus Ferst     /* Hypervisor decrementer exception */
175137e62398SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HDECR) {
1752a9899d42SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
175337e62398SHarsh Prateek Bora         bool hdice = !!(lpcr & LPCR_HDICE);
175464a9b5eeSMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1755a9899d42SMatheus Ferst             /* HDEC clears on delivery */
1756a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1757a9899d42SMatheus Ferst         }
1758a9899d42SMatheus Ferst     }
1759a9899d42SMatheus Ferst 
1760a9899d42SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
176137e62398SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_EXT) {
176237e62398SHarsh Prateek Bora         bool lpes0 = !!(lpcr & LPCR_LPES0);
176337e62398SHarsh Prateek Bora         bool heic = !!(lpcr & LPCR_HEIC);
1764a9899d42SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
176564a9b5eeSMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1766a9899d42SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1767a9899d42SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1768a9899d42SMatheus Ferst             return PPC_INTERRUPT_EXT;
1769a9899d42SMatheus Ferst         }
1770a9899d42SMatheus Ferst     }
177164a9b5eeSMatheus Ferst     if (msr_ee != 0) {
1772a9899d42SMatheus Ferst         /* Decrementer exception */
177337e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DECR) {
1774a9899d42SMatheus Ferst             return PPC_INTERRUPT_DECR;
1775a9899d42SMatheus Ferst         }
177637e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1777a9899d42SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
1778a9899d42SMatheus Ferst         }
177937e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
1780a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
1781a9899d42SMatheus Ferst         }
178237e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_PERFM) {
1783a9899d42SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1784a9899d42SMatheus Ferst         }
1785a9899d42SMatheus Ferst         /* EBB exception */
178637e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_EBB) {
1787a9899d42SMatheus Ferst             /*
1788a9899d42SMatheus Ferst              * EBB exception must be taken in problem state and
1789a9899d42SMatheus Ferst              * with BESCR_GE set.
1790a9899d42SMatheus Ferst              */
1791a9899d42SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
1792a9899d42SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
1793a9899d42SMatheus Ferst                 return PPC_INTERRUPT_EBB;
1794a9899d42SMatheus Ferst             }
1795a9899d42SMatheus Ferst         }
1796a9899d42SMatheus Ferst     }
1797a9899d42SMatheus Ferst 
1798a9899d42SMatheus Ferst     return 0;
1799a9899d42SMatheus Ferst }
1800a9899d42SMatheus Ferst 
1801b00e9a2fSMatheus Ferst #define P9_UNUSED_INTERRUPTS \
1802b00e9a2fSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT |   \
1803b00e9a2fSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |  \
1804b00e9a2fSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1805b00e9a2fSMatheus Ferst 
p9_interrupt_powersave(CPUPPCState * env,uint32_t pending_interrupts,target_ulong lpcr)18062a05a63cSHarsh Prateek Bora static int p9_interrupt_powersave(CPUPPCState *env,
18072a05a63cSHarsh Prateek Bora                                   uint32_t pending_interrupts,
18082a05a63cSHarsh Prateek Bora                                   target_ulong lpcr)
18099c713713SMatheus Ferst {
18102a05a63cSHarsh Prateek Bora 
18119c713713SMatheus Ferst     /* External Exception */
18122a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_EXT) &&
18132a05a63cSHarsh Prateek Bora         (lpcr & LPCR_EEE)) {
18142a05a63cSHarsh Prateek Bora         bool heic = !!(lpcr & LPCR_HEIC);
18159c713713SMatheus Ferst         if (!heic || !FIELD_EX64_HV(env->msr) ||
18169c713713SMatheus Ferst             FIELD_EX64(env->msr, MSR, PR)) {
18179c713713SMatheus Ferst             return PPC_INTERRUPT_EXT;
18189c713713SMatheus Ferst         }
18199c713713SMatheus Ferst     }
18209c713713SMatheus Ferst     /* Decrementer Exception */
18212a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DECR) &&
18222a05a63cSHarsh Prateek Bora         (lpcr & LPCR_DEE)) {
18239c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
18249c713713SMatheus Ferst     }
18259c713713SMatheus Ferst     /* Machine Check or Hypervisor Maintenance Exception */
18262a05a63cSHarsh Prateek Bora     if (lpcr & LPCR_OEE) {
18272a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_MCK) {
18289c713713SMatheus Ferst             return PPC_INTERRUPT_MCK;
18299c713713SMatheus Ferst         }
18302a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_HMI) {
18319c713713SMatheus Ferst             return PPC_INTERRUPT_HMI;
18329c713713SMatheus Ferst         }
18339c713713SMatheus Ferst     }
18349c713713SMatheus Ferst     /* Privileged Doorbell Exception */
18352a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
18362a05a63cSHarsh Prateek Bora         (lpcr & LPCR_PDEE)) {
18379c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
18389c713713SMatheus Ferst     }
18399c713713SMatheus Ferst     /* Hypervisor Doorbell Exception */
18402a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
18412a05a63cSHarsh Prateek Bora         (lpcr & LPCR_HDEE)) {
18429c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
18439c713713SMatheus Ferst     }
18449c713713SMatheus Ferst     /* Hypervisor virtualization exception */
18452a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HVIRT) &&
18462a05a63cSHarsh Prateek Bora         (lpcr & LPCR_HVEE)) {
18479c713713SMatheus Ferst         return PPC_INTERRUPT_HVIRT;
18489c713713SMatheus Ferst     }
18492a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_RESET) {
18509c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
18519c713713SMatheus Ferst     }
18529c713713SMatheus Ferst     return 0;
18539c713713SMatheus Ferst }
18549c713713SMatheus Ferst 
p9_next_unmasked_interrupt(CPUPPCState * env,uint32_t pending_interrupts,target_ulong lpcr)18552a05a63cSHarsh Prateek Bora static int p9_next_unmasked_interrupt(CPUPPCState *env,
18562a05a63cSHarsh Prateek Bora                                       uint32_t pending_interrupts,
18572a05a63cSHarsh Prateek Bora                                       target_ulong lpcr)
18582dfecf01SMatheus Ferst {
18594e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
18604e6b7db2SBALATON Zoltan 
186127796411SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
186227796411SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
18632dfecf01SMatheus Ferst 
18642a05a63cSHarsh Prateek Bora     assert((pending_interrupts & P9_UNUSED_INTERRUPTS) == 0);
1865b00e9a2fSMatheus Ferst 
186627796411SMatheus Ferst     if (cs->halted) {
186727796411SMatheus Ferst         if (env->spr[SPR_PSSCR] & PSSCR_EC) {
186827796411SMatheus Ferst             /*
186927796411SMatheus Ferst              * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can
187027796411SMatheus Ferst              * wakeup the processor
187127796411SMatheus Ferst              */
18722a05a63cSHarsh Prateek Bora             return p9_interrupt_powersave(env, pending_interrupts, lpcr);
187327796411SMatheus Ferst         } else {
187427796411SMatheus Ferst             /*
187527796411SMatheus Ferst              * When it's clear, any system-caused exception exits power-saving
187627796411SMatheus Ferst              * mode, even the ones that gate on MSR[EE].
187727796411SMatheus Ferst              */
187827796411SMatheus Ferst             msr_ee = true;
187927796411SMatheus Ferst         }
188027796411SMatheus Ferst     }
188127796411SMatheus Ferst 
18822dfecf01SMatheus Ferst     /* Machine check exception */
18832a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_MCK) {
18842dfecf01SMatheus Ferst         return PPC_INTERRUPT_MCK;
18852dfecf01SMatheus Ferst     }
18862dfecf01SMatheus Ferst 
18872dfecf01SMatheus Ferst     /* Hypervisor decrementer exception */
18882a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HDECR) {
18892dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
18902a05a63cSHarsh Prateek Bora         bool hdice = !!(lpcr & LPCR_HDICE);
189127796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
18922dfecf01SMatheus Ferst             /* HDEC clears on delivery */
18932dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDECR;
18942dfecf01SMatheus Ferst         }
18952dfecf01SMatheus Ferst     }
18962dfecf01SMatheus Ferst 
18972dfecf01SMatheus Ferst     /* Hypervisor virtualization interrupt */
18982a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HVIRT) {
18992dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
19002a05a63cSHarsh Prateek Bora         bool hvice = !!(lpcr & LPCR_HVICE);
190127796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hvice) {
19022dfecf01SMatheus Ferst             return PPC_INTERRUPT_HVIRT;
19032dfecf01SMatheus Ferst         }
19042dfecf01SMatheus Ferst     }
19052dfecf01SMatheus Ferst 
19062dfecf01SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
19072a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_EXT) {
19082a05a63cSHarsh Prateek Bora         bool lpes0 = !!(lpcr & LPCR_LPES0);
19092a05a63cSHarsh Prateek Bora         bool heic = !!(lpcr & LPCR_HEIC);
19102dfecf01SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
191127796411SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
19122dfecf01SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
19132dfecf01SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
19142dfecf01SMatheus Ferst             return PPC_INTERRUPT_EXT;
19152dfecf01SMatheus Ferst         }
19162dfecf01SMatheus Ferst     }
191727796411SMatheus Ferst     if (msr_ee != 0) {
19182dfecf01SMatheus Ferst         /* Decrementer exception */
19192a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DECR) {
19202dfecf01SMatheus Ferst             return PPC_INTERRUPT_DECR;
19212dfecf01SMatheus Ferst         }
19222a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DOORBELL) {
19232dfecf01SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
19242dfecf01SMatheus Ferst         }
19252a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
19262dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
19272dfecf01SMatheus Ferst         }
19282a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_PERFM) {
19292dfecf01SMatheus Ferst             return PPC_INTERRUPT_PERFM;
19302dfecf01SMatheus Ferst         }
19312dfecf01SMatheus Ferst         /* EBB exception */
19322a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_EBB) {
19332dfecf01SMatheus Ferst             /*
19342dfecf01SMatheus Ferst              * EBB exception must be taken in problem state and
19352dfecf01SMatheus Ferst              * with BESCR_GE set.
19362dfecf01SMatheus Ferst              */
19372dfecf01SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
19382dfecf01SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
19392dfecf01SMatheus Ferst                 return PPC_INTERRUPT_EBB;
19402dfecf01SMatheus Ferst             }
19412dfecf01SMatheus Ferst         }
19422dfecf01SMatheus Ferst     }
19432dfecf01SMatheus Ferst 
19442dfecf01SMatheus Ferst     return 0;
19452dfecf01SMatheus Ferst }
1946f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
19472dfecf01SMatheus Ferst 
ppc_next_unmasked_interrupt(CPUPPCState * env)1948868cb6baSBALATON Zoltan static int ppc_next_unmasked_interrupt(CPUPPCState *env)
1949c79c73f6SBlue Swirl {
1950bb547c4cSHarsh Prateek Bora     uint32_t pending_interrupts = env->pending_interrupts;
1951bb547c4cSHarsh Prateek Bora     target_ulong lpcr = env->spr[SPR_LPCR];
1952bb547c4cSHarsh Prateek Bora     bool async_deliver;
1953bb547c4cSHarsh Prateek Bora 
1954fb802acdSNicholas Piggin     if (unlikely(env->quiesced)) {
1955fb802acdSNicholas Piggin         return 0;
1956fb802acdSNicholas Piggin     }
1957fb802acdSNicholas Piggin 
1958868cb6baSBALATON Zoltan #ifdef TARGET_PPC64
1959868cb6baSBALATON Zoltan     switch (env->excp_model) {
1960868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER7:
1961bb547c4cSHarsh Prateek Bora         return p7_next_unmasked_interrupt(env, pending_interrupts, lpcr);
1962868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER8:
1963bb547c4cSHarsh Prateek Bora         return p8_next_unmasked_interrupt(env, pending_interrupts, lpcr);
1964868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER9:
1965868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER10:
1966c0d96407SAditya Gupta     case POWERPC_EXCP_POWER11:
1967bb547c4cSHarsh Prateek Bora         return p9_next_unmasked_interrupt(env, pending_interrupts, lpcr);
1968868cb6baSBALATON Zoltan     default:
1969868cb6baSBALATON Zoltan         break;
1970868cb6baSBALATON Zoltan     }
1971868cb6baSBALATON Zoltan #endif
1972259186a7SAndreas Färber 
1973c79c73f6SBlue Swirl     /* External reset */
1974bb547c4cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_RESET) {
1975de76b85cSMatheus Ferst         return PPC_INTERRUPT_RESET;
1976c79c73f6SBlue Swirl     }
1977c79c73f6SBlue Swirl     /* Machine check exception */
1978bb547c4cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_MCK) {
1979de76b85cSMatheus Ferst         return PPC_INTERRUPT_MCK;
1980c79c73f6SBlue Swirl     }
1981c79c73f6SBlue Swirl #if 0 /* TODO */
1982c79c73f6SBlue Swirl     /* External debug exception */
1983f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) {
1984de76b85cSMatheus Ferst         return PPC_INTERRUPT_DEBUG;
1985c79c73f6SBlue Swirl     }
1986c79c73f6SBlue Swirl #endif
19873621e2c9SBenjamin Herrenschmidt 
19883621e2c9SBenjamin Herrenschmidt     /*
19893621e2c9SBenjamin Herrenschmidt      * For interrupts that gate on MSR:EE, we need to do something a
19903621e2c9SBenjamin Herrenschmidt      * bit more subtle, as we need to let them through even when EE is
19913621e2c9SBenjamin Herrenschmidt      * clear when coming out of some power management states (in order
19923621e2c9SBenjamin Herrenschmidt      * for them to become a 0x100).
19933621e2c9SBenjamin Herrenschmidt      */
19940939b8f8SVíctor Colombo     async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
19953621e2c9SBenjamin Herrenschmidt 
1996c79c73f6SBlue Swirl     /* Hypervisor decrementer exception */
1997bb547c4cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HDECR) {
19984b236b62SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
1999bb547c4cSHarsh Prateek Bora         bool hdice = !!(lpcr & LPCR_HDICE);
20009de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
20014b236b62SBenjamin Herrenschmidt             /* HDEC clears on delivery */
2002de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDECR;
2003c79c73f6SBlue Swirl         }
2004c79c73f6SBlue Swirl     }
2005d8ce5fd6SBenjamin Herrenschmidt 
2006d8ce5fd6SBenjamin Herrenschmidt     /* Hypervisor virtualization interrupt */
2007bb547c4cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HVIRT) {
2008d8ce5fd6SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
2009bb547c4cSHarsh Prateek Bora         bool hvice = !!(lpcr & LPCR_HVICE);
20109de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
2011de76b85cSMatheus Ferst             return PPC_INTERRUPT_HVIRT;
2012d8ce5fd6SBenjamin Herrenschmidt         }
2013d8ce5fd6SBenjamin Herrenschmidt     }
2014d8ce5fd6SBenjamin Herrenschmidt 
2015d8ce5fd6SBenjamin Herrenschmidt     /* External interrupt can ignore MSR:EE under some circumstances */
2016bb547c4cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_EXT) {
2017bb547c4cSHarsh Prateek Bora         bool lpes0 = !!(lpcr & LPCR_LPES0);
2018bb547c4cSHarsh Prateek Bora         bool heic = !!(lpcr & LPCR_HEIC);
20196eebe6dcSBenjamin Herrenschmidt         /* HEIC blocks delivery to the hypervisor */
20209de754d3SVíctor Colombo         if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
2021d41ccf6eSVíctor Colombo             !FIELD_EX64(env->msr, MSR, PR))) ||
20229de754d3SVíctor Colombo             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
2023de76b85cSMatheus Ferst             return PPC_INTERRUPT_EXT;
2024d1dbe37cSBenjamin Herrenschmidt         }
2025d1dbe37cSBenjamin Herrenschmidt     }
2026acc861c2SVíctor Colombo     if (FIELD_EX64(env->msr, MSR, CE)) {
2027c79c73f6SBlue Swirl         /* External critical interrupt */
2028bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_CEXT) {
2029de76b85cSMatheus Ferst             return PPC_INTERRUPT_CEXT;
2030c79c73f6SBlue Swirl         }
2031c79c73f6SBlue Swirl     }
20323621e2c9SBenjamin Herrenschmidt     if (async_deliver != 0) {
2033c79c73f6SBlue Swirl         /* Watchdog timer on embedded PowerPC */
2034bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_WDT) {
2035de76b85cSMatheus Ferst             return PPC_INTERRUPT_WDT;
2036c79c73f6SBlue Swirl         }
2037bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_CDOORBELL) {
2038de76b85cSMatheus Ferst             return PPC_INTERRUPT_CDOORBELL;
2039c79c73f6SBlue Swirl         }
2040c79c73f6SBlue Swirl         /* Fixed interval timer on embedded PowerPC */
2041bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_FIT) {
2042de76b85cSMatheus Ferst             return PPC_INTERRUPT_FIT;
2043c79c73f6SBlue Swirl         }
2044c79c73f6SBlue Swirl         /* Programmable interval timer on embedded PowerPC */
2045bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_PIT) {
2046de76b85cSMatheus Ferst             return PPC_INTERRUPT_PIT;
2047c79c73f6SBlue Swirl         }
2048c79c73f6SBlue Swirl         /* Decrementer exception */
2049bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DECR) {
2050de76b85cSMatheus Ferst             return PPC_INTERRUPT_DECR;
2051c79c73f6SBlue Swirl         }
2052bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2053de76b85cSMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
2054c79c73f6SBlue Swirl         }
2055bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
2056de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
20577af1e7b0SCédric Le Goater         }
2058bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_PERFM) {
2059de76b85cSMatheus Ferst             return PPC_INTERRUPT_PERFM;
2060c79c73f6SBlue Swirl         }
2061c79c73f6SBlue Swirl         /* Thermal interrupt */
2062bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_THERM) {
2063de76b85cSMatheus Ferst             return PPC_INTERRUPT_THERM;
2064c79c73f6SBlue Swirl         }
2065cb76bbc4SDaniel Henrique Barboza         /* EBB exception */
2066bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_EBB) {
2067cb76bbc4SDaniel Henrique Barboza             /*
2068cb76bbc4SDaniel Henrique Barboza              * EBB exception must be taken in problem state and
2069cb76bbc4SDaniel Henrique Barboza              * with BESCR_GE set.
2070cb76bbc4SDaniel Henrique Barboza              */
2071d41ccf6eSVíctor Colombo             if (FIELD_EX64(env->msr, MSR, PR) &&
2072d41ccf6eSVíctor Colombo                 (env->spr[SPR_BESCR] & BESCR_GE)) {
2073de76b85cSMatheus Ferst                 return PPC_INTERRUPT_EBB;
2074de76b85cSMatheus Ferst             }
2075de76b85cSMatheus Ferst         }
2076de76b85cSMatheus Ferst     }
2077cb76bbc4SDaniel Henrique Barboza 
2078de76b85cSMatheus Ferst     return 0;
2079de76b85cSMatheus Ferst }
2080de76b85cSMatheus Ferst 
20812fdedcbcSMatheus Ferst /*
20822fdedcbcSMatheus Ferst  * Sets CPU_INTERRUPT_HARD if there is at least one unmasked interrupt to be
20832fdedcbcSMatheus Ferst  * delivered and clears CPU_INTERRUPT_HARD otherwise.
20842fdedcbcSMatheus Ferst  *
20852fdedcbcSMatheus Ferst  * This method is called by ppc_set_interrupt when an interrupt is raised or
20862fdedcbcSMatheus Ferst  * lowered, and should also be called whenever an interrupt masking condition
20872fdedcbcSMatheus Ferst  * is changed, e.g.:
20882fdedcbcSMatheus Ferst  *  - When relevant bits of MSR are altered, like EE, HV, PR, etc.;
20892fdedcbcSMatheus Ferst  *  - When relevant bits of LPCR are altered, like PECE, HDICE, HVICE, etc.;
20902fdedcbcSMatheus Ferst  *  - When PSSCR[EC] or env->resume_as_sreset are changed;
20912fdedcbcSMatheus Ferst  *  - When cs->halted is changed and the CPU has a different interrupt masking
20922fdedcbcSMatheus Ferst  *    logic in power-saving mode (e.g., POWER7/8/9/10);
20932fdedcbcSMatheus Ferst  */
ppc_maybe_interrupt(CPUPPCState * env)20942fdedcbcSMatheus Ferst void ppc_maybe_interrupt(CPUPPCState *env)
20952fdedcbcSMatheus Ferst {
20962fdedcbcSMatheus Ferst     CPUState *cs = env_cpu(env);
209732ead8e6SStefan Hajnoczi     BQL_LOCK_GUARD();
20982fdedcbcSMatheus Ferst 
20992fdedcbcSMatheus Ferst     if (ppc_next_unmasked_interrupt(env)) {
21002fdedcbcSMatheus Ferst         cpu_interrupt(cs, CPU_INTERRUPT_HARD);
21012fdedcbcSMatheus Ferst     } else {
21022fdedcbcSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
21032fdedcbcSMatheus Ferst     }
21042fdedcbcSMatheus Ferst }
21052fdedcbcSMatheus Ferst 
2106f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
p7_deliver_interrupt(CPUPPCState * env,int interrupt)2107d93a4856SMatheus Ferst static void p7_deliver_interrupt(CPUPPCState *env, int interrupt)
2108d93a4856SMatheus Ferst {
2109d93a4856SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2110d93a4856SMatheus Ferst 
2111d93a4856SMatheus Ferst     switch (interrupt) {
2112d93a4856SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2113d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2114d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2115d93a4856SMatheus Ferst         break;
2116d93a4856SMatheus Ferst 
2117d93a4856SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2118d93a4856SMatheus Ferst         /* HDEC clears on delivery */
2119d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2120d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2121d93a4856SMatheus Ferst         break;
2122d93a4856SMatheus Ferst 
2123d93a4856SMatheus Ferst     case PPC_INTERRUPT_EXT:
2124d93a4856SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2125d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2126d93a4856SMatheus Ferst         } else {
2127d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2128d93a4856SMatheus Ferst         }
2129d93a4856SMatheus Ferst         break;
2130d93a4856SMatheus Ferst 
2131d93a4856SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2132d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2133d93a4856SMatheus Ferst         break;
2134d93a4856SMatheus Ferst     case PPC_INTERRUPT_PERFM:
2135d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2136d93a4856SMatheus Ferst         break;
2137d93a4856SMatheus Ferst     case 0:
2138d93a4856SMatheus Ferst         /*
2139d93a4856SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
2140d93a4856SMatheus Ferst          * anything to deliver while in a PM state that requires getting
2141d93a4856SMatheus Ferst          * out via a 0x100
2142d93a4856SMatheus Ferst          *
2143d93a4856SMatheus Ferst          * This means we will incorrectly execute past the power management
2144d93a4856SMatheus Ferst          * instruction instead of triggering a reset.
2145d93a4856SMatheus Ferst          *
2146d93a4856SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
2147d93a4856SMatheus Ferst          * processor has_work implementation and the logic in this function.
2148d93a4856SMatheus Ferst          */
2149d93a4856SMatheus Ferst         assert(!env->resume_as_sreset);
2150d93a4856SMatheus Ferst         break;
2151d93a4856SMatheus Ferst     default:
2152bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2153bc30c1c6SBALATON Zoltan                   interrupt);
2154d93a4856SMatheus Ferst     }
2155d93a4856SMatheus Ferst }
2156d93a4856SMatheus Ferst 
p8_deliver_interrupt(CPUPPCState * env,int interrupt)21576527e757SMatheus Ferst static void p8_deliver_interrupt(CPUPPCState *env, int interrupt)
21586527e757SMatheus Ferst {
21596527e757SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
21606527e757SMatheus Ferst 
21616527e757SMatheus Ferst     switch (interrupt) {
21626527e757SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
21636527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
21646527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
21656527e757SMatheus Ferst         break;
21666527e757SMatheus Ferst 
21676527e757SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
21686527e757SMatheus Ferst         /* HDEC clears on delivery */
21696527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
21706527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
21716527e757SMatheus Ferst         break;
21726527e757SMatheus Ferst 
21736527e757SMatheus Ferst     case PPC_INTERRUPT_EXT:
21746527e757SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
21756527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
21766527e757SMatheus Ferst         } else {
21776527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
21786527e757SMatheus Ferst         }
21796527e757SMatheus Ferst         break;
21806527e757SMatheus Ferst 
21816527e757SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
21826527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
21836527e757SMatheus Ferst         break;
21846527e757SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
218506229545SNicholas Piggin         if (!env->resume_as_sreset) {
21866527e757SMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
218706229545SNicholas Piggin         }
21886527e757SMatheus Ferst         if (is_book3s_arch2x(env)) {
21896527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
21906527e757SMatheus Ferst         } else {
21916527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
21926527e757SMatheus Ferst         }
21936527e757SMatheus Ferst         break;
21946527e757SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
219506229545SNicholas Piggin         if (!env->resume_as_sreset) {
21966527e757SMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
219706229545SNicholas Piggin         }
21986527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
21996527e757SMatheus Ferst         break;
22006527e757SMatheus Ferst     case PPC_INTERRUPT_PERFM:
22016527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
22026527e757SMatheus Ferst         break;
22036527e757SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
22046527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
22056527e757SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
22066527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
22076527e757SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
22086527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
22096527e757SMatheus Ferst         }
22106527e757SMatheus Ferst         break;
22116527e757SMatheus Ferst     case 0:
22126527e757SMatheus Ferst         /*
22136527e757SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
22146527e757SMatheus Ferst          * anything to deliver while in a PM state that requires getting
22156527e757SMatheus Ferst          * out via a 0x100
22166527e757SMatheus Ferst          *
22176527e757SMatheus Ferst          * This means we will incorrectly execute past the power management
22186527e757SMatheus Ferst          * instruction instead of triggering a reset.
22196527e757SMatheus Ferst          *
22206527e757SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
22216527e757SMatheus Ferst          * processor has_work implementation and the logic in this function.
22226527e757SMatheus Ferst          */
22236527e757SMatheus Ferst         assert(!env->resume_as_sreset);
22246527e757SMatheus Ferst         break;
22256527e757SMatheus Ferst     default:
2226bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2227bc30c1c6SBALATON Zoltan                   interrupt);
22286527e757SMatheus Ferst     }
22296527e757SMatheus Ferst }
22306527e757SMatheus Ferst 
p9_deliver_interrupt(CPUPPCState * env,int interrupt)22313654e238SMatheus Ferst static void p9_deliver_interrupt(CPUPPCState *env, int interrupt)
22323654e238SMatheus Ferst {
22333654e238SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
22343654e238SMatheus Ferst     CPUState *cs = env_cpu(env);
22353654e238SMatheus Ferst 
223627796411SMatheus Ferst     if (cs->halted && !(env->spr[SPR_PSSCR] & PSSCR_EC) &&
223727796411SMatheus Ferst         !FIELD_EX64(env->msr, MSR, EE)) {
223827796411SMatheus Ferst         /*
223927796411SMatheus Ferst          * A pending interrupt took us out of power-saving, but MSR[EE] says
224027796411SMatheus Ferst          * that we should return to NIP+4 instead of delivering it.
224127796411SMatheus Ferst          */
224227796411SMatheus Ferst         return;
224327796411SMatheus Ferst     }
224427796411SMatheus Ferst 
22453654e238SMatheus Ferst     switch (interrupt) {
22463654e238SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
22473654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
22483654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
22493654e238SMatheus Ferst         break;
22503654e238SMatheus Ferst 
22513654e238SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
22523654e238SMatheus Ferst         /* HDEC clears on delivery */
225306229545SNicholas Piggin         /* XXX: should not see an HDEC if resume_as_sreset. assert? */
22543654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
22553654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
22563654e238SMatheus Ferst         break;
22573654e238SMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
22583654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
22593654e238SMatheus Ferst         break;
22603654e238SMatheus Ferst 
22613654e238SMatheus Ferst     case PPC_INTERRUPT_EXT:
22623654e238SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
22633654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
22643654e238SMatheus Ferst         } else {
22653654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
22663654e238SMatheus Ferst         }
22673654e238SMatheus Ferst         break;
22683654e238SMatheus Ferst 
22693654e238SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
22703654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
22713654e238SMatheus Ferst         break;
22723654e238SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
227306229545SNicholas Piggin         if (!env->resume_as_sreset) {
22743654e238SMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
227506229545SNicholas Piggin         }
22763654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
22773654e238SMatheus Ferst         break;
22783654e238SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
227906229545SNicholas Piggin         if (!env->resume_as_sreset) {
22803654e238SMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
228106229545SNicholas Piggin         }
22823654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
22833654e238SMatheus Ferst         break;
22843654e238SMatheus Ferst     case PPC_INTERRUPT_PERFM:
22853654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
22863654e238SMatheus Ferst         break;
22873654e238SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
22883654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
22893654e238SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
22903654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
22913654e238SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
22923654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
22933654e238SMatheus Ferst         }
22943654e238SMatheus Ferst         break;
22953654e238SMatheus Ferst     case 0:
22963654e238SMatheus Ferst         /*
22973654e238SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
22983654e238SMatheus Ferst          * anything to deliver while in a PM state that requires getting
22993654e238SMatheus Ferst          * out via a 0x100
23003654e238SMatheus Ferst          *
23013654e238SMatheus Ferst          * This means we will incorrectly execute past the power management
23023654e238SMatheus Ferst          * instruction instead of triggering a reset.
23033654e238SMatheus Ferst          *
23043654e238SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
23053654e238SMatheus Ferst          * processor has_work implementation and the logic in this function.
23063654e238SMatheus Ferst          */
23073654e238SMatheus Ferst         assert(!env->resume_as_sreset);
23083654e238SMatheus Ferst         break;
23093654e238SMatheus Ferst     default:
2310bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2311bc30c1c6SBALATON Zoltan                   interrupt);
23123654e238SMatheus Ferst     }
23133654e238SMatheus Ferst }
2314f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
23153654e238SMatheus Ferst 
ppc_deliver_interrupt(CPUPPCState * env,int interrupt)2316868cb6baSBALATON Zoltan static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt)
2317de76b85cSMatheus Ferst {
2318868cb6baSBALATON Zoltan #ifdef TARGET_PPC64
2319868cb6baSBALATON Zoltan     switch (env->excp_model) {
2320868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER7:
2321868cb6baSBALATON Zoltan         return p7_deliver_interrupt(env, interrupt);
2322868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER8:
2323868cb6baSBALATON Zoltan         return p8_deliver_interrupt(env, interrupt);
2324868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER9:
2325868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER10:
2326c0d96407SAditya Gupta     case POWERPC_EXCP_POWER11:
2327868cb6baSBALATON Zoltan         return p9_deliver_interrupt(env, interrupt);
2328868cb6baSBALATON Zoltan     default:
2329868cb6baSBALATON Zoltan         break;
2330868cb6baSBALATON Zoltan     }
2331de76b85cSMatheus Ferst #endif
2332de76b85cSMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2333de76b85cSMatheus Ferst 
2334de76b85cSMatheus Ferst     switch (interrupt) {
2335de76b85cSMatheus Ferst     case PPC_INTERRUPT_RESET: /* External reset */
2336de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_RESET;
2337de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_RESET);
2338de76b85cSMatheus Ferst         break;
2339de76b85cSMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2340de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2341de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2342de76b85cSMatheus Ferst         break;
2343de76b85cSMatheus Ferst 
2344de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2345de76b85cSMatheus Ferst         /* HDEC clears on delivery */
2346de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2347de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2348de76b85cSMatheus Ferst         break;
2349de76b85cSMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
2350de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2351de76b85cSMatheus Ferst         break;
2352de76b85cSMatheus Ferst 
2353de76b85cSMatheus Ferst     case PPC_INTERRUPT_EXT:
2354de76b85cSMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2355de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2356de76b85cSMatheus Ferst         } else {
2357de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2358de76b85cSMatheus Ferst         }
2359de76b85cSMatheus Ferst         break;
2360de76b85cSMatheus Ferst     case PPC_INTERRUPT_CEXT: /* External critical interrupt */
2361de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_CRITICAL);
2362de76b85cSMatheus Ferst         break;
2363de76b85cSMatheus Ferst 
2364de76b85cSMatheus Ferst     case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */
2365de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_WDT;
2366de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_WDT);
2367de76b85cSMatheus Ferst         break;
2368de76b85cSMatheus Ferst     case PPC_INTERRUPT_CDOORBELL:
2369de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL;
2370de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DOORCI);
2371de76b85cSMatheus Ferst         break;
2372de76b85cSMatheus Ferst     case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */
2373de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_FIT;
2374de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_FIT);
2375de76b85cSMatheus Ferst         break;
2376de76b85cSMatheus Ferst     case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */
2377de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PIT;
2378de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PIT);
2379de76b85cSMatheus Ferst         break;
2380de76b85cSMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2381de76b85cSMatheus Ferst         if (ppc_decr_clear_on_delivery(env)) {
2382de76b85cSMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DECR;
2383de76b85cSMatheus Ferst         }
2384de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2385de76b85cSMatheus Ferst         break;
2386de76b85cSMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
2387de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
2388de76b85cSMatheus Ferst         if (is_book3s_arch2x(env)) {
2389de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
2390de76b85cSMatheus Ferst         } else {
2391de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
2392de76b85cSMatheus Ferst         }
2393de76b85cSMatheus Ferst         break;
2394de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
2395de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
2396de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
2397de76b85cSMatheus Ferst         break;
2398de76b85cSMatheus Ferst     case PPC_INTERRUPT_PERFM:
2399de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2400de76b85cSMatheus Ferst         break;
2401de76b85cSMatheus Ferst     case PPC_INTERRUPT_THERM:  /* Thermal interrupt */
2402de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_THERM;
2403de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_THERM);
2404de76b85cSMatheus Ferst         break;
2405de76b85cSMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
2406de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
2407cb76bbc4SDaniel Henrique Barboza         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
2408cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
2409cb76bbc4SDaniel Henrique Barboza         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
2410cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
2411cb76bbc4SDaniel Henrique Barboza         }
2412de76b85cSMatheus Ferst         break;
2413de76b85cSMatheus Ferst     case 0:
2414f8154fd2SBenjamin Herrenschmidt         /*
2415f8154fd2SBenjamin Herrenschmidt          * This is a bug ! It means that has_work took us out of halt without
2416f8154fd2SBenjamin Herrenschmidt          * anything to deliver while in a PM state that requires getting
2417f8154fd2SBenjamin Herrenschmidt          * out via a 0x100
2418f8154fd2SBenjamin Herrenschmidt          *
2419f8154fd2SBenjamin Herrenschmidt          * This means we will incorrectly execute past the power management
2420f8154fd2SBenjamin Herrenschmidt          * instruction instead of triggering a reset.
2421f8154fd2SBenjamin Herrenschmidt          *
2422136fbf65Szhaolichang          * It generally means a discrepancy between the wakeup conditions in the
2423f8154fd2SBenjamin Herrenschmidt          * processor has_work implementation and the logic in this function.
2424f8154fd2SBenjamin Herrenschmidt          */
2425de76b85cSMatheus Ferst         assert(!env->resume_as_sreset);
2426de76b85cSMatheus Ferst         break;
2427de76b85cSMatheus Ferst     default:
2428bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2429bc30c1c6SBALATON Zoltan                   interrupt);
2430ba2898f7SMatheus Ferst     }
2431ba2898f7SMatheus Ferst }
2432ba2898f7SMatheus Ferst 
2433fa416ae6SNicholas Piggin /*
2434fa416ae6SNicholas Piggin  * system reset is not delivered via normal irq method, so have to set
2435fa416ae6SNicholas Piggin  * halted = 0 to resume CPU running if it was halted. Possibly we should
2436fa416ae6SNicholas Piggin  * move it over to using PPC_INTERRUPT_RESET rather than async_run_on_cpu.
2437fa416ae6SNicholas Piggin  */
ppc_cpu_do_system_reset(CPUState * cs)2438b5b7f391SNicholas Piggin void ppc_cpu_do_system_reset(CPUState *cs)
243934316482SAlexey Kardashevskiy {
244034316482SAlexey Kardashevskiy     PowerPCCPU *cpu = POWERPC_CPU(cs);
244134316482SAlexey Kardashevskiy 
2442fa416ae6SNicholas Piggin     cs->halted = 0;
244393130c84SFabiano Rosas     powerpc_excp(cpu, POWERPC_EXCP_RESET);
244434316482SAlexey Kardashevskiy }
2445ad77c6caSNicholas Piggin 
ppc_cpu_do_fwnmi_machine_check(CPUState * cs,target_ulong vector)2446ad77c6caSNicholas Piggin void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector)
2447ad77c6caSNicholas Piggin {
2448ad77c6caSNicholas Piggin     PowerPCCPU *cpu = POWERPC_CPU(cs);
2449ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
2450ad77c6caSNicholas Piggin     target_ulong msr = 0;
2451ad77c6caSNicholas Piggin 
2452ad77c6caSNicholas Piggin     /*
2453ad77c6caSNicholas Piggin      * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already
2454ad77c6caSNicholas Piggin      * been set by KVM.
2455ad77c6caSNicholas Piggin      */
2456ad77c6caSNicholas Piggin     msr = (1ULL << MSR_ME);
2457ad77c6caSNicholas Piggin     msr |= env->msr & (1ULL << MSR_SF);
2458516fc103SFabiano Rosas     if (ppc_interrupts_little_endian(cpu, false)) {
2459ad77c6caSNicholas Piggin         msr |= (1ULL << MSR_LE);
2460ad77c6caSNicholas Piggin     }
2461ad77c6caSNicholas Piggin 
24627cebc5dbSNicholas Piggin     /* Anything for nested required here? MSR[HV] bit? */
24637cebc5dbSNicholas Piggin 
2464fa416ae6SNicholas Piggin     cs->halted = 0;
2465ad77c6caSNicholas Piggin     powerpc_set_excp_state(cpu, vector, msr);
2466ad77c6caSNicholas Piggin }
2467c79c73f6SBlue Swirl 
ppc_cpu_exec_interrupt(CPUState * cs,int interrupt_request)2468458dd766SRichard Henderson bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
2469458dd766SRichard Henderson {
2470794511bcSPhilippe Mathieu-Daudé     CPUPPCState *env = cpu_env(cs);
2471de76b85cSMatheus Ferst     int interrupt;
2472458dd766SRichard Henderson 
2473de76b85cSMatheus Ferst     if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) {
2474de76b85cSMatheus Ferst         return false;
2475de76b85cSMatheus Ferst     }
2476de76b85cSMatheus Ferst 
2477de76b85cSMatheus Ferst     interrupt = ppc_next_unmasked_interrupt(env);
2478de76b85cSMatheus Ferst     if (interrupt == 0) {
2479de76b85cSMatheus Ferst         return false;
2480de76b85cSMatheus Ferst     }
2481de76b85cSMatheus Ferst 
2482de76b85cSMatheus Ferst     ppc_deliver_interrupt(env, interrupt);
2483458dd766SRichard Henderson     if (env->pending_interrupts == 0) {
2484de76b85cSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
2485458dd766SRichard Henderson     }
2486458dd766SRichard Henderson     return true;
2487458dd766SRichard Henderson }
2488458dd766SRichard Henderson 
2489f725245cSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
2490