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/barrier.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 /* 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
)
49 enabled
= signals_enabled
;
50 if(!enabled
&& (sig
== SIGIO
)){
51 pending
|= SIGIO_MASK
;
57 CHOOSE_MODE_PROC(sig_handler_common_tt
, sig_handler_common_skas
,
63 static void real_alarm_handler(int sig
, struct sigcontext
*sc
)
68 CHOOSE_MODE_PROC(sig_handler_common_tt
, sig_handler_common_skas
,
76 void alarm_handler(int sig
, struct sigcontext
*sc
)
80 enabled
= signals_enabled
;
83 pending
|= SIGVTALRM_MASK
;
84 else pending
|= SIGALRM_MASK
;
91 real_alarm_handler(sig
, sc
);
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
,
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
;
126 handlers
[sig
] = (void (*)(int, struct sigcontext
*)) handler
;
127 action
.sa_handler
= hard_handler
;
129 sigemptyset(&action
.sa_mask
);
132 while((mask
= va_arg(ap
, int)) != -1)
133 sigaddset(&action
.sa_mask
, mask
);
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)
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.
168 void unblock_signals(void)
172 if(signals_enabled
== 1)
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
180 /* Save and reset save_pending after enabling signals. This
181 * way, pending won't be changed while we're reading it.
185 /* Setting signals_enabled and reading pending must
186 * happen in this order.
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.
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.
212 /* Deal with SIGIO first because the alarm handler might
213 * schedule, leaving the pending SIGIO stranded until we come
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
)
236 if(signals_enabled
== enable
)
239 ret
= signals_enabled
;
242 else block_signals();
247 void os_usr1_signal(int on
)
249 change_sig(SIGUSR1
, on
);