4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
23 #include "cpu_loop-common.h"
24 #include "signal-common.h"
26 static inline uint64_t cpu_ppc_get_tb(CPUPPCState
*env
)
28 return cpu_get_host_ticks();
31 uint64_t cpu_ppc_load_tbl(CPUPPCState
*env
)
33 return cpu_ppc_get_tb(env
);
36 uint32_t cpu_ppc_load_tbu(CPUPPCState
*env
)
38 return cpu_ppc_get_tb(env
) >> 32;
41 uint64_t cpu_ppc_load_atbl(CPUPPCState
*env
)
43 return cpu_ppc_get_tb(env
);
46 uint32_t cpu_ppc_load_atbu(CPUPPCState
*env
)
48 return cpu_ppc_get_tb(env
) >> 32;
51 uint64_t cpu_ppc_load_vtb(CPUPPCState
*env
)
53 return cpu_ppc_get_tb(env
);
56 uint32_t cpu_ppc601_load_rtcu(CPUPPCState
*env
)
57 __attribute__ (( alias ("cpu_ppc_load_tbu") ));
59 uint32_t cpu_ppc601_load_rtcl(CPUPPCState
*env
)
61 return cpu_ppc_load_tbl(env
) & 0x3FFFFF80;
64 /* XXX: to be fixed */
65 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t *valp
)
70 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t val
)
75 void cpu_loop(CPUPPCState
*env
)
77 CPUState
*cs
= env_cpu(env
);
78 target_siginfo_t info
;
86 trapnr
= cpu_exec(cs
);
88 process_queued_cpu_work(cs
);
90 arch_interrupt
= true;
92 case POWERPC_EXCP_NONE
:
95 case POWERPC_EXCP_CRITICAL
: /* Critical input */
96 cpu_abort(cs
, "Critical interrupt while in user mode. "
99 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
100 cpu_abort(cs
, "Machine check exception while in user mode. "
103 case POWERPC_EXCP_DSI
: /* Data storage exception */
104 /* XXX: check this. Seems bugged */
105 switch (env
->error_code
& 0xFF000000) {
108 info
.si_signo
= TARGET_SIGSEGV
;
110 info
.si_code
= TARGET_SEGV_MAPERR
;
113 info
.si_signo
= TARGET_SIGILL
;
115 info
.si_code
= TARGET_ILL_ILLADR
;
118 info
.si_signo
= TARGET_SIGSEGV
;
120 info
.si_code
= TARGET_SEGV_ACCERR
;
123 /* Let's send a regular segfault... */
124 EXCP_DUMP(env
, "Invalid segfault errno (%02x)\n",
126 info
.si_signo
= TARGET_SIGSEGV
;
128 info
.si_code
= TARGET_SEGV_MAPERR
;
131 info
._sifields
._sigfault
._addr
= env
->spr
[SPR_DAR
];
132 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
134 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
135 /* XXX: check this */
136 switch (env
->error_code
& 0xFF000000) {
138 info
.si_signo
= TARGET_SIGSEGV
;
140 info
.si_code
= TARGET_SEGV_MAPERR
;
144 info
.si_signo
= TARGET_SIGSEGV
;
146 info
.si_code
= TARGET_SEGV_ACCERR
;
149 /* Let's send a regular segfault... */
150 EXCP_DUMP(env
, "Invalid segfault errno (%02x)\n",
152 info
.si_signo
= TARGET_SIGSEGV
;
154 info
.si_code
= TARGET_SEGV_MAPERR
;
157 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
158 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
160 case POWERPC_EXCP_EXTERNAL
: /* External input */
161 cpu_abort(cs
, "External interrupt while in user mode. "
164 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
165 /* XXX: check this */
166 info
.si_signo
= TARGET_SIGBUS
;
168 info
.si_code
= TARGET_BUS_ADRALN
;
169 info
._sifields
._sigfault
._addr
= env
->nip
;
170 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
172 case POWERPC_EXCP_PROGRAM
: /* Program exception */
173 case POWERPC_EXCP_HV_EMU
: /* HV emulation */
174 /* XXX: check this */
175 switch (env
->error_code
& ~0xF) {
176 case POWERPC_EXCP_FP
:
177 info
.si_signo
= TARGET_SIGFPE
;
179 switch (env
->error_code
& 0xF) {
180 case POWERPC_EXCP_FP_OX
:
181 info
.si_code
= TARGET_FPE_FLTOVF
;
183 case POWERPC_EXCP_FP_UX
:
184 info
.si_code
= TARGET_FPE_FLTUND
;
186 case POWERPC_EXCP_FP_ZX
:
187 case POWERPC_EXCP_FP_VXZDZ
:
188 info
.si_code
= TARGET_FPE_FLTDIV
;
190 case POWERPC_EXCP_FP_XX
:
191 info
.si_code
= TARGET_FPE_FLTRES
;
193 case POWERPC_EXCP_FP_VXSOFT
:
194 info
.si_code
= TARGET_FPE_FLTINV
;
196 case POWERPC_EXCP_FP_VXSNAN
:
197 case POWERPC_EXCP_FP_VXISI
:
198 case POWERPC_EXCP_FP_VXIDI
:
199 case POWERPC_EXCP_FP_VXIMZ
:
200 case POWERPC_EXCP_FP_VXVC
:
201 case POWERPC_EXCP_FP_VXSQRT
:
202 case POWERPC_EXCP_FP_VXCVI
:
203 info
.si_code
= TARGET_FPE_FLTSUB
;
206 EXCP_DUMP(env
, "Unknown floating point exception (%02x)\n",
211 case POWERPC_EXCP_INVAL
:
212 info
.si_signo
= TARGET_SIGILL
;
214 switch (env
->error_code
& 0xF) {
215 case POWERPC_EXCP_INVAL_INVAL
:
216 info
.si_code
= TARGET_ILL_ILLOPC
;
218 case POWERPC_EXCP_INVAL_LSWX
:
219 info
.si_code
= TARGET_ILL_ILLOPN
;
221 case POWERPC_EXCP_INVAL_SPR
:
222 info
.si_code
= TARGET_ILL_PRVREG
;
224 case POWERPC_EXCP_INVAL_FP
:
225 info
.si_code
= TARGET_ILL_COPROC
;
228 EXCP_DUMP(env
, "Unknown invalid operation (%02x)\n",
229 env
->error_code
& 0xF);
230 info
.si_code
= TARGET_ILL_ILLADR
;
234 case POWERPC_EXCP_PRIV
:
235 info
.si_signo
= TARGET_SIGILL
;
237 switch (env
->error_code
& 0xF) {
238 case POWERPC_EXCP_PRIV_OPC
:
239 info
.si_code
= TARGET_ILL_PRVOPC
;
241 case POWERPC_EXCP_PRIV_REG
:
242 info
.si_code
= TARGET_ILL_PRVREG
;
245 EXCP_DUMP(env
, "Unknown privilege violation (%02x)\n",
246 env
->error_code
& 0xF);
247 info
.si_code
= TARGET_ILL_PRVOPC
;
251 case POWERPC_EXCP_TRAP
:
252 cpu_abort(cs
, "Tried to call a TRAP\n");
255 /* Should not happen ! */
256 cpu_abort(cs
, "Unknown program exception (%02x)\n",
260 info
._sifields
._sigfault
._addr
= env
->nip
;
261 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
263 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
264 info
.si_signo
= TARGET_SIGILL
;
266 info
.si_code
= TARGET_ILL_COPROC
;
267 info
._sifields
._sigfault
._addr
= env
->nip
;
268 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
270 case POWERPC_EXCP_SYSCALL
: /* System call exception */
271 case POWERPC_EXCP_SYSCALL_VECTORED
:
272 cpu_abort(cs
, "Syscall exception while in user mode. "
275 case POWERPC_EXCP_APU
: /* Auxiliary processor unavailable */
276 info
.si_signo
= TARGET_SIGILL
;
278 info
.si_code
= TARGET_ILL_COPROC
;
279 info
._sifields
._sigfault
._addr
= env
->nip
;
280 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
282 case POWERPC_EXCP_DECR
: /* Decrementer exception */
283 cpu_abort(cs
, "Decrementer interrupt while in user mode. "
286 case POWERPC_EXCP_FIT
: /* Fixed-interval timer interrupt */
287 cpu_abort(cs
, "Fix interval timer interrupt while in user mode. "
290 case POWERPC_EXCP_WDT
: /* Watchdog timer interrupt */
291 cpu_abort(cs
, "Watchdog timer interrupt while in user mode. "
294 case POWERPC_EXCP_DTLB
: /* Data TLB error */
295 cpu_abort(cs
, "Data TLB exception while in user mode. "
298 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
299 cpu_abort(cs
, "Instruction TLB exception while in user mode. "
302 case POWERPC_EXCP_SPEU
: /* SPE/embedded floating-point unavail. */
303 info
.si_signo
= TARGET_SIGILL
;
305 info
.si_code
= TARGET_ILL_COPROC
;
306 info
._sifields
._sigfault
._addr
= env
->nip
;
307 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
309 case POWERPC_EXCP_EFPDI
: /* Embedded floating-point data IRQ */
310 cpu_abort(cs
, "Embedded floating-point data IRQ not handled\n");
312 case POWERPC_EXCP_EFPRI
: /* Embedded floating-point round IRQ */
313 cpu_abort(cs
, "Embedded floating-point round IRQ not handled\n");
315 case POWERPC_EXCP_EPERFM
: /* Embedded performance monitor IRQ */
316 cpu_abort(cs
, "Performance monitor exception not handled\n");
318 case POWERPC_EXCP_DOORI
: /* Embedded doorbell interrupt */
319 cpu_abort(cs
, "Doorbell interrupt while in user mode. "
322 case POWERPC_EXCP_DOORCI
: /* Embedded doorbell critical interrupt */
323 cpu_abort(cs
, "Doorbell critical interrupt while in user mode. "
326 case POWERPC_EXCP_RESET
: /* System reset exception */
327 cpu_abort(cs
, "Reset interrupt while in user mode. "
330 case POWERPC_EXCP_DSEG
: /* Data segment exception */
331 cpu_abort(cs
, "Data segment exception while in user mode. "
334 case POWERPC_EXCP_ISEG
: /* Instruction segment exception */
335 cpu_abort(cs
, "Instruction segment exception "
336 "while in user mode. Aborting\n");
338 /* PowerPC 64 with hypervisor mode support */
339 case POWERPC_EXCP_HDECR
: /* Hypervisor decrementer exception */
340 cpu_abort(cs
, "Hypervisor decrementer interrupt "
341 "while in user mode. Aborting\n");
343 case POWERPC_EXCP_TRACE
: /* Trace exception */
345 * we use this exception to emulate step-by-step execution mode.
348 /* PowerPC 64 with hypervisor mode support */
349 case POWERPC_EXCP_HDSI
: /* Hypervisor data storage exception */
350 cpu_abort(cs
, "Hypervisor data storage exception "
351 "while in user mode. Aborting\n");
353 case POWERPC_EXCP_HISI
: /* Hypervisor instruction storage excp */
354 cpu_abort(cs
, "Hypervisor instruction storage exception "
355 "while in user mode. Aborting\n");
357 case POWERPC_EXCP_HDSEG
: /* Hypervisor data segment exception */
358 cpu_abort(cs
, "Hypervisor data segment exception "
359 "while in user mode. Aborting\n");
361 case POWERPC_EXCP_HISEG
: /* Hypervisor instruction segment excp */
362 cpu_abort(cs
, "Hypervisor instruction segment exception "
363 "while in user mode. Aborting\n");
365 case POWERPC_EXCP_VPU
: /* Vector unavailable exception */
366 info
.si_signo
= TARGET_SIGILL
;
368 info
.si_code
= TARGET_ILL_COPROC
;
369 info
._sifields
._sigfault
._addr
= env
->nip
;
370 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
372 case POWERPC_EXCP_PIT
: /* Programmable interval timer IRQ */
373 cpu_abort(cs
, "Programmable interval timer interrupt "
374 "while in user mode. Aborting\n");
376 case POWERPC_EXCP_IO
: /* IO error exception */
377 cpu_abort(cs
, "IO error exception while in user mode. "
380 case POWERPC_EXCP_RUNM
: /* Run mode exception */
381 cpu_abort(cs
, "Run mode exception while in user mode. "
384 case POWERPC_EXCP_EMUL
: /* Emulation trap exception */
385 cpu_abort(cs
, "Emulation trap exception not handled\n");
387 case POWERPC_EXCP_IFTLB
: /* Instruction fetch TLB error */
388 cpu_abort(cs
, "Instruction fetch TLB exception "
389 "while in user-mode. Aborting");
391 case POWERPC_EXCP_DLTLB
: /* Data load TLB miss */
392 cpu_abort(cs
, "Data load TLB exception while in user-mode. "
395 case POWERPC_EXCP_DSTLB
: /* Data store TLB miss */
396 cpu_abort(cs
, "Data store TLB exception while in user-mode. "
399 case POWERPC_EXCP_FPA
: /* Floating-point assist exception */
400 cpu_abort(cs
, "Floating-point assist exception not handled\n");
402 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
403 cpu_abort(cs
, "Instruction address breakpoint exception "
406 case POWERPC_EXCP_SMI
: /* System management interrupt */
407 cpu_abort(cs
, "System management interrupt while in user mode. "
410 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
411 cpu_abort(cs
, "Thermal interrupt interrupt while in user mode. "
414 case POWERPC_EXCP_PERFM
: /* Embedded performance monitor IRQ */
415 cpu_abort(cs
, "Performance monitor exception not handled\n");
417 case POWERPC_EXCP_VPUA
: /* Vector assist exception */
418 cpu_abort(cs
, "Vector assist exception not handled\n");
420 case POWERPC_EXCP_SOFTP
: /* Soft patch exception */
421 cpu_abort(cs
, "Soft patch exception not handled\n");
423 case POWERPC_EXCP_MAINT
: /* Maintenance exception */
424 cpu_abort(cs
, "Maintenance exception while in user mode. "
427 case POWERPC_EXCP_SYSCALL_USER
:
428 /* system call in user-mode emulation */
430 * PPC ABI uses overflow flag in cr0 to signal an error
435 ret
= do_syscall(env
, env
->gpr
[0], env
->gpr
[3], env
->gpr
[4],
436 env
->gpr
[5], env
->gpr
[6], env
->gpr
[7],
438 if (ret
== -TARGET_ERESTARTSYS
) {
442 if (ret
== (target_ulong
)(-TARGET_QEMU_ESIGRETURN
)) {
443 /* Returning from a successful sigreturn syscall.
444 Avoid corrupting register state. */
447 if (ret
> (target_ulong
)(-515)) {
454 info
.si_signo
= TARGET_SIGTRAP
;
456 info
.si_code
= TARGET_TRAP_BRKPT
;
457 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
460 /* just indicate that signals should be handled asap */
463 cpu_exec_step_atomic(cs
);
464 arch_interrupt
= false;
467 cpu_abort(cs
, "Unknown exception 0x%x. Aborting\n", trapnr
);
470 process_pending_signals(env
);
472 /* Most of the traps imply a transition through kernel mode,
473 * which implies an REI instruction has been executed. Which
474 * means that RX and LOCK_ADDR should be cleared. But there
475 * are a few exceptions for traps internal to QEMU.
477 if (arch_interrupt
) {
478 env
->reserve_addr
= -1;
483 void target_cpu_copy_regs(CPUArchState
*env
, struct target_pt_regs
*regs
)
487 #if defined(TARGET_PPC64)
488 int flag
= (env
->insns_flags2
& PPC2_BOOKE206
) ? MSR_CM
: MSR_SF
;
489 #if defined(TARGET_ABI32)
490 ppc_store_msr(env
, env
->msr
& ~((target_ulong
)1 << flag
));
492 ppc_store_msr(env
, env
->msr
| (target_ulong
)1 << flag
);
496 env
->nip
= regs
->nip
;
497 for(i
= 0; i
< 32; i
++) {
498 env
->gpr
[i
] = regs
->gpr
[i
];