signal.c 6.4 KB

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