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 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"
22 #include "exec/helper-proto.h"
23 #include "exec/exec-all.h"
24 #include "exec/cpu_ldst.h"
26 #include "helper_regs.h"
29 //#define DEBUG_SOFTWARE_TLB
30 //#define DEBUG_EXCEPTIONS
32 #ifdef DEBUG_EXCEPTIONS
33 # define LOG_EXCP(...) qemu_log(__VA_ARGS__)
35 # define LOG_EXCP(...) do { } while (0)
38 /*****************************************************************************/
39 /* Exception processing */
40 #if defined(CONFIG_USER_ONLY)
41 void ppc_cpu_do_interrupt(CPUState
*cs
)
43 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
44 CPUPPCState
*env
= &cpu
->env
;
46 cs
->exception_index
= POWERPC_EXCP_NONE
;
50 static void ppc_hw_interrupt(CPUPPCState
*env
)
52 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
54 cs
->exception_index
= POWERPC_EXCP_NONE
;
57 #else /* defined(CONFIG_USER_ONLY) */
58 static inline void dump_syscall(CPUPPCState
*env
)
60 qemu_log_mask(CPU_LOG_INT
, "syscall r0=%016" PRIx64
" r3=%016" PRIx64
61 " r4=%016" PRIx64
" r5=%016" PRIx64
" r6=%016" PRIx64
62 " nip=" TARGET_FMT_lx
"\n",
63 ppc_dump_gpr(env
, 0), ppc_dump_gpr(env
, 3),
64 ppc_dump_gpr(env
, 4), ppc_dump_gpr(env
, 5),
65 ppc_dump_gpr(env
, 6), env
->nip
);
68 /* Note that this function should be greatly optimized
69 * when called with a constant excp, from ppc_hw_interrupt
71 static inline void powerpc_excp(PowerPCCPU
*cpu
, int excp_model
, int excp
)
73 CPUState
*cs
= CPU(cpu
);
74 CPUPPCState
*env
= &cpu
->env
;
75 target_ulong msr
, new_msr
, vector
;
76 int srr0
, srr1
, asrr0
, asrr1
, lev
, ail
;
79 qemu_log_mask(CPU_LOG_INT
, "Raise exception at " TARGET_FMT_lx
80 " => %08x (%02x)\n", env
->nip
, excp
, env
->error_code
);
82 /* new srr1 value excluding must-be-zero bits */
83 if (excp_model
== POWERPC_EXCP_BOOKE
) {
86 msr
= env
->msr
& ~0x783f0000ULL
;
89 /* new interrupt handler msr preserves existing HV and ME unless
90 * explicitly overriden
92 new_msr
= env
->msr
& (((target_ulong
)1 << MSR_ME
) | MSR_HVB
);
94 /* target registers */
100 /* check for special resume at 0x100 from doze/nap/sleep/winkle on P7/P8 */
101 if (env
->in_pm_state
) {
102 env
->in_pm_state
= false;
104 /* Pretend to be returning from doze always as we don't lose state */
105 msr
|= (0x1ull
<< (63 - 47));
107 /* Non-machine check are routed to 0x100 with a wakeup cause
110 if (excp
!= POWERPC_EXCP_MCHECK
) {
112 case POWERPC_EXCP_RESET
:
113 msr
|= 0x4ull
<< (63 - 45);
115 case POWERPC_EXCP_EXTERNAL
:
116 msr
|= 0x8ull
<< (63 - 45);
118 case POWERPC_EXCP_DECR
:
119 msr
|= 0x6ull
<< (63 - 45);
121 case POWERPC_EXCP_SDOOR
:
122 msr
|= 0x5ull
<< (63 - 45);
124 case POWERPC_EXCP_SDOOR_HV
:
125 msr
|= 0x3ull
<< (63 - 45);
127 case POWERPC_EXCP_HV_MAINT
:
128 msr
|= 0xaull
<< (63 - 45);
131 cpu_abort(cs
, "Unsupported exception %d in Power Save mode\n",
134 excp
= POWERPC_EXCP_RESET
;
138 /* Exception targetting modifiers
140 * LPES0 is supported on POWER7/8
141 * LPES1 is not supported (old iSeries mode)
143 * On anything else, we behave as if LPES0 is 1
144 * (externals don't alter MSR:HV)
146 * AIL is initialized here but can be cleared by
147 * selected exceptions
149 #if defined(TARGET_PPC64)
150 if (excp_model
== POWERPC_EXCP_POWER7
||
151 excp_model
== POWERPC_EXCP_POWER8
) {
152 lpes0
= !!(env
->spr
[SPR_LPCR
] & LPCR_LPES0
);
153 if (excp_model
== POWERPC_EXCP_POWER8
) {
154 ail
= (env
->spr
[SPR_LPCR
] & LPCR_AIL
) >> LPCR_AIL_SHIFT
;
159 #endif /* defined(TARGET_PPC64) */
165 /* Hypervisor emulation assistance interrupt only exists on server
166 * arch 2.05 server or later. We also don't want to generate it if
167 * we don't have HVB in msr_mask (PAPR mode).
169 if (excp
== POWERPC_EXCP_HV_EMU
170 #if defined(TARGET_PPC64)
171 && !((env
->mmu_model
& POWERPC_MMU_64
) && (env
->msr_mask
& MSR_HVB
))
172 #endif /* defined(TARGET_PPC64) */
175 excp
= POWERPC_EXCP_PROGRAM
;
179 case POWERPC_EXCP_NONE
:
180 /* Should never happen */
182 case POWERPC_EXCP_CRITICAL
: /* Critical input */
183 switch (excp_model
) {
184 case POWERPC_EXCP_40x
:
188 case POWERPC_EXCP_BOOKE
:
189 srr0
= SPR_BOOKE_CSRR0
;
190 srr1
= SPR_BOOKE_CSRR1
;
192 case POWERPC_EXCP_G2
:
198 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
200 /* Machine check exception is not enabled.
201 * Enter checkstop state.
203 fprintf(stderr
, "Machine check while not allowed. "
204 "Entering checkstop state\n");
205 if (qemu_log_separate()) {
206 qemu_log("Machine check while not allowed. "
207 "Entering checkstop state\n");
210 cs
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
212 if (env
->msr_mask
& MSR_HVB
) {
213 /* ISA specifies HV, but can be delivered to guest with HV clear
214 * (e.g., see FWNMI in PAPR).
216 new_msr
|= (target_ulong
)MSR_HVB
;
220 /* machine check exceptions don't have ME set */
221 new_msr
&= ~((target_ulong
)1 << MSR_ME
);
223 /* XXX: should also have something loaded in DAR / DSISR */
224 switch (excp_model
) {
225 case POWERPC_EXCP_40x
:
229 case POWERPC_EXCP_BOOKE
:
230 /* FIXME: choose one or the other based on CPU type */
231 srr0
= SPR_BOOKE_MCSRR0
;
232 srr1
= SPR_BOOKE_MCSRR1
;
233 asrr0
= SPR_BOOKE_CSRR0
;
234 asrr1
= SPR_BOOKE_CSRR1
;
240 case POWERPC_EXCP_DSI
: /* Data storage exception */
241 LOG_EXCP("DSI exception: DSISR=" TARGET_FMT_lx
" DAR=" TARGET_FMT_lx
242 "\n", env
->spr
[SPR_DSISR
], env
->spr
[SPR_DAR
]);
244 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
245 LOG_EXCP("ISI exception: msr=" TARGET_FMT_lx
", nip=" TARGET_FMT_lx
246 "\n", msr
, env
->nip
);
247 msr
|= env
->error_code
;
249 case POWERPC_EXCP_EXTERNAL
: /* External input */
253 new_msr
|= (target_ulong
)MSR_HVB
;
254 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_RI
);
258 if (env
->mpic_proxy
) {
259 /* IACK the IRQ on delivery */
260 env
->spr
[SPR_BOOKE_EPR
] = ldl_phys(cs
->as
, env
->mpic_iack
);
263 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
264 /* Get rS/rD and rA from faulting opcode */
265 /* Note: the opcode fields will not be set properly for a direct
266 * store load/store, but nobody cares as nobody actually uses
267 * direct store segments.
269 env
->spr
[SPR_DSISR
] |= (env
->error_code
& 0x03FF0000) >> 16;
271 case POWERPC_EXCP_PROGRAM
: /* Program exception */
272 switch (env
->error_code
& ~0xF) {
273 case POWERPC_EXCP_FP
:
274 if ((msr_fe0
== 0 && msr_fe1
== 0) || msr_fp
== 0) {
275 LOG_EXCP("Ignore floating point exception\n");
276 cs
->exception_index
= POWERPC_EXCP_NONE
;
281 /* FP exceptions always have NIP pointing to the faulting
282 * instruction, so always use store_next and claim we are
283 * precise in the MSR.
286 env
->spr
[SPR_BOOKE_ESR
] = ESR_FP
;
288 case POWERPC_EXCP_INVAL
:
289 LOG_EXCP("Invalid instruction at " TARGET_FMT_lx
"\n", env
->nip
);
291 env
->spr
[SPR_BOOKE_ESR
] = ESR_PIL
;
293 case POWERPC_EXCP_PRIV
:
295 env
->spr
[SPR_BOOKE_ESR
] = ESR_PPR
;
297 case POWERPC_EXCP_TRAP
:
299 env
->spr
[SPR_BOOKE_ESR
] = ESR_PTR
;
302 /* Should never occur */
303 cpu_abort(cs
, "Invalid program exception %d. Aborting\n",
308 case POWERPC_EXCP_SYSCALL
: /* System call exception */
310 lev
= env
->error_code
;
312 /* We need to correct the NIP which in this case is supposed
313 * to point to the next instruction
317 /* "PAPR mode" built-in hypercall emulation */
318 if ((lev
== 1) && cpu
->vhyp
) {
319 PPCVirtualHypervisorClass
*vhc
=
320 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu
->vhyp
);
321 vhc
->hypercall(cpu
->vhyp
, cpu
);
325 new_msr
|= (target_ulong
)MSR_HVB
;
328 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
329 case POWERPC_EXCP_APU
: /* Auxiliary processor unavailable */
330 case POWERPC_EXCP_DECR
: /* Decrementer exception */
332 case POWERPC_EXCP_FIT
: /* Fixed-interval timer interrupt */
334 LOG_EXCP("FIT exception\n");
336 case POWERPC_EXCP_WDT
: /* Watchdog timer interrupt */
337 LOG_EXCP("WDT exception\n");
338 switch (excp_model
) {
339 case POWERPC_EXCP_BOOKE
:
340 srr0
= SPR_BOOKE_CSRR0
;
341 srr1
= SPR_BOOKE_CSRR1
;
347 case POWERPC_EXCP_DTLB
: /* Data TLB error */
348 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
350 case POWERPC_EXCP_DEBUG
: /* Debug interrupt */
351 switch (excp_model
) {
352 case POWERPC_EXCP_BOOKE
:
353 /* FIXME: choose one or the other based on CPU type */
354 srr0
= SPR_BOOKE_DSRR0
;
355 srr1
= SPR_BOOKE_DSRR1
;
356 asrr0
= SPR_BOOKE_CSRR0
;
357 asrr1
= SPR_BOOKE_CSRR1
;
363 cpu_abort(cs
, "Debug exception is not implemented yet !\n");
365 case POWERPC_EXCP_SPEU
: /* SPE/embedded floating-point unavailable */
366 env
->spr
[SPR_BOOKE_ESR
] = ESR_SPV
;
368 case POWERPC_EXCP_EFPDI
: /* Embedded floating-point data interrupt */
370 cpu_abort(cs
, "Embedded floating point data exception "
371 "is not implemented yet !\n");
372 env
->spr
[SPR_BOOKE_ESR
] = ESR_SPV
;
374 case POWERPC_EXCP_EFPRI
: /* Embedded floating-point round interrupt */
376 cpu_abort(cs
, "Embedded floating point round exception "
377 "is not implemented yet !\n");
378 env
->spr
[SPR_BOOKE_ESR
] = ESR_SPV
;
380 case POWERPC_EXCP_EPERFM
: /* Embedded performance monitor interrupt */
383 "Performance counter exception is not implemented yet !\n");
385 case POWERPC_EXCP_DOORI
: /* Embedded doorbell interrupt */
387 case POWERPC_EXCP_DOORCI
: /* Embedded doorbell critical interrupt */
388 srr0
= SPR_BOOKE_CSRR0
;
389 srr1
= SPR_BOOKE_CSRR1
;
391 case POWERPC_EXCP_RESET
: /* System reset exception */
392 /* A power-saving exception sets ME, otherwise it is unchanged */
394 /* indicate that we resumed from power save mode */
396 new_msr
|= ((target_ulong
)1 << MSR_ME
);
398 if (env
->msr_mask
& MSR_HVB
) {
399 /* ISA specifies HV, but can be delivered to guest with HV clear
400 * (e.g., see FWNMI in PAPR, NMI injection in QEMU).
402 new_msr
|= (target_ulong
)MSR_HVB
;
405 cpu_abort(cs
, "Trying to deliver power-saving system reset "
406 "exception %d with no HV support\n", excp
);
411 case POWERPC_EXCP_DSEG
: /* Data segment exception */
412 case POWERPC_EXCP_ISEG
: /* Instruction segment exception */
413 case POWERPC_EXCP_TRACE
: /* Trace exception */
415 case POWERPC_EXCP_HDECR
: /* Hypervisor decrementer exception */
416 case POWERPC_EXCP_HDSI
: /* Hypervisor data storage exception */
417 case POWERPC_EXCP_HISI
: /* Hypervisor instruction storage exception */
418 case POWERPC_EXCP_HDSEG
: /* Hypervisor data segment exception */
419 case POWERPC_EXCP_HISEG
: /* Hypervisor instruction segment exception */
420 case POWERPC_EXCP_HV_EMU
:
423 new_msr
|= (target_ulong
)MSR_HVB
;
424 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_RI
);
426 case POWERPC_EXCP_VPU
: /* Vector unavailable exception */
427 case POWERPC_EXCP_VSXU
: /* VSX unavailable exception */
428 case POWERPC_EXCP_FU
: /* Facility unavailable exception */
430 env
->spr
[SPR_FSCR
] |= ((target_ulong
)env
->error_code
<< 56);
433 case POWERPC_EXCP_PIT
: /* Programmable interval timer interrupt */
434 LOG_EXCP("PIT exception\n");
436 case POWERPC_EXCP_IO
: /* IO error exception */
438 cpu_abort(cs
, "601 IO error exception is not implemented yet !\n");
440 case POWERPC_EXCP_RUNM
: /* Run mode exception */
442 cpu_abort(cs
, "601 run mode exception is not implemented yet !\n");
444 case POWERPC_EXCP_EMUL
: /* Emulation trap exception */
446 cpu_abort(cs
, "602 emulation trap exception "
447 "is not implemented yet !\n");
449 case POWERPC_EXCP_IFTLB
: /* Instruction fetch TLB error */
450 switch (excp_model
) {
451 case POWERPC_EXCP_602
:
452 case POWERPC_EXCP_603
:
453 case POWERPC_EXCP_603E
:
454 case POWERPC_EXCP_G2
:
456 case POWERPC_EXCP_7x5
:
458 case POWERPC_EXCP_74xx
:
461 cpu_abort(cs
, "Invalid instruction TLB miss exception\n");
465 case POWERPC_EXCP_DLTLB
: /* Data load TLB miss */
466 switch (excp_model
) {
467 case POWERPC_EXCP_602
:
468 case POWERPC_EXCP_603
:
469 case POWERPC_EXCP_603E
:
470 case POWERPC_EXCP_G2
:
472 case POWERPC_EXCP_7x5
:
474 case POWERPC_EXCP_74xx
:
477 cpu_abort(cs
, "Invalid data load TLB miss exception\n");
481 case POWERPC_EXCP_DSTLB
: /* Data store TLB miss */
482 switch (excp_model
) {
483 case POWERPC_EXCP_602
:
484 case POWERPC_EXCP_603
:
485 case POWERPC_EXCP_603E
:
486 case POWERPC_EXCP_G2
:
488 /* Swap temporary saved registers with GPRs */
489 if (!(new_msr
& ((target_ulong
)1 << MSR_TGPR
))) {
490 new_msr
|= (target_ulong
)1 << MSR_TGPR
;
491 hreg_swap_gpr_tgpr(env
);
494 case POWERPC_EXCP_7x5
:
496 #if defined(DEBUG_SOFTWARE_TLB)
497 if (qemu_log_enabled()) {
499 target_ulong
*miss
, *cmp
;
502 if (excp
== POWERPC_EXCP_IFTLB
) {
505 miss
= &env
->spr
[SPR_IMISS
];
506 cmp
= &env
->spr
[SPR_ICMP
];
508 if (excp
== POWERPC_EXCP_DLTLB
) {
514 miss
= &env
->spr
[SPR_DMISS
];
515 cmp
= &env
->spr
[SPR_DCMP
];
517 qemu_log("6xx %sTLB miss: %cM " TARGET_FMT_lx
" %cC "
518 TARGET_FMT_lx
" H1 " TARGET_FMT_lx
" H2 "
519 TARGET_FMT_lx
" %08x\n", es
, en
, *miss
, en
, *cmp
,
520 env
->spr
[SPR_HASH1
], env
->spr
[SPR_HASH2
],
524 msr
|= env
->crf
[0] << 28;
525 msr
|= env
->error_code
; /* key, D/I, S/L bits */
526 /* Set way using a LRU mechanism */
527 msr
|= ((env
->last_way
+ 1) & (env
->nb_ways
- 1)) << 17;
529 case POWERPC_EXCP_74xx
:
531 #if defined(DEBUG_SOFTWARE_TLB)
532 if (qemu_log_enabled()) {
534 target_ulong
*miss
, *cmp
;
537 if (excp
== POWERPC_EXCP_IFTLB
) {
540 miss
= &env
->spr
[SPR_TLBMISS
];
541 cmp
= &env
->spr
[SPR_PTEHI
];
543 if (excp
== POWERPC_EXCP_DLTLB
) {
549 miss
= &env
->spr
[SPR_TLBMISS
];
550 cmp
= &env
->spr
[SPR_PTEHI
];
552 qemu_log("74xx %sTLB miss: %cM " TARGET_FMT_lx
" %cC "
553 TARGET_FMT_lx
" %08x\n", es
, en
, *miss
, en
, *cmp
,
557 msr
|= env
->error_code
; /* key bit */
560 cpu_abort(cs
, "Invalid data store TLB miss exception\n");
564 case POWERPC_EXCP_FPA
: /* Floating-point assist exception */
566 cpu_abort(cs
, "Floating point assist exception "
567 "is not implemented yet !\n");
569 case POWERPC_EXCP_DABR
: /* Data address breakpoint */
571 cpu_abort(cs
, "DABR exception is not implemented yet !\n");
573 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
575 cpu_abort(cs
, "IABR exception is not implemented yet !\n");
577 case POWERPC_EXCP_SMI
: /* System management interrupt */
579 cpu_abort(cs
, "SMI exception is not implemented yet !\n");
581 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
583 cpu_abort(cs
, "Thermal management exception "
584 "is not implemented yet !\n");
586 case POWERPC_EXCP_PERFM
: /* Embedded performance monitor interrupt */
589 "Performance counter exception is not implemented yet !\n");
591 case POWERPC_EXCP_VPUA
: /* Vector assist exception */
593 cpu_abort(cs
, "VPU assist exception is not implemented yet !\n");
595 case POWERPC_EXCP_SOFTP
: /* Soft patch exception */
598 "970 soft-patch exception is not implemented yet !\n");
600 case POWERPC_EXCP_MAINT
: /* Maintenance exception */
603 "970 maintenance exception is not implemented yet !\n");
605 case POWERPC_EXCP_MEXTBR
: /* Maskable external breakpoint */
607 cpu_abort(cs
, "Maskable external exception "
608 "is not implemented yet !\n");
610 case POWERPC_EXCP_NMEXTBR
: /* Non maskable external breakpoint */
612 cpu_abort(cs
, "Non maskable external exception "
613 "is not implemented yet !\n");
617 cpu_abort(cs
, "Invalid PowerPC exception %d. Aborting\n", excp
);
622 env
->spr
[srr0
] = env
->nip
;
625 env
->spr
[srr1
] = msr
;
628 if (!(env
->msr_mask
& MSR_HVB
)) {
629 if (new_msr
& MSR_HVB
) {
630 cpu_abort(cs
, "Trying to deliver HV exception (MSR) %d with "
631 "no HV support\n", excp
);
633 if (srr0
== SPR_HSRR0
) {
634 cpu_abort(cs
, "Trying to deliver HV exception (HSRR) %d with "
635 "no HV support\n", excp
);
639 /* If any alternate SRR register are defined, duplicate saved values */
641 env
->spr
[asrr0
] = env
->spr
[srr0
];
644 env
->spr
[asrr1
] = env
->spr
[srr1
];
647 /* Sort out endianness of interrupt, this differs depending on the
648 * CPU, the HV mode, etc...
651 if (excp_model
== POWERPC_EXCP_POWER7
) {
652 if (!(new_msr
& MSR_HVB
) && (env
->spr
[SPR_LPCR
] & LPCR_ILE
)) {
653 new_msr
|= (target_ulong
)1 << MSR_LE
;
655 } else if (excp_model
== POWERPC_EXCP_POWER8
) {
656 if (new_msr
& MSR_HVB
) {
657 if (env
->spr
[SPR_HID0
] & HID0_HILE
) {
658 new_msr
|= (target_ulong
)1 << MSR_LE
;
660 } else if (env
->spr
[SPR_LPCR
] & LPCR_ILE
) {
661 new_msr
|= (target_ulong
)1 << MSR_LE
;
663 } else if (msr_ile
) {
664 new_msr
|= (target_ulong
)1 << MSR_LE
;
668 new_msr
|= (target_ulong
)1 << MSR_LE
;
672 /* Jump to handler */
673 vector
= env
->excp_vectors
[excp
];
674 if (vector
== (target_ulong
)-1ULL) {
675 cpu_abort(cs
, "Raised an exception without defined vector %d\n",
678 vector
|= env
->excp_prefix
;
680 /* AIL only works if there is no HV transition and we are running with
681 * translations enabled
683 if (!((msr
>> MSR_IR
) & 1) || !((msr
>> MSR_DR
) & 1) ||
684 ((new_msr
& MSR_HVB
) && !(msr
& MSR_HVB
))) {
689 new_msr
|= (1 << MSR_IR
) | (1 << MSR_DR
);
694 case AIL_C000_0000_0000_4000
:
695 vector
|= 0xc000000000004000ull
;
698 cpu_abort(cs
, "Invalid AIL combination %d\n", ail
);
703 #if defined(TARGET_PPC64)
704 if (excp_model
== POWERPC_EXCP_BOOKE
) {
705 if (env
->spr
[SPR_BOOKE_EPCR
] & EPCR_ICM
) {
706 /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */
707 new_msr
|= (target_ulong
)1 << MSR_CM
;
709 vector
= (uint32_t)vector
;
712 if (!msr_isf
&& !(env
->mmu_model
& POWERPC_MMU_64
)) {
713 vector
= (uint32_t)vector
;
715 new_msr
|= (target_ulong
)1 << MSR_SF
;
719 /* We don't use hreg_store_msr here as already have treated
720 * any special case that could occur. Just store MSR and update hflags
722 * Note: We *MUST* not use hreg_store_msr() as-is anyway because it
723 * will prevent setting of the HV bit which some exceptions might need
726 env
->msr
= new_msr
& env
->msr_mask
;
727 hreg_compute_hflags(env
);
729 /* Reset exception state */
730 cs
->exception_index
= POWERPC_EXCP_NONE
;
733 /* Reset the reservation */
734 env
->reserve_addr
= -1;
736 /* Any interrupt is context synchronizing, check if TCG TLB
737 * needs a delayed flush on ppc64
739 check_tlb_flush(env
, false);
742 void ppc_cpu_do_interrupt(CPUState
*cs
)
744 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
745 CPUPPCState
*env
= &cpu
->env
;
747 powerpc_excp(cpu
, env
->excp_model
, cs
->exception_index
);
750 static void ppc_hw_interrupt(CPUPPCState
*env
)
752 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
754 CPUState
*cs
= CPU(cpu
);
756 qemu_log_mask(CPU_LOG_INT
, "%s: %p pending %08x req %08x me %d ee %d\n",
757 __func__
, env
, env
->pending_interrupts
,
758 cs
->interrupt_request
, (int)msr_me
, (int)msr_ee
);
761 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_RESET
)) {
762 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_RESET
);
763 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_RESET
);
766 /* Machine check exception */
767 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_MCK
)) {
768 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_MCK
);
769 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_MCHECK
);
773 /* External debug exception */
774 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DEBUG
)) {
775 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DEBUG
);
776 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_DEBUG
);
780 /* Hypervisor decrementer exception */
781 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_HDECR
)) {
782 /* LPCR will be clear when not supported so this will work */
783 bool hdice
= !!(env
->spr
[SPR_LPCR
] & LPCR_HDICE
);
784 if ((msr_ee
!= 0 || msr_hv
== 0) && hdice
) {
785 /* HDEC clears on delivery */
786 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_HDECR
);
787 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_HDECR
);
791 /* Extermal interrupt can ignore MSR:EE under some circumstances */
792 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_EXT
)) {
793 bool lpes0
= !!(env
->spr
[SPR_LPCR
] & LPCR_LPES0
);
794 if (msr_ee
!= 0 || (env
->has_hv_mode
&& msr_hv
== 0 && !lpes0
)) {
795 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_EXTERNAL
);
800 /* External critical interrupt */
801 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_CEXT
)) {
802 /* Taking a critical external interrupt does not clear the external
803 * critical interrupt status
806 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_CEXT
);
808 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_CRITICAL
);
813 /* Watchdog timer on embedded PowerPC */
814 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_WDT
)) {
815 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_WDT
);
816 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_WDT
);
819 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_CDOORBELL
)) {
820 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_CDOORBELL
);
821 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_DOORCI
);
824 /* Fixed interval timer on embedded PowerPC */
825 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_FIT
)) {
826 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_FIT
);
827 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_FIT
);
830 /* Programmable interval timer on embedded PowerPC */
831 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_PIT
)) {
832 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_PIT
);
833 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_PIT
);
836 /* Decrementer exception */
837 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DECR
)) {
838 if (ppc_decr_clear_on_delivery(env
)) {
839 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DECR
);
841 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_DECR
);
844 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DOORBELL
)) {
845 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DOORBELL
);
846 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_DOORI
);
849 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_PERFM
)) {
850 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_PERFM
);
851 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_PERFM
);
854 /* Thermal interrupt */
855 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_THERM
)) {
856 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_THERM
);
857 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_THERM
);
863 void ppc_cpu_do_system_reset(CPUState
*cs
)
865 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
866 CPUPPCState
*env
= &cpu
->env
;
868 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_RESET
);
870 #endif /* !CONFIG_USER_ONLY */
872 bool ppc_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
874 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
875 CPUPPCState
*env
= &cpu
->env
;
877 if (interrupt_request
& CPU_INTERRUPT_HARD
) {
878 ppc_hw_interrupt(env
);
879 if (env
->pending_interrupts
== 0) {
880 cs
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
887 #if defined(DEBUG_OP)
888 static void cpu_dump_rfi(target_ulong RA
, target_ulong msr
)
890 qemu_log("Return from exception at " TARGET_FMT_lx
" with flags "
891 TARGET_FMT_lx
"\n", RA
, msr
);
895 /*****************************************************************************/
896 /* Exceptions processing helpers */
898 void raise_exception_err_ra(CPUPPCState
*env
, uint32_t exception
,
899 uint32_t error_code
, uintptr_t raddr
)
901 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
903 cs
->exception_index
= exception
;
904 env
->error_code
= error_code
;
905 cpu_loop_exit_restore(cs
, raddr
);
908 void raise_exception_err(CPUPPCState
*env
, uint32_t exception
,
911 raise_exception_err_ra(env
, exception
, error_code
, 0);
914 void raise_exception(CPUPPCState
*env
, uint32_t exception
)
916 raise_exception_err_ra(env
, exception
, 0, 0);
919 void raise_exception_ra(CPUPPCState
*env
, uint32_t exception
,
922 raise_exception_err_ra(env
, exception
, 0, raddr
);
925 void helper_raise_exception_err(CPUPPCState
*env
, uint32_t exception
,
928 raise_exception_err_ra(env
, exception
, error_code
, 0);
931 void helper_raise_exception(CPUPPCState
*env
, uint32_t exception
)
933 raise_exception_err_ra(env
, exception
, 0, 0);
936 #if !defined(CONFIG_USER_ONLY)
937 void helper_store_msr(CPUPPCState
*env
, target_ulong val
)
939 uint32_t excp
= hreg_store_msr(env
, val
, 0);
942 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
943 cs
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
944 raise_exception(env
, excp
);
948 #if defined(TARGET_PPC64)
949 void helper_pminsn(CPUPPCState
*env
, powerpc_pm_insn_t insn
)
953 cs
= CPU(ppc_env_get_cpu(env
));
955 env
->in_pm_state
= true;
957 /* The architecture specifies that HDEC interrupts are
958 * discarded in PM states
960 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_HDECR
);
962 /* Technically, nap doesn't set EE, but if we don't set it
963 * then ppc_hw_interrupt() won't deliver. We could add some
964 * other tests there based on LPCR but it's simpler to just
965 * whack EE in. It will be cleared by the 0x100 at wakeup
966 * anyway. It will still be observable by the guest in SRR1
967 * but this doesn't seem to be a problem.
969 env
->msr
|= (1ull << MSR_EE
);
970 raise_exception(env
, EXCP_HLT
);
972 #endif /* defined(TARGET_PPC64) */
974 static inline void do_rfi(CPUPPCState
*env
, target_ulong nip
, target_ulong msr
)
976 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
978 /* MSR:POW cannot be set by any form of rfi */
979 msr
&= ~(1ULL << MSR_POW
);
981 #if defined(TARGET_PPC64)
982 /* Switching to 32-bit ? Crop the nip */
983 if (!msr_is_64bit(env
, msr
)) {
989 /* XXX: beware: this is false if VLE is supported */
990 env
->nip
= nip
& ~((target_ulong
)0x00000003);
991 hreg_store_msr(env
, msr
, 1);
992 #if defined(DEBUG_OP)
993 cpu_dump_rfi(env
->nip
, env
->msr
);
995 /* No need to raise an exception here,
996 * as rfi is always the last insn of a TB
998 cs
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
1000 /* Reset the reservation */
1001 env
->reserve_addr
= -1;
1003 /* Context synchronizing: check if TCG TLB needs flush */
1004 check_tlb_flush(env
, false);
1007 void helper_rfi(CPUPPCState
*env
)
1009 do_rfi(env
, env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
] & 0xfffffffful
);
1012 #define MSR_BOOK3S_MASK
1013 #if defined(TARGET_PPC64)
1014 void helper_rfid(CPUPPCState
*env
)
1016 /* The architeture defines a number of rules for which bits
1017 * can change but in practice, we handle this in hreg_store_msr()
1018 * which will be called by do_rfi(), so there is no need to filter
1021 do_rfi(env
, env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
]);
1024 void helper_hrfid(CPUPPCState
*env
)
1026 do_rfi(env
, env
->spr
[SPR_HSRR0
], env
->spr
[SPR_HSRR1
]);
1030 /*****************************************************************************/
1031 /* Embedded PowerPC specific helpers */
1032 void helper_40x_rfci(CPUPPCState
*env
)
1034 do_rfi(env
, env
->spr
[SPR_40x_SRR2
], env
->spr
[SPR_40x_SRR3
]);
1037 void helper_rfci(CPUPPCState
*env
)
1039 do_rfi(env
, env
->spr
[SPR_BOOKE_CSRR0
], env
->spr
[SPR_BOOKE_CSRR1
]);
1042 void helper_rfdi(CPUPPCState
*env
)
1044 /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
1045 do_rfi(env
, env
->spr
[SPR_BOOKE_DSRR0
], env
->spr
[SPR_BOOKE_DSRR1
]);
1048 void helper_rfmci(CPUPPCState
*env
)
1050 /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
1051 do_rfi(env
, env
->spr
[SPR_BOOKE_MCSRR0
], env
->spr
[SPR_BOOKE_MCSRR1
]);
1055 void helper_tw(CPUPPCState
*env
, target_ulong arg1
, target_ulong arg2
,
1058 if (!likely(!(((int32_t)arg1
< (int32_t)arg2
&& (flags
& 0x10)) ||
1059 ((int32_t)arg1
> (int32_t)arg2
&& (flags
& 0x08)) ||
1060 ((int32_t)arg1
== (int32_t)arg2
&& (flags
& 0x04)) ||
1061 ((uint32_t)arg1
< (uint32_t)arg2
&& (flags
& 0x02)) ||
1062 ((uint32_t)arg1
> (uint32_t)arg2
&& (flags
& 0x01))))) {
1063 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
1064 POWERPC_EXCP_TRAP
, GETPC());
1068 #if defined(TARGET_PPC64)
1069 void helper_td(CPUPPCState
*env
, target_ulong arg1
, target_ulong arg2
,
1072 if (!likely(!(((int64_t)arg1
< (int64_t)arg2
&& (flags
& 0x10)) ||
1073 ((int64_t)arg1
> (int64_t)arg2
&& (flags
& 0x08)) ||
1074 ((int64_t)arg1
== (int64_t)arg2
&& (flags
& 0x04)) ||
1075 ((uint64_t)arg1
< (uint64_t)arg2
&& (flags
& 0x02)) ||
1076 ((uint64_t)arg1
> (uint64_t)arg2
&& (flags
& 0x01))))) {
1077 raise_exception_err_ra(env
, POWERPC_EXCP_PROGRAM
,
1078 POWERPC_EXCP_TRAP
, GETPC());
1083 #if !defined(CONFIG_USER_ONLY)
1084 /*****************************************************************************/
1085 /* PowerPC 601 specific instructions (POWER bridge) */
1087 void helper_rfsvc(CPUPPCState
*env
)
1089 do_rfi(env
, env
->lr
, env
->ctr
& 0x0000FFFF);
1092 /* Embedded.Processor Control */
1093 static int dbell2irq(target_ulong rb
)
1095 int msg
= rb
& DBELL_TYPE_MASK
;
1099 case DBELL_TYPE_DBELL
:
1100 irq
= PPC_INTERRUPT_DOORBELL
;
1102 case DBELL_TYPE_DBELL_CRIT
:
1103 irq
= PPC_INTERRUPT_CDOORBELL
;
1105 case DBELL_TYPE_G_DBELL
:
1106 case DBELL_TYPE_G_DBELL_CRIT
:
1107 case DBELL_TYPE_G_DBELL_MC
:
1116 void helper_msgclr(CPUPPCState
*env
, target_ulong rb
)
1118 int irq
= dbell2irq(rb
);
1124 env
->pending_interrupts
&= ~(1 << irq
);
1127 void helper_msgsnd(target_ulong rb
)
1129 int irq
= dbell2irq(rb
);
1130 int pir
= rb
& DBELL_PIRTAG_MASK
;
1137 qemu_mutex_lock_iothread();
1139 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
1140 CPUPPCState
*cenv
= &cpu
->env
;
1142 if ((rb
& DBELL_BRDCAST
) || (cenv
->spr
[SPR_BOOKE_PIR
] == pir
)) {
1143 cenv
->pending_interrupts
|= 1 << irq
;
1144 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);
1147 qemu_mutex_unlock_iothread();