signal.c 5.5 KB

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