1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
4 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
5 * Copyright (C) 2004 PathScale, Inc
6 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
7 */
8
9#include <stdlib.h>
10#include <stdarg.h>
11#include <errno.h>
12#include <signal.h>
13#include <string.h>
14#include <strings.h>
15#include <as-layout.h>
16#include <kern_util.h>
17#include <os.h>
18#include <sysdep/mcontext.h>
19#include <um_malloc.h>
20#include <sys/ucontext.h>
21
22void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *) = {
23	[SIGTRAP]	= relay_signal,
24	[SIGFPE]	= relay_signal,
25	[SIGILL]	= relay_signal,
26	[SIGWINCH]	= winch,
27	[SIGBUS]	= bus_handler,
28	[SIGSEGV]	= segv_handler,
29	[SIGIO]		= sigio_handler,
30};
31
32static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc)
33{
34	struct uml_pt_regs r;
35	int save_errno = errno;
36
37	r.is_user = 0;
38	if (sig == SIGSEGV) {
39		/* For segfaults, we want the data from the sigcontext. */
40		get_regs_from_mc(&r, mc);
41		GET_FAULTINFO_FROM_MC(r.faultinfo, mc);
42	}
43
44	/* enable signals if sig isn't IRQ signal */
45	if ((sig != SIGIO) && (sig != SIGWINCH))
46		unblock_signals_trace();
47
48	(*sig_info[sig])(sig, si, &r);
49
50	errno = save_errno;
51}
52
53/*
54 * These are the asynchronous signals.  SIGPROF is excluded because we want to
55 * be able to profile all of UML, not just the non-critical sections.  If
56 * profiling is not thread-safe, then that is not my problem.  We can disable
57 * profiling when SMP is enabled in that case.
58 */
59#define SIGIO_BIT 0
60#define SIGIO_MASK (1 << SIGIO_BIT)
61
62#define SIGALRM_BIT 1
63#define SIGALRM_MASK (1 << SIGALRM_BIT)
64
65static int signals_enabled;
66static unsigned int signals_pending;
67static unsigned int signals_active = 0;
68
69void sig_handler(int sig, struct siginfo *si, mcontext_t *mc)
70{
71	int enabled;
72
73	enabled = signals_enabled;
74	if (!enabled && (sig == SIGIO)) {
75		signals_pending |= SIGIO_MASK;
76		return;
77	}
78
79	block_signals_trace();
80
81	sig_handler_common(sig, si, mc);
82
83	set_signals_trace(enabled);
84}
85
86static void timer_real_alarm_handler(mcontext_t *mc)
87{
88	struct uml_pt_regs regs;
89
90	if (mc != NULL)
91		get_regs_from_mc(&regs, mc);
92	else
93		memset(&regs, 0, sizeof(regs));
94	timer_handler(SIGALRM, NULL, &regs);
95}
96
97void timer_alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
98{
99	int enabled;
100
101	enabled = signals_enabled;
102	if (!signals_enabled) {
103		signals_pending |= SIGALRM_MASK;
104		return;
105	}
106
107	block_signals_trace();
108
109	signals_active |= SIGALRM_MASK;
110
111	timer_real_alarm_handler(mc);
112
113	signals_active &= ~SIGALRM_MASK;
114
115	set_signals_trace(enabled);
116}
117
118void deliver_alarm(void) {
119    timer_alarm_handler(SIGALRM, NULL, NULL);
120}
121
122void timer_set_signal_handler(void)
123{
124	set_handler(SIGALRM);
125}
126
127void set_sigstack(void *sig_stack, int size)
128{
129	stack_t stack = {
130		.ss_flags = 0,
131		.ss_sp = sig_stack,
132		.ss_size = size - sizeof(void *)
133	};
134
135	if (sigaltstack(&stack, NULL) != 0)
136		panic("enabling signal stack failed, errno = %d\n", errno);
137}
138
139static void sigusr1_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
140{
141	uml_pm_wake();
142}
143
144void register_pm_wake_signal(void)
145{
146	set_handler(SIGUSR1);
147}
148
149static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
150	[SIGSEGV] = sig_handler,
151	[SIGBUS] = sig_handler,
152	[SIGILL] = sig_handler,
153	[SIGFPE] = sig_handler,
154	[SIGTRAP] = sig_handler,
155
156	[SIGIO] = sig_handler,
157	[SIGWINCH] = sig_handler,
158	[SIGALRM] = timer_alarm_handler,
159
160	[SIGUSR1] = sigusr1_handler,
161};
162
163static void hard_handler(int sig, siginfo_t *si, void *p)
164{
165	ucontext_t *uc = p;
166	mcontext_t *mc = &uc->uc_mcontext;
167	unsigned long pending = 1UL << sig;
168
169	do {
170		int nested, bail;
171
172		/*
173		 * pending comes back with one bit set for each
174		 * interrupt that arrived while setting up the stack,
175		 * plus a bit for this interrupt, plus the zero bit is
176		 * set if this is a nested interrupt.
177		 * If bail is true, then we interrupted another
178		 * handler setting up the stack.  In this case, we
179		 * have to return, and the upper handler will deal
180		 * with this interrupt.
181		 */
182		bail = to_irq_stack(&pending);
183		if (bail)
184			return;
185
186		nested = pending & 1;
187		pending &= ~1;
188
189		while ((sig = ffs(pending)) != 0){
190			sig--;
191			pending &= ~(1 << sig);
192			(*handlers[sig])(sig, (struct siginfo *)si, mc);
193		}
194
195		/*
196		 * Again, pending comes back with a mask of signals
197		 * that arrived while tearing down the stack.  If this
198		 * is non-zero, we just go back, set up the stack
199		 * again, and handle the new interrupts.
200		 */
201		if (!nested)
202			pending = from_irq_stack(nested);
203	} while (pending);
204}
205
206void set_handler(int sig)
207{
208	struct sigaction action;
209	int flags = SA_SIGINFO | SA_ONSTACK;
210	sigset_t sig_mask;
211
212	action.sa_sigaction = hard_handler;
213
214	/* block irq ones */
215	sigemptyset(&action.sa_mask);
216	sigaddset(&action.sa_mask, SIGIO);
217	sigaddset(&action.sa_mask, SIGWINCH);
218	sigaddset(&action.sa_mask, SIGALRM);
219
220	if (sig == SIGSEGV)
221		flags |= SA_NODEFER;
222
223	if (sigismember(&action.sa_mask, sig))
224		flags |= SA_RESTART; /* if it's an irq signal */
225
226	action.sa_flags = flags;
227	action.sa_restorer = NULL;
228	if (sigaction(sig, &action, NULL) < 0)
229		panic("sigaction failed - errno = %d\n", errno);
230
231	sigemptyset(&sig_mask);
232	sigaddset(&sig_mask, sig);
233	if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
234		panic("sigprocmask failed - errno = %d\n", errno);
235}
236
237int change_sig(int signal, int on)
238{
239	sigset_t sigset;
240
241	sigemptyset(&sigset);
242	sigaddset(&sigset, signal);
243	if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
244		return -errno;
245
246	return 0;
247}
248
249void block_signals(void)
250{
251	signals_enabled = 0;
252	/*
253	 * This must return with signals disabled, so this barrier
254	 * ensures that writes are flushed out before the return.
255	 * This might matter if gcc figures out how to inline this and
256	 * decides to shuffle this code into the caller.
257	 */
258	barrier();
259}
260
261void unblock_signals(void)
262{
263	int save_pending;
264
265	if (signals_enabled == 1)
266		return;
267
268	signals_enabled = 1;
269
270	/*
271	 * We loop because the IRQ handler returns with interrupts off.  So,
272	 * interrupts may have arrived and we need to re-enable them and
273	 * recheck signals_pending.
274	 */
275	while (1) {
276		/*
277		 * Save and reset save_pending after enabling signals.  This
278		 * way, signals_pending won't be changed while we're reading it.
279		 *
280		 * Setting signals_enabled and reading signals_pending must
281		 * happen in this order, so have the barrier here.
282		 */
283		barrier();
284
285		save_pending = signals_pending;
286		if (save_pending == 0)
287			return;
288
289		signals_pending = 0;
290
291		/*
292		 * We have pending interrupts, so disable signals, as the
293		 * handlers expect them off when they are called.  They will
294		 * be enabled again above. We need to trace this, as we're
295		 * expected to be enabling interrupts already, but any more
296		 * tracing that happens inside the handlers we call for the
297		 * pending signals will mess up the tracing state.
298		 */
299		signals_enabled = 0;
300		um_trace_signals_off();
301
302		/*
303		 * Deal with SIGIO first because the alarm handler might
304		 * schedule, leaving the pending SIGIO stranded until we come
305		 * back here.
306		 *
307		 * SIGIO's handler doesn't use siginfo or mcontext,
308		 * so they can be NULL.
309		 */
310		if (save_pending & SIGIO_MASK)
311			sig_handler_common(SIGIO, NULL, NULL);
312
313		/* Do not reenter the handler */
314
315		if ((save_pending & SIGALRM_MASK) && (!(signals_active & SIGALRM_MASK)))
316			timer_real_alarm_handler(NULL);
317
318		/* Rerun the loop only if there is still pending SIGIO and not in TIMER handler */
319
320		if (!(signals_pending & SIGIO_MASK) && (signals_active & SIGALRM_MASK))
321			return;
322
323		/* Re-enable signals and trace that we're doing so. */
324		um_trace_signals_on();
325		signals_enabled = 1;
326	}
327}
328
329int get_signals(void)
330{
331	return signals_enabled;
332}
333
334int set_signals(int enable)
335{
336	int ret;
337	if (signals_enabled == enable)
338		return enable;
339
340	ret = signals_enabled;
341	if (enable)
342		unblock_signals();
343	else block_signals();
344
345	return ret;
346}
347
348int set_signals_trace(int enable)
349{
350	int ret;
351	if (signals_enabled == enable)
352		return enable;
353
354	ret = signals_enabled;
355	if (enable)
356		unblock_signals_trace();
357	else
358		block_signals_trace();
359
360	return ret;
361}
362
363int os_is_signal_stack(void)
364{
365	stack_t ss;
366	sigaltstack(NULL, &ss);
367
368	return ss.ss_flags & SS_ONSTACK;
369}
370