signal.c 6.4 KB

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