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 "hw/core/tcg-cpu-ops.h"
22 #include "disas/disas.h"
23 #include "exec/exec-all.h"
25 #include "qemu/bitops.h"
26 #include "exec/cpu_ldst.h"
27 #include "exec/translate-all.h"
28 #include "exec/helper-proto.h"
29 #include "qemu/atomic128.h"
30 #include "trace/trace-root.h"
31 #include "trace/mem.h"
43 #include <sys/ucontext.h>
46 __thread
uintptr_t helper_retaddr
;
48 //#define DEBUG_SIGNAL
50 /* exit the current TB from a signal handler. The host registers are
51 restored in a state compatible with the CPU emulator
53 static void QEMU_NORETURN
cpu_exit_tb_from_sighandler(CPUState
*cpu
,
56 /* XXX: use siglongjmp ? */
57 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
58 cpu_loop_exit_noexc(cpu
);
61 /* 'pc' is the host PC at which the exception was raised. 'address' is
62 the effective address of the memory exception. 'is_write' is 1 if a
63 write caused the exception and otherwise 0'. 'old_set' is the
64 signal set which should be restored */
65 static inline int handle_cpu_signal(uintptr_t pc
, siginfo_t
*info
,
66 int is_write
, sigset_t
*old_set
)
68 CPUState
*cpu
= current_cpu
;
70 unsigned long address
= (unsigned long)info
->si_addr
;
71 MMUAccessType access_type
= is_write
? MMU_DATA_STORE
: MMU_DATA_LOAD
;
73 switch (helper_retaddr
) {
76 * Fault during host memory operation within a helper function.
77 * The helper's host return address, saved here, gives us a
78 * pointer into the generated code that will unwind to the
86 * Fault during host memory operation within generated code.
87 * (Or, a unrelated bug within qemu, but we can't tell from here).
89 * We take the host pc from the signal frame. However, we cannot
90 * use that value directly. Within cpu_restore_state_from_tb, we
91 * assume PC comes from GETPC(), as used by the helper functions,
92 * so we adjust the address by -GETPC_ADJ to form an address that
93 * is within the call insn, so that the address does not accidentally
94 * match the beginning of the next guest insn. However, when the
95 * pc comes from the signal frame it points to the actual faulting
96 * host memory insn and not the return from a call insn.
98 * Therefore, adjust to compensate for what will be done later
99 * by cpu_restore_state_from_tb.
106 * Fault during host read for translation, or loosely, "execution".
108 * The guest pc is already pointing to the start of the TB for which
109 * code is being generated. If the guest translator manages the
110 * page crossings correctly, this is exactly the correct address
111 * (and if the translator doesn't handle page boundaries correctly
112 * there's little we can do about that here). Therefore, do not
113 * trigger the unwinder.
115 * Like tb_gen_code, release the memory lock before cpu_loop_exit.
118 access_type
= MMU_INST_FETCH
;
123 /* For synchronous signals we expect to be coming from the vCPU
124 * thread (so current_cpu should be valid) and either from running
125 * code or during translation which can fault as we cross pages.
127 * If neither is true then something has gone wrong and we should
128 * abort rather than try and restart the vCPU execution.
130 if (!cpu
|| !cpu
->running
) {
131 printf("qemu:%s received signal outside vCPU context @ pc=0x%"
132 PRIxPTR
"\n", __func__
, pc
);
136 #if defined(DEBUG_SIGNAL)
137 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
138 pc
, address
, is_write
, *(unsigned long *)old_set
);
140 /* XXX: locking issue */
141 /* Note that it is important that we don't call page_unprotect() unless
142 * this is really a "write to nonwriteable page" fault, because
143 * page_unprotect() assumes that if it is called for an access to
144 * a page that's writeable this means we had two threads racing and
145 * another thread got there first and already made the page writeable;
146 * so we will retry the access. If we were to call page_unprotect()
147 * for some other kind of fault that should really be passed to the
148 * guest, we'd end up in an infinite loop of retrying the faulting
151 if (is_write
&& info
->si_signo
== SIGSEGV
&& info
->si_code
== SEGV_ACCERR
&&
152 h2g_valid(address
)) {
153 switch (page_unprotect(h2g(address
), pc
)) {
155 /* Fault not caused by a page marked unwritable to protect
156 * cached translations, must be the guest binary's problem.
160 /* Fault caused by protection of cached translation; TBs
161 * invalidated, so resume execution. Retain helper_retaddr
162 * for a possible second fault.
166 /* Fault caused by protection of cached translation, and the
167 * currently executing TB was modified and must be exited
168 * immediately. Clear helper_retaddr for next execution.
170 clear_helper_retaddr();
171 cpu_exit_tb_from_sighandler(cpu
, old_set
);
175 g_assert_not_reached();
179 /* Convert forcefully to guest address space, invalid addresses
180 are still valid segv ones */
181 address
= h2g_nocheck(address
);
184 * There is no way the target can handle this other than raising
185 * an exception. Undo signal and retaddr state prior to longjmp.
187 sigprocmask(SIG_SETMASK
, old_set
, NULL
);
188 clear_helper_retaddr();
190 cc
= CPU_GET_CLASS(cpu
);
191 cc
->tcg_ops
->tlb_fill(cpu
, address
, 0, access_type
,
192 MMU_USER_IDX
, false, pc
);
193 g_assert_not_reached();
196 static int probe_access_internal(CPUArchState
*env
, target_ulong addr
,
197 int fault_size
, MMUAccessType access_type
,
198 bool nonfault
, uintptr_t ra
)
202 switch (access_type
) {
213 g_assert_not_reached();
216 if (!guest_addr_valid(addr
) || page_check_range(addr
, 1, flags
) < 0) {
218 return TLB_INVALID_MASK
;
220 CPUState
*cpu
= env_cpu(env
);
221 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
222 cc
->tcg_ops
->tlb_fill(cpu
, addr
, fault_size
, access_type
,
223 MMU_USER_IDX
, false, ra
);
224 g_assert_not_reached();
230 int probe_access_flags(CPUArchState
*env
, target_ulong addr
,
231 MMUAccessType access_type
, int mmu_idx
,
232 bool nonfault
, void **phost
, uintptr_t ra
)
236 flags
= probe_access_internal(env
, addr
, 0, access_type
, nonfault
, ra
);
237 *phost
= flags
? NULL
: g2h(addr
);
241 void *probe_access(CPUArchState
*env
, target_ulong addr
, int size
,
242 MMUAccessType access_type
, int mmu_idx
, uintptr_t ra
)
246 g_assert(-(addr
| TARGET_PAGE_MASK
) >= size
);
247 flags
= probe_access_internal(env
, addr
, size
, access_type
, false, ra
);
248 g_assert(flags
== 0);
250 return size
? g2h(addr
) : NULL
;
253 #if defined(__i386__)
255 #if defined(__NetBSD__)
256 #include <ucontext.h>
258 #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
259 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
260 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
261 #define MASK_sig(context) ((context)->uc_sigmask)
262 #elif defined(__FreeBSD__) || defined(__DragonFly__)
263 #include <ucontext.h>
265 #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
266 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
267 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
268 #define MASK_sig(context) ((context)->uc_sigmask)
269 #elif defined(__OpenBSD__)
270 #define EIP_sig(context) ((context)->sc_eip)
271 #define TRAP_sig(context) ((context)->sc_trapno)
272 #define ERROR_sig(context) ((context)->sc_err)
273 #define MASK_sig(context) ((context)->sc_mask)
275 #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
276 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
277 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
278 #define MASK_sig(context) ((context)->uc_sigmask)
281 int cpu_signal_handler(int host_signum
, void *pinfo
,
284 siginfo_t
*info
= pinfo
;
285 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
286 ucontext_t
*uc
= puc
;
287 #elif defined(__OpenBSD__)
288 struct sigcontext
*uc
= puc
;
290 ucontext_t
*uc
= puc
;
299 #define REG_TRAPNO TRAPNO
302 trapno
= TRAP_sig(uc
);
303 return handle_cpu_signal(pc
, info
,
304 trapno
== 0xe ? (ERROR_sig(uc
) >> 1) & 1 : 0,
308 #elif defined(__x86_64__)
311 #define PC_sig(context) _UC_MACHINE_PC(context)
312 #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
313 #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
314 #define MASK_sig(context) ((context)->uc_sigmask)
315 #elif defined(__OpenBSD__)
316 #define PC_sig(context) ((context)->sc_rip)
317 #define TRAP_sig(context) ((context)->sc_trapno)
318 #define ERROR_sig(context) ((context)->sc_err)
319 #define MASK_sig(context) ((context)->sc_mask)
320 #elif defined(__FreeBSD__) || defined(__DragonFly__)
321 #include <ucontext.h>
323 #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
324 #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
325 #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
326 #define MASK_sig(context) ((context)->uc_sigmask)
328 #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
329 #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
330 #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
331 #define MASK_sig(context) ((context)->uc_sigmask)
334 int cpu_signal_handler(int host_signum
, void *pinfo
,
337 siginfo_t
*info
= pinfo
;
339 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
340 ucontext_t
*uc
= puc
;
341 #elif defined(__OpenBSD__)
342 struct sigcontext
*uc
= puc
;
344 ucontext_t
*uc
= puc
;
348 return handle_cpu_signal(pc
, info
,
349 TRAP_sig(uc
) == 0xe ? (ERROR_sig(uc
) >> 1) & 1 : 0,
353 #elif defined(_ARCH_PPC)
355 /***********************************************************************
356 * signal context platform-specific definitions
360 /* All Registers access - only for local access */
361 #define REG_sig(reg_name, context) \
362 ((context)->uc_mcontext.regs->reg_name)
363 /* Gpr Registers access */
364 #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
365 /* Program counter */
366 #define IAR_sig(context) REG_sig(nip, context)
367 /* Machine State Register (Supervisor) */
368 #define MSR_sig(context) REG_sig(msr, context)
370 #define CTR_sig(context) REG_sig(ctr, context)
371 /* User's integer exception register */
372 #define XER_sig(context) REG_sig(xer, context)
374 #define LR_sig(context) REG_sig(link, context)
375 /* Condition register */
376 #define CR_sig(context) REG_sig(ccr, context)
378 /* Float Registers access */
379 #define FLOAT_sig(reg_num, context) \
380 (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
381 #define FPSCR_sig(context) \
382 (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
383 /* Exception Registers access */
384 #define DAR_sig(context) REG_sig(dar, context)
385 #define DSISR_sig(context) REG_sig(dsisr, context)
386 #define TRAP_sig(context) REG_sig(trap, context)
389 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
390 #include <ucontext.h>
391 #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
392 #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
393 #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
394 #define XER_sig(context) ((context)->uc_mcontext.mc_xer)
395 #define LR_sig(context) ((context)->uc_mcontext.mc_lr)
396 #define CR_sig(context) ((context)->uc_mcontext.mc_cr)
397 /* Exception Registers access */
398 #define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
399 #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
400 #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
401 #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
403 int cpu_signal_handler(int host_signum
, void *pinfo
,
406 siginfo_t
*info
= pinfo
;
407 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
408 ucontext_t
*uc
= puc
;
410 ucontext_t
*uc
= puc
;
419 if (DSISR_sig(uc
) & 0x00800000) {
423 if (TRAP_sig(uc
) != 0x400 && (DSISR_sig(uc
) & 0x02000000)) {
427 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
430 #elif defined(__alpha__)
432 int cpu_signal_handler(int host_signum
, void *pinfo
,
435 siginfo_t
*info
= pinfo
;
436 ucontext_t
*uc
= puc
;
437 uint32_t *pc
= uc
->uc_mcontext
.sc_pc
;
441 /* XXX: need kernel patch to get write flag faster */
442 switch (insn
>> 26) {
445 case 0x0f: /* stq_u */
452 case 0x2e: /* stl_c */
453 case 0x2f: /* stq_c */
457 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
459 #elif defined(__sparc__)
461 int cpu_signal_handler(int host_signum
, void *pinfo
,
464 siginfo_t
*info
= pinfo
;
467 #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
468 uint32_t *regs
= (uint32_t *)(info
+ 1);
469 void *sigmask
= (regs
+ 20);
470 /* XXX: is there a standard glibc define ? */
471 unsigned long pc
= regs
[1];
474 struct sigcontext
*sc
= puc
;
475 unsigned long pc
= sc
->sigc_regs
.tpc
;
476 void *sigmask
= (void *)sc
->sigc_mask
;
477 #elif defined(__OpenBSD__)
478 struct sigcontext
*uc
= puc
;
479 unsigned long pc
= uc
->sc_pc
;
480 void *sigmask
= (void *)(long)uc
->sc_mask
;
481 #elif defined(__NetBSD__)
482 ucontext_t
*uc
= puc
;
483 unsigned long pc
= _UC_MACHINE_PC(uc
);
484 void *sigmask
= (void *)&uc
->uc_sigmask
;
488 /* XXX: need kernel patch to get write flag faster */
490 insn
= *(uint32_t *)pc
;
491 if ((insn
>> 30) == 3) {
492 switch ((insn
>> 19) & 0x3f) {
494 case 0x15: /* stba */
496 case 0x16: /* stha */
500 case 0x17: /* stda */
502 case 0x1e: /* stxa */
504 case 0x34: /* stfa */
505 case 0x27: /* stdf */
506 case 0x37: /* stdfa */
507 case 0x26: /* stqf */
508 case 0x36: /* stqfa */
509 case 0x25: /* stfsr */
510 case 0x3c: /* casa */
511 case 0x3e: /* casxa */
516 return handle_cpu_signal(pc
, info
, is_write
, sigmask
);
519 #elif defined(__arm__)
521 #if defined(__NetBSD__)
522 #include <ucontext.h>
523 #include <sys/siginfo.h>
526 int cpu_signal_handler(int host_signum
, void *pinfo
,
529 siginfo_t
*info
= pinfo
;
530 #if defined(__NetBSD__)
531 ucontext_t
*uc
= puc
;
532 siginfo_t
*si
= pinfo
;
534 ucontext_t
*uc
= puc
;
540 #if defined(__NetBSD__)
541 pc
= uc
->uc_mcontext
.__gregs
[_REG_R15
];
542 #elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
543 pc
= uc
->uc_mcontext
.gregs
[R15
];
545 pc
= uc
->uc_mcontext
.arm_pc
;
551 fsr
= uc
->uc_mcontext
.error_code
;
554 * In the FSR, bit 11 is WnR, assuming a v6 or
555 * later processor. On v5 we will always report
556 * this as a read, which will fail later.
558 is_write
= extract32(fsr
, 11, 1);
559 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
562 #elif defined(__aarch64__)
564 #if defined(__NetBSD__)
566 #include <ucontext.h>
567 #include <sys/siginfo.h>
569 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
571 ucontext_t
*uc
= puc
;
572 siginfo_t
*si
= pinfo
;
577 pc
= uc
->uc_mcontext
.__gregs
[_REG_PC
];
581 * siginfo_t::si_trap is the ESR value, for data aborts ESR.EC
582 * is 0b10010x: then bit 6 is the WnR bit
584 is_write
= extract32(esr
, 27, 5) == 0x12 && extract32(esr
, 6, 1) == 1;
585 return handle_cpu_signal(pc
, si
, is_write
, &uc
->uc_sigmask
);
591 /* Pre-3.16 kernel headers don't have these, so provide fallback definitions */
592 #define ESR_MAGIC 0x45535201
594 struct _aarch64_ctx head
;
599 static inline struct _aarch64_ctx
*first_ctx(ucontext_t
*uc
)
601 return (struct _aarch64_ctx
*)&uc
->uc_mcontext
.__reserved
;
604 static inline struct _aarch64_ctx
*next_ctx(struct _aarch64_ctx
*hdr
)
606 return (struct _aarch64_ctx
*)((char *)hdr
+ hdr
->size
);
609 int cpu_signal_handler(int host_signum
, void *pinfo
, void *puc
)
611 siginfo_t
*info
= pinfo
;
612 ucontext_t
*uc
= puc
;
613 uintptr_t pc
= uc
->uc_mcontext
.pc
;
615 struct _aarch64_ctx
*hdr
;
616 struct esr_context
const *esrctx
= NULL
;
618 /* Find the esr_context, which has the WnR bit in it */
619 for (hdr
= first_ctx(uc
); hdr
->magic
; hdr
= next_ctx(hdr
)) {
620 if (hdr
->magic
== ESR_MAGIC
) {
621 esrctx
= (struct esr_context
const *)hdr
;
627 /* For data aborts ESR.EC is 0b10010x: then bit 6 is the WnR bit */
628 uint64_t esr
= esrctx
->esr
;
629 is_write
= extract32(esr
, 27, 5) == 0x12 && extract32(esr
, 6, 1) == 1;
632 * Fall back to parsing instructions; will only be needed
633 * for really ancient (pre-3.16) kernels.
635 uint32_t insn
= *(uint32_t *)pc
;
637 is_write
= ((insn
& 0xbfff0000) == 0x0c000000 /* C3.3.1 */
638 || (insn
& 0xbfe00000) == 0x0c800000 /* C3.3.2 */
639 || (insn
& 0xbfdf0000) == 0x0d000000 /* C3.3.3 */
640 || (insn
& 0xbfc00000) == 0x0d800000 /* C3.3.4 */
641 || (insn
& 0x3f400000) == 0x08000000 /* C3.3.6 */
642 || (insn
& 0x3bc00000) == 0x39000000 /* C3.3.13 */
643 || (insn
& 0x3fc00000) == 0x3d800000 /* ... 128bit */
644 /* Ignore bits 10, 11 & 21, controlling indexing. */
645 || (insn
& 0x3bc00000) == 0x38000000 /* C3.3.8-12 */
646 || (insn
& 0x3fe00000) == 0x3c800000 /* ... 128bit */
647 /* Ignore bits 23 & 24, controlling indexing. */
648 || (insn
& 0x3a400000) == 0x28000000); /* C3.3.7,14-16 */
650 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
654 #elif defined(__s390__)
656 int cpu_signal_handler(int host_signum
, void *pinfo
,
659 siginfo_t
*info
= pinfo
;
660 ucontext_t
*uc
= puc
;
665 pc
= uc
->uc_mcontext
.psw
.addr
;
667 /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
668 of the normal 2 arguments. The 3rd argument contains the "int_code"
669 from the hardware which does in fact contain the is_write value.
670 The rt signal handler, as far as I can tell, does not give this value
671 at all. Not that we could get to it from here even if it were. */
672 /* ??? This is not even close to complete, since it ignores all
673 of the read-modify-write instructions. */
674 pinsn
= (uint16_t *)pc
;
675 switch (pinsn
[0] >> 8) {
681 case 0xc4: /* RIL format insns */
682 switch (pinsn
[0] & 0xf) {
684 case 0xb: /* STGRL */
685 case 0x7: /* STHRL */
689 case 0xe3: /* RXY format insns */
690 switch (pinsn
[2] & 0xff) {
693 case 0x72: /* STCY */
694 case 0x70: /* STHY */
695 case 0x8e: /* STPQ */
696 case 0x3f: /* STRVH */
697 case 0x3e: /* STRV */
698 case 0x2f: /* STRVG */
703 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
706 #elif defined(__mips__)
708 #if defined(__misp16) || defined(__mips_micromips)
709 #error "Unsupported encoding"
712 int cpu_signal_handler(int host_signum
, void *pinfo
,
715 siginfo_t
*info
= pinfo
;
716 ucontext_t
*uc
= puc
;
717 uintptr_t pc
= uc
->uc_mcontext
.pc
;
718 uint32_t insn
= *(uint32_t *)pc
;
721 /* Detect all store instructions at program counter. */
722 switch((insn
>> 26) & 077) {
735 #if !defined(__mips_isa_rev) || __mips_isa_rev < 6
741 case 023: /* COP1X */
742 /* Required in all versions of MIPS64 since
743 MIPS64r1 and subsequent versions of MIPS32r2. */
744 switch (insn
& 077) {
745 case 010: /* SWXC1 */
746 case 011: /* SDXC1 */
747 case 015: /* SUXC1 */
753 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
756 #elif defined(__riscv)
758 int cpu_signal_handler(int host_signum
, void *pinfo
,
761 siginfo_t
*info
= pinfo
;
762 ucontext_t
*uc
= puc
;
763 greg_t pc
= uc
->uc_mcontext
.__gregs
[REG_PC
];
764 uint32_t insn
= *(uint32_t *)pc
;
767 /* Detect store by reading the instruction at the program
768 counter. Note: we currently only generate 32-bit
769 instructions so we thus only detect 32-bit stores */
770 switch (((insn
>> 0) & 0b11)) {
772 switch (((insn
>> 2) & 0b11111)) {
774 switch (((insn
>> 12) & 0b111)) {
787 switch (((insn
>> 12) & 0b111)) {
802 /* Check for compressed instructions */
803 switch (((insn
>> 13) & 0b111)) {
805 switch (insn
& 0b11) {
815 switch (insn
& 0b11) {
828 return handle_cpu_signal(pc
, info
, is_write
, &uc
->uc_sigmask
);
833 #error host CPU specific signal handler needed
837 /* The softmmu versions of these helpers are in cputlb.c. */
839 uint32_t cpu_ldub_data(CPUArchState
*env
, abi_ptr ptr
)
842 uint16_t meminfo
= trace_mem_get_info(MO_UB
, MMU_USER_IDX
, false);
844 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
845 ret
= ldub_p(g2h(ptr
));
846 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
850 int cpu_ldsb_data(CPUArchState
*env
, abi_ptr ptr
)
853 uint16_t meminfo
= trace_mem_get_info(MO_SB
, MMU_USER_IDX
, false);
855 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
856 ret
= ldsb_p(g2h(ptr
));
857 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
861 uint32_t cpu_lduw_be_data(CPUArchState
*env
, abi_ptr ptr
)
864 uint16_t meminfo
= trace_mem_get_info(MO_BEUW
, MMU_USER_IDX
, false);
866 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
867 ret
= lduw_be_p(g2h(ptr
));
868 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
872 int cpu_ldsw_be_data(CPUArchState
*env
, abi_ptr ptr
)
875 uint16_t meminfo
= trace_mem_get_info(MO_BESW
, MMU_USER_IDX
, false);
877 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
878 ret
= ldsw_be_p(g2h(ptr
));
879 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
883 uint32_t cpu_ldl_be_data(CPUArchState
*env
, abi_ptr ptr
)
886 uint16_t meminfo
= trace_mem_get_info(MO_BEUL
, MMU_USER_IDX
, false);
888 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
889 ret
= ldl_be_p(g2h(ptr
));
890 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
894 uint64_t cpu_ldq_be_data(CPUArchState
*env
, abi_ptr ptr
)
897 uint16_t meminfo
= trace_mem_get_info(MO_BEQ
, MMU_USER_IDX
, false);
899 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
900 ret
= ldq_be_p(g2h(ptr
));
901 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
905 uint32_t cpu_lduw_le_data(CPUArchState
*env
, abi_ptr ptr
)
908 uint16_t meminfo
= trace_mem_get_info(MO_LEUW
, MMU_USER_IDX
, false);
910 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
911 ret
= lduw_le_p(g2h(ptr
));
912 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
916 int cpu_ldsw_le_data(CPUArchState
*env
, abi_ptr ptr
)
919 uint16_t meminfo
= trace_mem_get_info(MO_LESW
, MMU_USER_IDX
, false);
921 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
922 ret
= ldsw_le_p(g2h(ptr
));
923 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
927 uint32_t cpu_ldl_le_data(CPUArchState
*env
, abi_ptr ptr
)
930 uint16_t meminfo
= trace_mem_get_info(MO_LEUL
, MMU_USER_IDX
, false);
932 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
933 ret
= ldl_le_p(g2h(ptr
));
934 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
938 uint64_t cpu_ldq_le_data(CPUArchState
*env
, abi_ptr ptr
)
941 uint16_t meminfo
= trace_mem_get_info(MO_LEQ
, MMU_USER_IDX
, false);
943 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
944 ret
= ldq_le_p(g2h(ptr
));
945 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
949 uint32_t cpu_ldub_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t retaddr
)
953 set_helper_retaddr(retaddr
);
954 ret
= cpu_ldub_data(env
, ptr
);
955 clear_helper_retaddr();
959 int cpu_ldsb_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t retaddr
)
963 set_helper_retaddr(retaddr
);
964 ret
= cpu_ldsb_data(env
, ptr
);
965 clear_helper_retaddr();
969 uint32_t cpu_lduw_be_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t retaddr
)
973 set_helper_retaddr(retaddr
);
974 ret
= cpu_lduw_be_data(env
, ptr
);
975 clear_helper_retaddr();
979 int cpu_ldsw_be_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t retaddr
)
983 set_helper_retaddr(retaddr
);
984 ret
= cpu_ldsw_be_data(env
, ptr
);
985 clear_helper_retaddr();
989 uint32_t cpu_ldl_be_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t retaddr
)
993 set_helper_retaddr(retaddr
);
994 ret
= cpu_ldl_be_data(env
, ptr
);
995 clear_helper_retaddr();
999 uint64_t cpu_ldq_be_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t retaddr
)
1003 set_helper_retaddr(retaddr
);
1004 ret
= cpu_ldq_be_data(env
, ptr
);
1005 clear_helper_retaddr();
1009 uint32_t cpu_lduw_le_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t retaddr
)
1013 set_helper_retaddr(retaddr
);
1014 ret
= cpu_lduw_le_data(env
, ptr
);
1015 clear_helper_retaddr();
1019 int cpu_ldsw_le_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t retaddr
)
1023 set_helper_retaddr(retaddr
);
1024 ret
= cpu_ldsw_le_data(env
, ptr
);
1025 clear_helper_retaddr();
1029 uint32_t cpu_ldl_le_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t retaddr
)
1033 set_helper_retaddr(retaddr
);
1034 ret
= cpu_ldl_le_data(env
, ptr
);
1035 clear_helper_retaddr();
1039 uint64_t cpu_ldq_le_data_ra(CPUArchState
*env
, abi_ptr ptr
, uintptr_t retaddr
)
1043 set_helper_retaddr(retaddr
);
1044 ret
= cpu_ldq_le_data(env
, ptr
);
1045 clear_helper_retaddr();
1049 void cpu_stb_data(CPUArchState
*env
, abi_ptr ptr
, uint32_t val
)
1051 uint16_t meminfo
= trace_mem_get_info(MO_UB
, MMU_USER_IDX
, true);
1053 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
1054 stb_p(g2h(ptr
), val
);
1055 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
1058 void cpu_stw_be_data(CPUArchState
*env
, abi_ptr ptr
, uint32_t val
)
1060 uint16_t meminfo
= trace_mem_get_info(MO_BEUW
, MMU_USER_IDX
, true);
1062 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
1063 stw_be_p(g2h(ptr
), val
);
1064 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
1067 void cpu_stl_be_data(CPUArchState
*env
, abi_ptr ptr
, uint32_t val
)
1069 uint16_t meminfo
= trace_mem_get_info(MO_BEUL
, MMU_USER_IDX
, true);
1071 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
1072 stl_be_p(g2h(ptr
), val
);
1073 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
1076 void cpu_stq_be_data(CPUArchState
*env
, abi_ptr ptr
, uint64_t val
)
1078 uint16_t meminfo
= trace_mem_get_info(MO_BEQ
, MMU_USER_IDX
, true);
1080 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
1081 stq_be_p(g2h(ptr
), val
);
1082 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
1085 void cpu_stw_le_data(CPUArchState
*env
, abi_ptr ptr
, uint32_t val
)
1087 uint16_t meminfo
= trace_mem_get_info(MO_LEUW
, MMU_USER_IDX
, true);
1089 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
1090 stw_le_p(g2h(ptr
), val
);
1091 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
1094 void cpu_stl_le_data(CPUArchState
*env
, abi_ptr ptr
, uint32_t val
)
1096 uint16_t meminfo
= trace_mem_get_info(MO_LEUL
, MMU_USER_IDX
, true);
1098 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
1099 stl_le_p(g2h(ptr
), val
);
1100 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
1103 void cpu_stq_le_data(CPUArchState
*env
, abi_ptr ptr
, uint64_t val
)
1105 uint16_t meminfo
= trace_mem_get_info(MO_LEQ
, MMU_USER_IDX
, true);
1107 trace_guest_mem_before_exec(env_cpu(env
), ptr
, meminfo
);
1108 stq_le_p(g2h(ptr
), val
);
1109 qemu_plugin_vcpu_mem_cb(env_cpu(env
), ptr
, meminfo
);
1112 void cpu_stb_data_ra(CPUArchState
*env
, abi_ptr ptr
,
1113 uint32_t val
, uintptr_t retaddr
)
1115 set_helper_retaddr(retaddr
);
1116 cpu_stb_data(env
, ptr
, val
);
1117 clear_helper_retaddr();
1120 void cpu_stw_be_data_ra(CPUArchState
*env
, abi_ptr ptr
,
1121 uint32_t val
, uintptr_t retaddr
)
1123 set_helper_retaddr(retaddr
);
1124 cpu_stw_be_data(env
, ptr
, val
);
1125 clear_helper_retaddr();
1128 void cpu_stl_be_data_ra(CPUArchState
*env
, abi_ptr ptr
,
1129 uint32_t val
, uintptr_t retaddr
)
1131 set_helper_retaddr(retaddr
);
1132 cpu_stl_be_data(env
, ptr
, val
);
1133 clear_helper_retaddr();
1136 void cpu_stq_be_data_ra(CPUArchState
*env
, abi_ptr ptr
,
1137 uint64_t val
, uintptr_t retaddr
)
1139 set_helper_retaddr(retaddr
);
1140 cpu_stq_be_data(env
, ptr
, val
);
1141 clear_helper_retaddr();
1144 void cpu_stw_le_data_ra(CPUArchState
*env
, abi_ptr ptr
,
1145 uint32_t val
, uintptr_t retaddr
)
1147 set_helper_retaddr(retaddr
);
1148 cpu_stw_le_data(env
, ptr
, val
);
1149 clear_helper_retaddr();
1152 void cpu_stl_le_data_ra(CPUArchState
*env
, abi_ptr ptr
,
1153 uint32_t val
, uintptr_t retaddr
)
1155 set_helper_retaddr(retaddr
);
1156 cpu_stl_le_data(env
, ptr
, val
);
1157 clear_helper_retaddr();
1160 void cpu_stq_le_data_ra(CPUArchState
*env
, abi_ptr ptr
,
1161 uint64_t val
, uintptr_t retaddr
)
1163 set_helper_retaddr(retaddr
);
1164 cpu_stq_le_data(env
, ptr
, val
);
1165 clear_helper_retaddr();
1168 uint32_t cpu_ldub_code(CPUArchState
*env
, abi_ptr ptr
)
1172 set_helper_retaddr(1);
1173 ret
= ldub_p(g2h(ptr
));
1174 clear_helper_retaddr();
1178 uint32_t cpu_lduw_code(CPUArchState
*env
, abi_ptr ptr
)
1182 set_helper_retaddr(1);
1183 ret
= lduw_p(g2h(ptr
));
1184 clear_helper_retaddr();
1188 uint32_t cpu_ldl_code(CPUArchState
*env
, abi_ptr ptr
)
1192 set_helper_retaddr(1);
1193 ret
= ldl_p(g2h(ptr
));
1194 clear_helper_retaddr();
1198 uint64_t cpu_ldq_code(CPUArchState
*env
, abi_ptr ptr
)
1202 set_helper_retaddr(1);
1203 ret
= ldq_p(g2h(ptr
));
1204 clear_helper_retaddr();
1208 /* Do not allow unaligned operations to proceed. Return the host address. */
1209 static void *atomic_mmu_lookup(CPUArchState
*env
, target_ulong addr
,
1210 int size
, uintptr_t retaddr
)
1212 /* Enforce qemu required alignment. */
1213 if (unlikely(addr
& (size
- 1))) {
1214 cpu_loop_exit_atomic(env_cpu(env
), retaddr
);
1216 void *ret
= g2h(addr
);
1217 set_helper_retaddr(retaddr
);
1221 /* Macro to call the above, with local variables from the use context. */
1222 #define ATOMIC_MMU_DECLS do {} while (0)
1223 #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, DATA_SIZE, GETPC())
1224 #define ATOMIC_MMU_CLEANUP do { clear_helper_retaddr(); } while (0)
1225 #define ATOMIC_MMU_IDX MMU_USER_IDX
1227 #define ATOMIC_NAME(X) HELPER(glue(glue(atomic_ ## X, SUFFIX), END))
1230 #include "atomic_common.c.inc"
1233 #include "atomic_template.h"
1236 #include "atomic_template.h"
1239 #include "atomic_template.h"
1241 #ifdef CONFIG_ATOMIC64
1243 #include "atomic_template.h"
1246 /* The following is only callable from other helpers, and matches up
1247 with the softmmu version. */
1249 #if HAVE_ATOMIC128 || HAVE_CMPXCHG128
1253 #undef ATOMIC_MMU_LOOKUP
1255 #define EXTRA_ARGS , TCGMemOpIdx oi, uintptr_t retaddr
1256 #define ATOMIC_NAME(X) \
1257 HELPER(glue(glue(glue(atomic_ ## X, SUFFIX), END), _mmu))
1258 #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, DATA_SIZE, retaddr)
1260 #define DATA_SIZE 16
1261 #include "atomic_template.h"