signal.c 5.8 KB

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