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