xref: /qemu/target/ppc/excp_helper.c (revision bf303fb3f11c3e131f315e44b792ad79a8ae07bb)
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);
393ad77c6caSNicholas Piggin 
3943680e994SNicholas Piggin     powerpc_reset_excp_state(cpu);
395ad77c6caSNicholas Piggin 
396ad77c6caSNicholas Piggin     /*
397ad77c6caSNicholas Piggin      * Any interrupt is context synchronizing, check if TCG TLB needs
398ad77c6caSNicholas Piggin      * a delayed flush on ppc64
399ad77c6caSNicholas Piggin      */
400ad77c6caSNicholas Piggin     check_tlb_flush(env, false);
4013680e994SNicholas Piggin 
4023680e994SNicholas Piggin     /* Reset the reservation */
4033680e994SNicholas Piggin     env->reserve_addr = -1;
404ad77c6caSNicholas Piggin }
405ad77c6caSNicholas Piggin 
406e808c2edSFabiano Rosas static void powerpc_excp_40x(PowerPCCPU *cpu, int excp)
407e808c2edSFabiano Rosas {
408e808c2edSFabiano Rosas     CPUState *cs = CPU(cpu);
409e808c2edSFabiano Rosas     CPUPPCState *env = &cpu->env;
410e808c2edSFabiano Rosas     target_ulong msr, new_msr, vector;
4118428cdb2SFabiano Rosas     int srr0, srr1;
412e808c2edSFabiano Rosas 
413e808c2edSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
414e808c2edSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
415e808c2edSFabiano Rosas 
416e808c2edSFabiano Rosas     /*
417495fc7ffSFabiano Rosas      * new interrupt handler msr preserves existing ME unless
418495fc7ffSFabiano Rosas      * explicitly overriden.
419e808c2edSFabiano Rosas      */
420495fc7ffSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME));
421e808c2edSFabiano Rosas 
422e808c2edSFabiano Rosas     /* target registers */
423e808c2edSFabiano Rosas     srr0 = SPR_SRR0;
424e808c2edSFabiano Rosas     srr1 = SPR_SRR1;
425e808c2edSFabiano Rosas 
426e808c2edSFabiano Rosas     /*
427e808c2edSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
428495fc7ffSFabiano Rosas      * arch 2.05 server or later.
429e808c2edSFabiano Rosas      */
430495fc7ffSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
431e808c2edSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
432e808c2edSFabiano Rosas     }
433e808c2edSFabiano Rosas 
434e808c2edSFabiano Rosas     vector = env->excp_vectors[excp];
435e808c2edSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
436e808c2edSFabiano Rosas         cpu_abort(cs, "Raised an exception without defined vector %d\n",
437e808c2edSFabiano Rosas                   excp);
438e808c2edSFabiano Rosas     }
439e808c2edSFabiano Rosas 
440e808c2edSFabiano Rosas     vector |= env->excp_prefix;
441e808c2edSFabiano Rosas 
442e808c2edSFabiano Rosas     switch (excp) {
443e808c2edSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
444e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
445e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
446e808c2edSFabiano Rosas         break;
447e808c2edSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
448c354d858SVíctor Colombo         if (!FIELD_EX64(env->msr, MSR, ME)) {
449e808c2edSFabiano Rosas             /*
450e808c2edSFabiano Rosas              * Machine check exception is not enabled.  Enter
451e808c2edSFabiano Rosas              * checkstop state.
452e808c2edSFabiano Rosas              */
453e808c2edSFabiano Rosas             fprintf(stderr, "Machine check while not allowed. "
454e808c2edSFabiano Rosas                     "Entering checkstop state\n");
455e808c2edSFabiano Rosas             if (qemu_log_separate()) {
456e808c2edSFabiano Rosas                 qemu_log("Machine check while not allowed. "
457e808c2edSFabiano Rosas                         "Entering checkstop state\n");
458e808c2edSFabiano Rosas             }
459e808c2edSFabiano Rosas             cs->halted = 1;
460e808c2edSFabiano Rosas             cpu_interrupt_exittb(cs);
461e808c2edSFabiano Rosas         }
462e808c2edSFabiano Rosas 
463e808c2edSFabiano Rosas         /* machine check exceptions don't have ME set */
464e808c2edSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
465e808c2edSFabiano Rosas 
466e808c2edSFabiano Rosas         srr0 = SPR_40x_SRR2;
467e808c2edSFabiano Rosas         srr1 = SPR_40x_SRR3;
468e808c2edSFabiano Rosas         break;
469e808c2edSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
470f9911e1eSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_40x_ESR], env->spr[SPR_40x_DEAR]);
471e808c2edSFabiano Rosas         break;
472e808c2edSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
473e808c2edSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
474e808c2edSFabiano Rosas         break;
475e808c2edSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
476e808c2edSFabiano Rosas         break;
477e808c2edSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
478e808c2edSFabiano Rosas         break;
479e808c2edSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
480e808c2edSFabiano Rosas         switch (env->error_code & ~0xF) {
481e808c2edSFabiano Rosas         case POWERPC_EXCP_FP:
482da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
483e808c2edSFabiano Rosas                 trace_ppc_excp_fp_ignore();
4843680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
485e808c2edSFabiano Rosas                 return;
486e808c2edSFabiano Rosas             }
48764e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_FP;
488e808c2edSFabiano Rosas             break;
489e808c2edSFabiano Rosas         case POWERPC_EXCP_INVAL:
490e808c2edSFabiano Rosas             trace_ppc_excp_inval(env->nip);
49164e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PIL;
492e808c2edSFabiano Rosas             break;
493e808c2edSFabiano Rosas         case POWERPC_EXCP_PRIV:
49464e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PPR;
495e808c2edSFabiano Rosas             break;
496e808c2edSFabiano Rosas         case POWERPC_EXCP_TRAP:
49764e62cfbSFabiano Rosas             env->spr[SPR_40x_ESR] = ESR_PTR;
498e808c2edSFabiano Rosas             break;
499e808c2edSFabiano Rosas         default:
500e808c2edSFabiano Rosas             cpu_abort(cs, "Invalid program exception %d. Aborting\n",
501e808c2edSFabiano Rosas                       env->error_code);
502e808c2edSFabiano Rosas             break;
503e808c2edSFabiano Rosas         }
504e808c2edSFabiano Rosas         break;
505e808c2edSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
506e808c2edSFabiano Rosas         dump_syscall(env);
507e808c2edSFabiano Rosas 
508e808c2edSFabiano Rosas         /*
509e808c2edSFabiano Rosas          * We need to correct the NIP which in this case is supposed
510e808c2edSFabiano Rosas          * to point to the next instruction
511e808c2edSFabiano Rosas          */
512e808c2edSFabiano Rosas         env->nip += 4;
513e808c2edSFabiano Rosas         break;
514e808c2edSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
515e808c2edSFabiano Rosas         trace_ppc_excp_print("FIT");
516e808c2edSFabiano Rosas         break;
517e808c2edSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
518e808c2edSFabiano Rosas         trace_ppc_excp_print("WDT");
519e808c2edSFabiano Rosas         break;
520e808c2edSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
521e808c2edSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
522e808c2edSFabiano Rosas         break;
523e808c2edSFabiano Rosas     case POWERPC_EXCP_PIT:       /* Programmable interval timer interrupt    */
524e808c2edSFabiano Rosas         trace_ppc_excp_print("PIT");
525e808c2edSFabiano Rosas         break;
5264d8ac1d1SFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
5274d8ac1d1SFabiano Rosas         cpu_abort(cs, "%s exception not implemented\n",
5284d8ac1d1SFabiano Rosas                   powerpc_excp_name(excp));
5294d8ac1d1SFabiano Rosas         break;
530e808c2edSFabiano Rosas     default:
531e808c2edSFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
532e808c2edSFabiano Rosas         break;
533e808c2edSFabiano Rosas     }
534e808c2edSFabiano Rosas 
535e808c2edSFabiano Rosas     /* Save PC */
536e808c2edSFabiano Rosas     env->spr[srr0] = env->nip;
537e808c2edSFabiano Rosas 
538e808c2edSFabiano Rosas     /* Save MSR */
539e808c2edSFabiano Rosas     env->spr[srr1] = msr;
540e808c2edSFabiano Rosas 
541e808c2edSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
542e808c2edSFabiano Rosas }
543e808c2edSFabiano Rosas 
54458d178fbSFabiano Rosas static void powerpc_excp_6xx(PowerPCCPU *cpu, int excp)
54558d178fbSFabiano Rosas {
54658d178fbSFabiano Rosas     CPUState *cs = CPU(cpu);
54758d178fbSFabiano Rosas     CPUPPCState *env = &cpu->env;
54858d178fbSFabiano Rosas     target_ulong msr, new_msr, vector;
54958d178fbSFabiano Rosas 
55058d178fbSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
55158d178fbSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
55258d178fbSFabiano Rosas 
55358d178fbSFabiano Rosas     /*
554082d783bSFabiano Rosas      * new interrupt handler msr preserves existing ME unless
55558d178fbSFabiano Rosas      * explicitly overriden
55658d178fbSFabiano Rosas      */
557082d783bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
55858d178fbSFabiano Rosas 
55958d178fbSFabiano Rosas     /*
56058d178fbSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
561082d783bSFabiano Rosas      * arch 2.05 server or later.
56258d178fbSFabiano Rosas      */
563082d783bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
56458d178fbSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
56558d178fbSFabiano Rosas     }
56658d178fbSFabiano Rosas 
56758d178fbSFabiano Rosas     vector = env->excp_vectors[excp];
56858d178fbSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
56958d178fbSFabiano Rosas         cpu_abort(cs, "Raised an exception without defined vector %d\n",
57058d178fbSFabiano Rosas                   excp);
57158d178fbSFabiano Rosas     }
57258d178fbSFabiano Rosas 
57358d178fbSFabiano Rosas     vector |= env->excp_prefix;
57458d178fbSFabiano Rosas 
57558d178fbSFabiano Rosas     switch (excp) {
57658d178fbSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
57758d178fbSFabiano Rosas         break;
57858d178fbSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
579c354d858SVíctor Colombo         if (!FIELD_EX64(env->msr, MSR, ME)) {
58058d178fbSFabiano Rosas             /*
58158d178fbSFabiano Rosas              * Machine check exception is not enabled.  Enter
58258d178fbSFabiano Rosas              * checkstop state.
58358d178fbSFabiano Rosas              */
58458d178fbSFabiano Rosas             fprintf(stderr, "Machine check while not allowed. "
58558d178fbSFabiano Rosas                     "Entering checkstop state\n");
58658d178fbSFabiano Rosas             if (qemu_log_separate()) {
58758d178fbSFabiano Rosas                 qemu_log("Machine check while not allowed. "
58858d178fbSFabiano Rosas                         "Entering checkstop state\n");
58958d178fbSFabiano Rosas             }
59058d178fbSFabiano Rosas             cs->halted = 1;
59158d178fbSFabiano Rosas             cpu_interrupt_exittb(cs);
59258d178fbSFabiano Rosas         }
59358d178fbSFabiano Rosas 
59458d178fbSFabiano Rosas         /* machine check exceptions don't have ME set */
59558d178fbSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
59658d178fbSFabiano Rosas 
59758d178fbSFabiano Rosas         break;
59858d178fbSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
59958d178fbSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
60058d178fbSFabiano Rosas         break;
60158d178fbSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
60258d178fbSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
60358d178fbSFabiano Rosas         msr |= env->error_code;
60458d178fbSFabiano Rosas         break;
60558d178fbSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
60658d178fbSFabiano Rosas         break;
60758d178fbSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
60858d178fbSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
60958d178fbSFabiano Rosas         /*
61058d178fbSFabiano Rosas          * Note: the opcode fields will not be set properly for a
61158d178fbSFabiano Rosas          * direct store load/store, but nobody cares as nobody
61258d178fbSFabiano Rosas          * actually uses direct store segments.
61358d178fbSFabiano Rosas          */
61458d178fbSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
61558d178fbSFabiano Rosas         break;
61658d178fbSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
61758d178fbSFabiano Rosas         switch (env->error_code & ~0xF) {
61858d178fbSFabiano Rosas         case POWERPC_EXCP_FP:
619da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
62058d178fbSFabiano Rosas                 trace_ppc_excp_fp_ignore();
6213680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
62258d178fbSFabiano Rosas                 return;
62358d178fbSFabiano Rosas             }
62458d178fbSFabiano Rosas 
62558d178fbSFabiano Rosas             /*
62658d178fbSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
62758d178fbSFabiano Rosas              * instruction, so always use store_next and claim we are
62858d178fbSFabiano Rosas              * precise in the MSR.
62958d178fbSFabiano Rosas              */
63058d178fbSFabiano Rosas             msr |= 0x00100000;
63158d178fbSFabiano Rosas             break;
63258d178fbSFabiano Rosas         case POWERPC_EXCP_INVAL:
63358d178fbSFabiano Rosas             trace_ppc_excp_inval(env->nip);
63458d178fbSFabiano Rosas             msr |= 0x00080000;
63558d178fbSFabiano Rosas             break;
63658d178fbSFabiano Rosas         case POWERPC_EXCP_PRIV:
63758d178fbSFabiano Rosas             msr |= 0x00040000;
63858d178fbSFabiano Rosas             break;
63958d178fbSFabiano Rosas         case POWERPC_EXCP_TRAP:
64058d178fbSFabiano Rosas             msr |= 0x00020000;
64158d178fbSFabiano Rosas             break;
64258d178fbSFabiano Rosas         default:
64358d178fbSFabiano Rosas             /* Should never occur */
64458d178fbSFabiano Rosas             cpu_abort(cs, "Invalid program exception %d. Aborting\n",
64558d178fbSFabiano Rosas                       env->error_code);
64658d178fbSFabiano Rosas             break;
64758d178fbSFabiano Rosas         }
64858d178fbSFabiano Rosas         break;
64958d178fbSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
65058d178fbSFabiano Rosas         dump_syscall(env);
65158d178fbSFabiano Rosas 
65258d178fbSFabiano Rosas         /*
65358d178fbSFabiano Rosas          * We need to correct the NIP which in this case is supposed
65458d178fbSFabiano Rosas          * to point to the next instruction
65558d178fbSFabiano Rosas          */
65658d178fbSFabiano Rosas         env->nip += 4;
65758d178fbSFabiano Rosas         break;
65858d178fbSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
65958d178fbSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
66058d178fbSFabiano Rosas         break;
66158d178fbSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
66258d178fbSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
66358d178fbSFabiano Rosas         break;
66458d178fbSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
6658e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
66658d178fbSFabiano Rosas             cpu_abort(cs, "Trying to deliver power-saving system reset "
66758d178fbSFabiano Rosas                       "exception %d with no HV support\n", excp);
66858d178fbSFabiano Rosas         }
66958d178fbSFabiano Rosas         break;
67058d178fbSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
67158d178fbSFabiano Rosas         break;
67258d178fbSFabiano Rosas     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
67358d178fbSFabiano Rosas     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
67458d178fbSFabiano Rosas     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
67558d178fbSFabiano Rosas         /* Swap temporary saved registers with GPRs */
67658d178fbSFabiano Rosas         if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
67758d178fbSFabiano Rosas             new_msr |= (target_ulong)1 << MSR_TGPR;
67858d178fbSFabiano Rosas             hreg_swap_gpr_tgpr(env);
67958d178fbSFabiano Rosas         }
6808f8c7932SFabiano Rosas 
68158d178fbSFabiano Rosas         ppc_excp_debug_sw_tlb(env, excp);
68258d178fbSFabiano Rosas 
68358d178fbSFabiano Rosas         msr |= env->crf[0] << 28;
68458d178fbSFabiano Rosas         msr |= env->error_code; /* key, D/I, S/L bits */
68558d178fbSFabiano Rosas         /* Set way using a LRU mechanism */
68658d178fbSFabiano Rosas         msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
68758d178fbSFabiano Rosas         break;
68858d178fbSFabiano Rosas     case POWERPC_EXCP_FPA:       /* Floating-point assist exception          */
68958d178fbSFabiano Rosas     case POWERPC_EXCP_DABR:      /* Data address breakpoint                  */
69058d178fbSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
69158d178fbSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
69258d178fbSFabiano Rosas     case POWERPC_EXCP_MEXTBR:    /* Maskable external breakpoint             */
69358d178fbSFabiano Rosas     case POWERPC_EXCP_NMEXTBR:   /* Non maskable external breakpoint         */
69458d178fbSFabiano Rosas         cpu_abort(cs, "%s exception not implemented\n",
69558d178fbSFabiano Rosas                   powerpc_excp_name(excp));
69658d178fbSFabiano Rosas         break;
69758d178fbSFabiano Rosas     default:
69858d178fbSFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
69958d178fbSFabiano Rosas         break;
70058d178fbSFabiano Rosas     }
70158d178fbSFabiano Rosas 
70258d178fbSFabiano Rosas     /*
70358d178fbSFabiano Rosas      * Sort out endianness of interrupt, this differs depending on the
70458d178fbSFabiano Rosas      * CPU, the HV mode, etc...
70558d178fbSFabiano Rosas      */
70658d178fbSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
70758d178fbSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
70858d178fbSFabiano Rosas     }
70958d178fbSFabiano Rosas 
71058d178fbSFabiano Rosas     /* Save PC */
711c50eaed1SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
71258d178fbSFabiano Rosas 
71358d178fbSFabiano Rosas     /* Save MSR */
714c50eaed1SFabiano Rosas     env->spr[SPR_SRR1] = msr;
71558d178fbSFabiano Rosas 
71658d178fbSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
71758d178fbSFabiano Rosas }
71858d178fbSFabiano Rosas 
719ccfca2fcSFabiano Rosas static void powerpc_excp_7xx(PowerPCCPU *cpu, int excp)
720ccfca2fcSFabiano Rosas {
721ccfca2fcSFabiano Rosas     CPUState *cs = CPU(cpu);
722ccfca2fcSFabiano Rosas     CPUPPCState *env = &cpu->env;
723ccfca2fcSFabiano Rosas     target_ulong msr, new_msr, vector;
724ccfca2fcSFabiano Rosas 
725ccfca2fcSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
726ccfca2fcSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
727ccfca2fcSFabiano Rosas 
728ccfca2fcSFabiano Rosas     /*
72993848d6aSFabiano Rosas      * new interrupt handler msr preserves existing ME unless
730ccfca2fcSFabiano Rosas      * explicitly overriden
731ccfca2fcSFabiano Rosas      */
73293848d6aSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
733ccfca2fcSFabiano Rosas 
734ccfca2fcSFabiano Rosas     /*
735ccfca2fcSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
73693848d6aSFabiano Rosas      * arch 2.05 server or later.
737ccfca2fcSFabiano Rosas      */
73893848d6aSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
739ccfca2fcSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
740ccfca2fcSFabiano Rosas     }
741ccfca2fcSFabiano Rosas 
742ccfca2fcSFabiano Rosas     vector = env->excp_vectors[excp];
743ccfca2fcSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
744ccfca2fcSFabiano Rosas         cpu_abort(cs, "Raised an exception without defined vector %d\n",
745ccfca2fcSFabiano Rosas                   excp);
746ccfca2fcSFabiano Rosas     }
747ccfca2fcSFabiano Rosas 
748ccfca2fcSFabiano Rosas     vector |= env->excp_prefix;
749ccfca2fcSFabiano Rosas 
750ccfca2fcSFabiano Rosas     switch (excp) {
751ccfca2fcSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
752c354d858SVíctor Colombo         if (!FIELD_EX64(env->msr, MSR, ME)) {
753ccfca2fcSFabiano Rosas             /*
754ccfca2fcSFabiano Rosas              * Machine check exception is not enabled.  Enter
755ccfca2fcSFabiano Rosas              * checkstop state.
756ccfca2fcSFabiano Rosas              */
757ccfca2fcSFabiano Rosas             fprintf(stderr, "Machine check while not allowed. "
758ccfca2fcSFabiano Rosas                     "Entering checkstop state\n");
759ccfca2fcSFabiano Rosas             if (qemu_log_separate()) {
760ccfca2fcSFabiano Rosas                 qemu_log("Machine check while not allowed. "
761ccfca2fcSFabiano Rosas                         "Entering checkstop state\n");
762ccfca2fcSFabiano Rosas             }
763ccfca2fcSFabiano Rosas             cs->halted = 1;
764ccfca2fcSFabiano Rosas             cpu_interrupt_exittb(cs);
765ccfca2fcSFabiano Rosas         }
766ccfca2fcSFabiano Rosas 
767ccfca2fcSFabiano Rosas         /* machine check exceptions don't have ME set */
768ccfca2fcSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
769ccfca2fcSFabiano Rosas 
770ccfca2fcSFabiano Rosas         break;
771ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
772ccfca2fcSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
773ccfca2fcSFabiano Rosas         break;
774ccfca2fcSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
775ccfca2fcSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
776ccfca2fcSFabiano Rosas         msr |= env->error_code;
777ccfca2fcSFabiano Rosas         break;
778ccfca2fcSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
779ccfca2fcSFabiano Rosas         break;
780ccfca2fcSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
781ccfca2fcSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
782ccfca2fcSFabiano Rosas         /*
783ccfca2fcSFabiano Rosas          * Note: the opcode fields will not be set properly for a
784ccfca2fcSFabiano Rosas          * direct store load/store, but nobody cares as nobody
785ccfca2fcSFabiano Rosas          * actually uses direct store segments.
786ccfca2fcSFabiano Rosas          */
787ccfca2fcSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
788ccfca2fcSFabiano Rosas         break;
789ccfca2fcSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
790ccfca2fcSFabiano Rosas         switch (env->error_code & ~0xF) {
791ccfca2fcSFabiano Rosas         case POWERPC_EXCP_FP:
792da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
793ccfca2fcSFabiano Rosas                 trace_ppc_excp_fp_ignore();
7943680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
795ccfca2fcSFabiano Rosas                 return;
796ccfca2fcSFabiano Rosas             }
797ccfca2fcSFabiano Rosas 
798ccfca2fcSFabiano Rosas             /*
799ccfca2fcSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
800ccfca2fcSFabiano Rosas              * instruction, so always use store_next and claim we are
801ccfca2fcSFabiano Rosas              * precise in the MSR.
802ccfca2fcSFabiano Rosas              */
803ccfca2fcSFabiano Rosas             msr |= 0x00100000;
804ccfca2fcSFabiano Rosas             break;
805ccfca2fcSFabiano Rosas         case POWERPC_EXCP_INVAL:
806ccfca2fcSFabiano Rosas             trace_ppc_excp_inval(env->nip);
807ccfca2fcSFabiano Rosas             msr |= 0x00080000;
808ccfca2fcSFabiano Rosas             break;
809ccfca2fcSFabiano Rosas         case POWERPC_EXCP_PRIV:
810ccfca2fcSFabiano Rosas             msr |= 0x00040000;
811ccfca2fcSFabiano Rosas             break;
812ccfca2fcSFabiano Rosas         case POWERPC_EXCP_TRAP:
813ccfca2fcSFabiano Rosas             msr |= 0x00020000;
814ccfca2fcSFabiano Rosas             break;
815ccfca2fcSFabiano Rosas         default:
816ccfca2fcSFabiano Rosas             /* Should never occur */
817ccfca2fcSFabiano Rosas             cpu_abort(cs, "Invalid program exception %d. Aborting\n",
818ccfca2fcSFabiano Rosas                       env->error_code);
819ccfca2fcSFabiano Rosas             break;
820ccfca2fcSFabiano Rosas         }
821ccfca2fcSFabiano Rosas         break;
822ccfca2fcSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
8233b578635SFabiano Rosas     {
8243b578635SFabiano Rosas         int lev = env->error_code;
825ccfca2fcSFabiano Rosas 
8263b578635SFabiano Rosas         if (lev == 1 && cpu->vhyp) {
827ccfca2fcSFabiano Rosas             dump_hcall(env);
828ccfca2fcSFabiano Rosas         } else {
829ccfca2fcSFabiano Rosas             dump_syscall(env);
830ccfca2fcSFabiano Rosas         }
831ccfca2fcSFabiano Rosas 
832ccfca2fcSFabiano Rosas         /*
833ccfca2fcSFabiano Rosas          * We need to correct the NIP which in this case is supposed
834ccfca2fcSFabiano Rosas          * to point to the next instruction
835ccfca2fcSFabiano Rosas          */
836ccfca2fcSFabiano Rosas         env->nip += 4;
837ccfca2fcSFabiano Rosas 
8383b578635SFabiano Rosas         /*
8393b578635SFabiano Rosas          * The Virtual Open Firmware (VOF) relies on the 'sc 1'
8403b578635SFabiano Rosas          * instruction to communicate with QEMU. The pegasos2 machine
8413b578635SFabiano Rosas          * uses VOF and the 7xx CPUs, so although the 7xx don't have
8423b578635SFabiano Rosas          * HV mode, we need to keep hypercall support.
8433b578635SFabiano Rosas          */
8443b578635SFabiano Rosas         if (lev == 1 && cpu->vhyp) {
845ccfca2fcSFabiano Rosas             PPCVirtualHypervisorClass *vhc =
846ccfca2fcSFabiano Rosas                 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
847ccfca2fcSFabiano Rosas             vhc->hypercall(cpu->vhyp, cpu);
848ccfca2fcSFabiano Rosas             return;
849ccfca2fcSFabiano Rosas         }
8503b578635SFabiano Rosas 
851ccfca2fcSFabiano Rosas         break;
8523b578635SFabiano Rosas     }
853ccfca2fcSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
854ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
855ccfca2fcSFabiano Rosas         break;
856ccfca2fcSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
8578e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
858ccfca2fcSFabiano Rosas             cpu_abort(cs, "Trying to deliver power-saving system reset "
859ccfca2fcSFabiano Rosas                       "exception %d with no HV support\n", excp);
860ccfca2fcSFabiano Rosas         }
861ccfca2fcSFabiano Rosas         break;
862ccfca2fcSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
863ccfca2fcSFabiano Rosas         break;
864ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IFTLB:     /* Instruction fetch TLB error              */
865ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DLTLB:     /* Data load TLB miss                       */
866ccfca2fcSFabiano Rosas     case POWERPC_EXCP_DSTLB:     /* Data store TLB miss                      */
867ccfca2fcSFabiano Rosas         ppc_excp_debug_sw_tlb(env, excp);
868ccfca2fcSFabiano Rosas 
869ccfca2fcSFabiano Rosas         msr |= env->crf[0] << 28;
870ccfca2fcSFabiano Rosas         msr |= env->error_code; /* key, D/I, S/L bits */
871ccfca2fcSFabiano Rosas         /* Set way using a LRU mechanism */
872ccfca2fcSFabiano Rosas         msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
8737df40c54SFabiano Rosas 
874ccfca2fcSFabiano Rosas         break;
875ccfca2fcSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
876ccfca2fcSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
877ccfca2fcSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
878ccfca2fcSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
879ccfca2fcSFabiano Rosas         cpu_abort(cs, "%s exception not implemented\n",
880ccfca2fcSFabiano Rosas                   powerpc_excp_name(excp));
881ccfca2fcSFabiano Rosas         break;
882ccfca2fcSFabiano Rosas     default:
883ccfca2fcSFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
884ccfca2fcSFabiano Rosas         break;
885ccfca2fcSFabiano Rosas     }
886ccfca2fcSFabiano Rosas 
887ccfca2fcSFabiano Rosas     /*
888ccfca2fcSFabiano Rosas      * Sort out endianness of interrupt, this differs depending on the
889ccfca2fcSFabiano Rosas      * CPU, the HV mode, etc...
890ccfca2fcSFabiano Rosas      */
891ccfca2fcSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
892ccfca2fcSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
893ccfca2fcSFabiano Rosas     }
894ccfca2fcSFabiano Rosas 
895ccfca2fcSFabiano Rosas     /* Save PC */
896fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
897ccfca2fcSFabiano Rosas 
898ccfca2fcSFabiano Rosas     /* Save MSR */
899fe4b5c4cSFabiano Rosas     env->spr[SPR_SRR1] = msr;
900ccfca2fcSFabiano Rosas 
901ccfca2fcSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
902ccfca2fcSFabiano Rosas }
903ccfca2fcSFabiano Rosas 
90452926b0dSFabiano Rosas static void powerpc_excp_74xx(PowerPCCPU *cpu, int excp)
90552926b0dSFabiano Rosas {
90652926b0dSFabiano Rosas     CPUState *cs = CPU(cpu);
90752926b0dSFabiano Rosas     CPUPPCState *env = &cpu->env;
90852926b0dSFabiano Rosas     target_ulong msr, new_msr, vector;
90952926b0dSFabiano Rosas 
91052926b0dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
91152926b0dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
91252926b0dSFabiano Rosas 
91352926b0dSFabiano Rosas     /*
9141f6faf8bSFabiano Rosas      * new interrupt handler msr preserves existing ME unless
91552926b0dSFabiano Rosas      * explicitly overriden
91652926b0dSFabiano Rosas      */
9171f6faf8bSFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
91852926b0dSFabiano Rosas 
91952926b0dSFabiano Rosas     /*
92052926b0dSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
9211f6faf8bSFabiano Rosas      * arch 2.05 server or later.
92252926b0dSFabiano Rosas      */
9231f6faf8bSFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
92452926b0dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
92552926b0dSFabiano Rosas     }
92652926b0dSFabiano Rosas 
92752926b0dSFabiano Rosas     vector = env->excp_vectors[excp];
92852926b0dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
92952926b0dSFabiano Rosas         cpu_abort(cs, "Raised an exception without defined vector %d\n",
93052926b0dSFabiano Rosas                   excp);
93152926b0dSFabiano Rosas     }
93252926b0dSFabiano Rosas 
93352926b0dSFabiano Rosas     vector |= env->excp_prefix;
93452926b0dSFabiano Rosas 
93552926b0dSFabiano Rosas     switch (excp) {
93652926b0dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
937c354d858SVíctor Colombo         if (!FIELD_EX64(env->msr, MSR, ME)) {
93852926b0dSFabiano Rosas             /*
93952926b0dSFabiano Rosas              * Machine check exception is not enabled.  Enter
94052926b0dSFabiano Rosas              * checkstop state.
94152926b0dSFabiano Rosas              */
94252926b0dSFabiano Rosas             fprintf(stderr, "Machine check while not allowed. "
94352926b0dSFabiano Rosas                     "Entering checkstop state\n");
94452926b0dSFabiano Rosas             if (qemu_log_separate()) {
94552926b0dSFabiano Rosas                 qemu_log("Machine check while not allowed. "
94652926b0dSFabiano Rosas                         "Entering checkstop state\n");
94752926b0dSFabiano Rosas             }
94852926b0dSFabiano Rosas             cs->halted = 1;
94952926b0dSFabiano Rosas             cpu_interrupt_exittb(cs);
95052926b0dSFabiano Rosas         }
95152926b0dSFabiano Rosas 
95252926b0dSFabiano Rosas         /* machine check exceptions don't have ME set */
95352926b0dSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
95452926b0dSFabiano Rosas 
95552926b0dSFabiano Rosas         break;
95652926b0dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
95752926b0dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
95852926b0dSFabiano Rosas         break;
95952926b0dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
96052926b0dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
96152926b0dSFabiano Rosas         msr |= env->error_code;
96252926b0dSFabiano Rosas         break;
96352926b0dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
96452926b0dSFabiano Rosas         break;
96552926b0dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
96652926b0dSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
96752926b0dSFabiano Rosas         /*
96852926b0dSFabiano Rosas          * Note: the opcode fields will not be set properly for a
96952926b0dSFabiano Rosas          * direct store load/store, but nobody cares as nobody
97052926b0dSFabiano Rosas          * actually uses direct store segments.
97152926b0dSFabiano Rosas          */
97252926b0dSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
97352926b0dSFabiano Rosas         break;
97452926b0dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
97552926b0dSFabiano Rosas         switch (env->error_code & ~0xF) {
97652926b0dSFabiano Rosas         case POWERPC_EXCP_FP:
977da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
97852926b0dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
9793680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
98052926b0dSFabiano Rosas                 return;
98152926b0dSFabiano Rosas             }
98252926b0dSFabiano Rosas 
98352926b0dSFabiano Rosas             /*
98452926b0dSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
98552926b0dSFabiano Rosas              * instruction, so always use store_next and claim we are
98652926b0dSFabiano Rosas              * precise in the MSR.
98752926b0dSFabiano Rosas              */
98852926b0dSFabiano Rosas             msr |= 0x00100000;
98952926b0dSFabiano Rosas             break;
99052926b0dSFabiano Rosas         case POWERPC_EXCP_INVAL:
99152926b0dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
99252926b0dSFabiano Rosas             msr |= 0x00080000;
99352926b0dSFabiano Rosas             break;
99452926b0dSFabiano Rosas         case POWERPC_EXCP_PRIV:
99552926b0dSFabiano Rosas             msr |= 0x00040000;
99652926b0dSFabiano Rosas             break;
99752926b0dSFabiano Rosas         case POWERPC_EXCP_TRAP:
99852926b0dSFabiano Rosas             msr |= 0x00020000;
99952926b0dSFabiano Rosas             break;
100052926b0dSFabiano Rosas         default:
100152926b0dSFabiano Rosas             /* Should never occur */
100252926b0dSFabiano Rosas             cpu_abort(cs, "Invalid program exception %d. Aborting\n",
100352926b0dSFabiano Rosas                       env->error_code);
100452926b0dSFabiano Rosas             break;
100552926b0dSFabiano Rosas         }
100652926b0dSFabiano Rosas         break;
100752926b0dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
1008bca2c6d9SFabiano Rosas     {
1009bca2c6d9SFabiano Rosas         int lev = env->error_code;
101052926b0dSFabiano Rosas 
101152926b0dSFabiano Rosas         if ((lev == 1) && cpu->vhyp) {
101252926b0dSFabiano Rosas             dump_hcall(env);
101352926b0dSFabiano Rosas         } else {
101452926b0dSFabiano Rosas             dump_syscall(env);
101552926b0dSFabiano Rosas         }
101652926b0dSFabiano Rosas 
101752926b0dSFabiano Rosas         /*
101852926b0dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
101952926b0dSFabiano Rosas          * to point to the next instruction
102052926b0dSFabiano Rosas          */
102152926b0dSFabiano Rosas         env->nip += 4;
102252926b0dSFabiano Rosas 
1023bca2c6d9SFabiano Rosas         /*
1024bca2c6d9SFabiano Rosas          * The Virtual Open Firmware (VOF) relies on the 'sc 1'
1025bca2c6d9SFabiano Rosas          * instruction to communicate with QEMU. The pegasos2 machine
1026bca2c6d9SFabiano Rosas          * uses VOF and the 74xx CPUs, so although the 74xx don't have
1027bca2c6d9SFabiano Rosas          * HV mode, we need to keep hypercall support.
1028bca2c6d9SFabiano Rosas          */
102952926b0dSFabiano Rosas         if ((lev == 1) && cpu->vhyp) {
103052926b0dSFabiano Rosas             PPCVirtualHypervisorClass *vhc =
103152926b0dSFabiano Rosas                 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
103252926b0dSFabiano Rosas             vhc->hypercall(cpu->vhyp, cpu);
103352926b0dSFabiano Rosas             return;
103452926b0dSFabiano Rosas         }
1035bca2c6d9SFabiano Rosas 
103652926b0dSFabiano Rosas         break;
1037bca2c6d9SFabiano Rosas     }
103852926b0dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
103952926b0dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
104052926b0dSFabiano Rosas         break;
104152926b0dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
10428e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
104352926b0dSFabiano Rosas             cpu_abort(cs, "Trying to deliver power-saving system reset "
104452926b0dSFabiano Rosas                       "exception %d with no HV support\n", excp);
104552926b0dSFabiano Rosas         }
104652926b0dSFabiano Rosas         break;
104752926b0dSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
104852926b0dSFabiano Rosas         break;
104952926b0dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
105052926b0dSFabiano Rosas         break;
105152926b0dSFabiano Rosas     case POWERPC_EXCP_IABR:      /* Instruction address breakpoint           */
105252926b0dSFabiano Rosas     case POWERPC_EXCP_SMI:       /* System management interrupt              */
105352926b0dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
105452926b0dSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
105552926b0dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
105652926b0dSFabiano Rosas         cpu_abort(cs, "%s exception not implemented\n",
105752926b0dSFabiano Rosas                   powerpc_excp_name(excp));
105852926b0dSFabiano Rosas         break;
105952926b0dSFabiano Rosas     default:
106052926b0dSFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
106152926b0dSFabiano Rosas         break;
106252926b0dSFabiano Rosas     }
106352926b0dSFabiano Rosas 
106452926b0dSFabiano Rosas     /*
106552926b0dSFabiano Rosas      * Sort out endianness of interrupt, this differs depending on the
106652926b0dSFabiano Rosas      * CPU, the HV mode, etc...
106752926b0dSFabiano Rosas      */
106852926b0dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
106952926b0dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
107052926b0dSFabiano Rosas     }
107152926b0dSFabiano Rosas 
107252926b0dSFabiano Rosas     /* Save PC */
1073f82db777SFabiano Rosas     env->spr[SPR_SRR0] = env->nip;
107452926b0dSFabiano Rosas 
107552926b0dSFabiano Rosas     /* Save MSR */
1076f82db777SFabiano Rosas     env->spr[SPR_SRR1] = msr;
107752926b0dSFabiano Rosas 
107852926b0dSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
107952926b0dSFabiano Rosas }
108052926b0dSFabiano Rosas 
1081180952ceSFabiano Rosas static void powerpc_excp_booke(PowerPCCPU *cpu, int excp)
1082180952ceSFabiano Rosas {
1083180952ceSFabiano Rosas     CPUState *cs = CPU(cpu);
1084180952ceSFabiano Rosas     CPUPPCState *env = &cpu->env;
1085180952ceSFabiano Rosas     target_ulong msr, new_msr, vector;
1086904e8428SFabiano Rosas     int srr0, srr1;
1087180952ceSFabiano Rosas 
1088180952ceSFabiano Rosas     msr = env->msr;
1089180952ceSFabiano Rosas 
1090180952ceSFabiano Rosas     /*
10919dc20cc3SFabiano Rosas      * new interrupt handler msr preserves existing ME unless
1092180952ceSFabiano Rosas      * explicitly overriden
1093180952ceSFabiano Rosas      */
10949dc20cc3SFabiano Rosas     new_msr = env->msr & ((target_ulong)1 << MSR_ME);
1095180952ceSFabiano Rosas 
1096180952ceSFabiano Rosas     /* target registers */
1097180952ceSFabiano Rosas     srr0 = SPR_SRR0;
1098180952ceSFabiano Rosas     srr1 = SPR_SRR1;
1099180952ceSFabiano Rosas 
1100180952ceSFabiano Rosas     /*
1101180952ceSFabiano Rosas      * Hypervisor emulation assistance interrupt only exists on server
11029dc20cc3SFabiano Rosas      * arch 2.05 server or later.
1103180952ceSFabiano Rosas      */
11049dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU) {
1105180952ceSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
1106180952ceSFabiano Rosas     }
1107180952ceSFabiano Rosas 
1108180952ceSFabiano Rosas #ifdef TARGET_PPC64
1109180952ceSFabiano Rosas     /*
1110180952ceSFabiano Rosas      * SPEU and VPU share the same IVOR but they exist in different
1111180952ceSFabiano Rosas      * processors. SPEU is e500v1/2 only and VPU is e6500 only.
1112180952ceSFabiano Rosas      */
11139dc20cc3SFabiano Rosas     if (excp == POWERPC_EXCP_VPU) {
1114180952ceSFabiano Rosas         excp = POWERPC_EXCP_SPEU;
1115180952ceSFabiano Rosas     }
1116180952ceSFabiano Rosas #endif
1117180952ceSFabiano Rosas 
1118180952ceSFabiano Rosas     vector = env->excp_vectors[excp];
1119180952ceSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
1120180952ceSFabiano Rosas         cpu_abort(cs, "Raised an exception without defined vector %d\n",
1121180952ceSFabiano Rosas                   excp);
1122180952ceSFabiano Rosas     }
1123180952ceSFabiano Rosas 
1124180952ceSFabiano Rosas     vector |= env->excp_prefix;
1125180952ceSFabiano Rosas 
1126180952ceSFabiano Rosas     switch (excp) {
1127180952ceSFabiano Rosas     case POWERPC_EXCP_CRITICAL:    /* Critical input                         */
1128180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
1129180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
1130180952ceSFabiano Rosas         break;
1131180952ceSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
1132c354d858SVíctor Colombo         if (!FIELD_EX64(env->msr, MSR, ME)) {
1133180952ceSFabiano Rosas             /*
1134180952ceSFabiano Rosas              * Machine check exception is not enabled.  Enter
1135180952ceSFabiano Rosas              * checkstop state.
1136180952ceSFabiano Rosas              */
1137180952ceSFabiano Rosas             fprintf(stderr, "Machine check while not allowed. "
1138180952ceSFabiano Rosas                     "Entering checkstop state\n");
1139180952ceSFabiano Rosas             if (qemu_log_separate()) {
1140180952ceSFabiano Rosas                 qemu_log("Machine check while not allowed. "
1141180952ceSFabiano Rosas                         "Entering checkstop state\n");
1142180952ceSFabiano Rosas             }
1143180952ceSFabiano Rosas             cs->halted = 1;
1144180952ceSFabiano Rosas             cpu_interrupt_exittb(cs);
1145180952ceSFabiano Rosas         }
1146180952ceSFabiano Rosas 
1147180952ceSFabiano Rosas         /* machine check exceptions don't have ME set */
1148180952ceSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
1149180952ceSFabiano Rosas 
1150180952ceSFabiano Rosas         /* FIXME: choose one or the other based on CPU type */
1151180952ceSFabiano Rosas         srr0 = SPR_BOOKE_MCSRR0;
1152180952ceSFabiano Rosas         srr1 = SPR_BOOKE_MCSRR1;
1153180952ceSFabiano Rosas 
1154180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR0] = env->nip;
1155180952ceSFabiano Rosas         env->spr[SPR_BOOKE_CSRR1] = msr;
1156db403211SFabiano Rosas 
1157180952ceSFabiano Rosas         break;
1158180952ceSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
1159afdbc869SFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
1160180952ceSFabiano Rosas         break;
1161180952ceSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
1162180952ceSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
1163180952ceSFabiano Rosas         break;
1164180952ceSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
1165180952ceSFabiano Rosas         if (env->mpic_proxy) {
1166180952ceSFabiano Rosas             /* IACK the IRQ on delivery */
1167180952ceSFabiano Rosas             env->spr[SPR_BOOKE_EPR] = ldl_phys(cs->as, env->mpic_iack);
1168180952ceSFabiano Rosas         }
1169180952ceSFabiano Rosas         break;
1170180952ceSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
1171180952ceSFabiano Rosas         break;
1172180952ceSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
1173180952ceSFabiano Rosas         switch (env->error_code & ~0xF) {
1174180952ceSFabiano Rosas         case POWERPC_EXCP_FP:
1175da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
1176180952ceSFabiano Rosas                 trace_ppc_excp_fp_ignore();
11773680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
1178180952ceSFabiano Rosas                 return;
1179180952ceSFabiano Rosas             }
1180180952ceSFabiano Rosas 
1181180952ceSFabiano Rosas             /*
1182180952ceSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
1183180952ceSFabiano Rosas              * instruction, so always use store_next and claim we are
1184180952ceSFabiano Rosas              * precise in the MSR.
1185180952ceSFabiano Rosas              */
1186180952ceSFabiano Rosas             msr |= 0x00100000;
1187180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_FP;
1188180952ceSFabiano Rosas             break;
1189180952ceSFabiano Rosas         case POWERPC_EXCP_INVAL:
1190180952ceSFabiano Rosas             trace_ppc_excp_inval(env->nip);
1191180952ceSFabiano Rosas             msr |= 0x00080000;
1192180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PIL;
1193180952ceSFabiano Rosas             break;
1194180952ceSFabiano Rosas         case POWERPC_EXCP_PRIV:
1195180952ceSFabiano Rosas             msr |= 0x00040000;
1196180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PPR;
1197180952ceSFabiano Rosas             break;
1198180952ceSFabiano Rosas         case POWERPC_EXCP_TRAP:
1199180952ceSFabiano Rosas             msr |= 0x00020000;
1200180952ceSFabiano Rosas             env->spr[SPR_BOOKE_ESR] = ESR_PTR;
1201180952ceSFabiano Rosas             break;
1202180952ceSFabiano Rosas         default:
1203180952ceSFabiano Rosas             /* Should never occur */
1204180952ceSFabiano Rosas             cpu_abort(cs, "Invalid program exception %d. Aborting\n",
1205180952ceSFabiano Rosas                       env->error_code);
1206180952ceSFabiano Rosas             break;
1207180952ceSFabiano Rosas         }
1208180952ceSFabiano Rosas         break;
1209180952ceSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
1210180952ceSFabiano Rosas         dump_syscall(env);
1211180952ceSFabiano Rosas 
1212180952ceSFabiano Rosas         /*
1213180952ceSFabiano Rosas          * We need to correct the NIP which in this case is supposed
1214180952ceSFabiano Rosas          * to point to the next instruction
1215180952ceSFabiano Rosas          */
1216180952ceSFabiano Rosas         env->nip += 4;
1217180952ceSFabiano Rosas         break;
1218180952ceSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
1219180952ceSFabiano Rosas     case POWERPC_EXCP_APU:       /* Auxiliary processor unavailable          */
1220180952ceSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
1221180952ceSFabiano Rosas         break;
1222180952ceSFabiano Rosas     case POWERPC_EXCP_FIT:       /* Fixed-interval timer interrupt           */
1223180952ceSFabiano Rosas         /* FIT on 4xx */
1224180952ceSFabiano Rosas         trace_ppc_excp_print("FIT");
1225180952ceSFabiano Rosas         break;
1226180952ceSFabiano Rosas     case POWERPC_EXCP_WDT:       /* Watchdog timer interrupt                 */
1227180952ceSFabiano Rosas         trace_ppc_excp_print("WDT");
1228180952ceSFabiano Rosas         srr0 = SPR_BOOKE_CSRR0;
1229180952ceSFabiano Rosas         srr1 = SPR_BOOKE_CSRR1;
1230180952ceSFabiano Rosas         break;
1231180952ceSFabiano Rosas     case POWERPC_EXCP_DTLB:      /* Data TLB error                           */
1232180952ceSFabiano Rosas     case POWERPC_EXCP_ITLB:      /* Instruction TLB error                    */
1233180952ceSFabiano Rosas         break;
1234180952ceSFabiano Rosas     case POWERPC_EXCP_DEBUG:     /* Debug interrupt                          */
1235180952ceSFabiano Rosas         if (env->flags & POWERPC_FLAG_DE) {
1236180952ceSFabiano Rosas             /* FIXME: choose one or the other based on CPU type */
1237180952ceSFabiano Rosas             srr0 = SPR_BOOKE_DSRR0;
1238180952ceSFabiano Rosas             srr1 = SPR_BOOKE_DSRR1;
1239180952ceSFabiano Rosas 
1240180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR0] = env->nip;
1241180952ceSFabiano Rosas             env->spr[SPR_BOOKE_CSRR1] = msr;
1242180952ceSFabiano Rosas 
1243180952ceSFabiano Rosas             /* DBSR already modified by caller */
1244180952ceSFabiano Rosas         } else {
1245180952ceSFabiano Rosas             cpu_abort(cs, "Debug exception triggered on unsupported model\n");
1246180952ceSFabiano Rosas         }
1247180952ceSFabiano Rosas         break;
1248180952ceSFabiano Rosas     case POWERPC_EXCP_SPEU:   /* SPE/embedded floating-point unavailable/VPU  */
1249180952ceSFabiano Rosas         env->spr[SPR_BOOKE_ESR] = ESR_SPV;
1250180952ceSFabiano Rosas         break;
12519364df26SNicholas Piggin     case POWERPC_EXCP_DOORI:     /* Embedded doorbell interrupt              */
12529364df26SNicholas Piggin         break;
12539364df26SNicholas Piggin     case POWERPC_EXCP_DOORCI:    /* Embedded doorbell critical interrupt     */
12549364df26SNicholas Piggin         srr0 = SPR_BOOKE_CSRR0;
12559364df26SNicholas Piggin         srr1 = SPR_BOOKE_CSRR1;
12569364df26SNicholas Piggin         break;
1257180952ceSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
12588e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
1259180952ceSFabiano Rosas             cpu_abort(cs, "Trying to deliver power-saving system reset "
1260180952ceSFabiano Rosas                       "exception %d with no HV support\n", excp);
1261180952ceSFabiano Rosas         }
1262180952ceSFabiano Rosas         break;
1263180952ceSFabiano Rosas     case POWERPC_EXCP_EFPDI:     /* Embedded floating-point data interrupt   */
1264180952ceSFabiano Rosas     case POWERPC_EXCP_EFPRI:     /* Embedded floating-point round interrupt  */
1265180952ceSFabiano Rosas         cpu_abort(cs, "%s exception not implemented\n",
1266180952ceSFabiano Rosas                   powerpc_excp_name(excp));
1267180952ceSFabiano Rosas         break;
1268180952ceSFabiano Rosas     default:
1269180952ceSFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
1270180952ceSFabiano Rosas         break;
1271180952ceSFabiano Rosas     }
1272180952ceSFabiano Rosas 
1273180952ceSFabiano Rosas #if defined(TARGET_PPC64)
1274180952ceSFabiano Rosas     if (env->spr[SPR_BOOKE_EPCR] & EPCR_ICM) {
1275180952ceSFabiano Rosas         /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */
1276180952ceSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_CM;
1277180952ceSFabiano Rosas     } else {
1278180952ceSFabiano Rosas         vector = (uint32_t)vector;
1279180952ceSFabiano Rosas     }
1280180952ceSFabiano Rosas #endif
1281180952ceSFabiano Rosas 
1282180952ceSFabiano Rosas     /* Save PC */
1283180952ceSFabiano Rosas     env->spr[srr0] = env->nip;
1284180952ceSFabiano Rosas 
1285180952ceSFabiano Rosas     /* Save MSR */
1286180952ceSFabiano Rosas     env->spr[srr1] = msr;
1287180952ceSFabiano Rosas 
1288180952ceSFabiano Rosas     powerpc_set_excp_state(cpu, vector, new_msr);
1289180952ceSFabiano Rosas }
1290180952ceSFabiano Rosas 
12917cebc5dbSNicholas Piggin /*
12927cebc5dbSNicholas Piggin  * When running a nested HV guest under vhyp, external interrupts are
12937cebc5dbSNicholas Piggin  * delivered as HVIRT.
12947cebc5dbSNicholas Piggin  */
12957cebc5dbSNicholas Piggin static bool books_vhyp_promotes_external_to_hvirt(PowerPCCPU *cpu)
12967cebc5dbSNicholas Piggin {
12977cebc5dbSNicholas Piggin     if (cpu->vhyp) {
12987cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
12997cebc5dbSNicholas Piggin     }
13007cebc5dbSNicholas Piggin     return false;
13017cebc5dbSNicholas Piggin }
13027cebc5dbSNicholas Piggin 
130330c4e426SFabiano Rosas #ifdef TARGET_PPC64
13044c6cf6b2SNicholas Piggin /*
13054c6cf6b2SNicholas Piggin  * When running under vhyp, hcalls are always intercepted and sent to the
13064c6cf6b2SNicholas Piggin  * vhc->hypercall handler.
13074c6cf6b2SNicholas Piggin  */
13084c6cf6b2SNicholas Piggin static bool books_vhyp_handles_hcall(PowerPCCPU *cpu)
13094c6cf6b2SNicholas Piggin {
13104c6cf6b2SNicholas Piggin     if (cpu->vhyp) {
13117cebc5dbSNicholas Piggin         return !vhyp_cpu_in_nested(cpu);
13127cebc5dbSNicholas Piggin     }
13137cebc5dbSNicholas Piggin     return false;
13147cebc5dbSNicholas Piggin }
13157cebc5dbSNicholas Piggin 
13167cebc5dbSNicholas Piggin /*
13177cebc5dbSNicholas Piggin  * When running a nested KVM HV guest under vhyp, HV exceptions are not
13187cebc5dbSNicholas Piggin  * delivered to the guest (because there is no concept of HV support), but
13197cebc5dbSNicholas Piggin  * rather they are sent tothe vhyp to exit from the L2 back to the L1 and
13207cebc5dbSNicholas Piggin  * return from the H_ENTER_NESTED hypercall.
13217cebc5dbSNicholas Piggin  */
13227cebc5dbSNicholas Piggin static bool books_vhyp_handles_hv_excp(PowerPCCPU *cpu)
13237cebc5dbSNicholas Piggin {
13247cebc5dbSNicholas Piggin     if (cpu->vhyp) {
13257cebc5dbSNicholas Piggin         return vhyp_cpu_in_nested(cpu);
13264c6cf6b2SNicholas Piggin     }
13274c6cf6b2SNicholas Piggin     return false;
13284c6cf6b2SNicholas Piggin }
13294c6cf6b2SNicholas Piggin 
13309f338e4dSFabiano Rosas static void powerpc_excp_books(PowerPCCPU *cpu, int excp)
13319f338e4dSFabiano Rosas {
13329f338e4dSFabiano Rosas     CPUState *cs = CPU(cpu);
13339f338e4dSFabiano Rosas     CPUPPCState *env = &cpu->env;
13349f338e4dSFabiano Rosas     target_ulong msr, new_msr, vector;
13359f338e4dSFabiano Rosas     int srr0, srr1, lev = -1;
13369f338e4dSFabiano Rosas 
13379f338e4dSFabiano Rosas     /* new srr1 value excluding must-be-zero bits */
13389f338e4dSFabiano Rosas     msr = env->msr & ~0x783f0000ULL;
13399f338e4dSFabiano Rosas 
13409f338e4dSFabiano Rosas     /*
13419f338e4dSFabiano Rosas      * new interrupt handler msr preserves existing HV and ME unless
13429f338e4dSFabiano Rosas      * explicitly overriden
13439f338e4dSFabiano Rosas      */
13449f338e4dSFabiano Rosas     new_msr = env->msr & (((target_ulong)1 << MSR_ME) | MSR_HVB);
13459f338e4dSFabiano Rosas 
13469f338e4dSFabiano Rosas     /* target registers */
13479f338e4dSFabiano Rosas     srr0 = SPR_SRR0;
13489f338e4dSFabiano Rosas     srr1 = SPR_SRR1;
13499f338e4dSFabiano Rosas 
13509f338e4dSFabiano Rosas     /*
13519f338e4dSFabiano Rosas      * check for special resume at 0x100 from doze/nap/sleep/winkle on
13529f338e4dSFabiano Rosas      * P7/P8/P9
13539f338e4dSFabiano Rosas      */
13549f338e4dSFabiano Rosas     if (env->resume_as_sreset) {
13559f338e4dSFabiano Rosas         excp = powerpc_reset_wakeup(cs, env, excp, &msr);
13569f338e4dSFabiano Rosas     }
13579f338e4dSFabiano Rosas 
13589f338e4dSFabiano Rosas     /*
135930c4e426SFabiano Rosas      * We don't want to generate a Hypervisor Emulation Assistance
136030c4e426SFabiano Rosas      * Interrupt if we don't have HVB in msr_mask (PAPR mode).
13619f338e4dSFabiano Rosas      */
136230c4e426SFabiano Rosas     if (excp == POWERPC_EXCP_HV_EMU && !(env->msr_mask & MSR_HVB)) {
13639f338e4dSFabiano Rosas         excp = POWERPC_EXCP_PROGRAM;
13649f338e4dSFabiano Rosas     }
13659f338e4dSFabiano Rosas 
13669f338e4dSFabiano Rosas     vector = env->excp_vectors[excp];
13679f338e4dSFabiano Rosas     if (vector == (target_ulong)-1ULL) {
13689f338e4dSFabiano Rosas         cpu_abort(cs, "Raised an exception without defined vector %d\n",
13699f338e4dSFabiano Rosas                   excp);
13709f338e4dSFabiano Rosas     }
13719f338e4dSFabiano Rosas 
13729f338e4dSFabiano Rosas     vector |= env->excp_prefix;
13739f338e4dSFabiano Rosas 
13749f338e4dSFabiano Rosas     switch (excp) {
13759f338e4dSFabiano Rosas     case POWERPC_EXCP_MCHECK:    /* Machine check exception                  */
1376c354d858SVíctor Colombo         if (!FIELD_EX64(env->msr, MSR, ME)) {
13779f338e4dSFabiano Rosas             /*
13789f338e4dSFabiano Rosas              * Machine check exception is not enabled.  Enter
13799f338e4dSFabiano Rosas              * checkstop state.
13809f338e4dSFabiano Rosas              */
13819f338e4dSFabiano Rosas             fprintf(stderr, "Machine check while not allowed. "
13829f338e4dSFabiano Rosas                     "Entering checkstop state\n");
13839f338e4dSFabiano Rosas             if (qemu_log_separate()) {
13849f338e4dSFabiano Rosas                 qemu_log("Machine check while not allowed. "
13859f338e4dSFabiano Rosas                         "Entering checkstop state\n");
13869f338e4dSFabiano Rosas             }
13879f338e4dSFabiano Rosas             cs->halted = 1;
13889f338e4dSFabiano Rosas             cpu_interrupt_exittb(cs);
13899f338e4dSFabiano Rosas         }
13909f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
13919f338e4dSFabiano Rosas             /*
13929f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
13939f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR).
13949f338e4dSFabiano Rosas              */
13959f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
13969f338e4dSFabiano Rosas         }
13979f338e4dSFabiano Rosas 
13989f338e4dSFabiano Rosas         /* machine check exceptions don't have ME set */
13999f338e4dSFabiano Rosas         new_msr &= ~((target_ulong)1 << MSR_ME);
14009f338e4dSFabiano Rosas 
14019f338e4dSFabiano Rosas         break;
14029f338e4dSFabiano Rosas     case POWERPC_EXCP_DSI:       /* Data storage exception                   */
14039f338e4dSFabiano Rosas         trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
14049f338e4dSFabiano Rosas         break;
14059f338e4dSFabiano Rosas     case POWERPC_EXCP_ISI:       /* Instruction storage exception            */
14069f338e4dSFabiano Rosas         trace_ppc_excp_isi(msr, env->nip);
14079f338e4dSFabiano Rosas         msr |= env->error_code;
14089f338e4dSFabiano Rosas         break;
14099f338e4dSFabiano Rosas     case POWERPC_EXCP_EXTERNAL:  /* External input                           */
14109f338e4dSFabiano Rosas     {
14119f338e4dSFabiano Rosas         bool lpes0;
14129f338e4dSFabiano Rosas 
14139f338e4dSFabiano Rosas         /*
141467baff77SFabiano Rosas          * LPES0 is only taken into consideration if we support HV
141567baff77SFabiano Rosas          * mode for this CPU.
14169f338e4dSFabiano Rosas          */
141767baff77SFabiano Rosas         if (!env->has_hv_mode) {
141867baff77SFabiano Rosas             break;
14199f338e4dSFabiano Rosas         }
14209f338e4dSFabiano Rosas 
142167baff77SFabiano Rosas         lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
142267baff77SFabiano Rosas 
14239f338e4dSFabiano Rosas         if (!lpes0) {
14249f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14259f338e4dSFabiano Rosas             new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
14269f338e4dSFabiano Rosas             srr0 = SPR_HSRR0;
14279f338e4dSFabiano Rosas             srr1 = SPR_HSRR1;
14289f338e4dSFabiano Rosas         }
142967baff77SFabiano Rosas 
14309f338e4dSFabiano Rosas         break;
14319f338e4dSFabiano Rosas     }
14329f338e4dSFabiano Rosas     case POWERPC_EXCP_ALIGN:     /* Alignment exception                      */
14339f338e4dSFabiano Rosas         /* Get rS/rD and rA from faulting opcode */
14349f338e4dSFabiano Rosas         /*
14359f338e4dSFabiano Rosas          * Note: the opcode fields will not be set properly for a
14369f338e4dSFabiano Rosas          * direct store load/store, but nobody cares as nobody
14379f338e4dSFabiano Rosas          * actually uses direct store segments.
14389f338e4dSFabiano Rosas          */
14399f338e4dSFabiano Rosas         env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
14409f338e4dSFabiano Rosas         break;
14419f338e4dSFabiano Rosas     case POWERPC_EXCP_PROGRAM:   /* Program exception                        */
14429f338e4dSFabiano Rosas         switch (env->error_code & ~0xF) {
14439f338e4dSFabiano Rosas         case POWERPC_EXCP_FP:
1444da806a6cSVíctor Colombo             if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
14459f338e4dSFabiano Rosas                 trace_ppc_excp_fp_ignore();
14463680e994SNicholas Piggin                 powerpc_reset_excp_state(cpu);
14479f338e4dSFabiano Rosas                 return;
14489f338e4dSFabiano Rosas             }
14499f338e4dSFabiano Rosas 
14509f338e4dSFabiano Rosas             /*
14519f338e4dSFabiano Rosas              * FP exceptions always have NIP pointing to the faulting
14529f338e4dSFabiano Rosas              * instruction, so always use store_next and claim we are
14539f338e4dSFabiano Rosas              * precise in the MSR.
14549f338e4dSFabiano Rosas              */
14559f338e4dSFabiano Rosas             msr |= 0x00100000;
14569f338e4dSFabiano Rosas             break;
14579f338e4dSFabiano Rosas         case POWERPC_EXCP_INVAL:
14589f338e4dSFabiano Rosas             trace_ppc_excp_inval(env->nip);
14599f338e4dSFabiano Rosas             msr |= 0x00080000;
14609f338e4dSFabiano Rosas             break;
14619f338e4dSFabiano Rosas         case POWERPC_EXCP_PRIV:
14629f338e4dSFabiano Rosas             msr |= 0x00040000;
14639f338e4dSFabiano Rosas             break;
14649f338e4dSFabiano Rosas         case POWERPC_EXCP_TRAP:
14659f338e4dSFabiano Rosas             msr |= 0x00020000;
14669f338e4dSFabiano Rosas             break;
14679f338e4dSFabiano Rosas         default:
14689f338e4dSFabiano Rosas             /* Should never occur */
14699f338e4dSFabiano Rosas             cpu_abort(cs, "Invalid program exception %d. Aborting\n",
14709f338e4dSFabiano Rosas                       env->error_code);
14719f338e4dSFabiano Rosas             break;
14729f338e4dSFabiano Rosas         }
14739f338e4dSFabiano Rosas         break;
14749f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL:   /* System call exception                    */
14759f338e4dSFabiano Rosas         lev = env->error_code;
14769f338e4dSFabiano Rosas 
14779f338e4dSFabiano Rosas         if ((lev == 1) && cpu->vhyp) {
14789f338e4dSFabiano Rosas             dump_hcall(env);
14799f338e4dSFabiano Rosas         } else {
14809f338e4dSFabiano Rosas             dump_syscall(env);
14819f338e4dSFabiano Rosas         }
14829f338e4dSFabiano Rosas 
14839f338e4dSFabiano Rosas         /*
14849f338e4dSFabiano Rosas          * We need to correct the NIP which in this case is supposed
14859f338e4dSFabiano Rosas          * to point to the next instruction
14869f338e4dSFabiano Rosas          */
14879f338e4dSFabiano Rosas         env->nip += 4;
14889f338e4dSFabiano Rosas 
14899f338e4dSFabiano Rosas         /* "PAPR mode" built-in hypercall emulation */
14904c6cf6b2SNicholas Piggin         if ((lev == 1) && books_vhyp_handles_hcall(cpu)) {
14919f338e4dSFabiano Rosas             PPCVirtualHypervisorClass *vhc =
14929f338e4dSFabiano Rosas                 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
14939f338e4dSFabiano Rosas             vhc->hypercall(cpu->vhyp, cpu);
14949f338e4dSFabiano Rosas             return;
14959f338e4dSFabiano Rosas         }
14969f338e4dSFabiano Rosas         if (lev == 1) {
14979f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
14989f338e4dSFabiano Rosas         }
14999f338e4dSFabiano Rosas         break;
15009f338e4dSFabiano Rosas     case POWERPC_EXCP_SYSCALL_VECTORED: /* scv exception                     */
15019f338e4dSFabiano Rosas         lev = env->error_code;
15029f338e4dSFabiano Rosas         dump_syscall(env);
15039f338e4dSFabiano Rosas         env->nip += 4;
15049f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_EE);
15059f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
15069f338e4dSFabiano Rosas 
15079f338e4dSFabiano Rosas         vector += lev * 0x20;
15089f338e4dSFabiano Rosas 
15099f338e4dSFabiano Rosas         env->lr = env->nip;
15109f338e4dSFabiano Rosas         env->ctr = msr;
15119f338e4dSFabiano Rosas         break;
15129f338e4dSFabiano Rosas     case POWERPC_EXCP_FPU:       /* Floating-point unavailable exception     */
15139f338e4dSFabiano Rosas     case POWERPC_EXCP_DECR:      /* Decrementer exception                    */
15149f338e4dSFabiano Rosas         break;
15159f338e4dSFabiano Rosas     case POWERPC_EXCP_RESET:     /* System reset exception                   */
15169f338e4dSFabiano Rosas         /* A power-saving exception sets ME, otherwise it is unchanged */
15178e54ad65SVíctor Colombo         if (FIELD_EX64(env->msr, MSR, POW)) {
15189f338e4dSFabiano Rosas             /* indicate that we resumed from power save mode */
15199f338e4dSFabiano Rosas             msr |= 0x10000;
15209f338e4dSFabiano Rosas             new_msr |= ((target_ulong)1 << MSR_ME);
15219f338e4dSFabiano Rosas         }
15229f338e4dSFabiano Rosas         if (env->msr_mask & MSR_HVB) {
15239f338e4dSFabiano Rosas             /*
15249f338e4dSFabiano Rosas              * ISA specifies HV, but can be delivered to guest with HV
15259f338e4dSFabiano Rosas              * clear (e.g., see FWNMI in PAPR, NMI injection in QEMU).
15269f338e4dSFabiano Rosas              */
15279f338e4dSFabiano Rosas             new_msr |= (target_ulong)MSR_HVB;
15289f338e4dSFabiano Rosas         } else {
15298e54ad65SVíctor Colombo             if (FIELD_EX64(env->msr, MSR, POW)) {
15309f338e4dSFabiano Rosas                 cpu_abort(cs, "Trying to deliver power-saving system reset "
15319f338e4dSFabiano Rosas                           "exception %d with no HV support\n", excp);
15329f338e4dSFabiano Rosas             }
15339f338e4dSFabiano Rosas         }
15349f338e4dSFabiano Rosas         break;
15359f338e4dSFabiano Rosas     case POWERPC_EXCP_DSEG:      /* Data segment exception                   */
15369f338e4dSFabiano Rosas     case POWERPC_EXCP_ISEG:      /* Instruction segment exception            */
15379f338e4dSFabiano Rosas     case POWERPC_EXCP_TRACE:     /* Trace exception                          */
15389f338e4dSFabiano Rosas         break;
15399f338e4dSFabiano Rosas     case POWERPC_EXCP_HISI:      /* Hypervisor instruction storage exception */
15409f338e4dSFabiano Rosas         msr |= env->error_code;
15419f338e4dSFabiano Rosas         /* fall through */
15429f338e4dSFabiano Rosas     case POWERPC_EXCP_HDECR:     /* Hypervisor decrementer exception         */
15439f338e4dSFabiano Rosas     case POWERPC_EXCP_HDSI:      /* Hypervisor data storage exception        */
15449f338e4dSFabiano Rosas     case POWERPC_EXCP_SDOOR_HV:  /* Hypervisor Doorbell interrupt            */
15459f338e4dSFabiano Rosas     case POWERPC_EXCP_HV_EMU:
15469f338e4dSFabiano Rosas     case POWERPC_EXCP_HVIRT:     /* Hypervisor virtualization                */
15479f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
15489f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
15499f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
15509f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
15519f338e4dSFabiano Rosas         break;
15529f338e4dSFabiano Rosas     case POWERPC_EXCP_VPU:       /* Vector unavailable exception             */
15539f338e4dSFabiano Rosas     case POWERPC_EXCP_VSXU:       /* VSX unavailable exception               */
15549f338e4dSFabiano Rosas     case POWERPC_EXCP_FU:         /* Facility unavailable exception          */
15559f338e4dSFabiano Rosas         env->spr[SPR_FSCR] |= ((target_ulong)env->error_code << 56);
15569f338e4dSFabiano Rosas         break;
15579f338e4dSFabiano Rosas     case POWERPC_EXCP_HV_FU:     /* Hypervisor Facility Unavailable Exception */
15589f338e4dSFabiano Rosas         env->spr[SPR_HFSCR] |= ((target_ulong)env->error_code << FSCR_IC_POS);
15599f338e4dSFabiano Rosas         srr0 = SPR_HSRR0;
15609f338e4dSFabiano Rosas         srr1 = SPR_HSRR1;
15619f338e4dSFabiano Rosas         new_msr |= (target_ulong)MSR_HVB;
15629f338e4dSFabiano Rosas         new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
15639f338e4dSFabiano Rosas         break;
1564cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_PERFM_EBB:        /* Performance Monitor EBB Exception  */
1565cb76bbc4SDaniel Henrique Barboza     case POWERPC_EXCP_EXTERNAL_EBB:     /* External EBB Exception             */
1566cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
1567cb76bbc4SDaniel Henrique Barboza 
1568cb76bbc4SDaniel Henrique Barboza         /*
1569cb76bbc4SDaniel Henrique Barboza          * Save NIP for rfebb insn in SPR_EBBRR. Next nip is
1570cb76bbc4SDaniel Henrique Barboza          * stored in the EBB Handler SPR_EBBHR.
1571cb76bbc4SDaniel Henrique Barboza          */
1572cb76bbc4SDaniel Henrique Barboza         env->spr[SPR_EBBRR] = env->nip;
1573cb76bbc4SDaniel Henrique Barboza         powerpc_set_excp_state(cpu, env->spr[SPR_EBBHR], env->msr);
1574cb76bbc4SDaniel Henrique Barboza 
1575cb76bbc4SDaniel Henrique Barboza         /*
1576cb76bbc4SDaniel Henrique Barboza          * This exception is handled in userspace. No need to proceed.
1577cb76bbc4SDaniel Henrique Barboza          */
1578cb76bbc4SDaniel Henrique Barboza         return;
15799f338e4dSFabiano Rosas     case POWERPC_EXCP_THERM:     /* Thermal interrupt                        */
15809f338e4dSFabiano Rosas     case POWERPC_EXCP_PERFM:     /* Embedded performance monitor interrupt   */
15819f338e4dSFabiano Rosas     case POWERPC_EXCP_VPUA:      /* Vector assist exception                  */
15829f338e4dSFabiano Rosas     case POWERPC_EXCP_MAINT:     /* Maintenance exception                    */
158330c4e426SFabiano Rosas     case POWERPC_EXCP_SDOOR:     /* Doorbell interrupt                       */
158430c4e426SFabiano Rosas     case POWERPC_EXCP_HV_MAINT:  /* Hypervisor Maintenance exception         */
15859f338e4dSFabiano Rosas         cpu_abort(cs, "%s exception not implemented\n",
15869f338e4dSFabiano Rosas                   powerpc_excp_name(excp));
15879f338e4dSFabiano Rosas         break;
15889f338e4dSFabiano Rosas     default:
15899f338e4dSFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
15909f338e4dSFabiano Rosas         break;
15919f338e4dSFabiano Rosas     }
15929f338e4dSFabiano Rosas 
15939f338e4dSFabiano Rosas     /*
15949f338e4dSFabiano Rosas      * Sort out endianness of interrupt, this differs depending on the
15959f338e4dSFabiano Rosas      * CPU, the HV mode, etc...
15969f338e4dSFabiano Rosas      */
15979f338e4dSFabiano Rosas     if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
15989f338e4dSFabiano Rosas         new_msr |= (target_ulong)1 << MSR_LE;
15999f338e4dSFabiano Rosas     }
16009f338e4dSFabiano Rosas 
16019f338e4dSFabiano Rosas     new_msr |= (target_ulong)1 << MSR_SF;
16029f338e4dSFabiano Rosas 
16039f338e4dSFabiano Rosas     if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
16049f338e4dSFabiano Rosas         /* Save PC */
16059f338e4dSFabiano Rosas         env->spr[srr0] = env->nip;
16069f338e4dSFabiano Rosas 
16079f338e4dSFabiano Rosas         /* Save MSR */
16089f338e4dSFabiano Rosas         env->spr[srr1] = msr;
16099f338e4dSFabiano Rosas     }
16109f338e4dSFabiano Rosas 
16117cebc5dbSNicholas Piggin     if ((new_msr & MSR_HVB) && books_vhyp_handles_hv_excp(cpu)) {
16127cebc5dbSNicholas Piggin         PPCVirtualHypervisorClass *vhc =
16137cebc5dbSNicholas Piggin             PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
16147cebc5dbSNicholas Piggin         /* Deliver interrupt to L1 by returning from the H_ENTER_NESTED call */
16157cebc5dbSNicholas Piggin         vhc->deliver_hv_excp(cpu, excp);
16167cebc5dbSNicholas Piggin 
16177cebc5dbSNicholas Piggin         powerpc_reset_excp_state(cpu);
16187cebc5dbSNicholas Piggin 
16197cebc5dbSNicholas Piggin     } else {
16207cebc5dbSNicholas Piggin         /* Sanity check */
16217cebc5dbSNicholas Piggin         if (!(env->msr_mask & MSR_HVB) && srr0 == SPR_HSRR0) {
16227cebc5dbSNicholas Piggin             cpu_abort(cs, "Trying to deliver HV exception (HSRR) %d with "
16237cebc5dbSNicholas Piggin                       "no HV support\n", excp);
16247cebc5dbSNicholas Piggin         }
16257cebc5dbSNicholas Piggin 
16269f338e4dSFabiano Rosas         /* This can update new_msr and vector if AIL applies */
162710895ab6SFabiano Rosas         ppc_excp_apply_ail(cpu, excp, msr, &new_msr, &vector);
16289f338e4dSFabiano Rosas 
16299f338e4dSFabiano Rosas         powerpc_set_excp_state(cpu, vector, new_msr);
16309f338e4dSFabiano Rosas     }
16317cebc5dbSNicholas Piggin }
163230c4e426SFabiano Rosas #else
163330c4e426SFabiano Rosas static inline void powerpc_excp_books(PowerPCCPU *cpu, int excp)
163430c4e426SFabiano Rosas {
163530c4e426SFabiano Rosas     g_assert_not_reached();
163630c4e426SFabiano Rosas }
163730c4e426SFabiano Rosas #endif
16389f338e4dSFabiano Rosas 
1639dc88dd0aSFabiano Rosas static void powerpc_excp(PowerPCCPU *cpu, int excp)
1640dc88dd0aSFabiano Rosas {
1641c6eaac89SFabiano Rosas     CPUState *cs = CPU(cpu);
1642dc88dd0aSFabiano Rosas     CPUPPCState *env = &cpu->env;
1643dc88dd0aSFabiano Rosas 
1644c6eaac89SFabiano Rosas     if (excp <= POWERPC_EXCP_NONE || excp >= POWERPC_EXCP_NB) {
1645c6eaac89SFabiano Rosas         cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
1646c6eaac89SFabiano Rosas     }
1647c6eaac89SFabiano Rosas 
1648c6eaac89SFabiano Rosas     qemu_log_mask(CPU_LOG_INT, "Raise exception at " TARGET_FMT_lx
1649c6eaac89SFabiano Rosas                   " => %s (%d) error=%02x\n", env->nip, powerpc_excp_name(excp),
1650c6eaac89SFabiano Rosas                   excp, env->error_code);
1651c6eaac89SFabiano Rosas 
1652dc88dd0aSFabiano Rosas     switch (env->excp_model) {
1653e808c2edSFabiano Rosas     case POWERPC_EXCP_40x:
1654e808c2edSFabiano Rosas         powerpc_excp_40x(cpu, excp);
1655e808c2edSFabiano Rosas         break;
165658d178fbSFabiano Rosas     case POWERPC_EXCP_6xx:
165758d178fbSFabiano Rosas         powerpc_excp_6xx(cpu, excp);
165858d178fbSFabiano Rosas         break;
1659ccfca2fcSFabiano Rosas     case POWERPC_EXCP_7xx:
1660ccfca2fcSFabiano Rosas         powerpc_excp_7xx(cpu, excp);
1661ccfca2fcSFabiano Rosas         break;
166252926b0dSFabiano Rosas     case POWERPC_EXCP_74xx:
166352926b0dSFabiano Rosas         powerpc_excp_74xx(cpu, excp);
166452926b0dSFabiano Rosas         break;
1665180952ceSFabiano Rosas     case POWERPC_EXCP_BOOKE:
1666180952ceSFabiano Rosas         powerpc_excp_booke(cpu, excp);
1667180952ceSFabiano Rosas         break;
16689f338e4dSFabiano Rosas     case POWERPC_EXCP_970:
16699f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER7:
16709f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER8:
16719f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER9:
16729f338e4dSFabiano Rosas     case POWERPC_EXCP_POWER10:
16739f338e4dSFabiano Rosas         powerpc_excp_books(cpu, excp);
16749f338e4dSFabiano Rosas         break;
1675dc88dd0aSFabiano Rosas     default:
167628091374SFabiano Rosas         g_assert_not_reached();
1677dc88dd0aSFabiano Rosas     }
1678dc88dd0aSFabiano Rosas }
1679dc88dd0aSFabiano Rosas 
168097a8ea5aSAndreas Färber void ppc_cpu_do_interrupt(CPUState *cs)
1681c79c73f6SBlue Swirl {
168297a8ea5aSAndreas Färber     PowerPCCPU *cpu = POWERPC_CPU(cs);
16835c26a5b3SAndreas Färber 
168493130c84SFabiano Rosas     powerpc_excp(cpu, cs->exception_index);
1685c79c73f6SBlue Swirl }
1686c79c73f6SBlue Swirl 
16872dfecf01SMatheus Ferst #if defined(TARGET_PPC64)
1688*bf303fb3SMatheus Ferst static int p7_next_unmasked_interrupt(CPUPPCState *env)
1689*bf303fb3SMatheus Ferst {
1690*bf303fb3SMatheus Ferst     bool async_deliver;
1691*bf303fb3SMatheus Ferst 
1692*bf303fb3SMatheus Ferst     /* External reset */
1693*bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
1694*bf303fb3SMatheus Ferst         return PPC_INTERRUPT_RESET;
1695*bf303fb3SMatheus Ferst     }
1696*bf303fb3SMatheus Ferst     /* Machine check exception */
1697*bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1698*bf303fb3SMatheus Ferst         return PPC_INTERRUPT_MCK;
1699*bf303fb3SMatheus Ferst     }
1700*bf303fb3SMatheus Ferst 
1701*bf303fb3SMatheus Ferst     /*
1702*bf303fb3SMatheus Ferst      * For interrupts that gate on MSR:EE, we need to do something a
1703*bf303fb3SMatheus Ferst      * bit more subtle, as we need to let them through even when EE is
1704*bf303fb3SMatheus Ferst      * clear when coming out of some power management states (in order
1705*bf303fb3SMatheus Ferst      * for them to become a 0x100).
1706*bf303fb3SMatheus Ferst      */
1707*bf303fb3SMatheus Ferst     async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1708*bf303fb3SMatheus Ferst 
1709*bf303fb3SMatheus Ferst     /* Hypervisor decrementer exception */
1710*bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1711*bf303fb3SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1712*bf303fb3SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
1713*bf303fb3SMatheus Ferst         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
1714*bf303fb3SMatheus Ferst             /* HDEC clears on delivery */
1715*bf303fb3SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1716*bf303fb3SMatheus Ferst         }
1717*bf303fb3SMatheus Ferst     }
1718*bf303fb3SMatheus Ferst 
1719*bf303fb3SMatheus Ferst     /* Hypervisor virtualization interrupt */
1720*bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
1721*bf303fb3SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1722*bf303fb3SMatheus Ferst         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
1723*bf303fb3SMatheus Ferst         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
1724*bf303fb3SMatheus Ferst             return PPC_INTERRUPT_HVIRT;
1725*bf303fb3SMatheus Ferst         }
1726*bf303fb3SMatheus Ferst     }
1727*bf303fb3SMatheus Ferst 
1728*bf303fb3SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1729*bf303fb3SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1730*bf303fb3SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1731*bf303fb3SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1732*bf303fb3SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
1733*bf303fb3SMatheus Ferst         if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
1734*bf303fb3SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1735*bf303fb3SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1736*bf303fb3SMatheus Ferst             return PPC_INTERRUPT_EXT;
1737*bf303fb3SMatheus Ferst         }
1738*bf303fb3SMatheus Ferst     }
1739*bf303fb3SMatheus Ferst     if (FIELD_EX64(env->msr, MSR, CE)) {
1740*bf303fb3SMatheus Ferst         /* External critical interrupt */
1741*bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CEXT) {
1742*bf303fb3SMatheus Ferst             return PPC_INTERRUPT_CEXT;
1743*bf303fb3SMatheus Ferst         }
1744*bf303fb3SMatheus Ferst     }
1745*bf303fb3SMatheus Ferst     if (async_deliver != 0) {
1746*bf303fb3SMatheus Ferst         /* Watchdog timer on embedded PowerPC */
1747*bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_WDT) {
1748*bf303fb3SMatheus Ferst             return PPC_INTERRUPT_WDT;
1749*bf303fb3SMatheus Ferst         }
1750*bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) {
1751*bf303fb3SMatheus Ferst             return PPC_INTERRUPT_CDOORBELL;
1752*bf303fb3SMatheus Ferst         }
1753*bf303fb3SMatheus Ferst         /* Fixed interval timer on embedded PowerPC */
1754*bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_FIT) {
1755*bf303fb3SMatheus Ferst             return PPC_INTERRUPT_FIT;
1756*bf303fb3SMatheus Ferst         }
1757*bf303fb3SMatheus Ferst         /* Programmable interval timer on embedded PowerPC */
1758*bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PIT) {
1759*bf303fb3SMatheus Ferst             return PPC_INTERRUPT_PIT;
1760*bf303fb3SMatheus Ferst         }
1761*bf303fb3SMatheus Ferst         /* Decrementer exception */
1762*bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1763*bf303fb3SMatheus Ferst             return PPC_INTERRUPT_DECR;
1764*bf303fb3SMatheus Ferst         }
1765*bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1766*bf303fb3SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
1767*bf303fb3SMatheus Ferst         }
1768*bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
1769*bf303fb3SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
1770*bf303fb3SMatheus Ferst         }
1771*bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1772*bf303fb3SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1773*bf303fb3SMatheus Ferst         }
1774*bf303fb3SMatheus Ferst         /* Thermal interrupt */
1775*bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_THERM) {
1776*bf303fb3SMatheus Ferst             return PPC_INTERRUPT_THERM;
1777*bf303fb3SMatheus Ferst         }
1778*bf303fb3SMatheus Ferst         /* EBB exception */
1779*bf303fb3SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
1780*bf303fb3SMatheus Ferst             /*
1781*bf303fb3SMatheus Ferst              * EBB exception must be taken in problem state and
1782*bf303fb3SMatheus Ferst              * with BESCR_GE set.
1783*bf303fb3SMatheus Ferst              */
1784*bf303fb3SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
1785*bf303fb3SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
1786*bf303fb3SMatheus Ferst                 return PPC_INTERRUPT_EBB;
1787*bf303fb3SMatheus Ferst             }
1788*bf303fb3SMatheus Ferst         }
1789*bf303fb3SMatheus Ferst     }
1790*bf303fb3SMatheus Ferst 
1791*bf303fb3SMatheus Ferst     return 0;
1792*bf303fb3SMatheus Ferst }
1793*bf303fb3SMatheus Ferst 
1794f6194fddSMatheus Ferst #define P8_UNUSED_INTERRUPTS \
1795f6194fddSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_HVIRT |  \
1796f6194fddSMatheus Ferst     PPC_INTERRUPT_CEXT | PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL |  \
1797f6194fddSMatheus Ferst     PPC_INTERRUPT_FIT | PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1798f6194fddSMatheus Ferst 
1799a9899d42SMatheus Ferst static int p8_next_unmasked_interrupt(CPUPPCState *env)
1800a9899d42SMatheus Ferst {
180164a9b5eeSMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
180264a9b5eeSMatheus Ferst     CPUState *cs = CPU(cpu);
180364a9b5eeSMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
180464a9b5eeSMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1805a9899d42SMatheus Ferst 
1806f6194fddSMatheus Ferst     assert((env->pending_interrupts & P8_UNUSED_INTERRUPTS) == 0);
1807f6194fddSMatheus Ferst 
180864a9b5eeSMatheus Ferst     if (cs->halted) {
180964a9b5eeSMatheus Ferst         /* LPCR[PECE] controls which interrupts can exit power-saving mode */
181064a9b5eeSMatheus Ferst         return p8_interrupt_powersave(env);
181164a9b5eeSMatheus Ferst     }
181264a9b5eeSMatheus Ferst 
1813a9899d42SMatheus Ferst     /* Machine check exception */
1814a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1815a9899d42SMatheus Ferst         return PPC_INTERRUPT_MCK;
1816a9899d42SMatheus Ferst     }
1817a9899d42SMatheus Ferst 
1818a9899d42SMatheus Ferst     /* Hypervisor decrementer exception */
1819a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1820a9899d42SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1821a9899d42SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
182264a9b5eeSMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1823a9899d42SMatheus Ferst             /* HDEC clears on delivery */
1824a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1825a9899d42SMatheus Ferst         }
1826a9899d42SMatheus Ferst     }
1827a9899d42SMatheus Ferst 
1828a9899d42SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1829a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1830a9899d42SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1831a9899d42SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1832a9899d42SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
183364a9b5eeSMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1834a9899d42SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1835a9899d42SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1836a9899d42SMatheus Ferst             return PPC_INTERRUPT_EXT;
1837a9899d42SMatheus Ferst         }
1838a9899d42SMatheus Ferst     }
183964a9b5eeSMatheus Ferst     if (msr_ee != 0) {
1840a9899d42SMatheus Ferst         /* Decrementer exception */
1841a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1842a9899d42SMatheus Ferst             return PPC_INTERRUPT_DECR;
1843a9899d42SMatheus Ferst         }
1844a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1845a9899d42SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
1846a9899d42SMatheus Ferst         }
1847a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
1848a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
1849a9899d42SMatheus Ferst         }
1850a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1851a9899d42SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1852a9899d42SMatheus Ferst         }
1853a9899d42SMatheus Ferst         /* EBB exception */
1854a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
1855a9899d42SMatheus Ferst             /*
1856a9899d42SMatheus Ferst              * EBB exception must be taken in problem state and
1857a9899d42SMatheus Ferst              * with BESCR_GE set.
1858a9899d42SMatheus Ferst              */
1859a9899d42SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
1860a9899d42SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
1861a9899d42SMatheus Ferst                 return PPC_INTERRUPT_EBB;
1862a9899d42SMatheus Ferst             }
1863a9899d42SMatheus Ferst         }
1864a9899d42SMatheus Ferst     }
1865a9899d42SMatheus Ferst 
1866a9899d42SMatheus Ferst     return 0;
1867a9899d42SMatheus Ferst }
1868a9899d42SMatheus Ferst 
1869b00e9a2fSMatheus Ferst #define P9_UNUSED_INTERRUPTS \
1870b00e9a2fSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT |   \
1871b00e9a2fSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |  \
1872b00e9a2fSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1873b00e9a2fSMatheus Ferst 
18742dfecf01SMatheus Ferst static int p9_next_unmasked_interrupt(CPUPPCState *env)
18752dfecf01SMatheus Ferst {
187627796411SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
187727796411SMatheus Ferst     CPUState *cs = CPU(cpu);
187827796411SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
187927796411SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
18802dfecf01SMatheus Ferst 
1881b00e9a2fSMatheus Ferst     assert((env->pending_interrupts & P9_UNUSED_INTERRUPTS) == 0);
1882b00e9a2fSMatheus Ferst 
188327796411SMatheus Ferst     if (cs->halted) {
188427796411SMatheus Ferst         if (env->spr[SPR_PSSCR] & PSSCR_EC) {
188527796411SMatheus Ferst             /*
188627796411SMatheus Ferst              * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can
188727796411SMatheus Ferst              * wakeup the processor
188827796411SMatheus Ferst              */
188927796411SMatheus Ferst             return p9_interrupt_powersave(env);
189027796411SMatheus Ferst         } else {
189127796411SMatheus Ferst             /*
189227796411SMatheus Ferst              * When it's clear, any system-caused exception exits power-saving
189327796411SMatheus Ferst              * mode, even the ones that gate on MSR[EE].
189427796411SMatheus Ferst              */
189527796411SMatheus Ferst             msr_ee = true;
189627796411SMatheus Ferst         }
189727796411SMatheus Ferst     }
189827796411SMatheus Ferst 
18992dfecf01SMatheus Ferst     /* Machine check exception */
19002dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
19012dfecf01SMatheus Ferst         return PPC_INTERRUPT_MCK;
19022dfecf01SMatheus Ferst     }
19032dfecf01SMatheus Ferst 
19042dfecf01SMatheus Ferst     /* Hypervisor decrementer exception */
19052dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
19062dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
19072dfecf01SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
190827796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
19092dfecf01SMatheus Ferst             /* HDEC clears on delivery */
19102dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDECR;
19112dfecf01SMatheus Ferst         }
19122dfecf01SMatheus Ferst     }
19132dfecf01SMatheus Ferst 
19142dfecf01SMatheus Ferst     /* Hypervisor virtualization interrupt */
19152dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
19162dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
19172dfecf01SMatheus Ferst         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
191827796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hvice) {
19192dfecf01SMatheus Ferst             return PPC_INTERRUPT_HVIRT;
19202dfecf01SMatheus Ferst         }
19212dfecf01SMatheus Ferst     }
19222dfecf01SMatheus Ferst 
19232dfecf01SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
19242dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
19252dfecf01SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
19262dfecf01SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
19272dfecf01SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
192827796411SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
19292dfecf01SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
19302dfecf01SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
19312dfecf01SMatheus Ferst             return PPC_INTERRUPT_EXT;
19322dfecf01SMatheus Ferst         }
19332dfecf01SMatheus Ferst     }
193427796411SMatheus Ferst     if (msr_ee != 0) {
19352dfecf01SMatheus Ferst         /* Decrementer exception */
19362dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
19372dfecf01SMatheus Ferst             return PPC_INTERRUPT_DECR;
19382dfecf01SMatheus Ferst         }
19392dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
19402dfecf01SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
19412dfecf01SMatheus Ferst         }
19422dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
19432dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
19442dfecf01SMatheus Ferst         }
19452dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
19462dfecf01SMatheus Ferst             return PPC_INTERRUPT_PERFM;
19472dfecf01SMatheus Ferst         }
19482dfecf01SMatheus Ferst         /* EBB exception */
19492dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
19502dfecf01SMatheus Ferst             /*
19512dfecf01SMatheus Ferst              * EBB exception must be taken in problem state and
19522dfecf01SMatheus Ferst              * with BESCR_GE set.
19532dfecf01SMatheus Ferst              */
19542dfecf01SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
19552dfecf01SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
19562dfecf01SMatheus Ferst                 return PPC_INTERRUPT_EBB;
19572dfecf01SMatheus Ferst             }
19582dfecf01SMatheus Ferst         }
19592dfecf01SMatheus Ferst     }
19602dfecf01SMatheus Ferst 
19612dfecf01SMatheus Ferst     return 0;
19622dfecf01SMatheus Ferst }
19632dfecf01SMatheus Ferst #endif
19642dfecf01SMatheus Ferst 
1965ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt_generic(CPUPPCState *env)
1966c79c73f6SBlue Swirl {
19673621e2c9SBenjamin Herrenschmidt     bool async_deliver;
1968259186a7SAndreas Färber 
1969c79c73f6SBlue Swirl     /* External reset */
1970f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
1971de76b85cSMatheus Ferst         return PPC_INTERRUPT_RESET;
1972c79c73f6SBlue Swirl     }
1973c79c73f6SBlue Swirl     /* Machine check exception */
1974f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1975de76b85cSMatheus Ferst         return PPC_INTERRUPT_MCK;
1976c79c73f6SBlue Swirl     }
1977c79c73f6SBlue Swirl #if 0 /* TODO */
1978c79c73f6SBlue Swirl     /* External debug exception */
1979f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) {
1980de76b85cSMatheus Ferst         return PPC_INTERRUPT_DEBUG;
1981c79c73f6SBlue Swirl     }
1982c79c73f6SBlue Swirl #endif
19833621e2c9SBenjamin Herrenschmidt 
19843621e2c9SBenjamin Herrenschmidt     /*
19853621e2c9SBenjamin Herrenschmidt      * For interrupts that gate on MSR:EE, we need to do something a
19863621e2c9SBenjamin Herrenschmidt      * bit more subtle, as we need to let them through even when EE is
19873621e2c9SBenjamin Herrenschmidt      * clear when coming out of some power management states (in order
19883621e2c9SBenjamin Herrenschmidt      * for them to become a 0x100).
19893621e2c9SBenjamin Herrenschmidt      */
19900939b8f8SVíctor Colombo     async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
19913621e2c9SBenjamin Herrenschmidt 
1992c79c73f6SBlue Swirl     /* Hypervisor decrementer exception */
1993f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
19944b236b62SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
19954b236b62SBenjamin Herrenschmidt         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
19969de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
19974b236b62SBenjamin Herrenschmidt             /* HDEC clears on delivery */
1998de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDECR;
1999c79c73f6SBlue Swirl         }
2000c79c73f6SBlue Swirl     }
2001d8ce5fd6SBenjamin Herrenschmidt 
2002d8ce5fd6SBenjamin Herrenschmidt     /* Hypervisor virtualization interrupt */
2003f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
2004d8ce5fd6SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
2005d8ce5fd6SBenjamin Herrenschmidt         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
20069de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
2007de76b85cSMatheus Ferst             return PPC_INTERRUPT_HVIRT;
2008d8ce5fd6SBenjamin Herrenschmidt         }
2009d8ce5fd6SBenjamin Herrenschmidt     }
2010d8ce5fd6SBenjamin Herrenschmidt 
2011d8ce5fd6SBenjamin Herrenschmidt     /* External interrupt can ignore MSR:EE under some circumstances */
2012f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
2013d1dbe37cSBenjamin Herrenschmidt         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
20146eebe6dcSBenjamin Herrenschmidt         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
20156eebe6dcSBenjamin Herrenschmidt         /* HEIC blocks delivery to the hypervisor */
20169de754d3SVíctor Colombo         if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
2017d41ccf6eSVíctor Colombo             !FIELD_EX64(env->msr, MSR, PR))) ||
20189de754d3SVíctor Colombo             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
2019de76b85cSMatheus Ferst             return PPC_INTERRUPT_EXT;
2020d1dbe37cSBenjamin Herrenschmidt         }
2021d1dbe37cSBenjamin Herrenschmidt     }
2022acc861c2SVíctor Colombo     if (FIELD_EX64(env->msr, MSR, CE)) {
2023c79c73f6SBlue Swirl         /* External critical interrupt */
2024f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CEXT) {
2025de76b85cSMatheus Ferst             return PPC_INTERRUPT_CEXT;
2026c79c73f6SBlue Swirl         }
2027c79c73f6SBlue Swirl     }
20283621e2c9SBenjamin Herrenschmidt     if (async_deliver != 0) {
2029c79c73f6SBlue Swirl         /* Watchdog timer on embedded PowerPC */
2030f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_WDT) {
2031de76b85cSMatheus Ferst             return PPC_INTERRUPT_WDT;
2032c79c73f6SBlue Swirl         }
2033f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) {
2034de76b85cSMatheus Ferst             return PPC_INTERRUPT_CDOORBELL;
2035c79c73f6SBlue Swirl         }
2036c79c73f6SBlue Swirl         /* Fixed interval timer on embedded PowerPC */
2037f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_FIT) {
2038de76b85cSMatheus Ferst             return PPC_INTERRUPT_FIT;
2039c79c73f6SBlue Swirl         }
2040c79c73f6SBlue Swirl         /* Programmable interval timer on embedded PowerPC */
2041f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PIT) {
2042de76b85cSMatheus Ferst             return PPC_INTERRUPT_PIT;
2043c79c73f6SBlue Swirl         }
2044c79c73f6SBlue Swirl         /* Decrementer exception */
2045f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
2046de76b85cSMatheus Ferst             return PPC_INTERRUPT_DECR;
2047c79c73f6SBlue Swirl         }
2048f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2049de76b85cSMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
2050c79c73f6SBlue Swirl         }
2051f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
2052de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
20537af1e7b0SCédric Le Goater         }
2054f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
2055de76b85cSMatheus Ferst             return PPC_INTERRUPT_PERFM;
2056c79c73f6SBlue Swirl         }
2057c79c73f6SBlue Swirl         /* Thermal interrupt */
2058f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_THERM) {
2059de76b85cSMatheus Ferst             return PPC_INTERRUPT_THERM;
2060c79c73f6SBlue Swirl         }
2061cb76bbc4SDaniel Henrique Barboza         /* EBB exception */
2062f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
2063cb76bbc4SDaniel Henrique Barboza             /*
2064cb76bbc4SDaniel Henrique Barboza              * EBB exception must be taken in problem state and
2065cb76bbc4SDaniel Henrique Barboza              * with BESCR_GE set.
2066cb76bbc4SDaniel Henrique Barboza              */
2067d41ccf6eSVíctor Colombo             if (FIELD_EX64(env->msr, MSR, PR) &&
2068d41ccf6eSVíctor Colombo                 (env->spr[SPR_BESCR] & BESCR_GE)) {
2069de76b85cSMatheus Ferst                 return PPC_INTERRUPT_EBB;
2070de76b85cSMatheus Ferst             }
2071de76b85cSMatheus Ferst         }
2072de76b85cSMatheus Ferst     }
2073cb76bbc4SDaniel Henrique Barboza 
2074de76b85cSMatheus Ferst     return 0;
2075de76b85cSMatheus Ferst }
2076de76b85cSMatheus Ferst 
2077ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt(CPUPPCState *env)
2078ba2898f7SMatheus Ferst {
2079ba2898f7SMatheus Ferst     switch (env->excp_model) {
20802dfecf01SMatheus Ferst #if defined(TARGET_PPC64)
2081*bf303fb3SMatheus Ferst     case POWERPC_EXCP_POWER7:
2082*bf303fb3SMatheus Ferst         return p7_next_unmasked_interrupt(env);
2083a9899d42SMatheus Ferst     case POWERPC_EXCP_POWER8:
2084a9899d42SMatheus Ferst         return p8_next_unmasked_interrupt(env);
20852dfecf01SMatheus Ferst     case POWERPC_EXCP_POWER9:
20862dfecf01SMatheus Ferst     case POWERPC_EXCP_POWER10:
20872dfecf01SMatheus Ferst         return p9_next_unmasked_interrupt(env);
20882dfecf01SMatheus Ferst #endif
2089ba2898f7SMatheus Ferst     default:
2090ba2898f7SMatheus Ferst         return ppc_next_unmasked_interrupt_generic(env);
2091ba2898f7SMatheus Ferst     }
2092ba2898f7SMatheus Ferst }
2093ba2898f7SMatheus Ferst 
20943654e238SMatheus Ferst #if defined(TARGET_PPC64)
20956527e757SMatheus Ferst static void p8_deliver_interrupt(CPUPPCState *env, int interrupt)
20966527e757SMatheus Ferst {
20976527e757SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
20986527e757SMatheus Ferst     CPUState *cs = env_cpu(env);
20996527e757SMatheus Ferst 
21006527e757SMatheus Ferst     switch (interrupt) {
21016527e757SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
21026527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
21036527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
21046527e757SMatheus Ferst         break;
21056527e757SMatheus Ferst 
21066527e757SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
21076527e757SMatheus Ferst         /* HDEC clears on delivery */
21086527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
21096527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
21106527e757SMatheus Ferst         break;
21116527e757SMatheus Ferst 
21126527e757SMatheus Ferst     case PPC_INTERRUPT_EXT:
21136527e757SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
21146527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
21156527e757SMatheus Ferst         } else {
21166527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
21176527e757SMatheus Ferst         }
21186527e757SMatheus Ferst         break;
21196527e757SMatheus Ferst 
21206527e757SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
21216527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
21226527e757SMatheus Ferst         break;
21236527e757SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
21246527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
21256527e757SMatheus Ferst         if (is_book3s_arch2x(env)) {
21266527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
21276527e757SMatheus Ferst         } else {
21286527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
21296527e757SMatheus Ferst         }
21306527e757SMatheus Ferst         break;
21316527e757SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
21326527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
21336527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
21346527e757SMatheus Ferst         break;
21356527e757SMatheus Ferst     case PPC_INTERRUPT_PERFM:
21366527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
21376527e757SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
21386527e757SMatheus Ferst         break;
21396527e757SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
21406527e757SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
21416527e757SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
21426527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
21436527e757SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
21446527e757SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
21456527e757SMatheus Ferst         }
21466527e757SMatheus Ferst         break;
21476527e757SMatheus Ferst     case 0:
21486527e757SMatheus Ferst         /*
21496527e757SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
21506527e757SMatheus Ferst          * anything to deliver while in a PM state that requires getting
21516527e757SMatheus Ferst          * out via a 0x100
21526527e757SMatheus Ferst          *
21536527e757SMatheus Ferst          * This means we will incorrectly execute past the power management
21546527e757SMatheus Ferst          * instruction instead of triggering a reset.
21556527e757SMatheus Ferst          *
21566527e757SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
21576527e757SMatheus Ferst          * processor has_work implementation and the logic in this function.
21586527e757SMatheus Ferst          */
21596527e757SMatheus Ferst         assert(!env->resume_as_sreset);
21606527e757SMatheus Ferst         break;
21616527e757SMatheus Ferst     default:
21626527e757SMatheus Ferst         cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
21636527e757SMatheus Ferst     }
21646527e757SMatheus Ferst }
21656527e757SMatheus Ferst 
21663654e238SMatheus Ferst static void p9_deliver_interrupt(CPUPPCState *env, int interrupt)
21673654e238SMatheus Ferst {
21683654e238SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
21693654e238SMatheus Ferst     CPUState *cs = env_cpu(env);
21703654e238SMatheus Ferst 
217127796411SMatheus Ferst     if (cs->halted && !(env->spr[SPR_PSSCR] & PSSCR_EC) &&
217227796411SMatheus Ferst         !FIELD_EX64(env->msr, MSR, EE)) {
217327796411SMatheus Ferst         /*
217427796411SMatheus Ferst          * A pending interrupt took us out of power-saving, but MSR[EE] says
217527796411SMatheus Ferst          * that we should return to NIP+4 instead of delivering it.
217627796411SMatheus Ferst          */
217727796411SMatheus Ferst         return;
217827796411SMatheus Ferst     }
217927796411SMatheus Ferst 
21803654e238SMatheus Ferst     switch (interrupt) {
21813654e238SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
21823654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
21833654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
21843654e238SMatheus Ferst         break;
21853654e238SMatheus Ferst 
21863654e238SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
21873654e238SMatheus Ferst         /* HDEC clears on delivery */
21883654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
21893654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
21903654e238SMatheus Ferst         break;
21913654e238SMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
21923654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
21933654e238SMatheus Ferst         break;
21943654e238SMatheus Ferst 
21953654e238SMatheus Ferst     case PPC_INTERRUPT_EXT:
21963654e238SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
21973654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
21983654e238SMatheus Ferst         } else {
21993654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
22003654e238SMatheus Ferst         }
22013654e238SMatheus Ferst         break;
22023654e238SMatheus Ferst 
22033654e238SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
22043654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
22053654e238SMatheus Ferst         break;
22063654e238SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
22073654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
22083654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
22093654e238SMatheus Ferst         break;
22103654e238SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
22113654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
22123654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
22133654e238SMatheus Ferst         break;
22143654e238SMatheus Ferst     case PPC_INTERRUPT_PERFM:
22153654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
22163654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
22173654e238SMatheus Ferst         break;
22183654e238SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
22193654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
22203654e238SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
22213654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
22223654e238SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
22233654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
22243654e238SMatheus Ferst         }
22253654e238SMatheus Ferst         break;
22263654e238SMatheus Ferst     case 0:
22273654e238SMatheus Ferst         /*
22283654e238SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
22293654e238SMatheus Ferst          * anything to deliver while in a PM state that requires getting
22303654e238SMatheus Ferst          * out via a 0x100
22313654e238SMatheus Ferst          *
22323654e238SMatheus Ferst          * This means we will incorrectly execute past the power management
22333654e238SMatheus Ferst          * instruction instead of triggering a reset.
22343654e238SMatheus Ferst          *
22353654e238SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
22363654e238SMatheus Ferst          * processor has_work implementation and the logic in this function.
22373654e238SMatheus Ferst          */
22383654e238SMatheus Ferst         assert(!env->resume_as_sreset);
22393654e238SMatheus Ferst         break;
22403654e238SMatheus Ferst     default:
22413654e238SMatheus Ferst         cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
22423654e238SMatheus Ferst     }
22433654e238SMatheus Ferst }
22443654e238SMatheus Ferst #endif
22453654e238SMatheus Ferst 
2246ba2898f7SMatheus Ferst static void ppc_deliver_interrupt_generic(CPUPPCState *env, int interrupt)
2247de76b85cSMatheus Ferst {
2248de76b85cSMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2249de76b85cSMatheus Ferst     CPUState *cs = env_cpu(env);
2250de76b85cSMatheus Ferst 
2251de76b85cSMatheus Ferst     switch (interrupt) {
2252de76b85cSMatheus Ferst     case PPC_INTERRUPT_RESET: /* External reset */
2253de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_RESET;
2254de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_RESET);
2255de76b85cSMatheus Ferst         break;
2256de76b85cSMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2257de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2258de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2259de76b85cSMatheus Ferst         break;
2260de76b85cSMatheus Ferst 
2261de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2262de76b85cSMatheus Ferst         /* HDEC clears on delivery */
2263de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2264de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2265de76b85cSMatheus Ferst         break;
2266de76b85cSMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
2267de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2268de76b85cSMatheus Ferst         break;
2269de76b85cSMatheus Ferst 
2270de76b85cSMatheus Ferst     case PPC_INTERRUPT_EXT:
2271de76b85cSMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2272de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2273de76b85cSMatheus Ferst         } else {
2274de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2275de76b85cSMatheus Ferst         }
2276de76b85cSMatheus Ferst         break;
2277de76b85cSMatheus Ferst     case PPC_INTERRUPT_CEXT: /* External critical interrupt */
2278de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_CRITICAL);
2279de76b85cSMatheus Ferst         break;
2280de76b85cSMatheus Ferst 
2281de76b85cSMatheus Ferst     case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */
2282de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_WDT;
2283de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_WDT);
2284de76b85cSMatheus Ferst         break;
2285de76b85cSMatheus Ferst     case PPC_INTERRUPT_CDOORBELL:
2286de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL;
2287de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DOORCI);
2288de76b85cSMatheus Ferst         break;
2289de76b85cSMatheus Ferst     case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */
2290de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_FIT;
2291de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_FIT);
2292de76b85cSMatheus Ferst         break;
2293de76b85cSMatheus Ferst     case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */
2294de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PIT;
2295de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PIT);
2296de76b85cSMatheus Ferst         break;
2297de76b85cSMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2298de76b85cSMatheus Ferst         if (ppc_decr_clear_on_delivery(env)) {
2299de76b85cSMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DECR;
2300de76b85cSMatheus Ferst         }
2301de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2302de76b85cSMatheus Ferst         break;
2303de76b85cSMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
2304de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
2305de76b85cSMatheus Ferst         if (is_book3s_arch2x(env)) {
2306de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
2307de76b85cSMatheus Ferst         } else {
2308de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
2309de76b85cSMatheus Ferst         }
2310de76b85cSMatheus Ferst         break;
2311de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
2312de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
2313de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
2314de76b85cSMatheus Ferst         break;
2315de76b85cSMatheus Ferst     case PPC_INTERRUPT_PERFM:
2316de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2317de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2318de76b85cSMatheus Ferst         break;
2319de76b85cSMatheus Ferst     case PPC_INTERRUPT_THERM:  /* Thermal interrupt */
2320de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_THERM;
2321de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_THERM);
2322de76b85cSMatheus Ferst         break;
2323de76b85cSMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
2324de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
2325cb76bbc4SDaniel Henrique Barboza         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
2326cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
2327cb76bbc4SDaniel Henrique Barboza         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
2328cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
2329cb76bbc4SDaniel Henrique Barboza         }
2330de76b85cSMatheus Ferst         break;
2331de76b85cSMatheus Ferst     case 0:
2332f8154fd2SBenjamin Herrenschmidt         /*
2333f8154fd2SBenjamin Herrenschmidt          * This is a bug ! It means that has_work took us out of halt without
2334f8154fd2SBenjamin Herrenschmidt          * anything to deliver while in a PM state that requires getting
2335f8154fd2SBenjamin Herrenschmidt          * out via a 0x100
2336f8154fd2SBenjamin Herrenschmidt          *
2337f8154fd2SBenjamin Herrenschmidt          * This means we will incorrectly execute past the power management
2338f8154fd2SBenjamin Herrenschmidt          * instruction instead of triggering a reset.
2339f8154fd2SBenjamin Herrenschmidt          *
2340136fbf65Szhaolichang          * It generally means a discrepancy between the wakeup conditions in the
2341f8154fd2SBenjamin Herrenschmidt          * processor has_work implementation and the logic in this function.
2342f8154fd2SBenjamin Herrenschmidt          */
2343de76b85cSMatheus Ferst         assert(!env->resume_as_sreset);
2344de76b85cSMatheus Ferst         break;
2345de76b85cSMatheus Ferst     default:
2346de76b85cSMatheus Ferst         cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
2347f8154fd2SBenjamin Herrenschmidt     }
2348c79c73f6SBlue Swirl }
234934316482SAlexey Kardashevskiy 
2350ba2898f7SMatheus Ferst static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt)
2351ba2898f7SMatheus Ferst {
2352ba2898f7SMatheus Ferst     switch (env->excp_model) {
23533654e238SMatheus Ferst #if defined(TARGET_PPC64)
23546527e757SMatheus Ferst     case POWERPC_EXCP_POWER8:
23556527e757SMatheus Ferst         p8_deliver_interrupt(env, interrupt);
23566527e757SMatheus Ferst         break;
23573654e238SMatheus Ferst     case POWERPC_EXCP_POWER9:
23583654e238SMatheus Ferst     case POWERPC_EXCP_POWER10:
23593654e238SMatheus Ferst         p9_deliver_interrupt(env, interrupt);
23603654e238SMatheus Ferst         break;
23613654e238SMatheus Ferst #endif
2362ba2898f7SMatheus Ferst     default:
2363ba2898f7SMatheus Ferst         ppc_deliver_interrupt_generic(env, interrupt);
2364ba2898f7SMatheus Ferst     }
2365ba2898f7SMatheus Ferst }
2366ba2898f7SMatheus Ferst 
2367b5b7f391SNicholas Piggin void ppc_cpu_do_system_reset(CPUState *cs)
236834316482SAlexey Kardashevskiy {
236934316482SAlexey Kardashevskiy     PowerPCCPU *cpu = POWERPC_CPU(cs);
237034316482SAlexey Kardashevskiy 
237193130c84SFabiano Rosas     powerpc_excp(cpu, POWERPC_EXCP_RESET);
237234316482SAlexey Kardashevskiy }
2373ad77c6caSNicholas Piggin 
2374ad77c6caSNicholas Piggin void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector)
2375ad77c6caSNicholas Piggin {
2376ad77c6caSNicholas Piggin     PowerPCCPU *cpu = POWERPC_CPU(cs);
2377ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
2378ad77c6caSNicholas Piggin     target_ulong msr = 0;
2379ad77c6caSNicholas Piggin 
2380ad77c6caSNicholas Piggin     /*
2381ad77c6caSNicholas Piggin      * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already
2382ad77c6caSNicholas Piggin      * been set by KVM.
2383ad77c6caSNicholas Piggin      */
2384ad77c6caSNicholas Piggin     msr = (1ULL << MSR_ME);
2385ad77c6caSNicholas Piggin     msr |= env->msr & (1ULL << MSR_SF);
2386516fc103SFabiano Rosas     if (ppc_interrupts_little_endian(cpu, false)) {
2387ad77c6caSNicholas Piggin         msr |= (1ULL << MSR_LE);
2388ad77c6caSNicholas Piggin     }
2389ad77c6caSNicholas Piggin 
23907cebc5dbSNicholas Piggin     /* Anything for nested required here? MSR[HV] bit? */
23917cebc5dbSNicholas Piggin 
2392ad77c6caSNicholas Piggin     powerpc_set_excp_state(cpu, vector, msr);
2393ad77c6caSNicholas Piggin }
2394c79c73f6SBlue Swirl 
2395458dd766SRichard Henderson bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
2396458dd766SRichard Henderson {
2397458dd766SRichard Henderson     PowerPCCPU *cpu = POWERPC_CPU(cs);
2398458dd766SRichard Henderson     CPUPPCState *env = &cpu->env;
2399de76b85cSMatheus Ferst     int interrupt;
2400458dd766SRichard Henderson 
2401de76b85cSMatheus Ferst     if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) {
2402de76b85cSMatheus Ferst         return false;
2403de76b85cSMatheus Ferst     }
2404de76b85cSMatheus Ferst 
2405de76b85cSMatheus Ferst     interrupt = ppc_next_unmasked_interrupt(env);
2406de76b85cSMatheus Ferst     if (interrupt == 0) {
2407de76b85cSMatheus Ferst         return false;
2408de76b85cSMatheus Ferst     }
2409de76b85cSMatheus Ferst 
2410de76b85cSMatheus Ferst     ppc_deliver_interrupt(env, interrupt);
2411458dd766SRichard Henderson     if (env->pending_interrupts == 0) {
2412de76b85cSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
2413458dd766SRichard Henderson     }
2414458dd766SRichard Henderson     return true;
2415458dd766SRichard Henderson }
2416458dd766SRichard Henderson 
2417f725245cSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
2418f725245cSPhilippe Mathieu-Daudé 
2419ad71ed68SBlue Swirl /*****************************************************************************/
2420ad71ed68SBlue Swirl /* Exceptions processing helpers */
2421ad71ed68SBlue Swirl 
2422db789c6cSBenjamin Herrenschmidt void raise_exception_err_ra(CPUPPCState *env, uint32_t exception,
2423db789c6cSBenjamin Herrenschmidt                             uint32_t error_code, uintptr_t raddr)
2424ad71ed68SBlue Swirl {
2425db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
242627103424SAndreas Färber 
242727103424SAndreas Färber     cs->exception_index = exception;
2428ad71ed68SBlue Swirl     env->error_code = error_code;
2429db789c6cSBenjamin Herrenschmidt     cpu_loop_exit_restore(cs, raddr);
2430db789c6cSBenjamin Herrenschmidt }
2431db789c6cSBenjamin Herrenschmidt 
2432db789c6cSBenjamin Herrenschmidt void raise_exception_err(CPUPPCState *env, uint32_t exception,
2433db789c6cSBenjamin Herrenschmidt                          uint32_t error_code)
2434db789c6cSBenjamin Herrenschmidt {
2435db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2436db789c6cSBenjamin Herrenschmidt }
2437db789c6cSBenjamin Herrenschmidt 
2438db789c6cSBenjamin Herrenschmidt void raise_exception(CPUPPCState *env, uint32_t exception)
2439db789c6cSBenjamin Herrenschmidt {
2440db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2441db789c6cSBenjamin Herrenschmidt }
2442db789c6cSBenjamin Herrenschmidt 
2443db789c6cSBenjamin Herrenschmidt void raise_exception_ra(CPUPPCState *env, uint32_t exception,
2444db789c6cSBenjamin Herrenschmidt                         uintptr_t raddr)
2445db789c6cSBenjamin Herrenschmidt {
2446db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, raddr);
2447db789c6cSBenjamin Herrenschmidt }
2448db789c6cSBenjamin Herrenschmidt 
24492b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2450db789c6cSBenjamin Herrenschmidt void helper_raise_exception_err(CPUPPCState *env, uint32_t exception,
2451db789c6cSBenjamin Herrenschmidt                                 uint32_t error_code)
2452db789c6cSBenjamin Herrenschmidt {
2453db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2454ad71ed68SBlue Swirl }
2455ad71ed68SBlue Swirl 
2456e5f17ac6SBlue Swirl void helper_raise_exception(CPUPPCState *env, uint32_t exception)
2457ad71ed68SBlue Swirl {
2458db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2459ad71ed68SBlue Swirl }
24602b44e219SBruno Larsen (billionai) #endif
2461ad71ed68SBlue Swirl 
2462ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY)
24632b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2464e5f17ac6SBlue Swirl void helper_store_msr(CPUPPCState *env, target_ulong val)
2465ad71ed68SBlue Swirl {
2466db789c6cSBenjamin Herrenschmidt     uint32_t excp = hreg_store_msr(env, val, 0);
2467259186a7SAndreas Färber 
2468db789c6cSBenjamin Herrenschmidt     if (excp != 0) {
2469db70b311SRichard Henderson         CPUState *cs = env_cpu(env);
2470044897efSRichard Purdie         cpu_interrupt_exittb(cs);
2471db789c6cSBenjamin Herrenschmidt         raise_exception(env, excp);
2472ad71ed68SBlue Swirl     }
2473ad71ed68SBlue Swirl }
2474ad71ed68SBlue Swirl 
24757778a575SBenjamin Herrenschmidt #if defined(TARGET_PPC64)
2476f43520e5SRichard Henderson void helper_scv(CPUPPCState *env, uint32_t lev)
2477f43520e5SRichard Henderson {
2478f43520e5SRichard Henderson     if (env->spr[SPR_FSCR] & (1ull << FSCR_SCV)) {
2479f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_SYSCALL_VECTORED, lev);
2480f43520e5SRichard Henderson     } else {
2481f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_FU, FSCR_IC_SCV);
2482f43520e5SRichard Henderson     }
2483f43520e5SRichard Henderson }
2484f43520e5SRichard Henderson 
24857778a575SBenjamin Herrenschmidt void helper_pminsn(CPUPPCState *env, powerpc_pm_insn_t insn)
24867778a575SBenjamin Herrenschmidt {
24877778a575SBenjamin Herrenschmidt     CPUState *cs;
24887778a575SBenjamin Herrenschmidt 
2489db70b311SRichard Henderson     cs = env_cpu(env);
24907778a575SBenjamin Herrenschmidt     cs->halted = 1;
24917778a575SBenjamin Herrenschmidt 
24923621e2c9SBenjamin Herrenschmidt     /* Condition for waking up at 0x100 */
24931e7fd61dSBenjamin Herrenschmidt     env->resume_as_sreset = (insn != PPC_PM_STOP) ||
249421c0d66aSBenjamin Herrenschmidt         (env->spr[SPR_PSSCR] & PSSCR_EC);
24957778a575SBenjamin Herrenschmidt }
24967778a575SBenjamin Herrenschmidt #endif /* defined(TARGET_PPC64) */
24977778a575SBenjamin Herrenschmidt 
249862e79ef9SCédric Le Goater static void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr)
2499ad71ed68SBlue Swirl {
2500db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
2501259186a7SAndreas Färber 
2502a2e71b28SBenjamin Herrenschmidt     /* MSR:POW cannot be set by any form of rfi */
2503a2e71b28SBenjamin Herrenschmidt     msr &= ~(1ULL << MSR_POW);
2504a2e71b28SBenjamin Herrenschmidt 
25055aad0457SChristophe Leroy     /* MSR:TGPR cannot be set by any form of rfi */
25065aad0457SChristophe Leroy     if (env->flags & POWERPC_FLAG_TGPR)
25075aad0457SChristophe Leroy         msr &= ~(1ULL << MSR_TGPR);
25085aad0457SChristophe Leroy 
2509ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2510a2e71b28SBenjamin Herrenschmidt     /* Switching to 32-bit ? Crop the nip */
2511a2e71b28SBenjamin Herrenschmidt     if (!msr_is_64bit(env, msr)) {
2512ad71ed68SBlue Swirl         nip = (uint32_t)nip;
2513ad71ed68SBlue Swirl     }
2514ad71ed68SBlue Swirl #else
2515ad71ed68SBlue Swirl     nip = (uint32_t)nip;
2516ad71ed68SBlue Swirl #endif
2517ad71ed68SBlue Swirl     /* XXX: beware: this is false if VLE is supported */
2518ad71ed68SBlue Swirl     env->nip = nip & ~((target_ulong)0x00000003);
2519ad71ed68SBlue Swirl     hreg_store_msr(env, msr, 1);
25202eb1ef73SCédric Le Goater     trace_ppc_excp_rfi(env->nip, env->msr);
252147733729SDavid Gibson     /*
252247733729SDavid Gibson      * No need to raise an exception here, as rfi is always the last
252347733729SDavid Gibson      * insn of a TB
2524ad71ed68SBlue Swirl      */
2525044897efSRichard Purdie     cpu_interrupt_exittb(cs);
2526a8b73734SNikunj A Dadhania     /* Reset the reservation */
2527a8b73734SNikunj A Dadhania     env->reserve_addr = -1;
2528a8b73734SNikunj A Dadhania 
2529cd0c6f47SBenjamin Herrenschmidt     /* Context synchronizing: check if TCG TLB needs flush */
2530e3cffe6fSNikunj A Dadhania     check_tlb_flush(env, false);
2531ad71ed68SBlue Swirl }
2532ad71ed68SBlue Swirl 
2533e5f17ac6SBlue Swirl void helper_rfi(CPUPPCState *env)
2534ad71ed68SBlue Swirl {
2535a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful);
2536a1bb7384SScott Wood }
2537ad71ed68SBlue Swirl 
2538ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2539e5f17ac6SBlue Swirl void helper_rfid(CPUPPCState *env)
2540ad71ed68SBlue Swirl {
254147733729SDavid Gibson     /*
2542136fbf65Szhaolichang      * The architecture defines a number of rules for which bits can
254347733729SDavid Gibson      * change but in practice, we handle this in hreg_store_msr()
2544a2e71b28SBenjamin Herrenschmidt      * which will be called by do_rfi(), so there is no need to filter
2545a2e71b28SBenjamin Herrenschmidt      * here
2546a2e71b28SBenjamin Herrenschmidt      */
2547a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]);
2548ad71ed68SBlue Swirl }
2549ad71ed68SBlue Swirl 
25503c89b8d6SNicholas Piggin void helper_rfscv(CPUPPCState *env)
25513c89b8d6SNicholas Piggin {
25523c89b8d6SNicholas Piggin     do_rfi(env, env->lr, env->ctr);
25533c89b8d6SNicholas Piggin }
25543c89b8d6SNicholas Piggin 
2555e5f17ac6SBlue Swirl void helper_hrfid(CPUPPCState *env)
2556ad71ed68SBlue Swirl {
2557a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
2558ad71ed68SBlue Swirl }
2559ad71ed68SBlue Swirl #endif
2560ad71ed68SBlue Swirl 
25611f26c751SDaniel Henrique Barboza #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
25621f26c751SDaniel Henrique Barboza void helper_rfebb(CPUPPCState *env, target_ulong s)
25631f26c751SDaniel Henrique Barboza {
25641f26c751SDaniel Henrique Barboza     target_ulong msr = env->msr;
25651f26c751SDaniel Henrique Barboza 
25661f26c751SDaniel Henrique Barboza     /*
25671f26c751SDaniel Henrique Barboza      * Handling of BESCR bits 32:33 according to PowerISA v3.1:
25681f26c751SDaniel Henrique Barboza      *
25691f26c751SDaniel Henrique Barboza      * "If BESCR 32:33 != 0b00 the instruction is treated as if
25701f26c751SDaniel Henrique Barboza      *  the instruction form were invalid."
25711f26c751SDaniel Henrique Barboza      */
25721f26c751SDaniel Henrique Barboza     if (env->spr[SPR_BESCR] & BESCR_INVALID) {
25731f26c751SDaniel Henrique Barboza         raise_exception_err(env, POWERPC_EXCP_PROGRAM,
25741f26c751SDaniel Henrique Barboza                             POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
25751f26c751SDaniel Henrique Barboza     }
25761f26c751SDaniel Henrique Barboza 
25771f26c751SDaniel Henrique Barboza     env->nip = env->spr[SPR_EBBRR];
25781f26c751SDaniel Henrique Barboza 
25791f26c751SDaniel Henrique Barboza     /* Switching to 32-bit ? Crop the nip */
25801f26c751SDaniel Henrique Barboza     if (!msr_is_64bit(env, msr)) {
25811f26c751SDaniel Henrique Barboza         env->nip = (uint32_t)env->spr[SPR_EBBRR];
25821f26c751SDaniel Henrique Barboza     }
25831f26c751SDaniel Henrique Barboza 
25841f26c751SDaniel Henrique Barboza     if (s) {
25851f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_GE;
25861f26c751SDaniel Henrique Barboza     } else {
25871f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
25881f26c751SDaniel Henrique Barboza     }
25891f26c751SDaniel Henrique Barboza }
2590d3412df2SDaniel Henrique Barboza 
2591d3412df2SDaniel Henrique Barboza /*
2592d3412df2SDaniel Henrique Barboza  * Triggers or queues an 'ebb_excp' EBB exception. All checks
2593d3412df2SDaniel Henrique Barboza  * but FSCR, HFSCR and msr_pr must be done beforehand.
2594d3412df2SDaniel Henrique Barboza  *
2595d3412df2SDaniel Henrique Barboza  * PowerISA v3.1 isn't clear about whether an EBB should be
2596d3412df2SDaniel Henrique Barboza  * postponed or cancelled if the EBB facility is unavailable.
2597d3412df2SDaniel Henrique Barboza  * Our assumption here is that the EBB is cancelled if both
2598d3412df2SDaniel Henrique Barboza  * FSCR and HFSCR EBB facilities aren't available.
2599d3412df2SDaniel Henrique Barboza  */
2600d3412df2SDaniel Henrique Barboza static void do_ebb(CPUPPCState *env, int ebb_excp)
2601d3412df2SDaniel Henrique Barboza {
2602d3412df2SDaniel Henrique Barboza     PowerPCCPU *cpu = env_archcpu(env);
2603d3412df2SDaniel Henrique Barboza 
2604d3412df2SDaniel Henrique Barboza     /*
2605d3412df2SDaniel Henrique Barboza      * FSCR_EBB and FSCR_IC_EBB are the same bits used with
2606d3412df2SDaniel Henrique Barboza      * HFSCR.
2607d3412df2SDaniel Henrique Barboza      */
2608d3412df2SDaniel Henrique Barboza     helper_fscr_facility_check(env, FSCR_EBB, 0, FSCR_IC_EBB);
2609d3412df2SDaniel Henrique Barboza     helper_hfscr_facility_check(env, FSCR_EBB, "EBB", FSCR_IC_EBB);
2610d3412df2SDaniel Henrique Barboza 
2611d3412df2SDaniel Henrique Barboza     if (ebb_excp == POWERPC_EXCP_PERFM_EBB) {
2612d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_PMEO;
2613d3412df2SDaniel Henrique Barboza     } else if (ebb_excp == POWERPC_EXCP_EXTERNAL_EBB) {
2614d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_EEO;
2615d3412df2SDaniel Henrique Barboza     }
2616d3412df2SDaniel Henrique Barboza 
2617d41ccf6eSVíctor Colombo     if (FIELD_EX64(env->msr, MSR, PR)) {
2618d3412df2SDaniel Henrique Barboza         powerpc_excp(cpu, ebb_excp);
2619d3412df2SDaniel Henrique Barboza     } else {
26207b694df6SMatheus Ferst         ppc_set_irq(cpu, PPC_INTERRUPT_EBB, 1);
2621d3412df2SDaniel Henrique Barboza     }
2622d3412df2SDaniel Henrique Barboza }
2623d3412df2SDaniel Henrique Barboza 
2624d3412df2SDaniel Henrique Barboza void raise_ebb_perfm_exception(CPUPPCState *env)
2625d3412df2SDaniel Henrique Barboza {
2626d3412df2SDaniel Henrique Barboza     bool perfm_ebb_enabled = env->spr[SPR_POWER_MMCR0] & MMCR0_EBE &&
2627d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_PME &&
2628d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_GE;
2629d3412df2SDaniel Henrique Barboza 
2630d3412df2SDaniel Henrique Barboza     if (!perfm_ebb_enabled) {
2631d3412df2SDaniel Henrique Barboza         return;
2632d3412df2SDaniel Henrique Barboza     }
2633d3412df2SDaniel Henrique Barboza 
2634d3412df2SDaniel Henrique Barboza     do_ebb(env, POWERPC_EXCP_PERFM_EBB);
2635d3412df2SDaniel Henrique Barboza }
26361f26c751SDaniel Henrique Barboza #endif
26371f26c751SDaniel Henrique Barboza 
2638ad71ed68SBlue Swirl /*****************************************************************************/
2639ad71ed68SBlue Swirl /* Embedded PowerPC specific helpers */
2640e5f17ac6SBlue Swirl void helper_40x_rfci(CPUPPCState *env)
2641ad71ed68SBlue Swirl {
2642a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]);
2643ad71ed68SBlue Swirl }
2644ad71ed68SBlue Swirl 
2645e5f17ac6SBlue Swirl void helper_rfci(CPUPPCState *env)
2646ad71ed68SBlue Swirl {
2647a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]);
2648ad71ed68SBlue Swirl }
2649ad71ed68SBlue Swirl 
2650e5f17ac6SBlue Swirl void helper_rfdi(CPUPPCState *env)
2651ad71ed68SBlue Swirl {
2652a1bb7384SScott Wood     /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
2653a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]);
2654ad71ed68SBlue Swirl }
2655ad71ed68SBlue Swirl 
2656e5f17ac6SBlue Swirl void helper_rfmci(CPUPPCState *env)
2657ad71ed68SBlue Swirl {
2658a1bb7384SScott Wood     /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
2659a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
2660ad71ed68SBlue Swirl }
26612b44e219SBruno Larsen (billionai) #endif /* CONFIG_TCG */
26622b44e219SBruno Larsen (billionai) #endif /* !defined(CONFIG_USER_ONLY) */
2663ad71ed68SBlue Swirl 
26642b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2665e5f17ac6SBlue Swirl void helper_tw(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2666e5f17ac6SBlue Swirl                uint32_t flags)
2667ad71ed68SBlue Swirl {
2668ad71ed68SBlue Swirl     if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
2669ad71ed68SBlue Swirl                   ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
2670ad71ed68SBlue Swirl                   ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
2671ad71ed68SBlue Swirl                   ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
2672ad71ed68SBlue Swirl                   ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
267372073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
267472073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2675ad71ed68SBlue Swirl     }
2676ad71ed68SBlue Swirl }
2677ad71ed68SBlue Swirl 
2678ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2679e5f17ac6SBlue Swirl void helper_td(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2680e5f17ac6SBlue Swirl                uint32_t flags)
2681ad71ed68SBlue Swirl {
2682ad71ed68SBlue Swirl     if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
2683ad71ed68SBlue Swirl                   ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
2684ad71ed68SBlue Swirl                   ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
2685ad71ed68SBlue Swirl                   ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
2686ad71ed68SBlue Swirl                   ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) {
268772073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
268872073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2689ad71ed68SBlue Swirl     }
2690ad71ed68SBlue Swirl }
2691ad71ed68SBlue Swirl #endif
26922b44e219SBruno Larsen (billionai) #endif
2693ad71ed68SBlue Swirl 
2694670f1da3SVíctor Colombo static uint32_t helper_SIMON_LIKE_32_64(uint32_t x, uint64_t key, uint32_t lane)
2695670f1da3SVíctor Colombo {
2696670f1da3SVíctor Colombo     const uint16_t c = 0xfffc;
2697670f1da3SVíctor Colombo     const uint64_t z0 = 0xfa2561cdf44ac398ULL;
2698670f1da3SVíctor Colombo     uint16_t z = 0, temp;
2699670f1da3SVíctor Colombo     uint16_t k[32], eff_k[32], xleft[33], xright[33], fxleft[32];
2700670f1da3SVíctor Colombo 
2701670f1da3SVíctor Colombo     for (int i = 3; i >= 0; i--) {
2702670f1da3SVíctor Colombo         k[i] = key & 0xffff;
2703670f1da3SVíctor Colombo         key >>= 16;
2704670f1da3SVíctor Colombo     }
2705670f1da3SVíctor Colombo     xleft[0] = x & 0xffff;
2706670f1da3SVíctor Colombo     xright[0] = (x >> 16) & 0xffff;
2707670f1da3SVíctor Colombo 
2708670f1da3SVíctor Colombo     for (int i = 0; i < 28; i++) {
2709670f1da3SVíctor Colombo         z = (z0 >> (63 - i)) & 1;
2710670f1da3SVíctor Colombo         temp = ror16(k[i + 3], 3) ^ k[i + 1];
2711670f1da3SVíctor Colombo         k[i + 4] = c ^ z ^ k[i] ^ temp ^ ror16(temp, 1);
2712670f1da3SVíctor Colombo     }
2713670f1da3SVíctor Colombo 
2714670f1da3SVíctor Colombo     for (int i = 0; i < 8; i++) {
2715670f1da3SVíctor Colombo         eff_k[4 * i + 0] = k[4 * i + ((0 + lane) % 4)];
2716670f1da3SVíctor Colombo         eff_k[4 * i + 1] = k[4 * i + ((1 + lane) % 4)];
2717670f1da3SVíctor Colombo         eff_k[4 * i + 2] = k[4 * i + ((2 + lane) % 4)];
2718670f1da3SVíctor Colombo         eff_k[4 * i + 3] = k[4 * i + ((3 + lane) % 4)];
2719670f1da3SVíctor Colombo     }
2720670f1da3SVíctor Colombo 
2721670f1da3SVíctor Colombo     for (int i = 0; i < 32; i++) {
2722670f1da3SVíctor Colombo         fxleft[i] = (rol16(xleft[i], 1) &
2723670f1da3SVíctor Colombo             rol16(xleft[i], 8)) ^ rol16(xleft[i], 2);
2724670f1da3SVíctor Colombo         xleft[i + 1] = xright[i] ^ fxleft[i] ^ eff_k[i];
2725670f1da3SVíctor Colombo         xright[i + 1] = xleft[i];
2726670f1da3SVíctor Colombo     }
2727670f1da3SVíctor Colombo 
2728670f1da3SVíctor Colombo     return (((uint32_t)xright[32]) << 16) | xleft[32];
2729670f1da3SVíctor Colombo }
2730670f1da3SVíctor Colombo 
2731670f1da3SVíctor Colombo static uint64_t hash_digest(uint64_t ra, uint64_t rb, uint64_t key)
2732670f1da3SVíctor Colombo {
2733670f1da3SVíctor Colombo     uint64_t stage0_h = 0ULL, stage0_l = 0ULL;
2734670f1da3SVíctor Colombo     uint64_t stage1_h, stage1_l;
2735670f1da3SVíctor Colombo 
2736670f1da3SVíctor Colombo     for (int i = 0; i < 4; i++) {
2737670f1da3SVíctor Colombo         stage0_h |= ror64(rb & 0xff, 8 * (2 * i + 1));
2738670f1da3SVíctor Colombo         stage0_h |= ((ra >> 32) & 0xff) << (8 * 2 * i);
2739670f1da3SVíctor Colombo         stage0_l |= ror64((rb >> 32) & 0xff, 8 * (2 * i + 1));
2740670f1da3SVíctor Colombo         stage0_l |= (ra & 0xff) << (8 * 2 * i);
2741670f1da3SVíctor Colombo         rb >>= 8;
2742670f1da3SVíctor Colombo         ra >>= 8;
2743670f1da3SVíctor Colombo     }
2744670f1da3SVíctor Colombo 
2745670f1da3SVíctor Colombo     stage1_h = (uint64_t)helper_SIMON_LIKE_32_64(stage0_h >> 32, key, 0) << 32;
2746670f1da3SVíctor Colombo     stage1_h |= helper_SIMON_LIKE_32_64(stage0_h, key, 1);
2747670f1da3SVíctor Colombo     stage1_l = (uint64_t)helper_SIMON_LIKE_32_64(stage0_l >> 32, key, 2) << 32;
2748670f1da3SVíctor Colombo     stage1_l |= helper_SIMON_LIKE_32_64(stage0_l, key, 3);
2749670f1da3SVíctor Colombo 
2750670f1da3SVíctor Colombo     return stage1_h ^ stage1_l;
2751670f1da3SVíctor Colombo }
2752670f1da3SVíctor Colombo 
2753670f1da3SVíctor Colombo #include "qemu/guest-random.h"
2754670f1da3SVíctor Colombo 
2755670f1da3SVíctor Colombo #define HELPER_HASH(op, key, store)                                           \
2756670f1da3SVíctor Colombo void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
2757670f1da3SVíctor Colombo                  target_ulong rb)                                             \
2758670f1da3SVíctor Colombo {                                                                             \
2759670f1da3SVíctor Colombo     uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash;         \
2760670f1da3SVíctor Colombo                                                                               \
2761670f1da3SVíctor Colombo     if (store) {                                                              \
2762670f1da3SVíctor Colombo         cpu_stq_data_ra(env, ea, calculated_hash, GETPC());                   \
2763670f1da3SVíctor Colombo     } else {                                                                  \
2764670f1da3SVíctor Colombo         loaded_hash = cpu_ldq_data_ra(env, ea, GETPC());                      \
2765670f1da3SVíctor Colombo         if (loaded_hash != calculated_hash) {                                 \
2766670f1da3SVíctor Colombo             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,                 \
2767670f1da3SVíctor Colombo                 POWERPC_EXCP_TRAP, GETPC());                                  \
2768670f1da3SVíctor Colombo         }                                                                     \
2769670f1da3SVíctor Colombo     }                                                                         \
2770670f1da3SVíctor Colombo }
2771670f1da3SVíctor Colombo 
2772670f1da3SVíctor Colombo HELPER_HASH(HASHST, env->spr[SPR_HASHKEYR], true)
2773670f1da3SVíctor Colombo HELPER_HASH(HASHCHK, env->spr[SPR_HASHKEYR], false)
277453ae2aebSVíctor Colombo HELPER_HASH(HASHSTP, env->spr[SPR_HASHPKEYR], true)
277553ae2aebSVíctor Colombo HELPER_HASH(HASHCHKP, env->spr[SPR_HASHPKEYR], false)
2776670f1da3SVíctor Colombo 
2777ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY)
2778ad71ed68SBlue Swirl 
27792b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2780ad71ed68SBlue Swirl 
2781ad71ed68SBlue Swirl /* Embedded.Processor Control */
2782ad71ed68SBlue Swirl static int dbell2irq(target_ulong rb)
2783ad71ed68SBlue Swirl {
2784ad71ed68SBlue Swirl     int msg = rb & DBELL_TYPE_MASK;
2785ad71ed68SBlue Swirl     int irq = -1;
2786ad71ed68SBlue Swirl 
2787ad71ed68SBlue Swirl     switch (msg) {
2788ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL:
2789ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_DOORBELL;
2790ad71ed68SBlue Swirl         break;
2791ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL_CRIT:
2792ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_CDOORBELL;
2793ad71ed68SBlue Swirl         break;
2794ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL:
2795ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_CRIT:
2796ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_MC:
2797ad71ed68SBlue Swirl         /* XXX implement */
2798ad71ed68SBlue Swirl     default:
2799ad71ed68SBlue Swirl         break;
2800ad71ed68SBlue Swirl     }
2801ad71ed68SBlue Swirl 
2802ad71ed68SBlue Swirl     return irq;
2803ad71ed68SBlue Swirl }
2804ad71ed68SBlue Swirl 
2805e5f17ac6SBlue Swirl void helper_msgclr(CPUPPCState *env, target_ulong rb)
2806ad71ed68SBlue Swirl {
2807ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2808ad71ed68SBlue Swirl 
2809ad71ed68SBlue Swirl     if (irq < 0) {
2810ad71ed68SBlue Swirl         return;
2811ad71ed68SBlue Swirl     }
2812ad71ed68SBlue Swirl 
28137b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), irq, 0);
2814ad71ed68SBlue Swirl }
2815ad71ed68SBlue Swirl 
2816ad71ed68SBlue Swirl void helper_msgsnd(target_ulong rb)
2817ad71ed68SBlue Swirl {
2818ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2819ad71ed68SBlue Swirl     int pir = rb & DBELL_PIRTAG_MASK;
2820182735efSAndreas Färber     CPUState *cs;
2821ad71ed68SBlue Swirl 
2822ad71ed68SBlue Swirl     if (irq < 0) {
2823ad71ed68SBlue Swirl         return;
2824ad71ed68SBlue Swirl     }
2825ad71ed68SBlue Swirl 
2826f1c29ebcSThomas Huth     qemu_mutex_lock_iothread();
2827bdc44640SAndreas Färber     CPU_FOREACH(cs) {
2828182735efSAndreas Färber         PowerPCCPU *cpu = POWERPC_CPU(cs);
2829182735efSAndreas Färber         CPUPPCState *cenv = &cpu->env;
2830182735efSAndreas Färber 
2831ad71ed68SBlue Swirl         if ((rb & DBELL_BRDCAST) || (cenv->spr[SPR_BOOKE_PIR] == pir)) {
28327b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
2833ad71ed68SBlue Swirl         }
2834ad71ed68SBlue Swirl     }
2835f1c29ebcSThomas Huth     qemu_mutex_unlock_iothread();
2836ad71ed68SBlue Swirl }
28377af1e7b0SCédric Le Goater 
28387af1e7b0SCédric Le Goater /* Server Processor Control */
28397af1e7b0SCédric Le Goater 
28405ba7ba1dSCédric Le Goater static bool dbell_type_server(target_ulong rb)
28415ba7ba1dSCédric Le Goater {
284247733729SDavid Gibson     /*
284347733729SDavid Gibson      * A Directed Hypervisor Doorbell message is sent only if the
28447af1e7b0SCédric Le Goater      * message type is 5. All other types are reserved and the
284547733729SDavid Gibson      * instruction is a no-op
284647733729SDavid Gibson      */
28475ba7ba1dSCédric Le Goater     return (rb & DBELL_TYPE_MASK) == DBELL_TYPE_DBELL_SERVER;
28487af1e7b0SCédric Le Goater }
28497af1e7b0SCédric Le Goater 
28507af1e7b0SCédric Le Goater void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb)
28517af1e7b0SCédric Le Goater {
28525ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
28537af1e7b0SCédric Le Goater         return;
28547af1e7b0SCédric Le Goater     }
28557af1e7b0SCédric Le Goater 
28567b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
28577af1e7b0SCédric Le Goater }
28587af1e7b0SCédric Le Goater 
28595ba7ba1dSCédric Le Goater static void book3s_msgsnd_common(int pir, int irq)
28607af1e7b0SCédric Le Goater {
28617af1e7b0SCédric Le Goater     CPUState *cs;
28627af1e7b0SCédric Le Goater 
28637af1e7b0SCédric Le Goater     qemu_mutex_lock_iothread();
28647af1e7b0SCédric Le Goater     CPU_FOREACH(cs) {
28657af1e7b0SCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
28667af1e7b0SCédric Le Goater         CPUPPCState *cenv = &cpu->env;
28677af1e7b0SCédric Le Goater 
28687af1e7b0SCédric Le Goater         /* TODO: broadcast message to all threads of the same  processor */
28697af1e7b0SCédric Le Goater         if (cenv->spr_cb[SPR_PIR].default_value == pir) {
28707b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
28717af1e7b0SCédric Le Goater         }
28727af1e7b0SCédric Le Goater     }
28737af1e7b0SCédric Le Goater     qemu_mutex_unlock_iothread();
28747af1e7b0SCédric Le Goater }
28755ba7ba1dSCédric Le Goater 
28765ba7ba1dSCédric Le Goater void helper_book3s_msgsnd(target_ulong rb)
28775ba7ba1dSCédric Le Goater {
28785ba7ba1dSCédric Le Goater     int pir = rb & DBELL_PROCIDTAG_MASK;
28795ba7ba1dSCédric Le Goater 
28805ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
28815ba7ba1dSCédric Le Goater         return;
28825ba7ba1dSCédric Le Goater     }
28835ba7ba1dSCédric Le Goater 
28845ba7ba1dSCédric Le Goater     book3s_msgsnd_common(pir, PPC_INTERRUPT_HDOORBELL);
28855ba7ba1dSCédric Le Goater }
28865ba7ba1dSCédric Le Goater 
28875ba7ba1dSCédric Le Goater #if defined(TARGET_PPC64)
28885ba7ba1dSCédric Le Goater void helper_book3s_msgclrp(CPUPPCState *env, target_ulong rb)
28895ba7ba1dSCédric Le Goater {
2890493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgclrp", HFSCR_IC_MSGP);
2891493028d8SCédric Le Goater 
28925ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
28935ba7ba1dSCédric Le Goater         return;
28945ba7ba1dSCédric Le Goater     }
28955ba7ba1dSCédric Le Goater 
28967b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
28975ba7ba1dSCédric Le Goater }
28985ba7ba1dSCédric Le Goater 
28995ba7ba1dSCédric Le Goater /*
29005ba7ba1dSCédric Le Goater  * sends a message to other threads that are on the same
29015ba7ba1dSCédric Le Goater  * multi-threaded processor
29025ba7ba1dSCédric Le Goater  */
29035ba7ba1dSCédric Le Goater void helper_book3s_msgsndp(CPUPPCState *env, target_ulong rb)
29045ba7ba1dSCédric Le Goater {
29055ba7ba1dSCédric Le Goater     int pir = env->spr_cb[SPR_PIR].default_value;
29065ba7ba1dSCédric Le Goater 
2907493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgsndp", HFSCR_IC_MSGP);
2908493028d8SCédric Le Goater 
29095ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
29105ba7ba1dSCédric Le Goater         return;
29115ba7ba1dSCédric Le Goater     }
29125ba7ba1dSCédric Le Goater 
29135ba7ba1dSCédric Le Goater     /* TODO: TCG supports only one thread */
29145ba7ba1dSCédric Le Goater 
29155ba7ba1dSCédric Le Goater     book3s_msgsnd_common(pir, PPC_INTERRUPT_DOORBELL);
29165ba7ba1dSCédric Le Goater }
2917996473e4SRichard Henderson #endif /* TARGET_PPC64 */
29180f3110faSRichard Henderson 
29190f3110faSRichard Henderson void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
29200f3110faSRichard Henderson                                  MMUAccessType access_type,
29210f3110faSRichard Henderson                                  int mmu_idx, uintptr_t retaddr)
29220f3110faSRichard Henderson {
29230f3110faSRichard Henderson     CPUPPCState *env = cs->env_ptr;
292429c4a336SFabiano Rosas     uint32_t insn;
292529c4a336SFabiano Rosas 
292629c4a336SFabiano Rosas     /* Restore state and reload the insn we executed, for filling in DSISR.  */
292729c4a336SFabiano Rosas     cpu_restore_state(cs, retaddr, true);
292829c4a336SFabiano Rosas     insn = cpu_ldl_code(env, env->nip);
29290f3110faSRichard Henderson 
2930a7e3af13SRichard Henderson     switch (env->mmu_model) {
2931a7e3af13SRichard Henderson     case POWERPC_MMU_SOFT_4xx:
2932a7e3af13SRichard Henderson         env->spr[SPR_40x_DEAR] = vaddr;
2933a7e3af13SRichard Henderson         break;
2934a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE:
2935a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE206:
2936a7e3af13SRichard Henderson         env->spr[SPR_BOOKE_DEAR] = vaddr;
2937a7e3af13SRichard Henderson         break;
2938a7e3af13SRichard Henderson     default:
2939a7e3af13SRichard Henderson         env->spr[SPR_DAR] = vaddr;
2940a7e3af13SRichard Henderson         break;
2941a7e3af13SRichard Henderson     }
2942a7e3af13SRichard Henderson 
29430f3110faSRichard Henderson     cs->exception_index = POWERPC_EXCP_ALIGN;
294429c4a336SFabiano Rosas     env->error_code = insn & 0x03FF0000;
294529c4a336SFabiano Rosas     cpu_loop_exit(cs);
29460f3110faSRichard Henderson }
2947996473e4SRichard Henderson #endif /* CONFIG_TCG */
2948996473e4SRichard Henderson #endif /* !CONFIG_USER_ONLY */
2949