xref: /qemu/target/ppc/excp_helper.c (revision 37e62398ab1deb5cbf425cca1d43c85f0fcb986c)
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"
22cce7aee8SNicholas Piggin #include "sysemu/sysemu.h"
23cce7aee8SNicholas Piggin #include "sysemu/runstate.h"
24ad71ed68SBlue Swirl #include "cpu.h"
2563c91552SPaolo Bonzini #include "exec/exec-all.h"
260f3110faSRichard Henderson #include "internal.h"
27ad71ed68SBlue Swirl #include "helper_regs.h"
287b694df6SMatheus Ferst #include "hw/ppc/ppc.h"
29ad71ed68SBlue Swirl 
302eb1ef73SCédric Le Goater #include "trace.h"
312eb1ef73SCédric Le Goater 
322b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
335a5d3b23SNicholas Piggin #include "sysemu/tcg.h"
342b44e219SBruno Larsen (billionai) #include "exec/helper-proto.h"
352b44e219SBruno Larsen (billionai) #include "exec/cpu_ldst.h"
362b44e219SBruno Larsen (billionai) #endif
372b44e219SBruno Larsen (billionai) 
38c79c73f6SBlue Swirl /*****************************************************************************/
39c79c73f6SBlue Swirl /* Exception processing */
40f6c2d68bSBALATON Zoltan #ifndef CONFIG_USER_ONLY
4197a8ea5aSAndreas Färber 
426789f23bSCédric Le Goater static const char *powerpc_excp_name(int excp)
436789f23bSCédric Le Goater {
446789f23bSCédric Le Goater     switch (excp) {
456789f23bSCédric Le Goater     case POWERPC_EXCP_CRITICAL: return "CRITICAL";
466789f23bSCédric Le Goater     case POWERPC_EXCP_MCHECK:   return "MCHECK";
476789f23bSCédric Le Goater     case POWERPC_EXCP_DSI:      return "DSI";
486789f23bSCédric Le Goater     case POWERPC_EXCP_ISI:      return "ISI";
496789f23bSCédric Le Goater     case POWERPC_EXCP_EXTERNAL: return "EXTERNAL";
506789f23bSCédric Le Goater     case POWERPC_EXCP_ALIGN:    return "ALIGN";
516789f23bSCédric Le Goater     case POWERPC_EXCP_PROGRAM:  return "PROGRAM";
526789f23bSCédric Le Goater     case POWERPC_EXCP_FPU:      return "FPU";
536789f23bSCédric Le Goater     case POWERPC_EXCP_SYSCALL:  return "SYSCALL";
546789f23bSCédric Le Goater     case POWERPC_EXCP_APU:      return "APU";
556789f23bSCédric Le Goater     case POWERPC_EXCP_DECR:     return "DECR";
566789f23bSCédric Le Goater     case POWERPC_EXCP_FIT:      return "FIT";
576789f23bSCédric Le Goater     case POWERPC_EXCP_WDT:      return "WDT";
586789f23bSCédric Le Goater     case POWERPC_EXCP_DTLB:     return "DTLB";
596789f23bSCédric Le Goater     case POWERPC_EXCP_ITLB:     return "ITLB";
606789f23bSCédric Le Goater     case POWERPC_EXCP_DEBUG:    return "DEBUG";
616789f23bSCédric Le Goater     case POWERPC_EXCP_SPEU:     return "SPEU";
626789f23bSCédric Le Goater     case POWERPC_EXCP_EFPDI:    return "EFPDI";
636789f23bSCédric Le Goater     case POWERPC_EXCP_EFPRI:    return "EFPRI";
646789f23bSCédric Le Goater     case POWERPC_EXCP_EPERFM:   return "EPERFM";
656789f23bSCédric Le Goater     case POWERPC_EXCP_DOORI:    return "DOORI";
666789f23bSCédric Le Goater     case POWERPC_EXCP_DOORCI:   return "DOORCI";
676789f23bSCédric Le Goater     case POWERPC_EXCP_GDOORI:   return "GDOORI";
686789f23bSCédric Le Goater     case POWERPC_EXCP_GDOORCI:  return "GDOORCI";
696789f23bSCédric Le Goater     case POWERPC_EXCP_HYPPRIV:  return "HYPPRIV";
706789f23bSCédric Le Goater     case POWERPC_EXCP_RESET:    return "RESET";
716789f23bSCédric Le Goater     case POWERPC_EXCP_DSEG:     return "DSEG";
726789f23bSCédric Le Goater     case POWERPC_EXCP_ISEG:     return "ISEG";
736789f23bSCédric Le Goater     case POWERPC_EXCP_HDECR:    return "HDECR";
746789f23bSCédric Le Goater     case POWERPC_EXCP_TRACE:    return "TRACE";
756789f23bSCédric Le Goater     case POWERPC_EXCP_HDSI:     return "HDSI";
766789f23bSCédric Le Goater     case POWERPC_EXCP_HISI:     return "HISI";
776789f23bSCédric Le Goater     case POWERPC_EXCP_HDSEG:    return "HDSEG";
786789f23bSCédric Le Goater     case POWERPC_EXCP_HISEG:    return "HISEG";
796789f23bSCédric Le Goater     case POWERPC_EXCP_VPU:      return "VPU";
806789f23bSCédric Le Goater     case POWERPC_EXCP_PIT:      return "PIT";
816789f23bSCédric Le Goater     case POWERPC_EXCP_EMUL:     return "EMUL";
826789f23bSCédric Le Goater     case POWERPC_EXCP_IFTLB:    return "IFTLB";
836789f23bSCédric Le Goater     case POWERPC_EXCP_DLTLB:    return "DLTLB";
846789f23bSCédric Le Goater     case POWERPC_EXCP_DSTLB:    return "DSTLB";
856789f23bSCédric Le Goater     case POWERPC_EXCP_FPA:      return "FPA";
866789f23bSCédric Le Goater     case POWERPC_EXCP_DABR:     return "DABR";
876789f23bSCédric Le Goater     case POWERPC_EXCP_IABR:     return "IABR";
886789f23bSCédric Le Goater     case POWERPC_EXCP_SMI:      return "SMI";
896789f23bSCédric Le Goater     case POWERPC_EXCP_PERFM:    return "PERFM";
906789f23bSCédric Le Goater     case POWERPC_EXCP_THERM:    return "THERM";
916789f23bSCédric Le Goater     case POWERPC_EXCP_VPUA:     return "VPUA";
926789f23bSCédric Le Goater     case POWERPC_EXCP_SOFTP:    return "SOFTP";
936789f23bSCédric Le Goater     case POWERPC_EXCP_MAINT:    return "MAINT";
946789f23bSCédric Le Goater     case POWERPC_EXCP_MEXTBR:   return "MEXTBR";
956789f23bSCédric Le Goater     case POWERPC_EXCP_NMEXTBR:  return "NMEXTBR";
966789f23bSCédric Le Goater     case POWERPC_EXCP_ITLBE:    return "ITLBE";
976789f23bSCédric Le Goater     case POWERPC_EXCP_DTLBE:    return "DTLBE";
986789f23bSCédric Le Goater     case POWERPC_EXCP_VSXU:     return "VSXU";
996789f23bSCédric Le Goater     case POWERPC_EXCP_FU:       return "FU";
1006789f23bSCédric Le Goater     case POWERPC_EXCP_HV_EMU:   return "HV_EMU";
1016789f23bSCédric Le Goater     case POWERPC_EXCP_HV_MAINT: return "HV_MAINT";
1026789f23bSCédric Le Goater     case POWERPC_EXCP_HV_FU:    return "HV_FU";
1036789f23bSCédric Le Goater     case POWERPC_EXCP_SDOOR:    return "SDOOR";
1046789f23bSCédric Le Goater     case POWERPC_EXCP_SDOOR_HV: return "SDOOR_HV";
1056789f23bSCédric Le Goater     case POWERPC_EXCP_HVIRT:    return "HVIRT";
1066789f23bSCédric Le Goater     case POWERPC_EXCP_SYSCALL_VECTORED: return "SYSCALL_VECTORED";
1076789f23bSCédric Le Goater     default:
1086789f23bSCédric Le Goater         g_assert_not_reached();
1096789f23bSCédric Le Goater     }
1106789f23bSCédric Le Goater }
1116789f23bSCédric Le Goater 
11262e79ef9SCédric Le Goater static void dump_syscall(CPUPPCState *env)
113c79c73f6SBlue Swirl {
1146dc6b557SNicholas Piggin     qemu_log_mask(CPU_LOG_INT, "syscall r0=%016" PRIx64
1156dc6b557SNicholas Piggin                   " r3=%016" PRIx64 " r4=%016" PRIx64 " r5=%016" PRIx64
1166dc6b557SNicholas Piggin                   " r6=%016" PRIx64 " r7=%016" PRIx64 " r8=%016" PRIx64
117c79c73f6SBlue Swirl                   " nip=" TARGET_FMT_lx "\n",
118c79c73f6SBlue Swirl                   ppc_dump_gpr(env, 0), ppc_dump_gpr(env, 3),
119c79c73f6SBlue Swirl                   ppc_dump_gpr(env, 4), ppc_dump_gpr(env, 5),
1206dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 6), ppc_dump_gpr(env, 7),
1216dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 8), env->nip);
1226dc6b557SNicholas Piggin }
1236dc6b557SNicholas Piggin 
12462e79ef9SCédric Le Goater static void dump_hcall(CPUPPCState *env)
1256dc6b557SNicholas Piggin {
1266dc6b557SNicholas Piggin     qemu_log_mask(CPU_LOG_INT, "hypercall r3=%016" PRIx64
1276dc6b557SNicholas Piggin                   " r4=%016" PRIx64 " r5=%016" PRIx64 " r6=%016" PRIx64
1286dc6b557SNicholas Piggin                   " r7=%016" PRIx64 " r8=%016" PRIx64 " r9=%016" PRIx64
1296dc6b557SNicholas Piggin                   " r10=%016" PRIx64 " r11=%016" PRIx64 " r12=%016" PRIx64
1306dc6b557SNicholas Piggin                   " nip=" TARGET_FMT_lx "\n",
1316dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 3), ppc_dump_gpr(env, 4),
1326dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 5), ppc_dump_gpr(env, 6),
1336dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 7), ppc_dump_gpr(env, 8),
1346dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 9), ppc_dump_gpr(env, 10),
1356dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 11), ppc_dump_gpr(env, 12),
1366dc6b557SNicholas Piggin                   env->nip);
137c79c73f6SBlue Swirl }
138c79c73f6SBlue Swirl 
139888050cfSNicholas Piggin #ifdef CONFIG_TCG
140888050cfSNicholas Piggin /* Return true iff byteswap is needed to load instruction */
141888050cfSNicholas Piggin static inline bool insn_need_byteswap(CPUArchState *env)
142888050cfSNicholas Piggin {
143888050cfSNicholas Piggin     /* SYSTEM builds TARGET_BIG_ENDIAN. Need to swap when MSR[LE] is set */
144888050cfSNicholas Piggin     return !!(env->msr & ((target_ulong)1 << MSR_LE));
145888050cfSNicholas Piggin }
146888050cfSNicholas Piggin 
147eedd1095SPhilippe Mathieu-Daudé static uint32_t ppc_ldl_code(CPUArchState *env, target_ulong addr)
148888050cfSNicholas Piggin {
149888050cfSNicholas Piggin     uint32_t insn = cpu_ldl_code(env, addr);
150888050cfSNicholas Piggin 
151888050cfSNicholas Piggin     if (insn_need_byteswap(env)) {
152888050cfSNicholas Piggin         insn = bswap32(insn);
153888050cfSNicholas Piggin     }
154888050cfSNicholas Piggin 
155888050cfSNicholas Piggin     return insn;
156888050cfSNicholas Piggin }
15745693f94SNicholas Piggin 
158888050cfSNicholas Piggin #endif
159888050cfSNicholas Piggin 
160e4e27df7SFabiano Rosas static void ppc_excp_debug_sw_tlb(CPUPPCState *env, int excp)
161e4e27df7SFabiano Rosas {
162e4e27df7SFabiano Rosas     const char *es;
163e4e27df7SFabiano Rosas     target_ulong *miss, *cmp;
164e4e27df7SFabiano Rosas     int en;
165e4e27df7SFabiano Rosas 
1662e089eceSFabiano Rosas     if (!qemu_loglevel_mask(CPU_LOG_MMU)) {
167e4e27df7SFabiano Rosas         return;
168e4e27df7SFabiano Rosas     }
169e4e27df7SFabiano Rosas 
170e4e27df7SFabiano Rosas     if (excp == POWERPC_EXCP_IFTLB) {
171e4e27df7SFabiano Rosas         es = "I";
172e4e27df7SFabiano Rosas         en = 'I';
173e4e27df7SFabiano Rosas         miss = &env->spr[SPR_IMISS];
174e4e27df7SFabiano Rosas         cmp = &env->spr[SPR_ICMP];
175e4e27df7SFabiano Rosas     } else {
176e4e27df7SFabiano Rosas         if (excp == POWERPC_EXCP_DLTLB) {
177e4e27df7SFabiano Rosas             es = "DL";
178e4e27df7SFabiano Rosas         } else {
179e4e27df7SFabiano Rosas             es = "DS";
180e4e27df7SFabiano Rosas         }
181e4e27df7SFabiano Rosas         en = 'D';
182e4e27df7SFabiano Rosas         miss = &env->spr[SPR_DMISS];
183e4e27df7SFabiano Rosas         cmp = &env->spr[SPR_DCMP];
184e4e27df7SFabiano Rosas     }
185e4e27df7SFabiano Rosas     qemu_log("6xx %sTLB miss: %cM " TARGET_FMT_lx " %cC "
186e4e27df7SFabiano Rosas              TARGET_FMT_lx " H1 " TARGET_FMT_lx " H2 "
187e4e27df7SFabiano Rosas              TARGET_FMT_lx " %08x\n", es, en, *miss, en, *cmp,
188e4e27df7SFabiano Rosas              env->spr[SPR_HASH1], env->spr[SPR_HASH2],
189e4e27df7SFabiano Rosas              env->error_code);
190e4e27df7SFabiano Rosas }
191e4e27df7SFabiano Rosas 
192f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
1933f88a89dSBALATON Zoltan static int powerpc_reset_wakeup(CPUPPCState *env, int excp, target_ulong *msr)
194dead760bSBenjamin Herrenschmidt {
195dead760bSBenjamin Herrenschmidt     /* We no longer are in a PM state */
1961e7fd61dSBenjamin Herrenschmidt     env->resume_as_sreset = false;
197dead760bSBenjamin Herrenschmidt 
198dead760bSBenjamin Herrenschmidt     /* Pretend to be returning from doze always as we don't lose state */
1990911a60cSLeonardo Bras     *msr |= SRR1_WS_NOLOSS;
200dead760bSBenjamin Herrenschmidt 
201dead760bSBenjamin Herrenschmidt     /* Machine checks are sent normally */
202dead760bSBenjamin Herrenschmidt     if (excp == POWERPC_EXCP_MCHECK) {
203dead760bSBenjamin Herrenschmidt         return excp;
204dead760bSBenjamin Herrenschmidt     }
205dead760bSBenjamin Herrenschmidt     switch (excp) {
206dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_RESET:
2070911a60cSLeonardo Bras         *msr |= SRR1_WAKERESET;
208dead760bSBenjamin Herrenschmidt         break;
209dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_EXTERNAL:
2100911a60cSLeonardo Bras         *msr |= SRR1_WAKEEE;
211dead760bSBenjamin Herrenschmidt         break;
212dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_DECR:
2130911a60cSLeonardo Bras         *msr |= SRR1_WAKEDEC;
214dead760bSBenjamin Herrenschmidt         break;
215dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_SDOOR:
2160911a60cSLeonardo Bras         *msr |= SRR1_WAKEDBELL;
217dead760bSBenjamin Herrenschmidt         break;
218dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_SDOOR_HV:
2190911a60cSLeonardo Bras         *msr |= SRR1_WAKEHDBELL;
220dead760bSBenjamin Herrenschmidt         break;
221dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_HV_MAINT:
2220911a60cSLeonardo Bras         *msr |= SRR1_WAKEHMI;
223dead760bSBenjamin Herrenschmidt         break;
224d8ce5fd6SBenjamin Herrenschmidt     case POWERPC_EXCP_HVIRT:
2250911a60cSLeonardo Bras         *msr |= SRR1_WAKEHVI;
226d8ce5fd6SBenjamin Herrenschmidt         break;
227dead760bSBenjamin Herrenschmidt     default:
2283f88a89dSBALATON Zoltan         cpu_abort(env_cpu(env),
2293f88a89dSBALATON Zoltan                   "Unsupported exception %d in Power Save mode\n", excp);
230dead760bSBenjamin Herrenschmidt     }
231dead760bSBenjamin Herrenschmidt     return POWERPC_EXCP_RESET;
232dead760bSBenjamin Herrenschmidt }
233dead760bSBenjamin Herrenschmidt 
2348b7e6b07SNicholas Piggin /*
2358b7e6b07SNicholas Piggin  * AIL - Alternate Interrupt Location, a mode that allows interrupts to be
2368b7e6b07SNicholas Piggin  * taken with the MMU on, and which uses an alternate location (e.g., so the
2378b7e6b07SNicholas Piggin  * kernel/hv can map the vectors there with an effective address).
2388b7e6b07SNicholas Piggin  *
2398b7e6b07SNicholas Piggin  * An interrupt is considered to be taken "with AIL" or "AIL applies" if they
2408b7e6b07SNicholas Piggin  * are delivered in this way. AIL requires the LPCR to be set to enable this
2418b7e6b07SNicholas Piggin  * mode, and then a number of conditions have to be true for AIL to apply.
2428b7e6b07SNicholas Piggin  *
2438b7e6b07SNicholas Piggin  * First of all, SRESET, MCE, and HMI are always delivered without AIL, because
2448b7e6b07SNicholas Piggin  * they specifically want to be in real mode (e.g., the MCE might be signaling
2458b7e6b07SNicholas Piggin  * a SLB multi-hit which requires SLB flush before the MMU can be enabled).
2468b7e6b07SNicholas Piggin  *
2478b7e6b07SNicholas Piggin  * After that, behaviour depends on the current MSR[IR], MSR[DR], MSR[HV],
2488b7e6b07SNicholas Piggin  * whether or not the interrupt changes MSR[HV] from 0 to 1, and the current
2498b7e6b07SNicholas Piggin  * radix mode (LPCR[HR]).
2508b7e6b07SNicholas Piggin  *
2518b7e6b07SNicholas Piggin  * POWER8, POWER9 with LPCR[HR]=0
2528b7e6b07SNicholas Piggin  * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
2538b7e6b07SNicholas Piggin  * +-----------+-------------+---------+-------------+-----+
2548b7e6b07SNicholas Piggin  * | a         | 00/01/10    | x       | x           | 0   |
2558b7e6b07SNicholas Piggin  * | a         | 11          | 0       | 1           | 0   |
2568b7e6b07SNicholas Piggin  * | a         | 11          | 1       | 1           | a   |
2578b7e6b07SNicholas Piggin  * | a         | 11          | 0       | 0           | a   |
2588b7e6b07SNicholas Piggin  * +-------------------------------------------------------+
2598b7e6b07SNicholas Piggin  *
2608b7e6b07SNicholas Piggin  * POWER9 with LPCR[HR]=1
2618b7e6b07SNicholas Piggin  * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
2628b7e6b07SNicholas Piggin  * +-----------+-------------+---------+-------------+-----+
2638b7e6b07SNicholas Piggin  * | a         | 00/01/10    | x       | x           | 0   |
2648b7e6b07SNicholas Piggin  * | a         | 11          | x       | x           | a   |
2658b7e6b07SNicholas Piggin  * +-------------------------------------------------------+
2668b7e6b07SNicholas Piggin  *
2678b7e6b07SNicholas Piggin  * The difference with POWER9 being that MSR[HV] 0->1 interrupts can be sent to
268526cdce7SNicholas Piggin  * the hypervisor in AIL mode if the guest is radix. This is good for
269526cdce7SNicholas Piggin  * performance but allows the guest to influence the AIL of hypervisor
270526cdce7SNicholas Piggin  * interrupts using its MSR, and also the hypervisor must disallow guest
271526cdce7SNicholas Piggin  * interrupts (MSR[HV] 0->0) from using AIL if the hypervisor does not want to
272526cdce7SNicholas Piggin  * use AIL for its MSR[HV] 0->1 interrupts.
273526cdce7SNicholas Piggin  *
274526cdce7SNicholas Piggin  * POWER10 addresses those issues with a new LPCR[HAIL] bit that is applied to
275526cdce7SNicholas Piggin  * interrupts that begin execution with MSR[HV]=1 (so both MSR[HV] 0->1 and
276526cdce7SNicholas Piggin  * MSR[HV] 1->1).
277526cdce7SNicholas Piggin  *
278526cdce7SNicholas Piggin  * HAIL=1 is equivalent to AIL=3, for interrupts delivered with MSR[HV]=1.
279526cdce7SNicholas Piggin  *
280526cdce7SNicholas Piggin  * POWER10 behaviour is
281526cdce7SNicholas Piggin  * | LPCR[AIL] | LPCR[HAIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
282526cdce7SNicholas Piggin  * +-----------+------------+-------------+---------+-------------+-----+
283526cdce7SNicholas Piggin  * | a         | h          | 00/01/10    | 0       | 0           | 0   |
284526cdce7SNicholas Piggin  * | a         | h          | 11          | 0       | 0           | a   |
285526cdce7SNicholas Piggin  * | a         | h          | x           | 0       | 1           | h   |
286526cdce7SNicholas Piggin  * | a         | h          | 00/01/10    | 1       | 1           | 0   |
287526cdce7SNicholas Piggin  * | a         | h          | 11          | 1       | 1           | h   |
288526cdce7SNicholas Piggin  * +--------------------------------------------------------------------+
2898b7e6b07SNicholas Piggin  */
29010895ab6SFabiano Rosas static void ppc_excp_apply_ail(PowerPCCPU *cpu, int excp, target_ulong msr,
29110895ab6SFabiano Rosas                                target_ulong *new_msr, target_ulong *vector)
2922586a4d7SFabiano Rosas {
29310895ab6SFabiano Rosas     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
2948b7e6b07SNicholas Piggin     CPUPPCState *env = &cpu->env;
2958b7e6b07SNicholas Piggin     bool mmu_all_on = ((msr >> MSR_IR) & 1) && ((msr >> MSR_DR) & 1);
2968b7e6b07SNicholas Piggin     bool hv_escalation = !(msr & MSR_HVB) && (*new_msr & MSR_HVB);
2978b7e6b07SNicholas Piggin     int ail = 0;
2982586a4d7SFabiano Rosas 
2998b7e6b07SNicholas Piggin     if (excp == POWERPC_EXCP_MCHECK ||
3008b7e6b07SNicholas Piggin         excp == POWERPC_EXCP_RESET ||
3018b7e6b07SNicholas Piggin         excp == POWERPC_EXCP_HV_MAINT) {
3028b7e6b07SNicholas Piggin         /* SRESET, MCE, HMI never apply AIL */
3038b7e6b07SNicholas Piggin         return;
3042586a4d7SFabiano Rosas     }
3052586a4d7SFabiano Rosas 
30610895ab6SFabiano Rosas     if (!(pcc->lpcr_mask & LPCR_AIL)) {
30710895ab6SFabiano Rosas         /* This CPU does not have AIL */
30810895ab6SFabiano Rosas         return;
30910895ab6SFabiano Rosas     }
31010895ab6SFabiano Rosas 
31110895ab6SFabiano Rosas     /* P8 & P9 */
31210895ab6SFabiano Rosas     if (!(pcc->lpcr_mask & LPCR_HAIL)) {
3138b7e6b07SNicholas Piggin         if (!mmu_all_on) {
3148b7e6b07SNicholas Piggin             /* AIL only works if MSR[IR] and MSR[DR] are both enabled. */
3158b7e6b07SNicholas Piggin             return;
3168b7e6b07SNicholas Piggin         }
3178b7e6b07SNicholas Piggin         if (hv_escalation && !(env->spr[SPR_LPCR] & LPCR_HR)) {
3188b7e6b07SNicholas Piggin             /*
3198b7e6b07SNicholas Piggin              * AIL does not work if there is a MSR[HV] 0->1 transition and the
3208b7e6b07SNicholas Piggin              * partition is in HPT mode. For radix guests, such interrupts are
3218b7e6b07SNicholas Piggin              * allowed to be delivered to the hypervisor in ail mode.
3228b7e6b07SNicholas Piggin              */
3238b7e6b07SNicholas Piggin             return;
3248b7e6b07SNicholas Piggin         }
3258b7e6b07SNicholas Piggin 
3268b7e6b07SNicholas Piggin         ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT;
3278b7e6b07SNicholas Piggin         if (ail == 0) {
3288b7e6b07SNicholas Piggin             return;
3298b7e6b07SNicholas Piggin         }
3308b7e6b07SNicholas Piggin         if (ail == 1) {
3318b7e6b07SNicholas Piggin             /* AIL=1 is reserved, treat it like AIL=0 */
3328b7e6b07SNicholas Piggin             return;
3338b7e6b07SNicholas Piggin         }
334526cdce7SNicholas Piggin 
33510895ab6SFabiano Rosas     /* P10 and up */
33610895ab6SFabiano Rosas     } else {
337526cdce7SNicholas Piggin         if (!mmu_all_on && !hv_escalation) {
338526cdce7SNicholas Piggin             /*
339526cdce7SNicholas Piggin              * AIL works for HV interrupts even with guest MSR[IR/DR] disabled.
340526cdce7SNicholas Piggin              * Guest->guest and HV->HV interrupts do require MMU on.
341526cdce7SNicholas Piggin              */
342526cdce7SNicholas Piggin             return;
343526cdce7SNicholas Piggin         }
344526cdce7SNicholas Piggin 
345526cdce7SNicholas Piggin         if (*new_msr & MSR_HVB) {
346526cdce7SNicholas Piggin             if (!(env->spr[SPR_LPCR] & LPCR_HAIL)) {
347526cdce7SNicholas Piggin                 /* HV interrupts depend on LPCR[HAIL] */
348526cdce7SNicholas Piggin                 return;
349526cdce7SNicholas Piggin             }
350526cdce7SNicholas Piggin             ail = 3; /* HAIL=1 gives AIL=3 behaviour for HV interrupts */
351526cdce7SNicholas Piggin         } else {
352526cdce7SNicholas Piggin             ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT;
353526cdce7SNicholas Piggin         }
354526cdce7SNicholas Piggin         if (ail == 0) {
355526cdce7SNicholas Piggin             return;
356526cdce7SNicholas Piggin         }
357526cdce7SNicholas Piggin         if (ail == 1 || ail == 2) {
358526cdce7SNicholas Piggin             /* AIL=1 and AIL=2 are reserved, treat them like AIL=0 */
359526cdce7SNicholas Piggin             return;
360526cdce7SNicholas Piggin         }
3618b7e6b07SNicholas Piggin     }
3628b7e6b07SNicholas Piggin 
3638b7e6b07SNicholas Piggin     /*
3648b7e6b07SNicholas Piggin      * AIL applies, so the new MSR gets IR and DR set, and an offset applied
3658b7e6b07SNicholas Piggin      * to the new IP.
3668b7e6b07SNicholas Piggin      */
3678b7e6b07SNicholas Piggin     *new_msr |= (1 << MSR_IR) | (1 << MSR_DR);
3688b7e6b07SNicholas Piggin 
3698b7e6b07SNicholas Piggin     if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
3708b7e6b07SNicholas Piggin         if (ail == 2) {
3718b7e6b07SNicholas Piggin             *vector |= 0x0000000000018000ull;
3728b7e6b07SNicholas Piggin         } else if (ail == 3) {
3738b7e6b07SNicholas Piggin             *vector |= 0xc000000000004000ull;
3748b7e6b07SNicholas Piggin         }
3758b7e6b07SNicholas Piggin     } else {
3768b7e6b07SNicholas Piggin         /*
3778b7e6b07SNicholas Piggin          * scv AIL is a little different. AIL=2 does not change the address,
3788b7e6b07SNicholas Piggin          * only the MSR. AIL=3 replaces the 0x17000 base with 0xc...3000.
3798b7e6b07SNicholas Piggin          */
3808b7e6b07SNicholas Piggin         if (ail == 3) {
3818b7e6b07SNicholas Piggin             *vector &= ~0x0000000000017000ull; /* Un-apply the base offset */
3828b7e6b07SNicholas Piggin             *vector |= 0xc000000000003000ull; /* Apply scv's AIL=3 offset */
3838b7e6b07SNicholas Piggin         }
3848b7e6b07SNicholas Piggin     }
3852586a4d7SFabiano Rosas }
386f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
387dead760bSBenjamin Herrenschmidt 
3883680e994SNicholas Piggin static void powerpc_reset_excp_state(PowerPCCPU *cpu)
389ad77c6caSNicholas Piggin {
390ad77c6caSNicholas Piggin     CPUState *cs = CPU(cpu);
391ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
392ad77c6caSNicholas Piggin 
3933680e994SNicholas Piggin     /* Reset exception state */
3943680e994SNicholas Piggin     cs->exception_index = POWERPC_EXCP_NONE;
3953680e994SNicholas Piggin     env->error_code = 0;
3963680e994SNicholas Piggin }
3973680e994SNicholas Piggin 
3983680e994SNicholas Piggin static void powerpc_set_excp_state(PowerPCCPU *cpu, target_ulong vector,
3993680e994SNicholas Piggin                                    target_ulong msr)
4003680e994SNicholas Piggin {
4013680e994SNicholas Piggin     CPUPPCState *env = &cpu->env;
4023680e994SNicholas Piggin 
403fce9fbafSFabiano Rosas     assert((msr & env->msr_mask) == msr);
404fce9fbafSFabiano Rosas 
405ad77c6caSNicholas Piggin     /*
406ad77c6caSNicholas Piggin      * We don't use hreg_store_msr here as already have treated any
407ad77c6caSNicholas Piggin      * special case that could occur. Just store MSR and update hflags
408ad77c6caSNicholas Piggin      *
409ab452503SBALATON Zoltan      * Note: We *MUST* not use hreg_store_msr() as-is anyway because it will
410ab452503SBALATON Zoltan      * prevent setting of the HV bit which some exceptions might need to do.
411ad77c6caSNicholas Piggin      */
4123680e994SNicholas Piggin     env->nip = vector;
413fce9fbafSFabiano Rosas     env->msr = msr;
414ad77c6caSNicholas Piggin     hreg_compute_hflags(env);
4152fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
416ad77c6caSNicholas Piggin 
4173680e994SNicholas Piggin     powerpc_reset_excp_state(cpu);
418ad77c6caSNicholas Piggin 
419ad77c6caSNicholas Piggin     /*
420ad77c6caSNicholas Piggin      * Any interrupt is context synchronizing, check if TCG TLB needs
421ad77c6caSNicholas Piggin      * a delayed flush on ppc64
422ad77c6caSNicholas Piggin      */
423ad77c6caSNicholas Piggin     check_tlb_flush(env, false);
4243680e994SNicholas Piggin 
4253680e994SNicholas Piggin     /* Reset the reservation */
4263680e994SNicholas Piggin     env->reserve_addr = -1;
427ad77c6caSNicholas Piggin }
428ad77c6caSNicholas Piggin 
429cce7aee8SNicholas Piggin #ifdef CONFIG_TCG
43045693f94SNicholas Piggin /*
43145693f94SNicholas Piggin  * This stops the machine and logs CPU state without killing QEMU (like
43245693f94SNicholas Piggin  * cpu_abort()) because it is often a guest error as opposed to a QEMU error,
43345693f94SNicholas Piggin  * so the machine can still be debugged.
43445693f94SNicholas Piggin  */
43545693f94SNicholas Piggin static G_NORETURN void powerpc_checkstop(CPUPPCState *env, const char *reason)
43645693f94SNicholas Piggin {
437819b31b0SBALATON Zoltan     CPUState *cs = env_cpu(env);
4389728fb5cSNicholas Piggin     FILE *f;
439819b31b0SBALATON Zoltan 
4409728fb5cSNicholas Piggin     f = qemu_log_trylock();
4419728fb5cSNicholas Piggin     if (f) {
44245693f94SNicholas Piggin         fprintf(f, "Entering checkstop state: %s\n", reason);
4439728fb5cSNicholas Piggin         cpu_dump_state(cs, f, CPU_DUMP_FPU | CPU_DUMP_CCOP);
4449728fb5cSNicholas Piggin         qemu_log_unlock(f);
4459728fb5cSNicholas Piggin     }
446cce7aee8SNicholas Piggin 
447cce7aee8SNicholas Piggin     /*
448cce7aee8SNicholas Piggin      * This stops the machine and logs CPU state without killing QEMU
449cce7aee8SNicholas Piggin      * (like cpu_abort()) so the machine can still be debugged (because
450cce7aee8SNicholas Piggin      * it is often a guest error).
451cce7aee8SNicholas Piggin      */
452cce7aee8SNicholas Piggin     qemu_system_guest_panicked(NULL);
453cce7aee8SNicholas Piggin     cpu_loop_exit_noexc(cs);
45445693f94SNicholas Piggin }
45545693f94SNicholas Piggin 
45645693f94SNicholas Piggin #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
45745693f94SNicholas Piggin void helper_attn(CPUPPCState *env)
45845693f94SNicholas Piggin {
45945693f94SNicholas Piggin     /* POWER attn is unprivileged when enabled by HID, otherwise illegal */
46045693f94SNicholas Piggin     if ((*env->check_attn)(env)) {
46145693f94SNicholas Piggin         powerpc_checkstop(env, "host executed attn");
46245693f94SNicholas Piggin     } else {
46345693f94SNicholas Piggin         raise_exception_err(env, POWERPC_EXCP_HV_EMU,
46445693f94SNicholas Piggin                             POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
46545693f94SNicholas Piggin     }
46645693f94SNicholas Piggin }
46745693f94SNicholas Piggin #endif
46845693f94SNicholas Piggin #endif /* CONFIG_TCG */
46945693f94SNicholas Piggin 
47045693f94SNicholas Piggin static void powerpc_mcheck_checkstop(CPUPPCState *env)
47145693f94SNicholas Piggin {
47245693f94SNicholas Piggin     /* KVM guests always have MSR[ME] enabled */
47345693f94SNicholas Piggin #ifdef CONFIG_TCG
47445693f94SNicholas Piggin     if (FIELD_EX64(env->msr, MSR, ME)) {
47545693f94SNicholas Piggin         return;
47645693f94SNicholas Piggin     }
47745693f94SNicholas Piggin 
47845693f94SNicholas Piggin     powerpc_checkstop(env, "machine check with MSR[ME]=0");
479cce7aee8SNicholas Piggin #endif
480819b31b0SBALATON Zoltan }
481819b31b0SBALATON Zoltan 
482e808c2edSFabiano Rosas static void powerpc_excp_40x(PowerPCCPU *cpu, int excp)
483e808c2edSFabiano Rosas {
484e808c2edSFabiano Rosas     CPUPPCState *env = &cpu->env;
485e808c2edSFabiano Rosas     target_ulong msr, new_msr, vector;
486ab452503SBALATON Zoltan     int srr0 = SPR_SRR0, srr1 = SPR_SRR1;
487e808c2edSFabiano Rosas 
488e808c2edSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
489e808c2edSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
490e808c2edSFabiano Rosas 
491ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
492495fc7ffSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME));
493e808c2edSFabiano Rosas 
494ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
495495fc7ffSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
496e808c2edSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
497e808c2edSFabiano Rosas     }
498e808c2edSFabiano Rosas 
499e808c2edSFabiano Rosas     vector = env->excp_vectors[excp];
500e808c2edSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
501bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
502bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
503e808c2edSFabiano Rosas     }
504e808c2edSFabiano Rosas     vector |= env->excp_prefix;
505e808c2edSFabiano Rosas 
506e808c2edSFabiano Rosas     switch (excp) {
507e808c2edSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
508e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
509e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
510e808c2edSFabiano Rosas         break;
511e808c2edSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
512819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
513e808c2edSFabiano Rosas         /* machine check exceptions don't have ME set */
514e808c2edSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
515e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
516e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
517e808c2edSFabiano Rosas         break;
518e808c2edSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
519f9911e1eSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_40x_ESR], env->spr[SPR_40x_DEAR]);
520e808c2edSFabiano Rosas         break;
521e808c2edSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
522e808c2edSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
523e808c2edSFabiano Rosas         break;
524e808c2edSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
525e808c2edSFabiano Rosas         break;
526e808c2edSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
527e808c2edSFabiano Rosas         break;
528e808c2edSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
529e808c2edSFabiano Rosas         switch (env->error_code & ~0xF) {
530e808c2edSFabiano Rosas         case POWERPC_EXCP_FP:
531da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
532e808c2edSFabiano Rosas                 trace_ppc_excp_fp_ignore();
5333680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
534e808c2edSFabiano Rosas                 return;
535e808c2edSFabiano Rosas             }
53664e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_FP;
537e808c2edSFabiano Rosas             break;
538e808c2edSFabiano Rosas         case POWERPC_EXCP_INVAL:
539e808c2edSFabiano Rosas             trace_ppc_excp_inval(env->nip);
54064e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PIL;
541e808c2edSFabiano Rosas             break;
542e808c2edSFabiano Rosas         case POWERPC_EXCP_PRIV:
54364e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PPR;
544e808c2edSFabiano Rosas             break;
545e808c2edSFabiano Rosas         case POWERPC_EXCP_TRAP:
54664e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PTR;
547e808c2edSFabiano Rosas             break;
548e808c2edSFabiano Rosas         default:
549bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
550e808c2edSFabiano Rosas                       env->error_code);
551e808c2edSFabiano Rosas             break;
552e808c2edSFabiano Rosas         }
553e808c2edSFabiano Rosas         break;
554e808c2edSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
555e808c2edSFabiano Rosas         dump_syscall(env);
556e808c2edSFabiano Rosas 
557e808c2edSFabiano Rosas         /*
558e808c2edSFabiano Rosas          * We need to correct the NIP which in this case is supposed
559e808c2edSFabiano Rosas          * to point to the next instruction
560e808c2edSFabiano Rosas          */
561e808c2edSFabiano Rosas         env->nip += 4;
562e808c2edSFabiano Rosas         break;
563e808c2edSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
564e808c2edSFabiano Rosas         trace_ppc_excp_print("FIT");
565e808c2edSFabiano Rosas         break;
566e808c2edSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
567e808c2edSFabiano Rosas         trace_ppc_excp_print("WDT");
568e808c2edSFabiano Rosas         break;
569e808c2edSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
570e808c2edSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
571e808c2edSFabiano Rosas         break;
572e808c2edSFabiano Rosas     case POWERPC_EXCP_PIT:       /* Programmable interval timer interrupt    */
573e808c2edSFabiano Rosas         trace_ppc_excp_print("PIT");
574e808c2edSFabiano Rosas         break;
5754d8ac1d1SFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
576bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
5774d8ac1d1SFabiano Rosas                   powerpc_excp_name(excp));
5784d8ac1d1SFabiano Rosas         break;
579e808c2edSFabiano Rosas     default:
580bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
581bc30c1c6SBALATON Zoltan                   excp);
582e808c2edSFabiano Rosas         break;
583e808c2edSFabiano Rosas     }
584e808c2edSFabiano Rosas 
585e808c2edSFabiano Rosas     env->spr[srr0] = env->nip;
586e808c2edSFabiano Rosas     env->spr[srr1] = msr;
587e808c2edSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
588e808c2edSFabiano Rosas }
589e808c2edSFabiano Rosas 
59058d178fbSFabiano Rosas static void powerpc_excp_6xx(PowerPCCPU *cpu, int excp)
59158d178fbSFabiano Rosas {
59258d178fbSFabiano Rosas     CPUPPCState *env = &cpu->env;
59358d178fbSFabiano Rosas     target_ulong msr, new_msr, vector;
59458d178fbSFabiano Rosas 
59558d178fbSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
59658d178fbSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
59758d178fbSFabiano Rosas 
598ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
599082d783bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
60058d178fbSFabiano Rosas 
601ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
602082d783bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
60358d178fbSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
60458d178fbSFabiano Rosas     }
60558d178fbSFabiano Rosas 
60658d178fbSFabiano Rosas     vector = env->excp_vectors[excp];
60758d178fbSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
608bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
609bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
61058d178fbSFabiano Rosas     }
61158d178fbSFabiano Rosas     vector |= env->excp_prefix;
61258d178fbSFabiano Rosas 
61358d178fbSFabiano Rosas     switch (excp) {
61458d178fbSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
61558d178fbSFabiano Rosas         break;
61658d178fbSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
617819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
61858d178fbSFabiano Rosas         /* machine check exceptions don't have ME set */
61958d178fbSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
62058d178fbSFabiano Rosas         break;
62158d178fbSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
62258d178fbSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
62358d178fbSFabiano Rosas         break;
62458d178fbSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
62558d178fbSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
62658d178fbSFabiano Rosas         msr |= env->error_code;
62758d178fbSFabiano Rosas         break;
62858d178fbSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
62958d178fbSFabiano Rosas         break;
63058d178fbSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
63158d178fbSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
63258d178fbSFabiano Rosas         /*
63358d178fbSFabiano Rosas          * Note: the opcode fields will not be set properly for a
63458d178fbSFabiano Rosas          * direct store load/store, but nobody cares as nobody
63558d178fbSFabiano Rosas          * actually uses direct store segments.
63658d178fbSFabiano Rosas          */
63758d178fbSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
63858d178fbSFabiano Rosas         break;
63958d178fbSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
64058d178fbSFabiano Rosas         switch (env->error_code & ~0xF) {
64158d178fbSFabiano Rosas         case POWERPC_EXCP_FP:
642da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
64358d178fbSFabiano Rosas                 trace_ppc_excp_fp_ignore();
6443680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
64558d178fbSFabiano Rosas                 return;
64658d178fbSFabiano Rosas             }
64758d178fbSFabiano Rosas             /*
648ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
649ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
65058d178fbSFabiano Rosas              */
65158d178fbSFabiano Rosas             msr |= 0x00100000;
65258d178fbSFabiano Rosas             break;
65358d178fbSFabiano Rosas         case POWERPC_EXCP_INVAL:
65458d178fbSFabiano Rosas             trace_ppc_excp_inval(env->nip);
65558d178fbSFabiano Rosas             msr |= 0x00080000;
65658d178fbSFabiano Rosas             break;
65758d178fbSFabiano Rosas         case POWERPC_EXCP_PRIV:
65858d178fbSFabiano Rosas             msr |= 0x00040000;
65958d178fbSFabiano Rosas             break;
66058d178fbSFabiano Rosas         case POWERPC_EXCP_TRAP:
66158d178fbSFabiano Rosas             msr |= 0x00020000;
66258d178fbSFabiano Rosas             break;
66358d178fbSFabiano Rosas         default:
66458d178fbSFabiano Rosas             /* Should never occur */
665bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
66658d178fbSFabiano Rosas                       env->error_code);
66758d178fbSFabiano Rosas             break;
66858d178fbSFabiano Rosas         }
66958d178fbSFabiano Rosas         break;
67058d178fbSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
67158d178fbSFabiano Rosas         dump_syscall(env);
67258d178fbSFabiano Rosas 
67358d178fbSFabiano Rosas         /*
67458d178fbSFabiano Rosas          * We need to correct the NIP which in this case is supposed
67558d178fbSFabiano Rosas          * to point to the next instruction
67658d178fbSFabiano Rosas          */
67758d178fbSFabiano Rosas         env->nip += 4;
67858d178fbSFabiano Rosas         break;
67958d178fbSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
68058d178fbSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
68158d178fbSFabiano Rosas         break;
68258d178fbSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
68358d178fbSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
68458d178fbSFabiano Rosas         break;
68558d178fbSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
6868e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
687bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
688bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset exception "
689bc30c1c6SBALATON Zoltan                       "%d with no HV support\n", excp);
69058d178fbSFabiano Rosas         }
69158d178fbSFabiano Rosas         break;
69258d178fbSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
69358d178fbSFabiano Rosas         break;
69458d178fbSFabiano Rosas     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
69558d178fbSFabiano Rosas     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
69658d178fbSFabiano Rosas     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
69758d178fbSFabiano Rosas         /* Swap temporary saved registers with GPRs */
69858d178fbSFabiano Rosas         if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
69958d178fbSFabiano Rosas             new_msr |= (target_ulong)1 << MSR_TGPR;
70058d178fbSFabiano Rosas             hreg_swap_gpr_tgpr(env);
70158d178fbSFabiano Rosas         }
7028f8c7932SFabiano Rosas 
70358d178fbSFabiano Rosas         ppc_excp_debug_sw_tlb(env, excp);
70458d178fbSFabiano Rosas 
70558d178fbSFabiano Rosas         msr |= env->crf[0] << 28;
70658d178fbSFabiano Rosas         msr |= env->error_code; /* key, D/I, S/L bits */
70758d178fbSFabiano Rosas         /* Set way using a LRU mechanism */
70858d178fbSFabiano Rosas         msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
70958d178fbSFabiano Rosas         break;
71058d178fbSFabiano Rosas     case POWERPC_EXCP_FPA:       /* Floating-point assist exception          */
71158d178fbSFabiano Rosas     case POWERPC_EXCP_DABR:      /* Data address breakpoint                  */
71258d178fbSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
71358d178fbSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
71458d178fbSFabiano Rosas     case POWERPC_EXCP_MEXTBR:    /* Maskable external breakpoint             */
71558d178fbSFabiano Rosas     case POWERPC_EXCP_NMEXTBR:   /* Non maskable external breakpoint         */
716bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
71758d178fbSFabiano Rosas                   powerpc_excp_name(excp));
71858d178fbSFabiano Rosas         break;
71958d178fbSFabiano Rosas     default:
720bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
721bc30c1c6SBALATON Zoltan                   excp);
72258d178fbSFabiano Rosas         break;
72358d178fbSFabiano Rosas     }
72458d178fbSFabiano Rosas 
72558d178fbSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
72658d178fbSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
72758d178fbSFabiano Rosas     }
728c50eaed1SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
729c50eaed1SFabiano Rosas     env->spr[SPR_SRR1] = msr;
73058d178fbSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
73158d178fbSFabiano Rosas }
73258d178fbSFabiano Rosas 
733ccfca2fcSFabiano Rosas static void powerpc_excp_7xx(PowerPCCPU *cpu, int excp)
734ccfca2fcSFabiano Rosas {
735ccfca2fcSFabiano Rosas     CPUPPCState *env = &cpu->env;
736ccfca2fcSFabiano Rosas     target_ulong msr, new_msr, vector;
737ccfca2fcSFabiano Rosas 
738ccfca2fcSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
739ccfca2fcSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
740ccfca2fcSFabiano Rosas 
741ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
74293848d6aSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
743ccfca2fcSFabiano Rosas 
744ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
74593848d6aSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
746ccfca2fcSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
747ccfca2fcSFabiano Rosas     }
748ccfca2fcSFabiano Rosas 
749ccfca2fcSFabiano Rosas     vector = env->excp_vectors[excp];
750ccfca2fcSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
751bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
752bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
753ccfca2fcSFabiano Rosas     }
754ccfca2fcSFabiano Rosas     vector |= env->excp_prefix;
755ccfca2fcSFabiano Rosas 
756ccfca2fcSFabiano Rosas     switch (excp) {
757ccfca2fcSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
758819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
759ccfca2fcSFabiano Rosas         /* machine check exceptions don't have ME set */
760ccfca2fcSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
761ccfca2fcSFabiano Rosas         break;
762ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
763ccfca2fcSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
764ccfca2fcSFabiano Rosas         break;
765ccfca2fcSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
766ccfca2fcSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
767ccfca2fcSFabiano Rosas         msr |= env->error_code;
768ccfca2fcSFabiano Rosas         break;
769ccfca2fcSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
770ccfca2fcSFabiano Rosas         break;
771ccfca2fcSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
772ccfca2fcSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
773ccfca2fcSFabiano Rosas         /*
774ccfca2fcSFabiano Rosas          * Note: the opcode fields will not be set properly for a
775ccfca2fcSFabiano Rosas          * direct store load/store, but nobody cares as nobody
776ccfca2fcSFabiano Rosas          * actually uses direct store segments.
777ccfca2fcSFabiano Rosas          */
778ccfca2fcSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
779ccfca2fcSFabiano Rosas         break;
780ccfca2fcSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
781ccfca2fcSFabiano Rosas         switch (env->error_code & ~0xF) {
782ccfca2fcSFabiano Rosas         case POWERPC_EXCP_FP:
783da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
784ccfca2fcSFabiano Rosas                 trace_ppc_excp_fp_ignore();
7853680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
786ccfca2fcSFabiano Rosas                 return;
787ccfca2fcSFabiano Rosas             }
788ccfca2fcSFabiano Rosas             /*
789ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
790ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
791ccfca2fcSFabiano Rosas              */
792ccfca2fcSFabiano Rosas             msr |= 0x00100000;
793ccfca2fcSFabiano Rosas             break;
794ccfca2fcSFabiano Rosas         case POWERPC_EXCP_INVAL:
795ccfca2fcSFabiano Rosas             trace_ppc_excp_inval(env->nip);
796ccfca2fcSFabiano Rosas             msr |= 0x00080000;
797ccfca2fcSFabiano Rosas             break;
798ccfca2fcSFabiano Rosas         case POWERPC_EXCP_PRIV:
799ccfca2fcSFabiano Rosas             msr |= 0x00040000;
800ccfca2fcSFabiano Rosas             break;
801ccfca2fcSFabiano Rosas         case POWERPC_EXCP_TRAP:
802ccfca2fcSFabiano Rosas             msr |= 0x00020000;
803ccfca2fcSFabiano Rosas             break;
804ccfca2fcSFabiano Rosas         default:
805ccfca2fcSFabiano Rosas             /* Should never occur */
806bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
807ccfca2fcSFabiano Rosas                       env->error_code);
808ccfca2fcSFabiano Rosas             break;
809ccfca2fcSFabiano Rosas         }
810ccfca2fcSFabiano Rosas         break;
811ccfca2fcSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
8123b578635SFabiano Rosas     {
8133b578635SFabiano Rosas         int lev = env->error_code;
814ccfca2fcSFabiano Rosas 
8153b578635SFabiano Rosas         if (lev == 1 && cpu->vhyp) {
816ccfca2fcSFabiano Rosas             dump_hcall(env);
817ccfca2fcSFabiano Rosas         } else {
818ccfca2fcSFabiano Rosas             dump_syscall(env);
819ccfca2fcSFabiano Rosas         }
820ccfca2fcSFabiano Rosas 
821ccfca2fcSFabiano Rosas         /*
822ccfca2fcSFabiano Rosas          * We need to correct the NIP which in this case is supposed
823ccfca2fcSFabiano Rosas          * to point to the next instruction
824ccfca2fcSFabiano Rosas          */
825ccfca2fcSFabiano Rosas         env->nip += 4;
826ccfca2fcSFabiano Rosas 
8273b578635SFabiano Rosas         /*
8283b578635SFabiano Rosas          * The Virtual Open Firmware (VOF) relies on the 'sc 1'
8293b578635SFabiano Rosas          * instruction to communicate with QEMU. The pegasos2 machine
8303b578635SFabiano Rosas          * uses VOF and the 7xx CPUs, so although the 7xx don't have
8313b578635SFabiano Rosas          * HV mode, we need to keep hypercall support.
8323b578635SFabiano Rosas          */
8333b578635SFabiano Rosas         if (lev == 1 && cpu->vhyp) {
834c700b5e1SNicholas Piggin             cpu->vhyp_class->hypercall(cpu->vhyp, cpu);
8352c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
836ccfca2fcSFabiano Rosas             return;
837ccfca2fcSFabiano Rosas         }
8383b578635SFabiano Rosas 
839ccfca2fcSFabiano Rosas         break;
8403b578635SFabiano Rosas     }
841ccfca2fcSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
842ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
843ccfca2fcSFabiano Rosas         break;
844ccfca2fcSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
8458e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
846bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
847bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset exception "
848bc30c1c6SBALATON Zoltan                       "%d with no HV support\n", excp);
849ccfca2fcSFabiano Rosas         }
850ccfca2fcSFabiano Rosas         break;
851ccfca2fcSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
852ccfca2fcSFabiano Rosas         break;
853ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
854ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
855ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
856ccfca2fcSFabiano Rosas         ppc_excp_debug_sw_tlb(env, excp);
857ccfca2fcSFabiano Rosas         msr |= env->crf[0] << 28;
858ccfca2fcSFabiano Rosas         msr |= env->error_code; /* key, D/I, S/L bits */
859ccfca2fcSFabiano Rosas         /* Set way using a LRU mechanism */
860ccfca2fcSFabiano Rosas         msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
861ccfca2fcSFabiano Rosas         break;
862ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
863ccfca2fcSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
864ccfca2fcSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
865ccfca2fcSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
866bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
867ccfca2fcSFabiano Rosas                   powerpc_excp_name(excp));
868ccfca2fcSFabiano Rosas         break;
869ccfca2fcSFabiano Rosas     default:
870bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
871bc30c1c6SBALATON Zoltan                   excp);
872ccfca2fcSFabiano Rosas         break;
873ccfca2fcSFabiano Rosas     }
874ccfca2fcSFabiano Rosas 
875ccfca2fcSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
876ccfca2fcSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
877ccfca2fcSFabiano Rosas     }
878fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
879fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR1] = msr;
880ccfca2fcSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
881ccfca2fcSFabiano Rosas }
882ccfca2fcSFabiano Rosas 
88352926b0dSFabiano Rosas static void powerpc_excp_74xx(PowerPCCPU *cpu, int excp)
88452926b0dSFabiano Rosas {
88552926b0dSFabiano Rosas     CPUPPCState *env = &cpu->env;
88652926b0dSFabiano Rosas     target_ulong msr, new_msr, vector;
88752926b0dSFabiano Rosas 
88852926b0dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
88952926b0dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
89052926b0dSFabiano Rosas 
891ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
8921f6faf8bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
89352926b0dSFabiano Rosas 
894ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
8951f6faf8bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
89652926b0dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
89752926b0dSFabiano Rosas     }
89852926b0dSFabiano Rosas 
89952926b0dSFabiano Rosas     vector = env->excp_vectors[excp];
90052926b0dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
901bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
902bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
90352926b0dSFabiano Rosas     }
90452926b0dSFabiano Rosas     vector |= env->excp_prefix;
90552926b0dSFabiano Rosas 
90652926b0dSFabiano Rosas     switch (excp) {
90752926b0dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
908819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
90952926b0dSFabiano Rosas         /* machine check exceptions don't have ME set */
91052926b0dSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
91152926b0dSFabiano Rosas         break;
91252926b0dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
91352926b0dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
91452926b0dSFabiano Rosas         break;
91552926b0dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
91652926b0dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
91752926b0dSFabiano Rosas         msr |= env->error_code;
91852926b0dSFabiano Rosas         break;
91952926b0dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
92052926b0dSFabiano Rosas         break;
92152926b0dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
92252926b0dSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
92352926b0dSFabiano Rosas         /*
92452926b0dSFabiano Rosas          * Note: the opcode fields will not be set properly for a
92552926b0dSFabiano Rosas          * direct store load/store, but nobody cares as nobody
92652926b0dSFabiano Rosas          * actually uses direct store segments.
92752926b0dSFabiano Rosas          */
92852926b0dSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
92952926b0dSFabiano Rosas         break;
93052926b0dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
93152926b0dSFabiano Rosas         switch (env->error_code & ~0xF) {
93252926b0dSFabiano Rosas         case POWERPC_EXCP_FP:
933da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
93452926b0dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
9353680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
93652926b0dSFabiano Rosas                 return;
93752926b0dSFabiano Rosas             }
93852926b0dSFabiano Rosas             /*
939ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
940ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
94152926b0dSFabiano Rosas              */
94252926b0dSFabiano Rosas             msr |= 0x00100000;
94352926b0dSFabiano Rosas             break;
94452926b0dSFabiano Rosas         case POWERPC_EXCP_INVAL:
94552926b0dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
94652926b0dSFabiano Rosas             msr |= 0x00080000;
94752926b0dSFabiano Rosas             break;
94852926b0dSFabiano Rosas         case POWERPC_EXCP_PRIV:
94952926b0dSFabiano Rosas             msr |= 0x00040000;
95052926b0dSFabiano Rosas             break;
95152926b0dSFabiano Rosas         case POWERPC_EXCP_TRAP:
95252926b0dSFabiano Rosas             msr |= 0x00020000;
95352926b0dSFabiano Rosas             break;
95452926b0dSFabiano Rosas         default:
95552926b0dSFabiano Rosas             /* Should never occur */
956bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
95752926b0dSFabiano Rosas                       env->error_code);
95852926b0dSFabiano Rosas             break;
95952926b0dSFabiano Rosas         }
96052926b0dSFabiano Rosas         break;
96152926b0dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
962bca2c6d9SFabiano Rosas     {
963bca2c6d9SFabiano Rosas         int lev = env->error_code;
96452926b0dSFabiano Rosas 
9652306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
96652926b0dSFabiano Rosas             dump_hcall(env);
96752926b0dSFabiano Rosas         } else {
96852926b0dSFabiano Rosas             dump_syscall(env);
96952926b0dSFabiano Rosas         }
97052926b0dSFabiano Rosas 
97152926b0dSFabiano Rosas         /*
97252926b0dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
97352926b0dSFabiano Rosas          * to point to the next instruction
97452926b0dSFabiano Rosas          */
97552926b0dSFabiano Rosas         env->nip += 4;
97652926b0dSFabiano Rosas 
977bca2c6d9SFabiano Rosas         /*
978bca2c6d9SFabiano Rosas          * The Virtual Open Firmware (VOF) relies on the 'sc 1'
979bca2c6d9SFabiano Rosas          * instruction to communicate with QEMU. The pegasos2 machine
980bca2c6d9SFabiano Rosas          * uses VOF and the 74xx CPUs, so although the 74xx don't have
981bca2c6d9SFabiano Rosas          * HV mode, we need to keep hypercall support.
982bca2c6d9SFabiano Rosas          */
9832306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
984c700b5e1SNicholas Piggin             cpu->vhyp_class->hypercall(cpu->vhyp, cpu);
9852c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
98652926b0dSFabiano Rosas             return;
98752926b0dSFabiano Rosas         }
988bca2c6d9SFabiano Rosas 
98952926b0dSFabiano Rosas         break;
990bca2c6d9SFabiano Rosas     }
99152926b0dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
99252926b0dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
99352926b0dSFabiano Rosas         break;
99452926b0dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
9958e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
996bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
997bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset "
99852926b0dSFabiano Rosas                       "exception %d with no HV support\n", excp);
99952926b0dSFabiano Rosas         }
100052926b0dSFabiano Rosas         break;
100152926b0dSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
100252926b0dSFabiano Rosas         break;
100352926b0dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
100452926b0dSFabiano Rosas         break;
100552926b0dSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
100652926b0dSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
100752926b0dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
100852926b0dSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
100952926b0dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
1010bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
101152926b0dSFabiano Rosas                   powerpc_excp_name(excp));
101252926b0dSFabiano Rosas         break;
101352926b0dSFabiano Rosas     default:
1014bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1015bc30c1c6SBALATON Zoltan                   excp);
101652926b0dSFabiano Rosas         break;
101752926b0dSFabiano Rosas     }
101852926b0dSFabiano Rosas 
101952926b0dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
102052926b0dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
102152926b0dSFabiano Rosas     }
1022f82db777SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
1023f82db777SFabiano Rosas     env->spr[SPR_SRR1] = msr;
102452926b0dSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
102552926b0dSFabiano Rosas }
102652926b0dSFabiano Rosas 
1027180952ceSFabiano Rosas static void powerpc_excp_booke(PowerPCCPU *cpu, int excp)
1028180952ceSFabiano Rosas {
1029180952ceSFabiano Rosas     CPUPPCState *env = &cpu->env;
1030180952ceSFabiano Rosas     target_ulong msr, new_msr, vector;
1031ab452503SBALATON Zoltan     int srr0 = SPR_SRR0, srr1 = SPR_SRR1;
1032180952ceSFabiano Rosas 
1033ab452503SBALATON Zoltan     /*
1034ab452503SBALATON Zoltan      * Book E does not play games with certain bits of xSRR1 being MSR save
1035ab452503SBALATON Zoltan      * bits and others being error status. xSRR1 is the old MSR, period.
1036ab452503SBALATON Zoltan      */
1037180952ceSFabiano Rosas     msr = env->msr;
1038180952ceSFabiano Rosas 
1039ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
10409dc20cc3SFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
1041180952ceSFabiano Rosas 
1042ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
10439dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
1044180952ceSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
1045180952ceSFabiano Rosas     }
1046180952ceSFabiano Rosas 
1047180952ceSFabiano Rosas #ifdef TARGET_PPC64
1048180952ceSFabiano Rosas     /*
1049180952ceSFabiano Rosas      * SPEU and VPU share the same IVOR but they exist in different
1050180952ceSFabiano Rosas      * processors. SPEU is e500v1/2 only and VPU is e6500 only.
1051180952ceSFabiano Rosas      */
10529dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_VPU) {
1053180952ceSFabiano Rosas         excp = POWERPC_EXCP_SPEU;
1054180952ceSFabiano Rosas     }
1055180952ceSFabiano Rosas #endif
1056180952ceSFabiano Rosas 
1057180952ceSFabiano Rosas     vector = env->excp_vectors[excp];
1058180952ceSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
1059bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
1060bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
1061180952ceSFabiano Rosas     }
1062180952ceSFabiano Rosas     vector |= env->excp_prefix;
1063180952ceSFabiano Rosas 
1064180952ceSFabiano Rosas     switch (excp) {
1065180952ceSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
1066180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
1067180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
1068180952ceSFabiano Rosas         break;
1069180952ceSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
1070819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
1071180952ceSFabiano Rosas         /* machine check exceptions don't have ME set */
1072180952ceSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
1073180952ceSFabiano Rosas 
1074180952ceSFabiano Rosas         /* FIXME: choose one or the other based on CPU type */
1075180952ceSFabiano Rosas         srr0 = SPR_BOOKE_MCSRR0;
1076180952ceSFabiano Rosas         srr1 = SPR_BOOKE_MCSRR1;
1077180952ceSFabiano Rosas 
1078180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR0] = env->nip;
1079180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR1] = msr;
1080db403211SFabiano Rosas 
1081180952ceSFabiano Rosas         break;
1082180952ceSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
1083afdbc869SFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
1084180952ceSFabiano Rosas         break;
1085180952ceSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
1086180952ceSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
1087180952ceSFabiano Rosas         break;
1088180952ceSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
1089180952ceSFabiano Rosas         if (env->mpic_proxy) {
1090bc30c1c6SBALATON Zoltan             CPUState *cs = env_cpu(env);
1091180952ceSFabiano Rosas             /* IACK the IRQ on delivery */
1092180952ceSFabiano Rosas             env->spr[SPR_BOOKE_EPR] = ldl_phys(cs->as, env->mpic_iack);
1093180952ceSFabiano Rosas         }
1094180952ceSFabiano Rosas         break;
1095180952ceSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
1096180952ceSFabiano Rosas         break;
1097180952ceSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
1098180952ceSFabiano Rosas         switch (env->error_code & ~0xF) {
1099180952ceSFabiano Rosas         case POWERPC_EXCP_FP:
1100da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
1101180952ceSFabiano Rosas                 trace_ppc_excp_fp_ignore();
11023680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
1103180952ceSFabiano Rosas                 return;
1104180952ceSFabiano Rosas             }
1105180952ceSFabiano Rosas             /*
1106ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
1107ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
1108180952ceSFabiano Rosas              */
1109180952ceSFabiano Rosas             msr |= 0x00100000;
1110180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_FP;
1111180952ceSFabiano Rosas             break;
1112180952ceSFabiano Rosas         case POWERPC_EXCP_INVAL:
1113180952ceSFabiano Rosas             trace_ppc_excp_inval(env->nip);
1114180952ceSFabiano Rosas             msr |= 0x00080000;
1115180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PIL;
1116180952ceSFabiano Rosas             break;
1117180952ceSFabiano Rosas         case POWERPC_EXCP_PRIV:
1118180952ceSFabiano Rosas             msr |= 0x00040000;
1119180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PPR;
1120180952ceSFabiano Rosas             break;
1121180952ceSFabiano Rosas         case POWERPC_EXCP_TRAP:
1122180952ceSFabiano Rosas             msr |= 0x00020000;
1123180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PTR;
1124180952ceSFabiano Rosas             break;
1125180952ceSFabiano Rosas         default:
1126180952ceSFabiano Rosas             /* Should never occur */
1127bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
1128180952ceSFabiano Rosas                       env->error_code);
1129180952ceSFabiano Rosas             break;
1130180952ceSFabiano Rosas         }
1131180952ceSFabiano Rosas         break;
1132180952ceSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
1133180952ceSFabiano Rosas         dump_syscall(env);
1134180952ceSFabiano Rosas 
1135180952ceSFabiano Rosas         /*
1136180952ceSFabiano Rosas          * We need to correct the NIP which in this case is supposed
1137180952ceSFabiano Rosas          * to point to the next instruction
1138180952ceSFabiano Rosas          */
1139180952ceSFabiano Rosas         env->nip += 4;
1140180952ceSFabiano Rosas         break;
1141180952ceSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
1142180952ceSFabiano Rosas     case POWERPC_EXCP_APU:       /* Auxiliary processor unavailable          */
1143180952ceSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
1144180952ceSFabiano Rosas         break;
1145180952ceSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
1146180952ceSFabiano Rosas         /* FIT on 4xx */
1147180952ceSFabiano Rosas         trace_ppc_excp_print("FIT");
1148180952ceSFabiano Rosas         break;
1149180952ceSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
1150180952ceSFabiano Rosas         trace_ppc_excp_print("WDT");
1151180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
1152180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
1153180952ceSFabiano Rosas         break;
1154180952ceSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
1155180952ceSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
1156180952ceSFabiano Rosas         break;
1157180952ceSFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
1158180952ceSFabiano Rosas         if (env->flags & POWERPC_FLAG_DE) {
1159180952ceSFabiano Rosas             /* FIXME: choose one or the other based on CPU type */
1160180952ceSFabiano Rosas             srr0 = SPR_BOOKE_DSRR0;
1161180952ceSFabiano Rosas             srr1 = SPR_BOOKE_DSRR1;
1162180952ceSFabiano Rosas 
1163180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR0] = env->nip;
1164180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR1] = msr;
1165180952ceSFabiano Rosas 
1166180952ceSFabiano Rosas             /* DBSR already modified by caller */
1167180952ceSFabiano Rosas         } else {
1168bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
1169bc30c1c6SBALATON Zoltan                       "Debug exception triggered on unsupported model\n");
1170180952ceSFabiano Rosas         }
1171180952ceSFabiano Rosas         break;
1172180952ceSFabiano Rosas     case POWERPC_EXCP_SPEU:   /* SPE/embedded floating-point unavailable/VPU  */
1173180952ceSFabiano Rosas         env->spr[SPR_BOOKE_ESR] = ESR_SPV;
1174180952ceSFabiano Rosas         break;
11759364df26SNicholas Piggin     case POWERPC_EXCP_DOORI:     /* Embedded doorbell interrupt              */
11769364df26SNicholas Piggin         break;
11779364df26SNicholas Piggin     case POWERPC_EXCP_DOORCI:    /* Embedded doorbell critical interrupt     */
11789364df26SNicholas Piggin         srr0 = SPR_BOOKE_CSRR0;
11799364df26SNicholas Piggin         srr1 = SPR_BOOKE_CSRR1;
11809364df26SNicholas Piggin         break;
1181180952ceSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
11828e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
1183bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
1184bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset "
1185180952ceSFabiano Rosas                       "exception %d with no HV support\n", excp);
1186180952ceSFabiano Rosas         }
1187180952ceSFabiano Rosas         break;
1188180952ceSFabiano Rosas     case POWERPC_EXCP_EFPDI:     /* Embedded floating-point data interrupt   */
1189180952ceSFabiano Rosas     case POWERPC_EXCP_EFPRI:     /* Embedded floating-point round interrupt  */
1190bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
1191180952ceSFabiano Rosas                   powerpc_excp_name(excp));
1192180952ceSFabiano Rosas         break;
1193180952ceSFabiano Rosas     default:
1194bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1195bc30c1c6SBALATON Zoltan                   excp);
1196180952ceSFabiano Rosas         break;
1197180952ceSFabiano Rosas     }
1198180952ceSFabiano Rosas 
1199f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
1200180952ceSFabiano Rosas     if (env->spr[SPR_BOOKE_EPCR] & EPCR_ICM) {
1201180952ceSFabiano Rosas         /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */
1202180952ceSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_CM;
1203180952ceSFabiano Rosas     } else {
1204180952ceSFabiano Rosas         vector = (uint32_t)vector;
1205180952ceSFabiano Rosas     }
1206180952ceSFabiano Rosas #endif
1207180952ceSFabiano Rosas 
1208180952ceSFabiano Rosas     env->spr[srr0] = env->nip;
1209180952ceSFabiano Rosas     env->spr[srr1] = msr;
1210180952ceSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
1211180952ceSFabiano Rosas }
1212180952ceSFabiano Rosas 
12137cebc5dbSNicholas Piggin /*
12147cebc5dbSNicholas Piggin  * When running a nested HV guest under vhyp, external interrupts are
12157cebc5dbSNicholas Piggin  * delivered as HVIRT.
12167cebc5dbSNicholas Piggin  */
12177cebc5dbSNicholas Piggin static bool books_vhyp_promotes_external_to_hvirt(PowerPCCPU *cpu)
12187cebc5dbSNicholas Piggin {
12197cebc5dbSNicholas Piggin     if (cpu->vhyp) {
12207cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
12217cebc5dbSNicholas Piggin     }
12227cebc5dbSNicholas Piggin     return false;
12237cebc5dbSNicholas Piggin }
12247cebc5dbSNicholas Piggin 
122530c4e426SFabiano Rosas #ifdef TARGET_PPC64
12264c6cf6b2SNicholas Piggin /*
12274c6cf6b2SNicholas Piggin  * When running under vhyp, hcalls are always intercepted and sent to the
12284c6cf6b2SNicholas Piggin  * vhc->hypercall handler.
12294c6cf6b2SNicholas Piggin  */
12304c6cf6b2SNicholas Piggin static bool books_vhyp_handles_hcall(PowerPCCPU *cpu)
12314c6cf6b2SNicholas Piggin {
12324c6cf6b2SNicholas Piggin     if (cpu->vhyp) {
12337cebc5dbSNicholas Piggin         return !vhyp_cpu_in_nested(cpu);
12347cebc5dbSNicholas Piggin     }
12357cebc5dbSNicholas Piggin     return false;
12367cebc5dbSNicholas Piggin }
12377cebc5dbSNicholas Piggin 
12387cebc5dbSNicholas Piggin /*
12397cebc5dbSNicholas Piggin  * When running a nested KVM HV guest under vhyp, HV exceptions are not
12407cebc5dbSNicholas Piggin  * delivered to the guest (because there is no concept of HV support), but
12417cebc5dbSNicholas Piggin  * rather they are sent to the vhyp to exit from the L2 back to the L1 and
12427cebc5dbSNicholas Piggin  * return from the H_ENTER_NESTED hypercall.
12437cebc5dbSNicholas Piggin  */
12447cebc5dbSNicholas Piggin static bool books_vhyp_handles_hv_excp(PowerPCCPU *cpu)
12457cebc5dbSNicholas Piggin {
12467cebc5dbSNicholas Piggin     if (cpu->vhyp) {
12477cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
12484c6cf6b2SNicholas Piggin     }
12494c6cf6b2SNicholas Piggin     return false;
12504c6cf6b2SNicholas Piggin }
12514c6cf6b2SNicholas Piggin 
12525a5d3b23SNicholas Piggin #ifdef CONFIG_TCG
12535a5d3b23SNicholas Piggin static bool is_prefix_insn(CPUPPCState *env, uint32_t insn)
12545a5d3b23SNicholas Piggin {
12555a5d3b23SNicholas Piggin     if (!(env->insns_flags2 & PPC2_ISA310)) {
12565a5d3b23SNicholas Piggin         return false;
12575a5d3b23SNicholas Piggin     }
12585a5d3b23SNicholas Piggin     return ((insn & 0xfc000000) == 0x04000000);
12595a5d3b23SNicholas Piggin }
12605a5d3b23SNicholas Piggin 
12615a5d3b23SNicholas Piggin static bool is_prefix_insn_excp(PowerPCCPU *cpu, int excp)
12625a5d3b23SNicholas Piggin {
12635a5d3b23SNicholas Piggin     CPUPPCState *env = &cpu->env;
12645a5d3b23SNicholas Piggin 
1265c8fd9667SNicholas Piggin     if (!(env->insns_flags2 & PPC2_ISA310)) {
1266c8fd9667SNicholas Piggin         return false;
1267c8fd9667SNicholas Piggin     }
1268c8fd9667SNicholas Piggin 
12695a5d3b23SNicholas Piggin     if (!tcg_enabled()) {
12705a5d3b23SNicholas Piggin         /*
12715a5d3b23SNicholas Piggin          * This does not load instructions and set the prefix bit correctly
12725a5d3b23SNicholas Piggin          * for injected interrupts with KVM. That may have to be discovered
12735a5d3b23SNicholas Piggin          * and set by the KVM layer before injecting.
12745a5d3b23SNicholas Piggin          */
12755a5d3b23SNicholas Piggin         return false;
12765a5d3b23SNicholas Piggin     }
12775a5d3b23SNicholas Piggin 
12785a5d3b23SNicholas Piggin     switch (excp) {
1279c8fd9667SNicholas Piggin     case POWERPC_EXCP_MCHECK:
1280c8fd9667SNicholas Piggin         if (!(env->error_code & PPC_BIT(42))) {
1281c8fd9667SNicholas Piggin             /*
1282c8fd9667SNicholas Piggin              * Fetch attempt caused a machine check, so attempting to fetch
1283c8fd9667SNicholas Piggin              * again would cause a recursive machine check.
1284c8fd9667SNicholas Piggin              */
1285c8fd9667SNicholas Piggin             return false;
1286c8fd9667SNicholas Piggin         }
1287c8fd9667SNicholas Piggin         break;
12885a5d3b23SNicholas Piggin     case POWERPC_EXCP_HDSI:
12895a5d3b23SNicholas Piggin         /* HDSI PRTABLE_FAULT has the originating access type in error_code */
12905a5d3b23SNicholas Piggin         if ((env->spr[SPR_HDSISR] & DSISR_PRTABLE_FAULT) &&
12915a5d3b23SNicholas Piggin             (env->error_code == MMU_INST_FETCH)) {
12925a5d3b23SNicholas Piggin             /*
12935a5d3b23SNicholas Piggin              * Fetch failed due to partition scope translation, so prefix
12945a5d3b23SNicholas Piggin              * indication is not relevant (and attempting to load the
12955a5d3b23SNicholas Piggin              * instruction at NIP would cause recursive faults with the same
12965a5d3b23SNicholas Piggin              * translation).
12975a5d3b23SNicholas Piggin              */
1298c8fd9667SNicholas Piggin             return false;
12995a5d3b23SNicholas Piggin         }
1300c8fd9667SNicholas Piggin         break;
1301c8fd9667SNicholas Piggin 
13025a5d3b23SNicholas Piggin     case POWERPC_EXCP_DSI:
13035a5d3b23SNicholas Piggin     case POWERPC_EXCP_DSEG:
13045a5d3b23SNicholas Piggin     case POWERPC_EXCP_ALIGN:
13055a5d3b23SNicholas Piggin     case POWERPC_EXCP_PROGRAM:
13065a5d3b23SNicholas Piggin     case POWERPC_EXCP_FPU:
13075a5d3b23SNicholas Piggin     case POWERPC_EXCP_TRACE:
13085a5d3b23SNicholas Piggin     case POWERPC_EXCP_HV_EMU:
13095a5d3b23SNicholas Piggin     case POWERPC_EXCP_VPU:
13105a5d3b23SNicholas Piggin     case POWERPC_EXCP_VSXU:
13115a5d3b23SNicholas Piggin     case POWERPC_EXCP_FU:
1312c8fd9667SNicholas Piggin     case POWERPC_EXCP_HV_FU:
13135a5d3b23SNicholas Piggin         break;
13145a5d3b23SNicholas Piggin     default:
13155a5d3b23SNicholas Piggin         return false;
13165a5d3b23SNicholas Piggin     }
1317c8fd9667SNicholas Piggin 
1318c8fd9667SNicholas Piggin     return is_prefix_insn(env, ppc_ldl_code(env, env->nip));
1319c8fd9667SNicholas Piggin }
13205a5d3b23SNicholas Piggin #else
13215a5d3b23SNicholas Piggin static bool is_prefix_insn_excp(PowerPCCPU *cpu, int excp)
13225a5d3b23SNicholas Piggin {
13235a5d3b23SNicholas Piggin     return false;
13245a5d3b23SNicholas Piggin }
13255a5d3b23SNicholas Piggin #endif
13265a5d3b23SNicholas Piggin 
13279f338e4dSFabiano Rosas static void powerpc_excp_books(PowerPCCPU *cpu, int excp)
13289f338e4dSFabiano Rosas {
13299f338e4dSFabiano Rosas     CPUPPCState *env = &cpu->env;
13309f338e4dSFabiano Rosas     target_ulong msr, new_msr, vector;
1331ab452503SBALATON Zoltan     int srr0 = SPR_SRR0, srr1 = SPR_SRR1, lev = -1;
13329f338e4dSFabiano Rosas 
13339f338e4dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
13349f338e4dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
13359f338e4dSFabiano Rosas 
13369f338e4dSFabiano Rosas     /*
1337ab452503SBALATON Zoltan      * new interrupt handler msr preserves HV and ME unless explicitly
1338ab452503SBALATON Zoltan      * overridden
13399f338e4dSFabiano Rosas      */
13409f338e4dSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME) | MSR_HVB);
13419f338e4dSFabiano Rosas 
13429f338e4dSFabiano Rosas     /*
13439f338e4dSFabiano Rosas      * check for special resume at 0x100 from doze/nap/sleep/winkle on
13449f338e4dSFabiano Rosas      * P7/P8/P9
13459f338e4dSFabiano Rosas      */
13469f338e4dSFabiano Rosas     if (env->resume_as_sreset) {
13473f88a89dSBALATON Zoltan         excp = powerpc_reset_wakeup(env, excp, &msr);
13489f338e4dSFabiano Rosas     }
13499f338e4dSFabiano Rosas 
13509f338e4dSFabiano Rosas     /*
135130c4e426SFabiano Rosas      * We don't want to generate a Hypervisor Emulation Assistance
13526c242e79SNicholas Piggin      * Interrupt if we don't have HVB in msr_mask (PAPR mode),
13536c242e79SNicholas Piggin      * unless running a nested-hv guest, in which case the L1
13546c242e79SNicholas Piggin      * kernel wants the interrupt.
13559f338e4dSFabiano Rosas      */
13566c242e79SNicholas Piggin     if (excp == POWERPC_EXCP_HV_EMU && !(env->msr_mask & MSR_HVB) &&
13576c242e79SNicholas Piggin             !books_vhyp_handles_hv_excp(cpu)) {
13589f338e4dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
13599f338e4dSFabiano Rosas     }
13609f338e4dSFabiano Rosas 
13619f338e4dSFabiano Rosas     vector = env->excp_vectors[excp];
13629f338e4dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
1363bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
1364bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
13659f338e4dSFabiano Rosas     }
13669f338e4dSFabiano Rosas     vector |= env->excp_prefix;
13679f338e4dSFabiano Rosas 
13685a5d3b23SNicholas Piggin     if (is_prefix_insn_excp(cpu, excp)) {
13695a5d3b23SNicholas Piggin         msr |= PPC_BIT(34);
13705a5d3b23SNicholas Piggin     }
13715a5d3b23SNicholas Piggin 
13729f338e4dSFabiano Rosas     switch (excp) {
13739f338e4dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
1374819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
13759f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
13769f338e4dSFabiano Rosas             /*
13779f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
13789f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR).
13799f338e4dSFabiano Rosas              */
13809f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
138143453161SNicholas Piggin 
138243453161SNicholas Piggin             /* HV machine check exceptions don't have ME set */
13839f338e4dSFabiano Rosas             new_msr &= ~((target_ulong)1 << MSR_ME);
138443453161SNicholas Piggin         }
13859f338e4dSFabiano Rosas 
138655a7fa34SNicholas Piggin         msr |= env->error_code;
13879f338e4dSFabiano Rosas         break;
138855a7fa34SNicholas Piggin 
13899f338e4dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
13909f338e4dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
13919f338e4dSFabiano Rosas         break;
13929f338e4dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
13939f338e4dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
13949f338e4dSFabiano Rosas         msr |= env->error_code;
13959f338e4dSFabiano Rosas         break;
13969f338e4dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
13979f338e4dSFabiano Rosas     {
13989f338e4dSFabiano Rosas         bool lpes0;
13999f338e4dSFabiano Rosas 
1400ab452503SBALATON Zoltan         /* LPES0 is only taken into consideration if we support HV mode */
140167baff77SFabiano Rosas         if (!env->has_hv_mode) {
140267baff77SFabiano Rosas             break;
14039f338e4dSFabiano Rosas         }
140467baff77SFabiano Rosas         lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
14059f338e4dSFabiano Rosas         if (!lpes0) {
14069f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14079f338e4dSFabiano Rosas             new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
14089f338e4dSFabiano Rosas             srr0 = SPR_HSRR0;
14099f338e4dSFabiano Rosas             srr1 = SPR_HSRR1;
14109f338e4dSFabiano Rosas         }
14119f338e4dSFabiano Rosas         break;
14129f338e4dSFabiano Rosas     }
14139f338e4dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
14144ee5d281SNicholas Piggin         /* Optional DSISR update was removed from ISA v3.0 */
14154ee5d281SNicholas Piggin         if (!(env->insns_flags2 & PPC2_ISA300)) {
14169f338e4dSFabiano Rosas             /* Get rS/rD and rA from faulting opcode */
14179f338e4dSFabiano Rosas             /*
14189f338e4dSFabiano Rosas              * Note: the opcode fields will not be set properly for a
14199f338e4dSFabiano Rosas              * direct store load/store, but nobody cares as nobody
14209f338e4dSFabiano Rosas              * actually uses direct store segments.
14219f338e4dSFabiano Rosas              */
14229f338e4dSFabiano Rosas             env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
14234ee5d281SNicholas Piggin         }
14249f338e4dSFabiano Rosas         break;
14259f338e4dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
14269f338e4dSFabiano Rosas         switch (env->error_code & ~0xF) {
14279f338e4dSFabiano Rosas         case POWERPC_EXCP_FP:
1428da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
14299f338e4dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
14303680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
14319f338e4dSFabiano Rosas                 return;
14329f338e4dSFabiano Rosas             }
14339f338e4dSFabiano Rosas             /*
1434ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
1435ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
14369f338e4dSFabiano Rosas              */
14379f338e4dSFabiano Rosas             msr |= 0x00100000;
14389f338e4dSFabiano Rosas             break;
14399f338e4dSFabiano Rosas         case POWERPC_EXCP_INVAL:
14409f338e4dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
14419f338e4dSFabiano Rosas             msr |= 0x00080000;
14429f338e4dSFabiano Rosas             break;
14439f338e4dSFabiano Rosas         case POWERPC_EXCP_PRIV:
14449f338e4dSFabiano Rosas             msr |= 0x00040000;
14459f338e4dSFabiano Rosas             break;
14469f338e4dSFabiano Rosas         case POWERPC_EXCP_TRAP:
14479f338e4dSFabiano Rosas             msr |= 0x00020000;
14489f338e4dSFabiano Rosas             break;
14499f338e4dSFabiano Rosas         default:
14509f338e4dSFabiano Rosas             /* Should never occur */
1451bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
14529f338e4dSFabiano Rosas                       env->error_code);
14539f338e4dSFabiano Rosas             break;
14549f338e4dSFabiano Rosas         }
14559f338e4dSFabiano Rosas         break;
14569f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
14579f338e4dSFabiano Rosas         lev = env->error_code;
14589f338e4dSFabiano Rosas 
14592306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
14609f338e4dSFabiano Rosas             dump_hcall(env);
14619f338e4dSFabiano Rosas         } else {
14629f338e4dSFabiano Rosas             dump_syscall(env);
14639f338e4dSFabiano Rosas         }
14649f338e4dSFabiano Rosas 
14659f338e4dSFabiano Rosas         /*
14669f338e4dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
14679f338e4dSFabiano Rosas          * to point to the next instruction
14689f338e4dSFabiano Rosas          */
14699f338e4dSFabiano Rosas         env->nip += 4;
14709f338e4dSFabiano Rosas 
14719f338e4dSFabiano Rosas         /* "PAPR mode" built-in hypercall emulation */
14722306c606SBALATON Zoltan         if (lev == 1 && books_vhyp_handles_hcall(cpu)) {
1473c700b5e1SNicholas Piggin             cpu->vhyp_class->hypercall(cpu->vhyp, cpu);
14742c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
14759f338e4dSFabiano Rosas             return;
14769f338e4dSFabiano Rosas         }
1477eb701f30SNicholas Piggin         if (env->insns_flags2 & PPC2_ISA310) {
1478eb701f30SNicholas Piggin             /* ISAv3.1 puts LEV into SRR1 */
1479eb701f30SNicholas Piggin             msr |= lev << 20;
1480eb701f30SNicholas Piggin         }
14819f338e4dSFabiano Rosas         if (lev == 1) {
14829f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14839f338e4dSFabiano Rosas         }
14849f338e4dSFabiano Rosas         break;
14859f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL_VECTORED: /* scv exception                     */
14869f338e4dSFabiano Rosas         lev = env->error_code;
14879f338e4dSFabiano Rosas         dump_syscall(env);
14889f338e4dSFabiano Rosas         env->nip += 4;
14899f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_EE);
14909f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
14919f338e4dSFabiano Rosas 
14929f338e4dSFabiano Rosas         vector += lev * 0x20;
14939f338e4dSFabiano Rosas 
14949f338e4dSFabiano Rosas         env->lr = env->nip;
14959f338e4dSFabiano Rosas         env->ctr = msr;
14969f338e4dSFabiano Rosas         break;
14979f338e4dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
14989f338e4dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
14999f338e4dSFabiano Rosas         break;
15009f338e4dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
15019f338e4dSFabiano Rosas         /* A power-saving exception sets ME, otherwise it is unchanged */
15028e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
15039f338e4dSFabiano Rosas             /* indicate that we resumed from power save mode */
15049f338e4dSFabiano Rosas             msr |= 0x10000;
15059f338e4dSFabiano Rosas             new_msr |= ((target_ulong)1 << MSR_ME);
15069f338e4dSFabiano Rosas         }
15079f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
15089f338e4dSFabiano Rosas             /*
15099f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
15109f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR, NMI injection in QEMU).
15119f338e4dSFabiano Rosas              */
15129f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
15139f338e4dSFabiano Rosas         } else {
15148e54ad65SVíctor Colombo             if (FIELD_EX64(env->msr, MSR, POW)) {
1515bc30c1c6SBALATON Zoltan                 cpu_abort(env_cpu(env),
1516bc30c1c6SBALATON Zoltan                           "Trying to deliver power-saving system reset "
15179f338e4dSFabiano Rosas                           "exception %d with no HV support\n", excp);
15189f338e4dSFabiano Rosas             }
15199f338e4dSFabiano Rosas         }
15209f338e4dSFabiano Rosas         break;
152114895384SNicholas Piggin     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
152214895384SNicholas Piggin         msr |= env->error_code;
152314895384SNicholas Piggin         /* fall through */
15249f338e4dSFabiano Rosas     case POWERPC_EXCP_DSEG:      /* Data segment exception                   */
15259f338e4dSFabiano Rosas     case POWERPC_EXCP_ISEG:      /* Instruction segment exception            */
1526fd7abfabSNicholas Piggin     case POWERPC_EXCP_SDOOR:     /* Doorbell interrupt                       */
1527c29b0704SNicholas Piggin     case POWERPC_EXCP_PERFM:     /* Performance monitor interrupt            */
15289f338e4dSFabiano Rosas         break;
15299f338e4dSFabiano Rosas     case POWERPC_EXCP_HISI:      /* Hypervisor instruction storage exception */
15309f338e4dSFabiano Rosas         msr |= env->error_code;
15319f338e4dSFabiano Rosas         /* fall through */
15329f338e4dSFabiano Rosas     case POWERPC_EXCP_HDECR:     /* Hypervisor decrementer exception         */
15339f338e4dSFabiano Rosas     case POWERPC_EXCP_HDSI:      /* Hypervisor data storage exception        */
15349f338e4dSFabiano Rosas     case POWERPC_EXCP_SDOOR_HV:  /* Hypervisor Doorbell interrupt            */
15359f338e4dSFabiano Rosas     case POWERPC_EXCP_HVIRT:     /* Hypervisor virtualization                */
15369f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
15379f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
15389f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
15399f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
15409f338e4dSFabiano Rosas         break;
1541a3c020d8SNicholas Piggin #ifdef CONFIG_TCG
1542a3c020d8SNicholas Piggin     case POWERPC_EXCP_HV_EMU: {
1543a3c020d8SNicholas Piggin         uint32_t insn = ppc_ldl_code(env, env->nip);
1544a3c020d8SNicholas Piggin         env->spr[SPR_HEIR] = insn;
1545a3c020d8SNicholas Piggin         if (is_prefix_insn(env, insn)) {
1546a3c020d8SNicholas Piggin             uint32_t insn2 = ppc_ldl_code(env, env->nip + 4);
1547a3c020d8SNicholas Piggin             env->spr[SPR_HEIR] <<= 32;
1548a3c020d8SNicholas Piggin             env->spr[SPR_HEIR] |= insn2;
1549a3c020d8SNicholas Piggin         }
1550a3c020d8SNicholas Piggin         srr0 = SPR_HSRR0;
1551a3c020d8SNicholas Piggin         srr1 = SPR_HSRR1;
1552a3c020d8SNicholas Piggin         new_msr |= (target_ulong)MSR_HVB;
1553a3c020d8SNicholas Piggin         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
1554a3c020d8SNicholas Piggin         break;
1555a3c020d8SNicholas Piggin     }
1556a3c020d8SNicholas Piggin #endif
15579f338e4dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
15589f338e4dSFabiano Rosas     case POWERPC_EXCP_VSXU:       /* VSX unavailable exception               */
15599f338e4dSFabiano Rosas     case POWERPC_EXCP_FU:         /* Facility unavailable exception          */
15609f338e4dSFabiano Rosas         env->spr[SPR_FSCR] |= ((target_ulong)env->error_code << 56);
15619f338e4dSFabiano Rosas         break;
15629f338e4dSFabiano Rosas     case POWERPC_EXCP_HV_FU:     /* Hypervisor Facility Unavailable Exception */
15639f338e4dSFabiano Rosas         env->spr[SPR_HFSCR] |= ((target_ulong)env->error_code << FSCR_IC_POS);
15649f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
15659f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
15669f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
15679f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
15689f338e4dSFabiano Rosas         break;
1569cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_PERFM_EBB:        /* Performance Monitor EBB Exception  */
1570cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_EXTERNAL_EBB:     /* External EBB Exception             */
1571cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
1572cb76bbc4SDaniel Henrique Barboza 
1573cb76bbc4SDaniel Henrique Barboza         /*
1574cb76bbc4SDaniel Henrique Barboza          * Save NIP for rfebb insn in SPR_EBBRR. Next nip is
1575cb76bbc4SDaniel Henrique Barboza          * stored in the EBB Handler SPR_EBBHR.
1576cb76bbc4SDaniel Henrique Barboza          */
1577cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_EBBRR] = env->nip;
1578cb76bbc4SDaniel Henrique Barboza         powerpc_set_excp_state(cpu, env->spr[SPR_EBBHR], env->msr);
1579cb76bbc4SDaniel Henrique Barboza 
1580cb76bbc4SDaniel Henrique Barboza         /*
1581cb76bbc4SDaniel Henrique Barboza          * This exception is handled in userspace. No need to proceed.
1582cb76bbc4SDaniel Henrique Barboza          */
1583cb76bbc4SDaniel Henrique Barboza         return;
15849f338e4dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
15859f338e4dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
15869f338e4dSFabiano Rosas     case POWERPC_EXCP_MAINT:     /* Maintenance exception                    */
158730c4e426SFabiano Rosas     case POWERPC_EXCP_HV_MAINT:  /* Hypervisor Maintenance exception         */
1588bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
15899f338e4dSFabiano Rosas                   powerpc_excp_name(excp));
15909f338e4dSFabiano Rosas         break;
15919f338e4dSFabiano Rosas     default:
1592bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1593bc30c1c6SBALATON Zoltan                   excp);
15949f338e4dSFabiano Rosas         break;
15959f338e4dSFabiano Rosas     }
15969f338e4dSFabiano Rosas 
15979f338e4dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
15989f338e4dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
15999f338e4dSFabiano Rosas     }
16009f338e4dSFabiano Rosas     new_msr |= (target_ulong)1 << MSR_SF;
16019f338e4dSFabiano Rosas 
16029f338e4dSFabiano Rosas     if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
16039f338e4dSFabiano Rosas         env->spr[srr0] = env->nip;
16049f338e4dSFabiano Rosas         env->spr[srr1] = msr;
16059f338e4dSFabiano Rosas     }
16069f338e4dSFabiano Rosas 
16077cebc5dbSNicholas Piggin     if ((new_msr & MSR_HVB) && books_vhyp_handles_hv_excp(cpu)) {
16087cebc5dbSNicholas Piggin         /* Deliver interrupt to L1 by returning from the H_ENTER_NESTED call */
1609c700b5e1SNicholas Piggin         cpu->vhyp_class->deliver_hv_excp(cpu, excp);
16107cebc5dbSNicholas Piggin         powerpc_reset_excp_state(cpu);
16117cebc5dbSNicholas Piggin     } else {
16127cebc5dbSNicholas Piggin         /* Sanity check */
16137cebc5dbSNicholas Piggin         if (!(env->msr_mask & MSR_HVB) && srr0 == SPR_HSRR0) {
1614bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Trying to deliver HV exception (HSRR) %d "
1615bc30c1c6SBALATON Zoltan                       "with no HV support\n", excp);
16167cebc5dbSNicholas Piggin         }
16179f338e4dSFabiano Rosas         /* This can update new_msr and vector if AIL applies */
161810895ab6SFabiano Rosas         ppc_excp_apply_ail(cpu, excp, msr, &new_msr, &vector);
16199f338e4dSFabiano Rosas         powerpc_set_excp_state(cpu, vector, new_msr);
16209f338e4dSFabiano Rosas     }
16217cebc5dbSNicholas Piggin }
162230c4e426SFabiano Rosas #else
162330c4e426SFabiano Rosas static inline void powerpc_excp_books(PowerPCCPU *cpu, int excp)
162430c4e426SFabiano Rosas {
162530c4e426SFabiano Rosas     g_assert_not_reached();
162630c4e426SFabiano Rosas }
1627f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
16289f338e4dSFabiano Rosas 
1629dc88dd0aSFabiano Rosas static void powerpc_excp(PowerPCCPU *cpu, int excp)
1630dc88dd0aSFabiano Rosas {
1631dc88dd0aSFabiano Rosas     CPUPPCState *env = &cpu->env;
1632dc88dd0aSFabiano Rosas 
1633c6eaac89SFabiano Rosas     if (excp <= POWERPC_EXCP_NONE || excp >= POWERPC_EXCP_NB) {
1634bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1635bc30c1c6SBALATON Zoltan                   excp);
1636c6eaac89SFabiano Rosas     }
1637c6eaac89SFabiano Rosas 
1638c6eaac89SFabiano Rosas     qemu_log_mask(CPU_LOG_INT, "Raise exception at " TARGET_FMT_lx
1639c6eaac89SFabiano Rosas                   " => %s (%d) error=%02x\n", env->nip, powerpc_excp_name(excp),
1640c6eaac89SFabiano Rosas                   excp, env->error_code);
16418a15cceeSBALATON Zoltan     env->excp_stats[excp]++;
1642c6eaac89SFabiano Rosas 
1643dc88dd0aSFabiano Rosas     switch (env->excp_model) {
1644e808c2edSFabiano Rosas     case POWERPC_EXCP_40x:
1645e808c2edSFabiano Rosas         powerpc_excp_40x(cpu, excp);
1646e808c2edSFabiano Rosas         break;
164758d178fbSFabiano Rosas     case POWERPC_EXCP_6xx:
164858d178fbSFabiano Rosas         powerpc_excp_6xx(cpu, excp);
164958d178fbSFabiano Rosas         break;
1650ccfca2fcSFabiano Rosas     case POWERPC_EXCP_7xx:
1651ccfca2fcSFabiano Rosas         powerpc_excp_7xx(cpu, excp);
1652ccfca2fcSFabiano Rosas         break;
165352926b0dSFabiano Rosas     case POWERPC_EXCP_74xx:
165452926b0dSFabiano Rosas         powerpc_excp_74xx(cpu, excp);
165552926b0dSFabiano Rosas         break;
1656180952ceSFabiano Rosas     case POWERPC_EXCP_BOOKE:
1657180952ceSFabiano Rosas         powerpc_excp_booke(cpu, excp);
1658180952ceSFabiano Rosas         break;
16599f338e4dSFabiano Rosas     case POWERPC_EXCP_970:
16609f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER7:
16619f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER8:
16629f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER9:
16639f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER10:
1664c0d96407SAditya Gupta     case POWERPC_EXCP_POWER11:
16659f338e4dSFabiano Rosas         powerpc_excp_books(cpu, excp);
16669f338e4dSFabiano Rosas         break;
1667dc88dd0aSFabiano Rosas     default:
166828091374SFabiano Rosas         g_assert_not_reached();
1669dc88dd0aSFabiano Rosas     }
1670dc88dd0aSFabiano Rosas }
1671dc88dd0aSFabiano Rosas 
167297a8ea5aSAndreas Färber void ppc_cpu_do_interrupt(CPUState *cs)
1673c79c73f6SBlue Swirl {
167497a8ea5aSAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
16755c26a5b3SAndreas Färber 
167693130c84SFabiano Rosas     powerpc_excp(cpu, cs->exception_index);
1677c79c73f6SBlue Swirl }
1678c79c73f6SBlue Swirl 
1679f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
1680c8e1de2eSMatheus Ferst #define P7_UNUSED_INTERRUPTS \
1681c8e1de2eSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_HVIRT | PPC_INTERRUPT_CEXT |       \
1682c8e1de2eSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |      \
1683c8e1de2eSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_DOORBELL | PPC_INTERRUPT_HDOORBELL | \
1684c8e1de2eSMatheus Ferst      PPC_INTERRUPT_THERM | PPC_INTERRUPT_EBB)
1685c8e1de2eSMatheus Ferst 
16869c713713SMatheus Ferst static int p7_interrupt_powersave(CPUPPCState *env)
16879c713713SMatheus Ferst {
16889c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
16899c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE0)) {
16909c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
16919c713713SMatheus Ferst     }
16929c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
16939c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE1)) {
16949c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
16959c713713SMatheus Ferst     }
16969c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_MCK) &&
16979c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
16989c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
16999c713713SMatheus Ferst     }
17009c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HMI) &&
17019c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
17029c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
17039c713713SMatheus Ferst     }
17049c713713SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
17059c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
17069c713713SMatheus Ferst     }
17079c713713SMatheus Ferst     return 0;
17089c713713SMatheus Ferst }
17099c713713SMatheus Ferst 
1710bf303fb3SMatheus Ferst static int p7_next_unmasked_interrupt(CPUPPCState *env)
1711bf303fb3SMatheus Ferst {
17124e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
17134e6b7db2SBALATON Zoltan 
1714022b7128SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
1715022b7128SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1716bf303fb3SMatheus Ferst 
1717c8e1de2eSMatheus Ferst     assert((env->pending_interrupts & P7_UNUSED_INTERRUPTS) == 0);
1718c8e1de2eSMatheus Ferst 
1719022b7128SMatheus Ferst     if (cs->halted) {
1720022b7128SMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
1721022b7128SMatheus Ferst         return p7_interrupt_powersave(env);
1722022b7128SMatheus Ferst     }
1723022b7128SMatheus Ferst 
1724bf303fb3SMatheus Ferst     /* Machine check exception */
1725bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1726bf303fb3SMatheus Ferst         return PPC_INTERRUPT_MCK;
1727bf303fb3SMatheus Ferst     }
1728bf303fb3SMatheus Ferst 
1729bf303fb3SMatheus Ferst     /* Hypervisor decrementer exception */
1730bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1731bf303fb3SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1732bf303fb3SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
1733022b7128SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1734bf303fb3SMatheus Ferst             /* HDEC clears on delivery */
1735bf303fb3SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1736bf303fb3SMatheus Ferst         }
1737bf303fb3SMatheus Ferst     }
1738bf303fb3SMatheus Ferst 
1739bf303fb3SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1740bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1741bf303fb3SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1742bf303fb3SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1743bf303fb3SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
1744022b7128SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1745bf303fb3SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1746bf303fb3SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1747bf303fb3SMatheus Ferst             return PPC_INTERRUPT_EXT;
1748bf303fb3SMatheus Ferst         }
1749bf303fb3SMatheus Ferst     }
1750022b7128SMatheus Ferst     if (msr_ee != 0) {
1751bf303fb3SMatheus Ferst         /* Decrementer exception */
1752bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1753bf303fb3SMatheus Ferst             return PPC_INTERRUPT_DECR;
1754bf303fb3SMatheus Ferst         }
1755bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1756bf303fb3SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1757bf303fb3SMatheus Ferst         }
1758bf303fb3SMatheus Ferst     }
1759bf303fb3SMatheus Ferst 
1760bf303fb3SMatheus Ferst     return 0;
1761bf303fb3SMatheus Ferst }
1762bf303fb3SMatheus Ferst 
1763f6194fddSMatheus Ferst #define P8_UNUSED_INTERRUPTS \
1764f6194fddSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_HVIRT |  \
1765f6194fddSMatheus Ferst     PPC_INTERRUPT_CEXT | PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL |  \
1766f6194fddSMatheus Ferst     PPC_INTERRUPT_FIT | PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1767f6194fddSMatheus Ferst 
1768*37e62398SHarsh Prateek Bora static int p8_interrupt_powersave(uint32_t pending_interrupts,
1769*37e62398SHarsh Prateek Bora                                   target_ulong lpcr)
17709c713713SMatheus Ferst {
1771*37e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_EXT) &&
1772*37e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE2)) {
17739c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
17749c713713SMatheus Ferst     }
1775*37e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DECR) &&
1776*37e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE3)) {
17779c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
17789c713713SMatheus Ferst     }
1779*37e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_MCK) &&
1780*37e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE4)) {
17819c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
17829c713713SMatheus Ferst     }
1783*37e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HMI) &&
1784*37e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE4)) {
17859c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
17869c713713SMatheus Ferst     }
1787*37e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
1788*37e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE0)) {
17899c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
17909c713713SMatheus Ferst     }
1791*37e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
1792*37e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE1)) {
17939c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
17949c713713SMatheus Ferst     }
1795*37e62398SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_RESET) {
17969c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
17979c713713SMatheus Ferst     }
17989c713713SMatheus Ferst     return 0;
17999c713713SMatheus Ferst }
18009c713713SMatheus Ferst 
1801*37e62398SHarsh Prateek Bora static int p8_next_unmasked_interrupt(CPUPPCState *env,
1802*37e62398SHarsh Prateek Bora                                       uint32_t pending_interrupts,
1803*37e62398SHarsh Prateek Bora                                       target_ulong lpcr)
1804a9899d42SMatheus Ferst {
18054e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
18064e6b7db2SBALATON Zoltan 
180764a9b5eeSMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
180864a9b5eeSMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1809a9899d42SMatheus Ferst 
1810f6194fddSMatheus Ferst     assert((env->pending_interrupts & P8_UNUSED_INTERRUPTS) == 0);
1811f6194fddSMatheus Ferst 
181264a9b5eeSMatheus Ferst     if (cs->halted) {
181364a9b5eeSMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
1814*37e62398SHarsh Prateek Bora         return p8_interrupt_powersave(pending_interrupts, lpcr);
181564a9b5eeSMatheus Ferst     }
181664a9b5eeSMatheus Ferst 
1817a9899d42SMatheus Ferst     /* Machine check exception */
1818*37e62398SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_MCK) {
1819a9899d42SMatheus Ferst         return PPC_INTERRUPT_MCK;
1820a9899d42SMatheus Ferst     }
1821a9899d42SMatheus Ferst 
1822a9899d42SMatheus Ferst     /* Hypervisor decrementer exception */
1823*37e62398SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HDECR) {
1824a9899d42SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1825*37e62398SHarsh Prateek Bora         bool hdice = !!(lpcr & LPCR_HDICE);
182664a9b5eeSMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1827a9899d42SMatheus Ferst             /* HDEC clears on delivery */
1828a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1829a9899d42SMatheus Ferst         }
1830a9899d42SMatheus Ferst     }
1831a9899d42SMatheus Ferst 
1832a9899d42SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1833*37e62398SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_EXT) {
1834*37e62398SHarsh Prateek Bora         bool lpes0 = !!(lpcr & LPCR_LPES0);
1835*37e62398SHarsh Prateek Bora         bool heic = !!(lpcr & LPCR_HEIC);
1836a9899d42SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
183764a9b5eeSMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1838a9899d42SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1839a9899d42SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1840a9899d42SMatheus Ferst             return PPC_INTERRUPT_EXT;
1841a9899d42SMatheus Ferst         }
1842a9899d42SMatheus Ferst     }
184364a9b5eeSMatheus Ferst     if (msr_ee != 0) {
1844a9899d42SMatheus Ferst         /* Decrementer exception */
1845*37e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DECR) {
1846a9899d42SMatheus Ferst             return PPC_INTERRUPT_DECR;
1847a9899d42SMatheus Ferst         }
1848*37e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1849a9899d42SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
1850a9899d42SMatheus Ferst         }
1851*37e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
1852a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
1853a9899d42SMatheus Ferst         }
1854*37e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_PERFM) {
1855a9899d42SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1856a9899d42SMatheus Ferst         }
1857a9899d42SMatheus Ferst         /* EBB exception */
1858*37e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_EBB) {
1859a9899d42SMatheus Ferst             /*
1860a9899d42SMatheus Ferst              * EBB exception must be taken in problem state and
1861a9899d42SMatheus Ferst              * with BESCR_GE set.
1862a9899d42SMatheus Ferst              */
1863a9899d42SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
1864a9899d42SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
1865a9899d42SMatheus Ferst                 return PPC_INTERRUPT_EBB;
1866a9899d42SMatheus Ferst             }
1867a9899d42SMatheus Ferst         }
1868a9899d42SMatheus Ferst     }
1869a9899d42SMatheus Ferst 
1870a9899d42SMatheus Ferst     return 0;
1871a9899d42SMatheus Ferst }
1872a9899d42SMatheus Ferst 
1873b00e9a2fSMatheus Ferst #define P9_UNUSED_INTERRUPTS \
1874b00e9a2fSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT |   \
1875b00e9a2fSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |  \
1876b00e9a2fSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1877b00e9a2fSMatheus Ferst 
18782a05a63cSHarsh Prateek Bora static int p9_interrupt_powersave(CPUPPCState *env,
18792a05a63cSHarsh Prateek Bora                                   uint32_t pending_interrupts,
18802a05a63cSHarsh Prateek Bora                                   target_ulong lpcr)
18819c713713SMatheus Ferst {
18822a05a63cSHarsh Prateek Bora 
18839c713713SMatheus Ferst     /* External Exception */
18842a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_EXT) &&
18852a05a63cSHarsh Prateek Bora         (lpcr & LPCR_EEE)) {
18862a05a63cSHarsh Prateek Bora         bool heic = !!(lpcr & LPCR_HEIC);
18879c713713SMatheus Ferst         if (!heic || !FIELD_EX64_HV(env->msr) ||
18889c713713SMatheus Ferst             FIELD_EX64(env->msr, MSR, PR)) {
18899c713713SMatheus Ferst             return PPC_INTERRUPT_EXT;
18909c713713SMatheus Ferst         }
18919c713713SMatheus Ferst     }
18929c713713SMatheus Ferst     /* Decrementer Exception */
18932a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DECR) &&
18942a05a63cSHarsh Prateek Bora         (lpcr & LPCR_DEE)) {
18959c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
18969c713713SMatheus Ferst     }
18979c713713SMatheus Ferst     /* Machine Check or Hypervisor Maintenance Exception */
18982a05a63cSHarsh Prateek Bora     if (lpcr & LPCR_OEE) {
18992a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_MCK) {
19009c713713SMatheus Ferst             return PPC_INTERRUPT_MCK;
19019c713713SMatheus Ferst         }
19022a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_HMI) {
19039c713713SMatheus Ferst             return PPC_INTERRUPT_HMI;
19049c713713SMatheus Ferst         }
19059c713713SMatheus Ferst     }
19069c713713SMatheus Ferst     /* Privileged Doorbell Exception */
19072a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
19082a05a63cSHarsh Prateek Bora         (lpcr & LPCR_PDEE)) {
19099c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
19109c713713SMatheus Ferst     }
19119c713713SMatheus Ferst     /* Hypervisor Doorbell Exception */
19122a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
19132a05a63cSHarsh Prateek Bora         (lpcr & LPCR_HDEE)) {
19149c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
19159c713713SMatheus Ferst     }
19169c713713SMatheus Ferst     /* Hypervisor virtualization exception */
19172a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HVIRT) &&
19182a05a63cSHarsh Prateek Bora         (lpcr & LPCR_HVEE)) {
19199c713713SMatheus Ferst         return PPC_INTERRUPT_HVIRT;
19209c713713SMatheus Ferst     }
19212a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_RESET) {
19229c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
19239c713713SMatheus Ferst     }
19249c713713SMatheus Ferst     return 0;
19259c713713SMatheus Ferst }
19269c713713SMatheus Ferst 
19272a05a63cSHarsh Prateek Bora static int p9_next_unmasked_interrupt(CPUPPCState *env,
19282a05a63cSHarsh Prateek Bora                                       uint32_t pending_interrupts,
19292a05a63cSHarsh Prateek Bora                                       target_ulong lpcr)
19302dfecf01SMatheus Ferst {
19314e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
19324e6b7db2SBALATON Zoltan 
193327796411SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
193427796411SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
19352dfecf01SMatheus Ferst 
19362a05a63cSHarsh Prateek Bora     assert((pending_interrupts & P9_UNUSED_INTERRUPTS) == 0);
1937b00e9a2fSMatheus Ferst 
193827796411SMatheus Ferst     if (cs->halted) {
193927796411SMatheus Ferst         if (env->spr[SPR_PSSCR] & PSSCR_EC) {
194027796411SMatheus Ferst             /*
194127796411SMatheus Ferst              * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can
194227796411SMatheus Ferst              * wakeup the processor
194327796411SMatheus Ferst              */
19442a05a63cSHarsh Prateek Bora             return p9_interrupt_powersave(env, pending_interrupts, lpcr);
194527796411SMatheus Ferst         } else {
194627796411SMatheus Ferst             /*
194727796411SMatheus Ferst              * When it's clear, any system-caused exception exits power-saving
194827796411SMatheus Ferst              * mode, even the ones that gate on MSR[EE].
194927796411SMatheus Ferst              */
195027796411SMatheus Ferst             msr_ee = true;
195127796411SMatheus Ferst         }
195227796411SMatheus Ferst     }
195327796411SMatheus Ferst 
19542dfecf01SMatheus Ferst     /* Machine check exception */
19552a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_MCK) {
19562dfecf01SMatheus Ferst         return PPC_INTERRUPT_MCK;
19572dfecf01SMatheus Ferst     }
19582dfecf01SMatheus Ferst 
19592dfecf01SMatheus Ferst     /* Hypervisor decrementer exception */
19602a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HDECR) {
19612dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
19622a05a63cSHarsh Prateek Bora         bool hdice = !!(lpcr & LPCR_HDICE);
196327796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
19642dfecf01SMatheus Ferst             /* HDEC clears on delivery */
19652dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDECR;
19662dfecf01SMatheus Ferst         }
19672dfecf01SMatheus Ferst     }
19682dfecf01SMatheus Ferst 
19692dfecf01SMatheus Ferst     /* Hypervisor virtualization interrupt */
19702a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HVIRT) {
19712dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
19722a05a63cSHarsh Prateek Bora         bool hvice = !!(lpcr & LPCR_HVICE);
197327796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hvice) {
19742dfecf01SMatheus Ferst             return PPC_INTERRUPT_HVIRT;
19752dfecf01SMatheus Ferst         }
19762dfecf01SMatheus Ferst     }
19772dfecf01SMatheus Ferst 
19782dfecf01SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
19792a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_EXT) {
19802a05a63cSHarsh Prateek Bora         bool lpes0 = !!(lpcr & LPCR_LPES0);
19812a05a63cSHarsh Prateek Bora         bool heic = !!(lpcr & LPCR_HEIC);
19822dfecf01SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
198327796411SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
19842dfecf01SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
19852dfecf01SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
19862dfecf01SMatheus Ferst             return PPC_INTERRUPT_EXT;
19872dfecf01SMatheus Ferst         }
19882dfecf01SMatheus Ferst     }
198927796411SMatheus Ferst     if (msr_ee != 0) {
19902dfecf01SMatheus Ferst         /* Decrementer exception */
19912a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DECR) {
19922dfecf01SMatheus Ferst             return PPC_INTERRUPT_DECR;
19932dfecf01SMatheus Ferst         }
19942a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DOORBELL) {
19952dfecf01SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
19962dfecf01SMatheus Ferst         }
19972a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
19982dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
19992dfecf01SMatheus Ferst         }
20002a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_PERFM) {
20012dfecf01SMatheus Ferst             return PPC_INTERRUPT_PERFM;
20022dfecf01SMatheus Ferst         }
20032dfecf01SMatheus Ferst         /* EBB exception */
20042a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_EBB) {
20052dfecf01SMatheus Ferst             /*
20062dfecf01SMatheus Ferst              * EBB exception must be taken in problem state and
20072dfecf01SMatheus Ferst              * with BESCR_GE set.
20082dfecf01SMatheus Ferst              */
20092dfecf01SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
20102dfecf01SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
20112dfecf01SMatheus Ferst                 return PPC_INTERRUPT_EBB;
20122dfecf01SMatheus Ferst             }
20132dfecf01SMatheus Ferst         }
20142dfecf01SMatheus Ferst     }
20152dfecf01SMatheus Ferst 
20162dfecf01SMatheus Ferst     return 0;
20172dfecf01SMatheus Ferst }
2018f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
20192dfecf01SMatheus Ferst 
2020868cb6baSBALATON Zoltan static int ppc_next_unmasked_interrupt(CPUPPCState *env)
2021c79c73f6SBlue Swirl {
2022868cb6baSBALATON Zoltan #ifdef TARGET_PPC64
2023868cb6baSBALATON Zoltan     switch (env->excp_model) {
2024868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER7:
2025868cb6baSBALATON Zoltan         return p7_next_unmasked_interrupt(env);
2026868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER8:
2027*37e62398SHarsh Prateek Bora         return p8_next_unmasked_interrupt(env, env->pending_interrupts,
2028*37e62398SHarsh Prateek Bora                                           env->spr[SPR_LPCR]);
2029868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER9:
2030868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER10:
2031c0d96407SAditya Gupta     case POWERPC_EXCP_POWER11:
20322a05a63cSHarsh Prateek Bora         return p9_next_unmasked_interrupt(env, env->pending_interrupts,
20332a05a63cSHarsh Prateek Bora                                           env->spr[SPR_LPCR]);
2034868cb6baSBALATON Zoltan     default:
2035868cb6baSBALATON Zoltan         break;
2036868cb6baSBALATON Zoltan     }
2037868cb6baSBALATON Zoltan #endif
20383621e2c9SBenjamin Herrenschmidt     bool async_deliver;
2039259186a7SAndreas Färber 
2040c79c73f6SBlue Swirl     /* External reset */
2041f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
2042de76b85cSMatheus Ferst         return PPC_INTERRUPT_RESET;
2043c79c73f6SBlue Swirl     }
2044c79c73f6SBlue Swirl     /* Machine check exception */
2045f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
2046de76b85cSMatheus Ferst         return PPC_INTERRUPT_MCK;
2047c79c73f6SBlue Swirl     }
2048c79c73f6SBlue Swirl #if 0 /* TODO */
2049c79c73f6SBlue Swirl     /* External debug exception */
2050f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) {
2051de76b85cSMatheus Ferst         return PPC_INTERRUPT_DEBUG;
2052c79c73f6SBlue Swirl     }
2053c79c73f6SBlue Swirl #endif
20543621e2c9SBenjamin Herrenschmidt 
20553621e2c9SBenjamin Herrenschmidt     /*
20563621e2c9SBenjamin Herrenschmidt      * For interrupts that gate on MSR:EE, we need to do something a
20573621e2c9SBenjamin Herrenschmidt      * bit more subtle, as we need to let them through even when EE is
20583621e2c9SBenjamin Herrenschmidt      * clear when coming out of some power management states (in order
20593621e2c9SBenjamin Herrenschmidt      * for them to become a 0x100).
20603621e2c9SBenjamin Herrenschmidt      */
20610939b8f8SVíctor Colombo     async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
20623621e2c9SBenjamin Herrenschmidt 
2063c79c73f6SBlue Swirl     /* Hypervisor decrementer exception */
2064f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
20654b236b62SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
20664b236b62SBenjamin Herrenschmidt         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
20679de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
20684b236b62SBenjamin Herrenschmidt             /* HDEC clears on delivery */
2069de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDECR;
2070c79c73f6SBlue Swirl         }
2071c79c73f6SBlue Swirl     }
2072d8ce5fd6SBenjamin Herrenschmidt 
2073d8ce5fd6SBenjamin Herrenschmidt     /* Hypervisor virtualization interrupt */
2074f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
2075d8ce5fd6SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
2076d8ce5fd6SBenjamin Herrenschmidt         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
20779de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
2078de76b85cSMatheus Ferst             return PPC_INTERRUPT_HVIRT;
2079d8ce5fd6SBenjamin Herrenschmidt         }
2080d8ce5fd6SBenjamin Herrenschmidt     }
2081d8ce5fd6SBenjamin Herrenschmidt 
2082d8ce5fd6SBenjamin Herrenschmidt     /* External interrupt can ignore MSR:EE under some circumstances */
2083f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
2084d1dbe37cSBenjamin Herrenschmidt         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
20856eebe6dcSBenjamin Herrenschmidt         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
20866eebe6dcSBenjamin Herrenschmidt         /* HEIC blocks delivery to the hypervisor */
20879de754d3SVíctor Colombo         if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
2088d41ccf6eSVíctor Colombo             !FIELD_EX64(env->msr, MSR, PR))) ||
20899de754d3SVíctor Colombo             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
2090de76b85cSMatheus Ferst             return PPC_INTERRUPT_EXT;
2091d1dbe37cSBenjamin Herrenschmidt         }
2092d1dbe37cSBenjamin Herrenschmidt     }
2093acc861c2SVíctor Colombo     if (FIELD_EX64(env->msr, MSR, CE)) {
2094c79c73f6SBlue Swirl         /* External critical interrupt */
2095f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CEXT) {
2096de76b85cSMatheus Ferst             return PPC_INTERRUPT_CEXT;
2097c79c73f6SBlue Swirl         }
2098c79c73f6SBlue Swirl     }
20993621e2c9SBenjamin Herrenschmidt     if (async_deliver != 0) {
2100c79c73f6SBlue Swirl         /* Watchdog timer on embedded PowerPC */
2101f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_WDT) {
2102de76b85cSMatheus Ferst             return PPC_INTERRUPT_WDT;
2103c79c73f6SBlue Swirl         }
2104f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) {
2105de76b85cSMatheus Ferst             return PPC_INTERRUPT_CDOORBELL;
2106c79c73f6SBlue Swirl         }
2107c79c73f6SBlue Swirl         /* Fixed interval timer on embedded PowerPC */
2108f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_FIT) {
2109de76b85cSMatheus Ferst             return PPC_INTERRUPT_FIT;
2110c79c73f6SBlue Swirl         }
2111c79c73f6SBlue Swirl         /* Programmable interval timer on embedded PowerPC */
2112f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PIT) {
2113de76b85cSMatheus Ferst             return PPC_INTERRUPT_PIT;
2114c79c73f6SBlue Swirl         }
2115c79c73f6SBlue Swirl         /* Decrementer exception */
2116f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
2117de76b85cSMatheus Ferst             return PPC_INTERRUPT_DECR;
2118c79c73f6SBlue Swirl         }
2119f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2120de76b85cSMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
2121c79c73f6SBlue Swirl         }
2122f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
2123de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
21247af1e7b0SCédric Le Goater         }
2125f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
2126de76b85cSMatheus Ferst             return PPC_INTERRUPT_PERFM;
2127c79c73f6SBlue Swirl         }
2128c79c73f6SBlue Swirl         /* Thermal interrupt */
2129f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_THERM) {
2130de76b85cSMatheus Ferst             return PPC_INTERRUPT_THERM;
2131c79c73f6SBlue Swirl         }
2132cb76bbc4SDaniel Henrique Barboza         /* EBB exception */
2133f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
2134cb76bbc4SDaniel Henrique Barboza             /*
2135cb76bbc4SDaniel Henrique Barboza              * EBB exception must be taken in problem state and
2136cb76bbc4SDaniel Henrique Barboza              * with BESCR_GE set.
2137cb76bbc4SDaniel Henrique Barboza              */
2138d41ccf6eSVíctor Colombo             if (FIELD_EX64(env->msr, MSR, PR) &&
2139d41ccf6eSVíctor Colombo                 (env->spr[SPR_BESCR] & BESCR_GE)) {
2140de76b85cSMatheus Ferst                 return PPC_INTERRUPT_EBB;
2141de76b85cSMatheus Ferst             }
2142de76b85cSMatheus Ferst         }
2143de76b85cSMatheus Ferst     }
2144cb76bbc4SDaniel Henrique Barboza 
2145de76b85cSMatheus Ferst     return 0;
2146de76b85cSMatheus Ferst }
2147de76b85cSMatheus Ferst 
21482fdedcbcSMatheus Ferst /*
21492fdedcbcSMatheus Ferst  * Sets CPU_INTERRUPT_HARD if there is at least one unmasked interrupt to be
21502fdedcbcSMatheus Ferst  * delivered and clears CPU_INTERRUPT_HARD otherwise.
21512fdedcbcSMatheus Ferst  *
21522fdedcbcSMatheus Ferst  * This method is called by ppc_set_interrupt when an interrupt is raised or
21532fdedcbcSMatheus Ferst  * lowered, and should also be called whenever an interrupt masking condition
21542fdedcbcSMatheus Ferst  * is changed, e.g.:
21552fdedcbcSMatheus Ferst  *  - When relevant bits of MSR are altered, like EE, HV, PR, etc.;
21562fdedcbcSMatheus Ferst  *  - When relevant bits of LPCR are altered, like PECE, HDICE, HVICE, etc.;
21572fdedcbcSMatheus Ferst  *  - When PSSCR[EC] or env->resume_as_sreset are changed;
21582fdedcbcSMatheus Ferst  *  - When cs->halted is changed and the CPU has a different interrupt masking
21592fdedcbcSMatheus Ferst  *    logic in power-saving mode (e.g., POWER7/8/9/10);
21602fdedcbcSMatheus Ferst  */
21612fdedcbcSMatheus Ferst void ppc_maybe_interrupt(CPUPPCState *env)
21622fdedcbcSMatheus Ferst {
21632fdedcbcSMatheus Ferst     CPUState *cs = env_cpu(env);
216432ead8e6SStefan Hajnoczi     BQL_LOCK_GUARD();
21652fdedcbcSMatheus Ferst 
21662fdedcbcSMatheus Ferst     if (ppc_next_unmasked_interrupt(env)) {
21672fdedcbcSMatheus Ferst         cpu_interrupt(cs, CPU_INTERRUPT_HARD);
21682fdedcbcSMatheus Ferst     } else {
21692fdedcbcSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
21702fdedcbcSMatheus Ferst     }
21712fdedcbcSMatheus Ferst }
21722fdedcbcSMatheus Ferst 
2173f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2174d93a4856SMatheus Ferst static void p7_deliver_interrupt(CPUPPCState *env, int interrupt)
2175d93a4856SMatheus Ferst {
2176d93a4856SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2177d93a4856SMatheus Ferst 
2178d93a4856SMatheus Ferst     switch (interrupt) {
2179d93a4856SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2180d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2181d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2182d93a4856SMatheus Ferst         break;
2183d93a4856SMatheus Ferst 
2184d93a4856SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2185d93a4856SMatheus Ferst         /* HDEC clears on delivery */
2186d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2187d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2188d93a4856SMatheus Ferst         break;
2189d93a4856SMatheus Ferst 
2190d93a4856SMatheus Ferst     case PPC_INTERRUPT_EXT:
2191d93a4856SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2192d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2193d93a4856SMatheus Ferst         } else {
2194d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2195d93a4856SMatheus Ferst         }
2196d93a4856SMatheus Ferst         break;
2197d93a4856SMatheus Ferst 
2198d93a4856SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2199d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2200d93a4856SMatheus Ferst         break;
2201d93a4856SMatheus Ferst     case PPC_INTERRUPT_PERFM:
2202d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2203d93a4856SMatheus Ferst         break;
2204d93a4856SMatheus Ferst     case 0:
2205d93a4856SMatheus Ferst         /*
2206d93a4856SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
2207d93a4856SMatheus Ferst          * anything to deliver while in a PM state that requires getting
2208d93a4856SMatheus Ferst          * out via a 0x100
2209d93a4856SMatheus Ferst          *
2210d93a4856SMatheus Ferst          * This means we will incorrectly execute past the power management
2211d93a4856SMatheus Ferst          * instruction instead of triggering a reset.
2212d93a4856SMatheus Ferst          *
2213d93a4856SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
2214d93a4856SMatheus Ferst          * processor has_work implementation and the logic in this function.
2215d93a4856SMatheus Ferst          */
2216d93a4856SMatheus Ferst         assert(!env->resume_as_sreset);
2217d93a4856SMatheus Ferst         break;
2218d93a4856SMatheus Ferst     default:
2219bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2220bc30c1c6SBALATON Zoltan                   interrupt);
2221d93a4856SMatheus Ferst     }
2222d93a4856SMatheus Ferst }
2223d93a4856SMatheus Ferst 
22246527e757SMatheus Ferst static void p8_deliver_interrupt(CPUPPCState *env, int interrupt)
22256527e757SMatheus Ferst {
22266527e757SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
22276527e757SMatheus Ferst 
22286527e757SMatheus Ferst     switch (interrupt) {
22296527e757SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
22306527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
22316527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
22326527e757SMatheus Ferst         break;
22336527e757SMatheus Ferst 
22346527e757SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
22356527e757SMatheus Ferst         /* HDEC clears on delivery */
22366527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
22376527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
22386527e757SMatheus Ferst         break;
22396527e757SMatheus Ferst 
22406527e757SMatheus Ferst     case PPC_INTERRUPT_EXT:
22416527e757SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
22426527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
22436527e757SMatheus Ferst         } else {
22446527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
22456527e757SMatheus Ferst         }
22466527e757SMatheus Ferst         break;
22476527e757SMatheus Ferst 
22486527e757SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
22496527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
22506527e757SMatheus Ferst         break;
22516527e757SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
225206229545SNicholas Piggin         if (!env->resume_as_sreset) {
22536527e757SMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
225406229545SNicholas Piggin         }
22556527e757SMatheus Ferst         if (is_book3s_arch2x(env)) {
22566527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
22576527e757SMatheus Ferst         } else {
22586527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
22596527e757SMatheus Ferst         }
22606527e757SMatheus Ferst         break;
22616527e757SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
226206229545SNicholas Piggin         if (!env->resume_as_sreset) {
22636527e757SMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
226406229545SNicholas Piggin         }
22656527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
22666527e757SMatheus Ferst         break;
22676527e757SMatheus Ferst     case PPC_INTERRUPT_PERFM:
22686527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
22696527e757SMatheus Ferst         break;
22706527e757SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
22716527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
22726527e757SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
22736527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
22746527e757SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
22756527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
22766527e757SMatheus Ferst         }
22776527e757SMatheus Ferst         break;
22786527e757SMatheus Ferst     case 0:
22796527e757SMatheus Ferst         /*
22806527e757SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
22816527e757SMatheus Ferst          * anything to deliver while in a PM state that requires getting
22826527e757SMatheus Ferst          * out via a 0x100
22836527e757SMatheus Ferst          *
22846527e757SMatheus Ferst          * This means we will incorrectly execute past the power management
22856527e757SMatheus Ferst          * instruction instead of triggering a reset.
22866527e757SMatheus Ferst          *
22876527e757SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
22886527e757SMatheus Ferst          * processor has_work implementation and the logic in this function.
22896527e757SMatheus Ferst          */
22906527e757SMatheus Ferst         assert(!env->resume_as_sreset);
22916527e757SMatheus Ferst         break;
22926527e757SMatheus Ferst     default:
2293bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2294bc30c1c6SBALATON Zoltan                   interrupt);
22956527e757SMatheus Ferst     }
22966527e757SMatheus Ferst }
22976527e757SMatheus Ferst 
22983654e238SMatheus Ferst static void p9_deliver_interrupt(CPUPPCState *env, int interrupt)
22993654e238SMatheus Ferst {
23003654e238SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
23013654e238SMatheus Ferst     CPUState *cs = env_cpu(env);
23023654e238SMatheus Ferst 
230327796411SMatheus Ferst     if (cs->halted && !(env->spr[SPR_PSSCR] & PSSCR_EC) &&
230427796411SMatheus Ferst         !FIELD_EX64(env->msr, MSR, EE)) {
230527796411SMatheus Ferst         /*
230627796411SMatheus Ferst          * A pending interrupt took us out of power-saving, but MSR[EE] says
230727796411SMatheus Ferst          * that we should return to NIP+4 instead of delivering it.
230827796411SMatheus Ferst          */
230927796411SMatheus Ferst         return;
231027796411SMatheus Ferst     }
231127796411SMatheus Ferst 
23123654e238SMatheus Ferst     switch (interrupt) {
23133654e238SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
23143654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
23153654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
23163654e238SMatheus Ferst         break;
23173654e238SMatheus Ferst 
23183654e238SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
23193654e238SMatheus Ferst         /* HDEC clears on delivery */
232006229545SNicholas Piggin         /* XXX: should not see an HDEC if resume_as_sreset. assert? */
23213654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
23223654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
23233654e238SMatheus Ferst         break;
23243654e238SMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
23253654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
23263654e238SMatheus Ferst         break;
23273654e238SMatheus Ferst 
23283654e238SMatheus Ferst     case PPC_INTERRUPT_EXT:
23293654e238SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
23303654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
23313654e238SMatheus Ferst         } else {
23323654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
23333654e238SMatheus Ferst         }
23343654e238SMatheus Ferst         break;
23353654e238SMatheus Ferst 
23363654e238SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
23373654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
23383654e238SMatheus Ferst         break;
23393654e238SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
234006229545SNicholas Piggin         if (!env->resume_as_sreset) {
23413654e238SMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
234206229545SNicholas Piggin         }
23433654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
23443654e238SMatheus Ferst         break;
23453654e238SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
234606229545SNicholas Piggin         if (!env->resume_as_sreset) {
23473654e238SMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
234806229545SNicholas Piggin         }
23493654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
23503654e238SMatheus Ferst         break;
23513654e238SMatheus Ferst     case PPC_INTERRUPT_PERFM:
23523654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
23533654e238SMatheus Ferst         break;
23543654e238SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
23553654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
23563654e238SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
23573654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
23583654e238SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
23593654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
23603654e238SMatheus Ferst         }
23613654e238SMatheus Ferst         break;
23623654e238SMatheus Ferst     case 0:
23633654e238SMatheus Ferst         /*
23643654e238SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
23653654e238SMatheus Ferst          * anything to deliver while in a PM state that requires getting
23663654e238SMatheus Ferst          * out via a 0x100
23673654e238SMatheus Ferst          *
23683654e238SMatheus Ferst          * This means we will incorrectly execute past the power management
23693654e238SMatheus Ferst          * instruction instead of triggering a reset.
23703654e238SMatheus Ferst          *
23713654e238SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
23723654e238SMatheus Ferst          * processor has_work implementation and the logic in this function.
23733654e238SMatheus Ferst          */
23743654e238SMatheus Ferst         assert(!env->resume_as_sreset);
23753654e238SMatheus Ferst         break;
23763654e238SMatheus Ferst     default:
2377bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2378bc30c1c6SBALATON Zoltan                   interrupt);
23793654e238SMatheus Ferst     }
23803654e238SMatheus Ferst }
2381f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
23823654e238SMatheus Ferst 
2383868cb6baSBALATON Zoltan static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt)
2384de76b85cSMatheus Ferst {
2385868cb6baSBALATON Zoltan #ifdef TARGET_PPC64
2386868cb6baSBALATON Zoltan     switch (env->excp_model) {
2387868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER7:
2388868cb6baSBALATON Zoltan         return p7_deliver_interrupt(env, interrupt);
2389868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER8:
2390868cb6baSBALATON Zoltan         return p8_deliver_interrupt(env, interrupt);
2391868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER9:
2392868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER10:
2393c0d96407SAditya Gupta     case POWERPC_EXCP_POWER11:
2394868cb6baSBALATON Zoltan         return p9_deliver_interrupt(env, interrupt);
2395868cb6baSBALATON Zoltan     default:
2396868cb6baSBALATON Zoltan         break;
2397868cb6baSBALATON Zoltan     }
2398de76b85cSMatheus Ferst #endif
2399de76b85cSMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2400de76b85cSMatheus Ferst 
2401de76b85cSMatheus Ferst     switch (interrupt) {
2402de76b85cSMatheus Ferst     case PPC_INTERRUPT_RESET: /* External reset */
2403de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_RESET;
2404de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_RESET);
2405de76b85cSMatheus Ferst         break;
2406de76b85cSMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2407de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2408de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2409de76b85cSMatheus Ferst         break;
2410de76b85cSMatheus Ferst 
2411de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2412de76b85cSMatheus Ferst         /* HDEC clears on delivery */
2413de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2414de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2415de76b85cSMatheus Ferst         break;
2416de76b85cSMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
2417de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2418de76b85cSMatheus Ferst         break;
2419de76b85cSMatheus Ferst 
2420de76b85cSMatheus Ferst     case PPC_INTERRUPT_EXT:
2421de76b85cSMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2422de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2423de76b85cSMatheus Ferst         } else {
2424de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2425de76b85cSMatheus Ferst         }
2426de76b85cSMatheus Ferst         break;
2427de76b85cSMatheus Ferst     case PPC_INTERRUPT_CEXT: /* External critical interrupt */
2428de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_CRITICAL);
2429de76b85cSMatheus Ferst         break;
2430de76b85cSMatheus Ferst 
2431de76b85cSMatheus Ferst     case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */
2432de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_WDT;
2433de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_WDT);
2434de76b85cSMatheus Ferst         break;
2435de76b85cSMatheus Ferst     case PPC_INTERRUPT_CDOORBELL:
2436de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL;
2437de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DOORCI);
2438de76b85cSMatheus Ferst         break;
2439de76b85cSMatheus Ferst     case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */
2440de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_FIT;
2441de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_FIT);
2442de76b85cSMatheus Ferst         break;
2443de76b85cSMatheus Ferst     case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */
2444de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PIT;
2445de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PIT);
2446de76b85cSMatheus Ferst         break;
2447de76b85cSMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2448de76b85cSMatheus Ferst         if (ppc_decr_clear_on_delivery(env)) {
2449de76b85cSMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DECR;
2450de76b85cSMatheus Ferst         }
2451de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2452de76b85cSMatheus Ferst         break;
2453de76b85cSMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
2454de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
2455de76b85cSMatheus Ferst         if (is_book3s_arch2x(env)) {
2456de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
2457de76b85cSMatheus Ferst         } else {
2458de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
2459de76b85cSMatheus Ferst         }
2460de76b85cSMatheus Ferst         break;
2461de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
2462de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
2463de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
2464de76b85cSMatheus Ferst         break;
2465de76b85cSMatheus Ferst     case PPC_INTERRUPT_PERFM:
2466de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2467de76b85cSMatheus Ferst         break;
2468de76b85cSMatheus Ferst     case PPC_INTERRUPT_THERM:  /* Thermal interrupt */
2469de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_THERM;
2470de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_THERM);
2471de76b85cSMatheus Ferst         break;
2472de76b85cSMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
2473de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
2474cb76bbc4SDaniel Henrique Barboza         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
2475cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
2476cb76bbc4SDaniel Henrique Barboza         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
2477cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
2478cb76bbc4SDaniel Henrique Barboza         }
2479de76b85cSMatheus Ferst         break;
2480de76b85cSMatheus Ferst     case 0:
2481f8154fd2SBenjamin Herrenschmidt         /*
2482f8154fd2SBenjamin Herrenschmidt          * This is a bug ! It means that has_work took us out of halt without
2483f8154fd2SBenjamin Herrenschmidt          * anything to deliver while in a PM state that requires getting
2484f8154fd2SBenjamin Herrenschmidt          * out via a 0x100
2485f8154fd2SBenjamin Herrenschmidt          *
2486f8154fd2SBenjamin Herrenschmidt          * This means we will incorrectly execute past the power management
2487f8154fd2SBenjamin Herrenschmidt          * instruction instead of triggering a reset.
2488f8154fd2SBenjamin Herrenschmidt          *
2489136fbf65Szhaolichang          * It generally means a discrepancy between the wakeup conditions in the
2490f8154fd2SBenjamin Herrenschmidt          * processor has_work implementation and the logic in this function.
2491f8154fd2SBenjamin Herrenschmidt          */
2492de76b85cSMatheus Ferst         assert(!env->resume_as_sreset);
2493de76b85cSMatheus Ferst         break;
2494de76b85cSMatheus Ferst     default:
2495bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2496bc30c1c6SBALATON Zoltan                   interrupt);
2497ba2898f7SMatheus Ferst     }
2498ba2898f7SMatheus Ferst }
2499ba2898f7SMatheus Ferst 
2500b5b7f391SNicholas Piggin void ppc_cpu_do_system_reset(CPUState *cs)
250134316482SAlexey Kardashevskiy {
250234316482SAlexey Kardashevskiy     PowerPCCPU *cpu = POWERPC_CPU(cs);
250334316482SAlexey Kardashevskiy 
250493130c84SFabiano Rosas     powerpc_excp(cpu, POWERPC_EXCP_RESET);
250534316482SAlexey Kardashevskiy }
2506ad77c6caSNicholas Piggin 
2507ad77c6caSNicholas Piggin void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector)
2508ad77c6caSNicholas Piggin {
2509ad77c6caSNicholas Piggin     PowerPCCPU *cpu = POWERPC_CPU(cs);
2510ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
2511ad77c6caSNicholas Piggin     target_ulong msr = 0;
2512ad77c6caSNicholas Piggin 
2513ad77c6caSNicholas Piggin     /*
2514ad77c6caSNicholas Piggin      * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already
2515ad77c6caSNicholas Piggin      * been set by KVM.
2516ad77c6caSNicholas Piggin      */
2517ad77c6caSNicholas Piggin     msr = (1ULL << MSR_ME);
2518ad77c6caSNicholas Piggin     msr |= env->msr & (1ULL << MSR_SF);
2519516fc103SFabiano Rosas     if (ppc_interrupts_little_endian(cpu, false)) {
2520ad77c6caSNicholas Piggin         msr |= (1ULL << MSR_LE);
2521ad77c6caSNicholas Piggin     }
2522ad77c6caSNicholas Piggin 
25237cebc5dbSNicholas Piggin     /* Anything for nested required here? MSR[HV] bit? */
25247cebc5dbSNicholas Piggin 
2525ad77c6caSNicholas Piggin     powerpc_set_excp_state(cpu, vector, msr);
2526ad77c6caSNicholas Piggin }
2527c79c73f6SBlue Swirl 
2528458dd766SRichard Henderson bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
2529458dd766SRichard Henderson {
2530794511bcSPhilippe Mathieu-Daudé     CPUPPCState *env = cpu_env(cs);
2531de76b85cSMatheus Ferst     int interrupt;
2532458dd766SRichard Henderson 
2533de76b85cSMatheus Ferst     if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) {
2534de76b85cSMatheus Ferst         return false;
2535de76b85cSMatheus Ferst     }
2536de76b85cSMatheus Ferst 
2537de76b85cSMatheus Ferst     interrupt = ppc_next_unmasked_interrupt(env);
2538de76b85cSMatheus Ferst     if (interrupt == 0) {
2539de76b85cSMatheus Ferst         return false;
2540de76b85cSMatheus Ferst     }
2541de76b85cSMatheus Ferst 
2542de76b85cSMatheus Ferst     ppc_deliver_interrupt(env, interrupt);
2543458dd766SRichard Henderson     if (env->pending_interrupts == 0) {
2544de76b85cSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
2545458dd766SRichard Henderson     }
2546458dd766SRichard Henderson     return true;
2547458dd766SRichard Henderson }
2548458dd766SRichard Henderson 
2549f725245cSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
2550f725245cSPhilippe Mathieu-Daudé 
2551ad71ed68SBlue Swirl /*****************************************************************************/
2552ad71ed68SBlue Swirl /* Exceptions processing helpers */
2553ad71ed68SBlue Swirl 
2554db789c6cSBenjamin Herrenschmidt void raise_exception_err_ra(CPUPPCState *env, uint32_t exception,
2555db789c6cSBenjamin Herrenschmidt                             uint32_t error_code, uintptr_t raddr)
2556ad71ed68SBlue Swirl {
2557db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
255827103424SAndreas Färber 
255927103424SAndreas Färber     cs->exception_index = exception;
2560ad71ed68SBlue Swirl     env->error_code = error_code;
2561db789c6cSBenjamin Herrenschmidt     cpu_loop_exit_restore(cs, raddr);
2562db789c6cSBenjamin Herrenschmidt }
2563db789c6cSBenjamin Herrenschmidt 
2564db789c6cSBenjamin Herrenschmidt void raise_exception_err(CPUPPCState *env, uint32_t exception,
2565db789c6cSBenjamin Herrenschmidt                          uint32_t error_code)
2566db789c6cSBenjamin Herrenschmidt {
2567db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2568db789c6cSBenjamin Herrenschmidt }
2569db789c6cSBenjamin Herrenschmidt 
2570db789c6cSBenjamin Herrenschmidt void raise_exception(CPUPPCState *env, uint32_t exception)
2571db789c6cSBenjamin Herrenschmidt {
2572db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2573db789c6cSBenjamin Herrenschmidt }
2574db789c6cSBenjamin Herrenschmidt 
2575db789c6cSBenjamin Herrenschmidt void raise_exception_ra(CPUPPCState *env, uint32_t exception,
2576db789c6cSBenjamin Herrenschmidt                         uintptr_t raddr)
2577db789c6cSBenjamin Herrenschmidt {
2578db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, raddr);
2579db789c6cSBenjamin Herrenschmidt }
2580db789c6cSBenjamin Herrenschmidt 
25812b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2582db789c6cSBenjamin Herrenschmidt void helper_raise_exception_err(CPUPPCState *env, uint32_t exception,
2583db789c6cSBenjamin Herrenschmidt                                 uint32_t error_code)
2584db789c6cSBenjamin Herrenschmidt {
2585db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2586ad71ed68SBlue Swirl }
2587ad71ed68SBlue Swirl 
2588e5f17ac6SBlue Swirl void helper_raise_exception(CPUPPCState *env, uint32_t exception)
2589ad71ed68SBlue Swirl {
2590db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2591ad71ed68SBlue Swirl }
2592ad71ed68SBlue Swirl 
2593f6c2d68bSBALATON Zoltan #ifndef CONFIG_USER_ONLY
2594e5f17ac6SBlue Swirl void helper_store_msr(CPUPPCState *env, target_ulong val)
2595ad71ed68SBlue Swirl {
2596db789c6cSBenjamin Herrenschmidt     uint32_t excp = hreg_store_msr(env, val, 0);
2597259186a7SAndreas Färber 
2598db789c6cSBenjamin Herrenschmidt     if (excp != 0) {
25990661329aSBALATON Zoltan         cpu_interrupt_exittb(env_cpu(env));
2600db789c6cSBenjamin Herrenschmidt         raise_exception(env, excp);
2601ad71ed68SBlue Swirl     }
2602ad71ed68SBlue Swirl }
2603ad71ed68SBlue Swirl 
26042fdedcbcSMatheus Ferst void helper_ppc_maybe_interrupt(CPUPPCState *env)
26052fdedcbcSMatheus Ferst {
26062fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
26072fdedcbcSMatheus Ferst }
26082fdedcbcSMatheus Ferst 
2609f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2610f43520e5SRichard Henderson void helper_scv(CPUPPCState *env, uint32_t lev)
2611f43520e5SRichard Henderson {
2612f43520e5SRichard Henderson     if (env->spr[SPR_FSCR] & (1ull << FSCR_SCV)) {
2613f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_SYSCALL_VECTORED, lev);
2614f43520e5SRichard Henderson     } else {
2615f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_FU, FSCR_IC_SCV);
2616f43520e5SRichard Henderson     }
2617f43520e5SRichard Henderson }
2618f43520e5SRichard Henderson 
261907e4804fSCédric Le Goater void helper_pminsn(CPUPPCState *env, uint32_t insn)
26207778a575SBenjamin Herrenschmidt {
26214e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
26227778a575SBenjamin Herrenschmidt 
26237778a575SBenjamin Herrenschmidt     cs->halted = 1;
26247778a575SBenjamin Herrenschmidt 
26253621e2c9SBenjamin Herrenschmidt     /* Condition for waking up at 0x100 */
26261e7fd61dSBenjamin Herrenschmidt     env->resume_as_sreset = (insn != PPC_PM_STOP) ||
262721c0d66aSBenjamin Herrenschmidt         (env->spr[SPR_PSSCR] & PSSCR_EC);
26282fdedcbcSMatheus Ferst 
26299915dac4SNicholas Piggin     /* HDECR is not to wake from PM state, it may have already fired */
26309915dac4SNicholas Piggin     if (env->resume_as_sreset) {
26319915dac4SNicholas Piggin         PowerPCCPU *cpu = env_archcpu(env);
26329915dac4SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 0);
26339915dac4SNicholas Piggin     }
26349915dac4SNicholas Piggin 
26352fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
26367778a575SBenjamin Herrenschmidt }
2637f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
26387778a575SBenjamin Herrenschmidt 
263962e79ef9SCédric Le Goater static void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr)
2640ad71ed68SBlue Swirl {
2641a2e71b28SBenjamin Herrenschmidt     /* MSR:POW cannot be set by any form of rfi */
2642a2e71b28SBenjamin Herrenschmidt     msr &= ~(1ULL << MSR_POW);
2643a2e71b28SBenjamin Herrenschmidt 
26445aad0457SChristophe Leroy     /* MSR:TGPR cannot be set by any form of rfi */
26455aad0457SChristophe Leroy     if (env->flags & POWERPC_FLAG_TGPR)
26465aad0457SChristophe Leroy         msr &= ~(1ULL << MSR_TGPR);
26475aad0457SChristophe Leroy 
2648f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2649a2e71b28SBenjamin Herrenschmidt     /* Switching to 32-bit ? Crop the nip */
2650a2e71b28SBenjamin Herrenschmidt     if (!msr_is_64bit(env, msr)) {
2651ad71ed68SBlue Swirl         nip = (uint32_t)nip;
2652ad71ed68SBlue Swirl     }
2653ad71ed68SBlue Swirl #else
2654ad71ed68SBlue Swirl     nip = (uint32_t)nip;
2655ad71ed68SBlue Swirl #endif
2656ad71ed68SBlue Swirl     /* XXX: beware: this is false if VLE is supported */
2657ad71ed68SBlue Swirl     env->nip = nip & ~((target_ulong)0x00000003);
2658ad71ed68SBlue Swirl     hreg_store_msr(env, msr, 1);
26592eb1ef73SCédric Le Goater     trace_ppc_excp_rfi(env->nip, env->msr);
266047733729SDavid Gibson     /*
266147733729SDavid Gibson      * No need to raise an exception here, as rfi is always the last
266247733729SDavid Gibson      * insn of a TB
2663ad71ed68SBlue Swirl      */
26640661329aSBALATON Zoltan     cpu_interrupt_exittb(env_cpu(env));
2665a8b73734SNikunj A Dadhania     /* Reset the reservation */
2666a8b73734SNikunj A Dadhania     env->reserve_addr = -1;
2667a8b73734SNikunj A Dadhania 
2668cd0c6f47SBenjamin Herrenschmidt     /* Context synchronizing: check if TCG TLB needs flush */
2669e3cffe6fSNikunj A Dadhania     check_tlb_flush(env, false);
2670ad71ed68SBlue Swirl }
2671ad71ed68SBlue Swirl 
2672e5f17ac6SBlue Swirl void helper_rfi(CPUPPCState *env)
2673ad71ed68SBlue Swirl {
2674a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful);
2675a1bb7384SScott Wood }
2676ad71ed68SBlue Swirl 
2677f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2678e5f17ac6SBlue Swirl void helper_rfid(CPUPPCState *env)
2679ad71ed68SBlue Swirl {
268047733729SDavid Gibson     /*
2681136fbf65Szhaolichang      * The architecture defines a number of rules for which bits can
268247733729SDavid Gibson      * change but in practice, we handle this in hreg_store_msr()
2683a2e71b28SBenjamin Herrenschmidt      * which will be called by do_rfi(), so there is no need to filter
2684a2e71b28SBenjamin Herrenschmidt      * here
2685a2e71b28SBenjamin Herrenschmidt      */
2686a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]);
2687ad71ed68SBlue Swirl }
2688ad71ed68SBlue Swirl 
26893c89b8d6SNicholas Piggin void helper_rfscv(CPUPPCState *env)
26903c89b8d6SNicholas Piggin {
26913c89b8d6SNicholas Piggin     do_rfi(env, env->lr, env->ctr);
26923c89b8d6SNicholas Piggin }
26933c89b8d6SNicholas Piggin 
2694e5f17ac6SBlue Swirl void helper_hrfid(CPUPPCState *env)
2695ad71ed68SBlue Swirl {
2696a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
2697ad71ed68SBlue Swirl }
2698ad71ed68SBlue Swirl 
26991f26c751SDaniel Henrique Barboza void helper_rfebb(CPUPPCState *env, target_ulong s)
27001f26c751SDaniel Henrique Barboza {
27011f26c751SDaniel Henrique Barboza     target_ulong msr = env->msr;
27021f26c751SDaniel Henrique Barboza 
27031f26c751SDaniel Henrique Barboza     /*
27041f26c751SDaniel Henrique Barboza      * Handling of BESCR bits 32:33 according to PowerISA v3.1:
27051f26c751SDaniel Henrique Barboza      *
27061f26c751SDaniel Henrique Barboza      * "If BESCR 32:33 != 0b00 the instruction is treated as if
27071f26c751SDaniel Henrique Barboza      *  the instruction form were invalid."
27081f26c751SDaniel Henrique Barboza      */
27091f26c751SDaniel Henrique Barboza     if (env->spr[SPR_BESCR] & BESCR_INVALID) {
27101f26c751SDaniel Henrique Barboza         raise_exception_err(env, POWERPC_EXCP_PROGRAM,
27111f26c751SDaniel Henrique Barboza                             POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
27121f26c751SDaniel Henrique Barboza     }
27131f26c751SDaniel Henrique Barboza 
27141f26c751SDaniel Henrique Barboza     env->nip = env->spr[SPR_EBBRR];
27151f26c751SDaniel Henrique Barboza 
27161f26c751SDaniel Henrique Barboza     /* Switching to 32-bit ? Crop the nip */
27171f26c751SDaniel Henrique Barboza     if (!msr_is_64bit(env, msr)) {
27181f26c751SDaniel Henrique Barboza         env->nip = (uint32_t)env->spr[SPR_EBBRR];
27191f26c751SDaniel Henrique Barboza     }
27201f26c751SDaniel Henrique Barboza 
27211f26c751SDaniel Henrique Barboza     if (s) {
27221f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_GE;
27231f26c751SDaniel Henrique Barboza     } else {
27241f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
27251f26c751SDaniel Henrique Barboza     }
27261f26c751SDaniel Henrique Barboza }
2727d3412df2SDaniel Henrique Barboza 
2728d3412df2SDaniel Henrique Barboza /*
2729d3412df2SDaniel Henrique Barboza  * Triggers or queues an 'ebb_excp' EBB exception. All checks
2730d3412df2SDaniel Henrique Barboza  * but FSCR, HFSCR and msr_pr must be done beforehand.
2731d3412df2SDaniel Henrique Barboza  *
2732d3412df2SDaniel Henrique Barboza  * PowerISA v3.1 isn't clear about whether an EBB should be
2733d3412df2SDaniel Henrique Barboza  * postponed or cancelled if the EBB facility is unavailable.
2734d3412df2SDaniel Henrique Barboza  * Our assumption here is that the EBB is cancelled if both
2735d3412df2SDaniel Henrique Barboza  * FSCR and HFSCR EBB facilities aren't available.
2736d3412df2SDaniel Henrique Barboza  */
2737d3412df2SDaniel Henrique Barboza static void do_ebb(CPUPPCState *env, int ebb_excp)
2738d3412df2SDaniel Henrique Barboza {
2739d3412df2SDaniel Henrique Barboza     PowerPCCPU *cpu = env_archcpu(env);
2740d3412df2SDaniel Henrique Barboza 
2741d3412df2SDaniel Henrique Barboza     /*
2742d3412df2SDaniel Henrique Barboza      * FSCR_EBB and FSCR_IC_EBB are the same bits used with
2743d3412df2SDaniel Henrique Barboza      * HFSCR.
2744d3412df2SDaniel Henrique Barboza      */
2745d3412df2SDaniel Henrique Barboza     helper_fscr_facility_check(env, FSCR_EBB, 0, FSCR_IC_EBB);
2746d3412df2SDaniel Henrique Barboza     helper_hfscr_facility_check(env, FSCR_EBB, "EBB", FSCR_IC_EBB);
2747d3412df2SDaniel Henrique Barboza 
2748d3412df2SDaniel Henrique Barboza     if (ebb_excp == POWERPC_EXCP_PERFM_EBB) {
2749d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_PMEO;
2750d3412df2SDaniel Henrique Barboza     } else if (ebb_excp == POWERPC_EXCP_EXTERNAL_EBB) {
2751d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_EEO;
2752d3412df2SDaniel Henrique Barboza     }
2753d3412df2SDaniel Henrique Barboza 
2754d41ccf6eSVíctor Colombo     if (FIELD_EX64(env->msr, MSR, PR)) {
2755d3412df2SDaniel Henrique Barboza         powerpc_excp(cpu, ebb_excp);
2756d3412df2SDaniel Henrique Barboza     } else {
27577b694df6SMatheus Ferst         ppc_set_irq(cpu, PPC_INTERRUPT_EBB, 1);
2758d3412df2SDaniel Henrique Barboza     }
2759d3412df2SDaniel Henrique Barboza }
2760d3412df2SDaniel Henrique Barboza 
2761d3412df2SDaniel Henrique Barboza void raise_ebb_perfm_exception(CPUPPCState *env)
2762d3412df2SDaniel Henrique Barboza {
2763d3412df2SDaniel Henrique Barboza     bool perfm_ebb_enabled = env->spr[SPR_POWER_MMCR0] & MMCR0_EBE &&
2764d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_PME &&
2765d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_GE;
2766d3412df2SDaniel Henrique Barboza 
2767d3412df2SDaniel Henrique Barboza     if (!perfm_ebb_enabled) {
2768d3412df2SDaniel Henrique Barboza         return;
2769d3412df2SDaniel Henrique Barboza     }
2770d3412df2SDaniel Henrique Barboza 
2771d3412df2SDaniel Henrique Barboza     do_ebb(env, POWERPC_EXCP_PERFM_EBB);
2772d3412df2SDaniel Henrique Barboza }
2773754920c7SBALATON Zoltan #endif /* TARGET_PPC64 */
27741f26c751SDaniel Henrique Barboza 
2775ad71ed68SBlue Swirl /*****************************************************************************/
2776ad71ed68SBlue Swirl /* Embedded PowerPC specific helpers */
2777e5f17ac6SBlue Swirl void helper_40x_rfci(CPUPPCState *env)
2778ad71ed68SBlue Swirl {
2779a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]);
2780ad71ed68SBlue Swirl }
2781ad71ed68SBlue Swirl 
2782e5f17ac6SBlue Swirl void helper_rfci(CPUPPCState *env)
2783ad71ed68SBlue Swirl {
2784a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]);
2785ad71ed68SBlue Swirl }
2786ad71ed68SBlue Swirl 
2787e5f17ac6SBlue Swirl void helper_rfdi(CPUPPCState *env)
2788ad71ed68SBlue Swirl {
2789a1bb7384SScott Wood     /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
2790a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]);
2791ad71ed68SBlue Swirl }
2792ad71ed68SBlue Swirl 
2793e5f17ac6SBlue Swirl void helper_rfmci(CPUPPCState *env)
2794ad71ed68SBlue Swirl {
2795a1bb7384SScott Wood     /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
2796a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
2797ad71ed68SBlue Swirl }
27985ca958cfSBALATON Zoltan #endif /* !CONFIG_USER_ONLY */
2799ad71ed68SBlue Swirl 
2800ae556c6aSChinmay Rath void helper_TW(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2801e5f17ac6SBlue Swirl                uint32_t flags)
2802ad71ed68SBlue Swirl {
2803ad71ed68SBlue Swirl     if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
2804ad71ed68SBlue Swirl                   ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
2805ad71ed68SBlue Swirl                   ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
2806ad71ed68SBlue Swirl                   ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
2807ad71ed68SBlue Swirl                   ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
280872073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
280972073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2810ad71ed68SBlue Swirl     }
2811ad71ed68SBlue Swirl }
2812ad71ed68SBlue Swirl 
2813f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2814ae556c6aSChinmay Rath void helper_TD(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2815e5f17ac6SBlue Swirl                uint32_t flags)
2816ad71ed68SBlue Swirl {
2817ad71ed68SBlue Swirl     if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
2818ad71ed68SBlue Swirl                   ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
2819ad71ed68SBlue Swirl                   ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
2820ad71ed68SBlue Swirl                   ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
2821ad71ed68SBlue Swirl                   ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) {
282272073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
282372073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2824ad71ed68SBlue Swirl     }
2825ad71ed68SBlue Swirl }
2826f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
2827ad71ed68SBlue Swirl 
2828670f1da3SVíctor Colombo static uint32_t helper_SIMON_LIKE_32_64(uint32_t x, uint64_t key, uint32_t lane)
2829670f1da3SVíctor Colombo {
2830670f1da3SVíctor Colombo     const uint16_t c = 0xfffc;
2831670f1da3SVíctor Colombo     const uint64_t z0 = 0xfa2561cdf44ac398ULL;
2832670f1da3SVíctor Colombo     uint16_t z = 0, temp;
2833670f1da3SVíctor Colombo     uint16_t k[32], eff_k[32], xleft[33], xright[33], fxleft[32];
2834670f1da3SVíctor Colombo 
2835670f1da3SVíctor Colombo     for (int i = 3; i >= 0; i--) {
2836670f1da3SVíctor Colombo         k[i] = key & 0xffff;
2837670f1da3SVíctor Colombo         key >>= 16;
2838670f1da3SVíctor Colombo     }
2839670f1da3SVíctor Colombo     xleft[0] = x & 0xffff;
2840670f1da3SVíctor Colombo     xright[0] = (x >> 16) & 0xffff;
2841670f1da3SVíctor Colombo 
2842670f1da3SVíctor Colombo     for (int i = 0; i < 28; i++) {
2843670f1da3SVíctor Colombo         z = (z0 >> (63 - i)) & 1;
2844670f1da3SVíctor Colombo         temp = ror16(k[i + 3], 3) ^ k[i + 1];
2845670f1da3SVíctor Colombo         k[i + 4] = c ^ z ^ k[i] ^ temp ^ ror16(temp, 1);
2846670f1da3SVíctor Colombo     }
2847670f1da3SVíctor Colombo 
2848670f1da3SVíctor Colombo     for (int i = 0; i < 8; i++) {
2849670f1da3SVíctor Colombo         eff_k[4 * i + 0] = k[4 * i + ((0 + lane) % 4)];
2850670f1da3SVíctor Colombo         eff_k[4 * i + 1] = k[4 * i + ((1 + lane) % 4)];
2851670f1da3SVíctor Colombo         eff_k[4 * i + 2] = k[4 * i + ((2 + lane) % 4)];
2852670f1da3SVíctor Colombo         eff_k[4 * i + 3] = k[4 * i + ((3 + lane) % 4)];
2853670f1da3SVíctor Colombo     }
2854670f1da3SVíctor Colombo 
2855670f1da3SVíctor Colombo     for (int i = 0; i < 32; i++) {
2856670f1da3SVíctor Colombo         fxleft[i] = (rol16(xleft[i], 1) &
2857670f1da3SVíctor Colombo             rol16(xleft[i], 8)) ^ rol16(xleft[i], 2);
2858670f1da3SVíctor Colombo         xleft[i + 1] = xright[i] ^ fxleft[i] ^ eff_k[i];
2859670f1da3SVíctor Colombo         xright[i + 1] = xleft[i];
2860670f1da3SVíctor Colombo     }
2861670f1da3SVíctor Colombo 
2862670f1da3SVíctor Colombo     return (((uint32_t)xright[32]) << 16) | xleft[32];
2863670f1da3SVíctor Colombo }
2864670f1da3SVíctor Colombo 
2865670f1da3SVíctor Colombo static uint64_t hash_digest(uint64_t ra, uint64_t rb, uint64_t key)
2866670f1da3SVíctor Colombo {
2867670f1da3SVíctor Colombo     uint64_t stage0_h = 0ULL, stage0_l = 0ULL;
2868670f1da3SVíctor Colombo     uint64_t stage1_h, stage1_l;
2869670f1da3SVíctor Colombo 
2870670f1da3SVíctor Colombo     for (int i = 0; i < 4; i++) {
2871670f1da3SVíctor Colombo         stage0_h |= ror64(rb & 0xff, 8 * (2 * i + 1));
2872670f1da3SVíctor Colombo         stage0_h |= ((ra >> 32) & 0xff) << (8 * 2 * i);
2873670f1da3SVíctor Colombo         stage0_l |= ror64((rb >> 32) & 0xff, 8 * (2 * i + 1));
2874670f1da3SVíctor Colombo         stage0_l |= (ra & 0xff) << (8 * 2 * i);
2875670f1da3SVíctor Colombo         rb >>= 8;
2876670f1da3SVíctor Colombo         ra >>= 8;
2877670f1da3SVíctor Colombo     }
2878670f1da3SVíctor Colombo 
2879670f1da3SVíctor Colombo     stage1_h = (uint64_t)helper_SIMON_LIKE_32_64(stage0_h >> 32, key, 0) << 32;
2880670f1da3SVíctor Colombo     stage1_h |= helper_SIMON_LIKE_32_64(stage0_h, key, 1);
2881670f1da3SVíctor Colombo     stage1_l = (uint64_t)helper_SIMON_LIKE_32_64(stage0_l >> 32, key, 2) << 32;
2882670f1da3SVíctor Colombo     stage1_l |= helper_SIMON_LIKE_32_64(stage0_l, key, 3);
2883670f1da3SVíctor Colombo 
2884670f1da3SVíctor Colombo     return stage1_h ^ stage1_l;
2885670f1da3SVíctor Colombo }
2886670f1da3SVíctor Colombo 
28874091fabfSNicholas Miehlbradt static void do_hash(CPUPPCState *env, target_ulong ea, target_ulong ra,
28884091fabfSNicholas Miehlbradt                     target_ulong rb, uint64_t key, bool store)
28894091fabfSNicholas Miehlbradt {
28904091fabfSNicholas Miehlbradt     uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash;
28914091fabfSNicholas Miehlbradt 
28924091fabfSNicholas Miehlbradt     if (store) {
28934091fabfSNicholas Miehlbradt         cpu_stq_data_ra(env, ea, calculated_hash, GETPC());
28944091fabfSNicholas Miehlbradt     } else {
28954091fabfSNicholas Miehlbradt         loaded_hash = cpu_ldq_data_ra(env, ea, GETPC());
28964091fabfSNicholas Miehlbradt         if (loaded_hash != calculated_hash) {
28974091fabfSNicholas Miehlbradt             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
28984091fabfSNicholas Miehlbradt                 POWERPC_EXCP_TRAP, GETPC());
28994091fabfSNicholas Miehlbradt         }
29004091fabfSNicholas Miehlbradt     }
29014091fabfSNicholas Miehlbradt }
29024091fabfSNicholas Miehlbradt 
2903670f1da3SVíctor Colombo #include "qemu/guest-random.h"
2904670f1da3SVíctor Colombo 
29054091fabfSNicholas Miehlbradt #ifdef TARGET_PPC64
29064091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect)                             \
2907670f1da3SVíctor Colombo void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
2908670f1da3SVíctor Colombo                  target_ulong rb)                                             \
2909670f1da3SVíctor Colombo {                                                                             \
29104091fabfSNicholas Miehlbradt     if (env->msr & R_MSR_PR_MASK) {                                           \
29114091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_DEXCR] & R_DEXCR_PRO_##dexcr_aspect##_MASK ||      \
29124091fabfSNicholas Miehlbradt             env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK))       \
29134091fabfSNicholas Miehlbradt             return;                                                           \
29144091fabfSNicholas Miehlbradt     } else if (!(env->msr & R_MSR_HV_MASK)) {                                 \
29154091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_DEXCR] & R_DEXCR_PNH_##dexcr_aspect##_MASK ||      \
29164091fabfSNicholas Miehlbradt             env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK))       \
29174091fabfSNicholas Miehlbradt             return;                                                           \
29184091fabfSNicholas Miehlbradt     } else if (!(env->msr & R_MSR_S_MASK)) {                                  \
29194091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_HDEXCR] & R_HDEXCR_HNU_##dexcr_aspect##_MASK))     \
29204091fabfSNicholas Miehlbradt             return;                                                           \
29214091fabfSNicholas Miehlbradt     }                                                                         \
2922670f1da3SVíctor Colombo                                                                               \
29234091fabfSNicholas Miehlbradt     do_hash(env, ea, ra, rb, key, store);                                     \
2924670f1da3SVíctor Colombo }
29254091fabfSNicholas Miehlbradt #else
29264091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect)                             \
29274091fabfSNicholas Miehlbradt void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
29284091fabfSNicholas Miehlbradt                  target_ulong rb)                                             \
29294091fabfSNicholas Miehlbradt {                                                                             \
29304091fabfSNicholas Miehlbradt     do_hash(env, ea, ra, rb, key, store);                                     \
29314091fabfSNicholas Miehlbradt }
29324091fabfSNicholas Miehlbradt #endif /* TARGET_PPC64 */
2933670f1da3SVíctor Colombo 
29344091fabfSNicholas Miehlbradt HELPER_HASH(HASHST, env->spr[SPR_HASHKEYR], true, NPHIE)
29354091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHK, env->spr[SPR_HASHKEYR], false, NPHIE)
29364091fabfSNicholas Miehlbradt HELPER_HASH(HASHSTP, env->spr[SPR_HASHPKEYR], true, PHIE)
29374091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHKP, env->spr[SPR_HASHPKEYR], false, PHIE)
2938670f1da3SVíctor Colombo 
2939f6c2d68bSBALATON Zoltan #ifndef CONFIG_USER_ONLY
2940ad71ed68SBlue Swirl /* Embedded.Processor Control */
2941ad71ed68SBlue Swirl static int dbell2irq(target_ulong rb)
2942ad71ed68SBlue Swirl {
2943ad71ed68SBlue Swirl     int msg = rb & DBELL_TYPE_MASK;
2944ad71ed68SBlue Swirl     int irq = -1;
2945ad71ed68SBlue Swirl 
2946ad71ed68SBlue Swirl     switch (msg) {
2947ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL:
2948ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_DOORBELL;
2949ad71ed68SBlue Swirl         break;
2950ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL_CRIT:
2951ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_CDOORBELL;
2952ad71ed68SBlue Swirl         break;
2953ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL:
2954ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_CRIT:
2955ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_MC:
2956ad71ed68SBlue Swirl         /* XXX implement */
2957ad71ed68SBlue Swirl     default:
2958ad71ed68SBlue Swirl         break;
2959ad71ed68SBlue Swirl     }
2960ad71ed68SBlue Swirl 
2961ad71ed68SBlue Swirl     return irq;
2962ad71ed68SBlue Swirl }
2963ad71ed68SBlue Swirl 
2964e5f17ac6SBlue Swirl void helper_msgclr(CPUPPCState *env, target_ulong rb)
2965ad71ed68SBlue Swirl {
2966ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2967ad71ed68SBlue Swirl 
2968ad71ed68SBlue Swirl     if (irq < 0) {
2969ad71ed68SBlue Swirl         return;
2970ad71ed68SBlue Swirl     }
2971ad71ed68SBlue Swirl 
29727b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), irq, 0);
2973ad71ed68SBlue Swirl }
2974ad71ed68SBlue Swirl 
2975ad71ed68SBlue Swirl void helper_msgsnd(target_ulong rb)
2976ad71ed68SBlue Swirl {
2977ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2978ad71ed68SBlue Swirl     int pir = rb & DBELL_PIRTAG_MASK;
2979182735efSAndreas Färber     CPUState *cs;
2980ad71ed68SBlue Swirl 
2981ad71ed68SBlue Swirl     if (irq < 0) {
2982ad71ed68SBlue Swirl         return;
2983ad71ed68SBlue Swirl     }
2984ad71ed68SBlue Swirl 
2985195801d7SStefan Hajnoczi     bql_lock();
2986bdc44640SAndreas Färber     CPU_FOREACH(cs) {
2987182735efSAndreas Färber         PowerPCCPU *cpu = POWERPC_CPU(cs);
2988182735efSAndreas Färber         CPUPPCState *cenv = &cpu->env;
2989182735efSAndreas Färber 
29900dfe59feSNicholas Piggin         if ((rb & DBELL_BRDCAST_MASK) || (cenv->spr[SPR_BOOKE_PIR] == pir)) {
29917b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
2992ad71ed68SBlue Swirl         }
2993ad71ed68SBlue Swirl     }
2994195801d7SStefan Hajnoczi     bql_unlock();
2995ad71ed68SBlue Swirl }
29967af1e7b0SCédric Le Goater 
29977af1e7b0SCédric Le Goater /* Server Processor Control */
29987af1e7b0SCédric Le Goater 
29995ba7ba1dSCédric Le Goater static bool dbell_type_server(target_ulong rb)
30005ba7ba1dSCédric Le Goater {
300147733729SDavid Gibson     /*
300247733729SDavid Gibson      * A Directed Hypervisor Doorbell message is sent only if the
30037af1e7b0SCédric Le Goater      * message type is 5. All other types are reserved and the
300447733729SDavid Gibson      * instruction is a no-op
300547733729SDavid Gibson      */
30065ba7ba1dSCédric Le Goater     return (rb & DBELL_TYPE_MASK) == DBELL_TYPE_DBELL_SERVER;
30077af1e7b0SCédric Le Goater }
30087af1e7b0SCédric Le Goater 
30090dfe59feSNicholas Piggin static inline bool dbell_bcast_core(target_ulong rb)
30100dfe59feSNicholas Piggin {
30110dfe59feSNicholas Piggin     return (rb & DBELL_BRDCAST_MASK) == DBELL_BRDCAST_CORE;
30120dfe59feSNicholas Piggin }
30130dfe59feSNicholas Piggin 
30140dfe59feSNicholas Piggin static inline bool dbell_bcast_subproc(target_ulong rb)
30150dfe59feSNicholas Piggin {
30160dfe59feSNicholas Piggin     return (rb & DBELL_BRDCAST_MASK) == DBELL_BRDCAST_SUBPROC;
30170dfe59feSNicholas Piggin }
30180dfe59feSNicholas Piggin 
30197f516cdeSNicholas Piggin /*
30207f516cdeSNicholas Piggin  * Send an interrupt to a thread in the same core as env).
30217f516cdeSNicholas Piggin  */
30227f516cdeSNicholas Piggin static void msgsnd_core_tir(CPUPPCState *env, uint32_t target_tir, int irq)
30237f516cdeSNicholas Piggin {
30247f516cdeSNicholas Piggin     PowerPCCPU *cpu = env_archcpu(env);
30257f516cdeSNicholas Piggin     CPUState *cs = env_cpu(env);
30267f516cdeSNicholas Piggin 
302750d8cfb9SNicholas Piggin     if (ppc_cpu_lpar_single_threaded(cs)) {
302850d8cfb9SNicholas Piggin         if (target_tir == 0) {
30297f516cdeSNicholas Piggin             ppc_set_irq(cpu, irq, 1);
303050d8cfb9SNicholas Piggin         }
30317f516cdeSNicholas Piggin     } else {
30327f516cdeSNicholas Piggin         CPUState *ccs;
30337f516cdeSNicholas Piggin 
30347f516cdeSNicholas Piggin         /* Does iothread need to be locked for walking CPU list? */
30357f516cdeSNicholas Piggin         bql_lock();
30367f516cdeSNicholas Piggin         THREAD_SIBLING_FOREACH(cs, ccs) {
30377f516cdeSNicholas Piggin             PowerPCCPU *ccpu = POWERPC_CPU(ccs);
30387f516cdeSNicholas Piggin             if (target_tir == ppc_cpu_tir(ccpu)) {
30397f516cdeSNicholas Piggin                 ppc_set_irq(ccpu, irq, 1);
30407f516cdeSNicholas Piggin                 break;
30417f516cdeSNicholas Piggin             }
30427f516cdeSNicholas Piggin         }
30437f516cdeSNicholas Piggin         bql_unlock();
30447f516cdeSNicholas Piggin     }
30457f516cdeSNicholas Piggin }
30467f516cdeSNicholas Piggin 
30477af1e7b0SCédric Le Goater void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb)
30487af1e7b0SCédric Le Goater {
30495ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
30507af1e7b0SCédric Le Goater         return;
30517af1e7b0SCédric Le Goater     }
30527af1e7b0SCédric Le Goater 
30537b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
30547af1e7b0SCédric Le Goater }
30557af1e7b0SCédric Le Goater 
30560dfe59feSNicholas Piggin void helper_book3s_msgsnd(CPUPPCState *env, target_ulong rb)
30575ba7ba1dSCédric Le Goater {
30585ba7ba1dSCédric Le Goater     int pir = rb & DBELL_PROCIDTAG_MASK;
30590dfe59feSNicholas Piggin     bool brdcast = false;
30600dfe59feSNicholas Piggin     CPUState *cs, *ccs;
30610dfe59feSNicholas Piggin     PowerPCCPU *cpu;
30625ba7ba1dSCédric Le Goater 
30635ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
30645ba7ba1dSCédric Le Goater         return;
30655ba7ba1dSCédric Le Goater     }
30665ba7ba1dSCédric Le Goater 
30677f516cdeSNicholas Piggin     /* POWER8 msgsnd is like msgsndp (targets a thread within core) */
30687f516cdeSNicholas Piggin     if (!(env->insns_flags2 & PPC2_ISA300)) {
30697f516cdeSNicholas Piggin         msgsnd_core_tir(env, rb & PPC_BITMASK(57, 63), PPC_INTERRUPT_HDOORBELL);
30707f516cdeSNicholas Piggin         return;
30717f516cdeSNicholas Piggin     }
30727f516cdeSNicholas Piggin 
30737f516cdeSNicholas Piggin     /* POWER9 and later msgsnd is a global (targets any thread) */
30740dfe59feSNicholas Piggin     cpu = ppc_get_vcpu_by_pir(pir);
30750dfe59feSNicholas Piggin     if (!cpu) {
30760dfe59feSNicholas Piggin         return;
30770dfe59feSNicholas Piggin     }
30780dfe59feSNicholas Piggin     cs = CPU(cpu);
30790dfe59feSNicholas Piggin 
30800dfe59feSNicholas Piggin     if (dbell_bcast_core(rb) || (dbell_bcast_subproc(rb) &&
30810dfe59feSNicholas Piggin                                  (env->flags & POWERPC_FLAG_SMT_1LPAR))) {
30820dfe59feSNicholas Piggin         brdcast = true;
30830dfe59feSNicholas Piggin     }
30840dfe59feSNicholas Piggin 
308550d8cfb9SNicholas Piggin     if (ppc_cpu_core_single_threaded(cs) || !brdcast) {
30860dfe59feSNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_HDOORBELL, 1);
30870dfe59feSNicholas Piggin         return;
30880dfe59feSNicholas Piggin     }
30890dfe59feSNicholas Piggin 
30900dfe59feSNicholas Piggin     /*
30910dfe59feSNicholas Piggin      * Why is bql needed for walking CPU list? Answer seems to be because ppc
30920dfe59feSNicholas Piggin      * irq handling needs it, but ppc_set_irq takes the lock itself if needed,
30930dfe59feSNicholas Piggin      * so could this be removed?
30940dfe59feSNicholas Piggin      */
30950dfe59feSNicholas Piggin     bql_lock();
30960dfe59feSNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
30970dfe59feSNicholas Piggin         ppc_set_irq(POWERPC_CPU(ccs), PPC_INTERRUPT_HDOORBELL, 1);
30980dfe59feSNicholas Piggin     }
30990dfe59feSNicholas Piggin     bql_unlock();
31005ba7ba1dSCédric Le Goater }
31015ba7ba1dSCédric Le Goater 
3102f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
31035ba7ba1dSCédric Le Goater void helper_book3s_msgclrp(CPUPPCState *env, target_ulong rb)
31045ba7ba1dSCédric Le Goater {
3105493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgclrp", HFSCR_IC_MSGP);
3106493028d8SCédric Le Goater 
31075ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
31085ba7ba1dSCédric Le Goater         return;
31095ba7ba1dSCédric Le Goater     }
31105ba7ba1dSCédric Le Goater 
31112e985555SNicholas Piggin     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_DOORBELL, 0);
31125ba7ba1dSCédric Le Goater }
31135ba7ba1dSCédric Le Goater 
31145ba7ba1dSCédric Le Goater /*
3115d24e80b2SNicholas Piggin  * sends a message to another thread  on the same
31165ba7ba1dSCédric Le Goater  * multi-threaded processor
31175ba7ba1dSCédric Le Goater  */
31185ba7ba1dSCédric Le Goater void helper_book3s_msgsndp(CPUPPCState *env, target_ulong rb)
31195ba7ba1dSCédric Le Goater {
3120493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgsndp", HFSCR_IC_MSGP);
3121493028d8SCédric Le Goater 
31227f516cdeSNicholas Piggin     if (!dbell_type_server(rb)) {
31235ba7ba1dSCédric Le Goater         return;
31245ba7ba1dSCédric Le Goater     }
31255ba7ba1dSCédric Le Goater 
31267f516cdeSNicholas Piggin     msgsnd_core_tir(env, rb & PPC_BITMASK(57, 63), PPC_INTERRUPT_DOORBELL);
31275ba7ba1dSCédric Le Goater }
3128996473e4SRichard Henderson #endif /* TARGET_PPC64 */
31290f3110faSRichard Henderson 
313014895384SNicholas Piggin /* Single-step tracing */
313114895384SNicholas Piggin void helper_book3s_trace(CPUPPCState *env, target_ulong prev_ip)
313214895384SNicholas Piggin {
313314895384SNicholas Piggin     uint32_t error_code = 0;
313414895384SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
313514895384SNicholas Piggin         /* Load/store reporting, SRR1[35, 36] and SDAR, are not implemented. */
313614895384SNicholas Piggin         env->spr[SPR_POWER_SIAR] = prev_ip;
313714895384SNicholas Piggin         error_code = PPC_BIT(33);
313814895384SNicholas Piggin     }
313914895384SNicholas Piggin     raise_exception_err(env, POWERPC_EXCP_TRACE, error_code);
314014895384SNicholas Piggin }
314114895384SNicholas Piggin 
31420f3110faSRichard Henderson void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
31430f3110faSRichard Henderson                                  MMUAccessType access_type,
31440f3110faSRichard Henderson                                  int mmu_idx, uintptr_t retaddr)
31450f3110faSRichard Henderson {
3146b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
314729c4a336SFabiano Rosas     uint32_t insn;
314829c4a336SFabiano Rosas 
314929c4a336SFabiano Rosas     /* Restore state and reload the insn we executed, for filling in DSISR.  */
31503d419a4dSRichard Henderson     cpu_restore_state(cs, retaddr);
3151888050cfSNicholas Piggin     insn = ppc_ldl_code(env, env->nip);
31520f3110faSRichard Henderson 
3153a7e3af13SRichard Henderson     switch (env->mmu_model) {
3154a7e3af13SRichard Henderson     case POWERPC_MMU_SOFT_4xx:
3155a7e3af13SRichard Henderson         env->spr[SPR_40x_DEAR] = vaddr;
3156a7e3af13SRichard Henderson         break;
3157a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE:
3158a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE206:
3159a7e3af13SRichard Henderson         env->spr[SPR_BOOKE_DEAR] = vaddr;
3160a7e3af13SRichard Henderson         break;
3161a7e3af13SRichard Henderson     default:
3162a7e3af13SRichard Henderson         env->spr[SPR_DAR] = vaddr;
3163a7e3af13SRichard Henderson         break;
3164a7e3af13SRichard Henderson     }
3165a7e3af13SRichard Henderson 
31660f3110faSRichard Henderson     cs->exception_index = POWERPC_EXCP_ALIGN;
316729c4a336SFabiano Rosas     env->error_code = insn & 0x03FF0000;
316829c4a336SFabiano Rosas     cpu_loop_exit(cs);
31690f3110faSRichard Henderson }
317055a7fa34SNicholas Piggin 
317155a7fa34SNicholas Piggin void ppc_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
317255a7fa34SNicholas Piggin                                    vaddr vaddr, unsigned size,
317355a7fa34SNicholas Piggin                                    MMUAccessType access_type,
317455a7fa34SNicholas Piggin                                    int mmu_idx, MemTxAttrs attrs,
317555a7fa34SNicholas Piggin                                    MemTxResult response, uintptr_t retaddr)
317655a7fa34SNicholas Piggin {
3177b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
317855a7fa34SNicholas Piggin 
317955a7fa34SNicholas Piggin     switch (env->excp_model) {
318055a7fa34SNicholas Piggin #if defined(TARGET_PPC64)
3181c8fd9667SNicholas Piggin     case POWERPC_EXCP_POWER8:
318255a7fa34SNicholas Piggin     case POWERPC_EXCP_POWER9:
318355a7fa34SNicholas Piggin     case POWERPC_EXCP_POWER10:
3184c0d96407SAditya Gupta     case POWERPC_EXCP_POWER11:
318555a7fa34SNicholas Piggin         /*
318655a7fa34SNicholas Piggin          * Machine check codes can be found in processor User Manual or
318755a7fa34SNicholas Piggin          * Linux or skiboot source.
318855a7fa34SNicholas Piggin          */
318955a7fa34SNicholas Piggin         if (access_type == MMU_DATA_LOAD) {
319055a7fa34SNicholas Piggin             env->spr[SPR_DAR] = vaddr;
319155a7fa34SNicholas Piggin             env->spr[SPR_DSISR] = PPC_BIT(57);
319255a7fa34SNicholas Piggin             env->error_code = PPC_BIT(42);
319355a7fa34SNicholas Piggin 
319455a7fa34SNicholas Piggin         } else if (access_type == MMU_DATA_STORE) {
319555a7fa34SNicholas Piggin             /*
319655a7fa34SNicholas Piggin              * MCE for stores in POWER is asynchronous so hardware does
319755a7fa34SNicholas Piggin              * not set DAR, but QEMU can do better.
319855a7fa34SNicholas Piggin              */
319955a7fa34SNicholas Piggin             env->spr[SPR_DAR] = vaddr;
320055a7fa34SNicholas Piggin             env->error_code = PPC_BIT(36) | PPC_BIT(43) | PPC_BIT(45);
320155a7fa34SNicholas Piggin             env->error_code |= PPC_BIT(42);
320255a7fa34SNicholas Piggin 
320355a7fa34SNicholas Piggin         } else { /* Fetch */
3204c8fd9667SNicholas Piggin             /*
3205c8fd9667SNicholas Piggin              * is_prefix_insn_excp() tests !PPC_BIT(42) to avoid fetching
3206c8fd9667SNicholas Piggin              * the instruction, so that must always be clear for fetches.
3207c8fd9667SNicholas Piggin              */
320855a7fa34SNicholas Piggin             env->error_code = PPC_BIT(36) | PPC_BIT(44) | PPC_BIT(45);
320955a7fa34SNicholas Piggin         }
321055a7fa34SNicholas Piggin         break;
321155a7fa34SNicholas Piggin #endif
321255a7fa34SNicholas Piggin     default:
321355a7fa34SNicholas Piggin         /*
321455a7fa34SNicholas Piggin          * TODO: Check behaviour for other CPUs, for now do nothing.
321555a7fa34SNicholas Piggin          * Could add a basic MCE even if real hardware ignores.
321655a7fa34SNicholas Piggin          */
321755a7fa34SNicholas Piggin         return;
321855a7fa34SNicholas Piggin     }
321955a7fa34SNicholas Piggin 
322055a7fa34SNicholas Piggin     cs->exception_index = POWERPC_EXCP_MCHECK;
322155a7fa34SNicholas Piggin     cpu_loop_exit_restore(cs, retaddr);
322255a7fa34SNicholas Piggin }
322314192307SNicholas Piggin 
322414192307SNicholas Piggin void ppc_cpu_debug_excp_handler(CPUState *cs)
322514192307SNicholas Piggin {
322614192307SNicholas Piggin #if defined(TARGET_PPC64)
3227b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
322814192307SNicholas Piggin 
322914192307SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
3230d5ee641cSNicholas Piggin         if (cs->watchpoint_hit) {
3231d5ee641cSNicholas Piggin             if (cs->watchpoint_hit->flags & BP_CPU) {
3232d5ee641cSNicholas Piggin                 env->spr[SPR_DAR] = cs->watchpoint_hit->hitaddr;
3233d5ee641cSNicholas Piggin                 env->spr[SPR_DSISR] = PPC_BIT(41);
3234d5ee641cSNicholas Piggin                 cs->watchpoint_hit = NULL;
3235d5ee641cSNicholas Piggin                 raise_exception(env, POWERPC_EXCP_DSI);
3236d5ee641cSNicholas Piggin             }
3237d5ee641cSNicholas Piggin             cs->watchpoint_hit = NULL;
3238d5ee641cSNicholas Piggin         } else if (cpu_breakpoint_test(cs, env->nip, BP_CPU)) {
323914192307SNicholas Piggin             raise_exception_err(env, POWERPC_EXCP_TRACE,
324014192307SNicholas Piggin                                 PPC_BIT(33) | PPC_BIT(43));
324114192307SNicholas Piggin         }
324214192307SNicholas Piggin     }
324314192307SNicholas Piggin #endif
324414192307SNicholas Piggin }
324514192307SNicholas Piggin 
324614192307SNicholas Piggin bool ppc_cpu_debug_check_breakpoint(CPUState *cs)
324714192307SNicholas Piggin {
324814192307SNicholas Piggin #if defined(TARGET_PPC64)
3249b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
325014192307SNicholas Piggin 
325114192307SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
325214192307SNicholas Piggin         target_ulong priv;
325314192307SNicholas Piggin 
325414192307SNicholas Piggin         priv = env->spr[SPR_CIABR] & PPC_BITMASK(62, 63);
325514192307SNicholas Piggin         switch (priv) {
325614192307SNicholas Piggin         case 0x1: /* problem */
325714192307SNicholas Piggin             return env->msr & ((target_ulong)1 << MSR_PR);
325814192307SNicholas Piggin         case 0x2: /* supervisor */
325914192307SNicholas Piggin             return (!(env->msr & ((target_ulong)1 << MSR_PR)) &&
326014192307SNicholas Piggin                     !(env->msr & ((target_ulong)1 << MSR_HV)));
326114192307SNicholas Piggin         case 0x3: /* hypervisor */
326214192307SNicholas Piggin             return (!(env->msr & ((target_ulong)1 << MSR_PR)) &&
326314192307SNicholas Piggin                      (env->msr & ((target_ulong)1 << MSR_HV)));
326414192307SNicholas Piggin         default:
326514192307SNicholas Piggin             g_assert_not_reached();
326614192307SNicholas Piggin         }
326714192307SNicholas Piggin     }
326814192307SNicholas Piggin #endif
326914192307SNicholas Piggin 
327014192307SNicholas Piggin     return false;
327114192307SNicholas Piggin }
327214192307SNicholas Piggin 
3273d5ee641cSNicholas Piggin bool ppc_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
3274d5ee641cSNicholas Piggin {
3275d5ee641cSNicholas Piggin #if defined(TARGET_PPC64)
3276b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
3277d5ee641cSNicholas Piggin 
3278d5ee641cSNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
3279d5ee641cSNicholas Piggin         if (wp == env->dawr0_watchpoint) {
3280d5ee641cSNicholas Piggin             uint32_t dawrx = env->spr[SPR_DAWRX0];
3281d5ee641cSNicholas Piggin             bool wt = extract32(dawrx, PPC_BIT_NR(59), 1);
3282d5ee641cSNicholas Piggin             bool wti = extract32(dawrx, PPC_BIT_NR(60), 1);
3283d5ee641cSNicholas Piggin             bool hv = extract32(dawrx, PPC_BIT_NR(61), 1);
3284d5ee641cSNicholas Piggin             bool sv = extract32(dawrx, PPC_BIT_NR(62), 1);
3285d5ee641cSNicholas Piggin             bool pr = extract32(dawrx, PPC_BIT_NR(62), 1);
3286d5ee641cSNicholas Piggin 
3287d5ee641cSNicholas Piggin             if ((env->msr & ((target_ulong)1 << MSR_PR)) && !pr) {
3288d5ee641cSNicholas Piggin                 return false;
3289d5ee641cSNicholas Piggin             } else if ((env->msr & ((target_ulong)1 << MSR_HV)) && !hv) {
3290d5ee641cSNicholas Piggin                 return false;
3291d5ee641cSNicholas Piggin             } else if (!sv) {
3292d5ee641cSNicholas Piggin                 return false;
3293d5ee641cSNicholas Piggin             }
3294d5ee641cSNicholas Piggin 
3295d5ee641cSNicholas Piggin             if (!wti) {
3296d5ee641cSNicholas Piggin                 if (env->msr & ((target_ulong)1 << MSR_DR)) {
3297d5ee641cSNicholas Piggin                     if (!wt) {
3298d5ee641cSNicholas Piggin                         return false;
3299d5ee641cSNicholas Piggin                     }
3300d5ee641cSNicholas Piggin                 } else {
3301d5ee641cSNicholas Piggin                     if (wt) {
3302d5ee641cSNicholas Piggin                         return false;
3303d5ee641cSNicholas Piggin                     }
3304d5ee641cSNicholas Piggin                 }
3305d5ee641cSNicholas Piggin             }
3306d5ee641cSNicholas Piggin 
3307d5ee641cSNicholas Piggin             return true;
3308d5ee641cSNicholas Piggin         }
3309d5ee641cSNicholas Piggin     }
3310d5ee641cSNicholas Piggin #endif
3311d5ee641cSNicholas Piggin 
3312d5ee641cSNicholas Piggin     return false;
3313d5ee641cSNicholas Piggin }
3314d5ee641cSNicholas Piggin 
3315996473e4SRichard Henderson #endif /* !CONFIG_USER_ONLY */
33165ca958cfSBALATON Zoltan #endif /* CONFIG_TCG */
3317