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"
22 #include "exec/exec-all.h"
24 #include "qemu/bitops.h"
25 #include "exec/cpu_ldst.h"
26 #include "translate-all.h"
38 #include <sys/ucontext.h>
41 //#define DEBUG_SIGNAL
43 /* exit the current TB from a signal handler. The host registers are
44 restored in a state compatible with the CPU emulator
46 static void cpu_exit_tb_from_sighandler(CPUState
*cpu
, sigset_t
*old_set
)
48 /* XXX: use siglongjmp ? */
49 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
50 cpu_loop_exit_noexc(cpu
);
53 /* 'pc' is the host PC at which the exception was raised. 'address' is
54 the effective address of the memory exception. 'is_write' is 1 if a
55 write caused the exception and otherwise 0'. 'old_set' is the
56 signal set which should be restored */
57 static inline int handle_cpu_signal(uintptr_t pc
, unsigned long address
,
58 int is_write
, sigset_t
*old_set
)
64 #if defined(DEBUG_SIGNAL)
65 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
66 pc
, address
, is_write
, *(unsigned long *)old_set
);
68 /* XXX: locking issue */
69 if (is_write
&& h2g_valid(address
)) {
70 switch (page_unprotect(h2g(address
), pc
)) {
72 /* Fault not caused by a page marked unwritable to protect
73 * cached translations, must be the guest binary's problem
77 /* Fault caused by protection of cached translation; TBs
78 * invalidated, so resume execution
82 /* Fault caused by protection of cached translation, and the
83 * currently executing TB was modified and must be exited
86 cpu_exit_tb_from_sighandler(current_cpu
, old_set
);
87 g_assert_not_reached();
89 g_assert_not_reached();
93 /* Convert forcefully to guest address space, invalid addresses
94 are still valid segv ones */
95 address
= h2g_nocheck(address
);
98 cc
= CPU_GET_CLASS(cpu
);
99 /* see if it is an MMU fault */
100 g_assert(cc
->handle_mmu_fault
);
101 ret
= cc
->handle_mmu_fault(cpu
, address
, is_write
, MMU_USER_IDX
);
103 return 0; /* not an MMU fault */
106 return 1; /* the MMU fault was handled without causing real CPU fault */
108 /* now we have a real cpu fault */
109 cpu_restore_state(cpu
, pc
);
111 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
114 /* never comes here */
118 #if defined(__i386__)
120 #if defined(__NetBSD__)
121 #include <ucontext.h>
123 #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
124 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
125 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
126 #define MASK_sig(context) ((context)->uc_sigmask)
127 #elif defined(__FreeBSD__) || defined(__DragonFly__)
128 #include <ucontext.h>
130 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
131 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
132 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
133 #define MASK_sig(context) ((context)->uc_sigmask)
134 #elif defined(__OpenBSD__)
135 #define EIP_sig(context) ((context)->sc_eip)
136 #define TRAP_sig(context) ((context)->sc_trapno)
137 #define ERROR_sig(context) ((context)->sc_err)
138 #define MASK_sig(context) ((context)->sc_mask)
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)
146 int cpu_signal_handler(int host_signum
, void *pinfo
,
149 siginfo_t
*info
= pinfo
;
150 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
151 ucontext_t
*uc
= puc
;
152 #elif defined(__OpenBSD__)
153 struct sigcontext
*uc
= puc
;
155 struct ucontext
*uc
= puc
;
164 #define REG_TRAPNO TRAPNO
167 trapno
= TRAP_sig(uc
);
168 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
170 (ERROR_sig(uc
) >> 1) & 1 : 0,
174 #elif defined(__x86_64__)
177 #define PC_sig(context) _UC_MACHINE_PC(context)
178 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
179 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
180 #define MASK_sig(context) ((context)->uc_sigmask)
181 #elif defined(__OpenBSD__)
182 #define PC_sig(context) ((context)->sc_rip)
183 #define TRAP_sig(context) ((context)->sc_trapno)
184 #define ERROR_sig(context) ((context)->sc_err)
185 #define MASK_sig(context) ((context)->sc_mask)
186 #elif defined(__FreeBSD__) || defined(__DragonFly__)
187 #include <ucontext.h>
189 #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
190 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
191 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
192 #define MASK_sig(context) ((context)->uc_sigmask)
194 #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
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)
200 int cpu_signal_handler(int host_signum
, void *pinfo
,
203 siginfo_t
*info
= pinfo
;
205 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
206 ucontext_t
*uc
= puc
;
207 #elif defined(__OpenBSD__)
208 struct sigcontext
*uc
= puc
;
210 struct ucontext
*uc
= puc
;
214 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
215 TRAP_sig(uc
) == 0xe ?
216 (ERROR_sig(uc
) >> 1) & 1 : 0,
220 #elif defined(_ARCH_PPC)
222 /***********************************************************************
223 * signal context platform-specific definitions
227 /* All Registers access - only for local access */
228 #define REG_sig(reg_name, context) \
229 ((context)->uc_mcontext.regs->reg_name)
230 /* Gpr Registers access */
231 #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
232 /* Program counter */
233 #define IAR_sig(context) REG_sig(nip, context)
234 /* Machine State Register (Supervisor) */
235 #define MSR_sig(context) REG_sig(msr, context)
237 #define CTR_sig(context) REG_sig(ctr, context)
238 /* User's integer exception register */
239 #define XER_sig(context) REG_sig(xer, context)
241 #define LR_sig(context) REG_sig(link, context)
242 /* Condition register */
243 #define CR_sig(context) REG_sig(ccr, context)
245 /* Float Registers access */
246 #define FLOAT_sig(reg_num, context) \
247 (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
248 #define FPSCR_sig(context) \
249 (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
250 /* Exception Registers access */
251 #define DAR_sig(context) REG_sig(dar, context)
252 #define DSISR_sig(context) REG_sig(dsisr, context)
253 #define TRAP_sig(context) REG_sig(trap, context)
256 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
257 #include <ucontext.h>
258 #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
259 #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
260 #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
261 #define XER_sig(context) ((context)->uc_mcontext.mc_xer)
262 #define LR_sig(context) ((context)->uc_mcontext.mc_lr)
263 #define CR_sig(context) ((context)->uc_mcontext.mc_cr)
264 /* Exception Registers access */
265 #define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
266 #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
267 #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
268 #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
270 int cpu_signal_handler(int host_signum
, void *pinfo
,
273 siginfo_t
*info
= pinfo
;
274 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
275 ucontext_t
*uc
= puc
;
277 struct ucontext
*uc
= puc
;
286 if (DSISR_sig(uc
) & 0x00800000) {
290 if (TRAP_sig(uc
) != 0x400 && (DSISR_sig(uc
) & 0x02000000)) {
294 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
295 is_write
, &uc
->uc_sigmask
);
298 #elif defined(__alpha__)
300 int cpu_signal_handler(int host_signum
, void *pinfo
,
303 siginfo_t
*info
= pinfo
;
304 struct ucontext
*uc
= puc
;
305 uint32_t *pc
= uc
->uc_mcontext
.sc_pc
;
309 /* XXX: need kernel patch to get write flag faster */
310 switch (insn
>> 26) {
313 case 0x0f: /* stq_u */
320 case 0x2e: /* stl_c */
321 case 0x2f: /* stq_c */
325 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
326 is_write
, &uc
->uc_sigmask
);
328 #elif defined(__sparc__)
330 int cpu_signal_handler(int host_signum
, void *pinfo
,
333 siginfo_t
*info
= pinfo
;
336 #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
337 uint32_t *regs
= (uint32_t *)(info
+ 1);
338 void *sigmask
= (regs
+ 20);
339 /* XXX: is there a standard glibc define ? */
340 unsigned long pc
= regs
[1];
343 struct sigcontext
*sc
= puc
;
344 unsigned long pc
= sc
->sigc_regs
.tpc
;
345 void *sigmask
= (void *)sc
->sigc_mask
;
346 #elif defined(__OpenBSD__)
347 struct sigcontext
*uc
= puc
;
348 unsigned long pc
= uc
->sc_pc
;
349 void *sigmask
= (void *)(long)uc
->sc_mask
;
350 #elif defined(__NetBSD__)
351 ucontext_t
*uc
= puc
;
352 unsigned long pc
= _UC_MACHINE_PC(uc
);
353 void *sigmask
= (void *)&uc
->uc_sigmask
;
357 /* XXX: need kernel patch to get write flag faster */
359 insn
= *(uint32_t *)pc
;
360 if ((insn
>> 30) == 3) {
361 switch ((insn
>> 19) & 0x3f) {
363 case 0x15: /* stba */
365 case 0x16: /* stha */
369 case 0x17: /* stda */
371 case 0x1e: /* stxa */
373 case 0x34: /* stfa */
374 case 0x27: /* stdf */
375 case 0x37: /* stdfa */
376 case 0x26: /* stqf */
377 case 0x36: /* stqfa */
378 case 0x25: /* stfsr */
379 case 0x3c: /* casa */
380 case 0x3e: /* casxa */
385 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
389 #elif defined(__arm__)
391 #if defined(__NetBSD__)
392 #include <ucontext.h>
395 int cpu_signal_handler(int host_signum
, void *pinfo
,
398 siginfo_t
*info
= pinfo
;
399 #if defined(__NetBSD__)
400 ucontext_t
*uc
= puc
;
402 struct ucontext
*uc
= puc
;
407 #if defined(__NetBSD__)
408 pc
= uc
->uc_mcontext
.__gregs
[_REG_R15
];
409 #elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
410 pc
= uc
->uc_mcontext
.gregs
[R15
];
412 pc
= uc
->uc_mcontext
.arm_pc
;
415 /* error_code is the FSR value, in which bit 11 is WnR (assuming a v6 or
416 * later processor; on v5 we will always report this as a read).
418 is_write
= extract32(uc
->uc_mcontext
.error_code
, 11, 1);
419 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
424 #elif defined(__aarch64__)
426 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
428 siginfo_t
*info
= pinfo
;
429 struct ucontext
*uc
= puc
;
430 uintptr_t pc
= uc
->uc_mcontext
.pc
;
431 uint32_t insn
= *(uint32_t *)pc
;
434 /* XXX: need kernel patch to get write flag faster. */
435 is_write
= ( (insn
& 0xbfff0000) == 0x0c000000 /* C3.3.1 */
436 || (insn
& 0xbfe00000) == 0x0c800000 /* C3.3.2 */
437 || (insn
& 0xbfdf0000) == 0x0d000000 /* C3.3.3 */
438 || (insn
& 0xbfc00000) == 0x0d800000 /* C3.3.4 */
439 || (insn
& 0x3f400000) == 0x08000000 /* C3.3.6 */
440 || (insn
& 0x3bc00000) == 0x39000000 /* C3.3.13 */
441 || (insn
& 0x3fc00000) == 0x3d800000 /* ... 128bit */
442 /* Ingore bits 10, 11 & 21, controlling indexing. */
443 || (insn
& 0x3bc00000) == 0x38000000 /* C3.3.8-12 */
444 || (insn
& 0x3fe00000) == 0x3c800000 /* ... 128bit */
445 /* Ignore bits 23 & 24, controlling indexing. */
446 || (insn
& 0x3a400000) == 0x28000000); /* C3.3.7,14-16 */
448 return handle_cpu_signal(pc
, (uintptr_t)info
->si_addr
,
449 is_write
, &uc
->uc_sigmask
);
452 #elif defined(__ia64)
455 /* This ought to be in <bits/siginfo.h>... */
456 # define __ISR_VALID 1
459 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
461 siginfo_t
*info
= pinfo
;
462 struct ucontext
*uc
= puc
;
466 ip
= uc
->uc_mcontext
.sc_ip
;
467 switch (host_signum
) {
473 if (info
->si_code
&& (info
->si_segvflags
& __ISR_VALID
)) {
474 /* ISR.W (write-access) is bit 33: */
475 is_write
= (info
->si_isr
>> 33) & 1;
482 return handle_cpu_signal(ip
, (unsigned long)info
->si_addr
,
484 (sigset_t
*)&uc
->uc_sigmask
);
487 #elif defined(__s390__)
489 int cpu_signal_handler(int host_signum
, void *pinfo
,
492 siginfo_t
*info
= pinfo
;
493 struct ucontext
*uc
= puc
;
498 pc
= uc
->uc_mcontext
.psw
.addr
;
500 /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
501 of the normal 2 arguments. The 3rd argument contains the "int_code"
502 from the hardware which does in fact contain the is_write value.
503 The rt signal handler, as far as I can tell, does not give this value
504 at all. Not that we could get to it from here even if it were. */
505 /* ??? This is not even close to complete, since it ignores all
506 of the read-modify-write instructions. */
507 pinsn
= (uint16_t *)pc
;
508 switch (pinsn
[0] >> 8) {
514 case 0xc4: /* RIL format insns */
515 switch (pinsn
[0] & 0xf) {
517 case 0xb: /* STGRL */
518 case 0x7: /* STHRL */
522 case 0xe3: /* RXY format insns */
523 switch (pinsn
[2] & 0xff) {
526 case 0x72: /* STCY */
527 case 0x70: /* STHY */
528 case 0x8e: /* STPQ */
529 case 0x3f: /* STRVH */
530 case 0x3e: /* STRV */
531 case 0x2f: /* STRVG */
536 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
537 is_write
, &uc
->uc_sigmask
);
540 #elif defined(__mips__)
542 int cpu_signal_handler(int host_signum
, void *pinfo
,
545 siginfo_t
*info
= pinfo
;
546 struct ucontext
*uc
= puc
;
547 greg_t pc
= uc
->uc_mcontext
.pc
;
550 /* XXX: compute is_write */
552 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
553 is_write
, &uc
->uc_sigmask
);
558 #error host CPU specific signal handler needed