signal.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/sigcontext.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, struct sigcontext *sc)
  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. copy_sc(&r, sc);
  34. GET_FAULTINFO_FROM_SC(r.faultinfo, sc);
  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, struct sigcontext *sc)
  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, sc);
  64. set_signals(enabled);
  65. }
  66. static void real_alarm_handler(struct sigcontext *sc)
  67. {
  68. struct uml_pt_regs regs;
  69. if (sc != NULL)
  70. copy_sc(&regs, sc);
  71. regs.is_user = 0;
  72. unblock_signals();
  73. timer_handler(SIGVTALRM, &regs);
  74. }
  75. void alarm_handler(int sig, struct sigcontext *sc)
  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(sc);
  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, struct sigcontext *sc) = {
  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 handle_signal(int sig, struct sigcontext *sc)
  110. {
  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, sc);
  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. static void hard_handler(int sig, siginfo_t *info, void *p)
  145. {
  146. struct ucontext *uc = p;
  147. handle_signal(sig, (struct sigcontext *) &uc->uc_mcontext);
  148. }
  149. void set_handler(int sig)
  150. {
  151. struct sigaction action;
  152. int flags = SA_SIGINFO | SA_ONSTACK;
  153. sigset_t sig_mask;
  154. action.sa_sigaction = hard_handler;
  155. /* block irq ones */
  156. sigemptyset(&action.sa_mask);
  157. sigaddset(&action.sa_mask, SIGVTALRM);
  158. sigaddset(&action.sa_mask, SIGIO);
  159. sigaddset(&action.sa_mask, SIGWINCH);
  160. if (sig == SIGSEGV)
  161. flags |= SA_NODEFER;
  162. if (sigismember(&action.sa_mask, sig))
  163. flags |= SA_RESTART; /* if it's an irq signal */
  164. action.sa_flags = flags;
  165. action.sa_restorer = NULL;
  166. if (sigaction(sig, &action, NULL) < 0)
  167. panic("sigaction failed - errno = %d\n", errno);
  168. sigemptyset(&sig_mask);
  169. sigaddset(&sig_mask, sig);
  170. if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
  171. panic("sigprocmask failed - errno = %d\n", errno);
  172. }
  173. int change_sig(int signal, int on)
  174. {
  175. sigset_t sigset;
  176. sigemptyset(&sigset);
  177. sigaddset(&sigset, signal);
  178. if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
  179. return -errno;
  180. return 0;
  181. }
  182. void block_signals(void)
  183. {
  184. signals_enabled = 0;
  185. /*
  186. * This must return with signals disabled, so this barrier
  187. * ensures that writes are flushed out before the return.
  188. * This might matter if gcc figures out how to inline this and
  189. * decides to shuffle this code into the caller.
  190. */
  191. barrier();
  192. }
  193. void unblock_signals(void)
  194. {
  195. int save_pending;
  196. if (signals_enabled == 1)
  197. return;
  198. /*
  199. * We loop because the IRQ handler returns with interrupts off. So,
  200. * interrupts may have arrived and we need to re-enable them and
  201. * recheck signals_pending.
  202. */
  203. while (1) {
  204. /*
  205. * Save and reset save_pending after enabling signals. This
  206. * way, signals_pending won't be changed while we're reading it.
  207. */
  208. signals_enabled = 1;
  209. /*
  210. * Setting signals_enabled and reading signals_pending must
  211. * happen in this order.
  212. */
  213. barrier();
  214. save_pending = signals_pending;
  215. if (save_pending == 0)
  216. return;
  217. signals_pending = 0;
  218. /*
  219. * We have pending interrupts, so disable signals, as the
  220. * handlers expect them off when they are called. They will
  221. * be enabled again above.
  222. */
  223. signals_enabled = 0;
  224. /*
  225. * Deal with SIGIO first because the alarm handler might
  226. * schedule, leaving the pending SIGIO stranded until we come
  227. * back here.
  228. */
  229. if (save_pending & SIGIO_MASK)
  230. sig_handler_common(SIGIO, NULL);
  231. if (save_pending & SIGVTALRM_MASK)
  232. real_alarm_handler(NULL);
  233. }
  234. }
  235. int get_signals(void)
  236. {
  237. return signals_enabled;
  238. }
  239. int set_signals(int enable)
  240. {
  241. int ret;
  242. if (signals_enabled == enable)
  243. return enable;
  244. ret = signals_enabled;
  245. if (enable)
  246. unblock_signals();
  247. else block_signals();
  248. return ret;
  249. }