signal.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "mode.h"
  19. #include "os.h"
  20. /* These are the asynchronous signals. SIGVTALRM and SIGARLM are handled
  21. * together under SIGVTALRM_BIT. SIGPROF is excluded because we want to
  22. * be able to profile all of UML, not just the non-critical sections. If
  23. * profiling is not thread-safe, then that is not my problem. We can disable
  24. * profiling when SMP is enabled in that case.
  25. */
  26. #define SIGIO_BIT 0
  27. #define SIGIO_MASK (1 << SIGIO_BIT)
  28. #define SIGVTALRM_BIT 1
  29. #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
  30. #define SIGALRM_BIT 2
  31. #define SIGALRM_MASK (1 << SIGALRM_BIT)
  32. /* These are used by both the signal handlers and
  33. * block/unblock_signals. I don't want modifications cached in a
  34. * register - they must go straight to memory.
  35. */
  36. static volatile int signals_enabled = 1;
  37. static volatile int pending = 0;
  38. void sig_handler(int sig, struct sigcontext *sc)
  39. {
  40. int enabled;
  41. enabled = signals_enabled;
  42. if(!enabled && (sig == SIGIO)){
  43. pending |= SIGIO_MASK;
  44. return;
  45. }
  46. block_signals();
  47. CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
  48. sig, sc);
  49. set_signals(enabled);
  50. }
  51. static void real_alarm_handler(int sig, struct sigcontext *sc)
  52. {
  53. if(sig == SIGALRM)
  54. switch_timers(0);
  55. CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
  56. sig, sc);
  57. if(sig == SIGALRM)
  58. switch_timers(1);
  59. }
  60. void alarm_handler(int sig, struct sigcontext *sc)
  61. {
  62. int enabled;
  63. enabled = signals_enabled;
  64. if(!signals_enabled){
  65. if(sig == SIGVTALRM)
  66. pending |= SIGVTALRM_MASK;
  67. else pending |= SIGALRM_MASK;
  68. return;
  69. }
  70. block_signals();
  71. real_alarm_handler(sig, sc);
  72. set_signals(enabled);
  73. }
  74. void set_sigstack(void *sig_stack, int size)
  75. {
  76. stack_t stack = ((stack_t) { .ss_flags = 0,
  77. .ss_sp = (__ptr_t) sig_stack,
  78. .ss_size = size - sizeof(void *) });
  79. if(sigaltstack(&stack, NULL) != 0)
  80. panic("enabling signal stack failed, errno = %d\n", errno);
  81. }
  82. void remove_sigstack(void)
  83. {
  84. stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
  85. .ss_sp = NULL,
  86. .ss_size = 0 });
  87. if(sigaltstack(&stack, NULL) != 0)
  88. panic("disabling signal stack failed, errno = %d\n", errno);
  89. }
  90. void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
  91. extern void hard_handler(int sig);
  92. void set_handler(int sig, void (*handler)(int), int flags, ...)
  93. {
  94. struct sigaction action;
  95. va_list ap;
  96. sigset_t sig_mask;
  97. int mask;
  98. handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
  99. action.sa_handler = hard_handler;
  100. sigemptyset(&action.sa_mask);
  101. va_start(ap, flags);
  102. while((mask = va_arg(ap, int)) != -1)
  103. sigaddset(&action.sa_mask, mask);
  104. va_end(ap);
  105. action.sa_flags = flags;
  106. action.sa_restorer = NULL;
  107. if(sigaction(sig, &action, NULL) < 0)
  108. panic("sigaction failed - errno = %d\n", errno);
  109. sigemptyset(&sig_mask);
  110. sigaddset(&sig_mask, sig);
  111. if(sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
  112. panic("sigprocmask failed - errno = %d\n", errno);
  113. }
  114. int change_sig(int signal, int on)
  115. {
  116. sigset_t sigset, old;
  117. sigemptyset(&sigset);
  118. sigaddset(&sigset, signal);
  119. sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old);
  120. return(!sigismember(&old, signal));
  121. }
  122. void block_signals(void)
  123. {
  124. signals_enabled = 0;
  125. /* This must return with signals disabled, so this barrier
  126. * ensures that writes are flushed out before the return.
  127. * This might matter if gcc figures out how to inline this and
  128. * decides to shuffle this code into the caller.
  129. */
  130. mb();
  131. }
  132. void unblock_signals(void)
  133. {
  134. int save_pending;
  135. if(signals_enabled == 1)
  136. return;
  137. /* We loop because the IRQ handler returns with interrupts off. So,
  138. * interrupts may have arrived and we need to re-enable them and
  139. * recheck pending.
  140. */
  141. while(1){
  142. /* Save and reset save_pending after enabling signals. This
  143. * way, pending won't be changed while we're reading it.
  144. */
  145. signals_enabled = 1;
  146. /* Setting signals_enabled and reading pending must
  147. * happen in this order.
  148. */
  149. mb();
  150. save_pending = pending;
  151. if(save_pending == 0){
  152. /* This must return with signals enabled, so
  153. * this barrier ensures that writes are
  154. * flushed out before the return. This might
  155. * matter if gcc figures out how to inline
  156. * this (unlikely, given its size) and decides
  157. * to shuffle this code into the caller.
  158. */
  159. mb();
  160. return;
  161. }
  162. pending = 0;
  163. /* We have pending interrupts, so disable signals, as the
  164. * handlers expect them off when they are called. They will
  165. * be enabled again above.
  166. */
  167. signals_enabled = 0;
  168. /* Deal with SIGIO first because the alarm handler might
  169. * schedule, leaving the pending SIGIO stranded until we come
  170. * back here.
  171. */
  172. if(save_pending & SIGIO_MASK)
  173. CHOOSE_MODE_PROC(sig_handler_common_tt,
  174. sig_handler_common_skas, SIGIO, NULL);
  175. if(save_pending & SIGALRM_MASK)
  176. real_alarm_handler(SIGALRM, NULL);
  177. if(save_pending & SIGVTALRM_MASK)
  178. real_alarm_handler(SIGVTALRM, NULL);
  179. }
  180. }
  181. int get_signals(void)
  182. {
  183. return signals_enabled;
  184. }
  185. int set_signals(int enable)
  186. {
  187. int ret;
  188. if(signals_enabled == enable)
  189. return enable;
  190. ret = signals_enabled;
  191. if(enable)
  192. unblock_signals();
  193. else block_signals();
  194. return ret;
  195. }