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 void set_sigstack(void *sig_stack
, int size
)
111 stack_t stack
= ((stack_t
) { .ss_flags
= 0,
112 .ss_sp
= (__ptr_t
) sig_stack
,
113 .ss_size
= size
- sizeof(void *) });
115 if(sigaltstack(&stack
, NULL
) != 0)
116 panic("enabling signal stack failed, errno = %d\n", errno
);
119 void remove_sigstack(void)
121 stack_t stack
= ((stack_t
) { .ss_flags
= SS_DISABLE
,
125 if(sigaltstack(&stack
, NULL
) != 0)
126 panic("disabling signal stack failed, errno = %d\n", errno
);
129 void set_handler(int sig
, void (*handler
)(int), int flags
, ...)
131 struct sigaction action
;
137 action
.sa_handler
= handler
;
138 sigemptyset(&action
.sa_mask
);
139 while((mask
= va_arg(ap
, int)) != -1){
140 sigaddset(&action
.sa_mask
, mask
);
143 action
.sa_flags
= flags
;
144 action
.sa_restorer
= NULL
;
145 if(sigaction(sig
, &action
, NULL
) < 0)
146 panic("sigaction failed - errno = %d\n", errno
);
148 sigemptyset(&sig_mask
);
149 sigaddset(&sig_mask
, sig
);
150 if(sigprocmask(SIG_UNBLOCK
, &sig_mask
, NULL
) < 0)
151 panic("sigprocmask failed - errno = %d\n", errno
);
154 int change_sig(int signal
, int on
)
156 sigset_t sigset
, old
;
158 sigemptyset(&sigset
);
159 sigaddset(&sigset
, signal
);
160 sigprocmask(on
? SIG_UNBLOCK
: SIG_BLOCK
, &sigset
, &old
);
161 return(!sigismember(&old
, signal
));
164 void block_signals(void)
169 void unblock_signals(void)
173 if(signals_enabled
== 1)
176 /* We loop because the IRQ handler returns with interrupts off. So,
177 * interrupts may have arrived and we need to re-enable them and
181 /* Save and reset save_pending after enabling signals. This
182 * way, pending won't be changed while we're reading it.
186 save_pending
= pending
;
187 if(save_pending
== 0)
192 /* We have pending interrupts, so disable signals, as the
193 * handlers expect them off when they are called. They will
194 * be enabled again above.
199 /* Deal with SIGIO first because the alarm handler might
200 * schedule, leaving the pending SIGIO stranded until we come
203 if(save_pending
& SIGIO_MASK
)
204 CHOOSE_MODE_PROC(sig_handler_common_tt
,
205 sig_handler_common_skas
, SIGIO
, NULL
);
207 if(save_pending
& SIGALRM_MASK
)
208 real_alarm_handler(SIGALRM
, NULL
);
210 if(save_pending
& SIGVTALRM_MASK
)
211 real_alarm_handler(SIGVTALRM
, NULL
);
215 int get_signals(void)
217 return signals_enabled
;
220 int set_signals(int enable
)
223 if(signals_enabled
== enable
)
226 ret
= signals_enabled
;
229 else block_signals();
234 void os_usr1_signal(int on
)
236 change_sig(SIGUSR1
, on
);