signal.c 6.3 KB

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