target/ppc: Add Hypervisor Virtualization Interrupt on POWER9
[qemu.git] / target / ppc / excp_helper.c
blobd171a5eb6236dce40fc785dde563976126c95c8d
1 /*
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"
21 #include "cpu.h"
22 #include "exec/helper-proto.h"
23 #include "exec/exec-all.h"
24 #include "exec/cpu_ldst.h"
25 #include "internal.h"
26 #include "helper_regs.h"
28 //#define DEBUG_OP
29 //#define DEBUG_SOFTWARE_TLB
30 //#define DEBUG_EXCEPTIONS
32 #ifdef DEBUG_EXCEPTIONS
33 # define LOG_EXCP(...) qemu_log(__VA_ARGS__)
34 #else
35 # define LOG_EXCP(...) do { } while (0)
36 #endif
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;
47 env->error_code = 0;
50 static void ppc_hw_interrupt(CPUPPCState *env)
52 CPUState *cs = CPU(ppc_env_get_cpu(env));
54 cs->exception_index = POWERPC_EXCP_NONE;
55 env->error_code = 0;
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 static int powerpc_reset_wakeup(CPUState *cs, CPUPPCState *env, int excp,
69 target_ulong *msr)
71 /* We no longer are in a PM state */
72 env->resume_as_sreset = false;
74 /* Pretend to be returning from doze always as we don't lose state */
75 *msr |= (0x1ull << (63 - 47));
77 /* Machine checks are sent normally */
78 if (excp == POWERPC_EXCP_MCHECK) {
79 return excp;
81 switch (excp) {
82 case POWERPC_EXCP_RESET:
83 *msr |= 0x4ull << (63 - 45);
84 break;
85 case POWERPC_EXCP_EXTERNAL:
86 *msr |= 0x8ull << (63 - 45);
87 break;
88 case POWERPC_EXCP_DECR:
89 *msr |= 0x6ull << (63 - 45);
90 break;
91 case POWERPC_EXCP_SDOOR:
92 *msr |= 0x5ull << (63 - 45);
93 break;
94 case POWERPC_EXCP_SDOOR_HV:
95 *msr |= 0x3ull << (63 - 45);
96 break;
97 case POWERPC_EXCP_HV_MAINT:
98 *msr |= 0xaull << (63 - 45);
99 break;
100 case POWERPC_EXCP_HVIRT:
101 *msr |= 0x9ull << (63 - 45);
102 break;
103 default:
104 cpu_abort(cs, "Unsupported exception %d in Power Save mode\n",
105 excp);
107 return POWERPC_EXCP_RESET;
111 /* Note that this function should be greatly optimized
112 * when called with a constant excp, from ppc_hw_interrupt
114 static inline void powerpc_excp(PowerPCCPU *cpu, int excp_model, int excp)
116 CPUState *cs = CPU(cpu);
117 CPUPPCState *env = &cpu->env;
118 target_ulong msr, new_msr, vector;
119 int srr0, srr1, asrr0, asrr1, lev, ail;
120 bool lpes0;
122 qemu_log_mask(CPU_LOG_INT, "Raise exception at " TARGET_FMT_lx
123 " => %08x (%02x)\n", env->nip, excp, env->error_code);
125 /* new srr1 value excluding must-be-zero bits */
126 if (excp_model == POWERPC_EXCP_BOOKE) {
127 msr = env->msr;
128 } else {
129 msr = env->msr & ~0x783f0000ULL;
132 /* new interrupt handler msr preserves existing HV and ME unless
133 * explicitly overriden
135 new_msr = env->msr & (((target_ulong)1 << MSR_ME) | MSR_HVB);
137 /* target registers */
138 srr0 = SPR_SRR0;
139 srr1 = SPR_SRR1;
140 asrr0 = -1;
141 asrr1 = -1;
144 * check for special resume at 0x100 from doze/nap/sleep/winkle on
145 * P7/P8/P9
147 if (env->resume_as_sreset) {
148 excp = powerpc_reset_wakeup(cs, env, excp, &msr);
151 /* Exception targetting modifiers
153 * LPES0 is supported on POWER7/8/9
154 * LPES1 is not supported (old iSeries mode)
156 * On anything else, we behave as if LPES0 is 1
157 * (externals don't alter MSR:HV)
159 * AIL is initialized here but can be cleared by
160 * selected exceptions
162 #if defined(TARGET_PPC64)
163 if (excp_model == POWERPC_EXCP_POWER7 ||
164 excp_model == POWERPC_EXCP_POWER8 ||
165 excp_model == POWERPC_EXCP_POWER9) {
166 lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
167 if (excp_model != POWERPC_EXCP_POWER7) {
168 ail = (env->spr[SPR_LPCR] & LPCR_AIL) >> LPCR_AIL_SHIFT;
169 } else {
170 ail = 0;
172 } else
173 #endif /* defined(TARGET_PPC64) */
175 lpes0 = true;
176 ail = 0;
179 /* Hypervisor emulation assistance interrupt only exists on server
180 * arch 2.05 server or later. We also don't want to generate it if
181 * we don't have HVB in msr_mask (PAPR mode).
183 if (excp == POWERPC_EXCP_HV_EMU
184 #if defined(TARGET_PPC64)
185 && !((env->mmu_model & POWERPC_MMU_64) && (env->msr_mask & MSR_HVB))
186 #endif /* defined(TARGET_PPC64) */
189 excp = POWERPC_EXCP_PROGRAM;
192 switch (excp) {
193 case POWERPC_EXCP_NONE:
194 /* Should never happen */
195 return;
196 case POWERPC_EXCP_CRITICAL: /* Critical input */
197 switch (excp_model) {
198 case POWERPC_EXCP_40x:
199 srr0 = SPR_40x_SRR2;
200 srr1 = SPR_40x_SRR3;
201 break;
202 case POWERPC_EXCP_BOOKE:
203 srr0 = SPR_BOOKE_CSRR0;
204 srr1 = SPR_BOOKE_CSRR1;
205 break;
206 case POWERPC_EXCP_G2:
207 break;
208 default:
209 goto excp_invalid;
211 break;
212 case POWERPC_EXCP_MCHECK: /* Machine check exception */
213 if (msr_me == 0) {
214 /* Machine check exception is not enabled.
215 * Enter checkstop state.
217 fprintf(stderr, "Machine check while not allowed. "
218 "Entering checkstop state\n");
219 if (qemu_log_separate()) {
220 qemu_log("Machine check while not allowed. "
221 "Entering checkstop state\n");
223 cs->halted = 1;
224 cpu_interrupt_exittb(cs);
226 if (env->msr_mask & MSR_HVB) {
227 /* ISA specifies HV, but can be delivered to guest with HV clear
228 * (e.g., see FWNMI in PAPR).
230 new_msr |= (target_ulong)MSR_HVB;
232 ail = 0;
234 /* machine check exceptions don't have ME set */
235 new_msr &= ~((target_ulong)1 << MSR_ME);
237 /* XXX: should also have something loaded in DAR / DSISR */
238 switch (excp_model) {
239 case POWERPC_EXCP_40x:
240 srr0 = SPR_40x_SRR2;
241 srr1 = SPR_40x_SRR3;
242 break;
243 case POWERPC_EXCP_BOOKE:
244 /* FIXME: choose one or the other based on CPU type */
245 srr0 = SPR_BOOKE_MCSRR0;
246 srr1 = SPR_BOOKE_MCSRR1;
247 asrr0 = SPR_BOOKE_CSRR0;
248 asrr1 = SPR_BOOKE_CSRR1;
249 break;
250 default:
251 break;
253 break;
254 case POWERPC_EXCP_DSI: /* Data storage exception */
255 LOG_EXCP("DSI exception: DSISR=" TARGET_FMT_lx" DAR=" TARGET_FMT_lx
256 "\n", env->spr[SPR_DSISR], env->spr[SPR_DAR]);
257 break;
258 case POWERPC_EXCP_ISI: /* Instruction storage exception */
259 LOG_EXCP("ISI exception: msr=" TARGET_FMT_lx ", nip=" TARGET_FMT_lx
260 "\n", msr, env->nip);
261 msr |= env->error_code;
262 break;
263 case POWERPC_EXCP_EXTERNAL: /* External input */
264 cs = CPU(cpu);
266 if (!lpes0) {
267 new_msr |= (target_ulong)MSR_HVB;
268 new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
269 srr0 = SPR_HSRR0;
270 srr1 = SPR_HSRR1;
272 if (env->mpic_proxy) {
273 /* IACK the IRQ on delivery */
274 env->spr[SPR_BOOKE_EPR] = ldl_phys(cs->as, env->mpic_iack);
276 break;
277 case POWERPC_EXCP_ALIGN: /* Alignment exception */
278 /* Get rS/rD and rA from faulting opcode */
279 /* Note: the opcode fields will not be set properly for a direct
280 * store load/store, but nobody cares as nobody actually uses
281 * direct store segments.
283 env->spr[SPR_DSISR] |= (env->error_code & 0x03FF0000) >> 16;
284 break;
285 case POWERPC_EXCP_PROGRAM: /* Program exception */
286 switch (env->error_code & ~0xF) {
287 case POWERPC_EXCP_FP:
288 if ((msr_fe0 == 0 && msr_fe1 == 0) || msr_fp == 0) {
289 LOG_EXCP("Ignore floating point exception\n");
290 cs->exception_index = POWERPC_EXCP_NONE;
291 env->error_code = 0;
292 return;
295 /* FP exceptions always have NIP pointing to the faulting
296 * instruction, so always use store_next and claim we are
297 * precise in the MSR.
299 msr |= 0x00100000;
300 env->spr[SPR_BOOKE_ESR] = ESR_FP;
301 break;
302 case POWERPC_EXCP_INVAL:
303 LOG_EXCP("Invalid instruction at " TARGET_FMT_lx "\n", env->nip);
304 msr |= 0x00080000;
305 env->spr[SPR_BOOKE_ESR] = ESR_PIL;
306 break;
307 case POWERPC_EXCP_PRIV:
308 msr |= 0x00040000;
309 env->spr[SPR_BOOKE_ESR] = ESR_PPR;
310 break;
311 case POWERPC_EXCP_TRAP:
312 msr |= 0x00020000;
313 env->spr[SPR_BOOKE_ESR] = ESR_PTR;
314 break;
315 default:
316 /* Should never occur */
317 cpu_abort(cs, "Invalid program exception %d. Aborting\n",
318 env->error_code);
319 break;
321 break;
322 case POWERPC_EXCP_SYSCALL: /* System call exception */
323 dump_syscall(env);
324 lev = env->error_code;
326 /* We need to correct the NIP which in this case is supposed
327 * to point to the next instruction
329 env->nip += 4;
331 /* "PAPR mode" built-in hypercall emulation */
332 if ((lev == 1) && cpu->vhyp) {
333 PPCVirtualHypervisorClass *vhc =
334 PPC_VIRTUAL_HYPERVISOR_GET_CLASS(cpu->vhyp);
335 vhc->hypercall(cpu->vhyp, cpu);
336 return;
338 if (lev == 1) {
339 new_msr |= (target_ulong)MSR_HVB;
341 break;
342 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
343 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
344 case POWERPC_EXCP_DECR: /* Decrementer exception */
345 break;
346 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
347 /* FIT on 4xx */
348 LOG_EXCP("FIT exception\n");
349 break;
350 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
351 LOG_EXCP("WDT exception\n");
352 switch (excp_model) {
353 case POWERPC_EXCP_BOOKE:
354 srr0 = SPR_BOOKE_CSRR0;
355 srr1 = SPR_BOOKE_CSRR1;
356 break;
357 default:
358 break;
360 break;
361 case POWERPC_EXCP_DTLB: /* Data TLB error */
362 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
363 break;
364 case POWERPC_EXCP_DEBUG: /* Debug interrupt */
365 if (env->flags & POWERPC_FLAG_DE) {
366 /* FIXME: choose one or the other based on CPU type */
367 srr0 = SPR_BOOKE_DSRR0;
368 srr1 = SPR_BOOKE_DSRR1;
369 asrr0 = SPR_BOOKE_CSRR0;
370 asrr1 = SPR_BOOKE_CSRR1;
371 /* DBSR already modified by caller */
372 } else {
373 cpu_abort(cs, "Debug exception triggered on unsupported model\n");
375 break;
376 case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavailable */
377 env->spr[SPR_BOOKE_ESR] = ESR_SPV;
378 break;
379 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data interrupt */
380 /* XXX: TODO */
381 cpu_abort(cs, "Embedded floating point data exception "
382 "is not implemented yet !\n");
383 env->spr[SPR_BOOKE_ESR] = ESR_SPV;
384 break;
385 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round interrupt */
386 /* XXX: TODO */
387 cpu_abort(cs, "Embedded floating point round exception "
388 "is not implemented yet !\n");
389 env->spr[SPR_BOOKE_ESR] = ESR_SPV;
390 break;
391 case POWERPC_EXCP_EPERFM: /* Embedded performance monitor interrupt */
392 /* XXX: TODO */
393 cpu_abort(cs,
394 "Performance counter exception is not implemented yet !\n");
395 break;
396 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
397 break;
398 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
399 srr0 = SPR_BOOKE_CSRR0;
400 srr1 = SPR_BOOKE_CSRR1;
401 break;
402 case POWERPC_EXCP_RESET: /* System reset exception */
403 /* A power-saving exception sets ME, otherwise it is unchanged */
404 if (msr_pow) {
405 /* indicate that we resumed from power save mode */
406 msr |= 0x10000;
407 new_msr |= ((target_ulong)1 << MSR_ME);
409 if (env->msr_mask & MSR_HVB) {
410 /* ISA specifies HV, but can be delivered to guest with HV clear
411 * (e.g., see FWNMI in PAPR, NMI injection in QEMU).
413 new_msr |= (target_ulong)MSR_HVB;
414 } else {
415 if (msr_pow) {
416 cpu_abort(cs, "Trying to deliver power-saving system reset "
417 "exception %d with no HV support\n", excp);
420 ail = 0;
421 break;
422 case POWERPC_EXCP_DSEG: /* Data segment exception */
423 case POWERPC_EXCP_ISEG: /* Instruction segment exception */
424 case POWERPC_EXCP_TRACE: /* Trace exception */
425 break;
426 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
427 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
428 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage exception */
429 case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
430 case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment exception */
431 case POWERPC_EXCP_SDOOR_HV: /* Hypervisor Doorbell interrupt */
432 case POWERPC_EXCP_HV_EMU:
433 case POWERPC_EXCP_HVIRT: /* Hypervisor virtualization */
434 srr0 = SPR_HSRR0;
435 srr1 = SPR_HSRR1;
436 new_msr |= (target_ulong)MSR_HVB;
437 new_msr |= env->msr & ((target_ulong)1 << MSR_RI);
438 break;
439 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
440 case POWERPC_EXCP_VSXU: /* VSX unavailable exception */
441 case POWERPC_EXCP_FU: /* Facility unavailable exception */
442 #ifdef TARGET_PPC64
443 env->spr[SPR_FSCR] |= ((target_ulong)env->error_code << 56);
444 #endif
445 break;
446 case POWERPC_EXCP_PIT: /* Programmable interval timer interrupt */
447 LOG_EXCP("PIT exception\n");
448 break;
449 case POWERPC_EXCP_IO: /* IO error exception */
450 /* XXX: TODO */
451 cpu_abort(cs, "601 IO error exception is not implemented yet !\n");
452 break;
453 case POWERPC_EXCP_RUNM: /* Run mode exception */
454 /* XXX: TODO */
455 cpu_abort(cs, "601 run mode exception is not implemented yet !\n");
456 break;
457 case POWERPC_EXCP_EMUL: /* Emulation trap exception */
458 /* XXX: TODO */
459 cpu_abort(cs, "602 emulation trap exception "
460 "is not implemented yet !\n");
461 break;
462 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
463 switch (excp_model) {
464 case POWERPC_EXCP_602:
465 case POWERPC_EXCP_603:
466 case POWERPC_EXCP_603E:
467 case POWERPC_EXCP_G2:
468 goto tlb_miss_tgpr;
469 case POWERPC_EXCP_7x5:
470 goto tlb_miss;
471 case POWERPC_EXCP_74xx:
472 goto tlb_miss_74xx;
473 default:
474 cpu_abort(cs, "Invalid instruction TLB miss exception\n");
475 break;
477 break;
478 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
479 switch (excp_model) {
480 case POWERPC_EXCP_602:
481 case POWERPC_EXCP_603:
482 case POWERPC_EXCP_603E:
483 case POWERPC_EXCP_G2:
484 goto tlb_miss_tgpr;
485 case POWERPC_EXCP_7x5:
486 goto tlb_miss;
487 case POWERPC_EXCP_74xx:
488 goto tlb_miss_74xx;
489 default:
490 cpu_abort(cs, "Invalid data load TLB miss exception\n");
491 break;
493 break;
494 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
495 switch (excp_model) {
496 case POWERPC_EXCP_602:
497 case POWERPC_EXCP_603:
498 case POWERPC_EXCP_603E:
499 case POWERPC_EXCP_G2:
500 tlb_miss_tgpr:
501 /* Swap temporary saved registers with GPRs */
502 if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
503 new_msr |= (target_ulong)1 << MSR_TGPR;
504 hreg_swap_gpr_tgpr(env);
506 goto tlb_miss;
507 case POWERPC_EXCP_7x5:
508 tlb_miss:
509 #if defined(DEBUG_SOFTWARE_TLB)
510 if (qemu_log_enabled()) {
511 const char *es;
512 target_ulong *miss, *cmp;
513 int en;
515 if (excp == POWERPC_EXCP_IFTLB) {
516 es = "I";
517 en = 'I';
518 miss = &env->spr[SPR_IMISS];
519 cmp = &env->spr[SPR_ICMP];
520 } else {
521 if (excp == POWERPC_EXCP_DLTLB) {
522 es = "DL";
523 } else {
524 es = "DS";
526 en = 'D';
527 miss = &env->spr[SPR_DMISS];
528 cmp = &env->spr[SPR_DCMP];
530 qemu_log("6xx %sTLB miss: %cM " TARGET_FMT_lx " %cC "
531 TARGET_FMT_lx " H1 " TARGET_FMT_lx " H2 "
532 TARGET_FMT_lx " %08x\n", es, en, *miss, en, *cmp,
533 env->spr[SPR_HASH1], env->spr[SPR_HASH2],
534 env->error_code);
536 #endif
537 msr |= env->crf[0] << 28;
538 msr |= env->error_code; /* key, D/I, S/L bits */
539 /* Set way using a LRU mechanism */
540 msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
541 break;
542 case POWERPC_EXCP_74xx:
543 tlb_miss_74xx:
544 #if defined(DEBUG_SOFTWARE_TLB)
545 if (qemu_log_enabled()) {
546 const char *es;
547 target_ulong *miss, *cmp;
548 int en;
550 if (excp == POWERPC_EXCP_IFTLB) {
551 es = "I";
552 en = 'I';
553 miss = &env->spr[SPR_TLBMISS];
554 cmp = &env->spr[SPR_PTEHI];
555 } else {
556 if (excp == POWERPC_EXCP_DLTLB) {
557 es = "DL";
558 } else {
559 es = "DS";
561 en = 'D';
562 miss = &env->spr[SPR_TLBMISS];
563 cmp = &env->spr[SPR_PTEHI];
565 qemu_log("74xx %sTLB miss: %cM " TARGET_FMT_lx " %cC "
566 TARGET_FMT_lx " %08x\n", es, en, *miss, en, *cmp,
567 env->error_code);
569 #endif
570 msr |= env->error_code; /* key bit */
571 break;
572 default:
573 cpu_abort(cs, "Invalid data store TLB miss exception\n");
574 break;
576 break;
577 case POWERPC_EXCP_FPA: /* Floating-point assist exception */
578 /* XXX: TODO */
579 cpu_abort(cs, "Floating point assist exception "
580 "is not implemented yet !\n");
581 break;
582 case POWERPC_EXCP_DABR: /* Data address breakpoint */
583 /* XXX: TODO */
584 cpu_abort(cs, "DABR exception is not implemented yet !\n");
585 break;
586 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
587 /* XXX: TODO */
588 cpu_abort(cs, "IABR exception is not implemented yet !\n");
589 break;
590 case POWERPC_EXCP_SMI: /* System management interrupt */
591 /* XXX: TODO */
592 cpu_abort(cs, "SMI exception is not implemented yet !\n");
593 break;
594 case POWERPC_EXCP_THERM: /* Thermal interrupt */
595 /* XXX: TODO */
596 cpu_abort(cs, "Thermal management exception "
597 "is not implemented yet !\n");
598 break;
599 case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */
600 /* XXX: TODO */
601 cpu_abort(cs,
602 "Performance counter exception is not implemented yet !\n");
603 break;
604 case POWERPC_EXCP_VPUA: /* Vector assist exception */
605 /* XXX: TODO */
606 cpu_abort(cs, "VPU assist exception is not implemented yet !\n");
607 break;
608 case POWERPC_EXCP_SOFTP: /* Soft patch exception */
609 /* XXX: TODO */
610 cpu_abort(cs,
611 "970 soft-patch exception is not implemented yet !\n");
612 break;
613 case POWERPC_EXCP_MAINT: /* Maintenance exception */
614 /* XXX: TODO */
615 cpu_abort(cs,
616 "970 maintenance exception is not implemented yet !\n");
617 break;
618 case POWERPC_EXCP_MEXTBR: /* Maskable external breakpoint */
619 /* XXX: TODO */
620 cpu_abort(cs, "Maskable external exception "
621 "is not implemented yet !\n");
622 break;
623 case POWERPC_EXCP_NMEXTBR: /* Non maskable external breakpoint */
624 /* XXX: TODO */
625 cpu_abort(cs, "Non maskable external exception "
626 "is not implemented yet !\n");
627 break;
628 default:
629 excp_invalid:
630 cpu_abort(cs, "Invalid PowerPC exception %d. Aborting\n", excp);
631 break;
634 /* Save PC */
635 env->spr[srr0] = env->nip;
637 /* Save MSR */
638 env->spr[srr1] = msr;
640 /* Sanity check */
641 if (!(env->msr_mask & MSR_HVB)) {
642 if (new_msr & MSR_HVB) {
643 cpu_abort(cs, "Trying to deliver HV exception (MSR) %d with "
644 "no HV support\n", excp);
646 if (srr0 == SPR_HSRR0) {
647 cpu_abort(cs, "Trying to deliver HV exception (HSRR) %d with "
648 "no HV support\n", excp);
652 /* If any alternate SRR register are defined, duplicate saved values */
653 if (asrr0 != -1) {
654 env->spr[asrr0] = env->spr[srr0];
656 if (asrr1 != -1) {
657 env->spr[asrr1] = env->spr[srr1];
660 /* Sort out endianness of interrupt, this differs depending on the
661 * CPU, the HV mode, etc...
663 #ifdef TARGET_PPC64
664 if (excp_model == POWERPC_EXCP_POWER7) {
665 if (!(new_msr & MSR_HVB) && (env->spr[SPR_LPCR] & LPCR_ILE)) {
666 new_msr |= (target_ulong)1 << MSR_LE;
668 } else if (excp_model == POWERPC_EXCP_POWER8) {
669 if (new_msr & MSR_HVB) {
670 if (env->spr[SPR_HID0] & HID0_HILE) {
671 new_msr |= (target_ulong)1 << MSR_LE;
673 } else if (env->spr[SPR_LPCR] & LPCR_ILE) {
674 new_msr |= (target_ulong)1 << MSR_LE;
676 } else if (excp_model == POWERPC_EXCP_POWER9) {
677 if (new_msr & MSR_HVB) {
678 if (env->spr[SPR_HID0] & HID0_POWER9_HILE) {
679 new_msr |= (target_ulong)1 << MSR_LE;
681 } else if (env->spr[SPR_LPCR] & LPCR_ILE) {
682 new_msr |= (target_ulong)1 << MSR_LE;
684 } else if (msr_ile) {
685 new_msr |= (target_ulong)1 << MSR_LE;
687 #else
688 if (msr_ile) {
689 new_msr |= (target_ulong)1 << MSR_LE;
691 #endif
693 /* Jump to handler */
694 vector = env->excp_vectors[excp];
695 if (vector == (target_ulong)-1ULL) {
696 cpu_abort(cs, "Raised an exception without defined vector %d\n",
697 excp);
699 vector |= env->excp_prefix;
701 /* AIL only works if there is no HV transition and we are running with
702 * translations enabled
704 if (!((msr >> MSR_IR) & 1) || !((msr >> MSR_DR) & 1) ||
705 ((new_msr & MSR_HVB) && !(msr & MSR_HVB))) {
706 ail = 0;
708 /* Handle AIL */
709 if (ail) {
710 new_msr |= (1 << MSR_IR) | (1 << MSR_DR);
711 switch(ail) {
712 case AIL_0001_8000:
713 vector |= 0x18000;
714 break;
715 case AIL_C000_0000_0000_4000:
716 vector |= 0xc000000000004000ull;
717 break;
718 default:
719 cpu_abort(cs, "Invalid AIL combination %d\n", ail);
720 break;
724 #if defined(TARGET_PPC64)
725 if (excp_model == POWERPC_EXCP_BOOKE) {
726 if (env->spr[SPR_BOOKE_EPCR] & EPCR_ICM) {
727 /* Cat.64-bit: EPCR.ICM is copied to MSR.CM */
728 new_msr |= (target_ulong)1 << MSR_CM;
729 } else {
730 vector = (uint32_t)vector;
732 } else {
733 if (!msr_isf && !(env->mmu_model & POWERPC_MMU_64)) {
734 vector = (uint32_t)vector;
735 } else {
736 new_msr |= (target_ulong)1 << MSR_SF;
739 #endif
740 /* We don't use hreg_store_msr here as already have treated
741 * any special case that could occur. Just store MSR and update hflags
743 * Note: We *MUST* not use hreg_store_msr() as-is anyway because it
744 * will prevent setting of the HV bit which some exceptions might need
745 * to do.
747 env->msr = new_msr & env->msr_mask;
748 hreg_compute_hflags(env);
749 env->nip = vector;
750 /* Reset exception state */
751 cs->exception_index = POWERPC_EXCP_NONE;
752 env->error_code = 0;
754 /* Reset the reservation */
755 env->reserve_addr = -1;
757 /* Any interrupt is context synchronizing, check if TCG TLB
758 * needs a delayed flush on ppc64
760 check_tlb_flush(env, false);
763 void ppc_cpu_do_interrupt(CPUState *cs)
765 PowerPCCPU *cpu = POWERPC_CPU(cs);
766 CPUPPCState *env = &cpu->env;
768 powerpc_excp(cpu, env->excp_model, cs->exception_index);
771 static void ppc_hw_interrupt(CPUPPCState *env)
773 PowerPCCPU *cpu = ppc_env_get_cpu(env);
774 bool async_deliver;
776 /* External reset */
777 if (env->pending_interrupts & (1 << PPC_INTERRUPT_RESET)) {
778 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_RESET);
779 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_RESET);
780 return;
782 /* Machine check exception */
783 if (env->pending_interrupts & (1 << PPC_INTERRUPT_MCK)) {
784 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_MCK);
785 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_MCHECK);
786 return;
788 #if 0 /* TODO */
789 /* External debug exception */
790 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DEBUG)) {
791 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DEBUG);
792 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_DEBUG);
793 return;
795 #endif
798 * For interrupts that gate on MSR:EE, we need to do something a
799 * bit more subtle, as we need to let them through even when EE is
800 * clear when coming out of some power management states (in order
801 * for them to become a 0x100).
803 async_deliver = (msr_ee != 0) || env->resume_as_sreset;
805 /* Hypervisor decrementer exception */
806 if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDECR)) {
807 /* LPCR will be clear when not supported so this will work */
808 bool hdice = !!(env->spr[SPR_LPCR] & LPCR_HDICE);
809 if ((async_deliver || msr_hv == 0) && hdice) {
810 /* HDEC clears on delivery */
811 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR);
812 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_HDECR);
813 return;
817 /* Hypervisor virtualization interrupt */
818 if (env->pending_interrupts & (1 << PPC_INTERRUPT_HVIRT)) {
819 /* LPCR will be clear when not supported so this will work */
820 bool hvice = !!(env->spr[SPR_LPCR] & LPCR_HVICE);
821 if ((async_deliver || msr_hv == 0) && hvice) {
822 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_HVIRT);
823 return;
827 /* External interrupt can ignore MSR:EE under some circumstances */
828 if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
829 bool lpes0 = !!(env->spr[SPR_LPCR] & LPCR_LPES0);
830 if (async_deliver || (env->has_hv_mode && msr_hv == 0 && !lpes0)) {
831 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_EXTERNAL);
832 return;
835 if (msr_ce != 0) {
836 /* External critical interrupt */
837 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CEXT)) {
838 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_CRITICAL);
839 return;
842 if (async_deliver != 0) {
843 /* Watchdog timer on embedded PowerPC */
844 if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) {
845 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
846 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_WDT);
847 return;
849 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CDOORBELL)) {
850 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CDOORBELL);
851 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_DOORCI);
852 return;
854 /* Fixed interval timer on embedded PowerPC */
855 if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) {
856 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
857 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_FIT);
858 return;
860 /* Programmable interval timer on embedded PowerPC */
861 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PIT)) {
862 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PIT);
863 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_PIT);
864 return;
866 /* Decrementer exception */
867 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) {
868 if (ppc_decr_clear_on_delivery(env)) {
869 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
871 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_DECR);
872 return;
874 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) {
875 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL);
876 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_DOORI);
877 return;
879 if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDOORBELL)) {
880 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDOORBELL);
881 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_SDOOR_HV);
882 return;
884 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PERFM)) {
885 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PERFM);
886 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_PERFM);
887 return;
889 /* Thermal interrupt */
890 if (env->pending_interrupts & (1 << PPC_INTERRUPT_THERM)) {
891 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_THERM);
892 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_THERM);
893 return;
897 if (env->resume_as_sreset) {
899 * This is a bug ! It means that has_work took us out of halt without
900 * anything to deliver while in a PM state that requires getting
901 * out via a 0x100
903 * This means we will incorrectly execute past the power management
904 * instruction instead of triggering a reset.
906 * It generally means a discrepancy between the wakup conditions in the
907 * processor has_work implementation and the logic in this function.
909 cpu_abort(CPU(ppc_env_get_cpu(env)),
910 "Wakeup from PM state but interrupt Undelivered");
914 void ppc_cpu_do_system_reset(CPUState *cs)
916 PowerPCCPU *cpu = POWERPC_CPU(cs);
917 CPUPPCState *env = &cpu->env;
919 powerpc_excp(cpu, env->excp_model, POWERPC_EXCP_RESET);
921 #endif /* !CONFIG_USER_ONLY */
923 bool ppc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
925 PowerPCCPU *cpu = POWERPC_CPU(cs);
926 CPUPPCState *env = &cpu->env;
928 if (interrupt_request & CPU_INTERRUPT_HARD) {
929 ppc_hw_interrupt(env);
930 if (env->pending_interrupts == 0) {
931 cs->interrupt_request &= ~CPU_INTERRUPT_HARD;
933 return true;
935 return false;
938 #if defined(DEBUG_OP)
939 static void cpu_dump_rfi(target_ulong RA, target_ulong msr)
941 qemu_log("Return from exception at " TARGET_FMT_lx " with flags "
942 TARGET_FMT_lx "\n", RA, msr);
944 #endif
946 /*****************************************************************************/
947 /* Exceptions processing helpers */
949 void raise_exception_err_ra(CPUPPCState *env, uint32_t exception,
950 uint32_t error_code, uintptr_t raddr)
952 CPUState *cs = CPU(ppc_env_get_cpu(env));
954 cs->exception_index = exception;
955 env->error_code = error_code;
956 cpu_loop_exit_restore(cs, raddr);
959 void raise_exception_err(CPUPPCState *env, uint32_t exception,
960 uint32_t error_code)
962 raise_exception_err_ra(env, exception, error_code, 0);
965 void raise_exception(CPUPPCState *env, uint32_t exception)
967 raise_exception_err_ra(env, exception, 0, 0);
970 void raise_exception_ra(CPUPPCState *env, uint32_t exception,
971 uintptr_t raddr)
973 raise_exception_err_ra(env, exception, 0, raddr);
976 void helper_raise_exception_err(CPUPPCState *env, uint32_t exception,
977 uint32_t error_code)
979 raise_exception_err_ra(env, exception, error_code, 0);
982 void helper_raise_exception(CPUPPCState *env, uint32_t exception)
984 raise_exception_err_ra(env, exception, 0, 0);
987 #if !defined(CONFIG_USER_ONLY)
988 void helper_store_msr(CPUPPCState *env, target_ulong val)
990 uint32_t excp = hreg_store_msr(env, val, 0);
992 if (excp != 0) {
993 CPUState *cs = CPU(ppc_env_get_cpu(env));
994 cpu_interrupt_exittb(cs);
995 raise_exception(env, excp);
999 #if defined(TARGET_PPC64)
1000 void helper_pminsn(CPUPPCState *env, powerpc_pm_insn_t insn)
1002 CPUState *cs;
1004 cs = CPU(ppc_env_get_cpu(env));
1005 cs->halted = 1;
1007 /* The architecture specifies that HDEC interrupts are
1008 * discarded in PM states
1010 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR);
1012 /* Condition for waking up at 0x100 */
1013 env->resume_as_sreset = (insn != PPC_PM_STOP) ||
1014 (env->spr[SPR_PSSCR] & PSSCR_EC);
1016 #endif /* defined(TARGET_PPC64) */
1018 static inline void do_rfi(CPUPPCState *env, target_ulong nip, target_ulong msr)
1020 CPUState *cs = CPU(ppc_env_get_cpu(env));
1022 /* MSR:POW cannot be set by any form of rfi */
1023 msr &= ~(1ULL << MSR_POW);
1025 #if defined(TARGET_PPC64)
1026 /* Switching to 32-bit ? Crop the nip */
1027 if (!msr_is_64bit(env, msr)) {
1028 nip = (uint32_t)nip;
1030 #else
1031 nip = (uint32_t)nip;
1032 #endif
1033 /* XXX: beware: this is false if VLE is supported */
1034 env->nip = nip & ~((target_ulong)0x00000003);
1035 hreg_store_msr(env, msr, 1);
1036 #if defined(DEBUG_OP)
1037 cpu_dump_rfi(env->nip, env->msr);
1038 #endif
1039 /* No need to raise an exception here,
1040 * as rfi is always the last insn of a TB
1042 cpu_interrupt_exittb(cs);
1043 /* Reset the reservation */
1044 env->reserve_addr = -1;
1046 /* Context synchronizing: check if TCG TLB needs flush */
1047 check_tlb_flush(env, false);
1050 void helper_rfi(CPUPPCState *env)
1052 do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1] & 0xfffffffful);
1055 #define MSR_BOOK3S_MASK
1056 #if defined(TARGET_PPC64)
1057 void helper_rfid(CPUPPCState *env)
1059 /* The architeture defines a number of rules for which bits
1060 * can change but in practice, we handle this in hreg_store_msr()
1061 * which will be called by do_rfi(), so there is no need to filter
1062 * here
1064 do_rfi(env, env->spr[SPR_SRR0], env->spr[SPR_SRR1]);
1067 void helper_hrfid(CPUPPCState *env)
1069 do_rfi(env, env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
1071 #endif
1073 /*****************************************************************************/
1074 /* Embedded PowerPC specific helpers */
1075 void helper_40x_rfci(CPUPPCState *env)
1077 do_rfi(env, env->spr[SPR_40x_SRR2], env->spr[SPR_40x_SRR3]);
1080 void helper_rfci(CPUPPCState *env)
1082 do_rfi(env, env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1]);
1085 void helper_rfdi(CPUPPCState *env)
1087 /* FIXME: choose CSRR1 or DSRR1 based on cpu type */
1088 do_rfi(env, env->spr[SPR_BOOKE_DSRR0], env->spr[SPR_BOOKE_DSRR1]);
1091 void helper_rfmci(CPUPPCState *env)
1093 /* FIXME: choose CSRR1 or MCSRR1 based on cpu type */
1094 do_rfi(env, env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
1096 #endif
1098 void helper_tw(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
1099 uint32_t flags)
1101 if (!likely(!(((int32_t)arg1 < (int32_t)arg2 && (flags & 0x10)) ||
1102 ((int32_t)arg1 > (int32_t)arg2 && (flags & 0x08)) ||
1103 ((int32_t)arg1 == (int32_t)arg2 && (flags & 0x04)) ||
1104 ((uint32_t)arg1 < (uint32_t)arg2 && (flags & 0x02)) ||
1105 ((uint32_t)arg1 > (uint32_t)arg2 && (flags & 0x01))))) {
1106 raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
1107 POWERPC_EXCP_TRAP, GETPC());
1111 #if defined(TARGET_PPC64)
1112 void helper_td(CPUPPCState *env, target_ulong arg1, target_ulong arg2,
1113 uint32_t flags)
1115 if (!likely(!(((int64_t)arg1 < (int64_t)arg2 && (flags & 0x10)) ||
1116 ((int64_t)arg1 > (int64_t)arg2 && (flags & 0x08)) ||
1117 ((int64_t)arg1 == (int64_t)arg2 && (flags & 0x04)) ||
1118 ((uint64_t)arg1 < (uint64_t)arg2 && (flags & 0x02)) ||
1119 ((uint64_t)arg1 > (uint64_t)arg2 && (flags & 0x01))))) {
1120 raise_exception_err_ra(env, POWERPC_EXCP_PROGRAM,
1121 POWERPC_EXCP_TRAP, GETPC());
1124 #endif
1126 #if !defined(CONFIG_USER_ONLY)
1127 /*****************************************************************************/
1128 /* PowerPC 601 specific instructions (POWER bridge) */
1130 void helper_rfsvc(CPUPPCState *env)
1132 do_rfi(env, env->lr, env->ctr & 0x0000FFFF);
1135 /* Embedded.Processor Control */
1136 static int dbell2irq(target_ulong rb)
1138 int msg = rb & DBELL_TYPE_MASK;
1139 int irq = -1;
1141 switch (msg) {
1142 case DBELL_TYPE_DBELL:
1143 irq = PPC_INTERRUPT_DOORBELL;
1144 break;
1145 case DBELL_TYPE_DBELL_CRIT:
1146 irq = PPC_INTERRUPT_CDOORBELL;
1147 break;
1148 case DBELL_TYPE_G_DBELL:
1149 case DBELL_TYPE_G_DBELL_CRIT:
1150 case DBELL_TYPE_G_DBELL_MC:
1151 /* XXX implement */
1152 default:
1153 break;
1156 return irq;
1159 void helper_msgclr(CPUPPCState *env, target_ulong rb)
1161 int irq = dbell2irq(rb);
1163 if (irq < 0) {
1164 return;
1167 env->pending_interrupts &= ~(1 << irq);
1170 void helper_msgsnd(target_ulong rb)
1172 int irq = dbell2irq(rb);
1173 int pir = rb & DBELL_PIRTAG_MASK;
1174 CPUState *cs;
1176 if (irq < 0) {
1177 return;
1180 qemu_mutex_lock_iothread();
1181 CPU_FOREACH(cs) {
1182 PowerPCCPU *cpu = POWERPC_CPU(cs);
1183 CPUPPCState *cenv = &cpu->env;
1185 if ((rb & DBELL_BRDCAST) || (cenv->spr[SPR_BOOKE_PIR] == pir)) {
1186 cenv->pending_interrupts |= 1 << irq;
1187 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
1190 qemu_mutex_unlock_iothread();
1193 /* Server Processor Control */
1194 static int book3s_dbell2irq(target_ulong rb)
1196 int msg = rb & DBELL_TYPE_MASK;
1198 /* A Directed Hypervisor Doorbell message is sent only if the
1199 * message type is 5. All other types are reserved and the
1200 * instruction is a no-op */
1201 return msg == DBELL_TYPE_DBELL_SERVER ? PPC_INTERRUPT_HDOORBELL : -1;
1204 void helper_book3s_msgclr(CPUPPCState *env, target_ulong rb)
1206 int irq = book3s_dbell2irq(rb);
1208 if (irq < 0) {
1209 return;
1212 env->pending_interrupts &= ~(1 << irq);
1215 void helper_book3s_msgsnd(target_ulong rb)
1217 int irq = book3s_dbell2irq(rb);
1218 int pir = rb & DBELL_PROCIDTAG_MASK;
1219 CPUState *cs;
1221 if (irq < 0) {
1222 return;
1225 qemu_mutex_lock_iothread();
1226 CPU_FOREACH(cs) {
1227 PowerPCCPU *cpu = POWERPC_CPU(cs);
1228 CPUPPCState *cenv = &cpu->env;
1230 /* TODO: broadcast message to all threads of the same processor */
1231 if (cenv->spr_cb[SPR_PIR].default_value == pir) {
1232 cenv->pending_interrupts |= 1 << irq;
1233 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
1236 qemu_mutex_unlock_iothread();
1238 #endif
1240 void ppc_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
1241 MMUAccessType access_type,
1242 int mmu_idx, uintptr_t retaddr)
1244 CPUPPCState *env = cs->env_ptr;
1245 uint32_t insn;
1247 /* Restore state and reload the insn we executed, for filling in DSISR. */
1248 cpu_restore_state(cs, retaddr, true);
1249 insn = cpu_ldl_code(env, env->nip);
1251 cs->exception_index = POWERPC_EXCP_ALIGN;
1252 env->error_code = insn & 0x03FF0000;
1253 cpu_loop_exit(cs);