shim_signal.c 21 KB

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