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
;
66 unsigned long address
= (unsigned long)info
->si_addr
;
67 MMUAccessType access_type
;
69 /* We must handle PC addresses from two different sources:
70 * a call return address and a signal frame address.
72 * Within cpu_restore_state_from_tb we assume the former and adjust
73 * the address by -GETPC_ADJ so that the address is within the call
74 * insn so that addr does not accidentally match the beginning of the
77 * However, when the PC comes from the signal frame, it points to
78 * the actual faulting host insn and not a call insn. Subtracting
79 * GETPC_ADJ in that case may accidentally match the previous guest insn.
81 * So for the later case, adjust forward to compensate for what
82 * will be done later by cpu_restore_state_from_tb.
90 /* For synchronous signals we expect to be coming from the vCPU
91 * thread (so current_cpu should be valid) and either from running
92 * code or during translation which can fault as we cross pages.
94 * If neither is true then something has gone wrong and we should
95 * abort rather than try and restart the vCPU execution.
97 if (!cpu
|| !cpu
->running
) {
98 printf("qemu:%s received signal outside vCPU context @ pc=0x%"
99 PRIxPTR
"\n", __func__
, pc
);
103 #if defined(DEBUG_SIGNAL)
104 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
105 pc
, address
, is_write
, *(unsigned long *)old_set
);
107 /* XXX: locking issue */
108 /* Note that it is important that we don't call page_unprotect() unless
109 * this is really a "write to nonwriteable page" fault, because
110 * page_unprotect() assumes that if it is called for an access to
111 * a page that's writeable this means we had two threads racing and
112 * another thread got there first and already made the page writeable;
113 * so we will retry the access. If we were to call page_unprotect()
114 * for some other kind of fault that should really be passed to the
115 * guest, we'd end up in an infinite loop of retrying the faulting
118 if (is_write
&& info
->si_signo
== SIGSEGV
&& info
->si_code
== SEGV_ACCERR
&&
119 h2g_valid(address
)) {
120 switch (page_unprotect(h2g(address
), pc
)) {
122 /* Fault not caused by a page marked unwritable to protect
123 * cached translations, must be the guest binary's problem.
127 /* Fault caused by protection of cached translation; TBs
128 * invalidated, so resume execution. Retain helper_retaddr
129 * for a possible second fault.
133 /* Fault caused by protection of cached translation, and the
134 * currently executing TB was modified and must be exited
135 * immediately. Clear helper_retaddr for next execution.
138 cpu_exit_tb_from_sighandler(cpu
, old_set
);
142 g_assert_not_reached();
146 /* Convert forcefully to guest address space, invalid addresses
147 are still valid segv ones */
148 address
= h2g_nocheck(address
);
151 * There is no way the target can handle this other than raising
152 * an exception. Undo signal and retaddr state prior to longjmp.
154 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
157 cc
= CPU_GET_CLASS(cpu
);
158 access_type
= is_write
? MMU_DATA_STORE
: MMU_DATA_LOAD
;
159 cc
->tlb_fill(cpu
, address
, 0, access_type
, MMU_USER_IDX
, false, pc
);
160 g_assert_not_reached();
163 #if defined(__i386__)
165 #if defined(__NetBSD__)
166 #include <ucontext.h>
168 #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
169 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
170 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
171 #define MASK_sig(context) ((context)->uc_sigmask)
172 #elif defined(__FreeBSD__) || defined(__DragonFly__)
173 #include <ucontext.h>
175 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
176 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
177 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
178 #define MASK_sig(context) ((context)->uc_sigmask)
179 #elif defined(__OpenBSD__)
180 #define EIP_sig(context) ((context)->sc_eip)
181 #define TRAP_sig(context) ((context)->sc_trapno)
182 #define ERROR_sig(context) ((context)->sc_err)
183 #define MASK_sig(context) ((context)->sc_mask)
185 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
186 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
187 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
188 #define MASK_sig(context) ((context)->uc_sigmask)
191 int cpu_signal_handler(int host_signum
, void *pinfo
,
194 siginfo_t
*info
= pinfo
;
195 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
196 ucontext_t
*uc
= puc
;
197 #elif defined(__OpenBSD__)
198 struct sigcontext
*uc
= puc
;
200 ucontext_t
*uc
= puc
;
209 #define REG_TRAPNO TRAPNO
212 trapno
= TRAP_sig(uc
);
213 return handle_cpu_signal(pc
, info
,
214 trapno
== 0xe ? (ERROR_sig(uc
) >> 1) & 1 : 0,
218 #elif defined(__x86_64__)
221 #define PC_sig(context) _UC_MACHINE_PC(context)
222 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
223 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
224 #define MASK_sig(context) ((context)->uc_sigmask)
225 #elif defined(__OpenBSD__)
226 #define PC_sig(context) ((context)->sc_rip)
227 #define TRAP_sig(context) ((context)->sc_trapno)
228 #define ERROR_sig(context) ((context)->sc_err)
229 #define MASK_sig(context) ((context)->sc_mask)
230 #elif defined(__FreeBSD__) || defined(__DragonFly__)
231 #include <ucontext.h>
233 #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
234 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
235 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
236 #define MASK_sig(context) ((context)->uc_sigmask)
238 #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
239 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
240 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
241 #define MASK_sig(context) ((context)->uc_sigmask)
244 int cpu_signal_handler(int host_signum
, void *pinfo
,
247 siginfo_t
*info
= pinfo
;
249 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
250 ucontext_t
*uc
= puc
;
251 #elif defined(__OpenBSD__)
252 struct sigcontext
*uc
= puc
;
254 ucontext_t
*uc
= puc
;
258 return handle_cpu_signal(pc
, info
,
259 TRAP_sig(uc
) == 0xe ? (ERROR_sig(uc
) >> 1) & 1 : 0,
263 #elif defined(_ARCH_PPC)
265 /***********************************************************************
266 * signal context platform-specific definitions
270 /* All Registers access - only for local access */
271 #define REG_sig(reg_name, context) \
272 ((context)->uc_mcontext.regs->reg_name)
273 /* Gpr Registers access */
274 #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
275 /* Program counter */
276 #define IAR_sig(context) REG_sig(nip, context)
277 /* Machine State Register (Supervisor) */
278 #define MSR_sig(context) REG_sig(msr, context)
280 #define CTR_sig(context) REG_sig(ctr, context)
281 /* User's integer exception register */
282 #define XER_sig(context) REG_sig(xer, context)
284 #define LR_sig(context) REG_sig(link, context)
285 /* Condition register */
286 #define CR_sig(context) REG_sig(ccr, context)
288 /* Float Registers access */
289 #define FLOAT_sig(reg_num, context) \
290 (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
291 #define FPSCR_sig(context) \
292 (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
293 /* Exception Registers access */
294 #define DAR_sig(context) REG_sig(dar, context)
295 #define DSISR_sig(context) REG_sig(dsisr, context)
296 #define TRAP_sig(context) REG_sig(trap, context)
299 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
300 #include <ucontext.h>
301 #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
302 #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
303 #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
304 #define XER_sig(context) ((context)->uc_mcontext.mc_xer)
305 #define LR_sig(context) ((context)->uc_mcontext.mc_lr)
306 #define CR_sig(context) ((context)->uc_mcontext.mc_cr)
307 /* Exception Registers access */
308 #define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
309 #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
310 #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
311 #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
313 int cpu_signal_handler(int host_signum
, void *pinfo
,
316 siginfo_t
*info
= pinfo
;
317 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
318 ucontext_t
*uc
= puc
;
320 ucontext_t
*uc
= puc
;
329 if (DSISR_sig(uc
) & 0x00800000) {
333 if (TRAP_sig(uc
) != 0x400 && (DSISR_sig(uc
) & 0x02000000)) {
337 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
340 #elif defined(__alpha__)
342 int cpu_signal_handler(int host_signum
, void *pinfo
,
345 siginfo_t
*info
= pinfo
;
346 ucontext_t
*uc
= puc
;
347 uint32_t *pc
= uc
->uc_mcontext
.sc_pc
;
351 /* XXX: need kernel patch to get write flag faster */
352 switch (insn
>> 26) {
355 case 0x0f: /* stq_u */
362 case 0x2e: /* stl_c */
363 case 0x2f: /* stq_c */
367 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
369 #elif defined(__sparc__)
371 int cpu_signal_handler(int host_signum
, void *pinfo
,
374 siginfo_t
*info
= pinfo
;
377 #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
378 uint32_t *regs
= (uint32_t *)(info
+ 1);
379 void *sigmask
= (regs
+ 20);
380 /* XXX: is there a standard glibc define ? */
381 unsigned long pc
= regs
[1];
384 struct sigcontext
*sc
= puc
;
385 unsigned long pc
= sc
->sigc_regs
.tpc
;
386 void *sigmask
= (void *)sc
->sigc_mask
;
387 #elif defined(__OpenBSD__)
388 struct sigcontext
*uc
= puc
;
389 unsigned long pc
= uc
->sc_pc
;
390 void *sigmask
= (void *)(long)uc
->sc_mask
;
391 #elif defined(__NetBSD__)
392 ucontext_t
*uc
= puc
;
393 unsigned long pc
= _UC_MACHINE_PC(uc
);
394 void *sigmask
= (void *)&uc
->uc_sigmask
;
398 /* XXX: need kernel patch to get write flag faster */
400 insn
= *(uint32_t *)pc
;
401 if ((insn
>> 30) == 3) {
402 switch ((insn
>> 19) & 0x3f) {
404 case 0x15: /* stba */
406 case 0x16: /* stha */
410 case 0x17: /* stda */
412 case 0x1e: /* stxa */
414 case 0x34: /* stfa */
415 case 0x27: /* stdf */
416 case 0x37: /* stdfa */
417 case 0x26: /* stqf */
418 case 0x36: /* stqfa */
419 case 0x25: /* stfsr */
420 case 0x3c: /* casa */
421 case 0x3e: /* casxa */
426 return handle_cpu_signal(pc
, info
, is_write
, sigmask
);
429 #elif defined(__arm__)
431 #if defined(__NetBSD__)
432 #include <ucontext.h>
435 int cpu_signal_handler(int host_signum
, void *pinfo
,
438 siginfo_t
*info
= pinfo
;
439 #if defined(__NetBSD__)
440 ucontext_t
*uc
= puc
;
442 ucontext_t
*uc
= puc
;
447 #if defined(__NetBSD__)
448 pc
= uc
->uc_mcontext
.__gregs
[_REG_R15
];
449 #elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
450 pc
= uc
->uc_mcontext
.gregs
[R15
];
452 pc
= uc
->uc_mcontext
.arm_pc
;
455 /* error_code is the FSR value, in which bit 11 is WnR (assuming a v6 or
456 * later processor; on v5 we will always report this as a read).
458 is_write
= extract32(uc
->uc_mcontext
.error_code
, 11, 1);
459 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
462 #elif defined(__aarch64__)
465 /* Pre-3.16 kernel headers don't have these, so provide fallback definitions */
466 #define ESR_MAGIC 0x45535201
468 struct _aarch64_ctx head
;
473 static inline struct _aarch64_ctx
*first_ctx(ucontext_t
*uc
)
475 return (struct _aarch64_ctx
*)&uc
->uc_mcontext
.__reserved
;
478 static inline struct _aarch64_ctx
*next_ctx(struct _aarch64_ctx
*hdr
)
480 return (struct _aarch64_ctx
*)((char *)hdr
+ hdr
->size
);
483 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
485 siginfo_t
*info
= pinfo
;
486 ucontext_t
*uc
= puc
;
487 uintptr_t pc
= uc
->uc_mcontext
.pc
;
489 struct _aarch64_ctx
*hdr
;
490 struct esr_context
const *esrctx
= NULL
;
492 /* Find the esr_context, which has the WnR bit in it */
493 for (hdr
= first_ctx(uc
); hdr
->magic
; hdr
= next_ctx(hdr
)) {
494 if (hdr
->magic
== ESR_MAGIC
) {
495 esrctx
= (struct esr_context
const *)hdr
;
501 /* For data aborts ESR.EC is 0b10010x: then bit 6 is the WnR bit */
502 uint64_t esr
= esrctx
->esr
;
503 is_write
= extract32(esr
, 27, 5) == 0x12 && extract32(esr
, 6, 1) == 1;
506 * Fall back to parsing instructions; will only be needed
507 * for really ancient (pre-3.16) kernels.
509 uint32_t insn
= *(uint32_t *)pc
;
511 is_write
= ((insn
& 0xbfff0000) == 0x0c000000 /* C3.3.1 */
512 || (insn
& 0xbfe00000) == 0x0c800000 /* C3.3.2 */
513 || (insn
& 0xbfdf0000) == 0x0d000000 /* C3.3.3 */
514 || (insn
& 0xbfc00000) == 0x0d800000 /* C3.3.4 */
515 || (insn
& 0x3f400000) == 0x08000000 /* C3.3.6 */
516 || (insn
& 0x3bc00000) == 0x39000000 /* C3.3.13 */
517 || (insn
& 0x3fc00000) == 0x3d800000 /* ... 128bit */
518 /* Ignore bits 10, 11 & 21, controlling indexing. */
519 || (insn
& 0x3bc00000) == 0x38000000 /* C3.3.8-12 */
520 || (insn
& 0x3fe00000) == 0x3c800000 /* ... 128bit */
521 /* Ignore bits 23 & 24, controlling indexing. */
522 || (insn
& 0x3a400000) == 0x28000000); /* C3.3.7,14-16 */
524 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
527 #elif defined(__s390__)
529 int cpu_signal_handler(int host_signum
, void *pinfo
,
532 siginfo_t
*info
= pinfo
;
533 ucontext_t
*uc
= puc
;
538 pc
= uc
->uc_mcontext
.psw
.addr
;
540 /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
541 of the normal 2 arguments. The 3rd argument contains the "int_code"
542 from the hardware which does in fact contain the is_write value.
543 The rt signal handler, as far as I can tell, does not give this value
544 at all. Not that we could get to it from here even if it were. */
545 /* ??? This is not even close to complete, since it ignores all
546 of the read-modify-write instructions. */
547 pinsn
= (uint16_t *)pc
;
548 switch (pinsn
[0] >> 8) {
554 case 0xc4: /* RIL format insns */
555 switch (pinsn
[0] & 0xf) {
557 case 0xb: /* STGRL */
558 case 0x7: /* STHRL */
562 case 0xe3: /* RXY format insns */
563 switch (pinsn
[2] & 0xff) {
566 case 0x72: /* STCY */
567 case 0x70: /* STHY */
568 case 0x8e: /* STPQ */
569 case 0x3f: /* STRVH */
570 case 0x3e: /* STRV */
571 case 0x2f: /* STRVG */
576 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
579 #elif defined(__mips__)
581 int cpu_signal_handler(int host_signum
, void *pinfo
,
584 siginfo_t
*info
= pinfo
;
585 ucontext_t
*uc
= puc
;
586 greg_t pc
= uc
->uc_mcontext
.pc
;
589 /* XXX: compute is_write */
591 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
594 #elif defined(__riscv)
596 int cpu_signal_handler(int host_signum
, void *pinfo
,
599 siginfo_t
*info
= pinfo
;
600 ucontext_t
*uc
= puc
;
601 greg_t pc
= uc
->uc_mcontext
.__gregs
[REG_PC
];
602 uint32_t insn
= *(uint32_t *)pc
;
605 /* Detect store by reading the instruction at the program
606 counter. Note: we currently only generate 32-bit
607 instructions so we thus only detect 32-bit stores */
608 switch (((insn
>> 0) & 0b11)) {
610 switch (((insn
>> 2) & 0b11111)) {
612 switch (((insn
>> 12) & 0b111)) {
625 switch (((insn
>> 12) & 0b111)) {
640 /* Check for compressed instructions */
641 switch (((insn
>> 13) & 0b111)) {
643 switch (insn
& 0b11) {
653 switch (insn
& 0b11) {
666 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
671 #error host CPU specific signal handler needed
675 /* The softmmu versions of these helpers are in cputlb.c. */
677 /* Do not allow unaligned operations to proceed. Return the host address. */
678 static void *atomic_mmu_lookup(CPUArchState
*env
, target_ulong addr
,
679 int size
, uintptr_t retaddr
)
681 /* Enforce qemu required alignment. */
682 if (unlikely(addr
& (size
- 1))) {
683 cpu_loop_exit_atomic(env_cpu(env
), retaddr
);
685 helper_retaddr
= retaddr
;
689 /* Macro to call the above, with local variables from the use context. */
690 #define ATOMIC_MMU_DECLS do {} while (0)
691 #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, DATA_SIZE, GETPC())
692 #define ATOMIC_MMU_CLEANUP do { helper_retaddr = 0; } while (0)
694 #define ATOMIC_NAME(X) HELPER(glue(glue(atomic_ ## X, SUFFIX), END))
698 #include "atomic_template.h"
701 #include "atomic_template.h"
704 #include "atomic_template.h"
706 #ifdef CONFIG_ATOMIC64
708 #include "atomic_template.h"
711 /* The following is only callable from other helpers, and matches up
712 with the softmmu version. */
714 #if HAVE_ATOMIC128 || HAVE_CMPXCHG128
718 #undef ATOMIC_MMU_LOOKUP
720 #define EXTRA_ARGS , TCGMemOpIdx oi, uintptr_t retaddr
721 #define ATOMIC_NAME(X) \
722 HELPER(glue(glue(glue(atomic_ ## X, SUFFIX), END), _mmu))
723 #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, DATA_SIZE, retaddr)
726 #include "atomic_template.h"