xref: /qemu/target/ppc/excp_helper.c (revision bc30c1c6632c33eb9a27680b2de673d8b22a65b3)
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     CPUPPCState *env = &cpu->env;
449e808c2edSFabiano Rosas     target_ulong msr, new_msr, vector;
4508428cdb2SFabiano Rosas     int srr0, srr1;
451e808c2edSFabiano Rosas 
452e808c2edSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
453e808c2edSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
454e808c2edSFabiano Rosas 
455e808c2edSFabiano Rosas     /*
456495fc7ffSFabiano Rosas      * new interrupt handler msr preserves existing ME unless
457e6a19a64SMichael Tokarev      * explicitly overridden.
458e808c2edSFabiano Rosas      */
459495fc7ffSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME));
460e808c2edSFabiano Rosas 
461e808c2edSFabiano Rosas     /* target registers */
462e808c2edSFabiano Rosas     srr0 = SPR_SRR0;
463e808c2edSFabiano Rosas     srr1 = SPR_SRR1;
464e808c2edSFabiano Rosas 
465e808c2edSFabiano Rosas     /*
466e808c2edSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
467495fc7ffSFabiano Rosas      * arch 2.05 server or later.
468e808c2edSFabiano Rosas      */
469495fc7ffSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
470e808c2edSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
471e808c2edSFabiano Rosas     }
472e808c2edSFabiano Rosas 
473e808c2edSFabiano Rosas     vector = env->excp_vectors[excp];
474e808c2edSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
475*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
476*bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
477e808c2edSFabiano Rosas     }
478e808c2edSFabiano Rosas 
479e808c2edSFabiano Rosas     vector |= env->excp_prefix;
480e808c2edSFabiano Rosas 
481e808c2edSFabiano Rosas     switch (excp) {
482e808c2edSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
483e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
484e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
485e808c2edSFabiano Rosas         break;
486e808c2edSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
487819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
488e808c2edSFabiano Rosas         /* machine check exceptions don't have ME set */
489e808c2edSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
490e808c2edSFabiano Rosas 
491e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
492e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
493e808c2edSFabiano Rosas         break;
494e808c2edSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
495f9911e1eSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_40x_ESR], env->spr[SPR_40x_DEAR]);
496e808c2edSFabiano Rosas         break;
497e808c2edSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
498e808c2edSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
499e808c2edSFabiano Rosas         break;
500e808c2edSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
501e808c2edSFabiano Rosas         break;
502e808c2edSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
503e808c2edSFabiano Rosas         break;
504e808c2edSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
505e808c2edSFabiano Rosas         switch (env->error_code & ~0xF) {
506e808c2edSFabiano Rosas         case POWERPC_EXCP_FP:
507da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
508e808c2edSFabiano Rosas                 trace_ppc_excp_fp_ignore();
5093680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
510e808c2edSFabiano Rosas                 return;
511e808c2edSFabiano Rosas             }
51264e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_FP;
513e808c2edSFabiano Rosas             break;
514e808c2edSFabiano Rosas         case POWERPC_EXCP_INVAL:
515e808c2edSFabiano Rosas             trace_ppc_excp_inval(env->nip);
51664e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PIL;
517e808c2edSFabiano Rosas             break;
518e808c2edSFabiano Rosas         case POWERPC_EXCP_PRIV:
51964e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PPR;
520e808c2edSFabiano Rosas             break;
521e808c2edSFabiano Rosas         case POWERPC_EXCP_TRAP:
52264e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PTR;
523e808c2edSFabiano Rosas             break;
524e808c2edSFabiano Rosas         default:
525*bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
526e808c2edSFabiano Rosas                       env->error_code);
527e808c2edSFabiano Rosas             break;
528e808c2edSFabiano Rosas         }
529e808c2edSFabiano Rosas         break;
530e808c2edSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
531e808c2edSFabiano Rosas         dump_syscall(env);
532e808c2edSFabiano Rosas 
533e808c2edSFabiano Rosas         /*
534e808c2edSFabiano Rosas          * We need to correct the NIP which in this case is supposed
535e808c2edSFabiano Rosas          * to point to the next instruction
536e808c2edSFabiano Rosas          */
537e808c2edSFabiano Rosas         env->nip += 4;
538e808c2edSFabiano Rosas         break;
539e808c2edSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
540e808c2edSFabiano Rosas         trace_ppc_excp_print("FIT");
541e808c2edSFabiano Rosas         break;
542e808c2edSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
543e808c2edSFabiano Rosas         trace_ppc_excp_print("WDT");
544e808c2edSFabiano Rosas         break;
545e808c2edSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
546e808c2edSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
547e808c2edSFabiano Rosas         break;
548e808c2edSFabiano Rosas     case POWERPC_EXCP_PIT:       /* Programmable interval timer interrupt    */
549e808c2edSFabiano Rosas         trace_ppc_excp_print("PIT");
550e808c2edSFabiano Rosas         break;
5514d8ac1d1SFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
552*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
5534d8ac1d1SFabiano Rosas                   powerpc_excp_name(excp));
5544d8ac1d1SFabiano Rosas         break;
555e808c2edSFabiano Rosas     default:
556*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
557*bc30c1c6SBALATON Zoltan                   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     CPUPPCState *env = &cpu->env;
57358d178fbSFabiano Rosas     target_ulong msr, new_msr, vector;
57458d178fbSFabiano Rosas 
57558d178fbSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
57658d178fbSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
57758d178fbSFabiano Rosas 
57858d178fbSFabiano Rosas     /*
579082d783bSFabiano Rosas      * new interrupt handler msr preserves existing ME unless
580e6a19a64SMichael Tokarev      * explicitly overridden
58158d178fbSFabiano Rosas      */
582082d783bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
58358d178fbSFabiano Rosas 
58458d178fbSFabiano Rosas     /*
58558d178fbSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
586082d783bSFabiano Rosas      * arch 2.05 server or later.
58758d178fbSFabiano Rosas      */
588082d783bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
58958d178fbSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
59058d178fbSFabiano Rosas     }
59158d178fbSFabiano Rosas 
59258d178fbSFabiano Rosas     vector = env->excp_vectors[excp];
59358d178fbSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
594*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
595*bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
59658d178fbSFabiano Rosas     }
59758d178fbSFabiano Rosas 
59858d178fbSFabiano Rosas     vector |= env->excp_prefix;
59958d178fbSFabiano Rosas 
60058d178fbSFabiano Rosas     switch (excp) {
60158d178fbSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
60258d178fbSFabiano Rosas         break;
60358d178fbSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
604819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
60558d178fbSFabiano Rosas         /* machine check exceptions don't have ME set */
60658d178fbSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
60758d178fbSFabiano Rosas 
60858d178fbSFabiano Rosas         break;
60958d178fbSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
61058d178fbSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
61158d178fbSFabiano Rosas         break;
61258d178fbSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
61358d178fbSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
61458d178fbSFabiano Rosas         msr |= env->error_code;
61558d178fbSFabiano Rosas         break;
61658d178fbSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
61758d178fbSFabiano Rosas         break;
61858d178fbSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
61958d178fbSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
62058d178fbSFabiano Rosas         /*
62158d178fbSFabiano Rosas          * Note: the opcode fields will not be set properly for a
62258d178fbSFabiano Rosas          * direct store load/store, but nobody cares as nobody
62358d178fbSFabiano Rosas          * actually uses direct store segments.
62458d178fbSFabiano Rosas          */
62558d178fbSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
62658d178fbSFabiano Rosas         break;
62758d178fbSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
62858d178fbSFabiano Rosas         switch (env->error_code & ~0xF) {
62958d178fbSFabiano Rosas         case POWERPC_EXCP_FP:
630da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
63158d178fbSFabiano Rosas                 trace_ppc_excp_fp_ignore();
6323680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
63358d178fbSFabiano Rosas                 return;
63458d178fbSFabiano Rosas             }
63558d178fbSFabiano Rosas 
63658d178fbSFabiano Rosas             /*
63758d178fbSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
63858d178fbSFabiano Rosas              * instruction, so always use store_next and claim we are
63958d178fbSFabiano Rosas              * precise in the MSR.
64058d178fbSFabiano Rosas              */
64158d178fbSFabiano Rosas             msr |= 0x00100000;
64258d178fbSFabiano Rosas             break;
64358d178fbSFabiano Rosas         case POWERPC_EXCP_INVAL:
64458d178fbSFabiano Rosas             trace_ppc_excp_inval(env->nip);
64558d178fbSFabiano Rosas             msr |= 0x00080000;
64658d178fbSFabiano Rosas             break;
64758d178fbSFabiano Rosas         case POWERPC_EXCP_PRIV:
64858d178fbSFabiano Rosas             msr |= 0x00040000;
64958d178fbSFabiano Rosas             break;
65058d178fbSFabiano Rosas         case POWERPC_EXCP_TRAP:
65158d178fbSFabiano Rosas             msr |= 0x00020000;
65258d178fbSFabiano Rosas             break;
65358d178fbSFabiano Rosas         default:
65458d178fbSFabiano Rosas             /* Should never occur */
655*bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
65658d178fbSFabiano Rosas                       env->error_code);
65758d178fbSFabiano Rosas             break;
65858d178fbSFabiano Rosas         }
65958d178fbSFabiano Rosas         break;
66058d178fbSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
66158d178fbSFabiano Rosas         dump_syscall(env);
66258d178fbSFabiano Rosas 
66358d178fbSFabiano Rosas         /*
66458d178fbSFabiano Rosas          * We need to correct the NIP which in this case is supposed
66558d178fbSFabiano Rosas          * to point to the next instruction
66658d178fbSFabiano Rosas          */
66758d178fbSFabiano Rosas         env->nip += 4;
66858d178fbSFabiano Rosas         break;
66958d178fbSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
67058d178fbSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
67158d178fbSFabiano Rosas         break;
67258d178fbSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
67358d178fbSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
67458d178fbSFabiano Rosas         break;
67558d178fbSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
6768e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
677*bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
678*bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset exception "
679*bc30c1c6SBALATON Zoltan                       "%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         */
706*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
70758d178fbSFabiano Rosas                   powerpc_excp_name(excp));
70858d178fbSFabiano Rosas         break;
70958d178fbSFabiano Rosas     default:
710*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
711*bc30c1c6SBALATON Zoltan                   excp);
71258d178fbSFabiano Rosas         break;
71358d178fbSFabiano Rosas     }
71458d178fbSFabiano Rosas 
71558d178fbSFabiano Rosas     /*
71658d178fbSFabiano Rosas      * Sort out endianness of interrupt, this differs depending on the
71758d178fbSFabiano Rosas      * CPU, the HV mode, etc...
71858d178fbSFabiano Rosas      */
71958d178fbSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
72058d178fbSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
72158d178fbSFabiano Rosas     }
72258d178fbSFabiano Rosas 
72358d178fbSFabiano Rosas     /* Save PC */
724c50eaed1SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
72558d178fbSFabiano Rosas 
72658d178fbSFabiano Rosas     /* Save MSR */
727c50eaed1SFabiano Rosas     env->spr[SPR_SRR1] = msr;
72858d178fbSFabiano Rosas 
72958d178fbSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
73058d178fbSFabiano Rosas }
73158d178fbSFabiano Rosas 
732ccfca2fcSFabiano Rosas static void powerpc_excp_7xx(PowerPCCPU *cpu, int excp)
733ccfca2fcSFabiano Rosas {
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
742e6a19a64SMichael Tokarev      * explicitly overridden
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) {
756*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
757*bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", 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 */
815*bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "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);
8462c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
847ccfca2fcSFabiano Rosas             return;
848ccfca2fcSFabiano Rosas         }
8493b578635SFabiano Rosas 
850ccfca2fcSFabiano Rosas         break;
8513b578635SFabiano Rosas     }
852ccfca2fcSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
853ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
854ccfca2fcSFabiano Rosas         break;
855ccfca2fcSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
8568e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
857*bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
858*bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset exception "
859*bc30c1c6SBALATON Zoltan                       "%d with no HV support\n", excp);
860ccfca2fcSFabiano Rosas         }
861ccfca2fcSFabiano Rosas         break;
862ccfca2fcSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
863ccfca2fcSFabiano Rosas         break;
864ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
865ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
866ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
867ccfca2fcSFabiano Rosas         ppc_excp_debug_sw_tlb(env, excp);
868ccfca2fcSFabiano Rosas 
869ccfca2fcSFabiano Rosas         msr |= env->crf[0] << 28;
870ccfca2fcSFabiano Rosas         msr |= env->error_code; /* key, D/I, S/L bits */
871ccfca2fcSFabiano Rosas         /* Set way using a LRU mechanism */
872ccfca2fcSFabiano Rosas         msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
8737df40c54SFabiano Rosas 
874ccfca2fcSFabiano Rosas         break;
875ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
876ccfca2fcSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
877ccfca2fcSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
878ccfca2fcSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
879*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
880ccfca2fcSFabiano Rosas                   powerpc_excp_name(excp));
881ccfca2fcSFabiano Rosas         break;
882ccfca2fcSFabiano Rosas     default:
883*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
884*bc30c1c6SBALATON Zoltan                   excp);
885ccfca2fcSFabiano Rosas         break;
886ccfca2fcSFabiano Rosas     }
887ccfca2fcSFabiano Rosas 
888ccfca2fcSFabiano Rosas     /*
889ccfca2fcSFabiano Rosas      * Sort out endianness of interrupt, this differs depending on the
890ccfca2fcSFabiano Rosas      * CPU, the HV mode, etc...
891ccfca2fcSFabiano Rosas      */
892ccfca2fcSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
893ccfca2fcSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
894ccfca2fcSFabiano Rosas     }
895ccfca2fcSFabiano Rosas 
896ccfca2fcSFabiano Rosas     /* Save PC */
897fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
898ccfca2fcSFabiano Rosas 
899ccfca2fcSFabiano Rosas     /* Save MSR */
900fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR1] = msr;
901ccfca2fcSFabiano Rosas 
902ccfca2fcSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
903ccfca2fcSFabiano Rosas }
904ccfca2fcSFabiano Rosas 
90552926b0dSFabiano Rosas static void powerpc_excp_74xx(PowerPCCPU *cpu, int excp)
90652926b0dSFabiano Rosas {
90752926b0dSFabiano Rosas     CPUPPCState *env = &cpu->env;
90852926b0dSFabiano Rosas     target_ulong msr, new_msr, vector;
90952926b0dSFabiano Rosas 
91052926b0dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
91152926b0dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
91252926b0dSFabiano Rosas 
91352926b0dSFabiano Rosas     /*
9141f6faf8bSFabiano Rosas      * new interrupt handler msr preserves existing ME unless
915e6a19a64SMichael Tokarev      * explicitly overridden
91652926b0dSFabiano Rosas      */
9171f6faf8bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
91852926b0dSFabiano Rosas 
91952926b0dSFabiano Rosas     /*
92052926b0dSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
9211f6faf8bSFabiano Rosas      * arch 2.05 server or later.
92252926b0dSFabiano Rosas      */
9231f6faf8bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
92452926b0dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
92552926b0dSFabiano Rosas     }
92652926b0dSFabiano Rosas 
92752926b0dSFabiano Rosas     vector = env->excp_vectors[excp];
92852926b0dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
929*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
930*bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
93152926b0dSFabiano Rosas     }
93252926b0dSFabiano Rosas 
93352926b0dSFabiano Rosas     vector |= env->excp_prefix;
93452926b0dSFabiano Rosas 
93552926b0dSFabiano Rosas     switch (excp) {
93652926b0dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
937819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
93852926b0dSFabiano Rosas         /* machine check exceptions don't have ME set */
93952926b0dSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
94052926b0dSFabiano Rosas 
94152926b0dSFabiano Rosas         break;
94252926b0dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
94352926b0dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
94452926b0dSFabiano Rosas         break;
94552926b0dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
94652926b0dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
94752926b0dSFabiano Rosas         msr |= env->error_code;
94852926b0dSFabiano Rosas         break;
94952926b0dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
95052926b0dSFabiano Rosas         break;
95152926b0dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
95252926b0dSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
95352926b0dSFabiano Rosas         /*
95452926b0dSFabiano Rosas          * Note: the opcode fields will not be set properly for a
95552926b0dSFabiano Rosas          * direct store load/store, but nobody cares as nobody
95652926b0dSFabiano Rosas          * actually uses direct store segments.
95752926b0dSFabiano Rosas          */
95852926b0dSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
95952926b0dSFabiano Rosas         break;
96052926b0dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
96152926b0dSFabiano Rosas         switch (env->error_code & ~0xF) {
96252926b0dSFabiano Rosas         case POWERPC_EXCP_FP:
963da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
96452926b0dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
9653680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
96652926b0dSFabiano Rosas                 return;
96752926b0dSFabiano Rosas             }
96852926b0dSFabiano Rosas 
96952926b0dSFabiano Rosas             /*
97052926b0dSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
97152926b0dSFabiano Rosas              * instruction, so always use store_next and claim we are
97252926b0dSFabiano Rosas              * precise in the MSR.
97352926b0dSFabiano Rosas              */
97452926b0dSFabiano Rosas             msr |= 0x00100000;
97552926b0dSFabiano Rosas             break;
97652926b0dSFabiano Rosas         case POWERPC_EXCP_INVAL:
97752926b0dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
97852926b0dSFabiano Rosas             msr |= 0x00080000;
97952926b0dSFabiano Rosas             break;
98052926b0dSFabiano Rosas         case POWERPC_EXCP_PRIV:
98152926b0dSFabiano Rosas             msr |= 0x00040000;
98252926b0dSFabiano Rosas             break;
98352926b0dSFabiano Rosas         case POWERPC_EXCP_TRAP:
98452926b0dSFabiano Rosas             msr |= 0x00020000;
98552926b0dSFabiano Rosas             break;
98652926b0dSFabiano Rosas         default:
98752926b0dSFabiano Rosas             /* Should never occur */
988*bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
98952926b0dSFabiano Rosas                       env->error_code);
99052926b0dSFabiano Rosas             break;
99152926b0dSFabiano Rosas         }
99252926b0dSFabiano Rosas         break;
99352926b0dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
994bca2c6d9SFabiano Rosas     {
995bca2c6d9SFabiano Rosas         int lev = env->error_code;
99652926b0dSFabiano Rosas 
9972306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
99852926b0dSFabiano Rosas             dump_hcall(env);
99952926b0dSFabiano Rosas         } else {
100052926b0dSFabiano Rosas             dump_syscall(env);
100152926b0dSFabiano Rosas         }
100252926b0dSFabiano Rosas 
100352926b0dSFabiano Rosas         /*
100452926b0dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
100552926b0dSFabiano Rosas          * to point to the next instruction
100652926b0dSFabiano Rosas          */
100752926b0dSFabiano Rosas         env->nip += 4;
100852926b0dSFabiano Rosas 
1009bca2c6d9SFabiano Rosas         /*
1010bca2c6d9SFabiano Rosas          * The Virtual Open Firmware (VOF) relies on the 'sc 1'
1011bca2c6d9SFabiano Rosas          * instruction to communicate with QEMU. The pegasos2 machine
1012bca2c6d9SFabiano Rosas          * uses VOF and the 74xx CPUs, so although the 74xx don't have
1013bca2c6d9SFabiano Rosas          * HV mode, we need to keep hypercall support.
1014bca2c6d9SFabiano Rosas          */
10152306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
101652926b0dSFabiano Rosas             PPCVirtualHypervisorClass *vhc =
101752926b0dSFabiano Rosas                 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
101852926b0dSFabiano Rosas             vhc->hypercall(cpu->vhyp, cpu);
10192c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
102052926b0dSFabiano Rosas             return;
102152926b0dSFabiano Rosas         }
1022bca2c6d9SFabiano Rosas 
102352926b0dSFabiano Rosas         break;
1024bca2c6d9SFabiano Rosas     }
102552926b0dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
102652926b0dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
102752926b0dSFabiano Rosas         break;
102852926b0dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
10298e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
1030*bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
1031*bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset "
103252926b0dSFabiano Rosas                       "exception %d with no HV support\n", excp);
103352926b0dSFabiano Rosas         }
103452926b0dSFabiano Rosas         break;
103552926b0dSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
103652926b0dSFabiano Rosas         break;
103752926b0dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
103852926b0dSFabiano Rosas         break;
103952926b0dSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
104052926b0dSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
104152926b0dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
104252926b0dSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
104352926b0dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
1044*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
104552926b0dSFabiano Rosas                   powerpc_excp_name(excp));
104652926b0dSFabiano Rosas         break;
104752926b0dSFabiano Rosas     default:
1048*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1049*bc30c1c6SBALATON Zoltan                   excp);
105052926b0dSFabiano Rosas         break;
105152926b0dSFabiano Rosas     }
105252926b0dSFabiano Rosas 
105352926b0dSFabiano Rosas     /*
105452926b0dSFabiano Rosas      * Sort out endianness of interrupt, this differs depending on the
105552926b0dSFabiano Rosas      * CPU, the HV mode, etc...
105652926b0dSFabiano Rosas      */
105752926b0dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
105852926b0dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
105952926b0dSFabiano Rosas     }
106052926b0dSFabiano Rosas 
106152926b0dSFabiano Rosas     /* Save PC */
1062f82db777SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
106352926b0dSFabiano Rosas 
106452926b0dSFabiano Rosas     /* Save MSR */
1065f82db777SFabiano Rosas     env->spr[SPR_SRR1] = msr;
106652926b0dSFabiano Rosas 
106752926b0dSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
106852926b0dSFabiano Rosas }
106952926b0dSFabiano Rosas 
1070180952ceSFabiano Rosas static void powerpc_excp_booke(PowerPCCPU *cpu, int excp)
1071180952ceSFabiano Rosas {
1072180952ceSFabiano Rosas     CPUPPCState *env = &cpu->env;
1073180952ceSFabiano Rosas     target_ulong msr, new_msr, vector;
1074904e8428SFabiano Rosas     int srr0, srr1;
1075180952ceSFabiano Rosas 
1076180952ceSFabiano Rosas     msr = env->msr;
1077180952ceSFabiano Rosas 
1078180952ceSFabiano Rosas     /*
10799dc20cc3SFabiano Rosas      * new interrupt handler msr preserves existing ME unless
1080e6a19a64SMichael Tokarev      * explicitly overridden
1081180952ceSFabiano Rosas      */
10829dc20cc3SFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
1083180952ceSFabiano Rosas 
1084180952ceSFabiano Rosas     /* target registers */
1085180952ceSFabiano Rosas     srr0 = SPR_SRR0;
1086180952ceSFabiano Rosas     srr1 = SPR_SRR1;
1087180952ceSFabiano Rosas 
1088180952ceSFabiano Rosas     /*
1089180952ceSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
10909dc20cc3SFabiano Rosas      * arch 2.05 server or later.
1091180952ceSFabiano Rosas      */
10929dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
1093180952ceSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
1094180952ceSFabiano Rosas     }
1095180952ceSFabiano Rosas 
1096180952ceSFabiano Rosas #ifdef TARGET_PPC64
1097180952ceSFabiano Rosas     /*
1098180952ceSFabiano Rosas      * SPEU and VPU share the same IVOR but they exist in different
1099180952ceSFabiano Rosas      * processors. SPEU is e500v1/2 only and VPU is e6500 only.
1100180952ceSFabiano Rosas      */
11019dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_VPU) {
1102180952ceSFabiano Rosas         excp = POWERPC_EXCP_SPEU;
1103180952ceSFabiano Rosas     }
1104180952ceSFabiano Rosas #endif
1105180952ceSFabiano Rosas 
1106180952ceSFabiano Rosas     vector = env->excp_vectors[excp];
1107180952ceSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
1108*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
1109*bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
1110180952ceSFabiano Rosas     }
1111180952ceSFabiano Rosas 
1112180952ceSFabiano Rosas     vector |= env->excp_prefix;
1113180952ceSFabiano Rosas 
1114180952ceSFabiano Rosas     switch (excp) {
1115180952ceSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
1116180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
1117180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
1118180952ceSFabiano Rosas         break;
1119180952ceSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
1120819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
1121180952ceSFabiano Rosas         /* machine check exceptions don't have ME set */
1122180952ceSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
1123180952ceSFabiano Rosas 
1124180952ceSFabiano Rosas         /* FIXME: choose one or the other based on CPU type */
1125180952ceSFabiano Rosas         srr0 = SPR_BOOKE_MCSRR0;
1126180952ceSFabiano Rosas         srr1 = SPR_BOOKE_MCSRR1;
1127180952ceSFabiano Rosas 
1128180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR0] = env->nip;
1129180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR1] = msr;
1130db403211SFabiano Rosas 
1131180952ceSFabiano Rosas         break;
1132180952ceSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
1133afdbc869SFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
1134180952ceSFabiano Rosas         break;
1135180952ceSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
1136180952ceSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
1137180952ceSFabiano Rosas         break;
1138180952ceSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
1139180952ceSFabiano Rosas         if (env->mpic_proxy) {
1140*bc30c1c6SBALATON Zoltan             CPUState *cs = env_cpu(env);
1141180952ceSFabiano Rosas             /* IACK the IRQ on delivery */
1142180952ceSFabiano Rosas             env->spr[SPR_BOOKE_EPR] = ldl_phys(cs->as, env->mpic_iack);
1143180952ceSFabiano Rosas         }
1144180952ceSFabiano Rosas         break;
1145180952ceSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
1146180952ceSFabiano Rosas         break;
1147180952ceSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
1148180952ceSFabiano Rosas         switch (env->error_code & ~0xF) {
1149180952ceSFabiano Rosas         case POWERPC_EXCP_FP:
1150da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
1151180952ceSFabiano Rosas                 trace_ppc_excp_fp_ignore();
11523680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
1153180952ceSFabiano Rosas                 return;
1154180952ceSFabiano Rosas             }
1155180952ceSFabiano Rosas 
1156180952ceSFabiano Rosas             /*
1157180952ceSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
1158180952ceSFabiano Rosas              * instruction, so always use store_next and claim we are
1159180952ceSFabiano Rosas              * precise in the MSR.
1160180952ceSFabiano Rosas              */
1161180952ceSFabiano Rosas             msr |= 0x00100000;
1162180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_FP;
1163180952ceSFabiano Rosas             break;
1164180952ceSFabiano Rosas         case POWERPC_EXCP_INVAL:
1165180952ceSFabiano Rosas             trace_ppc_excp_inval(env->nip);
1166180952ceSFabiano Rosas             msr |= 0x00080000;
1167180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PIL;
1168180952ceSFabiano Rosas             break;
1169180952ceSFabiano Rosas         case POWERPC_EXCP_PRIV:
1170180952ceSFabiano Rosas             msr |= 0x00040000;
1171180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PPR;
1172180952ceSFabiano Rosas             break;
1173180952ceSFabiano Rosas         case POWERPC_EXCP_TRAP:
1174180952ceSFabiano Rosas             msr |= 0x00020000;
1175180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PTR;
1176180952ceSFabiano Rosas             break;
1177180952ceSFabiano Rosas         default:
1178180952ceSFabiano Rosas             /* Should never occur */
1179*bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
1180180952ceSFabiano Rosas                       env->error_code);
1181180952ceSFabiano Rosas             break;
1182180952ceSFabiano Rosas         }
1183180952ceSFabiano Rosas         break;
1184180952ceSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
1185180952ceSFabiano Rosas         dump_syscall(env);
1186180952ceSFabiano Rosas 
1187180952ceSFabiano Rosas         /*
1188180952ceSFabiano Rosas          * We need to correct the NIP which in this case is supposed
1189180952ceSFabiano Rosas          * to point to the next instruction
1190180952ceSFabiano Rosas          */
1191180952ceSFabiano Rosas         env->nip += 4;
1192180952ceSFabiano Rosas         break;
1193180952ceSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
1194180952ceSFabiano Rosas     case POWERPC_EXCP_APU:       /* Auxiliary processor unavailable          */
1195180952ceSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
1196180952ceSFabiano Rosas         break;
1197180952ceSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
1198180952ceSFabiano Rosas         /* FIT on 4xx */
1199180952ceSFabiano Rosas         trace_ppc_excp_print("FIT");
1200180952ceSFabiano Rosas         break;
1201180952ceSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
1202180952ceSFabiano Rosas         trace_ppc_excp_print("WDT");
1203180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
1204180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
1205180952ceSFabiano Rosas         break;
1206180952ceSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
1207180952ceSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
1208180952ceSFabiano Rosas         break;
1209180952ceSFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
1210180952ceSFabiano Rosas         if (env->flags & POWERPC_FLAG_DE) {
1211180952ceSFabiano Rosas             /* FIXME: choose one or the other based on CPU type */
1212180952ceSFabiano Rosas             srr0 = SPR_BOOKE_DSRR0;
1213180952ceSFabiano Rosas             srr1 = SPR_BOOKE_DSRR1;
1214180952ceSFabiano Rosas 
1215180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR0] = env->nip;
1216180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR1] = msr;
1217180952ceSFabiano Rosas 
1218180952ceSFabiano Rosas             /* DBSR already modified by caller */
1219180952ceSFabiano Rosas         } else {
1220*bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
1221*bc30c1c6SBALATON Zoltan                       "Debug exception triggered on unsupported model\n");
1222180952ceSFabiano Rosas         }
1223180952ceSFabiano Rosas         break;
1224180952ceSFabiano Rosas     case POWERPC_EXCP_SPEU:   /* SPE/embedded floating-point unavailable/VPU  */
1225180952ceSFabiano Rosas         env->spr[SPR_BOOKE_ESR] = ESR_SPV;
1226180952ceSFabiano Rosas         break;
12279364df26SNicholas Piggin     case POWERPC_EXCP_DOORI:     /* Embedded doorbell interrupt              */
12289364df26SNicholas Piggin         break;
12299364df26SNicholas Piggin     case POWERPC_EXCP_DOORCI:    /* Embedded doorbell critical interrupt     */
12309364df26SNicholas Piggin         srr0 = SPR_BOOKE_CSRR0;
12319364df26SNicholas Piggin         srr1 = SPR_BOOKE_CSRR1;
12329364df26SNicholas Piggin         break;
1233180952ceSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
12348e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
1235*bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
1236*bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset "
1237180952ceSFabiano Rosas                       "exception %d with no HV support\n", excp);
1238180952ceSFabiano Rosas         }
1239180952ceSFabiano Rosas         break;
1240180952ceSFabiano Rosas     case POWERPC_EXCP_EFPDI:     /* Embedded floating-point data interrupt   */
1241180952ceSFabiano Rosas     case POWERPC_EXCP_EFPRI:     /* Embedded floating-point round interrupt  */
1242*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
1243180952ceSFabiano Rosas                   powerpc_excp_name(excp));
1244180952ceSFabiano Rosas         break;
1245180952ceSFabiano Rosas     default:
1246*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1247*bc30c1c6SBALATON Zoltan                   excp);
1248180952ceSFabiano Rosas         break;
1249180952ceSFabiano Rosas     }
1250180952ceSFabiano Rosas 
1251180952ceSFabiano Rosas #if defined(TARGET_PPC64)
1252180952ceSFabiano Rosas     if (env->spr[SPR_BOOKE_EPCR] & EPCR_ICM) {
1253180952ceSFabiano Rosas         /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */
1254180952ceSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_CM;
1255180952ceSFabiano Rosas     } else {
1256180952ceSFabiano Rosas         vector = (uint32_t)vector;
1257180952ceSFabiano Rosas     }
1258180952ceSFabiano Rosas #endif
1259180952ceSFabiano Rosas 
1260180952ceSFabiano Rosas     /* Save PC */
1261180952ceSFabiano Rosas     env->spr[srr0] = env->nip;
1262180952ceSFabiano Rosas 
1263180952ceSFabiano Rosas     /* Save MSR */
1264180952ceSFabiano Rosas     env->spr[srr1] = msr;
1265180952ceSFabiano Rosas 
1266180952ceSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
1267180952ceSFabiano Rosas }
1268180952ceSFabiano Rosas 
12697cebc5dbSNicholas Piggin /*
12707cebc5dbSNicholas Piggin  * When running a nested HV guest under vhyp, external interrupts are
12717cebc5dbSNicholas Piggin  * delivered as HVIRT.
12727cebc5dbSNicholas Piggin  */
12737cebc5dbSNicholas Piggin static bool books_vhyp_promotes_external_to_hvirt(PowerPCCPU *cpu)
12747cebc5dbSNicholas Piggin {
12757cebc5dbSNicholas Piggin     if (cpu->vhyp) {
12767cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
12777cebc5dbSNicholas Piggin     }
12787cebc5dbSNicholas Piggin     return false;
12797cebc5dbSNicholas Piggin }
12807cebc5dbSNicholas Piggin 
128130c4e426SFabiano Rosas #ifdef TARGET_PPC64
12824c6cf6b2SNicholas Piggin /*
12834c6cf6b2SNicholas Piggin  * When running under vhyp, hcalls are always intercepted and sent to the
12844c6cf6b2SNicholas Piggin  * vhc->hypercall handler.
12854c6cf6b2SNicholas Piggin  */
12864c6cf6b2SNicholas Piggin static bool books_vhyp_handles_hcall(PowerPCCPU *cpu)
12874c6cf6b2SNicholas Piggin {
12884c6cf6b2SNicholas Piggin     if (cpu->vhyp) {
12897cebc5dbSNicholas Piggin         return !vhyp_cpu_in_nested(cpu);
12907cebc5dbSNicholas Piggin     }
12917cebc5dbSNicholas Piggin     return false;
12927cebc5dbSNicholas Piggin }
12937cebc5dbSNicholas Piggin 
12947cebc5dbSNicholas Piggin /*
12957cebc5dbSNicholas Piggin  * When running a nested KVM HV guest under vhyp, HV exceptions are not
12967cebc5dbSNicholas Piggin  * delivered to the guest (because there is no concept of HV support), but
12977cebc5dbSNicholas Piggin  * rather they are sent to the vhyp to exit from the L2 back to the L1 and
12987cebc5dbSNicholas Piggin  * return from the H_ENTER_NESTED hypercall.
12997cebc5dbSNicholas Piggin  */
13007cebc5dbSNicholas Piggin static bool books_vhyp_handles_hv_excp(PowerPCCPU *cpu)
13017cebc5dbSNicholas Piggin {
13027cebc5dbSNicholas Piggin     if (cpu->vhyp) {
13037cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
13044c6cf6b2SNicholas Piggin     }
13054c6cf6b2SNicholas Piggin     return false;
13064c6cf6b2SNicholas Piggin }
13074c6cf6b2SNicholas Piggin 
13085a5d3b23SNicholas Piggin #ifdef CONFIG_TCG
13095a5d3b23SNicholas Piggin static bool is_prefix_insn(CPUPPCState *env, uint32_t insn)
13105a5d3b23SNicholas Piggin {
13115a5d3b23SNicholas Piggin     if (!(env->insns_flags2 & PPC2_ISA310)) {
13125a5d3b23SNicholas Piggin         return false;
13135a5d3b23SNicholas Piggin     }
13145a5d3b23SNicholas Piggin     return ((insn & 0xfc000000) == 0x04000000);
13155a5d3b23SNicholas Piggin }
13165a5d3b23SNicholas Piggin 
13175a5d3b23SNicholas Piggin static bool is_prefix_insn_excp(PowerPCCPU *cpu, int excp)
13185a5d3b23SNicholas Piggin {
13195a5d3b23SNicholas Piggin     CPUPPCState *env = &cpu->env;
13205a5d3b23SNicholas Piggin 
1321c8fd9667SNicholas Piggin     if (!(env->insns_flags2 & PPC2_ISA310)) {
1322c8fd9667SNicholas Piggin         return false;
1323c8fd9667SNicholas Piggin     }
1324c8fd9667SNicholas Piggin 
13255a5d3b23SNicholas Piggin     if (!tcg_enabled()) {
13265a5d3b23SNicholas Piggin         /*
13275a5d3b23SNicholas Piggin          * This does not load instructions and set the prefix bit correctly
13285a5d3b23SNicholas Piggin          * for injected interrupts with KVM. That may have to be discovered
13295a5d3b23SNicholas Piggin          * and set by the KVM layer before injecting.
13305a5d3b23SNicholas Piggin          */
13315a5d3b23SNicholas Piggin         return false;
13325a5d3b23SNicholas Piggin     }
13335a5d3b23SNicholas Piggin 
13345a5d3b23SNicholas Piggin     switch (excp) {
1335c8fd9667SNicholas Piggin     case POWERPC_EXCP_MCHECK:
1336c8fd9667SNicholas Piggin         if (!(env->error_code & PPC_BIT(42))) {
1337c8fd9667SNicholas Piggin             /*
1338c8fd9667SNicholas Piggin              * Fetch attempt caused a machine check, so attempting to fetch
1339c8fd9667SNicholas Piggin              * again would cause a recursive machine check.
1340c8fd9667SNicholas Piggin              */
1341c8fd9667SNicholas Piggin             return false;
1342c8fd9667SNicholas Piggin         }
1343c8fd9667SNicholas Piggin         break;
13445a5d3b23SNicholas Piggin     case POWERPC_EXCP_HDSI:
13455a5d3b23SNicholas Piggin         /* HDSI PRTABLE_FAULT has the originating access type in error_code */
13465a5d3b23SNicholas Piggin         if ((env->spr[SPR_HDSISR] & DSISR_PRTABLE_FAULT) &&
13475a5d3b23SNicholas Piggin             (env->error_code == MMU_INST_FETCH)) {
13485a5d3b23SNicholas Piggin             /*
13495a5d3b23SNicholas Piggin              * Fetch failed due to partition scope translation, so prefix
13505a5d3b23SNicholas Piggin              * indication is not relevant (and attempting to load the
13515a5d3b23SNicholas Piggin              * instruction at NIP would cause recursive faults with the same
13525a5d3b23SNicholas Piggin              * translation).
13535a5d3b23SNicholas Piggin              */
1354c8fd9667SNicholas Piggin             return false;
13555a5d3b23SNicholas Piggin         }
1356c8fd9667SNicholas Piggin         break;
1357c8fd9667SNicholas Piggin 
13585a5d3b23SNicholas Piggin     case POWERPC_EXCP_DSI:
13595a5d3b23SNicholas Piggin     case POWERPC_EXCP_DSEG:
13605a5d3b23SNicholas Piggin     case POWERPC_EXCP_ALIGN:
13615a5d3b23SNicholas Piggin     case POWERPC_EXCP_PROGRAM:
13625a5d3b23SNicholas Piggin     case POWERPC_EXCP_FPU:
13635a5d3b23SNicholas Piggin     case POWERPC_EXCP_TRACE:
13645a5d3b23SNicholas Piggin     case POWERPC_EXCP_HV_EMU:
13655a5d3b23SNicholas Piggin     case POWERPC_EXCP_VPU:
13665a5d3b23SNicholas Piggin     case POWERPC_EXCP_VSXU:
13675a5d3b23SNicholas Piggin     case POWERPC_EXCP_FU:
1368c8fd9667SNicholas Piggin     case POWERPC_EXCP_HV_FU:
13695a5d3b23SNicholas Piggin         break;
13705a5d3b23SNicholas Piggin     default:
13715a5d3b23SNicholas Piggin         return false;
13725a5d3b23SNicholas Piggin     }
1373c8fd9667SNicholas Piggin 
1374c8fd9667SNicholas Piggin     return is_prefix_insn(env, ppc_ldl_code(env, env->nip));
1375c8fd9667SNicholas Piggin }
13765a5d3b23SNicholas Piggin #else
13775a5d3b23SNicholas Piggin static bool is_prefix_insn_excp(PowerPCCPU *cpu, int excp)
13785a5d3b23SNicholas Piggin {
13795a5d3b23SNicholas Piggin     return false;
13805a5d3b23SNicholas Piggin }
13815a5d3b23SNicholas Piggin #endif
13825a5d3b23SNicholas Piggin 
13839f338e4dSFabiano Rosas static void powerpc_excp_books(PowerPCCPU *cpu, int excp)
13849f338e4dSFabiano Rosas {
13859f338e4dSFabiano Rosas     CPUPPCState *env = &cpu->env;
13869f338e4dSFabiano Rosas     target_ulong msr, new_msr, vector;
13879f338e4dSFabiano Rosas     int srr0, srr1, lev = -1;
13889f338e4dSFabiano Rosas 
13899f338e4dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
13909f338e4dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
13919f338e4dSFabiano Rosas 
13929f338e4dSFabiano Rosas     /*
13939f338e4dSFabiano Rosas      * new interrupt handler msr preserves existing HV and ME unless
1394e6a19a64SMichael Tokarev      * explicitly overridden
13959f338e4dSFabiano Rosas      */
13969f338e4dSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME) | MSR_HVB);
13979f338e4dSFabiano Rosas 
13989f338e4dSFabiano Rosas     /* target registers */
13999f338e4dSFabiano Rosas     srr0 = SPR_SRR0;
14009f338e4dSFabiano Rosas     srr1 = SPR_SRR1;
14019f338e4dSFabiano Rosas 
14029f338e4dSFabiano Rosas     /*
14039f338e4dSFabiano Rosas      * check for special resume at 0x100 from doze/nap/sleep/winkle on
14049f338e4dSFabiano Rosas      * P7/P8/P9
14059f338e4dSFabiano Rosas      */
14069f338e4dSFabiano Rosas     if (env->resume_as_sreset) {
14073f88a89dSBALATON Zoltan         excp = powerpc_reset_wakeup(env, excp, &msr);
14089f338e4dSFabiano Rosas     }
14099f338e4dSFabiano Rosas 
14109f338e4dSFabiano Rosas     /*
141130c4e426SFabiano Rosas      * We don't want to generate a Hypervisor Emulation Assistance
14126c242e79SNicholas Piggin      * Interrupt if we don't have HVB in msr_mask (PAPR mode),
14136c242e79SNicholas Piggin      * unless running a nested-hv guest, in which case the L1
14146c242e79SNicholas Piggin      * kernel wants the interrupt.
14159f338e4dSFabiano Rosas      */
14166c242e79SNicholas Piggin     if (excp == POWERPC_EXCP_HV_EMU && !(env->msr_mask & MSR_HVB) &&
14176c242e79SNicholas Piggin             !books_vhyp_handles_hv_excp(cpu)) {
14189f338e4dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
14199f338e4dSFabiano Rosas     }
14209f338e4dSFabiano Rosas 
14219f338e4dSFabiano Rosas     vector = env->excp_vectors[excp];
14229f338e4dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
1423*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
1424*bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
14259f338e4dSFabiano Rosas     }
14269f338e4dSFabiano Rosas 
14279f338e4dSFabiano Rosas     vector |= env->excp_prefix;
14289f338e4dSFabiano Rosas 
14295a5d3b23SNicholas Piggin     if (is_prefix_insn_excp(cpu, excp)) {
14305a5d3b23SNicholas Piggin         msr |= PPC_BIT(34);
14315a5d3b23SNicholas Piggin     }
14325a5d3b23SNicholas Piggin 
14339f338e4dSFabiano Rosas     switch (excp) {
14349f338e4dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
1435819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
14369f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
14379f338e4dSFabiano Rosas             /*
14389f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
14399f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR).
14409f338e4dSFabiano Rosas              */
14419f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14429f338e4dSFabiano Rosas         }
14439f338e4dSFabiano Rosas 
14449f338e4dSFabiano Rosas         /* machine check exceptions don't have ME set */
14459f338e4dSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
14469f338e4dSFabiano Rosas 
144755a7fa34SNicholas Piggin         msr |= env->error_code;
14489f338e4dSFabiano Rosas         break;
144955a7fa34SNicholas Piggin 
14509f338e4dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
14519f338e4dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
14529f338e4dSFabiano Rosas         break;
14539f338e4dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
14549f338e4dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
14559f338e4dSFabiano Rosas         msr |= env->error_code;
14569f338e4dSFabiano Rosas         break;
14579f338e4dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
14589f338e4dSFabiano Rosas     {
14599f338e4dSFabiano Rosas         bool lpes0;
14609f338e4dSFabiano Rosas 
14619f338e4dSFabiano Rosas         /*
146267baff77SFabiano Rosas          * LPES0 is only taken into consideration if we support HV
146367baff77SFabiano Rosas          * mode for this CPU.
14649f338e4dSFabiano Rosas          */
146567baff77SFabiano Rosas         if (!env->has_hv_mode) {
146667baff77SFabiano Rosas             break;
14679f338e4dSFabiano Rosas         }
14689f338e4dSFabiano Rosas 
146967baff77SFabiano Rosas         lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
147067baff77SFabiano Rosas 
14719f338e4dSFabiano Rosas         if (!lpes0) {
14729f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14739f338e4dSFabiano Rosas             new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
14749f338e4dSFabiano Rosas             srr0 = SPR_HSRR0;
14759f338e4dSFabiano Rosas             srr1 = SPR_HSRR1;
14769f338e4dSFabiano Rosas         }
147767baff77SFabiano Rosas 
14789f338e4dSFabiano Rosas         break;
14799f338e4dSFabiano Rosas     }
14809f338e4dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
14814ee5d281SNicholas Piggin         /* Optional DSISR update was removed from ISA v3.0 */
14824ee5d281SNicholas Piggin         if (!(env->insns_flags2 & PPC2_ISA300)) {
14839f338e4dSFabiano Rosas             /* Get rS/rD and rA from faulting opcode */
14849f338e4dSFabiano Rosas             /*
14859f338e4dSFabiano Rosas              * Note: the opcode fields will not be set properly for a
14869f338e4dSFabiano Rosas              * direct store load/store, but nobody cares as nobody
14879f338e4dSFabiano Rosas              * actually uses direct store segments.
14889f338e4dSFabiano Rosas              */
14899f338e4dSFabiano Rosas             env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
14904ee5d281SNicholas Piggin         }
14919f338e4dSFabiano Rosas         break;
14929f338e4dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
14939f338e4dSFabiano Rosas         switch (env->error_code & ~0xF) {
14949f338e4dSFabiano Rosas         case POWERPC_EXCP_FP:
1495da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
14969f338e4dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
14973680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
14989f338e4dSFabiano Rosas                 return;
14999f338e4dSFabiano Rosas             }
15009f338e4dSFabiano Rosas 
15019f338e4dSFabiano Rosas             /*
15029f338e4dSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
15039f338e4dSFabiano Rosas              * instruction, so always use store_next and claim we are
15049f338e4dSFabiano Rosas              * precise in the MSR.
15059f338e4dSFabiano Rosas              */
15069f338e4dSFabiano Rosas             msr |= 0x00100000;
15079f338e4dSFabiano Rosas             break;
15089f338e4dSFabiano Rosas         case POWERPC_EXCP_INVAL:
15099f338e4dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
15109f338e4dSFabiano Rosas             msr |= 0x00080000;
15119f338e4dSFabiano Rosas             break;
15129f338e4dSFabiano Rosas         case POWERPC_EXCP_PRIV:
15139f338e4dSFabiano Rosas             msr |= 0x00040000;
15149f338e4dSFabiano Rosas             break;
15159f338e4dSFabiano Rosas         case POWERPC_EXCP_TRAP:
15169f338e4dSFabiano Rosas             msr |= 0x00020000;
15179f338e4dSFabiano Rosas             break;
15189f338e4dSFabiano Rosas         default:
15199f338e4dSFabiano Rosas             /* Should never occur */
1520*bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
15219f338e4dSFabiano Rosas                       env->error_code);
15229f338e4dSFabiano Rosas             break;
15239f338e4dSFabiano Rosas         }
15249f338e4dSFabiano Rosas         break;
15259f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
15269f338e4dSFabiano Rosas         lev = env->error_code;
15279f338e4dSFabiano Rosas 
15282306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
15299f338e4dSFabiano Rosas             dump_hcall(env);
15309f338e4dSFabiano Rosas         } else {
15319f338e4dSFabiano Rosas             dump_syscall(env);
15329f338e4dSFabiano Rosas         }
15339f338e4dSFabiano Rosas 
15349f338e4dSFabiano Rosas         /*
15359f338e4dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
15369f338e4dSFabiano Rosas          * to point to the next instruction
15379f338e4dSFabiano Rosas          */
15389f338e4dSFabiano Rosas         env->nip += 4;
15399f338e4dSFabiano Rosas 
15409f338e4dSFabiano Rosas         /* "PAPR mode" built-in hypercall emulation */
15412306c606SBALATON Zoltan         if (lev == 1 && books_vhyp_handles_hcall(cpu)) {
15429f338e4dSFabiano Rosas             PPCVirtualHypervisorClass *vhc =
15439f338e4dSFabiano Rosas                 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
15449f338e4dSFabiano Rosas             vhc->hypercall(cpu->vhyp, cpu);
15452c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
15469f338e4dSFabiano Rosas             return;
15479f338e4dSFabiano Rosas         }
1548eb701f30SNicholas Piggin         if (env->insns_flags2 & PPC2_ISA310) {
1549eb701f30SNicholas Piggin             /* ISAv3.1 puts LEV into SRR1 */
1550eb701f30SNicholas Piggin             msr |= lev << 20;
1551eb701f30SNicholas Piggin         }
15529f338e4dSFabiano Rosas         if (lev == 1) {
15539f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
15549f338e4dSFabiano Rosas         }
15559f338e4dSFabiano Rosas         break;
15569f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL_VECTORED: /* scv exception                     */
15579f338e4dSFabiano Rosas         lev = env->error_code;
15589f338e4dSFabiano Rosas         dump_syscall(env);
15599f338e4dSFabiano Rosas         env->nip += 4;
15609f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_EE);
15619f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
15629f338e4dSFabiano Rosas 
15639f338e4dSFabiano Rosas         vector += lev * 0x20;
15649f338e4dSFabiano Rosas 
15659f338e4dSFabiano Rosas         env->lr = env->nip;
15669f338e4dSFabiano Rosas         env->ctr = msr;
15679f338e4dSFabiano Rosas         break;
15689f338e4dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
15699f338e4dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
15709f338e4dSFabiano Rosas         break;
15719f338e4dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
15729f338e4dSFabiano Rosas         /* A power-saving exception sets ME, otherwise it is unchanged */
15738e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
15749f338e4dSFabiano Rosas             /* indicate that we resumed from power save mode */
15759f338e4dSFabiano Rosas             msr |= 0x10000;
15769f338e4dSFabiano Rosas             new_msr |= ((target_ulong)1 << MSR_ME);
15779f338e4dSFabiano Rosas         }
15789f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
15799f338e4dSFabiano Rosas             /*
15809f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
15819f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR, NMI injection in QEMU).
15829f338e4dSFabiano Rosas              */
15839f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
15849f338e4dSFabiano Rosas         } else {
15858e54ad65SVíctor Colombo             if (FIELD_EX64(env->msr, MSR, POW)) {
1586*bc30c1c6SBALATON Zoltan                 cpu_abort(env_cpu(env),
1587*bc30c1c6SBALATON Zoltan                           "Trying to deliver power-saving system reset "
15889f338e4dSFabiano Rosas                           "exception %d with no HV support\n", excp);
15899f338e4dSFabiano Rosas             }
15909f338e4dSFabiano Rosas         }
15919f338e4dSFabiano Rosas         break;
159214895384SNicholas Piggin     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
159314895384SNicholas Piggin         msr |= env->error_code;
159414895384SNicholas Piggin         /* fall through */
15959f338e4dSFabiano Rosas     case POWERPC_EXCP_DSEG:      /* Data segment exception                   */
15969f338e4dSFabiano Rosas     case POWERPC_EXCP_ISEG:      /* Instruction segment exception            */
1597fd7abfabSNicholas Piggin     case POWERPC_EXCP_SDOOR:     /* Doorbell interrupt                       */
1598c29b0704SNicholas Piggin     case POWERPC_EXCP_PERFM:     /* Performance monitor interrupt            */
15999f338e4dSFabiano Rosas         break;
16009f338e4dSFabiano Rosas     case POWERPC_EXCP_HISI:      /* Hypervisor instruction storage exception */
16019f338e4dSFabiano Rosas         msr |= env->error_code;
16029f338e4dSFabiano Rosas         /* fall through */
16039f338e4dSFabiano Rosas     case POWERPC_EXCP_HDECR:     /* Hypervisor decrementer exception         */
16049f338e4dSFabiano Rosas     case POWERPC_EXCP_HDSI:      /* Hypervisor data storage exception        */
16059f338e4dSFabiano Rosas     case POWERPC_EXCP_SDOOR_HV:  /* Hypervisor Doorbell interrupt            */
16069f338e4dSFabiano Rosas     case POWERPC_EXCP_HVIRT:     /* Hypervisor virtualization                */
16079f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
16089f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
16099f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
16109f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
16119f338e4dSFabiano Rosas         break;
1612a3c020d8SNicholas Piggin #ifdef CONFIG_TCG
1613a3c020d8SNicholas Piggin     case POWERPC_EXCP_HV_EMU: {
1614a3c020d8SNicholas Piggin         uint32_t insn = ppc_ldl_code(env, env->nip);
1615a3c020d8SNicholas Piggin         env->spr[SPR_HEIR] = insn;
1616a3c020d8SNicholas Piggin         if (is_prefix_insn(env, insn)) {
1617a3c020d8SNicholas Piggin             uint32_t insn2 = ppc_ldl_code(env, env->nip + 4);
1618a3c020d8SNicholas Piggin             env->spr[SPR_HEIR] <<= 32;
1619a3c020d8SNicholas Piggin             env->spr[SPR_HEIR] |= insn2;
1620a3c020d8SNicholas Piggin         }
1621a3c020d8SNicholas Piggin         srr0 = SPR_HSRR0;
1622a3c020d8SNicholas Piggin         srr1 = SPR_HSRR1;
1623a3c020d8SNicholas Piggin         new_msr |= (target_ulong)MSR_HVB;
1624a3c020d8SNicholas Piggin         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
1625a3c020d8SNicholas Piggin         break;
1626a3c020d8SNicholas Piggin     }
1627a3c020d8SNicholas Piggin #endif
16289f338e4dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
16299f338e4dSFabiano Rosas     case POWERPC_EXCP_VSXU:       /* VSX unavailable exception               */
16309f338e4dSFabiano Rosas     case POWERPC_EXCP_FU:         /* Facility unavailable exception          */
16319f338e4dSFabiano Rosas         env->spr[SPR_FSCR] |= ((target_ulong)env->error_code << 56);
16329f338e4dSFabiano Rosas         break;
16339f338e4dSFabiano Rosas     case POWERPC_EXCP_HV_FU:     /* Hypervisor Facility Unavailable Exception */
16349f338e4dSFabiano Rosas         env->spr[SPR_HFSCR] |= ((target_ulong)env->error_code << FSCR_IC_POS);
16359f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
16369f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
16379f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
16389f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
16399f338e4dSFabiano Rosas         break;
1640cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_PERFM_EBB:        /* Performance Monitor EBB Exception  */
1641cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_EXTERNAL_EBB:     /* External EBB Exception             */
1642cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
1643cb76bbc4SDaniel Henrique Barboza 
1644cb76bbc4SDaniel Henrique Barboza         /*
1645cb76bbc4SDaniel Henrique Barboza          * Save NIP for rfebb insn in SPR_EBBRR. Next nip is
1646cb76bbc4SDaniel Henrique Barboza          * stored in the EBB Handler SPR_EBBHR.
1647cb76bbc4SDaniel Henrique Barboza          */
1648cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_EBBRR] = env->nip;
1649cb76bbc4SDaniel Henrique Barboza         powerpc_set_excp_state(cpu, env->spr[SPR_EBBHR], env->msr);
1650cb76bbc4SDaniel Henrique Barboza 
1651cb76bbc4SDaniel Henrique Barboza         /*
1652cb76bbc4SDaniel Henrique Barboza          * This exception is handled in userspace. No need to proceed.
1653cb76bbc4SDaniel Henrique Barboza          */
1654cb76bbc4SDaniel Henrique Barboza         return;
16559f338e4dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
16569f338e4dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
16579f338e4dSFabiano Rosas     case POWERPC_EXCP_MAINT:     /* Maintenance exception                    */
165830c4e426SFabiano Rosas     case POWERPC_EXCP_HV_MAINT:  /* Hypervisor Maintenance exception         */
1659*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
16609f338e4dSFabiano Rosas                   powerpc_excp_name(excp));
16619f338e4dSFabiano Rosas         break;
16629f338e4dSFabiano Rosas     default:
1663*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1664*bc30c1c6SBALATON Zoltan                   excp);
16659f338e4dSFabiano Rosas         break;
16669f338e4dSFabiano Rosas     }
16679f338e4dSFabiano Rosas 
16689f338e4dSFabiano Rosas     /*
16699f338e4dSFabiano Rosas      * Sort out endianness of interrupt, this differs depending on the
16709f338e4dSFabiano Rosas      * CPU, the HV mode, etc...
16719f338e4dSFabiano Rosas      */
16729f338e4dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
16739f338e4dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
16749f338e4dSFabiano Rosas     }
16759f338e4dSFabiano Rosas 
16769f338e4dSFabiano Rosas     new_msr |= (target_ulong)1 << MSR_SF;
16779f338e4dSFabiano Rosas 
16789f338e4dSFabiano Rosas     if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
16799f338e4dSFabiano Rosas         /* Save PC */
16809f338e4dSFabiano Rosas         env->spr[srr0] = env->nip;
16819f338e4dSFabiano Rosas 
16829f338e4dSFabiano Rosas         /* Save MSR */
16839f338e4dSFabiano Rosas         env->spr[srr1] = msr;
16849f338e4dSFabiano Rosas     }
16859f338e4dSFabiano Rosas 
16867cebc5dbSNicholas Piggin     if ((new_msr & MSR_HVB) && books_vhyp_handles_hv_excp(cpu)) {
16877cebc5dbSNicholas Piggin         PPCVirtualHypervisorClass *vhc =
16887cebc5dbSNicholas Piggin             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
16897cebc5dbSNicholas Piggin         /* Deliver interrupt to L1 by returning from the H_ENTER_NESTED call */
16907cebc5dbSNicholas Piggin         vhc->deliver_hv_excp(cpu, excp);
16917cebc5dbSNicholas Piggin 
16927cebc5dbSNicholas Piggin         powerpc_reset_excp_state(cpu);
16937cebc5dbSNicholas Piggin 
16947cebc5dbSNicholas Piggin     } else {
16957cebc5dbSNicholas Piggin         /* Sanity check */
16967cebc5dbSNicholas Piggin         if (!(env->msr_mask & MSR_HVB) && srr0 == SPR_HSRR0) {
1697*bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Trying to deliver HV exception (HSRR) %d "
1698*bc30c1c6SBALATON Zoltan                       "with no HV support\n", excp);
16997cebc5dbSNicholas Piggin         }
17007cebc5dbSNicholas Piggin 
17019f338e4dSFabiano Rosas         /* This can update new_msr and vector if AIL applies */
170210895ab6SFabiano Rosas         ppc_excp_apply_ail(cpu, excp, msr, &new_msr, &vector);
17039f338e4dSFabiano Rosas 
17049f338e4dSFabiano Rosas         powerpc_set_excp_state(cpu, vector, new_msr);
17059f338e4dSFabiano Rosas     }
17067cebc5dbSNicholas Piggin }
170730c4e426SFabiano Rosas #else
170830c4e426SFabiano Rosas static inline void powerpc_excp_books(PowerPCCPU *cpu, int excp)
170930c4e426SFabiano Rosas {
171030c4e426SFabiano Rosas     g_assert_not_reached();
171130c4e426SFabiano Rosas }
171230c4e426SFabiano Rosas #endif
17139f338e4dSFabiano Rosas 
1714dc88dd0aSFabiano Rosas static void powerpc_excp(PowerPCCPU *cpu, int excp)
1715dc88dd0aSFabiano Rosas {
1716dc88dd0aSFabiano Rosas     CPUPPCState *env = &cpu->env;
1717dc88dd0aSFabiano Rosas 
1718c6eaac89SFabiano Rosas     if (excp <= POWERPC_EXCP_NONE || excp >= POWERPC_EXCP_NB) {
1719*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1720*bc30c1c6SBALATON Zoltan                   excp);
1721c6eaac89SFabiano Rosas     }
1722c6eaac89SFabiano Rosas 
1723c6eaac89SFabiano Rosas     qemu_log_mask(CPU_LOG_INT, "Raise exception at " TARGET_FMT_lx
1724c6eaac89SFabiano Rosas                   " => %s (%d) error=%02x\n", env->nip, powerpc_excp_name(excp),
1725c6eaac89SFabiano Rosas                   excp, env->error_code);
17268a15cceeSBALATON Zoltan     env->excp_stats[excp]++;
1727c6eaac89SFabiano Rosas 
1728dc88dd0aSFabiano Rosas     switch (env->excp_model) {
1729e808c2edSFabiano Rosas     case POWERPC_EXCP_40x:
1730e808c2edSFabiano Rosas         powerpc_excp_40x(cpu, excp);
1731e808c2edSFabiano Rosas         break;
173258d178fbSFabiano Rosas     case POWERPC_EXCP_6xx:
173358d178fbSFabiano Rosas         powerpc_excp_6xx(cpu, excp);
173458d178fbSFabiano Rosas         break;
1735ccfca2fcSFabiano Rosas     case POWERPC_EXCP_7xx:
1736ccfca2fcSFabiano Rosas         powerpc_excp_7xx(cpu, excp);
1737ccfca2fcSFabiano Rosas         break;
173852926b0dSFabiano Rosas     case POWERPC_EXCP_74xx:
173952926b0dSFabiano Rosas         powerpc_excp_74xx(cpu, excp);
174052926b0dSFabiano Rosas         break;
1741180952ceSFabiano Rosas     case POWERPC_EXCP_BOOKE:
1742180952ceSFabiano Rosas         powerpc_excp_booke(cpu, excp);
1743180952ceSFabiano Rosas         break;
17449f338e4dSFabiano Rosas     case POWERPC_EXCP_970:
17459f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER7:
17469f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER8:
17479f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER9:
17489f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER10:
17499f338e4dSFabiano Rosas         powerpc_excp_books(cpu, excp);
17509f338e4dSFabiano Rosas         break;
1751dc88dd0aSFabiano Rosas     default:
175228091374SFabiano Rosas         g_assert_not_reached();
1753dc88dd0aSFabiano Rosas     }
1754dc88dd0aSFabiano Rosas }
1755dc88dd0aSFabiano Rosas 
175697a8ea5aSAndreas Färber void ppc_cpu_do_interrupt(CPUState *cs)
1757c79c73f6SBlue Swirl {
175897a8ea5aSAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
17595c26a5b3SAndreas Färber 
176093130c84SFabiano Rosas     powerpc_excp(cpu, cs->exception_index);
1761c79c73f6SBlue Swirl }
1762c79c73f6SBlue Swirl 
17632dfecf01SMatheus Ferst #if defined(TARGET_PPC64)
1764c8e1de2eSMatheus Ferst #define P7_UNUSED_INTERRUPTS \
1765c8e1de2eSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_HVIRT | PPC_INTERRUPT_CEXT |       \
1766c8e1de2eSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |      \
1767c8e1de2eSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_DOORBELL | PPC_INTERRUPT_HDOORBELL | \
1768c8e1de2eSMatheus Ferst      PPC_INTERRUPT_THERM | PPC_INTERRUPT_EBB)
1769c8e1de2eSMatheus Ferst 
17709c713713SMatheus Ferst static int p7_interrupt_powersave(CPUPPCState *env)
17719c713713SMatheus Ferst {
17729c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
17739c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE0)) {
17749c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
17759c713713SMatheus Ferst     }
17769c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
17779c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE1)) {
17789c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
17799c713713SMatheus Ferst     }
17809c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_MCK) &&
17819c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
17829c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
17839c713713SMatheus Ferst     }
17849c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HMI) &&
17859c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
17869c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
17879c713713SMatheus Ferst     }
17889c713713SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
17899c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
17909c713713SMatheus Ferst     }
17919c713713SMatheus Ferst     return 0;
17929c713713SMatheus Ferst }
17939c713713SMatheus Ferst 
1794bf303fb3SMatheus Ferst static int p7_next_unmasked_interrupt(CPUPPCState *env)
1795bf303fb3SMatheus Ferst {
17964e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
17974e6b7db2SBALATON Zoltan 
1798022b7128SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
1799022b7128SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1800bf303fb3SMatheus Ferst 
1801c8e1de2eSMatheus Ferst     assert((env->pending_interrupts & P7_UNUSED_INTERRUPTS) == 0);
1802c8e1de2eSMatheus Ferst 
1803022b7128SMatheus Ferst     if (cs->halted) {
1804022b7128SMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
1805022b7128SMatheus Ferst         return p7_interrupt_powersave(env);
1806022b7128SMatheus Ferst     }
1807022b7128SMatheus Ferst 
1808bf303fb3SMatheus Ferst     /* Machine check exception */
1809bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1810bf303fb3SMatheus Ferst         return PPC_INTERRUPT_MCK;
1811bf303fb3SMatheus Ferst     }
1812bf303fb3SMatheus Ferst 
1813bf303fb3SMatheus Ferst     /* Hypervisor decrementer exception */
1814bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1815bf303fb3SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1816bf303fb3SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
1817022b7128SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1818bf303fb3SMatheus Ferst             /* HDEC clears on delivery */
1819bf303fb3SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1820bf303fb3SMatheus Ferst         }
1821bf303fb3SMatheus Ferst     }
1822bf303fb3SMatheus Ferst 
1823bf303fb3SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1824bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1825bf303fb3SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1826bf303fb3SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1827bf303fb3SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
1828022b7128SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1829bf303fb3SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1830bf303fb3SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1831bf303fb3SMatheus Ferst             return PPC_INTERRUPT_EXT;
1832bf303fb3SMatheus Ferst         }
1833bf303fb3SMatheus Ferst     }
1834022b7128SMatheus Ferst     if (msr_ee != 0) {
1835bf303fb3SMatheus Ferst         /* Decrementer exception */
1836bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1837bf303fb3SMatheus Ferst             return PPC_INTERRUPT_DECR;
1838bf303fb3SMatheus Ferst         }
1839bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1840bf303fb3SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1841bf303fb3SMatheus Ferst         }
1842bf303fb3SMatheus Ferst     }
1843bf303fb3SMatheus Ferst 
1844bf303fb3SMatheus Ferst     return 0;
1845bf303fb3SMatheus Ferst }
1846bf303fb3SMatheus Ferst 
1847f6194fddSMatheus Ferst #define P8_UNUSED_INTERRUPTS \
1848f6194fddSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_HVIRT |  \
1849f6194fddSMatheus Ferst     PPC_INTERRUPT_CEXT | PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL |  \
1850f6194fddSMatheus Ferst     PPC_INTERRUPT_FIT | PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1851f6194fddSMatheus Ferst 
18529c713713SMatheus Ferst static int p8_interrupt_powersave(CPUPPCState *env)
18539c713713SMatheus Ferst {
18549c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
18559c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE2)) {
18569c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
18579c713713SMatheus Ferst     }
18589c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
18599c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE3)) {
18609c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
18619c713713SMatheus Ferst     }
18629c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_MCK) &&
18639c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) {
18649c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
18659c713713SMatheus Ferst     }
18669c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HMI) &&
18679c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) {
18689c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
18699c713713SMatheus Ferst     }
18709c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
18719c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE0)) {
18729c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
18739c713713SMatheus Ferst     }
18749c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
18759c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE1)) {
18769c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
18779c713713SMatheus Ferst     }
18789c713713SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
18799c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
18809c713713SMatheus Ferst     }
18819c713713SMatheus Ferst     return 0;
18829c713713SMatheus Ferst }
18839c713713SMatheus Ferst 
1884a9899d42SMatheus Ferst static int p8_next_unmasked_interrupt(CPUPPCState *env)
1885a9899d42SMatheus Ferst {
18864e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
18874e6b7db2SBALATON Zoltan 
188864a9b5eeSMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
188964a9b5eeSMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1890a9899d42SMatheus Ferst 
1891f6194fddSMatheus Ferst     assert((env->pending_interrupts & P8_UNUSED_INTERRUPTS) == 0);
1892f6194fddSMatheus Ferst 
189364a9b5eeSMatheus Ferst     if (cs->halted) {
189464a9b5eeSMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
189564a9b5eeSMatheus Ferst         return p8_interrupt_powersave(env);
189664a9b5eeSMatheus Ferst     }
189764a9b5eeSMatheus Ferst 
1898a9899d42SMatheus Ferst     /* Machine check exception */
1899a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1900a9899d42SMatheus Ferst         return PPC_INTERRUPT_MCK;
1901a9899d42SMatheus Ferst     }
1902a9899d42SMatheus Ferst 
1903a9899d42SMatheus Ferst     /* Hypervisor decrementer exception */
1904a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1905a9899d42SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1906a9899d42SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
190764a9b5eeSMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1908a9899d42SMatheus Ferst             /* HDEC clears on delivery */
1909a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1910a9899d42SMatheus Ferst         }
1911a9899d42SMatheus Ferst     }
1912a9899d42SMatheus Ferst 
1913a9899d42SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1914a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1915a9899d42SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1916a9899d42SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1917a9899d42SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
191864a9b5eeSMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1919a9899d42SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1920a9899d42SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1921a9899d42SMatheus Ferst             return PPC_INTERRUPT_EXT;
1922a9899d42SMatheus Ferst         }
1923a9899d42SMatheus Ferst     }
192464a9b5eeSMatheus Ferst     if (msr_ee != 0) {
1925a9899d42SMatheus Ferst         /* Decrementer exception */
1926a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1927a9899d42SMatheus Ferst             return PPC_INTERRUPT_DECR;
1928a9899d42SMatheus Ferst         }
1929a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1930a9899d42SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
1931a9899d42SMatheus Ferst         }
1932a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
1933a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
1934a9899d42SMatheus Ferst         }
1935a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1936a9899d42SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1937a9899d42SMatheus Ferst         }
1938a9899d42SMatheus Ferst         /* EBB exception */
1939a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
1940a9899d42SMatheus Ferst             /*
1941a9899d42SMatheus Ferst              * EBB exception must be taken in problem state and
1942a9899d42SMatheus Ferst              * with BESCR_GE set.
1943a9899d42SMatheus Ferst              */
1944a9899d42SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
1945a9899d42SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
1946a9899d42SMatheus Ferst                 return PPC_INTERRUPT_EBB;
1947a9899d42SMatheus Ferst             }
1948a9899d42SMatheus Ferst         }
1949a9899d42SMatheus Ferst     }
1950a9899d42SMatheus Ferst 
1951a9899d42SMatheus Ferst     return 0;
1952a9899d42SMatheus Ferst }
1953a9899d42SMatheus Ferst 
1954b00e9a2fSMatheus Ferst #define P9_UNUSED_INTERRUPTS \
1955b00e9a2fSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT |   \
1956b00e9a2fSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |  \
1957b00e9a2fSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1958b00e9a2fSMatheus Ferst 
19599c713713SMatheus Ferst static int p9_interrupt_powersave(CPUPPCState *env)
19609c713713SMatheus Ferst {
19619c713713SMatheus Ferst     /* External Exception */
19629c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
19639c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_EEE)) {
19649c713713SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
19659c713713SMatheus Ferst         if (!heic || !FIELD_EX64_HV(env->msr) ||
19669c713713SMatheus Ferst             FIELD_EX64(env->msr, MSR, PR)) {
19679c713713SMatheus Ferst             return PPC_INTERRUPT_EXT;
19689c713713SMatheus Ferst         }
19699c713713SMatheus Ferst     }
19709c713713SMatheus Ferst     /* Decrementer Exception */
19719c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
19729c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_DEE)) {
19739c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
19749c713713SMatheus Ferst     }
19759c713713SMatheus Ferst     /* Machine Check or Hypervisor Maintenance Exception */
19769c713713SMatheus Ferst     if (env->spr[SPR_LPCR] & LPCR_OEE) {
19779c713713SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
19789c713713SMatheus Ferst             return PPC_INTERRUPT_MCK;
19799c713713SMatheus Ferst         }
19809c713713SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HMI) {
19819c713713SMatheus Ferst             return PPC_INTERRUPT_HMI;
19829c713713SMatheus Ferst         }
19839c713713SMatheus Ferst     }
19849c713713SMatheus Ferst     /* Privileged Doorbell Exception */
19859c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
19869c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_PDEE)) {
19879c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
19889c713713SMatheus Ferst     }
19899c713713SMatheus Ferst     /* Hypervisor Doorbell Exception */
19909c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
19919c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_HDEE)) {
19929c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
19939c713713SMatheus Ferst     }
19949c713713SMatheus Ferst     /* Hypervisor virtualization exception */
19959c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HVIRT) &&
19969c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_HVEE)) {
19979c713713SMatheus Ferst         return PPC_INTERRUPT_HVIRT;
19989c713713SMatheus Ferst     }
19999c713713SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
20009c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
20019c713713SMatheus Ferst     }
20029c713713SMatheus Ferst     return 0;
20039c713713SMatheus Ferst }
20049c713713SMatheus Ferst 
20052dfecf01SMatheus Ferst static int p9_next_unmasked_interrupt(CPUPPCState *env)
20062dfecf01SMatheus Ferst {
20074e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
20084e6b7db2SBALATON Zoltan 
200927796411SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
201027796411SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
20112dfecf01SMatheus Ferst 
2012b00e9a2fSMatheus Ferst     assert((env->pending_interrupts & P9_UNUSED_INTERRUPTS) == 0);
2013b00e9a2fSMatheus Ferst 
201427796411SMatheus Ferst     if (cs->halted) {
201527796411SMatheus Ferst         if (env->spr[SPR_PSSCR] & PSSCR_EC) {
201627796411SMatheus Ferst             /*
201727796411SMatheus Ferst              * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can
201827796411SMatheus Ferst              * wakeup the processor
201927796411SMatheus Ferst              */
202027796411SMatheus Ferst             return p9_interrupt_powersave(env);
202127796411SMatheus Ferst         } else {
202227796411SMatheus Ferst             /*
202327796411SMatheus Ferst              * When it's clear, any system-caused exception exits power-saving
202427796411SMatheus Ferst              * mode, even the ones that gate on MSR[EE].
202527796411SMatheus Ferst              */
202627796411SMatheus Ferst             msr_ee = true;
202727796411SMatheus Ferst         }
202827796411SMatheus Ferst     }
202927796411SMatheus Ferst 
20302dfecf01SMatheus Ferst     /* Machine check exception */
20312dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
20322dfecf01SMatheus Ferst         return PPC_INTERRUPT_MCK;
20332dfecf01SMatheus Ferst     }
20342dfecf01SMatheus Ferst 
20352dfecf01SMatheus Ferst     /* Hypervisor decrementer exception */
20362dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
20372dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
20382dfecf01SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
203927796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
20402dfecf01SMatheus Ferst             /* HDEC clears on delivery */
20412dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDECR;
20422dfecf01SMatheus Ferst         }
20432dfecf01SMatheus Ferst     }
20442dfecf01SMatheus Ferst 
20452dfecf01SMatheus Ferst     /* Hypervisor virtualization interrupt */
20462dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
20472dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
20482dfecf01SMatheus Ferst         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
204927796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hvice) {
20502dfecf01SMatheus Ferst             return PPC_INTERRUPT_HVIRT;
20512dfecf01SMatheus Ferst         }
20522dfecf01SMatheus Ferst     }
20532dfecf01SMatheus Ferst 
20542dfecf01SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
20552dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
20562dfecf01SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
20572dfecf01SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
20582dfecf01SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
205927796411SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
20602dfecf01SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
20612dfecf01SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
20622dfecf01SMatheus Ferst             return PPC_INTERRUPT_EXT;
20632dfecf01SMatheus Ferst         }
20642dfecf01SMatheus Ferst     }
206527796411SMatheus Ferst     if (msr_ee != 0) {
20662dfecf01SMatheus Ferst         /* Decrementer exception */
20672dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
20682dfecf01SMatheus Ferst             return PPC_INTERRUPT_DECR;
20692dfecf01SMatheus Ferst         }
20702dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
20712dfecf01SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
20722dfecf01SMatheus Ferst         }
20732dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
20742dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
20752dfecf01SMatheus Ferst         }
20762dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
20772dfecf01SMatheus Ferst             return PPC_INTERRUPT_PERFM;
20782dfecf01SMatheus Ferst         }
20792dfecf01SMatheus Ferst         /* EBB exception */
20802dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
20812dfecf01SMatheus Ferst             /*
20822dfecf01SMatheus Ferst              * EBB exception must be taken in problem state and
20832dfecf01SMatheus Ferst              * with BESCR_GE set.
20842dfecf01SMatheus Ferst              */
20852dfecf01SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
20862dfecf01SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
20872dfecf01SMatheus Ferst                 return PPC_INTERRUPT_EBB;
20882dfecf01SMatheus Ferst             }
20892dfecf01SMatheus Ferst         }
20902dfecf01SMatheus Ferst     }
20912dfecf01SMatheus Ferst 
20922dfecf01SMatheus Ferst     return 0;
20932dfecf01SMatheus Ferst }
20942dfecf01SMatheus Ferst #endif
20952dfecf01SMatheus Ferst 
2096ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt_generic(CPUPPCState *env)
2097c79c73f6SBlue Swirl {
20983621e2c9SBenjamin Herrenschmidt     bool async_deliver;
2099259186a7SAndreas Färber 
2100c79c73f6SBlue Swirl     /* External reset */
2101f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
2102de76b85cSMatheus Ferst         return PPC_INTERRUPT_RESET;
2103c79c73f6SBlue Swirl     }
2104c79c73f6SBlue Swirl     /* Machine check exception */
2105f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
2106de76b85cSMatheus Ferst         return PPC_INTERRUPT_MCK;
2107c79c73f6SBlue Swirl     }
2108c79c73f6SBlue Swirl #if 0 /* TODO */
2109c79c73f6SBlue Swirl     /* External debug exception */
2110f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) {
2111de76b85cSMatheus Ferst         return PPC_INTERRUPT_DEBUG;
2112c79c73f6SBlue Swirl     }
2113c79c73f6SBlue Swirl #endif
21143621e2c9SBenjamin Herrenschmidt 
21153621e2c9SBenjamin Herrenschmidt     /*
21163621e2c9SBenjamin Herrenschmidt      * For interrupts that gate on MSR:EE, we need to do something a
21173621e2c9SBenjamin Herrenschmidt      * bit more subtle, as we need to let them through even when EE is
21183621e2c9SBenjamin Herrenschmidt      * clear when coming out of some power management states (in order
21193621e2c9SBenjamin Herrenschmidt      * for them to become a 0x100).
21203621e2c9SBenjamin Herrenschmidt      */
21210939b8f8SVíctor Colombo     async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
21223621e2c9SBenjamin Herrenschmidt 
2123c79c73f6SBlue Swirl     /* Hypervisor decrementer exception */
2124f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
21254b236b62SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
21264b236b62SBenjamin Herrenschmidt         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
21279de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
21284b236b62SBenjamin Herrenschmidt             /* HDEC clears on delivery */
2129de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDECR;
2130c79c73f6SBlue Swirl         }
2131c79c73f6SBlue Swirl     }
2132d8ce5fd6SBenjamin Herrenschmidt 
2133d8ce5fd6SBenjamin Herrenschmidt     /* Hypervisor virtualization interrupt */
2134f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
2135d8ce5fd6SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
2136d8ce5fd6SBenjamin Herrenschmidt         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
21379de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
2138de76b85cSMatheus Ferst             return PPC_INTERRUPT_HVIRT;
2139d8ce5fd6SBenjamin Herrenschmidt         }
2140d8ce5fd6SBenjamin Herrenschmidt     }
2141d8ce5fd6SBenjamin Herrenschmidt 
2142d8ce5fd6SBenjamin Herrenschmidt     /* External interrupt can ignore MSR:EE under some circumstances */
2143f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
2144d1dbe37cSBenjamin Herrenschmidt         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
21456eebe6dcSBenjamin Herrenschmidt         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
21466eebe6dcSBenjamin Herrenschmidt         /* HEIC blocks delivery to the hypervisor */
21479de754d3SVíctor Colombo         if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
2148d41ccf6eSVíctor Colombo             !FIELD_EX64(env->msr, MSR, PR))) ||
21499de754d3SVíctor Colombo             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
2150de76b85cSMatheus Ferst             return PPC_INTERRUPT_EXT;
2151d1dbe37cSBenjamin Herrenschmidt         }
2152d1dbe37cSBenjamin Herrenschmidt     }
2153acc861c2SVíctor Colombo     if (FIELD_EX64(env->msr, MSR, CE)) {
2154c79c73f6SBlue Swirl         /* External critical interrupt */
2155f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CEXT) {
2156de76b85cSMatheus Ferst             return PPC_INTERRUPT_CEXT;
2157c79c73f6SBlue Swirl         }
2158c79c73f6SBlue Swirl     }
21593621e2c9SBenjamin Herrenschmidt     if (async_deliver != 0) {
2160c79c73f6SBlue Swirl         /* Watchdog timer on embedded PowerPC */
2161f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_WDT) {
2162de76b85cSMatheus Ferst             return PPC_INTERRUPT_WDT;
2163c79c73f6SBlue Swirl         }
2164f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) {
2165de76b85cSMatheus Ferst             return PPC_INTERRUPT_CDOORBELL;
2166c79c73f6SBlue Swirl         }
2167c79c73f6SBlue Swirl         /* Fixed interval timer on embedded PowerPC */
2168f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_FIT) {
2169de76b85cSMatheus Ferst             return PPC_INTERRUPT_FIT;
2170c79c73f6SBlue Swirl         }
2171c79c73f6SBlue Swirl         /* Programmable interval timer on embedded PowerPC */
2172f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PIT) {
2173de76b85cSMatheus Ferst             return PPC_INTERRUPT_PIT;
2174c79c73f6SBlue Swirl         }
2175c79c73f6SBlue Swirl         /* Decrementer exception */
2176f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
2177de76b85cSMatheus Ferst             return PPC_INTERRUPT_DECR;
2178c79c73f6SBlue Swirl         }
2179f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2180de76b85cSMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
2181c79c73f6SBlue Swirl         }
2182f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
2183de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
21847af1e7b0SCédric Le Goater         }
2185f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
2186de76b85cSMatheus Ferst             return PPC_INTERRUPT_PERFM;
2187c79c73f6SBlue Swirl         }
2188c79c73f6SBlue Swirl         /* Thermal interrupt */
2189f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_THERM) {
2190de76b85cSMatheus Ferst             return PPC_INTERRUPT_THERM;
2191c79c73f6SBlue Swirl         }
2192cb76bbc4SDaniel Henrique Barboza         /* EBB exception */
2193f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
2194cb76bbc4SDaniel Henrique Barboza             /*
2195cb76bbc4SDaniel Henrique Barboza              * EBB exception must be taken in problem state and
2196cb76bbc4SDaniel Henrique Barboza              * with BESCR_GE set.
2197cb76bbc4SDaniel Henrique Barboza              */
2198d41ccf6eSVíctor Colombo             if (FIELD_EX64(env->msr, MSR, PR) &&
2199d41ccf6eSVíctor Colombo                 (env->spr[SPR_BESCR] & BESCR_GE)) {
2200de76b85cSMatheus Ferst                 return PPC_INTERRUPT_EBB;
2201de76b85cSMatheus Ferst             }
2202de76b85cSMatheus Ferst         }
2203de76b85cSMatheus Ferst     }
2204cb76bbc4SDaniel Henrique Barboza 
2205de76b85cSMatheus Ferst     return 0;
2206de76b85cSMatheus Ferst }
2207de76b85cSMatheus Ferst 
2208ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt(CPUPPCState *env)
2209ba2898f7SMatheus Ferst {
2210ba2898f7SMatheus Ferst     switch (env->excp_model) {
22112dfecf01SMatheus Ferst #if defined(TARGET_PPC64)
2212bf303fb3SMatheus Ferst     case POWERPC_EXCP_POWER7:
2213bf303fb3SMatheus Ferst         return p7_next_unmasked_interrupt(env);
2214a9899d42SMatheus Ferst     case POWERPC_EXCP_POWER8:
2215a9899d42SMatheus Ferst         return p8_next_unmasked_interrupt(env);
22162dfecf01SMatheus Ferst     case POWERPC_EXCP_POWER9:
22172dfecf01SMatheus Ferst     case POWERPC_EXCP_POWER10:
22182dfecf01SMatheus Ferst         return p9_next_unmasked_interrupt(env);
22192dfecf01SMatheus Ferst #endif
2220ba2898f7SMatheus Ferst     default:
2221ba2898f7SMatheus Ferst         return ppc_next_unmasked_interrupt_generic(env);
2222ba2898f7SMatheus Ferst     }
2223ba2898f7SMatheus Ferst }
2224ba2898f7SMatheus Ferst 
22252fdedcbcSMatheus Ferst /*
22262fdedcbcSMatheus Ferst  * Sets CPU_INTERRUPT_HARD if there is at least one unmasked interrupt to be
22272fdedcbcSMatheus Ferst  * delivered and clears CPU_INTERRUPT_HARD otherwise.
22282fdedcbcSMatheus Ferst  *
22292fdedcbcSMatheus Ferst  * This method is called by ppc_set_interrupt when an interrupt is raised or
22302fdedcbcSMatheus Ferst  * lowered, and should also be called whenever an interrupt masking condition
22312fdedcbcSMatheus Ferst  * is changed, e.g.:
22322fdedcbcSMatheus Ferst  *  - When relevant bits of MSR are altered, like EE, HV, PR, etc.;
22332fdedcbcSMatheus Ferst  *  - When relevant bits of LPCR are altered, like PECE, HDICE, HVICE, etc.;
22342fdedcbcSMatheus Ferst  *  - When PSSCR[EC] or env->resume_as_sreset are changed;
22352fdedcbcSMatheus Ferst  *  - When cs->halted is changed and the CPU has a different interrupt masking
22362fdedcbcSMatheus Ferst  *    logic in power-saving mode (e.g., POWER7/8/9/10);
22372fdedcbcSMatheus Ferst  */
22382fdedcbcSMatheus Ferst void ppc_maybe_interrupt(CPUPPCState *env)
22392fdedcbcSMatheus Ferst {
22402fdedcbcSMatheus Ferst     CPUState *cs = env_cpu(env);
224132ead8e6SStefan Hajnoczi     BQL_LOCK_GUARD();
22422fdedcbcSMatheus Ferst 
22432fdedcbcSMatheus Ferst     if (ppc_next_unmasked_interrupt(env)) {
22442fdedcbcSMatheus Ferst         cpu_interrupt(cs, CPU_INTERRUPT_HARD);
22452fdedcbcSMatheus Ferst     } else {
22462fdedcbcSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
22472fdedcbcSMatheus Ferst     }
22482fdedcbcSMatheus Ferst }
22492fdedcbcSMatheus Ferst 
22503654e238SMatheus Ferst #if defined(TARGET_PPC64)
2251d93a4856SMatheus Ferst static void p7_deliver_interrupt(CPUPPCState *env, int interrupt)
2252d93a4856SMatheus Ferst {
2253d93a4856SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2254d93a4856SMatheus Ferst 
2255d93a4856SMatheus Ferst     switch (interrupt) {
2256d93a4856SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2257d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2258d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2259d93a4856SMatheus Ferst         break;
2260d93a4856SMatheus Ferst 
2261d93a4856SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2262d93a4856SMatheus Ferst         /* HDEC clears on delivery */
2263d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2264d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2265d93a4856SMatheus Ferst         break;
2266d93a4856SMatheus Ferst 
2267d93a4856SMatheus Ferst     case PPC_INTERRUPT_EXT:
2268d93a4856SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2269d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2270d93a4856SMatheus Ferst         } else {
2271d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2272d93a4856SMatheus Ferst         }
2273d93a4856SMatheus Ferst         break;
2274d93a4856SMatheus Ferst 
2275d93a4856SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2276d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2277d93a4856SMatheus Ferst         break;
2278d93a4856SMatheus Ferst     case PPC_INTERRUPT_PERFM:
2279d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2280d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2281d93a4856SMatheus Ferst         break;
2282d93a4856SMatheus Ferst     case 0:
2283d93a4856SMatheus Ferst         /*
2284d93a4856SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
2285d93a4856SMatheus Ferst          * anything to deliver while in a PM state that requires getting
2286d93a4856SMatheus Ferst          * out via a 0x100
2287d93a4856SMatheus Ferst          *
2288d93a4856SMatheus Ferst          * This means we will incorrectly execute past the power management
2289d93a4856SMatheus Ferst          * instruction instead of triggering a reset.
2290d93a4856SMatheus Ferst          *
2291d93a4856SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
2292d93a4856SMatheus Ferst          * processor has_work implementation and the logic in this function.
2293d93a4856SMatheus Ferst          */
2294d93a4856SMatheus Ferst         assert(!env->resume_as_sreset);
2295d93a4856SMatheus Ferst         break;
2296d93a4856SMatheus Ferst     default:
2297*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2298*bc30c1c6SBALATON Zoltan                   interrupt);
2299d93a4856SMatheus Ferst     }
2300d93a4856SMatheus Ferst }
2301d93a4856SMatheus Ferst 
23026527e757SMatheus Ferst static void p8_deliver_interrupt(CPUPPCState *env, int interrupt)
23036527e757SMatheus Ferst {
23046527e757SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
23056527e757SMatheus Ferst 
23066527e757SMatheus Ferst     switch (interrupt) {
23076527e757SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
23086527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
23096527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
23106527e757SMatheus Ferst         break;
23116527e757SMatheus Ferst 
23126527e757SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
23136527e757SMatheus Ferst         /* HDEC clears on delivery */
23146527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
23156527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
23166527e757SMatheus Ferst         break;
23176527e757SMatheus Ferst 
23186527e757SMatheus Ferst     case PPC_INTERRUPT_EXT:
23196527e757SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
23206527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
23216527e757SMatheus Ferst         } else {
23226527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
23236527e757SMatheus Ferst         }
23246527e757SMatheus Ferst         break;
23256527e757SMatheus Ferst 
23266527e757SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
23276527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
23286527e757SMatheus Ferst         break;
23296527e757SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
23306527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
23316527e757SMatheus Ferst         if (is_book3s_arch2x(env)) {
23326527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
23336527e757SMatheus Ferst         } else {
23346527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
23356527e757SMatheus Ferst         }
23366527e757SMatheus Ferst         break;
23376527e757SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
23386527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
23396527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
23406527e757SMatheus Ferst         break;
23416527e757SMatheus Ferst     case PPC_INTERRUPT_PERFM:
23426527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
23436527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
23446527e757SMatheus Ferst         break;
23456527e757SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
23466527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
23476527e757SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
23486527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
23496527e757SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
23506527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
23516527e757SMatheus Ferst         }
23526527e757SMatheus Ferst         break;
23536527e757SMatheus Ferst     case 0:
23546527e757SMatheus Ferst         /*
23556527e757SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
23566527e757SMatheus Ferst          * anything to deliver while in a PM state that requires getting
23576527e757SMatheus Ferst          * out via a 0x100
23586527e757SMatheus Ferst          *
23596527e757SMatheus Ferst          * This means we will incorrectly execute past the power management
23606527e757SMatheus Ferst          * instruction instead of triggering a reset.
23616527e757SMatheus Ferst          *
23626527e757SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
23636527e757SMatheus Ferst          * processor has_work implementation and the logic in this function.
23646527e757SMatheus Ferst          */
23656527e757SMatheus Ferst         assert(!env->resume_as_sreset);
23666527e757SMatheus Ferst         break;
23676527e757SMatheus Ferst     default:
2368*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2369*bc30c1c6SBALATON Zoltan                   interrupt);
23706527e757SMatheus Ferst     }
23716527e757SMatheus Ferst }
23726527e757SMatheus Ferst 
23733654e238SMatheus Ferst static void p9_deliver_interrupt(CPUPPCState *env, int interrupt)
23743654e238SMatheus Ferst {
23753654e238SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
23763654e238SMatheus Ferst     CPUState *cs = env_cpu(env);
23773654e238SMatheus Ferst 
237827796411SMatheus Ferst     if (cs->halted && !(env->spr[SPR_PSSCR] & PSSCR_EC) &&
237927796411SMatheus Ferst         !FIELD_EX64(env->msr, MSR, EE)) {
238027796411SMatheus Ferst         /*
238127796411SMatheus Ferst          * A pending interrupt took us out of power-saving, but MSR[EE] says
238227796411SMatheus Ferst          * that we should return to NIP+4 instead of delivering it.
238327796411SMatheus Ferst          */
238427796411SMatheus Ferst         return;
238527796411SMatheus Ferst     }
238627796411SMatheus Ferst 
23873654e238SMatheus Ferst     switch (interrupt) {
23883654e238SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
23893654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
23903654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
23913654e238SMatheus Ferst         break;
23923654e238SMatheus Ferst 
23933654e238SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
23943654e238SMatheus Ferst         /* HDEC clears on delivery */
23953654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
23963654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
23973654e238SMatheus Ferst         break;
23983654e238SMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
23993654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
24003654e238SMatheus Ferst         break;
24013654e238SMatheus Ferst 
24023654e238SMatheus Ferst     case PPC_INTERRUPT_EXT:
24033654e238SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
24043654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
24053654e238SMatheus Ferst         } else {
24063654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
24073654e238SMatheus Ferst         }
24083654e238SMatheus Ferst         break;
24093654e238SMatheus Ferst 
24103654e238SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
24113654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
24123654e238SMatheus Ferst         break;
24133654e238SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
24143654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
24153654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
24163654e238SMatheus Ferst         break;
24173654e238SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
24183654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
24193654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
24203654e238SMatheus Ferst         break;
24213654e238SMatheus Ferst     case PPC_INTERRUPT_PERFM:
24223654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
24233654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
24243654e238SMatheus Ferst         break;
24253654e238SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
24263654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
24273654e238SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
24283654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
24293654e238SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
24303654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
24313654e238SMatheus Ferst         }
24323654e238SMatheus Ferst         break;
24333654e238SMatheus Ferst     case 0:
24343654e238SMatheus Ferst         /*
24353654e238SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
24363654e238SMatheus Ferst          * anything to deliver while in a PM state that requires getting
24373654e238SMatheus Ferst          * out via a 0x100
24383654e238SMatheus Ferst          *
24393654e238SMatheus Ferst          * This means we will incorrectly execute past the power management
24403654e238SMatheus Ferst          * instruction instead of triggering a reset.
24413654e238SMatheus Ferst          *
24423654e238SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
24433654e238SMatheus Ferst          * processor has_work implementation and the logic in this function.
24443654e238SMatheus Ferst          */
24453654e238SMatheus Ferst         assert(!env->resume_as_sreset);
24463654e238SMatheus Ferst         break;
24473654e238SMatheus Ferst     default:
2448*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2449*bc30c1c6SBALATON Zoltan                   interrupt);
24503654e238SMatheus Ferst     }
24513654e238SMatheus Ferst }
24523654e238SMatheus Ferst #endif
24533654e238SMatheus Ferst 
2454ba2898f7SMatheus Ferst static void ppc_deliver_interrupt_generic(CPUPPCState *env, int interrupt)
2455de76b85cSMatheus Ferst {
2456de76b85cSMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2457de76b85cSMatheus Ferst 
2458de76b85cSMatheus Ferst     switch (interrupt) {
2459de76b85cSMatheus Ferst     case PPC_INTERRUPT_RESET: /* External reset */
2460de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_RESET;
2461de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_RESET);
2462de76b85cSMatheus Ferst         break;
2463de76b85cSMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2464de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2465de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2466de76b85cSMatheus Ferst         break;
2467de76b85cSMatheus Ferst 
2468de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2469de76b85cSMatheus Ferst         /* HDEC clears on delivery */
2470de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2471de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2472de76b85cSMatheus Ferst         break;
2473de76b85cSMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
2474de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2475de76b85cSMatheus Ferst         break;
2476de76b85cSMatheus Ferst 
2477de76b85cSMatheus Ferst     case PPC_INTERRUPT_EXT:
2478de76b85cSMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2479de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2480de76b85cSMatheus Ferst         } else {
2481de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2482de76b85cSMatheus Ferst         }
2483de76b85cSMatheus Ferst         break;
2484de76b85cSMatheus Ferst     case PPC_INTERRUPT_CEXT: /* External critical interrupt */
2485de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_CRITICAL);
2486de76b85cSMatheus Ferst         break;
2487de76b85cSMatheus Ferst 
2488de76b85cSMatheus Ferst     case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */
2489de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_WDT;
2490de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_WDT);
2491de76b85cSMatheus Ferst         break;
2492de76b85cSMatheus Ferst     case PPC_INTERRUPT_CDOORBELL:
2493de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL;
2494de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DOORCI);
2495de76b85cSMatheus Ferst         break;
2496de76b85cSMatheus Ferst     case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */
2497de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_FIT;
2498de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_FIT);
2499de76b85cSMatheus Ferst         break;
2500de76b85cSMatheus Ferst     case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */
2501de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PIT;
2502de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PIT);
2503de76b85cSMatheus Ferst         break;
2504de76b85cSMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2505de76b85cSMatheus Ferst         if (ppc_decr_clear_on_delivery(env)) {
2506de76b85cSMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DECR;
2507de76b85cSMatheus Ferst         }
2508de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2509de76b85cSMatheus Ferst         break;
2510de76b85cSMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
2511de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
2512de76b85cSMatheus Ferst         if (is_book3s_arch2x(env)) {
2513de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
2514de76b85cSMatheus Ferst         } else {
2515de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
2516de76b85cSMatheus Ferst         }
2517de76b85cSMatheus Ferst         break;
2518de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
2519de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
2520de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
2521de76b85cSMatheus Ferst         break;
2522de76b85cSMatheus Ferst     case PPC_INTERRUPT_PERFM:
2523de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2524de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2525de76b85cSMatheus Ferst         break;
2526de76b85cSMatheus Ferst     case PPC_INTERRUPT_THERM:  /* Thermal interrupt */
2527de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_THERM;
2528de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_THERM);
2529de76b85cSMatheus Ferst         break;
2530de76b85cSMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
2531de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
2532cb76bbc4SDaniel Henrique Barboza         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
2533cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
2534cb76bbc4SDaniel Henrique Barboza         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
2535cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
2536cb76bbc4SDaniel Henrique Barboza         }
2537de76b85cSMatheus Ferst         break;
2538de76b85cSMatheus Ferst     case 0:
2539f8154fd2SBenjamin Herrenschmidt         /*
2540f8154fd2SBenjamin Herrenschmidt          * This is a bug ! It means that has_work took us out of halt without
2541f8154fd2SBenjamin Herrenschmidt          * anything to deliver while in a PM state that requires getting
2542f8154fd2SBenjamin Herrenschmidt          * out via a 0x100
2543f8154fd2SBenjamin Herrenschmidt          *
2544f8154fd2SBenjamin Herrenschmidt          * This means we will incorrectly execute past the power management
2545f8154fd2SBenjamin Herrenschmidt          * instruction instead of triggering a reset.
2546f8154fd2SBenjamin Herrenschmidt          *
2547136fbf65Szhaolichang          * It generally means a discrepancy between the wakeup conditions in the
2548f8154fd2SBenjamin Herrenschmidt          * processor has_work implementation and the logic in this function.
2549f8154fd2SBenjamin Herrenschmidt          */
2550de76b85cSMatheus Ferst         assert(!env->resume_as_sreset);
2551de76b85cSMatheus Ferst         break;
2552de76b85cSMatheus Ferst     default:
2553*bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2554*bc30c1c6SBALATON Zoltan                   interrupt);
2555f8154fd2SBenjamin Herrenschmidt     }
2556c79c73f6SBlue Swirl }
255734316482SAlexey Kardashevskiy 
2558ba2898f7SMatheus Ferst static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt)
2559ba2898f7SMatheus Ferst {
2560ba2898f7SMatheus Ferst     switch (env->excp_model) {
25613654e238SMatheus Ferst #if defined(TARGET_PPC64)
2562d93a4856SMatheus Ferst     case POWERPC_EXCP_POWER7:
2563d93a4856SMatheus Ferst         p7_deliver_interrupt(env, interrupt);
2564d93a4856SMatheus Ferst         break;
25656527e757SMatheus Ferst     case POWERPC_EXCP_POWER8:
25666527e757SMatheus Ferst         p8_deliver_interrupt(env, interrupt);
25676527e757SMatheus Ferst         break;
25683654e238SMatheus Ferst     case POWERPC_EXCP_POWER9:
25693654e238SMatheus Ferst     case POWERPC_EXCP_POWER10:
25703654e238SMatheus Ferst         p9_deliver_interrupt(env, interrupt);
25713654e238SMatheus Ferst         break;
25723654e238SMatheus Ferst #endif
2573ba2898f7SMatheus Ferst     default:
2574ba2898f7SMatheus Ferst         ppc_deliver_interrupt_generic(env, interrupt);
2575ba2898f7SMatheus Ferst     }
2576ba2898f7SMatheus Ferst }
2577ba2898f7SMatheus Ferst 
2578b5b7f391SNicholas Piggin void ppc_cpu_do_system_reset(CPUState *cs)
257934316482SAlexey Kardashevskiy {
258034316482SAlexey Kardashevskiy     PowerPCCPU *cpu = POWERPC_CPU(cs);
258134316482SAlexey Kardashevskiy 
258293130c84SFabiano Rosas     powerpc_excp(cpu, POWERPC_EXCP_RESET);
258334316482SAlexey Kardashevskiy }
2584ad77c6caSNicholas Piggin 
2585ad77c6caSNicholas Piggin void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector)
2586ad77c6caSNicholas Piggin {
2587ad77c6caSNicholas Piggin     PowerPCCPU *cpu = POWERPC_CPU(cs);
2588ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
2589ad77c6caSNicholas Piggin     target_ulong msr = 0;
2590ad77c6caSNicholas Piggin 
2591ad77c6caSNicholas Piggin     /*
2592ad77c6caSNicholas Piggin      * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already
2593ad77c6caSNicholas Piggin      * been set by KVM.
2594ad77c6caSNicholas Piggin      */
2595ad77c6caSNicholas Piggin     msr = (1ULL << MSR_ME);
2596ad77c6caSNicholas Piggin     msr |= env->msr & (1ULL << MSR_SF);
2597516fc103SFabiano Rosas     if (ppc_interrupts_little_endian(cpu, false)) {
2598ad77c6caSNicholas Piggin         msr |= (1ULL << MSR_LE);
2599ad77c6caSNicholas Piggin     }
2600ad77c6caSNicholas Piggin 
26017cebc5dbSNicholas Piggin     /* Anything for nested required here? MSR[HV] bit? */
26027cebc5dbSNicholas Piggin 
2603ad77c6caSNicholas Piggin     powerpc_set_excp_state(cpu, vector, msr);
2604ad77c6caSNicholas Piggin }
2605c79c73f6SBlue Swirl 
2606458dd766SRichard Henderson bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
2607458dd766SRichard Henderson {
2608458dd766SRichard Henderson     PowerPCCPU *cpu = POWERPC_CPU(cs);
2609458dd766SRichard Henderson     CPUPPCState *env = &cpu->env;
2610de76b85cSMatheus Ferst     int interrupt;
2611458dd766SRichard Henderson 
2612de76b85cSMatheus Ferst     if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) {
2613de76b85cSMatheus Ferst         return false;
2614de76b85cSMatheus Ferst     }
2615de76b85cSMatheus Ferst 
2616de76b85cSMatheus Ferst     interrupt = ppc_next_unmasked_interrupt(env);
2617de76b85cSMatheus Ferst     if (interrupt == 0) {
2618de76b85cSMatheus Ferst         return false;
2619de76b85cSMatheus Ferst     }
2620de76b85cSMatheus Ferst 
2621de76b85cSMatheus Ferst     ppc_deliver_interrupt(env, interrupt);
2622458dd766SRichard Henderson     if (env->pending_interrupts == 0) {
2623de76b85cSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
2624458dd766SRichard Henderson     }
2625458dd766SRichard Henderson     return true;
2626458dd766SRichard Henderson }
2627458dd766SRichard Henderson 
2628f725245cSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
2629f725245cSPhilippe Mathieu-Daudé 
2630ad71ed68SBlue Swirl /*****************************************************************************/
2631ad71ed68SBlue Swirl /* Exceptions processing helpers */
2632ad71ed68SBlue Swirl 
2633db789c6cSBenjamin Herrenschmidt void raise_exception_err_ra(CPUPPCState *env, uint32_t exception,
2634db789c6cSBenjamin Herrenschmidt                             uint32_t error_code, uintptr_t raddr)
2635ad71ed68SBlue Swirl {
2636db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
263727103424SAndreas Färber 
263827103424SAndreas Färber     cs->exception_index = exception;
2639ad71ed68SBlue Swirl     env->error_code = error_code;
2640db789c6cSBenjamin Herrenschmidt     cpu_loop_exit_restore(cs, raddr);
2641db789c6cSBenjamin Herrenschmidt }
2642db789c6cSBenjamin Herrenschmidt 
2643db789c6cSBenjamin Herrenschmidt void raise_exception_err(CPUPPCState *env, uint32_t exception,
2644db789c6cSBenjamin Herrenschmidt                          uint32_t error_code)
2645db789c6cSBenjamin Herrenschmidt {
2646db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2647db789c6cSBenjamin Herrenschmidt }
2648db789c6cSBenjamin Herrenschmidt 
2649db789c6cSBenjamin Herrenschmidt void raise_exception(CPUPPCState *env, uint32_t exception)
2650db789c6cSBenjamin Herrenschmidt {
2651db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2652db789c6cSBenjamin Herrenschmidt }
2653db789c6cSBenjamin Herrenschmidt 
2654db789c6cSBenjamin Herrenschmidt void raise_exception_ra(CPUPPCState *env, uint32_t exception,
2655db789c6cSBenjamin Herrenschmidt                         uintptr_t raddr)
2656db789c6cSBenjamin Herrenschmidt {
2657db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, raddr);
2658db789c6cSBenjamin Herrenschmidt }
2659db789c6cSBenjamin Herrenschmidt 
26602b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2661db789c6cSBenjamin Herrenschmidt void helper_raise_exception_err(CPUPPCState *env, uint32_t exception,
2662db789c6cSBenjamin Herrenschmidt                                 uint32_t error_code)
2663db789c6cSBenjamin Herrenschmidt {
2664db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2665ad71ed68SBlue Swirl }
2666ad71ed68SBlue Swirl 
2667e5f17ac6SBlue Swirl void helper_raise_exception(CPUPPCState *env, uint32_t exception)
2668ad71ed68SBlue Swirl {
2669db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2670ad71ed68SBlue Swirl }
26712b44e219SBruno Larsen (billionai) #endif
2672ad71ed68SBlue Swirl 
2673ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY)
26742b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2675e5f17ac6SBlue Swirl void helper_store_msr(CPUPPCState *env, target_ulong val)
2676ad71ed68SBlue Swirl {
2677db789c6cSBenjamin Herrenschmidt     uint32_t excp = hreg_store_msr(env, val, 0);
2678259186a7SAndreas Färber 
2679db789c6cSBenjamin Herrenschmidt     if (excp != 0) {
26800661329aSBALATON Zoltan         cpu_interrupt_exittb(env_cpu(env));
2681db789c6cSBenjamin Herrenschmidt         raise_exception(env, excp);
2682ad71ed68SBlue Swirl     }
2683ad71ed68SBlue Swirl }
2684ad71ed68SBlue Swirl 
26852fdedcbcSMatheus Ferst void helper_ppc_maybe_interrupt(CPUPPCState *env)
26862fdedcbcSMatheus Ferst {
26872fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
26882fdedcbcSMatheus Ferst }
26892fdedcbcSMatheus Ferst 
26907778a575SBenjamin Herrenschmidt #if defined(TARGET_PPC64)
2691f43520e5SRichard Henderson void helper_scv(CPUPPCState *env, uint32_t lev)
2692f43520e5SRichard Henderson {
2693f43520e5SRichard Henderson     if (env->spr[SPR_FSCR] & (1ull << FSCR_SCV)) {
2694f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_SYSCALL_VECTORED, lev);
2695f43520e5SRichard Henderson     } else {
2696f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_FU, FSCR_IC_SCV);
2697f43520e5SRichard Henderson     }
2698f43520e5SRichard Henderson }
2699f43520e5SRichard Henderson 
270007e4804fSCédric Le Goater void helper_pminsn(CPUPPCState *env, uint32_t insn)
27017778a575SBenjamin Herrenschmidt {
27024e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
27037778a575SBenjamin Herrenschmidt 
27047778a575SBenjamin Herrenschmidt     cs->halted = 1;
27057778a575SBenjamin Herrenschmidt 
27063621e2c9SBenjamin Herrenschmidt     /* Condition for waking up at 0x100 */
27071e7fd61dSBenjamin Herrenschmidt     env->resume_as_sreset = (insn != PPC_PM_STOP) ||
270821c0d66aSBenjamin Herrenschmidt         (env->spr[SPR_PSSCR] & PSSCR_EC);
27092fdedcbcSMatheus Ferst 
27109915dac4SNicholas Piggin     /* HDECR is not to wake from PM state, it may have already fired */
27119915dac4SNicholas Piggin     if (env->resume_as_sreset) {
27129915dac4SNicholas Piggin         PowerPCCPU *cpu = env_archcpu(env);
27139915dac4SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 0);
27149915dac4SNicholas Piggin     }
27159915dac4SNicholas Piggin 
27162fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
27177778a575SBenjamin Herrenschmidt }
27187778a575SBenjamin Herrenschmidt #endif /* defined(TARGET_PPC64) */
27197778a575SBenjamin Herrenschmidt 
272062e79ef9SCédric Le Goater static void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr)
2721ad71ed68SBlue Swirl {
2722a2e71b28SBenjamin Herrenschmidt     /* MSR:POW cannot be set by any form of rfi */
2723a2e71b28SBenjamin Herrenschmidt     msr &= ~(1ULL << MSR_POW);
2724a2e71b28SBenjamin Herrenschmidt 
27255aad0457SChristophe Leroy     /* MSR:TGPR cannot be set by any form of rfi */
27265aad0457SChristophe Leroy     if (env->flags & POWERPC_FLAG_TGPR)
27275aad0457SChristophe Leroy         msr &= ~(1ULL << MSR_TGPR);
27285aad0457SChristophe Leroy 
2729ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2730a2e71b28SBenjamin Herrenschmidt     /* Switching to 32-bit ? Crop the nip */
2731a2e71b28SBenjamin Herrenschmidt     if (!msr_is_64bit(env, msr)) {
2732ad71ed68SBlue Swirl         nip = (uint32_t)nip;
2733ad71ed68SBlue Swirl     }
2734ad71ed68SBlue Swirl #else
2735ad71ed68SBlue Swirl     nip = (uint32_t)nip;
2736ad71ed68SBlue Swirl #endif
2737ad71ed68SBlue Swirl     /* XXX: beware: this is false if VLE is supported */
2738ad71ed68SBlue Swirl     env->nip = nip & ~((target_ulong)0x00000003);
2739ad71ed68SBlue Swirl     hreg_store_msr(env, msr, 1);
27402eb1ef73SCédric Le Goater     trace_ppc_excp_rfi(env->nip, env->msr);
274147733729SDavid Gibson     /*
274247733729SDavid Gibson      * No need to raise an exception here, as rfi is always the last
274347733729SDavid Gibson      * insn of a TB
2744ad71ed68SBlue Swirl      */
27450661329aSBALATON Zoltan     cpu_interrupt_exittb(env_cpu(env));
2746a8b73734SNikunj A Dadhania     /* Reset the reservation */
2747a8b73734SNikunj A Dadhania     env->reserve_addr = -1;
2748a8b73734SNikunj A Dadhania 
2749cd0c6f47SBenjamin Herrenschmidt     /* Context synchronizing: check if TCG TLB needs flush */
2750e3cffe6fSNikunj A Dadhania     check_tlb_flush(env, false);
2751ad71ed68SBlue Swirl }
2752ad71ed68SBlue Swirl 
2753e5f17ac6SBlue Swirl void helper_rfi(CPUPPCState *env)
2754ad71ed68SBlue Swirl {
2755a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful);
2756a1bb7384SScott Wood }
2757ad71ed68SBlue Swirl 
2758ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2759e5f17ac6SBlue Swirl void helper_rfid(CPUPPCState *env)
2760ad71ed68SBlue Swirl {
276147733729SDavid Gibson     /*
2762136fbf65Szhaolichang      * The architecture defines a number of rules for which bits can
276347733729SDavid Gibson      * change but in practice, we handle this in hreg_store_msr()
2764a2e71b28SBenjamin Herrenschmidt      * which will be called by do_rfi(), so there is no need to filter
2765a2e71b28SBenjamin Herrenschmidt      * here
2766a2e71b28SBenjamin Herrenschmidt      */
2767a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]);
2768ad71ed68SBlue Swirl }
2769ad71ed68SBlue Swirl 
27703c89b8d6SNicholas Piggin void helper_rfscv(CPUPPCState *env)
27713c89b8d6SNicholas Piggin {
27723c89b8d6SNicholas Piggin     do_rfi(env, env->lr, env->ctr);
27733c89b8d6SNicholas Piggin }
27743c89b8d6SNicholas Piggin 
2775e5f17ac6SBlue Swirl void helper_hrfid(CPUPPCState *env)
2776ad71ed68SBlue Swirl {
2777a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
2778ad71ed68SBlue Swirl }
2779ad71ed68SBlue Swirl #endif
2780ad71ed68SBlue Swirl 
27811f26c751SDaniel Henrique Barboza #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
27821f26c751SDaniel Henrique Barboza void helper_rfebb(CPUPPCState *env, target_ulong s)
27831f26c751SDaniel Henrique Barboza {
27841f26c751SDaniel Henrique Barboza     target_ulong msr = env->msr;
27851f26c751SDaniel Henrique Barboza 
27861f26c751SDaniel Henrique Barboza     /*
27871f26c751SDaniel Henrique Barboza      * Handling of BESCR bits 32:33 according to PowerISA v3.1:
27881f26c751SDaniel Henrique Barboza      *
27891f26c751SDaniel Henrique Barboza      * "If BESCR 32:33 != 0b00 the instruction is treated as if
27901f26c751SDaniel Henrique Barboza      *  the instruction form were invalid."
27911f26c751SDaniel Henrique Barboza      */
27921f26c751SDaniel Henrique Barboza     if (env->spr[SPR_BESCR] & BESCR_INVALID) {
27931f26c751SDaniel Henrique Barboza         raise_exception_err(env, POWERPC_EXCP_PROGRAM,
27941f26c751SDaniel Henrique Barboza                             POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
27951f26c751SDaniel Henrique Barboza     }
27961f26c751SDaniel Henrique Barboza 
27971f26c751SDaniel Henrique Barboza     env->nip = env->spr[SPR_EBBRR];
27981f26c751SDaniel Henrique Barboza 
27991f26c751SDaniel Henrique Barboza     /* Switching to 32-bit ? Crop the nip */
28001f26c751SDaniel Henrique Barboza     if (!msr_is_64bit(env, msr)) {
28011f26c751SDaniel Henrique Barboza         env->nip = (uint32_t)env->spr[SPR_EBBRR];
28021f26c751SDaniel Henrique Barboza     }
28031f26c751SDaniel Henrique Barboza 
28041f26c751SDaniel Henrique Barboza     if (s) {
28051f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_GE;
28061f26c751SDaniel Henrique Barboza     } else {
28071f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
28081f26c751SDaniel Henrique Barboza     }
28091f26c751SDaniel Henrique Barboza }
2810d3412df2SDaniel Henrique Barboza 
2811d3412df2SDaniel Henrique Barboza /*
2812d3412df2SDaniel Henrique Barboza  * Triggers or queues an 'ebb_excp' EBB exception. All checks
2813d3412df2SDaniel Henrique Barboza  * but FSCR, HFSCR and msr_pr must be done beforehand.
2814d3412df2SDaniel Henrique Barboza  *
2815d3412df2SDaniel Henrique Barboza  * PowerISA v3.1 isn't clear about whether an EBB should be
2816d3412df2SDaniel Henrique Barboza  * postponed or cancelled if the EBB facility is unavailable.
2817d3412df2SDaniel Henrique Barboza  * Our assumption here is that the EBB is cancelled if both
2818d3412df2SDaniel Henrique Barboza  * FSCR and HFSCR EBB facilities aren't available.
2819d3412df2SDaniel Henrique Barboza  */
2820d3412df2SDaniel Henrique Barboza static void do_ebb(CPUPPCState *env, int ebb_excp)
2821d3412df2SDaniel Henrique Barboza {
2822d3412df2SDaniel Henrique Barboza     PowerPCCPU *cpu = env_archcpu(env);
2823d3412df2SDaniel Henrique Barboza 
2824d3412df2SDaniel Henrique Barboza     /*
2825d3412df2SDaniel Henrique Barboza      * FSCR_EBB and FSCR_IC_EBB are the same bits used with
2826d3412df2SDaniel Henrique Barboza      * HFSCR.
2827d3412df2SDaniel Henrique Barboza      */
2828d3412df2SDaniel Henrique Barboza     helper_fscr_facility_check(env, FSCR_EBB, 0, FSCR_IC_EBB);
2829d3412df2SDaniel Henrique Barboza     helper_hfscr_facility_check(env, FSCR_EBB, "EBB", FSCR_IC_EBB);
2830d3412df2SDaniel Henrique Barboza 
2831d3412df2SDaniel Henrique Barboza     if (ebb_excp == POWERPC_EXCP_PERFM_EBB) {
2832d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_PMEO;
2833d3412df2SDaniel Henrique Barboza     } else if (ebb_excp == POWERPC_EXCP_EXTERNAL_EBB) {
2834d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_EEO;
2835d3412df2SDaniel Henrique Barboza     }
2836d3412df2SDaniel Henrique Barboza 
2837d41ccf6eSVíctor Colombo     if (FIELD_EX64(env->msr, MSR, PR)) {
2838d3412df2SDaniel Henrique Barboza         powerpc_excp(cpu, ebb_excp);
2839d3412df2SDaniel Henrique Barboza     } else {
28407b694df6SMatheus Ferst         ppc_set_irq(cpu, PPC_INTERRUPT_EBB, 1);
2841d3412df2SDaniel Henrique Barboza     }
2842d3412df2SDaniel Henrique Barboza }
2843d3412df2SDaniel Henrique Barboza 
2844d3412df2SDaniel Henrique Barboza void raise_ebb_perfm_exception(CPUPPCState *env)
2845d3412df2SDaniel Henrique Barboza {
2846d3412df2SDaniel Henrique Barboza     bool perfm_ebb_enabled = env->spr[SPR_POWER_MMCR0] & MMCR0_EBE &&
2847d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_PME &&
2848d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_GE;
2849d3412df2SDaniel Henrique Barboza 
2850d3412df2SDaniel Henrique Barboza     if (!perfm_ebb_enabled) {
2851d3412df2SDaniel Henrique Barboza         return;
2852d3412df2SDaniel Henrique Barboza     }
2853d3412df2SDaniel Henrique Barboza 
2854d3412df2SDaniel Henrique Barboza     do_ebb(env, POWERPC_EXCP_PERFM_EBB);
2855d3412df2SDaniel Henrique Barboza }
28561f26c751SDaniel Henrique Barboza #endif
28571f26c751SDaniel Henrique Barboza 
2858ad71ed68SBlue Swirl /*****************************************************************************/
2859ad71ed68SBlue Swirl /* Embedded PowerPC specific helpers */
2860e5f17ac6SBlue Swirl void helper_40x_rfci(CPUPPCState *env)
2861ad71ed68SBlue Swirl {
2862a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]);
2863ad71ed68SBlue Swirl }
2864ad71ed68SBlue Swirl 
2865e5f17ac6SBlue Swirl void helper_rfci(CPUPPCState *env)
2866ad71ed68SBlue Swirl {
2867a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]);
2868ad71ed68SBlue Swirl }
2869ad71ed68SBlue Swirl 
2870e5f17ac6SBlue Swirl void helper_rfdi(CPUPPCState *env)
2871ad71ed68SBlue Swirl {
2872a1bb7384SScott Wood     /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
2873a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]);
2874ad71ed68SBlue Swirl }
2875ad71ed68SBlue Swirl 
2876e5f17ac6SBlue Swirl void helper_rfmci(CPUPPCState *env)
2877ad71ed68SBlue Swirl {
2878a1bb7384SScott Wood     /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
2879a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
2880ad71ed68SBlue Swirl }
28812b44e219SBruno Larsen (billionai) #endif /* CONFIG_TCG */
28822b44e219SBruno Larsen (billionai) #endif /* !defined(CONFIG_USER_ONLY) */
2883ad71ed68SBlue Swirl 
28842b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2885e5f17ac6SBlue Swirl void helper_tw(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2886e5f17ac6SBlue Swirl                uint32_t flags)
2887ad71ed68SBlue Swirl {
2888ad71ed68SBlue Swirl     if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
2889ad71ed68SBlue Swirl                   ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
2890ad71ed68SBlue Swirl                   ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
2891ad71ed68SBlue Swirl                   ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
2892ad71ed68SBlue Swirl                   ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
289372073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
289472073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2895ad71ed68SBlue Swirl     }
2896ad71ed68SBlue Swirl }
2897ad71ed68SBlue Swirl 
2898ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2899e5f17ac6SBlue Swirl void helper_td(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2900e5f17ac6SBlue Swirl                uint32_t flags)
2901ad71ed68SBlue Swirl {
2902ad71ed68SBlue Swirl     if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
2903ad71ed68SBlue Swirl                   ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
2904ad71ed68SBlue Swirl                   ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
2905ad71ed68SBlue Swirl                   ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
2906ad71ed68SBlue Swirl                   ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) {
290772073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
290872073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2909ad71ed68SBlue Swirl     }
2910ad71ed68SBlue Swirl }
2911ad71ed68SBlue Swirl #endif
29122b44e219SBruno Larsen (billionai) #endif
2913ad71ed68SBlue Swirl 
2914049b4ad6SVaibhav Jain #ifdef CONFIG_TCG
2915670f1da3SVíctor Colombo static uint32_t helper_SIMON_LIKE_32_64(uint32_t x, uint64_t key, uint32_t lane)
2916670f1da3SVíctor Colombo {
2917670f1da3SVíctor Colombo     const uint16_t c = 0xfffc;
2918670f1da3SVíctor Colombo     const uint64_t z0 = 0xfa2561cdf44ac398ULL;
2919670f1da3SVíctor Colombo     uint16_t z = 0, temp;
2920670f1da3SVíctor Colombo     uint16_t k[32], eff_k[32], xleft[33], xright[33], fxleft[32];
2921670f1da3SVíctor Colombo 
2922670f1da3SVíctor Colombo     for (int i = 3; i >= 0; i--) {
2923670f1da3SVíctor Colombo         k[i] = key & 0xffff;
2924670f1da3SVíctor Colombo         key >>= 16;
2925670f1da3SVíctor Colombo     }
2926670f1da3SVíctor Colombo     xleft[0] = x & 0xffff;
2927670f1da3SVíctor Colombo     xright[0] = (x >> 16) & 0xffff;
2928670f1da3SVíctor Colombo 
2929670f1da3SVíctor Colombo     for (int i = 0; i < 28; i++) {
2930670f1da3SVíctor Colombo         z = (z0 >> (63 - i)) & 1;
2931670f1da3SVíctor Colombo         temp = ror16(k[i + 3], 3) ^ k[i + 1];
2932670f1da3SVíctor Colombo         k[i + 4] = c ^ z ^ k[i] ^ temp ^ ror16(temp, 1);
2933670f1da3SVíctor Colombo     }
2934670f1da3SVíctor Colombo 
2935670f1da3SVíctor Colombo     for (int i = 0; i < 8; i++) {
2936670f1da3SVíctor Colombo         eff_k[4 * i + 0] = k[4 * i + ((0 + lane) % 4)];
2937670f1da3SVíctor Colombo         eff_k[4 * i + 1] = k[4 * i + ((1 + lane) % 4)];
2938670f1da3SVíctor Colombo         eff_k[4 * i + 2] = k[4 * i + ((2 + lane) % 4)];
2939670f1da3SVíctor Colombo         eff_k[4 * i + 3] = k[4 * i + ((3 + lane) % 4)];
2940670f1da3SVíctor Colombo     }
2941670f1da3SVíctor Colombo 
2942670f1da3SVíctor Colombo     for (int i = 0; i < 32; i++) {
2943670f1da3SVíctor Colombo         fxleft[i] = (rol16(xleft[i], 1) &
2944670f1da3SVíctor Colombo             rol16(xleft[i], 8)) ^ rol16(xleft[i], 2);
2945670f1da3SVíctor Colombo         xleft[i + 1] = xright[i] ^ fxleft[i] ^ eff_k[i];
2946670f1da3SVíctor Colombo         xright[i + 1] = xleft[i];
2947670f1da3SVíctor Colombo     }
2948670f1da3SVíctor Colombo 
2949670f1da3SVíctor Colombo     return (((uint32_t)xright[32]) << 16) | xleft[32];
2950670f1da3SVíctor Colombo }
2951670f1da3SVíctor Colombo 
2952670f1da3SVíctor Colombo static uint64_t hash_digest(uint64_t ra, uint64_t rb, uint64_t key)
2953670f1da3SVíctor Colombo {
2954670f1da3SVíctor Colombo     uint64_t stage0_h = 0ULL, stage0_l = 0ULL;
2955670f1da3SVíctor Colombo     uint64_t stage1_h, stage1_l;
2956670f1da3SVíctor Colombo 
2957670f1da3SVíctor Colombo     for (int i = 0; i < 4; i++) {
2958670f1da3SVíctor Colombo         stage0_h |= ror64(rb & 0xff, 8 * (2 * i + 1));
2959670f1da3SVíctor Colombo         stage0_h |= ((ra >> 32) & 0xff) << (8 * 2 * i);
2960670f1da3SVíctor Colombo         stage0_l |= ror64((rb >> 32) & 0xff, 8 * (2 * i + 1));
2961670f1da3SVíctor Colombo         stage0_l |= (ra & 0xff) << (8 * 2 * i);
2962670f1da3SVíctor Colombo         rb >>= 8;
2963670f1da3SVíctor Colombo         ra >>= 8;
2964670f1da3SVíctor Colombo     }
2965670f1da3SVíctor Colombo 
2966670f1da3SVíctor Colombo     stage1_h = (uint64_t)helper_SIMON_LIKE_32_64(stage0_h >> 32, key, 0) << 32;
2967670f1da3SVíctor Colombo     stage1_h |= helper_SIMON_LIKE_32_64(stage0_h, key, 1);
2968670f1da3SVíctor Colombo     stage1_l = (uint64_t)helper_SIMON_LIKE_32_64(stage0_l >> 32, key, 2) << 32;
2969670f1da3SVíctor Colombo     stage1_l |= helper_SIMON_LIKE_32_64(stage0_l, key, 3);
2970670f1da3SVíctor Colombo 
2971670f1da3SVíctor Colombo     return stage1_h ^ stage1_l;
2972670f1da3SVíctor Colombo }
2973670f1da3SVíctor Colombo 
29744091fabfSNicholas Miehlbradt static void do_hash(CPUPPCState *env, target_ulong ea, target_ulong ra,
29754091fabfSNicholas Miehlbradt                     target_ulong rb, uint64_t key, bool store)
29764091fabfSNicholas Miehlbradt {
29774091fabfSNicholas Miehlbradt     uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash;
29784091fabfSNicholas Miehlbradt 
29794091fabfSNicholas Miehlbradt     if (store) {
29804091fabfSNicholas Miehlbradt         cpu_stq_data_ra(env, ea, calculated_hash, GETPC());
29814091fabfSNicholas Miehlbradt     } else {
29824091fabfSNicholas Miehlbradt         loaded_hash = cpu_ldq_data_ra(env, ea, GETPC());
29834091fabfSNicholas Miehlbradt         if (loaded_hash != calculated_hash) {
29844091fabfSNicholas Miehlbradt             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
29854091fabfSNicholas Miehlbradt                 POWERPC_EXCP_TRAP, GETPC());
29864091fabfSNicholas Miehlbradt         }
29874091fabfSNicholas Miehlbradt     }
29884091fabfSNicholas Miehlbradt }
29894091fabfSNicholas Miehlbradt 
2990670f1da3SVíctor Colombo #include "qemu/guest-random.h"
2991670f1da3SVíctor Colombo 
29924091fabfSNicholas Miehlbradt #ifdef TARGET_PPC64
29934091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect)                             \
2994670f1da3SVíctor Colombo void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
2995670f1da3SVíctor Colombo                  target_ulong rb)                                             \
2996670f1da3SVíctor Colombo {                                                                             \
29974091fabfSNicholas Miehlbradt     if (env->msr & R_MSR_PR_MASK) {                                           \
29984091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_DEXCR] & R_DEXCR_PRO_##dexcr_aspect##_MASK ||      \
29994091fabfSNicholas Miehlbradt             env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK))       \
30004091fabfSNicholas Miehlbradt             return;                                                           \
30014091fabfSNicholas Miehlbradt     } else if (!(env->msr & R_MSR_HV_MASK)) {                                 \
30024091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_DEXCR] & R_DEXCR_PNH_##dexcr_aspect##_MASK ||      \
30034091fabfSNicholas Miehlbradt             env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK))       \
30044091fabfSNicholas Miehlbradt             return;                                                           \
30054091fabfSNicholas Miehlbradt     } else if (!(env->msr & R_MSR_S_MASK)) {                                  \
30064091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_HDEXCR] & R_HDEXCR_HNU_##dexcr_aspect##_MASK))     \
30074091fabfSNicholas Miehlbradt             return;                                                           \
30084091fabfSNicholas Miehlbradt     }                                                                         \
3009670f1da3SVíctor Colombo                                                                               \
30104091fabfSNicholas Miehlbradt     do_hash(env, ea, ra, rb, key, store);                                     \
3011670f1da3SVíctor Colombo }
30124091fabfSNicholas Miehlbradt #else
30134091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect)                             \
30144091fabfSNicholas Miehlbradt void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
30154091fabfSNicholas Miehlbradt                  target_ulong rb)                                             \
30164091fabfSNicholas Miehlbradt {                                                                             \
30174091fabfSNicholas Miehlbradt     do_hash(env, ea, ra, rb, key, store);                                     \
30184091fabfSNicholas Miehlbradt }
30194091fabfSNicholas Miehlbradt #endif /* TARGET_PPC64 */
3020670f1da3SVíctor Colombo 
30214091fabfSNicholas Miehlbradt HELPER_HASH(HASHST, env->spr[SPR_HASHKEYR], true, NPHIE)
30224091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHK, env->spr[SPR_HASHKEYR], false, NPHIE)
30234091fabfSNicholas Miehlbradt HELPER_HASH(HASHSTP, env->spr[SPR_HASHPKEYR], true, PHIE)
30244091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHKP, env->spr[SPR_HASHPKEYR], false, PHIE)
3025049b4ad6SVaibhav Jain #endif /* CONFIG_TCG */
3026670f1da3SVíctor Colombo 
3027ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY)
3028ad71ed68SBlue Swirl 
30292b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
3030ad71ed68SBlue Swirl 
3031ad71ed68SBlue Swirl /* Embedded.Processor Control */
3032ad71ed68SBlue Swirl static int dbell2irq(target_ulong rb)
3033ad71ed68SBlue Swirl {
3034ad71ed68SBlue Swirl     int msg = rb & DBELL_TYPE_MASK;
3035ad71ed68SBlue Swirl     int irq = -1;
3036ad71ed68SBlue Swirl 
3037ad71ed68SBlue Swirl     switch (msg) {
3038ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL:
3039ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_DOORBELL;
3040ad71ed68SBlue Swirl         break;
3041ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL_CRIT:
3042ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_CDOORBELL;
3043ad71ed68SBlue Swirl         break;
3044ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL:
3045ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_CRIT:
3046ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_MC:
3047ad71ed68SBlue Swirl         /* XXX implement */
3048ad71ed68SBlue Swirl     default:
3049ad71ed68SBlue Swirl         break;
3050ad71ed68SBlue Swirl     }
3051ad71ed68SBlue Swirl 
3052ad71ed68SBlue Swirl     return irq;
3053ad71ed68SBlue Swirl }
3054ad71ed68SBlue Swirl 
3055e5f17ac6SBlue Swirl void helper_msgclr(CPUPPCState *env, target_ulong rb)
3056ad71ed68SBlue Swirl {
3057ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
3058ad71ed68SBlue Swirl 
3059ad71ed68SBlue Swirl     if (irq < 0) {
3060ad71ed68SBlue Swirl         return;
3061ad71ed68SBlue Swirl     }
3062ad71ed68SBlue Swirl 
30637b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), irq, 0);
3064ad71ed68SBlue Swirl }
3065ad71ed68SBlue Swirl 
3066ad71ed68SBlue Swirl void helper_msgsnd(target_ulong rb)
3067ad71ed68SBlue Swirl {
3068ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
3069ad71ed68SBlue Swirl     int pir = rb & DBELL_PIRTAG_MASK;
3070182735efSAndreas Färber     CPUState *cs;
3071ad71ed68SBlue Swirl 
3072ad71ed68SBlue Swirl     if (irq < 0) {
3073ad71ed68SBlue Swirl         return;
3074ad71ed68SBlue Swirl     }
3075ad71ed68SBlue Swirl 
3076195801d7SStefan Hajnoczi     bql_lock();
3077bdc44640SAndreas Färber     CPU_FOREACH(cs) {
3078182735efSAndreas Färber         PowerPCCPU *cpu = POWERPC_CPU(cs);
3079182735efSAndreas Färber         CPUPPCState *cenv = &cpu->env;
3080182735efSAndreas Färber 
3081ad71ed68SBlue Swirl         if ((rb & DBELL_BRDCAST) || (cenv->spr[SPR_BOOKE_PIR] == pir)) {
30827b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
3083ad71ed68SBlue Swirl         }
3084ad71ed68SBlue Swirl     }
3085195801d7SStefan Hajnoczi     bql_unlock();
3086ad71ed68SBlue Swirl }
30877af1e7b0SCédric Le Goater 
30887af1e7b0SCédric Le Goater /* Server Processor Control */
30897af1e7b0SCédric Le Goater 
30905ba7ba1dSCédric Le Goater static bool dbell_type_server(target_ulong rb)
30915ba7ba1dSCédric Le Goater {
309247733729SDavid Gibson     /*
309347733729SDavid Gibson      * A Directed Hypervisor Doorbell message is sent only if the
30947af1e7b0SCédric Le Goater      * message type is 5. All other types are reserved and the
309547733729SDavid Gibson      * instruction is a no-op
309647733729SDavid Gibson      */
30975ba7ba1dSCédric Le Goater     return (rb & DBELL_TYPE_MASK) == DBELL_TYPE_DBELL_SERVER;
30987af1e7b0SCédric Le Goater }
30997af1e7b0SCédric Le Goater 
31007af1e7b0SCédric Le Goater void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb)
31017af1e7b0SCédric Le Goater {
31025ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
31037af1e7b0SCédric Le Goater         return;
31047af1e7b0SCédric Le Goater     }
31057af1e7b0SCédric Le Goater 
31067b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
31077af1e7b0SCédric Le Goater }
31087af1e7b0SCédric Le Goater 
31095ba7ba1dSCédric Le Goater static void book3s_msgsnd_common(int pir, int irq)
31107af1e7b0SCédric Le Goater {
31117af1e7b0SCédric Le Goater     CPUState *cs;
31127af1e7b0SCédric Le Goater 
3113195801d7SStefan Hajnoczi     bql_lock();
31147af1e7b0SCédric Le Goater     CPU_FOREACH(cs) {
31157af1e7b0SCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
31167af1e7b0SCédric Le Goater         CPUPPCState *cenv = &cpu->env;
31177af1e7b0SCédric Le Goater 
31187af1e7b0SCédric Le Goater         /* TODO: broadcast message to all threads of the same  processor */
31197af1e7b0SCédric Le Goater         if (cenv->spr_cb[SPR_PIR].default_value == pir) {
31207b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
31217af1e7b0SCédric Le Goater         }
31227af1e7b0SCédric Le Goater     }
3123195801d7SStefan Hajnoczi     bql_unlock();
31247af1e7b0SCédric Le Goater }
31255ba7ba1dSCédric Le Goater 
31265ba7ba1dSCédric Le Goater void helper_book3s_msgsnd(target_ulong rb)
31275ba7ba1dSCédric Le Goater {
31285ba7ba1dSCédric Le Goater     int pir = rb & DBELL_PROCIDTAG_MASK;
31295ba7ba1dSCédric Le Goater 
31305ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
31315ba7ba1dSCédric Le Goater         return;
31325ba7ba1dSCédric Le Goater     }
31335ba7ba1dSCédric Le Goater 
31345ba7ba1dSCédric Le Goater     book3s_msgsnd_common(pir, PPC_INTERRUPT_HDOORBELL);
31355ba7ba1dSCédric Le Goater }
31365ba7ba1dSCédric Le Goater 
31375ba7ba1dSCédric Le Goater #if defined(TARGET_PPC64)
31385ba7ba1dSCédric Le Goater void helper_book3s_msgclrp(CPUPPCState *env, target_ulong rb)
31395ba7ba1dSCédric Le Goater {
3140493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgclrp", HFSCR_IC_MSGP);
3141493028d8SCédric Le Goater 
31425ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
31435ba7ba1dSCédric Le Goater         return;
31445ba7ba1dSCédric Le Goater     }
31455ba7ba1dSCédric Le Goater 
31462e985555SNicholas Piggin     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_DOORBELL, 0);
31475ba7ba1dSCédric Le Goater }
31485ba7ba1dSCédric Le Goater 
31495ba7ba1dSCédric Le Goater /*
3150d24e80b2SNicholas Piggin  * sends a message to another thread  on the same
31515ba7ba1dSCédric Le Goater  * multi-threaded processor
31525ba7ba1dSCédric Le Goater  */
31535ba7ba1dSCédric Le Goater void helper_book3s_msgsndp(CPUPPCState *env, target_ulong rb)
31545ba7ba1dSCédric Le Goater {
3155d24e80b2SNicholas Piggin     CPUState *cs = env_cpu(env);
3156b0a13333SPhilippe Mathieu-Daudé     PowerPCCPU *cpu = env_archcpu(env);
3157d24e80b2SNicholas Piggin     CPUState *ccs;
3158d24e80b2SNicholas Piggin     uint32_t nr_threads = cs->nr_threads;
3159d24e80b2SNicholas Piggin     int ttir = rb & PPC_BITMASK(57, 63);
31605ba7ba1dSCédric Le Goater 
3161493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgsndp", HFSCR_IC_MSGP);
3162493028d8SCédric Le Goater 
31633401ea3cSNicholas Piggin     if (!(env->flags & POWERPC_FLAG_SMT_1LPAR)) {
31643401ea3cSNicholas Piggin         nr_threads = 1; /* msgsndp behaves as 1-thread in LPAR-per-thread mode*/
31653401ea3cSNicholas Piggin     }
31663401ea3cSNicholas Piggin 
3167d24e80b2SNicholas Piggin     if (!dbell_type_server(rb) || ttir >= nr_threads) {
31685ba7ba1dSCédric Le Goater         return;
31695ba7ba1dSCédric Le Goater     }
31705ba7ba1dSCédric Le Goater 
3171d24e80b2SNicholas Piggin     if (nr_threads == 1) {
3172d24e80b2SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, 1);
3173d24e80b2SNicholas Piggin         return;
3174d24e80b2SNicholas Piggin     }
31755ba7ba1dSCédric Le Goater 
3176d24e80b2SNicholas Piggin     /* Does iothread need to be locked for walking CPU list? */
3177195801d7SStefan Hajnoczi     bql_lock();
3178d24e80b2SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
3179d24e80b2SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
3180d24e80b2SNicholas Piggin         uint32_t thread_id = ppc_cpu_tir(ccpu);
3181d24e80b2SNicholas Piggin 
3182d24e80b2SNicholas Piggin         if (ttir == thread_id) {
3183d24e80b2SNicholas Piggin             ppc_set_irq(ccpu, PPC_INTERRUPT_DOORBELL, 1);
3184195801d7SStefan Hajnoczi             bql_unlock();
3185d24e80b2SNicholas Piggin             return;
3186d24e80b2SNicholas Piggin         }
3187d24e80b2SNicholas Piggin     }
3188d24e80b2SNicholas Piggin 
3189d24e80b2SNicholas Piggin     g_assert_not_reached();
31905ba7ba1dSCédric Le Goater }
3191996473e4SRichard Henderson #endif /* TARGET_PPC64 */
31920f3110faSRichard Henderson 
319314895384SNicholas Piggin /* Single-step tracing */
319414895384SNicholas Piggin void helper_book3s_trace(CPUPPCState *env, target_ulong prev_ip)
319514895384SNicholas Piggin {
319614895384SNicholas Piggin     uint32_t error_code = 0;
319714895384SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
319814895384SNicholas Piggin         /* Load/store reporting, SRR1[35, 36] and SDAR, are not implemented. */
319914895384SNicholas Piggin         env->spr[SPR_POWER_SIAR] = prev_ip;
320014895384SNicholas Piggin         error_code = PPC_BIT(33);
320114895384SNicholas Piggin     }
320214895384SNicholas Piggin     raise_exception_err(env, POWERPC_EXCP_TRACE, error_code);
320314895384SNicholas Piggin }
320414895384SNicholas Piggin 
32050f3110faSRichard Henderson void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
32060f3110faSRichard Henderson                                  MMUAccessType access_type,
32070f3110faSRichard Henderson                                  int mmu_idx, uintptr_t retaddr)
32080f3110faSRichard Henderson {
3209b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
321029c4a336SFabiano Rosas     uint32_t insn;
321129c4a336SFabiano Rosas 
321229c4a336SFabiano Rosas     /* Restore state and reload the insn we executed, for filling in DSISR.  */
32133d419a4dSRichard Henderson     cpu_restore_state(cs, retaddr);
3214888050cfSNicholas Piggin     insn = ppc_ldl_code(env, env->nip);
32150f3110faSRichard Henderson 
3216a7e3af13SRichard Henderson     switch (env->mmu_model) {
3217a7e3af13SRichard Henderson     case POWERPC_MMU_SOFT_4xx:
3218a7e3af13SRichard Henderson         env->spr[SPR_40x_DEAR] = vaddr;
3219a7e3af13SRichard Henderson         break;
3220a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE:
3221a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE206:
3222a7e3af13SRichard Henderson         env->spr[SPR_BOOKE_DEAR] = vaddr;
3223a7e3af13SRichard Henderson         break;
3224a7e3af13SRichard Henderson     default:
3225a7e3af13SRichard Henderson         env->spr[SPR_DAR] = vaddr;
3226a7e3af13SRichard Henderson         break;
3227a7e3af13SRichard Henderson     }
3228a7e3af13SRichard Henderson 
32290f3110faSRichard Henderson     cs->exception_index = POWERPC_EXCP_ALIGN;
323029c4a336SFabiano Rosas     env->error_code = insn & 0x03FF0000;
323129c4a336SFabiano Rosas     cpu_loop_exit(cs);
32320f3110faSRichard Henderson }
323355a7fa34SNicholas Piggin 
323455a7fa34SNicholas Piggin void ppc_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
323555a7fa34SNicholas Piggin                                    vaddr vaddr, unsigned size,
323655a7fa34SNicholas Piggin                                    MMUAccessType access_type,
323755a7fa34SNicholas Piggin                                    int mmu_idx, MemTxAttrs attrs,
323855a7fa34SNicholas Piggin                                    MemTxResult response, uintptr_t retaddr)
323955a7fa34SNicholas Piggin {
3240b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
324155a7fa34SNicholas Piggin 
324255a7fa34SNicholas Piggin     switch (env->excp_model) {
324355a7fa34SNicholas Piggin #if defined(TARGET_PPC64)
3244c8fd9667SNicholas Piggin     case POWERPC_EXCP_POWER8:
324555a7fa34SNicholas Piggin     case POWERPC_EXCP_POWER9:
324655a7fa34SNicholas Piggin     case POWERPC_EXCP_POWER10:
324755a7fa34SNicholas Piggin         /*
324855a7fa34SNicholas Piggin          * Machine check codes can be found in processor User Manual or
324955a7fa34SNicholas Piggin          * Linux or skiboot source.
325055a7fa34SNicholas Piggin          */
325155a7fa34SNicholas Piggin         if (access_type == MMU_DATA_LOAD) {
325255a7fa34SNicholas Piggin             env->spr[SPR_DAR] = vaddr;
325355a7fa34SNicholas Piggin             env->spr[SPR_DSISR] = PPC_BIT(57);
325455a7fa34SNicholas Piggin             env->error_code = PPC_BIT(42);
325555a7fa34SNicholas Piggin 
325655a7fa34SNicholas Piggin         } else if (access_type == MMU_DATA_STORE) {
325755a7fa34SNicholas Piggin             /*
325855a7fa34SNicholas Piggin              * MCE for stores in POWER is asynchronous so hardware does
325955a7fa34SNicholas Piggin              * not set DAR, but QEMU can do better.
326055a7fa34SNicholas Piggin              */
326155a7fa34SNicholas Piggin             env->spr[SPR_DAR] = vaddr;
326255a7fa34SNicholas Piggin             env->error_code = PPC_BIT(36) | PPC_BIT(43) | PPC_BIT(45);
326355a7fa34SNicholas Piggin             env->error_code |= PPC_BIT(42);
326455a7fa34SNicholas Piggin 
326555a7fa34SNicholas Piggin         } else { /* Fetch */
3266c8fd9667SNicholas Piggin             /*
3267c8fd9667SNicholas Piggin              * is_prefix_insn_excp() tests !PPC_BIT(42) to avoid fetching
3268c8fd9667SNicholas Piggin              * the instruction, so that must always be clear for fetches.
3269c8fd9667SNicholas Piggin              */
327055a7fa34SNicholas Piggin             env->error_code = PPC_BIT(36) | PPC_BIT(44) | PPC_BIT(45);
327155a7fa34SNicholas Piggin         }
327255a7fa34SNicholas Piggin         break;
327355a7fa34SNicholas Piggin #endif
327455a7fa34SNicholas Piggin     default:
327555a7fa34SNicholas Piggin         /*
327655a7fa34SNicholas Piggin          * TODO: Check behaviour for other CPUs, for now do nothing.
327755a7fa34SNicholas Piggin          * Could add a basic MCE even if real hardware ignores.
327855a7fa34SNicholas Piggin          */
327955a7fa34SNicholas Piggin         return;
328055a7fa34SNicholas Piggin     }
328155a7fa34SNicholas Piggin 
328255a7fa34SNicholas Piggin     cs->exception_index = POWERPC_EXCP_MCHECK;
328355a7fa34SNicholas Piggin     cpu_loop_exit_restore(cs, retaddr);
328455a7fa34SNicholas Piggin }
328514192307SNicholas Piggin 
328614192307SNicholas Piggin void ppc_cpu_debug_excp_handler(CPUState *cs)
328714192307SNicholas Piggin {
328814192307SNicholas Piggin #if defined(TARGET_PPC64)
3289b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
329014192307SNicholas Piggin 
329114192307SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
3292d5ee641cSNicholas Piggin         if (cs->watchpoint_hit) {
3293d5ee641cSNicholas Piggin             if (cs->watchpoint_hit->flags & BP_CPU) {
3294d5ee641cSNicholas Piggin                 env->spr[SPR_DAR] = cs->watchpoint_hit->hitaddr;
3295d5ee641cSNicholas Piggin                 env->spr[SPR_DSISR] = PPC_BIT(41);
3296d5ee641cSNicholas Piggin                 cs->watchpoint_hit = NULL;
3297d5ee641cSNicholas Piggin                 raise_exception(env, POWERPC_EXCP_DSI);
3298d5ee641cSNicholas Piggin             }
3299d5ee641cSNicholas Piggin             cs->watchpoint_hit = NULL;
3300d5ee641cSNicholas Piggin         } else if (cpu_breakpoint_test(cs, env->nip, BP_CPU)) {
330114192307SNicholas Piggin             raise_exception_err(env, POWERPC_EXCP_TRACE,
330214192307SNicholas Piggin                                 PPC_BIT(33) | PPC_BIT(43));
330314192307SNicholas Piggin         }
330414192307SNicholas Piggin     }
330514192307SNicholas Piggin #endif
330614192307SNicholas Piggin }
330714192307SNicholas Piggin 
330814192307SNicholas Piggin bool ppc_cpu_debug_check_breakpoint(CPUState *cs)
330914192307SNicholas Piggin {
331014192307SNicholas Piggin #if defined(TARGET_PPC64)
3311b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
331214192307SNicholas Piggin 
331314192307SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
331414192307SNicholas Piggin         target_ulong priv;
331514192307SNicholas Piggin 
331614192307SNicholas Piggin         priv = env->spr[SPR_CIABR] & PPC_BITMASK(62, 63);
331714192307SNicholas Piggin         switch (priv) {
331814192307SNicholas Piggin         case 0x1: /* problem */
331914192307SNicholas Piggin             return env->msr & ((target_ulong)1 << MSR_PR);
332014192307SNicholas Piggin         case 0x2: /* supervisor */
332114192307SNicholas Piggin             return (!(env->msr & ((target_ulong)1 << MSR_PR)) &&
332214192307SNicholas Piggin                     !(env->msr & ((target_ulong)1 << MSR_HV)));
332314192307SNicholas Piggin         case 0x3: /* hypervisor */
332414192307SNicholas Piggin             return (!(env->msr & ((target_ulong)1 << MSR_PR)) &&
332514192307SNicholas Piggin                      (env->msr & ((target_ulong)1 << MSR_HV)));
332614192307SNicholas Piggin         default:
332714192307SNicholas Piggin             g_assert_not_reached();
332814192307SNicholas Piggin         }
332914192307SNicholas Piggin     }
333014192307SNicholas Piggin #endif
333114192307SNicholas Piggin 
333214192307SNicholas Piggin     return false;
333314192307SNicholas Piggin }
333414192307SNicholas Piggin 
3335d5ee641cSNicholas Piggin bool ppc_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
3336d5ee641cSNicholas Piggin {
3337d5ee641cSNicholas Piggin #if defined(TARGET_PPC64)
3338b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
3339d5ee641cSNicholas Piggin 
3340d5ee641cSNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
3341d5ee641cSNicholas Piggin         if (wp == env->dawr0_watchpoint) {
3342d5ee641cSNicholas Piggin             uint32_t dawrx = env->spr[SPR_DAWRX0];
3343d5ee641cSNicholas Piggin             bool wt = extract32(dawrx, PPC_BIT_NR(59), 1);
3344d5ee641cSNicholas Piggin             bool wti = extract32(dawrx, PPC_BIT_NR(60), 1);
3345d5ee641cSNicholas Piggin             bool hv = extract32(dawrx, PPC_BIT_NR(61), 1);
3346d5ee641cSNicholas Piggin             bool sv = extract32(dawrx, PPC_BIT_NR(62), 1);
3347d5ee641cSNicholas Piggin             bool pr = extract32(dawrx, PPC_BIT_NR(62), 1);
3348d5ee641cSNicholas Piggin 
3349d5ee641cSNicholas Piggin             if ((env->msr & ((target_ulong)1 << MSR_PR)) && !pr) {
3350d5ee641cSNicholas Piggin                 return false;
3351d5ee641cSNicholas Piggin             } else if ((env->msr & ((target_ulong)1 << MSR_HV)) && !hv) {
3352d5ee641cSNicholas Piggin                 return false;
3353d5ee641cSNicholas Piggin             } else if (!sv) {
3354d5ee641cSNicholas Piggin                 return false;
3355d5ee641cSNicholas Piggin             }
3356d5ee641cSNicholas Piggin 
3357d5ee641cSNicholas Piggin             if (!wti) {
3358d5ee641cSNicholas Piggin                 if (env->msr & ((target_ulong)1 << MSR_DR)) {
3359d5ee641cSNicholas Piggin                     if (!wt) {
3360d5ee641cSNicholas Piggin                         return false;
3361d5ee641cSNicholas Piggin                     }
3362d5ee641cSNicholas Piggin                 } else {
3363d5ee641cSNicholas Piggin                     if (wt) {
3364d5ee641cSNicholas Piggin                         return false;
3365d5ee641cSNicholas Piggin                     }
3366d5ee641cSNicholas Piggin                 }
3367d5ee641cSNicholas Piggin             }
3368d5ee641cSNicholas Piggin 
3369d5ee641cSNicholas Piggin             return true;
3370d5ee641cSNicholas Piggin         }
3371d5ee641cSNicholas Piggin     }
3372d5ee641cSNicholas Piggin #endif
3373d5ee641cSNicholas Piggin 
3374d5ee641cSNicholas Piggin     return false;
3375d5ee641cSNicholas Piggin }
3376d5ee641cSNicholas Piggin 
3377996473e4SRichard Henderson #endif /* CONFIG_TCG */
3378996473e4SRichard Henderson #endif /* !CONFIG_USER_ONLY */
3379