xref: /qemu/target/ppc/excp_helper.c (revision 049b4ad66984aad278b3fc0a75b5a995c9b66a16)
1ad71ed68SBlue Swirl /*
2ad71ed68SBlue Swirl  *  PowerPC exception emulation helpers for QEMU.
3ad71ed68SBlue Swirl  *
4ad71ed68SBlue Swirl  *  Copyright (c) 2003-2007 Jocelyn Mayer
5ad71ed68SBlue Swirl  *
6ad71ed68SBlue Swirl  * This library is free software; you can redistribute it and/or
7ad71ed68SBlue Swirl  * modify it under the terms of the GNU Lesser General Public
8ad71ed68SBlue Swirl  * License as published by the Free Software Foundation; either
96bd039cdSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
10ad71ed68SBlue Swirl  *
11ad71ed68SBlue Swirl  * This library is distributed in the hope that it will be useful,
12ad71ed68SBlue Swirl  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13ad71ed68SBlue Swirl  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14ad71ed68SBlue Swirl  * Lesser General Public License for more details.
15ad71ed68SBlue Swirl  *
16ad71ed68SBlue Swirl  * You should have received a copy of the GNU Lesser General Public
17ad71ed68SBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18ad71ed68SBlue Swirl  */
190d75590dSPeter Maydell #include "qemu/osdep.h"
20f1c29ebcSThomas Huth #include "qemu/main-loop.h"
21cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h"
22ad71ed68SBlue Swirl #include "cpu.h"
2363c91552SPaolo Bonzini #include "exec/exec-all.h"
240f3110faSRichard Henderson #include "internal.h"
25ad71ed68SBlue Swirl #include "helper_regs.h"
267b694df6SMatheus Ferst #include "hw/ppc/ppc.h"
27ad71ed68SBlue Swirl 
282eb1ef73SCédric Le Goater #include "trace.h"
292eb1ef73SCédric Le Goater 
302b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
312b44e219SBruno Larsen (billionai) #include "exec/helper-proto.h"
322b44e219SBruno Larsen (billionai) #include "exec/cpu_ldst.h"
332b44e219SBruno Larsen (billionai) #endif
342b44e219SBruno Larsen (billionai) 
35c79c73f6SBlue Swirl /*****************************************************************************/
36c79c73f6SBlue Swirl /* Exception processing */
37f725245cSPhilippe Mathieu-Daudé #if !defined(CONFIG_USER_ONLY)
3897a8ea5aSAndreas Färber 
396789f23bSCédric Le Goater static const char *powerpc_excp_name(int excp)
406789f23bSCédric Le Goater {
416789f23bSCédric Le Goater     switch (excp) {
426789f23bSCédric Le Goater     case POWERPC_EXCP_CRITICAL: return "CRITICAL";
436789f23bSCédric Le Goater     case POWERPC_EXCP_MCHECK:   return "MCHECK";
446789f23bSCédric Le Goater     case POWERPC_EXCP_DSI:      return "DSI";
456789f23bSCédric Le Goater     case POWERPC_EXCP_ISI:      return "ISI";
466789f23bSCédric Le Goater     case POWERPC_EXCP_EXTERNAL: return "EXTERNAL";
476789f23bSCédric Le Goater     case POWERPC_EXCP_ALIGN:    return "ALIGN";
486789f23bSCédric Le Goater     case POWERPC_EXCP_PROGRAM:  return "PROGRAM";
496789f23bSCédric Le Goater     case POWERPC_EXCP_FPU:      return "FPU";
506789f23bSCédric Le Goater     case POWERPC_EXCP_SYSCALL:  return "SYSCALL";
516789f23bSCédric Le Goater     case POWERPC_EXCP_APU:      return "APU";
526789f23bSCédric Le Goater     case POWERPC_EXCP_DECR:     return "DECR";
536789f23bSCédric Le Goater     case POWERPC_EXCP_FIT:      return "FIT";
546789f23bSCédric Le Goater     case POWERPC_EXCP_WDT:      return "WDT";
556789f23bSCédric Le Goater     case POWERPC_EXCP_DTLB:     return "DTLB";
566789f23bSCédric Le Goater     case POWERPC_EXCP_ITLB:     return "ITLB";
576789f23bSCédric Le Goater     case POWERPC_EXCP_DEBUG:    return "DEBUG";
586789f23bSCédric Le Goater     case POWERPC_EXCP_SPEU:     return "SPEU";
596789f23bSCédric Le Goater     case POWERPC_EXCP_EFPDI:    return "EFPDI";
606789f23bSCédric Le Goater     case POWERPC_EXCP_EFPRI:    return "EFPRI";
616789f23bSCédric Le Goater     case POWERPC_EXCP_EPERFM:   return "EPERFM";
626789f23bSCédric Le Goater     case POWERPC_EXCP_DOORI:    return "DOORI";
636789f23bSCédric Le Goater     case POWERPC_EXCP_DOORCI:   return "DOORCI";
646789f23bSCédric Le Goater     case POWERPC_EXCP_GDOORI:   return "GDOORI";
656789f23bSCédric Le Goater     case POWERPC_EXCP_GDOORCI:  return "GDOORCI";
666789f23bSCédric Le Goater     case POWERPC_EXCP_HYPPRIV:  return "HYPPRIV";
676789f23bSCédric Le Goater     case POWERPC_EXCP_RESET:    return "RESET";
686789f23bSCédric Le Goater     case POWERPC_EXCP_DSEG:     return "DSEG";
696789f23bSCédric Le Goater     case POWERPC_EXCP_ISEG:     return "ISEG";
706789f23bSCédric Le Goater     case POWERPC_EXCP_HDECR:    return "HDECR";
716789f23bSCédric Le Goater     case POWERPC_EXCP_TRACE:    return "TRACE";
726789f23bSCédric Le Goater     case POWERPC_EXCP_HDSI:     return "HDSI";
736789f23bSCédric Le Goater     case POWERPC_EXCP_HISI:     return "HISI";
746789f23bSCédric Le Goater     case POWERPC_EXCP_HDSEG:    return "HDSEG";
756789f23bSCédric Le Goater     case POWERPC_EXCP_HISEG:    return "HISEG";
766789f23bSCédric Le Goater     case POWERPC_EXCP_VPU:      return "VPU";
776789f23bSCédric Le Goater     case POWERPC_EXCP_PIT:      return "PIT";
786789f23bSCédric Le Goater     case POWERPC_EXCP_EMUL:     return "EMUL";
796789f23bSCédric Le Goater     case POWERPC_EXCP_IFTLB:    return "IFTLB";
806789f23bSCédric Le Goater     case POWERPC_EXCP_DLTLB:    return "DLTLB";
816789f23bSCédric Le Goater     case POWERPC_EXCP_DSTLB:    return "DSTLB";
826789f23bSCédric Le Goater     case POWERPC_EXCP_FPA:      return "FPA";
836789f23bSCédric Le Goater     case POWERPC_EXCP_DABR:     return "DABR";
846789f23bSCédric Le Goater     case POWERPC_EXCP_IABR:     return "IABR";
856789f23bSCédric Le Goater     case POWERPC_EXCP_SMI:      return "SMI";
866789f23bSCédric Le Goater     case POWERPC_EXCP_PERFM:    return "PERFM";
876789f23bSCédric Le Goater     case POWERPC_EXCP_THERM:    return "THERM";
886789f23bSCédric Le Goater     case POWERPC_EXCP_VPUA:     return "VPUA";
896789f23bSCédric Le Goater     case POWERPC_EXCP_SOFTP:    return "SOFTP";
906789f23bSCédric Le Goater     case POWERPC_EXCP_MAINT:    return "MAINT";
916789f23bSCédric Le Goater     case POWERPC_EXCP_MEXTBR:   return "MEXTBR";
926789f23bSCédric Le Goater     case POWERPC_EXCP_NMEXTBR:  return "NMEXTBR";
936789f23bSCédric Le Goater     case POWERPC_EXCP_ITLBE:    return "ITLBE";
946789f23bSCédric Le Goater     case POWERPC_EXCP_DTLBE:    return "DTLBE";
956789f23bSCédric Le Goater     case POWERPC_EXCP_VSXU:     return "VSXU";
966789f23bSCédric Le Goater     case POWERPC_EXCP_FU:       return "FU";
976789f23bSCédric Le Goater     case POWERPC_EXCP_HV_EMU:   return "HV_EMU";
986789f23bSCédric Le Goater     case POWERPC_EXCP_HV_MAINT: return "HV_MAINT";
996789f23bSCédric Le Goater     case POWERPC_EXCP_HV_FU:    return "HV_FU";
1006789f23bSCédric Le Goater     case POWERPC_EXCP_SDOOR:    return "SDOOR";
1016789f23bSCédric Le Goater     case POWERPC_EXCP_SDOOR_HV: return "SDOOR_HV";
1026789f23bSCédric Le Goater     case POWERPC_EXCP_HVIRT:    return "HVIRT";
1036789f23bSCédric Le Goater     case POWERPC_EXCP_SYSCALL_VECTORED: return "SYSCALL_VECTORED";
1046789f23bSCédric Le Goater     default:
1056789f23bSCédric Le Goater         g_assert_not_reached();
1066789f23bSCédric Le Goater     }
1076789f23bSCédric Le Goater }
1086789f23bSCédric Le Goater 
10962e79ef9SCédric Le Goater static void dump_syscall(CPUPPCState *env)
110c79c73f6SBlue Swirl {
1116dc6b557SNicholas Piggin     qemu_log_mask(CPU_LOG_INT, "syscall r0=%016" PRIx64
1126dc6b557SNicholas Piggin                   " r3=%016" PRIx64 " r4=%016" PRIx64 " r5=%016" PRIx64
1136dc6b557SNicholas Piggin                   " r6=%016" PRIx64 " r7=%016" PRIx64 " r8=%016" PRIx64
114c79c73f6SBlue Swirl                   " nip=" TARGET_FMT_lx "\n",
115c79c73f6SBlue Swirl                   ppc_dump_gpr(env, 0), ppc_dump_gpr(env, 3),
116c79c73f6SBlue Swirl                   ppc_dump_gpr(env, 4), ppc_dump_gpr(env, 5),
1176dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 6), ppc_dump_gpr(env, 7),
1186dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 8), env->nip);
1196dc6b557SNicholas Piggin }
1206dc6b557SNicholas Piggin 
12162e79ef9SCédric Le Goater static void dump_hcall(CPUPPCState *env)
1226dc6b557SNicholas Piggin {
1236dc6b557SNicholas Piggin     qemu_log_mask(CPU_LOG_INT, "hypercall r3=%016" PRIx64
1246dc6b557SNicholas Piggin                   " r4=%016" PRIx64 " r5=%016" PRIx64 " r6=%016" PRIx64
1256dc6b557SNicholas Piggin                   " r7=%016" PRIx64 " r8=%016" PRIx64 " r9=%016" PRIx64
1266dc6b557SNicholas Piggin                   " r10=%016" PRIx64 " r11=%016" PRIx64 " r12=%016" PRIx64
1276dc6b557SNicholas Piggin                   " nip=" TARGET_FMT_lx "\n",
1286dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 3), ppc_dump_gpr(env, 4),
1296dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 5), ppc_dump_gpr(env, 6),
1306dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 7), ppc_dump_gpr(env, 8),
1316dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 9), ppc_dump_gpr(env, 10),
1326dc6b557SNicholas Piggin                   ppc_dump_gpr(env, 11), ppc_dump_gpr(env, 12),
1336dc6b557SNicholas Piggin                   env->nip);
134c79c73f6SBlue Swirl }
135c79c73f6SBlue Swirl 
136e4e27df7SFabiano Rosas static void ppc_excp_debug_sw_tlb(CPUPPCState *env, int excp)
137e4e27df7SFabiano Rosas {
138e4e27df7SFabiano Rosas     const char *es;
139e4e27df7SFabiano Rosas     target_ulong *miss, *cmp;
140e4e27df7SFabiano Rosas     int en;
141e4e27df7SFabiano Rosas 
1422e089eceSFabiano Rosas     if (!qemu_loglevel_mask(CPU_LOG_MMU)) {
143e4e27df7SFabiano Rosas         return;
144e4e27df7SFabiano Rosas     }
145e4e27df7SFabiano Rosas 
146e4e27df7SFabiano Rosas     if (excp == POWERPC_EXCP_IFTLB) {
147e4e27df7SFabiano Rosas         es = "I";
148e4e27df7SFabiano Rosas         en = 'I';
149e4e27df7SFabiano Rosas         miss = &env->spr[SPR_IMISS];
150e4e27df7SFabiano Rosas         cmp = &env->spr[SPR_ICMP];
151e4e27df7SFabiano Rosas     } else {
152e4e27df7SFabiano Rosas         if (excp == POWERPC_EXCP_DLTLB) {
153e4e27df7SFabiano Rosas             es = "DL";
154e4e27df7SFabiano Rosas         } else {
155e4e27df7SFabiano Rosas             es = "DS";
156e4e27df7SFabiano Rosas         }
157e4e27df7SFabiano Rosas         en = 'D';
158e4e27df7SFabiano Rosas         miss = &env->spr[SPR_DMISS];
159e4e27df7SFabiano Rosas         cmp = &env->spr[SPR_DCMP];
160e4e27df7SFabiano Rosas     }
161e4e27df7SFabiano Rosas     qemu_log("6xx %sTLB miss: %cM " TARGET_FMT_lx " %cC "
162e4e27df7SFabiano Rosas              TARGET_FMT_lx " H1 " TARGET_FMT_lx " H2 "
163e4e27df7SFabiano Rosas              TARGET_FMT_lx " %08x\n", es, en, *miss, en, *cmp,
164e4e27df7SFabiano Rosas              env->spr[SPR_HASH1], env->spr[SPR_HASH2],
165e4e27df7SFabiano Rosas              env->error_code);
166e4e27df7SFabiano Rosas }
167e4e27df7SFabiano Rosas 
16828091374SFabiano Rosas #if defined(TARGET_PPC64)
169dead760bSBenjamin Herrenschmidt static int powerpc_reset_wakeup(CPUState *cs, CPUPPCState *env, int excp,
170dead760bSBenjamin Herrenschmidt                                 target_ulong *msr)
171dead760bSBenjamin Herrenschmidt {
172dead760bSBenjamin Herrenschmidt     /* We no longer are in a PM state */
1731e7fd61dSBenjamin Herrenschmidt     env->resume_as_sreset = false;
174dead760bSBenjamin Herrenschmidt 
175dead760bSBenjamin Herrenschmidt     /* Pretend to be returning from doze always as we don't lose state */
1760911a60cSLeonardo Bras     *msr |= SRR1_WS_NOLOSS;
177dead760bSBenjamin Herrenschmidt 
178dead760bSBenjamin Herrenschmidt     /* Machine checks are sent normally */
179dead760bSBenjamin Herrenschmidt     if (excp == POWERPC_EXCP_MCHECK) {
180dead760bSBenjamin Herrenschmidt         return excp;
181dead760bSBenjamin Herrenschmidt     }
182dead760bSBenjamin Herrenschmidt     switch (excp) {
183dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_RESET:
1840911a60cSLeonardo Bras         *msr |= SRR1_WAKERESET;
185dead760bSBenjamin Herrenschmidt         break;
186dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_EXTERNAL:
1870911a60cSLeonardo Bras         *msr |= SRR1_WAKEEE;
188dead760bSBenjamin Herrenschmidt         break;
189dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_DECR:
1900911a60cSLeonardo Bras         *msr |= SRR1_WAKEDEC;
191dead760bSBenjamin Herrenschmidt         break;
192dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_SDOOR:
1930911a60cSLeonardo Bras         *msr |= SRR1_WAKEDBELL;
194dead760bSBenjamin Herrenschmidt         break;
195dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_SDOOR_HV:
1960911a60cSLeonardo Bras         *msr |= SRR1_WAKEHDBELL;
197dead760bSBenjamin Herrenschmidt         break;
198dead760bSBenjamin Herrenschmidt     case POWERPC_EXCP_HV_MAINT:
1990911a60cSLeonardo Bras         *msr |= SRR1_WAKEHMI;
200dead760bSBenjamin Herrenschmidt         break;
201d8ce5fd6SBenjamin Herrenschmidt     case POWERPC_EXCP_HVIRT:
2020911a60cSLeonardo Bras         *msr |= SRR1_WAKEHVI;
203d8ce5fd6SBenjamin Herrenschmidt         break;
204dead760bSBenjamin Herrenschmidt     default:
205dead760bSBenjamin Herrenschmidt         cpu_abort(cs, "Unsupported exception %d in Power Save mode\n",
206dead760bSBenjamin Herrenschmidt                   excp);
207dead760bSBenjamin Herrenschmidt     }
208dead760bSBenjamin Herrenschmidt     return POWERPC_EXCP_RESET;
209dead760bSBenjamin Herrenschmidt }
210dead760bSBenjamin Herrenschmidt 
2118b7e6b07SNicholas Piggin /*
2128b7e6b07SNicholas Piggin  * AIL - Alternate Interrupt Location, a mode that allows interrupts to be
2138b7e6b07SNicholas Piggin  * taken with the MMU on, and which uses an alternate location (e.g., so the
2148b7e6b07SNicholas Piggin  * kernel/hv can map the vectors there with an effective address).
2158b7e6b07SNicholas Piggin  *
2168b7e6b07SNicholas Piggin  * An interrupt is considered to be taken "with AIL" or "AIL applies" if they
2178b7e6b07SNicholas Piggin  * are delivered in this way. AIL requires the LPCR to be set to enable this
2188b7e6b07SNicholas Piggin  * mode, and then a number of conditions have to be true for AIL to apply.
2198b7e6b07SNicholas Piggin  *
2208b7e6b07SNicholas Piggin  * First of all, SRESET, MCE, and HMI are always delivered without AIL, because
2218b7e6b07SNicholas Piggin  * they specifically want to be in real mode (e.g., the MCE might be signaling
2228b7e6b07SNicholas Piggin  * a SLB multi-hit which requires SLB flush before the MMU can be enabled).
2238b7e6b07SNicholas Piggin  *
2248b7e6b07SNicholas Piggin  * After that, behaviour depends on the current MSR[IR], MSR[DR], MSR[HV],
2258b7e6b07SNicholas Piggin  * whether or not the interrupt changes MSR[HV] from 0 to 1, and the current
2268b7e6b07SNicholas Piggin  * radix mode (LPCR[HR]).
2278b7e6b07SNicholas Piggin  *
2288b7e6b07SNicholas Piggin  * POWER8, POWER9 with LPCR[HR]=0
2298b7e6b07SNicholas Piggin  * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
2308b7e6b07SNicholas Piggin  * +-----------+-------------+---------+-------------+-----+
2318b7e6b07SNicholas Piggin  * | a         | 00/01/10    | x       | x           | 0   |
2328b7e6b07SNicholas Piggin  * | a         | 11          | 0       | 1           | 0   |
2338b7e6b07SNicholas Piggin  * | a         | 11          | 1       | 1           | a   |
2348b7e6b07SNicholas Piggin  * | a         | 11          | 0       | 0           | a   |
2358b7e6b07SNicholas Piggin  * +-------------------------------------------------------+
2368b7e6b07SNicholas Piggin  *
2378b7e6b07SNicholas Piggin  * POWER9 with LPCR[HR]=1
2388b7e6b07SNicholas Piggin  * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
2398b7e6b07SNicholas Piggin  * +-----------+-------------+---------+-------------+-----+
2408b7e6b07SNicholas Piggin  * | a         | 00/01/10    | x       | x           | 0   |
2418b7e6b07SNicholas Piggin  * | a         | 11          | x       | x           | a   |
2428b7e6b07SNicholas Piggin  * +-------------------------------------------------------+
2438b7e6b07SNicholas Piggin  *
2448b7e6b07SNicholas Piggin  * The difference with POWER9 being that MSR[HV] 0->1 interrupts can be sent to
245526cdce7SNicholas Piggin  * the hypervisor in AIL mode if the guest is radix. This is good for
246526cdce7SNicholas Piggin  * performance but allows the guest to influence the AIL of hypervisor
247526cdce7SNicholas Piggin  * interrupts using its MSR, and also the hypervisor must disallow guest
248526cdce7SNicholas Piggin  * interrupts (MSR[HV] 0->0) from using AIL if the hypervisor does not want to
249526cdce7SNicholas Piggin  * use AIL for its MSR[HV] 0->1 interrupts.
250526cdce7SNicholas Piggin  *
251526cdce7SNicholas Piggin  * POWER10 addresses those issues with a new LPCR[HAIL] bit that is applied to
252526cdce7SNicholas Piggin  * interrupts that begin execution with MSR[HV]=1 (so both MSR[HV] 0->1 and
253526cdce7SNicholas Piggin  * MSR[HV] 1->1).
254526cdce7SNicholas Piggin  *
255526cdce7SNicholas Piggin  * HAIL=1 is equivalent to AIL=3, for interrupts delivered with MSR[HV]=1.
256526cdce7SNicholas Piggin  *
257526cdce7SNicholas Piggin  * POWER10 behaviour is
258526cdce7SNicholas Piggin  * | LPCR[AIL] | LPCR[HAIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
259526cdce7SNicholas Piggin  * +-----------+------------+-------------+---------+-------------+-----+
260526cdce7SNicholas Piggin  * | a         | h          | 00/01/10    | 0       | 0           | 0   |
261526cdce7SNicholas Piggin  * | a         | h          | 11          | 0       | 0           | a   |
262526cdce7SNicholas Piggin  * | a         | h          | x           | 0       | 1           | h   |
263526cdce7SNicholas Piggin  * | a         | h          | 00/01/10    | 1       | 1           | 0   |
264526cdce7SNicholas Piggin  * | a         | h          | 11          | 1       | 1           | h   |
265526cdce7SNicholas Piggin  * +--------------------------------------------------------------------+
2668b7e6b07SNicholas Piggin  */
26710895ab6SFabiano Rosas static void ppc_excp_apply_ail(PowerPCCPU *cpu, int excp, target_ulong msr,
26810895ab6SFabiano Rosas                                target_ulong *new_msr, target_ulong *vector)
2692586a4d7SFabiano Rosas {
27010895ab6SFabiano Rosas     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
2718b7e6b07SNicholas Piggin     CPUPPCState *env = &cpu->env;
2728b7e6b07SNicholas Piggin     bool mmu_all_on = ((msr >> MSR_IR) & 1) && ((msr >> MSR_DR) & 1);
2738b7e6b07SNicholas Piggin     bool hv_escalation = !(msr & MSR_HVB) && (*new_msr & MSR_HVB);
2748b7e6b07SNicholas Piggin     int ail = 0;
2752586a4d7SFabiano Rosas 
2768b7e6b07SNicholas Piggin     if (excp == POWERPC_EXCP_MCHECK ||
2778b7e6b07SNicholas Piggin         excp == POWERPC_EXCP_RESET ||
2788b7e6b07SNicholas Piggin         excp == POWERPC_EXCP_HV_MAINT) {
2798b7e6b07SNicholas Piggin         /* SRESET, MCE, HMI never apply AIL */
2808b7e6b07SNicholas Piggin         return;
2812586a4d7SFabiano Rosas     }
2822586a4d7SFabiano Rosas 
28310895ab6SFabiano Rosas     if (!(pcc->lpcr_mask & LPCR_AIL)) {
28410895ab6SFabiano Rosas         /* This CPU does not have AIL */
28510895ab6SFabiano Rosas         return;
28610895ab6SFabiano Rosas     }
28710895ab6SFabiano Rosas 
28810895ab6SFabiano Rosas     /* P8 & P9 */
28910895ab6SFabiano Rosas     if (!(pcc->lpcr_mask & LPCR_HAIL)) {
2908b7e6b07SNicholas Piggin         if (!mmu_all_on) {
2918b7e6b07SNicholas Piggin             /* AIL only works if MSR[IR] and MSR[DR] are both enabled. */
2928b7e6b07SNicholas Piggin             return;
2938b7e6b07SNicholas Piggin         }
2948b7e6b07SNicholas Piggin         if (hv_escalation && !(env->spr[SPR_LPCR] & LPCR_HR)) {
2958b7e6b07SNicholas Piggin             /*
2968b7e6b07SNicholas Piggin              * AIL does not work if there is a MSR[HV] 0->1 transition and the
2978b7e6b07SNicholas Piggin              * partition is in HPT mode. For radix guests, such interrupts are
2988b7e6b07SNicholas Piggin              * allowed to be delivered to the hypervisor in ail mode.
2998b7e6b07SNicholas Piggin              */
3008b7e6b07SNicholas Piggin             return;
3018b7e6b07SNicholas Piggin         }
3028b7e6b07SNicholas Piggin 
3038b7e6b07SNicholas Piggin         ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT;
3048b7e6b07SNicholas Piggin         if (ail == 0) {
3058b7e6b07SNicholas Piggin             return;
3068b7e6b07SNicholas Piggin         }
3078b7e6b07SNicholas Piggin         if (ail == 1) {
3088b7e6b07SNicholas Piggin             /* AIL=1 is reserved, treat it like AIL=0 */
3098b7e6b07SNicholas Piggin             return;
3108b7e6b07SNicholas Piggin         }
311526cdce7SNicholas Piggin 
31210895ab6SFabiano Rosas     /* P10 and up */
31310895ab6SFabiano Rosas     } else {
314526cdce7SNicholas Piggin         if (!mmu_all_on && !hv_escalation) {
315526cdce7SNicholas Piggin             /*
316526cdce7SNicholas Piggin              * AIL works for HV interrupts even with guest MSR[IR/DR] disabled.
317526cdce7SNicholas Piggin              * Guest->guest and HV->HV interrupts do require MMU on.
318526cdce7SNicholas Piggin              */
319526cdce7SNicholas Piggin             return;
320526cdce7SNicholas Piggin         }
321526cdce7SNicholas Piggin 
322526cdce7SNicholas Piggin         if (*new_msr & MSR_HVB) {
323526cdce7SNicholas Piggin             if (!(env->spr[SPR_LPCR] & LPCR_HAIL)) {
324526cdce7SNicholas Piggin                 /* HV interrupts depend on LPCR[HAIL] */
325526cdce7SNicholas Piggin                 return;
326526cdce7SNicholas Piggin             }
327526cdce7SNicholas Piggin             ail = 3; /* HAIL=1 gives AIL=3 behaviour for HV interrupts */
328526cdce7SNicholas Piggin         } else {
329526cdce7SNicholas Piggin             ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT;
330526cdce7SNicholas Piggin         }
331526cdce7SNicholas Piggin         if (ail == 0) {
332526cdce7SNicholas Piggin             return;
333526cdce7SNicholas Piggin         }
334526cdce7SNicholas Piggin         if (ail == 1 || ail == 2) {
335526cdce7SNicholas Piggin             /* AIL=1 and AIL=2 are reserved, treat them like AIL=0 */
336526cdce7SNicholas Piggin             return;
337526cdce7SNicholas Piggin         }
3388b7e6b07SNicholas Piggin     }
3398b7e6b07SNicholas Piggin 
3408b7e6b07SNicholas Piggin     /*
3418b7e6b07SNicholas Piggin      * AIL applies, so the new MSR gets IR and DR set, and an offset applied
3428b7e6b07SNicholas Piggin      * to the new IP.
3438b7e6b07SNicholas Piggin      */
3448b7e6b07SNicholas Piggin     *new_msr |= (1 << MSR_IR) | (1 << MSR_DR);
3458b7e6b07SNicholas Piggin 
3468b7e6b07SNicholas Piggin     if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
3478b7e6b07SNicholas Piggin         if (ail == 2) {
3488b7e6b07SNicholas Piggin             *vector |= 0x0000000000018000ull;
3498b7e6b07SNicholas Piggin         } else if (ail == 3) {
3508b7e6b07SNicholas Piggin             *vector |= 0xc000000000004000ull;
3518b7e6b07SNicholas Piggin         }
3528b7e6b07SNicholas Piggin     } else {
3538b7e6b07SNicholas Piggin         /*
3548b7e6b07SNicholas Piggin          * scv AIL is a little different. AIL=2 does not change the address,
3558b7e6b07SNicholas Piggin          * only the MSR. AIL=3 replaces the 0x17000 base with 0xc...3000.
3568b7e6b07SNicholas Piggin          */
3578b7e6b07SNicholas Piggin         if (ail == 3) {
3588b7e6b07SNicholas Piggin             *vector &= ~0x0000000000017000ull; /* Un-apply the base offset */
3598b7e6b07SNicholas Piggin             *vector |= 0xc000000000003000ull; /* Apply scv's AIL=3 offset */
3608b7e6b07SNicholas Piggin         }
3618b7e6b07SNicholas Piggin     }
3622586a4d7SFabiano Rosas }
36328091374SFabiano Rosas #endif
364dead760bSBenjamin Herrenschmidt 
3653680e994SNicholas Piggin static void powerpc_reset_excp_state(PowerPCCPU *cpu)
366ad77c6caSNicholas Piggin {
367ad77c6caSNicholas Piggin     CPUState *cs = CPU(cpu);
368ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
369ad77c6caSNicholas Piggin 
3703680e994SNicholas Piggin     /* Reset exception state */
3713680e994SNicholas Piggin     cs->exception_index = POWERPC_EXCP_NONE;
3723680e994SNicholas Piggin     env->error_code = 0;
3733680e994SNicholas Piggin }
3743680e994SNicholas Piggin 
3753680e994SNicholas Piggin static void powerpc_set_excp_state(PowerPCCPU *cpu, target_ulong vector,
3763680e994SNicholas Piggin                                    target_ulong msr)
3773680e994SNicholas Piggin {
3783680e994SNicholas Piggin     CPUPPCState *env = &cpu->env;
3793680e994SNicholas Piggin 
380fce9fbafSFabiano Rosas     assert((msr & env->msr_mask) == msr);
381fce9fbafSFabiano Rosas 
382ad77c6caSNicholas Piggin     /*
383ad77c6caSNicholas Piggin      * We don't use hreg_store_msr here as already have treated any
384ad77c6caSNicholas Piggin      * special case that could occur. Just store MSR and update hflags
385ad77c6caSNicholas Piggin      *
386ad77c6caSNicholas Piggin      * Note: We *MUST* not use hreg_store_msr() as-is anyway because it
387ad77c6caSNicholas Piggin      * will prevent setting of the HV bit which some exceptions might need
388ad77c6caSNicholas Piggin      * to do.
389ad77c6caSNicholas Piggin      */
3903680e994SNicholas Piggin     env->nip = vector;
391fce9fbafSFabiano Rosas     env->msr = msr;
392ad77c6caSNicholas Piggin     hreg_compute_hflags(env);
3932fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
394ad77c6caSNicholas Piggin 
3953680e994SNicholas Piggin     powerpc_reset_excp_state(cpu);
396ad77c6caSNicholas Piggin 
397ad77c6caSNicholas Piggin     /*
398ad77c6caSNicholas Piggin      * Any interrupt is context synchronizing, check if TCG TLB needs
399ad77c6caSNicholas Piggin      * a delayed flush on ppc64
400ad77c6caSNicholas Piggin      */
401ad77c6caSNicholas Piggin     check_tlb_flush(env, false);
4023680e994SNicholas Piggin 
4033680e994SNicholas Piggin     /* Reset the reservation */
4043680e994SNicholas Piggin     env->reserve_addr = -1;
405ad77c6caSNicholas Piggin }
406ad77c6caSNicholas Piggin 
407e808c2edSFabiano Rosas static void powerpc_excp_40x(PowerPCCPU *cpu, int excp)
408e808c2edSFabiano Rosas {
409e808c2edSFabiano Rosas     CPUState *cs = CPU(cpu);
410e808c2edSFabiano Rosas     CPUPPCState *env = &cpu->env;
411e808c2edSFabiano Rosas     target_ulong msr, new_msr, vector;
4128428cdb2SFabiano Rosas     int srr0, srr1;
413e808c2edSFabiano Rosas 
414e808c2edSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
415e808c2edSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
416e808c2edSFabiano Rosas 
417e808c2edSFabiano Rosas     /*
418495fc7ffSFabiano Rosas      * new interrupt handler msr preserves existing ME unless
419495fc7ffSFabiano Rosas      * explicitly overriden.
420e808c2edSFabiano Rosas      */
421495fc7ffSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME));
422e808c2edSFabiano Rosas 
423e808c2edSFabiano Rosas     /* target registers */
424e808c2edSFabiano Rosas     srr0 = SPR_SRR0;
425e808c2edSFabiano Rosas     srr1 = SPR_SRR1;
426e808c2edSFabiano Rosas 
427e808c2edSFabiano Rosas     /*
428e808c2edSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
429495fc7ffSFabiano Rosas      * arch 2.05 server or later.
430e808c2edSFabiano Rosas      */
431495fc7ffSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
432e808c2edSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
433e808c2edSFabiano Rosas     }
434e808c2edSFabiano Rosas 
435e808c2edSFabiano Rosas     vector = env->excp_vectors[excp];
436e808c2edSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
437e808c2edSFabiano Rosas         cpu_abort(cs, "Raised an exception without defined vector %d\n",
438e808c2edSFabiano Rosas                   excp);
439e808c2edSFabiano Rosas     }
440e808c2edSFabiano Rosas 
441e808c2edSFabiano Rosas     vector |= env->excp_prefix;
442e808c2edSFabiano Rosas 
443e808c2edSFabiano Rosas     switch (excp) {
444e808c2edSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
445e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
446e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
447e808c2edSFabiano Rosas         break;
448e808c2edSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
449c354d858SVíctor Colombo         if (!FIELD_EX64(env->msr, MSR, ME)) {
450e808c2edSFabiano Rosas             /*
451e808c2edSFabiano Rosas              * Machine check exception is not enabled.  Enter
452e808c2edSFabiano Rosas              * checkstop state.
453e808c2edSFabiano Rosas              */
454e808c2edSFabiano Rosas             fprintf(stderr, "Machine check while not allowed. "
455e808c2edSFabiano Rosas                     "Entering checkstop state\n");
456e808c2edSFabiano Rosas             if (qemu_log_separate()) {
457e808c2edSFabiano Rosas                 qemu_log("Machine check while not allowed. "
458e808c2edSFabiano Rosas                         "Entering checkstop state\n");
459e808c2edSFabiano Rosas             }
460e808c2edSFabiano Rosas             cs->halted = 1;
461e808c2edSFabiano Rosas             cpu_interrupt_exittb(cs);
462e808c2edSFabiano Rosas         }
463e808c2edSFabiano Rosas 
464e808c2edSFabiano Rosas         /* machine check exceptions don't have ME set */
465e808c2edSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
466e808c2edSFabiano Rosas 
467e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
468e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
469e808c2edSFabiano Rosas         break;
470e808c2edSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
471f9911e1eSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_40x_ESR], env->spr[SPR_40x_DEAR]);
472e808c2edSFabiano Rosas         break;
473e808c2edSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
474e808c2edSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
475e808c2edSFabiano Rosas         break;
476e808c2edSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
477e808c2edSFabiano Rosas         break;
478e808c2edSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
479e808c2edSFabiano Rosas         break;
480e808c2edSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
481e808c2edSFabiano Rosas         switch (env->error_code & ~0xF) {
482e808c2edSFabiano Rosas         case POWERPC_EXCP_FP:
483da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
484e808c2edSFabiano Rosas                 trace_ppc_excp_fp_ignore();
4853680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
486e808c2edSFabiano Rosas                 return;
487e808c2edSFabiano Rosas             }
48864e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_FP;
489e808c2edSFabiano Rosas             break;
490e808c2edSFabiano Rosas         case POWERPC_EXCP_INVAL:
491e808c2edSFabiano Rosas             trace_ppc_excp_inval(env->nip);
49264e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PIL;
493e808c2edSFabiano Rosas             break;
494e808c2edSFabiano Rosas         case POWERPC_EXCP_PRIV:
49564e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PPR;
496e808c2edSFabiano Rosas             break;
497e808c2edSFabiano Rosas         case POWERPC_EXCP_TRAP:
49864e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PTR;
499e808c2edSFabiano Rosas             break;
500e808c2edSFabiano Rosas         default:
501e808c2edSFabiano Rosas             cpu_abort(cs, "Invalid program exception %d. Aborting\n",
502e808c2edSFabiano Rosas                       env->error_code);
503e808c2edSFabiano Rosas             break;
504e808c2edSFabiano Rosas         }
505e808c2edSFabiano Rosas         break;
506e808c2edSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
507e808c2edSFabiano Rosas         dump_syscall(env);
508e808c2edSFabiano Rosas 
509e808c2edSFabiano Rosas         /*
510e808c2edSFabiano Rosas          * We need to correct the NIP which in this case is supposed
511e808c2edSFabiano Rosas          * to point to the next instruction
512e808c2edSFabiano Rosas          */
513e808c2edSFabiano Rosas         env->nip += 4;
514e808c2edSFabiano Rosas         break;
515e808c2edSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
516e808c2edSFabiano Rosas         trace_ppc_excp_print("FIT");
517e808c2edSFabiano Rosas         break;
518e808c2edSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
519e808c2edSFabiano Rosas         trace_ppc_excp_print("WDT");
520e808c2edSFabiano Rosas         break;
521e808c2edSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
522e808c2edSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
523e808c2edSFabiano Rosas         break;
524e808c2edSFabiano Rosas     case POWERPC_EXCP_PIT:       /* Programmable interval timer interrupt    */
525e808c2edSFabiano Rosas         trace_ppc_excp_print("PIT");
526e808c2edSFabiano Rosas         break;
5274d8ac1d1SFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
5284d8ac1d1SFabiano Rosas         cpu_abort(cs, "%s exception not implemented\n",
5294d8ac1d1SFabiano Rosas                   powerpc_excp_name(excp));
5304d8ac1d1SFabiano Rosas         break;
531e808c2edSFabiano Rosas     default:
532e808c2edSFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
533e808c2edSFabiano Rosas         break;
534e808c2edSFabiano Rosas     }
535e808c2edSFabiano Rosas 
536e808c2edSFabiano Rosas     /* Save PC */
537e808c2edSFabiano Rosas     env->spr[srr0] = env->nip;
538e808c2edSFabiano Rosas 
539e808c2edSFabiano Rosas     /* Save MSR */
540e808c2edSFabiano Rosas     env->spr[srr1] = msr;
541e808c2edSFabiano Rosas 
542e808c2edSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
543e808c2edSFabiano Rosas }
544e808c2edSFabiano Rosas 
54558d178fbSFabiano Rosas static void powerpc_excp_6xx(PowerPCCPU *cpu, int excp)
54658d178fbSFabiano Rosas {
54758d178fbSFabiano Rosas     CPUState *cs = CPU(cpu);
54858d178fbSFabiano Rosas     CPUPPCState *env = &cpu->env;
54958d178fbSFabiano Rosas     target_ulong msr, new_msr, vector;
55058d178fbSFabiano Rosas 
55158d178fbSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
55258d178fbSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
55358d178fbSFabiano Rosas 
55458d178fbSFabiano Rosas     /*
555082d783bSFabiano Rosas      * new interrupt handler msr preserves existing ME unless
55658d178fbSFabiano Rosas      * explicitly overriden
55758d178fbSFabiano Rosas      */
558082d783bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
55958d178fbSFabiano Rosas 
56058d178fbSFabiano Rosas     /*
56158d178fbSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
562082d783bSFabiano Rosas      * arch 2.05 server or later.
56358d178fbSFabiano Rosas      */
564082d783bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
56558d178fbSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
56658d178fbSFabiano Rosas     }
56758d178fbSFabiano Rosas 
56858d178fbSFabiano Rosas     vector = env->excp_vectors[excp];
56958d178fbSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
57058d178fbSFabiano Rosas         cpu_abort(cs, "Raised an exception without defined vector %d\n",
57158d178fbSFabiano Rosas                   excp);
57258d178fbSFabiano Rosas     }
57358d178fbSFabiano Rosas 
57458d178fbSFabiano Rosas     vector |= env->excp_prefix;
57558d178fbSFabiano Rosas 
57658d178fbSFabiano Rosas     switch (excp) {
57758d178fbSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
57858d178fbSFabiano Rosas         break;
57958d178fbSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
580c354d858SVíctor Colombo         if (!FIELD_EX64(env->msr, MSR, ME)) {
58158d178fbSFabiano Rosas             /*
58258d178fbSFabiano Rosas              * Machine check exception is not enabled.  Enter
58358d178fbSFabiano Rosas              * checkstop state.
58458d178fbSFabiano Rosas              */
58558d178fbSFabiano Rosas             fprintf(stderr, "Machine check while not allowed. "
58658d178fbSFabiano Rosas                     "Entering checkstop state\n");
58758d178fbSFabiano Rosas             if (qemu_log_separate()) {
58858d178fbSFabiano Rosas                 qemu_log("Machine check while not allowed. "
58958d178fbSFabiano Rosas                         "Entering checkstop state\n");
59058d178fbSFabiano Rosas             }
59158d178fbSFabiano Rosas             cs->halted = 1;
59258d178fbSFabiano Rosas             cpu_interrupt_exittb(cs);
59358d178fbSFabiano Rosas         }
59458d178fbSFabiano Rosas 
59558d178fbSFabiano Rosas         /* machine check exceptions don't have ME set */
59658d178fbSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
59758d178fbSFabiano Rosas 
59858d178fbSFabiano Rosas         break;
59958d178fbSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
60058d178fbSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
60158d178fbSFabiano Rosas         break;
60258d178fbSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
60358d178fbSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
60458d178fbSFabiano Rosas         msr |= env->error_code;
60558d178fbSFabiano Rosas         break;
60658d178fbSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
60758d178fbSFabiano Rosas         break;
60858d178fbSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
60958d178fbSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
61058d178fbSFabiano Rosas         /*
61158d178fbSFabiano Rosas          * Note: the opcode fields will not be set properly for a
61258d178fbSFabiano Rosas          * direct store load/store, but nobody cares as nobody
61358d178fbSFabiano Rosas          * actually uses direct store segments.
61458d178fbSFabiano Rosas          */
61558d178fbSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
61658d178fbSFabiano Rosas         break;
61758d178fbSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
61858d178fbSFabiano Rosas         switch (env->error_code & ~0xF) {
61958d178fbSFabiano Rosas         case POWERPC_EXCP_FP:
620da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
62158d178fbSFabiano Rosas                 trace_ppc_excp_fp_ignore();
6223680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
62358d178fbSFabiano Rosas                 return;
62458d178fbSFabiano Rosas             }
62558d178fbSFabiano Rosas 
62658d178fbSFabiano Rosas             /*
62758d178fbSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
62858d178fbSFabiano Rosas              * instruction, so always use store_next and claim we are
62958d178fbSFabiano Rosas              * precise in the MSR.
63058d178fbSFabiano Rosas              */
63158d178fbSFabiano Rosas             msr |= 0x00100000;
63258d178fbSFabiano Rosas             break;
63358d178fbSFabiano Rosas         case POWERPC_EXCP_INVAL:
63458d178fbSFabiano Rosas             trace_ppc_excp_inval(env->nip);
63558d178fbSFabiano Rosas             msr |= 0x00080000;
63658d178fbSFabiano Rosas             break;
63758d178fbSFabiano Rosas         case POWERPC_EXCP_PRIV:
63858d178fbSFabiano Rosas             msr |= 0x00040000;
63958d178fbSFabiano Rosas             break;
64058d178fbSFabiano Rosas         case POWERPC_EXCP_TRAP:
64158d178fbSFabiano Rosas             msr |= 0x00020000;
64258d178fbSFabiano Rosas             break;
64358d178fbSFabiano Rosas         default:
64458d178fbSFabiano Rosas             /* Should never occur */
64558d178fbSFabiano Rosas             cpu_abort(cs, "Invalid program exception %d. Aborting\n",
64658d178fbSFabiano Rosas                       env->error_code);
64758d178fbSFabiano Rosas             break;
64858d178fbSFabiano Rosas         }
64958d178fbSFabiano Rosas         break;
65058d178fbSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
65158d178fbSFabiano Rosas         dump_syscall(env);
65258d178fbSFabiano Rosas 
65358d178fbSFabiano Rosas         /*
65458d178fbSFabiano Rosas          * We need to correct the NIP which in this case is supposed
65558d178fbSFabiano Rosas          * to point to the next instruction
65658d178fbSFabiano Rosas          */
65758d178fbSFabiano Rosas         env->nip += 4;
65858d178fbSFabiano Rosas         break;
65958d178fbSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
66058d178fbSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
66158d178fbSFabiano Rosas         break;
66258d178fbSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
66358d178fbSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
66458d178fbSFabiano Rosas         break;
66558d178fbSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
6668e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
66758d178fbSFabiano Rosas             cpu_abort(cs, "Trying to deliver power-saving system reset "
66858d178fbSFabiano Rosas                       "exception %d with no HV support\n", excp);
66958d178fbSFabiano Rosas         }
67058d178fbSFabiano Rosas         break;
67158d178fbSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
67258d178fbSFabiano Rosas         break;
67358d178fbSFabiano Rosas     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
67458d178fbSFabiano Rosas     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
67558d178fbSFabiano Rosas     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
67658d178fbSFabiano Rosas         /* Swap temporary saved registers with GPRs */
67758d178fbSFabiano Rosas         if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
67858d178fbSFabiano Rosas             new_msr |= (target_ulong)1 << MSR_TGPR;
67958d178fbSFabiano Rosas             hreg_swap_gpr_tgpr(env);
68058d178fbSFabiano Rosas         }
6818f8c7932SFabiano Rosas 
68258d178fbSFabiano Rosas         ppc_excp_debug_sw_tlb(env, excp);
68358d178fbSFabiano Rosas 
68458d178fbSFabiano Rosas         msr |= env->crf[0] << 28;
68558d178fbSFabiano Rosas         msr |= env->error_code; /* key, D/I, S/L bits */
68658d178fbSFabiano Rosas         /* Set way using a LRU mechanism */
68758d178fbSFabiano Rosas         msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
68858d178fbSFabiano Rosas         break;
68958d178fbSFabiano Rosas     case POWERPC_EXCP_FPA:       /* Floating-point assist exception          */
69058d178fbSFabiano Rosas     case POWERPC_EXCP_DABR:      /* Data address breakpoint                  */
69158d178fbSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
69258d178fbSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
69358d178fbSFabiano Rosas     case POWERPC_EXCP_MEXTBR:    /* Maskable external breakpoint             */
69458d178fbSFabiano Rosas     case POWERPC_EXCP_NMEXTBR:   /* Non maskable external breakpoint         */
69558d178fbSFabiano Rosas         cpu_abort(cs, "%s exception not implemented\n",
69658d178fbSFabiano Rosas                   powerpc_excp_name(excp));
69758d178fbSFabiano Rosas         break;
69858d178fbSFabiano Rosas     default:
69958d178fbSFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
70058d178fbSFabiano Rosas         break;
70158d178fbSFabiano Rosas     }
70258d178fbSFabiano Rosas 
70358d178fbSFabiano Rosas     /*
70458d178fbSFabiano Rosas      * Sort out endianness of interrupt, this differs depending on the
70558d178fbSFabiano Rosas      * CPU, the HV mode, etc...
70658d178fbSFabiano Rosas      */
70758d178fbSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
70858d178fbSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
70958d178fbSFabiano Rosas     }
71058d178fbSFabiano Rosas 
71158d178fbSFabiano Rosas     /* Save PC */
712c50eaed1SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
71358d178fbSFabiano Rosas 
71458d178fbSFabiano Rosas     /* Save MSR */
715c50eaed1SFabiano Rosas     env->spr[SPR_SRR1] = msr;
71658d178fbSFabiano Rosas 
71758d178fbSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
71858d178fbSFabiano Rosas }
71958d178fbSFabiano Rosas 
720ccfca2fcSFabiano Rosas static void powerpc_excp_7xx(PowerPCCPU *cpu, int excp)
721ccfca2fcSFabiano Rosas {
722ccfca2fcSFabiano Rosas     CPUState *cs = CPU(cpu);
723ccfca2fcSFabiano Rosas     CPUPPCState *env = &cpu->env;
724ccfca2fcSFabiano Rosas     target_ulong msr, new_msr, vector;
725ccfca2fcSFabiano Rosas 
726ccfca2fcSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
727ccfca2fcSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
728ccfca2fcSFabiano Rosas 
729ccfca2fcSFabiano Rosas     /*
73093848d6aSFabiano Rosas      * new interrupt handler msr preserves existing ME unless
731ccfca2fcSFabiano Rosas      * explicitly overriden
732ccfca2fcSFabiano Rosas      */
73393848d6aSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
734ccfca2fcSFabiano Rosas 
735ccfca2fcSFabiano Rosas     /*
736ccfca2fcSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
73793848d6aSFabiano Rosas      * arch 2.05 server or later.
738ccfca2fcSFabiano Rosas      */
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) {
745ccfca2fcSFabiano Rosas         cpu_abort(cs, "Raised an exception without defined vector %d\n",
746ccfca2fcSFabiano Rosas                   excp);
747ccfca2fcSFabiano Rosas     }
748ccfca2fcSFabiano Rosas 
749ccfca2fcSFabiano Rosas     vector |= env->excp_prefix;
750ccfca2fcSFabiano Rosas 
751ccfca2fcSFabiano Rosas     switch (excp) {
752ccfca2fcSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
753c354d858SVíctor Colombo         if (!FIELD_EX64(env->msr, MSR, ME)) {
754ccfca2fcSFabiano Rosas             /*
755ccfca2fcSFabiano Rosas              * Machine check exception is not enabled.  Enter
756ccfca2fcSFabiano Rosas              * checkstop state.
757ccfca2fcSFabiano Rosas              */
758ccfca2fcSFabiano Rosas             fprintf(stderr, "Machine check while not allowed. "
759ccfca2fcSFabiano Rosas                     "Entering checkstop state\n");
760ccfca2fcSFabiano Rosas             if (qemu_log_separate()) {
761ccfca2fcSFabiano Rosas                 qemu_log("Machine check while not allowed. "
762ccfca2fcSFabiano Rosas                         "Entering checkstop state\n");
763ccfca2fcSFabiano Rosas             }
764ccfca2fcSFabiano Rosas             cs->halted = 1;
765ccfca2fcSFabiano Rosas             cpu_interrupt_exittb(cs);
766ccfca2fcSFabiano Rosas         }
767ccfca2fcSFabiano Rosas 
768ccfca2fcSFabiano Rosas         /* machine check exceptions don't have ME set */
769ccfca2fcSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
770ccfca2fcSFabiano Rosas 
771ccfca2fcSFabiano Rosas         break;
772ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
773ccfca2fcSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
774ccfca2fcSFabiano Rosas         break;
775ccfca2fcSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
776ccfca2fcSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
777ccfca2fcSFabiano Rosas         msr |= env->error_code;
778ccfca2fcSFabiano Rosas         break;
779ccfca2fcSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
780ccfca2fcSFabiano Rosas         break;
781ccfca2fcSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
782ccfca2fcSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
783ccfca2fcSFabiano Rosas         /*
784ccfca2fcSFabiano Rosas          * Note: the opcode fields will not be set properly for a
785ccfca2fcSFabiano Rosas          * direct store load/store, but nobody cares as nobody
786ccfca2fcSFabiano Rosas          * actually uses direct store segments.
787ccfca2fcSFabiano Rosas          */
788ccfca2fcSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
789ccfca2fcSFabiano Rosas         break;
790ccfca2fcSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
791ccfca2fcSFabiano Rosas         switch (env->error_code & ~0xF) {
792ccfca2fcSFabiano Rosas         case POWERPC_EXCP_FP:
793da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
794ccfca2fcSFabiano Rosas                 trace_ppc_excp_fp_ignore();
7953680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
796ccfca2fcSFabiano Rosas                 return;
797ccfca2fcSFabiano Rosas             }
798ccfca2fcSFabiano Rosas 
799ccfca2fcSFabiano Rosas             /*
800ccfca2fcSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
801ccfca2fcSFabiano Rosas              * instruction, so always use store_next and claim we are
802ccfca2fcSFabiano Rosas              * precise in the MSR.
803ccfca2fcSFabiano Rosas              */
804ccfca2fcSFabiano Rosas             msr |= 0x00100000;
805ccfca2fcSFabiano Rosas             break;
806ccfca2fcSFabiano Rosas         case POWERPC_EXCP_INVAL:
807ccfca2fcSFabiano Rosas             trace_ppc_excp_inval(env->nip);
808ccfca2fcSFabiano Rosas             msr |= 0x00080000;
809ccfca2fcSFabiano Rosas             break;
810ccfca2fcSFabiano Rosas         case POWERPC_EXCP_PRIV:
811ccfca2fcSFabiano Rosas             msr |= 0x00040000;
812ccfca2fcSFabiano Rosas             break;
813ccfca2fcSFabiano Rosas         case POWERPC_EXCP_TRAP:
814ccfca2fcSFabiano Rosas             msr |= 0x00020000;
815ccfca2fcSFabiano Rosas             break;
816ccfca2fcSFabiano Rosas         default:
817ccfca2fcSFabiano Rosas             /* Should never occur */
818ccfca2fcSFabiano Rosas             cpu_abort(cs, "Invalid program exception %d. Aborting\n",
819ccfca2fcSFabiano Rosas                       env->error_code);
820ccfca2fcSFabiano Rosas             break;
821ccfca2fcSFabiano Rosas         }
822ccfca2fcSFabiano Rosas         break;
823ccfca2fcSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
8243b578635SFabiano Rosas     {
8253b578635SFabiano Rosas         int lev = env->error_code;
826ccfca2fcSFabiano Rosas 
8273b578635SFabiano Rosas         if (lev == 1 && cpu->vhyp) {
828ccfca2fcSFabiano Rosas             dump_hcall(env);
829ccfca2fcSFabiano Rosas         } else {
830ccfca2fcSFabiano Rosas             dump_syscall(env);
831ccfca2fcSFabiano Rosas         }
832ccfca2fcSFabiano Rosas 
833ccfca2fcSFabiano Rosas         /*
834ccfca2fcSFabiano Rosas          * We need to correct the NIP which in this case is supposed
835ccfca2fcSFabiano Rosas          * to point to the next instruction
836ccfca2fcSFabiano Rosas          */
837ccfca2fcSFabiano Rosas         env->nip += 4;
838ccfca2fcSFabiano Rosas 
8393b578635SFabiano Rosas         /*
8403b578635SFabiano Rosas          * The Virtual Open Firmware (VOF) relies on the 'sc 1'
8413b578635SFabiano Rosas          * instruction to communicate with QEMU. The pegasos2 machine
8423b578635SFabiano Rosas          * uses VOF and the 7xx CPUs, so although the 7xx don't have
8433b578635SFabiano Rosas          * HV mode, we need to keep hypercall support.
8443b578635SFabiano Rosas          */
8453b578635SFabiano Rosas         if (lev == 1 && cpu->vhyp) {
846ccfca2fcSFabiano Rosas             PPCVirtualHypervisorClass *vhc =
847ccfca2fcSFabiano Rosas                 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
848ccfca2fcSFabiano Rosas             vhc->hypercall(cpu->vhyp, cpu);
849ccfca2fcSFabiano Rosas             return;
850ccfca2fcSFabiano Rosas         }
8513b578635SFabiano Rosas 
852ccfca2fcSFabiano Rosas         break;
8533b578635SFabiano Rosas     }
854ccfca2fcSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
855ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
856ccfca2fcSFabiano Rosas         break;
857ccfca2fcSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
8588e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
859ccfca2fcSFabiano Rosas             cpu_abort(cs, "Trying to deliver power-saving system reset "
860ccfca2fcSFabiano Rosas                       "exception %d with no HV support\n", excp);
861ccfca2fcSFabiano Rosas         }
862ccfca2fcSFabiano Rosas         break;
863ccfca2fcSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
864ccfca2fcSFabiano Rosas         break;
865ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
866ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
867ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
868ccfca2fcSFabiano Rosas         ppc_excp_debug_sw_tlb(env, excp);
869ccfca2fcSFabiano Rosas 
870ccfca2fcSFabiano Rosas         msr |= env->crf[0] << 28;
871ccfca2fcSFabiano Rosas         msr |= env->error_code; /* key, D/I, S/L bits */
872ccfca2fcSFabiano Rosas         /* Set way using a LRU mechanism */
873ccfca2fcSFabiano Rosas         msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
8747df40c54SFabiano Rosas 
875ccfca2fcSFabiano Rosas         break;
876ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
877ccfca2fcSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
878ccfca2fcSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
879ccfca2fcSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
880ccfca2fcSFabiano Rosas         cpu_abort(cs, "%s exception not implemented\n",
881ccfca2fcSFabiano Rosas                   powerpc_excp_name(excp));
882ccfca2fcSFabiano Rosas         break;
883ccfca2fcSFabiano Rosas     default:
884ccfca2fcSFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
885ccfca2fcSFabiano Rosas         break;
886ccfca2fcSFabiano Rosas     }
887ccfca2fcSFabiano Rosas 
888ccfca2fcSFabiano Rosas     /*
889ccfca2fcSFabiano Rosas      * Sort out endianness of interrupt, this differs depending on the
890ccfca2fcSFabiano Rosas      * CPU, the HV mode, etc...
891ccfca2fcSFabiano Rosas      */
892ccfca2fcSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
893ccfca2fcSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
894ccfca2fcSFabiano Rosas     }
895ccfca2fcSFabiano Rosas 
896ccfca2fcSFabiano Rosas     /* Save PC */
897fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
898ccfca2fcSFabiano Rosas 
899ccfca2fcSFabiano Rosas     /* Save MSR */
900fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR1] = msr;
901ccfca2fcSFabiano Rosas 
902ccfca2fcSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
903ccfca2fcSFabiano Rosas }
904ccfca2fcSFabiano Rosas 
90552926b0dSFabiano Rosas static void powerpc_excp_74xx(PowerPCCPU *cpu, int excp)
90652926b0dSFabiano Rosas {
90752926b0dSFabiano Rosas     CPUState *cs = CPU(cpu);
90852926b0dSFabiano Rosas     CPUPPCState *env = &cpu->env;
90952926b0dSFabiano Rosas     target_ulong msr, new_msr, vector;
91052926b0dSFabiano Rosas 
91152926b0dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
91252926b0dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
91352926b0dSFabiano Rosas 
91452926b0dSFabiano Rosas     /*
9151f6faf8bSFabiano Rosas      * new interrupt handler msr preserves existing ME unless
91652926b0dSFabiano Rosas      * explicitly overriden
91752926b0dSFabiano Rosas      */
9181f6faf8bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
91952926b0dSFabiano Rosas 
92052926b0dSFabiano Rosas     /*
92152926b0dSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
9221f6faf8bSFabiano Rosas      * arch 2.05 server or later.
92352926b0dSFabiano Rosas      */
9241f6faf8bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
92552926b0dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
92652926b0dSFabiano Rosas     }
92752926b0dSFabiano Rosas 
92852926b0dSFabiano Rosas     vector = env->excp_vectors[excp];
92952926b0dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
93052926b0dSFabiano Rosas         cpu_abort(cs, "Raised an exception without defined vector %d\n",
93152926b0dSFabiano Rosas                   excp);
93252926b0dSFabiano Rosas     }
93352926b0dSFabiano Rosas 
93452926b0dSFabiano Rosas     vector |= env->excp_prefix;
93552926b0dSFabiano Rosas 
93652926b0dSFabiano Rosas     switch (excp) {
93752926b0dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
938c354d858SVíctor Colombo         if (!FIELD_EX64(env->msr, MSR, ME)) {
93952926b0dSFabiano Rosas             /*
94052926b0dSFabiano Rosas              * Machine check exception is not enabled.  Enter
94152926b0dSFabiano Rosas              * checkstop state.
94252926b0dSFabiano Rosas              */
94352926b0dSFabiano Rosas             fprintf(stderr, "Machine check while not allowed. "
94452926b0dSFabiano Rosas                     "Entering checkstop state\n");
94552926b0dSFabiano Rosas             if (qemu_log_separate()) {
94652926b0dSFabiano Rosas                 qemu_log("Machine check while not allowed. "
94752926b0dSFabiano Rosas                         "Entering checkstop state\n");
94852926b0dSFabiano Rosas             }
94952926b0dSFabiano Rosas             cs->halted = 1;
95052926b0dSFabiano Rosas             cpu_interrupt_exittb(cs);
95152926b0dSFabiano Rosas         }
95252926b0dSFabiano Rosas 
95352926b0dSFabiano Rosas         /* machine check exceptions don't have ME set */
95452926b0dSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
95552926b0dSFabiano Rosas 
95652926b0dSFabiano Rosas         break;
95752926b0dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
95852926b0dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
95952926b0dSFabiano Rosas         break;
96052926b0dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
96152926b0dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
96252926b0dSFabiano Rosas         msr |= env->error_code;
96352926b0dSFabiano Rosas         break;
96452926b0dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
96552926b0dSFabiano Rosas         break;
96652926b0dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
96752926b0dSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
96852926b0dSFabiano Rosas         /*
96952926b0dSFabiano Rosas          * Note: the opcode fields will not be set properly for a
97052926b0dSFabiano Rosas          * direct store load/store, but nobody cares as nobody
97152926b0dSFabiano Rosas          * actually uses direct store segments.
97252926b0dSFabiano Rosas          */
97352926b0dSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
97452926b0dSFabiano Rosas         break;
97552926b0dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
97652926b0dSFabiano Rosas         switch (env->error_code & ~0xF) {
97752926b0dSFabiano Rosas         case POWERPC_EXCP_FP:
978da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
97952926b0dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
9803680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
98152926b0dSFabiano Rosas                 return;
98252926b0dSFabiano Rosas             }
98352926b0dSFabiano Rosas 
98452926b0dSFabiano Rosas             /*
98552926b0dSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
98652926b0dSFabiano Rosas              * instruction, so always use store_next and claim we are
98752926b0dSFabiano Rosas              * precise in the MSR.
98852926b0dSFabiano Rosas              */
98952926b0dSFabiano Rosas             msr |= 0x00100000;
99052926b0dSFabiano Rosas             break;
99152926b0dSFabiano Rosas         case POWERPC_EXCP_INVAL:
99252926b0dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
99352926b0dSFabiano Rosas             msr |= 0x00080000;
99452926b0dSFabiano Rosas             break;
99552926b0dSFabiano Rosas         case POWERPC_EXCP_PRIV:
99652926b0dSFabiano Rosas             msr |= 0x00040000;
99752926b0dSFabiano Rosas             break;
99852926b0dSFabiano Rosas         case POWERPC_EXCP_TRAP:
99952926b0dSFabiano Rosas             msr |= 0x00020000;
100052926b0dSFabiano Rosas             break;
100152926b0dSFabiano Rosas         default:
100252926b0dSFabiano Rosas             /* Should never occur */
100352926b0dSFabiano Rosas             cpu_abort(cs, "Invalid program exception %d. Aborting\n",
100452926b0dSFabiano Rosas                       env->error_code);
100552926b0dSFabiano Rosas             break;
100652926b0dSFabiano Rosas         }
100752926b0dSFabiano Rosas         break;
100852926b0dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
1009bca2c6d9SFabiano Rosas     {
1010bca2c6d9SFabiano Rosas         int lev = env->error_code;
101152926b0dSFabiano Rosas 
101252926b0dSFabiano Rosas         if ((lev == 1) && cpu->vhyp) {
101352926b0dSFabiano Rosas             dump_hcall(env);
101452926b0dSFabiano Rosas         } else {
101552926b0dSFabiano Rosas             dump_syscall(env);
101652926b0dSFabiano Rosas         }
101752926b0dSFabiano Rosas 
101852926b0dSFabiano Rosas         /*
101952926b0dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
102052926b0dSFabiano Rosas          * to point to the next instruction
102152926b0dSFabiano Rosas          */
102252926b0dSFabiano Rosas         env->nip += 4;
102352926b0dSFabiano Rosas 
1024bca2c6d9SFabiano Rosas         /*
1025bca2c6d9SFabiano Rosas          * The Virtual Open Firmware (VOF) relies on the 'sc 1'
1026bca2c6d9SFabiano Rosas          * instruction to communicate with QEMU. The pegasos2 machine
1027bca2c6d9SFabiano Rosas          * uses VOF and the 74xx CPUs, so although the 74xx don't have
1028bca2c6d9SFabiano Rosas          * HV mode, we need to keep hypercall support.
1029bca2c6d9SFabiano Rosas          */
103052926b0dSFabiano Rosas         if ((lev == 1) && cpu->vhyp) {
103152926b0dSFabiano Rosas             PPCVirtualHypervisorClass *vhc =
103252926b0dSFabiano Rosas                 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
103352926b0dSFabiano Rosas             vhc->hypercall(cpu->vhyp, cpu);
103452926b0dSFabiano Rosas             return;
103552926b0dSFabiano Rosas         }
1036bca2c6d9SFabiano Rosas 
103752926b0dSFabiano Rosas         break;
1038bca2c6d9SFabiano Rosas     }
103952926b0dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
104052926b0dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
104152926b0dSFabiano Rosas         break;
104252926b0dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
10438e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
104452926b0dSFabiano Rosas             cpu_abort(cs, "Trying to deliver power-saving system reset "
104552926b0dSFabiano Rosas                       "exception %d with no HV support\n", excp);
104652926b0dSFabiano Rosas         }
104752926b0dSFabiano Rosas         break;
104852926b0dSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
104952926b0dSFabiano Rosas         break;
105052926b0dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
105152926b0dSFabiano Rosas         break;
105252926b0dSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
105352926b0dSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
105452926b0dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
105552926b0dSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
105652926b0dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
105752926b0dSFabiano Rosas         cpu_abort(cs, "%s exception not implemented\n",
105852926b0dSFabiano Rosas                   powerpc_excp_name(excp));
105952926b0dSFabiano Rosas         break;
106052926b0dSFabiano Rosas     default:
106152926b0dSFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
106252926b0dSFabiano Rosas         break;
106352926b0dSFabiano Rosas     }
106452926b0dSFabiano Rosas 
106552926b0dSFabiano Rosas     /*
106652926b0dSFabiano Rosas      * Sort out endianness of interrupt, this differs depending on the
106752926b0dSFabiano Rosas      * CPU, the HV mode, etc...
106852926b0dSFabiano Rosas      */
106952926b0dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
107052926b0dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
107152926b0dSFabiano Rosas     }
107252926b0dSFabiano Rosas 
107352926b0dSFabiano Rosas     /* Save PC */
1074f82db777SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
107552926b0dSFabiano Rosas 
107652926b0dSFabiano Rosas     /* Save MSR */
1077f82db777SFabiano Rosas     env->spr[SPR_SRR1] = msr;
107852926b0dSFabiano Rosas 
107952926b0dSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
108052926b0dSFabiano Rosas }
108152926b0dSFabiano Rosas 
1082180952ceSFabiano Rosas static void powerpc_excp_booke(PowerPCCPU *cpu, int excp)
1083180952ceSFabiano Rosas {
1084180952ceSFabiano Rosas     CPUState *cs = CPU(cpu);
1085180952ceSFabiano Rosas     CPUPPCState *env = &cpu->env;
1086180952ceSFabiano Rosas     target_ulong msr, new_msr, vector;
1087904e8428SFabiano Rosas     int srr0, srr1;
1088180952ceSFabiano Rosas 
1089180952ceSFabiano Rosas     msr = env->msr;
1090180952ceSFabiano Rosas 
1091180952ceSFabiano Rosas     /*
10929dc20cc3SFabiano Rosas      * new interrupt handler msr preserves existing ME unless
1093180952ceSFabiano Rosas      * explicitly overriden
1094180952ceSFabiano Rosas      */
10959dc20cc3SFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
1096180952ceSFabiano Rosas 
1097180952ceSFabiano Rosas     /* target registers */
1098180952ceSFabiano Rosas     srr0 = SPR_SRR0;
1099180952ceSFabiano Rosas     srr1 = SPR_SRR1;
1100180952ceSFabiano Rosas 
1101180952ceSFabiano Rosas     /*
1102180952ceSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
11039dc20cc3SFabiano Rosas      * arch 2.05 server or later.
1104180952ceSFabiano Rosas      */
11059dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
1106180952ceSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
1107180952ceSFabiano Rosas     }
1108180952ceSFabiano Rosas 
1109180952ceSFabiano Rosas #ifdef TARGET_PPC64
1110180952ceSFabiano Rosas     /*
1111180952ceSFabiano Rosas      * SPEU and VPU share the same IVOR but they exist in different
1112180952ceSFabiano Rosas      * processors. SPEU is e500v1/2 only and VPU is e6500 only.
1113180952ceSFabiano Rosas      */
11149dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_VPU) {
1115180952ceSFabiano Rosas         excp = POWERPC_EXCP_SPEU;
1116180952ceSFabiano Rosas     }
1117180952ceSFabiano Rosas #endif
1118180952ceSFabiano Rosas 
1119180952ceSFabiano Rosas     vector = env->excp_vectors[excp];
1120180952ceSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
1121180952ceSFabiano Rosas         cpu_abort(cs, "Raised an exception without defined vector %d\n",
1122180952ceSFabiano Rosas                   excp);
1123180952ceSFabiano Rosas     }
1124180952ceSFabiano Rosas 
1125180952ceSFabiano Rosas     vector |= env->excp_prefix;
1126180952ceSFabiano Rosas 
1127180952ceSFabiano Rosas     switch (excp) {
1128180952ceSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
1129180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
1130180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
1131180952ceSFabiano Rosas         break;
1132180952ceSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
1133c354d858SVíctor Colombo         if (!FIELD_EX64(env->msr, MSR, ME)) {
1134180952ceSFabiano Rosas             /*
1135180952ceSFabiano Rosas              * Machine check exception is not enabled.  Enter
1136180952ceSFabiano Rosas              * checkstop state.
1137180952ceSFabiano Rosas              */
1138180952ceSFabiano Rosas             fprintf(stderr, "Machine check while not allowed. "
1139180952ceSFabiano Rosas                     "Entering checkstop state\n");
1140180952ceSFabiano Rosas             if (qemu_log_separate()) {
1141180952ceSFabiano Rosas                 qemu_log("Machine check while not allowed. "
1142180952ceSFabiano Rosas                         "Entering checkstop state\n");
1143180952ceSFabiano Rosas             }
1144180952ceSFabiano Rosas             cs->halted = 1;
1145180952ceSFabiano Rosas             cpu_interrupt_exittb(cs);
1146180952ceSFabiano Rosas         }
1147180952ceSFabiano Rosas 
1148180952ceSFabiano Rosas         /* machine check exceptions don't have ME set */
1149180952ceSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
1150180952ceSFabiano Rosas 
1151180952ceSFabiano Rosas         /* FIXME: choose one or the other based on CPU type */
1152180952ceSFabiano Rosas         srr0 = SPR_BOOKE_MCSRR0;
1153180952ceSFabiano Rosas         srr1 = SPR_BOOKE_MCSRR1;
1154180952ceSFabiano Rosas 
1155180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR0] = env->nip;
1156180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR1] = msr;
1157db403211SFabiano Rosas 
1158180952ceSFabiano Rosas         break;
1159180952ceSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
1160afdbc869SFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
1161180952ceSFabiano Rosas         break;
1162180952ceSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
1163180952ceSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
1164180952ceSFabiano Rosas         break;
1165180952ceSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
1166180952ceSFabiano Rosas         if (env->mpic_proxy) {
1167180952ceSFabiano Rosas             /* IACK the IRQ on delivery */
1168180952ceSFabiano Rosas             env->spr[SPR_BOOKE_EPR] = ldl_phys(cs->as, env->mpic_iack);
1169180952ceSFabiano Rosas         }
1170180952ceSFabiano Rosas         break;
1171180952ceSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
1172180952ceSFabiano Rosas         break;
1173180952ceSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
1174180952ceSFabiano Rosas         switch (env->error_code & ~0xF) {
1175180952ceSFabiano Rosas         case POWERPC_EXCP_FP:
1176da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
1177180952ceSFabiano Rosas                 trace_ppc_excp_fp_ignore();
11783680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
1179180952ceSFabiano Rosas                 return;
1180180952ceSFabiano Rosas             }
1181180952ceSFabiano Rosas 
1182180952ceSFabiano Rosas             /*
1183180952ceSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
1184180952ceSFabiano Rosas              * instruction, so always use store_next and claim we are
1185180952ceSFabiano Rosas              * precise in the MSR.
1186180952ceSFabiano Rosas              */
1187180952ceSFabiano Rosas             msr |= 0x00100000;
1188180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_FP;
1189180952ceSFabiano Rosas             break;
1190180952ceSFabiano Rosas         case POWERPC_EXCP_INVAL:
1191180952ceSFabiano Rosas             trace_ppc_excp_inval(env->nip);
1192180952ceSFabiano Rosas             msr |= 0x00080000;
1193180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PIL;
1194180952ceSFabiano Rosas             break;
1195180952ceSFabiano Rosas         case POWERPC_EXCP_PRIV:
1196180952ceSFabiano Rosas             msr |= 0x00040000;
1197180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PPR;
1198180952ceSFabiano Rosas             break;
1199180952ceSFabiano Rosas         case POWERPC_EXCP_TRAP:
1200180952ceSFabiano Rosas             msr |= 0x00020000;
1201180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PTR;
1202180952ceSFabiano Rosas             break;
1203180952ceSFabiano Rosas         default:
1204180952ceSFabiano Rosas             /* Should never occur */
1205180952ceSFabiano Rosas             cpu_abort(cs, "Invalid program exception %d. Aborting\n",
1206180952ceSFabiano Rosas                       env->error_code);
1207180952ceSFabiano Rosas             break;
1208180952ceSFabiano Rosas         }
1209180952ceSFabiano Rosas         break;
1210180952ceSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
1211180952ceSFabiano Rosas         dump_syscall(env);
1212180952ceSFabiano Rosas 
1213180952ceSFabiano Rosas         /*
1214180952ceSFabiano Rosas          * We need to correct the NIP which in this case is supposed
1215180952ceSFabiano Rosas          * to point to the next instruction
1216180952ceSFabiano Rosas          */
1217180952ceSFabiano Rosas         env->nip += 4;
1218180952ceSFabiano Rosas         break;
1219180952ceSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
1220180952ceSFabiano Rosas     case POWERPC_EXCP_APU:       /* Auxiliary processor unavailable          */
1221180952ceSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
1222180952ceSFabiano Rosas         break;
1223180952ceSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
1224180952ceSFabiano Rosas         /* FIT on 4xx */
1225180952ceSFabiano Rosas         trace_ppc_excp_print("FIT");
1226180952ceSFabiano Rosas         break;
1227180952ceSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
1228180952ceSFabiano Rosas         trace_ppc_excp_print("WDT");
1229180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
1230180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
1231180952ceSFabiano Rosas         break;
1232180952ceSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
1233180952ceSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
1234180952ceSFabiano Rosas         break;
1235180952ceSFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
1236180952ceSFabiano Rosas         if (env->flags & POWERPC_FLAG_DE) {
1237180952ceSFabiano Rosas             /* FIXME: choose one or the other based on CPU type */
1238180952ceSFabiano Rosas             srr0 = SPR_BOOKE_DSRR0;
1239180952ceSFabiano Rosas             srr1 = SPR_BOOKE_DSRR1;
1240180952ceSFabiano Rosas 
1241180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR0] = env->nip;
1242180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR1] = msr;
1243180952ceSFabiano Rosas 
1244180952ceSFabiano Rosas             /* DBSR already modified by caller */
1245180952ceSFabiano Rosas         } else {
1246180952ceSFabiano Rosas             cpu_abort(cs, "Debug exception triggered on unsupported model\n");
1247180952ceSFabiano Rosas         }
1248180952ceSFabiano Rosas         break;
1249180952ceSFabiano Rosas     case POWERPC_EXCP_SPEU:   /* SPE/embedded floating-point unavailable/VPU  */
1250180952ceSFabiano Rosas         env->spr[SPR_BOOKE_ESR] = ESR_SPV;
1251180952ceSFabiano Rosas         break;
12529364df26SNicholas Piggin     case POWERPC_EXCP_DOORI:     /* Embedded doorbell interrupt              */
12539364df26SNicholas Piggin         break;
12549364df26SNicholas Piggin     case POWERPC_EXCP_DOORCI:    /* Embedded doorbell critical interrupt     */
12559364df26SNicholas Piggin         srr0 = SPR_BOOKE_CSRR0;
12569364df26SNicholas Piggin         srr1 = SPR_BOOKE_CSRR1;
12579364df26SNicholas Piggin         break;
1258180952ceSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
12598e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
1260180952ceSFabiano Rosas             cpu_abort(cs, "Trying to deliver power-saving system reset "
1261180952ceSFabiano Rosas                       "exception %d with no HV support\n", excp);
1262180952ceSFabiano Rosas         }
1263180952ceSFabiano Rosas         break;
1264180952ceSFabiano Rosas     case POWERPC_EXCP_EFPDI:     /* Embedded floating-point data interrupt   */
1265180952ceSFabiano Rosas     case POWERPC_EXCP_EFPRI:     /* Embedded floating-point round interrupt  */
1266180952ceSFabiano Rosas         cpu_abort(cs, "%s exception not implemented\n",
1267180952ceSFabiano Rosas                   powerpc_excp_name(excp));
1268180952ceSFabiano Rosas         break;
1269180952ceSFabiano Rosas     default:
1270180952ceSFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
1271180952ceSFabiano Rosas         break;
1272180952ceSFabiano Rosas     }
1273180952ceSFabiano Rosas 
1274180952ceSFabiano Rosas #if defined(TARGET_PPC64)
1275180952ceSFabiano Rosas     if (env->spr[SPR_BOOKE_EPCR] & EPCR_ICM) {
1276180952ceSFabiano Rosas         /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */
1277180952ceSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_CM;
1278180952ceSFabiano Rosas     } else {
1279180952ceSFabiano Rosas         vector = (uint32_t)vector;
1280180952ceSFabiano Rosas     }
1281180952ceSFabiano Rosas #endif
1282180952ceSFabiano Rosas 
1283180952ceSFabiano Rosas     /* Save PC */
1284180952ceSFabiano Rosas     env->spr[srr0] = env->nip;
1285180952ceSFabiano Rosas 
1286180952ceSFabiano Rosas     /* Save MSR */
1287180952ceSFabiano Rosas     env->spr[srr1] = msr;
1288180952ceSFabiano Rosas 
1289180952ceSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
1290180952ceSFabiano Rosas }
1291180952ceSFabiano Rosas 
12927cebc5dbSNicholas Piggin /*
12937cebc5dbSNicholas Piggin  * When running a nested HV guest under vhyp, external interrupts are
12947cebc5dbSNicholas Piggin  * delivered as HVIRT.
12957cebc5dbSNicholas Piggin  */
12967cebc5dbSNicholas Piggin static bool books_vhyp_promotes_external_to_hvirt(PowerPCCPU *cpu)
12977cebc5dbSNicholas Piggin {
12987cebc5dbSNicholas Piggin     if (cpu->vhyp) {
12997cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
13007cebc5dbSNicholas Piggin     }
13017cebc5dbSNicholas Piggin     return false;
13027cebc5dbSNicholas Piggin }
13037cebc5dbSNicholas Piggin 
130430c4e426SFabiano Rosas #ifdef TARGET_PPC64
13054c6cf6b2SNicholas Piggin /*
13064c6cf6b2SNicholas Piggin  * When running under vhyp, hcalls are always intercepted and sent to the
13074c6cf6b2SNicholas Piggin  * vhc->hypercall handler.
13084c6cf6b2SNicholas Piggin  */
13094c6cf6b2SNicholas Piggin static bool books_vhyp_handles_hcall(PowerPCCPU *cpu)
13104c6cf6b2SNicholas Piggin {
13114c6cf6b2SNicholas Piggin     if (cpu->vhyp) {
13127cebc5dbSNicholas Piggin         return !vhyp_cpu_in_nested(cpu);
13137cebc5dbSNicholas Piggin     }
13147cebc5dbSNicholas Piggin     return false;
13157cebc5dbSNicholas Piggin }
13167cebc5dbSNicholas Piggin 
13177cebc5dbSNicholas Piggin /*
13187cebc5dbSNicholas Piggin  * When running a nested KVM HV guest under vhyp, HV exceptions are not
13197cebc5dbSNicholas Piggin  * delivered to the guest (because there is no concept of HV support), but
13207cebc5dbSNicholas Piggin  * rather they are sent tothe vhyp to exit from the L2 back to the L1 and
13217cebc5dbSNicholas Piggin  * return from the H_ENTER_NESTED hypercall.
13227cebc5dbSNicholas Piggin  */
13237cebc5dbSNicholas Piggin static bool books_vhyp_handles_hv_excp(PowerPCCPU *cpu)
13247cebc5dbSNicholas Piggin {
13257cebc5dbSNicholas Piggin     if (cpu->vhyp) {
13267cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
13274c6cf6b2SNicholas Piggin     }
13284c6cf6b2SNicholas Piggin     return false;
13294c6cf6b2SNicholas Piggin }
13304c6cf6b2SNicholas Piggin 
13319f338e4dSFabiano Rosas static void powerpc_excp_books(PowerPCCPU *cpu, int excp)
13329f338e4dSFabiano Rosas {
13339f338e4dSFabiano Rosas     CPUState *cs = CPU(cpu);
13349f338e4dSFabiano Rosas     CPUPPCState *env = &cpu->env;
13359f338e4dSFabiano Rosas     target_ulong msr, new_msr, vector;
13369f338e4dSFabiano Rosas     int srr0, srr1, lev = -1;
13379f338e4dSFabiano Rosas 
13389f338e4dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
13399f338e4dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
13409f338e4dSFabiano Rosas 
13419f338e4dSFabiano Rosas     /*
13429f338e4dSFabiano Rosas      * new interrupt handler msr preserves existing HV and ME unless
13439f338e4dSFabiano Rosas      * explicitly overriden
13449f338e4dSFabiano Rosas      */
13459f338e4dSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME) | MSR_HVB);
13469f338e4dSFabiano Rosas 
13479f338e4dSFabiano Rosas     /* target registers */
13489f338e4dSFabiano Rosas     srr0 = SPR_SRR0;
13499f338e4dSFabiano Rosas     srr1 = SPR_SRR1;
13509f338e4dSFabiano Rosas 
13519f338e4dSFabiano Rosas     /*
13529f338e4dSFabiano Rosas      * check for special resume at 0x100 from doze/nap/sleep/winkle on
13539f338e4dSFabiano Rosas      * P7/P8/P9
13549f338e4dSFabiano Rosas      */
13559f338e4dSFabiano Rosas     if (env->resume_as_sreset) {
13569f338e4dSFabiano Rosas         excp = powerpc_reset_wakeup(cs, env, excp, &msr);
13579f338e4dSFabiano Rosas     }
13589f338e4dSFabiano Rosas 
13599f338e4dSFabiano Rosas     /*
136030c4e426SFabiano Rosas      * We don't want to generate a Hypervisor Emulation Assistance
136130c4e426SFabiano Rosas      * Interrupt if we don't have HVB in msr_mask (PAPR mode).
13629f338e4dSFabiano Rosas      */
136330c4e426SFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU && !(env->msr_mask & MSR_HVB)) {
13649f338e4dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
13659f338e4dSFabiano Rosas     }
13669f338e4dSFabiano Rosas 
13679f338e4dSFabiano Rosas     vector = env->excp_vectors[excp];
13689f338e4dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
13699f338e4dSFabiano Rosas         cpu_abort(cs, "Raised an exception without defined vector %d\n",
13709f338e4dSFabiano Rosas                   excp);
13719f338e4dSFabiano Rosas     }
13729f338e4dSFabiano Rosas 
13739f338e4dSFabiano Rosas     vector |= env->excp_prefix;
13749f338e4dSFabiano Rosas 
13759f338e4dSFabiano Rosas     switch (excp) {
13769f338e4dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
1377c354d858SVíctor Colombo         if (!FIELD_EX64(env->msr, MSR, ME)) {
13789f338e4dSFabiano Rosas             /*
13799f338e4dSFabiano Rosas              * Machine check exception is not enabled.  Enter
13809f338e4dSFabiano Rosas              * checkstop state.
13819f338e4dSFabiano Rosas              */
13829f338e4dSFabiano Rosas             fprintf(stderr, "Machine check while not allowed. "
13839f338e4dSFabiano Rosas                     "Entering checkstop state\n");
13849f338e4dSFabiano Rosas             if (qemu_log_separate()) {
13859f338e4dSFabiano Rosas                 qemu_log("Machine check while not allowed. "
13869f338e4dSFabiano Rosas                         "Entering checkstop state\n");
13879f338e4dSFabiano Rosas             }
13889f338e4dSFabiano Rosas             cs->halted = 1;
13899f338e4dSFabiano Rosas             cpu_interrupt_exittb(cs);
13909f338e4dSFabiano Rosas         }
13919f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
13929f338e4dSFabiano Rosas             /*
13939f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
13949f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR).
13959f338e4dSFabiano Rosas              */
13969f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
13979f338e4dSFabiano Rosas         }
13989f338e4dSFabiano Rosas 
13999f338e4dSFabiano Rosas         /* machine check exceptions don't have ME set */
14009f338e4dSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
14019f338e4dSFabiano Rosas 
14029f338e4dSFabiano Rosas         break;
14039f338e4dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
14049f338e4dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
14059f338e4dSFabiano Rosas         break;
14069f338e4dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
14079f338e4dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
14089f338e4dSFabiano Rosas         msr |= env->error_code;
14099f338e4dSFabiano Rosas         break;
14109f338e4dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
14119f338e4dSFabiano Rosas     {
14129f338e4dSFabiano Rosas         bool lpes0;
14139f338e4dSFabiano Rosas 
14149f338e4dSFabiano Rosas         /*
141567baff77SFabiano Rosas          * LPES0 is only taken into consideration if we support HV
141667baff77SFabiano Rosas          * mode for this CPU.
14179f338e4dSFabiano Rosas          */
141867baff77SFabiano Rosas         if (!env->has_hv_mode) {
141967baff77SFabiano Rosas             break;
14209f338e4dSFabiano Rosas         }
14219f338e4dSFabiano Rosas 
142267baff77SFabiano Rosas         lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
142367baff77SFabiano Rosas 
14249f338e4dSFabiano Rosas         if (!lpes0) {
14259f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14269f338e4dSFabiano Rosas             new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
14279f338e4dSFabiano Rosas             srr0 = SPR_HSRR0;
14289f338e4dSFabiano Rosas             srr1 = SPR_HSRR1;
14299f338e4dSFabiano Rosas         }
143067baff77SFabiano Rosas 
14319f338e4dSFabiano Rosas         break;
14329f338e4dSFabiano Rosas     }
14339f338e4dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
14349f338e4dSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
14359f338e4dSFabiano Rosas         /*
14369f338e4dSFabiano Rosas          * Note: the opcode fields will not be set properly for a
14379f338e4dSFabiano Rosas          * direct store load/store, but nobody cares as nobody
14389f338e4dSFabiano Rosas          * actually uses direct store segments.
14399f338e4dSFabiano Rosas          */
14409f338e4dSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
14419f338e4dSFabiano Rosas         break;
14429f338e4dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
14439f338e4dSFabiano Rosas         switch (env->error_code & ~0xF) {
14449f338e4dSFabiano Rosas         case POWERPC_EXCP_FP:
1445da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
14469f338e4dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
14473680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
14489f338e4dSFabiano Rosas                 return;
14499f338e4dSFabiano Rosas             }
14509f338e4dSFabiano Rosas 
14519f338e4dSFabiano Rosas             /*
14529f338e4dSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
14539f338e4dSFabiano Rosas              * instruction, so always use store_next and claim we are
14549f338e4dSFabiano Rosas              * precise in the MSR.
14559f338e4dSFabiano Rosas              */
14569f338e4dSFabiano Rosas             msr |= 0x00100000;
14579f338e4dSFabiano Rosas             break;
14589f338e4dSFabiano Rosas         case POWERPC_EXCP_INVAL:
14599f338e4dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
14609f338e4dSFabiano Rosas             msr |= 0x00080000;
14619f338e4dSFabiano Rosas             break;
14629f338e4dSFabiano Rosas         case POWERPC_EXCP_PRIV:
14639f338e4dSFabiano Rosas             msr |= 0x00040000;
14649f338e4dSFabiano Rosas             break;
14659f338e4dSFabiano Rosas         case POWERPC_EXCP_TRAP:
14669f338e4dSFabiano Rosas             msr |= 0x00020000;
14679f338e4dSFabiano Rosas             break;
14689f338e4dSFabiano Rosas         default:
14699f338e4dSFabiano Rosas             /* Should never occur */
14709f338e4dSFabiano Rosas             cpu_abort(cs, "Invalid program exception %d. Aborting\n",
14719f338e4dSFabiano Rosas                       env->error_code);
14729f338e4dSFabiano Rosas             break;
14739f338e4dSFabiano Rosas         }
14749f338e4dSFabiano Rosas         break;
14759f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
14769f338e4dSFabiano Rosas         lev = env->error_code;
14779f338e4dSFabiano Rosas 
14789f338e4dSFabiano Rosas         if ((lev == 1) && cpu->vhyp) {
14799f338e4dSFabiano Rosas             dump_hcall(env);
14809f338e4dSFabiano Rosas         } else {
14819f338e4dSFabiano Rosas             dump_syscall(env);
14829f338e4dSFabiano Rosas         }
14839f338e4dSFabiano Rosas 
14849f338e4dSFabiano Rosas         /*
14859f338e4dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
14869f338e4dSFabiano Rosas          * to point to the next instruction
14879f338e4dSFabiano Rosas          */
14889f338e4dSFabiano Rosas         env->nip += 4;
14899f338e4dSFabiano Rosas 
14909f338e4dSFabiano Rosas         /* "PAPR mode" built-in hypercall emulation */
14914c6cf6b2SNicholas Piggin         if ((lev == 1) && books_vhyp_handles_hcall(cpu)) {
14929f338e4dSFabiano Rosas             PPCVirtualHypervisorClass *vhc =
14939f338e4dSFabiano Rosas                 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
14949f338e4dSFabiano Rosas             vhc->hypercall(cpu->vhyp, cpu);
14959f338e4dSFabiano Rosas             return;
14969f338e4dSFabiano Rosas         }
14979f338e4dSFabiano Rosas         if (lev == 1) {
14989f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14999f338e4dSFabiano Rosas         }
15009f338e4dSFabiano Rosas         break;
15019f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL_VECTORED: /* scv exception                     */
15029f338e4dSFabiano Rosas         lev = env->error_code;
15039f338e4dSFabiano Rosas         dump_syscall(env);
15049f338e4dSFabiano Rosas         env->nip += 4;
15059f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_EE);
15069f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
15079f338e4dSFabiano Rosas 
15089f338e4dSFabiano Rosas         vector += lev * 0x20;
15099f338e4dSFabiano Rosas 
15109f338e4dSFabiano Rosas         env->lr = env->nip;
15119f338e4dSFabiano Rosas         env->ctr = msr;
15129f338e4dSFabiano Rosas         break;
15139f338e4dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
15149f338e4dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
15159f338e4dSFabiano Rosas         break;
15169f338e4dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
15179f338e4dSFabiano Rosas         /* A power-saving exception sets ME, otherwise it is unchanged */
15188e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
15199f338e4dSFabiano Rosas             /* indicate that we resumed from power save mode */
15209f338e4dSFabiano Rosas             msr |= 0x10000;
15219f338e4dSFabiano Rosas             new_msr |= ((target_ulong)1 << MSR_ME);
15229f338e4dSFabiano Rosas         }
15239f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
15249f338e4dSFabiano Rosas             /*
15259f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
15269f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR, NMI injection in QEMU).
15279f338e4dSFabiano Rosas              */
15289f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
15299f338e4dSFabiano Rosas         } else {
15308e54ad65SVíctor Colombo             if (FIELD_EX64(env->msr, MSR, POW)) {
15319f338e4dSFabiano Rosas                 cpu_abort(cs, "Trying to deliver power-saving system reset "
15329f338e4dSFabiano Rosas                           "exception %d with no HV support\n", excp);
15339f338e4dSFabiano Rosas             }
15349f338e4dSFabiano Rosas         }
15359f338e4dSFabiano Rosas         break;
15369f338e4dSFabiano Rosas     case POWERPC_EXCP_DSEG:      /* Data segment exception                   */
15379f338e4dSFabiano Rosas     case POWERPC_EXCP_ISEG:      /* Instruction segment exception            */
15389f338e4dSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
15399f338e4dSFabiano Rosas         break;
15409f338e4dSFabiano Rosas     case POWERPC_EXCP_HISI:      /* Hypervisor instruction storage exception */
15419f338e4dSFabiano Rosas         msr |= env->error_code;
15429f338e4dSFabiano Rosas         /* fall through */
15439f338e4dSFabiano Rosas     case POWERPC_EXCP_HDECR:     /* Hypervisor decrementer exception         */
15449f338e4dSFabiano Rosas     case POWERPC_EXCP_HDSI:      /* Hypervisor data storage exception        */
15459f338e4dSFabiano Rosas     case POWERPC_EXCP_SDOOR_HV:  /* Hypervisor Doorbell interrupt            */
15469f338e4dSFabiano Rosas     case POWERPC_EXCP_HV_EMU:
15479f338e4dSFabiano Rosas     case POWERPC_EXCP_HVIRT:     /* Hypervisor virtualization                */
15489f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
15499f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
15509f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
15519f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
15529f338e4dSFabiano Rosas         break;
15539f338e4dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
15549f338e4dSFabiano Rosas     case POWERPC_EXCP_VSXU:       /* VSX unavailable exception               */
15559f338e4dSFabiano Rosas     case POWERPC_EXCP_FU:         /* Facility unavailable exception          */
15569f338e4dSFabiano Rosas         env->spr[SPR_FSCR] |= ((target_ulong)env->error_code << 56);
15579f338e4dSFabiano Rosas         break;
15589f338e4dSFabiano Rosas     case POWERPC_EXCP_HV_FU:     /* Hypervisor Facility Unavailable Exception */
15599f338e4dSFabiano Rosas         env->spr[SPR_HFSCR] |= ((target_ulong)env->error_code << FSCR_IC_POS);
15609f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
15619f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
15629f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
15639f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
15649f338e4dSFabiano Rosas         break;
1565cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_PERFM_EBB:        /* Performance Monitor EBB Exception  */
1566cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_EXTERNAL_EBB:     /* External EBB Exception             */
1567cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
1568cb76bbc4SDaniel Henrique Barboza 
1569cb76bbc4SDaniel Henrique Barboza         /*
1570cb76bbc4SDaniel Henrique Barboza          * Save NIP for rfebb insn in SPR_EBBRR. Next nip is
1571cb76bbc4SDaniel Henrique Barboza          * stored in the EBB Handler SPR_EBBHR.
1572cb76bbc4SDaniel Henrique Barboza          */
1573cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_EBBRR] = env->nip;
1574cb76bbc4SDaniel Henrique Barboza         powerpc_set_excp_state(cpu, env->spr[SPR_EBBHR], env->msr);
1575cb76bbc4SDaniel Henrique Barboza 
1576cb76bbc4SDaniel Henrique Barboza         /*
1577cb76bbc4SDaniel Henrique Barboza          * This exception is handled in userspace. No need to proceed.
1578cb76bbc4SDaniel Henrique Barboza          */
1579cb76bbc4SDaniel Henrique Barboza         return;
15809f338e4dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
15819f338e4dSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
15829f338e4dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
15839f338e4dSFabiano Rosas     case POWERPC_EXCP_MAINT:     /* Maintenance exception                    */
158430c4e426SFabiano Rosas     case POWERPC_EXCP_SDOOR:     /* Doorbell interrupt                       */
158530c4e426SFabiano Rosas     case POWERPC_EXCP_HV_MAINT:  /* Hypervisor Maintenance exception         */
15869f338e4dSFabiano Rosas         cpu_abort(cs, "%s exception not implemented\n",
15879f338e4dSFabiano Rosas                   powerpc_excp_name(excp));
15889f338e4dSFabiano Rosas         break;
15899f338e4dSFabiano Rosas     default:
15909f338e4dSFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
15919f338e4dSFabiano Rosas         break;
15929f338e4dSFabiano Rosas     }
15939f338e4dSFabiano Rosas 
15949f338e4dSFabiano Rosas     /*
15959f338e4dSFabiano Rosas      * Sort out endianness of interrupt, this differs depending on the
15969f338e4dSFabiano Rosas      * CPU, the HV mode, etc...
15979f338e4dSFabiano Rosas      */
15989f338e4dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
15999f338e4dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
16009f338e4dSFabiano Rosas     }
16019f338e4dSFabiano Rosas 
16029f338e4dSFabiano Rosas     new_msr |= (target_ulong)1 << MSR_SF;
16039f338e4dSFabiano Rosas 
16049f338e4dSFabiano Rosas     if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
16059f338e4dSFabiano Rosas         /* Save PC */
16069f338e4dSFabiano Rosas         env->spr[srr0] = env->nip;
16079f338e4dSFabiano Rosas 
16089f338e4dSFabiano Rosas         /* Save MSR */
16099f338e4dSFabiano Rosas         env->spr[srr1] = msr;
16109f338e4dSFabiano Rosas     }
16119f338e4dSFabiano Rosas 
16127cebc5dbSNicholas Piggin     if ((new_msr & MSR_HVB) && books_vhyp_handles_hv_excp(cpu)) {
16137cebc5dbSNicholas Piggin         PPCVirtualHypervisorClass *vhc =
16147cebc5dbSNicholas Piggin             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
16157cebc5dbSNicholas Piggin         /* Deliver interrupt to L1 by returning from the H_ENTER_NESTED call */
16167cebc5dbSNicholas Piggin         vhc->deliver_hv_excp(cpu, excp);
16177cebc5dbSNicholas Piggin 
16187cebc5dbSNicholas Piggin         powerpc_reset_excp_state(cpu);
16197cebc5dbSNicholas Piggin 
16207cebc5dbSNicholas Piggin     } else {
16217cebc5dbSNicholas Piggin         /* Sanity check */
16227cebc5dbSNicholas Piggin         if (!(env->msr_mask & MSR_HVB) && srr0 == SPR_HSRR0) {
16237cebc5dbSNicholas Piggin             cpu_abort(cs, "Trying to deliver HV exception (HSRR) %d with "
16247cebc5dbSNicholas Piggin                       "no HV support\n", excp);
16257cebc5dbSNicholas Piggin         }
16267cebc5dbSNicholas Piggin 
16279f338e4dSFabiano Rosas         /* This can update new_msr and vector if AIL applies */
162810895ab6SFabiano Rosas         ppc_excp_apply_ail(cpu, excp, msr, &new_msr, &vector);
16299f338e4dSFabiano Rosas 
16309f338e4dSFabiano Rosas         powerpc_set_excp_state(cpu, vector, new_msr);
16319f338e4dSFabiano Rosas     }
16327cebc5dbSNicholas Piggin }
163330c4e426SFabiano Rosas #else
163430c4e426SFabiano Rosas static inline void powerpc_excp_books(PowerPCCPU *cpu, int excp)
163530c4e426SFabiano Rosas {
163630c4e426SFabiano Rosas     g_assert_not_reached();
163730c4e426SFabiano Rosas }
163830c4e426SFabiano Rosas #endif
16399f338e4dSFabiano Rosas 
1640dc88dd0aSFabiano Rosas static void powerpc_excp(PowerPCCPU *cpu, int excp)
1641dc88dd0aSFabiano Rosas {
1642c6eaac89SFabiano Rosas     CPUState *cs = CPU(cpu);
1643dc88dd0aSFabiano Rosas     CPUPPCState *env = &cpu->env;
1644dc88dd0aSFabiano Rosas 
1645c6eaac89SFabiano Rosas     if (excp <= POWERPC_EXCP_NONE || excp >= POWERPC_EXCP_NB) {
1646c6eaac89SFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
1647c6eaac89SFabiano Rosas     }
1648c6eaac89SFabiano Rosas 
1649c6eaac89SFabiano Rosas     qemu_log_mask(CPU_LOG_INT, "Raise exception at " TARGET_FMT_lx
1650c6eaac89SFabiano Rosas                   " => %s (%d) error=%02x\n", env->nip, powerpc_excp_name(excp),
1651c6eaac89SFabiano Rosas                   excp, env->error_code);
1652c6eaac89SFabiano Rosas 
1653dc88dd0aSFabiano Rosas     switch (env->excp_model) {
1654e808c2edSFabiano Rosas     case POWERPC_EXCP_40x:
1655e808c2edSFabiano Rosas         powerpc_excp_40x(cpu, excp);
1656e808c2edSFabiano Rosas         break;
165758d178fbSFabiano Rosas     case POWERPC_EXCP_6xx:
165858d178fbSFabiano Rosas         powerpc_excp_6xx(cpu, excp);
165958d178fbSFabiano Rosas         break;
1660ccfca2fcSFabiano Rosas     case POWERPC_EXCP_7xx:
1661ccfca2fcSFabiano Rosas         powerpc_excp_7xx(cpu, excp);
1662ccfca2fcSFabiano Rosas         break;
166352926b0dSFabiano Rosas     case POWERPC_EXCP_74xx:
166452926b0dSFabiano Rosas         powerpc_excp_74xx(cpu, excp);
166552926b0dSFabiano Rosas         break;
1666180952ceSFabiano Rosas     case POWERPC_EXCP_BOOKE:
1667180952ceSFabiano Rosas         powerpc_excp_booke(cpu, excp);
1668180952ceSFabiano Rosas         break;
16699f338e4dSFabiano Rosas     case POWERPC_EXCP_970:
16709f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER7:
16719f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER8:
16729f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER9:
16739f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER10:
16749f338e4dSFabiano Rosas         powerpc_excp_books(cpu, excp);
16759f338e4dSFabiano Rosas         break;
1676dc88dd0aSFabiano Rosas     default:
167728091374SFabiano Rosas         g_assert_not_reached();
1678dc88dd0aSFabiano Rosas     }
1679dc88dd0aSFabiano Rosas }
1680dc88dd0aSFabiano Rosas 
168197a8ea5aSAndreas Färber void ppc_cpu_do_interrupt(CPUState *cs)
1682c79c73f6SBlue Swirl {
168397a8ea5aSAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
16845c26a5b3SAndreas Färber 
168593130c84SFabiano Rosas     powerpc_excp(cpu, cs->exception_index);
1686c79c73f6SBlue Swirl }
1687c79c73f6SBlue Swirl 
16882dfecf01SMatheus Ferst #if defined(TARGET_PPC64)
1689c8e1de2eSMatheus Ferst #define P7_UNUSED_INTERRUPTS \
1690c8e1de2eSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_HVIRT | PPC_INTERRUPT_CEXT |       \
1691c8e1de2eSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |      \
1692c8e1de2eSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_DOORBELL | PPC_INTERRUPT_HDOORBELL | \
1693c8e1de2eSMatheus Ferst      PPC_INTERRUPT_THERM | PPC_INTERRUPT_EBB)
1694c8e1de2eSMatheus Ferst 
16959c713713SMatheus Ferst static int p7_interrupt_powersave(CPUPPCState *env)
16969c713713SMatheus Ferst {
16979c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
16989c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE0)) {
16999c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
17009c713713SMatheus Ferst     }
17019c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
17029c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE1)) {
17039c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
17049c713713SMatheus Ferst     }
17059c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_MCK) &&
17069c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
17079c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
17089c713713SMatheus Ferst     }
17099c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HMI) &&
17109c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
17119c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
17129c713713SMatheus Ferst     }
17139c713713SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
17149c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
17159c713713SMatheus Ferst     }
17169c713713SMatheus Ferst     return 0;
17179c713713SMatheus Ferst }
17189c713713SMatheus Ferst 
1719bf303fb3SMatheus Ferst static int p7_next_unmasked_interrupt(CPUPPCState *env)
1720bf303fb3SMatheus Ferst {
1721022b7128SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
1722022b7128SMatheus Ferst     CPUState *cs = CPU(cpu);
1723022b7128SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
1724022b7128SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1725bf303fb3SMatheus Ferst 
1726c8e1de2eSMatheus Ferst     assert((env->pending_interrupts & P7_UNUSED_INTERRUPTS) == 0);
1727c8e1de2eSMatheus Ferst 
1728022b7128SMatheus Ferst     if (cs->halted) {
1729022b7128SMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
1730022b7128SMatheus Ferst         return p7_interrupt_powersave(env);
1731022b7128SMatheus Ferst     }
1732022b7128SMatheus Ferst 
1733bf303fb3SMatheus Ferst     /* Machine check exception */
1734bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1735bf303fb3SMatheus Ferst         return PPC_INTERRUPT_MCK;
1736bf303fb3SMatheus Ferst     }
1737bf303fb3SMatheus Ferst 
1738bf303fb3SMatheus Ferst     /* Hypervisor decrementer exception */
1739bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1740bf303fb3SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1741bf303fb3SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
1742022b7128SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1743bf303fb3SMatheus Ferst             /* HDEC clears on delivery */
1744bf303fb3SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1745bf303fb3SMatheus Ferst         }
1746bf303fb3SMatheus Ferst     }
1747bf303fb3SMatheus Ferst 
1748bf303fb3SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1749bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1750bf303fb3SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1751bf303fb3SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1752bf303fb3SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
1753022b7128SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1754bf303fb3SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1755bf303fb3SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1756bf303fb3SMatheus Ferst             return PPC_INTERRUPT_EXT;
1757bf303fb3SMatheus Ferst         }
1758bf303fb3SMatheus Ferst     }
1759022b7128SMatheus Ferst     if (msr_ee != 0) {
1760bf303fb3SMatheus Ferst         /* Decrementer exception */
1761bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1762bf303fb3SMatheus Ferst             return PPC_INTERRUPT_DECR;
1763bf303fb3SMatheus Ferst         }
1764bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1765bf303fb3SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1766bf303fb3SMatheus Ferst         }
1767bf303fb3SMatheus Ferst     }
1768bf303fb3SMatheus Ferst 
1769bf303fb3SMatheus Ferst     return 0;
1770bf303fb3SMatheus Ferst }
1771bf303fb3SMatheus Ferst 
1772f6194fddSMatheus Ferst #define P8_UNUSED_INTERRUPTS \
1773f6194fddSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_HVIRT |  \
1774f6194fddSMatheus Ferst     PPC_INTERRUPT_CEXT | PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL |  \
1775f6194fddSMatheus Ferst     PPC_INTERRUPT_FIT | PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1776f6194fddSMatheus Ferst 
17779c713713SMatheus Ferst static int p8_interrupt_powersave(CPUPPCState *env)
17789c713713SMatheus Ferst {
17799c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
17809c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE2)) {
17819c713713SMatheus Ferst         return PPC_INTERRUPT_EXT;
17829c713713SMatheus Ferst     }
17839c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
17849c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE3)) {
17859c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
17869c713713SMatheus Ferst     }
17879c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_MCK) &&
17889c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) {
17899c713713SMatheus Ferst         return PPC_INTERRUPT_MCK;
17909c713713SMatheus Ferst     }
17919c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HMI) &&
17929c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) {
17939c713713SMatheus Ferst         return PPC_INTERRUPT_HMI;
17949c713713SMatheus Ferst     }
17959c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
17969c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE0)) {
17979c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
17989c713713SMatheus Ferst     }
17999c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
18009c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_P8_PECE1)) {
18019c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
18029c713713SMatheus Ferst     }
18039c713713SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
18049c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
18059c713713SMatheus Ferst     }
18069c713713SMatheus Ferst     return 0;
18079c713713SMatheus Ferst }
18089c713713SMatheus Ferst 
1809a9899d42SMatheus Ferst static int p8_next_unmasked_interrupt(CPUPPCState *env)
1810a9899d42SMatheus Ferst {
181164a9b5eeSMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
181264a9b5eeSMatheus Ferst     CPUState *cs = CPU(cpu);
181364a9b5eeSMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
181464a9b5eeSMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1815a9899d42SMatheus Ferst 
1816f6194fddSMatheus Ferst     assert((env->pending_interrupts & P8_UNUSED_INTERRUPTS) == 0);
1817f6194fddSMatheus Ferst 
181864a9b5eeSMatheus Ferst     if (cs->halted) {
181964a9b5eeSMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
182064a9b5eeSMatheus Ferst         return p8_interrupt_powersave(env);
182164a9b5eeSMatheus Ferst     }
182264a9b5eeSMatheus Ferst 
1823a9899d42SMatheus Ferst     /* Machine check exception */
1824a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1825a9899d42SMatheus Ferst         return PPC_INTERRUPT_MCK;
1826a9899d42SMatheus Ferst     }
1827a9899d42SMatheus Ferst 
1828a9899d42SMatheus Ferst     /* Hypervisor decrementer exception */
1829a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1830a9899d42SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1831a9899d42SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
183264a9b5eeSMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1833a9899d42SMatheus Ferst             /* HDEC clears on delivery */
1834a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1835a9899d42SMatheus Ferst         }
1836a9899d42SMatheus Ferst     }
1837a9899d42SMatheus Ferst 
1838a9899d42SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1839a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1840a9899d42SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1841a9899d42SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1842a9899d42SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
184364a9b5eeSMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1844a9899d42SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1845a9899d42SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1846a9899d42SMatheus Ferst             return PPC_INTERRUPT_EXT;
1847a9899d42SMatheus Ferst         }
1848a9899d42SMatheus Ferst     }
184964a9b5eeSMatheus Ferst     if (msr_ee != 0) {
1850a9899d42SMatheus Ferst         /* Decrementer exception */
1851a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1852a9899d42SMatheus Ferst             return PPC_INTERRUPT_DECR;
1853a9899d42SMatheus Ferst         }
1854a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1855a9899d42SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
1856a9899d42SMatheus Ferst         }
1857a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
1858a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
1859a9899d42SMatheus Ferst         }
1860a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1861a9899d42SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1862a9899d42SMatheus Ferst         }
1863a9899d42SMatheus Ferst         /* EBB exception */
1864a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
1865a9899d42SMatheus Ferst             /*
1866a9899d42SMatheus Ferst              * EBB exception must be taken in problem state and
1867a9899d42SMatheus Ferst              * with BESCR_GE set.
1868a9899d42SMatheus Ferst              */
1869a9899d42SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
1870a9899d42SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
1871a9899d42SMatheus Ferst                 return PPC_INTERRUPT_EBB;
1872a9899d42SMatheus Ferst             }
1873a9899d42SMatheus Ferst         }
1874a9899d42SMatheus Ferst     }
1875a9899d42SMatheus Ferst 
1876a9899d42SMatheus Ferst     return 0;
1877a9899d42SMatheus Ferst }
1878a9899d42SMatheus Ferst 
1879b00e9a2fSMatheus Ferst #define P9_UNUSED_INTERRUPTS \
1880b00e9a2fSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT |   \
1881b00e9a2fSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |  \
1882b00e9a2fSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1883b00e9a2fSMatheus Ferst 
18849c713713SMatheus Ferst static int p9_interrupt_powersave(CPUPPCState *env)
18859c713713SMatheus Ferst {
18869c713713SMatheus Ferst     /* External Exception */
18879c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
18889c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_EEE)) {
18899c713713SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
18909c713713SMatheus Ferst         if (!heic || !FIELD_EX64_HV(env->msr) ||
18919c713713SMatheus Ferst             FIELD_EX64(env->msr, MSR, PR)) {
18929c713713SMatheus Ferst             return PPC_INTERRUPT_EXT;
18939c713713SMatheus Ferst         }
18949c713713SMatheus Ferst     }
18959c713713SMatheus Ferst     /* Decrementer Exception */
18969c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
18979c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_DEE)) {
18989c713713SMatheus Ferst         return PPC_INTERRUPT_DECR;
18999c713713SMatheus Ferst     }
19009c713713SMatheus Ferst     /* Machine Check or Hypervisor Maintenance Exception */
19019c713713SMatheus Ferst     if (env->spr[SPR_LPCR] & LPCR_OEE) {
19029c713713SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
19039c713713SMatheus Ferst             return PPC_INTERRUPT_MCK;
19049c713713SMatheus Ferst         }
19059c713713SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HMI) {
19069c713713SMatheus Ferst             return PPC_INTERRUPT_HMI;
19079c713713SMatheus Ferst         }
19089c713713SMatheus Ferst     }
19099c713713SMatheus Ferst     /* Privileged Doorbell Exception */
19109c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
19119c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_PDEE)) {
19129c713713SMatheus Ferst         return PPC_INTERRUPT_DOORBELL;
19139c713713SMatheus Ferst     }
19149c713713SMatheus Ferst     /* Hypervisor Doorbell Exception */
19159c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
19169c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_HDEE)) {
19179c713713SMatheus Ferst         return PPC_INTERRUPT_HDOORBELL;
19189c713713SMatheus Ferst     }
19199c713713SMatheus Ferst     /* Hypervisor virtualization exception */
19209c713713SMatheus Ferst     if ((env->pending_interrupts & PPC_INTERRUPT_HVIRT) &&
19219c713713SMatheus Ferst         (env->spr[SPR_LPCR] & LPCR_HVEE)) {
19229c713713SMatheus Ferst         return PPC_INTERRUPT_HVIRT;
19239c713713SMatheus Ferst     }
19249c713713SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
19259c713713SMatheus Ferst         return PPC_INTERRUPT_RESET;
19269c713713SMatheus Ferst     }
19279c713713SMatheus Ferst     return 0;
19289c713713SMatheus Ferst }
19299c713713SMatheus Ferst 
19302dfecf01SMatheus Ferst static int p9_next_unmasked_interrupt(CPUPPCState *env)
19312dfecf01SMatheus Ferst {
193227796411SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
193327796411SMatheus Ferst     CPUState *cs = CPU(cpu);
193427796411SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
193527796411SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
19362dfecf01SMatheus Ferst 
1937b00e9a2fSMatheus Ferst     assert((env->pending_interrupts & P9_UNUSED_INTERRUPTS) == 0);
1938b00e9a2fSMatheus Ferst 
193927796411SMatheus Ferst     if (cs->halted) {
194027796411SMatheus Ferst         if (env->spr[SPR_PSSCR] & PSSCR_EC) {
194127796411SMatheus Ferst             /*
194227796411SMatheus Ferst              * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can
194327796411SMatheus Ferst              * wakeup the processor
194427796411SMatheus Ferst              */
194527796411SMatheus Ferst             return p9_interrupt_powersave(env);
194627796411SMatheus Ferst         } else {
194727796411SMatheus Ferst             /*
194827796411SMatheus Ferst              * When it's clear, any system-caused exception exits power-saving
194927796411SMatheus Ferst              * mode, even the ones that gate on MSR[EE].
195027796411SMatheus Ferst              */
195127796411SMatheus Ferst             msr_ee = true;
195227796411SMatheus Ferst         }
195327796411SMatheus Ferst     }
195427796411SMatheus Ferst 
19552dfecf01SMatheus Ferst     /* Machine check exception */
19562dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
19572dfecf01SMatheus Ferst         return PPC_INTERRUPT_MCK;
19582dfecf01SMatheus Ferst     }
19592dfecf01SMatheus Ferst 
19602dfecf01SMatheus Ferst     /* Hypervisor decrementer exception */
19612dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
19622dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
19632dfecf01SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
196427796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
19652dfecf01SMatheus Ferst             /* HDEC clears on delivery */
19662dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDECR;
19672dfecf01SMatheus Ferst         }
19682dfecf01SMatheus Ferst     }
19692dfecf01SMatheus Ferst 
19702dfecf01SMatheus Ferst     /* Hypervisor virtualization interrupt */
19712dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
19722dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
19732dfecf01SMatheus Ferst         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
197427796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hvice) {
19752dfecf01SMatheus Ferst             return PPC_INTERRUPT_HVIRT;
19762dfecf01SMatheus Ferst         }
19772dfecf01SMatheus Ferst     }
19782dfecf01SMatheus Ferst 
19792dfecf01SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
19802dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
19812dfecf01SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
19822dfecf01SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
19832dfecf01SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
198427796411SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
19852dfecf01SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
19862dfecf01SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
19872dfecf01SMatheus Ferst             return PPC_INTERRUPT_EXT;
19882dfecf01SMatheus Ferst         }
19892dfecf01SMatheus Ferst     }
199027796411SMatheus Ferst     if (msr_ee != 0) {
19912dfecf01SMatheus Ferst         /* Decrementer exception */
19922dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
19932dfecf01SMatheus Ferst             return PPC_INTERRUPT_DECR;
19942dfecf01SMatheus Ferst         }
19952dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
19962dfecf01SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
19972dfecf01SMatheus Ferst         }
19982dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
19992dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
20002dfecf01SMatheus Ferst         }
20012dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
20022dfecf01SMatheus Ferst             return PPC_INTERRUPT_PERFM;
20032dfecf01SMatheus Ferst         }
20042dfecf01SMatheus Ferst         /* EBB exception */
20052dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
20062dfecf01SMatheus Ferst             /*
20072dfecf01SMatheus Ferst              * EBB exception must be taken in problem state and
20082dfecf01SMatheus Ferst              * with BESCR_GE set.
20092dfecf01SMatheus Ferst              */
20102dfecf01SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
20112dfecf01SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
20122dfecf01SMatheus Ferst                 return PPC_INTERRUPT_EBB;
20132dfecf01SMatheus Ferst             }
20142dfecf01SMatheus Ferst         }
20152dfecf01SMatheus Ferst     }
20162dfecf01SMatheus Ferst 
20172dfecf01SMatheus Ferst     return 0;
20182dfecf01SMatheus Ferst }
20192dfecf01SMatheus Ferst #endif
20202dfecf01SMatheus Ferst 
2021ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt_generic(CPUPPCState *env)
2022c79c73f6SBlue Swirl {
20233621e2c9SBenjamin Herrenschmidt     bool async_deliver;
2024259186a7SAndreas Färber 
2025c79c73f6SBlue Swirl     /* External reset */
2026f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
2027de76b85cSMatheus Ferst         return PPC_INTERRUPT_RESET;
2028c79c73f6SBlue Swirl     }
2029c79c73f6SBlue Swirl     /* Machine check exception */
2030f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
2031de76b85cSMatheus Ferst         return PPC_INTERRUPT_MCK;
2032c79c73f6SBlue Swirl     }
2033c79c73f6SBlue Swirl #if 0 /* TODO */
2034c79c73f6SBlue Swirl     /* External debug exception */
2035f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) {
2036de76b85cSMatheus Ferst         return PPC_INTERRUPT_DEBUG;
2037c79c73f6SBlue Swirl     }
2038c79c73f6SBlue Swirl #endif
20393621e2c9SBenjamin Herrenschmidt 
20403621e2c9SBenjamin Herrenschmidt     /*
20413621e2c9SBenjamin Herrenschmidt      * For interrupts that gate on MSR:EE, we need to do something a
20423621e2c9SBenjamin Herrenschmidt      * bit more subtle, as we need to let them through even when EE is
20433621e2c9SBenjamin Herrenschmidt      * clear when coming out of some power management states (in order
20443621e2c9SBenjamin Herrenschmidt      * for them to become a 0x100).
20453621e2c9SBenjamin Herrenschmidt      */
20460939b8f8SVíctor Colombo     async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
20473621e2c9SBenjamin Herrenschmidt 
2048c79c73f6SBlue Swirl     /* Hypervisor decrementer exception */
2049f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
20504b236b62SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
20514b236b62SBenjamin Herrenschmidt         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
20529de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
20534b236b62SBenjamin Herrenschmidt             /* HDEC clears on delivery */
2054de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDECR;
2055c79c73f6SBlue Swirl         }
2056c79c73f6SBlue Swirl     }
2057d8ce5fd6SBenjamin Herrenschmidt 
2058d8ce5fd6SBenjamin Herrenschmidt     /* Hypervisor virtualization interrupt */
2059f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
2060d8ce5fd6SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
2061d8ce5fd6SBenjamin Herrenschmidt         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
20629de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
2063de76b85cSMatheus Ferst             return PPC_INTERRUPT_HVIRT;
2064d8ce5fd6SBenjamin Herrenschmidt         }
2065d8ce5fd6SBenjamin Herrenschmidt     }
2066d8ce5fd6SBenjamin Herrenschmidt 
2067d8ce5fd6SBenjamin Herrenschmidt     /* External interrupt can ignore MSR:EE under some circumstances */
2068f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
2069d1dbe37cSBenjamin Herrenschmidt         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
20706eebe6dcSBenjamin Herrenschmidt         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
20716eebe6dcSBenjamin Herrenschmidt         /* HEIC blocks delivery to the hypervisor */
20729de754d3SVíctor Colombo         if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
2073d41ccf6eSVíctor Colombo             !FIELD_EX64(env->msr, MSR, PR))) ||
20749de754d3SVíctor Colombo             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
2075de76b85cSMatheus Ferst             return PPC_INTERRUPT_EXT;
2076d1dbe37cSBenjamin Herrenschmidt         }
2077d1dbe37cSBenjamin Herrenschmidt     }
2078acc861c2SVíctor Colombo     if (FIELD_EX64(env->msr, MSR, CE)) {
2079c79c73f6SBlue Swirl         /* External critical interrupt */
2080f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CEXT) {
2081de76b85cSMatheus Ferst             return PPC_INTERRUPT_CEXT;
2082c79c73f6SBlue Swirl         }
2083c79c73f6SBlue Swirl     }
20843621e2c9SBenjamin Herrenschmidt     if (async_deliver != 0) {
2085c79c73f6SBlue Swirl         /* Watchdog timer on embedded PowerPC */
2086f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_WDT) {
2087de76b85cSMatheus Ferst             return PPC_INTERRUPT_WDT;
2088c79c73f6SBlue Swirl         }
2089f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) {
2090de76b85cSMatheus Ferst             return PPC_INTERRUPT_CDOORBELL;
2091c79c73f6SBlue Swirl         }
2092c79c73f6SBlue Swirl         /* Fixed interval timer on embedded PowerPC */
2093f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_FIT) {
2094de76b85cSMatheus Ferst             return PPC_INTERRUPT_FIT;
2095c79c73f6SBlue Swirl         }
2096c79c73f6SBlue Swirl         /* Programmable interval timer on embedded PowerPC */
2097f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PIT) {
2098de76b85cSMatheus Ferst             return PPC_INTERRUPT_PIT;
2099c79c73f6SBlue Swirl         }
2100c79c73f6SBlue Swirl         /* Decrementer exception */
2101f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
2102de76b85cSMatheus Ferst             return PPC_INTERRUPT_DECR;
2103c79c73f6SBlue Swirl         }
2104f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2105de76b85cSMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
2106c79c73f6SBlue Swirl         }
2107f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
2108de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
21097af1e7b0SCédric Le Goater         }
2110f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
2111de76b85cSMatheus Ferst             return PPC_INTERRUPT_PERFM;
2112c79c73f6SBlue Swirl         }
2113c79c73f6SBlue Swirl         /* Thermal interrupt */
2114f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_THERM) {
2115de76b85cSMatheus Ferst             return PPC_INTERRUPT_THERM;
2116c79c73f6SBlue Swirl         }
2117cb76bbc4SDaniel Henrique Barboza         /* EBB exception */
2118f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
2119cb76bbc4SDaniel Henrique Barboza             /*
2120cb76bbc4SDaniel Henrique Barboza              * EBB exception must be taken in problem state and
2121cb76bbc4SDaniel Henrique Barboza              * with BESCR_GE set.
2122cb76bbc4SDaniel Henrique Barboza              */
2123d41ccf6eSVíctor Colombo             if (FIELD_EX64(env->msr, MSR, PR) &&
2124d41ccf6eSVíctor Colombo                 (env->spr[SPR_BESCR] & BESCR_GE)) {
2125de76b85cSMatheus Ferst                 return PPC_INTERRUPT_EBB;
2126de76b85cSMatheus Ferst             }
2127de76b85cSMatheus Ferst         }
2128de76b85cSMatheus Ferst     }
2129cb76bbc4SDaniel Henrique Barboza 
2130de76b85cSMatheus Ferst     return 0;
2131de76b85cSMatheus Ferst }
2132de76b85cSMatheus Ferst 
2133ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt(CPUPPCState *env)
2134ba2898f7SMatheus Ferst {
2135ba2898f7SMatheus Ferst     switch (env->excp_model) {
21362dfecf01SMatheus Ferst #if defined(TARGET_PPC64)
2137bf303fb3SMatheus Ferst     case POWERPC_EXCP_POWER7:
2138bf303fb3SMatheus Ferst         return p7_next_unmasked_interrupt(env);
2139a9899d42SMatheus Ferst     case POWERPC_EXCP_POWER8:
2140a9899d42SMatheus Ferst         return p8_next_unmasked_interrupt(env);
21412dfecf01SMatheus Ferst     case POWERPC_EXCP_POWER9:
21422dfecf01SMatheus Ferst     case POWERPC_EXCP_POWER10:
21432dfecf01SMatheus Ferst         return p9_next_unmasked_interrupt(env);
21442dfecf01SMatheus Ferst #endif
2145ba2898f7SMatheus Ferst     default:
2146ba2898f7SMatheus Ferst         return ppc_next_unmasked_interrupt_generic(env);
2147ba2898f7SMatheus Ferst     }
2148ba2898f7SMatheus Ferst }
2149ba2898f7SMatheus Ferst 
21502fdedcbcSMatheus Ferst /*
21512fdedcbcSMatheus Ferst  * Sets CPU_INTERRUPT_HARD if there is at least one unmasked interrupt to be
21522fdedcbcSMatheus Ferst  * delivered and clears CPU_INTERRUPT_HARD otherwise.
21532fdedcbcSMatheus Ferst  *
21542fdedcbcSMatheus Ferst  * This method is called by ppc_set_interrupt when an interrupt is raised or
21552fdedcbcSMatheus Ferst  * lowered, and should also be called whenever an interrupt masking condition
21562fdedcbcSMatheus Ferst  * is changed, e.g.:
21572fdedcbcSMatheus Ferst  *  - When relevant bits of MSR are altered, like EE, HV, PR, etc.;
21582fdedcbcSMatheus Ferst  *  - When relevant bits of LPCR are altered, like PECE, HDICE, HVICE, etc.;
21592fdedcbcSMatheus Ferst  *  - When PSSCR[EC] or env->resume_as_sreset are changed;
21602fdedcbcSMatheus Ferst  *  - When cs->halted is changed and the CPU has a different interrupt masking
21612fdedcbcSMatheus Ferst  *    logic in power-saving mode (e.g., POWER7/8/9/10);
21622fdedcbcSMatheus Ferst  */
21632fdedcbcSMatheus Ferst void ppc_maybe_interrupt(CPUPPCState *env)
21642fdedcbcSMatheus Ferst {
21652fdedcbcSMatheus Ferst     CPUState *cs = env_cpu(env);
21662fdedcbcSMatheus Ferst     bool locked = false;
21672fdedcbcSMatheus Ferst 
21682fdedcbcSMatheus Ferst     if (!qemu_mutex_iothread_locked()) {
21692fdedcbcSMatheus Ferst         locked = true;
21702fdedcbcSMatheus Ferst         qemu_mutex_lock_iothread();
21712fdedcbcSMatheus Ferst     }
21722fdedcbcSMatheus Ferst 
21732fdedcbcSMatheus Ferst     if (ppc_next_unmasked_interrupt(env)) {
21742fdedcbcSMatheus Ferst         cpu_interrupt(cs, CPU_INTERRUPT_HARD);
21752fdedcbcSMatheus Ferst     } else {
21762fdedcbcSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
21772fdedcbcSMatheus Ferst     }
21782fdedcbcSMatheus Ferst 
21792fdedcbcSMatheus Ferst     if (locked) {
21802fdedcbcSMatheus Ferst         qemu_mutex_unlock_iothread();
21812fdedcbcSMatheus Ferst     }
21822fdedcbcSMatheus Ferst }
21832fdedcbcSMatheus Ferst 
21843654e238SMatheus Ferst #if defined(TARGET_PPC64)
2185d93a4856SMatheus Ferst static void p7_deliver_interrupt(CPUPPCState *env, int interrupt)
2186d93a4856SMatheus Ferst {
2187d93a4856SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2188d93a4856SMatheus Ferst     CPUState *cs = env_cpu(env);
2189d93a4856SMatheus Ferst 
2190d93a4856SMatheus Ferst     switch (interrupt) {
2191d93a4856SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2192d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2193d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2194d93a4856SMatheus Ferst         break;
2195d93a4856SMatheus Ferst 
2196d93a4856SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2197d93a4856SMatheus Ferst         /* HDEC clears on delivery */
2198d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2199d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2200d93a4856SMatheus Ferst         break;
2201d93a4856SMatheus Ferst 
2202d93a4856SMatheus Ferst     case PPC_INTERRUPT_EXT:
2203d93a4856SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2204d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2205d93a4856SMatheus Ferst         } else {
2206d93a4856SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2207d93a4856SMatheus Ferst         }
2208d93a4856SMatheus Ferst         break;
2209d93a4856SMatheus Ferst 
2210d93a4856SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2211d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2212d93a4856SMatheus Ferst         break;
2213d93a4856SMatheus Ferst     case PPC_INTERRUPT_PERFM:
2214d93a4856SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2215d93a4856SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2216d93a4856SMatheus Ferst         break;
2217d93a4856SMatheus Ferst     case 0:
2218d93a4856SMatheus Ferst         /*
2219d93a4856SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
2220d93a4856SMatheus Ferst          * anything to deliver while in a PM state that requires getting
2221d93a4856SMatheus Ferst          * out via a 0x100
2222d93a4856SMatheus Ferst          *
2223d93a4856SMatheus Ferst          * This means we will incorrectly execute past the power management
2224d93a4856SMatheus Ferst          * instruction instead of triggering a reset.
2225d93a4856SMatheus Ferst          *
2226d93a4856SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
2227d93a4856SMatheus Ferst          * processor has_work implementation and the logic in this function.
2228d93a4856SMatheus Ferst          */
2229d93a4856SMatheus Ferst         assert(!env->resume_as_sreset);
2230d93a4856SMatheus Ferst         break;
2231d93a4856SMatheus Ferst     default:
2232d93a4856SMatheus Ferst         cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
2233d93a4856SMatheus Ferst     }
2234d93a4856SMatheus Ferst }
2235d93a4856SMatheus Ferst 
22366527e757SMatheus Ferst static void p8_deliver_interrupt(CPUPPCState *env, int interrupt)
22376527e757SMatheus Ferst {
22386527e757SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
22396527e757SMatheus Ferst     CPUState *cs = env_cpu(env);
22406527e757SMatheus Ferst 
22416527e757SMatheus Ferst     switch (interrupt) {
22426527e757SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
22436527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
22446527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
22456527e757SMatheus Ferst         break;
22466527e757SMatheus Ferst 
22476527e757SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
22486527e757SMatheus Ferst         /* HDEC clears on delivery */
22496527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
22506527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
22516527e757SMatheus Ferst         break;
22526527e757SMatheus Ferst 
22536527e757SMatheus Ferst     case PPC_INTERRUPT_EXT:
22546527e757SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
22556527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
22566527e757SMatheus Ferst         } else {
22576527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
22586527e757SMatheus Ferst         }
22596527e757SMatheus Ferst         break;
22606527e757SMatheus Ferst 
22616527e757SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
22626527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
22636527e757SMatheus Ferst         break;
22646527e757SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
22656527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
22666527e757SMatheus Ferst         if (is_book3s_arch2x(env)) {
22676527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
22686527e757SMatheus Ferst         } else {
22696527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
22706527e757SMatheus Ferst         }
22716527e757SMatheus Ferst         break;
22726527e757SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
22736527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
22746527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
22756527e757SMatheus Ferst         break;
22766527e757SMatheus Ferst     case PPC_INTERRUPT_PERFM:
22776527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
22786527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
22796527e757SMatheus Ferst         break;
22806527e757SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
22816527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
22826527e757SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
22836527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
22846527e757SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
22856527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
22866527e757SMatheus Ferst         }
22876527e757SMatheus Ferst         break;
22886527e757SMatheus Ferst     case 0:
22896527e757SMatheus Ferst         /*
22906527e757SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
22916527e757SMatheus Ferst          * anything to deliver while in a PM state that requires getting
22926527e757SMatheus Ferst          * out via a 0x100
22936527e757SMatheus Ferst          *
22946527e757SMatheus Ferst          * This means we will incorrectly execute past the power management
22956527e757SMatheus Ferst          * instruction instead of triggering a reset.
22966527e757SMatheus Ferst          *
22976527e757SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
22986527e757SMatheus Ferst          * processor has_work implementation and the logic in this function.
22996527e757SMatheus Ferst          */
23006527e757SMatheus Ferst         assert(!env->resume_as_sreset);
23016527e757SMatheus Ferst         break;
23026527e757SMatheus Ferst     default:
23036527e757SMatheus Ferst         cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
23046527e757SMatheus Ferst     }
23056527e757SMatheus Ferst }
23066527e757SMatheus Ferst 
23073654e238SMatheus Ferst static void p9_deliver_interrupt(CPUPPCState *env, int interrupt)
23083654e238SMatheus Ferst {
23093654e238SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
23103654e238SMatheus Ferst     CPUState *cs = env_cpu(env);
23113654e238SMatheus Ferst 
231227796411SMatheus Ferst     if (cs->halted && !(env->spr[SPR_PSSCR] & PSSCR_EC) &&
231327796411SMatheus Ferst         !FIELD_EX64(env->msr, MSR, EE)) {
231427796411SMatheus Ferst         /*
231527796411SMatheus Ferst          * A pending interrupt took us out of power-saving, but MSR[EE] says
231627796411SMatheus Ferst          * that we should return to NIP+4 instead of delivering it.
231727796411SMatheus Ferst          */
231827796411SMatheus Ferst         return;
231927796411SMatheus Ferst     }
232027796411SMatheus Ferst 
23213654e238SMatheus Ferst     switch (interrupt) {
23223654e238SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
23233654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
23243654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
23253654e238SMatheus Ferst         break;
23263654e238SMatheus Ferst 
23273654e238SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
23283654e238SMatheus Ferst         /* HDEC clears on delivery */
23293654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
23303654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
23313654e238SMatheus Ferst         break;
23323654e238SMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
23333654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
23343654e238SMatheus Ferst         break;
23353654e238SMatheus Ferst 
23363654e238SMatheus Ferst     case PPC_INTERRUPT_EXT:
23373654e238SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
23383654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
23393654e238SMatheus Ferst         } else {
23403654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
23413654e238SMatheus Ferst         }
23423654e238SMatheus Ferst         break;
23433654e238SMatheus Ferst 
23443654e238SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
23453654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
23463654e238SMatheus Ferst         break;
23473654e238SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
23483654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
23493654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
23503654e238SMatheus Ferst         break;
23513654e238SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
23523654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
23533654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
23543654e238SMatheus Ferst         break;
23553654e238SMatheus Ferst     case PPC_INTERRUPT_PERFM:
23563654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
23573654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
23583654e238SMatheus Ferst         break;
23593654e238SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
23603654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
23613654e238SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
23623654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
23633654e238SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
23643654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
23653654e238SMatheus Ferst         }
23663654e238SMatheus Ferst         break;
23673654e238SMatheus Ferst     case 0:
23683654e238SMatheus Ferst         /*
23693654e238SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
23703654e238SMatheus Ferst          * anything to deliver while in a PM state that requires getting
23713654e238SMatheus Ferst          * out via a 0x100
23723654e238SMatheus Ferst          *
23733654e238SMatheus Ferst          * This means we will incorrectly execute past the power management
23743654e238SMatheus Ferst          * instruction instead of triggering a reset.
23753654e238SMatheus Ferst          *
23763654e238SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
23773654e238SMatheus Ferst          * processor has_work implementation and the logic in this function.
23783654e238SMatheus Ferst          */
23793654e238SMatheus Ferst         assert(!env->resume_as_sreset);
23803654e238SMatheus Ferst         break;
23813654e238SMatheus Ferst     default:
23823654e238SMatheus Ferst         cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
23833654e238SMatheus Ferst     }
23843654e238SMatheus Ferst }
23853654e238SMatheus Ferst #endif
23863654e238SMatheus Ferst 
2387ba2898f7SMatheus Ferst static void ppc_deliver_interrupt_generic(CPUPPCState *env, int interrupt)
2388de76b85cSMatheus Ferst {
2389de76b85cSMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2390de76b85cSMatheus Ferst     CPUState *cs = env_cpu(env);
2391de76b85cSMatheus Ferst 
2392de76b85cSMatheus Ferst     switch (interrupt) {
2393de76b85cSMatheus Ferst     case PPC_INTERRUPT_RESET: /* External reset */
2394de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_RESET;
2395de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_RESET);
2396de76b85cSMatheus Ferst         break;
2397de76b85cSMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2398de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2399de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2400de76b85cSMatheus Ferst         break;
2401de76b85cSMatheus Ferst 
2402de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2403de76b85cSMatheus Ferst         /* HDEC clears on delivery */
2404de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2405de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2406de76b85cSMatheus Ferst         break;
2407de76b85cSMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
2408de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2409de76b85cSMatheus Ferst         break;
2410de76b85cSMatheus Ferst 
2411de76b85cSMatheus Ferst     case PPC_INTERRUPT_EXT:
2412de76b85cSMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2413de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2414de76b85cSMatheus Ferst         } else {
2415de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2416de76b85cSMatheus Ferst         }
2417de76b85cSMatheus Ferst         break;
2418de76b85cSMatheus Ferst     case PPC_INTERRUPT_CEXT: /* External critical interrupt */
2419de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_CRITICAL);
2420de76b85cSMatheus Ferst         break;
2421de76b85cSMatheus Ferst 
2422de76b85cSMatheus Ferst     case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */
2423de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_WDT;
2424de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_WDT);
2425de76b85cSMatheus Ferst         break;
2426de76b85cSMatheus Ferst     case PPC_INTERRUPT_CDOORBELL:
2427de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL;
2428de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DOORCI);
2429de76b85cSMatheus Ferst         break;
2430de76b85cSMatheus Ferst     case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */
2431de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_FIT;
2432de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_FIT);
2433de76b85cSMatheus Ferst         break;
2434de76b85cSMatheus Ferst     case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */
2435de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PIT;
2436de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PIT);
2437de76b85cSMatheus Ferst         break;
2438de76b85cSMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2439de76b85cSMatheus Ferst         if (ppc_decr_clear_on_delivery(env)) {
2440de76b85cSMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DECR;
2441de76b85cSMatheus Ferst         }
2442de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2443de76b85cSMatheus Ferst         break;
2444de76b85cSMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
2445de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
2446de76b85cSMatheus Ferst         if (is_book3s_arch2x(env)) {
2447de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
2448de76b85cSMatheus Ferst         } else {
2449de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
2450de76b85cSMatheus Ferst         }
2451de76b85cSMatheus Ferst         break;
2452de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
2453de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
2454de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
2455de76b85cSMatheus Ferst         break;
2456de76b85cSMatheus Ferst     case PPC_INTERRUPT_PERFM:
2457de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2458de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2459de76b85cSMatheus Ferst         break;
2460de76b85cSMatheus Ferst     case PPC_INTERRUPT_THERM:  /* Thermal interrupt */
2461de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_THERM;
2462de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_THERM);
2463de76b85cSMatheus Ferst         break;
2464de76b85cSMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
2465de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
2466cb76bbc4SDaniel Henrique Barboza         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
2467cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
2468cb76bbc4SDaniel Henrique Barboza         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
2469cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
2470cb76bbc4SDaniel Henrique Barboza         }
2471de76b85cSMatheus Ferst         break;
2472de76b85cSMatheus Ferst     case 0:
2473f8154fd2SBenjamin Herrenschmidt         /*
2474f8154fd2SBenjamin Herrenschmidt          * This is a bug ! It means that has_work took us out of halt without
2475f8154fd2SBenjamin Herrenschmidt          * anything to deliver while in a PM state that requires getting
2476f8154fd2SBenjamin Herrenschmidt          * out via a 0x100
2477f8154fd2SBenjamin Herrenschmidt          *
2478f8154fd2SBenjamin Herrenschmidt          * This means we will incorrectly execute past the power management
2479f8154fd2SBenjamin Herrenschmidt          * instruction instead of triggering a reset.
2480f8154fd2SBenjamin Herrenschmidt          *
2481136fbf65Szhaolichang          * It generally means a discrepancy between the wakeup conditions in the
2482f8154fd2SBenjamin Herrenschmidt          * processor has_work implementation and the logic in this function.
2483f8154fd2SBenjamin Herrenschmidt          */
2484de76b85cSMatheus Ferst         assert(!env->resume_as_sreset);
2485de76b85cSMatheus Ferst         break;
2486de76b85cSMatheus Ferst     default:
2487de76b85cSMatheus Ferst         cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
2488f8154fd2SBenjamin Herrenschmidt     }
2489c79c73f6SBlue Swirl }
249034316482SAlexey Kardashevskiy 
2491ba2898f7SMatheus Ferst static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt)
2492ba2898f7SMatheus Ferst {
2493ba2898f7SMatheus Ferst     switch (env->excp_model) {
24943654e238SMatheus Ferst #if defined(TARGET_PPC64)
2495d93a4856SMatheus Ferst     case POWERPC_EXCP_POWER7:
2496d93a4856SMatheus Ferst         p7_deliver_interrupt(env, interrupt);
2497d93a4856SMatheus Ferst         break;
24986527e757SMatheus Ferst     case POWERPC_EXCP_POWER8:
24996527e757SMatheus Ferst         p8_deliver_interrupt(env, interrupt);
25006527e757SMatheus Ferst         break;
25013654e238SMatheus Ferst     case POWERPC_EXCP_POWER9:
25023654e238SMatheus Ferst     case POWERPC_EXCP_POWER10:
25033654e238SMatheus Ferst         p9_deliver_interrupt(env, interrupt);
25043654e238SMatheus Ferst         break;
25053654e238SMatheus Ferst #endif
2506ba2898f7SMatheus Ferst     default:
2507ba2898f7SMatheus Ferst         ppc_deliver_interrupt_generic(env, interrupt);
2508ba2898f7SMatheus Ferst     }
2509ba2898f7SMatheus Ferst }
2510ba2898f7SMatheus Ferst 
2511b5b7f391SNicholas Piggin void ppc_cpu_do_system_reset(CPUState *cs)
251234316482SAlexey Kardashevskiy {
251334316482SAlexey Kardashevskiy     PowerPCCPU *cpu = POWERPC_CPU(cs);
251434316482SAlexey Kardashevskiy 
251593130c84SFabiano Rosas     powerpc_excp(cpu, POWERPC_EXCP_RESET);
251634316482SAlexey Kardashevskiy }
2517ad77c6caSNicholas Piggin 
2518ad77c6caSNicholas Piggin void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector)
2519ad77c6caSNicholas Piggin {
2520ad77c6caSNicholas Piggin     PowerPCCPU *cpu = POWERPC_CPU(cs);
2521ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
2522ad77c6caSNicholas Piggin     target_ulong msr = 0;
2523ad77c6caSNicholas Piggin 
2524ad77c6caSNicholas Piggin     /*
2525ad77c6caSNicholas Piggin      * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already
2526ad77c6caSNicholas Piggin      * been set by KVM.
2527ad77c6caSNicholas Piggin      */
2528ad77c6caSNicholas Piggin     msr = (1ULL << MSR_ME);
2529ad77c6caSNicholas Piggin     msr |= env->msr & (1ULL << MSR_SF);
2530516fc103SFabiano Rosas     if (ppc_interrupts_little_endian(cpu, false)) {
2531ad77c6caSNicholas Piggin         msr |= (1ULL << MSR_LE);
2532ad77c6caSNicholas Piggin     }
2533ad77c6caSNicholas Piggin 
25347cebc5dbSNicholas Piggin     /* Anything for nested required here? MSR[HV] bit? */
25357cebc5dbSNicholas Piggin 
2536ad77c6caSNicholas Piggin     powerpc_set_excp_state(cpu, vector, msr);
2537ad77c6caSNicholas Piggin }
2538c79c73f6SBlue Swirl 
2539458dd766SRichard Henderson bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
2540458dd766SRichard Henderson {
2541458dd766SRichard Henderson     PowerPCCPU *cpu = POWERPC_CPU(cs);
2542458dd766SRichard Henderson     CPUPPCState *env = &cpu->env;
2543de76b85cSMatheus Ferst     int interrupt;
2544458dd766SRichard Henderson 
2545de76b85cSMatheus Ferst     if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) {
2546de76b85cSMatheus Ferst         return false;
2547de76b85cSMatheus Ferst     }
2548de76b85cSMatheus Ferst 
2549de76b85cSMatheus Ferst     interrupt = ppc_next_unmasked_interrupt(env);
2550de76b85cSMatheus Ferst     if (interrupt == 0) {
2551de76b85cSMatheus Ferst         return false;
2552de76b85cSMatheus Ferst     }
2553de76b85cSMatheus Ferst 
2554de76b85cSMatheus Ferst     ppc_deliver_interrupt(env, interrupt);
2555458dd766SRichard Henderson     if (env->pending_interrupts == 0) {
2556de76b85cSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
2557458dd766SRichard Henderson     }
2558458dd766SRichard Henderson     return true;
2559458dd766SRichard Henderson }
2560458dd766SRichard Henderson 
2561f725245cSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
2562f725245cSPhilippe Mathieu-Daudé 
2563ad71ed68SBlue Swirl /*****************************************************************************/
2564ad71ed68SBlue Swirl /* Exceptions processing helpers */
2565ad71ed68SBlue Swirl 
2566db789c6cSBenjamin Herrenschmidt void raise_exception_err_ra(CPUPPCState *env, uint32_t exception,
2567db789c6cSBenjamin Herrenschmidt                             uint32_t error_code, uintptr_t raddr)
2568ad71ed68SBlue Swirl {
2569db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
257027103424SAndreas Färber 
257127103424SAndreas Färber     cs->exception_index = exception;
2572ad71ed68SBlue Swirl     env->error_code = error_code;
2573db789c6cSBenjamin Herrenschmidt     cpu_loop_exit_restore(cs, raddr);
2574db789c6cSBenjamin Herrenschmidt }
2575db789c6cSBenjamin Herrenschmidt 
2576db789c6cSBenjamin Herrenschmidt void raise_exception_err(CPUPPCState *env, uint32_t exception,
2577db789c6cSBenjamin Herrenschmidt                          uint32_t error_code)
2578db789c6cSBenjamin Herrenschmidt {
2579db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2580db789c6cSBenjamin Herrenschmidt }
2581db789c6cSBenjamin Herrenschmidt 
2582db789c6cSBenjamin Herrenschmidt void raise_exception(CPUPPCState *env, uint32_t exception)
2583db789c6cSBenjamin Herrenschmidt {
2584db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2585db789c6cSBenjamin Herrenschmidt }
2586db789c6cSBenjamin Herrenschmidt 
2587db789c6cSBenjamin Herrenschmidt void raise_exception_ra(CPUPPCState *env, uint32_t exception,
2588db789c6cSBenjamin Herrenschmidt                         uintptr_t raddr)
2589db789c6cSBenjamin Herrenschmidt {
2590db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, raddr);
2591db789c6cSBenjamin Herrenschmidt }
2592db789c6cSBenjamin Herrenschmidt 
25932b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2594db789c6cSBenjamin Herrenschmidt void helper_raise_exception_err(CPUPPCState *env, uint32_t exception,
2595db789c6cSBenjamin Herrenschmidt                                 uint32_t error_code)
2596db789c6cSBenjamin Herrenschmidt {
2597db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2598ad71ed68SBlue Swirl }
2599ad71ed68SBlue Swirl 
2600e5f17ac6SBlue Swirl void helper_raise_exception(CPUPPCState *env, uint32_t exception)
2601ad71ed68SBlue Swirl {
2602db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2603ad71ed68SBlue Swirl }
26042b44e219SBruno Larsen (billionai) #endif
2605ad71ed68SBlue Swirl 
2606ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY)
26072b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2608e5f17ac6SBlue Swirl void helper_store_msr(CPUPPCState *env, target_ulong val)
2609ad71ed68SBlue Swirl {
2610db789c6cSBenjamin Herrenschmidt     uint32_t excp = hreg_store_msr(env, val, 0);
2611259186a7SAndreas Färber 
2612db789c6cSBenjamin Herrenschmidt     if (excp != 0) {
2613db70b311SRichard Henderson         CPUState *cs = env_cpu(env);
2614044897efSRichard Purdie         cpu_interrupt_exittb(cs);
2615db789c6cSBenjamin Herrenschmidt         raise_exception(env, excp);
2616ad71ed68SBlue Swirl     }
2617ad71ed68SBlue Swirl }
2618ad71ed68SBlue Swirl 
26192fdedcbcSMatheus Ferst void helper_ppc_maybe_interrupt(CPUPPCState *env)
26202fdedcbcSMatheus Ferst {
26212fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
26222fdedcbcSMatheus Ferst }
26232fdedcbcSMatheus Ferst 
26247778a575SBenjamin Herrenschmidt #if defined(TARGET_PPC64)
2625f43520e5SRichard Henderson void helper_scv(CPUPPCState *env, uint32_t lev)
2626f43520e5SRichard Henderson {
2627f43520e5SRichard Henderson     if (env->spr[SPR_FSCR] & (1ull << FSCR_SCV)) {
2628f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_SYSCALL_VECTORED, lev);
2629f43520e5SRichard Henderson     } else {
2630f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_FU, FSCR_IC_SCV);
2631f43520e5SRichard Henderson     }
2632f43520e5SRichard Henderson }
2633f43520e5SRichard Henderson 
26347778a575SBenjamin Herrenschmidt void helper_pminsn(CPUPPCState *env, powerpc_pm_insn_t insn)
26357778a575SBenjamin Herrenschmidt {
26367778a575SBenjamin Herrenschmidt     CPUState *cs;
26377778a575SBenjamin Herrenschmidt 
2638db70b311SRichard Henderson     cs = env_cpu(env);
26397778a575SBenjamin Herrenschmidt     cs->halted = 1;
26407778a575SBenjamin Herrenschmidt 
26413621e2c9SBenjamin Herrenschmidt     /* Condition for waking up at 0x100 */
26421e7fd61dSBenjamin Herrenschmidt     env->resume_as_sreset = (insn != PPC_PM_STOP) ||
264321c0d66aSBenjamin Herrenschmidt         (env->spr[SPR_PSSCR] & PSSCR_EC);
26442fdedcbcSMatheus Ferst 
26452fdedcbcSMatheus Ferst     ppc_maybe_interrupt(env);
26467778a575SBenjamin Herrenschmidt }
26477778a575SBenjamin Herrenschmidt #endif /* defined(TARGET_PPC64) */
26487778a575SBenjamin Herrenschmidt 
264962e79ef9SCédric Le Goater static void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr)
2650ad71ed68SBlue Swirl {
2651db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
2652259186a7SAndreas Färber 
2653a2e71b28SBenjamin Herrenschmidt     /* MSR:POW cannot be set by any form of rfi */
2654a2e71b28SBenjamin Herrenschmidt     msr &= ~(1ULL << MSR_POW);
2655a2e71b28SBenjamin Herrenschmidt 
26565aad0457SChristophe Leroy     /* MSR:TGPR cannot be set by any form of rfi */
26575aad0457SChristophe Leroy     if (env->flags & POWERPC_FLAG_TGPR)
26585aad0457SChristophe Leroy         msr &= ~(1ULL << MSR_TGPR);
26595aad0457SChristophe Leroy 
2660ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2661a2e71b28SBenjamin Herrenschmidt     /* Switching to 32-bit ? Crop the nip */
2662a2e71b28SBenjamin Herrenschmidt     if (!msr_is_64bit(env, msr)) {
2663ad71ed68SBlue Swirl         nip = (uint32_t)nip;
2664ad71ed68SBlue Swirl     }
2665ad71ed68SBlue Swirl #else
2666ad71ed68SBlue Swirl     nip = (uint32_t)nip;
2667ad71ed68SBlue Swirl #endif
2668ad71ed68SBlue Swirl     /* XXX: beware: this is false if VLE is supported */
2669ad71ed68SBlue Swirl     env->nip = nip & ~((target_ulong)0x00000003);
2670ad71ed68SBlue Swirl     hreg_store_msr(env, msr, 1);
26712eb1ef73SCédric Le Goater     trace_ppc_excp_rfi(env->nip, env->msr);
267247733729SDavid Gibson     /*
267347733729SDavid Gibson      * No need to raise an exception here, as rfi is always the last
267447733729SDavid Gibson      * insn of a TB
2675ad71ed68SBlue Swirl      */
2676044897efSRichard Purdie     cpu_interrupt_exittb(cs);
2677a8b73734SNikunj A Dadhania     /* Reset the reservation */
2678a8b73734SNikunj A Dadhania     env->reserve_addr = -1;
2679a8b73734SNikunj A Dadhania 
2680cd0c6f47SBenjamin Herrenschmidt     /* Context synchronizing: check if TCG TLB needs flush */
2681e3cffe6fSNikunj A Dadhania     check_tlb_flush(env, false);
2682ad71ed68SBlue Swirl }
2683ad71ed68SBlue Swirl 
2684e5f17ac6SBlue Swirl void helper_rfi(CPUPPCState *env)
2685ad71ed68SBlue Swirl {
2686a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful);
2687a1bb7384SScott Wood }
2688ad71ed68SBlue Swirl 
2689ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2690e5f17ac6SBlue Swirl void helper_rfid(CPUPPCState *env)
2691ad71ed68SBlue Swirl {
269247733729SDavid Gibson     /*
2693136fbf65Szhaolichang      * The architecture defines a number of rules for which bits can
269447733729SDavid Gibson      * change but in practice, we handle this in hreg_store_msr()
2695a2e71b28SBenjamin Herrenschmidt      * which will be called by do_rfi(), so there is no need to filter
2696a2e71b28SBenjamin Herrenschmidt      * here
2697a2e71b28SBenjamin Herrenschmidt      */
2698a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]);
2699ad71ed68SBlue Swirl }
2700ad71ed68SBlue Swirl 
27013c89b8d6SNicholas Piggin void helper_rfscv(CPUPPCState *env)
27023c89b8d6SNicholas Piggin {
27033c89b8d6SNicholas Piggin     do_rfi(env, env->lr, env->ctr);
27043c89b8d6SNicholas Piggin }
27053c89b8d6SNicholas Piggin 
2706e5f17ac6SBlue Swirl void helper_hrfid(CPUPPCState *env)
2707ad71ed68SBlue Swirl {
2708a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
2709ad71ed68SBlue Swirl }
2710ad71ed68SBlue Swirl #endif
2711ad71ed68SBlue Swirl 
27121f26c751SDaniel Henrique Barboza #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
27131f26c751SDaniel Henrique Barboza void helper_rfebb(CPUPPCState *env, target_ulong s)
27141f26c751SDaniel Henrique Barboza {
27151f26c751SDaniel Henrique Barboza     target_ulong msr = env->msr;
27161f26c751SDaniel Henrique Barboza 
27171f26c751SDaniel Henrique Barboza     /*
27181f26c751SDaniel Henrique Barboza      * Handling of BESCR bits 32:33 according to PowerISA v3.1:
27191f26c751SDaniel Henrique Barboza      *
27201f26c751SDaniel Henrique Barboza      * "If BESCR 32:33 != 0b00 the instruction is treated as if
27211f26c751SDaniel Henrique Barboza      *  the instruction form were invalid."
27221f26c751SDaniel Henrique Barboza      */
27231f26c751SDaniel Henrique Barboza     if (env->spr[SPR_BESCR] & BESCR_INVALID) {
27241f26c751SDaniel Henrique Barboza         raise_exception_err(env, POWERPC_EXCP_PROGRAM,
27251f26c751SDaniel Henrique Barboza                             POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
27261f26c751SDaniel Henrique Barboza     }
27271f26c751SDaniel Henrique Barboza 
27281f26c751SDaniel Henrique Barboza     env->nip = env->spr[SPR_EBBRR];
27291f26c751SDaniel Henrique Barboza 
27301f26c751SDaniel Henrique Barboza     /* Switching to 32-bit ? Crop the nip */
27311f26c751SDaniel Henrique Barboza     if (!msr_is_64bit(env, msr)) {
27321f26c751SDaniel Henrique Barboza         env->nip = (uint32_t)env->spr[SPR_EBBRR];
27331f26c751SDaniel Henrique Barboza     }
27341f26c751SDaniel Henrique Barboza 
27351f26c751SDaniel Henrique Barboza     if (s) {
27361f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_GE;
27371f26c751SDaniel Henrique Barboza     } else {
27381f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
27391f26c751SDaniel Henrique Barboza     }
27401f26c751SDaniel Henrique Barboza }
2741d3412df2SDaniel Henrique Barboza 
2742d3412df2SDaniel Henrique Barboza /*
2743d3412df2SDaniel Henrique Barboza  * Triggers or queues an 'ebb_excp' EBB exception. All checks
2744d3412df2SDaniel Henrique Barboza  * but FSCR, HFSCR and msr_pr must be done beforehand.
2745d3412df2SDaniel Henrique Barboza  *
2746d3412df2SDaniel Henrique Barboza  * PowerISA v3.1 isn't clear about whether an EBB should be
2747d3412df2SDaniel Henrique Barboza  * postponed or cancelled if the EBB facility is unavailable.
2748d3412df2SDaniel Henrique Barboza  * Our assumption here is that the EBB is cancelled if both
2749d3412df2SDaniel Henrique Barboza  * FSCR and HFSCR EBB facilities aren't available.
2750d3412df2SDaniel Henrique Barboza  */
2751d3412df2SDaniel Henrique Barboza static void do_ebb(CPUPPCState *env, int ebb_excp)
2752d3412df2SDaniel Henrique Barboza {
2753d3412df2SDaniel Henrique Barboza     PowerPCCPU *cpu = env_archcpu(env);
2754d3412df2SDaniel Henrique Barboza 
2755d3412df2SDaniel Henrique Barboza     /*
2756d3412df2SDaniel Henrique Barboza      * FSCR_EBB and FSCR_IC_EBB are the same bits used with
2757d3412df2SDaniel Henrique Barboza      * HFSCR.
2758d3412df2SDaniel Henrique Barboza      */
2759d3412df2SDaniel Henrique Barboza     helper_fscr_facility_check(env, FSCR_EBB, 0, FSCR_IC_EBB);
2760d3412df2SDaniel Henrique Barboza     helper_hfscr_facility_check(env, FSCR_EBB, "EBB", FSCR_IC_EBB);
2761d3412df2SDaniel Henrique Barboza 
2762d3412df2SDaniel Henrique Barboza     if (ebb_excp == POWERPC_EXCP_PERFM_EBB) {
2763d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_PMEO;
2764d3412df2SDaniel Henrique Barboza     } else if (ebb_excp == POWERPC_EXCP_EXTERNAL_EBB) {
2765d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_EEO;
2766d3412df2SDaniel Henrique Barboza     }
2767d3412df2SDaniel Henrique Barboza 
2768d41ccf6eSVíctor Colombo     if (FIELD_EX64(env->msr, MSR, PR)) {
2769d3412df2SDaniel Henrique Barboza         powerpc_excp(cpu, ebb_excp);
2770d3412df2SDaniel Henrique Barboza     } else {
27717b694df6SMatheus Ferst         ppc_set_irq(cpu, PPC_INTERRUPT_EBB, 1);
2772d3412df2SDaniel Henrique Barboza     }
2773d3412df2SDaniel Henrique Barboza }
2774d3412df2SDaniel Henrique Barboza 
2775d3412df2SDaniel Henrique Barboza void raise_ebb_perfm_exception(CPUPPCState *env)
2776d3412df2SDaniel Henrique Barboza {
2777d3412df2SDaniel Henrique Barboza     bool perfm_ebb_enabled = env->spr[SPR_POWER_MMCR0] & MMCR0_EBE &&
2778d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_PME &&
2779d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_GE;
2780d3412df2SDaniel Henrique Barboza 
2781d3412df2SDaniel Henrique Barboza     if (!perfm_ebb_enabled) {
2782d3412df2SDaniel Henrique Barboza         return;
2783d3412df2SDaniel Henrique Barboza     }
2784d3412df2SDaniel Henrique Barboza 
2785d3412df2SDaniel Henrique Barboza     do_ebb(env, POWERPC_EXCP_PERFM_EBB);
2786d3412df2SDaniel Henrique Barboza }
27871f26c751SDaniel Henrique Barboza #endif
27881f26c751SDaniel Henrique Barboza 
2789ad71ed68SBlue Swirl /*****************************************************************************/
2790ad71ed68SBlue Swirl /* Embedded PowerPC specific helpers */
2791e5f17ac6SBlue Swirl void helper_40x_rfci(CPUPPCState *env)
2792ad71ed68SBlue Swirl {
2793a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]);
2794ad71ed68SBlue Swirl }
2795ad71ed68SBlue Swirl 
2796e5f17ac6SBlue Swirl void helper_rfci(CPUPPCState *env)
2797ad71ed68SBlue Swirl {
2798a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]);
2799ad71ed68SBlue Swirl }
2800ad71ed68SBlue Swirl 
2801e5f17ac6SBlue Swirl void helper_rfdi(CPUPPCState *env)
2802ad71ed68SBlue Swirl {
2803a1bb7384SScott Wood     /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
2804a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]);
2805ad71ed68SBlue Swirl }
2806ad71ed68SBlue Swirl 
2807e5f17ac6SBlue Swirl void helper_rfmci(CPUPPCState *env)
2808ad71ed68SBlue Swirl {
2809a1bb7384SScott Wood     /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
2810a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
2811ad71ed68SBlue Swirl }
28122b44e219SBruno Larsen (billionai) #endif /* CONFIG_TCG */
28132b44e219SBruno Larsen (billionai) #endif /* !defined(CONFIG_USER_ONLY) */
2814ad71ed68SBlue Swirl 
28152b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2816e5f17ac6SBlue Swirl void helper_tw(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2817e5f17ac6SBlue Swirl                uint32_t flags)
2818ad71ed68SBlue Swirl {
2819ad71ed68SBlue Swirl     if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
2820ad71ed68SBlue Swirl                   ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
2821ad71ed68SBlue Swirl                   ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
2822ad71ed68SBlue Swirl                   ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
2823ad71ed68SBlue Swirl                   ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
282472073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
282572073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2826ad71ed68SBlue Swirl     }
2827ad71ed68SBlue Swirl }
2828ad71ed68SBlue Swirl 
2829ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2830e5f17ac6SBlue Swirl void helper_td(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2831e5f17ac6SBlue Swirl                uint32_t flags)
2832ad71ed68SBlue Swirl {
2833ad71ed68SBlue Swirl     if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
2834ad71ed68SBlue Swirl                   ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
2835ad71ed68SBlue Swirl                   ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
2836ad71ed68SBlue Swirl                   ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
2837ad71ed68SBlue Swirl                   ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) {
283872073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
283972073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2840ad71ed68SBlue Swirl     }
2841ad71ed68SBlue Swirl }
2842ad71ed68SBlue Swirl #endif
28432b44e219SBruno Larsen (billionai) #endif
2844ad71ed68SBlue Swirl 
2845*049b4ad6SVaibhav Jain #ifdef CONFIG_TCG
2846670f1da3SVíctor Colombo static uint32_t helper_SIMON_LIKE_32_64(uint32_t x, uint64_t key, uint32_t lane)
2847670f1da3SVíctor Colombo {
2848670f1da3SVíctor Colombo     const uint16_t c = 0xfffc;
2849670f1da3SVíctor Colombo     const uint64_t z0 = 0xfa2561cdf44ac398ULL;
2850670f1da3SVíctor Colombo     uint16_t z = 0, temp;
2851670f1da3SVíctor Colombo     uint16_t k[32], eff_k[32], xleft[33], xright[33], fxleft[32];
2852670f1da3SVíctor Colombo 
2853670f1da3SVíctor Colombo     for (int i = 3; i >= 0; i--) {
2854670f1da3SVíctor Colombo         k[i] = key & 0xffff;
2855670f1da3SVíctor Colombo         key >>= 16;
2856670f1da3SVíctor Colombo     }
2857670f1da3SVíctor Colombo     xleft[0] = x & 0xffff;
2858670f1da3SVíctor Colombo     xright[0] = (x >> 16) & 0xffff;
2859670f1da3SVíctor Colombo 
2860670f1da3SVíctor Colombo     for (int i = 0; i < 28; i++) {
2861670f1da3SVíctor Colombo         z = (z0 >> (63 - i)) & 1;
2862670f1da3SVíctor Colombo         temp = ror16(k[i + 3], 3) ^ k[i + 1];
2863670f1da3SVíctor Colombo         k[i + 4] = c ^ z ^ k[i] ^ temp ^ ror16(temp, 1);
2864670f1da3SVíctor Colombo     }
2865670f1da3SVíctor Colombo 
2866670f1da3SVíctor Colombo     for (int i = 0; i < 8; i++) {
2867670f1da3SVíctor Colombo         eff_k[4 * i + 0] = k[4 * i + ((0 + lane) % 4)];
2868670f1da3SVíctor Colombo         eff_k[4 * i + 1] = k[4 * i + ((1 + lane) % 4)];
2869670f1da3SVíctor Colombo         eff_k[4 * i + 2] = k[4 * i + ((2 + lane) % 4)];
2870670f1da3SVíctor Colombo         eff_k[4 * i + 3] = k[4 * i + ((3 + lane) % 4)];
2871670f1da3SVíctor Colombo     }
2872670f1da3SVíctor Colombo 
2873670f1da3SVíctor Colombo     for (int i = 0; i < 32; i++) {
2874670f1da3SVíctor Colombo         fxleft[i] = (rol16(xleft[i], 1) &
2875670f1da3SVíctor Colombo             rol16(xleft[i], 8)) ^ rol16(xleft[i], 2);
2876670f1da3SVíctor Colombo         xleft[i + 1] = xright[i] ^ fxleft[i] ^ eff_k[i];
2877670f1da3SVíctor Colombo         xright[i + 1] = xleft[i];
2878670f1da3SVíctor Colombo     }
2879670f1da3SVíctor Colombo 
2880670f1da3SVíctor Colombo     return (((uint32_t)xright[32]) << 16) | xleft[32];
2881670f1da3SVíctor Colombo }
2882670f1da3SVíctor Colombo 
2883670f1da3SVíctor Colombo static uint64_t hash_digest(uint64_t ra, uint64_t rb, uint64_t key)
2884670f1da3SVíctor Colombo {
2885670f1da3SVíctor Colombo     uint64_t stage0_h = 0ULL, stage0_l = 0ULL;
2886670f1da3SVíctor Colombo     uint64_t stage1_h, stage1_l;
2887670f1da3SVíctor Colombo 
2888670f1da3SVíctor Colombo     for (int i = 0; i < 4; i++) {
2889670f1da3SVíctor Colombo         stage0_h |= ror64(rb & 0xff, 8 * (2 * i + 1));
2890670f1da3SVíctor Colombo         stage0_h |= ((ra >> 32) & 0xff) << (8 * 2 * i);
2891670f1da3SVíctor Colombo         stage0_l |= ror64((rb >> 32) & 0xff, 8 * (2 * i + 1));
2892670f1da3SVíctor Colombo         stage0_l |= (ra & 0xff) << (8 * 2 * i);
2893670f1da3SVíctor Colombo         rb >>= 8;
2894670f1da3SVíctor Colombo         ra >>= 8;
2895670f1da3SVíctor Colombo     }
2896670f1da3SVíctor Colombo 
2897670f1da3SVíctor Colombo     stage1_h = (uint64_t)helper_SIMON_LIKE_32_64(stage0_h >> 32, key, 0) << 32;
2898670f1da3SVíctor Colombo     stage1_h |= helper_SIMON_LIKE_32_64(stage0_h, key, 1);
2899670f1da3SVíctor Colombo     stage1_l = (uint64_t)helper_SIMON_LIKE_32_64(stage0_l >> 32, key, 2) << 32;
2900670f1da3SVíctor Colombo     stage1_l |= helper_SIMON_LIKE_32_64(stage0_l, key, 3);
2901670f1da3SVíctor Colombo 
2902670f1da3SVíctor Colombo     return stage1_h ^ stage1_l;
2903670f1da3SVíctor Colombo }
2904670f1da3SVíctor Colombo 
2905670f1da3SVíctor Colombo #include "qemu/guest-random.h"
2906670f1da3SVíctor Colombo 
2907670f1da3SVíctor Colombo #define HELPER_HASH(op, key, store)                                           \
2908670f1da3SVíctor Colombo void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
2909670f1da3SVíctor Colombo                  target_ulong rb)                                             \
2910670f1da3SVíctor Colombo {                                                                             \
2911670f1da3SVíctor Colombo     uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash;         \
2912670f1da3SVíctor Colombo                                                                               \
2913670f1da3SVíctor Colombo     if (store) {                                                              \
2914670f1da3SVíctor Colombo         cpu_stq_data_ra(env, ea, calculated_hash, GETPC());                   \
2915670f1da3SVíctor Colombo     } else {                                                                  \
2916670f1da3SVíctor Colombo         loaded_hash = cpu_ldq_data_ra(env, ea, GETPC());                      \
2917670f1da3SVíctor Colombo         if (loaded_hash != calculated_hash) {                                 \
2918670f1da3SVíctor Colombo             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,                 \
2919670f1da3SVíctor Colombo                 POWERPC_EXCP_TRAP, GETPC());                                  \
2920670f1da3SVíctor Colombo         }                                                                     \
2921670f1da3SVíctor Colombo     }                                                                         \
2922670f1da3SVíctor Colombo }
2923670f1da3SVíctor Colombo 
2924670f1da3SVíctor Colombo HELPER_HASH(HASHST, env->spr[SPR_HASHKEYR], true)
2925670f1da3SVíctor Colombo HELPER_HASH(HASHCHK, env->spr[SPR_HASHKEYR], false)
292653ae2aebSVíctor Colombo HELPER_HASH(HASHSTP, env->spr[SPR_HASHPKEYR], true)
292753ae2aebSVíctor Colombo HELPER_HASH(HASHCHKP, env->spr[SPR_HASHPKEYR], false)
2928*049b4ad6SVaibhav Jain #endif /* CONFIG_TCG */
2929670f1da3SVíctor Colombo 
2930ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY)
2931ad71ed68SBlue Swirl 
29322b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2933ad71ed68SBlue Swirl 
2934ad71ed68SBlue Swirl /* Embedded.Processor Control */
2935ad71ed68SBlue Swirl static int dbell2irq(target_ulong rb)
2936ad71ed68SBlue Swirl {
2937ad71ed68SBlue Swirl     int msg = rb & DBELL_TYPE_MASK;
2938ad71ed68SBlue Swirl     int irq = -1;
2939ad71ed68SBlue Swirl 
2940ad71ed68SBlue Swirl     switch (msg) {
2941ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL:
2942ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_DOORBELL;
2943ad71ed68SBlue Swirl         break;
2944ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL_CRIT:
2945ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_CDOORBELL;
2946ad71ed68SBlue Swirl         break;
2947ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL:
2948ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_CRIT:
2949ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_MC:
2950ad71ed68SBlue Swirl         /* XXX implement */
2951ad71ed68SBlue Swirl     default:
2952ad71ed68SBlue Swirl         break;
2953ad71ed68SBlue Swirl     }
2954ad71ed68SBlue Swirl 
2955ad71ed68SBlue Swirl     return irq;
2956ad71ed68SBlue Swirl }
2957ad71ed68SBlue Swirl 
2958e5f17ac6SBlue Swirl void helper_msgclr(CPUPPCState *env, target_ulong rb)
2959ad71ed68SBlue Swirl {
2960ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2961ad71ed68SBlue Swirl 
2962ad71ed68SBlue Swirl     if (irq < 0) {
2963ad71ed68SBlue Swirl         return;
2964ad71ed68SBlue Swirl     }
2965ad71ed68SBlue Swirl 
29667b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), irq, 0);
2967ad71ed68SBlue Swirl }
2968ad71ed68SBlue Swirl 
2969ad71ed68SBlue Swirl void helper_msgsnd(target_ulong rb)
2970ad71ed68SBlue Swirl {
2971ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2972ad71ed68SBlue Swirl     int pir = rb & DBELL_PIRTAG_MASK;
2973182735efSAndreas Färber     CPUState *cs;
2974ad71ed68SBlue Swirl 
2975ad71ed68SBlue Swirl     if (irq < 0) {
2976ad71ed68SBlue Swirl         return;
2977ad71ed68SBlue Swirl     }
2978ad71ed68SBlue Swirl 
2979f1c29ebcSThomas Huth     qemu_mutex_lock_iothread();
2980bdc44640SAndreas Färber     CPU_FOREACH(cs) {
2981182735efSAndreas Färber         PowerPCCPU *cpu = POWERPC_CPU(cs);
2982182735efSAndreas Färber         CPUPPCState *cenv = &cpu->env;
2983182735efSAndreas Färber 
2984ad71ed68SBlue Swirl         if ((rb & DBELL_BRDCAST) || (cenv->spr[SPR_BOOKE_PIR] == pir)) {
29857b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
2986ad71ed68SBlue Swirl         }
2987ad71ed68SBlue Swirl     }
2988f1c29ebcSThomas Huth     qemu_mutex_unlock_iothread();
2989ad71ed68SBlue Swirl }
29907af1e7b0SCédric Le Goater 
29917af1e7b0SCédric Le Goater /* Server Processor Control */
29927af1e7b0SCédric Le Goater 
29935ba7ba1dSCédric Le Goater static bool dbell_type_server(target_ulong rb)
29945ba7ba1dSCédric Le Goater {
299547733729SDavid Gibson     /*
299647733729SDavid Gibson      * A Directed Hypervisor Doorbell message is sent only if the
29977af1e7b0SCédric Le Goater      * message type is 5. All other types are reserved and the
299847733729SDavid Gibson      * instruction is a no-op
299947733729SDavid Gibson      */
30005ba7ba1dSCédric Le Goater     return (rb & DBELL_TYPE_MASK) == DBELL_TYPE_DBELL_SERVER;
30017af1e7b0SCédric Le Goater }
30027af1e7b0SCédric Le Goater 
30037af1e7b0SCédric Le Goater void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb)
30047af1e7b0SCédric Le Goater {
30055ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
30067af1e7b0SCédric Le Goater         return;
30077af1e7b0SCédric Le Goater     }
30087af1e7b0SCédric Le Goater 
30097b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
30107af1e7b0SCédric Le Goater }
30117af1e7b0SCédric Le Goater 
30125ba7ba1dSCédric Le Goater static void book3s_msgsnd_common(int pir, int irq)
30137af1e7b0SCédric Le Goater {
30147af1e7b0SCédric Le Goater     CPUState *cs;
30157af1e7b0SCédric Le Goater 
30167af1e7b0SCédric Le Goater     qemu_mutex_lock_iothread();
30177af1e7b0SCédric Le Goater     CPU_FOREACH(cs) {
30187af1e7b0SCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
30197af1e7b0SCédric Le Goater         CPUPPCState *cenv = &cpu->env;
30207af1e7b0SCédric Le Goater 
30217af1e7b0SCédric Le Goater         /* TODO: broadcast message to all threads of the same  processor */
30227af1e7b0SCédric Le Goater         if (cenv->spr_cb[SPR_PIR].default_value == pir) {
30237b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
30247af1e7b0SCédric Le Goater         }
30257af1e7b0SCédric Le Goater     }
30267af1e7b0SCédric Le Goater     qemu_mutex_unlock_iothread();
30277af1e7b0SCédric Le Goater }
30285ba7ba1dSCédric Le Goater 
30295ba7ba1dSCédric Le Goater void helper_book3s_msgsnd(target_ulong rb)
30305ba7ba1dSCédric Le Goater {
30315ba7ba1dSCédric Le Goater     int pir = rb & DBELL_PROCIDTAG_MASK;
30325ba7ba1dSCédric Le Goater 
30335ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
30345ba7ba1dSCédric Le Goater         return;
30355ba7ba1dSCédric Le Goater     }
30365ba7ba1dSCédric Le Goater 
30375ba7ba1dSCédric Le Goater     book3s_msgsnd_common(pir, PPC_INTERRUPT_HDOORBELL);
30385ba7ba1dSCédric Le Goater }
30395ba7ba1dSCédric Le Goater 
30405ba7ba1dSCédric Le Goater #if defined(TARGET_PPC64)
30415ba7ba1dSCédric Le Goater void helper_book3s_msgclrp(CPUPPCState *env, target_ulong rb)
30425ba7ba1dSCédric Le Goater {
3043493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgclrp", HFSCR_IC_MSGP);
3044493028d8SCédric Le Goater 
30455ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
30465ba7ba1dSCédric Le Goater         return;
30475ba7ba1dSCédric Le Goater     }
30485ba7ba1dSCédric Le Goater 
30497b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
30505ba7ba1dSCédric Le Goater }
30515ba7ba1dSCédric Le Goater 
30525ba7ba1dSCédric Le Goater /*
30535ba7ba1dSCédric Le Goater  * sends a message to other threads that are on the same
30545ba7ba1dSCédric Le Goater  * multi-threaded processor
30555ba7ba1dSCédric Le Goater  */
30565ba7ba1dSCédric Le Goater void helper_book3s_msgsndp(CPUPPCState *env, target_ulong rb)
30575ba7ba1dSCédric Le Goater {
30585ba7ba1dSCédric Le Goater     int pir = env->spr_cb[SPR_PIR].default_value;
30595ba7ba1dSCédric Le Goater 
3060493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgsndp", HFSCR_IC_MSGP);
3061493028d8SCédric Le Goater 
30625ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
30635ba7ba1dSCédric Le Goater         return;
30645ba7ba1dSCédric Le Goater     }
30655ba7ba1dSCédric Le Goater 
30665ba7ba1dSCédric Le Goater     /* TODO: TCG supports only one thread */
30675ba7ba1dSCédric Le Goater 
30685ba7ba1dSCédric Le Goater     book3s_msgsnd_common(pir, PPC_INTERRUPT_DOORBELL);
30695ba7ba1dSCédric Le Goater }
3070996473e4SRichard Henderson #endif /* TARGET_PPC64 */
30710f3110faSRichard Henderson 
30720f3110faSRichard Henderson void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
30730f3110faSRichard Henderson                                  MMUAccessType access_type,
30740f3110faSRichard Henderson                                  int mmu_idx, uintptr_t retaddr)
30750f3110faSRichard Henderson {
30760f3110faSRichard Henderson     CPUPPCState *env = cs->env_ptr;
307729c4a336SFabiano Rosas     uint32_t insn;
307829c4a336SFabiano Rosas 
307929c4a336SFabiano Rosas     /* Restore state and reload the insn we executed, for filling in DSISR.  */
30803d419a4dSRichard Henderson     cpu_restore_state(cs, retaddr);
308129c4a336SFabiano Rosas     insn = cpu_ldl_code(env, env->nip);
30820f3110faSRichard Henderson 
3083a7e3af13SRichard Henderson     switch (env->mmu_model) {
3084a7e3af13SRichard Henderson     case POWERPC_MMU_SOFT_4xx:
3085a7e3af13SRichard Henderson         env->spr[SPR_40x_DEAR] = vaddr;
3086a7e3af13SRichard Henderson         break;
3087a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE:
3088a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE206:
3089a7e3af13SRichard Henderson         env->spr[SPR_BOOKE_DEAR] = vaddr;
3090a7e3af13SRichard Henderson         break;
3091a7e3af13SRichard Henderson     default:
3092a7e3af13SRichard Henderson         env->spr[SPR_DAR] = vaddr;
3093a7e3af13SRichard Henderson         break;
3094a7e3af13SRichard Henderson     }
3095a7e3af13SRichard Henderson 
30960f3110faSRichard Henderson     cs->exception_index = POWERPC_EXCP_ALIGN;
309729c4a336SFabiano Rosas     env->error_code = insn & 0x03FF0000;
309829c4a336SFabiano Rosas     cpu_loop_exit(cs);
30990f3110faSRichard Henderson }
3100996473e4SRichard Henderson #endif /* CONFIG_TCG */
3101996473e4SRichard Henderson #endif /* !CONFIG_USER_ONLY */
3102