shim_signal.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_signal.c
  15. *
  16. * This file contains codes to handle signals and exceptions passed from PAL.
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_utils.h>
  20. #include <shim_table.h>
  21. #include <shim_thread.h>
  22. #include <shim_handle.h>
  23. #include <shim_vma.h>
  24. #include <shim_checkpoint.h>
  25. #include <shim_signal.h>
  26. #include <shim_unistd.h>
  27. #include <pal.h>
  28. static struct shim_signal **
  29. allocate_signal_log (struct shim_thread * thread, int sig)
  30. {
  31. if (!thread->signal_logs)
  32. return NULL;
  33. struct shim_signal_log * log = &thread->signal_logs[sig - 1];
  34. int head, tail, old_tail;
  35. do {
  36. head = atomic_read(&log->head);
  37. old_tail = tail = atomic_read(&log->tail);
  38. if (head == tail + 1 || (!head && tail == (MAX_SIGNAL_LOG - 1)))
  39. return NULL;
  40. tail = (tail == MAX_SIGNAL_LOG - 1) ? 0 : tail + 1;
  41. } while (atomic_cmpxchg(&log->tail, old_tail, tail) == tail);
  42. debug("signal_logs[%d]: head=%d, tail=%d (counter = %ld)\n", sig - 1,
  43. head, tail, thread->has_signal.counter + 1);
  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. debug("signal_logs[%d]: head=%d, tail=%d\n", sig -1, head, tail);
  67. atomic_dec(&thread->has_signal);
  68. return signal;
  69. }
  70. static void
  71. __handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal);
  72. static void __store_info (siginfo_t * info, struct shim_signal * signal)
  73. {
  74. if (info)
  75. memcpy(&signal->info, info, sizeof(siginfo_t));
  76. }
  77. void __store_context (shim_tcb_t * tcb, PAL_CONTEXT * pal_context,
  78. struct shim_signal * signal)
  79. {
  80. ucontext_t * context = &signal->context;
  81. if (tcb && tcb->context.syscall_nr) {
  82. struct shim_context * ct = &tcb->context;
  83. context->uc_mcontext.gregs[REG_RSP] = (unsigned long) ct->sp;
  84. context->uc_mcontext.gregs[REG_RIP] = (unsigned long) ct->ret_ip;
  85. if (ct->regs) {
  86. struct shim_regs * regs = ct->regs;
  87. context->uc_mcontext.gregs[REG_EFL] = regs->rflags;
  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. assert(tcb);
  115. // Signals should not be delivered before the user process starts
  116. // or after the user process dies.
  117. if (!tcb->tp || !cur_thread_is_alive())
  118. return;
  119. struct shim_thread * cur_thread = (struct shim_thread *) tcb->tp;
  120. int sig = info->si_signo;
  121. __disable_preempt(tcb);
  122. struct shim_signal * signal = __alloca(sizeof(struct shim_signal));
  123. /* save in signal */
  124. memset(signal, 0, sizeof(struct shim_signal));
  125. __store_info(info, signal);
  126. __store_context(tcb, context, signal);
  127. signal->pal_context = context;
  128. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1 ||
  129. __sigismember(&cur_thread->signal_mask, sig)) {
  130. struct shim_signal ** signal_log = NULL;
  131. if ((signal = malloc_copy(signal,sizeof(struct shim_signal))) &&
  132. (signal_log = allocate_signal_log(cur_thread, sig))) {
  133. *signal_log = signal;
  134. }
  135. if (signal && !signal_log) {
  136. SYS_PRINTF("signal queue is full (TID = %u, SIG = %d)\n",
  137. tcb->tid, sig);
  138. free(signal);
  139. }
  140. } else {
  141. __handle_signal(tcb, sig, &signal->context);
  142. __handle_one_signal(tcb, sig, signal);
  143. }
  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. static inline bool context_is_internal(PAL_CONTEXT * context)
  161. {
  162. return context &&
  163. (void *) context->IP >= (void *) &__code_address &&
  164. (void *) context->IP < (void *) &__code_address_end;
  165. }
  166. static inline void internal_fault(const char* errstr,
  167. PAL_NUM addr, PAL_CONTEXT * context)
  168. {
  169. IDTYPE tid = get_cur_tid();
  170. if (context_is_internal(context))
  171. SYS_PRINTF("%s at 0x%08lx (IP = +0x%lx, VMID = %u, TID = %u)\n", errstr,
  172. addr, (void *) context->IP - (void *) &__load_address,
  173. cur_process.vmid, is_internal_tid(tid) ? 0 : tid);
  174. else
  175. SYS_PRINTF("%s at 0x%08lx (IP = 0x%08lx, VMID = %u, TID = %u)\n", errstr,
  176. addr, context ? context->IP : 0,
  177. cur_process.vmid, is_internal_tid(tid) ? 0 : tid);
  178. PAUSE();
  179. }
  180. static void arithmetic_error_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  181. {
  182. if (is_internal_tid(get_cur_tid()) || context_is_internal(context)) {
  183. internal_fault("Internal arithmetic fault", arg, context);
  184. } else {
  185. if (context)
  186. debug("arithmetic fault at 0x%08lx\n", context->IP);
  187. deliver_signal(ALLOC_SIGINFO(SIGFPE, FPE_INTDIV,
  188. si_addr, (void *) arg), context);
  189. }
  190. DkExceptionReturn(event);
  191. }
  192. static void memfault_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  193. {
  194. shim_tcb_t * tcb = shim_get_tls();
  195. assert(tcb);
  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()) || context_is_internal(context)) {
  204. internal_fault("Internal memory fault", arg, context);
  205. goto ret_exception;
  206. }
  207. if (context)
  208. debug("memory fault at 0x%08lx (IP = 0x%08lx)\n", arg, context->IP);
  209. struct shim_vma_val vma;
  210. int signo = SIGSEGV;
  211. int code;
  212. if (!arg) {
  213. code = SEGV_MAPERR;
  214. } else if (!lookup_vma((void *) arg, &vma)) {
  215. if (vma.flags & VMA_INTERNAL) {
  216. internal_fault("Internal memory fault with VMA", arg, context);
  217. goto ret_exception;
  218. }
  219. if (vma.file && vma.file->type == TYPE_FILE) {
  220. /* DEP 3/3/17: If the mapping exceeds end of a file (but is in the VMA)
  221. * then return a SIGBUS. */
  222. uint64_t eof_in_vma = (uint64_t) vma.addr + vma.offset + vma.file->info.file.size;
  223. if (arg > eof_in_vma) {
  224. signo = SIGBUS;
  225. code = BUS_ADRERR;
  226. } else if ((context->err & 4) && !(vma.flags & PROT_WRITE)) {
  227. /* DEP 3/3/17: If the page fault gives a write error, and
  228. * the VMA is read-only, return SIGSEGV+SEGV_ACCERR */
  229. signo = SIGSEGV;
  230. code = SEGV_ACCERR;
  231. } else {
  232. /* XXX: need more sophisticated judgement */
  233. signo = SIGBUS;
  234. code = BUS_ADRERR;
  235. }
  236. } else {
  237. code = SEGV_ACCERR;
  238. }
  239. } else {
  240. code = SEGV_MAPERR;
  241. }
  242. deliver_signal(ALLOC_SIGINFO(signo, code, si_addr, (void *) arg), context);
  243. ret_exception:
  244. DkExceptionReturn(event);
  245. }
  246. /*
  247. * Helper function for test_user_memory / test_user_string; they behave
  248. * differently for different PALs:
  249. *
  250. * - For Linux-SGX, the faulting address is not propagated in memfault
  251. * exception (SGX v1 does not write address in SSA frame, SGX v2 writes
  252. * it only at a granularity of 4K pages). Thus, we cannot rely on
  253. * exception handling to compare against tcb.test_range.start/end.
  254. * Instead, traverse VMAs to see if [addr, addr+size) is addressable;
  255. * before traversing VMAs, grab a VMA lock.
  256. *
  257. * - For other PALs, we touch one byte of each page in [addr, addr+size).
  258. * If some byte is not addressable, exception is raised. memfault_upcall
  259. * handles this exception and resumes execution from ret_fault.
  260. *
  261. * The second option is faster in fault-free case but cannot be used under
  262. * SGX PAL. We use the best option for each PAL for now. */
  263. static bool is_sgx_pal(void) {
  264. static struct atomic_int sgx_pal = { .counter = 0 };
  265. static struct atomic_int inited = { .counter = 0 };
  266. if (!atomic_read(&inited)) {
  267. /* Ensure that is_sgx_pal is updated before initialized */
  268. atomic_set(&sgx_pal, strcmp_static(PAL_CB(host_type), "Linux-SGX"));
  269. MB();
  270. atomic_set(&inited, 1);
  271. }
  272. MB();
  273. return atomic_read(&sgx_pal) != 0;
  274. }
  275. /*
  276. * 'test_user_memory' and 'test_user_string' are helper functions for testing
  277. * if a user-given buffer or data structure is readable / writable (according
  278. * to the system call semantics). If the memory test fails, the system call
  279. * should return -EFAULT or -EINVAL accordingly. These helper functions cannot
  280. * guarantee further corruption of the buffer, or if the buffer is unmapped
  281. * with a concurrent system call. The purpose of these functions is simply for
  282. * the compatibility with programs that rely on the error numbers, such as the
  283. * LTP test suite. */
  284. bool test_user_memory (void * addr, size_t size, bool write)
  285. {
  286. if (!size)
  287. return false;
  288. if (!access_ok(addr, size))
  289. return true;
  290. /* SGX path: check if [addr, addr+size) is addressable (in some VMA) */
  291. if (is_sgx_pal())
  292. return !is_in_one_vma(addr, size);
  293. /* Non-SGX path: check if [addr, addr+size) is addressable by touching
  294. * a byte of each page; invalid access will be caught in memfault_upcall */
  295. shim_tcb_t * tcb = shim_get_tls();
  296. assert(tcb && tcb->tp);
  297. __disable_preempt(tcb);
  298. bool has_fault = true;
  299. /* Add the memory region to the watch list. This is not racy because
  300. * each thread has its own record. */
  301. assert(!tcb->test_range.cont_addr);
  302. tcb->test_range.cont_addr = &&ret_fault;
  303. tcb->test_range.start = addr;
  304. tcb->test_range.end = addr + size - 1;
  305. /* Try to read or write into one byte inside each page */
  306. void * tmp = addr;
  307. while (tmp <= addr + size - 1) {
  308. if (write) {
  309. *(volatile char *) tmp = *(volatile char *) tmp;
  310. } else {
  311. *(volatile char *) tmp;
  312. }
  313. tmp = ALIGN_UP(tmp + 1);
  314. }
  315. has_fault = false; /* All accesses have passed. Nothing wrong. */
  316. ret_fault:
  317. /* If any read or write into the target region causes an exception,
  318. * the control flow will immediately jump to here. */
  319. tcb->test_range.cont_addr = NULL;
  320. tcb->test_range.start = tcb->test_range.end = NULL;
  321. __enable_preempt(tcb);
  322. return has_fault;
  323. }
  324. /*
  325. * This function tests a user string with unknown length. It only tests
  326. * whether the memory is readable.
  327. */
  328. bool test_user_string (const char * addr)
  329. {
  330. if (!access_ok(addr, 1))
  331. return true;
  332. size_t size, maxlen;
  333. const char * next = ALIGN_UP(addr + 1);
  334. /* SGX path: check if [addr, addr+size) is addressable (in some VMA). */
  335. if (is_sgx_pal()) {
  336. /* We don't know length but using unprotected strlen() is dangerous
  337. * so we check string in chunks of 4K pages. */
  338. do {
  339. maxlen = next - addr;
  340. if (!access_ok(addr, maxlen) || !is_in_one_vma((void*) addr, maxlen))
  341. return true;
  342. size = strnlen(addr, maxlen);
  343. addr = next;
  344. next = ALIGN_UP(addr + 1);
  345. } while (size == maxlen);
  346. return false;
  347. }
  348. /* Non-SGX path: check if [addr, addr+size) is addressable by touching
  349. * a byte of each page; invalid access will be caught in memfault_upcall. */
  350. shim_tcb_t * tcb = shim_get_tls();
  351. assert(tcb && tcb->tp);
  352. __disable_preempt(tcb);
  353. bool has_fault = true;
  354. assert(!tcb->test_range.cont_addr);
  355. tcb->test_range.cont_addr = &&ret_fault;
  356. do {
  357. /* Add the memory region to the watch list. This is not racy because
  358. * each thread has its own record. */
  359. tcb->test_range.start = (void *) addr;
  360. tcb->test_range.end = (void *) (next - 1);
  361. maxlen = next - addr;
  362. if (!access_ok(addr, maxlen))
  363. return true;
  364. *(volatile char *) addr; /* try to read one byte from the page */
  365. size = strnlen(addr, maxlen);
  366. addr = next;
  367. next = ALIGN_UP(addr + 1);
  368. } while (size == maxlen);
  369. has_fault = false; /* All accesses have passed. Nothing wrong. */
  370. ret_fault:
  371. /* If any read or write into the target region causes an exception,
  372. * the control flow will immediately jump to here. */
  373. tcb->test_range.cont_addr = NULL;
  374. tcb->test_range.start = tcb->test_range.end = NULL;
  375. __enable_preempt(tcb);
  376. return has_fault;
  377. }
  378. void __attribute__((weak)) syscall_wrapper(void)
  379. {
  380. /*
  381. * work around for link.
  382. * syscalldb.S is excluded for libsysdb_debug.so so it fails to link
  383. * due to missing syscall_wrapper.
  384. */
  385. }
  386. static void illegal_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  387. {
  388. struct shim_vma_val vma;
  389. if (!is_internal_tid(get_cur_tid()) &&
  390. !context_is_internal(context) &&
  391. !(lookup_vma((void *) arg, &vma)) &&
  392. !(vma.flags & VMA_INTERNAL)) {
  393. if (context)
  394. debug("illegal instruction at 0x%08lx\n", context->IP);
  395. uint8_t * rip = (uint8_t*)context->IP;
  396. /*
  397. * Emulate syscall instruction (opcode 0x0f 0x05);
  398. * syscall instruction is prohibited in
  399. * Linux-SGX PAL and raises a SIGILL exception and
  400. * Linux PAL with seccomp and raise SIGSYS exception.
  401. */
  402. #if 0
  403. if (rip[-2] == 0x0f && rip[-1] == 0x05) {
  404. /* TODO: once finished, remove "#if 0" above. */
  405. /*
  406. * SIGSYS case (can happen with Linux PAL with seccomp)
  407. * rip points to the address after syscall instruction
  408. * %rcx: syscall instruction must put an
  409. * instruction-after-syscall in rcx
  410. */
  411. context->rax = siginfo->si_syscall; /* PAL_CONTEXT doesn't
  412. * include a member
  413. * corresponding to
  414. * siginfo_t::si_syscall yet.
  415. */
  416. context->rcx = (long)rip;
  417. context->r11 = context->efl;
  418. context->rip = (long)&syscall_wrapper;
  419. } else
  420. #endif
  421. if (rip[0] == 0x0f && rip[1] == 0x05) {
  422. /*
  423. * SIGILL case (can happen in Linux-SGX PAL)
  424. * %rcx: syscall instruction must put an instruction-after-syscall
  425. * in rcx. See the syscall_wrapper in syscallas.S
  426. * TODO: check SIGILL and ILL_ILLOPN
  427. */
  428. context->rcx = (long)rip + 2;
  429. context->r11 = context->efl;
  430. context->rip = (long)&syscall_wrapper;
  431. } else {
  432. deliver_signal(ALLOC_SIGINFO(SIGILL, ILL_ILLOPC,
  433. si_addr, (void *) arg), context);
  434. }
  435. } else {
  436. internal_fault("Internal illegal fault", arg, context);
  437. }
  438. DkExceptionReturn(event);
  439. }
  440. static void quit_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  441. {
  442. if (!is_internal_tid(get_cur_tid())) {
  443. deliver_signal(ALLOC_SIGINFO(SIGTERM, SI_USER, si_pid, 0), NULL);
  444. }
  445. DkExceptionReturn(event);
  446. }
  447. static void suspend_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  448. {
  449. if (!is_internal_tid(get_cur_tid())) {
  450. deliver_signal(ALLOC_SIGINFO(SIGINT, SI_USER, si_pid, 0), NULL);
  451. }
  452. DkExceptionReturn(event);
  453. }
  454. static void resume_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  455. {
  456. shim_tcb_t * tcb = shim_get_tls();
  457. if (!tcb || !tcb->tp)
  458. return;
  459. if (!is_internal_tid(get_cur_tid())) {
  460. __disable_preempt(tcb);
  461. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
  462. tcb->context.preempt |= SIGNAL_DELAYED;
  463. } else {
  464. __handle_signal(tcb, 0, NULL);
  465. }
  466. __enable_preempt(tcb);
  467. }
  468. DkExceptionReturn(event);
  469. }
  470. int init_signal (void)
  471. {
  472. DkSetExceptionHandler(&arithmetic_error_upcall, PAL_EVENT_ARITHMETIC_ERROR);
  473. DkSetExceptionHandler(&memfault_upcall, PAL_EVENT_MEMFAULT);
  474. DkSetExceptionHandler(&illegal_upcall, PAL_EVENT_ILLEGAL);
  475. DkSetExceptionHandler(&quit_upcall, PAL_EVENT_QUIT);
  476. DkSetExceptionHandler(&suspend_upcall, PAL_EVENT_SUSPEND);
  477. DkSetExceptionHandler(&resume_upcall, PAL_EVENT_RESUME);
  478. return 0;
  479. }
  480. __sigset_t * get_sig_mask (struct shim_thread * thread)
  481. {
  482. if (!thread)
  483. thread = get_cur_thread();
  484. assert(thread);
  485. return &(thread->signal_mask);
  486. }
  487. __sigset_t * set_sig_mask (struct shim_thread * thread,
  488. const __sigset_t * set)
  489. {
  490. if (!thread)
  491. thread = get_cur_thread();
  492. assert(thread);
  493. if (set)
  494. memcpy(&thread->signal_mask, set, sizeof(__sigset_t));
  495. return &thread->signal_mask;
  496. }
  497. static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *);
  498. static void
  499. __handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal)
  500. {
  501. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  502. struct shim_signal_handle * sighdl = &thread->signal_handles[sig - 1];
  503. void (*handler) (int, siginfo_t *, void *) = NULL;
  504. if (signal->info.si_signo == SIGCP) {
  505. join_checkpoint(thread, &signal->context, SI_CP_SESSION(&signal->info));
  506. return;
  507. }
  508. debug("%s handled\n", signal_name(sig));
  509. lock(&thread->lock);
  510. if (sighdl->action) {
  511. struct __kernel_sigaction * act = sighdl->action;
  512. /* This is a workaround. The truth is that many program will
  513. use sa_handler as sa_sigaction, because sa_sigaction is
  514. not supported in amd64 */
  515. #ifdef __i386__
  516. handler = (void (*) (int, siginfo_t *, void *)) act->_u._sa_handler;
  517. if (act->sa_flags & SA_SIGINFO)
  518. sa_handler = act->_u._sa_sigaction;
  519. #else
  520. handler = (void (*) (int, siginfo_t *, void *)) act->k_sa_handler;
  521. #endif
  522. if (act->sa_flags & SA_RESETHAND) {
  523. sighdl->action = NULL;
  524. free(act);
  525. }
  526. }
  527. unlock(&thread->lock);
  528. if ((void *) handler == (void *) 1) /* SIG_IGN */
  529. return;
  530. if (!handler && !(handler = default_sighandler[sig - 1]))
  531. return;
  532. /* if the context is never stored in the signal, it means the
  533. signal is handled during system calls, and before the thread
  534. is resumed. */
  535. if (!signal->context_stored)
  536. __store_context(tcb, NULL, signal);
  537. struct shim_context * context = NULL;
  538. if (tcb->context.syscall_nr) {
  539. context = __alloca(sizeof(struct shim_context));
  540. memcpy(context, &tcb->context, sizeof(struct shim_context));
  541. tcb->context.syscall_nr = 0;
  542. tcb->context.next = context;
  543. }
  544. debug("run signal handler %p (%d, %p, %p)\n", handler, sig, &signal->info,
  545. &signal->context);
  546. (*handler) (sig, &signal->info, &signal->context);
  547. if (context)
  548. memcpy(&tcb->context, context, sizeof(struct shim_context));
  549. if (signal->pal_context)
  550. memcpy(signal->pal_context, signal->context.uc_mcontext.gregs,
  551. sizeof(PAL_CONTEXT));
  552. }
  553. void __handle_signal (shim_tcb_t * tcb, int sig, ucontext_t * uc)
  554. {
  555. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  556. int begin_sig = 1, end_sig = NUM_KNOWN_SIGS;
  557. if (sig)
  558. end_sig = (begin_sig = sig) + 1;
  559. sig = begin_sig;
  560. while (atomic_read(&thread->has_signal)) {
  561. struct shim_signal * signal = NULL;
  562. for ( ; sig < end_sig ; sig++)
  563. if (!__sigismember(&thread->signal_mask, sig) &&
  564. (signal = fetch_signal_log(tcb, thread, sig)))
  565. break;
  566. if (!signal)
  567. break;
  568. if (!signal->context_stored)
  569. __store_context(tcb, NULL, signal);
  570. __handle_one_signal(tcb, sig, signal);
  571. free(signal);
  572. DkThreadYieldExecution();
  573. tcb->context.preempt &= ~SIGNAL_DELAYED;
  574. }
  575. }
  576. void handle_signal (bool delayed_only)
  577. {
  578. shim_tcb_t * tcb = shim_get_tls();
  579. assert(tcb);
  580. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  581. /* Fast path */
  582. if (!thread || !thread->has_signal.counter)
  583. return;
  584. __disable_preempt(tcb);
  585. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
  586. debug("signal delayed (%ld)\n", tcb->context.preempt & ~SIGNAL_DELAYED);
  587. tcb->context.preempt |= SIGNAL_DELAYED;
  588. } else if (!(delayed_only && !(tcb->context.preempt & SIGNAL_DELAYED))) {
  589. __handle_signal(tcb, 0, NULL);
  590. }
  591. __enable_preempt(tcb);
  592. debug("__enable_preempt: %s:%d\n", __FILE__, __LINE__);
  593. }
  594. void append_signal (struct shim_thread * thread, int sig, siginfo_t * info,
  595. bool wakeup)
  596. {
  597. struct shim_signal * signal = malloc(sizeof(struct shim_signal));
  598. if (!signal)
  599. return;
  600. /* save in signal */
  601. if (info) {
  602. __store_info(info, signal);
  603. signal->context_stored = false;
  604. } else {
  605. memset(signal, 0, sizeof(struct shim_signal));
  606. }
  607. struct shim_signal ** signal_log = allocate_signal_log(thread, sig);
  608. if (signal_log) {
  609. *signal_log = signal;
  610. if (wakeup) {
  611. debug("resuming thread %u\n", thread->tid);
  612. thread_wakeup(thread);
  613. DkThreadResume(thread->pal_handle);
  614. }
  615. } else {
  616. SYS_PRINTF("signal queue is full (TID = %u, SIG = %d)\n",
  617. thread->tid, sig);
  618. free(signal);
  619. }
  620. }
  621. static void sighandler_kill (int sig, siginfo_t * info, void * ucontext)
  622. {
  623. debug("killed by %s\n", signal_name(sig));
  624. if (!info->si_pid)
  625. switch(sig) {
  626. case SIGTERM:
  627. case SIGINT:
  628. shim_do_kill(-1, sig);
  629. break;
  630. }
  631. try_process_exit(0, sig);
  632. DkThreadExit();
  633. }
  634. /* We don't currently implement core dumps, but put a wrapper
  635. * in case we do in the future */
  636. static void sighandler_core (int sig, siginfo_t * info, void * ucontext)
  637. {
  638. sighandler_kill(sig, info, ucontext);
  639. }
  640. static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *) =
  641. {
  642. /* SIGHUP */ &sighandler_kill,
  643. /* SIGINT */ &sighandler_kill,
  644. /* SIGQUIT */ &sighandler_kill,
  645. /* SIGILL */ &sighandler_kill,
  646. /* SIGTRAP */ &sighandler_core,
  647. /* SIGABRT */ &sighandler_kill,
  648. /* SIGBUS */ &sighandler_kill,
  649. /* SIGFPE */ &sighandler_kill,
  650. /* SIGKILL */ &sighandler_kill,
  651. /* SIGUSR1 */ NULL,
  652. /* SIGSEGV */ &sighandler_kill,
  653. /* SIGUSR2 */ NULL,
  654. /* SIGPIPE */ &sighandler_kill,
  655. /* SIGALRM */ &sighandler_kill,
  656. /* SIGTERM */ &sighandler_kill,
  657. /* SIGSTKFLT */ NULL,
  658. /* SIGCHLD */ NULL,
  659. /* SIGCONT */ NULL,
  660. /* SIGSTOP */ NULL,
  661. /* SIGTSTP */ NULL,
  662. /* SIGTTIN */ NULL,
  663. /* SIGTTOU */ NULL,
  664. };