shim_signal.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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. #include <asm/signal.h>
  29. // __rt_sighandler_t is different from __sighandler_t in <asm-generic/signal-defs.h>:
  30. // typedef void __signalfn_t(int);
  31. // typedef __signalfn_t *__sighandler_t
  32. typedef void (*__rt_sighandler_t)(int, siginfo_t*, void*);
  33. static __rt_sighandler_t default_sighandler[NUM_SIGS];
  34. static struct shim_signal **
  35. allocate_signal_log (struct shim_thread * thread, int sig)
  36. {
  37. if (!thread->signal_logs)
  38. return NULL;
  39. struct shim_signal_log * log = &thread->signal_logs[sig - 1];
  40. int head, tail, old_tail;
  41. do {
  42. head = atomic_read(&log->head);
  43. old_tail = tail = atomic_read(&log->tail);
  44. if (head == tail + 1 || (!head && tail == (MAX_SIGNAL_LOG - 1)))
  45. return NULL;
  46. tail = (tail == MAX_SIGNAL_LOG - 1) ? 0 : tail + 1;
  47. } while (atomic_cmpxchg(&log->tail, old_tail, tail) == tail);
  48. debug("signal_logs[%d]: head=%d, tail=%d (counter = %ld)\n", sig - 1,
  49. head, tail, thread->has_signal.counter + 1);
  50. atomic_inc(&thread->has_signal);
  51. return &log->logs[old_tail];
  52. }
  53. static struct shim_signal *
  54. fetch_signal_log (struct shim_thread * thread, int sig)
  55. {
  56. struct shim_signal_log * log = &thread->signal_logs[sig - 1];
  57. struct shim_signal * signal = NULL;
  58. int head, tail, old_head;
  59. while (1) {
  60. old_head = head = atomic_read(&log->head);
  61. tail = atomic_read(&log->tail);
  62. if (head == tail)
  63. return NULL;
  64. if (!(signal = log->logs[head]))
  65. return NULL;
  66. log->logs[head] = NULL;
  67. head = (head == MAX_SIGNAL_LOG - 1) ? 0 : head + 1;
  68. if (atomic_cmpxchg(&log->head, old_head, head) == old_head)
  69. break;
  70. log->logs[old_head] = signal;
  71. }
  72. debug("signal_logs[%d]: head=%d, tail=%d\n", sig -1, head, tail);
  73. atomic_dec(&thread->has_signal);
  74. return signal;
  75. }
  76. static void
  77. __handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal);
  78. static void __store_info (siginfo_t * info, struct shim_signal * signal)
  79. {
  80. if (info)
  81. memcpy(&signal->info, info, sizeof(siginfo_t));
  82. }
  83. void __store_context (shim_tcb_t * tcb, PAL_CONTEXT * pal_context,
  84. struct shim_signal * signal)
  85. {
  86. ucontext_t * context = &signal->context;
  87. if (tcb && tcb->context.regs && tcb->context.regs->orig_rax) {
  88. struct shim_context * ct = &tcb->context;
  89. if (ct->regs) {
  90. struct shim_regs * regs = ct->regs;
  91. context->uc_mcontext.gregs[REG_RIP] = regs->rip;
  92. context->uc_mcontext.gregs[REG_EFL] = regs->rflags;
  93. context->uc_mcontext.gregs[REG_R15] = regs->r15;
  94. context->uc_mcontext.gregs[REG_R14] = regs->r14;
  95. context->uc_mcontext.gregs[REG_R13] = regs->r13;
  96. context->uc_mcontext.gregs[REG_R12] = regs->r12;
  97. context->uc_mcontext.gregs[REG_R11] = regs->r11;
  98. context->uc_mcontext.gregs[REG_R10] = regs->r10;
  99. context->uc_mcontext.gregs[REG_R9] = regs->r9;
  100. context->uc_mcontext.gregs[REG_R8] = regs->r8;
  101. context->uc_mcontext.gregs[REG_RCX] = regs->rcx;
  102. context->uc_mcontext.gregs[REG_RDX] = regs->rdx;
  103. context->uc_mcontext.gregs[REG_RSI] = regs->rsi;
  104. context->uc_mcontext.gregs[REG_RDI] = regs->rdi;
  105. context->uc_mcontext.gregs[REG_RBX] = regs->rbx;
  106. context->uc_mcontext.gregs[REG_RBP] = regs->rbp;
  107. context->uc_mcontext.gregs[REG_RSP] = regs->rsp;
  108. }
  109. signal->context_stored = true;
  110. return;
  111. }
  112. if (pal_context) {
  113. memcpy(context->uc_mcontext.gregs, pal_context, sizeof(PAL_CONTEXT));
  114. signal->context_stored = true;
  115. }
  116. }
  117. void deliver_signal (siginfo_t * info, PAL_CONTEXT * context)
  118. {
  119. shim_tcb_t * tcb = shim_get_tcb();
  120. assert(tcb);
  121. // Signals should not be delivered before the user process starts
  122. // or after the user process dies.
  123. if (!tcb->tp || !cur_thread_is_alive())
  124. return;
  125. struct shim_thread * cur_thread = (struct shim_thread *) tcb->tp;
  126. int sig = info->si_signo;
  127. int64_t preempt = __disable_preempt(tcb);
  128. struct shim_signal * signal = __alloca(sizeof(struct shim_signal));
  129. /* save in signal */
  130. memset(signal, 0, sizeof(struct shim_signal));
  131. __store_info(info, signal);
  132. __store_context(tcb, context, signal);
  133. signal->pal_context = context;
  134. if (preempt > 1 ||
  135. __sigismember(&cur_thread->signal_mask, sig)) {
  136. struct shim_signal ** signal_log = NULL;
  137. if ((signal = malloc_copy(signal,sizeof(struct shim_signal))) &&
  138. (signal_log = allocate_signal_log(cur_thread, sig))) {
  139. *signal_log = signal;
  140. }
  141. if (signal && !signal_log) {
  142. SYS_PRINTF("signal queue is full (TID = %u, SIG = %d)\n",
  143. tcb->tid, sig);
  144. free(signal);
  145. }
  146. } else {
  147. __handle_signal(tcb, sig);
  148. __handle_one_signal(tcb, sig, signal);
  149. }
  150. __enable_preempt(tcb);
  151. }
  152. #define ALLOC_SIGINFO(signo, code, member, value) \
  153. ({ \
  154. siginfo_t * _info = __alloca(sizeof(siginfo_t)); \
  155. memset(_info, 0, sizeof(siginfo_t)); \
  156. _info->si_signo = (signo); \
  157. _info->si_code = (code); \
  158. _info->member = (value); \
  159. _info; \
  160. })
  161. #ifdef __x86_64__
  162. #define IP rip
  163. #else
  164. #define IP eip
  165. #endif
  166. static inline bool context_is_internal(PAL_CONTEXT * context)
  167. {
  168. return context &&
  169. (void *) context->IP >= (void *) &__code_address &&
  170. (void *) context->IP < (void *) &__code_address_end;
  171. }
  172. static inline void internal_fault(const char* errstr,
  173. PAL_NUM addr, PAL_CONTEXT * context)
  174. {
  175. IDTYPE tid = get_cur_tid();
  176. if (context_is_internal(context))
  177. SYS_PRINTF("%s at 0x%08lx (IP = +0x%lx, VMID = %u, TID = %u)\n", errstr,
  178. addr, (void *) context->IP - (void *) &__load_address,
  179. cur_process.vmid, is_internal_tid(tid) ? 0 : tid);
  180. else
  181. SYS_PRINTF("%s at 0x%08lx (IP = 0x%08lx, VMID = %u, TID = %u)\n", errstr,
  182. addr, context ? context->IP : 0,
  183. cur_process.vmid, is_internal_tid(tid) ? 0 : tid);
  184. PAUSE();
  185. }
  186. static void arithmetic_error_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  187. {
  188. if (is_internal_tid(get_cur_tid()) || context_is_internal(context)) {
  189. internal_fault("Internal arithmetic fault", arg, context);
  190. } else {
  191. if (context)
  192. debug("arithmetic fault at 0x%08lx\n", context->IP);
  193. deliver_signal(ALLOC_SIGINFO(SIGFPE, FPE_INTDIV,
  194. si_addr, (void *) arg), context);
  195. }
  196. DkExceptionReturn(event);
  197. }
  198. static void memfault_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  199. {
  200. shim_tcb_t * tcb = shim_get_tcb();
  201. assert(tcb);
  202. if (tcb->test_range.cont_addr && arg
  203. && (void *) arg >= tcb->test_range.start
  204. && (void *) arg <= tcb->test_range.end) {
  205. assert(context);
  206. tcb->test_range.has_fault = true;
  207. context->rip = (PAL_NUM) tcb->test_range.cont_addr;
  208. goto ret_exception;
  209. }
  210. if (is_internal_tid(get_cur_tid()) || context_is_internal(context)) {
  211. internal_fault("Internal memory fault", arg, context);
  212. goto ret_exception;
  213. }
  214. if (context)
  215. debug("memory fault at 0x%08lx (IP = 0x%08lx)\n", arg, context->IP);
  216. struct shim_vma_val vma;
  217. int signo = SIGSEGV;
  218. int code;
  219. if (!arg) {
  220. code = SEGV_MAPERR;
  221. } else if (!lookup_vma((void *) arg, &vma)) {
  222. if (vma.flags & VMA_INTERNAL) {
  223. internal_fault("Internal memory fault with VMA", arg, context);
  224. goto ret_exception;
  225. }
  226. if (vma.file && vma.file->type == TYPE_FILE) {
  227. /* DEP 3/3/17: If the mapping exceeds end of a file (but is in the VMA)
  228. * then return a SIGBUS. */
  229. uintptr_t eof_in_vma = (uintptr_t) vma.addr + vma.offset + vma.file->info.file.size;
  230. if (arg > eof_in_vma) {
  231. signo = SIGBUS;
  232. code = BUS_ADRERR;
  233. } else if ((context->err & 4) && !(vma.flags & PROT_WRITE)) {
  234. /* DEP 3/3/17: If the page fault gives a write error, and
  235. * the VMA is read-only, return SIGSEGV+SEGV_ACCERR */
  236. signo = SIGSEGV;
  237. code = SEGV_ACCERR;
  238. } else {
  239. /* XXX: need more sophisticated judgement */
  240. signo = SIGBUS;
  241. code = BUS_ADRERR;
  242. }
  243. } else {
  244. code = SEGV_ACCERR;
  245. }
  246. } else {
  247. code = SEGV_MAPERR;
  248. }
  249. deliver_signal(ALLOC_SIGINFO(signo, code, si_addr, (void *) arg), context);
  250. ret_exception:
  251. DkExceptionReturn(event);
  252. }
  253. /*
  254. * Helper function for test_user_memory / test_user_string; they behave
  255. * differently for different PALs:
  256. *
  257. * - For Linux-SGX, the faulting address is not propagated in memfault
  258. * exception (SGX v1 does not write address in SSA frame, SGX v2 writes
  259. * it only at a granularity of 4K pages). Thus, we cannot rely on
  260. * exception handling to compare against tcb.test_range.start/end.
  261. * Instead, traverse VMAs to see if [addr, addr+size) is addressable;
  262. * before traversing VMAs, grab a VMA lock.
  263. *
  264. * - For other PALs, we touch one byte of each page in [addr, addr+size).
  265. * If some byte is not addressable, exception is raised. memfault_upcall
  266. * handles this exception and resumes execution from ret_fault.
  267. *
  268. * The second option is faster in fault-free case but cannot be used under
  269. * SGX PAL. We use the best option for each PAL for now. */
  270. static bool is_sgx_pal(void) {
  271. static struct atomic_int sgx_pal = { .counter = 0 };
  272. static struct atomic_int inited = { .counter = 0 };
  273. if (!atomic_read(&inited)) {
  274. /* Ensure that is_sgx_pal is updated before initialized */
  275. atomic_set(&sgx_pal, !strcmp_static(PAL_CB(host_type), "Linux-SGX"));
  276. MB();
  277. atomic_set(&inited, 1);
  278. }
  279. MB();
  280. return atomic_read(&sgx_pal) != 0;
  281. }
  282. /*
  283. * 'test_user_memory' and 'test_user_string' are helper functions for testing
  284. * if a user-given buffer or data structure is readable / writable (according
  285. * to the system call semantics). If the memory test fails, the system call
  286. * should return -EFAULT or -EINVAL accordingly. These helper functions cannot
  287. * guarantee further corruption of the buffer, or if the buffer is unmapped
  288. * with a concurrent system call. The purpose of these functions is simply for
  289. * the compatibility with programs that rely on the error numbers, such as the
  290. * LTP test suite. */
  291. bool test_user_memory (void * addr, size_t size, bool write)
  292. {
  293. if (!size)
  294. return false;
  295. if (!access_ok(addr, size))
  296. return true;
  297. /* SGX path: check if [addr, addr+size) is addressable (in some VMA) */
  298. if (is_sgx_pal())
  299. return !is_in_adjacent_vmas(addr, size);
  300. /* Non-SGX path: check if [addr, addr+size) is addressable by touching
  301. * a byte of each page; invalid access will be caught in memfault_upcall */
  302. shim_tcb_t * tcb = shim_get_tcb();
  303. assert(tcb && tcb->tp);
  304. __disable_preempt(tcb);
  305. /* Add the memory region to the watch list. This is not racy because
  306. * each thread has its own record. */
  307. assert(!tcb->test_range.cont_addr);
  308. tcb->test_range.has_fault = false;
  309. tcb->test_range.cont_addr = &&ret_fault;
  310. tcb->test_range.start = addr;
  311. tcb->test_range.end = addr + size - 1;
  312. /* enforce compiler to store tcb->test_range into memory */
  313. __asm__ volatile(""::: "memory");
  314. /* Try to read or write into one byte inside each page */
  315. void * tmp = addr;
  316. while (tmp <= addr + size - 1) {
  317. if (write) {
  318. *(volatile char *) tmp = *(volatile char *) tmp;
  319. } else {
  320. *(volatile char *) tmp;
  321. }
  322. tmp = ALLOC_ALIGN_UP_PTR(tmp + 1);
  323. }
  324. ret_fault:
  325. /* enforce compiler to load tcb->test_range.has_fault below */
  326. __asm__ volatile("": "=m"(tcb->test_range.has_fault));
  327. /* If any read or write into the target region causes an exception,
  328. * the control flow will immediately jump to here. */
  329. bool has_fault = tcb->test_range.has_fault;
  330. tcb->test_range.has_fault = false;
  331. tcb->test_range.cont_addr = NULL;
  332. tcb->test_range.start = tcb->test_range.end = NULL;
  333. __enable_preempt(tcb);
  334. return has_fault;
  335. }
  336. /*
  337. * This function tests a user string with unknown length. It only tests
  338. * whether the memory is readable.
  339. */
  340. bool test_user_string (const char * addr)
  341. {
  342. if (!access_ok(addr, 1))
  343. return true;
  344. size_t size, maxlen;
  345. const char* next = ALLOC_ALIGN_UP_PTR(addr + 1);
  346. /* SGX path: check if [addr, addr+size) is addressable (in some VMA). */
  347. if (is_sgx_pal()) {
  348. /* We don't know length but using unprotected strlen() is dangerous
  349. * so we check string in chunks of 4K pages. */
  350. do {
  351. maxlen = next - addr;
  352. if (!access_ok(addr, maxlen) || !is_in_adjacent_vmas((void*) addr, maxlen))
  353. return true;
  354. size = strnlen(addr, maxlen);
  355. addr = next;
  356. next = ALLOC_ALIGN_UP_PTR(addr + 1);
  357. } while (size == maxlen);
  358. return false;
  359. }
  360. /* Non-SGX path: check if [addr, addr+size) is addressable by touching
  361. * a byte of each page; invalid access will be caught in memfault_upcall. */
  362. shim_tcb_t * tcb = shim_get_tcb();
  363. assert(tcb && tcb->tp);
  364. __disable_preempt(tcb);
  365. assert(!tcb->test_range.cont_addr);
  366. tcb->test_range.has_fault = false;
  367. tcb->test_range.cont_addr = &&ret_fault;
  368. /* enforce compiler to store tcb->test_range into memory */
  369. __asm__ volatile(""::: "memory");
  370. do {
  371. /* Add the memory region to the watch list. This is not racy because
  372. * each thread has its own record. */
  373. tcb->test_range.start = (void *) addr;
  374. tcb->test_range.end = (void *) (next - 1);
  375. maxlen = next - addr;
  376. if (!access_ok(addr, maxlen))
  377. return true;
  378. *(volatile char *) addr; /* try to read one byte from the page */
  379. size = strnlen(addr, maxlen);
  380. addr = next;
  381. next = ALLOC_ALIGN_UP_PTR(addr + 1);
  382. } while (size == maxlen);
  383. ret_fault:
  384. /* enforce compiler to load tcb->test_range.has_fault below */
  385. __asm__ volatile("": "=m"(tcb->test_range.has_fault));
  386. /* If any read or write into the target region causes an exception,
  387. * the control flow will immediately jump to here. */
  388. bool has_fault = tcb->test_range.has_fault;
  389. tcb->test_range.has_fault = false;
  390. tcb->test_range.cont_addr = NULL;
  391. tcb->test_range.start = tcb->test_range.end = NULL;
  392. __enable_preempt(tcb);
  393. return has_fault;
  394. }
  395. void __attribute__((weak)) syscall_wrapper(void)
  396. {
  397. /*
  398. * work around for link.
  399. * syscalldb.S is excluded for libsysdb_debug.so so it fails to link
  400. * due to missing syscall_wrapper.
  401. */
  402. }
  403. static void illegal_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  404. {
  405. struct shim_vma_val vma;
  406. if (!is_internal_tid(get_cur_tid()) &&
  407. !context_is_internal(context) &&
  408. !(lookup_vma((void *) arg, &vma)) &&
  409. !(vma.flags & VMA_INTERNAL)) {
  410. assert(context);
  411. debug("illegal instruction at 0x%08lx\n", context->IP);
  412. uint8_t * rip = (uint8_t*)context->IP;
  413. /*
  414. * Emulate syscall instruction (opcode 0x0f 0x05);
  415. * syscall instruction is prohibited in
  416. * Linux-SGX PAL and raises a SIGILL exception and
  417. * Linux PAL with seccomp and raise SIGSYS exception.
  418. */
  419. #if 0
  420. if (rip[-2] == 0x0f && rip[-1] == 0x05) {
  421. /* TODO: once finished, remove "#if 0" above. */
  422. /*
  423. * SIGSYS case (can happen with Linux PAL with seccomp)
  424. * rip points to the address after syscall instruction
  425. * %rcx: syscall instruction must put an
  426. * instruction-after-syscall in rcx
  427. */
  428. context->rax = siginfo->si_syscall; /* PAL_CONTEXT doesn't
  429. * include a member
  430. * corresponding to
  431. * siginfo_t::si_syscall yet.
  432. */
  433. context->rcx = (long)rip;
  434. context->r11 = context->efl;
  435. context->rip = (long)&syscall_wrapper;
  436. } else
  437. #endif
  438. if (rip[0] == 0x0f && rip[1] == 0x05) {
  439. /*
  440. * SIGILL case (can happen in Linux-SGX PAL)
  441. * %rcx: syscall instruction must put an instruction-after-syscall
  442. * in rcx. See the syscall_wrapper in syscallas.S
  443. * TODO: check SIGILL and ILL_ILLOPN
  444. */
  445. context->rcx = (long)rip + 2;
  446. context->r11 = context->efl;
  447. context->rip = (long)&syscall_wrapper;
  448. } else {
  449. deliver_signal(ALLOC_SIGINFO(SIGILL, ILL_ILLOPC,
  450. si_addr, (void *) arg), context);
  451. }
  452. } else {
  453. internal_fault("Internal illegal fault", arg, context);
  454. }
  455. DkExceptionReturn(event);
  456. }
  457. static void quit_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  458. {
  459. __UNUSED(arg);
  460. __UNUSED(context);
  461. if (!is_internal_tid(get_cur_tid())) {
  462. deliver_signal(ALLOC_SIGINFO(SIGTERM, SI_USER, si_pid, 0), NULL);
  463. }
  464. DkExceptionReturn(event);
  465. }
  466. static void suspend_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  467. {
  468. __UNUSED(arg);
  469. __UNUSED(context);
  470. if (!is_internal_tid(get_cur_tid())) {
  471. deliver_signal(ALLOC_SIGINFO(SIGINT, SI_USER, si_pid, 0), NULL);
  472. }
  473. DkExceptionReturn(event);
  474. }
  475. static void resume_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  476. {
  477. __UNUSED(arg);
  478. __UNUSED(context);
  479. shim_tcb_t * tcb = shim_get_tcb();
  480. if (!tcb || !tcb->tp)
  481. return;
  482. if (!is_internal_tid(get_cur_tid())) {
  483. int64_t preempt = __disable_preempt(tcb);
  484. if (preempt <= 1)
  485. __handle_signal(tcb, 0);
  486. __enable_preempt(tcb);
  487. }
  488. DkExceptionReturn(event);
  489. }
  490. int init_signal (void)
  491. {
  492. DkSetExceptionHandler(&arithmetic_error_upcall, PAL_EVENT_ARITHMETIC_ERROR);
  493. DkSetExceptionHandler(&memfault_upcall, PAL_EVENT_MEMFAULT);
  494. DkSetExceptionHandler(&illegal_upcall, PAL_EVENT_ILLEGAL);
  495. DkSetExceptionHandler(&quit_upcall, PAL_EVENT_QUIT);
  496. DkSetExceptionHandler(&suspend_upcall, PAL_EVENT_SUSPEND);
  497. DkSetExceptionHandler(&resume_upcall, PAL_EVENT_RESUME);
  498. return 0;
  499. }
  500. __sigset_t * get_sig_mask (struct shim_thread * thread)
  501. {
  502. if (!thread)
  503. thread = get_cur_thread();
  504. assert(thread);
  505. return &(thread->signal_mask);
  506. }
  507. __sigset_t * set_sig_mask (struct shim_thread * thread,
  508. const __sigset_t * set)
  509. {
  510. if (!thread)
  511. thread = get_cur_thread();
  512. assert(thread);
  513. if (set) {
  514. memcpy(&thread->signal_mask, set, sizeof(__sigset_t));
  515. /* SIGKILL and SIGSTOP cannot be ignored */
  516. __sigdelset(&thread->signal_mask, SIGKILL);
  517. __sigdelset(&thread->signal_mask, SIGSTOP);
  518. }
  519. return &thread->signal_mask;
  520. }
  521. static __rt_sighandler_t __get_sighandler(struct shim_thread* thread, int sig) {
  522. struct shim_signal_handle* sighdl = &thread->signal_handles[sig - 1];
  523. __rt_sighandler_t handler = NULL;
  524. if (sighdl->action) {
  525. struct __kernel_sigaction * act = sighdl->action;
  526. /*
  527. * on amd64, sa_handler can be treated as sa_sigaction
  528. * because 1-3 arguments are passed by register and
  529. * sa_handler simply ignores 2nd and 3rd argument.
  530. */
  531. #ifdef __i386__
  532. # error "x86-32 support is heavily broken."
  533. #endif
  534. handler = (void*)act->k_sa_handler;
  535. if (act->sa_flags & SA_RESETHAND) {
  536. sighdl->action = NULL;
  537. free(act);
  538. }
  539. }
  540. if ((void*)handler == SIG_IGN)
  541. return NULL;
  542. return handler ? : default_sighandler[sig - 1];
  543. }
  544. static void
  545. __handle_one_signal(shim_tcb_t* tcb, int sig, struct shim_signal* signal) {
  546. struct shim_thread* thread = (struct shim_thread*)tcb->tp;
  547. __rt_sighandler_t handler = NULL;
  548. if (signal->info.si_signo == SIGCP) {
  549. join_checkpoint(thread, SI_CP_SESSION(&signal->info));
  550. return;
  551. }
  552. lock(&thread->lock);
  553. handler = __get_sighandler(thread, sig);
  554. unlock(&thread->lock);
  555. if (!handler)
  556. return;
  557. debug("%s handled\n", signal_name(sig));
  558. // If the context is never stored in the signal, it means the signal is handled during
  559. // system calls, and before the thread is resumed.
  560. if (!signal->context_stored)
  561. __store_context(tcb, NULL, signal);
  562. struct shim_context * context = NULL;
  563. if (tcb->context.regs && tcb->context.regs->orig_rax) {
  564. context = __alloca(sizeof(struct shim_context));
  565. memcpy(context, &tcb->context, sizeof(struct shim_context));
  566. tcb->context.regs->orig_rax = 0;
  567. tcb->context.next = context;
  568. }
  569. debug("run signal handler %p (%d, %p, %p)\n", handler, sig, &signal->info,
  570. &signal->context);
  571. (*handler) (sig, &signal->info, &signal->context);
  572. if (context)
  573. memcpy(&tcb->context, context, sizeof(struct shim_context));
  574. if (signal->pal_context)
  575. memcpy(signal->pal_context, signal->context.uc_mcontext.gregs, sizeof(PAL_CONTEXT));
  576. }
  577. void __handle_signal (shim_tcb_t * tcb, int sig)
  578. {
  579. struct shim_thread * thread = tcb->tp;
  580. assert(thread);
  581. int begin_sig = 1, end_sig = NUM_KNOWN_SIGS;
  582. if (sig)
  583. end_sig = (begin_sig = sig) + 1;
  584. sig = begin_sig;
  585. while (atomic_read(&thread->has_signal)) {
  586. struct shim_signal * signal = NULL;
  587. for ( ; sig < end_sig ; sig++)
  588. if (!__sigismember(&thread->signal_mask, sig) &&
  589. (signal = fetch_signal_log(thread, sig)))
  590. break;
  591. if (!signal)
  592. break;
  593. if (!signal->context_stored)
  594. __store_context(tcb, NULL, signal);
  595. __handle_one_signal(tcb, sig, signal);
  596. free(signal);
  597. DkThreadYieldExecution();
  598. }
  599. }
  600. void handle_signal (void)
  601. {
  602. shim_tcb_t * tcb = shim_get_tcb();
  603. assert(tcb);
  604. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  605. /* Fast path */
  606. if (!thread || !thread->has_signal.counter)
  607. return;
  608. int64_t preempt = __disable_preempt(tcb);
  609. if (preempt > 1)
  610. debug("signal delayed (%ld)\n", preempt);
  611. else
  612. __handle_signal(tcb, 0);
  613. __enable_preempt(tcb);
  614. debug("__enable_preempt: %s:%d\n", __FILE__, __LINE__);
  615. }
  616. // Need to hold thread->lock when calling this function
  617. void append_signal(struct shim_thread* thread, int sig, siginfo_t* info, bool need_interrupt) {
  618. __rt_sighandler_t handler = __get_sighandler(thread, sig);
  619. if (!handler) {
  620. // SIGSTOP and SIGKILL cannot be ignored
  621. assert(sig != SIGSTOP && sig != SIGKILL);
  622. /*
  623. * If signal is ignored and unmasked, the signal can be discarded
  624. * directly. Otherwise it causes memory leak.
  625. *
  626. * SIGCHLD can be discarded even if it's masked.
  627. * For Linux implementation, please refer to
  628. * do_notify_parent() in linux/kernel/signal.c
  629. * For standard, please refer to
  630. * https://pubs.opengroup.org/onlinepubs/9699919799/functions/_Exit.html
  631. */
  632. if (!__sigismember(&thread->signal_mask, sig) || sig == SIGCHLD)
  633. return;
  634. // If a signal is set to be ignored, append the signal but don't interrupt the thread
  635. need_interrupt = false;
  636. }
  637. struct shim_signal * signal = malloc(sizeof(struct shim_signal));
  638. if (!signal)
  639. return;
  640. /* save in signal */
  641. if (info) {
  642. __store_info(info, signal);
  643. signal->context_stored = false;
  644. } else {
  645. memset(signal, 0, sizeof(struct shim_signal));
  646. }
  647. struct shim_signal ** signal_log = allocate_signal_log(thread, sig);
  648. if (signal_log) {
  649. *signal_log = signal;
  650. if (need_interrupt) {
  651. debug("resuming thread %u\n", thread->tid);
  652. thread_wakeup(thread);
  653. DkThreadResume(thread->pal_handle);
  654. }
  655. } else {
  656. SYS_PRINTF("signal queue is full (TID = %u, SIG = %d)\n",
  657. thread->tid, sig);
  658. free(signal);
  659. }
  660. }
  661. #define __WCOREDUMP_BIT 0x80
  662. static void sighandler_kill (int sig, siginfo_t * info, void * ucontext)
  663. {
  664. struct shim_thread* cur_thread = get_cur_thread();
  665. int sig_without_coredump_bit = sig & ~(__WCOREDUMP_BIT);
  666. __UNUSED(ucontext);
  667. debug("killed by %s\n", signal_name(sig_without_coredump_bit));
  668. if (sig_without_coredump_bit == SIGABRT ||
  669. (!info->si_pid && /* signal is sent from host OS, not from another process */
  670. (sig_without_coredump_bit == SIGTERM || sig_without_coredump_bit == SIGINT))) {
  671. /* Received signal to kill the process:
  672. * - SIGABRT must always kill the whole process (even if sent by Graphene itself),
  673. * - SIGTERM/SIGINT must kill the whole process if signal sent from host OS. */
  674. /* If several signals arrive simultaneously, only one signal proceeds past this
  675. * point. For more information, see shim_do_exit_group(). */
  676. static struct atomic_int first = ATOMIC_INIT(0);
  677. if (atomic_cmpxchg(&first, 0, 1) == 1) {
  678. while (1)
  679. DkThreadYieldExecution();
  680. }
  681. do_kill_proc(cur_thread->tgid, cur_thread->tgid, SIGKILL, false);
  682. /* Ensure that the current thread wins in setting the process code/signal.
  683. * For more information, see shim_do_exit_group(). */
  684. while (check_last_thread(cur_thread)) {
  685. DkThreadYieldExecution();
  686. }
  687. }
  688. try_process_exit(0, sig);
  689. DkThreadExit();
  690. }
  691. static void sighandler_core (int sig, siginfo_t * info, void * ucontext)
  692. {
  693. /* NOTE: This implementation only indicates the core dump for wait4()
  694. * and friends. No actual core-dump file is created. */
  695. sig = __WCOREDUMP_BIT | sig;
  696. sighandler_kill(sig, info, ucontext);
  697. }
  698. static __rt_sighandler_t default_sighandler[NUM_SIGS] = {
  699. /* SIGHUP */ &sighandler_kill,
  700. /* SIGINT */ &sighandler_kill,
  701. /* SIGQUIT */ &sighandler_core,
  702. /* SIGILL */ &sighandler_core,
  703. /* SIGTRAP */ &sighandler_core,
  704. /* SIGABRT */ &sighandler_core,
  705. /* SIGBUS */ &sighandler_core,
  706. /* SIGFPE */ &sighandler_core,
  707. /* SIGKILL */ &sighandler_kill,
  708. /* SIGUSR1 */ &sighandler_kill,
  709. /* SIGSEGV */ &sighandler_core,
  710. /* SIGUSR2 */ &sighandler_kill,
  711. /* SIGPIPE */ &sighandler_kill,
  712. /* SIGALRM */ &sighandler_kill,
  713. /* SIGTERM */ &sighandler_kill,
  714. /* SIGSTKFLT */ &sighandler_kill,
  715. /* SIGCHLD */ NULL,
  716. /* SIGCONT */ NULL,
  717. /* SIGSTOP */ NULL,
  718. /* SIGTSTP */ NULL,
  719. /* SIGTTIN */ NULL,
  720. /* SIGTTOU */ NULL,
  721. /* SIGURG */ NULL,
  722. /* SIGXCPU */ &sighandler_core,
  723. /* SIGXFSZ */ &sighandler_core,
  724. /* SIGVTALRM */ &sighandler_kill,
  725. /* SIGPROF */ &sighandler_kill,
  726. /* SIGWINCH */ NULL,
  727. /* SIGIO */ &sighandler_kill,
  728. /* SIGPWR */ &sighandler_kill,
  729. /* SIGSYS */ &sighandler_core
  730. };