xref: /qemu/target/ppc/excp_helper.c (revision 434531619fecd5ff9a08e548ed87b2b73c29cf3e)
1ad71ed68SBlue Swirl /*
2ad71ed68SBlue Swirl  *  PowerPC exception emulation helpers for QEMU.
3ad71ed68SBlue Swirl  *
4ad71ed68SBlue Swirl  *  Copyright (c) 2003-2007 Jocelyn Mayer
5ad71ed68SBlue Swirl  *
6ad71ed68SBlue Swirl  * This library is free software; you can redistribute it and/or
7ad71ed68SBlue Swirl  * modify it under the terms of the GNU Lesser General Public
8ad71ed68SBlue Swirl  * License as published by the Free Software Foundation; either
96bd039cdSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
10ad71ed68SBlue Swirl  *
11ad71ed68SBlue Swirl  * This library is distributed in the hope that it will be useful,
12ad71ed68SBlue Swirl  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13ad71ed68SBlue Swirl  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14ad71ed68SBlue Swirl  * Lesser General Public License for more details.
15ad71ed68SBlue Swirl  *
16ad71ed68SBlue Swirl  * You should have received a copy of the GNU Lesser General Public
17ad71ed68SBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18ad71ed68SBlue Swirl  */
190d75590dSPeter Maydell #include "qemu/osdep.h"
20f1c29ebcSThomas Huth #include "qemu/main-loop.h"
21cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h"
22ad71ed68SBlue Swirl #include "cpu.h"
2363c91552SPaolo Bonzini #include "exec/exec-all.h"
240f3110faSRichard Henderson #include "internal.h"
25ad71ed68SBlue Swirl #include "helper_regs.h"
267b694df6SMatheus Ferst #include "hw/ppc/ppc.h"
27ad71ed68SBlue Swirl 
282eb1ef73SCédric Le Goater #include "trace.h"
292eb1ef73SCédric Le Goater 
302b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
315a5d3b23SNicholas Piggin #include "sysemu/tcg.h"
322b44e219SBruno Larsen (billionai) #include "exec/helper-proto.h"
332b44e219SBruno Larsen (billionai) #include "exec/cpu_ldst.h"
342b44e219SBruno Larsen (billionai) #endif
352b44e219SBruno Larsen (billionai) 
36c79c73f6SBlue Swirl /*****************************************************************************/
37c79c73f6SBlue Swirl /* Exception processing */
38f6c2d68bSBALATON Zoltan #ifndef CONFIG_USER_ONLY
3997a8ea5aSAndreas Färber 
406789f23bSCédric Le Goater static const char *powerpc_excp_name(int excp)
416789f23bSCédric Le Goater {
426789f23bSCédric Le Goater     switch (excp) {
436789f23bSCédric Le Goater     case POWERPC_EXCP_CRITICAL: return "CRITICAL";
446789f23bSCédric Le Goater     case POWERPC_EXCP_MCHECK:   return "MCHECK";
456789f23bSCédric Le Goater     case POWERPC_EXCP_DSI:      return "DSI";
466789f23bSCédric Le Goater     case POWERPC_EXCP_ISI:      return "ISI";
476789f23bSCédric Le Goater     case POWERPC_EXCP_EXTERNAL: return "EXTERNAL";
486789f23bSCédric Le Goater     case POWERPC_EXCP_ALIGN:    return "ALIGN";
496789f23bSCédric Le Goater     case POWERPC_EXCP_PROGRAM:  return "PROGRAM";
506789f23bSCédric Le Goater     case POWERPC_EXCP_FPU:      return "FPU";
516789f23bSCédric Le Goater     case POWERPC_EXCP_SYSCALL:  return "SYSCALL";
526789f23bSCédric Le Goater     case POWERPC_EXCP_APU:      return "APU";
536789f23bSCédric Le Goater     case POWERPC_EXCP_DECR:     return "DECR";
546789f23bSCédric Le Goater     case POWERPC_EXCP_FIT:      return "FIT";
556789f23bSCédric Le Goater     case POWERPC_EXCP_WDT:      return "WDT";
566789f23bSCédric Le Goater     case POWERPC_EXCP_DTLB:     return "DTLB";
576789f23bSCédric Le Goater     case POWERPC_EXCP_ITLB:     return "ITLB";
586789f23bSCédric Le Goater     case POWERPC_EXCP_DEBUG:    return "DEBUG";
596789f23bSCédric Le Goater     case POWERPC_EXCP_SPEU:     return "SPEU";
606789f23bSCédric Le Goater     case POWERPC_EXCP_EFPDI:    return "EFPDI";
616789f23bSCédric Le Goater     case POWERPC_EXCP_EFPRI:    return "EFPRI";
626789f23bSCédric Le Goater     case POWERPC_EXCP_EPERFM:   return "EPERFM";
636789f23bSCédric Le Goater     case POWERPC_EXCP_DOORI:    return "DOORI";
646789f23bSCédric Le Goater     case POWERPC_EXCP_DOORCI:   return "DOORCI";
656789f23bSCédric Le Goater     case POWERPC_EXCP_GDOORI:   return "GDOORI";
666789f23bSCédric Le Goater     case POWERPC_EXCP_GDOORCI:  return "GDOORCI";
676789f23bSCédric Le Goater     case POWERPC_EXCP_HYPPRIV:  return "HYPPRIV";
686789f23bSCédric Le Goater     case POWERPC_EXCP_RESET:    return "RESET";
696789f23bSCédric Le Goater     case POWERPC_EXCP_DSEG:     return "DSEG";
706789f23bSCédric Le Goater     case POWERPC_EXCP_ISEG:     return "ISEG";
716789f23bSCédric Le Goater     case POWERPC_EXCP_HDECR:    return "HDECR";
726789f23bSCédric Le Goater     case POWERPC_EXCP_TRACE:    return "TRACE";
736789f23bSCédric Le Goater     case POWERPC_EXCP_HDSI:     return "HDSI";
746789f23bSCédric Le Goater     case POWERPC_EXCP_HISI:     return "HISI";
756789f23bSCédric Le Goater     case POWERPC_EXCP_HDSEG:    return "HDSEG";
766789f23bSCédric Le Goater     case POWERPC_EXCP_HISEG:    return "HISEG";
776789f23bSCédric Le Goater     case POWERPC_EXCP_VPU:      return "VPU";
786789f23bSCédric Le Goater     case POWERPC_EXCP_PIT:      return "PIT";
796789f23bSCédric Le Goater     case POWERPC_EXCP_EMUL:     return "EMUL";
806789f23bSCédric Le Goater     case POWERPC_EXCP_IFTLB:    return "IFTLB";
816789f23bSCédric Le Goater     case POWERPC_EXCP_DLTLB:    return "DLTLB";
826789f23bSCédric Le Goater     case POWERPC_EXCP_DSTLB:    return "DSTLB";
836789f23bSCédric Le Goater     case POWERPC_EXCP_FPA:      return "FPA";
846789f23bSCédric Le Goater     case POWERPC_EXCP_DABR:     return "DABR";
856789f23bSCédric Le Goater     case POWERPC_EXCP_IABR:     return "IABR";
866789f23bSCédric Le Goater     case POWERPC_EXCP_SMI:      return "SMI";
876789f23bSCédric Le Goater     case POWERPC_EXCP_PERFM:    return "PERFM";
886789f23bSCédric Le Goater     case POWERPC_EXCP_THERM:    return "THERM";
896789f23bSCédric Le Goater     case POWERPC_EXCP_VPUA:     return "VPUA";
906789f23bSCédric Le Goater     case POWERPC_EXCP_SOFTP:    return "SOFTP";
916789f23bSCédric Le Goater     case POWERPC_EXCP_MAINT:    return "MAINT";
926789f23bSCédric Le Goater     case POWERPC_EXCP_MEXTBR:   return "MEXTBR";
936789f23bSCédric Le Goater     case POWERPC_EXCP_NMEXTBR:  return "NMEXTBR";
946789f23bSCédric Le Goater     case POWERPC_EXCP_ITLBE:    return "ITLBE";
956789f23bSCédric Le Goater     case POWERPC_EXCP_DTLBE:    return "DTLBE";
966789f23bSCédric Le Goater     case POWERPC_EXCP_VSXU:     return "VSXU";
976789f23bSCédric Le Goater     case POWERPC_EXCP_FU:       return "FU";
986789f23bSCédric Le Goater     case POWERPC_EXCP_HV_EMU:   return "HV_EMU";
996789f23bSCédric Le Goater     case POWERPC_EXCP_HV_MAINT: return "HV_MAINT";
1006789f23bSCédric Le Goater     case POWERPC_EXCP_HV_FU:    return "HV_FU";
1016789f23bSCédric Le Goater     case POWERPC_EXCP_SDOOR:    return "SDOOR";
1026789f23bSCédric Le Goater     case POWERPC_EXCP_SDOOR_HV: return "SDOOR_HV";
1036789f23bSCédric Le Goater     case POWERPC_EXCP_HVIRT:    return "HVIRT";
1046789f23bSCédric Le Goater     case POWERPC_EXCP_SYSCALL_VECTORED: return "SYSCALL_VECTORED";
1056789f23bSCédric Le Goater     default:
1066789f23bSCédric Le Goater         g_assert_not_reached();
1076789f23bSCédric Le Goater     }
1086789f23bSCédric Le Goater }
1096789f23bSCédric Le Goater 
11062e79ef9SCédric Le Goater static void dump_syscall(CPUPPCState *env)
111c79c73f6SBlue Swirl {
1126dc6b557SNicholas Piggin     qemu_log_mask(CPU_LOG_INT, "syscall r0=%016" PRIx64
1136dc6b557SNicholas Piggin                   " r3=%016" PRIx64 " r4=%016" PRIx64 " r5=%016" PRIx64
1146dc6b557SNicholas Piggin                   " r6=%016" PRIx64 " r7=%016" PRIx64 " r8=%016" PRIx64
115c79c73f6SBlue Swirl                   " nip=" TARGET_FMT_lx "\n",
116c79c73f6SBlue Swirl                   ppc_dump_gpr(env, 0), ppc_dump_gpr(env, 3),
117c79c73f6SBlue Swirl                   ppc_dump_gpr(env, 4), ppc_dump_gpr(env, 5),
1186dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 6), ppc_dump_gpr(env, 7),
1196dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 8), env->nip);
1206dc6b557SNicholas Piggin }
1216dc6b557SNicholas Piggin 
12262e79ef9SCédric Le Goater static void dump_hcall(CPUPPCState *env)
1236dc6b557SNicholas Piggin {
1246dc6b557SNicholas Piggin     qemu_log_mask(CPU_LOG_INT, "hypercall r3=%016" PRIx64
1256dc6b557SNicholas Piggin                   " r4=%016" PRIx64 " r5=%016" PRIx64 " r6=%016" PRIx64
1266dc6b557SNicholas Piggin                   " r7=%016" PRIx64 " r8=%016" PRIx64 " r9=%016" PRIx64
1276dc6b557SNicholas Piggin                   " r10=%016" PRIx64 " r11=%016" PRIx64 " r12=%016" PRIx64
1286dc6b557SNicholas Piggin                   " nip=" TARGET_FMT_lx "\n",
1296dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 3), ppc_dump_gpr(env, 4),
1306dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 5), ppc_dump_gpr(env, 6),
1316dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 7), ppc_dump_gpr(env, 8),
1326dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 9), ppc_dump_gpr(env, 10),
1336dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 11), ppc_dump_gpr(env, 12),
1346dc6b557SNicholas Piggin                   env->nip);
135c79c73f6SBlue Swirl }
136c79c73f6SBlue Swirl 
137888050cfSNicholas Piggin #ifdef CONFIG_TCG
138888050cfSNicholas Piggin /* Return true iff byteswap is needed to load instruction */
139888050cfSNicholas Piggin static inline bool insn_need_byteswap(CPUArchState *env)
140888050cfSNicholas Piggin {
141888050cfSNicholas Piggin     /* SYSTEM builds TARGET_BIG_ENDIAN. Need to swap when MSR[LE] is set */
142888050cfSNicholas Piggin     return !!(env->msr & ((target_ulong)1 << MSR_LE));
143888050cfSNicholas Piggin }
144888050cfSNicholas Piggin 
1455a5d3b23SNicholas Piggin static uint32_t ppc_ldl_code(CPUArchState *env, abi_ptr addr)
146888050cfSNicholas Piggin {
147888050cfSNicholas Piggin     uint32_t insn = cpu_ldl_code(env, addr);
148888050cfSNicholas Piggin 
149888050cfSNicholas Piggin     if (insn_need_byteswap(env)) {
150888050cfSNicholas Piggin         insn = bswap32(insn);
151888050cfSNicholas Piggin     }
152888050cfSNicholas Piggin 
153888050cfSNicholas Piggin     return insn;
154888050cfSNicholas Piggin }
155888050cfSNicholas Piggin #endif
156888050cfSNicholas Piggin 
157e4e27df7SFabiano Rosas static void ppc_excp_debug_sw_tlb(CPUPPCState *env, int excp)
158e4e27df7SFabiano Rosas {
159e4e27df7SFabiano Rosas     const char *es;
160e4e27df7SFabiano Rosas     target_ulong *miss, *cmp;
161e4e27df7SFabiano Rosas     int en;
162e4e27df7SFabiano Rosas 
1632e089eceSFabiano Rosas     if (!qemu_loglevel_mask(CPU_LOG_MMU)) {
164e4e27df7SFabiano Rosas         return;
165e4e27df7SFabiano Rosas     }
166e4e27df7SFabiano Rosas 
167e4e27df7SFabiano Rosas     if (excp == POWERPC_EXCP_IFTLB) {
168e4e27df7SFabiano Rosas         es = "I";
169e4e27df7SFabiano Rosas         en = 'I';
170e4e27df7SFabiano Rosas         miss = &env->spr[SPR_IMISS];
171e4e27df7SFabiano Rosas         cmp = &env->spr[SPR_ICMP];
172e4e27df7SFabiano Rosas     } else {
173e4e27df7SFabiano Rosas         if (excp == POWERPC_EXCP_DLTLB) {
174e4e27df7SFabiano Rosas             es = "DL";
175e4e27df7SFabiano Rosas         } else {
176e4e27df7SFabiano Rosas             es = "DS";
177e4e27df7SFabiano Rosas         }
178e4e27df7SFabiano Rosas         en = 'D';
179e4e27df7SFabiano Rosas         miss = &env->spr[SPR_DMISS];
180e4e27df7SFabiano Rosas         cmp = &env->spr[SPR_DCMP];
181e4e27df7SFabiano Rosas     }
182e4e27df7SFabiano Rosas     qemu_log("6xx %sTLB miss: %cM " TARGET_FMT_lx " %cC "
183e4e27df7SFabiano Rosas              TARGET_FMT_lx " H1 " TARGET_FMT_lx " H2 "
184e4e27df7SFabiano Rosas              TARGET_FMT_lx " %08x\n", es, en, *miss, en, *cmp,
185e4e27df7SFabiano Rosas              env->spr[SPR_HASH1], env->spr[SPR_HASH2],
186e4e27df7SFabiano Rosas              env->error_code);
187e4e27df7SFabiano Rosas }
188e4e27df7SFabiano Rosas 
189f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
1903f88a89dSBALATON Zoltan static int powerpc_reset_wakeup(CPUPPCState *env, int excp, target_ulong *msr)
191dead760bSBenjamin Herrenschmidt {
192dead760bSBenjamin Herrenschmidt     /* We no longer are in a PM state */
1931e7fd61dSBenjamin Herrenschmidt     env->resume_as_sreset = false;
194dead760bSBenjamin Herrenschmidt 
195dead760bSBenjamin Herrenschmidt     /* Pretend to be returning from doze always as we don't lose state */
1960911a60cSLeonardo Bras     *msr |= SRR1_WS_NOLOSS;
197dead760bSBenjamin Herrenschmidt 
198dead760bSBenjamin Herrenschmidt     /* Machine checks are sent normally */
199dead760bSBenjamin Herrenschmidt     if (excp == POWERPC_EXCP_MCHECK) {
200dead760bSBenjamin Herrenschmidt         return excp;
201dead760bSBenjamin Herrenschmidt     }
202dead760bSBenjamin Herrenschmidt     switch (excp) {
203dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_RESET:
2040911a60cSLeonardo Bras         *msr |= SRR1_WAKERESET;
205dead760bSBenjamin Herrenschmidt         break;
206dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_EXTERNAL:
2070911a60cSLeonardo Bras         *msr |= SRR1_WAKEEE;
208dead760bSBenjamin Herrenschmidt         break;
209dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_DECR:
2100911a60cSLeonardo Bras         *msr |= SRR1_WAKEDEC;
211dead760bSBenjamin Herrenschmidt         break;
212dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_SDOOR:
2130911a60cSLeonardo Bras         *msr |= SRR1_WAKEDBELL;
214dead760bSBenjamin Herrenschmidt         break;
215dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_SDOOR_HV:
2160911a60cSLeonardo Bras         *msr |= SRR1_WAKEHDBELL;
217dead760bSBenjamin Herrenschmidt         break;
218dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_HV_MAINT:
2190911a60cSLeonardo Bras         *msr |= SRR1_WAKEHMI;
220dead760bSBenjamin Herrenschmidt         break;
221d8ce5fd6SBenjamin Herrenschmidt     case POWERPC_EXCP_HVIRT:
2220911a60cSLeonardo Bras         *msr |= SRR1_WAKEHVI;
223d8ce5fd6SBenjamin Herrenschmidt         break;
224dead760bSBenjamin Herrenschmidt     default:
2253f88a89dSBALATON Zoltan         cpu_abort(env_cpu(env),
2263f88a89dSBALATON Zoltan                   "Unsupported exception %d in Power Save mode\n", excp);
227dead760bSBenjamin Herrenschmidt     }
228dead760bSBenjamin Herrenschmidt     return POWERPC_EXCP_RESET;
229dead760bSBenjamin Herrenschmidt }
230dead760bSBenjamin Herrenschmidt 
2318b7e6b07SNicholas Piggin /*
2328b7e6b07SNicholas Piggin  * AIL - Alternate Interrupt Location, a mode that allows interrupts to be
2338b7e6b07SNicholas Piggin  * taken with the MMU on, and which uses an alternate location (e.g., so the
2348b7e6b07SNicholas Piggin  * kernel/hv can map the vectors there with an effective address).
2358b7e6b07SNicholas Piggin  *
2368b7e6b07SNicholas Piggin  * An interrupt is considered to be taken "with AIL" or "AIL applies" if they
2378b7e6b07SNicholas Piggin  * are delivered in this way. AIL requires the LPCR to be set to enable this
2388b7e6b07SNicholas Piggin  * mode, and then a number of conditions have to be true for AIL to apply.
2398b7e6b07SNicholas Piggin  *
2408b7e6b07SNicholas Piggin  * First of all, SRESET, MCE, and HMI are always delivered without AIL, because
2418b7e6b07SNicholas Piggin  * they specifically want to be in real mode (e.g., the MCE might be signaling
2428b7e6b07SNicholas Piggin  * a SLB multi-hit which requires SLB flush before the MMU can be enabled).
2438b7e6b07SNicholas Piggin  *
2448b7e6b07SNicholas Piggin  * After that, behaviour depends on the current MSR[IR], MSR[DR], MSR[HV],
2458b7e6b07SNicholas Piggin  * whether or not the interrupt changes MSR[HV] from 0 to 1, and the current
2468b7e6b07SNicholas Piggin  * radix mode (LPCR[HR]).
2478b7e6b07SNicholas Piggin  *
2488b7e6b07SNicholas Piggin  * POWER8, POWER9 with LPCR[HR]=0
2498b7e6b07SNicholas Piggin  * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
2508b7e6b07SNicholas Piggin  * +-----------+-------------+---------+-------------+-----+
2518b7e6b07SNicholas Piggin  * | a         | 00/01/10    | x       | x           | 0   |
2528b7e6b07SNicholas Piggin  * | a         | 11          | 0       | 1           | 0   |
2538b7e6b07SNicholas Piggin  * | a         | 11          | 1       | 1           | a   |
2548b7e6b07SNicholas Piggin  * | a         | 11          | 0       | 0           | a   |
2558b7e6b07SNicholas Piggin  * +-------------------------------------------------------+
2568b7e6b07SNicholas Piggin  *
2578b7e6b07SNicholas Piggin  * POWER9 with LPCR[HR]=1
2588b7e6b07SNicholas Piggin  * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
2598b7e6b07SNicholas Piggin  * +-----------+-------------+---------+-------------+-----+
2608b7e6b07SNicholas Piggin  * | a         | 00/01/10    | x       | x           | 0   |
2618b7e6b07SNicholas Piggin  * | a         | 11          | x       | x           | a   |
2628b7e6b07SNicholas Piggin  * +-------------------------------------------------------+
2638b7e6b07SNicholas Piggin  *
2648b7e6b07SNicholas Piggin  * The difference with POWER9 being that MSR[HV] 0->1 interrupts can be sent to
265526cdce7SNicholas Piggin  * the hypervisor in AIL mode if the guest is radix. This is good for
266526cdce7SNicholas Piggin  * performance but allows the guest to influence the AIL of hypervisor
267526cdce7SNicholas Piggin  * interrupts using its MSR, and also the hypervisor must disallow guest
268526cdce7SNicholas Piggin  * interrupts (MSR[HV] 0->0) from using AIL if the hypervisor does not want to
269526cdce7SNicholas Piggin  * use AIL for its MSR[HV] 0->1 interrupts.
270526cdce7SNicholas Piggin  *
271526cdce7SNicholas Piggin  * POWER10 addresses those issues with a new LPCR[HAIL] bit that is applied to
272526cdce7SNicholas Piggin  * interrupts that begin execution with MSR[HV]=1 (so both MSR[HV] 0->1 and
273526cdce7SNicholas Piggin  * MSR[HV] 1->1).
274526cdce7SNicholas Piggin  *
275526cdce7SNicholas Piggin  * HAIL=1 is equivalent to AIL=3, for interrupts delivered with MSR[HV]=1.
276526cdce7SNicholas Piggin  *
277526cdce7SNicholas Piggin  * POWER10 behaviour is
278526cdce7SNicholas Piggin  * | LPCR[AIL] | LPCR[HAIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
279526cdce7SNicholas Piggin  * +-----------+------------+-------------+---------+-------------+-----+
280526cdce7SNicholas Piggin  * | a         | h          | 00/01/10    | 0       | 0           | 0   |
281526cdce7SNicholas Piggin  * | a         | h          | 11          | 0       | 0           | a   |
282526cdce7SNicholas Piggin  * | a         | h          | x           | 0       | 1           | h   |
283526cdce7SNicholas Piggin  * | a         | h          | 00/01/10    | 1       | 1           | 0   |
284526cdce7SNicholas Piggin  * | a         | h          | 11          | 1       | 1           | h   |
285526cdce7SNicholas Piggin  * +--------------------------------------------------------------------+
2868b7e6b07SNicholas Piggin  */
28710895ab6SFabiano Rosas static void ppc_excp_apply_ail(PowerPCCPU *cpu, int excp, target_ulong msr,
28810895ab6SFabiano Rosas                                target_ulong *new_msr, target_ulong *vector)
2892586a4d7SFabiano Rosas {
29010895ab6SFabiano Rosas     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
2918b7e6b07SNicholas Piggin     CPUPPCState *env = &cpu->env;
2928b7e6b07SNicholas Piggin     bool mmu_all_on = ((msr >> MSR_IR) & 1) && ((msr >> MSR_DR) & 1);
2938b7e6b07SNicholas Piggin     bool hv_escalation = !(msr & MSR_HVB) && (*new_msr & MSR_HVB);
2948b7e6b07SNicholas Piggin     int ail = 0;
2952586a4d7SFabiano Rosas 
2968b7e6b07SNicholas Piggin     if (excp == POWERPC_EXCP_MCHECK ||
2978b7e6b07SNicholas Piggin         excp == POWERPC_EXCP_RESET ||
2988b7e6b07SNicholas Piggin         excp == POWERPC_EXCP_HV_MAINT) {
2998b7e6b07SNicholas Piggin         /* SRESET, MCE, HMI never apply AIL */
3008b7e6b07SNicholas Piggin         return;
3012586a4d7SFabiano Rosas     }
3022586a4d7SFabiano Rosas 
30310895ab6SFabiano Rosas     if (!(pcc->lpcr_mask & LPCR_AIL)) {
30410895ab6SFabiano Rosas         /* This CPU does not have AIL */
30510895ab6SFabiano Rosas         return;
30610895ab6SFabiano Rosas     }
30710895ab6SFabiano Rosas 
30810895ab6SFabiano Rosas     /* P8 & P9 */
30910895ab6SFabiano Rosas     if (!(pcc->lpcr_mask & LPCR_HAIL)) {
3108b7e6b07SNicholas Piggin         if (!mmu_all_on) {
3118b7e6b07SNicholas Piggin             /* AIL only works if MSR[IR] and MSR[DR] are both enabled. */
3128b7e6b07SNicholas Piggin             return;
3138b7e6b07SNicholas Piggin         }
3148b7e6b07SNicholas Piggin         if (hv_escalation && !(env->spr[SPR_LPCR] & LPCR_HR)) {
3158b7e6b07SNicholas Piggin             /*
3168b7e6b07SNicholas Piggin              * AIL does not work if there is a MSR[HV] 0->1 transition and the
3178b7e6b07SNicholas Piggin              * partition is in HPT mode. For radix guests, such interrupts are
3188b7e6b07SNicholas Piggin              * allowed to be delivered to the hypervisor in ail mode.
3198b7e6b07SNicholas Piggin              */
3208b7e6b07SNicholas Piggin             return;
3218b7e6b07SNicholas Piggin         }
3228b7e6b07SNicholas Piggin 
3238b7e6b07SNicholas Piggin         ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT;
3248b7e6b07SNicholas Piggin         if (ail == 0) {
3258b7e6b07SNicholas Piggin             return;
3268b7e6b07SNicholas Piggin         }
3278b7e6b07SNicholas Piggin         if (ail == 1) {
3288b7e6b07SNicholas Piggin             /* AIL=1 is reserved, treat it like AIL=0 */
3298b7e6b07SNicholas Piggin             return;
3308b7e6b07SNicholas Piggin         }
331526cdce7SNicholas Piggin 
33210895ab6SFabiano Rosas     /* P10 and up */
33310895ab6SFabiano Rosas     } else {
334526cdce7SNicholas Piggin         if (!mmu_all_on && !hv_escalation) {
335526cdce7SNicholas Piggin             /*
336526cdce7SNicholas Piggin              * AIL works for HV interrupts even with guest MSR[IR/DR] disabled.
337526cdce7SNicholas Piggin              * Guest->guest and HV->HV interrupts do require MMU on.
338526cdce7SNicholas Piggin              */
339526cdce7SNicholas Piggin             return;
340526cdce7SNicholas Piggin         }
341526cdce7SNicholas Piggin 
342526cdce7SNicholas Piggin         if (*new_msr & MSR_HVB) {
343526cdce7SNicholas Piggin             if (!(env->spr[SPR_LPCR] & LPCR_HAIL)) {
344526cdce7SNicholas Piggin                 /* HV interrupts depend on LPCR[HAIL] */
345526cdce7SNicholas Piggin                 return;
346526cdce7SNicholas Piggin             }
347526cdce7SNicholas Piggin             ail = 3; /* HAIL=1 gives AIL=3 behaviour for HV interrupts */
348526cdce7SNicholas Piggin         } else {
349526cdce7SNicholas Piggin             ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT;
350526cdce7SNicholas Piggin         }
351526cdce7SNicholas Piggin         if (ail == 0) {
352526cdce7SNicholas Piggin             return;
353526cdce7SNicholas Piggin         }
354526cdce7SNicholas Piggin         if (ail == 1 || ail == 2) {
355526cdce7SNicholas Piggin             /* AIL=1 and AIL=2 are reserved, treat them like AIL=0 */
356526cdce7SNicholas Piggin             return;
357526cdce7SNicholas Piggin         }
3588b7e6b07SNicholas Piggin     }
3598b7e6b07SNicholas Piggin 
3608b7e6b07SNicholas Piggin     /*
3618b7e6b07SNicholas Piggin      * AIL applies, so the new MSR gets IR and DR set, and an offset applied
3628b7e6b07SNicholas Piggin      * to the new IP.
3638b7e6b07SNicholas Piggin      */
3648b7e6b07SNicholas Piggin     *new_msr |= (1 << MSR_IR) | (1 << MSR_DR);
3658b7e6b07SNicholas Piggin 
3668b7e6b07SNicholas Piggin     if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
3678b7e6b07SNicholas Piggin         if (ail == 2) {
3688b7e6b07SNicholas Piggin             *vector |= 0x0000000000018000ull;
3698b7e6b07SNicholas Piggin         } else if (ail == 3) {
3708b7e6b07SNicholas Piggin             *vector |= 0xc000000000004000ull;
3718b7e6b07SNicholas Piggin         }
3728b7e6b07SNicholas Piggin     } else {
3738b7e6b07SNicholas Piggin         /*
3748b7e6b07SNicholas Piggin          * scv AIL is a little different. AIL=2 does not change the address,
3758b7e6b07SNicholas Piggin          * only the MSR. AIL=3 replaces the 0x17000 base with 0xc...3000.
3768b7e6b07SNicholas Piggin          */
3778b7e6b07SNicholas Piggin         if (ail == 3) {
3788b7e6b07SNicholas Piggin             *vector &= ~0x0000000000017000ull; /* Un-apply the base offset */
3798b7e6b07SNicholas Piggin             *vector |= 0xc000000000003000ull; /* Apply scv's AIL=3 offset */
3808b7e6b07SNicholas Piggin         }
3818b7e6b07SNicholas Piggin     }
3822586a4d7SFabiano Rosas }
383f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
384dead760bSBenjamin Herrenschmidt 
3853680e994SNicholas Piggin static void powerpc_reset_excp_state(PowerPCCPU *cpu)
386ad77c6caSNicholas Piggin {
387ad77c6caSNicholas Piggin     CPUState *cs = CPU(cpu);
388ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
389ad77c6caSNicholas Piggin 
3903680e994SNicholas Piggin     /* Reset exception state */
3913680e994SNicholas Piggin     cs->exception_index = POWERPC_EXCP_NONE;
3923680e994SNicholas Piggin     env->error_code = 0;
3933680e994SNicholas Piggin }
3943680e994SNicholas Piggin 
3953680e994SNicholas Piggin static void powerpc_set_excp_state(PowerPCCPU *cpu, target_ulong vector,
3963680e994SNicholas Piggin                                    target_ulong msr)
3973680e994SNicholas Piggin {
3983680e994SNicholas Piggin     CPUPPCState *env = &cpu->env;
3993680e994SNicholas Piggin 
400fce9fbafSFabiano Rosas     assert((msr & env->msr_mask) == msr);
401fce9fbafSFabiano Rosas 
402ad77c6caSNicholas Piggin     /*
403ad77c6caSNicholas Piggin      * We don't use hreg_store_msr here as already have treated any
404ad77c6caSNicholas Piggin      * special case that could occur. Just store MSR and update hflags
405ad77c6caSNicholas Piggin      *
406ab452503SBALATON Zoltan      * Note: We *MUST* not use hreg_store_msr() as-is anyway because it will
407ab452503SBALATON Zoltan      * prevent setting of the HV bit which some exceptions might need to do.
408ad77c6caSNicholas Piggin      */
4093680e994SNicholas Piggin     env->nip = vector;
410fce9fbafSFabiano Rosas     env->msr = msr;
411ad77c6caSNicholas Piggin     hreg_compute_hflags(env);
4122fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
413ad77c6caSNicholas Piggin 
4143680e994SNicholas Piggin     powerpc_reset_excp_state(cpu);
415ad77c6caSNicholas Piggin 
416ad77c6caSNicholas Piggin     /*
417ad77c6caSNicholas Piggin      * Any interrupt is context synchronizing, check if TCG TLB needs
418ad77c6caSNicholas Piggin      * a delayed flush on ppc64
419ad77c6caSNicholas Piggin      */
420ad77c6caSNicholas Piggin     check_tlb_flush(env, false);
4213680e994SNicholas Piggin 
4223680e994SNicholas Piggin     /* Reset the reservation */
4233680e994SNicholas Piggin     env->reserve_addr = -1;
424ad77c6caSNicholas Piggin }
425ad77c6caSNicholas Piggin 
426819b31b0SBALATON Zoltan static void powerpc_mcheck_checkstop(CPUPPCState *env)
427819b31b0SBALATON Zoltan {
428819b31b0SBALATON Zoltan     CPUState *cs = env_cpu(env);
429819b31b0SBALATON Zoltan 
430819b31b0SBALATON Zoltan     if (FIELD_EX64(env->msr, MSR, ME)) {
431819b31b0SBALATON Zoltan         return;
432819b31b0SBALATON Zoltan     }
433819b31b0SBALATON Zoltan 
434819b31b0SBALATON Zoltan     /* Machine check exception is not enabled. Enter checkstop state. */
435819b31b0SBALATON Zoltan     fprintf(stderr, "Machine check while not allowed. "
436819b31b0SBALATON Zoltan             "Entering checkstop state\n");
437819b31b0SBALATON Zoltan     if (qemu_log_separate()) {
438819b31b0SBALATON Zoltan         qemu_log("Machine check while not allowed. "
439819b31b0SBALATON Zoltan                  "Entering checkstop state\n");
440819b31b0SBALATON Zoltan     }
441819b31b0SBALATON Zoltan     cs->halted = 1;
442819b31b0SBALATON Zoltan     cpu_interrupt_exittb(cs);
443819b31b0SBALATON Zoltan }
444819b31b0SBALATON Zoltan 
445e808c2edSFabiano Rosas static void powerpc_excp_40x(PowerPCCPU *cpu, int excp)
446e808c2edSFabiano Rosas {
447e808c2edSFabiano Rosas     CPUPPCState *env = &cpu->env;
448e808c2edSFabiano Rosas     target_ulong msr, new_msr, vector;
449ab452503SBALATON Zoltan     int srr0 = SPR_SRR0, srr1 = SPR_SRR1;
450e808c2edSFabiano Rosas 
451e808c2edSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
452e808c2edSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
453e808c2edSFabiano Rosas 
454ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
455495fc7ffSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME));
456e808c2edSFabiano Rosas 
457ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
458495fc7ffSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
459e808c2edSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
460e808c2edSFabiano Rosas     }
461e808c2edSFabiano Rosas 
462e808c2edSFabiano Rosas     vector = env->excp_vectors[excp];
463e808c2edSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
464bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
465bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
466e808c2edSFabiano Rosas     }
467e808c2edSFabiano Rosas     vector |= env->excp_prefix;
468e808c2edSFabiano Rosas 
469e808c2edSFabiano Rosas     switch (excp) {
470e808c2edSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
471e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
472e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
473e808c2edSFabiano Rosas         break;
474e808c2edSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
475819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
476e808c2edSFabiano Rosas         /* machine check exceptions don't have ME set */
477e808c2edSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
478e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
479e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
480e808c2edSFabiano Rosas         break;
481e808c2edSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
482f9911e1eSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_40x_ESR], env->spr[SPR_40x_DEAR]);
483e808c2edSFabiano Rosas         break;
484e808c2edSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
485e808c2edSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
486e808c2edSFabiano Rosas         break;
487e808c2edSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
488e808c2edSFabiano Rosas         break;
489e808c2edSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
490e808c2edSFabiano Rosas         break;
491e808c2edSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
492e808c2edSFabiano Rosas         switch (env->error_code & ~0xF) {
493e808c2edSFabiano Rosas         case POWERPC_EXCP_FP:
494da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
495e808c2edSFabiano Rosas                 trace_ppc_excp_fp_ignore();
4963680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
497e808c2edSFabiano Rosas                 return;
498e808c2edSFabiano Rosas             }
49964e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_FP;
500e808c2edSFabiano Rosas             break;
501e808c2edSFabiano Rosas         case POWERPC_EXCP_INVAL:
502e808c2edSFabiano Rosas             trace_ppc_excp_inval(env->nip);
50364e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PIL;
504e808c2edSFabiano Rosas             break;
505e808c2edSFabiano Rosas         case POWERPC_EXCP_PRIV:
50664e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PPR;
507e808c2edSFabiano Rosas             break;
508e808c2edSFabiano Rosas         case POWERPC_EXCP_TRAP:
50964e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PTR;
510e808c2edSFabiano Rosas             break;
511e808c2edSFabiano Rosas         default:
512bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
513e808c2edSFabiano Rosas                       env->error_code);
514e808c2edSFabiano Rosas             break;
515e808c2edSFabiano Rosas         }
516e808c2edSFabiano Rosas         break;
517e808c2edSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
518e808c2edSFabiano Rosas         dump_syscall(env);
519e808c2edSFabiano Rosas 
520e808c2edSFabiano Rosas         /*
521e808c2edSFabiano Rosas          * We need to correct the NIP which in this case is supposed
522e808c2edSFabiano Rosas          * to point to the next instruction
523e808c2edSFabiano Rosas          */
524e808c2edSFabiano Rosas         env->nip += 4;
525e808c2edSFabiano Rosas         break;
526e808c2edSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
527e808c2edSFabiano Rosas         trace_ppc_excp_print("FIT");
528e808c2edSFabiano Rosas         break;
529e808c2edSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
530e808c2edSFabiano Rosas         trace_ppc_excp_print("WDT");
531e808c2edSFabiano Rosas         break;
532e808c2edSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
533e808c2edSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
534e808c2edSFabiano Rosas         break;
535e808c2edSFabiano Rosas     case POWERPC_EXCP_PIT:       /* Programmable interval timer interrupt    */
536e808c2edSFabiano Rosas         trace_ppc_excp_print("PIT");
537e808c2edSFabiano Rosas         break;
5384d8ac1d1SFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
539bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
5404d8ac1d1SFabiano Rosas                   powerpc_excp_name(excp));
5414d8ac1d1SFabiano Rosas         break;
542e808c2edSFabiano Rosas     default:
543bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
544bc30c1c6SBALATON Zoltan                   excp);
545e808c2edSFabiano Rosas         break;
546e808c2edSFabiano Rosas     }
547e808c2edSFabiano Rosas 
548e808c2edSFabiano Rosas     env->spr[srr0] = env->nip;
549e808c2edSFabiano Rosas     env->spr[srr1] = msr;
550e808c2edSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
551e808c2edSFabiano Rosas }
552e808c2edSFabiano Rosas 
55358d178fbSFabiano Rosas static void powerpc_excp_6xx(PowerPCCPU *cpu, int excp)
55458d178fbSFabiano Rosas {
55558d178fbSFabiano Rosas     CPUPPCState *env = &cpu->env;
55658d178fbSFabiano Rosas     target_ulong msr, new_msr, vector;
55758d178fbSFabiano Rosas 
55858d178fbSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
55958d178fbSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
56058d178fbSFabiano Rosas 
561ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
562082d783bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
56358d178fbSFabiano Rosas 
564ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
565082d783bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
56658d178fbSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
56758d178fbSFabiano Rosas     }
56858d178fbSFabiano Rosas 
56958d178fbSFabiano Rosas     vector = env->excp_vectors[excp];
57058d178fbSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
571bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
572bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
57358d178fbSFabiano Rosas     }
57458d178fbSFabiano Rosas     vector |= env->excp_prefix;
57558d178fbSFabiano Rosas 
57658d178fbSFabiano Rosas     switch (excp) {
57758d178fbSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
57858d178fbSFabiano Rosas         break;
57958d178fbSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
580819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
58158d178fbSFabiano Rosas         /* machine check exceptions don't have ME set */
58258d178fbSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
58358d178fbSFabiano Rosas         break;
58458d178fbSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
58558d178fbSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
58658d178fbSFabiano Rosas         break;
58758d178fbSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
58858d178fbSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
58958d178fbSFabiano Rosas         msr |= env->error_code;
59058d178fbSFabiano Rosas         break;
59158d178fbSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
59258d178fbSFabiano Rosas         break;
59358d178fbSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
59458d178fbSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
59558d178fbSFabiano Rosas         /*
59658d178fbSFabiano Rosas          * Note: the opcode fields will not be set properly for a
59758d178fbSFabiano Rosas          * direct store load/store, but nobody cares as nobody
59858d178fbSFabiano Rosas          * actually uses direct store segments.
59958d178fbSFabiano Rosas          */
60058d178fbSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
60158d178fbSFabiano Rosas         break;
60258d178fbSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
60358d178fbSFabiano Rosas         switch (env->error_code & ~0xF) {
60458d178fbSFabiano Rosas         case POWERPC_EXCP_FP:
605da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
60658d178fbSFabiano Rosas                 trace_ppc_excp_fp_ignore();
6073680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
60858d178fbSFabiano Rosas                 return;
60958d178fbSFabiano Rosas             }
61058d178fbSFabiano Rosas             /*
611ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
612ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
61358d178fbSFabiano Rosas              */
61458d178fbSFabiano Rosas             msr |= 0x00100000;
61558d178fbSFabiano Rosas             break;
61658d178fbSFabiano Rosas         case POWERPC_EXCP_INVAL:
61758d178fbSFabiano Rosas             trace_ppc_excp_inval(env->nip);
61858d178fbSFabiano Rosas             msr |= 0x00080000;
61958d178fbSFabiano Rosas             break;
62058d178fbSFabiano Rosas         case POWERPC_EXCP_PRIV:
62158d178fbSFabiano Rosas             msr |= 0x00040000;
62258d178fbSFabiano Rosas             break;
62358d178fbSFabiano Rosas         case POWERPC_EXCP_TRAP:
62458d178fbSFabiano Rosas             msr |= 0x00020000;
62558d178fbSFabiano Rosas             break;
62658d178fbSFabiano Rosas         default:
62758d178fbSFabiano Rosas             /* Should never occur */
628bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
62958d178fbSFabiano Rosas                       env->error_code);
63058d178fbSFabiano Rosas             break;
63158d178fbSFabiano Rosas         }
63258d178fbSFabiano Rosas         break;
63358d178fbSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
63458d178fbSFabiano Rosas         dump_syscall(env);
63558d178fbSFabiano Rosas 
63658d178fbSFabiano Rosas         /*
63758d178fbSFabiano Rosas          * We need to correct the NIP which in this case is supposed
63858d178fbSFabiano Rosas          * to point to the next instruction
63958d178fbSFabiano Rosas          */
64058d178fbSFabiano Rosas         env->nip += 4;
64158d178fbSFabiano Rosas         break;
64258d178fbSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
64358d178fbSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
64458d178fbSFabiano Rosas         break;
64558d178fbSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
64658d178fbSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
64758d178fbSFabiano Rosas         break;
64858d178fbSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
6498e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
650bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
651bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset exception "
652bc30c1c6SBALATON Zoltan                       "%d with no HV support\n", excp);
65358d178fbSFabiano Rosas         }
65458d178fbSFabiano Rosas         break;
65558d178fbSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
65658d178fbSFabiano Rosas         break;
65758d178fbSFabiano Rosas     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
65858d178fbSFabiano Rosas     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
65958d178fbSFabiano Rosas     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
66058d178fbSFabiano Rosas         /* Swap temporary saved registers with GPRs */
66158d178fbSFabiano Rosas         if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
66258d178fbSFabiano Rosas             new_msr |= (target_ulong)1 << MSR_TGPR;
66358d178fbSFabiano Rosas             hreg_swap_gpr_tgpr(env);
66458d178fbSFabiano Rosas         }
6658f8c7932SFabiano Rosas 
66658d178fbSFabiano Rosas         ppc_excp_debug_sw_tlb(env, excp);
66758d178fbSFabiano Rosas 
66858d178fbSFabiano Rosas         msr |= env->crf[0] << 28;
66958d178fbSFabiano Rosas         msr |= env->error_code; /* key, D/I, S/L bits */
67058d178fbSFabiano Rosas         /* Set way using a LRU mechanism */
67158d178fbSFabiano Rosas         msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
67258d178fbSFabiano Rosas         break;
67358d178fbSFabiano Rosas     case POWERPC_EXCP_FPA:       /* Floating-point assist exception          */
67458d178fbSFabiano Rosas     case POWERPC_EXCP_DABR:      /* Data address breakpoint                  */
67558d178fbSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
67658d178fbSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
67758d178fbSFabiano Rosas     case POWERPC_EXCP_MEXTBR:    /* Maskable external breakpoint             */
67858d178fbSFabiano Rosas     case POWERPC_EXCP_NMEXTBR:   /* Non maskable external breakpoint         */
679bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
68058d178fbSFabiano Rosas                   powerpc_excp_name(excp));
68158d178fbSFabiano Rosas         break;
68258d178fbSFabiano Rosas     default:
683bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
684bc30c1c6SBALATON Zoltan                   excp);
68558d178fbSFabiano Rosas         break;
68658d178fbSFabiano Rosas     }
68758d178fbSFabiano Rosas 
68858d178fbSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
68958d178fbSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
69058d178fbSFabiano Rosas     }
691c50eaed1SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
692c50eaed1SFabiano Rosas     env->spr[SPR_SRR1] = msr;
69358d178fbSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
69458d178fbSFabiano Rosas }
69558d178fbSFabiano Rosas 
696ccfca2fcSFabiano Rosas static void powerpc_excp_7xx(PowerPCCPU *cpu, int excp)
697ccfca2fcSFabiano Rosas {
698ccfca2fcSFabiano Rosas     CPUPPCState *env = &cpu->env;
699ccfca2fcSFabiano Rosas     target_ulong msr, new_msr, vector;
700ccfca2fcSFabiano Rosas 
701ccfca2fcSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
702ccfca2fcSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
703ccfca2fcSFabiano Rosas 
704ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
70593848d6aSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
706ccfca2fcSFabiano Rosas 
707ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
70893848d6aSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
709ccfca2fcSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
710ccfca2fcSFabiano Rosas     }
711ccfca2fcSFabiano Rosas 
712ccfca2fcSFabiano Rosas     vector = env->excp_vectors[excp];
713ccfca2fcSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
714bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
715bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
716ccfca2fcSFabiano Rosas     }
717ccfca2fcSFabiano Rosas     vector |= env->excp_prefix;
718ccfca2fcSFabiano Rosas 
719ccfca2fcSFabiano Rosas     switch (excp) {
720ccfca2fcSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
721819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
722ccfca2fcSFabiano Rosas         /* machine check exceptions don't have ME set */
723ccfca2fcSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
724ccfca2fcSFabiano Rosas         break;
725ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
726ccfca2fcSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
727ccfca2fcSFabiano Rosas         break;
728ccfca2fcSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
729ccfca2fcSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
730ccfca2fcSFabiano Rosas         msr |= env->error_code;
731ccfca2fcSFabiano Rosas         break;
732ccfca2fcSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
733ccfca2fcSFabiano Rosas         break;
734ccfca2fcSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
735ccfca2fcSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
736ccfca2fcSFabiano Rosas         /*
737ccfca2fcSFabiano Rosas          * Note: the opcode fields will not be set properly for a
738ccfca2fcSFabiano Rosas          * direct store load/store, but nobody cares as nobody
739ccfca2fcSFabiano Rosas          * actually uses direct store segments.
740ccfca2fcSFabiano Rosas          */
741ccfca2fcSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
742ccfca2fcSFabiano Rosas         break;
743ccfca2fcSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
744ccfca2fcSFabiano Rosas         switch (env->error_code & ~0xF) {
745ccfca2fcSFabiano Rosas         case POWERPC_EXCP_FP:
746da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
747ccfca2fcSFabiano Rosas                 trace_ppc_excp_fp_ignore();
7483680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
749ccfca2fcSFabiano Rosas                 return;
750ccfca2fcSFabiano Rosas             }
751ccfca2fcSFabiano Rosas             /*
752ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
753ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
754ccfca2fcSFabiano Rosas              */
755ccfca2fcSFabiano Rosas             msr |= 0x00100000;
756ccfca2fcSFabiano Rosas             break;
757ccfca2fcSFabiano Rosas         case POWERPC_EXCP_INVAL:
758ccfca2fcSFabiano Rosas             trace_ppc_excp_inval(env->nip);
759ccfca2fcSFabiano Rosas             msr |= 0x00080000;
760ccfca2fcSFabiano Rosas             break;
761ccfca2fcSFabiano Rosas         case POWERPC_EXCP_PRIV:
762ccfca2fcSFabiano Rosas             msr |= 0x00040000;
763ccfca2fcSFabiano Rosas             break;
764ccfca2fcSFabiano Rosas         case POWERPC_EXCP_TRAP:
765ccfca2fcSFabiano Rosas             msr |= 0x00020000;
766ccfca2fcSFabiano Rosas             break;
767ccfca2fcSFabiano Rosas         default:
768ccfca2fcSFabiano Rosas             /* Should never occur */
769bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
770ccfca2fcSFabiano Rosas                       env->error_code);
771ccfca2fcSFabiano Rosas             break;
772ccfca2fcSFabiano Rosas         }
773ccfca2fcSFabiano Rosas         break;
774ccfca2fcSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
7753b578635SFabiano Rosas     {
7763b578635SFabiano Rosas         int lev = env->error_code;
777ccfca2fcSFabiano Rosas 
7783b578635SFabiano Rosas         if (lev == 1 && cpu->vhyp) {
779ccfca2fcSFabiano Rosas             dump_hcall(env);
780ccfca2fcSFabiano Rosas         } else {
781ccfca2fcSFabiano Rosas             dump_syscall(env);
782ccfca2fcSFabiano Rosas         }
783ccfca2fcSFabiano Rosas 
784ccfca2fcSFabiano Rosas         /*
785ccfca2fcSFabiano Rosas          * We need to correct the NIP which in this case is supposed
786ccfca2fcSFabiano Rosas          * to point to the next instruction
787ccfca2fcSFabiano Rosas          */
788ccfca2fcSFabiano Rosas         env->nip += 4;
789ccfca2fcSFabiano Rosas 
7903b578635SFabiano Rosas         /*
7913b578635SFabiano Rosas          * The Virtual Open Firmware (VOF) relies on the 'sc 1'
7923b578635SFabiano Rosas          * instruction to communicate with QEMU. The pegasos2 machine
7933b578635SFabiano Rosas          * uses VOF and the 7xx CPUs, so although the 7xx don't have
7943b578635SFabiano Rosas          * HV mode, we need to keep hypercall support.
7953b578635SFabiano Rosas          */
7963b578635SFabiano Rosas         if (lev == 1 && cpu->vhyp) {
797ccfca2fcSFabiano Rosas             PPCVirtualHypervisorClass *vhc =
798ccfca2fcSFabiano Rosas                 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
799ccfca2fcSFabiano Rosas             vhc->hypercall(cpu->vhyp, cpu);
8002c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
801ccfca2fcSFabiano Rosas             return;
802ccfca2fcSFabiano Rosas         }
8033b578635SFabiano Rosas 
804ccfca2fcSFabiano Rosas         break;
8053b578635SFabiano Rosas     }
806ccfca2fcSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
807ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
808ccfca2fcSFabiano Rosas         break;
809ccfca2fcSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
8108e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
811bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
812bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset exception "
813bc30c1c6SBALATON Zoltan                       "%d with no HV support\n", excp);
814ccfca2fcSFabiano Rosas         }
815ccfca2fcSFabiano Rosas         break;
816ccfca2fcSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
817ccfca2fcSFabiano Rosas         break;
818ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
819ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
820ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
821ccfca2fcSFabiano Rosas         ppc_excp_debug_sw_tlb(env, excp);
822ccfca2fcSFabiano Rosas         msr |= env->crf[0] << 28;
823ccfca2fcSFabiano Rosas         msr |= env->error_code; /* key, D/I, S/L bits */
824ccfca2fcSFabiano Rosas         /* Set way using a LRU mechanism */
825ccfca2fcSFabiano Rosas         msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
826ccfca2fcSFabiano Rosas         break;
827ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
828ccfca2fcSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
829ccfca2fcSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
830ccfca2fcSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
831bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
832ccfca2fcSFabiano Rosas                   powerpc_excp_name(excp));
833ccfca2fcSFabiano Rosas         break;
834ccfca2fcSFabiano Rosas     default:
835bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
836bc30c1c6SBALATON Zoltan                   excp);
837ccfca2fcSFabiano Rosas         break;
838ccfca2fcSFabiano Rosas     }
839ccfca2fcSFabiano Rosas 
840ccfca2fcSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
841ccfca2fcSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
842ccfca2fcSFabiano Rosas     }
843fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
844fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR1] = msr;
845ccfca2fcSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
846ccfca2fcSFabiano Rosas }
847ccfca2fcSFabiano Rosas 
84852926b0dSFabiano Rosas static void powerpc_excp_74xx(PowerPCCPU *cpu, int excp)
84952926b0dSFabiano Rosas {
85052926b0dSFabiano Rosas     CPUPPCState *env = &cpu->env;
85152926b0dSFabiano Rosas     target_ulong msr, new_msr, vector;
85252926b0dSFabiano Rosas 
85352926b0dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
85452926b0dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
85552926b0dSFabiano Rosas 
856ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
8571f6faf8bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
85852926b0dSFabiano Rosas 
859ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
8601f6faf8bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
86152926b0dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
86252926b0dSFabiano Rosas     }
86352926b0dSFabiano Rosas 
86452926b0dSFabiano Rosas     vector = env->excp_vectors[excp];
86552926b0dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
866bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
867bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
86852926b0dSFabiano Rosas     }
86952926b0dSFabiano Rosas     vector |= env->excp_prefix;
87052926b0dSFabiano Rosas 
87152926b0dSFabiano Rosas     switch (excp) {
87252926b0dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
873819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
87452926b0dSFabiano Rosas         /* machine check exceptions don't have ME set */
87552926b0dSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
87652926b0dSFabiano Rosas         break;
87752926b0dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
87852926b0dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
87952926b0dSFabiano Rosas         break;
88052926b0dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
88152926b0dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
88252926b0dSFabiano Rosas         msr |= env->error_code;
88352926b0dSFabiano Rosas         break;
88452926b0dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
88552926b0dSFabiano Rosas         break;
88652926b0dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
88752926b0dSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
88852926b0dSFabiano Rosas         /*
88952926b0dSFabiano Rosas          * Note: the opcode fields will not be set properly for a
89052926b0dSFabiano Rosas          * direct store load/store, but nobody cares as nobody
89152926b0dSFabiano Rosas          * actually uses direct store segments.
89252926b0dSFabiano Rosas          */
89352926b0dSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
89452926b0dSFabiano Rosas         break;
89552926b0dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
89652926b0dSFabiano Rosas         switch (env->error_code & ~0xF) {
89752926b0dSFabiano Rosas         case POWERPC_EXCP_FP:
898da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
89952926b0dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
9003680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
90152926b0dSFabiano Rosas                 return;
90252926b0dSFabiano Rosas             }
90352926b0dSFabiano Rosas             /*
904ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
905ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
90652926b0dSFabiano Rosas              */
90752926b0dSFabiano Rosas             msr |= 0x00100000;
90852926b0dSFabiano Rosas             break;
90952926b0dSFabiano Rosas         case POWERPC_EXCP_INVAL:
91052926b0dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
91152926b0dSFabiano Rosas             msr |= 0x00080000;
91252926b0dSFabiano Rosas             break;
91352926b0dSFabiano Rosas         case POWERPC_EXCP_PRIV:
91452926b0dSFabiano Rosas             msr |= 0x00040000;
91552926b0dSFabiano Rosas             break;
91652926b0dSFabiano Rosas         case POWERPC_EXCP_TRAP:
91752926b0dSFabiano Rosas             msr |= 0x00020000;
91852926b0dSFabiano Rosas             break;
91952926b0dSFabiano Rosas         default:
92052926b0dSFabiano Rosas             /* Should never occur */
921bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
92252926b0dSFabiano Rosas                       env->error_code);
92352926b0dSFabiano Rosas             break;
92452926b0dSFabiano Rosas         }
92552926b0dSFabiano Rosas         break;
92652926b0dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
927bca2c6d9SFabiano Rosas     {
928bca2c6d9SFabiano Rosas         int lev = env->error_code;
92952926b0dSFabiano Rosas 
9302306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
93152926b0dSFabiano Rosas             dump_hcall(env);
93252926b0dSFabiano Rosas         } else {
93352926b0dSFabiano Rosas             dump_syscall(env);
93452926b0dSFabiano Rosas         }
93552926b0dSFabiano Rosas 
93652926b0dSFabiano Rosas         /*
93752926b0dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
93852926b0dSFabiano Rosas          * to point to the next instruction
93952926b0dSFabiano Rosas          */
94052926b0dSFabiano Rosas         env->nip += 4;
94152926b0dSFabiano Rosas 
942bca2c6d9SFabiano Rosas         /*
943bca2c6d9SFabiano Rosas          * The Virtual Open Firmware (VOF) relies on the 'sc 1'
944bca2c6d9SFabiano Rosas          * instruction to communicate with QEMU. The pegasos2 machine
945bca2c6d9SFabiano Rosas          * uses VOF and the 74xx CPUs, so although the 74xx don't have
946bca2c6d9SFabiano Rosas          * HV mode, we need to keep hypercall support.
947bca2c6d9SFabiano Rosas          */
9482306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
94952926b0dSFabiano Rosas             PPCVirtualHypervisorClass *vhc =
95052926b0dSFabiano Rosas                 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
95152926b0dSFabiano Rosas             vhc->hypercall(cpu->vhyp, cpu);
9522c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
95352926b0dSFabiano Rosas             return;
95452926b0dSFabiano Rosas         }
955bca2c6d9SFabiano Rosas 
95652926b0dSFabiano Rosas         break;
957bca2c6d9SFabiano Rosas     }
95852926b0dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
95952926b0dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
96052926b0dSFabiano Rosas         break;
96152926b0dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
9628e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
963bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
964bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset "
96552926b0dSFabiano Rosas                       "exception %d with no HV support\n", excp);
96652926b0dSFabiano Rosas         }
96752926b0dSFabiano Rosas         break;
96852926b0dSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
96952926b0dSFabiano Rosas         break;
97052926b0dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
97152926b0dSFabiano Rosas         break;
97252926b0dSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
97352926b0dSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
97452926b0dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
97552926b0dSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
97652926b0dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
977bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
97852926b0dSFabiano Rosas                   powerpc_excp_name(excp));
97952926b0dSFabiano Rosas         break;
98052926b0dSFabiano Rosas     default:
981bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
982bc30c1c6SBALATON Zoltan                   excp);
98352926b0dSFabiano Rosas         break;
98452926b0dSFabiano Rosas     }
98552926b0dSFabiano Rosas 
98652926b0dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
98752926b0dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
98852926b0dSFabiano Rosas     }
989f82db777SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
990f82db777SFabiano Rosas     env->spr[SPR_SRR1] = msr;
99152926b0dSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
99252926b0dSFabiano Rosas }
99352926b0dSFabiano Rosas 
994180952ceSFabiano Rosas static void powerpc_excp_booke(PowerPCCPU *cpu, int excp)
995180952ceSFabiano Rosas {
996180952ceSFabiano Rosas     CPUPPCState *env = &cpu->env;
997180952ceSFabiano Rosas     target_ulong msr, new_msr, vector;
998ab452503SBALATON Zoltan     int srr0 = SPR_SRR0, srr1 = SPR_SRR1;
999180952ceSFabiano Rosas 
1000ab452503SBALATON Zoltan     /*
1001ab452503SBALATON Zoltan      * Book E does not play games with certain bits of xSRR1 being MSR save
1002ab452503SBALATON Zoltan      * bits and others being error status. xSRR1 is the old MSR, period.
1003ab452503SBALATON Zoltan      */
1004180952ceSFabiano Rosas     msr = env->msr;
1005180952ceSFabiano Rosas 
1006ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
10079dc20cc3SFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
1008180952ceSFabiano Rosas 
1009ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
10109dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
1011180952ceSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
1012180952ceSFabiano Rosas     }
1013180952ceSFabiano Rosas 
1014180952ceSFabiano Rosas #ifdef TARGET_PPC64
1015180952ceSFabiano Rosas     /*
1016180952ceSFabiano Rosas      * SPEU and VPU share the same IVOR but they exist in different
1017180952ceSFabiano Rosas      * processors. SPEU is e500v1/2 only and VPU is e6500 only.
1018180952ceSFabiano Rosas      */
10199dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_VPU) {
1020180952ceSFabiano Rosas         excp = POWERPC_EXCP_SPEU;
1021180952ceSFabiano Rosas     }
1022180952ceSFabiano Rosas #endif
1023180952ceSFabiano Rosas 
1024180952ceSFabiano Rosas     vector = env->excp_vectors[excp];
1025180952ceSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
1026bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
1027bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
1028180952ceSFabiano Rosas     }
1029180952ceSFabiano Rosas     vector |= env->excp_prefix;
1030180952ceSFabiano Rosas 
1031180952ceSFabiano Rosas     switch (excp) {
1032180952ceSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
1033180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
1034180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
1035180952ceSFabiano Rosas         break;
1036180952ceSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
1037819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
1038180952ceSFabiano Rosas         /* machine check exceptions don't have ME set */
1039180952ceSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
1040180952ceSFabiano Rosas 
1041180952ceSFabiano Rosas         /* FIXME: choose one or the other based on CPU type */
1042180952ceSFabiano Rosas         srr0 = SPR_BOOKE_MCSRR0;
1043180952ceSFabiano Rosas         srr1 = SPR_BOOKE_MCSRR1;
1044180952ceSFabiano Rosas 
1045180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR0] = env->nip;
1046180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR1] = msr;
1047db403211SFabiano Rosas 
1048180952ceSFabiano Rosas         break;
1049180952ceSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
1050afdbc869SFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
1051180952ceSFabiano Rosas         break;
1052180952ceSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
1053180952ceSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
1054180952ceSFabiano Rosas         break;
1055180952ceSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
1056180952ceSFabiano Rosas         if (env->mpic_proxy) {
1057bc30c1c6SBALATON Zoltan             CPUState *cs = env_cpu(env);
1058180952ceSFabiano Rosas             /* IACK the IRQ on delivery */
1059180952ceSFabiano Rosas             env->spr[SPR_BOOKE_EPR] = ldl_phys(cs->as, env->mpic_iack);
1060180952ceSFabiano Rosas         }
1061180952ceSFabiano Rosas         break;
1062180952ceSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
1063180952ceSFabiano Rosas         break;
1064180952ceSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
1065180952ceSFabiano Rosas         switch (env->error_code & ~0xF) {
1066180952ceSFabiano Rosas         case POWERPC_EXCP_FP:
1067da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
1068180952ceSFabiano Rosas                 trace_ppc_excp_fp_ignore();
10693680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
1070180952ceSFabiano Rosas                 return;
1071180952ceSFabiano Rosas             }
1072180952ceSFabiano Rosas             /*
1073ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
1074ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
1075180952ceSFabiano Rosas              */
1076180952ceSFabiano Rosas             msr |= 0x00100000;
1077180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_FP;
1078180952ceSFabiano Rosas             break;
1079180952ceSFabiano Rosas         case POWERPC_EXCP_INVAL:
1080180952ceSFabiano Rosas             trace_ppc_excp_inval(env->nip);
1081180952ceSFabiano Rosas             msr |= 0x00080000;
1082180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PIL;
1083180952ceSFabiano Rosas             break;
1084180952ceSFabiano Rosas         case POWERPC_EXCP_PRIV:
1085180952ceSFabiano Rosas             msr |= 0x00040000;
1086180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PPR;
1087180952ceSFabiano Rosas             break;
1088180952ceSFabiano Rosas         case POWERPC_EXCP_TRAP:
1089180952ceSFabiano Rosas             msr |= 0x00020000;
1090180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PTR;
1091180952ceSFabiano Rosas             break;
1092180952ceSFabiano Rosas         default:
1093180952ceSFabiano Rosas             /* Should never occur */
1094bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
1095180952ceSFabiano Rosas                       env->error_code);
1096180952ceSFabiano Rosas             break;
1097180952ceSFabiano Rosas         }
1098180952ceSFabiano Rosas         break;
1099180952ceSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
1100180952ceSFabiano Rosas         dump_syscall(env);
1101180952ceSFabiano Rosas 
1102180952ceSFabiano Rosas         /*
1103180952ceSFabiano Rosas          * We need to correct the NIP which in this case is supposed
1104180952ceSFabiano Rosas          * to point to the next instruction
1105180952ceSFabiano Rosas          */
1106180952ceSFabiano Rosas         env->nip += 4;
1107180952ceSFabiano Rosas         break;
1108180952ceSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
1109180952ceSFabiano Rosas     case POWERPC_EXCP_APU:       /* Auxiliary processor unavailable          */
1110180952ceSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
1111180952ceSFabiano Rosas         break;
1112180952ceSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
1113180952ceSFabiano Rosas         /* FIT on 4xx */
1114180952ceSFabiano Rosas         trace_ppc_excp_print("FIT");
1115180952ceSFabiano Rosas         break;
1116180952ceSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
1117180952ceSFabiano Rosas         trace_ppc_excp_print("WDT");
1118180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
1119180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
1120180952ceSFabiano Rosas         break;
1121180952ceSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
1122180952ceSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
1123180952ceSFabiano Rosas         break;
1124180952ceSFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
1125180952ceSFabiano Rosas         if (env->flags & POWERPC_FLAG_DE) {
1126180952ceSFabiano Rosas             /* FIXME: choose one or the other based on CPU type */
1127180952ceSFabiano Rosas             srr0 = SPR_BOOKE_DSRR0;
1128180952ceSFabiano Rosas             srr1 = SPR_BOOKE_DSRR1;
1129180952ceSFabiano Rosas 
1130180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR0] = env->nip;
1131180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR1] = msr;
1132180952ceSFabiano Rosas 
1133180952ceSFabiano Rosas             /* DBSR already modified by caller */
1134180952ceSFabiano Rosas         } else {
1135bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
1136bc30c1c6SBALATON Zoltan                       "Debug exception triggered on unsupported model\n");
1137180952ceSFabiano Rosas         }
1138180952ceSFabiano Rosas         break;
1139180952ceSFabiano Rosas     case POWERPC_EXCP_SPEU:   /* SPE/embedded floating-point unavailable/VPU  */
1140180952ceSFabiano Rosas         env->spr[SPR_BOOKE_ESR] = ESR_SPV;
1141180952ceSFabiano Rosas         break;
11429364df26SNicholas Piggin     case POWERPC_EXCP_DOORI:     /* Embedded doorbell interrupt              */
11439364df26SNicholas Piggin         break;
11449364df26SNicholas Piggin     case POWERPC_EXCP_DOORCI:    /* Embedded doorbell critical interrupt     */
11459364df26SNicholas Piggin         srr0 = SPR_BOOKE_CSRR0;
11469364df26SNicholas Piggin         srr1 = SPR_BOOKE_CSRR1;
11479364df26SNicholas Piggin         break;
1148180952ceSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
11498e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
1150bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
1151bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset "
1152180952ceSFabiano Rosas                       "exception %d with no HV support\n", excp);
1153180952ceSFabiano Rosas         }
1154180952ceSFabiano Rosas         break;
1155180952ceSFabiano Rosas     case POWERPC_EXCP_EFPDI:     /* Embedded floating-point data interrupt   */
1156180952ceSFabiano Rosas     case POWERPC_EXCP_EFPRI:     /* Embedded floating-point round interrupt  */
1157bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
1158180952ceSFabiano Rosas                   powerpc_excp_name(excp));
1159180952ceSFabiano Rosas         break;
1160180952ceSFabiano Rosas     default:
1161bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1162bc30c1c6SBALATON Zoltan                   excp);
1163180952ceSFabiano Rosas         break;
1164180952ceSFabiano Rosas     }
1165180952ceSFabiano Rosas 
1166f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
1167180952ceSFabiano Rosas     if (env->spr[SPR_BOOKE_EPCR] & EPCR_ICM) {
1168180952ceSFabiano Rosas         /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */
1169180952ceSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_CM;
1170180952ceSFabiano Rosas     } else {
1171180952ceSFabiano Rosas         vector = (uint32_t)vector;
1172180952ceSFabiano Rosas     }
1173180952ceSFabiano Rosas #endif
1174180952ceSFabiano Rosas 
1175180952ceSFabiano Rosas     env->spr[srr0] = env->nip;
1176180952ceSFabiano Rosas     env->spr[srr1] = msr;
1177180952ceSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
1178180952ceSFabiano Rosas }
1179180952ceSFabiano Rosas 
11807cebc5dbSNicholas Piggin /*
11817cebc5dbSNicholas Piggin  * When running a nested HV guest under vhyp, external interrupts are
11827cebc5dbSNicholas Piggin  * delivered as HVIRT.
11837cebc5dbSNicholas Piggin  */
11847cebc5dbSNicholas Piggin static bool books_vhyp_promotes_external_to_hvirt(PowerPCCPU *cpu)
11857cebc5dbSNicholas Piggin {
11867cebc5dbSNicholas Piggin     if (cpu->vhyp) {
11877cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
11887cebc5dbSNicholas Piggin     }
11897cebc5dbSNicholas Piggin     return false;
11907cebc5dbSNicholas Piggin }
11917cebc5dbSNicholas Piggin 
119230c4e426SFabiano Rosas #ifdef TARGET_PPC64
11934c6cf6b2SNicholas Piggin /*
11944c6cf6b2SNicholas Piggin  * When running under vhyp, hcalls are always intercepted and sent to the
11954c6cf6b2SNicholas Piggin  * vhc->hypercall handler.
11964c6cf6b2SNicholas Piggin  */
11974c6cf6b2SNicholas Piggin static bool books_vhyp_handles_hcall(PowerPCCPU *cpu)
11984c6cf6b2SNicholas Piggin {
11994c6cf6b2SNicholas Piggin     if (cpu->vhyp) {
12007cebc5dbSNicholas Piggin         return !vhyp_cpu_in_nested(cpu);
12017cebc5dbSNicholas Piggin     }
12027cebc5dbSNicholas Piggin     return false;
12037cebc5dbSNicholas Piggin }
12047cebc5dbSNicholas Piggin 
12057cebc5dbSNicholas Piggin /*
12067cebc5dbSNicholas Piggin  * When running a nested KVM HV guest under vhyp, HV exceptions are not
12077cebc5dbSNicholas Piggin  * delivered to the guest (because there is no concept of HV support), but
12087cebc5dbSNicholas Piggin  * rather they are sent to the vhyp to exit from the L2 back to the L1 and
12097cebc5dbSNicholas Piggin  * return from the H_ENTER_NESTED hypercall.
12107cebc5dbSNicholas Piggin  */
12117cebc5dbSNicholas Piggin static bool books_vhyp_handles_hv_excp(PowerPCCPU *cpu)
12127cebc5dbSNicholas Piggin {
12137cebc5dbSNicholas Piggin     if (cpu->vhyp) {
12147cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
12154c6cf6b2SNicholas Piggin     }
12164c6cf6b2SNicholas Piggin     return false;
12174c6cf6b2SNicholas Piggin }
12184c6cf6b2SNicholas Piggin 
12195a5d3b23SNicholas Piggin #ifdef CONFIG_TCG
12205a5d3b23SNicholas Piggin static bool is_prefix_insn(CPUPPCState *env, uint32_t insn)
12215a5d3b23SNicholas Piggin {
12225a5d3b23SNicholas Piggin     if (!(env->insns_flags2 & PPC2_ISA310)) {
12235a5d3b23SNicholas Piggin         return false;
12245a5d3b23SNicholas Piggin     }
12255a5d3b23SNicholas Piggin     return ((insn & 0xfc000000) == 0x04000000);
12265a5d3b23SNicholas Piggin }
12275a5d3b23SNicholas Piggin 
12285a5d3b23SNicholas Piggin static bool is_prefix_insn_excp(PowerPCCPU *cpu, int excp)
12295a5d3b23SNicholas Piggin {
12305a5d3b23SNicholas Piggin     CPUPPCState *env = &cpu->env;
12315a5d3b23SNicholas Piggin 
1232c8fd9667SNicholas Piggin     if (!(env->insns_flags2 & PPC2_ISA310)) {
1233c8fd9667SNicholas Piggin         return false;
1234c8fd9667SNicholas Piggin     }
1235c8fd9667SNicholas Piggin 
12365a5d3b23SNicholas Piggin     if (!tcg_enabled()) {
12375a5d3b23SNicholas Piggin         /*
12385a5d3b23SNicholas Piggin          * This does not load instructions and set the prefix bit correctly
12395a5d3b23SNicholas Piggin          * for injected interrupts with KVM. That may have to be discovered
12405a5d3b23SNicholas Piggin          * and set by the KVM layer before injecting.
12415a5d3b23SNicholas Piggin          */
12425a5d3b23SNicholas Piggin         return false;
12435a5d3b23SNicholas Piggin     }
12445a5d3b23SNicholas Piggin 
12455a5d3b23SNicholas Piggin     switch (excp) {
1246c8fd9667SNicholas Piggin     case POWERPC_EXCP_MCHECK:
1247c8fd9667SNicholas Piggin         if (!(env->error_code & PPC_BIT(42))) {
1248c8fd9667SNicholas Piggin             /*
1249c8fd9667SNicholas Piggin              * Fetch attempt caused a machine check, so attempting to fetch
1250c8fd9667SNicholas Piggin              * again would cause a recursive machine check.
1251c8fd9667SNicholas Piggin              */
1252c8fd9667SNicholas Piggin             return false;
1253c8fd9667SNicholas Piggin         }
1254c8fd9667SNicholas Piggin         break;
12555a5d3b23SNicholas Piggin     case POWERPC_EXCP_HDSI:
12565a5d3b23SNicholas Piggin         /* HDSI PRTABLE_FAULT has the originating access type in error_code */
12575a5d3b23SNicholas Piggin         if ((env->spr[SPR_HDSISR] & DSISR_PRTABLE_FAULT) &&
12585a5d3b23SNicholas Piggin             (env->error_code == MMU_INST_FETCH)) {
12595a5d3b23SNicholas Piggin             /*
12605a5d3b23SNicholas Piggin              * Fetch failed due to partition scope translation, so prefix
12615a5d3b23SNicholas Piggin              * indication is not relevant (and attempting to load the
12625a5d3b23SNicholas Piggin              * instruction at NIP would cause recursive faults with the same
12635a5d3b23SNicholas Piggin              * translation).
12645a5d3b23SNicholas Piggin              */
1265c8fd9667SNicholas Piggin             return false;
12665a5d3b23SNicholas Piggin         }
1267c8fd9667SNicholas Piggin         break;
1268c8fd9667SNicholas Piggin 
12695a5d3b23SNicholas Piggin     case POWERPC_EXCP_DSI:
12705a5d3b23SNicholas Piggin     case POWERPC_EXCP_DSEG:
12715a5d3b23SNicholas Piggin     case POWERPC_EXCP_ALIGN:
12725a5d3b23SNicholas Piggin     case POWERPC_EXCP_PROGRAM:
12735a5d3b23SNicholas Piggin     case POWERPC_EXCP_FPU:
12745a5d3b23SNicholas Piggin     case POWERPC_EXCP_TRACE:
12755a5d3b23SNicholas Piggin     case POWERPC_EXCP_HV_EMU:
12765a5d3b23SNicholas Piggin     case POWERPC_EXCP_VPU:
12775a5d3b23SNicholas Piggin     case POWERPC_EXCP_VSXU:
12785a5d3b23SNicholas Piggin     case POWERPC_EXCP_FU:
1279c8fd9667SNicholas Piggin     case POWERPC_EXCP_HV_FU:
12805a5d3b23SNicholas Piggin         break;
12815a5d3b23SNicholas Piggin     default:
12825a5d3b23SNicholas Piggin         return false;
12835a5d3b23SNicholas Piggin     }
1284c8fd9667SNicholas Piggin 
1285c8fd9667SNicholas Piggin     return is_prefix_insn(env, ppc_ldl_code(env, env->nip));
1286c8fd9667SNicholas Piggin }
12875a5d3b23SNicholas Piggin #else
12885a5d3b23SNicholas Piggin static bool is_prefix_insn_excp(PowerPCCPU *cpu, int excp)
12895a5d3b23SNicholas Piggin {
12905a5d3b23SNicholas Piggin     return false;
12915a5d3b23SNicholas Piggin }
12925a5d3b23SNicholas Piggin #endif
12935a5d3b23SNicholas Piggin 
12949f338e4dSFabiano Rosas static void powerpc_excp_books(PowerPCCPU *cpu, int excp)
12959f338e4dSFabiano Rosas {
12969f338e4dSFabiano Rosas     CPUPPCState *env = &cpu->env;
12979f338e4dSFabiano Rosas     target_ulong msr, new_msr, vector;
1298ab452503SBALATON Zoltan     int srr0 = SPR_SRR0, srr1 = SPR_SRR1, lev = -1;
12999f338e4dSFabiano Rosas 
13009f338e4dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
13019f338e4dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
13029f338e4dSFabiano Rosas 
13039f338e4dSFabiano Rosas     /*
1304ab452503SBALATON Zoltan      * new interrupt handler msr preserves HV and ME unless explicitly
1305ab452503SBALATON Zoltan      * overridden
13069f338e4dSFabiano Rosas      */
13079f338e4dSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME) | MSR_HVB);
13089f338e4dSFabiano Rosas 
13099f338e4dSFabiano Rosas     /*
13109f338e4dSFabiano Rosas      * check for special resume at 0x100 from doze/nap/sleep/winkle on
13119f338e4dSFabiano Rosas      * P7/P8/P9
13129f338e4dSFabiano Rosas      */
13139f338e4dSFabiano Rosas     if (env->resume_as_sreset) {
13143f88a89dSBALATON Zoltan         excp = powerpc_reset_wakeup(env, excp, &msr);
13159f338e4dSFabiano Rosas     }
13169f338e4dSFabiano Rosas 
13179f338e4dSFabiano Rosas     /*
131830c4e426SFabiano Rosas      * We don't want to generate a Hypervisor Emulation Assistance
13196c242e79SNicholas Piggin      * Interrupt if we don't have HVB in msr_mask (PAPR mode),
13206c242e79SNicholas Piggin      * unless running a nested-hv guest, in which case the L1
13216c242e79SNicholas Piggin      * kernel wants the interrupt.
13229f338e4dSFabiano Rosas      */
13236c242e79SNicholas Piggin     if (excp == POWERPC_EXCP_HV_EMU && !(env->msr_mask & MSR_HVB) &&
13246c242e79SNicholas Piggin             !books_vhyp_handles_hv_excp(cpu)) {
13259f338e4dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
13269f338e4dSFabiano Rosas     }
13279f338e4dSFabiano Rosas 
13289f338e4dSFabiano Rosas     vector = env->excp_vectors[excp];
13299f338e4dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
1330bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
1331bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
13329f338e4dSFabiano Rosas     }
13339f338e4dSFabiano Rosas     vector |= env->excp_prefix;
13349f338e4dSFabiano Rosas 
13355a5d3b23SNicholas Piggin     if (is_prefix_insn_excp(cpu, excp)) {
13365a5d3b23SNicholas Piggin         msr |= PPC_BIT(34);
13375a5d3b23SNicholas Piggin     }
13385a5d3b23SNicholas Piggin 
13399f338e4dSFabiano Rosas     switch (excp) {
13409f338e4dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
1341819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
13429f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
13439f338e4dSFabiano Rosas             /*
13449f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
13459f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR).
13469f338e4dSFabiano Rosas              */
13479f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
1348*43453161SNicholas Piggin 
1349*43453161SNicholas Piggin             /* HV machine check exceptions don't have ME set */
13509f338e4dSFabiano Rosas             new_msr &= ~((target_ulong)1 << MSR_ME);
1351*43453161SNicholas Piggin         }
13529f338e4dSFabiano Rosas 
135355a7fa34SNicholas Piggin         msr |= env->error_code;
13549f338e4dSFabiano Rosas         break;
135555a7fa34SNicholas Piggin 
13569f338e4dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
13579f338e4dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
13589f338e4dSFabiano Rosas         break;
13599f338e4dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
13609f338e4dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
13619f338e4dSFabiano Rosas         msr |= env->error_code;
13629f338e4dSFabiano Rosas         break;
13639f338e4dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
13649f338e4dSFabiano Rosas     {
13659f338e4dSFabiano Rosas         bool lpes0;
13669f338e4dSFabiano Rosas 
1367ab452503SBALATON Zoltan         /* LPES0 is only taken into consideration if we support HV mode */
136867baff77SFabiano Rosas         if (!env->has_hv_mode) {
136967baff77SFabiano Rosas             break;
13709f338e4dSFabiano Rosas         }
137167baff77SFabiano Rosas         lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
13729f338e4dSFabiano Rosas         if (!lpes0) {
13739f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
13749f338e4dSFabiano Rosas             new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
13759f338e4dSFabiano Rosas             srr0 = SPR_HSRR0;
13769f338e4dSFabiano Rosas             srr1 = SPR_HSRR1;
13779f338e4dSFabiano Rosas         }
13789f338e4dSFabiano Rosas         break;
13799f338e4dSFabiano Rosas     }
13809f338e4dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
13814ee5d281SNicholas Piggin         /* Optional DSISR update was removed from ISA v3.0 */
13824ee5d281SNicholas Piggin         if (!(env->insns_flags2 & PPC2_ISA300)) {
13839f338e4dSFabiano Rosas             /* Get rS/rD and rA from faulting opcode */
13849f338e4dSFabiano Rosas             /*
13859f338e4dSFabiano Rosas              * Note: the opcode fields will not be set properly for a
13869f338e4dSFabiano Rosas              * direct store load/store, but nobody cares as nobody
13879f338e4dSFabiano Rosas              * actually uses direct store segments.
13889f338e4dSFabiano Rosas              */
13899f338e4dSFabiano Rosas             env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
13904ee5d281SNicholas Piggin         }
13919f338e4dSFabiano Rosas         break;
13929f338e4dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
13939f338e4dSFabiano Rosas         switch (env->error_code & ~0xF) {
13949f338e4dSFabiano Rosas         case POWERPC_EXCP_FP:
1395da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
13969f338e4dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
13973680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
13989f338e4dSFabiano Rosas                 return;
13999f338e4dSFabiano Rosas             }
14009f338e4dSFabiano Rosas             /*
1401ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
1402ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
14039f338e4dSFabiano Rosas              */
14049f338e4dSFabiano Rosas             msr |= 0x00100000;
14059f338e4dSFabiano Rosas             break;
14069f338e4dSFabiano Rosas         case POWERPC_EXCP_INVAL:
14079f338e4dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
14089f338e4dSFabiano Rosas             msr |= 0x00080000;
14099f338e4dSFabiano Rosas             break;
14109f338e4dSFabiano Rosas         case POWERPC_EXCP_PRIV:
14119f338e4dSFabiano Rosas             msr |= 0x00040000;
14129f338e4dSFabiano Rosas             break;
14139f338e4dSFabiano Rosas         case POWERPC_EXCP_TRAP:
14149f338e4dSFabiano Rosas             msr |= 0x00020000;
14159f338e4dSFabiano Rosas             break;
14169f338e4dSFabiano Rosas         default:
14179f338e4dSFabiano Rosas             /* Should never occur */
1418bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
14199f338e4dSFabiano Rosas                       env->error_code);
14209f338e4dSFabiano Rosas             break;
14219f338e4dSFabiano Rosas         }
14229f338e4dSFabiano Rosas         break;
14239f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
14249f338e4dSFabiano Rosas         lev = env->error_code;
14259f338e4dSFabiano Rosas 
14262306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
14279f338e4dSFabiano Rosas             dump_hcall(env);
14289f338e4dSFabiano Rosas         } else {
14299f338e4dSFabiano Rosas             dump_syscall(env);
14309f338e4dSFabiano Rosas         }
14319f338e4dSFabiano Rosas 
14329f338e4dSFabiano Rosas         /*
14339f338e4dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
14349f338e4dSFabiano Rosas          * to point to the next instruction
14359f338e4dSFabiano Rosas          */
14369f338e4dSFabiano Rosas         env->nip += 4;
14379f338e4dSFabiano Rosas 
14389f338e4dSFabiano Rosas         /* "PAPR mode" built-in hypercall emulation */
14392306c606SBALATON Zoltan         if (lev == 1 && books_vhyp_handles_hcall(cpu)) {
14409f338e4dSFabiano Rosas             PPCVirtualHypervisorClass *vhc =
14419f338e4dSFabiano Rosas                 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
14429f338e4dSFabiano Rosas             vhc->hypercall(cpu->vhyp, cpu);
14432c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
14449f338e4dSFabiano Rosas             return;
14459f338e4dSFabiano Rosas         }
1446eb701f30SNicholas Piggin         if (env->insns_flags2 & PPC2_ISA310) {
1447eb701f30SNicholas Piggin             /* ISAv3.1 puts LEV into SRR1 */
1448eb701f30SNicholas Piggin             msr |= lev << 20;
1449eb701f30SNicholas Piggin         }
14509f338e4dSFabiano Rosas         if (lev == 1) {
14519f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14529f338e4dSFabiano Rosas         }
14539f338e4dSFabiano Rosas         break;
14549f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL_VECTORED: /* scv exception                     */
14559f338e4dSFabiano Rosas         lev = env->error_code;
14569f338e4dSFabiano Rosas         dump_syscall(env);
14579f338e4dSFabiano Rosas         env->nip += 4;
14589f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_EE);
14599f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
14609f338e4dSFabiano Rosas 
14619f338e4dSFabiano Rosas         vector += lev * 0x20;
14629f338e4dSFabiano Rosas 
14639f338e4dSFabiano Rosas         env->lr = env->nip;
14649f338e4dSFabiano Rosas         env->ctr = msr;
14659f338e4dSFabiano Rosas         break;
14669f338e4dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
14679f338e4dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
14689f338e4dSFabiano Rosas         break;
14699f338e4dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
14709f338e4dSFabiano Rosas         /* A power-saving exception sets ME, otherwise it is unchanged */
14718e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
14729f338e4dSFabiano Rosas             /* indicate that we resumed from power save mode */
14739f338e4dSFabiano Rosas             msr |= 0x10000;
14749f338e4dSFabiano Rosas             new_msr |= ((target_ulong)1 << MSR_ME);
14759f338e4dSFabiano Rosas         }
14769f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
14779f338e4dSFabiano Rosas             /*
14789f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
14799f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR, NMI injection in QEMU).
14809f338e4dSFabiano Rosas              */
14819f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14829f338e4dSFabiano Rosas         } else {
14838e54ad65SVíctor Colombo             if (FIELD_EX64(env->msr, MSR, POW)) {
1484bc30c1c6SBALATON Zoltan                 cpu_abort(env_cpu(env),
1485bc30c1c6SBALATON Zoltan                           "Trying to deliver power-saving system reset "
14869f338e4dSFabiano Rosas                           "exception %d with no HV support\n", excp);
14879f338e4dSFabiano Rosas             }
14889f338e4dSFabiano Rosas         }
14899f338e4dSFabiano Rosas         break;
149014895384SNicholas Piggin     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
149114895384SNicholas Piggin         msr |= env->error_code;
149214895384SNicholas Piggin         /* fall through */
14939f338e4dSFabiano Rosas     case POWERPC_EXCP_DSEG:      /* Data segment exception                   */
14949f338e4dSFabiano Rosas     case POWERPC_EXCP_ISEG:      /* Instruction segment exception            */
1495fd7abfabSNicholas Piggin     case POWERPC_EXCP_SDOOR:     /* Doorbell interrupt                       */
1496c29b0704SNicholas Piggin     case POWERPC_EXCP_PERFM:     /* Performance monitor interrupt            */
14979f338e4dSFabiano Rosas         break;
14989f338e4dSFabiano Rosas     case POWERPC_EXCP_HISI:      /* Hypervisor instruction storage exception */
14999f338e4dSFabiano Rosas         msr |= env->error_code;
15009f338e4dSFabiano Rosas         /* fall through */
15019f338e4dSFabiano Rosas     case POWERPC_EXCP_HDECR:     /* Hypervisor decrementer exception         */
15029f338e4dSFabiano Rosas     case POWERPC_EXCP_HDSI:      /* Hypervisor data storage exception        */
15039f338e4dSFabiano Rosas     case POWERPC_EXCP_SDOOR_HV:  /* Hypervisor Doorbell interrupt            */
15049f338e4dSFabiano Rosas     case POWERPC_EXCP_HVIRT:     /* Hypervisor virtualization                */
15059f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
15069f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
15079f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
15089f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
15099f338e4dSFabiano Rosas         break;
1510a3c020d8SNicholas Piggin #ifdef CONFIG_TCG
1511a3c020d8SNicholas Piggin     case POWERPC_EXCP_HV_EMU: {
1512a3c020d8SNicholas Piggin         uint32_t insn = ppc_ldl_code(env, env->nip);
1513a3c020d8SNicholas Piggin         env->spr[SPR_HEIR] = insn;
1514a3c020d8SNicholas Piggin         if (is_prefix_insn(env, insn)) {
1515a3c020d8SNicholas Piggin             uint32_t insn2 = ppc_ldl_code(env, env->nip + 4);
1516a3c020d8SNicholas Piggin             env->spr[SPR_HEIR] <<= 32;
1517a3c020d8SNicholas Piggin             env->spr[SPR_HEIR] |= insn2;
1518a3c020d8SNicholas Piggin         }
1519a3c020d8SNicholas Piggin         srr0 = SPR_HSRR0;
1520a3c020d8SNicholas Piggin         srr1 = SPR_HSRR1;
1521a3c020d8SNicholas Piggin         new_msr |= (target_ulong)MSR_HVB;
1522a3c020d8SNicholas Piggin         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
1523a3c020d8SNicholas Piggin         break;
1524a3c020d8SNicholas Piggin     }
1525a3c020d8SNicholas Piggin #endif
15269f338e4dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
15279f338e4dSFabiano Rosas     case POWERPC_EXCP_VSXU:       /* VSX unavailable exception               */
15289f338e4dSFabiano Rosas     case POWERPC_EXCP_FU:         /* Facility unavailable exception          */
15299f338e4dSFabiano Rosas         env->spr[SPR_FSCR] |= ((target_ulong)env->error_code << 56);
15309f338e4dSFabiano Rosas         break;
15319f338e4dSFabiano Rosas     case POWERPC_EXCP_HV_FU:     /* Hypervisor Facility Unavailable Exception */
15329f338e4dSFabiano Rosas         env->spr[SPR_HFSCR] |= ((target_ulong)env->error_code << FSCR_IC_POS);
15339f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
15349f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
15359f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
15369f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
15379f338e4dSFabiano Rosas         break;
1538cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_PERFM_EBB:        /* Performance Monitor EBB Exception  */
1539cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_EXTERNAL_EBB:     /* External EBB Exception             */
1540cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
1541cb76bbc4SDaniel Henrique Barboza 
1542cb76bbc4SDaniel Henrique Barboza         /*
1543cb76bbc4SDaniel Henrique Barboza          * Save NIP for rfebb insn in SPR_EBBRR. Next nip is
1544cb76bbc4SDaniel Henrique Barboza          * stored in the EBB Handler SPR_EBBHR.
1545cb76bbc4SDaniel Henrique Barboza          */
1546cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_EBBRR] = env->nip;
1547cb76bbc4SDaniel Henrique Barboza         powerpc_set_excp_state(cpu, env->spr[SPR_EBBHR], env->msr);
1548cb76bbc4SDaniel Henrique Barboza 
1549cb76bbc4SDaniel Henrique Barboza         /*
1550cb76bbc4SDaniel Henrique Barboza          * This exception is handled in userspace. No need to proceed.
1551cb76bbc4SDaniel Henrique Barboza          */
1552cb76bbc4SDaniel Henrique Barboza         return;
15539f338e4dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
15549f338e4dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
15559f338e4dSFabiano Rosas     case POWERPC_EXCP_MAINT:     /* Maintenance exception                    */
155630c4e426SFabiano Rosas     case POWERPC_EXCP_HV_MAINT:  /* Hypervisor Maintenance exception         */
1557bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
15589f338e4dSFabiano Rosas                   powerpc_excp_name(excp));
15599f338e4dSFabiano Rosas         break;
15609f338e4dSFabiano Rosas     default:
1561bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1562bc30c1c6SBALATON Zoltan                   excp);
15639f338e4dSFabiano Rosas         break;
15649f338e4dSFabiano Rosas     }
15659f338e4dSFabiano Rosas 
15669f338e4dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
15679f338e4dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
15689f338e4dSFabiano Rosas     }
15699f338e4dSFabiano Rosas     new_msr |= (target_ulong)1 << MSR_SF;
15709f338e4dSFabiano Rosas 
15719f338e4dSFabiano Rosas     if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
15729f338e4dSFabiano Rosas         env->spr[srr0] = env->nip;
15739f338e4dSFabiano Rosas         env->spr[srr1] = msr;
15749f338e4dSFabiano Rosas     }
15759f338e4dSFabiano Rosas 
15767cebc5dbSNicholas Piggin     if ((new_msr & MSR_HVB) && books_vhyp_handles_hv_excp(cpu)) {
15777cebc5dbSNicholas Piggin         PPCVirtualHypervisorClass *vhc =
15787cebc5dbSNicholas Piggin             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
15797cebc5dbSNicholas Piggin         /* Deliver interrupt to L1 by returning from the H_ENTER_NESTED call */
15807cebc5dbSNicholas Piggin         vhc->deliver_hv_excp(cpu, excp);
15817cebc5dbSNicholas Piggin         powerpc_reset_excp_state(cpu);
15827cebc5dbSNicholas Piggin     } else {
15837cebc5dbSNicholas Piggin         /* Sanity check */
15847cebc5dbSNicholas Piggin         if (!(env->msr_mask & MSR_HVB) && srr0 == SPR_HSRR0) {
1585bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Trying to deliver HV exception (HSRR) %d "
1586bc30c1c6SBALATON Zoltan                       "with no HV support\n", excp);
15877cebc5dbSNicholas Piggin         }
15889f338e4dSFabiano Rosas         /* This can update new_msr and vector if AIL applies */
158910895ab6SFabiano Rosas         ppc_excp_apply_ail(cpu, excp, msr, &new_msr, &vector);
15909f338e4dSFabiano Rosas         powerpc_set_excp_state(cpu, vector, new_msr);
15919f338e4dSFabiano Rosas     }
15927cebc5dbSNicholas Piggin }
159330c4e426SFabiano Rosas #else
159430c4e426SFabiano Rosas static inline void powerpc_excp_books(PowerPCCPU *cpu, int excp)
159530c4e426SFabiano Rosas {
159630c4e426SFabiano Rosas     g_assert_not_reached();
159730c4e426SFabiano Rosas }
1598f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
15999f338e4dSFabiano Rosas 
1600dc88dd0aSFabiano Rosas static void powerpc_excp(PowerPCCPU *cpu, int excp)
1601dc88dd0aSFabiano Rosas {
1602dc88dd0aSFabiano Rosas     CPUPPCState *env = &cpu->env;
1603dc88dd0aSFabiano Rosas 
1604c6eaac89SFabiano Rosas     if (excp <= POWERPC_EXCP_NONE || excp >= POWERPC_EXCP_NB) {
1605bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1606bc30c1c6SBALATON Zoltan                   excp);
1607c6eaac89SFabiano Rosas     }
1608c6eaac89SFabiano Rosas 
1609c6eaac89SFabiano Rosas     qemu_log_mask(CPU_LOG_INT, "Raise exception at " TARGET_FMT_lx
1610c6eaac89SFabiano Rosas                   " => %s (%d) error=%02x\n", env->nip, powerpc_excp_name(excp),
1611c6eaac89SFabiano Rosas                   excp, env->error_code);
16128a15cceeSBALATON Zoltan     env->excp_stats[excp]++;
1613c6eaac89SFabiano Rosas 
1614dc88dd0aSFabiano Rosas     switch (env->excp_model) {
1615e808c2edSFabiano Rosas     case POWERPC_EXCP_40x:
1616e808c2edSFabiano Rosas         powerpc_excp_40x(cpu, excp);
1617e808c2edSFabiano Rosas         break;
161858d178fbSFabiano Rosas     case POWERPC_EXCP_6xx:
161958d178fbSFabiano Rosas         powerpc_excp_6xx(cpu, excp);
162058d178fbSFabiano Rosas         break;
1621ccfca2fcSFabiano Rosas     case POWERPC_EXCP_7xx:
1622ccfca2fcSFabiano Rosas         powerpc_excp_7xx(cpu, excp);
1623ccfca2fcSFabiano Rosas         break;
162452926b0dSFabiano Rosas     case POWERPC_EXCP_74xx:
162552926b0dSFabiano Rosas         powerpc_excp_74xx(cpu, excp);
162652926b0dSFabiano Rosas         break;
1627180952ceSFabiano Rosas     case POWERPC_EXCP_BOOKE:
1628180952ceSFabiano Rosas         powerpc_excp_booke(cpu, excp);
1629180952ceSFabiano Rosas         break;
16309f338e4dSFabiano Rosas     case POWERPC_EXCP_970:
16319f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER7:
16329f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER8:
16339f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER9:
16349f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER10:
16359f338e4dSFabiano Rosas         powerpc_excp_books(cpu, excp);
16369f338e4dSFabiano Rosas         break;
1637dc88dd0aSFabiano Rosas     default:
163828091374SFabiano Rosas         g_assert_not_reached();
1639dc88dd0aSFabiano Rosas     }
1640dc88dd0aSFabiano Rosas }
1641dc88dd0aSFabiano Rosas 
164297a8ea5aSAndreas Färber void ppc_cpu_do_interrupt(CPUState *cs)
1643c79c73f6SBlue Swirl {
164497a8ea5aSAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
16455c26a5b3SAndreas Färber 
164693130c84SFabiano Rosas     powerpc_excp(cpu, cs->exception_index);
1647c79c73f6SBlue Swirl }
1648c79c73f6SBlue Swirl 
1649f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
1650c8e1de2eSMatheus Ferst #define P7_UNUSED_INTERRUPTS \
1651c8e1de2eSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_HVIRT | PPC_INTERRUPT_CEXT |       \
1652c8e1de2eSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |      \
1653c8e1de2eSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_DOORBELL | PPC_INTERRUPT_HDOORBELL | \
1654c8e1de2eSMatheus Ferst      PPC_INTERRUPT_THERM | PPC_INTERRUPT_EBB)
1655c8e1de2eSMatheus Ferst 
16569c713713SMatheus Ferst static int p7_interrupt_powersave(CPUPPCState *env)
16579c713713SMatheus Ferst {
16589c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
16599c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE0)) {
16609c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
16619c713713SMatheus Ferst     }
16629c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
16639c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE1)) {
16649c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
16659c713713SMatheus Ferst     }
16669c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_MCK) &&
16679c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
16689c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
16699c713713SMatheus Ferst     }
16709c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HMI) &&
16719c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
16729c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
16739c713713SMatheus Ferst     }
16749c713713SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
16759c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
16769c713713SMatheus Ferst     }
16779c713713SMatheus Ferst     return 0;
16789c713713SMatheus Ferst }
16799c713713SMatheus Ferst 
1680bf303fb3SMatheus Ferst static int p7_next_unmasked_interrupt(CPUPPCState *env)
1681bf303fb3SMatheus Ferst {
16824e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
16834e6b7db2SBALATON Zoltan 
1684022b7128SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
1685022b7128SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1686bf303fb3SMatheus Ferst 
1687c8e1de2eSMatheus Ferst     assert((env->pending_interrupts & P7_UNUSED_INTERRUPTS) == 0);
1688c8e1de2eSMatheus Ferst 
1689022b7128SMatheus Ferst     if (cs->halted) {
1690022b7128SMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
1691022b7128SMatheus Ferst         return p7_interrupt_powersave(env);
1692022b7128SMatheus Ferst     }
1693022b7128SMatheus Ferst 
1694bf303fb3SMatheus Ferst     /* Machine check exception */
1695bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1696bf303fb3SMatheus Ferst         return PPC_INTERRUPT_MCK;
1697bf303fb3SMatheus Ferst     }
1698bf303fb3SMatheus Ferst 
1699bf303fb3SMatheus Ferst     /* Hypervisor decrementer exception */
1700bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1701bf303fb3SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1702bf303fb3SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
1703022b7128SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1704bf303fb3SMatheus Ferst             /* HDEC clears on delivery */
1705bf303fb3SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1706bf303fb3SMatheus Ferst         }
1707bf303fb3SMatheus Ferst     }
1708bf303fb3SMatheus Ferst 
1709bf303fb3SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1710bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1711bf303fb3SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1712bf303fb3SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1713bf303fb3SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
1714022b7128SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1715bf303fb3SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1716bf303fb3SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1717bf303fb3SMatheus Ferst             return PPC_INTERRUPT_EXT;
1718bf303fb3SMatheus Ferst         }
1719bf303fb3SMatheus Ferst     }
1720022b7128SMatheus Ferst     if (msr_ee != 0) {
1721bf303fb3SMatheus Ferst         /* Decrementer exception */
1722bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1723bf303fb3SMatheus Ferst             return PPC_INTERRUPT_DECR;
1724bf303fb3SMatheus Ferst         }
1725bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1726bf303fb3SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1727bf303fb3SMatheus Ferst         }
1728bf303fb3SMatheus Ferst     }
1729bf303fb3SMatheus Ferst 
1730bf303fb3SMatheus Ferst     return 0;
1731bf303fb3SMatheus Ferst }
1732bf303fb3SMatheus Ferst 
1733f6194fddSMatheus Ferst #define P8_UNUSED_INTERRUPTS \
1734f6194fddSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_HVIRT |  \
1735f6194fddSMatheus Ferst     PPC_INTERRUPT_CEXT | PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL |  \
1736f6194fddSMatheus Ferst     PPC_INTERRUPT_FIT | PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1737f6194fddSMatheus Ferst 
17389c713713SMatheus Ferst static int p8_interrupt_powersave(CPUPPCState *env)
17399c713713SMatheus Ferst {
17409c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
17419c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE2)) {
17429c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
17439c713713SMatheus Ferst     }
17449c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
17459c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE3)) {
17469c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
17479c713713SMatheus Ferst     }
17489c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_MCK) &&
17499c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) {
17509c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
17519c713713SMatheus Ferst     }
17529c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HMI) &&
17539c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) {
17549c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
17559c713713SMatheus Ferst     }
17569c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
17579c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE0)) {
17589c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
17599c713713SMatheus Ferst     }
17609c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
17619c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE1)) {
17629c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
17639c713713SMatheus Ferst     }
17649c713713SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
17659c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
17669c713713SMatheus Ferst     }
17679c713713SMatheus Ferst     return 0;
17689c713713SMatheus Ferst }
17699c713713SMatheus Ferst 
1770a9899d42SMatheus Ferst static int p8_next_unmasked_interrupt(CPUPPCState *env)
1771a9899d42SMatheus Ferst {
17724e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
17734e6b7db2SBALATON Zoltan 
177464a9b5eeSMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
177564a9b5eeSMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1776a9899d42SMatheus Ferst 
1777f6194fddSMatheus Ferst     assert((env->pending_interrupts & P8_UNUSED_INTERRUPTS) == 0);
1778f6194fddSMatheus Ferst 
177964a9b5eeSMatheus Ferst     if (cs->halted) {
178064a9b5eeSMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
178164a9b5eeSMatheus Ferst         return p8_interrupt_powersave(env);
178264a9b5eeSMatheus Ferst     }
178364a9b5eeSMatheus Ferst 
1784a9899d42SMatheus Ferst     /* Machine check exception */
1785a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1786a9899d42SMatheus Ferst         return PPC_INTERRUPT_MCK;
1787a9899d42SMatheus Ferst     }
1788a9899d42SMatheus Ferst 
1789a9899d42SMatheus Ferst     /* Hypervisor decrementer exception */
1790a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1791a9899d42SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1792a9899d42SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
179364a9b5eeSMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1794a9899d42SMatheus Ferst             /* HDEC clears on delivery */
1795a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1796a9899d42SMatheus Ferst         }
1797a9899d42SMatheus Ferst     }
1798a9899d42SMatheus Ferst 
1799a9899d42SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1800a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1801a9899d42SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1802a9899d42SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1803a9899d42SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
180464a9b5eeSMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1805a9899d42SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1806a9899d42SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1807a9899d42SMatheus Ferst             return PPC_INTERRUPT_EXT;
1808a9899d42SMatheus Ferst         }
1809a9899d42SMatheus Ferst     }
181064a9b5eeSMatheus Ferst     if (msr_ee != 0) {
1811a9899d42SMatheus Ferst         /* Decrementer exception */
1812a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1813a9899d42SMatheus Ferst             return PPC_INTERRUPT_DECR;
1814a9899d42SMatheus Ferst         }
1815a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1816a9899d42SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
1817a9899d42SMatheus Ferst         }
1818a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
1819a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
1820a9899d42SMatheus Ferst         }
1821a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1822a9899d42SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1823a9899d42SMatheus Ferst         }
1824a9899d42SMatheus Ferst         /* EBB exception */
1825a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
1826a9899d42SMatheus Ferst             /*
1827a9899d42SMatheus Ferst              * EBB exception must be taken in problem state and
1828a9899d42SMatheus Ferst              * with BESCR_GE set.
1829a9899d42SMatheus Ferst              */
1830a9899d42SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
1831a9899d42SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
1832a9899d42SMatheus Ferst                 return PPC_INTERRUPT_EBB;
1833a9899d42SMatheus Ferst             }
1834a9899d42SMatheus Ferst         }
1835a9899d42SMatheus Ferst     }
1836a9899d42SMatheus Ferst 
1837a9899d42SMatheus Ferst     return 0;
1838a9899d42SMatheus Ferst }
1839a9899d42SMatheus Ferst 
1840b00e9a2fSMatheus Ferst #define P9_UNUSED_INTERRUPTS \
1841b00e9a2fSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT |   \
1842b00e9a2fSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |  \
1843b00e9a2fSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1844b00e9a2fSMatheus Ferst 
18459c713713SMatheus Ferst static int p9_interrupt_powersave(CPUPPCState *env)
18469c713713SMatheus Ferst {
18479c713713SMatheus Ferst     /* External Exception */
18489c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
18499c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_EEE)) {
18509c713713SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
18519c713713SMatheus Ferst         if (!heic || !FIELD_EX64_HV(env->msr) ||
18529c713713SMatheus Ferst             FIELD_EX64(env->msr, MSR, PR)) {
18539c713713SMatheus Ferst             return PPC_INTERRUPT_EXT;
18549c713713SMatheus Ferst         }
18559c713713SMatheus Ferst     }
18569c713713SMatheus Ferst     /* Decrementer Exception */
18579c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
18589c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_DEE)) {
18599c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
18609c713713SMatheus Ferst     }
18619c713713SMatheus Ferst     /* Machine Check or Hypervisor Maintenance Exception */
18629c713713SMatheus Ferst     if (env->spr[SPR_LPCR] & LPCR_OEE) {
18639c713713SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
18649c713713SMatheus Ferst             return PPC_INTERRUPT_MCK;
18659c713713SMatheus Ferst         }
18669c713713SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HMI) {
18679c713713SMatheus Ferst             return PPC_INTERRUPT_HMI;
18689c713713SMatheus Ferst         }
18699c713713SMatheus Ferst     }
18709c713713SMatheus Ferst     /* Privileged Doorbell Exception */
18719c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
18729c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_PDEE)) {
18739c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
18749c713713SMatheus Ferst     }
18759c713713SMatheus Ferst     /* Hypervisor Doorbell Exception */
18769c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
18779c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_HDEE)) {
18789c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
18799c713713SMatheus Ferst     }
18809c713713SMatheus Ferst     /* Hypervisor virtualization exception */
18819c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HVIRT) &&
18829c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_HVEE)) {
18839c713713SMatheus Ferst         return PPC_INTERRUPT_HVIRT;
18849c713713SMatheus Ferst     }
18859c713713SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
18869c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
18879c713713SMatheus Ferst     }
18889c713713SMatheus Ferst     return 0;
18899c713713SMatheus Ferst }
18909c713713SMatheus Ferst 
18912dfecf01SMatheus Ferst static int p9_next_unmasked_interrupt(CPUPPCState *env)
18922dfecf01SMatheus Ferst {
18934e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
18944e6b7db2SBALATON Zoltan 
189527796411SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
189627796411SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
18972dfecf01SMatheus Ferst 
1898b00e9a2fSMatheus Ferst     assert((env->pending_interrupts & P9_UNUSED_INTERRUPTS) == 0);
1899b00e9a2fSMatheus Ferst 
190027796411SMatheus Ferst     if (cs->halted) {
190127796411SMatheus Ferst         if (env->spr[SPR_PSSCR] & PSSCR_EC) {
190227796411SMatheus Ferst             /*
190327796411SMatheus Ferst              * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can
190427796411SMatheus Ferst              * wakeup the processor
190527796411SMatheus Ferst              */
190627796411SMatheus Ferst             return p9_interrupt_powersave(env);
190727796411SMatheus Ferst         } else {
190827796411SMatheus Ferst             /*
190927796411SMatheus Ferst              * When it's clear, any system-caused exception exits power-saving
191027796411SMatheus Ferst              * mode, even the ones that gate on MSR[EE].
191127796411SMatheus Ferst              */
191227796411SMatheus Ferst             msr_ee = true;
191327796411SMatheus Ferst         }
191427796411SMatheus Ferst     }
191527796411SMatheus Ferst 
19162dfecf01SMatheus Ferst     /* Machine check exception */
19172dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
19182dfecf01SMatheus Ferst         return PPC_INTERRUPT_MCK;
19192dfecf01SMatheus Ferst     }
19202dfecf01SMatheus Ferst 
19212dfecf01SMatheus Ferst     /* Hypervisor decrementer exception */
19222dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
19232dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
19242dfecf01SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
192527796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
19262dfecf01SMatheus Ferst             /* HDEC clears on delivery */
19272dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDECR;
19282dfecf01SMatheus Ferst         }
19292dfecf01SMatheus Ferst     }
19302dfecf01SMatheus Ferst 
19312dfecf01SMatheus Ferst     /* Hypervisor virtualization interrupt */
19322dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
19332dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
19342dfecf01SMatheus Ferst         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
193527796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hvice) {
19362dfecf01SMatheus Ferst             return PPC_INTERRUPT_HVIRT;
19372dfecf01SMatheus Ferst         }
19382dfecf01SMatheus Ferst     }
19392dfecf01SMatheus Ferst 
19402dfecf01SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
19412dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
19422dfecf01SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
19432dfecf01SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
19442dfecf01SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
194527796411SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
19462dfecf01SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
19472dfecf01SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
19482dfecf01SMatheus Ferst             return PPC_INTERRUPT_EXT;
19492dfecf01SMatheus Ferst         }
19502dfecf01SMatheus Ferst     }
195127796411SMatheus Ferst     if (msr_ee != 0) {
19522dfecf01SMatheus Ferst         /* Decrementer exception */
19532dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
19542dfecf01SMatheus Ferst             return PPC_INTERRUPT_DECR;
19552dfecf01SMatheus Ferst         }
19562dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
19572dfecf01SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
19582dfecf01SMatheus Ferst         }
19592dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
19602dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
19612dfecf01SMatheus Ferst         }
19622dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
19632dfecf01SMatheus Ferst             return PPC_INTERRUPT_PERFM;
19642dfecf01SMatheus Ferst         }
19652dfecf01SMatheus Ferst         /* EBB exception */
19662dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
19672dfecf01SMatheus Ferst             /*
19682dfecf01SMatheus Ferst              * EBB exception must be taken in problem state and
19692dfecf01SMatheus Ferst              * with BESCR_GE set.
19702dfecf01SMatheus Ferst              */
19712dfecf01SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
19722dfecf01SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
19732dfecf01SMatheus Ferst                 return PPC_INTERRUPT_EBB;
19742dfecf01SMatheus Ferst             }
19752dfecf01SMatheus Ferst         }
19762dfecf01SMatheus Ferst     }
19772dfecf01SMatheus Ferst 
19782dfecf01SMatheus Ferst     return 0;
19792dfecf01SMatheus Ferst }
1980f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
19812dfecf01SMatheus Ferst 
1982868cb6baSBALATON Zoltan static int ppc_next_unmasked_interrupt(CPUPPCState *env)
1983c79c73f6SBlue Swirl {
1984868cb6baSBALATON Zoltan #ifdef TARGET_PPC64
1985868cb6baSBALATON Zoltan     switch (env->excp_model) {
1986868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER7:
1987868cb6baSBALATON Zoltan         return p7_next_unmasked_interrupt(env);
1988868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER8:
1989868cb6baSBALATON Zoltan         return p8_next_unmasked_interrupt(env);
1990868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER9:
1991868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER10:
1992868cb6baSBALATON Zoltan         return p9_next_unmasked_interrupt(env);
1993868cb6baSBALATON Zoltan     default:
1994868cb6baSBALATON Zoltan         break;
1995868cb6baSBALATON Zoltan     }
1996868cb6baSBALATON Zoltan #endif
19973621e2c9SBenjamin Herrenschmidt     bool async_deliver;
1998259186a7SAndreas Färber 
1999c79c73f6SBlue Swirl     /* External reset */
2000f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
2001de76b85cSMatheus Ferst         return PPC_INTERRUPT_RESET;
2002c79c73f6SBlue Swirl     }
2003c79c73f6SBlue Swirl     /* Machine check exception */
2004f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
2005de76b85cSMatheus Ferst         return PPC_INTERRUPT_MCK;
2006c79c73f6SBlue Swirl     }
2007c79c73f6SBlue Swirl #if 0 /* TODO */
2008c79c73f6SBlue Swirl     /* External debug exception */
2009f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) {
2010de76b85cSMatheus Ferst         return PPC_INTERRUPT_DEBUG;
2011c79c73f6SBlue Swirl     }
2012c79c73f6SBlue Swirl #endif
20133621e2c9SBenjamin Herrenschmidt 
20143621e2c9SBenjamin Herrenschmidt     /*
20153621e2c9SBenjamin Herrenschmidt      * For interrupts that gate on MSR:EE, we need to do something a
20163621e2c9SBenjamin Herrenschmidt      * bit more subtle, as we need to let them through even when EE is
20173621e2c9SBenjamin Herrenschmidt      * clear when coming out of some power management states (in order
20183621e2c9SBenjamin Herrenschmidt      * for them to become a 0x100).
20193621e2c9SBenjamin Herrenschmidt      */
20200939b8f8SVíctor Colombo     async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
20213621e2c9SBenjamin Herrenschmidt 
2022c79c73f6SBlue Swirl     /* Hypervisor decrementer exception */
2023f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
20244b236b62SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
20254b236b62SBenjamin Herrenschmidt         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
20269de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
20274b236b62SBenjamin Herrenschmidt             /* HDEC clears on delivery */
2028de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDECR;
2029c79c73f6SBlue Swirl         }
2030c79c73f6SBlue Swirl     }
2031d8ce5fd6SBenjamin Herrenschmidt 
2032d8ce5fd6SBenjamin Herrenschmidt     /* Hypervisor virtualization interrupt */
2033f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
2034d8ce5fd6SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
2035d8ce5fd6SBenjamin Herrenschmidt         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
20369de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
2037de76b85cSMatheus Ferst             return PPC_INTERRUPT_HVIRT;
2038d8ce5fd6SBenjamin Herrenschmidt         }
2039d8ce5fd6SBenjamin Herrenschmidt     }
2040d8ce5fd6SBenjamin Herrenschmidt 
2041d8ce5fd6SBenjamin Herrenschmidt     /* External interrupt can ignore MSR:EE under some circumstances */
2042f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
2043d1dbe37cSBenjamin Herrenschmidt         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
20446eebe6dcSBenjamin Herrenschmidt         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
20456eebe6dcSBenjamin Herrenschmidt         /* HEIC blocks delivery to the hypervisor */
20469de754d3SVíctor Colombo         if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
2047d41ccf6eSVíctor Colombo             !FIELD_EX64(env->msr, MSR, PR))) ||
20489de754d3SVíctor Colombo             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
2049de76b85cSMatheus Ferst             return PPC_INTERRUPT_EXT;
2050d1dbe37cSBenjamin Herrenschmidt         }
2051d1dbe37cSBenjamin Herrenschmidt     }
2052acc861c2SVíctor Colombo     if (FIELD_EX64(env->msr, MSR, CE)) {
2053c79c73f6SBlue Swirl         /* External critical interrupt */
2054f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CEXT) {
2055de76b85cSMatheus Ferst             return PPC_INTERRUPT_CEXT;
2056c79c73f6SBlue Swirl         }
2057c79c73f6SBlue Swirl     }
20583621e2c9SBenjamin Herrenschmidt     if (async_deliver != 0) {
2059c79c73f6SBlue Swirl         /* Watchdog timer on embedded PowerPC */
2060f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_WDT) {
2061de76b85cSMatheus Ferst             return PPC_INTERRUPT_WDT;
2062c79c73f6SBlue Swirl         }
2063f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) {
2064de76b85cSMatheus Ferst             return PPC_INTERRUPT_CDOORBELL;
2065c79c73f6SBlue Swirl         }
2066c79c73f6SBlue Swirl         /* Fixed interval timer on embedded PowerPC */
2067f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_FIT) {
2068de76b85cSMatheus Ferst             return PPC_INTERRUPT_FIT;
2069c79c73f6SBlue Swirl         }
2070c79c73f6SBlue Swirl         /* Programmable interval timer on embedded PowerPC */
2071f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PIT) {
2072de76b85cSMatheus Ferst             return PPC_INTERRUPT_PIT;
2073c79c73f6SBlue Swirl         }
2074c79c73f6SBlue Swirl         /* Decrementer exception */
2075f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
2076de76b85cSMatheus Ferst             return PPC_INTERRUPT_DECR;
2077c79c73f6SBlue Swirl         }
2078f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2079de76b85cSMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
2080c79c73f6SBlue Swirl         }
2081f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
2082de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
20837af1e7b0SCédric Le Goater         }
2084f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
2085de76b85cSMatheus Ferst             return PPC_INTERRUPT_PERFM;
2086c79c73f6SBlue Swirl         }
2087c79c73f6SBlue Swirl         /* Thermal interrupt */
2088f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_THERM) {
2089de76b85cSMatheus Ferst             return PPC_INTERRUPT_THERM;
2090c79c73f6SBlue Swirl         }
2091cb76bbc4SDaniel Henrique Barboza         /* EBB exception */
2092f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
2093cb76bbc4SDaniel Henrique Barboza             /*
2094cb76bbc4SDaniel Henrique Barboza              * EBB exception must be taken in problem state and
2095cb76bbc4SDaniel Henrique Barboza              * with BESCR_GE set.
2096cb76bbc4SDaniel Henrique Barboza              */
2097d41ccf6eSVíctor Colombo             if (FIELD_EX64(env->msr, MSR, PR) &&
2098d41ccf6eSVíctor Colombo                 (env->spr[SPR_BESCR] & BESCR_GE)) {
2099de76b85cSMatheus Ferst                 return PPC_INTERRUPT_EBB;
2100de76b85cSMatheus Ferst             }
2101de76b85cSMatheus Ferst         }
2102de76b85cSMatheus Ferst     }
2103cb76bbc4SDaniel Henrique Barboza 
2104de76b85cSMatheus Ferst     return 0;
2105de76b85cSMatheus Ferst }
2106de76b85cSMatheus Ferst 
21072fdedcbcSMatheus Ferst /*
21082fdedcbcSMatheus Ferst  * Sets CPU_INTERRUPT_HARD if there is at least one unmasked interrupt to be
21092fdedcbcSMatheus Ferst  * delivered and clears CPU_INTERRUPT_HARD otherwise.
21102fdedcbcSMatheus Ferst  *
21112fdedcbcSMatheus Ferst  * This method is called by ppc_set_interrupt when an interrupt is raised or
21122fdedcbcSMatheus Ferst  * lowered, and should also be called whenever an interrupt masking condition
21132fdedcbcSMatheus Ferst  * is changed, e.g.:
21142fdedcbcSMatheus Ferst  *  - When relevant bits of MSR are altered, like EE, HV, PR, etc.;
21152fdedcbcSMatheus Ferst  *  - When relevant bits of LPCR are altered, like PECE, HDICE, HVICE, etc.;
21162fdedcbcSMatheus Ferst  *  - When PSSCR[EC] or env->resume_as_sreset are changed;
21172fdedcbcSMatheus Ferst  *  - When cs->halted is changed and the CPU has a different interrupt masking
21182fdedcbcSMatheus Ferst  *    logic in power-saving mode (e.g., POWER7/8/9/10);
21192fdedcbcSMatheus Ferst  */
21202fdedcbcSMatheus Ferst void ppc_maybe_interrupt(CPUPPCState *env)
21212fdedcbcSMatheus Ferst {
21222fdedcbcSMatheus Ferst     CPUState *cs = env_cpu(env);
212332ead8e6SStefan Hajnoczi     BQL_LOCK_GUARD();
21242fdedcbcSMatheus Ferst 
21252fdedcbcSMatheus Ferst     if (ppc_next_unmasked_interrupt(env)) {
21262fdedcbcSMatheus Ferst         cpu_interrupt(cs, CPU_INTERRUPT_HARD);
21272fdedcbcSMatheus Ferst     } else {
21282fdedcbcSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
21292fdedcbcSMatheus Ferst     }
21302fdedcbcSMatheus Ferst }
21312fdedcbcSMatheus Ferst 
2132f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2133d93a4856SMatheus Ferst static void p7_deliver_interrupt(CPUPPCState *env, int interrupt)
2134d93a4856SMatheus Ferst {
2135d93a4856SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2136d93a4856SMatheus Ferst 
2137d93a4856SMatheus Ferst     switch (interrupt) {
2138d93a4856SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2139d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2140d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2141d93a4856SMatheus Ferst         break;
2142d93a4856SMatheus Ferst 
2143d93a4856SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2144d93a4856SMatheus Ferst         /* HDEC clears on delivery */
2145d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2146d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2147d93a4856SMatheus Ferst         break;
2148d93a4856SMatheus Ferst 
2149d93a4856SMatheus Ferst     case PPC_INTERRUPT_EXT:
2150d93a4856SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2151d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2152d93a4856SMatheus Ferst         } else {
2153d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2154d93a4856SMatheus Ferst         }
2155d93a4856SMatheus Ferst         break;
2156d93a4856SMatheus Ferst 
2157d93a4856SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2158d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2159d93a4856SMatheus Ferst         break;
2160d93a4856SMatheus Ferst     case PPC_INTERRUPT_PERFM:
2161d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2162d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2163d93a4856SMatheus Ferst         break;
2164d93a4856SMatheus Ferst     case 0:
2165d93a4856SMatheus Ferst         /*
2166d93a4856SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
2167d93a4856SMatheus Ferst          * anything to deliver while in a PM state that requires getting
2168d93a4856SMatheus Ferst          * out via a 0x100
2169d93a4856SMatheus Ferst          *
2170d93a4856SMatheus Ferst          * This means we will incorrectly execute past the power management
2171d93a4856SMatheus Ferst          * instruction instead of triggering a reset.
2172d93a4856SMatheus Ferst          *
2173d93a4856SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
2174d93a4856SMatheus Ferst          * processor has_work implementation and the logic in this function.
2175d93a4856SMatheus Ferst          */
2176d93a4856SMatheus Ferst         assert(!env->resume_as_sreset);
2177d93a4856SMatheus Ferst         break;
2178d93a4856SMatheus Ferst     default:
2179bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2180bc30c1c6SBALATON Zoltan                   interrupt);
2181d93a4856SMatheus Ferst     }
2182d93a4856SMatheus Ferst }
2183d93a4856SMatheus Ferst 
21846527e757SMatheus Ferst static void p8_deliver_interrupt(CPUPPCState *env, int interrupt)
21856527e757SMatheus Ferst {
21866527e757SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
21876527e757SMatheus Ferst 
21886527e757SMatheus Ferst     switch (interrupt) {
21896527e757SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
21906527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
21916527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
21926527e757SMatheus Ferst         break;
21936527e757SMatheus Ferst 
21946527e757SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
21956527e757SMatheus Ferst         /* HDEC clears on delivery */
21966527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
21976527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
21986527e757SMatheus Ferst         break;
21996527e757SMatheus Ferst 
22006527e757SMatheus Ferst     case PPC_INTERRUPT_EXT:
22016527e757SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
22026527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
22036527e757SMatheus Ferst         } else {
22046527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
22056527e757SMatheus Ferst         }
22066527e757SMatheus Ferst         break;
22076527e757SMatheus Ferst 
22086527e757SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
22096527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
22106527e757SMatheus Ferst         break;
22116527e757SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
22126527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
22136527e757SMatheus Ferst         if (is_book3s_arch2x(env)) {
22146527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
22156527e757SMatheus Ferst         } else {
22166527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
22176527e757SMatheus Ferst         }
22186527e757SMatheus Ferst         break;
22196527e757SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
22206527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
22216527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
22226527e757SMatheus Ferst         break;
22236527e757SMatheus Ferst     case PPC_INTERRUPT_PERFM:
22246527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
22256527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
22266527e757SMatheus Ferst         break;
22276527e757SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
22286527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
22296527e757SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
22306527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
22316527e757SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
22326527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
22336527e757SMatheus Ferst         }
22346527e757SMatheus Ferst         break;
22356527e757SMatheus Ferst     case 0:
22366527e757SMatheus Ferst         /*
22376527e757SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
22386527e757SMatheus Ferst          * anything to deliver while in a PM state that requires getting
22396527e757SMatheus Ferst          * out via a 0x100
22406527e757SMatheus Ferst          *
22416527e757SMatheus Ferst          * This means we will incorrectly execute past the power management
22426527e757SMatheus Ferst          * instruction instead of triggering a reset.
22436527e757SMatheus Ferst          *
22446527e757SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
22456527e757SMatheus Ferst          * processor has_work implementation and the logic in this function.
22466527e757SMatheus Ferst          */
22476527e757SMatheus Ferst         assert(!env->resume_as_sreset);
22486527e757SMatheus Ferst         break;
22496527e757SMatheus Ferst     default:
2250bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2251bc30c1c6SBALATON Zoltan                   interrupt);
22526527e757SMatheus Ferst     }
22536527e757SMatheus Ferst }
22546527e757SMatheus Ferst 
22553654e238SMatheus Ferst static void p9_deliver_interrupt(CPUPPCState *env, int interrupt)
22563654e238SMatheus Ferst {
22573654e238SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
22583654e238SMatheus Ferst     CPUState *cs = env_cpu(env);
22593654e238SMatheus Ferst 
226027796411SMatheus Ferst     if (cs->halted && !(env->spr[SPR_PSSCR] & PSSCR_EC) &&
226127796411SMatheus Ferst         !FIELD_EX64(env->msr, MSR, EE)) {
226227796411SMatheus Ferst         /*
226327796411SMatheus Ferst          * A pending interrupt took us out of power-saving, but MSR[EE] says
226427796411SMatheus Ferst          * that we should return to NIP+4 instead of delivering it.
226527796411SMatheus Ferst          */
226627796411SMatheus Ferst         return;
226727796411SMatheus Ferst     }
226827796411SMatheus Ferst 
22693654e238SMatheus Ferst     switch (interrupt) {
22703654e238SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
22713654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
22723654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
22733654e238SMatheus Ferst         break;
22743654e238SMatheus Ferst 
22753654e238SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
22763654e238SMatheus Ferst         /* HDEC clears on delivery */
22773654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
22783654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
22793654e238SMatheus Ferst         break;
22803654e238SMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
22813654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
22823654e238SMatheus Ferst         break;
22833654e238SMatheus Ferst 
22843654e238SMatheus Ferst     case PPC_INTERRUPT_EXT:
22853654e238SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
22863654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
22873654e238SMatheus Ferst         } else {
22883654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
22893654e238SMatheus Ferst         }
22903654e238SMatheus Ferst         break;
22913654e238SMatheus Ferst 
22923654e238SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
22933654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
22943654e238SMatheus Ferst         break;
22953654e238SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
22963654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
22973654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
22983654e238SMatheus Ferst         break;
22993654e238SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
23003654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
23013654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
23023654e238SMatheus Ferst         break;
23033654e238SMatheus Ferst     case PPC_INTERRUPT_PERFM:
23043654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
23053654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
23063654e238SMatheus Ferst         break;
23073654e238SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
23083654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
23093654e238SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
23103654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
23113654e238SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
23123654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
23133654e238SMatheus Ferst         }
23143654e238SMatheus Ferst         break;
23153654e238SMatheus Ferst     case 0:
23163654e238SMatheus Ferst         /*
23173654e238SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
23183654e238SMatheus Ferst          * anything to deliver while in a PM state that requires getting
23193654e238SMatheus Ferst          * out via a 0x100
23203654e238SMatheus Ferst          *
23213654e238SMatheus Ferst          * This means we will incorrectly execute past the power management
23223654e238SMatheus Ferst          * instruction instead of triggering a reset.
23233654e238SMatheus Ferst          *
23243654e238SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
23253654e238SMatheus Ferst          * processor has_work implementation and the logic in this function.
23263654e238SMatheus Ferst          */
23273654e238SMatheus Ferst         assert(!env->resume_as_sreset);
23283654e238SMatheus Ferst         break;
23293654e238SMatheus Ferst     default:
2330bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2331bc30c1c6SBALATON Zoltan                   interrupt);
23323654e238SMatheus Ferst     }
23333654e238SMatheus Ferst }
2334f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
23353654e238SMatheus Ferst 
2336868cb6baSBALATON Zoltan static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt)
2337de76b85cSMatheus Ferst {
2338868cb6baSBALATON Zoltan #ifdef TARGET_PPC64
2339868cb6baSBALATON Zoltan     switch (env->excp_model) {
2340868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER7:
2341868cb6baSBALATON Zoltan         return p7_deliver_interrupt(env, interrupt);
2342868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER8:
2343868cb6baSBALATON Zoltan         return p8_deliver_interrupt(env, interrupt);
2344868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER9:
2345868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER10:
2346868cb6baSBALATON Zoltan         return p9_deliver_interrupt(env, interrupt);
2347868cb6baSBALATON Zoltan     default:
2348868cb6baSBALATON Zoltan         break;
2349868cb6baSBALATON Zoltan     }
2350de76b85cSMatheus Ferst #endif
2351de76b85cSMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2352de76b85cSMatheus Ferst 
2353de76b85cSMatheus Ferst     switch (interrupt) {
2354de76b85cSMatheus Ferst     case PPC_INTERRUPT_RESET: /* External reset */
2355de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_RESET;
2356de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_RESET);
2357de76b85cSMatheus Ferst         break;
2358de76b85cSMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2359de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2360de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2361de76b85cSMatheus Ferst         break;
2362de76b85cSMatheus Ferst 
2363de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2364de76b85cSMatheus Ferst         /* HDEC clears on delivery */
2365de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2366de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2367de76b85cSMatheus Ferst         break;
2368de76b85cSMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
2369de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2370de76b85cSMatheus Ferst         break;
2371de76b85cSMatheus Ferst 
2372de76b85cSMatheus Ferst     case PPC_INTERRUPT_EXT:
2373de76b85cSMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2374de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2375de76b85cSMatheus Ferst         } else {
2376de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2377de76b85cSMatheus Ferst         }
2378de76b85cSMatheus Ferst         break;
2379de76b85cSMatheus Ferst     case PPC_INTERRUPT_CEXT: /* External critical interrupt */
2380de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_CRITICAL);
2381de76b85cSMatheus Ferst         break;
2382de76b85cSMatheus Ferst 
2383de76b85cSMatheus Ferst     case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */
2384de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_WDT;
2385de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_WDT);
2386de76b85cSMatheus Ferst         break;
2387de76b85cSMatheus Ferst     case PPC_INTERRUPT_CDOORBELL:
2388de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL;
2389de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DOORCI);
2390de76b85cSMatheus Ferst         break;
2391de76b85cSMatheus Ferst     case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */
2392de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_FIT;
2393de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_FIT);
2394de76b85cSMatheus Ferst         break;
2395de76b85cSMatheus Ferst     case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */
2396de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PIT;
2397de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PIT);
2398de76b85cSMatheus Ferst         break;
2399de76b85cSMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2400de76b85cSMatheus Ferst         if (ppc_decr_clear_on_delivery(env)) {
2401de76b85cSMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DECR;
2402de76b85cSMatheus Ferst         }
2403de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2404de76b85cSMatheus Ferst         break;
2405de76b85cSMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
2406de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
2407de76b85cSMatheus Ferst         if (is_book3s_arch2x(env)) {
2408de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
2409de76b85cSMatheus Ferst         } else {
2410de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
2411de76b85cSMatheus Ferst         }
2412de76b85cSMatheus Ferst         break;
2413de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
2414de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
2415de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
2416de76b85cSMatheus Ferst         break;
2417de76b85cSMatheus Ferst     case PPC_INTERRUPT_PERFM:
2418de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2419de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2420de76b85cSMatheus Ferst         break;
2421de76b85cSMatheus Ferst     case PPC_INTERRUPT_THERM:  /* Thermal interrupt */
2422de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_THERM;
2423de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_THERM);
2424de76b85cSMatheus Ferst         break;
2425de76b85cSMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
2426de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
2427cb76bbc4SDaniel Henrique Barboza         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
2428cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
2429cb76bbc4SDaniel Henrique Barboza         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
2430cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
2431cb76bbc4SDaniel Henrique Barboza         }
2432de76b85cSMatheus Ferst         break;
2433de76b85cSMatheus Ferst     case 0:
2434f8154fd2SBenjamin Herrenschmidt         /*
2435f8154fd2SBenjamin Herrenschmidt          * This is a bug ! It means that has_work took us out of halt without
2436f8154fd2SBenjamin Herrenschmidt          * anything to deliver while in a PM state that requires getting
2437f8154fd2SBenjamin Herrenschmidt          * out via a 0x100
2438f8154fd2SBenjamin Herrenschmidt          *
2439f8154fd2SBenjamin Herrenschmidt          * This means we will incorrectly execute past the power management
2440f8154fd2SBenjamin Herrenschmidt          * instruction instead of triggering a reset.
2441f8154fd2SBenjamin Herrenschmidt          *
2442136fbf65Szhaolichang          * It generally means a discrepancy between the wakeup conditions in the
2443f8154fd2SBenjamin Herrenschmidt          * processor has_work implementation and the logic in this function.
2444f8154fd2SBenjamin Herrenschmidt          */
2445de76b85cSMatheus Ferst         assert(!env->resume_as_sreset);
2446de76b85cSMatheus Ferst         break;
2447de76b85cSMatheus Ferst     default:
2448bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2449bc30c1c6SBALATON Zoltan                   interrupt);
2450ba2898f7SMatheus Ferst     }
2451ba2898f7SMatheus Ferst }
2452ba2898f7SMatheus Ferst 
2453b5b7f391SNicholas Piggin void ppc_cpu_do_system_reset(CPUState *cs)
245434316482SAlexey Kardashevskiy {
245534316482SAlexey Kardashevskiy     PowerPCCPU *cpu = POWERPC_CPU(cs);
245634316482SAlexey Kardashevskiy 
245793130c84SFabiano Rosas     powerpc_excp(cpu, POWERPC_EXCP_RESET);
245834316482SAlexey Kardashevskiy }
2459ad77c6caSNicholas Piggin 
2460ad77c6caSNicholas Piggin void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector)
2461ad77c6caSNicholas Piggin {
2462ad77c6caSNicholas Piggin     PowerPCCPU *cpu = POWERPC_CPU(cs);
2463ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
2464ad77c6caSNicholas Piggin     target_ulong msr = 0;
2465ad77c6caSNicholas Piggin 
2466ad77c6caSNicholas Piggin     /*
2467ad77c6caSNicholas Piggin      * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already
2468ad77c6caSNicholas Piggin      * been set by KVM.
2469ad77c6caSNicholas Piggin      */
2470ad77c6caSNicholas Piggin     msr = (1ULL << MSR_ME);
2471ad77c6caSNicholas Piggin     msr |= env->msr & (1ULL << MSR_SF);
2472516fc103SFabiano Rosas     if (ppc_interrupts_little_endian(cpu, false)) {
2473ad77c6caSNicholas Piggin         msr |= (1ULL << MSR_LE);
2474ad77c6caSNicholas Piggin     }
2475ad77c6caSNicholas Piggin 
24767cebc5dbSNicholas Piggin     /* Anything for nested required here? MSR[HV] bit? */
24777cebc5dbSNicholas Piggin 
2478ad77c6caSNicholas Piggin     powerpc_set_excp_state(cpu, vector, msr);
2479ad77c6caSNicholas Piggin }
2480c79c73f6SBlue Swirl 
2481458dd766SRichard Henderson bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
2482458dd766SRichard Henderson {
2483794511bcSPhilippe Mathieu-Daudé     CPUPPCState *env = cpu_env(cs);
2484de76b85cSMatheus Ferst     int interrupt;
2485458dd766SRichard Henderson 
2486de76b85cSMatheus Ferst     if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) {
2487de76b85cSMatheus Ferst         return false;
2488de76b85cSMatheus Ferst     }
2489de76b85cSMatheus Ferst 
2490de76b85cSMatheus Ferst     interrupt = ppc_next_unmasked_interrupt(env);
2491de76b85cSMatheus Ferst     if (interrupt == 0) {
2492de76b85cSMatheus Ferst         return false;
2493de76b85cSMatheus Ferst     }
2494de76b85cSMatheus Ferst 
2495de76b85cSMatheus Ferst     ppc_deliver_interrupt(env, interrupt);
2496458dd766SRichard Henderson     if (env->pending_interrupts == 0) {
2497de76b85cSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
2498458dd766SRichard Henderson     }
2499458dd766SRichard Henderson     return true;
2500458dd766SRichard Henderson }
2501458dd766SRichard Henderson 
2502f725245cSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
2503f725245cSPhilippe Mathieu-Daudé 
2504ad71ed68SBlue Swirl /*****************************************************************************/
2505ad71ed68SBlue Swirl /* Exceptions processing helpers */
2506ad71ed68SBlue Swirl 
2507db789c6cSBenjamin Herrenschmidt void raise_exception_err_ra(CPUPPCState *env, uint32_t exception,
2508db789c6cSBenjamin Herrenschmidt                             uint32_t error_code, uintptr_t raddr)
2509ad71ed68SBlue Swirl {
2510db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
251127103424SAndreas Färber 
251227103424SAndreas Färber     cs->exception_index = exception;
2513ad71ed68SBlue Swirl     env->error_code = error_code;
2514db789c6cSBenjamin Herrenschmidt     cpu_loop_exit_restore(cs, raddr);
2515db789c6cSBenjamin Herrenschmidt }
2516db789c6cSBenjamin Herrenschmidt 
2517db789c6cSBenjamin Herrenschmidt void raise_exception_err(CPUPPCState *env, uint32_t exception,
2518db789c6cSBenjamin Herrenschmidt                          uint32_t error_code)
2519db789c6cSBenjamin Herrenschmidt {
2520db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2521db789c6cSBenjamin Herrenschmidt }
2522db789c6cSBenjamin Herrenschmidt 
2523db789c6cSBenjamin Herrenschmidt void raise_exception(CPUPPCState *env, uint32_t exception)
2524db789c6cSBenjamin Herrenschmidt {
2525db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2526db789c6cSBenjamin Herrenschmidt }
2527db789c6cSBenjamin Herrenschmidt 
2528db789c6cSBenjamin Herrenschmidt void raise_exception_ra(CPUPPCState *env, uint32_t exception,
2529db789c6cSBenjamin Herrenschmidt                         uintptr_t raddr)
2530db789c6cSBenjamin Herrenschmidt {
2531db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, raddr);
2532db789c6cSBenjamin Herrenschmidt }
2533db789c6cSBenjamin Herrenschmidt 
25342b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2535db789c6cSBenjamin Herrenschmidt void helper_raise_exception_err(CPUPPCState *env, uint32_t exception,
2536db789c6cSBenjamin Herrenschmidt                                 uint32_t error_code)
2537db789c6cSBenjamin Herrenschmidt {
2538db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2539ad71ed68SBlue Swirl }
2540ad71ed68SBlue Swirl 
2541e5f17ac6SBlue Swirl void helper_raise_exception(CPUPPCState *env, uint32_t exception)
2542ad71ed68SBlue Swirl {
2543db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2544ad71ed68SBlue Swirl }
2545ad71ed68SBlue Swirl 
2546f6c2d68bSBALATON Zoltan #ifndef CONFIG_USER_ONLY
2547e5f17ac6SBlue Swirl void helper_store_msr(CPUPPCState *env, target_ulong val)
2548ad71ed68SBlue Swirl {
2549db789c6cSBenjamin Herrenschmidt     uint32_t excp = hreg_store_msr(env, val, 0);
2550259186a7SAndreas Färber 
2551db789c6cSBenjamin Herrenschmidt     if (excp != 0) {
25520661329aSBALATON Zoltan         cpu_interrupt_exittb(env_cpu(env));
2553db789c6cSBenjamin Herrenschmidt         raise_exception(env, excp);
2554ad71ed68SBlue Swirl     }
2555ad71ed68SBlue Swirl }
2556ad71ed68SBlue Swirl 
25572fdedcbcSMatheus Ferst void helper_ppc_maybe_interrupt(CPUPPCState *env)
25582fdedcbcSMatheus Ferst {
25592fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
25602fdedcbcSMatheus Ferst }
25612fdedcbcSMatheus Ferst 
2562f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2563f43520e5SRichard Henderson void helper_scv(CPUPPCState *env, uint32_t lev)
2564f43520e5SRichard Henderson {
2565f43520e5SRichard Henderson     if (env->spr[SPR_FSCR] & (1ull << FSCR_SCV)) {
2566f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_SYSCALL_VECTORED, lev);
2567f43520e5SRichard Henderson     } else {
2568f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_FU, FSCR_IC_SCV);
2569f43520e5SRichard Henderson     }
2570f43520e5SRichard Henderson }
2571f43520e5SRichard Henderson 
257207e4804fSCédric Le Goater void helper_pminsn(CPUPPCState *env, uint32_t insn)
25737778a575SBenjamin Herrenschmidt {
25744e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
25757778a575SBenjamin Herrenschmidt 
25767778a575SBenjamin Herrenschmidt     cs->halted = 1;
25777778a575SBenjamin Herrenschmidt 
25783621e2c9SBenjamin Herrenschmidt     /* Condition for waking up at 0x100 */
25791e7fd61dSBenjamin Herrenschmidt     env->resume_as_sreset = (insn != PPC_PM_STOP) ||
258021c0d66aSBenjamin Herrenschmidt         (env->spr[SPR_PSSCR] & PSSCR_EC);
25812fdedcbcSMatheus Ferst 
25829915dac4SNicholas Piggin     /* HDECR is not to wake from PM state, it may have already fired */
25839915dac4SNicholas Piggin     if (env->resume_as_sreset) {
25849915dac4SNicholas Piggin         PowerPCCPU *cpu = env_archcpu(env);
25859915dac4SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 0);
25869915dac4SNicholas Piggin     }
25879915dac4SNicholas Piggin 
25882fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
25897778a575SBenjamin Herrenschmidt }
2590f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
25917778a575SBenjamin Herrenschmidt 
259262e79ef9SCédric Le Goater static void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr)
2593ad71ed68SBlue Swirl {
2594a2e71b28SBenjamin Herrenschmidt     /* MSR:POW cannot be set by any form of rfi */
2595a2e71b28SBenjamin Herrenschmidt     msr &= ~(1ULL << MSR_POW);
2596a2e71b28SBenjamin Herrenschmidt 
25975aad0457SChristophe Leroy     /* MSR:TGPR cannot be set by any form of rfi */
25985aad0457SChristophe Leroy     if (env->flags & POWERPC_FLAG_TGPR)
25995aad0457SChristophe Leroy         msr &= ~(1ULL << MSR_TGPR);
26005aad0457SChristophe Leroy 
2601f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2602a2e71b28SBenjamin Herrenschmidt     /* Switching to 32-bit ? Crop the nip */
2603a2e71b28SBenjamin Herrenschmidt     if (!msr_is_64bit(env, msr)) {
2604ad71ed68SBlue Swirl         nip = (uint32_t)nip;
2605ad71ed68SBlue Swirl     }
2606ad71ed68SBlue Swirl #else
2607ad71ed68SBlue Swirl     nip = (uint32_t)nip;
2608ad71ed68SBlue Swirl #endif
2609ad71ed68SBlue Swirl     /* XXX: beware: this is false if VLE is supported */
2610ad71ed68SBlue Swirl     env->nip = nip & ~((target_ulong)0x00000003);
2611ad71ed68SBlue Swirl     hreg_store_msr(env, msr, 1);
26122eb1ef73SCédric Le Goater     trace_ppc_excp_rfi(env->nip, env->msr);
261347733729SDavid Gibson     /*
261447733729SDavid Gibson      * No need to raise an exception here, as rfi is always the last
261547733729SDavid Gibson      * insn of a TB
2616ad71ed68SBlue Swirl      */
26170661329aSBALATON Zoltan     cpu_interrupt_exittb(env_cpu(env));
2618a8b73734SNikunj A Dadhania     /* Reset the reservation */
2619a8b73734SNikunj A Dadhania     env->reserve_addr = -1;
2620a8b73734SNikunj A Dadhania 
2621cd0c6f47SBenjamin Herrenschmidt     /* Context synchronizing: check if TCG TLB needs flush */
2622e3cffe6fSNikunj A Dadhania     check_tlb_flush(env, false);
2623ad71ed68SBlue Swirl }
2624ad71ed68SBlue Swirl 
2625e5f17ac6SBlue Swirl void helper_rfi(CPUPPCState *env)
2626ad71ed68SBlue Swirl {
2627a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful);
2628a1bb7384SScott Wood }
2629ad71ed68SBlue Swirl 
2630f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2631e5f17ac6SBlue Swirl void helper_rfid(CPUPPCState *env)
2632ad71ed68SBlue Swirl {
263347733729SDavid Gibson     /*
2634136fbf65Szhaolichang      * The architecture defines a number of rules for which bits can
263547733729SDavid Gibson      * change but in practice, we handle this in hreg_store_msr()
2636a2e71b28SBenjamin Herrenschmidt      * which will be called by do_rfi(), so there is no need to filter
2637a2e71b28SBenjamin Herrenschmidt      * here
2638a2e71b28SBenjamin Herrenschmidt      */
2639a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]);
2640ad71ed68SBlue Swirl }
2641ad71ed68SBlue Swirl 
26423c89b8d6SNicholas Piggin void helper_rfscv(CPUPPCState *env)
26433c89b8d6SNicholas Piggin {
26443c89b8d6SNicholas Piggin     do_rfi(env, env->lr, env->ctr);
26453c89b8d6SNicholas Piggin }
26463c89b8d6SNicholas Piggin 
2647e5f17ac6SBlue Swirl void helper_hrfid(CPUPPCState *env)
2648ad71ed68SBlue Swirl {
2649a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
2650ad71ed68SBlue Swirl }
2651ad71ed68SBlue Swirl 
26521f26c751SDaniel Henrique Barboza void helper_rfebb(CPUPPCState *env, target_ulong s)
26531f26c751SDaniel Henrique Barboza {
26541f26c751SDaniel Henrique Barboza     target_ulong msr = env->msr;
26551f26c751SDaniel Henrique Barboza 
26561f26c751SDaniel Henrique Barboza     /*
26571f26c751SDaniel Henrique Barboza      * Handling of BESCR bits 32:33 according to PowerISA v3.1:
26581f26c751SDaniel Henrique Barboza      *
26591f26c751SDaniel Henrique Barboza      * "If BESCR 32:33 != 0b00 the instruction is treated as if
26601f26c751SDaniel Henrique Barboza      *  the instruction form were invalid."
26611f26c751SDaniel Henrique Barboza      */
26621f26c751SDaniel Henrique Barboza     if (env->spr[SPR_BESCR] & BESCR_INVALID) {
26631f26c751SDaniel Henrique Barboza         raise_exception_err(env, POWERPC_EXCP_PROGRAM,
26641f26c751SDaniel Henrique Barboza                             POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
26651f26c751SDaniel Henrique Barboza     }
26661f26c751SDaniel Henrique Barboza 
26671f26c751SDaniel Henrique Barboza     env->nip = env->spr[SPR_EBBRR];
26681f26c751SDaniel Henrique Barboza 
26691f26c751SDaniel Henrique Barboza     /* Switching to 32-bit ? Crop the nip */
26701f26c751SDaniel Henrique Barboza     if (!msr_is_64bit(env, msr)) {
26711f26c751SDaniel Henrique Barboza         env->nip = (uint32_t)env->spr[SPR_EBBRR];
26721f26c751SDaniel Henrique Barboza     }
26731f26c751SDaniel Henrique Barboza 
26741f26c751SDaniel Henrique Barboza     if (s) {
26751f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_GE;
26761f26c751SDaniel Henrique Barboza     } else {
26771f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
26781f26c751SDaniel Henrique Barboza     }
26791f26c751SDaniel Henrique Barboza }
2680d3412df2SDaniel Henrique Barboza 
2681d3412df2SDaniel Henrique Barboza /*
2682d3412df2SDaniel Henrique Barboza  * Triggers or queues an 'ebb_excp' EBB exception. All checks
2683d3412df2SDaniel Henrique Barboza  * but FSCR, HFSCR and msr_pr must be done beforehand.
2684d3412df2SDaniel Henrique Barboza  *
2685d3412df2SDaniel Henrique Barboza  * PowerISA v3.1 isn't clear about whether an EBB should be
2686d3412df2SDaniel Henrique Barboza  * postponed or cancelled if the EBB facility is unavailable.
2687d3412df2SDaniel Henrique Barboza  * Our assumption here is that the EBB is cancelled if both
2688d3412df2SDaniel Henrique Barboza  * FSCR and HFSCR EBB facilities aren't available.
2689d3412df2SDaniel Henrique Barboza  */
2690d3412df2SDaniel Henrique Barboza static void do_ebb(CPUPPCState *env, int ebb_excp)
2691d3412df2SDaniel Henrique Barboza {
2692d3412df2SDaniel Henrique Barboza     PowerPCCPU *cpu = env_archcpu(env);
2693d3412df2SDaniel Henrique Barboza 
2694d3412df2SDaniel Henrique Barboza     /*
2695d3412df2SDaniel Henrique Barboza      * FSCR_EBB and FSCR_IC_EBB are the same bits used with
2696d3412df2SDaniel Henrique Barboza      * HFSCR.
2697d3412df2SDaniel Henrique Barboza      */
2698d3412df2SDaniel Henrique Barboza     helper_fscr_facility_check(env, FSCR_EBB, 0, FSCR_IC_EBB);
2699d3412df2SDaniel Henrique Barboza     helper_hfscr_facility_check(env, FSCR_EBB, "EBB", FSCR_IC_EBB);
2700d3412df2SDaniel Henrique Barboza 
2701d3412df2SDaniel Henrique Barboza     if (ebb_excp == POWERPC_EXCP_PERFM_EBB) {
2702d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_PMEO;
2703d3412df2SDaniel Henrique Barboza     } else if (ebb_excp == POWERPC_EXCP_EXTERNAL_EBB) {
2704d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_EEO;
2705d3412df2SDaniel Henrique Barboza     }
2706d3412df2SDaniel Henrique Barboza 
2707d41ccf6eSVíctor Colombo     if (FIELD_EX64(env->msr, MSR, PR)) {
2708d3412df2SDaniel Henrique Barboza         powerpc_excp(cpu, ebb_excp);
2709d3412df2SDaniel Henrique Barboza     } else {
27107b694df6SMatheus Ferst         ppc_set_irq(cpu, PPC_INTERRUPT_EBB, 1);
2711d3412df2SDaniel Henrique Barboza     }
2712d3412df2SDaniel Henrique Barboza }
2713d3412df2SDaniel Henrique Barboza 
2714d3412df2SDaniel Henrique Barboza void raise_ebb_perfm_exception(CPUPPCState *env)
2715d3412df2SDaniel Henrique Barboza {
2716d3412df2SDaniel Henrique Barboza     bool perfm_ebb_enabled = env->spr[SPR_POWER_MMCR0] & MMCR0_EBE &&
2717d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_PME &&
2718d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_GE;
2719d3412df2SDaniel Henrique Barboza 
2720d3412df2SDaniel Henrique Barboza     if (!perfm_ebb_enabled) {
2721d3412df2SDaniel Henrique Barboza         return;
2722d3412df2SDaniel Henrique Barboza     }
2723d3412df2SDaniel Henrique Barboza 
2724d3412df2SDaniel Henrique Barboza     do_ebb(env, POWERPC_EXCP_PERFM_EBB);
2725d3412df2SDaniel Henrique Barboza }
2726754920c7SBALATON Zoltan #endif /* TARGET_PPC64 */
27271f26c751SDaniel Henrique Barboza 
2728ad71ed68SBlue Swirl /*****************************************************************************/
2729ad71ed68SBlue Swirl /* Embedded PowerPC specific helpers */
2730e5f17ac6SBlue Swirl void helper_40x_rfci(CPUPPCState *env)
2731ad71ed68SBlue Swirl {
2732a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]);
2733ad71ed68SBlue Swirl }
2734ad71ed68SBlue Swirl 
2735e5f17ac6SBlue Swirl void helper_rfci(CPUPPCState *env)
2736ad71ed68SBlue Swirl {
2737a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]);
2738ad71ed68SBlue Swirl }
2739ad71ed68SBlue Swirl 
2740e5f17ac6SBlue Swirl void helper_rfdi(CPUPPCState *env)
2741ad71ed68SBlue Swirl {
2742a1bb7384SScott Wood     /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
2743a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]);
2744ad71ed68SBlue Swirl }
2745ad71ed68SBlue Swirl 
2746e5f17ac6SBlue Swirl void helper_rfmci(CPUPPCState *env)
2747ad71ed68SBlue Swirl {
2748a1bb7384SScott Wood     /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
2749a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
2750ad71ed68SBlue Swirl }
27515ca958cfSBALATON Zoltan #endif /* !CONFIG_USER_ONLY */
2752ad71ed68SBlue Swirl 
2753e5f17ac6SBlue Swirl void helper_tw(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2754e5f17ac6SBlue Swirl                uint32_t flags)
2755ad71ed68SBlue Swirl {
2756ad71ed68SBlue Swirl     if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
2757ad71ed68SBlue Swirl                   ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
2758ad71ed68SBlue Swirl                   ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
2759ad71ed68SBlue Swirl                   ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
2760ad71ed68SBlue Swirl                   ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
276172073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
276272073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2763ad71ed68SBlue Swirl     }
2764ad71ed68SBlue Swirl }
2765ad71ed68SBlue Swirl 
2766f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2767e5f17ac6SBlue Swirl void helper_td(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2768e5f17ac6SBlue Swirl                uint32_t flags)
2769ad71ed68SBlue Swirl {
2770ad71ed68SBlue Swirl     if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
2771ad71ed68SBlue Swirl                   ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
2772ad71ed68SBlue Swirl                   ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
2773ad71ed68SBlue Swirl                   ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
2774ad71ed68SBlue Swirl                   ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) {
277572073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
277672073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2777ad71ed68SBlue Swirl     }
2778ad71ed68SBlue Swirl }
2779f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
2780ad71ed68SBlue Swirl 
2781670f1da3SVíctor Colombo static uint32_t helper_SIMON_LIKE_32_64(uint32_t x, uint64_t key, uint32_t lane)
2782670f1da3SVíctor Colombo {
2783670f1da3SVíctor Colombo     const uint16_t c = 0xfffc;
2784670f1da3SVíctor Colombo     const uint64_t z0 = 0xfa2561cdf44ac398ULL;
2785670f1da3SVíctor Colombo     uint16_t z = 0, temp;
2786670f1da3SVíctor Colombo     uint16_t k[32], eff_k[32], xleft[33], xright[33], fxleft[32];
2787670f1da3SVíctor Colombo 
2788670f1da3SVíctor Colombo     for (int i = 3; i >= 0; i--) {
2789670f1da3SVíctor Colombo         k[i] = key & 0xffff;
2790670f1da3SVíctor Colombo         key >>= 16;
2791670f1da3SVíctor Colombo     }
2792670f1da3SVíctor Colombo     xleft[0] = x & 0xffff;
2793670f1da3SVíctor Colombo     xright[0] = (x >> 16) & 0xffff;
2794670f1da3SVíctor Colombo 
2795670f1da3SVíctor Colombo     for (int i = 0; i < 28; i++) {
2796670f1da3SVíctor Colombo         z = (z0 >> (63 - i)) & 1;
2797670f1da3SVíctor Colombo         temp = ror16(k[i + 3], 3) ^ k[i + 1];
2798670f1da3SVíctor Colombo         k[i + 4] = c ^ z ^ k[i] ^ temp ^ ror16(temp, 1);
2799670f1da3SVíctor Colombo     }
2800670f1da3SVíctor Colombo 
2801670f1da3SVíctor Colombo     for (int i = 0; i < 8; i++) {
2802670f1da3SVíctor Colombo         eff_k[4 * i + 0] = k[4 * i + ((0 + lane) % 4)];
2803670f1da3SVíctor Colombo         eff_k[4 * i + 1] = k[4 * i + ((1 + lane) % 4)];
2804670f1da3SVíctor Colombo         eff_k[4 * i + 2] = k[4 * i + ((2 + lane) % 4)];
2805670f1da3SVíctor Colombo         eff_k[4 * i + 3] = k[4 * i + ((3 + lane) % 4)];
2806670f1da3SVíctor Colombo     }
2807670f1da3SVíctor Colombo 
2808670f1da3SVíctor Colombo     for (int i = 0; i < 32; i++) {
2809670f1da3SVíctor Colombo         fxleft[i] = (rol16(xleft[i], 1) &
2810670f1da3SVíctor Colombo             rol16(xleft[i], 8)) ^ rol16(xleft[i], 2);
2811670f1da3SVíctor Colombo         xleft[i + 1] = xright[i] ^ fxleft[i] ^ eff_k[i];
2812670f1da3SVíctor Colombo         xright[i + 1] = xleft[i];
2813670f1da3SVíctor Colombo     }
2814670f1da3SVíctor Colombo 
2815670f1da3SVíctor Colombo     return (((uint32_t)xright[32]) << 16) | xleft[32];
2816670f1da3SVíctor Colombo }
2817670f1da3SVíctor Colombo 
2818670f1da3SVíctor Colombo static uint64_t hash_digest(uint64_t ra, uint64_t rb, uint64_t key)
2819670f1da3SVíctor Colombo {
2820670f1da3SVíctor Colombo     uint64_t stage0_h = 0ULL, stage0_l = 0ULL;
2821670f1da3SVíctor Colombo     uint64_t stage1_h, stage1_l;
2822670f1da3SVíctor Colombo 
2823670f1da3SVíctor Colombo     for (int i = 0; i < 4; i++) {
2824670f1da3SVíctor Colombo         stage0_h |= ror64(rb & 0xff, 8 * (2 * i + 1));
2825670f1da3SVíctor Colombo         stage0_h |= ((ra >> 32) & 0xff) << (8 * 2 * i);
2826670f1da3SVíctor Colombo         stage0_l |= ror64((rb >> 32) & 0xff, 8 * (2 * i + 1));
2827670f1da3SVíctor Colombo         stage0_l |= (ra & 0xff) << (8 * 2 * i);
2828670f1da3SVíctor Colombo         rb >>= 8;
2829670f1da3SVíctor Colombo         ra >>= 8;
2830670f1da3SVíctor Colombo     }
2831670f1da3SVíctor Colombo 
2832670f1da3SVíctor Colombo     stage1_h = (uint64_t)helper_SIMON_LIKE_32_64(stage0_h >> 32, key, 0) << 32;
2833670f1da3SVíctor Colombo     stage1_h |= helper_SIMON_LIKE_32_64(stage0_h, key, 1);
2834670f1da3SVíctor Colombo     stage1_l = (uint64_t)helper_SIMON_LIKE_32_64(stage0_l >> 32, key, 2) << 32;
2835670f1da3SVíctor Colombo     stage1_l |= helper_SIMON_LIKE_32_64(stage0_l, key, 3);
2836670f1da3SVíctor Colombo 
2837670f1da3SVíctor Colombo     return stage1_h ^ stage1_l;
2838670f1da3SVíctor Colombo }
2839670f1da3SVíctor Colombo 
28404091fabfSNicholas Miehlbradt static void do_hash(CPUPPCState *env, target_ulong ea, target_ulong ra,
28414091fabfSNicholas Miehlbradt                     target_ulong rb, uint64_t key, bool store)
28424091fabfSNicholas Miehlbradt {
28434091fabfSNicholas Miehlbradt     uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash;
28444091fabfSNicholas Miehlbradt 
28454091fabfSNicholas Miehlbradt     if (store) {
28464091fabfSNicholas Miehlbradt         cpu_stq_data_ra(env, ea, calculated_hash, GETPC());
28474091fabfSNicholas Miehlbradt     } else {
28484091fabfSNicholas Miehlbradt         loaded_hash = cpu_ldq_data_ra(env, ea, GETPC());
28494091fabfSNicholas Miehlbradt         if (loaded_hash != calculated_hash) {
28504091fabfSNicholas Miehlbradt             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
28514091fabfSNicholas Miehlbradt                 POWERPC_EXCP_TRAP, GETPC());
28524091fabfSNicholas Miehlbradt         }
28534091fabfSNicholas Miehlbradt     }
28544091fabfSNicholas Miehlbradt }
28554091fabfSNicholas Miehlbradt 
2856670f1da3SVíctor Colombo #include "qemu/guest-random.h"
2857670f1da3SVíctor Colombo 
28584091fabfSNicholas Miehlbradt #ifdef TARGET_PPC64
28594091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect)                             \
2860670f1da3SVíctor Colombo void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
2861670f1da3SVíctor Colombo                  target_ulong rb)                                             \
2862670f1da3SVíctor Colombo {                                                                             \
28634091fabfSNicholas Miehlbradt     if (env->msr & R_MSR_PR_MASK) {                                           \
28644091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_DEXCR] & R_DEXCR_PRO_##dexcr_aspect##_MASK ||      \
28654091fabfSNicholas Miehlbradt             env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK))       \
28664091fabfSNicholas Miehlbradt             return;                                                           \
28674091fabfSNicholas Miehlbradt     } else if (!(env->msr & R_MSR_HV_MASK)) {                                 \
28684091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_DEXCR] & R_DEXCR_PNH_##dexcr_aspect##_MASK ||      \
28694091fabfSNicholas Miehlbradt             env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK))       \
28704091fabfSNicholas Miehlbradt             return;                                                           \
28714091fabfSNicholas Miehlbradt     } else if (!(env->msr & R_MSR_S_MASK)) {                                  \
28724091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_HDEXCR] & R_HDEXCR_HNU_##dexcr_aspect##_MASK))     \
28734091fabfSNicholas Miehlbradt             return;                                                           \
28744091fabfSNicholas Miehlbradt     }                                                                         \
2875670f1da3SVíctor Colombo                                                                               \
28764091fabfSNicholas Miehlbradt     do_hash(env, ea, ra, rb, key, store);                                     \
2877670f1da3SVíctor Colombo }
28784091fabfSNicholas Miehlbradt #else
28794091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect)                             \
28804091fabfSNicholas Miehlbradt void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
28814091fabfSNicholas Miehlbradt                  target_ulong rb)                                             \
28824091fabfSNicholas Miehlbradt {                                                                             \
28834091fabfSNicholas Miehlbradt     do_hash(env, ea, ra, rb, key, store);                                     \
28844091fabfSNicholas Miehlbradt }
28854091fabfSNicholas Miehlbradt #endif /* TARGET_PPC64 */
2886670f1da3SVíctor Colombo 
28874091fabfSNicholas Miehlbradt HELPER_HASH(HASHST, env->spr[SPR_HASHKEYR], true, NPHIE)
28884091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHK, env->spr[SPR_HASHKEYR], false, NPHIE)
28894091fabfSNicholas Miehlbradt HELPER_HASH(HASHSTP, env->spr[SPR_HASHPKEYR], true, PHIE)
28904091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHKP, env->spr[SPR_HASHPKEYR], false, PHIE)
2891670f1da3SVíctor Colombo 
2892f6c2d68bSBALATON Zoltan #ifndef CONFIG_USER_ONLY
2893ad71ed68SBlue Swirl /* Embedded.Processor Control */
2894ad71ed68SBlue Swirl static int dbell2irq(target_ulong rb)
2895ad71ed68SBlue Swirl {
2896ad71ed68SBlue Swirl     int msg = rb & DBELL_TYPE_MASK;
2897ad71ed68SBlue Swirl     int irq = -1;
2898ad71ed68SBlue Swirl 
2899ad71ed68SBlue Swirl     switch (msg) {
2900ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL:
2901ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_DOORBELL;
2902ad71ed68SBlue Swirl         break;
2903ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL_CRIT:
2904ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_CDOORBELL;
2905ad71ed68SBlue Swirl         break;
2906ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL:
2907ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_CRIT:
2908ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_MC:
2909ad71ed68SBlue Swirl         /* XXX implement */
2910ad71ed68SBlue Swirl     default:
2911ad71ed68SBlue Swirl         break;
2912ad71ed68SBlue Swirl     }
2913ad71ed68SBlue Swirl 
2914ad71ed68SBlue Swirl     return irq;
2915ad71ed68SBlue Swirl }
2916ad71ed68SBlue Swirl 
2917e5f17ac6SBlue Swirl void helper_msgclr(CPUPPCState *env, target_ulong rb)
2918ad71ed68SBlue Swirl {
2919ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2920ad71ed68SBlue Swirl 
2921ad71ed68SBlue Swirl     if (irq < 0) {
2922ad71ed68SBlue Swirl         return;
2923ad71ed68SBlue Swirl     }
2924ad71ed68SBlue Swirl 
29257b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), irq, 0);
2926ad71ed68SBlue Swirl }
2927ad71ed68SBlue Swirl 
2928ad71ed68SBlue Swirl void helper_msgsnd(target_ulong rb)
2929ad71ed68SBlue Swirl {
2930ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2931ad71ed68SBlue Swirl     int pir = rb & DBELL_PIRTAG_MASK;
2932182735efSAndreas Färber     CPUState *cs;
2933ad71ed68SBlue Swirl 
2934ad71ed68SBlue Swirl     if (irq < 0) {
2935ad71ed68SBlue Swirl         return;
2936ad71ed68SBlue Swirl     }
2937ad71ed68SBlue Swirl 
2938195801d7SStefan Hajnoczi     bql_lock();
2939bdc44640SAndreas Färber     CPU_FOREACH(cs) {
2940182735efSAndreas Färber         PowerPCCPU *cpu = POWERPC_CPU(cs);
2941182735efSAndreas Färber         CPUPPCState *cenv = &cpu->env;
2942182735efSAndreas Färber 
2943ad71ed68SBlue Swirl         if ((rb & DBELL_BRDCAST) || (cenv->spr[SPR_BOOKE_PIR] == pir)) {
29447b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
2945ad71ed68SBlue Swirl         }
2946ad71ed68SBlue Swirl     }
2947195801d7SStefan Hajnoczi     bql_unlock();
2948ad71ed68SBlue Swirl }
29497af1e7b0SCédric Le Goater 
29507af1e7b0SCédric Le Goater /* Server Processor Control */
29517af1e7b0SCédric Le Goater 
29525ba7ba1dSCédric Le Goater static bool dbell_type_server(target_ulong rb)
29535ba7ba1dSCédric Le Goater {
295447733729SDavid Gibson     /*
295547733729SDavid Gibson      * A Directed Hypervisor Doorbell message is sent only if the
29567af1e7b0SCédric Le Goater      * message type is 5. All other types are reserved and the
295747733729SDavid Gibson      * instruction is a no-op
295847733729SDavid Gibson      */
29595ba7ba1dSCédric Le Goater     return (rb & DBELL_TYPE_MASK) == DBELL_TYPE_DBELL_SERVER;
29607af1e7b0SCédric Le Goater }
29617af1e7b0SCédric Le Goater 
29627af1e7b0SCédric Le Goater void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb)
29637af1e7b0SCédric Le Goater {
29645ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
29657af1e7b0SCédric Le Goater         return;
29667af1e7b0SCédric Le Goater     }
29677af1e7b0SCédric Le Goater 
29687b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
29697af1e7b0SCédric Le Goater }
29707af1e7b0SCédric Le Goater 
29715ba7ba1dSCédric Le Goater static void book3s_msgsnd_common(int pir, int irq)
29727af1e7b0SCédric Le Goater {
29737af1e7b0SCédric Le Goater     CPUState *cs;
29747af1e7b0SCédric Le Goater 
2975195801d7SStefan Hajnoczi     bql_lock();
29767af1e7b0SCédric Le Goater     CPU_FOREACH(cs) {
29777af1e7b0SCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
29787af1e7b0SCédric Le Goater         CPUPPCState *cenv = &cpu->env;
29797af1e7b0SCédric Le Goater 
29807af1e7b0SCédric Le Goater         /* TODO: broadcast message to all threads of the same  processor */
29817af1e7b0SCédric Le Goater         if (cenv->spr_cb[SPR_PIR].default_value == pir) {
29827b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
29837af1e7b0SCédric Le Goater         }
29847af1e7b0SCédric Le Goater     }
2985195801d7SStefan Hajnoczi     bql_unlock();
29867af1e7b0SCédric Le Goater }
29875ba7ba1dSCédric Le Goater 
29885ba7ba1dSCédric Le Goater void helper_book3s_msgsnd(target_ulong rb)
29895ba7ba1dSCédric Le Goater {
29905ba7ba1dSCédric Le Goater     int pir = rb & DBELL_PROCIDTAG_MASK;
29915ba7ba1dSCédric Le Goater 
29925ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
29935ba7ba1dSCédric Le Goater         return;
29945ba7ba1dSCédric Le Goater     }
29955ba7ba1dSCédric Le Goater 
29965ba7ba1dSCédric Le Goater     book3s_msgsnd_common(pir, PPC_INTERRUPT_HDOORBELL);
29975ba7ba1dSCédric Le Goater }
29985ba7ba1dSCédric Le Goater 
2999f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
30005ba7ba1dSCédric Le Goater void helper_book3s_msgclrp(CPUPPCState *env, target_ulong rb)
30015ba7ba1dSCédric Le Goater {
3002493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgclrp", HFSCR_IC_MSGP);
3003493028d8SCédric Le Goater 
30045ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
30055ba7ba1dSCédric Le Goater         return;
30065ba7ba1dSCédric Le Goater     }
30075ba7ba1dSCédric Le Goater 
30082e985555SNicholas Piggin     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_DOORBELL, 0);
30095ba7ba1dSCédric Le Goater }
30105ba7ba1dSCédric Le Goater 
30115ba7ba1dSCédric Le Goater /*
3012d24e80b2SNicholas Piggin  * sends a message to another thread  on the same
30135ba7ba1dSCédric Le Goater  * multi-threaded processor
30145ba7ba1dSCédric Le Goater  */
30155ba7ba1dSCédric Le Goater void helper_book3s_msgsndp(CPUPPCState *env, target_ulong rb)
30165ba7ba1dSCédric Le Goater {
3017d24e80b2SNicholas Piggin     CPUState *cs = env_cpu(env);
3018b0a13333SPhilippe Mathieu-Daudé     PowerPCCPU *cpu = env_archcpu(env);
3019d24e80b2SNicholas Piggin     CPUState *ccs;
3020d24e80b2SNicholas Piggin     uint32_t nr_threads = cs->nr_threads;
3021d24e80b2SNicholas Piggin     int ttir = rb & PPC_BITMASK(57, 63);
30225ba7ba1dSCédric Le Goater 
3023493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgsndp", HFSCR_IC_MSGP);
3024493028d8SCédric Le Goater 
30253401ea3cSNicholas Piggin     if (!(env->flags & POWERPC_FLAG_SMT_1LPAR)) {
30263401ea3cSNicholas Piggin         nr_threads = 1; /* msgsndp behaves as 1-thread in LPAR-per-thread mode*/
30273401ea3cSNicholas Piggin     }
30283401ea3cSNicholas Piggin 
3029d24e80b2SNicholas Piggin     if (!dbell_type_server(rb) || ttir >= nr_threads) {
30305ba7ba1dSCédric Le Goater         return;
30315ba7ba1dSCédric Le Goater     }
30325ba7ba1dSCédric Le Goater 
3033d24e80b2SNicholas Piggin     if (nr_threads == 1) {
3034d24e80b2SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, 1);
3035d24e80b2SNicholas Piggin         return;
3036d24e80b2SNicholas Piggin     }
30375ba7ba1dSCédric Le Goater 
3038d24e80b2SNicholas Piggin     /* Does iothread need to be locked for walking CPU list? */
3039195801d7SStefan Hajnoczi     bql_lock();
3040d24e80b2SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
3041d24e80b2SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
3042d24e80b2SNicholas Piggin         uint32_t thread_id = ppc_cpu_tir(ccpu);
3043d24e80b2SNicholas Piggin 
3044d24e80b2SNicholas Piggin         if (ttir == thread_id) {
3045d24e80b2SNicholas Piggin             ppc_set_irq(ccpu, PPC_INTERRUPT_DOORBELL, 1);
3046195801d7SStefan Hajnoczi             bql_unlock();
3047d24e80b2SNicholas Piggin             return;
3048d24e80b2SNicholas Piggin         }
3049d24e80b2SNicholas Piggin     }
3050d24e80b2SNicholas Piggin 
3051d24e80b2SNicholas Piggin     g_assert_not_reached();
30525ba7ba1dSCédric Le Goater }
3053996473e4SRichard Henderson #endif /* TARGET_PPC64 */
30540f3110faSRichard Henderson 
305514895384SNicholas Piggin /* Single-step tracing */
305614895384SNicholas Piggin void helper_book3s_trace(CPUPPCState *env, target_ulong prev_ip)
305714895384SNicholas Piggin {
305814895384SNicholas Piggin     uint32_t error_code = 0;
305914895384SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
306014895384SNicholas Piggin         /* Load/store reporting, SRR1[35, 36] and SDAR, are not implemented. */
306114895384SNicholas Piggin         env->spr[SPR_POWER_SIAR] = prev_ip;
306214895384SNicholas Piggin         error_code = PPC_BIT(33);
306314895384SNicholas Piggin     }
306414895384SNicholas Piggin     raise_exception_err(env, POWERPC_EXCP_TRACE, error_code);
306514895384SNicholas Piggin }
306614895384SNicholas Piggin 
30670f3110faSRichard Henderson void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
30680f3110faSRichard Henderson                                  MMUAccessType access_type,
30690f3110faSRichard Henderson                                  int mmu_idx, uintptr_t retaddr)
30700f3110faSRichard Henderson {
3071b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
307229c4a336SFabiano Rosas     uint32_t insn;
307329c4a336SFabiano Rosas 
307429c4a336SFabiano Rosas     /* Restore state and reload the insn we executed, for filling in DSISR.  */
30753d419a4dSRichard Henderson     cpu_restore_state(cs, retaddr);
3076888050cfSNicholas Piggin     insn = ppc_ldl_code(env, env->nip);
30770f3110faSRichard Henderson 
3078a7e3af13SRichard Henderson     switch (env->mmu_model) {
3079a7e3af13SRichard Henderson     case POWERPC_MMU_SOFT_4xx:
3080a7e3af13SRichard Henderson         env->spr[SPR_40x_DEAR] = vaddr;
3081a7e3af13SRichard Henderson         break;
3082a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE:
3083a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE206:
3084a7e3af13SRichard Henderson         env->spr[SPR_BOOKE_DEAR] = vaddr;
3085a7e3af13SRichard Henderson         break;
3086a7e3af13SRichard Henderson     default:
3087a7e3af13SRichard Henderson         env->spr[SPR_DAR] = vaddr;
3088a7e3af13SRichard Henderson         break;
3089a7e3af13SRichard Henderson     }
3090a7e3af13SRichard Henderson 
30910f3110faSRichard Henderson     cs->exception_index = POWERPC_EXCP_ALIGN;
309229c4a336SFabiano Rosas     env->error_code = insn & 0x03FF0000;
309329c4a336SFabiano Rosas     cpu_loop_exit(cs);
30940f3110faSRichard Henderson }
309555a7fa34SNicholas Piggin 
309655a7fa34SNicholas Piggin void ppc_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
309755a7fa34SNicholas Piggin                                    vaddr vaddr, unsigned size,
309855a7fa34SNicholas Piggin                                    MMUAccessType access_type,
309955a7fa34SNicholas Piggin                                    int mmu_idx, MemTxAttrs attrs,
310055a7fa34SNicholas Piggin                                    MemTxResult response, uintptr_t retaddr)
310155a7fa34SNicholas Piggin {
3102b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
310355a7fa34SNicholas Piggin 
310455a7fa34SNicholas Piggin     switch (env->excp_model) {
310555a7fa34SNicholas Piggin #if defined(TARGET_PPC64)
3106c8fd9667SNicholas Piggin     case POWERPC_EXCP_POWER8:
310755a7fa34SNicholas Piggin     case POWERPC_EXCP_POWER9:
310855a7fa34SNicholas Piggin     case POWERPC_EXCP_POWER10:
310955a7fa34SNicholas Piggin         /*
311055a7fa34SNicholas Piggin          * Machine check codes can be found in processor User Manual or
311155a7fa34SNicholas Piggin          * Linux or skiboot source.
311255a7fa34SNicholas Piggin          */
311355a7fa34SNicholas Piggin         if (access_type == MMU_DATA_LOAD) {
311455a7fa34SNicholas Piggin             env->spr[SPR_DAR] = vaddr;
311555a7fa34SNicholas Piggin             env->spr[SPR_DSISR] = PPC_BIT(57);
311655a7fa34SNicholas Piggin             env->error_code = PPC_BIT(42);
311755a7fa34SNicholas Piggin 
311855a7fa34SNicholas Piggin         } else if (access_type == MMU_DATA_STORE) {
311955a7fa34SNicholas Piggin             /*
312055a7fa34SNicholas Piggin              * MCE for stores in POWER is asynchronous so hardware does
312155a7fa34SNicholas Piggin              * not set DAR, but QEMU can do better.
312255a7fa34SNicholas Piggin              */
312355a7fa34SNicholas Piggin             env->spr[SPR_DAR] = vaddr;
312455a7fa34SNicholas Piggin             env->error_code = PPC_BIT(36) | PPC_BIT(43) | PPC_BIT(45);
312555a7fa34SNicholas Piggin             env->error_code |= PPC_BIT(42);
312655a7fa34SNicholas Piggin 
312755a7fa34SNicholas Piggin         } else { /* Fetch */
3128c8fd9667SNicholas Piggin             /*
3129c8fd9667SNicholas Piggin              * is_prefix_insn_excp() tests !PPC_BIT(42) to avoid fetching
3130c8fd9667SNicholas Piggin              * the instruction, so that must always be clear for fetches.
3131c8fd9667SNicholas Piggin              */
313255a7fa34SNicholas Piggin             env->error_code = PPC_BIT(36) | PPC_BIT(44) | PPC_BIT(45);
313355a7fa34SNicholas Piggin         }
313455a7fa34SNicholas Piggin         break;
313555a7fa34SNicholas Piggin #endif
313655a7fa34SNicholas Piggin     default:
313755a7fa34SNicholas Piggin         /*
313855a7fa34SNicholas Piggin          * TODO: Check behaviour for other CPUs, for now do nothing.
313955a7fa34SNicholas Piggin          * Could add a basic MCE even if real hardware ignores.
314055a7fa34SNicholas Piggin          */
314155a7fa34SNicholas Piggin         return;
314255a7fa34SNicholas Piggin     }
314355a7fa34SNicholas Piggin 
314455a7fa34SNicholas Piggin     cs->exception_index = POWERPC_EXCP_MCHECK;
314555a7fa34SNicholas Piggin     cpu_loop_exit_restore(cs, retaddr);
314655a7fa34SNicholas Piggin }
314714192307SNicholas Piggin 
314814192307SNicholas Piggin void ppc_cpu_debug_excp_handler(CPUState *cs)
314914192307SNicholas Piggin {
315014192307SNicholas Piggin #if defined(TARGET_PPC64)
3151b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
315214192307SNicholas Piggin 
315314192307SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
3154d5ee641cSNicholas Piggin         if (cs->watchpoint_hit) {
3155d5ee641cSNicholas Piggin             if (cs->watchpoint_hit->flags & BP_CPU) {
3156d5ee641cSNicholas Piggin                 env->spr[SPR_DAR] = cs->watchpoint_hit->hitaddr;
3157d5ee641cSNicholas Piggin                 env->spr[SPR_DSISR] = PPC_BIT(41);
3158d5ee641cSNicholas Piggin                 cs->watchpoint_hit = NULL;
3159d5ee641cSNicholas Piggin                 raise_exception(env, POWERPC_EXCP_DSI);
3160d5ee641cSNicholas Piggin             }
3161d5ee641cSNicholas Piggin             cs->watchpoint_hit = NULL;
3162d5ee641cSNicholas Piggin         } else if (cpu_breakpoint_test(cs, env->nip, BP_CPU)) {
316314192307SNicholas Piggin             raise_exception_err(env, POWERPC_EXCP_TRACE,
316414192307SNicholas Piggin                                 PPC_BIT(33) | PPC_BIT(43));
316514192307SNicholas Piggin         }
316614192307SNicholas Piggin     }
316714192307SNicholas Piggin #endif
316814192307SNicholas Piggin }
316914192307SNicholas Piggin 
317014192307SNicholas Piggin bool ppc_cpu_debug_check_breakpoint(CPUState *cs)
317114192307SNicholas Piggin {
317214192307SNicholas Piggin #if defined(TARGET_PPC64)
3173b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
317414192307SNicholas Piggin 
317514192307SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
317614192307SNicholas Piggin         target_ulong priv;
317714192307SNicholas Piggin 
317814192307SNicholas Piggin         priv = env->spr[SPR_CIABR] & PPC_BITMASK(62, 63);
317914192307SNicholas Piggin         switch (priv) {
318014192307SNicholas Piggin         case 0x1: /* problem */
318114192307SNicholas Piggin             return env->msr & ((target_ulong)1 << MSR_PR);
318214192307SNicholas Piggin         case 0x2: /* supervisor */
318314192307SNicholas Piggin             return (!(env->msr & ((target_ulong)1 << MSR_PR)) &&
318414192307SNicholas Piggin                     !(env->msr & ((target_ulong)1 << MSR_HV)));
318514192307SNicholas Piggin         case 0x3: /* hypervisor */
318614192307SNicholas Piggin             return (!(env->msr & ((target_ulong)1 << MSR_PR)) &&
318714192307SNicholas Piggin                      (env->msr & ((target_ulong)1 << MSR_HV)));
318814192307SNicholas Piggin         default:
318914192307SNicholas Piggin             g_assert_not_reached();
319014192307SNicholas Piggin         }
319114192307SNicholas Piggin     }
319214192307SNicholas Piggin #endif
319314192307SNicholas Piggin 
319414192307SNicholas Piggin     return false;
319514192307SNicholas Piggin }
319614192307SNicholas Piggin 
3197d5ee641cSNicholas Piggin bool ppc_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
3198d5ee641cSNicholas Piggin {
3199d5ee641cSNicholas Piggin #if defined(TARGET_PPC64)
3200b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
3201d5ee641cSNicholas Piggin 
3202d5ee641cSNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
3203d5ee641cSNicholas Piggin         if (wp == env->dawr0_watchpoint) {
3204d5ee641cSNicholas Piggin             uint32_t dawrx = env->spr[SPR_DAWRX0];
3205d5ee641cSNicholas Piggin             bool wt = extract32(dawrx, PPC_BIT_NR(59), 1);
3206d5ee641cSNicholas Piggin             bool wti = extract32(dawrx, PPC_BIT_NR(60), 1);
3207d5ee641cSNicholas Piggin             bool hv = extract32(dawrx, PPC_BIT_NR(61), 1);
3208d5ee641cSNicholas Piggin             bool sv = extract32(dawrx, PPC_BIT_NR(62), 1);
3209d5ee641cSNicholas Piggin             bool pr = extract32(dawrx, PPC_BIT_NR(62), 1);
3210d5ee641cSNicholas Piggin 
3211d5ee641cSNicholas Piggin             if ((env->msr & ((target_ulong)1 << MSR_PR)) && !pr) {
3212d5ee641cSNicholas Piggin                 return false;
3213d5ee641cSNicholas Piggin             } else if ((env->msr & ((target_ulong)1 << MSR_HV)) && !hv) {
3214d5ee641cSNicholas Piggin                 return false;
3215d5ee641cSNicholas Piggin             } else if (!sv) {
3216d5ee641cSNicholas Piggin                 return false;
3217d5ee641cSNicholas Piggin             }
3218d5ee641cSNicholas Piggin 
3219d5ee641cSNicholas Piggin             if (!wti) {
3220d5ee641cSNicholas Piggin                 if (env->msr & ((target_ulong)1 << MSR_DR)) {
3221d5ee641cSNicholas Piggin                     if (!wt) {
3222d5ee641cSNicholas Piggin                         return false;
3223d5ee641cSNicholas Piggin                     }
3224d5ee641cSNicholas Piggin                 } else {
3225d5ee641cSNicholas Piggin                     if (wt) {
3226d5ee641cSNicholas Piggin                         return false;
3227d5ee641cSNicholas Piggin                     }
3228d5ee641cSNicholas Piggin                 }
3229d5ee641cSNicholas Piggin             }
3230d5ee641cSNicholas Piggin 
3231d5ee641cSNicholas Piggin             return true;
3232d5ee641cSNicholas Piggin         }
3233d5ee641cSNicholas Piggin     }
3234d5ee641cSNicholas Piggin #endif
3235d5ee641cSNicholas Piggin 
3236d5ee641cSNicholas Piggin     return false;
3237d5ee641cSNicholas Piggin }
3238d5ee641cSNicholas Piggin 
3239996473e4SRichard Henderson #endif /* !CONFIG_USER_ONLY */
32405ca958cfSBALATON Zoltan #endif /* CONFIG_TCG */
3241