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"
21 #include "exec/helper-proto.h"
22 #include "exec/exec-all.h"
23 #include "exec/cpu_ldst.h"
25 #include "helper_regs.h"
28 //#define DEBUG_SOFTWARE_TLB
29 //#define DEBUG_EXCEPTIONS
31 #ifdef DEBUG_EXCEPTIONS
32 # define LOG_EXCP(...) qemu_log(__VA_ARGS__)
34 # define LOG_EXCP(...) do { } while (0)
37 /*****************************************************************************/
38 /* PowerPC Hypercall emulation */
40 void (*cpu_ppc_hypercall
)(PowerPCCPU
*);
42 /*****************************************************************************/
43 /* Exception processing */
44 #if defined(CONFIG_USER_ONLY)
45 void ppc_cpu_do_interrupt(CPUState
*cs
)
47 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
48 CPUPPCState
*env
= &cpu
->env
;
50 cs
->exception_index
= POWERPC_EXCP_NONE
;
54 static void ppc_hw_interrupt(CPUPPCState
*env
)
56 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
58 cs
->exception_index
= POWERPC_EXCP_NONE
;
61 #else /* defined(CONFIG_USER_ONLY) */
62 static inline void dump_syscall(CPUPPCState
*env
)
64 qemu_log_mask(CPU_LOG_INT
, "syscall r0=%016" PRIx64
" r3=%016" PRIx64
65 " r4=%016" PRIx64
" r5=%016" PRIx64
" r6=%016" PRIx64
66 " nip=" TARGET_FMT_lx
"\n",
67 ppc_dump_gpr(env
, 0), ppc_dump_gpr(env
, 3),
68 ppc_dump_gpr(env
, 4), ppc_dump_gpr(env
, 5),
69 ppc_dump_gpr(env
, 6), env
->nip
);
72 /* Note that this function should be greatly optimized
73 * when called with a constant excp, from ppc_hw_interrupt
75 static inline void powerpc_excp(PowerPCCPU
*cpu
, int excp_model
, int excp
)
77 CPUState
*cs
= CPU(cpu
);
78 CPUPPCState
*env
= &cpu
->env
;
79 target_ulong msr
, new_msr
, vector
;
80 int srr0
, srr1
, asrr0
, asrr1
, lev
, ail
;
83 qemu_log_mask(CPU_LOG_INT
, "Raise exception at " TARGET_FMT_lx
84 " => %08x (%02x)\n", env
->nip
, excp
, env
->error_code
);
86 /* new srr1 value excluding must-be-zero bits */
87 if (excp_model
== POWERPC_EXCP_BOOKE
) {
90 msr
= env
->msr
& ~0x783f0000ULL
;
93 /* new interrupt handler msr preserves existing HV and ME unless
94 * explicitly overriden
96 new_msr
= env
->msr
& (((target_ulong
)1 << MSR_ME
) | MSR_HVB
);
98 /* target registers */
104 /* Exception targetting modifiers
106 * LPES0 is supported on POWER7/8
107 * LPES1 is not supported (old iSeries mode)
109 * On anything else, we behave as if LPES0 is 1
110 * (externals don't alter MSR:HV)
112 * AIL is initialized here but can be cleared by
113 * selected exceptions
115 #if defined(TARGET_PPC64)
116 if (excp_model
== POWERPC_EXCP_POWER7
||
117 excp_model
== POWERPC_EXCP_POWER8
) {
118 lpes0
= !!(env
->spr
[SPR_LPCR
] & LPCR_LPES0
);
119 if (excp_model
== POWERPC_EXCP_POWER8
) {
120 ail
= (env
->spr
[SPR_LPCR
] & LPCR_AIL
) >> LPCR_AIL_SHIFT
;
125 #endif /* defined(TARGET_PPC64) */
132 case POWERPC_EXCP_NONE
:
133 /* Should never happen */
135 case POWERPC_EXCP_CRITICAL
: /* Critical input */
136 switch (excp_model
) {
137 case POWERPC_EXCP_40x
:
141 case POWERPC_EXCP_BOOKE
:
142 srr0
= SPR_BOOKE_CSRR0
;
143 srr1
= SPR_BOOKE_CSRR1
;
145 case POWERPC_EXCP_G2
:
151 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
153 /* Machine check exception is not enabled.
154 * Enter checkstop state.
156 fprintf(stderr
, "Machine check while not allowed. "
157 "Entering checkstop state\n");
158 if (qemu_log_separate()) {
159 qemu_log("Machine check while not allowed. "
160 "Entering checkstop state\n");
163 cs
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
165 new_msr
|= (target_ulong
)MSR_HVB
;
168 /* machine check exceptions don't have ME set */
169 new_msr
&= ~((target_ulong
)1 << MSR_ME
);
171 /* XXX: should also have something loaded in DAR / DSISR */
172 switch (excp_model
) {
173 case POWERPC_EXCP_40x
:
177 case POWERPC_EXCP_BOOKE
:
178 /* FIXME: choose one or the other based on CPU type */
179 srr0
= SPR_BOOKE_MCSRR0
;
180 srr1
= SPR_BOOKE_MCSRR1
;
181 asrr0
= SPR_BOOKE_CSRR0
;
182 asrr1
= SPR_BOOKE_CSRR1
;
188 case POWERPC_EXCP_DSI
: /* Data storage exception */
189 LOG_EXCP("DSI exception: DSISR=" TARGET_FMT_lx
" DAR=" TARGET_FMT_lx
190 "\n", env
->spr
[SPR_DSISR
], env
->spr
[SPR_DAR
]);
192 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
193 LOG_EXCP("ISI exception: msr=" TARGET_FMT_lx
", nip=" TARGET_FMT_lx
194 "\n", msr
, env
->nip
);
195 msr
|= env
->error_code
;
197 case POWERPC_EXCP_EXTERNAL
: /* External input */
201 new_msr
|= (target_ulong
)MSR_HVB
;
202 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_RI
);
206 if (env
->mpic_proxy
) {
207 /* IACK the IRQ on delivery */
208 env
->spr
[SPR_BOOKE_EPR
] = ldl_phys(cs
->as
, env
->mpic_iack
);
211 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
212 /* XXX: this is false */
213 /* Get rS/rD and rA from faulting opcode */
214 env
->spr
[SPR_DSISR
] |= (cpu_ldl_code(env
, (env
->nip
- 4))
217 case POWERPC_EXCP_PROGRAM
: /* Program exception */
218 switch (env
->error_code
& ~0xF) {
219 case POWERPC_EXCP_FP
:
220 if ((msr_fe0
== 0 && msr_fe1
== 0) || msr_fp
== 0) {
221 LOG_EXCP("Ignore floating point exception\n");
222 cs
->exception_index
= POWERPC_EXCP_NONE
;
227 if (msr_fe0
== msr_fe1
) {
232 case POWERPC_EXCP_INVAL
:
233 LOG_EXCP("Invalid instruction at " TARGET_FMT_lx
"\n", env
->nip
);
235 env
->spr
[SPR_BOOKE_ESR
] = ESR_PIL
;
237 case POWERPC_EXCP_PRIV
:
239 env
->spr
[SPR_BOOKE_ESR
] = ESR_PPR
;
241 case POWERPC_EXCP_TRAP
:
243 env
->spr
[SPR_BOOKE_ESR
] = ESR_PTR
;
246 /* Should never occur */
247 cpu_abort(cs
, "Invalid program exception %d. Aborting\n",
252 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
254 case POWERPC_EXCP_SYSCALL
: /* System call exception */
256 lev
= env
->error_code
;
258 /* "PAPR mode" built-in hypercall emulation */
259 if ((lev
== 1) && cpu_ppc_hypercall
) {
260 cpu_ppc_hypercall(cpu
);
264 new_msr
|= (target_ulong
)MSR_HVB
;
267 case POWERPC_EXCP_APU
: /* Auxiliary processor unavailable */
269 case POWERPC_EXCP_DECR
: /* Decrementer exception */
271 case POWERPC_EXCP_FIT
: /* Fixed-interval timer interrupt */
273 LOG_EXCP("FIT exception\n");
275 case POWERPC_EXCP_WDT
: /* Watchdog timer interrupt */
276 LOG_EXCP("WDT exception\n");
277 switch (excp_model
) {
278 case POWERPC_EXCP_BOOKE
:
279 srr0
= SPR_BOOKE_CSRR0
;
280 srr1
= SPR_BOOKE_CSRR1
;
286 case POWERPC_EXCP_DTLB
: /* Data TLB error */
288 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
290 case POWERPC_EXCP_DEBUG
: /* Debug interrupt */
291 switch (excp_model
) {
292 case POWERPC_EXCP_BOOKE
:
293 /* FIXME: choose one or the other based on CPU type */
294 srr0
= SPR_BOOKE_DSRR0
;
295 srr1
= SPR_BOOKE_DSRR1
;
296 asrr0
= SPR_BOOKE_CSRR0
;
297 asrr1
= SPR_BOOKE_CSRR1
;
303 cpu_abort(cs
, "Debug exception is not implemented yet !\n");
305 case POWERPC_EXCP_SPEU
: /* SPE/embedded floating-point unavailable */
306 env
->spr
[SPR_BOOKE_ESR
] = ESR_SPV
;
308 case POWERPC_EXCP_EFPDI
: /* Embedded floating-point data interrupt */
310 cpu_abort(cs
, "Embedded floating point data exception "
311 "is not implemented yet !\n");
312 env
->spr
[SPR_BOOKE_ESR
] = ESR_SPV
;
314 case POWERPC_EXCP_EFPRI
: /* Embedded floating-point round interrupt */
316 cpu_abort(cs
, "Embedded floating point round exception "
317 "is not implemented yet !\n");
318 env
->spr
[SPR_BOOKE_ESR
] = ESR_SPV
;
320 case POWERPC_EXCP_EPERFM
: /* Embedded performance monitor interrupt */
323 "Performance counter exception is not implemented yet !\n");
325 case POWERPC_EXCP_DOORI
: /* Embedded doorbell interrupt */
327 case POWERPC_EXCP_DOORCI
: /* Embedded doorbell critical interrupt */
328 srr0
= SPR_BOOKE_CSRR0
;
329 srr1
= SPR_BOOKE_CSRR1
;
331 case POWERPC_EXCP_RESET
: /* System reset exception */
333 /* indicate that we resumed from power save mode */
336 new_msr
&= ~((target_ulong
)1 << MSR_ME
);
339 new_msr
|= (target_ulong
)MSR_HVB
;
342 case POWERPC_EXCP_DSEG
: /* Data segment exception */
344 case POWERPC_EXCP_ISEG
: /* Instruction segment exception */
346 case POWERPC_EXCP_HDECR
: /* Hypervisor decrementer exception */
349 new_msr
|= (target_ulong
)MSR_HVB
;
350 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_RI
);
352 case POWERPC_EXCP_TRACE
: /* Trace exception */
354 case POWERPC_EXCP_HDSI
: /* Hypervisor data storage exception */
357 new_msr
|= (target_ulong
)MSR_HVB
;
358 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_RI
);
360 case POWERPC_EXCP_HISI
: /* Hypervisor instruction storage exception */
363 new_msr
|= (target_ulong
)MSR_HVB
;
364 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_RI
);
366 case POWERPC_EXCP_HDSEG
: /* Hypervisor data segment exception */
369 new_msr
|= (target_ulong
)MSR_HVB
;
370 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_RI
);
372 case POWERPC_EXCP_HISEG
: /* Hypervisor instruction segment exception */
375 new_msr
|= (target_ulong
)MSR_HVB
;
376 new_msr
|= env
->msr
& ((target_ulong
)1 << MSR_RI
);
378 case POWERPC_EXCP_VPU
: /* Vector unavailable exception */
380 case POWERPC_EXCP_VSXU
: /* VSX unavailable exception */
382 case POWERPC_EXCP_FU
: /* Facility unavailable exception */
384 case POWERPC_EXCP_PIT
: /* Programmable interval timer interrupt */
385 LOG_EXCP("PIT exception\n");
387 case POWERPC_EXCP_IO
: /* IO error exception */
389 cpu_abort(cs
, "601 IO error exception is not implemented yet !\n");
391 case POWERPC_EXCP_RUNM
: /* Run mode exception */
393 cpu_abort(cs
, "601 run mode exception is not implemented yet !\n");
395 case POWERPC_EXCP_EMUL
: /* Emulation trap exception */
397 cpu_abort(cs
, "602 emulation trap exception "
398 "is not implemented yet !\n");
400 case POWERPC_EXCP_IFTLB
: /* Instruction fetch TLB error */
401 switch (excp_model
) {
402 case POWERPC_EXCP_602
:
403 case POWERPC_EXCP_603
:
404 case POWERPC_EXCP_603E
:
405 case POWERPC_EXCP_G2
:
407 case POWERPC_EXCP_7x5
:
409 case POWERPC_EXCP_74xx
:
412 cpu_abort(cs
, "Invalid instruction TLB miss exception\n");
416 case POWERPC_EXCP_DLTLB
: /* Data load TLB miss */
417 switch (excp_model
) {
418 case POWERPC_EXCP_602
:
419 case POWERPC_EXCP_603
:
420 case POWERPC_EXCP_603E
:
421 case POWERPC_EXCP_G2
:
423 case POWERPC_EXCP_7x5
:
425 case POWERPC_EXCP_74xx
:
428 cpu_abort(cs
, "Invalid data load TLB miss exception\n");
432 case POWERPC_EXCP_DSTLB
: /* Data store TLB miss */
433 switch (excp_model
) {
434 case POWERPC_EXCP_602
:
435 case POWERPC_EXCP_603
:
436 case POWERPC_EXCP_603E
:
437 case POWERPC_EXCP_G2
:
439 /* Swap temporary saved registers with GPRs */
440 if (!(new_msr
& ((target_ulong
)1 << MSR_TGPR
))) {
441 new_msr
|= (target_ulong
)1 << MSR_TGPR
;
442 hreg_swap_gpr_tgpr(env
);
445 case POWERPC_EXCP_7x5
:
447 #if defined(DEBUG_SOFTWARE_TLB)
448 if (qemu_log_enabled()) {
450 target_ulong
*miss
, *cmp
;
453 if (excp
== POWERPC_EXCP_IFTLB
) {
456 miss
= &env
->spr
[SPR_IMISS
];
457 cmp
= &env
->spr
[SPR_ICMP
];
459 if (excp
== POWERPC_EXCP_DLTLB
) {
465 miss
= &env
->spr
[SPR_DMISS
];
466 cmp
= &env
->spr
[SPR_DCMP
];
468 qemu_log("6xx %sTLB miss: %cM " TARGET_FMT_lx
" %cC "
469 TARGET_FMT_lx
" H1 " TARGET_FMT_lx
" H2 "
470 TARGET_FMT_lx
" %08x\n", es
, en
, *miss
, en
, *cmp
,
471 env
->spr
[SPR_HASH1
], env
->spr
[SPR_HASH2
],
475 msr
|= env
->crf
[0] << 28;
476 msr
|= env
->error_code
; /* key, D/I, S/L bits */
477 /* Set way using a LRU mechanism */
478 msr
|= ((env
->last_way
+ 1) & (env
->nb_ways
- 1)) << 17;
480 case POWERPC_EXCP_74xx
:
482 #if defined(DEBUG_SOFTWARE_TLB)
483 if (qemu_log_enabled()) {
485 target_ulong
*miss
, *cmp
;
488 if (excp
== POWERPC_EXCP_IFTLB
) {
491 miss
= &env
->spr
[SPR_TLBMISS
];
492 cmp
= &env
->spr
[SPR_PTEHI
];
494 if (excp
== POWERPC_EXCP_DLTLB
) {
500 miss
= &env
->spr
[SPR_TLBMISS
];
501 cmp
= &env
->spr
[SPR_PTEHI
];
503 qemu_log("74xx %sTLB miss: %cM " TARGET_FMT_lx
" %cC "
504 TARGET_FMT_lx
" %08x\n", es
, en
, *miss
, en
, *cmp
,
508 msr
|= env
->error_code
; /* key bit */
511 cpu_abort(cs
, "Invalid data store TLB miss exception\n");
515 case POWERPC_EXCP_FPA
: /* Floating-point assist exception */
517 cpu_abort(cs
, "Floating point assist exception "
518 "is not implemented yet !\n");
520 case POWERPC_EXCP_DABR
: /* Data address breakpoint */
522 cpu_abort(cs
, "DABR exception is not implemented yet !\n");
524 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
526 cpu_abort(cs
, "IABR exception is not implemented yet !\n");
528 case POWERPC_EXCP_SMI
: /* System management interrupt */
530 cpu_abort(cs
, "SMI exception is not implemented yet !\n");
532 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
534 cpu_abort(cs
, "Thermal management exception "
535 "is not implemented yet !\n");
537 case POWERPC_EXCP_PERFM
: /* Embedded performance monitor interrupt */
540 "Performance counter exception is not implemented yet !\n");
542 case POWERPC_EXCP_VPUA
: /* Vector assist exception */
544 cpu_abort(cs
, "VPU assist exception is not implemented yet !\n");
546 case POWERPC_EXCP_SOFTP
: /* Soft patch exception */
549 "970 soft-patch exception is not implemented yet !\n");
551 case POWERPC_EXCP_MAINT
: /* Maintenance exception */
554 "970 maintenance exception is not implemented yet !\n");
556 case POWERPC_EXCP_MEXTBR
: /* Maskable external breakpoint */
558 cpu_abort(cs
, "Maskable external exception "
559 "is not implemented yet !\n");
561 case POWERPC_EXCP_NMEXTBR
: /* Non maskable external breakpoint */
563 cpu_abort(cs
, "Non maskable external exception "
564 "is not implemented yet !\n");
568 cpu_abort(cs
, "Invalid PowerPC exception %d. Aborting\n", excp
);
571 /* save current instruction location */
572 env
->spr
[srr0
] = env
->nip
- 4;
575 /* save next instruction location */
576 env
->spr
[srr0
] = env
->nip
;
580 env
->spr
[srr1
] = msr
;
583 if (!(env
->msr_mask
& MSR_HVB
) && (srr0
== SPR_HSRR0
)) {
584 cpu_abort(cs
, "Trying to deliver HV exception %d with "
585 "no HV support\n", excp
);
588 /* If any alternate SRR register are defined, duplicate saved values */
590 env
->spr
[asrr0
] = env
->spr
[srr0
];
593 env
->spr
[asrr1
] = env
->spr
[srr1
];
596 /* Sort out endianness of interrupt, this differs depending on the
597 * CPU, the HV mode, etc...
600 if (excp_model
== POWERPC_EXCP_POWER7
) {
601 if (!(new_msr
& MSR_HVB
) && (env
->spr
[SPR_LPCR
] & LPCR_ILE
)) {
602 new_msr
|= (target_ulong
)1 << MSR_LE
;
604 } else if (excp_model
== POWERPC_EXCP_POWER8
) {
605 if (new_msr
& MSR_HVB
) {
606 if (env
->spr
[SPR_HID0
] & HID0_HILE
) {
607 new_msr
|= (target_ulong
)1 << MSR_LE
;
609 } else if (env
->spr
[SPR_LPCR
] & LPCR_ILE
) {
610 new_msr
|= (target_ulong
)1 << MSR_LE
;
612 } else if (msr_ile
) {
613 new_msr
|= (target_ulong
)1 << MSR_LE
;
617 new_msr
|= (target_ulong
)1 << MSR_LE
;
621 /* Jump to handler */
622 vector
= env
->excp_vectors
[excp
];
623 if (vector
== (target_ulong
)-1ULL) {
624 cpu_abort(cs
, "Raised an exception without defined vector %d\n",
627 vector
|= env
->excp_prefix
;
629 /* AIL only works if there is no HV transition and we are running with
630 * translations enabled
632 if (!((msr
>> MSR_IR
) & 1) || !((msr
>> MSR_DR
) & 1) ||
633 ((new_msr
& MSR_HVB
) && !(msr
& MSR_HVB
))) {
638 new_msr
|= (1 << MSR_IR
) | (1 << MSR_DR
);
643 case AIL_C000_0000_0000_4000
:
644 vector
|= 0xc000000000004000ull
;
647 cpu_abort(cs
, "Invalid AIL combination %d\n", ail
);
652 #if defined(TARGET_PPC64)
653 if (excp_model
== POWERPC_EXCP_BOOKE
) {
654 if (env
->spr
[SPR_BOOKE_EPCR
] & EPCR_ICM
) {
655 /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */
656 new_msr
|= (target_ulong
)1 << MSR_CM
;
658 vector
= (uint32_t)vector
;
661 if (!msr_isf
&& !(env
->mmu_model
& POWERPC_MMU_64
)) {
662 vector
= (uint32_t)vector
;
664 new_msr
|= (target_ulong
)1 << MSR_SF
;
668 /* We don't use hreg_store_msr here as already have treated
669 * any special case that could occur. Just store MSR and update hflags
671 * Note: We *MUST* not use hreg_store_msr() as-is anyway because it
672 * will prevent setting of the HV bit which some exceptions might need
675 env
->msr
= new_msr
& env
->msr_mask
;
676 hreg_compute_hflags(env
);
678 /* Reset exception state */
679 cs
->exception_index
= POWERPC_EXCP_NONE
;
682 /* Any interrupt is context synchronizing, check if TCG TLB
683 * needs a delayed flush on ppc64
685 check_tlb_flush(env
);
688 void ppc_cpu_do_interrupt(CPUState
*cs
)
690 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
691 CPUPPCState
*env
= &cpu
->env
;
693 powerpc_excp(cpu
, env
->excp_model
, cs
->exception_index
);
696 static void ppc_hw_interrupt(CPUPPCState
*env
)
698 PowerPCCPU
*cpu
= ppc_env_get_cpu(env
);
701 CPUState
*cs
= CPU(cpu
);
703 qemu_log_mask(CPU_LOG_INT
, "%s: %p pending %08x req %08x me %d ee %d\n",
704 __func__
, env
, env
->pending_interrupts
,
705 cs
->interrupt_request
, (int)msr_me
, (int)msr_ee
);
708 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_RESET
)) {
709 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_RESET
);
710 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_RESET
);
713 /* Machine check exception */
714 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_MCK
)) {
715 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_MCK
);
716 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_MCHECK
);
720 /* External debug exception */
721 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DEBUG
)) {
722 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DEBUG
);
723 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_DEBUG
);
728 /* XXX: find a suitable condition to enable the hypervisor mode */
729 hdice
= env
->spr
[SPR_LPCR
] & 1;
733 if ((msr_ee
!= 0 || msr_hv
== 0 || msr_pr
!= 0) && hdice
!= 0) {
734 /* Hypervisor decrementer exception */
735 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_HDECR
)) {
736 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_HDECR
);
741 /* External critical interrupt */
742 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_CEXT
)) {
743 /* Taking a critical external interrupt does not clear the external
744 * critical interrupt status
747 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_CEXT
);
749 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_CRITICAL
);
754 /* Watchdog timer on embedded PowerPC */
755 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_WDT
)) {
756 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_WDT
);
757 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_WDT
);
760 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_CDOORBELL
)) {
761 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_CDOORBELL
);
762 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_DOORCI
);
765 /* Fixed interval timer on embedded PowerPC */
766 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_FIT
)) {
767 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_FIT
);
768 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_FIT
);
771 /* Programmable interval timer on embedded PowerPC */
772 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_PIT
)) {
773 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_PIT
);
774 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_PIT
);
777 /* Decrementer exception */
778 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DECR
)) {
779 if (ppc_decr_clear_on_delivery(env
)) {
780 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DECR
);
782 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_DECR
);
785 /* External interrupt */
786 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_EXT
)) {
787 /* Taking an external interrupt does not clear the external
791 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_EXT
);
793 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_EXTERNAL
);
796 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_DOORBELL
)) {
797 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_DOORBELL
);
798 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_DOORI
);
801 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_PERFM
)) {
802 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_PERFM
);
803 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_PERFM
);
806 /* Thermal interrupt */
807 if (env
->pending_interrupts
& (1 << PPC_INTERRUPT_THERM
)) {
808 env
->pending_interrupts
&= ~(1 << PPC_INTERRUPT_THERM
);
809 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_THERM
);
815 void ppc_cpu_do_system_reset(CPUState
*cs
)
817 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
818 CPUPPCState
*env
= &cpu
->env
;
820 powerpc_excp(cpu
, env
->excp_model
, POWERPC_EXCP_RESET
);
822 #endif /* !CONFIG_USER_ONLY */
824 bool ppc_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
826 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
827 CPUPPCState
*env
= &cpu
->env
;
829 if (interrupt_request
& CPU_INTERRUPT_HARD
) {
830 ppc_hw_interrupt(env
);
831 if (env
->pending_interrupts
== 0) {
832 cs
->interrupt_request
&= ~CPU_INTERRUPT_HARD
;
839 #if defined(DEBUG_OP)
840 static void cpu_dump_rfi(target_ulong RA
, target_ulong msr
)
842 qemu_log("Return from exception at " TARGET_FMT_lx
" with flags "
843 TARGET_FMT_lx
"\n", RA
, msr
);
847 /*****************************************************************************/
848 /* Exceptions processing helpers */
850 void helper_raise_exception_err(CPUPPCState
*env
, uint32_t exception
,
853 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
856 printf("Raise exception %3x code : %d\n", exception
, error_code
);
858 cs
->exception_index
= exception
;
859 env
->error_code
= error_code
;
863 void helper_raise_exception(CPUPPCState
*env
, uint32_t exception
)
865 helper_raise_exception_err(env
, exception
, 0);
868 #if !defined(CONFIG_USER_ONLY)
869 void helper_store_msr(CPUPPCState
*env
, target_ulong val
)
873 val
= hreg_store_msr(env
, val
, 0);
875 cs
= CPU(ppc_env_get_cpu(env
));
876 cs
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
877 helper_raise_exception(env
, val
);
881 static inline void do_rfi(CPUPPCState
*env
, target_ulong nip
, target_ulong msr
)
883 CPUState
*cs
= CPU(ppc_env_get_cpu(env
));
885 /* MSR:POW cannot be set by any form of rfi */
886 msr
&= ~(1ULL << MSR_POW
);
888 #if defined(TARGET_PPC64)
889 /* Switching to 32-bit ? Crop the nip */
890 if (!msr_is_64bit(env
, msr
)) {
896 /* XXX: beware: this is false if VLE is supported */
897 env
->nip
= nip
& ~((target_ulong
)0x00000003);
898 hreg_store_msr(env
, msr
, 1);
899 #if defined(DEBUG_OP)
900 cpu_dump_rfi(env
->nip
, env
->msr
);
902 /* No need to raise an exception here,
903 * as rfi is always the last insn of a TB
905 cs
->interrupt_request
|= CPU_INTERRUPT_EXITTB
;
907 /* Context synchronizing: check if TCG TLB needs flush */
908 check_tlb_flush(env
);
911 void helper_rfi(CPUPPCState
*env
)
913 do_rfi(env
, env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
] & 0xfffffffful
);
916 #define MSR_BOOK3S_MASK
917 #if defined(TARGET_PPC64)
918 void helper_rfid(CPUPPCState
*env
)
920 /* The architeture defines a number of rules for which bits
921 * can change but in practice, we handle this in hreg_store_msr()
922 * which will be called by do_rfi(), so there is no need to filter
925 do_rfi(env
, env
->spr
[SPR_SRR0
], env
->spr
[SPR_SRR1
]);
928 void helper_hrfid(CPUPPCState
*env
)
930 do_rfi(env
, env
->spr
[SPR_HSRR0
], env
->spr
[SPR_HSRR1
]);
934 /*****************************************************************************/
935 /* Embedded PowerPC specific helpers */
936 void helper_40x_rfci(CPUPPCState
*env
)
938 do_rfi(env
, env
->spr
[SPR_40x_SRR2
], env
->spr
[SPR_40x_SRR3
]);
941 void helper_rfci(CPUPPCState
*env
)
943 do_rfi(env
, env
->spr
[SPR_BOOKE_CSRR0
], env
->spr
[SPR_BOOKE_CSRR1
]);
946 void helper_rfdi(CPUPPCState
*env
)
948 /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
949 do_rfi(env
, env
->spr
[SPR_BOOKE_DSRR0
], env
->spr
[SPR_BOOKE_DSRR1
]);
952 void helper_rfmci(CPUPPCState
*env
)
954 /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
955 do_rfi(env
, env
->spr
[SPR_BOOKE_MCSRR0
], env
->spr
[SPR_BOOKE_MCSRR1
]);
959 void helper_tw(CPUPPCState
*env
, target_ulong arg1
, target_ulong arg2
,
962 if (!likely(!(((int32_t)arg1
< (int32_t)arg2
&& (flags
& 0x10)) ||
963 ((int32_t)arg1
> (int32_t)arg2
&& (flags
& 0x08)) ||
964 ((int32_t)arg1
== (int32_t)arg2
&& (flags
& 0x04)) ||
965 ((uint32_t)arg1
< (uint32_t)arg2
&& (flags
& 0x02)) ||
966 ((uint32_t)arg1
> (uint32_t)arg2
&& (flags
& 0x01))))) {
967 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
972 #if defined(TARGET_PPC64)
973 void helper_td(CPUPPCState
*env
, target_ulong arg1
, target_ulong arg2
,
976 if (!likely(!(((int64_t)arg1
< (int64_t)arg2
&& (flags
& 0x10)) ||
977 ((int64_t)arg1
> (int64_t)arg2
&& (flags
& 0x08)) ||
978 ((int64_t)arg1
== (int64_t)arg2
&& (flags
& 0x04)) ||
979 ((uint64_t)arg1
< (uint64_t)arg2
&& (flags
& 0x02)) ||
980 ((uint64_t)arg1
> (uint64_t)arg2
&& (flags
& 0x01))))) {
981 helper_raise_exception_err(env
, POWERPC_EXCP_PROGRAM
,
987 #if !defined(CONFIG_USER_ONLY)
988 /*****************************************************************************/
989 /* PowerPC 601 specific instructions (POWER bridge) */
991 void helper_rfsvc(CPUPPCState
*env
)
993 do_rfi(env
, env
->lr
, env
->ctr
& 0x0000FFFF);
996 /* Embedded.Processor Control */
997 static int dbell2irq(target_ulong rb
)
999 int msg
= rb
& DBELL_TYPE_MASK
;
1003 case DBELL_TYPE_DBELL
:
1004 irq
= PPC_INTERRUPT_DOORBELL
;
1006 case DBELL_TYPE_DBELL_CRIT
:
1007 irq
= PPC_INTERRUPT_CDOORBELL
;
1009 case DBELL_TYPE_G_DBELL
:
1010 case DBELL_TYPE_G_DBELL_CRIT
:
1011 case DBELL_TYPE_G_DBELL_MC
:
1020 void helper_msgclr(CPUPPCState
*env
, target_ulong rb
)
1022 int irq
= dbell2irq(rb
);
1028 env
->pending_interrupts
&= ~(1 << irq
);
1031 void helper_msgsnd(target_ulong rb
)
1033 int irq
= dbell2irq(rb
);
1034 int pir
= rb
& DBELL_PIRTAG_MASK
;
1042 PowerPCCPU
*cpu
= POWERPC_CPU(cs
);
1043 CPUPPCState
*cenv
= &cpu
->env
;
1045 if ((rb
& DBELL_BRDCAST
) || (cenv
->spr
[SPR_BOOKE_PIR
] == pir
)) {
1046 cenv
->pending_interrupts
|= 1 << irq
;
1047 cpu_interrupt(cs
, CPU_INTERRUPT_HARD
);