signal.c 7.2 KB

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