2 * PowerPC exception emulation helpers for QEMU.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
20 #include "qemu/main-loop.h"
23 #include "exec/exec-all.h"
25 #include "helper_regs.h"
26 #include "hw/ppc/ppc.h"
31 #include "exec/helper-proto.h"
32 #include "exec/cpu_ldst.h"
35 /*****************************************************************************/
36 /* Exception processing */
37 #if !defined(CONFIG_USER_ONLY)
39 static const char *powerpc_excp_name(int excp)
42 case POWERPC_EXCP_CRITICAL: return "CRITICAL";
43 case POWERPC_EXCP_MCHECK: return "MCHECK";
44 case POWERPC_EXCP_DSI: return "DSI";
45 case POWERPC_EXCP_ISI: return "ISI";
46 case POWERPC_EXCP_EXTERNAL: return "EXTERNAL";
47 case POWERPC_EXCP_ALIGN: return "ALIGN";
48 case POWERPC_EXCP_PROGRAM: return "PROGRAM";
49 case POWERPC_EXCP_FPU: return "FPU";
50 case POWERPC_EXCP_SYSCALL: return "SYSCALL";
51 case POWERPC_EXCP_APU: return "APU";
52 case POWERPC_EXCP_DECR: return "DECR";
53 case POWERPC_EXCP_FIT: return "FIT";
54 case POWERPC_EXCP_WDT: return "WDT";
55 case POWERPC_EXCP_DTLB: return "DTLB";
56 case POWERPC_EXCP_ITLB: return "ITLB";
57 case POWERPC_EXCP_DEBUG: return "DEBUG";
58 case POWERPC_EXCP_SPEU: return "SPEU";
59 case POWERPC_EXCP_EFPDI: return "EFPDI";
60 case POWERPC_EXCP_EFPRI: return "EFPRI";
61 case POWERPC_EXCP_EPERFM: return "EPERFM";
62 case POWERPC_EXCP_DOORI: return "DOORI";
63 case POWERPC_EXCP_DOORCI: return "DOORCI";
64 case POWERPC_EXCP_GDOORI: return "GDOORI";
65 case POWERPC_EXCP_GDOORCI: return "GDOORCI";
66 case POWERPC_EXCP_HYPPRIV: return "HYPPRIV";
67 case POWERPC_EXCP_RESET: return "RESET";
68 case POWERPC_EXCP_DSEG: return "DSEG";
69 case POWERPC_EXCP_ISEG: return "ISEG";
70 case POWERPC_EXCP_HDECR: return "HDECR";
71 case POWERPC_EXCP_TRACE: return "TRACE";
72 case POWERPC_EXCP_HDSI: return "HDSI";
73 case POWERPC_EXCP_HISI: return "HISI";
74 case POWERPC_EXCP_HDSEG: return "HDSEG";
75 case POWERPC_EXCP_HISEG: return "HISEG";
76 case POWERPC_EXCP_VPU: return "VPU";
77 case POWERPC_EXCP_PIT: return "PIT";
78 case POWERPC_EXCP_EMUL: return "EMUL";
79 case POWERPC_EXCP_IFTLB: return "IFTLB";
80 case POWERPC_EXCP_DLTLB: return "DLTLB";
81 case POWERPC_EXCP_DSTLB: return "DSTLB";
82 case POWERPC_EXCP_FPA: return "FPA";
83 case POWERPC_EXCP_DABR: return "DABR";
84 case POWERPC_EXCP_IABR: return "IABR";
85 case POWERPC_EXCP_SMI: return "SMI";
86 case POWERPC_EXCP_PERFM: return "PERFM";
87 case POWERPC_EXCP_THERM: return "THERM";
88 case POWERPC_EXCP_VPUA: return "VPUA";
89 case POWERPC_EXCP_SOFTP: return "SOFTP";
90 case POWERPC_EXCP_MAINT: return "MAINT";
91 case POWERPC_EXCP_MEXTBR: return "MEXTBR";
92 case POWERPC_EXCP_NMEXTBR: return "NMEXTBR";
93 case POWERPC_EXCP_ITLBE: return "ITLBE";
94 case POWERPC_EXCP_DTLBE: return "DTLBE";
95 case POWERPC_EXCP_VSXU: return "VSXU";
96 case POWERPC_EXCP_FU: return "FU";
97 case POWERPC_EXCP_HV_EMU: return "HV_EMU";
98 case POWERPC_EXCP_HV_MAINT: return "HV_MAINT";
99 case POWERPC_EXCP_HV_FU: return "HV_FU";
100 case POWERPC_EXCP_SDOOR: return "SDOOR";
101 case POWERPC_EXCP_SDOOR_HV: return "SDOOR_HV";
102 case POWERPC_EXCP_HVIRT: return "HVIRT";
103 case POWERPC_EXCP_SYSCALL_VECTORED: return "SYSCALL_VECTORED";
105 g_assert_not_reached();
109 static void dump_syscall(CPUPPCState *env)
111 qemu_log_mask(CPU_LOG_INT, "syscall r0=%016" PRIx64
112 " r3=%016" PRIx64 " r4=%016" PRIx64 " r5=%016" PRIx64
113 " r6=%016" PRIx64 " r7=%016" PRIx64 " r8=%016" PRIx64
114 " nip=" TARGET_FMT_lx "\n",
115 ppc_dump_gpr(env, 0), ppc_dump_gpr(env, 3),
116 ppc_dump_gpr(env, 4), ppc_dump_gpr(env, 5),
117 ppc_dump_gpr(env, 6), ppc_dump_gpr(env, 7),
118 ppc_dump_gpr(env, 8), env->nip);
121 static void dump_hcall(CPUPPCState *env)
123 qemu_log_mask(CPU_LOG_INT, "hypercall r3=%016" PRIx64
124 " r4=%016" PRIx64 " r5=%016" PRIx64 " r6=%016" PRIx64
125 " r7=%016" PRIx64 " r8=%016" PRIx64 " r9=%016" PRIx64
126 " r10=%016" PRIx64 " r11=%016" PRIx64 " r12=%016" PRIx64
127 " nip=" TARGET_FMT_lx "\n",
128 ppc_dump_gpr(env, 3), ppc_dump_gpr(env, 4),
129 ppc_dump_gpr(env, 5), ppc_dump_gpr(env, 6),
130 ppc_dump_gpr(env, 7), ppc_dump_gpr(env, 8),
131 ppc_dump_gpr(env, 9), ppc_dump_gpr(env, 10),
132 ppc_dump_gpr(env, 11), ppc_dump_gpr(env, 12),
136 static void ppc_excp_debug_sw_tlb(CPUPPCState *env, int excp)
139 target_ulong *miss, *cmp;
142 if (!qemu_loglevel_mask(CPU_LOG_MMU)) {
146 if (excp == POWERPC_EXCP_IFTLB) {
149 miss = &env->spr[SPR_IMISS];
150 cmp = &env->spr[SPR_ICMP];
152 if (excp == POWERPC_EXCP_DLTLB) {
158 miss = &env->spr[SPR_DMISS];
159 cmp = &env->spr[SPR_DCMP];
161 qemu_log("6xx %sTLB miss: %cM " TARGET_FMT_lx " %cC "
162 TARGET_FMT_lx " H1 " TARGET_FMT_lx " H2 "
163 TARGET_FMT_lx " %08x\n", es, en, *miss, en, *cmp,
164 env->spr[SPR_HASH1], env->spr[SPR_HASH2],
168 #if defined(TARGET_PPC64)
169 static int powerpc_reset_wakeup(CPUState *cs, CPUPPCState *env, int excp,
172 /* We no longer are in a PM state */
173 env->resume_as_sreset = false;
175 /* Pretend to be returning from doze always as we don't lose state */
176 *msr |= SRR1_WS_NOLOSS;
178 /* Machine checks are sent normally */
179 if (excp == POWERPC_EXCP_MCHECK) {
183 case POWERPC_EXCP_RESET:
184 *msr |= SRR1_WAKERESET;
186 case POWERPC_EXCP_EXTERNAL:
189 case POWERPC_EXCP_DECR:
190 *msr |= SRR1_WAKEDEC;
192 case POWERPC_EXCP_SDOOR:
193 *msr |= SRR1_WAKEDBELL;
195 case POWERPC_EXCP_SDOOR_HV:
196 *msr |= SRR1_WAKEHDBELL;
198 case POWERPC_EXCP_HV_MAINT:
199 *msr |= SRR1_WAKEHMI;
201 case POWERPC_EXCP_HVIRT:
202 *msr |= SRR1_WAKEHVI;
205 cpu_abort(cs, "Unsupported exception %d in Power Save mode\n",
208 return POWERPC_EXCP_RESET;
212 * AIL - Alternate Interrupt Location, a mode that allows interrupts to be
213 * taken with the MMU on, and which uses an alternate location (e.g., so the
214 * kernel/hv can map the vectors there with an effective address).
216 * An interrupt is considered to be taken "with AIL" or "AIL applies" if they
217 * are delivered in this way. AIL requires the LPCR to be set to enable this
218 * mode, and then a number of conditions have to be true for AIL to apply.
220 * First of all, SRESET, MCE, and HMI are always delivered without AIL, because
221 * they specifically want to be in real mode (e.g., the MCE might be signaling
222 * a SLB multi-hit which requires SLB flush before the MMU can be enabled).
224 * After that, behaviour depends on the current MSR[IR], MSR[DR], MSR[HV],
225 * whether or not the interrupt changes MSR[HV] from 0 to 1, and the current
226 * radix mode (LPCR[HR]).
228 * POWER8, POWER9 with LPCR[HR]=0
229 * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
230 * +-----------+-------------+---------+-------------+-----+
231 * | a | 00/01/10 | x | x | 0 |
232 * | a | 11 | 0 | 1 | 0 |
233 * | a | 11 | 1 | 1 | a |
234 * | a | 11 | 0 | 0 | a |
235 * +-------------------------------------------------------+
237 * POWER9 with LPCR[HR]=1
238 * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
239 * +-----------+-------------+---------+-------------+-----+
240 * | a | 00/01/10 | x | x | 0 |
241 * | a | 11 | x | x | a |
242 * +-------------------------------------------------------+
244 * The difference with POWER9 being that MSR[HV] 0->1 interrupts can be sent to
245 * the hypervisor in AIL mode if the guest is radix. This is good for
246 * performance but allows the guest to influence the AIL of hypervisor
247 * interrupts using its MSR, and also the hypervisor must disallow guest
248 * interrupts (MSR[HV] 0->0) from using AIL if the hypervisor does not want to
249 * use AIL for its MSR[HV] 0->1 interrupts.
251 * POWER10 addresses those issues with a new LPCR[HAIL] bit that is applied to
252 * interrupts that begin execution with MSR[HV]=1 (so both MSR[HV] 0->1 and
255 * HAIL=1 is equivalent to AIL=3, for interrupts delivered with MSR[HV]=1.
257 * POWER10 behaviour is
258 * | LPCR[AIL] | LPCR[HAIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
259 * +-----------+------------+-------------+---------+-------------+-----+
260 * | a | h | 00/01/10 | 0 | 0 | 0 |
261 * | a | h | 11 | 0 | 0 | a |
262 * | a | h | x | 0 | 1 | h |
263 * | a | h | 00/01/10 | 1 | 1 | 0 |
264 * | a | h | 11 | 1 | 1 | h |
265 * +--------------------------------------------------------------------+
267 static void ppc_excp_apply_ail(PowerPCCPU *cpu, int excp, target_ulong msr,
268 target_ulong *new_msr, target_ulong *vector)
270 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
271 CPUPPCState *env = &cpu->env;
272 bool mmu_all_on = ((msr >> MSR_IR) & 1) && ((msr >> MSR_DR) & 1);
273 bool hv_escalation = !(msr & MSR_HVB) && (*new_msr & MSR_HVB);
276 if (excp == POWERPC_EXCP_MCHECK ||
277 excp == POWERPC_EXCP_RESET ||
278 excp == POWERPC_EXCP_HV_MAINT) {
279 /* SRESET, MCE, HMI never apply AIL */
283 if (!(pcc->lpcr_mask & LPCR_AIL)) {
284 /* This CPU does not have AIL */
289 if (!(pcc->lpcr_mask & LPCR_HAIL)) {
291 /* AIL only works if MSR[IR] and MSR[DR] are both enabled. */
294 if (hv_escalation && !(env->spr[SPR_LPCR] & LPCR_HR)) {
296 * AIL does not work if there is a MSR[HV] 0->1 transition and the
297 * partition is in HPT mode. For radix guests, such interrupts are
298 * allowed to be delivered to the hypervisor in ail mode.
303 ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT;
308 /* AIL=1 is reserved, treat it like AIL=0 */
314 if (!mmu_all_on && !hv_escalation) {
316 * AIL works for HV interrupts even with guest MSR[IR/DR] disabled.
317 * Guest->guest and HV->HV interrupts do require MMU on.
322 if (*new_msr & MSR_HVB) {
323 if (!(env->spr[SPR_LPCR] & LPCR_HAIL)) {
324 /* HV interrupts depend on LPCR[HAIL] */
327 ail = 3; /* HAIL=1 gives AIL=3 behaviour for HV interrupts */
329 ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT;
334 if (ail == 1 || ail == 2) {
335 /* AIL=1 and AIL=2 are reserved, treat them like AIL=0 */
341 * AIL applies, so the new MSR gets IR and DR set, and an offset applied
344 *new_msr |= (1 << MSR_IR) | (1 << MSR_DR);
346 if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
348 *vector |= 0x0000000000018000ull;
349 } else if (ail == 3) {
350 *vector |= 0xc000000000004000ull;
354 * scv AIL is a little different. AIL=2 does not change the address,
355 * only the MSR. AIL=3 replaces the 0x17000 base with 0xc...3000.
358 *vector &= ~0x0000000000017000ull; /* Un-apply the base offset */
359 *vector |= 0xc000000000003000ull; /* Apply scv's AIL=3 offset */
365 static void powerpc_reset_excp_state(PowerPCCPU *cpu)
367 CPUState *cs = CPU(cpu);
368 CPUPPCState *env = &cpu->env;
370 /* Reset exception state */
371 cs->exception_index = POWERPC_EXCP_NONE;
375 static void powerpc_set_excp_state(PowerPCCPU *cpu, target_ulong vector,
378 CPUPPCState *env = &cpu->env;
380 assert((msr & env->msr_mask) == msr);
383 * We don't use hreg_store_msr here as already have treated any
384 * special case that could occur. Just store MSR and update hflags
386 * Note: We *MUST* not use hreg_store_msr() as-is anyway because it
387 * will prevent setting of the HV bit which some exceptions might need
392 hreg_compute_hflags(env);
393 ppc_maybe_interrupt(env);
395 powerpc_reset_excp_state(cpu);
398 * Any interrupt is context synchronizing, check if TCG TLB needs
399 * a delayed flush on ppc64
401 check_tlb_flush(env, false);
403 /* Reset the reservation */
404 env->reserve_addr = -1;
407 static void powerpc_excp_40x(PowerPCCPU *cpu, int excp)
409 CPUState *cs = CPU(cpu);
410 CPUPPCState *env = &cpu->env;
411 target_ulong msr, new_msr, vector;
414 /* new srr1 value excluding must-be-zero bits */
415 msr = env->msr & ~0x783f0000ULL;
418 * new interrupt handler msr preserves existing ME unless
419 * explicitly overriden.
421 new_msr = env->msr & (((target_ulong)1 << MSR_ME));
423 /* target registers */
428 * Hypervisor emulation assistance interrupt only exists on server
429 * arch 2.05 server or later.
431 if (excp == POWERPC_EXCP_HV_EMU) {
432 excp = POWERPC_EXCP_PROGRAM;
435 vector = env->excp_vectors[excp];
436 if (vector == (target_ulong)-1ULL) {
437 cpu_abort(cs, "Raised an exception without defined vector %d\n",
441 vector |= env->excp_prefix;
444 case POWERPC_EXCP_CRITICAL: /* Critical input */
448 case POWERPC_EXCP_MCHECK: /* Machine check exception */
449 if (!FIELD_EX64(env->msr, MSR, ME)) {
451 * Machine check exception is not enabled. Enter
454 fprintf(stderr, "Machine check while not allowed. "
455 "Entering checkstop state\n");
456 if (qemu_log_separate()) {
457 qemu_log("Machine check while not allowed. "
458 "Entering checkstop state\n");
461 cpu_interrupt_exittb(cs);
464 /* machine check exceptions don't have ME set */
465 new_msr &= ~((target_ulong)1 << MSR_ME);
470 case POWERPC_EXCP_DSI: /* Data storage exception */
471 trace_ppc_excp_dsi(env->spr[SPR_40x_ESR], env->spr[SPR_40x_DEAR]);
473 case POWERPC_EXCP_ISI: /* Instruction storage exception */
474 trace_ppc_excp_isi(msr, env->nip);
476 case POWERPC_EXCP_EXTERNAL: /* External input */
478 case POWERPC_EXCP_ALIGN: /* Alignment exception */
480 case POWERPC_EXCP_PROGRAM: /* Program exception */
481 switch (env->error_code & ~0xF) {
482 case POWERPC_EXCP_FP:
483 if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
484 trace_ppc_excp_fp_ignore();
485 powerpc_reset_excp_state(cpu);
488 env->spr[SPR_40x_ESR] = ESR_FP;
490 case POWERPC_EXCP_INVAL:
491 trace_ppc_excp_inval(env->nip);
492 env->spr[SPR_40x_ESR] = ESR_PIL;
494 case POWERPC_EXCP_PRIV:
495 env->spr[SPR_40x_ESR] = ESR_PPR;
497 case POWERPC_EXCP_TRAP:
498 env->spr[SPR_40x_ESR] = ESR_PTR;
501 cpu_abort(cs, "Invalid program exception %d. Aborting\n",
506 case POWERPC_EXCP_SYSCALL: /* System call exception */
510 * We need to correct the NIP which in this case is supposed
511 * to point to the next instruction
515 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
516 trace_ppc_excp_print("FIT");
518 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
519 trace_ppc_excp_print("WDT");
521 case POWERPC_EXCP_DTLB: /* Data TLB error */
522 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
524 case POWERPC_EXCP_PIT: /* Programmable interval timer interrupt */
525 trace_ppc_excp_print("PIT");
527 case POWERPC_EXCP_DEBUG: /* Debug interrupt */
528 cpu_abort(cs, "%s exception not implemented\n",
529 powerpc_excp_name(excp));
532 cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
537 env->spr[srr0] = env->nip;
540 env->spr[srr1] = msr;
542 powerpc_set_excp_state(cpu, vector, new_msr);
545 static void powerpc_excp_6xx(PowerPCCPU *cpu, int excp)
547 CPUState *cs = CPU(cpu);
548 CPUPPCState *env = &cpu->env;
549 target_ulong msr, new_msr, vector;
551 /* new srr1 value excluding must-be-zero bits */
552 msr = env->msr & ~0x783f0000ULL;
555 * new interrupt handler msr preserves existing ME unless
556 * explicitly overriden
558 new_msr = env->msr & ((target_ulong)1 << MSR_ME);
561 * Hypervisor emulation assistance interrupt only exists on server
562 * arch 2.05 server or later.
564 if (excp == POWERPC_EXCP_HV_EMU) {
565 excp = POWERPC_EXCP_PROGRAM;
568 vector = env->excp_vectors[excp];
569 if (vector == (target_ulong)-1ULL) {
570 cpu_abort(cs, "Raised an exception without defined vector %d\n",
574 vector |= env->excp_prefix;
577 case POWERPC_EXCP_CRITICAL: /* Critical input */
579 case POWERPC_EXCP_MCHECK: /* Machine check exception */
580 if (!FIELD_EX64(env->msr, MSR, ME)) {
582 * Machine check exception is not enabled. Enter
585 fprintf(stderr, "Machine check while not allowed. "
586 "Entering checkstop state\n");
587 if (qemu_log_separate()) {
588 qemu_log("Machine check while not allowed. "
589 "Entering checkstop state\n");
592 cpu_interrupt_exittb(cs);
595 /* machine check exceptions don't have ME set */
596 new_msr &= ~((target_ulong)1 << MSR_ME);
599 case POWERPC_EXCP_DSI: /* Data storage exception */
600 trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
602 case POWERPC_EXCP_ISI: /* Instruction storage exception */
603 trace_ppc_excp_isi(msr, env->nip);
604 msr |= env->error_code;
606 case POWERPC_EXCP_EXTERNAL: /* External input */
608 case POWERPC_EXCP_ALIGN: /* Alignment exception */
609 /* Get rS/rD and rA from faulting opcode */
611 * Note: the opcode fields will not be set properly for a
612 * direct store load/store, but nobody cares as nobody
613 * actually uses direct store segments.
615 env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
617 case POWERPC_EXCP_PROGRAM: /* Program exception */
618 switch (env->error_code & ~0xF) {
619 case POWERPC_EXCP_FP:
620 if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
621 trace_ppc_excp_fp_ignore();
622 powerpc_reset_excp_state(cpu);
627 * FP exceptions always have NIP pointing to the faulting
628 * instruction, so always use store_next and claim we are
629 * precise in the MSR.
633 case POWERPC_EXCP_INVAL:
634 trace_ppc_excp_inval(env->nip);
637 case POWERPC_EXCP_PRIV:
640 case POWERPC_EXCP_TRAP:
644 /* Should never occur */
645 cpu_abort(cs, "Invalid program exception %d. Aborting\n",
650 case POWERPC_EXCP_SYSCALL: /* System call exception */
654 * We need to correct the NIP which in this case is supposed
655 * to point to the next instruction
659 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
660 case POWERPC_EXCP_DECR: /* Decrementer exception */
662 case POWERPC_EXCP_DTLB: /* Data TLB error */
663 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
665 case POWERPC_EXCP_RESET: /* System reset exception */
666 if (FIELD_EX64(env->msr, MSR, POW)) {
667 cpu_abort(cs, "Trying to deliver power-saving system reset "
668 "exception %d with no HV support\n", excp);
671 case POWERPC_EXCP_TRACE: /* Trace exception */
673 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
674 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
675 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
676 /* Swap temporary saved registers with GPRs */
677 if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
678 new_msr |= (target_ulong)1 << MSR_TGPR;
679 hreg_swap_gpr_tgpr(env);
682 ppc_excp_debug_sw_tlb(env, excp);
684 msr |= env->crf[0] << 28;
685 msr |= env->error_code; /* key, D/I, S/L bits */
686 /* Set way using a LRU mechanism */
687 msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
689 case POWERPC_EXCP_FPA: /* Floating-point assist exception */
690 case POWERPC_EXCP_DABR: /* Data address breakpoint */
691 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
692 case POWERPC_EXCP_SMI: /* System management interrupt */
693 case POWERPC_EXCP_MEXTBR: /* Maskable external breakpoint */
694 case POWERPC_EXCP_NMEXTBR: /* Non maskable external breakpoint */
695 cpu_abort(cs, "%s exception not implemented\n",
696 powerpc_excp_name(excp));
699 cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
704 * Sort out endianness of interrupt, this differs depending on the
705 * CPU, the HV mode, etc...
707 if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
708 new_msr |= (target_ulong)1 << MSR_LE;
712 env->spr[SPR_SRR0] = env->nip;
715 env->spr[SPR_SRR1] = msr;
717 powerpc_set_excp_state(cpu, vector, new_msr);
720 static void powerpc_excp_7xx(PowerPCCPU *cpu, int excp)
722 CPUState *cs = CPU(cpu);
723 CPUPPCState *env = &cpu->env;
724 target_ulong msr, new_msr, vector;
726 /* new srr1 value excluding must-be-zero bits */
727 msr = env->msr & ~0x783f0000ULL;
730 * new interrupt handler msr preserves existing ME unless
731 * explicitly overriden
733 new_msr = env->msr & ((target_ulong)1 << MSR_ME);
736 * Hypervisor emulation assistance interrupt only exists on server
737 * arch 2.05 server or later.
739 if (excp == POWERPC_EXCP_HV_EMU) {
740 excp = POWERPC_EXCP_PROGRAM;
743 vector = env->excp_vectors[excp];
744 if (vector == (target_ulong)-1ULL) {
745 cpu_abort(cs, "Raised an exception without defined vector %d\n",
749 vector |= env->excp_prefix;
752 case POWERPC_EXCP_MCHECK: /* Machine check exception */
753 if (!FIELD_EX64(env->msr, MSR, ME)) {
755 * Machine check exception is not enabled. Enter
758 fprintf(stderr, "Machine check while not allowed. "
759 "Entering checkstop state\n");
760 if (qemu_log_separate()) {
761 qemu_log("Machine check while not allowed. "
762 "Entering checkstop state\n");
765 cpu_interrupt_exittb(cs);
768 /* machine check exceptions don't have ME set */
769 new_msr &= ~((target_ulong)1 << MSR_ME);
772 case POWERPC_EXCP_DSI: /* Data storage exception */
773 trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
775 case POWERPC_EXCP_ISI: /* Instruction storage exception */
776 trace_ppc_excp_isi(msr, env->nip);
777 msr |= env->error_code;
779 case POWERPC_EXCP_EXTERNAL: /* External input */
781 case POWERPC_EXCP_ALIGN: /* Alignment exception */
782 /* Get rS/rD and rA from faulting opcode */
784 * Note: the opcode fields will not be set properly for a
785 * direct store load/store, but nobody cares as nobody
786 * actually uses direct store segments.
788 env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
790 case POWERPC_EXCP_PROGRAM: /* Program exception */
791 switch (env->error_code & ~0xF) {
792 case POWERPC_EXCP_FP:
793 if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
794 trace_ppc_excp_fp_ignore();
795 powerpc_reset_excp_state(cpu);
800 * FP exceptions always have NIP pointing to the faulting
801 * instruction, so always use store_next and claim we are
802 * precise in the MSR.
806 case POWERPC_EXCP_INVAL:
807 trace_ppc_excp_inval(env->nip);
810 case POWERPC_EXCP_PRIV:
813 case POWERPC_EXCP_TRAP:
817 /* Should never occur */
818 cpu_abort(cs, "Invalid program exception %d. Aborting\n",
823 case POWERPC_EXCP_SYSCALL: /* System call exception */
825 int lev = env->error_code;
827 if (lev == 1 && cpu->vhyp) {
834 * We need to correct the NIP which in this case is supposed
835 * to point to the next instruction
840 * The Virtual Open Firmware (VOF) relies on the 'sc 1'
841 * instruction to communicate with QEMU. The pegasos2 machine
842 * uses VOF and the 7xx CPUs, so although the 7xx don't have
843 * HV mode, we need to keep hypercall support.
845 if (lev == 1 && cpu->vhyp) {
846 PPCVirtualHypervisorClass *vhc =
847 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
848 vhc->hypercall(cpu->vhyp, cpu);
854 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
855 case POWERPC_EXCP_DECR: /* Decrementer exception */
857 case POWERPC_EXCP_RESET: /* System reset exception */
858 if (FIELD_EX64(env->msr, MSR, POW)) {
859 cpu_abort(cs, "Trying to deliver power-saving system reset "
860 "exception %d with no HV support\n", excp);
863 case POWERPC_EXCP_TRACE: /* Trace exception */
865 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
866 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
867 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
868 ppc_excp_debug_sw_tlb(env, excp);
870 msr |= env->crf[0] << 28;
871 msr |= env->error_code; /* key, D/I, S/L bits */
872 /* Set way using a LRU mechanism */
873 msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
876 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
877 case POWERPC_EXCP_SMI: /* System management interrupt */
878 case POWERPC_EXCP_THERM: /* Thermal interrupt */
879 case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */
880 cpu_abort(cs, "%s exception not implemented\n",
881 powerpc_excp_name(excp));
884 cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
889 * Sort out endianness of interrupt, this differs depending on the
890 * CPU, the HV mode, etc...
892 if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
893 new_msr |= (target_ulong)1 << MSR_LE;
897 env->spr[SPR_SRR0] = env->nip;
900 env->spr[SPR_SRR1] = msr;
902 powerpc_set_excp_state(cpu, vector, new_msr);
905 static void powerpc_excp_74xx(PowerPCCPU *cpu, int excp)
907 CPUState *cs = CPU(cpu);
908 CPUPPCState *env = &cpu->env;
909 target_ulong msr, new_msr, vector;
911 /* new srr1 value excluding must-be-zero bits */
912 msr = env->msr & ~0x783f0000ULL;
915 * new interrupt handler msr preserves existing ME unless
916 * explicitly overriden
918 new_msr = env->msr & ((target_ulong)1 << MSR_ME);
921 * Hypervisor emulation assistance interrupt only exists on server
922 * arch 2.05 server or later.
924 if (excp == POWERPC_EXCP_HV_EMU) {
925 excp = POWERPC_EXCP_PROGRAM;
928 vector = env->excp_vectors[excp];
929 if (vector == (target_ulong)-1ULL) {
930 cpu_abort(cs, "Raised an exception without defined vector %d\n",
934 vector |= env->excp_prefix;
937 case POWERPC_EXCP_MCHECK: /* Machine check exception */
938 if (!FIELD_EX64(env->msr, MSR, ME)) {
940 * Machine check exception is not enabled. Enter
943 fprintf(stderr, "Machine check while not allowed. "
944 "Entering checkstop state\n");
945 if (qemu_log_separate()) {
946 qemu_log("Machine check while not allowed. "
947 "Entering checkstop state\n");
950 cpu_interrupt_exittb(cs);
953 /* machine check exceptions don't have ME set */
954 new_msr &= ~((target_ulong)1 << MSR_ME);
957 case POWERPC_EXCP_DSI: /* Data storage exception */
958 trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
960 case POWERPC_EXCP_ISI: /* Instruction storage exception */
961 trace_ppc_excp_isi(msr, env->nip);
962 msr |= env->error_code;
964 case POWERPC_EXCP_EXTERNAL: /* External input */
966 case POWERPC_EXCP_ALIGN: /* Alignment exception */
967 /* Get rS/rD and rA from faulting opcode */
969 * Note: the opcode fields will not be set properly for a
970 * direct store load/store, but nobody cares as nobody
971 * actually uses direct store segments.
973 env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
975 case POWERPC_EXCP_PROGRAM: /* Program exception */
976 switch (env->error_code & ~0xF) {
977 case POWERPC_EXCP_FP:
978 if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
979 trace_ppc_excp_fp_ignore();
980 powerpc_reset_excp_state(cpu);
985 * FP exceptions always have NIP pointing to the faulting
986 * instruction, so always use store_next and claim we are
987 * precise in the MSR.
991 case POWERPC_EXCP_INVAL:
992 trace_ppc_excp_inval(env->nip);
995 case POWERPC_EXCP_PRIV:
998 case POWERPC_EXCP_TRAP:
1002 /* Should never occur */
1003 cpu_abort(cs, "Invalid program exception %d. Aborting\n",
1008 case POWERPC_EXCP_SYSCALL: /* System call exception */
1010 int lev = env->error_code;
1012 if ((lev == 1) && cpu->vhyp) {
1019 * We need to correct the NIP which in this case is supposed
1020 * to point to the next instruction
1025 * The Virtual Open Firmware (VOF) relies on the 'sc 1'
1026 * instruction to communicate with QEMU. The pegasos2 machine
1027 * uses VOF and the 74xx CPUs, so although the 74xx don't have
1028 * HV mode, we need to keep hypercall support.
1030 if ((lev == 1) && cpu->vhyp) {
1031 PPCVirtualHypervisorClass *vhc =
1032 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
1033 vhc->hypercall(cpu->vhyp, cpu);
1039 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
1040 case POWERPC_EXCP_DECR: /* Decrementer exception */
1042 case POWERPC_EXCP_RESET: /* System reset exception */
1043 if (FIELD_EX64(env->msr, MSR, POW)) {
1044 cpu_abort(cs, "Trying to deliver power-saving system reset "
1045 "exception %d with no HV support\n", excp);
1048 case POWERPC_EXCP_TRACE: /* Trace exception */
1050 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
1052 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
1053 case POWERPC_EXCP_SMI: /* System management interrupt */
1054 case POWERPC_EXCP_THERM: /* Thermal interrupt */
1055 case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */
1056 case POWERPC_EXCP_VPUA: /* Vector assist exception */
1057 cpu_abort(cs, "%s exception not implemented\n",
1058 powerpc_excp_name(excp));
1061 cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
1066 * Sort out endianness of interrupt, this differs depending on the
1067 * CPU, the HV mode, etc...
1069 if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
1070 new_msr |= (target_ulong)1 << MSR_LE;
1074 env->spr[SPR_SRR0] = env->nip;
1077 env->spr[SPR_SRR1] = msr;
1079 powerpc_set_excp_state(cpu, vector, new_msr);
1082 static void powerpc_excp_booke(PowerPCCPU *cpu, int excp)
1084 CPUState *cs = CPU(cpu);
1085 CPUPPCState *env = &cpu->env;
1086 target_ulong msr, new_msr, vector;
1092 * new interrupt handler msr preserves existing ME unless
1093 * explicitly overriden
1095 new_msr = env->msr & ((target_ulong)1 << MSR_ME);
1097 /* target registers */
1102 * Hypervisor emulation assistance interrupt only exists on server
1103 * arch 2.05 server or later.
1105 if (excp == POWERPC_EXCP_HV_EMU) {
1106 excp = POWERPC_EXCP_PROGRAM;
1111 * SPEU and VPU share the same IVOR but they exist in different
1112 * processors. SPEU is e500v1/2 only and VPU is e6500 only.
1114 if (excp == POWERPC_EXCP_VPU) {
1115 excp = POWERPC_EXCP_SPEU;
1119 vector = env->excp_vectors[excp];
1120 if (vector == (target_ulong)-1ULL) {
1121 cpu_abort(cs, "Raised an exception without defined vector %d\n",
1125 vector |= env->excp_prefix;
1128 case POWERPC_EXCP_CRITICAL: /* Critical input */
1129 srr0 = SPR_BOOKE_CSRR0;
1130 srr1 = SPR_BOOKE_CSRR1;
1132 case POWERPC_EXCP_MCHECK: /* Machine check exception */
1133 if (!FIELD_EX64(env->msr, MSR, ME)) {
1135 * Machine check exception is not enabled. Enter
1138 fprintf(stderr, "Machine check while not allowed. "
1139 "Entering checkstop state\n");
1140 if (qemu_log_separate()) {
1141 qemu_log("Machine check while not allowed. "
1142 "Entering checkstop state\n");
1145 cpu_interrupt_exittb(cs);
1148 /* machine check exceptions don't have ME set */
1149 new_msr &= ~((target_ulong)1 << MSR_ME);
1151 /* FIXME: choose one or the other based on CPU type */
1152 srr0 = SPR_BOOKE_MCSRR0;
1153 srr1 = SPR_BOOKE_MCSRR1;
1155 env->spr[SPR_BOOKE_CSRR0] = env->nip;
1156 env->spr[SPR_BOOKE_CSRR1] = msr;
1159 case POWERPC_EXCP_DSI: /* Data storage exception */
1160 trace_ppc_excp_dsi(env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
1162 case POWERPC_EXCP_ISI: /* Instruction storage exception */
1163 trace_ppc_excp_isi(msr, env->nip);
1165 case POWERPC_EXCP_EXTERNAL: /* External input */
1166 if (env->mpic_proxy) {
1167 /* IACK the IRQ on delivery */
1168 env->spr[SPR_BOOKE_EPR] = ldl_phys(cs->as, env->mpic_iack);
1171 case POWERPC_EXCP_ALIGN: /* Alignment exception */
1173 case POWERPC_EXCP_PROGRAM: /* Program exception */
1174 switch (env->error_code & ~0xF) {
1175 case POWERPC_EXCP_FP:
1176 if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
1177 trace_ppc_excp_fp_ignore();
1178 powerpc_reset_excp_state(cpu);
1183 * FP exceptions always have NIP pointing to the faulting
1184 * instruction, so always use store_next and claim we are
1185 * precise in the MSR.
1188 env->spr[SPR_BOOKE_ESR] = ESR_FP;
1190 case POWERPC_EXCP_INVAL:
1191 trace_ppc_excp_inval(env->nip);
1193 env->spr[SPR_BOOKE_ESR] = ESR_PIL;
1195 case POWERPC_EXCP_PRIV:
1197 env->spr[SPR_BOOKE_ESR] = ESR_PPR;
1199 case POWERPC_EXCP_TRAP:
1201 env->spr[SPR_BOOKE_ESR] = ESR_PTR;
1204 /* Should never occur */
1205 cpu_abort(cs, "Invalid program exception %d. Aborting\n",
1210 case POWERPC_EXCP_SYSCALL: /* System call exception */
1214 * We need to correct the NIP which in this case is supposed
1215 * to point to the next instruction
1219 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
1220 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
1221 case POWERPC_EXCP_DECR: /* Decrementer exception */
1223 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
1225 trace_ppc_excp_print("FIT");
1227 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
1228 trace_ppc_excp_print("WDT");
1229 srr0 = SPR_BOOKE_CSRR0;
1230 srr1 = SPR_BOOKE_CSRR1;
1232 case POWERPC_EXCP_DTLB: /* Data TLB error */
1233 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
1235 case POWERPC_EXCP_DEBUG: /* Debug interrupt */
1236 if (env->flags & POWERPC_FLAG_DE) {
1237 /* FIXME: choose one or the other based on CPU type */
1238 srr0 = SPR_BOOKE_DSRR0;
1239 srr1 = SPR_BOOKE_DSRR1;
1241 env->spr[SPR_BOOKE_CSRR0] = env->nip;
1242 env->spr[SPR_BOOKE_CSRR1] = msr;
1244 /* DBSR already modified by caller */
1246 cpu_abort(cs, "Debug exception triggered on unsupported model\n");
1249 case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavailable/VPU */
1250 env->spr[SPR_BOOKE_ESR] = ESR_SPV;
1252 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
1254 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
1255 srr0 = SPR_BOOKE_CSRR0;
1256 srr1 = SPR_BOOKE_CSRR1;
1258 case POWERPC_EXCP_RESET: /* System reset exception */
1259 if (FIELD_EX64(env->msr, MSR, POW)) {
1260 cpu_abort(cs, "Trying to deliver power-saving system reset "
1261 "exception %d with no HV support\n", excp);
1264 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data interrupt */
1265 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round interrupt */
1266 cpu_abort(cs, "%s exception not implemented\n",
1267 powerpc_excp_name(excp));
1270 cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
1274 #if defined(TARGET_PPC64)
1275 if (env->spr[SPR_BOOKE_EPCR] & EPCR_ICM) {
1276 /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */
1277 new_msr |= (target_ulong)1 << MSR_CM;
1279 vector = (uint32_t)vector;
1284 env->spr[srr0] = env->nip;
1287 env->spr[srr1] = msr;
1289 powerpc_set_excp_state(cpu, vector, new_msr);
1293 * When running a nested HV guest under vhyp, external interrupts are
1294 * delivered as HVIRT.
1296 static bool books_vhyp_promotes_external_to_hvirt(PowerPCCPU *cpu)
1299 return vhyp_cpu_in_nested(cpu);
1306 * When running under vhyp, hcalls are always intercepted and sent to the
1307 * vhc->hypercall handler.
1309 static bool books_vhyp_handles_hcall(PowerPCCPU *cpu)
1312 return !vhyp_cpu_in_nested(cpu);
1318 * When running a nested KVM HV guest under vhyp, HV exceptions are not
1319 * delivered to the guest (because there is no concept of HV support), but
1320 * rather they are sent tothe vhyp to exit from the L2 back to the L1 and
1321 * return from the H_ENTER_NESTED hypercall.
1323 static bool books_vhyp_handles_hv_excp(PowerPCCPU *cpu)
1326 return vhyp_cpu_in_nested(cpu);
1331 static void powerpc_excp_books(PowerPCCPU *cpu, int excp)
1333 CPUState *cs = CPU(cpu);
1334 CPUPPCState *env = &cpu->env;
1335 target_ulong msr, new_msr, vector;
1336 int srr0, srr1, lev = -1;
1338 /* new srr1 value excluding must-be-zero bits */
1339 msr = env->msr & ~0x783f0000ULL;
1342 * new interrupt handler msr preserves existing HV and ME unless
1343 * explicitly overriden
1345 new_msr = env->msr & (((target_ulong)1 << MSR_ME) | MSR_HVB);
1347 /* target registers */
1352 * check for special resume at 0x100 from doze/nap/sleep/winkle on
1355 if (env->resume_as_sreset) {
1356 excp = powerpc_reset_wakeup(cs, env, excp, &msr);
1360 * We don't want to generate a Hypervisor Emulation Assistance
1361 * Interrupt if we don't have HVB in msr_mask (PAPR mode),
1362 * unless running a nested-hv guest, in which case the L1
1363 * kernel wants the interrupt.
1365 if (excp == POWERPC_EXCP_HV_EMU && !(env->msr_mask & MSR_HVB) &&
1366 !books_vhyp_handles_hv_excp(cpu)) {
1367 excp = POWERPC_EXCP_PROGRAM;
1370 vector = env->excp_vectors[excp];
1371 if (vector == (target_ulong)-1ULL) {
1372 cpu_abort(cs, "Raised an exception without defined vector %d\n",
1376 vector |= env->excp_prefix;
1379 case POWERPC_EXCP_MCHECK: /* Machine check exception */
1380 if (!FIELD_EX64(env->msr, MSR, ME)) {
1382 * Machine check exception is not enabled. Enter
1385 fprintf(stderr, "Machine check while not allowed. "
1386 "Entering checkstop state\n");
1387 if (qemu_log_separate()) {
1388 qemu_log("Machine check while not allowed. "
1389 "Entering checkstop state\n");
1392 cpu_interrupt_exittb(cs);
1394 if (env->msr_mask & MSR_HVB) {
1396 * ISA specifies HV, but can be delivered to guest with HV
1397 * clear (e.g., see FWNMI in PAPR).
1399 new_msr |= (target_ulong)MSR_HVB;
1402 /* machine check exceptions don't have ME set */
1403 new_msr &= ~((target_ulong)1 << MSR_ME);
1406 case POWERPC_EXCP_DSI: /* Data storage exception */
1407 trace_ppc_excp_dsi(env->spr[SPR_DSISR], env->spr[SPR_DAR]);
1409 case POWERPC_EXCP_ISI: /* Instruction storage exception */
1410 trace_ppc_excp_isi(msr, env->nip);
1411 msr |= env->error_code;
1413 case POWERPC_EXCP_EXTERNAL: /* External input */
1418 * LPES0 is only taken into consideration if we support HV
1419 * mode for this CPU.
1421 if (!env->has_hv_mode) {
1425 lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1428 new_msr |= (target_ulong)MSR_HVB;
1429 new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
1436 case POWERPC_EXCP_ALIGN: /* Alignment exception */
1437 /* Optional DSISR update was removed from ISA v3.0 */
1438 if (!(env->insns_flags2 & PPC2_ISA300)) {
1439 /* Get rS/rD and rA from faulting opcode */
1441 * Note: the opcode fields will not be set properly for a
1442 * direct store load/store, but nobody cares as nobody
1443 * actually uses direct store segments.
1445 env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
1448 case POWERPC_EXCP_PROGRAM: /* Program exception */
1449 switch (env->error_code & ~0xF) {
1450 case POWERPC_EXCP_FP:
1451 if (!FIELD_EX64_FE(env->msr) || !FIELD_EX64(env->msr, MSR, FP)) {
1452 trace_ppc_excp_fp_ignore();
1453 powerpc_reset_excp_state(cpu);
1458 * FP exceptions always have NIP pointing to the faulting
1459 * instruction, so always use store_next and claim we are
1460 * precise in the MSR.
1464 case POWERPC_EXCP_INVAL:
1465 trace_ppc_excp_inval(env->nip);
1468 case POWERPC_EXCP_PRIV:
1471 case POWERPC_EXCP_TRAP:
1475 /* Should never occur */
1476 cpu_abort(cs, "Invalid program exception %d. Aborting\n",
1481 case POWERPC_EXCP_SYSCALL: /* System call exception */
1482 lev = env->error_code;
1484 if ((lev == 1) && cpu->vhyp) {
1491 * We need to correct the NIP which in this case is supposed
1492 * to point to the next instruction
1496 /* "PAPR mode" built-in hypercall emulation */
1497 if ((lev == 1) && books_vhyp_handles_hcall(cpu)) {
1498 PPCVirtualHypervisorClass *vhc =
1499 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
1500 vhc->hypercall(cpu->vhyp, cpu);
1504 new_msr |= (target_ulong)MSR_HVB;
1507 case POWERPC_EXCP_SYSCALL_VECTORED: /* scv exception */
1508 lev = env->error_code;
1511 new_msr |= env->msr & ((target_ulong)1 << MSR_EE);
1512 new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
1514 vector += lev * 0x20;
1519 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
1520 case POWERPC_EXCP_DECR: /* Decrementer exception */
1522 case POWERPC_EXCP_RESET: /* System reset exception */
1523 /* A power-saving exception sets ME, otherwise it is unchanged */
1524 if (FIELD_EX64(env->msr, MSR, POW)) {
1525 /* indicate that we resumed from power save mode */
1527 new_msr |= ((target_ulong)1 << MSR_ME);
1529 if (env->msr_mask & MSR_HVB) {
1531 * ISA specifies HV, but can be delivered to guest with HV
1532 * clear (e.g., see FWNMI in PAPR, NMI injection in QEMU).
1534 new_msr |= (target_ulong)MSR_HVB;
1536 if (FIELD_EX64(env->msr, MSR, POW)) {
1537 cpu_abort(cs, "Trying to deliver power-saving system reset "
1538 "exception %d with no HV support\n", excp);
1542 case POWERPC_EXCP_DSEG: /* Data segment exception */
1543 case POWERPC_EXCP_ISEG: /* Instruction segment exception */
1544 case POWERPC_EXCP_TRACE: /* Trace exception */
1545 case POWERPC_EXCP_SDOOR: /* Doorbell interrupt */
1546 case POWERPC_EXCP_PERFM: /* Performance monitor interrupt */
1548 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage exception */
1549 msr |= env->error_code;
1551 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
1552 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
1553 case POWERPC_EXCP_SDOOR_HV: /* Hypervisor Doorbell interrupt */
1554 case POWERPC_EXCP_HV_EMU:
1555 case POWERPC_EXCP_HVIRT: /* Hypervisor virtualization */
1558 new_msr |= (target_ulong)MSR_HVB;
1559 new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
1561 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
1562 case POWERPC_EXCP_VSXU: /* VSX unavailable exception */
1563 case POWERPC_EXCP_FU: /* Facility unavailable exception */
1564 env->spr[SPR_FSCR] |= ((target_ulong)env->error_code << 56);
1566 case POWERPC_EXCP_HV_FU: /* Hypervisor Facility Unavailable Exception */
1567 env->spr[SPR_HFSCR] |= ((target_ulong)env->error_code << FSCR_IC_POS);
1570 new_msr |= (target_ulong)MSR_HVB;
1571 new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
1573 case POWERPC_EXCP_PERFM_EBB: /* Performance Monitor EBB Exception */
1574 case POWERPC_EXCP_EXTERNAL_EBB: /* External EBB Exception */
1575 env->spr[SPR_BESCR] &= ~BESCR_GE;
1578 * Save NIP for rfebb insn in SPR_EBBRR. Next nip is
1579 * stored in the EBB Handler SPR_EBBHR.
1581 env->spr[SPR_EBBRR] = env->nip;
1582 powerpc_set_excp_state(cpu, env->spr[SPR_EBBHR], env->msr);
1585 * This exception is handled in userspace. No need to proceed.
1588 case POWERPC_EXCP_THERM: /* Thermal interrupt */
1589 case POWERPC_EXCP_VPUA: /* Vector assist exception */
1590 case POWERPC_EXCP_MAINT: /* Maintenance exception */
1591 case POWERPC_EXCP_HV_MAINT: /* Hypervisor Maintenance exception */
1592 cpu_abort(cs, "%s exception not implemented\n",
1593 powerpc_excp_name(excp));
1596 cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
1601 * Sort out endianness of interrupt, this differs depending on the
1602 * CPU, the HV mode, etc...
1604 if (ppc_interrupts_little_endian(cpu, !!(new_msr & MSR_HVB))) {
1605 new_msr |= (target_ulong)1 << MSR_LE;
1608 new_msr |= (target_ulong)1 << MSR_SF;
1610 if (excp != POWERPC_EXCP_SYSCALL_VECTORED) {
1612 env->spr[srr0] = env->nip;
1615 env->spr[srr1] = msr;
1618 if ((new_msr & MSR_HVB) && books_vhyp_handles_hv_excp(cpu)) {
1619 PPCVirtualHypervisorClass *vhc =
1620 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
1621 /* Deliver interrupt to L1 by returning from the H_ENTER_NESTED call */
1622 vhc->deliver_hv_excp(cpu, excp);
1624 powerpc_reset_excp_state(cpu);
1628 if (!(env->msr_mask & MSR_HVB) && srr0 == SPR_HSRR0) {
1629 cpu_abort(cs, "Trying to deliver HV exception (HSRR) %d with "
1630 "no HV support\n", excp);
1633 /* This can update new_msr and vector if AIL applies */
1634 ppc_excp_apply_ail(cpu, excp, msr, &new_msr, &vector);
1636 powerpc_set_excp_state(cpu, vector, new_msr);
1640 static inline void powerpc_excp_books(PowerPCCPU *cpu, int excp)
1642 g_assert_not_reached();
1646 static void powerpc_excp(PowerPCCPU *cpu, int excp)
1648 CPUState *cs = CPU(cpu);
1649 CPUPPCState *env = &cpu->env;
1651 if (excp <= POWERPC_EXCP_NONE || excp >= POWERPC_EXCP_NB) {
1652 cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
1655 qemu_log_mask(CPU_LOG_INT, "Raise exception at " TARGET_FMT_lx
1656 " => %s (%d) error=%02x\n", env->nip, powerpc_excp_name(excp),
1657 excp, env->error_code);
1659 switch (env->excp_model) {
1660 case POWERPC_EXCP_40x:
1661 powerpc_excp_40x(cpu, excp);
1663 case POWERPC_EXCP_6xx:
1664 powerpc_excp_6xx(cpu, excp);
1666 case POWERPC_EXCP_7xx:
1667 powerpc_excp_7xx(cpu, excp);
1669 case POWERPC_EXCP_74xx:
1670 powerpc_excp_74xx(cpu, excp);
1672 case POWERPC_EXCP_BOOKE:
1673 powerpc_excp_booke(cpu, excp);
1675 case POWERPC_EXCP_970:
1676 case POWERPC_EXCP_POWER7:
1677 case POWERPC_EXCP_POWER8:
1678 case POWERPC_EXCP_POWER9:
1679 case POWERPC_EXCP_POWER10:
1680 powerpc_excp_books(cpu, excp);
1683 g_assert_not_reached();
1687 void ppc_cpu_do_interrupt(CPUState *cs)
1689 PowerPCCPU *cpu = POWERPC_CPU(cs);
1691 powerpc_excp(cpu, cs->exception_index);
1694 #if defined(TARGET_PPC64)
1695 #define P7_UNUSED_INTERRUPTS \
1696 (PPC_INTERRUPT_RESET | PPC_INTERRUPT_HVIRT | PPC_INTERRUPT_CEXT | \
1697 PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT | \
1698 PPC_INTERRUPT_PIT | PPC_INTERRUPT_DOORBELL | PPC_INTERRUPT_HDOORBELL | \
1699 PPC_INTERRUPT_THERM | PPC_INTERRUPT_EBB)
1701 static int p7_interrupt_powersave(CPUPPCState *env)
1703 if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
1704 (env->spr[SPR_LPCR] & LPCR_P7_PECE0)) {
1705 return PPC_INTERRUPT_EXT;
1707 if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
1708 (env->spr[SPR_LPCR] & LPCR_P7_PECE1)) {
1709 return PPC_INTERRUPT_DECR;
1711 if ((env->pending_interrupts & PPC_INTERRUPT_MCK) &&
1712 (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
1713 return PPC_INTERRUPT_MCK;
1715 if ((env->pending_interrupts & PPC_INTERRUPT_HMI) &&
1716 (env->spr[SPR_LPCR] & LPCR_P7_PECE2)) {
1717 return PPC_INTERRUPT_HMI;
1719 if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
1720 return PPC_INTERRUPT_RESET;
1725 static int p7_next_unmasked_interrupt(CPUPPCState *env)
1727 PowerPCCPU *cpu = env_archcpu(env);
1728 CPUState *cs = CPU(cpu);
1729 /* Ignore MSR[EE] when coming out of some power management states */
1730 bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1732 assert((env->pending_interrupts & P7_UNUSED_INTERRUPTS) == 0);
1735 /* LPCR[PECE] controls which interrupts can exit power-saving mode */
1736 return p7_interrupt_powersave(env);
1739 /* Machine check exception */
1740 if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1741 return PPC_INTERRUPT_MCK;
1744 /* Hypervisor decrementer exception */
1745 if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1746 /* LPCR will be clear when not supported so this will work */
1747 bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
1748 if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1749 /* HDEC clears on delivery */
1750 return PPC_INTERRUPT_HDECR;
1754 /* External interrupt can ignore MSR:EE under some circumstances */
1755 if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1756 bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1757 bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1758 /* HEIC blocks delivery to the hypervisor */
1759 if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1760 !FIELD_EX64(env->msr, MSR, PR))) ||
1761 (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1762 return PPC_INTERRUPT_EXT;
1766 /* Decrementer exception */
1767 if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1768 return PPC_INTERRUPT_DECR;
1770 if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1771 return PPC_INTERRUPT_PERFM;
1778 #define P8_UNUSED_INTERRUPTS \
1779 (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_HVIRT | \
1780 PPC_INTERRUPT_CEXT | PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | \
1781 PPC_INTERRUPT_FIT | PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1783 static int p8_interrupt_powersave(CPUPPCState *env)
1785 if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
1786 (env->spr[SPR_LPCR] & LPCR_P8_PECE2)) {
1787 return PPC_INTERRUPT_EXT;
1789 if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
1790 (env->spr[SPR_LPCR] & LPCR_P8_PECE3)) {
1791 return PPC_INTERRUPT_DECR;
1793 if ((env->pending_interrupts & PPC_INTERRUPT_MCK) &&
1794 (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) {
1795 return PPC_INTERRUPT_MCK;
1797 if ((env->pending_interrupts & PPC_INTERRUPT_HMI) &&
1798 (env->spr[SPR_LPCR] & LPCR_P8_PECE4)) {
1799 return PPC_INTERRUPT_HMI;
1801 if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
1802 (env->spr[SPR_LPCR] & LPCR_P8_PECE0)) {
1803 return PPC_INTERRUPT_DOORBELL;
1805 if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
1806 (env->spr[SPR_LPCR] & LPCR_P8_PECE1)) {
1807 return PPC_INTERRUPT_HDOORBELL;
1809 if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
1810 return PPC_INTERRUPT_RESET;
1815 static int p8_next_unmasked_interrupt(CPUPPCState *env)
1817 PowerPCCPU *cpu = env_archcpu(env);
1818 CPUState *cs = CPU(cpu);
1819 /* Ignore MSR[EE] when coming out of some power management states */
1820 bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1822 assert((env->pending_interrupts & P8_UNUSED_INTERRUPTS) == 0);
1825 /* LPCR[PECE] controls which interrupts can exit power-saving mode */
1826 return p8_interrupt_powersave(env);
1829 /* Machine check exception */
1830 if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1831 return PPC_INTERRUPT_MCK;
1834 /* Hypervisor decrementer exception */
1835 if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1836 /* LPCR will be clear when not supported so this will work */
1837 bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
1838 if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1839 /* HDEC clears on delivery */
1840 return PPC_INTERRUPT_HDECR;
1844 /* External interrupt can ignore MSR:EE under some circumstances */
1845 if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1846 bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1847 bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1848 /* HEIC blocks delivery to the hypervisor */
1849 if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1850 !FIELD_EX64(env->msr, MSR, PR))) ||
1851 (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1852 return PPC_INTERRUPT_EXT;
1856 /* Decrementer exception */
1857 if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1858 return PPC_INTERRUPT_DECR;
1860 if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
1861 return PPC_INTERRUPT_DOORBELL;
1863 if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
1864 return PPC_INTERRUPT_HDOORBELL;
1866 if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
1867 return PPC_INTERRUPT_PERFM;
1870 if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
1872 * EBB exception must be taken in problem state and
1873 * with BESCR_GE set.
1875 if (FIELD_EX64(env->msr, MSR, PR) &&
1876 (env->spr[SPR_BESCR] & BESCR_GE)) {
1877 return PPC_INTERRUPT_EBB;
1885 #define P9_UNUSED_INTERRUPTS \
1886 (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT | \
1887 PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT | \
1888 PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1890 static int p9_interrupt_powersave(CPUPPCState *env)
1892 /* External Exception */
1893 if ((env->pending_interrupts & PPC_INTERRUPT_EXT) &&
1894 (env->spr[SPR_LPCR] & LPCR_EEE)) {
1895 bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1896 if (!heic || !FIELD_EX64_HV(env->msr) ||
1897 FIELD_EX64(env->msr, MSR, PR)) {
1898 return PPC_INTERRUPT_EXT;
1901 /* Decrementer Exception */
1902 if ((env->pending_interrupts & PPC_INTERRUPT_DECR) &&
1903 (env->spr[SPR_LPCR] & LPCR_DEE)) {
1904 return PPC_INTERRUPT_DECR;
1906 /* Machine Check or Hypervisor Maintenance Exception */
1907 if (env->spr[SPR_LPCR] & LPCR_OEE) {
1908 if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1909 return PPC_INTERRUPT_MCK;
1911 if (env->pending_interrupts & PPC_INTERRUPT_HMI) {
1912 return PPC_INTERRUPT_HMI;
1915 /* Privileged Doorbell Exception */
1916 if ((env->pending_interrupts & PPC_INTERRUPT_DOORBELL) &&
1917 (env->spr[SPR_LPCR] & LPCR_PDEE)) {
1918 return PPC_INTERRUPT_DOORBELL;
1920 /* Hypervisor Doorbell Exception */
1921 if ((env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) &&
1922 (env->spr[SPR_LPCR] & LPCR_HDEE)) {
1923 return PPC_INTERRUPT_HDOORBELL;
1925 /* Hypervisor virtualization exception */
1926 if ((env->pending_interrupts & PPC_INTERRUPT_HVIRT) &&
1927 (env->spr[SPR_LPCR] & LPCR_HVEE)) {
1928 return PPC_INTERRUPT_HVIRT;
1930 if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
1931 return PPC_INTERRUPT_RESET;
1936 static int p9_next_unmasked_interrupt(CPUPPCState *env)
1938 PowerPCCPU *cpu = env_archcpu(env);
1939 CPUState *cs = CPU(cpu);
1940 /* Ignore MSR[EE] when coming out of some power management states */
1941 bool msr_ee = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
1943 assert((env->pending_interrupts & P9_UNUSED_INTERRUPTS) == 0);
1946 if (env->spr[SPR_PSSCR] & PSSCR_EC) {
1948 * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can
1949 * wakeup the processor
1951 return p9_interrupt_powersave(env);
1954 * When it's clear, any system-caused exception exits power-saving
1955 * mode, even the ones that gate on MSR[EE].
1961 /* Machine check exception */
1962 if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
1963 return PPC_INTERRUPT_MCK;
1966 /* Hypervisor decrementer exception */
1967 if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
1968 /* LPCR will be clear when not supported so this will work */
1969 bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
1970 if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hdice) {
1971 /* HDEC clears on delivery */
1972 return PPC_INTERRUPT_HDECR;
1976 /* Hypervisor virtualization interrupt */
1977 if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
1978 /* LPCR will be clear when not supported so this will work */
1979 bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
1980 if ((msr_ee || !FIELD_EX64_HV(env->msr)) && hvice) {
1981 return PPC_INTERRUPT_HVIRT;
1985 /* External interrupt can ignore MSR:EE under some circumstances */
1986 if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
1987 bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
1988 bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
1989 /* HEIC blocks delivery to the hypervisor */
1990 if ((msr_ee && !(heic && FIELD_EX64_HV(env->msr) &&
1991 !FIELD_EX64(env->msr, MSR, PR))) ||
1992 (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
1993 return PPC_INTERRUPT_EXT;
1997 /* Decrementer exception */
1998 if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
1999 return PPC_INTERRUPT_DECR;
2001 if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2002 return PPC_INTERRUPT_DOORBELL;
2004 if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
2005 return PPC_INTERRUPT_HDOORBELL;
2007 if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
2008 return PPC_INTERRUPT_PERFM;
2011 if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
2013 * EBB exception must be taken in problem state and
2014 * with BESCR_GE set.
2016 if (FIELD_EX64(env->msr, MSR, PR) &&
2017 (env->spr[SPR_BESCR] & BESCR_GE)) {
2018 return PPC_INTERRUPT_EBB;
2027 static int ppc_next_unmasked_interrupt_generic(CPUPPCState *env)
2031 /* External reset */
2032 if (env->pending_interrupts & PPC_INTERRUPT_RESET) {
2033 return PPC_INTERRUPT_RESET;
2035 /* Machine check exception */
2036 if (env->pending_interrupts & PPC_INTERRUPT_MCK) {
2037 return PPC_INTERRUPT_MCK;
2040 /* External debug exception */
2041 if (env->pending_interrupts & PPC_INTERRUPT_DEBUG) {
2042 return PPC_INTERRUPT_DEBUG;
2047 * For interrupts that gate on MSR:EE, we need to do something a
2048 * bit more subtle, as we need to let them through even when EE is
2049 * clear when coming out of some power management states (in order
2050 * for them to become a 0x100).
2052 async_deliver = FIELD_EX64(env->msr, MSR, EE) || env->resume_as_sreset;
2054 /* Hypervisor decrementer exception */
2055 if (env->pending_interrupts & PPC_INTERRUPT_HDECR) {
2056 /* LPCR will be clear when not supported so this will work */
2057 bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
2058 if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hdice) {
2059 /* HDEC clears on delivery */
2060 return PPC_INTERRUPT_HDECR;
2064 /* Hypervisor virtualization interrupt */
2065 if (env->pending_interrupts & PPC_INTERRUPT_HVIRT) {
2066 /* LPCR will be clear when not supported so this will work */
2067 bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
2068 if ((async_deliver || !FIELD_EX64_HV(env->msr)) && hvice) {
2069 return PPC_INTERRUPT_HVIRT;
2073 /* External interrupt can ignore MSR:EE under some circumstances */
2074 if (env->pending_interrupts & PPC_INTERRUPT_EXT) {
2075 bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
2076 bool heic = !!(env->spr[SPR_LPCR] & LPCR_HEIC);
2077 /* HEIC blocks delivery to the hypervisor */
2078 if ((async_deliver && !(heic && FIELD_EX64_HV(env->msr) &&
2079 !FIELD_EX64(env->msr, MSR, PR))) ||
2080 (env->has_hv_mode && !FIELD_EX64_HV(env->msr) && !lpes0)) {
2081 return PPC_INTERRUPT_EXT;
2084 if (FIELD_EX64(env->msr, MSR, CE)) {
2085 /* External critical interrupt */
2086 if (env->pending_interrupts & PPC_INTERRUPT_CEXT) {
2087 return PPC_INTERRUPT_CEXT;
2090 if (async_deliver != 0) {
2091 /* Watchdog timer on embedded PowerPC */
2092 if (env->pending_interrupts & PPC_INTERRUPT_WDT) {
2093 return PPC_INTERRUPT_WDT;
2095 if (env->pending_interrupts & PPC_INTERRUPT_CDOORBELL) {
2096 return PPC_INTERRUPT_CDOORBELL;
2098 /* Fixed interval timer on embedded PowerPC */
2099 if (env->pending_interrupts & PPC_INTERRUPT_FIT) {
2100 return PPC_INTERRUPT_FIT;
2102 /* Programmable interval timer on embedded PowerPC */
2103 if (env->pending_interrupts & PPC_INTERRUPT_PIT) {
2104 return PPC_INTERRUPT_PIT;
2106 /* Decrementer exception */
2107 if (env->pending_interrupts & PPC_INTERRUPT_DECR) {
2108 return PPC_INTERRUPT_DECR;
2110 if (env->pending_interrupts & PPC_INTERRUPT_DOORBELL) {
2111 return PPC_INTERRUPT_DOORBELL;
2113 if (env->pending_interrupts & PPC_INTERRUPT_HDOORBELL) {
2114 return PPC_INTERRUPT_HDOORBELL;
2116 if (env->pending_interrupts & PPC_INTERRUPT_PERFM) {
2117 return PPC_INTERRUPT_PERFM;
2119 /* Thermal interrupt */
2120 if (env->pending_interrupts & PPC_INTERRUPT_THERM) {
2121 return PPC_INTERRUPT_THERM;
2124 if (env->pending_interrupts & PPC_INTERRUPT_EBB) {
2126 * EBB exception must be taken in problem state and
2127 * with BESCR_GE set.
2129 if (FIELD_EX64(env->msr, MSR, PR) &&
2130 (env->spr[SPR_BESCR] & BESCR_GE)) {
2131 return PPC_INTERRUPT_EBB;
2139 static int ppc_next_unmasked_interrupt(CPUPPCState *env)
2141 switch (env->excp_model) {
2142 #if defined(TARGET_PPC64)
2143 case POWERPC_EXCP_POWER7:
2144 return p7_next_unmasked_interrupt(env);
2145 case POWERPC_EXCP_POWER8:
2146 return p8_next_unmasked_interrupt(env);
2147 case POWERPC_EXCP_POWER9:
2148 case POWERPC_EXCP_POWER10:
2149 return p9_next_unmasked_interrupt(env);
2152 return ppc_next_unmasked_interrupt_generic(env);
2157 * Sets CPU_INTERRUPT_HARD if there is at least one unmasked interrupt to be
2158 * delivered and clears CPU_INTERRUPT_HARD otherwise.
2160 * This method is called by ppc_set_interrupt when an interrupt is raised or
2161 * lowered, and should also be called whenever an interrupt masking condition
2163 * - When relevant bits of MSR are altered, like EE, HV, PR, etc.;
2164 * - When relevant bits of LPCR are altered, like PECE, HDICE, HVICE, etc.;
2165 * - When PSSCR[EC] or env->resume_as_sreset are changed;
2166 * - When cs->halted is changed and the CPU has a different interrupt masking
2167 * logic in power-saving mode (e.g., POWER7/8/9/10);
2169 void ppc_maybe_interrupt(CPUPPCState *env)
2171 CPUState *cs = env_cpu(env);
2172 QEMU_IOTHREAD_LOCK_GUARD();
2174 if (ppc_next_unmasked_interrupt(env)) {
2175 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
2177 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
2181 #if defined(TARGET_PPC64)
2182 static void p7_deliver_interrupt(CPUPPCState *env, int interrupt)
2184 PowerPCCPU *cpu = env_archcpu(env);
2185 CPUState *cs = env_cpu(env);
2187 switch (interrupt) {
2188 case PPC_INTERRUPT_MCK: /* Machine check exception */
2189 env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2190 powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2193 case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2194 /* HDEC clears on delivery */
2195 env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2196 powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2199 case PPC_INTERRUPT_EXT:
2200 if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2201 powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2203 powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2207 case PPC_INTERRUPT_DECR: /* Decrementer exception */
2208 powerpc_excp(cpu, POWERPC_EXCP_DECR);
2210 case PPC_INTERRUPT_PERFM:
2211 env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2212 powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2216 * This is a bug ! It means that has_work took us out of halt without
2217 * anything to deliver while in a PM state that requires getting
2220 * This means we will incorrectly execute past the power management
2221 * instruction instead of triggering a reset.
2223 * It generally means a discrepancy between the wakeup conditions in the
2224 * processor has_work implementation and the logic in this function.
2226 assert(!env->resume_as_sreset);
2229 cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
2233 static void p8_deliver_interrupt(CPUPPCState *env, int interrupt)
2235 PowerPCCPU *cpu = env_archcpu(env);
2236 CPUState *cs = env_cpu(env);
2238 switch (interrupt) {
2239 case PPC_INTERRUPT_MCK: /* Machine check exception */
2240 env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2241 powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2244 case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2245 /* HDEC clears on delivery */
2246 env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2247 powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2250 case PPC_INTERRUPT_EXT:
2251 if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2252 powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2254 powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2258 case PPC_INTERRUPT_DECR: /* Decrementer exception */
2259 powerpc_excp(cpu, POWERPC_EXCP_DECR);
2261 case PPC_INTERRUPT_DOORBELL:
2262 env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
2263 if (is_book3s_arch2x(env)) {
2264 powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
2266 powerpc_excp(cpu, POWERPC_EXCP_DOORI);
2269 case PPC_INTERRUPT_HDOORBELL:
2270 env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
2271 powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
2273 case PPC_INTERRUPT_PERFM:
2274 env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2275 powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2277 case PPC_INTERRUPT_EBB: /* EBB exception */
2278 env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
2279 if (env->spr[SPR_BESCR] & BESCR_PMEO) {
2280 powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
2281 } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
2282 powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
2287 * This is a bug ! It means that has_work took us out of halt without
2288 * anything to deliver while in a PM state that requires getting
2291 * This means we will incorrectly execute past the power management
2292 * instruction instead of triggering a reset.
2294 * It generally means a discrepancy between the wakeup conditions in the
2295 * processor has_work implementation and the logic in this function.
2297 assert(!env->resume_as_sreset);
2300 cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
2304 static void p9_deliver_interrupt(CPUPPCState *env, int interrupt)
2306 PowerPCCPU *cpu = env_archcpu(env);
2307 CPUState *cs = env_cpu(env);
2309 if (cs->halted && !(env->spr[SPR_PSSCR] & PSSCR_EC) &&
2310 !FIELD_EX64(env->msr, MSR, EE)) {
2312 * A pending interrupt took us out of power-saving, but MSR[EE] says
2313 * that we should return to NIP+4 instead of delivering it.
2318 switch (interrupt) {
2319 case PPC_INTERRUPT_MCK: /* Machine check exception */
2320 env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2321 powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2324 case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2325 /* HDEC clears on delivery */
2326 env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2327 powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2329 case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
2330 powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2333 case PPC_INTERRUPT_EXT:
2334 if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2335 powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2337 powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2341 case PPC_INTERRUPT_DECR: /* Decrementer exception */
2342 powerpc_excp(cpu, POWERPC_EXCP_DECR);
2344 case PPC_INTERRUPT_DOORBELL:
2345 env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
2346 powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
2348 case PPC_INTERRUPT_HDOORBELL:
2349 env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
2350 powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
2352 case PPC_INTERRUPT_PERFM:
2353 env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2354 powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2356 case PPC_INTERRUPT_EBB: /* EBB exception */
2357 env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
2358 if (env->spr[SPR_BESCR] & BESCR_PMEO) {
2359 powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
2360 } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
2361 powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
2366 * This is a bug ! It means that has_work took us out of halt without
2367 * anything to deliver while in a PM state that requires getting
2370 * This means we will incorrectly execute past the power management
2371 * instruction instead of triggering a reset.
2373 * It generally means a discrepancy between the wakeup conditions in the
2374 * processor has_work implementation and the logic in this function.
2376 assert(!env->resume_as_sreset);
2379 cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
2384 static void ppc_deliver_interrupt_generic(CPUPPCState *env, int interrupt)
2386 PowerPCCPU *cpu = env_archcpu(env);
2387 CPUState *cs = env_cpu(env);
2389 switch (interrupt) {
2390 case PPC_INTERRUPT_RESET: /* External reset */
2391 env->pending_interrupts &= ~PPC_INTERRUPT_RESET;
2392 powerpc_excp(cpu, POWERPC_EXCP_RESET);
2394 case PPC_INTERRUPT_MCK: /* Machine check exception */
2395 env->pending_interrupts &= ~PPC_INTERRUPT_MCK;
2396 powerpc_excp(cpu, POWERPC_EXCP_MCHECK);
2399 case PPC_INTERRUPT_HDECR: /* Hypervisor decrementer exception */
2400 /* HDEC clears on delivery */
2401 env->pending_interrupts &= ~PPC_INTERRUPT_HDECR;
2402 powerpc_excp(cpu, POWERPC_EXCP_HDECR);
2404 case PPC_INTERRUPT_HVIRT: /* Hypervisor virtualization interrupt */
2405 powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2408 case PPC_INTERRUPT_EXT:
2409 if (books_vhyp_promotes_external_to_hvirt(cpu)) {
2410 powerpc_excp(cpu, POWERPC_EXCP_HVIRT);
2412 powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL);
2415 case PPC_INTERRUPT_CEXT: /* External critical interrupt */
2416 powerpc_excp(cpu, POWERPC_EXCP_CRITICAL);
2419 case PPC_INTERRUPT_WDT: /* Watchdog timer on embedded PowerPC */
2420 env->pending_interrupts &= ~PPC_INTERRUPT_WDT;
2421 powerpc_excp(cpu, POWERPC_EXCP_WDT);
2423 case PPC_INTERRUPT_CDOORBELL:
2424 env->pending_interrupts &= ~PPC_INTERRUPT_CDOORBELL;
2425 powerpc_excp(cpu, POWERPC_EXCP_DOORCI);
2427 case PPC_INTERRUPT_FIT: /* Fixed interval timer on embedded PowerPC */
2428 env->pending_interrupts &= ~PPC_INTERRUPT_FIT;
2429 powerpc_excp(cpu, POWERPC_EXCP_FIT);
2431 case PPC_INTERRUPT_PIT: /* Programmable interval timer on embedded ppc */
2432 env->pending_interrupts &= ~PPC_INTERRUPT_PIT;
2433 powerpc_excp(cpu, POWERPC_EXCP_PIT);
2435 case PPC_INTERRUPT_DECR: /* Decrementer exception */
2436 if (ppc_decr_clear_on_delivery(env)) {
2437 env->pending_interrupts &= ~PPC_INTERRUPT_DECR;
2439 powerpc_excp(cpu, POWERPC_EXCP_DECR);
2441 case PPC_INTERRUPT_DOORBELL:
2442 env->pending_interrupts &= ~PPC_INTERRUPT_DOORBELL;
2443 if (is_book3s_arch2x(env)) {
2444 powerpc_excp(cpu, POWERPC_EXCP_SDOOR);
2446 powerpc_excp(cpu, POWERPC_EXCP_DOORI);
2449 case PPC_INTERRUPT_HDOORBELL:
2450 env->pending_interrupts &= ~PPC_INTERRUPT_HDOORBELL;
2451 powerpc_excp(cpu, POWERPC_EXCP_SDOOR_HV);
2453 case PPC_INTERRUPT_PERFM:
2454 env->pending_interrupts &= ~PPC_INTERRUPT_PERFM;
2455 powerpc_excp(cpu, POWERPC_EXCP_PERFM);
2457 case PPC_INTERRUPT_THERM: /* Thermal interrupt */
2458 env->pending_interrupts &= ~PPC_INTERRUPT_THERM;
2459 powerpc_excp(cpu, POWERPC_EXCP_THERM);
2461 case PPC_INTERRUPT_EBB: /* EBB exception */
2462 env->pending_interrupts &= ~PPC_INTERRUPT_EBB;
2463 if (env->spr[SPR_BESCR] & BESCR_PMEO) {
2464 powerpc_excp(cpu, POWERPC_EXCP_PERFM_EBB);
2465 } else if (env->spr[SPR_BESCR] & BESCR_EEO) {
2466 powerpc_excp(cpu, POWERPC_EXCP_EXTERNAL_EBB);
2471 * This is a bug ! It means that has_work took us out of halt without
2472 * anything to deliver while in a PM state that requires getting
2475 * This means we will incorrectly execute past the power management
2476 * instruction instead of triggering a reset.
2478 * It generally means a discrepancy between the wakeup conditions in the
2479 * processor has_work implementation and the logic in this function.
2481 assert(!env->resume_as_sreset);
2484 cpu_abort(cs, "Invalid PowerPC interrupt %d. Aborting\n", interrupt);
2488 static void ppc_deliver_interrupt(CPUPPCState *env, int interrupt)
2490 switch (env->excp_model) {
2491 #if defined(TARGET_PPC64)
2492 case POWERPC_EXCP_POWER7:
2493 p7_deliver_interrupt(env, interrupt);
2495 case POWERPC_EXCP_POWER8:
2496 p8_deliver_interrupt(env, interrupt);
2498 case POWERPC_EXCP_POWER9:
2499 case POWERPC_EXCP_POWER10:
2500 p9_deliver_interrupt(env, interrupt);
2504 ppc_deliver_interrupt_generic(env, interrupt);
2508 void ppc_cpu_do_system_reset(CPUState *cs)
2510 PowerPCCPU *cpu = POWERPC_CPU(cs);
2512 powerpc_excp(cpu, POWERPC_EXCP_RESET);
2515 void ppc_cpu_do_fwnmi_machine_check(CPUState *cs, target_ulong vector)
2517 PowerPCCPU *cpu = POWERPC_CPU(cs);
2518 CPUPPCState *env = &cpu->env;
2519 target_ulong msr = 0;
2522 * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already
2525 msr = (1ULL << MSR_ME);
2526 msr |= env->msr & (1ULL << MSR_SF);
2527 if (ppc_interrupts_little_endian(cpu, false)) {
2528 msr |= (1ULL << MSR_LE);
2531 /* Anything for nested required here? MSR[HV] bit? */
2533 powerpc_set_excp_state(cpu, vector, msr);
2536 bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
2538 PowerPCCPU *cpu = POWERPC_CPU(cs);
2539 CPUPPCState *env = &cpu->env;
2542 if ((interrupt_request & CPU_INTERRUPT_HARD) == 0) {
2546 interrupt = ppc_next_unmasked_interrupt(env);
2547 if (interrupt == 0) {
2551 ppc_deliver_interrupt(env, interrupt);
2552 if (env->pending_interrupts == 0) {
2553 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
2558 #endif /* !CONFIG_USER_ONLY */
2560 /*****************************************************************************/
2561 /* Exceptions processing helpers */
2563 void raise_exception_err_ra(CPUPPCState *env, uint32_t exception,
2564 uint32_t error_code, uintptr_t raddr)
2566 CPUState *cs = env_cpu(env);
2568 cs->exception_index = exception;
2569 env->error_code = error_code;
2570 cpu_loop_exit_restore(cs, raddr);
2573 void raise_exception_err(CPUPPCState *env, uint32_t exception,
2574 uint32_t error_code)
2576 raise_exception_err_ra(env, exception, error_code, 0);
2579 void raise_exception(CPUPPCState *env, uint32_t exception)
2581 raise_exception_err_ra(env, exception, 0, 0);
2584 void raise_exception_ra(CPUPPCState *env, uint32_t exception,
2587 raise_exception_err_ra(env, exception, 0, raddr);
2591 void helper_raise_exception_err(CPUPPCState *env, uint32_t exception,
2592 uint32_t error_code)
2594 raise_exception_err_ra(env, exception, error_code, 0);
2597 void helper_raise_exception(CPUPPCState *env, uint32_t exception)
2599 raise_exception_err_ra(env, exception, 0, 0);
2603 #if !defined(CONFIG_USER_ONLY)
2605 void helper_store_msr(CPUPPCState *env, target_ulong val)
2607 uint32_t excp = hreg_store_msr(env, val, 0);
2610 CPUState *cs = env_cpu(env);
2611 cpu_interrupt_exittb(cs);
2612 raise_exception(env, excp);
2616 void helper_ppc_maybe_interrupt(CPUPPCState *env)
2618 ppc_maybe_interrupt(env);
2621 #if defined(TARGET_PPC64)
2622 void helper_scv(CPUPPCState *env, uint32_t lev)
2624 if (env->spr[SPR_FSCR] & (1ull << FSCR_SCV)) {
2625 raise_exception_err(env, POWERPC_EXCP_SYSCALL_VECTORED, lev);
2627 raise_exception_err(env, POWERPC_EXCP_FU, FSCR_IC_SCV);
2631 void helper_pminsn(CPUPPCState *env, uint32_t insn)
2638 /* Condition for waking up at 0x100 */
2639 env->resume_as_sreset = (insn != PPC_PM_STOP) ||
2640 (env->spr[SPR_PSSCR] & PSSCR_EC);
2642 ppc_maybe_interrupt(env);
2644 #endif /* defined(TARGET_PPC64) */
2646 static void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr)
2648 CPUState *cs = env_cpu(env);
2650 /* MSR:POW cannot be set by any form of rfi */
2651 msr &= ~(1ULL << MSR_POW);
2653 /* MSR:TGPR cannot be set by any form of rfi */
2654 if (env->flags & POWERPC_FLAG_TGPR)
2655 msr &= ~(1ULL << MSR_TGPR);
2657 #if defined(TARGET_PPC64)
2658 /* Switching to 32-bit ? Crop the nip */
2659 if (!msr_is_64bit(env, msr)) {
2660 nip = (uint32_t)nip;
2663 nip = (uint32_t)nip;
2665 /* XXX: beware: this is false if VLE is supported */
2666 env->nip = nip & ~((target_ulong)0x00000003);
2667 hreg_store_msr(env, msr, 1);
2668 trace_ppc_excp_rfi(env->nip, env->msr);
2670 * No need to raise an exception here, as rfi is always the last
2673 cpu_interrupt_exittb(cs);
2674 /* Reset the reservation */
2675 env->reserve_addr = -1;
2677 /* Context synchronizing: check if TCG TLB needs flush */
2678 check_tlb_flush(env, false);
2681 void helper_rfi(CPUPPCState *env)
2683 do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful);
2686 #if defined(TARGET_PPC64)
2687 void helper_rfid(CPUPPCState *env)
2690 * The architecture defines a number of rules for which bits can
2691 * change but in practice, we handle this in hreg_store_msr()
2692 * which will be called by do_rfi(), so there is no need to filter
2695 do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]);
2698 void helper_rfscv(CPUPPCState *env)
2700 do_rfi(env, env->lr, env->ctr);
2703 void helper_hrfid(CPUPPCState *env)
2705 do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
2709 #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
2710 void helper_rfebb(CPUPPCState *env, target_ulong s)
2712 target_ulong msr = env->msr;
2715 * Handling of BESCR bits 32:33 according to PowerISA v3.1:
2717 * "If BESCR 32:33 != 0b00 the instruction is treated as if
2718 * the instruction form were invalid."
2720 if (env->spr[SPR_BESCR] & BESCR_INVALID) {
2721 raise_exception_err(env, POWERPC_EXCP_PROGRAM,
2722 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL);
2725 env->nip = env->spr[SPR_EBBRR];
2727 /* Switching to 32-bit ? Crop the nip */
2728 if (!msr_is_64bit(env, msr)) {
2729 env->nip = (uint32_t)env->spr[SPR_EBBRR];
2733 env->spr[SPR_BESCR] |= BESCR_GE;
2735 env->spr[SPR_BESCR] &= ~BESCR_GE;
2740 * Triggers or queues an 'ebb_excp' EBB exception. All checks
2741 * but FSCR, HFSCR and msr_pr must be done beforehand.
2743 * PowerISA v3.1 isn't clear about whether an EBB should be
2744 * postponed or cancelled if the EBB facility is unavailable.
2745 * Our assumption here is that the EBB is cancelled if both
2746 * FSCR and HFSCR EBB facilities aren't available.
2748 static void do_ebb(CPUPPCState *env, int ebb_excp)
2750 PowerPCCPU *cpu = env_archcpu(env);
2753 * FSCR_EBB and FSCR_IC_EBB are the same bits used with
2756 helper_fscr_facility_check(env, FSCR_EBB, 0, FSCR_IC_EBB);
2757 helper_hfscr_facility_check(env, FSCR_EBB, "EBB", FSCR_IC_EBB);
2759 if (ebb_excp == POWERPC_EXCP_PERFM_EBB) {
2760 env->spr[SPR_BESCR] |= BESCR_PMEO;
2761 } else if (ebb_excp == POWERPC_EXCP_EXTERNAL_EBB) {
2762 env->spr[SPR_BESCR] |= BESCR_EEO;
2765 if (FIELD_EX64(env->msr, MSR, PR)) {
2766 powerpc_excp(cpu, ebb_excp);
2768 ppc_set_irq(cpu, PPC_INTERRUPT_EBB, 1);
2772 void raise_ebb_perfm_exception(CPUPPCState *env)
2774 bool perfm_ebb_enabled = env->spr[SPR_POWER_MMCR0] & MMCR0_EBE &&
2775 env->spr[SPR_BESCR] & BESCR_PME &&
2776 env->spr[SPR_BESCR] & BESCR_GE;
2778 if (!perfm_ebb_enabled) {
2782 do_ebb(env, POWERPC_EXCP_PERFM_EBB);
2786 /*****************************************************************************/
2787 /* Embedded PowerPC specific helpers */
2788 void helper_40x_rfci(CPUPPCState *env)
2790 do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]);
2793 void helper_rfci(CPUPPCState *env)
2795 do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]);
2798 void helper_rfdi(CPUPPCState *env)
2800 /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
2801 do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]);
2804 void helper_rfmci(CPUPPCState *env)
2806 /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
2807 do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
2809 #endif /* CONFIG_TCG */
2810 #endif /* !defined(CONFIG_USER_ONLY) */
2813 void helper_tw(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2816 if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
2817 ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
2818 ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
2819 ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
2820 ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
2821 raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
2822 POWERPC_EXCP_TRAP, GETPC());
2826 #if defined(TARGET_PPC64)
2827 void helper_td(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
2830 if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
2831 ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
2832 ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
2833 ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
2834 ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) {
2835 raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
2836 POWERPC_EXCP_TRAP, GETPC());
2843 static uint32_t helper_SIMON_LIKE_32_64(uint32_t x, uint64_t key, uint32_t lane)
2845 const uint16_t c = 0xfffc;
2846 const uint64_t z0 = 0xfa2561cdf44ac398ULL;
2847 uint16_t z = 0, temp;
2848 uint16_t k[32], eff_k[32], xleft[33], xright[33], fxleft[32];
2850 for (int i = 3; i >= 0; i--) {
2851 k[i] = key & 0xffff;
2854 xleft[0] = x & 0xffff;
2855 xright[0] = (x >> 16) & 0xffff;
2857 for (int i = 0; i < 28; i++) {
2858 z = (z0 >> (63 - i)) & 1;
2859 temp = ror16(k[i + 3], 3) ^ k[i + 1];
2860 k[i + 4] = c ^ z ^ k[i] ^ temp ^ ror16(temp, 1);
2863 for (int i = 0; i < 8; i++) {
2864 eff_k[4 * i + 0] = k[4 * i + ((0 + lane) % 4)];
2865 eff_k[4 * i + 1] = k[4 * i + ((1 + lane) % 4)];
2866 eff_k[4 * i + 2] = k[4 * i + ((2 + lane) % 4)];
2867 eff_k[4 * i + 3] = k[4 * i + ((3 + lane) % 4)];
2870 for (int i = 0; i < 32; i++) {
2871 fxleft[i] = (rol16(xleft[i], 1) &
2872 rol16(xleft[i], 8)) ^ rol16(xleft[i], 2);
2873 xleft[i + 1] = xright[i] ^ fxleft[i] ^ eff_k[i];
2874 xright[i + 1] = xleft[i];
2877 return (((uint32_t)xright[32]) << 16) | xleft[32];
2880 static uint64_t hash_digest(uint64_t ra, uint64_t rb, uint64_t key)
2882 uint64_t stage0_h = 0ULL, stage0_l = 0ULL;
2883 uint64_t stage1_h, stage1_l;
2885 for (int i = 0; i < 4; i++) {
2886 stage0_h |= ror64(rb & 0xff, 8 * (2 * i + 1));
2887 stage0_h |= ((ra >> 32) & 0xff) << (8 * 2 * i);
2888 stage0_l |= ror64((rb >> 32) & 0xff, 8 * (2 * i + 1));
2889 stage0_l |= (ra & 0xff) << (8 * 2 * i);
2894 stage1_h = (uint64_t)helper_SIMON_LIKE_32_64(stage0_h >> 32, key, 0) << 32;
2895 stage1_h |= helper_SIMON_LIKE_32_64(stage0_h, key, 1);
2896 stage1_l = (uint64_t)helper_SIMON_LIKE_32_64(stage0_l >> 32, key, 2) << 32;
2897 stage1_l |= helper_SIMON_LIKE_32_64(stage0_l, key, 3);
2899 return stage1_h ^ stage1_l;
2902 static void do_hash(CPUPPCState *env, target_ulong ea, target_ulong ra,
2903 target_ulong rb, uint64_t key, bool store)
2905 uint64_t calculated_hash = hash_digest(ra, rb, key), loaded_hash;
2908 cpu_stq_data_ra(env, ea, calculated_hash, GETPC());
2910 loaded_hash = cpu_ldq_data_ra(env, ea, GETPC());
2911 if (loaded_hash != calculated_hash) {
2912 raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
2913 POWERPC_EXCP_TRAP, GETPC());
2918 #include "qemu/guest-random.h"
2921 #define HELPER_HASH(op, key, store, dexcr_aspect) \
2922 void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra, \
2925 if (env->msr & R_MSR_PR_MASK) { \
2926 if (!(env->spr[SPR_DEXCR] & R_DEXCR_PRO_##dexcr_aspect##_MASK || \
2927 env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK)) \
2929 } else if (!(env->msr & R_MSR_HV_MASK)) { \
2930 if (!(env->spr[SPR_DEXCR] & R_DEXCR_PNH_##dexcr_aspect##_MASK || \
2931 env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK)) \
2933 } else if (!(env->msr & R_MSR_S_MASK)) { \
2934 if (!(env->spr[SPR_HDEXCR] & R_HDEXCR_HNU_##dexcr_aspect##_MASK)) \
2938 do_hash(env, ea, ra, rb, key, store); \
2941 #define HELPER_HASH(op, key, store, dexcr_aspect) \
2942 void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra, \
2945 do_hash(env, ea, ra, rb, key, store); \
2947 #endif /* TARGET_PPC64 */
2949 HELPER_HASH(HASHST, env->spr[SPR_HASHKEYR], true, NPHIE)
2950 HELPER_HASH(HASHCHK, env->spr[SPR_HASHKEYR], false, NPHIE)
2951 HELPER_HASH(HASHSTP, env->spr[SPR_HASHPKEYR], true, PHIE)
2952 HELPER_HASH(HASHCHKP, env->spr[SPR_HASHPKEYR], false, PHIE)
2953 #endif /* CONFIG_TCG */
2955 #if !defined(CONFIG_USER_ONLY)
2959 /* Embedded.Processor Control */
2960 static int dbell2irq(target_ulong rb)
2962 int msg = rb & DBELL_TYPE_MASK;
2966 case DBELL_TYPE_DBELL:
2967 irq = PPC_INTERRUPT_DOORBELL;
2969 case DBELL_TYPE_DBELL_CRIT:
2970 irq = PPC_INTERRUPT_CDOORBELL;
2972 case DBELL_TYPE_G_DBELL:
2973 case DBELL_TYPE_G_DBELL_CRIT:
2974 case DBELL_TYPE_G_DBELL_MC:
2983 void helper_msgclr(CPUPPCState *env, target_ulong rb)
2985 int irq = dbell2irq(rb);
2991 ppc_set_irq(env_archcpu(env), irq, 0);
2994 void helper_msgsnd(target_ulong rb)
2996 int irq = dbell2irq(rb);
2997 int pir = rb & DBELL_PIRTAG_MASK;
3004 qemu_mutex_lock_iothread();
3006 PowerPCCPU *cpu = POWERPC_CPU(cs);
3007 CPUPPCState *cenv = &cpu->env;
3009 if ((rb & DBELL_BRDCAST) || (cenv->spr[SPR_BOOKE_PIR] == pir)) {
3010 ppc_set_irq(cpu, irq, 1);
3013 qemu_mutex_unlock_iothread();
3016 /* Server Processor Control */
3018 static bool dbell_type_server(target_ulong rb)
3021 * A Directed Hypervisor Doorbell message is sent only if the
3022 * message type is 5. All other types are reserved and the
3023 * instruction is a no-op
3025 return (rb & DBELL_TYPE_MASK) == DBELL_TYPE_DBELL_SERVER;
3028 void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb)
3030 if (!dbell_type_server(rb)) {
3034 ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_HDOORBELL, 0);
3037 static void book3s_msgsnd_common(int pir, int irq)
3041 qemu_mutex_lock_iothread();
3043 PowerPCCPU *cpu = POWERPC_CPU(cs);
3044 CPUPPCState *cenv = &cpu->env;
3046 /* TODO: broadcast message to all threads of the same processor */
3047 if (cenv->spr_cb[SPR_PIR].default_value == pir) {
3048 ppc_set_irq(cpu, irq, 1);
3051 qemu_mutex_unlock_iothread();
3054 void helper_book3s_msgsnd(target_ulong rb)
3056 int pir = rb & DBELL_PROCIDTAG_MASK;
3058 if (!dbell_type_server(rb)) {
3062 book3s_msgsnd_common(pir, PPC_INTERRUPT_HDOORBELL);
3065 #if defined(TARGET_PPC64)
3066 void helper_book3s_msgclrp(CPUPPCState *env, target_ulong rb)
3068 helper_hfscr_facility_check(env, HFSCR_MSGP, "msgclrp", HFSCR_IC_MSGP);
3070 if (!dbell_type_server(rb)) {
3074 ppc_set_irq(env_archcpu(env), PPC_INTERRUPT_DOORBELL, 0);
3078 * sends a message to other threads that are on the same
3079 * multi-threaded processor
3081 void helper_book3s_msgsndp(CPUPPCState *env, target_ulong rb)
3083 int pir = env->spr_cb[SPR_PIR].default_value;
3085 helper_hfscr_facility_check(env, HFSCR_MSGP, "msgsndp", HFSCR_IC_MSGP);
3087 if (!dbell_type_server(rb)) {
3091 /* TODO: TCG supports only one thread */
3093 book3s_msgsnd_common(pir, PPC_INTERRUPT_DOORBELL);
3095 #endif /* TARGET_PPC64 */
3097 void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
3098 MMUAccessType access_type,
3099 int mmu_idx, uintptr_t retaddr)
3101 CPUPPCState *env = cs->env_ptr;
3104 /* Restore state and reload the insn we executed, for filling in DSISR. */
3105 cpu_restore_state(cs, retaddr);
3106 insn = cpu_ldl_code(env, env->nip);
3108 switch (env->mmu_model) {
3109 case POWERPC_MMU_SOFT_4xx:
3110 env->spr[SPR_40x_DEAR] = vaddr;
3112 case POWERPC_MMU_BOOKE:
3113 case POWERPC_MMU_BOOKE206:
3114 env->spr[SPR_BOOKE_DEAR] = vaddr;
3117 env->spr[SPR_DAR] = vaddr;
3121 cs->exception_index = POWERPC_EXCP_ALIGN;
3122 env->error_code = insn & 0x03FF0000;
3125 #endif /* CONFIG_TCG */
3126 #endif /* !CONFIG_USER_ONLY */