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 942a623c7SBlue Swirl * version 2 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" 27*a411d296SPhilippe Mathieu-Daudé #include "exec/helper-proto.h" 2842a623c7SBlue Swirl 2942a623c7SBlue Swirl #undef EAX 3042a623c7SBlue Swirl #undef ECX 3142a623c7SBlue Swirl #undef EDX 3242a623c7SBlue Swirl #undef EBX 3342a623c7SBlue Swirl #undef ESP 3442a623c7SBlue Swirl #undef EBP 3542a623c7SBlue Swirl #undef ESI 3642a623c7SBlue Swirl #undef EDI 3742a623c7SBlue Swirl #undef EIP 3842a623c7SBlue Swirl #ifdef __linux__ 3942a623c7SBlue Swirl #include <sys/ucontext.h> 4042a623c7SBlue Swirl #endif 4142a623c7SBlue Swirl 4242a623c7SBlue Swirl //#define DEBUG_SIGNAL 4342a623c7SBlue Swirl 4442a623c7SBlue Swirl /* exit the current TB from a signal handler. The host registers are 4542a623c7SBlue Swirl restored in a state compatible with the CPU emulator 4642a623c7SBlue Swirl */ 47a5852dc5SPeter Maydell static void cpu_exit_tb_from_sighandler(CPUState *cpu, sigset_t *old_set) 4842a623c7SBlue Swirl { 4942a623c7SBlue Swirl /* XXX: use siglongjmp ? */ 50a5852dc5SPeter Maydell sigprocmask(SIG_SETMASK, old_set, NULL); 516886b980SPeter Maydell cpu_loop_exit_noexc(cpu); 5242a623c7SBlue Swirl } 5342a623c7SBlue Swirl 5442a623c7SBlue Swirl /* 'pc' is the host PC at which the exception was raised. 'address' is 5542a623c7SBlue Swirl the effective address of the memory exception. 'is_write' is 1 if a 5642a623c7SBlue Swirl write caused the exception and otherwise 0'. 'old_set' is the 5742a623c7SBlue Swirl signal set which should be restored */ 5820503968SBlue Swirl static inline int handle_cpu_signal(uintptr_t pc, unsigned long address, 59a5852dc5SPeter Maydell int is_write, sigset_t *old_set) 6042a623c7SBlue Swirl { 6102bed6bdSAlex Bennée CPUState *cpu = current_cpu; 627510454eSAndreas Färber CPUClass *cc; 6342a623c7SBlue Swirl int ret; 6442a623c7SBlue Swirl 6502bed6bdSAlex Bennée /* For synchronous signals we expect to be coming from the vCPU 6602bed6bdSAlex Bennée * thread (so current_cpu should be valid) and either from running 6702bed6bdSAlex Bennée * code or during translation which can fault as we cross pages. 6802bed6bdSAlex Bennée * 6902bed6bdSAlex Bennée * If neither is true then something has gone wrong and we should 7002bed6bdSAlex Bennée * abort rather than try and restart the vCPU execution. 7102bed6bdSAlex Bennée */ 7202bed6bdSAlex Bennée if (!cpu || !cpu->running) { 7302bed6bdSAlex Bennée printf("qemu:%s received signal outside vCPU context @ pc=0x%" 7402bed6bdSAlex Bennée PRIxPTR "\n", __func__, pc); 7502bed6bdSAlex Bennée abort(); 7602bed6bdSAlex Bennée } 7702bed6bdSAlex Bennée 7842a623c7SBlue Swirl #if defined(DEBUG_SIGNAL) 7971baf787SPeter Maydell printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n", 8042a623c7SBlue Swirl pc, address, is_write, *(unsigned long *)old_set); 8142a623c7SBlue Swirl #endif 8242a623c7SBlue Swirl /* XXX: locking issue */ 83f213e72fSPeter Maydell if (is_write && h2g_valid(address)) { 84f213e72fSPeter Maydell switch (page_unprotect(h2g(address), pc)) { 85f213e72fSPeter Maydell case 0: 86f213e72fSPeter Maydell /* Fault not caused by a page marked unwritable to protect 87f213e72fSPeter Maydell * cached translations, must be the guest binary's problem 88f213e72fSPeter Maydell */ 89f213e72fSPeter Maydell break; 90f213e72fSPeter Maydell case 1: 91f213e72fSPeter Maydell /* Fault caused by protection of cached translation; TBs 92f213e72fSPeter Maydell * invalidated, so resume execution 93f213e72fSPeter Maydell */ 9442a623c7SBlue Swirl return 1; 95f213e72fSPeter Maydell case 2: 96f213e72fSPeter Maydell /* Fault caused by protection of cached translation, and the 97f213e72fSPeter Maydell * currently executing TB was modified and must be exited 98f213e72fSPeter Maydell * immediately. 99f213e72fSPeter Maydell */ 10002bed6bdSAlex Bennée cpu_exit_tb_from_sighandler(cpu, old_set); 101f213e72fSPeter Maydell g_assert_not_reached(); 102f213e72fSPeter Maydell default: 103f213e72fSPeter Maydell g_assert_not_reached(); 104f213e72fSPeter Maydell } 10542a623c7SBlue Swirl } 10642a623c7SBlue Swirl 107732f9e89SAlexander Graf /* Convert forcefully to guest address space, invalid addresses 108732f9e89SAlexander Graf are still valid segv ones */ 109732f9e89SAlexander Graf address = h2g_nocheck(address); 110732f9e89SAlexander Graf 1117510454eSAndreas Färber cc = CPU_GET_CLASS(cpu); 11242a623c7SBlue Swirl /* see if it is an MMU fault */ 1137510454eSAndreas Färber g_assert(cc->handle_mmu_fault); 1147510454eSAndreas Färber ret = cc->handle_mmu_fault(cpu, address, is_write, MMU_USER_IDX); 11542a623c7SBlue Swirl if (ret < 0) { 11642a623c7SBlue Swirl return 0; /* not an MMU fault */ 11742a623c7SBlue Swirl } 11842a623c7SBlue Swirl if (ret == 0) { 11942a623c7SBlue Swirl return 1; /* the MMU fault was handled without causing real CPU fault */ 12042a623c7SBlue Swirl } 12101ecaf43SRichard Henderson 12201ecaf43SRichard Henderson /* Now we have a real cpu fault. Since this is the exact location of 12301ecaf43SRichard Henderson * the exception, we must undo the adjustment done by cpu_restore_state 12401ecaf43SRichard Henderson * for handling call return addresses. */ 12501ecaf43SRichard Henderson cpu_restore_state(cpu, pc + GETPC_ADJ); 12642a623c7SBlue Swirl 12742a623c7SBlue Swirl sigprocmask(SIG_SETMASK, old_set, NULL); 1280c33682dSPeter Maydell cpu_loop_exit(cpu); 12942a623c7SBlue Swirl 13042a623c7SBlue Swirl /* never comes here */ 13142a623c7SBlue Swirl return 1; 13242a623c7SBlue Swirl } 13342a623c7SBlue Swirl 13442a623c7SBlue Swirl #if defined(__i386__) 13542a623c7SBlue Swirl 136c5679026SPeter Maydell #if defined(__NetBSD__) 13742a623c7SBlue Swirl #include <ucontext.h> 13842a623c7SBlue Swirl 13942a623c7SBlue Swirl #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP]) 14042a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO]) 14142a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR]) 14242a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 14342a623c7SBlue Swirl #elif defined(__FreeBSD__) || defined(__DragonFly__) 14442a623c7SBlue Swirl #include <ucontext.h> 14542a623c7SBlue Swirl 14642a623c7SBlue Swirl #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip)) 14742a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno) 14842a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.mc_err) 14942a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 15042a623c7SBlue Swirl #elif defined(__OpenBSD__) 15142a623c7SBlue Swirl #define EIP_sig(context) ((context)->sc_eip) 15242a623c7SBlue Swirl #define TRAP_sig(context) ((context)->sc_trapno) 15342a623c7SBlue Swirl #define ERROR_sig(context) ((context)->sc_err) 15442a623c7SBlue Swirl #define MASK_sig(context) ((context)->sc_mask) 15542a623c7SBlue Swirl #else 15642a623c7SBlue Swirl #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP]) 15742a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO]) 15842a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR]) 15942a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 16042a623c7SBlue Swirl #endif 16142a623c7SBlue Swirl 16242a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 16342a623c7SBlue Swirl void *puc) 16442a623c7SBlue Swirl { 16542a623c7SBlue Swirl siginfo_t *info = pinfo; 16642a623c7SBlue Swirl #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) 16742a623c7SBlue Swirl ucontext_t *uc = puc; 16842a623c7SBlue Swirl #elif defined(__OpenBSD__) 16942a623c7SBlue Swirl struct sigcontext *uc = puc; 17042a623c7SBlue Swirl #else 17104b33e21SKhem Raj ucontext_t *uc = puc; 17242a623c7SBlue Swirl #endif 17342a623c7SBlue Swirl unsigned long pc; 17442a623c7SBlue Swirl int trapno; 17542a623c7SBlue Swirl 17642a623c7SBlue Swirl #ifndef REG_EIP 17742a623c7SBlue Swirl /* for glibc 2.1 */ 17842a623c7SBlue Swirl #define REG_EIP EIP 17942a623c7SBlue Swirl #define REG_ERR ERR 18042a623c7SBlue Swirl #define REG_TRAPNO TRAPNO 18142a623c7SBlue Swirl #endif 18242a623c7SBlue Swirl pc = EIP_sig(uc); 18342a623c7SBlue Swirl trapno = TRAP_sig(uc); 18442a623c7SBlue Swirl return handle_cpu_signal(pc, (unsigned long)info->si_addr, 18542a623c7SBlue Swirl trapno == 0xe ? 18642a623c7SBlue Swirl (ERROR_sig(uc) >> 1) & 1 : 0, 187a5852dc5SPeter Maydell &MASK_sig(uc)); 18842a623c7SBlue Swirl } 18942a623c7SBlue Swirl 19042a623c7SBlue Swirl #elif defined(__x86_64__) 19142a623c7SBlue Swirl 19242a623c7SBlue Swirl #ifdef __NetBSD__ 19342a623c7SBlue Swirl #define PC_sig(context) _UC_MACHINE_PC(context) 19442a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO]) 19542a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR]) 19642a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 19742a623c7SBlue Swirl #elif defined(__OpenBSD__) 19842a623c7SBlue Swirl #define PC_sig(context) ((context)->sc_rip) 19942a623c7SBlue Swirl #define TRAP_sig(context) ((context)->sc_trapno) 20042a623c7SBlue Swirl #define ERROR_sig(context) ((context)->sc_err) 20142a623c7SBlue Swirl #define MASK_sig(context) ((context)->sc_mask) 20242a623c7SBlue Swirl #elif defined(__FreeBSD__) || defined(__DragonFly__) 20342a623c7SBlue Swirl #include <ucontext.h> 20442a623c7SBlue Swirl 20542a623c7SBlue Swirl #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip)) 20642a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno) 20742a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.mc_err) 20842a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 20942a623c7SBlue Swirl #else 21042a623c7SBlue Swirl #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP]) 21142a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO]) 21242a623c7SBlue Swirl #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR]) 21342a623c7SBlue Swirl #define MASK_sig(context) ((context)->uc_sigmask) 21442a623c7SBlue Swirl #endif 21542a623c7SBlue Swirl 21642a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 21742a623c7SBlue Swirl void *puc) 21842a623c7SBlue Swirl { 21942a623c7SBlue Swirl siginfo_t *info = pinfo; 22042a623c7SBlue Swirl unsigned long pc; 22142a623c7SBlue Swirl #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) 22242a623c7SBlue Swirl ucontext_t *uc = puc; 22342a623c7SBlue Swirl #elif defined(__OpenBSD__) 22442a623c7SBlue Swirl struct sigcontext *uc = puc; 22542a623c7SBlue Swirl #else 22604b33e21SKhem Raj ucontext_t *uc = puc; 22742a623c7SBlue Swirl #endif 22842a623c7SBlue Swirl 22942a623c7SBlue Swirl pc = PC_sig(uc); 23042a623c7SBlue Swirl return handle_cpu_signal(pc, (unsigned long)info->si_addr, 23142a623c7SBlue Swirl TRAP_sig(uc) == 0xe ? 23242a623c7SBlue Swirl (ERROR_sig(uc) >> 1) & 1 : 0, 233a5852dc5SPeter Maydell &MASK_sig(uc)); 23442a623c7SBlue Swirl } 23542a623c7SBlue Swirl 23642a623c7SBlue Swirl #elif defined(_ARCH_PPC) 23742a623c7SBlue Swirl 23842a623c7SBlue Swirl /*********************************************************************** 23942a623c7SBlue Swirl * signal context platform-specific definitions 24042a623c7SBlue Swirl * From Wine 24142a623c7SBlue Swirl */ 24242a623c7SBlue Swirl #ifdef linux 24342a623c7SBlue Swirl /* All Registers access - only for local access */ 24442a623c7SBlue Swirl #define REG_sig(reg_name, context) \ 24542a623c7SBlue Swirl ((context)->uc_mcontext.regs->reg_name) 24642a623c7SBlue Swirl /* Gpr Registers access */ 24742a623c7SBlue Swirl #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context) 24842a623c7SBlue Swirl /* Program counter */ 24942a623c7SBlue Swirl #define IAR_sig(context) REG_sig(nip, context) 25042a623c7SBlue Swirl /* Machine State Register (Supervisor) */ 25142a623c7SBlue Swirl #define MSR_sig(context) REG_sig(msr, context) 25242a623c7SBlue Swirl /* Count register */ 25342a623c7SBlue Swirl #define CTR_sig(context) REG_sig(ctr, context) 25442a623c7SBlue Swirl /* User's integer exception register */ 25542a623c7SBlue Swirl #define XER_sig(context) REG_sig(xer, context) 25642a623c7SBlue Swirl /* Link register */ 25742a623c7SBlue Swirl #define LR_sig(context) REG_sig(link, context) 25842a623c7SBlue Swirl /* Condition register */ 25942a623c7SBlue Swirl #define CR_sig(context) REG_sig(ccr, context) 26042a623c7SBlue Swirl 26142a623c7SBlue Swirl /* Float Registers access */ 26242a623c7SBlue Swirl #define FLOAT_sig(reg_num, context) \ 26342a623c7SBlue Swirl (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num]) 26442a623c7SBlue Swirl #define FPSCR_sig(context) \ 26542a623c7SBlue Swirl (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4))) 26642a623c7SBlue Swirl /* Exception Registers access */ 26742a623c7SBlue Swirl #define DAR_sig(context) REG_sig(dar, context) 26842a623c7SBlue Swirl #define DSISR_sig(context) REG_sig(dsisr, context) 26942a623c7SBlue Swirl #define TRAP_sig(context) REG_sig(trap, context) 27042a623c7SBlue Swirl #endif /* linux */ 27142a623c7SBlue Swirl 27242a623c7SBlue Swirl #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 27342a623c7SBlue Swirl #include <ucontext.h> 27442a623c7SBlue Swirl #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0) 27542a623c7SBlue Swirl #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1) 27642a623c7SBlue Swirl #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr) 27742a623c7SBlue Swirl #define XER_sig(context) ((context)->uc_mcontext.mc_xer) 27842a623c7SBlue Swirl #define LR_sig(context) ((context)->uc_mcontext.mc_lr) 27942a623c7SBlue Swirl #define CR_sig(context) ((context)->uc_mcontext.mc_cr) 28042a623c7SBlue Swirl /* Exception Registers access */ 28142a623c7SBlue Swirl #define DAR_sig(context) ((context)->uc_mcontext.mc_dar) 28242a623c7SBlue Swirl #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr) 28342a623c7SBlue Swirl #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc) 28442a623c7SBlue Swirl #endif /* __FreeBSD__|| __FreeBSD_kernel__ */ 28542a623c7SBlue Swirl 28642a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 28742a623c7SBlue Swirl void *puc) 28842a623c7SBlue Swirl { 28942a623c7SBlue Swirl siginfo_t *info = pinfo; 29042a623c7SBlue Swirl #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) 29142a623c7SBlue Swirl ucontext_t *uc = puc; 29242a623c7SBlue Swirl #else 29304b33e21SKhem Raj ucontext_t *uc = puc; 29442a623c7SBlue Swirl #endif 29542a623c7SBlue Swirl unsigned long pc; 29642a623c7SBlue Swirl int is_write; 29742a623c7SBlue Swirl 29842a623c7SBlue Swirl pc = IAR_sig(uc); 29942a623c7SBlue Swirl is_write = 0; 30042a623c7SBlue Swirl #if 0 30142a623c7SBlue Swirl /* ppc 4xx case */ 30242a623c7SBlue Swirl if (DSISR_sig(uc) & 0x00800000) { 30342a623c7SBlue Swirl is_write = 1; 30442a623c7SBlue Swirl } 30542a623c7SBlue Swirl #else 30642a623c7SBlue Swirl if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) { 30742a623c7SBlue Swirl is_write = 1; 30842a623c7SBlue Swirl } 30942a623c7SBlue Swirl #endif 31042a623c7SBlue Swirl return handle_cpu_signal(pc, (unsigned long)info->si_addr, 311a5852dc5SPeter Maydell is_write, &uc->uc_sigmask); 31242a623c7SBlue Swirl } 31342a623c7SBlue Swirl 31442a623c7SBlue Swirl #elif defined(__alpha__) 31542a623c7SBlue Swirl 31642a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 31742a623c7SBlue Swirl void *puc) 31842a623c7SBlue Swirl { 31942a623c7SBlue Swirl siginfo_t *info = pinfo; 32004b33e21SKhem Raj ucontext_t *uc = puc; 32142a623c7SBlue Swirl uint32_t *pc = uc->uc_mcontext.sc_pc; 32242a623c7SBlue Swirl uint32_t insn = *pc; 32342a623c7SBlue Swirl int is_write = 0; 32442a623c7SBlue Swirl 32542a623c7SBlue Swirl /* XXX: need kernel patch to get write flag faster */ 32642a623c7SBlue Swirl switch (insn >> 26) { 32742a623c7SBlue Swirl case 0x0d: /* stw */ 32842a623c7SBlue Swirl case 0x0e: /* stb */ 32942a623c7SBlue Swirl case 0x0f: /* stq_u */ 33042a623c7SBlue Swirl case 0x24: /* stf */ 33142a623c7SBlue Swirl case 0x25: /* stg */ 33242a623c7SBlue Swirl case 0x26: /* sts */ 33342a623c7SBlue Swirl case 0x27: /* stt */ 33442a623c7SBlue Swirl case 0x2c: /* stl */ 33542a623c7SBlue Swirl case 0x2d: /* stq */ 33642a623c7SBlue Swirl case 0x2e: /* stl_c */ 33742a623c7SBlue Swirl case 0x2f: /* stq_c */ 33842a623c7SBlue Swirl is_write = 1; 33942a623c7SBlue Swirl } 34042a623c7SBlue Swirl 34142a623c7SBlue Swirl return handle_cpu_signal(pc, (unsigned long)info->si_addr, 342a5852dc5SPeter Maydell is_write, &uc->uc_sigmask); 34342a623c7SBlue Swirl } 34442a623c7SBlue Swirl #elif defined(__sparc__) 34542a623c7SBlue Swirl 34642a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 34742a623c7SBlue Swirl void *puc) 34842a623c7SBlue Swirl { 34942a623c7SBlue Swirl siginfo_t *info = pinfo; 35042a623c7SBlue Swirl int is_write; 35142a623c7SBlue Swirl uint32_t insn; 35242a623c7SBlue Swirl #if !defined(__arch64__) || defined(CONFIG_SOLARIS) 35342a623c7SBlue Swirl uint32_t *regs = (uint32_t *)(info + 1); 35442a623c7SBlue Swirl void *sigmask = (regs + 20); 35542a623c7SBlue Swirl /* XXX: is there a standard glibc define ? */ 35642a623c7SBlue Swirl unsigned long pc = regs[1]; 35742a623c7SBlue Swirl #else 35842a623c7SBlue Swirl #ifdef __linux__ 35942a623c7SBlue Swirl struct sigcontext *sc = puc; 36042a623c7SBlue Swirl unsigned long pc = sc->sigc_regs.tpc; 36142a623c7SBlue Swirl void *sigmask = (void *)sc->sigc_mask; 36242a623c7SBlue Swirl #elif defined(__OpenBSD__) 36342a623c7SBlue Swirl struct sigcontext *uc = puc; 36442a623c7SBlue Swirl unsigned long pc = uc->sc_pc; 36542a623c7SBlue Swirl void *sigmask = (void *)(long)uc->sc_mask; 3667ccfb495STobias Nygren #elif defined(__NetBSD__) 3677ccfb495STobias Nygren ucontext_t *uc = puc; 3687ccfb495STobias Nygren unsigned long pc = _UC_MACHINE_PC(uc); 3697ccfb495STobias Nygren void *sigmask = (void *)&uc->uc_sigmask; 37042a623c7SBlue Swirl #endif 37142a623c7SBlue Swirl #endif 37242a623c7SBlue Swirl 37342a623c7SBlue Swirl /* XXX: need kernel patch to get write flag faster */ 37442a623c7SBlue Swirl is_write = 0; 37542a623c7SBlue Swirl insn = *(uint32_t *)pc; 37642a623c7SBlue Swirl if ((insn >> 30) == 3) { 37742a623c7SBlue Swirl switch ((insn >> 19) & 0x3f) { 37842a623c7SBlue Swirl case 0x05: /* stb */ 37942a623c7SBlue Swirl case 0x15: /* stba */ 38042a623c7SBlue Swirl case 0x06: /* sth */ 38142a623c7SBlue Swirl case 0x16: /* stha */ 38242a623c7SBlue Swirl case 0x04: /* st */ 38342a623c7SBlue Swirl case 0x14: /* sta */ 38442a623c7SBlue Swirl case 0x07: /* std */ 38542a623c7SBlue Swirl case 0x17: /* stda */ 38642a623c7SBlue Swirl case 0x0e: /* stx */ 38742a623c7SBlue Swirl case 0x1e: /* stxa */ 38842a623c7SBlue Swirl case 0x24: /* stf */ 38942a623c7SBlue Swirl case 0x34: /* stfa */ 39042a623c7SBlue Swirl case 0x27: /* stdf */ 39142a623c7SBlue Swirl case 0x37: /* stdfa */ 39242a623c7SBlue Swirl case 0x26: /* stqf */ 39342a623c7SBlue Swirl case 0x36: /* stqfa */ 39442a623c7SBlue Swirl case 0x25: /* stfsr */ 39542a623c7SBlue Swirl case 0x3c: /* casa */ 39642a623c7SBlue Swirl case 0x3e: /* casxa */ 39742a623c7SBlue Swirl is_write = 1; 39842a623c7SBlue Swirl break; 39942a623c7SBlue Swirl } 40042a623c7SBlue Swirl } 40142a623c7SBlue Swirl return handle_cpu_signal(pc, (unsigned long)info->si_addr, 402a5852dc5SPeter Maydell is_write, sigmask); 40342a623c7SBlue Swirl } 40442a623c7SBlue Swirl 40542a623c7SBlue Swirl #elif defined(__arm__) 40642a623c7SBlue Swirl 4077ccfb495STobias Nygren #if defined(__NetBSD__) 4087ccfb495STobias Nygren #include <ucontext.h> 4097ccfb495STobias Nygren #endif 4107ccfb495STobias Nygren 41142a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 41242a623c7SBlue Swirl void *puc) 41342a623c7SBlue Swirl { 41442a623c7SBlue Swirl siginfo_t *info = pinfo; 4157ccfb495STobias Nygren #if defined(__NetBSD__) 4167ccfb495STobias Nygren ucontext_t *uc = puc; 4177ccfb495STobias Nygren #else 41804b33e21SKhem Raj ucontext_t *uc = puc; 4197ccfb495STobias Nygren #endif 42042a623c7SBlue Swirl unsigned long pc; 42142a623c7SBlue Swirl int is_write; 42242a623c7SBlue Swirl 4237ccfb495STobias Nygren #if defined(__NetBSD__) 4247ccfb495STobias Nygren pc = uc->uc_mcontext.__gregs[_REG_R15]; 4257ccfb495STobias Nygren #elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) 42642a623c7SBlue Swirl pc = uc->uc_mcontext.gregs[R15]; 42742a623c7SBlue Swirl #else 42842a623c7SBlue Swirl pc = uc->uc_mcontext.arm_pc; 42942a623c7SBlue Swirl #endif 430023b0ae3SPeter Maydell 431023b0ae3SPeter Maydell /* error_code is the FSR value, in which bit 11 is WnR (assuming a v6 or 432023b0ae3SPeter Maydell * later processor; on v5 we will always report this as a read). 433023b0ae3SPeter Maydell */ 434023b0ae3SPeter Maydell is_write = extract32(uc->uc_mcontext.error_code, 11, 1); 43542a623c7SBlue Swirl return handle_cpu_signal(pc, (unsigned long)info->si_addr, 43642a623c7SBlue Swirl is_write, 437a5852dc5SPeter Maydell &uc->uc_sigmask); 43842a623c7SBlue Swirl } 43942a623c7SBlue Swirl 440f129061cSClaudio Fontana #elif defined(__aarch64__) 441f129061cSClaudio Fontana 442661f7fa4SRichard Henderson int cpu_signal_handler(int host_signum, void *pinfo, void *puc) 443f129061cSClaudio Fontana { 444f129061cSClaudio Fontana siginfo_t *info = pinfo; 44504b33e21SKhem Raj ucontext_t *uc = puc; 446661f7fa4SRichard Henderson uintptr_t pc = uc->uc_mcontext.pc; 447661f7fa4SRichard Henderson uint32_t insn = *(uint32_t *)pc; 448661f7fa4SRichard Henderson bool is_write; 449f129061cSClaudio Fontana 450661f7fa4SRichard Henderson /* XXX: need kernel patch to get write flag faster. */ 451661f7fa4SRichard Henderson is_write = ( (insn & 0xbfff0000) == 0x0c000000 /* C3.3.1 */ 452661f7fa4SRichard Henderson || (insn & 0xbfe00000) == 0x0c800000 /* C3.3.2 */ 453661f7fa4SRichard Henderson || (insn & 0xbfdf0000) == 0x0d000000 /* C3.3.3 */ 454661f7fa4SRichard Henderson || (insn & 0xbfc00000) == 0x0d800000 /* C3.3.4 */ 455661f7fa4SRichard Henderson || (insn & 0x3f400000) == 0x08000000 /* C3.3.6 */ 456661f7fa4SRichard Henderson || (insn & 0x3bc00000) == 0x39000000 /* C3.3.13 */ 457661f7fa4SRichard Henderson || (insn & 0x3fc00000) == 0x3d800000 /* ... 128bit */ 458661f7fa4SRichard Henderson /* Ingore bits 10, 11 & 21, controlling indexing. */ 459661f7fa4SRichard Henderson || (insn & 0x3bc00000) == 0x38000000 /* C3.3.8-12 */ 460661f7fa4SRichard Henderson || (insn & 0x3fe00000) == 0x3c800000 /* ... 128bit */ 461661f7fa4SRichard Henderson /* Ignore bits 23 & 24, controlling indexing. */ 462661f7fa4SRichard Henderson || (insn & 0x3a400000) == 0x28000000); /* C3.3.7,14-16 */ 463661f7fa4SRichard Henderson 464661f7fa4SRichard Henderson return handle_cpu_signal(pc, (uintptr_t)info->si_addr, 465a5852dc5SPeter Maydell is_write, &uc->uc_sigmask); 466f129061cSClaudio Fontana } 467f129061cSClaudio Fontana 46842a623c7SBlue Swirl #elif defined(__ia64) 46942a623c7SBlue Swirl 47042a623c7SBlue Swirl #ifndef __ISR_VALID 47142a623c7SBlue Swirl /* This ought to be in <bits/siginfo.h>... */ 47242a623c7SBlue Swirl # define __ISR_VALID 1 47342a623c7SBlue Swirl #endif 47442a623c7SBlue Swirl 47542a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, void *puc) 47642a623c7SBlue Swirl { 47742a623c7SBlue Swirl siginfo_t *info = pinfo; 47804b33e21SKhem Raj ucontext_t *uc = puc; 47942a623c7SBlue Swirl unsigned long ip; 48042a623c7SBlue Swirl int is_write = 0; 48142a623c7SBlue Swirl 48242a623c7SBlue Swirl ip = uc->uc_mcontext.sc_ip; 48342a623c7SBlue Swirl switch (host_signum) { 48442a623c7SBlue Swirl case SIGILL: 48542a623c7SBlue Swirl case SIGFPE: 48642a623c7SBlue Swirl case SIGSEGV: 48742a623c7SBlue Swirl case SIGBUS: 48842a623c7SBlue Swirl case SIGTRAP: 48942a623c7SBlue Swirl if (info->si_code && (info->si_segvflags & __ISR_VALID)) { 49042a623c7SBlue Swirl /* ISR.W (write-access) is bit 33: */ 49142a623c7SBlue Swirl is_write = (info->si_isr >> 33) & 1; 49242a623c7SBlue Swirl } 49342a623c7SBlue Swirl break; 49442a623c7SBlue Swirl 49542a623c7SBlue Swirl default: 49642a623c7SBlue Swirl break; 49742a623c7SBlue Swirl } 49842a623c7SBlue Swirl return handle_cpu_signal(ip, (unsigned long)info->si_addr, 49942a623c7SBlue Swirl is_write, 500a5852dc5SPeter Maydell (sigset_t *)&uc->uc_sigmask); 50142a623c7SBlue Swirl } 50242a623c7SBlue Swirl 50342a623c7SBlue Swirl #elif defined(__s390__) 50442a623c7SBlue Swirl 50542a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 50642a623c7SBlue Swirl void *puc) 50742a623c7SBlue Swirl { 50842a623c7SBlue Swirl siginfo_t *info = pinfo; 50904b33e21SKhem Raj ucontext_t *uc = puc; 51042a623c7SBlue Swirl unsigned long pc; 51142a623c7SBlue Swirl uint16_t *pinsn; 51242a623c7SBlue Swirl int is_write = 0; 51342a623c7SBlue Swirl 51442a623c7SBlue Swirl pc = uc->uc_mcontext.psw.addr; 51542a623c7SBlue Swirl 51642a623c7SBlue Swirl /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead 51742a623c7SBlue Swirl of the normal 2 arguments. The 3rd argument contains the "int_code" 51842a623c7SBlue Swirl from the hardware which does in fact contain the is_write value. 51942a623c7SBlue Swirl The rt signal handler, as far as I can tell, does not give this value 52042a623c7SBlue Swirl at all. Not that we could get to it from here even if it were. */ 52142a623c7SBlue Swirl /* ??? This is not even close to complete, since it ignores all 52242a623c7SBlue Swirl of the read-modify-write instructions. */ 52342a623c7SBlue Swirl pinsn = (uint16_t *)pc; 52442a623c7SBlue Swirl switch (pinsn[0] >> 8) { 52542a623c7SBlue Swirl case 0x50: /* ST */ 52642a623c7SBlue Swirl case 0x42: /* STC */ 52742a623c7SBlue Swirl case 0x40: /* STH */ 52842a623c7SBlue Swirl is_write = 1; 52942a623c7SBlue Swirl break; 53042a623c7SBlue Swirl case 0xc4: /* RIL format insns */ 53142a623c7SBlue Swirl switch (pinsn[0] & 0xf) { 53242a623c7SBlue Swirl case 0xf: /* STRL */ 53342a623c7SBlue Swirl case 0xb: /* STGRL */ 53442a623c7SBlue Swirl case 0x7: /* STHRL */ 53542a623c7SBlue Swirl is_write = 1; 53642a623c7SBlue Swirl } 53742a623c7SBlue Swirl break; 53842a623c7SBlue Swirl case 0xe3: /* RXY format insns */ 53942a623c7SBlue Swirl switch (pinsn[2] & 0xff) { 54042a623c7SBlue Swirl case 0x50: /* STY */ 54142a623c7SBlue Swirl case 0x24: /* STG */ 54242a623c7SBlue Swirl case 0x72: /* STCY */ 54342a623c7SBlue Swirl case 0x70: /* STHY */ 54442a623c7SBlue Swirl case 0x8e: /* STPQ */ 54542a623c7SBlue Swirl case 0x3f: /* STRVH */ 54642a623c7SBlue Swirl case 0x3e: /* STRV */ 54742a623c7SBlue Swirl case 0x2f: /* STRVG */ 54842a623c7SBlue Swirl is_write = 1; 54942a623c7SBlue Swirl } 55042a623c7SBlue Swirl break; 55142a623c7SBlue Swirl } 55242a623c7SBlue Swirl return handle_cpu_signal(pc, (unsigned long)info->si_addr, 553a5852dc5SPeter Maydell is_write, &uc->uc_sigmask); 55442a623c7SBlue Swirl } 55542a623c7SBlue Swirl 55642a623c7SBlue Swirl #elif defined(__mips__) 55742a623c7SBlue Swirl 55842a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo, 55942a623c7SBlue Swirl void *puc) 56042a623c7SBlue Swirl { 56142a623c7SBlue Swirl siginfo_t *info = pinfo; 56204b33e21SKhem Raj ucontext_t *uc = puc; 56342a623c7SBlue Swirl greg_t pc = uc->uc_mcontext.pc; 56442a623c7SBlue Swirl int is_write; 56542a623c7SBlue Swirl 56642a623c7SBlue Swirl /* XXX: compute is_write */ 56742a623c7SBlue Swirl is_write = 0; 56842a623c7SBlue Swirl return handle_cpu_signal(pc, (unsigned long)info->si_addr, 569a5852dc5SPeter Maydell is_write, &uc->uc_sigmask); 57042a623c7SBlue Swirl } 57142a623c7SBlue Swirl 57242a623c7SBlue Swirl #else 57342a623c7SBlue Swirl 57442a623c7SBlue Swirl #error host CPU specific signal handler needed 57542a623c7SBlue Swirl 57642a623c7SBlue Swirl #endif 577*a411d296SPhilippe Mathieu-Daudé 578*a411d296SPhilippe Mathieu-Daudé /* The softmmu versions of these helpers are in cputlb.c. */ 579*a411d296SPhilippe Mathieu-Daudé 580*a411d296SPhilippe Mathieu-Daudé /* Do not allow unaligned operations to proceed. Return the host address. */ 581*a411d296SPhilippe Mathieu-Daudé static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr, 582*a411d296SPhilippe Mathieu-Daudé int size, uintptr_t retaddr) 583*a411d296SPhilippe Mathieu-Daudé { 584*a411d296SPhilippe Mathieu-Daudé /* Enforce qemu required alignment. */ 585*a411d296SPhilippe Mathieu-Daudé if (unlikely(addr & (size - 1))) { 586*a411d296SPhilippe Mathieu-Daudé cpu_loop_exit_atomic(ENV_GET_CPU(env), retaddr); 587*a411d296SPhilippe Mathieu-Daudé } 588*a411d296SPhilippe Mathieu-Daudé return g2h(addr); 589*a411d296SPhilippe Mathieu-Daudé } 590*a411d296SPhilippe Mathieu-Daudé 591*a411d296SPhilippe Mathieu-Daudé /* Macro to call the above, with local variables from the use context. */ 592*a411d296SPhilippe Mathieu-Daudé #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, DATA_SIZE, GETPC()) 593*a411d296SPhilippe Mathieu-Daudé 594*a411d296SPhilippe Mathieu-Daudé #define ATOMIC_NAME(X) HELPER(glue(glue(atomic_ ## X, SUFFIX), END)) 595*a411d296SPhilippe Mathieu-Daudé #define EXTRA_ARGS 596*a411d296SPhilippe Mathieu-Daudé 597*a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 1 598*a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 599*a411d296SPhilippe Mathieu-Daudé 600*a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 2 601*a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 602*a411d296SPhilippe Mathieu-Daudé 603*a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 4 604*a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 605*a411d296SPhilippe Mathieu-Daudé 606*a411d296SPhilippe Mathieu-Daudé #ifdef CONFIG_ATOMIC64 607*a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 8 608*a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 609*a411d296SPhilippe Mathieu-Daudé #endif 610*a411d296SPhilippe Mathieu-Daudé 611*a411d296SPhilippe Mathieu-Daudé /* The following is only callable from other helpers, and matches up 612*a411d296SPhilippe Mathieu-Daudé with the softmmu version. */ 613*a411d296SPhilippe Mathieu-Daudé 614*a411d296SPhilippe Mathieu-Daudé #ifdef CONFIG_ATOMIC128 615*a411d296SPhilippe Mathieu-Daudé 616*a411d296SPhilippe Mathieu-Daudé #undef EXTRA_ARGS 617*a411d296SPhilippe Mathieu-Daudé #undef ATOMIC_NAME 618*a411d296SPhilippe Mathieu-Daudé #undef ATOMIC_MMU_LOOKUP 619*a411d296SPhilippe Mathieu-Daudé 620*a411d296SPhilippe Mathieu-Daudé #define EXTRA_ARGS , TCGMemOpIdx oi, uintptr_t retaddr 621*a411d296SPhilippe Mathieu-Daudé #define ATOMIC_NAME(X) \ 622*a411d296SPhilippe Mathieu-Daudé HELPER(glue(glue(glue(atomic_ ## X, SUFFIX), END), _mmu)) 623*a411d296SPhilippe Mathieu-Daudé #define ATOMIC_MMU_LOOKUP atomic_mmu_lookup(env, addr, DATA_SIZE, retaddr) 624*a411d296SPhilippe Mathieu-Daudé 625*a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 16 626*a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h" 627*a411d296SPhilippe Mathieu-Daudé #endif /* CONFIG_ATOMIC128 */ 628