Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 PathScale, Inc |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 3 | * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Licensed under the GPL |
| 5 | */ |
| 6 | |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 7 | #include <stdlib.h> |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 8 | #include <stdarg.h> |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <signal.h> |
| 11 | #include <strings.h> |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 12 | #include "os.h" |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 13 | #include "sysdep/barrier.h" |
| 14 | #include "sysdep/sigcontext.h" |
| 15 | #include "user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 17 | /* |
| 18 | * These are the asynchronous signals. SIGVTALRM and SIGARLM are handled |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 19 | * together under SIGVTALRM_BIT. SIGPROF is excluded because we want to |
| 20 | * be able to profile all of UML, not just the non-critical sections. If |
| 21 | * profiling is not thread-safe, then that is not my problem. We can disable |
| 22 | * profiling when SMP is enabled in that case. |
| 23 | */ |
| 24 | #define SIGIO_BIT 0 |
| 25 | #define SIGIO_MASK (1 << SIGIO_BIT) |
| 26 | |
| 27 | #define SIGVTALRM_BIT 1 |
| 28 | #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT) |
| 29 | |
| 30 | #define SIGALRM_BIT 2 |
| 31 | #define SIGALRM_MASK (1 << SIGALRM_BIT) |
| 32 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 33 | /* |
| 34 | * These are used by both the signal handlers and |
Jeff Dike | 53b1733 | 2006-11-02 22:07:22 -0800 | [diff] [blame] | 35 | * block/unblock_signals. I don't want modifications cached in a |
| 36 | * register - they must go straight to memory. |
| 37 | */ |
| 38 | static volatile int signals_enabled = 1; |
| 39 | static volatile int pending = 0; |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 40 | |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 41 | void sig_handler(int sig, struct sigcontext *sc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 43 | int enabled; |
| 44 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 45 | enabled = signals_enabled; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 46 | if (!enabled && (sig == SIGIO)) { |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 47 | pending |= SIGIO_MASK; |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | block_signals(); |
| 52 | |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 53 | sig_handler_common_skas(sig, sc); |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 54 | |
| 55 | set_signals(enabled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 58 | static void real_alarm_handler(int sig, struct sigcontext *sc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 60 | struct uml_pt_regs regs; |
Jeff Dike | 2ea5bc5 | 2007-05-10 22:22:32 -0700 | [diff] [blame] | 61 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 62 | if (sc != NULL) |
Jeff Dike | 2ea5bc5 | 2007-05-10 22:22:32 -0700 | [diff] [blame] | 63 | copy_sc(®s, sc); |
Jeff Dike | 77bf440 | 2007-10-16 01:26:58 -0700 | [diff] [blame] | 64 | regs.is_user = 0; |
Jeff Dike | 2ea5bc5 | 2007-05-10 22:22:32 -0700 | [diff] [blame] | 65 | unblock_signals(); |
| 66 | timer_handler(sig, ®s); |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 69 | void alarm_handler(int sig, struct sigcontext *sc) |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 70 | { |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 71 | int enabled; |
| 72 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 73 | enabled = signals_enabled; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 74 | if (!signals_enabled) { |
| 75 | if (sig == SIGVTALRM) |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 76 | pending |= SIGVTALRM_MASK; |
| 77 | else pending |= SIGALRM_MASK; |
| 78 | |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | block_signals(); |
| 83 | |
| 84 | real_alarm_handler(sig, sc); |
| 85 | set_signals(enabled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 88 | void set_sigstack(void *sig_stack, int size) |
| 89 | { |
| 90 | stack_t stack = ((stack_t) { .ss_flags = 0, |
| 91 | .ss_sp = (__ptr_t) sig_stack, |
| 92 | .ss_size = size - sizeof(void *) }); |
| 93 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 94 | if (sigaltstack(&stack, NULL) != 0) |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 95 | panic("enabling signal stack failed, errno = %d\n", errno); |
| 96 | } |
| 97 | |
| 98 | void remove_sigstack(void) |
| 99 | { |
| 100 | stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE, |
| 101 | .ss_sp = NULL, |
| 102 | .ss_size = 0 }); |
| 103 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 104 | if (sigaltstack(&stack, NULL) != 0) |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 105 | panic("disabling signal stack failed, errno = %d\n", errno); |
| 106 | } |
| 107 | |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 108 | void (*handlers[_NSIG])(int sig, struct sigcontext *sc); |
| 109 | |
Jeff Dike | c14b849 | 2007-05-10 22:22:34 -0700 | [diff] [blame] | 110 | void handle_signal(int sig, struct sigcontext *sc) |
| 111 | { |
Jeff Dike | 508a927 | 2007-09-18 22:46:49 -0700 | [diff] [blame] | 112 | unsigned long pending = 1UL << sig; |
Jeff Dike | 181bde8 | 2007-10-16 01:27:22 -0700 | [diff] [blame] | 113 | int timer = switch_timers(0); |
Jeff Dike | c14b849 | 2007-05-10 22:22:34 -0700 | [diff] [blame] | 114 | |
| 115 | do { |
| 116 | int nested, bail; |
| 117 | |
| 118 | /* |
| 119 | * pending comes back with one bit set for each |
| 120 | * interrupt that arrived while setting up the stack, |
| 121 | * plus a bit for this interrupt, plus the zero bit is |
| 122 | * set if this is a nested interrupt. |
| 123 | * If bail is true, then we interrupted another |
| 124 | * handler setting up the stack. In this case, we |
| 125 | * have to return, and the upper handler will deal |
| 126 | * with this interrupt. |
| 127 | */ |
Jeff Dike | 508a927 | 2007-09-18 22:46:49 -0700 | [diff] [blame] | 128 | bail = to_irq_stack(&pending); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 129 | if (bail) |
Jeff Dike | c14b849 | 2007-05-10 22:22:34 -0700 | [diff] [blame] | 130 | return; |
| 131 | |
| 132 | nested = pending & 1; |
| 133 | pending &= ~1; |
| 134 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 135 | while ((sig = ffs(pending)) != 0){ |
Jeff Dike | c14b849 | 2007-05-10 22:22:34 -0700 | [diff] [blame] | 136 | sig--; |
| 137 | pending &= ~(1 << sig); |
| 138 | (*handlers[sig])(sig, sc); |
| 139 | } |
| 140 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 141 | /* |
| 142 | * Again, pending comes back with a mask of signals |
Jeff Dike | c14b849 | 2007-05-10 22:22:34 -0700 | [diff] [blame] | 143 | * that arrived while tearing down the stack. If this |
| 144 | * is non-zero, we just go back, set up the stack |
| 145 | * again, and handle the new interrupts. |
| 146 | */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 147 | if (!nested) |
Jeff Dike | c14b849 | 2007-05-10 22:22:34 -0700 | [diff] [blame] | 148 | pending = from_irq_stack(nested); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 149 | } while (pending); |
Jeff Dike | 181bde8 | 2007-10-16 01:27:22 -0700 | [diff] [blame] | 150 | |
| 151 | switch_timers(timer); |
Jeff Dike | c14b849 | 2007-05-10 22:22:34 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 154 | extern void hard_handler(int sig); |
| 155 | |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 156 | void set_handler(int sig, void (*handler)(int), int flags, ...) |
| 157 | { |
| 158 | struct sigaction action; |
| 159 | va_list ap; |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 160 | sigset_t sig_mask; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 161 | int mask; |
| 162 | |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 163 | handlers[sig] = (void (*)(int, struct sigcontext *)) handler; |
| 164 | action.sa_handler = hard_handler; |
| 165 | |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 166 | sigemptyset(&action.sa_mask); |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 167 | |
| 168 | va_start(ap, flags); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 169 | while ((mask = va_arg(ap, int)) != -1) |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 170 | sigaddset(&action.sa_mask, mask); |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 171 | va_end(ap); |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 172 | |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 173 | action.sa_flags = flags; |
| 174 | action.sa_restorer = NULL; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 175 | if (sigaction(sig, &action, NULL) < 0) |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 176 | panic("sigaction failed - errno = %d\n", errno); |
| 177 | |
| 178 | sigemptyset(&sig_mask); |
| 179 | sigaddset(&sig_mask, sig); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 180 | if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0) |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 181 | panic("sigprocmask failed - errno = %d\n", errno); |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | int change_sig(int signal, int on) |
| 185 | { |
| 186 | sigset_t sigset, old; |
| 187 | |
| 188 | sigemptyset(&sigset); |
| 189 | sigaddset(&sigset, signal); |
| 190 | sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 191 | return !sigismember(&old, signal); |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 194 | void block_signals(void) |
| 195 | { |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 196 | signals_enabled = 0; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 197 | /* |
| 198 | * This must return with signals disabled, so this barrier |
Jeff Dike | 53b1733 | 2006-11-02 22:07:22 -0800 | [diff] [blame] | 199 | * ensures that writes are flushed out before the return. |
| 200 | * This might matter if gcc figures out how to inline this and |
| 201 | * decides to shuffle this code into the caller. |
| 202 | */ |
| 203 | mb(); |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void unblock_signals(void) |
| 207 | { |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 208 | int save_pending; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 209 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 210 | if (signals_enabled == 1) |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 211 | return; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 212 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 213 | /* |
| 214 | * We loop because the IRQ handler returns with interrupts off. So, |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 215 | * interrupts may have arrived and we need to re-enable them and |
| 216 | * recheck pending. |
| 217 | */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 218 | while(1) { |
| 219 | /* |
| 220 | * Save and reset save_pending after enabling signals. This |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 221 | * way, pending won't be changed while we're reading it. |
| 222 | */ |
| 223 | signals_enabled = 1; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 224 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 225 | /* |
| 226 | * Setting signals_enabled and reading pending must |
Jeff Dike | 53b1733 | 2006-11-02 22:07:22 -0800 | [diff] [blame] | 227 | * happen in this order. |
| 228 | */ |
| 229 | mb(); |
| 230 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 231 | save_pending = pending; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 232 | if (save_pending == 0) { |
| 233 | /* |
| 234 | * This must return with signals enabled, so |
Jeff Dike | 53b1733 | 2006-11-02 22:07:22 -0800 | [diff] [blame] | 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. |
| 240 | */ |
| 241 | mb(); |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 242 | return; |
Jeff Dike | 53b1733 | 2006-11-02 22:07:22 -0800 | [diff] [blame] | 243 | } |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 244 | |
| 245 | pending = 0; |
| 246 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 247 | /* |
| 248 | * We have pending interrupts, so disable signals, as the |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 249 | * handlers expect them off when they are called. They will |
| 250 | * be enabled again above. |
| 251 | */ |
| 252 | |
| 253 | signals_enabled = 0; |
| 254 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 255 | /* |
| 256 | * Deal with SIGIO first because the alarm handler might |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 257 | * schedule, leaving the pending SIGIO stranded until we come |
| 258 | * back here. |
| 259 | */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 260 | if (save_pending & SIGIO_MASK) |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 261 | sig_handler_common_skas(SIGIO, NULL); |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 262 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 263 | if (save_pending & SIGALRM_MASK) |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 264 | real_alarm_handler(SIGALRM, NULL); |
| 265 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 266 | if (save_pending & SIGVTALRM_MASK) |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 267 | real_alarm_handler(SIGVTALRM, NULL); |
| 268 | } |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | int get_signals(void) |
| 272 | { |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 273 | return signals_enabled; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | int set_signals(int enable) |
| 277 | { |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 278 | int ret; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 279 | if (signals_enabled == enable) |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 280 | return enable; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 281 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 282 | ret = signals_enabled; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 283 | if (enable) |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 284 | unblock_signals(); |
| 285 | else block_signals(); |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 286 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 287 | return ret; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 288 | } |