shim_signal.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 (counter = %d)\n", sig - 1,
  45. head, tail, thread->has_signal.counter + 1);
  46. atomic_inc(&thread->has_signal);
  47. return &log->logs[old_tail];
  48. }
  49. static struct shim_signal *
  50. fetch_signal_log (shim_tcb_t * tcb, struct shim_thread * thread, int sig)
  51. {
  52. struct shim_signal_log * log = &thread->signal_logs[sig - 1];
  53. struct shim_signal * signal = NULL;
  54. int head, tail, old_head;
  55. while (1) {
  56. old_head = head = atomic_read(&log->head);
  57. tail = atomic_read(&log->tail);
  58. if (head == tail)
  59. return NULL;
  60. if (!(signal = log->logs[head]))
  61. return NULL;
  62. log->logs[head] = NULL;
  63. head = (head == MAX_SIGNAL_LOG - 1) ? 0 : head + 1;
  64. if (atomic_cmpxchg(&log->head, old_head, head) == old_head)
  65. break;
  66. log->logs[old_head] = signal;
  67. }
  68. debug("signal_logs[%d]: head=%d, tail=%d\n", sig -1, head, tail);
  69. atomic_dec(&thread->has_signal);
  70. return signal;
  71. }
  72. static void
  73. __handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal);
  74. static void __store_info (siginfo_t * info, struct shim_signal * signal)
  75. {
  76. if (info)
  77. memcpy(&signal->info, info, sizeof(siginfo_t));
  78. }
  79. void __store_context (shim_tcb_t * tcb, PAL_CONTEXT * pal_context,
  80. struct shim_signal * signal)
  81. {
  82. ucontext_t * context = &signal->context;
  83. if (tcb && tcb->context.syscall_nr) {
  84. struct shim_context * ct = &tcb->context;
  85. context->uc_mcontext.gregs[REG_RSP] = (unsigned long) ct->sp;
  86. context->uc_mcontext.gregs[REG_RIP] = (unsigned long) ct->ret_ip;
  87. if (ct->regs) {
  88. struct shim_regs * regs = ct->regs;
  89. context->uc_mcontext.gregs[REG_R15] = regs->r15;
  90. context->uc_mcontext.gregs[REG_R14] = regs->r14;
  91. context->uc_mcontext.gregs[REG_R13] = regs->r13;
  92. context->uc_mcontext.gregs[REG_R12] = regs->r12;
  93. context->uc_mcontext.gregs[REG_R11] = regs->r11;
  94. context->uc_mcontext.gregs[REG_R10] = regs->r10;
  95. context->uc_mcontext.gregs[REG_R9] = regs->r9;
  96. context->uc_mcontext.gregs[REG_R8] = regs->r8;
  97. context->uc_mcontext.gregs[REG_RCX] = regs->rcx;
  98. context->uc_mcontext.gregs[REG_RDX] = regs->rdx;
  99. context->uc_mcontext.gregs[REG_RSI] = regs->rsi;
  100. context->uc_mcontext.gregs[REG_RDI] = regs->rdi;
  101. context->uc_mcontext.gregs[REG_RBX] = regs->rbx;
  102. context->uc_mcontext.gregs[REG_RBP] = regs->rbp;
  103. }
  104. signal->context_stored = true;
  105. return;
  106. }
  107. if (pal_context) {
  108. memcpy(context->uc_mcontext.gregs, pal_context, sizeof(PAL_CONTEXT));
  109. signal->context_stored = true;
  110. }
  111. }
  112. void deliver_signal (siginfo_t * info, PAL_CONTEXT * context)
  113. {
  114. shim_tcb_t * tcb = SHIM_GET_TLS();
  115. if (!tcb || !tcb->tp)
  116. return;
  117. struct shim_thread * cur_thread = (struct shim_thread *) tcb->tp;
  118. int sig = info->si_signo;
  119. __disable_preempt(tcb);
  120. struct shim_signal * signal = __alloca(sizeof(struct shim_signal));
  121. /* save in signal */
  122. memset(signal, 0, sizeof(struct shim_signal));
  123. __store_info(info, signal);
  124. __store_context(tcb, context, signal);
  125. signal->pal_context = context;
  126. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1)
  127. goto delay;
  128. if (__sigismember(&cur_thread->signal_mask, sig))
  129. goto delay;
  130. __handle_signal(tcb, sig, &signal->context);
  131. __handle_one_signal(tcb, sig, signal);
  132. goto out;
  133. delay:
  134. {
  135. if (!(signal = malloc_copy(signal,sizeof(struct shim_signal))))
  136. goto out;
  137. struct shim_signal ** signal_log = allocate_signal_log(cur_thread, sig);
  138. if (!signal_log) {
  139. sys_printf("signal queue is full (TID = %u, SIG = %d)\n",
  140. tcb->tid, sig);
  141. free(signal);
  142. goto out;
  143. }
  144. *signal_log = signal;
  145. }
  146. out:
  147. __enable_preempt(tcb);
  148. }
  149. #define ALLOC_SIGINFO(signo, code, member, value) \
  150. ({ \
  151. siginfo_t * _info = __alloca(sizeof(siginfo_t)); \
  152. memset(_info, 0, sizeof(siginfo_t)); \
  153. _info->si_signo = (signo); \
  154. _info->si_code = (code); \
  155. _info->member = (value); \
  156. _info; \
  157. })
  158. #ifdef __x86_64__
  159. #define IP rip
  160. #else
  161. #define IP eip
  162. #endif
  163. #define is_internal(context) \
  164. ((context) && \
  165. (void *) (context)->IP >= (void *) &__code_address && \
  166. (void *) (context)->IP < (void *) &__code_address_end)
  167. #define internal_fault(errstr, addr, context) \
  168. do { \
  169. IDTYPE tid = get_cur_tid(); \
  170. if (is_internal((context))) \
  171. sys_printf(errstr " at %p (IP = +0x%lx, VMID = %u, TID = %u)\n",\
  172. arg, \
  173. (void *) context->IP - (void *) &__load_address, \
  174. cur_process.vmid, IS_INTERNAL_TID(tid) ? 0 : tid); \
  175. else \
  176. sys_printf(errstr " at %p (IP = %p, VMID = %u, TID = %u)\n", \
  177. arg, context ? context->IP : 0, \
  178. cur_process.vmid, IS_INTERNAL_TID(tid) ? 0 : tid); \
  179. } while (0)
  180. static void divzero_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  181. {
  182. if (IS_INTERNAL_TID(get_cur_tid()) || is_internal(context)) {
  183. internal_fault("Internal arithmetic fault", arg, context);
  184. pause();
  185. goto ret_exception;
  186. }
  187. if (context)
  188. debug("arithmetic fault at %p\n", context->IP);
  189. deliver_signal(ALLOC_SIGINFO(SIGFPE, FPE_INTDIV, si_addr, (void *) arg), context);
  190. ret_exception:
  191. DkExceptionReturn(event);
  192. }
  193. static void memfault_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  194. {
  195. shim_tcb_t * tcb = SHIM_GET_TLS();
  196. if (tcb->test_range.cont_addr && arg
  197. && (void *) arg >= tcb->test_range.start
  198. && (void *) arg <= tcb->test_range.end) {
  199. assert(context);
  200. context->rip = (PAL_NUM) tcb->test_range.cont_addr;
  201. goto ret_exception;
  202. }
  203. if (IS_INTERNAL_TID(get_cur_tid()) || is_internal(context)) {
  204. internal:
  205. internal_fault("Internal memory fault", arg, context);
  206. pause();
  207. goto ret_exception;
  208. }
  209. if (context)
  210. debug("memory fault at %p (IP = %p)\n", arg, context->IP);
  211. struct shim_vma_val vma;
  212. int signo = SIGSEGV;
  213. int code;
  214. if (!arg) {
  215. code = SEGV_MAPERR;
  216. } else if (!lookup_vma((void *) arg, &vma)) {
  217. if (vma.flags & VMA_INTERNAL) {
  218. goto internal;
  219. }
  220. if (vma.file && vma.file->type == TYPE_FILE) {
  221. /* DEP 3/3/17: If the mapping exceeds end of a file (but is in the VMA)
  222. * then return a SIGBUS. */
  223. uint64_t eof_in_vma = (uint64_t) vma.addr + vma.offset + vma.file->info.file.size;
  224. if (arg > eof_in_vma) {
  225. signo = SIGBUS;
  226. code = BUS_ADRERR;
  227. } else if ((context->err & 4) && !(vma.flags & PROT_WRITE)) {
  228. /* DEP 3/3/17: If the page fault gives a write error, and
  229. * the VMA is read-only, return SIGSEGV+SEGV_ACCERR */
  230. signo = SIGSEGV;
  231. code = SEGV_ACCERR;
  232. } else {
  233. /* XXX: need more sophisticated judgement */
  234. signo = SIGBUS;
  235. code = BUS_ADRERR;
  236. }
  237. } else {
  238. code = SEGV_ACCERR;
  239. }
  240. } else {
  241. code = SEGV_MAPERR;
  242. }
  243. deliver_signal(ALLOC_SIGINFO(signo, code, si_addr, (void *) arg), context);
  244. ret_exception:
  245. DkExceptionReturn(event);
  246. }
  247. /*
  248. * 'test_user_memory' and 'test_user_string' are helper functions for testing
  249. * if a user-given buffer or data structure is readable / writable (according
  250. * to the system call semantics). If the memory test fails, the system call
  251. * should return -EFAULT or -EINVAL accordingly. These helper functions cannot
  252. * guarantee further corruption of the buffer, or if the buffer is unmapped
  253. * with a concurrent system call. The purpose of these functions is simply for
  254. * the compatibility with programs that rely on the error numbers, such as the
  255. * LTP test suite.
  256. */
  257. bool test_user_memory (void * addr, size_t size, bool write)
  258. {
  259. if (!size)
  260. return false;
  261. shim_tcb_t * tcb = SHIM_GET_TLS();
  262. assert(tcb && tcb->tp);
  263. __disable_preempt(tcb);
  264. if (addr + size - 1 < addr)
  265. size = (void *) 0x0 - addr;
  266. bool has_fault = true;
  267. /* Add the memory region to the watch list. This is not racy because
  268. * each thread has its own record. */
  269. assert(!tcb->test_range.cont_addr);
  270. tcb->test_range.cont_addr = &&ret_fault;
  271. tcb->test_range.start = addr;
  272. tcb->test_range.end = addr + size - 1;
  273. /* Try to read or write into one byte inside each page */
  274. void * tmp = addr;
  275. while (tmp <= addr + size - 1) {
  276. if (write) {
  277. *(volatile char *) tmp = *(volatile char *) tmp;
  278. } else {
  279. *(volatile char *) tmp;
  280. }
  281. tmp = ALIGN_UP(tmp + 1);
  282. }
  283. has_fault = false; /* All accesses have passed. Nothing wrong. */
  284. ret_fault:
  285. /* If any read or write into the target region causes an exception,
  286. * the control flow will immediately jump to here. */
  287. tcb->test_range.cont_addr = NULL;
  288. tcb->test_range.start = tcb->test_range.end = NULL;
  289. __enable_preempt(tcb);
  290. return has_fault;
  291. }
  292. /*
  293. * This function tests a user string with unknown length. It only tests
  294. * whether the memory is readable.
  295. */
  296. bool test_user_string (const char * addr)
  297. {
  298. shim_tcb_t * tcb = SHIM_GET_TLS();
  299. assert(tcb && tcb->tp);
  300. __disable_preempt(tcb);
  301. bool has_fault = true;
  302. assert(!tcb->test_range.cont_addr);
  303. tcb->test_range.cont_addr = &&ret_fault;
  304. /* Test one page at a time. */
  305. const char * next = ALIGN_UP(addr + 1);
  306. do {
  307. /* Add the memory region to the watch list. This is not racy because
  308. * each thread has its own record. */
  309. tcb->test_range.start = (void *) addr;
  310. tcb->test_range.end = (void *) (next - 1);
  311. *(volatile char *) addr; /* try to read one byte from the page */
  312. /* If the string ends in this page, exit the loop. */
  313. if (strnlen(addr, next - addr) < next - addr)
  314. break;
  315. addr = next;
  316. next = ALIGN_UP(addr + 1);
  317. } while (addr < next);
  318. has_fault = false; /* All accesses have passed. Nothing wrong. */
  319. ret_fault:
  320. /* If any read or write into the target region causes an exception,
  321. * the control flow will immediately jump to here. */
  322. tcb->test_range.cont_addr = NULL;
  323. tcb->test_range.start = tcb->test_range.end = NULL;
  324. __enable_preempt(tcb);
  325. return has_fault;
  326. }
  327. static void illegal_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  328. {
  329. if (IS_INTERNAL_TID(get_cur_tid()) || is_internal(context)) {
  330. internal:
  331. internal_fault("Internal illegal fault", arg, context);
  332. pause();
  333. goto ret_exception;
  334. }
  335. struct shim_vma_val vma;
  336. if (!(lookup_vma((void *) arg, &vma)) &&
  337. !(vma.flags & VMA_INTERNAL)) {
  338. if (context)
  339. debug("illegal instruction at %p\n", context->IP);
  340. deliver_signal(ALLOC_SIGINFO(SIGILL, ILL_ILLOPC, si_addr, (void *) arg), context);
  341. } else {
  342. goto internal;
  343. }
  344. ret_exception:
  345. DkExceptionReturn(event);
  346. }
  347. static void quit_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  348. {
  349. if (IS_INTERNAL_TID(get_cur_tid()))
  350. goto ret_exception;
  351. deliver_signal(ALLOC_SIGINFO(SIGTERM, SI_USER, si_pid, 0), NULL);
  352. ret_exception:
  353. DkExceptionReturn(event);
  354. }
  355. static void suspend_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  356. {
  357. if (IS_INTERNAL_TID(get_cur_tid()))
  358. goto ret_exception;
  359. deliver_signal(ALLOC_SIGINFO(SIGINT, SI_USER, si_pid, 0), NULL);
  360. ret_exception:
  361. DkExceptionReturn(event);
  362. }
  363. static void resume_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  364. {
  365. if (IS_INTERNAL_TID(get_cur_tid()))
  366. goto ret_exception;
  367. shim_tcb_t * tcb = SHIM_GET_TLS();
  368. if (!tcb || !tcb->tp)
  369. return;
  370. __disable_preempt(tcb);
  371. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
  372. tcb->context.preempt |= SIGNAL_DELAYED;
  373. __enable_preempt(tcb);
  374. goto ret_exception;
  375. }
  376. __handle_signal(tcb, 0, NULL);
  377. __enable_preempt(tcb);
  378. ret_exception:
  379. DkExceptionReturn(event);
  380. }
  381. int init_signal (void)
  382. {
  383. DkSetExceptionHandler(&divzero_upcall, PAL_EVENT_DIVZERO, 0);
  384. DkSetExceptionHandler(&memfault_upcall, PAL_EVENT_MEMFAULT, 0);
  385. DkSetExceptionHandler(&illegal_upcall, PAL_EVENT_ILLEGAL, 0);
  386. DkSetExceptionHandler(&quit_upcall, PAL_EVENT_QUIT, 0);
  387. DkSetExceptionHandler(&suspend_upcall, PAL_EVENT_SUSPEND, 0);
  388. DkSetExceptionHandler(&resume_upcall, PAL_EVENT_RESUME, 0);
  389. return 0;
  390. }
  391. __sigset_t * get_sig_mask (struct shim_thread * thread)
  392. {
  393. if (!thread)
  394. thread = get_cur_thread();
  395. assert(thread);
  396. return &(thread->signal_mask);
  397. }
  398. __sigset_t * set_sig_mask (struct shim_thread * thread,
  399. const __sigset_t * set)
  400. {
  401. if (!thread)
  402. thread = get_cur_thread();
  403. assert(thread);
  404. if (set)
  405. memcpy(&thread->signal_mask, set, sizeof(__sigset_t));
  406. return &thread->signal_mask;
  407. }
  408. static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *);
  409. static void
  410. __handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal)
  411. {
  412. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  413. struct shim_signal_handle * sighdl = &thread->signal_handles[sig - 1];
  414. void (*handler) (int, siginfo_t *, void *) = NULL;
  415. if (signal->info.si_signo == SIGCP) {
  416. join_checkpoint(thread, &signal->context, si_cp_session(&signal->info));
  417. return;
  418. }
  419. debug("%s handled\n", signal_name(sig));
  420. lock(thread->lock);
  421. if (sighdl->action) {
  422. struct __kernel_sigaction * act = sighdl->action;
  423. /* This is a workaround. The truth is that many program will
  424. use sa_handler as sa_sigaction, because sa_sigaction is
  425. not supported in amd64 */
  426. #ifdef __i386__
  427. handler = (void (*) (int, siginfo_t *, void *)) act->_u._sa_handler;
  428. if (act->sa_flags & SA_SIGINFO)
  429. sa_handler = act->_u._sa_sigaction;
  430. #else
  431. handler = (void (*) (int, siginfo_t *, void *)) act->k_sa_handler;
  432. #endif
  433. if (act->sa_flags & SA_RESETHAND) {
  434. sighdl->action = NULL;
  435. free(act);
  436. }
  437. }
  438. unlock(thread->lock);
  439. if ((void *) handler == (void *) 1) /* SIG_IGN */
  440. return;
  441. if (!handler && !(handler = default_sighandler[sig - 1]))
  442. return;
  443. /* if the context is never stored in the signal, it means the
  444. signal is handled during system calls, and before the thread
  445. is resumed. */
  446. if (!signal->context_stored)
  447. __store_context(tcb, NULL, signal);
  448. struct shim_context * context = NULL;
  449. if (tcb->context.syscall_nr) {
  450. context = __alloca(sizeof(struct shim_context));
  451. memcpy(context, &tcb->context, sizeof(struct shim_context));
  452. tcb->context.syscall_nr = 0;
  453. tcb->context.next = context;
  454. }
  455. debug("run signal handler %p (%d, %p, %p)\n", handler, sig, &signal->info,
  456. &signal->context);
  457. (*handler) (sig, &signal->info, &signal->context);
  458. if (context)
  459. memcpy(&tcb->context, context, sizeof(struct shim_context));
  460. if (signal->pal_context)
  461. memcpy(signal->pal_context, signal->context.uc_mcontext.gregs,
  462. sizeof(PAL_CONTEXT));
  463. }
  464. void __handle_signal (shim_tcb_t * tcb, int sig, ucontext_t * uc)
  465. {
  466. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  467. int begin_sig = 1, end_sig = NUM_KNOWN_SIGS;
  468. if (sig)
  469. end_sig = (begin_sig = sig) + 1;
  470. sig = begin_sig;
  471. while (atomic_read(&thread->has_signal)) {
  472. struct shim_signal * signal = NULL;
  473. for ( ; sig < end_sig ; sig++)
  474. if (!__sigismember(&thread->signal_mask, sig) &&
  475. (signal = fetch_signal_log(tcb, thread, sig)))
  476. break;
  477. if (!signal)
  478. break;
  479. if (!signal->context_stored)
  480. __store_context(tcb, NULL, signal);
  481. __handle_one_signal(tcb, sig, signal);
  482. free(signal);
  483. DkThreadYieldExecution();
  484. tcb->context.preempt &= ~SIGNAL_DELAYED;
  485. }
  486. }
  487. void handle_signal (bool delayed_only)
  488. {
  489. shim_tcb_t * tcb = SHIM_GET_TLS();
  490. if (!tcb || !tcb->tp)
  491. return;
  492. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  493. debug("handle signal (counter = %d)\n", thread->has_signal.counter);
  494. /* Fast path */
  495. if (!thread->has_signal.counter)
  496. return;
  497. __disable_preempt(tcb);
  498. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
  499. debug("signal delayed (%d)\n", tcb->context.preempt & ~SIGNAL_DELAYED);
  500. tcb->context.preempt |= SIGNAL_DELAYED;
  501. goto out;
  502. }
  503. if (delayed_only && !(tcb->context.preempt & SIGNAL_DELAYED))
  504. goto out;
  505. __handle_signal(tcb, 0, NULL);
  506. out:
  507. __enable_preempt(tcb);
  508. debug("__enable_preempt: %s:%d\n", __FILE__, __LINE__);
  509. }
  510. void append_signal (struct shim_thread * thread, int sig, siginfo_t * info,
  511. bool wakeup)
  512. {
  513. struct shim_signal * signal = malloc(sizeof(struct shim_signal));
  514. if (!signal)
  515. return;
  516. /* save in signal */
  517. if (info) {
  518. __store_info(info, signal);
  519. signal->context_stored = false;
  520. } else {
  521. memset(signal, 0, sizeof(struct shim_signal));
  522. }
  523. struct shim_signal ** signal_log = allocate_signal_log(thread, sig);
  524. if (signal_log) {
  525. *signal_log = signal;
  526. if (wakeup) {
  527. debug("resuming thread %u\n", thread->tid);
  528. DkThreadResume(thread->pal_handle);
  529. }
  530. } else {
  531. sys_printf("signal queue is full (TID = %u, SIG = %d)\n",
  532. thread->tid, sig);
  533. free(signal);
  534. }
  535. }
  536. static void sighandler_kill (int sig, siginfo_t * info, void * ucontext)
  537. {
  538. debug("killed by %s\n", signal_name(sig));
  539. if (!info->si_pid)
  540. switch(sig) {
  541. case SIGTERM:
  542. case SIGINT:
  543. shim_do_kill(-1, sig);
  544. break;
  545. }
  546. try_process_exit(0, sig);
  547. DkThreadExit();
  548. }
  549. /* We don't currently implement core dumps, but put a wrapper
  550. * in case we do in the future */
  551. static void sighandler_core (int sig, siginfo_t * info, void * ucontext)
  552. {
  553. sighandler_kill(sig, info, ucontext);
  554. }
  555. static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *) =
  556. {
  557. /* SIGHUP */ &sighandler_kill,
  558. /* SIGINT */ &sighandler_kill,
  559. /* SIGQUIT */ &sighandler_kill,
  560. /* SIGILL */ &sighandler_kill,
  561. /* SIGTRAP */ &sighandler_core,
  562. /* SIGABRT */ &sighandler_kill,
  563. /* SIGBUS */ &sighandler_kill,
  564. /* SIGFPE */ &sighandler_kill,
  565. /* SIGKILL */ &sighandler_kill,
  566. /* SIGUSR1 */ NULL,
  567. /* SIGSEGV */ &sighandler_kill,
  568. /* SIGUSR2 */ NULL,
  569. /* SIGPIPE */ &sighandler_kill,
  570. /* SIGALRM */ &sighandler_kill,
  571. /* SIGTERM */ &sighandler_kill,
  572. /* SIGSTKFLT */ NULL,
  573. /* SIGCHLD */ NULL,
  574. /* SIGCONT */ NULL,
  575. /* SIGSTOP */ NULL,
  576. /* SIGTSTP */ NULL,
  577. /* SIGTTIN */ NULL,
  578. /* SIGTTOU */ NULL,
  579. };