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 #ifndef CONFIG_TCG_PASS_AREG0
22 #include "dyngen-exec.h"
38 #include <sys/ucontext.h>
41 //#define DEBUG_SIGNAL
43 static void exception_action(CPUArchState *env1)
45 #if defined(TARGET_I386)
46 raise_exception_err(env1, env1->exception_index, env1->error_code);
52 /* exit the current TB from a signal handler. The host registers are
53 restored in a state compatible with the CPU emulator
55 void cpu_resume_from_signal(CPUArchState *env1, void *puc)
58 struct ucontext *uc = puc;
59 #elif defined(__OpenBSD__)
60 struct sigcontext *uc = puc;
63 #ifndef CONFIG_TCG_PASS_AREG0
66 /* XXX: restore cpu registers saved in host registers */
70 /* XXX: use siglongjmp ? */
73 sigprocmask(SIG_SETMASK, (sigset_t *)&uc->uc_sigmask, NULL);
75 sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
77 #elif defined(__OpenBSD__)
78 sigprocmask(SIG_SETMASK, &uc->sc_mask, NULL);
81 env1->exception_index = -1;
82 longjmp(env1->jmp_env, 1);
85 /* 'pc' is the host PC at which the exception was raised. 'address' is
86 the effective address of the memory exception. 'is_write' is 1 if a
87 write caused the exception and otherwise 0'. 'old_set' is the
88 signal set which should be restored */
89 static inline int handle_cpu_signal(uintptr_t pc, unsigned long address,
90 int is_write, sigset_t *old_set,
96 #ifndef CONFIG_TCG_PASS_AREG0
98 env = cpu_single_env; /* XXX: find a correct solution for multithread */
101 #if defined(DEBUG_SIGNAL)
102 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
103 pc, address, is_write, *(unsigned long *)old_set);
105 /* XXX: locking issue */
106 if (is_write && h2g_valid(address)
107 && page_unprotect(h2g(address), pc, puc)) {
111 /* see if it is an MMU fault */
112 ret = cpu_handle_mmu_fault(cpu_single_env, address, is_write,
115 return 0; /* not an MMU fault */
118 return 1; /* the MMU fault was handled without causing real CPU fault */
120 /* now we have a real cpu fault */
123 /* the PC is inside the translated code. It means that we have
124 a virtual CPU fault */
125 cpu_restore_state(tb, cpu_single_env, pc);
128 /* we restore the process signal mask as the sigreturn should
129 do it (XXX: use sigsetjmp) */
130 sigprocmask(SIG_SETMASK, old_set, NULL);
131 exception_action(cpu_single_env);
133 /* never comes here */
137 #if defined(__i386__)
139 #if defined(__APPLE__)
140 #include <sys/ucontext.h>
142 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext->ss.eip))
143 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
144 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
145 #define MASK_sig(context) ((context)->uc_sigmask)
146 #elif defined(__NetBSD__)
147 #include <ucontext.h>
149 #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
150 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
151 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
152 #define MASK_sig(context) ((context)->uc_sigmask)
153 #elif defined(__FreeBSD__) || defined(__DragonFly__)
154 #include <ucontext.h>
156 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
157 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
158 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
159 #define MASK_sig(context) ((context)->uc_sigmask)
160 #elif defined(__OpenBSD__)
161 #define EIP_sig(context) ((context)->sc_eip)
162 #define TRAP_sig(context) ((context)->sc_trapno)
163 #define ERROR_sig(context) ((context)->sc_err)
164 #define MASK_sig(context) ((context)->sc_mask)
166 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
167 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
168 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
169 #define MASK_sig(context) ((context)->uc_sigmask)
172 int cpu_signal_handler(int host_signum, void *pinfo,
175 siginfo_t *info = pinfo;
176 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
177 ucontext_t *uc = puc;
178 #elif defined(__OpenBSD__)
179 struct sigcontext *uc = puc;
181 struct ucontext *uc = puc;
190 #define REG_TRAPNO TRAPNO
193 trapno = TRAP_sig(uc);
194 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
196 (ERROR_sig(uc) >> 1) & 1 : 0,
200 #elif defined(__x86_64__)
203 #define PC_sig(context) _UC_MACHINE_PC(context)
204 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
205 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
206 #define MASK_sig(context) ((context)->uc_sigmask)
207 #elif defined(__OpenBSD__)
208 #define PC_sig(context) ((context)->sc_rip)
209 #define TRAP_sig(context) ((context)->sc_trapno)
210 #define ERROR_sig(context) ((context)->sc_err)
211 #define MASK_sig(context) ((context)->sc_mask)
212 #elif defined(__FreeBSD__) || defined(__DragonFly__)
213 #include <ucontext.h>
215 #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
216 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
217 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
218 #define MASK_sig(context) ((context)->uc_sigmask)
220 #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
221 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
222 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
223 #define MASK_sig(context) ((context)->uc_sigmask)
226 int cpu_signal_handler(int host_signum, void *pinfo,
229 siginfo_t *info = pinfo;
231 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
232 ucontext_t *uc = puc;
233 #elif defined(__OpenBSD__)
234 struct sigcontext *uc = puc;
236 struct ucontext *uc = puc;
240 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
241 TRAP_sig(uc) == 0xe ?
242 (ERROR_sig(uc) >> 1) & 1 : 0,
246 #elif defined(_ARCH_PPC)
248 /***********************************************************************
249 * signal context platform-specific definitions
253 /* All Registers access - only for local access */
254 #define REG_sig(reg_name, context) \
255 ((context)->uc_mcontext.regs->reg_name)
256 /* Gpr Registers access */
257 #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
258 /* Program counter */
259 #define IAR_sig(context) REG_sig(nip, context)
260 /* Machine State Register (Supervisor) */
261 #define MSR_sig(context) REG_sig(msr, context)
263 #define CTR_sig(context) REG_sig(ctr, context)
264 /* User's integer exception register */
265 #define XER_sig(context) REG_sig(xer, context)
267 #define LR_sig(context) REG_sig(link, context)
268 /* Condition register */
269 #define CR_sig(context) REG_sig(ccr, context)
271 /* Float Registers access */
272 #define FLOAT_sig(reg_num, context) \
273 (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
274 #define FPSCR_sig(context) \
275 (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
276 /* Exception Registers access */
277 #define DAR_sig(context) REG_sig(dar, context)
278 #define DSISR_sig(context) REG_sig(dsisr, context)
279 #define TRAP_sig(context) REG_sig(trap, context)
282 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
283 #include <ucontext.h>
284 #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
285 #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
286 #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
287 #define XER_sig(context) ((context)->uc_mcontext.mc_xer)
288 #define LR_sig(context) ((context)->uc_mcontext.mc_lr)
289 #define CR_sig(context) ((context)->uc_mcontext.mc_cr)
290 /* Exception Registers access */
291 #define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
292 #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
293 #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
294 #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
297 #include <sys/ucontext.h>
298 typedef struct ucontext SIGCONTEXT;
299 /* All Registers access - only for local access */
300 #define REG_sig(reg_name, context) \
301 ((context)->uc_mcontext->ss.reg_name)
302 #define FLOATREG_sig(reg_name, context) \
303 ((context)->uc_mcontext->fs.reg_name)
304 #define EXCEPREG_sig(reg_name, context) \
305 ((context)->uc_mcontext->es.reg_name)
306 #define VECREG_sig(reg_name, context) \
307 ((context)->uc_mcontext->vs.reg_name)
308 /* Gpr Registers access */
309 #define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
310 /* Program counter */
311 #define IAR_sig(context) REG_sig(srr0, context)
312 /* Machine State Register (Supervisor) */
313 #define MSR_sig(context) REG_sig(srr1, context)
314 #define CTR_sig(context) REG_sig(ctr, context)
316 #define XER_sig(context) REG_sig(xer, context)
317 /* User's integer exception register */
318 #define LR_sig(context) REG_sig(lr, context)
319 /* Condition register */
320 #define CR_sig(context) REG_sig(cr, context)
321 /* Float Registers access */
322 #define FLOAT_sig(reg_num, context) \
323 FLOATREG_sig(fpregs[reg_num], context)
324 #define FPSCR_sig(context) \
325 ((double)FLOATREG_sig(fpscr, context))
326 /* Exception Registers access */
327 /* Fault registers for coredump */
328 #define DAR_sig(context) EXCEPREG_sig(dar, context)
329 #define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
330 /* number of powerpc exception taken */
331 #define TRAP_sig(context) EXCEPREG_sig(exception, context)
332 #endif /* __APPLE__ */
334 int cpu_signal_handler(int host_signum, void *pinfo,
337 siginfo_t *info = pinfo;
338 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
339 ucontext_t *uc = puc;
341 struct ucontext *uc = puc;
350 if (DSISR_sig(uc) & 0x00800000) {
354 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) {
358 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
359 is_write, &uc->uc_sigmask, puc);
362 #elif defined(__alpha__)
364 int cpu_signal_handler(int host_signum, void *pinfo,
367 siginfo_t *info = pinfo;
368 struct ucontext *uc = puc;
369 uint32_t *pc = uc->uc_mcontext.sc_pc;
373 /* XXX: need kernel patch to get write flag faster */
374 switch (insn >> 26) {
377 case 0x0f: /* stq_u */
384 case 0x2e: /* stl_c */
385 case 0x2f: /* stq_c */
389 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
390 is_write, &uc->uc_sigmask, puc);
392 #elif defined(__sparc__)
394 int cpu_signal_handler(int host_signum, void *pinfo,
397 siginfo_t *info = pinfo;
400 #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
401 uint32_t *regs = (uint32_t *)(info + 1);
402 void *sigmask = (regs + 20);
403 /* XXX: is there a standard glibc define ? */
404 unsigned long pc = regs[1];
407 struct sigcontext *sc = puc;
408 unsigned long pc = sc->sigc_regs.tpc;
409 void *sigmask = (void *)sc->sigc_mask;
410 #elif defined(__OpenBSD__)
411 struct sigcontext *uc = puc;
412 unsigned long pc = uc->sc_pc;
413 void *sigmask = (void *)(long)uc->sc_mask;
417 /* XXX: need kernel patch to get write flag faster */
419 insn = *(uint32_t *)pc;
420 if ((insn >> 30) == 3) {
421 switch ((insn >> 19) & 0x3f) {
423 case 0x15: /* stba */
425 case 0x16: /* stha */
429 case 0x17: /* stda */
431 case 0x1e: /* stxa */
433 case 0x34: /* stfa */
434 case 0x27: /* stdf */
435 case 0x37: /* stdfa */
436 case 0x26: /* stqf */
437 case 0x36: /* stqfa */
438 case 0x25: /* stfsr */
439 case 0x3c: /* casa */
440 case 0x3e: /* casxa */
445 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
446 is_write, sigmask, NULL);
449 #elif defined(__arm__)
451 int cpu_signal_handler(int host_signum, void *pinfo,
454 siginfo_t *info = pinfo;
455 struct ucontext *uc = puc;
459 #if (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
460 pc = uc->uc_mcontext.gregs[R15];
462 pc = uc->uc_mcontext.arm_pc;
464 /* XXX: compute is_write */
466 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
468 &uc->uc_sigmask, puc);
471 #elif defined(__mc68000)
473 int cpu_signal_handler(int host_signum, void *pinfo,
476 siginfo_t *info = pinfo;
477 struct ucontext *uc = puc;
481 pc = uc->uc_mcontext.gregs[16];
482 /* XXX: compute is_write */
484 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
486 &uc->uc_sigmask, puc);
489 #elif defined(__ia64)
492 /* This ought to be in <bits/siginfo.h>... */
493 # define __ISR_VALID 1
496 int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
498 siginfo_t *info = pinfo;
499 struct ucontext *uc = puc;
503 ip = uc->uc_mcontext.sc_ip;
504 switch (host_signum) {
510 if (info->si_code && (info->si_segvflags & __ISR_VALID)) {
511 /* ISR.W (write-access) is bit 33: */
512 is_write = (info->si_isr >> 33) & 1;
519 return handle_cpu_signal(ip, (unsigned long)info->si_addr,
521 (sigset_t *)&uc->uc_sigmask, puc);
524 #elif defined(__s390__)
526 int cpu_signal_handler(int host_signum, void *pinfo,
529 siginfo_t *info = pinfo;
530 struct ucontext *uc = puc;
535 pc = uc->uc_mcontext.psw.addr;
537 /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
538 of the normal 2 arguments. The 3rd argument contains the "int_code"
539 from the hardware which does in fact contain the is_write value.
540 The rt signal handler, as far as I can tell, does not give this value
541 at all. Not that we could get to it from here even if it were. */
542 /* ??? This is not even close to complete, since it ignores all
543 of the read-modify-write instructions. */
544 pinsn = (uint16_t *)pc;
545 switch (pinsn[0] >> 8) {
551 case 0xc4: /* RIL format insns */
552 switch (pinsn[0] & 0xf) {
554 case 0xb: /* STGRL */
555 case 0x7: /* STHRL */
559 case 0xe3: /* RXY format insns */
560 switch (pinsn[2] & 0xff) {
563 case 0x72: /* STCY */
564 case 0x70: /* STHY */
565 case 0x8e: /* STPQ */
566 case 0x3f: /* STRVH */
567 case 0x3e: /* STRV */
568 case 0x2f: /* STRVG */
573 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
574 is_write, &uc->uc_sigmask, puc);
577 #elif defined(__mips__)
579 int cpu_signal_handler(int host_signum, void *pinfo,
582 siginfo_t *info = pinfo;
583 struct ucontext *uc = puc;
584 greg_t pc = uc->uc_mcontext.pc;
587 /* XXX: compute is_write */
589 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
590 is_write, &uc->uc_sigmask, puc);
593 #elif defined(__hppa__)
595 int cpu_signal_handler(int host_signum, void *pinfo,
598 siginfo_t *info = pinfo;
599 struct ucontext *uc = puc;
600 unsigned long pc = uc->uc_mcontext.sc_iaoq[0];
601 uint32_t insn = *(uint32_t *)pc;
604 /* XXX: need kernel patch to get write flag faster. */
605 switch (insn >> 26) {
609 case 0x1b: /* STWM */
613 case 0x09: /* CSTWX, FSTWX, FSTWS */
614 case 0x0b: /* CSTDX, FSTDX, FSTDS */
615 /* Distinguish from coprocessor load ... */
616 is_write = (insn >> 9) & 1;
620 switch ((insn >> 6) & 15) {
624 case 0xe: /* STWAS */
625 case 0xc: /* STBYS */
631 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
632 is_write, &uc->uc_sigmask, puc);
637 #error host CPU specific signal handler needed