blob: 63ac2df53b768012e3ca97a279caaa4797f0225c [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"
Alex Bennéed96bf492023-03-02 18:57:47 -080021#include "gdbstub/user.h"
Philippe Mathieu-Daudé74781c02023-12-06 20:27:32 +010022#include "exec/page-protection.h"
Richard Hendersone6037d02021-09-16 14:44:17 -070023#include "hw/core/tcg-cpu-ops.h"
Peter Maydell85b4fa02021-09-08 16:44:04 +010024
bellard31e31b82003-02-18 22:55:36 +000025#include <sys/ucontext.h>
Mika Westerbergedf8e2a2009-04-07 09:57:11 +030026#include <sys/resource.h>
bellard31e31b82003-02-18 22:55:36 +000027
bellard3ef693a2003-03-23 20:17:16 +000028#include "qemu.h"
Peter Maydell3b249d22021-09-08 16:44:03 +010029#include "user-internals.h"
Peter Maydella44d57a2021-09-08 16:43:58 +010030#include "strace.h"
Peter Maydell3ad0a762021-09-08 16:44:00 +010031#include "loader.h"
Paolo Bonzinic8ee0a42015-11-13 13:52:21 +010032#include "trace.h"
Laurent Vivierbefb7442018-04-24 21:26:16 +020033#include "signal-common.h"
Richard Hendersone6037d02021-09-16 14:44:17 -070034#include "host-signal.h"
Richard Hendersonbbf15aa2021-11-17 16:14:00 +010035#include "user/safe-syscall.h"
Helge Deller7dfd3ca2023-08-12 18:43:14 +020036#include "tcg/tcg.h"
bellard66fb9762003-03-23 01:06:05 +000037
Gustavo Romerof84e3132024-03-09 03:08:59 +000038/* target_siginfo_t must fit in gdbstub's siginfo save area. */
39QEMU_BUILD_BUG_ON(sizeof(target_siginfo_t) > MAX_SIGINFO_LENGTH);
40
pbrook624f7972008-05-31 16:11:38 +000041static struct target_sigaction sigact_table[TARGET_NSIG];
bellard31e31b82003-02-18 22:55:36 +000042
ths5fafdf22007-09-16 21:08:06 +000043static void host_signal_handler(int host_signum, siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +000044 void *puc);
45
Richard Hendersondb2af692021-09-29 09:05:28 -040046/* Fallback addresses into sigtramp page. */
47abi_ulong default_sigreturn;
48abi_ulong default_rt_sigreturn;
Laurent Vivier9fcff3a2020-02-12 13:56:57 +010049
50/*
Richard Hendersonb60b91a2023-08-22 20:45:38 -070051 * System includes define _NSIG as SIGRTMAX + 1, but qemu (like the kernel)
52 * defines TARGET_NSIG as TARGET_SIGRTMAX and the first signal is 1.
Laurent Vivier9fcff3a2020-02-12 13:56:57 +010053 * Signal number 0 is reserved for use as kill(pid, 0), to test whether
54 * a process exists without sending it a signal.
55 */
Michael Forney144bff02021-05-26 12:02:03 -070056#ifdef __SIGRTMAX
Laurent Vivier9fcff3a2020-02-12 13:56:57 +010057QEMU_BUILD_BUG_ON(__SIGRTMAX + 1 != _NSIG);
Michael Forney144bff02021-05-26 12:02:03 -070058#endif
Arnaud Patard3ca05582009-03-30 01:18:20 +020059static uint8_t host_to_target_signal_table[_NSIG] = {
Helge Deller7b72aa12022-09-18 21:45:44 +020060#define MAKE_SIG_ENTRY(sig) [sig] = TARGET_##sig,
61 MAKE_SIGNAL_LIST
62#undef MAKE_SIG_ENTRY
bellard9e5f5282003-07-13 17:33:54 +000063};
bellard9e5f5282003-07-13 17:33:54 +000064
Laurent Vivier9fcff3a2020-02-12 13:56:57 +010065static uint8_t target_to_host_signal_table[TARGET_NSIG + 1];
66
67/* valid sig is between 1 and _NSIG - 1 */
pbrook1d9d8b52009-04-16 15:17:02 +000068int host_to_target_signal(int sig)
bellard31e31b82003-02-18 22:55:36 +000069{
Richard Hendersonb60b91a2023-08-22 20:45:38 -070070 if (sig < 1) {
pbrook4cb05962008-05-30 18:05:19 +000071 return sig;
Laurent Vivier9fcff3a2020-02-12 13:56:57 +010072 }
Richard Hendersonb60b91a2023-08-22 20:45:38 -070073 if (sig >= _NSIG) {
74 return TARGET_NSIG + 1;
75 }
bellard9e5f5282003-07-13 17:33:54 +000076 return host_to_target_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +000077}
78
Laurent Vivier9fcff3a2020-02-12 13:56:57 +010079/* valid sig is between 1 and TARGET_NSIG */
pbrook4cb05962008-05-30 18:05:19 +000080int target_to_host_signal(int sig)
bellard31e31b82003-02-18 22:55:36 +000081{
Richard Hendersonb60b91a2023-08-22 20:45:38 -070082 if (sig < 1) {
pbrook4cb05962008-05-30 18:05:19 +000083 return sig;
Laurent Vivier9fcff3a2020-02-12 13:56:57 +010084 }
Richard Hendersonb60b91a2023-08-22 20:45:38 -070085 if (sig > TARGET_NSIG) {
86 return _NSIG;
87 }
bellard9e5f5282003-07-13 17:33:54 +000088 return target_to_host_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +000089}
90
Anthony Liguoric227f092009-10-01 16:12:16 -050091static inline void target_sigaddset(target_sigset_t *set, int signum)
pbrookf5545b52008-05-30 22:37:07 +000092{
93 signum--;
94 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
95 set->sig[signum / TARGET_NSIG_BPW] |= mask;
96}
97
Anthony Liguoric227f092009-10-01 16:12:16 -050098static inline int target_sigismember(const target_sigset_t *set, int signum)
pbrookf5545b52008-05-30 22:37:07 +000099{
100 signum--;
101 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
102 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
103}
104
Laurent Vivierbefb7442018-04-24 21:26:16 +0200105void host_to_target_sigset_internal(target_sigset_t *d,
106 const sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000107{
Laurent Vivier9fcff3a2020-02-12 13:56:57 +0100108 int host_sig, target_sig;
pbrookf5545b52008-05-30 22:37:07 +0000109 target_sigemptyset(d);
Laurent Vivier9fcff3a2020-02-12 13:56:57 +0100110 for (host_sig = 1; host_sig < _NSIG; host_sig++) {
111 target_sig = host_to_target_signal(host_sig);
112 if (target_sig < 1 || target_sig > TARGET_NSIG) {
113 continue;
114 }
115 if (sigismember(s, host_sig)) {
116 target_sigaddset(d, target_sig);
pbrookf5545b52008-05-30 22:37:07 +0000117 }
bellard9e5f5282003-07-13 17:33:54 +0000118 }
bellard66fb9762003-03-23 01:06:05 +0000119}
120
Anthony Liguoric227f092009-10-01 16:12:16 -0500121void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
bellard92319442004-06-19 16:58:13 +0000122{
Anthony Liguoric227f092009-10-01 16:12:16 -0500123 target_sigset_t d1;
bellard92319442004-06-19 16:58:13 +0000124 int i;
125
126 host_to_target_sigset_internal(&d1, s);
127 for(i = 0;i < TARGET_NSIG_WORDS; i++)
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200128 d->sig[i] = tswapal(d1.sig[i]);
bellard92319442004-06-19 16:58:13 +0000129}
130
Laurent Vivierbefb7442018-04-24 21:26:16 +0200131void target_to_host_sigset_internal(sigset_t *d,
132 const target_sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000133{
Laurent Vivier9fcff3a2020-02-12 13:56:57 +0100134 int host_sig, target_sig;
pbrookf5545b52008-05-30 22:37:07 +0000135 sigemptyset(d);
Laurent Vivier9fcff3a2020-02-12 13:56:57 +0100136 for (target_sig = 1; target_sig <= TARGET_NSIG; target_sig++) {
137 host_sig = target_to_host_signal(target_sig);
138 if (host_sig < 1 || host_sig >= _NSIG) {
139 continue;
140 }
141 if (target_sigismember(s, target_sig)) {
142 sigaddset(d, host_sig);
pbrookf5545b52008-05-30 22:37:07 +0000143 }
Timothy E Baldwinda7c8642016-05-12 18:47:27 +0100144 }
bellard66fb9762003-03-23 01:06:05 +0000145}
146
Anthony Liguoric227f092009-10-01 16:12:16 -0500147void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
bellard92319442004-06-19 16:58:13 +0000148{
Anthony Liguoric227f092009-10-01 16:12:16 -0500149 target_sigset_t s1;
bellard92319442004-06-19 16:58:13 +0000150 int i;
151
152 for(i = 0;i < TARGET_NSIG_WORDS; i++)
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200153 s1.sig[i] = tswapal(s->sig[i]);
bellard92319442004-06-19 16:58:13 +0000154 target_to_host_sigset_internal(d, &s1);
155}
ths3b46e622007-09-17 08:09:54 +0000156
blueswir1992f48a2007-10-14 16:27:31 +0000157void host_to_target_old_sigset(abi_ulong *old_sigset,
bellard66fb9762003-03-23 01:06:05 +0000158 const sigset_t *sigset)
159{
Anthony Liguoric227f092009-10-01 16:12:16 -0500160 target_sigset_t d;
bellard9e5f5282003-07-13 17:33:54 +0000161 host_to_target_sigset(&d, sigset);
162 *old_sigset = d.sig[0];
bellard66fb9762003-03-23 01:06:05 +0000163}
164
ths5fafdf22007-09-16 21:08:06 +0000165void target_to_host_old_sigset(sigset_t *sigset,
blueswir1992f48a2007-10-14 16:27:31 +0000166 const abi_ulong *old_sigset)
bellard66fb9762003-03-23 01:06:05 +0000167{
Anthony Liguoric227f092009-10-01 16:12:16 -0500168 target_sigset_t d;
bellard9e5f5282003-07-13 17:33:54 +0000169 int i;
170
171 d.sig[0] = *old_sigset;
172 for(i = 1;i < TARGET_NSIG_WORDS; i++)
173 d.sig[i] = 0;
174 target_to_host_sigset(sigset, &d);
bellard66fb9762003-03-23 01:06:05 +0000175}
176
Peter Maydell3d3efba2016-05-27 15:51:49 +0100177int block_signals(void)
178{
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000179 TaskState *ts = get_task_state(thread_cpu);
Peter Maydell3d3efba2016-05-27 15:51:49 +0100180 sigset_t set;
Peter Maydell3d3efba2016-05-27 15:51:49 +0100181
182 /* It's OK to block everything including SIGSEGV, because we won't
183 * run any further guest code before unblocking signals in
184 * process_pending_signals().
185 */
186 sigfillset(&set);
187 sigprocmask(SIG_SETMASK, &set, 0);
188
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100189 return qatomic_xchg(&ts->signal_pending, 1);
Peter Maydell3d3efba2016-05-27 15:51:49 +0100190}
191
Alex Barcelo1c275922014-03-14 14:36:55 +0000192/* Wrapper for sigprocmask function
193 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
Richard Hendersonaf254a22021-11-22 19:47:33 +0100194 * are host signal set, not guest ones. Returns -QEMU_ERESTARTSYS if
Peter Maydell3d3efba2016-05-27 15:51:49 +0100195 * a signal was already pending and the syscall must be restarted, or
196 * 0 on success.
197 * If set is NULL, this is guaranteed not to fail.
Alex Barcelo1c275922014-03-14 14:36:55 +0000198 */
199int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
200{
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000201 TaskState *ts = get_task_state(thread_cpu);
Peter Maydell3d3efba2016-05-27 15:51:49 +0100202
203 if (oldset) {
204 *oldset = ts->signal_mask;
205 }
Peter Maydella7ec0f92014-03-14 14:36:56 +0000206
207 if (set) {
Peter Maydell3d3efba2016-05-27 15:51:49 +0100208 int i;
Peter Maydella7ec0f92014-03-14 14:36:56 +0000209
Peter Maydell3d3efba2016-05-27 15:51:49 +0100210 if (block_signals()) {
Richard Hendersonaf254a22021-11-22 19:47:33 +0100211 return -QEMU_ERESTARTSYS;
Peter Maydell3d3efba2016-05-27 15:51:49 +0100212 }
Peter Maydella7ec0f92014-03-14 14:36:56 +0000213
214 switch (how) {
215 case SIG_BLOCK:
Peter Maydell3d3efba2016-05-27 15:51:49 +0100216 sigorset(&ts->signal_mask, &ts->signal_mask, set);
Peter Maydella7ec0f92014-03-14 14:36:56 +0000217 break;
218 case SIG_UNBLOCK:
Peter Maydell3d3efba2016-05-27 15:51:49 +0100219 for (i = 1; i <= NSIG; ++i) {
220 if (sigismember(set, i)) {
221 sigdelset(&ts->signal_mask, i);
222 }
Peter Maydella7ec0f92014-03-14 14:36:56 +0000223 }
224 break;
225 case SIG_SETMASK:
Peter Maydell3d3efba2016-05-27 15:51:49 +0100226 ts->signal_mask = *set;
Peter Maydella7ec0f92014-03-14 14:36:56 +0000227 break;
228 default:
229 g_assert_not_reached();
230 }
Peter Maydell3d3efba2016-05-27 15:51:49 +0100231
232 /* Silently ignore attempts to change blocking status of KILL or STOP */
233 sigdelset(&ts->signal_mask, SIGKILL);
234 sigdelset(&ts->signal_mask, SIGSTOP);
Peter Maydella7ec0f92014-03-14 14:36:56 +0000235 }
Peter Maydell3d3efba2016-05-27 15:51:49 +0100236 return 0;
Alex Barcelo1c275922014-03-14 14:36:55 +0000237}
238
Peter Maydell3d3efba2016-05-27 15:51:49 +0100239/* Just set the guest's signal mask to the specified value; the
240 * caller is assumed to have called block_signals() already.
241 */
Laurent Vivierbefb7442018-04-24 21:26:16 +0200242void set_sigmask(const sigset_t *set)
Peter Maydell9eede5b2016-05-27 15:51:46 +0100243{
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000244 TaskState *ts = get_task_state(thread_cpu);
Peter Maydell3d3efba2016-05-27 15:51:49 +0100245
246 ts->signal_mask = *set;
Peter Maydell9eede5b2016-05-27 15:51:46 +0100247}
Peter Maydell9eede5b2016-05-27 15:51:46 +0100248
Laurent Vivier465e2372018-04-11 21:23:47 +0200249/* sigaltstack management */
250
251int on_sig_stack(unsigned long sp)
252{
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000253 TaskState *ts = get_task_state(thread_cpu);
Peter Maydell5bfce0b2019-07-25 14:16:45 +0100254
255 return (sp - ts->sigaltstack_used.ss_sp
256 < ts->sigaltstack_used.ss_size);
Laurent Vivier465e2372018-04-11 21:23:47 +0200257}
258
259int sas_ss_flags(unsigned long sp)
260{
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000261 TaskState *ts = get_task_state(thread_cpu);
Peter Maydell5bfce0b2019-07-25 14:16:45 +0100262
263 return (ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE
Laurent Vivier465e2372018-04-11 21:23:47 +0200264 : on_sig_stack(sp) ? SS_ONSTACK : 0);
265}
266
267abi_ulong target_sigsp(abi_ulong sp, struct target_sigaction *ka)
268{
269 /*
270 * This is the X/Open sanctioned signal stack switching.
271 */
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000272 TaskState *ts = get_task_state(thread_cpu);
Peter Maydell5bfce0b2019-07-25 14:16:45 +0100273
Laurent Vivier465e2372018-04-11 21:23:47 +0200274 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
Peter Maydell5bfce0b2019-07-25 14:16:45 +0100275 return ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
Laurent Vivier465e2372018-04-11 21:23:47 +0200276 }
277 return sp;
278}
279
280void target_save_altstack(target_stack_t *uss, CPUArchState *env)
281{
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000282 TaskState *ts = get_task_state(thread_cpu);
Peter Maydell5bfce0b2019-07-25 14:16:45 +0100283
284 __put_user(ts->sigaltstack_used.ss_sp, &uss->ss_sp);
Laurent Vivier465e2372018-04-11 21:23:47 +0200285 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &uss->ss_flags);
Peter Maydell5bfce0b2019-07-25 14:16:45 +0100286 __put_user(ts->sigaltstack_used.ss_size, &uss->ss_size);
Laurent Vivier465e2372018-04-11 21:23:47 +0200287}
288
Richard Hendersonddc3e742021-04-25 19:53:13 -0700289abi_long target_restore_altstack(target_stack_t *uss, CPUArchState *env)
Richard Henderson92bad942021-04-25 19:53:10 -0700290{
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000291 TaskState *ts = get_task_state(thread_cpu);
Richard Henderson92bad942021-04-25 19:53:10 -0700292 size_t minstacksize = TARGET_MINSIGSTKSZ;
293 target_stack_t ss;
294
295#if defined(TARGET_PPC64)
296 /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
297 struct image_info *image = ts->info;
298 if (get_ppc64_abi(image) > 1) {
299 minstacksize = 4096;
300 }
301#endif
302
303 __get_user(ss.ss_sp, &uss->ss_sp);
304 __get_user(ss.ss_size, &uss->ss_size);
305 __get_user(ss.ss_flags, &uss->ss_flags);
306
Richard Hendersonddc3e742021-04-25 19:53:13 -0700307 if (on_sig_stack(get_sp_from_cpustate(env))) {
Richard Henderson92bad942021-04-25 19:53:10 -0700308 return -TARGET_EPERM;
309 }
310
311 switch (ss.ss_flags) {
312 default:
313 return -TARGET_EINVAL;
314
315 case TARGET_SS_DISABLE:
316 ss.ss_size = 0;
317 ss.ss_sp = 0;
318 break;
319
320 case TARGET_SS_ONSTACK:
321 case 0:
322 if (ss.ss_size < minstacksize) {
323 return -TARGET_ENOMEM;
324 }
325 break;
326 }
327
328 ts->sigaltstack_used.ss_sp = ss.ss_sp;
329 ts->sigaltstack_used.ss_size = ss.ss_size;
330 return 0;
331}
332
bellard9de5e442003-03-23 16:49:39 +0000333/* siginfo conversion */
334
Anthony Liguoric227f092009-10-01 16:12:16 -0500335static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000336 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000337{
Richard Hendersona05c6402012-09-15 11:34:20 -0700338 int sig = host_to_target_signal(info->si_signo);
Peter Maydella70dadc2016-05-27 15:51:59 +0100339 int si_code = info->si_code;
340 int si_type;
bellard9de5e442003-03-23 16:49:39 +0000341 tinfo->si_signo = sig;
342 tinfo->si_errno = 0;
pbrookafd7cd92008-05-31 12:14:21 +0000343 tinfo->si_code = info->si_code;
Richard Hendersona05c6402012-09-15 11:34:20 -0700344
Peter Maydell55d72a72016-06-13 11:22:05 +0100345 /* This memset serves two purposes:
346 * (1) ensure we don't leak random junk to the guest later
347 * (2) placate false positives from gcc about fields
348 * being used uninitialized if it chooses to inline both this
349 * function and tswap_siginfo() into host_to_target_siginfo().
350 */
351 memset(tinfo->_sifields._pad, 0, sizeof(tinfo->_sifields._pad));
352
Peter Maydella70dadc2016-05-27 15:51:59 +0100353 /* This is awkward, because we have to use a combination of
354 * the si_code and si_signo to figure out which of the union's
355 * members are valid. (Within the host kernel it is always possible
356 * to tell, but the kernel carefully avoids giving userspace the
357 * high 16 bits of si_code, so we don't have the information to
358 * do this the easy way...) We therefore make our best guess,
359 * bearing in mind that a guest can spoof most of the si_codes
360 * via rt_sigqueueinfo() if it likes.
361 *
362 * Once we have made our guess, we record it in the top 16 bits of
363 * the si_code, so that tswap_siginfo() later can use it.
364 * tswap_siginfo() will strip these top bits out before writing
365 * si_code to the guest (sign-extending the lower bits).
366 */
367
368 switch (si_code) {
369 case SI_USER:
370 case SI_TKILL:
371 case SI_KERNEL:
372 /* Sent via kill(), tkill() or tgkill(), or direct from the kernel.
373 * These are the only unspoofable si_code values.
374 */
375 tinfo->_sifields._kill._pid = info->si_pid;
376 tinfo->_sifields._kill._uid = info->si_uid;
377 si_type = QEMU_SI_KILL;
378 break;
379 default:
380 /* Everything else is spoofable. Make best guess based on signal */
381 switch (sig) {
382 case TARGET_SIGCHLD:
383 tinfo->_sifields._sigchld._pid = info->si_pid;
384 tinfo->_sifields._sigchld._uid = info->si_uid;
Matthias Schiffer139e5de2021-10-23 21:59:10 +0200385 if (si_code == CLD_EXITED)
386 tinfo->_sifields._sigchld._status = info->si_status;
387 else
388 tinfo->_sifields._sigchld._status
389 = host_to_target_signal(info->si_status & 0x7f)
390 | (info->si_status & ~0x7f);
Peter Maydella70dadc2016-05-27 15:51:59 +0100391 tinfo->_sifields._sigchld._utime = info->si_utime;
392 tinfo->_sifields._sigchld._stime = info->si_stime;
393 si_type = QEMU_SI_CHLD;
394 break;
395 case TARGET_SIGIO:
396 tinfo->_sifields._sigpoll._band = info->si_band;
397 tinfo->_sifields._sigpoll._fd = info->si_fd;
398 si_type = QEMU_SI_POLL;
399 break;
400 default:
401 /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */
402 tinfo->_sifields._rt._pid = info->si_pid;
403 tinfo->_sifields._rt._uid = info->si_uid;
404 /* XXX: potential problem if 64 bit */
405 tinfo->_sifields._rt._sigval.sival_ptr
Timothy E Baldwinda7c8642016-05-12 18:47:27 +0100406 = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
Peter Maydella70dadc2016-05-27 15:51:59 +0100407 si_type = QEMU_SI_RT;
408 break;
409 }
410 break;
bellard9de5e442003-03-23 16:49:39 +0000411 }
Peter Maydella70dadc2016-05-27 15:51:59 +0100412
413 tinfo->si_code = deposit32(si_code, 16, 16, si_type);
bellard66fb9762003-03-23 01:06:05 +0000414}
415
Gustavo Romero4d6d8a02024-03-09 03:08:58 +0000416static void tswap_siginfo(target_siginfo_t *tinfo,
417 const target_siginfo_t *info)
bellard9de5e442003-03-23 16:49:39 +0000418{
Peter Maydella70dadc2016-05-27 15:51:59 +0100419 int si_type = extract32(info->si_code, 16, 16);
420 int si_code = sextract32(info->si_code, 0, 16);
Richard Hendersona05c6402012-09-15 11:34:20 -0700421
Peter Maydella70dadc2016-05-27 15:51:59 +0100422 __put_user(info->si_signo, &tinfo->si_signo);
423 __put_user(info->si_errno, &tinfo->si_errno);
424 __put_user(si_code, &tinfo->si_code);
425
426 /* We can use our internal marker of which fields in the structure
427 * are valid, rather than duplicating the guesswork of
428 * host_to_target_siginfo_noswap() here.
429 */
430 switch (si_type) {
431 case QEMU_SI_KILL:
432 __put_user(info->_sifields._kill._pid, &tinfo->_sifields._kill._pid);
433 __put_user(info->_sifields._kill._uid, &tinfo->_sifields._kill._uid);
434 break;
435 case QEMU_SI_TIMER:
436 __put_user(info->_sifields._timer._timer1,
437 &tinfo->_sifields._timer._timer1);
438 __put_user(info->_sifields._timer._timer2,
439 &tinfo->_sifields._timer._timer2);
440 break;
441 case QEMU_SI_POLL:
442 __put_user(info->_sifields._sigpoll._band,
443 &tinfo->_sifields._sigpoll._band);
444 __put_user(info->_sifields._sigpoll._fd,
445 &tinfo->_sifields._sigpoll._fd);
446 break;
447 case QEMU_SI_FAULT:
448 __put_user(info->_sifields._sigfault._addr,
449 &tinfo->_sifields._sigfault._addr);
450 break;
451 case QEMU_SI_CHLD:
452 __put_user(info->_sifields._sigchld._pid,
453 &tinfo->_sifields._sigchld._pid);
454 __put_user(info->_sifields._sigchld._uid,
455 &tinfo->_sifields._sigchld._uid);
456 __put_user(info->_sifields._sigchld._status,
457 &tinfo->_sifields._sigchld._status);
458 __put_user(info->_sifields._sigchld._utime,
459 &tinfo->_sifields._sigchld._utime);
460 __put_user(info->_sifields._sigchld._stime,
461 &tinfo->_sifields._sigchld._stime);
462 break;
463 case QEMU_SI_RT:
464 __put_user(info->_sifields._rt._pid, &tinfo->_sifields._rt._pid);
465 __put_user(info->_sifields._rt._uid, &tinfo->_sifields._rt._uid);
466 __put_user(info->_sifields._rt._sigval.sival_ptr,
467 &tinfo->_sifields._rt._sigval.sival_ptr);
468 break;
469 default:
470 g_assert_not_reached();
bellard9de5e442003-03-23 16:49:39 +0000471 }
472}
473
Anthony Liguoric227f092009-10-01 16:12:16 -0500474void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
bellard9de5e442003-03-23 16:49:39 +0000475{
Peter Maydell55d72a72016-06-13 11:22:05 +0100476 target_siginfo_t tgt_tmp;
477 host_to_target_siginfo_noswap(&tgt_tmp, info);
478 tswap_siginfo(tinfo, &tgt_tmp);
bellard9de5e442003-03-23 16:49:39 +0000479}
480
481/* XXX: we support only POSIX RT signals are used. */
thsaa1f17c2007-07-11 22:48:58 +0000482/* XXX: find a solution for 64 bit (additional malloced data is needed) */
Anthony Liguoric227f092009-10-01 16:12:16 -0500483void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000484{
Peter Maydell90c0f082016-05-27 15:52:01 +0100485 /* This conversion is used only for the rt_sigqueueinfo syscall,
486 * and so we know that the _rt fields are the valid ones.
487 */
488 abi_ulong sival_ptr;
489
490 __get_user(info->si_signo, &tinfo->si_signo);
491 __get_user(info->si_errno, &tinfo->si_errno);
492 __get_user(info->si_code, &tinfo->si_code);
493 __get_user(info->si_pid, &tinfo->_sifields._rt._pid);
494 __get_user(info->si_uid, &tinfo->_sifields._rt._uid);
495 __get_user(sival_ptr, &tinfo->_sifields._rt._sigval.sival_ptr);
496 info->si_value.sival_ptr = (void *)(long)sival_ptr;
bellard66fb9762003-03-23 01:06:05 +0000497}
498
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300499/* returns 1 if given signal should dump core if not handled */
500static int core_dump_signal(int sig)
501{
502 switch (sig) {
503 case TARGET_SIGABRT:
504 case TARGET_SIGFPE:
505 case TARGET_SIGILL:
506 case TARGET_SIGQUIT:
507 case TARGET_SIGSEGV:
508 case TARGET_SIGTRAP:
509 case TARGET_SIGBUS:
510 return (1);
511 default:
512 return (0);
513 }
514}
515
Laurent Vivier365510f2020-02-12 13:56:56 +0100516static void signal_table_init(void)
517{
Richard Hendersonb60b91a2023-08-22 20:45:38 -0700518 int hsig, tsig, count;
Laurent Vivier365510f2020-02-12 13:56:56 +0100519
520 /*
Laurent Vivier6bc024e2020-02-12 13:56:58 +0100521 * Signals are supported starting from TARGET_SIGRTMIN and going up
Richard Hendersonb60b91a2023-08-22 20:45:38 -0700522 * until we run out of host realtime signals. Glibc uses the lower 2
523 * RT signals and (hopefully) nobody uses the upper ones.
524 * This is why SIGRTMIN (34) is generally greater than __SIGRTMIN (32).
525 * To fix this properly we would need to do manual signal delivery
526 * multiplexed over a single host signal.
Laurent Vivier6bc024e2020-02-12 13:56:58 +0100527 * Attempts for configure "missing" signals via sigaction will be
528 * silently ignored.
Richard Henderson38ee0a72023-09-30 12:05:11 -0700529 *
530 * Remap the target SIGABRT, so that we can distinguish host abort
531 * from guest abort. When the guest registers a signal handler or
532 * calls raise(SIGABRT), the host will raise SIG_RTn. If the guest
533 * arrives at dump_core_and_abort(), we will map back to host SIGABRT
534 * so that the parent (native or emulated) sees the correct signal.
535 * Finally, also map host to guest SIGABRT so that the emulated
536 * parent sees the correct mapping from wait status.
Laurent Vivier365510f2020-02-12 13:56:56 +0100537 */
Richard Henderson38ee0a72023-09-30 12:05:11 -0700538
539 hsig = SIGRTMIN;
540 host_to_target_signal_table[SIGABRT] = 0;
541 host_to_target_signal_table[hsig++] = TARGET_SIGABRT;
542
Richard Henderson02d9f5b2023-10-27 22:03:08 +0000543 for (tsig = TARGET_SIGRTMIN;
544 hsig <= SIGRTMAX && tsig <= TARGET_NSIG;
545 hsig++, tsig++) {
546 host_to_target_signal_table[hsig] = tsig;
Laurent Vivier6bc024e2020-02-12 13:56:58 +0100547 }
Laurent Vivier365510f2020-02-12 13:56:56 +0100548
Richard Hendersonb60b91a2023-08-22 20:45:38 -0700549 /* Invert the mapping that has already been assigned. */
550 for (hsig = 1; hsig < _NSIG; hsig++) {
551 tsig = host_to_target_signal_table[hsig];
552 if (tsig) {
553 assert(target_to_host_signal_table[tsig] == 0);
554 target_to_host_signal_table[tsig] = hsig;
Laurent Vivier9fcff3a2020-02-12 13:56:57 +0100555 }
Laurent Vivier365510f2020-02-12 13:56:56 +0100556 }
Laurent Vivier6bc024e2020-02-12 13:56:58 +0100557
Richard Henderson38ee0a72023-09-30 12:05:11 -0700558 host_to_target_signal_table[SIGABRT] = TARGET_SIGABRT;
559
Richard Hendersonb60b91a2023-08-22 20:45:38 -0700560 /* Map everything else out-of-bounds. */
561 for (hsig = 1; hsig < _NSIG; hsig++) {
562 if (host_to_target_signal_table[hsig] == 0) {
563 host_to_target_signal_table[hsig] = TARGET_NSIG + 1;
Laurent Vivier6bc024e2020-02-12 13:56:58 +0100564 }
Laurent Vivier6bc024e2020-02-12 13:56:58 +0100565 }
Richard Hendersonb60b91a2023-08-22 20:45:38 -0700566 for (count = 0, tsig = 1; tsig <= TARGET_NSIG; tsig++) {
567 if (target_to_host_signal_table[tsig] == 0) {
568 target_to_host_signal_table[tsig] = _NSIG;
569 count++;
570 }
571 }
572
573 trace_signal_table_init(count);
Laurent Vivier365510f2020-02-12 13:56:56 +0100574}
575
bellard31e31b82003-02-18 22:55:36 +0000576void signal_init(void)
577{
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000578 TaskState *ts = get_task_state(thread_cpu);
Richard Henderson58c4e362023-08-22 21:20:47 -0700579 struct sigaction act, oact;
bellard31e31b82003-02-18 22:55:36 +0000580
Laurent Vivier365510f2020-02-12 13:56:56 +0100581 /* initialize signal conversion tables */
582 signal_table_init();
ths3b46e622007-09-17 08:09:54 +0000583
Peter Maydell3d3efba2016-05-27 15:51:49 +0100584 /* Set the signal mask from the host mask. */
585 sigprocmask(0, 0, &ts->signal_mask);
586
bellard9de5e442003-03-23 16:49:39 +0000587 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000588 act.sa_flags = SA_SIGINFO;
589 act.sa_sigaction = host_signal_handler;
Richard Henderson58c4e362023-08-22 21:20:47 -0700590
591 /*
592 * A parent process may configure ignored signals, but all other
593 * signals are default. For any target signals that have no host
594 * mapping, set to ignore. For all core_dump_signal, install our
595 * host signal handler so that we may invoke dump_core_and_abort.
596 * This includes SIGSEGV and SIGBUS, which are also need our signal
597 * handler for paging and exceptions.
598 */
599 for (int tsig = 1; tsig <= TARGET_NSIG; tsig++) {
600 int hsig = target_to_host_signal(tsig);
601 abi_ptr thand = TARGET_SIG_IGN;
602
Richard Henderson38ee0a72023-09-30 12:05:11 -0700603 if (hsig >= _NSIG) {
604 continue;
605 }
Richard Henderson58c4e362023-08-22 21:20:47 -0700606
Richard Henderson38ee0a72023-09-30 12:05:11 -0700607 /* As we force remap SIGABRT, cannot probe and install in one step. */
608 if (tsig == TARGET_SIGABRT) {
609 sigaction(SIGABRT, NULL, &oact);
610 sigaction(hsig, &act, NULL);
611 } else {
612 struct sigaction *iact = core_dump_signal(tsig) ? &act : NULL;
Richard Henderson58c4e362023-08-22 21:20:47 -0700613 sigaction(hsig, iact, &oact);
Richard Henderson38ee0a72023-09-30 12:05:11 -0700614 }
615
616 if (oact.sa_sigaction != (void *)SIG_IGN) {
617 thand = TARGET_SIG_DFL;
pbrook624f7972008-05-31 16:11:38 +0000618 }
Richard Henderson58c4e362023-08-22 21:20:47 -0700619 sigact_table[tsig - 1]._sa_handler = thand;
bellard31e31b82003-02-18 22:55:36 +0000620 }
bellard31e31b82003-02-18 22:55:36 +0000621}
622
Peter Maydellc599d4d2016-07-28 16:44:49 +0100623/* Force a synchronously taken signal. The kernel force_sig() function
624 * also forces the signal to "not blocked, not ignored", but for QEMU
625 * that work is done in process_pending_signals().
626 */
Laurent Vivierbefb7442018-04-24 21:26:16 +0200627void force_sig(int sig)
Peter Maydellc599d4d2016-07-28 16:44:49 +0100628{
629 CPUState *cpu = thread_cpu;
Peter Maydell819121b2021-08-13 14:18:06 +0100630 target_siginfo_t info = {};
Peter Maydellc599d4d2016-07-28 16:44:49 +0100631
632 info.si_signo = sig;
633 info.si_errno = 0;
634 info.si_code = TARGET_SI_KERNEL;
635 info._sifields._kill._pid = 0;
636 info._sifields._kill._uid = 0;
Philippe Mathieu-Daudé42e62aa2024-01-29 17:45:11 +0100637 queue_signal(cpu_env(cpu), info.si_signo, QEMU_SI_KILL, &info);
Peter Maydellc599d4d2016-07-28 16:44:49 +0100638}
Peter Maydell09391662016-07-28 16:44:47 +0100639
Peter Maydellaf796962021-08-13 14:18:07 +0100640/*
641 * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the
642 * 'force' part is handled in process_pending_signals().
643 */
644void force_sig_fault(int sig, int code, abi_ulong addr)
645{
646 CPUState *cpu = thread_cpu;
Peter Maydellaf796962021-08-13 14:18:07 +0100647 target_siginfo_t info = {};
648
649 info.si_signo = sig;
650 info.si_errno = 0;
651 info.si_code = code;
652 info._sifields._sigfault._addr = addr;
Philippe Mathieu-Daudé42e62aa2024-01-29 17:45:11 +0100653 queue_signal(cpu_env(cpu), sig, QEMU_SI_FAULT, &info);
Peter Maydellaf796962021-08-13 14:18:07 +0100654}
655
Peter Maydell09391662016-07-28 16:44:47 +0100656/* Force a SIGSEGV if we couldn't write to memory trying to set
657 * up the signal frame. oldsig is the signal we were trying to handle
658 * at the point of failure.
659 */
Michael Clark47ae93c2018-03-03 01:31:11 +1300660#if !defined(TARGET_RISCV)
Laurent Vivierbefb7442018-04-24 21:26:16 +0200661void force_sigsegv(int oldsig)
Peter Maydell09391662016-07-28 16:44:47 +0100662{
Peter Maydell09391662016-07-28 16:44:47 +0100663 if (oldsig == SIGSEGV) {
664 /* Make sure we don't try to deliver the signal again; this will
Peter Maydellc599d4d2016-07-28 16:44:49 +0100665 * end up with handle_pending_signal() calling dump_core_and_abort().
Peter Maydell09391662016-07-28 16:44:47 +0100666 */
667 sigact_table[oldsig - 1]._sa_handler = TARGET_SIG_DFL;
668 }
Peter Maydellc4b35742016-07-28 16:44:50 +0100669 force_sig(TARGET_SIGSEGV);
Peter Maydell09391662016-07-28 16:44:47 +0100670}
Michael Clark47ae93c2018-03-03 01:31:11 +1300671#endif
672
Richard Henderson72d2bbf2021-09-17 17:32:56 -0700673void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr,
674 MMUAccessType access_type, bool maperr, uintptr_t ra)
675{
Richard Henderson1764ad72024-01-28 12:46:44 +1000676 const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
Richard Henderson72d2bbf2021-09-17 17:32:56 -0700677
678 if (tcg_ops->record_sigsegv) {
679 tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra);
Richard Henderson72d2bbf2021-09-17 17:32:56 -0700680 }
681
682 force_sig_fault(TARGET_SIGSEGV,
683 maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR,
684 addr);
685 cpu->exception_index = EXCP_INTERRUPT;
686 cpu_loop_exit_restore(cpu, ra);
687}
688
Richard Henderson12ed5642021-10-04 10:06:10 -0700689void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr,
690 MMUAccessType access_type, uintptr_t ra)
691{
Richard Henderson1764ad72024-01-28 12:46:44 +1000692 const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
Richard Henderson12ed5642021-10-04 10:06:10 -0700693
694 if (tcg_ops->record_sigbus) {
695 tcg_ops->record_sigbus(cpu, addr, access_type, ra);
696 }
697
698 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr);
699 cpu->exception_index = EXCP_INTERRUPT;
700 cpu_loop_exit_restore(cpu, ra);
701}
702
bellard9de5e442003-03-23 16:49:39 +0000703/* abort execution with signal */
Marc-André Lureau89057702022-04-20 17:26:02 +0400704static G_NORETURN
Richard Hendersonb8b50f12023-08-22 12:53:19 -0700705void die_with_signal(int host_sig)
706{
707 struct sigaction act = {
708 .sa_handler = SIG_DFL,
709 };
710
711 /*
712 * The proper exit code for dying from an uncaught signal is -<signal>.
713 * The kernel doesn't allow exit() or _exit() to pass a negative value.
714 * To get the proper exit code we need to actually die from an uncaught
715 * signal. Here the default signal handler is installed, we send
716 * the signal and we wait for it to arrive.
717 */
718 sigfillset(&act.sa_mask);
719 sigaction(host_sig, &act, NULL);
720
721 kill(getpid(), host_sig);
722
723 /* Make sure the signal isn't masked (reusing the mask inside of act). */
724 sigdelset(&act.sa_mask, host_sig);
725 sigsuspend(&act.sa_mask);
726
727 /* unreachable */
Richard Hendersonee72c472023-08-22 13:08:11 -0700728 _exit(EXIT_FAILURE);
Richard Hendersonb8b50f12023-08-22 12:53:19 -0700729}
730
731static G_NORETURN
Richard Hendersonb77af262023-09-13 17:22:49 -0700732void dump_core_and_abort(CPUArchState *env, int target_sig)
bellard66fb9762003-03-23 01:06:05 +0000733{
Richard Hendersonb77af262023-09-13 17:22:49 -0700734 CPUState *cpu = env_cpu(env);
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000735 TaskState *ts = get_task_state(cpu);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300736 int host_sig, core_dumped = 0;
Paolo Bonzinic8ee0a42015-11-13 13:52:21 +0100737
Richard Henderson38ee0a72023-09-30 12:05:11 -0700738 /* On exit, undo the remapping of SIGABRT. */
739 if (target_sig == TARGET_SIGABRT) {
740 host_sig = SIGABRT;
741 } else {
742 host_sig = target_to_host_signal(target_sig);
743 }
Peter Maydellb5f95362022-01-14 15:37:31 +0000744 trace_user_dump_core_and_abort(env, target_sig, host_sig);
Andreas Färbera2247f82013-06-09 19:47:04 +0200745 gdb_signalled(env, target_sig);
aurel32603e4fd2009-04-15 16:18:38 +0000746
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300747 /* dump core if supported by target binary format */
Riku Voipio66393fb2009-12-04 15:16:32 +0200748 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300749 stop_all_tasks();
750 core_dumped =
Andreas Färbera2247f82013-06-09 19:47:04 +0200751 ((*ts->bprm->core_dump)(target_sig, env) == 0);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300752 }
753 if (core_dumped) {
754 /* we already dumped the core of target process, we don't want
755 * a coredump of qemu itself */
756 struct rlimit nodump;
757 getrlimit(RLIMIT_CORE, &nodump);
758 nodump.rlim_cur=0;
759 setrlimit(RLIMIT_CORE, &nodump);
760 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
Riku Voipio66393fb2009-12-04 15:16:32 +0200761 target_sig, strsignal(host_sig), "core dumped" );
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300762 }
763
Richard Hendersonb77af262023-09-13 17:22:49 -0700764 preexit_cleanup(env, 128 + target_sig);
Richard Hendersonb8b50f12023-08-22 12:53:19 -0700765 die_with_signal(host_sig);
bellard66fb9762003-03-23 01:06:05 +0000766}
767
bellard9de5e442003-03-23 16:49:39 +0000768/* queue a signal so that it will be send to the virtual CPU as soon
769 as possible */
Peter Maydell337e88d2022-01-14 15:37:32 +0000770void queue_signal(CPUArchState *env, int sig, int si_type,
771 target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000772{
Richard Henderson29a0af62019-03-22 16:07:18 -0700773 CPUState *cpu = env_cpu(env);
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000774 TaskState *ts = get_task_state(cpu);
bellard66fb9762003-03-23 01:06:05 +0000775
Paolo Bonzinic8ee0a42015-11-13 13:52:21 +0100776 trace_user_queue_signal(env, sig);
Peter Maydella7ec0f92014-03-14 14:36:56 +0000777
Peter Maydell9d2803f2016-07-28 16:44:46 +0100778 info->si_code = deposit32(info->si_code, 16, 16, si_type);
Peter Maydella70dadc2016-05-27 15:51:59 +0100779
Timothy E Baldwin655ed672016-05-27 15:51:53 +0100780 ts->sync_signal.info = *info;
781 ts->sync_signal.pending = sig;
Timothy E Baldwin907f5fd2016-05-27 15:51:52 +0100782 /* signal that a new signal is pending */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100783 qatomic_set(&ts->signal_pending, 1);
bellard9de5e442003-03-23 16:49:39 +0000784}
785
Warner Losh07637882021-11-12 21:56:00 -0700786
787/* Adjust the signal context to rewind out of safe-syscall if we're in it */
Timothy E Baldwin4d330ce2016-05-12 18:47:46 +0100788static inline void rewind_if_in_safe_syscall(void *puc)
789{
Richard Henderson99407992022-02-08 09:40:00 +0300790 host_sigcontext *uc = (host_sigcontext *)puc;
Warner Losh07637882021-11-12 21:56:00 -0700791 uintptr_t pcreg = host_signal_pc(uc);
792
793 if (pcreg > (uintptr_t)safe_syscall_start
794 && pcreg < (uintptr_t)safe_syscall_end) {
795 host_signal_set_pc(uc, (uintptr_t)safe_syscall_start);
796 }
Warner Losh07637882021-11-12 21:56:00 -0700797}
Timothy E Baldwin4d330ce2016-05-12 18:47:46 +0100798
Helge Deller7dfd3ca2023-08-12 18:43:14 +0200799static G_NORETURN
800void die_from_signal(siginfo_t *info)
801{
802 char sigbuf[4], codebuf[12];
803 const char *sig, *code = NULL;
804
805 switch (info->si_signo) {
806 case SIGSEGV:
807 sig = "SEGV";
808 switch (info->si_code) {
809 case SEGV_MAPERR:
810 code = "MAPERR";
811 break;
812 case SEGV_ACCERR:
813 code = "ACCERR";
814 break;
815 }
816 break;
817 case SIGBUS:
818 sig = "BUS";
819 switch (info->si_code) {
820 case BUS_ADRALN:
821 code = "ADRALN";
822 break;
823 case BUS_ADRERR:
824 code = "ADRERR";
825 break;
826 }
827 break;
Richard Henderson4a6ebc12023-08-22 22:07:41 -0700828 case SIGILL:
829 sig = "ILL";
830 switch (info->si_code) {
831 case ILL_ILLOPC:
832 code = "ILLOPC";
833 break;
834 case ILL_ILLOPN:
835 code = "ILLOPN";
836 break;
837 case ILL_ILLADR:
838 code = "ILLADR";
839 break;
840 case ILL_PRVOPC:
841 code = "PRVOPC";
842 break;
843 case ILL_PRVREG:
844 code = "PRVREG";
845 break;
846 case ILL_COPROC:
847 code = "COPROC";
848 break;
849 }
850 break;
851 case SIGFPE:
852 sig = "FPE";
853 switch (info->si_code) {
854 case FPE_INTDIV:
855 code = "INTDIV";
856 break;
857 case FPE_INTOVF:
858 code = "INTOVF";
859 break;
860 }
861 break;
862 case SIGTRAP:
863 sig = "TRAP";
864 break;
Helge Deller7dfd3ca2023-08-12 18:43:14 +0200865 default:
866 snprintf(sigbuf, sizeof(sigbuf), "%d", info->si_signo);
867 sig = sigbuf;
868 break;
869 }
870 if (code == NULL) {
871 snprintf(codebuf, sizeof(sigbuf), "%d", info->si_code);
872 code = codebuf;
873 }
874
875 error_report("QEMU internal SIG%s {code=%s, addr=%p}",
876 sig, code, info->si_addr);
877 die_with_signal(info->si_signo);
878}
879
Richard Hendersonf4e11682023-08-22 21:56:10 -0700880static void host_sigsegv_handler(CPUState *cpu, siginfo_t *info,
881 host_sigcontext *uc)
882{
883 uintptr_t host_addr = (uintptr_t)info->si_addr;
884 /*
885 * Convert forcefully to guest address space: addresses outside
886 * reserved_va are still valid to report via SEGV_MAPERR.
887 */
888 bool is_valid = h2g_valid(host_addr);
889 abi_ptr guest_addr = h2g_nocheck(host_addr);
890 uintptr_t pc = host_signal_pc(uc);
891 bool is_write = host_signal_write(info, uc);
892 MMUAccessType access_type = adjust_signal_pc(&pc, is_write);
893 bool maperr;
894
895 /* If this was a write to a TB protected page, restart. */
896 if (is_write
897 && is_valid
898 && info->si_code == SEGV_ACCERR
899 && handle_sigsegv_accerr_write(cpu, host_signal_mask(uc),
900 pc, guest_addr)) {
901 return;
902 }
903
904 /*
905 * If the access was not on behalf of the guest, within the executable
906 * mapping of the generated code buffer, then it is a host bug.
907 */
908 if (access_type != MMU_INST_FETCH
909 && !in_code_gen_buffer((void *)(pc - tcg_splitwx_diff))) {
910 die_from_signal(info);
911 }
912
913 maperr = true;
914 if (is_valid && info->si_code == SEGV_ACCERR) {
915 /*
916 * With reserved_va, the whole address space is PROT_NONE,
917 * which means that we may get ACCERR when we want MAPERR.
918 */
919 if (page_get_flags(guest_addr) & PAGE_VALID) {
920 maperr = false;
921 } else {
922 info->si_code = SEGV_MAPERR;
923 }
924 }
925
926 sigprocmask(SIG_SETMASK, host_signal_mask(uc), NULL);
927 cpu_loop_exit_sigsegv(cpu, guest_addr, access_type, maperr, pc);
928}
929
Robbin Ehn6d913152024-01-12 21:57:22 +0100930static uintptr_t host_sigbus_handler(CPUState *cpu, siginfo_t *info,
Richard Hendersonf4e11682023-08-22 21:56:10 -0700931 host_sigcontext *uc)
932{
933 uintptr_t pc = host_signal_pc(uc);
934 bool is_write = host_signal_write(info, uc);
935 MMUAccessType access_type = adjust_signal_pc(&pc, is_write);
936
937 /*
938 * If the access was not on behalf of the guest, within the executable
939 * mapping of the generated code buffer, then it is a host bug.
940 */
941 if (!in_code_gen_buffer((void *)(pc - tcg_splitwx_diff))) {
942 die_from_signal(info);
943 }
944
945 if (info->si_code == BUS_ADRALN) {
946 uintptr_t host_addr = (uintptr_t)info->si_addr;
947 abi_ptr guest_addr = h2g_nocheck(host_addr);
948
949 sigprocmask(SIG_SETMASK, host_signal_mask(uc), NULL);
950 cpu_loop_exit_sigbus(cpu, guest_addr, access_type, pc);
951 }
Robbin Ehn6d913152024-01-12 21:57:22 +0100952 return pc;
Richard Hendersonf4e11682023-08-22 21:56:10 -0700953}
954
Richard Hendersone6037d02021-09-16 14:44:17 -0700955static void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
bellard9de5e442003-03-23 16:49:39 +0000956{
Richard Hendersonb77af262023-09-13 17:22:49 -0700957 CPUState *cpu = thread_cpu;
958 CPUArchState *env = cpu_env(cpu);
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +0000959 TaskState *ts = get_task_state(cpu);
Anthony Liguoric227f092009-10-01 16:12:16 -0500960 target_siginfo_t tinfo;
Richard Henderson99407992022-02-08 09:40:00 +0300961 host_sigcontext *uc = puc;
Timothy E Baldwin655ed672016-05-27 15:51:53 +0100962 struct emulated_sigtable *k;
Richard Hendersone6037d02021-09-16 14:44:17 -0700963 int guest_sig;
Richard Hendersone6037d02021-09-16 14:44:17 -0700964 uintptr_t pc = 0;
965 bool sync_sig = false;
Richard Hendersonf4e11682023-08-22 21:56:10 -0700966 void *sigmask;
Richard Hendersone6037d02021-09-16 14:44:17 -0700967
968 /*
969 * Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special
Richard Henderson4a6ebc12023-08-22 22:07:41 -0700970 * handling wrt signal blocking and unwinding. Non-spoofed SIGILL,
971 * SIGFPE, SIGTRAP are always host bugs.
Richard Hendersone6037d02021-09-16 14:44:17 -0700972 */
Richard Hendersonf4e11682023-08-22 21:56:10 -0700973 if (info->si_code > 0) {
974 switch (host_sig) {
975 case SIGSEGV:
976 /* Only returns on handle_sigsegv_accerr_write success. */
977 host_sigsegv_handler(cpu, info, uc);
Helge Deller7dfd3ca2023-08-12 18:43:14 +0200978 return;
Richard Hendersonf4e11682023-08-22 21:56:10 -0700979 case SIGBUS:
Robbin Ehn6d913152024-01-12 21:57:22 +0100980 pc = host_sigbus_handler(cpu, info, uc);
Richard Hendersonf4e11682023-08-22 21:56:10 -0700981 sync_sig = true;
982 break;
Richard Henderson4a6ebc12023-08-22 22:07:41 -0700983 case SIGILL:
984 case SIGFPE:
985 case SIGTRAP:
986 die_from_signal(info);
Helge Deller7dfd3ca2023-08-12 18:43:14 +0200987 }
Richard Hendersone6037d02021-09-16 14:44:17 -0700988 }
bellard9de5e442003-03-23 16:49:39 +0000989
990 /* get target signal number */
Richard Hendersone6037d02021-09-16 14:44:17 -0700991 guest_sig = host_to_target_signal(host_sig);
992 if (guest_sig < 1 || guest_sig > TARGET_NSIG) {
bellard9de5e442003-03-23 16:49:39 +0000993 return;
Richard Hendersone6037d02021-09-16 14:44:17 -0700994 }
995 trace_user_host_signal(env, host_sig, guest_sig);
996
997 host_to_target_siginfo_noswap(&tinfo, info);
998 k = &ts->sigtab[guest_sig - 1];
999 k->info = tinfo;
1000 k->pending = guest_sig;
1001 ts->signal_pending = 1;
1002
Richard Hendersone6037d02021-09-16 14:44:17 -07001003 /*
1004 * For synchronous signals, unwind the cpu state to the faulting
1005 * insn and then exit back to the main loop so that the signal
1006 * is delivered immediately.
1007 */
1008 if (sync_sig) {
1009 cpu->exception_index = EXCP_INTERRUPT;
1010 cpu_loop_exit_restore(cpu, pc);
1011 }
Timothy E Baldwin4d330ce2016-05-12 18:47:46 +01001012
1013 rewind_if_in_safe_syscall(puc);
1014
Richard Hendersone6037d02021-09-16 14:44:17 -07001015 /*
1016 * Block host signals until target signal handler entered. We
Timothy E Baldwin655ed672016-05-27 15:51:53 +01001017 * can't block SIGSEGV or SIGBUS while we're executing guest
1018 * code in case the guest code provokes one in the window between
1019 * now and it getting out to the main loop. Signals will be
1020 * unblocked again in process_pending_signals().
Peter Maydell1d48fdd2016-06-14 12:49:18 +01001021 *
Richard Hendersonc8c89a62022-02-08 09:30:42 +03001022 * WARNING: we cannot use sigfillset() here because the sigmask
Peter Maydell1d48fdd2016-06-14 12:49:18 +01001023 * field is a kernel sigset_t, which is much smaller than the
1024 * libc sigset_t which sigfillset() operates on. Using sigfillset()
1025 * would write 0xff bytes off the end of the structure and trash
1026 * data on the struct.
Timothy E Baldwin655ed672016-05-27 15:51:53 +01001027 */
Richard Hendersonf4e11682023-08-22 21:56:10 -07001028 sigmask = host_signal_mask(uc);
Richard Hendersonc8c89a62022-02-08 09:30:42 +03001029 memset(sigmask, 0xff, SIGSET_T_SIZE);
1030 sigdelset(sigmask, SIGSEGV);
1031 sigdelset(sigmask, SIGBUS);
Timothy E Baldwin655ed672016-05-27 15:51:53 +01001032
1033 /* interrupt the virtual CPU as soon as possible */
1034 cpu_exit(thread_cpu);
bellard31e31b82003-02-18 22:55:36 +00001035}
1036
ths0da46a62007-10-20 20:23:07 +00001037/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +00001038/* compare linux/kernel/signal.c:do_sigaltstack() */
Richard Henderson6b208752021-04-25 19:53:12 -07001039abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr,
1040 CPUArchState *env)
thsa04e1342007-09-27 13:57:58 +00001041{
Richard Henderson92bad942021-04-25 19:53:10 -07001042 target_stack_t oss, *uoss = NULL;
1043 abi_long ret = -TARGET_EFAULT;
thsa04e1342007-09-27 13:57:58 +00001044
Richard Henderson92bad942021-04-25 19:53:10 -07001045 if (uoss_addr) {
Richard Henderson92bad942021-04-25 19:53:10 -07001046 /* Verify writability now, but do not alter user memory yet. */
1047 if (!lock_user_struct(VERIFY_WRITE, uoss, uoss_addr, 0)) {
1048 goto out;
1049 }
Richard Henderson6b208752021-04-25 19:53:12 -07001050 target_save_altstack(&oss, env);
thsa04e1342007-09-27 13:57:58 +00001051 }
1052
Richard Henderson92bad942021-04-25 19:53:10 -07001053 if (uss_addr) {
1054 target_stack_t *uss;
Tom Musta0903c8b2014-08-12 13:53:40 -05001055
Riku Voipio9eeb8302014-04-23 11:26:34 +03001056 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
thsa04e1342007-09-27 13:57:58 +00001057 goto out;
Riku Voipio9eeb8302014-04-23 11:26:34 +03001058 }
Richard Hendersonddc3e742021-04-25 19:53:13 -07001059 ret = target_restore_altstack(uss, env);
Richard Henderson92bad942021-04-25 19:53:10 -07001060 if (ret) {
thsa04e1342007-09-27 13:57:58 +00001061 goto out;
Paolo Bonzini7d374352018-12-13 23:37:37 +01001062 }
thsa04e1342007-09-27 13:57:58 +00001063 }
1064
bellard579a97f2007-11-11 14:26:47 +00001065 if (uoss_addr) {
Richard Henderson92bad942021-04-25 19:53:10 -07001066 memcpy(uoss, &oss, sizeof(oss));
1067 unlock_user_struct(uoss, uoss_addr, 1);
1068 uoss = NULL;
thsa04e1342007-09-27 13:57:58 +00001069 }
thsa04e1342007-09-27 13:57:58 +00001070 ret = 0;
Richard Henderson92bad942021-04-25 19:53:10 -07001071
1072 out:
1073 if (uoss) {
1074 unlock_user_struct(uoss, uoss_addr, 0);
1075 }
thsa04e1342007-09-27 13:57:58 +00001076 return ret;
1077}
1078
Timothy E Baldwinef6a7782016-05-27 15:51:54 +01001079/* do_sigaction() return target values and host errnos */
bellard66fb9762003-03-23 01:06:05 +00001080int do_sigaction(int sig, const struct target_sigaction *act,
Richard Henderson02fb28e2021-04-22 16:02:23 -07001081 struct target_sigaction *oact, abi_ulong ka_restorer)
bellard31e31b82003-02-18 22:55:36 +00001082{
pbrook624f7972008-05-31 16:11:38 +00001083 struct target_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +00001084 int host_sig;
ths0da46a62007-10-20 20:23:07 +00001085 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +00001086
Laurent Vivier6bc024e2020-02-12 13:56:58 +01001087 trace_signal_do_sigaction_guest(sig, TARGET_NSIG);
1088
Ilya Leoshkevichee3500d2021-06-01 16:55:59 +02001089 if (sig < 1 || sig > TARGET_NSIG) {
1090 return -TARGET_EINVAL;
1091 }
1092
1093 if (act && (sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)) {
Timothy E Baldwinef6a7782016-05-27 15:51:54 +01001094 return -TARGET_EINVAL;
1095 }
1096
1097 if (block_signals()) {
Richard Hendersonaf254a22021-11-22 19:47:33 +01001098 return -QEMU_ERESTARTSYS;
Timothy E Baldwinef6a7782016-05-27 15:51:54 +01001099 }
1100
bellard66fb9762003-03-23 01:06:05 +00001101 k = &sigact_table[sig - 1];
bellard66fb9762003-03-23 01:06:05 +00001102 if (oact) {
Richard Hendersond2565872013-01-04 16:39:32 -08001103 __put_user(k->_sa_handler, &oact->_sa_handler);
1104 __put_user(k->sa_flags, &oact->sa_flags);
Richard Henderson7f047de2017-10-31 13:53:52 +01001105#ifdef TARGET_ARCH_HAS_SA_RESTORER
Richard Hendersond2565872013-01-04 16:39:32 -08001106 __put_user(k->sa_restorer, &oact->sa_restorer);
ths388bb212007-05-13 13:58:00 +00001107#endif
Richard Hendersond2565872013-01-04 16:39:32 -08001108 /* Not swapped. */
pbrook624f7972008-05-31 16:11:38 +00001109 oact->sa_mask = k->sa_mask;
bellard66fb9762003-03-23 01:06:05 +00001110 }
1111 if (act) {
Richard Hendersond2565872013-01-04 16:39:32 -08001112 __get_user(k->_sa_handler, &act->_sa_handler);
1113 __get_user(k->sa_flags, &act->sa_flags);
Richard Henderson7f047de2017-10-31 13:53:52 +01001114#ifdef TARGET_ARCH_HAS_SA_RESTORER
Richard Hendersond2565872013-01-04 16:39:32 -08001115 __get_user(k->sa_restorer, &act->sa_restorer);
ths388bb212007-05-13 13:58:00 +00001116#endif
Richard Henderson02fb28e2021-04-22 16:02:23 -07001117#ifdef TARGET_ARCH_HAS_KA_RESTORER
1118 k->ka_restorer = ka_restorer;
1119#endif
Richard Hendersond2565872013-01-04 16:39:32 -08001120 /* To be swapped in target_to_host_sigset. */
pbrook624f7972008-05-31 16:11:38 +00001121 k->sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +00001122
1123 /* we update the host linux signal state */
1124 host_sig = target_to_host_signal(sig);
Laurent Vivier6bc024e2020-02-12 13:56:58 +01001125 trace_signal_do_sigaction_host(host_sig, TARGET_NSIG);
1126 if (host_sig > SIGRTMAX) {
1127 /* we don't have enough host signals to map all target signals */
1128 qemu_log_mask(LOG_UNIMP, "Unsupported target signal #%d, ignored\n",
1129 sig);
1130 /*
1131 * we don't return an error here because some programs try to
1132 * register an handler for all possible rt signals even if they
1133 * don't need it.
1134 * An error here can abort them whereas there can be no problem
1135 * to not have the signal available later.
1136 * This is the case for golang,
1137 * See https://github.com/golang/go/issues/33746
1138 * So we silently ignore the error.
1139 */
1140 return 0;
1141 }
bellard773b93e2004-01-04 17:15:59 +00001142 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
Richard Hendersondbde2c02023-08-22 14:55:03 -07001143 struct sigaction act1;
1144
bellard773b93e2004-01-04 17:15:59 +00001145 sigfillset(&act1.sa_mask);
1146 act1.sa_flags = SA_SIGINFO;
pbrook624f7972008-05-31 16:11:38 +00001147 if (k->_sa_handler == TARGET_SIG_IGN) {
Richard Hendersondbde2c02023-08-22 14:55:03 -07001148 /*
1149 * It is important to update the host kernel signal ignore
1150 * state to avoid getting unexpected interrupted syscalls.
1151 */
bellard773b93e2004-01-04 17:15:59 +00001152 act1.sa_sigaction = (void *)SIG_IGN;
pbrook624f7972008-05-31 16:11:38 +00001153 } else if (k->_sa_handler == TARGET_SIG_DFL) {
Richard Hendersondbde2c02023-08-22 14:55:03 -07001154 if (core_dump_signal(sig)) {
aurel32ca587a82008-12-18 22:44:13 +00001155 act1.sa_sigaction = host_signal_handler;
Richard Hendersondbde2c02023-08-22 14:55:03 -07001156 } else {
aurel32ca587a82008-12-18 22:44:13 +00001157 act1.sa_sigaction = (void *)SIG_DFL;
Richard Hendersondbde2c02023-08-22 14:55:03 -07001158 }
bellard773b93e2004-01-04 17:15:59 +00001159 } else {
1160 act1.sa_sigaction = host_signal_handler;
Richard Hendersondbde2c02023-08-22 14:55:03 -07001161 if (k->sa_flags & TARGET_SA_RESTART) {
1162 act1.sa_flags |= SA_RESTART;
1163 }
bellard773b93e2004-01-04 17:15:59 +00001164 }
ths0da46a62007-10-20 20:23:07 +00001165 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +00001166 }
bellard66fb9762003-03-23 01:06:05 +00001167 }
ths0da46a62007-10-20 20:23:07 +00001168 return ret;
bellard66fb9762003-03-23 01:06:05 +00001169}
bellard31e31b82003-02-18 22:55:36 +00001170
Peter Maydell31efaef2016-07-06 15:09:29 +01001171static void handle_pending_signal(CPUArchState *cpu_env, int sig,
1172 struct emulated_sigtable *k)
Peter Maydelleb552502016-05-27 15:51:43 +01001173{
Richard Henderson29a0af62019-03-22 16:07:18 -07001174 CPUState *cpu = env_cpu(cpu_env);
Peter Maydelleb552502016-05-27 15:51:43 +01001175 abi_ulong handler;
Peter Maydell3d3efba2016-05-27 15:51:49 +01001176 sigset_t set;
Richard Henderson143bcc12024-04-08 14:33:35 -10001177 target_siginfo_t unswapped;
Peter Maydelleb552502016-05-27 15:51:43 +01001178 target_sigset_t target_old_set;
1179 struct target_sigaction *sa;
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +00001180 TaskState *ts = get_task_state(cpu);
Peter Maydelleb552502016-05-27 15:51:43 +01001181
Paolo Bonzinic8ee0a42015-11-13 13:52:21 +01001182 trace_user_handle_signal(cpu_env, sig);
bellard66fb9762003-03-23 01:06:05 +00001183 /* dequeue signal */
Timothy E Baldwin907f5fd2016-05-27 15:51:52 +01001184 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00001185
Gustavo Romero4d6d8a02024-03-09 03:08:58 +00001186 /*
Richard Henderson143bcc12024-04-08 14:33:35 -10001187 * Writes out siginfo values byteswapped, accordingly to the target.
1188 * It also cleans the si_type from si_code making it correct for
1189 * the target. We must hold on to the original unswapped copy for
1190 * strace below, because si_type is still required there.
Gustavo Romero4d6d8a02024-03-09 03:08:58 +00001191 */
Richard Henderson143bcc12024-04-08 14:33:35 -10001192 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
1193 unswapped = k->info;
1194 }
Gustavo Romero4d6d8a02024-03-09 03:08:58 +00001195 tswap_siginfo(&k->info, &k->info);
1196
Gustavo Romerof84e3132024-03-09 03:08:59 +00001197 sig = gdb_handlesig(cpu, sig, NULL, &k->info, sizeof(k->info));
bellard1fddef42005-04-17 19:16:13 +00001198 if (!sig) {
aurel32ca587a82008-12-18 22:44:13 +00001199 sa = NULL;
1200 handler = TARGET_SIG_IGN;
1201 } else {
1202 sa = &sigact_table[sig - 1];
1203 handler = sa->_sa_handler;
bellard1fddef42005-04-17 19:16:13 +00001204 }
bellard66fb9762003-03-23 01:06:05 +00001205
Josh Kunz4b25a502020-02-03 18:54:14 -08001206 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
Richard Henderson143bcc12024-04-08 14:33:35 -10001207 print_taken_signal(sig, &unswapped);
Peter Maydell0cb581d2016-07-18 18:12:24 +01001208 }
1209
bellard66fb9762003-03-23 01:06:05 +00001210 if (handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +00001211 /* default handler : ignore some signal. The other are job control or fatal */
1212 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
1213 kill(getpid(),SIGSTOP);
1214 } else if (sig != TARGET_SIGCHLD &&
1215 sig != TARGET_SIGURG &&
1216 sig != TARGET_SIGWINCH &&
1217 sig != TARGET_SIGCONT) {
Ilya Leoshkevichda91c192023-01-12 16:20:11 +01001218 dump_core_and_abort(cpu_env, sig);
bellard66fb9762003-03-23 01:06:05 +00001219 }
1220 } else if (handler == TARGET_SIG_IGN) {
1221 /* ignore sig */
1222 } else if (handler == TARGET_SIG_ERR) {
Ilya Leoshkevichda91c192023-01-12 16:20:11 +01001223 dump_core_and_abort(cpu_env, sig);
bellard66fb9762003-03-23 01:06:05 +00001224 } else {
bellard9de5e442003-03-23 16:49:39 +00001225 /* compute the blocked signals during the handler execution */
Peter Maydell3d3efba2016-05-27 15:51:49 +01001226 sigset_t *blocked_set;
1227
pbrook624f7972008-05-31 16:11:38 +00001228 target_to_host_sigset(&set, &sa->sa_mask);
bellard9de5e442003-03-23 16:49:39 +00001229 /* SA_NODEFER indicates that the current signal should not be
1230 blocked during the handler */
pbrook624f7972008-05-31 16:11:38 +00001231 if (!(sa->sa_flags & TARGET_SA_NODEFER))
bellard9de5e442003-03-23 16:49:39 +00001232 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00001233
bellard9de5e442003-03-23 16:49:39 +00001234 /* save the previous blocked signal state to restore it at the
1235 end of the signal execution (see do_sigreturn) */
Peter Maydell3d3efba2016-05-27 15:51:49 +01001236 host_to_target_sigset_internal(&target_old_set, &ts->signal_mask);
1237
1238 /* block signals in the handler */
1239 blocked_set = ts->in_sigsuspend ?
1240 &ts->sigsuspend_mask : &ts->signal_mask;
1241 sigorset(&ts->signal_mask, blocked_set, &set);
1242 ts->in_sigsuspend = 0;
bellard9de5e442003-03-23 16:49:39 +00001243
bellardbc8a22c2003-03-30 21:02:40 +00001244 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00001245#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00001246 {
1247 CPUX86State *env = cpu_env;
1248 if (env->eflags & VM_MASK)
1249 save_v86_state(env);
1250 }
1251#endif
bellard9de5e442003-03-23 16:49:39 +00001252 /* prepare the stack frame of the virtual CPU */
Laurent Viviercb6ac802018-04-24 21:26:35 +02001253#if defined(TARGET_ARCH_HAS_SETUP_FRAME)
1254 if (sa->sa_flags & TARGET_SA_SIGINFO) {
1255 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
1256 } else {
1257 setup_frame(sig, sa, &target_old_set, cpu_env);
1258 }
1259#else
Richard Hendersonff970902013-02-10 10:30:42 -08001260 /* These targets do not have traditional signals. */
Timothy E Baldwin907f5fd2016-05-27 15:51:52 +01001261 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
Richard Hendersonff970902013-02-10 10:30:42 -08001262#endif
Peter Maydell7ec87e02016-05-27 15:51:45 +01001263 if (sa->sa_flags & TARGET_SA_RESETHAND) {
pbrook624f7972008-05-31 16:11:38 +00001264 sa->_sa_handler = TARGET_SIG_DFL;
Peter Maydell7ec87e02016-05-27 15:51:45 +01001265 }
bellard31e31b82003-02-18 22:55:36 +00001266 }
bellard31e31b82003-02-18 22:55:36 +00001267}
Peter Maydelle902d582016-05-27 15:51:44 +01001268
1269void process_pending_signals(CPUArchState *cpu_env)
1270{
Richard Henderson29a0af62019-03-22 16:07:18 -07001271 CPUState *cpu = env_cpu(cpu_env);
Peter Maydelle902d582016-05-27 15:51:44 +01001272 int sig;
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +00001273 TaskState *ts = get_task_state(cpu);
Peter Maydell3d3efba2016-05-27 15:51:49 +01001274 sigset_t set;
1275 sigset_t *blocked_set;
Peter Maydelle902d582016-05-27 15:51:44 +01001276
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001277 while (qatomic_read(&ts->signal_pending)) {
Peter Maydell3d3efba2016-05-27 15:51:49 +01001278 sigfillset(&set);
1279 sigprocmask(SIG_SETMASK, &set, 0);
Peter Maydelle902d582016-05-27 15:51:44 +01001280
Peter Maydell8bd37732016-07-28 16:44:45 +01001281 restart_scan:
Timothy E Baldwin655ed672016-05-27 15:51:53 +01001282 sig = ts->sync_signal.pending;
1283 if (sig) {
1284 /* Synchronous signals are forced,
1285 * see force_sig_info() and callers in Linux
1286 * Note that not all of our queue_signal() calls in QEMU correspond
1287 * to force_sig_info() calls in Linux (some are send_sig_info()).
1288 * However it seems like a kernel bug to me to allow the process
1289 * to block a synchronous signal since it could then just end up
1290 * looping round and round indefinitely.
1291 */
1292 if (sigismember(&ts->signal_mask, target_to_host_signal_table[sig])
1293 || sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) {
1294 sigdelset(&ts->signal_mask, target_to_host_signal_table[sig]);
1295 sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL;
1296 }
1297
Peter Maydell31efaef2016-07-06 15:09:29 +01001298 handle_pending_signal(cpu_env, sig, &ts->sync_signal);
Timothy E Baldwin655ed672016-05-27 15:51:53 +01001299 }
1300
Peter Maydell3d3efba2016-05-27 15:51:49 +01001301 for (sig = 1; sig <= TARGET_NSIG; sig++) {
1302 blocked_set = ts->in_sigsuspend ?
1303 &ts->sigsuspend_mask : &ts->signal_mask;
1304
1305 if (ts->sigtab[sig - 1].pending &&
1306 (!sigismember(blocked_set,
Timothy E Baldwin655ed672016-05-27 15:51:53 +01001307 target_to_host_signal_table[sig]))) {
Peter Maydell31efaef2016-07-06 15:09:29 +01001308 handle_pending_signal(cpu_env, sig, &ts->sigtab[sig - 1]);
Peter Maydell8bd37732016-07-28 16:44:45 +01001309 /* Restart scan from the beginning, as handle_pending_signal
1310 * might have resulted in a new synchronous signal (eg SIGSEGV).
1311 */
1312 goto restart_scan;
Peter Maydell3d3efba2016-05-27 15:51:49 +01001313 }
Peter Maydelle902d582016-05-27 15:51:44 +01001314 }
Peter Maydell3d3efba2016-05-27 15:51:49 +01001315
1316 /* if no signal is pending, unblock signals and recheck (the act
1317 * of unblocking might cause us to take another host signal which
1318 * will set signal_pending again).
1319 */
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001320 qatomic_set(&ts->signal_pending, 0);
Peter Maydell3d3efba2016-05-27 15:51:49 +01001321 ts->in_sigsuspend = 0;
1322 set = ts->signal_mask;
1323 sigdelset(&set, SIGSEGV);
1324 sigdelset(&set, SIGBUS);
1325 sigprocmask(SIG_SETMASK, &set, 0);
Peter Maydelle902d582016-05-27 15:51:44 +01001326 }
Peter Maydell3d3efba2016-05-27 15:51:49 +01001327 ts->in_sigsuspend = 0;
Peter Maydelle902d582016-05-27 15:51:44 +01001328}
Richard Henderson0a99f092022-03-15 01:43:05 -07001329
1330int process_sigsuspend_mask(sigset_t **pset, target_ulong sigset,
1331 target_ulong sigsize)
1332{
Ilya Leoshkeviche4e5cb42024-03-05 12:09:39 +00001333 TaskState *ts = get_task_state(thread_cpu);
Richard Henderson0a99f092022-03-15 01:43:05 -07001334 sigset_t *host_set = &ts->sigsuspend_mask;
1335 target_sigset_t *target_sigset;
1336
1337 if (sigsize != sizeof(*target_sigset)) {
1338 /* Like the kernel, we enforce correct size sigsets */
1339 return -TARGET_EINVAL;
1340 }
1341
1342 target_sigset = lock_user(VERIFY_READ, sigset, sigsize, 1);
1343 if (!target_sigset) {
1344 return -TARGET_EFAULT;
1345 }
1346 target_to_host_sigset(host_set, target_sigset);
1347 unlock_user(target_sigset, sigset, 0);
1348
1349 *pset = host_set;
1350 return 0;
1351}