blob: 24a403a70a02017008198cb9312d800acb3973f2 [file] [log] [blame]
Alex Dewar97870c32019-08-25 10:49:18 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Anton Ivanov2eb5f312015-11-02 16:16:37 +00003 * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
4 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (C) 2004 PathScale, Inc
Jeff Dikeba180fd2007-10-16 01:27:00 -07006 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Gennady Sharapov0805d892006-01-08 01:01:29 -08009#include <stdlib.h>
Gennady Sharapov0805d892006-01-08 01:01:29 -080010#include <stdarg.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070011#include <errno.h>
12#include <signal.h>
Johannes Bergb8f78822019-08-23 12:44:27 +020013#include <string.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070014#include <strings.h>
Al Viro37185b32012-10-08 03:27:32 +010015#include <as-layout.h>
16#include <kern_util.h>
17#include <os.h>
18#include <sysdep/mcontext.h>
Richard Weinberger57a05d82016-06-12 22:03:16 +020019#include <um_malloc.h>
Jason A. Donenfeld530ba6c2017-12-14 03:23:37 +010020#include <sys/ucontext.h>
Johannes Bergd6b399a2021-03-05 13:19:56 +010021#include <timetravel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Sergei Trofimovich72383d42012-12-30 01:37:31 +030023void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *) = {
Jeff Dike75ada8f2008-02-04 22:31:12 -080024 [SIGTRAP] = relay_signal,
25 [SIGFPE] = relay_signal,
26 [SIGILL] = relay_signal,
27 [SIGWINCH] = winch,
28 [SIGBUS] = bus_handler,
29 [SIGSEGV] = segv_handler,
30 [SIGIO] = sigio_handler,
Anton Ivanov2eb5f312015-11-02 16:16:37 +000031};
Jeff Dike75ada8f2008-02-04 22:31:12 -080032
Richard Weinberger9a8c1352013-07-19 11:31:36 +020033static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc)
Jeff Dike75ada8f2008-02-04 22:31:12 -080034{
Anton Ivanov5c2ffce2019-01-04 15:38:21 +000035 struct uml_pt_regs r;
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080036 int save_errno = errno;
Jeff Dike75ada8f2008-02-04 22:31:12 -080037
Anton Ivanov5c2ffce2019-01-04 15:38:21 +000038 r.is_user = 0;
Jeff Dike75ada8f2008-02-04 22:31:12 -080039 if (sig == SIGSEGV) {
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080040 /* For segfaults, we want the data from the sigcontext. */
Anton Ivanov5c2ffce2019-01-04 15:38:21 +000041 get_regs_from_mc(&r, mc);
42 GET_FAULTINFO_FROM_MC(r.faultinfo, mc);
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080043 }
Jeff Dike75ada8f2008-02-04 22:31:12 -080044
Jeff Dikee6a2d1f2008-02-04 22:31:13 -080045 /* enable signals if sig isn't IRQ signal */
Johannes Berg68c15a22019-08-23 12:44:26 +020046 if ((sig != SIGIO) && (sig != SIGWINCH))
Johannes Berg0dafcbe2019-08-23 13:16:23 +020047 unblock_signals_trace();
Jeff Dike75ada8f2008-02-04 22:31:12 -080048
Anton Ivanov5c2ffce2019-01-04 15:38:21 +000049 (*sig_info[sig])(sig, si, &r);
Jeff Dike75ada8f2008-02-04 22:31:12 -080050
51 errno = save_errno;
Jeff Dike75ada8f2008-02-04 22:31:12 -080052}
53
Jeff Dikeba180fd2007-10-16 01:27:00 -070054/*
Jeff Dike61b63c52007-10-16 01:27:27 -070055 * These are the asynchronous signals. SIGPROF is excluded because we want to
Jeff Dike1d7173b2006-01-18 17:42:49 -080056 * be able to profile all of UML, not just the non-critical sections. If
57 * profiling is not thread-safe, then that is not my problem. We can disable
58 * profiling when SMP is enabled in that case.
59 */
60#define SIGIO_BIT 0
61#define SIGIO_MASK (1 << SIGIO_BIT)
62
Anton Ivanov2eb5f312015-11-02 16:16:37 +000063#define SIGALRM_BIT 1
64#define SIGALRM_MASK (1 << SIGALRM_BIT)
Jeff Dike1d7173b2006-01-18 17:42:49 -080065
Johannes Bergfbb42e7f2021-03-05 13:19:54 +010066int signals_enabled;
Johannes Bergd6b399a2021-03-05 13:19:56 +010067#ifdef UML_CONFIG_UML_TIME_TRAVEL_SUPPORT
68static int signals_blocked;
69#else
Alexey Dobriyan39f75da2021-08-02 23:40:31 +030070#define signals_blocked 0
Johannes Bergd6b399a2021-03-05 13:19:56 +010071#endif
Jeff Dikecfef8f32008-02-04 22:31:16 -080072static unsigned int signals_pending;
Anton Ivanovd5e3f5c2015-12-21 11:28:02 +000073static unsigned int signals_active = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -080074
Richard Weinberger9a8c1352013-07-19 11:31:36 +020075void sig_handler(int sig, struct siginfo *si, mcontext_t *mc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Johannes Bergd6b399a2021-03-05 13:19:56 +010077 int enabled = signals_enabled;
Jeff Dike1d7173b2006-01-18 17:42:49 -080078
Johannes Bergd6b399a2021-03-05 13:19:56 +010079 if ((signals_blocked || !enabled) && (sig == SIGIO)) {
80 /*
81 * In TT_MODE_EXTERNAL, need to still call time-travel
82 * handlers unless signals are also blocked for the
83 * external time message processing. This will mark
84 * signals_pending by itself (only if necessary.)
85 */
86 if (!signals_blocked && time_travel_mode == TT_MODE_EXTERNAL)
87 sigio_run_timetravel_handlers();
88 else
89 signals_pending |= SIGIO_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -080090 return;
91 }
92
Johannes Berg0dafcbe2019-08-23 13:16:23 +020093 block_signals_trace();
Jeff Dike1d7173b2006-01-18 17:42:49 -080094
Martin Pärteld3c1cfc2012-08-02 00:49:17 +020095 sig_handler_common(sig, si, mc);
Jeff Dike1d7173b2006-01-18 17:42:49 -080096
Johannes Bergbbe33502021-08-31 09:11:15 +020097 um_set_signals_trace(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000100static void timer_real_alarm_handler(mcontext_t *mc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Anton Ivanov5c2ffce2019-01-04 15:38:21 +0000102 struct uml_pt_regs regs;
Jeff Dike2ea5bc52007-05-10 22:22:32 -0700103
Al Viro248b74c2011-08-18 20:05:09 +0100104 if (mc != NULL)
Anton Ivanov5c2ffce2019-01-04 15:38:21 +0000105 get_regs_from_mc(&regs, mc);
Johannes Bergb8f78822019-08-23 12:44:27 +0200106 else
107 memset(&regs, 0, sizeof(regs));
Anton Ivanov5c2ffce2019-01-04 15:38:21 +0000108 timer_handler(SIGALRM, NULL, &regs);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800109}
110
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000111void timer_alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800112{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800113 int enabled;
114
Jeff Dike1d7173b2006-01-18 17:42:49 -0800115 enabled = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700116 if (!signals_enabled) {
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000117 signals_pending |= SIGALRM_MASK;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800118 return;
119 }
120
Johannes Berg0dafcbe2019-08-23 13:16:23 +0200121 block_signals_trace();
Jeff Dike1d7173b2006-01-18 17:42:49 -0800122
Anton Ivanovd5e3f5c2015-12-21 11:28:02 +0000123 signals_active |= SIGALRM_MASK;
124
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000125 timer_real_alarm_handler(mc);
Anton Ivanovd5e3f5c2015-12-21 11:28:02 +0000126
127 signals_active &= ~SIGALRM_MASK;
128
Johannes Bergbbe33502021-08-31 09:11:15 +0200129 um_set_signals_trace(enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000132void deliver_alarm(void) {
133 timer_alarm_handler(SIGALRM, NULL, NULL);
134}
135
136void timer_set_signal_handler(void)
Jeff Dike78a26e22007-10-16 01:27:23 -0700137{
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000138 set_handler(SIGALRM);
Jeff Dike78a26e22007-10-16 01:27:23 -0700139}
140
Gennady Sharapov0805d892006-01-08 01:01:29 -0800141void set_sigstack(void *sig_stack, int size)
142{
Hans-Werner Hilse9a755512015-06-11 11:29:18 +0200143 stack_t stack = {
144 .ss_flags = 0,
145 .ss_sp = sig_stack,
YiFei Zhu558f9b22021-04-20 00:56:10 -0500146 .ss_size = size
Hans-Werner Hilse9a755512015-06-11 11:29:18 +0200147 };
Gennady Sharapov0805d892006-01-08 01:01:29 -0800148
Jeff Dikeba180fd2007-10-16 01:27:00 -0700149 if (sigaltstack(&stack, NULL) != 0)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800150 panic("enabling signal stack failed, errno = %d\n", errno);
151}
152
Johannes Berg92dcd3d2020-12-02 20:58:06 +0100153static void sigusr1_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
154{
155 uml_pm_wake();
156}
157
158void register_pm_wake_signal(void)
159{
160 set_handler(SIGUSR1);
161}
162
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200163static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
Al Viro00361682011-08-18 20:04:39 +0100164 [SIGSEGV] = sig_handler,
165 [SIGBUS] = sig_handler,
166 [SIGILL] = sig_handler,
167 [SIGFPE] = sig_handler,
168 [SIGTRAP] = sig_handler,
169
170 [SIGIO] = sig_handler,
171 [SIGWINCH] = sig_handler,
Johannes Berg92dcd3d2020-12-02 20:58:06 +0100172 [SIGALRM] = timer_alarm_handler,
173
174 [SIGUSR1] = sigusr1_handler,
Al Viro00361682011-08-18 20:04:39 +0100175};
Jeff Dike4b84c692006-09-25 23:33:04 -0700176
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200177static void hard_handler(int sig, siginfo_t *si, void *p)
Jeff Dikec14b8492007-05-10 22:22:34 -0700178{
Krzysztof Mazur4d1a5352017-11-15 11:12:39 +0100179 ucontext_t *uc = p;
Al Viro248b74c2011-08-18 20:05:09 +0100180 mcontext_t *mc = &uc->uc_mcontext;
Jeff Dike508a9272007-09-18 22:46:49 -0700181 unsigned long pending = 1UL << sig;
Jeff Dikec14b8492007-05-10 22:22:34 -0700182
183 do {
184 int nested, bail;
185
186 /*
187 * pending comes back with one bit set for each
188 * interrupt that arrived while setting up the stack,
189 * plus a bit for this interrupt, plus the zero bit is
190 * set if this is a nested interrupt.
191 * If bail is true, then we interrupted another
192 * handler setting up the stack. In this case, we
193 * have to return, and the upper handler will deal
194 * with this interrupt.
195 */
Jeff Dike508a9272007-09-18 22:46:49 -0700196 bail = to_irq_stack(&pending);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700197 if (bail)
Jeff Dikec14b8492007-05-10 22:22:34 -0700198 return;
199
200 nested = pending & 1;
201 pending &= ~1;
202
Jeff Dikeba180fd2007-10-16 01:27:00 -0700203 while ((sig = ffs(pending)) != 0){
Jeff Dikec14b8492007-05-10 22:22:34 -0700204 sig--;
205 pending &= ~(1 << sig);
Richard Weinberger9a8c1352013-07-19 11:31:36 +0200206 (*handlers[sig])(sig, (struct siginfo *)si, mc);
Jeff Dikec14b8492007-05-10 22:22:34 -0700207 }
208
Jeff Dikeba180fd2007-10-16 01:27:00 -0700209 /*
210 * Again, pending comes back with a mask of signals
Jeff Dikec14b8492007-05-10 22:22:34 -0700211 * that arrived while tearing down the stack. If this
212 * is non-zero, we just go back, set up the stack
213 * again, and handle the new interrupts.
214 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700215 if (!nested)
Jeff Dikec14b8492007-05-10 22:22:34 -0700216 pending = from_irq_stack(nested);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700217 } while (pending);
Jeff Dikec14b8492007-05-10 22:22:34 -0700218}
219
Al Viro00361682011-08-18 20:04:39 +0100220void set_handler(int sig)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800221{
222 struct sigaction action;
Al Viroe87df982011-08-18 20:04:29 +0100223 int flags = SA_SIGINFO | SA_ONSTACK;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800224 sigset_t sig_mask;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800225
Al Viro7eb12252011-08-18 20:03:39 +0100226 action.sa_sigaction = hard_handler;
Jeff Dike4b84c692006-09-25 23:33:04 -0700227
Al Viroe87df982011-08-18 20:04:29 +0100228 /* block irq ones */
Gennady Sharapov0805d892006-01-08 01:01:29 -0800229 sigemptyset(&action.sa_mask);
Al Viroe87df982011-08-18 20:04:29 +0100230 sigaddset(&action.sa_mask, SIGIO);
231 sigaddset(&action.sa_mask, SIGWINCH);
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000232 sigaddset(&action.sa_mask, SIGALRM);
Jeff Dike4b84c692006-09-25 23:33:04 -0700233
Jeff Dikee6a2d1f2008-02-04 22:31:13 -0800234 if (sig == SIGSEGV)
235 flags |= SA_NODEFER;
236
Al Viroe87df982011-08-18 20:04:29 +0100237 if (sigismember(&action.sa_mask, sig))
238 flags |= SA_RESTART; /* if it's an irq signal */
239
240 action.sa_flags = flags;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800241 action.sa_restorer = NULL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700242 if (sigaction(sig, &action, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800243 panic("sigaction failed - errno = %d\n", errno);
244
245 sigemptyset(&sig_mask);
246 sigaddset(&sig_mask, sig);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700247 if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800248 panic("sigprocmask failed - errno = %d\n", errno);
Gennady Sharapov0805d892006-01-08 01:01:29 -0800249}
250
Johannes Berga374b7c2020-12-02 20:58:07 +0100251void send_sigio_to_self(void)
252{
253 kill(os_getpid(), SIGIO);
254}
255
Gennady Sharapov0805d892006-01-08 01:01:29 -0800256int change_sig(int signal, int on)
257{
Jeff Dikecfef8f32008-02-04 22:31:16 -0800258 sigset_t sigset;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800259
260 sigemptyset(&sigset);
261 sigaddset(&sigset, signal);
Jeff Dikecfef8f32008-02-04 22:31:16 -0800262 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
WANG Congc9a30722008-02-04 22:30:35 -0800263 return -errno;
Jeff Dikecfef8f32008-02-04 22:31:16 -0800264
265 return 0;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800266}
267
Gennady Sharapov0805d892006-01-08 01:01:29 -0800268void block_signals(void)
269{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800270 signals_enabled = 0;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700271 /*
272 * This must return with signals disabled, so this barrier
Jeff Dike53b17332006-11-02 22:07:22 -0800273 * ensures that writes are flushed out before the return.
274 * This might matter if gcc figures out how to inline this and
275 * decides to shuffle this code into the caller.
276 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800277 barrier();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800278}
279
280void unblock_signals(void)
281{
Jeff Dike1d7173b2006-01-18 17:42:49 -0800282 int save_pending;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800283
Jeff Dikeba180fd2007-10-16 01:27:00 -0700284 if (signals_enabled == 1)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800285 return;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800286
Johannes Berg0dafcbe2019-08-23 13:16:23 +0200287 signals_enabled = 1;
Johannes Berg11385532020-12-13 22:18:18 +0100288#ifdef UML_CONFIG_UML_TIME_TRAVEL_SUPPORT
289 deliver_time_travel_irqs();
290#endif
Johannes Berg0dafcbe2019-08-23 13:16:23 +0200291
Jeff Dikeba180fd2007-10-16 01:27:00 -0700292 /*
293 * We loop because the IRQ handler returns with interrupts off. So,
Jeff Dike1d7173b2006-01-18 17:42:49 -0800294 * interrupts may have arrived and we need to re-enable them and
Jeff Dikecfef8f32008-02-04 22:31:16 -0800295 * recheck signals_pending.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800296 */
Jeff Dike5134d8f2008-02-08 04:22:08 -0800297 while (1) {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700298 /*
299 * Save and reset save_pending after enabling signals. This
Jeff Dikecfef8f32008-02-04 22:31:16 -0800300 * way, signals_pending won't be changed while we're reading it.
Johannes Berg0dafcbe2019-08-23 13:16:23 +0200301 *
Jeff Dikecfef8f32008-02-04 22:31:16 -0800302 * Setting signals_enabled and reading signals_pending must
Johannes Berg0dafcbe2019-08-23 13:16:23 +0200303 * happen in this order, so have the barrier here.
Jeff Dike53b17332006-11-02 22:07:22 -0800304 */
Jeff Dikefce8c412008-02-04 22:31:09 -0800305 barrier();
Jeff Dike53b17332006-11-02 22:07:22 -0800306
Jeff Dikecfef8f32008-02-04 22:31:16 -0800307 save_pending = signals_pending;
Jeff Dikefce8c412008-02-04 22:31:09 -0800308 if (save_pending == 0)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800309 return;
310
Jeff Dikecfef8f32008-02-04 22:31:16 -0800311 signals_pending = 0;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800312
Jeff Dikeba180fd2007-10-16 01:27:00 -0700313 /*
314 * We have pending interrupts, so disable signals, as the
Jeff Dike1d7173b2006-01-18 17:42:49 -0800315 * handlers expect them off when they are called. They will
Johannes Berg0dafcbe2019-08-23 13:16:23 +0200316 * be enabled again above. We need to trace this, as we're
317 * expected to be enabling interrupts already, but any more
318 * tracing that happens inside the handlers we call for the
319 * pending signals will mess up the tracing state.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800320 */
Jeff Dike1d7173b2006-01-18 17:42:49 -0800321 signals_enabled = 0;
Johannes Berg0dafcbe2019-08-23 13:16:23 +0200322 um_trace_signals_off();
Jeff Dike1d7173b2006-01-18 17:42:49 -0800323
Jeff Dikeba180fd2007-10-16 01:27:00 -0700324 /*
325 * Deal with SIGIO first because the alarm handler might
Jeff Dike1d7173b2006-01-18 17:42:49 -0800326 * schedule, leaving the pending SIGIO stranded until we come
327 * back here.
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200328 *
329 * SIGIO's handler doesn't use siginfo or mcontext,
330 * so they can be NULL.
Jeff Dike1d7173b2006-01-18 17:42:49 -0800331 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700332 if (save_pending & SIGIO_MASK)
Martin Pärteld3c1cfc2012-08-02 00:49:17 +0200333 sig_handler_common(SIGIO, NULL, NULL);
Jeff Dike1d7173b2006-01-18 17:42:49 -0800334
Anton Ivanovd5e3f5c2015-12-21 11:28:02 +0000335 /* Do not reenter the handler */
336
337 if ((save_pending & SIGALRM_MASK) && (!(signals_active & SIGALRM_MASK)))
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000338 timer_real_alarm_handler(NULL);
Anton Ivanovd5e3f5c2015-12-21 11:28:02 +0000339
340 /* Rerun the loop only if there is still pending SIGIO and not in TIMER handler */
341
342 if (!(signals_pending & SIGIO_MASK) && (signals_active & SIGALRM_MASK))
343 return;
344
Johannes Berg0dafcbe2019-08-23 13:16:23 +0200345 /* Re-enable signals and trace that we're doing so. */
346 um_trace_signals_on();
347 signals_enabled = 1;
Jeff Dike1d7173b2006-01-18 17:42:49 -0800348 }
Gennady Sharapov0805d892006-01-08 01:01:29 -0800349}
350
Johannes Bergbbe33502021-08-31 09:11:15 +0200351int um_set_signals(int enable)
Gennady Sharapov0805d892006-01-08 01:01:29 -0800352{
Gennady Sharapov0805d892006-01-08 01:01:29 -0800353 int ret;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700354 if (signals_enabled == enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800355 return enable;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800356
Jeff Dike1d7173b2006-01-18 17:42:49 -0800357 ret = signals_enabled;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700358 if (enable)
Jeff Dike1d7173b2006-01-18 17:42:49 -0800359 unblock_signals();
360 else block_signals();
Gennady Sharapov0805d892006-01-08 01:01:29 -0800361
Jeff Dike1d7173b2006-01-18 17:42:49 -0800362 return ret;
Gennady Sharapov0805d892006-01-08 01:01:29 -0800363}
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200364
Johannes Bergbbe33502021-08-31 09:11:15 +0200365int um_set_signals_trace(int enable)
Johannes Berg0dafcbe2019-08-23 13:16:23 +0200366{
367 int ret;
368 if (signals_enabled == enable)
369 return enable;
370
371 ret = signals_enabled;
372 if (enable)
373 unblock_signals_trace();
374 else
375 block_signals_trace();
376
377 return ret;
378}
379
Johannes Bergd6b399a2021-03-05 13:19:56 +0100380#ifdef UML_CONFIG_UML_TIME_TRAVEL_SUPPORT
381void mark_sigio_pending(void)
382{
383 signals_pending |= SIGIO_MASK;
384}
385
386void block_signals_hard(void)
387{
388 if (signals_blocked)
389 return;
390 signals_blocked = 1;
391 barrier();
392}
393
394void unblock_signals_hard(void)
395{
396 if (!signals_blocked)
397 return;
398 /* Must be set to 0 before we check the pending bits etc. */
399 signals_blocked = 0;
400 barrier();
401
402 if (signals_pending && signals_enabled) {
403 /* this is a bit inefficient, but that's not really important */
404 block_signals();
405 unblock_signals();
406 } else if (signals_pending & SIGIO_MASK) {
407 /* we need to run time-travel handlers even if not enabled */
408 sigio_run_timetravel_handlers();
409 }
410}
411#endif
412
Richard Weinbergerf72c22e2013-09-23 17:38:02 +0200413int os_is_signal_stack(void)
414{
415 stack_t ss;
416 sigaltstack(NULL, &ss);
417
418 return ss.ss_flags & SS_ONSTACK;
419}