xref: /qemu/linux-user/signal.c (revision b6617e93)
1 /*
2  *  Emulation of Linux signals
3  *
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
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20 #include "qemu/bitops.h"
21 #include "gdbstub/user.h"
22 #include "hw/core/tcg-cpu-ops.h"
23 
24 #include <sys/ucontext.h>
25 #include <sys/resource.h>
26 
27 #include "qemu.h"
28 #include "user-internals.h"
29 #include "strace.h"
30 #include "loader.h"
31 #include "trace.h"
32 #include "signal-common.h"
33 #include "host-signal.h"
34 #include "user/safe-syscall.h"
35 #include "tcg/tcg.h"
36 
37 static struct target_sigaction sigact_table[TARGET_NSIG];
38 
39 static void host_signal_handler(int host_signum, siginfo_t *info,
40                                 void *puc);
41 
42 /* Fallback addresses into sigtramp page. */
43 abi_ulong default_sigreturn;
44 abi_ulong default_rt_sigreturn;
45 
46 /*
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.
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  */
52 #ifdef __SIGRTMAX
53 QEMU_BUILD_BUG_ON(__SIGRTMAX + 1 != _NSIG);
54 #endif
55 static uint8_t host_to_target_signal_table[_NSIG] = {
56 #define MAKE_SIG_ENTRY(sig)     [sig] = TARGET_##sig,
57         MAKE_SIGNAL_LIST
58 #undef MAKE_SIG_ENTRY
59 };
60 
61 static uint8_t target_to_host_signal_table[TARGET_NSIG + 1];
62 
63 /* valid sig is between 1 and _NSIG - 1 */
64 int host_to_target_signal(int sig)
65 {
66     if (sig < 1) {
67         return sig;
68     }
69     if (sig >= _NSIG) {
70         return TARGET_NSIG + 1;
71     }
72     return host_to_target_signal_table[sig];
73 }
74 
75 /* valid sig is between 1 and TARGET_NSIG */
76 int target_to_host_signal(int sig)
77 {
78     if (sig < 1) {
79         return sig;
80     }
81     if (sig > TARGET_NSIG) {
82         return _NSIG;
83     }
84     return target_to_host_signal_table[sig];
85 }
86 
87 static inline void target_sigaddset(target_sigset_t *set, int signum)
88 {
89     signum--;
90     abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
91     set->sig[signum / TARGET_NSIG_BPW] |= mask;
92 }
93 
94 static inline int target_sigismember(const target_sigset_t *set, int signum)
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 
101 void host_to_target_sigset_internal(target_sigset_t *d,
102                                     const sigset_t *s)
103 {
104     int host_sig, target_sig;
105     target_sigemptyset(d);
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);
113         }
114     }
115 }
116 
117 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
118 {
119     target_sigset_t d1;
120     int i;
121 
122     host_to_target_sigset_internal(&d1, s);
123     for(i = 0;i < TARGET_NSIG_WORDS; i++)
124         d->sig[i] = tswapal(d1.sig[i]);
125 }
126 
127 void target_to_host_sigset_internal(sigset_t *d,
128                                     const target_sigset_t *s)
129 {
130     int host_sig, target_sig;
131     sigemptyset(d);
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);
139         }
140     }
141 }
142 
143 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
144 {
145     target_sigset_t s1;
146     int i;
147 
148     for(i = 0;i < TARGET_NSIG_WORDS; i++)
149         s1.sig[i] = tswapal(s->sig[i]);
150     target_to_host_sigset_internal(d, &s1);
151 }
152 
153 void host_to_target_old_sigset(abi_ulong *old_sigset,
154                                const sigset_t *sigset)
155 {
156     target_sigset_t d;
157     host_to_target_sigset(&d, sigset);
158     *old_sigset = d.sig[0];
159 }
160 
161 void target_to_host_old_sigset(sigset_t *sigset,
162                                const abi_ulong *old_sigset)
163 {
164     target_sigset_t d;
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);
171 }
172 
173 int block_signals(void)
174 {
175     TaskState *ts = get_task_state(thread_cpu);
176     sigset_t set;
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 
185     return qatomic_xchg(&ts->signal_pending, 1);
186 }
187 
188 /* Wrapper for sigprocmask function
189  * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
190  * are host signal set, not guest ones. Returns -QEMU_ERESTARTSYS if
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.
194  */
195 int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
196 {
197     TaskState *ts = get_task_state(thread_cpu);
198 
199     if (oldset) {
200         *oldset = ts->signal_mask;
201     }
202 
203     if (set) {
204         int i;
205 
206         if (block_signals()) {
207             return -QEMU_ERESTARTSYS;
208         }
209 
210         switch (how) {
211         case SIG_BLOCK:
212             sigorset(&ts->signal_mask, &ts->signal_mask, set);
213             break;
214         case SIG_UNBLOCK:
215             for (i = 1; i <= NSIG; ++i) {
216                 if (sigismember(set, i)) {
217                     sigdelset(&ts->signal_mask, i);
218                 }
219             }
220             break;
221         case SIG_SETMASK:
222             ts->signal_mask = *set;
223             break;
224         default:
225             g_assert_not_reached();
226         }
227 
228         /* Silently ignore attempts to change blocking status of KILL or STOP */
229         sigdelset(&ts->signal_mask, SIGKILL);
230         sigdelset(&ts->signal_mask, SIGSTOP);
231     }
232     return 0;
233 }
234 
235 /* Just set the guest's signal mask to the specified value; the
236  * caller is assumed to have called block_signals() already.
237  */
238 void set_sigmask(const sigset_t *set)
239 {
240     TaskState *ts = get_task_state(thread_cpu);
241 
242     ts->signal_mask = *set;
243 }
244 
245 /* sigaltstack management */
246 
247 int on_sig_stack(unsigned long sp)
248 {
249     TaskState *ts = get_task_state(thread_cpu);
250 
251     return (sp - ts->sigaltstack_used.ss_sp
252             < ts->sigaltstack_used.ss_size);
253 }
254 
255 int sas_ss_flags(unsigned long sp)
256 {
257     TaskState *ts = get_task_state(thread_cpu);
258 
259     return (ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE
260             : on_sig_stack(sp) ? SS_ONSTACK : 0);
261 }
262 
263 abi_ulong target_sigsp(abi_ulong sp, struct target_sigaction *ka)
264 {
265     /*
266      * This is the X/Open sanctioned signal stack switching.
267      */
268     TaskState *ts = get_task_state(thread_cpu);
269 
270     if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
271         return ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
272     }
273     return sp;
274 }
275 
276 void target_save_altstack(target_stack_t *uss, CPUArchState *env)
277 {
278     TaskState *ts = get_task_state(thread_cpu);
279 
280     __put_user(ts->sigaltstack_used.ss_sp, &uss->ss_sp);
281     __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &uss->ss_flags);
282     __put_user(ts->sigaltstack_used.ss_size, &uss->ss_size);
283 }
284 
285 abi_long target_restore_altstack(target_stack_t *uss, CPUArchState *env)
286 {
287     TaskState *ts = get_task_state(thread_cpu);
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 
303     if (on_sig_stack(get_sp_from_cpustate(env))) {
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 
329 /* siginfo conversion */
330 
331 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
332                                                  const siginfo_t *info)
333 {
334     int sig = host_to_target_signal(info->si_signo);
335     int si_code = info->si_code;
336     int si_type;
337     tinfo->si_signo = sig;
338     tinfo->si_errno = 0;
339     tinfo->si_code = info->si_code;
340 
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 
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;
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);
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
402                 = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
403             si_type = QEMU_SI_RT;
404             break;
405         }
406         break;
407     }
408 
409     tinfo->si_code = deposit32(si_code, 16, 16, si_type);
410 }
411 
412 void tswap_siginfo(target_siginfo_t *tinfo,
413                    const target_siginfo_t *info)
414 {
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();
467     }
468 }
469 
470 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
471 {
472     target_siginfo_t tgt_tmp;
473     host_to_target_siginfo_noswap(&tgt_tmp, info);
474     tswap_siginfo(tinfo, &tgt_tmp);
475 }
476 
477 /* XXX: we support only POSIX RT signals are used. */
478 /* XXX: find a solution for 64 bit (additional malloced data is needed) */
479 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
480 {
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;
493 }
494 
495 /* returns 1 if given signal should dump core if not handled */
496 static 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 
512 static void signal_table_init(void)
513 {
514     int hsig, tsig, count;
515 
516     /*
517      * Signals are supported starting from TARGET_SIGRTMIN and going up
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.
523      * Attempts for configure "missing" signals via sigaction will be
524      * silently ignored.
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.
533      */
534 
535     hsig = SIGRTMIN;
536     host_to_target_signal_table[SIGABRT] = 0;
537     host_to_target_signal_table[hsig++] = TARGET_SIGABRT;
538 
539     for (tsig = TARGET_SIGRTMIN;
540          hsig <= SIGRTMAX && tsig <= TARGET_NSIG;
541          hsig++, tsig++) {
542         host_to_target_signal_table[hsig] = tsig;
543     }
544 
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;
551         }
552     }
553 
554     host_to_target_signal_table[SIGABRT] = TARGET_SIGABRT;
555 
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;
560         }
561     }
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);
570 }
571 
572 void signal_init(void)
573 {
574     TaskState *ts = get_task_state(thread_cpu);
575     struct sigaction act, oact;
576 
577     /* initialize signal conversion tables */
578     signal_table_init();
579 
580     /* Set the signal mask from the host mask. */
581     sigprocmask(0, 0, &ts->signal_mask);
582 
583     sigfillset(&act.sa_mask);
584     act.sa_flags = SA_SIGINFO;
585     act.sa_sigaction = host_signal_handler;
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 
599         if (hsig >= _NSIG) {
600             continue;
601         }
602 
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;
609             sigaction(hsig, iact, &oact);
610         }
611 
612         if (oact.sa_sigaction != (void *)SIG_IGN) {
613             thand = TARGET_SIG_DFL;
614         }
615         sigact_table[tsig - 1]._sa_handler = thand;
616     }
617 }
618 
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  */
623 void force_sig(int sig)
624 {
625     CPUState *cpu = thread_cpu;
626     target_siginfo_t info = {};
627 
628     info.si_signo = sig;
629     info.si_errno = 0;
630     info.si_code = TARGET_SI_KERNEL;
631     info._sifields._kill._pid = 0;
632     info._sifields._kill._uid = 0;
633     queue_signal(cpu_env(cpu), info.si_signo, QEMU_SI_KILL, &info);
634 }
635 
636 /*
637  * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the
638  * 'force' part is handled in process_pending_signals().
639  */
640 void force_sig_fault(int sig, int code, abi_ulong addr)
641 {
642     CPUState *cpu = thread_cpu;
643     target_siginfo_t info = {};
644 
645     info.si_signo = sig;
646     info.si_errno = 0;
647     info.si_code = code;
648     info._sifields._sigfault._addr = addr;
649     queue_signal(cpu_env(cpu), sig, QEMU_SI_FAULT, &info);
650 }
651 
652 /* Force a SIGSEGV if we couldn't write to memory trying to set
653  * up the signal frame. oldsig is the signal we were trying to handle
654  * at the point of failure.
655  */
656 #if !defined(TARGET_RISCV)
657 void force_sigsegv(int oldsig)
658 {
659     if (oldsig == SIGSEGV) {
660         /* Make sure we don't try to deliver the signal again; this will
661          * end up with handle_pending_signal() calling dump_core_and_abort().
662          */
663         sigact_table[oldsig - 1]._sa_handler = TARGET_SIG_DFL;
664     }
665     force_sig(TARGET_SIGSEGV);
666 }
667 #endif
668 
669 void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr,
670                            MMUAccessType access_type, bool maperr, uintptr_t ra)
671 {
672     const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
673 
674     if (tcg_ops->record_sigsegv) {
675         tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra);
676     }
677 
678     force_sig_fault(TARGET_SIGSEGV,
679                     maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR,
680                     addr);
681     cpu->exception_index = EXCP_INTERRUPT;
682     cpu_loop_exit_restore(cpu, ra);
683 }
684 
685 void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr,
686                           MMUAccessType access_type, uintptr_t ra)
687 {
688     const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
689 
690     if (tcg_ops->record_sigbus) {
691         tcg_ops->record_sigbus(cpu, addr, access_type, ra);
692     }
693 
694     force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr);
695     cpu->exception_index = EXCP_INTERRUPT;
696     cpu_loop_exit_restore(cpu, ra);
697 }
698 
699 /* abort execution with signal */
700 static G_NORETURN
701 void die_with_signal(int host_sig)
702 {
703     struct sigaction act = {
704         .sa_handler = SIG_DFL,
705     };
706 
707     /*
708      * The proper exit code for dying from an uncaught signal is -<signal>.
709      * The kernel doesn't allow exit() or _exit() to pass a negative value.
710      * To get the proper exit code we need to actually die from an uncaught
711      * signal.  Here the default signal handler is installed, we send
712      * the signal and we wait for it to arrive.
713      */
714     sigfillset(&act.sa_mask);
715     sigaction(host_sig, &act, NULL);
716 
717     kill(getpid(), host_sig);
718 
719     /* Make sure the signal isn't masked (reusing the mask inside of act). */
720     sigdelset(&act.sa_mask, host_sig);
721     sigsuspend(&act.sa_mask);
722 
723     /* unreachable */
724     _exit(EXIT_FAILURE);
725 }
726 
727 static G_NORETURN
728 void dump_core_and_abort(CPUArchState *env, int target_sig)
729 {
730     CPUState *cpu = env_cpu(env);
731     TaskState *ts = get_task_state(cpu);
732     int host_sig, core_dumped = 0;
733 
734     /* On exit, undo the remapping of SIGABRT. */
735     if (target_sig == TARGET_SIGABRT) {
736         host_sig = SIGABRT;
737     } else {
738         host_sig = target_to_host_signal(target_sig);
739     }
740     trace_user_dump_core_and_abort(env, target_sig, host_sig);
741     gdb_signalled(env, target_sig);
742 
743     /* dump core if supported by target binary format */
744     if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
745         stop_all_tasks();
746         core_dumped =
747             ((*ts->bprm->core_dump)(target_sig, env) == 0);
748     }
749     if (core_dumped) {
750         /* we already dumped the core of target process, we don't want
751          * a coredump of qemu itself */
752         struct rlimit nodump;
753         getrlimit(RLIMIT_CORE, &nodump);
754         nodump.rlim_cur=0;
755         setrlimit(RLIMIT_CORE, &nodump);
756         (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
757             target_sig, strsignal(host_sig), "core dumped" );
758     }
759 
760     preexit_cleanup(env, 128 + target_sig);
761     die_with_signal(host_sig);
762 }
763 
764 /* queue a signal so that it will be send to the virtual CPU as soon
765    as possible */
766 void queue_signal(CPUArchState *env, int sig, int si_type,
767                   target_siginfo_t *info)
768 {
769     CPUState *cpu = env_cpu(env);
770     TaskState *ts = get_task_state(cpu);
771 
772     trace_user_queue_signal(env, sig);
773 
774     info->si_code = deposit32(info->si_code, 16, 16, si_type);
775 
776     ts->sync_signal.info = *info;
777     ts->sync_signal.pending = sig;
778     /* signal that a new signal is pending */
779     qatomic_set(&ts->signal_pending, 1);
780 }
781 
782 
783 /* Adjust the signal context to rewind out of safe-syscall if we're in it */
784 static inline void rewind_if_in_safe_syscall(void *puc)
785 {
786     host_sigcontext *uc = (host_sigcontext *)puc;
787     uintptr_t pcreg = host_signal_pc(uc);
788 
789     if (pcreg > (uintptr_t)safe_syscall_start
790         && pcreg < (uintptr_t)safe_syscall_end) {
791         host_signal_set_pc(uc, (uintptr_t)safe_syscall_start);
792     }
793 }
794 
795 static G_NORETURN
796 void die_from_signal(siginfo_t *info)
797 {
798     char sigbuf[4], codebuf[12];
799     const char *sig, *code = NULL;
800 
801     switch (info->si_signo) {
802     case SIGSEGV:
803         sig = "SEGV";
804         switch (info->si_code) {
805         case SEGV_MAPERR:
806             code = "MAPERR";
807             break;
808         case SEGV_ACCERR:
809             code = "ACCERR";
810             break;
811         }
812         break;
813     case SIGBUS:
814         sig = "BUS";
815         switch (info->si_code) {
816         case BUS_ADRALN:
817             code = "ADRALN";
818             break;
819         case BUS_ADRERR:
820             code = "ADRERR";
821             break;
822         }
823         break;
824     case SIGILL:
825         sig = "ILL";
826         switch (info->si_code) {
827         case ILL_ILLOPC:
828             code = "ILLOPC";
829             break;
830         case ILL_ILLOPN:
831             code = "ILLOPN";
832             break;
833         case ILL_ILLADR:
834             code = "ILLADR";
835             break;
836         case ILL_PRVOPC:
837             code = "PRVOPC";
838             break;
839         case ILL_PRVREG:
840             code = "PRVREG";
841             break;
842         case ILL_COPROC:
843             code = "COPROC";
844             break;
845         }
846         break;
847     case SIGFPE:
848         sig = "FPE";
849         switch (info->si_code) {
850         case FPE_INTDIV:
851             code = "INTDIV";
852             break;
853         case FPE_INTOVF:
854             code = "INTOVF";
855             break;
856         }
857         break;
858     case SIGTRAP:
859         sig = "TRAP";
860         break;
861     default:
862         snprintf(sigbuf, sizeof(sigbuf), "%d", info->si_signo);
863         sig = sigbuf;
864         break;
865     }
866     if (code == NULL) {
867         snprintf(codebuf, sizeof(sigbuf), "%d", info->si_code);
868         code = codebuf;
869     }
870 
871     error_report("QEMU internal SIG%s {code=%s, addr=%p}",
872                  sig, code, info->si_addr);
873     die_with_signal(info->si_signo);
874 }
875 
876 static void host_sigsegv_handler(CPUState *cpu, siginfo_t *info,
877                                  host_sigcontext *uc)
878 {
879     uintptr_t host_addr = (uintptr_t)info->si_addr;
880     /*
881      * Convert forcefully to guest address space: addresses outside
882      * reserved_va are still valid to report via SEGV_MAPERR.
883      */
884     bool is_valid = h2g_valid(host_addr);
885     abi_ptr guest_addr = h2g_nocheck(host_addr);
886     uintptr_t pc = host_signal_pc(uc);
887     bool is_write = host_signal_write(info, uc);
888     MMUAccessType access_type = adjust_signal_pc(&pc, is_write);
889     bool maperr;
890 
891     /* If this was a write to a TB protected page, restart. */
892     if (is_write
893         && is_valid
894         && info->si_code == SEGV_ACCERR
895         && handle_sigsegv_accerr_write(cpu, host_signal_mask(uc),
896                                        pc, guest_addr)) {
897         return;
898     }
899 
900     /*
901      * If the access was not on behalf of the guest, within the executable
902      * mapping of the generated code buffer, then it is a host bug.
903      */
904     if (access_type != MMU_INST_FETCH
905         && !in_code_gen_buffer((void *)(pc - tcg_splitwx_diff))) {
906         die_from_signal(info);
907     }
908 
909     maperr = true;
910     if (is_valid && info->si_code == SEGV_ACCERR) {
911         /*
912          * With reserved_va, the whole address space is PROT_NONE,
913          * which means that we may get ACCERR when we want MAPERR.
914          */
915         if (page_get_flags(guest_addr) & PAGE_VALID) {
916             maperr = false;
917         } else {
918             info->si_code = SEGV_MAPERR;
919         }
920     }
921 
922     sigprocmask(SIG_SETMASK, host_signal_mask(uc), NULL);
923     cpu_loop_exit_sigsegv(cpu, guest_addr, access_type, maperr, pc);
924 }
925 
926 static uintptr_t host_sigbus_handler(CPUState *cpu, siginfo_t *info,
927                                 host_sigcontext *uc)
928 {
929     uintptr_t pc = host_signal_pc(uc);
930     bool is_write = host_signal_write(info, uc);
931     MMUAccessType access_type = adjust_signal_pc(&pc, is_write);
932 
933     /*
934      * If the access was not on behalf of the guest, within the executable
935      * mapping of the generated code buffer, then it is a host bug.
936      */
937     if (!in_code_gen_buffer((void *)(pc - tcg_splitwx_diff))) {
938         die_from_signal(info);
939     }
940 
941     if (info->si_code == BUS_ADRALN) {
942         uintptr_t host_addr = (uintptr_t)info->si_addr;
943         abi_ptr guest_addr = h2g_nocheck(host_addr);
944 
945         sigprocmask(SIG_SETMASK, host_signal_mask(uc), NULL);
946         cpu_loop_exit_sigbus(cpu, guest_addr, access_type, pc);
947     }
948     return pc;
949 }
950 
951 static void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
952 {
953     CPUState *cpu = thread_cpu;
954     CPUArchState *env = cpu_env(cpu);
955     TaskState *ts = get_task_state(cpu);
956     target_siginfo_t tinfo;
957     host_sigcontext *uc = puc;
958     struct emulated_sigtable *k;
959     int guest_sig;
960     uintptr_t pc = 0;
961     bool sync_sig = false;
962     void *sigmask;
963 
964     /*
965      * Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special
966      * handling wrt signal blocking and unwinding.  Non-spoofed SIGILL,
967      * SIGFPE, SIGTRAP are always host bugs.
968      */
969     if (info->si_code > 0) {
970         switch (host_sig) {
971         case SIGSEGV:
972             /* Only returns on handle_sigsegv_accerr_write success. */
973             host_sigsegv_handler(cpu, info, uc);
974             return;
975         case SIGBUS:
976             pc = host_sigbus_handler(cpu, info, uc);
977             sync_sig = true;
978             break;
979         case SIGILL:
980         case SIGFPE:
981         case SIGTRAP:
982             die_from_signal(info);
983         }
984     }
985 
986     /* get target signal number */
987     guest_sig = host_to_target_signal(host_sig);
988     if (guest_sig < 1 || guest_sig > TARGET_NSIG) {
989         return;
990     }
991     trace_user_host_signal(env, host_sig, guest_sig);
992 
993     host_to_target_siginfo_noswap(&tinfo, info);
994     k = &ts->sigtab[guest_sig - 1];
995     k->info = tinfo;
996     k->pending = guest_sig;
997     ts->signal_pending = 1;
998 
999     /*
1000      * For synchronous signals, unwind the cpu state to the faulting
1001      * insn and then exit back to the main loop so that the signal
1002      * is delivered immediately.
1003      */
1004     if (sync_sig) {
1005         cpu->exception_index = EXCP_INTERRUPT;
1006         cpu_loop_exit_restore(cpu, pc);
1007     }
1008 
1009     rewind_if_in_safe_syscall(puc);
1010 
1011     /*
1012      * Block host signals until target signal handler entered. We
1013      * can't block SIGSEGV or SIGBUS while we're executing guest
1014      * code in case the guest code provokes one in the window between
1015      * now and it getting out to the main loop. Signals will be
1016      * unblocked again in process_pending_signals().
1017      *
1018      * WARNING: we cannot use sigfillset() here because the sigmask
1019      * field is a kernel sigset_t, which is much smaller than the
1020      * libc sigset_t which sigfillset() operates on. Using sigfillset()
1021      * would write 0xff bytes off the end of the structure and trash
1022      * data on the struct.
1023      */
1024     sigmask = host_signal_mask(uc);
1025     memset(sigmask, 0xff, SIGSET_T_SIZE);
1026     sigdelset(sigmask, SIGSEGV);
1027     sigdelset(sigmask, SIGBUS);
1028 
1029     /* interrupt the virtual CPU as soon as possible */
1030     cpu_exit(thread_cpu);
1031 }
1032 
1033 /* do_sigaltstack() returns target values and errnos. */
1034 /* compare linux/kernel/signal.c:do_sigaltstack() */
1035 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr,
1036                         CPUArchState *env)
1037 {
1038     target_stack_t oss, *uoss = NULL;
1039     abi_long ret = -TARGET_EFAULT;
1040 
1041     if (uoss_addr) {
1042         /* Verify writability now, but do not alter user memory yet. */
1043         if (!lock_user_struct(VERIFY_WRITE, uoss, uoss_addr, 0)) {
1044             goto out;
1045         }
1046         target_save_altstack(&oss, env);
1047     }
1048 
1049     if (uss_addr) {
1050         target_stack_t *uss;
1051 
1052         if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
1053             goto out;
1054         }
1055         ret = target_restore_altstack(uss, env);
1056         if (ret) {
1057             goto out;
1058         }
1059     }
1060 
1061     if (uoss_addr) {
1062         memcpy(uoss, &oss, sizeof(oss));
1063         unlock_user_struct(uoss, uoss_addr, 1);
1064         uoss = NULL;
1065     }
1066     ret = 0;
1067 
1068  out:
1069     if (uoss) {
1070         unlock_user_struct(uoss, uoss_addr, 0);
1071     }
1072     return ret;
1073 }
1074 
1075 /* do_sigaction() return target values and host errnos */
1076 int do_sigaction(int sig, const struct target_sigaction *act,
1077                  struct target_sigaction *oact, abi_ulong ka_restorer)
1078 {
1079     struct target_sigaction *k;
1080     int host_sig;
1081     int ret = 0;
1082 
1083     trace_signal_do_sigaction_guest(sig, TARGET_NSIG);
1084 
1085     if (sig < 1 || sig > TARGET_NSIG) {
1086         return -TARGET_EINVAL;
1087     }
1088 
1089     if (act && (sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)) {
1090         return -TARGET_EINVAL;
1091     }
1092 
1093     if (block_signals()) {
1094         return -QEMU_ERESTARTSYS;
1095     }
1096 
1097     k = &sigact_table[sig - 1];
1098     if (oact) {
1099         __put_user(k->_sa_handler, &oact->_sa_handler);
1100         __put_user(k->sa_flags, &oact->sa_flags);
1101 #ifdef TARGET_ARCH_HAS_SA_RESTORER
1102         __put_user(k->sa_restorer, &oact->sa_restorer);
1103 #endif
1104         /* Not swapped.  */
1105         oact->sa_mask = k->sa_mask;
1106     }
1107     if (act) {
1108         __get_user(k->_sa_handler, &act->_sa_handler);
1109         __get_user(k->sa_flags, &act->sa_flags);
1110 #ifdef TARGET_ARCH_HAS_SA_RESTORER
1111         __get_user(k->sa_restorer, &act->sa_restorer);
1112 #endif
1113 #ifdef TARGET_ARCH_HAS_KA_RESTORER
1114         k->ka_restorer = ka_restorer;
1115 #endif
1116         /* To be swapped in target_to_host_sigset.  */
1117         k->sa_mask = act->sa_mask;
1118 
1119         /* we update the host linux signal state */
1120         host_sig = target_to_host_signal(sig);
1121         trace_signal_do_sigaction_host(host_sig, TARGET_NSIG);
1122         if (host_sig > SIGRTMAX) {
1123             /* we don't have enough host signals to map all target signals */
1124             qemu_log_mask(LOG_UNIMP, "Unsupported target signal #%d, ignored\n",
1125                           sig);
1126             /*
1127              * we don't return an error here because some programs try to
1128              * register an handler for all possible rt signals even if they
1129              * don't need it.
1130              * An error here can abort them whereas there can be no problem
1131              * to not have the signal available later.
1132              * This is the case for golang,
1133              *   See https://github.com/golang/go/issues/33746
1134              * So we silently ignore the error.
1135              */
1136             return 0;
1137         }
1138         if (host_sig != SIGSEGV && host_sig != SIGBUS) {
1139             struct sigaction act1;
1140 
1141             sigfillset(&act1.sa_mask);
1142             act1.sa_flags = SA_SIGINFO;
1143             if (k->_sa_handler == TARGET_SIG_IGN) {
1144                 /*
1145                  * It is important to update the host kernel signal ignore
1146                  * state to avoid getting unexpected interrupted syscalls.
1147                  */
1148                 act1.sa_sigaction = (void *)SIG_IGN;
1149             } else if (k->_sa_handler == TARGET_SIG_DFL) {
1150                 if (core_dump_signal(sig)) {
1151                     act1.sa_sigaction = host_signal_handler;
1152                 } else {
1153                     act1.sa_sigaction = (void *)SIG_DFL;
1154                 }
1155             } else {
1156                 act1.sa_sigaction = host_signal_handler;
1157                 if (k->sa_flags & TARGET_SA_RESTART) {
1158                     act1.sa_flags |= SA_RESTART;
1159                 }
1160             }
1161             ret = sigaction(host_sig, &act1, NULL);
1162         }
1163     }
1164     return ret;
1165 }
1166 
1167 static void handle_pending_signal(CPUArchState *cpu_env, int sig,
1168                                   struct emulated_sigtable *k)
1169 {
1170     CPUState *cpu = env_cpu(cpu_env);
1171     abi_ulong handler;
1172     sigset_t set;
1173     target_sigset_t target_old_set;
1174     struct target_sigaction *sa;
1175     TaskState *ts = get_task_state(cpu);
1176 
1177     trace_user_handle_signal(cpu_env, sig);
1178     /* dequeue signal */
1179     k->pending = 0;
1180 
1181     sig = gdb_handlesig(cpu, sig, NULL);
1182     if (!sig) {
1183         sa = NULL;
1184         handler = TARGET_SIG_IGN;
1185     } else {
1186         sa = &sigact_table[sig - 1];
1187         handler = sa->_sa_handler;
1188     }
1189 
1190     if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
1191         print_taken_signal(sig, &k->info);
1192     }
1193 
1194     if (handler == TARGET_SIG_DFL) {
1195         /* default handler : ignore some signal. The other are job control or fatal */
1196         if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
1197             kill(getpid(),SIGSTOP);
1198         } else if (sig != TARGET_SIGCHLD &&
1199                    sig != TARGET_SIGURG &&
1200                    sig != TARGET_SIGWINCH &&
1201                    sig != TARGET_SIGCONT) {
1202             dump_core_and_abort(cpu_env, sig);
1203         }
1204     } else if (handler == TARGET_SIG_IGN) {
1205         /* ignore sig */
1206     } else if (handler == TARGET_SIG_ERR) {
1207         dump_core_and_abort(cpu_env, sig);
1208     } else {
1209         /* compute the blocked signals during the handler execution */
1210         sigset_t *blocked_set;
1211 
1212         target_to_host_sigset(&set, &sa->sa_mask);
1213         /* SA_NODEFER indicates that the current signal should not be
1214            blocked during the handler */
1215         if (!(sa->sa_flags & TARGET_SA_NODEFER))
1216             sigaddset(&set, target_to_host_signal(sig));
1217 
1218         /* save the previous blocked signal state to restore it at the
1219            end of the signal execution (see do_sigreturn) */
1220         host_to_target_sigset_internal(&target_old_set, &ts->signal_mask);
1221 
1222         /* block signals in the handler */
1223         blocked_set = ts->in_sigsuspend ?
1224             &ts->sigsuspend_mask : &ts->signal_mask;
1225         sigorset(&ts->signal_mask, blocked_set, &set);
1226         ts->in_sigsuspend = 0;
1227 
1228         /* if the CPU is in VM86 mode, we restore the 32 bit values */
1229 #if defined(TARGET_I386) && !defined(TARGET_X86_64)
1230         {
1231             CPUX86State *env = cpu_env;
1232             if (env->eflags & VM_MASK)
1233                 save_v86_state(env);
1234         }
1235 #endif
1236         /* prepare the stack frame of the virtual CPU */
1237 #if defined(TARGET_ARCH_HAS_SETUP_FRAME)
1238         if (sa->sa_flags & TARGET_SA_SIGINFO) {
1239             setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
1240         } else {
1241             setup_frame(sig, sa, &target_old_set, cpu_env);
1242         }
1243 #else
1244         /* These targets do not have traditional signals.  */
1245         setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
1246 #endif
1247         if (sa->sa_flags & TARGET_SA_RESETHAND) {
1248             sa->_sa_handler = TARGET_SIG_DFL;
1249         }
1250     }
1251 }
1252 
1253 void process_pending_signals(CPUArchState *cpu_env)
1254 {
1255     CPUState *cpu = env_cpu(cpu_env);
1256     int sig;
1257     TaskState *ts = get_task_state(cpu);
1258     sigset_t set;
1259     sigset_t *blocked_set;
1260 
1261     while (qatomic_read(&ts->signal_pending)) {
1262         sigfillset(&set);
1263         sigprocmask(SIG_SETMASK, &set, 0);
1264 
1265     restart_scan:
1266         sig = ts->sync_signal.pending;
1267         if (sig) {
1268             /* Synchronous signals are forced,
1269              * see force_sig_info() and callers in Linux
1270              * Note that not all of our queue_signal() calls in QEMU correspond
1271              * to force_sig_info() calls in Linux (some are send_sig_info()).
1272              * However it seems like a kernel bug to me to allow the process
1273              * to block a synchronous signal since it could then just end up
1274              * looping round and round indefinitely.
1275              */
1276             if (sigismember(&ts->signal_mask, target_to_host_signal_table[sig])
1277                 || sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) {
1278                 sigdelset(&ts->signal_mask, target_to_host_signal_table[sig]);
1279                 sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL;
1280             }
1281 
1282             handle_pending_signal(cpu_env, sig, &ts->sync_signal);
1283         }
1284 
1285         for (sig = 1; sig <= TARGET_NSIG; sig++) {
1286             blocked_set = ts->in_sigsuspend ?
1287                 &ts->sigsuspend_mask : &ts->signal_mask;
1288 
1289             if (ts->sigtab[sig - 1].pending &&
1290                 (!sigismember(blocked_set,
1291                               target_to_host_signal_table[sig]))) {
1292                 handle_pending_signal(cpu_env, sig, &ts->sigtab[sig - 1]);
1293                 /* Restart scan from the beginning, as handle_pending_signal
1294                  * might have resulted in a new synchronous signal (eg SIGSEGV).
1295                  */
1296                 goto restart_scan;
1297             }
1298         }
1299 
1300         /* if no signal is pending, unblock signals and recheck (the act
1301          * of unblocking might cause us to take another host signal which
1302          * will set signal_pending again).
1303          */
1304         qatomic_set(&ts->signal_pending, 0);
1305         ts->in_sigsuspend = 0;
1306         set = ts->signal_mask;
1307         sigdelset(&set, SIGSEGV);
1308         sigdelset(&set, SIGBUS);
1309         sigprocmask(SIG_SETMASK, &set, 0);
1310     }
1311     ts->in_sigsuspend = 0;
1312 }
1313 
1314 int process_sigsuspend_mask(sigset_t **pset, target_ulong sigset,
1315                             target_ulong sigsize)
1316 {
1317     TaskState *ts = get_task_state(thread_cpu);
1318     sigset_t *host_set = &ts->sigsuspend_mask;
1319     target_sigset_t *target_sigset;
1320 
1321     if (sigsize != sizeof(*target_sigset)) {
1322         /* Like the kernel, we enforce correct size sigsets */
1323         return -TARGET_EINVAL;
1324     }
1325 
1326     target_sigset = lock_user(VERIFY_READ, sigset, sigsize, 1);
1327     if (!target_sigset) {
1328         return -TARGET_EFAULT;
1329     }
1330     target_to_host_sigset(host_set, target_sigset);
1331     unlock_user(target_sigset, sigset, 0);
1332 
1333     *pset = host_set;
1334     return 0;
1335 }
1336