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 /* 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.
30 #define SIGIO_MASK (1 << SIGIO_BIT)
32 #define SIGVTALRM_BIT 1
33 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
36 #define SIGALRM_MASK (1 << SIGALRM_BIT)
38 static int signals_enabled
= 1;
39 static int pending
= 0;
41 void sig_handler(ARCH_SIGHDLR_PARAM
)
43 struct sigcontext
*sc
;
46 /* Must be the first thing that this handler does - x86_64 stores
47 * the sigcontext in %rdx, and we need to save it before it has a
48 * chance to get trashed.
51 ARCH_GET_SIGCONTEXT(sc
, sig
);
53 enabled
= signals_enabled
;
54 if(!enabled
&& (sig
== SIGIO
)){
55 pending
|= SIGIO_MASK
;
61 CHOOSE_MODE_PROC(sig_handler_common_tt
, sig_handler_common_skas
,
67 extern int timer_irq_inited
;
69 static void real_alarm_handler(int sig
, struct sigcontext
*sc
)
71 if(!timer_irq_inited
){
79 CHOOSE_MODE_PROC(sig_handler_common_tt
, sig_handler_common_skas
,
87 void alarm_handler(ARCH_SIGHDLR_PARAM
)
89 struct sigcontext
*sc
;
92 ARCH_GET_SIGCONTEXT(sc
, sig
);
94 enabled
= signals_enabled
;
97 pending
|= SIGVTALRM_MASK
;
98 else pending
|= SIGALRM_MASK
;
105 real_alarm_handler(sig
, sc
);
106 set_signals(enabled
);
109 extern void do_boot_timer_handler(struct sigcontext
* sc
);
111 void boot_timer_handler(ARCH_SIGHDLR_PARAM
)
113 struct sigcontext
*sc
;
116 ARCH_GET_SIGCONTEXT(sc
, sig
);
118 enabled
= signals_enabled
;
121 pending
|= SIGVTALRM_MASK
;
122 else pending
|= SIGALRM_MASK
;
128 do_boot_timer_handler(sc
);
129 set_signals(enabled
);
132 void set_sigstack(void *sig_stack
, int size
)
134 stack_t stack
= ((stack_t
) { .ss_flags
= 0,
135 .ss_sp
= (__ptr_t
) sig_stack
,
136 .ss_size
= size
- sizeof(void *) });
138 if(sigaltstack(&stack
, NULL
) != 0)
139 panic("enabling signal stack failed, errno = %d\n", errno
);
142 void remove_sigstack(void)
144 stack_t stack
= ((stack_t
) { .ss_flags
= SS_DISABLE
,
148 if(sigaltstack(&stack
, NULL
) != 0)
149 panic("disabling signal stack failed, errno = %d\n", errno
);
152 void set_handler(int sig
, void (*handler
)(int), int flags
, ...)
154 struct sigaction action
;
160 action
.sa_handler
= handler
;
161 sigemptyset(&action
.sa_mask
);
162 while((mask
= va_arg(ap
, int)) != -1){
163 sigaddset(&action
.sa_mask
, mask
);
166 action
.sa_flags
= flags
;
167 action
.sa_restorer
= NULL
;
168 if(sigaction(sig
, &action
, NULL
) < 0)
169 panic("sigaction failed - errno = %d\n", errno
);
171 sigemptyset(&sig_mask
);
172 sigaddset(&sig_mask
, sig
);
173 if(sigprocmask(SIG_UNBLOCK
, &sig_mask
, NULL
) < 0)
174 panic("sigprocmask failed - errno = %d\n", errno
);
177 int change_sig(int signal
, int on
)
179 sigset_t sigset
, old
;
181 sigemptyset(&sigset
);
182 sigaddset(&sigset
, signal
);
183 sigprocmask(on
? SIG_UNBLOCK
: SIG_BLOCK
, &sigset
, &old
);
184 return(!sigismember(&old
, signal
));
187 void block_signals(void)
192 void unblock_signals(void)
196 if(signals_enabled
== 1)
199 /* We loop because the IRQ handler returns with interrupts off. So,
200 * interrupts may have arrived and we need to re-enable them and
204 /* Save and reset save_pending after enabling signals. This
205 * way, pending won't be changed while we're reading it.
209 save_pending
= pending
;
210 if(save_pending
== 0)
215 /* We have pending interrupts, so disable signals, as the
216 * handlers expect them off when they are called. They will
217 * be enabled again above.
222 /* Deal with SIGIO first because the alarm handler might
223 * schedule, leaving the pending SIGIO stranded until we come
226 if(save_pending
& SIGIO_MASK
)
227 CHOOSE_MODE_PROC(sig_handler_common_tt
,
228 sig_handler_common_skas
, SIGIO
, NULL
);
230 if(save_pending
& SIGALRM_MASK
)
231 real_alarm_handler(SIGALRM
, NULL
);
233 if(save_pending
& SIGVTALRM_MASK
)
234 real_alarm_handler(SIGVTALRM
, NULL
);
238 int get_signals(void)
240 return signals_enabled
;
243 int set_signals(int enable
)
246 if(signals_enabled
== enable
)
249 ret
= signals_enabled
;
252 else block_signals();
257 void os_usr1_signal(int on
)
259 change_sig(SIGUSR1
, on
);