2 * Copyright (C) 2004 PathScale, Inc
3 * Licensed under the GPL
14 #include "user_util.h"
16 #include "signal_kern.h"
17 #include "sysdep/sigcontext.h"
18 #include "sigcontext.h"
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.
29 #define SIGIO_MASK (1 << SIGIO_BIT)
31 #define SIGVTALRM_BIT 1
32 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
35 #define SIGALRM_MASK (1 << SIGALRM_BIT)
37 static int signals_enabled = 1;
38 static int pending = 0;
40 void sig_handler(int sig, struct sigcontext *sc)
44 enabled = signals_enabled;
45 if(!enabled && (sig == SIGIO)){
46 pending |= SIGIO_MASK;
52 CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
58 static void real_alarm_handler(int sig, struct sigcontext *sc)
63 CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
71 void alarm_handler(int sig, struct sigcontext *sc)
75 enabled = signals_enabled;
78 pending |= SIGVTALRM_MASK;
79 else pending |= SIGALRM_MASK;
86 real_alarm_handler(sig, sc);
90 void set_sigstack(void *sig_stack, int size)
92 stack_t stack = ((stack_t) { .ss_flags = 0,
93 .ss_sp = (__ptr_t) sig_stack,
94 .ss_size = size - sizeof(void *) });
96 if(sigaltstack(&stack, NULL) != 0)
97 panic("enabling signal stack failed, errno = %d\n", errno);
100 void remove_sigstack(void)
102 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
106 if(sigaltstack(&stack, NULL) != 0)
107 panic("disabling signal stack failed, errno = %d\n", errno);
110 void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
112 extern void hard_handler(int sig);
114 void set_handler(int sig, void (*handler)(int), int flags, ...)
116 struct sigaction action;
121 handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
122 action.sa_handler = hard_handler;
124 sigemptyset(&action.sa_mask);
127 while((mask = va_arg(ap, int)) != -1)
128 sigaddset(&action.sa_mask, mask);
131 action.sa_flags = flags;
132 action.sa_restorer = NULL;
133 if(sigaction(sig, &action, NULL) < 0)
134 panic("sigaction failed - errno = %d\n", errno);
136 sigemptyset(&sig_mask);
137 sigaddset(&sig_mask, sig);
138 if(sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
139 panic("sigprocmask failed - errno = %d\n", errno);
142 int change_sig(int signal, int on)
144 sigset_t sigset, old;
146 sigemptyset(&sigset);
147 sigaddset(&sigset, signal);
148 sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
149 return(!sigismember(&old, signal));
152 void block_signals(void)
157 void unblock_signals(void)
161 if(signals_enabled == 1)
164 /* We loop because the IRQ handler returns with interrupts off. So,
165 * interrupts may have arrived and we need to re-enable them and
169 /* Save and reset save_pending after enabling signals. This
170 * way, pending won't be changed while we're reading it.
174 save_pending = pending;
175 if(save_pending == 0)
180 /* We have pending interrupts, so disable signals, as the
181 * handlers expect them off when they are called. They will
182 * be enabled again above.
187 /* Deal with SIGIO first because the alarm handler might
188 * schedule, leaving the pending SIGIO stranded until we come
191 if(save_pending & SIGIO_MASK)
192 CHOOSE_MODE_PROC(sig_handler_common_tt,
193 sig_handler_common_skas, SIGIO, NULL);
195 if(save_pending & SIGALRM_MASK)
196 real_alarm_handler(SIGALRM, NULL);
198 if(save_pending & SIGVTALRM_MASK)
199 real_alarm_handler(SIGVTALRM, NULL);
203 int get_signals(void)
205 return signals_enabled;
208 int set_signals(int enable)
211 if(signals_enabled == enable)
214 ret = signals_enabled;
217 else block_signals();
222 void os_usr1_signal(int on)
224 change_sig(SIGUSR1, on);