signal.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/barrier.h"
  15. #include "sysdep/sigcontext.h"
  16. #include "user.h"
  17. /* Copied from linux/compiler-gcc.h since we can't include it directly */
  18. #define barrier() __asm__ __volatile__("": : :"memory")
  19. void (*sig_info[NSIG])(int, 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. [SIGVTALRM] = timer_handler };
  28. static void sig_handler_common(int sig, struct sigcontext *sc)
  29. {
  30. struct uml_pt_regs r;
  31. int save_errno = errno;
  32. r.is_user = 0;
  33. if (sig == SIGSEGV) {
  34. /* For segfaults, we want the data from the sigcontext. */
  35. copy_sc(&r, sc);
  36. GET_FAULTINFO_FROM_SC(r.faultinfo, sc);
  37. }
  38. /* enable signals if sig isn't IRQ signal */
  39. if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGVTALRM))
  40. unblock_signals();
  41. (*sig_info[sig])(sig, &r);
  42. errno = save_errno;
  43. }
  44. /*
  45. * These are the asynchronous signals. SIGPROF is excluded because we want to
  46. * be able to profile all of UML, not just the non-critical sections. If
  47. * profiling is not thread-safe, then that is not my problem. We can disable
  48. * profiling when SMP is enabled in that case.
  49. */
  50. #define SIGIO_BIT 0
  51. #define SIGIO_MASK (1 << SIGIO_BIT)
  52. #define SIGVTALRM_BIT 1
  53. #define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
  54. static int signals_enabled;
  55. static unsigned int pending;
  56. void sig_handler(int sig, struct sigcontext *sc)
  57. {
  58. int enabled;
  59. enabled = signals_enabled;
  60. if (!enabled && (sig == SIGIO)) {
  61. pending |= SIGIO_MASK;
  62. return;
  63. }
  64. block_signals();
  65. sig_handler_common(sig, sc);
  66. set_signals(enabled);
  67. }
  68. static void real_alarm_handler(struct sigcontext *sc)
  69. {
  70. struct uml_pt_regs regs;
  71. if (sc != NULL)
  72. copy_sc(&regs, sc);
  73. regs.is_user = 0;
  74. unblock_signals();
  75. timer_handler(SIGVTALRM, &regs);
  76. }
  77. void alarm_handler(int sig, struct sigcontext *sc)
  78. {
  79. int enabled;
  80. enabled = signals_enabled;
  81. if (!signals_enabled) {
  82. pending |= SIGVTALRM_MASK;
  83. return;
  84. }
  85. block_signals();
  86. real_alarm_handler(sc);
  87. set_signals(enabled);
  88. }
  89. void timer_init(void)
  90. {
  91. set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
  92. SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
  93. }
  94. void set_sigstack(void *sig_stack, int size)
  95. {
  96. stack_t stack = ((stack_t) { .ss_flags = 0,
  97. .ss_sp = (__ptr_t) sig_stack,
  98. .ss_size = size - sizeof(void *) });
  99. if (sigaltstack(&stack, NULL) != 0)
  100. panic("enabling signal stack failed, errno = %d\n", errno);
  101. }
  102. void remove_sigstack(void)
  103. {
  104. stack_t stack = ((stack_t) { .ss_flags = SS_DISABLE,
  105. .ss_sp = NULL,
  106. .ss_size = 0 });
  107. if (sigaltstack(&stack, NULL) != 0)
  108. panic("disabling signal stack failed, errno = %d\n", errno);
  109. }
  110. void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
  111. void handle_signal(int sig, struct sigcontext *sc)
  112. {
  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, sc);
  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. extern void hard_handler(int sig);
  147. void set_handler(int sig, void (*handler)(int), int flags, ...)
  148. {
  149. struct sigaction action;
  150. va_list ap;
  151. sigset_t sig_mask;
  152. int mask;
  153. handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
  154. action.sa_handler = hard_handler;
  155. sigemptyset(&action.sa_mask);
  156. va_start(ap, flags);
  157. while ((mask = va_arg(ap, int)) != -1)
  158. sigaddset(&action.sa_mask, mask);
  159. va_end(ap);
  160. if (sig == SIGSEGV)
  161. flags |= SA_NODEFER;
  162. action.sa_flags = flags;
  163. action.sa_restorer = NULL;
  164. if (sigaction(sig, &action, NULL) < 0)
  165. panic("sigaction failed - errno = %d\n", errno);
  166. sigemptyset(&sig_mask);
  167. sigaddset(&sig_mask, sig);
  168. if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
  169. panic("sigprocmask failed - errno = %d\n", errno);
  170. }
  171. int change_sig(int signal, int on)
  172. {
  173. sigset_t sigset, old;
  174. sigemptyset(&sigset);
  175. sigaddset(&sigset, signal);
  176. if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, &old) < 0)
  177. return -errno;
  178. return !sigismember(&old, signal);
  179. }
  180. void block_signals(void)
  181. {
  182. signals_enabled = 0;
  183. /*
  184. * This must return with signals disabled, so this barrier
  185. * ensures that writes are flushed out before the return.
  186. * This might matter if gcc figures out how to inline this and
  187. * decides to shuffle this code into the caller.
  188. */
  189. barrier();
  190. }
  191. void unblock_signals(void)
  192. {
  193. int save_pending;
  194. if (signals_enabled == 1)
  195. return;
  196. /*
  197. * We loop because the IRQ handler returns with interrupts off. So,
  198. * interrupts may have arrived and we need to re-enable them and
  199. * recheck pending.
  200. */
  201. while(1) {
  202. /*
  203. * Save and reset save_pending after enabling signals. This
  204. * way, pending won't be changed while we're reading it.
  205. */
  206. signals_enabled = 1;
  207. /*
  208. * Setting signals_enabled and reading pending must
  209. * happen in this order.
  210. */
  211. barrier();
  212. save_pending = pending;
  213. if (save_pending == 0)
  214. return;
  215. pending = 0;
  216. /*
  217. * We have pending interrupts, so disable signals, as the
  218. * handlers expect them off when they are called. They will
  219. * be enabled again above.
  220. */
  221. signals_enabled = 0;
  222. /*
  223. * Deal with SIGIO first because the alarm handler might
  224. * schedule, leaving the pending SIGIO stranded until we come
  225. * back here.
  226. */
  227. if (save_pending & SIGIO_MASK)
  228. sig_handler_common(SIGIO, NULL);
  229. if (save_pending & SIGVTALRM_MASK)
  230. real_alarm_handler(NULL);
  231. }
  232. }
  233. int get_signals(void)
  234. {
  235. return signals_enabled;
  236. }
  237. int set_signals(int enable)
  238. {
  239. int ret;
  240. if (signals_enabled == enable)
  241. return enable;
  242. ret = signals_enabled;
  243. if (enable)
  244. unblock_signals();
  245. else block_signals();
  246. return ret;
  247. }