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/>.
35 #include <sys/ucontext.h>
38 //#define DEBUG_SIGNAL
40 #if defined(TARGET_I386)
41 #define EXCEPTION_ACTION \
42 raise_exception_err(env->exception_index, env->error_code)
44 #define EXCEPTION_ACTION \
48 /* exit the current TB from a signal handler. The host registers are
49 restored in a state compatible with the CPU emulator
51 void cpu_resume_from_signal(CPUState
*env1
, void *puc
)
54 struct ucontext
*uc
= puc
;
55 #elif defined(__OpenBSD__)
56 struct sigcontext
*uc
= puc
;
61 /* XXX: restore cpu registers saved in host registers */
64 /* XXX: use siglongjmp ? */
67 sigprocmask(SIG_SETMASK
, (sigset_t
*)&uc
->uc_sigmask
, NULL
);
69 sigprocmask(SIG_SETMASK
, &uc
->uc_sigmask
, NULL
);
71 #elif defined(__OpenBSD__)
72 sigprocmask(SIG_SETMASK
, &uc
->sc_mask
, NULL
);
75 env
->exception_index
= -1;
76 longjmp(env
->jmp_env
, 1);
79 /* 'pc' is the host PC at which the exception was raised. 'address' is
80 the effective address of the memory exception. 'is_write' is 1 if a
81 write caused the exception and otherwise 0'. 'old_set' is the
82 signal set which should be restored */
83 static inline int handle_cpu_signal(unsigned long pc
, unsigned long address
,
84 int is_write
, sigset_t
*old_set
,
91 env
= cpu_single_env
; /* XXX: find a correct solution for multithread */
93 #if defined(DEBUG_SIGNAL)
94 qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
95 pc
, address
, is_write
, *(unsigned long *)old_set
);
97 /* XXX: locking issue */
98 if (is_write
&& page_unprotect(h2g(address
), pc
, puc
)) {
102 /* see if it is an MMU fault */
103 ret
= cpu_handle_mmu_fault(env
, address
, is_write
, MMU_USER_IDX
, 0);
105 return 0; /* not an MMU fault */
108 return 1; /* the MMU fault was handled without causing real CPU fault */
110 /* now we have a real cpu fault */
113 /* the PC is inside the translated code. It means that we have
114 a virtual CPU fault */
115 cpu_restore_state(tb
, env
, pc
);
118 /* we restore the process signal mask as the sigreturn should
119 do it (XXX: use sigsetjmp) */
120 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
123 /* never comes here */
127 #if defined(__i386__)
129 #if defined(__APPLE__)
130 #include <sys/ucontext.h>
132 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext->ss.eip))
133 #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
134 #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
135 #define MASK_sig(context) ((context)->uc_sigmask)
136 #elif defined(__NetBSD__)
137 #include <ucontext.h>
139 #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
140 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
141 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
142 #define MASK_sig(context) ((context)->uc_sigmask)
143 #elif defined(__FreeBSD__) || defined(__DragonFly__)
144 #include <ucontext.h>
146 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
147 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
148 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
149 #define MASK_sig(context) ((context)->uc_sigmask)
150 #elif defined(__OpenBSD__)
151 #define EIP_sig(context) ((context)->sc_eip)
152 #define TRAP_sig(context) ((context)->sc_trapno)
153 #define ERROR_sig(context) ((context)->sc_err)
154 #define MASK_sig(context) ((context)->sc_mask)
156 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
157 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
158 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
159 #define MASK_sig(context) ((context)->uc_sigmask)
162 int cpu_signal_handler(int host_signum
, void *pinfo
,
165 siginfo_t
*info
= pinfo
;
166 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
167 ucontext_t
*uc
= puc
;
168 #elif defined(__OpenBSD__)
169 struct sigcontext
*uc
= puc
;
171 struct ucontext
*uc
= puc
;
180 #define REG_TRAPNO TRAPNO
183 trapno
= TRAP_sig(uc
);
184 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
186 (ERROR_sig(uc
) >> 1) & 1 : 0,
190 #elif defined(__x86_64__)
193 #define PC_sig(context) _UC_MACHINE_PC(context)
194 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
195 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
196 #define MASK_sig(context) ((context)->uc_sigmask)
197 #elif defined(__OpenBSD__)
198 #define PC_sig(context) ((context)->sc_rip)
199 #define TRAP_sig(context) ((context)->sc_trapno)
200 #define ERROR_sig(context) ((context)->sc_err)
201 #define MASK_sig(context) ((context)->sc_mask)
202 #elif defined(__FreeBSD__) || defined(__DragonFly__)
203 #include <ucontext.h>
205 #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
206 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
207 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
208 #define MASK_sig(context) ((context)->uc_sigmask)
210 #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
211 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
212 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
213 #define MASK_sig(context) ((context)->uc_sigmask)
216 int cpu_signal_handler(int host_signum
, void *pinfo
,
219 siginfo_t
*info
= pinfo
;
221 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
222 ucontext_t
*uc
= puc
;
223 #elif defined(__OpenBSD__)
224 struct sigcontext
*uc
= puc
;
226 struct ucontext
*uc
= puc
;
230 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
231 TRAP_sig(uc
) == 0xe ?
232 (ERROR_sig(uc
) >> 1) & 1 : 0,
236 #elif defined(_ARCH_PPC)
238 /***********************************************************************
239 * signal context platform-specific definitions
243 /* All Registers access - only for local access */
244 #define REG_sig(reg_name, context) \
245 ((context)->uc_mcontext.regs->reg_name)
246 /* Gpr Registers access */
247 #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
248 /* Program counter */
249 #define IAR_sig(context) REG_sig(nip, context)
250 /* Machine State Register (Supervisor) */
251 #define MSR_sig(context) REG_sig(msr, context)
253 #define CTR_sig(context) REG_sig(ctr, context)
254 /* User's integer exception register */
255 #define XER_sig(context) REG_sig(xer, context)
257 #define LR_sig(context) REG_sig(link, context)
258 /* Condition register */
259 #define CR_sig(context) REG_sig(ccr, context)
261 /* Float Registers access */
262 #define FLOAT_sig(reg_num, context) \
263 (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
264 #define FPSCR_sig(context) \
265 (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
266 /* Exception Registers access */
267 #define DAR_sig(context) REG_sig(dar, context)
268 #define DSISR_sig(context) REG_sig(dsisr, context)
269 #define TRAP_sig(context) REG_sig(trap, context)
272 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
273 #include <ucontext.h>
274 #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
275 #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
276 #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
277 #define XER_sig(context) ((context)->uc_mcontext.mc_xer)
278 #define LR_sig(context) ((context)->uc_mcontext.mc_lr)
279 #define CR_sig(context) ((context)->uc_mcontext.mc_cr)
280 /* Exception Registers access */
281 #define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
282 #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
283 #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
284 #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
287 #include <sys/ucontext.h>
288 typedef struct ucontext SIGCONTEXT
;
289 /* All Registers access - only for local access */
290 #define REG_sig(reg_name, context) \
291 ((context)->uc_mcontext->ss.reg_name)
292 #define FLOATREG_sig(reg_name, context) \
293 ((context)->uc_mcontext->fs.reg_name)
294 #define EXCEPREG_sig(reg_name, context) \
295 ((context)->uc_mcontext->es.reg_name)
296 #define VECREG_sig(reg_name, context) \
297 ((context)->uc_mcontext->vs.reg_name)
298 /* Gpr Registers access */
299 #define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
300 /* Program counter */
301 #define IAR_sig(context) REG_sig(srr0, context)
302 /* Machine State Register (Supervisor) */
303 #define MSR_sig(context) REG_sig(srr1, context)
304 #define CTR_sig(context) REG_sig(ctr, context)
306 #define XER_sig(context) REG_sig(xer, context)
307 /* User's integer exception register */
308 #define LR_sig(context) REG_sig(lr, context)
309 /* Condition register */
310 #define CR_sig(context) REG_sig(cr, context)
311 /* Float Registers access */
312 #define FLOAT_sig(reg_num, context) \
313 FLOATREG_sig(fpregs[reg_num], context)
314 #define FPSCR_sig(context) \
315 ((double)FLOATREG_sig(fpscr, context))
316 /* Exception Registers access */
317 /* Fault registers for coredump */
318 #define DAR_sig(context) EXCEPREG_sig(dar, context)
319 #define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
320 /* number of powerpc exception taken */
321 #define TRAP_sig(context) EXCEPREG_sig(exception, context)
322 #endif /* __APPLE__ */
324 int cpu_signal_handler(int host_signum
, void *pinfo
,
327 siginfo_t
*info
= pinfo
;
328 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
329 ucontext_t
*uc
= puc
;
331 struct ucontext
*uc
= puc
;
340 if (DSISR_sig(uc
) & 0x00800000) {
344 if (TRAP_sig(uc
) != 0x400 && (DSISR_sig(uc
) & 0x02000000)) {
348 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
349 is_write
, &uc
->uc_sigmask
, puc
);
352 #elif defined(__alpha__)
354 int cpu_signal_handler(int host_signum
, void *pinfo
,
357 siginfo_t
*info
= pinfo
;
358 struct ucontext
*uc
= puc
;
359 uint32_t *pc
= uc
->uc_mcontext
.sc_pc
;
363 /* XXX: need kernel patch to get write flag faster */
364 switch (insn
>> 26) {
367 case 0x0f: /* stq_u */
374 case 0x2e: /* stl_c */
375 case 0x2f: /* stq_c */
379 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
380 is_write
, &uc
->uc_sigmask
, puc
);
382 #elif defined(__sparc__)
384 int cpu_signal_handler(int host_signum
, void *pinfo
,
387 siginfo_t
*info
= pinfo
;
390 #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
391 uint32_t *regs
= (uint32_t *)(info
+ 1);
392 void *sigmask
= (regs
+ 20);
393 /* XXX: is there a standard glibc define ? */
394 unsigned long pc
= regs
[1];
397 struct sigcontext
*sc
= puc
;
398 unsigned long pc
= sc
->sigc_regs
.tpc
;
399 void *sigmask
= (void *)sc
->sigc_mask
;
400 #elif defined(__OpenBSD__)
401 struct sigcontext
*uc
= puc
;
402 unsigned long pc
= uc
->sc_pc
;
403 void *sigmask
= (void *)(long)uc
->sc_mask
;
407 /* XXX: need kernel patch to get write flag faster */
409 insn
= *(uint32_t *)pc
;
410 if ((insn
>> 30) == 3) {
411 switch ((insn
>> 19) & 0x3f) {
413 case 0x15: /* stba */
415 case 0x16: /* stha */
419 case 0x17: /* stda */
421 case 0x1e: /* stxa */
423 case 0x34: /* stfa */
424 case 0x27: /* stdf */
425 case 0x37: /* stdfa */
426 case 0x26: /* stqf */
427 case 0x36: /* stqfa */
428 case 0x25: /* stfsr */
429 case 0x3c: /* casa */
430 case 0x3e: /* casxa */
435 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
436 is_write
, sigmask
, NULL
);
439 #elif defined(__arm__)
441 int cpu_signal_handler(int host_signum
, void *pinfo
,
444 siginfo_t
*info
= pinfo
;
445 struct ucontext
*uc
= puc
;
449 #if (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
450 pc
= uc
->uc_mcontext
.gregs
[R15
];
452 pc
= uc
->uc_mcontext
.arm_pc
;
454 /* XXX: compute is_write */
456 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
458 &uc
->uc_sigmask
, puc
);
461 #elif defined(__mc68000)
463 int cpu_signal_handler(int host_signum
, void *pinfo
,
466 siginfo_t
*info
= pinfo
;
467 struct ucontext
*uc
= puc
;
471 pc
= uc
->uc_mcontext
.gregs
[16];
472 /* XXX: compute is_write */
474 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
476 &uc
->uc_sigmask
, puc
);
479 #elif defined(__ia64)
482 /* This ought to be in <bits/siginfo.h>... */
483 # define __ISR_VALID 1
486 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
488 siginfo_t
*info
= pinfo
;
489 struct ucontext
*uc
= puc
;
493 ip
= uc
->uc_mcontext
.sc_ip
;
494 switch (host_signum
) {
500 if (info
->si_code
&& (info
->si_segvflags
& __ISR_VALID
)) {
501 /* ISR.W (write-access) is bit 33: */
502 is_write
= (info
->si_isr
>> 33) & 1;
509 return handle_cpu_signal(ip
, (unsigned long)info
->si_addr
,
511 (sigset_t
*)&uc
->uc_sigmask
, puc
);
514 #elif defined(__s390__)
516 int cpu_signal_handler(int host_signum
, void *pinfo
,
519 siginfo_t
*info
= pinfo
;
520 struct ucontext
*uc
= puc
;
525 pc
= uc
->uc_mcontext
.psw
.addr
;
527 /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
528 of the normal 2 arguments. The 3rd argument contains the "int_code"
529 from the hardware which does in fact contain the is_write value.
530 The rt signal handler, as far as I can tell, does not give this value
531 at all. Not that we could get to it from here even if it were. */
532 /* ??? This is not even close to complete, since it ignores all
533 of the read-modify-write instructions. */
534 pinsn
= (uint16_t *)pc
;
535 switch (pinsn
[0] >> 8) {
541 case 0xc4: /* RIL format insns */
542 switch (pinsn
[0] & 0xf) {
544 case 0xb: /* STGRL */
545 case 0x7: /* STHRL */
549 case 0xe3: /* RXY format insns */
550 switch (pinsn
[2] & 0xff) {
553 case 0x72: /* STCY */
554 case 0x70: /* STHY */
555 case 0x8e: /* STPQ */
556 case 0x3f: /* STRVH */
557 case 0x3e: /* STRV */
558 case 0x2f: /* STRVG */
563 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
564 is_write
, &uc
->uc_sigmask
, puc
);
567 #elif defined(__mips__)
569 int cpu_signal_handler(int host_signum
, void *pinfo
,
572 siginfo_t
*info
= pinfo
;
573 struct ucontext
*uc
= puc
;
574 greg_t pc
= uc
->uc_mcontext
.pc
;
577 /* XXX: compute is_write */
579 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
580 is_write
, &uc
->uc_sigmask
, puc
);
583 #elif defined(__hppa__)
585 int cpu_signal_handler(int host_signum
, void *pinfo
,
588 struct siginfo
*info
= pinfo
;
589 struct ucontext
*uc
= puc
;
590 unsigned long pc
= uc
->uc_mcontext
.sc_iaoq
[0];
591 uint32_t insn
= *(uint32_t *)pc
;
594 /* XXX: need kernel patch to get write flag faster. */
595 switch (insn
>> 26) {
599 case 0x1b: /* STWM */
603 case 0x09: /* CSTWX, FSTWX, FSTWS */
604 case 0x0b: /* CSTDX, FSTDX, FSTDS */
605 /* Distinguish from coprocessor load ... */
606 is_write
= (insn
>> 9) & 1;
610 switch ((insn
>> 6) & 15) {
614 case 0xe: /* STWAS */
615 case 0xc: /* STBYS */
621 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
622 is_write
, &uc
->uc_sigmask
, puc
);
627 #error host CPU specific signal handler needed
631 #if defined(TARGET_I386)
633 void cpu_x86_load_seg(CPUX86State
*s
, int seg_reg
, int selector
)
635 CPUX86State
*saved_env
;
639 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
)) {
641 cpu_x86_load_seg_cache(env
, seg_reg
, selector
,
642 (selector
<< 4), 0xffff, 0);
644 helper_load_seg(seg_reg
, selector
);
649 void cpu_x86_fsave(CPUX86State
*s
, target_ulong ptr
, int data32
)
651 CPUX86State
*saved_env
;
656 helper_fsave(ptr
, data32
);
661 void cpu_x86_frstor(CPUX86State
*s
, target_ulong ptr
, int data32
)
663 CPUX86State
*saved_env
;
668 helper_frstor(ptr
, data32
);
673 #endif /* TARGET_I386 */