shim_signal.c 18 KB

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