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