signal.c 7.5 KB

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