xref: /linux/arch/um/os-Linux/signal.c (revision b8d312aa075f33282565467662c4628dae0a2aff)
1 /*
2  * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
3  * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
4  * Copyright (C) 2004 PathScale, Inc
5  * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
6  * Licensed under the GPL
7  */
8 
9 #include <stdlib.h>
10 #include <stdarg.h>
11 #include <errno.h>
12 #include <signal.h>
13 #include <strings.h>
14 #include <as-layout.h>
15 #include <kern_util.h>
16 #include <os.h>
17 #include <sysdep/mcontext.h>
18 #include <um_malloc.h>
19 #include <sys/ucontext.h>
20 
21 void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *) = {
22 	[SIGTRAP]	= relay_signal,
23 	[SIGFPE]	= relay_signal,
24 	[SIGILL]	= relay_signal,
25 	[SIGWINCH]	= winch,
26 	[SIGBUS]	= bus_handler,
27 	[SIGSEGV]	= segv_handler,
28 	[SIGIO]		= sigio_handler,
29 	[SIGALRM]	= timer_handler
30 };
31 
32 static 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) && (sig != SIGALRM))
46 		unblock_signals();
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 
65 static int signals_enabled;
66 static unsigned int signals_pending;
67 static unsigned int signals_active = 0;
68 
69 void 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();
80 
81 	sig_handler_common(sig, si, mc);
82 
83 	set_signals(enabled);
84 }
85 
86 static 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 	timer_handler(SIGALRM, NULL, &regs);
93 }
94 
95 void timer_alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
96 {
97 	int enabled;
98 
99 	enabled = signals_enabled;
100 	if (!signals_enabled) {
101 		signals_pending |= SIGALRM_MASK;
102 		return;
103 	}
104 
105 	block_signals();
106 
107 	signals_active |= SIGALRM_MASK;
108 
109 	timer_real_alarm_handler(mc);
110 
111 	signals_active &= ~SIGALRM_MASK;
112 
113 	set_signals(enabled);
114 }
115 
116 void deliver_alarm(void) {
117     timer_alarm_handler(SIGALRM, NULL, NULL);
118 }
119 
120 void timer_set_signal_handler(void)
121 {
122 	set_handler(SIGALRM);
123 }
124 
125 void set_sigstack(void *sig_stack, int size)
126 {
127 	stack_t stack = {
128 		.ss_flags = 0,
129 		.ss_sp = sig_stack,
130 		.ss_size = size - sizeof(void *)
131 	};
132 
133 	if (sigaltstack(&stack, NULL) != 0)
134 		panic("enabling signal stack failed, errno = %d\n", errno);
135 }
136 
137 static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
138 	[SIGSEGV] = sig_handler,
139 	[SIGBUS] = sig_handler,
140 	[SIGILL] = sig_handler,
141 	[SIGFPE] = sig_handler,
142 	[SIGTRAP] = sig_handler,
143 
144 	[SIGIO] = sig_handler,
145 	[SIGWINCH] = sig_handler,
146 	[SIGALRM] = timer_alarm_handler
147 };
148 
149 static void hard_handler(int sig, siginfo_t *si, void *p)
150 {
151 	ucontext_t *uc = p;
152 	mcontext_t *mc = &uc->uc_mcontext;
153 	unsigned long pending = 1UL << sig;
154 
155 	do {
156 		int nested, bail;
157 
158 		/*
159 		 * pending comes back with one bit set for each
160 		 * interrupt that arrived while setting up the stack,
161 		 * plus a bit for this interrupt, plus the zero bit is
162 		 * set if this is a nested interrupt.
163 		 * If bail is true, then we interrupted another
164 		 * handler setting up the stack.  In this case, we
165 		 * have to return, and the upper handler will deal
166 		 * with this interrupt.
167 		 */
168 		bail = to_irq_stack(&pending);
169 		if (bail)
170 			return;
171 
172 		nested = pending & 1;
173 		pending &= ~1;
174 
175 		while ((sig = ffs(pending)) != 0){
176 			sig--;
177 			pending &= ~(1 << sig);
178 			(*handlers[sig])(sig, (struct siginfo *)si, mc);
179 		}
180 
181 		/*
182 		 * Again, pending comes back with a mask of signals
183 		 * that arrived while tearing down the stack.  If this
184 		 * is non-zero, we just go back, set up the stack
185 		 * again, and handle the new interrupts.
186 		 */
187 		if (!nested)
188 			pending = from_irq_stack(nested);
189 	} while (pending);
190 }
191 
192 void set_handler(int sig)
193 {
194 	struct sigaction action;
195 	int flags = SA_SIGINFO | SA_ONSTACK;
196 	sigset_t sig_mask;
197 
198 	action.sa_sigaction = hard_handler;
199 
200 	/* block irq ones */
201 	sigemptyset(&action.sa_mask);
202 	sigaddset(&action.sa_mask, SIGIO);
203 	sigaddset(&action.sa_mask, SIGWINCH);
204 	sigaddset(&action.sa_mask, SIGALRM);
205 
206 	if (sig == SIGSEGV)
207 		flags |= SA_NODEFER;
208 
209 	if (sigismember(&action.sa_mask, sig))
210 		flags |= SA_RESTART; /* if it's an irq signal */
211 
212 	action.sa_flags = flags;
213 	action.sa_restorer = NULL;
214 	if (sigaction(sig, &action, NULL) < 0)
215 		panic("sigaction failed - errno = %d\n", errno);
216 
217 	sigemptyset(&sig_mask);
218 	sigaddset(&sig_mask, sig);
219 	if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
220 		panic("sigprocmask failed - errno = %d\n", errno);
221 }
222 
223 int change_sig(int signal, int on)
224 {
225 	sigset_t sigset;
226 
227 	sigemptyset(&sigset);
228 	sigaddset(&sigset, signal);
229 	if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
230 		return -errno;
231 
232 	return 0;
233 }
234 
235 void block_signals(void)
236 {
237 	signals_enabled = 0;
238 	/*
239 	 * This must return with signals disabled, so this barrier
240 	 * ensures that writes are flushed out before the return.
241 	 * This might matter if gcc figures out how to inline this and
242 	 * decides to shuffle this code into the caller.
243 	 */
244 	barrier();
245 }
246 
247 void unblock_signals(void)
248 {
249 	int save_pending;
250 
251 	if (signals_enabled == 1)
252 		return;
253 
254 	/*
255 	 * We loop because the IRQ handler returns with interrupts off.  So,
256 	 * interrupts may have arrived and we need to re-enable them and
257 	 * recheck signals_pending.
258 	 */
259 	while (1) {
260 		/*
261 		 * Save and reset save_pending after enabling signals.  This
262 		 * way, signals_pending won't be changed while we're reading it.
263 		 */
264 		signals_enabled = 1;
265 
266 		/*
267 		 * Setting signals_enabled and reading signals_pending must
268 		 * happen in this order.
269 		 */
270 		barrier();
271 
272 		save_pending = signals_pending;
273 		if (save_pending == 0)
274 			return;
275 
276 		signals_pending = 0;
277 
278 		/*
279 		 * We have pending interrupts, so disable signals, as the
280 		 * handlers expect them off when they are called.  They will
281 		 * be enabled again above.
282 		 */
283 
284 		signals_enabled = 0;
285 
286 		/*
287 		 * Deal with SIGIO first because the alarm handler might
288 		 * schedule, leaving the pending SIGIO stranded until we come
289 		 * back here.
290 		 *
291 		 * SIGIO's handler doesn't use siginfo or mcontext,
292 		 * so they can be NULL.
293 		 */
294 		if (save_pending & SIGIO_MASK)
295 			sig_handler_common(SIGIO, NULL, NULL);
296 
297 		/* Do not reenter the handler */
298 
299 		if ((save_pending & SIGALRM_MASK) && (!(signals_active & SIGALRM_MASK)))
300 			timer_real_alarm_handler(NULL);
301 
302 		/* Rerun the loop only if there is still pending SIGIO and not in TIMER handler */
303 
304 		if (!(signals_pending & SIGIO_MASK) && (signals_active & SIGALRM_MASK))
305 			return;
306 
307 	}
308 }
309 
310 int get_signals(void)
311 {
312 	return signals_enabled;
313 }
314 
315 int set_signals(int enable)
316 {
317 	int ret;
318 	if (signals_enabled == enable)
319 		return enable;
320 
321 	ret = signals_enabled;
322 	if (enable)
323 		unblock_signals();
324 	else block_signals();
325 
326 	return ret;
327 }
328 
329 int os_is_signal_stack(void)
330 {
331 	stack_t ss;
332 	sigaltstack(NULL, &ss);
333 
334 	return ss.ss_flags & SS_ONSTACK;
335 }
336