signal.c 6.9 KB

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