2 * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
4 * Copyright (C) 2004 PathScale, Inc
5 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
6 * Licensed under the GPL
14 #include <as-layout.h>
15 #include <kern_util.h>
17 #include <sysdep/mcontext.h>
19 void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *) = {
20 [SIGTRAP] = relay_signal,
21 [SIGFPE] = relay_signal,
22 [SIGILL] = relay_signal,
24 [SIGBUS] = bus_handler,
25 [SIGSEGV] = segv_handler,
26 [SIGIO] = sigio_handler,
27 [SIGALRM] = timer_handler
30 static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc)
33 int save_errno = errno;
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);
42 /* enable signals if sig isn't IRQ signal */
43 if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGALRM))
46 (*sig_info[sig])(sig, si, &r);
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.
58 #define SIGIO_MASK (1 << SIGIO_BIT)
61 #define SIGALRM_MASK (1 << SIGALRM_BIT)
63 static int signals_enabled;
64 static unsigned int signals_pending;
66 void sig_handler(int sig, struct siginfo *si, mcontext_t *mc)
70 enabled = signals_enabled;
71 if (!enabled && (sig == SIGIO)) {
72 signals_pending |= SIGIO_MASK;
78 sig_handler_common(sig, si, mc);
83 static void timer_real_alarm_handler(mcontext_t *mc)
85 struct uml_pt_regs regs;
88 get_regs_from_mc(®s, mc);
89 timer_handler(SIGALRM, NULL, ®s);
92 void timer_alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
96 enabled = signals_enabled;
97 if (!signals_enabled) {
98 signals_pending |= SIGALRM_MASK;
104 timer_real_alarm_handler(mc);
105 set_signals(enabled);
108 void deliver_alarm(void) {
109 timer_alarm_handler(SIGALRM, NULL, NULL);
112 void timer_set_signal_handler(void)
114 set_handler(SIGALRM);
117 void set_sigstack(void *sig_stack, int size)
122 .ss_size = size - sizeof(void *)
125 if (sigaltstack(&stack, NULL) != 0)
126 panic("enabling signal stack failed, errno = %d\n", errno);
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,
136 [SIGIO] = sig_handler,
137 [SIGWINCH] = sig_handler,
138 [SIGALRM] = timer_alarm_handler
141 static void hard_handler(int sig, siginfo_t *si, void *p)
143 struct ucontext *uc = p;
144 mcontext_t *mc = &uc->uc_mcontext;
145 unsigned long pending = 1UL << sig;
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.
160 bail = to_irq_stack(&pending);
164 nested = pending & 1;
167 while ((sig = ffs(pending)) != 0){
169 pending &= ~(1 << sig);
170 (*handlers[sig])(sig, (struct siginfo *)si, mc);
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.
180 pending = from_irq_stack(nested);
184 void set_handler(int sig)
186 struct sigaction action;
187 int flags = SA_SIGINFO | SA_ONSTACK;
190 action.sa_sigaction = hard_handler;
193 sigemptyset(&action.sa_mask);
194 sigaddset(&action.sa_mask, SIGIO);
195 sigaddset(&action.sa_mask, SIGWINCH);
196 sigaddset(&action.sa_mask, SIGALRM);
201 if (sigismember(&action.sa_mask, sig))
202 flags |= SA_RESTART; /* if it's an irq signal */
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);
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);
215 int change_sig(int signal, int on)
219 sigemptyset(&sigset);
220 sigaddset(&sigset, signal);
221 if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
227 void block_signals(void)
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.
239 void unblock_signals(void)
243 if (signals_enabled == 1)
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.
253 * Save and reset save_pending after enabling signals. This
254 * way, signals_pending won't be changed while we're reading it.
259 * Setting signals_enabled and reading signals_pending must
260 * happen in this order.
264 save_pending = signals_pending;
265 if (save_pending == 0)
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.
279 * Deal with SIGIO first because the alarm handler might
280 * schedule, leaving the pending SIGIO stranded until we come
283 * SIGIO's handler doesn't use siginfo or mcontext,
284 * so they can be NULL.
286 if (save_pending & SIGIO_MASK)
287 sig_handler_common(SIGIO, NULL, NULL);
289 if (save_pending & SIGALRM_MASK)
290 timer_real_alarm_handler(NULL);
294 int get_signals(void)
296 return signals_enabled;
299 int set_signals(int enable)
302 if (signals_enabled == enable)
305 ret = signals_enabled;
308 else block_signals();
313 int os_is_signal_stack(void)
316 sigaltstack(NULL, &ss);
318 return ss.ss_flags & SS_ONSTACK;