uml: signal handling tidying
[sfrench/cifs-2.6.git] / arch / um / os-Linux / signal.c
1 /*
2  * Copyright (C) 2004 PathScale, Inc
3  * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4  * Licensed under the GPL
5  */
6
7 #include <stdlib.h>
8 #include <stdarg.h>
9 #include <errno.h>
10 #include <signal.h>
11 #include <strings.h>
12 #include "as-layout.h"
13 #include "kern_util.h"
14 #include "os.h"
15 #include "sysdep/barrier.h"
16 #include "sysdep/sigcontext.h"
17 #include "user.h"
18
19 /* Copied from linux/compiler-gcc.h since we can't include it directly */
20 #define barrier() __asm__ __volatile__("": : :"memory")
21
22 void (*sig_info[NSIG])(int, 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         [SIGVTALRM]     = timer_handler };
31
32 static void sig_handler_common(int sig, struct sigcontext *sc)
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                 copy_sc(&r, sc);
41                 GET_FAULTINFO_FROM_SC(r.faultinfo, sc);
42         }
43
44         /* enable signals if sig isn't IRQ signal */
45         if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGVTALRM))
46                 unblock_signals();
47
48         (*sig_info[sig])(sig, &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 SIGVTALRM_BIT 1
63 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
64
65 static int signals_enabled;
66 static unsigned int signals_pending;
67
68 void sig_handler(int sig, struct sigcontext *sc)
69 {
70         int enabled;
71
72         enabled = signals_enabled;
73         if (!enabled && (sig == SIGIO)) {
74                 signals_pending |= SIGIO_MASK;
75                 return;
76         }
77
78         block_signals();
79
80         sig_handler_common(sig, sc);
81
82         set_signals(enabled);
83 }
84
85 static void real_alarm_handler(struct sigcontext *sc)
86 {
87         struct uml_pt_regs regs;
88
89         if (sc != NULL)
90                 copy_sc(&regs, sc);
91         regs.is_user = 0;
92         unblock_signals();
93         timer_handler(SIGVTALRM, &regs);
94 }
95
96 void alarm_handler(int sig, struct sigcontext *sc)
97 {
98         int enabled;
99
100         enabled = signals_enabled;
101         if (!signals_enabled) {
102                 signals_pending |= SIGVTALRM_MASK;
103                 return;
104         }
105
106         block_signals();
107
108         real_alarm_handler(sc);
109         set_signals(enabled);
110 }
111
112 void timer_init(void)
113 {
114         set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
115                     SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
116 }
117
118 void set_sigstack(void *sig_stack, int size)
119 {
120         stack_t stack = ((stack_t) { .ss_flags  = 0,
121                                      .ss_sp     = (__ptr_t) sig_stack,
122                                      .ss_size   = size - sizeof(void *) });
123
124         if (sigaltstack(&stack, NULL) != 0)
125                 panic("enabling signal stack failed, errno = %d\n", errno);
126 }
127
128 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
129
130 void handle_signal(int sig, struct sigcontext *sc)
131 {
132         unsigned long pending = 1UL << sig;
133
134         do {
135                 int nested, bail;
136
137                 /*
138                  * pending comes back with one bit set for each
139                  * interrupt that arrived while setting up the stack,
140                  * plus a bit for this interrupt, plus the zero bit is
141                  * set if this is a nested interrupt.
142                  * If bail is true, then we interrupted another
143                  * handler setting up the stack.  In this case, we
144                  * have to return, and the upper handler will deal
145                  * with this interrupt.
146                  */
147                 bail = to_irq_stack(&pending);
148                 if (bail)
149                         return;
150
151                 nested = pending & 1;
152                 pending &= ~1;
153
154                 while ((sig = ffs(pending)) != 0){
155                         sig--;
156                         pending &= ~(1 << sig);
157                         (*handlers[sig])(sig, sc);
158                 }
159
160                 /*
161                  * Again, pending comes back with a mask of signals
162                  * that arrived while tearing down the stack.  If this
163                  * is non-zero, we just go back, set up the stack
164                  * again, and handle the new interrupts.
165                  */
166                 if (!nested)
167                         pending = from_irq_stack(nested);
168         } while (pending);
169 }
170
171 extern void hard_handler(int sig);
172
173 void set_handler(int sig, void (*handler)(int), int flags, ...)
174 {
175         struct sigaction action;
176         va_list ap;
177         sigset_t sig_mask;
178         int mask;
179
180         handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
181         action.sa_handler = hard_handler;
182
183         sigemptyset(&action.sa_mask);
184
185         va_start(ap, flags);
186         while ((mask = va_arg(ap, int)) != -1)
187                 sigaddset(&action.sa_mask, mask);
188         va_end(ap);
189
190         if (sig == SIGSEGV)
191                 flags |= SA_NODEFER;
192
193         action.sa_flags = flags;
194         action.sa_restorer = NULL;
195         if (sigaction(sig, &action, NULL) < 0)
196                 panic("sigaction failed - errno = %d\n", errno);
197
198         sigemptyset(&sig_mask);
199         sigaddset(&sig_mask, sig);
200         if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
201                 panic("sigprocmask failed - errno = %d\n", errno);
202 }
203
204 int change_sig(int signal, int on)
205 {
206         sigset_t sigset;
207
208         sigemptyset(&sigset);
209         sigaddset(&sigset, signal);
210         if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
211                 return -errno;
212
213         return 0;
214 }
215
216 void block_signals(void)
217 {
218         signals_enabled = 0;
219         /*
220          * This must return with signals disabled, so this barrier
221          * ensures that writes are flushed out before the return.
222          * This might matter if gcc figures out how to inline this and
223          * decides to shuffle this code into the caller.
224          */
225         barrier();
226 }
227
228 void unblock_signals(void)
229 {
230         int save_pending;
231
232         if (signals_enabled == 1)
233                 return;
234
235         /*
236          * We loop because the IRQ handler returns with interrupts off.  So,
237          * interrupts may have arrived and we need to re-enable them and
238          * recheck signals_pending.
239          */
240         while(1) {
241                 /*
242                  * Save and reset save_pending after enabling signals.  This
243                  * way, signals_pending won't be changed while we're reading it.
244                  */
245                 signals_enabled = 1;
246
247                 /*
248                  * Setting signals_enabled and reading signals_pending must
249                  * happen in this order.
250                  */
251                 barrier();
252
253                 save_pending = signals_pending;
254                 if (save_pending == 0)
255                         return;
256
257                 signals_pending = 0;
258
259                 /*
260                  * We have pending interrupts, so disable signals, as the
261                  * handlers expect them off when they are called.  They will
262                  * be enabled again above.
263                  */
264
265                 signals_enabled = 0;
266
267                 /*
268                  * Deal with SIGIO first because the alarm handler might
269                  * schedule, leaving the pending SIGIO stranded until we come
270                  * back here.
271                  */
272                 if (save_pending & SIGIO_MASK)
273                         sig_handler_common(SIGIO, NULL);
274
275                 if (save_pending & SIGVTALRM_MASK)
276                         real_alarm_handler(NULL);
277         }
278 }
279
280 int get_signals(void)
281 {
282         return signals_enabled;
283 }
284
285 int set_signals(int enable)
286 {
287         int ret;
288         if (signals_enabled == enable)
289                 return enable;
290
291         ret = signals_enabled;
292         if (enable)
293                 unblock_signals();
294         else block_signals();
295
296         return ret;
297 }