uml: tidy kern_util.h
[safe/jmp/linux-2.6] / 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 "kern_util.h"
13 #include "os.h"
14 #include "sysdep/barrier.h"
15 #include "sysdep/sigcontext.h"
16 #include "user.h"
17
18 /*
19  * These are the asynchronous signals.  SIGPROF is excluded because we want to
20  * be able to profile all of UML, not just the non-critical sections.  If
21  * profiling is not thread-safe, then that is not my problem.  We can disable
22  * profiling when SMP is enabled in that case.
23  */
24 #define SIGIO_BIT 0
25 #define SIGIO_MASK (1 << SIGIO_BIT)
26
27 #define SIGVTALRM_BIT 1
28 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
29
30 /*
31  * These are used by both the signal handlers and
32  * block/unblock_signals.  I don't want modifications cached in a
33  * register - they must go straight to memory.
34  */
35 static volatile int signals_enabled = 1;
36 static volatile int pending = 0;
37
38 void sig_handler(int sig, struct sigcontext *sc)
39 {
40         int enabled;
41
42         enabled = signals_enabled;
43         if (!enabled && (sig == SIGIO)) {
44                 pending |= SIGIO_MASK;
45                 return;
46         }
47
48         block_signals();
49
50         sig_handler_common_skas(sig, sc);
51
52         set_signals(enabled);
53 }
54
55 static void real_alarm_handler(struct sigcontext *sc)
56 {
57         struct uml_pt_regs regs;
58
59         if (sc != NULL)
60                 copy_sc(&regs, sc);
61         regs.is_user = 0;
62         unblock_signals();
63         timer_handler(SIGVTALRM, &regs);
64 }
65
66 void alarm_handler(int sig, struct sigcontext *sc)
67 {
68         int enabled;
69
70         enabled = signals_enabled;
71         if (!signals_enabled) {
72                 pending |= SIGVTALRM_MASK;
73                 return;
74         }
75
76         block_signals();
77
78         real_alarm_handler(sc);
79         set_signals(enabled);
80 }
81
82 void timer_init(void)
83 {
84         set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
85                     SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
86 }
87
88 void set_sigstack(void *sig_stack, int size)
89 {
90         stack_t stack = ((stack_t) { .ss_flags  = 0,
91                                      .ss_sp     = (__ptr_t) sig_stack,
92                                      .ss_size   = size - sizeof(void *) });
93
94         if (sigaltstack(&stack, NULL) != 0)
95                 panic("enabling signal stack failed, errno = %d\n", errno);
96 }
97
98 void remove_sigstack(void)
99 {
100         stack_t stack = ((stack_t) { .ss_flags  = SS_DISABLE,
101                                      .ss_sp     = NULL,
102                                      .ss_size   = 0 });
103
104         if (sigaltstack(&stack, NULL) != 0)
105                 panic("disabling signal stack failed, errno = %d\n", errno);
106 }
107
108 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
109
110 void handle_signal(int sig, struct sigcontext *sc)
111 {
112         unsigned long pending = 1UL << sig;
113
114         do {
115                 int nested, bail;
116
117                 /*
118                  * pending comes back with one bit set for each
119                  * interrupt that arrived while setting up the stack,
120                  * plus a bit for this interrupt, plus the zero bit is
121                  * set if this is a nested interrupt.
122                  * If bail is true, then we interrupted another
123                  * handler setting up the stack.  In this case, we
124                  * have to return, and the upper handler will deal
125                  * with this interrupt.
126                  */
127                 bail = to_irq_stack(&pending);
128                 if (bail)
129                         return;
130
131                 nested = pending & 1;
132                 pending &= ~1;
133
134                 while ((sig = ffs(pending)) != 0){
135                         sig--;
136                         pending &= ~(1 << sig);
137                         (*handlers[sig])(sig, sc);
138                 }
139
140                 /*
141                  * Again, pending comes back with a mask of signals
142                  * that arrived while tearing down the stack.  If this
143                  * is non-zero, we just go back, set up the stack
144                  * again, and handle the new interrupts.
145                  */
146                 if (!nested)
147                         pending = from_irq_stack(nested);
148         } while (pending);
149 }
150
151 extern void hard_handler(int sig);
152
153 void set_handler(int sig, void (*handler)(int), int flags, ...)
154 {
155         struct sigaction action;
156         va_list ap;
157         sigset_t sig_mask;
158         int mask;
159
160         handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
161         action.sa_handler = hard_handler;
162
163         sigemptyset(&action.sa_mask);
164
165         va_start(ap, flags);
166         while ((mask = va_arg(ap, int)) != -1)
167                 sigaddset(&action.sa_mask, mask);
168         va_end(ap);
169
170         action.sa_flags = flags;
171         action.sa_restorer = NULL;
172         if (sigaction(sig, &action, NULL) < 0)
173                 panic("sigaction failed - errno = %d\n", errno);
174
175         sigemptyset(&sig_mask);
176         sigaddset(&sig_mask, sig);
177         if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
178                 panic("sigprocmask failed - errno = %d\n", errno);
179 }
180
181 int change_sig(int signal, int on)
182 {
183         sigset_t sigset, old;
184
185         sigemptyset(&sigset);
186         sigaddset(&sigset, signal);
187         if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
188                 return -errno;
189         return !sigismember(&old, signal);
190 }
191
192 void block_signals(void)
193 {
194         signals_enabled = 0;
195         /*
196          * This must return with signals disabled, so this barrier
197          * ensures that writes are flushed out before the return.
198          * This might matter if gcc figures out how to inline this and
199          * decides to shuffle this code into the caller.
200          */
201         mb();
202 }
203
204 void unblock_signals(void)
205 {
206         int save_pending;
207
208         if (signals_enabled == 1)
209                 return;
210
211         /*
212          * We loop because the IRQ handler returns with interrupts off.  So,
213          * interrupts may have arrived and we need to re-enable them and
214          * recheck pending.
215          */
216         while(1) {
217                 /*
218                  * Save and reset save_pending after enabling signals.  This
219                  * way, pending won't be changed while we're reading it.
220                  */
221                 signals_enabled = 1;
222
223                 /*
224                  * Setting signals_enabled and reading pending must
225                  * happen in this order.
226                  */
227                 mb();
228
229                 save_pending = pending;
230                 if (save_pending == 0) {
231                         /*
232                          * This must return with signals enabled, so
233                          * this barrier ensures that writes are
234                          * flushed out before the return.  This might
235                          * matter if gcc figures out how to inline
236                          * this (unlikely, given its size) and decides
237                          * to shuffle this code into the caller.
238                          */
239                         mb();
240                         return;
241                 }
242
243                 pending = 0;
244
245                 /*
246                  * We have pending interrupts, so disable signals, as the
247                  * handlers expect them off when they are called.  They will
248                  * be enabled again above.
249                  */
250
251                 signals_enabled = 0;
252
253                 /*
254                  * Deal with SIGIO first because the alarm handler might
255                  * schedule, leaving the pending SIGIO stranded until we come
256                  * back here.
257                  */
258                 if (save_pending & SIGIO_MASK)
259                         sig_handler_common_skas(SIGIO, NULL);
260
261                 if (save_pending & SIGVTALRM_MASK)
262                         real_alarm_handler(NULL);
263         }
264 }
265
266 int get_signals(void)
267 {
268         return signals_enabled;
269 }
270
271 int set_signals(int enable)
272 {
273         int ret;
274         if (signals_enabled == enable)
275                 return enable;
276
277         ret = signals_enabled;
278         if (enable)
279                 unblock_signals();
280         else block_signals();
281
282         return ret;
283 }