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