2 * Copyright (C) 2004 PathScale, Inc
3 * Licensed under the GPL
15 #include "signal_kern.h"
16 #include "sysdep/sigcontext.h"
17 #include "sysdep/barrier.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 /* These are used by both the signal handlers and
38 * block/unblock_signals. I don't want modifications cached in a
39 * register - they must go straight to memory.
41 static volatile int signals_enabled
= 1;
42 static volatile int pending
= 0;
44 void sig_handler(int sig
, struct sigcontext
*sc
)
48 enabled
= signals_enabled
;
49 if(!enabled
&& (sig
== SIGIO
)){
50 pending
|= SIGIO_MASK
;
56 sig_handler_common_skas(sig
, sc
);
61 static void real_alarm_handler(int sig
, struct sigcontext
*sc
)
63 union uml_pt_regs regs
;
70 regs
.skas
.is_user
= 0;
72 timer_handler(sig
, ®s
);
78 void alarm_handler(int sig
, struct sigcontext
*sc
)
82 enabled
= signals_enabled
;
85 pending
|= SIGVTALRM_MASK
;
86 else pending
|= SIGALRM_MASK
;
93 real_alarm_handler(sig
, sc
);
97 void set_sigstack(void *sig_stack
, int size
)
99 stack_t stack
= ((stack_t
) { .ss_flags
= 0,
100 .ss_sp
= (__ptr_t
) sig_stack
,
101 .ss_size
= size
- sizeof(void *) });
103 if(sigaltstack(&stack
, NULL
) != 0)
104 panic("enabling signal stack failed, errno = %d\n", errno
);
107 void remove_sigstack(void)
109 stack_t stack
= ((stack_t
) { .ss_flags
= SS_DISABLE
,
113 if(sigaltstack(&stack
, NULL
) != 0)
114 panic("disabling signal stack failed, errno = %d\n", errno
);
117 void (*handlers
[_NSIG
])(int sig
, struct sigcontext
*sc
);
119 void handle_signal(int sig
, struct sigcontext
*sc
)
121 unsigned long pending
= 1UL << sig
;
127 * pending comes back with one bit set for each
128 * interrupt that arrived while setting up the stack,
129 * plus a bit for this interrupt, plus the zero bit is
130 * set if this is a nested interrupt.
131 * If bail is true, then we interrupted another
132 * handler setting up the stack. In this case, we
133 * have to return, and the upper handler will deal
134 * with this interrupt.
136 bail
= to_irq_stack(&pending
);
140 nested
= pending
& 1;
143 while((sig
= ffs(pending
)) != 0){
145 pending
&= ~(1 << sig
);
146 (*handlers
[sig
])(sig
, sc
);
149 /* Again, pending comes back with a mask of signals
150 * that arrived while tearing down the stack. If this
151 * is non-zero, we just go back, set up the stack
152 * again, and handle the new interrupts.
155 pending
= from_irq_stack(nested
);
159 extern void hard_handler(int sig
);
161 void set_handler(int sig
, void (*handler
)(int), int flags
, ...)
163 struct sigaction action
;
168 handlers
[sig
] = (void (*)(int, struct sigcontext
*)) handler
;
169 action
.sa_handler
= hard_handler
;
171 sigemptyset(&action
.sa_mask
);
174 while((mask
= va_arg(ap
, int)) != -1)
175 sigaddset(&action
.sa_mask
, mask
);
178 action
.sa_flags
= flags
;
179 action
.sa_restorer
= NULL
;
180 if(sigaction(sig
, &action
, NULL
) < 0)
181 panic("sigaction failed - errno = %d\n", errno
);
183 sigemptyset(&sig_mask
);
184 sigaddset(&sig_mask
, sig
);
185 if(sigprocmask(SIG_UNBLOCK
, &sig_mask
, NULL
) < 0)
186 panic("sigprocmask failed - errno = %d\n", errno
);
189 int change_sig(int signal
, int on
)
191 sigset_t sigset
, old
;
193 sigemptyset(&sigset
);
194 sigaddset(&sigset
, signal
);
195 sigprocmask(on
? SIG_UNBLOCK
: SIG_BLOCK
, &sigset
, &old
);
196 return(!sigismember(&old
, signal
));
199 void block_signals(void)
202 /* This must return with signals disabled, so this barrier
203 * ensures that writes are flushed out before the return.
204 * This might matter if gcc figures out how to inline this and
205 * decides to shuffle this code into the caller.
210 void unblock_signals(void)
214 if(signals_enabled
== 1)
217 /* We loop because the IRQ handler returns with interrupts off. So,
218 * interrupts may have arrived and we need to re-enable them and
222 /* Save and reset save_pending after enabling signals. This
223 * way, pending won't be changed while we're reading it.
227 /* Setting signals_enabled and reading pending must
228 * happen in this order.
232 save_pending
= pending
;
233 if(save_pending
== 0){
234 /* This must return with signals enabled, so
235 * this barrier ensures that writes are
236 * flushed out before the return. This might
237 * matter if gcc figures out how to inline
238 * this (unlikely, given its size) and decides
239 * to shuffle this code into the caller.
247 /* We have pending interrupts, so disable signals, as the
248 * handlers expect them off when they are called. They will
249 * be enabled again above.
254 /* Deal with SIGIO first because the alarm handler might
255 * schedule, leaving the pending SIGIO stranded until we come
258 if(save_pending
& SIGIO_MASK
)
259 sig_handler_common_skas(SIGIO
, NULL
);
261 if(save_pending
& SIGALRM_MASK
)
262 real_alarm_handler(SIGALRM
, NULL
);
264 if(save_pending
& SIGVTALRM_MASK
)
265 real_alarm_handler(SIGVTALRM
, NULL
);
269 int get_signals(void)
271 return signals_enabled
;
274 int set_signals(int enable
)
277 if(signals_enabled
== enable
)
280 ret
= signals_enabled
;
283 else block_signals();
This page took 0.049959 seconds and 6 git commands to generate.