142a623c7SBlue Swirl /* 242a623c7SBlue Swirl * User emulator execution 342a623c7SBlue Swirl * 442a623c7SBlue Swirl * Copyright (c) 2003-2005 Fabrice Bellard 542a623c7SBlue Swirl * 642a623c7SBlue Swirl * This library is free software; you can redistribute it and/or 742a623c7SBlue Swirl * modify it under the terms of the GNU Lesser General Public 842a623c7SBlue Swirl * License as published by the Free Software Foundation; either 9fb0343d5SThomas Huth * version 2.1 of the License, or (at your option) any later version. 1042a623c7SBlue Swirl * 1142a623c7SBlue Swirl * This library is distributed in the hope that it will be useful, 1242a623c7SBlue Swirl * but WITHOUT ANY WARRANTY; without even the implied warranty of 1342a623c7SBlue Swirl * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1442a623c7SBlue Swirl * Lesser General Public License for more details. 1542a623c7SBlue Swirl * 1642a623c7SBlue Swirl * You should have received a copy of the GNU Lesser General Public 1742a623c7SBlue Swirl * License along with this library; if not, see <http://www.gnu.org/licenses/>. 1842a623c7SBlue Swirl */ 19d38ea87aSPeter Maydell #include "qemu/osdep.h" 2078271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h" 2176cad711SPaolo Bonzini #include "disas/disas.h" 2263c91552SPaolo Bonzini #include "exec/exec-all.h" 23dcb32f1dSPhilippe Mathieu-Daudé #include "tcg/tcg.h" 24023b0ae3SPeter Maydell #include "qemu/bitops.h" 25f08b6170SPaolo Bonzini #include "exec/cpu_ldst.h" 263b9bd3f4SPaolo Bonzini #include "exec/translate-all.h" 27a411d296SPhilippe Mathieu-Daudé #include "exec/helper-proto.h" 28e6cd4bb5SRichard Henderson #include "qemu/atomic128.h" 29243af022SPaolo Bonzini #include "trace/trace-root.h" 300583f775SRichard Henderson #include "internal.h" 3142a623c7SBlue Swirl 3242a623c7SBlue Swirl #undef EAX 3342a623c7SBlue Swirl #undef ECX 3442a623c7SBlue Swirl #undef EDX 3542a623c7SBlue Swirl #undef EBX 3642a623c7SBlue Swirl #undef ESP 3742a623c7SBlue Swirl #undef EBP 3842a623c7SBlue Swirl #undef ESI 3942a623c7SBlue Swirl #undef EDI 4042a623c7SBlue Swirl #undef EIP 4142a623c7SBlue Swirl #ifdef __linux__ 4242a623c7SBlue Swirl #include <sys/ucontext.h> 4342a623c7SBlue Swirl #endif 4442a623c7SBlue Swirl 45ec603b55SRichard Henderson __thread uintptr_t helper_retaddr; 46ec603b55SRichard Henderson 4742a623c7SBlue Swirl //#define DEBUG_SIGNAL 4842a623c7SBlue Swirl 490fdbb7d2SRichard Henderson /* 500fdbb7d2SRichard Henderson * Adjust the pc to pass to cpu_restore_state; return the memop type. 510fdbb7d2SRichard Henderson */ 520fdbb7d2SRichard Henderson MMUAccessType adjust_signal_pc(uintptr_t *pc, bool is_write) 5342a623c7SBlue Swirl { 5452ba13f0SRichard Henderson switch (helper_retaddr) { 5552ba13f0SRichard Henderson default: 5652ba13f0SRichard Henderson /* 5752ba13f0SRichard Henderson * Fault during host memory operation within a helper function. 5852ba13f0SRichard Henderson * The helper's host return address, saved here, gives us a 5952ba13f0SRichard Henderson * pointer into the generated code that will unwind to the 6052ba13f0SRichard Henderson * correct guest pc. 61ec603b55SRichard Henderson */ 620fdbb7d2SRichard Henderson *pc = helper_retaddr; 6352ba13f0SRichard Henderson break; 6452ba13f0SRichard Henderson 6552ba13f0SRichard Henderson case 0: 6652ba13f0SRichard Henderson /* 6752ba13f0SRichard Henderson * Fault during host memory operation within generated code. 6852ba13f0SRichard Henderson * (Or, a unrelated bug within qemu, but we can't tell from here). 6952ba13f0SRichard Henderson * 7052ba13f0SRichard Henderson * We take the host pc from the signal frame. However, we cannot 7152ba13f0SRichard Henderson * use that value directly. Within cpu_restore_state_from_tb, we 7252ba13f0SRichard Henderson * assume PC comes from GETPC(), as used by the helper functions, 7352ba13f0SRichard Henderson * so we adjust the address by -GETPC_ADJ to form an address that 74e3a6e0daSzhaolichang * is within the call insn, so that the address does not accidentally 7552ba13f0SRichard Henderson * match the beginning of the next guest insn. However, when the 7652ba13f0SRichard Henderson * pc comes from the signal frame it points to the actual faulting 7752ba13f0SRichard Henderson * host memory insn and not the return from a call insn. 7852ba13f0SRichard Henderson * 7952ba13f0SRichard Henderson * Therefore, adjust to compensate for what will be done later 8052ba13f0SRichard Henderson * by cpu_restore_state_from_tb. 8152ba13f0SRichard Henderson */ 820fdbb7d2SRichard Henderson *pc += GETPC_ADJ; 8352ba13f0SRichard Henderson break; 8452ba13f0SRichard Henderson 8552ba13f0SRichard Henderson case 1: 8652ba13f0SRichard Henderson /* 8752ba13f0SRichard Henderson * Fault during host read for translation, or loosely, "execution". 8852ba13f0SRichard Henderson * 8952ba13f0SRichard Henderson * The guest pc is already pointing to the start of the TB for which 9052ba13f0SRichard Henderson * code is being generated. If the guest translator manages the 9152ba13f0SRichard Henderson * page crossings correctly, this is exactly the correct address 9252ba13f0SRichard Henderson * (and if the translator doesn't handle page boundaries correctly 9352ba13f0SRichard Henderson * there's little we can do about that here). Therefore, do not 9452ba13f0SRichard Henderson * trigger the unwinder. 9552ba13f0SRichard Henderson * 9652ba13f0SRichard Henderson * Like tb_gen_code, release the memory lock before cpu_loop_exit. 9752ba13f0SRichard Henderson */ 9852ba13f0SRichard Henderson mmap_unlock(); 990fdbb7d2SRichard Henderson *pc = 0; 1000fdbb7d2SRichard Henderson return MMU_INST_FETCH; 101ec603b55SRichard Henderson } 102ec603b55SRichard Henderson 1030fdbb7d2SRichard Henderson return is_write ? MMU_DATA_STORE : MMU_DATA_LOAD; 1040fdbb7d2SRichard Henderson } 1050fdbb7d2SRichard Henderson 1065e38ba7dSRichard Henderson /** 1075e38ba7dSRichard Henderson * handle_sigsegv_accerr_write: 1085e38ba7dSRichard Henderson * @cpu: the cpu context 1095e38ba7dSRichard Henderson * @old_set: the sigset_t from the signal ucontext_t 1105e38ba7dSRichard Henderson * @host_pc: the host pc, adjusted for the signal 1115e38ba7dSRichard Henderson * @guest_addr: the guest address of the fault 1125e38ba7dSRichard Henderson * 1135e38ba7dSRichard Henderson * Return true if the write fault has been handled, and should be re-tried. 1145e38ba7dSRichard Henderson * 1155e38ba7dSRichard Henderson * Note that it is important that we don't call page_unprotect() unless 1165e38ba7dSRichard Henderson * this is really a "write to nonwriteable page" fault, because 1175e38ba7dSRichard Henderson * page_unprotect() assumes that if it is called for an access to 1185e38ba7dSRichard Henderson * a page that's writeable this means we had two threads racing and 1195e38ba7dSRichard Henderson * another thread got there first and already made the page writeable; 1205e38ba7dSRichard Henderson * so we will retry the access. If we were to call page_unprotect() 1215e38ba7dSRichard Henderson * for some other kind of fault that should really be passed to the 1225e38ba7dSRichard Henderson * guest, we'd end up in an infinite loop of retrying the faulting access. 1235e38ba7dSRichard Henderson */ 1245e38ba7dSRichard Henderson bool handle_sigsegv_accerr_write(CPUState *cpu, sigset_t *old_set, 1255e38ba7dSRichard Henderson uintptr_t host_pc, abi_ptr guest_addr) 1265e38ba7dSRichard Henderson { 1275e38ba7dSRichard Henderson switch (page_unprotect(guest_addr, host_pc)) { 1285e38ba7dSRichard Henderson case 0: 1295e38ba7dSRichard Henderson /* 1305e38ba7dSRichard Henderson * Fault not caused by a page marked unwritable to protect 1315e38ba7dSRichard Henderson * cached translations, must be the guest binary's problem. 1325e38ba7dSRichard Henderson */ 1335e38ba7dSRichard Henderson return false; 1345e38ba7dSRichard Henderson case 1: 1355e38ba7dSRichard Henderson /* 1365e38ba7dSRichard Henderson * Fault caused by protection of cached translation; TBs 1375e38ba7dSRichard Henderson * invalidated, so resume execution. 1385e38ba7dSRichard Henderson */ 1395e38ba7dSRichard Henderson return true; 1405e38ba7dSRichard Henderson case 2: 1415e38ba7dSRichard Henderson /* 1425e38ba7dSRichard Henderson * Fault caused by protection of cached translation, and the 1435e38ba7dSRichard Henderson * currently executing TB was modified and must be exited immediately. 1445e38ba7dSRichard Henderson */ 145*940b3090SRichard Henderson sigprocmask(SIG_SETMASK, old_set, NULL); 146*940b3090SRichard Henderson cpu_loop_exit_noexc(cpu); 1475e38ba7dSRichard Henderson /* NORETURN */ 1485e38ba7dSRichard Henderson default: 1495e38ba7dSRichard Henderson g_assert_not_reached(); 1505e38ba7dSRichard Henderson } 1515e38ba7dSRichard Henderson } 1525e38ba7dSRichard Henderson 1530fdbb7d2SRichard Henderson /* 1540fdbb7d2SRichard Henderson * 'pc' is the host PC at which the exception was raised. 1550fdbb7d2SRichard Henderson * 'address' is the effective address of the memory exception. 1560fdbb7d2SRichard Henderson * 'is_write' is 1 if a write caused the exception and otherwise 0. 1570fdbb7d2SRichard Henderson * 'old_set' is the signal set which should be restored. 1580fdbb7d2SRichard Henderson */ 1590fdbb7d2SRichard Henderson static inline int handle_cpu_signal(uintptr_t pc, siginfo_t *info, 1600fdbb7d2SRichard Henderson int is_write, sigset_t *old_set) 1610fdbb7d2SRichard Henderson { 1620fdbb7d2SRichard Henderson CPUState *cpu = current_cpu; 1630fdbb7d2SRichard Henderson CPUClass *cc; 1645e38ba7dSRichard Henderson unsigned long host_addr = (unsigned long)info->si_addr; 1650fdbb7d2SRichard Henderson MMUAccessType access_type = adjust_signal_pc(&pc, is_write); 1665e38ba7dSRichard Henderson abi_ptr guest_addr; 1670fdbb7d2SRichard Henderson 16802bed6bdSAlex Bennée /* For synchronous signals we expect to be coming from the vCPU 16902bed6bdSAlex Bennée * thread (so current_cpu should be valid) and either from running 17002bed6bdSAlex Bennée * code or during translation which can fault as we cross pages. 17102bed6bdSAlex Bennée * 17202bed6bdSAlex Bennée * If neither is true then something has gone wrong and we should 17302bed6bdSAlex Bennée * abort rather than try and restart the vCPU execution. 17402bed6bdSAlex Bennée */ 17502bed6bdSAlex Bennée if (!cpu || !cpu->running) { 17602bed6bdSAlex Bennée printf("qemu:%s received signal outside vCPU context @ pc=0x%" 17702bed6bdSAlex Bennée PRIxPTR "\n", __func__, pc); 17802bed6bdSAlex Bennée abort(); 17902bed6bdSAlex Bennée } 18002bed6bdSAlex Bennée 18142a623c7SBlue Swirl #if defined(DEBUG_SIGNAL) 18271baf787SPeter Maydell printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 1835e38ba7dSRichard Henderson pc, host_addr, is_write, *(unsigned long *)old_set); 18442a623c7SBlue Swirl #endif 18542a623c7SBlue Swirl 186732f9e89SAlexander Graf /* Convert forcefully to guest address space, invalid addresses 187732f9e89SAlexander Graf are still valid segv ones */ 1885e38ba7dSRichard Henderson guest_addr = h2g_nocheck(host_addr); 1895e38ba7dSRichard Henderson 1905e38ba7dSRichard Henderson /* XXX: locking issue */ 1915e38ba7dSRichard Henderson if (is_write && 1925e38ba7dSRichard Henderson info->si_signo == SIGSEGV && 1935e38ba7dSRichard Henderson info->si_code == SEGV_ACCERR && 1945e38ba7dSRichard Henderson h2g_valid(host_addr) && 1955e38ba7dSRichard Henderson handle_sigsegv_accerr_write(cpu, old_set, pc, guest_addr)) { 1965e38ba7dSRichard Henderson return 1; 1975e38ba7dSRichard Henderson } 198732f9e89SAlexander Graf 199da6bbf85SRichard Henderson /* 200da6bbf85SRichard Henderson * There is no way the target can handle this other than raising 201da6bbf85SRichard Henderson * an exception. Undo signal and retaddr state prior to longjmp. 202ec603b55SRichard Henderson */ 203da6bbf85SRichard Henderson sigprocmask(SIG_SETMASK, old_set, NULL); 204ec603b55SRichard Henderson 205da6bbf85SRichard Henderson cc = CPU_GET_CLASS(cpu); 2065e38ba7dSRichard Henderson cc->tcg_ops->tlb_fill(cpu, guest_addr, 0, access_type, 207c73bdb35SClaudio Fontana MMU_USER_IDX, false, pc); 208da6bbf85SRichard Henderson g_assert_not_reached(); 20942a623c7SBlue Swirl } 21042a623c7SBlue Swirl 211069cfe77SRichard Henderson static int probe_access_internal(CPUArchState *env, target_ulong addr, 212069cfe77SRichard Henderson int fault_size, MMUAccessType access_type, 213069cfe77SRichard Henderson bool nonfault, uintptr_t ra) 21459e96ac6SDavid Hildenbrand { 215c25c283dSDavid Hildenbrand int flags; 216c25c283dSDavid Hildenbrand 217c25c283dSDavid Hildenbrand switch (access_type) { 218c25c283dSDavid Hildenbrand case MMU_DATA_STORE: 219c25c283dSDavid Hildenbrand flags = PAGE_WRITE; 220c25c283dSDavid Hildenbrand break; 221c25c283dSDavid Hildenbrand case MMU_DATA_LOAD: 222c25c283dSDavid Hildenbrand flags = PAGE_READ; 223c25c283dSDavid Hildenbrand break; 224c25c283dSDavid Hildenbrand case MMU_INST_FETCH: 225c25c283dSDavid Hildenbrand flags = PAGE_EXEC; 226c25c283dSDavid Hildenbrand break; 227c25c283dSDavid Hildenbrand default: 228c25c283dSDavid Hildenbrand g_assert_not_reached(); 229c25c283dSDavid Hildenbrand } 230c25c283dSDavid Hildenbrand 23146b12f46SRichard Henderson if (!guest_addr_valid_untagged(addr) || 23246b12f46SRichard Henderson page_check_range(addr, 1, flags) < 0) { 233069cfe77SRichard Henderson if (nonfault) { 234069cfe77SRichard Henderson return TLB_INVALID_MASK; 235069cfe77SRichard Henderson } else { 23659e96ac6SDavid Hildenbrand CPUState *cpu = env_cpu(env); 23759e96ac6SDavid Hildenbrand CPUClass *cc = CPU_GET_CLASS(cpu); 23878271684SClaudio Fontana cc->tcg_ops->tlb_fill(cpu, addr, fault_size, access_type, 239069cfe77SRichard Henderson MMU_USER_IDX, false, ra); 24059e96ac6SDavid Hildenbrand g_assert_not_reached(); 24159e96ac6SDavid Hildenbrand } 242069cfe77SRichard Henderson } 243069cfe77SRichard Henderson return 0; 244069cfe77SRichard Henderson } 245069cfe77SRichard Henderson 246069cfe77SRichard Henderson int probe_access_flags(CPUArchState *env, target_ulong addr, 247069cfe77SRichard Henderson MMUAccessType access_type, int mmu_idx, 248069cfe77SRichard Henderson bool nonfault, void **phost, uintptr_t ra) 249069cfe77SRichard Henderson { 250069cfe77SRichard Henderson int flags; 251069cfe77SRichard Henderson 252069cfe77SRichard Henderson flags = probe_access_internal(env, addr, 0, access_type, nonfault, ra); 2533e8f1628SRichard Henderson *phost = flags ? NULL : g2h(env_cpu(env), addr); 254069cfe77SRichard Henderson return flags; 255069cfe77SRichard Henderson } 256069cfe77SRichard Henderson 257069cfe77SRichard Henderson void *probe_access(CPUArchState *env, target_ulong addr, int size, 258069cfe77SRichard Henderson MMUAccessType access_type, int mmu_idx, uintptr_t ra) 259069cfe77SRichard Henderson { 260069cfe77SRichard Henderson int flags; 261069cfe77SRichard Henderson 262069cfe77SRichard Henderson g_assert(-(addr | TARGET_PAGE_MASK) >= size); 263069cfe77SRichard Henderson flags = probe_access_internal(env, addr, size, access_type, false, ra); 264069cfe77SRichard Henderson g_assert(flags == 0); 265fef39ccdSDavid Hildenbrand 2663e8f1628SRichard Henderson return size ? g2h(env_cpu(env), addr) : NULL; 26759e96ac6SDavid Hildenbrand } 26859e96ac6SDavid Hildenbrand 26942a623c7SBlue Swirl #if defined(__i386__) 27042a623c7SBlue Swirl 271c5679026SPeter Maydell #if defined(__NetBSD__) 27242a623c7SBlue Swirl #include <ucontext.h> 2734f862f79SWarner Losh #include <machine/trap.h> 27442a623c7SBlue Swirl 27542a623c7SBlue Swirl #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP]) 27642a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO]) 27742a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR]) 27842a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 2794f862f79SWarner Losh #define PAGE_FAULT_TRAP T_PAGEFLT 28042a623c7SBlue Swirl #elif defined(__FreeBSD__) || defined(__DragonFly__) 28142a623c7SBlue Swirl #include <ucontext.h> 2824f862f79SWarner Losh #include <machine/trap.h> 28342a623c7SBlue Swirl 28442a623c7SBlue Swirl #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip)) 28542a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno) 28642a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.mc_err) 28742a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 2884f862f79SWarner Losh #define PAGE_FAULT_TRAP T_PAGEFLT 28942a623c7SBlue Swirl #elif defined(__OpenBSD__) 2904f862f79SWarner Losh #include <machine/trap.h> 29142a623c7SBlue Swirl #define EIP_sig(context) ((context)->sc_eip) 29242a623c7SBlue Swirl #define TRAP_sig(context) ((context)->sc_trapno) 29342a623c7SBlue Swirl #define ERROR_sig(context) ((context)->sc_err) 29442a623c7SBlue Swirl #define MASK_sig(context) ((context)->sc_mask) 2954f862f79SWarner Losh #define PAGE_FAULT_TRAP T_PAGEFLT 29642a623c7SBlue Swirl #else 29742a623c7SBlue Swirl #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP]) 29842a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO]) 29942a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR]) 30042a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 3014f862f79SWarner Losh #define PAGE_FAULT_TRAP 0xe 30242a623c7SBlue Swirl #endif 30342a623c7SBlue Swirl 30442a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 30542a623c7SBlue Swirl void *puc) 30642a623c7SBlue Swirl { 30742a623c7SBlue Swirl siginfo_t *info = pinfo; 30842a623c7SBlue Swirl #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) 30942a623c7SBlue Swirl ucontext_t *uc = puc; 31042a623c7SBlue Swirl #elif defined(__OpenBSD__) 31142a623c7SBlue Swirl struct sigcontext *uc = puc; 31242a623c7SBlue Swirl #else 31304b33e21SKhem Raj ucontext_t *uc = puc; 31442a623c7SBlue Swirl #endif 31542a623c7SBlue Swirl unsigned long pc; 31642a623c7SBlue Swirl int trapno; 31742a623c7SBlue Swirl 31842a623c7SBlue Swirl #ifndef REG_EIP 31942a623c7SBlue Swirl /* for glibc 2.1 */ 32042a623c7SBlue Swirl #define REG_EIP EIP 32142a623c7SBlue Swirl #define REG_ERR ERR 32242a623c7SBlue Swirl #define REG_TRAPNO TRAPNO 32342a623c7SBlue Swirl #endif 32442a623c7SBlue Swirl pc = EIP_sig(uc); 32542a623c7SBlue Swirl trapno = TRAP_sig(uc); 326a78b1299SPeter Maydell return handle_cpu_signal(pc, info, 3274f862f79SWarner Losh trapno == PAGE_FAULT_TRAP ? 3284f862f79SWarner Losh (ERROR_sig(uc) >> 1) & 1 : 0, 329a5852dc5SPeter Maydell &MASK_sig(uc)); 33042a623c7SBlue Swirl } 33142a623c7SBlue Swirl 33242a623c7SBlue Swirl #elif defined(__x86_64__) 33342a623c7SBlue Swirl 33442a623c7SBlue Swirl #ifdef __NetBSD__ 3354f862f79SWarner Losh #include <machine/trap.h> 33642a623c7SBlue Swirl #define PC_sig(context) _UC_MACHINE_PC(context) 33742a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO]) 33842a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR]) 33942a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 3404f862f79SWarner Losh #define PAGE_FAULT_TRAP T_PAGEFLT 34142a623c7SBlue Swirl #elif defined(__OpenBSD__) 3424f862f79SWarner Losh #include <machine/trap.h> 34342a623c7SBlue Swirl #define PC_sig(context) ((context)->sc_rip) 34442a623c7SBlue Swirl #define TRAP_sig(context) ((context)->sc_trapno) 34542a623c7SBlue Swirl #define ERROR_sig(context) ((context)->sc_err) 34642a623c7SBlue Swirl #define MASK_sig(context) ((context)->sc_mask) 3474f862f79SWarner Losh #define PAGE_FAULT_TRAP T_PAGEFLT 34842a623c7SBlue Swirl #elif defined(__FreeBSD__) || defined(__DragonFly__) 34942a623c7SBlue Swirl #include <ucontext.h> 3504f862f79SWarner Losh #include <machine/trap.h> 35142a623c7SBlue Swirl 35242a623c7SBlue Swirl #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip)) 35342a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno) 35442a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.mc_err) 35542a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 3564f862f79SWarner Losh #define PAGE_FAULT_TRAP T_PAGEFLT 35742a623c7SBlue Swirl #else 35842a623c7SBlue Swirl #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP]) 35942a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO]) 36042a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR]) 36142a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 3624f862f79SWarner Losh #define PAGE_FAULT_TRAP 0xe 36342a623c7SBlue Swirl #endif 36442a623c7SBlue Swirl 36542a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 36642a623c7SBlue Swirl void *puc) 36742a623c7SBlue Swirl { 36842a623c7SBlue Swirl siginfo_t *info = pinfo; 36942a623c7SBlue Swirl unsigned long pc; 37042a623c7SBlue Swirl #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) 37142a623c7SBlue Swirl ucontext_t *uc = puc; 37242a623c7SBlue Swirl #elif defined(__OpenBSD__) 37342a623c7SBlue Swirl struct sigcontext *uc = puc; 37442a623c7SBlue Swirl #else 37504b33e21SKhem Raj ucontext_t *uc = puc; 37642a623c7SBlue Swirl #endif 37742a623c7SBlue Swirl 37842a623c7SBlue Swirl pc = PC_sig(uc); 379a78b1299SPeter Maydell return handle_cpu_signal(pc, info, 3804f862f79SWarner Losh TRAP_sig(uc) == PAGE_FAULT_TRAP ? 3814f862f79SWarner Losh (ERROR_sig(uc) >> 1) & 1 : 0, 382a5852dc5SPeter Maydell &MASK_sig(uc)); 38342a623c7SBlue Swirl } 38442a623c7SBlue Swirl 38542a623c7SBlue Swirl #elif defined(_ARCH_PPC) 38642a623c7SBlue Swirl 38742a623c7SBlue Swirl /*********************************************************************** 38842a623c7SBlue Swirl * signal context platform-specific definitions 38942a623c7SBlue Swirl * From Wine 39042a623c7SBlue Swirl */ 39142a623c7SBlue Swirl #ifdef linux 39242a623c7SBlue Swirl /* All Registers access - only for local access */ 39342a623c7SBlue Swirl #define REG_sig(reg_name, context) \ 39442a623c7SBlue Swirl ((context)->uc_mcontext.regs->reg_name) 39542a623c7SBlue Swirl /* Gpr Registers access */ 39642a623c7SBlue Swirl #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context) 39742a623c7SBlue Swirl /* Program counter */ 39842a623c7SBlue Swirl #define IAR_sig(context) REG_sig(nip, context) 39942a623c7SBlue Swirl /* Machine State Register (Supervisor) */ 40042a623c7SBlue Swirl #define MSR_sig(context) REG_sig(msr, context) 40142a623c7SBlue Swirl /* Count register */ 40242a623c7SBlue Swirl #define CTR_sig(context) REG_sig(ctr, context) 40342a623c7SBlue Swirl /* User's integer exception register */ 40442a623c7SBlue Swirl #define XER_sig(context) REG_sig(xer, context) 40542a623c7SBlue Swirl /* Link register */ 40642a623c7SBlue Swirl #define LR_sig(context) REG_sig(link, context) 40742a623c7SBlue Swirl /* Condition register */ 40842a623c7SBlue Swirl #define CR_sig(context) REG_sig(ccr, context) 40942a623c7SBlue Swirl 41042a623c7SBlue Swirl /* Float Registers access */ 41142a623c7SBlue Swirl #define FLOAT_sig(reg_num, context) \ 41242a623c7SBlue Swirl (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num]) 41342a623c7SBlue Swirl #define FPSCR_sig(context) \ 41442a623c7SBlue Swirl (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4))) 41542a623c7SBlue Swirl /* Exception Registers access */ 41642a623c7SBlue Swirl #define DAR_sig(context) REG_sig(dar, context) 41742a623c7SBlue Swirl #define DSISR_sig(context) REG_sig(dsisr, context) 41842a623c7SBlue Swirl #define TRAP_sig(context) REG_sig(trap, context) 41942a623c7SBlue Swirl #endif /* linux */ 42042a623c7SBlue Swirl 42142a623c7SBlue Swirl #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 42242a623c7SBlue Swirl #include <ucontext.h> 42342a623c7SBlue Swirl #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0) 42442a623c7SBlue Swirl #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1) 42542a623c7SBlue Swirl #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr) 42642a623c7SBlue Swirl #define XER_sig(context) ((context)->uc_mcontext.mc_xer) 42742a623c7SBlue Swirl #define LR_sig(context) ((context)->uc_mcontext.mc_lr) 42842a623c7SBlue Swirl #define CR_sig(context) ((context)->uc_mcontext.mc_cr) 42942a623c7SBlue Swirl /* Exception Registers access */ 43042a623c7SBlue Swirl #define DAR_sig(context) ((context)->uc_mcontext.mc_dar) 43142a623c7SBlue Swirl #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr) 43242a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc) 43342a623c7SBlue Swirl #endif /* __FreeBSD__|| __FreeBSD_kernel__ */ 43442a623c7SBlue Swirl 43542a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 43642a623c7SBlue Swirl void *puc) 43742a623c7SBlue Swirl { 43842a623c7SBlue Swirl siginfo_t *info = pinfo; 43942a623c7SBlue Swirl #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 44042a623c7SBlue Swirl ucontext_t *uc = puc; 44142a623c7SBlue Swirl #else 44204b33e21SKhem Raj ucontext_t *uc = puc; 44342a623c7SBlue Swirl #endif 44442a623c7SBlue Swirl unsigned long pc; 44542a623c7SBlue Swirl int is_write; 44642a623c7SBlue Swirl 44742a623c7SBlue Swirl pc = IAR_sig(uc); 44842a623c7SBlue Swirl is_write = 0; 44942a623c7SBlue Swirl #if 0 45042a623c7SBlue Swirl /* ppc 4xx case */ 45142a623c7SBlue Swirl if (DSISR_sig(uc) & 0x00800000) { 45242a623c7SBlue Swirl is_write = 1; 45342a623c7SBlue Swirl } 45442a623c7SBlue Swirl #else 45542a623c7SBlue Swirl if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) { 45642a623c7SBlue Swirl is_write = 1; 45742a623c7SBlue Swirl } 45842a623c7SBlue Swirl #endif 459a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 46042a623c7SBlue Swirl } 46142a623c7SBlue Swirl 46242a623c7SBlue Swirl #elif defined(__alpha__) 46342a623c7SBlue Swirl 46442a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 46542a623c7SBlue Swirl void *puc) 46642a623c7SBlue Swirl { 46742a623c7SBlue Swirl siginfo_t *info = pinfo; 46804b33e21SKhem Raj ucontext_t *uc = puc; 46942a623c7SBlue Swirl uint32_t *pc = uc->uc_mcontext.sc_pc; 47042a623c7SBlue Swirl uint32_t insn = *pc; 47142a623c7SBlue Swirl int is_write = 0; 47242a623c7SBlue Swirl 47342a623c7SBlue Swirl /* XXX: need kernel patch to get write flag faster */ 47442a623c7SBlue Swirl switch (insn >> 26) { 47542a623c7SBlue Swirl case 0x0d: /* stw */ 47642a623c7SBlue Swirl case 0x0e: /* stb */ 47742a623c7SBlue Swirl case 0x0f: /* stq_u */ 47842a623c7SBlue Swirl case 0x24: /* stf */ 47942a623c7SBlue Swirl case 0x25: /* stg */ 48042a623c7SBlue Swirl case 0x26: /* sts */ 48142a623c7SBlue Swirl case 0x27: /* stt */ 48242a623c7SBlue Swirl case 0x2c: /* stl */ 48342a623c7SBlue Swirl case 0x2d: /* stq */ 48442a623c7SBlue Swirl case 0x2e: /* stl_c */ 48542a623c7SBlue Swirl case 0x2f: /* stq_c */ 48642a623c7SBlue Swirl is_write = 1; 48742a623c7SBlue Swirl } 48842a623c7SBlue Swirl 489a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 49042a623c7SBlue Swirl } 49142a623c7SBlue Swirl #elif defined(__sparc__) 49242a623c7SBlue Swirl 49342a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 49442a623c7SBlue Swirl void *puc) 49542a623c7SBlue Swirl { 49642a623c7SBlue Swirl siginfo_t *info = pinfo; 49742a623c7SBlue Swirl int is_write; 49842a623c7SBlue Swirl uint32_t insn; 49942a623c7SBlue Swirl #if !defined(__arch64__) || defined(CONFIG_SOLARIS) 50042a623c7SBlue Swirl uint32_t *regs = (uint32_t *)(info + 1); 50142a623c7SBlue Swirl void *sigmask = (regs + 20); 50242a623c7SBlue Swirl /* XXX: is there a standard glibc define ? */ 50342a623c7SBlue Swirl unsigned long pc = regs[1]; 50442a623c7SBlue Swirl #else 50542a623c7SBlue Swirl #ifdef __linux__ 50642a623c7SBlue Swirl struct sigcontext *sc = puc; 50742a623c7SBlue Swirl unsigned long pc = sc->sigc_regs.tpc; 50842a623c7SBlue Swirl void *sigmask = (void *)sc->sigc_mask; 50942a623c7SBlue Swirl #elif defined(__OpenBSD__) 51042a623c7SBlue Swirl struct sigcontext *uc = puc; 51142a623c7SBlue Swirl unsigned long pc = uc->sc_pc; 51242a623c7SBlue Swirl void *sigmask = (void *)(long)uc->sc_mask; 5137ccfb495STobias Nygren #elif defined(__NetBSD__) 5147ccfb495STobias Nygren ucontext_t *uc = puc; 5157ccfb495STobias Nygren unsigned long pc = _UC_MACHINE_PC(uc); 5167ccfb495STobias Nygren void *sigmask = (void *)&uc->uc_sigmask; 51742a623c7SBlue Swirl #endif 51842a623c7SBlue Swirl #endif 51942a623c7SBlue Swirl 52042a623c7SBlue Swirl /* XXX: need kernel patch to get write flag faster */ 52142a623c7SBlue Swirl is_write = 0; 52242a623c7SBlue Swirl insn = *(uint32_t *)pc; 52342a623c7SBlue Swirl if ((insn >> 30) == 3) { 52442a623c7SBlue Swirl switch ((insn >> 19) & 0x3f) { 52542a623c7SBlue Swirl case 0x05: /* stb */ 52642a623c7SBlue Swirl case 0x15: /* stba */ 52742a623c7SBlue Swirl case 0x06: /* sth */ 52842a623c7SBlue Swirl case 0x16: /* stha */ 52942a623c7SBlue Swirl case 0x04: /* st */ 53042a623c7SBlue Swirl case 0x14: /* sta */ 53142a623c7SBlue Swirl case 0x07: /* std */ 53242a623c7SBlue Swirl case 0x17: /* stda */ 53342a623c7SBlue Swirl case 0x0e: /* stx */ 53442a623c7SBlue Swirl case 0x1e: /* stxa */ 53542a623c7SBlue Swirl case 0x24: /* stf */ 53642a623c7SBlue Swirl case 0x34: /* stfa */ 53742a623c7SBlue Swirl case 0x27: /* stdf */ 53842a623c7SBlue Swirl case 0x37: /* stdfa */ 53942a623c7SBlue Swirl case 0x26: /* stqf */ 54042a623c7SBlue Swirl case 0x36: /* stqfa */ 54142a623c7SBlue Swirl case 0x25: /* stfsr */ 54242a623c7SBlue Swirl case 0x3c: /* casa */ 54342a623c7SBlue Swirl case 0x3e: /* casxa */ 54442a623c7SBlue Swirl is_write = 1; 54542a623c7SBlue Swirl break; 54642a623c7SBlue Swirl } 54742a623c7SBlue Swirl } 548a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, sigmask); 54942a623c7SBlue Swirl } 55042a623c7SBlue Swirl 55142a623c7SBlue Swirl #elif defined(__arm__) 55242a623c7SBlue Swirl 5537ccfb495STobias Nygren #if defined(__NetBSD__) 5547ccfb495STobias Nygren #include <ucontext.h> 555853d9a4bSNick Hudson #include <sys/siginfo.h> 5567ccfb495STobias Nygren #endif 5577ccfb495STobias Nygren 55842a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 55942a623c7SBlue Swirl void *puc) 56042a623c7SBlue Swirl { 56142a623c7SBlue Swirl siginfo_t *info = pinfo; 5627ccfb495STobias Nygren #if defined(__NetBSD__) 5637ccfb495STobias Nygren ucontext_t *uc = puc; 564853d9a4bSNick Hudson siginfo_t *si = pinfo; 5657ccfb495STobias Nygren #else 56604b33e21SKhem Raj ucontext_t *uc = puc; 5677ccfb495STobias Nygren #endif 56842a623c7SBlue Swirl unsigned long pc; 569853d9a4bSNick Hudson uint32_t fsr; 57042a623c7SBlue Swirl int is_write; 57142a623c7SBlue Swirl 5727ccfb495STobias Nygren #if defined(__NetBSD__) 5737ccfb495STobias Nygren pc = uc->uc_mcontext.__gregs[_REG_R15]; 5747ccfb495STobias Nygren #elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) 57542a623c7SBlue Swirl pc = uc->uc_mcontext.gregs[R15]; 57642a623c7SBlue Swirl #else 57742a623c7SBlue Swirl pc = uc->uc_mcontext.arm_pc; 57842a623c7SBlue Swirl #endif 579023b0ae3SPeter Maydell 580853d9a4bSNick Hudson #ifdef __NetBSD__ 581853d9a4bSNick Hudson fsr = si->si_trap; 582853d9a4bSNick Hudson #else 583853d9a4bSNick Hudson fsr = uc->uc_mcontext.error_code; 584853d9a4bSNick Hudson #endif 585853d9a4bSNick Hudson /* 586853d9a4bSNick Hudson * In the FSR, bit 11 is WnR, assuming a v6 or 587853d9a4bSNick Hudson * later processor. On v5 we will always report 588853d9a4bSNick Hudson * this as a read, which will fail later. 589023b0ae3SPeter Maydell */ 590853d9a4bSNick Hudson is_write = extract32(fsr, 11, 1); 591a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 59242a623c7SBlue Swirl } 59342a623c7SBlue Swirl 594f129061cSClaudio Fontana #elif defined(__aarch64__) 595f129061cSClaudio Fontana 59671b04329SNick Hudson #if defined(__NetBSD__) 59771b04329SNick Hudson 59871b04329SNick Hudson #include <ucontext.h> 59971b04329SNick Hudson #include <sys/siginfo.h> 60071b04329SNick Hudson 60171b04329SNick Hudson int cpu_signal_handler(int host_signum, void *pinfo, void *puc) 60271b04329SNick Hudson { 60371b04329SNick Hudson ucontext_t *uc = puc; 60471b04329SNick Hudson siginfo_t *si = pinfo; 60571b04329SNick Hudson unsigned long pc; 60671b04329SNick Hudson int is_write; 60771b04329SNick Hudson uint32_t esr; 60871b04329SNick Hudson 60971b04329SNick Hudson pc = uc->uc_mcontext.__gregs[_REG_PC]; 61071b04329SNick Hudson esr = si->si_trap; 61171b04329SNick Hudson 61271b04329SNick Hudson /* 61371b04329SNick Hudson * siginfo_t::si_trap is the ESR value, for data aborts ESR.EC 61471b04329SNick Hudson * is 0b10010x: then bit 6 is the WnR bit 61571b04329SNick Hudson */ 61671b04329SNick Hudson is_write = extract32(esr, 27, 5) == 0x12 && extract32(esr, 6, 1) == 1; 61771b04329SNick Hudson return handle_cpu_signal(pc, si, is_write, &uc->uc_sigmask); 61871b04329SNick Hudson } 61971b04329SNick Hudson 62071b04329SNick Hudson #else 62171b04329SNick Hudson 622f454a54fSPeter Maydell #ifndef ESR_MAGIC 623f454a54fSPeter Maydell /* Pre-3.16 kernel headers don't have these, so provide fallback definitions */ 624f454a54fSPeter Maydell #define ESR_MAGIC 0x45535201 625f454a54fSPeter Maydell struct esr_context { 626f454a54fSPeter Maydell struct _aarch64_ctx head; 627f454a54fSPeter Maydell uint64_t esr; 628f454a54fSPeter Maydell }; 629f454a54fSPeter Maydell #endif 630f454a54fSPeter Maydell 631f454a54fSPeter Maydell static inline struct _aarch64_ctx *first_ctx(ucontext_t *uc) 632f454a54fSPeter Maydell { 633f454a54fSPeter Maydell return (struct _aarch64_ctx *)&uc->uc_mcontext.__reserved; 634f454a54fSPeter Maydell } 635f454a54fSPeter Maydell 636f454a54fSPeter Maydell static inline struct _aarch64_ctx *next_ctx(struct _aarch64_ctx *hdr) 637f454a54fSPeter Maydell { 638f454a54fSPeter Maydell return (struct _aarch64_ctx *)((char *)hdr + hdr->size); 639f454a54fSPeter Maydell } 640f454a54fSPeter Maydell 641661f7fa4SRichard Henderson int cpu_signal_handler(int host_signum, void *pinfo, void *puc) 642f129061cSClaudio Fontana { 643f129061cSClaudio Fontana siginfo_t *info = pinfo; 64404b33e21SKhem Raj ucontext_t *uc = puc; 645661f7fa4SRichard Henderson uintptr_t pc = uc->uc_mcontext.pc; 646661f7fa4SRichard Henderson bool is_write; 647f454a54fSPeter Maydell struct _aarch64_ctx *hdr; 648f454a54fSPeter Maydell struct esr_context const *esrctx = NULL; 649f129061cSClaudio Fontana 650f454a54fSPeter Maydell /* Find the esr_context, which has the WnR bit in it */ 651f454a54fSPeter Maydell for (hdr = first_ctx(uc); hdr->magic; hdr = next_ctx(hdr)) { 652f454a54fSPeter Maydell if (hdr->magic == ESR_MAGIC) { 653f454a54fSPeter Maydell esrctx = (struct esr_context const *)hdr; 654f454a54fSPeter Maydell break; 655f454a54fSPeter Maydell } 656f454a54fSPeter Maydell } 657f454a54fSPeter Maydell 658f454a54fSPeter Maydell if (esrctx) { 659f454a54fSPeter Maydell /* For data aborts ESR.EC is 0b10010x: then bit 6 is the WnR bit */ 660f454a54fSPeter Maydell uint64_t esr = esrctx->esr; 661f454a54fSPeter Maydell is_write = extract32(esr, 27, 5) == 0x12 && extract32(esr, 6, 1) == 1; 662f454a54fSPeter Maydell } else { 663f454a54fSPeter Maydell /* 664f454a54fSPeter Maydell * Fall back to parsing instructions; will only be needed 665f454a54fSPeter Maydell * for really ancient (pre-3.16) kernels. 666f454a54fSPeter Maydell */ 667f454a54fSPeter Maydell uint32_t insn = *(uint32_t *)pc; 668f454a54fSPeter Maydell 669661f7fa4SRichard Henderson is_write = ((insn & 0xbfff0000) == 0x0c000000 /* C3.3.1 */ 670661f7fa4SRichard Henderson || (insn & 0xbfe00000) == 0x0c800000 /* C3.3.2 */ 671661f7fa4SRichard Henderson || (insn & 0xbfdf0000) == 0x0d000000 /* C3.3.3 */ 672661f7fa4SRichard Henderson || (insn & 0xbfc00000) == 0x0d800000 /* C3.3.4 */ 673661f7fa4SRichard Henderson || (insn & 0x3f400000) == 0x08000000 /* C3.3.6 */ 674661f7fa4SRichard Henderson || (insn & 0x3bc00000) == 0x39000000 /* C3.3.13 */ 675661f7fa4SRichard Henderson || (insn & 0x3fc00000) == 0x3d800000 /* ... 128bit */ 676f454a54fSPeter Maydell /* Ignore bits 10, 11 & 21, controlling indexing. */ 677661f7fa4SRichard Henderson || (insn & 0x3bc00000) == 0x38000000 /* C3.3.8-12 */ 678661f7fa4SRichard Henderson || (insn & 0x3fe00000) == 0x3c800000 /* ... 128bit */ 679661f7fa4SRichard Henderson /* Ignore bits 23 & 24, controlling indexing. */ 680661f7fa4SRichard Henderson || (insn & 0x3a400000) == 0x28000000); /* C3.3.7,14-16 */ 681f454a54fSPeter Maydell } 682a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 683f129061cSClaudio Fontana } 68471b04329SNick Hudson #endif 685f129061cSClaudio Fontana 68642a623c7SBlue Swirl #elif defined(__s390__) 68742a623c7SBlue Swirl 68842a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 68942a623c7SBlue Swirl void *puc) 69042a623c7SBlue Swirl { 69142a623c7SBlue Swirl siginfo_t *info = pinfo; 69204b33e21SKhem Raj ucontext_t *uc = puc; 69342a623c7SBlue Swirl unsigned long pc; 69442a623c7SBlue Swirl uint16_t *pinsn; 69542a623c7SBlue Swirl int is_write = 0; 69642a623c7SBlue Swirl 69742a623c7SBlue Swirl pc = uc->uc_mcontext.psw.addr; 69842a623c7SBlue Swirl 699db17d2cdSIlya Leoshkevich /* 700db17d2cdSIlya Leoshkevich * ??? On linux, the non-rt signal handler has 4 (!) arguments instead 701db17d2cdSIlya Leoshkevich * of the normal 2 arguments. The 4th argument contains the "Translation- 702db17d2cdSIlya Leoshkevich * Exception Identification for DAT Exceptions" from the hardware (aka 703db17d2cdSIlya Leoshkevich * "int_parm_long"), which does in fact contain the is_write value. 704db17d2cdSIlya Leoshkevich * The rt signal handler, as far as I can tell, does not give this value 705db17d2cdSIlya Leoshkevich * at all. Not that we could get to it from here even if it were. 706db17d2cdSIlya Leoshkevich * So fall back to parsing instructions. Treat read-modify-write ones as 707db17d2cdSIlya Leoshkevich * writes, which is not fully correct, but for tracking self-modifying code 708db17d2cdSIlya Leoshkevich * this is better than treating them as reads. Checking si_addr page flags 709db17d2cdSIlya Leoshkevich * might be a viable improvement, albeit a racy one. 710db17d2cdSIlya Leoshkevich */ 711db17d2cdSIlya Leoshkevich /* ??? This is not even close to complete. */ 71242a623c7SBlue Swirl pinsn = (uint16_t *)pc; 71342a623c7SBlue Swirl switch (pinsn[0] >> 8) { 71442a623c7SBlue Swirl case 0x50: /* ST */ 71542a623c7SBlue Swirl case 0x42: /* STC */ 71642a623c7SBlue Swirl case 0x40: /* STH */ 717db17d2cdSIlya Leoshkevich case 0xba: /* CS */ 718db17d2cdSIlya Leoshkevich case 0xbb: /* CDS */ 71942a623c7SBlue Swirl is_write = 1; 72042a623c7SBlue Swirl break; 72142a623c7SBlue Swirl case 0xc4: /* RIL format insns */ 72242a623c7SBlue Swirl switch (pinsn[0] & 0xf) { 72342a623c7SBlue Swirl case 0xf: /* STRL */ 72442a623c7SBlue Swirl case 0xb: /* STGRL */ 72542a623c7SBlue Swirl case 0x7: /* STHRL */ 72642a623c7SBlue Swirl is_write = 1; 72742a623c7SBlue Swirl } 72842a623c7SBlue Swirl break; 729db17d2cdSIlya Leoshkevich case 0xc8: /* SSF format insns */ 730db17d2cdSIlya Leoshkevich switch (pinsn[0] & 0xf) { 731db17d2cdSIlya Leoshkevich case 0x2: /* CSST */ 732db17d2cdSIlya Leoshkevich is_write = 1; 733db17d2cdSIlya Leoshkevich } 734db17d2cdSIlya Leoshkevich break; 73542a623c7SBlue Swirl case 0xe3: /* RXY format insns */ 73642a623c7SBlue Swirl switch (pinsn[2] & 0xff) { 73742a623c7SBlue Swirl case 0x50: /* STY */ 73842a623c7SBlue Swirl case 0x24: /* STG */ 73942a623c7SBlue Swirl case 0x72: /* STCY */ 74042a623c7SBlue Swirl case 0x70: /* STHY */ 74142a623c7SBlue Swirl case 0x8e: /* STPQ */ 74242a623c7SBlue Swirl case 0x3f: /* STRVH */ 74342a623c7SBlue Swirl case 0x3e: /* STRV */ 74442a623c7SBlue Swirl case 0x2f: /* STRVG */ 74542a623c7SBlue Swirl is_write = 1; 74642a623c7SBlue Swirl } 74742a623c7SBlue Swirl break; 748db17d2cdSIlya Leoshkevich case 0xeb: /* RSY format insns */ 749db17d2cdSIlya Leoshkevich switch (pinsn[2] & 0xff) { 750db17d2cdSIlya Leoshkevich case 0x14: /* CSY */ 751db17d2cdSIlya Leoshkevich case 0x30: /* CSG */ 752db17d2cdSIlya Leoshkevich case 0x31: /* CDSY */ 753db17d2cdSIlya Leoshkevich case 0x3e: /* CDSG */ 754db17d2cdSIlya Leoshkevich case 0xe4: /* LANG */ 755db17d2cdSIlya Leoshkevich case 0xe6: /* LAOG */ 756db17d2cdSIlya Leoshkevich case 0xe7: /* LAXG */ 757db17d2cdSIlya Leoshkevich case 0xe8: /* LAAG */ 758db17d2cdSIlya Leoshkevich case 0xea: /* LAALG */ 759db17d2cdSIlya Leoshkevich case 0xf4: /* LAN */ 760db17d2cdSIlya Leoshkevich case 0xf6: /* LAO */ 761db17d2cdSIlya Leoshkevich case 0xf7: /* LAX */ 762db17d2cdSIlya Leoshkevich case 0xfa: /* LAAL */ 763db17d2cdSIlya Leoshkevich case 0xf8: /* LAA */ 764db17d2cdSIlya Leoshkevich is_write = 1; 76542a623c7SBlue Swirl } 766db17d2cdSIlya Leoshkevich break; 767db17d2cdSIlya Leoshkevich } 768db17d2cdSIlya Leoshkevich 769a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 77042a623c7SBlue Swirl } 77142a623c7SBlue Swirl 77242a623c7SBlue Swirl #elif defined(__mips__) 77342a623c7SBlue Swirl 77462475e9dSKele Huang #if defined(__misp16) || defined(__mips_micromips) 77562475e9dSKele Huang #error "Unsupported encoding" 77662475e9dSKele Huang #endif 77762475e9dSKele Huang 77842a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 77942a623c7SBlue Swirl void *puc) 78042a623c7SBlue Swirl { 78142a623c7SBlue Swirl siginfo_t *info = pinfo; 78204b33e21SKhem Raj ucontext_t *uc = puc; 78362475e9dSKele Huang uintptr_t pc = uc->uc_mcontext.pc; 78462475e9dSKele Huang uint32_t insn = *(uint32_t *)pc; 78562475e9dSKele Huang int is_write = 0; 78642a623c7SBlue Swirl 78762475e9dSKele Huang /* Detect all store instructions at program counter. */ 78862475e9dSKele Huang switch((insn >> 26) & 077) { 78962475e9dSKele Huang case 050: /* SB */ 79062475e9dSKele Huang case 051: /* SH */ 79162475e9dSKele Huang case 052: /* SWL */ 79262475e9dSKele Huang case 053: /* SW */ 79362475e9dSKele Huang case 054: /* SDL */ 79462475e9dSKele Huang case 055: /* SDR */ 79562475e9dSKele Huang case 056: /* SWR */ 79662475e9dSKele Huang case 070: /* SC */ 79762475e9dSKele Huang case 071: /* SWC1 */ 79862475e9dSKele Huang case 074: /* SCD */ 79962475e9dSKele Huang case 075: /* SDC1 */ 80062475e9dSKele Huang case 077: /* SD */ 80162475e9dSKele Huang #if !defined(__mips_isa_rev) || __mips_isa_rev < 6 80262475e9dSKele Huang case 072: /* SWC2 */ 80362475e9dSKele Huang case 076: /* SDC2 */ 80462475e9dSKele Huang #endif 80562475e9dSKele Huang is_write = 1; 80662475e9dSKele Huang break; 80762475e9dSKele Huang case 023: /* COP1X */ 80862475e9dSKele Huang /* Required in all versions of MIPS64 since 80962475e9dSKele Huang MIPS64r1 and subsequent versions of MIPS32r2. */ 81062475e9dSKele Huang switch (insn & 077) { 81162475e9dSKele Huang case 010: /* SWXC1 */ 81262475e9dSKele Huang case 011: /* SDXC1 */ 81362475e9dSKele Huang case 015: /* SUXC1 */ 81462475e9dSKele Huang is_write = 1; 81562475e9dSKele Huang } 81662475e9dSKele Huang break; 81762475e9dSKele Huang } 81862475e9dSKele Huang 819a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 82042a623c7SBlue Swirl } 82142a623c7SBlue Swirl 822464e447aSAlistair Francis #elif defined(__riscv) 823464e447aSAlistair Francis 824464e447aSAlistair Francis int cpu_signal_handler(int host_signum, void *pinfo, 825464e447aSAlistair Francis void *puc) 826464e447aSAlistair Francis { 827464e447aSAlistair Francis siginfo_t *info = pinfo; 828464e447aSAlistair Francis ucontext_t *uc = puc; 829464e447aSAlistair Francis greg_t pc = uc->uc_mcontext.__gregs[REG_PC]; 830464e447aSAlistair Francis uint32_t insn = *(uint32_t *)pc; 831464e447aSAlistair Francis int is_write = 0; 832464e447aSAlistair Francis 833464e447aSAlistair Francis /* Detect store by reading the instruction at the program 834464e447aSAlistair Francis counter. Note: we currently only generate 32-bit 835464e447aSAlistair Francis instructions so we thus only detect 32-bit stores */ 836464e447aSAlistair Francis switch (((insn >> 0) & 0b11)) { 837464e447aSAlistair Francis case 3: 838464e447aSAlistair Francis switch (((insn >> 2) & 0b11111)) { 839464e447aSAlistair Francis case 8: 840464e447aSAlistair Francis switch (((insn >> 12) & 0b111)) { 841464e447aSAlistair Francis case 0: /* sb */ 842464e447aSAlistair Francis case 1: /* sh */ 843464e447aSAlistair Francis case 2: /* sw */ 844464e447aSAlistair Francis case 3: /* sd */ 845464e447aSAlistair Francis case 4: /* sq */ 846464e447aSAlistair Francis is_write = 1; 847464e447aSAlistair Francis break; 848464e447aSAlistair Francis default: 849464e447aSAlistair Francis break; 850464e447aSAlistair Francis } 851464e447aSAlistair Francis break; 852464e447aSAlistair Francis case 9: 853464e447aSAlistair Francis switch (((insn >> 12) & 0b111)) { 854464e447aSAlistair Francis case 2: /* fsw */ 855464e447aSAlistair Francis case 3: /* fsd */ 856464e447aSAlistair Francis case 4: /* fsq */ 857464e447aSAlistair Francis is_write = 1; 858464e447aSAlistair Francis break; 859464e447aSAlistair Francis default: 860464e447aSAlistair Francis break; 861464e447aSAlistair Francis } 862464e447aSAlistair Francis break; 863464e447aSAlistair Francis default: 864464e447aSAlistair Francis break; 865464e447aSAlistair Francis } 866464e447aSAlistair Francis } 867464e447aSAlistair Francis 868464e447aSAlistair Francis /* Check for compressed instructions */ 869464e447aSAlistair Francis switch (((insn >> 13) & 0b111)) { 870464e447aSAlistair Francis case 7: 871464e447aSAlistair Francis switch (insn & 0b11) { 872464e447aSAlistair Francis case 0: /*c.sd */ 873464e447aSAlistair Francis case 2: /* c.sdsp */ 874464e447aSAlistair Francis is_write = 1; 875464e447aSAlistair Francis break; 876464e447aSAlistair Francis default: 877464e447aSAlistair Francis break; 878464e447aSAlistair Francis } 879464e447aSAlistair Francis break; 880464e447aSAlistair Francis case 6: 881464e447aSAlistair Francis switch (insn & 0b11) { 882464e447aSAlistair Francis case 0: /* c.sw */ 883464e447aSAlistair Francis case 3: /* c.swsp */ 884464e447aSAlistair Francis is_write = 1; 885464e447aSAlistair Francis break; 886464e447aSAlistair Francis default: 887464e447aSAlistair Francis break; 888464e447aSAlistair Francis } 889464e447aSAlistair Francis break; 890464e447aSAlistair Francis default: 891464e447aSAlistair Francis break; 892464e447aSAlistair Francis } 893464e447aSAlistair Francis 894464e447aSAlistair Francis return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 895464e447aSAlistair Francis } 896464e447aSAlistair Francis 89742a623c7SBlue Swirl #else 89842a623c7SBlue Swirl 89942a623c7SBlue Swirl #error host CPU specific signal handler needed 90042a623c7SBlue Swirl 90142a623c7SBlue Swirl #endif 902a411d296SPhilippe Mathieu-Daudé 903a411d296SPhilippe Mathieu-Daudé /* The softmmu versions of these helpers are in cputlb.c. */ 904a411d296SPhilippe Mathieu-Daudé 905f83bcecbSRichard Henderson /* 906f83bcecbSRichard Henderson * Verify that we have passed the correct MemOp to the correct function. 907f83bcecbSRichard Henderson * 908f83bcecbSRichard Henderson * We could present one function to target code, and dispatch based on 909f83bcecbSRichard Henderson * the MemOp, but so far we have worked hard to avoid an indirect function 910f83bcecbSRichard Henderson * call along the memory path. 911f83bcecbSRichard Henderson */ 912f83bcecbSRichard Henderson static void validate_memop(MemOpIdx oi, MemOp expected) 913ed4cfbcdSRichard Henderson { 914f83bcecbSRichard Henderson #ifdef CONFIG_DEBUG_TCG 915f83bcecbSRichard Henderson MemOp have = get_memop(oi) & (MO_SIZE | MO_BSWAP); 916f83bcecbSRichard Henderson assert(have == expected); 917f83bcecbSRichard Henderson #endif 918f83bcecbSRichard Henderson } 919ed4cfbcdSRichard Henderson 920f83bcecbSRichard Henderson static void *cpu_mmu_lookup(CPUArchState *env, target_ulong addr, 921f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra, MMUAccessType type) 922f83bcecbSRichard Henderson { 923f83bcecbSRichard Henderson void *ret; 924f83bcecbSRichard Henderson 925f83bcecbSRichard Henderson /* TODO: Enforce guest required alignment. */ 926f83bcecbSRichard Henderson 927f83bcecbSRichard Henderson ret = g2h(env_cpu(env), addr); 928f83bcecbSRichard Henderson set_helper_retaddr(ra); 929ed4cfbcdSRichard Henderson return ret; 930ed4cfbcdSRichard Henderson } 931ed4cfbcdSRichard Henderson 932f83bcecbSRichard Henderson uint8_t cpu_ldb_mmu(CPUArchState *env, abi_ptr addr, 933f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 934ed4cfbcdSRichard Henderson { 935f83bcecbSRichard Henderson void *haddr; 936f83bcecbSRichard Henderson uint8_t ret; 937ed4cfbcdSRichard Henderson 938f83bcecbSRichard Henderson validate_memop(oi, MO_UB); 939f83bcecbSRichard Henderson trace_guest_ld_before_exec(env_cpu(env), addr, oi); 940f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD); 941f83bcecbSRichard Henderson ret = ldub_p(haddr); 942f83bcecbSRichard Henderson clear_helper_retaddr(); 943f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R); 944ed4cfbcdSRichard Henderson return ret; 945ed4cfbcdSRichard Henderson } 946ed4cfbcdSRichard Henderson 947f83bcecbSRichard Henderson uint16_t cpu_ldw_be_mmu(CPUArchState *env, abi_ptr addr, 948f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 949ed4cfbcdSRichard Henderson { 950f83bcecbSRichard Henderson void *haddr; 951f83bcecbSRichard Henderson uint16_t ret; 952ed4cfbcdSRichard Henderson 953f83bcecbSRichard Henderson validate_memop(oi, MO_BEUW); 954f83bcecbSRichard Henderson trace_guest_ld_before_exec(env_cpu(env), addr, oi); 955f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD); 956f83bcecbSRichard Henderson ret = lduw_be_p(haddr); 957f83bcecbSRichard Henderson clear_helper_retaddr(); 958f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R); 959ed4cfbcdSRichard Henderson return ret; 960ed4cfbcdSRichard Henderson } 961ed4cfbcdSRichard Henderson 962f83bcecbSRichard Henderson uint32_t cpu_ldl_be_mmu(CPUArchState *env, abi_ptr addr, 963f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 964ed4cfbcdSRichard Henderson { 965f83bcecbSRichard Henderson void *haddr; 966f83bcecbSRichard Henderson uint32_t ret; 967f83bcecbSRichard Henderson 968f83bcecbSRichard Henderson validate_memop(oi, MO_BEUL); 969f83bcecbSRichard Henderson trace_guest_ld_before_exec(env_cpu(env), addr, oi); 970f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD); 971f83bcecbSRichard Henderson ret = ldl_be_p(haddr); 972f83bcecbSRichard Henderson clear_helper_retaddr(); 973f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R); 974f83bcecbSRichard Henderson return ret; 975f83bcecbSRichard Henderson } 976f83bcecbSRichard Henderson 977f83bcecbSRichard Henderson uint64_t cpu_ldq_be_mmu(CPUArchState *env, abi_ptr addr, 978f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 979f83bcecbSRichard Henderson { 980f83bcecbSRichard Henderson void *haddr; 981ed4cfbcdSRichard Henderson uint64_t ret; 982ed4cfbcdSRichard Henderson 983f83bcecbSRichard Henderson validate_memop(oi, MO_BEQ); 984f83bcecbSRichard Henderson trace_guest_ld_before_exec(env_cpu(env), addr, oi); 985f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD); 986f83bcecbSRichard Henderson ret = ldq_be_p(haddr); 987f83bcecbSRichard Henderson clear_helper_retaddr(); 988f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R); 989b9e60257SRichard Henderson return ret; 990b9e60257SRichard Henderson } 991b9e60257SRichard Henderson 992f83bcecbSRichard Henderson uint16_t cpu_ldw_le_mmu(CPUArchState *env, abi_ptr addr, 993f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 994b9e60257SRichard Henderson { 995f83bcecbSRichard Henderson void *haddr; 996f83bcecbSRichard Henderson uint16_t ret; 997f83bcecbSRichard Henderson 998f83bcecbSRichard Henderson validate_memop(oi, MO_LEUW); 999f83bcecbSRichard Henderson trace_guest_ld_before_exec(env_cpu(env), addr, oi); 1000f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD); 1001f83bcecbSRichard Henderson ret = lduw_le_p(haddr); 1002f83bcecbSRichard Henderson clear_helper_retaddr(); 1003f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R); 1004f83bcecbSRichard Henderson return ret; 1005f83bcecbSRichard Henderson } 1006f83bcecbSRichard Henderson 1007f83bcecbSRichard Henderson uint32_t cpu_ldl_le_mmu(CPUArchState *env, abi_ptr addr, 1008f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 1009f83bcecbSRichard Henderson { 1010f83bcecbSRichard Henderson void *haddr; 1011b9e60257SRichard Henderson uint32_t ret; 1012b9e60257SRichard Henderson 1013f83bcecbSRichard Henderson validate_memop(oi, MO_LEUL); 1014f83bcecbSRichard Henderson trace_guest_ld_before_exec(env_cpu(env), addr, oi); 1015f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD); 1016f83bcecbSRichard Henderson ret = ldl_le_p(haddr); 1017f83bcecbSRichard Henderson clear_helper_retaddr(); 1018f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R); 1019b9e60257SRichard Henderson return ret; 1020b9e60257SRichard Henderson } 1021b9e60257SRichard Henderson 1022f83bcecbSRichard Henderson uint64_t cpu_ldq_le_mmu(CPUArchState *env, abi_ptr addr, 1023f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 1024b9e60257SRichard Henderson { 1025f83bcecbSRichard Henderson void *haddr; 1026b9e60257SRichard Henderson uint64_t ret; 1027b9e60257SRichard Henderson 1028f83bcecbSRichard Henderson validate_memop(oi, MO_LEQ); 1029f83bcecbSRichard Henderson trace_guest_ld_before_exec(env_cpu(env), addr, oi); 1030f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD); 1031f83bcecbSRichard Henderson ret = ldq_le_p(haddr); 1032f83bcecbSRichard Henderson clear_helper_retaddr(); 1033f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R); 1034ed4cfbcdSRichard Henderson return ret; 1035ed4cfbcdSRichard Henderson } 1036ed4cfbcdSRichard Henderson 1037f83bcecbSRichard Henderson void cpu_stb_mmu(CPUArchState *env, abi_ptr addr, uint8_t val, 1038f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 1039ed4cfbcdSRichard Henderson { 1040f83bcecbSRichard Henderson void *haddr; 1041ed4cfbcdSRichard Henderson 1042f83bcecbSRichard Henderson validate_memop(oi, MO_UB); 1043f83bcecbSRichard Henderson trace_guest_st_before_exec(env_cpu(env), addr, oi); 1044f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE); 1045f83bcecbSRichard Henderson stb_p(haddr, val); 1046ed4cfbcdSRichard Henderson clear_helper_retaddr(); 1047f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W); 1048ed4cfbcdSRichard Henderson } 1049ed4cfbcdSRichard Henderson 1050f83bcecbSRichard Henderson void cpu_stw_be_mmu(CPUArchState *env, abi_ptr addr, uint16_t val, 1051f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 1052ed4cfbcdSRichard Henderson { 1053f83bcecbSRichard Henderson void *haddr; 1054ed4cfbcdSRichard Henderson 1055f83bcecbSRichard Henderson validate_memop(oi, MO_BEUW); 1056f83bcecbSRichard Henderson trace_guest_st_before_exec(env_cpu(env), addr, oi); 1057f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE); 1058f83bcecbSRichard Henderson stw_be_p(haddr, val); 1059ed4cfbcdSRichard Henderson clear_helper_retaddr(); 1060f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W); 1061ed4cfbcdSRichard Henderson } 1062ed4cfbcdSRichard Henderson 1063f83bcecbSRichard Henderson void cpu_stl_be_mmu(CPUArchState *env, abi_ptr addr, uint32_t val, 1064f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 1065ed4cfbcdSRichard Henderson { 1066f83bcecbSRichard Henderson void *haddr; 1067ed4cfbcdSRichard Henderson 1068f83bcecbSRichard Henderson validate_memop(oi, MO_BEUL); 1069f83bcecbSRichard Henderson trace_guest_st_before_exec(env_cpu(env), addr, oi); 1070f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE); 1071f83bcecbSRichard Henderson stl_be_p(haddr, val); 1072ed4cfbcdSRichard Henderson clear_helper_retaddr(); 1073f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W); 1074ed4cfbcdSRichard Henderson } 1075ed4cfbcdSRichard Henderson 1076f83bcecbSRichard Henderson void cpu_stq_be_mmu(CPUArchState *env, abi_ptr addr, uint64_t val, 1077f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 1078ed4cfbcdSRichard Henderson { 1079f83bcecbSRichard Henderson void *haddr; 1080ed4cfbcdSRichard Henderson 1081f83bcecbSRichard Henderson validate_memop(oi, MO_BEQ); 1082f83bcecbSRichard Henderson trace_guest_st_before_exec(env_cpu(env), addr, oi); 1083f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE); 1084f83bcecbSRichard Henderson stq_be_p(haddr, val); 1085b9e60257SRichard Henderson clear_helper_retaddr(); 1086f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W); 1087b9e60257SRichard Henderson } 1088b9e60257SRichard Henderson 1089f83bcecbSRichard Henderson void cpu_stw_le_mmu(CPUArchState *env, abi_ptr addr, uint16_t val, 1090f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 1091b9e60257SRichard Henderson { 1092f83bcecbSRichard Henderson void *haddr; 1093b9e60257SRichard Henderson 1094f83bcecbSRichard Henderson validate_memop(oi, MO_LEUW); 1095f83bcecbSRichard Henderson trace_guest_st_before_exec(env_cpu(env), addr, oi); 1096f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE); 1097f83bcecbSRichard Henderson stw_le_p(haddr, val); 1098b9e60257SRichard Henderson clear_helper_retaddr(); 1099f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W); 1100b9e60257SRichard Henderson } 1101b9e60257SRichard Henderson 1102f83bcecbSRichard Henderson void cpu_stl_le_mmu(CPUArchState *env, abi_ptr addr, uint32_t val, 1103f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 1104b9e60257SRichard Henderson { 1105f83bcecbSRichard Henderson void *haddr; 1106b9e60257SRichard Henderson 1107f83bcecbSRichard Henderson validate_memop(oi, MO_LEUL); 1108f83bcecbSRichard Henderson trace_guest_st_before_exec(env_cpu(env), addr, oi); 1109f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE); 1110f83bcecbSRichard Henderson stl_le_p(haddr, val); 1111b9e60257SRichard Henderson clear_helper_retaddr(); 1112f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W); 1113b9e60257SRichard Henderson } 1114b9e60257SRichard Henderson 1115f83bcecbSRichard Henderson void cpu_stq_le_mmu(CPUArchState *env, abi_ptr addr, uint64_t val, 1116f83bcecbSRichard Henderson MemOpIdx oi, uintptr_t ra) 1117b9e60257SRichard Henderson { 1118f83bcecbSRichard Henderson void *haddr; 1119b9e60257SRichard Henderson 1120f83bcecbSRichard Henderson validate_memop(oi, MO_LEQ); 1121f83bcecbSRichard Henderson trace_guest_st_before_exec(env_cpu(env), addr, oi); 1122f83bcecbSRichard Henderson haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE); 1123f83bcecbSRichard Henderson stq_le_p(haddr, val); 1124ed4cfbcdSRichard Henderson clear_helper_retaddr(); 1125f83bcecbSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W); 1126ed4cfbcdSRichard Henderson } 1127ed4cfbcdSRichard Henderson 1128ed4cfbcdSRichard Henderson uint32_t cpu_ldub_code(CPUArchState *env, abi_ptr ptr) 1129ed4cfbcdSRichard Henderson { 1130ed4cfbcdSRichard Henderson uint32_t ret; 1131ed4cfbcdSRichard Henderson 1132ed4cfbcdSRichard Henderson set_helper_retaddr(1); 11333e8f1628SRichard Henderson ret = ldub_p(g2h_untagged(ptr)); 1134ed4cfbcdSRichard Henderson clear_helper_retaddr(); 1135ed4cfbcdSRichard Henderson return ret; 1136ed4cfbcdSRichard Henderson } 1137ed4cfbcdSRichard Henderson 1138ed4cfbcdSRichard Henderson uint32_t cpu_lduw_code(CPUArchState *env, abi_ptr ptr) 1139ed4cfbcdSRichard Henderson { 1140ed4cfbcdSRichard Henderson uint32_t ret; 1141ed4cfbcdSRichard Henderson 1142ed4cfbcdSRichard Henderson set_helper_retaddr(1); 11433e8f1628SRichard Henderson ret = lduw_p(g2h_untagged(ptr)); 1144ed4cfbcdSRichard Henderson clear_helper_retaddr(); 1145ed4cfbcdSRichard Henderson return ret; 1146ed4cfbcdSRichard Henderson } 1147ed4cfbcdSRichard Henderson 1148ed4cfbcdSRichard Henderson uint32_t cpu_ldl_code(CPUArchState *env, abi_ptr ptr) 1149ed4cfbcdSRichard Henderson { 1150ed4cfbcdSRichard Henderson uint32_t ret; 1151ed4cfbcdSRichard Henderson 1152ed4cfbcdSRichard Henderson set_helper_retaddr(1); 11533e8f1628SRichard Henderson ret = ldl_p(g2h_untagged(ptr)); 1154ed4cfbcdSRichard Henderson clear_helper_retaddr(); 1155ed4cfbcdSRichard Henderson return ret; 1156ed4cfbcdSRichard Henderson } 1157ed4cfbcdSRichard Henderson 1158ed4cfbcdSRichard Henderson uint64_t cpu_ldq_code(CPUArchState *env, abi_ptr ptr) 1159ed4cfbcdSRichard Henderson { 1160ed4cfbcdSRichard Henderson uint64_t ret; 1161ed4cfbcdSRichard Henderson 1162ed4cfbcdSRichard Henderson set_helper_retaddr(1); 11633e8f1628SRichard Henderson ret = ldq_p(g2h_untagged(ptr)); 1164ed4cfbcdSRichard Henderson clear_helper_retaddr(); 1165ed4cfbcdSRichard Henderson return ret; 1166ed4cfbcdSRichard Henderson } 1167ed4cfbcdSRichard Henderson 1168f83bcecbSRichard Henderson #include "ldst_common.c.inc" 1169f83bcecbSRichard Henderson 1170a754f7f3SRichard Henderson /* 1171a754f7f3SRichard Henderson * Do not allow unaligned operations to proceed. Return the host address. 1172a754f7f3SRichard Henderson * 1173a754f7f3SRichard Henderson * @prot may be PAGE_READ, PAGE_WRITE, or PAGE_READ|PAGE_WRITE. 1174a754f7f3SRichard Henderson */ 1175a411d296SPhilippe Mathieu-Daudé static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr, 11769002ffcbSRichard Henderson MemOpIdx oi, int size, int prot, 1177a754f7f3SRichard Henderson uintptr_t retaddr) 1178a411d296SPhilippe Mathieu-Daudé { 1179a411d296SPhilippe Mathieu-Daudé /* Enforce qemu required alignment. */ 1180a411d296SPhilippe Mathieu-Daudé if (unlikely(addr & (size - 1))) { 118129a0af61SRichard Henderson cpu_loop_exit_atomic(env_cpu(env), retaddr); 1182a411d296SPhilippe Mathieu-Daudé } 11833e8f1628SRichard Henderson void *ret = g2h(env_cpu(env), addr); 118408b97f7fSRichard Henderson set_helper_retaddr(retaddr); 118508b97f7fSRichard Henderson return ret; 1186a411d296SPhilippe Mathieu-Daudé } 1187a411d296SPhilippe Mathieu-Daudé 1188be9568b4SRichard Henderson #include "atomic_common.c.inc" 1189be9568b4SRichard Henderson 1190be9568b4SRichard Henderson /* 1191be9568b4SRichard Henderson * First set of functions passes in OI and RETADDR. 1192be9568b4SRichard Henderson * This makes them callable from other helpers. 1193be9568b4SRichard Henderson */ 1194be9568b4SRichard Henderson 1195be9568b4SRichard Henderson #define ATOMIC_NAME(X) \ 1196be9568b4SRichard Henderson glue(glue(glue(cpu_atomic_ ## X, SUFFIX), END), _mmu) 119708b97f7fSRichard Henderson #define ATOMIC_MMU_CLEANUP do { clear_helper_retaddr(); } while (0) 1198504f73f7SAlex Bennée #define ATOMIC_MMU_IDX MMU_USER_IDX 1199a411d296SPhilippe Mathieu-Daudé 1200a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 1 1201a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 1202a411d296SPhilippe Mathieu-Daudé 1203a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 2 1204a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 1205a411d296SPhilippe Mathieu-Daudé 1206a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 4 1207a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 1208a411d296SPhilippe Mathieu-Daudé 1209a411d296SPhilippe Mathieu-Daudé #ifdef CONFIG_ATOMIC64 1210a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 8 1211a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 1212a411d296SPhilippe Mathieu-Daudé #endif 1213a411d296SPhilippe Mathieu-Daudé 1214e6cd4bb5SRichard Henderson #if HAVE_ATOMIC128 || HAVE_CMPXCHG128 1215be9568b4SRichard Henderson #define DATA_SIZE 16 1216be9568b4SRichard Henderson #include "atomic_template.h" 1217be9568b4SRichard Henderson #endif 1218