shim_signal.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_signal.c
  17. *
  18. * This file contains codes to handle signals and exceptions passed from PAL.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_utils.h>
  22. #include <shim_table.h>
  23. #include <shim_thread.h>
  24. #include <shim_handle.h>
  25. #include <shim_vma.h>
  26. #include <shim_checkpoint.h>
  27. #include <shim_signal.h>
  28. #include <pal.h>
  29. static struct shim_signal **
  30. allocate_signal_log (struct shim_thread * thread, int sig)
  31. {
  32. struct shim_signal_log * log = &thread->signal_logs[sig - 1];
  33. int head, tail, old_tail;
  34. do {
  35. head = atomic_read(&log->head);
  36. old_tail = tail = atomic_read(&log->tail);
  37. if (head == tail + 1 || (!head && tail == (MAX_SIGNAL_LOG - 1)))
  38. return NULL;
  39. tail = (tail == MAX_SIGNAL_LOG - 1) ? 0 : tail + 1;
  40. } while (atomic_cmpxchg(&log->tail, old_tail, tail) == tail);
  41. atomic_inc(&thread->has_signal);
  42. return &log->logs[old_tail];
  43. }
  44. static struct shim_signal *
  45. fetch_signal_log (shim_tcb_t * tcb, struct shim_thread * thread, int sig)
  46. {
  47. struct shim_signal_log * log = &thread->signal_logs[sig - 1];
  48. struct shim_signal * signal = NULL;
  49. int head, tail, old_head;
  50. while (1) {
  51. old_head = head = atomic_read(&log->head);
  52. tail = atomic_read(&log->tail);
  53. if (head == tail)
  54. return NULL;
  55. if (!(signal = log->logs[head]))
  56. return NULL;
  57. log->logs[head] = NULL;
  58. head = (head == MAX_SIGNAL_LOG - 1) ? 0 : head + 1;
  59. if (atomic_cmpxchg(&log->head, old_head, head) == old_head)
  60. break;
  61. log->logs[old_head] = signal;
  62. }
  63. atomic_dec(&thread->has_signal);
  64. return signal;
  65. }
  66. static void
  67. __handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal);
  68. static void __store_info (siginfo_t * info, struct shim_signal * signal)
  69. {
  70. if (info)
  71. memcpy(&signal->info, info, sizeof(siginfo_t));
  72. }
  73. void __store_context (shim_tcb_t * tcb, PAL_CONTEXT * pal_context,
  74. struct shim_signal * signal)
  75. {
  76. ucontext_t * context = &signal->context;
  77. if (tcb && tcb->context.syscall_nr) {
  78. struct shim_context * ct = &tcb->context;
  79. context->uc_mcontext.gregs[REG_RSP] = (unsigned long) ct->sp;
  80. context->uc_mcontext.gregs[REG_RIP] = (unsigned long) ct->ret_ip;
  81. if (ct->regs) {
  82. struct shim_regs * regs = ct->regs;
  83. context->uc_mcontext.gregs[REG_R15] = regs->r15;
  84. context->uc_mcontext.gregs[REG_R14] = regs->r14;
  85. context->uc_mcontext.gregs[REG_R13] = regs->r13;
  86. context->uc_mcontext.gregs[REG_R9] = regs->r9;
  87. context->uc_mcontext.gregs[REG_R8] = regs->r8;
  88. context->uc_mcontext.gregs[REG_RCX] = regs->rcx;
  89. context->uc_mcontext.gregs[REG_RDX] = regs->rdx;
  90. context->uc_mcontext.gregs[REG_RSI] = regs->rsi;
  91. context->uc_mcontext.gregs[REG_RDI] = regs->rdi;
  92. context->uc_mcontext.gregs[REG_R12] = regs->r12;
  93. context->uc_mcontext.gregs[REG_RBX] = regs->rbx;
  94. context->uc_mcontext.gregs[REG_RBP] = regs->rbp;
  95. }
  96. signal->context_stored = true;
  97. return;
  98. }
  99. if (pal_context) {
  100. memcpy(context->uc_mcontext.gregs, pal_context, sizeof(PAL_CONTEXT));
  101. signal->context_stored = true;
  102. }
  103. }
  104. void deliver_signal (siginfo_t * info, PAL_CONTEXT * context)
  105. {
  106. shim_tcb_t * tcb = SHIM_GET_TLS();
  107. struct shim_thread * cur_thread = (struct shim_thread *) tcb->tp;
  108. int sig = info->si_signo;
  109. __disable_preempt(tcb);
  110. struct shim_signal * signal = __alloca(sizeof(struct shim_signal));
  111. /* save in signal */
  112. memset(signal, 0, sizeof(struct shim_signal));
  113. __store_info(info, signal);
  114. __store_context(tcb, context, signal);
  115. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1)
  116. goto delay;
  117. if (__sigismember(&cur_thread->signal_mask, sig))
  118. goto delay;
  119. __handle_signal(tcb, sig, &signal->context);
  120. __handle_one_signal(tcb, sig, signal);
  121. goto out;
  122. delay:
  123. {
  124. if (!(signal = remalloc(signal,sizeof(struct shim_signal))))
  125. goto out;
  126. struct shim_signal ** signal_log = allocate_signal_log(cur_thread, sig);
  127. if (!signal_log) {
  128. sys_printf("signal queue is full (TID = %u, SIG = %d)\n",
  129. tcb->tid, sig);
  130. free(signal);
  131. goto out;
  132. }
  133. *signal_log = signal;
  134. }
  135. out:
  136. __enable_preempt(tcb);
  137. }
  138. #define ALLOC_SIGINFO(signo, member, value) \
  139. ({ \
  140. siginfo_t * _info = __alloca(sizeof(siginfo_t)); \
  141. memset(_info, 0, sizeof(siginfo_t)); \
  142. _info->si_signo = (signo); \
  143. _info->member = (value); \
  144. _info; \
  145. })
  146. #ifdef __x86_64__
  147. #define IP rip
  148. #else
  149. #define IP eip
  150. #endif
  151. #define is_internal(context) \
  152. ((context) && \
  153. (void *) (context)->IP >= (void *) &__code_address && \
  154. (void *) (context)->IP < (void *) &__code_address_end)
  155. #define internal_fault(errstr, addr, context) \
  156. do { \
  157. IDTYPE tid = get_cur_tid(); \
  158. if (is_internal((context))) \
  159. sys_printf(errstr " at %p (IP = +0x%lx, VMID = %u, TID = %u)\n",\
  160. arg, \
  161. (void *) context->IP - (void *) &__load_address, \
  162. cur_process.vmid, IS_INTERNAL_TID(tid) ? 0 : tid); \
  163. else \
  164. sys_printf(errstr " at %p (IP = %p, VMID = %u, TID = %u)\n", \
  165. arg, context ? context->IP : 0, \
  166. cur_process.vmid, IS_INTERNAL_TID(tid) ? 0 : tid); \
  167. } while (0)
  168. static void divzero_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  169. {
  170. if (IS_INTERNAL_TID(get_cur_tid()) || is_internal(context)) {
  171. internal_fault("Internal arithmetic fault", arg, context);
  172. pause();
  173. goto ret_exception;
  174. }
  175. if (context)
  176. debug("arithmetic fault at %p\n", context->IP);
  177. deliver_signal(ALLOC_SIGINFO(SIGFPE, si_addr, (void *) arg), context);
  178. ret_exception:
  179. DkExceptionReturn(event);
  180. }
  181. static void memfault_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  182. {
  183. if (IS_INTERNAL_TID(get_cur_tid()) || is_internal(context)) {
  184. internal:
  185. internal_fault("Internal memory fault", arg, context);
  186. pause();
  187. goto ret_exception;
  188. }
  189. struct shim_vma * vma = NULL;
  190. if (!(lookup_supervma((void *) arg, 0, &vma)) &&
  191. !(vma->flags & VMA_INTERNAL)) {
  192. int signo = SIGSEGV;
  193. if (context)
  194. debug("memory fault at %p (IP = %p)\n", arg, context->IP);
  195. if (vma)
  196. put_vma(vma);
  197. deliver_signal(ALLOC_SIGINFO(signo, si_addr, (void *) arg), context);
  198. } else {
  199. if (vma)
  200. put_vma(vma);
  201. goto internal;
  202. }
  203. ret_exception:
  204. DkExceptionReturn(event);
  205. }
  206. static void illegal_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  207. {
  208. if (IS_INTERNAL_TID(get_cur_tid()) || is_internal(context)) {
  209. internal:
  210. internal_fault("Internal memory fault", arg, context);
  211. pause();
  212. goto ret_exception;
  213. }
  214. struct shim_vma * vma = NULL;
  215. if (!(lookup_supervma((void *) arg, 0, &vma)) &&
  216. !(vma->flags & VMA_INTERNAL)) {
  217. if (context)
  218. debug("illegal instruction at %p\n", context->IP);
  219. if (vma)
  220. put_vma(vma);
  221. deliver_signal(ALLOC_SIGINFO(SIGILL, si_addr, (void *) arg), context);
  222. } else {
  223. if (vma)
  224. put_vma(vma);
  225. goto internal;
  226. }
  227. ret_exception:
  228. DkExceptionReturn(event);
  229. }
  230. static void quit_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  231. {
  232. if (IS_INTERNAL_TID(get_cur_tid()))
  233. goto ret_exception;
  234. deliver_signal(ALLOC_SIGINFO(SIGTERM, si_pid, 0), NULL);
  235. ret_exception:
  236. DkExceptionReturn(event);
  237. }
  238. bool ask_for_checkpoint = false;
  239. static void suspend_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  240. {
  241. if (IS_INTERNAL_TID(get_cur_tid()))
  242. goto ret_exception;
  243. if (ask_for_checkpoint) {
  244. int ans = message_confirm("checkpoint execution "
  245. "(\'k\' to kill the process)",
  246. "yk");
  247. if (ans == 'K' || ans == 'k')
  248. goto kill;
  249. if (ans != 'Y' && ans != 'y')
  250. goto ret_exception;
  251. shim_tcb_t * tcb = SHIM_GET_TLS();
  252. assert(tcb && tcb->tp);
  253. struct shim_signal signal;
  254. __store_context(tcb, context, &signal);
  255. IDTYPE session = 0;
  256. char cpdir[20];
  257. if (create_dir("checkpoint-", cpdir, 20, NULL) < 0)
  258. goto ret_exception;
  259. sys_printf("creating checkpoint \"%s\"...\n", cpdir);
  260. if (create_checkpoint(cpdir, &session) < 0)
  261. goto ret_exception;
  262. ipc_checkpoint_send(cpdir, session);
  263. kill_all_threads(tcb->tp, CHECKPOINT_REQUESTED, SIGINT);
  264. join_checkpoint(tcb->tp, &signal.context);
  265. goto ret_exception;
  266. }
  267. kill:
  268. deliver_signal(ALLOC_SIGINFO(SIGINT, si_pid, 0), NULL);
  269. ret_exception:
  270. DkExceptionReturn(event);
  271. }
  272. static void resume_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  273. {
  274. if (IS_INTERNAL_TID(get_cur_tid()))
  275. goto ret_exception;
  276. shim_tcb_t * tcb = SHIM_GET_TLS();
  277. assert(tcb && tcb->tp);
  278. __disable_preempt(tcb);
  279. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
  280. tcb->context.preempt |= SIGNAL_DELAYED;
  281. __enable_preempt(tcb);
  282. goto ret_exception;
  283. }
  284. __handle_signal(tcb, 0, NULL);
  285. __enable_preempt(tcb);
  286. ret_exception:
  287. DkExceptionReturn(event);
  288. }
  289. int init_signal (void)
  290. {
  291. DkSetExceptionHandler(&divzero_upcall, PAL_EVENT_DIVZERO, 0);
  292. DkSetExceptionHandler(&memfault_upcall, PAL_EVENT_MEMFAULT, 0);
  293. DkSetExceptionHandler(&illegal_upcall, PAL_EVENT_ILLEGAL, 0);
  294. DkSetExceptionHandler(&quit_upcall, PAL_EVENT_QUIT, 0);
  295. DkSetExceptionHandler(&suspend_upcall, PAL_EVENT_SUSPEND, 0);
  296. DkSetExceptionHandler(&resume_upcall, PAL_EVENT_RESUME, 0);
  297. return 0;
  298. }
  299. sigset_t * get_sig_mask (struct shim_thread * thread)
  300. {
  301. if (!thread)
  302. thread = get_cur_thread();
  303. assert(thread);
  304. return &(thread->signal_mask);
  305. }
  306. sigset_t * set_sig_mask (struct shim_thread * thread, sigset_t * set)
  307. {
  308. if (!thread)
  309. thread = get_cur_thread();
  310. assert(thread);
  311. if (set)
  312. memcpy(&thread->signal_mask, set, sizeof(sigset_t));
  313. return &thread->signal_mask;
  314. }
  315. static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *);
  316. static void
  317. __handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal)
  318. {
  319. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  320. struct shim_signal_handle * sighdl = &thread->signal_handles[sig - 1];
  321. void (*handler) (int, siginfo_t *, void *) = NULL;
  322. //void (*restorer) (void) = NULL;
  323. if (signal->info.si_signo == SIGINT &&
  324. signal->info.si_pid == CHECKPOINT_REQUESTED) {
  325. join_checkpoint(thread, &signal->context);
  326. return;
  327. }
  328. if (sig <= NUM_KNOWN_SIGS)
  329. debug("handle %s\n", siglist[sig]);
  330. else
  331. debug("handle signal %d\n", sig);
  332. lock(thread->lock);
  333. if (sighdl->action) {
  334. struct __kernel_sigaction * act = sighdl->action;
  335. /* This is a workaround. The truth is that many program will
  336. use sa_handler as sa_sigaction, because sa_sigaction is
  337. not supported in amd64 */
  338. #ifdef __i386__
  339. handler = (void (*) (int, siginfo_t *, void *)) act->_u._sa_handler;
  340. if (act->sa_flags & SA_SIGINFO)
  341. sa_handler = act->_u._sa_sigaction;
  342. #else
  343. handler = (void (*) (int, siginfo_t *, void *)) act->k_sa_handler;
  344. #endif
  345. if (act->sa_flags & SA_RESETHAND) {
  346. sighdl->action = NULL;
  347. free(act);
  348. }
  349. }
  350. unlock(thread->lock);
  351. if ((void *) handler == (void *) 1) /* SIG_IGN */
  352. return;
  353. if (!handler && !(handler = default_sighandler[sig - 1]))
  354. return;
  355. /* if the context is never stored in the signal, it means the
  356. signal is handled during system calls, and before the thread
  357. is resumed. */
  358. if (!signal->context_stored)
  359. __store_context(tcb, NULL, signal);
  360. struct shim_context * context = NULL;
  361. if (tcb->context.syscall_nr) {
  362. context = __alloca(sizeof(struct shim_context));
  363. memcpy(context, &tcb->context, sizeof(struct shim_context));
  364. tcb->context.syscall_nr = 0;
  365. tcb->context.next = context;
  366. }
  367. debug("run signal handler %p (%d, %p, %p)\n", handler, sig, &signal->info,
  368. &signal->context);
  369. (*handler) (sig, &signal->info, &signal->context);
  370. if (context)
  371. memcpy(&tcb->context, context, sizeof(struct shim_context));
  372. }
  373. void __handle_signal (shim_tcb_t * tcb, int sig, ucontext_t * uc)
  374. {
  375. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  376. int begin_sig = 1, end_sig = NUM_KNOWN_SIGS;
  377. if (sig)
  378. end_sig = (begin_sig = sig) + 1;
  379. sig = begin_sig;
  380. if (!thread->has_signal.counter)
  381. return;
  382. while (atomic_read(&thread->has_signal)) {
  383. struct shim_signal * signal = NULL;
  384. for ( ; sig < end_sig ; sig++)
  385. if (!__sigismember(&thread->signal_mask, sig) &&
  386. (signal = fetch_signal_log(tcb, thread, sig)))
  387. break;
  388. if (!signal)
  389. break;
  390. if (!signal->context_stored)
  391. __store_context(tcb, NULL, signal);
  392. __handle_one_signal(tcb, sig, signal);
  393. free(signal);
  394. DkThreadYieldExecution();
  395. }
  396. tcb->context.preempt &= ~SIGNAL_DELAYED;
  397. }
  398. void handle_signal (bool delayed_only)
  399. {
  400. shim_tcb_t * tcb = SHIM_GET_TLS();
  401. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  402. assert(tcb && tcb->tp);
  403. /* Fast path */
  404. if (!thread->has_signal.counter)
  405. return;
  406. __disable_preempt(tcb);
  407. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
  408. tcb->context.preempt |= SIGNAL_DELAYED;
  409. goto out;
  410. }
  411. if (delayed_only && !(tcb->context.preempt & SIGNAL_DELAYED))
  412. goto out;
  413. __handle_signal(tcb, 0, NULL);
  414. out:
  415. __enable_preempt(tcb);
  416. }
  417. void append_signal (struct shim_thread * thread, int sig, siginfo_t * info)
  418. {
  419. struct shim_signal * signal = malloc(sizeof(struct shim_signal));
  420. if (!signal)
  421. return;
  422. /* save in signal */
  423. if (info) {
  424. __store_info(info, signal);
  425. signal->context_stored = false;
  426. } else {
  427. memset(signal, 0, sizeof(struct shim_signal));
  428. }
  429. struct shim_signal ** signal_log = allocate_signal_log(thread, sig);
  430. if (signal_log) {
  431. *signal_log = signal;
  432. debug("resuming thread %u\n", thread->tid);
  433. DkThreadResume(thread->pal_handle);
  434. } else {
  435. sys_printf("signal queue is full (TID = %u, SIG = %d)\n",
  436. thread->tid, sig);
  437. free(signal);
  438. }
  439. }
  440. static void sighandler_kill (int sig, siginfo_t * info, void * ucontext)
  441. {
  442. if (sig <= NUM_KNOWN_SIGS)
  443. debug("killed by %s\n", siglist[sig]);
  444. else
  445. debug("killed by signal %d\n", sig);
  446. if (!info->si_pid)
  447. switch(sig) {
  448. case SIGTERM:
  449. case SIGINT:
  450. shim_do_kill(-1, sig);
  451. break;
  452. }
  453. try_process_exit(0);
  454. DkThreadExit();
  455. }
  456. static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *) =
  457. {
  458. /* SIGHUP */ &sighandler_kill,
  459. /* SIGINT */ &sighandler_kill,
  460. /* SIGQUIT */ &sighandler_kill,
  461. /* SIGILL */ &sighandler_kill,
  462. /* SIGTRAP */ NULL,
  463. /* SIGABRT */ &sighandler_kill,
  464. /* SIGBUS */ &sighandler_kill,
  465. /* SIGFPE */ &sighandler_kill,
  466. /* SIGKILL */ &sighandler_kill,
  467. /* SIGUSR1 */ NULL,
  468. /* SIGSEGV */ &sighandler_kill,
  469. /* SIGUSR2 */ NULL,
  470. /* SIGPIPE */ &sighandler_kill,
  471. /* SIGALRM */ NULL,
  472. /* SIGTERM */ &sighandler_kill,
  473. /* SIGSTKFLT */ NULL,
  474. /* SIGCHLD */ NULL,
  475. /* SIGCONT */ NULL,
  476. /* SIGSTOP */ NULL,
  477. /* SIGTSTP */ NULL,
  478. /* SIGTTIN */ NULL,
  479. /* SIGTTOU */ NULL,
  480. };