signal.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 "kern_util.h"
  12. #include "os.h"
  13. #include "sysdep/barrier.h"
  14. #include "sysdep/sigcontext.h"
  15. #include "user.h"
  16. /*
  17. * These are the asynchronous signals. 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. /*
  27. * These are used by both the signal handlers and
  28. * block/unblock_signals. I don't want modifications cached in a
  29. * register - they must go straight to memory.
  30. */
  31. static volatile int signals_enabled = 1;
  32. static volatile int pending = 0;
  33. void sig_handler(int sig, struct sigcontext *sc)
  34. {
  35. int enabled;
  36. enabled = signals_enabled;
  37. if (!enabled && (sig == SIGIO)) {
  38. pending |= SIGIO_MASK;
  39. return;
  40. }
  41. block_signals();
  42. sig_handler_common_skas(sig, sc);
  43. set_signals(enabled);
  44. }
  45. static void real_alarm_handler(struct sigcontext *sc)
  46. {
  47. struct uml_pt_regs regs;
  48. if (sc != NULL)
  49. copy_sc(&regs, sc);
  50. regs.is_user = 0;
  51. unblock_signals();
  52. timer_handler(SIGVTALRM, &regs);
  53. }
  54. void alarm_handler(int sig, struct sigcontext *sc)
  55. {
  56. int enabled;
  57. enabled = signals_enabled;
  58. if (!signals_enabled) {
  59. pending |= SIGVTALRM_MASK;
  60. return;
  61. }
  62. block_signals();
  63. real_alarm_handler(sc);
  64. set_signals(enabled);
  65. }
  66. void timer_init(void)
  67. {
  68. set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
  69. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
  70. }
  71. void set_sigstack(void *sig_stack, int size)
  72. {
  73. stack_t stack = ((stack_t) { .ss_flags = 0,
  74. .ss_sp = (__ptr_t) sig_stack,
  75. .ss_size = size - sizeof(void *) });
  76. if (sigaltstack(&stack, NULL) != 0)
  77. panic("enabling signal stack failed, errno = %d\n", errno);
  78. }
  79. void remove_sigstack(void)
  80. {
  81. stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
  82. .ss_sp = NULL,
  83. .ss_size = 0 });
  84. if (sigaltstack(&stack, NULL) != 0)
  85. panic("disabling signal stack failed, errno = %d\n", errno);
  86. }
  87. void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
  88. void handle_signal(int sig, struct sigcontext *sc)
  89. {
  90. unsigned long pending = 1UL << sig;
  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. }
  123. extern void hard_handler(int sig);
  124. void set_handler(int sig, void (*handler)(int), int flags, ...)
  125. {
  126. struct sigaction action;
  127. va_list ap;
  128. sigset_t sig_mask;
  129. int mask;
  130. handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
  131. action.sa_handler = hard_handler;
  132. sigemptyset(&action.sa_mask);
  133. va_start(ap, flags);
  134. while ((mask = va_arg(ap, int)) != -1)
  135. sigaddset(&action.sa_mask, mask);
  136. va_end(ap);
  137. action.sa_flags = flags;
  138. action.sa_restorer = NULL;
  139. if (sigaction(sig, &action, NULL) < 0)
  140. panic("sigaction failed - errno = %d\n", errno);
  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);
  145. }
  146. int change_sig(int signal, int on)
  147. {
  148. sigset_t sigset, old;
  149. sigemptyset(&sigset);
  150. sigaddset(&sigset, signal);
  151. if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
  152. return -errno;
  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 & SIGVTALRM_MASK)
  215. real_alarm_handler(NULL);
  216. }
  217. }
  218. int get_signals(void)
  219. {
  220. return signals_enabled;
  221. }
  222. int set_signals(int enable)
  223. {
  224. int ret;
  225. if (signals_enabled == enable)
  226. return enable;
  227. ret = signals_enabled;
  228. if (enable)
  229. unblock_signals();
  230. else block_signals();
  231. return ret;
  232. }