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
)
60 CPUState
*cpu
= current_cpu
;
64 /* For synchronous signals we expect to be coming from the vCPU
65 * thread (so current_cpu should be valid) and either from running
66 * code or during translation which can fault as we cross pages.
68 * If neither is true then something has gone wrong and we should
69 * abort rather than try and restart the vCPU execution.
71 if (!cpu
|| !cpu
->running
) {
72 printf("qemu:%s received signal outside vCPU context @ pc=0x%"
73 PRIxPTR
"\n", __func__
, pc
);
77 #if defined(DEBUG_SIGNAL)
78 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
79 pc
, address
, is_write
, *(unsigned long *)old_set
);
81 /* XXX: locking issue */
82 if (is_write
&& h2g_valid(address
)) {
83 switch (page_unprotect(h2g(address
), pc
)) {
85 /* Fault not caused by a page marked unwritable to protect
86 * cached translations, must be the guest binary's problem
90 /* Fault caused by protection of cached translation; TBs
91 * invalidated, so resume execution
95 /* Fault caused by protection of cached translation, and the
96 * currently executing TB was modified and must be exited
99 cpu_exit_tb_from_sighandler(cpu
, old_set
);
100 g_assert_not_reached();
102 g_assert_not_reached();
106 /* Convert forcefully to guest address space, invalid addresses
107 are still valid segv ones */
108 address
= h2g_nocheck(address
);
110 cc
= CPU_GET_CLASS(cpu
);
111 /* see if it is an MMU fault */
112 g_assert(cc
->handle_mmu_fault
);
113 ret
= cc
->handle_mmu_fault(cpu
, address
, is_write
, MMU_USER_IDX
);
115 return 0; /* not an MMU fault */
118 return 1; /* the MMU fault was handled without causing real CPU fault */
121 /* Now we have a real cpu fault. Since this is the exact location of
122 * the exception, we must undo the adjustment done by cpu_restore_state
123 * for handling call return addresses. */
124 cpu_restore_state(cpu
, pc
+ GETPC_ADJ
);
126 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
129 /* never comes here */
133 #if defined(__i386__)
135 #if defined(__NetBSD__)
136 #include <ucontext.h>
138 #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
139 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
140 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
141 #define MASK_sig(context) ((context)->uc_sigmask)
142 #elif defined(__FreeBSD__) || defined(__DragonFly__)
143 #include <ucontext.h>
145 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
146 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
147 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
148 #define MASK_sig(context) ((context)->uc_sigmask)
149 #elif defined(__OpenBSD__)
150 #define EIP_sig(context) ((context)->sc_eip)
151 #define TRAP_sig(context) ((context)->sc_trapno)
152 #define ERROR_sig(context) ((context)->sc_err)
153 #define MASK_sig(context) ((context)->sc_mask)
155 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
156 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
157 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
158 #define MASK_sig(context) ((context)->uc_sigmask)
161 int cpu_signal_handler(int host_signum
, void *pinfo
,
164 siginfo_t
*info
= pinfo
;
165 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
166 ucontext_t
*uc
= puc
;
167 #elif defined(__OpenBSD__)
168 struct sigcontext
*uc
= puc
;
170 struct ucontext
*uc
= puc
;
179 #define REG_TRAPNO TRAPNO
182 trapno
= TRAP_sig(uc
);
183 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
185 (ERROR_sig(uc
) >> 1) & 1 : 0,
189 #elif defined(__x86_64__)
192 #define PC_sig(context) _UC_MACHINE_PC(context)
193 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
194 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
195 #define MASK_sig(context) ((context)->uc_sigmask)
196 #elif defined(__OpenBSD__)
197 #define PC_sig(context) ((context)->sc_rip)
198 #define TRAP_sig(context) ((context)->sc_trapno)
199 #define ERROR_sig(context) ((context)->sc_err)
200 #define MASK_sig(context) ((context)->sc_mask)
201 #elif defined(__FreeBSD__) || defined(__DragonFly__)
202 #include <ucontext.h>
204 #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
205 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
206 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
207 #define MASK_sig(context) ((context)->uc_sigmask)
209 #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
210 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
211 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
212 #define MASK_sig(context) ((context)->uc_sigmask)
215 int cpu_signal_handler(int host_signum
, void *pinfo
,
218 siginfo_t
*info
= pinfo
;
220 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
221 ucontext_t
*uc
= puc
;
222 #elif defined(__OpenBSD__)
223 struct sigcontext
*uc
= puc
;
225 struct ucontext
*uc
= puc
;
229 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
230 TRAP_sig(uc
) == 0xe ?
231 (ERROR_sig(uc
) >> 1) & 1 : 0,
235 #elif defined(_ARCH_PPC)
237 /***********************************************************************
238 * signal context platform-specific definitions
242 /* All Registers access - only for local access */
243 #define REG_sig(reg_name, context) \
244 ((context)->uc_mcontext.regs->reg_name)
245 /* Gpr Registers access */
246 #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
247 /* Program counter */
248 #define IAR_sig(context) REG_sig(nip, context)
249 /* Machine State Register (Supervisor) */
250 #define MSR_sig(context) REG_sig(msr, context)
252 #define CTR_sig(context) REG_sig(ctr, context)
253 /* User's integer exception register */
254 #define XER_sig(context) REG_sig(xer, context)
256 #define LR_sig(context) REG_sig(link, context)
257 /* Condition register */
258 #define CR_sig(context) REG_sig(ccr, context)
260 /* Float Registers access */
261 #define FLOAT_sig(reg_num, context) \
262 (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
263 #define FPSCR_sig(context) \
264 (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
265 /* Exception Registers access */
266 #define DAR_sig(context) REG_sig(dar, context)
267 #define DSISR_sig(context) REG_sig(dsisr, context)
268 #define TRAP_sig(context) REG_sig(trap, context)
271 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
272 #include <ucontext.h>
273 #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
274 #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
275 #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
276 #define XER_sig(context) ((context)->uc_mcontext.mc_xer)
277 #define LR_sig(context) ((context)->uc_mcontext.mc_lr)
278 #define CR_sig(context) ((context)->uc_mcontext.mc_cr)
279 /* Exception Registers access */
280 #define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
281 #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
282 #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
283 #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
285 int cpu_signal_handler(int host_signum
, void *pinfo
,
288 siginfo_t
*info
= pinfo
;
289 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
290 ucontext_t
*uc
= puc
;
292 struct ucontext
*uc
= puc
;
301 if (DSISR_sig(uc
) & 0x00800000) {
305 if (TRAP_sig(uc
) != 0x400 && (DSISR_sig(uc
) & 0x02000000)) {
309 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
310 is_write
, &uc
->uc_sigmask
);
313 #elif defined(__alpha__)
315 int cpu_signal_handler(int host_signum
, void *pinfo
,
318 siginfo_t
*info
= pinfo
;
319 struct ucontext
*uc
= puc
;
320 uint32_t *pc
= uc
->uc_mcontext
.sc_pc
;
324 /* XXX: need kernel patch to get write flag faster */
325 switch (insn
>> 26) {
328 case 0x0f: /* stq_u */
335 case 0x2e: /* stl_c */
336 case 0x2f: /* stq_c */
340 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
341 is_write
, &uc
->uc_sigmask
);
343 #elif defined(__sparc__)
345 int cpu_signal_handler(int host_signum
, void *pinfo
,
348 siginfo_t
*info
= pinfo
;
351 #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
352 uint32_t *regs
= (uint32_t *)(info
+ 1);
353 void *sigmask
= (regs
+ 20);
354 /* XXX: is there a standard glibc define ? */
355 unsigned long pc
= regs
[1];
358 struct sigcontext
*sc
= puc
;
359 unsigned long pc
= sc
->sigc_regs
.tpc
;
360 void *sigmask
= (void *)sc
->sigc_mask
;
361 #elif defined(__OpenBSD__)
362 struct sigcontext
*uc
= puc
;
363 unsigned long pc
= uc
->sc_pc
;
364 void *sigmask
= (void *)(long)uc
->sc_mask
;
365 #elif defined(__NetBSD__)
366 ucontext_t
*uc
= puc
;
367 unsigned long pc
= _UC_MACHINE_PC(uc
);
368 void *sigmask
= (void *)&uc
->uc_sigmask
;
372 /* XXX: need kernel patch to get write flag faster */
374 insn
= *(uint32_t *)pc
;
375 if ((insn
>> 30) == 3) {
376 switch ((insn
>> 19) & 0x3f) {
378 case 0x15: /* stba */
380 case 0x16: /* stha */
384 case 0x17: /* stda */
386 case 0x1e: /* stxa */
388 case 0x34: /* stfa */
389 case 0x27: /* stdf */
390 case 0x37: /* stdfa */
391 case 0x26: /* stqf */
392 case 0x36: /* stqfa */
393 case 0x25: /* stfsr */
394 case 0x3c: /* casa */
395 case 0x3e: /* casxa */
400 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
404 #elif defined(__arm__)
406 #if defined(__NetBSD__)
407 #include <ucontext.h>
410 int cpu_signal_handler(int host_signum
, void *pinfo
,
413 siginfo_t
*info
= pinfo
;
414 #if defined(__NetBSD__)
415 ucontext_t
*uc
= puc
;
417 struct ucontext
*uc
= puc
;
422 #if defined(__NetBSD__)
423 pc
= uc
->uc_mcontext
.__gregs
[_REG_R15
];
424 #elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
425 pc
= uc
->uc_mcontext
.gregs
[R15
];
427 pc
= uc
->uc_mcontext
.arm_pc
;
430 /* error_code is the FSR value, in which bit 11 is WnR (assuming a v6 or
431 * later processor; on v5 we will always report this as a read).
433 is_write
= extract32(uc
->uc_mcontext
.error_code
, 11, 1);
434 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
439 #elif defined(__aarch64__)
441 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
443 siginfo_t
*info
= pinfo
;
444 struct ucontext
*uc
= puc
;
445 uintptr_t pc
= uc
->uc_mcontext
.pc
;
446 uint32_t insn
= *(uint32_t *)pc
;
449 /* XXX: need kernel patch to get write flag faster. */
450 is_write
= ( (insn
& 0xbfff0000) == 0x0c000000 /* C3.3.1 */
451 || (insn
& 0xbfe00000) == 0x0c800000 /* C3.3.2 */
452 || (insn
& 0xbfdf0000) == 0x0d000000 /* C3.3.3 */
453 || (insn
& 0xbfc00000) == 0x0d800000 /* C3.3.4 */
454 || (insn
& 0x3f400000) == 0x08000000 /* C3.3.6 */
455 || (insn
& 0x3bc00000) == 0x39000000 /* C3.3.13 */
456 || (insn
& 0x3fc00000) == 0x3d800000 /* ... 128bit */
457 /* Ingore bits 10, 11 & 21, controlling indexing. */
458 || (insn
& 0x3bc00000) == 0x38000000 /* C3.3.8-12 */
459 || (insn
& 0x3fe00000) == 0x3c800000 /* ... 128bit */
460 /* Ignore bits 23 & 24, controlling indexing. */
461 || (insn
& 0x3a400000) == 0x28000000); /* C3.3.7,14-16 */
463 return handle_cpu_signal(pc
, (uintptr_t)info
->si_addr
,
464 is_write
, &uc
->uc_sigmask
);
467 #elif defined(__ia64)
470 /* This ought to be in <bits/siginfo.h>... */
471 # define __ISR_VALID 1
474 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
476 siginfo_t
*info
= pinfo
;
477 struct ucontext
*uc
= puc
;
481 ip
= uc
->uc_mcontext
.sc_ip
;
482 switch (host_signum
) {
488 if (info
->si_code
&& (info
->si_segvflags
& __ISR_VALID
)) {
489 /* ISR.W (write-access) is bit 33: */
490 is_write
= (info
->si_isr
>> 33) & 1;
497 return handle_cpu_signal(ip
, (unsigned long)info
->si_addr
,
499 (sigset_t
*)&uc
->uc_sigmask
);
502 #elif defined(__s390__)
504 int cpu_signal_handler(int host_signum
, void *pinfo
,
507 siginfo_t
*info
= pinfo
;
508 struct ucontext
*uc
= puc
;
513 pc
= uc
->uc_mcontext
.psw
.addr
;
515 /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
516 of the normal 2 arguments. The 3rd argument contains the "int_code"
517 from the hardware which does in fact contain the is_write value.
518 The rt signal handler, as far as I can tell, does not give this value
519 at all. Not that we could get to it from here even if it were. */
520 /* ??? This is not even close to complete, since it ignores all
521 of the read-modify-write instructions. */
522 pinsn
= (uint16_t *)pc
;
523 switch (pinsn
[0] >> 8) {
529 case 0xc4: /* RIL format insns */
530 switch (pinsn
[0] & 0xf) {
532 case 0xb: /* STGRL */
533 case 0x7: /* STHRL */
537 case 0xe3: /* RXY format insns */
538 switch (pinsn
[2] & 0xff) {
541 case 0x72: /* STCY */
542 case 0x70: /* STHY */
543 case 0x8e: /* STPQ */
544 case 0x3f: /* STRVH */
545 case 0x3e: /* STRV */
546 case 0x2f: /* STRVG */
551 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
552 is_write
, &uc
->uc_sigmask
);
555 #elif defined(__mips__)
557 int cpu_signal_handler(int host_signum
, void *pinfo
,
560 siginfo_t
*info
= pinfo
;
561 struct ucontext
*uc
= puc
;
562 greg_t pc
= uc
->uc_mcontext
.pc
;
565 /* XXX: compute is_write */
567 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
568 is_write
, &uc
->uc_sigmask
);
573 #error host CPU specific signal handler needed