blob: 7f435c46060d5d68f74c4ebb21487c315606f2ba [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
bellard66fb9762003-03-23 01:06:05 +00002 * Emulation of Linux signals
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard31e31b82003-02-18 22:55:36 +00004 * 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 Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard31e31b82003-02-18 22:55:36 +000018 */
Peter Maydelld39594e2016-01-26 18:17:02 +000019#include "qemu/osdep.h"
Peter Maydella70dadc2016-05-27 15:51:59 +010020#include "qemu/bitops.h"
bellard31e31b82003-02-18 22:55:36 +000021#include <sys/ucontext.h>
Mika Westerbergedf8e2a2009-04-07 09:57:11 +030022#include <sys/resource.h>
bellard31e31b82003-02-18 22:55:36 +000023
bellard3ef693a2003-03-23 20:17:16 +000024#include "qemu.h"
blueswir17d99a002009-01-14 19:00:36 +000025#include "qemu-common.h"
blueswir1992f48a2007-10-14 16:27:31 +000026#include "target_signal.h"
Paolo Bonzinic8ee0a42015-11-13 13:52:21 +010027#include "trace.h"
Laurent Vivierbefb7442018-04-24 21:26:16 +020028#include "signal-common.h"
bellard66fb9762003-03-23 01:06:05 +000029
Laurent Vivierbefb7442018-04-24 21:26:16 +020030struct target_sigaltstack target_sigaltstack_used = {
thsa04e1342007-09-27 13:57:58 +000031 .ss_sp = 0,
32 .ss_size = 0,
33 .ss_flags = TARGET_SS_DISABLE,
34};
35
pbrook624f7972008-05-31 16:11:38 +000036static struct target_sigaction sigact_table[TARGET_NSIG];
bellard31e31b82003-02-18 22:55:36 +000037
ths5fafdf22007-09-16 21:08:06 +000038static void host_signal_handler(int host_signum, siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +000039 void *puc);
40
Arnaud Patard3ca05582009-03-30 01:18:20 +020041static uint8_t host_to_target_signal_table[_NSIG] = {
bellard9e5f5282003-07-13 17:33:54 +000042 [SIGHUP] = TARGET_SIGHUP,
43 [SIGINT] = TARGET_SIGINT,
44 [SIGQUIT] = TARGET_SIGQUIT,
45 [SIGILL] = TARGET_SIGILL,
46 [SIGTRAP] = TARGET_SIGTRAP,
47 [SIGABRT] = TARGET_SIGABRT,
bellard01e3b762003-09-30 21:10:14 +000048/* [SIGIOT] = TARGET_SIGIOT,*/
bellard9e5f5282003-07-13 17:33:54 +000049 [SIGBUS] = TARGET_SIGBUS,
50 [SIGFPE] = TARGET_SIGFPE,
51 [SIGKILL] = TARGET_SIGKILL,
52 [SIGUSR1] = TARGET_SIGUSR1,
53 [SIGSEGV] = TARGET_SIGSEGV,
54 [SIGUSR2] = TARGET_SIGUSR2,
55 [SIGPIPE] = TARGET_SIGPIPE,
56 [SIGALRM] = TARGET_SIGALRM,
57 [SIGTERM] = TARGET_SIGTERM,
58#ifdef SIGSTKFLT
59 [SIGSTKFLT] = TARGET_SIGSTKFLT,
60#endif
61 [SIGCHLD] = TARGET_SIGCHLD,
62 [SIGCONT] = TARGET_SIGCONT,
63 [SIGSTOP] = TARGET_SIGSTOP,
64 [SIGTSTP] = TARGET_SIGTSTP,
65 [SIGTTIN] = TARGET_SIGTTIN,
66 [SIGTTOU] = TARGET_SIGTTOU,
67 [SIGURG] = TARGET_SIGURG,
68 [SIGXCPU] = TARGET_SIGXCPU,
69 [SIGXFSZ] = TARGET_SIGXFSZ,
70 [SIGVTALRM] = TARGET_SIGVTALRM,
71 [SIGPROF] = TARGET_SIGPROF,
72 [SIGWINCH] = TARGET_SIGWINCH,
73 [SIGIO] = TARGET_SIGIO,
74 [SIGPWR] = TARGET_SIGPWR,
75 [SIGSYS] = TARGET_SIGSYS,
76 /* next signals stay the same */
pbrook624f7972008-05-31 16:11:38 +000077 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
Dong Xu Wangb4916d72011-11-22 18:06:17 +080078 host libpthread signals. This assumes no one actually uses SIGRTMAX :-/
pbrook624f7972008-05-31 16:11:38 +000079 To fix this properly we need to do manual signal delivery multiplexed
80 over a single host signal. */
81 [__SIGRTMIN] = __SIGRTMAX,
82 [__SIGRTMAX] = __SIGRTMIN,
bellard9e5f5282003-07-13 17:33:54 +000083};
Arnaud Patard3ca05582009-03-30 01:18:20 +020084static uint8_t target_to_host_signal_table[_NSIG];
bellard9e5f5282003-07-13 17:33:54 +000085
pbrook1d9d8b52009-04-16 15:17:02 +000086int host_to_target_signal(int sig)
bellard31e31b82003-02-18 22:55:36 +000087{
Andreas Schwab167c50d2013-07-02 14:04:12 +010088 if (sig < 0 || sig >= _NSIG)
pbrook4cb05962008-05-30 18:05:19 +000089 return sig;
bellard9e5f5282003-07-13 17:33:54 +000090 return host_to_target_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +000091}
92
pbrook4cb05962008-05-30 18:05:19 +000093int target_to_host_signal(int sig)
bellard31e31b82003-02-18 22:55:36 +000094{
Andreas Schwab167c50d2013-07-02 14:04:12 +010095 if (sig < 0 || sig >= _NSIG)
pbrook4cb05962008-05-30 18:05:19 +000096 return sig;
bellard9e5f5282003-07-13 17:33:54 +000097 return target_to_host_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +000098}
99
Anthony Liguoric227f092009-10-01 16:12:16 -0500100static inline void target_sigaddset(target_sigset_t *set, int signum)
pbrookf5545b52008-05-30 22:37:07 +0000101{
102 signum--;
103 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
104 set->sig[signum / TARGET_NSIG_BPW] |= mask;
105}
106
Anthony Liguoric227f092009-10-01 16:12:16 -0500107static inline int target_sigismember(const target_sigset_t *set, int signum)
pbrookf5545b52008-05-30 22:37:07 +0000108{
109 signum--;
110 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
111 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
112}
113
Laurent Vivierbefb7442018-04-24 21:26:16 +0200114void host_to_target_sigset_internal(target_sigset_t *d,
115 const sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000116{
117 int i;
pbrookf5545b52008-05-30 22:37:07 +0000118 target_sigemptyset(d);
119 for (i = 1; i <= TARGET_NSIG; i++) {
120 if (sigismember(s, i)) {
121 target_sigaddset(d, host_to_target_signal(i));
122 }
bellard9e5f5282003-07-13 17:33:54 +0000123 }
bellard66fb9762003-03-23 01:06:05 +0000124}
125
Anthony Liguoric227f092009-10-01 16:12:16 -0500126void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
bellard92319442004-06-19 16:58:13 +0000127{
Anthony Liguoric227f092009-10-01 16:12:16 -0500128 target_sigset_t d1;
bellard92319442004-06-19 16:58:13 +0000129 int i;
130
131 host_to_target_sigset_internal(&d1, s);
132 for(i = 0;i < TARGET_NSIG_WORDS; i++)
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200133 d->sig[i] = tswapal(d1.sig[i]);
bellard92319442004-06-19 16:58:13 +0000134}
135
Laurent Vivierbefb7442018-04-24 21:26:16 +0200136void target_to_host_sigset_internal(sigset_t *d,
137 const target_sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000138{
139 int i;
pbrookf5545b52008-05-30 22:37:07 +0000140 sigemptyset(d);
141 for (i = 1; i <= TARGET_NSIG; i++) {
142 if (target_sigismember(s, i)) {
143 sigaddset(d, target_to_host_signal(i));
144 }
Timothy E Baldwinda7c8642016-05-12 18:47:27 +0100145 }
bellard66fb9762003-03-23 01:06:05 +0000146}
147
Anthony Liguoric227f092009-10-01 16:12:16 -0500148void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
bellard92319442004-06-19 16:58:13 +0000149{
Anthony Liguoric227f092009-10-01 16:12:16 -0500150 target_sigset_t s1;
bellard92319442004-06-19 16:58:13 +0000151 int i;
152
153 for(i = 0;i < TARGET_NSIG_WORDS; i++)
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200154 s1.sig[i] = tswapal(s->sig[i]);
bellard92319442004-06-19 16:58:13 +0000155 target_to_host_sigset_internal(d, &s1);
156}
ths3b46e622007-09-17 08:09:54 +0000157
blueswir1992f48a2007-10-14 16:27:31 +0000158void host_to_target_old_sigset(abi_ulong *old_sigset,
bellard66fb9762003-03-23 01:06:05 +0000159 const sigset_t *sigset)
160{
Anthony Liguoric227f092009-10-01 16:12:16 -0500161 target_sigset_t d;
bellard9e5f5282003-07-13 17:33:54 +0000162 host_to_target_sigset(&d, sigset);
163 *old_sigset = d.sig[0];
bellard66fb9762003-03-23 01:06:05 +0000164}
165
ths5fafdf22007-09-16 21:08:06 +0000166void target_to_host_old_sigset(sigset_t *sigset,
blueswir1992f48a2007-10-14 16:27:31 +0000167 const abi_ulong *old_sigset)
bellard66fb9762003-03-23 01:06:05 +0000168{
Anthony Liguoric227f092009-10-01 16:12:16 -0500169 target_sigset_t d;
bellard9e5f5282003-07-13 17:33:54 +0000170 int i;
171
172 d.sig[0] = *old_sigset;
173 for(i = 1;i < TARGET_NSIG_WORDS; i++)
174 d.sig[i] = 0;
175 target_to_host_sigset(sigset, &d);
bellard66fb9762003-03-23 01:06:05 +0000176}
177
Peter Maydell3d3efba2016-05-27 15:51:49 +0100178int block_signals(void)
179{
180 TaskState *ts = (TaskState *)thread_cpu->opaque;
181 sigset_t set;
Peter Maydell3d3efba2016-05-27 15:51:49 +0100182
183 /* It's OK to block everything including SIGSEGV, because we won't
184 * run any further guest code before unblocking signals in
185 * process_pending_signals().
186 */
187 sigfillset(&set);
188 sigprocmask(SIG_SETMASK, &set, 0);
189
Eduardo Habkost9be38592016-06-13 18:57:58 -0300190 return atomic_xchg(&ts->signal_pending, 1);
Peter Maydell3d3efba2016-05-27 15:51:49 +0100191}
192
Alex Barcelo1c275922014-03-14 14:36:55 +0000193/* Wrapper for sigprocmask function
194 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
Peter Maydell3d3efba2016-05-27 15:51:49 +0100195 * are host signal set, not guest ones. Returns -TARGET_ERESTARTSYS if
196 * a signal was already pending and the syscall must be restarted, or
197 * 0 on success.
198 * If set is NULL, this is guaranteed not to fail.
Alex Barcelo1c275922014-03-14 14:36:55 +0000199 */
200int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
201{
Peter Maydell3d3efba2016-05-27 15:51:49 +0100202 TaskState *ts = (TaskState *)thread_cpu->opaque;
203
204 if (oldset) {
205 *oldset = ts->signal_mask;
206 }
Peter Maydella7ec0f92014-03-14 14:36:56 +0000207
208 if (set) {
Peter Maydell3d3efba2016-05-27 15:51:49 +0100209 int i;
Peter Maydella7ec0f92014-03-14 14:36:56 +0000210
Peter Maydell3d3efba2016-05-27 15:51:49 +0100211 if (block_signals()) {
212 return -TARGET_ERESTARTSYS;
213 }
Peter Maydella7ec0f92014-03-14 14:36:56 +0000214
215 switch (how) {
216 case SIG_BLOCK:
Peter Maydell3d3efba2016-05-27 15:51:49 +0100217 sigorset(&ts->signal_mask, &ts->signal_mask, set);
Peter Maydella7ec0f92014-03-14 14:36:56 +0000218 break;
219 case SIG_UNBLOCK:
Peter Maydell3d3efba2016-05-27 15:51:49 +0100220 for (i = 1; i <= NSIG; ++i) {
221 if (sigismember(set, i)) {
222 sigdelset(&ts->signal_mask, i);
223 }
Peter Maydella7ec0f92014-03-14 14:36:56 +0000224 }
225 break;
226 case SIG_SETMASK:
Peter Maydell3d3efba2016-05-27 15:51:49 +0100227 ts->signal_mask = *set;
Peter Maydella7ec0f92014-03-14 14:36:56 +0000228 break;
229 default:
230 g_assert_not_reached();
231 }
Peter Maydell3d3efba2016-05-27 15:51:49 +0100232
233 /* Silently ignore attempts to change blocking status of KILL or STOP */
234 sigdelset(&ts->signal_mask, SIGKILL);
235 sigdelset(&ts->signal_mask, SIGSTOP);
Peter Maydella7ec0f92014-03-14 14:36:56 +0000236 }
Peter Maydell3d3efba2016-05-27 15:51:49 +0100237 return 0;
Alex Barcelo1c275922014-03-14 14:36:55 +0000238}
239
Peter Maydelldaa43742018-03-08 14:47:32 +0000240#if !defined(TARGET_OPENRISC) && !defined(TARGET_NIOS2)
Peter Maydell3d3efba2016-05-27 15:51:49 +0100241/* Just set the guest's signal mask to the specified value; the
242 * caller is assumed to have called block_signals() already.
243 */
Laurent Vivierbefb7442018-04-24 21:26:16 +0200244void set_sigmask(const sigset_t *set)
Peter Maydell9eede5b2016-05-27 15:51:46 +0100245{
Peter Maydell3d3efba2016-05-27 15:51:49 +0100246 TaskState *ts = (TaskState *)thread_cpu->opaque;
247
248 ts->signal_mask = *set;
Peter Maydell9eede5b2016-05-27 15:51:46 +0100249}
250#endif
251
bellard9de5e442003-03-23 16:49:39 +0000252/* siginfo conversion */
253
Anthony Liguoric227f092009-10-01 16:12:16 -0500254static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000255 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000256{
Richard Hendersona05c6402012-09-15 11:34:20 -0700257 int sig = host_to_target_signal(info->si_signo);
Peter Maydella70dadc2016-05-27 15:51:59 +0100258 int si_code = info->si_code;
259 int si_type;
bellard9de5e442003-03-23 16:49:39 +0000260 tinfo->si_signo = sig;
261 tinfo->si_errno = 0;
pbrookafd7cd92008-05-31 12:14:21 +0000262 tinfo->si_code = info->si_code;
Richard Hendersona05c6402012-09-15 11:34:20 -0700263
Peter Maydell55d72a72016-06-13 11:22:05 +0100264 /* This memset serves two purposes:
265 * (1) ensure we don't leak random junk to the guest later
266 * (2) placate false positives from gcc about fields
267 * being used uninitialized if it chooses to inline both this
268 * function and tswap_siginfo() into host_to_target_siginfo().
269 */
270 memset(tinfo->_sifields._pad, 0, sizeof(tinfo->_sifields._pad));
271
Peter Maydella70dadc2016-05-27 15:51:59 +0100272 /* This is awkward, because we have to use a combination of
273 * the si_code and si_signo to figure out which of the union's
274 * members are valid. (Within the host kernel it is always possible
275 * to tell, but the kernel carefully avoids giving userspace the
276 * high 16 bits of si_code, so we don't have the information to
277 * do this the easy way...) We therefore make our best guess,
278 * bearing in mind that a guest can spoof most of the si_codes
279 * via rt_sigqueueinfo() if it likes.
280 *
281 * Once we have made our guess, we record it in the top 16 bits of
282 * the si_code, so that tswap_siginfo() later can use it.
283 * tswap_siginfo() will strip these top bits out before writing
284 * si_code to the guest (sign-extending the lower bits).
285 */
286
287 switch (si_code) {
288 case SI_USER:
289 case SI_TKILL:
290 case SI_KERNEL:
291 /* Sent via kill(), tkill() or tgkill(), or direct from the kernel.
292 * These are the only unspoofable si_code values.
293 */
294 tinfo->_sifields._kill._pid = info->si_pid;
295 tinfo->_sifields._kill._uid = info->si_uid;
296 si_type = QEMU_SI_KILL;
297 break;
298 default:
299 /* Everything else is spoofable. Make best guess based on signal */
300 switch (sig) {
301 case TARGET_SIGCHLD:
302 tinfo->_sifields._sigchld._pid = info->si_pid;
303 tinfo->_sifields._sigchld._uid = info->si_uid;
304 tinfo->_sifields._sigchld._status
Timothy E Baldwinda7c8642016-05-12 18:47:27 +0100305 = host_to_target_waitstatus(info->si_status);
Peter Maydella70dadc2016-05-27 15:51:59 +0100306 tinfo->_sifields._sigchld._utime = info->si_utime;
307 tinfo->_sifields._sigchld._stime = info->si_stime;
308 si_type = QEMU_SI_CHLD;
309 break;
310 case TARGET_SIGIO:
311 tinfo->_sifields._sigpoll._band = info->si_band;
312 tinfo->_sifields._sigpoll._fd = info->si_fd;
313 si_type = QEMU_SI_POLL;
314 break;
315 default:
316 /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */
317 tinfo->_sifields._rt._pid = info->si_pid;
318 tinfo->_sifields._rt._uid = info->si_uid;
319 /* XXX: potential problem if 64 bit */
320 tinfo->_sifields._rt._sigval.sival_ptr
Timothy E Baldwinda7c8642016-05-12 18:47:27 +0100321 = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
Peter Maydella70dadc2016-05-27 15:51:59 +0100322 si_type = QEMU_SI_RT;
323 break;
324 }
325 break;
bellard9de5e442003-03-23 16:49:39 +0000326 }
Peter Maydella70dadc2016-05-27 15:51:59 +0100327
328 tinfo->si_code = deposit32(si_code, 16, 16, si_type);
bellard66fb9762003-03-23 01:06:05 +0000329}
330
Laurent Vivierbefb7442018-04-24 21:26:16 +0200331void tswap_siginfo(target_siginfo_t *tinfo,
332 const target_siginfo_t *info)
bellard9de5e442003-03-23 16:49:39 +0000333{
Peter Maydella70dadc2016-05-27 15:51:59 +0100334 int si_type = extract32(info->si_code, 16, 16);
335 int si_code = sextract32(info->si_code, 0, 16);
Richard Hendersona05c6402012-09-15 11:34:20 -0700336
Peter Maydella70dadc2016-05-27 15:51:59 +0100337 __put_user(info->si_signo, &tinfo->si_signo);
338 __put_user(info->si_errno, &tinfo->si_errno);
339 __put_user(si_code, &tinfo->si_code);
340
341 /* We can use our internal marker of which fields in the structure
342 * are valid, rather than duplicating the guesswork of
343 * host_to_target_siginfo_noswap() here.
344 */
345 switch (si_type) {
346 case QEMU_SI_KILL:
347 __put_user(info->_sifields._kill._pid, &tinfo->_sifields._kill._pid);
348 __put_user(info->_sifields._kill._uid, &tinfo->_sifields._kill._uid);
349 break;
350 case QEMU_SI_TIMER:
351 __put_user(info->_sifields._timer._timer1,
352 &tinfo->_sifields._timer._timer1);
353 __put_user(info->_sifields._timer._timer2,
354 &tinfo->_sifields._timer._timer2);
355 break;
356 case QEMU_SI_POLL:
357 __put_user(info->_sifields._sigpoll._band,
358 &tinfo->_sifields._sigpoll._band);
359 __put_user(info->_sifields._sigpoll._fd,
360 &tinfo->_sifields._sigpoll._fd);
361 break;
362 case QEMU_SI_FAULT:
363 __put_user(info->_sifields._sigfault._addr,
364 &tinfo->_sifields._sigfault._addr);
365 break;
366 case QEMU_SI_CHLD:
367 __put_user(info->_sifields._sigchld._pid,
368 &tinfo->_sifields._sigchld._pid);
369 __put_user(info->_sifields._sigchld._uid,
370 &tinfo->_sifields._sigchld._uid);
371 __put_user(info->_sifields._sigchld._status,
372 &tinfo->_sifields._sigchld._status);
373 __put_user(info->_sifields._sigchld._utime,
374 &tinfo->_sifields._sigchld._utime);
375 __put_user(info->_sifields._sigchld._stime,
376 &tinfo->_sifields._sigchld._stime);
377 break;
378 case QEMU_SI_RT:
379 __put_user(info->_sifields._rt._pid, &tinfo->_sifields._rt._pid);
380 __put_user(info->_sifields._rt._uid, &tinfo->_sifields._rt._uid);
381 __put_user(info->_sifields._rt._sigval.sival_ptr,
382 &tinfo->_sifields._rt._sigval.sival_ptr);
383 break;
384 default:
385 g_assert_not_reached();
bellard9de5e442003-03-23 16:49:39 +0000386 }
387}
388
Anthony Liguoric227f092009-10-01 16:12:16 -0500389void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
bellard9de5e442003-03-23 16:49:39 +0000390{
Peter Maydell55d72a72016-06-13 11:22:05 +0100391 target_siginfo_t tgt_tmp;
392 host_to_target_siginfo_noswap(&tgt_tmp, info);
393 tswap_siginfo(tinfo, &tgt_tmp);
bellard9de5e442003-03-23 16:49:39 +0000394}
395
396/* XXX: we support only POSIX RT signals are used. */
thsaa1f17c2007-07-11 22:48:58 +0000397/* XXX: find a solution for 64 bit (additional malloced data is needed) */
Anthony Liguoric227f092009-10-01 16:12:16 -0500398void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000399{
Peter Maydell90c0f082016-05-27 15:52:01 +0100400 /* This conversion is used only for the rt_sigqueueinfo syscall,
401 * and so we know that the _rt fields are the valid ones.
402 */
403 abi_ulong sival_ptr;
404
405 __get_user(info->si_signo, &tinfo->si_signo);
406 __get_user(info->si_errno, &tinfo->si_errno);
407 __get_user(info->si_code, &tinfo->si_code);
408 __get_user(info->si_pid, &tinfo->_sifields._rt._pid);
409 __get_user(info->si_uid, &tinfo->_sifields._rt._uid);
410 __get_user(sival_ptr, &tinfo->_sifields._rt._sigval.sival_ptr);
411 info->si_value.sival_ptr = (void *)(long)sival_ptr;
bellard66fb9762003-03-23 01:06:05 +0000412}
413
aurel32ca587a82008-12-18 22:44:13 +0000414static int fatal_signal (int sig)
415{
416 switch (sig) {
417 case TARGET_SIGCHLD:
418 case TARGET_SIGURG:
419 case TARGET_SIGWINCH:
420 /* Ignored by default. */
421 return 0;
422 case TARGET_SIGCONT:
423 case TARGET_SIGSTOP:
424 case TARGET_SIGTSTP:
425 case TARGET_SIGTTIN:
426 case TARGET_SIGTTOU:
427 /* Job control signals. */
428 return 0;
429 default:
430 return 1;
431 }
432}
433
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300434/* returns 1 if given signal should dump core if not handled */
435static int core_dump_signal(int sig)
436{
437 switch (sig) {
438 case TARGET_SIGABRT:
439 case TARGET_SIGFPE:
440 case TARGET_SIGILL:
441 case TARGET_SIGQUIT:
442 case TARGET_SIGSEGV:
443 case TARGET_SIGTRAP:
444 case TARGET_SIGBUS:
445 return (1);
446 default:
447 return (0);
448 }
449}
450
bellard31e31b82003-02-18 22:55:36 +0000451void signal_init(void)
452{
Peter Maydell3d3efba2016-05-27 15:51:49 +0100453 TaskState *ts = (TaskState *)thread_cpu->opaque;
bellard31e31b82003-02-18 22:55:36 +0000454 struct sigaction act;
pbrook624f7972008-05-31 16:11:38 +0000455 struct sigaction oact;
bellard9e5f5282003-07-13 17:33:54 +0000456 int i, j;
pbrook624f7972008-05-31 16:11:38 +0000457 int host_sig;
bellard31e31b82003-02-18 22:55:36 +0000458
bellard9e5f5282003-07-13 17:33:54 +0000459 /* generate signal conversion tables */
Arnaud Patard3ca05582009-03-30 01:18:20 +0200460 for(i = 1; i < _NSIG; i++) {
bellard9e5f5282003-07-13 17:33:54 +0000461 if (host_to_target_signal_table[i] == 0)
462 host_to_target_signal_table[i] = i;
463 }
Arnaud Patard3ca05582009-03-30 01:18:20 +0200464 for(i = 1; i < _NSIG; i++) {
bellard9e5f5282003-07-13 17:33:54 +0000465 j = host_to_target_signal_table[i];
466 target_to_host_signal_table[j] = i;
467 }
ths3b46e622007-09-17 08:09:54 +0000468
Peter Maydell3d3efba2016-05-27 15:51:49 +0100469 /* Set the signal mask from the host mask. */
470 sigprocmask(0, 0, &ts->signal_mask);
471
bellard9de5e442003-03-23 16:49:39 +0000472 /* set all host signal handlers. ALL signals are blocked during
473 the handlers to serialize them. */
pbrook624f7972008-05-31 16:11:38 +0000474 memset(sigact_table, 0, sizeof(sigact_table));
475
bellard9de5e442003-03-23 16:49:39 +0000476 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000477 act.sa_flags = SA_SIGINFO;
478 act.sa_sigaction = host_signal_handler;
pbrook624f7972008-05-31 16:11:38 +0000479 for(i = 1; i <= TARGET_NSIG; i++) {
480 host_sig = target_to_host_signal(i);
481 sigaction(host_sig, NULL, &oact);
482 if (oact.sa_sigaction == (void *)SIG_IGN) {
483 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
484 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
485 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
486 }
487 /* If there's already a handler installed then something has
488 gone horribly wrong, so don't even try to handle that case. */
aurel32ca587a82008-12-18 22:44:13 +0000489 /* Install some handlers for our own use. We need at least
490 SIGSEGV and SIGBUS, to detect exceptions. We can not just
491 trap all signals because it affects syscall interrupt
492 behavior. But do trap all default-fatal signals. */
493 if (fatal_signal (i))
pbrook624f7972008-05-31 16:11:38 +0000494 sigaction(host_sig, &act, NULL);
bellard31e31b82003-02-18 22:55:36 +0000495 }
bellard31e31b82003-02-18 22:55:36 +0000496}
497
Peter Maydellc599d4d2016-07-28 16:44:49 +0100498/* Force a synchronously taken signal. The kernel force_sig() function
499 * also forces the signal to "not blocked, not ignored", but for QEMU
500 * that work is done in process_pending_signals().
501 */
Laurent Vivierbefb7442018-04-24 21:26:16 +0200502void force_sig(int sig)
Peter Maydellc599d4d2016-07-28 16:44:49 +0100503{
504 CPUState *cpu = thread_cpu;
505 CPUArchState *env = cpu->env_ptr;
506 target_siginfo_t info;
507
508 info.si_signo = sig;
509 info.si_errno = 0;
510 info.si_code = TARGET_SI_KERNEL;
511 info._sifields._kill._pid = 0;
512 info._sifields._kill._uid = 0;
513 queue_signal(env, info.si_signo, QEMU_SI_KILL, &info);
514}
Peter Maydell09391662016-07-28 16:44:47 +0100515
516/* Force a SIGSEGV if we couldn't write to memory trying to set
517 * up the signal frame. oldsig is the signal we were trying to handle
518 * at the point of failure.
519 */
Michael Clark47ae93c2018-03-03 01:31:11 +1300520#if !defined(TARGET_RISCV)
Laurent Vivierbefb7442018-04-24 21:26:16 +0200521void force_sigsegv(int oldsig)
Peter Maydell09391662016-07-28 16:44:47 +0100522{
Peter Maydell09391662016-07-28 16:44:47 +0100523 if (oldsig == SIGSEGV) {
524 /* Make sure we don't try to deliver the signal again; this will
Peter Maydellc599d4d2016-07-28 16:44:49 +0100525 * end up with handle_pending_signal() calling dump_core_and_abort().
Peter Maydell09391662016-07-28 16:44:47 +0100526 */
527 sigact_table[oldsig - 1]._sa_handler = TARGET_SIG_DFL;
528 }
Peter Maydellc4b35742016-07-28 16:44:50 +0100529 force_sig(TARGET_SIGSEGV);
Peter Maydell09391662016-07-28 16:44:47 +0100530}
bellard66fb9762003-03-23 01:06:05 +0000531
Michael Clark47ae93c2018-03-03 01:31:11 +1300532#endif
533
bellard9de5e442003-03-23 16:49:39 +0000534/* abort execution with signal */
Peter Maydellc599d4d2016-07-28 16:44:49 +0100535static void QEMU_NORETURN dump_core_and_abort(int target_sig)
bellard66fb9762003-03-23 01:06:05 +0000536{
Andreas Färber0429a972013-08-26 18:14:44 +0200537 CPUState *cpu = thread_cpu;
538 CPUArchState *env = cpu->env_ptr;
539 TaskState *ts = (TaskState *)cpu->opaque;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300540 int host_sig, core_dumped = 0;
aurel32603e4fd2009-04-15 16:18:38 +0000541 struct sigaction act;
Paolo Bonzinic8ee0a42015-11-13 13:52:21 +0100542
Riku Voipio66393fb2009-12-04 15:16:32 +0200543 host_sig = target_to_host_signal(target_sig);
Paolo Bonzinic8ee0a42015-11-13 13:52:21 +0100544 trace_user_force_sig(env, target_sig, host_sig);
Andreas Färbera2247f82013-06-09 19:47:04 +0200545 gdb_signalled(env, target_sig);
aurel32603e4fd2009-04-15 16:18:38 +0000546
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300547 /* dump core if supported by target binary format */
Riku Voipio66393fb2009-12-04 15:16:32 +0200548 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300549 stop_all_tasks();
550 core_dumped =
Andreas Färbera2247f82013-06-09 19:47:04 +0200551 ((*ts->bprm->core_dump)(target_sig, env) == 0);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300552 }
553 if (core_dumped) {
554 /* we already dumped the core of target process, we don't want
555 * a coredump of qemu itself */
556 struct rlimit nodump;
557 getrlimit(RLIMIT_CORE, &nodump);
558 nodump.rlim_cur=0;
559 setrlimit(RLIMIT_CORE, &nodump);
560 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
Riku Voipio66393fb2009-12-04 15:16:32 +0200561 target_sig, strsignal(host_sig), "core dumped" );
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300562 }
563
Stefan Weil0c587512011-04-28 17:20:32 +0200564 /* The proper exit code for dying from an uncaught signal is
aurel32603e4fd2009-04-15 16:18:38 +0000565 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
566 * a negative value. To get the proper exit code we need to
567 * actually die from an uncaught signal. Here the default signal
568 * handler is installed, we send ourself a signal and we wait for
569 * it to arrive. */
570 sigfillset(&act.sa_mask);
571 act.sa_handler = SIG_DFL;
Peter Maydell3a5d30b2014-02-17 18:55:32 +0000572 act.sa_flags = 0;
aurel32603e4fd2009-04-15 16:18:38 +0000573 sigaction(host_sig, &act, NULL);
574
575 /* For some reason raise(host_sig) doesn't send the signal when
576 * statically linked on x86-64. */
577 kill(getpid(), host_sig);
578
579 /* Make sure the signal isn't masked (just reuse the mask inside
580 of act) */
581 sigdelset(&act.sa_mask, host_sig);
582 sigsuspend(&act.sa_mask);
583
584 /* unreachable */
Blue Swirla6c6f762010-03-13 14:18:50 +0000585 abort();
bellard66fb9762003-03-23 01:06:05 +0000586}
587
bellard9de5e442003-03-23 16:49:39 +0000588/* queue a signal so that it will be send to the virtual CPU as soon
589 as possible */
Peter Maydell9d2803f2016-07-28 16:44:46 +0100590int queue_signal(CPUArchState *env, int sig, int si_type,
591 target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000592{
Andreas Färber0429a972013-08-26 18:14:44 +0200593 CPUState *cpu = ENV_GET_CPU(env);
594 TaskState *ts = cpu->opaque;
bellard66fb9762003-03-23 01:06:05 +0000595
Paolo Bonzinic8ee0a42015-11-13 13:52:21 +0100596 trace_user_queue_signal(env, sig);
Peter Maydella7ec0f92014-03-14 14:36:56 +0000597
Peter Maydell9d2803f2016-07-28 16:44:46 +0100598 info->si_code = deposit32(info->si_code, 16, 16, si_type);
Peter Maydella70dadc2016-05-27 15:51:59 +0100599
Timothy E Baldwin655ed672016-05-27 15:51:53 +0100600 ts->sync_signal.info = *info;
601 ts->sync_signal.pending = sig;
Timothy E Baldwin907f5fd2016-05-27 15:51:52 +0100602 /* signal that a new signal is pending */
603 atomic_set(&ts->signal_pending, 1);
604 return 1; /* indicates that the signal was queued */
bellard9de5e442003-03-23 16:49:39 +0000605}
606
Timothy E Baldwin4d330ce2016-05-12 18:47:46 +0100607#ifndef HAVE_SAFE_SYSCALL
608static inline void rewind_if_in_safe_syscall(void *puc)
609{
610 /* Default version: never rewind */
611}
612#endif
613
ths5fafdf22007-09-16 21:08:06 +0000614static void host_signal_handler(int host_signum, siginfo_t *info,
bellard9de5e442003-03-23 16:49:39 +0000615 void *puc)
616{
Andreas Färbera2247f82013-06-09 19:47:04 +0200617 CPUArchState *env = thread_cpu->env_ptr;
Timothy E Baldwin655ed672016-05-27 15:51:53 +0100618 CPUState *cpu = ENV_GET_CPU(env);
619 TaskState *ts = cpu->opaque;
620
bellard9de5e442003-03-23 16:49:39 +0000621 int sig;
Anthony Liguoric227f092009-10-01 16:12:16 -0500622 target_siginfo_t tinfo;
Peter Maydell3d3efba2016-05-27 15:51:49 +0100623 ucontext_t *uc = puc;
Timothy E Baldwin655ed672016-05-27 15:51:53 +0100624 struct emulated_sigtable *k;
bellard9de5e442003-03-23 16:49:39 +0000625
626 /* the CPU emulator uses some host signals to detect exceptions,
aurel32eaa449b2009-01-03 13:14:52 +0000627 we forward to it some signals */
aurel32ca587a82008-12-18 22:44:13 +0000628 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
aurel32eaa449b2009-01-03 13:14:52 +0000629 && info->si_code > 0) {
bellardb346ff42003-06-15 20:05:50 +0000630 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000631 return;
632 }
633
634 /* get target signal number */
635 sig = host_to_target_signal(host_signum);
636 if (sig < 1 || sig > TARGET_NSIG)
637 return;
Paolo Bonzinic8ee0a42015-11-13 13:52:21 +0100638 trace_user_host_signal(env, host_signum, sig);
Timothy E Baldwin4d330ce2016-05-12 18:47:46 +0100639
640 rewind_if_in_safe_syscall(puc);
641
bellard9de5e442003-03-23 16:49:39 +0000642 host_to_target_siginfo_noswap(&tinfo, info);
Timothy E Baldwin655ed672016-05-27 15:51:53 +0100643 k = &ts->sigtab[sig - 1];
644 k->info = tinfo;
645 k->pending = sig;
646 ts->signal_pending = 1;
Peter Maydell3d3efba2016-05-27 15:51:49 +0100647
Timothy E Baldwin655ed672016-05-27 15:51:53 +0100648 /* Block host signals until target signal handler entered. We
649 * can't block SIGSEGV or SIGBUS while we're executing guest
650 * code in case the guest code provokes one in the window between
651 * now and it getting out to the main loop. Signals will be
652 * unblocked again in process_pending_signals().
Peter Maydell1d48fdd2016-06-14 12:49:18 +0100653 *
654 * WARNING: we cannot use sigfillset() here because the uc_sigmask
655 * field is a kernel sigset_t, which is much smaller than the
656 * libc sigset_t which sigfillset() operates on. Using sigfillset()
657 * would write 0xff bytes off the end of the structure and trash
658 * data on the struct.
659 * We can't use sizeof(uc->uc_sigmask) either, because the libc
660 * headers define the struct field with the wrong (too large) type.
Timothy E Baldwin655ed672016-05-27 15:51:53 +0100661 */
Peter Maydell1d48fdd2016-06-14 12:49:18 +0100662 memset(&uc->uc_sigmask, 0xff, SIGSET_T_SIZE);
Timothy E Baldwin655ed672016-05-27 15:51:53 +0100663 sigdelset(&uc->uc_sigmask, SIGSEGV);
664 sigdelset(&uc->uc_sigmask, SIGBUS);
665
666 /* interrupt the virtual CPU as soon as possible */
667 cpu_exit(thread_cpu);
bellard31e31b82003-02-18 22:55:36 +0000668}
669
ths0da46a62007-10-20 20:23:07 +0000670/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +0000671/* compare linux/kernel/signal.c:do_sigaltstack() */
672abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000673{
674 int ret;
675 struct target_sigaltstack oss;
676
677 /* XXX: test errors */
bellard579a97f2007-11-11 14:26:47 +0000678 if(uoss_addr)
thsa04e1342007-09-27 13:57:58 +0000679 {
680 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
681 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
682 __put_user(sas_ss_flags(sp), &oss.ss_flags);
683 }
684
bellard579a97f2007-11-11 14:26:47 +0000685 if(uss_addr)
thsa04e1342007-09-27 13:57:58 +0000686 {
bellard579a97f2007-11-11 14:26:47 +0000687 struct target_sigaltstack *uss;
688 struct target_sigaltstack ss;
Tom Musta0903c8b2014-08-12 13:53:40 -0500689 size_t minstacksize = TARGET_MINSIGSTKSZ;
690
691#if defined(TARGET_PPC64)
692 /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
693 struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
694 if (get_ppc64_abi(image) > 1) {
695 minstacksize = 4096;
696 }
697#endif
thsa04e1342007-09-27 13:57:58 +0000698
ths0da46a62007-10-20 20:23:07 +0000699 ret = -TARGET_EFAULT;
Riku Voipio9eeb8302014-04-23 11:26:34 +0300700 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
thsa04e1342007-09-27 13:57:58 +0000701 goto out;
Riku Voipio9eeb8302014-04-23 11:26:34 +0300702 }
703 __get_user(ss.ss_sp, &uss->ss_sp);
704 __get_user(ss.ss_size, &uss->ss_size);
705 __get_user(ss.ss_flags, &uss->ss_flags);
bellard579a97f2007-11-11 14:26:47 +0000706 unlock_user_struct(uss, uss_addr, 0);
thsa04e1342007-09-27 13:57:58 +0000707
ths0da46a62007-10-20 20:23:07 +0000708 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000709 if (on_sig_stack(sp))
710 goto out;
711
ths0da46a62007-10-20 20:23:07 +0000712 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000713 if (ss.ss_flags != TARGET_SS_DISABLE
714 && ss.ss_flags != TARGET_SS_ONSTACK
715 && ss.ss_flags != 0)
716 goto out;
717
718 if (ss.ss_flags == TARGET_SS_DISABLE) {
719 ss.ss_size = 0;
720 ss.ss_sp = 0;
721 } else {
ths0da46a62007-10-20 20:23:07 +0000722 ret = -TARGET_ENOMEM;
Tom Musta0903c8b2014-08-12 13:53:40 -0500723 if (ss.ss_size < minstacksize) {
thsa04e1342007-09-27 13:57:58 +0000724 goto out;
Tom Musta0903c8b2014-08-12 13:53:40 -0500725 }
thsa04e1342007-09-27 13:57:58 +0000726 }
727
728 target_sigaltstack_used.ss_sp = ss.ss_sp;
729 target_sigaltstack_used.ss_size = ss.ss_size;
730 }
731
bellard579a97f2007-11-11 14:26:47 +0000732 if (uoss_addr) {
ths0da46a62007-10-20 20:23:07 +0000733 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000734 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
thsa04e1342007-09-27 13:57:58 +0000735 goto out;
thsa04e1342007-09-27 13:57:58 +0000736 }
737
738 ret = 0;
739out:
740 return ret;
741}
742
Timothy E Baldwinef6a7782016-05-27 15:51:54 +0100743/* do_sigaction() return target values and host errnos */
bellard66fb9762003-03-23 01:06:05 +0000744int do_sigaction(int sig, const struct target_sigaction *act,
745 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000746{
pbrook624f7972008-05-31 16:11:38 +0000747 struct target_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000748 struct sigaction act1;
749 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000750 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000751
Timothy E Baldwinef6a7782016-05-27 15:51:54 +0100752 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP) {
753 return -TARGET_EINVAL;
754 }
755
756 if (block_signals()) {
757 return -TARGET_ERESTARTSYS;
758 }
759
bellard66fb9762003-03-23 01:06:05 +0000760 k = &sigact_table[sig - 1];
bellard66fb9762003-03-23 01:06:05 +0000761 if (oact) {
Richard Hendersond2565872013-01-04 16:39:32 -0800762 __put_user(k->_sa_handler, &oact->_sa_handler);
763 __put_user(k->sa_flags, &oact->sa_flags);
Richard Henderson7f047de2017-10-31 13:53:52 +0100764#ifdef TARGET_ARCH_HAS_SA_RESTORER
Richard Hendersond2565872013-01-04 16:39:32 -0800765 __put_user(k->sa_restorer, &oact->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000766#endif
Richard Hendersond2565872013-01-04 16:39:32 -0800767 /* Not swapped. */
pbrook624f7972008-05-31 16:11:38 +0000768 oact->sa_mask = k->sa_mask;
bellard66fb9762003-03-23 01:06:05 +0000769 }
770 if (act) {
pbrook624f7972008-05-31 16:11:38 +0000771 /* FIXME: This is not threadsafe. */
Richard Hendersond2565872013-01-04 16:39:32 -0800772 __get_user(k->_sa_handler, &act->_sa_handler);
773 __get_user(k->sa_flags, &act->sa_flags);
Richard Henderson7f047de2017-10-31 13:53:52 +0100774#ifdef TARGET_ARCH_HAS_SA_RESTORER
Richard Hendersond2565872013-01-04 16:39:32 -0800775 __get_user(k->sa_restorer, &act->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000776#endif
Richard Hendersond2565872013-01-04 16:39:32 -0800777 /* To be swapped in target_to_host_sigset. */
pbrook624f7972008-05-31 16:11:38 +0000778 k->sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000779
780 /* we update the host linux signal state */
781 host_sig = target_to_host_signal(sig);
782 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
783 sigfillset(&act1.sa_mask);
784 act1.sa_flags = SA_SIGINFO;
pbrook624f7972008-05-31 16:11:38 +0000785 if (k->sa_flags & TARGET_SA_RESTART)
bellard773b93e2004-01-04 17:15:59 +0000786 act1.sa_flags |= SA_RESTART;
787 /* NOTE: it is important to update the host kernel signal
788 ignore state to avoid getting unexpected interrupted
789 syscalls */
pbrook624f7972008-05-31 16:11:38 +0000790 if (k->_sa_handler == TARGET_SIG_IGN) {
bellard773b93e2004-01-04 17:15:59 +0000791 act1.sa_sigaction = (void *)SIG_IGN;
pbrook624f7972008-05-31 16:11:38 +0000792 } else if (k->_sa_handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +0000793 if (fatal_signal (sig))
794 act1.sa_sigaction = host_signal_handler;
795 else
796 act1.sa_sigaction = (void *)SIG_DFL;
bellard773b93e2004-01-04 17:15:59 +0000797 } else {
798 act1.sa_sigaction = host_signal_handler;
799 }
ths0da46a62007-10-20 20:23:07 +0000800 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000801 }
bellard66fb9762003-03-23 01:06:05 +0000802 }
ths0da46a62007-10-20 20:23:07 +0000803 return ret;
bellard66fb9762003-03-23 01:06:05 +0000804}
bellard31e31b82003-02-18 22:55:36 +0000805
Peter Maydell31efaef2016-07-06 15:09:29 +0100806static void handle_pending_signal(CPUArchState *cpu_env, int sig,
807 struct emulated_sigtable *k)
Peter Maydelleb552502016-05-27 15:51:43 +0100808{
809 CPUState *cpu = ENV_GET_CPU(cpu_env);
810 abi_ulong handler;
Peter Maydell3d3efba2016-05-27 15:51:49 +0100811 sigset_t set;
Peter Maydelleb552502016-05-27 15:51:43 +0100812 target_sigset_t target_old_set;
813 struct target_sigaction *sa;
Peter Maydelleb552502016-05-27 15:51:43 +0100814 TaskState *ts = cpu->opaque;
Peter Maydelleb552502016-05-27 15:51:43 +0100815
Paolo Bonzinic8ee0a42015-11-13 13:52:21 +0100816 trace_user_handle_signal(cpu_env, sig);
bellard66fb9762003-03-23 01:06:05 +0000817 /* dequeue signal */
Timothy E Baldwin907f5fd2016-05-27 15:51:52 +0100818 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +0000819
Andreas Färberdb6b81d2013-06-27 19:49:31 +0200820 sig = gdb_handlesig(cpu, sig);
bellard1fddef42005-04-17 19:16:13 +0000821 if (!sig) {
aurel32ca587a82008-12-18 22:44:13 +0000822 sa = NULL;
823 handler = TARGET_SIG_IGN;
824 } else {
825 sa = &sigact_table[sig - 1];
826 handler = sa->_sa_handler;
bellard1fddef42005-04-17 19:16:13 +0000827 }
bellard66fb9762003-03-23 01:06:05 +0000828
Peter Maydell0cb581d2016-07-18 18:12:24 +0100829 if (do_strace) {
830 print_taken_signal(sig, &k->info);
831 }
832
bellard66fb9762003-03-23 01:06:05 +0000833 if (handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +0000834 /* default handler : ignore some signal. The other are job control or fatal */
835 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
836 kill(getpid(),SIGSTOP);
837 } else if (sig != TARGET_SIGCHLD &&
838 sig != TARGET_SIGURG &&
839 sig != TARGET_SIGWINCH &&
840 sig != TARGET_SIGCONT) {
Peter Maydellc599d4d2016-07-28 16:44:49 +0100841 dump_core_and_abort(sig);
bellard66fb9762003-03-23 01:06:05 +0000842 }
843 } else if (handler == TARGET_SIG_IGN) {
844 /* ignore sig */
845 } else if (handler == TARGET_SIG_ERR) {
Peter Maydellc599d4d2016-07-28 16:44:49 +0100846 dump_core_and_abort(sig);
bellard66fb9762003-03-23 01:06:05 +0000847 } else {
bellard9de5e442003-03-23 16:49:39 +0000848 /* compute the blocked signals during the handler execution */
Peter Maydell3d3efba2016-05-27 15:51:49 +0100849 sigset_t *blocked_set;
850
pbrook624f7972008-05-31 16:11:38 +0000851 target_to_host_sigset(&set, &sa->sa_mask);
bellard9de5e442003-03-23 16:49:39 +0000852 /* SA_NODEFER indicates that the current signal should not be
853 blocked during the handler */
pbrook624f7972008-05-31 16:11:38 +0000854 if (!(sa->sa_flags & TARGET_SA_NODEFER))
bellard9de5e442003-03-23 16:49:39 +0000855 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +0000856
bellard9de5e442003-03-23 16:49:39 +0000857 /* save the previous blocked signal state to restore it at the
858 end of the signal execution (see do_sigreturn) */
Peter Maydell3d3efba2016-05-27 15:51:49 +0100859 host_to_target_sigset_internal(&target_old_set, &ts->signal_mask);
860
861 /* block signals in the handler */
862 blocked_set = ts->in_sigsuspend ?
863 &ts->sigsuspend_mask : &ts->signal_mask;
864 sigorset(&ts->signal_mask, blocked_set, &set);
865 ts->in_sigsuspend = 0;
bellard9de5e442003-03-23 16:49:39 +0000866
bellardbc8a22c2003-03-30 21:02:40 +0000867 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +0000868#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +0000869 {
870 CPUX86State *env = cpu_env;
871 if (env->eflags & VM_MASK)
872 save_v86_state(env);
873 }
874#endif
bellard9de5e442003-03-23 16:49:39 +0000875 /* prepare the stack frame of the virtual CPU */
Chen Gangd0924a22015-09-12 23:32:30 +0800876#if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64) \
Benjamin Herrenschmidt95cda4c2016-08-03 22:38:51 +1000877 || defined(TARGET_OPENRISC) || defined(TARGET_TILEGX) \
Marek Vasuta0a839b2017-01-18 23:01:42 +0100878 || defined(TARGET_PPC64) || defined(TARGET_HPPA) \
Michael Clark47ae93c2018-03-03 01:31:11 +1300879 || defined(TARGET_NIOS2) || defined(TARGET_X86_64) \
Max Filippovba7651f2017-01-25 10:54:11 -0800880 || defined(TARGET_RISCV) || defined(TARGET_XTENSA)
Richard Hendersonff970902013-02-10 10:30:42 -0800881 /* These targets do not have traditional signals. */
Timothy E Baldwin907f5fd2016-05-27 15:51:52 +0100882 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
Richard Hendersonff970902013-02-10 10:30:42 -0800883#else
pbrook624f7972008-05-31 16:11:38 +0000884 if (sa->sa_flags & TARGET_SA_SIGINFO)
Timothy E Baldwin907f5fd2016-05-27 15:51:52 +0100885 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +0000886 else
pbrook624f7972008-05-31 16:11:38 +0000887 setup_frame(sig, sa, &target_old_set, cpu_env);
Richard Hendersonff970902013-02-10 10:30:42 -0800888#endif
Peter Maydell7ec87e02016-05-27 15:51:45 +0100889 if (sa->sa_flags & TARGET_SA_RESETHAND) {
pbrook624f7972008-05-31 16:11:38 +0000890 sa->_sa_handler = TARGET_SIG_DFL;
Peter Maydell7ec87e02016-05-27 15:51:45 +0100891 }
bellard31e31b82003-02-18 22:55:36 +0000892 }
bellard31e31b82003-02-18 22:55:36 +0000893}
Peter Maydelle902d582016-05-27 15:51:44 +0100894
895void process_pending_signals(CPUArchState *cpu_env)
896{
897 CPUState *cpu = ENV_GET_CPU(cpu_env);
898 int sig;
899 TaskState *ts = cpu->opaque;
Peter Maydell3d3efba2016-05-27 15:51:49 +0100900 sigset_t set;
901 sigset_t *blocked_set;
Peter Maydelle902d582016-05-27 15:51:44 +0100902
Peter Maydell3d3efba2016-05-27 15:51:49 +0100903 while (atomic_read(&ts->signal_pending)) {
904 /* FIXME: This is not threadsafe. */
905 sigfillset(&set);
906 sigprocmask(SIG_SETMASK, &set, 0);
Peter Maydelle902d582016-05-27 15:51:44 +0100907
Peter Maydell8bd37732016-07-28 16:44:45 +0100908 restart_scan:
Timothy E Baldwin655ed672016-05-27 15:51:53 +0100909 sig = ts->sync_signal.pending;
910 if (sig) {
911 /* Synchronous signals are forced,
912 * see force_sig_info() and callers in Linux
913 * Note that not all of our queue_signal() calls in QEMU correspond
914 * to force_sig_info() calls in Linux (some are send_sig_info()).
915 * However it seems like a kernel bug to me to allow the process
916 * to block a synchronous signal since it could then just end up
917 * looping round and round indefinitely.
918 */
919 if (sigismember(&ts->signal_mask, target_to_host_signal_table[sig])
920 || sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) {
921 sigdelset(&ts->signal_mask, target_to_host_signal_table[sig]);
922 sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL;
923 }
924
Peter Maydell31efaef2016-07-06 15:09:29 +0100925 handle_pending_signal(cpu_env, sig, &ts->sync_signal);
Timothy E Baldwin655ed672016-05-27 15:51:53 +0100926 }
927
Peter Maydell3d3efba2016-05-27 15:51:49 +0100928 for (sig = 1; sig <= TARGET_NSIG; sig++) {
929 blocked_set = ts->in_sigsuspend ?
930 &ts->sigsuspend_mask : &ts->signal_mask;
931
932 if (ts->sigtab[sig - 1].pending &&
933 (!sigismember(blocked_set,
Timothy E Baldwin655ed672016-05-27 15:51:53 +0100934 target_to_host_signal_table[sig]))) {
Peter Maydell31efaef2016-07-06 15:09:29 +0100935 handle_pending_signal(cpu_env, sig, &ts->sigtab[sig - 1]);
Peter Maydell8bd37732016-07-28 16:44:45 +0100936 /* Restart scan from the beginning, as handle_pending_signal
937 * might have resulted in a new synchronous signal (eg SIGSEGV).
938 */
939 goto restart_scan;
Peter Maydell3d3efba2016-05-27 15:51:49 +0100940 }
Peter Maydelle902d582016-05-27 15:51:44 +0100941 }
Peter Maydell3d3efba2016-05-27 15:51:49 +0100942
943 /* if no signal is pending, unblock signals and recheck (the act
944 * of unblocking might cause us to take another host signal which
945 * will set signal_pending again).
946 */
947 atomic_set(&ts->signal_pending, 0);
948 ts->in_sigsuspend = 0;
949 set = ts->signal_mask;
950 sigdelset(&set, SIGSEGV);
951 sigdelset(&set, SIGBUS);
952 sigprocmask(SIG_SETMASK, &set, 0);
Peter Maydelle902d582016-05-27 15:51:44 +0100953 }
Peter Maydell3d3efba2016-05-27 15:51:49 +0100954 ts->in_sigsuspend = 0;
Peter Maydelle902d582016-05-27 15:51:44 +0100955}