sev/i386: Add initial support for SEV-ES
[qemu/ar7.git] / accel / tcg / user-exec.c
blob0b6f56ca4073d9eaab1963490ed4ed6a88e59605
1 /*
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"
20 #include "cpu.h"
21 #include "hw/core/tcg-cpu-ops.h"
22 #include "disas/disas.h"
23 #include "exec/exec-all.h"
24 #include "tcg/tcg.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"
33 #undef EAX
34 #undef ECX
35 #undef EDX
36 #undef EBX
37 #undef ESP
38 #undef EBP
39 #undef ESI
40 #undef EDI
41 #undef EIP
42 #ifdef __linux__
43 #include <sys/ucontext.h>
44 #endif
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,
54 sigset_t *old_set)
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;
69 CPUClass *cc;
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) {
74 default:
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
79 * correct guest pc.
81 pc = helper_retaddr;
82 break;
84 case 0:
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.
101 pc += GETPC_ADJ;
102 break;
104 case 1:
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.
117 pc = 0;
118 access_type = MMU_INST_FETCH;
119 mmap_unlock();
120 break;
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);
133 abort();
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);
139 #endif
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
149 * access.
151 if (is_write && info->si_signo == SIGSEGV && info->si_code == SEGV_ACCERR &&
152 h2g_valid(address)) {
153 switch (page_unprotect(h2g(address), pc)) {
154 case 0:
155 /* Fault not caused by a page marked unwritable to protect
156 * cached translations, must be the guest binary's problem.
158 break;
159 case 1:
160 /* Fault caused by protection of cached translation; TBs
161 * invalidated, so resume execution. Retain helper_retaddr
162 * for a possible second fault.
164 return 1;
165 case 2:
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);
172 /* NORETURN */
174 default:
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)
200 int flags;
202 switch (access_type) {
203 case MMU_DATA_STORE:
204 flags = PAGE_WRITE;
205 break;
206 case MMU_DATA_LOAD:
207 flags = PAGE_READ;
208 break;
209 case MMU_INST_FETCH:
210 flags = PAGE_EXEC;
211 break;
212 default:
213 g_assert_not_reached();
216 if (!guest_addr_valid(addr) || page_check_range(addr, 1, flags) < 0) {
217 if (nonfault) {
218 return TLB_INVALID_MASK;
219 } else {
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();
227 return 0;
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)
234 int flags;
236 flags = probe_access_internal(env, addr, 0, access_type, nonfault, ra);
237 *phost = flags ? NULL : g2h(addr);
238 return flags;
241 void *probe_access(CPUArchState *env, target_ulong addr, int size,
242 MMUAccessType access_type, int mmu_idx, uintptr_t ra)
244 int flags;
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)
274 #else
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)
279 #endif
281 int cpu_signal_handler(int host_signum, void *pinfo,
282 void *puc)
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;
289 #else
290 ucontext_t *uc = puc;
291 #endif
292 unsigned long pc;
293 int trapno;
295 #ifndef REG_EIP
296 /* for glibc 2.1 */
297 #define REG_EIP EIP
298 #define REG_ERR ERR
299 #define REG_TRAPNO TRAPNO
300 #endif
301 pc = EIP_sig(uc);
302 trapno = TRAP_sig(uc);
303 return handle_cpu_signal(pc, info,
304 trapno == 0xe ? (ERROR_sig(uc) >> 1) & 1 : 0,
305 &MASK_sig(uc));
308 #elif defined(__x86_64__)
310 #ifdef __NetBSD__
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)
327 #else
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)
332 #endif
334 int cpu_signal_handler(int host_signum, void *pinfo,
335 void *puc)
337 siginfo_t *info = pinfo;
338 unsigned long pc;
339 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
340 ucontext_t *uc = puc;
341 #elif defined(__OpenBSD__)
342 struct sigcontext *uc = puc;
343 #else
344 ucontext_t *uc = puc;
345 #endif
347 pc = PC_sig(uc);
348 return handle_cpu_signal(pc, info,
349 TRAP_sig(uc) == 0xe ? (ERROR_sig(uc) >> 1) & 1 : 0,
350 &MASK_sig(uc));
353 #elif defined(_ARCH_PPC)
355 /***********************************************************************
356 * signal context platform-specific definitions
357 * From Wine
359 #ifdef linux
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)
369 /* Count register */
370 #define CTR_sig(context) REG_sig(ctr, context)
371 /* User's integer exception register */
372 #define XER_sig(context) REG_sig(xer, context)
373 /* Link register */
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)
387 #endif /* linux */
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,
404 void *puc)
406 siginfo_t *info = pinfo;
407 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
408 ucontext_t *uc = puc;
409 #else
410 ucontext_t *uc = puc;
411 #endif
412 unsigned long pc;
413 int is_write;
415 pc = IAR_sig(uc);
416 is_write = 0;
417 #if 0
418 /* ppc 4xx case */
419 if (DSISR_sig(uc) & 0x00800000) {
420 is_write = 1;
422 #else
423 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) {
424 is_write = 1;
426 #endif
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,
433 void *puc)
435 siginfo_t *info = pinfo;
436 ucontext_t *uc = puc;
437 uint32_t *pc = uc->uc_mcontext.sc_pc;
438 uint32_t insn = *pc;
439 int is_write = 0;
441 /* XXX: need kernel patch to get write flag faster */
442 switch (insn >> 26) {
443 case 0x0d: /* stw */
444 case 0x0e: /* stb */
445 case 0x0f: /* stq_u */
446 case 0x24: /* stf */
447 case 0x25: /* stg */
448 case 0x26: /* sts */
449 case 0x27: /* stt */
450 case 0x2c: /* stl */
451 case 0x2d: /* stq */
452 case 0x2e: /* stl_c */
453 case 0x2f: /* stq_c */
454 is_write = 1;
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,
462 void *puc)
464 siginfo_t *info = pinfo;
465 int is_write;
466 uint32_t insn;
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];
472 #else
473 #ifdef __linux__
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;
485 #endif
486 #endif
488 /* XXX: need kernel patch to get write flag faster */
489 is_write = 0;
490 insn = *(uint32_t *)pc;
491 if ((insn >> 30) == 3) {
492 switch ((insn >> 19) & 0x3f) {
493 case 0x05: /* stb */
494 case 0x15: /* stba */
495 case 0x06: /* sth */
496 case 0x16: /* stha */
497 case 0x04: /* st */
498 case 0x14: /* sta */
499 case 0x07: /* std */
500 case 0x17: /* stda */
501 case 0x0e: /* stx */
502 case 0x1e: /* stxa */
503 case 0x24: /* stf */
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 */
512 is_write = 1;
513 break;
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>
524 #endif
526 int cpu_signal_handler(int host_signum, void *pinfo,
527 void *puc)
529 siginfo_t *info = pinfo;
530 #if defined(__NetBSD__)
531 ucontext_t *uc = puc;
532 siginfo_t *si = pinfo;
533 #else
534 ucontext_t *uc = puc;
535 #endif
536 unsigned long pc;
537 uint32_t fsr;
538 int is_write;
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];
544 #else
545 pc = uc->uc_mcontext.arm_pc;
546 #endif
548 #ifdef __NetBSD__
549 fsr = si->si_trap;
550 #else
551 fsr = uc->uc_mcontext.error_code;
552 #endif
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;
573 unsigned long pc;
574 int is_write;
575 uint32_t esr;
577 pc = uc->uc_mcontext.__gregs[_REG_PC];
578 esr = si->si_trap;
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);
588 #else
590 #ifndef ESR_MAGIC
591 /* Pre-3.16 kernel headers don't have these, so provide fallback definitions */
592 #define ESR_MAGIC 0x45535201
593 struct esr_context {
594 struct _aarch64_ctx head;
595 uint64_t esr;
597 #endif
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;
614 bool is_write;
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;
622 break;
626 if (esrctx) {
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;
630 } else {
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);
652 #endif
654 #elif defined(__s390__)
656 int cpu_signal_handler(int host_signum, void *pinfo,
657 void *puc)
659 siginfo_t *info = pinfo;
660 ucontext_t *uc = puc;
661 unsigned long pc;
662 uint16_t *pinsn;
663 int is_write = 0;
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) {
676 case 0x50: /* ST */
677 case 0x42: /* STC */
678 case 0x40: /* STH */
679 is_write = 1;
680 break;
681 case 0xc4: /* RIL format insns */
682 switch (pinsn[0] & 0xf) {
683 case 0xf: /* STRL */
684 case 0xb: /* STGRL */
685 case 0x7: /* STHRL */
686 is_write = 1;
688 break;
689 case 0xe3: /* RXY format insns */
690 switch (pinsn[2] & 0xff) {
691 case 0x50: /* STY */
692 case 0x24: /* STG */
693 case 0x72: /* STCY */
694 case 0x70: /* STHY */
695 case 0x8e: /* STPQ */
696 case 0x3f: /* STRVH */
697 case 0x3e: /* STRV */
698 case 0x2f: /* STRVG */
699 is_write = 1;
701 break;
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"
710 #endif
712 int cpu_signal_handler(int host_signum, void *pinfo,
713 void *puc)
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;
719 int is_write = 0;
721 /* Detect all store instructions at program counter. */
722 switch((insn >> 26) & 077) {
723 case 050: /* SB */
724 case 051: /* SH */
725 case 052: /* SWL */
726 case 053: /* SW */
727 case 054: /* SDL */
728 case 055: /* SDR */
729 case 056: /* SWR */
730 case 070: /* SC */
731 case 071: /* SWC1 */
732 case 074: /* SCD */
733 case 075: /* SDC1 */
734 case 077: /* SD */
735 #if !defined(__mips_isa_rev) || __mips_isa_rev < 6
736 case 072: /* SWC2 */
737 case 076: /* SDC2 */
738 #endif
739 is_write = 1;
740 break;
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 */
748 is_write = 1;
750 break;
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,
759 void *puc)
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;
765 int is_write = 0;
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)) {
771 case 3:
772 switch (((insn >> 2) & 0b11111)) {
773 case 8:
774 switch (((insn >> 12) & 0b111)) {
775 case 0: /* sb */
776 case 1: /* sh */
777 case 2: /* sw */
778 case 3: /* sd */
779 case 4: /* sq */
780 is_write = 1;
781 break;
782 default:
783 break;
785 break;
786 case 9:
787 switch (((insn >> 12) & 0b111)) {
788 case 2: /* fsw */
789 case 3: /* fsd */
790 case 4: /* fsq */
791 is_write = 1;
792 break;
793 default:
794 break;
796 break;
797 default:
798 break;
802 /* Check for compressed instructions */
803 switch (((insn >> 13) & 0b111)) {
804 case 7:
805 switch (insn & 0b11) {
806 case 0: /*c.sd */
807 case 2: /* c.sdsp */
808 is_write = 1;
809 break;
810 default:
811 break;
813 break;
814 case 6:
815 switch (insn & 0b11) {
816 case 0: /* c.sw */
817 case 3: /* c.swsp */
818 is_write = 1;
819 break;
820 default:
821 break;
823 break;
824 default:
825 break;
828 return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
831 #else
833 #error host CPU specific signal handler needed
835 #endif
837 /* The softmmu versions of these helpers are in cputlb.c. */
839 uint32_t cpu_ldub_data(CPUArchState *env, abi_ptr ptr)
841 uint32_t ret;
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);
847 return ret;
850 int cpu_ldsb_data(CPUArchState *env, abi_ptr ptr)
852 int ret;
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);
858 return ret;
861 uint32_t cpu_lduw_be_data(CPUArchState *env, abi_ptr ptr)
863 uint32_t ret;
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);
869 return ret;
872 int cpu_ldsw_be_data(CPUArchState *env, abi_ptr ptr)
874 int ret;
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);
880 return ret;
883 uint32_t cpu_ldl_be_data(CPUArchState *env, abi_ptr ptr)
885 uint32_t ret;
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);
891 return ret;
894 uint64_t cpu_ldq_be_data(CPUArchState *env, abi_ptr ptr)
896 uint64_t ret;
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);
902 return ret;
905 uint32_t cpu_lduw_le_data(CPUArchState *env, abi_ptr ptr)
907 uint32_t ret;
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);
913 return ret;
916 int cpu_ldsw_le_data(CPUArchState *env, abi_ptr ptr)
918 int ret;
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);
924 return ret;
927 uint32_t cpu_ldl_le_data(CPUArchState *env, abi_ptr ptr)
929 uint32_t ret;
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);
935 return ret;
938 uint64_t cpu_ldq_le_data(CPUArchState *env, abi_ptr ptr)
940 uint64_t ret;
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);
946 return ret;
949 uint32_t cpu_ldub_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr)
951 uint32_t ret;
953 set_helper_retaddr(retaddr);
954 ret = cpu_ldub_data(env, ptr);
955 clear_helper_retaddr();
956 return ret;
959 int cpu_ldsb_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr)
961 int ret;
963 set_helper_retaddr(retaddr);
964 ret = cpu_ldsb_data(env, ptr);
965 clear_helper_retaddr();
966 return ret;
969 uint32_t cpu_lduw_be_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr)
971 uint32_t ret;
973 set_helper_retaddr(retaddr);
974 ret = cpu_lduw_be_data(env, ptr);
975 clear_helper_retaddr();
976 return ret;
979 int cpu_ldsw_be_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr)
981 int ret;
983 set_helper_retaddr(retaddr);
984 ret = cpu_ldsw_be_data(env, ptr);
985 clear_helper_retaddr();
986 return ret;
989 uint32_t cpu_ldl_be_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr)
991 uint32_t ret;
993 set_helper_retaddr(retaddr);
994 ret = cpu_ldl_be_data(env, ptr);
995 clear_helper_retaddr();
996 return ret;
999 uint64_t cpu_ldq_be_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr)
1001 uint64_t ret;
1003 set_helper_retaddr(retaddr);
1004 ret = cpu_ldq_be_data(env, ptr);
1005 clear_helper_retaddr();
1006 return ret;
1009 uint32_t cpu_lduw_le_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr)
1011 uint32_t ret;
1013 set_helper_retaddr(retaddr);
1014 ret = cpu_lduw_le_data(env, ptr);
1015 clear_helper_retaddr();
1016 return ret;
1019 int cpu_ldsw_le_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr)
1021 int ret;
1023 set_helper_retaddr(retaddr);
1024 ret = cpu_ldsw_le_data(env, ptr);
1025 clear_helper_retaddr();
1026 return ret;
1029 uint32_t cpu_ldl_le_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr)
1031 uint32_t ret;
1033 set_helper_retaddr(retaddr);
1034 ret = cpu_ldl_le_data(env, ptr);
1035 clear_helper_retaddr();
1036 return ret;
1039 uint64_t cpu_ldq_le_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr)
1041 uint64_t ret;
1043 set_helper_retaddr(retaddr);
1044 ret = cpu_ldq_le_data(env, ptr);
1045 clear_helper_retaddr();
1046 return ret;
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)
1170 uint32_t ret;
1172 set_helper_retaddr(1);
1173 ret = ldub_p(g2h(ptr));
1174 clear_helper_retaddr();
1175 return ret;
1178 uint32_t cpu_lduw_code(CPUArchState *env, abi_ptr ptr)
1180 uint32_t ret;
1182 set_helper_retaddr(1);
1183 ret = lduw_p(g2h(ptr));
1184 clear_helper_retaddr();
1185 return ret;
1188 uint32_t cpu_ldl_code(CPUArchState *env, abi_ptr ptr)
1190 uint32_t ret;
1192 set_helper_retaddr(1);
1193 ret = ldl_p(g2h(ptr));
1194 clear_helper_retaddr();
1195 return ret;
1198 uint64_t cpu_ldq_code(CPUArchState *env, abi_ptr ptr)
1200 uint64_t ret;
1202 set_helper_retaddr(1);
1203 ret = ldq_p(g2h(ptr));
1204 clear_helper_retaddr();
1205 return ret;
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);
1218 return ret;
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))
1228 #define EXTRA_ARGS
1230 #include "atomic_common.c.inc"
1232 #define DATA_SIZE 1
1233 #include "atomic_template.h"
1235 #define DATA_SIZE 2
1236 #include "atomic_template.h"
1238 #define DATA_SIZE 4
1239 #include "atomic_template.h"
1241 #ifdef CONFIG_ATOMIC64
1242 #define DATA_SIZE 8
1243 #include "atomic_template.h"
1244 #endif
1246 /* The following is only callable from other helpers, and matches up
1247 with the softmmu version. */
1249 #if HAVE_ATOMIC128 || HAVE_CMPXCHG128
1251 #undef EXTRA_ARGS
1252 #undef ATOMIC_NAME
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"
1262 #endif