2 * Copyright (C) 2004 PathScale, Inc
3 * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 * Licensed under the GPL
12 #include "as-layout.h"
13 #include "kern_util.h"
15 #include "sysdep/barrier.h"
16 #include "sysdep/sigcontext.h"
19 /* Copied from linux/compiler-gcc.h since we can't include it directly */
20 #define barrier() __asm__ __volatile__("": : :"memory")
22 void (*sig_info
[NSIG
])(int, struct uml_pt_regs
*) = {
23 [SIGTRAP
] = relay_signal
,
24 [SIGFPE
] = relay_signal
,
25 [SIGILL
] = relay_signal
,
27 [SIGBUS
] = bus_handler
,
28 [SIGSEGV
] = segv_handler
,
29 [SIGIO
] = sigio_handler
,
30 [SIGVTALRM
] = timer_handler
};
32 static void sig_handler_common(int sig
, struct sigcontext
*sc
)
35 int save_errno
= errno
;
39 /* For segfaults, we want the data from the sigcontext. */
41 GET_FAULTINFO_FROM_SC(r
.faultinfo
, sc
);
44 /* enable signals if sig isn't IRQ signal */
45 if ((sig
!= SIGIO
) && (sig
!= SIGWINCH
) && (sig
!= SIGVTALRM
))
48 (*sig_info
[sig
])(sig
, &r
);
54 * These are the asynchronous signals. SIGPROF is excluded because we want to
55 * be able to profile all of UML, not just the non-critical sections. If
56 * profiling is not thread-safe, then that is not my problem. We can disable
57 * profiling when SMP is enabled in that case.
60 #define SIGIO_MASK (1 << SIGIO_BIT)
62 #define SIGVTALRM_BIT 1
63 #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
65 static int signals_enabled
;
66 static unsigned int signals_pending
;
68 void sig_handler(int sig
, struct sigcontext
*sc
)
72 enabled
= signals_enabled
;
73 if (!enabled
&& (sig
== SIGIO
)) {
74 signals_pending
|= SIGIO_MASK
;
80 sig_handler_common(sig
, sc
);
85 static void real_alarm_handler(struct sigcontext
*sc
)
87 struct uml_pt_regs regs
;
93 timer_handler(SIGVTALRM
, ®s
);
96 void alarm_handler(int sig
, struct sigcontext
*sc
)
100 enabled
= signals_enabled
;
101 if (!signals_enabled
) {
102 signals_pending
|= SIGVTALRM_MASK
;
108 real_alarm_handler(sc
);
109 set_signals(enabled
);
112 void timer_init(void)
114 set_handler(SIGVTALRM
, (__sighandler_t
) alarm_handler
,
115 SA_ONSTACK
| SA_RESTART
, SIGUSR1
, SIGIO
, SIGWINCH
, -1);
118 void set_sigstack(void *sig_stack
, int size
)
120 stack_t stack
= ((stack_t
) { .ss_flags
= 0,
121 .ss_sp
= (__ptr_t
) sig_stack
,
122 .ss_size
= size
- sizeof(void *) });
124 if (sigaltstack(&stack
, NULL
) != 0)
125 panic("enabling signal stack failed, errno = %d\n", errno
);
128 void (*handlers
[_NSIG
])(int sig
, struct sigcontext
*sc
);
130 void handle_signal(int sig
, struct sigcontext
*sc
)
132 unsigned long pending
= 1UL << sig
;
138 * pending comes back with one bit set for each
139 * interrupt that arrived while setting up the stack,
140 * plus a bit for this interrupt, plus the zero bit is
141 * set if this is a nested interrupt.
142 * If bail is true, then we interrupted another
143 * handler setting up the stack. In this case, we
144 * have to return, and the upper handler will deal
145 * with this interrupt.
147 bail
= to_irq_stack(&pending
);
151 nested
= pending
& 1;
154 while ((sig
= ffs(pending
)) != 0){
156 pending
&= ~(1 << sig
);
157 (*handlers
[sig
])(sig
, sc
);
161 * Again, pending comes back with a mask of signals
162 * that arrived while tearing down the stack. If this
163 * is non-zero, we just go back, set up the stack
164 * again, and handle the new interrupts.
167 pending
= from_irq_stack(nested
);
171 extern void hard_handler(int sig
);
173 void set_handler(int sig
, void (*handler
)(int), int flags
, ...)
175 struct sigaction action
;
180 handlers
[sig
] = (void (*)(int, struct sigcontext
*)) handler
;
181 action
.sa_handler
= hard_handler
;
183 sigemptyset(&action
.sa_mask
);
186 while ((mask
= va_arg(ap
, int)) != -1)
187 sigaddset(&action
.sa_mask
, mask
);
193 action
.sa_flags
= flags
;
194 action
.sa_restorer
= NULL
;
195 if (sigaction(sig
, &action
, NULL
) < 0)
196 panic("sigaction failed - errno = %d\n", errno
);
198 sigemptyset(&sig_mask
);
199 sigaddset(&sig_mask
, sig
);
200 if (sigprocmask(SIG_UNBLOCK
, &sig_mask
, NULL
) < 0)
201 panic("sigprocmask failed - errno = %d\n", errno
);
204 int change_sig(int signal
, int on
)
208 sigemptyset(&sigset
);
209 sigaddset(&sigset
, signal
);
210 if (sigprocmask(on
? SIG_UNBLOCK
: SIG_BLOCK
, &sigset
, NULL
) < 0)
216 void block_signals(void)
220 * This must return with signals disabled, so this barrier
221 * ensures that writes are flushed out before the return.
222 * This might matter if gcc figures out how to inline this and
223 * decides to shuffle this code into the caller.
228 void unblock_signals(void)
232 if (signals_enabled
== 1)
236 * We loop because the IRQ handler returns with interrupts off. So,
237 * interrupts may have arrived and we need to re-enable them and
238 * recheck signals_pending.
242 * Save and reset save_pending after enabling signals. This
243 * way, signals_pending won't be changed while we're reading it.
248 * Setting signals_enabled and reading signals_pending must
249 * happen in this order.
253 save_pending
= signals_pending
;
254 if (save_pending
== 0)
260 * We have pending interrupts, so disable signals, as the
261 * handlers expect them off when they are called. They will
262 * be enabled again above.
268 * Deal with SIGIO first because the alarm handler might
269 * schedule, leaving the pending SIGIO stranded until we come
272 if (save_pending
& SIGIO_MASK
)
273 sig_handler_common(SIGIO
, NULL
);
275 if (save_pending
& SIGVTALRM_MASK
)
276 real_alarm_handler(NULL
);
280 int get_signals(void)
282 return signals_enabled
;
285 int set_signals(int enable
)
288 if (signals_enabled
== enable
)
291 ret
= signals_enabled
;
294 else block_signals();