signal.c 6.4 KB

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