signal.c 6.5 KB

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