signal.c 6.1 KB

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