xref: /qemu/target/ppc/excp_helper.c (revision a9899d42012c49169074fb85b61c78aa6e17af8e)
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*a9899d42SMatheus Ferst static int p8_next_unmasked_interrupt(CPUPPCState *env)
1689*a9899d42SMatheus Ferst {
1690*a9899d42SMatheus Ferst     bool async_deliver;
1691*a9899d42SMatheus Ferst 
1692*a9899d42SMatheus Ferst     /* External reset */
1693*a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
1694*a9899d42SMatheus Ferst         return PPC_INTERRUPT_RESET;
1695*a9899d42SMatheus Ferst     }
1696*a9899d42SMatheus Ferst     /* Machine check exception */
1697*a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1698*a9899d42SMatheus Ferst         return PPC_INTERRUPT_MCK;
1699*a9899d42SMatheus Ferst     }
1700*a9899d42SMatheus Ferst 
1701*a9899d42SMatheus Ferst     /*
1702*a9899d42SMatheus Ferst      * For interrupts that gate on MSR:EE, we need to do something a
1703*a9899d42SMatheus Ferst      * bit more subtle, as we need to let them through even when EE is
1704*a9899d42SMatheus Ferst      * clear when coming out of some power management states (in order
1705*a9899d42SMatheus Ferst      * for them to become a 0x100).
1706*a9899d42SMatheus Ferst      */
1707*a9899d42SMatheus Ferst     async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1708*a9899d42SMatheus Ferst 
1709*a9899d42SMatheus Ferst     /* Hypervisor decrementer exception */
1710*a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1711*a9899d42SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1712*a9899d42SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
1713*a9899d42SMatheus Ferst         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
1714*a9899d42SMatheus Ferst             /* HDEC clears on delivery */
1715*a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDECR;
1716*a9899d42SMatheus Ferst         }
1717*a9899d42SMatheus Ferst     }
1718*a9899d42SMatheus Ferst 
1719*a9899d42SMatheus Ferst     /* Hypervisor virtualization interrupt */
1720*a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
1721*a9899d42SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
1722*a9899d42SMatheus Ferst         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
1723*a9899d42SMatheus Ferst         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
1724*a9899d42SMatheus Ferst             return PPC_INTERRUPT_HVIRT;
1725*a9899d42SMatheus Ferst         }
1726*a9899d42SMatheus Ferst     }
1727*a9899d42SMatheus Ferst 
1728*a9899d42SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
1729*a9899d42SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1730*a9899d42SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1731*a9899d42SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1732*a9899d42SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
1733*a9899d42SMatheus Ferst         if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
1734*a9899d42SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
1735*a9899d42SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1736*a9899d42SMatheus Ferst             return PPC_INTERRUPT_EXT;
1737*a9899d42SMatheus Ferst         }
1738*a9899d42SMatheus Ferst     }
1739*a9899d42SMatheus Ferst     if (FIELD_EX64(env->msr, MSR, CE)) {
1740*a9899d42SMatheus Ferst         /* External critical interrupt */
1741*a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CEXT) {
1742*a9899d42SMatheus Ferst             return PPC_INTERRUPT_CEXT;
1743*a9899d42SMatheus Ferst         }
1744*a9899d42SMatheus Ferst     }
1745*a9899d42SMatheus Ferst     if (async_deliver != 0) {
1746*a9899d42SMatheus Ferst         /* Watchdog timer on embedded PowerPC */
1747*a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_WDT) {
1748*a9899d42SMatheus Ferst             return PPC_INTERRUPT_WDT;
1749*a9899d42SMatheus Ferst         }
1750*a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) {
1751*a9899d42SMatheus Ferst             return PPC_INTERRUPT_CDOORBELL;
1752*a9899d42SMatheus Ferst         }
1753*a9899d42SMatheus Ferst         /* Fixed interval timer on embedded PowerPC */
1754*a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_FIT) {
1755*a9899d42SMatheus Ferst             return PPC_INTERRUPT_FIT;
1756*a9899d42SMatheus Ferst         }
1757*a9899d42SMatheus Ferst         /* Programmable interval timer on embedded PowerPC */
1758*a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PIT) {
1759*a9899d42SMatheus Ferst             return PPC_INTERRUPT_PIT;
1760*a9899d42SMatheus Ferst         }
1761*a9899d42SMatheus Ferst         /* Decrementer exception */
1762*a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1763*a9899d42SMatheus Ferst             return PPC_INTERRUPT_DECR;
1764*a9899d42SMatheus Ferst         }
1765*a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1766*a9899d42SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
1767*a9899d42SMatheus Ferst         }
1768*a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
1769*a9899d42SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
1770*a9899d42SMatheus Ferst         }
1771*a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1772*a9899d42SMatheus Ferst             return PPC_INTERRUPT_PERFM;
1773*a9899d42SMatheus Ferst         }
1774*a9899d42SMatheus Ferst         /* Thermal interrupt */
1775*a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_THERM) {
1776*a9899d42SMatheus Ferst             return PPC_INTERRUPT_THERM;
1777*a9899d42SMatheus Ferst         }
1778*a9899d42SMatheus Ferst         /* EBB exception */
1779*a9899d42SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
1780*a9899d42SMatheus Ferst             /*
1781*a9899d42SMatheus Ferst              * EBB exception must be taken in problem state and
1782*a9899d42SMatheus Ferst              * with BESCR_GE set.
1783*a9899d42SMatheus Ferst              */
1784*a9899d42SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
1785*a9899d42SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
1786*a9899d42SMatheus Ferst                 return PPC_INTERRUPT_EBB;
1787*a9899d42SMatheus Ferst             }
1788*a9899d42SMatheus Ferst         }
1789*a9899d42SMatheus Ferst     }
1790*a9899d42SMatheus Ferst 
1791*a9899d42SMatheus Ferst     return 0;
1792*a9899d42SMatheus Ferst }
1793*a9899d42SMatheus Ferst 
1794b00e9a2fSMatheus Ferst #define P9_UNUSED_INTERRUPTS \
1795b00e9a2fSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT |   \
1796b00e9a2fSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |  \
1797b00e9a2fSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1798b00e9a2fSMatheus Ferst 
17992dfecf01SMatheus Ferst static int p9_next_unmasked_interrupt(CPUPPCState *env)
18002dfecf01SMatheus Ferst {
180127796411SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
180227796411SMatheus Ferst     CPUState *cs = CPU(cpu);
180327796411SMatheus Ferst     /* Ignore MSR[EE] when coming out of some power management states */
180427796411SMatheus Ferst     bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
18052dfecf01SMatheus Ferst 
1806b00e9a2fSMatheus Ferst     assert((env->pending_interrupts & P9_UNUSED_INTERRUPTS) == 0);
1807b00e9a2fSMatheus Ferst 
180827796411SMatheus Ferst     if (cs->halted) {
180927796411SMatheus Ferst         if (env->spr[SPR_PSSCR] & PSSCR_EC) {
181027796411SMatheus Ferst             /*
181127796411SMatheus Ferst              * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can
181227796411SMatheus Ferst              * wakeup the processor
181327796411SMatheus Ferst              */
181427796411SMatheus Ferst             return p9_interrupt_powersave(env);
181527796411SMatheus Ferst         } else {
181627796411SMatheus Ferst             /*
181727796411SMatheus Ferst              * When it's clear, any system-caused exception exits power-saving
181827796411SMatheus Ferst              * mode, even the ones that gate on MSR[EE].
181927796411SMatheus Ferst              */
182027796411SMatheus Ferst             msr_ee = true;
182127796411SMatheus Ferst         }
182227796411SMatheus Ferst     }
182327796411SMatheus Ferst 
18242dfecf01SMatheus Ferst     /* Machine check exception */
18252dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
18262dfecf01SMatheus Ferst         return PPC_INTERRUPT_MCK;
18272dfecf01SMatheus Ferst     }
18282dfecf01SMatheus Ferst 
18292dfecf01SMatheus Ferst     /* Hypervisor decrementer exception */
18302dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
18312dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
18322dfecf01SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
183327796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
18342dfecf01SMatheus Ferst             /* HDEC clears on delivery */
18352dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDECR;
18362dfecf01SMatheus Ferst         }
18372dfecf01SMatheus Ferst     }
18382dfecf01SMatheus Ferst 
18392dfecf01SMatheus Ferst     /* Hypervisor virtualization interrupt */
18402dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
18412dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
18422dfecf01SMatheus Ferst         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
184327796411SMatheus Ferst         if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hvice) {
18442dfecf01SMatheus Ferst             return PPC_INTERRUPT_HVIRT;
18452dfecf01SMatheus Ferst         }
18462dfecf01SMatheus Ferst     }
18472dfecf01SMatheus Ferst 
18482dfecf01SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
18492dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
18502dfecf01SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
18512dfecf01SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
18522dfecf01SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
185327796411SMatheus Ferst         if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
18542dfecf01SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
18552dfecf01SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
18562dfecf01SMatheus Ferst             return PPC_INTERRUPT_EXT;
18572dfecf01SMatheus Ferst         }
18582dfecf01SMatheus Ferst     }
185927796411SMatheus Ferst     if (msr_ee != 0) {
18602dfecf01SMatheus Ferst         /* Decrementer exception */
18612dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
18622dfecf01SMatheus Ferst             return PPC_INTERRUPT_DECR;
18632dfecf01SMatheus Ferst         }
18642dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
18652dfecf01SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
18662dfecf01SMatheus Ferst         }
18672dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
18682dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
18692dfecf01SMatheus Ferst         }
18702dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
18712dfecf01SMatheus Ferst             return PPC_INTERRUPT_PERFM;
18722dfecf01SMatheus Ferst         }
18732dfecf01SMatheus Ferst         /* EBB exception */
18742dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
18752dfecf01SMatheus Ferst             /*
18762dfecf01SMatheus Ferst              * EBB exception must be taken in problem state and
18772dfecf01SMatheus Ferst              * with BESCR_GE set.
18782dfecf01SMatheus Ferst              */
18792dfecf01SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
18802dfecf01SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
18812dfecf01SMatheus Ferst                 return PPC_INTERRUPT_EBB;
18822dfecf01SMatheus Ferst             }
18832dfecf01SMatheus Ferst         }
18842dfecf01SMatheus Ferst     }
18852dfecf01SMatheus Ferst 
18862dfecf01SMatheus Ferst     return 0;
18872dfecf01SMatheus Ferst }
18882dfecf01SMatheus Ferst #endif
18892dfecf01SMatheus Ferst 
1890ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt_generic(CPUPPCState *env)
1891c79c73f6SBlue Swirl {
18923621e2c9SBenjamin Herrenschmidt     bool async_deliver;
1893259186a7SAndreas Färber 
1894c79c73f6SBlue Swirl     /* External reset */
1895f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
1896de76b85cSMatheus Ferst         return PPC_INTERRUPT_RESET;
1897c79c73f6SBlue Swirl     }
1898c79c73f6SBlue Swirl     /* Machine check exception */
1899f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1900de76b85cSMatheus Ferst         return PPC_INTERRUPT_MCK;
1901c79c73f6SBlue Swirl     }
1902c79c73f6SBlue Swirl #if 0 /* TODO */
1903c79c73f6SBlue Swirl     /* External debug exception */
1904f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) {
1905de76b85cSMatheus Ferst         return PPC_INTERRUPT_DEBUG;
1906c79c73f6SBlue Swirl     }
1907c79c73f6SBlue Swirl #endif
19083621e2c9SBenjamin Herrenschmidt 
19093621e2c9SBenjamin Herrenschmidt     /*
19103621e2c9SBenjamin Herrenschmidt      * For interrupts that gate on MSR:EE, we need to do something a
19113621e2c9SBenjamin Herrenschmidt      * bit more subtle, as we need to let them through even when EE is
19123621e2c9SBenjamin Herrenschmidt      * clear when coming out of some power management states (in order
19133621e2c9SBenjamin Herrenschmidt      * for them to become a 0x100).
19143621e2c9SBenjamin Herrenschmidt      */
19150939b8f8SVíctor Colombo     async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
19163621e2c9SBenjamin Herrenschmidt 
1917c79c73f6SBlue Swirl     /* Hypervisor decrementer exception */
1918f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
19194b236b62SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
19204b236b62SBenjamin Herrenschmidt         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
19219de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
19224b236b62SBenjamin Herrenschmidt             /* HDEC clears on delivery */
1923de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDECR;
1924c79c73f6SBlue Swirl         }
1925c79c73f6SBlue Swirl     }
1926d8ce5fd6SBenjamin Herrenschmidt 
1927d8ce5fd6SBenjamin Herrenschmidt     /* Hypervisor virtualization interrupt */
1928f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
1929d8ce5fd6SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
1930d8ce5fd6SBenjamin Herrenschmidt         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
19319de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
1932de76b85cSMatheus Ferst             return PPC_INTERRUPT_HVIRT;
1933d8ce5fd6SBenjamin Herrenschmidt         }
1934d8ce5fd6SBenjamin Herrenschmidt     }
1935d8ce5fd6SBenjamin Herrenschmidt 
1936d8ce5fd6SBenjamin Herrenschmidt     /* External interrupt can ignore MSR:EE under some circumstances */
1937f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1938d1dbe37cSBenjamin Herrenschmidt         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
19396eebe6dcSBenjamin Herrenschmidt         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
19406eebe6dcSBenjamin Herrenschmidt         /* HEIC blocks delivery to the hypervisor */
19419de754d3SVíctor Colombo         if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
1942d41ccf6eSVíctor Colombo             !FIELD_EX64(env->msr, MSR, PR))) ||
19439de754d3SVíctor Colombo             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1944de76b85cSMatheus Ferst             return PPC_INTERRUPT_EXT;
1945d1dbe37cSBenjamin Herrenschmidt         }
1946d1dbe37cSBenjamin Herrenschmidt     }
1947acc861c2SVíctor Colombo     if (FIELD_EX64(env->msr, MSR, CE)) {
1948c79c73f6SBlue Swirl         /* External critical interrupt */
1949f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CEXT) {
1950de76b85cSMatheus Ferst             return PPC_INTERRUPT_CEXT;
1951c79c73f6SBlue Swirl         }
1952c79c73f6SBlue Swirl     }
19533621e2c9SBenjamin Herrenschmidt     if (async_deliver != 0) {
1954c79c73f6SBlue Swirl         /* Watchdog timer on embedded PowerPC */
1955f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_WDT) {
1956de76b85cSMatheus Ferst             return PPC_INTERRUPT_WDT;
1957c79c73f6SBlue Swirl         }
1958f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) {
1959de76b85cSMatheus Ferst             return PPC_INTERRUPT_CDOORBELL;
1960c79c73f6SBlue Swirl         }
1961c79c73f6SBlue Swirl         /* Fixed interval timer on embedded PowerPC */
1962f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_FIT) {
1963de76b85cSMatheus Ferst             return PPC_INTERRUPT_FIT;
1964c79c73f6SBlue Swirl         }
1965c79c73f6SBlue Swirl         /* Programmable interval timer on embedded PowerPC */
1966f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PIT) {
1967de76b85cSMatheus Ferst             return PPC_INTERRUPT_PIT;
1968c79c73f6SBlue Swirl         }
1969c79c73f6SBlue Swirl         /* Decrementer exception */
1970f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1971de76b85cSMatheus Ferst             return PPC_INTERRUPT_DECR;
1972c79c73f6SBlue Swirl         }
1973f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1974de76b85cSMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
1975c79c73f6SBlue Swirl         }
1976f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
1977de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
19787af1e7b0SCédric Le Goater         }
1979f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1980de76b85cSMatheus Ferst             return PPC_INTERRUPT_PERFM;
1981c79c73f6SBlue Swirl         }
1982c79c73f6SBlue Swirl         /* Thermal interrupt */
1983f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_THERM) {
1984de76b85cSMatheus Ferst             return PPC_INTERRUPT_THERM;
1985c79c73f6SBlue Swirl         }
1986cb76bbc4SDaniel Henrique Barboza         /* EBB exception */
1987f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
1988cb76bbc4SDaniel Henrique Barboza             /*
1989cb76bbc4SDaniel Henrique Barboza              * EBB exception must be taken in problem state and
1990cb76bbc4SDaniel Henrique Barboza              * with BESCR_GE set.
1991cb76bbc4SDaniel Henrique Barboza              */
1992d41ccf6eSVíctor Colombo             if (FIELD_EX64(env->msr, MSR, PR) &&
1993d41ccf6eSVíctor Colombo                 (env->spr[SPR_BESCR] & BESCR_GE)) {
1994de76b85cSMatheus Ferst                 return PPC_INTERRUPT_EBB;
1995de76b85cSMatheus Ferst             }
1996de76b85cSMatheus Ferst         }
1997de76b85cSMatheus Ferst     }
1998cb76bbc4SDaniel Henrique Barboza 
1999de76b85cSMatheus Ferst     return 0;
2000de76b85cSMatheus Ferst }
2001de76b85cSMatheus Ferst 
2002ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt(CPUPPCState *env)
2003ba2898f7SMatheus Ferst {
2004ba2898f7SMatheus Ferst     switch (env->excp_model) {
20052dfecf01SMatheus Ferst #if defined(TARGET_PPC64)
2006*a9899d42SMatheus Ferst     case POWERPC_EXCP_POWER8:
2007*a9899d42SMatheus Ferst         return p8_next_unmasked_interrupt(env);
20082dfecf01SMatheus Ferst     case POWERPC_EXCP_POWER9:
20092dfecf01SMatheus Ferst     case POWERPC_EXCP_POWER10:
20102dfecf01SMatheus Ferst         return p9_next_unmasked_interrupt(env);
20112dfecf01SMatheus Ferst #endif
2012ba2898f7SMatheus Ferst     default:
2013ba2898f7SMatheus Ferst         return ppc_next_unmasked_interrupt_generic(env);
2014ba2898f7SMatheus Ferst     }
2015ba2898f7SMatheus Ferst }
2016ba2898f7SMatheus Ferst 
20173654e238SMatheus Ferst #if defined(TARGET_PPC64)
20183654e238SMatheus Ferst static void p9_deliver_interrupt(CPUPPCState *env, int interrupt)
20193654e238SMatheus Ferst {
20203654e238SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
20213654e238SMatheus Ferst     CPUState *cs = env_cpu(env);
20223654e238SMatheus Ferst 
202327796411SMatheus Ferst     if (cs->halted && !(env->spr[SPR_PSSCR] & PSSCR_EC) &&
202427796411SMatheus Ferst         !FIELD_EX64(env->msr, MSR, EE)) {
202527796411SMatheus Ferst         /*
202627796411SMatheus Ferst          * A pending interrupt took us out of power-saving, but MSR[EE] says
202727796411SMatheus Ferst          * that we should return to NIP+4 instead of delivering it.
202827796411SMatheus Ferst          */
202927796411SMatheus Ferst         return;
203027796411SMatheus Ferst     }
203127796411SMatheus Ferst 
20323654e238SMatheus Ferst     switch (interrupt) {
20333654e238SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
20343654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
20353654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
20363654e238SMatheus Ferst         break;
20373654e238SMatheus Ferst 
20383654e238SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
20393654e238SMatheus Ferst         /* HDEC clears on delivery */
20403654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
20413654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
20423654e238SMatheus Ferst         break;
20433654e238SMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
20443654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
20453654e238SMatheus Ferst         break;
20463654e238SMatheus Ferst 
20473654e238SMatheus Ferst     case PPC_INTERRUPT_EXT:
20483654e238SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
20493654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
20503654e238SMatheus Ferst         } else {
20513654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
20523654e238SMatheus Ferst         }
20533654e238SMatheus Ferst         break;
20543654e238SMatheus Ferst 
20553654e238SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
20563654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
20573654e238SMatheus Ferst         break;
20583654e238SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
20593654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
20603654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
20613654e238SMatheus Ferst         break;
20623654e238SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
20633654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
20643654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
20653654e238SMatheus Ferst         break;
20663654e238SMatheus Ferst     case PPC_INTERRUPT_PERFM:
20673654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
20683654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
20693654e238SMatheus Ferst         break;
20703654e238SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
20713654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
20723654e238SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
20733654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
20743654e238SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
20753654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
20763654e238SMatheus Ferst         }
20773654e238SMatheus Ferst         break;
20783654e238SMatheus Ferst     case 0:
20793654e238SMatheus Ferst         /*
20803654e238SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
20813654e238SMatheus Ferst          * anything to deliver while in a PM state that requires getting
20823654e238SMatheus Ferst          * out via a 0x100
20833654e238SMatheus Ferst          *
20843654e238SMatheus Ferst          * This means we will incorrectly execute past the power management
20853654e238SMatheus Ferst          * instruction instead of triggering a reset.
20863654e238SMatheus Ferst          *
20873654e238SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
20883654e238SMatheus Ferst          * processor has_work implementation and the logic in this function.
20893654e238SMatheus Ferst          */
20903654e238SMatheus Ferst         assert(!env->resume_as_sreset);
20913654e238SMatheus Ferst         break;
20923654e238SMatheus Ferst     default:
20933654e238SMatheus Ferst         cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
20943654e238SMatheus Ferst     }
20953654e238SMatheus Ferst }
20963654e238SMatheus Ferst #endif
20973654e238SMatheus Ferst 
2098ba2898f7SMatheus Ferst static void ppc_deliver_interrupt_generic(CPUPPCState *env, int interrupt)
2099de76b85cSMatheus Ferst {
2100de76b85cSMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2101de76b85cSMatheus Ferst     CPUState *cs = env_cpu(env);
2102de76b85cSMatheus Ferst 
2103de76b85cSMatheus Ferst     switch (interrupt) {
2104de76b85cSMatheus Ferst     case PPC_INTERRUPT_RESET: /* External reset */
2105de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_RESET;
2106de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_RESET);
2107de76b85cSMatheus Ferst         break;
2108de76b85cSMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2109de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2110de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2111de76b85cSMatheus Ferst         break;
2112de76b85cSMatheus Ferst 
2113de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2114de76b85cSMatheus Ferst         /* HDEC clears on delivery */
2115de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2116de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2117de76b85cSMatheus Ferst         break;
2118de76b85cSMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
2119de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2120de76b85cSMatheus Ferst         break;
2121de76b85cSMatheus Ferst 
2122de76b85cSMatheus Ferst     case PPC_INTERRUPT_EXT:
2123de76b85cSMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2124de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2125de76b85cSMatheus Ferst         } else {
2126de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2127de76b85cSMatheus Ferst         }
2128de76b85cSMatheus Ferst         break;
2129de76b85cSMatheus Ferst     case PPC_INTERRUPT_CEXT: /* External critical interrupt */
2130de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_CRITICAL);
2131de76b85cSMatheus Ferst         break;
2132de76b85cSMatheus Ferst 
2133de76b85cSMatheus Ferst     case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */
2134de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_WDT;
2135de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_WDT);
2136de76b85cSMatheus Ferst         break;
2137de76b85cSMatheus Ferst     case PPC_INTERRUPT_CDOORBELL:
2138de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL;
2139de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DOORCI);
2140de76b85cSMatheus Ferst         break;
2141de76b85cSMatheus Ferst     case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */
2142de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_FIT;
2143de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_FIT);
2144de76b85cSMatheus Ferst         break;
2145de76b85cSMatheus Ferst     case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */
2146de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PIT;
2147de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PIT);
2148de76b85cSMatheus Ferst         break;
2149de76b85cSMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2150de76b85cSMatheus Ferst         if (ppc_decr_clear_on_delivery(env)) {
2151de76b85cSMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DECR;
2152de76b85cSMatheus Ferst         }
2153de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2154de76b85cSMatheus Ferst         break;
2155de76b85cSMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
2156de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
2157de76b85cSMatheus Ferst         if (is_book3s_arch2x(env)) {
2158de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
2159de76b85cSMatheus Ferst         } else {
2160de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
2161de76b85cSMatheus Ferst         }
2162de76b85cSMatheus Ferst         break;
2163de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
2164de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
2165de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
2166de76b85cSMatheus Ferst         break;
2167de76b85cSMatheus Ferst     case PPC_INTERRUPT_PERFM:
2168de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2169de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2170de76b85cSMatheus Ferst         break;
2171de76b85cSMatheus Ferst     case PPC_INTERRUPT_THERM:  /* Thermal interrupt */
2172de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_THERM;
2173de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_THERM);
2174de76b85cSMatheus Ferst         break;
2175de76b85cSMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
2176de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
2177cb76bbc4SDaniel Henrique Barboza         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
2178cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
2179cb76bbc4SDaniel Henrique Barboza         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
2180cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
2181cb76bbc4SDaniel Henrique Barboza         }
2182de76b85cSMatheus Ferst         break;
2183de76b85cSMatheus Ferst     case 0:
2184f8154fd2SBenjamin Herrenschmidt         /*
2185f8154fd2SBenjamin Herrenschmidt          * This is a bug ! It means that has_work took us out of halt without
2186f8154fd2SBenjamin Herrenschmidt          * anything to deliver while in a PM state that requires getting
2187f8154fd2SBenjamin Herrenschmidt          * out via a 0x100
2188f8154fd2SBenjamin Herrenschmidt          *
2189f8154fd2SBenjamin Herrenschmidt          * This means we will incorrectly execute past the power management
2190f8154fd2SBenjamin Herrenschmidt          * instruction instead of triggering a reset.
2191f8154fd2SBenjamin Herrenschmidt          *
2192136fbf65Szhaolichang          * It generally means a discrepancy between the wakeup conditions in the
2193f8154fd2SBenjamin Herrenschmidt          * processor has_work implementation and the logic in this function.
2194f8154fd2SBenjamin Herrenschmidt          */
2195de76b85cSMatheus Ferst         assert(!env->resume_as_sreset);
2196de76b85cSMatheus Ferst         break;
2197de76b85cSMatheus Ferst     default:
2198de76b85cSMatheus Ferst         cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
2199f8154fd2SBenjamin Herrenschmidt     }
2200c79c73f6SBlue Swirl }
220134316482SAlexey Kardashevskiy 
2202ba2898f7SMatheus Ferst static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt)
2203ba2898f7SMatheus Ferst {
2204ba2898f7SMatheus Ferst     switch (env->excp_model) {
22053654e238SMatheus Ferst #if defined(TARGET_PPC64)
22063654e238SMatheus Ferst     case POWERPC_EXCP_POWER9:
22073654e238SMatheus Ferst     case POWERPC_EXCP_POWER10:
22083654e238SMatheus Ferst         p9_deliver_interrupt(env, interrupt);
22093654e238SMatheus Ferst         break;
22103654e238SMatheus Ferst #endif
2211ba2898f7SMatheus Ferst     default:
2212ba2898f7SMatheus Ferst         ppc_deliver_interrupt_generic(env, interrupt);
2213ba2898f7SMatheus Ferst     }
2214ba2898f7SMatheus Ferst }
2215ba2898f7SMatheus Ferst 
2216b5b7f391SNicholas Piggin void ppc_cpu_do_system_reset(CPUState *cs)
221734316482SAlexey Kardashevskiy {
221834316482SAlexey Kardashevskiy     PowerPCCPU *cpu = POWERPC_CPU(cs);
221934316482SAlexey Kardashevskiy 
222093130c84SFabiano Rosas     powerpc_excp(cpu, POWERPC_EXCP_RESET);
222134316482SAlexey Kardashevskiy }
2222ad77c6caSNicholas Piggin 
2223ad77c6caSNicholas Piggin void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector)
2224ad77c6caSNicholas Piggin {
2225ad77c6caSNicholas Piggin     PowerPCCPU *cpu = POWERPC_CPU(cs);
2226ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
2227ad77c6caSNicholas Piggin     target_ulong msr = 0;
2228ad77c6caSNicholas Piggin 
2229ad77c6caSNicholas Piggin     /*
2230ad77c6caSNicholas Piggin      * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already
2231ad77c6caSNicholas Piggin      * been set by KVM.
2232ad77c6caSNicholas Piggin      */
2233ad77c6caSNicholas Piggin     msr = (1ULL << MSR_ME);
2234ad77c6caSNicholas Piggin     msr |= env->msr & (1ULL << MSR_SF);
2235516fc103SFabiano Rosas     if (ppc_interrupts_little_endian(cpu, false)) {
2236ad77c6caSNicholas Piggin         msr |= (1ULL << MSR_LE);
2237ad77c6caSNicholas Piggin     }
2238ad77c6caSNicholas Piggin 
22397cebc5dbSNicholas Piggin     /* Anything for nested required here? MSR[HV] bit? */
22407cebc5dbSNicholas Piggin 
2241ad77c6caSNicholas Piggin     powerpc_set_excp_state(cpu, vector, msr);
2242ad77c6caSNicholas Piggin }
2243c79c73f6SBlue Swirl 
2244458dd766SRichard Henderson bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
2245458dd766SRichard Henderson {
2246458dd766SRichard Henderson     PowerPCCPU *cpu = POWERPC_CPU(cs);
2247458dd766SRichard Henderson     CPUPPCState *env = &cpu->env;
2248de76b85cSMatheus Ferst     int interrupt;
2249458dd766SRichard Henderson 
2250de76b85cSMatheus Ferst     if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) {
2251de76b85cSMatheus Ferst         return false;
2252de76b85cSMatheus Ferst     }
2253de76b85cSMatheus Ferst 
2254de76b85cSMatheus Ferst     interrupt = ppc_next_unmasked_interrupt(env);
2255de76b85cSMatheus Ferst     if (interrupt == 0) {
2256de76b85cSMatheus Ferst         return false;
2257de76b85cSMatheus Ferst     }
2258de76b85cSMatheus Ferst 
2259de76b85cSMatheus Ferst     ppc_deliver_interrupt(env, interrupt);
2260458dd766SRichard Henderson     if (env->pending_interrupts == 0) {
2261de76b85cSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
2262458dd766SRichard Henderson     }
2263458dd766SRichard Henderson     return true;
2264458dd766SRichard Henderson }
2265458dd766SRichard Henderson 
2266f725245cSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
2267f725245cSPhilippe Mathieu-Daudé 
2268ad71ed68SBlue Swirl /*****************************************************************************/
2269ad71ed68SBlue Swirl /* Exceptions processing helpers */
2270ad71ed68SBlue Swirl 
2271db789c6cSBenjamin Herrenschmidt void raise_exception_err_ra(CPUPPCState *env, uint32_t exception,
2272db789c6cSBenjamin Herrenschmidt                             uint32_t error_code, uintptr_t raddr)
2273ad71ed68SBlue Swirl {
2274db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
227527103424SAndreas Färber 
227627103424SAndreas Färber     cs->exception_index = exception;
2277ad71ed68SBlue Swirl     env->error_code = error_code;
2278db789c6cSBenjamin Herrenschmidt     cpu_loop_exit_restore(cs, raddr);
2279db789c6cSBenjamin Herrenschmidt }
2280db789c6cSBenjamin Herrenschmidt 
2281db789c6cSBenjamin Herrenschmidt void raise_exception_err(CPUPPCState *env, uint32_t exception,
2282db789c6cSBenjamin Herrenschmidt                          uint32_t error_code)
2283db789c6cSBenjamin Herrenschmidt {
2284db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2285db789c6cSBenjamin Herrenschmidt }
2286db789c6cSBenjamin Herrenschmidt 
2287db789c6cSBenjamin Herrenschmidt void raise_exception(CPUPPCState *env, uint32_t exception)
2288db789c6cSBenjamin Herrenschmidt {
2289db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2290db789c6cSBenjamin Herrenschmidt }
2291db789c6cSBenjamin Herrenschmidt 
2292db789c6cSBenjamin Herrenschmidt void raise_exception_ra(CPUPPCState *env, uint32_t exception,
2293db789c6cSBenjamin Herrenschmidt                         uintptr_t raddr)
2294db789c6cSBenjamin Herrenschmidt {
2295db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, raddr);
2296db789c6cSBenjamin Herrenschmidt }
2297db789c6cSBenjamin Herrenschmidt 
22982b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2299db789c6cSBenjamin Herrenschmidt void helper_raise_exception_err(CPUPPCState *env, uint32_t exception,
2300db789c6cSBenjamin Herrenschmidt                                 uint32_t error_code)
2301db789c6cSBenjamin Herrenschmidt {
2302db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2303ad71ed68SBlue Swirl }
2304ad71ed68SBlue Swirl 
2305e5f17ac6SBlue Swirl void helper_raise_exception(CPUPPCState *env, uint32_t exception)
2306ad71ed68SBlue Swirl {
2307db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2308ad71ed68SBlue Swirl }
23092b44e219SBruno Larsen (billionai) #endif
2310ad71ed68SBlue Swirl 
2311ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY)
23122b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2313e5f17ac6SBlue Swirl void helper_store_msr(CPUPPCState *env, target_ulong val)
2314ad71ed68SBlue Swirl {
2315db789c6cSBenjamin Herrenschmidt     uint32_t excp = hreg_store_msr(env, val, 0);
2316259186a7SAndreas Färber 
2317db789c6cSBenjamin Herrenschmidt     if (excp != 0) {
2318db70b311SRichard Henderson         CPUState *cs = env_cpu(env);
2319044897efSRichard Purdie         cpu_interrupt_exittb(cs);
2320db789c6cSBenjamin Herrenschmidt         raise_exception(env, excp);
2321ad71ed68SBlue Swirl     }
2322ad71ed68SBlue Swirl }
2323ad71ed68SBlue Swirl 
23247778a575SBenjamin Herrenschmidt #if defined(TARGET_PPC64)
2325f43520e5SRichard Henderson void helper_scv(CPUPPCState *env, uint32_t lev)
2326f43520e5SRichard Henderson {
2327f43520e5SRichard Henderson     if (env->spr[SPR_FSCR] & (1ull << FSCR_SCV)) {
2328f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_SYSCALL_VECTORED, lev);
2329f43520e5SRichard Henderson     } else {
2330f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_FU, FSCR_IC_SCV);
2331f43520e5SRichard Henderson     }
2332f43520e5SRichard Henderson }
2333f43520e5SRichard Henderson 
23347778a575SBenjamin Herrenschmidt void helper_pminsn(CPUPPCState *env, powerpc_pm_insn_t insn)
23357778a575SBenjamin Herrenschmidt {
23367778a575SBenjamin Herrenschmidt     CPUState *cs;
23377778a575SBenjamin Herrenschmidt 
2338db70b311SRichard Henderson     cs = env_cpu(env);
23397778a575SBenjamin Herrenschmidt     cs->halted = 1;
23407778a575SBenjamin Herrenschmidt 
23413621e2c9SBenjamin Herrenschmidt     /* Condition for waking up at 0x100 */
23421e7fd61dSBenjamin Herrenschmidt     env->resume_as_sreset = (insn != PPC_PM_STOP) ||
234321c0d66aSBenjamin Herrenschmidt         (env->spr[SPR_PSSCR] & PSSCR_EC);
23447778a575SBenjamin Herrenschmidt }
23457778a575SBenjamin Herrenschmidt #endif /* defined(TARGET_PPC64) */
23467778a575SBenjamin Herrenschmidt 
234762e79ef9SCédric Le Goater static void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr)
2348ad71ed68SBlue Swirl {
2349db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
2350259186a7SAndreas Färber 
2351a2e71b28SBenjamin Herrenschmidt     /* MSR:POW cannot be set by any form of rfi */
2352a2e71b28SBenjamin Herrenschmidt     msr &= ~(1ULL << MSR_POW);
2353a2e71b28SBenjamin Herrenschmidt 
23545aad0457SChristophe Leroy     /* MSR:TGPR cannot be set by any form of rfi */
23555aad0457SChristophe Leroy     if (env->flags & POWERPC_FLAG_TGPR)
23565aad0457SChristophe Leroy         msr &= ~(1ULL << MSR_TGPR);
23575aad0457SChristophe Leroy 
2358ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2359a2e71b28SBenjamin Herrenschmidt     /* Switching to 32-bit ? Crop the nip */
2360a2e71b28SBenjamin Herrenschmidt     if (!msr_is_64bit(env, msr)) {
2361ad71ed68SBlue Swirl         nip = (uint32_t)nip;
2362ad71ed68SBlue Swirl     }
2363ad71ed68SBlue Swirl #else
2364ad71ed68SBlue Swirl     nip = (uint32_t)nip;
2365ad71ed68SBlue Swirl #endif
2366ad71ed68SBlue Swirl     /* XXX: beware: this is false if VLE is supported */
2367ad71ed68SBlue Swirl     env->nip = nip & ~((target_ulong)0x00000003);
2368ad71ed68SBlue Swirl     hreg_store_msr(env, msr, 1);
23692eb1ef73SCédric Le Goater     trace_ppc_excp_rfi(env->nip, env->msr);
237047733729SDavid Gibson     /*
237147733729SDavid Gibson      * No need to raise an exception here, as rfi is always the last
237247733729SDavid Gibson      * insn of a TB
2373ad71ed68SBlue Swirl      */
2374044897efSRichard Purdie     cpu_interrupt_exittb(cs);
2375a8b73734SNikunj A Dadhania     /* Reset the reservation */
2376a8b73734SNikunj A Dadhania     env->reserve_addr = -1;
2377a8b73734SNikunj A Dadhania 
2378cd0c6f47SBenjamin Herrenschmidt     /* Context synchronizing: check if TCG TLB needs flush */
2379e3cffe6fSNikunj A Dadhania     check_tlb_flush(env, false);
2380ad71ed68SBlue Swirl }
2381ad71ed68SBlue Swirl 
2382e5f17ac6SBlue Swirl void helper_rfi(CPUPPCState *env)
2383ad71ed68SBlue Swirl {
2384a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful);
2385a1bb7384SScott Wood }
2386ad71ed68SBlue Swirl 
2387ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2388e5f17ac6SBlue Swirl void helper_rfid(CPUPPCState *env)
2389ad71ed68SBlue Swirl {
239047733729SDavid Gibson     /*
2391136fbf65Szhaolichang      * The architecture defines a number of rules for which bits can
239247733729SDavid Gibson      * change but in practice, we handle this in hreg_store_msr()
2393a2e71b28SBenjamin Herrenschmidt      * which will be called by do_rfi(), so there is no need to filter
2394a2e71b28SBenjamin Herrenschmidt      * here
2395a2e71b28SBenjamin Herrenschmidt      */
2396a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]);
2397ad71ed68SBlue Swirl }
2398ad71ed68SBlue Swirl 
23993c89b8d6SNicholas Piggin void helper_rfscv(CPUPPCState *env)
24003c89b8d6SNicholas Piggin {
24013c89b8d6SNicholas Piggin     do_rfi(env, env->lr, env->ctr);
24023c89b8d6SNicholas Piggin }
24033c89b8d6SNicholas Piggin 
2404e5f17ac6SBlue Swirl void helper_hrfid(CPUPPCState *env)
2405ad71ed68SBlue Swirl {
2406a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
2407ad71ed68SBlue Swirl }
2408ad71ed68SBlue Swirl #endif
2409ad71ed68SBlue Swirl 
24101f26c751SDaniel Henrique Barboza #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
24111f26c751SDaniel Henrique Barboza void helper_rfebb(CPUPPCState *env, target_ulong s)
24121f26c751SDaniel Henrique Barboza {
24131f26c751SDaniel Henrique Barboza     target_ulong msr = env->msr;
24141f26c751SDaniel Henrique Barboza 
24151f26c751SDaniel Henrique Barboza     /*
24161f26c751SDaniel Henrique Barboza      * Handling of BESCR bits 32:33 according to PowerISA v3.1:
24171f26c751SDaniel Henrique Barboza      *
24181f26c751SDaniel Henrique Barboza      * "If BESCR 32:33 != 0b00 the instruction is treated as if
24191f26c751SDaniel Henrique Barboza      *  the instruction form were invalid."
24201f26c751SDaniel Henrique Barboza      */
24211f26c751SDaniel Henrique Barboza     if (env->spr[SPR_BESCR] & BESCR_INVALID) {
24221f26c751SDaniel Henrique Barboza         raise_exception_err(env, POWERPC_EXCP_PROGRAM,
24231f26c751SDaniel Henrique Barboza                             POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
24241f26c751SDaniel Henrique Barboza     }
24251f26c751SDaniel Henrique Barboza 
24261f26c751SDaniel Henrique Barboza     env->nip = env->spr[SPR_EBBRR];
24271f26c751SDaniel Henrique Barboza 
24281f26c751SDaniel Henrique Barboza     /* Switching to 32-bit ? Crop the nip */
24291f26c751SDaniel Henrique Barboza     if (!msr_is_64bit(env, msr)) {
24301f26c751SDaniel Henrique Barboza         env->nip = (uint32_t)env->spr[SPR_EBBRR];
24311f26c751SDaniel Henrique Barboza     }
24321f26c751SDaniel Henrique Barboza 
24331f26c751SDaniel Henrique Barboza     if (s) {
24341f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_GE;
24351f26c751SDaniel Henrique Barboza     } else {
24361f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
24371f26c751SDaniel Henrique Barboza     }
24381f26c751SDaniel Henrique Barboza }
2439d3412df2SDaniel Henrique Barboza 
2440d3412df2SDaniel Henrique Barboza /*
2441d3412df2SDaniel Henrique Barboza  * Triggers or queues an 'ebb_excp' EBB exception. All checks
2442d3412df2SDaniel Henrique Barboza  * but FSCR, HFSCR and msr_pr must be done beforehand.
2443d3412df2SDaniel Henrique Barboza  *
2444d3412df2SDaniel Henrique Barboza  * PowerISA v3.1 isn't clear about whether an EBB should be
2445d3412df2SDaniel Henrique Barboza  * postponed or cancelled if the EBB facility is unavailable.
2446d3412df2SDaniel Henrique Barboza  * Our assumption here is that the EBB is cancelled if both
2447d3412df2SDaniel Henrique Barboza  * FSCR and HFSCR EBB facilities aren't available.
2448d3412df2SDaniel Henrique Barboza  */
2449d3412df2SDaniel Henrique Barboza static void do_ebb(CPUPPCState *env, int ebb_excp)
2450d3412df2SDaniel Henrique Barboza {
2451d3412df2SDaniel Henrique Barboza     PowerPCCPU *cpu = env_archcpu(env);
2452d3412df2SDaniel Henrique Barboza 
2453d3412df2SDaniel Henrique Barboza     /*
2454d3412df2SDaniel Henrique Barboza      * FSCR_EBB and FSCR_IC_EBB are the same bits used with
2455d3412df2SDaniel Henrique Barboza      * HFSCR.
2456d3412df2SDaniel Henrique Barboza      */
2457d3412df2SDaniel Henrique Barboza     helper_fscr_facility_check(env, FSCR_EBB, 0, FSCR_IC_EBB);
2458d3412df2SDaniel Henrique Barboza     helper_hfscr_facility_check(env, FSCR_EBB, "EBB", FSCR_IC_EBB);
2459d3412df2SDaniel Henrique Barboza 
2460d3412df2SDaniel Henrique Barboza     if (ebb_excp == POWERPC_EXCP_PERFM_EBB) {
2461d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_PMEO;
2462d3412df2SDaniel Henrique Barboza     } else if (ebb_excp == POWERPC_EXCP_EXTERNAL_EBB) {
2463d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_EEO;
2464d3412df2SDaniel Henrique Barboza     }
2465d3412df2SDaniel Henrique Barboza 
2466d41ccf6eSVíctor Colombo     if (FIELD_EX64(env->msr, MSR, PR)) {
2467d3412df2SDaniel Henrique Barboza         powerpc_excp(cpu, ebb_excp);
2468d3412df2SDaniel Henrique Barboza     } else {
24697b694df6SMatheus Ferst         ppc_set_irq(cpu, PPC_INTERRUPT_EBB, 1);
2470d3412df2SDaniel Henrique Barboza     }
2471d3412df2SDaniel Henrique Barboza }
2472d3412df2SDaniel Henrique Barboza 
2473d3412df2SDaniel Henrique Barboza void raise_ebb_perfm_exception(CPUPPCState *env)
2474d3412df2SDaniel Henrique Barboza {
2475d3412df2SDaniel Henrique Barboza     bool perfm_ebb_enabled = env->spr[SPR_POWER_MMCR0] & MMCR0_EBE &&
2476d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_PME &&
2477d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_GE;
2478d3412df2SDaniel Henrique Barboza 
2479d3412df2SDaniel Henrique Barboza     if (!perfm_ebb_enabled) {
2480d3412df2SDaniel Henrique Barboza         return;
2481d3412df2SDaniel Henrique Barboza     }
2482d3412df2SDaniel Henrique Barboza 
2483d3412df2SDaniel Henrique Barboza     do_ebb(env, POWERPC_EXCP_PERFM_EBB);
2484d3412df2SDaniel Henrique Barboza }
24851f26c751SDaniel Henrique Barboza #endif
24861f26c751SDaniel Henrique Barboza 
2487ad71ed68SBlue Swirl /*****************************************************************************/
2488ad71ed68SBlue Swirl /* Embedded PowerPC specific helpers */
2489e5f17ac6SBlue Swirl void helper_40x_rfci(CPUPPCState *env)
2490ad71ed68SBlue Swirl {
2491a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]);
2492ad71ed68SBlue Swirl }
2493ad71ed68SBlue Swirl 
2494e5f17ac6SBlue Swirl void helper_rfci(CPUPPCState *env)
2495ad71ed68SBlue Swirl {
2496a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]);
2497ad71ed68SBlue Swirl }
2498ad71ed68SBlue Swirl 
2499e5f17ac6SBlue Swirl void helper_rfdi(CPUPPCState *env)
2500ad71ed68SBlue Swirl {
2501a1bb7384SScott Wood     /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
2502a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]);
2503ad71ed68SBlue Swirl }
2504ad71ed68SBlue Swirl 
2505e5f17ac6SBlue Swirl void helper_rfmci(CPUPPCState *env)
2506ad71ed68SBlue Swirl {
2507a1bb7384SScott Wood     /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
2508a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
2509ad71ed68SBlue Swirl }
25102b44e219SBruno Larsen (billionai) #endif /* CONFIG_TCG */
25112b44e219SBruno Larsen (billionai) #endif /* !defined(CONFIG_USER_ONLY) */
2512ad71ed68SBlue Swirl 
25132b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2514e5f17ac6SBlue Swirl void helper_tw(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2515e5f17ac6SBlue Swirl                uint32_t flags)
2516ad71ed68SBlue Swirl {
2517ad71ed68SBlue Swirl     if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
2518ad71ed68SBlue Swirl                   ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
2519ad71ed68SBlue Swirl                   ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
2520ad71ed68SBlue Swirl                   ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
2521ad71ed68SBlue Swirl                   ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
252272073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
252372073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2524ad71ed68SBlue Swirl     }
2525ad71ed68SBlue Swirl }
2526ad71ed68SBlue Swirl 
2527ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2528e5f17ac6SBlue Swirl void helper_td(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2529e5f17ac6SBlue Swirl                uint32_t flags)
2530ad71ed68SBlue Swirl {
2531ad71ed68SBlue Swirl     if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
2532ad71ed68SBlue Swirl                   ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
2533ad71ed68SBlue Swirl                   ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
2534ad71ed68SBlue Swirl                   ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
2535ad71ed68SBlue Swirl                   ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) {
253672073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
253772073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2538ad71ed68SBlue Swirl     }
2539ad71ed68SBlue Swirl }
2540ad71ed68SBlue Swirl #endif
25412b44e219SBruno Larsen (billionai) #endif
2542ad71ed68SBlue Swirl 
2543670f1da3SVíctor Colombo static uint32_t helper_SIMON_LIKE_32_64(uint32_t x, uint64_t key, uint32_t lane)
2544670f1da3SVíctor Colombo {
2545670f1da3SVíctor Colombo     const uint16_t c = 0xfffc;
2546670f1da3SVíctor Colombo     const uint64_t z0 = 0xfa2561cdf44ac398ULL;
2547670f1da3SVíctor Colombo     uint16_t z = 0, temp;
2548670f1da3SVíctor Colombo     uint16_t k[32], eff_k[32], xleft[33], xright[33], fxleft[32];
2549670f1da3SVíctor Colombo 
2550670f1da3SVíctor Colombo     for (int i = 3; i >= 0; i--) {
2551670f1da3SVíctor Colombo         k[i] = key & 0xffff;
2552670f1da3SVíctor Colombo         key >>= 16;
2553670f1da3SVíctor Colombo     }
2554670f1da3SVíctor Colombo     xleft[0] = x & 0xffff;
2555670f1da3SVíctor Colombo     xright[0] = (x >> 16) & 0xffff;
2556670f1da3SVíctor Colombo 
2557670f1da3SVíctor Colombo     for (int i = 0; i < 28; i++) {
2558670f1da3SVíctor Colombo         z = (z0 >> (63 - i)) & 1;
2559670f1da3SVíctor Colombo         temp = ror16(k[i + 3], 3) ^ k[i + 1];
2560670f1da3SVíctor Colombo         k[i + 4] = c ^ z ^ k[i] ^ temp ^ ror16(temp, 1);
2561670f1da3SVíctor Colombo     }
2562670f1da3SVíctor Colombo 
2563670f1da3SVíctor Colombo     for (int i = 0; i < 8; i++) {
2564670f1da3SVíctor Colombo         eff_k[4 * i + 0] = k[4 * i + ((0 + lane) % 4)];
2565670f1da3SVíctor Colombo         eff_k[4 * i + 1] = k[4 * i + ((1 + lane) % 4)];
2566670f1da3SVíctor Colombo         eff_k[4 * i + 2] = k[4 * i + ((2 + lane) % 4)];
2567670f1da3SVíctor Colombo         eff_k[4 * i + 3] = k[4 * i + ((3 + lane) % 4)];
2568670f1da3SVíctor Colombo     }
2569670f1da3SVíctor Colombo 
2570670f1da3SVíctor Colombo     for (int i = 0; i < 32; i++) {
2571670f1da3SVíctor Colombo         fxleft[i] = (rol16(xleft[i], 1) &
2572670f1da3SVíctor Colombo             rol16(xleft[i], 8)) ^ rol16(xleft[i], 2);
2573670f1da3SVíctor Colombo         xleft[i + 1] = xright[i] ^ fxleft[i] ^ eff_k[i];
2574670f1da3SVíctor Colombo         xright[i + 1] = xleft[i];
2575670f1da3SVíctor Colombo     }
2576670f1da3SVíctor Colombo 
2577670f1da3SVíctor Colombo     return (((uint32_t)xright[32]) << 16) | xleft[32];
2578670f1da3SVíctor Colombo }
2579670f1da3SVíctor Colombo 
2580670f1da3SVíctor Colombo static uint64_t hash_digest(uint64_t ra, uint64_t rb, uint64_t key)
2581670f1da3SVíctor Colombo {
2582670f1da3SVíctor Colombo     uint64_t stage0_h = 0ULL, stage0_l = 0ULL;
2583670f1da3SVíctor Colombo     uint64_t stage1_h, stage1_l;
2584670f1da3SVíctor Colombo 
2585670f1da3SVíctor Colombo     for (int i = 0; i < 4; i++) {
2586670f1da3SVíctor Colombo         stage0_h |= ror64(rb & 0xff, 8 * (2 * i + 1));
2587670f1da3SVíctor Colombo         stage0_h |= ((ra >> 32) & 0xff) << (8 * 2 * i);
2588670f1da3SVíctor Colombo         stage0_l |= ror64((rb >> 32) & 0xff, 8 * (2 * i + 1));
2589670f1da3SVíctor Colombo         stage0_l |= (ra & 0xff) << (8 * 2 * i);
2590670f1da3SVíctor Colombo         rb >>= 8;
2591670f1da3SVíctor Colombo         ra >>= 8;
2592670f1da3SVíctor Colombo     }
2593670f1da3SVíctor Colombo 
2594670f1da3SVíctor Colombo     stage1_h = (uint64_t)helper_SIMON_LIKE_32_64(stage0_h >> 32, key, 0) << 32;
2595670f1da3SVíctor Colombo     stage1_h |= helper_SIMON_LIKE_32_64(stage0_h, key, 1);
2596670f1da3SVíctor Colombo     stage1_l = (uint64_t)helper_SIMON_LIKE_32_64(stage0_l >> 32, key, 2) << 32;
2597670f1da3SVíctor Colombo     stage1_l |= helper_SIMON_LIKE_32_64(stage0_l, key, 3);
2598670f1da3SVíctor Colombo 
2599670f1da3SVíctor Colombo     return stage1_h ^ stage1_l;
2600670f1da3SVíctor Colombo }
2601670f1da3SVíctor Colombo 
2602670f1da3SVíctor Colombo #include "qemu/guest-random.h"
2603670f1da3SVíctor Colombo 
2604670f1da3SVíctor Colombo #define HELPER_HASH(op, key, store)                                           \
2605670f1da3SVíctor Colombo void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
2606670f1da3SVíctor Colombo                  target_ulong rb)                                             \
2607670f1da3SVíctor Colombo {                                                                             \
2608670f1da3SVíctor Colombo     uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash;         \
2609670f1da3SVíctor Colombo                                                                               \
2610670f1da3SVíctor Colombo     if (store) {                                                              \
2611670f1da3SVíctor Colombo         cpu_stq_data_ra(env, ea, calculated_hash, GETPC());                   \
2612670f1da3SVíctor Colombo     } else {                                                                  \
2613670f1da3SVíctor Colombo         loaded_hash = cpu_ldq_data_ra(env, ea, GETPC());                      \
2614670f1da3SVíctor Colombo         if (loaded_hash != calculated_hash) {                                 \
2615670f1da3SVíctor Colombo             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,                 \
2616670f1da3SVíctor Colombo                 POWERPC_EXCP_TRAP, GETPC());                                  \
2617670f1da3SVíctor Colombo         }                                                                     \
2618670f1da3SVíctor Colombo     }                                                                         \
2619670f1da3SVíctor Colombo }
2620670f1da3SVíctor Colombo 
2621670f1da3SVíctor Colombo HELPER_HASH(HASHST, env->spr[SPR_HASHKEYR], true)
2622670f1da3SVíctor Colombo HELPER_HASH(HASHCHK, env->spr[SPR_HASHKEYR], false)
262353ae2aebSVíctor Colombo HELPER_HASH(HASHSTP, env->spr[SPR_HASHPKEYR], true)
262453ae2aebSVíctor Colombo HELPER_HASH(HASHCHKP, env->spr[SPR_HASHPKEYR], false)
2625670f1da3SVíctor Colombo 
2626ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY)
2627ad71ed68SBlue Swirl 
26282b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2629ad71ed68SBlue Swirl 
2630ad71ed68SBlue Swirl /* Embedded.Processor Control */
2631ad71ed68SBlue Swirl static int dbell2irq(target_ulong rb)
2632ad71ed68SBlue Swirl {
2633ad71ed68SBlue Swirl     int msg = rb & DBELL_TYPE_MASK;
2634ad71ed68SBlue Swirl     int irq = -1;
2635ad71ed68SBlue Swirl 
2636ad71ed68SBlue Swirl     switch (msg) {
2637ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL:
2638ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_DOORBELL;
2639ad71ed68SBlue Swirl         break;
2640ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL_CRIT:
2641ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_CDOORBELL;
2642ad71ed68SBlue Swirl         break;
2643ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL:
2644ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_CRIT:
2645ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_MC:
2646ad71ed68SBlue Swirl         /* XXX implement */
2647ad71ed68SBlue Swirl     default:
2648ad71ed68SBlue Swirl         break;
2649ad71ed68SBlue Swirl     }
2650ad71ed68SBlue Swirl 
2651ad71ed68SBlue Swirl     return irq;
2652ad71ed68SBlue Swirl }
2653ad71ed68SBlue Swirl 
2654e5f17ac6SBlue Swirl void helper_msgclr(CPUPPCState *env, target_ulong rb)
2655ad71ed68SBlue Swirl {
2656ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2657ad71ed68SBlue Swirl 
2658ad71ed68SBlue Swirl     if (irq < 0) {
2659ad71ed68SBlue Swirl         return;
2660ad71ed68SBlue Swirl     }
2661ad71ed68SBlue Swirl 
26627b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), irq, 0);
2663ad71ed68SBlue Swirl }
2664ad71ed68SBlue Swirl 
2665ad71ed68SBlue Swirl void helper_msgsnd(target_ulong rb)
2666ad71ed68SBlue Swirl {
2667ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2668ad71ed68SBlue Swirl     int pir = rb & DBELL_PIRTAG_MASK;
2669182735efSAndreas Färber     CPUState *cs;
2670ad71ed68SBlue Swirl 
2671ad71ed68SBlue Swirl     if (irq < 0) {
2672ad71ed68SBlue Swirl         return;
2673ad71ed68SBlue Swirl     }
2674ad71ed68SBlue Swirl 
2675f1c29ebcSThomas Huth     qemu_mutex_lock_iothread();
2676bdc44640SAndreas Färber     CPU_FOREACH(cs) {
2677182735efSAndreas Färber         PowerPCCPU *cpu = POWERPC_CPU(cs);
2678182735efSAndreas Färber         CPUPPCState *cenv = &cpu->env;
2679182735efSAndreas Färber 
2680ad71ed68SBlue Swirl         if ((rb & DBELL_BRDCAST) || (cenv->spr[SPR_BOOKE_PIR] == pir)) {
26817b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
2682ad71ed68SBlue Swirl         }
2683ad71ed68SBlue Swirl     }
2684f1c29ebcSThomas Huth     qemu_mutex_unlock_iothread();
2685ad71ed68SBlue Swirl }
26867af1e7b0SCédric Le Goater 
26877af1e7b0SCédric Le Goater /* Server Processor Control */
26887af1e7b0SCédric Le Goater 
26895ba7ba1dSCédric Le Goater static bool dbell_type_server(target_ulong rb)
26905ba7ba1dSCédric Le Goater {
269147733729SDavid Gibson     /*
269247733729SDavid Gibson      * A Directed Hypervisor Doorbell message is sent only if the
26937af1e7b0SCédric Le Goater      * message type is 5. All other types are reserved and the
269447733729SDavid Gibson      * instruction is a no-op
269547733729SDavid Gibson      */
26965ba7ba1dSCédric Le Goater     return (rb & DBELL_TYPE_MASK) == DBELL_TYPE_DBELL_SERVER;
26977af1e7b0SCédric Le Goater }
26987af1e7b0SCédric Le Goater 
26997af1e7b0SCédric Le Goater void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb)
27007af1e7b0SCédric Le Goater {
27015ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
27027af1e7b0SCédric Le Goater         return;
27037af1e7b0SCédric Le Goater     }
27047af1e7b0SCédric Le Goater 
27057b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
27067af1e7b0SCédric Le Goater }
27077af1e7b0SCédric Le Goater 
27085ba7ba1dSCédric Le Goater static void book3s_msgsnd_common(int pir, int irq)
27097af1e7b0SCédric Le Goater {
27107af1e7b0SCédric Le Goater     CPUState *cs;
27117af1e7b0SCédric Le Goater 
27127af1e7b0SCédric Le Goater     qemu_mutex_lock_iothread();
27137af1e7b0SCédric Le Goater     CPU_FOREACH(cs) {
27147af1e7b0SCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
27157af1e7b0SCédric Le Goater         CPUPPCState *cenv = &cpu->env;
27167af1e7b0SCédric Le Goater 
27177af1e7b0SCédric Le Goater         /* TODO: broadcast message to all threads of the same  processor */
27187af1e7b0SCédric Le Goater         if (cenv->spr_cb[SPR_PIR].default_value == pir) {
27197b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
27207af1e7b0SCédric Le Goater         }
27217af1e7b0SCédric Le Goater     }
27227af1e7b0SCédric Le Goater     qemu_mutex_unlock_iothread();
27237af1e7b0SCédric Le Goater }
27245ba7ba1dSCédric Le Goater 
27255ba7ba1dSCédric Le Goater void helper_book3s_msgsnd(target_ulong rb)
27265ba7ba1dSCédric Le Goater {
27275ba7ba1dSCédric Le Goater     int pir = rb & DBELL_PROCIDTAG_MASK;
27285ba7ba1dSCédric Le Goater 
27295ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
27305ba7ba1dSCédric Le Goater         return;
27315ba7ba1dSCédric Le Goater     }
27325ba7ba1dSCédric Le Goater 
27335ba7ba1dSCédric Le Goater     book3s_msgsnd_common(pir, PPC_INTERRUPT_HDOORBELL);
27345ba7ba1dSCédric Le Goater }
27355ba7ba1dSCédric Le Goater 
27365ba7ba1dSCédric Le Goater #if defined(TARGET_PPC64)
27375ba7ba1dSCédric Le Goater void helper_book3s_msgclrp(CPUPPCState *env, target_ulong rb)
27385ba7ba1dSCédric Le Goater {
2739493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgclrp", HFSCR_IC_MSGP);
2740493028d8SCédric Le Goater 
27415ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
27425ba7ba1dSCédric Le Goater         return;
27435ba7ba1dSCédric Le Goater     }
27445ba7ba1dSCédric Le Goater 
27457b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
27465ba7ba1dSCédric Le Goater }
27475ba7ba1dSCédric Le Goater 
27485ba7ba1dSCédric Le Goater /*
27495ba7ba1dSCédric Le Goater  * sends a message to other threads that are on the same
27505ba7ba1dSCédric Le Goater  * multi-threaded processor
27515ba7ba1dSCédric Le Goater  */
27525ba7ba1dSCédric Le Goater void helper_book3s_msgsndp(CPUPPCState *env, target_ulong rb)
27535ba7ba1dSCédric Le Goater {
27545ba7ba1dSCédric Le Goater     int pir = env->spr_cb[SPR_PIR].default_value;
27555ba7ba1dSCédric Le Goater 
2756493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgsndp", HFSCR_IC_MSGP);
2757493028d8SCédric Le Goater 
27585ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
27595ba7ba1dSCédric Le Goater         return;
27605ba7ba1dSCédric Le Goater     }
27615ba7ba1dSCédric Le Goater 
27625ba7ba1dSCédric Le Goater     /* TODO: TCG supports only one thread */
27635ba7ba1dSCédric Le Goater 
27645ba7ba1dSCédric Le Goater     book3s_msgsnd_common(pir, PPC_INTERRUPT_DOORBELL);
27655ba7ba1dSCédric Le Goater }
2766996473e4SRichard Henderson #endif /* TARGET_PPC64 */
27670f3110faSRichard Henderson 
27680f3110faSRichard Henderson void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
27690f3110faSRichard Henderson                                  MMUAccessType access_type,
27700f3110faSRichard Henderson                                  int mmu_idx, uintptr_t retaddr)
27710f3110faSRichard Henderson {
27720f3110faSRichard Henderson     CPUPPCState *env = cs->env_ptr;
277329c4a336SFabiano Rosas     uint32_t insn;
277429c4a336SFabiano Rosas 
277529c4a336SFabiano Rosas     /* Restore state and reload the insn we executed, for filling in DSISR.  */
277629c4a336SFabiano Rosas     cpu_restore_state(cs, retaddr, true);
277729c4a336SFabiano Rosas     insn = cpu_ldl_code(env, env->nip);
27780f3110faSRichard Henderson 
2779a7e3af13SRichard Henderson     switch (env->mmu_model) {
2780a7e3af13SRichard Henderson     case POWERPC_MMU_SOFT_4xx:
2781a7e3af13SRichard Henderson         env->spr[SPR_40x_DEAR] = vaddr;
2782a7e3af13SRichard Henderson         break;
2783a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE:
2784a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE206:
2785a7e3af13SRichard Henderson         env->spr[SPR_BOOKE_DEAR] = vaddr;
2786a7e3af13SRichard Henderson         break;
2787a7e3af13SRichard Henderson     default:
2788a7e3af13SRichard Henderson         env->spr[SPR_DAR] = vaddr;
2789a7e3af13SRichard Henderson         break;
2790a7e3af13SRichard Henderson     }
2791a7e3af13SRichard Henderson 
27920f3110faSRichard Henderson     cs->exception_index = POWERPC_EXCP_ALIGN;
279329c4a336SFabiano Rosas     env->error_code = insn & 0x03FF0000;
279429c4a336SFabiano Rosas     cpu_loop_exit(cs);
27950f3110faSRichard Henderson }
2796996473e4SRichard Henderson #endif /* CONFIG_TCG */
2797996473e4SRichard Henderson #endif /* !CONFIG_USER_ONLY */
2798