184778508Sblueswir1 /* 284778508Sblueswir1 * Emulation of BSD signals 384778508Sblueswir1 * 484778508Sblueswir1 * Copyright (c) 2003 - 2008 Fabrice Bellard 51366ef81SWarner Losh * Copyright (c) 2013 Stacey Son 684778508Sblueswir1 * 784778508Sblueswir1 * This program is free software; you can redistribute it and/or modify 884778508Sblueswir1 * it under the terms of the GNU General Public License as published by 984778508Sblueswir1 * the Free Software Foundation; either version 2 of the License, or 1084778508Sblueswir1 * (at your option) any later version. 1184778508Sblueswir1 * 1284778508Sblueswir1 * This program is distributed in the hope that it will be useful, 1384778508Sblueswir1 * but WITHOUT ANY WARRANTY; without even the implied warranty of 1484778508Sblueswir1 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1584778508Sblueswir1 * GNU General Public License for more details. 1684778508Sblueswir1 * 1784778508Sblueswir1 * You should have received a copy of the GNU General Public License 188167ee88SBlue Swirl * along with this program; if not, see <http://www.gnu.org/licenses/>. 1984778508Sblueswir1 */ 2084778508Sblueswir1 215abfac27SWarner Losh #include "qemu/osdep.h" 22cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h" 2384778508Sblueswir1 #include "qemu.h" 24*d96bf49bSAlex Bennée #include "gdbstub/user.h" 250ef59989SWarner Losh #include "signal-common.h" 266ddc1abeSWarner Losh #include "trace.h" 27fc9f9bddSWarner Losh #include "hw/core/tcg-cpu-ops.h" 2885fc1b5dSWarner Losh #include "host-signal.h" 2984778508Sblueswir1 30149076adSWarner Losh static struct target_sigaction sigact_table[TARGET_NSIG]; 31149076adSWarner Losh static void host_signal_handler(int host_sig, siginfo_t *info, void *puc); 32c93cbac1SWarner Losh static void target_to_host_sigset_internal(sigset_t *d, 33c93cbac1SWarner Losh const target_sigset_t *s); 34c93cbac1SWarner Losh 3546f4f76dSWarner Losh static inline int on_sig_stack(TaskState *ts, unsigned long sp) 3646f4f76dSWarner Losh { 3746f4f76dSWarner Losh return sp - ts->sigaltstack_used.ss_sp < ts->sigaltstack_used.ss_size; 3846f4f76dSWarner Losh } 3946f4f76dSWarner Losh 4046f4f76dSWarner Losh static inline int sas_ss_flags(TaskState *ts, unsigned long sp) 4146f4f76dSWarner Losh { 4246f4f76dSWarner Losh return ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE : 4346f4f76dSWarner Losh on_sig_stack(ts, sp) ? SS_ONSTACK : 0; 4446f4f76dSWarner Losh } 45149076adSWarner Losh 46835b04edSWarner Losh /* 471366ef81SWarner Losh * The BSD ABIs use the same singal numbers across all the CPU architectures, so 481366ef81SWarner Losh * (unlike Linux) these functions are just the identity mapping. This might not 491366ef81SWarner Losh * be true for XyzBSD running on AbcBSD, which doesn't currently work. 501366ef81SWarner Losh */ 511366ef81SWarner Losh int host_to_target_signal(int sig) 521366ef81SWarner Losh { 531366ef81SWarner Losh return sig; 541366ef81SWarner Losh } 551366ef81SWarner Losh 561366ef81SWarner Losh int target_to_host_signal(int sig) 571366ef81SWarner Losh { 581366ef81SWarner Losh return sig; 591366ef81SWarner Losh } 601366ef81SWarner Losh 61c93cbac1SWarner Losh static inline void target_sigemptyset(target_sigset_t *set) 62c93cbac1SWarner Losh { 63c93cbac1SWarner Losh memset(set, 0, sizeof(*set)); 64c93cbac1SWarner Losh } 65c93cbac1SWarner Losh 66c93cbac1SWarner Losh static inline void target_sigaddset(target_sigset_t *set, int signum) 67c93cbac1SWarner Losh { 68c93cbac1SWarner Losh signum--; 69c93cbac1SWarner Losh uint32_t mask = (uint32_t)1 << (signum % TARGET_NSIG_BPW); 70c93cbac1SWarner Losh set->__bits[signum / TARGET_NSIG_BPW] |= mask; 71c93cbac1SWarner Losh } 72c93cbac1SWarner Losh 73c93cbac1SWarner Losh static inline int target_sigismember(const target_sigset_t *set, int signum) 74c93cbac1SWarner Losh { 75c93cbac1SWarner Losh signum--; 76c93cbac1SWarner Losh abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW); 77c93cbac1SWarner Losh return (set->__bits[signum / TARGET_NSIG_BPW] & mask) != 0; 78c93cbac1SWarner Losh } 79c93cbac1SWarner Losh 80aae57ac3SWarner Losh /* Adjust the signal context to rewind out of safe-syscall if we're in it */ 81aae57ac3SWarner Losh static inline void rewind_if_in_safe_syscall(void *puc) 82aae57ac3SWarner Losh { 83aae57ac3SWarner Losh ucontext_t *uc = (ucontext_t *)puc; 84aae57ac3SWarner Losh uintptr_t pcreg = host_signal_pc(uc); 85aae57ac3SWarner Losh 86aae57ac3SWarner Losh if (pcreg > (uintptr_t)safe_syscall_start 87aae57ac3SWarner Losh && pcreg < (uintptr_t)safe_syscall_end) { 88aae57ac3SWarner Losh host_signal_set_pc(uc, (uintptr_t)safe_syscall_start); 89aae57ac3SWarner Losh } 90aae57ac3SWarner Losh } 91aae57ac3SWarner Losh 92c93cbac1SWarner Losh /* 93c93cbac1SWarner Losh * Note: The following take advantage of the BSD signal property that all 94c93cbac1SWarner Losh * signals are available on all architectures. 95c93cbac1SWarner Losh */ 96c93cbac1SWarner Losh static void host_to_target_sigset_internal(target_sigset_t *d, 97c93cbac1SWarner Losh const sigset_t *s) 98c93cbac1SWarner Losh { 99c93cbac1SWarner Losh int i; 100c93cbac1SWarner Losh 101c93cbac1SWarner Losh target_sigemptyset(d); 102c93cbac1SWarner Losh for (i = 1; i <= NSIG; i++) { 103c93cbac1SWarner Losh if (sigismember(s, i)) { 104c93cbac1SWarner Losh target_sigaddset(d, host_to_target_signal(i)); 105c93cbac1SWarner Losh } 106c93cbac1SWarner Losh } 107c93cbac1SWarner Losh } 108c93cbac1SWarner Losh 109c93cbac1SWarner Losh void host_to_target_sigset(target_sigset_t *d, const sigset_t *s) 110c93cbac1SWarner Losh { 111c93cbac1SWarner Losh target_sigset_t d1; 112c93cbac1SWarner Losh int i; 113c93cbac1SWarner Losh 114c93cbac1SWarner Losh host_to_target_sigset_internal(&d1, s); 115c93cbac1SWarner Losh for (i = 0; i < _SIG_WORDS; i++) { 116c93cbac1SWarner Losh d->__bits[i] = tswap32(d1.__bits[i]); 117c93cbac1SWarner Losh } 118c93cbac1SWarner Losh } 119c93cbac1SWarner Losh 120c93cbac1SWarner Losh static void target_to_host_sigset_internal(sigset_t *d, 121c93cbac1SWarner Losh const target_sigset_t *s) 122c93cbac1SWarner Losh { 123c93cbac1SWarner Losh int i; 124c93cbac1SWarner Losh 125c93cbac1SWarner Losh sigemptyset(d); 126c93cbac1SWarner Losh for (i = 1; i <= TARGET_NSIG; i++) { 127c93cbac1SWarner Losh if (target_sigismember(s, i)) { 128c93cbac1SWarner Losh sigaddset(d, target_to_host_signal(i)); 129c93cbac1SWarner Losh } 130c93cbac1SWarner Losh } 131c93cbac1SWarner Losh } 132c93cbac1SWarner Losh 133c93cbac1SWarner Losh void target_to_host_sigset(sigset_t *d, const target_sigset_t *s) 134c93cbac1SWarner Losh { 135c93cbac1SWarner Losh target_sigset_t s1; 136c93cbac1SWarner Losh int i; 137c93cbac1SWarner Losh 138c93cbac1SWarner Losh for (i = 0; i < TARGET_NSIG_WORDS; i++) { 139c93cbac1SWarner Losh s1.__bits[i] = tswap32(s->__bits[i]); 140c93cbac1SWarner Losh } 141c93cbac1SWarner Losh target_to_host_sigset_internal(d, &s1); 142c93cbac1SWarner Losh } 143c93cbac1SWarner Losh 144c34f2aafSWarner Losh static bool has_trapno(int tsig) 145c34f2aafSWarner Losh { 146c34f2aafSWarner Losh return tsig == TARGET_SIGILL || 147c34f2aafSWarner Losh tsig == TARGET_SIGFPE || 148c34f2aafSWarner Losh tsig == TARGET_SIGSEGV || 149c34f2aafSWarner Losh tsig == TARGET_SIGBUS || 150c34f2aafSWarner Losh tsig == TARGET_SIGTRAP; 151c34f2aafSWarner Losh } 152c34f2aafSWarner Losh 153c34f2aafSWarner Losh /* Siginfo conversion. */ 154c34f2aafSWarner Losh 155c34f2aafSWarner Losh /* 156c34f2aafSWarner Losh * Populate tinfo w/o swapping based on guessing which fields are valid. 157c34f2aafSWarner Losh */ 158c34f2aafSWarner Losh static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo, 159c34f2aafSWarner Losh const siginfo_t *info) 160c34f2aafSWarner Losh { 161c34f2aafSWarner Losh int sig = host_to_target_signal(info->si_signo); 162c34f2aafSWarner Losh int si_code = info->si_code; 163c34f2aafSWarner Losh int si_type; 164c34f2aafSWarner Losh 165c34f2aafSWarner Losh /* 166c34f2aafSWarner Losh * Make sure we that the variable portion of the target siginfo is zeroed 167c34f2aafSWarner Losh * out so we don't leak anything into that. 168c34f2aafSWarner Losh */ 169c34f2aafSWarner Losh memset(&tinfo->_reason, 0, sizeof(tinfo->_reason)); 170c34f2aafSWarner Losh 171c34f2aafSWarner Losh /* 172c34f2aafSWarner Losh * This is awkward, because we have to use a combination of the si_code and 173c34f2aafSWarner Losh * si_signo to figure out which of the union's members are valid.o We 174c34f2aafSWarner Losh * therefore make our best guess. 175c34f2aafSWarner Losh * 176c34f2aafSWarner Losh * Once we have made our guess, we record it in the top 16 bits of 177c34f2aafSWarner Losh * the si_code, so that tswap_siginfo() later can use it. 178c34f2aafSWarner Losh * tswap_siginfo() will strip these top bits out before writing 179c34f2aafSWarner Losh * si_code to the guest (sign-extending the lower bits). 180c34f2aafSWarner Losh */ 181c34f2aafSWarner Losh tinfo->si_signo = sig; 182c34f2aafSWarner Losh tinfo->si_errno = info->si_errno; 183c34f2aafSWarner Losh tinfo->si_code = info->si_code; 184c34f2aafSWarner Losh tinfo->si_pid = info->si_pid; 185c34f2aafSWarner Losh tinfo->si_uid = info->si_uid; 186c34f2aafSWarner Losh tinfo->si_status = info->si_status; 187c34f2aafSWarner Losh tinfo->si_addr = (abi_ulong)(unsigned long)info->si_addr; 188c34f2aafSWarner Losh /* 189c34f2aafSWarner Losh * si_value is opaque to kernel. On all FreeBSD platforms, 190c34f2aafSWarner Losh * sizeof(sival_ptr) >= sizeof(sival_int) so the following 191c34f2aafSWarner Losh * always will copy the larger element. 192c34f2aafSWarner Losh */ 193c34f2aafSWarner Losh tinfo->si_value.sival_ptr = 194c34f2aafSWarner Losh (abi_ulong)(unsigned long)info->si_value.sival_ptr; 195c34f2aafSWarner Losh 196c34f2aafSWarner Losh switch (si_code) { 197c34f2aafSWarner Losh /* 198c34f2aafSWarner Losh * All the SI_xxx codes that are defined here are global to 199c34f2aafSWarner Losh * all the signals (they have values that none of the other, 200c34f2aafSWarner Losh * more specific signal info will set). 201c34f2aafSWarner Losh */ 202c34f2aafSWarner Losh case SI_USER: 203c34f2aafSWarner Losh case SI_LWP: 204c34f2aafSWarner Losh case SI_KERNEL: 205c34f2aafSWarner Losh case SI_QUEUE: 206c34f2aafSWarner Losh case SI_ASYNCIO: 207c34f2aafSWarner Losh /* 208c34f2aafSWarner Losh * Only the fixed parts are valid (though FreeBSD doesn't always 209c34f2aafSWarner Losh * set all the fields to non-zero values. 210c34f2aafSWarner Losh */ 211c34f2aafSWarner Losh si_type = QEMU_SI_NOINFO; 212c34f2aafSWarner Losh break; 213c34f2aafSWarner Losh case SI_TIMER: 214c34f2aafSWarner Losh tinfo->_reason._timer._timerid = info->_reason._timer._timerid; 215c34f2aafSWarner Losh tinfo->_reason._timer._overrun = info->_reason._timer._overrun; 216c34f2aafSWarner Losh si_type = QEMU_SI_TIMER; 217c34f2aafSWarner Losh break; 218c34f2aafSWarner Losh case SI_MESGQ: 219c34f2aafSWarner Losh tinfo->_reason._mesgq._mqd = info->_reason._mesgq._mqd; 220c34f2aafSWarner Losh si_type = QEMU_SI_MESGQ; 221c34f2aafSWarner Losh break; 222c34f2aafSWarner Losh default: 223c34f2aafSWarner Losh /* 224c34f2aafSWarner Losh * We have to go based on the signal number now to figure out 225c34f2aafSWarner Losh * what's valid. 226c34f2aafSWarner Losh */ 227eb9d35f6SWarner Losh si_type = QEMU_SI_NOINFO; 228c34f2aafSWarner Losh if (has_trapno(sig)) { 229c34f2aafSWarner Losh tinfo->_reason._fault._trapno = info->_reason._fault._trapno; 230c34f2aafSWarner Losh si_type = QEMU_SI_FAULT; 231c34f2aafSWarner Losh } 232c34f2aafSWarner Losh #ifdef TARGET_SIGPOLL 233c34f2aafSWarner Losh /* 234c34f2aafSWarner Losh * FreeBSD never had SIGPOLL, but emulates it for Linux so there's 235c34f2aafSWarner Losh * a chance it may popup in the future. 236c34f2aafSWarner Losh */ 237c34f2aafSWarner Losh if (sig == TARGET_SIGPOLL) { 238c34f2aafSWarner Losh tinfo->_reason._poll._band = info->_reason._poll._band; 239c34f2aafSWarner Losh si_type = QEMU_SI_POLL; 240c34f2aafSWarner Losh } 241c34f2aafSWarner Losh #endif 242c34f2aafSWarner Losh /* 243c34f2aafSWarner Losh * Unsure that this can actually be generated, and our support for 244c34f2aafSWarner Losh * capsicum is somewhere between weak and non-existant, but if we get 245c34f2aafSWarner Losh * one, then we know what to save. 246c34f2aafSWarner Losh */ 247eb9d35f6SWarner Losh #ifdef QEMU_SI_CAPSICUM 248c34f2aafSWarner Losh if (sig == TARGET_SIGTRAP) { 249c34f2aafSWarner Losh tinfo->_reason._capsicum._syscall = 250c34f2aafSWarner Losh info->_reason._capsicum._syscall; 251c34f2aafSWarner Losh si_type = QEMU_SI_CAPSICUM; 252c34f2aafSWarner Losh } 253eb9d35f6SWarner Losh #endif 254c34f2aafSWarner Losh break; 255c34f2aafSWarner Losh } 256c34f2aafSWarner Losh tinfo->si_code = deposit32(si_code, 24, 8, si_type); 257c34f2aafSWarner Losh } 258c34f2aafSWarner Losh 25908eb66d5SWarner Losh static void tswap_siginfo(target_siginfo_t *tinfo, const target_siginfo_t *info) 26008eb66d5SWarner Losh { 26108eb66d5SWarner Losh int si_type = extract32(info->si_code, 24, 8); 26208eb66d5SWarner Losh int si_code = sextract32(info->si_code, 0, 24); 26308eb66d5SWarner Losh 26408eb66d5SWarner Losh __put_user(info->si_signo, &tinfo->si_signo); 26508eb66d5SWarner Losh __put_user(info->si_errno, &tinfo->si_errno); 26608eb66d5SWarner Losh __put_user(si_code, &tinfo->si_code); /* Zero out si_type, it's internal */ 26708eb66d5SWarner Losh __put_user(info->si_pid, &tinfo->si_pid); 26808eb66d5SWarner Losh __put_user(info->si_uid, &tinfo->si_uid); 26908eb66d5SWarner Losh __put_user(info->si_status, &tinfo->si_status); 27008eb66d5SWarner Losh __put_user(info->si_addr, &tinfo->si_addr); 27108eb66d5SWarner Losh /* 27208eb66d5SWarner Losh * Unswapped, because we passed it through mostly untouched. si_value is 27308eb66d5SWarner Losh * opaque to the kernel, so we didn't bother with potentially wasting cycles 27408eb66d5SWarner Losh * to swap it into host byte order. 27508eb66d5SWarner Losh */ 27608eb66d5SWarner Losh tinfo->si_value.sival_ptr = info->si_value.sival_ptr; 27708eb66d5SWarner Losh 27808eb66d5SWarner Losh /* 27908eb66d5SWarner Losh * We can use our internal marker of which fields in the structure 28008eb66d5SWarner Losh * are valid, rather than duplicating the guesswork of 28108eb66d5SWarner Losh * host_to_target_siginfo_noswap() here. 28208eb66d5SWarner Losh */ 28308eb66d5SWarner Losh switch (si_type) { 28408eb66d5SWarner Losh case QEMU_SI_NOINFO: /* No additional info */ 28508eb66d5SWarner Losh break; 28608eb66d5SWarner Losh case QEMU_SI_FAULT: 28708eb66d5SWarner Losh __put_user(info->_reason._fault._trapno, 28808eb66d5SWarner Losh &tinfo->_reason._fault._trapno); 28908eb66d5SWarner Losh break; 29008eb66d5SWarner Losh case QEMU_SI_TIMER: 29108eb66d5SWarner Losh __put_user(info->_reason._timer._timerid, 29208eb66d5SWarner Losh &tinfo->_reason._timer._timerid); 29308eb66d5SWarner Losh __put_user(info->_reason._timer._overrun, 29408eb66d5SWarner Losh &tinfo->_reason._timer._overrun); 29508eb66d5SWarner Losh break; 29608eb66d5SWarner Losh case QEMU_SI_MESGQ: 29708eb66d5SWarner Losh __put_user(info->_reason._mesgq._mqd, &tinfo->_reason._mesgq._mqd); 29808eb66d5SWarner Losh break; 29908eb66d5SWarner Losh case QEMU_SI_POLL: 30008eb66d5SWarner Losh /* Note: Not generated on FreeBSD */ 30108eb66d5SWarner Losh __put_user(info->_reason._poll._band, &tinfo->_reason._poll._band); 30208eb66d5SWarner Losh break; 303eb9d35f6SWarner Losh #ifdef QEMU_SI_CAPSICUM 30408eb66d5SWarner Losh case QEMU_SI_CAPSICUM: 30508eb66d5SWarner Losh __put_user(info->_reason._capsicum._syscall, 30608eb66d5SWarner Losh &tinfo->_reason._capsicum._syscall); 30708eb66d5SWarner Losh break; 308eb9d35f6SWarner Losh #endif 30908eb66d5SWarner Losh default: 31008eb66d5SWarner Losh g_assert_not_reached(); 31108eb66d5SWarner Losh } 31208eb66d5SWarner Losh } 31308eb66d5SWarner Losh 314394cf694SWarner Losh int block_signals(void) 315394cf694SWarner Losh { 316394cf694SWarner Losh TaskState *ts = (TaskState *)thread_cpu->opaque; 317394cf694SWarner Losh sigset_t set; 318394cf694SWarner Losh 319394cf694SWarner Losh /* 320394cf694SWarner Losh * It's OK to block everything including SIGSEGV, because we won't run any 321394cf694SWarner Losh * further guest code before unblocking signals in 322394cf694SWarner Losh * process_pending_signals(). We depend on the FreeBSD behaivor here where 323394cf694SWarner Losh * this will only affect this thread's signal mask. We don't use 324394cf694SWarner Losh * pthread_sigmask which might seem more correct because that routine also 325394cf694SWarner Losh * does odd things with SIGCANCEL to implement pthread_cancel(). 326394cf694SWarner Losh */ 327394cf694SWarner Losh sigfillset(&set); 328394cf694SWarner Losh sigprocmask(SIG_SETMASK, &set, 0); 329394cf694SWarner Losh 330394cf694SWarner Losh return qatomic_xchg(&ts->signal_pending, 1); 331394cf694SWarner Losh } 332394cf694SWarner Losh 33337714547SWarner Losh /* Returns 1 if given signal should dump core if not handled. */ 33437714547SWarner Losh static int core_dump_signal(int sig) 33537714547SWarner Losh { 33637714547SWarner Losh switch (sig) { 33737714547SWarner Losh case TARGET_SIGABRT: 33837714547SWarner Losh case TARGET_SIGFPE: 33937714547SWarner Losh case TARGET_SIGILL: 34037714547SWarner Losh case TARGET_SIGQUIT: 34137714547SWarner Losh case TARGET_SIGSEGV: 34237714547SWarner Losh case TARGET_SIGTRAP: 34337714547SWarner Losh case TARGET_SIGBUS: 34437714547SWarner Losh return 1; 34537714547SWarner Losh default: 34637714547SWarner Losh return 0; 34737714547SWarner Losh } 34837714547SWarner Losh } 34937714547SWarner Losh 35037714547SWarner Losh /* Abort execution with signal. */ 3518905770bSMarc-André Lureau static G_NORETURN 3528905770bSMarc-André Lureau void dump_core_and_abort(int target_sig) 35337714547SWarner Losh { 35437714547SWarner Losh CPUArchState *env = thread_cpu->env_ptr; 35537714547SWarner Losh CPUState *cpu = env_cpu(env); 35637714547SWarner Losh TaskState *ts = cpu->opaque; 35737714547SWarner Losh int core_dumped = 0; 35837714547SWarner Losh int host_sig; 35937714547SWarner Losh struct sigaction act; 36037714547SWarner Losh 36137714547SWarner Losh host_sig = target_to_host_signal(target_sig); 36237714547SWarner Losh gdb_signalled(env, target_sig); 36337714547SWarner Losh 36437714547SWarner Losh /* Dump core if supported by target binary format */ 36537714547SWarner Losh if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) { 36637714547SWarner Losh stop_all_tasks(); 36737714547SWarner Losh core_dumped = 36837714547SWarner Losh ((*ts->bprm->core_dump)(target_sig, env) == 0); 36937714547SWarner Losh } 37037714547SWarner Losh if (core_dumped) { 37137714547SWarner Losh struct rlimit nodump; 37237714547SWarner Losh 37337714547SWarner Losh /* 37437714547SWarner Losh * We already dumped the core of target process, we don't want 37537714547SWarner Losh * a coredump of qemu itself. 37637714547SWarner Losh */ 37737714547SWarner Losh getrlimit(RLIMIT_CORE, &nodump); 37837714547SWarner Losh nodump.rlim_cur = 0; 37937714547SWarner Losh setrlimit(RLIMIT_CORE, &nodump); 38037714547SWarner Losh (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) " 38137714547SWarner Losh "- %s\n", target_sig, strsignal(host_sig), "core dumped"); 38237714547SWarner Losh } 38337714547SWarner Losh 38437714547SWarner Losh /* 38537714547SWarner Losh * The proper exit code for dying from an uncaught signal is 38637714547SWarner Losh * -<signal>. The kernel doesn't allow exit() or _exit() to pass 38737714547SWarner Losh * a negative value. To get the proper exit code we need to 38837714547SWarner Losh * actually die from an uncaught signal. Here the default signal 38937714547SWarner Losh * handler is installed, we send ourself a signal and we wait for 39037714547SWarner Losh * it to arrive. 39137714547SWarner Losh */ 39237714547SWarner Losh memset(&act, 0, sizeof(act)); 39337714547SWarner Losh sigfillset(&act.sa_mask); 39437714547SWarner Losh act.sa_handler = SIG_DFL; 39537714547SWarner Losh sigaction(host_sig, &act, NULL); 39637714547SWarner Losh 39737714547SWarner Losh kill(getpid(), host_sig); 39837714547SWarner Losh 39937714547SWarner Losh /* 40037714547SWarner Losh * Make sure the signal isn't masked (just reuse the mask inside 40137714547SWarner Losh * of act). 40237714547SWarner Losh */ 40337714547SWarner Losh sigdelset(&act.sa_mask, host_sig); 40437714547SWarner Losh sigsuspend(&act.sa_mask); 40537714547SWarner Losh 40637714547SWarner Losh /* unreachable */ 40737714547SWarner Losh abort(); 40837714547SWarner Losh } 40937714547SWarner Losh 4101366ef81SWarner Losh /* 4115abfac27SWarner Losh * Queue a signal so that it will be send to the virtual CPU as soon as 4125abfac27SWarner Losh * possible. 4135abfac27SWarner Losh */ 414e32a6301SWarner Losh void queue_signal(CPUArchState *env, int sig, int si_type, 415e32a6301SWarner Losh target_siginfo_t *info) 4165abfac27SWarner Losh { 41738be620cSWarner Losh CPUState *cpu = env_cpu(env); 41838be620cSWarner Losh TaskState *ts = cpu->opaque; 41938be620cSWarner Losh 42038be620cSWarner Losh trace_user_queue_signal(env, sig); 42138be620cSWarner Losh 42238be620cSWarner Losh info->si_code = deposit32(info->si_code, 24, 8, si_type); 42338be620cSWarner Losh 42438be620cSWarner Losh ts->sync_signal.info = *info; 42538be620cSWarner Losh ts->sync_signal.pending = sig; 42638be620cSWarner Losh /* Signal that a new signal is pending. */ 42738be620cSWarner Losh qatomic_set(&ts->signal_pending, 1); 42838be620cSWarner Losh return; 4295abfac27SWarner Losh } 4305abfac27SWarner Losh 431149076adSWarner Losh static int fatal_signal(int sig) 432149076adSWarner Losh { 433149076adSWarner Losh 434149076adSWarner Losh switch (sig) { 435149076adSWarner Losh case TARGET_SIGCHLD: 436149076adSWarner Losh case TARGET_SIGURG: 437149076adSWarner Losh case TARGET_SIGWINCH: 438149076adSWarner Losh case TARGET_SIGINFO: 439149076adSWarner Losh /* Ignored by default. */ 440149076adSWarner Losh return 0; 441149076adSWarner Losh case TARGET_SIGCONT: 442149076adSWarner Losh case TARGET_SIGSTOP: 443149076adSWarner Losh case TARGET_SIGTSTP: 444149076adSWarner Losh case TARGET_SIGTTIN: 445149076adSWarner Losh case TARGET_SIGTTOU: 446149076adSWarner Losh /* Job control signals. */ 447149076adSWarner Losh return 0; 448149076adSWarner Losh default: 449149076adSWarner Losh return 1; 450149076adSWarner Losh } 451149076adSWarner Losh } 452149076adSWarner Losh 4530ef59989SWarner Losh /* 4540ef59989SWarner Losh * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the 4550ef59989SWarner Losh * 'force' part is handled in process_pending_signals(). 4560ef59989SWarner Losh */ 4570ef59989SWarner Losh void force_sig_fault(int sig, int code, abi_ulong addr) 4580ef59989SWarner Losh { 4590ef59989SWarner Losh CPUState *cpu = thread_cpu; 4600ef59989SWarner Losh CPUArchState *env = cpu->env_ptr; 4610ef59989SWarner Losh target_siginfo_t info = {}; 4620ef59989SWarner Losh 4630ef59989SWarner Losh info.si_signo = sig; 4640ef59989SWarner Losh info.si_errno = 0; 4650ef59989SWarner Losh info.si_code = code; 4660ef59989SWarner Losh info.si_addr = addr; 467e32a6301SWarner Losh queue_signal(env, sig, QEMU_SI_FAULT, &info); 4680ef59989SWarner Losh } 4690ef59989SWarner Losh 470149076adSWarner Losh static void host_signal_handler(int host_sig, siginfo_t *info, void *puc) 471149076adSWarner Losh { 472e625c7efSWarner Losh CPUArchState *env = thread_cpu->env_ptr; 473e625c7efSWarner Losh CPUState *cpu = env_cpu(env); 474e625c7efSWarner Losh TaskState *ts = cpu->opaque; 475e625c7efSWarner Losh target_siginfo_t tinfo; 476e625c7efSWarner Losh ucontext_t *uc = puc; 477e625c7efSWarner Losh struct emulated_sigtable *k; 478e625c7efSWarner Losh int guest_sig; 479e625c7efSWarner Losh uintptr_t pc = 0; 480e625c7efSWarner Losh bool sync_sig = false; 481e625c7efSWarner Losh 482e625c7efSWarner Losh /* 483e625c7efSWarner Losh * Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special 484e625c7efSWarner Losh * handling wrt signal blocking and unwinding. 485e625c7efSWarner Losh */ 486e625c7efSWarner Losh if ((host_sig == SIGSEGV || host_sig == SIGBUS) && info->si_code > 0) { 487e625c7efSWarner Losh MMUAccessType access_type; 488e625c7efSWarner Losh uintptr_t host_addr; 489e625c7efSWarner Losh abi_ptr guest_addr; 490e625c7efSWarner Losh bool is_write; 491e625c7efSWarner Losh 492e625c7efSWarner Losh host_addr = (uintptr_t)info->si_addr; 493e625c7efSWarner Losh 494e625c7efSWarner Losh /* 495e625c7efSWarner Losh * Convert forcefully to guest address space: addresses outside 496e625c7efSWarner Losh * reserved_va are still valid to report via SEGV_MAPERR. 497e625c7efSWarner Losh */ 498e625c7efSWarner Losh guest_addr = h2g_nocheck(host_addr); 499e625c7efSWarner Losh 500e625c7efSWarner Losh pc = host_signal_pc(uc); 501e625c7efSWarner Losh is_write = host_signal_write(info, uc); 502e625c7efSWarner Losh access_type = adjust_signal_pc(&pc, is_write); 503e625c7efSWarner Losh 504e625c7efSWarner Losh if (host_sig == SIGSEGV) { 505e625c7efSWarner Losh bool maperr = true; 506e625c7efSWarner Losh 507e625c7efSWarner Losh if (info->si_code == SEGV_ACCERR && h2g_valid(host_addr)) { 508e625c7efSWarner Losh /* If this was a write to a TB protected page, restart. */ 509e625c7efSWarner Losh if (is_write && 510e625c7efSWarner Losh handle_sigsegv_accerr_write(cpu, &uc->uc_sigmask, 511e625c7efSWarner Losh pc, guest_addr)) { 512e625c7efSWarner Losh return; 513e625c7efSWarner Losh } 514e625c7efSWarner Losh 515e625c7efSWarner Losh /* 516e625c7efSWarner Losh * With reserved_va, the whole address space is PROT_NONE, 517e625c7efSWarner Losh * which means that we may get ACCERR when we want MAPERR. 518e625c7efSWarner Losh */ 519e625c7efSWarner Losh if (page_get_flags(guest_addr) & PAGE_VALID) { 520e625c7efSWarner Losh maperr = false; 521e625c7efSWarner Losh } else { 522e625c7efSWarner Losh info->si_code = SEGV_MAPERR; 523e625c7efSWarner Losh } 524e625c7efSWarner Losh } 525e625c7efSWarner Losh 526e625c7efSWarner Losh sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL); 527e625c7efSWarner Losh cpu_loop_exit_sigsegv(cpu, guest_addr, access_type, maperr, pc); 528e625c7efSWarner Losh } else { 529e625c7efSWarner Losh sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL); 530e625c7efSWarner Losh if (info->si_code == BUS_ADRALN) { 531e625c7efSWarner Losh cpu_loop_exit_sigbus(cpu, guest_addr, access_type, pc); 532e625c7efSWarner Losh } 533e625c7efSWarner Losh } 534e625c7efSWarner Losh 535e625c7efSWarner Losh sync_sig = true; 536e625c7efSWarner Losh } 537e625c7efSWarner Losh 538e625c7efSWarner Losh /* Get the target signal number. */ 539e625c7efSWarner Losh guest_sig = host_to_target_signal(host_sig); 540e625c7efSWarner Losh if (guest_sig < 1 || guest_sig > TARGET_NSIG) { 541e625c7efSWarner Losh return; 542e625c7efSWarner Losh } 543e625c7efSWarner Losh trace_user_host_signal(cpu, host_sig, guest_sig); 544e625c7efSWarner Losh 545e625c7efSWarner Losh host_to_target_siginfo_noswap(&tinfo, info); 546e625c7efSWarner Losh 547e625c7efSWarner Losh k = &ts->sigtab[guest_sig - 1]; 548e625c7efSWarner Losh k->info = tinfo; 549e625c7efSWarner Losh k->pending = guest_sig; 550e625c7efSWarner Losh ts->signal_pending = 1; 551e625c7efSWarner Losh 552e625c7efSWarner Losh /* 553e625c7efSWarner Losh * For synchronous signals, unwind the cpu state to the faulting 554e625c7efSWarner Losh * insn and then exit back to the main loop so that the signal 555e625c7efSWarner Losh * is delivered immediately. 556e625c7efSWarner Losh */ 557e625c7efSWarner Losh if (sync_sig) { 558e625c7efSWarner Losh cpu->exception_index = EXCP_INTERRUPT; 559e625c7efSWarner Losh cpu_loop_exit_restore(cpu, pc); 560e625c7efSWarner Losh } 561e625c7efSWarner Losh 562e625c7efSWarner Losh rewind_if_in_safe_syscall(puc); 563e625c7efSWarner Losh 564e625c7efSWarner Losh /* 565e625c7efSWarner Losh * Block host signals until target signal handler entered. We 566e625c7efSWarner Losh * can't block SIGSEGV or SIGBUS while we're executing guest 567e625c7efSWarner Losh * code in case the guest code provokes one in the window between 568e625c7efSWarner Losh * now and it getting out to the main loop. Signals will be 569e625c7efSWarner Losh * unblocked again in process_pending_signals(). 570e625c7efSWarner Losh */ 571e625c7efSWarner Losh sigfillset(&uc->uc_sigmask); 572e625c7efSWarner Losh sigdelset(&uc->uc_sigmask, SIGSEGV); 573e625c7efSWarner Losh sigdelset(&uc->uc_sigmask, SIGBUS); 574e625c7efSWarner Losh 575e625c7efSWarner Losh /* Interrupt the virtual CPU as soon as possible. */ 576e625c7efSWarner Losh cpu_exit(thread_cpu); 577149076adSWarner Losh } 578149076adSWarner Losh 57943ed4267SWarner Losh /* do_sigaltstack() returns target values and errnos. */ 58043ed4267SWarner Losh /* compare to kern/kern_sig.c sys_sigaltstack() and kern_sigaltstack() */ 58143ed4267SWarner Losh abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp) 58243ed4267SWarner Losh { 58343ed4267SWarner Losh TaskState *ts = (TaskState *)thread_cpu->opaque; 58443ed4267SWarner Losh int ret; 58543ed4267SWarner Losh target_stack_t oss; 58643ed4267SWarner Losh 58743ed4267SWarner Losh if (uoss_addr) { 58843ed4267SWarner Losh /* Save current signal stack params */ 58943ed4267SWarner Losh oss.ss_sp = tswapl(ts->sigaltstack_used.ss_sp); 59043ed4267SWarner Losh oss.ss_size = tswapl(ts->sigaltstack_used.ss_size); 59143ed4267SWarner Losh oss.ss_flags = tswapl(sas_ss_flags(ts, sp)); 59243ed4267SWarner Losh } 59343ed4267SWarner Losh 59443ed4267SWarner Losh if (uss_addr) { 59543ed4267SWarner Losh target_stack_t *uss; 59643ed4267SWarner Losh target_stack_t ss; 59743ed4267SWarner Losh size_t minstacksize = TARGET_MINSIGSTKSZ; 59843ed4267SWarner Losh 59943ed4267SWarner Losh ret = -TARGET_EFAULT; 60043ed4267SWarner Losh if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) { 60143ed4267SWarner Losh goto out; 60243ed4267SWarner Losh } 60343ed4267SWarner Losh __get_user(ss.ss_sp, &uss->ss_sp); 60443ed4267SWarner Losh __get_user(ss.ss_size, &uss->ss_size); 60543ed4267SWarner Losh __get_user(ss.ss_flags, &uss->ss_flags); 60643ed4267SWarner Losh unlock_user_struct(uss, uss_addr, 0); 60743ed4267SWarner Losh 60843ed4267SWarner Losh ret = -TARGET_EPERM; 60943ed4267SWarner Losh if (on_sig_stack(ts, sp)) { 61043ed4267SWarner Losh goto out; 61143ed4267SWarner Losh } 61243ed4267SWarner Losh 61343ed4267SWarner Losh ret = -TARGET_EINVAL; 61443ed4267SWarner Losh if (ss.ss_flags != TARGET_SS_DISABLE 61543ed4267SWarner Losh && ss.ss_flags != TARGET_SS_ONSTACK 61643ed4267SWarner Losh && ss.ss_flags != 0) { 61743ed4267SWarner Losh goto out; 61843ed4267SWarner Losh } 61943ed4267SWarner Losh 62043ed4267SWarner Losh if (ss.ss_flags == TARGET_SS_DISABLE) { 62143ed4267SWarner Losh ss.ss_size = 0; 62243ed4267SWarner Losh ss.ss_sp = 0; 62343ed4267SWarner Losh } else { 62443ed4267SWarner Losh ret = -TARGET_ENOMEM; 62543ed4267SWarner Losh if (ss.ss_size < minstacksize) { 62643ed4267SWarner Losh goto out; 62743ed4267SWarner Losh } 62843ed4267SWarner Losh } 62943ed4267SWarner Losh 63043ed4267SWarner Losh ts->sigaltstack_used.ss_sp = ss.ss_sp; 63143ed4267SWarner Losh ts->sigaltstack_used.ss_size = ss.ss_size; 63243ed4267SWarner Losh } 63343ed4267SWarner Losh 63443ed4267SWarner Losh if (uoss_addr) { 63543ed4267SWarner Losh ret = -TARGET_EFAULT; 63643ed4267SWarner Losh if (copy_to_user(uoss_addr, &oss, sizeof(oss))) { 63743ed4267SWarner Losh goto out; 63843ed4267SWarner Losh } 63943ed4267SWarner Losh } 64043ed4267SWarner Losh 64143ed4267SWarner Losh ret = 0; 64243ed4267SWarner Losh out: 64343ed4267SWarner Losh return ret; 64443ed4267SWarner Losh } 64543ed4267SWarner Losh 646394cf694SWarner Losh /* do_sigaction() return host values and errnos */ 647394cf694SWarner Losh int do_sigaction(int sig, const struct target_sigaction *act, 648394cf694SWarner Losh struct target_sigaction *oact) 649394cf694SWarner Losh { 650394cf694SWarner Losh struct target_sigaction *k; 651394cf694SWarner Losh struct sigaction act1; 652394cf694SWarner Losh int host_sig; 653394cf694SWarner Losh int ret = 0; 654394cf694SWarner Losh 655394cf694SWarner Losh if (sig < 1 || sig > TARGET_NSIG) { 656394cf694SWarner Losh return -TARGET_EINVAL; 657394cf694SWarner Losh } 658394cf694SWarner Losh 659394cf694SWarner Losh if ((sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP) && 660394cf694SWarner Losh act != NULL && act->_sa_handler != TARGET_SIG_DFL) { 661394cf694SWarner Losh return -TARGET_EINVAL; 662394cf694SWarner Losh } 663394cf694SWarner Losh 664394cf694SWarner Losh if (block_signals()) { 665394cf694SWarner Losh return -TARGET_ERESTART; 666394cf694SWarner Losh } 667394cf694SWarner Losh 668394cf694SWarner Losh k = &sigact_table[sig - 1]; 669394cf694SWarner Losh if (oact) { 670394cf694SWarner Losh oact->_sa_handler = tswapal(k->_sa_handler); 671394cf694SWarner Losh oact->sa_flags = tswap32(k->sa_flags); 672394cf694SWarner Losh oact->sa_mask = k->sa_mask; 673394cf694SWarner Losh } 674394cf694SWarner Losh if (act) { 675394cf694SWarner Losh k->_sa_handler = tswapal(act->_sa_handler); 676394cf694SWarner Losh k->sa_flags = tswap32(act->sa_flags); 677394cf694SWarner Losh k->sa_mask = act->sa_mask; 678394cf694SWarner Losh 679394cf694SWarner Losh /* Update the host signal state. */ 680394cf694SWarner Losh host_sig = target_to_host_signal(sig); 681394cf694SWarner Losh if (host_sig != SIGSEGV && host_sig != SIGBUS) { 682394cf694SWarner Losh memset(&act1, 0, sizeof(struct sigaction)); 683394cf694SWarner Losh sigfillset(&act1.sa_mask); 684394cf694SWarner Losh act1.sa_flags = SA_SIGINFO; 685394cf694SWarner Losh if (k->sa_flags & TARGET_SA_RESTART) { 686394cf694SWarner Losh act1.sa_flags |= SA_RESTART; 687394cf694SWarner Losh } 688394cf694SWarner Losh /* 689394cf694SWarner Losh * Note: It is important to update the host kernel signal mask to 690394cf694SWarner Losh * avoid getting unexpected interrupted system calls. 691394cf694SWarner Losh */ 692394cf694SWarner Losh if (k->_sa_handler == TARGET_SIG_IGN) { 693394cf694SWarner Losh act1.sa_sigaction = (void *)SIG_IGN; 694394cf694SWarner Losh } else if (k->_sa_handler == TARGET_SIG_DFL) { 695394cf694SWarner Losh if (fatal_signal(sig)) { 696394cf694SWarner Losh act1.sa_sigaction = host_signal_handler; 697394cf694SWarner Losh } else { 698394cf694SWarner Losh act1.sa_sigaction = (void *)SIG_DFL; 699394cf694SWarner Losh } 700394cf694SWarner Losh } else { 701394cf694SWarner Losh act1.sa_sigaction = host_signal_handler; 702394cf694SWarner Losh } 703394cf694SWarner Losh ret = sigaction(host_sig, &act1, NULL); 704394cf694SWarner Losh } 705394cf694SWarner Losh } 706394cf694SWarner Losh return ret; 707394cf694SWarner Losh } 708394cf694SWarner Losh 70946f4f76dSWarner Losh static inline abi_ulong get_sigframe(struct target_sigaction *ka, 71046f4f76dSWarner Losh CPUArchState *env, size_t frame_size) 71146f4f76dSWarner Losh { 71246f4f76dSWarner Losh TaskState *ts = (TaskState *)thread_cpu->opaque; 71346f4f76dSWarner Losh abi_ulong sp; 71446f4f76dSWarner Losh 71546f4f76dSWarner Losh /* Use default user stack */ 71646f4f76dSWarner Losh sp = get_sp_from_cpustate(env); 71746f4f76dSWarner Losh 71846f4f76dSWarner Losh if ((ka->sa_flags & TARGET_SA_ONSTACK) && sas_ss_flags(ts, sp) == 0) { 71946f4f76dSWarner Losh sp = ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size; 72046f4f76dSWarner Losh } 72146f4f76dSWarner Losh 72246f4f76dSWarner Losh /* TODO: make this a target_arch function / define */ 72346f4f76dSWarner Losh #if defined(TARGET_ARM) 72446f4f76dSWarner Losh return (sp - frame_size) & ~7; 72546f4f76dSWarner Losh #elif defined(TARGET_AARCH64) 72646f4f76dSWarner Losh return (sp - frame_size) & ~15; 72746f4f76dSWarner Losh #else 72846f4f76dSWarner Losh return sp - frame_size; 72946f4f76dSWarner Losh #endif 73046f4f76dSWarner Losh } 73146f4f76dSWarner Losh 73246f4f76dSWarner Losh /* compare to $M/$M/exec_machdep.c sendsig and sys/kern/kern_sig.c sigexit */ 73346f4f76dSWarner Losh 73446f4f76dSWarner Losh static void setup_frame(int sig, int code, struct target_sigaction *ka, 73546f4f76dSWarner Losh target_sigset_t *set, target_siginfo_t *tinfo, CPUArchState *env) 73646f4f76dSWarner Losh { 73746f4f76dSWarner Losh struct target_sigframe *frame; 73846f4f76dSWarner Losh abi_ulong frame_addr; 73946f4f76dSWarner Losh int i; 74046f4f76dSWarner Losh 74146f4f76dSWarner Losh frame_addr = get_sigframe(ka, env, sizeof(*frame)); 74246f4f76dSWarner Losh trace_user_setup_frame(env, frame_addr); 74346f4f76dSWarner Losh if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { 74446f4f76dSWarner Losh unlock_user_struct(frame, frame_addr, 1); 74546f4f76dSWarner Losh dump_core_and_abort(TARGET_SIGILL); 74646f4f76dSWarner Losh return; 74746f4f76dSWarner Losh } 74846f4f76dSWarner Losh 74946f4f76dSWarner Losh memset(frame, 0, sizeof(*frame)); 75046f4f76dSWarner Losh setup_sigframe_arch(env, frame_addr, frame, 0); 75146f4f76dSWarner Losh 75246f4f76dSWarner Losh for (i = 0; i < TARGET_NSIG_WORDS; i++) { 75346f4f76dSWarner Losh __put_user(set->__bits[i], &frame->sf_uc.uc_sigmask.__bits[i]); 75446f4f76dSWarner Losh } 75546f4f76dSWarner Losh 75646f4f76dSWarner Losh if (tinfo) { 75746f4f76dSWarner Losh frame->sf_si.si_signo = tinfo->si_signo; 75846f4f76dSWarner Losh frame->sf_si.si_errno = tinfo->si_errno; 75946f4f76dSWarner Losh frame->sf_si.si_code = tinfo->si_code; 76046f4f76dSWarner Losh frame->sf_si.si_pid = tinfo->si_pid; 76146f4f76dSWarner Losh frame->sf_si.si_uid = tinfo->si_uid; 76246f4f76dSWarner Losh frame->sf_si.si_status = tinfo->si_status; 76346f4f76dSWarner Losh frame->sf_si.si_addr = tinfo->si_addr; 76446f4f76dSWarner Losh /* see host_to_target_siginfo_noswap() for more details */ 76546f4f76dSWarner Losh frame->sf_si.si_value.sival_ptr = tinfo->si_value.sival_ptr; 76646f4f76dSWarner Losh /* 76746f4f76dSWarner Losh * At this point, whatever is in the _reason union is complete 76846f4f76dSWarner Losh * and in target order, so just copy the whole thing over, even 76946f4f76dSWarner Losh * if it's too large for this specific signal. 77046f4f76dSWarner Losh * host_to_target_siginfo_noswap() and tswap_siginfo() have ensured 77146f4f76dSWarner Losh * that's so. 77246f4f76dSWarner Losh */ 77346f4f76dSWarner Losh memcpy(&frame->sf_si._reason, &tinfo->_reason, 77446f4f76dSWarner Losh sizeof(tinfo->_reason)); 77546f4f76dSWarner Losh } 77646f4f76dSWarner Losh 77746f4f76dSWarner Losh set_sigtramp_args(env, sig, frame, frame_addr, ka); 77846f4f76dSWarner Losh 77946f4f76dSWarner Losh unlock_user_struct(frame, frame_addr, 1); 78046f4f76dSWarner Losh } 78146f4f76dSWarner Losh 782c885ae0eSWarner Losh static int reset_signal_mask(target_ucontext_t *ucontext) 783c885ae0eSWarner Losh { 784c885ae0eSWarner Losh int i; 785c885ae0eSWarner Losh sigset_t blocked; 786c885ae0eSWarner Losh target_sigset_t target_set; 787c885ae0eSWarner Losh TaskState *ts = (TaskState *)thread_cpu->opaque; 788c885ae0eSWarner Losh 789c885ae0eSWarner Losh for (i = 0; i < TARGET_NSIG_WORDS; i++) { 790c885ae0eSWarner Losh if (__get_user(target_set.__bits[i], 791c885ae0eSWarner Losh &ucontext->uc_sigmask.__bits[i])) { 792c885ae0eSWarner Losh return -TARGET_EFAULT; 793c885ae0eSWarner Losh } 794c885ae0eSWarner Losh } 795c885ae0eSWarner Losh target_to_host_sigset_internal(&blocked, &target_set); 796c885ae0eSWarner Losh ts->signal_mask = blocked; 797c885ae0eSWarner Losh 798c885ae0eSWarner Losh return 0; 799c885ae0eSWarner Losh } 800c885ae0eSWarner Losh 801c885ae0eSWarner Losh /* See sys/$M/$M/exec_machdep.c sigreturn() */ 802c885ae0eSWarner Losh long do_sigreturn(CPUArchState *env, abi_ulong addr) 803c885ae0eSWarner Losh { 804c885ae0eSWarner Losh long ret; 805c885ae0eSWarner Losh abi_ulong target_ucontext; 806c885ae0eSWarner Losh target_ucontext_t *ucontext = NULL; 807c885ae0eSWarner Losh 808c885ae0eSWarner Losh /* Get the target ucontext address from the stack frame */ 809c885ae0eSWarner Losh ret = get_ucontext_sigreturn(env, addr, &target_ucontext); 810c885ae0eSWarner Losh if (is_error(ret)) { 811c885ae0eSWarner Losh return ret; 812c885ae0eSWarner Losh } 813c885ae0eSWarner Losh trace_user_do_sigreturn(env, addr); 814c885ae0eSWarner Losh if (!lock_user_struct(VERIFY_READ, ucontext, target_ucontext, 0)) { 815c885ae0eSWarner Losh goto badframe; 816c885ae0eSWarner Losh } 817c885ae0eSWarner Losh 818c885ae0eSWarner Losh /* Set the register state back to before the signal. */ 819c885ae0eSWarner Losh if (set_mcontext(env, &ucontext->uc_mcontext, 1)) { 820c885ae0eSWarner Losh goto badframe; 821c885ae0eSWarner Losh } 822c885ae0eSWarner Losh 823c885ae0eSWarner Losh /* And reset the signal mask. */ 824c885ae0eSWarner Losh if (reset_signal_mask(ucontext)) { 825c885ae0eSWarner Losh goto badframe; 826c885ae0eSWarner Losh } 827c885ae0eSWarner Losh 828c885ae0eSWarner Losh unlock_user_struct(ucontext, target_ucontext, 0); 829c885ae0eSWarner Losh return -TARGET_EJUSTRETURN; 830c885ae0eSWarner Losh 831c885ae0eSWarner Losh badframe: 832c885ae0eSWarner Losh if (ucontext != NULL) { 833c885ae0eSWarner Losh unlock_user_struct(ucontext, target_ucontext, 0); 834c885ae0eSWarner Losh } 835c885ae0eSWarner Losh return -TARGET_EFAULT; 836c885ae0eSWarner Losh } 837c885ae0eSWarner Losh 83884778508Sblueswir1 void signal_init(void) 83984778508Sblueswir1 { 840149076adSWarner Losh TaskState *ts = (TaskState *)thread_cpu->opaque; 841149076adSWarner Losh struct sigaction act; 842149076adSWarner Losh struct sigaction oact; 843149076adSWarner Losh int i; 844149076adSWarner Losh int host_sig; 845149076adSWarner Losh 846149076adSWarner Losh /* Set the signal mask from the host mask. */ 847149076adSWarner Losh sigprocmask(0, 0, &ts->signal_mask); 848149076adSWarner Losh 849149076adSWarner Losh sigfillset(&act.sa_mask); 850149076adSWarner Losh act.sa_sigaction = host_signal_handler; 851149076adSWarner Losh act.sa_flags = SA_SIGINFO; 852149076adSWarner Losh 853149076adSWarner Losh for (i = 1; i <= TARGET_NSIG; i++) { 854149076adSWarner Losh #ifdef CONFIG_GPROF 855149076adSWarner Losh if (i == TARGET_SIGPROF) { 856149076adSWarner Losh continue; 857149076adSWarner Losh } 858149076adSWarner Losh #endif 859149076adSWarner Losh host_sig = target_to_host_signal(i); 860149076adSWarner Losh sigaction(host_sig, NULL, &oact); 861149076adSWarner Losh if (oact.sa_sigaction == (void *)SIG_IGN) { 862149076adSWarner Losh sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN; 863149076adSWarner Losh } else if (oact.sa_sigaction == (void *)SIG_DFL) { 864149076adSWarner Losh sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL; 865149076adSWarner Losh } 866149076adSWarner Losh /* 867149076adSWarner Losh * If there's already a handler installed then something has 868149076adSWarner Losh * gone horribly wrong, so don't even try to handle that case. 869149076adSWarner Losh * Install some handlers for our own use. We need at least 870149076adSWarner Losh * SIGSEGV and SIGBUS, to detect exceptions. We can not just 871149076adSWarner Losh * trap all signals because it affects syscall interrupt 872149076adSWarner Losh * behavior. But do trap all default-fatal signals. 873149076adSWarner Losh */ 874149076adSWarner Losh if (fatal_signal(i)) { 875149076adSWarner Losh sigaction(host_sig, &act, NULL); 876149076adSWarner Losh } 877149076adSWarner Losh } 87884778508Sblueswir1 } 87984778508Sblueswir1 8806c6d4b56SWarner Losh static void handle_pending_signal(CPUArchState *env, int sig, 8816c6d4b56SWarner Losh struct emulated_sigtable *k) 8826c6d4b56SWarner Losh { 8836c6d4b56SWarner Losh CPUState *cpu = env_cpu(env); 8846c6d4b56SWarner Losh TaskState *ts = cpu->opaque; 8856c6d4b56SWarner Losh struct target_sigaction *sa; 8866c6d4b56SWarner Losh int code; 8876c6d4b56SWarner Losh sigset_t set; 8886c6d4b56SWarner Losh abi_ulong handler; 8896c6d4b56SWarner Losh target_siginfo_t tinfo; 8906c6d4b56SWarner Losh target_sigset_t target_old_set; 8916c6d4b56SWarner Losh 8926c6d4b56SWarner Losh trace_user_handle_signal(env, sig); 8936c6d4b56SWarner Losh 8946c6d4b56SWarner Losh k->pending = 0; 8956c6d4b56SWarner Losh 8966c6d4b56SWarner Losh sig = gdb_handlesig(cpu, sig); 8976c6d4b56SWarner Losh if (!sig) { 8986c6d4b56SWarner Losh sa = NULL; 8996c6d4b56SWarner Losh handler = TARGET_SIG_IGN; 9006c6d4b56SWarner Losh } else { 9016c6d4b56SWarner Losh sa = &sigact_table[sig - 1]; 9026c6d4b56SWarner Losh handler = sa->_sa_handler; 9036c6d4b56SWarner Losh } 9046c6d4b56SWarner Losh 9056c6d4b56SWarner Losh if (do_strace) { 9066c6d4b56SWarner Losh print_taken_signal(sig, &k->info); 9076c6d4b56SWarner Losh } 9086c6d4b56SWarner Losh 9096c6d4b56SWarner Losh if (handler == TARGET_SIG_DFL) { 9106c6d4b56SWarner Losh /* 9116c6d4b56SWarner Losh * default handler : ignore some signal. The other are job 9126c6d4b56SWarner Losh * control or fatal. 9136c6d4b56SWarner Losh */ 9146c6d4b56SWarner Losh if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || 9156c6d4b56SWarner Losh sig == TARGET_SIGTTOU) { 9166c6d4b56SWarner Losh kill(getpid(), SIGSTOP); 9176c6d4b56SWarner Losh } else if (sig != TARGET_SIGCHLD && sig != TARGET_SIGURG && 9186c6d4b56SWarner Losh sig != TARGET_SIGINFO && sig != TARGET_SIGWINCH && 9196c6d4b56SWarner Losh sig != TARGET_SIGCONT) { 9206c6d4b56SWarner Losh dump_core_and_abort(sig); 9216c6d4b56SWarner Losh } 9226c6d4b56SWarner Losh } else if (handler == TARGET_SIG_IGN) { 9236c6d4b56SWarner Losh /* ignore sig */ 9246c6d4b56SWarner Losh } else if (handler == TARGET_SIG_ERR) { 9256c6d4b56SWarner Losh dump_core_and_abort(sig); 9266c6d4b56SWarner Losh } else { 9276c6d4b56SWarner Losh /* compute the blocked signals during the handler execution */ 9286c6d4b56SWarner Losh sigset_t *blocked_set; 9296c6d4b56SWarner Losh 9306c6d4b56SWarner Losh target_to_host_sigset(&set, &sa->sa_mask); 9316c6d4b56SWarner Losh /* 9326c6d4b56SWarner Losh * SA_NODEFER indicates that the current signal should not be 9336c6d4b56SWarner Losh * blocked during the handler. 9346c6d4b56SWarner Losh */ 9356c6d4b56SWarner Losh if (!(sa->sa_flags & TARGET_SA_NODEFER)) { 9366c6d4b56SWarner Losh sigaddset(&set, target_to_host_signal(sig)); 9376c6d4b56SWarner Losh } 9386c6d4b56SWarner Losh 9396c6d4b56SWarner Losh /* 9406c6d4b56SWarner Losh * Save the previous blocked signal state to restore it at the 9416c6d4b56SWarner Losh * end of the signal execution (see do_sigreturn). 9426c6d4b56SWarner Losh */ 9436c6d4b56SWarner Losh host_to_target_sigset_internal(&target_old_set, &ts->signal_mask); 9446c6d4b56SWarner Losh 9456c6d4b56SWarner Losh blocked_set = ts->in_sigsuspend ? 9466c6d4b56SWarner Losh &ts->sigsuspend_mask : &ts->signal_mask; 9476c6d4b56SWarner Losh sigorset(&ts->signal_mask, blocked_set, &set); 9486c6d4b56SWarner Losh ts->in_sigsuspend = false; 9496c6d4b56SWarner Losh sigprocmask(SIG_SETMASK, &ts->signal_mask, NULL); 9506c6d4b56SWarner Losh 9516c6d4b56SWarner Losh /* XXX VM86 on x86 ??? */ 9526c6d4b56SWarner Losh 9536c6d4b56SWarner Losh code = k->info.si_code; /* From host, so no si_type */ 9546c6d4b56SWarner Losh /* prepare the stack frame of the virtual CPU */ 9556c6d4b56SWarner Losh if (sa->sa_flags & TARGET_SA_SIGINFO) { 9566c6d4b56SWarner Losh tswap_siginfo(&tinfo, &k->info); 9576c6d4b56SWarner Losh setup_frame(sig, code, sa, &target_old_set, &tinfo, env); 9586c6d4b56SWarner Losh } else { 9596c6d4b56SWarner Losh setup_frame(sig, code, sa, &target_old_set, NULL, env); 9606c6d4b56SWarner Losh } 9616c6d4b56SWarner Losh if (sa->sa_flags & TARGET_SA_RESETHAND) { 9626c6d4b56SWarner Losh sa->_sa_handler = TARGET_SIG_DFL; 9636c6d4b56SWarner Losh } 9646c6d4b56SWarner Losh } 9656c6d4b56SWarner Losh } 9666c6d4b56SWarner Losh 967d7acd317SWarner Losh void process_pending_signals(CPUArchState *env) 96884778508Sblueswir1 { 969d7acd317SWarner Losh CPUState *cpu = env_cpu(env); 970d7acd317SWarner Losh int sig; 971d7acd317SWarner Losh sigset_t *blocked_set, set; 972d7acd317SWarner Losh struct emulated_sigtable *k; 973d7acd317SWarner Losh TaskState *ts = cpu->opaque; 974d7acd317SWarner Losh 975d7acd317SWarner Losh while (qatomic_read(&ts->signal_pending)) { 976d7acd317SWarner Losh sigfillset(&set); 977d7acd317SWarner Losh sigprocmask(SIG_SETMASK, &set, 0); 978d7acd317SWarner Losh 979d7acd317SWarner Losh restart_scan: 980d7acd317SWarner Losh sig = ts->sync_signal.pending; 981d7acd317SWarner Losh if (sig) { 982d7acd317SWarner Losh /* 983d7acd317SWarner Losh * Synchronous signals are forced by the emulated CPU in some way. 984d7acd317SWarner Losh * If they are set to ignore, restore the default handler (see 985d7acd317SWarner Losh * sys/kern_sig.c trapsignal() and execsigs() for this behavior) 986d7acd317SWarner Losh * though maybe this is done only when forcing exit for non SIGCHLD. 987d7acd317SWarner Losh */ 988d7acd317SWarner Losh if (sigismember(&ts->signal_mask, target_to_host_signal(sig)) || 989d7acd317SWarner Losh sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) { 990d7acd317SWarner Losh sigdelset(&ts->signal_mask, target_to_host_signal(sig)); 991d7acd317SWarner Losh sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL; 992d7acd317SWarner Losh } 993d7acd317SWarner Losh handle_pending_signal(env, sig, &ts->sync_signal); 994d7acd317SWarner Losh } 995d7acd317SWarner Losh 996d7acd317SWarner Losh k = ts->sigtab; 997d7acd317SWarner Losh for (sig = 1; sig <= TARGET_NSIG; sig++, k++) { 998d7acd317SWarner Losh blocked_set = ts->in_sigsuspend ? 999d7acd317SWarner Losh &ts->sigsuspend_mask : &ts->signal_mask; 1000d7acd317SWarner Losh if (k->pending && 1001d7acd317SWarner Losh !sigismember(blocked_set, target_to_host_signal(sig))) { 1002d7acd317SWarner Losh handle_pending_signal(env, sig, k); 1003d7acd317SWarner Losh /* 1004d7acd317SWarner Losh * Restart scan from the beginning, as handle_pending_signal 1005d7acd317SWarner Losh * might have resulted in a new synchronous signal (eg SIGSEGV). 1006d7acd317SWarner Losh */ 1007d7acd317SWarner Losh goto restart_scan; 1008d7acd317SWarner Losh } 1009d7acd317SWarner Losh } 1010d7acd317SWarner Losh 1011d7acd317SWarner Losh /* 1012d7acd317SWarner Losh * Unblock signals and check one more time. Unblocking signals may cause 1013d7acd317SWarner Losh * us to take another host signal, which will set signal_pending again. 1014d7acd317SWarner Losh */ 1015d7acd317SWarner Losh qatomic_set(&ts->signal_pending, 0); 1016d7acd317SWarner Losh ts->in_sigsuspend = false; 1017d7acd317SWarner Losh set = ts->signal_mask; 1018d7acd317SWarner Losh sigdelset(&set, SIGSEGV); 1019d7acd317SWarner Losh sigdelset(&set, SIGBUS); 1020d7acd317SWarner Losh sigprocmask(SIG_SETMASK, &set, 0); 1021d7acd317SWarner Losh } 1022d7acd317SWarner Losh ts->in_sigsuspend = false; 102384778508Sblueswir1 } 1024835b04edSWarner Losh 1025835b04edSWarner Losh void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr, 1026835b04edSWarner Losh MMUAccessType access_type, bool maperr, uintptr_t ra) 1027835b04edSWarner Losh { 1028fc9f9bddSWarner Losh const struct TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops; 1029fc9f9bddSWarner Losh 1030fc9f9bddSWarner Losh if (tcg_ops->record_sigsegv) { 1031fc9f9bddSWarner Losh tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra); 1032fc9f9bddSWarner Losh } 1033fc9f9bddSWarner Losh 1034fc9f9bddSWarner Losh force_sig_fault(TARGET_SIGSEGV, 1035fc9f9bddSWarner Losh maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR, 1036fc9f9bddSWarner Losh addr); 1037fc9f9bddSWarner Losh cpu->exception_index = EXCP_INTERRUPT; 1038fc9f9bddSWarner Losh cpu_loop_exit_restore(cpu, ra); 1039835b04edSWarner Losh } 1040835b04edSWarner Losh 1041835b04edSWarner Losh void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr, 1042835b04edSWarner Losh MMUAccessType access_type, uintptr_t ra) 1043835b04edSWarner Losh { 1044cfdee273SWarner Losh const struct TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops; 1045cfdee273SWarner Losh 1046cfdee273SWarner Losh if (tcg_ops->record_sigbus) { 1047cfdee273SWarner Losh tcg_ops->record_sigbus(cpu, addr, access_type, ra); 1048cfdee273SWarner Losh } 1049cfdee273SWarner Losh 1050cfdee273SWarner Losh force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr); 1051cfdee273SWarner Losh cpu->exception_index = EXCP_INTERRUPT; 1052cfdee273SWarner Losh cpu_loop_exit_restore(cpu, ra); 1053835b04edSWarner Losh } 1054