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