Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 PathScale, Inc |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <signal.h> |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <unistd.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <errno.h> |
| 11 | #include <stdarg.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/mman.h> |
| 14 | #include "user_util.h" |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 15 | #include "user.h" |
| 16 | #include "signal_kern.h" |
| 17 | #include "sysdep/sigcontext.h" |
Jeff Dike | 53b1733 | 2006-11-02 22:07:22 -0800 | [diff] [blame] | 18 | #include "sysdep/barrier.h" |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 19 | #include "sigcontext.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "mode.h" |
Gennady Sharapov | cff65c4 | 2006-01-18 17:42:42 -0800 | [diff] [blame] | 21 | #include "os.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 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. |
| 28 | */ |
| 29 | #define SIGIO_BIT 0 |
| 30 | #define SIGIO_MASK (1 << SIGIO_BIT) |
| 31 | |
| 32 | #define SIGVTALRM_BIT 1 |
| 33 | #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT) |
| 34 | |
| 35 | #define SIGALRM_BIT 2 |
| 36 | #define SIGALRM_MASK (1 << SIGALRM_BIT) |
| 37 | |
Jeff Dike | 53b1733 | 2006-11-02 22:07:22 -0800 | [diff] [blame] | 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. |
| 41 | */ |
| 42 | static volatile int signals_enabled = 1; |
| 43 | static volatile int pending = 0; |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 44 | |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 45 | void sig_handler(int sig, struct sigcontext *sc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 47 | int enabled; |
| 48 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 49 | enabled = signals_enabled; |
| 50 | if(!enabled && (sig == SIGIO)){ |
| 51 | pending |= SIGIO_MASK; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | block_signals(); |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas, |
| 58 | sig, sc); |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 59 | |
| 60 | set_signals(enabled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 63 | static void real_alarm_handler(int sig, struct sigcontext *sc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | if(sig == SIGALRM) |
| 66 | switch_timers(0); |
| 67 | |
| 68 | CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas, |
| 69 | sig, sc); |
| 70 | |
| 71 | if(sig == SIGALRM) |
| 72 | switch_timers(1); |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 73 | |
| 74 | } |
| 75 | |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 76 | void alarm_handler(int sig, struct sigcontext *sc) |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 77 | { |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 78 | int enabled; |
| 79 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 80 | enabled = signals_enabled; |
| 81 | if(!signals_enabled){ |
| 82 | if(sig == SIGVTALRM) |
| 83 | pending |= SIGVTALRM_MASK; |
| 84 | else pending |= SIGALRM_MASK; |
| 85 | |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | block_signals(); |
| 90 | |
| 91 | real_alarm_handler(sig, sc); |
| 92 | set_signals(enabled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 95 | void set_sigstack(void *sig_stack, int size) |
| 96 | { |
| 97 | stack_t stack = ((stack_t) { .ss_flags = 0, |
| 98 | .ss_sp = (__ptr_t) sig_stack, |
| 99 | .ss_size = size - sizeof(void *) }); |
| 100 | |
| 101 | if(sigaltstack(&stack, NULL) != 0) |
| 102 | panic("enabling signal stack failed, errno = %d\n", errno); |
| 103 | } |
| 104 | |
| 105 | void remove_sigstack(void) |
| 106 | { |
| 107 | stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE, |
| 108 | .ss_sp = NULL, |
| 109 | .ss_size = 0 }); |
| 110 | |
| 111 | if(sigaltstack(&stack, NULL) != 0) |
| 112 | panic("disabling signal stack failed, errno = %d\n", errno); |
| 113 | } |
| 114 | |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 115 | void (*handlers[_NSIG])(int sig, struct sigcontext *sc); |
| 116 | |
| 117 | extern void hard_handler(int sig); |
| 118 | |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 119 | void set_handler(int sig, void (*handler)(int), int flags, ...) |
| 120 | { |
| 121 | struct sigaction action; |
| 122 | va_list ap; |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 123 | sigset_t sig_mask; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 124 | int mask; |
| 125 | |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 126 | handlers[sig] = (void (*)(int, struct sigcontext *)) handler; |
| 127 | action.sa_handler = hard_handler; |
| 128 | |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 129 | sigemptyset(&action.sa_mask); |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 130 | |
| 131 | va_start(ap, flags); |
| 132 | while((mask = va_arg(ap, int)) != -1) |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 133 | sigaddset(&action.sa_mask, mask); |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 134 | va_end(ap); |
Jeff Dike | 4b84c69 | 2006-09-25 23:33:04 -0700 | [diff] [blame] | 135 | |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 136 | action.sa_flags = flags; |
| 137 | action.sa_restorer = NULL; |
| 138 | if(sigaction(sig, &action, NULL) < 0) |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 139 | panic("sigaction failed - errno = %d\n", errno); |
| 140 | |
| 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); |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | int change_sig(int signal, int on) |
| 148 | { |
| 149 | sigset_t sigset, old; |
| 150 | |
| 151 | sigemptyset(&sigset); |
| 152 | sigaddset(&sigset, signal); |
| 153 | sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old); |
| 154 | return(!sigismember(&old, signal)); |
| 155 | } |
| 156 | |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 157 | void block_signals(void) |
| 158 | { |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 159 | signals_enabled = 0; |
Jeff Dike | 53b1733 | 2006-11-02 22:07:22 -0800 | [diff] [blame] | 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. |
| 164 | */ |
| 165 | mb(); |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void unblock_signals(void) |
| 169 | { |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 170 | int save_pending; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 171 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 172 | if(signals_enabled == 1) |
| 173 | return; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 174 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 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 |
| 177 | * recheck pending. |
| 178 | */ |
| 179 | while(1){ |
| 180 | /* Save and reset save_pending after enabling signals. This |
| 181 | * way, pending won't be changed while we're reading it. |
| 182 | */ |
| 183 | signals_enabled = 1; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 184 | |
Jeff Dike | 53b1733 | 2006-11-02 22:07:22 -0800 | [diff] [blame] | 185 | /* Setting signals_enabled and reading pending must |
| 186 | * happen in this order. |
| 187 | */ |
| 188 | mb(); |
| 189 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 190 | save_pending = pending; |
Jeff Dike | 53b1733 | 2006-11-02 22:07:22 -0800 | [diff] [blame] | 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. |
| 198 | */ |
| 199 | mb(); |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 200 | return; |
Jeff Dike | 53b1733 | 2006-11-02 22:07:22 -0800 | [diff] [blame] | 201 | } |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 202 | |
| 203 | pending = 0; |
| 204 | |
| 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. |
| 208 | */ |
| 209 | |
| 210 | signals_enabled = 0; |
| 211 | |
| 212 | /* Deal with SIGIO first because the alarm handler might |
| 213 | * schedule, leaving the pending SIGIO stranded until we come |
| 214 | * back here. |
| 215 | */ |
| 216 | if(save_pending & SIGIO_MASK) |
| 217 | CHOOSE_MODE_PROC(sig_handler_common_tt, |
| 218 | sig_handler_common_skas, SIGIO, NULL); |
| 219 | |
| 220 | if(save_pending & SIGALRM_MASK) |
| 221 | real_alarm_handler(SIGALRM, NULL); |
| 222 | |
| 223 | if(save_pending & SIGVTALRM_MASK) |
| 224 | real_alarm_handler(SIGVTALRM, NULL); |
| 225 | } |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | int get_signals(void) |
| 229 | { |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 230 | return signals_enabled; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | int set_signals(int enable) |
| 234 | { |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 235 | int ret; |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 236 | if(signals_enabled == enable) |
| 237 | return enable; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 238 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 239 | ret = signals_enabled; |
| 240 | if(enable) |
| 241 | unblock_signals(); |
| 242 | else block_signals(); |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 243 | |
Jeff Dike | 1d7173b | 2006-01-18 17:42:49 -0800 | [diff] [blame] | 244 | return ret; |
Gennady Sharapov | 0805d89 | 2006-01-08 01:01:29 -0800 | [diff] [blame] | 245 | } |
Gennady Sharapov | abaf697 | 2006-01-18 17:42:46 -0800 | [diff] [blame] | 246 | |
| 247 | void os_usr1_signal(int on) |
| 248 | { |
| 249 | change_sig(SIGUSR1, on); |
| 250 | } |