[PATCH] um: remove dead code about os_usr1_signal() and os_usr1_process()
[linux-2.6/linux-loongson.git] / arch / um / os-Linux / signal.c
blob266768629fee6de48152d0674333685a72578021
1 /*
2 * Copyright (C) 2004 PathScale, Inc
3 * Licensed under the GPL
4 */
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_util.h"
15 #include "user.h"
16 #include "signal_kern.h"
17 #include "sysdep/sigcontext.h"
18 #include "sysdep/barrier.h"
19 #include "sigcontext.h"
20 #include "mode.h"
21 #include "os.h"
23 /* These are the asynchronous signals. SIGVTALRM and SIGARLM are handled
24 * together under SIGVTALRM_BIT. SIGPROF is excluded because we want to
25 * be able to profile all of UML, not just the non-critical sections. If
26 * profiling is not thread-safe, then that is not my problem. We can disable
27 * profiling when SMP is enabled in that case.
29 #define SIGIO_BIT 0
30 #define SIGIO_MASK (1 << SIGIO_BIT)
32 #define SIGVTALRM_BIT 1
33 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
35 #define SIGALRM_BIT 2
36 #define SIGALRM_MASK (1 << SIGALRM_BIT)
38 /* These are used by both the signal handlers and
39 * block/unblock_signals. I don't want modifications cached in a
40 * register - they must go straight to memory.
42 static volatile int signals_enabled = 1;
43 static volatile int pending = 0;
45 void sig_handler(int sig, struct sigcontext *sc)
47 int enabled;
49 enabled = signals_enabled;
50 if(!enabled && (sig == SIGIO)){
51 pending |= SIGIO_MASK;
52 return;
55 block_signals();
57 CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
58 sig, sc);
60 set_signals(enabled);
63 static void real_alarm_handler(int sig, struct sigcontext *sc)
65 if(sig == SIGALRM)
66 switch_timers(0);
68 CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
69 sig, sc);
71 if(sig == SIGALRM)
72 switch_timers(1);
76 void alarm_handler(int sig, struct sigcontext *sc)
78 int enabled;
80 enabled = signals_enabled;
81 if(!signals_enabled){
82 if(sig == SIGVTALRM)
83 pending |= SIGVTALRM_MASK;
84 else pending |= SIGALRM_MASK;
86 return;
89 block_signals();
91 real_alarm_handler(sig, sc);
92 set_signals(enabled);
95 void set_sigstack(void *sig_stack, int size)
97 stack_t stack = ((stack_t) { .ss_flags = 0,
98 .ss_sp = (__ptr_t) sig_stack,
99 .ss_size = size - sizeof(void *) });
101 if(sigaltstack(&stack, NULL) != 0)
102 panic("enabling signal stack failed, errno = %d\n", errno);
105 void remove_sigstack(void)
107 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
108 .ss_sp = NULL,
109 .ss_size = 0 });
111 if(sigaltstack(&stack, NULL) != 0)
112 panic("disabling signal stack failed, errno = %d\n", errno);
115 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
117 extern void hard_handler(int sig);
119 void set_handler(int sig, void (*handler)(int), int flags, ...)
121 struct sigaction action;
122 va_list ap;
123 sigset_t sig_mask;
124 int mask;
126 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
127 action.sa_handler = hard_handler;
129 sigemptyset(&action.sa_mask);
131 va_start(ap, flags);
132 while((mask = va_arg(ap, int)) != -1)
133 sigaddset(&action.sa_mask, mask);
134 va_end(ap);
136 action.sa_flags = flags;
137 action.sa_restorer = NULL;
138 if(sigaction(sig, &action, NULL) < 0)
139 panic("sigaction failed - errno = %d\n", errno);
141 sigemptyset(&sig_mask);
142 sigaddset(&sig_mask, sig);
143 if(sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
144 panic("sigprocmask failed - errno = %d\n", errno);
147 int change_sig(int signal, int on)
149 sigset_t sigset, old;
151 sigemptyset(&sigset);
152 sigaddset(&sigset, signal);
153 sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
154 return(!sigismember(&old, signal));
157 void block_signals(void)
159 signals_enabled = 0;
160 /* This must return with signals disabled, so this barrier
161 * ensures that writes are flushed out before the return.
162 * This might matter if gcc figures out how to inline this and
163 * decides to shuffle this code into the caller.
165 mb();
168 void unblock_signals(void)
170 int save_pending;
172 if(signals_enabled == 1)
173 return;
175 /* We loop because the IRQ handler returns with interrupts off. So,
176 * interrupts may have arrived and we need to re-enable them and
177 * recheck pending.
179 while(1){
180 /* Save and reset save_pending after enabling signals. This
181 * way, pending won't be changed while we're reading it.
183 signals_enabled = 1;
185 /* Setting signals_enabled and reading pending must
186 * happen in this order.
188 mb();
190 save_pending = pending;
191 if(save_pending == 0){
192 /* This must return with signals enabled, so
193 * this barrier ensures that writes are
194 * flushed out before the return. This might
195 * matter if gcc figures out how to inline
196 * this (unlikely, given its size) and decides
197 * to shuffle this code into the caller.
199 mb();
200 return;
203 pending = 0;
205 /* We have pending interrupts, so disable signals, as the
206 * handlers expect them off when they are called. They will
207 * be enabled again above.
210 signals_enabled = 0;
212 /* Deal with SIGIO first because the alarm handler might
213 * schedule, leaving the pending SIGIO stranded until we come
214 * back here.
216 if(save_pending & SIGIO_MASK)
217 CHOOSE_MODE_PROC(sig_handler_common_tt,
218 sig_handler_common_skas, SIGIO, NULL);
220 if(save_pending & SIGALRM_MASK)
221 real_alarm_handler(SIGALRM, NULL);
223 if(save_pending & SIGVTALRM_MASK)
224 real_alarm_handler(SIGVTALRM, NULL);
228 int get_signals(void)
230 return signals_enabled;
233 int set_signals(int enable)
235 int ret;
236 if(signals_enabled == enable)
237 return enable;
239 ret = signals_enabled;
240 if(enable)
241 unblock_signals();
242 else block_signals();
244 return ret;