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 "sysdep/signal.h"
19 #include "sigcontext.h"
23 void sig_handler(ARCH_SIGHDLR_PARAM)
25 struct sigcontext *sc;
27 ARCH_GET_SIGCONTEXT(sc, sig);
28 CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
32 extern int timer_irq_inited;
34 void alarm_handler(ARCH_SIGHDLR_PARAM)
36 struct sigcontext *sc;
38 ARCH_GET_SIGCONTEXT(sc, sig);
39 if(!timer_irq_inited) return;
44 CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
51 extern void do_boot_timer_handler(struct sigcontext * sc);
53 void boot_timer_handler(ARCH_SIGHDLR_PARAM)
55 struct sigcontext *sc;
57 ARCH_GET_SIGCONTEXT(sc, sig);
59 do_boot_timer_handler(sc);
62 void set_sigstack(void *sig_stack, int size)
64 stack_t stack = ((stack_t) { .ss_flags = 0,
65 .ss_sp = (__ptr_t) sig_stack,
66 .ss_size = size - sizeof(void *) });
68 if(sigaltstack(&stack, NULL) != 0)
69 panic("enabling signal stack failed, errno = %d\n", errno);
72 void remove_sigstack(void)
74 stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
78 if(sigaltstack(&stack, NULL) != 0)
79 panic("disabling signal stack failed, errno = %d\n", errno);
82 void set_handler(int sig, void (*handler)(int), int flags, ...)
84 struct sigaction action;
89 action.sa_handler = handler;
90 sigemptyset(&action.sa_mask);
91 while((mask = va_arg(ap, int)) != -1){
92 sigaddset(&action.sa_mask, mask);
95 action.sa_flags = flags;
96 action.sa_restorer = NULL;
97 if(sigaction(sig, &action, NULL) < 0)
98 panic("sigaction failed");
101 int change_sig(int signal, int on)
103 sigset_t sigset, old;
105 sigemptyset(&sigset);
106 sigaddset(&sigset, signal);
107 sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
108 return(!sigismember(&old, signal));
111 /* Both here and in set/get_signal we don't touch SIGPROF, because we must not
112 * disable profiling; it's safe because the profiling code does not interact
113 * with the kernel code at all.*/
115 static void change_signals(int type)
120 sigaddset(&mask, SIGVTALRM);
121 sigaddset(&mask, SIGALRM);
122 sigaddset(&mask, SIGIO);
123 if(sigprocmask(type, &mask, NULL) < 0)
124 panic("Failed to change signal mask - errno = %d", errno);
127 void block_signals(void)
129 change_signals(SIG_BLOCK);
132 void unblock_signals(void)
134 change_signals(SIG_UNBLOCK);
137 /* These are the asynchronous signals. SIGVTALRM and SIGARLM are handled
138 * together under SIGVTALRM_BIT. SIGPROF is excluded because we want to
139 * be able to profile all of UML, not just the non-critical sections. If
140 * profiling is not thread-safe, then that is not my problem. We can disable
141 * profiling when SMP is enabled in that case.
144 #define SIGVTALRM_BIT 1
146 static int enable_mask(sigset_t *mask)
150 sigs = sigismember(mask, SIGIO) ? 0 : 1 << SIGIO_BIT;
151 sigs |= sigismember(mask, SIGVTALRM) ? 0 : 1 << SIGVTALRM_BIT;
152 sigs |= sigismember(mask, SIGALRM) ? 0 : 1 << SIGVTALRM_BIT;
156 int get_signals(void)
160 if(sigprocmask(SIG_SETMASK, NULL, &mask) < 0)
161 panic("Failed to get signal mask");
162 return(enable_mask(&mask));
165 int set_signals(int enable)
171 if(enable & (1 << SIGIO_BIT))
172 sigaddset(&mask, SIGIO);
173 if(enable & (1 << SIGVTALRM_BIT)){
174 sigaddset(&mask, SIGVTALRM);
175 sigaddset(&mask, SIGALRM);
178 /* This is safe - sigprocmask is guaranteed to copy locally the
179 * value of new_set, do his work and then, at the end, write to
182 if(sigprocmask(SIG_UNBLOCK, &mask, &mask) < 0)
183 panic("Failed to enable signals");
184 ret = enable_mask(&mask);
186 if((enable & (1 << SIGIO_BIT)) == 0)
187 sigaddset(&mask, SIGIO);
188 if((enable & (1 << SIGVTALRM_BIT)) == 0){
189 sigaddset(&mask, SIGVTALRM);
190 sigaddset(&mask, SIGALRM);
192 if(sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
193 panic("Failed to block signals");
198 void os_usr1_signal(int on)
200 change_sig(SIGUSR1, on);