signal.c 6.6 KB

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