Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[cascardo/linux.git] / arch / um / os-Linux / signal.c
1 /*
2  * Copyright (C) 2004 PathScale, Inc
3  * Licensed under the GPL
4  */
5
6 #include <signal.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <stdarg.h>
12 #include <string.h>
13 #include <sys/mman.h>
14 #include "user.h"
15 #include "signal_kern.h"
16 #include "sysdep/sigcontext.h"
17 #include "sysdep/barrier.h"
18 #include "sigcontext.h"
19 #include "mode.h"
20 #include "os.h"
21
22 /* These are the asynchronous signals.  SIGVTALRM and SIGARLM are handled
23  * together under SIGVTALRM_BIT.  SIGPROF is excluded because we want to
24  * be able to profile all of UML, not just the non-critical sections.  If
25  * profiling is not thread-safe, then that is not my problem.  We can disable
26  * profiling when SMP is enabled in that case.
27  */
28 #define SIGIO_BIT 0
29 #define SIGIO_MASK (1 << SIGIO_BIT)
30
31 #define SIGVTALRM_BIT 1
32 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
33
34 #define SIGALRM_BIT 2
35 #define SIGALRM_MASK (1 << SIGALRM_BIT)
36
37 /* These are used by both the signal handlers and
38  * block/unblock_signals.  I don't want modifications cached in a
39  * register - they must go straight to memory.
40  */
41 static volatile int signals_enabled = 1;
42 static volatile int pending = 0;
43
44 void sig_handler(int sig, struct sigcontext *sc)
45 {
46         int enabled;
47
48         enabled = signals_enabled;
49         if(!enabled && (sig == SIGIO)){
50                 pending |= SIGIO_MASK;
51                 return;
52         }
53
54         block_signals();
55
56         CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
57                          sig, sc);
58
59         set_signals(enabled);
60 }
61
62 static void real_alarm_handler(int sig, struct sigcontext *sc)
63 {
64         if(sig == SIGALRM)
65                 switch_timers(0);
66
67         CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
68                          sig, sc);
69
70         if(sig == SIGALRM)
71                 switch_timers(1);
72
73 }
74
75 void alarm_handler(int sig, struct sigcontext *sc)
76 {
77         int enabled;
78
79         enabled = signals_enabled;
80         if(!signals_enabled){
81                 if(sig == SIGVTALRM)
82                         pending |= SIGVTALRM_MASK;
83                 else pending |= SIGALRM_MASK;
84
85                 return;
86         }
87
88         block_signals();
89
90         real_alarm_handler(sig, sc);
91         set_signals(enabled);
92 }
93
94 void set_sigstack(void *sig_stack, int size)
95 {
96         stack_t stack = ((stack_t) { .ss_flags  = 0,
97                                      .ss_sp     = (__ptr_t) sig_stack,
98                                      .ss_size   = size - sizeof(void *) });
99
100         if(sigaltstack(&stack, NULL) != 0)
101                 panic("enabling signal stack failed, errno = %d\n", errno);
102 }
103
104 void remove_sigstack(void)
105 {
106         stack_t stack = ((stack_t) { .ss_flags  = SS_DISABLE,
107                                      .ss_sp     = NULL,
108                                      .ss_size   = 0 });
109
110         if(sigaltstack(&stack, NULL) != 0)
111                 panic("disabling signal stack failed, errno = %d\n", errno);
112 }
113
114 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
115
116 extern void hard_handler(int sig);
117
118 void set_handler(int sig, void (*handler)(int), int flags, ...)
119 {
120         struct sigaction action;
121         va_list ap;
122         sigset_t sig_mask;
123         int mask;
124
125         handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
126         action.sa_handler = hard_handler;
127
128         sigemptyset(&action.sa_mask);
129
130         va_start(ap, flags);
131         while((mask = va_arg(ap, int)) != -1)
132                 sigaddset(&action.sa_mask, mask);
133         va_end(ap);
134
135         action.sa_flags = flags;
136         action.sa_restorer = NULL;
137         if(sigaction(sig, &action, NULL) < 0)
138                 panic("sigaction failed - errno = %d\n", errno);
139
140         sigemptyset(&sig_mask);
141         sigaddset(&sig_mask, sig);
142         if(sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
143                 panic("sigprocmask failed - errno = %d\n", errno);
144 }
145
146 int change_sig(int signal, int on)
147 {
148         sigset_t sigset, old;
149
150         sigemptyset(&sigset);
151         sigaddset(&sigset, signal);
152         sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
153         return(!sigismember(&old, signal));
154 }
155
156 void block_signals(void)
157 {
158         signals_enabled = 0;
159         /* This must return with signals disabled, so this barrier
160          * ensures that writes are flushed out before the return.
161          * This might matter if gcc figures out how to inline this and
162          * decides to shuffle this code into the caller.
163          */
164         mb();
165 }
166
167 void unblock_signals(void)
168 {
169         int save_pending;
170
171         if(signals_enabled == 1)
172                 return;
173
174         /* We loop because the IRQ handler returns with interrupts off.  So,
175          * interrupts may have arrived and we need to re-enable them and
176          * recheck pending.
177          */
178         while(1){
179                 /* Save and reset save_pending after enabling signals.  This
180                  * way, pending won't be changed while we're reading it.
181                  */
182                 signals_enabled = 1;
183
184                 /* Setting signals_enabled and reading pending must
185                  * happen in this order.
186                  */
187                 mb();
188
189                 save_pending = pending;
190                 if(save_pending == 0){
191                         /* This must return with signals enabled, so
192                          * this barrier ensures that writes are
193                          * flushed out before the return.  This might
194                          * matter if gcc figures out how to inline
195                          * this (unlikely, given its size) and decides
196                          * to shuffle this code into the caller.
197                          */
198                         mb();
199                         return;
200                 }
201
202                 pending = 0;
203
204                 /* We have pending interrupts, so disable signals, as the
205                  * handlers expect them off when they are called.  They will
206                  * be enabled again above.
207                  */
208
209                 signals_enabled = 0;
210
211                 /* Deal with SIGIO first because the alarm handler might
212                  * schedule, leaving the pending SIGIO stranded until we come
213                  * back here.
214                  */
215                 if(save_pending & SIGIO_MASK)
216                         CHOOSE_MODE_PROC(sig_handler_common_tt,
217                                          sig_handler_common_skas, SIGIO, NULL);
218
219                 if(save_pending & SIGALRM_MASK)
220                         real_alarm_handler(SIGALRM, NULL);
221
222                 if(save_pending & SIGVTALRM_MASK)
223                         real_alarm_handler(SIGVTALRM, NULL);
224         }
225 }
226
227 int get_signals(void)
228 {
229         return signals_enabled;
230 }
231
232 int set_signals(int enable)
233 {
234         int ret;
235         if(signals_enabled == enable)
236                 return enable;
237
238         ret = signals_enabled;
239         if(enable)
240                 unblock_signals();
241         else block_signals();
242
243         return ret;
244 }