]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/signal.c
linux-user/host/s390: Treat EX and EXRL as writes
[mirror_qemu.git] / linux-user / signal.c
CommitLineData
31e31b8a 1/*
66fb9763 2 * Emulation of Linux signals
5fafdf24 3 *
31e31b8a
FB
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
8167ee88 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31e31b8a 18 */
d39594e9 19#include "qemu/osdep.h"
a70dadc7 20#include "qemu/bitops.h"
85b4fa0c 21#include "exec/gdbstub.h"
e6037d04 22#include "hw/core/tcg-cpu-ops.h"
85b4fa0c 23
31e31b8a 24#include <sys/ucontext.h>
edf8e2af 25#include <sys/resource.h>
31e31b8a 26
3ef693a0 27#include "qemu.h"
3b249d26 28#include "user-internals.h"
a44d57a3 29#include "strace.h"
3ad0a769 30#include "loader.h"
c8ee0a44 31#include "trace.h"
befb7447 32#include "signal-common.h"
e6037d04 33#include "host-signal.h"
bbf15aaf 34#include "user/safe-syscall.h"
66fb9763 35
624f7979 36static struct target_sigaction sigact_table[TARGET_NSIG];
31e31b8a 37
5fafdf24 38static void host_signal_handler(int host_signum, siginfo_t *info,
66fb9763
FB
39 void *puc);
40
db2af69d
RH
41/* Fallback addresses into sigtramp page. */
42abi_ulong default_sigreturn;
43abi_ulong default_rt_sigreturn;
9fcff3a6
LV
44
45/*
46 * System includes define _NSIG as SIGRTMAX + 1,
47 * but qemu (like the kernel) defines TARGET_NSIG as TARGET_SIGRTMAX
48 * and the first signal is SIGHUP defined as 1
49 * Signal number 0 is reserved for use as kill(pid, 0), to test whether
50 * a process exists without sending it a signal.
51 */
144bff03 52#ifdef __SIGRTMAX
9fcff3a6 53QEMU_BUILD_BUG_ON(__SIGRTMAX + 1 != _NSIG);
144bff03 54#endif
3ca05588 55static uint8_t host_to_target_signal_table[_NSIG] = {
9e5f5284
FB
56 [SIGHUP] = TARGET_SIGHUP,
57 [SIGINT] = TARGET_SIGINT,
58 [SIGQUIT] = TARGET_SIGQUIT,
59 [SIGILL] = TARGET_SIGILL,
60 [SIGTRAP] = TARGET_SIGTRAP,
61 [SIGABRT] = TARGET_SIGABRT,
01e3b763 62/* [SIGIOT] = TARGET_SIGIOT,*/
9e5f5284
FB
63 [SIGBUS] = TARGET_SIGBUS,
64 [SIGFPE] = TARGET_SIGFPE,
65 [SIGKILL] = TARGET_SIGKILL,
66 [SIGUSR1] = TARGET_SIGUSR1,
67 [SIGSEGV] = TARGET_SIGSEGV,
68 [SIGUSR2] = TARGET_SIGUSR2,
69 [SIGPIPE] = TARGET_SIGPIPE,
70 [SIGALRM] = TARGET_SIGALRM,
71 [SIGTERM] = TARGET_SIGTERM,
72#ifdef SIGSTKFLT
73 [SIGSTKFLT] = TARGET_SIGSTKFLT,
74#endif
75 [SIGCHLD] = TARGET_SIGCHLD,
76 [SIGCONT] = TARGET_SIGCONT,
77 [SIGSTOP] = TARGET_SIGSTOP,
78 [SIGTSTP] = TARGET_SIGTSTP,
79 [SIGTTIN] = TARGET_SIGTTIN,
80 [SIGTTOU] = TARGET_SIGTTOU,
81 [SIGURG] = TARGET_SIGURG,
82 [SIGXCPU] = TARGET_SIGXCPU,
83 [SIGXFSZ] = TARGET_SIGXFSZ,
84 [SIGVTALRM] = TARGET_SIGVTALRM,
85 [SIGPROF] = TARGET_SIGPROF,
86 [SIGWINCH] = TARGET_SIGWINCH,
87 [SIGIO] = TARGET_SIGIO,
88 [SIGPWR] = TARGET_SIGPWR,
89 [SIGSYS] = TARGET_SIGSYS,
90 /* next signals stay the same */
91};
9e5f5284 92
9fcff3a6
LV
93static uint8_t target_to_host_signal_table[TARGET_NSIG + 1];
94
95/* valid sig is between 1 and _NSIG - 1 */
1d9d8b55 96int host_to_target_signal(int sig)
31e31b8a 97{
9fcff3a6 98 if (sig < 1 || sig >= _NSIG) {
4cb05961 99 return sig;
9fcff3a6 100 }
9e5f5284 101 return host_to_target_signal_table[sig];
31e31b8a
FB
102}
103
9fcff3a6 104/* valid sig is between 1 and TARGET_NSIG */
4cb05961 105int target_to_host_signal(int sig)
31e31b8a 106{
9fcff3a6 107 if (sig < 1 || sig > TARGET_NSIG) {
4cb05961 108 return sig;
9fcff3a6 109 }
9e5f5284 110 return target_to_host_signal_table[sig];
31e31b8a
FB
111}
112
c227f099 113static inline void target_sigaddset(target_sigset_t *set, int signum)
f5545b5c
PB
114{
115 signum--;
116 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
117 set->sig[signum / TARGET_NSIG_BPW] |= mask;
118}
119
c227f099 120static inline int target_sigismember(const target_sigset_t *set, int signum)
f5545b5c
PB
121{
122 signum--;
123 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
124 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
125}
126
befb7447
LV
127void host_to_target_sigset_internal(target_sigset_t *d,
128 const sigset_t *s)
66fb9763 129{
9fcff3a6 130 int host_sig, target_sig;
f5545b5c 131 target_sigemptyset(d);
9fcff3a6
LV
132 for (host_sig = 1; host_sig < _NSIG; host_sig++) {
133 target_sig = host_to_target_signal(host_sig);
134 if (target_sig < 1 || target_sig > TARGET_NSIG) {
135 continue;
136 }
137 if (sigismember(s, host_sig)) {
138 target_sigaddset(d, target_sig);
f5545b5c 139 }
66fb9763
FB
140 }
141}
142
c227f099 143void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
9231944d 144{
c227f099 145 target_sigset_t d1;
9231944d
FB
146 int i;
147
148 host_to_target_sigset_internal(&d1, s);
149 for(i = 0;i < TARGET_NSIG_WORDS; i++)
cbb21eed 150 d->sig[i] = tswapal(d1.sig[i]);
9231944d
FB
151}
152
befb7447
LV
153void target_to_host_sigset_internal(sigset_t *d,
154 const target_sigset_t *s)
66fb9763 155{
9fcff3a6 156 int host_sig, target_sig;
f5545b5c 157 sigemptyset(d);
9fcff3a6
LV
158 for (target_sig = 1; target_sig <= TARGET_NSIG; target_sig++) {
159 host_sig = target_to_host_signal(target_sig);
160 if (host_sig < 1 || host_sig >= _NSIG) {
161 continue;
162 }
163 if (target_sigismember(s, target_sig)) {
164 sigaddset(d, host_sig);
f5545b5c 165 }
da7c8647 166 }
66fb9763
FB
167}
168
c227f099 169void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
9231944d 170{
c227f099 171 target_sigset_t s1;
9231944d
FB
172 int i;
173
174 for(i = 0;i < TARGET_NSIG_WORDS; i++)
cbb21eed 175 s1.sig[i] = tswapal(s->sig[i]);
9231944d
FB
176 target_to_host_sigset_internal(d, &s1);
177}
3b46e624 178
992f48a0 179void host_to_target_old_sigset(abi_ulong *old_sigset,
66fb9763
FB
180 const sigset_t *sigset)
181{
c227f099 182 target_sigset_t d;
9e5f5284
FB
183 host_to_target_sigset(&d, sigset);
184 *old_sigset = d.sig[0];
66fb9763
FB
185}
186
5fafdf24 187void target_to_host_old_sigset(sigset_t *sigset,
992f48a0 188 const abi_ulong *old_sigset)
66fb9763 189{
c227f099 190 target_sigset_t d;
9e5f5284
FB
191 int i;
192
193 d.sig[0] = *old_sigset;
194 for(i = 1;i < TARGET_NSIG_WORDS; i++)
195 d.sig[i] = 0;
196 target_to_host_sigset(sigset, &d);
66fb9763
FB
197}
198
3d3efba0
PM
199int block_signals(void)
200{
201 TaskState *ts = (TaskState *)thread_cpu->opaque;
202 sigset_t set;
3d3efba0
PM
203
204 /* It's OK to block everything including SIGSEGV, because we won't
205 * run any further guest code before unblocking signals in
206 * process_pending_signals().
207 */
208 sigfillset(&set);
209 sigprocmask(SIG_SETMASK, &set, 0);
210
d73415a3 211 return qatomic_xchg(&ts->signal_pending, 1);
3d3efba0
PM
212}
213
1c275925
AB
214/* Wrapper for sigprocmask function
215 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
af254a27 216 * are host signal set, not guest ones. Returns -QEMU_ERESTARTSYS if
3d3efba0
PM
217 * a signal was already pending and the syscall must be restarted, or
218 * 0 on success.
219 * If set is NULL, this is guaranteed not to fail.
1c275925
AB
220 */
221int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
222{
3d3efba0
PM
223 TaskState *ts = (TaskState *)thread_cpu->opaque;
224
225 if (oldset) {
226 *oldset = ts->signal_mask;
227 }
a7ec0f98
PM
228
229 if (set) {
3d3efba0 230 int i;
a7ec0f98 231
3d3efba0 232 if (block_signals()) {
af254a27 233 return -QEMU_ERESTARTSYS;
3d3efba0 234 }
a7ec0f98
PM
235
236 switch (how) {
237 case SIG_BLOCK:
3d3efba0 238 sigorset(&ts->signal_mask, &ts->signal_mask, set);
a7ec0f98
PM
239 break;
240 case SIG_UNBLOCK:
3d3efba0
PM
241 for (i = 1; i <= NSIG; ++i) {
242 if (sigismember(set, i)) {
243 sigdelset(&ts->signal_mask, i);
244 }
a7ec0f98
PM
245 }
246 break;
247 case SIG_SETMASK:
3d3efba0 248 ts->signal_mask = *set;
a7ec0f98
PM
249 break;
250 default:
251 g_assert_not_reached();
252 }
a7ec0f98 253
3d3efba0
PM
254 /* Silently ignore attempts to change blocking status of KILL or STOP */
255 sigdelset(&ts->signal_mask, SIGKILL);
256 sigdelset(&ts->signal_mask, SIGSTOP);
a7ec0f98 257 }
3d3efba0 258 return 0;
1c275925
AB
259}
260
3d3efba0
PM
261/* Just set the guest's signal mask to the specified value; the
262 * caller is assumed to have called block_signals() already.
263 */
befb7447 264void set_sigmask(const sigset_t *set)
9eede5b6 265{
3d3efba0
PM
266 TaskState *ts = (TaskState *)thread_cpu->opaque;
267
268 ts->signal_mask = *set;
9eede5b6 269}
9eede5b6 270
465e237b
LV
271/* sigaltstack management */
272
273int on_sig_stack(unsigned long sp)
274{
5bfce0b7
PM
275 TaskState *ts = (TaskState *)thread_cpu->opaque;
276
277 return (sp - ts->sigaltstack_used.ss_sp
278 < ts->sigaltstack_used.ss_size);
465e237b
LV
279}
280
281int sas_ss_flags(unsigned long sp)
282{
5bfce0b7
PM
283 TaskState *ts = (TaskState *)thread_cpu->opaque;
284
285 return (ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE
465e237b
LV
286 : on_sig_stack(sp) ? SS_ONSTACK : 0);
287}
288
289abi_ulong target_sigsp(abi_ulong sp, struct target_sigaction *ka)
290{
291 /*
292 * This is the X/Open sanctioned signal stack switching.
293 */
5bfce0b7
PM
294 TaskState *ts = (TaskState *)thread_cpu->opaque;
295
465e237b 296 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
5bfce0b7 297 return ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
465e237b
LV
298 }
299 return sp;
300}
301
302void target_save_altstack(target_stack_t *uss, CPUArchState *env)
303{
5bfce0b7
PM
304 TaskState *ts = (TaskState *)thread_cpu->opaque;
305
306 __put_user(ts->sigaltstack_used.ss_sp, &uss->ss_sp);
465e237b 307 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &uss->ss_flags);
5bfce0b7 308 __put_user(ts->sigaltstack_used.ss_size, &uss->ss_size);
465e237b
LV
309}
310
ddc3e74d 311abi_long target_restore_altstack(target_stack_t *uss, CPUArchState *env)
92bad948
RH
312{
313 TaskState *ts = (TaskState *)thread_cpu->opaque;
314 size_t minstacksize = TARGET_MINSIGSTKSZ;
315 target_stack_t ss;
316
317#if defined(TARGET_PPC64)
318 /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
319 struct image_info *image = ts->info;
320 if (get_ppc64_abi(image) > 1) {
321 minstacksize = 4096;
322 }
323#endif
324
325 __get_user(ss.ss_sp, &uss->ss_sp);
326 __get_user(ss.ss_size, &uss->ss_size);
327 __get_user(ss.ss_flags, &uss->ss_flags);
328
ddc3e74d 329 if (on_sig_stack(get_sp_from_cpustate(env))) {
92bad948
RH
330 return -TARGET_EPERM;
331 }
332
333 switch (ss.ss_flags) {
334 default:
335 return -TARGET_EINVAL;
336
337 case TARGET_SS_DISABLE:
338 ss.ss_size = 0;
339 ss.ss_sp = 0;
340 break;
341
342 case TARGET_SS_ONSTACK:
343 case 0:
344 if (ss.ss_size < minstacksize) {
345 return -TARGET_ENOMEM;
346 }
347 break;
348 }
349
350 ts->sigaltstack_used.ss_sp = ss.ss_sp;
351 ts->sigaltstack_used.ss_size = ss.ss_size;
352 return 0;
353}
354
9de5e440
FB
355/* siginfo conversion */
356
c227f099 357static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
9de5e440 358 const siginfo_t *info)
66fb9763 359{
a05c6409 360 int sig = host_to_target_signal(info->si_signo);
a70dadc7
PM
361 int si_code = info->si_code;
362 int si_type;
9de5e440
FB
363 tinfo->si_signo = sig;
364 tinfo->si_errno = 0;
afd7cd92 365 tinfo->si_code = info->si_code;
a05c6409 366
55d72a7e
PM
367 /* This memset serves two purposes:
368 * (1) ensure we don't leak random junk to the guest later
369 * (2) placate false positives from gcc about fields
370 * being used uninitialized if it chooses to inline both this
371 * function and tswap_siginfo() into host_to_target_siginfo().
372 */
373 memset(tinfo->_sifields._pad, 0, sizeof(tinfo->_sifields._pad));
374
a70dadc7
PM
375 /* This is awkward, because we have to use a combination of
376 * the si_code and si_signo to figure out which of the union's
377 * members are valid. (Within the host kernel it is always possible
378 * to tell, but the kernel carefully avoids giving userspace the
379 * high 16 bits of si_code, so we don't have the information to
380 * do this the easy way...) We therefore make our best guess,
381 * bearing in mind that a guest can spoof most of the si_codes
382 * via rt_sigqueueinfo() if it likes.
383 *
384 * Once we have made our guess, we record it in the top 16 bits of
385 * the si_code, so that tswap_siginfo() later can use it.
386 * tswap_siginfo() will strip these top bits out before writing
387 * si_code to the guest (sign-extending the lower bits).
388 */
389
390 switch (si_code) {
391 case SI_USER:
392 case SI_TKILL:
393 case SI_KERNEL:
394 /* Sent via kill(), tkill() or tgkill(), or direct from the kernel.
395 * These are the only unspoofable si_code values.
396 */
397 tinfo->_sifields._kill._pid = info->si_pid;
398 tinfo->_sifields._kill._uid = info->si_uid;
399 si_type = QEMU_SI_KILL;
400 break;
401 default:
402 /* Everything else is spoofable. Make best guess based on signal */
403 switch (sig) {
404 case TARGET_SIGCHLD:
405 tinfo->_sifields._sigchld._pid = info->si_pid;
406 tinfo->_sifields._sigchld._uid = info->si_uid;
139e5de7
MS
407 if (si_code == CLD_EXITED)
408 tinfo->_sifields._sigchld._status = info->si_status;
409 else
410 tinfo->_sifields._sigchld._status
411 = host_to_target_signal(info->si_status & 0x7f)
412 | (info->si_status & ~0x7f);
a70dadc7
PM
413 tinfo->_sifields._sigchld._utime = info->si_utime;
414 tinfo->_sifields._sigchld._stime = info->si_stime;
415 si_type = QEMU_SI_CHLD;
416 break;
417 case TARGET_SIGIO:
418 tinfo->_sifields._sigpoll._band = info->si_band;
419 tinfo->_sifields._sigpoll._fd = info->si_fd;
420 si_type = QEMU_SI_POLL;
421 break;
422 default:
423 /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */
424 tinfo->_sifields._rt._pid = info->si_pid;
425 tinfo->_sifields._rt._uid = info->si_uid;
426 /* XXX: potential problem if 64 bit */
427 tinfo->_sifields._rt._sigval.sival_ptr
da7c8647 428 = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
a70dadc7
PM
429 si_type = QEMU_SI_RT;
430 break;
431 }
432 break;
9de5e440 433 }
a70dadc7
PM
434
435 tinfo->si_code = deposit32(si_code, 16, 16, si_type);
9de5e440
FB
436}
437
befb7447
LV
438void tswap_siginfo(target_siginfo_t *tinfo,
439 const target_siginfo_t *info)
9de5e440 440{
a70dadc7
PM
441 int si_type = extract32(info->si_code, 16, 16);
442 int si_code = sextract32(info->si_code, 0, 16);
443
444 __put_user(info->si_signo, &tinfo->si_signo);
445 __put_user(info->si_errno, &tinfo->si_errno);
446 __put_user(si_code, &tinfo->si_code);
447
448 /* We can use our internal marker of which fields in the structure
449 * are valid, rather than duplicating the guesswork of
450 * host_to_target_siginfo_noswap() here.
451 */
452 switch (si_type) {
453 case QEMU_SI_KILL:
454 __put_user(info->_sifields._kill._pid, &tinfo->_sifields._kill._pid);
455 __put_user(info->_sifields._kill._uid, &tinfo->_sifields._kill._uid);
456 break;
457 case QEMU_SI_TIMER:
458 __put_user(info->_sifields._timer._timer1,
459 &tinfo->_sifields._timer._timer1);
460 __put_user(info->_sifields._timer._timer2,
461 &tinfo->_sifields._timer._timer2);
462 break;
463 case QEMU_SI_POLL:
464 __put_user(info->_sifields._sigpoll._band,
465 &tinfo->_sifields._sigpoll._band);
466 __put_user(info->_sifields._sigpoll._fd,
467 &tinfo->_sifields._sigpoll._fd);
468 break;
469 case QEMU_SI_FAULT:
470 __put_user(info->_sifields._sigfault._addr,
471 &tinfo->_sifields._sigfault._addr);
472 break;
473 case QEMU_SI_CHLD:
474 __put_user(info->_sifields._sigchld._pid,
475 &tinfo->_sifields._sigchld._pid);
476 __put_user(info->_sifields._sigchld._uid,
477 &tinfo->_sifields._sigchld._uid);
478 __put_user(info->_sifields._sigchld._status,
479 &tinfo->_sifields._sigchld._status);
480 __put_user(info->_sifields._sigchld._utime,
481 &tinfo->_sifields._sigchld._utime);
482 __put_user(info->_sifields._sigchld._stime,
483 &tinfo->_sifields._sigchld._stime);
484 break;
485 case QEMU_SI_RT:
486 __put_user(info->_sifields._rt._pid, &tinfo->_sifields._rt._pid);
487 __put_user(info->_sifields._rt._uid, &tinfo->_sifields._rt._uid);
488 __put_user(info->_sifields._rt._sigval.sival_ptr,
489 &tinfo->_sifields._rt._sigval.sival_ptr);
490 break;
491 default:
492 g_assert_not_reached();
9de5e440
FB
493 }
494}
495
c227f099 496void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
9de5e440 497{
55d72a7e
PM
498 target_siginfo_t tgt_tmp;
499 host_to_target_siginfo_noswap(&tgt_tmp, info);
500 tswap_siginfo(tinfo, &tgt_tmp);
66fb9763
FB
501}
502
9de5e440 503/* XXX: we support only POSIX RT signals are used. */
aa1f17c1 504/* XXX: find a solution for 64 bit (additional malloced data is needed) */
c227f099 505void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
66fb9763 506{
90c0f080
PM
507 /* This conversion is used only for the rt_sigqueueinfo syscall,
508 * and so we know that the _rt fields are the valid ones.
509 */
510 abi_ulong sival_ptr;
511
512 __get_user(info->si_signo, &tinfo->si_signo);
513 __get_user(info->si_errno, &tinfo->si_errno);
514 __get_user(info->si_code, &tinfo->si_code);
515 __get_user(info->si_pid, &tinfo->_sifields._rt._pid);
516 __get_user(info->si_uid, &tinfo->_sifields._rt._uid);
517 __get_user(sival_ptr, &tinfo->_sifields._rt._sigval.sival_ptr);
518 info->si_value.sival_ptr = (void *)(long)sival_ptr;
66fb9763
FB
519}
520
ca587a8e
AJ
521static int fatal_signal (int sig)
522{
523 switch (sig) {
524 case TARGET_SIGCHLD:
525 case TARGET_SIGURG:
526 case TARGET_SIGWINCH:
527 /* Ignored by default. */
528 return 0;
529 case TARGET_SIGCONT:
530 case TARGET_SIGSTOP:
531 case TARGET_SIGTSTP:
532 case TARGET_SIGTTIN:
533 case TARGET_SIGTTOU:
534 /* Job control signals. */
535 return 0;
536 default:
537 return 1;
538 }
539}
540
edf8e2af
MW
541/* returns 1 if given signal should dump core if not handled */
542static int core_dump_signal(int sig)
543{
544 switch (sig) {
545 case TARGET_SIGABRT:
546 case TARGET_SIGFPE:
547 case TARGET_SIGILL:
548 case TARGET_SIGQUIT:
549 case TARGET_SIGSEGV:
550 case TARGET_SIGTRAP:
551 case TARGET_SIGBUS:
552 return (1);
553 default:
554 return (0);
555 }
556}
557
365510fb
LV
558static void signal_table_init(void)
559{
6bc024e7 560 int host_sig, target_sig, count;
365510fb
LV
561
562 /*
6bc024e7
LV
563 * Signals are supported starting from TARGET_SIGRTMIN and going up
564 * until we run out of host realtime signals.
565 * glibc at least uses only the lower 2 rt signals and probably
566 * nobody's using the upper ones.
567 * it's why SIGRTMIN (34) is generally greater than __SIGRTMIN (32)
365510fb
LV
568 * To fix this properly we need to do manual signal delivery multiplexed
569 * over a single host signal.
6bc024e7
LV
570 * Attempts for configure "missing" signals via sigaction will be
571 * silently ignored.
365510fb 572 */
6bc024e7
LV
573 for (host_sig = SIGRTMIN; host_sig <= SIGRTMAX; host_sig++) {
574 target_sig = host_sig - SIGRTMIN + TARGET_SIGRTMIN;
575 if (target_sig <= TARGET_NSIG) {
576 host_to_target_signal_table[host_sig] = target_sig;
577 }
578 }
365510fb
LV
579
580 /* generate signal conversion tables */
6bc024e7
LV
581 for (target_sig = 1; target_sig <= TARGET_NSIG; target_sig++) {
582 target_to_host_signal_table[target_sig] = _NSIG; /* poison */
583 }
365510fb
LV
584 for (host_sig = 1; host_sig < _NSIG; host_sig++) {
585 if (host_to_target_signal_table[host_sig] == 0) {
586 host_to_target_signal_table[host_sig] = host_sig;
587 }
365510fb 588 target_sig = host_to_target_signal_table[host_sig];
9fcff3a6
LV
589 if (target_sig <= TARGET_NSIG) {
590 target_to_host_signal_table[target_sig] = host_sig;
591 }
365510fb 592 }
6bc024e7
LV
593
594 if (trace_event_get_state_backends(TRACE_SIGNAL_TABLE_INIT)) {
595 for (target_sig = 1, count = 0; target_sig <= TARGET_NSIG; target_sig++) {
596 if (target_to_host_signal_table[target_sig] == _NSIG) {
597 count++;
598 }
599 }
600 trace_signal_table_init(count);
601 }
365510fb
LV
602}
603
31e31b8a
FB
604void signal_init(void)
605{
3d3efba0 606 TaskState *ts = (TaskState *)thread_cpu->opaque;
31e31b8a 607 struct sigaction act;
624f7979 608 struct sigaction oact;
365510fb 609 int i;
624f7979 610 int host_sig;
31e31b8a 611
365510fb
LV
612 /* initialize signal conversion tables */
613 signal_table_init();
3b46e624 614
3d3efba0
PM
615 /* Set the signal mask from the host mask. */
616 sigprocmask(0, 0, &ts->signal_mask);
617
9de5e440 618 sigfillset(&act.sa_mask);
31e31b8a
FB
619 act.sa_flags = SA_SIGINFO;
620 act.sa_sigaction = host_signal_handler;
624f7979 621 for(i = 1; i <= TARGET_NSIG; i++) {
4cc600d2 622#ifdef CONFIG_GPROF
9fcff3a6 623 if (i == TARGET_SIGPROF) {
716cdbe0
AB
624 continue;
625 }
626#endif
624f7979
PB
627 host_sig = target_to_host_signal(i);
628 sigaction(host_sig, NULL, &oact);
629 if (oact.sa_sigaction == (void *)SIG_IGN) {
630 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
631 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
632 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
633 }
634 /* If there's already a handler installed then something has
635 gone horribly wrong, so don't even try to handle that case. */
ca587a8e
AJ
636 /* Install some handlers for our own use. We need at least
637 SIGSEGV and SIGBUS, to detect exceptions. We can not just
638 trap all signals because it affects syscall interrupt
639 behavior. But do trap all default-fatal signals. */
640 if (fatal_signal (i))
624f7979 641 sigaction(host_sig, &act, NULL);
31e31b8a 642 }
66fb9763
FB
643}
644
c599d4d6
PM
645/* Force a synchronously taken signal. The kernel force_sig() function
646 * also forces the signal to "not blocked, not ignored", but for QEMU
647 * that work is done in process_pending_signals().
648 */
befb7447 649void force_sig(int sig)
c599d4d6
PM
650{
651 CPUState *cpu = thread_cpu;
652 CPUArchState *env = cpu->env_ptr;
819121b9 653 target_siginfo_t info = {};
c599d4d6
PM
654
655 info.si_signo = sig;
656 info.si_errno = 0;
657 info.si_code = TARGET_SI_KERNEL;
658 info._sifields._kill._pid = 0;
659 info._sifields._kill._uid = 0;
660 queue_signal(env, info.si_signo, QEMU_SI_KILL, &info);
661}
09391669 662
af796960
PM
663/*
664 * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the
665 * 'force' part is handled in process_pending_signals().
666 */
667void force_sig_fault(int sig, int code, abi_ulong addr)
668{
669 CPUState *cpu = thread_cpu;
670 CPUArchState *env = cpu->env_ptr;
671 target_siginfo_t info = {};
672
673 info.si_signo = sig;
674 info.si_errno = 0;
675 info.si_code = code;
676 info._sifields._sigfault._addr = addr;
677 queue_signal(env, sig, QEMU_SI_FAULT, &info);
678}
679
09391669
PM
680/* Force a SIGSEGV if we couldn't write to memory trying to set
681 * up the signal frame. oldsig is the signal we were trying to handle
682 * at the point of failure.
683 */
47ae93cd 684#if !defined(TARGET_RISCV)
befb7447 685void force_sigsegv(int oldsig)
09391669 686{
09391669
PM
687 if (oldsig == SIGSEGV) {
688 /* Make sure we don't try to deliver the signal again; this will
c599d4d6 689 * end up with handle_pending_signal() calling dump_core_and_abort().
09391669
PM
690 */
691 sigact_table[oldsig - 1]._sa_handler = TARGET_SIG_DFL;
692 }
c4b35744 693 force_sig(TARGET_SIGSEGV);
09391669 694}
47ae93cd
MC
695#endif
696
72d2bbf9
RH
697void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr,
698 MMUAccessType access_type, bool maperr, uintptr_t ra)
699{
700 const struct TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
701
702 if (tcg_ops->record_sigsegv) {
703 tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra);
72d2bbf9
RH
704 }
705
706 force_sig_fault(TARGET_SIGSEGV,
707 maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR,
708 addr);
709 cpu->exception_index = EXCP_INTERRUPT;
710 cpu_loop_exit_restore(cpu, ra);
711}
712
12ed5640
RH
713void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr,
714 MMUAccessType access_type, uintptr_t ra)
715{
716 const struct TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
717
718 if (tcg_ops->record_sigbus) {
719 tcg_ops->record_sigbus(cpu, addr, access_type, ra);
720 }
721
722 force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr);
723 cpu->exception_index = EXCP_INTERRUPT;
724 cpu_loop_exit_restore(cpu, ra);
725}
726
9de5e440 727/* abort execution with signal */
8905770b
MAL
728static G_NORETURN
729void dump_core_and_abort(int target_sig)
66fb9763 730{
0429a971
AF
731 CPUState *cpu = thread_cpu;
732 CPUArchState *env = cpu->env_ptr;
733 TaskState *ts = (TaskState *)cpu->opaque;
edf8e2af 734 int host_sig, core_dumped = 0;
603e4fd7 735 struct sigaction act;
c8ee0a44 736
66393fb9 737 host_sig = target_to_host_signal(target_sig);
b5f95366 738 trace_user_dump_core_and_abort(env, target_sig, host_sig);
a2247f8e 739 gdb_signalled(env, target_sig);
603e4fd7 740
edf8e2af 741 /* dump core if supported by target binary format */
66393fb9 742 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
edf8e2af
MW
743 stop_all_tasks();
744 core_dumped =
a2247f8e 745 ((*ts->bprm->core_dump)(target_sig, env) == 0);
edf8e2af
MW
746 }
747 if (core_dumped) {
748 /* we already dumped the core of target process, we don't want
749 * a coredump of qemu itself */
750 struct rlimit nodump;
751 getrlimit(RLIMIT_CORE, &nodump);
752 nodump.rlim_cur=0;
753 setrlimit(RLIMIT_CORE, &nodump);
754 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
66393fb9 755 target_sig, strsignal(host_sig), "core dumped" );
edf8e2af
MW
756 }
757
0c58751c 758 /* The proper exit code for dying from an uncaught signal is
603e4fd7
AJ
759 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
760 * a negative value. To get the proper exit code we need to
761 * actually die from an uncaught signal. Here the default signal
762 * handler is installed, we send ourself a signal and we wait for
763 * it to arrive. */
764 sigfillset(&act.sa_mask);
765 act.sa_handler = SIG_DFL;
3a5d30bf 766 act.sa_flags = 0;
603e4fd7
AJ
767 sigaction(host_sig, &act, NULL);
768
769 /* For some reason raise(host_sig) doesn't send the signal when
770 * statically linked on x86-64. */
771 kill(getpid(), host_sig);
772
773 /* Make sure the signal isn't masked (just reuse the mask inside
774 of act) */
775 sigdelset(&act.sa_mask, host_sig);
776 sigsuspend(&act.sa_mask);
777
778 /* unreachable */
a6c6f76c 779 abort();
66fb9763
FB
780}
781
9de5e440
FB
782/* queue a signal so that it will be send to the virtual CPU as soon
783 as possible */
337e88d8
PM
784void queue_signal(CPUArchState *env, int sig, int si_type,
785 target_siginfo_t *info)
31e31b8a 786{
29a0af61 787 CPUState *cpu = env_cpu(env);
0429a971 788 TaskState *ts = cpu->opaque;
66fb9763 789
c8ee0a44 790 trace_user_queue_signal(env, sig);
907f5fdd 791
9d2803f7 792 info->si_code = deposit32(info->si_code, 16, 16, si_type);
a70dadc7 793
655ed67c
TB
794 ts->sync_signal.info = *info;
795 ts->sync_signal.pending = sig;
907f5fdd 796 /* signal that a new signal is pending */
d73415a3 797 qatomic_set(&ts->signal_pending, 1);
9de5e440
FB
798}
799
07637888
WL
800
801/* Adjust the signal context to rewind out of safe-syscall if we're in it */
4d330cee
TB
802static inline void rewind_if_in_safe_syscall(void *puc)
803{
9940799b 804 host_sigcontext *uc = (host_sigcontext *)puc;
07637888
WL
805 uintptr_t pcreg = host_signal_pc(uc);
806
807 if (pcreg > (uintptr_t)safe_syscall_start
808 && pcreg < (uintptr_t)safe_syscall_end) {
809 host_signal_set_pc(uc, (uintptr_t)safe_syscall_start);
810 }
07637888 811}
4d330cee 812
e6037d04 813static void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
9de5e440 814{
a2247f8e 815 CPUArchState *env = thread_cpu->env_ptr;
29a0af61 816 CPUState *cpu = env_cpu(env);
655ed67c 817 TaskState *ts = cpu->opaque;
c227f099 818 target_siginfo_t tinfo;
9940799b 819 host_sigcontext *uc = puc;
655ed67c 820 struct emulated_sigtable *k;
e6037d04 821 int guest_sig;
e6037d04
RH
822 uintptr_t pc = 0;
823 bool sync_sig = false;
c8c89a6a 824 void *sigmask = host_signal_mask(uc);
e6037d04
RH
825
826 /*
827 * Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special
828 * handling wrt signal blocking and unwinding.
829 */
830 if ((host_sig == SIGSEGV || host_sig == SIGBUS) && info->si_code > 0) {
831 MMUAccessType access_type;
832 uintptr_t host_addr;
833 abi_ptr guest_addr;
834 bool is_write;
835
836 host_addr = (uintptr_t)info->si_addr;
837
838 /*
839 * Convert forcefully to guest address space: addresses outside
840 * reserved_va are still valid to report via SEGV_MAPERR.
841 */
842 guest_addr = h2g_nocheck(host_addr);
843
844 pc = host_signal_pc(uc);
845 is_write = host_signal_write(info, uc);
846 access_type = adjust_signal_pc(&pc, is_write);
847
848 if (host_sig == SIGSEGV) {
72d2bbf9 849 bool maperr = true;
e6037d04
RH
850
851 if (info->si_code == SEGV_ACCERR && h2g_valid(host_addr)) {
852 /* If this was a write to a TB protected page, restart. */
853 if (is_write &&
c8c89a6a 854 handle_sigsegv_accerr_write(cpu, sigmask, pc, guest_addr)) {
e6037d04
RH
855 return;
856 }
857
858 /*
859 * With reserved_va, the whole address space is PROT_NONE,
860 * which means that we may get ACCERR when we want MAPERR.
861 */
862 if (page_get_flags(guest_addr) & PAGE_VALID) {
72d2bbf9 863 maperr = false;
e6037d04
RH
864 } else {
865 info->si_code = SEGV_MAPERR;
866 }
867 }
868
c8c89a6a 869 sigprocmask(SIG_SETMASK, sigmask, NULL);
72d2bbf9 870 cpu_loop_exit_sigsegv(cpu, guest_addr, access_type, maperr, pc);
e6037d04 871 } else {
c8c89a6a 872 sigprocmask(SIG_SETMASK, sigmask, NULL);
742f0762
RH
873 if (info->si_code == BUS_ADRALN) {
874 cpu_loop_exit_sigbus(cpu, guest_addr, access_type, pc);
875 }
e6037d04
RH
876 }
877
878 sync_sig = true;
9de5e440
FB
879 }
880
881 /* get target signal number */
e6037d04
RH
882 guest_sig = host_to_target_signal(host_sig);
883 if (guest_sig < 1 || guest_sig > TARGET_NSIG) {
9de5e440 884 return;
e6037d04
RH
885 }
886 trace_user_host_signal(env, host_sig, guest_sig);
4d330cee 887
9de5e440 888 host_to_target_siginfo_noswap(&tinfo, info);
e6037d04 889 k = &ts->sigtab[guest_sig - 1];
655ed67c 890 k->info = tinfo;
e6037d04 891 k->pending = guest_sig;
655ed67c
TB
892 ts->signal_pending = 1;
893
e6037d04
RH
894 /*
895 * For synchronous signals, unwind the cpu state to the faulting
896 * insn and then exit back to the main loop so that the signal
897 * is delivered immediately.
898 */
899 if (sync_sig) {
900 cpu->exception_index = EXCP_INTERRUPT;
901 cpu_loop_exit_restore(cpu, pc);
902 }
e6037d04
RH
903
904 rewind_if_in_safe_syscall(puc);
905
906 /*
907 * Block host signals until target signal handler entered. We
655ed67c
TB
908 * can't block SIGSEGV or SIGBUS while we're executing guest
909 * code in case the guest code provokes one in the window between
910 * now and it getting out to the main loop. Signals will be
911 * unblocked again in process_pending_signals().
1d48fdd9 912 *
c8c89a6a 913 * WARNING: we cannot use sigfillset() here because the sigmask
1d48fdd9
PM
914 * field is a kernel sigset_t, which is much smaller than the
915 * libc sigset_t which sigfillset() operates on. Using sigfillset()
916 * would write 0xff bytes off the end of the structure and trash
917 * data on the struct.
655ed67c 918 */
c8c89a6a
RH
919 memset(sigmask, 0xff, SIGSET_T_SIZE);
920 sigdelset(sigmask, SIGSEGV);
921 sigdelset(sigmask, SIGBUS);
3d3efba0 922
655ed67c
TB
923 /* interrupt the virtual CPU as soon as possible */
924 cpu_exit(thread_cpu);
66fb9763
FB
925}
926
0da46a6e 927/* do_sigaltstack() returns target values and errnos. */
579a97f7 928/* compare linux/kernel/signal.c:do_sigaltstack() */
6b208755
RH
929abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr,
930 CPUArchState *env)
a04e134a 931{
92bad948
RH
932 target_stack_t oss, *uoss = NULL;
933 abi_long ret = -TARGET_EFAULT;
a04e134a 934
92bad948 935 if (uoss_addr) {
92bad948
RH
936 /* Verify writability now, but do not alter user memory yet. */
937 if (!lock_user_struct(VERIFY_WRITE, uoss, uoss_addr, 0)) {
938 goto out;
939 }
6b208755 940 target_save_altstack(&oss, env);
a04e134a
TS
941 }
942
92bad948
RH
943 if (uss_addr) {
944 target_stack_t *uss;
a04e134a 945
9eeb8306 946 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
a04e134a 947 goto out;
9eeb8306 948 }
ddc3e74d 949 ret = target_restore_altstack(uss, env);
92bad948 950 if (ret) {
a04e134a 951 goto out;
7d37435b 952 }
a04e134a
TS
953 }
954
579a97f7 955 if (uoss_addr) {
92bad948
RH
956 memcpy(uoss, &oss, sizeof(oss));
957 unlock_user_struct(uoss, uoss_addr, 1);
958 uoss = NULL;
a04e134a 959 }
a04e134a 960 ret = 0;
92bad948
RH
961
962 out:
963 if (uoss) {
964 unlock_user_struct(uoss, uoss_addr, 0);
965 }
a04e134a
TS
966 return ret;
967}
968
ef6a778e 969/* do_sigaction() return target values and host errnos */
66fb9763 970int do_sigaction(int sig, const struct target_sigaction *act,
02fb28e8 971 struct target_sigaction *oact, abi_ulong ka_restorer)
66fb9763 972{
624f7979 973 struct target_sigaction *k;
773b93ee
FB
974 struct sigaction act1;
975 int host_sig;
0da46a6e 976 int ret = 0;
66fb9763 977
6bc024e7
LV
978 trace_signal_do_sigaction_guest(sig, TARGET_NSIG);
979
ee3500d3
IL
980 if (sig < 1 || sig > TARGET_NSIG) {
981 return -TARGET_EINVAL;
982 }
983
984 if (act && (sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)) {
ef6a778e
TB
985 return -TARGET_EINVAL;
986 }
987
988 if (block_signals()) {
af254a27 989 return -QEMU_ERESTARTSYS;
ef6a778e
TB
990 }
991
66fb9763 992 k = &sigact_table[sig - 1];
66fb9763 993 if (oact) {
d2565875
RH
994 __put_user(k->_sa_handler, &oact->_sa_handler);
995 __put_user(k->sa_flags, &oact->sa_flags);
7f047de1 996#ifdef TARGET_ARCH_HAS_SA_RESTORER
d2565875 997 __put_user(k->sa_restorer, &oact->sa_restorer);
388bb21a 998#endif
d2565875 999 /* Not swapped. */
624f7979 1000 oact->sa_mask = k->sa_mask;
66fb9763
FB
1001 }
1002 if (act) {
d2565875
RH
1003 __get_user(k->_sa_handler, &act->_sa_handler);
1004 __get_user(k->sa_flags, &act->sa_flags);
7f047de1 1005#ifdef TARGET_ARCH_HAS_SA_RESTORER
d2565875 1006 __get_user(k->sa_restorer, &act->sa_restorer);
02fb28e8
RH
1007#endif
1008#ifdef TARGET_ARCH_HAS_KA_RESTORER
1009 k->ka_restorer = ka_restorer;
388bb21a 1010#endif
d2565875 1011 /* To be swapped in target_to_host_sigset. */
624f7979 1012 k->sa_mask = act->sa_mask;
773b93ee
FB
1013
1014 /* we update the host linux signal state */
1015 host_sig = target_to_host_signal(sig);
6bc024e7
LV
1016 trace_signal_do_sigaction_host(host_sig, TARGET_NSIG);
1017 if (host_sig > SIGRTMAX) {
1018 /* we don't have enough host signals to map all target signals */
1019 qemu_log_mask(LOG_UNIMP, "Unsupported target signal #%d, ignored\n",
1020 sig);
1021 /*
1022 * we don't return an error here because some programs try to
1023 * register an handler for all possible rt signals even if they
1024 * don't need it.
1025 * An error here can abort them whereas there can be no problem
1026 * to not have the signal available later.
1027 * This is the case for golang,
1028 * See https://github.com/golang/go/issues/33746
1029 * So we silently ignore the error.
1030 */
1031 return 0;
1032 }
773b93ee
FB
1033 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
1034 sigfillset(&act1.sa_mask);
1035 act1.sa_flags = SA_SIGINFO;
624f7979 1036 if (k->sa_flags & TARGET_SA_RESTART)
773b93ee
FB
1037 act1.sa_flags |= SA_RESTART;
1038 /* NOTE: it is important to update the host kernel signal
1039 ignore state to avoid getting unexpected interrupted
1040 syscalls */
624f7979 1041 if (k->_sa_handler == TARGET_SIG_IGN) {
773b93ee 1042 act1.sa_sigaction = (void *)SIG_IGN;
624f7979 1043 } else if (k->_sa_handler == TARGET_SIG_DFL) {
ca587a8e
AJ
1044 if (fatal_signal (sig))
1045 act1.sa_sigaction = host_signal_handler;
1046 else
1047 act1.sa_sigaction = (void *)SIG_DFL;
773b93ee
FB
1048 } else {
1049 act1.sa_sigaction = host_signal_handler;
1050 }
0da46a6e 1051 ret = sigaction(host_sig, &act1, NULL);
773b93ee 1052 }
66fb9763 1053 }
0da46a6e 1054 return ret;
66fb9763
FB
1055}
1056
31efaef1
PM
1057static void handle_pending_signal(CPUArchState *cpu_env, int sig,
1058 struct emulated_sigtable *k)
eb552501 1059{
29a0af61 1060 CPUState *cpu = env_cpu(cpu_env);
eb552501 1061 abi_ulong handler;
3d3efba0 1062 sigset_t set;
eb552501
PM
1063 target_sigset_t target_old_set;
1064 struct target_sigaction *sa;
eb552501 1065 TaskState *ts = cpu->opaque;
66fb9763 1066
c8ee0a44 1067 trace_user_handle_signal(cpu_env, sig);
66fb9763 1068 /* dequeue signal */
907f5fdd 1069 k->pending = 0;
3b46e624 1070
db6b81d4 1071 sig = gdb_handlesig(cpu, sig);
1fddef4b 1072 if (!sig) {
ca587a8e
AJ
1073 sa = NULL;
1074 handler = TARGET_SIG_IGN;
1075 } else {
1076 sa = &sigact_table[sig - 1];
1077 handler = sa->_sa_handler;
1fddef4b 1078 }
66fb9763 1079
4b25a506 1080 if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
0cb581d6
PM
1081 print_taken_signal(sig, &k->info);
1082 }
1083
66fb9763 1084 if (handler == TARGET_SIG_DFL) {
ca587a8e
AJ
1085 /* default handler : ignore some signal. The other are job control or fatal */
1086 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
1087 kill(getpid(),SIGSTOP);
1088 } else if (sig != TARGET_SIGCHLD &&
1089 sig != TARGET_SIGURG &&
1090 sig != TARGET_SIGWINCH &&
1091 sig != TARGET_SIGCONT) {
c599d4d6 1092 dump_core_and_abort(sig);
66fb9763
FB
1093 }
1094 } else if (handler == TARGET_SIG_IGN) {
1095 /* ignore sig */
1096 } else if (handler == TARGET_SIG_ERR) {
c599d4d6 1097 dump_core_and_abort(sig);
66fb9763 1098 } else {
9de5e440 1099 /* compute the blocked signals during the handler execution */
3d3efba0
PM
1100 sigset_t *blocked_set;
1101
624f7979 1102 target_to_host_sigset(&set, &sa->sa_mask);
9de5e440
FB
1103 /* SA_NODEFER indicates that the current signal should not be
1104 blocked during the handler */
624f7979 1105 if (!(sa->sa_flags & TARGET_SA_NODEFER))
9de5e440 1106 sigaddset(&set, target_to_host_signal(sig));
3b46e624 1107
9de5e440
FB
1108 /* save the previous blocked signal state to restore it at the
1109 end of the signal execution (see do_sigreturn) */
3d3efba0
PM
1110 host_to_target_sigset_internal(&target_old_set, &ts->signal_mask);
1111
1112 /* block signals in the handler */
1113 blocked_set = ts->in_sigsuspend ?
1114 &ts->sigsuspend_mask : &ts->signal_mask;
1115 sigorset(&ts->signal_mask, blocked_set, &set);
1116 ts->in_sigsuspend = 0;
9de5e440 1117
bc8a22cc 1118 /* if the CPU is in VM86 mode, we restore the 32 bit values */
84409ddb 1119#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bc8a22cc
FB
1120 {
1121 CPUX86State *env = cpu_env;
1122 if (env->eflags & VM_MASK)
1123 save_v86_state(env);
1124 }
1125#endif
9de5e440 1126 /* prepare the stack frame of the virtual CPU */
cb6ac802
LV
1127#if defined(TARGET_ARCH_HAS_SETUP_FRAME)
1128 if (sa->sa_flags & TARGET_SA_SIGINFO) {
907f5fdd 1129 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
cb6ac802 1130 } else {
624f7979 1131 setup_frame(sig, sa, &target_old_set, cpu_env);
cb6ac802
LV
1132 }
1133#else
1134 /* These targets do not have traditional signals. */
1135 setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
ff970904 1136#endif
7ec87e06 1137 if (sa->sa_flags & TARGET_SA_RESETHAND) {
624f7979 1138 sa->_sa_handler = TARGET_SIG_DFL;
7ec87e06 1139 }
31e31b8a 1140 }
66fb9763 1141}
e902d588
PM
1142
1143void process_pending_signals(CPUArchState *cpu_env)
1144{
29a0af61 1145 CPUState *cpu = env_cpu(cpu_env);
e902d588
PM
1146 int sig;
1147 TaskState *ts = cpu->opaque;
3d3efba0
PM
1148 sigset_t set;
1149 sigset_t *blocked_set;
e902d588 1150
d73415a3 1151 while (qatomic_read(&ts->signal_pending)) {
3d3efba0
PM
1152 sigfillset(&set);
1153 sigprocmask(SIG_SETMASK, &set, 0);
1154
8bd3773c 1155 restart_scan:
655ed67c
TB
1156 sig = ts->sync_signal.pending;
1157 if (sig) {
1158 /* Synchronous signals are forced,
1159 * see force_sig_info() and callers in Linux
1160 * Note that not all of our queue_signal() calls in QEMU correspond
1161 * to force_sig_info() calls in Linux (some are send_sig_info()).
1162 * However it seems like a kernel bug to me to allow the process
1163 * to block a synchronous signal since it could then just end up
1164 * looping round and round indefinitely.
1165 */
1166 if (sigismember(&ts->signal_mask, target_to_host_signal_table[sig])
1167 || sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) {
1168 sigdelset(&ts->signal_mask, target_to_host_signal_table[sig]);
1169 sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL;
1170 }
1171
31efaef1 1172 handle_pending_signal(cpu_env, sig, &ts->sync_signal);
655ed67c
TB
1173 }
1174
3d3efba0
PM
1175 for (sig = 1; sig <= TARGET_NSIG; sig++) {
1176 blocked_set = ts->in_sigsuspend ?
1177 &ts->sigsuspend_mask : &ts->signal_mask;
1178
1179 if (ts->sigtab[sig - 1].pending &&
1180 (!sigismember(blocked_set,
655ed67c 1181 target_to_host_signal_table[sig]))) {
31efaef1 1182 handle_pending_signal(cpu_env, sig, &ts->sigtab[sig - 1]);
8bd3773c
PM
1183 /* Restart scan from the beginning, as handle_pending_signal
1184 * might have resulted in a new synchronous signal (eg SIGSEGV).
1185 */
1186 goto restart_scan;
3d3efba0 1187 }
e902d588 1188 }
3d3efba0
PM
1189
1190 /* if no signal is pending, unblock signals and recheck (the act
1191 * of unblocking might cause us to take another host signal which
1192 * will set signal_pending again).
1193 */
d73415a3 1194 qatomic_set(&ts->signal_pending, 0);
3d3efba0
PM
1195 ts->in_sigsuspend = 0;
1196 set = ts->signal_mask;
1197 sigdelset(&set, SIGSEGV);
1198 sigdelset(&set, SIGBUS);
1199 sigprocmask(SIG_SETMASK, &set, 0);
1200 }
1201 ts->in_sigsuspend = 0;
e902d588 1202}
0a99f093
RH
1203
1204int process_sigsuspend_mask(sigset_t **pset, target_ulong sigset,
1205 target_ulong sigsize)
1206{
1207 TaskState *ts = (TaskState *)thread_cpu->opaque;
1208 sigset_t *host_set = &ts->sigsuspend_mask;
1209 target_sigset_t *target_sigset;
1210
1211 if (sigsize != sizeof(*target_sigset)) {
1212 /* Like the kernel, we enforce correct size sigsets */
1213 return -TARGET_EINVAL;
1214 }
1215
1216 target_sigset = lock_user(VERIFY_READ, sigset, sigsize, 1);
1217 if (!target_sigset) {
1218 return -TARGET_EFAULT;
1219 }
1220 target_to_host_sigset(host_set, target_sigset);
1221 unlock_user(target_sigset, sigset, 0);
1222
1223 *pset = host_set;
1224 return 0;
1225}