bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1 | /* |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 2 | * Emulation of Linux signals |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 18 | */ |
Peter Maydell | d39594e | 2016-01-26 18:17:02 +0000 | [diff] [blame] | 19 | #include "qemu/osdep.h" |
Peter Maydell | a70dadc | 2016-05-27 15:51:59 +0100 | [diff] [blame] | 20 | #include "qemu/bitops.h" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 21 | #include <sys/ucontext.h> |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 22 | #include <sys/resource.h> |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 23 | |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 24 | #include "qemu.h" |
Paolo Bonzini | c8ee0a4 | 2015-11-13 13:52:21 +0100 | [diff] [blame] | 25 | #include "trace.h" |
Laurent Vivier | befb744 | 2018-04-24 21:26:16 +0200 | [diff] [blame] | 26 | #include "signal-common.h" |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 27 | |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 28 | static struct target_sigaction sigact_table[TARGET_NSIG]; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 29 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 30 | static void host_signal_handler(int host_signum, siginfo_t *info, |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 31 | void *puc); |
| 32 | |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * System includes define _NSIG as SIGRTMAX + 1, |
| 36 | * but qemu (like the kernel) defines TARGET_NSIG as TARGET_SIGRTMAX |
| 37 | * and the first signal is SIGHUP defined as 1 |
| 38 | * Signal number 0 is reserved for use as kill(pid, 0), to test whether |
| 39 | * a process exists without sending it a signal. |
| 40 | */ |
Michael Forney | 144bff0 | 2021-05-26 12:02:03 -0700 | [diff] [blame] | 41 | #ifdef __SIGRTMAX |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 42 | QEMU_BUILD_BUG_ON(__SIGRTMAX + 1 != _NSIG); |
Michael Forney | 144bff0 | 2021-05-26 12:02:03 -0700 | [diff] [blame] | 43 | #endif |
Arnaud Patard | 3ca0558 | 2009-03-30 01:18:20 +0200 | [diff] [blame] | 44 | static uint8_t host_to_target_signal_table[_NSIG] = { |
bellard | 9e5f528 | 2003-07-13 17:33:54 +0000 | [diff] [blame] | 45 | [SIGHUP] = TARGET_SIGHUP, |
| 46 | [SIGINT] = TARGET_SIGINT, |
| 47 | [SIGQUIT] = TARGET_SIGQUIT, |
| 48 | [SIGILL] = TARGET_SIGILL, |
| 49 | [SIGTRAP] = TARGET_SIGTRAP, |
| 50 | [SIGABRT] = TARGET_SIGABRT, |
bellard | 01e3b76 | 2003-09-30 21:10:14 +0000 | [diff] [blame] | 51 | /* [SIGIOT] = TARGET_SIGIOT,*/ |
bellard | 9e5f528 | 2003-07-13 17:33:54 +0000 | [diff] [blame] | 52 | [SIGBUS] = TARGET_SIGBUS, |
| 53 | [SIGFPE] = TARGET_SIGFPE, |
| 54 | [SIGKILL] = TARGET_SIGKILL, |
| 55 | [SIGUSR1] = TARGET_SIGUSR1, |
| 56 | [SIGSEGV] = TARGET_SIGSEGV, |
| 57 | [SIGUSR2] = TARGET_SIGUSR2, |
| 58 | [SIGPIPE] = TARGET_SIGPIPE, |
| 59 | [SIGALRM] = TARGET_SIGALRM, |
| 60 | [SIGTERM] = TARGET_SIGTERM, |
| 61 | #ifdef SIGSTKFLT |
| 62 | [SIGSTKFLT] = TARGET_SIGSTKFLT, |
| 63 | #endif |
| 64 | [SIGCHLD] = TARGET_SIGCHLD, |
| 65 | [SIGCONT] = TARGET_SIGCONT, |
| 66 | [SIGSTOP] = TARGET_SIGSTOP, |
| 67 | [SIGTSTP] = TARGET_SIGTSTP, |
| 68 | [SIGTTIN] = TARGET_SIGTTIN, |
| 69 | [SIGTTOU] = TARGET_SIGTTOU, |
| 70 | [SIGURG] = TARGET_SIGURG, |
| 71 | [SIGXCPU] = TARGET_SIGXCPU, |
| 72 | [SIGXFSZ] = TARGET_SIGXFSZ, |
| 73 | [SIGVTALRM] = TARGET_SIGVTALRM, |
| 74 | [SIGPROF] = TARGET_SIGPROF, |
| 75 | [SIGWINCH] = TARGET_SIGWINCH, |
| 76 | [SIGIO] = TARGET_SIGIO, |
| 77 | [SIGPWR] = TARGET_SIGPWR, |
| 78 | [SIGSYS] = TARGET_SIGSYS, |
| 79 | /* next signals stay the same */ |
| 80 | }; |
bellard | 9e5f528 | 2003-07-13 17:33:54 +0000 | [diff] [blame] | 81 | |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 82 | static uint8_t target_to_host_signal_table[TARGET_NSIG + 1]; |
| 83 | |
| 84 | /* valid sig is between 1 and _NSIG - 1 */ |
pbrook | 1d9d8b5 | 2009-04-16 15:17:02 +0000 | [diff] [blame] | 85 | int host_to_target_signal(int sig) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 86 | { |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 87 | if (sig < 1 || sig >= _NSIG) { |
pbrook | 4cb0596 | 2008-05-30 18:05:19 +0000 | [diff] [blame] | 88 | return sig; |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 89 | } |
bellard | 9e5f528 | 2003-07-13 17:33:54 +0000 | [diff] [blame] | 90 | return host_to_target_signal_table[sig]; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 93 | /* valid sig is between 1 and TARGET_NSIG */ |
pbrook | 4cb0596 | 2008-05-30 18:05:19 +0000 | [diff] [blame] | 94 | int target_to_host_signal(int sig) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 95 | { |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 96 | if (sig < 1 || sig > TARGET_NSIG) { |
pbrook | 4cb0596 | 2008-05-30 18:05:19 +0000 | [diff] [blame] | 97 | return sig; |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 98 | } |
bellard | 9e5f528 | 2003-07-13 17:33:54 +0000 | [diff] [blame] | 99 | return target_to_host_signal_table[sig]; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 102 | static inline void target_sigaddset(target_sigset_t *set, int signum) |
pbrook | f5545b5 | 2008-05-30 22:37:07 +0000 | [diff] [blame] | 103 | { |
| 104 | signum--; |
| 105 | abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW); |
| 106 | set->sig[signum / TARGET_NSIG_BPW] |= mask; |
| 107 | } |
| 108 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 109 | static inline int target_sigismember(const target_sigset_t *set, int signum) |
pbrook | f5545b5 | 2008-05-30 22:37:07 +0000 | [diff] [blame] | 110 | { |
| 111 | signum--; |
| 112 | abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW); |
| 113 | return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0); |
| 114 | } |
| 115 | |
Laurent Vivier | befb744 | 2018-04-24 21:26:16 +0200 | [diff] [blame] | 116 | void host_to_target_sigset_internal(target_sigset_t *d, |
| 117 | const sigset_t *s) |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 118 | { |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 119 | int host_sig, target_sig; |
pbrook | f5545b5 | 2008-05-30 22:37:07 +0000 | [diff] [blame] | 120 | target_sigemptyset(d); |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 121 | for (host_sig = 1; host_sig < _NSIG; host_sig++) { |
| 122 | target_sig = host_to_target_signal(host_sig); |
| 123 | if (target_sig < 1 || target_sig > TARGET_NSIG) { |
| 124 | continue; |
| 125 | } |
| 126 | if (sigismember(s, host_sig)) { |
| 127 | target_sigaddset(d, target_sig); |
pbrook | f5545b5 | 2008-05-30 22:37:07 +0000 | [diff] [blame] | 128 | } |
bellard | 9e5f528 | 2003-07-13 17:33:54 +0000 | [diff] [blame] | 129 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 132 | void host_to_target_sigset(target_sigset_t *d, const sigset_t *s) |
bellard | 9231944 | 2004-06-19 16:58:13 +0000 | [diff] [blame] | 133 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 134 | target_sigset_t d1; |
bellard | 9231944 | 2004-06-19 16:58:13 +0000 | [diff] [blame] | 135 | int i; |
| 136 | |
| 137 | host_to_target_sigset_internal(&d1, s); |
| 138 | for(i = 0;i < TARGET_NSIG_WORDS; i++) |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 139 | d->sig[i] = tswapal(d1.sig[i]); |
bellard | 9231944 | 2004-06-19 16:58:13 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Laurent Vivier | befb744 | 2018-04-24 21:26:16 +0200 | [diff] [blame] | 142 | void target_to_host_sigset_internal(sigset_t *d, |
| 143 | const target_sigset_t *s) |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 144 | { |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 145 | int host_sig, target_sig; |
pbrook | f5545b5 | 2008-05-30 22:37:07 +0000 | [diff] [blame] | 146 | sigemptyset(d); |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 147 | for (target_sig = 1; target_sig <= TARGET_NSIG; target_sig++) { |
| 148 | host_sig = target_to_host_signal(target_sig); |
| 149 | if (host_sig < 1 || host_sig >= _NSIG) { |
| 150 | continue; |
| 151 | } |
| 152 | if (target_sigismember(s, target_sig)) { |
| 153 | sigaddset(d, host_sig); |
pbrook | f5545b5 | 2008-05-30 22:37:07 +0000 | [diff] [blame] | 154 | } |
Timothy E Baldwin | da7c864 | 2016-05-12 18:47:27 +0100 | [diff] [blame] | 155 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 158 | void target_to_host_sigset(sigset_t *d, const target_sigset_t *s) |
bellard | 9231944 | 2004-06-19 16:58:13 +0000 | [diff] [blame] | 159 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 160 | target_sigset_t s1; |
bellard | 9231944 | 2004-06-19 16:58:13 +0000 | [diff] [blame] | 161 | int i; |
| 162 | |
| 163 | for(i = 0;i < TARGET_NSIG_WORDS; i++) |
Matthias Braun | cbb21ee | 2011-08-12 19:57:41 +0200 | [diff] [blame] | 164 | s1.sig[i] = tswapal(s->sig[i]); |
bellard | 9231944 | 2004-06-19 16:58:13 +0000 | [diff] [blame] | 165 | target_to_host_sigset_internal(d, &s1); |
| 166 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 167 | |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 168 | void host_to_target_old_sigset(abi_ulong *old_sigset, |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 169 | const sigset_t *sigset) |
| 170 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 171 | target_sigset_t d; |
bellard | 9e5f528 | 2003-07-13 17:33:54 +0000 | [diff] [blame] | 172 | host_to_target_sigset(&d, sigset); |
| 173 | *old_sigset = d.sig[0]; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 174 | } |
| 175 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 176 | void target_to_host_old_sigset(sigset_t *sigset, |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 177 | const abi_ulong *old_sigset) |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 178 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 179 | target_sigset_t d; |
bellard | 9e5f528 | 2003-07-13 17:33:54 +0000 | [diff] [blame] | 180 | int i; |
| 181 | |
| 182 | d.sig[0] = *old_sigset; |
| 183 | for(i = 1;i < TARGET_NSIG_WORDS; i++) |
| 184 | d.sig[i] = 0; |
| 185 | target_to_host_sigset(sigset, &d); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 188 | int block_signals(void) |
| 189 | { |
| 190 | TaskState *ts = (TaskState *)thread_cpu->opaque; |
| 191 | sigset_t set; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 192 | |
| 193 | /* It's OK to block everything including SIGSEGV, because we won't |
| 194 | * run any further guest code before unblocking signals in |
| 195 | * process_pending_signals(). |
| 196 | */ |
| 197 | sigfillset(&set); |
| 198 | sigprocmask(SIG_SETMASK, &set, 0); |
| 199 | |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 200 | return qatomic_xchg(&ts->signal_pending, 1); |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Alex Barcelo | 1c27592 | 2014-03-14 14:36:55 +0000 | [diff] [blame] | 203 | /* Wrapper for sigprocmask function |
| 204 | * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 205 | * are host signal set, not guest ones. Returns -TARGET_ERESTARTSYS if |
| 206 | * a signal was already pending and the syscall must be restarted, or |
| 207 | * 0 on success. |
| 208 | * If set is NULL, this is guaranteed not to fail. |
Alex Barcelo | 1c27592 | 2014-03-14 14:36:55 +0000 | [diff] [blame] | 209 | */ |
| 210 | int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset) |
| 211 | { |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 212 | TaskState *ts = (TaskState *)thread_cpu->opaque; |
| 213 | |
| 214 | if (oldset) { |
| 215 | *oldset = ts->signal_mask; |
| 216 | } |
Peter Maydell | a7ec0f9 | 2014-03-14 14:36:56 +0000 | [diff] [blame] | 217 | |
| 218 | if (set) { |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 219 | int i; |
Peter Maydell | a7ec0f9 | 2014-03-14 14:36:56 +0000 | [diff] [blame] | 220 | |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 221 | if (block_signals()) { |
| 222 | return -TARGET_ERESTARTSYS; |
| 223 | } |
Peter Maydell | a7ec0f9 | 2014-03-14 14:36:56 +0000 | [diff] [blame] | 224 | |
| 225 | switch (how) { |
| 226 | case SIG_BLOCK: |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 227 | sigorset(&ts->signal_mask, &ts->signal_mask, set); |
Peter Maydell | a7ec0f9 | 2014-03-14 14:36:56 +0000 | [diff] [blame] | 228 | break; |
| 229 | case SIG_UNBLOCK: |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 230 | for (i = 1; i <= NSIG; ++i) { |
| 231 | if (sigismember(set, i)) { |
| 232 | sigdelset(&ts->signal_mask, i); |
| 233 | } |
Peter Maydell | a7ec0f9 | 2014-03-14 14:36:56 +0000 | [diff] [blame] | 234 | } |
| 235 | break; |
| 236 | case SIG_SETMASK: |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 237 | ts->signal_mask = *set; |
Peter Maydell | a7ec0f9 | 2014-03-14 14:36:56 +0000 | [diff] [blame] | 238 | break; |
| 239 | default: |
| 240 | g_assert_not_reached(); |
| 241 | } |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 242 | |
| 243 | /* Silently ignore attempts to change blocking status of KILL or STOP */ |
| 244 | sigdelset(&ts->signal_mask, SIGKILL); |
| 245 | sigdelset(&ts->signal_mask, SIGSTOP); |
Peter Maydell | a7ec0f9 | 2014-03-14 14:36:56 +0000 | [diff] [blame] | 246 | } |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 247 | return 0; |
Alex Barcelo | 1c27592 | 2014-03-14 14:36:55 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Richard Henderson | e8f2904 | 2018-05-27 14:02:17 -0500 | [diff] [blame] | 250 | #if !defined(TARGET_NIOS2) |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 251 | /* Just set the guest's signal mask to the specified value; the |
| 252 | * caller is assumed to have called block_signals() already. |
| 253 | */ |
Laurent Vivier | befb744 | 2018-04-24 21:26:16 +0200 | [diff] [blame] | 254 | void set_sigmask(const sigset_t *set) |
Peter Maydell | 9eede5b | 2016-05-27 15:51:46 +0100 | [diff] [blame] | 255 | { |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 256 | TaskState *ts = (TaskState *)thread_cpu->opaque; |
| 257 | |
| 258 | ts->signal_mask = *set; |
Peter Maydell | 9eede5b | 2016-05-27 15:51:46 +0100 | [diff] [blame] | 259 | } |
| 260 | #endif |
| 261 | |
Laurent Vivier | 465e237 | 2018-04-11 21:23:47 +0200 | [diff] [blame] | 262 | /* sigaltstack management */ |
| 263 | |
| 264 | int on_sig_stack(unsigned long sp) |
| 265 | { |
Peter Maydell | 5bfce0b | 2019-07-25 14:16:45 +0100 | [diff] [blame] | 266 | TaskState *ts = (TaskState *)thread_cpu->opaque; |
| 267 | |
| 268 | return (sp - ts->sigaltstack_used.ss_sp |
| 269 | < ts->sigaltstack_used.ss_size); |
Laurent Vivier | 465e237 | 2018-04-11 21:23:47 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | int sas_ss_flags(unsigned long sp) |
| 273 | { |
Peter Maydell | 5bfce0b | 2019-07-25 14:16:45 +0100 | [diff] [blame] | 274 | TaskState *ts = (TaskState *)thread_cpu->opaque; |
| 275 | |
| 276 | return (ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE |
Laurent Vivier | 465e237 | 2018-04-11 21:23:47 +0200 | [diff] [blame] | 277 | : on_sig_stack(sp) ? SS_ONSTACK : 0); |
| 278 | } |
| 279 | |
| 280 | abi_ulong target_sigsp(abi_ulong sp, struct target_sigaction *ka) |
| 281 | { |
| 282 | /* |
| 283 | * This is the X/Open sanctioned signal stack switching. |
| 284 | */ |
Peter Maydell | 5bfce0b | 2019-07-25 14:16:45 +0100 | [diff] [blame] | 285 | TaskState *ts = (TaskState *)thread_cpu->opaque; |
| 286 | |
Laurent Vivier | 465e237 | 2018-04-11 21:23:47 +0200 | [diff] [blame] | 287 | if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) { |
Peter Maydell | 5bfce0b | 2019-07-25 14:16:45 +0100 | [diff] [blame] | 288 | return ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size; |
Laurent Vivier | 465e237 | 2018-04-11 21:23:47 +0200 | [diff] [blame] | 289 | } |
| 290 | return sp; |
| 291 | } |
| 292 | |
| 293 | void target_save_altstack(target_stack_t *uss, CPUArchState *env) |
| 294 | { |
Peter Maydell | 5bfce0b | 2019-07-25 14:16:45 +0100 | [diff] [blame] | 295 | TaskState *ts = (TaskState *)thread_cpu->opaque; |
| 296 | |
| 297 | __put_user(ts->sigaltstack_used.ss_sp, &uss->ss_sp); |
Laurent Vivier | 465e237 | 2018-04-11 21:23:47 +0200 | [diff] [blame] | 298 | __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &uss->ss_flags); |
Peter Maydell | 5bfce0b | 2019-07-25 14:16:45 +0100 | [diff] [blame] | 299 | __put_user(ts->sigaltstack_used.ss_size, &uss->ss_size); |
Laurent Vivier | 465e237 | 2018-04-11 21:23:47 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Richard Henderson | ddc3e74 | 2021-04-25 19:53:13 -0700 | [diff] [blame] | 302 | abi_long target_restore_altstack(target_stack_t *uss, CPUArchState *env) |
Richard Henderson | 92bad94 | 2021-04-25 19:53:10 -0700 | [diff] [blame] | 303 | { |
| 304 | TaskState *ts = (TaskState *)thread_cpu->opaque; |
| 305 | size_t minstacksize = TARGET_MINSIGSTKSZ; |
| 306 | target_stack_t ss; |
| 307 | |
| 308 | #if defined(TARGET_PPC64) |
| 309 | /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */ |
| 310 | struct image_info *image = ts->info; |
| 311 | if (get_ppc64_abi(image) > 1) { |
| 312 | minstacksize = 4096; |
| 313 | } |
| 314 | #endif |
| 315 | |
| 316 | __get_user(ss.ss_sp, &uss->ss_sp); |
| 317 | __get_user(ss.ss_size, &uss->ss_size); |
| 318 | __get_user(ss.ss_flags, &uss->ss_flags); |
| 319 | |
Richard Henderson | ddc3e74 | 2021-04-25 19:53:13 -0700 | [diff] [blame] | 320 | if (on_sig_stack(get_sp_from_cpustate(env))) { |
Richard Henderson | 92bad94 | 2021-04-25 19:53:10 -0700 | [diff] [blame] | 321 | return -TARGET_EPERM; |
| 322 | } |
| 323 | |
| 324 | switch (ss.ss_flags) { |
| 325 | default: |
| 326 | return -TARGET_EINVAL; |
| 327 | |
| 328 | case TARGET_SS_DISABLE: |
| 329 | ss.ss_size = 0; |
| 330 | ss.ss_sp = 0; |
| 331 | break; |
| 332 | |
| 333 | case TARGET_SS_ONSTACK: |
| 334 | case 0: |
| 335 | if (ss.ss_size < minstacksize) { |
| 336 | return -TARGET_ENOMEM; |
| 337 | } |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | ts->sigaltstack_used.ss_sp = ss.ss_sp; |
| 342 | ts->sigaltstack_used.ss_size = ss.ss_size; |
| 343 | return 0; |
| 344 | } |
| 345 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 346 | /* siginfo conversion */ |
| 347 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 348 | static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo, |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 349 | const siginfo_t *info) |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 350 | { |
Richard Henderson | a05c640 | 2012-09-15 11:34:20 -0700 | [diff] [blame] | 351 | int sig = host_to_target_signal(info->si_signo); |
Peter Maydell | a70dadc | 2016-05-27 15:51:59 +0100 | [diff] [blame] | 352 | int si_code = info->si_code; |
| 353 | int si_type; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 354 | tinfo->si_signo = sig; |
| 355 | tinfo->si_errno = 0; |
pbrook | afd7cd9 | 2008-05-31 12:14:21 +0000 | [diff] [blame] | 356 | tinfo->si_code = info->si_code; |
Richard Henderson | a05c640 | 2012-09-15 11:34:20 -0700 | [diff] [blame] | 357 | |
Peter Maydell | 55d72a7 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 358 | /* This memset serves two purposes: |
| 359 | * (1) ensure we don't leak random junk to the guest later |
| 360 | * (2) placate false positives from gcc about fields |
| 361 | * being used uninitialized if it chooses to inline both this |
| 362 | * function and tswap_siginfo() into host_to_target_siginfo(). |
| 363 | */ |
| 364 | memset(tinfo->_sifields._pad, 0, sizeof(tinfo->_sifields._pad)); |
| 365 | |
Peter Maydell | a70dadc | 2016-05-27 15:51:59 +0100 | [diff] [blame] | 366 | /* This is awkward, because we have to use a combination of |
| 367 | * the si_code and si_signo to figure out which of the union's |
| 368 | * members are valid. (Within the host kernel it is always possible |
| 369 | * to tell, but the kernel carefully avoids giving userspace the |
| 370 | * high 16 bits of si_code, so we don't have the information to |
| 371 | * do this the easy way...) We therefore make our best guess, |
| 372 | * bearing in mind that a guest can spoof most of the si_codes |
| 373 | * via rt_sigqueueinfo() if it likes. |
| 374 | * |
| 375 | * Once we have made our guess, we record it in the top 16 bits of |
| 376 | * the si_code, so that tswap_siginfo() later can use it. |
| 377 | * tswap_siginfo() will strip these top bits out before writing |
| 378 | * si_code to the guest (sign-extending the lower bits). |
| 379 | */ |
| 380 | |
| 381 | switch (si_code) { |
| 382 | case SI_USER: |
| 383 | case SI_TKILL: |
| 384 | case SI_KERNEL: |
| 385 | /* Sent via kill(), tkill() or tgkill(), or direct from the kernel. |
| 386 | * These are the only unspoofable si_code values. |
| 387 | */ |
| 388 | tinfo->_sifields._kill._pid = info->si_pid; |
| 389 | tinfo->_sifields._kill._uid = info->si_uid; |
| 390 | si_type = QEMU_SI_KILL; |
| 391 | break; |
| 392 | default: |
| 393 | /* Everything else is spoofable. Make best guess based on signal */ |
| 394 | switch (sig) { |
| 395 | case TARGET_SIGCHLD: |
| 396 | tinfo->_sifields._sigchld._pid = info->si_pid; |
| 397 | tinfo->_sifields._sigchld._uid = info->si_uid; |
Alistair Francis | 1c3dfb5 | 2021-01-19 10:24:52 -0800 | [diff] [blame] | 398 | tinfo->_sifields._sigchld._status = info->si_status; |
Peter Maydell | a70dadc | 2016-05-27 15:51:59 +0100 | [diff] [blame] | 399 | tinfo->_sifields._sigchld._utime = info->si_utime; |
| 400 | tinfo->_sifields._sigchld._stime = info->si_stime; |
| 401 | si_type = QEMU_SI_CHLD; |
| 402 | break; |
| 403 | case TARGET_SIGIO: |
| 404 | tinfo->_sifields._sigpoll._band = info->si_band; |
| 405 | tinfo->_sifields._sigpoll._fd = info->si_fd; |
| 406 | si_type = QEMU_SI_POLL; |
| 407 | break; |
| 408 | default: |
| 409 | /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */ |
| 410 | tinfo->_sifields._rt._pid = info->si_pid; |
| 411 | tinfo->_sifields._rt._uid = info->si_uid; |
| 412 | /* XXX: potential problem if 64 bit */ |
| 413 | tinfo->_sifields._rt._sigval.sival_ptr |
Timothy E Baldwin | da7c864 | 2016-05-12 18:47:27 +0100 | [diff] [blame] | 414 | = (abi_ulong)(unsigned long)info->si_value.sival_ptr; |
Peter Maydell | a70dadc | 2016-05-27 15:51:59 +0100 | [diff] [blame] | 415 | si_type = QEMU_SI_RT; |
| 416 | break; |
| 417 | } |
| 418 | break; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 419 | } |
Peter Maydell | a70dadc | 2016-05-27 15:51:59 +0100 | [diff] [blame] | 420 | |
| 421 | tinfo->si_code = deposit32(si_code, 16, 16, si_type); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Laurent Vivier | befb744 | 2018-04-24 21:26:16 +0200 | [diff] [blame] | 424 | void tswap_siginfo(target_siginfo_t *tinfo, |
| 425 | const target_siginfo_t *info) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 426 | { |
Peter Maydell | a70dadc | 2016-05-27 15:51:59 +0100 | [diff] [blame] | 427 | int si_type = extract32(info->si_code, 16, 16); |
| 428 | int si_code = sextract32(info->si_code, 0, 16); |
Richard Henderson | a05c640 | 2012-09-15 11:34:20 -0700 | [diff] [blame] | 429 | |
Peter Maydell | a70dadc | 2016-05-27 15:51:59 +0100 | [diff] [blame] | 430 | __put_user(info->si_signo, &tinfo->si_signo); |
| 431 | __put_user(info->si_errno, &tinfo->si_errno); |
| 432 | __put_user(si_code, &tinfo->si_code); |
| 433 | |
| 434 | /* We can use our internal marker of which fields in the structure |
| 435 | * are valid, rather than duplicating the guesswork of |
| 436 | * host_to_target_siginfo_noswap() here. |
| 437 | */ |
| 438 | switch (si_type) { |
| 439 | case QEMU_SI_KILL: |
| 440 | __put_user(info->_sifields._kill._pid, &tinfo->_sifields._kill._pid); |
| 441 | __put_user(info->_sifields._kill._uid, &tinfo->_sifields._kill._uid); |
| 442 | break; |
| 443 | case QEMU_SI_TIMER: |
| 444 | __put_user(info->_sifields._timer._timer1, |
| 445 | &tinfo->_sifields._timer._timer1); |
| 446 | __put_user(info->_sifields._timer._timer2, |
| 447 | &tinfo->_sifields._timer._timer2); |
| 448 | break; |
| 449 | case QEMU_SI_POLL: |
| 450 | __put_user(info->_sifields._sigpoll._band, |
| 451 | &tinfo->_sifields._sigpoll._band); |
| 452 | __put_user(info->_sifields._sigpoll._fd, |
| 453 | &tinfo->_sifields._sigpoll._fd); |
| 454 | break; |
| 455 | case QEMU_SI_FAULT: |
| 456 | __put_user(info->_sifields._sigfault._addr, |
| 457 | &tinfo->_sifields._sigfault._addr); |
| 458 | break; |
| 459 | case QEMU_SI_CHLD: |
| 460 | __put_user(info->_sifields._sigchld._pid, |
| 461 | &tinfo->_sifields._sigchld._pid); |
| 462 | __put_user(info->_sifields._sigchld._uid, |
| 463 | &tinfo->_sifields._sigchld._uid); |
| 464 | __put_user(info->_sifields._sigchld._status, |
| 465 | &tinfo->_sifields._sigchld._status); |
| 466 | __put_user(info->_sifields._sigchld._utime, |
| 467 | &tinfo->_sifields._sigchld._utime); |
| 468 | __put_user(info->_sifields._sigchld._stime, |
| 469 | &tinfo->_sifields._sigchld._stime); |
| 470 | break; |
| 471 | case QEMU_SI_RT: |
| 472 | __put_user(info->_sifields._rt._pid, &tinfo->_sifields._rt._pid); |
| 473 | __put_user(info->_sifields._rt._uid, &tinfo->_sifields._rt._uid); |
| 474 | __put_user(info->_sifields._rt._sigval.sival_ptr, |
| 475 | &tinfo->_sifields._rt._sigval.sival_ptr); |
| 476 | break; |
| 477 | default: |
| 478 | g_assert_not_reached(); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 482 | void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 483 | { |
Peter Maydell | 55d72a7 | 2016-06-13 11:22:05 +0100 | [diff] [blame] | 484 | target_siginfo_t tgt_tmp; |
| 485 | host_to_target_siginfo_noswap(&tgt_tmp, info); |
| 486 | tswap_siginfo(tinfo, &tgt_tmp); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /* XXX: we support only POSIX RT signals are used. */ |
ths | aa1f17c | 2007-07-11 22:48:58 +0000 | [diff] [blame] | 490 | /* XXX: find a solution for 64 bit (additional malloced data is needed) */ |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 491 | void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo) |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 492 | { |
Peter Maydell | 90c0f08 | 2016-05-27 15:52:01 +0100 | [diff] [blame] | 493 | /* This conversion is used only for the rt_sigqueueinfo syscall, |
| 494 | * and so we know that the _rt fields are the valid ones. |
| 495 | */ |
| 496 | abi_ulong sival_ptr; |
| 497 | |
| 498 | __get_user(info->si_signo, &tinfo->si_signo); |
| 499 | __get_user(info->si_errno, &tinfo->si_errno); |
| 500 | __get_user(info->si_code, &tinfo->si_code); |
| 501 | __get_user(info->si_pid, &tinfo->_sifields._rt._pid); |
| 502 | __get_user(info->si_uid, &tinfo->_sifields._rt._uid); |
| 503 | __get_user(sival_ptr, &tinfo->_sifields._rt._sigval.sival_ptr); |
| 504 | info->si_value.sival_ptr = (void *)(long)sival_ptr; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 505 | } |
| 506 | |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 507 | static int fatal_signal (int sig) |
| 508 | { |
| 509 | switch (sig) { |
| 510 | case TARGET_SIGCHLD: |
| 511 | case TARGET_SIGURG: |
| 512 | case TARGET_SIGWINCH: |
| 513 | /* Ignored by default. */ |
| 514 | return 0; |
| 515 | case TARGET_SIGCONT: |
| 516 | case TARGET_SIGSTOP: |
| 517 | case TARGET_SIGTSTP: |
| 518 | case TARGET_SIGTTIN: |
| 519 | case TARGET_SIGTTOU: |
| 520 | /* Job control signals. */ |
| 521 | return 0; |
| 522 | default: |
| 523 | return 1; |
| 524 | } |
| 525 | } |
| 526 | |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 527 | /* returns 1 if given signal should dump core if not handled */ |
| 528 | static int core_dump_signal(int sig) |
| 529 | { |
| 530 | switch (sig) { |
| 531 | case TARGET_SIGABRT: |
| 532 | case TARGET_SIGFPE: |
| 533 | case TARGET_SIGILL: |
| 534 | case TARGET_SIGQUIT: |
| 535 | case TARGET_SIGSEGV: |
| 536 | case TARGET_SIGTRAP: |
| 537 | case TARGET_SIGBUS: |
| 538 | return (1); |
| 539 | default: |
| 540 | return (0); |
| 541 | } |
| 542 | } |
| 543 | |
Laurent Vivier | 365510f | 2020-02-12 13:56:56 +0100 | [diff] [blame] | 544 | static void signal_table_init(void) |
| 545 | { |
Laurent Vivier | 6bc024e | 2020-02-12 13:56:58 +0100 | [diff] [blame] | 546 | int host_sig, target_sig, count; |
Laurent Vivier | 365510f | 2020-02-12 13:56:56 +0100 | [diff] [blame] | 547 | |
| 548 | /* |
Laurent Vivier | 6bc024e | 2020-02-12 13:56:58 +0100 | [diff] [blame] | 549 | * Signals are supported starting from TARGET_SIGRTMIN and going up |
| 550 | * until we run out of host realtime signals. |
| 551 | * glibc at least uses only the lower 2 rt signals and probably |
| 552 | * nobody's using the upper ones. |
| 553 | * it's why SIGRTMIN (34) is generally greater than __SIGRTMIN (32) |
Laurent Vivier | 365510f | 2020-02-12 13:56:56 +0100 | [diff] [blame] | 554 | * To fix this properly we need to do manual signal delivery multiplexed |
| 555 | * over a single host signal. |
Laurent Vivier | 6bc024e | 2020-02-12 13:56:58 +0100 | [diff] [blame] | 556 | * Attempts for configure "missing" signals via sigaction will be |
| 557 | * silently ignored. |
Laurent Vivier | 365510f | 2020-02-12 13:56:56 +0100 | [diff] [blame] | 558 | */ |
Laurent Vivier | 6bc024e | 2020-02-12 13:56:58 +0100 | [diff] [blame] | 559 | for (host_sig = SIGRTMIN; host_sig <= SIGRTMAX; host_sig++) { |
| 560 | target_sig = host_sig - SIGRTMIN + TARGET_SIGRTMIN; |
| 561 | if (target_sig <= TARGET_NSIG) { |
| 562 | host_to_target_signal_table[host_sig] = target_sig; |
| 563 | } |
| 564 | } |
Laurent Vivier | 365510f | 2020-02-12 13:56:56 +0100 | [diff] [blame] | 565 | |
| 566 | /* generate signal conversion tables */ |
Laurent Vivier | 6bc024e | 2020-02-12 13:56:58 +0100 | [diff] [blame] | 567 | for (target_sig = 1; target_sig <= TARGET_NSIG; target_sig++) { |
| 568 | target_to_host_signal_table[target_sig] = _NSIG; /* poison */ |
| 569 | } |
Laurent Vivier | 365510f | 2020-02-12 13:56:56 +0100 | [diff] [blame] | 570 | for (host_sig = 1; host_sig < _NSIG; host_sig++) { |
| 571 | if (host_to_target_signal_table[host_sig] == 0) { |
| 572 | host_to_target_signal_table[host_sig] = host_sig; |
| 573 | } |
Laurent Vivier | 365510f | 2020-02-12 13:56:56 +0100 | [diff] [blame] | 574 | target_sig = host_to_target_signal_table[host_sig]; |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 575 | if (target_sig <= TARGET_NSIG) { |
| 576 | target_to_host_signal_table[target_sig] = host_sig; |
| 577 | } |
Laurent Vivier | 365510f | 2020-02-12 13:56:56 +0100 | [diff] [blame] | 578 | } |
Laurent Vivier | 6bc024e | 2020-02-12 13:56:58 +0100 | [diff] [blame] | 579 | |
| 580 | if (trace_event_get_state_backends(TRACE_SIGNAL_TABLE_INIT)) { |
| 581 | for (target_sig = 1, count = 0; target_sig <= TARGET_NSIG; target_sig++) { |
| 582 | if (target_to_host_signal_table[target_sig] == _NSIG) { |
| 583 | count++; |
| 584 | } |
| 585 | } |
| 586 | trace_signal_table_init(count); |
| 587 | } |
Laurent Vivier | 365510f | 2020-02-12 13:56:56 +0100 | [diff] [blame] | 588 | } |
| 589 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 590 | void signal_init(void) |
| 591 | { |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 592 | TaskState *ts = (TaskState *)thread_cpu->opaque; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 593 | struct sigaction act; |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 594 | struct sigaction oact; |
Laurent Vivier | 365510f | 2020-02-12 13:56:56 +0100 | [diff] [blame] | 595 | int i; |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 596 | int host_sig; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 597 | |
Laurent Vivier | 365510f | 2020-02-12 13:56:56 +0100 | [diff] [blame] | 598 | /* initialize signal conversion tables */ |
| 599 | signal_table_init(); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 600 | |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 601 | /* Set the signal mask from the host mask. */ |
| 602 | sigprocmask(0, 0, &ts->signal_mask); |
| 603 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 604 | sigfillset(&act.sa_mask); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 605 | act.sa_flags = SA_SIGINFO; |
| 606 | act.sa_sigaction = host_signal_handler; |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 607 | for(i = 1; i <= TARGET_NSIG; i++) { |
Paolo Bonzini | 4cc600d | 2020-02-04 17:11:04 +0100 | [diff] [blame] | 608 | #ifdef CONFIG_GPROF |
Laurent Vivier | 9fcff3a | 2020-02-12 13:56:57 +0100 | [diff] [blame] | 609 | if (i == TARGET_SIGPROF) { |
Alex Bennée | 716cdbe | 2019-05-02 15:58:46 +0100 | [diff] [blame] | 610 | continue; |
| 611 | } |
| 612 | #endif |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 613 | host_sig = target_to_host_signal(i); |
| 614 | sigaction(host_sig, NULL, &oact); |
| 615 | if (oact.sa_sigaction == (void *)SIG_IGN) { |
| 616 | sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN; |
| 617 | } else if (oact.sa_sigaction == (void *)SIG_DFL) { |
| 618 | sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL; |
| 619 | } |
| 620 | /* If there's already a handler installed then something has |
| 621 | gone horribly wrong, so don't even try to handle that case. */ |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 622 | /* Install some handlers for our own use. We need at least |
| 623 | SIGSEGV and SIGBUS, to detect exceptions. We can not just |
| 624 | trap all signals because it affects syscall interrupt |
| 625 | behavior. But do trap all default-fatal signals. */ |
| 626 | if (fatal_signal (i)) |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 627 | sigaction(host_sig, &act, NULL); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 628 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Peter Maydell | c599d4d | 2016-07-28 16:44:49 +0100 | [diff] [blame] | 631 | /* Force a synchronously taken signal. The kernel force_sig() function |
| 632 | * also forces the signal to "not blocked, not ignored", but for QEMU |
| 633 | * that work is done in process_pending_signals(). |
| 634 | */ |
Laurent Vivier | befb744 | 2018-04-24 21:26:16 +0200 | [diff] [blame] | 635 | void force_sig(int sig) |
Peter Maydell | c599d4d | 2016-07-28 16:44:49 +0100 | [diff] [blame] | 636 | { |
| 637 | CPUState *cpu = thread_cpu; |
| 638 | CPUArchState *env = cpu->env_ptr; |
| 639 | target_siginfo_t info; |
| 640 | |
| 641 | info.si_signo = sig; |
| 642 | info.si_errno = 0; |
| 643 | info.si_code = TARGET_SI_KERNEL; |
| 644 | info._sifields._kill._pid = 0; |
| 645 | info._sifields._kill._uid = 0; |
| 646 | queue_signal(env, info.si_signo, QEMU_SI_KILL, &info); |
| 647 | } |
Peter Maydell | 0939166 | 2016-07-28 16:44:47 +0100 | [diff] [blame] | 648 | |
| 649 | /* Force a SIGSEGV if we couldn't write to memory trying to set |
| 650 | * up the signal frame. oldsig is the signal we were trying to handle |
| 651 | * at the point of failure. |
| 652 | */ |
Michael Clark | 47ae93c | 2018-03-03 01:31:11 +1300 | [diff] [blame] | 653 | #if !defined(TARGET_RISCV) |
Laurent Vivier | befb744 | 2018-04-24 21:26:16 +0200 | [diff] [blame] | 654 | void force_sigsegv(int oldsig) |
Peter Maydell | 0939166 | 2016-07-28 16:44:47 +0100 | [diff] [blame] | 655 | { |
Peter Maydell | 0939166 | 2016-07-28 16:44:47 +0100 | [diff] [blame] | 656 | if (oldsig == SIGSEGV) { |
| 657 | /* Make sure we don't try to deliver the signal again; this will |
Peter Maydell | c599d4d | 2016-07-28 16:44:49 +0100 | [diff] [blame] | 658 | * end up with handle_pending_signal() calling dump_core_and_abort(). |
Peter Maydell | 0939166 | 2016-07-28 16:44:47 +0100 | [diff] [blame] | 659 | */ |
| 660 | sigact_table[oldsig - 1]._sa_handler = TARGET_SIG_DFL; |
| 661 | } |
Peter Maydell | c4b3574 | 2016-07-28 16:44:50 +0100 | [diff] [blame] | 662 | force_sig(TARGET_SIGSEGV); |
Peter Maydell | 0939166 | 2016-07-28 16:44:47 +0100 | [diff] [blame] | 663 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 664 | |
Michael Clark | 47ae93c | 2018-03-03 01:31:11 +1300 | [diff] [blame] | 665 | #endif |
| 666 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 667 | /* abort execution with signal */ |
Peter Maydell | c599d4d | 2016-07-28 16:44:49 +0100 | [diff] [blame] | 668 | static void QEMU_NORETURN dump_core_and_abort(int target_sig) |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 669 | { |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 670 | CPUState *cpu = thread_cpu; |
| 671 | CPUArchState *env = cpu->env_ptr; |
| 672 | TaskState *ts = (TaskState *)cpu->opaque; |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 673 | int host_sig, core_dumped = 0; |
aurel32 | 603e4fd | 2009-04-15 16:18:38 +0000 | [diff] [blame] | 674 | struct sigaction act; |
Paolo Bonzini | c8ee0a4 | 2015-11-13 13:52:21 +0100 | [diff] [blame] | 675 | |
Riku Voipio | 66393fb | 2009-12-04 15:16:32 +0200 | [diff] [blame] | 676 | host_sig = target_to_host_signal(target_sig); |
Paolo Bonzini | c8ee0a4 | 2015-11-13 13:52:21 +0100 | [diff] [blame] | 677 | trace_user_force_sig(env, target_sig, host_sig); |
Andreas Färber | a2247f8 | 2013-06-09 19:47:04 +0200 | [diff] [blame] | 678 | gdb_signalled(env, target_sig); |
aurel32 | 603e4fd | 2009-04-15 16:18:38 +0000 | [diff] [blame] | 679 | |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 680 | /* dump core if supported by target binary format */ |
Riku Voipio | 66393fb | 2009-12-04 15:16:32 +0200 | [diff] [blame] | 681 | if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) { |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 682 | stop_all_tasks(); |
| 683 | core_dumped = |
Andreas Färber | a2247f8 | 2013-06-09 19:47:04 +0200 | [diff] [blame] | 684 | ((*ts->bprm->core_dump)(target_sig, env) == 0); |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 685 | } |
| 686 | if (core_dumped) { |
| 687 | /* we already dumped the core of target process, we don't want |
| 688 | * a coredump of qemu itself */ |
| 689 | struct rlimit nodump; |
| 690 | getrlimit(RLIMIT_CORE, &nodump); |
| 691 | nodump.rlim_cur=0; |
| 692 | setrlimit(RLIMIT_CORE, &nodump); |
| 693 | (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n", |
Riku Voipio | 66393fb | 2009-12-04 15:16:32 +0200 | [diff] [blame] | 694 | target_sig, strsignal(host_sig), "core dumped" ); |
Mika Westerberg | edf8e2a | 2009-04-07 09:57:11 +0300 | [diff] [blame] | 695 | } |
| 696 | |
Stefan Weil | 0c58751 | 2011-04-28 17:20:32 +0200 | [diff] [blame] | 697 | /* The proper exit code for dying from an uncaught signal is |
aurel32 | 603e4fd | 2009-04-15 16:18:38 +0000 | [diff] [blame] | 698 | * -<signal>. The kernel doesn't allow exit() or _exit() to pass |
| 699 | * a negative value. To get the proper exit code we need to |
| 700 | * actually die from an uncaught signal. Here the default signal |
| 701 | * handler is installed, we send ourself a signal and we wait for |
| 702 | * it to arrive. */ |
| 703 | sigfillset(&act.sa_mask); |
| 704 | act.sa_handler = SIG_DFL; |
Peter Maydell | 3a5d30b | 2014-02-17 18:55:32 +0000 | [diff] [blame] | 705 | act.sa_flags = 0; |
aurel32 | 603e4fd | 2009-04-15 16:18:38 +0000 | [diff] [blame] | 706 | sigaction(host_sig, &act, NULL); |
| 707 | |
| 708 | /* For some reason raise(host_sig) doesn't send the signal when |
| 709 | * statically linked on x86-64. */ |
| 710 | kill(getpid(), host_sig); |
| 711 | |
| 712 | /* Make sure the signal isn't masked (just reuse the mask inside |
| 713 | of act) */ |
| 714 | sigdelset(&act.sa_mask, host_sig); |
| 715 | sigsuspend(&act.sa_mask); |
| 716 | |
| 717 | /* unreachable */ |
Blue Swirl | a6c6f76 | 2010-03-13 14:18:50 +0000 | [diff] [blame] | 718 | abort(); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 719 | } |
| 720 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 721 | /* queue a signal so that it will be send to the virtual CPU as soon |
| 722 | as possible */ |
Peter Maydell | 9d2803f | 2016-07-28 16:44:46 +0100 | [diff] [blame] | 723 | int queue_signal(CPUArchState *env, int sig, int si_type, |
| 724 | target_siginfo_t *info) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 725 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 726 | CPUState *cpu = env_cpu(env); |
Andreas Färber | 0429a97 | 2013-08-26 18:14:44 +0200 | [diff] [blame] | 727 | TaskState *ts = cpu->opaque; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 728 | |
Paolo Bonzini | c8ee0a4 | 2015-11-13 13:52:21 +0100 | [diff] [blame] | 729 | trace_user_queue_signal(env, sig); |
Peter Maydell | a7ec0f9 | 2014-03-14 14:36:56 +0000 | [diff] [blame] | 730 | |
Peter Maydell | 9d2803f | 2016-07-28 16:44:46 +0100 | [diff] [blame] | 731 | info->si_code = deposit32(info->si_code, 16, 16, si_type); |
Peter Maydell | a70dadc | 2016-05-27 15:51:59 +0100 | [diff] [blame] | 732 | |
Timothy E Baldwin | 655ed67 | 2016-05-27 15:51:53 +0100 | [diff] [blame] | 733 | ts->sync_signal.info = *info; |
| 734 | ts->sync_signal.pending = sig; |
Timothy E Baldwin | 907f5fd | 2016-05-27 15:51:52 +0100 | [diff] [blame] | 735 | /* signal that a new signal is pending */ |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 736 | qatomic_set(&ts->signal_pending, 1); |
Timothy E Baldwin | 907f5fd | 2016-05-27 15:51:52 +0100 | [diff] [blame] | 737 | return 1; /* indicates that the signal was queued */ |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Timothy E Baldwin | 4d330ce | 2016-05-12 18:47:46 +0100 | [diff] [blame] | 740 | #ifndef HAVE_SAFE_SYSCALL |
| 741 | static inline void rewind_if_in_safe_syscall(void *puc) |
| 742 | { |
| 743 | /* Default version: never rewind */ |
| 744 | } |
| 745 | #endif |
| 746 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 747 | static void host_signal_handler(int host_signum, siginfo_t *info, |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 748 | void *puc) |
| 749 | { |
Andreas Färber | a2247f8 | 2013-06-09 19:47:04 +0200 | [diff] [blame] | 750 | CPUArchState *env = thread_cpu->env_ptr; |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 751 | CPUState *cpu = env_cpu(env); |
Timothy E Baldwin | 655ed67 | 2016-05-27 15:51:53 +0100 | [diff] [blame] | 752 | TaskState *ts = cpu->opaque; |
| 753 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 754 | int sig; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 755 | target_siginfo_t tinfo; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 756 | ucontext_t *uc = puc; |
Timothy E Baldwin | 655ed67 | 2016-05-27 15:51:53 +0100 | [diff] [blame] | 757 | struct emulated_sigtable *k; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 758 | |
| 759 | /* the CPU emulator uses some host signals to detect exceptions, |
aurel32 | eaa449b | 2009-01-03 13:14:52 +0000 | [diff] [blame] | 760 | we forward to it some signals */ |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 761 | if ((host_signum == SIGSEGV || host_signum == SIGBUS) |
aurel32 | eaa449b | 2009-01-03 13:14:52 +0000 | [diff] [blame] | 762 | && info->si_code > 0) { |
bellard | b346ff4 | 2003-06-15 20:05:50 +0000 | [diff] [blame] | 763 | if (cpu_signal_handler(host_signum, info, puc)) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 764 | return; |
| 765 | } |
| 766 | |
| 767 | /* get target signal number */ |
| 768 | sig = host_to_target_signal(host_signum); |
| 769 | if (sig < 1 || sig > TARGET_NSIG) |
| 770 | return; |
Paolo Bonzini | c8ee0a4 | 2015-11-13 13:52:21 +0100 | [diff] [blame] | 771 | trace_user_host_signal(env, host_signum, sig); |
Timothy E Baldwin | 4d330ce | 2016-05-12 18:47:46 +0100 | [diff] [blame] | 772 | |
| 773 | rewind_if_in_safe_syscall(puc); |
| 774 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 775 | host_to_target_siginfo_noswap(&tinfo, info); |
Timothy E Baldwin | 655ed67 | 2016-05-27 15:51:53 +0100 | [diff] [blame] | 776 | k = &ts->sigtab[sig - 1]; |
| 777 | k->info = tinfo; |
| 778 | k->pending = sig; |
| 779 | ts->signal_pending = 1; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 780 | |
Timothy E Baldwin | 655ed67 | 2016-05-27 15:51:53 +0100 | [diff] [blame] | 781 | /* Block host signals until target signal handler entered. We |
| 782 | * can't block SIGSEGV or SIGBUS while we're executing guest |
| 783 | * code in case the guest code provokes one in the window between |
| 784 | * now and it getting out to the main loop. Signals will be |
| 785 | * unblocked again in process_pending_signals(). |
Peter Maydell | 1d48fdd | 2016-06-14 12:49:18 +0100 | [diff] [blame] | 786 | * |
| 787 | * WARNING: we cannot use sigfillset() here because the uc_sigmask |
| 788 | * field is a kernel sigset_t, which is much smaller than the |
| 789 | * libc sigset_t which sigfillset() operates on. Using sigfillset() |
| 790 | * would write 0xff bytes off the end of the structure and trash |
| 791 | * data on the struct. |
| 792 | * We can't use sizeof(uc->uc_sigmask) either, because the libc |
| 793 | * headers define the struct field with the wrong (too large) type. |
Timothy E Baldwin | 655ed67 | 2016-05-27 15:51:53 +0100 | [diff] [blame] | 794 | */ |
Peter Maydell | 1d48fdd | 2016-06-14 12:49:18 +0100 | [diff] [blame] | 795 | memset(&uc->uc_sigmask, 0xff, SIGSET_T_SIZE); |
Timothy E Baldwin | 655ed67 | 2016-05-27 15:51:53 +0100 | [diff] [blame] | 796 | sigdelset(&uc->uc_sigmask, SIGSEGV); |
| 797 | sigdelset(&uc->uc_sigmask, SIGBUS); |
| 798 | |
| 799 | /* interrupt the virtual CPU as soon as possible */ |
| 800 | cpu_exit(thread_cpu); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 801 | } |
| 802 | |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 803 | /* do_sigaltstack() returns target values and errnos. */ |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 804 | /* compare linux/kernel/signal.c:do_sigaltstack() */ |
Richard Henderson | 6b20875 | 2021-04-25 19:53:12 -0700 | [diff] [blame] | 805 | abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, |
| 806 | CPUArchState *env) |
ths | a04e134 | 2007-09-27 13:57:58 +0000 | [diff] [blame] | 807 | { |
Richard Henderson | 92bad94 | 2021-04-25 19:53:10 -0700 | [diff] [blame] | 808 | target_stack_t oss, *uoss = NULL; |
| 809 | abi_long ret = -TARGET_EFAULT; |
ths | a04e134 | 2007-09-27 13:57:58 +0000 | [diff] [blame] | 810 | |
Richard Henderson | 92bad94 | 2021-04-25 19:53:10 -0700 | [diff] [blame] | 811 | if (uoss_addr) { |
Richard Henderson | 92bad94 | 2021-04-25 19:53:10 -0700 | [diff] [blame] | 812 | /* Verify writability now, but do not alter user memory yet. */ |
| 813 | if (!lock_user_struct(VERIFY_WRITE, uoss, uoss_addr, 0)) { |
| 814 | goto out; |
| 815 | } |
Richard Henderson | 6b20875 | 2021-04-25 19:53:12 -0700 | [diff] [blame] | 816 | target_save_altstack(&oss, env); |
ths | a04e134 | 2007-09-27 13:57:58 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Richard Henderson | 92bad94 | 2021-04-25 19:53:10 -0700 | [diff] [blame] | 819 | if (uss_addr) { |
| 820 | target_stack_t *uss; |
Tom Musta | 0903c8b | 2014-08-12 13:53:40 -0500 | [diff] [blame] | 821 | |
Riku Voipio | 9eeb830 | 2014-04-23 11:26:34 +0300 | [diff] [blame] | 822 | if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) { |
ths | a04e134 | 2007-09-27 13:57:58 +0000 | [diff] [blame] | 823 | goto out; |
Riku Voipio | 9eeb830 | 2014-04-23 11:26:34 +0300 | [diff] [blame] | 824 | } |
Richard Henderson | ddc3e74 | 2021-04-25 19:53:13 -0700 | [diff] [blame] | 825 | ret = target_restore_altstack(uss, env); |
Richard Henderson | 92bad94 | 2021-04-25 19:53:10 -0700 | [diff] [blame] | 826 | if (ret) { |
ths | a04e134 | 2007-09-27 13:57:58 +0000 | [diff] [blame] | 827 | goto out; |
Paolo Bonzini | 7d37435 | 2018-12-13 23:37:37 +0100 | [diff] [blame] | 828 | } |
ths | a04e134 | 2007-09-27 13:57:58 +0000 | [diff] [blame] | 829 | } |
| 830 | |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 831 | if (uoss_addr) { |
Richard Henderson | 92bad94 | 2021-04-25 19:53:10 -0700 | [diff] [blame] | 832 | memcpy(uoss, &oss, sizeof(oss)); |
| 833 | unlock_user_struct(uoss, uoss_addr, 1); |
| 834 | uoss = NULL; |
ths | a04e134 | 2007-09-27 13:57:58 +0000 | [diff] [blame] | 835 | } |
ths | a04e134 | 2007-09-27 13:57:58 +0000 | [diff] [blame] | 836 | ret = 0; |
Richard Henderson | 92bad94 | 2021-04-25 19:53:10 -0700 | [diff] [blame] | 837 | |
| 838 | out: |
| 839 | if (uoss) { |
| 840 | unlock_user_struct(uoss, uoss_addr, 0); |
| 841 | } |
ths | a04e134 | 2007-09-27 13:57:58 +0000 | [diff] [blame] | 842 | return ret; |
| 843 | } |
| 844 | |
Timothy E Baldwin | ef6a778 | 2016-05-27 15:51:54 +0100 | [diff] [blame] | 845 | /* do_sigaction() return target values and host errnos */ |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 846 | int do_sigaction(int sig, const struct target_sigaction *act, |
Richard Henderson | 02fb28e | 2021-04-22 16:02:23 -0700 | [diff] [blame] | 847 | struct target_sigaction *oact, abi_ulong ka_restorer) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 848 | { |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 849 | struct target_sigaction *k; |
bellard | 773b93e | 2004-01-04 17:15:59 +0000 | [diff] [blame] | 850 | struct sigaction act1; |
| 851 | int host_sig; |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 852 | int ret = 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 853 | |
Laurent Vivier | 6bc024e | 2020-02-12 13:56:58 +0100 | [diff] [blame] | 854 | trace_signal_do_sigaction_guest(sig, TARGET_NSIG); |
| 855 | |
Ilya Leoshkevich | ee3500d | 2021-06-01 16:55:59 +0200 | [diff] [blame] | 856 | if (sig < 1 || sig > TARGET_NSIG) { |
| 857 | return -TARGET_EINVAL; |
| 858 | } |
| 859 | |
| 860 | if (act && (sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)) { |
Timothy E Baldwin | ef6a778 | 2016-05-27 15:51:54 +0100 | [diff] [blame] | 861 | return -TARGET_EINVAL; |
| 862 | } |
| 863 | |
| 864 | if (block_signals()) { |
| 865 | return -TARGET_ERESTARTSYS; |
| 866 | } |
| 867 | |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 868 | k = &sigact_table[sig - 1]; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 869 | if (oact) { |
Richard Henderson | d256587 | 2013-01-04 16:39:32 -0800 | [diff] [blame] | 870 | __put_user(k->_sa_handler, &oact->_sa_handler); |
| 871 | __put_user(k->sa_flags, &oact->sa_flags); |
Richard Henderson | 7f047de | 2017-10-31 13:53:52 +0100 | [diff] [blame] | 872 | #ifdef TARGET_ARCH_HAS_SA_RESTORER |
Richard Henderson | d256587 | 2013-01-04 16:39:32 -0800 | [diff] [blame] | 873 | __put_user(k->sa_restorer, &oact->sa_restorer); |
ths | 388bb21 | 2007-05-13 13:58:00 +0000 | [diff] [blame] | 874 | #endif |
Richard Henderson | d256587 | 2013-01-04 16:39:32 -0800 | [diff] [blame] | 875 | /* Not swapped. */ |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 876 | oact->sa_mask = k->sa_mask; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 877 | } |
| 878 | if (act) { |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 879 | /* FIXME: This is not threadsafe. */ |
Richard Henderson | d256587 | 2013-01-04 16:39:32 -0800 | [diff] [blame] | 880 | __get_user(k->_sa_handler, &act->_sa_handler); |
| 881 | __get_user(k->sa_flags, &act->sa_flags); |
Richard Henderson | 7f047de | 2017-10-31 13:53:52 +0100 | [diff] [blame] | 882 | #ifdef TARGET_ARCH_HAS_SA_RESTORER |
Richard Henderson | d256587 | 2013-01-04 16:39:32 -0800 | [diff] [blame] | 883 | __get_user(k->sa_restorer, &act->sa_restorer); |
ths | 388bb21 | 2007-05-13 13:58:00 +0000 | [diff] [blame] | 884 | #endif |
Richard Henderson | 02fb28e | 2021-04-22 16:02:23 -0700 | [diff] [blame] | 885 | #ifdef TARGET_ARCH_HAS_KA_RESTORER |
| 886 | k->ka_restorer = ka_restorer; |
| 887 | #endif |
Richard Henderson | d256587 | 2013-01-04 16:39:32 -0800 | [diff] [blame] | 888 | /* To be swapped in target_to_host_sigset. */ |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 889 | k->sa_mask = act->sa_mask; |
bellard | 773b93e | 2004-01-04 17:15:59 +0000 | [diff] [blame] | 890 | |
| 891 | /* we update the host linux signal state */ |
| 892 | host_sig = target_to_host_signal(sig); |
Laurent Vivier | 6bc024e | 2020-02-12 13:56:58 +0100 | [diff] [blame] | 893 | trace_signal_do_sigaction_host(host_sig, TARGET_NSIG); |
| 894 | if (host_sig > SIGRTMAX) { |
| 895 | /* we don't have enough host signals to map all target signals */ |
| 896 | qemu_log_mask(LOG_UNIMP, "Unsupported target signal #%d, ignored\n", |
| 897 | sig); |
| 898 | /* |
| 899 | * we don't return an error here because some programs try to |
| 900 | * register an handler for all possible rt signals even if they |
| 901 | * don't need it. |
| 902 | * An error here can abort them whereas there can be no problem |
| 903 | * to not have the signal available later. |
| 904 | * This is the case for golang, |
| 905 | * See https://github.com/golang/go/issues/33746 |
| 906 | * So we silently ignore the error. |
| 907 | */ |
| 908 | return 0; |
| 909 | } |
bellard | 773b93e | 2004-01-04 17:15:59 +0000 | [diff] [blame] | 910 | if (host_sig != SIGSEGV && host_sig != SIGBUS) { |
| 911 | sigfillset(&act1.sa_mask); |
| 912 | act1.sa_flags = SA_SIGINFO; |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 913 | if (k->sa_flags & TARGET_SA_RESTART) |
bellard | 773b93e | 2004-01-04 17:15:59 +0000 | [diff] [blame] | 914 | act1.sa_flags |= SA_RESTART; |
| 915 | /* NOTE: it is important to update the host kernel signal |
| 916 | ignore state to avoid getting unexpected interrupted |
| 917 | syscalls */ |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 918 | if (k->_sa_handler == TARGET_SIG_IGN) { |
bellard | 773b93e | 2004-01-04 17:15:59 +0000 | [diff] [blame] | 919 | act1.sa_sigaction = (void *)SIG_IGN; |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 920 | } else if (k->_sa_handler == TARGET_SIG_DFL) { |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 921 | if (fatal_signal (sig)) |
| 922 | act1.sa_sigaction = host_signal_handler; |
| 923 | else |
| 924 | act1.sa_sigaction = (void *)SIG_DFL; |
bellard | 773b93e | 2004-01-04 17:15:59 +0000 | [diff] [blame] | 925 | } else { |
| 926 | act1.sa_sigaction = host_signal_handler; |
| 927 | } |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 928 | ret = sigaction(host_sig, &act1, NULL); |
bellard | 773b93e | 2004-01-04 17:15:59 +0000 | [diff] [blame] | 929 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 930 | } |
ths | 0da46a6 | 2007-10-20 20:23:07 +0000 | [diff] [blame] | 931 | return ret; |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 932 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 933 | |
Peter Maydell | 31efaef | 2016-07-06 15:09:29 +0100 | [diff] [blame] | 934 | static void handle_pending_signal(CPUArchState *cpu_env, int sig, |
| 935 | struct emulated_sigtable *k) |
Peter Maydell | eb55250 | 2016-05-27 15:51:43 +0100 | [diff] [blame] | 936 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 937 | CPUState *cpu = env_cpu(cpu_env); |
Peter Maydell | eb55250 | 2016-05-27 15:51:43 +0100 | [diff] [blame] | 938 | abi_ulong handler; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 939 | sigset_t set; |
Peter Maydell | eb55250 | 2016-05-27 15:51:43 +0100 | [diff] [blame] | 940 | target_sigset_t target_old_set; |
| 941 | struct target_sigaction *sa; |
Peter Maydell | eb55250 | 2016-05-27 15:51:43 +0100 | [diff] [blame] | 942 | TaskState *ts = cpu->opaque; |
Peter Maydell | eb55250 | 2016-05-27 15:51:43 +0100 | [diff] [blame] | 943 | |
Paolo Bonzini | c8ee0a4 | 2015-11-13 13:52:21 +0100 | [diff] [blame] | 944 | trace_user_handle_signal(cpu_env, sig); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 945 | /* dequeue signal */ |
Timothy E Baldwin | 907f5fd | 2016-05-27 15:51:52 +0100 | [diff] [blame] | 946 | k->pending = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 947 | |
Andreas Färber | db6b81d | 2013-06-27 19:49:31 +0200 | [diff] [blame] | 948 | sig = gdb_handlesig(cpu, sig); |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 949 | if (!sig) { |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 950 | sa = NULL; |
| 951 | handler = TARGET_SIG_IGN; |
| 952 | } else { |
| 953 | sa = &sigact_table[sig - 1]; |
| 954 | handler = sa->_sa_handler; |
bellard | 1fddef4 | 2005-04-17 19:16:13 +0000 | [diff] [blame] | 955 | } |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 956 | |
Josh Kunz | 4b25a50 | 2020-02-03 18:54:14 -0800 | [diff] [blame] | 957 | if (unlikely(qemu_loglevel_mask(LOG_STRACE))) { |
Peter Maydell | 0cb581d | 2016-07-18 18:12:24 +0100 | [diff] [blame] | 958 | print_taken_signal(sig, &k->info); |
| 959 | } |
| 960 | |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 961 | if (handler == TARGET_SIG_DFL) { |
aurel32 | ca587a8 | 2008-12-18 22:44:13 +0000 | [diff] [blame] | 962 | /* default handler : ignore some signal. The other are job control or fatal */ |
| 963 | if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) { |
| 964 | kill(getpid(),SIGSTOP); |
| 965 | } else if (sig != TARGET_SIGCHLD && |
| 966 | sig != TARGET_SIGURG && |
| 967 | sig != TARGET_SIGWINCH && |
| 968 | sig != TARGET_SIGCONT) { |
Peter Maydell | c599d4d | 2016-07-28 16:44:49 +0100 | [diff] [blame] | 969 | dump_core_and_abort(sig); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 970 | } |
| 971 | } else if (handler == TARGET_SIG_IGN) { |
| 972 | /* ignore sig */ |
| 973 | } else if (handler == TARGET_SIG_ERR) { |
Peter Maydell | c599d4d | 2016-07-28 16:44:49 +0100 | [diff] [blame] | 974 | dump_core_and_abort(sig); |
bellard | 66fb976 | 2003-03-23 01:06:05 +0000 | [diff] [blame] | 975 | } else { |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 976 | /* compute the blocked signals during the handler execution */ |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 977 | sigset_t *blocked_set; |
| 978 | |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 979 | target_to_host_sigset(&set, &sa->sa_mask); |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 980 | /* SA_NODEFER indicates that the current signal should not be |
| 981 | blocked during the handler */ |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 982 | if (!(sa->sa_flags & TARGET_SA_NODEFER)) |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 983 | sigaddset(&set, target_to_host_signal(sig)); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 984 | |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 985 | /* save the previous blocked signal state to restore it at the |
| 986 | end of the signal execution (see do_sigreturn) */ |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 987 | host_to_target_sigset_internal(&target_old_set, &ts->signal_mask); |
| 988 | |
| 989 | /* block signals in the handler */ |
| 990 | blocked_set = ts->in_sigsuspend ? |
| 991 | &ts->sigsuspend_mask : &ts->signal_mask; |
| 992 | sigorset(&ts->signal_mask, blocked_set, &set); |
| 993 | ts->in_sigsuspend = 0; |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 994 | |
bellard | bc8a22c | 2003-03-30 21:02:40 +0000 | [diff] [blame] | 995 | /* if the CPU is in VM86 mode, we restore the 32 bit values */ |
j_mayer | 84409dd | 2007-04-06 08:56:50 +0000 | [diff] [blame] | 996 | #if defined(TARGET_I386) && !defined(TARGET_X86_64) |
bellard | bc8a22c | 2003-03-30 21:02:40 +0000 | [diff] [blame] | 997 | { |
| 998 | CPUX86State *env = cpu_env; |
| 999 | if (env->eflags & VM_MASK) |
| 1000 | save_v86_state(env); |
| 1001 | } |
| 1002 | #endif |
bellard | 9de5e44 | 2003-03-23 16:49:39 +0000 | [diff] [blame] | 1003 | /* prepare the stack frame of the virtual CPU */ |
Laurent Vivier | cb6ac80 | 2018-04-24 21:26:35 +0200 | [diff] [blame] | 1004 | #if defined(TARGET_ARCH_HAS_SETUP_FRAME) |
| 1005 | if (sa->sa_flags & TARGET_SA_SIGINFO) { |
| 1006 | setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env); |
| 1007 | } else { |
| 1008 | setup_frame(sig, sa, &target_old_set, cpu_env); |
| 1009 | } |
| 1010 | #else |
Richard Henderson | ff97090 | 2013-02-10 10:30:42 -0800 | [diff] [blame] | 1011 | /* These targets do not have traditional signals. */ |
Timothy E Baldwin | 907f5fd | 2016-05-27 15:51:52 +0100 | [diff] [blame] | 1012 | setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env); |
Richard Henderson | ff97090 | 2013-02-10 10:30:42 -0800 | [diff] [blame] | 1013 | #endif |
Peter Maydell | 7ec87e0 | 2016-05-27 15:51:45 +0100 | [diff] [blame] | 1014 | if (sa->sa_flags & TARGET_SA_RESETHAND) { |
pbrook | 624f797 | 2008-05-31 16:11:38 +0000 | [diff] [blame] | 1015 | sa->_sa_handler = TARGET_SIG_DFL; |
Peter Maydell | 7ec87e0 | 2016-05-27 15:51:45 +0100 | [diff] [blame] | 1016 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1017 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1018 | } |
Peter Maydell | e902d58 | 2016-05-27 15:51:44 +0100 | [diff] [blame] | 1019 | |
| 1020 | void process_pending_signals(CPUArchState *cpu_env) |
| 1021 | { |
Richard Henderson | 29a0af6 | 2019-03-22 16:07:18 -0700 | [diff] [blame] | 1022 | CPUState *cpu = env_cpu(cpu_env); |
Peter Maydell | e902d58 | 2016-05-27 15:51:44 +0100 | [diff] [blame] | 1023 | int sig; |
| 1024 | TaskState *ts = cpu->opaque; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 1025 | sigset_t set; |
| 1026 | sigset_t *blocked_set; |
Peter Maydell | e902d58 | 2016-05-27 15:51:44 +0100 | [diff] [blame] | 1027 | |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 1028 | while (qatomic_read(&ts->signal_pending)) { |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 1029 | /* FIXME: This is not threadsafe. */ |
| 1030 | sigfillset(&set); |
| 1031 | sigprocmask(SIG_SETMASK, &set, 0); |
Peter Maydell | e902d58 | 2016-05-27 15:51:44 +0100 | [diff] [blame] | 1032 | |
Peter Maydell | 8bd3773 | 2016-07-28 16:44:45 +0100 | [diff] [blame] | 1033 | restart_scan: |
Timothy E Baldwin | 655ed67 | 2016-05-27 15:51:53 +0100 | [diff] [blame] | 1034 | sig = ts->sync_signal.pending; |
| 1035 | if (sig) { |
| 1036 | /* Synchronous signals are forced, |
| 1037 | * see force_sig_info() and callers in Linux |
| 1038 | * Note that not all of our queue_signal() calls in QEMU correspond |
| 1039 | * to force_sig_info() calls in Linux (some are send_sig_info()). |
| 1040 | * However it seems like a kernel bug to me to allow the process |
| 1041 | * to block a synchronous signal since it could then just end up |
| 1042 | * looping round and round indefinitely. |
| 1043 | */ |
| 1044 | if (sigismember(&ts->signal_mask, target_to_host_signal_table[sig]) |
| 1045 | || sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) { |
| 1046 | sigdelset(&ts->signal_mask, target_to_host_signal_table[sig]); |
| 1047 | sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL; |
| 1048 | } |
| 1049 | |
Peter Maydell | 31efaef | 2016-07-06 15:09:29 +0100 | [diff] [blame] | 1050 | handle_pending_signal(cpu_env, sig, &ts->sync_signal); |
Timothy E Baldwin | 655ed67 | 2016-05-27 15:51:53 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 1053 | for (sig = 1; sig <= TARGET_NSIG; sig++) { |
| 1054 | blocked_set = ts->in_sigsuspend ? |
| 1055 | &ts->sigsuspend_mask : &ts->signal_mask; |
| 1056 | |
| 1057 | if (ts->sigtab[sig - 1].pending && |
| 1058 | (!sigismember(blocked_set, |
Timothy E Baldwin | 655ed67 | 2016-05-27 15:51:53 +0100 | [diff] [blame] | 1059 | target_to_host_signal_table[sig]))) { |
Peter Maydell | 31efaef | 2016-07-06 15:09:29 +0100 | [diff] [blame] | 1060 | handle_pending_signal(cpu_env, sig, &ts->sigtab[sig - 1]); |
Peter Maydell | 8bd3773 | 2016-07-28 16:44:45 +0100 | [diff] [blame] | 1061 | /* Restart scan from the beginning, as handle_pending_signal |
| 1062 | * might have resulted in a new synchronous signal (eg SIGSEGV). |
| 1063 | */ |
| 1064 | goto restart_scan; |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 1065 | } |
Peter Maydell | e902d58 | 2016-05-27 15:51:44 +0100 | [diff] [blame] | 1066 | } |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 1067 | |
| 1068 | /* if no signal is pending, unblock signals and recheck (the act |
| 1069 | * of unblocking might cause us to take another host signal which |
| 1070 | * will set signal_pending again). |
| 1071 | */ |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 1072 | qatomic_set(&ts->signal_pending, 0); |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 1073 | ts->in_sigsuspend = 0; |
| 1074 | set = ts->signal_mask; |
| 1075 | sigdelset(&set, SIGSEGV); |
| 1076 | sigdelset(&set, SIGBUS); |
| 1077 | sigprocmask(SIG_SETMASK, &set, 0); |
Peter Maydell | e902d58 | 2016-05-27 15:51:44 +0100 | [diff] [blame] | 1078 | } |
Peter Maydell | 3d3efba | 2016-05-27 15:51:49 +0100 | [diff] [blame] | 1079 | ts->in_sigsuspend = 0; |
Peter Maydell | e902d58 | 2016-05-27 15:51:44 +0100 | [diff] [blame] | 1080 | } |