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.1 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"
28 #include "qemu/atomic128.h"
40 #include <sys/ucontext.h>
43 __thread
uintptr_t helper_retaddr
;
45 //#define DEBUG_SIGNAL
47 /* exit the current TB from a signal handler. The host registers are
48 restored in a state compatible with the CPU emulator
50 static void cpu_exit_tb_from_sighandler(CPUState
*cpu
, sigset_t
*old_set
)
52 /* XXX: use siglongjmp ? */
53 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
54 cpu_loop_exit_noexc(cpu
);
57 /* 'pc' is the host PC at which the exception was raised. 'address' is
58 the effective address of the memory exception. 'is_write' is 1 if a
59 write caused the exception and otherwise 0'. 'old_set' is the
60 signal set which should be restored */
61 static inline int handle_cpu_signal(uintptr_t pc
, siginfo_t
*info
,
62 int is_write
, sigset_t
*old_set
)
64 CPUState
*cpu
= current_cpu
;
67 unsigned long address
= (unsigned long)info
->si_addr
;
68 MMUAccessType access_type
;
70 /* We must handle PC addresses from two different sources:
71 * a call return address and a signal frame address.
73 * Within cpu_restore_state_from_tb we assume the former and adjust
74 * the address by -GETPC_ADJ so that the address is within the call
75 * insn so that addr does not accidentally match the beginning of the
78 * However, when the PC comes from the signal frame, it points to
79 * the actual faulting host insn and not a call insn. Subtracting
80 * GETPC_ADJ in that case may accidentally match the previous guest insn.
82 * So for the later case, adjust forward to compensate for what
83 * will be done later by cpu_restore_state_from_tb.
91 /* For synchronous signals we expect to be coming from the vCPU
92 * thread (so current_cpu should be valid) and either from running
93 * code or during translation which can fault as we cross pages.
95 * If neither is true then something has gone wrong and we should
96 * abort rather than try and restart the vCPU execution.
98 if (!cpu
|| !cpu
->running
) {
99 printf("qemu:%s received signal outside vCPU context @ pc=0x%"
100 PRIxPTR
"\n", __func__
, pc
);
104 #if defined(DEBUG_SIGNAL)
105 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
106 pc
, address
, is_write
, *(unsigned long *)old_set
);
108 /* XXX: locking issue */
109 /* Note that it is important that we don't call page_unprotect() unless
110 * this is really a "write to nonwriteable page" fault, because
111 * page_unprotect() assumes that if it is called for an access to
112 * a page that's writeable this means we had two threads racing and
113 * another thread got there first and already made the page writeable;
114 * so we will retry the access. If we were to call page_unprotect()
115 * for some other kind of fault that should really be passed to the
116 * guest, we'd end up in an infinite loop of retrying the faulting
119 if (is_write
&& info
->si_signo
== SIGSEGV
&& info
->si_code
== SEGV_ACCERR
&&
120 h2g_valid(address
)) {
121 switch (page_unprotect(h2g(address
), pc
)) {
123 /* Fault not caused by a page marked unwritable to protect
124 * cached translations, must be the guest binary's problem.
128 /* Fault caused by protection of cached translation; TBs
129 * invalidated, so resume execution. Retain helper_retaddr
130 * for a possible second fault.
134 /* Fault caused by protection of cached translation, and the
135 * currently executing TB was modified and must be exited
136 * immediately. Clear helper_retaddr for next execution.
139 cpu_exit_tb_from_sighandler(cpu
, old_set
);
143 g_assert_not_reached();
147 /* Convert forcefully to guest address space, invalid addresses
148 are still valid segv ones */
149 address
= h2g_nocheck(address
);
152 * There is no way the target can handle this other than raising
153 * an exception. Undo signal and retaddr state prior to longjmp.
155 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
158 cc
= CPU_GET_CLASS(cpu
);
160 access_type
= is_write
? MMU_DATA_STORE
: MMU_DATA_LOAD
;
161 cc
->tlb_fill(cpu
, address
, 0, access_type
, MMU_USER_IDX
, false, pc
);
162 g_assert_not_reached();
164 ret
= cc
->handle_mmu_fault(cpu
, address
, 0, is_write
, MMU_USER_IDX
);
166 cpu_loop_exit_restore(cpu
, pc
);
170 #if defined(__i386__)
172 #if defined(__NetBSD__)
173 #include <ucontext.h>
175 #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
176 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
177 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
178 #define MASK_sig(context) ((context)->uc_sigmask)
179 #elif defined(__FreeBSD__) || defined(__DragonFly__)
180 #include <ucontext.h>
182 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
183 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
184 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
185 #define MASK_sig(context) ((context)->uc_sigmask)
186 #elif defined(__OpenBSD__)
187 #define EIP_sig(context) ((context)->sc_eip)
188 #define TRAP_sig(context) ((context)->sc_trapno)
189 #define ERROR_sig(context) ((context)->sc_err)
190 #define MASK_sig(context) ((context)->sc_mask)
192 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
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)
198 int cpu_signal_handler(int host_signum
, void *pinfo
,
201 siginfo_t
*info
= pinfo
;
202 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
203 ucontext_t
*uc
= puc
;
204 #elif defined(__OpenBSD__)
205 struct sigcontext
*uc
= puc
;
207 ucontext_t
*uc
= puc
;
216 #define REG_TRAPNO TRAPNO
219 trapno
= TRAP_sig(uc
);
220 return handle_cpu_signal(pc
, info
,
221 trapno
== 0xe ? (ERROR_sig(uc
) >> 1) & 1 : 0,
225 #elif defined(__x86_64__)
228 #define PC_sig(context) _UC_MACHINE_PC(context)
229 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
230 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
231 #define MASK_sig(context) ((context)->uc_sigmask)
232 #elif defined(__OpenBSD__)
233 #define PC_sig(context) ((context)->sc_rip)
234 #define TRAP_sig(context) ((context)->sc_trapno)
235 #define ERROR_sig(context) ((context)->sc_err)
236 #define MASK_sig(context) ((context)->sc_mask)
237 #elif defined(__FreeBSD__) || defined(__DragonFly__)
238 #include <ucontext.h>
240 #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
241 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
242 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
243 #define MASK_sig(context) ((context)->uc_sigmask)
245 #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
246 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
247 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
248 #define MASK_sig(context) ((context)->uc_sigmask)
251 int cpu_signal_handler(int host_signum
, void *pinfo
,
254 siginfo_t
*info
= pinfo
;
256 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
257 ucontext_t
*uc
= puc
;
258 #elif defined(__OpenBSD__)
259 struct sigcontext
*uc
= puc
;
261 ucontext_t
*uc
= puc
;
265 return handle_cpu_signal(pc
, info
,
266 TRAP_sig(uc
) == 0xe ? (ERROR_sig(uc
) >> 1) & 1 : 0,
270 #elif defined(_ARCH_PPC)
272 /***********************************************************************
273 * signal context platform-specific definitions
277 /* All Registers access - only for local access */
278 #define REG_sig(reg_name, context) \
279 ((context)->uc_mcontext.regs->reg_name)
280 /* Gpr Registers access */
281 #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
282 /* Program counter */
283 #define IAR_sig(context) REG_sig(nip, context)
284 /* Machine State Register (Supervisor) */
285 #define MSR_sig(context) REG_sig(msr, context)
287 #define CTR_sig(context) REG_sig(ctr, context)
288 /* User's integer exception register */
289 #define XER_sig(context) REG_sig(xer, context)
291 #define LR_sig(context) REG_sig(link, context)
292 /* Condition register */
293 #define CR_sig(context) REG_sig(ccr, context)
295 /* Float Registers access */
296 #define FLOAT_sig(reg_num, context) \
297 (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
298 #define FPSCR_sig(context) \
299 (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
300 /* Exception Registers access */
301 #define DAR_sig(context) REG_sig(dar, context)
302 #define DSISR_sig(context) REG_sig(dsisr, context)
303 #define TRAP_sig(context) REG_sig(trap, context)
306 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
307 #include <ucontext.h>
308 #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
309 #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
310 #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
311 #define XER_sig(context) ((context)->uc_mcontext.mc_xer)
312 #define LR_sig(context) ((context)->uc_mcontext.mc_lr)
313 #define CR_sig(context) ((context)->uc_mcontext.mc_cr)
314 /* Exception Registers access */
315 #define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
316 #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
317 #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
318 #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
320 int cpu_signal_handler(int host_signum
, void *pinfo
,
323 siginfo_t
*info
= pinfo
;
324 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
325 ucontext_t
*uc
= puc
;
327 ucontext_t
*uc
= puc
;
336 if (DSISR_sig(uc
) & 0x00800000) {
340 if (TRAP_sig(uc
) != 0x400 && (DSISR_sig(uc
) & 0x02000000)) {
344 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
347 #elif defined(__alpha__)
349 int cpu_signal_handler(int host_signum
, void *pinfo
,
352 siginfo_t
*info
= pinfo
;
353 ucontext_t
*uc
= puc
;
354 uint32_t *pc
= uc
->uc_mcontext
.sc_pc
;
358 /* XXX: need kernel patch to get write flag faster */
359 switch (insn
>> 26) {
362 case 0x0f: /* stq_u */
369 case 0x2e: /* stl_c */
370 case 0x2f: /* stq_c */
374 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
376 #elif defined(__sparc__)
378 int cpu_signal_handler(int host_signum
, void *pinfo
,
381 siginfo_t
*info
= pinfo
;
384 #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
385 uint32_t *regs
= (uint32_t *)(info
+ 1);
386 void *sigmask
= (regs
+ 20);
387 /* XXX: is there a standard glibc define ? */
388 unsigned long pc
= regs
[1];
391 struct sigcontext
*sc
= puc
;
392 unsigned long pc
= sc
->sigc_regs
.tpc
;
393 void *sigmask
= (void *)sc
->sigc_mask
;
394 #elif defined(__OpenBSD__)
395 struct sigcontext
*uc
= puc
;
396 unsigned long pc
= uc
->sc_pc
;
397 void *sigmask
= (void *)(long)uc
->sc_mask
;
398 #elif defined(__NetBSD__)
399 ucontext_t
*uc
= puc
;
400 unsigned long pc
= _UC_MACHINE_PC(uc
);
401 void *sigmask
= (void *)&uc
->uc_sigmask
;
405 /* XXX: need kernel patch to get write flag faster */
407 insn
= *(uint32_t *)pc
;
408 if ((insn
>> 30) == 3) {
409 switch ((insn
>> 19) & 0x3f) {
411 case 0x15: /* stba */
413 case 0x16: /* stha */
417 case 0x17: /* stda */
419 case 0x1e: /* stxa */
421 case 0x34: /* stfa */
422 case 0x27: /* stdf */
423 case 0x37: /* stdfa */
424 case 0x26: /* stqf */
425 case 0x36: /* stqfa */
426 case 0x25: /* stfsr */
427 case 0x3c: /* casa */
428 case 0x3e: /* casxa */
433 return handle_cpu_signal(pc
, info
, is_write
, sigmask
);
436 #elif defined(__arm__)
438 #if defined(__NetBSD__)
439 #include <ucontext.h>
442 int cpu_signal_handler(int host_signum
, void *pinfo
,
445 siginfo_t
*info
= pinfo
;
446 #if defined(__NetBSD__)
447 ucontext_t
*uc
= puc
;
449 ucontext_t
*uc
= puc
;
454 #if defined(__NetBSD__)
455 pc
= uc
->uc_mcontext
.__gregs
[_REG_R15
];
456 #elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
457 pc
= uc
->uc_mcontext
.gregs
[R15
];
459 pc
= uc
->uc_mcontext
.arm_pc
;
462 /* error_code is the FSR value, in which bit 11 is WnR (assuming a v6 or
463 * later processor; on v5 we will always report this as a read).
465 is_write
= extract32(uc
->uc_mcontext
.error_code
, 11, 1);
466 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
469 #elif defined(__aarch64__)
472 /* Pre-3.16 kernel headers don't have these, so provide fallback definitions */
473 #define ESR_MAGIC 0x45535201
475 struct _aarch64_ctx head
;
480 static inline struct _aarch64_ctx
*first_ctx(ucontext_t
*uc
)
482 return (struct _aarch64_ctx
*)&uc
->uc_mcontext
.__reserved
;
485 static inline struct _aarch64_ctx
*next_ctx(struct _aarch64_ctx
*hdr
)
487 return (struct _aarch64_ctx
*)((char *)hdr
+ hdr
->size
);
490 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
492 siginfo_t
*info
= pinfo
;
493 ucontext_t
*uc
= puc
;
494 uintptr_t pc
= uc
->uc_mcontext
.pc
;
496 struct _aarch64_ctx
*hdr
;
497 struct esr_context
const *esrctx
= NULL
;
499 /* Find the esr_context, which has the WnR bit in it */
500 for (hdr
= first_ctx(uc
); hdr
->magic
; hdr
= next_ctx(hdr
)) {
501 if (hdr
->magic
== ESR_MAGIC
) {
502 esrctx
= (struct esr_context
const *)hdr
;
508 /* For data aborts ESR.EC is 0b10010x: then bit 6 is the WnR bit */
509 uint64_t esr
= esrctx
->esr
;
510 is_write
= extract32(esr
, 27, 5) == 0x12 && extract32(esr
, 6, 1) == 1;
513 * Fall back to parsing instructions; will only be needed
514 * for really ancient (pre-3.16) kernels.
516 uint32_t insn
= *(uint32_t *)pc
;
518 is_write
= ((insn
& 0xbfff0000) == 0x0c000000 /* C3.3.1 */
519 || (insn
& 0xbfe00000) == 0x0c800000 /* C3.3.2 */
520 || (insn
& 0xbfdf0000) == 0x0d000000 /* C3.3.3 */
521 || (insn
& 0xbfc00000) == 0x0d800000 /* C3.3.4 */
522 || (insn
& 0x3f400000) == 0x08000000 /* C3.3.6 */
523 || (insn
& 0x3bc00000) == 0x39000000 /* C3.3.13 */
524 || (insn
& 0x3fc00000) == 0x3d800000 /* ... 128bit */
525 /* Ignore bits 10, 11 & 21, controlling indexing. */
526 || (insn
& 0x3bc00000) == 0x38000000 /* C3.3.8-12 */
527 || (insn
& 0x3fe00000) == 0x3c800000 /* ... 128bit */
528 /* Ignore bits 23 & 24, controlling indexing. */
529 || (insn
& 0x3a400000) == 0x28000000); /* C3.3.7,14-16 */
531 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
534 #elif defined(__s390__)
536 int cpu_signal_handler(int host_signum
, void *pinfo
,
539 siginfo_t
*info
= pinfo
;
540 ucontext_t
*uc
= puc
;
545 pc
= uc
->uc_mcontext
.psw
.addr
;
547 /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
548 of the normal 2 arguments. The 3rd argument contains the "int_code"
549 from the hardware which does in fact contain the is_write value.
550 The rt signal handler, as far as I can tell, does not give this value
551 at all. Not that we could get to it from here even if it were. */
552 /* ??? This is not even close to complete, since it ignores all
553 of the read-modify-write instructions. */
554 pinsn
= (uint16_t *)pc
;
555 switch (pinsn
[0] >> 8) {
561 case 0xc4: /* RIL format insns */
562 switch (pinsn
[0] & 0xf) {
564 case 0xb: /* STGRL */
565 case 0x7: /* STHRL */
569 case 0xe3: /* RXY format insns */
570 switch (pinsn
[2] & 0xff) {
573 case 0x72: /* STCY */
574 case 0x70: /* STHY */
575 case 0x8e: /* STPQ */
576 case 0x3f: /* STRVH */
577 case 0x3e: /* STRV */
578 case 0x2f: /* STRVG */
583 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
586 #elif defined(__mips__)
588 int cpu_signal_handler(int host_signum
, void *pinfo
,
591 siginfo_t
*info
= pinfo
;
592 ucontext_t
*uc
= puc
;
593 greg_t pc
= uc
->uc_mcontext
.pc
;
596 /* XXX: compute is_write */
598 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
601 #elif defined(__riscv)
603 int cpu_signal_handler(int host_signum
, void *pinfo
,
606 siginfo_t
*info
= pinfo
;
607 ucontext_t
*uc
= puc
;
608 greg_t pc
= uc
->uc_mcontext
.__gregs
[REG_PC
];
609 uint32_t insn
= *(uint32_t *)pc
;
612 /* Detect store by reading the instruction at the program
613 counter. Note: we currently only generate 32-bit
614 instructions so we thus only detect 32-bit stores */
615 switch (((insn
>> 0) & 0b11)) {
617 switch (((insn
>> 2) & 0b11111)) {
619 switch (((insn
>> 12) & 0b111)) {
632 switch (((insn
>> 12) & 0b111)) {
647 /* Check for compressed instructions */
648 switch (((insn
>> 13) & 0b111)) {
650 switch (insn
& 0b11) {
660 switch (insn
& 0b11) {
673 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
678 #error host CPU specific signal handler needed
682 /* The softmmu versions of these helpers are in cputlb.c. */
684 /* Do not allow unaligned operations to proceed. Return the host address. */
685 static void *atomic_mmu_lookup(CPUArchState
*env
, target_ulong addr
,
686 int size
, uintptr_t retaddr
)
688 /* Enforce qemu required alignment. */
689 if (unlikely(addr
& (size
- 1))) {
690 cpu_loop_exit_atomic(ENV_GET_CPU(env
), retaddr
);
692 helper_retaddr
= retaddr
;
696 /* Macro to call the above, with local variables from the use context. */
697 #define ATOMIC_MMU_DECLS do {} while (0)
698 #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, DATA_SIZE, GETPC())
699 #define ATOMIC_MMU_CLEANUP do { helper_retaddr = 0; } while (0)
701 #define ATOMIC_NAME(X) HELPER(glue(glue(atomic_ ## X, SUFFIX), END))
705 #include "atomic_template.h"
708 #include "atomic_template.h"
711 #include "atomic_template.h"
713 #ifdef CONFIG_ATOMIC64
715 #include "atomic_template.h"
718 /* The following is only callable from other helpers, and matches up
719 with the softmmu version. */
721 #if HAVE_ATOMIC128 || HAVE_CMPXCHG128
725 #undef ATOMIC_MMU_LOOKUP
727 #define EXTRA_ARGS , TCGMemOpIdx oi, uintptr_t retaddr
728 #define ATOMIC_NAME(X) \
729 HELPER(glue(glue(glue(atomic_ ## X, SUFFIX), END), _mmu))
730 #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, DATA_SIZE, retaddr)
733 #include "atomic_template.h"