signal.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 ([email protected])
  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. #include <timetravel.h>
  21. void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *) = {
  22. [SIGTRAP] = relay_signal,
  23. [SIGFPE] = relay_signal,
  24. [SIGILL] = relay_signal,
  25. [SIGWINCH] = winch,
  26. [SIGBUS] = bus_handler,
  27. [SIGSEGV] = segv_handler,
  28. [SIGIO] = sigio_handler,
  29. };
  30. static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc)
  31. {
  32. struct uml_pt_regs r;
  33. int save_errno = errno;
  34. r.is_user = 0;
  35. if (sig == SIGSEGV) {
  36. /* For segfaults, we want the data from the sigcontext. */
  37. get_regs_from_mc(&r, mc);
  38. GET_FAULTINFO_FROM_MC(r.faultinfo, mc);
  39. }
  40. /* enable signals if sig isn't IRQ signal */
  41. if ((sig != SIGIO) && (sig != SIGWINCH))
  42. unblock_signals_trace();
  43. (*sig_info[sig])(sig, si, &r);
  44. errno = save_errno;
  45. }
  46. /*
  47. * These are the asynchronous signals. SIGPROF is excluded because we want to
  48. * be able to profile all of UML, not just the non-critical sections. If
  49. * profiling is not thread-safe, then that is not my problem. We can disable
  50. * profiling when SMP is enabled in that case.
  51. */
  52. #define SIGIO_BIT 0
  53. #define SIGIO_MASK (1 << SIGIO_BIT)
  54. #define SIGALRM_BIT 1
  55. #define SIGALRM_MASK (1 << SIGALRM_BIT)
  56. int signals_enabled;
  57. #ifdef UML_CONFIG_UML_TIME_TRAVEL_SUPPORT
  58. static int signals_blocked;
  59. #else
  60. #define signals_blocked 0
  61. #endif
  62. static unsigned int signals_pending;
  63. static unsigned int signals_active = 0;
  64. void sig_handler(int sig, struct siginfo *si, mcontext_t *mc)
  65. {
  66. int enabled = signals_enabled;
  67. if ((signals_blocked || !enabled) && (sig == SIGIO)) {
  68. /*
  69. * In TT_MODE_EXTERNAL, need to still call time-travel
  70. * handlers unless signals are also blocked for the
  71. * external time message processing. This will mark
  72. * signals_pending by itself (only if necessary.)
  73. */
  74. if (!signals_blocked && time_travel_mode == TT_MODE_EXTERNAL)
  75. sigio_run_timetravel_handlers();
  76. else
  77. signals_pending |= SIGIO_MASK;
  78. return;
  79. }
  80. block_signals_trace();
  81. sig_handler_common(sig, si, mc);
  82. um_set_signals_trace(enabled);
  83. }
  84. static void timer_real_alarm_handler(mcontext_t *mc)
  85. {
  86. struct uml_pt_regs regs;
  87. if (mc != NULL)
  88. get_regs_from_mc(&regs, mc);
  89. else
  90. memset(&regs, 0, sizeof(regs));
  91. timer_handler(SIGALRM, NULL, &regs);
  92. }
  93. void timer_alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
  94. {
  95. int enabled;
  96. enabled = signals_enabled;
  97. if (!signals_enabled) {
  98. signals_pending |= SIGALRM_MASK;
  99. return;
  100. }
  101. block_signals_trace();
  102. signals_active |= SIGALRM_MASK;
  103. timer_real_alarm_handler(mc);
  104. signals_active &= ~SIGALRM_MASK;
  105. um_set_signals_trace(enabled);
  106. }
  107. void deliver_alarm(void) {
  108. timer_alarm_handler(SIGALRM, NULL, NULL);
  109. }
  110. void timer_set_signal_handler(void)
  111. {
  112. set_handler(SIGALRM);
  113. }
  114. void set_sigstack(void *sig_stack, int size)
  115. {
  116. stack_t stack = {
  117. .ss_flags = 0,
  118. .ss_sp = sig_stack,
  119. .ss_size = size
  120. };
  121. if (sigaltstack(&stack, NULL) != 0)
  122. panic("enabling signal stack failed, errno = %d\n", errno);
  123. }
  124. static void sigusr1_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
  125. {
  126. uml_pm_wake();
  127. }
  128. void register_pm_wake_signal(void)
  129. {
  130. set_handler(SIGUSR1);
  131. }
  132. static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
  133. [SIGSEGV] = sig_handler,
  134. [SIGBUS] = sig_handler,
  135. [SIGILL] = sig_handler,
  136. [SIGFPE] = sig_handler,
  137. [SIGTRAP] = sig_handler,
  138. [SIGIO] = sig_handler,
  139. [SIGWINCH] = sig_handler,
  140. [SIGALRM] = timer_alarm_handler,
  141. [SIGUSR1] = sigusr1_handler,
  142. };
  143. static void hard_handler(int sig, siginfo_t *si, void *p)
  144. {
  145. ucontext_t *uc = p;
  146. mcontext_t *mc = &uc->uc_mcontext;
  147. unsigned long pending = 1UL << sig;
  148. do {
  149. int nested, bail;
  150. /*
  151. * pending comes back with one bit set for each
  152. * interrupt that arrived while setting up the stack,
  153. * plus a bit for this interrupt, plus the zero bit is
  154. * set if this is a nested interrupt.
  155. * If bail is true, then we interrupted another
  156. * handler setting up the stack. In this case, we
  157. * have to return, and the upper handler will deal
  158. * with this interrupt.
  159. */
  160. bail = to_irq_stack(&pending);
  161. if (bail)
  162. return;
  163. nested = pending & 1;
  164. pending &= ~1;
  165. while ((sig = ffs(pending)) != 0){
  166. sig--;
  167. pending &= ~(1 << sig);
  168. (*handlers[sig])(sig, (struct siginfo *)si, mc);
  169. }
  170. /*
  171. * Again, pending comes back with a mask of signals
  172. * that arrived while tearing down the stack. If this
  173. * is non-zero, we just go back, set up the stack
  174. * again, and handle the new interrupts.
  175. */
  176. if (!nested)
  177. pending = from_irq_stack(nested);
  178. } while (pending);
  179. }
  180. void set_handler(int sig)
  181. {
  182. struct sigaction action;
  183. int flags = SA_SIGINFO | SA_ONSTACK;
  184. sigset_t sig_mask;
  185. action.sa_sigaction = hard_handler;
  186. /* block irq ones */
  187. sigemptyset(&action.sa_mask);
  188. sigaddset(&action.sa_mask, SIGIO);
  189. sigaddset(&action.sa_mask, SIGWINCH);
  190. sigaddset(&action.sa_mask, SIGALRM);
  191. if (sig == SIGSEGV)
  192. flags |= SA_NODEFER;
  193. if (sigismember(&action.sa_mask, sig))
  194. flags |= SA_RESTART; /* if it's an irq signal */
  195. action.sa_flags = flags;
  196. action.sa_restorer = NULL;
  197. if (sigaction(sig, &action, NULL) < 0)
  198. panic("sigaction failed - errno = %d\n", errno);
  199. sigemptyset(&sig_mask);
  200. sigaddset(&sig_mask, sig);
  201. if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
  202. panic("sigprocmask failed - errno = %d\n", errno);
  203. }
  204. void send_sigio_to_self(void)
  205. {
  206. kill(os_getpid(), SIGIO);
  207. }
  208. int change_sig(int signal, int on)
  209. {
  210. sigset_t sigset;
  211. sigemptyset(&sigset);
  212. sigaddset(&sigset, signal);
  213. if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
  214. return -errno;
  215. return 0;
  216. }
  217. void block_signals(void)
  218. {
  219. signals_enabled = 0;
  220. /*
  221. * This must return with signals disabled, so this barrier
  222. * ensures that writes are flushed out before the return.
  223. * This might matter if gcc figures out how to inline this and
  224. * decides to shuffle this code into the caller.
  225. */
  226. barrier();
  227. }
  228. void unblock_signals(void)
  229. {
  230. int save_pending;
  231. if (signals_enabled == 1)
  232. return;
  233. signals_enabled = 1;
  234. #ifdef UML_CONFIG_UML_TIME_TRAVEL_SUPPORT
  235. deliver_time_travel_irqs();
  236. #endif
  237. /*
  238. * We loop because the IRQ handler returns with interrupts off. So,
  239. * interrupts may have arrived and we need to re-enable them and
  240. * recheck signals_pending.
  241. */
  242. while (1) {
  243. /*
  244. * Save and reset save_pending after enabling signals. This
  245. * way, signals_pending won't be changed while we're reading it.
  246. *
  247. * Setting signals_enabled and reading signals_pending must
  248. * happen in this order, so have the barrier here.
  249. */
  250. barrier();
  251. save_pending = signals_pending;
  252. if (save_pending == 0)
  253. return;
  254. signals_pending = 0;
  255. /*
  256. * We have pending interrupts, so disable signals, as the
  257. * handlers expect them off when they are called. They will
  258. * be enabled again above. We need to trace this, as we're
  259. * expected to be enabling interrupts already, but any more
  260. * tracing that happens inside the handlers we call for the
  261. * pending signals will mess up the tracing state.
  262. */
  263. signals_enabled = 0;
  264. um_trace_signals_off();
  265. /*
  266. * Deal with SIGIO first because the alarm handler might
  267. * schedule, leaving the pending SIGIO stranded until we come
  268. * back here.
  269. *
  270. * SIGIO's handler doesn't use siginfo or mcontext,
  271. * so they can be NULL.
  272. */
  273. if (save_pending & SIGIO_MASK)
  274. sig_handler_common(SIGIO, NULL, NULL);
  275. /* Do not reenter the handler */
  276. if ((save_pending & SIGALRM_MASK) && (!(signals_active & SIGALRM_MASK)))
  277. timer_real_alarm_handler(NULL);
  278. /* Rerun the loop only if there is still pending SIGIO and not in TIMER handler */
  279. if (!(signals_pending & SIGIO_MASK) && (signals_active & SIGALRM_MASK))
  280. return;
  281. /* Re-enable signals and trace that we're doing so. */
  282. um_trace_signals_on();
  283. signals_enabled = 1;
  284. }
  285. }
  286. int um_set_signals(int enable)
  287. {
  288. int ret;
  289. if (signals_enabled == enable)
  290. return enable;
  291. ret = signals_enabled;
  292. if (enable)
  293. unblock_signals();
  294. else block_signals();
  295. return ret;
  296. }
  297. int um_set_signals_trace(int enable)
  298. {
  299. int ret;
  300. if (signals_enabled == enable)
  301. return enable;
  302. ret = signals_enabled;
  303. if (enable)
  304. unblock_signals_trace();
  305. else
  306. block_signals_trace();
  307. return ret;
  308. }
  309. #ifdef UML_CONFIG_UML_TIME_TRAVEL_SUPPORT
  310. void mark_sigio_pending(void)
  311. {
  312. signals_pending |= SIGIO_MASK;
  313. }
  314. void block_signals_hard(void)
  315. {
  316. if (signals_blocked)
  317. return;
  318. signals_blocked = 1;
  319. barrier();
  320. }
  321. void unblock_signals_hard(void)
  322. {
  323. if (!signals_blocked)
  324. return;
  325. /* Must be set to 0 before we check the pending bits etc. */
  326. signals_blocked = 0;
  327. barrier();
  328. if (signals_pending && signals_enabled) {
  329. /* this is a bit inefficient, but that's not really important */
  330. block_signals();
  331. unblock_signals();
  332. } else if (signals_pending & SIGIO_MASK) {
  333. /* we need to run time-travel handlers even if not enabled */
  334. sigio_run_timetravel_handlers();
  335. }
  336. }
  337. #endif
  338. int os_is_signal_stack(void)
  339. {
  340. stack_t ss;
  341. sigaltstack(NULL, &ss);
  342. return ss.ss_flags & SS_ONSTACK;
  343. }