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 "sysemu/tcg.h"
32 #include "exec/helper-proto.h"
33 #include "exec/cpu_ldst.h"
36 /*****************************************************************************/
37 /* Exception processing */
38 #if !defined(CONFIG_USER_ONLY)
40 static const char *powerpc_excp_name(int excp
)
43 case POWERPC_EXCP_CRITICAL
: return "CRITICAL";
44 case POWERPC_EXCP_MCHECK
: return "MCHECK";
45 case POWERPC_EXCP_DSI
: return "DSI";
46 case POWERPC_EXCP_ISI
: return "ISI";
47 case POWERPC_EXCP_EXTERNAL
: return "EXTERNAL";
48 case POWERPC_EXCP_ALIGN
: return "ALIGN";
49 case POWERPC_EXCP_PROGRAM
: return "PROGRAM";
50 case POWERPC_EXCP_FPU
: return "FPU";
51 case POWERPC_EXCP_SYSCALL
: return "SYSCALL";
52 case POWERPC_EXCP_APU
: return "APU";
53 case POWERPC_EXCP_DECR
: return "DECR";
54 case POWERPC_EXCP_FIT
: return "FIT";
55 case POWERPC_EXCP_WDT
: return "WDT";
56 case POWERPC_EXCP_DTLB
: return "DTLB";
57 case POWERPC_EXCP_ITLB
: return "ITLB";
58 case POWERPC_EXCP_DEBUG
: return "DEBUG";
59 case POWERPC_EXCP_SPEU
: return "SPEU";
60 case POWERPC_EXCP_EFPDI
: return "EFPDI";
61 case POWERPC_EXCP_EFPRI
: return "EFPRI";
62 case POWERPC_EXCP_EPERFM
: return "EPERFM";
63 case POWERPC_EXCP_DOORI
: return "DOORI";
64 case POWERPC_EXCP_DOORCI
: return "DOORCI";
65 case POWERPC_EXCP_GDOORI
: return "GDOORI";
66 case POWERPC_EXCP_GDOORCI
: return "GDOORCI";
67 case POWERPC_EXCP_HYPPRIV
: return "HYPPRIV";
68 case POWERPC_EXCP_RESET
: return "RESET";
69 case POWERPC_EXCP_DSEG
: return "DSEG";
70 case POWERPC_EXCP_ISEG
: return "ISEG";
71 case POWERPC_EXCP_HDECR
: return "HDECR";
72 case POWERPC_EXCP_TRACE
: return "TRACE";
73 case POWERPC_EXCP_HDSI
: return "HDSI";
74 case POWERPC_EXCP_HISI
: return "HISI";
75 case POWERPC_EXCP_HDSEG
: return "HDSEG";
76 case POWERPC_EXCP_HISEG
: return "HISEG";
77 case POWERPC_EXCP_VPU
: return "VPU";
78 case POWERPC_EXCP_PIT
: return "PIT";
79 case POWERPC_EXCP_EMUL
: return "EMUL";
80 case POWERPC_EXCP_IFTLB
: return "IFTLB";
81 case POWERPC_EXCP_DLTLB
: return "DLTLB";
82 case POWERPC_EXCP_DSTLB
: return "DSTLB";
83 case POWERPC_EXCP_FPA
: return "FPA";
84 case POWERPC_EXCP_DABR
: return "DABR";
85 case POWERPC_EXCP_IABR
: return "IABR";
86 case POWERPC_EXCP_SMI
: return "SMI";
87 case POWERPC_EXCP_PERFM
: return "PERFM";
88 case POWERPC_EXCP_THERM
: return "THERM";
89 case POWERPC_EXCP_VPUA
: return "VPUA";
90 case POWERPC_EXCP_SOFTP
: return "SOFTP";
91 case POWERPC_EXCP_MAINT
: return "MAINT";
92 case POWERPC_EXCP_MEXTBR
: return "MEXTBR";
93 case POWERPC_EXCP_NMEXTBR
: return "NMEXTBR";
94 case POWERPC_EXCP_ITLBE
: return "ITLBE";
95 case POWERPC_EXCP_DTLBE
: return "DTLBE";
96 case POWERPC_EXCP_VSXU
: return "VSXU";
97 case POWERPC_EXCP_FU
: return "FU";
98 case POWERPC_EXCP_HV_EMU
: return "HV_EMU";
99 case POWERPC_EXCP_HV_MAINT
: return "HV_MAINT";
100 case POWERPC_EXCP_HV_FU
: return "HV_FU";
101 case POWERPC_EXCP_SDOOR
: return "SDOOR";
102 case POWERPC_EXCP_SDOOR_HV
: return "SDOOR_HV";
103 case POWERPC_EXCP_HVIRT
: return "HVIRT";
104 case POWERPC_EXCP_SYSCALL_VECTORED
: return "SYSCALL_VECTORED";
106 g_assert_not_reached();
110 static void dump_syscall(CPUPPCState
*env
)
112 qemu_log_mask(CPU_LOG_INT
, "syscall r0=%016" PRIx64
113 " r3=%016" PRIx64
" r4=%016" PRIx64
" r5=%016" PRIx64
114 " r6=%016" PRIx64
" r7=%016" PRIx64
" r8=%016" PRIx64
115 " nip=" TARGET_FMT_lx
"\n",
116 ppc_dump_gpr(env
, 0), ppc_dump_gpr(env
, 3),
117 ppc_dump_gpr(env
, 4), ppc_dump_gpr(env
, 5),
118 ppc_dump_gpr(env
, 6), ppc_dump_gpr(env
, 7),
119 ppc_dump_gpr(env
, 8), env
->nip
);
122 static void dump_hcall(CPUPPCState
*env
)
124 qemu_log_mask(CPU_LOG_INT
, "hypercall r3=%016" PRIx64
125 " r4=%016" PRIx64
" r5=%016" PRIx64
" r6=%016" PRIx64
126 " r7=%016" PRIx64
" r8=%016" PRIx64
" r9=%016" PRIx64
127 " r10=%016" PRIx64
" r11=%016" PRIx64
" r12=%016" PRIx64
128 " nip=" TARGET_FMT_lx
"\n",
129 ppc_dump_gpr(env
, 3), ppc_dump_gpr(env
, 4),
130 ppc_dump_gpr(env
, 5), ppc_dump_gpr(env
, 6),
131 ppc_dump_gpr(env
, 7), ppc_dump_gpr(env
, 8),
132 ppc_dump_gpr(env
, 9), ppc_dump_gpr(env
, 10),
133 ppc_dump_gpr(env
, 11), ppc_dump_gpr(env
, 12),
138 /* Return true iff byteswap is needed to load instruction */
139 static inline bool insn_need_byteswap(CPUArchState
*env
)
141 /* SYSTEM builds TARGET_BIG_ENDIAN. Need to swap when MSR[LE] is set */
142 return !!(env
->msr
& ((target_ulong
)1 << MSR_LE
));
145 static uint32_t ppc_ldl_code(CPUArchState
*env
, abi_ptr addr
)
147 uint32_t insn
= cpu_ldl_code(env
, addr
);
149 if (insn_need_byteswap(env
)) {
150 insn
= bswap32(insn
);
157 static void ppc_excp_debug_sw_tlb(CPUPPCState
*env
, int excp
)
160 target_ulong
*miss
, *cmp
;
163 if (!qemu_loglevel_mask(CPU_LOG_MMU
)) {
167 if (excp
== POWERPC_EXCP_IFTLB
) {
170 miss
= &env
->spr
[SPR_IMISS
];
171 cmp
= &env
->spr
[SPR_ICMP
];
173 if (excp
== POWERPC_EXCP_DLTLB
) {
179 miss
= &env
->spr
[SPR_DMISS
];
180 cmp
= &env
->spr
[SPR_DCMP
];
182 qemu_log("6xx %sTLB miss: %cM " TARGET_FMT_lx
" %cC "
183 TARGET_FMT_lx
" H1 " TARGET_FMT_lx
" H2 "
184 TARGET_FMT_lx
" %08x\n", es
, en
, *miss
, en
, *cmp
,
185 env
->spr
[SPR_HASH1
], env
->spr
[SPR_HASH2
],
189 #if defined(TARGET_PPC64)
190 static int powerpc_reset_wakeup(CPUPPCState
*env
, int excp
, target_ulong
*msr
)
192 /* We no longer are in a PM state */
193 env
->resume_as_sreset
= false;
195 /* Pretend to be returning from doze always as we don't lose state */
196 *msr
|= SRR1_WS_NOLOSS
;
198 /* Machine checks are sent normally */
199 if (excp
== POWERPC_EXCP_MCHECK
) {
203 case POWERPC_EXCP_RESET
:
204 *msr
|= SRR1_WAKERESET
;
206 case POWERPC_EXCP_EXTERNAL
:
209 case POWERPC_EXCP_DECR
:
210 *msr
|= SRR1_WAKEDEC
;
212 case POWERPC_EXCP_SDOOR
:
213 *msr
|= SRR1_WAKEDBELL
;
215 case POWERPC_EXCP_SDOOR_HV
:
216 *msr
|= SRR1_WAKEHDBELL
;
218 case POWERPC_EXCP_HV_MAINT
:
219 *msr
|= SRR1_WAKEHMI
;
221 case POWERPC_EXCP_HVIRT
:
222 *msr
|= SRR1_WAKEHVI
;
225 cpu_abort(env_cpu(env
),
226 "Unsupported exception %d in Power Save mode\n", excp
);
228 return POWERPC_EXCP_RESET
;
232 * AIL - Alternate Interrupt Location, a mode that allows interrupts to be
233 * taken with the MMU on, and which uses an alternate location (e.g., so the
234 * kernel/hv can map the vectors there with an effective address).
236 * An interrupt is considered to be taken "with AIL" or "AIL applies" if they
237 * are delivered in this way. AIL requires the LPCR to be set to enable this
238 * mode, and then a number of conditions have to be true for AIL to apply.
240 * First of all, SRESET, MCE, and HMI are always delivered without AIL, because
241 * they specifically want to be in real mode (e.g., the MCE might be signaling
242 * a SLB multi-hit which requires SLB flush before the MMU can be enabled).
244 * After that, behaviour depends on the current MSR[IR], MSR[DR], MSR[HV],
245 * whether or not the interrupt changes MSR[HV] from 0 to 1, and the current
246 * radix mode (LPCR[HR]).
248 * POWER8, POWER9 with LPCR[HR]=0
249 * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
250 * +-----------+-------------+---------+-------------+-----+
251 * | a | 00/01/10 | x | x | 0 |
252 * | a | 11 | 0 | 1 | 0 |
253 * | a | 11 | 1 | 1 | a |
254 * | a | 11 | 0 | 0 | a |
255 * +-------------------------------------------------------+
257 * POWER9 with LPCR[HR]=1
258 * | LPCR[AIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
259 * +-----------+-------------+---------+-------------+-----+
260 * | a | 00/01/10 | x | x | 0 |
261 * | a | 11 | x | x | a |
262 * +-------------------------------------------------------+
264 * The difference with POWER9 being that MSR[HV] 0->1 interrupts can be sent to
265 * the hypervisor in AIL mode if the guest is radix. This is good for
266 * performance but allows the guest to influence the AIL of hypervisor
267 * interrupts using its MSR, and also the hypervisor must disallow guest
268 * interrupts (MSR[HV] 0->0) from using AIL if the hypervisor does not want to
269 * use AIL for its MSR[HV] 0->1 interrupts.
271 * POWER10 addresses those issues with a new LPCR[HAIL] bit that is applied to
272 * interrupts that begin execution with MSR[HV]=1 (so both MSR[HV] 0->1 and
275 * HAIL=1 is equivalent to AIL=3, for interrupts delivered with MSR[HV]=1.
277 * POWER10 behaviour is
278 * | LPCR[AIL] | LPCR[HAIL] | MSR[IR||DR] | MSR[HV] | new MSR[HV] | AIL |
279 * +-----------+------------+-------------+---------+-------------+-----+
280 * | a | h | 00/01/10 | 0 | 0 | 0 |
281 * | a | h | 11 | 0 | 0 | a |
282 * | a | h | x | 0 | 1 | h |
283 * | a | h | 00/01/10 | 1 | 1 | 0 |
284 * | a | h | 11 | 1 | 1 | h |
285 * +--------------------------------------------------------------------+
287 static void ppc_excp_apply_ail(PowerPCCPU
*cpu
, int excp
, target_ulong msr
,
288 target_ulong
*new_msr
, target_ulong
*vector
)
290 PowerPCCPUClass
*pcc
= POWERPC_CPU_GET_CLASS(cpu
);
291 CPUPPCState
*env
= &cpu
->env
;
292 bool mmu_all_on
= ((msr
>> MSR_IR
) & 1) && ((msr
>> MSR_DR
) & 1);
293 bool hv_escalation
= !(msr
& MSR_HVB
) && (*new_msr
& MSR_HVB
);
296 if (excp
== POWERPC_EXCP_MCHECK
||
297 excp
== POWERPC_EXCP_RESET
||
298 excp
== POWERPC_EXCP_HV_MAINT
) {
299 /* SRESET, MCE, HMI never apply AIL */
303 if (!(pcc
->lpcr_mask
& LPCR_AIL
)) {
304 /* This CPU does not have AIL */
309 if (!(pcc
->lpcr_mask
& LPCR_HAIL
)) {
311 /* AIL only works if MSR[IR] and MSR[DR] are both enabled. */
314 if (hv_escalation
&& !(env
->spr
[SPR_LPCR
] & LPCR_HR
)) {
316 * AIL does not work if there is a MSR[HV] 0->1 transition and the
317 * partition is in HPT mode. For radix guests, such interrupts are
318 * allowed to be delivered to the hypervisor in ail mode.
323 ail
= (env
->spr
[SPR_LPCR
] & LPCR_AIL
) >> LPCR_AIL_SHIFT
;
328 /* AIL=1 is reserved, treat it like AIL=0 */
334 if (!mmu_all_on
&& !hv_escalation
) {
336 * AIL works for HV interrupts even with guest MSR[IR/DR] disabled.
337 * Guest->guest and HV->HV interrupts do require MMU on.
342 if (*new_msr
& MSR_HVB
) {
343 if (!(env
->spr
[SPR_LPCR
] & LPCR_HAIL
)) {
344 /* HV interrupts depend on LPCR[HAIL] */
347 ail
= 3; /* HAIL=1 gives AIL=3 behaviour for HV interrupts */
349 ail
= (env
->spr
[SPR_LPCR
] & LPCR_AIL
) >> LPCR_AIL_SHIFT
;
354 if (ail
== 1 || ail
== 2) {
355 /* AIL=1 and AIL=2 are reserved, treat them like AIL=0 */
361 * AIL applies, so the new MSR gets IR and DR set, and an offset applied
364 *new_msr
|= (1 << MSR_IR
) | (1 << MSR_DR
);
366 if (excp
!= POWERPC_EXCP_SYSCALL_VECTORED
) {
368 *vector
|= 0x0000000000018000ull
;
369 } else if (ail
== 3) {
370 *vector
|= 0xc000000000004000ull
;
374 * scv AIL is a little different. AIL=2 does not change the address,
375 * only the MSR. AIL=3 replaces the 0x17000 base with 0xc...3000.
378 *vector
&= ~0x0000000000017000ull
; /* Un-apply the base offset */
379 *vector
|= 0xc000000000003000ull
; /* Apply scv's AIL=3 offset */
385 static void powerpc_reset_excp_state(PowerPCCPU
*cpu
)
387 CPUState
*cs
= CPU(cpu
);
388 CPUPPCState
*env
= &cpu
->env
;
390 /* Reset exception state */
391 cs
->exception_index
= POWERPC_EXCP_NONE
;
395 static void powerpc_set_excp_state(PowerPCCPU
*cpu
, target_ulong vector
,
398 CPUPPCState
*env
= &cpu
->env
;
400 assert((msr
& env
->msr_mask
) == msr
);
403 * We don't use hreg_store_msr here as already have treated any
404 * special case that could occur. Just store MSR and update hflags
406 * Note: We *MUST* not use hreg_store_msr() as-is anyway because it
407 * will prevent setting of the HV bit which some exceptions might need
412 hreg_compute_hflags(env
);
413 ppc_maybe_interrupt(env
);
415 powerpc_reset_excp_state(cpu
);
418 * Any interrupt is context synchronizing, check if TCG TLB needs
419 * a delayed flush on ppc64
421 check_tlb_flush(env
, false);
423 /* Reset the reservation */
424 env
->reserve_addr
= -1;
427 static void powerpc_mcheck_checkstop(CPUPPCState
*env
)
429 CPUState
*cs
= env_cpu(env
);
431 if (FIELD_EX64(env
->msr
, MSR
, ME
)) {
435 /* Machine check exception is not enabled. Enter checkstop state. */
436 fprintf(stderr
, "Machine check while not allowed. "
437 "Entering checkstop state\n");
438 if (qemu_log_separate()) {
439 qemu_log("Machine check while not allowed. "
440 "Entering checkstop state\n");
443 cpu_interrupt_exittb(cs
);
446 static void powerpc_excp_40x(PowerPCCPU
*cpu
, int excp
)
448 CPUState
*cs
= CPU(cpu
);
449 CPUPPCState
*env
= &cpu
->env
;
450 target_ulong msr
, new_msr
, vector
;
453 /* new srr1 value excluding must-be-zero bits */
454 msr
= env
->msr
& ~0x783f0000ULL
;
457 * new interrupt handler msr preserves existing ME unless
458 * explicitly overridden.
460 new_msr
= env
->msr
& (((target_ulong
)1 << MSR_ME
));
462 /* target registers */
467 * Hypervisor emulation assistance interrupt only exists on server
468 * arch 2.05 server or later.
470 if (excp
== POWERPC_EXCP_HV_EMU
) {
471 excp
= POWERPC_EXCP_PROGRAM
;
474 vector
= env
->excp_vectors
[excp
];
475 if (vector
== (target_ulong
)-1ULL) {
476 cpu_abort(cs
, "Raised an exception without defined vector %d\n",
480 vector
|= env
->excp_prefix
;
483 case POWERPC_EXCP_CRITICAL
: /* Critical input */
487 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
488 powerpc_mcheck_checkstop(env
);
489 /* machine check exceptions don't have ME set */
490 new_msr
&= ~((target_ulong
)1 << MSR_ME
);
495 case POWERPC_EXCP_DSI
: /* Data storage exception */
496 trace_ppc_excp_dsi(env
->spr
[SPR_40x_ESR
], env
->spr
[SPR_40x_DEAR
]);
498 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
499 trace_ppc_excp_isi(msr
, env
->nip
);
501 case POWERPC_EXCP_EXTERNAL
: /* External input */
503 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
505 case POWERPC_EXCP_PROGRAM
: /* Program exception */
506 switch (env
->error_code
& ~0xF) {
507 case POWERPC_EXCP_FP
:
508 if (!FIELD_EX64_FE(env
->msr
) || !FIELD_EX64(env
->msr
, MSR
, FP
)) {
509 trace_ppc_excp_fp_ignore();
510 powerpc_reset_excp_state(cpu
);
513 env
->spr
[SPR_40x_ESR
] = ESR_FP
;
515 case POWERPC_EXCP_INVAL
:
516 trace_ppc_excp_inval(env
->nip
);
517 env
->spr
[SPR_40x_ESR
] = ESR_PIL
;
519 case POWERPC_EXCP_PRIV
:
520 env
->spr
[SPR_40x_ESR
] = ESR_PPR
;
522 case POWERPC_EXCP_TRAP
:
523 env
->spr
[SPR_40x_ESR
] = ESR_PTR
;
526 cpu_abort(cs
, "Invalid program exception %d. Aborting\n",
531 case POWERPC_EXCP_SYSCALL
: /* System call exception */
535 * We need to correct the NIP which in this case is supposed
536 * to point to the next instruction
540 case POWERPC_EXCP_FIT
: /* Fixed-interval timer interrupt */
541 trace_ppc_excp_print("FIT");
543 case POWERPC_EXCP_WDT
: /* Watchdog timer interrupt */
544 trace_ppc_excp_print("WDT");
546 case POWERPC_EXCP_DTLB
: /* Data TLB error */
547 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
549 case POWERPC_EXCP_PIT
: /* Programmable interval timer interrupt */
550 trace_ppc_excp_print("PIT");
552 case POWERPC_EXCP_DEBUG
: /* Debug interrupt */
553 cpu_abort(cs
, "%s exception not implemented\n",
554 powerpc_excp_name(excp
));
557 cpu_abort(cs
, "Invalid PowerPC exception %d. Aborting\n", excp
);
562 env
->spr
[srr0
] = env
->nip
;
565 env
->spr
[srr1
] = msr
;
567 powerpc_set_excp_state(cpu
, vector
, new_msr
);
570 static void powerpc_excp_6xx(PowerPCCPU
*cpu
, int excp
)
572 CPUState
*cs
= CPU(cpu
);
573 CPUPPCState
*env
= &cpu
->env
;
574 target_ulong msr
, new_msr
, vector
;
576 /* new srr1 value excluding must-be-zero bits */
577 msr
= env
->msr
& ~0x783f0000ULL
;
580 * new interrupt handler msr preserves existing ME unless
581 * explicitly overridden
583 new_msr
= env
->msr
& ((target_ulong
)1 << MSR_ME
);
586 * Hypervisor emulation assistance interrupt only exists on server
587 * arch 2.05 server or later.
589 if (excp
== POWERPC_EXCP_HV_EMU
) {
590 excp
= POWERPC_EXCP_PROGRAM
;
593 vector
= env
->excp_vectors
[excp
];
594 if (vector
== (target_ulong
)-1ULL) {
595 cpu_abort(cs
, "Raised an exception without defined vector %d\n",
599 vector
|= env
->excp_prefix
;
602 case POWERPC_EXCP_CRITICAL
: /* Critical input */
604 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
605 powerpc_mcheck_checkstop(env
);
606 /* machine check exceptions don't have ME set */
607 new_msr
&= ~((target_ulong
)1 << MSR_ME
);
610 case POWERPC_EXCP_DSI
: /* Data storage exception */
611 trace_ppc_excp_dsi(env
->spr
[SPR_DSISR
], env
->spr
[SPR_DAR
]);
613 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
614 trace_ppc_excp_isi(msr
, env
->nip
);
615 msr
|= env
->error_code
;
617 case POWERPC_EXCP_EXTERNAL
: /* External input */
619 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
620 /* Get rS/rD and rA from faulting opcode */
622 * Note: the opcode fields will not be set properly for a
623 * direct store load/store, but nobody cares as nobody
624 * actually uses direct store segments.
626 env
->spr
[SPR_DSISR
] |= (env
->error_code
& 0x03FF0000) >> 16;
628 case POWERPC_EXCP_PROGRAM
: /* Program exception */
629 switch (env
->error_code
& ~0xF) {
630 case POWERPC_EXCP_FP
:
631 if (!FIELD_EX64_FE(env
->msr
) || !FIELD_EX64(env
->msr
, MSR
, FP
)) {
632 trace_ppc_excp_fp_ignore();
633 powerpc_reset_excp_state(cpu
);
638 * FP exceptions always have NIP pointing to the faulting
639 * instruction, so always use store_next and claim we are
640 * precise in the MSR.
644 case POWERPC_EXCP_INVAL
:
645 trace_ppc_excp_inval(env
->nip
);
648 case POWERPC_EXCP_PRIV
:
651 case POWERPC_EXCP_TRAP
:
655 /* Should never occur */
656 cpu_abort(cs
, "Invalid program exception %d. Aborting\n",
661 case POWERPC_EXCP_SYSCALL
: /* System call exception */
665 * We need to correct the NIP which in this case is supposed
666 * to point to the next instruction
670 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
671 case POWERPC_EXCP_DECR
: /* Decrementer exception */
673 case POWERPC_EXCP_DTLB
: /* Data TLB error */
674 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
676 case POWERPC_EXCP_RESET
: /* System reset exception */
677 if (FIELD_EX64(env
->msr
, MSR
, POW
)) {
678 cpu_abort(cs
, "Trying to deliver power-saving system reset "
679 "exception %d with no HV support\n", excp
);
682 case POWERPC_EXCP_TRACE
: /* Trace exception */
684 case POWERPC_EXCP_IFTLB
: /* Instruction fetch TLB error */
685 case POWERPC_EXCP_DLTLB
: /* Data load TLB miss */
686 case POWERPC_EXCP_DSTLB
: /* Data store TLB miss */
687 /* Swap temporary saved registers with GPRs */
688 if (!(new_msr
& ((target_ulong
)1 << MSR_TGPR
))) {
689 new_msr
|= (target_ulong
)1 << MSR_TGPR
;
690 hreg_swap_gpr_tgpr(env
);
693 ppc_excp_debug_sw_tlb(env
, excp
);
695 msr
|= env
->crf
[0] << 28;
696 msr
|= env
->error_code
; /* key, D/I, S/L bits */
697 /* Set way using a LRU mechanism */
698 msr
|= ((env
->last_way
+ 1) & (env
->nb_ways
- 1)) << 17;
700 case POWERPC_EXCP_FPA
: /* Floating-point assist exception */
701 case POWERPC_EXCP_DABR
: /* Data address breakpoint */
702 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
703 case POWERPC_EXCP_SMI
: /* System management interrupt */
704 case POWERPC_EXCP_MEXTBR
: /* Maskable external breakpoint */
705 case POWERPC_EXCP_NMEXTBR
: /* Non maskable external breakpoint */
706 cpu_abort(cs
, "%s exception not implemented\n",
707 powerpc_excp_name(excp
));
710 cpu_abort(cs
, "Invalid PowerPC exception %d. Aborting\n", excp
);
715 * Sort out endianness of interrupt, this differs depending on the
716 * CPU, the HV mode, etc...
718 if (ppc_interrupts_little_endian(cpu
, !!(new_msr
& MSR_HVB
))) {
719 new_msr
|= (target_ulong
)1 << MSR_LE
;
723 env
->spr
[SPR_SRR0
] = env
->nip
;
726 env
->spr
[SPR_SRR1
] = msr
;
728 powerpc_set_excp_state(cpu
, vector
, new_msr
);
731 static void powerpc_excp_7xx(PowerPCCPU
*cpu
, int excp
)
733 CPUState
*cs
= CPU(cpu
);
734 CPUPPCState
*env
= &cpu
->env
;
735 target_ulong msr
, new_msr
, vector
;
737 /* new srr1 value excluding must-be-zero bits */
738 msr
= env
->msr
& ~0x783f0000ULL
;
741 * new interrupt handler msr preserves existing ME unless
742 * explicitly overridden
744 new_msr
= env
->msr
& ((target_ulong
)1 << MSR_ME
);
747 * Hypervisor emulation assistance interrupt only exists on server
748 * arch 2.05 server or later.
750 if (excp
== POWERPC_EXCP_HV_EMU
) {
751 excp
= POWERPC_EXCP_PROGRAM
;
754 vector
= env
->excp_vectors
[excp
];
755 if (vector
== (target_ulong
)-1ULL) {
756 cpu_abort(cs
, "Raised an exception without defined vector %d\n",
760 vector
|= env
->excp_prefix
;
763 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
764 powerpc_mcheck_checkstop(env
);
765 /* machine check exceptions don't have ME set */
766 new_msr
&= ~((target_ulong
)1 << MSR_ME
);
769 case POWERPC_EXCP_DSI
: /* Data storage exception */
770 trace_ppc_excp_dsi(env
->spr
[SPR_DSISR
], env
->spr
[SPR_DAR
]);
772 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
773 trace_ppc_excp_isi(msr
, env
->nip
);
774 msr
|= env
->error_code
;
776 case POWERPC_EXCP_EXTERNAL
: /* External input */
778 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
779 /* Get rS/rD and rA from faulting opcode */
781 * Note: the opcode fields will not be set properly for a
782 * direct store load/store, but nobody cares as nobody
783 * actually uses direct store segments.
785 env
->spr
[SPR_DSISR
] |= (env
->error_code
& 0x03FF0000) >> 16;
787 case POWERPC_EXCP_PROGRAM
: /* Program exception */
788 switch (env
->error_code
& ~0xF) {
789 case POWERPC_EXCP_FP
:
790 if (!FIELD_EX64_FE(env
->msr
) || !FIELD_EX64(env
->msr
, MSR
, FP
)) {
791 trace_ppc_excp_fp_ignore();
792 powerpc_reset_excp_state(cpu
);
797 * FP exceptions always have NIP pointing to the faulting
798 * instruction, so always use store_next and claim we are
799 * precise in the MSR.
803 case POWERPC_EXCP_INVAL
:
804 trace_ppc_excp_inval(env
->nip
);
807 case POWERPC_EXCP_PRIV
:
810 case POWERPC_EXCP_TRAP
:
814 /* Should never occur */
815 cpu_abort(cs
, "Invalid program exception %d. Aborting\n",
820 case POWERPC_EXCP_SYSCALL
: /* System call exception */
822 int lev
= env
->error_code
;
824 if (lev
== 1 && cpu
->vhyp
) {
831 * We need to correct the NIP which in this case is supposed
832 * to point to the next instruction
837 * The Virtual Open Firmware (VOF) relies on the 'sc 1'
838 * instruction to communicate with QEMU. The pegasos2 machine
839 * uses VOF and the 7xx CPUs, so although the 7xx don't have
840 * HV mode, we need to keep hypercall support.
842 if (lev
== 1 && cpu
->vhyp
) {
843 PPCVirtualHypervisorClass
*vhc
=
844 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
845 vhc
->hypercall(cpu
->vhyp
, cpu
);
846 powerpc_reset_excp_state(cpu
);
852 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
853 case POWERPC_EXCP_DECR
: /* Decrementer exception */
855 case POWERPC_EXCP_RESET
: /* System reset exception */
856 if (FIELD_EX64(env
->msr
, MSR
, POW
)) {
857 cpu_abort(cs
, "Trying to deliver power-saving system reset "
858 "exception %d with no HV support\n", excp
);
861 case POWERPC_EXCP_TRACE
: /* Trace exception */
863 case POWERPC_EXCP_IFTLB
: /* Instruction fetch TLB error */
864 case POWERPC_EXCP_DLTLB
: /* Data load TLB miss */
865 case POWERPC_EXCP_DSTLB
: /* Data store TLB miss */
866 ppc_excp_debug_sw_tlb(env
, excp
);
868 msr
|= env
->crf
[0] << 28;
869 msr
|= env
->error_code
; /* key, D/I, S/L bits */
870 /* Set way using a LRU mechanism */
871 msr
|= ((env
->last_way
+ 1) & (env
->nb_ways
- 1)) << 17;
874 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
875 case POWERPC_EXCP_SMI
: /* System management interrupt */
876 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
877 case POWERPC_EXCP_PERFM
: /* Embedded performance monitor interrupt */
878 cpu_abort(cs
, "%s exception not implemented\n",
879 powerpc_excp_name(excp
));
882 cpu_abort(cs
, "Invalid PowerPC exception %d. Aborting\n", excp
);
887 * Sort out endianness of interrupt, this differs depending on the
888 * CPU, the HV mode, etc...
890 if (ppc_interrupts_little_endian(cpu
, !!(new_msr
& MSR_HVB
))) {
891 new_msr
|= (target_ulong
)1 << MSR_LE
;
895 env
->spr
[SPR_SRR0
] = env
->nip
;
898 env
->spr
[SPR_SRR1
] = msr
;
900 powerpc_set_excp_state(cpu
, vector
, new_msr
);
903 static void powerpc_excp_74xx(PowerPCCPU
*cpu
, int excp
)
905 CPUState
*cs
= CPU(cpu
);
906 CPUPPCState
*env
= &cpu
->env
;
907 target_ulong msr
, new_msr
, vector
;
909 /* new srr1 value excluding must-be-zero bits */
910 msr
= env
->msr
& ~0x783f0000ULL
;
913 * new interrupt handler msr preserves existing ME unless
914 * explicitly overridden
916 new_msr
= env
->msr
& ((target_ulong
)1 << MSR_ME
);
919 * Hypervisor emulation assistance interrupt only exists on server
920 * arch 2.05 server or later.
922 if (excp
== POWERPC_EXCP_HV_EMU
) {
923 excp
= POWERPC_EXCP_PROGRAM
;
926 vector
= env
->excp_vectors
[excp
];
927 if (vector
== (target_ulong
)-1ULL) {
928 cpu_abort(cs
, "Raised an exception without defined vector %d\n",
932 vector
|= env
->excp_prefix
;
935 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
936 powerpc_mcheck_checkstop(env
);
937 /* machine check exceptions don't have ME set */
938 new_msr
&= ~((target_ulong
)1 << MSR_ME
);
941 case POWERPC_EXCP_DSI
: /* Data storage exception */
942 trace_ppc_excp_dsi(env
->spr
[SPR_DSISR
], env
->spr
[SPR_DAR
]);
944 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
945 trace_ppc_excp_isi(msr
, env
->nip
);
946 msr
|= env
->error_code
;
948 case POWERPC_EXCP_EXTERNAL
: /* External input */
950 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
951 /* Get rS/rD and rA from faulting opcode */
953 * Note: the opcode fields will not be set properly for a
954 * direct store load/store, but nobody cares as nobody
955 * actually uses direct store segments.
957 env
->spr
[SPR_DSISR
] |= (env
->error_code
& 0x03FF0000) >> 16;
959 case POWERPC_EXCP_PROGRAM
: /* Program exception */
960 switch (env
->error_code
& ~0xF) {
961 case POWERPC_EXCP_FP
:
962 if (!FIELD_EX64_FE(env
->msr
) || !FIELD_EX64(env
->msr
, MSR
, FP
)) {
963 trace_ppc_excp_fp_ignore();
964 powerpc_reset_excp_state(cpu
);
969 * FP exceptions always have NIP pointing to the faulting
970 * instruction, so always use store_next and claim we are
971 * precise in the MSR.
975 case POWERPC_EXCP_INVAL
:
976 trace_ppc_excp_inval(env
->nip
);
979 case POWERPC_EXCP_PRIV
:
982 case POWERPC_EXCP_TRAP
:
986 /* Should never occur */
987 cpu_abort(cs
, "Invalid program exception %d. Aborting\n",
992 case POWERPC_EXCP_SYSCALL
: /* System call exception */
994 int lev
= env
->error_code
;
996 if (lev
== 1 && cpu
->vhyp
) {
1003 * We need to correct the NIP which in this case is supposed
1004 * to point to the next instruction
1009 * The Virtual Open Firmware (VOF) relies on the 'sc 1'
1010 * instruction to communicate with QEMU. The pegasos2 machine
1011 * uses VOF and the 74xx CPUs, so although the 74xx don't have
1012 * HV mode, we need to keep hypercall support.
1014 if (lev
== 1 && cpu
->vhyp
) {
1015 PPCVirtualHypervisorClass
*vhc
=
1016 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
1017 vhc
->hypercall(cpu
->vhyp
, cpu
);
1018 powerpc_reset_excp_state(cpu
);
1024 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
1025 case POWERPC_EXCP_DECR
: /* Decrementer exception */
1027 case POWERPC_EXCP_RESET
: /* System reset exception */
1028 if (FIELD_EX64(env
->msr
, MSR
, POW
)) {
1029 cpu_abort(cs
, "Trying to deliver power-saving system reset "
1030 "exception %d with no HV support\n", excp
);
1033 case POWERPC_EXCP_TRACE
: /* Trace exception */
1035 case POWERPC_EXCP_VPU
: /* Vector unavailable exception */
1037 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
1038 case POWERPC_EXCP_SMI
: /* System management interrupt */
1039 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
1040 case POWERPC_EXCP_PERFM
: /* Embedded performance monitor interrupt */
1041 case POWERPC_EXCP_VPUA
: /* Vector assist exception */
1042 cpu_abort(cs
, "%s exception not implemented\n",
1043 powerpc_excp_name(excp
));
1046 cpu_abort(cs
, "Invalid PowerPC exception %d. Aborting\n", excp
);
1051 * Sort out endianness of interrupt, this differs depending on the
1052 * CPU, the HV mode, etc...
1054 if (ppc_interrupts_little_endian(cpu
, !!(new_msr
& MSR_HVB
))) {
1055 new_msr
|= (target_ulong
)1 << MSR_LE
;
1059 env
->spr
[SPR_SRR0
] = env
->nip
;
1062 env
->spr
[SPR_SRR1
] = msr
;
1064 powerpc_set_excp_state(cpu
, vector
, new_msr
);
1067 static void powerpc_excp_booke(PowerPCCPU
*cpu
, int excp
)
1069 CPUState
*cs
= CPU(cpu
);
1070 CPUPPCState
*env
= &cpu
->env
;
1071 target_ulong msr
, new_msr
, vector
;
1077 * new interrupt handler msr preserves existing ME unless
1078 * explicitly overridden
1080 new_msr
= env
->msr
& ((target_ulong
)1 << MSR_ME
);
1082 /* target registers */
1087 * Hypervisor emulation assistance interrupt only exists on server
1088 * arch 2.05 server or later.
1090 if (excp
== POWERPC_EXCP_HV_EMU
) {
1091 excp
= POWERPC_EXCP_PROGRAM
;
1096 * SPEU and VPU share the same IVOR but they exist in different
1097 * processors. SPEU is e500v1/2 only and VPU is e6500 only.
1099 if (excp
== POWERPC_EXCP_VPU
) {
1100 excp
= POWERPC_EXCP_SPEU
;
1104 vector
= env
->excp_vectors
[excp
];
1105 if (vector
== (target_ulong
)-1ULL) {
1106 cpu_abort(cs
, "Raised an exception without defined vector %d\n",
1110 vector
|= env
->excp_prefix
;
1113 case POWERPC_EXCP_CRITICAL
: /* Critical input */
1114 srr0
= SPR_BOOKE_CSRR0
;
1115 srr1
= SPR_BOOKE_CSRR1
;
1117 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
1118 powerpc_mcheck_checkstop(env
);
1119 /* machine check exceptions don't have ME set */
1120 new_msr
&= ~((target_ulong
)1 << MSR_ME
);
1122 /* FIXME: choose one or the other based on CPU type */
1123 srr0
= SPR_BOOKE_MCSRR0
;
1124 srr1
= SPR_BOOKE_MCSRR1
;
1126 env
->spr
[SPR_BOOKE_CSRR0
] = env
->nip
;
1127 env
->spr
[SPR_BOOKE_CSRR1
] = msr
;
1130 case POWERPC_EXCP_DSI
: /* Data storage exception */
1131 trace_ppc_excp_dsi(env
->spr
[SPR_BOOKE_ESR
], env
->spr
[SPR_BOOKE_DEAR
]);
1133 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
1134 trace_ppc_excp_isi(msr
, env
->nip
);
1136 case POWERPC_EXCP_EXTERNAL
: /* External input */
1137 if (env
->mpic_proxy
) {
1138 /* IACK the IRQ on delivery */
1139 env
->spr
[SPR_BOOKE_EPR
] = ldl_phys(cs
->as
, env
->mpic_iack
);
1142 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
1144 case POWERPC_EXCP_PROGRAM
: /* Program exception */
1145 switch (env
->error_code
& ~0xF) {
1146 case POWERPC_EXCP_FP
:
1147 if (!FIELD_EX64_FE(env
->msr
) || !FIELD_EX64(env
->msr
, MSR
, FP
)) {
1148 trace_ppc_excp_fp_ignore();
1149 powerpc_reset_excp_state(cpu
);
1154 * FP exceptions always have NIP pointing to the faulting
1155 * instruction, so always use store_next and claim we are
1156 * precise in the MSR.
1159 env
->spr
[SPR_BOOKE_ESR
] = ESR_FP
;
1161 case POWERPC_EXCP_INVAL
:
1162 trace_ppc_excp_inval(env
->nip
);
1164 env
->spr
[SPR_BOOKE_ESR
] = ESR_PIL
;
1166 case POWERPC_EXCP_PRIV
:
1168 env
->spr
[SPR_BOOKE_ESR
] = ESR_PPR
;
1170 case POWERPC_EXCP_TRAP
:
1172 env
->spr
[SPR_BOOKE_ESR
] = ESR_PTR
;
1175 /* Should never occur */
1176 cpu_abort(cs
, "Invalid program exception %d. Aborting\n",
1181 case POWERPC_EXCP_SYSCALL
: /* System call exception */
1185 * We need to correct the NIP which in this case is supposed
1186 * to point to the next instruction
1190 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
1191 case POWERPC_EXCP_APU
: /* Auxiliary processor unavailable */
1192 case POWERPC_EXCP_DECR
: /* Decrementer exception */
1194 case POWERPC_EXCP_FIT
: /* Fixed-interval timer interrupt */
1196 trace_ppc_excp_print("FIT");
1198 case POWERPC_EXCP_WDT
: /* Watchdog timer interrupt */
1199 trace_ppc_excp_print("WDT");
1200 srr0
= SPR_BOOKE_CSRR0
;
1201 srr1
= SPR_BOOKE_CSRR1
;
1203 case POWERPC_EXCP_DTLB
: /* Data TLB error */
1204 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
1206 case POWERPC_EXCP_DEBUG
: /* Debug interrupt */
1207 if (env
->flags
& POWERPC_FLAG_DE
) {
1208 /* FIXME: choose one or the other based on CPU type */
1209 srr0
= SPR_BOOKE_DSRR0
;
1210 srr1
= SPR_BOOKE_DSRR1
;
1212 env
->spr
[SPR_BOOKE_CSRR0
] = env
->nip
;
1213 env
->spr
[SPR_BOOKE_CSRR1
] = msr
;
1215 /* DBSR already modified by caller */
1217 cpu_abort(cs
, "Debug exception triggered on unsupported model\n");
1220 case POWERPC_EXCP_SPEU
: /* SPE/embedded floating-point unavailable/VPU */
1221 env
->spr
[SPR_BOOKE_ESR
] = ESR_SPV
;
1223 case POWERPC_EXCP_DOORI
: /* Embedded doorbell interrupt */
1225 case POWERPC_EXCP_DOORCI
: /* Embedded doorbell critical interrupt */
1226 srr0
= SPR_BOOKE_CSRR0
;
1227 srr1
= SPR_BOOKE_CSRR1
;
1229 case POWERPC_EXCP_RESET
: /* System reset exception */
1230 if (FIELD_EX64(env
->msr
, MSR
, POW
)) {
1231 cpu_abort(cs
, "Trying to deliver power-saving system reset "
1232 "exception %d with no HV support\n", excp
);
1235 case POWERPC_EXCP_EFPDI
: /* Embedded floating-point data interrupt */
1236 case POWERPC_EXCP_EFPRI
: /* Embedded floating-point round interrupt */
1237 cpu_abort(cs
, "%s exception not implemented\n",
1238 powerpc_excp_name(excp
));
1241 cpu_abort(cs
, "Invalid PowerPC exception %d. Aborting\n", excp
);
1245 #if defined(TARGET_PPC64)
1246 if (env
->spr
[SPR_BOOKE_EPCR
] & EPCR_ICM
) {
1247 /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */
1248 new_msr
|= (target_ulong
)1 << MSR_CM
;
1250 vector
= (uint32_t)vector
;
1255 env
->spr
[srr0
] = env
->nip
;
1258 env
->spr
[srr1
] = msr
;
1260 powerpc_set_excp_state(cpu
, vector
, new_msr
);
1264 * When running a nested HV guest under vhyp, external interrupts are
1265 * delivered as HVIRT.
1267 static bool books_vhyp_promotes_external_to_hvirt(PowerPCCPU
*cpu
)
1270 return vhyp_cpu_in_nested(cpu
);
1277 * When running under vhyp, hcalls are always intercepted and sent to the
1278 * vhc->hypercall handler.
1280 static bool books_vhyp_handles_hcall(PowerPCCPU
*cpu
)
1283 return !vhyp_cpu_in_nested(cpu
);
1289 * When running a nested KVM HV guest under vhyp, HV exceptions are not
1290 * delivered to the guest (because there is no concept of HV support), but
1291 * rather they are sent to the vhyp to exit from the L2 back to the L1 and
1292 * return from the H_ENTER_NESTED hypercall.
1294 static bool books_vhyp_handles_hv_excp(PowerPCCPU
*cpu
)
1297 return vhyp_cpu_in_nested(cpu
);
1303 static bool is_prefix_insn(CPUPPCState
*env
, uint32_t insn
)
1305 if (!(env
->insns_flags2
& PPC2_ISA310
)) {
1308 return ((insn
& 0xfc000000) == 0x04000000);
1311 static bool is_prefix_insn_excp(PowerPCCPU
*cpu
, int excp
)
1313 CPUPPCState
*env
= &cpu
->env
;
1315 if (!(env
->insns_flags2
& PPC2_ISA310
)) {
1319 if (!tcg_enabled()) {
1321 * This does not load instructions and set the prefix bit correctly
1322 * for injected interrupts with KVM. That may have to be discovered
1323 * and set by the KVM layer before injecting.
1329 case POWERPC_EXCP_MCHECK
:
1330 if (!(env
->error_code
& PPC_BIT(42))) {
1332 * Fetch attempt caused a machine check, so attempting to fetch
1333 * again would cause a recursive machine check.
1338 case POWERPC_EXCP_HDSI
:
1339 /* HDSI PRTABLE_FAULT has the originating access type in error_code */
1340 if ((env
->spr
[SPR_HDSISR
] & DSISR_PRTABLE_FAULT
) &&
1341 (env
->error_code
== MMU_INST_FETCH
)) {
1343 * Fetch failed due to partition scope translation, so prefix
1344 * indication is not relevant (and attempting to load the
1345 * instruction at NIP would cause recursive faults with the same
1352 case POWERPC_EXCP_DSI
:
1353 case POWERPC_EXCP_DSEG
:
1354 case POWERPC_EXCP_ALIGN
:
1355 case POWERPC_EXCP_PROGRAM
:
1356 case POWERPC_EXCP_FPU
:
1357 case POWERPC_EXCP_TRACE
:
1358 case POWERPC_EXCP_HV_EMU
:
1359 case POWERPC_EXCP_VPU
:
1360 case POWERPC_EXCP_VSXU
:
1361 case POWERPC_EXCP_FU
:
1362 case POWERPC_EXCP_HV_FU
:
1368 return is_prefix_insn(env
, ppc_ldl_code(env
, env
->nip
));
1371 static bool is_prefix_insn_excp(PowerPCCPU
*cpu
, int excp
)
1377 static void powerpc_excp_books(PowerPCCPU
*cpu
, int excp
)
1379 CPUState
*cs
= CPU(cpu
);
1380 CPUPPCState
*env
= &cpu
->env
;
1381 target_ulong msr
, new_msr
, vector
;
1382 int srr0
, srr1
, lev
= -1;
1384 /* new srr1 value excluding must-be-zero bits */
1385 msr
= env
->msr
& ~0x783f0000ULL
;
1388 * new interrupt handler msr preserves existing HV and ME unless
1389 * explicitly overridden
1391 new_msr
= env
->msr
& (((target_ulong
)1 << MSR_ME
) | MSR_HVB
);
1393 /* target registers */
1398 * check for special resume at 0x100 from doze/nap/sleep/winkle on
1401 if (env
->resume_as_sreset
) {
1402 excp
= powerpc_reset_wakeup(env
, excp
, &msr
);
1406 * We don't want to generate a Hypervisor Emulation Assistance
1407 * Interrupt if we don't have HVB in msr_mask (PAPR mode),
1408 * unless running a nested-hv guest, in which case the L1
1409 * kernel wants the interrupt.
1411 if (excp
== POWERPC_EXCP_HV_EMU
&& !(env
->msr_mask
& MSR_HVB
) &&
1412 !books_vhyp_handles_hv_excp(cpu
)) {
1413 excp
= POWERPC_EXCP_PROGRAM
;
1416 vector
= env
->excp_vectors
[excp
];
1417 if (vector
== (target_ulong
)-1ULL) {
1418 cpu_abort(cs
, "Raised an exception without defined vector %d\n",
1422 vector
|= env
->excp_prefix
;
1424 if (is_prefix_insn_excp(cpu
, excp
)) {
1429 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
1430 powerpc_mcheck_checkstop(env
);
1431 if (env
->msr_mask
& MSR_HVB
) {
1433 * ISA specifies HV, but can be delivered to guest with HV
1434 * clear (e.g., see FWNMI in PAPR).
1436 new_msr
|= (target_ulong
)MSR_HVB
;
1439 /* machine check exceptions don't have ME set */
1440 new_msr
&= ~((target_ulong
)1 << MSR_ME
);
1442 msr
|= env
->error_code
;
1445 case POWERPC_EXCP_DSI
: /* Data storage exception */
1446 trace_ppc_excp_dsi(env
->spr
[SPR_DSISR
], env
->spr
[SPR_DAR
]);
1448 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
1449 trace_ppc_excp_isi(msr
, env
->nip
);
1450 msr
|= env
->error_code
;
1452 case POWERPC_EXCP_EXTERNAL
: /* External input */
1457 * LPES0 is only taken into consideration if we support HV
1458 * mode for this CPU.
1460 if (!env
->has_hv_mode
) {
1464 lpes0
= !!(env
->spr
[SPR_LPCR
] & LPCR_LPES0
);
1467 new_msr
|= (target_ulong
)MSR_HVB
;
1468 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_RI
);
1475 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
1476 /* Optional DSISR update was removed from ISA v3.0 */
1477 if (!(env
->insns_flags2
& PPC2_ISA300
)) {
1478 /* Get rS/rD and rA from faulting opcode */
1480 * Note: the opcode fields will not be set properly for a
1481 * direct store load/store, but nobody cares as nobody
1482 * actually uses direct store segments.
1484 env
->spr
[SPR_DSISR
] |= (env
->error_code
& 0x03FF0000) >> 16;
1487 case POWERPC_EXCP_PROGRAM
: /* Program exception */
1488 switch (env
->error_code
& ~0xF) {
1489 case POWERPC_EXCP_FP
:
1490 if (!FIELD_EX64_FE(env
->msr
) || !FIELD_EX64(env
->msr
, MSR
, FP
)) {
1491 trace_ppc_excp_fp_ignore();
1492 powerpc_reset_excp_state(cpu
);
1497 * FP exceptions always have NIP pointing to the faulting
1498 * instruction, so always use store_next and claim we are
1499 * precise in the MSR.
1503 case POWERPC_EXCP_INVAL
:
1504 trace_ppc_excp_inval(env
->nip
);
1507 case POWERPC_EXCP_PRIV
:
1510 case POWERPC_EXCP_TRAP
:
1514 /* Should never occur */
1515 cpu_abort(cs
, "Invalid program exception %d. Aborting\n",
1520 case POWERPC_EXCP_SYSCALL
: /* System call exception */
1521 lev
= env
->error_code
;
1523 if (lev
== 1 && cpu
->vhyp
) {
1530 * We need to correct the NIP which in this case is supposed
1531 * to point to the next instruction
1535 /* "PAPR mode" built-in hypercall emulation */
1536 if (lev
== 1 && books_vhyp_handles_hcall(cpu
)) {
1537 PPCVirtualHypervisorClass
*vhc
=
1538 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
1539 vhc
->hypercall(cpu
->vhyp
, cpu
);
1540 powerpc_reset_excp_state(cpu
);
1543 if (env
->insns_flags2
& PPC2_ISA310
) {
1544 /* ISAv3.1 puts LEV into SRR1 */
1548 new_msr
|= (target_ulong
)MSR_HVB
;
1551 case POWERPC_EXCP_SYSCALL_VECTORED
: /* scv exception */
1552 lev
= env
->error_code
;
1555 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_EE
);
1556 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_RI
);
1558 vector
+= lev
* 0x20;
1563 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
1564 case POWERPC_EXCP_DECR
: /* Decrementer exception */
1566 case POWERPC_EXCP_RESET
: /* System reset exception */
1567 /* A power-saving exception sets ME, otherwise it is unchanged */
1568 if (FIELD_EX64(env
->msr
, MSR
, POW
)) {
1569 /* indicate that we resumed from power save mode */
1571 new_msr
|= ((target_ulong
)1 << MSR_ME
);
1573 if (env
->msr_mask
& MSR_HVB
) {
1575 * ISA specifies HV, but can be delivered to guest with HV
1576 * clear (e.g., see FWNMI in PAPR, NMI injection in QEMU).
1578 new_msr
|= (target_ulong
)MSR_HVB
;
1580 if (FIELD_EX64(env
->msr
, MSR
, POW
)) {
1581 cpu_abort(cs
, "Trying to deliver power-saving system reset "
1582 "exception %d with no HV support\n", excp
);
1586 case POWERPC_EXCP_TRACE
: /* Trace exception */
1587 msr
|= env
->error_code
;
1589 case POWERPC_EXCP_DSEG
: /* Data segment exception */
1590 case POWERPC_EXCP_ISEG
: /* Instruction segment exception */
1591 case POWERPC_EXCP_SDOOR
: /* Doorbell interrupt */
1592 case POWERPC_EXCP_PERFM
: /* Performance monitor interrupt */
1594 case POWERPC_EXCP_HISI
: /* Hypervisor instruction storage exception */
1595 msr
|= env
->error_code
;
1597 case POWERPC_EXCP_HDECR
: /* Hypervisor decrementer exception */
1598 case POWERPC_EXCP_HDSI
: /* Hypervisor data storage exception */
1599 case POWERPC_EXCP_SDOOR_HV
: /* Hypervisor Doorbell interrupt */
1600 case POWERPC_EXCP_HVIRT
: /* Hypervisor virtualization */
1603 new_msr
|= (target_ulong
)MSR_HVB
;
1604 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_RI
);
1607 case POWERPC_EXCP_HV_EMU
: {
1608 uint32_t insn
= ppc_ldl_code(env
, env
->nip
);
1609 env
->spr
[SPR_HEIR
] = insn
;
1610 if (is_prefix_insn(env
, insn
)) {
1611 uint32_t insn2
= ppc_ldl_code(env
, env
->nip
+ 4);
1612 env
->spr
[SPR_HEIR
] <<= 32;
1613 env
->spr
[SPR_HEIR
] |= insn2
;
1617 new_msr
|= (target_ulong
)MSR_HVB
;
1618 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_RI
);
1622 case POWERPC_EXCP_VPU
: /* Vector unavailable exception */
1623 case POWERPC_EXCP_VSXU
: /* VSX unavailable exception */
1624 case POWERPC_EXCP_FU
: /* Facility unavailable exception */
1625 env
->spr
[SPR_FSCR
] |= ((target_ulong
)env
->error_code
<< 56);
1627 case POWERPC_EXCP_HV_FU
: /* Hypervisor Facility Unavailable Exception */
1628 env
->spr
[SPR_HFSCR
] |= ((target_ulong
)env
->error_code
<< FSCR_IC_POS
);
1631 new_msr
|= (target_ulong
)MSR_HVB
;
1632 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_RI
);
1634 case POWERPC_EXCP_PERFM_EBB
: /* Performance Monitor EBB Exception */
1635 case POWERPC_EXCP_EXTERNAL_EBB
: /* External EBB Exception */
1636 env
->spr
[SPR_BESCR
] &= ~BESCR_GE
;
1639 * Save NIP for rfebb insn in SPR_EBBRR. Next nip is
1640 * stored in the EBB Handler SPR_EBBHR.
1642 env
->spr
[SPR_EBBRR
] = env
->nip
;
1643 powerpc_set_excp_state(cpu
, env
->spr
[SPR_EBBHR
], env
->msr
);
1646 * This exception is handled in userspace. No need to proceed.
1649 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
1650 case POWERPC_EXCP_VPUA
: /* Vector assist exception */
1651 case POWERPC_EXCP_MAINT
: /* Maintenance exception */
1652 case POWERPC_EXCP_HV_MAINT
: /* Hypervisor Maintenance exception */
1653 cpu_abort(cs
, "%s exception not implemented\n",
1654 powerpc_excp_name(excp
));
1657 cpu_abort(cs
, "Invalid PowerPC exception %d. Aborting\n", excp
);
1662 * Sort out endianness of interrupt, this differs depending on the
1663 * CPU, the HV mode, etc...
1665 if (ppc_interrupts_little_endian(cpu
, !!(new_msr
& MSR_HVB
))) {
1666 new_msr
|= (target_ulong
)1 << MSR_LE
;
1669 new_msr
|= (target_ulong
)1 << MSR_SF
;
1671 if (excp
!= POWERPC_EXCP_SYSCALL_VECTORED
) {
1673 env
->spr
[srr0
] = env
->nip
;
1676 env
->spr
[srr1
] = msr
;
1679 if ((new_msr
& MSR_HVB
) && books_vhyp_handles_hv_excp(cpu
)) {
1680 PPCVirtualHypervisorClass
*vhc
=
1681 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
1682 /* Deliver interrupt to L1 by returning from the H_ENTER_NESTED call */
1683 vhc
->deliver_hv_excp(cpu
, excp
);
1685 powerpc_reset_excp_state(cpu
);
1689 if (!(env
->msr_mask
& MSR_HVB
) && srr0
== SPR_HSRR0
) {
1690 cpu_abort(cs
, "Trying to deliver HV exception (HSRR) %d with "
1691 "no HV support\n", excp
);
1694 /* This can update new_msr and vector if AIL applies */
1695 ppc_excp_apply_ail(cpu
, excp
, msr
, &new_msr
, &vector
);
1697 powerpc_set_excp_state(cpu
, vector
, new_msr
);
1701 static inline void powerpc_excp_books(PowerPCCPU
*cpu
, int excp
)
1703 g_assert_not_reached();
1707 static void powerpc_excp(PowerPCCPU
*cpu
, int excp
)
1709 CPUState
*cs
= CPU(cpu
);
1710 CPUPPCState
*env
= &cpu
->env
;
1712 if (excp
<= POWERPC_EXCP_NONE
|| excp
>= POWERPC_EXCP_NB
) {
1713 cpu_abort(cs
, "Invalid PowerPC exception %d. Aborting\n", excp
);
1716 qemu_log_mask(CPU_LOG_INT
, "Raise exception at " TARGET_FMT_lx
1717 " => %s (%d) error=%02x\n", env
->nip
, powerpc_excp_name(excp
),
1718 excp
, env
->error_code
);
1719 env
->excp_stats
[excp
]++;
1721 switch (env
->excp_model
) {
1722 case POWERPC_EXCP_40x
:
1723 powerpc_excp_40x(cpu
, excp
);
1725 case POWERPC_EXCP_6xx
:
1726 powerpc_excp_6xx(cpu
, excp
);
1728 case POWERPC_EXCP_7xx
:
1729 powerpc_excp_7xx(cpu
, excp
);
1731 case POWERPC_EXCP_74xx
:
1732 powerpc_excp_74xx(cpu
, excp
);
1734 case POWERPC_EXCP_BOOKE
:
1735 powerpc_excp_booke(cpu
, excp
);
1737 case POWERPC_EXCP_970
:
1738 case POWERPC_EXCP_POWER7
:
1739 case POWERPC_EXCP_POWER8
:
1740 case POWERPC_EXCP_POWER9
:
1741 case POWERPC_EXCP_POWER10
:
1742 powerpc_excp_books(cpu
, excp
);
1745 g_assert_not_reached();
1749 void ppc_cpu_do_interrupt(CPUState
*cs
)
1751 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
1753 powerpc_excp(cpu
, cs
->exception_index
);
1756 #if defined(TARGET_PPC64)
1757 #define P7_UNUSED_INTERRUPTS \
1758 (PPC_INTERRUPT_RESET | PPC_INTERRUPT_HVIRT | PPC_INTERRUPT_CEXT | \
1759 PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT | \
1760 PPC_INTERRUPT_PIT | PPC_INTERRUPT_DOORBELL | PPC_INTERRUPT_HDOORBELL | \
1761 PPC_INTERRUPT_THERM | PPC_INTERRUPT_EBB)
1763 static int p7_interrupt_powersave(CPUPPCState
*env
)
1765 if ((env
->pending_interrupts
& PPC_INTERRUPT_EXT
) &&
1766 (env
->spr
[SPR_LPCR
] & LPCR_P7_PECE0
)) {
1767 return PPC_INTERRUPT_EXT
;
1769 if ((env
->pending_interrupts
& PPC_INTERRUPT_DECR
) &&
1770 (env
->spr
[SPR_LPCR
] & LPCR_P7_PECE1
)) {
1771 return PPC_INTERRUPT_DECR
;
1773 if ((env
->pending_interrupts
& PPC_INTERRUPT_MCK
) &&
1774 (env
->spr
[SPR_LPCR
] & LPCR_P7_PECE2
)) {
1775 return PPC_INTERRUPT_MCK
;
1777 if ((env
->pending_interrupts
& PPC_INTERRUPT_HMI
) &&
1778 (env
->spr
[SPR_LPCR
] & LPCR_P7_PECE2
)) {
1779 return PPC_INTERRUPT_HMI
;
1781 if (env
->pending_interrupts
& PPC_INTERRUPT_RESET
) {
1782 return PPC_INTERRUPT_RESET
;
1787 static int p7_next_unmasked_interrupt(CPUPPCState
*env
)
1789 CPUState
*cs
= env_cpu(env
);
1791 /* Ignore MSR[EE] when coming out of some power management states */
1792 bool msr_ee
= FIELD_EX64(env
->msr
, MSR
, EE
) || env
->resume_as_sreset
;
1794 assert((env
->pending_interrupts
& P7_UNUSED_INTERRUPTS
) == 0);
1797 /* LPCR[PECE] controls which interrupts can exit power-saving mode */
1798 return p7_interrupt_powersave(env
);
1801 /* Machine check exception */
1802 if (env
->pending_interrupts
& PPC_INTERRUPT_MCK
) {
1803 return PPC_INTERRUPT_MCK
;
1806 /* Hypervisor decrementer exception */
1807 if (env
->pending_interrupts
& PPC_INTERRUPT_HDECR
) {
1808 /* LPCR will be clear when not supported so this will work */
1809 bool hdice
= !!(env
->spr
[SPR_LPCR
] & LPCR_HDICE
);
1810 if ((msr_ee
|| !FIELD_EX64_HV(env
->msr
)) && hdice
) {
1811 /* HDEC clears on delivery */
1812 return PPC_INTERRUPT_HDECR
;
1816 /* External interrupt can ignore MSR:EE under some circumstances */
1817 if (env
->pending_interrupts
& PPC_INTERRUPT_EXT
) {
1818 bool lpes0
= !!(env
->spr
[SPR_LPCR
] & LPCR_LPES0
);
1819 bool heic
= !!(env
->spr
[SPR_LPCR
] & LPCR_HEIC
);
1820 /* HEIC blocks delivery to the hypervisor */
1821 if ((msr_ee
&& !(heic
&& FIELD_EX64_HV(env
->msr
) &&
1822 !FIELD_EX64(env
->msr
, MSR
, PR
))) ||
1823 (env
->has_hv_mode
&& !FIELD_EX64_HV(env
->msr
) && !lpes0
)) {
1824 return PPC_INTERRUPT_EXT
;
1828 /* Decrementer exception */
1829 if (env
->pending_interrupts
& PPC_INTERRUPT_DECR
) {
1830 return PPC_INTERRUPT_DECR
;
1832 if (env
->pending_interrupts
& PPC_INTERRUPT_PERFM
) {
1833 return PPC_INTERRUPT_PERFM
;
1840 #define P8_UNUSED_INTERRUPTS \
1841 (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_HVIRT | \
1842 PPC_INTERRUPT_CEXT | PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | \
1843 PPC_INTERRUPT_FIT | PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1845 static int p8_interrupt_powersave(CPUPPCState
*env
)
1847 if ((env
->pending_interrupts
& PPC_INTERRUPT_EXT
) &&
1848 (env
->spr
[SPR_LPCR
] & LPCR_P8_PECE2
)) {
1849 return PPC_INTERRUPT_EXT
;
1851 if ((env
->pending_interrupts
& PPC_INTERRUPT_DECR
) &&
1852 (env
->spr
[SPR_LPCR
] & LPCR_P8_PECE3
)) {
1853 return PPC_INTERRUPT_DECR
;
1855 if ((env
->pending_interrupts
& PPC_INTERRUPT_MCK
) &&
1856 (env
->spr
[SPR_LPCR
] & LPCR_P8_PECE4
)) {
1857 return PPC_INTERRUPT_MCK
;
1859 if ((env
->pending_interrupts
& PPC_INTERRUPT_HMI
) &&
1860 (env
->spr
[SPR_LPCR
] & LPCR_P8_PECE4
)) {
1861 return PPC_INTERRUPT_HMI
;
1863 if ((env
->pending_interrupts
& PPC_INTERRUPT_DOORBELL
) &&
1864 (env
->spr
[SPR_LPCR
] & LPCR_P8_PECE0
)) {
1865 return PPC_INTERRUPT_DOORBELL
;
1867 if ((env
->pending_interrupts
& PPC_INTERRUPT_HDOORBELL
) &&
1868 (env
->spr
[SPR_LPCR
] & LPCR_P8_PECE1
)) {
1869 return PPC_INTERRUPT_HDOORBELL
;
1871 if (env
->pending_interrupts
& PPC_INTERRUPT_RESET
) {
1872 return PPC_INTERRUPT_RESET
;
1877 static int p8_next_unmasked_interrupt(CPUPPCState
*env
)
1879 CPUState
*cs
= env_cpu(env
);
1881 /* Ignore MSR[EE] when coming out of some power management states */
1882 bool msr_ee
= FIELD_EX64(env
->msr
, MSR
, EE
) || env
->resume_as_sreset
;
1884 assert((env
->pending_interrupts
& P8_UNUSED_INTERRUPTS
) == 0);
1887 /* LPCR[PECE] controls which interrupts can exit power-saving mode */
1888 return p8_interrupt_powersave(env
);
1891 /* Machine check exception */
1892 if (env
->pending_interrupts
& PPC_INTERRUPT_MCK
) {
1893 return PPC_INTERRUPT_MCK
;
1896 /* Hypervisor decrementer exception */
1897 if (env
->pending_interrupts
& PPC_INTERRUPT_HDECR
) {
1898 /* LPCR will be clear when not supported so this will work */
1899 bool hdice
= !!(env
->spr
[SPR_LPCR
] & LPCR_HDICE
);
1900 if ((msr_ee
|| !FIELD_EX64_HV(env
->msr
)) && hdice
) {
1901 /* HDEC clears on delivery */
1902 return PPC_INTERRUPT_HDECR
;
1906 /* External interrupt can ignore MSR:EE under some circumstances */
1907 if (env
->pending_interrupts
& PPC_INTERRUPT_EXT
) {
1908 bool lpes0
= !!(env
->spr
[SPR_LPCR
] & LPCR_LPES0
);
1909 bool heic
= !!(env
->spr
[SPR_LPCR
] & LPCR_HEIC
);
1910 /* HEIC blocks delivery to the hypervisor */
1911 if ((msr_ee
&& !(heic
&& FIELD_EX64_HV(env
->msr
) &&
1912 !FIELD_EX64(env
->msr
, MSR
, PR
))) ||
1913 (env
->has_hv_mode
&& !FIELD_EX64_HV(env
->msr
) && !lpes0
)) {
1914 return PPC_INTERRUPT_EXT
;
1918 /* Decrementer exception */
1919 if (env
->pending_interrupts
& PPC_INTERRUPT_DECR
) {
1920 return PPC_INTERRUPT_DECR
;
1922 if (env
->pending_interrupts
& PPC_INTERRUPT_DOORBELL
) {
1923 return PPC_INTERRUPT_DOORBELL
;
1925 if (env
->pending_interrupts
& PPC_INTERRUPT_HDOORBELL
) {
1926 return PPC_INTERRUPT_HDOORBELL
;
1928 if (env
->pending_interrupts
& PPC_INTERRUPT_PERFM
) {
1929 return PPC_INTERRUPT_PERFM
;
1932 if (env
->pending_interrupts
& PPC_INTERRUPT_EBB
) {
1934 * EBB exception must be taken in problem state and
1935 * with BESCR_GE set.
1937 if (FIELD_EX64(env
->msr
, MSR
, PR
) &&
1938 (env
->spr
[SPR_BESCR
] & BESCR_GE
)) {
1939 return PPC_INTERRUPT_EBB
;
1947 #define P9_UNUSED_INTERRUPTS \
1948 (PPC_INTERRUPT_RESET | PPC_INTERRUPT_DEBUG | PPC_INTERRUPT_CEXT | \
1949 PPC_INTERRUPT_WDT | PPC_INTERRUPT_CDOORBELL | PPC_INTERRUPT_FIT | \
1950 PPC_INTERRUPT_PIT | PPC_INTERRUPT_THERM)
1952 static int p9_interrupt_powersave(CPUPPCState
*env
)
1954 /* External Exception */
1955 if ((env
->pending_interrupts
& PPC_INTERRUPT_EXT
) &&
1956 (env
->spr
[SPR_LPCR
] & LPCR_EEE
)) {
1957 bool heic
= !!(env
->spr
[SPR_LPCR
] & LPCR_HEIC
);
1958 if (!heic
|| !FIELD_EX64_HV(env
->msr
) ||
1959 FIELD_EX64(env
->msr
, MSR
, PR
)) {
1960 return PPC_INTERRUPT_EXT
;
1963 /* Decrementer Exception */
1964 if ((env
->pending_interrupts
& PPC_INTERRUPT_DECR
) &&
1965 (env
->spr
[SPR_LPCR
] & LPCR_DEE
)) {
1966 return PPC_INTERRUPT_DECR
;
1968 /* Machine Check or Hypervisor Maintenance Exception */
1969 if (env
->spr
[SPR_LPCR
] & LPCR_OEE
) {
1970 if (env
->pending_interrupts
& PPC_INTERRUPT_MCK
) {
1971 return PPC_INTERRUPT_MCK
;
1973 if (env
->pending_interrupts
& PPC_INTERRUPT_HMI
) {
1974 return PPC_INTERRUPT_HMI
;
1977 /* Privileged Doorbell Exception */
1978 if ((env
->pending_interrupts
& PPC_INTERRUPT_DOORBELL
) &&
1979 (env
->spr
[SPR_LPCR
] & LPCR_PDEE
)) {
1980 return PPC_INTERRUPT_DOORBELL
;
1982 /* Hypervisor Doorbell Exception */
1983 if ((env
->pending_interrupts
& PPC_INTERRUPT_HDOORBELL
) &&
1984 (env
->spr
[SPR_LPCR
] & LPCR_HDEE
)) {
1985 return PPC_INTERRUPT_HDOORBELL
;
1987 /* Hypervisor virtualization exception */
1988 if ((env
->pending_interrupts
& PPC_INTERRUPT_HVIRT
) &&
1989 (env
->spr
[SPR_LPCR
] & LPCR_HVEE
)) {
1990 return PPC_INTERRUPT_HVIRT
;
1992 if (env
->pending_interrupts
& PPC_INTERRUPT_RESET
) {
1993 return PPC_INTERRUPT_RESET
;
1998 static int p9_next_unmasked_interrupt(CPUPPCState
*env
)
2000 CPUState
*cs
= env_cpu(env
);
2002 /* Ignore MSR[EE] when coming out of some power management states */
2003 bool msr_ee
= FIELD_EX64(env
->msr
, MSR
, EE
) || env
->resume_as_sreset
;
2005 assert((env
->pending_interrupts
& P9_UNUSED_INTERRUPTS
) == 0);
2008 if (env
->spr
[SPR_PSSCR
] & PSSCR_EC
) {
2010 * When PSSCR[EC] is set, LPCR[PECE] controls which interrupts can
2011 * wakeup the processor
2013 return p9_interrupt_powersave(env
);
2016 * When it's clear, any system-caused exception exits power-saving
2017 * mode, even the ones that gate on MSR[EE].
2023 /* Machine check exception */
2024 if (env
->pending_interrupts
& PPC_INTERRUPT_MCK
) {
2025 return PPC_INTERRUPT_MCK
;
2028 /* Hypervisor decrementer exception */
2029 if (env
->pending_interrupts
& PPC_INTERRUPT_HDECR
) {
2030 /* LPCR will be clear when not supported so this will work */
2031 bool hdice
= !!(env
->spr
[SPR_LPCR
] & LPCR_HDICE
);
2032 if ((msr_ee
|| !FIELD_EX64_HV(env
->msr
)) && hdice
) {
2033 /* HDEC clears on delivery */
2034 return PPC_INTERRUPT_HDECR
;
2038 /* Hypervisor virtualization interrupt */
2039 if (env
->pending_interrupts
& PPC_INTERRUPT_HVIRT
) {
2040 /* LPCR will be clear when not supported so this will work */
2041 bool hvice
= !!(env
->spr
[SPR_LPCR
] & LPCR_HVICE
);
2042 if ((msr_ee
|| !FIELD_EX64_HV(env
->msr
)) && hvice
) {
2043 return PPC_INTERRUPT_HVIRT
;
2047 /* External interrupt can ignore MSR:EE under some circumstances */
2048 if (env
->pending_interrupts
& PPC_INTERRUPT_EXT
) {
2049 bool lpes0
= !!(env
->spr
[SPR_LPCR
] & LPCR_LPES0
);
2050 bool heic
= !!(env
->spr
[SPR_LPCR
] & LPCR_HEIC
);
2051 /* HEIC blocks delivery to the hypervisor */
2052 if ((msr_ee
&& !(heic
&& FIELD_EX64_HV(env
->msr
) &&
2053 !FIELD_EX64(env
->msr
, MSR
, PR
))) ||
2054 (env
->has_hv_mode
&& !FIELD_EX64_HV(env
->msr
) && !lpes0
)) {
2055 return PPC_INTERRUPT_EXT
;
2059 /* Decrementer exception */
2060 if (env
->pending_interrupts
& PPC_INTERRUPT_DECR
) {
2061 return PPC_INTERRUPT_DECR
;
2063 if (env
->pending_interrupts
& PPC_INTERRUPT_DOORBELL
) {
2064 return PPC_INTERRUPT_DOORBELL
;
2066 if (env
->pending_interrupts
& PPC_INTERRUPT_HDOORBELL
) {
2067 return PPC_INTERRUPT_HDOORBELL
;
2069 if (env
->pending_interrupts
& PPC_INTERRUPT_PERFM
) {
2070 return PPC_INTERRUPT_PERFM
;
2073 if (env
->pending_interrupts
& PPC_INTERRUPT_EBB
) {
2075 * EBB exception must be taken in problem state and
2076 * with BESCR_GE set.
2078 if (FIELD_EX64(env
->msr
, MSR
, PR
) &&
2079 (env
->spr
[SPR_BESCR
] & BESCR_GE
)) {
2080 return PPC_INTERRUPT_EBB
;
2089 static int ppc_next_unmasked_interrupt_generic(CPUPPCState
*env
)
2093 /* External reset */
2094 if (env
->pending_interrupts
& PPC_INTERRUPT_RESET
) {
2095 return PPC_INTERRUPT_RESET
;
2097 /* Machine check exception */
2098 if (env
->pending_interrupts
& PPC_INTERRUPT_MCK
) {
2099 return PPC_INTERRUPT_MCK
;
2102 /* External debug exception */
2103 if (env
->pending_interrupts
& PPC_INTERRUPT_DEBUG
) {
2104 return PPC_INTERRUPT_DEBUG
;
2109 * For interrupts that gate on MSR:EE, we need to do something a
2110 * bit more subtle, as we need to let them through even when EE is
2111 * clear when coming out of some power management states (in order
2112 * for them to become a 0x100).
2114 async_deliver
= FIELD_EX64(env
->msr
, MSR
, EE
) || env
->resume_as_sreset
;
2116 /* Hypervisor decrementer exception */
2117 if (env
->pending_interrupts
& PPC_INTERRUPT_HDECR
) {
2118 /* LPCR will be clear when not supported so this will work */
2119 bool hdice
= !!(env
->spr
[SPR_LPCR
] & LPCR_HDICE
);
2120 if ((async_deliver
|| !FIELD_EX64_HV(env
->msr
)) && hdice
) {
2121 /* HDEC clears on delivery */
2122 return PPC_INTERRUPT_HDECR
;
2126 /* Hypervisor virtualization interrupt */
2127 if (env
->pending_interrupts
& PPC_INTERRUPT_HVIRT
) {
2128 /* LPCR will be clear when not supported so this will work */
2129 bool hvice
= !!(env
->spr
[SPR_LPCR
] & LPCR_HVICE
);
2130 if ((async_deliver
|| !FIELD_EX64_HV(env
->msr
)) && hvice
) {
2131 return PPC_INTERRUPT_HVIRT
;
2135 /* External interrupt can ignore MSR:EE under some circumstances */
2136 if (env
->pending_interrupts
& PPC_INTERRUPT_EXT
) {
2137 bool lpes0
= !!(env
->spr
[SPR_LPCR
] & LPCR_LPES0
);
2138 bool heic
= !!(env
->spr
[SPR_LPCR
] & LPCR_HEIC
);
2139 /* HEIC blocks delivery to the hypervisor */
2140 if ((async_deliver
&& !(heic
&& FIELD_EX64_HV(env
->msr
) &&
2141 !FIELD_EX64(env
->msr
, MSR
, PR
))) ||
2142 (env
->has_hv_mode
&& !FIELD_EX64_HV(env
->msr
) && !lpes0
)) {
2143 return PPC_INTERRUPT_EXT
;
2146 if (FIELD_EX64(env
->msr
, MSR
, CE
)) {
2147 /* External critical interrupt */
2148 if (env
->pending_interrupts
& PPC_INTERRUPT_CEXT
) {
2149 return PPC_INTERRUPT_CEXT
;
2152 if (async_deliver
!= 0) {
2153 /* Watchdog timer on embedded PowerPC */
2154 if (env
->pending_interrupts
& PPC_INTERRUPT_WDT
) {
2155 return PPC_INTERRUPT_WDT
;
2157 if (env
->pending_interrupts
& PPC_INTERRUPT_CDOORBELL
) {
2158 return PPC_INTERRUPT_CDOORBELL
;
2160 /* Fixed interval timer on embedded PowerPC */
2161 if (env
->pending_interrupts
& PPC_INTERRUPT_FIT
) {
2162 return PPC_INTERRUPT_FIT
;
2164 /* Programmable interval timer on embedded PowerPC */
2165 if (env
->pending_interrupts
& PPC_INTERRUPT_PIT
) {
2166 return PPC_INTERRUPT_PIT
;
2168 /* Decrementer exception */
2169 if (env
->pending_interrupts
& PPC_INTERRUPT_DECR
) {
2170 return PPC_INTERRUPT_DECR
;
2172 if (env
->pending_interrupts
& PPC_INTERRUPT_DOORBELL
) {
2173 return PPC_INTERRUPT_DOORBELL
;
2175 if (env
->pending_interrupts
& PPC_INTERRUPT_HDOORBELL
) {
2176 return PPC_INTERRUPT_HDOORBELL
;
2178 if (env
->pending_interrupts
& PPC_INTERRUPT_PERFM
) {
2179 return PPC_INTERRUPT_PERFM
;
2181 /* Thermal interrupt */
2182 if (env
->pending_interrupts
& PPC_INTERRUPT_THERM
) {
2183 return PPC_INTERRUPT_THERM
;
2186 if (env
->pending_interrupts
& PPC_INTERRUPT_EBB
) {
2188 * EBB exception must be taken in problem state and
2189 * with BESCR_GE set.
2191 if (FIELD_EX64(env
->msr
, MSR
, PR
) &&
2192 (env
->spr
[SPR_BESCR
] & BESCR_GE
)) {
2193 return PPC_INTERRUPT_EBB
;
2201 static int ppc_next_unmasked_interrupt(CPUPPCState
*env
)
2203 switch (env
->excp_model
) {
2204 #if defined(TARGET_PPC64)
2205 case POWERPC_EXCP_POWER7
:
2206 return p7_next_unmasked_interrupt(env
);
2207 case POWERPC_EXCP_POWER8
:
2208 return p8_next_unmasked_interrupt(env
);
2209 case POWERPC_EXCP_POWER9
:
2210 case POWERPC_EXCP_POWER10
:
2211 return p9_next_unmasked_interrupt(env
);
2214 return ppc_next_unmasked_interrupt_generic(env
);
2219 * Sets CPU_INTERRUPT_HARD if there is at least one unmasked interrupt to be
2220 * delivered and clears CPU_INTERRUPT_HARD otherwise.
2222 * This method is called by ppc_set_interrupt when an interrupt is raised or
2223 * lowered, and should also be called whenever an interrupt masking condition
2225 * - When relevant bits of MSR are altered, like EE, HV, PR, etc.;
2226 * - When relevant bits of LPCR are altered, like PECE, HDICE, HVICE, etc.;
2227 * - When PSSCR[EC] or env->resume_as_sreset are changed;
2228 * - When cs->halted is changed and the CPU has a different interrupt masking
2229 * logic in power-saving mode (e.g., POWER7/8/9/10);
2231 void ppc_maybe_interrupt(CPUPPCState
*env
)
2233 CPUState
*cs
= env_cpu(env
);
2236 if (ppc_next_unmasked_interrupt(env
)) {
2237 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
2239 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
2243 #if defined(TARGET_PPC64)
2244 static void p7_deliver_interrupt(CPUPPCState
*env
, int interrupt
)
2246 PowerPCCPU
*cpu
= env_archcpu(env
);
2247 CPUState
*cs
= env_cpu(env
);
2249 switch (interrupt
) {
2250 case PPC_INTERRUPT_MCK
: /* Machine check exception */
2251 env
->pending_interrupts
&= ~PPC_INTERRUPT_MCK
;
2252 powerpc_excp(cpu
, POWERPC_EXCP_MCHECK
);
2255 case PPC_INTERRUPT_HDECR
: /* Hypervisor decrementer exception */
2256 /* HDEC clears on delivery */
2257 env
->pending_interrupts
&= ~PPC_INTERRUPT_HDECR
;
2258 powerpc_excp(cpu
, POWERPC_EXCP_HDECR
);
2261 case PPC_INTERRUPT_EXT
:
2262 if (books_vhyp_promotes_external_to_hvirt(cpu
)) {
2263 powerpc_excp(cpu
, POWERPC_EXCP_HVIRT
);
2265 powerpc_excp(cpu
, POWERPC_EXCP_EXTERNAL
);
2269 case PPC_INTERRUPT_DECR
: /* Decrementer exception */
2270 powerpc_excp(cpu
, POWERPC_EXCP_DECR
);
2272 case PPC_INTERRUPT_PERFM
:
2273 env
->pending_interrupts
&= ~PPC_INTERRUPT_PERFM
;
2274 powerpc_excp(cpu
, POWERPC_EXCP_PERFM
);
2278 * This is a bug ! It means that has_work took us out of halt without
2279 * anything to deliver while in a PM state that requires getting
2282 * This means we will incorrectly execute past the power management
2283 * instruction instead of triggering a reset.
2285 * It generally means a discrepancy between the wakeup conditions in the
2286 * processor has_work implementation and the logic in this function.
2288 assert(!env
->resume_as_sreset
);
2291 cpu_abort(cs
, "Invalid PowerPC interrupt %d. Aborting\n", interrupt
);
2295 static void p8_deliver_interrupt(CPUPPCState
*env
, int interrupt
)
2297 PowerPCCPU
*cpu
= env_archcpu(env
);
2298 CPUState
*cs
= env_cpu(env
);
2300 switch (interrupt
) {
2301 case PPC_INTERRUPT_MCK
: /* Machine check exception */
2302 env
->pending_interrupts
&= ~PPC_INTERRUPT_MCK
;
2303 powerpc_excp(cpu
, POWERPC_EXCP_MCHECK
);
2306 case PPC_INTERRUPT_HDECR
: /* Hypervisor decrementer exception */
2307 /* HDEC clears on delivery */
2308 env
->pending_interrupts
&= ~PPC_INTERRUPT_HDECR
;
2309 powerpc_excp(cpu
, POWERPC_EXCP_HDECR
);
2312 case PPC_INTERRUPT_EXT
:
2313 if (books_vhyp_promotes_external_to_hvirt(cpu
)) {
2314 powerpc_excp(cpu
, POWERPC_EXCP_HVIRT
);
2316 powerpc_excp(cpu
, POWERPC_EXCP_EXTERNAL
);
2320 case PPC_INTERRUPT_DECR
: /* Decrementer exception */
2321 powerpc_excp(cpu
, POWERPC_EXCP_DECR
);
2323 case PPC_INTERRUPT_DOORBELL
:
2324 env
->pending_interrupts
&= ~PPC_INTERRUPT_DOORBELL
;
2325 if (is_book3s_arch2x(env
)) {
2326 powerpc_excp(cpu
, POWERPC_EXCP_SDOOR
);
2328 powerpc_excp(cpu
, POWERPC_EXCP_DOORI
);
2331 case PPC_INTERRUPT_HDOORBELL
:
2332 env
->pending_interrupts
&= ~PPC_INTERRUPT_HDOORBELL
;
2333 powerpc_excp(cpu
, POWERPC_EXCP_SDOOR_HV
);
2335 case PPC_INTERRUPT_PERFM
:
2336 env
->pending_interrupts
&= ~PPC_INTERRUPT_PERFM
;
2337 powerpc_excp(cpu
, POWERPC_EXCP_PERFM
);
2339 case PPC_INTERRUPT_EBB
: /* EBB exception */
2340 env
->pending_interrupts
&= ~PPC_INTERRUPT_EBB
;
2341 if (env
->spr
[SPR_BESCR
] & BESCR_PMEO
) {
2342 powerpc_excp(cpu
, POWERPC_EXCP_PERFM_EBB
);
2343 } else if (env
->spr
[SPR_BESCR
] & BESCR_EEO
) {
2344 powerpc_excp(cpu
, POWERPC_EXCP_EXTERNAL_EBB
);
2349 * This is a bug ! It means that has_work took us out of halt without
2350 * anything to deliver while in a PM state that requires getting
2353 * This means we will incorrectly execute past the power management
2354 * instruction instead of triggering a reset.
2356 * It generally means a discrepancy between the wakeup conditions in the
2357 * processor has_work implementation and the logic in this function.
2359 assert(!env
->resume_as_sreset
);
2362 cpu_abort(cs
, "Invalid PowerPC interrupt %d. Aborting\n", interrupt
);
2366 static void p9_deliver_interrupt(CPUPPCState
*env
, int interrupt
)
2368 PowerPCCPU
*cpu
= env_archcpu(env
);
2369 CPUState
*cs
= env_cpu(env
);
2371 if (cs
->halted
&& !(env
->spr
[SPR_PSSCR
] & PSSCR_EC
) &&
2372 !FIELD_EX64(env
->msr
, MSR
, EE
)) {
2374 * A pending interrupt took us out of power-saving, but MSR[EE] says
2375 * that we should return to NIP+4 instead of delivering it.
2380 switch (interrupt
) {
2381 case PPC_INTERRUPT_MCK
: /* Machine check exception */
2382 env
->pending_interrupts
&= ~PPC_INTERRUPT_MCK
;
2383 powerpc_excp(cpu
, POWERPC_EXCP_MCHECK
);
2386 case PPC_INTERRUPT_HDECR
: /* Hypervisor decrementer exception */
2387 /* HDEC clears on delivery */
2388 env
->pending_interrupts
&= ~PPC_INTERRUPT_HDECR
;
2389 powerpc_excp(cpu
, POWERPC_EXCP_HDECR
);
2391 case PPC_INTERRUPT_HVIRT
: /* Hypervisor virtualization interrupt */
2392 powerpc_excp(cpu
, POWERPC_EXCP_HVIRT
);
2395 case PPC_INTERRUPT_EXT
:
2396 if (books_vhyp_promotes_external_to_hvirt(cpu
)) {
2397 powerpc_excp(cpu
, POWERPC_EXCP_HVIRT
);
2399 powerpc_excp(cpu
, POWERPC_EXCP_EXTERNAL
);
2403 case PPC_INTERRUPT_DECR
: /* Decrementer exception */
2404 powerpc_excp(cpu
, POWERPC_EXCP_DECR
);
2406 case PPC_INTERRUPT_DOORBELL
:
2407 env
->pending_interrupts
&= ~PPC_INTERRUPT_DOORBELL
;
2408 powerpc_excp(cpu
, POWERPC_EXCP_SDOOR
);
2410 case PPC_INTERRUPT_HDOORBELL
:
2411 env
->pending_interrupts
&= ~PPC_INTERRUPT_HDOORBELL
;
2412 powerpc_excp(cpu
, POWERPC_EXCP_SDOOR_HV
);
2414 case PPC_INTERRUPT_PERFM
:
2415 env
->pending_interrupts
&= ~PPC_INTERRUPT_PERFM
;
2416 powerpc_excp(cpu
, POWERPC_EXCP_PERFM
);
2418 case PPC_INTERRUPT_EBB
: /* EBB exception */
2419 env
->pending_interrupts
&= ~PPC_INTERRUPT_EBB
;
2420 if (env
->spr
[SPR_BESCR
] & BESCR_PMEO
) {
2421 powerpc_excp(cpu
, POWERPC_EXCP_PERFM_EBB
);
2422 } else if (env
->spr
[SPR_BESCR
] & BESCR_EEO
) {
2423 powerpc_excp(cpu
, POWERPC_EXCP_EXTERNAL_EBB
);
2428 * This is a bug ! It means that has_work took us out of halt without
2429 * anything to deliver while in a PM state that requires getting
2432 * This means we will incorrectly execute past the power management
2433 * instruction instead of triggering a reset.
2435 * It generally means a discrepancy between the wakeup conditions in the
2436 * processor has_work implementation and the logic in this function.
2438 assert(!env
->resume_as_sreset
);
2441 cpu_abort(cs
, "Invalid PowerPC interrupt %d. Aborting\n", interrupt
);
2446 static void ppc_deliver_interrupt_generic(CPUPPCState
*env
, int interrupt
)
2448 PowerPCCPU
*cpu
= env_archcpu(env
);
2449 CPUState
*cs
= env_cpu(env
);
2451 switch (interrupt
) {
2452 case PPC_INTERRUPT_RESET
: /* External reset */
2453 env
->pending_interrupts
&= ~PPC_INTERRUPT_RESET
;
2454 powerpc_excp(cpu
, POWERPC_EXCP_RESET
);
2456 case PPC_INTERRUPT_MCK
: /* Machine check exception */
2457 env
->pending_interrupts
&= ~PPC_INTERRUPT_MCK
;
2458 powerpc_excp(cpu
, POWERPC_EXCP_MCHECK
);
2461 case PPC_INTERRUPT_HDECR
: /* Hypervisor decrementer exception */
2462 /* HDEC clears on delivery */
2463 env
->pending_interrupts
&= ~PPC_INTERRUPT_HDECR
;
2464 powerpc_excp(cpu
, POWERPC_EXCP_HDECR
);
2466 case PPC_INTERRUPT_HVIRT
: /* Hypervisor virtualization interrupt */
2467 powerpc_excp(cpu
, POWERPC_EXCP_HVIRT
);
2470 case PPC_INTERRUPT_EXT
:
2471 if (books_vhyp_promotes_external_to_hvirt(cpu
)) {
2472 powerpc_excp(cpu
, POWERPC_EXCP_HVIRT
);
2474 powerpc_excp(cpu
, POWERPC_EXCP_EXTERNAL
);
2477 case PPC_INTERRUPT_CEXT
: /* External critical interrupt */
2478 powerpc_excp(cpu
, POWERPC_EXCP_CRITICAL
);
2481 case PPC_INTERRUPT_WDT
: /* Watchdog timer on embedded PowerPC */
2482 env
->pending_interrupts
&= ~PPC_INTERRUPT_WDT
;
2483 powerpc_excp(cpu
, POWERPC_EXCP_WDT
);
2485 case PPC_INTERRUPT_CDOORBELL
:
2486 env
->pending_interrupts
&= ~PPC_INTERRUPT_CDOORBELL
;
2487 powerpc_excp(cpu
, POWERPC_EXCP_DOORCI
);
2489 case PPC_INTERRUPT_FIT
: /* Fixed interval timer on embedded PowerPC */
2490 env
->pending_interrupts
&= ~PPC_INTERRUPT_FIT
;
2491 powerpc_excp(cpu
, POWERPC_EXCP_FIT
);
2493 case PPC_INTERRUPT_PIT
: /* Programmable interval timer on embedded ppc */
2494 env
->pending_interrupts
&= ~PPC_INTERRUPT_PIT
;
2495 powerpc_excp(cpu
, POWERPC_EXCP_PIT
);
2497 case PPC_INTERRUPT_DECR
: /* Decrementer exception */
2498 if (ppc_decr_clear_on_delivery(env
)) {
2499 env
->pending_interrupts
&= ~PPC_INTERRUPT_DECR
;
2501 powerpc_excp(cpu
, POWERPC_EXCP_DECR
);
2503 case PPC_INTERRUPT_DOORBELL
:
2504 env
->pending_interrupts
&= ~PPC_INTERRUPT_DOORBELL
;
2505 if (is_book3s_arch2x(env
)) {
2506 powerpc_excp(cpu
, POWERPC_EXCP_SDOOR
);
2508 powerpc_excp(cpu
, POWERPC_EXCP_DOORI
);
2511 case PPC_INTERRUPT_HDOORBELL
:
2512 env
->pending_interrupts
&= ~PPC_INTERRUPT_HDOORBELL
;
2513 powerpc_excp(cpu
, POWERPC_EXCP_SDOOR_HV
);
2515 case PPC_INTERRUPT_PERFM
:
2516 env
->pending_interrupts
&= ~PPC_INTERRUPT_PERFM
;
2517 powerpc_excp(cpu
, POWERPC_EXCP_PERFM
);
2519 case PPC_INTERRUPT_THERM
: /* Thermal interrupt */
2520 env
->pending_interrupts
&= ~PPC_INTERRUPT_THERM
;
2521 powerpc_excp(cpu
, POWERPC_EXCP_THERM
);
2523 case PPC_INTERRUPT_EBB
: /* EBB exception */
2524 env
->pending_interrupts
&= ~PPC_INTERRUPT_EBB
;
2525 if (env
->spr
[SPR_BESCR
] & BESCR_PMEO
) {
2526 powerpc_excp(cpu
, POWERPC_EXCP_PERFM_EBB
);
2527 } else if (env
->spr
[SPR_BESCR
] & BESCR_EEO
) {
2528 powerpc_excp(cpu
, POWERPC_EXCP_EXTERNAL_EBB
);
2533 * This is a bug ! It means that has_work took us out of halt without
2534 * anything to deliver while in a PM state that requires getting
2537 * This means we will incorrectly execute past the power management
2538 * instruction instead of triggering a reset.
2540 * It generally means a discrepancy between the wakeup conditions in the
2541 * processor has_work implementation and the logic in this function.
2543 assert(!env
->resume_as_sreset
);
2546 cpu_abort(cs
, "Invalid PowerPC interrupt %d. Aborting\n", interrupt
);
2550 static void ppc_deliver_interrupt(CPUPPCState
*env
, int interrupt
)
2552 switch (env
->excp_model
) {
2553 #if defined(TARGET_PPC64)
2554 case POWERPC_EXCP_POWER7
:
2555 p7_deliver_interrupt(env
, interrupt
);
2557 case POWERPC_EXCP_POWER8
:
2558 p8_deliver_interrupt(env
, interrupt
);
2560 case POWERPC_EXCP_POWER9
:
2561 case POWERPC_EXCP_POWER10
:
2562 p9_deliver_interrupt(env
, interrupt
);
2566 ppc_deliver_interrupt_generic(env
, interrupt
);
2570 void ppc_cpu_do_system_reset(CPUState
*cs
)
2572 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
2574 powerpc_excp(cpu
, POWERPC_EXCP_RESET
);
2577 void ppc_cpu_do_fwnmi_machine_check(CPUState
*cs
, target_ulong vector
)
2579 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
2580 CPUPPCState
*env
= &cpu
->env
;
2581 target_ulong msr
= 0;
2584 * Set MSR and NIP for the handler, SRR0/1, DAR and DSISR have already
2587 msr
= (1ULL << MSR_ME
);
2588 msr
|= env
->msr
& (1ULL << MSR_SF
);
2589 if (ppc_interrupts_little_endian(cpu
, false)) {
2590 msr
|= (1ULL << MSR_LE
);
2593 /* Anything for nested required here? MSR[HV] bit? */
2595 powerpc_set_excp_state(cpu
, vector
, msr
);
2598 bool ppc_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
2600 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
2601 CPUPPCState
*env
= &cpu
->env
;
2604 if ((interrupt_request
& CPU_INTERRUPT_HARD
) == 0) {
2608 interrupt
= ppc_next_unmasked_interrupt(env
);
2609 if (interrupt
== 0) {
2613 ppc_deliver_interrupt(env
, interrupt
);
2614 if (env
->pending_interrupts
== 0) {
2615 cpu_reset_interrupt(cs
, CPU_INTERRUPT_HARD
);
2620 #endif /* !CONFIG_USER_ONLY */
2622 /*****************************************************************************/
2623 /* Exceptions processing helpers */
2625 void raise_exception_err_ra(CPUPPCState
*env
, uint32_t exception
,
2626 uint32_t error_code
, uintptr_t raddr
)
2628 CPUState
*cs
= env_cpu(env
);
2630 cs
->exception_index
= exception
;
2631 env
->error_code
= error_code
;
2632 cpu_loop_exit_restore(cs
, raddr
);
2635 void raise_exception_err(CPUPPCState
*env
, uint32_t exception
,
2636 uint32_t error_code
)
2638 raise_exception_err_ra(env
, exception
, error_code
, 0);
2641 void raise_exception(CPUPPCState
*env
, uint32_t exception
)
2643 raise_exception_err_ra(env
, exception
, 0, 0);
2646 void raise_exception_ra(CPUPPCState
*env
, uint32_t exception
,
2649 raise_exception_err_ra(env
, exception
, 0, raddr
);
2653 void helper_raise_exception_err(CPUPPCState
*env
, uint32_t exception
,
2654 uint32_t error_code
)
2656 raise_exception_err_ra(env
, exception
, error_code
, 0);
2659 void helper_raise_exception(CPUPPCState
*env
, uint32_t exception
)
2661 raise_exception_err_ra(env
, exception
, 0, 0);
2665 #if !defined(CONFIG_USER_ONLY)
2667 void helper_store_msr(CPUPPCState
*env
, target_ulong val
)
2669 uint32_t excp
= hreg_store_msr(env
, val
, 0);
2672 cpu_interrupt_exittb(env_cpu(env
));
2673 raise_exception(env
, excp
);
2677 void helper_ppc_maybe_interrupt(CPUPPCState
*env
)
2679 ppc_maybe_interrupt(env
);
2682 #if defined(TARGET_PPC64)
2683 void helper_scv(CPUPPCState
*env
, uint32_t lev
)
2685 if (env
->spr
[SPR_FSCR
] & (1ull << FSCR_SCV
)) {
2686 raise_exception_err(env
, POWERPC_EXCP_SYSCALL_VECTORED
, lev
);
2688 raise_exception_err(env
, POWERPC_EXCP_FU
, FSCR_IC_SCV
);
2692 void helper_pminsn(CPUPPCState
*env
, uint32_t insn
)
2694 CPUState
*cs
= env_cpu(env
);
2698 /* Condition for waking up at 0x100 */
2699 env
->resume_as_sreset
= (insn
!= PPC_PM_STOP
) ||
2700 (env
->spr
[SPR_PSSCR
] & PSSCR_EC
);
2702 /* HDECR is not to wake from PM state, it may have already fired */
2703 if (env
->resume_as_sreset
) {
2704 PowerPCCPU
*cpu
= env_archcpu(env
);
2705 ppc_set_irq(cpu
, PPC_INTERRUPT_HDECR
, 0);
2708 ppc_maybe_interrupt(env
);
2710 #endif /* defined(TARGET_PPC64) */
2712 static void do_rfi(CPUPPCState
*env
, target_ulong nip
, target_ulong msr
)
2714 /* MSR:POW cannot be set by any form of rfi */
2715 msr
&= ~(1ULL << MSR_POW
);
2717 /* MSR:TGPR cannot be set by any form of rfi */
2718 if (env
->flags
& POWERPC_FLAG_TGPR
)
2719 msr
&= ~(1ULL << MSR_TGPR
);
2721 #if defined(TARGET_PPC64)
2722 /* Switching to 32-bit ? Crop the nip */
2723 if (!msr_is_64bit(env
, msr
)) {
2724 nip
= (uint32_t)nip
;
2727 nip
= (uint32_t)nip
;
2729 /* XXX: beware: this is false if VLE is supported */
2730 env
->nip
= nip
& ~((target_ulong
)0x00000003);
2731 hreg_store_msr(env
, msr
, 1);
2732 trace_ppc_excp_rfi(env
->nip
, env
->msr
);
2734 * No need to raise an exception here, as rfi is always the last
2737 cpu_interrupt_exittb(env_cpu(env
));
2738 /* Reset the reservation */
2739 env
->reserve_addr
= -1;
2741 /* Context synchronizing: check if TCG TLB needs flush */
2742 check_tlb_flush(env
, false);
2745 void helper_rfi(CPUPPCState
*env
)
2747 do_rfi(env
, env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
] & 0xfffffffful
);
2750 #if defined(TARGET_PPC64)
2751 void helper_rfid(CPUPPCState
*env
)
2754 * The architecture defines a number of rules for which bits can
2755 * change but in practice, we handle this in hreg_store_msr()
2756 * which will be called by do_rfi(), so there is no need to filter
2759 do_rfi(env
, env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
]);
2762 void helper_rfscv(CPUPPCState
*env
)
2764 do_rfi(env
, env
->lr
, env
->ctr
);
2767 void helper_hrfid(CPUPPCState
*env
)
2769 do_rfi(env
, env
->spr
[SPR_HSRR0
], env
->spr
[SPR_HSRR1
]);
2773 #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
2774 void helper_rfebb(CPUPPCState
*env
, target_ulong s
)
2776 target_ulong msr
= env
->msr
;
2779 * Handling of BESCR bits 32:33 according to PowerISA v3.1:
2781 * "If BESCR 32:33 != 0b00 the instruction is treated as if
2782 * the instruction form were invalid."
2784 if (env
->spr
[SPR_BESCR
] & BESCR_INVALID
) {
2785 raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
2786 POWERPC_EXCP_INVAL
| POWERPC_EXCP_INVAL_INVAL
);
2789 env
->nip
= env
->spr
[SPR_EBBRR
];
2791 /* Switching to 32-bit ? Crop the nip */
2792 if (!msr_is_64bit(env
, msr
)) {
2793 env
->nip
= (uint32_t)env
->spr
[SPR_EBBRR
];
2797 env
->spr
[SPR_BESCR
] |= BESCR_GE
;
2799 env
->spr
[SPR_BESCR
] &= ~BESCR_GE
;
2804 * Triggers or queues an 'ebb_excp' EBB exception. All checks
2805 * but FSCR, HFSCR and msr_pr must be done beforehand.
2807 * PowerISA v3.1 isn't clear about whether an EBB should be
2808 * postponed or cancelled if the EBB facility is unavailable.
2809 * Our assumption here is that the EBB is cancelled if both
2810 * FSCR and HFSCR EBB facilities aren't available.
2812 static void do_ebb(CPUPPCState
*env
, int ebb_excp
)
2814 PowerPCCPU
*cpu
= env_archcpu(env
);
2817 * FSCR_EBB and FSCR_IC_EBB are the same bits used with
2820 helper_fscr_facility_check(env
, FSCR_EBB
, 0, FSCR_IC_EBB
);
2821 helper_hfscr_facility_check(env
, FSCR_EBB
, "EBB", FSCR_IC_EBB
);
2823 if (ebb_excp
== POWERPC_EXCP_PERFM_EBB
) {
2824 env
->spr
[SPR_BESCR
] |= BESCR_PMEO
;
2825 } else if (ebb_excp
== POWERPC_EXCP_EXTERNAL_EBB
) {
2826 env
->spr
[SPR_BESCR
] |= BESCR_EEO
;
2829 if (FIELD_EX64(env
->msr
, MSR
, PR
)) {
2830 powerpc_excp(cpu
, ebb_excp
);
2832 ppc_set_irq(cpu
, PPC_INTERRUPT_EBB
, 1);
2836 void raise_ebb_perfm_exception(CPUPPCState
*env
)
2838 bool perfm_ebb_enabled
= env
->spr
[SPR_POWER_MMCR0
] & MMCR0_EBE
&&
2839 env
->spr
[SPR_BESCR
] & BESCR_PME
&&
2840 env
->spr
[SPR_BESCR
] & BESCR_GE
;
2842 if (!perfm_ebb_enabled
) {
2846 do_ebb(env
, POWERPC_EXCP_PERFM_EBB
);
2850 /*****************************************************************************/
2851 /* Embedded PowerPC specific helpers */
2852 void helper_40x_rfci(CPUPPCState
*env
)
2854 do_rfi(env
, env
->spr
[SPR_40x_SRR2
], env
->spr
[SPR_40x_SRR3
]);
2857 void helper_rfci(CPUPPCState
*env
)
2859 do_rfi(env
, env
->spr
[SPR_BOOKE_CSRR0
], env
->spr
[SPR_BOOKE_CSRR1
]);
2862 void helper_rfdi(CPUPPCState
*env
)
2864 /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
2865 do_rfi(env
, env
->spr
[SPR_BOOKE_DSRR0
], env
->spr
[SPR_BOOKE_DSRR1
]);
2868 void helper_rfmci(CPUPPCState
*env
)
2870 /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
2871 do_rfi(env
, env
->spr
[SPR_BOOKE_MCSRR0
], env
->spr
[SPR_BOOKE_MCSRR1
]);
2873 #endif /* CONFIG_TCG */
2874 #endif /* !defined(CONFIG_USER_ONLY) */
2877 void helper_tw(CPUPPCState
*env
, target_ulong arg1
, target_ulong arg2
,
2880 if (!likely(!(((int32_t)arg1
< (int32_t)arg2
&& (flags
& 0x10)) ||
2881 ((int32_t)arg1
> (int32_t)arg2
&& (flags
& 0x08)) ||
2882 ((int32_t)arg1
== (int32_t)arg2
&& (flags
& 0x04)) ||
2883 ((uint32_t)arg1
< (uint32_t)arg2
&& (flags
& 0x02)) ||
2884 ((uint32_t)arg1
> (uint32_t)arg2
&& (flags
& 0x01))))) {
2885 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
2886 POWERPC_EXCP_TRAP
, GETPC());
2890 #if defined(TARGET_PPC64)
2891 void helper_td(CPUPPCState
*env
, target_ulong arg1
, target_ulong arg2
,
2894 if (!likely(!(((int64_t)arg1
< (int64_t)arg2
&& (flags
& 0x10)) ||
2895 ((int64_t)arg1
> (int64_t)arg2
&& (flags
& 0x08)) ||
2896 ((int64_t)arg1
== (int64_t)arg2
&& (flags
& 0x04)) ||
2897 ((uint64_t)arg1
< (uint64_t)arg2
&& (flags
& 0x02)) ||
2898 ((uint64_t)arg1
> (uint64_t)arg2
&& (flags
& 0x01))))) {
2899 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
2900 POWERPC_EXCP_TRAP
, GETPC());
2907 static uint32_t helper_SIMON_LIKE_32_64(uint32_t x
, uint64_t key
, uint32_t lane
)
2909 const uint16_t c
= 0xfffc;
2910 const uint64_t z0
= 0xfa2561cdf44ac398ULL
;
2911 uint16_t z
= 0, temp
;
2912 uint16_t k
[32], eff_k
[32], xleft
[33], xright
[33], fxleft
[32];
2914 for (int i
= 3; i
>= 0; i
--) {
2915 k
[i
] = key
& 0xffff;
2918 xleft
[0] = x
& 0xffff;
2919 xright
[0] = (x
>> 16) & 0xffff;
2921 for (int i
= 0; i
< 28; i
++) {
2922 z
= (z0
>> (63 - i
)) & 1;
2923 temp
= ror16(k
[i
+ 3], 3) ^ k
[i
+ 1];
2924 k
[i
+ 4] = c
^ z
^ k
[i
] ^ temp
^ ror16(temp
, 1);
2927 for (int i
= 0; i
< 8; i
++) {
2928 eff_k
[4 * i
+ 0] = k
[4 * i
+ ((0 + lane
) % 4)];
2929 eff_k
[4 * i
+ 1] = k
[4 * i
+ ((1 + lane
) % 4)];
2930 eff_k
[4 * i
+ 2] = k
[4 * i
+ ((2 + lane
) % 4)];
2931 eff_k
[4 * i
+ 3] = k
[4 * i
+ ((3 + lane
) % 4)];
2934 for (int i
= 0; i
< 32; i
++) {
2935 fxleft
[i
] = (rol16(xleft
[i
], 1) &
2936 rol16(xleft
[i
], 8)) ^ rol16(xleft
[i
], 2);
2937 xleft
[i
+ 1] = xright
[i
] ^ fxleft
[i
] ^ eff_k
[i
];
2938 xright
[i
+ 1] = xleft
[i
];
2941 return (((uint32_t)xright
[32]) << 16) | xleft
[32];
2944 static uint64_t hash_digest(uint64_t ra
, uint64_t rb
, uint64_t key
)
2946 uint64_t stage0_h
= 0ULL, stage0_l
= 0ULL;
2947 uint64_t stage1_h
, stage1_l
;
2949 for (int i
= 0; i
< 4; i
++) {
2950 stage0_h
|= ror64(rb
& 0xff, 8 * (2 * i
+ 1));
2951 stage0_h
|= ((ra
>> 32) & 0xff) << (8 * 2 * i
);
2952 stage0_l
|= ror64((rb
>> 32) & 0xff, 8 * (2 * i
+ 1));
2953 stage0_l
|= (ra
& 0xff) << (8 * 2 * i
);
2958 stage1_h
= (uint64_t)helper_SIMON_LIKE_32_64(stage0_h
>> 32, key
, 0) << 32;
2959 stage1_h
|= helper_SIMON_LIKE_32_64(stage0_h
, key
, 1);
2960 stage1_l
= (uint64_t)helper_SIMON_LIKE_32_64(stage0_l
>> 32, key
, 2) << 32;
2961 stage1_l
|= helper_SIMON_LIKE_32_64(stage0_l
, key
, 3);
2963 return stage1_h
^ stage1_l
;
2966 static void do_hash(CPUPPCState
*env
, target_ulong ea
, target_ulong ra
,
2967 target_ulong rb
, uint64_t key
, bool store
)
2969 uint64_t calculated_hash
= hash_digest(ra
, rb
, key
), loaded_hash
;
2972 cpu_stq_data_ra(env
, ea
, calculated_hash
, GETPC());
2974 loaded_hash
= cpu_ldq_data_ra(env
, ea
, GETPC());
2975 if (loaded_hash
!= calculated_hash
) {
2976 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
2977 POWERPC_EXCP_TRAP
, GETPC());
2982 #include "qemu/guest-random.h"
2985 #define HELPER_HASH(op, key, store, dexcr_aspect) \
2986 void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra, \
2989 if (env->msr & R_MSR_PR_MASK) { \
2990 if (!(env->spr[SPR_DEXCR] & R_DEXCR_PRO_##dexcr_aspect##_MASK || \
2991 env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK)) \
2993 } else if (!(env->msr & R_MSR_HV_MASK)) { \
2994 if (!(env->spr[SPR_DEXCR] & R_DEXCR_PNH_##dexcr_aspect##_MASK || \
2995 env->spr[SPR_HDEXCR] & R_HDEXCR_ENF_##dexcr_aspect##_MASK)) \
2997 } else if (!(env->msr & R_MSR_S_MASK)) { \
2998 if (!(env->spr[SPR_HDEXCR] & R_HDEXCR_HNU_##dexcr_aspect##_MASK)) \
3002 do_hash(env, ea, ra, rb, key, store); \
3005 #define HELPER_HASH(op, key, store, dexcr_aspect) \
3006 void helper_##op(CPUPPCState *env, target_ulong ea, target_ulong ra, \
3009 do_hash(env, ea, ra, rb, key, store); \
3011 #endif /* TARGET_PPC64 */
3013 HELPER_HASH(HASHST
, env
->spr
[SPR_HASHKEYR
], true, NPHIE
)
3014 HELPER_HASH(HASHCHK
, env
->spr
[SPR_HASHKEYR
], false, NPHIE
)
3015 HELPER_HASH(HASHSTP
, env
->spr
[SPR_HASHPKEYR
], true, PHIE
)
3016 HELPER_HASH(HASHCHKP
, env
->spr
[SPR_HASHPKEYR
], false, PHIE
)
3017 #endif /* CONFIG_TCG */
3019 #if !defined(CONFIG_USER_ONLY)
3023 /* Embedded.Processor Control */
3024 static int dbell2irq(target_ulong rb
)
3026 int msg
= rb
& DBELL_TYPE_MASK
;
3030 case DBELL_TYPE_DBELL
:
3031 irq
= PPC_INTERRUPT_DOORBELL
;
3033 case DBELL_TYPE_DBELL_CRIT
:
3034 irq
= PPC_INTERRUPT_CDOORBELL
;
3036 case DBELL_TYPE_G_DBELL
:
3037 case DBELL_TYPE_G_DBELL_CRIT
:
3038 case DBELL_TYPE_G_DBELL_MC
:
3047 void helper_msgclr(CPUPPCState
*env
, target_ulong rb
)
3049 int irq
= dbell2irq(rb
);
3055 ppc_set_irq(env_archcpu(env
), irq
, 0);
3058 void helper_msgsnd(target_ulong rb
)
3060 int irq
= dbell2irq(rb
);
3061 int pir
= rb
& DBELL_PIRTAG_MASK
;
3070 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
3071 CPUPPCState
*cenv
= &cpu
->env
;
3073 if ((rb
& DBELL_BRDCAST
) || (cenv
->spr
[SPR_BOOKE_PIR
] == pir
)) {
3074 ppc_set_irq(cpu
, irq
, 1);
3080 /* Server Processor Control */
3082 static bool dbell_type_server(target_ulong rb
)
3085 * A Directed Hypervisor Doorbell message is sent only if the
3086 * message type is 5. All other types are reserved and the
3087 * instruction is a no-op
3089 return (rb
& DBELL_TYPE_MASK
) == DBELL_TYPE_DBELL_SERVER
;
3092 void helper_book3s_msgclr(CPUPPCState
*env
, target_ulong rb
)
3094 if (!dbell_type_server(rb
)) {
3098 ppc_set_irq(env_archcpu(env
), PPC_INTERRUPT_HDOORBELL
, 0);
3101 static void book3s_msgsnd_common(int pir
, int irq
)
3107 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
3108 CPUPPCState
*cenv
= &cpu
->env
;
3110 /* TODO: broadcast message to all threads of the same processor */
3111 if (cenv
->spr_cb
[SPR_PIR
].default_value
== pir
) {
3112 ppc_set_irq(cpu
, irq
, 1);
3118 void helper_book3s_msgsnd(target_ulong rb
)
3120 int pir
= rb
& DBELL_PROCIDTAG_MASK
;
3122 if (!dbell_type_server(rb
)) {
3126 book3s_msgsnd_common(pir
, PPC_INTERRUPT_HDOORBELL
);
3129 #if defined(TARGET_PPC64)
3130 void helper_book3s_msgclrp(CPUPPCState
*env
, target_ulong rb
)
3132 helper_hfscr_facility_check(env
, HFSCR_MSGP
, "msgclrp", HFSCR_IC_MSGP
);
3134 if (!dbell_type_server(rb
)) {
3138 ppc_set_irq(env_archcpu(env
), PPC_INTERRUPT_DOORBELL
, 0);
3142 * sends a message to another thread on the same
3143 * multi-threaded processor
3145 void helper_book3s_msgsndp(CPUPPCState
*env
, target_ulong rb
)
3147 CPUState
*cs
= env_cpu(env
);
3148 PowerPCCPU
*cpu
= env_archcpu(env
);
3150 uint32_t nr_threads
= cs
->nr_threads
;
3151 int ttir
= rb
& PPC_BITMASK(57, 63);
3153 helper_hfscr_facility_check(env
, HFSCR_MSGP
, "msgsndp", HFSCR_IC_MSGP
);
3155 if (!(env
->flags
& POWERPC_FLAG_SMT_1LPAR
)) {
3156 nr_threads
= 1; /* msgsndp behaves as 1-thread in LPAR-per-thread mode*/
3159 if (!dbell_type_server(rb
) || ttir
>= nr_threads
) {
3163 if (nr_threads
== 1) {
3164 ppc_set_irq(cpu
, PPC_INTERRUPT_DOORBELL
, 1);
3168 /* Does iothread need to be locked for walking CPU list? */
3170 THREAD_SIBLING_FOREACH(cs
, ccs
) {
3171 PowerPCCPU
*ccpu
= POWERPC_CPU(ccs
);
3172 uint32_t thread_id
= ppc_cpu_tir(ccpu
);
3174 if (ttir
== thread_id
) {
3175 ppc_set_irq(ccpu
, PPC_INTERRUPT_DOORBELL
, 1);
3181 g_assert_not_reached();
3183 #endif /* TARGET_PPC64 */
3185 /* Single-step tracing */
3186 void helper_book3s_trace(CPUPPCState
*env
, target_ulong prev_ip
)
3188 uint32_t error_code
= 0;
3189 if (env
->insns_flags2
& PPC2_ISA207S
) {
3190 /* Load/store reporting, SRR1[35, 36] and SDAR, are not implemented. */
3191 env
->spr
[SPR_POWER_SIAR
] = prev_ip
;
3192 error_code
= PPC_BIT(33);
3194 raise_exception_err(env
, POWERPC_EXCP_TRACE
, error_code
);
3197 void ppc_cpu_do_unaligned_access(CPUState
*cs
, vaddr vaddr
,
3198 MMUAccessType access_type
,
3199 int mmu_idx
, uintptr_t retaddr
)
3201 CPUPPCState
*env
= cpu_env(cs
);
3204 /* Restore state and reload the insn we executed, for filling in DSISR. */
3205 cpu_restore_state(cs
, retaddr
);
3206 insn
= ppc_ldl_code(env
, env
->nip
);
3208 switch (env
->mmu_model
) {
3209 case POWERPC_MMU_SOFT_4xx
:
3210 env
->spr
[SPR_40x_DEAR
] = vaddr
;
3212 case POWERPC_MMU_BOOKE
:
3213 case POWERPC_MMU_BOOKE206
:
3214 env
->spr
[SPR_BOOKE_DEAR
] = vaddr
;
3217 env
->spr
[SPR_DAR
] = vaddr
;
3221 cs
->exception_index
= POWERPC_EXCP_ALIGN
;
3222 env
->error_code
= insn
& 0x03FF0000;
3226 void ppc_cpu_do_transaction_failed(CPUState
*cs
, hwaddr physaddr
,
3227 vaddr vaddr
, unsigned size
,
3228 MMUAccessType access_type
,
3229 int mmu_idx
, MemTxAttrs attrs
,
3230 MemTxResult response
, uintptr_t retaddr
)
3232 CPUPPCState
*env
= cpu_env(cs
);
3234 switch (env
->excp_model
) {
3235 #if defined(TARGET_PPC64)
3236 case POWERPC_EXCP_POWER8
:
3237 case POWERPC_EXCP_POWER9
:
3238 case POWERPC_EXCP_POWER10
:
3240 * Machine check codes can be found in processor User Manual or
3241 * Linux or skiboot source.
3243 if (access_type
== MMU_DATA_LOAD
) {
3244 env
->spr
[SPR_DAR
] = vaddr
;
3245 env
->spr
[SPR_DSISR
] = PPC_BIT(57);
3246 env
->error_code
= PPC_BIT(42);
3248 } else if (access_type
== MMU_DATA_STORE
) {
3250 * MCE for stores in POWER is asynchronous so hardware does
3251 * not set DAR, but QEMU can do better.
3253 env
->spr
[SPR_DAR
] = vaddr
;
3254 env
->error_code
= PPC_BIT(36) | PPC_BIT(43) | PPC_BIT(45);
3255 env
->error_code
|= PPC_BIT(42);
3257 } else { /* Fetch */
3259 * is_prefix_insn_excp() tests !PPC_BIT(42) to avoid fetching
3260 * the instruction, so that must always be clear for fetches.
3262 env
->error_code
= PPC_BIT(36) | PPC_BIT(44) | PPC_BIT(45);
3268 * TODO: Check behaviour for other CPUs, for now do nothing.
3269 * Could add a basic MCE even if real hardware ignores.
3274 cs
->exception_index
= POWERPC_EXCP_MCHECK
;
3275 cpu_loop_exit_restore(cs
, retaddr
);
3278 void ppc_cpu_debug_excp_handler(CPUState
*cs
)
3280 #if defined(TARGET_PPC64)
3281 CPUPPCState
*env
= cpu_env(cs
);
3283 if (env
->insns_flags2
& PPC2_ISA207S
) {
3284 if (cs
->watchpoint_hit
) {
3285 if (cs
->watchpoint_hit
->flags
& BP_CPU
) {
3286 env
->spr
[SPR_DAR
] = cs
->watchpoint_hit
->hitaddr
;
3287 env
->spr
[SPR_DSISR
] = PPC_BIT(41);
3288 cs
->watchpoint_hit
= NULL
;
3289 raise_exception(env
, POWERPC_EXCP_DSI
);
3291 cs
->watchpoint_hit
= NULL
;
3292 } else if (cpu_breakpoint_test(cs
, env
->nip
, BP_CPU
)) {
3293 raise_exception_err(env
, POWERPC_EXCP_TRACE
,
3294 PPC_BIT(33) | PPC_BIT(43));
3300 bool ppc_cpu_debug_check_breakpoint(CPUState
*cs
)
3302 #if defined(TARGET_PPC64)
3303 CPUPPCState
*env
= cpu_env(cs
);
3305 if (env
->insns_flags2
& PPC2_ISA207S
) {
3308 priv
= env
->spr
[SPR_CIABR
] & PPC_BITMASK(62, 63);
3310 case 0x1: /* problem */
3311 return env
->msr
& ((target_ulong
)1 << MSR_PR
);
3312 case 0x2: /* supervisor */
3313 return (!(env
->msr
& ((target_ulong
)1 << MSR_PR
)) &&
3314 !(env
->msr
& ((target_ulong
)1 << MSR_HV
)));
3315 case 0x3: /* hypervisor */
3316 return (!(env
->msr
& ((target_ulong
)1 << MSR_PR
)) &&
3317 (env
->msr
& ((target_ulong
)1 << MSR_HV
)));
3319 g_assert_not_reached();
3327 bool ppc_cpu_debug_check_watchpoint(CPUState
*cs
, CPUWatchpoint
*wp
)
3329 #if defined(TARGET_PPC64)
3330 CPUPPCState
*env
= cpu_env(cs
);
3332 if (env
->insns_flags2
& PPC2_ISA207S
) {
3333 if (wp
== env
->dawr0_watchpoint
) {
3334 uint32_t dawrx
= env
->spr
[SPR_DAWRX0
];
3335 bool wt
= extract32(dawrx
, PPC_BIT_NR(59), 1);
3336 bool wti
= extract32(dawrx
, PPC_BIT_NR(60), 1);
3337 bool hv
= extract32(dawrx
, PPC_BIT_NR(61), 1);
3338 bool sv
= extract32(dawrx
, PPC_BIT_NR(62), 1);
3339 bool pr
= extract32(dawrx
, PPC_BIT_NR(62), 1);
3341 if ((env
->msr
& ((target_ulong
)1 << MSR_PR
)) && !pr
) {
3343 } else if ((env
->msr
& ((target_ulong
)1 << MSR_HV
)) && !hv
) {
3350 if (env
->msr
& ((target_ulong
)1 << MSR_DR
)) {
3369 #endif /* CONFIG_TCG */
3370 #endif /* !CONFIG_USER_ONLY */