signal.c 6.3 KB

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