signal.c 7.5 KB

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