signal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 <stdbool.h>
  11. #include <errno.h>
  12. #include <signal.h>
  13. #include <string.h>
  14. #include <strings.h>
  15. #include <as-layout.h>
  16. #include <kern_util.h>
  17. #include <os.h>
  18. #include <skas.h>
  19. #include <sysdep/mcontext.h>
  20. #include <um_malloc.h>
  21. #include <sys/ucontext.h>
  22. #include <timetravel.h>
  23. #include "internal.h"
  24. void (*sig_info[NSIG])(int, struct siginfo *, struct uml_pt_regs *, void *mc) = {
  25. [SIGTRAP] = relay_signal,
  26. [SIGFPE] = relay_signal,
  27. [SIGILL] = relay_signal,
  28. [SIGWINCH] = winch,
  29. [SIGBUS] = relay_signal,
  30. [SIGSEGV] = segv_handler,
  31. [SIGIO] = sigio_handler,
  32. [SIGCHLD] = sigchld_handler,
  33. };
  34. static void sig_handler_common(int sig, struct siginfo *si, mcontext_t *mc)
  35. {
  36. struct uml_pt_regs r;
  37. r.is_user = 0;
  38. if (sig == SIGSEGV) {
  39. /* For segfaults, we want the data from the sigcontext. */
  40. get_regs_from_mc(&r, mc);
  41. GET_FAULTINFO_FROM_MC(r.faultinfo, mc);
  42. }
  43. /* enable signals if sig isn't IRQ signal */
  44. if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGCHLD))
  45. unblock_signals_trace();
  46. (*sig_info[sig])(sig, si, &r, mc);
  47. }
  48. /*
  49. * These are the asynchronous signals. SIGPROF is excluded because we want to
  50. * be able to profile all of UML, not just the non-critical sections. If
  51. * profiling is not thread-safe, then that is not my problem. We can disable
  52. * profiling when SMP is enabled in that case.
  53. */
  54. #define SIGIO_BIT 0
  55. #define SIGIO_MASK (1 << SIGIO_BIT)
  56. #define SIGALRM_BIT 1
  57. #define SIGALRM_MASK (1 << SIGALRM_BIT)
  58. #define SIGCHLD_BIT 2
  59. #define SIGCHLD_MASK (1 << SIGCHLD_BIT)
  60. __thread int signals_enabled;
  61. #if IS_ENABLED(CONFIG_UML_TIME_TRAVEL_SUPPORT)
  62. static int signals_blocked, signals_blocked_pending;
  63. #endif
  64. static __thread unsigned int signals_pending;
  65. static __thread unsigned int signals_active;
  66. static void sig_handler(int sig, struct siginfo *si, mcontext_t *mc)
  67. {
  68. int enabled = signals_enabled;
  69. #if IS_ENABLED(CONFIG_UML_TIME_TRAVEL_SUPPORT)
  70. if ((signals_blocked ||
  71. __atomic_load_n(&signals_blocked_pending, __ATOMIC_SEQ_CST)) &&
  72. (sig == SIGIO)) {
  73. /* increment so unblock will do another round */
  74. __atomic_add_fetch(&signals_blocked_pending, 1,
  75. __ATOMIC_SEQ_CST);
  76. return;
  77. }
  78. #endif
  79. if (!enabled && (sig == SIGIO)) {
  80. /*
  81. * In TT_MODE_EXTERNAL, need to still call time-travel
  82. * handlers. This will mark signals_pending by itself
  83. * (only if necessary.)
  84. * Note we won't get here if signals are hard-blocked
  85. * (which is handled above), in that case the hard-
  86. * unblock will handle things.
  87. */
  88. if (time_travel_mode == TT_MODE_EXTERNAL)
  89. sigio_run_timetravel_handlers();
  90. else
  91. signals_pending |= SIGIO_MASK;
  92. return;
  93. }
  94. if (!enabled && (sig == SIGCHLD)) {
  95. signals_pending |= SIGCHLD_MASK;
  96. return;
  97. }
  98. block_signals_trace();
  99. sig_handler_common(sig, si, mc);
  100. um_set_signals_trace(enabled);
  101. }
  102. static void timer_real_alarm_handler(mcontext_t *mc)
  103. {
  104. struct uml_pt_regs regs;
  105. if (mc != NULL)
  106. get_regs_from_mc(&regs, mc);
  107. else
  108. memset(&regs, 0, sizeof(regs));
  109. timer_handler(SIGALRM, NULL, &regs);
  110. }
  111. static void timer_alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
  112. {
  113. int enabled;
  114. enabled = signals_enabled;
  115. if (!signals_enabled) {
  116. signals_pending |= SIGALRM_MASK;
  117. return;
  118. }
  119. block_signals_trace();
  120. signals_active |= SIGALRM_MASK;
  121. timer_real_alarm_handler(mc);
  122. signals_active &= ~SIGALRM_MASK;
  123. um_set_signals_trace(enabled);
  124. }
  125. void deliver_alarm(void) {
  126. timer_alarm_handler(SIGALRM, NULL, NULL);
  127. }
  128. void timer_set_signal_handler(void)
  129. {
  130. set_handler(SIGALRM);
  131. }
  132. int timer_alarm_pending(void)
  133. {
  134. return !!(signals_pending & SIGALRM_MASK);
  135. }
  136. void set_sigstack(void *sig_stack, int size)
  137. {
  138. stack_t stack = {
  139. .ss_flags = 0,
  140. .ss_sp = sig_stack,
  141. .ss_size = size
  142. };
  143. if (sigaltstack(&stack, NULL) != 0)
  144. panic("enabling signal stack failed, errno = %d\n", errno);
  145. }
  146. static void sigusr1_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
  147. {
  148. uml_pm_wake();
  149. }
  150. void register_pm_wake_signal(void)
  151. {
  152. set_handler(SIGUSR1);
  153. }
  154. static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
  155. [SIGSEGV] = sig_handler,
  156. [SIGBUS] = sig_handler,
  157. [SIGILL] = sig_handler,
  158. [SIGFPE] = sig_handler,
  159. [SIGTRAP] = sig_handler,
  160. [SIGIO] = sig_handler,
  161. [SIGWINCH] = sig_handler,
  162. /* SIGCHLD is only actually registered in seccomp mode. */
  163. [SIGCHLD] = sig_handler,
  164. [SIGALRM] = timer_alarm_handler,
  165. [SIGUSR1] = sigusr1_handler,
  166. };
  167. static void hard_handler(int sig, siginfo_t *si, void *p)
  168. {
  169. ucontext_t *uc = p;
  170. mcontext_t *mc = &uc->uc_mcontext;
  171. int save_errno = errno;
  172. (*handlers[sig])(sig, (struct siginfo *)si, mc);
  173. errno = save_errno;
  174. }
  175. void set_handler(int sig)
  176. {
  177. struct sigaction action;
  178. int flags = SA_SIGINFO | SA_ONSTACK;
  179. sigset_t sig_mask;
  180. action.sa_sigaction = hard_handler;
  181. /* block irq ones */
  182. sigemptyset(&action.sa_mask);
  183. sigaddset(&action.sa_mask, SIGIO);
  184. sigaddset(&action.sa_mask, SIGWINCH);
  185. sigaddset(&action.sa_mask, SIGALRM);
  186. if (using_seccomp)
  187. sigaddset(&action.sa_mask, SIGCHLD);
  188. if (sig == SIGSEGV)
  189. flags |= SA_NODEFER;
  190. if (sigismember(&action.sa_mask, sig))
  191. flags |= SA_RESTART; /* if it's an irq signal */
  192. action.sa_flags = flags;
  193. action.sa_restorer = NULL;
  194. if (sigaction(sig, &action, NULL) < 0)
  195. panic("sigaction failed - errno = %d\n", errno);
  196. sigemptyset(&sig_mask);
  197. sigaddset(&sig_mask, sig);
  198. if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
  199. panic("sigprocmask failed - errno = %d\n", errno);
  200. }
  201. void send_sigio_to_self(void)
  202. {
  203. kill(os_getpid(), SIGIO);
  204. }
  205. int change_sig(int signal, int on)
  206. {
  207. sigset_t sigset;
  208. sigemptyset(&sigset);
  209. sigaddset(&sigset, signal);
  210. if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
  211. return -errno;
  212. return 0;
  213. }
  214. static inline void __block_signals(void)
  215. {
  216. if (!signals_enabled)
  217. return;
  218. os_local_ipi_disable();
  219. barrier();
  220. signals_enabled = 0;
  221. }
  222. static inline void __unblock_signals(void)
  223. {
  224. if (signals_enabled)
  225. return;
  226. signals_enabled = 1;
  227. barrier();
  228. os_local_ipi_enable();
  229. }
  230. void block_signals(void)
  231. {
  232. __block_signals();
  233. /*
  234. * This must return with signals disabled, so this barrier
  235. * ensures that writes are flushed out before the return.
  236. * This might matter if gcc figures out how to inline this and
  237. * decides to shuffle this code into the caller.
  238. */
  239. barrier();
  240. }
  241. void unblock_signals(void)
  242. {
  243. int save_pending;
  244. if (signals_enabled == 1)
  245. return;
  246. __unblock_signals();
  247. #if IS_ENABLED(CONFIG_UML_TIME_TRAVEL_SUPPORT)
  248. deliver_time_travel_irqs();
  249. #endif
  250. /*
  251. * We loop because the IRQ handler returns with interrupts off. So,
  252. * interrupts may have arrived and we need to re-enable them and
  253. * recheck signals_pending.
  254. */
  255. while (1) {
  256. /*
  257. * Save and reset save_pending after enabling signals. This
  258. * way, signals_pending won't be changed while we're reading it.
  259. *
  260. * Setting signals_enabled and reading signals_pending must
  261. * happen in this order, so have the barrier here.
  262. */
  263. barrier();
  264. save_pending = signals_pending;
  265. if (save_pending == 0)
  266. return;
  267. signals_pending = 0;
  268. /*
  269. * We have pending interrupts, so disable signals, as the
  270. * handlers expect them off when they are called. They will
  271. * be enabled again above. We need to trace this, as we're
  272. * expected to be enabling interrupts already, but any more
  273. * tracing that happens inside the handlers we call for the
  274. * pending signals will mess up the tracing state.
  275. */
  276. __block_signals();
  277. um_trace_signals_off();
  278. /*
  279. * Deal with SIGIO first because the alarm handler might
  280. * schedule, leaving the pending SIGIO stranded until we come
  281. * back here.
  282. *
  283. * SIGIO's handler doesn't use siginfo or mcontext,
  284. * so they can be NULL.
  285. */
  286. if (save_pending & SIGIO_MASK)
  287. sig_handler_common(SIGIO, NULL, NULL);
  288. if (save_pending & SIGCHLD_MASK) {
  289. struct uml_pt_regs regs = {};
  290. sigchld_handler(SIGCHLD, NULL, &regs, NULL);
  291. }
  292. /* Do not reenter the handler */
  293. if ((save_pending & SIGALRM_MASK) && (!(signals_active & SIGALRM_MASK)))
  294. timer_real_alarm_handler(NULL);
  295. /* Rerun the loop only if there is still pending SIGIO and not in TIMER handler */
  296. if (!(signals_pending & SIGIO_MASK) && (signals_active & SIGALRM_MASK))
  297. return;
  298. /* Re-enable signals and trace that we're doing so. */
  299. um_trace_signals_on();
  300. __unblock_signals();
  301. }
  302. }
  303. int um_get_signals(void)
  304. {
  305. return signals_enabled;
  306. }
  307. int um_set_signals(int enable)
  308. {
  309. int ret;
  310. if (signals_enabled == enable)
  311. return enable;
  312. ret = signals_enabled;
  313. if (enable)
  314. unblock_signals();
  315. else block_signals();
  316. return ret;
  317. }
  318. int um_set_signals_trace(int enable)
  319. {
  320. int ret;
  321. if (signals_enabled == enable)
  322. return enable;
  323. ret = signals_enabled;
  324. if (enable)
  325. unblock_signals_trace();
  326. else
  327. block_signals_trace();
  328. return ret;
  329. }
  330. #if IS_ENABLED(CONFIG_UML_TIME_TRAVEL_SUPPORT)
  331. void mark_sigio_pending(void)
  332. {
  333. /*
  334. * It would seem that this should be atomic so
  335. * it isn't a read-modify-write with a signal
  336. * that could happen in the middle, losing the
  337. * value set by the signal.
  338. *
  339. * However, this function is only called when in
  340. * time-travel=ext simulation mode, in which case
  341. * the only signal ever pending is SIGIO, which
  342. * is blocked while this can be called, and the
  343. * timer signal (SIGALRM) cannot happen.
  344. */
  345. signals_pending |= SIGIO_MASK;
  346. }
  347. void block_signals_hard(void)
  348. {
  349. signals_blocked++;
  350. barrier();
  351. }
  352. void unblock_signals_hard(void)
  353. {
  354. static bool unblocking;
  355. if (!signals_blocked)
  356. panic("unblocking signals while not blocked");
  357. if (--signals_blocked)
  358. return;
  359. /*
  360. * Must be set to 0 before we check pending so the
  361. * SIGIO handler will run as normal unless we're still
  362. * going to process signals_blocked_pending.
  363. */
  364. barrier();
  365. /*
  366. * Note that block_signals_hard()/unblock_signals_hard() can be called
  367. * within the unblock_signals()/sigio_run_timetravel_handlers() below.
  368. * This would still be prone to race conditions since it's actually a
  369. * call _within_ e.g. vu_req_read_message(), where we observed this
  370. * issue, which loops. Thus, if the inner call handles the recorded
  371. * pending signals, we can get out of the inner call with the real
  372. * signal hander no longer blocked, and still have a race. Thus don't
  373. * handle unblocking in the inner call, if it happens, but only in
  374. * the outermost call - 'unblocking' serves as an ownership for the
  375. * signals_blocked_pending decrement.
  376. */
  377. if (unblocking)
  378. return;
  379. unblocking = true;
  380. while (__atomic_load_n(&signals_blocked_pending, __ATOMIC_SEQ_CST)) {
  381. if (signals_enabled) {
  382. /* signals are enabled so we can touch this */
  383. signals_pending |= SIGIO_MASK;
  384. /*
  385. * this is a bit inefficient, but that's
  386. * not really important
  387. */
  388. block_signals();
  389. unblock_signals();
  390. } else {
  391. /*
  392. * we need to run time-travel handlers even
  393. * if not enabled
  394. */
  395. sigio_run_timetravel_handlers();
  396. }
  397. /*
  398. * The decrement of signals_blocked_pending must be atomic so
  399. * that the signal handler will either happen before or after
  400. * the decrement, not during a read-modify-write:
  401. * - If it happens before, it can increment it and we'll
  402. * decrement it and do another round in the loop.
  403. * - If it happens after it'll see 0 for both signals_blocked
  404. * and signals_blocked_pending and thus run the handler as
  405. * usual (subject to signals_enabled, but that's unrelated.)
  406. *
  407. * Note that a call to unblock_signals_hard() within the calls
  408. * to unblock_signals() or sigio_run_timetravel_handlers() above
  409. * will do nothing due to the 'unblocking' state, so this cannot
  410. * underflow as the only one decrementing will be the outermost
  411. * one.
  412. */
  413. if (__atomic_sub_fetch(&signals_blocked_pending, 1,
  414. __ATOMIC_SEQ_CST) < 0)
  415. panic("signals_blocked_pending underflow");
  416. }
  417. unblocking = false;
  418. }
  419. #endif