signal.c 7.9 KB

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