shim_signal.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. static void suspend_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  242. {
  243. if (IS_INTERNAL_TID(get_cur_tid()))
  244. goto ret_exception;
  245. deliver_signal(ALLOC_SIGINFO(SIGINT, si_pid, 0), NULL);
  246. ret_exception:
  247. DkExceptionReturn(event);
  248. }
  249. static void resume_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  250. {
  251. if (IS_INTERNAL_TID(get_cur_tid()))
  252. goto ret_exception;
  253. shim_tcb_t * tcb = SHIM_GET_TLS();
  254. if (!tcb || !tcb->tp)
  255. return;
  256. __disable_preempt(tcb);
  257. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
  258. tcb->context.preempt |= SIGNAL_DELAYED;
  259. __enable_preempt(tcb);
  260. goto ret_exception;
  261. }
  262. __handle_signal(tcb, 0, NULL);
  263. __enable_preempt(tcb);
  264. ret_exception:
  265. DkExceptionReturn(event);
  266. }
  267. int init_signal (void)
  268. {
  269. DkSetExceptionHandler(&divzero_upcall, PAL_EVENT_DIVZERO, 0);
  270. DkSetExceptionHandler(&memfault_upcall, PAL_EVENT_MEMFAULT, 0);
  271. DkSetExceptionHandler(&illegal_upcall, PAL_EVENT_ILLEGAL, 0);
  272. DkSetExceptionHandler(&quit_upcall, PAL_EVENT_QUIT, 0);
  273. DkSetExceptionHandler(&suspend_upcall, PAL_EVENT_SUSPEND, 0);
  274. DkSetExceptionHandler(&resume_upcall, PAL_EVENT_RESUME, 0);
  275. return 0;
  276. }
  277. __sigset_t * get_sig_mask (struct shim_thread * thread)
  278. {
  279. if (!thread)
  280. thread = get_cur_thread();
  281. assert(thread);
  282. return &(thread->signal_mask);
  283. }
  284. __sigset_t * set_sig_mask (struct shim_thread * thread, __sigset_t * set)
  285. {
  286. if (!thread)
  287. thread = get_cur_thread();
  288. assert(thread);
  289. if (set)
  290. memcpy(&thread->signal_mask, set, sizeof(__sigset_t));
  291. return &thread->signal_mask;
  292. }
  293. static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *);
  294. static void
  295. __handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal)
  296. {
  297. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  298. struct shim_signal_handle * sighdl = &thread->signal_handles[sig - 1];
  299. void (*handler) (int, siginfo_t *, void *) = NULL;
  300. if (signal->info.si_signo == SIGCP) {
  301. join_checkpoint(thread, &signal->context, si_cp_session(&signal->info));
  302. return;
  303. }
  304. debug("%s handled\n", signal_name(sig));
  305. lock(thread->lock);
  306. if (sighdl->action) {
  307. struct __kernel_sigaction * act = sighdl->action;
  308. /* This is a workaround. The truth is that many program will
  309. use sa_handler as sa_sigaction, because sa_sigaction is
  310. not supported in amd64 */
  311. #ifdef __i386__
  312. handler = (void (*) (int, siginfo_t *, void *)) act->_u._sa_handler;
  313. if (act->sa_flags & SA_SIGINFO)
  314. sa_handler = act->_u._sa_sigaction;
  315. #else
  316. handler = (void (*) (int, siginfo_t *, void *)) act->k_sa_handler;
  317. #endif
  318. if (act->sa_flags & SA_RESETHAND) {
  319. sighdl->action = NULL;
  320. free(act);
  321. }
  322. }
  323. unlock(thread->lock);
  324. if ((void *) handler == (void *) 1) /* SIG_IGN */
  325. return;
  326. if (!handler && !(handler = default_sighandler[sig - 1]))
  327. return;
  328. /* if the context is never stored in the signal, it means the
  329. signal is handled during system calls, and before the thread
  330. is resumed. */
  331. if (!signal->context_stored)
  332. __store_context(tcb, NULL, signal);
  333. struct shim_context * context = NULL;
  334. if (tcb->context.syscall_nr) {
  335. context = __alloca(sizeof(struct shim_context));
  336. memcpy(context, &tcb->context, sizeof(struct shim_context));
  337. tcb->context.syscall_nr = 0;
  338. tcb->context.next = context;
  339. }
  340. debug("run signal handler %p (%d, %p, %p)\n", handler, sig, &signal->info,
  341. &signal->context);
  342. (*handler) (sig, &signal->info, &signal->context);
  343. if (context)
  344. memcpy(&tcb->context, context, sizeof(struct shim_context));
  345. }
  346. void __handle_signal (shim_tcb_t * tcb, int sig, ucontext_t * uc)
  347. {
  348. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  349. int begin_sig = 1, end_sig = NUM_KNOWN_SIGS;
  350. if (sig)
  351. end_sig = (begin_sig = sig) + 1;
  352. sig = begin_sig;
  353. while (atomic_read(&thread->has_signal)) {
  354. struct shim_signal * signal = NULL;
  355. for ( ; sig < end_sig ; sig++)
  356. if (!__sigismember(&thread->signal_mask, sig) &&
  357. (signal = fetch_signal_log(tcb, thread, sig)))
  358. break;
  359. if (!signal)
  360. break;
  361. if (!signal->context_stored)
  362. __store_context(tcb, NULL, signal);
  363. __handle_one_signal(tcb, sig, signal);
  364. free(signal);
  365. DkThreadYieldExecution();
  366. tcb->context.preempt &= ~SIGNAL_DELAYED;
  367. }
  368. }
  369. void handle_signal (bool delayed_only)
  370. {
  371. shim_tcb_t * tcb = SHIM_GET_TLS();
  372. if (!tcb || !tcb->tp)
  373. return;
  374. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  375. /* Fast path */
  376. if (!thread->has_signal.counter)
  377. return;
  378. __disable_preempt(tcb);
  379. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
  380. tcb->context.preempt |= SIGNAL_DELAYED;
  381. goto out;
  382. }
  383. if (delayed_only && !(tcb->context.preempt & SIGNAL_DELAYED))
  384. goto out;
  385. __handle_signal(tcb, 0, NULL);
  386. out:
  387. __enable_preempt(tcb);
  388. }
  389. void append_signal (struct shim_thread * thread, int sig, siginfo_t * info,
  390. bool wakeup)
  391. {
  392. struct shim_signal * signal = malloc(sizeof(struct shim_signal));
  393. if (!signal)
  394. return;
  395. /* save in signal */
  396. if (info) {
  397. __store_info(info, signal);
  398. signal->context_stored = false;
  399. } else {
  400. memset(signal, 0, sizeof(struct shim_signal));
  401. }
  402. struct shim_signal ** signal_log = allocate_signal_log(thread, sig);
  403. if (signal_log) {
  404. *signal_log = signal;
  405. if (wakeup) {
  406. debug("resuming thread %u\n", thread->tid);
  407. DkThreadResume(thread->pal_handle);
  408. }
  409. } else {
  410. sys_printf("signal queue is full (TID = %u, SIG = %d)\n",
  411. thread->tid, sig);
  412. free(signal);
  413. }
  414. }
  415. static void sighandler_kill (int sig, siginfo_t * info, void * ucontext)
  416. {
  417. debug("killed by %s\n", signal_name(sig));
  418. if (!info->si_pid)
  419. switch(sig) {
  420. case SIGTERM:
  421. case SIGINT:
  422. shim_do_kill(-1, sig);
  423. break;
  424. }
  425. try_process_exit(0);
  426. DkThreadExit();
  427. }
  428. static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *) =
  429. {
  430. /* SIGHUP */ &sighandler_kill,
  431. /* SIGINT */ &sighandler_kill,
  432. /* SIGQUIT */ &sighandler_kill,
  433. /* SIGILL */ &sighandler_kill,
  434. /* SIGTRAP */ NULL,
  435. /* SIGABRT */ &sighandler_kill,
  436. /* SIGBUS */ &sighandler_kill,
  437. /* SIGFPE */ &sighandler_kill,
  438. /* SIGKILL */ &sighandler_kill,
  439. /* SIGUSR1 */ NULL,
  440. /* SIGSEGV */ &sighandler_kill,
  441. /* SIGUSR2 */ NULL,
  442. /* SIGPIPE */ &sighandler_kill,
  443. /* SIGALRM */ NULL,
  444. /* SIGTERM */ &sighandler_kill,
  445. /* SIGSTKFLT */ NULL,
  446. /* SIGCHLD */ NULL,
  447. /* SIGCONT */ NULL,
  448. /* SIGSTOP */ NULL,
  449. /* SIGTSTP */ NULL,
  450. /* SIGTTIN */ NULL,
  451. /* SIGTTOU */ NULL,
  452. };