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"
27 #include "exec/helper-proto.h"
39 #include <sys/ucontext.h>
42 //#define DEBUG_SIGNAL
44 /* exit the current TB from a signal handler. The host registers are
45 restored in a state compatible with the CPU emulator
47 static void cpu_exit_tb_from_sighandler(CPUState
*cpu
, sigset_t
*old_set
)
49 /* XXX: use siglongjmp ? */
50 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
51 cpu_loop_exit_noexc(cpu
);
54 /* 'pc' is the host PC at which the exception was raised. 'address' is
55 the effective address of the memory exception. 'is_write' is 1 if a
56 write caused the exception and otherwise 0'. 'old_set' is the
57 signal set which should be restored */
58 static inline int handle_cpu_signal(uintptr_t pc
, unsigned long address
,
59 int is_write
, sigset_t
*old_set
)
61 CPUState
*cpu
= current_cpu
;
65 /* For synchronous signals we expect to be coming from the vCPU
66 * thread (so current_cpu should be valid) and either from running
67 * code or during translation which can fault as we cross pages.
69 * If neither is true then something has gone wrong and we should
70 * abort rather than try and restart the vCPU execution.
72 if (!cpu
|| !cpu
->running
) {
73 printf("qemu:%s received signal outside vCPU context @ pc=0x%"
74 PRIxPTR
"\n", __func__
, pc
);
78 #if defined(DEBUG_SIGNAL)
79 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
80 pc
, address
, is_write
, *(unsigned long *)old_set
);
82 /* XXX: locking issue */
83 if (is_write
&& h2g_valid(address
)) {
84 switch (page_unprotect(h2g(address
), pc
)) {
86 /* Fault not caused by a page marked unwritable to protect
87 * cached translations, must be the guest binary's problem
91 /* Fault caused by protection of cached translation; TBs
92 * invalidated, so resume execution
96 /* Fault caused by protection of cached translation, and the
97 * currently executing TB was modified and must be exited
100 cpu_exit_tb_from_sighandler(cpu
, old_set
);
101 g_assert_not_reached();
103 g_assert_not_reached();
107 /* Convert forcefully to guest address space, invalid addresses
108 are still valid segv ones */
109 address
= h2g_nocheck(address
);
111 cc
= CPU_GET_CLASS(cpu
);
112 /* see if it is an MMU fault */
113 g_assert(cc
->handle_mmu_fault
);
114 ret
= cc
->handle_mmu_fault(cpu
, address
, is_write
, MMU_USER_IDX
);
116 return 0; /* not an MMU fault */
119 return 1; /* the MMU fault was handled without causing real CPU fault */
122 /* Now we have a real cpu fault. Since this is the exact location of
123 * the exception, we must undo the adjustment done by cpu_restore_state
124 * for handling call return addresses. */
125 cpu_restore_state(cpu
, pc
+ GETPC_ADJ
);
127 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
130 /* never comes here */
134 #if defined(__i386__)
136 #if 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 ucontext_t
*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 ucontext_t
*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__ */
286 int cpu_signal_handler(int host_signum
, void *pinfo
,
289 siginfo_t
*info
= pinfo
;
290 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
291 ucontext_t
*uc
= puc
;
293 ucontext_t
*uc
= puc
;
302 if (DSISR_sig(uc
) & 0x00800000) {
306 if (TRAP_sig(uc
) != 0x400 && (DSISR_sig(uc
) & 0x02000000)) {
310 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
311 is_write
, &uc
->uc_sigmask
);
314 #elif defined(__alpha__)
316 int cpu_signal_handler(int host_signum
, void *pinfo
,
319 siginfo_t
*info
= pinfo
;
320 ucontext_t
*uc
= puc
;
321 uint32_t *pc
= uc
->uc_mcontext
.sc_pc
;
325 /* XXX: need kernel patch to get write flag faster */
326 switch (insn
>> 26) {
329 case 0x0f: /* stq_u */
336 case 0x2e: /* stl_c */
337 case 0x2f: /* stq_c */
341 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
342 is_write
, &uc
->uc_sigmask
);
344 #elif defined(__sparc__)
346 int cpu_signal_handler(int host_signum
, void *pinfo
,
349 siginfo_t
*info
= pinfo
;
352 #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
353 uint32_t *regs
= (uint32_t *)(info
+ 1);
354 void *sigmask
= (regs
+ 20);
355 /* XXX: is there a standard glibc define ? */
356 unsigned long pc
= regs
[1];
359 struct sigcontext
*sc
= puc
;
360 unsigned long pc
= sc
->sigc_regs
.tpc
;
361 void *sigmask
= (void *)sc
->sigc_mask
;
362 #elif defined(__OpenBSD__)
363 struct sigcontext
*uc
= puc
;
364 unsigned long pc
= uc
->sc_pc
;
365 void *sigmask
= (void *)(long)uc
->sc_mask
;
366 #elif defined(__NetBSD__)
367 ucontext_t
*uc
= puc
;
368 unsigned long pc
= _UC_MACHINE_PC(uc
);
369 void *sigmask
= (void *)&uc
->uc_sigmask
;
373 /* XXX: need kernel patch to get write flag faster */
375 insn
= *(uint32_t *)pc
;
376 if ((insn
>> 30) == 3) {
377 switch ((insn
>> 19) & 0x3f) {
379 case 0x15: /* stba */
381 case 0x16: /* stha */
385 case 0x17: /* stda */
387 case 0x1e: /* stxa */
389 case 0x34: /* stfa */
390 case 0x27: /* stdf */
391 case 0x37: /* stdfa */
392 case 0x26: /* stqf */
393 case 0x36: /* stqfa */
394 case 0x25: /* stfsr */
395 case 0x3c: /* casa */
396 case 0x3e: /* casxa */
401 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
405 #elif defined(__arm__)
407 #if defined(__NetBSD__)
408 #include <ucontext.h>
411 int cpu_signal_handler(int host_signum
, void *pinfo
,
414 siginfo_t
*info
= pinfo
;
415 #if defined(__NetBSD__)
416 ucontext_t
*uc
= puc
;
418 ucontext_t
*uc
= puc
;
423 #if defined(__NetBSD__)
424 pc
= uc
->uc_mcontext
.__gregs
[_REG_R15
];
425 #elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
426 pc
= uc
->uc_mcontext
.gregs
[R15
];
428 pc
= uc
->uc_mcontext
.arm_pc
;
431 /* error_code is the FSR value, in which bit 11 is WnR (assuming a v6 or
432 * later processor; on v5 we will always report this as a read).
434 is_write
= extract32(uc
->uc_mcontext
.error_code
, 11, 1);
435 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
440 #elif defined(__aarch64__)
442 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
444 siginfo_t
*info
= pinfo
;
445 ucontext_t
*uc
= puc
;
446 uintptr_t pc
= uc
->uc_mcontext
.pc
;
447 uint32_t insn
= *(uint32_t *)pc
;
450 /* XXX: need kernel patch to get write flag faster. */
451 is_write
= ( (insn
& 0xbfff0000) == 0x0c000000 /* C3.3.1 */
452 || (insn
& 0xbfe00000) == 0x0c800000 /* C3.3.2 */
453 || (insn
& 0xbfdf0000) == 0x0d000000 /* C3.3.3 */
454 || (insn
& 0xbfc00000) == 0x0d800000 /* C3.3.4 */
455 || (insn
& 0x3f400000) == 0x08000000 /* C3.3.6 */
456 || (insn
& 0x3bc00000) == 0x39000000 /* C3.3.13 */
457 || (insn
& 0x3fc00000) == 0x3d800000 /* ... 128bit */
458 /* Ingore bits 10, 11 & 21, controlling indexing. */
459 || (insn
& 0x3bc00000) == 0x38000000 /* C3.3.8-12 */
460 || (insn
& 0x3fe00000) == 0x3c800000 /* ... 128bit */
461 /* Ignore bits 23 & 24, controlling indexing. */
462 || (insn
& 0x3a400000) == 0x28000000); /* C3.3.7,14-16 */
464 return handle_cpu_signal(pc
, (uintptr_t)info
->si_addr
,
465 is_write
, &uc
->uc_sigmask
);
468 #elif defined(__ia64)
471 /* This ought to be in <bits/siginfo.h>... */
472 # define __ISR_VALID 1
475 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
477 siginfo_t
*info
= pinfo
;
478 ucontext_t
*uc
= puc
;
482 ip
= uc
->uc_mcontext
.sc_ip
;
483 switch (host_signum
) {
489 if (info
->si_code
&& (info
->si_segvflags
& __ISR_VALID
)) {
490 /* ISR.W (write-access) is bit 33: */
491 is_write
= (info
->si_isr
>> 33) & 1;
498 return handle_cpu_signal(ip
, (unsigned long)info
->si_addr
,
500 (sigset_t
*)&uc
->uc_sigmask
);
503 #elif defined(__s390__)
505 int cpu_signal_handler(int host_signum
, void *pinfo
,
508 siginfo_t
*info
= pinfo
;
509 ucontext_t
*uc
= puc
;
514 pc
= uc
->uc_mcontext
.psw
.addr
;
516 /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
517 of the normal 2 arguments. The 3rd argument contains the "int_code"
518 from the hardware which does in fact contain the is_write value.
519 The rt signal handler, as far as I can tell, does not give this value
520 at all. Not that we could get to it from here even if it were. */
521 /* ??? This is not even close to complete, since it ignores all
522 of the read-modify-write instructions. */
523 pinsn
= (uint16_t *)pc
;
524 switch (pinsn
[0] >> 8) {
530 case 0xc4: /* RIL format insns */
531 switch (pinsn
[0] & 0xf) {
533 case 0xb: /* STGRL */
534 case 0x7: /* STHRL */
538 case 0xe3: /* RXY format insns */
539 switch (pinsn
[2] & 0xff) {
542 case 0x72: /* STCY */
543 case 0x70: /* STHY */
544 case 0x8e: /* STPQ */
545 case 0x3f: /* STRVH */
546 case 0x3e: /* STRV */
547 case 0x2f: /* STRVG */
552 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
553 is_write
, &uc
->uc_sigmask
);
556 #elif defined(__mips__)
558 int cpu_signal_handler(int host_signum
, void *pinfo
,
561 siginfo_t
*info
= pinfo
;
562 ucontext_t
*uc
= puc
;
563 greg_t pc
= uc
->uc_mcontext
.pc
;
566 /* XXX: compute is_write */
568 return handle_cpu_signal(pc
, (unsigned long)info
->si_addr
,
569 is_write
, &uc
->uc_sigmask
);
574 #error host CPU specific signal handler needed
578 /* The softmmu versions of these helpers are in cputlb.c. */
580 /* Do not allow unaligned operations to proceed. Return the host address. */
581 static void *atomic_mmu_lookup(CPUArchState
*env
, target_ulong addr
,
582 int size
, uintptr_t retaddr
)
584 /* Enforce qemu required alignment. */
585 if (unlikely(addr
& (size
- 1))) {
586 cpu_loop_exit_atomic(ENV_GET_CPU(env
), retaddr
);
591 /* Macro to call the above, with local variables from the use context. */
592 #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, DATA_SIZE, GETPC())
594 #define ATOMIC_NAME(X) HELPER(glue(glue(atomic_ ## X, SUFFIX), END))
598 #include "atomic_template.h"
601 #include "atomic_template.h"
604 #include "atomic_template.h"
606 #ifdef CONFIG_ATOMIC64
608 #include "atomic_template.h"
611 /* The following is only callable from other helpers, and matches up
612 with the softmmu version. */
614 #ifdef CONFIG_ATOMIC128
618 #undef ATOMIC_MMU_LOOKUP
620 #define EXTRA_ARGS , TCGMemOpIdx oi, uintptr_t retaddr
621 #define ATOMIC_NAME(X) \
622 HELPER(glue(glue(glue(atomic_ ## X, SUFFIX), END), _mmu))
623 #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, DATA_SIZE, retaddr)
626 #include "atomic_template.h"
627 #endif /* CONFIG_ATOMIC128 */