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