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/>.
21 #include "disas/disas.h"
23 #include "qemu/bitops.h"
36 #include <sys/ucontext.h>
39 //#define DEBUG_SIGNAL
41 static void exception_action(CPUArchState
*env1
)
43 #if defined(TARGET_I386)
44 raise_exception_err(env1
, env1
->exception_index
, env1
->error_code
);
50 /* exit the current TB from a signal handler. The host registers are
51 restored in a state compatible with the CPU emulator
53 void cpu_resume_from_signal(CPUArchState
*env1
, void *puc
)
56 struct ucontext
*uc
= puc
;
57 #elif defined(__OpenBSD__)
58 struct sigcontext
*uc
= puc
;
62 /* XXX: use siglongjmp ? */
65 sigprocmask(SIG_SETMASK
, (sigset_t
*)&uc
->uc_sigmask
, NULL
);
67 sigprocmask(SIG_SETMASK
, &uc
->uc_sigmask
, NULL
);
69 #elif defined(__OpenBSD__)
70 sigprocmask(SIG_SETMASK
, &uc
->sc_mask
, NULL
);
73 env1
->exception_index
= -1;
74 siglongjmp(env1
->jmp_env
, 1);
77 /* 'pc' is the host PC at which the exception was raised. 'address' is
78 the effective address of the memory exception. 'is_write' is 1 if a
79 write caused the exception and otherwise 0'. 'old_set' is the
80 signal set which should be restored */
81 static inline int handle_cpu_signal(uintptr_t pc
, unsigned long address
,
82 int is_write
, sigset_t
*old_set
,
90 #if defined(DEBUG_SIGNAL)
91 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
92 pc
, address
, is_write
, *(unsigned long *)old_set
);
94 /* XXX: locking issue */
95 if (is_write
&& h2g_valid(address
)
96 && page_unprotect(h2g(address
), pc
, puc
)) {
100 /* Convert forcefully to guest address space, invalid addresses
101 are still valid segv ones */
102 address
= h2g_nocheck(address
);
105 cc
= CPU_GET_CLASS(cpu
);
107 /* see if it is an MMU fault */
108 g_assert(cc
->handle_mmu_fault
);
109 ret
= cc
->handle_mmu_fault(cpu
, address
, is_write
, MMU_USER_IDX
);
111 return 0; /* not an MMU fault */
114 return 1; /* the MMU fault was handled without causing real CPU fault */
116 /* now we have a real cpu fault */
117 cpu_restore_state(env
, pc
);
119 /* we restore the process signal mask as the sigreturn should
120 do it (XXX: use sigsetjmp) */
121 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
122 exception_action(env
);
124 /* never comes here */
128 #if defined(__i386__)
130 #if defined(__APPLE__)
131 #include <sys/ucontext.h>
133 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext->ss.eip))
134 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
135 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
136 #define MASK_sig(context) ((context)->uc_sigmask)
137 #elif defined(__NetBSD__)
138 #include <ucontext.h>
140 #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
141 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
142 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
143 #define MASK_sig(context) ((context)->uc_sigmask)
144 #elif defined(__FreeBSD__) || defined(__DragonFly__)
145 #include <ucontext.h>
147 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
148 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
149 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
150 #define MASK_sig(context) ((context)->uc_sigmask)
151 #elif defined(__OpenBSD__)
152 #define EIP_sig(context) ((context)->sc_eip)
153 #define TRAP_sig(context) ((context)->sc_trapno)
154 #define ERROR_sig(context) ((context)->sc_err)
155 #define MASK_sig(context) ((context)->sc_mask)
157 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
158 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
159 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
160 #define MASK_sig(context) ((context)->uc_sigmask)
163 int cpu_signal_handler(int host_signum
, void *pinfo
,
166 siginfo_t
*info
= pinfo
;
167 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
168 ucontext_t
*uc
= puc
;
169 #elif defined(__OpenBSD__)
170 struct sigcontext
*uc
= puc
;
172 struct ucontext
*uc
= puc
;
181 #define REG_TRAPNO TRAPNO
184 trapno
= TRAP_sig(uc
);
185 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
187 (ERROR_sig(uc
) >> 1) & 1 : 0,
191 #elif defined(__x86_64__)
194 #define PC_sig(context) _UC_MACHINE_PC(context)
195 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
196 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
197 #define MASK_sig(context) ((context)->uc_sigmask)
198 #elif defined(__OpenBSD__)
199 #define PC_sig(context) ((context)->sc_rip)
200 #define TRAP_sig(context) ((context)->sc_trapno)
201 #define ERROR_sig(context) ((context)->sc_err)
202 #define MASK_sig(context) ((context)->sc_mask)
203 #elif defined(__FreeBSD__) || defined(__DragonFly__)
204 #include <ucontext.h>
206 #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
207 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
208 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
209 #define MASK_sig(context) ((context)->uc_sigmask)
211 #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
212 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
213 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
214 #define MASK_sig(context) ((context)->uc_sigmask)
217 int cpu_signal_handler(int host_signum
, void *pinfo
,
220 siginfo_t
*info
= pinfo
;
222 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
223 ucontext_t
*uc
= puc
;
224 #elif defined(__OpenBSD__)
225 struct sigcontext
*uc
= puc
;
227 struct ucontext
*uc
= puc
;
231 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
232 TRAP_sig(uc
) == 0xe ?
233 (ERROR_sig(uc
) >> 1) & 1 : 0,
237 #elif defined(_ARCH_PPC)
239 /***********************************************************************
240 * signal context platform-specific definitions
244 /* All Registers access - only for local access */
245 #define REG_sig(reg_name, context) \
246 ((context)->uc_mcontext.regs->reg_name)
247 /* Gpr Registers access */
248 #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
249 /* Program counter */
250 #define IAR_sig(context) REG_sig(nip, context)
251 /* Machine State Register (Supervisor) */
252 #define MSR_sig(context) REG_sig(msr, context)
254 #define CTR_sig(context) REG_sig(ctr, context)
255 /* User's integer exception register */
256 #define XER_sig(context) REG_sig(xer, context)
258 #define LR_sig(context) REG_sig(link, context)
259 /* Condition register */
260 #define CR_sig(context) REG_sig(ccr, context)
262 /* Float Registers access */
263 #define FLOAT_sig(reg_num, context) \
264 (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
265 #define FPSCR_sig(context) \
266 (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
267 /* Exception Registers access */
268 #define DAR_sig(context) REG_sig(dar, context)
269 #define DSISR_sig(context) REG_sig(dsisr, context)
270 #define TRAP_sig(context) REG_sig(trap, context)
273 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
274 #include <ucontext.h>
275 #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
276 #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
277 #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
278 #define XER_sig(context) ((context)->uc_mcontext.mc_xer)
279 #define LR_sig(context) ((context)->uc_mcontext.mc_lr)
280 #define CR_sig(context) ((context)->uc_mcontext.mc_cr)
281 /* Exception Registers access */
282 #define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
283 #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
284 #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
285 #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
288 #include <sys/ucontext.h>
289 typedef struct ucontext SIGCONTEXT
;
290 /* All Registers access - only for local access */
291 #define REG_sig(reg_name, context) \
292 ((context)->uc_mcontext->ss.reg_name)
293 #define FLOATREG_sig(reg_name, context) \
294 ((context)->uc_mcontext->fs.reg_name)
295 #define EXCEPREG_sig(reg_name, context) \
296 ((context)->uc_mcontext->es.reg_name)
297 #define VECREG_sig(reg_name, context) \
298 ((context)->uc_mcontext->vs.reg_name)
299 /* Gpr Registers access */
300 #define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
301 /* Program counter */
302 #define IAR_sig(context) REG_sig(srr0, context)
303 /* Machine State Register (Supervisor) */
304 #define MSR_sig(context) REG_sig(srr1, context)
305 #define CTR_sig(context) REG_sig(ctr, context)
307 #define XER_sig(context) REG_sig(xer, context)
308 /* User's integer exception register */
309 #define LR_sig(context) REG_sig(lr, context)
310 /* Condition register */
311 #define CR_sig(context) REG_sig(cr, context)
312 /* Float Registers access */
313 #define FLOAT_sig(reg_num, context) \
314 FLOATREG_sig(fpregs[reg_num], context)
315 #define FPSCR_sig(context) \
316 ((double)FLOATREG_sig(fpscr, context))
317 /* Exception Registers access */
318 /* Fault registers for coredump */
319 #define DAR_sig(context) EXCEPREG_sig(dar, context)
320 #define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
321 /* number of powerpc exception taken */
322 #define TRAP_sig(context) EXCEPREG_sig(exception, context)
323 #endif /* __APPLE__ */
325 int cpu_signal_handler(int host_signum
, void *pinfo
,
328 siginfo_t
*info
= pinfo
;
329 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
330 ucontext_t
*uc
= puc
;
332 struct ucontext
*uc
= puc
;
341 if (DSISR_sig(uc
) & 0x00800000) {
345 if (TRAP_sig(uc
) != 0x400 && (DSISR_sig(uc
) & 0x02000000)) {
349 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
350 is_write
, &uc
->uc_sigmask
, puc
);
353 #elif defined(__alpha__)
355 int cpu_signal_handler(int host_signum
, void *pinfo
,
358 siginfo_t
*info
= pinfo
;
359 struct ucontext
*uc
= puc
;
360 uint32_t *pc
= uc
->uc_mcontext
.sc_pc
;
364 /* XXX: need kernel patch to get write flag faster */
365 switch (insn
>> 26) {
368 case 0x0f: /* stq_u */
375 case 0x2e: /* stl_c */
376 case 0x2f: /* stq_c */
380 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
381 is_write
, &uc
->uc_sigmask
, puc
);
383 #elif defined(__sparc__)
385 int cpu_signal_handler(int host_signum
, void *pinfo
,
388 siginfo_t
*info
= pinfo
;
391 #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
392 uint32_t *regs
= (uint32_t *)(info
+ 1);
393 void *sigmask
= (regs
+ 20);
394 /* XXX: is there a standard glibc define ? */
395 unsigned long pc
= regs
[1];
398 struct sigcontext
*sc
= puc
;
399 unsigned long pc
= sc
->sigc_regs
.tpc
;
400 void *sigmask
= (void *)sc
->sigc_mask
;
401 #elif defined(__OpenBSD__)
402 struct sigcontext
*uc
= puc
;
403 unsigned long pc
= uc
->sc_pc
;
404 void *sigmask
= (void *)(long)uc
->sc_mask
;
408 /* XXX: need kernel patch to get write flag faster */
410 insn
= *(uint32_t *)pc
;
411 if ((insn
>> 30) == 3) {
412 switch ((insn
>> 19) & 0x3f) {
414 case 0x15: /* stba */
416 case 0x16: /* stha */
420 case 0x17: /* stda */
422 case 0x1e: /* stxa */
424 case 0x34: /* stfa */
425 case 0x27: /* stdf */
426 case 0x37: /* stdfa */
427 case 0x26: /* stqf */
428 case 0x36: /* stqfa */
429 case 0x25: /* stfsr */
430 case 0x3c: /* casa */
431 case 0x3e: /* casxa */
436 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
437 is_write
, sigmask
, NULL
);
440 #elif defined(__arm__)
442 int cpu_signal_handler(int host_signum
, void *pinfo
,
445 siginfo_t
*info
= pinfo
;
446 struct ucontext
*uc
= puc
;
450 #if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
451 pc
= uc
->uc_mcontext
.gregs
[R15
];
453 pc
= uc
->uc_mcontext
.arm_pc
;
456 /* error_code is the FSR value, in which bit 11 is WnR (assuming a v6 or
457 * later processor; on v5 we will always report this as a read).
459 is_write
= extract32(uc
->uc_mcontext
.error_code
, 11, 1);
460 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
462 &uc
->uc_sigmask
, puc
);
465 #elif defined(__aarch64__)
467 int cpu_signal_handler(int host_signum
, void *pinfo
,
470 siginfo_t
*info
= pinfo
;
471 struct ucontext
*uc
= puc
;
473 int is_write
= 0; /* XXX how to determine? */
475 pc
= uc
->uc_mcontext
.pc
;
476 return handle_cpu_signal(pc
, (uint64_t)info
->si_addr
,
477 is_write
, &uc
->uc_sigmask
, puc
);
480 #elif defined(__mc68000)
482 int cpu_signal_handler(int host_signum
, void *pinfo
,
485 siginfo_t
*info
= pinfo
;
486 struct ucontext
*uc
= puc
;
490 pc
= uc
->uc_mcontext
.gregs
[16];
491 /* XXX: compute is_write */
493 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
495 &uc
->uc_sigmask
, puc
);
498 #elif defined(__ia64)
501 /* This ought to be in <bits/siginfo.h>... */
502 # define __ISR_VALID 1
505 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
507 siginfo_t
*info
= pinfo
;
508 struct ucontext
*uc
= puc
;
512 ip
= uc
->uc_mcontext
.sc_ip
;
513 switch (host_signum
) {
519 if (info
->si_code
&& (info
->si_segvflags
& __ISR_VALID
)) {
520 /* ISR.W (write-access) is bit 33: */
521 is_write
= (info
->si_isr
>> 33) & 1;
528 return handle_cpu_signal(ip
, (unsigned long)info
->si_addr
,
530 (sigset_t
*)&uc
->uc_sigmask
, puc
);
533 #elif defined(__s390__)
535 int cpu_signal_handler(int host_signum
, void *pinfo
,
538 siginfo_t
*info
= pinfo
;
539 struct ucontext
*uc
= puc
;
544 pc
= uc
->uc_mcontext
.psw
.addr
;
546 /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
547 of the normal 2 arguments. The 3rd argument contains the "int_code"
548 from the hardware which does in fact contain the is_write value.
549 The rt signal handler, as far as I can tell, does not give this value
550 at all. Not that we could get to it from here even if it were. */
551 /* ??? This is not even close to complete, since it ignores all
552 of the read-modify-write instructions. */
553 pinsn
= (uint16_t *)pc
;
554 switch (pinsn
[0] >> 8) {
560 case 0xc4: /* RIL format insns */
561 switch (pinsn
[0] & 0xf) {
563 case 0xb: /* STGRL */
564 case 0x7: /* STHRL */
568 case 0xe3: /* RXY format insns */
569 switch (pinsn
[2] & 0xff) {
572 case 0x72: /* STCY */
573 case 0x70: /* STHY */
574 case 0x8e: /* STPQ */
575 case 0x3f: /* STRVH */
576 case 0x3e: /* STRV */
577 case 0x2f: /* STRVG */
582 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
583 is_write
, &uc
->uc_sigmask
, puc
);
586 #elif defined(__mips__)
588 int cpu_signal_handler(int host_signum
, void *pinfo
,
591 siginfo_t
*info
= pinfo
;
592 struct ucontext
*uc
= puc
;
593 greg_t pc
= uc
->uc_mcontext
.pc
;
596 /* XXX: compute is_write */
598 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
599 is_write
, &uc
->uc_sigmask
, puc
);
602 #elif defined(__hppa__)
604 int cpu_signal_handler(int host_signum
, void *pinfo
,
607 siginfo_t
*info
= pinfo
;
608 struct ucontext
*uc
= puc
;
609 unsigned long pc
= uc
->uc_mcontext
.sc_iaoq
[0];
610 uint32_t insn
= *(uint32_t *)pc
;
613 /* XXX: need kernel patch to get write flag faster. */
614 switch (insn
>> 26) {
618 case 0x1b: /* STWM */
622 case 0x09: /* CSTWX, FSTWX, FSTWS */
623 case 0x0b: /* CSTDX, FSTDX, FSTDS */
624 /* Distinguish from coprocessor load ... */
625 is_write
= (insn
>> 9) & 1;
629 switch ((insn
>> 6) & 15) {
633 case 0xe: /* STWAS */
634 case 0xc: /* STBYS */
640 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
641 is_write
, &uc
->uc_sigmask
, puc
);
646 #error host CPU specific signal handler needed