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