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