signal.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 timer_init(void)
  71. {
  72. set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
  73. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
  74. SIGALRM, -1);
  75. set_handler(SIGALRM, (__sighandler_t) alarm_handler,
  76. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH,
  77. SIGALRM, -1);
  78. }
  79. void set_sigstack(void *sig_stack, int size)
  80. {
  81. stack_t stack = ((stack_t) { .ss_flags = 0,
  82. .ss_sp = (__ptr_t) sig_stack,
  83. .ss_size = size - sizeof(void *) });
  84. if (sigaltstack(&stack, NULL) != 0)
  85. panic("enabling signal stack failed, errno = %d\n", errno);
  86. }
  87. void remove_sigstack(void)
  88. {
  89. stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
  90. .ss_sp = NULL,
  91. .ss_size = 0 });
  92. if (sigaltstack(&stack, NULL) != 0)
  93. panic("disabling signal stack failed, errno = %d\n", errno);
  94. }
  95. void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
  96. void handle_signal(int sig, struct sigcontext *sc)
  97. {
  98. unsigned long pending = 1UL << sig;
  99. int timer = switch_timers(0);
  100. do {
  101. int nested, bail;
  102. /*
  103. * pending comes back with one bit set for each
  104. * interrupt that arrived while setting up the stack,
  105. * plus a bit for this interrupt, plus the zero bit is
  106. * set if this is a nested interrupt.
  107. * If bail is true, then we interrupted another
  108. * handler setting up the stack. In this case, we
  109. * have to return, and the upper handler will deal
  110. * with this interrupt.
  111. */
  112. bail = to_irq_stack(&pending);
  113. if (bail)
  114. return;
  115. nested = pending & 1;
  116. pending &= ~1;
  117. while ((sig = ffs(pending)) != 0){
  118. sig--;
  119. pending &= ~(1 << sig);
  120. (*handlers[sig])(sig, sc);
  121. }
  122. /*
  123. * Again, pending comes back with a mask of signals
  124. * that arrived while tearing down the stack. If this
  125. * is non-zero, we just go back, set up the stack
  126. * again, and handle the new interrupts.
  127. */
  128. if (!nested)
  129. pending = from_irq_stack(nested);
  130. } while (pending);
  131. switch_timers(timer);
  132. }
  133. extern void hard_handler(int sig);
  134. void set_handler(int sig, void (*handler)(int), int flags, ...)
  135. {
  136. struct sigaction action;
  137. va_list ap;
  138. sigset_t sig_mask;
  139. int mask;
  140. handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
  141. action.sa_handler = hard_handler;
  142. sigemptyset(&action.sa_mask);
  143. va_start(ap, flags);
  144. while ((mask = va_arg(ap, int)) != -1)
  145. sigaddset(&action.sa_mask, mask);
  146. va_end(ap);
  147. action.sa_flags = flags;
  148. action.sa_restorer = NULL;
  149. if (sigaction(sig, &action, NULL) < 0)
  150. panic("sigaction failed - errno = %d\n", errno);
  151. sigemptyset(&sig_mask);
  152. sigaddset(&sig_mask, sig);
  153. if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
  154. panic("sigprocmask failed - errno = %d\n", errno);
  155. }
  156. int change_sig(int signal, int on)
  157. {
  158. sigset_t sigset, old;
  159. sigemptyset(&sigset);
  160. sigaddset(&sigset, signal);
  161. sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
  162. return !sigismember(&old, signal);
  163. }
  164. void block_signals(void)
  165. {
  166. signals_enabled = 0;
  167. /*
  168. * This must return with signals disabled, so this barrier
  169. * ensures that writes are flushed out before the return.
  170. * This might matter if gcc figures out how to inline this and
  171. * decides to shuffle this code into the caller.
  172. */
  173. mb();
  174. }
  175. void unblock_signals(void)
  176. {
  177. int save_pending;
  178. if (signals_enabled == 1)
  179. return;
  180. /*
  181. * We loop because the IRQ handler returns with interrupts off. So,
  182. * interrupts may have arrived and we need to re-enable them and
  183. * recheck pending.
  184. */
  185. while(1) {
  186. /*
  187. * Save and reset save_pending after enabling signals. This
  188. * way, pending won't be changed while we're reading it.
  189. */
  190. signals_enabled = 1;
  191. /*
  192. * Setting signals_enabled and reading pending must
  193. * happen in this order.
  194. */
  195. mb();
  196. save_pending = pending;
  197. if (save_pending == 0) {
  198. /*
  199. * This must return with signals enabled, so
  200. * this barrier ensures that writes are
  201. * flushed out before the return. This might
  202. * matter if gcc figures out how to inline
  203. * this (unlikely, given its size) and decides
  204. * to shuffle this code into the caller.
  205. */
  206. mb();
  207. return;
  208. }
  209. pending = 0;
  210. /*
  211. * We have pending interrupts, so disable signals, as the
  212. * handlers expect them off when they are called. They will
  213. * be enabled again above.
  214. */
  215. signals_enabled = 0;
  216. /*
  217. * Deal with SIGIO first because the alarm handler might
  218. * schedule, leaving the pending SIGIO stranded until we come
  219. * back here.
  220. */
  221. if (save_pending & SIGIO_MASK)
  222. sig_handler_common_skas(SIGIO, NULL);
  223. if (save_pending & SIGALRM_MASK)
  224. real_alarm_handler(SIGALRM, NULL);
  225. if (save_pending & SIGVTALRM_MASK)
  226. real_alarm_handler(SIGVTALRM, NULL);
  227. }
  228. }
  229. int get_signals(void)
  230. {
  231. return signals_enabled;
  232. }
  233. int set_signals(int enable)
  234. {
  235. int ret;
  236. if (signals_enabled == enable)
  237. return enable;
  238. ret = signals_enabled;
  239. if (enable)
  240. unblock_signals();
  241. else block_signals();
  242. return ret;
  243. }