184778508Sblueswir1 /*
284778508Sblueswir1 * Emulation of BSD signals
384778508Sblueswir1 *
484778508Sblueswir1 * Copyright (c) 2003 - 2008 Fabrice Bellard
51366ef81SWarner Losh * Copyright (c) 2013 Stacey Son
684778508Sblueswir1 *
784778508Sblueswir1 * This program is free software; you can redistribute it and/or modify
884778508Sblueswir1 * it under the terms of the GNU General Public License as published by
984778508Sblueswir1 * the Free Software Foundation; either version 2 of the License, or
1084778508Sblueswir1 * (at your option) any later version.
1184778508Sblueswir1 *
1284778508Sblueswir1 * This program is distributed in the hope that it will be useful,
1384778508Sblueswir1 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1484778508Sblueswir1 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1584778508Sblueswir1 * GNU General Public License for more details.
1684778508Sblueswir1 *
1784778508Sblueswir1 * You should have received a copy of the GNU General Public License
188167ee88SBlue Swirl * along with this program; if not, see <http://www.gnu.org/licenses/>.
1984778508Sblueswir1 */
2084778508Sblueswir1
215abfac27SWarner Losh #include "qemu/osdep.h"
22cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h"
2384778508Sblueswir1 #include "qemu.h"
2474781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h"
2582723866SPhilippe Mathieu-Daudé #include "user/tswap-target.h"
26d96bf49bSAlex Bennée #include "gdbstub/user.h"
270ef59989SWarner Losh #include "signal-common.h"
286ddc1abeSWarner Losh #include "trace.h"
29fc9f9bddSWarner Losh #include "hw/core/tcg-cpu-ops.h"
3085fc1b5dSWarner Losh #include "host-signal.h"
3184778508Sblueswir1
32f84e313eSGustavo Romero /* target_siginfo_t must fit in gdbstub's siginfo save area. */
33f84e313eSGustavo Romero QEMU_BUILD_BUG_ON(sizeof(target_siginfo_t) > MAX_SIGINFO_LENGTH);
34f84e313eSGustavo Romero
35149076adSWarner Losh static struct target_sigaction sigact_table[TARGET_NSIG];
36149076adSWarner Losh static void host_signal_handler(int host_sig, siginfo_t *info, void *puc);
37c93cbac1SWarner Losh static void target_to_host_sigset_internal(sigset_t *d,
38c93cbac1SWarner Losh const target_sigset_t *s);
39c93cbac1SWarner Losh
on_sig_stack(TaskState * ts,unsigned long sp)4046f4f76dSWarner Losh static inline int on_sig_stack(TaskState *ts, unsigned long sp)
4146f4f76dSWarner Losh {
4246f4f76dSWarner Losh return sp - ts->sigaltstack_used.ss_sp < ts->sigaltstack_used.ss_size;
4346f4f76dSWarner Losh }
4446f4f76dSWarner Losh
sas_ss_flags(TaskState * ts,unsigned long sp)4546f4f76dSWarner Losh static inline int sas_ss_flags(TaskState *ts, unsigned long sp)
4646f4f76dSWarner Losh {
4746f4f76dSWarner Losh return ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE :
4846f4f76dSWarner Losh on_sig_stack(ts, sp) ? SS_ONSTACK : 0;
4946f4f76dSWarner Losh }
50149076adSWarner Losh
51835b04edSWarner Losh /*
52944399ffSMichael Tokarev * The BSD ABIs use the same signal numbers across all the CPU architectures, so
531366ef81SWarner Losh * (unlike Linux) these functions are just the identity mapping. This might not
541366ef81SWarner Losh * be true for XyzBSD running on AbcBSD, which doesn't currently work.
551366ef81SWarner Losh */
host_to_target_signal(int sig)561366ef81SWarner Losh int host_to_target_signal(int sig)
571366ef81SWarner Losh {
581366ef81SWarner Losh return sig;
591366ef81SWarner Losh }
601366ef81SWarner Losh
target_to_host_signal(int sig)611366ef81SWarner Losh int target_to_host_signal(int sig)
621366ef81SWarner Losh {
631366ef81SWarner Losh return sig;
641366ef81SWarner Losh }
651366ef81SWarner Losh
target_sigemptyset(target_sigset_t * set)66c93cbac1SWarner Losh static inline void target_sigemptyset(target_sigset_t *set)
67c93cbac1SWarner Losh {
68c93cbac1SWarner Losh memset(set, 0, sizeof(*set));
69c93cbac1SWarner Losh }
70c93cbac1SWarner Losh
target_sigaddset(target_sigset_t * set,int signum)71c93cbac1SWarner Losh static inline void target_sigaddset(target_sigset_t *set, int signum)
72c93cbac1SWarner Losh {
73c93cbac1SWarner Losh signum--;
74c93cbac1SWarner Losh uint32_t mask = (uint32_t)1 << (signum % TARGET_NSIG_BPW);
75c93cbac1SWarner Losh set->__bits[signum / TARGET_NSIG_BPW] |= mask;
76c93cbac1SWarner Losh }
77c93cbac1SWarner Losh
target_sigismember(const target_sigset_t * set,int signum)78c93cbac1SWarner Losh static inline int target_sigismember(const target_sigset_t *set, int signum)
79c93cbac1SWarner Losh {
80c93cbac1SWarner Losh signum--;
81c93cbac1SWarner Losh abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
82c93cbac1SWarner Losh return (set->__bits[signum / TARGET_NSIG_BPW] & mask) != 0;
83c93cbac1SWarner Losh }
84c93cbac1SWarner Losh
85aae57ac3SWarner Losh /* Adjust the signal context to rewind out of safe-syscall if we're in it */
rewind_if_in_safe_syscall(void * puc)86aae57ac3SWarner Losh static inline void rewind_if_in_safe_syscall(void *puc)
87aae57ac3SWarner Losh {
88aae57ac3SWarner Losh ucontext_t *uc = (ucontext_t *)puc;
89aae57ac3SWarner Losh uintptr_t pcreg = host_signal_pc(uc);
90aae57ac3SWarner Losh
91aae57ac3SWarner Losh if (pcreg > (uintptr_t)safe_syscall_start
92aae57ac3SWarner Losh && pcreg < (uintptr_t)safe_syscall_end) {
93aae57ac3SWarner Losh host_signal_set_pc(uc, (uintptr_t)safe_syscall_start);
94aae57ac3SWarner Losh }
95aae57ac3SWarner Losh }
96aae57ac3SWarner Losh
97c93cbac1SWarner Losh /*
98c93cbac1SWarner Losh * Note: The following take advantage of the BSD signal property that all
99c93cbac1SWarner Losh * signals are available on all architectures.
100c93cbac1SWarner Losh */
host_to_target_sigset_internal(target_sigset_t * d,const sigset_t * s)101c93cbac1SWarner Losh static void host_to_target_sigset_internal(target_sigset_t *d,
102c93cbac1SWarner Losh const sigset_t *s)
103c93cbac1SWarner Losh {
104c93cbac1SWarner Losh int i;
105c93cbac1SWarner Losh
106c93cbac1SWarner Losh target_sigemptyset(d);
107c93cbac1SWarner Losh for (i = 1; i <= NSIG; i++) {
108c93cbac1SWarner Losh if (sigismember(s, i)) {
109c93cbac1SWarner Losh target_sigaddset(d, host_to_target_signal(i));
110c93cbac1SWarner Losh }
111c93cbac1SWarner Losh }
112c93cbac1SWarner Losh }
113c93cbac1SWarner Losh
host_to_target_sigset(target_sigset_t * d,const sigset_t * s)114c93cbac1SWarner Losh void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
115c93cbac1SWarner Losh {
116c93cbac1SWarner Losh target_sigset_t d1;
117c93cbac1SWarner Losh int i;
118c93cbac1SWarner Losh
119c93cbac1SWarner Losh host_to_target_sigset_internal(&d1, s);
120c93cbac1SWarner Losh for (i = 0; i < _SIG_WORDS; i++) {
121c93cbac1SWarner Losh d->__bits[i] = tswap32(d1.__bits[i]);
122c93cbac1SWarner Losh }
123c93cbac1SWarner Losh }
124c93cbac1SWarner Losh
target_to_host_sigset_internal(sigset_t * d,const target_sigset_t * s)125c93cbac1SWarner Losh static void target_to_host_sigset_internal(sigset_t *d,
126c93cbac1SWarner Losh const target_sigset_t *s)
127c93cbac1SWarner Losh {
128c93cbac1SWarner Losh int i;
129c93cbac1SWarner Losh
130c93cbac1SWarner Losh sigemptyset(d);
131c93cbac1SWarner Losh for (i = 1; i <= TARGET_NSIG; i++) {
132c93cbac1SWarner Losh if (target_sigismember(s, i)) {
133c93cbac1SWarner Losh sigaddset(d, target_to_host_signal(i));
134c93cbac1SWarner Losh }
135c93cbac1SWarner Losh }
136c93cbac1SWarner Losh }
137c93cbac1SWarner Losh
target_to_host_sigset(sigset_t * d,const target_sigset_t * s)138c93cbac1SWarner Losh void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
139c93cbac1SWarner Losh {
140c93cbac1SWarner Losh target_sigset_t s1;
141c93cbac1SWarner Losh int i;
142c93cbac1SWarner Losh
143c93cbac1SWarner Losh for (i = 0; i < TARGET_NSIG_WORDS; i++) {
144c93cbac1SWarner Losh s1.__bits[i] = tswap32(s->__bits[i]);
145c93cbac1SWarner Losh }
146c93cbac1SWarner Losh target_to_host_sigset_internal(d, &s1);
147c93cbac1SWarner Losh }
148c93cbac1SWarner Losh
has_trapno(int tsig)149c34f2aafSWarner Losh static bool has_trapno(int tsig)
150c34f2aafSWarner Losh {
151c34f2aafSWarner Losh return tsig == TARGET_SIGILL ||
152c34f2aafSWarner Losh tsig == TARGET_SIGFPE ||
153c34f2aafSWarner Losh tsig == TARGET_SIGSEGV ||
154c34f2aafSWarner Losh tsig == TARGET_SIGBUS ||
155c34f2aafSWarner Losh tsig == TARGET_SIGTRAP;
156c34f2aafSWarner Losh }
157c34f2aafSWarner Losh
158c34f2aafSWarner Losh /* Siginfo conversion. */
159c34f2aafSWarner Losh
160c34f2aafSWarner Losh /*
161c34f2aafSWarner Losh * Populate tinfo w/o swapping based on guessing which fields are valid.
162c34f2aafSWarner Losh */
host_to_target_siginfo_noswap(target_siginfo_t * tinfo,const siginfo_t * info)163c34f2aafSWarner Losh static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
164c34f2aafSWarner Losh const siginfo_t *info)
165c34f2aafSWarner Losh {
166c34f2aafSWarner Losh int sig = host_to_target_signal(info->si_signo);
167c34f2aafSWarner Losh int si_code = info->si_code;
168c34f2aafSWarner Losh int si_type;
169c34f2aafSWarner Losh
170c34f2aafSWarner Losh /*
171c34f2aafSWarner Losh * Make sure we that the variable portion of the target siginfo is zeroed
172c34f2aafSWarner Losh * out so we don't leak anything into that.
173c34f2aafSWarner Losh */
174c34f2aafSWarner Losh memset(&tinfo->_reason, 0, sizeof(tinfo->_reason));
175c34f2aafSWarner Losh
176c34f2aafSWarner Losh /*
177c34f2aafSWarner Losh * This is awkward, because we have to use a combination of the si_code and
178c34f2aafSWarner Losh * si_signo to figure out which of the union's members are valid.o We
179c34f2aafSWarner Losh * therefore make our best guess.
180c34f2aafSWarner Losh *
181c34f2aafSWarner Losh * Once we have made our guess, we record it in the top 16 bits of
182c34f2aafSWarner Losh * the si_code, so that tswap_siginfo() later can use it.
183c34f2aafSWarner Losh * tswap_siginfo() will strip these top bits out before writing
184c34f2aafSWarner Losh * si_code to the guest (sign-extending the lower bits).
185c34f2aafSWarner Losh */
186c34f2aafSWarner Losh tinfo->si_signo = sig;
187c34f2aafSWarner Losh tinfo->si_errno = info->si_errno;
188c34f2aafSWarner Losh tinfo->si_code = info->si_code;
189c34f2aafSWarner Losh tinfo->si_pid = info->si_pid;
190c34f2aafSWarner Losh tinfo->si_uid = info->si_uid;
191c34f2aafSWarner Losh tinfo->si_status = info->si_status;
192c34f2aafSWarner Losh tinfo->si_addr = (abi_ulong)(unsigned long)info->si_addr;
193c34f2aafSWarner Losh /*
194c34f2aafSWarner Losh * si_value is opaque to kernel. On all FreeBSD platforms,
195c34f2aafSWarner Losh * sizeof(sival_ptr) >= sizeof(sival_int) so the following
196c34f2aafSWarner Losh * always will copy the larger element.
197c34f2aafSWarner Losh */
198c34f2aafSWarner Losh tinfo->si_value.sival_ptr =
199c34f2aafSWarner Losh (abi_ulong)(unsigned long)info->si_value.sival_ptr;
200c34f2aafSWarner Losh
201c34f2aafSWarner Losh switch (si_code) {
202c34f2aafSWarner Losh /*
203c34f2aafSWarner Losh * All the SI_xxx codes that are defined here are global to
204c34f2aafSWarner Losh * all the signals (they have values that none of the other,
205c34f2aafSWarner Losh * more specific signal info will set).
206c34f2aafSWarner Losh */
207c34f2aafSWarner Losh case SI_USER:
208c34f2aafSWarner Losh case SI_LWP:
209c34f2aafSWarner Losh case SI_KERNEL:
210c34f2aafSWarner Losh case SI_QUEUE:
211c34f2aafSWarner Losh case SI_ASYNCIO:
212c34f2aafSWarner Losh /*
213c34f2aafSWarner Losh * Only the fixed parts are valid (though FreeBSD doesn't always
214c34f2aafSWarner Losh * set all the fields to non-zero values.
215c34f2aafSWarner Losh */
216c34f2aafSWarner Losh si_type = QEMU_SI_NOINFO;
217c34f2aafSWarner Losh break;
218c34f2aafSWarner Losh case SI_TIMER:
219c34f2aafSWarner Losh tinfo->_reason._timer._timerid = info->_reason._timer._timerid;
220c34f2aafSWarner Losh tinfo->_reason._timer._overrun = info->_reason._timer._overrun;
221c34f2aafSWarner Losh si_type = QEMU_SI_TIMER;
222c34f2aafSWarner Losh break;
223c34f2aafSWarner Losh case SI_MESGQ:
224c34f2aafSWarner Losh tinfo->_reason._mesgq._mqd = info->_reason._mesgq._mqd;
225c34f2aafSWarner Losh si_type = QEMU_SI_MESGQ;
226c34f2aafSWarner Losh break;
227c34f2aafSWarner Losh default:
228c34f2aafSWarner Losh /*
229c34f2aafSWarner Losh * We have to go based on the signal number now to figure out
230c34f2aafSWarner Losh * what's valid.
231c34f2aafSWarner Losh */
232eb9d35f6SWarner Losh si_type = QEMU_SI_NOINFO;
233c34f2aafSWarner Losh if (has_trapno(sig)) {
234c34f2aafSWarner Losh tinfo->_reason._fault._trapno = info->_reason._fault._trapno;
235c34f2aafSWarner Losh si_type = QEMU_SI_FAULT;
236c34f2aafSWarner Losh }
237c34f2aafSWarner Losh #ifdef TARGET_SIGPOLL
238c34f2aafSWarner Losh /*
239c34f2aafSWarner Losh * FreeBSD never had SIGPOLL, but emulates it for Linux so there's
240c34f2aafSWarner Losh * a chance it may popup in the future.
241c34f2aafSWarner Losh */
242c34f2aafSWarner Losh if (sig == TARGET_SIGPOLL) {
243c34f2aafSWarner Losh tinfo->_reason._poll._band = info->_reason._poll._band;
244c34f2aafSWarner Losh si_type = QEMU_SI_POLL;
245c34f2aafSWarner Losh }
246c34f2aafSWarner Losh #endif
247c34f2aafSWarner Losh /*
248c34f2aafSWarner Losh * Unsure that this can actually be generated, and our support for
249944399ffSMichael Tokarev * capsicum is somewhere between weak and non-existent, but if we get
250c34f2aafSWarner Losh * one, then we know what to save.
251c34f2aafSWarner Losh */
252eb9d35f6SWarner Losh #ifdef QEMU_SI_CAPSICUM
253c34f2aafSWarner Losh if (sig == TARGET_SIGTRAP) {
254c34f2aafSWarner Losh tinfo->_reason._capsicum._syscall =
255c34f2aafSWarner Losh info->_reason._capsicum._syscall;
256c34f2aafSWarner Losh si_type = QEMU_SI_CAPSICUM;
257c34f2aafSWarner Losh }
258eb9d35f6SWarner Losh #endif
259c34f2aafSWarner Losh break;
260c34f2aafSWarner Losh }
261c34f2aafSWarner Losh tinfo->si_code = deposit32(si_code, 24, 8, si_type);
262c34f2aafSWarner Losh }
263c34f2aafSWarner Losh
tswap_siginfo(target_siginfo_t * tinfo,const target_siginfo_t * info)26408eb66d5SWarner Losh static void tswap_siginfo(target_siginfo_t *tinfo, const target_siginfo_t *info)
26508eb66d5SWarner Losh {
26608eb66d5SWarner Losh int si_type = extract32(info->si_code, 24, 8);
26708eb66d5SWarner Losh int si_code = sextract32(info->si_code, 0, 24);
26808eb66d5SWarner Losh
26908eb66d5SWarner Losh __put_user(info->si_signo, &tinfo->si_signo);
27008eb66d5SWarner Losh __put_user(info->si_errno, &tinfo->si_errno);
27108eb66d5SWarner Losh __put_user(si_code, &tinfo->si_code); /* Zero out si_type, it's internal */
27208eb66d5SWarner Losh __put_user(info->si_pid, &tinfo->si_pid);
27308eb66d5SWarner Losh __put_user(info->si_uid, &tinfo->si_uid);
27408eb66d5SWarner Losh __put_user(info->si_status, &tinfo->si_status);
27508eb66d5SWarner Losh __put_user(info->si_addr, &tinfo->si_addr);
27608eb66d5SWarner Losh /*
27708eb66d5SWarner Losh * Unswapped, because we passed it through mostly untouched. si_value is
27808eb66d5SWarner Losh * opaque to the kernel, so we didn't bother with potentially wasting cycles
27908eb66d5SWarner Losh * to swap it into host byte order.
28008eb66d5SWarner Losh */
28108eb66d5SWarner Losh tinfo->si_value.sival_ptr = info->si_value.sival_ptr;
28208eb66d5SWarner Losh
28308eb66d5SWarner Losh /*
28408eb66d5SWarner Losh * We can use our internal marker of which fields in the structure
28508eb66d5SWarner Losh * are valid, rather than duplicating the guesswork of
28608eb66d5SWarner Losh * host_to_target_siginfo_noswap() here.
28708eb66d5SWarner Losh */
28808eb66d5SWarner Losh switch (si_type) {
28908eb66d5SWarner Losh case QEMU_SI_NOINFO: /* No additional info */
29008eb66d5SWarner Losh break;
29108eb66d5SWarner Losh case QEMU_SI_FAULT:
29208eb66d5SWarner Losh __put_user(info->_reason._fault._trapno,
29308eb66d5SWarner Losh &tinfo->_reason._fault._trapno);
29408eb66d5SWarner Losh break;
29508eb66d5SWarner Losh case QEMU_SI_TIMER:
29608eb66d5SWarner Losh __put_user(info->_reason._timer._timerid,
29708eb66d5SWarner Losh &tinfo->_reason._timer._timerid);
29808eb66d5SWarner Losh __put_user(info->_reason._timer._overrun,
29908eb66d5SWarner Losh &tinfo->_reason._timer._overrun);
30008eb66d5SWarner Losh break;
30108eb66d5SWarner Losh case QEMU_SI_MESGQ:
30208eb66d5SWarner Losh __put_user(info->_reason._mesgq._mqd, &tinfo->_reason._mesgq._mqd);
30308eb66d5SWarner Losh break;
30408eb66d5SWarner Losh case QEMU_SI_POLL:
30508eb66d5SWarner Losh /* Note: Not generated on FreeBSD */
30608eb66d5SWarner Losh __put_user(info->_reason._poll._band, &tinfo->_reason._poll._band);
30708eb66d5SWarner Losh break;
308eb9d35f6SWarner Losh #ifdef QEMU_SI_CAPSICUM
30908eb66d5SWarner Losh case QEMU_SI_CAPSICUM:
31008eb66d5SWarner Losh __put_user(info->_reason._capsicum._syscall,
31108eb66d5SWarner Losh &tinfo->_reason._capsicum._syscall);
31208eb66d5SWarner Losh break;
313eb9d35f6SWarner Losh #endif
31408eb66d5SWarner Losh default:
31508eb66d5SWarner Losh g_assert_not_reached();
31608eb66d5SWarner Losh }
31708eb66d5SWarner Losh }
31808eb66d5SWarner Losh
host_to_target_siginfo(target_siginfo_t * tinfo,const siginfo_t * info)3193f254cf2SStacey Son void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
3203f254cf2SStacey Son {
3213f254cf2SStacey Son host_to_target_siginfo_noswap(tinfo, info);
3223f254cf2SStacey Son tswap_siginfo(tinfo, tinfo);
3233f254cf2SStacey Son }
3243f254cf2SStacey Son
block_signals(void)325394cf694SWarner Losh int block_signals(void)
326394cf694SWarner Losh {
327e4e5cb4aSIlya Leoshkevich TaskState *ts = get_task_state(thread_cpu);
328394cf694SWarner Losh sigset_t set;
329394cf694SWarner Losh
330394cf694SWarner Losh /*
331394cf694SWarner Losh * It's OK to block everything including SIGSEGV, because we won't run any
332394cf694SWarner Losh * further guest code before unblocking signals in
333944399ffSMichael Tokarev * process_pending_signals(). We depend on the FreeBSD behavior here where
334394cf694SWarner Losh * this will only affect this thread's signal mask. We don't use
335394cf694SWarner Losh * pthread_sigmask which might seem more correct because that routine also
336394cf694SWarner Losh * does odd things with SIGCANCEL to implement pthread_cancel().
337394cf694SWarner Losh */
338394cf694SWarner Losh sigfillset(&set);
339394cf694SWarner Losh sigprocmask(SIG_SETMASK, &set, 0);
340394cf694SWarner Losh
341394cf694SWarner Losh return qatomic_xchg(&ts->signal_pending, 1);
342394cf694SWarner Losh }
343394cf694SWarner Losh
34437714547SWarner Losh /* Returns 1 if given signal should dump core if not handled. */
core_dump_signal(int sig)34537714547SWarner Losh static int core_dump_signal(int sig)
34637714547SWarner Losh {
34737714547SWarner Losh switch (sig) {
34837714547SWarner Losh case TARGET_SIGABRT:
34937714547SWarner Losh case TARGET_SIGFPE:
35037714547SWarner Losh case TARGET_SIGILL:
35137714547SWarner Losh case TARGET_SIGQUIT:
35237714547SWarner Losh case TARGET_SIGSEGV:
35337714547SWarner Losh case TARGET_SIGTRAP:
35437714547SWarner Losh case TARGET_SIGBUS:
35537714547SWarner Losh return 1;
35637714547SWarner Losh default:
35737714547SWarner Losh return 0;
35837714547SWarner Losh }
35937714547SWarner Losh }
36037714547SWarner Losh
36137714547SWarner Losh /* Abort execution with signal. */
3628905770bSMarc-André Lureau static G_NORETURN
dump_core_and_abort(int target_sig)3638905770bSMarc-André Lureau void dump_core_and_abort(int target_sig)
36437714547SWarner Losh {
365b77af26eSRichard Henderson CPUState *cpu = thread_cpu;
366b77af26eSRichard Henderson CPUArchState *env = cpu_env(cpu);
367e4e5cb4aSIlya Leoshkevich TaskState *ts = get_task_state(cpu);
36837714547SWarner Losh int core_dumped = 0;
36937714547SWarner Losh int host_sig;
37037714547SWarner Losh struct sigaction act;
37137714547SWarner Losh
37237714547SWarner Losh host_sig = target_to_host_signal(target_sig);
37337714547SWarner Losh gdb_signalled(env, target_sig);
37437714547SWarner Losh
37537714547SWarner Losh /* Dump core if supported by target binary format */
37637714547SWarner Losh if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
37737714547SWarner Losh stop_all_tasks();
37837714547SWarner Losh core_dumped =
37937714547SWarner Losh ((*ts->bprm->core_dump)(target_sig, env) == 0);
38037714547SWarner Losh }
38137714547SWarner Losh if (core_dumped) {
38237714547SWarner Losh struct rlimit nodump;
38337714547SWarner Losh
38437714547SWarner Losh /*
38537714547SWarner Losh * We already dumped the core of target process, we don't want
38637714547SWarner Losh * a coredump of qemu itself.
38737714547SWarner Losh */
38837714547SWarner Losh getrlimit(RLIMIT_CORE, &nodump);
38937714547SWarner Losh nodump.rlim_cur = 0;
39037714547SWarner Losh setrlimit(RLIMIT_CORE, &nodump);
39137714547SWarner Losh (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) "
39237714547SWarner Losh "- %s\n", target_sig, strsignal(host_sig), "core dumped");
39337714547SWarner Losh }
39437714547SWarner Losh
39537714547SWarner Losh /*
39637714547SWarner Losh * The proper exit code for dying from an uncaught signal is
39737714547SWarner Losh * -<signal>. The kernel doesn't allow exit() or _exit() to pass
39837714547SWarner Losh * a negative value. To get the proper exit code we need to
39937714547SWarner Losh * actually die from an uncaught signal. Here the default signal
40037714547SWarner Losh * handler is installed, we send ourself a signal and we wait for
40137714547SWarner Losh * it to arrive.
40237714547SWarner Losh */
40337714547SWarner Losh memset(&act, 0, sizeof(act));
40437714547SWarner Losh sigfillset(&act.sa_mask);
40537714547SWarner Losh act.sa_handler = SIG_DFL;
40637714547SWarner Losh sigaction(host_sig, &act, NULL);
40737714547SWarner Losh
40837714547SWarner Losh kill(getpid(), host_sig);
40937714547SWarner Losh
41037714547SWarner Losh /*
41137714547SWarner Losh * Make sure the signal isn't masked (just reuse the mask inside
41237714547SWarner Losh * of act).
41337714547SWarner Losh */
41437714547SWarner Losh sigdelset(&act.sa_mask, host_sig);
41537714547SWarner Losh sigsuspend(&act.sa_mask);
41637714547SWarner Losh
41737714547SWarner Losh /* unreachable */
41837714547SWarner Losh abort();
41937714547SWarner Losh }
42037714547SWarner Losh
4211366ef81SWarner Losh /*
4225abfac27SWarner Losh * Queue a signal so that it will be send to the virtual CPU as soon as
4235abfac27SWarner Losh * possible.
4245abfac27SWarner Losh */
queue_signal(CPUArchState * env,int sig,int si_type,target_siginfo_t * info)425e32a6301SWarner Losh void queue_signal(CPUArchState *env, int sig, int si_type,
426e32a6301SWarner Losh target_siginfo_t *info)
4275abfac27SWarner Losh {
42838be620cSWarner Losh CPUState *cpu = env_cpu(env);
429e4e5cb4aSIlya Leoshkevich TaskState *ts = get_task_state(cpu);
43038be620cSWarner Losh
43138be620cSWarner Losh trace_user_queue_signal(env, sig);
43238be620cSWarner Losh
43338be620cSWarner Losh info->si_code = deposit32(info->si_code, 24, 8, si_type);
43438be620cSWarner Losh
43538be620cSWarner Losh ts->sync_signal.info = *info;
43638be620cSWarner Losh ts->sync_signal.pending = sig;
43738be620cSWarner Losh /* Signal that a new signal is pending. */
43838be620cSWarner Losh qatomic_set(&ts->signal_pending, 1);
43938be620cSWarner Losh return;
4405abfac27SWarner Losh }
4415abfac27SWarner Losh
fatal_signal(int sig)442149076adSWarner Losh static int fatal_signal(int sig)
443149076adSWarner Losh {
444149076adSWarner Losh
445149076adSWarner Losh switch (sig) {
446149076adSWarner Losh case TARGET_SIGCHLD:
447149076adSWarner Losh case TARGET_SIGURG:
448149076adSWarner Losh case TARGET_SIGWINCH:
449149076adSWarner Losh case TARGET_SIGINFO:
450149076adSWarner Losh /* Ignored by default. */
451149076adSWarner Losh return 0;
452149076adSWarner Losh case TARGET_SIGCONT:
453149076adSWarner Losh case TARGET_SIGSTOP:
454149076adSWarner Losh case TARGET_SIGTSTP:
455149076adSWarner Losh case TARGET_SIGTTIN:
456149076adSWarner Losh case TARGET_SIGTTOU:
457149076adSWarner Losh /* Job control signals. */
458149076adSWarner Losh return 0;
459149076adSWarner Losh default:
460149076adSWarner Losh return 1;
461149076adSWarner Losh }
462149076adSWarner Losh }
463149076adSWarner Losh
4640ef59989SWarner Losh /*
4650ef59989SWarner Losh * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the
4660ef59989SWarner Losh * 'force' part is handled in process_pending_signals().
4670ef59989SWarner Losh */
force_sig_fault(int sig,int code,abi_ulong addr)4680ef59989SWarner Losh void force_sig_fault(int sig, int code, abi_ulong addr)
4690ef59989SWarner Losh {
4700ef59989SWarner Losh CPUState *cpu = thread_cpu;
4710ef59989SWarner Losh target_siginfo_t info = {};
4720ef59989SWarner Losh
4730ef59989SWarner Losh info.si_signo = sig;
4740ef59989SWarner Losh info.si_errno = 0;
4750ef59989SWarner Losh info.si_code = code;
4760ef59989SWarner Losh info.si_addr = addr;
47742e62aadSPhilippe Mathieu-Daudé queue_signal(cpu_env(cpu), sig, QEMU_SI_FAULT, &info);
4780ef59989SWarner Losh }
4790ef59989SWarner Losh
host_signal_handler(int host_sig,siginfo_t * info,void * puc)480149076adSWarner Losh static void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
481149076adSWarner Losh {
482b77af26eSRichard Henderson CPUState *cpu = thread_cpu;
483e4e5cb4aSIlya Leoshkevich TaskState *ts = get_task_state(cpu);
484e625c7efSWarner Losh target_siginfo_t tinfo;
485e625c7efSWarner Losh ucontext_t *uc = puc;
486e625c7efSWarner Losh struct emulated_sigtable *k;
487e625c7efSWarner Losh int guest_sig;
488e625c7efSWarner Losh uintptr_t pc = 0;
489e625c7efSWarner Losh bool sync_sig = false;
490e625c7efSWarner Losh
491e625c7efSWarner Losh /*
492e625c7efSWarner Losh * Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special
493e625c7efSWarner Losh * handling wrt signal blocking and unwinding.
494e625c7efSWarner Losh */
495e625c7efSWarner Losh if ((host_sig == SIGSEGV || host_sig == SIGBUS) && info->si_code > 0) {
496e625c7efSWarner Losh MMUAccessType access_type;
497e625c7efSWarner Losh uintptr_t host_addr;
498e625c7efSWarner Losh abi_ptr guest_addr;
499e625c7efSWarner Losh bool is_write;
500e625c7efSWarner Losh
501e625c7efSWarner Losh host_addr = (uintptr_t)info->si_addr;
502e625c7efSWarner Losh
503e625c7efSWarner Losh /*
504e625c7efSWarner Losh * Convert forcefully to guest address space: addresses outside
505e625c7efSWarner Losh * reserved_va are still valid to report via SEGV_MAPERR.
506e625c7efSWarner Losh */
507e625c7efSWarner Losh guest_addr = h2g_nocheck(host_addr);
508e625c7efSWarner Losh
509e625c7efSWarner Losh pc = host_signal_pc(uc);
510e625c7efSWarner Losh is_write = host_signal_write(info, uc);
511e625c7efSWarner Losh access_type = adjust_signal_pc(&pc, is_write);
512e625c7efSWarner Losh
513e625c7efSWarner Losh if (host_sig == SIGSEGV) {
514e625c7efSWarner Losh bool maperr = true;
515e625c7efSWarner Losh
516e625c7efSWarner Losh if (info->si_code == SEGV_ACCERR && h2g_valid(host_addr)) {
517e625c7efSWarner Losh /* If this was a write to a TB protected page, restart. */
518e625c7efSWarner Losh if (is_write &&
519e625c7efSWarner Losh handle_sigsegv_accerr_write(cpu, &uc->uc_sigmask,
520e625c7efSWarner Losh pc, guest_addr)) {
521e625c7efSWarner Losh return;
522e625c7efSWarner Losh }
523e625c7efSWarner Losh
524e625c7efSWarner Losh /*
525e625c7efSWarner Losh * With reserved_va, the whole address space is PROT_NONE,
526e625c7efSWarner Losh * which means that we may get ACCERR when we want MAPERR.
527e625c7efSWarner Losh */
528e625c7efSWarner Losh if (page_get_flags(guest_addr) & PAGE_VALID) {
529e625c7efSWarner Losh maperr = false;
530e625c7efSWarner Losh } else {
531e625c7efSWarner Losh info->si_code = SEGV_MAPERR;
532e625c7efSWarner Losh }
533e625c7efSWarner Losh }
534e625c7efSWarner Losh
535e625c7efSWarner Losh sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
536e625c7efSWarner Losh cpu_loop_exit_sigsegv(cpu, guest_addr, access_type, maperr, pc);
537e625c7efSWarner Losh } else {
538e625c7efSWarner Losh sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
539e625c7efSWarner Losh if (info->si_code == BUS_ADRALN) {
540e625c7efSWarner Losh cpu_loop_exit_sigbus(cpu, guest_addr, access_type, pc);
541e625c7efSWarner Losh }
542e625c7efSWarner Losh }
543e625c7efSWarner Losh
544e625c7efSWarner Losh sync_sig = true;
545e625c7efSWarner Losh }
546e625c7efSWarner Losh
547e625c7efSWarner Losh /* Get the target signal number. */
548e625c7efSWarner Losh guest_sig = host_to_target_signal(host_sig);
549e625c7efSWarner Losh if (guest_sig < 1 || guest_sig > TARGET_NSIG) {
550e625c7efSWarner Losh return;
551e625c7efSWarner Losh }
552e625c7efSWarner Losh trace_user_host_signal(cpu, host_sig, guest_sig);
553e625c7efSWarner Losh
554e625c7efSWarner Losh host_to_target_siginfo_noswap(&tinfo, info);
555e625c7efSWarner Losh
556e625c7efSWarner Losh k = &ts->sigtab[guest_sig - 1];
557e625c7efSWarner Losh k->info = tinfo;
558e625c7efSWarner Losh k->pending = guest_sig;
559e625c7efSWarner Losh ts->signal_pending = 1;
560e625c7efSWarner Losh
561e625c7efSWarner Losh /*
562e625c7efSWarner Losh * For synchronous signals, unwind the cpu state to the faulting
563e625c7efSWarner Losh * insn and then exit back to the main loop so that the signal
564e625c7efSWarner Losh * is delivered immediately.
565e625c7efSWarner Losh */
566e625c7efSWarner Losh if (sync_sig) {
567e625c7efSWarner Losh cpu->exception_index = EXCP_INTERRUPT;
568e625c7efSWarner Losh cpu_loop_exit_restore(cpu, pc);
569e625c7efSWarner Losh }
570e625c7efSWarner Losh
571e625c7efSWarner Losh rewind_if_in_safe_syscall(puc);
572e625c7efSWarner Losh
573e625c7efSWarner Losh /*
574e625c7efSWarner Losh * Block host signals until target signal handler entered. We
575e625c7efSWarner Losh * can't block SIGSEGV or SIGBUS while we're executing guest
576e625c7efSWarner Losh * code in case the guest code provokes one in the window between
577e625c7efSWarner Losh * now and it getting out to the main loop. Signals will be
578e625c7efSWarner Losh * unblocked again in process_pending_signals().
579e625c7efSWarner Losh */
580e625c7efSWarner Losh sigfillset(&uc->uc_sigmask);
581e625c7efSWarner Losh sigdelset(&uc->uc_sigmask, SIGSEGV);
582e625c7efSWarner Losh sigdelset(&uc->uc_sigmask, SIGBUS);
583e625c7efSWarner Losh
584e625c7efSWarner Losh /* Interrupt the virtual CPU as soon as possible. */
585e625c7efSWarner Losh cpu_exit(thread_cpu);
586149076adSWarner Losh }
587149076adSWarner Losh
58843ed4267SWarner Losh /* do_sigaltstack() returns target values and errnos. */
58943ed4267SWarner Losh /* compare to kern/kern_sig.c sys_sigaltstack() and kern_sigaltstack() */
do_sigaltstack(abi_ulong uss_addr,abi_ulong uoss_addr,abi_ulong sp)59043ed4267SWarner Losh abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
59143ed4267SWarner Losh {
592e4e5cb4aSIlya Leoshkevich TaskState *ts = get_task_state(thread_cpu);
59343ed4267SWarner Losh int ret;
59443ed4267SWarner Losh target_stack_t oss;
59543ed4267SWarner Losh
59643ed4267SWarner Losh if (uoss_addr) {
59743ed4267SWarner Losh /* Save current signal stack params */
59843ed4267SWarner Losh oss.ss_sp = tswapl(ts->sigaltstack_used.ss_sp);
59943ed4267SWarner Losh oss.ss_size = tswapl(ts->sigaltstack_used.ss_size);
60043ed4267SWarner Losh oss.ss_flags = tswapl(sas_ss_flags(ts, sp));
60143ed4267SWarner Losh }
60243ed4267SWarner Losh
60343ed4267SWarner Losh if (uss_addr) {
60443ed4267SWarner Losh target_stack_t *uss;
60543ed4267SWarner Losh target_stack_t ss;
60643ed4267SWarner Losh size_t minstacksize = TARGET_MINSIGSTKSZ;
60743ed4267SWarner Losh
60843ed4267SWarner Losh ret = -TARGET_EFAULT;
60943ed4267SWarner Losh if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
61043ed4267SWarner Losh goto out;
61143ed4267SWarner Losh }
61243ed4267SWarner Losh __get_user(ss.ss_sp, &uss->ss_sp);
61343ed4267SWarner Losh __get_user(ss.ss_size, &uss->ss_size);
61443ed4267SWarner Losh __get_user(ss.ss_flags, &uss->ss_flags);
61543ed4267SWarner Losh unlock_user_struct(uss, uss_addr, 0);
61643ed4267SWarner Losh
61743ed4267SWarner Losh ret = -TARGET_EPERM;
61843ed4267SWarner Losh if (on_sig_stack(ts, sp)) {
61943ed4267SWarner Losh goto out;
62043ed4267SWarner Losh }
62143ed4267SWarner Losh
62243ed4267SWarner Losh ret = -TARGET_EINVAL;
62343ed4267SWarner Losh if (ss.ss_flags != TARGET_SS_DISABLE
62443ed4267SWarner Losh && ss.ss_flags != TARGET_SS_ONSTACK
62543ed4267SWarner Losh && ss.ss_flags != 0) {
62643ed4267SWarner Losh goto out;
62743ed4267SWarner Losh }
62843ed4267SWarner Losh
62943ed4267SWarner Losh if (ss.ss_flags == TARGET_SS_DISABLE) {
63043ed4267SWarner Losh ss.ss_size = 0;
63143ed4267SWarner Losh ss.ss_sp = 0;
63243ed4267SWarner Losh } else {
63343ed4267SWarner Losh ret = -TARGET_ENOMEM;
63443ed4267SWarner Losh if (ss.ss_size < minstacksize) {
63543ed4267SWarner Losh goto out;
63643ed4267SWarner Losh }
63743ed4267SWarner Losh }
63843ed4267SWarner Losh
63943ed4267SWarner Losh ts->sigaltstack_used.ss_sp = ss.ss_sp;
64043ed4267SWarner Losh ts->sigaltstack_used.ss_size = ss.ss_size;
64143ed4267SWarner Losh }
64243ed4267SWarner Losh
64343ed4267SWarner Losh if (uoss_addr) {
64443ed4267SWarner Losh ret = -TARGET_EFAULT;
64543ed4267SWarner Losh if (copy_to_user(uoss_addr, &oss, sizeof(oss))) {
64643ed4267SWarner Losh goto out;
64743ed4267SWarner Losh }
64843ed4267SWarner Losh }
64943ed4267SWarner Losh
65043ed4267SWarner Losh ret = 0;
65143ed4267SWarner Losh out:
65243ed4267SWarner Losh return ret;
65343ed4267SWarner Losh }
65443ed4267SWarner Losh
655394cf694SWarner Losh /* do_sigaction() return host values and errnos */
do_sigaction(int sig,const struct target_sigaction * act,struct target_sigaction * oact)656394cf694SWarner Losh int do_sigaction(int sig, const struct target_sigaction *act,
657394cf694SWarner Losh struct target_sigaction *oact)
658394cf694SWarner Losh {
659394cf694SWarner Losh struct target_sigaction *k;
660394cf694SWarner Losh struct sigaction act1;
661394cf694SWarner Losh int host_sig;
662394cf694SWarner Losh int ret = 0;
663394cf694SWarner Losh
664394cf694SWarner Losh if (sig < 1 || sig > TARGET_NSIG) {
665394cf694SWarner Losh return -TARGET_EINVAL;
666394cf694SWarner Losh }
667394cf694SWarner Losh
668394cf694SWarner Losh if ((sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP) &&
669394cf694SWarner Losh act != NULL && act->_sa_handler != TARGET_SIG_DFL) {
670394cf694SWarner Losh return -TARGET_EINVAL;
671394cf694SWarner Losh }
672394cf694SWarner Losh
673394cf694SWarner Losh if (block_signals()) {
674394cf694SWarner Losh return -TARGET_ERESTART;
675394cf694SWarner Losh }
676394cf694SWarner Losh
677394cf694SWarner Losh k = &sigact_table[sig - 1];
678394cf694SWarner Losh if (oact) {
679394cf694SWarner Losh oact->_sa_handler = tswapal(k->_sa_handler);
680394cf694SWarner Losh oact->sa_flags = tswap32(k->sa_flags);
681394cf694SWarner Losh oact->sa_mask = k->sa_mask;
682394cf694SWarner Losh }
683394cf694SWarner Losh if (act) {
684394cf694SWarner Losh k->_sa_handler = tswapal(act->_sa_handler);
685394cf694SWarner Losh k->sa_flags = tswap32(act->sa_flags);
686394cf694SWarner Losh k->sa_mask = act->sa_mask;
687394cf694SWarner Losh
688394cf694SWarner Losh /* Update the host signal state. */
689394cf694SWarner Losh host_sig = target_to_host_signal(sig);
690394cf694SWarner Losh if (host_sig != SIGSEGV && host_sig != SIGBUS) {
691394cf694SWarner Losh memset(&act1, 0, sizeof(struct sigaction));
692394cf694SWarner Losh sigfillset(&act1.sa_mask);
693394cf694SWarner Losh act1.sa_flags = SA_SIGINFO;
694394cf694SWarner Losh if (k->sa_flags & TARGET_SA_RESTART) {
695394cf694SWarner Losh act1.sa_flags |= SA_RESTART;
696394cf694SWarner Losh }
697394cf694SWarner Losh /*
698394cf694SWarner Losh * Note: It is important to update the host kernel signal mask to
699394cf694SWarner Losh * avoid getting unexpected interrupted system calls.
700394cf694SWarner Losh */
701394cf694SWarner Losh if (k->_sa_handler == TARGET_SIG_IGN) {
702394cf694SWarner Losh act1.sa_sigaction = (void *)SIG_IGN;
703394cf694SWarner Losh } else if (k->_sa_handler == TARGET_SIG_DFL) {
704394cf694SWarner Losh if (fatal_signal(sig)) {
705394cf694SWarner Losh act1.sa_sigaction = host_signal_handler;
706394cf694SWarner Losh } else {
707394cf694SWarner Losh act1.sa_sigaction = (void *)SIG_DFL;
708394cf694SWarner Losh }
709394cf694SWarner Losh } else {
710394cf694SWarner Losh act1.sa_sigaction = host_signal_handler;
711394cf694SWarner Losh }
712394cf694SWarner Losh ret = sigaction(host_sig, &act1, NULL);
713394cf694SWarner Losh }
714394cf694SWarner Losh }
715394cf694SWarner Losh return ret;
716394cf694SWarner Losh }
717394cf694SWarner Losh
get_sigframe(struct target_sigaction * ka,CPUArchState * env,size_t frame_size)71846f4f76dSWarner Losh static inline abi_ulong get_sigframe(struct target_sigaction *ka,
71946f4f76dSWarner Losh CPUArchState *env, size_t frame_size)
72046f4f76dSWarner Losh {
721e4e5cb4aSIlya Leoshkevich TaskState *ts = get_task_state(thread_cpu);
72246f4f76dSWarner Losh abi_ulong sp;
72346f4f76dSWarner Losh
72446f4f76dSWarner Losh /* Use default user stack */
72546f4f76dSWarner Losh sp = get_sp_from_cpustate(env);
72646f4f76dSWarner Losh
72746f4f76dSWarner Losh if ((ka->sa_flags & TARGET_SA_ONSTACK) && sas_ss_flags(ts, sp) == 0) {
72846f4f76dSWarner Losh sp = ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
72946f4f76dSWarner Losh }
73046f4f76dSWarner Losh
731*5fa2a10bSWarner Losh return ROUND_DOWN(sp - frame_size, TARGET_SIGSTACK_ALIGN);
73246f4f76dSWarner Losh }
73346f4f76dSWarner Losh
73446f4f76dSWarner Losh /* compare to $M/$M/exec_machdep.c sendsig and sys/kern/kern_sig.c sigexit */
73546f4f76dSWarner Losh
setup_frame(int sig,int code,struct target_sigaction * ka,target_sigset_t * set,target_siginfo_t * tinfo,CPUArchState * env)73646f4f76dSWarner Losh static void setup_frame(int sig, int code, struct target_sigaction *ka,
73746f4f76dSWarner Losh target_sigset_t *set, target_siginfo_t *tinfo, CPUArchState *env)
73846f4f76dSWarner Losh {
73946f4f76dSWarner Losh struct target_sigframe *frame;
74046f4f76dSWarner Losh abi_ulong frame_addr;
74146f4f76dSWarner Losh int i;
74246f4f76dSWarner Losh
74346f4f76dSWarner Losh frame_addr = get_sigframe(ka, env, sizeof(*frame));
74446f4f76dSWarner Losh trace_user_setup_frame(env, frame_addr);
74546f4f76dSWarner Losh if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
74646f4f76dSWarner Losh unlock_user_struct(frame, frame_addr, 1);
74746f4f76dSWarner Losh dump_core_and_abort(TARGET_SIGILL);
74846f4f76dSWarner Losh return;
74946f4f76dSWarner Losh }
75046f4f76dSWarner Losh
75146f4f76dSWarner Losh memset(frame, 0, sizeof(*frame));
75246f4f76dSWarner Losh setup_sigframe_arch(env, frame_addr, frame, 0);
75346f4f76dSWarner Losh
75446f4f76dSWarner Losh for (i = 0; i < TARGET_NSIG_WORDS; i++) {
75546f4f76dSWarner Losh __put_user(set->__bits[i], &frame->sf_uc.uc_sigmask.__bits[i]);
75646f4f76dSWarner Losh }
75746f4f76dSWarner Losh
75846f4f76dSWarner Losh if (tinfo) {
75946f4f76dSWarner Losh frame->sf_si.si_signo = tinfo->si_signo;
76046f4f76dSWarner Losh frame->sf_si.si_errno = tinfo->si_errno;
76146f4f76dSWarner Losh frame->sf_si.si_code = tinfo->si_code;
76246f4f76dSWarner Losh frame->sf_si.si_pid = tinfo->si_pid;
76346f4f76dSWarner Losh frame->sf_si.si_uid = tinfo->si_uid;
76446f4f76dSWarner Losh frame->sf_si.si_status = tinfo->si_status;
76546f4f76dSWarner Losh frame->sf_si.si_addr = tinfo->si_addr;
76646f4f76dSWarner Losh /* see host_to_target_siginfo_noswap() for more details */
76746f4f76dSWarner Losh frame->sf_si.si_value.sival_ptr = tinfo->si_value.sival_ptr;
76846f4f76dSWarner Losh /*
76946f4f76dSWarner Losh * At this point, whatever is in the _reason union is complete
77046f4f76dSWarner Losh * and in target order, so just copy the whole thing over, even
77146f4f76dSWarner Losh * if it's too large for this specific signal.
77246f4f76dSWarner Losh * host_to_target_siginfo_noswap() and tswap_siginfo() have ensured
77346f4f76dSWarner Losh * that's so.
77446f4f76dSWarner Losh */
77546f4f76dSWarner Losh memcpy(&frame->sf_si._reason, &tinfo->_reason,
77646f4f76dSWarner Losh sizeof(tinfo->_reason));
77746f4f76dSWarner Losh }
77846f4f76dSWarner Losh
77946f4f76dSWarner Losh set_sigtramp_args(env, sig, frame, frame_addr, ka);
78046f4f76dSWarner Losh
78146f4f76dSWarner Losh unlock_user_struct(frame, frame_addr, 1);
78246f4f76dSWarner Losh }
78346f4f76dSWarner Losh
reset_signal_mask(target_ucontext_t * ucontext)784c885ae0eSWarner Losh static int reset_signal_mask(target_ucontext_t *ucontext)
785c885ae0eSWarner Losh {
786c885ae0eSWarner Losh int i;
787c885ae0eSWarner Losh sigset_t blocked;
788c885ae0eSWarner Losh target_sigset_t target_set;
789e4e5cb4aSIlya Leoshkevich TaskState *ts = get_task_state(thread_cpu);
790c885ae0eSWarner Losh
791c885ae0eSWarner Losh for (i = 0; i < TARGET_NSIG_WORDS; i++) {
7926538c682SWarner Losh __get_user(target_set.__bits[i], &ucontext->uc_sigmask.__bits[i]);
793c885ae0eSWarner Losh }
794c885ae0eSWarner Losh target_to_host_sigset_internal(&blocked, &target_set);
795c885ae0eSWarner Losh ts->signal_mask = blocked;
796c885ae0eSWarner Losh
797c885ae0eSWarner Losh return 0;
798c885ae0eSWarner Losh }
799c885ae0eSWarner Losh
800c885ae0eSWarner Losh /* See sys/$M/$M/exec_machdep.c sigreturn() */
do_sigreturn(CPUArchState * env,abi_ulong addr)801c885ae0eSWarner Losh long do_sigreturn(CPUArchState *env, abi_ulong addr)
802c885ae0eSWarner Losh {
803c885ae0eSWarner Losh long ret;
804c885ae0eSWarner Losh abi_ulong target_ucontext;
805c885ae0eSWarner Losh target_ucontext_t *ucontext = NULL;
806c885ae0eSWarner Losh
807c885ae0eSWarner Losh /* Get the target ucontext address from the stack frame */
808c885ae0eSWarner Losh ret = get_ucontext_sigreturn(env, addr, &target_ucontext);
809c885ae0eSWarner Losh if (is_error(ret)) {
810c885ae0eSWarner Losh return ret;
811c885ae0eSWarner Losh }
812c885ae0eSWarner Losh trace_user_do_sigreturn(env, addr);
813c885ae0eSWarner Losh if (!lock_user_struct(VERIFY_READ, ucontext, target_ucontext, 0)) {
814c885ae0eSWarner Losh goto badframe;
815c885ae0eSWarner Losh }
816c885ae0eSWarner Losh
817c885ae0eSWarner Losh /* Set the register state back to before the signal. */
818c885ae0eSWarner Losh if (set_mcontext(env, &ucontext->uc_mcontext, 1)) {
819c885ae0eSWarner Losh goto badframe;
820c885ae0eSWarner Losh }
821c885ae0eSWarner Losh
822c885ae0eSWarner Losh /* And reset the signal mask. */
823c885ae0eSWarner Losh if (reset_signal_mask(ucontext)) {
824c885ae0eSWarner Losh goto badframe;
825c885ae0eSWarner Losh }
826c885ae0eSWarner Losh
827c885ae0eSWarner Losh unlock_user_struct(ucontext, target_ucontext, 0);
828c885ae0eSWarner Losh return -TARGET_EJUSTRETURN;
829c885ae0eSWarner Losh
830c885ae0eSWarner Losh badframe:
831c885ae0eSWarner Losh if (ucontext != NULL) {
832c885ae0eSWarner Losh unlock_user_struct(ucontext, target_ucontext, 0);
833c885ae0eSWarner Losh }
834c885ae0eSWarner Losh return -TARGET_EFAULT;
835c885ae0eSWarner Losh }
836c885ae0eSWarner Losh
signal_init(void)83784778508Sblueswir1 void signal_init(void)
83884778508Sblueswir1 {
839e4e5cb4aSIlya Leoshkevich TaskState *ts = get_task_state(thread_cpu);
840149076adSWarner Losh struct sigaction act;
841149076adSWarner Losh struct sigaction oact;
842149076adSWarner Losh int i;
843149076adSWarner Losh int host_sig;
844149076adSWarner Losh
845149076adSWarner Losh /* Set the signal mask from the host mask. */
846149076adSWarner Losh sigprocmask(0, 0, &ts->signal_mask);
847149076adSWarner Losh
848149076adSWarner Losh sigfillset(&act.sa_mask);
849149076adSWarner Losh act.sa_sigaction = host_signal_handler;
850149076adSWarner Losh act.sa_flags = SA_SIGINFO;
851149076adSWarner Losh
852149076adSWarner Losh for (i = 1; i <= TARGET_NSIG; i++) {
853149076adSWarner Losh host_sig = target_to_host_signal(i);
854149076adSWarner Losh sigaction(host_sig, NULL, &oact);
855149076adSWarner Losh if (oact.sa_sigaction == (void *)SIG_IGN) {
856149076adSWarner Losh sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
857149076adSWarner Losh } else if (oact.sa_sigaction == (void *)SIG_DFL) {
858149076adSWarner Losh sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
859149076adSWarner Losh }
860149076adSWarner Losh /*
861149076adSWarner Losh * If there's already a handler installed then something has
862149076adSWarner Losh * gone horribly wrong, so don't even try to handle that case.
863149076adSWarner Losh * Install some handlers for our own use. We need at least
864149076adSWarner Losh * SIGSEGV and SIGBUS, to detect exceptions. We can not just
865149076adSWarner Losh * trap all signals because it affects syscall interrupt
866149076adSWarner Losh * behavior. But do trap all default-fatal signals.
867149076adSWarner Losh */
868149076adSWarner Losh if (fatal_signal(i)) {
869149076adSWarner Losh sigaction(host_sig, &act, NULL);
870149076adSWarner Losh }
871149076adSWarner Losh }
87284778508Sblueswir1 }
87384778508Sblueswir1
handle_pending_signal(CPUArchState * env,int sig,struct emulated_sigtable * k)8746c6d4b56SWarner Losh static void handle_pending_signal(CPUArchState *env, int sig,
8756c6d4b56SWarner Losh struct emulated_sigtable *k)
8766c6d4b56SWarner Losh {
8776c6d4b56SWarner Losh CPUState *cpu = env_cpu(env);
878e4e5cb4aSIlya Leoshkevich TaskState *ts = get_task_state(cpu);
8796c6d4b56SWarner Losh struct target_sigaction *sa;
8806c6d4b56SWarner Losh int code;
8816c6d4b56SWarner Losh sigset_t set;
8826c6d4b56SWarner Losh abi_ulong handler;
8836c6d4b56SWarner Losh target_siginfo_t tinfo;
8846c6d4b56SWarner Losh target_sigset_t target_old_set;
8856c6d4b56SWarner Losh
8866c6d4b56SWarner Losh trace_user_handle_signal(env, sig);
8876c6d4b56SWarner Losh
8886c6d4b56SWarner Losh k->pending = 0;
8896c6d4b56SWarner Losh
890f84e313eSGustavo Romero sig = gdb_handlesig(cpu, sig, NULL, &k->info, sizeof(k->info));
8916c6d4b56SWarner Losh if (!sig) {
8926c6d4b56SWarner Losh sa = NULL;
8936c6d4b56SWarner Losh handler = TARGET_SIG_IGN;
8946c6d4b56SWarner Losh } else {
8956c6d4b56SWarner Losh sa = &sigact_table[sig - 1];
8966c6d4b56SWarner Losh handler = sa->_sa_handler;
8976c6d4b56SWarner Losh }
8986c6d4b56SWarner Losh
8996c6d4b56SWarner Losh if (do_strace) {
9006c6d4b56SWarner Losh print_taken_signal(sig, &k->info);
9016c6d4b56SWarner Losh }
9026c6d4b56SWarner Losh
9036c6d4b56SWarner Losh if (handler == TARGET_SIG_DFL) {
9046c6d4b56SWarner Losh /*
9056c6d4b56SWarner Losh * default handler : ignore some signal. The other are job
9066c6d4b56SWarner Losh * control or fatal.
9076c6d4b56SWarner Losh */
9086c6d4b56SWarner Losh if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN ||
9096c6d4b56SWarner Losh sig == TARGET_SIGTTOU) {
9106c6d4b56SWarner Losh kill(getpid(), SIGSTOP);
9116c6d4b56SWarner Losh } else if (sig != TARGET_SIGCHLD && sig != TARGET_SIGURG &&
9126c6d4b56SWarner Losh sig != TARGET_SIGINFO && sig != TARGET_SIGWINCH &&
9136c6d4b56SWarner Losh sig != TARGET_SIGCONT) {
9146c6d4b56SWarner Losh dump_core_and_abort(sig);
9156c6d4b56SWarner Losh }
9166c6d4b56SWarner Losh } else if (handler == TARGET_SIG_IGN) {
9176c6d4b56SWarner Losh /* ignore sig */
9186c6d4b56SWarner Losh } else if (handler == TARGET_SIG_ERR) {
9196c6d4b56SWarner Losh dump_core_and_abort(sig);
9206c6d4b56SWarner Losh } else {
9216c6d4b56SWarner Losh /* compute the blocked signals during the handler execution */
9226c6d4b56SWarner Losh sigset_t *blocked_set;
9236c6d4b56SWarner Losh
9246c6d4b56SWarner Losh target_to_host_sigset(&set, &sa->sa_mask);
9256c6d4b56SWarner Losh /*
9266c6d4b56SWarner Losh * SA_NODEFER indicates that the current signal should not be
9276c6d4b56SWarner Losh * blocked during the handler.
9286c6d4b56SWarner Losh */
9296c6d4b56SWarner Losh if (!(sa->sa_flags & TARGET_SA_NODEFER)) {
9306c6d4b56SWarner Losh sigaddset(&set, target_to_host_signal(sig));
9316c6d4b56SWarner Losh }
9326c6d4b56SWarner Losh
9336c6d4b56SWarner Losh /*
9346c6d4b56SWarner Losh * Save the previous blocked signal state to restore it at the
9356c6d4b56SWarner Losh * end of the signal execution (see do_sigreturn).
9366c6d4b56SWarner Losh */
9376c6d4b56SWarner Losh host_to_target_sigset_internal(&target_old_set, &ts->signal_mask);
9386c6d4b56SWarner Losh
9396c6d4b56SWarner Losh blocked_set = ts->in_sigsuspend ?
9406c6d4b56SWarner Losh &ts->sigsuspend_mask : &ts->signal_mask;
9416c6d4b56SWarner Losh sigorset(&ts->signal_mask, blocked_set, &set);
9426c6d4b56SWarner Losh ts->in_sigsuspend = false;
9436c6d4b56SWarner Losh sigprocmask(SIG_SETMASK, &ts->signal_mask, NULL);
9446c6d4b56SWarner Losh
9456c6d4b56SWarner Losh /* XXX VM86 on x86 ??? */
9466c6d4b56SWarner Losh
9476c6d4b56SWarner Losh code = k->info.si_code; /* From host, so no si_type */
9486c6d4b56SWarner Losh /* prepare the stack frame of the virtual CPU */
9496c6d4b56SWarner Losh if (sa->sa_flags & TARGET_SA_SIGINFO) {
9506c6d4b56SWarner Losh tswap_siginfo(&tinfo, &k->info);
9516c6d4b56SWarner Losh setup_frame(sig, code, sa, &target_old_set, &tinfo, env);
9526c6d4b56SWarner Losh } else {
9536c6d4b56SWarner Losh setup_frame(sig, code, sa, &target_old_set, NULL, env);
9546c6d4b56SWarner Losh }
9556c6d4b56SWarner Losh if (sa->sa_flags & TARGET_SA_RESETHAND) {
9566c6d4b56SWarner Losh sa->_sa_handler = TARGET_SIG_DFL;
9576c6d4b56SWarner Losh }
9586c6d4b56SWarner Losh }
9596c6d4b56SWarner Losh }
9606c6d4b56SWarner Losh
process_pending_signals(CPUArchState * env)961d7acd317SWarner Losh void process_pending_signals(CPUArchState *env)
96284778508Sblueswir1 {
963d7acd317SWarner Losh CPUState *cpu = env_cpu(env);
964d7acd317SWarner Losh int sig;
965d7acd317SWarner Losh sigset_t *blocked_set, set;
966d7acd317SWarner Losh struct emulated_sigtable *k;
967e4e5cb4aSIlya Leoshkevich TaskState *ts = get_task_state(cpu);
968d7acd317SWarner Losh
969d7acd317SWarner Losh while (qatomic_read(&ts->signal_pending)) {
970d7acd317SWarner Losh sigfillset(&set);
971d7acd317SWarner Losh sigprocmask(SIG_SETMASK, &set, 0);
972d7acd317SWarner Losh
973d7acd317SWarner Losh restart_scan:
974d7acd317SWarner Losh sig = ts->sync_signal.pending;
975d7acd317SWarner Losh if (sig) {
976d7acd317SWarner Losh /*
977d7acd317SWarner Losh * Synchronous signals are forced by the emulated CPU in some way.
978d7acd317SWarner Losh * If they are set to ignore, restore the default handler (see
979d7acd317SWarner Losh * sys/kern_sig.c trapsignal() and execsigs() for this behavior)
980d7acd317SWarner Losh * though maybe this is done only when forcing exit for non SIGCHLD.
981d7acd317SWarner Losh */
982d7acd317SWarner Losh if (sigismember(&ts->signal_mask, target_to_host_signal(sig)) ||
983d7acd317SWarner Losh sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) {
984d7acd317SWarner Losh sigdelset(&ts->signal_mask, target_to_host_signal(sig));
985d7acd317SWarner Losh sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL;
986d7acd317SWarner Losh }
987d7acd317SWarner Losh handle_pending_signal(env, sig, &ts->sync_signal);
988d7acd317SWarner Losh }
989d7acd317SWarner Losh
990d7acd317SWarner Losh k = ts->sigtab;
991d7acd317SWarner Losh for (sig = 1; sig <= TARGET_NSIG; sig++, k++) {
992d7acd317SWarner Losh blocked_set = ts->in_sigsuspend ?
993d7acd317SWarner Losh &ts->sigsuspend_mask : &ts->signal_mask;
994d7acd317SWarner Losh if (k->pending &&
995d7acd317SWarner Losh !sigismember(blocked_set, target_to_host_signal(sig))) {
996d7acd317SWarner Losh handle_pending_signal(env, sig, k);
997d7acd317SWarner Losh /*
998d7acd317SWarner Losh * Restart scan from the beginning, as handle_pending_signal
999d7acd317SWarner Losh * might have resulted in a new synchronous signal (eg SIGSEGV).
1000d7acd317SWarner Losh */
1001d7acd317SWarner Losh goto restart_scan;
1002d7acd317SWarner Losh }
1003d7acd317SWarner Losh }
1004d7acd317SWarner Losh
1005d7acd317SWarner Losh /*
1006d7acd317SWarner Losh * Unblock signals and check one more time. Unblocking signals may cause
1007d7acd317SWarner Losh * us to take another host signal, which will set signal_pending again.
1008d7acd317SWarner Losh */
1009d7acd317SWarner Losh qatomic_set(&ts->signal_pending, 0);
1010d7acd317SWarner Losh ts->in_sigsuspend = false;
1011d7acd317SWarner Losh set = ts->signal_mask;
1012d7acd317SWarner Losh sigdelset(&set, SIGSEGV);
1013d7acd317SWarner Losh sigdelset(&set, SIGBUS);
1014d7acd317SWarner Losh sigprocmask(SIG_SETMASK, &set, 0);
1015d7acd317SWarner Losh }
1016d7acd317SWarner Losh ts->in_sigsuspend = false;
101784778508Sblueswir1 }
1018835b04edSWarner Losh
cpu_loop_exit_sigsegv(CPUState * cpu,target_ulong addr,MMUAccessType access_type,bool maperr,uintptr_t ra)1019835b04edSWarner Losh void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr,
1020835b04edSWarner Losh MMUAccessType access_type, bool maperr, uintptr_t ra)
1021835b04edSWarner Losh {
10221764ad70SRichard Henderson const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
1023fc9f9bddSWarner Losh
1024fc9f9bddSWarner Losh if (tcg_ops->record_sigsegv) {
1025fc9f9bddSWarner Losh tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra);
1026fc9f9bddSWarner Losh }
1027fc9f9bddSWarner Losh
1028fc9f9bddSWarner Losh force_sig_fault(TARGET_SIGSEGV,
1029fc9f9bddSWarner Losh maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR,
1030fc9f9bddSWarner Losh addr);
1031fc9f9bddSWarner Losh cpu->exception_index = EXCP_INTERRUPT;
1032fc9f9bddSWarner Losh cpu_loop_exit_restore(cpu, ra);
1033835b04edSWarner Losh }
1034835b04edSWarner Losh
cpu_loop_exit_sigbus(CPUState * cpu,target_ulong addr,MMUAccessType access_type,uintptr_t ra)1035835b04edSWarner Losh void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr,
1036835b04edSWarner Losh MMUAccessType access_type, uintptr_t ra)
1037835b04edSWarner Losh {
10381764ad70SRichard Henderson const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
1039cfdee273SWarner Losh
1040cfdee273SWarner Losh if (tcg_ops->record_sigbus) {
1041cfdee273SWarner Losh tcg_ops->record_sigbus(cpu, addr, access_type, ra);
1042cfdee273SWarner Losh }
1043cfdee273SWarner Losh
1044cfdee273SWarner Losh force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr);
1045cfdee273SWarner Losh cpu->exception_index = EXCP_INTERRUPT;
1046cfdee273SWarner Losh cpu_loop_exit_restore(cpu, ra);
1047835b04edSWarner Losh }
1048