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" 203e457172SBlue Swirl #include "cpu.h" 2176cad711SPaolo Bonzini #include "disas/disas.h" 2263c91552SPaolo Bonzini #include "exec/exec-all.h" 2342a623c7SBlue Swirl #include "tcg.h" 24023b0ae3SPeter Maydell #include "qemu/bitops.h" 25f08b6170SPaolo Bonzini #include "exec/cpu_ldst.h" 261652b974SPaolo Bonzini #include "translate-all.h" 27a411d296SPhilippe Mathieu-Daudé #include "exec/helper-proto.h" 28e6cd4bb5SRichard Henderson #include "qemu/atomic128.h" 29*ed4cfbcdSRichard Henderson #include "trace-root.h" 30*ed4cfbcdSRichard Henderson #include "trace/mem.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 4942a623c7SBlue Swirl /* exit the current TB from a signal handler. The host registers are 5042a623c7SBlue Swirl restored in a state compatible with the CPU emulator 5142a623c7SBlue Swirl */ 52a5852dc5SPeter Maydell static void cpu_exit_tb_from_sighandler(CPUState *cpu, sigset_t *old_set) 5342a623c7SBlue Swirl { 5442a623c7SBlue Swirl /* XXX: use siglongjmp ? */ 55a5852dc5SPeter Maydell sigprocmask(SIG_SETMASK, old_set, NULL); 566886b980SPeter Maydell cpu_loop_exit_noexc(cpu); 5742a623c7SBlue Swirl } 5842a623c7SBlue Swirl 5942a623c7SBlue Swirl /* 'pc' is the host PC at which the exception was raised. 'address' is 6042a623c7SBlue Swirl the effective address of the memory exception. 'is_write' is 1 if a 6142a623c7SBlue Swirl write caused the exception and otherwise 0'. 'old_set' is the 6242a623c7SBlue Swirl signal set which should be restored */ 63a78b1299SPeter Maydell static inline int handle_cpu_signal(uintptr_t pc, siginfo_t *info, 64a5852dc5SPeter Maydell int is_write, sigset_t *old_set) 6542a623c7SBlue Swirl { 6602bed6bdSAlex Bennée CPUState *cpu = current_cpu; 677510454eSAndreas Färber CPUClass *cc; 68a78b1299SPeter Maydell unsigned long address = (unsigned long)info->si_addr; 6952ba13f0SRichard Henderson MMUAccessType access_type = is_write ? MMU_DATA_STORE : MMU_DATA_LOAD; 7042a623c7SBlue Swirl 7152ba13f0SRichard Henderson switch (helper_retaddr) { 7252ba13f0SRichard Henderson default: 7352ba13f0SRichard Henderson /* 7452ba13f0SRichard Henderson * Fault during host memory operation within a helper function. 7552ba13f0SRichard Henderson * The helper's host return address, saved here, gives us a 7652ba13f0SRichard Henderson * pointer into the generated code that will unwind to the 7752ba13f0SRichard Henderson * correct guest pc. 78ec603b55SRichard Henderson */ 79ec603b55SRichard Henderson pc = helper_retaddr; 8052ba13f0SRichard Henderson break; 8152ba13f0SRichard Henderson 8252ba13f0SRichard Henderson case 0: 8352ba13f0SRichard Henderson /* 8452ba13f0SRichard Henderson * Fault during host memory operation within generated code. 8552ba13f0SRichard Henderson * (Or, a unrelated bug within qemu, but we can't tell from here). 8652ba13f0SRichard Henderson * 8752ba13f0SRichard Henderson * We take the host pc from the signal frame. However, we cannot 8852ba13f0SRichard Henderson * use that value directly. Within cpu_restore_state_from_tb, we 8952ba13f0SRichard Henderson * assume PC comes from GETPC(), as used by the helper functions, 9052ba13f0SRichard Henderson * so we adjust the address by -GETPC_ADJ to form an address that 9152ba13f0SRichard Henderson * is within the call insn, so that the address does not accidentially 9252ba13f0SRichard Henderson * match the beginning of the next guest insn. However, when the 9352ba13f0SRichard Henderson * pc comes from the signal frame it points to the actual faulting 9452ba13f0SRichard Henderson * host memory insn and not the return from a call insn. 9552ba13f0SRichard Henderson * 9652ba13f0SRichard Henderson * Therefore, adjust to compensate for what will be done later 9752ba13f0SRichard Henderson * by cpu_restore_state_from_tb. 9852ba13f0SRichard Henderson */ 99ec603b55SRichard Henderson pc += GETPC_ADJ; 10052ba13f0SRichard Henderson break; 10152ba13f0SRichard Henderson 10252ba13f0SRichard Henderson case 1: 10352ba13f0SRichard Henderson /* 10452ba13f0SRichard Henderson * Fault during host read for translation, or loosely, "execution". 10552ba13f0SRichard Henderson * 10652ba13f0SRichard Henderson * The guest pc is already pointing to the start of the TB for which 10752ba13f0SRichard Henderson * code is being generated. If the guest translator manages the 10852ba13f0SRichard Henderson * page crossings correctly, this is exactly the correct address 10952ba13f0SRichard Henderson * (and if the translator doesn't handle page boundaries correctly 11052ba13f0SRichard Henderson * there's little we can do about that here). Therefore, do not 11152ba13f0SRichard Henderson * trigger the unwinder. 11252ba13f0SRichard Henderson * 11352ba13f0SRichard Henderson * Like tb_gen_code, release the memory lock before cpu_loop_exit. 11452ba13f0SRichard Henderson */ 11552ba13f0SRichard Henderson pc = 0; 11652ba13f0SRichard Henderson access_type = MMU_INST_FETCH; 11752ba13f0SRichard Henderson mmap_unlock(); 11852ba13f0SRichard Henderson break; 119ec603b55SRichard Henderson } 120ec603b55SRichard Henderson 12102bed6bdSAlex Bennée /* For synchronous signals we expect to be coming from the vCPU 12202bed6bdSAlex Bennée * thread (so current_cpu should be valid) and either from running 12302bed6bdSAlex Bennée * code or during translation which can fault as we cross pages. 12402bed6bdSAlex Bennée * 12502bed6bdSAlex Bennée * If neither is true then something has gone wrong and we should 12602bed6bdSAlex Bennée * abort rather than try and restart the vCPU execution. 12702bed6bdSAlex Bennée */ 12802bed6bdSAlex Bennée if (!cpu || !cpu->running) { 12902bed6bdSAlex Bennée printf("qemu:%s received signal outside vCPU context @ pc=0x%" 13002bed6bdSAlex Bennée PRIxPTR "\n", __func__, pc); 13102bed6bdSAlex Bennée abort(); 13202bed6bdSAlex Bennée } 13302bed6bdSAlex Bennée 13442a623c7SBlue Swirl #if defined(DEBUG_SIGNAL) 13571baf787SPeter Maydell printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 13642a623c7SBlue Swirl pc, address, is_write, *(unsigned long *)old_set); 13742a623c7SBlue Swirl #endif 13842a623c7SBlue Swirl /* XXX: locking issue */ 1399c4bbee9SPeter Maydell /* Note that it is important that we don't call page_unprotect() unless 1409c4bbee9SPeter Maydell * this is really a "write to nonwriteable page" fault, because 1419c4bbee9SPeter Maydell * page_unprotect() assumes that if it is called for an access to 1429c4bbee9SPeter Maydell * a page that's writeable this means we had two threads racing and 1439c4bbee9SPeter Maydell * another thread got there first and already made the page writeable; 1449c4bbee9SPeter Maydell * so we will retry the access. If we were to call page_unprotect() 1459c4bbee9SPeter Maydell * for some other kind of fault that should really be passed to the 1469c4bbee9SPeter Maydell * guest, we'd end up in an infinite loop of retrying the faulting 1479c4bbee9SPeter Maydell * access. 1489c4bbee9SPeter Maydell */ 1499c4bbee9SPeter Maydell if (is_write && info->si_signo == SIGSEGV && info->si_code == SEGV_ACCERR && 1509c4bbee9SPeter Maydell h2g_valid(address)) { 151f213e72fSPeter Maydell switch (page_unprotect(h2g(address), pc)) { 152f213e72fSPeter Maydell case 0: 153f213e72fSPeter Maydell /* Fault not caused by a page marked unwritable to protect 154ec603b55SRichard Henderson * cached translations, must be the guest binary's problem. 155f213e72fSPeter Maydell */ 156f213e72fSPeter Maydell break; 157f213e72fSPeter Maydell case 1: 158f213e72fSPeter Maydell /* Fault caused by protection of cached translation; TBs 159ec603b55SRichard Henderson * invalidated, so resume execution. Retain helper_retaddr 160ec603b55SRichard Henderson * for a possible second fault. 161f213e72fSPeter Maydell */ 16242a623c7SBlue Swirl return 1; 163f213e72fSPeter Maydell case 2: 164f213e72fSPeter Maydell /* Fault caused by protection of cached translation, and the 165f213e72fSPeter Maydell * currently executing TB was modified and must be exited 166ec603b55SRichard Henderson * immediately. Clear helper_retaddr for next execution. 167f213e72fSPeter Maydell */ 16808b97f7fSRichard Henderson clear_helper_retaddr(); 16902bed6bdSAlex Bennée cpu_exit_tb_from_sighandler(cpu, old_set); 170ec603b55SRichard Henderson /* NORETURN */ 171ec603b55SRichard Henderson 172f213e72fSPeter Maydell default: 173f213e72fSPeter Maydell g_assert_not_reached(); 174f213e72fSPeter Maydell } 17542a623c7SBlue Swirl } 17642a623c7SBlue Swirl 177732f9e89SAlexander Graf /* Convert forcefully to guest address space, invalid addresses 178732f9e89SAlexander Graf are still valid segv ones */ 179732f9e89SAlexander Graf address = h2g_nocheck(address); 180732f9e89SAlexander Graf 181da6bbf85SRichard Henderson /* 182da6bbf85SRichard Henderson * There is no way the target can handle this other than raising 183da6bbf85SRichard Henderson * an exception. Undo signal and retaddr state prior to longjmp. 184ec603b55SRichard Henderson */ 185da6bbf85SRichard Henderson sigprocmask(SIG_SETMASK, old_set, NULL); 18608b97f7fSRichard Henderson clear_helper_retaddr(); 187ec603b55SRichard Henderson 188da6bbf85SRichard Henderson cc = CPU_GET_CLASS(cpu); 189da6bbf85SRichard Henderson cc->tlb_fill(cpu, address, 0, access_type, MMU_USER_IDX, false, pc); 190da6bbf85SRichard Henderson g_assert_not_reached(); 19142a623c7SBlue Swirl } 19242a623c7SBlue Swirl 193c25c283dSDavid Hildenbrand void *probe_access(CPUArchState *env, target_ulong addr, int size, 194c25c283dSDavid Hildenbrand MMUAccessType access_type, int mmu_idx, uintptr_t retaddr) 19559e96ac6SDavid Hildenbrand { 196c25c283dSDavid Hildenbrand int flags; 197c25c283dSDavid Hildenbrand 198ca86cf32SDavid Hildenbrand g_assert(-(addr | TARGET_PAGE_MASK) >= size); 199ca86cf32SDavid Hildenbrand 200c25c283dSDavid Hildenbrand switch (access_type) { 201c25c283dSDavid Hildenbrand case MMU_DATA_STORE: 202c25c283dSDavid Hildenbrand flags = PAGE_WRITE; 203c25c283dSDavid Hildenbrand break; 204c25c283dSDavid Hildenbrand case MMU_DATA_LOAD: 205c25c283dSDavid Hildenbrand flags = PAGE_READ; 206c25c283dSDavid Hildenbrand break; 207c25c283dSDavid Hildenbrand case MMU_INST_FETCH: 208c25c283dSDavid Hildenbrand flags = PAGE_EXEC; 209c25c283dSDavid Hildenbrand break; 210c25c283dSDavid Hildenbrand default: 211c25c283dSDavid Hildenbrand g_assert_not_reached(); 212c25c283dSDavid Hildenbrand } 213c25c283dSDavid Hildenbrand 214c25c283dSDavid Hildenbrand if (!guest_addr_valid(addr) || page_check_range(addr, size, flags) < 0) { 21559e96ac6SDavid Hildenbrand CPUState *cpu = env_cpu(env); 21659e96ac6SDavid Hildenbrand CPUClass *cc = CPU_GET_CLASS(cpu); 217c25c283dSDavid Hildenbrand cc->tlb_fill(cpu, addr, size, access_type, MMU_USER_IDX, false, 21859e96ac6SDavid Hildenbrand retaddr); 21959e96ac6SDavid Hildenbrand g_assert_not_reached(); 22059e96ac6SDavid Hildenbrand } 221fef39ccdSDavid Hildenbrand 222fef39ccdSDavid Hildenbrand return size ? g2h(addr) : NULL; 22359e96ac6SDavid Hildenbrand } 22459e96ac6SDavid Hildenbrand 22542a623c7SBlue Swirl #if defined(__i386__) 22642a623c7SBlue Swirl 227c5679026SPeter Maydell #if defined(__NetBSD__) 22842a623c7SBlue Swirl #include <ucontext.h> 22942a623c7SBlue Swirl 23042a623c7SBlue Swirl #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP]) 23142a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO]) 23242a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR]) 23342a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 23442a623c7SBlue Swirl #elif defined(__FreeBSD__) || defined(__DragonFly__) 23542a623c7SBlue Swirl #include <ucontext.h> 23642a623c7SBlue Swirl 23742a623c7SBlue Swirl #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip)) 23842a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno) 23942a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.mc_err) 24042a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 24142a623c7SBlue Swirl #elif defined(__OpenBSD__) 24242a623c7SBlue Swirl #define EIP_sig(context) ((context)->sc_eip) 24342a623c7SBlue Swirl #define TRAP_sig(context) ((context)->sc_trapno) 24442a623c7SBlue Swirl #define ERROR_sig(context) ((context)->sc_err) 24542a623c7SBlue Swirl #define MASK_sig(context) ((context)->sc_mask) 24642a623c7SBlue Swirl #else 24742a623c7SBlue Swirl #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP]) 24842a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO]) 24942a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR]) 25042a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 25142a623c7SBlue Swirl #endif 25242a623c7SBlue Swirl 25342a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 25442a623c7SBlue Swirl void *puc) 25542a623c7SBlue Swirl { 25642a623c7SBlue Swirl siginfo_t *info = pinfo; 25742a623c7SBlue Swirl #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) 25842a623c7SBlue Swirl ucontext_t *uc = puc; 25942a623c7SBlue Swirl #elif defined(__OpenBSD__) 26042a623c7SBlue Swirl struct sigcontext *uc = puc; 26142a623c7SBlue Swirl #else 26204b33e21SKhem Raj ucontext_t *uc = puc; 26342a623c7SBlue Swirl #endif 26442a623c7SBlue Swirl unsigned long pc; 26542a623c7SBlue Swirl int trapno; 26642a623c7SBlue Swirl 26742a623c7SBlue Swirl #ifndef REG_EIP 26842a623c7SBlue Swirl /* for glibc 2.1 */ 26942a623c7SBlue Swirl #define REG_EIP EIP 27042a623c7SBlue Swirl #define REG_ERR ERR 27142a623c7SBlue Swirl #define REG_TRAPNO TRAPNO 27242a623c7SBlue Swirl #endif 27342a623c7SBlue Swirl pc = EIP_sig(uc); 27442a623c7SBlue Swirl trapno = TRAP_sig(uc); 275a78b1299SPeter Maydell return handle_cpu_signal(pc, info, 276a78b1299SPeter Maydell trapno == 0xe ? (ERROR_sig(uc) >> 1) & 1 : 0, 277a5852dc5SPeter Maydell &MASK_sig(uc)); 27842a623c7SBlue Swirl } 27942a623c7SBlue Swirl 28042a623c7SBlue Swirl #elif defined(__x86_64__) 28142a623c7SBlue Swirl 28242a623c7SBlue Swirl #ifdef __NetBSD__ 28342a623c7SBlue Swirl #define PC_sig(context) _UC_MACHINE_PC(context) 28442a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO]) 28542a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR]) 28642a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 28742a623c7SBlue Swirl #elif defined(__OpenBSD__) 28842a623c7SBlue Swirl #define PC_sig(context) ((context)->sc_rip) 28942a623c7SBlue Swirl #define TRAP_sig(context) ((context)->sc_trapno) 29042a623c7SBlue Swirl #define ERROR_sig(context) ((context)->sc_err) 29142a623c7SBlue Swirl #define MASK_sig(context) ((context)->sc_mask) 29242a623c7SBlue Swirl #elif defined(__FreeBSD__) || defined(__DragonFly__) 29342a623c7SBlue Swirl #include <ucontext.h> 29442a623c7SBlue Swirl 29542a623c7SBlue Swirl #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip)) 29642a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno) 29742a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.mc_err) 29842a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 29942a623c7SBlue Swirl #else 30042a623c7SBlue Swirl #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP]) 30142a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO]) 30242a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR]) 30342a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 30442a623c7SBlue Swirl #endif 30542a623c7SBlue Swirl 30642a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 30742a623c7SBlue Swirl void *puc) 30842a623c7SBlue Swirl { 30942a623c7SBlue Swirl siginfo_t *info = pinfo; 31042a623c7SBlue Swirl unsigned long pc; 31142a623c7SBlue Swirl #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) 31242a623c7SBlue Swirl ucontext_t *uc = puc; 31342a623c7SBlue Swirl #elif defined(__OpenBSD__) 31442a623c7SBlue Swirl struct sigcontext *uc = puc; 31542a623c7SBlue Swirl #else 31604b33e21SKhem Raj ucontext_t *uc = puc; 31742a623c7SBlue Swirl #endif 31842a623c7SBlue Swirl 31942a623c7SBlue Swirl pc = PC_sig(uc); 320a78b1299SPeter Maydell return handle_cpu_signal(pc, info, 321a78b1299SPeter Maydell TRAP_sig(uc) == 0xe ? (ERROR_sig(uc) >> 1) & 1 : 0, 322a5852dc5SPeter Maydell &MASK_sig(uc)); 32342a623c7SBlue Swirl } 32442a623c7SBlue Swirl 32542a623c7SBlue Swirl #elif defined(_ARCH_PPC) 32642a623c7SBlue Swirl 32742a623c7SBlue Swirl /*********************************************************************** 32842a623c7SBlue Swirl * signal context platform-specific definitions 32942a623c7SBlue Swirl * From Wine 33042a623c7SBlue Swirl */ 33142a623c7SBlue Swirl #ifdef linux 33242a623c7SBlue Swirl /* All Registers access - only for local access */ 33342a623c7SBlue Swirl #define REG_sig(reg_name, context) \ 33442a623c7SBlue Swirl ((context)->uc_mcontext.regs->reg_name) 33542a623c7SBlue Swirl /* Gpr Registers access */ 33642a623c7SBlue Swirl #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context) 33742a623c7SBlue Swirl /* Program counter */ 33842a623c7SBlue Swirl #define IAR_sig(context) REG_sig(nip, context) 33942a623c7SBlue Swirl /* Machine State Register (Supervisor) */ 34042a623c7SBlue Swirl #define MSR_sig(context) REG_sig(msr, context) 34142a623c7SBlue Swirl /* Count register */ 34242a623c7SBlue Swirl #define CTR_sig(context) REG_sig(ctr, context) 34342a623c7SBlue Swirl /* User's integer exception register */ 34442a623c7SBlue Swirl #define XER_sig(context) REG_sig(xer, context) 34542a623c7SBlue Swirl /* Link register */ 34642a623c7SBlue Swirl #define LR_sig(context) REG_sig(link, context) 34742a623c7SBlue Swirl /* Condition register */ 34842a623c7SBlue Swirl #define CR_sig(context) REG_sig(ccr, context) 34942a623c7SBlue Swirl 35042a623c7SBlue Swirl /* Float Registers access */ 35142a623c7SBlue Swirl #define FLOAT_sig(reg_num, context) \ 35242a623c7SBlue Swirl (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num]) 35342a623c7SBlue Swirl #define FPSCR_sig(context) \ 35442a623c7SBlue Swirl (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4))) 35542a623c7SBlue Swirl /* Exception Registers access */ 35642a623c7SBlue Swirl #define DAR_sig(context) REG_sig(dar, context) 35742a623c7SBlue Swirl #define DSISR_sig(context) REG_sig(dsisr, context) 35842a623c7SBlue Swirl #define TRAP_sig(context) REG_sig(trap, context) 35942a623c7SBlue Swirl #endif /* linux */ 36042a623c7SBlue Swirl 36142a623c7SBlue Swirl #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 36242a623c7SBlue Swirl #include <ucontext.h> 36342a623c7SBlue Swirl #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0) 36442a623c7SBlue Swirl #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1) 36542a623c7SBlue Swirl #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr) 36642a623c7SBlue Swirl #define XER_sig(context) ((context)->uc_mcontext.mc_xer) 36742a623c7SBlue Swirl #define LR_sig(context) ((context)->uc_mcontext.mc_lr) 36842a623c7SBlue Swirl #define CR_sig(context) ((context)->uc_mcontext.mc_cr) 36942a623c7SBlue Swirl /* Exception Registers access */ 37042a623c7SBlue Swirl #define DAR_sig(context) ((context)->uc_mcontext.mc_dar) 37142a623c7SBlue Swirl #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr) 37242a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc) 37342a623c7SBlue Swirl #endif /* __FreeBSD__|| __FreeBSD_kernel__ */ 37442a623c7SBlue Swirl 37542a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 37642a623c7SBlue Swirl void *puc) 37742a623c7SBlue Swirl { 37842a623c7SBlue Swirl siginfo_t *info = pinfo; 37942a623c7SBlue Swirl #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 38042a623c7SBlue Swirl ucontext_t *uc = puc; 38142a623c7SBlue Swirl #else 38204b33e21SKhem Raj ucontext_t *uc = puc; 38342a623c7SBlue Swirl #endif 38442a623c7SBlue Swirl unsigned long pc; 38542a623c7SBlue Swirl int is_write; 38642a623c7SBlue Swirl 38742a623c7SBlue Swirl pc = IAR_sig(uc); 38842a623c7SBlue Swirl is_write = 0; 38942a623c7SBlue Swirl #if 0 39042a623c7SBlue Swirl /* ppc 4xx case */ 39142a623c7SBlue Swirl if (DSISR_sig(uc) & 0x00800000) { 39242a623c7SBlue Swirl is_write = 1; 39342a623c7SBlue Swirl } 39442a623c7SBlue Swirl #else 39542a623c7SBlue Swirl if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) { 39642a623c7SBlue Swirl is_write = 1; 39742a623c7SBlue Swirl } 39842a623c7SBlue Swirl #endif 399a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 40042a623c7SBlue Swirl } 40142a623c7SBlue Swirl 40242a623c7SBlue Swirl #elif defined(__alpha__) 40342a623c7SBlue Swirl 40442a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 40542a623c7SBlue Swirl void *puc) 40642a623c7SBlue Swirl { 40742a623c7SBlue Swirl siginfo_t *info = pinfo; 40804b33e21SKhem Raj ucontext_t *uc = puc; 40942a623c7SBlue Swirl uint32_t *pc = uc->uc_mcontext.sc_pc; 41042a623c7SBlue Swirl uint32_t insn = *pc; 41142a623c7SBlue Swirl int is_write = 0; 41242a623c7SBlue Swirl 41342a623c7SBlue Swirl /* XXX: need kernel patch to get write flag faster */ 41442a623c7SBlue Swirl switch (insn >> 26) { 41542a623c7SBlue Swirl case 0x0d: /* stw */ 41642a623c7SBlue Swirl case 0x0e: /* stb */ 41742a623c7SBlue Swirl case 0x0f: /* stq_u */ 41842a623c7SBlue Swirl case 0x24: /* stf */ 41942a623c7SBlue Swirl case 0x25: /* stg */ 42042a623c7SBlue Swirl case 0x26: /* sts */ 42142a623c7SBlue Swirl case 0x27: /* stt */ 42242a623c7SBlue Swirl case 0x2c: /* stl */ 42342a623c7SBlue Swirl case 0x2d: /* stq */ 42442a623c7SBlue Swirl case 0x2e: /* stl_c */ 42542a623c7SBlue Swirl case 0x2f: /* stq_c */ 42642a623c7SBlue Swirl is_write = 1; 42742a623c7SBlue Swirl } 42842a623c7SBlue Swirl 429a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 43042a623c7SBlue Swirl } 43142a623c7SBlue Swirl #elif defined(__sparc__) 43242a623c7SBlue Swirl 43342a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 43442a623c7SBlue Swirl void *puc) 43542a623c7SBlue Swirl { 43642a623c7SBlue Swirl siginfo_t *info = pinfo; 43742a623c7SBlue Swirl int is_write; 43842a623c7SBlue Swirl uint32_t insn; 43942a623c7SBlue Swirl #if !defined(__arch64__) || defined(CONFIG_SOLARIS) 44042a623c7SBlue Swirl uint32_t *regs = (uint32_t *)(info + 1); 44142a623c7SBlue Swirl void *sigmask = (regs + 20); 44242a623c7SBlue Swirl /* XXX: is there a standard glibc define ? */ 44342a623c7SBlue Swirl unsigned long pc = regs[1]; 44442a623c7SBlue Swirl #else 44542a623c7SBlue Swirl #ifdef __linux__ 44642a623c7SBlue Swirl struct sigcontext *sc = puc; 44742a623c7SBlue Swirl unsigned long pc = sc->sigc_regs.tpc; 44842a623c7SBlue Swirl void *sigmask = (void *)sc->sigc_mask; 44942a623c7SBlue Swirl #elif defined(__OpenBSD__) 45042a623c7SBlue Swirl struct sigcontext *uc = puc; 45142a623c7SBlue Swirl unsigned long pc = uc->sc_pc; 45242a623c7SBlue Swirl void *sigmask = (void *)(long)uc->sc_mask; 4537ccfb495STobias Nygren #elif defined(__NetBSD__) 4547ccfb495STobias Nygren ucontext_t *uc = puc; 4557ccfb495STobias Nygren unsigned long pc = _UC_MACHINE_PC(uc); 4567ccfb495STobias Nygren void *sigmask = (void *)&uc->uc_sigmask; 45742a623c7SBlue Swirl #endif 45842a623c7SBlue Swirl #endif 45942a623c7SBlue Swirl 46042a623c7SBlue Swirl /* XXX: need kernel patch to get write flag faster */ 46142a623c7SBlue Swirl is_write = 0; 46242a623c7SBlue Swirl insn = *(uint32_t *)pc; 46342a623c7SBlue Swirl if ((insn >> 30) == 3) { 46442a623c7SBlue Swirl switch ((insn >> 19) & 0x3f) { 46542a623c7SBlue Swirl case 0x05: /* stb */ 46642a623c7SBlue Swirl case 0x15: /* stba */ 46742a623c7SBlue Swirl case 0x06: /* sth */ 46842a623c7SBlue Swirl case 0x16: /* stha */ 46942a623c7SBlue Swirl case 0x04: /* st */ 47042a623c7SBlue Swirl case 0x14: /* sta */ 47142a623c7SBlue Swirl case 0x07: /* std */ 47242a623c7SBlue Swirl case 0x17: /* stda */ 47342a623c7SBlue Swirl case 0x0e: /* stx */ 47442a623c7SBlue Swirl case 0x1e: /* stxa */ 47542a623c7SBlue Swirl case 0x24: /* stf */ 47642a623c7SBlue Swirl case 0x34: /* stfa */ 47742a623c7SBlue Swirl case 0x27: /* stdf */ 47842a623c7SBlue Swirl case 0x37: /* stdfa */ 47942a623c7SBlue Swirl case 0x26: /* stqf */ 48042a623c7SBlue Swirl case 0x36: /* stqfa */ 48142a623c7SBlue Swirl case 0x25: /* stfsr */ 48242a623c7SBlue Swirl case 0x3c: /* casa */ 48342a623c7SBlue Swirl case 0x3e: /* casxa */ 48442a623c7SBlue Swirl is_write = 1; 48542a623c7SBlue Swirl break; 48642a623c7SBlue Swirl } 48742a623c7SBlue Swirl } 488a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, sigmask); 48942a623c7SBlue Swirl } 49042a623c7SBlue Swirl 49142a623c7SBlue Swirl #elif defined(__arm__) 49242a623c7SBlue Swirl 4937ccfb495STobias Nygren #if defined(__NetBSD__) 4947ccfb495STobias Nygren #include <ucontext.h> 4957ccfb495STobias Nygren #endif 4967ccfb495STobias Nygren 49742a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 49842a623c7SBlue Swirl void *puc) 49942a623c7SBlue Swirl { 50042a623c7SBlue Swirl siginfo_t *info = pinfo; 5017ccfb495STobias Nygren #if defined(__NetBSD__) 5027ccfb495STobias Nygren ucontext_t *uc = puc; 5037ccfb495STobias Nygren #else 50404b33e21SKhem Raj ucontext_t *uc = puc; 5057ccfb495STobias Nygren #endif 50642a623c7SBlue Swirl unsigned long pc; 50742a623c7SBlue Swirl int is_write; 50842a623c7SBlue Swirl 5097ccfb495STobias Nygren #if defined(__NetBSD__) 5107ccfb495STobias Nygren pc = uc->uc_mcontext.__gregs[_REG_R15]; 5117ccfb495STobias Nygren #elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) 51242a623c7SBlue Swirl pc = uc->uc_mcontext.gregs[R15]; 51342a623c7SBlue Swirl #else 51442a623c7SBlue Swirl pc = uc->uc_mcontext.arm_pc; 51542a623c7SBlue Swirl #endif 516023b0ae3SPeter Maydell 517023b0ae3SPeter Maydell /* error_code is the FSR value, in which bit 11 is WnR (assuming a v6 or 518023b0ae3SPeter Maydell * later processor; on v5 we will always report this as a read). 519023b0ae3SPeter Maydell */ 520023b0ae3SPeter Maydell is_write = extract32(uc->uc_mcontext.error_code, 11, 1); 521a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 52242a623c7SBlue Swirl } 52342a623c7SBlue Swirl 524f129061cSClaudio Fontana #elif defined(__aarch64__) 525f129061cSClaudio Fontana 526f454a54fSPeter Maydell #ifndef ESR_MAGIC 527f454a54fSPeter Maydell /* Pre-3.16 kernel headers don't have these, so provide fallback definitions */ 528f454a54fSPeter Maydell #define ESR_MAGIC 0x45535201 529f454a54fSPeter Maydell struct esr_context { 530f454a54fSPeter Maydell struct _aarch64_ctx head; 531f454a54fSPeter Maydell uint64_t esr; 532f454a54fSPeter Maydell }; 533f454a54fSPeter Maydell #endif 534f454a54fSPeter Maydell 535f454a54fSPeter Maydell static inline struct _aarch64_ctx *first_ctx(ucontext_t *uc) 536f454a54fSPeter Maydell { 537f454a54fSPeter Maydell return (struct _aarch64_ctx *)&uc->uc_mcontext.__reserved; 538f454a54fSPeter Maydell } 539f454a54fSPeter Maydell 540f454a54fSPeter Maydell static inline struct _aarch64_ctx *next_ctx(struct _aarch64_ctx *hdr) 541f454a54fSPeter Maydell { 542f454a54fSPeter Maydell return (struct _aarch64_ctx *)((char *)hdr + hdr->size); 543f454a54fSPeter Maydell } 544f454a54fSPeter Maydell 545661f7fa4SRichard Henderson int cpu_signal_handler(int host_signum, void *pinfo, void *puc) 546f129061cSClaudio Fontana { 547f129061cSClaudio Fontana siginfo_t *info = pinfo; 54804b33e21SKhem Raj ucontext_t *uc = puc; 549661f7fa4SRichard Henderson uintptr_t pc = uc->uc_mcontext.pc; 550661f7fa4SRichard Henderson bool is_write; 551f454a54fSPeter Maydell struct _aarch64_ctx *hdr; 552f454a54fSPeter Maydell struct esr_context const *esrctx = NULL; 553f129061cSClaudio Fontana 554f454a54fSPeter Maydell /* Find the esr_context, which has the WnR bit in it */ 555f454a54fSPeter Maydell for (hdr = first_ctx(uc); hdr->magic; hdr = next_ctx(hdr)) { 556f454a54fSPeter Maydell if (hdr->magic == ESR_MAGIC) { 557f454a54fSPeter Maydell esrctx = (struct esr_context const *)hdr; 558f454a54fSPeter Maydell break; 559f454a54fSPeter Maydell } 560f454a54fSPeter Maydell } 561f454a54fSPeter Maydell 562f454a54fSPeter Maydell if (esrctx) { 563f454a54fSPeter Maydell /* For data aborts ESR.EC is 0b10010x: then bit 6 is the WnR bit */ 564f454a54fSPeter Maydell uint64_t esr = esrctx->esr; 565f454a54fSPeter Maydell is_write = extract32(esr, 27, 5) == 0x12 && extract32(esr, 6, 1) == 1; 566f454a54fSPeter Maydell } else { 567f454a54fSPeter Maydell /* 568f454a54fSPeter Maydell * Fall back to parsing instructions; will only be needed 569f454a54fSPeter Maydell * for really ancient (pre-3.16) kernels. 570f454a54fSPeter Maydell */ 571f454a54fSPeter Maydell uint32_t insn = *(uint32_t *)pc; 572f454a54fSPeter Maydell 573661f7fa4SRichard Henderson is_write = ((insn & 0xbfff0000) == 0x0c000000 /* C3.3.1 */ 574661f7fa4SRichard Henderson || (insn & 0xbfe00000) == 0x0c800000 /* C3.3.2 */ 575661f7fa4SRichard Henderson || (insn & 0xbfdf0000) == 0x0d000000 /* C3.3.3 */ 576661f7fa4SRichard Henderson || (insn & 0xbfc00000) == 0x0d800000 /* C3.3.4 */ 577661f7fa4SRichard Henderson || (insn & 0x3f400000) == 0x08000000 /* C3.3.6 */ 578661f7fa4SRichard Henderson || (insn & 0x3bc00000) == 0x39000000 /* C3.3.13 */ 579661f7fa4SRichard Henderson || (insn & 0x3fc00000) == 0x3d800000 /* ... 128bit */ 580f454a54fSPeter Maydell /* Ignore bits 10, 11 & 21, controlling indexing. */ 581661f7fa4SRichard Henderson || (insn & 0x3bc00000) == 0x38000000 /* C3.3.8-12 */ 582661f7fa4SRichard Henderson || (insn & 0x3fe00000) == 0x3c800000 /* ... 128bit */ 583661f7fa4SRichard Henderson /* Ignore bits 23 & 24, controlling indexing. */ 584661f7fa4SRichard Henderson || (insn & 0x3a400000) == 0x28000000); /* C3.3.7,14-16 */ 585f454a54fSPeter Maydell } 586a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 587f129061cSClaudio Fontana } 588f129061cSClaudio Fontana 58942a623c7SBlue Swirl #elif defined(__s390__) 59042a623c7SBlue Swirl 59142a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 59242a623c7SBlue Swirl void *puc) 59342a623c7SBlue Swirl { 59442a623c7SBlue Swirl siginfo_t *info = pinfo; 59504b33e21SKhem Raj ucontext_t *uc = puc; 59642a623c7SBlue Swirl unsigned long pc; 59742a623c7SBlue Swirl uint16_t *pinsn; 59842a623c7SBlue Swirl int is_write = 0; 59942a623c7SBlue Swirl 60042a623c7SBlue Swirl pc = uc->uc_mcontext.psw.addr; 60142a623c7SBlue Swirl 60242a623c7SBlue Swirl /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead 60342a623c7SBlue Swirl of the normal 2 arguments. The 3rd argument contains the "int_code" 60442a623c7SBlue Swirl from the hardware which does in fact contain the is_write value. 60542a623c7SBlue Swirl The rt signal handler, as far as I can tell, does not give this value 60642a623c7SBlue Swirl at all. Not that we could get to it from here even if it were. */ 60742a623c7SBlue Swirl /* ??? This is not even close to complete, since it ignores all 60842a623c7SBlue Swirl of the read-modify-write instructions. */ 60942a623c7SBlue Swirl pinsn = (uint16_t *)pc; 61042a623c7SBlue Swirl switch (pinsn[0] >> 8) { 61142a623c7SBlue Swirl case 0x50: /* ST */ 61242a623c7SBlue Swirl case 0x42: /* STC */ 61342a623c7SBlue Swirl case 0x40: /* STH */ 61442a623c7SBlue Swirl is_write = 1; 61542a623c7SBlue Swirl break; 61642a623c7SBlue Swirl case 0xc4: /* RIL format insns */ 61742a623c7SBlue Swirl switch (pinsn[0] & 0xf) { 61842a623c7SBlue Swirl case 0xf: /* STRL */ 61942a623c7SBlue Swirl case 0xb: /* STGRL */ 62042a623c7SBlue Swirl case 0x7: /* STHRL */ 62142a623c7SBlue Swirl is_write = 1; 62242a623c7SBlue Swirl } 62342a623c7SBlue Swirl break; 62442a623c7SBlue Swirl case 0xe3: /* RXY format insns */ 62542a623c7SBlue Swirl switch (pinsn[2] & 0xff) { 62642a623c7SBlue Swirl case 0x50: /* STY */ 62742a623c7SBlue Swirl case 0x24: /* STG */ 62842a623c7SBlue Swirl case 0x72: /* STCY */ 62942a623c7SBlue Swirl case 0x70: /* STHY */ 63042a623c7SBlue Swirl case 0x8e: /* STPQ */ 63142a623c7SBlue Swirl case 0x3f: /* STRVH */ 63242a623c7SBlue Swirl case 0x3e: /* STRV */ 63342a623c7SBlue Swirl case 0x2f: /* STRVG */ 63442a623c7SBlue Swirl is_write = 1; 63542a623c7SBlue Swirl } 63642a623c7SBlue Swirl break; 63742a623c7SBlue Swirl } 638a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 63942a623c7SBlue Swirl } 64042a623c7SBlue Swirl 64142a623c7SBlue Swirl #elif defined(__mips__) 64242a623c7SBlue Swirl 64342a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 64442a623c7SBlue Swirl void *puc) 64542a623c7SBlue Swirl { 64642a623c7SBlue Swirl siginfo_t *info = pinfo; 64704b33e21SKhem Raj ucontext_t *uc = puc; 64842a623c7SBlue Swirl greg_t pc = uc->uc_mcontext.pc; 64942a623c7SBlue Swirl int is_write; 65042a623c7SBlue Swirl 65142a623c7SBlue Swirl /* XXX: compute is_write */ 65242a623c7SBlue Swirl is_write = 0; 653a78b1299SPeter Maydell return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 65442a623c7SBlue Swirl } 65542a623c7SBlue Swirl 656464e447aSAlistair Francis #elif defined(__riscv) 657464e447aSAlistair Francis 658464e447aSAlistair Francis int cpu_signal_handler(int host_signum, void *pinfo, 659464e447aSAlistair Francis void *puc) 660464e447aSAlistair Francis { 661464e447aSAlistair Francis siginfo_t *info = pinfo; 662464e447aSAlistair Francis ucontext_t *uc = puc; 663464e447aSAlistair Francis greg_t pc = uc->uc_mcontext.__gregs[REG_PC]; 664464e447aSAlistair Francis uint32_t insn = *(uint32_t *)pc; 665464e447aSAlistair Francis int is_write = 0; 666464e447aSAlistair Francis 667464e447aSAlistair Francis /* Detect store by reading the instruction at the program 668464e447aSAlistair Francis counter. Note: we currently only generate 32-bit 669464e447aSAlistair Francis instructions so we thus only detect 32-bit stores */ 670464e447aSAlistair Francis switch (((insn >> 0) & 0b11)) { 671464e447aSAlistair Francis case 3: 672464e447aSAlistair Francis switch (((insn >> 2) & 0b11111)) { 673464e447aSAlistair Francis case 8: 674464e447aSAlistair Francis switch (((insn >> 12) & 0b111)) { 675464e447aSAlistair Francis case 0: /* sb */ 676464e447aSAlistair Francis case 1: /* sh */ 677464e447aSAlistair Francis case 2: /* sw */ 678464e447aSAlistair Francis case 3: /* sd */ 679464e447aSAlistair Francis case 4: /* sq */ 680464e447aSAlistair Francis is_write = 1; 681464e447aSAlistair Francis break; 682464e447aSAlistair Francis default: 683464e447aSAlistair Francis break; 684464e447aSAlistair Francis } 685464e447aSAlistair Francis break; 686464e447aSAlistair Francis case 9: 687464e447aSAlistair Francis switch (((insn >> 12) & 0b111)) { 688464e447aSAlistair Francis case 2: /* fsw */ 689464e447aSAlistair Francis case 3: /* fsd */ 690464e447aSAlistair Francis case 4: /* fsq */ 691464e447aSAlistair Francis is_write = 1; 692464e447aSAlistair Francis break; 693464e447aSAlistair Francis default: 694464e447aSAlistair Francis break; 695464e447aSAlistair Francis } 696464e447aSAlistair Francis break; 697464e447aSAlistair Francis default: 698464e447aSAlistair Francis break; 699464e447aSAlistair Francis } 700464e447aSAlistair Francis } 701464e447aSAlistair Francis 702464e447aSAlistair Francis /* Check for compressed instructions */ 703464e447aSAlistair Francis switch (((insn >> 13) & 0b111)) { 704464e447aSAlistair Francis case 7: 705464e447aSAlistair Francis switch (insn & 0b11) { 706464e447aSAlistair Francis case 0: /*c.sd */ 707464e447aSAlistair Francis case 2: /* c.sdsp */ 708464e447aSAlistair Francis is_write = 1; 709464e447aSAlistair Francis break; 710464e447aSAlistair Francis default: 711464e447aSAlistair Francis break; 712464e447aSAlistair Francis } 713464e447aSAlistair Francis break; 714464e447aSAlistair Francis case 6: 715464e447aSAlistair Francis switch (insn & 0b11) { 716464e447aSAlistair Francis case 0: /* c.sw */ 717464e447aSAlistair Francis case 3: /* c.swsp */ 718464e447aSAlistair Francis is_write = 1; 719464e447aSAlistair Francis break; 720464e447aSAlistair Francis default: 721464e447aSAlistair Francis break; 722464e447aSAlistair Francis } 723464e447aSAlistair Francis break; 724464e447aSAlistair Francis default: 725464e447aSAlistair Francis break; 726464e447aSAlistair Francis } 727464e447aSAlistair Francis 728464e447aSAlistair Francis return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask); 729464e447aSAlistair Francis } 730464e447aSAlistair Francis 73142a623c7SBlue Swirl #else 73242a623c7SBlue Swirl 73342a623c7SBlue Swirl #error host CPU specific signal handler needed 73442a623c7SBlue Swirl 73542a623c7SBlue Swirl #endif 736a411d296SPhilippe Mathieu-Daudé 737a411d296SPhilippe Mathieu-Daudé /* The softmmu versions of these helpers are in cputlb.c. */ 738a411d296SPhilippe Mathieu-Daudé 739*ed4cfbcdSRichard Henderson uint32_t cpu_ldub_data(CPUArchState *env, abi_ptr ptr) 740*ed4cfbcdSRichard Henderson { 741*ed4cfbcdSRichard Henderson uint32_t ret; 742*ed4cfbcdSRichard Henderson uint16_t meminfo = trace_mem_get_info(MO_UB, MMU_USER_IDX, false); 743*ed4cfbcdSRichard Henderson 744*ed4cfbcdSRichard Henderson trace_guest_mem_before_exec(env_cpu(env), ptr, meminfo); 745*ed4cfbcdSRichard Henderson ret = ldub_p(g2h(ptr)); 746*ed4cfbcdSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), ptr, meminfo); 747*ed4cfbcdSRichard Henderson return ret; 748*ed4cfbcdSRichard Henderson } 749*ed4cfbcdSRichard Henderson 750*ed4cfbcdSRichard Henderson int cpu_ldsb_data(CPUArchState *env, abi_ptr ptr) 751*ed4cfbcdSRichard Henderson { 752*ed4cfbcdSRichard Henderson int ret; 753*ed4cfbcdSRichard Henderson uint16_t meminfo = trace_mem_get_info(MO_SB, MMU_USER_IDX, false); 754*ed4cfbcdSRichard Henderson 755*ed4cfbcdSRichard Henderson trace_guest_mem_before_exec(env_cpu(env), ptr, meminfo); 756*ed4cfbcdSRichard Henderson ret = ldsb_p(g2h(ptr)); 757*ed4cfbcdSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), ptr, meminfo); 758*ed4cfbcdSRichard Henderson return ret; 759*ed4cfbcdSRichard Henderson } 760*ed4cfbcdSRichard Henderson 761*ed4cfbcdSRichard Henderson uint32_t cpu_lduw_data(CPUArchState *env, abi_ptr ptr) 762*ed4cfbcdSRichard Henderson { 763*ed4cfbcdSRichard Henderson uint32_t ret; 764*ed4cfbcdSRichard Henderson uint16_t meminfo = trace_mem_get_info(MO_TEUW, MMU_USER_IDX, false); 765*ed4cfbcdSRichard Henderson 766*ed4cfbcdSRichard Henderson trace_guest_mem_before_exec(env_cpu(env), ptr, meminfo); 767*ed4cfbcdSRichard Henderson ret = lduw_p(g2h(ptr)); 768*ed4cfbcdSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), ptr, meminfo); 769*ed4cfbcdSRichard Henderson return ret; 770*ed4cfbcdSRichard Henderson } 771*ed4cfbcdSRichard Henderson 772*ed4cfbcdSRichard Henderson int cpu_ldsw_data(CPUArchState *env, abi_ptr ptr) 773*ed4cfbcdSRichard Henderson { 774*ed4cfbcdSRichard Henderson int ret; 775*ed4cfbcdSRichard Henderson uint16_t meminfo = trace_mem_get_info(MO_TESW, MMU_USER_IDX, false); 776*ed4cfbcdSRichard Henderson 777*ed4cfbcdSRichard Henderson trace_guest_mem_before_exec(env_cpu(env), ptr, meminfo); 778*ed4cfbcdSRichard Henderson ret = ldsw_p(g2h(ptr)); 779*ed4cfbcdSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), ptr, meminfo); 780*ed4cfbcdSRichard Henderson return ret; 781*ed4cfbcdSRichard Henderson } 782*ed4cfbcdSRichard Henderson 783*ed4cfbcdSRichard Henderson uint32_t cpu_ldl_data(CPUArchState *env, abi_ptr ptr) 784*ed4cfbcdSRichard Henderson { 785*ed4cfbcdSRichard Henderson uint32_t ret; 786*ed4cfbcdSRichard Henderson uint16_t meminfo = trace_mem_get_info(MO_TEUL, MMU_USER_IDX, false); 787*ed4cfbcdSRichard Henderson 788*ed4cfbcdSRichard Henderson trace_guest_mem_before_exec(env_cpu(env), ptr, meminfo); 789*ed4cfbcdSRichard Henderson ret = ldl_p(g2h(ptr)); 790*ed4cfbcdSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), ptr, meminfo); 791*ed4cfbcdSRichard Henderson return ret; 792*ed4cfbcdSRichard Henderson } 793*ed4cfbcdSRichard Henderson 794*ed4cfbcdSRichard Henderson uint64_t cpu_ldq_data(CPUArchState *env, abi_ptr ptr) 795*ed4cfbcdSRichard Henderson { 796*ed4cfbcdSRichard Henderson uint64_t ret; 797*ed4cfbcdSRichard Henderson uint16_t meminfo = trace_mem_get_info(MO_TEQ, MMU_USER_IDX, false); 798*ed4cfbcdSRichard Henderson 799*ed4cfbcdSRichard Henderson trace_guest_mem_before_exec(env_cpu(env), ptr, meminfo); 800*ed4cfbcdSRichard Henderson ret = ldq_p(g2h(ptr)); 801*ed4cfbcdSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), ptr, meminfo); 802*ed4cfbcdSRichard Henderson return ret; 803*ed4cfbcdSRichard Henderson } 804*ed4cfbcdSRichard Henderson 805*ed4cfbcdSRichard Henderson uint32_t cpu_ldub_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr) 806*ed4cfbcdSRichard Henderson { 807*ed4cfbcdSRichard Henderson uint32_t ret; 808*ed4cfbcdSRichard Henderson 809*ed4cfbcdSRichard Henderson set_helper_retaddr(retaddr); 810*ed4cfbcdSRichard Henderson ret = cpu_ldub_data(env, ptr); 811*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 812*ed4cfbcdSRichard Henderson return ret; 813*ed4cfbcdSRichard Henderson } 814*ed4cfbcdSRichard Henderson 815*ed4cfbcdSRichard Henderson int cpu_ldsb_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr) 816*ed4cfbcdSRichard Henderson { 817*ed4cfbcdSRichard Henderson int ret; 818*ed4cfbcdSRichard Henderson 819*ed4cfbcdSRichard Henderson set_helper_retaddr(retaddr); 820*ed4cfbcdSRichard Henderson ret = cpu_ldsb_data(env, ptr); 821*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 822*ed4cfbcdSRichard Henderson return ret; 823*ed4cfbcdSRichard Henderson } 824*ed4cfbcdSRichard Henderson 825*ed4cfbcdSRichard Henderson uint32_t cpu_lduw_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr) 826*ed4cfbcdSRichard Henderson { 827*ed4cfbcdSRichard Henderson uint32_t ret; 828*ed4cfbcdSRichard Henderson 829*ed4cfbcdSRichard Henderson set_helper_retaddr(retaddr); 830*ed4cfbcdSRichard Henderson ret = cpu_lduw_data(env, ptr); 831*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 832*ed4cfbcdSRichard Henderson return ret; 833*ed4cfbcdSRichard Henderson } 834*ed4cfbcdSRichard Henderson 835*ed4cfbcdSRichard Henderson int cpu_ldsw_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr) 836*ed4cfbcdSRichard Henderson { 837*ed4cfbcdSRichard Henderson int ret; 838*ed4cfbcdSRichard Henderson 839*ed4cfbcdSRichard Henderson set_helper_retaddr(retaddr); 840*ed4cfbcdSRichard Henderson ret = cpu_ldsw_data(env, ptr); 841*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 842*ed4cfbcdSRichard Henderson return ret; 843*ed4cfbcdSRichard Henderson } 844*ed4cfbcdSRichard Henderson 845*ed4cfbcdSRichard Henderson uint32_t cpu_ldl_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr) 846*ed4cfbcdSRichard Henderson { 847*ed4cfbcdSRichard Henderson uint32_t ret; 848*ed4cfbcdSRichard Henderson 849*ed4cfbcdSRichard Henderson set_helper_retaddr(retaddr); 850*ed4cfbcdSRichard Henderson ret = cpu_ldl_data(env, ptr); 851*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 852*ed4cfbcdSRichard Henderson return ret; 853*ed4cfbcdSRichard Henderson } 854*ed4cfbcdSRichard Henderson 855*ed4cfbcdSRichard Henderson uint64_t cpu_ldq_data_ra(CPUArchState *env, abi_ptr ptr, uintptr_t retaddr) 856*ed4cfbcdSRichard Henderson { 857*ed4cfbcdSRichard Henderson uint64_t ret; 858*ed4cfbcdSRichard Henderson 859*ed4cfbcdSRichard Henderson set_helper_retaddr(retaddr); 860*ed4cfbcdSRichard Henderson ret = cpu_ldq_data(env, ptr); 861*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 862*ed4cfbcdSRichard Henderson return ret; 863*ed4cfbcdSRichard Henderson } 864*ed4cfbcdSRichard Henderson 865*ed4cfbcdSRichard Henderson void cpu_stb_data(CPUArchState *env, abi_ptr ptr, uint32_t val) 866*ed4cfbcdSRichard Henderson { 867*ed4cfbcdSRichard Henderson uint16_t meminfo = trace_mem_get_info(MO_UB, MMU_USER_IDX, true); 868*ed4cfbcdSRichard Henderson 869*ed4cfbcdSRichard Henderson trace_guest_mem_before_exec(env_cpu(env), ptr, meminfo); 870*ed4cfbcdSRichard Henderson stb_p(g2h(ptr), val); 871*ed4cfbcdSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), ptr, meminfo); 872*ed4cfbcdSRichard Henderson } 873*ed4cfbcdSRichard Henderson 874*ed4cfbcdSRichard Henderson void cpu_stw_data(CPUArchState *env, abi_ptr ptr, uint32_t val) 875*ed4cfbcdSRichard Henderson { 876*ed4cfbcdSRichard Henderson uint16_t meminfo = trace_mem_get_info(MO_TEUW, MMU_USER_IDX, true); 877*ed4cfbcdSRichard Henderson 878*ed4cfbcdSRichard Henderson trace_guest_mem_before_exec(env_cpu(env), ptr, meminfo); 879*ed4cfbcdSRichard Henderson stw_p(g2h(ptr), val); 880*ed4cfbcdSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), ptr, meminfo); 881*ed4cfbcdSRichard Henderson } 882*ed4cfbcdSRichard Henderson 883*ed4cfbcdSRichard Henderson void cpu_stl_data(CPUArchState *env, abi_ptr ptr, uint32_t val) 884*ed4cfbcdSRichard Henderson { 885*ed4cfbcdSRichard Henderson uint16_t meminfo = trace_mem_get_info(MO_TEUL, MMU_USER_IDX, true); 886*ed4cfbcdSRichard Henderson 887*ed4cfbcdSRichard Henderson trace_guest_mem_before_exec(env_cpu(env), ptr, meminfo); 888*ed4cfbcdSRichard Henderson stl_p(g2h(ptr), val); 889*ed4cfbcdSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), ptr, meminfo); 890*ed4cfbcdSRichard Henderson } 891*ed4cfbcdSRichard Henderson 892*ed4cfbcdSRichard Henderson void cpu_stq_data(CPUArchState *env, abi_ptr ptr, uint64_t val) 893*ed4cfbcdSRichard Henderson { 894*ed4cfbcdSRichard Henderson uint16_t meminfo = trace_mem_get_info(MO_TEQ, MMU_USER_IDX, true); 895*ed4cfbcdSRichard Henderson 896*ed4cfbcdSRichard Henderson trace_guest_mem_before_exec(env_cpu(env), ptr, meminfo); 897*ed4cfbcdSRichard Henderson stq_p(g2h(ptr), val); 898*ed4cfbcdSRichard Henderson qemu_plugin_vcpu_mem_cb(env_cpu(env), ptr, meminfo); 899*ed4cfbcdSRichard Henderson } 900*ed4cfbcdSRichard Henderson 901*ed4cfbcdSRichard Henderson void cpu_stb_data_ra(CPUArchState *env, abi_ptr ptr, 902*ed4cfbcdSRichard Henderson uint32_t val, uintptr_t retaddr) 903*ed4cfbcdSRichard Henderson { 904*ed4cfbcdSRichard Henderson set_helper_retaddr(retaddr); 905*ed4cfbcdSRichard Henderson cpu_stb_data(env, ptr, val); 906*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 907*ed4cfbcdSRichard Henderson } 908*ed4cfbcdSRichard Henderson 909*ed4cfbcdSRichard Henderson void cpu_stw_data_ra(CPUArchState *env, abi_ptr ptr, 910*ed4cfbcdSRichard Henderson uint32_t val, uintptr_t retaddr) 911*ed4cfbcdSRichard Henderson { 912*ed4cfbcdSRichard Henderson set_helper_retaddr(retaddr); 913*ed4cfbcdSRichard Henderson cpu_stw_data(env, ptr, val); 914*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 915*ed4cfbcdSRichard Henderson } 916*ed4cfbcdSRichard Henderson 917*ed4cfbcdSRichard Henderson void cpu_stl_data_ra(CPUArchState *env, abi_ptr ptr, 918*ed4cfbcdSRichard Henderson uint32_t val, uintptr_t retaddr) 919*ed4cfbcdSRichard Henderson { 920*ed4cfbcdSRichard Henderson set_helper_retaddr(retaddr); 921*ed4cfbcdSRichard Henderson cpu_stl_data(env, ptr, val); 922*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 923*ed4cfbcdSRichard Henderson } 924*ed4cfbcdSRichard Henderson 925*ed4cfbcdSRichard Henderson void cpu_stq_data_ra(CPUArchState *env, abi_ptr ptr, 926*ed4cfbcdSRichard Henderson uint64_t val, uintptr_t retaddr) 927*ed4cfbcdSRichard Henderson { 928*ed4cfbcdSRichard Henderson set_helper_retaddr(retaddr); 929*ed4cfbcdSRichard Henderson cpu_stq_data(env, ptr, val); 930*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 931*ed4cfbcdSRichard Henderson } 932*ed4cfbcdSRichard Henderson 933*ed4cfbcdSRichard Henderson uint32_t cpu_ldub_code(CPUArchState *env, abi_ptr ptr) 934*ed4cfbcdSRichard Henderson { 935*ed4cfbcdSRichard Henderson uint32_t ret; 936*ed4cfbcdSRichard Henderson 937*ed4cfbcdSRichard Henderson set_helper_retaddr(1); 938*ed4cfbcdSRichard Henderson ret = ldub_p(g2h(ptr)); 939*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 940*ed4cfbcdSRichard Henderson return ret; 941*ed4cfbcdSRichard Henderson } 942*ed4cfbcdSRichard Henderson 943*ed4cfbcdSRichard Henderson uint32_t cpu_lduw_code(CPUArchState *env, abi_ptr ptr) 944*ed4cfbcdSRichard Henderson { 945*ed4cfbcdSRichard Henderson uint32_t ret; 946*ed4cfbcdSRichard Henderson 947*ed4cfbcdSRichard Henderson set_helper_retaddr(1); 948*ed4cfbcdSRichard Henderson ret = lduw_p(g2h(ptr)); 949*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 950*ed4cfbcdSRichard Henderson return ret; 951*ed4cfbcdSRichard Henderson } 952*ed4cfbcdSRichard Henderson 953*ed4cfbcdSRichard Henderson uint32_t cpu_ldl_code(CPUArchState *env, abi_ptr ptr) 954*ed4cfbcdSRichard Henderson { 955*ed4cfbcdSRichard Henderson uint32_t ret; 956*ed4cfbcdSRichard Henderson 957*ed4cfbcdSRichard Henderson set_helper_retaddr(1); 958*ed4cfbcdSRichard Henderson ret = ldl_p(g2h(ptr)); 959*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 960*ed4cfbcdSRichard Henderson return ret; 961*ed4cfbcdSRichard Henderson } 962*ed4cfbcdSRichard Henderson 963*ed4cfbcdSRichard Henderson uint64_t cpu_ldq_code(CPUArchState *env, abi_ptr ptr) 964*ed4cfbcdSRichard Henderson { 965*ed4cfbcdSRichard Henderson uint64_t ret; 966*ed4cfbcdSRichard Henderson 967*ed4cfbcdSRichard Henderson set_helper_retaddr(1); 968*ed4cfbcdSRichard Henderson ret = ldq_p(g2h(ptr)); 969*ed4cfbcdSRichard Henderson clear_helper_retaddr(); 970*ed4cfbcdSRichard Henderson return ret; 971*ed4cfbcdSRichard Henderson } 972*ed4cfbcdSRichard Henderson 973a411d296SPhilippe Mathieu-Daudé /* Do not allow unaligned operations to proceed. Return the host address. */ 974a411d296SPhilippe Mathieu-Daudé static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr, 975a411d296SPhilippe Mathieu-Daudé int size, uintptr_t retaddr) 976a411d296SPhilippe Mathieu-Daudé { 977a411d296SPhilippe Mathieu-Daudé /* Enforce qemu required alignment. */ 978a411d296SPhilippe Mathieu-Daudé if (unlikely(addr & (size - 1))) { 97929a0af61SRichard Henderson cpu_loop_exit_atomic(env_cpu(env), retaddr); 980a411d296SPhilippe Mathieu-Daudé } 98108b97f7fSRichard Henderson void *ret = g2h(addr); 98208b97f7fSRichard Henderson set_helper_retaddr(retaddr); 98308b97f7fSRichard Henderson return ret; 984a411d296SPhilippe Mathieu-Daudé } 985a411d296SPhilippe Mathieu-Daudé 986a411d296SPhilippe Mathieu-Daudé /* Macro to call the above, with local variables from the use context. */ 98734d49937SPeter Maydell #define ATOMIC_MMU_DECLS do {} while (0) 988a411d296SPhilippe Mathieu-Daudé #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, DATA_SIZE, GETPC()) 98908b97f7fSRichard Henderson #define ATOMIC_MMU_CLEANUP do { clear_helper_retaddr(); } while (0) 990504f73f7SAlex Bennée #define ATOMIC_MMU_IDX MMU_USER_IDX 991a411d296SPhilippe Mathieu-Daudé 992a411d296SPhilippe Mathieu-Daudé #define ATOMIC_NAME(X) HELPER(glue(glue(atomic_ ## X, SUFFIX), END)) 993a411d296SPhilippe Mathieu-Daudé #define EXTRA_ARGS 994a411d296SPhilippe Mathieu-Daudé 995cfec3885SEmilio G. Cota #include "atomic_common.inc.c" 996cfec3885SEmilio G. Cota 997a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 1 998a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 999a411d296SPhilippe Mathieu-Daudé 1000a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 2 1001a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 1002a411d296SPhilippe Mathieu-Daudé 1003a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 4 1004a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 1005a411d296SPhilippe Mathieu-Daudé 1006a411d296SPhilippe Mathieu-Daudé #ifdef CONFIG_ATOMIC64 1007a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 8 1008a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 1009a411d296SPhilippe Mathieu-Daudé #endif 1010a411d296SPhilippe Mathieu-Daudé 1011a411d296SPhilippe Mathieu-Daudé /* The following is only callable from other helpers, and matches up 1012a411d296SPhilippe Mathieu-Daudé with the softmmu version. */ 1013a411d296SPhilippe Mathieu-Daudé 1014e6cd4bb5SRichard Henderson #if HAVE_ATOMIC128 || HAVE_CMPXCHG128 1015a411d296SPhilippe Mathieu-Daudé 1016a411d296SPhilippe Mathieu-Daudé #undef EXTRA_ARGS 1017a411d296SPhilippe Mathieu-Daudé #undef ATOMIC_NAME 1018a411d296SPhilippe Mathieu-Daudé #undef ATOMIC_MMU_LOOKUP 1019a411d296SPhilippe Mathieu-Daudé 1020a411d296SPhilippe Mathieu-Daudé #define EXTRA_ARGS , TCGMemOpIdx oi, uintptr_t retaddr 1021a411d296SPhilippe Mathieu-Daudé #define ATOMIC_NAME(X) \ 1022a411d296SPhilippe Mathieu-Daudé HELPER(glue(glue(glue(atomic_ ## X, SUFFIX), END), _mmu)) 1023a411d296SPhilippe Mathieu-Daudé #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, DATA_SIZE, retaddr) 1024a411d296SPhilippe Mathieu-Daudé 1025a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 16 1026a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 1027e6cd4bb5SRichard Henderson #endif 1028