xref: /openbmc/linux/arch/um/os-Linux/signal.c (revision a8da474e)
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 
19 void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *) = {
20 	[SIGTRAP]	= relay_signal,
21 	[SIGFPE]	= relay_signal,
22 	[SIGILL]	= relay_signal,
23 	[SIGWINCH]	= winch,
24 	[SIGBUS]	= bus_handler,
25 	[SIGSEGV]	= segv_handler,
26 	[SIGIO]		= sigio_handler,
27 	[SIGALRM]	= timer_handler
28 };
29 
30 static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc)
31 {
32 	struct uml_pt_regs r;
33 	int save_errno = errno;
34 
35 	r.is_user = 0;
36 	if (sig == SIGSEGV) {
37 		/* For segfaults, we want the data from the sigcontext. */
38 		get_regs_from_mc(&r, mc);
39 		GET_FAULTINFO_FROM_MC(r.faultinfo, mc);
40 	}
41 
42 	/* enable signals if sig isn't IRQ signal */
43 	if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGALRM))
44 		unblock_signals();
45 
46 	(*sig_info[sig])(sig, si, &r);
47 
48 	errno = save_errno;
49 }
50 
51 /*
52  * These are the asynchronous signals.  SIGPROF is excluded because we want to
53  * be able to profile all of UML, not just the non-critical sections.  If
54  * profiling is not thread-safe, then that is not my problem.  We can disable
55  * profiling when SMP is enabled in that case.
56  */
57 #define SIGIO_BIT 0
58 #define SIGIO_MASK (1 << SIGIO_BIT)
59 
60 #define SIGALRM_BIT 1
61 #define SIGALRM_MASK (1 << SIGALRM_BIT)
62 
63 static int signals_enabled;
64 static unsigned int signals_pending;
65 
66 void sig_handler(int sig, struct siginfo *si, mcontext_t *mc)
67 {
68 	int enabled;
69 
70 	enabled = signals_enabled;
71 	if (!enabled && (sig == SIGIO)) {
72 		signals_pending |= SIGIO_MASK;
73 		return;
74 	}
75 
76 	block_signals();
77 
78 	sig_handler_common(sig, si, mc);
79 
80 	set_signals(enabled);
81 }
82 
83 static void timer_real_alarm_handler(mcontext_t *mc)
84 {
85 	struct uml_pt_regs regs;
86 
87 	if (mc != NULL)
88 		get_regs_from_mc(&regs, mc);
89 	timer_handler(SIGALRM, NULL, &regs);
90 }
91 
92 void timer_alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
93 {
94 	int enabled;
95 
96 	enabled = signals_enabled;
97 	if (!signals_enabled) {
98 		signals_pending |= SIGALRM_MASK;
99 		return;
100 	}
101 
102 	block_signals();
103 
104 	timer_real_alarm_handler(mc);
105 	set_signals(enabled);
106 }
107 
108 void deliver_alarm(void) {
109     timer_alarm_handler(SIGALRM, NULL, NULL);
110 }
111 
112 void timer_set_signal_handler(void)
113 {
114 	set_handler(SIGALRM);
115 }
116 
117 void set_sigstack(void *sig_stack, int size)
118 {
119 	stack_t stack = {
120 		.ss_flags = 0,
121 		.ss_sp = sig_stack,
122 		.ss_size = size - sizeof(void *)
123 	};
124 
125 	if (sigaltstack(&stack, NULL) != 0)
126 		panic("enabling signal stack failed, errno = %d\n", errno);
127 }
128 
129 static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
130 	[SIGSEGV] = sig_handler,
131 	[SIGBUS] = sig_handler,
132 	[SIGILL] = sig_handler,
133 	[SIGFPE] = sig_handler,
134 	[SIGTRAP] = sig_handler,
135 
136 	[SIGIO] = sig_handler,
137 	[SIGWINCH] = sig_handler,
138 	[SIGALRM] = timer_alarm_handler
139 };
140 
141 static void hard_handler(int sig, siginfo_t *si, void *p)
142 {
143 	struct ucontext *uc = p;
144 	mcontext_t *mc = &uc->uc_mcontext;
145 	unsigned long pending = 1UL << sig;
146 
147 	do {
148 		int nested, bail;
149 
150 		/*
151 		 * pending comes back with one bit set for each
152 		 * interrupt that arrived while setting up the stack,
153 		 * plus a bit for this interrupt, plus the zero bit is
154 		 * set if this is a nested interrupt.
155 		 * If bail is true, then we interrupted another
156 		 * handler setting up the stack.  In this case, we
157 		 * have to return, and the upper handler will deal
158 		 * with this interrupt.
159 		 */
160 		bail = to_irq_stack(&pending);
161 		if (bail)
162 			return;
163 
164 		nested = pending & 1;
165 		pending &= ~1;
166 
167 		while ((sig = ffs(pending)) != 0){
168 			sig--;
169 			pending &= ~(1 << sig);
170 			(*handlers[sig])(sig, (struct siginfo *)si, mc);
171 		}
172 
173 		/*
174 		 * Again, pending comes back with a mask of signals
175 		 * that arrived while tearing down the stack.  If this
176 		 * is non-zero, we just go back, set up the stack
177 		 * again, and handle the new interrupts.
178 		 */
179 		if (!nested)
180 			pending = from_irq_stack(nested);
181 	} while (pending);
182 }
183 
184 void set_handler(int sig)
185 {
186 	struct sigaction action;
187 	int flags = SA_SIGINFO | SA_ONSTACK;
188 	sigset_t sig_mask;
189 
190 	action.sa_sigaction = hard_handler;
191 
192 	/* block irq ones */
193 	sigemptyset(&action.sa_mask);
194 	sigaddset(&action.sa_mask, SIGIO);
195 	sigaddset(&action.sa_mask, SIGWINCH);
196 	sigaddset(&action.sa_mask, SIGALRM);
197 
198 	if (sig == SIGSEGV)
199 		flags |= SA_NODEFER;
200 
201 	if (sigismember(&action.sa_mask, sig))
202 		flags |= SA_RESTART; /* if it's an irq signal */
203 
204 	action.sa_flags = flags;
205 	action.sa_restorer = NULL;
206 	if (sigaction(sig, &action, NULL) < 0)
207 		panic("sigaction failed - errno = %d\n", errno);
208 
209 	sigemptyset(&sig_mask);
210 	sigaddset(&sig_mask, sig);
211 	if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
212 		panic("sigprocmask failed - errno = %d\n", errno);
213 }
214 
215 int change_sig(int signal, int on)
216 {
217 	sigset_t sigset;
218 
219 	sigemptyset(&sigset);
220 	sigaddset(&sigset, signal);
221 	if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
222 		return -errno;
223 
224 	return 0;
225 }
226 
227 void block_signals(void)
228 {
229 	signals_enabled = 0;
230 	/*
231 	 * This must return with signals disabled, so this barrier
232 	 * ensures that writes are flushed out before the return.
233 	 * This might matter if gcc figures out how to inline this and
234 	 * decides to shuffle this code into the caller.
235 	 */
236 	barrier();
237 }
238 
239 void unblock_signals(void)
240 {
241 	int save_pending;
242 
243 	if (signals_enabled == 1)
244 		return;
245 
246 	/*
247 	 * We loop because the IRQ handler returns with interrupts off.  So,
248 	 * interrupts may have arrived and we need to re-enable them and
249 	 * recheck signals_pending.
250 	 */
251 	while (1) {
252 		/*
253 		 * Save and reset save_pending after enabling signals.  This
254 		 * way, signals_pending won't be changed while we're reading it.
255 		 */
256 		signals_enabled = 1;
257 
258 		/*
259 		 * Setting signals_enabled and reading signals_pending must
260 		 * happen in this order.
261 		 */
262 		barrier();
263 
264 		save_pending = signals_pending;
265 		if (save_pending == 0)
266 			return;
267 
268 		signals_pending = 0;
269 
270 		/*
271 		 * We have pending interrupts, so disable signals, as the
272 		 * handlers expect them off when they are called.  They will
273 		 * be enabled again above.
274 		 */
275 
276 		signals_enabled = 0;
277 
278 		/*
279 		 * Deal with SIGIO first because the alarm handler might
280 		 * schedule, leaving the pending SIGIO stranded until we come
281 		 * back here.
282 		 *
283 		 * SIGIO's handler doesn't use siginfo or mcontext,
284 		 * so they can be NULL.
285 		 */
286 		if (save_pending & SIGIO_MASK)
287 			sig_handler_common(SIGIO, NULL, NULL);
288 
289 		if (save_pending & SIGALRM_MASK)
290 			timer_real_alarm_handler(NULL);
291 	}
292 }
293 
294 int get_signals(void)
295 {
296 	return signals_enabled;
297 }
298 
299 int set_signals(int enable)
300 {
301 	int ret;
302 	if (signals_enabled == enable)
303 		return enable;
304 
305 	ret = signals_enabled;
306 	if (enable)
307 		unblock_signals();
308 	else block_signals();
309 
310 	return ret;
311 }
312 
313 int os_is_signal_stack(void)
314 {
315 	stack_t ss;
316 	sigaltstack(NULL, &ss);
317 
318 	return ss.ss_flags & SS_ONSTACK;
319 }
320