xref: /qemu/target/ppc/excp_helper.c (revision 0dfe59fe77ed571f23aefb70a3a226c3e1779862)
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:
16649f338e4dSFabiano Rosas         powerpc_excp_books(cpu, excp);
16659f338e4dSFabiano Rosas         break;
1666dc88dd0aSFabiano Rosas     default:
166728091374SFabiano Rosas         g_assert_not_reached();
1668dc88dd0aSFabiano Rosas     }
1669dc88dd0aSFabiano Rosas }
1670dc88dd0aSFabiano Rosas 
167197a8ea5aSAndreas Färber void ppc_cpu_do_interrupt(CPUState *cs)
1672c79c73f6SBlue Swirl {
167397a8ea5aSAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
16745c26a5b3SAndreas Färber 
167593130c84SFabiano Rosas     powerpc_excp(cpu, cs->exception_index);
1676c79c73f6SBlue Swirl }
1677c79c73f6SBlue Swirl 
1678f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
1679c8e1de2eSMatheus Ferst #define P7_UNUSED_INTERRUPTS \
1680c8e1de2eSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_HVIRT | PPC_INTERRUPT_CEXT |       \
1681c8e1de2eSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |      \
1682c8e1de2eSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_DOORBELL | PPC_INTERRUPT_HDOORBELL | \
1683c8e1de2eSMatheus Ferst      PPC_INTERRUPT_THERM | PPC_INTERRUPT_EBB)
1684c8e1de2eSMatheus Ferst 
16859c713713SMatheus Ferst static int p7_interrupt_powersave(CPUPPCState *env)
16869c713713SMatheus Ferst {
16879c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
16889c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE0)) {
16899c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
16909c713713SMatheus Ferst     }
16919c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
16929c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE1)) {
16939c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
16949c713713SMatheus Ferst     }
16959c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_MCK) &&
16969c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
16979c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
16989c713713SMatheus Ferst     }
16999c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HMI) &&
17009c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
17019c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
17029c713713SMatheus Ferst     }
17039c713713SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
17049c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
17059c713713SMatheus Ferst     }
17069c713713SMatheus Ferst     return 0;
17079c713713SMatheus Ferst }
17089c713713SMatheus Ferst 
1709bf303fb3SMatheus Ferst static int p7_next_unmasked_interrupt(CPUPPCState *env)
1710bf303fb3SMatheus Ferst {
17114e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
17124e6b7db2SBALATON Zoltan 
1713022b7128SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
1714022b7128SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1715bf303fb3SMatheus Ferst 
1716c8e1de2eSMatheus Ferst     assert((env->pending_interrupts & P7_UNUSED_INTERRUPTS) == 0);
1717c8e1de2eSMatheus Ferst 
1718022b7128SMatheus Ferst     if (cs->halted) {
1719022b7128SMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
1720022b7128SMatheus Ferst         return p7_interrupt_powersave(env);
1721022b7128SMatheus Ferst     }
1722022b7128SMatheus Ferst 
1723bf303fb3SMatheus Ferst     /* Machine check exception */
1724bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1725bf303fb3SMatheus Ferst         return PPC_INTERRUPT_MCK;
1726bf303fb3SMatheus Ferst     }
1727bf303fb3SMatheus Ferst 
1728bf303fb3SMatheus Ferst     /* Hypervisor decrementer exception */
1729bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1730bf303fb3SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1731bf303fb3SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
1732022b7128SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1733bf303fb3SMatheus Ferst             /* HDEC clears on delivery */
1734bf303fb3SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1735bf303fb3SMatheus Ferst         }
1736bf303fb3SMatheus Ferst     }
1737bf303fb3SMatheus Ferst 
1738bf303fb3SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1739bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1740bf303fb3SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1741bf303fb3SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1742bf303fb3SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
1743022b7128SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1744bf303fb3SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1745bf303fb3SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1746bf303fb3SMatheus Ferst             return PPC_INTERRUPT_EXT;
1747bf303fb3SMatheus Ferst         }
1748bf303fb3SMatheus Ferst     }
1749022b7128SMatheus Ferst     if (msr_ee != 0) {
1750bf303fb3SMatheus Ferst         /* Decrementer exception */
1751bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1752bf303fb3SMatheus Ferst             return PPC_INTERRUPT_DECR;
1753bf303fb3SMatheus Ferst         }
1754bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1755bf303fb3SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1756bf303fb3SMatheus Ferst         }
1757bf303fb3SMatheus Ferst     }
1758bf303fb3SMatheus Ferst 
1759bf303fb3SMatheus Ferst     return 0;
1760bf303fb3SMatheus Ferst }
1761bf303fb3SMatheus Ferst 
1762f6194fddSMatheus Ferst #define P8_UNUSED_INTERRUPTS \
1763f6194fddSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_HVIRT |  \
1764f6194fddSMatheus Ferst     PPC_INTERRUPT_CEXT | PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL |  \
1765f6194fddSMatheus Ferst     PPC_INTERRUPT_FIT | PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1766f6194fddSMatheus Ferst 
17679c713713SMatheus Ferst static int p8_interrupt_powersave(CPUPPCState *env)
17689c713713SMatheus Ferst {
17699c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
17709c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE2)) {
17719c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
17729c713713SMatheus Ferst     }
17739c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
17749c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE3)) {
17759c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
17769c713713SMatheus Ferst     }
17779c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_MCK) &&
17789c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) {
17799c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
17809c713713SMatheus Ferst     }
17819c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HMI) &&
17829c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) {
17839c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
17849c713713SMatheus Ferst     }
17859c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
17869c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE0)) {
17879c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
17889c713713SMatheus Ferst     }
17899c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
17909c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE1)) {
17919c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
17929c713713SMatheus Ferst     }
17939c713713SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
17949c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
17959c713713SMatheus Ferst     }
17969c713713SMatheus Ferst     return 0;
17979c713713SMatheus Ferst }
17989c713713SMatheus Ferst 
1799a9899d42SMatheus Ferst static int p8_next_unmasked_interrupt(CPUPPCState *env)
1800a9899d42SMatheus Ferst {
18014e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
18024e6b7db2SBALATON Zoltan 
180364a9b5eeSMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
180464a9b5eeSMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1805a9899d42SMatheus Ferst 
1806f6194fddSMatheus Ferst     assert((env->pending_interrupts & P8_UNUSED_INTERRUPTS) == 0);
1807f6194fddSMatheus Ferst 
180864a9b5eeSMatheus Ferst     if (cs->halted) {
180964a9b5eeSMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
181064a9b5eeSMatheus Ferst         return p8_interrupt_powersave(env);
181164a9b5eeSMatheus Ferst     }
181264a9b5eeSMatheus Ferst 
1813a9899d42SMatheus Ferst     /* Machine check exception */
1814a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1815a9899d42SMatheus Ferst         return PPC_INTERRUPT_MCK;
1816a9899d42SMatheus Ferst     }
1817a9899d42SMatheus Ferst 
1818a9899d42SMatheus Ferst     /* Hypervisor decrementer exception */
1819a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1820a9899d42SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1821a9899d42SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
182264a9b5eeSMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1823a9899d42SMatheus Ferst             /* HDEC clears on delivery */
1824a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1825a9899d42SMatheus Ferst         }
1826a9899d42SMatheus Ferst     }
1827a9899d42SMatheus Ferst 
1828a9899d42SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1829a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1830a9899d42SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1831a9899d42SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1832a9899d42SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
183364a9b5eeSMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1834a9899d42SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1835a9899d42SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1836a9899d42SMatheus Ferst             return PPC_INTERRUPT_EXT;
1837a9899d42SMatheus Ferst         }
1838a9899d42SMatheus Ferst     }
183964a9b5eeSMatheus Ferst     if (msr_ee != 0) {
1840a9899d42SMatheus Ferst         /* Decrementer exception */
1841a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1842a9899d42SMatheus Ferst             return PPC_INTERRUPT_DECR;
1843a9899d42SMatheus Ferst         }
1844a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1845a9899d42SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
1846a9899d42SMatheus Ferst         }
1847a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
1848a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
1849a9899d42SMatheus Ferst         }
1850a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1851a9899d42SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1852a9899d42SMatheus Ferst         }
1853a9899d42SMatheus Ferst         /* EBB exception */
1854a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
1855a9899d42SMatheus Ferst             /*
1856a9899d42SMatheus Ferst              * EBB exception must be taken in problem state and
1857a9899d42SMatheus Ferst              * with BESCR_GE set.
1858a9899d42SMatheus Ferst              */
1859a9899d42SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
1860a9899d42SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
1861a9899d42SMatheus Ferst                 return PPC_INTERRUPT_EBB;
1862a9899d42SMatheus Ferst             }
1863a9899d42SMatheus Ferst         }
1864a9899d42SMatheus Ferst     }
1865a9899d42SMatheus Ferst 
1866a9899d42SMatheus Ferst     return 0;
1867a9899d42SMatheus Ferst }
1868a9899d42SMatheus Ferst 
1869b00e9a2fSMatheus Ferst #define P9_UNUSED_INTERRUPTS \
1870b00e9a2fSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT |   \
1871b00e9a2fSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |  \
1872b00e9a2fSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1873b00e9a2fSMatheus Ferst 
18749c713713SMatheus Ferst static int p9_interrupt_powersave(CPUPPCState *env)
18759c713713SMatheus Ferst {
18769c713713SMatheus Ferst     /* External Exception */
18779c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
18789c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_EEE)) {
18799c713713SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
18809c713713SMatheus Ferst         if (!heic || !FIELD_EX64_HV(env->msr) ||
18819c713713SMatheus Ferst             FIELD_EX64(env->msr, MSR, PR)) {
18829c713713SMatheus Ferst             return PPC_INTERRUPT_EXT;
18839c713713SMatheus Ferst         }
18849c713713SMatheus Ferst     }
18859c713713SMatheus Ferst     /* Decrementer Exception */
18869c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
18879c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_DEE)) {
18889c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
18899c713713SMatheus Ferst     }
18909c713713SMatheus Ferst     /* Machine Check or Hypervisor Maintenance Exception */
18919c713713SMatheus Ferst     if (env->spr[SPR_LPCR] & LPCR_OEE) {
18929c713713SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
18939c713713SMatheus Ferst             return PPC_INTERRUPT_MCK;
18949c713713SMatheus Ferst         }
18959c713713SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HMI) {
18969c713713SMatheus Ferst             return PPC_INTERRUPT_HMI;
18979c713713SMatheus Ferst         }
18989c713713SMatheus Ferst     }
18999c713713SMatheus Ferst     /* Privileged Doorbell Exception */
19009c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
19019c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_PDEE)) {
19029c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
19039c713713SMatheus Ferst     }
19049c713713SMatheus Ferst     /* Hypervisor Doorbell Exception */
19059c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
19069c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_HDEE)) {
19079c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
19089c713713SMatheus Ferst     }
19099c713713SMatheus Ferst     /* Hypervisor virtualization exception */
19109c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HVIRT) &&
19119c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_HVEE)) {
19129c713713SMatheus Ferst         return PPC_INTERRUPT_HVIRT;
19139c713713SMatheus Ferst     }
19149c713713SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
19159c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
19169c713713SMatheus Ferst     }
19179c713713SMatheus Ferst     return 0;
19189c713713SMatheus Ferst }
19199c713713SMatheus Ferst 
19202dfecf01SMatheus Ferst static int p9_next_unmasked_interrupt(CPUPPCState *env)
19212dfecf01SMatheus Ferst {
19224e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
19234e6b7db2SBALATON Zoltan 
192427796411SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
192527796411SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
19262dfecf01SMatheus Ferst 
1927b00e9a2fSMatheus Ferst     assert((env->pending_interrupts & P9_UNUSED_INTERRUPTS) == 0);
1928b00e9a2fSMatheus Ferst 
192927796411SMatheus Ferst     if (cs->halted) {
193027796411SMatheus Ferst         if (env->spr[SPR_PSSCR] & PSSCR_EC) {
193127796411SMatheus Ferst             /*
193227796411SMatheus Ferst              * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can
193327796411SMatheus Ferst              * wakeup the processor
193427796411SMatheus Ferst              */
193527796411SMatheus Ferst             return p9_interrupt_powersave(env);
193627796411SMatheus Ferst         } else {
193727796411SMatheus Ferst             /*
193827796411SMatheus Ferst              * When it's clear, any system-caused exception exits power-saving
193927796411SMatheus Ferst              * mode, even the ones that gate on MSR[EE].
194027796411SMatheus Ferst              */
194127796411SMatheus Ferst             msr_ee = true;
194227796411SMatheus Ferst         }
194327796411SMatheus Ferst     }
194427796411SMatheus Ferst 
19452dfecf01SMatheus Ferst     /* Machine check exception */
19462dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
19472dfecf01SMatheus Ferst         return PPC_INTERRUPT_MCK;
19482dfecf01SMatheus Ferst     }
19492dfecf01SMatheus Ferst 
19502dfecf01SMatheus Ferst     /* Hypervisor decrementer exception */
19512dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
19522dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
19532dfecf01SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
195427796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
19552dfecf01SMatheus Ferst             /* HDEC clears on delivery */
19562dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDECR;
19572dfecf01SMatheus Ferst         }
19582dfecf01SMatheus Ferst     }
19592dfecf01SMatheus Ferst 
19602dfecf01SMatheus Ferst     /* Hypervisor virtualization interrupt */
19612dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
19622dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
19632dfecf01SMatheus Ferst         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
196427796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hvice) {
19652dfecf01SMatheus Ferst             return PPC_INTERRUPT_HVIRT;
19662dfecf01SMatheus Ferst         }
19672dfecf01SMatheus Ferst     }
19682dfecf01SMatheus Ferst 
19692dfecf01SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
19702dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
19712dfecf01SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
19722dfecf01SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
19732dfecf01SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
197427796411SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
19752dfecf01SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
19762dfecf01SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
19772dfecf01SMatheus Ferst             return PPC_INTERRUPT_EXT;
19782dfecf01SMatheus Ferst         }
19792dfecf01SMatheus Ferst     }
198027796411SMatheus Ferst     if (msr_ee != 0) {
19812dfecf01SMatheus Ferst         /* Decrementer exception */
19822dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
19832dfecf01SMatheus Ferst             return PPC_INTERRUPT_DECR;
19842dfecf01SMatheus Ferst         }
19852dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
19862dfecf01SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
19872dfecf01SMatheus Ferst         }
19882dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
19892dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
19902dfecf01SMatheus Ferst         }
19912dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
19922dfecf01SMatheus Ferst             return PPC_INTERRUPT_PERFM;
19932dfecf01SMatheus Ferst         }
19942dfecf01SMatheus Ferst         /* EBB exception */
19952dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
19962dfecf01SMatheus Ferst             /*
19972dfecf01SMatheus Ferst              * EBB exception must be taken in problem state and
19982dfecf01SMatheus Ferst              * with BESCR_GE set.
19992dfecf01SMatheus Ferst              */
20002dfecf01SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
20012dfecf01SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
20022dfecf01SMatheus Ferst                 return PPC_INTERRUPT_EBB;
20032dfecf01SMatheus Ferst             }
20042dfecf01SMatheus Ferst         }
20052dfecf01SMatheus Ferst     }
20062dfecf01SMatheus Ferst 
20072dfecf01SMatheus Ferst     return 0;
20082dfecf01SMatheus Ferst }
2009f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
20102dfecf01SMatheus Ferst 
2011868cb6baSBALATON Zoltan static int ppc_next_unmasked_interrupt(CPUPPCState *env)
2012c79c73f6SBlue Swirl {
2013868cb6baSBALATON Zoltan #ifdef TARGET_PPC64
2014868cb6baSBALATON Zoltan     switch (env->excp_model) {
2015868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER7:
2016868cb6baSBALATON Zoltan         return p7_next_unmasked_interrupt(env);
2017868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER8:
2018868cb6baSBALATON Zoltan         return p8_next_unmasked_interrupt(env);
2019868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER9:
2020868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER10:
2021868cb6baSBALATON Zoltan         return p9_next_unmasked_interrupt(env);
2022868cb6baSBALATON Zoltan     default:
2023868cb6baSBALATON Zoltan         break;
2024868cb6baSBALATON Zoltan     }
2025868cb6baSBALATON Zoltan #endif
20263621e2c9SBenjamin Herrenschmidt     bool async_deliver;
2027259186a7SAndreas Färber 
2028c79c73f6SBlue Swirl     /* External reset */
2029f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
2030de76b85cSMatheus Ferst         return PPC_INTERRUPT_RESET;
2031c79c73f6SBlue Swirl     }
2032c79c73f6SBlue Swirl     /* Machine check exception */
2033f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
2034de76b85cSMatheus Ferst         return PPC_INTERRUPT_MCK;
2035c79c73f6SBlue Swirl     }
2036c79c73f6SBlue Swirl #if 0 /* TODO */
2037c79c73f6SBlue Swirl     /* External debug exception */
2038f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) {
2039de76b85cSMatheus Ferst         return PPC_INTERRUPT_DEBUG;
2040c79c73f6SBlue Swirl     }
2041c79c73f6SBlue Swirl #endif
20423621e2c9SBenjamin Herrenschmidt 
20433621e2c9SBenjamin Herrenschmidt     /*
20443621e2c9SBenjamin Herrenschmidt      * For interrupts that gate on MSR:EE, we need to do something a
20453621e2c9SBenjamin Herrenschmidt      * bit more subtle, as we need to let them through even when EE is
20463621e2c9SBenjamin Herrenschmidt      * clear when coming out of some power management states (in order
20473621e2c9SBenjamin Herrenschmidt      * for them to become a 0x100).
20483621e2c9SBenjamin Herrenschmidt      */
20490939b8f8SVíctor Colombo     async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
20503621e2c9SBenjamin Herrenschmidt 
2051c79c73f6SBlue Swirl     /* Hypervisor decrementer exception */
2052f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
20534b236b62SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
20544b236b62SBenjamin Herrenschmidt         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
20559de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
20564b236b62SBenjamin Herrenschmidt             /* HDEC clears on delivery */
2057de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDECR;
2058c79c73f6SBlue Swirl         }
2059c79c73f6SBlue Swirl     }
2060d8ce5fd6SBenjamin Herrenschmidt 
2061d8ce5fd6SBenjamin Herrenschmidt     /* Hypervisor virtualization interrupt */
2062f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
2063d8ce5fd6SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
2064d8ce5fd6SBenjamin Herrenschmidt         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
20659de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
2066de76b85cSMatheus Ferst             return PPC_INTERRUPT_HVIRT;
2067d8ce5fd6SBenjamin Herrenschmidt         }
2068d8ce5fd6SBenjamin Herrenschmidt     }
2069d8ce5fd6SBenjamin Herrenschmidt 
2070d8ce5fd6SBenjamin Herrenschmidt     /* External interrupt can ignore MSR:EE under some circumstances */
2071f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
2072d1dbe37cSBenjamin Herrenschmidt         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
20736eebe6dcSBenjamin Herrenschmidt         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
20746eebe6dcSBenjamin Herrenschmidt         /* HEIC blocks delivery to the hypervisor */
20759de754d3SVíctor Colombo         if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
2076d41ccf6eSVíctor Colombo             !FIELD_EX64(env->msr, MSR, PR))) ||
20779de754d3SVíctor Colombo             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
2078de76b85cSMatheus Ferst             return PPC_INTERRUPT_EXT;
2079d1dbe37cSBenjamin Herrenschmidt         }
2080d1dbe37cSBenjamin Herrenschmidt     }
2081acc861c2SVíctor Colombo     if (FIELD_EX64(env->msr, MSR, CE)) {
2082c79c73f6SBlue Swirl         /* External critical interrupt */
2083f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CEXT) {
2084de76b85cSMatheus Ferst             return PPC_INTERRUPT_CEXT;
2085c79c73f6SBlue Swirl         }
2086c79c73f6SBlue Swirl     }
20873621e2c9SBenjamin Herrenschmidt     if (async_deliver != 0) {
2088c79c73f6SBlue Swirl         /* Watchdog timer on embedded PowerPC */
2089f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_WDT) {
2090de76b85cSMatheus Ferst             return PPC_INTERRUPT_WDT;
2091c79c73f6SBlue Swirl         }
2092f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) {
2093de76b85cSMatheus Ferst             return PPC_INTERRUPT_CDOORBELL;
2094c79c73f6SBlue Swirl         }
2095c79c73f6SBlue Swirl         /* Fixed interval timer on embedded PowerPC */
2096f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_FIT) {
2097de76b85cSMatheus Ferst             return PPC_INTERRUPT_FIT;
2098c79c73f6SBlue Swirl         }
2099c79c73f6SBlue Swirl         /* Programmable interval timer on embedded PowerPC */
2100f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PIT) {
2101de76b85cSMatheus Ferst             return PPC_INTERRUPT_PIT;
2102c79c73f6SBlue Swirl         }
2103c79c73f6SBlue Swirl         /* Decrementer exception */
2104f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
2105de76b85cSMatheus Ferst             return PPC_INTERRUPT_DECR;
2106c79c73f6SBlue Swirl         }
2107f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2108de76b85cSMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
2109c79c73f6SBlue Swirl         }
2110f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
2111de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
21127af1e7b0SCédric Le Goater         }
2113f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
2114de76b85cSMatheus Ferst             return PPC_INTERRUPT_PERFM;
2115c79c73f6SBlue Swirl         }
2116c79c73f6SBlue Swirl         /* Thermal interrupt */
2117f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_THERM) {
2118de76b85cSMatheus Ferst             return PPC_INTERRUPT_THERM;
2119c79c73f6SBlue Swirl         }
2120cb76bbc4SDaniel Henrique Barboza         /* EBB exception */
2121f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
2122cb76bbc4SDaniel Henrique Barboza             /*
2123cb76bbc4SDaniel Henrique Barboza              * EBB exception must be taken in problem state and
2124cb76bbc4SDaniel Henrique Barboza              * with BESCR_GE set.
2125cb76bbc4SDaniel Henrique Barboza              */
2126d41ccf6eSVíctor Colombo             if (FIELD_EX64(env->msr, MSR, PR) &&
2127d41ccf6eSVíctor Colombo                 (env->spr[SPR_BESCR] & BESCR_GE)) {
2128de76b85cSMatheus Ferst                 return PPC_INTERRUPT_EBB;
2129de76b85cSMatheus Ferst             }
2130de76b85cSMatheus Ferst         }
2131de76b85cSMatheus Ferst     }
2132cb76bbc4SDaniel Henrique Barboza 
2133de76b85cSMatheus Ferst     return 0;
2134de76b85cSMatheus Ferst }
2135de76b85cSMatheus Ferst 
21362fdedcbcSMatheus Ferst /*
21372fdedcbcSMatheus Ferst  * Sets CPU_INTERRUPT_HARD if there is at least one unmasked interrupt to be
21382fdedcbcSMatheus Ferst  * delivered and clears CPU_INTERRUPT_HARD otherwise.
21392fdedcbcSMatheus Ferst  *
21402fdedcbcSMatheus Ferst  * This method is called by ppc_set_interrupt when an interrupt is raised or
21412fdedcbcSMatheus Ferst  * lowered, and should also be called whenever an interrupt masking condition
21422fdedcbcSMatheus Ferst  * is changed, e.g.:
21432fdedcbcSMatheus Ferst  *  - When relevant bits of MSR are altered, like EE, HV, PR, etc.;
21442fdedcbcSMatheus Ferst  *  - When relevant bits of LPCR are altered, like PECE, HDICE, HVICE, etc.;
21452fdedcbcSMatheus Ferst  *  - When PSSCR[EC] or env->resume_as_sreset are changed;
21462fdedcbcSMatheus Ferst  *  - When cs->halted is changed and the CPU has a different interrupt masking
21472fdedcbcSMatheus Ferst  *    logic in power-saving mode (e.g., POWER7/8/9/10);
21482fdedcbcSMatheus Ferst  */
21492fdedcbcSMatheus Ferst void ppc_maybe_interrupt(CPUPPCState *env)
21502fdedcbcSMatheus Ferst {
21512fdedcbcSMatheus Ferst     CPUState *cs = env_cpu(env);
215232ead8e6SStefan Hajnoczi     BQL_LOCK_GUARD();
21532fdedcbcSMatheus Ferst 
21542fdedcbcSMatheus Ferst     if (ppc_next_unmasked_interrupt(env)) {
21552fdedcbcSMatheus Ferst         cpu_interrupt(cs, CPU_INTERRUPT_HARD);
21562fdedcbcSMatheus Ferst     } else {
21572fdedcbcSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
21582fdedcbcSMatheus Ferst     }
21592fdedcbcSMatheus Ferst }
21602fdedcbcSMatheus Ferst 
2161f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2162d93a4856SMatheus Ferst static void p7_deliver_interrupt(CPUPPCState *env, int interrupt)
2163d93a4856SMatheus Ferst {
2164d93a4856SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2165d93a4856SMatheus Ferst 
2166d93a4856SMatheus Ferst     switch (interrupt) {
2167d93a4856SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2168d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2169d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2170d93a4856SMatheus Ferst         break;
2171d93a4856SMatheus Ferst 
2172d93a4856SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2173d93a4856SMatheus Ferst         /* HDEC clears on delivery */
2174d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2175d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2176d93a4856SMatheus Ferst         break;
2177d93a4856SMatheus Ferst 
2178d93a4856SMatheus Ferst     case PPC_INTERRUPT_EXT:
2179d93a4856SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2180d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2181d93a4856SMatheus Ferst         } else {
2182d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2183d93a4856SMatheus Ferst         }
2184d93a4856SMatheus Ferst         break;
2185d93a4856SMatheus Ferst 
2186d93a4856SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2187d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2188d93a4856SMatheus Ferst         break;
2189d93a4856SMatheus Ferst     case PPC_INTERRUPT_PERFM:
2190d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2191d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2192d93a4856SMatheus Ferst         break;
2193d93a4856SMatheus Ferst     case 0:
2194d93a4856SMatheus Ferst         /*
2195d93a4856SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
2196d93a4856SMatheus Ferst          * anything to deliver while in a PM state that requires getting
2197d93a4856SMatheus Ferst          * out via a 0x100
2198d93a4856SMatheus Ferst          *
2199d93a4856SMatheus Ferst          * This means we will incorrectly execute past the power management
2200d93a4856SMatheus Ferst          * instruction instead of triggering a reset.
2201d93a4856SMatheus Ferst          *
2202d93a4856SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
2203d93a4856SMatheus Ferst          * processor has_work implementation and the logic in this function.
2204d93a4856SMatheus Ferst          */
2205d93a4856SMatheus Ferst         assert(!env->resume_as_sreset);
2206d93a4856SMatheus Ferst         break;
2207d93a4856SMatheus Ferst     default:
2208bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2209bc30c1c6SBALATON Zoltan                   interrupt);
2210d93a4856SMatheus Ferst     }
2211d93a4856SMatheus Ferst }
2212d93a4856SMatheus Ferst 
22136527e757SMatheus Ferst static void p8_deliver_interrupt(CPUPPCState *env, int interrupt)
22146527e757SMatheus Ferst {
22156527e757SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
22166527e757SMatheus Ferst 
22176527e757SMatheus Ferst     switch (interrupt) {
22186527e757SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
22196527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
22206527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
22216527e757SMatheus Ferst         break;
22226527e757SMatheus Ferst 
22236527e757SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
22246527e757SMatheus Ferst         /* HDEC clears on delivery */
22256527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
22266527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
22276527e757SMatheus Ferst         break;
22286527e757SMatheus Ferst 
22296527e757SMatheus Ferst     case PPC_INTERRUPT_EXT:
22306527e757SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
22316527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
22326527e757SMatheus Ferst         } else {
22336527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
22346527e757SMatheus Ferst         }
22356527e757SMatheus Ferst         break;
22366527e757SMatheus Ferst 
22376527e757SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
22386527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
22396527e757SMatheus Ferst         break;
22406527e757SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
22416527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
22426527e757SMatheus Ferst         if (is_book3s_arch2x(env)) {
22436527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
22446527e757SMatheus Ferst         } else {
22456527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
22466527e757SMatheus Ferst         }
22476527e757SMatheus Ferst         break;
22486527e757SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
22496527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
22506527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
22516527e757SMatheus Ferst         break;
22526527e757SMatheus Ferst     case PPC_INTERRUPT_PERFM:
22536527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
22546527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
22556527e757SMatheus Ferst         break;
22566527e757SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
22576527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
22586527e757SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
22596527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
22606527e757SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
22616527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
22626527e757SMatheus Ferst         }
22636527e757SMatheus Ferst         break;
22646527e757SMatheus Ferst     case 0:
22656527e757SMatheus Ferst         /*
22666527e757SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
22676527e757SMatheus Ferst          * anything to deliver while in a PM state that requires getting
22686527e757SMatheus Ferst          * out via a 0x100
22696527e757SMatheus Ferst          *
22706527e757SMatheus Ferst          * This means we will incorrectly execute past the power management
22716527e757SMatheus Ferst          * instruction instead of triggering a reset.
22726527e757SMatheus Ferst          *
22736527e757SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
22746527e757SMatheus Ferst          * processor has_work implementation and the logic in this function.
22756527e757SMatheus Ferst          */
22766527e757SMatheus Ferst         assert(!env->resume_as_sreset);
22776527e757SMatheus Ferst         break;
22786527e757SMatheus Ferst     default:
2279bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2280bc30c1c6SBALATON Zoltan                   interrupt);
22816527e757SMatheus Ferst     }
22826527e757SMatheus Ferst }
22836527e757SMatheus Ferst 
22843654e238SMatheus Ferst static void p9_deliver_interrupt(CPUPPCState *env, int interrupt)
22853654e238SMatheus Ferst {
22863654e238SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
22873654e238SMatheus Ferst     CPUState *cs = env_cpu(env);
22883654e238SMatheus Ferst 
228927796411SMatheus Ferst     if (cs->halted && !(env->spr[SPR_PSSCR] & PSSCR_EC) &&
229027796411SMatheus Ferst         !FIELD_EX64(env->msr, MSR, EE)) {
229127796411SMatheus Ferst         /*
229227796411SMatheus Ferst          * A pending interrupt took us out of power-saving, but MSR[EE] says
229327796411SMatheus Ferst          * that we should return to NIP+4 instead of delivering it.
229427796411SMatheus Ferst          */
229527796411SMatheus Ferst         return;
229627796411SMatheus Ferst     }
229727796411SMatheus Ferst 
22983654e238SMatheus Ferst     switch (interrupt) {
22993654e238SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
23003654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
23013654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
23023654e238SMatheus Ferst         break;
23033654e238SMatheus Ferst 
23043654e238SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
23053654e238SMatheus Ferst         /* HDEC clears on delivery */
23063654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
23073654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
23083654e238SMatheus Ferst         break;
23093654e238SMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
23103654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
23113654e238SMatheus Ferst         break;
23123654e238SMatheus Ferst 
23133654e238SMatheus Ferst     case PPC_INTERRUPT_EXT:
23143654e238SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
23153654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
23163654e238SMatheus Ferst         } else {
23173654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
23183654e238SMatheus Ferst         }
23193654e238SMatheus Ferst         break;
23203654e238SMatheus Ferst 
23213654e238SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
23223654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
23233654e238SMatheus Ferst         break;
23243654e238SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
23253654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
23263654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
23273654e238SMatheus Ferst         break;
23283654e238SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
23293654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
23303654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
23313654e238SMatheus Ferst         break;
23323654e238SMatheus Ferst     case PPC_INTERRUPT_PERFM:
23333654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
23343654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
23353654e238SMatheus Ferst         break;
23363654e238SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
23373654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
23383654e238SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
23393654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
23403654e238SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
23413654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
23423654e238SMatheus Ferst         }
23433654e238SMatheus Ferst         break;
23443654e238SMatheus Ferst     case 0:
23453654e238SMatheus Ferst         /*
23463654e238SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
23473654e238SMatheus Ferst          * anything to deliver while in a PM state that requires getting
23483654e238SMatheus Ferst          * out via a 0x100
23493654e238SMatheus Ferst          *
23503654e238SMatheus Ferst          * This means we will incorrectly execute past the power management
23513654e238SMatheus Ferst          * instruction instead of triggering a reset.
23523654e238SMatheus Ferst          *
23533654e238SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
23543654e238SMatheus Ferst          * processor has_work implementation and the logic in this function.
23553654e238SMatheus Ferst          */
23563654e238SMatheus Ferst         assert(!env->resume_as_sreset);
23573654e238SMatheus Ferst         break;
23583654e238SMatheus Ferst     default:
2359bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2360bc30c1c6SBALATON Zoltan                   interrupt);
23613654e238SMatheus Ferst     }
23623654e238SMatheus Ferst }
2363f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
23643654e238SMatheus Ferst 
2365868cb6baSBALATON Zoltan static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt)
2366de76b85cSMatheus Ferst {
2367868cb6baSBALATON Zoltan #ifdef TARGET_PPC64
2368868cb6baSBALATON Zoltan     switch (env->excp_model) {
2369868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER7:
2370868cb6baSBALATON Zoltan         return p7_deliver_interrupt(env, interrupt);
2371868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER8:
2372868cb6baSBALATON Zoltan         return p8_deliver_interrupt(env, interrupt);
2373868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER9:
2374868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER10:
2375868cb6baSBALATON Zoltan         return p9_deliver_interrupt(env, interrupt);
2376868cb6baSBALATON Zoltan     default:
2377868cb6baSBALATON Zoltan         break;
2378868cb6baSBALATON Zoltan     }
2379de76b85cSMatheus Ferst #endif
2380de76b85cSMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2381de76b85cSMatheus Ferst 
2382de76b85cSMatheus Ferst     switch (interrupt) {
2383de76b85cSMatheus Ferst     case PPC_INTERRUPT_RESET: /* External reset */
2384de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_RESET;
2385de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_RESET);
2386de76b85cSMatheus Ferst         break;
2387de76b85cSMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2388de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2389de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2390de76b85cSMatheus Ferst         break;
2391de76b85cSMatheus Ferst 
2392de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2393de76b85cSMatheus Ferst         /* HDEC clears on delivery */
2394de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2395de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2396de76b85cSMatheus Ferst         break;
2397de76b85cSMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
2398de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2399de76b85cSMatheus Ferst         break;
2400de76b85cSMatheus Ferst 
2401de76b85cSMatheus Ferst     case PPC_INTERRUPT_EXT:
2402de76b85cSMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2403de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2404de76b85cSMatheus Ferst         } else {
2405de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2406de76b85cSMatheus Ferst         }
2407de76b85cSMatheus Ferst         break;
2408de76b85cSMatheus Ferst     case PPC_INTERRUPT_CEXT: /* External critical interrupt */
2409de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_CRITICAL);
2410de76b85cSMatheus Ferst         break;
2411de76b85cSMatheus Ferst 
2412de76b85cSMatheus Ferst     case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */
2413de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_WDT;
2414de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_WDT);
2415de76b85cSMatheus Ferst         break;
2416de76b85cSMatheus Ferst     case PPC_INTERRUPT_CDOORBELL:
2417de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL;
2418de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DOORCI);
2419de76b85cSMatheus Ferst         break;
2420de76b85cSMatheus Ferst     case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */
2421de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_FIT;
2422de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_FIT);
2423de76b85cSMatheus Ferst         break;
2424de76b85cSMatheus Ferst     case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */
2425de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PIT;
2426de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PIT);
2427de76b85cSMatheus Ferst         break;
2428de76b85cSMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2429de76b85cSMatheus Ferst         if (ppc_decr_clear_on_delivery(env)) {
2430de76b85cSMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DECR;
2431de76b85cSMatheus Ferst         }
2432de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2433de76b85cSMatheus Ferst         break;
2434de76b85cSMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
2435de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
2436de76b85cSMatheus Ferst         if (is_book3s_arch2x(env)) {
2437de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
2438de76b85cSMatheus Ferst         } else {
2439de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
2440de76b85cSMatheus Ferst         }
2441de76b85cSMatheus Ferst         break;
2442de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
2443de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
2444de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
2445de76b85cSMatheus Ferst         break;
2446de76b85cSMatheus Ferst     case PPC_INTERRUPT_PERFM:
2447de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2448de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2449de76b85cSMatheus Ferst         break;
2450de76b85cSMatheus Ferst     case PPC_INTERRUPT_THERM:  /* Thermal interrupt */
2451de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_THERM;
2452de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_THERM);
2453de76b85cSMatheus Ferst         break;
2454de76b85cSMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
2455de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
2456cb76bbc4SDaniel Henrique Barboza         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
2457cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
2458cb76bbc4SDaniel Henrique Barboza         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
2459cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
2460cb76bbc4SDaniel Henrique Barboza         }
2461de76b85cSMatheus Ferst         break;
2462de76b85cSMatheus Ferst     case 0:
2463f8154fd2SBenjamin Herrenschmidt         /*
2464f8154fd2SBenjamin Herrenschmidt          * This is a bug ! It means that has_work took us out of halt without
2465f8154fd2SBenjamin Herrenschmidt          * anything to deliver while in a PM state that requires getting
2466f8154fd2SBenjamin Herrenschmidt          * out via a 0x100
2467f8154fd2SBenjamin Herrenschmidt          *
2468f8154fd2SBenjamin Herrenschmidt          * This means we will incorrectly execute past the power management
2469f8154fd2SBenjamin Herrenschmidt          * instruction instead of triggering a reset.
2470f8154fd2SBenjamin Herrenschmidt          *
2471136fbf65Szhaolichang          * It generally means a discrepancy between the wakeup conditions in the
2472f8154fd2SBenjamin Herrenschmidt          * processor has_work implementation and the logic in this function.
2473f8154fd2SBenjamin Herrenschmidt          */
2474de76b85cSMatheus Ferst         assert(!env->resume_as_sreset);
2475de76b85cSMatheus Ferst         break;
2476de76b85cSMatheus Ferst     default:
2477bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2478bc30c1c6SBALATON Zoltan                   interrupt);
2479ba2898f7SMatheus Ferst     }
2480ba2898f7SMatheus Ferst }
2481ba2898f7SMatheus Ferst 
2482b5b7f391SNicholas Piggin void ppc_cpu_do_system_reset(CPUState *cs)
248334316482SAlexey Kardashevskiy {
248434316482SAlexey Kardashevskiy     PowerPCCPU *cpu = POWERPC_CPU(cs);
248534316482SAlexey Kardashevskiy 
248693130c84SFabiano Rosas     powerpc_excp(cpu, POWERPC_EXCP_RESET);
248734316482SAlexey Kardashevskiy }
2488ad77c6caSNicholas Piggin 
2489ad77c6caSNicholas Piggin void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector)
2490ad77c6caSNicholas Piggin {
2491ad77c6caSNicholas Piggin     PowerPCCPU *cpu = POWERPC_CPU(cs);
2492ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
2493ad77c6caSNicholas Piggin     target_ulong msr = 0;
2494ad77c6caSNicholas Piggin 
2495ad77c6caSNicholas Piggin     /*
2496ad77c6caSNicholas Piggin      * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already
2497ad77c6caSNicholas Piggin      * been set by KVM.
2498ad77c6caSNicholas Piggin      */
2499ad77c6caSNicholas Piggin     msr = (1ULL << MSR_ME);
2500ad77c6caSNicholas Piggin     msr |= env->msr & (1ULL << MSR_SF);
2501516fc103SFabiano Rosas     if (ppc_interrupts_little_endian(cpu, false)) {
2502ad77c6caSNicholas Piggin         msr |= (1ULL << MSR_LE);
2503ad77c6caSNicholas Piggin     }
2504ad77c6caSNicholas Piggin 
25057cebc5dbSNicholas Piggin     /* Anything for nested required here? MSR[HV] bit? */
25067cebc5dbSNicholas Piggin 
2507ad77c6caSNicholas Piggin     powerpc_set_excp_state(cpu, vector, msr);
2508ad77c6caSNicholas Piggin }
2509c79c73f6SBlue Swirl 
2510458dd766SRichard Henderson bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
2511458dd766SRichard Henderson {
2512794511bcSPhilippe Mathieu-Daudé     CPUPPCState *env = cpu_env(cs);
2513de76b85cSMatheus Ferst     int interrupt;
2514458dd766SRichard Henderson 
2515de76b85cSMatheus Ferst     if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) {
2516de76b85cSMatheus Ferst         return false;
2517de76b85cSMatheus Ferst     }
2518de76b85cSMatheus Ferst 
2519de76b85cSMatheus Ferst     interrupt = ppc_next_unmasked_interrupt(env);
2520de76b85cSMatheus Ferst     if (interrupt == 0) {
2521de76b85cSMatheus Ferst         return false;
2522de76b85cSMatheus Ferst     }
2523de76b85cSMatheus Ferst 
2524de76b85cSMatheus Ferst     ppc_deliver_interrupt(env, interrupt);
2525458dd766SRichard Henderson     if (env->pending_interrupts == 0) {
2526de76b85cSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
2527458dd766SRichard Henderson     }
2528458dd766SRichard Henderson     return true;
2529458dd766SRichard Henderson }
2530458dd766SRichard Henderson 
2531f725245cSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
2532f725245cSPhilippe Mathieu-Daudé 
2533ad71ed68SBlue Swirl /*****************************************************************************/
2534ad71ed68SBlue Swirl /* Exceptions processing helpers */
2535ad71ed68SBlue Swirl 
2536db789c6cSBenjamin Herrenschmidt void raise_exception_err_ra(CPUPPCState *env, uint32_t exception,
2537db789c6cSBenjamin Herrenschmidt                             uint32_t error_code, uintptr_t raddr)
2538ad71ed68SBlue Swirl {
2539db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
254027103424SAndreas Färber 
254127103424SAndreas Färber     cs->exception_index = exception;
2542ad71ed68SBlue Swirl     env->error_code = error_code;
2543db789c6cSBenjamin Herrenschmidt     cpu_loop_exit_restore(cs, raddr);
2544db789c6cSBenjamin Herrenschmidt }
2545db789c6cSBenjamin Herrenschmidt 
2546db789c6cSBenjamin Herrenschmidt void raise_exception_err(CPUPPCState *env, uint32_t exception,
2547db789c6cSBenjamin Herrenschmidt                          uint32_t error_code)
2548db789c6cSBenjamin Herrenschmidt {
2549db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2550db789c6cSBenjamin Herrenschmidt }
2551db789c6cSBenjamin Herrenschmidt 
2552db789c6cSBenjamin Herrenschmidt void raise_exception(CPUPPCState *env, uint32_t exception)
2553db789c6cSBenjamin Herrenschmidt {
2554db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2555db789c6cSBenjamin Herrenschmidt }
2556db789c6cSBenjamin Herrenschmidt 
2557db789c6cSBenjamin Herrenschmidt void raise_exception_ra(CPUPPCState *env, uint32_t exception,
2558db789c6cSBenjamin Herrenschmidt                         uintptr_t raddr)
2559db789c6cSBenjamin Herrenschmidt {
2560db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, raddr);
2561db789c6cSBenjamin Herrenschmidt }
2562db789c6cSBenjamin Herrenschmidt 
25632b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2564db789c6cSBenjamin Herrenschmidt void helper_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);
2568ad71ed68SBlue Swirl }
2569ad71ed68SBlue Swirl 
2570e5f17ac6SBlue Swirl void helper_raise_exception(CPUPPCState *env, uint32_t exception)
2571ad71ed68SBlue Swirl {
2572db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2573ad71ed68SBlue Swirl }
2574ad71ed68SBlue Swirl 
2575f6c2d68bSBALATON Zoltan #ifndef CONFIG_USER_ONLY
2576e5f17ac6SBlue Swirl void helper_store_msr(CPUPPCState *env, target_ulong val)
2577ad71ed68SBlue Swirl {
2578db789c6cSBenjamin Herrenschmidt     uint32_t excp = hreg_store_msr(env, val, 0);
2579259186a7SAndreas Färber 
2580db789c6cSBenjamin Herrenschmidt     if (excp != 0) {
25810661329aSBALATON Zoltan         cpu_interrupt_exittb(env_cpu(env));
2582db789c6cSBenjamin Herrenschmidt         raise_exception(env, excp);
2583ad71ed68SBlue Swirl     }
2584ad71ed68SBlue Swirl }
2585ad71ed68SBlue Swirl 
25862fdedcbcSMatheus Ferst void helper_ppc_maybe_interrupt(CPUPPCState *env)
25872fdedcbcSMatheus Ferst {
25882fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
25892fdedcbcSMatheus Ferst }
25902fdedcbcSMatheus Ferst 
2591f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2592f43520e5SRichard Henderson void helper_scv(CPUPPCState *env, uint32_t lev)
2593f43520e5SRichard Henderson {
2594f43520e5SRichard Henderson     if (env->spr[SPR_FSCR] & (1ull << FSCR_SCV)) {
2595f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_SYSCALL_VECTORED, lev);
2596f43520e5SRichard Henderson     } else {
2597f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_FU, FSCR_IC_SCV);
2598f43520e5SRichard Henderson     }
2599f43520e5SRichard Henderson }
2600f43520e5SRichard Henderson 
260107e4804fSCédric Le Goater void helper_pminsn(CPUPPCState *env, uint32_t insn)
26027778a575SBenjamin Herrenschmidt {
26034e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
26047778a575SBenjamin Herrenschmidt 
26057778a575SBenjamin Herrenschmidt     cs->halted = 1;
26067778a575SBenjamin Herrenschmidt 
26073621e2c9SBenjamin Herrenschmidt     /* Condition for waking up at 0x100 */
26081e7fd61dSBenjamin Herrenschmidt     env->resume_as_sreset = (insn != PPC_PM_STOP) ||
260921c0d66aSBenjamin Herrenschmidt         (env->spr[SPR_PSSCR] & PSSCR_EC);
26102fdedcbcSMatheus Ferst 
26119915dac4SNicholas Piggin     /* HDECR is not to wake from PM state, it may have already fired */
26129915dac4SNicholas Piggin     if (env->resume_as_sreset) {
26139915dac4SNicholas Piggin         PowerPCCPU *cpu = env_archcpu(env);
26149915dac4SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 0);
26159915dac4SNicholas Piggin     }
26169915dac4SNicholas Piggin 
26172fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
26187778a575SBenjamin Herrenschmidt }
2619f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
26207778a575SBenjamin Herrenschmidt 
262162e79ef9SCédric Le Goater static void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr)
2622ad71ed68SBlue Swirl {
2623a2e71b28SBenjamin Herrenschmidt     /* MSR:POW cannot be set by any form of rfi */
2624a2e71b28SBenjamin Herrenschmidt     msr &= ~(1ULL << MSR_POW);
2625a2e71b28SBenjamin Herrenschmidt 
26265aad0457SChristophe Leroy     /* MSR:TGPR cannot be set by any form of rfi */
26275aad0457SChristophe Leroy     if (env->flags & POWERPC_FLAG_TGPR)
26285aad0457SChristophe Leroy         msr &= ~(1ULL << MSR_TGPR);
26295aad0457SChristophe Leroy 
2630f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2631a2e71b28SBenjamin Herrenschmidt     /* Switching to 32-bit ? Crop the nip */
2632a2e71b28SBenjamin Herrenschmidt     if (!msr_is_64bit(env, msr)) {
2633ad71ed68SBlue Swirl         nip = (uint32_t)nip;
2634ad71ed68SBlue Swirl     }
2635ad71ed68SBlue Swirl #else
2636ad71ed68SBlue Swirl     nip = (uint32_t)nip;
2637ad71ed68SBlue Swirl #endif
2638ad71ed68SBlue Swirl     /* XXX: beware: this is false if VLE is supported */
2639ad71ed68SBlue Swirl     env->nip = nip & ~((target_ulong)0x00000003);
2640ad71ed68SBlue Swirl     hreg_store_msr(env, msr, 1);
26412eb1ef73SCédric Le Goater     trace_ppc_excp_rfi(env->nip, env->msr);
264247733729SDavid Gibson     /*
264347733729SDavid Gibson      * No need to raise an exception here, as rfi is always the last
264447733729SDavid Gibson      * insn of a TB
2645ad71ed68SBlue Swirl      */
26460661329aSBALATON Zoltan     cpu_interrupt_exittb(env_cpu(env));
2647a8b73734SNikunj A Dadhania     /* Reset the reservation */
2648a8b73734SNikunj A Dadhania     env->reserve_addr = -1;
2649a8b73734SNikunj A Dadhania 
2650cd0c6f47SBenjamin Herrenschmidt     /* Context synchronizing: check if TCG TLB needs flush */
2651e3cffe6fSNikunj A Dadhania     check_tlb_flush(env, false);
2652ad71ed68SBlue Swirl }
2653ad71ed68SBlue Swirl 
2654e5f17ac6SBlue Swirl void helper_rfi(CPUPPCState *env)
2655ad71ed68SBlue Swirl {
2656a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful);
2657a1bb7384SScott Wood }
2658ad71ed68SBlue Swirl 
2659f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2660e5f17ac6SBlue Swirl void helper_rfid(CPUPPCState *env)
2661ad71ed68SBlue Swirl {
266247733729SDavid Gibson     /*
2663136fbf65Szhaolichang      * The architecture defines a number of rules for which bits can
266447733729SDavid Gibson      * change but in practice, we handle this in hreg_store_msr()
2665a2e71b28SBenjamin Herrenschmidt      * which will be called by do_rfi(), so there is no need to filter
2666a2e71b28SBenjamin Herrenschmidt      * here
2667a2e71b28SBenjamin Herrenschmidt      */
2668a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]);
2669ad71ed68SBlue Swirl }
2670ad71ed68SBlue Swirl 
26713c89b8d6SNicholas Piggin void helper_rfscv(CPUPPCState *env)
26723c89b8d6SNicholas Piggin {
26733c89b8d6SNicholas Piggin     do_rfi(env, env->lr, env->ctr);
26743c89b8d6SNicholas Piggin }
26753c89b8d6SNicholas Piggin 
2676e5f17ac6SBlue Swirl void helper_hrfid(CPUPPCState *env)
2677ad71ed68SBlue Swirl {
2678a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
2679ad71ed68SBlue Swirl }
2680ad71ed68SBlue Swirl 
26811f26c751SDaniel Henrique Barboza void helper_rfebb(CPUPPCState *env, target_ulong s)
26821f26c751SDaniel Henrique Barboza {
26831f26c751SDaniel Henrique Barboza     target_ulong msr = env->msr;
26841f26c751SDaniel Henrique Barboza 
26851f26c751SDaniel Henrique Barboza     /*
26861f26c751SDaniel Henrique Barboza      * Handling of BESCR bits 32:33 according to PowerISA v3.1:
26871f26c751SDaniel Henrique Barboza      *
26881f26c751SDaniel Henrique Barboza      * "If BESCR 32:33 != 0b00 the instruction is treated as if
26891f26c751SDaniel Henrique Barboza      *  the instruction form were invalid."
26901f26c751SDaniel Henrique Barboza      */
26911f26c751SDaniel Henrique Barboza     if (env->spr[SPR_BESCR] & BESCR_INVALID) {
26921f26c751SDaniel Henrique Barboza         raise_exception_err(env, POWERPC_EXCP_PROGRAM,
26931f26c751SDaniel Henrique Barboza                             POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
26941f26c751SDaniel Henrique Barboza     }
26951f26c751SDaniel Henrique Barboza 
26961f26c751SDaniel Henrique Barboza     env->nip = env->spr[SPR_EBBRR];
26971f26c751SDaniel Henrique Barboza 
26981f26c751SDaniel Henrique Barboza     /* Switching to 32-bit ? Crop the nip */
26991f26c751SDaniel Henrique Barboza     if (!msr_is_64bit(env, msr)) {
27001f26c751SDaniel Henrique Barboza         env->nip = (uint32_t)env->spr[SPR_EBBRR];
27011f26c751SDaniel Henrique Barboza     }
27021f26c751SDaniel Henrique Barboza 
27031f26c751SDaniel Henrique Barboza     if (s) {
27041f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_GE;
27051f26c751SDaniel Henrique Barboza     } else {
27061f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
27071f26c751SDaniel Henrique Barboza     }
27081f26c751SDaniel Henrique Barboza }
2709d3412df2SDaniel Henrique Barboza 
2710d3412df2SDaniel Henrique Barboza /*
2711d3412df2SDaniel Henrique Barboza  * Triggers or queues an 'ebb_excp' EBB exception. All checks
2712d3412df2SDaniel Henrique Barboza  * but FSCR, HFSCR and msr_pr must be done beforehand.
2713d3412df2SDaniel Henrique Barboza  *
2714d3412df2SDaniel Henrique Barboza  * PowerISA v3.1 isn't clear about whether an EBB should be
2715d3412df2SDaniel Henrique Barboza  * postponed or cancelled if the EBB facility is unavailable.
2716d3412df2SDaniel Henrique Barboza  * Our assumption here is that the EBB is cancelled if both
2717d3412df2SDaniel Henrique Barboza  * FSCR and HFSCR EBB facilities aren't available.
2718d3412df2SDaniel Henrique Barboza  */
2719d3412df2SDaniel Henrique Barboza static void do_ebb(CPUPPCState *env, int ebb_excp)
2720d3412df2SDaniel Henrique Barboza {
2721d3412df2SDaniel Henrique Barboza     PowerPCCPU *cpu = env_archcpu(env);
2722d3412df2SDaniel Henrique Barboza 
2723d3412df2SDaniel Henrique Barboza     /*
2724d3412df2SDaniel Henrique Barboza      * FSCR_EBB and FSCR_IC_EBB are the same bits used with
2725d3412df2SDaniel Henrique Barboza      * HFSCR.
2726d3412df2SDaniel Henrique Barboza      */
2727d3412df2SDaniel Henrique Barboza     helper_fscr_facility_check(env, FSCR_EBB, 0, FSCR_IC_EBB);
2728d3412df2SDaniel Henrique Barboza     helper_hfscr_facility_check(env, FSCR_EBB, "EBB", FSCR_IC_EBB);
2729d3412df2SDaniel Henrique Barboza 
2730d3412df2SDaniel Henrique Barboza     if (ebb_excp == POWERPC_EXCP_PERFM_EBB) {
2731d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_PMEO;
2732d3412df2SDaniel Henrique Barboza     } else if (ebb_excp == POWERPC_EXCP_EXTERNAL_EBB) {
2733d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_EEO;
2734d3412df2SDaniel Henrique Barboza     }
2735d3412df2SDaniel Henrique Barboza 
2736d41ccf6eSVíctor Colombo     if (FIELD_EX64(env->msr, MSR, PR)) {
2737d3412df2SDaniel Henrique Barboza         powerpc_excp(cpu, ebb_excp);
2738d3412df2SDaniel Henrique Barboza     } else {
27397b694df6SMatheus Ferst         ppc_set_irq(cpu, PPC_INTERRUPT_EBB, 1);
2740d3412df2SDaniel Henrique Barboza     }
2741d3412df2SDaniel Henrique Barboza }
2742d3412df2SDaniel Henrique Barboza 
2743d3412df2SDaniel Henrique Barboza void raise_ebb_perfm_exception(CPUPPCState *env)
2744d3412df2SDaniel Henrique Barboza {
2745d3412df2SDaniel Henrique Barboza     bool perfm_ebb_enabled = env->spr[SPR_POWER_MMCR0] & MMCR0_EBE &&
2746d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_PME &&
2747d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_GE;
2748d3412df2SDaniel Henrique Barboza 
2749d3412df2SDaniel Henrique Barboza     if (!perfm_ebb_enabled) {
2750d3412df2SDaniel Henrique Barboza         return;
2751d3412df2SDaniel Henrique Barboza     }
2752d3412df2SDaniel Henrique Barboza 
2753d3412df2SDaniel Henrique Barboza     do_ebb(env, POWERPC_EXCP_PERFM_EBB);
2754d3412df2SDaniel Henrique Barboza }
2755754920c7SBALATON Zoltan #endif /* TARGET_PPC64 */
27561f26c751SDaniel Henrique Barboza 
2757ad71ed68SBlue Swirl /*****************************************************************************/
2758ad71ed68SBlue Swirl /* Embedded PowerPC specific helpers */
2759e5f17ac6SBlue Swirl void helper_40x_rfci(CPUPPCState *env)
2760ad71ed68SBlue Swirl {
2761a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]);
2762ad71ed68SBlue Swirl }
2763ad71ed68SBlue Swirl 
2764e5f17ac6SBlue Swirl void helper_rfci(CPUPPCState *env)
2765ad71ed68SBlue Swirl {
2766a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]);
2767ad71ed68SBlue Swirl }
2768ad71ed68SBlue Swirl 
2769e5f17ac6SBlue Swirl void helper_rfdi(CPUPPCState *env)
2770ad71ed68SBlue Swirl {
2771a1bb7384SScott Wood     /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
2772a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]);
2773ad71ed68SBlue Swirl }
2774ad71ed68SBlue Swirl 
2775e5f17ac6SBlue Swirl void helper_rfmci(CPUPPCState *env)
2776ad71ed68SBlue Swirl {
2777a1bb7384SScott Wood     /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
2778a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
2779ad71ed68SBlue Swirl }
27805ca958cfSBALATON Zoltan #endif /* !CONFIG_USER_ONLY */
2781ad71ed68SBlue Swirl 
2782ae556c6aSChinmay Rath void helper_TW(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2783e5f17ac6SBlue Swirl                uint32_t flags)
2784ad71ed68SBlue Swirl {
2785ad71ed68SBlue Swirl     if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
2786ad71ed68SBlue Swirl                   ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
2787ad71ed68SBlue Swirl                   ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
2788ad71ed68SBlue Swirl                   ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
2789ad71ed68SBlue Swirl                   ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
279072073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
279172073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2792ad71ed68SBlue Swirl     }
2793ad71ed68SBlue Swirl }
2794ad71ed68SBlue Swirl 
2795f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2796ae556c6aSChinmay Rath void helper_TD(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2797e5f17ac6SBlue Swirl                uint32_t flags)
2798ad71ed68SBlue Swirl {
2799ad71ed68SBlue Swirl     if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
2800ad71ed68SBlue Swirl                   ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
2801ad71ed68SBlue Swirl                   ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
2802ad71ed68SBlue Swirl                   ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
2803ad71ed68SBlue Swirl                   ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) {
280472073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
280572073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2806ad71ed68SBlue Swirl     }
2807ad71ed68SBlue Swirl }
2808f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
2809ad71ed68SBlue Swirl 
2810670f1da3SVíctor Colombo static uint32_t helper_SIMON_LIKE_32_64(uint32_t x, uint64_t key, uint32_t lane)
2811670f1da3SVíctor Colombo {
2812670f1da3SVíctor Colombo     const uint16_t c = 0xfffc;
2813670f1da3SVíctor Colombo     const uint64_t z0 = 0xfa2561cdf44ac398ULL;
2814670f1da3SVíctor Colombo     uint16_t z = 0, temp;
2815670f1da3SVíctor Colombo     uint16_t k[32], eff_k[32], xleft[33], xright[33], fxleft[32];
2816670f1da3SVíctor Colombo 
2817670f1da3SVíctor Colombo     for (int i = 3; i >= 0; i--) {
2818670f1da3SVíctor Colombo         k[i] = key & 0xffff;
2819670f1da3SVíctor Colombo         key >>= 16;
2820670f1da3SVíctor Colombo     }
2821670f1da3SVíctor Colombo     xleft[0] = x & 0xffff;
2822670f1da3SVíctor Colombo     xright[0] = (x >> 16) & 0xffff;
2823670f1da3SVíctor Colombo 
2824670f1da3SVíctor Colombo     for (int i = 0; i < 28; i++) {
2825670f1da3SVíctor Colombo         z = (z0 >> (63 - i)) & 1;
2826670f1da3SVíctor Colombo         temp = ror16(k[i + 3], 3) ^ k[i + 1];
2827670f1da3SVíctor Colombo         k[i + 4] = c ^ z ^ k[i] ^ temp ^ ror16(temp, 1);
2828670f1da3SVíctor Colombo     }
2829670f1da3SVíctor Colombo 
2830670f1da3SVíctor Colombo     for (int i = 0; i < 8; i++) {
2831670f1da3SVíctor Colombo         eff_k[4 * i + 0] = k[4 * i + ((0 + lane) % 4)];
2832670f1da3SVíctor Colombo         eff_k[4 * i + 1] = k[4 * i + ((1 + lane) % 4)];
2833670f1da3SVíctor Colombo         eff_k[4 * i + 2] = k[4 * i + ((2 + lane) % 4)];
2834670f1da3SVíctor Colombo         eff_k[4 * i + 3] = k[4 * i + ((3 + lane) % 4)];
2835670f1da3SVíctor Colombo     }
2836670f1da3SVíctor Colombo 
2837670f1da3SVíctor Colombo     for (int i = 0; i < 32; i++) {
2838670f1da3SVíctor Colombo         fxleft[i] = (rol16(xleft[i], 1) &
2839670f1da3SVíctor Colombo             rol16(xleft[i], 8)) ^ rol16(xleft[i], 2);
2840670f1da3SVíctor Colombo         xleft[i + 1] = xright[i] ^ fxleft[i] ^ eff_k[i];
2841670f1da3SVíctor Colombo         xright[i + 1] = xleft[i];
2842670f1da3SVíctor Colombo     }
2843670f1da3SVíctor Colombo 
2844670f1da3SVíctor Colombo     return (((uint32_t)xright[32]) << 16) | xleft[32];
2845670f1da3SVíctor Colombo }
2846670f1da3SVíctor Colombo 
2847670f1da3SVíctor Colombo static uint64_t hash_digest(uint64_t ra, uint64_t rb, uint64_t key)
2848670f1da3SVíctor Colombo {
2849670f1da3SVíctor Colombo     uint64_t stage0_h = 0ULL, stage0_l = 0ULL;
2850670f1da3SVíctor Colombo     uint64_t stage1_h, stage1_l;
2851670f1da3SVíctor Colombo 
2852670f1da3SVíctor Colombo     for (int i = 0; i < 4; i++) {
2853670f1da3SVíctor Colombo         stage0_h |= ror64(rb & 0xff, 8 * (2 * i + 1));
2854670f1da3SVíctor Colombo         stage0_h |= ((ra >> 32) & 0xff) << (8 * 2 * i);
2855670f1da3SVíctor Colombo         stage0_l |= ror64((rb >> 32) & 0xff, 8 * (2 * i + 1));
2856670f1da3SVíctor Colombo         stage0_l |= (ra & 0xff) << (8 * 2 * i);
2857670f1da3SVíctor Colombo         rb >>= 8;
2858670f1da3SVíctor Colombo         ra >>= 8;
2859670f1da3SVíctor Colombo     }
2860670f1da3SVíctor Colombo 
2861670f1da3SVíctor Colombo     stage1_h = (uint64_t)helper_SIMON_LIKE_32_64(stage0_h >> 32, key, 0) << 32;
2862670f1da3SVíctor Colombo     stage1_h |= helper_SIMON_LIKE_32_64(stage0_h, key, 1);
2863670f1da3SVíctor Colombo     stage1_l = (uint64_t)helper_SIMON_LIKE_32_64(stage0_l >> 32, key, 2) << 32;
2864670f1da3SVíctor Colombo     stage1_l |= helper_SIMON_LIKE_32_64(stage0_l, key, 3);
2865670f1da3SVíctor Colombo 
2866670f1da3SVíctor Colombo     return stage1_h ^ stage1_l;
2867670f1da3SVíctor Colombo }
2868670f1da3SVíctor Colombo 
28694091fabfSNicholas Miehlbradt static void do_hash(CPUPPCState *env, target_ulong ea, target_ulong ra,
28704091fabfSNicholas Miehlbradt                     target_ulong rb, uint64_t key, bool store)
28714091fabfSNicholas Miehlbradt {
28724091fabfSNicholas Miehlbradt     uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash;
28734091fabfSNicholas Miehlbradt 
28744091fabfSNicholas Miehlbradt     if (store) {
28754091fabfSNicholas Miehlbradt         cpu_stq_data_ra(env, ea, calculated_hash, GETPC());
28764091fabfSNicholas Miehlbradt     } else {
28774091fabfSNicholas Miehlbradt         loaded_hash = cpu_ldq_data_ra(env, ea, GETPC());
28784091fabfSNicholas Miehlbradt         if (loaded_hash != calculated_hash) {
28794091fabfSNicholas Miehlbradt             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
28804091fabfSNicholas Miehlbradt                 POWERPC_EXCP_TRAP, GETPC());
28814091fabfSNicholas Miehlbradt         }
28824091fabfSNicholas Miehlbradt     }
28834091fabfSNicholas Miehlbradt }
28844091fabfSNicholas Miehlbradt 
2885670f1da3SVíctor Colombo #include "qemu/guest-random.h"
2886670f1da3SVíctor Colombo 
28874091fabfSNicholas Miehlbradt #ifdef TARGET_PPC64
28884091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect)                             \
2889670f1da3SVíctor Colombo void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
2890670f1da3SVíctor Colombo                  target_ulong rb)                                             \
2891670f1da3SVíctor Colombo {                                                                             \
28924091fabfSNicholas Miehlbradt     if (env->msr & R_MSR_PR_MASK) {                                           \
28934091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_DEXCR] & R_DEXCR_PRO_##dexcr_aspect##_MASK ||      \
28944091fabfSNicholas Miehlbradt             env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK))       \
28954091fabfSNicholas Miehlbradt             return;                                                           \
28964091fabfSNicholas Miehlbradt     } else if (!(env->msr & R_MSR_HV_MASK)) {                                 \
28974091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_DEXCR] & R_DEXCR_PNH_##dexcr_aspect##_MASK ||      \
28984091fabfSNicholas Miehlbradt             env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK))       \
28994091fabfSNicholas Miehlbradt             return;                                                           \
29004091fabfSNicholas Miehlbradt     } else if (!(env->msr & R_MSR_S_MASK)) {                                  \
29014091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_HDEXCR] & R_HDEXCR_HNU_##dexcr_aspect##_MASK))     \
29024091fabfSNicholas Miehlbradt             return;                                                           \
29034091fabfSNicholas Miehlbradt     }                                                                         \
2904670f1da3SVíctor Colombo                                                                               \
29054091fabfSNicholas Miehlbradt     do_hash(env, ea, ra, rb, key, store);                                     \
2906670f1da3SVíctor Colombo }
29074091fabfSNicholas Miehlbradt #else
29084091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect)                             \
29094091fabfSNicholas Miehlbradt void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
29104091fabfSNicholas Miehlbradt                  target_ulong rb)                                             \
29114091fabfSNicholas Miehlbradt {                                                                             \
29124091fabfSNicholas Miehlbradt     do_hash(env, ea, ra, rb, key, store);                                     \
29134091fabfSNicholas Miehlbradt }
29144091fabfSNicholas Miehlbradt #endif /* TARGET_PPC64 */
2915670f1da3SVíctor Colombo 
29164091fabfSNicholas Miehlbradt HELPER_HASH(HASHST, env->spr[SPR_HASHKEYR], true, NPHIE)
29174091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHK, env->spr[SPR_HASHKEYR], false, NPHIE)
29184091fabfSNicholas Miehlbradt HELPER_HASH(HASHSTP, env->spr[SPR_HASHPKEYR], true, PHIE)
29194091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHKP, env->spr[SPR_HASHPKEYR], false, PHIE)
2920670f1da3SVíctor Colombo 
2921f6c2d68bSBALATON Zoltan #ifndef CONFIG_USER_ONLY
2922ad71ed68SBlue Swirl /* Embedded.Processor Control */
2923ad71ed68SBlue Swirl static int dbell2irq(target_ulong rb)
2924ad71ed68SBlue Swirl {
2925ad71ed68SBlue Swirl     int msg = rb & DBELL_TYPE_MASK;
2926ad71ed68SBlue Swirl     int irq = -1;
2927ad71ed68SBlue Swirl 
2928ad71ed68SBlue Swirl     switch (msg) {
2929ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL:
2930ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_DOORBELL;
2931ad71ed68SBlue Swirl         break;
2932ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL_CRIT:
2933ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_CDOORBELL;
2934ad71ed68SBlue Swirl         break;
2935ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL:
2936ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_CRIT:
2937ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_MC:
2938ad71ed68SBlue Swirl         /* XXX implement */
2939ad71ed68SBlue Swirl     default:
2940ad71ed68SBlue Swirl         break;
2941ad71ed68SBlue Swirl     }
2942ad71ed68SBlue Swirl 
2943ad71ed68SBlue Swirl     return irq;
2944ad71ed68SBlue Swirl }
2945ad71ed68SBlue Swirl 
2946e5f17ac6SBlue Swirl void helper_msgclr(CPUPPCState *env, target_ulong rb)
2947ad71ed68SBlue Swirl {
2948ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2949ad71ed68SBlue Swirl 
2950ad71ed68SBlue Swirl     if (irq < 0) {
2951ad71ed68SBlue Swirl         return;
2952ad71ed68SBlue Swirl     }
2953ad71ed68SBlue Swirl 
29547b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), irq, 0);
2955ad71ed68SBlue Swirl }
2956ad71ed68SBlue Swirl 
2957ad71ed68SBlue Swirl void helper_msgsnd(target_ulong rb)
2958ad71ed68SBlue Swirl {
2959ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2960ad71ed68SBlue Swirl     int pir = rb & DBELL_PIRTAG_MASK;
2961182735efSAndreas Färber     CPUState *cs;
2962ad71ed68SBlue Swirl 
2963ad71ed68SBlue Swirl     if (irq < 0) {
2964ad71ed68SBlue Swirl         return;
2965ad71ed68SBlue Swirl     }
2966ad71ed68SBlue Swirl 
2967195801d7SStefan Hajnoczi     bql_lock();
2968bdc44640SAndreas Färber     CPU_FOREACH(cs) {
2969182735efSAndreas Färber         PowerPCCPU *cpu = POWERPC_CPU(cs);
2970182735efSAndreas Färber         CPUPPCState *cenv = &cpu->env;
2971182735efSAndreas Färber 
2972*0dfe59feSNicholas Piggin         if ((rb & DBELL_BRDCAST_MASK) || (cenv->spr[SPR_BOOKE_PIR] == pir)) {
29737b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
2974ad71ed68SBlue Swirl         }
2975ad71ed68SBlue Swirl     }
2976195801d7SStefan Hajnoczi     bql_unlock();
2977ad71ed68SBlue Swirl }
29787af1e7b0SCédric Le Goater 
29797af1e7b0SCédric Le Goater /* Server Processor Control */
29807af1e7b0SCédric Le Goater 
29815ba7ba1dSCédric Le Goater static bool dbell_type_server(target_ulong rb)
29825ba7ba1dSCédric Le Goater {
298347733729SDavid Gibson     /*
298447733729SDavid Gibson      * A Directed Hypervisor Doorbell message is sent only if the
29857af1e7b0SCédric Le Goater      * message type is 5. All other types are reserved and the
298647733729SDavid Gibson      * instruction is a no-op
298747733729SDavid Gibson      */
29885ba7ba1dSCédric Le Goater     return (rb & DBELL_TYPE_MASK) == DBELL_TYPE_DBELL_SERVER;
29897af1e7b0SCédric Le Goater }
29907af1e7b0SCédric Le Goater 
2991*0dfe59feSNicholas Piggin static inline bool dbell_bcast_core(target_ulong rb)
2992*0dfe59feSNicholas Piggin {
2993*0dfe59feSNicholas Piggin     return (rb & DBELL_BRDCAST_MASK) == DBELL_BRDCAST_CORE;
2994*0dfe59feSNicholas Piggin }
2995*0dfe59feSNicholas Piggin 
2996*0dfe59feSNicholas Piggin static inline bool dbell_bcast_subproc(target_ulong rb)
2997*0dfe59feSNicholas Piggin {
2998*0dfe59feSNicholas Piggin     return (rb & DBELL_BRDCAST_MASK) == DBELL_BRDCAST_SUBPROC;
2999*0dfe59feSNicholas Piggin }
3000*0dfe59feSNicholas Piggin 
30017af1e7b0SCédric Le Goater void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb)
30027af1e7b0SCédric Le Goater {
30035ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
30047af1e7b0SCédric Le Goater         return;
30057af1e7b0SCédric Le Goater     }
30067af1e7b0SCédric Le Goater 
30077b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
30087af1e7b0SCédric Le Goater }
30097af1e7b0SCédric Le Goater 
3010*0dfe59feSNicholas Piggin void helper_book3s_msgsnd(CPUPPCState *env, target_ulong rb)
30115ba7ba1dSCédric Le Goater {
30125ba7ba1dSCédric Le Goater     int pir = rb & DBELL_PROCIDTAG_MASK;
3013*0dfe59feSNicholas Piggin     bool brdcast = false;
3014*0dfe59feSNicholas Piggin     CPUState *cs, *ccs;
3015*0dfe59feSNicholas Piggin     PowerPCCPU *cpu;
30165ba7ba1dSCédric Le Goater 
30175ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
30185ba7ba1dSCédric Le Goater         return;
30195ba7ba1dSCédric Le Goater     }
30205ba7ba1dSCédric Le Goater 
3021*0dfe59feSNicholas Piggin     cpu = ppc_get_vcpu_by_pir(pir);
3022*0dfe59feSNicholas Piggin     if (!cpu) {
3023*0dfe59feSNicholas Piggin         return;
3024*0dfe59feSNicholas Piggin     }
3025*0dfe59feSNicholas Piggin     cs = CPU(cpu);
3026*0dfe59feSNicholas Piggin 
3027*0dfe59feSNicholas Piggin     if (dbell_bcast_core(rb) || (dbell_bcast_subproc(rb) &&
3028*0dfe59feSNicholas Piggin                                  (env->flags & POWERPC_FLAG_SMT_1LPAR))) {
3029*0dfe59feSNicholas Piggin         brdcast = true;
3030*0dfe59feSNicholas Piggin     }
3031*0dfe59feSNicholas Piggin 
3032*0dfe59feSNicholas Piggin     if (cs->nr_threads == 1 || !brdcast) {
3033*0dfe59feSNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_HDOORBELL, 1);
3034*0dfe59feSNicholas Piggin         return;
3035*0dfe59feSNicholas Piggin     }
3036*0dfe59feSNicholas Piggin 
3037*0dfe59feSNicholas Piggin     /*
3038*0dfe59feSNicholas Piggin      * Why is bql needed for walking CPU list? Answer seems to be because ppc
3039*0dfe59feSNicholas Piggin      * irq handling needs it, but ppc_set_irq takes the lock itself if needed,
3040*0dfe59feSNicholas Piggin      * so could this be removed?
3041*0dfe59feSNicholas Piggin      */
3042*0dfe59feSNicholas Piggin     bql_lock();
3043*0dfe59feSNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
3044*0dfe59feSNicholas Piggin         ppc_set_irq(POWERPC_CPU(ccs), PPC_INTERRUPT_HDOORBELL, 1);
3045*0dfe59feSNicholas Piggin     }
3046*0dfe59feSNicholas Piggin     bql_unlock();
30475ba7ba1dSCédric Le Goater }
30485ba7ba1dSCédric Le Goater 
3049f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
30505ba7ba1dSCédric Le Goater void helper_book3s_msgclrp(CPUPPCState *env, target_ulong rb)
30515ba7ba1dSCédric Le Goater {
3052493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgclrp", HFSCR_IC_MSGP);
3053493028d8SCédric Le Goater 
30545ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
30555ba7ba1dSCédric Le Goater         return;
30565ba7ba1dSCédric Le Goater     }
30575ba7ba1dSCédric Le Goater 
30582e985555SNicholas Piggin     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_DOORBELL, 0);
30595ba7ba1dSCédric Le Goater }
30605ba7ba1dSCédric Le Goater 
30615ba7ba1dSCédric Le Goater /*
3062d24e80b2SNicholas Piggin  * sends a message to another thread  on the same
30635ba7ba1dSCédric Le Goater  * multi-threaded processor
30645ba7ba1dSCédric Le Goater  */
30655ba7ba1dSCédric Le Goater void helper_book3s_msgsndp(CPUPPCState *env, target_ulong rb)
30665ba7ba1dSCédric Le Goater {
3067d24e80b2SNicholas Piggin     CPUState *cs = env_cpu(env);
3068b0a13333SPhilippe Mathieu-Daudé     PowerPCCPU *cpu = env_archcpu(env);
3069d24e80b2SNicholas Piggin     CPUState *ccs;
3070d24e80b2SNicholas Piggin     uint32_t nr_threads = cs->nr_threads;
3071d24e80b2SNicholas Piggin     int ttir = rb & PPC_BITMASK(57, 63);
30725ba7ba1dSCédric Le Goater 
3073493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgsndp", HFSCR_IC_MSGP);
3074493028d8SCédric Le Goater 
30753401ea3cSNicholas Piggin     if (!(env->flags & POWERPC_FLAG_SMT_1LPAR)) {
30763401ea3cSNicholas Piggin         nr_threads = 1; /* msgsndp behaves as 1-thread in LPAR-per-thread mode*/
30773401ea3cSNicholas Piggin     }
30783401ea3cSNicholas Piggin 
3079d24e80b2SNicholas Piggin     if (!dbell_type_server(rb) || ttir >= nr_threads) {
30805ba7ba1dSCédric Le Goater         return;
30815ba7ba1dSCédric Le Goater     }
30825ba7ba1dSCédric Le Goater 
3083d24e80b2SNicholas Piggin     if (nr_threads == 1) {
3084d24e80b2SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_DOORBELL, 1);
3085d24e80b2SNicholas Piggin         return;
3086d24e80b2SNicholas Piggin     }
30875ba7ba1dSCédric Le Goater 
3088d24e80b2SNicholas Piggin     /* Does iothread need to be locked for walking CPU list? */
3089195801d7SStefan Hajnoczi     bql_lock();
3090d24e80b2SNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
3091d24e80b2SNicholas Piggin         PowerPCCPU *ccpu = POWERPC_CPU(ccs);
3092d24e80b2SNicholas Piggin         uint32_t thread_id = ppc_cpu_tir(ccpu);
3093d24e80b2SNicholas Piggin 
3094d24e80b2SNicholas Piggin         if (ttir == thread_id) {
3095d24e80b2SNicholas Piggin             ppc_set_irq(ccpu, PPC_INTERRUPT_DOORBELL, 1);
3096195801d7SStefan Hajnoczi             bql_unlock();
3097d24e80b2SNicholas Piggin             return;
3098d24e80b2SNicholas Piggin         }
3099d24e80b2SNicholas Piggin     }
3100d24e80b2SNicholas Piggin 
3101d24e80b2SNicholas Piggin     g_assert_not_reached();
31025ba7ba1dSCédric Le Goater }
3103996473e4SRichard Henderson #endif /* TARGET_PPC64 */
31040f3110faSRichard Henderson 
310514895384SNicholas Piggin /* Single-step tracing */
310614895384SNicholas Piggin void helper_book3s_trace(CPUPPCState *env, target_ulong prev_ip)
310714895384SNicholas Piggin {
310814895384SNicholas Piggin     uint32_t error_code = 0;
310914895384SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
311014895384SNicholas Piggin         /* Load/store reporting, SRR1[35, 36] and SDAR, are not implemented. */
311114895384SNicholas Piggin         env->spr[SPR_POWER_SIAR] = prev_ip;
311214895384SNicholas Piggin         error_code = PPC_BIT(33);
311314895384SNicholas Piggin     }
311414895384SNicholas Piggin     raise_exception_err(env, POWERPC_EXCP_TRACE, error_code);
311514895384SNicholas Piggin }
311614895384SNicholas Piggin 
31170f3110faSRichard Henderson void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
31180f3110faSRichard Henderson                                  MMUAccessType access_type,
31190f3110faSRichard Henderson                                  int mmu_idx, uintptr_t retaddr)
31200f3110faSRichard Henderson {
3121b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
312229c4a336SFabiano Rosas     uint32_t insn;
312329c4a336SFabiano Rosas 
312429c4a336SFabiano Rosas     /* Restore state and reload the insn we executed, for filling in DSISR.  */
31253d419a4dSRichard Henderson     cpu_restore_state(cs, retaddr);
3126888050cfSNicholas Piggin     insn = ppc_ldl_code(env, env->nip);
31270f3110faSRichard Henderson 
3128a7e3af13SRichard Henderson     switch (env->mmu_model) {
3129a7e3af13SRichard Henderson     case POWERPC_MMU_SOFT_4xx:
3130a7e3af13SRichard Henderson         env->spr[SPR_40x_DEAR] = vaddr;
3131a7e3af13SRichard Henderson         break;
3132a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE:
3133a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE206:
3134a7e3af13SRichard Henderson         env->spr[SPR_BOOKE_DEAR] = vaddr;
3135a7e3af13SRichard Henderson         break;
3136a7e3af13SRichard Henderson     default:
3137a7e3af13SRichard Henderson         env->spr[SPR_DAR] = vaddr;
3138a7e3af13SRichard Henderson         break;
3139a7e3af13SRichard Henderson     }
3140a7e3af13SRichard Henderson 
31410f3110faSRichard Henderson     cs->exception_index = POWERPC_EXCP_ALIGN;
314229c4a336SFabiano Rosas     env->error_code = insn & 0x03FF0000;
314329c4a336SFabiano Rosas     cpu_loop_exit(cs);
31440f3110faSRichard Henderson }
314555a7fa34SNicholas Piggin 
314655a7fa34SNicholas Piggin void ppc_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
314755a7fa34SNicholas Piggin                                    vaddr vaddr, unsigned size,
314855a7fa34SNicholas Piggin                                    MMUAccessType access_type,
314955a7fa34SNicholas Piggin                                    int mmu_idx, MemTxAttrs attrs,
315055a7fa34SNicholas Piggin                                    MemTxResult response, uintptr_t retaddr)
315155a7fa34SNicholas Piggin {
3152b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
315355a7fa34SNicholas Piggin 
315455a7fa34SNicholas Piggin     switch (env->excp_model) {
315555a7fa34SNicholas Piggin #if defined(TARGET_PPC64)
3156c8fd9667SNicholas Piggin     case POWERPC_EXCP_POWER8:
315755a7fa34SNicholas Piggin     case POWERPC_EXCP_POWER9:
315855a7fa34SNicholas Piggin     case POWERPC_EXCP_POWER10:
315955a7fa34SNicholas Piggin         /*
316055a7fa34SNicholas Piggin          * Machine check codes can be found in processor User Manual or
316155a7fa34SNicholas Piggin          * Linux or skiboot source.
316255a7fa34SNicholas Piggin          */
316355a7fa34SNicholas Piggin         if (access_type == MMU_DATA_LOAD) {
316455a7fa34SNicholas Piggin             env->spr[SPR_DAR] = vaddr;
316555a7fa34SNicholas Piggin             env->spr[SPR_DSISR] = PPC_BIT(57);
316655a7fa34SNicholas Piggin             env->error_code = PPC_BIT(42);
316755a7fa34SNicholas Piggin 
316855a7fa34SNicholas Piggin         } else if (access_type == MMU_DATA_STORE) {
316955a7fa34SNicholas Piggin             /*
317055a7fa34SNicholas Piggin              * MCE for stores in POWER is asynchronous so hardware does
317155a7fa34SNicholas Piggin              * not set DAR, but QEMU can do better.
317255a7fa34SNicholas Piggin              */
317355a7fa34SNicholas Piggin             env->spr[SPR_DAR] = vaddr;
317455a7fa34SNicholas Piggin             env->error_code = PPC_BIT(36) | PPC_BIT(43) | PPC_BIT(45);
317555a7fa34SNicholas Piggin             env->error_code |= PPC_BIT(42);
317655a7fa34SNicholas Piggin 
317755a7fa34SNicholas Piggin         } else { /* Fetch */
3178c8fd9667SNicholas Piggin             /*
3179c8fd9667SNicholas Piggin              * is_prefix_insn_excp() tests !PPC_BIT(42) to avoid fetching
3180c8fd9667SNicholas Piggin              * the instruction, so that must always be clear for fetches.
3181c8fd9667SNicholas Piggin              */
318255a7fa34SNicholas Piggin             env->error_code = PPC_BIT(36) | PPC_BIT(44) | PPC_BIT(45);
318355a7fa34SNicholas Piggin         }
318455a7fa34SNicholas Piggin         break;
318555a7fa34SNicholas Piggin #endif
318655a7fa34SNicholas Piggin     default:
318755a7fa34SNicholas Piggin         /*
318855a7fa34SNicholas Piggin          * TODO: Check behaviour for other CPUs, for now do nothing.
318955a7fa34SNicholas Piggin          * Could add a basic MCE even if real hardware ignores.
319055a7fa34SNicholas Piggin          */
319155a7fa34SNicholas Piggin         return;
319255a7fa34SNicholas Piggin     }
319355a7fa34SNicholas Piggin 
319455a7fa34SNicholas Piggin     cs->exception_index = POWERPC_EXCP_MCHECK;
319555a7fa34SNicholas Piggin     cpu_loop_exit_restore(cs, retaddr);
319655a7fa34SNicholas Piggin }
319714192307SNicholas Piggin 
319814192307SNicholas Piggin void ppc_cpu_debug_excp_handler(CPUState *cs)
319914192307SNicholas Piggin {
320014192307SNicholas Piggin #if defined(TARGET_PPC64)
3201b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
320214192307SNicholas Piggin 
320314192307SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
3204d5ee641cSNicholas Piggin         if (cs->watchpoint_hit) {
3205d5ee641cSNicholas Piggin             if (cs->watchpoint_hit->flags & BP_CPU) {
3206d5ee641cSNicholas Piggin                 env->spr[SPR_DAR] = cs->watchpoint_hit->hitaddr;
3207d5ee641cSNicholas Piggin                 env->spr[SPR_DSISR] = PPC_BIT(41);
3208d5ee641cSNicholas Piggin                 cs->watchpoint_hit = NULL;
3209d5ee641cSNicholas Piggin                 raise_exception(env, POWERPC_EXCP_DSI);
3210d5ee641cSNicholas Piggin             }
3211d5ee641cSNicholas Piggin             cs->watchpoint_hit = NULL;
3212d5ee641cSNicholas Piggin         } else if (cpu_breakpoint_test(cs, env->nip, BP_CPU)) {
321314192307SNicholas Piggin             raise_exception_err(env, POWERPC_EXCP_TRACE,
321414192307SNicholas Piggin                                 PPC_BIT(33) | PPC_BIT(43));
321514192307SNicholas Piggin         }
321614192307SNicholas Piggin     }
321714192307SNicholas Piggin #endif
321814192307SNicholas Piggin }
321914192307SNicholas Piggin 
322014192307SNicholas Piggin bool ppc_cpu_debug_check_breakpoint(CPUState *cs)
322114192307SNicholas Piggin {
322214192307SNicholas Piggin #if defined(TARGET_PPC64)
3223b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
322414192307SNicholas Piggin 
322514192307SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
322614192307SNicholas Piggin         target_ulong priv;
322714192307SNicholas Piggin 
322814192307SNicholas Piggin         priv = env->spr[SPR_CIABR] & PPC_BITMASK(62, 63);
322914192307SNicholas Piggin         switch (priv) {
323014192307SNicholas Piggin         case 0x1: /* problem */
323114192307SNicholas Piggin             return env->msr & ((target_ulong)1 << MSR_PR);
323214192307SNicholas Piggin         case 0x2: /* supervisor */
323314192307SNicholas Piggin             return (!(env->msr & ((target_ulong)1 << MSR_PR)) &&
323414192307SNicholas Piggin                     !(env->msr & ((target_ulong)1 << MSR_HV)));
323514192307SNicholas Piggin         case 0x3: /* hypervisor */
323614192307SNicholas Piggin             return (!(env->msr & ((target_ulong)1 << MSR_PR)) &&
323714192307SNicholas Piggin                      (env->msr & ((target_ulong)1 << MSR_HV)));
323814192307SNicholas Piggin         default:
323914192307SNicholas Piggin             g_assert_not_reached();
324014192307SNicholas Piggin         }
324114192307SNicholas Piggin     }
324214192307SNicholas Piggin #endif
324314192307SNicholas Piggin 
324414192307SNicholas Piggin     return false;
324514192307SNicholas Piggin }
324614192307SNicholas Piggin 
3247d5ee641cSNicholas Piggin bool ppc_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
3248d5ee641cSNicholas Piggin {
3249d5ee641cSNicholas Piggin #if defined(TARGET_PPC64)
3250b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
3251d5ee641cSNicholas Piggin 
3252d5ee641cSNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
3253d5ee641cSNicholas Piggin         if (wp == env->dawr0_watchpoint) {
3254d5ee641cSNicholas Piggin             uint32_t dawrx = env->spr[SPR_DAWRX0];
3255d5ee641cSNicholas Piggin             bool wt = extract32(dawrx, PPC_BIT_NR(59), 1);
3256d5ee641cSNicholas Piggin             bool wti = extract32(dawrx, PPC_BIT_NR(60), 1);
3257d5ee641cSNicholas Piggin             bool hv = extract32(dawrx, PPC_BIT_NR(61), 1);
3258d5ee641cSNicholas Piggin             bool sv = extract32(dawrx, PPC_BIT_NR(62), 1);
3259d5ee641cSNicholas Piggin             bool pr = extract32(dawrx, PPC_BIT_NR(62), 1);
3260d5ee641cSNicholas Piggin 
3261d5ee641cSNicholas Piggin             if ((env->msr & ((target_ulong)1 << MSR_PR)) && !pr) {
3262d5ee641cSNicholas Piggin                 return false;
3263d5ee641cSNicholas Piggin             } else if ((env->msr & ((target_ulong)1 << MSR_HV)) && !hv) {
3264d5ee641cSNicholas Piggin                 return false;
3265d5ee641cSNicholas Piggin             } else if (!sv) {
3266d5ee641cSNicholas Piggin                 return false;
3267d5ee641cSNicholas Piggin             }
3268d5ee641cSNicholas Piggin 
3269d5ee641cSNicholas Piggin             if (!wti) {
3270d5ee641cSNicholas Piggin                 if (env->msr & ((target_ulong)1 << MSR_DR)) {
3271d5ee641cSNicholas Piggin                     if (!wt) {
3272d5ee641cSNicholas Piggin                         return false;
3273d5ee641cSNicholas Piggin                     }
3274d5ee641cSNicholas Piggin                 } else {
3275d5ee641cSNicholas Piggin                     if (wt) {
3276d5ee641cSNicholas Piggin                         return false;
3277d5ee641cSNicholas Piggin                     }
3278d5ee641cSNicholas Piggin                 }
3279d5ee641cSNicholas Piggin             }
3280d5ee641cSNicholas Piggin 
3281d5ee641cSNicholas Piggin             return true;
3282d5ee641cSNicholas Piggin         }
3283d5ee641cSNicholas Piggin     }
3284d5ee641cSNicholas Piggin #endif
3285d5ee641cSNicholas Piggin 
3286d5ee641cSNicholas Piggin     return false;
3287d5ee641cSNicholas Piggin }
3288d5ee641cSNicholas Piggin 
3289996473e4SRichard Henderson #endif /* !CONFIG_USER_ONLY */
32905ca958cfSBALATON Zoltan #endif /* CONFIG_TCG */
3291