shim_signal.c 30 KB

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