| 1 | /* |
| 2 | * Emulation of Linux signals |
| 3 | * |
| 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #ifndef SIGNAL_COMMON_H |
| 21 | #define SIGNAL_COMMON_H |
| 22 | |
| 23 | #include "special-errno.h" |
| 24 | |
| 25 | /* Fallback addresses into sigtramp page. */ |
| 26 | extern abi_ulong default_sigreturn; |
| 27 | extern abi_ulong default_rt_sigreturn; |
| 28 | extern abi_ulong vdso_sigreturn_region_start; |
| 29 | extern abi_ulong vdso_sigreturn_region_end; |
| 30 | |
| 31 | static inline bool is_vdso_sigreturn(abi_ulong pc) |
| 32 | { |
| 33 | return pc >= vdso_sigreturn_region_start && pc < vdso_sigreturn_region_end; |
| 34 | } |
| 35 | |
| 36 | void setup_sigtramp(abi_ulong tramp_page); |
| 37 | |
| 38 | int on_sig_stack(unsigned long sp); |
| 39 | int sas_ss_flags(unsigned long sp); |
| 40 | abi_ulong target_sigsp(abi_ulong sp, struct target_sigaction *ka); |
| 41 | void target_save_altstack(target_stack_t *uss, CPUArchState *env); |
| 42 | abi_long target_restore_altstack(target_stack_t *uss, CPUArchState *env); |
| 43 | |
| 44 | static inline void target_sigemptyset(target_sigset_t *set) |
| 45 | { |
| 46 | memset(set, 0, sizeof(*set)); |
| 47 | } |
| 48 | |
| 49 | void host_to_target_sigset_internal(target_sigset_t *d, |
| 50 | const sigset_t *s); |
| 51 | void target_to_host_sigset_internal(sigset_t *d, |
| 52 | const target_sigset_t *s); |
| 53 | void set_sigmask(const sigset_t *set); |
| 54 | void force_sig(int sig); |
| 55 | void force_sigsegv(int oldsig); |
| 56 | void force_sig_fault(int sig, int code, abi_ulong addr); |
| 57 | #if defined(TARGET_ARCH_HAS_SETUP_FRAME) |
| 58 | void setup_frame(int sig, struct target_sigaction *ka, |
| 59 | target_sigset_t *set, CPUArchState *env); |
| 60 | #endif |
| 61 | void setup_rt_frame(int sig, struct target_sigaction *ka, |
| 62 | target_siginfo_t *info, |
| 63 | target_sigset_t *set, CPUArchState *env); |
| 64 | |
| 65 | void process_pending_signals(CPUArchState *cpu_env); |
| 66 | void signal_init(const char *rtsig_map); |
| 67 | void queue_signal(CPUArchState *env, int sig, int si_type, |
| 68 | target_siginfo_t *info); |
| 69 | void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info); |
| 70 | void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo); |
| 71 | int host_to_target_signal(int sig); |
| 72 | long do_sigreturn(CPUArchState *env); |
| 73 | long do_rt_sigreturn(CPUArchState *env); |
| 74 | abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, |
| 75 | CPUArchState *env); |
| 76 | int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset); |
| 77 | abi_long do_swapcontext(CPUArchState *env, abi_ulong uold_ctx, |
| 78 | abi_ulong unew_ctx, abi_long ctx_size); |
| 79 | /** |
| 80 | * block_signals: block all signals while handling this guest syscall |
| 81 | * |
| 82 | * Block all signals, and arrange that the signal mask is returned to |
| 83 | * its correct value for the guest before we resume execution of guest code. |
| 84 | * If this function returns non-zero, then the caller should immediately |
| 85 | * return -QEMU_ERESTARTSYS to the main loop, which will take the pending |
| 86 | * signal and restart execution of the syscall. |
| 87 | * If block_signals() returns zero, then the caller can continue with |
| 88 | * emulation of the system call knowing that no signals can be taken |
| 89 | * (and therefore that no race conditions will result). |
| 90 | * This should only be called once, because if it is called a second time |
| 91 | * it will always return non-zero. (Think of it like a mutex that can't |
| 92 | * be recursively locked.) |
| 93 | * Signals will be unblocked again by process_pending_signals(). |
| 94 | * |
| 95 | * Return value: non-zero if there was a pending signal, zero if not. |
| 96 | */ |
| 97 | int block_signals(void); /* Returns non zero if signal pending */ |
| 98 | |
| 99 | /** |
| 100 | * process_sigsuspend_mask: read and apply syscall-local signal mask |
| 101 | * |
| 102 | * Read the guest signal mask from @sigset, length @sigsize. |
| 103 | * Convert that to a host signal mask and save it to sigpending_mask. |
| 104 | * |
| 105 | * Return value: negative target errno, or zero; |
| 106 | * store &sigpending_mask into *pset on success. |
| 107 | */ |
| 108 | int process_sigsuspend_mask(sigset_t **pset, target_ulong sigset, |
| 109 | target_ulong sigsize); |
| 110 | |
| 111 | /** |
| 112 | * finish_sigsuspend_mask: finish a sigsuspend-like syscall |
| 113 | * |
| 114 | * Set in_sigsuspend if we need to use the modified sigset |
| 115 | * during process_pending_signals. |
| 116 | */ |
| 117 | static inline void finish_sigsuspend_mask(int ret) |
| 118 | { |
| 119 | if (ret != -QEMU_ERESTARTSYS) { |
| 120 | TaskState *ts = get_task_state(thread_cpu); |
| 121 | ts->in_sigsuspend = 1; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | #if defined(SIGSTKFLT) && defined(TARGET_SIGSTKFLT) |
| 126 | #define MAKE_SIG_ENTRY_SIGSTKFLT MAKE_SIG_ENTRY(SIGSTKFLT) |
| 127 | #else |
| 128 | #define MAKE_SIG_ENTRY_SIGSTKFLT |
| 129 | #endif |
| 130 | |
| 131 | #if defined(SIGIOT) && defined(TARGET_SIGIOT) |
| 132 | #define MAKE_SIG_ENTRY_SIGIOT MAKE_SIG_ENTRY(SIGIOT) |
| 133 | #else |
| 134 | #define MAKE_SIG_ENTRY_SIGIOT |
| 135 | #endif |
| 136 | |
| 137 | #define MAKE_SIGNAL_LIST \ |
| 138 | MAKE_SIG_ENTRY(SIGHUP) \ |
| 139 | MAKE_SIG_ENTRY(SIGINT) \ |
| 140 | MAKE_SIG_ENTRY(SIGQUIT) \ |
| 141 | MAKE_SIG_ENTRY(SIGILL) \ |
| 142 | MAKE_SIG_ENTRY(SIGTRAP) \ |
| 143 | MAKE_SIG_ENTRY(SIGABRT) \ |
| 144 | MAKE_SIG_ENTRY(SIGBUS) \ |
| 145 | MAKE_SIG_ENTRY(SIGFPE) \ |
| 146 | MAKE_SIG_ENTRY(SIGKILL) \ |
| 147 | MAKE_SIG_ENTRY(SIGUSR1) \ |
| 148 | MAKE_SIG_ENTRY(SIGSEGV) \ |
| 149 | MAKE_SIG_ENTRY(SIGUSR2) \ |
| 150 | MAKE_SIG_ENTRY(SIGPIPE) \ |
| 151 | MAKE_SIG_ENTRY(SIGALRM) \ |
| 152 | MAKE_SIG_ENTRY(SIGTERM) \ |
| 153 | MAKE_SIG_ENTRY(SIGCHLD) \ |
| 154 | MAKE_SIG_ENTRY(SIGCONT) \ |
| 155 | MAKE_SIG_ENTRY(SIGSTOP) \ |
| 156 | MAKE_SIG_ENTRY(SIGTSTP) \ |
| 157 | MAKE_SIG_ENTRY(SIGTTIN) \ |
| 158 | MAKE_SIG_ENTRY(SIGTTOU) \ |
| 159 | MAKE_SIG_ENTRY(SIGURG) \ |
| 160 | MAKE_SIG_ENTRY(SIGXCPU) \ |
| 161 | MAKE_SIG_ENTRY(SIGXFSZ) \ |
| 162 | MAKE_SIG_ENTRY(SIGVTALRM) \ |
| 163 | MAKE_SIG_ENTRY(SIGPROF) \ |
| 164 | MAKE_SIG_ENTRY(SIGWINCH) \ |
| 165 | MAKE_SIG_ENTRY(SIGIO) \ |
| 166 | MAKE_SIG_ENTRY(SIGPWR) \ |
| 167 | MAKE_SIG_ENTRY(SIGSYS) \ |
| 168 | MAKE_SIG_ENTRY_SIGSTKFLT \ |
| 169 | MAKE_SIG_ENTRY_SIGIOT |
| 170 | |
| 171 | #endif |