xref: /qemu/target/ppc/excp_helper.c (revision 3654e238af02fb828b71aa85a4fc475770a3d91a)
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)
1688b00e9a2fSMatheus Ferst #define P9_UNUSED_INTERRUPTS \
1689b00e9a2fSMatheus Ferst     (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT |   \
1690b00e9a2fSMatheus Ferst      PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT |  \
1691b00e9a2fSMatheus Ferst      PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1692b00e9a2fSMatheus Ferst 
16932dfecf01SMatheus Ferst static int p9_next_unmasked_interrupt(CPUPPCState *env)
16942dfecf01SMatheus Ferst {
16952dfecf01SMatheus Ferst     bool async_deliver;
16962dfecf01SMatheus Ferst 
1697b00e9a2fSMatheus Ferst     assert((env->pending_interrupts & P9_UNUSED_INTERRUPTS) == 0);
1698b00e9a2fSMatheus Ferst 
16992dfecf01SMatheus Ferst     /* Machine check exception */
17002dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
17012dfecf01SMatheus Ferst         return PPC_INTERRUPT_MCK;
17022dfecf01SMatheus Ferst     }
17032dfecf01SMatheus Ferst 
17042dfecf01SMatheus Ferst     /*
17052dfecf01SMatheus Ferst      * For interrupts that gate on MSR:EE, we need to do something a
17062dfecf01SMatheus Ferst      * bit more subtle, as we need to let them through even when EE is
17072dfecf01SMatheus Ferst      * clear when coming out of some power management states (in order
17082dfecf01SMatheus Ferst      * for them to become a 0x100).
17092dfecf01SMatheus Ferst      */
17102dfecf01SMatheus Ferst     async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
17112dfecf01SMatheus Ferst 
17122dfecf01SMatheus Ferst     /* Hypervisor decrementer exception */
17132dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
17142dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
17152dfecf01SMatheus Ferst         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
17162dfecf01SMatheus Ferst         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
17172dfecf01SMatheus Ferst             /* HDEC clears on delivery */
17182dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDECR;
17192dfecf01SMatheus Ferst         }
17202dfecf01SMatheus Ferst     }
17212dfecf01SMatheus Ferst 
17222dfecf01SMatheus Ferst     /* Hypervisor virtualization interrupt */
17232dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
17242dfecf01SMatheus Ferst         /* LPCR will be clear when not supported so this will work */
17252dfecf01SMatheus Ferst         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
17262dfecf01SMatheus Ferst         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
17272dfecf01SMatheus Ferst             return PPC_INTERRUPT_HVIRT;
17282dfecf01SMatheus Ferst         }
17292dfecf01SMatheus Ferst     }
17302dfecf01SMatheus Ferst 
17312dfecf01SMatheus Ferst     /* External interrupt can ignore MSR:EE under some circumstances */
17322dfecf01SMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
17332dfecf01SMatheus Ferst         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
17342dfecf01SMatheus Ferst         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
17352dfecf01SMatheus Ferst         /* HEIC blocks delivery to the hypervisor */
17362dfecf01SMatheus Ferst         if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
17372dfecf01SMatheus Ferst             !FIELD_EX64(env->msr, MSR, PR))) ||
17382dfecf01SMatheus Ferst             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
17392dfecf01SMatheus Ferst             return PPC_INTERRUPT_EXT;
17402dfecf01SMatheus Ferst         }
17412dfecf01SMatheus Ferst     }
17422dfecf01SMatheus Ferst     if (async_deliver != 0) {
17432dfecf01SMatheus Ferst         /* Decrementer exception */
17442dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
17452dfecf01SMatheus Ferst             return PPC_INTERRUPT_DECR;
17462dfecf01SMatheus Ferst         }
17472dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
17482dfecf01SMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
17492dfecf01SMatheus Ferst         }
17502dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
17512dfecf01SMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
17522dfecf01SMatheus Ferst         }
17532dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
17542dfecf01SMatheus Ferst             return PPC_INTERRUPT_PERFM;
17552dfecf01SMatheus Ferst         }
17562dfecf01SMatheus Ferst         /* EBB exception */
17572dfecf01SMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
17582dfecf01SMatheus Ferst             /*
17592dfecf01SMatheus Ferst              * EBB exception must be taken in problem state and
17602dfecf01SMatheus Ferst              * with BESCR_GE set.
17612dfecf01SMatheus Ferst              */
17622dfecf01SMatheus Ferst             if (FIELD_EX64(env->msr, MSR, PR) &&
17632dfecf01SMatheus Ferst                 (env->spr[SPR_BESCR] & BESCR_GE)) {
17642dfecf01SMatheus Ferst                 return PPC_INTERRUPT_EBB;
17652dfecf01SMatheus Ferst             }
17662dfecf01SMatheus Ferst         }
17672dfecf01SMatheus Ferst     }
17682dfecf01SMatheus Ferst 
17692dfecf01SMatheus Ferst     return 0;
17702dfecf01SMatheus Ferst }
17712dfecf01SMatheus Ferst #endif
17722dfecf01SMatheus Ferst 
1773ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt_generic(CPUPPCState *env)
1774c79c73f6SBlue Swirl {
17753621e2c9SBenjamin Herrenschmidt     bool async_deliver;
1776259186a7SAndreas Färber 
1777c79c73f6SBlue Swirl     /* External reset */
1778f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
1779de76b85cSMatheus Ferst         return PPC_INTERRUPT_RESET;
1780c79c73f6SBlue Swirl     }
1781c79c73f6SBlue Swirl     /* Machine check exception */
1782f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1783de76b85cSMatheus Ferst         return PPC_INTERRUPT_MCK;
1784c79c73f6SBlue Swirl     }
1785c79c73f6SBlue Swirl #if 0 /* TODO */
1786c79c73f6SBlue Swirl     /* External debug exception */
1787f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) {
1788de76b85cSMatheus Ferst         return PPC_INTERRUPT_DEBUG;
1789c79c73f6SBlue Swirl     }
1790c79c73f6SBlue Swirl #endif
17913621e2c9SBenjamin Herrenschmidt 
17923621e2c9SBenjamin Herrenschmidt     /*
17933621e2c9SBenjamin Herrenschmidt      * For interrupts that gate on MSR:EE, we need to do something a
17943621e2c9SBenjamin Herrenschmidt      * bit more subtle, as we need to let them through even when EE is
17953621e2c9SBenjamin Herrenschmidt      * clear when coming out of some power management states (in order
17963621e2c9SBenjamin Herrenschmidt      * for them to become a 0x100).
17973621e2c9SBenjamin Herrenschmidt      */
17980939b8f8SVíctor Colombo     async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
17993621e2c9SBenjamin Herrenschmidt 
1800c79c73f6SBlue Swirl     /* Hypervisor decrementer exception */
1801f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
18024b236b62SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
18034b236b62SBenjamin Herrenschmidt         bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
18049de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
18054b236b62SBenjamin Herrenschmidt             /* HDEC clears on delivery */
1806de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDECR;
1807c79c73f6SBlue Swirl         }
1808c79c73f6SBlue Swirl     }
1809d8ce5fd6SBenjamin Herrenschmidt 
1810d8ce5fd6SBenjamin Herrenschmidt     /* Hypervisor virtualization interrupt */
1811f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
1812d8ce5fd6SBenjamin Herrenschmidt         /* LPCR will be clear when not supported so this will work */
1813d8ce5fd6SBenjamin Herrenschmidt         bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
18149de754d3SVíctor Colombo         if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
1815de76b85cSMatheus Ferst             return PPC_INTERRUPT_HVIRT;
1816d8ce5fd6SBenjamin Herrenschmidt         }
1817d8ce5fd6SBenjamin Herrenschmidt     }
1818d8ce5fd6SBenjamin Herrenschmidt 
1819d8ce5fd6SBenjamin Herrenschmidt     /* External interrupt can ignore MSR:EE under some circumstances */
1820f003109fSMatheus Ferst     if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1821d1dbe37cSBenjamin Herrenschmidt         bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
18226eebe6dcSBenjamin Herrenschmidt         bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
18236eebe6dcSBenjamin Herrenschmidt         /* HEIC blocks delivery to the hypervisor */
18249de754d3SVíctor Colombo         if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
1825d41ccf6eSVíctor Colombo             !FIELD_EX64(env->msr, MSR, PR))) ||
18269de754d3SVíctor Colombo             (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1827de76b85cSMatheus Ferst             return PPC_INTERRUPT_EXT;
1828d1dbe37cSBenjamin Herrenschmidt         }
1829d1dbe37cSBenjamin Herrenschmidt     }
1830acc861c2SVíctor Colombo     if (FIELD_EX64(env->msr, MSR, CE)) {
1831c79c73f6SBlue Swirl         /* External critical interrupt */
1832f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CEXT) {
1833de76b85cSMatheus Ferst             return PPC_INTERRUPT_CEXT;
1834c79c73f6SBlue Swirl         }
1835c79c73f6SBlue Swirl     }
18363621e2c9SBenjamin Herrenschmidt     if (async_deliver != 0) {
1837c79c73f6SBlue Swirl         /* Watchdog timer on embedded PowerPC */
1838f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_WDT) {
1839de76b85cSMatheus Ferst             return PPC_INTERRUPT_WDT;
1840c79c73f6SBlue Swirl         }
1841f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) {
1842de76b85cSMatheus Ferst             return PPC_INTERRUPT_CDOORBELL;
1843c79c73f6SBlue Swirl         }
1844c79c73f6SBlue Swirl         /* Fixed interval timer on embedded PowerPC */
1845f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_FIT) {
1846de76b85cSMatheus Ferst             return PPC_INTERRUPT_FIT;
1847c79c73f6SBlue Swirl         }
1848c79c73f6SBlue Swirl         /* Programmable interval timer on embedded PowerPC */
1849f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PIT) {
1850de76b85cSMatheus Ferst             return PPC_INTERRUPT_PIT;
1851c79c73f6SBlue Swirl         }
1852c79c73f6SBlue Swirl         /* Decrementer exception */
1853f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1854de76b85cSMatheus Ferst             return PPC_INTERRUPT_DECR;
1855c79c73f6SBlue Swirl         }
1856f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1857de76b85cSMatheus Ferst             return PPC_INTERRUPT_DOORBELL;
1858c79c73f6SBlue Swirl         }
1859f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
1860de76b85cSMatheus Ferst             return PPC_INTERRUPT_HDOORBELL;
18617af1e7b0SCédric Le Goater         }
1862f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1863de76b85cSMatheus Ferst             return PPC_INTERRUPT_PERFM;
1864c79c73f6SBlue Swirl         }
1865c79c73f6SBlue Swirl         /* Thermal interrupt */
1866f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_THERM) {
1867de76b85cSMatheus Ferst             return PPC_INTERRUPT_THERM;
1868c79c73f6SBlue Swirl         }
1869cb76bbc4SDaniel Henrique Barboza         /* EBB exception */
1870f003109fSMatheus Ferst         if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
1871cb76bbc4SDaniel Henrique Barboza             /*
1872cb76bbc4SDaniel Henrique Barboza              * EBB exception must be taken in problem state and
1873cb76bbc4SDaniel Henrique Barboza              * with BESCR_GE set.
1874cb76bbc4SDaniel Henrique Barboza              */
1875d41ccf6eSVíctor Colombo             if (FIELD_EX64(env->msr, MSR, PR) &&
1876d41ccf6eSVíctor Colombo                 (env->spr[SPR_BESCR] & BESCR_GE)) {
1877de76b85cSMatheus Ferst                 return PPC_INTERRUPT_EBB;
1878de76b85cSMatheus Ferst             }
1879de76b85cSMatheus Ferst         }
1880de76b85cSMatheus Ferst     }
1881cb76bbc4SDaniel Henrique Barboza 
1882de76b85cSMatheus Ferst     return 0;
1883de76b85cSMatheus Ferst }
1884de76b85cSMatheus Ferst 
1885ba2898f7SMatheus Ferst static int ppc_next_unmasked_interrupt(CPUPPCState *env)
1886ba2898f7SMatheus Ferst {
1887ba2898f7SMatheus Ferst     switch (env->excp_model) {
18882dfecf01SMatheus Ferst #if defined(TARGET_PPC64)
18892dfecf01SMatheus Ferst     case POWERPC_EXCP_POWER9:
18902dfecf01SMatheus Ferst     case POWERPC_EXCP_POWER10:
18912dfecf01SMatheus Ferst         return p9_next_unmasked_interrupt(env);
18922dfecf01SMatheus Ferst #endif
1893ba2898f7SMatheus Ferst     default:
1894ba2898f7SMatheus Ferst         return ppc_next_unmasked_interrupt_generic(env);
1895ba2898f7SMatheus Ferst     }
1896ba2898f7SMatheus Ferst }
1897ba2898f7SMatheus Ferst 
1898*3654e238SMatheus Ferst #if defined(TARGET_PPC64)
1899*3654e238SMatheus Ferst static void p9_deliver_interrupt(CPUPPCState *env, int interrupt)
1900*3654e238SMatheus Ferst {
1901*3654e238SMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
1902*3654e238SMatheus Ferst     CPUState *cs = env_cpu(env);
1903*3654e238SMatheus Ferst 
1904*3654e238SMatheus Ferst     switch (interrupt) {
1905*3654e238SMatheus Ferst     case PPC_INTERRUPT_RESET: /* External reset */
1906*3654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_RESET;
1907*3654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_RESET);
1908*3654e238SMatheus Ferst         break;
1909*3654e238SMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
1910*3654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
1911*3654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
1912*3654e238SMatheus Ferst         break;
1913*3654e238SMatheus Ferst 
1914*3654e238SMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
1915*3654e238SMatheus Ferst         /* HDEC clears on delivery */
1916*3654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
1917*3654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
1918*3654e238SMatheus Ferst         break;
1919*3654e238SMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
1920*3654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
1921*3654e238SMatheus Ferst         break;
1922*3654e238SMatheus Ferst 
1923*3654e238SMatheus Ferst     case PPC_INTERRUPT_EXT:
1924*3654e238SMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
1925*3654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
1926*3654e238SMatheus Ferst         } else {
1927*3654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
1928*3654e238SMatheus Ferst         }
1929*3654e238SMatheus Ferst         break;
1930*3654e238SMatheus Ferst     case PPC_INTERRUPT_CEXT: /* External critical interrupt */
1931*3654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_CRITICAL);
1932*3654e238SMatheus Ferst         break;
1933*3654e238SMatheus Ferst 
1934*3654e238SMatheus Ferst     case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */
1935*3654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_WDT;
1936*3654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_WDT);
1937*3654e238SMatheus Ferst         break;
1938*3654e238SMatheus Ferst     case PPC_INTERRUPT_CDOORBELL:
1939*3654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL;
1940*3654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DOORCI);
1941*3654e238SMatheus Ferst         break;
1942*3654e238SMatheus Ferst     case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */
1943*3654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_FIT;
1944*3654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_FIT);
1945*3654e238SMatheus Ferst         break;
1946*3654e238SMatheus Ferst     case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */
1947*3654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PIT;
1948*3654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PIT);
1949*3654e238SMatheus Ferst         break;
1950*3654e238SMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
1951*3654e238SMatheus Ferst         if (ppc_decr_clear_on_delivery(env)) {
1952*3654e238SMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DECR;
1953*3654e238SMatheus Ferst         }
1954*3654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
1955*3654e238SMatheus Ferst         break;
1956*3654e238SMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
1957*3654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
1958*3654e238SMatheus Ferst         if (is_book3s_arch2x(env)) {
1959*3654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
1960*3654e238SMatheus Ferst         } else {
1961*3654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
1962*3654e238SMatheus Ferst         }
1963*3654e238SMatheus Ferst         break;
1964*3654e238SMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
1965*3654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
1966*3654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
1967*3654e238SMatheus Ferst         break;
1968*3654e238SMatheus Ferst     case PPC_INTERRUPT_PERFM:
1969*3654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
1970*3654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
1971*3654e238SMatheus Ferst         break;
1972*3654e238SMatheus Ferst     case PPC_INTERRUPT_THERM:  /* Thermal interrupt */
1973*3654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_THERM;
1974*3654e238SMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_THERM);
1975*3654e238SMatheus Ferst         break;
1976*3654e238SMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
1977*3654e238SMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
1978*3654e238SMatheus Ferst         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
1979*3654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
1980*3654e238SMatheus Ferst         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
1981*3654e238SMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
1982*3654e238SMatheus Ferst         }
1983*3654e238SMatheus Ferst         break;
1984*3654e238SMatheus Ferst     case 0:
1985*3654e238SMatheus Ferst         /*
1986*3654e238SMatheus Ferst          * This is a bug ! It means that has_work took us out of halt without
1987*3654e238SMatheus Ferst          * anything to deliver while in a PM state that requires getting
1988*3654e238SMatheus Ferst          * out via a 0x100
1989*3654e238SMatheus Ferst          *
1990*3654e238SMatheus Ferst          * This means we will incorrectly execute past the power management
1991*3654e238SMatheus Ferst          * instruction instead of triggering a reset.
1992*3654e238SMatheus Ferst          *
1993*3654e238SMatheus Ferst          * It generally means a discrepancy between the wakeup conditions in the
1994*3654e238SMatheus Ferst          * processor has_work implementation and the logic in this function.
1995*3654e238SMatheus Ferst          */
1996*3654e238SMatheus Ferst         assert(!env->resume_as_sreset);
1997*3654e238SMatheus Ferst         break;
1998*3654e238SMatheus Ferst     default:
1999*3654e238SMatheus Ferst         cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
2000*3654e238SMatheus Ferst     }
2001*3654e238SMatheus Ferst }
2002*3654e238SMatheus Ferst #endif
2003*3654e238SMatheus Ferst 
2004ba2898f7SMatheus Ferst static void ppc_deliver_interrupt_generic(CPUPPCState *env, int interrupt)
2005de76b85cSMatheus Ferst {
2006de76b85cSMatheus Ferst     PowerPCCPU *cpu = env_archcpu(env);
2007de76b85cSMatheus Ferst     CPUState *cs = env_cpu(env);
2008de76b85cSMatheus Ferst 
2009de76b85cSMatheus Ferst     switch (interrupt) {
2010de76b85cSMatheus Ferst     case PPC_INTERRUPT_RESET: /* External reset */
2011de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_RESET;
2012de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_RESET);
2013de76b85cSMatheus Ferst         break;
2014de76b85cSMatheus Ferst     case PPC_INTERRUPT_MCK: /* Machine check exception */
2015de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2016de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2017de76b85cSMatheus Ferst         break;
2018de76b85cSMatheus Ferst 
2019de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2020de76b85cSMatheus Ferst         /* HDEC clears on delivery */
2021de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2022de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2023de76b85cSMatheus Ferst         break;
2024de76b85cSMatheus Ferst     case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
2025de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2026de76b85cSMatheus Ferst         break;
2027de76b85cSMatheus Ferst 
2028de76b85cSMatheus Ferst     case PPC_INTERRUPT_EXT:
2029de76b85cSMatheus Ferst         if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2030de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2031de76b85cSMatheus Ferst         } else {
2032de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2033de76b85cSMatheus Ferst         }
2034de76b85cSMatheus Ferst         break;
2035de76b85cSMatheus Ferst     case PPC_INTERRUPT_CEXT: /* External critical interrupt */
2036de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_CRITICAL);
2037de76b85cSMatheus Ferst         break;
2038de76b85cSMatheus Ferst 
2039de76b85cSMatheus Ferst     case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */
2040de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_WDT;
2041de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_WDT);
2042de76b85cSMatheus Ferst         break;
2043de76b85cSMatheus Ferst     case PPC_INTERRUPT_CDOORBELL:
2044de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL;
2045de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DOORCI);
2046de76b85cSMatheus Ferst         break;
2047de76b85cSMatheus Ferst     case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */
2048de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_FIT;
2049de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_FIT);
2050de76b85cSMatheus Ferst         break;
2051de76b85cSMatheus Ferst     case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */
2052de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PIT;
2053de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PIT);
2054de76b85cSMatheus Ferst         break;
2055de76b85cSMatheus Ferst     case PPC_INTERRUPT_DECR: /* Decrementer exception */
2056de76b85cSMatheus Ferst         if (ppc_decr_clear_on_delivery(env)) {
2057de76b85cSMatheus Ferst             env->pending_interrupts &= ~PPC_INTERRUPT_DECR;
2058de76b85cSMatheus Ferst         }
2059de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_DECR);
2060de76b85cSMatheus Ferst         break;
2061de76b85cSMatheus Ferst     case PPC_INTERRUPT_DOORBELL:
2062de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
2063de76b85cSMatheus Ferst         if (is_book3s_arch2x(env)) {
2064de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
2065de76b85cSMatheus Ferst         } else {
2066de76b85cSMatheus Ferst             powerpc_excp(cpu, POWERPC_EXCP_DOORI);
2067de76b85cSMatheus Ferst         }
2068de76b85cSMatheus Ferst         break;
2069de76b85cSMatheus Ferst     case PPC_INTERRUPT_HDOORBELL:
2070de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
2071de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
2072de76b85cSMatheus Ferst         break;
2073de76b85cSMatheus Ferst     case PPC_INTERRUPT_PERFM:
2074de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2075de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2076de76b85cSMatheus Ferst         break;
2077de76b85cSMatheus Ferst     case PPC_INTERRUPT_THERM:  /* Thermal interrupt */
2078de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_THERM;
2079de76b85cSMatheus Ferst         powerpc_excp(cpu, POWERPC_EXCP_THERM);
2080de76b85cSMatheus Ferst         break;
2081de76b85cSMatheus Ferst     case PPC_INTERRUPT_EBB: /* EBB exception */
2082de76b85cSMatheus Ferst         env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
2083cb76bbc4SDaniel Henrique Barboza         if (env->spr[SPR_BESCR] & BESCR_PMEO) {
2084cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
2085cb76bbc4SDaniel Henrique Barboza         } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
2086cb76bbc4SDaniel Henrique Barboza             powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
2087cb76bbc4SDaniel Henrique Barboza         }
2088de76b85cSMatheus Ferst         break;
2089de76b85cSMatheus Ferst     case 0:
2090f8154fd2SBenjamin Herrenschmidt         /*
2091f8154fd2SBenjamin Herrenschmidt          * This is a bug ! It means that has_work took us out of halt without
2092f8154fd2SBenjamin Herrenschmidt          * anything to deliver while in a PM state that requires getting
2093f8154fd2SBenjamin Herrenschmidt          * out via a 0x100
2094f8154fd2SBenjamin Herrenschmidt          *
2095f8154fd2SBenjamin Herrenschmidt          * This means we will incorrectly execute past the power management
2096f8154fd2SBenjamin Herrenschmidt          * instruction instead of triggering a reset.
2097f8154fd2SBenjamin Herrenschmidt          *
2098136fbf65Szhaolichang          * It generally means a discrepancy between the wakeup conditions in the
2099f8154fd2SBenjamin Herrenschmidt          * processor has_work implementation and the logic in this function.
2100f8154fd2SBenjamin Herrenschmidt          */
2101de76b85cSMatheus Ferst         assert(!env->resume_as_sreset);
2102de76b85cSMatheus Ferst         break;
2103de76b85cSMatheus Ferst     default:
2104de76b85cSMatheus Ferst         cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
2105f8154fd2SBenjamin Herrenschmidt     }
2106c79c73f6SBlue Swirl }
210734316482SAlexey Kardashevskiy 
2108ba2898f7SMatheus Ferst static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt)
2109ba2898f7SMatheus Ferst {
2110ba2898f7SMatheus Ferst     switch (env->excp_model) {
2111*3654e238SMatheus Ferst #if defined(TARGET_PPC64)
2112*3654e238SMatheus Ferst     case POWERPC_EXCP_POWER9:
2113*3654e238SMatheus Ferst     case POWERPC_EXCP_POWER10:
2114*3654e238SMatheus Ferst         p9_deliver_interrupt(env, interrupt);
2115*3654e238SMatheus Ferst         break;
2116*3654e238SMatheus Ferst #endif
2117ba2898f7SMatheus Ferst     default:
2118ba2898f7SMatheus Ferst         ppc_deliver_interrupt_generic(env, interrupt);
2119ba2898f7SMatheus Ferst     }
2120ba2898f7SMatheus Ferst }
2121ba2898f7SMatheus Ferst 
2122b5b7f391SNicholas Piggin void ppc_cpu_do_system_reset(CPUState *cs)
212334316482SAlexey Kardashevskiy {
212434316482SAlexey Kardashevskiy     PowerPCCPU *cpu = POWERPC_CPU(cs);
212534316482SAlexey Kardashevskiy 
212693130c84SFabiano Rosas     powerpc_excp(cpu, POWERPC_EXCP_RESET);
212734316482SAlexey Kardashevskiy }
2128ad77c6caSNicholas Piggin 
2129ad77c6caSNicholas Piggin void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector)
2130ad77c6caSNicholas Piggin {
2131ad77c6caSNicholas Piggin     PowerPCCPU *cpu = POWERPC_CPU(cs);
2132ad77c6caSNicholas Piggin     CPUPPCState *env = &cpu->env;
2133ad77c6caSNicholas Piggin     target_ulong msr = 0;
2134ad77c6caSNicholas Piggin 
2135ad77c6caSNicholas Piggin     /*
2136ad77c6caSNicholas Piggin      * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already
2137ad77c6caSNicholas Piggin      * been set by KVM.
2138ad77c6caSNicholas Piggin      */
2139ad77c6caSNicholas Piggin     msr = (1ULL << MSR_ME);
2140ad77c6caSNicholas Piggin     msr |= env->msr & (1ULL << MSR_SF);
2141516fc103SFabiano Rosas     if (ppc_interrupts_little_endian(cpu, false)) {
2142ad77c6caSNicholas Piggin         msr |= (1ULL << MSR_LE);
2143ad77c6caSNicholas Piggin     }
2144ad77c6caSNicholas Piggin 
21457cebc5dbSNicholas Piggin     /* Anything for nested required here? MSR[HV] bit? */
21467cebc5dbSNicholas Piggin 
2147ad77c6caSNicholas Piggin     powerpc_set_excp_state(cpu, vector, msr);
2148ad77c6caSNicholas Piggin }
2149c79c73f6SBlue Swirl 
2150458dd766SRichard Henderson bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
2151458dd766SRichard Henderson {
2152458dd766SRichard Henderson     PowerPCCPU *cpu = POWERPC_CPU(cs);
2153458dd766SRichard Henderson     CPUPPCState *env = &cpu->env;
2154de76b85cSMatheus Ferst     int interrupt;
2155458dd766SRichard Henderson 
2156de76b85cSMatheus Ferst     if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) {
2157de76b85cSMatheus Ferst         return false;
2158de76b85cSMatheus Ferst     }
2159de76b85cSMatheus Ferst 
2160de76b85cSMatheus Ferst     interrupt = ppc_next_unmasked_interrupt(env);
2161de76b85cSMatheus Ferst     if (interrupt == 0) {
2162de76b85cSMatheus Ferst         return false;
2163de76b85cSMatheus Ferst     }
2164de76b85cSMatheus Ferst 
2165de76b85cSMatheus Ferst     ppc_deliver_interrupt(env, interrupt);
2166458dd766SRichard Henderson     if (env->pending_interrupts == 0) {
2167de76b85cSMatheus Ferst         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
2168458dd766SRichard Henderson     }
2169458dd766SRichard Henderson     return true;
2170458dd766SRichard Henderson }
2171458dd766SRichard Henderson 
2172f725245cSPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
2173f725245cSPhilippe Mathieu-Daudé 
2174ad71ed68SBlue Swirl /*****************************************************************************/
2175ad71ed68SBlue Swirl /* Exceptions processing helpers */
2176ad71ed68SBlue Swirl 
2177db789c6cSBenjamin Herrenschmidt void raise_exception_err_ra(CPUPPCState *env, uint32_t exception,
2178db789c6cSBenjamin Herrenschmidt                             uint32_t error_code, uintptr_t raddr)
2179ad71ed68SBlue Swirl {
2180db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
218127103424SAndreas Färber 
218227103424SAndreas Färber     cs->exception_index = exception;
2183ad71ed68SBlue Swirl     env->error_code = error_code;
2184db789c6cSBenjamin Herrenschmidt     cpu_loop_exit_restore(cs, raddr);
2185db789c6cSBenjamin Herrenschmidt }
2186db789c6cSBenjamin Herrenschmidt 
2187db789c6cSBenjamin Herrenschmidt void raise_exception_err(CPUPPCState *env, uint32_t exception,
2188db789c6cSBenjamin Herrenschmidt                          uint32_t error_code)
2189db789c6cSBenjamin Herrenschmidt {
2190db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2191db789c6cSBenjamin Herrenschmidt }
2192db789c6cSBenjamin Herrenschmidt 
2193db789c6cSBenjamin Herrenschmidt void raise_exception(CPUPPCState *env, uint32_t exception)
2194db789c6cSBenjamin Herrenschmidt {
2195db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2196db789c6cSBenjamin Herrenschmidt }
2197db789c6cSBenjamin Herrenschmidt 
2198db789c6cSBenjamin Herrenschmidt void raise_exception_ra(CPUPPCState *env, uint32_t exception,
2199db789c6cSBenjamin Herrenschmidt                         uintptr_t raddr)
2200db789c6cSBenjamin Herrenschmidt {
2201db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, raddr);
2202db789c6cSBenjamin Herrenschmidt }
2203db789c6cSBenjamin Herrenschmidt 
22042b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2205db789c6cSBenjamin Herrenschmidt void helper_raise_exception_err(CPUPPCState *env, uint32_t exception,
2206db789c6cSBenjamin Herrenschmidt                                 uint32_t error_code)
2207db789c6cSBenjamin Herrenschmidt {
2208db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, error_code, 0);
2209ad71ed68SBlue Swirl }
2210ad71ed68SBlue Swirl 
2211e5f17ac6SBlue Swirl void helper_raise_exception(CPUPPCState *env, uint32_t exception)
2212ad71ed68SBlue Swirl {
2213db789c6cSBenjamin Herrenschmidt     raise_exception_err_ra(env, exception, 0, 0);
2214ad71ed68SBlue Swirl }
22152b44e219SBruno Larsen (billionai) #endif
2216ad71ed68SBlue Swirl 
2217ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY)
22182b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2219e5f17ac6SBlue Swirl void helper_store_msr(CPUPPCState *env, target_ulong val)
2220ad71ed68SBlue Swirl {
2221db789c6cSBenjamin Herrenschmidt     uint32_t excp = hreg_store_msr(env, val, 0);
2222259186a7SAndreas Färber 
2223db789c6cSBenjamin Herrenschmidt     if (excp != 0) {
2224db70b311SRichard Henderson         CPUState *cs = env_cpu(env);
2225044897efSRichard Purdie         cpu_interrupt_exittb(cs);
2226db789c6cSBenjamin Herrenschmidt         raise_exception(env, excp);
2227ad71ed68SBlue Swirl     }
2228ad71ed68SBlue Swirl }
2229ad71ed68SBlue Swirl 
22307778a575SBenjamin Herrenschmidt #if defined(TARGET_PPC64)
2231f43520e5SRichard Henderson void helper_scv(CPUPPCState *env, uint32_t lev)
2232f43520e5SRichard Henderson {
2233f43520e5SRichard Henderson     if (env->spr[SPR_FSCR] & (1ull << FSCR_SCV)) {
2234f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_SYSCALL_VECTORED, lev);
2235f43520e5SRichard Henderson     } else {
2236f43520e5SRichard Henderson         raise_exception_err(env, POWERPC_EXCP_FU, FSCR_IC_SCV);
2237f43520e5SRichard Henderson     }
2238f43520e5SRichard Henderson }
2239f43520e5SRichard Henderson 
22407778a575SBenjamin Herrenschmidt void helper_pminsn(CPUPPCState *env, powerpc_pm_insn_t insn)
22417778a575SBenjamin Herrenschmidt {
22427778a575SBenjamin Herrenschmidt     CPUState *cs;
22437778a575SBenjamin Herrenschmidt 
2244db70b311SRichard Henderson     cs = env_cpu(env);
22457778a575SBenjamin Herrenschmidt     cs->halted = 1;
22467778a575SBenjamin Herrenschmidt 
22473621e2c9SBenjamin Herrenschmidt     /* Condition for waking up at 0x100 */
22481e7fd61dSBenjamin Herrenschmidt     env->resume_as_sreset = (insn != PPC_PM_STOP) ||
224921c0d66aSBenjamin Herrenschmidt         (env->spr[SPR_PSSCR] & PSSCR_EC);
22507778a575SBenjamin Herrenschmidt }
22517778a575SBenjamin Herrenschmidt #endif /* defined(TARGET_PPC64) */
22527778a575SBenjamin Herrenschmidt 
225362e79ef9SCédric Le Goater static void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr)
2254ad71ed68SBlue Swirl {
2255db70b311SRichard Henderson     CPUState *cs = env_cpu(env);
2256259186a7SAndreas Färber 
2257a2e71b28SBenjamin Herrenschmidt     /* MSR:POW cannot be set by any form of rfi */
2258a2e71b28SBenjamin Herrenschmidt     msr &= ~(1ULL << MSR_POW);
2259a2e71b28SBenjamin Herrenschmidt 
22605aad0457SChristophe Leroy     /* MSR:TGPR cannot be set by any form of rfi */
22615aad0457SChristophe Leroy     if (env->flags & POWERPC_FLAG_TGPR)
22625aad0457SChristophe Leroy         msr &= ~(1ULL << MSR_TGPR);
22635aad0457SChristophe Leroy 
2264ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2265a2e71b28SBenjamin Herrenschmidt     /* Switching to 32-bit ? Crop the nip */
2266a2e71b28SBenjamin Herrenschmidt     if (!msr_is_64bit(env, msr)) {
2267ad71ed68SBlue Swirl         nip = (uint32_t)nip;
2268ad71ed68SBlue Swirl     }
2269ad71ed68SBlue Swirl #else
2270ad71ed68SBlue Swirl     nip = (uint32_t)nip;
2271ad71ed68SBlue Swirl #endif
2272ad71ed68SBlue Swirl     /* XXX: beware: this is false if VLE is supported */
2273ad71ed68SBlue Swirl     env->nip = nip & ~((target_ulong)0x00000003);
2274ad71ed68SBlue Swirl     hreg_store_msr(env, msr, 1);
22752eb1ef73SCédric Le Goater     trace_ppc_excp_rfi(env->nip, env->msr);
227647733729SDavid Gibson     /*
227747733729SDavid Gibson      * No need to raise an exception here, as rfi is always the last
227847733729SDavid Gibson      * insn of a TB
2279ad71ed68SBlue Swirl      */
2280044897efSRichard Purdie     cpu_interrupt_exittb(cs);
2281a8b73734SNikunj A Dadhania     /* Reset the reservation */
2282a8b73734SNikunj A Dadhania     env->reserve_addr = -1;
2283a8b73734SNikunj A Dadhania 
2284cd0c6f47SBenjamin Herrenschmidt     /* Context synchronizing: check if TCG TLB needs flush */
2285e3cffe6fSNikunj A Dadhania     check_tlb_flush(env, false);
2286ad71ed68SBlue Swirl }
2287ad71ed68SBlue Swirl 
2288e5f17ac6SBlue Swirl void helper_rfi(CPUPPCState *env)
2289ad71ed68SBlue Swirl {
2290a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful);
2291a1bb7384SScott Wood }
2292ad71ed68SBlue Swirl 
2293ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2294e5f17ac6SBlue Swirl void helper_rfid(CPUPPCState *env)
2295ad71ed68SBlue Swirl {
229647733729SDavid Gibson     /*
2297136fbf65Szhaolichang      * The architecture defines a number of rules for which bits can
229847733729SDavid Gibson      * change but in practice, we handle this in hreg_store_msr()
2299a2e71b28SBenjamin Herrenschmidt      * which will be called by do_rfi(), so there is no need to filter
2300a2e71b28SBenjamin Herrenschmidt      * here
2301a2e71b28SBenjamin Herrenschmidt      */
2302a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]);
2303ad71ed68SBlue Swirl }
2304ad71ed68SBlue Swirl 
23053c89b8d6SNicholas Piggin void helper_rfscv(CPUPPCState *env)
23063c89b8d6SNicholas Piggin {
23073c89b8d6SNicholas Piggin     do_rfi(env, env->lr, env->ctr);
23083c89b8d6SNicholas Piggin }
23093c89b8d6SNicholas Piggin 
2310e5f17ac6SBlue Swirl void helper_hrfid(CPUPPCState *env)
2311ad71ed68SBlue Swirl {
2312a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
2313ad71ed68SBlue Swirl }
2314ad71ed68SBlue Swirl #endif
2315ad71ed68SBlue Swirl 
23161f26c751SDaniel Henrique Barboza #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
23171f26c751SDaniel Henrique Barboza void helper_rfebb(CPUPPCState *env, target_ulong s)
23181f26c751SDaniel Henrique Barboza {
23191f26c751SDaniel Henrique Barboza     target_ulong msr = env->msr;
23201f26c751SDaniel Henrique Barboza 
23211f26c751SDaniel Henrique Barboza     /*
23221f26c751SDaniel Henrique Barboza      * Handling of BESCR bits 32:33 according to PowerISA v3.1:
23231f26c751SDaniel Henrique Barboza      *
23241f26c751SDaniel Henrique Barboza      * "If BESCR 32:33 != 0b00 the instruction is treated as if
23251f26c751SDaniel Henrique Barboza      *  the instruction form were invalid."
23261f26c751SDaniel Henrique Barboza      */
23271f26c751SDaniel Henrique Barboza     if (env->spr[SPR_BESCR] & BESCR_INVALID) {
23281f26c751SDaniel Henrique Barboza         raise_exception_err(env, POWERPC_EXCP_PROGRAM,
23291f26c751SDaniel Henrique Barboza                             POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
23301f26c751SDaniel Henrique Barboza     }
23311f26c751SDaniel Henrique Barboza 
23321f26c751SDaniel Henrique Barboza     env->nip = env->spr[SPR_EBBRR];
23331f26c751SDaniel Henrique Barboza 
23341f26c751SDaniel Henrique Barboza     /* Switching to 32-bit ? Crop the nip */
23351f26c751SDaniel Henrique Barboza     if (!msr_is_64bit(env, msr)) {
23361f26c751SDaniel Henrique Barboza         env->nip = (uint32_t)env->spr[SPR_EBBRR];
23371f26c751SDaniel Henrique Barboza     }
23381f26c751SDaniel Henrique Barboza 
23391f26c751SDaniel Henrique Barboza     if (s) {
23401f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_GE;
23411f26c751SDaniel Henrique Barboza     } else {
23421f26c751SDaniel Henrique Barboza         env->spr[SPR_BESCR] &= ~BESCR_GE;
23431f26c751SDaniel Henrique Barboza     }
23441f26c751SDaniel Henrique Barboza }
2345d3412df2SDaniel Henrique Barboza 
2346d3412df2SDaniel Henrique Barboza /*
2347d3412df2SDaniel Henrique Barboza  * Triggers or queues an 'ebb_excp' EBB exception. All checks
2348d3412df2SDaniel Henrique Barboza  * but FSCR, HFSCR and msr_pr must be done beforehand.
2349d3412df2SDaniel Henrique Barboza  *
2350d3412df2SDaniel Henrique Barboza  * PowerISA v3.1 isn't clear about whether an EBB should be
2351d3412df2SDaniel Henrique Barboza  * postponed or cancelled if the EBB facility is unavailable.
2352d3412df2SDaniel Henrique Barboza  * Our assumption here is that the EBB is cancelled if both
2353d3412df2SDaniel Henrique Barboza  * FSCR and HFSCR EBB facilities aren't available.
2354d3412df2SDaniel Henrique Barboza  */
2355d3412df2SDaniel Henrique Barboza static void do_ebb(CPUPPCState *env, int ebb_excp)
2356d3412df2SDaniel Henrique Barboza {
2357d3412df2SDaniel Henrique Barboza     PowerPCCPU *cpu = env_archcpu(env);
2358d3412df2SDaniel Henrique Barboza 
2359d3412df2SDaniel Henrique Barboza     /*
2360d3412df2SDaniel Henrique Barboza      * FSCR_EBB and FSCR_IC_EBB are the same bits used with
2361d3412df2SDaniel Henrique Barboza      * HFSCR.
2362d3412df2SDaniel Henrique Barboza      */
2363d3412df2SDaniel Henrique Barboza     helper_fscr_facility_check(env, FSCR_EBB, 0, FSCR_IC_EBB);
2364d3412df2SDaniel Henrique Barboza     helper_hfscr_facility_check(env, FSCR_EBB, "EBB", FSCR_IC_EBB);
2365d3412df2SDaniel Henrique Barboza 
2366d3412df2SDaniel Henrique Barboza     if (ebb_excp == POWERPC_EXCP_PERFM_EBB) {
2367d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_PMEO;
2368d3412df2SDaniel Henrique Barboza     } else if (ebb_excp == POWERPC_EXCP_EXTERNAL_EBB) {
2369d3412df2SDaniel Henrique Barboza         env->spr[SPR_BESCR] |= BESCR_EEO;
2370d3412df2SDaniel Henrique Barboza     }
2371d3412df2SDaniel Henrique Barboza 
2372d41ccf6eSVíctor Colombo     if (FIELD_EX64(env->msr, MSR, PR)) {
2373d3412df2SDaniel Henrique Barboza         powerpc_excp(cpu, ebb_excp);
2374d3412df2SDaniel Henrique Barboza     } else {
23757b694df6SMatheus Ferst         ppc_set_irq(cpu, PPC_INTERRUPT_EBB, 1);
2376d3412df2SDaniel Henrique Barboza     }
2377d3412df2SDaniel Henrique Barboza }
2378d3412df2SDaniel Henrique Barboza 
2379d3412df2SDaniel Henrique Barboza void raise_ebb_perfm_exception(CPUPPCState *env)
2380d3412df2SDaniel Henrique Barboza {
2381d3412df2SDaniel Henrique Barboza     bool perfm_ebb_enabled = env->spr[SPR_POWER_MMCR0] & MMCR0_EBE &&
2382d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_PME &&
2383d3412df2SDaniel Henrique Barboza                              env->spr[SPR_BESCR] & BESCR_GE;
2384d3412df2SDaniel Henrique Barboza 
2385d3412df2SDaniel Henrique Barboza     if (!perfm_ebb_enabled) {
2386d3412df2SDaniel Henrique Barboza         return;
2387d3412df2SDaniel Henrique Barboza     }
2388d3412df2SDaniel Henrique Barboza 
2389d3412df2SDaniel Henrique Barboza     do_ebb(env, POWERPC_EXCP_PERFM_EBB);
2390d3412df2SDaniel Henrique Barboza }
23911f26c751SDaniel Henrique Barboza #endif
23921f26c751SDaniel Henrique Barboza 
2393ad71ed68SBlue Swirl /*****************************************************************************/
2394ad71ed68SBlue Swirl /* Embedded PowerPC specific helpers */
2395e5f17ac6SBlue Swirl void helper_40x_rfci(CPUPPCState *env)
2396ad71ed68SBlue Swirl {
2397a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]);
2398ad71ed68SBlue Swirl }
2399ad71ed68SBlue Swirl 
2400e5f17ac6SBlue Swirl void helper_rfci(CPUPPCState *env)
2401ad71ed68SBlue Swirl {
2402a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]);
2403ad71ed68SBlue Swirl }
2404ad71ed68SBlue Swirl 
2405e5f17ac6SBlue Swirl void helper_rfdi(CPUPPCState *env)
2406ad71ed68SBlue Swirl {
2407a1bb7384SScott Wood     /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
2408a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]);
2409ad71ed68SBlue Swirl }
2410ad71ed68SBlue Swirl 
2411e5f17ac6SBlue Swirl void helper_rfmci(CPUPPCState *env)
2412ad71ed68SBlue Swirl {
2413a1bb7384SScott Wood     /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
2414a2e71b28SBenjamin Herrenschmidt     do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
2415ad71ed68SBlue Swirl }
24162b44e219SBruno Larsen (billionai) #endif /* CONFIG_TCG */
24172b44e219SBruno Larsen (billionai) #endif /* !defined(CONFIG_USER_ONLY) */
2418ad71ed68SBlue Swirl 
24192b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2420e5f17ac6SBlue Swirl void helper_tw(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2421e5f17ac6SBlue Swirl                uint32_t flags)
2422ad71ed68SBlue Swirl {
2423ad71ed68SBlue Swirl     if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
2424ad71ed68SBlue Swirl                   ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
2425ad71ed68SBlue Swirl                   ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
2426ad71ed68SBlue Swirl                   ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
2427ad71ed68SBlue Swirl                   ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
242872073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
242972073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2430ad71ed68SBlue Swirl     }
2431ad71ed68SBlue Swirl }
2432ad71ed68SBlue Swirl 
2433ad71ed68SBlue Swirl #if defined(TARGET_PPC64)
2434e5f17ac6SBlue Swirl void helper_td(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2435e5f17ac6SBlue Swirl                uint32_t flags)
2436ad71ed68SBlue Swirl {
2437ad71ed68SBlue Swirl     if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
2438ad71ed68SBlue Swirl                   ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
2439ad71ed68SBlue Swirl                   ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
2440ad71ed68SBlue Swirl                   ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
2441ad71ed68SBlue Swirl                   ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) {
244272073dccSBenjamin Herrenschmidt         raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
244372073dccSBenjamin Herrenschmidt                                POWERPC_EXCP_TRAP, GETPC());
2444ad71ed68SBlue Swirl     }
2445ad71ed68SBlue Swirl }
2446ad71ed68SBlue Swirl #endif
24472b44e219SBruno Larsen (billionai) #endif
2448ad71ed68SBlue Swirl 
2449670f1da3SVíctor Colombo static uint32_t helper_SIMON_LIKE_32_64(uint32_t x, uint64_t key, uint32_t lane)
2450670f1da3SVíctor Colombo {
2451670f1da3SVíctor Colombo     const uint16_t c = 0xfffc;
2452670f1da3SVíctor Colombo     const uint64_t z0 = 0xfa2561cdf44ac398ULL;
2453670f1da3SVíctor Colombo     uint16_t z = 0, temp;
2454670f1da3SVíctor Colombo     uint16_t k[32], eff_k[32], xleft[33], xright[33], fxleft[32];
2455670f1da3SVíctor Colombo 
2456670f1da3SVíctor Colombo     for (int i = 3; i >= 0; i--) {
2457670f1da3SVíctor Colombo         k[i] = key & 0xffff;
2458670f1da3SVíctor Colombo         key >>= 16;
2459670f1da3SVíctor Colombo     }
2460670f1da3SVíctor Colombo     xleft[0] = x & 0xffff;
2461670f1da3SVíctor Colombo     xright[0] = (x >> 16) & 0xffff;
2462670f1da3SVíctor Colombo 
2463670f1da3SVíctor Colombo     for (int i = 0; i < 28; i++) {
2464670f1da3SVíctor Colombo         z = (z0 >> (63 - i)) & 1;
2465670f1da3SVíctor Colombo         temp = ror16(k[i + 3], 3) ^ k[i + 1];
2466670f1da3SVíctor Colombo         k[i + 4] = c ^ z ^ k[i] ^ temp ^ ror16(temp, 1);
2467670f1da3SVíctor Colombo     }
2468670f1da3SVíctor Colombo 
2469670f1da3SVíctor Colombo     for (int i = 0; i < 8; i++) {
2470670f1da3SVíctor Colombo         eff_k[4 * i + 0] = k[4 * i + ((0 + lane) % 4)];
2471670f1da3SVíctor Colombo         eff_k[4 * i + 1] = k[4 * i + ((1 + lane) % 4)];
2472670f1da3SVíctor Colombo         eff_k[4 * i + 2] = k[4 * i + ((2 + lane) % 4)];
2473670f1da3SVíctor Colombo         eff_k[4 * i + 3] = k[4 * i + ((3 + lane) % 4)];
2474670f1da3SVíctor Colombo     }
2475670f1da3SVíctor Colombo 
2476670f1da3SVíctor Colombo     for (int i = 0; i < 32; i++) {
2477670f1da3SVíctor Colombo         fxleft[i] = (rol16(xleft[i], 1) &
2478670f1da3SVíctor Colombo             rol16(xleft[i], 8)) ^ rol16(xleft[i], 2);
2479670f1da3SVíctor Colombo         xleft[i + 1] = xright[i] ^ fxleft[i] ^ eff_k[i];
2480670f1da3SVíctor Colombo         xright[i + 1] = xleft[i];
2481670f1da3SVíctor Colombo     }
2482670f1da3SVíctor Colombo 
2483670f1da3SVíctor Colombo     return (((uint32_t)xright[32]) << 16) | xleft[32];
2484670f1da3SVíctor Colombo }
2485670f1da3SVíctor Colombo 
2486670f1da3SVíctor Colombo static uint64_t hash_digest(uint64_t ra, uint64_t rb, uint64_t key)
2487670f1da3SVíctor Colombo {
2488670f1da3SVíctor Colombo     uint64_t stage0_h = 0ULL, stage0_l = 0ULL;
2489670f1da3SVíctor Colombo     uint64_t stage1_h, stage1_l;
2490670f1da3SVíctor Colombo 
2491670f1da3SVíctor Colombo     for (int i = 0; i < 4; i++) {
2492670f1da3SVíctor Colombo         stage0_h |= ror64(rb & 0xff, 8 * (2 * i + 1));
2493670f1da3SVíctor Colombo         stage0_h |= ((ra >> 32) & 0xff) << (8 * 2 * i);
2494670f1da3SVíctor Colombo         stage0_l |= ror64((rb >> 32) & 0xff, 8 * (2 * i + 1));
2495670f1da3SVíctor Colombo         stage0_l |= (ra & 0xff) << (8 * 2 * i);
2496670f1da3SVíctor Colombo         rb >>= 8;
2497670f1da3SVíctor Colombo         ra >>= 8;
2498670f1da3SVíctor Colombo     }
2499670f1da3SVíctor Colombo 
2500670f1da3SVíctor Colombo     stage1_h = (uint64_t)helper_SIMON_LIKE_32_64(stage0_h >> 32, key, 0) << 32;
2501670f1da3SVíctor Colombo     stage1_h |= helper_SIMON_LIKE_32_64(stage0_h, key, 1);
2502670f1da3SVíctor Colombo     stage1_l = (uint64_t)helper_SIMON_LIKE_32_64(stage0_l >> 32, key, 2) << 32;
2503670f1da3SVíctor Colombo     stage1_l |= helper_SIMON_LIKE_32_64(stage0_l, key, 3);
2504670f1da3SVíctor Colombo 
2505670f1da3SVíctor Colombo     return stage1_h ^ stage1_l;
2506670f1da3SVíctor Colombo }
2507670f1da3SVíctor Colombo 
2508670f1da3SVíctor Colombo #include "qemu/guest-random.h"
2509670f1da3SVíctor Colombo 
2510670f1da3SVíctor Colombo #define HELPER_HASH(op, key, store)                                           \
2511670f1da3SVíctor Colombo void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra,          \
2512670f1da3SVíctor Colombo                  target_ulong rb)                                             \
2513670f1da3SVíctor Colombo {                                                                             \
2514670f1da3SVíctor Colombo     uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash;         \
2515670f1da3SVíctor Colombo                                                                               \
2516670f1da3SVíctor Colombo     if (store) {                                                              \
2517670f1da3SVíctor Colombo         cpu_stq_data_ra(env, ea, calculated_hash, GETPC());                   \
2518670f1da3SVíctor Colombo     } else {                                                                  \
2519670f1da3SVíctor Colombo         loaded_hash = cpu_ldq_data_ra(env, ea, GETPC());                      \
2520670f1da3SVíctor Colombo         if (loaded_hash != calculated_hash) {                                 \
2521670f1da3SVíctor Colombo             raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,                 \
2522670f1da3SVíctor Colombo                 POWERPC_EXCP_TRAP, GETPC());                                  \
2523670f1da3SVíctor Colombo         }                                                                     \
2524670f1da3SVíctor Colombo     }                                                                         \
2525670f1da3SVíctor Colombo }
2526670f1da3SVíctor Colombo 
2527670f1da3SVíctor Colombo HELPER_HASH(HASHST, env->spr[SPR_HASHKEYR], true)
2528670f1da3SVíctor Colombo HELPER_HASH(HASHCHK, env->spr[SPR_HASHKEYR], false)
252953ae2aebSVíctor Colombo HELPER_HASH(HASHSTP, env->spr[SPR_HASHPKEYR], true)
253053ae2aebSVíctor Colombo HELPER_HASH(HASHCHKP, env->spr[SPR_HASHPKEYR], false)
2531670f1da3SVíctor Colombo 
2532ad71ed68SBlue Swirl #if !defined(CONFIG_USER_ONLY)
2533ad71ed68SBlue Swirl 
25342b44e219SBruno Larsen (billionai) #ifdef CONFIG_TCG
2535ad71ed68SBlue Swirl 
2536ad71ed68SBlue Swirl /* Embedded.Processor Control */
2537ad71ed68SBlue Swirl static int dbell2irq(target_ulong rb)
2538ad71ed68SBlue Swirl {
2539ad71ed68SBlue Swirl     int msg = rb & DBELL_TYPE_MASK;
2540ad71ed68SBlue Swirl     int irq = -1;
2541ad71ed68SBlue Swirl 
2542ad71ed68SBlue Swirl     switch (msg) {
2543ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL:
2544ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_DOORBELL;
2545ad71ed68SBlue Swirl         break;
2546ad71ed68SBlue Swirl     case DBELL_TYPE_DBELL_CRIT:
2547ad71ed68SBlue Swirl         irq = PPC_INTERRUPT_CDOORBELL;
2548ad71ed68SBlue Swirl         break;
2549ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL:
2550ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_CRIT:
2551ad71ed68SBlue Swirl     case DBELL_TYPE_G_DBELL_MC:
2552ad71ed68SBlue Swirl         /* XXX implement */
2553ad71ed68SBlue Swirl     default:
2554ad71ed68SBlue Swirl         break;
2555ad71ed68SBlue Swirl     }
2556ad71ed68SBlue Swirl 
2557ad71ed68SBlue Swirl     return irq;
2558ad71ed68SBlue Swirl }
2559ad71ed68SBlue Swirl 
2560e5f17ac6SBlue Swirl void helper_msgclr(CPUPPCState *env, target_ulong rb)
2561ad71ed68SBlue Swirl {
2562ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2563ad71ed68SBlue Swirl 
2564ad71ed68SBlue Swirl     if (irq < 0) {
2565ad71ed68SBlue Swirl         return;
2566ad71ed68SBlue Swirl     }
2567ad71ed68SBlue Swirl 
25687b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), irq, 0);
2569ad71ed68SBlue Swirl }
2570ad71ed68SBlue Swirl 
2571ad71ed68SBlue Swirl void helper_msgsnd(target_ulong rb)
2572ad71ed68SBlue Swirl {
2573ad71ed68SBlue Swirl     int irq = dbell2irq(rb);
2574ad71ed68SBlue Swirl     int pir = rb & DBELL_PIRTAG_MASK;
2575182735efSAndreas Färber     CPUState *cs;
2576ad71ed68SBlue Swirl 
2577ad71ed68SBlue Swirl     if (irq < 0) {
2578ad71ed68SBlue Swirl         return;
2579ad71ed68SBlue Swirl     }
2580ad71ed68SBlue Swirl 
2581f1c29ebcSThomas Huth     qemu_mutex_lock_iothread();
2582bdc44640SAndreas Färber     CPU_FOREACH(cs) {
2583182735efSAndreas Färber         PowerPCCPU *cpu = POWERPC_CPU(cs);
2584182735efSAndreas Färber         CPUPPCState *cenv = &cpu->env;
2585182735efSAndreas Färber 
2586ad71ed68SBlue Swirl         if ((rb & DBELL_BRDCAST) || (cenv->spr[SPR_BOOKE_PIR] == pir)) {
25877b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
2588ad71ed68SBlue Swirl         }
2589ad71ed68SBlue Swirl     }
2590f1c29ebcSThomas Huth     qemu_mutex_unlock_iothread();
2591ad71ed68SBlue Swirl }
25927af1e7b0SCédric Le Goater 
25937af1e7b0SCédric Le Goater /* Server Processor Control */
25947af1e7b0SCédric Le Goater 
25955ba7ba1dSCédric Le Goater static bool dbell_type_server(target_ulong rb)
25965ba7ba1dSCédric Le Goater {
259747733729SDavid Gibson     /*
259847733729SDavid Gibson      * A Directed Hypervisor Doorbell message is sent only if the
25997af1e7b0SCédric Le Goater      * message type is 5. All other types are reserved and the
260047733729SDavid Gibson      * instruction is a no-op
260147733729SDavid Gibson      */
26025ba7ba1dSCédric Le Goater     return (rb & DBELL_TYPE_MASK) == DBELL_TYPE_DBELL_SERVER;
26037af1e7b0SCédric Le Goater }
26047af1e7b0SCédric Le Goater 
26057af1e7b0SCédric Le Goater void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb)
26067af1e7b0SCédric Le Goater {
26075ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
26087af1e7b0SCédric Le Goater         return;
26097af1e7b0SCédric Le Goater     }
26107af1e7b0SCédric Le Goater 
26117b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
26127af1e7b0SCédric Le Goater }
26137af1e7b0SCédric Le Goater 
26145ba7ba1dSCédric Le Goater static void book3s_msgsnd_common(int pir, int irq)
26157af1e7b0SCédric Le Goater {
26167af1e7b0SCédric Le Goater     CPUState *cs;
26177af1e7b0SCédric Le Goater 
26187af1e7b0SCédric Le Goater     qemu_mutex_lock_iothread();
26197af1e7b0SCédric Le Goater     CPU_FOREACH(cs) {
26207af1e7b0SCédric Le Goater         PowerPCCPU *cpu = POWERPC_CPU(cs);
26217af1e7b0SCédric Le Goater         CPUPPCState *cenv = &cpu->env;
26227af1e7b0SCédric Le Goater 
26237af1e7b0SCédric Le Goater         /* TODO: broadcast message to all threads of the same  processor */
26247af1e7b0SCédric Le Goater         if (cenv->spr_cb[SPR_PIR].default_value == pir) {
26257b694df6SMatheus Ferst             ppc_set_irq(cpu, irq, 1);
26267af1e7b0SCédric Le Goater         }
26277af1e7b0SCédric Le Goater     }
26287af1e7b0SCédric Le Goater     qemu_mutex_unlock_iothread();
26297af1e7b0SCédric Le Goater }
26305ba7ba1dSCédric Le Goater 
26315ba7ba1dSCédric Le Goater void helper_book3s_msgsnd(target_ulong rb)
26325ba7ba1dSCédric Le Goater {
26335ba7ba1dSCédric Le Goater     int pir = rb & DBELL_PROCIDTAG_MASK;
26345ba7ba1dSCédric Le Goater 
26355ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
26365ba7ba1dSCédric Le Goater         return;
26375ba7ba1dSCédric Le Goater     }
26385ba7ba1dSCédric Le Goater 
26395ba7ba1dSCédric Le Goater     book3s_msgsnd_common(pir, PPC_INTERRUPT_HDOORBELL);
26405ba7ba1dSCédric Le Goater }
26415ba7ba1dSCédric Le Goater 
26425ba7ba1dSCédric Le Goater #if defined(TARGET_PPC64)
26435ba7ba1dSCédric Le Goater void helper_book3s_msgclrp(CPUPPCState *env, target_ulong rb)
26445ba7ba1dSCédric Le Goater {
2645493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgclrp", HFSCR_IC_MSGP);
2646493028d8SCédric Le Goater 
26475ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
26485ba7ba1dSCédric Le Goater         return;
26495ba7ba1dSCédric Le Goater     }
26505ba7ba1dSCédric Le Goater 
26517b694df6SMatheus Ferst     ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
26525ba7ba1dSCédric Le Goater }
26535ba7ba1dSCédric Le Goater 
26545ba7ba1dSCédric Le Goater /*
26555ba7ba1dSCédric Le Goater  * sends a message to other threads that are on the same
26565ba7ba1dSCédric Le Goater  * multi-threaded processor
26575ba7ba1dSCédric Le Goater  */
26585ba7ba1dSCédric Le Goater void helper_book3s_msgsndp(CPUPPCState *env, target_ulong rb)
26595ba7ba1dSCédric Le Goater {
26605ba7ba1dSCédric Le Goater     int pir = env->spr_cb[SPR_PIR].default_value;
26615ba7ba1dSCédric Le Goater 
2662493028d8SCédric Le Goater     helper_hfscr_facility_check(env, HFSCR_MSGP, "msgsndp", HFSCR_IC_MSGP);
2663493028d8SCédric Le Goater 
26645ba7ba1dSCédric Le Goater     if (!dbell_type_server(rb)) {
26655ba7ba1dSCédric Le Goater         return;
26665ba7ba1dSCédric Le Goater     }
26675ba7ba1dSCédric Le Goater 
26685ba7ba1dSCédric Le Goater     /* TODO: TCG supports only one thread */
26695ba7ba1dSCédric Le Goater 
26705ba7ba1dSCédric Le Goater     book3s_msgsnd_common(pir, PPC_INTERRUPT_DOORBELL);
26715ba7ba1dSCédric Le Goater }
2672996473e4SRichard Henderson #endif /* TARGET_PPC64 */
26730f3110faSRichard Henderson 
26740f3110faSRichard Henderson void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
26750f3110faSRichard Henderson                                  MMUAccessType access_type,
26760f3110faSRichard Henderson                                  int mmu_idx, uintptr_t retaddr)
26770f3110faSRichard Henderson {
26780f3110faSRichard Henderson     CPUPPCState *env = cs->env_ptr;
267929c4a336SFabiano Rosas     uint32_t insn;
268029c4a336SFabiano Rosas 
268129c4a336SFabiano Rosas     /* Restore state and reload the insn we executed, for filling in DSISR.  */
268229c4a336SFabiano Rosas     cpu_restore_state(cs, retaddr, true);
268329c4a336SFabiano Rosas     insn = cpu_ldl_code(env, env->nip);
26840f3110faSRichard Henderson 
2685a7e3af13SRichard Henderson     switch (env->mmu_model) {
2686a7e3af13SRichard Henderson     case POWERPC_MMU_SOFT_4xx:
2687a7e3af13SRichard Henderson         env->spr[SPR_40x_DEAR] = vaddr;
2688a7e3af13SRichard Henderson         break;
2689a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE:
2690a7e3af13SRichard Henderson     case POWERPC_MMU_BOOKE206:
2691a7e3af13SRichard Henderson         env->spr[SPR_BOOKE_DEAR] = vaddr;
2692a7e3af13SRichard Henderson         break;
2693a7e3af13SRichard Henderson     default:
2694a7e3af13SRichard Henderson         env->spr[SPR_DAR] = vaddr;
2695a7e3af13SRichard Henderson         break;
2696a7e3af13SRichard Henderson     }
2697a7e3af13SRichard Henderson 
26980f3110faSRichard Henderson     cs->exception_index = POWERPC_EXCP_ALIGN;
269929c4a336SFabiano Rosas     env->error_code = insn & 0x03FF0000;
270029c4a336SFabiano Rosas     cpu_loop_exit(cs);
27010f3110faSRichard Henderson }
2702996473e4SRichard Henderson #endif /* CONFIG_TCG */
2703996473e4SRichard Henderson #endif /* !CONFIG_USER_ONLY */
2704