shim_signal.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 <shim_unistd.h>
  29. #include <pal.h>
  30. static struct shim_signal **
  31. allocate_signal_log (struct shim_thread * thread, int sig)
  32. {
  33. if (!thread->signal_logs)
  34. return NULL;
  35. struct shim_signal_log * log = &thread->signal_logs[sig - 1];
  36. int head, tail, old_tail;
  37. do {
  38. head = atomic_read(&log->head);
  39. old_tail = tail = atomic_read(&log->tail);
  40. if (head == tail + 1 || (!head && tail == (MAX_SIGNAL_LOG - 1)))
  41. return NULL;
  42. tail = (tail == MAX_SIGNAL_LOG - 1) ? 0 : tail + 1;
  43. } while (atomic_cmpxchg(&log->tail, old_tail, tail) == tail);
  44. atomic_inc(&thread->has_signal);
  45. return &log->logs[old_tail];
  46. }
  47. static struct shim_signal *
  48. fetch_signal_log (shim_tcb_t * tcb, struct shim_thread * thread, int sig)
  49. {
  50. struct shim_signal_log * log = &thread->signal_logs[sig - 1];
  51. struct shim_signal * signal = NULL;
  52. int head, tail, old_head;
  53. while (1) {
  54. old_head = head = atomic_read(&log->head);
  55. tail = atomic_read(&log->tail);
  56. if (head == tail)
  57. return NULL;
  58. if (!(signal = log->logs[head]))
  59. return NULL;
  60. log->logs[head] = NULL;
  61. head = (head == MAX_SIGNAL_LOG - 1) ? 0 : head + 1;
  62. if (atomic_cmpxchg(&log->head, old_head, head) == old_head)
  63. break;
  64. log->logs[old_head] = signal;
  65. }
  66. atomic_dec(&thread->has_signal);
  67. return signal;
  68. }
  69. static void
  70. __handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal);
  71. static void __store_info (siginfo_t * info, struct shim_signal * signal)
  72. {
  73. if (info)
  74. memcpy(&signal->info, info, sizeof(siginfo_t));
  75. }
  76. void __store_context (shim_tcb_t * tcb, PAL_CONTEXT * pal_context,
  77. struct shim_signal * signal)
  78. {
  79. ucontext_t * context = &signal->context;
  80. if (tcb && tcb->context.syscall_nr) {
  81. struct shim_context * ct = &tcb->context;
  82. context->uc_mcontext.gregs[REG_RSP] = (unsigned long) ct->sp;
  83. context->uc_mcontext.gregs[REG_RIP] = (unsigned long) ct->ret_ip;
  84. if (ct->regs) {
  85. struct shim_regs * regs = ct->regs;
  86. context->uc_mcontext.gregs[REG_R15] = regs->r15;
  87. context->uc_mcontext.gregs[REG_R14] = regs->r14;
  88. context->uc_mcontext.gregs[REG_R13] = regs->r13;
  89. context->uc_mcontext.gregs[REG_R9] = regs->r9;
  90. context->uc_mcontext.gregs[REG_R8] = regs->r8;
  91. context->uc_mcontext.gregs[REG_RCX] = regs->rcx;
  92. context->uc_mcontext.gregs[REG_RDX] = regs->rdx;
  93. context->uc_mcontext.gregs[REG_RSI] = regs->rsi;
  94. context->uc_mcontext.gregs[REG_RDI] = regs->rdi;
  95. context->uc_mcontext.gregs[REG_R12] = regs->r12;
  96. context->uc_mcontext.gregs[REG_RBX] = regs->rbx;
  97. context->uc_mcontext.gregs[REG_RBP] = regs->rbp;
  98. }
  99. signal->context_stored = true;
  100. return;
  101. }
  102. if (pal_context) {
  103. memcpy(context->uc_mcontext.gregs, pal_context, sizeof(PAL_CONTEXT));
  104. signal->context_stored = true;
  105. }
  106. }
  107. void deliver_signal (siginfo_t * info, PAL_CONTEXT * context)
  108. {
  109. shim_tcb_t * tcb = SHIM_GET_TLS();
  110. if (!tcb || !tcb->tp)
  111. return;
  112. struct shim_thread * cur_thread = (struct shim_thread *) tcb->tp;
  113. int sig = info->si_signo;
  114. __disable_preempt(tcb);
  115. struct shim_signal * signal = __alloca(sizeof(struct shim_signal));
  116. /* save in signal */
  117. memset(signal, 0, sizeof(struct shim_signal));
  118. __store_info(info, signal);
  119. __store_context(tcb, context, signal);
  120. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1)
  121. goto delay;
  122. if (__sigismember(&cur_thread->signal_mask, sig))
  123. goto delay;
  124. __handle_signal(tcb, sig, &signal->context);
  125. __handle_one_signal(tcb, sig, signal);
  126. goto out;
  127. delay:
  128. {
  129. if (!(signal = remalloc(signal,sizeof(struct shim_signal))))
  130. goto out;
  131. struct shim_signal ** signal_log = allocate_signal_log(cur_thread, sig);
  132. if (!signal_log) {
  133. sys_printf("signal queue is full (TID = %u, SIG = %d)\n",
  134. tcb->tid, sig);
  135. free(signal);
  136. goto out;
  137. }
  138. *signal_log = signal;
  139. }
  140. out:
  141. __enable_preempt(tcb);
  142. }
  143. #define ALLOC_SIGINFO(signo, member, value) \
  144. ({ \
  145. siginfo_t * _info = __alloca(sizeof(siginfo_t)); \
  146. memset(_info, 0, sizeof(siginfo_t)); \
  147. _info->si_signo = (signo); \
  148. _info->member = (value); \
  149. _info; \
  150. })
  151. #ifdef __x86_64__
  152. #define IP rip
  153. #else
  154. #define IP eip
  155. #endif
  156. #define is_internal(context) \
  157. ((context) && \
  158. (void *) (context)->IP >= (void *) &__code_address && \
  159. (void *) (context)->IP < (void *) &__code_address_end)
  160. #define internal_fault(errstr, addr, context) \
  161. do { \
  162. IDTYPE tid = get_cur_tid(); \
  163. if (is_internal((context))) \
  164. sys_printf(errstr " at %p (IP = +0x%lx, VMID = %u, TID = %u)\n",\
  165. arg, \
  166. (void *) context->IP - (void *) &__load_address, \
  167. cur_process.vmid, IS_INTERNAL_TID(tid) ? 0 : tid); \
  168. else \
  169. sys_printf(errstr " at %p (IP = %p, VMID = %u, TID = %u)\n", \
  170. arg, context ? context->IP : 0, \
  171. cur_process.vmid, IS_INTERNAL_TID(tid) ? 0 : tid); \
  172. } while (0)
  173. static void divzero_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  174. {
  175. if (IS_INTERNAL_TID(get_cur_tid()) || is_internal(context)) {
  176. internal_fault("Internal arithmetic fault", arg, context);
  177. pause();
  178. goto ret_exception;
  179. }
  180. if (context)
  181. debug("arithmetic fault at %p\n", context->IP);
  182. deliver_signal(ALLOC_SIGINFO(SIGFPE, si_addr, (void *) arg), context);
  183. ret_exception:
  184. DkExceptionReturn(event);
  185. }
  186. static void memfault_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  187. {
  188. if (IS_INTERNAL_TID(get_cur_tid()) || is_internal(context)) {
  189. internal:
  190. internal_fault("Internal memory fault", arg, context);
  191. pause();
  192. goto ret_exception;
  193. }
  194. if (context)
  195. debug("memory fault at %p (IP = %p)\n", arg, context->IP);
  196. struct shim_vma * vma = NULL;
  197. if (!lookup_supervma((void *) arg, 0, &vma)) {
  198. if (vma->flags & VMA_INTERNAL) {
  199. put_vma(vma);
  200. goto internal;
  201. }
  202. put_vma(vma);
  203. }
  204. int signo = SIGSEGV;
  205. deliver_signal(ALLOC_SIGINFO(signo, si_addr, (void *) arg), context);
  206. ret_exception:
  207. DkExceptionReturn(event);
  208. }
  209. static void illegal_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  210. {
  211. if (IS_INTERNAL_TID(get_cur_tid()) || is_internal(context)) {
  212. internal:
  213. internal_fault("Internal memory fault", arg, context);
  214. pause();
  215. goto ret_exception;
  216. }
  217. struct shim_vma * vma = NULL;
  218. if (!(lookup_supervma((void *) arg, 0, &vma)) &&
  219. !(vma->flags & VMA_INTERNAL)) {
  220. if (context)
  221. debug("illegal instruction at %p\n", context->IP);
  222. if (vma)
  223. put_vma(vma);
  224. deliver_signal(ALLOC_SIGINFO(SIGILL, si_addr, (void *) arg), context);
  225. } else {
  226. if (vma)
  227. put_vma(vma);
  228. goto internal;
  229. }
  230. ret_exception:
  231. DkExceptionReturn(event);
  232. }
  233. static void quit_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  234. {
  235. if (IS_INTERNAL_TID(get_cur_tid()))
  236. goto ret_exception;
  237. deliver_signal(ALLOC_SIGINFO(SIGTERM, si_pid, 0), NULL);
  238. ret_exception:
  239. DkExceptionReturn(event);
  240. }
  241. bool ask_for_checkpoint = false;
  242. static void suspend_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  243. {
  244. if (IS_INTERNAL_TID(get_cur_tid()))
  245. goto ret_exception;
  246. deliver_signal(ALLOC_SIGINFO(SIGINT, si_pid, 0), NULL);
  247. ret_exception:
  248. DkExceptionReturn(event);
  249. }
  250. static void resume_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  251. {
  252. if (IS_INTERNAL_TID(get_cur_tid()))
  253. goto ret_exception;
  254. shim_tcb_t * tcb = SHIM_GET_TLS();
  255. if (!tcb || !tcb->tp)
  256. return;
  257. __disable_preempt(tcb);
  258. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
  259. tcb->context.preempt |= SIGNAL_DELAYED;
  260. __enable_preempt(tcb);
  261. goto ret_exception;
  262. }
  263. __handle_signal(tcb, 0, NULL);
  264. __enable_preempt(tcb);
  265. ret_exception:
  266. DkExceptionReturn(event);
  267. }
  268. int init_signal (void)
  269. {
  270. DkSetExceptionHandler(&divzero_upcall, PAL_EVENT_DIVZERO, 0);
  271. DkSetExceptionHandler(&memfault_upcall, PAL_EVENT_MEMFAULT, 0);
  272. DkSetExceptionHandler(&illegal_upcall, PAL_EVENT_ILLEGAL, 0);
  273. DkSetExceptionHandler(&quit_upcall, PAL_EVENT_QUIT, 0);
  274. DkSetExceptionHandler(&suspend_upcall, PAL_EVENT_SUSPEND, 0);
  275. DkSetExceptionHandler(&resume_upcall, PAL_EVENT_RESUME, 0);
  276. return 0;
  277. }
  278. __sigset_t * get_sig_mask (struct shim_thread * thread)
  279. {
  280. if (!thread)
  281. thread = get_cur_thread();
  282. assert(thread);
  283. return &(thread->signal_mask);
  284. }
  285. __sigset_t * set_sig_mask (struct shim_thread * thread, __sigset_t * set)
  286. {
  287. if (!thread)
  288. thread = get_cur_thread();
  289. assert(thread);
  290. if (set)
  291. memcpy(&thread->signal_mask, set, sizeof(__sigset_t));
  292. return &thread->signal_mask;
  293. }
  294. static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *);
  295. static void
  296. __handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal)
  297. {
  298. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  299. struct shim_signal_handle * sighdl = &thread->signal_handles[sig - 1];
  300. void (*handler) (int, siginfo_t *, void *) = NULL;
  301. if (signal->info.si_signo == SIGCP) {
  302. join_checkpoint(thread, &signal->context, si_cp_session(&signal->info));
  303. return;
  304. }
  305. debug("%s handled\n", signal_name(sig));
  306. lock(thread->lock);
  307. if (sighdl->action) {
  308. struct __kernel_sigaction * act = sighdl->action;
  309. /* This is a workaround. The truth is that many program will
  310. use sa_handler as sa_sigaction, because sa_sigaction is
  311. not supported in amd64 */
  312. #ifdef __i386__
  313. handler = (void (*) (int, siginfo_t *, void *)) act->_u._sa_handler;
  314. if (act->sa_flags & SA_SIGINFO)
  315. sa_handler = act->_u._sa_sigaction;
  316. #else
  317. handler = (void (*) (int, siginfo_t *, void *)) act->k_sa_handler;
  318. #endif
  319. if (act->sa_flags & SA_RESETHAND) {
  320. sighdl->action = NULL;
  321. free(act);
  322. }
  323. }
  324. unlock(thread->lock);
  325. if ((void *) handler == (void *) 1) /* SIG_IGN */
  326. return;
  327. if (!handler && !(handler = default_sighandler[sig - 1]))
  328. return;
  329. /* if the context is never stored in the signal, it means the
  330. signal is handled during system calls, and before the thread
  331. is resumed. */
  332. if (!signal->context_stored)
  333. __store_context(tcb, NULL, signal);
  334. struct shim_context * context = NULL;
  335. if (tcb->context.syscall_nr) {
  336. context = __alloca(sizeof(struct shim_context));
  337. memcpy(context, &tcb->context, sizeof(struct shim_context));
  338. tcb->context.syscall_nr = 0;
  339. tcb->context.next = context;
  340. }
  341. debug("run signal handler %p (%d, %p, %p)\n", handler, sig, &signal->info,
  342. &signal->context);
  343. (*handler) (sig, &signal->info, &signal->context);
  344. if (context)
  345. memcpy(&tcb->context, context, sizeof(struct shim_context));
  346. }
  347. void __handle_signal (shim_tcb_t * tcb, int sig, ucontext_t * uc)
  348. {
  349. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  350. int begin_sig = 1, end_sig = NUM_KNOWN_SIGS;
  351. if (sig)
  352. end_sig = (begin_sig = sig) + 1;
  353. sig = begin_sig;
  354. while (atomic_read(&thread->has_signal)) {
  355. struct shim_signal * signal = NULL;
  356. for ( ; sig < end_sig ; sig++)
  357. if (!__sigismember(&thread->signal_mask, sig) &&
  358. (signal = fetch_signal_log(tcb, thread, sig)))
  359. break;
  360. if (!signal)
  361. break;
  362. if (!signal->context_stored)
  363. __store_context(tcb, NULL, signal);
  364. __handle_one_signal(tcb, sig, signal);
  365. free(signal);
  366. DkThreadYieldExecution();
  367. tcb->context.preempt &= ~SIGNAL_DELAYED;
  368. }
  369. }
  370. void handle_signal (bool delayed_only)
  371. {
  372. shim_tcb_t * tcb = SHIM_GET_TLS();
  373. if (!tcb || !tcb->tp)
  374. return;
  375. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  376. /* Fast path */
  377. if (!thread->has_signal.counter)
  378. return;
  379. __disable_preempt(tcb);
  380. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
  381. tcb->context.preempt |= SIGNAL_DELAYED;
  382. goto out;
  383. }
  384. if (delayed_only && !(tcb->context.preempt & SIGNAL_DELAYED))
  385. goto out;
  386. __handle_signal(tcb, 0, NULL);
  387. out:
  388. __enable_preempt(tcb);
  389. }
  390. void append_signal (struct shim_thread * thread, int sig, siginfo_t * info,
  391. bool wakeup)
  392. {
  393. struct shim_signal * signal = malloc(sizeof(struct shim_signal));
  394. if (!signal)
  395. return;
  396. /* save in signal */
  397. if (info) {
  398. __store_info(info, signal);
  399. signal->context_stored = false;
  400. } else {
  401. memset(signal, 0, sizeof(struct shim_signal));
  402. }
  403. struct shim_signal ** signal_log = allocate_signal_log(thread, sig);
  404. if (signal_log) {
  405. *signal_log = signal;
  406. if (wakeup) {
  407. debug("resuming thread %u\n", thread->tid);
  408. DkThreadResume(thread->pal_handle);
  409. }
  410. } else {
  411. sys_printf("signal queue is full (TID = %u, SIG = %d)\n",
  412. thread->tid, sig);
  413. free(signal);
  414. }
  415. }
  416. static void sighandler_kill (int sig, siginfo_t * info, void * ucontext)
  417. {
  418. debug("killed by %s\n", signal_name(sig));
  419. if (!info->si_pid)
  420. switch(sig) {
  421. case SIGTERM:
  422. case SIGINT:
  423. shim_do_kill(-1, sig);
  424. break;
  425. }
  426. try_process_exit(0);
  427. DkThreadExit();
  428. }
  429. static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *) =
  430. {
  431. /* SIGHUP */ &sighandler_kill,
  432. /* SIGINT */ &sighandler_kill,
  433. /* SIGQUIT */ &sighandler_kill,
  434. /* SIGILL */ &sighandler_kill,
  435. /* SIGTRAP */ NULL,
  436. /* SIGABRT */ &sighandler_kill,
  437. /* SIGBUS */ &sighandler_kill,
  438. /* SIGFPE */ &sighandler_kill,
  439. /* SIGKILL */ &sighandler_kill,
  440. /* SIGUSR1 */ NULL,
  441. /* SIGSEGV */ &sighandler_kill,
  442. /* SIGUSR2 */ NULL,
  443. /* SIGPIPE */ &sighandler_kill,
  444. /* SIGALRM */ NULL,
  445. /* SIGTERM */ &sighandler_kill,
  446. /* SIGSTKFLT */ NULL,
  447. /* SIGCHLD */ NULL,
  448. /* SIGCONT */ NULL,
  449. /* SIGSTOP */ NULL,
  450. /* SIGTSTP */ NULL,
  451. /* SIGTTIN */ NULL,
  452. /* SIGTTOU */ NULL,
  453. };