xref: /openbmc/qemu/target/ppc/excp_helper.c (revision 10bfa161fbcf40d74db4103a0f2402779e98d0f7)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  PowerPC exception emulation helpers for QEMU.
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2003-2007 Jocelyn Mayer
5fcf5ef2aSThomas Huth  *
6fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
7fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
8fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
96bd039cdSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
10fcf5ef2aSThomas Huth  *
11fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
12fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
15fcf5ef2aSThomas Huth  *
16fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
17fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18fcf5ef2aSThomas Huth  */
19fcf5ef2aSThomas Huth #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"
24fcf5ef2aSThomas Huth #include "cpu.h"
25fcf5ef2aSThomas Huth #include "exec/exec-all.h"
260f3110faSRichard Henderson #include "internal.h"
27fcf5ef2aSThomas Huth #include "helper_regs.h"
287b694df6SMatheus Ferst #include "hw/ppc/ppc.h"
29fcf5ef2aSThomas Huth 
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) 
38fcf5ef2aSThomas Huth /*****************************************************************************/
39fcf5ef2aSThomas Huth /* Exception processing */
40f6c2d68bSBALATON Zoltan #ifndef CONFIG_USER_ONLY
41fcf5ef2aSThomas Huth 
powerpc_excp_name(int excp)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 
dump_syscall(CPUPPCState * env)11262e79ef9SCédric Le Goater static void dump_syscall(CPUPPCState *env)
113fcf5ef2aSThomas Huth {
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
117fcf5ef2aSThomas Huth                   " nip=" TARGET_FMT_lx "\n",
118fcf5ef2aSThomas Huth                   ppc_dump_gpr(env, 0), ppc_dump_gpr(env, 3),
119fcf5ef2aSThomas Huth                   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 
dump_hcall(CPUPPCState * env)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);
137fcf5ef2aSThomas Huth }
138fcf5ef2aSThomas Huth 
139888050cfSNicholas Piggin #ifdef CONFIG_TCG
140888050cfSNicholas Piggin /* Return true iff byteswap is needed to load instruction */
insn_need_byteswap(CPUArchState * env)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 
ppc_ldl_code(CPUArchState * env,target_ulong addr)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 
ppc_excp_debug_sw_tlb(CPUPPCState * env,int excp)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
powerpc_reset_wakeup(CPUPPCState * env,int excp,target_ulong * msr)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  */
ppc_excp_apply_ail(PowerPCCPU * cpu,int excp,target_ulong msr,target_ulong * new_msr,target_ulong * vector)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;
3278bbf0cffSHarsh Prateek Bora         if (ail == 0 || ail == 1) {
3288b7e6b07SNicholas Piggin             /* AIL=1 is reserved, treat it like AIL=0 */
3298b7e6b07SNicholas Piggin             return;
3308b7e6b07SNicholas Piggin         }
331526cdce7SNicholas Piggin 
33210895ab6SFabiano Rosas     /* P10 and up */
33310895ab6SFabiano Rosas     } else {
334526cdce7SNicholas Piggin         if (!mmu_all_on && !hv_escalation) {
335526cdce7SNicholas Piggin             /*
336526cdce7SNicholas Piggin              * AIL works for HV interrupts even with guest MSR[IR/DR] disabled.
337526cdce7SNicholas Piggin              * Guest->guest and HV->HV interrupts do require MMU on.
338526cdce7SNicholas Piggin              */
339526cdce7SNicholas Piggin             return;
340526cdce7SNicholas Piggin         }
341526cdce7SNicholas Piggin 
342526cdce7SNicholas Piggin         if (*new_msr & MSR_HVB) {
343526cdce7SNicholas Piggin             if (!(env->spr[SPR_LPCR] & LPCR_HAIL)) {
344526cdce7SNicholas Piggin                 /* HV interrupts depend on LPCR[HAIL] */
345526cdce7SNicholas Piggin                 return;
346526cdce7SNicholas Piggin             }
347526cdce7SNicholas Piggin             ail = 3; /* HAIL=1 gives AIL=3 behaviour for HV interrupts */
348526cdce7SNicholas Piggin         } else {
349526cdce7SNicholas Piggin             ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT;
350526cdce7SNicholas Piggin         }
3518bbf0cffSHarsh Prateek Bora         if (ail == 0 || ail == 1 || ail == 2) {
352526cdce7SNicholas Piggin             /* AIL=1 and AIL=2 are reserved, treat them like AIL=0 */
353526cdce7SNicholas Piggin             return;
354526cdce7SNicholas Piggin         }
3558b7e6b07SNicholas Piggin     }
3568b7e6b07SNicholas Piggin 
3578b7e6b07SNicholas Piggin     /*
3588b7e6b07SNicholas Piggin      * AIL applies, so the new MSR gets IR and DR set, and an offset applied
3598b7e6b07SNicholas Piggin      * to the new IP.
3608b7e6b07SNicholas Piggin      */
3618b7e6b07SNicholas Piggin     *new_msr |= (1 << MSR_IR) | (1 << MSR_DR);
3628b7e6b07SNicholas Piggin 
3638b7e6b07SNicholas Piggin     if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
3648b7e6b07SNicholas Piggin         if (ail == 2) {
3658b7e6b07SNicholas Piggin             *vector |= 0x0000000000018000ull;
3668b7e6b07SNicholas Piggin         } else if (ail == 3) {
3678b7e6b07SNicholas Piggin             *vector |= 0xc000000000004000ull;
3688b7e6b07SNicholas Piggin         }
3698b7e6b07SNicholas Piggin     } else {
3708b7e6b07SNicholas Piggin         /*
3718b7e6b07SNicholas Piggin          * scv AIL is a little different. AIL=2 does not change the address,
3728b7e6b07SNicholas Piggin          * only the MSR. AIL=3 replaces the 0x17000 base with 0xc...3000.
3738b7e6b07SNicholas Piggin          */
3748b7e6b07SNicholas Piggin         if (ail == 3) {
3758b7e6b07SNicholas Piggin             *vector &= ~0x0000000000017000ull; /* Un-apply the base offset */
3768b7e6b07SNicholas Piggin             *vector |= 0xc000000000003000ull; /* Apply scv's AIL=3 offset */
3778b7e6b07SNicholas Piggin         }
3788b7e6b07SNicholas Piggin     }
3792586a4d7SFabiano Rosas }
380f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
381dead760bSBenjamin Herrenschmidt 
powerpc_reset_excp_state(PowerPCCPU * cpu)3823680e994SNicholas Piggin static void powerpc_reset_excp_state(PowerPCCPU *cpu)
383ad77c6caSNicholas Piggin {
384ad77c6caSNicholas Piggin     CPUState *cs = CPU(cpu);
385ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
386ad77c6caSNicholas Piggin 
3873680e994SNicholas Piggin     /* Reset exception state */
3883680e994SNicholas Piggin     cs->exception_index = POWERPC_EXCP_NONE;
3893680e994SNicholas Piggin     env->error_code = 0;
3903680e994SNicholas Piggin }
3913680e994SNicholas Piggin 
powerpc_set_excp_state(PowerPCCPU * cpu,target_ulong vector,target_ulong msr)3923680e994SNicholas Piggin static void powerpc_set_excp_state(PowerPCCPU *cpu, target_ulong vector,
3933680e994SNicholas Piggin                                    target_ulong msr)
3943680e994SNicholas Piggin {
3953680e994SNicholas Piggin     CPUPPCState *env = &cpu->env;
3963680e994SNicholas Piggin 
397fce9fbafSFabiano Rosas     assert((msr & env->msr_mask) == msr);
398fce9fbafSFabiano Rosas 
399ad77c6caSNicholas Piggin     /*
400ad77c6caSNicholas Piggin      * We don't use hreg_store_msr here as already have treated any
401ad77c6caSNicholas Piggin      * special case that could occur. Just store MSR and update hflags
402ad77c6caSNicholas Piggin      *
403ab452503SBALATON Zoltan      * Note: We *MUST* not use hreg_store_msr() as-is anyway because it will
404ab452503SBALATON Zoltan      * prevent setting of the HV bit which some exceptions might need to do.
405ad77c6caSNicholas Piggin      */
4063680e994SNicholas Piggin     env->nip = vector;
407fce9fbafSFabiano Rosas     env->msr = msr;
408ad77c6caSNicholas Piggin     hreg_compute_hflags(env);
4092fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
410ad77c6caSNicholas Piggin 
4113680e994SNicholas Piggin     powerpc_reset_excp_state(cpu);
412ad77c6caSNicholas Piggin 
413ad77c6caSNicholas Piggin     /*
414ad77c6caSNicholas Piggin      * Any interrupt is context synchronizing, check if TCG TLB needs
415ad77c6caSNicholas Piggin      * a delayed flush on ppc64
416ad77c6caSNicholas Piggin      */
417ad77c6caSNicholas Piggin     check_tlb_flush(env, false);
4183680e994SNicholas Piggin 
4193680e994SNicholas Piggin     /* Reset the reservation */
4203680e994SNicholas Piggin     env->reserve_addr = -1;
421ad77c6caSNicholas Piggin }
422ad77c6caSNicholas Piggin 
423cce7aee8SNicholas Piggin #ifdef CONFIG_TCG
42445693f94SNicholas Piggin /*
42545693f94SNicholas Piggin  * This stops the machine and logs CPU state without killing QEMU (like
42645693f94SNicholas Piggin  * cpu_abort()) because it is often a guest error as opposed to a QEMU error,
42745693f94SNicholas Piggin  * so the machine can still be debugged.
42845693f94SNicholas Piggin  */
powerpc_checkstop(CPUPPCState * env,const char * reason)42945693f94SNicholas Piggin static G_NORETURN void powerpc_checkstop(CPUPPCState *env, const char *reason)
43045693f94SNicholas Piggin {
431819b31b0SBALATON Zoltan     CPUState *cs = env_cpu(env);
4329728fb5cSNicholas Piggin     FILE *f;
433819b31b0SBALATON Zoltan 
4349728fb5cSNicholas Piggin     f = qemu_log_trylock();
4359728fb5cSNicholas Piggin     if (f) {
43645693f94SNicholas Piggin         fprintf(f, "Entering checkstop state: %s\n", reason);
4379728fb5cSNicholas Piggin         cpu_dump_state(cs, f, CPU_DUMP_FPU | CPU_DUMP_CCOP);
4389728fb5cSNicholas Piggin         qemu_log_unlock(f);
4399728fb5cSNicholas Piggin     }
440cce7aee8SNicholas Piggin 
441cce7aee8SNicholas Piggin     /*
442cce7aee8SNicholas Piggin      * This stops the machine and logs CPU state without killing QEMU
443cce7aee8SNicholas Piggin      * (like cpu_abort()) so the machine can still be debugged (because
444cce7aee8SNicholas Piggin      * it is often a guest error).
445cce7aee8SNicholas Piggin      */
446cce7aee8SNicholas Piggin     qemu_system_guest_panicked(NULL);
447cce7aee8SNicholas Piggin     cpu_loop_exit_noexc(cs);
44845693f94SNicholas Piggin }
44945693f94SNicholas Piggin 
45045693f94SNicholas Piggin #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
helper_attn(CPUPPCState * env)45145693f94SNicholas Piggin void helper_attn(CPUPPCState *env)
45245693f94SNicholas Piggin {
45345693f94SNicholas Piggin     /* POWER attn is unprivileged when enabled by HID, otherwise illegal */
45445693f94SNicholas Piggin     if ((*env->check_attn)(env)) {
45545693f94SNicholas Piggin         powerpc_checkstop(env, "host executed attn");
45645693f94SNicholas Piggin     } else {
45745693f94SNicholas Piggin         raise_exception_err(env, POWERPC_EXCP_HV_EMU,
45845693f94SNicholas Piggin                             POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
45945693f94SNicholas Piggin     }
46045693f94SNicholas Piggin }
46145693f94SNicholas Piggin #endif
46245693f94SNicholas Piggin #endif /* CONFIG_TCG */
46345693f94SNicholas Piggin 
powerpc_mcheck_checkstop(CPUPPCState * env)46445693f94SNicholas Piggin static void powerpc_mcheck_checkstop(CPUPPCState *env)
46545693f94SNicholas Piggin {
46645693f94SNicholas Piggin     /* KVM guests always have MSR[ME] enabled */
46745693f94SNicholas Piggin #ifdef CONFIG_TCG
46845693f94SNicholas Piggin     if (FIELD_EX64(env->msr, MSR, ME)) {
46945693f94SNicholas Piggin         return;
47045693f94SNicholas Piggin     }
47145693f94SNicholas Piggin 
47245693f94SNicholas Piggin     powerpc_checkstop(env, "machine check with MSR[ME]=0");
473cce7aee8SNicholas Piggin #endif
474819b31b0SBALATON Zoltan }
475819b31b0SBALATON Zoltan 
powerpc_excp_40x(PowerPCCPU * cpu,int excp)476e808c2edSFabiano Rosas static void powerpc_excp_40x(PowerPCCPU *cpu, int excp)
477e808c2edSFabiano Rosas {
478e808c2edSFabiano Rosas     CPUPPCState *env = &cpu->env;
479e808c2edSFabiano Rosas     target_ulong msr, new_msr, vector;
480ab452503SBALATON Zoltan     int srr0 = SPR_SRR0, srr1 = SPR_SRR1;
481e808c2edSFabiano Rosas 
482e808c2edSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
483e808c2edSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
484e808c2edSFabiano Rosas 
485ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
486495fc7ffSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME));
487e808c2edSFabiano Rosas 
488ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
489495fc7ffSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
490e808c2edSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
491e808c2edSFabiano Rosas     }
492e808c2edSFabiano Rosas 
493e808c2edSFabiano Rosas     vector = env->excp_vectors[excp];
494e808c2edSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
495bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
496bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
497e808c2edSFabiano Rosas     }
498e808c2edSFabiano Rosas     vector |= env->excp_prefix;
499e808c2edSFabiano Rosas 
500e808c2edSFabiano Rosas     switch (excp) {
501e808c2edSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
502e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
503e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
504e808c2edSFabiano Rosas         break;
505e808c2edSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
506819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
507e808c2edSFabiano Rosas         /* machine check exceptions don't have ME set */
508e808c2edSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
509e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
510e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
511e808c2edSFabiano Rosas         break;
512e808c2edSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
513f9911e1eSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_40x_ESR], env->spr[SPR_40x_DEAR]);
514e808c2edSFabiano Rosas         break;
515e808c2edSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
516e808c2edSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
517e808c2edSFabiano Rosas         break;
518e808c2edSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
519e808c2edSFabiano Rosas         break;
520e808c2edSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
521e808c2edSFabiano Rosas         break;
522e808c2edSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
523e808c2edSFabiano Rosas         switch (env->error_code & ~0xF) {
524e808c2edSFabiano Rosas         case POWERPC_EXCP_FP:
525da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
526e808c2edSFabiano Rosas                 trace_ppc_excp_fp_ignore();
5273680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
528e808c2edSFabiano Rosas                 return;
529e808c2edSFabiano Rosas             }
53064e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_FP;
531e808c2edSFabiano Rosas             break;
532e808c2edSFabiano Rosas         case POWERPC_EXCP_INVAL:
533e808c2edSFabiano Rosas             trace_ppc_excp_inval(env->nip);
53464e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PIL;
535e808c2edSFabiano Rosas             break;
536e808c2edSFabiano Rosas         case POWERPC_EXCP_PRIV:
53764e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PPR;
538e808c2edSFabiano Rosas             break;
539e808c2edSFabiano Rosas         case POWERPC_EXCP_TRAP:
54064e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PTR;
541e808c2edSFabiano Rosas             break;
542e808c2edSFabiano Rosas         default:
543bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
544e808c2edSFabiano Rosas                       env->error_code);
545e808c2edSFabiano Rosas             break;
546e808c2edSFabiano Rosas         }
547e808c2edSFabiano Rosas         break;
548e808c2edSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
549e808c2edSFabiano Rosas         dump_syscall(env);
550e808c2edSFabiano Rosas 
551e808c2edSFabiano Rosas         /*
552e808c2edSFabiano Rosas          * We need to correct the NIP which in this case is supposed
553e808c2edSFabiano Rosas          * to point to the next instruction
554e808c2edSFabiano Rosas          */
555e808c2edSFabiano Rosas         env->nip += 4;
556e808c2edSFabiano Rosas         break;
557e808c2edSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
558e808c2edSFabiano Rosas         trace_ppc_excp_print("FIT");
559e808c2edSFabiano Rosas         break;
560e808c2edSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
561e808c2edSFabiano Rosas         trace_ppc_excp_print("WDT");
562e808c2edSFabiano Rosas         break;
563e808c2edSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
564e808c2edSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
565e808c2edSFabiano Rosas         break;
566e808c2edSFabiano Rosas     case POWERPC_EXCP_PIT:       /* Programmable interval timer interrupt    */
567e808c2edSFabiano Rosas         trace_ppc_excp_print("PIT");
568e808c2edSFabiano Rosas         break;
5694d8ac1d1SFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
570bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
5714d8ac1d1SFabiano Rosas                   powerpc_excp_name(excp));
5724d8ac1d1SFabiano Rosas         break;
573e808c2edSFabiano Rosas     default:
574bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
575bc30c1c6SBALATON Zoltan                   excp);
576e808c2edSFabiano Rosas         break;
577e808c2edSFabiano Rosas     }
578e808c2edSFabiano Rosas 
579e808c2edSFabiano Rosas     env->spr[srr0] = env->nip;
580e808c2edSFabiano Rosas     env->spr[srr1] = msr;
581e808c2edSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
582e808c2edSFabiano Rosas }
583e808c2edSFabiano Rosas 
powerpc_excp_6xx(PowerPCCPU * cpu,int excp)58458d178fbSFabiano Rosas static void powerpc_excp_6xx(PowerPCCPU *cpu, int excp)
58558d178fbSFabiano Rosas {
58658d178fbSFabiano Rosas     CPUPPCState *env = &cpu->env;
58758d178fbSFabiano Rosas     target_ulong msr, new_msr, vector;
58858d178fbSFabiano Rosas 
58958d178fbSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
59058d178fbSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
59158d178fbSFabiano Rosas 
592ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
593082d783bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
59458d178fbSFabiano Rosas 
595ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
596082d783bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
59758d178fbSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
59858d178fbSFabiano Rosas     }
59958d178fbSFabiano Rosas 
60058d178fbSFabiano Rosas     vector = env->excp_vectors[excp];
60158d178fbSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
602bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
603bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
60458d178fbSFabiano Rosas     }
60558d178fbSFabiano Rosas     vector |= env->excp_prefix;
60658d178fbSFabiano Rosas 
60758d178fbSFabiano Rosas     switch (excp) {
60858d178fbSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
60958d178fbSFabiano Rosas         break;
61058d178fbSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
611819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
61258d178fbSFabiano Rosas         /* machine check exceptions don't have ME set */
61358d178fbSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
61458d178fbSFabiano Rosas         break;
61558d178fbSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
61658d178fbSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
61758d178fbSFabiano Rosas         break;
61858d178fbSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
61958d178fbSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
62058d178fbSFabiano Rosas         msr |= env->error_code;
62158d178fbSFabiano Rosas         break;
62258d178fbSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
62358d178fbSFabiano Rosas         break;
62458d178fbSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
62558d178fbSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
62658d178fbSFabiano Rosas         /*
62758d178fbSFabiano Rosas          * Note: the opcode fields will not be set properly for a
62858d178fbSFabiano Rosas          * direct store load/store, but nobody cares as nobody
62958d178fbSFabiano Rosas          * actually uses direct store segments.
63058d178fbSFabiano Rosas          */
63158d178fbSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
63258d178fbSFabiano Rosas         break;
63358d178fbSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
63458d178fbSFabiano Rosas         switch (env->error_code & ~0xF) {
63558d178fbSFabiano Rosas         case POWERPC_EXCP_FP:
636da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
63758d178fbSFabiano Rosas                 trace_ppc_excp_fp_ignore();
6383680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
63958d178fbSFabiano Rosas                 return;
64058d178fbSFabiano Rosas             }
64158d178fbSFabiano Rosas             /*
642ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
643ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
64458d178fbSFabiano Rosas              */
64558d178fbSFabiano Rosas             msr |= 0x00100000;
64658d178fbSFabiano Rosas             break;
64758d178fbSFabiano Rosas         case POWERPC_EXCP_INVAL:
64858d178fbSFabiano Rosas             trace_ppc_excp_inval(env->nip);
64958d178fbSFabiano Rosas             msr |= 0x00080000;
65058d178fbSFabiano Rosas             break;
65158d178fbSFabiano Rosas         case POWERPC_EXCP_PRIV:
65258d178fbSFabiano Rosas             msr |= 0x00040000;
65358d178fbSFabiano Rosas             break;
65458d178fbSFabiano Rosas         case POWERPC_EXCP_TRAP:
65558d178fbSFabiano Rosas             msr |= 0x00020000;
65658d178fbSFabiano Rosas             break;
65758d178fbSFabiano Rosas         default:
65858d178fbSFabiano Rosas             /* Should never occur */
659bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
66058d178fbSFabiano Rosas                       env->error_code);
66158d178fbSFabiano Rosas             break;
66258d178fbSFabiano Rosas         }
66358d178fbSFabiano Rosas         break;
66458d178fbSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
66558d178fbSFabiano Rosas         dump_syscall(env);
66658d178fbSFabiano Rosas 
66758d178fbSFabiano Rosas         /*
66858d178fbSFabiano Rosas          * We need to correct the NIP which in this case is supposed
66958d178fbSFabiano Rosas          * to point to the next instruction
67058d178fbSFabiano Rosas          */
67158d178fbSFabiano Rosas         env->nip += 4;
67258d178fbSFabiano Rosas         break;
67358d178fbSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
67458d178fbSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
67558d178fbSFabiano Rosas         break;
67658d178fbSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
67758d178fbSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
67858d178fbSFabiano Rosas         break;
67958d178fbSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
6808e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
681bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
682bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset exception "
683bc30c1c6SBALATON Zoltan                       "%d with no HV support\n", excp);
68458d178fbSFabiano Rosas         }
68558d178fbSFabiano Rosas         break;
68658d178fbSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
68758d178fbSFabiano Rosas         break;
68858d178fbSFabiano Rosas     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
68958d178fbSFabiano Rosas     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
69058d178fbSFabiano Rosas     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
69158d178fbSFabiano Rosas         /* Swap temporary saved registers with GPRs */
69258d178fbSFabiano Rosas         if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
69358d178fbSFabiano Rosas             new_msr |= (target_ulong)1 << MSR_TGPR;
69458d178fbSFabiano Rosas             hreg_swap_gpr_tgpr(env);
69558d178fbSFabiano Rosas         }
6968f8c7932SFabiano Rosas 
69758d178fbSFabiano Rosas         ppc_excp_debug_sw_tlb(env, excp);
69858d178fbSFabiano Rosas 
69958d178fbSFabiano Rosas         msr |= env->crf[0] << 28;
70058d178fbSFabiano Rosas         msr |= env->error_code; /* key, D/I, S/L bits */
70158d178fbSFabiano Rosas         /* Set way using a LRU mechanism */
70258d178fbSFabiano Rosas         msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
70358d178fbSFabiano Rosas         break;
70458d178fbSFabiano Rosas     case POWERPC_EXCP_FPA:       /* Floating-point assist exception          */
70558d178fbSFabiano Rosas     case POWERPC_EXCP_DABR:      /* Data address breakpoint                  */
70658d178fbSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
70758d178fbSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
70858d178fbSFabiano Rosas     case POWERPC_EXCP_MEXTBR:    /* Maskable external breakpoint             */
70958d178fbSFabiano Rosas     case POWERPC_EXCP_NMEXTBR:   /* Non maskable external breakpoint         */
710bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
71158d178fbSFabiano Rosas                   powerpc_excp_name(excp));
71258d178fbSFabiano Rosas         break;
71358d178fbSFabiano Rosas     default:
714bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
715bc30c1c6SBALATON Zoltan                   excp);
71658d178fbSFabiano Rosas         break;
71758d178fbSFabiano Rosas     }
71858d178fbSFabiano Rosas 
71958d178fbSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
72058d178fbSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
72158d178fbSFabiano Rosas     }
722c50eaed1SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
723c50eaed1SFabiano Rosas     env->spr[SPR_SRR1] = msr;
72458d178fbSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
72558d178fbSFabiano Rosas }
72658d178fbSFabiano Rosas 
powerpc_excp_7xx(PowerPCCPU * cpu,int excp)727ccfca2fcSFabiano Rosas static void powerpc_excp_7xx(PowerPCCPU *cpu, int excp)
728ccfca2fcSFabiano Rosas {
729ccfca2fcSFabiano Rosas     CPUPPCState *env = &cpu->env;
730ccfca2fcSFabiano Rosas     target_ulong msr, new_msr, vector;
731ccfca2fcSFabiano Rosas 
732ccfca2fcSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
733ccfca2fcSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
734ccfca2fcSFabiano Rosas 
735ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
73693848d6aSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
737ccfca2fcSFabiano Rosas 
738ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
73993848d6aSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
740ccfca2fcSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
741ccfca2fcSFabiano Rosas     }
742ccfca2fcSFabiano Rosas 
743ccfca2fcSFabiano Rosas     vector = env->excp_vectors[excp];
744ccfca2fcSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
745bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
746bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
747ccfca2fcSFabiano Rosas     }
748ccfca2fcSFabiano Rosas     vector |= env->excp_prefix;
749ccfca2fcSFabiano Rosas 
750ccfca2fcSFabiano Rosas     switch (excp) {
751ccfca2fcSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
752819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
753ccfca2fcSFabiano Rosas         /* machine check exceptions don't have ME set */
754ccfca2fcSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
755ccfca2fcSFabiano Rosas         break;
756ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
757ccfca2fcSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
758ccfca2fcSFabiano Rosas         break;
759ccfca2fcSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
760ccfca2fcSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
761ccfca2fcSFabiano Rosas         msr |= env->error_code;
762ccfca2fcSFabiano Rosas         break;
763ccfca2fcSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
764ccfca2fcSFabiano Rosas         break;
765ccfca2fcSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
766ccfca2fcSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
767ccfca2fcSFabiano Rosas         /*
768ccfca2fcSFabiano Rosas          * Note: the opcode fields will not be set properly for a
769ccfca2fcSFabiano Rosas          * direct store load/store, but nobody cares as nobody
770ccfca2fcSFabiano Rosas          * actually uses direct store segments.
771ccfca2fcSFabiano Rosas          */
772ccfca2fcSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
773ccfca2fcSFabiano Rosas         break;
774ccfca2fcSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
775ccfca2fcSFabiano Rosas         switch (env->error_code & ~0xF) {
776ccfca2fcSFabiano Rosas         case POWERPC_EXCP_FP:
777da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
778ccfca2fcSFabiano Rosas                 trace_ppc_excp_fp_ignore();
7793680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
780ccfca2fcSFabiano Rosas                 return;
781ccfca2fcSFabiano Rosas             }
782ccfca2fcSFabiano Rosas             /*
783ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
784ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
785ccfca2fcSFabiano Rosas              */
786ccfca2fcSFabiano Rosas             msr |= 0x00100000;
787ccfca2fcSFabiano Rosas             break;
788ccfca2fcSFabiano Rosas         case POWERPC_EXCP_INVAL:
789ccfca2fcSFabiano Rosas             trace_ppc_excp_inval(env->nip);
790ccfca2fcSFabiano Rosas             msr |= 0x00080000;
791ccfca2fcSFabiano Rosas             break;
792ccfca2fcSFabiano Rosas         case POWERPC_EXCP_PRIV:
793ccfca2fcSFabiano Rosas             msr |= 0x00040000;
794ccfca2fcSFabiano Rosas             break;
795ccfca2fcSFabiano Rosas         case POWERPC_EXCP_TRAP:
796ccfca2fcSFabiano Rosas             msr |= 0x00020000;
797ccfca2fcSFabiano Rosas             break;
798ccfca2fcSFabiano Rosas         default:
799ccfca2fcSFabiano Rosas             /* Should never occur */
800bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
801ccfca2fcSFabiano Rosas                       env->error_code);
802ccfca2fcSFabiano Rosas             break;
803ccfca2fcSFabiano Rosas         }
804ccfca2fcSFabiano Rosas         break;
805ccfca2fcSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
8063b578635SFabiano Rosas     {
8073b578635SFabiano Rosas         int lev = env->error_code;
808ccfca2fcSFabiano Rosas 
8093b578635SFabiano Rosas         if (lev == 1 && cpu->vhyp) {
810ccfca2fcSFabiano Rosas             dump_hcall(env);
811ccfca2fcSFabiano Rosas         } else {
812ccfca2fcSFabiano Rosas             dump_syscall(env);
813ccfca2fcSFabiano Rosas         }
814ccfca2fcSFabiano Rosas 
815ccfca2fcSFabiano Rosas         /*
816ccfca2fcSFabiano Rosas          * We need to correct the NIP which in this case is supposed
817ccfca2fcSFabiano Rosas          * to point to the next instruction
818ccfca2fcSFabiano Rosas          */
819ccfca2fcSFabiano Rosas         env->nip += 4;
820ccfca2fcSFabiano Rosas 
8213b578635SFabiano Rosas         /*
8223b578635SFabiano Rosas          * The Virtual Open Firmware (VOF) relies on the 'sc 1'
8233b578635SFabiano Rosas          * instruction to communicate with QEMU. The pegasos2 machine
8243b578635SFabiano Rosas          * uses VOF and the 7xx CPUs, so although the 7xx don't have
8253b578635SFabiano Rosas          * HV mode, we need to keep hypercall support.
8263b578635SFabiano Rosas          */
8273b578635SFabiano Rosas         if (lev == 1 && cpu->vhyp) {
828c700b5e1SNicholas Piggin             cpu->vhyp_class->hypercall(cpu->vhyp, cpu);
8292c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
830ccfca2fcSFabiano Rosas             return;
831ccfca2fcSFabiano Rosas         }
8323b578635SFabiano Rosas 
833ccfca2fcSFabiano Rosas         break;
8343b578635SFabiano Rosas     }
835ccfca2fcSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
836ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
837ccfca2fcSFabiano Rosas         break;
838ccfca2fcSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
8398e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
840bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
841bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset exception "
842bc30c1c6SBALATON Zoltan                       "%d with no HV support\n", excp);
843ccfca2fcSFabiano Rosas         }
844ccfca2fcSFabiano Rosas         break;
845ccfca2fcSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
846ccfca2fcSFabiano Rosas         break;
847ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
848ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
849ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
850ccfca2fcSFabiano Rosas         ppc_excp_debug_sw_tlb(env, excp);
851ccfca2fcSFabiano Rosas         msr |= env->crf[0] << 28;
852ccfca2fcSFabiano Rosas         msr |= env->error_code; /* key, D/I, S/L bits */
853ccfca2fcSFabiano Rosas         /* Set way using a LRU mechanism */
854ccfca2fcSFabiano Rosas         msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
855ccfca2fcSFabiano Rosas         break;
856ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
857ccfca2fcSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
858ccfca2fcSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
859ccfca2fcSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
860bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
861ccfca2fcSFabiano Rosas                   powerpc_excp_name(excp));
862ccfca2fcSFabiano Rosas         break;
863ccfca2fcSFabiano Rosas     default:
864bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
865bc30c1c6SBALATON Zoltan                   excp);
866ccfca2fcSFabiano Rosas         break;
867ccfca2fcSFabiano Rosas     }
868ccfca2fcSFabiano Rosas 
869ccfca2fcSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
870ccfca2fcSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
871ccfca2fcSFabiano Rosas     }
872fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
873fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR1] = msr;
874ccfca2fcSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
875ccfca2fcSFabiano Rosas }
876ccfca2fcSFabiano Rosas 
powerpc_excp_74xx(PowerPCCPU * cpu,int excp)87752926b0dSFabiano Rosas static void powerpc_excp_74xx(PowerPCCPU *cpu, int excp)
87852926b0dSFabiano Rosas {
87952926b0dSFabiano Rosas     CPUPPCState *env = &cpu->env;
88052926b0dSFabiano Rosas     target_ulong msr, new_msr, vector;
88152926b0dSFabiano Rosas 
88252926b0dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
88352926b0dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
88452926b0dSFabiano Rosas 
885ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
8861f6faf8bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
88752926b0dSFabiano Rosas 
888ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
8891f6faf8bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
89052926b0dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
89152926b0dSFabiano Rosas     }
89252926b0dSFabiano Rosas 
89352926b0dSFabiano Rosas     vector = env->excp_vectors[excp];
89452926b0dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
895bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
896bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
89752926b0dSFabiano Rosas     }
89852926b0dSFabiano Rosas     vector |= env->excp_prefix;
89952926b0dSFabiano Rosas 
90052926b0dSFabiano Rosas     switch (excp) {
90152926b0dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
902819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
90352926b0dSFabiano Rosas         /* machine check exceptions don't have ME set */
90452926b0dSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
90552926b0dSFabiano Rosas         break;
90652926b0dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
90752926b0dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
90852926b0dSFabiano Rosas         break;
90952926b0dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
91052926b0dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
91152926b0dSFabiano Rosas         msr |= env->error_code;
91252926b0dSFabiano Rosas         break;
91352926b0dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
91452926b0dSFabiano Rosas         break;
91552926b0dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
91652926b0dSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
91752926b0dSFabiano Rosas         /*
91852926b0dSFabiano Rosas          * Note: the opcode fields will not be set properly for a
91952926b0dSFabiano Rosas          * direct store load/store, but nobody cares as nobody
92052926b0dSFabiano Rosas          * actually uses direct store segments.
92152926b0dSFabiano Rosas          */
92252926b0dSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
92352926b0dSFabiano Rosas         break;
92452926b0dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
92552926b0dSFabiano Rosas         switch (env->error_code & ~0xF) {
92652926b0dSFabiano Rosas         case POWERPC_EXCP_FP:
927da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
92852926b0dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
9293680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
93052926b0dSFabiano Rosas                 return;
93152926b0dSFabiano Rosas             }
93252926b0dSFabiano Rosas             /*
933ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
934ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
93552926b0dSFabiano Rosas              */
93652926b0dSFabiano Rosas             msr |= 0x00100000;
93752926b0dSFabiano Rosas             break;
93852926b0dSFabiano Rosas         case POWERPC_EXCP_INVAL:
93952926b0dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
94052926b0dSFabiano Rosas             msr |= 0x00080000;
94152926b0dSFabiano Rosas             break;
94252926b0dSFabiano Rosas         case POWERPC_EXCP_PRIV:
94352926b0dSFabiano Rosas             msr |= 0x00040000;
94452926b0dSFabiano Rosas             break;
94552926b0dSFabiano Rosas         case POWERPC_EXCP_TRAP:
94652926b0dSFabiano Rosas             msr |= 0x00020000;
94752926b0dSFabiano Rosas             break;
94852926b0dSFabiano Rosas         default:
94952926b0dSFabiano Rosas             /* Should never occur */
950bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
95152926b0dSFabiano Rosas                       env->error_code);
95252926b0dSFabiano Rosas             break;
95352926b0dSFabiano Rosas         }
95452926b0dSFabiano Rosas         break;
95552926b0dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
956bca2c6d9SFabiano Rosas     {
957bca2c6d9SFabiano Rosas         int lev = env->error_code;
95852926b0dSFabiano Rosas 
9592306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
96052926b0dSFabiano Rosas             dump_hcall(env);
96152926b0dSFabiano Rosas         } else {
96252926b0dSFabiano Rosas             dump_syscall(env);
96352926b0dSFabiano Rosas         }
96452926b0dSFabiano Rosas 
96552926b0dSFabiano Rosas         /*
96652926b0dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
96752926b0dSFabiano Rosas          * to point to the next instruction
96852926b0dSFabiano Rosas          */
96952926b0dSFabiano Rosas         env->nip += 4;
97052926b0dSFabiano Rosas 
971bca2c6d9SFabiano Rosas         /*
972bca2c6d9SFabiano Rosas          * The Virtual Open Firmware (VOF) relies on the 'sc 1'
973bca2c6d9SFabiano Rosas          * instruction to communicate with QEMU. The pegasos2 machine
974bca2c6d9SFabiano Rosas          * uses VOF and the 74xx CPUs, so although the 74xx don't have
975bca2c6d9SFabiano Rosas          * HV mode, we need to keep hypercall support.
976bca2c6d9SFabiano Rosas          */
9772306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
978c700b5e1SNicholas Piggin             cpu->vhyp_class->hypercall(cpu->vhyp, cpu);
9792c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
98052926b0dSFabiano Rosas             return;
98152926b0dSFabiano Rosas         }
982bca2c6d9SFabiano Rosas 
98352926b0dSFabiano Rosas         break;
984bca2c6d9SFabiano Rosas     }
98552926b0dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
98652926b0dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
98752926b0dSFabiano Rosas         break;
98852926b0dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
9898e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
990bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
991bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset "
99252926b0dSFabiano Rosas                       "exception %d with no HV support\n", excp);
99352926b0dSFabiano Rosas         }
99452926b0dSFabiano Rosas         break;
99552926b0dSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
99652926b0dSFabiano Rosas         break;
99752926b0dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
99852926b0dSFabiano Rosas         break;
99952926b0dSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
100052926b0dSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
100152926b0dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
100252926b0dSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
100352926b0dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
1004bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
100552926b0dSFabiano Rosas                   powerpc_excp_name(excp));
100652926b0dSFabiano Rosas         break;
100752926b0dSFabiano Rosas     default:
1008bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1009bc30c1c6SBALATON Zoltan                   excp);
101052926b0dSFabiano Rosas         break;
101152926b0dSFabiano Rosas     }
101252926b0dSFabiano Rosas 
101352926b0dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
101452926b0dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
101552926b0dSFabiano Rosas     }
1016f82db777SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
1017f82db777SFabiano Rosas     env->spr[SPR_SRR1] = msr;
101852926b0dSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
101952926b0dSFabiano Rosas }
102052926b0dSFabiano Rosas 
powerpc_excp_booke(PowerPCCPU * cpu,int excp)1021180952ceSFabiano Rosas static void powerpc_excp_booke(PowerPCCPU *cpu, int excp)
1022180952ceSFabiano Rosas {
1023180952ceSFabiano Rosas     CPUPPCState *env = &cpu->env;
1024180952ceSFabiano Rosas     target_ulong msr, new_msr, vector;
1025ab452503SBALATON Zoltan     int srr0 = SPR_SRR0, srr1 = SPR_SRR1;
1026180952ceSFabiano Rosas 
1027ab452503SBALATON Zoltan     /*
1028ab452503SBALATON Zoltan      * Book E does not play games with certain bits of xSRR1 being MSR save
1029ab452503SBALATON Zoltan      * bits and others being error status. xSRR1 is the old MSR, period.
1030ab452503SBALATON Zoltan      */
1031180952ceSFabiano Rosas     msr = env->msr;
1032180952ceSFabiano Rosas 
1033ab452503SBALATON Zoltan     /* new interrupt handler msr preserves ME unless explicitly overridden */
10349dc20cc3SFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
1035180952ceSFabiano Rosas 
1036ab452503SBALATON Zoltan     /* HV emu assistance interrupt only exists on server arch 2.05 or later */
10379dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
1038180952ceSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
1039180952ceSFabiano Rosas     }
1040180952ceSFabiano Rosas 
1041180952ceSFabiano Rosas #ifdef TARGET_PPC64
1042180952ceSFabiano Rosas     /*
1043180952ceSFabiano Rosas      * SPEU and VPU share the same IVOR but they exist in different
1044180952ceSFabiano Rosas      * processors. SPEU is e500v1/2 only and VPU is e6500 only.
1045180952ceSFabiano Rosas      */
10469dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_VPU) {
1047180952ceSFabiano Rosas         excp = POWERPC_EXCP_SPEU;
1048180952ceSFabiano Rosas     }
1049180952ceSFabiano Rosas #endif
1050180952ceSFabiano Rosas 
1051180952ceSFabiano Rosas     vector = env->excp_vectors[excp];
1052180952ceSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
1053bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
1054bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
1055180952ceSFabiano Rosas     }
1056180952ceSFabiano Rosas     vector |= env->excp_prefix;
1057180952ceSFabiano Rosas 
1058180952ceSFabiano Rosas     switch (excp) {
1059180952ceSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
1060180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
1061180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
1062180952ceSFabiano Rosas         break;
1063180952ceSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
1064819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
1065180952ceSFabiano Rosas         /* machine check exceptions don't have ME set */
1066180952ceSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
1067180952ceSFabiano Rosas 
1068180952ceSFabiano Rosas         /* FIXME: choose one or the other based on CPU type */
1069180952ceSFabiano Rosas         srr0 = SPR_BOOKE_MCSRR0;
1070180952ceSFabiano Rosas         srr1 = SPR_BOOKE_MCSRR1;
1071180952ceSFabiano Rosas 
1072180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR0] = env->nip;
1073180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR1] = msr;
1074db403211SFabiano Rosas 
1075180952ceSFabiano Rosas         break;
1076180952ceSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
1077afdbc869SFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
1078180952ceSFabiano Rosas         break;
1079180952ceSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
1080180952ceSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
1081180952ceSFabiano Rosas         break;
1082180952ceSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
1083180952ceSFabiano Rosas         if (env->mpic_proxy) {
1084bc30c1c6SBALATON Zoltan             CPUState *cs = env_cpu(env);
1085180952ceSFabiano Rosas             /* IACK the IRQ on delivery */
1086180952ceSFabiano Rosas             env->spr[SPR_BOOKE_EPR] = ldl_phys(cs->as, env->mpic_iack);
1087180952ceSFabiano Rosas         }
1088180952ceSFabiano Rosas         break;
1089180952ceSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
1090180952ceSFabiano Rosas         break;
1091180952ceSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
1092180952ceSFabiano Rosas         switch (env->error_code & ~0xF) {
1093180952ceSFabiano Rosas         case POWERPC_EXCP_FP:
1094da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
1095180952ceSFabiano Rosas                 trace_ppc_excp_fp_ignore();
10963680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
1097180952ceSFabiano Rosas                 return;
1098180952ceSFabiano Rosas             }
1099180952ceSFabiano Rosas             /*
1100ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
1101ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
1102180952ceSFabiano Rosas              */
1103180952ceSFabiano Rosas             msr |= 0x00100000;
1104180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_FP;
1105180952ceSFabiano Rosas             break;
1106180952ceSFabiano Rosas         case POWERPC_EXCP_INVAL:
1107180952ceSFabiano Rosas             trace_ppc_excp_inval(env->nip);
1108180952ceSFabiano Rosas             msr |= 0x00080000;
1109180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PIL;
1110180952ceSFabiano Rosas             break;
1111180952ceSFabiano Rosas         case POWERPC_EXCP_PRIV:
1112180952ceSFabiano Rosas             msr |= 0x00040000;
1113180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PPR;
1114180952ceSFabiano Rosas             break;
1115180952ceSFabiano Rosas         case POWERPC_EXCP_TRAP:
1116180952ceSFabiano Rosas             msr |= 0x00020000;
1117180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PTR;
1118180952ceSFabiano Rosas             break;
1119180952ceSFabiano Rosas         default:
1120180952ceSFabiano Rosas             /* Should never occur */
1121bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
1122180952ceSFabiano Rosas                       env->error_code);
1123180952ceSFabiano Rosas             break;
1124180952ceSFabiano Rosas         }
1125180952ceSFabiano Rosas         break;
1126180952ceSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
1127180952ceSFabiano Rosas         dump_syscall(env);
1128180952ceSFabiano Rosas 
1129180952ceSFabiano Rosas         /*
1130180952ceSFabiano Rosas          * We need to correct the NIP which in this case is supposed
1131180952ceSFabiano Rosas          * to point to the next instruction
1132180952ceSFabiano Rosas          */
1133180952ceSFabiano Rosas         env->nip += 4;
1134180952ceSFabiano Rosas         break;
1135180952ceSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
1136180952ceSFabiano Rosas     case POWERPC_EXCP_APU:       /* Auxiliary processor unavailable          */
1137180952ceSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
1138180952ceSFabiano Rosas         break;
1139180952ceSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
1140180952ceSFabiano Rosas         /* FIT on 4xx */
1141180952ceSFabiano Rosas         trace_ppc_excp_print("FIT");
1142180952ceSFabiano Rosas         break;
1143180952ceSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
1144180952ceSFabiano Rosas         trace_ppc_excp_print("WDT");
1145180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
1146180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
1147180952ceSFabiano Rosas         break;
1148180952ceSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
1149180952ceSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
1150180952ceSFabiano Rosas         break;
1151180952ceSFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
1152180952ceSFabiano Rosas         if (env->flags & POWERPC_FLAG_DE) {
1153180952ceSFabiano Rosas             /* FIXME: choose one or the other based on CPU type */
1154180952ceSFabiano Rosas             srr0 = SPR_BOOKE_DSRR0;
1155180952ceSFabiano Rosas             srr1 = SPR_BOOKE_DSRR1;
1156180952ceSFabiano Rosas 
1157180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR0] = env->nip;
1158180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR1] = msr;
1159180952ceSFabiano Rosas 
1160180952ceSFabiano Rosas             /* DBSR already modified by caller */
1161180952ceSFabiano Rosas         } else {
1162bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
1163bc30c1c6SBALATON Zoltan                       "Debug exception triggered on unsupported model\n");
1164180952ceSFabiano Rosas         }
1165180952ceSFabiano Rosas         break;
1166180952ceSFabiano Rosas     case POWERPC_EXCP_SPEU:   /* SPE/embedded floating-point unavailable/VPU  */
1167180952ceSFabiano Rosas         env->spr[SPR_BOOKE_ESR] = ESR_SPV;
1168180952ceSFabiano Rosas         break;
11699364df26SNicholas Piggin     case POWERPC_EXCP_DOORI:     /* Embedded doorbell interrupt              */
11709364df26SNicholas Piggin         break;
11719364df26SNicholas Piggin     case POWERPC_EXCP_DOORCI:    /* Embedded doorbell critical interrupt     */
11729364df26SNicholas Piggin         srr0 = SPR_BOOKE_CSRR0;
11739364df26SNicholas Piggin         srr1 = SPR_BOOKE_CSRR1;
11749364df26SNicholas Piggin         break;
1175180952ceSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
11768e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
1177bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env),
1178bc30c1c6SBALATON Zoltan                       "Trying to deliver power-saving system reset "
1179180952ceSFabiano Rosas                       "exception %d with no HV support\n", excp);
1180180952ceSFabiano Rosas         }
1181180952ceSFabiano Rosas         break;
1182180952ceSFabiano Rosas     case POWERPC_EXCP_EFPDI:     /* Embedded floating-point data interrupt   */
1183180952ceSFabiano Rosas     case POWERPC_EXCP_EFPRI:     /* Embedded floating-point round interrupt  */
1184bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
1185180952ceSFabiano Rosas                   powerpc_excp_name(excp));
1186180952ceSFabiano Rosas         break;
1187180952ceSFabiano Rosas     default:
1188bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1189bc30c1c6SBALATON Zoltan                   excp);
1190180952ceSFabiano Rosas         break;
1191180952ceSFabiano Rosas     }
1192180952ceSFabiano Rosas 
1193f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
1194180952ceSFabiano Rosas     if (env->spr[SPR_BOOKE_EPCR] & EPCR_ICM) {
1195180952ceSFabiano Rosas         /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */
1196180952ceSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_CM;
1197180952ceSFabiano Rosas     } else {
1198180952ceSFabiano Rosas         vector = (uint32_t)vector;
1199180952ceSFabiano Rosas     }
1200180952ceSFabiano Rosas #endif
1201180952ceSFabiano Rosas 
1202180952ceSFabiano Rosas     env->spr[srr0] = env->nip;
1203180952ceSFabiano Rosas     env->spr[srr1] = msr;
1204180952ceSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
1205180952ceSFabiano Rosas }
1206180952ceSFabiano Rosas 
12077cebc5dbSNicholas Piggin /*
12087cebc5dbSNicholas Piggin  * When running a nested HV guest under vhyp, external interrupts are
12097cebc5dbSNicholas Piggin  * delivered as HVIRT.
12107cebc5dbSNicholas Piggin  */
books_vhyp_promotes_external_to_hvirt(PowerPCCPU * cpu)12117cebc5dbSNicholas Piggin static bool books_vhyp_promotes_external_to_hvirt(PowerPCCPU *cpu)
12127cebc5dbSNicholas Piggin {
12137cebc5dbSNicholas Piggin     if (cpu->vhyp) {
12147cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
12157cebc5dbSNicholas Piggin     }
12167cebc5dbSNicholas Piggin     return false;
12177cebc5dbSNicholas Piggin }
12187cebc5dbSNicholas Piggin 
121930c4e426SFabiano Rosas #ifdef TARGET_PPC64
12204c6cf6b2SNicholas Piggin /*
12214c6cf6b2SNicholas Piggin  * When running under vhyp, hcalls are always intercepted and sent to the
12224c6cf6b2SNicholas Piggin  * vhc->hypercall handler.
12234c6cf6b2SNicholas Piggin  */
books_vhyp_handles_hcall(PowerPCCPU * cpu)12244c6cf6b2SNicholas Piggin static bool books_vhyp_handles_hcall(PowerPCCPU *cpu)
12254c6cf6b2SNicholas Piggin {
12264c6cf6b2SNicholas Piggin     if (cpu->vhyp) {
12277cebc5dbSNicholas Piggin         return !vhyp_cpu_in_nested(cpu);
12287cebc5dbSNicholas Piggin     }
12297cebc5dbSNicholas Piggin     return false;
12307cebc5dbSNicholas Piggin }
12317cebc5dbSNicholas Piggin 
12327cebc5dbSNicholas Piggin /*
12337cebc5dbSNicholas Piggin  * When running a nested KVM HV guest under vhyp, HV exceptions are not
12347cebc5dbSNicholas Piggin  * delivered to the guest (because there is no concept of HV support), but
12357cebc5dbSNicholas Piggin  * rather they are sent to the vhyp to exit from the L2 back to the L1 and
12367cebc5dbSNicholas Piggin  * return from the H_ENTER_NESTED hypercall.
12377cebc5dbSNicholas Piggin  */
books_vhyp_handles_hv_excp(PowerPCCPU * cpu)12387cebc5dbSNicholas Piggin static bool books_vhyp_handles_hv_excp(PowerPCCPU *cpu)
12397cebc5dbSNicholas Piggin {
12407cebc5dbSNicholas Piggin     if (cpu->vhyp) {
12417cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
12424c6cf6b2SNicholas Piggin     }
12434c6cf6b2SNicholas Piggin     return false;
12444c6cf6b2SNicholas Piggin }
12454c6cf6b2SNicholas Piggin 
12465a5d3b23SNicholas Piggin #ifdef CONFIG_TCG
is_prefix_insn(CPUPPCState * env,uint32_t insn)12475a5d3b23SNicholas Piggin static bool is_prefix_insn(CPUPPCState *env, uint32_t insn)
12485a5d3b23SNicholas Piggin {
12495a5d3b23SNicholas Piggin     if (!(env->insns_flags2 & PPC2_ISA310)) {
12505a5d3b23SNicholas Piggin         return false;
12515a5d3b23SNicholas Piggin     }
12525a5d3b23SNicholas Piggin     return ((insn & 0xfc000000) == 0x04000000);
12535a5d3b23SNicholas Piggin }
12545a5d3b23SNicholas Piggin 
is_prefix_insn_excp(PowerPCCPU * cpu,int excp)12555a5d3b23SNicholas Piggin static bool is_prefix_insn_excp(PowerPCCPU *cpu, int excp)
12565a5d3b23SNicholas Piggin {
12575a5d3b23SNicholas Piggin     CPUPPCState *env = &cpu->env;
12585a5d3b23SNicholas Piggin 
1259c8fd9667SNicholas Piggin     if (!(env->insns_flags2 & PPC2_ISA310)) {
1260c8fd9667SNicholas Piggin         return false;
1261c8fd9667SNicholas Piggin     }
1262c8fd9667SNicholas Piggin 
12635a5d3b23SNicholas Piggin     if (!tcg_enabled()) {
12645a5d3b23SNicholas Piggin         /*
12655a5d3b23SNicholas Piggin          * This does not load instructions and set the prefix bit correctly
12665a5d3b23SNicholas Piggin          * for injected interrupts with KVM. That may have to be discovered
12675a5d3b23SNicholas Piggin          * and set by the KVM layer before injecting.
12685a5d3b23SNicholas Piggin          */
12695a5d3b23SNicholas Piggin         return false;
12705a5d3b23SNicholas Piggin     }
12715a5d3b23SNicholas Piggin 
12725a5d3b23SNicholas Piggin     switch (excp) {
1273c8fd9667SNicholas Piggin     case POWERPC_EXCP_MCHECK:
1274c8fd9667SNicholas Piggin         if (!(env->error_code & PPC_BIT(42))) {
1275c8fd9667SNicholas Piggin             /*
1276c8fd9667SNicholas Piggin              * Fetch attempt caused a machine check, so attempting to fetch
1277c8fd9667SNicholas Piggin              * again would cause a recursive machine check.
1278c8fd9667SNicholas Piggin              */
1279c8fd9667SNicholas Piggin             return false;
1280c8fd9667SNicholas Piggin         }
1281c8fd9667SNicholas Piggin         break;
12825a5d3b23SNicholas Piggin     case POWERPC_EXCP_HDSI:
12835a5d3b23SNicholas Piggin         /* HDSI PRTABLE_FAULT has the originating access type in error_code */
12845a5d3b23SNicholas Piggin         if ((env->spr[SPR_HDSISR] & DSISR_PRTABLE_FAULT) &&
12855a5d3b23SNicholas Piggin             (env->error_code == MMU_INST_FETCH)) {
12865a5d3b23SNicholas Piggin             /*
12875a5d3b23SNicholas Piggin              * Fetch failed due to partition scope translation, so prefix
12885a5d3b23SNicholas Piggin              * indication is not relevant (and attempting to load the
12895a5d3b23SNicholas Piggin              * instruction at NIP would cause recursive faults with the same
12905a5d3b23SNicholas Piggin              * translation).
12915a5d3b23SNicholas Piggin              */
1292c8fd9667SNicholas Piggin             return false;
12935a5d3b23SNicholas Piggin         }
1294c8fd9667SNicholas Piggin         break;
1295c8fd9667SNicholas Piggin 
12965a5d3b23SNicholas Piggin     case POWERPC_EXCP_DSI:
12975a5d3b23SNicholas Piggin     case POWERPC_EXCP_DSEG:
12985a5d3b23SNicholas Piggin     case POWERPC_EXCP_ALIGN:
12995a5d3b23SNicholas Piggin     case POWERPC_EXCP_PROGRAM:
13005a5d3b23SNicholas Piggin     case POWERPC_EXCP_FPU:
13015a5d3b23SNicholas Piggin     case POWERPC_EXCP_TRACE:
13025a5d3b23SNicholas Piggin     case POWERPC_EXCP_HV_EMU:
13035a5d3b23SNicholas Piggin     case POWERPC_EXCP_VPU:
13045a5d3b23SNicholas Piggin     case POWERPC_EXCP_VSXU:
13055a5d3b23SNicholas Piggin     case POWERPC_EXCP_FU:
1306c8fd9667SNicholas Piggin     case POWERPC_EXCP_HV_FU:
13075a5d3b23SNicholas Piggin         break;
13085a5d3b23SNicholas Piggin     default:
13095a5d3b23SNicholas Piggin         return false;
13105a5d3b23SNicholas Piggin     }
1311c8fd9667SNicholas Piggin 
1312c8fd9667SNicholas Piggin     return is_prefix_insn(env, ppc_ldl_code(env, env->nip));
1313c8fd9667SNicholas Piggin }
13145a5d3b23SNicholas Piggin #else
is_prefix_insn_excp(PowerPCCPU * cpu,int excp)13155a5d3b23SNicholas Piggin static bool is_prefix_insn_excp(PowerPCCPU *cpu, int excp)
13165a5d3b23SNicholas Piggin {
13175a5d3b23SNicholas Piggin     return false;
13185a5d3b23SNicholas Piggin }
13195a5d3b23SNicholas Piggin #endif
13205a5d3b23SNicholas Piggin 
powerpc_excp_books(PowerPCCPU * cpu,int excp)13219f338e4dSFabiano Rosas static void powerpc_excp_books(PowerPCCPU *cpu, int excp)
13229f338e4dSFabiano Rosas {
13239f338e4dSFabiano Rosas     CPUPPCState *env = &cpu->env;
13249f338e4dSFabiano Rosas     target_ulong msr, new_msr, vector;
1325ab452503SBALATON Zoltan     int srr0 = SPR_SRR0, srr1 = SPR_SRR1, lev = -1;
13269f338e4dSFabiano Rosas 
13279f338e4dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
13289f338e4dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
13299f338e4dSFabiano Rosas 
13309f338e4dSFabiano Rosas     /*
1331ab452503SBALATON Zoltan      * new interrupt handler msr preserves HV and ME unless explicitly
1332ab452503SBALATON Zoltan      * overridden
13339f338e4dSFabiano Rosas      */
13349f338e4dSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME) | MSR_HVB);
13359f338e4dSFabiano Rosas 
13369f338e4dSFabiano Rosas     /*
13379f338e4dSFabiano Rosas      * check for special resume at 0x100 from doze/nap/sleep/winkle on
13389f338e4dSFabiano Rosas      * P7/P8/P9
13399f338e4dSFabiano Rosas      */
13409f338e4dSFabiano Rosas     if (env->resume_as_sreset) {
13413f88a89dSBALATON Zoltan         excp = powerpc_reset_wakeup(env, excp, &msr);
13429f338e4dSFabiano Rosas     }
13439f338e4dSFabiano Rosas 
13449f338e4dSFabiano Rosas     /*
134530c4e426SFabiano Rosas      * We don't want to generate a Hypervisor Emulation Assistance
13466c242e79SNicholas Piggin      * Interrupt if we don't have HVB in msr_mask (PAPR mode),
13476c242e79SNicholas Piggin      * unless running a nested-hv guest, in which case the L1
13486c242e79SNicholas Piggin      * kernel wants the interrupt.
13499f338e4dSFabiano Rosas      */
13506c242e79SNicholas Piggin     if (excp == POWERPC_EXCP_HV_EMU && !(env->msr_mask & MSR_HVB) &&
13516c242e79SNicholas Piggin             !books_vhyp_handles_hv_excp(cpu)) {
13529f338e4dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
13539f338e4dSFabiano Rosas     }
13549f338e4dSFabiano Rosas 
13559f338e4dSFabiano Rosas     vector = env->excp_vectors[excp];
13569f338e4dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
1357bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env),
1358bc30c1c6SBALATON Zoltan                   "Raised an exception without defined vector %d\n", excp);
13599f338e4dSFabiano Rosas     }
13609f338e4dSFabiano Rosas     vector |= env->excp_prefix;
13619f338e4dSFabiano Rosas 
13625a5d3b23SNicholas Piggin     if (is_prefix_insn_excp(cpu, excp)) {
13635a5d3b23SNicholas Piggin         msr |= PPC_BIT(34);
13645a5d3b23SNicholas Piggin     }
13655a5d3b23SNicholas Piggin 
13669f338e4dSFabiano Rosas     switch (excp) {
13679f338e4dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
1368819b31b0SBALATON Zoltan         powerpc_mcheck_checkstop(env);
13699f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
13709f338e4dSFabiano Rosas             /*
13719f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
13729f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR).
13739f338e4dSFabiano Rosas              */
13749f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
137543453161SNicholas Piggin 
137643453161SNicholas Piggin             /* HV machine check exceptions don't have ME set */
13779f338e4dSFabiano Rosas             new_msr &= ~((target_ulong)1 << MSR_ME);
137843453161SNicholas Piggin         }
13799f338e4dSFabiano Rosas 
138055a7fa34SNicholas Piggin         msr |= env->error_code;
13819f338e4dSFabiano Rosas         break;
138255a7fa34SNicholas Piggin 
13839f338e4dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
13849f338e4dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
13859f338e4dSFabiano Rosas         break;
13869f338e4dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
13879f338e4dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
13889f338e4dSFabiano Rosas         msr |= env->error_code;
13899f338e4dSFabiano Rosas         break;
13909f338e4dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
13919f338e4dSFabiano Rosas     {
13929f338e4dSFabiano Rosas         bool lpes0;
13939f338e4dSFabiano Rosas 
1394ab452503SBALATON Zoltan         /* LPES0 is only taken into consideration if we support HV mode */
139567baff77SFabiano Rosas         if (!env->has_hv_mode) {
139667baff77SFabiano Rosas             break;
13979f338e4dSFabiano Rosas         }
139867baff77SFabiano Rosas         lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
13999f338e4dSFabiano Rosas         if (!lpes0) {
14009f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14019f338e4dSFabiano Rosas             new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
14029f338e4dSFabiano Rosas             srr0 = SPR_HSRR0;
14039f338e4dSFabiano Rosas             srr1 = SPR_HSRR1;
14049f338e4dSFabiano Rosas         }
14059f338e4dSFabiano Rosas         break;
14069f338e4dSFabiano Rosas     }
14079f338e4dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
14084ee5d281SNicholas Piggin         /* Optional DSISR update was removed from ISA v3.0 */
14094ee5d281SNicholas Piggin         if (!(env->insns_flags2 & PPC2_ISA300)) {
14109f338e4dSFabiano Rosas             /* Get rS/rD and rA from faulting opcode */
14119f338e4dSFabiano Rosas             /*
14129f338e4dSFabiano Rosas              * Note: the opcode fields will not be set properly for a
14139f338e4dSFabiano Rosas              * direct store load/store, but nobody cares as nobody
14149f338e4dSFabiano Rosas              * actually uses direct store segments.
14159f338e4dSFabiano Rosas              */
14169f338e4dSFabiano Rosas             env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
14174ee5d281SNicholas Piggin         }
14189f338e4dSFabiano Rosas         break;
14199f338e4dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
14209f338e4dSFabiano Rosas         switch (env->error_code & ~0xF) {
14219f338e4dSFabiano Rosas         case POWERPC_EXCP_FP:
1422da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
14239f338e4dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
14243680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
14259f338e4dSFabiano Rosas                 return;
14269f338e4dSFabiano Rosas             }
14279f338e4dSFabiano Rosas             /*
1428ab452503SBALATON Zoltan              * NIP always points to the faulting instruction for FP exceptions,
1429ab452503SBALATON Zoltan              * so always use store_next and claim we are precise in the MSR.
14309f338e4dSFabiano Rosas              */
14319f338e4dSFabiano Rosas             msr |= 0x00100000;
14329f338e4dSFabiano Rosas             break;
14339f338e4dSFabiano Rosas         case POWERPC_EXCP_INVAL:
14349f338e4dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
14359f338e4dSFabiano Rosas             msr |= 0x00080000;
14369f338e4dSFabiano Rosas             break;
14379f338e4dSFabiano Rosas         case POWERPC_EXCP_PRIV:
14389f338e4dSFabiano Rosas             msr |= 0x00040000;
14399f338e4dSFabiano Rosas             break;
14409f338e4dSFabiano Rosas         case POWERPC_EXCP_TRAP:
14419f338e4dSFabiano Rosas             msr |= 0x00020000;
14429f338e4dSFabiano Rosas             break;
14439f338e4dSFabiano Rosas         default:
14449f338e4dSFabiano Rosas             /* Should never occur */
1445bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Invalid program exception %d. Aborting\n",
14469f338e4dSFabiano Rosas                       env->error_code);
14479f338e4dSFabiano Rosas             break;
14489f338e4dSFabiano Rosas         }
14499f338e4dSFabiano Rosas         break;
14509f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
14519f338e4dSFabiano Rosas         lev = env->error_code;
14529f338e4dSFabiano Rosas 
14532306c606SBALATON Zoltan         if (lev == 1 && cpu->vhyp) {
14549f338e4dSFabiano Rosas             dump_hcall(env);
14559f338e4dSFabiano Rosas         } else {
14569f338e4dSFabiano Rosas             dump_syscall(env);
14579f338e4dSFabiano Rosas         }
14589f338e4dSFabiano Rosas 
14599f338e4dSFabiano Rosas         /*
14609f338e4dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
14619f338e4dSFabiano Rosas          * to point to the next instruction
14629f338e4dSFabiano Rosas          */
14639f338e4dSFabiano Rosas         env->nip += 4;
14649f338e4dSFabiano Rosas 
14659f338e4dSFabiano Rosas         /* "PAPR mode" built-in hypercall emulation */
14662306c606SBALATON Zoltan         if (lev == 1 && books_vhyp_handles_hcall(cpu)) {
1467c700b5e1SNicholas Piggin             cpu->vhyp_class->hypercall(cpu->vhyp, cpu);
14682c71b4f6SNicholas Piggin             powerpc_reset_excp_state(cpu);
14699f338e4dSFabiano Rosas             return;
14709f338e4dSFabiano Rosas         }
1471eb701f30SNicholas Piggin         if (env->insns_flags2 & PPC2_ISA310) {
1472eb701f30SNicholas Piggin             /* ISAv3.1 puts LEV into SRR1 */
1473eb701f30SNicholas Piggin             msr |= lev << 20;
1474eb701f30SNicholas Piggin         }
14759f338e4dSFabiano Rosas         if (lev == 1) {
14769f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14779f338e4dSFabiano Rosas         }
14789f338e4dSFabiano Rosas         break;
14799f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL_VECTORED: /* scv exception                     */
14809f338e4dSFabiano Rosas         lev = env->error_code;
14819f338e4dSFabiano Rosas         dump_syscall(env);
14829f338e4dSFabiano Rosas         env->nip += 4;
14839f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_EE);
14849f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
14859f338e4dSFabiano Rosas 
14869f338e4dSFabiano Rosas         vector += lev * 0x20;
14879f338e4dSFabiano Rosas 
14889f338e4dSFabiano Rosas         env->lr = env->nip;
14899f338e4dSFabiano Rosas         env->ctr = msr;
14909f338e4dSFabiano Rosas         break;
14919f338e4dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
14929f338e4dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
14939f338e4dSFabiano Rosas         break;
14949f338e4dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
14959f338e4dSFabiano Rosas         /* A power-saving exception sets ME, otherwise it is unchanged */
14968e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
14979f338e4dSFabiano Rosas             /* indicate that we resumed from power save mode */
14989f338e4dSFabiano Rosas             msr |= 0x10000;
14999f338e4dSFabiano Rosas             new_msr |= ((target_ulong)1 << MSR_ME);
15009f338e4dSFabiano Rosas         }
15019f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
15029f338e4dSFabiano Rosas             /*
15039f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
15049f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR, NMI injection in QEMU).
15059f338e4dSFabiano Rosas              */
15069f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
15079f338e4dSFabiano Rosas         } else {
15088e54ad65SVíctor Colombo             if (FIELD_EX64(env->msr, MSR, POW)) {
1509bc30c1c6SBALATON Zoltan                 cpu_abort(env_cpu(env),
1510bc30c1c6SBALATON Zoltan                           "Trying to deliver power-saving system reset "
15119f338e4dSFabiano Rosas                           "exception %d with no HV support\n", excp);
15129f338e4dSFabiano Rosas             }
15139f338e4dSFabiano Rosas         }
15149f338e4dSFabiano Rosas         break;
151514895384SNicholas Piggin     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
151614895384SNicholas Piggin         msr |= env->error_code;
151714895384SNicholas Piggin         /* fall through */
15189f338e4dSFabiano Rosas     case POWERPC_EXCP_DSEG:      /* Data segment exception                   */
15199f338e4dSFabiano Rosas     case POWERPC_EXCP_ISEG:      /* Instruction segment exception            */
1520fd7abfabSNicholas Piggin     case POWERPC_EXCP_SDOOR:     /* Doorbell interrupt                       */
1521c29b0704SNicholas Piggin     case POWERPC_EXCP_PERFM:     /* Performance monitor interrupt            */
15229f338e4dSFabiano Rosas         break;
15239f338e4dSFabiano Rosas     case POWERPC_EXCP_HISI:      /* Hypervisor instruction storage exception */
15249f338e4dSFabiano Rosas         msr |= env->error_code;
15259f338e4dSFabiano Rosas         /* fall through */
15269f338e4dSFabiano Rosas     case POWERPC_EXCP_HDECR:     /* Hypervisor decrementer exception         */
15279f338e4dSFabiano Rosas     case POWERPC_EXCP_HDSI:      /* Hypervisor data storage exception        */
15289f338e4dSFabiano Rosas     case POWERPC_EXCP_SDOOR_HV:  /* Hypervisor Doorbell interrupt            */
15299f338e4dSFabiano Rosas     case POWERPC_EXCP_HVIRT:     /* Hypervisor virtualization                */
15309f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
15319f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
15329f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
15339f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
15349f338e4dSFabiano Rosas         break;
1535a3c020d8SNicholas Piggin #ifdef CONFIG_TCG
1536a3c020d8SNicholas Piggin     case POWERPC_EXCP_HV_EMU: {
1537a3c020d8SNicholas Piggin         uint32_t insn = ppc_ldl_code(env, env->nip);
1538a3c020d8SNicholas Piggin         env->spr[SPR_HEIR] = insn;
1539a3c020d8SNicholas Piggin         if (is_prefix_insn(env, insn)) {
1540a3c020d8SNicholas Piggin             uint32_t insn2 = ppc_ldl_code(env, env->nip + 4);
1541a3c020d8SNicholas Piggin             env->spr[SPR_HEIR] <<= 32;
1542a3c020d8SNicholas Piggin             env->spr[SPR_HEIR] |= insn2;
1543a3c020d8SNicholas Piggin         }
1544a3c020d8SNicholas Piggin         srr0 = SPR_HSRR0;
1545a3c020d8SNicholas Piggin         srr1 = SPR_HSRR1;
1546a3c020d8SNicholas Piggin         new_msr |= (target_ulong)MSR_HVB;
1547a3c020d8SNicholas Piggin         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
1548a3c020d8SNicholas Piggin         break;
1549a3c020d8SNicholas Piggin     }
1550a3c020d8SNicholas Piggin #endif
15519f338e4dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
15529f338e4dSFabiano Rosas     case POWERPC_EXCP_VSXU:       /* VSX unavailable exception               */
15539f338e4dSFabiano Rosas     case POWERPC_EXCP_FU:         /* Facility unavailable exception          */
15549f338e4dSFabiano Rosas         env->spr[SPR_FSCR] |= ((target_ulong)env->error_code << 56);
15559f338e4dSFabiano Rosas         break;
15569f338e4dSFabiano Rosas     case POWERPC_EXCP_HV_FU:     /* Hypervisor Facility Unavailable Exception */
15579f338e4dSFabiano Rosas         env->spr[SPR_HFSCR] |= ((target_ulong)env->error_code << FSCR_IC_POS);
15589f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
15599f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
15609f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
15619f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
15629f338e4dSFabiano Rosas         break;
1563cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_PERFM_EBB:        /* Performance Monitor EBB Exception  */
1564cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_EXTERNAL_EBB:     /* External EBB Exception             */
1565cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
1566cb76bbc4SDaniel Henrique Barboza 
1567cb76bbc4SDaniel Henrique Barboza         /*
1568cb76bbc4SDaniel Henrique Barboza          * Save NIP for rfebb insn in SPR_EBBRR. Next nip is
1569cb76bbc4SDaniel Henrique Barboza          * stored in the EBB Handler SPR_EBBHR.
1570cb76bbc4SDaniel Henrique Barboza          */
1571cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_EBBRR] = env->nip;
1572cb76bbc4SDaniel Henrique Barboza         powerpc_set_excp_state(cpu, env->spr[SPR_EBBHR], env->msr);
1573cb76bbc4SDaniel Henrique Barboza 
1574cb76bbc4SDaniel Henrique Barboza         /*
1575cb76bbc4SDaniel Henrique Barboza          * This exception is handled in userspace. No need to proceed.
1576cb76bbc4SDaniel Henrique Barboza          */
1577cb76bbc4SDaniel Henrique Barboza         return;
15789f338e4dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
15799f338e4dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
15809f338e4dSFabiano Rosas     case POWERPC_EXCP_MAINT:     /* Maintenance exception                    */
158130c4e426SFabiano Rosas     case POWERPC_EXCP_HV_MAINT:  /* Hypervisor Maintenance exception         */
1582bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "%s exception not implemented\n",
15839f338e4dSFabiano Rosas                   powerpc_excp_name(excp));
15849f338e4dSFabiano Rosas         break;
15859f338e4dSFabiano Rosas     default:
1586bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1587bc30c1c6SBALATON Zoltan                   excp);
15889f338e4dSFabiano Rosas         break;
15899f338e4dSFabiano Rosas     }
15909f338e4dSFabiano Rosas 
15919f338e4dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
15929f338e4dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
15939f338e4dSFabiano Rosas     }
15949f338e4dSFabiano Rosas     new_msr |= (target_ulong)1 << MSR_SF;
15959f338e4dSFabiano Rosas 
15969f338e4dSFabiano Rosas     if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
15979f338e4dSFabiano Rosas         env->spr[srr0] = env->nip;
15989f338e4dSFabiano Rosas         env->spr[srr1] = msr;
15999f338e4dSFabiano Rosas     }
16009f338e4dSFabiano Rosas 
16017cebc5dbSNicholas Piggin     if ((new_msr & MSR_HVB) && books_vhyp_handles_hv_excp(cpu)) {
16027cebc5dbSNicholas Piggin         /* Deliver interrupt to L1 by returning from the H_ENTER_NESTED call */
1603c700b5e1SNicholas Piggin         cpu->vhyp_class->deliver_hv_excp(cpu, excp);
16047cebc5dbSNicholas Piggin         powerpc_reset_excp_state(cpu);
16057cebc5dbSNicholas Piggin     } else {
16067cebc5dbSNicholas Piggin         /* Sanity check */
16077cebc5dbSNicholas Piggin         if (!(env->msr_mask & MSR_HVB) && srr0 == SPR_HSRR0) {
1608bc30c1c6SBALATON Zoltan             cpu_abort(env_cpu(env), "Trying to deliver HV exception (HSRR) %d "
1609bc30c1c6SBALATON Zoltan                       "with no HV support\n", excp);
16107cebc5dbSNicholas Piggin         }
16119f338e4dSFabiano Rosas         /* This can update new_msr and vector if AIL applies */
161210895ab6SFabiano Rosas         ppc_excp_apply_ail(cpu, excp, msr, &new_msr, &vector);
16139f338e4dSFabiano Rosas         powerpc_set_excp_state(cpu, vector, new_msr);
16149f338e4dSFabiano Rosas     }
16157cebc5dbSNicholas Piggin }
161630c4e426SFabiano Rosas #else
powerpc_excp_books(PowerPCCPU * cpu,int excp)161730c4e426SFabiano Rosas static inline void powerpc_excp_books(PowerPCCPU *cpu, int excp)
161830c4e426SFabiano Rosas {
161930c4e426SFabiano Rosas     g_assert_not_reached();
162030c4e426SFabiano Rosas }
1621f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
16229f338e4dSFabiano Rosas 
powerpc_excp(PowerPCCPU * cpu,int excp)1623dc88dd0aSFabiano Rosas static void powerpc_excp(PowerPCCPU *cpu, int excp)
1624dc88dd0aSFabiano Rosas {
1625dc88dd0aSFabiano Rosas     CPUPPCState *env = &cpu->env;
1626dc88dd0aSFabiano Rosas 
1627c6eaac89SFabiano Rosas     if (excp <= POWERPC_EXCP_NONE || excp >= POWERPC_EXCP_NB) {
1628bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC exception %d. Aborting\n",
1629bc30c1c6SBALATON Zoltan                   excp);
1630c6eaac89SFabiano Rosas     }
1631c6eaac89SFabiano Rosas 
1632c6eaac89SFabiano Rosas     qemu_log_mask(CPU_LOG_INT, "Raise exception at " TARGET_FMT_lx
1633c6eaac89SFabiano Rosas                   " => %s (%d) error=%02x\n", env->nip, powerpc_excp_name(excp),
1634c6eaac89SFabiano Rosas                   excp, env->error_code);
16358a15cceeSBALATON Zoltan     env->excp_stats[excp]++;
1636c6eaac89SFabiano Rosas 
1637dc88dd0aSFabiano Rosas     switch (env->excp_model) {
1638e808c2edSFabiano Rosas     case POWERPC_EXCP_40x:
1639e808c2edSFabiano Rosas         powerpc_excp_40x(cpu, excp);
1640e808c2edSFabiano Rosas         break;
164158d178fbSFabiano Rosas     case POWERPC_EXCP_6xx:
164258d178fbSFabiano Rosas         powerpc_excp_6xx(cpu, excp);
164358d178fbSFabiano Rosas         break;
1644ccfca2fcSFabiano Rosas     case POWERPC_EXCP_7xx:
1645ccfca2fcSFabiano Rosas         powerpc_excp_7xx(cpu, excp);
1646ccfca2fcSFabiano Rosas         break;
164752926b0dSFabiano Rosas     case POWERPC_EXCP_74xx:
164852926b0dSFabiano Rosas         powerpc_excp_74xx(cpu, excp);
164952926b0dSFabiano Rosas         break;
1650180952ceSFabiano Rosas     case POWERPC_EXCP_BOOKE:
1651180952ceSFabiano Rosas         powerpc_excp_booke(cpu, excp);
1652180952ceSFabiano Rosas         break;
16539f338e4dSFabiano Rosas     case POWERPC_EXCP_970:
16549f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER7:
16559f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER8:
16569f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER9:
16579f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER10:
1658c0d96407SAditya Gupta     case POWERPC_EXCP_POWER11:
16599f338e4dSFabiano Rosas         powerpc_excp_books(cpu, excp);
16609f338e4dSFabiano Rosas         break;
1661dc88dd0aSFabiano Rosas     default:
166228091374SFabiano Rosas         g_assert_not_reached();
1663dc88dd0aSFabiano Rosas     }
1664dc88dd0aSFabiano Rosas }
1665dc88dd0aSFabiano Rosas 
ppc_cpu_do_interrupt(CPUState * cs)1666fcf5ef2aSThomas Huth void ppc_cpu_do_interrupt(CPUState *cs)
1667fcf5ef2aSThomas Huth {
1668fcf5ef2aSThomas Huth     PowerPCCPU *cpu = POWERPC_CPU(cs);
1669fcf5ef2aSThomas Huth 
167093130c84SFabiano Rosas     powerpc_excp(cpu, cs->exception_index);
1671fcf5ef2aSThomas Huth }
1672fcf5ef2aSThomas Huth 
1673f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
1674c8e1de2eSMatheus Ferst #define P7_UNUSED_INTERRUPTS \
1675c8e1de2eSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_HVIRT | PPC_INTERRUPT_CEXT |       \
1676c8e1de2eSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |      \
1677c8e1de2eSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_DOORBELL | PPC_INTERRUPT_HDOORBELL | \
1678c8e1de2eSMatheus Ferst      PPC_INTERRUPT_THERM | PPC_INTERRUPT_EBB)
1679c8e1de2eSMatheus Ferst 
p7_interrupt_powersave(uint32_t pending_interrupts,target_ulong lpcr)1680085cc364SHarsh Prateek Bora static int p7_interrupt_powersave(uint32_t pending_interrupts,
1681085cc364SHarsh Prateek Bora                                   target_ulong lpcr)
16829c713713SMatheus Ferst {
1683085cc364SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_EXT) &&
1684085cc364SHarsh Prateek Bora         (lpcr & LPCR_P7_PECE0)) {
16859c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
16869c713713SMatheus Ferst     }
1687085cc364SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DECR) &&
1688085cc364SHarsh Prateek Bora         (lpcr & LPCR_P7_PECE1)) {
16899c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
16909c713713SMatheus Ferst     }
1691085cc364SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_MCK) &&
1692085cc364SHarsh Prateek Bora         (lpcr & LPCR_P7_PECE2)) {
16939c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
16949c713713SMatheus Ferst     }
1695085cc364SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HMI) &&
1696085cc364SHarsh Prateek Bora         (lpcr & LPCR_P7_PECE2)) {
16979c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
16989c713713SMatheus Ferst     }
1699085cc364SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_RESET) {
17009c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
17019c713713SMatheus Ferst     }
17029c713713SMatheus Ferst     return 0;
17039c713713SMatheus Ferst }
17049c713713SMatheus Ferst 
p7_next_unmasked_interrupt(CPUPPCState * env,uint32_t pending_interrupts,target_ulong lpcr)1705085cc364SHarsh Prateek Bora static int p7_next_unmasked_interrupt(CPUPPCState *env,
1706085cc364SHarsh Prateek Bora                                       uint32_t pending_interrupts,
1707085cc364SHarsh Prateek Bora                                       target_ulong lpcr)
1708bf303fb3SMatheus Ferst {
17094e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
17104e6b7db2SBALATON Zoltan 
1711022b7128SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
1712022b7128SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1713bf303fb3SMatheus Ferst 
1714085cc364SHarsh Prateek Bora     assert((pending_interrupts & P7_UNUSED_INTERRUPTS) == 0);
1715c8e1de2eSMatheus Ferst 
1716022b7128SMatheus Ferst     if (cs->halted) {
1717022b7128SMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
1718085cc364SHarsh Prateek Bora         return p7_interrupt_powersave(pending_interrupts, lpcr);
1719022b7128SMatheus Ferst     }
1720022b7128SMatheus Ferst 
1721bf303fb3SMatheus Ferst     /* Machine check exception */
1722085cc364SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_MCK) {
1723bf303fb3SMatheus Ferst         return PPC_INTERRUPT_MCK;
1724bf303fb3SMatheus Ferst     }
1725bf303fb3SMatheus Ferst 
1726bf303fb3SMatheus Ferst     /* Hypervisor decrementer exception */
1727085cc364SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HDECR) {
1728bf303fb3SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1729bf303fb3SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
1730022b7128SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1731bf303fb3SMatheus Ferst             /* HDEC clears on delivery */
1732bf303fb3SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1733bf303fb3SMatheus Ferst         }
1734bf303fb3SMatheus Ferst     }
1735bf303fb3SMatheus Ferst 
1736bf303fb3SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1737085cc364SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_EXT) {
1738085cc364SHarsh Prateek Bora         bool lpes0 = !!(lpcr & LPCR_LPES0);
1739085cc364SHarsh Prateek Bora         bool heic = !!(lpcr & LPCR_HEIC);
1740bf303fb3SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
1741022b7128SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1742bf303fb3SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1743bf303fb3SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1744bf303fb3SMatheus Ferst             return PPC_INTERRUPT_EXT;
1745bf303fb3SMatheus Ferst         }
1746bf303fb3SMatheus Ferst     }
1747022b7128SMatheus Ferst     if (msr_ee != 0) {
1748bf303fb3SMatheus Ferst         /* Decrementer exception */
1749085cc364SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DECR) {
1750bf303fb3SMatheus Ferst             return PPC_INTERRUPT_DECR;
1751bf303fb3SMatheus Ferst         }
1752085cc364SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_PERFM) {
1753bf303fb3SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1754bf303fb3SMatheus Ferst         }
1755bf303fb3SMatheus Ferst     }
1756bf303fb3SMatheus Ferst 
1757bf303fb3SMatheus Ferst     return 0;
1758bf303fb3SMatheus Ferst }
1759bf303fb3SMatheus Ferst 
1760f6194fddSMatheus Ferst #define P8_UNUSED_INTERRUPTS \
1761f6194fddSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_HVIRT |  \
1762f6194fddSMatheus Ferst     PPC_INTERRUPT_CEXT | PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL |  \
1763f6194fddSMatheus Ferst     PPC_INTERRUPT_FIT | PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1764f6194fddSMatheus Ferst 
p8_interrupt_powersave(uint32_t pending_interrupts,target_ulong lpcr)176537e62398SHarsh Prateek Bora static int p8_interrupt_powersave(uint32_t pending_interrupts,
176637e62398SHarsh Prateek Bora                                   target_ulong lpcr)
17679c713713SMatheus Ferst {
176837e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_EXT) &&
176937e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE2)) {
17709c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
17719c713713SMatheus Ferst     }
177237e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DECR) &&
177337e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE3)) {
17749c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
17759c713713SMatheus Ferst     }
177637e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_MCK) &&
177737e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE4)) {
17789c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
17799c713713SMatheus Ferst     }
178037e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HMI) &&
178137e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE4)) {
17829c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
17839c713713SMatheus Ferst     }
178437e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
178537e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE0)) {
17869c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
17879c713713SMatheus Ferst     }
178837e62398SHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
178937e62398SHarsh Prateek Bora         (lpcr & LPCR_P8_PECE1)) {
17909c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
17919c713713SMatheus Ferst     }
179237e62398SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_RESET) {
17939c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
17949c713713SMatheus Ferst     }
17959c713713SMatheus Ferst     return 0;
17969c713713SMatheus Ferst }
17979c713713SMatheus Ferst 
p8_next_unmasked_interrupt(CPUPPCState * env,uint32_t pending_interrupts,target_ulong lpcr)179837e62398SHarsh Prateek Bora static int p8_next_unmasked_interrupt(CPUPPCState *env,
179937e62398SHarsh Prateek Bora                                       uint32_t pending_interrupts,
180037e62398SHarsh Prateek Bora                                       target_ulong lpcr)
1801a9899d42SMatheus Ferst {
18024e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
18034e6b7db2SBALATON Zoltan 
180464a9b5eeSMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
180564a9b5eeSMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1806a9899d42SMatheus Ferst 
1807f6194fddSMatheus Ferst     assert((env->pending_interrupts & P8_UNUSED_INTERRUPTS) == 0);
1808f6194fddSMatheus Ferst 
180964a9b5eeSMatheus Ferst     if (cs->halted) {
181064a9b5eeSMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
181137e62398SHarsh Prateek Bora         return p8_interrupt_powersave(pending_interrupts, lpcr);
181264a9b5eeSMatheus Ferst     }
181364a9b5eeSMatheus Ferst 
1814a9899d42SMatheus Ferst     /* Machine check exception */
181537e62398SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_MCK) {
1816a9899d42SMatheus Ferst         return PPC_INTERRUPT_MCK;
1817a9899d42SMatheus Ferst     }
1818a9899d42SMatheus Ferst 
1819a9899d42SMatheus Ferst     /* Hypervisor decrementer exception */
182037e62398SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HDECR) {
1821a9899d42SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
182237e62398SHarsh Prateek Bora         bool hdice = !!(lpcr & LPCR_HDICE);
182364a9b5eeSMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1824a9899d42SMatheus Ferst             /* HDEC clears on delivery */
1825a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1826a9899d42SMatheus Ferst         }
1827a9899d42SMatheus Ferst     }
1828a9899d42SMatheus Ferst 
1829a9899d42SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
183037e62398SHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_EXT) {
183137e62398SHarsh Prateek Bora         bool lpes0 = !!(lpcr & LPCR_LPES0);
183237e62398SHarsh Prateek Bora         bool heic = !!(lpcr & LPCR_HEIC);
1833a9899d42SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
183464a9b5eeSMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1835a9899d42SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1836a9899d42SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1837a9899d42SMatheus Ferst             return PPC_INTERRUPT_EXT;
1838a9899d42SMatheus Ferst         }
1839a9899d42SMatheus Ferst     }
184064a9b5eeSMatheus Ferst     if (msr_ee != 0) {
1841a9899d42SMatheus Ferst         /* Decrementer exception */
184237e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DECR) {
1843a9899d42SMatheus Ferst             return PPC_INTERRUPT_DECR;
1844a9899d42SMatheus Ferst         }
184537e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1846a9899d42SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
1847a9899d42SMatheus Ferst         }
184837e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
1849a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
1850a9899d42SMatheus Ferst         }
185137e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_PERFM) {
1852a9899d42SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1853a9899d42SMatheus Ferst         }
1854a9899d42SMatheus Ferst         /* EBB exception */
185537e62398SHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_EBB) {
1856a9899d42SMatheus Ferst             /*
1857a9899d42SMatheus Ferst              * EBB exception must be taken in problem state and
1858a9899d42SMatheus Ferst              * with BESCR_GE set.
1859a9899d42SMatheus Ferst              */
1860a9899d42SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
1861a9899d42SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
1862a9899d42SMatheus Ferst                 return PPC_INTERRUPT_EBB;
1863a9899d42SMatheus Ferst             }
1864a9899d42SMatheus Ferst         }
1865a9899d42SMatheus Ferst     }
1866a9899d42SMatheus Ferst 
1867a9899d42SMatheus Ferst     return 0;
1868a9899d42SMatheus Ferst }
1869a9899d42SMatheus Ferst 
1870b00e9a2fSMatheus Ferst #define P9_UNUSED_INTERRUPTS \
1871b00e9a2fSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT |   \
1872b00e9a2fSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |  \
1873b00e9a2fSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1874b00e9a2fSMatheus Ferst 
p9_interrupt_powersave(CPUPPCState * env,uint32_t pending_interrupts,target_ulong lpcr)18752a05a63cSHarsh Prateek Bora static int p9_interrupt_powersave(CPUPPCState *env,
18762a05a63cSHarsh Prateek Bora                                   uint32_t pending_interrupts,
18772a05a63cSHarsh Prateek Bora                                   target_ulong lpcr)
18789c713713SMatheus Ferst {
18792a05a63cSHarsh Prateek Bora 
18809c713713SMatheus Ferst     /* External Exception */
18812a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_EXT) &&
18822a05a63cSHarsh Prateek Bora         (lpcr & LPCR_EEE)) {
18832a05a63cSHarsh Prateek Bora         bool heic = !!(lpcr & LPCR_HEIC);
18849c713713SMatheus Ferst         if (!heic || !FIELD_EX64_HV(env->msr) ||
18859c713713SMatheus Ferst             FIELD_EX64(env->msr, MSR, PR)) {
18869c713713SMatheus Ferst             return PPC_INTERRUPT_EXT;
18879c713713SMatheus Ferst         }
18889c713713SMatheus Ferst     }
18899c713713SMatheus Ferst     /* Decrementer Exception */
18902a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DECR) &&
18912a05a63cSHarsh Prateek Bora         (lpcr & LPCR_DEE)) {
18929c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
18939c713713SMatheus Ferst     }
18949c713713SMatheus Ferst     /* Machine Check or Hypervisor Maintenance Exception */
18952a05a63cSHarsh Prateek Bora     if (lpcr & LPCR_OEE) {
18962a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_MCK) {
18979c713713SMatheus Ferst             return PPC_INTERRUPT_MCK;
18989c713713SMatheus Ferst         }
18992a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_HMI) {
19009c713713SMatheus Ferst             return PPC_INTERRUPT_HMI;
19019c713713SMatheus Ferst         }
19029c713713SMatheus Ferst     }
19039c713713SMatheus Ferst     /* Privileged Doorbell Exception */
19042a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
19052a05a63cSHarsh Prateek Bora         (lpcr & LPCR_PDEE)) {
19069c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
19079c713713SMatheus Ferst     }
19089c713713SMatheus Ferst     /* Hypervisor Doorbell Exception */
19092a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
19102a05a63cSHarsh Prateek Bora         (lpcr & LPCR_HDEE)) {
19119c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
19129c713713SMatheus Ferst     }
19139c713713SMatheus Ferst     /* Hypervisor virtualization exception */
19142a05a63cSHarsh Prateek Bora     if ((pending_interrupts & PPC_INTERRUPT_HVIRT) &&
19152a05a63cSHarsh Prateek Bora         (lpcr & LPCR_HVEE)) {
19169c713713SMatheus Ferst         return PPC_INTERRUPT_HVIRT;
19179c713713SMatheus Ferst     }
19182a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_RESET) {
19199c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
19209c713713SMatheus Ferst     }
19219c713713SMatheus Ferst     return 0;
19229c713713SMatheus Ferst }
19239c713713SMatheus Ferst 
p9_next_unmasked_interrupt(CPUPPCState * env,uint32_t pending_interrupts,target_ulong lpcr)19242a05a63cSHarsh Prateek Bora static int p9_next_unmasked_interrupt(CPUPPCState *env,
19252a05a63cSHarsh Prateek Bora                                       uint32_t pending_interrupts,
19262a05a63cSHarsh Prateek Bora                                       target_ulong lpcr)
19272dfecf01SMatheus Ferst {
19284e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
19294e6b7db2SBALATON Zoltan 
193027796411SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
193127796411SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
19322dfecf01SMatheus Ferst 
19332a05a63cSHarsh Prateek Bora     assert((pending_interrupts & P9_UNUSED_INTERRUPTS) == 0);
1934b00e9a2fSMatheus Ferst 
193527796411SMatheus Ferst     if (cs->halted) {
193627796411SMatheus Ferst         if (env->spr[SPR_PSSCR] & PSSCR_EC) {
193727796411SMatheus Ferst             /*
193827796411SMatheus Ferst              * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can
193927796411SMatheus Ferst              * wakeup the processor
194027796411SMatheus Ferst              */
19412a05a63cSHarsh Prateek Bora             return p9_interrupt_powersave(env, pending_interrupts, lpcr);
194227796411SMatheus Ferst         } else {
194327796411SMatheus Ferst             /*
194427796411SMatheus Ferst              * When it's clear, any system-caused exception exits power-saving
194527796411SMatheus Ferst              * mode, even the ones that gate on MSR[EE].
194627796411SMatheus Ferst              */
194727796411SMatheus Ferst             msr_ee = true;
194827796411SMatheus Ferst         }
194927796411SMatheus Ferst     }
195027796411SMatheus Ferst 
19512dfecf01SMatheus Ferst     /* Machine check exception */
19522a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_MCK) {
19532dfecf01SMatheus Ferst         return PPC_INTERRUPT_MCK;
19542dfecf01SMatheus Ferst     }
19552dfecf01SMatheus Ferst 
19562dfecf01SMatheus Ferst     /* Hypervisor decrementer exception */
19572a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HDECR) {
19582dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
19592a05a63cSHarsh Prateek Bora         bool hdice = !!(lpcr & LPCR_HDICE);
196027796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
19612dfecf01SMatheus Ferst             /* HDEC clears on delivery */
19622dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDECR;
19632dfecf01SMatheus Ferst         }
19642dfecf01SMatheus Ferst     }
19652dfecf01SMatheus Ferst 
19662dfecf01SMatheus Ferst     /* Hypervisor virtualization interrupt */
19672a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HVIRT) {
19682dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
19692a05a63cSHarsh Prateek Bora         bool hvice = !!(lpcr & LPCR_HVICE);
197027796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hvice) {
19712dfecf01SMatheus Ferst             return PPC_INTERRUPT_HVIRT;
19722dfecf01SMatheus Ferst         }
19732dfecf01SMatheus Ferst     }
19742dfecf01SMatheus Ferst 
19752dfecf01SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
19762a05a63cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_EXT) {
19772a05a63cSHarsh Prateek Bora         bool lpes0 = !!(lpcr & LPCR_LPES0);
19782a05a63cSHarsh Prateek Bora         bool heic = !!(lpcr & LPCR_HEIC);
19792dfecf01SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
198027796411SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
19812dfecf01SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
19822dfecf01SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
19832dfecf01SMatheus Ferst             return PPC_INTERRUPT_EXT;
19842dfecf01SMatheus Ferst         }
19852dfecf01SMatheus Ferst     }
198627796411SMatheus Ferst     if (msr_ee != 0) {
19872dfecf01SMatheus Ferst         /* Decrementer exception */
19882a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DECR) {
19892dfecf01SMatheus Ferst             return PPC_INTERRUPT_DECR;
19902dfecf01SMatheus Ferst         }
19912a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DOORBELL) {
19922dfecf01SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
19932dfecf01SMatheus Ferst         }
19942a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
19952dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
19962dfecf01SMatheus Ferst         }
19972a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_PERFM) {
19982dfecf01SMatheus Ferst             return PPC_INTERRUPT_PERFM;
19992dfecf01SMatheus Ferst         }
20002dfecf01SMatheus Ferst         /* EBB exception */
20012a05a63cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_EBB) {
20022dfecf01SMatheus Ferst             /*
20032dfecf01SMatheus Ferst              * EBB exception must be taken in problem state and
20042dfecf01SMatheus Ferst              * with BESCR_GE set.
20052dfecf01SMatheus Ferst              */
20062dfecf01SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
20072dfecf01SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
20082dfecf01SMatheus Ferst                 return PPC_INTERRUPT_EBB;
20092dfecf01SMatheus Ferst             }
20102dfecf01SMatheus Ferst         }
20112dfecf01SMatheus Ferst     }
20122dfecf01SMatheus Ferst 
20132dfecf01SMatheus Ferst     return 0;
20142dfecf01SMatheus Ferst }
2015f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
20162dfecf01SMatheus Ferst 
ppc_next_unmasked_interrupt(CPUPPCState * env)2017868cb6baSBALATON Zoltan static int ppc_next_unmasked_interrupt(CPUPPCState *env)
2018fcf5ef2aSThomas Huth {
2019bb547c4cSHarsh Prateek Bora     uint32_t pending_interrupts = env->pending_interrupts;
2020bb547c4cSHarsh Prateek Bora     target_ulong lpcr = env->spr[SPR_LPCR];
2021bb547c4cSHarsh Prateek Bora     bool async_deliver;
2022bb547c4cSHarsh Prateek Bora 
2023868cb6baSBALATON Zoltan #ifdef TARGET_PPC64
2024868cb6baSBALATON Zoltan     switch (env->excp_model) {
2025868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER7:
2026bb547c4cSHarsh Prateek Bora         return p7_next_unmasked_interrupt(env, pending_interrupts, lpcr);
2027868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER8:
2028bb547c4cSHarsh Prateek Bora         return p8_next_unmasked_interrupt(env, pending_interrupts, lpcr);
2029868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER9:
2030868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER10:
2031c0d96407SAditya Gupta     case POWERPC_EXCP_POWER11:
2032bb547c4cSHarsh Prateek Bora         return p9_next_unmasked_interrupt(env, pending_interrupts, lpcr);
2033868cb6baSBALATON Zoltan     default:
2034868cb6baSBALATON Zoltan         break;
2035868cb6baSBALATON Zoltan     }
2036868cb6baSBALATON Zoltan #endif
2037fcf5ef2aSThomas Huth 
2038fcf5ef2aSThomas Huth     /* External reset */
2039bb547c4cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_RESET) {
2040de76b85cSMatheus Ferst         return PPC_INTERRUPT_RESET;
2041fcf5ef2aSThomas Huth     }
2042fcf5ef2aSThomas Huth     /* Machine check exception */
2043bb547c4cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_MCK) {
2044de76b85cSMatheus Ferst         return PPC_INTERRUPT_MCK;
2045fcf5ef2aSThomas Huth     }
2046fcf5ef2aSThomas Huth #if 0 /* TODO */
2047fcf5ef2aSThomas Huth     /* External debug exception */
2048f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) {
2049de76b85cSMatheus Ferst         return PPC_INTERRUPT_DEBUG;
2050fcf5ef2aSThomas Huth     }
2051fcf5ef2aSThomas Huth #endif
20523621e2c9SBenjamin Herrenschmidt 
20533621e2c9SBenjamin Herrenschmidt     /*
20543621e2c9SBenjamin Herrenschmidt      * For interrupts that gate on MSR:EE, we need to do something a
20553621e2c9SBenjamin Herrenschmidt      * bit more subtle, as we need to let them through even when EE is
20563621e2c9SBenjamin Herrenschmidt      * clear when coming out of some power management states (in order
20573621e2c9SBenjamin Herrenschmidt      * for them to become a 0x100).
20583621e2c9SBenjamin Herrenschmidt      */
20590939b8f8SVíctor Colombo     async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
20603621e2c9SBenjamin Herrenschmidt 
2061fcf5ef2aSThomas Huth     /* Hypervisor decrementer exception */
2062bb547c4cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HDECR) {
2063fcf5ef2aSThomas Huth         /* LPCR will be clear when not supported so this will work */
2064bb547c4cSHarsh Prateek Bora         bool hdice = !!(lpcr & LPCR_HDICE);
20659de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
2066fcf5ef2aSThomas Huth             /* HDEC clears on delivery */
2067de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDECR;
2068fcf5ef2aSThomas Huth         }
2069fcf5ef2aSThomas Huth     }
2070d8ce5fd6SBenjamin Herrenschmidt 
2071d8ce5fd6SBenjamin Herrenschmidt     /* Hypervisor virtualization interrupt */
2072bb547c4cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_HVIRT) {
2073d8ce5fd6SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
2074bb547c4cSHarsh Prateek Bora         bool hvice = !!(lpcr & LPCR_HVICE);
20759de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
2076de76b85cSMatheus Ferst             return PPC_INTERRUPT_HVIRT;
2077d8ce5fd6SBenjamin Herrenschmidt         }
2078d8ce5fd6SBenjamin Herrenschmidt     }
2079d8ce5fd6SBenjamin Herrenschmidt 
2080d8ce5fd6SBenjamin Herrenschmidt     /* External interrupt can ignore MSR:EE under some circumstances */
2081bb547c4cSHarsh Prateek Bora     if (pending_interrupts & PPC_INTERRUPT_EXT) {
2082bb547c4cSHarsh Prateek Bora         bool lpes0 = !!(lpcr & LPCR_LPES0);
2083bb547c4cSHarsh Prateek Bora         bool heic = !!(lpcr & LPCR_HEIC);
20846eebe6dcSBenjamin Herrenschmidt         /* HEIC blocks delivery to the hypervisor */
20859de754d3SVíctor Colombo         if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
2086d41ccf6eSVíctor Colombo             !FIELD_EX64(env->msr, MSR, PR))) ||
20879de754d3SVíctor Colombo             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
2088de76b85cSMatheus Ferst             return PPC_INTERRUPT_EXT;
2089fcf5ef2aSThomas Huth         }
2090fcf5ef2aSThomas Huth     }
2091acc861c2SVíctor Colombo     if (FIELD_EX64(env->msr, MSR, CE)) {
2092fcf5ef2aSThomas Huth         /* External critical interrupt */
2093bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_CEXT) {
2094de76b85cSMatheus Ferst             return PPC_INTERRUPT_CEXT;
2095fcf5ef2aSThomas Huth         }
2096fcf5ef2aSThomas Huth     }
20973621e2c9SBenjamin Herrenschmidt     if (async_deliver != 0) {
2098fcf5ef2aSThomas Huth         /* Watchdog timer on embedded PowerPC */
2099bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_WDT) {
2100de76b85cSMatheus Ferst             return PPC_INTERRUPT_WDT;
2101fcf5ef2aSThomas Huth         }
2102bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_CDOORBELL) {
2103de76b85cSMatheus Ferst             return PPC_INTERRUPT_CDOORBELL;
2104fcf5ef2aSThomas Huth         }
2105fcf5ef2aSThomas Huth         /* Fixed interval timer on embedded PowerPC */
2106bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_FIT) {
2107de76b85cSMatheus Ferst             return PPC_INTERRUPT_FIT;
2108fcf5ef2aSThomas Huth         }
2109fcf5ef2aSThomas Huth         /* Programmable interval timer on embedded PowerPC */
2110bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_PIT) {
2111de76b85cSMatheus Ferst             return PPC_INTERRUPT_PIT;
2112fcf5ef2aSThomas Huth         }
2113fcf5ef2aSThomas Huth         /* Decrementer exception */
2114bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DECR) {
2115de76b85cSMatheus Ferst             return PPC_INTERRUPT_DECR;
2116fcf5ef2aSThomas Huth         }
2117bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2118de76b85cSMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
2119fcf5ef2aSThomas Huth         }
2120bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
2121de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
21227af1e7b0SCédric Le Goater         }
2123bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_PERFM) {
2124de76b85cSMatheus Ferst             return PPC_INTERRUPT_PERFM;
2125fcf5ef2aSThomas Huth         }
2126fcf5ef2aSThomas Huth         /* Thermal interrupt */
2127bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_THERM) {
2128de76b85cSMatheus Ferst             return PPC_INTERRUPT_THERM;
2129fcf5ef2aSThomas Huth         }
2130cb76bbc4SDaniel Henrique Barboza         /* EBB exception */
2131bb547c4cSHarsh Prateek Bora         if (pending_interrupts & PPC_INTERRUPT_EBB) {
2132cb76bbc4SDaniel Henrique Barboza             /*
2133cb76bbc4SDaniel Henrique Barboza              * EBB exception must be taken in problem state and
2134cb76bbc4SDaniel Henrique Barboza              * with BESCR_GE set.
2135cb76bbc4SDaniel Henrique Barboza              */
2136d41ccf6eSVíctor Colombo             if (FIELD_EX64(env->msr, MSR, PR) &&
2137d41ccf6eSVíctor Colombo                 (env->spr[SPR_BESCR] & BESCR_GE)) {
2138de76b85cSMatheus Ferst                 return PPC_INTERRUPT_EBB;
2139de76b85cSMatheus Ferst             }
2140de76b85cSMatheus Ferst         }
2141de76b85cSMatheus Ferst     }
2142cb76bbc4SDaniel Henrique Barboza 
2143de76b85cSMatheus Ferst     return 0;
2144de76b85cSMatheus Ferst }
2145de76b85cSMatheus Ferst 
21462fdedcbcSMatheus Ferst /*
21472fdedcbcSMatheus Ferst  * Sets CPU_INTERRUPT_HARD if there is at least one unmasked interrupt to be
21482fdedcbcSMatheus Ferst  * delivered and clears CPU_INTERRUPT_HARD otherwise.
21492fdedcbcSMatheus Ferst  *
21502fdedcbcSMatheus Ferst  * This method is called by ppc_set_interrupt when an interrupt is raised or
21512fdedcbcSMatheus Ferst  * lowered, and should also be called whenever an interrupt masking condition
21522fdedcbcSMatheus Ferst  * is changed, e.g.:
21532fdedcbcSMatheus Ferst  *  - When relevant bits of MSR are altered, like EE, HV, PR, etc.;
21542fdedcbcSMatheus Ferst  *  - When relevant bits of LPCR are altered, like PECE, HDICE, HVICE, etc.;
21552fdedcbcSMatheus Ferst  *  - When PSSCR[EC] or env->resume_as_sreset are changed;
21562fdedcbcSMatheus Ferst  *  - When cs->halted is changed and the CPU has a different interrupt masking
21572fdedcbcSMatheus Ferst  *    logic in power-saving mode (e.g., POWER7/8/9/10);
21582fdedcbcSMatheus Ferst  */
ppc_maybe_interrupt(CPUPPCState * env)21592fdedcbcSMatheus Ferst void ppc_maybe_interrupt(CPUPPCState *env)
21602fdedcbcSMatheus Ferst {
21612fdedcbcSMatheus Ferst     CPUState *cs = env_cpu(env);
216232ead8e6SStefan Hajnoczi     BQL_LOCK_GUARD();
21632fdedcbcSMatheus Ferst 
21642fdedcbcSMatheus Ferst     if (ppc_next_unmasked_interrupt(env)) {
21652fdedcbcSMatheus Ferst         cpu_interrupt(cs, CPU_INTERRUPT_HARD);
21662fdedcbcSMatheus Ferst     } else {
21672fdedcbcSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
21682fdedcbcSMatheus Ferst     }
21692fdedcbcSMatheus Ferst }
21702fdedcbcSMatheus Ferst 
2171f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
p7_deliver_interrupt(CPUPPCState * env,int interrupt)2172d93a4856SMatheus Ferst static void p7_deliver_interrupt(CPUPPCState *env, int interrupt)
2173d93a4856SMatheus Ferst {
2174d93a4856SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2175d93a4856SMatheus Ferst 
2176d93a4856SMatheus Ferst     switch (interrupt) {
2177d93a4856SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2178d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2179d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2180d93a4856SMatheus Ferst         break;
2181d93a4856SMatheus Ferst 
2182d93a4856SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2183d93a4856SMatheus Ferst         /* HDEC clears on delivery */
2184d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2185d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2186d93a4856SMatheus Ferst         break;
2187d93a4856SMatheus Ferst 
2188d93a4856SMatheus Ferst     case PPC_INTERRUPT_EXT:
2189d93a4856SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2190d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2191d93a4856SMatheus Ferst         } else {
2192d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2193d93a4856SMatheus Ferst         }
2194d93a4856SMatheus Ferst         break;
2195d93a4856SMatheus Ferst 
2196d93a4856SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2197d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2198d93a4856SMatheus Ferst         break;
2199d93a4856SMatheus Ferst     case PPC_INTERRUPT_PERFM:
2200d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2201d93a4856SMatheus Ferst         break;
2202d93a4856SMatheus Ferst     case 0:
2203d93a4856SMatheus Ferst         /*
2204d93a4856SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
2205d93a4856SMatheus Ferst          * anything to deliver while in a PM state that requires getting
2206d93a4856SMatheus Ferst          * out via a 0x100
2207d93a4856SMatheus Ferst          *
2208d93a4856SMatheus Ferst          * This means we will incorrectly execute past the power management
2209d93a4856SMatheus Ferst          * instruction instead of triggering a reset.
2210d93a4856SMatheus Ferst          *
2211d93a4856SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
2212d93a4856SMatheus Ferst          * processor has_work implementation and the logic in this function.
2213d93a4856SMatheus Ferst          */
2214d93a4856SMatheus Ferst         assert(!env->resume_as_sreset);
2215d93a4856SMatheus Ferst         break;
2216d93a4856SMatheus Ferst     default:
2217bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2218bc30c1c6SBALATON Zoltan                   interrupt);
2219d93a4856SMatheus Ferst     }
2220d93a4856SMatheus Ferst }
2221d93a4856SMatheus Ferst 
p8_deliver_interrupt(CPUPPCState * env,int interrupt)22226527e757SMatheus Ferst static void p8_deliver_interrupt(CPUPPCState *env, int interrupt)
22236527e757SMatheus Ferst {
22246527e757SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
22256527e757SMatheus Ferst 
22266527e757SMatheus Ferst     switch (interrupt) {
22276527e757SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
22286527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
22296527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
22306527e757SMatheus Ferst         break;
22316527e757SMatheus Ferst 
22326527e757SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
22336527e757SMatheus Ferst         /* HDEC clears on delivery */
22346527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
22356527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
22366527e757SMatheus Ferst         break;
22376527e757SMatheus Ferst 
22386527e757SMatheus Ferst     case PPC_INTERRUPT_EXT:
22396527e757SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
22406527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
22416527e757SMatheus Ferst         } else {
22426527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
22436527e757SMatheus Ferst         }
22446527e757SMatheus Ferst         break;
22456527e757SMatheus Ferst 
22466527e757SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
22476527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
22486527e757SMatheus Ferst         break;
22496527e757SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
225006229545SNicholas Piggin         if (!env->resume_as_sreset) {
22516527e757SMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
225206229545SNicholas Piggin         }
22536527e757SMatheus Ferst         if (is_book3s_arch2x(env)) {
22546527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
22556527e757SMatheus Ferst         } else {
22566527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
22576527e757SMatheus Ferst         }
22586527e757SMatheus Ferst         break;
22596527e757SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
226006229545SNicholas Piggin         if (!env->resume_as_sreset) {
22616527e757SMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
226206229545SNicholas Piggin         }
22636527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
22646527e757SMatheus Ferst         break;
22656527e757SMatheus Ferst     case PPC_INTERRUPT_PERFM:
22666527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
22676527e757SMatheus Ferst         break;
22686527e757SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
22696527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
22706527e757SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
22716527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
22726527e757SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
22736527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
22746527e757SMatheus Ferst         }
22756527e757SMatheus Ferst         break;
22766527e757SMatheus Ferst     case 0:
22776527e757SMatheus Ferst         /*
22786527e757SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
22796527e757SMatheus Ferst          * anything to deliver while in a PM state that requires getting
22806527e757SMatheus Ferst          * out via a 0x100
22816527e757SMatheus Ferst          *
22826527e757SMatheus Ferst          * This means we will incorrectly execute past the power management
22836527e757SMatheus Ferst          * instruction instead of triggering a reset.
22846527e757SMatheus Ferst          *
22856527e757SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
22866527e757SMatheus Ferst          * processor has_work implementation and the logic in this function.
22876527e757SMatheus Ferst          */
22886527e757SMatheus Ferst         assert(!env->resume_as_sreset);
22896527e757SMatheus Ferst         break;
22906527e757SMatheus Ferst     default:
2291bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2292bc30c1c6SBALATON Zoltan                   interrupt);
22936527e757SMatheus Ferst     }
22946527e757SMatheus Ferst }
22956527e757SMatheus Ferst 
p9_deliver_interrupt(CPUPPCState * env,int interrupt)22963654e238SMatheus Ferst static void p9_deliver_interrupt(CPUPPCState *env, int interrupt)
22973654e238SMatheus Ferst {
22983654e238SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
22993654e238SMatheus Ferst     CPUState *cs = env_cpu(env);
23003654e238SMatheus Ferst 
230127796411SMatheus Ferst     if (cs->halted && !(env->spr[SPR_PSSCR] & PSSCR_EC) &&
230227796411SMatheus Ferst         !FIELD_EX64(env->msr, MSR, EE)) {
230327796411SMatheus Ferst         /*
230427796411SMatheus Ferst          * A pending interrupt took us out of power-saving, but MSR[EE] says
230527796411SMatheus Ferst          * that we should return to NIP+4 instead of delivering it.
230627796411SMatheus Ferst          */
230727796411SMatheus Ferst         return;
230827796411SMatheus Ferst     }
230927796411SMatheus Ferst 
23103654e238SMatheus Ferst     switch (interrupt) {
23113654e238SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
23123654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
23133654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
23143654e238SMatheus Ferst         break;
23153654e238SMatheus Ferst 
23163654e238SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
23173654e238SMatheus Ferst         /* HDEC clears on delivery */
231806229545SNicholas Piggin         /* XXX: should not see an HDEC if resume_as_sreset. assert? */
23193654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
23203654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
23213654e238SMatheus Ferst         break;
23223654e238SMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
23233654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
23243654e238SMatheus Ferst         break;
23253654e238SMatheus Ferst 
23263654e238SMatheus Ferst     case PPC_INTERRUPT_EXT:
23273654e238SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
23283654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
23293654e238SMatheus Ferst         } else {
23303654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
23313654e238SMatheus Ferst         }
23323654e238SMatheus Ferst         break;
23333654e238SMatheus Ferst 
23343654e238SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
23353654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
23363654e238SMatheus Ferst         break;
23373654e238SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
233806229545SNicholas Piggin         if (!env->resume_as_sreset) {
23393654e238SMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
234006229545SNicholas Piggin         }
23413654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
23423654e238SMatheus Ferst         break;
23433654e238SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
234406229545SNicholas Piggin         if (!env->resume_as_sreset) {
23453654e238SMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
234606229545SNicholas Piggin         }
23473654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
23483654e238SMatheus Ferst         break;
23493654e238SMatheus Ferst     case PPC_INTERRUPT_PERFM:
23503654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
23513654e238SMatheus Ferst         break;
23523654e238SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
23533654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
23543654e238SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
23553654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
23563654e238SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
23573654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
23583654e238SMatheus Ferst         }
23593654e238SMatheus Ferst         break;
23603654e238SMatheus Ferst     case 0:
23613654e238SMatheus Ferst         /*
23623654e238SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
23633654e238SMatheus Ferst          * anything to deliver while in a PM state that requires getting
23643654e238SMatheus Ferst          * out via a 0x100
23653654e238SMatheus Ferst          *
23663654e238SMatheus Ferst          * This means we will incorrectly execute past the power management
23673654e238SMatheus Ferst          * instruction instead of triggering a reset.
23683654e238SMatheus Ferst          *
23693654e238SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
23703654e238SMatheus Ferst          * processor has_work implementation and the logic in this function.
23713654e238SMatheus Ferst          */
23723654e238SMatheus Ferst         assert(!env->resume_as_sreset);
23733654e238SMatheus Ferst         break;
23743654e238SMatheus Ferst     default:
2375bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2376bc30c1c6SBALATON Zoltan                   interrupt);
23773654e238SMatheus Ferst     }
23783654e238SMatheus Ferst }
2379f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
23803654e238SMatheus Ferst 
ppc_deliver_interrupt(CPUPPCState * env,int interrupt)2381868cb6baSBALATON Zoltan static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt)
2382de76b85cSMatheus Ferst {
2383868cb6baSBALATON Zoltan #ifdef TARGET_PPC64
2384868cb6baSBALATON Zoltan     switch (env->excp_model) {
2385868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER7:
2386868cb6baSBALATON Zoltan         return p7_deliver_interrupt(env, interrupt);
2387868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER8:
2388868cb6baSBALATON Zoltan         return p8_deliver_interrupt(env, interrupt);
2389868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER9:
2390868cb6baSBALATON Zoltan     case POWERPC_EXCP_POWER10:
2391c0d96407SAditya Gupta     case POWERPC_EXCP_POWER11:
2392868cb6baSBALATON Zoltan         return p9_deliver_interrupt(env, interrupt);
2393868cb6baSBALATON Zoltan     default:
2394868cb6baSBALATON Zoltan         break;
2395868cb6baSBALATON Zoltan     }
2396de76b85cSMatheus Ferst #endif
2397de76b85cSMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2398de76b85cSMatheus Ferst 
2399de76b85cSMatheus Ferst     switch (interrupt) {
2400de76b85cSMatheus Ferst     case PPC_INTERRUPT_RESET: /* External reset */
2401de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_RESET;
2402de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_RESET);
2403de76b85cSMatheus Ferst         break;
2404de76b85cSMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2405de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2406de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2407de76b85cSMatheus Ferst         break;
2408de76b85cSMatheus Ferst 
2409de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2410de76b85cSMatheus Ferst         /* HDEC clears on delivery */
2411de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2412de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2413de76b85cSMatheus Ferst         break;
2414de76b85cSMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
2415de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2416de76b85cSMatheus Ferst         break;
2417de76b85cSMatheus Ferst 
2418de76b85cSMatheus Ferst     case PPC_INTERRUPT_EXT:
2419de76b85cSMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2420de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2421de76b85cSMatheus Ferst         } else {
2422de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2423de76b85cSMatheus Ferst         }
2424de76b85cSMatheus Ferst         break;
2425de76b85cSMatheus Ferst     case PPC_INTERRUPT_CEXT: /* External critical interrupt */
2426de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_CRITICAL);
2427de76b85cSMatheus Ferst         break;
2428de76b85cSMatheus Ferst 
2429de76b85cSMatheus Ferst     case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */
2430de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_WDT;
2431de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_WDT);
2432de76b85cSMatheus Ferst         break;
2433de76b85cSMatheus Ferst     case PPC_INTERRUPT_CDOORBELL:
2434de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL;
2435de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DOORCI);
2436de76b85cSMatheus Ferst         break;
2437de76b85cSMatheus Ferst     case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */
2438de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_FIT;
2439de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_FIT);
2440de76b85cSMatheus Ferst         break;
2441de76b85cSMatheus Ferst     case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */
2442de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PIT;
2443de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PIT);
2444de76b85cSMatheus Ferst         break;
2445de76b85cSMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2446de76b85cSMatheus Ferst         if (ppc_decr_clear_on_delivery(env)) {
2447de76b85cSMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DECR;
2448de76b85cSMatheus Ferst         }
2449de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2450de76b85cSMatheus Ferst         break;
2451de76b85cSMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
2452de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
2453de76b85cSMatheus Ferst         if (is_book3s_arch2x(env)) {
2454de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
2455de76b85cSMatheus Ferst         } else {
2456de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
2457de76b85cSMatheus Ferst         }
2458de76b85cSMatheus Ferst         break;
2459de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
2460de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
2461de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
2462de76b85cSMatheus Ferst         break;
2463de76b85cSMatheus Ferst     case PPC_INTERRUPT_PERFM:
2464de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2465de76b85cSMatheus Ferst         break;
2466de76b85cSMatheus Ferst     case PPC_INTERRUPT_THERM:  /* Thermal interrupt */
2467de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_THERM;
2468de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_THERM);
2469de76b85cSMatheus Ferst         break;
2470de76b85cSMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
2471de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
2472cb76bbc4SDaniel Henrique Barboza         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
2473cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
2474cb76bbc4SDaniel Henrique Barboza         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
2475cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
2476cb76bbc4SDaniel Henrique Barboza         }
2477de76b85cSMatheus Ferst         break;
2478de76b85cSMatheus Ferst     case 0:
2479f8154fd2SBenjamin Herrenschmidt         /*
2480f8154fd2SBenjamin Herrenschmidt          * This is a bug ! It means that has_work took us out of halt without
2481f8154fd2SBenjamin Herrenschmidt          * anything to deliver while in a PM state that requires getting
2482f8154fd2SBenjamin Herrenschmidt          * out via a 0x100
2483f8154fd2SBenjamin Herrenschmidt          *
2484f8154fd2SBenjamin Herrenschmidt          * This means we will incorrectly execute past the power management
2485f8154fd2SBenjamin Herrenschmidt          * instruction instead of triggering a reset.
2486f8154fd2SBenjamin Herrenschmidt          *
2487136fbf65Szhaolichang          * It generally means a discrepancy between the wakeup conditions in the
2488f8154fd2SBenjamin Herrenschmidt          * processor has_work implementation and the logic in this function.
2489f8154fd2SBenjamin Herrenschmidt          */
2490de76b85cSMatheus Ferst         assert(!env->resume_as_sreset);
2491de76b85cSMatheus Ferst         break;
2492de76b85cSMatheus Ferst     default:
2493bc30c1c6SBALATON Zoltan         cpu_abort(env_cpu(env), "Invalid PowerPC interrupt %d. Aborting\n",
2494bc30c1c6SBALATON Zoltan                   interrupt);
2495ba2898f7SMatheus Ferst     }
2496ba2898f7SMatheus Ferst }
2497ba2898f7SMatheus Ferst 
2498*fa416ae6SNicholas Piggin /*
2499*fa416ae6SNicholas Piggin  * system reset is not delivered via normal irq method, so have to set
2500*fa416ae6SNicholas Piggin  * halted = 0 to resume CPU running if it was halted. Possibly we should
2501*fa416ae6SNicholas Piggin  * move it over to using PPC_INTERRUPT_RESET rather than async_run_on_cpu.
2502*fa416ae6SNicholas Piggin  */
ppc_cpu_do_system_reset(CPUState * cs)2503b5b7f391SNicholas Piggin void ppc_cpu_do_system_reset(CPUState *cs)
2504fcf5ef2aSThomas Huth {
2505fcf5ef2aSThomas Huth     PowerPCCPU *cpu = POWERPC_CPU(cs);
2506fcf5ef2aSThomas Huth 
2507*fa416ae6SNicholas Piggin     cs->halted = 0;
250893130c84SFabiano Rosas     powerpc_excp(cpu, POWERPC_EXCP_RESET);
2509fcf5ef2aSThomas Huth }
2510ad77c6caSNicholas Piggin 
ppc_cpu_do_fwnmi_machine_check(CPUState * cs,target_ulong vector)2511ad77c6caSNicholas Piggin void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector)
2512ad77c6caSNicholas Piggin {
2513ad77c6caSNicholas Piggin     PowerPCCPU *cpu = POWERPC_CPU(cs);
2514ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
2515ad77c6caSNicholas Piggin     target_ulong msr = 0;
2516ad77c6caSNicholas Piggin 
2517ad77c6caSNicholas Piggin     /*
2518ad77c6caSNicholas Piggin      * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already
2519ad77c6caSNicholas Piggin      * been set by KVM.
2520ad77c6caSNicholas Piggin      */
2521ad77c6caSNicholas Piggin     msr = (1ULL << MSR_ME);
2522ad77c6caSNicholas Piggin     msr |= env->msr & (1ULL << MSR_SF);
2523516fc103SFabiano Rosas     if (ppc_interrupts_little_endian(cpu, false)) {
2524ad77c6caSNicholas Piggin         msr |= (1ULL << MSR_LE);
2525ad77c6caSNicholas Piggin     }
2526ad77c6caSNicholas Piggin 
25277cebc5dbSNicholas Piggin     /* Anything for nested required here? MSR[HV] bit? */
25287cebc5dbSNicholas Piggin 
2529*fa416ae6SNicholas Piggin     cs->halted = 0;
2530ad77c6caSNicholas Piggin     powerpc_set_excp_state(cpu, vector, msr);
2531ad77c6caSNicholas Piggin }
2532fcf5ef2aSThomas Huth 
ppc_cpu_exec_interrupt(CPUState * cs,int interrupt_request)2533fcf5ef2aSThomas Huth bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
2534fcf5ef2aSThomas Huth {
2535794511bcSPhilippe Mathieu-Daudé     CPUPPCState *env = cpu_env(cs);
2536de76b85cSMatheus Ferst     int interrupt;
2537fcf5ef2aSThomas Huth 
2538de76b85cSMatheus Ferst     if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) {
2539de76b85cSMatheus Ferst         return false;
2540de76b85cSMatheus Ferst     }
2541de76b85cSMatheus Ferst 
2542de76b85cSMatheus Ferst     interrupt = ppc_next_unmasked_interrupt(env);
2543de76b85cSMatheus Ferst     if (interrupt == 0) {
2544de76b85cSMatheus Ferst         return false;
2545de76b85cSMatheus Ferst     }
2546de76b85cSMatheus Ferst 
2547de76b85cSMatheus Ferst     ppc_deliver_interrupt(env, interrupt);
2548fcf5ef2aSThomas Huth     if (env->pending_interrupts == 0) {
2549de76b85cSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
2550fcf5ef2aSThomas Huth     }
2551fcf5ef2aSThomas Huth     return true;
2552fcf5ef2aSThomas Huth }
2553fcf5ef2aSThomas Huth 
2554f725245cSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
2555f725245cSPhilippe Mathieu-Daudé 
2556fcf5ef2aSThomas Huth /*****************************************************************************/
2557fcf5ef2aSThomas Huth /* Exceptions processing helpers */
2558fcf5ef2aSThomas Huth 
raise_exception_err_ra(CPUPPCState * env,uint32_t exception,uint32_t error_code,uintptr_t raddr)2559fcf5ef2aSThomas Huth void raise_exception_err_ra(CPUPPCState *env, uint32_t exception,
2560fcf5ef2aSThomas Huth                             uint32_t error_code, uintptr_t raddr)
2561fcf5ef2aSThomas Huth {
2562db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
2563fcf5ef2aSThomas Huth 
2564fcf5ef2aSThomas Huth     cs->exception_index = exception;
2565fcf5ef2aSThomas Huth     env->error_code = error_code;
2566fcf5ef2aSThomas Huth     cpu_loop_exit_restore(cs, raddr);
2567fcf5ef2aSThomas Huth }
2568fcf5ef2aSThomas Huth 
raise_exception_err(CPUPPCState * env,uint32_t exception,uint32_t error_code)2569fcf5ef2aSThomas Huth void raise_exception_err(CPUPPCState *env, uint32_t exception,
2570fcf5ef2aSThomas Huth                          uint32_t error_code)
2571fcf5ef2aSThomas Huth {
2572fcf5ef2aSThomas Huth     raise_exception_err_ra(env, exception, error_code, 0);
2573fcf5ef2aSThomas Huth }
2574fcf5ef2aSThomas Huth 
raise_exception(CPUPPCState * env,uint32_t exception)2575fcf5ef2aSThomas Huth void raise_exception(CPUPPCState *env, uint32_t exception)
2576fcf5ef2aSThomas Huth {
2577fcf5ef2aSThomas Huth     raise_exception_err_ra(env, exception, 0, 0);
2578fcf5ef2aSThomas Huth }
2579fcf5ef2aSThomas Huth 
raise_exception_ra(CPUPPCState * env,uint32_t exception,uintptr_t raddr)2580fcf5ef2aSThomas Huth void raise_exception_ra(CPUPPCState *env, uint32_t exception,
2581fcf5ef2aSThomas Huth                         uintptr_t raddr)
2582fcf5ef2aSThomas Huth {
2583fcf5ef2aSThomas Huth     raise_exception_err_ra(env, exception, 0, raddr);
2584fcf5ef2aSThomas Huth }
2585fcf5ef2aSThomas Huth 
25862b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
helper_raise_exception_err(CPUPPCState * env,uint32_t exception,uint32_t error_code)2587fcf5ef2aSThomas Huth void helper_raise_exception_err(CPUPPCState *env, uint32_t exception,
2588fcf5ef2aSThomas Huth                                 uint32_t error_code)
2589fcf5ef2aSThomas Huth {
2590fcf5ef2aSThomas Huth     raise_exception_err_ra(env, exception, error_code, 0);
2591fcf5ef2aSThomas Huth }
2592fcf5ef2aSThomas Huth 
helper_raise_exception(CPUPPCState * env,uint32_t exception)2593fcf5ef2aSThomas Huth void helper_raise_exception(CPUPPCState *env, uint32_t exception)
2594fcf5ef2aSThomas Huth {
2595fcf5ef2aSThomas Huth     raise_exception_err_ra(env, exception, 0, 0);
2596fcf5ef2aSThomas Huth }
2597fcf5ef2aSThomas Huth 
2598f6c2d68bSBALATON Zoltan #ifndef CONFIG_USER_ONLY
helper_store_msr(CPUPPCState * env,target_ulong val)2599fcf5ef2aSThomas Huth void helper_store_msr(CPUPPCState *env, target_ulong val)
2600fcf5ef2aSThomas Huth {
2601fcf5ef2aSThomas Huth     uint32_t excp = hreg_store_msr(env, val, 0);
2602fcf5ef2aSThomas Huth 
2603fcf5ef2aSThomas Huth     if (excp != 0) {
26040661329aSBALATON Zoltan         cpu_interrupt_exittb(env_cpu(env));
2605fcf5ef2aSThomas Huth         raise_exception(env, excp);
2606fcf5ef2aSThomas Huth     }
2607fcf5ef2aSThomas Huth }
2608fcf5ef2aSThomas Huth 
helper_ppc_maybe_interrupt(CPUPPCState * env)26092fdedcbcSMatheus Ferst void helper_ppc_maybe_interrupt(CPUPPCState *env)
26102fdedcbcSMatheus Ferst {
26112fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
26122fdedcbcSMatheus Ferst }
26132fdedcbcSMatheus Ferst 
2614f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
helper_scv(CPUPPCState * env,uint32_t lev)2615f43520e5SRichard Henderson void helper_scv(CPUPPCState *env, uint32_t lev)
2616f43520e5SRichard Henderson {
2617f43520e5SRichard Henderson     if (env->spr[SPR_FSCR] & (1ull << FSCR_SCV)) {
2618f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_SYSCALL_VECTORED, lev);
2619f43520e5SRichard Henderson     } else {
2620f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_FU, FSCR_IC_SCV);
2621f43520e5SRichard Henderson     }
2622f43520e5SRichard Henderson }
2623f43520e5SRichard Henderson 
helper_pminsn(CPUPPCState * env,uint32_t insn)262407e4804fSCédric Le Goater void helper_pminsn(CPUPPCState *env, uint32_t insn)
2625fcf5ef2aSThomas Huth {
26264e6b7db2SBALATON Zoltan     CPUState *cs = env_cpu(env);
2627fcf5ef2aSThomas Huth 
2628fcf5ef2aSThomas Huth     cs->halted = 1;
2629fcf5ef2aSThomas Huth 
26303621e2c9SBenjamin Herrenschmidt     /* Condition for waking up at 0x100 */
26311e7fd61dSBenjamin Herrenschmidt     env->resume_as_sreset = (insn != PPC_PM_STOP) ||
263221c0d66aSBenjamin Herrenschmidt         (env->spr[SPR_PSSCR] & PSSCR_EC);
26332fdedcbcSMatheus Ferst 
26349915dac4SNicholas Piggin     /* HDECR is not to wake from PM state, it may have already fired */
26359915dac4SNicholas Piggin     if (env->resume_as_sreset) {
26369915dac4SNicholas Piggin         PowerPCCPU *cpu = env_archcpu(env);
26379915dac4SNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_HDECR, 0);
26389915dac4SNicholas Piggin     }
26399915dac4SNicholas Piggin 
26402fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
2641fcf5ef2aSThomas Huth }
2642f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
2643fcf5ef2aSThomas Huth 
do_rfi(CPUPPCState * env,target_ulong nip,target_ulong msr)264462e79ef9SCédric Le Goater static void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr)
2645fcf5ef2aSThomas Huth {
2646fcf5ef2aSThomas Huth     /* MSR:POW cannot be set by any form of rfi */
2647fcf5ef2aSThomas Huth     msr &= ~(1ULL << MSR_POW);
2648fcf5ef2aSThomas Huth 
26495aad0457SChristophe Leroy     /* MSR:TGPR cannot be set by any form of rfi */
26505aad0457SChristophe Leroy     if (env->flags & POWERPC_FLAG_TGPR)
26515aad0457SChristophe Leroy         msr &= ~(1ULL << MSR_TGPR);
26525aad0457SChristophe Leroy 
2653f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
2654fcf5ef2aSThomas Huth     /* Switching to 32-bit ? Crop the nip */
2655fcf5ef2aSThomas Huth     if (!msr_is_64bit(env, msr)) {
2656fcf5ef2aSThomas Huth         nip = (uint32_t)nip;
2657fcf5ef2aSThomas Huth     }
2658fcf5ef2aSThomas Huth #else
2659fcf5ef2aSThomas Huth     nip = (uint32_t)nip;
2660fcf5ef2aSThomas Huth #endif
2661fcf5ef2aSThomas Huth     /* XXX: beware: this is false if VLE is supported */
2662fcf5ef2aSThomas Huth     env->nip = nip & ~((target_ulong)0x00000003);
2663fcf5ef2aSThomas Huth     hreg_store_msr(env, msr, 1);
26642eb1ef73SCédric Le Goater     trace_ppc_excp_rfi(env->nip, env->msr);
266547733729SDavid Gibson     /*
266647733729SDavid Gibson      * No need to raise an exception here, as rfi is always the last
266747733729SDavid Gibson      * insn of a TB
2668fcf5ef2aSThomas Huth      */
26690661329aSBALATON Zoltan     cpu_interrupt_exittb(env_cpu(env));
2670a8b73734SNikunj A Dadhania     /* Reset the reservation */
2671a8b73734SNikunj A Dadhania     env->reserve_addr = -1;
2672a8b73734SNikunj A Dadhania 
2673fcf5ef2aSThomas Huth     /* Context synchronizing: check if TCG TLB needs flush */
2674fcf5ef2aSThomas Huth     check_tlb_flush(env, false);
2675fcf5ef2aSThomas Huth }
2676fcf5ef2aSThomas Huth 
helper_rfi(CPUPPCState * env)2677fcf5ef2aSThomas Huth void helper_rfi(CPUPPCState *env)
2678fcf5ef2aSThomas Huth {
2679fcf5ef2aSThomas Huth     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful);
2680fcf5ef2aSThomas Huth }
2681fcf5ef2aSThomas Huth 
2682f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
helper_rfid(CPUPPCState * env)2683fcf5ef2aSThomas Huth void helper_rfid(CPUPPCState *env)
2684fcf5ef2aSThomas Huth {
268547733729SDavid Gibson     /*
2686136fbf65Szhaolichang      * The architecture defines a number of rules for which bits can
268747733729SDavid Gibson      * change but in practice, we handle this in hreg_store_msr()
2688fcf5ef2aSThomas Huth      * which will be called by do_rfi(), so there is no need to filter
2689fcf5ef2aSThomas Huth      * here
2690fcf5ef2aSThomas Huth      */
2691fcf5ef2aSThomas Huth     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]);
2692fcf5ef2aSThomas Huth }
2693fcf5ef2aSThomas Huth 
helper_rfscv(CPUPPCState * env)26943c89b8d6SNicholas Piggin void helper_rfscv(CPUPPCState *env)
26953c89b8d6SNicholas Piggin {
26963c89b8d6SNicholas Piggin     do_rfi(env, env->lr, env->ctr);
26973c89b8d6SNicholas Piggin }
26983c89b8d6SNicholas Piggin 
helper_hrfid(CPUPPCState * env)2699fcf5ef2aSThomas Huth void helper_hrfid(CPUPPCState *env)
2700fcf5ef2aSThomas Huth {
2701fcf5ef2aSThomas Huth     do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
2702fcf5ef2aSThomas Huth }
2703fcf5ef2aSThomas Huth 
helper_rfebb(CPUPPCState * env,target_ulong s)27041f26c751SDaniel Henrique Barboza void helper_rfebb(CPUPPCState *env, target_ulong s)
27051f26c751SDaniel Henrique Barboza {
27061f26c751SDaniel Henrique Barboza     target_ulong msr = env->msr;
27071f26c751SDaniel Henrique Barboza 
27081f26c751SDaniel Henrique Barboza     /*
27091f26c751SDaniel Henrique Barboza      * Handling of BESCR bits 32:33 according to PowerISA v3.1:
27101f26c751SDaniel Henrique Barboza      *
27111f26c751SDaniel Henrique Barboza      * "If BESCR 32:33 != 0b00 the instruction is treated as if
27121f26c751SDaniel Henrique Barboza      *  the instruction form were invalid."
27131f26c751SDaniel Henrique Barboza      */
27141f26c751SDaniel Henrique Barboza     if (env->spr[SPR_BESCR] & BESCR_INVALID) {
27151f26c751SDaniel Henrique Barboza         raise_exception_err(env, POWERPC_EXCP_PROGRAM,
27161f26c751SDaniel Henrique Barboza                             POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
27171f26c751SDaniel Henrique Barboza     }
27181f26c751SDaniel Henrique Barboza 
27191f26c751SDaniel Henrique Barboza     env->nip = env->spr[SPR_EBBRR];
27201f26c751SDaniel Henrique Barboza 
27211f26c751SDaniel Henrique Barboza     /* Switching to 32-bit ? Crop the nip */
27221f26c751SDaniel Henrique Barboza     if (!msr_is_64bit(env, msr)) {
27231f26c751SDaniel Henrique Barboza         env->nip = (uint32_t)env->spr[SPR_EBBRR];
27241f26c751SDaniel Henrique Barboza     }
27251f26c751SDaniel Henrique Barboza 
27261f26c751SDaniel Henrique Barboza     if (s) {
27271f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_GE;
27281f26c751SDaniel Henrique Barboza     } else {
27291f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
27301f26c751SDaniel Henrique Barboza     }
27311f26c751SDaniel Henrique Barboza }
2732d3412df2SDaniel Henrique Barboza 
2733d3412df2SDaniel Henrique Barboza /*
2734d3412df2SDaniel Henrique Barboza  * Triggers or queues an 'ebb_excp' EBB exception. All checks
2735d3412df2SDaniel Henrique Barboza  * but FSCR, HFSCR and msr_pr must be done beforehand.
2736d3412df2SDaniel Henrique Barboza  *
2737d3412df2SDaniel Henrique Barboza  * PowerISA v3.1 isn't clear about whether an EBB should be
2738d3412df2SDaniel Henrique Barboza  * postponed or cancelled if the EBB facility is unavailable.
2739d3412df2SDaniel Henrique Barboza  * Our assumption here is that the EBB is cancelled if both
2740d3412df2SDaniel Henrique Barboza  * FSCR and HFSCR EBB facilities aren't available.
2741d3412df2SDaniel Henrique Barboza  */
do_ebb(CPUPPCState * env,int ebb_excp)2742d3412df2SDaniel Henrique Barboza static void do_ebb(CPUPPCState *env, int ebb_excp)
2743d3412df2SDaniel Henrique Barboza {
2744d3412df2SDaniel Henrique Barboza     PowerPCCPU *cpu = env_archcpu(env);
2745d3412df2SDaniel Henrique Barboza 
2746d3412df2SDaniel Henrique Barboza     /*
2747d3412df2SDaniel Henrique Barboza      * FSCR_EBB and FSCR_IC_EBB are the same bits used with
2748d3412df2SDaniel Henrique Barboza      * HFSCR.
2749d3412df2SDaniel Henrique Barboza      */
2750d3412df2SDaniel Henrique Barboza     helper_fscr_facility_check(env, FSCR_EBB, 0, FSCR_IC_EBB);
2751d3412df2SDaniel Henrique Barboza     helper_hfscr_facility_check(env, FSCR_EBB, "EBB", FSCR_IC_EBB);
2752d3412df2SDaniel Henrique Barboza 
2753d3412df2SDaniel Henrique Barboza     if (ebb_excp == POWERPC_EXCP_PERFM_EBB) {
2754d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_PMEO;
2755d3412df2SDaniel Henrique Barboza     } else if (ebb_excp == POWERPC_EXCP_EXTERNAL_EBB) {
2756d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_EEO;
2757d3412df2SDaniel Henrique Barboza     }
2758d3412df2SDaniel Henrique Barboza 
2759d41ccf6eSVíctor Colombo     if (FIELD_EX64(env->msr, MSR, PR)) {
2760d3412df2SDaniel Henrique Barboza         powerpc_excp(cpu, ebb_excp);
2761d3412df2SDaniel Henrique Barboza     } else {
27627b694df6SMatheus Ferst         ppc_set_irq(cpu, PPC_INTERRUPT_EBB, 1);
2763d3412df2SDaniel Henrique Barboza     }
2764d3412df2SDaniel Henrique Barboza }
2765d3412df2SDaniel Henrique Barboza 
raise_ebb_perfm_exception(CPUPPCState * env)2766d3412df2SDaniel Henrique Barboza void raise_ebb_perfm_exception(CPUPPCState *env)
2767d3412df2SDaniel Henrique Barboza {
2768d3412df2SDaniel Henrique Barboza     bool perfm_ebb_enabled = env->spr[SPR_POWER_MMCR0] & MMCR0_EBE &&
2769d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_PME &&
2770d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_GE;
2771d3412df2SDaniel Henrique Barboza 
2772d3412df2SDaniel Henrique Barboza     if (!perfm_ebb_enabled) {
2773d3412df2SDaniel Henrique Barboza         return;
2774d3412df2SDaniel Henrique Barboza     }
2775d3412df2SDaniel Henrique Barboza 
2776d3412df2SDaniel Henrique Barboza     do_ebb(env, POWERPC_EXCP_PERFM_EBB);
2777d3412df2SDaniel Henrique Barboza }
2778754920c7SBALATON Zoltan #endif /* TARGET_PPC64 */
27791f26c751SDaniel Henrique Barboza 
2780fcf5ef2aSThomas Huth /*****************************************************************************/
2781fcf5ef2aSThomas Huth /* Embedded PowerPC specific helpers */
helper_40x_rfci(CPUPPCState * env)2782fcf5ef2aSThomas Huth void helper_40x_rfci(CPUPPCState *env)
2783fcf5ef2aSThomas Huth {
2784fcf5ef2aSThomas Huth     do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]);
2785fcf5ef2aSThomas Huth }
2786fcf5ef2aSThomas Huth 
helper_rfci(CPUPPCState * env)2787fcf5ef2aSThomas Huth void helper_rfci(CPUPPCState *env)
2788fcf5ef2aSThomas Huth {
2789fcf5ef2aSThomas Huth     do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]);
2790fcf5ef2aSThomas Huth }
2791fcf5ef2aSThomas Huth 
helper_rfdi(CPUPPCState * env)2792fcf5ef2aSThomas Huth void helper_rfdi(CPUPPCState *env)
2793fcf5ef2aSThomas Huth {
2794fcf5ef2aSThomas Huth     /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
2795fcf5ef2aSThomas Huth     do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]);
2796fcf5ef2aSThomas Huth }
2797fcf5ef2aSThomas Huth 
helper_rfmci(CPUPPCState * env)2798fcf5ef2aSThomas Huth void helper_rfmci(CPUPPCState *env)
2799fcf5ef2aSThomas Huth {
2800fcf5ef2aSThomas Huth     /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
2801fcf5ef2aSThomas Huth     do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
2802fcf5ef2aSThomas Huth }
28035ca958cfSBALATON Zoltan #endif /* !CONFIG_USER_ONLY */
2804fcf5ef2aSThomas Huth 
helper_TW(CPUPPCState * env,target_ulong arg1,target_ulong arg2,uint32_t flags)2805ae556c6aSChinmay Rath void helper_TW(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2806fcf5ef2aSThomas Huth                uint32_t flags)
2807fcf5ef2aSThomas Huth {
2808fcf5ef2aSThomas Huth     if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
2809fcf5ef2aSThomas Huth                   ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
2810fcf5ef2aSThomas Huth                   ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
2811fcf5ef2aSThomas Huth                   ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
2812fcf5ef2aSThomas Huth                   ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
2813fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
2814fcf5ef2aSThomas Huth                                POWERPC_EXCP_TRAP, GETPC());
2815fcf5ef2aSThomas Huth     }
2816fcf5ef2aSThomas Huth }
2817fcf5ef2aSThomas Huth 
2818f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
helper_TD(CPUPPCState * env,target_ulong arg1,target_ulong arg2,uint32_t flags)2819ae556c6aSChinmay Rath void helper_TD(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2820fcf5ef2aSThomas Huth                uint32_t flags)
2821fcf5ef2aSThomas Huth {
2822fcf5ef2aSThomas Huth     if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
2823fcf5ef2aSThomas Huth                   ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
2824fcf5ef2aSThomas Huth                   ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
2825fcf5ef2aSThomas Huth                   ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
2826fcf5ef2aSThomas Huth                   ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) {
2827fcf5ef2aSThomas Huth         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
2828fcf5ef2aSThomas Huth                                POWERPC_EXCP_TRAP, GETPC());
2829fcf5ef2aSThomas Huth     }
2830fcf5ef2aSThomas Huth }
2831f6c2d68bSBALATON Zoltan #endif /* TARGET_PPC64 */
2832fcf5ef2aSThomas Huth 
helper_SIMON_LIKE_32_64(uint32_t x,uint64_t key,uint32_t lane)2833670f1da3SVíctor Colombo static uint32_t helper_SIMON_LIKE_32_64(uint32_t x, uint64_t key, uint32_t lane)
2834670f1da3SVíctor Colombo {
2835670f1da3SVíctor Colombo     const uint16_t c = 0xfffc;
2836670f1da3SVíctor Colombo     const uint64_t z0 = 0xfa2561cdf44ac398ULL;
2837670f1da3SVíctor Colombo     uint16_t z = 0, temp;
2838670f1da3SVíctor Colombo     uint16_t k[32], eff_k[32], xleft[33], xright[33], fxleft[32];
2839670f1da3SVíctor Colombo 
2840670f1da3SVíctor Colombo     for (int i = 3; i >= 0; i--) {
2841670f1da3SVíctor Colombo         k[i] = key & 0xffff;
2842670f1da3SVíctor Colombo         key >>= 16;
2843670f1da3SVíctor Colombo     }
2844670f1da3SVíctor Colombo     xleft[0] = x & 0xffff;
2845670f1da3SVíctor Colombo     xright[0] = (x >> 16) & 0xffff;
2846670f1da3SVíctor Colombo 
2847670f1da3SVíctor Colombo     for (int i = 0; i < 28; i++) {
2848670f1da3SVíctor Colombo         z = (z0 >> (63 - i)) & 1;
2849670f1da3SVíctor Colombo         temp = ror16(k[i + 3], 3) ^ k[i + 1];
2850670f1da3SVíctor Colombo         k[i + 4] = c ^ z ^ k[i] ^ temp ^ ror16(temp, 1);
2851670f1da3SVíctor Colombo     }
2852670f1da3SVíctor Colombo 
2853670f1da3SVíctor Colombo     for (int i = 0; i < 8; i++) {
2854670f1da3SVíctor Colombo         eff_k[4 * i + 0] = k[4 * i + ((0 + lane) % 4)];
2855670f1da3SVíctor Colombo         eff_k[4 * i + 1] = k[4 * i + ((1 + lane) % 4)];
2856670f1da3SVíctor Colombo         eff_k[4 * i + 2] = k[4 * i + ((2 + lane) % 4)];
2857670f1da3SVíctor Colombo         eff_k[4 * i + 3] = k[4 * i + ((3 + lane) % 4)];
2858670f1da3SVíctor Colombo     }
2859670f1da3SVíctor Colombo 
2860670f1da3SVíctor Colombo     for (int i = 0; i < 32; i++) {
2861670f1da3SVíctor Colombo         fxleft[i] = (rol16(xleft[i], 1) &
2862670f1da3SVíctor Colombo             rol16(xleft[i], 8)) ^ rol16(xleft[i], 2);
2863670f1da3SVíctor Colombo         xleft[i + 1] = xright[i] ^ fxleft[i] ^ eff_k[i];
2864670f1da3SVíctor Colombo         xright[i + 1] = xleft[i];
2865670f1da3SVíctor Colombo     }
2866670f1da3SVíctor Colombo 
2867670f1da3SVíctor Colombo     return (((uint32_t)xright[32]) << 16) | xleft[32];
2868670f1da3SVíctor Colombo }
2869670f1da3SVíctor Colombo 
hash_digest(uint64_t ra,uint64_t rb,uint64_t key)2870670f1da3SVíctor Colombo static uint64_t hash_digest(uint64_t ra, uint64_t rb, uint64_t key)
2871670f1da3SVíctor Colombo {
2872670f1da3SVíctor Colombo     uint64_t stage0_h = 0ULL, stage0_l = 0ULL;
2873670f1da3SVíctor Colombo     uint64_t stage1_h, stage1_l;
2874670f1da3SVíctor Colombo 
2875670f1da3SVíctor Colombo     for (int i = 0; i < 4; i++) {
2876670f1da3SVíctor Colombo         stage0_h |= ror64(rb & 0xff, 8 * (2 * i + 1));
2877670f1da3SVíctor Colombo         stage0_h |= ((ra >> 32) & 0xff) << (8 * 2 * i);
2878670f1da3SVíctor Colombo         stage0_l |= ror64((rb >> 32) & 0xff, 8 * (2 * i + 1));
2879670f1da3SVíctor Colombo         stage0_l |= (ra & 0xff) << (8 * 2 * i);
2880670f1da3SVíctor Colombo         rb >>= 8;
2881670f1da3SVíctor Colombo         ra >>= 8;
2882670f1da3SVíctor Colombo     }
2883670f1da3SVíctor Colombo 
2884670f1da3SVíctor Colombo     stage1_h = (uint64_t)helper_SIMON_LIKE_32_64(stage0_h >> 32, key, 0) << 32;
2885670f1da3SVíctor Colombo     stage1_h |= helper_SIMON_LIKE_32_64(stage0_h, key, 1);
2886670f1da3SVíctor Colombo     stage1_l = (uint64_t)helper_SIMON_LIKE_32_64(stage0_l >> 32, key, 2) << 32;
2887670f1da3SVíctor Colombo     stage1_l |= helper_SIMON_LIKE_32_64(stage0_l, key, 3);
2888670f1da3SVíctor Colombo 
2889670f1da3SVíctor Colombo     return stage1_h ^ stage1_l;
2890670f1da3SVíctor Colombo }
2891670f1da3SVíctor Colombo 
do_hash(CPUPPCState * env,target_ulong ea,target_ulong ra,target_ulong rb,uint64_t key,bool store)28924091fabfSNicholas Miehlbradt static void do_hash(CPUPPCState *env, target_ulong ea, target_ulong ra,
28934091fabfSNicholas Miehlbradt                     target_ulong rb, uint64_t key, bool store)
28944091fabfSNicholas Miehlbradt {
28954091fabfSNicholas Miehlbradt     uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash;
28964091fabfSNicholas Miehlbradt 
28974091fabfSNicholas Miehlbradt     if (store) {
28984091fabfSNicholas Miehlbradt         cpu_stq_data_ra(env, ea, calculated_hash, GETPC());
28994091fabfSNicholas Miehlbradt     } else {
29004091fabfSNicholas Miehlbradt         loaded_hash = cpu_ldq_data_ra(env, ea, GETPC());
29014091fabfSNicholas Miehlbradt         if (loaded_hash != calculated_hash) {
29024091fabfSNicholas Miehlbradt             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
29034091fabfSNicholas Miehlbradt                 POWERPC_EXCP_TRAP, GETPC());
29044091fabfSNicholas Miehlbradt         }
29054091fabfSNicholas Miehlbradt     }
29064091fabfSNicholas Miehlbradt }
29074091fabfSNicholas Miehlbradt 
2908670f1da3SVíctor Colombo #include "qemu/guest-random.h"
2909670f1da3SVíctor Colombo 
29104091fabfSNicholas Miehlbradt #ifdef TARGET_PPC64
29114091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect)                             \
2912670f1da3SVíctor Colombo void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
2913670f1da3SVíctor Colombo                  target_ulong rb)                                             \
2914670f1da3SVíctor Colombo {                                                                             \
29154091fabfSNicholas Miehlbradt     if (env->msr & R_MSR_PR_MASK) {                                           \
29164091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_DEXCR] & R_DEXCR_PRO_##dexcr_aspect##_MASK ||      \
29174091fabfSNicholas Miehlbradt             env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK))       \
29184091fabfSNicholas Miehlbradt             return;                                                           \
29194091fabfSNicholas Miehlbradt     } else if (!(env->msr & R_MSR_HV_MASK)) {                                 \
29204091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_DEXCR] & R_DEXCR_PNH_##dexcr_aspect##_MASK ||      \
29214091fabfSNicholas Miehlbradt             env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK))       \
29224091fabfSNicholas Miehlbradt             return;                                                           \
29234091fabfSNicholas Miehlbradt     } else if (!(env->msr & R_MSR_S_MASK)) {                                  \
29244091fabfSNicholas Miehlbradt         if (!(env->spr[SPR_HDEXCR] & R_HDEXCR_HNU_##dexcr_aspect##_MASK))     \
29254091fabfSNicholas Miehlbradt             return;                                                           \
29264091fabfSNicholas Miehlbradt     }                                                                         \
2927670f1da3SVíctor Colombo                                                                               \
29284091fabfSNicholas Miehlbradt     do_hash(env, ea, ra, rb, key, store);                                     \
2929670f1da3SVíctor Colombo }
29304091fabfSNicholas Miehlbradt #else
29314091fabfSNicholas Miehlbradt #define HELPER_HASH(op, key, store, dexcr_aspect)                             \
29324091fabfSNicholas Miehlbradt void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
29334091fabfSNicholas Miehlbradt                  target_ulong rb)                                             \
29344091fabfSNicholas Miehlbradt {                                                                             \
29354091fabfSNicholas Miehlbradt     do_hash(env, ea, ra, rb, key, store);                                     \
29364091fabfSNicholas Miehlbradt }
29374091fabfSNicholas Miehlbradt #endif /* TARGET_PPC64 */
2938670f1da3SVíctor Colombo 
29394091fabfSNicholas Miehlbradt HELPER_HASH(HASHST, env->spr[SPR_HASHKEYR], true, NPHIE)
29404091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHK, env->spr[SPR_HASHKEYR], false, NPHIE)
29414091fabfSNicholas Miehlbradt HELPER_HASH(HASHSTP, env->spr[SPR_HASHPKEYR], true, PHIE)
29424091fabfSNicholas Miehlbradt HELPER_HASH(HASHCHKP, env->spr[SPR_HASHPKEYR], false, PHIE)
2943670f1da3SVíctor Colombo 
2944f6c2d68bSBALATON Zoltan #ifndef CONFIG_USER_ONLY
2945fcf5ef2aSThomas Huth /* Embedded.Processor Control */
dbell2irq(target_ulong rb)2946fcf5ef2aSThomas Huth static int dbell2irq(target_ulong rb)
2947fcf5ef2aSThomas Huth {
2948fcf5ef2aSThomas Huth     int msg = rb & DBELL_TYPE_MASK;
2949fcf5ef2aSThomas Huth     int irq = -1;
2950fcf5ef2aSThomas Huth 
2951fcf5ef2aSThomas Huth     switch (msg) {
2952fcf5ef2aSThomas Huth     case DBELL_TYPE_DBELL:
2953fcf5ef2aSThomas Huth         irq = PPC_INTERRUPT_DOORBELL;
2954fcf5ef2aSThomas Huth         break;
2955fcf5ef2aSThomas Huth     case DBELL_TYPE_DBELL_CRIT:
2956fcf5ef2aSThomas Huth         irq = PPC_INTERRUPT_CDOORBELL;
2957fcf5ef2aSThomas Huth         break;
2958fcf5ef2aSThomas Huth     case DBELL_TYPE_G_DBELL:
2959fcf5ef2aSThomas Huth     case DBELL_TYPE_G_DBELL_CRIT:
2960fcf5ef2aSThomas Huth     case DBELL_TYPE_G_DBELL_MC:
2961fcf5ef2aSThomas Huth         /* XXX implement */
2962fcf5ef2aSThomas Huth     default:
2963fcf5ef2aSThomas Huth         break;
2964fcf5ef2aSThomas Huth     }
2965fcf5ef2aSThomas Huth 
2966fcf5ef2aSThomas Huth     return irq;
2967fcf5ef2aSThomas Huth }
2968fcf5ef2aSThomas Huth 
helper_msgclr(CPUPPCState * env,target_ulong rb)2969fcf5ef2aSThomas Huth void helper_msgclr(CPUPPCState *env, target_ulong rb)
2970fcf5ef2aSThomas Huth {
2971fcf5ef2aSThomas Huth     int irq = dbell2irq(rb);
2972fcf5ef2aSThomas Huth 
2973fcf5ef2aSThomas Huth     if (irq < 0) {
2974fcf5ef2aSThomas Huth         return;
2975fcf5ef2aSThomas Huth     }
2976fcf5ef2aSThomas Huth 
29777b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), irq, 0);
2978fcf5ef2aSThomas Huth }
2979fcf5ef2aSThomas Huth 
helper_msgsnd(target_ulong rb)2980fcf5ef2aSThomas Huth void helper_msgsnd(target_ulong rb)
2981fcf5ef2aSThomas Huth {
2982fcf5ef2aSThomas Huth     int irq = dbell2irq(rb);
2983fcf5ef2aSThomas Huth     int pir = rb & DBELL_PIRTAG_MASK;
2984fcf5ef2aSThomas Huth     CPUState *cs;
2985fcf5ef2aSThomas Huth 
2986fcf5ef2aSThomas Huth     if (irq < 0) {
2987fcf5ef2aSThomas Huth         return;
2988fcf5ef2aSThomas Huth     }
2989fcf5ef2aSThomas Huth 
2990195801d7SStefan Hajnoczi     bql_lock();
2991fcf5ef2aSThomas Huth     CPU_FOREACH(cs) {
2992fcf5ef2aSThomas Huth         PowerPCCPU *cpu = POWERPC_CPU(cs);
2993fcf5ef2aSThomas Huth         CPUPPCState *cenv = &cpu->env;
2994fcf5ef2aSThomas Huth 
29950dfe59feSNicholas Piggin         if ((rb & DBELL_BRDCAST_MASK) || (cenv->spr[SPR_BOOKE_PIR] == pir)) {
29967b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
2997fcf5ef2aSThomas Huth         }
2998fcf5ef2aSThomas Huth     }
2999195801d7SStefan Hajnoczi     bql_unlock();
3000fcf5ef2aSThomas Huth }
30017af1e7b0SCédric Le Goater 
30027af1e7b0SCédric Le Goater /* Server Processor Control */
30037af1e7b0SCédric Le Goater 
dbell_type_server(target_ulong rb)30045ba7ba1dSCédric Le Goater static bool dbell_type_server(target_ulong rb)
30055ba7ba1dSCédric Le Goater {
300647733729SDavid Gibson     /*
300747733729SDavid Gibson      * A Directed Hypervisor Doorbell message is sent only if the
30087af1e7b0SCédric Le Goater      * message type is 5. All other types are reserved and the
300947733729SDavid Gibson      * instruction is a no-op
301047733729SDavid Gibson      */
30115ba7ba1dSCédric Le Goater     return (rb & DBELL_TYPE_MASK) == DBELL_TYPE_DBELL_SERVER;
30127af1e7b0SCédric Le Goater }
30137af1e7b0SCédric Le Goater 
dbell_bcast_core(target_ulong rb)30140dfe59feSNicholas Piggin static inline bool dbell_bcast_core(target_ulong rb)
30150dfe59feSNicholas Piggin {
30160dfe59feSNicholas Piggin     return (rb & DBELL_BRDCAST_MASK) == DBELL_BRDCAST_CORE;
30170dfe59feSNicholas Piggin }
30180dfe59feSNicholas Piggin 
dbell_bcast_subproc(target_ulong rb)30190dfe59feSNicholas Piggin static inline bool dbell_bcast_subproc(target_ulong rb)
30200dfe59feSNicholas Piggin {
30210dfe59feSNicholas Piggin     return (rb & DBELL_BRDCAST_MASK) == DBELL_BRDCAST_SUBPROC;
30220dfe59feSNicholas Piggin }
30230dfe59feSNicholas Piggin 
30247f516cdeSNicholas Piggin /*
30257f516cdeSNicholas Piggin  * Send an interrupt to a thread in the same core as env).
30267f516cdeSNicholas Piggin  */
msgsnd_core_tir(CPUPPCState * env,uint32_t target_tir,int irq)30277f516cdeSNicholas Piggin static void msgsnd_core_tir(CPUPPCState *env, uint32_t target_tir, int irq)
30287f516cdeSNicholas Piggin {
30297f516cdeSNicholas Piggin     PowerPCCPU *cpu = env_archcpu(env);
30307f516cdeSNicholas Piggin     CPUState *cs = env_cpu(env);
30317f516cdeSNicholas Piggin 
303250d8cfb9SNicholas Piggin     if (ppc_cpu_lpar_single_threaded(cs)) {
303350d8cfb9SNicholas Piggin         if (target_tir == 0) {
30347f516cdeSNicholas Piggin             ppc_set_irq(cpu, irq, 1);
303550d8cfb9SNicholas Piggin         }
30367f516cdeSNicholas Piggin     } else {
30377f516cdeSNicholas Piggin         CPUState *ccs;
30387f516cdeSNicholas Piggin 
30397f516cdeSNicholas Piggin         /* Does iothread need to be locked for walking CPU list? */
30407f516cdeSNicholas Piggin         bql_lock();
30417f516cdeSNicholas Piggin         THREAD_SIBLING_FOREACH(cs, ccs) {
30427f516cdeSNicholas Piggin             PowerPCCPU *ccpu = POWERPC_CPU(ccs);
30437f516cdeSNicholas Piggin             if (target_tir == ppc_cpu_tir(ccpu)) {
30447f516cdeSNicholas Piggin                 ppc_set_irq(ccpu, irq, 1);
30457f516cdeSNicholas Piggin                 break;
30467f516cdeSNicholas Piggin             }
30477f516cdeSNicholas Piggin         }
30487f516cdeSNicholas Piggin         bql_unlock();
30497f516cdeSNicholas Piggin     }
30507f516cdeSNicholas Piggin }
30517f516cdeSNicholas Piggin 
helper_book3s_msgclr(CPUPPCState * env,target_ulong rb)30527af1e7b0SCédric Le Goater void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb)
30537af1e7b0SCédric Le Goater {
30545ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
30557af1e7b0SCédric Le Goater         return;
30567af1e7b0SCédric Le Goater     }
30577af1e7b0SCédric Le Goater 
30587b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
30597af1e7b0SCédric Le Goater }
30607af1e7b0SCédric Le Goater 
helper_book3s_msgsnd(CPUPPCState * env,target_ulong rb)30610dfe59feSNicholas Piggin void helper_book3s_msgsnd(CPUPPCState *env, target_ulong rb)
30625ba7ba1dSCédric Le Goater {
30635ba7ba1dSCédric Le Goater     int pir = rb & DBELL_PROCIDTAG_MASK;
30640dfe59feSNicholas Piggin     bool brdcast = false;
30650dfe59feSNicholas Piggin     CPUState *cs, *ccs;
30660dfe59feSNicholas Piggin     PowerPCCPU *cpu;
30675ba7ba1dSCédric Le Goater 
30685ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
30695ba7ba1dSCédric Le Goater         return;
30705ba7ba1dSCédric Le Goater     }
30715ba7ba1dSCédric Le Goater 
30727f516cdeSNicholas Piggin     /* POWER8 msgsnd is like msgsndp (targets a thread within core) */
30737f516cdeSNicholas Piggin     if (!(env->insns_flags2 & PPC2_ISA300)) {
30747f516cdeSNicholas Piggin         msgsnd_core_tir(env, rb & PPC_BITMASK(57, 63), PPC_INTERRUPT_HDOORBELL);
30757f516cdeSNicholas Piggin         return;
30767f516cdeSNicholas Piggin     }
30777f516cdeSNicholas Piggin 
30787f516cdeSNicholas Piggin     /* POWER9 and later msgsnd is a global (targets any thread) */
30790dfe59feSNicholas Piggin     cpu = ppc_get_vcpu_by_pir(pir);
30800dfe59feSNicholas Piggin     if (!cpu) {
30810dfe59feSNicholas Piggin         return;
30820dfe59feSNicholas Piggin     }
30830dfe59feSNicholas Piggin     cs = CPU(cpu);
30840dfe59feSNicholas Piggin 
30850dfe59feSNicholas Piggin     if (dbell_bcast_core(rb) || (dbell_bcast_subproc(rb) &&
30860dfe59feSNicholas Piggin                                  (env->flags & POWERPC_FLAG_SMT_1LPAR))) {
30870dfe59feSNicholas Piggin         brdcast = true;
30880dfe59feSNicholas Piggin     }
30890dfe59feSNicholas Piggin 
309050d8cfb9SNicholas Piggin     if (ppc_cpu_core_single_threaded(cs) || !brdcast) {
30910dfe59feSNicholas Piggin         ppc_set_irq(cpu, PPC_INTERRUPT_HDOORBELL, 1);
30920dfe59feSNicholas Piggin         return;
30930dfe59feSNicholas Piggin     }
30940dfe59feSNicholas Piggin 
30950dfe59feSNicholas Piggin     /*
30960dfe59feSNicholas Piggin      * Why is bql needed for walking CPU list? Answer seems to be because ppc
30970dfe59feSNicholas Piggin      * irq handling needs it, but ppc_set_irq takes the lock itself if needed,
30980dfe59feSNicholas Piggin      * so could this be removed?
30990dfe59feSNicholas Piggin      */
31000dfe59feSNicholas Piggin     bql_lock();
31010dfe59feSNicholas Piggin     THREAD_SIBLING_FOREACH(cs, ccs) {
31020dfe59feSNicholas Piggin         ppc_set_irq(POWERPC_CPU(ccs), PPC_INTERRUPT_HDOORBELL, 1);
31030dfe59feSNicholas Piggin     }
31040dfe59feSNicholas Piggin     bql_unlock();
31055ba7ba1dSCédric Le Goater }
31065ba7ba1dSCédric Le Goater 
3107f6c2d68bSBALATON Zoltan #ifdef TARGET_PPC64
helper_book3s_msgclrp(CPUPPCState * env,target_ulong rb)31085ba7ba1dSCédric Le Goater void helper_book3s_msgclrp(CPUPPCState *env, target_ulong rb)
31095ba7ba1dSCédric Le Goater {
3110493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgclrp", HFSCR_IC_MSGP);
3111493028d8SCédric Le Goater 
31125ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
31135ba7ba1dSCédric Le Goater         return;
31145ba7ba1dSCédric Le Goater     }
31155ba7ba1dSCédric Le Goater 
31162e985555SNicholas Piggin     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_DOORBELL, 0);
31175ba7ba1dSCédric Le Goater }
31185ba7ba1dSCédric Le Goater 
31195ba7ba1dSCédric Le Goater /*
3120d24e80b2SNicholas Piggin  * sends a message to another thread  on the same
31215ba7ba1dSCédric Le Goater  * multi-threaded processor
31225ba7ba1dSCédric Le Goater  */
helper_book3s_msgsndp(CPUPPCState * env,target_ulong rb)31235ba7ba1dSCédric Le Goater void helper_book3s_msgsndp(CPUPPCState *env, target_ulong rb)
31245ba7ba1dSCédric Le Goater {
3125493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgsndp", HFSCR_IC_MSGP);
3126493028d8SCédric Le Goater 
31277f516cdeSNicholas Piggin     if (!dbell_type_server(rb)) {
31285ba7ba1dSCédric Le Goater         return;
31295ba7ba1dSCédric Le Goater     }
31305ba7ba1dSCédric Le Goater 
31317f516cdeSNicholas Piggin     msgsnd_core_tir(env, rb & PPC_BITMASK(57, 63), PPC_INTERRUPT_DOORBELL);
31325ba7ba1dSCédric Le Goater }
3133996473e4SRichard Henderson #endif /* TARGET_PPC64 */
31340f3110faSRichard Henderson 
313514895384SNicholas Piggin /* Single-step tracing */
helper_book3s_trace(CPUPPCState * env,target_ulong prev_ip)313614895384SNicholas Piggin void helper_book3s_trace(CPUPPCState *env, target_ulong prev_ip)
313714895384SNicholas Piggin {
313814895384SNicholas Piggin     uint32_t error_code = 0;
313914895384SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
314014895384SNicholas Piggin         /* Load/store reporting, SRR1[35, 36] and SDAR, are not implemented. */
314114895384SNicholas Piggin         env->spr[SPR_POWER_SIAR] = prev_ip;
314214895384SNicholas Piggin         error_code = PPC_BIT(33);
314314895384SNicholas Piggin     }
314414895384SNicholas Piggin     raise_exception_err(env, POWERPC_EXCP_TRACE, error_code);
314514895384SNicholas Piggin }
314614895384SNicholas Piggin 
ppc_cpu_do_unaligned_access(CPUState * cs,vaddr vaddr,MMUAccessType access_type,int mmu_idx,uintptr_t retaddr)31470f3110faSRichard Henderson void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
31480f3110faSRichard Henderson                                  MMUAccessType access_type,
31490f3110faSRichard Henderson                                  int mmu_idx, uintptr_t retaddr)
31500f3110faSRichard Henderson {
3151b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
315229c4a336SFabiano Rosas     uint32_t insn;
315329c4a336SFabiano Rosas 
315429c4a336SFabiano Rosas     /* Restore state and reload the insn we executed, for filling in DSISR.  */
31553d419a4dSRichard Henderson     cpu_restore_state(cs, retaddr);
3156888050cfSNicholas Piggin     insn = ppc_ldl_code(env, env->nip);
31570f3110faSRichard Henderson 
3158a7e3af13SRichard Henderson     switch (env->mmu_model) {
3159a7e3af13SRichard Henderson     case POWERPC_MMU_SOFT_4xx:
3160a7e3af13SRichard Henderson         env->spr[SPR_40x_DEAR] = vaddr;
3161a7e3af13SRichard Henderson         break;
3162a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE:
3163a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE206:
3164a7e3af13SRichard Henderson         env->spr[SPR_BOOKE_DEAR] = vaddr;
3165a7e3af13SRichard Henderson         break;
3166a7e3af13SRichard Henderson     default:
3167a7e3af13SRichard Henderson         env->spr[SPR_DAR] = vaddr;
3168a7e3af13SRichard Henderson         break;
3169a7e3af13SRichard Henderson     }
3170a7e3af13SRichard Henderson 
31710f3110faSRichard Henderson     cs->exception_index = POWERPC_EXCP_ALIGN;
317229c4a336SFabiano Rosas     env->error_code = insn & 0x03FF0000;
317329c4a336SFabiano Rosas     cpu_loop_exit(cs);
31740f3110faSRichard Henderson }
317555a7fa34SNicholas Piggin 
ppc_cpu_do_transaction_failed(CPUState * cs,hwaddr physaddr,vaddr vaddr,unsigned size,MMUAccessType access_type,int mmu_idx,MemTxAttrs attrs,MemTxResult response,uintptr_t retaddr)317655a7fa34SNicholas Piggin void ppc_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
317755a7fa34SNicholas Piggin                                    vaddr vaddr, unsigned size,
317855a7fa34SNicholas Piggin                                    MMUAccessType access_type,
317955a7fa34SNicholas Piggin                                    int mmu_idx, MemTxAttrs attrs,
318055a7fa34SNicholas Piggin                                    MemTxResult response, uintptr_t retaddr)
318155a7fa34SNicholas Piggin {
3182b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
318355a7fa34SNicholas Piggin 
318455a7fa34SNicholas Piggin     switch (env->excp_model) {
318555a7fa34SNicholas Piggin #if defined(TARGET_PPC64)
3186c8fd9667SNicholas Piggin     case POWERPC_EXCP_POWER8:
318755a7fa34SNicholas Piggin     case POWERPC_EXCP_POWER9:
318855a7fa34SNicholas Piggin     case POWERPC_EXCP_POWER10:
3189c0d96407SAditya Gupta     case POWERPC_EXCP_POWER11:
319055a7fa34SNicholas Piggin         /*
319155a7fa34SNicholas Piggin          * Machine check codes can be found in processor User Manual or
319255a7fa34SNicholas Piggin          * Linux or skiboot source.
319355a7fa34SNicholas Piggin          */
319455a7fa34SNicholas Piggin         if (access_type == MMU_DATA_LOAD) {
319555a7fa34SNicholas Piggin             env->spr[SPR_DAR] = vaddr;
319655a7fa34SNicholas Piggin             env->spr[SPR_DSISR] = PPC_BIT(57);
319755a7fa34SNicholas Piggin             env->error_code = PPC_BIT(42);
319855a7fa34SNicholas Piggin 
319955a7fa34SNicholas Piggin         } else if (access_type == MMU_DATA_STORE) {
320055a7fa34SNicholas Piggin             /*
320155a7fa34SNicholas Piggin              * MCE for stores in POWER is asynchronous so hardware does
320255a7fa34SNicholas Piggin              * not set DAR, but QEMU can do better.
320355a7fa34SNicholas Piggin              */
320455a7fa34SNicholas Piggin             env->spr[SPR_DAR] = vaddr;
320555a7fa34SNicholas Piggin             env->error_code = PPC_BIT(36) | PPC_BIT(43) | PPC_BIT(45);
320655a7fa34SNicholas Piggin             env->error_code |= PPC_BIT(42);
320755a7fa34SNicholas Piggin 
320855a7fa34SNicholas Piggin         } else { /* Fetch */
3209c8fd9667SNicholas Piggin             /*
3210c8fd9667SNicholas Piggin              * is_prefix_insn_excp() tests !PPC_BIT(42) to avoid fetching
3211c8fd9667SNicholas Piggin              * the instruction, so that must always be clear for fetches.
3212c8fd9667SNicholas Piggin              */
321355a7fa34SNicholas Piggin             env->error_code = PPC_BIT(36) | PPC_BIT(44) | PPC_BIT(45);
321455a7fa34SNicholas Piggin         }
321555a7fa34SNicholas Piggin         break;
321655a7fa34SNicholas Piggin #endif
321755a7fa34SNicholas Piggin     default:
321855a7fa34SNicholas Piggin         /*
321955a7fa34SNicholas Piggin          * TODO: Check behaviour for other CPUs, for now do nothing.
322055a7fa34SNicholas Piggin          * Could add a basic MCE even if real hardware ignores.
322155a7fa34SNicholas Piggin          */
322255a7fa34SNicholas Piggin         return;
322355a7fa34SNicholas Piggin     }
322455a7fa34SNicholas Piggin 
322555a7fa34SNicholas Piggin     cs->exception_index = POWERPC_EXCP_MCHECK;
322655a7fa34SNicholas Piggin     cpu_loop_exit_restore(cs, retaddr);
322755a7fa34SNicholas Piggin }
322814192307SNicholas Piggin 
ppc_cpu_debug_excp_handler(CPUState * cs)322914192307SNicholas Piggin void ppc_cpu_debug_excp_handler(CPUState *cs)
323014192307SNicholas Piggin {
323114192307SNicholas Piggin #if defined(TARGET_PPC64)
3232b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
323314192307SNicholas Piggin 
323414192307SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
3235d5ee641cSNicholas Piggin         if (cs->watchpoint_hit) {
3236d5ee641cSNicholas Piggin             if (cs->watchpoint_hit->flags & BP_CPU) {
3237d5ee641cSNicholas Piggin                 env->spr[SPR_DAR] = cs->watchpoint_hit->hitaddr;
3238d5ee641cSNicholas Piggin                 env->spr[SPR_DSISR] = PPC_BIT(41);
3239d5ee641cSNicholas Piggin                 cs->watchpoint_hit = NULL;
3240d5ee641cSNicholas Piggin                 raise_exception(env, POWERPC_EXCP_DSI);
3241d5ee641cSNicholas Piggin             }
3242d5ee641cSNicholas Piggin             cs->watchpoint_hit = NULL;
3243d5ee641cSNicholas Piggin         } else if (cpu_breakpoint_test(cs, env->nip, BP_CPU)) {
324414192307SNicholas Piggin             raise_exception_err(env, POWERPC_EXCP_TRACE,
324514192307SNicholas Piggin                                 PPC_BIT(33) | PPC_BIT(43));
324614192307SNicholas Piggin         }
324714192307SNicholas Piggin     }
324814192307SNicholas Piggin #endif
324914192307SNicholas Piggin }
325014192307SNicholas Piggin 
ppc_cpu_debug_check_breakpoint(CPUState * cs)325114192307SNicholas Piggin bool ppc_cpu_debug_check_breakpoint(CPUState *cs)
325214192307SNicholas Piggin {
325314192307SNicholas Piggin #if defined(TARGET_PPC64)
3254b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
325514192307SNicholas Piggin 
325614192307SNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
325714192307SNicholas Piggin         target_ulong priv;
325814192307SNicholas Piggin 
325914192307SNicholas Piggin         priv = env->spr[SPR_CIABR] & PPC_BITMASK(62, 63);
326014192307SNicholas Piggin         switch (priv) {
326114192307SNicholas Piggin         case 0x1: /* problem */
326214192307SNicholas Piggin             return env->msr & ((target_ulong)1 << MSR_PR);
326314192307SNicholas Piggin         case 0x2: /* supervisor */
326414192307SNicholas Piggin             return (!(env->msr & ((target_ulong)1 << MSR_PR)) &&
326514192307SNicholas Piggin                     !(env->msr & ((target_ulong)1 << MSR_HV)));
326614192307SNicholas Piggin         case 0x3: /* hypervisor */
326714192307SNicholas Piggin             return (!(env->msr & ((target_ulong)1 << MSR_PR)) &&
326814192307SNicholas Piggin                      (env->msr & ((target_ulong)1 << MSR_HV)));
326914192307SNicholas Piggin         default:
327014192307SNicholas Piggin             g_assert_not_reached();
327114192307SNicholas Piggin         }
327214192307SNicholas Piggin     }
327314192307SNicholas Piggin #endif
327414192307SNicholas Piggin 
327514192307SNicholas Piggin     return false;
327614192307SNicholas Piggin }
327714192307SNicholas Piggin 
ppc_cpu_debug_check_watchpoint(CPUState * cs,CPUWatchpoint * wp)3278d5ee641cSNicholas Piggin bool ppc_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
3279d5ee641cSNicholas Piggin {
3280d5ee641cSNicholas Piggin #if defined(TARGET_PPC64)
3281b77af26eSRichard Henderson     CPUPPCState *env = cpu_env(cs);
3282d5ee641cSNicholas Piggin 
3283d5ee641cSNicholas Piggin     if (env->insns_flags2 & PPC2_ISA207S) {
3284d5ee641cSNicholas Piggin         if (wp == env->dawr0_watchpoint) {
3285d5ee641cSNicholas Piggin             uint32_t dawrx = env->spr[SPR_DAWRX0];
3286d5ee641cSNicholas Piggin             bool wt = extract32(dawrx, PPC_BIT_NR(59), 1);
3287d5ee641cSNicholas Piggin             bool wti = extract32(dawrx, PPC_BIT_NR(60), 1);
3288d5ee641cSNicholas Piggin             bool hv = extract32(dawrx, PPC_BIT_NR(61), 1);
3289d5ee641cSNicholas Piggin             bool sv = extract32(dawrx, PPC_BIT_NR(62), 1);
3290d5ee641cSNicholas Piggin             bool pr = extract32(dawrx, PPC_BIT_NR(62), 1);
3291d5ee641cSNicholas Piggin 
3292d5ee641cSNicholas Piggin             if ((env->msr & ((target_ulong)1 << MSR_PR)) && !pr) {
3293d5ee641cSNicholas Piggin                 return false;
3294d5ee641cSNicholas Piggin             } else if ((env->msr & ((target_ulong)1 << MSR_HV)) && !hv) {
3295d5ee641cSNicholas Piggin                 return false;
3296d5ee641cSNicholas Piggin             } else if (!sv) {
3297d5ee641cSNicholas Piggin                 return false;
3298d5ee641cSNicholas Piggin             }
3299d5ee641cSNicholas Piggin 
3300d5ee641cSNicholas Piggin             if (!wti) {
3301d5ee641cSNicholas Piggin                 if (env->msr & ((target_ulong)1 << MSR_DR)) {
3302d5ee641cSNicholas Piggin                     if (!wt) {
3303d5ee641cSNicholas Piggin                         return false;
3304d5ee641cSNicholas Piggin                     }
3305d5ee641cSNicholas Piggin                 } else {
3306d5ee641cSNicholas Piggin                     if (wt) {
3307d5ee641cSNicholas Piggin                         return false;
3308d5ee641cSNicholas Piggin                     }
3309d5ee641cSNicholas Piggin                 }
3310d5ee641cSNicholas Piggin             }
3311d5ee641cSNicholas Piggin 
3312d5ee641cSNicholas Piggin             return true;
3313d5ee641cSNicholas Piggin         }
3314d5ee641cSNicholas Piggin     }
3315d5ee641cSNicholas Piggin #endif
3316d5ee641cSNicholas Piggin 
3317d5ee641cSNicholas Piggin     return false;
3318d5ee641cSNicholas Piggin }
3319d5ee641cSNicholas Piggin 
3320996473e4SRichard Henderson #endif /* !CONFIG_USER_ONLY */
33215ca958cfSBALATON Zoltan #endif /* CONFIG_TCG */
3322