signal.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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, struct siginfo *, 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, struct siginfo *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, struct siginfo *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 = {
  93. .ss_flags = 0,
  94. .ss_sp = sig_stack,
  95. .ss_size = size - sizeof(void *)
  96. };
  97. if (sigaltstack(&stack, NULL) != 0)
  98. panic("enabling signal stack failed, errno = %d\n", errno);
  99. }
  100. static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
  101. [SIGSEGV] = sig_handler,
  102. [SIGBUS] = sig_handler,
  103. [SIGILL] = sig_handler,
  104. [SIGFPE] = sig_handler,
  105. [SIGTRAP] = sig_handler,
  106. [SIGIO] = sig_handler,
  107. [SIGWINCH] = sig_handler,
  108. [SIGVTALRM] = alarm_handler
  109. };
  110. static void hard_handler(int sig, siginfo_t *si, void *p)
  111. {
  112. struct ucontext *uc = p;
  113. mcontext_t *mc = &uc->uc_mcontext;
  114. unsigned long pending = 1UL << sig;
  115. do {
  116. int nested, bail;
  117. /*
  118. * pending comes back with one bit set for each
  119. * interrupt that arrived while setting up the stack,
  120. * plus a bit for this interrupt, plus the zero bit is
  121. * set if this is a nested interrupt.
  122. * If bail is true, then we interrupted another
  123. * handler setting up the stack. In this case, we
  124. * have to return, and the upper handler will deal
  125. * with this interrupt.
  126. */
  127. bail = to_irq_stack(&pending);
  128. if (bail)
  129. return;
  130. nested = pending & 1;
  131. pending &= ~1;
  132. while ((sig = ffs(pending)) != 0){
  133. sig--;
  134. pending &= ~(1 << sig);
  135. (*handlers[sig])(sig, (struct siginfo *)si, mc);
  136. }
  137. /*
  138. * Again, pending comes back with a mask of signals
  139. * that arrived while tearing down the stack. If this
  140. * is non-zero, we just go back, set up the stack
  141. * again, and handle the new interrupts.
  142. */
  143. if (!nested)
  144. pending = from_irq_stack(nested);
  145. } while (pending);
  146. }
  147. void set_handler(int sig)
  148. {
  149. struct sigaction action;
  150. int flags = SA_SIGINFO | SA_ONSTACK;
  151. sigset_t sig_mask;
  152. action.sa_sigaction = hard_handler;
  153. /* block irq ones */
  154. sigemptyset(&action.sa_mask);
  155. sigaddset(&action.sa_mask, SIGVTALRM);
  156. sigaddset(&action.sa_mask, SIGIO);
  157. sigaddset(&action.sa_mask, SIGWINCH);
  158. if (sig == SIGSEGV)
  159. flags |= SA_NODEFER;
  160. if (sigismember(&action.sa_mask, sig))
  161. flags |= SA_RESTART; /* if it's an irq signal */
  162. action.sa_flags = flags;
  163. action.sa_restorer = NULL;
  164. if (sigaction(sig, &action, NULL) < 0)
  165. panic("sigaction failed - errno = %d\n", errno);
  166. sigemptyset(&sig_mask);
  167. sigaddset(&sig_mask, sig);
  168. if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
  169. panic("sigprocmask failed - errno = %d\n", errno);
  170. }
  171. int change_sig(int signal, int on)
  172. {
  173. sigset_t sigset;
  174. sigemptyset(&sigset);
  175. sigaddset(&sigset, signal);
  176. if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
  177. return -errno;
  178. return 0;
  179. }
  180. void block_signals(void)
  181. {
  182. signals_enabled = 0;
  183. /*
  184. * This must return with signals disabled, so this barrier
  185. * ensures that writes are flushed out before the return.
  186. * This might matter if gcc figures out how to inline this and
  187. * decides to shuffle this code into the caller.
  188. */
  189. barrier();
  190. }
  191. void unblock_signals(void)
  192. {
  193. int save_pending;
  194. if (signals_enabled == 1)
  195. return;
  196. /*
  197. * We loop because the IRQ handler returns with interrupts off. So,
  198. * interrupts may have arrived and we need to re-enable them and
  199. * recheck signals_pending.
  200. */
  201. while (1) {
  202. /*
  203. * Save and reset save_pending after enabling signals. This
  204. * way, signals_pending won't be changed while we're reading it.
  205. */
  206. signals_enabled = 1;
  207. /*
  208. * Setting signals_enabled and reading signals_pending must
  209. * happen in this order.
  210. */
  211. barrier();
  212. save_pending = signals_pending;
  213. if (save_pending == 0)
  214. return;
  215. signals_pending = 0;
  216. /*
  217. * We have pending interrupts, so disable signals, as the
  218. * handlers expect them off when they are called. They will
  219. * be enabled again above.
  220. */
  221. signals_enabled = 0;
  222. /*
  223. * Deal with SIGIO first because the alarm handler might
  224. * schedule, leaving the pending SIGIO stranded until we come
  225. * back here.
  226. *
  227. * SIGIO's handler doesn't use siginfo or mcontext,
  228. * so they can be NULL.
  229. */
  230. if (save_pending & SIGIO_MASK)
  231. sig_handler_common(SIGIO, NULL, NULL);
  232. if (save_pending & SIGVTALRM_MASK)
  233. real_alarm_handler(NULL);
  234. }
  235. }
  236. int get_signals(void)
  237. {
  238. return signals_enabled;
  239. }
  240. int set_signals(int enable)
  241. {
  242. int ret;
  243. if (signals_enabled == enable)
  244. return enable;
  245. ret = signals_enabled;
  246. if (enable)
  247. unblock_signals();
  248. else block_signals();
  249. return ret;
  250. }
  251. int os_is_signal_stack(void)
  252. {
  253. stack_t ss;
  254. sigaltstack(NULL, &ss);
  255. return ss.ss_flags & SS_ONSTACK;
  256. }