2 * User emulator execution
4 * Copyright (c) 2003-2005 Fabrice Bellard
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 "disas/disas.h"
23 #include "qemu/bitops.h"
24 #include "exec/cpu_ldst.h"
25 #include "translate-all.h"
37 #include <sys/ucontext.h>
40 //#define DEBUG_SIGNAL
42 static void exception_action(CPUState
*cpu
)
44 #if defined(TARGET_I386)
45 X86CPU
*x86_cpu
= X86_CPU(cpu
);
46 CPUX86State
*env1
= &x86_cpu
->env
;
48 raise_exception_err(env1
, cpu
->exception_index
, env1
->error_code
);
54 /* exit the current TB from a signal handler. The host registers are
55 restored in a state compatible with the CPU emulator
57 void cpu_resume_from_signal(CPUState
*cpu
, void *puc
)
60 struct ucontext
*uc
= puc
;
61 #elif defined(__OpenBSD__)
62 struct sigcontext
*uc
= puc
;
66 /* XXX: use siglongjmp ? */
69 sigprocmask(SIG_SETMASK
, (sigset_t
*)&uc
->uc_sigmask
, NULL
);
71 sigprocmask(SIG_SETMASK
, &uc
->uc_sigmask
, NULL
);
73 #elif defined(__OpenBSD__)
74 sigprocmask(SIG_SETMASK
, &uc
->sc_mask
, NULL
);
77 cpu
->exception_index
= -1;
78 siglongjmp(cpu
->jmp_env
, 1);
81 /* 'pc' is the host PC at which the exception was raised. 'address' is
82 the effective address of the memory exception. 'is_write' is 1 if a
83 write caused the exception and otherwise 0'. 'old_set' is the
84 signal set which should be restored */
85 static inline int handle_cpu_signal(uintptr_t pc
, void *ptr
,
86 int is_write
, sigset_t
*old_set
,
89 uintptr_t address
= (uintptr_t)ptr
;
94 #if defined(DEBUG_SIGNAL)
95 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
96 pc
, address
, is_write
, *(unsigned long *)old_set
);
98 /* XXX: locking issue */
99 if (is_write
&& h2g_valid(address
)
100 && page_unprotect(h2g(address
), pc
, puc
)) {
104 /* Convert forcefully to guest address space, invalid addresses
105 are still valid segv ones */
106 address
= h2g_nocheck(address
);
109 cc
= CPU_GET_CLASS(cpu
);
110 /* see if it is an MMU fault */
111 g_assert(cc
->handle_mmu_fault
);
112 ret
= cc
->handle_mmu_fault(cpu
, address
, is_write
, MMU_USER_IDX
);
114 return 0; /* not an MMU fault */
117 return 1; /* the MMU fault was handled without causing real CPU fault */
119 /* now we have a real cpu fault */
120 cpu_restore_state(cpu
, pc
);
122 /* we restore the process signal mask as the sigreturn should
123 do it (XXX: use sigsetjmp) */
124 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
125 exception_action(cpu
);
127 /* never comes here */
131 #if defined(__i386__)
133 #if defined(__APPLE__)
134 #include <sys/ucontext.h>
136 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext->ss.eip))
137 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
138 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
139 #define MASK_sig(context) ((context)->uc_sigmask)
140 #elif defined(__NetBSD__)
141 #include <ucontext.h>
143 #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
144 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
145 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
146 #define MASK_sig(context) ((context)->uc_sigmask)
147 #elif defined(__FreeBSD__) || defined(__DragonFly__)
148 #include <ucontext.h>
150 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
151 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
152 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
153 #define MASK_sig(context) ((context)->uc_sigmask)
154 #elif defined(__OpenBSD__)
155 #define EIP_sig(context) ((context)->sc_eip)
156 #define TRAP_sig(context) ((context)->sc_trapno)
157 #define ERROR_sig(context) ((context)->sc_err)
158 #define MASK_sig(context) ((context)->sc_mask)
160 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
161 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
162 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
163 #define MASK_sig(context) ((context)->uc_sigmask)
166 int cpu_signal_handler(int host_signum
, void *pinfo
,
169 siginfo_t
*info
= pinfo
;
170 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
171 ucontext_t
*uc
= puc
;
172 #elif defined(__OpenBSD__)
173 struct sigcontext
*uc
= puc
;
175 struct ucontext
*uc
= puc
;
184 #define REG_TRAPNO TRAPNO
187 trapno
= TRAP_sig(uc
);
188 return handle_cpu_signal(pc
, info
->si_addr
,
190 (ERROR_sig(uc
) >> 1) & 1 : 0,
194 #elif defined(__x86_64__)
197 #define PC_sig(context) _UC_MACHINE_PC(context)
198 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
199 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
200 #define MASK_sig(context) ((context)->uc_sigmask)
201 #elif defined(__OpenBSD__)
202 #define PC_sig(context) ((context)->sc_rip)
203 #define TRAP_sig(context) ((context)->sc_trapno)
204 #define ERROR_sig(context) ((context)->sc_err)
205 #define MASK_sig(context) ((context)->sc_mask)
206 #elif defined(__FreeBSD__) || defined(__DragonFly__)
207 #include <ucontext.h>
209 #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
210 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
211 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
212 #define MASK_sig(context) ((context)->uc_sigmask)
214 #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
215 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
216 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
217 #define MASK_sig(context) ((context)->uc_sigmask)
220 int cpu_signal_handler(int host_signum
, void *pinfo
,
223 siginfo_t
*info
= pinfo
;
225 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
226 ucontext_t
*uc
= puc
;
227 #elif defined(__OpenBSD__)
228 struct sigcontext
*uc
= puc
;
230 struct ucontext
*uc
= puc
;
234 return handle_cpu_signal(pc
, info
->si_addr
,
235 TRAP_sig(uc
) == 0xe ?
236 (ERROR_sig(uc
) >> 1) & 1 : 0,
240 #elif defined(_ARCH_PPC)
242 /***********************************************************************
243 * signal context platform-specific definitions
247 /* All Registers access - only for local access */
248 #define REG_sig(reg_name, context) \
249 ((context)->uc_mcontext.regs->reg_name)
250 /* Gpr Registers access */
251 #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
252 /* Program counter */
253 #define IAR_sig(context) REG_sig(nip, context)
254 /* Machine State Register (Supervisor) */
255 #define MSR_sig(context) REG_sig(msr, context)
257 #define CTR_sig(context) REG_sig(ctr, context)
258 /* User's integer exception register */
259 #define XER_sig(context) REG_sig(xer, context)
261 #define LR_sig(context) REG_sig(link, context)
262 /* Condition register */
263 #define CR_sig(context) REG_sig(ccr, context)
265 /* Float Registers access */
266 #define FLOAT_sig(reg_num, context) \
267 (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
268 #define FPSCR_sig(context) \
269 (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
270 /* Exception Registers access */
271 #define DAR_sig(context) REG_sig(dar, context)
272 #define DSISR_sig(context) REG_sig(dsisr, context)
273 #define TRAP_sig(context) REG_sig(trap, context)
276 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
277 #include <ucontext.h>
278 #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
279 #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
280 #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
281 #define XER_sig(context) ((context)->uc_mcontext.mc_xer)
282 #define LR_sig(context) ((context)->uc_mcontext.mc_lr)
283 #define CR_sig(context) ((context)->uc_mcontext.mc_cr)
284 /* Exception Registers access */
285 #define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
286 #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
287 #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
288 #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
291 #include <sys/ucontext.h>
292 typedef struct ucontext SIGCONTEXT
;
293 /* All Registers access - only for local access */
294 #define REG_sig(reg_name, context) \
295 ((context)->uc_mcontext->ss.reg_name)
296 #define FLOATREG_sig(reg_name, context) \
297 ((context)->uc_mcontext->fs.reg_name)
298 #define EXCEPREG_sig(reg_name, context) \
299 ((context)->uc_mcontext->es.reg_name)
300 #define VECREG_sig(reg_name, context) \
301 ((context)->uc_mcontext->vs.reg_name)
302 /* Gpr Registers access */
303 #define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
304 /* Program counter */
305 #define IAR_sig(context) REG_sig(srr0, context)
306 /* Machine State Register (Supervisor) */
307 #define MSR_sig(context) REG_sig(srr1, context)
308 #define CTR_sig(context) REG_sig(ctr, context)
310 #define XER_sig(context) REG_sig(xer, context)
311 /* User's integer exception register */
312 #define LR_sig(context) REG_sig(lr, context)
313 /* Condition register */
314 #define CR_sig(context) REG_sig(cr, context)
315 /* Float Registers access */
316 #define FLOAT_sig(reg_num, context) \
317 FLOATREG_sig(fpregs[reg_num], context)
318 #define FPSCR_sig(context) \
319 ((double)FLOATREG_sig(fpscr, context))
320 /* Exception Registers access */
321 /* Fault registers for coredump */
322 #define DAR_sig(context) EXCEPREG_sig(dar, context)
323 #define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
324 /* number of powerpc exception taken */
325 #define TRAP_sig(context) EXCEPREG_sig(exception, context)
326 #endif /* __APPLE__ */
328 int cpu_signal_handler(int host_signum
, void *pinfo
,
331 siginfo_t
*info
= pinfo
;
332 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
333 ucontext_t
*uc
= puc
;
335 struct ucontext
*uc
= puc
;
344 if (DSISR_sig(uc
) & 0x00800000) {
348 if (TRAP_sig(uc
) != 0x400 && (DSISR_sig(uc
) & 0x02000000)) {
352 return handle_cpu_signal(pc
, info
->si_addr
, is_write
, &uc
->uc_sigmask
, puc
);
355 #elif defined(__alpha__)
357 int cpu_signal_handler(int host_signum
, void *pinfo
,
360 siginfo_t
*info
= pinfo
;
361 struct ucontext
*uc
= puc
;
362 uint32_t *pc
= uc
->uc_mcontext
.sc_pc
;
366 /* XXX: need kernel patch to get write flag faster */
367 switch (insn
>> 26) {
370 case 0x0f: /* stq_u */
377 case 0x2e: /* stl_c */
378 case 0x2f: /* stq_c */
382 return handle_cpu_signal(pc
, info
->si_addr
, is_write
, &uc
->uc_sigmask
, puc
);
384 #elif defined(__sparc__)
386 int cpu_signal_handler(int host_signum
, void *pinfo
,
389 siginfo_t
*info
= pinfo
;
392 #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
393 uint32_t *regs
= (uint32_t *)(info
+ 1);
394 void *sigmask
= (regs
+ 20);
395 /* XXX: is there a standard glibc define ? */
396 uintptr_t pc
= regs
[1];
399 struct sigcontext
*sc
= puc
;
400 uintptr_t pc
= sc
->sigc_regs
.tpc
;
401 void *sigmask
= (void *)sc
->sigc_mask
;
402 #elif defined(__OpenBSD__)
403 struct sigcontext
*uc
= puc
;
404 uintptr_t pc
= uc
->sc_pc
;
405 void *sigmask
= (void *)(long)uc
->sc_mask
;
406 #elif defined(__NetBSD__)
407 ucontext_t
*uc
= puc
;
408 unsigned long pc
= _UC_MACHINE_PC(uc
);
409 void *sigmask
= (void *)&uc
->uc_sigmask
;
413 /* XXX: need kernel patch to get write flag faster */
415 insn
= *(uint32_t *)pc
;
416 if ((insn
>> 30) == 3) {
417 switch ((insn
>> 19) & 0x3f) {
419 case 0x15: /* stba */
421 case 0x16: /* stha */
425 case 0x17: /* stda */
427 case 0x1e: /* stxa */
429 case 0x34: /* stfa */
430 case 0x27: /* stdf */
431 case 0x37: /* stdfa */
432 case 0x26: /* stqf */
433 case 0x36: /* stqfa */
434 case 0x25: /* stfsr */
435 case 0x3c: /* casa */
436 case 0x3e: /* casxa */
441 return handle_cpu_signal(pc
, info
->si_addr
, is_write
, sigmask
, NULL
);
444 #elif defined(__arm__)
446 #if defined(__NetBSD__)
447 #include <ucontext.h>
450 int cpu_signal_handler(int host_signum
, void *pinfo
,
453 siginfo_t
*info
= pinfo
;
454 #if defined(__NetBSD__)
455 ucontext_t
*uc
= puc
;
457 struct ucontext
*uc
= puc
;
462 #if defined(__NetBSD__)
463 pc
= uc
->uc_mcontext
.__gregs
[_REG_R15
];
464 #elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
465 pc
= uc
->uc_mcontext
.gregs
[R15
];
467 pc
= uc
->uc_mcontext
.arm_pc
;
470 /* error_code is the FSR value, in which bit 11 is WnR (assuming a v6 or
471 * later processor; on v5 we will always report this as a read).
473 is_write
= extract32(uc
->uc_mcontext
.error_code
, 11, 1);
474 return handle_cpu_signal(pc
, info
->si_addr
, is_write
, &uc
->uc_sigmask
, puc
);
477 #elif defined(__aarch64__)
479 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
481 siginfo_t
*info
= pinfo
;
482 struct ucontext
*uc
= puc
;
483 uintptr_t pc
= uc
->uc_mcontext
.pc
;
484 uint32_t insn
= *(uint32_t *)pc
;
487 /* XXX: need kernel patch to get write flag faster. */
488 is_write
= ( (insn
& 0xbfff0000) == 0x0c000000 /* C3.3.1 */
489 || (insn
& 0xbfe00000) == 0x0c800000 /* C3.3.2 */
490 || (insn
& 0xbfdf0000) == 0x0d000000 /* C3.3.3 */
491 || (insn
& 0xbfc00000) == 0x0d800000 /* C3.3.4 */
492 || (insn
& 0x3f400000) == 0x08000000 /* C3.3.6 */
493 || (insn
& 0x3bc00000) == 0x39000000 /* C3.3.13 */
494 || (insn
& 0x3fc00000) == 0x3d800000 /* ... 128bit */
495 /* Ingore bits 10, 11 & 21, controlling indexing. */
496 || (insn
& 0x3bc00000) == 0x38000000 /* C3.3.8-12 */
497 || (insn
& 0x3fe00000) == 0x3c800000 /* ... 128bit */
498 /* Ignore bits 23 & 24, controlling indexing. */
499 || (insn
& 0x3a400000) == 0x28000000); /* C3.3.7,14-16 */
501 return handle_cpu_signal(pc
, (uintptr_t)info
->si_addr
,
502 is_write
, &uc
->uc_sigmask
, puc
);
505 #elif defined(__mc68000)
507 int cpu_signal_handler(int host_signum
, void *pinfo
,
510 siginfo_t
*info
= pinfo
;
511 struct ucontext
*uc
= puc
;
515 pc
= uc
->uc_mcontext
.gregs
[16];
516 /* XXX: compute is_write */
518 return handle_cpu_signal(pc
, info
->si_addr
, is_write
, &uc
->uc_sigmask
, puc
);
521 #elif defined(__ia64)
524 /* This ought to be in <bits/siginfo.h>... */
525 # define __ISR_VALID 1
528 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
530 siginfo_t
*info
= pinfo
;
531 struct ucontext
*uc
= puc
;
535 ip
= uc
->uc_mcontext
.sc_ip
;
536 switch (host_signum
) {
542 if (info
->si_code
&& (info
->si_segvflags
& __ISR_VALID
)) {
543 /* ISR.W (write-access) is bit 33: */
544 is_write
= (info
->si_isr
>> 33) & 1;
551 return handle_cpu_signal(ip
, info
->si_addr
, is_write
,
552 (sigset_t
*)&uc
->uc_sigmask
, puc
);
555 #elif defined(__s390__)
557 int cpu_signal_handler(int host_signum
, void *pinfo
,
560 siginfo_t
*info
= pinfo
;
561 struct ucontext
*uc
= puc
;
566 pc
= uc
->uc_mcontext
.psw
.addr
;
568 /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
569 of the normal 2 arguments. The 3rd argument contains the "int_code"
570 from the hardware which does in fact contain the is_write value.
571 The rt signal handler, as far as I can tell, does not give this value
572 at all. Not that we could get to it from here even if it were. */
573 /* ??? This is not even close to complete, since it ignores all
574 of the read-modify-write instructions. */
575 pinsn
= (uint16_t *)pc
;
576 switch (pinsn
[0] >> 8) {
582 case 0xc4: /* RIL format insns */
583 switch (pinsn
[0] & 0xf) {
585 case 0xb: /* STGRL */
586 case 0x7: /* STHRL */
590 case 0xe3: /* RXY format insns */
591 switch (pinsn
[2] & 0xff) {
594 case 0x72: /* STCY */
595 case 0x70: /* STHY */
596 case 0x8e: /* STPQ */
597 case 0x3f: /* STRVH */
598 case 0x3e: /* STRV */
599 case 0x2f: /* STRVG */
604 return handle_cpu_signal(pc
, info
->si_addr
, is_write
, &uc
->uc_sigmask
, puc
);
607 #elif defined(__mips__)
609 int cpu_signal_handler(int host_signum
, void *pinfo
,
612 siginfo_t
*info
= pinfo
;
613 struct ucontext
*uc
= puc
;
614 greg_t pc
= uc
->uc_mcontext
.pc
;
617 /* XXX: compute is_write */
619 return handle_cpu_signal(pc
, info
->si_addr
, is_write
, &uc
->uc_sigmask
, puc
);
622 #elif defined(__hppa__)
624 int cpu_signal_handler(int host_signum
, void *pinfo
,
627 siginfo_t
*info
= pinfo
;
628 struct ucontext
*uc
= puc
;
629 uintptr_t pc
= uc
->uc_mcontext
.sc_iaoq
[0];
630 uint32_t insn
= *(uint32_t *)pc
;
633 /* XXX: need kernel patch to get write flag faster. */
634 switch (insn
>> 26) {
638 case 0x1b: /* STWM */
642 case 0x09: /* CSTWX, FSTWX, FSTWS */
643 case 0x0b: /* CSTDX, FSTDX, FSTDS */
644 /* Distinguish from coprocessor load ... */
645 is_write
= (insn
>> 9) & 1;
649 switch ((insn
>> 6) & 15) {
653 case 0xe: /* STWAS */
654 case 0xc: /* STBYS */
660 return handle_cpu_signal(pc
, info
->si_addr
, is_write
, &uc
->uc_sigmask
, puc
);
665 #error host CPU specific signal handler needed