signal.c 6.4 KB

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