shim_signal.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_signal.c
  17. *
  18. * This file contains codes to handle signals and exceptions passed from PAL.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_utils.h>
  22. #include <shim_table.h>
  23. #include <shim_thread.h>
  24. #include <shim_handle.h>
  25. #include <shim_vma.h>
  26. #include <shim_checkpoint.h>
  27. #include <shim_signal.h>
  28. #include <shim_unistd.h>
  29. #include <pal.h>
  30. static struct shim_signal **
  31. allocate_signal_log (struct shim_thread * thread, int sig)
  32. {
  33. if (!thread->signal_logs)
  34. return NULL;
  35. struct shim_signal_log * log = &thread->signal_logs[sig - 1];
  36. int head, tail, old_tail;
  37. do {
  38. head = atomic_read(&log->head);
  39. old_tail = tail = atomic_read(&log->tail);
  40. if (head == tail + 1 || (!head && tail == (MAX_SIGNAL_LOG - 1)))
  41. return NULL;
  42. tail = (tail == MAX_SIGNAL_LOG - 1) ? 0 : tail + 1;
  43. } while (atomic_cmpxchg(&log->tail, old_tail, tail) == tail);
  44. 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. atomic_dec(&thread->has_signal);
  67. return signal;
  68. }
  69. static void
  70. __handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal);
  71. static void __store_info (siginfo_t * info, struct shim_signal * signal)
  72. {
  73. if (info)
  74. memcpy(&signal->info, info, sizeof(siginfo_t));
  75. }
  76. void __store_context (shim_tcb_t * tcb, PAL_CONTEXT * pal_context,
  77. struct shim_signal * signal)
  78. {
  79. ucontext_t * context = &signal->context;
  80. if (tcb && tcb->context.syscall_nr) {
  81. struct shim_context * ct = &tcb->context;
  82. context->uc_mcontext.gregs[REG_RSP] = (unsigned long) ct->sp;
  83. context->uc_mcontext.gregs[REG_RIP] = (unsigned long) ct->ret_ip;
  84. if (ct->regs) {
  85. struct shim_regs * regs = ct->regs;
  86. context->uc_mcontext.gregs[REG_R15] = regs->r15;
  87. context->uc_mcontext.gregs[REG_R14] = regs->r14;
  88. context->uc_mcontext.gregs[REG_R13] = regs->r13;
  89. context->uc_mcontext.gregs[REG_R12] = regs->r12;
  90. context->uc_mcontext.gregs[REG_R11] = regs->r11;
  91. context->uc_mcontext.gregs[REG_R10] = regs->r10;
  92. context->uc_mcontext.gregs[REG_R9] = regs->r9;
  93. context->uc_mcontext.gregs[REG_R8] = regs->r8;
  94. context->uc_mcontext.gregs[REG_RCX] = regs->rcx;
  95. context->uc_mcontext.gregs[REG_RDX] = regs->rdx;
  96. context->uc_mcontext.gregs[REG_RSI] = regs->rsi;
  97. context->uc_mcontext.gregs[REG_RDI] = regs->rdi;
  98. context->uc_mcontext.gregs[REG_RBX] = regs->rbx;
  99. context->uc_mcontext.gregs[REG_RBP] = regs->rbp;
  100. }
  101. signal->context_stored = true;
  102. return;
  103. }
  104. if (pal_context) {
  105. memcpy(context->uc_mcontext.gregs, pal_context, sizeof(PAL_CONTEXT));
  106. signal->context_stored = true;
  107. }
  108. }
  109. void deliver_signal (siginfo_t * info, PAL_CONTEXT * context)
  110. {
  111. shim_tcb_t * tcb = SHIM_GET_TLS();
  112. if (!tcb || !tcb->tp)
  113. return;
  114. struct shim_thread * cur_thread = (struct shim_thread *) tcb->tp;
  115. int sig = info->si_signo;
  116. __disable_preempt(tcb);
  117. struct shim_signal * signal = __alloca(sizeof(struct shim_signal));
  118. /* save in signal */
  119. memset(signal, 0, sizeof(struct shim_signal));
  120. __store_info(info, signal);
  121. __store_context(tcb, context, signal);
  122. signal->pal_context = context;
  123. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1)
  124. goto delay;
  125. if (__sigismember(&cur_thread->signal_mask, sig))
  126. goto delay;
  127. __handle_signal(tcb, sig, &signal->context);
  128. __handle_one_signal(tcb, sig, signal);
  129. goto out;
  130. delay:
  131. {
  132. if (!(signal = remalloc(signal,sizeof(struct shim_signal))))
  133. goto out;
  134. struct shim_signal ** signal_log = allocate_signal_log(cur_thread, sig);
  135. if (!signal_log) {
  136. sys_printf("signal queue is full (TID = %u, SIG = %d)\n",
  137. tcb->tid, sig);
  138. free(signal);
  139. goto out;
  140. }
  141. *signal_log = signal;
  142. }
  143. out:
  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. #define is_internal(context) \
  161. ((context) && \
  162. (void *) (context)->IP >= (void *) &__code_address && \
  163. (void *) (context)->IP < (void *) &__code_address_end)
  164. #define internal_fault(errstr, addr, context) \
  165. do { \
  166. IDTYPE tid = get_cur_tid(); \
  167. if (is_internal((context))) \
  168. sys_printf(errstr " at %p (IP = +0x%lx, VMID = %u, TID = %u)\n",\
  169. arg, \
  170. (void *) context->IP - (void *) &__load_address, \
  171. cur_process.vmid, IS_INTERNAL_TID(tid) ? 0 : tid); \
  172. else \
  173. sys_printf(errstr " at %p (IP = %p, VMID = %u, TID = %u)\n", \
  174. arg, context ? context->IP : 0, \
  175. cur_process.vmid, IS_INTERNAL_TID(tid) ? 0 : tid); \
  176. } while (0)
  177. static void divzero_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  178. {
  179. if (IS_INTERNAL_TID(get_cur_tid()) || is_internal(context)) {
  180. internal_fault("Internal arithmetic fault", arg, context);
  181. pause();
  182. goto ret_exception;
  183. }
  184. if (context)
  185. debug("arithmetic fault at %p\n", context->IP);
  186. deliver_signal(ALLOC_SIGINFO(SIGFPE, FPE_INTDIV, si_addr, (void *) arg), context);
  187. ret_exception:
  188. DkExceptionReturn(event);
  189. }
  190. static void memfault_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  191. {
  192. if (IS_INTERNAL_TID(get_cur_tid()) || is_internal(context)) {
  193. internal:
  194. internal_fault("Internal memory fault", arg, context);
  195. pause();
  196. goto ret_exception;
  197. }
  198. if (context)
  199. debug("memory fault at %p (IP = %p)\n", arg, context->IP);
  200. struct shim_vma * vma = NULL;
  201. int signo = SIGSEGV;
  202. int code;
  203. if (!arg) {
  204. code = SEGV_MAPERR;
  205. } else if (!lookup_supervma((void *) arg, 0, &vma)) {
  206. if (vma->flags & VMA_INTERNAL) {
  207. put_vma(vma);
  208. goto internal;
  209. }
  210. if (vma->file) {
  211. /* XXX: need more sophisticated judgement */
  212. signo = SIGBUS;
  213. code = BUS_ADRERR;
  214. } else {
  215. code = SEGV_ACCERR;
  216. }
  217. put_vma(vma);
  218. } else {
  219. code = SEGV_MAPERR;
  220. }
  221. deliver_signal(ALLOC_SIGINFO(signo, code, si_addr, (void *) arg), context);
  222. ret_exception:
  223. DkExceptionReturn(event);
  224. }
  225. static void illegal_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  226. {
  227. if (IS_INTERNAL_TID(get_cur_tid()) || is_internal(context)) {
  228. internal:
  229. internal_fault("Internal illegal fault", arg, context);
  230. pause();
  231. goto ret_exception;
  232. }
  233. struct shim_vma * vma = NULL;
  234. if (!(lookup_supervma((void *) arg, 0, &vma)) &&
  235. !(vma->flags & VMA_INTERNAL)) {
  236. if (context)
  237. debug("illegal instruction at %p\n", context->IP);
  238. if (vma)
  239. put_vma(vma);
  240. deliver_signal(ALLOC_SIGINFO(SIGILL, ILL_ILLOPC, si_addr, (void *) arg), context);
  241. } else {
  242. if (vma)
  243. put_vma(vma);
  244. goto internal;
  245. }
  246. ret_exception:
  247. DkExceptionReturn(event);
  248. }
  249. static void quit_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  250. {
  251. if (IS_INTERNAL_TID(get_cur_tid()))
  252. goto ret_exception;
  253. deliver_signal(ALLOC_SIGINFO(SIGTERM, SI_USER, si_pid, 0), NULL);
  254. ret_exception:
  255. DkExceptionReturn(event);
  256. }
  257. static void suspend_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  258. {
  259. if (IS_INTERNAL_TID(get_cur_tid()))
  260. goto ret_exception;
  261. deliver_signal(ALLOC_SIGINFO(SIGINT, SI_USER, si_pid, 0), NULL);
  262. ret_exception:
  263. DkExceptionReturn(event);
  264. }
  265. static void resume_upcall (PAL_PTR event, PAL_NUM arg, PAL_CONTEXT * context)
  266. {
  267. if (IS_INTERNAL_TID(get_cur_tid()))
  268. goto ret_exception;
  269. shim_tcb_t * tcb = SHIM_GET_TLS();
  270. if (!tcb || !tcb->tp)
  271. return;
  272. __disable_preempt(tcb);
  273. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
  274. tcb->context.preempt |= SIGNAL_DELAYED;
  275. __enable_preempt(tcb);
  276. goto ret_exception;
  277. }
  278. __handle_signal(tcb, 0, NULL);
  279. __enable_preempt(tcb);
  280. ret_exception:
  281. DkExceptionReturn(event);
  282. }
  283. int init_signal (void)
  284. {
  285. DkSetExceptionHandler(&divzero_upcall, PAL_EVENT_DIVZERO, 0);
  286. DkSetExceptionHandler(&memfault_upcall, PAL_EVENT_MEMFAULT, 0);
  287. DkSetExceptionHandler(&illegal_upcall, PAL_EVENT_ILLEGAL, 0);
  288. DkSetExceptionHandler(&quit_upcall, PAL_EVENT_QUIT, 0);
  289. DkSetExceptionHandler(&suspend_upcall, PAL_EVENT_SUSPEND, 0);
  290. DkSetExceptionHandler(&resume_upcall, PAL_EVENT_RESUME, 0);
  291. return 0;
  292. }
  293. __sigset_t * get_sig_mask (struct shim_thread * thread)
  294. {
  295. if (!thread)
  296. thread = get_cur_thread();
  297. assert(thread);
  298. return &(thread->signal_mask);
  299. }
  300. __sigset_t * set_sig_mask (struct shim_thread * thread, __sigset_t * set)
  301. {
  302. if (!thread)
  303. thread = get_cur_thread();
  304. assert(thread);
  305. if (set)
  306. memcpy(&thread->signal_mask, set, sizeof(__sigset_t));
  307. return &thread->signal_mask;
  308. }
  309. static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *);
  310. static void
  311. __handle_one_signal (shim_tcb_t * tcb, int sig, struct shim_signal * signal)
  312. {
  313. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  314. struct shim_signal_handle * sighdl = &thread->signal_handles[sig - 1];
  315. void (*handler) (int, siginfo_t *, void *) = NULL;
  316. if (signal->info.si_signo == SIGCP) {
  317. join_checkpoint(thread, &signal->context, si_cp_session(&signal->info));
  318. return;
  319. }
  320. debug("%s handled\n", signal_name(sig));
  321. lock(thread->lock);
  322. if (sighdl->action) {
  323. struct __kernel_sigaction * act = sighdl->action;
  324. /* This is a workaround. The truth is that many program will
  325. use sa_handler as sa_sigaction, because sa_sigaction is
  326. not supported in amd64 */
  327. #ifdef __i386__
  328. handler = (void (*) (int, siginfo_t *, void *)) act->_u._sa_handler;
  329. if (act->sa_flags & SA_SIGINFO)
  330. sa_handler = act->_u._sa_sigaction;
  331. #else
  332. handler = (void (*) (int, siginfo_t *, void *)) act->k_sa_handler;
  333. #endif
  334. if (act->sa_flags & SA_RESETHAND) {
  335. sighdl->action = NULL;
  336. free(act);
  337. }
  338. }
  339. unlock(thread->lock);
  340. if ((void *) handler == (void *) 1) /* SIG_IGN */
  341. return;
  342. if (!handler && !(handler = default_sighandler[sig - 1]))
  343. return;
  344. /* if the context is never stored in the signal, it means the
  345. signal is handled during system calls, and before the thread
  346. is resumed. */
  347. if (!signal->context_stored)
  348. __store_context(tcb, NULL, signal);
  349. struct shim_context * context = NULL;
  350. if (tcb->context.syscall_nr) {
  351. context = __alloca(sizeof(struct shim_context));
  352. memcpy(context, &tcb->context, sizeof(struct shim_context));
  353. tcb->context.syscall_nr = 0;
  354. tcb->context.next = context;
  355. }
  356. debug("run signal handler %p (%d, %p, %p)\n", handler, sig, &signal->info,
  357. &signal->context);
  358. (*handler) (sig, &signal->info, &signal->context);
  359. if (context)
  360. memcpy(&tcb->context, context, sizeof(struct shim_context));
  361. if (signal->pal_context)
  362. memcpy(signal->pal_context, signal->context.uc_mcontext.gregs,
  363. sizeof(PAL_CONTEXT));
  364. }
  365. void __handle_signal (shim_tcb_t * tcb, int sig, ucontext_t * uc)
  366. {
  367. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  368. int begin_sig = 1, end_sig = NUM_KNOWN_SIGS;
  369. if (sig)
  370. end_sig = (begin_sig = sig) + 1;
  371. sig = begin_sig;
  372. while (atomic_read(&thread->has_signal)) {
  373. struct shim_signal * signal = NULL;
  374. for ( ; sig < end_sig ; sig++)
  375. if (!__sigismember(&thread->signal_mask, sig) &&
  376. (signal = fetch_signal_log(tcb, thread, sig)))
  377. break;
  378. if (!signal)
  379. break;
  380. if (!signal->context_stored)
  381. __store_context(tcb, NULL, signal);
  382. __handle_one_signal(tcb, sig, signal);
  383. free(signal);
  384. DkThreadYieldExecution();
  385. tcb->context.preempt &= ~SIGNAL_DELAYED;
  386. }
  387. }
  388. void handle_signal (bool delayed_only)
  389. {
  390. shim_tcb_t * tcb = SHIM_GET_TLS();
  391. if (!tcb || !tcb->tp)
  392. return;
  393. struct shim_thread * thread = (struct shim_thread *) tcb->tp;
  394. /* Fast path */
  395. if (!thread->has_signal.counter)
  396. return;
  397. __disable_preempt(tcb);
  398. if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) {
  399. tcb->context.preempt |= SIGNAL_DELAYED;
  400. goto out;
  401. }
  402. if (delayed_only && !(tcb->context.preempt & SIGNAL_DELAYED))
  403. goto out;
  404. __handle_signal(tcb, 0, NULL);
  405. out:
  406. __enable_preempt(tcb);
  407. }
  408. void append_signal (struct shim_thread * thread, int sig, siginfo_t * info,
  409. bool wakeup)
  410. {
  411. struct shim_signal * signal = malloc(sizeof(struct shim_signal));
  412. if (!signal)
  413. return;
  414. /* save in signal */
  415. if (info) {
  416. __store_info(info, signal);
  417. signal->context_stored = false;
  418. } else {
  419. memset(signal, 0, sizeof(struct shim_signal));
  420. }
  421. struct shim_signal ** signal_log = allocate_signal_log(thread, sig);
  422. if (signal_log) {
  423. *signal_log = signal;
  424. if (wakeup) {
  425. debug("resuming thread %u\n", thread->tid);
  426. DkThreadResume(thread->pal_handle);
  427. }
  428. } else {
  429. sys_printf("signal queue is full (TID = %u, SIG = %d)\n",
  430. thread->tid, sig);
  431. free(signal);
  432. }
  433. }
  434. static void sighandler_kill (int sig, siginfo_t * info, void * ucontext)
  435. {
  436. debug("killed by %s\n", signal_name(sig));
  437. if (!info->si_pid)
  438. switch(sig) {
  439. case SIGTERM:
  440. case SIGINT:
  441. shim_do_kill(-1, sig);
  442. break;
  443. }
  444. try_process_exit(0);
  445. DkThreadExit();
  446. }
  447. static void (*default_sighandler[NUM_SIGS]) (int, siginfo_t *, void *) =
  448. {
  449. /* SIGHUP */ &sighandler_kill,
  450. /* SIGINT */ &sighandler_kill,
  451. /* SIGQUIT */ &sighandler_kill,
  452. /* SIGILL */ &sighandler_kill,
  453. /* SIGTRAP */ NULL,
  454. /* SIGABRT */ &sighandler_kill,
  455. /* SIGBUS */ &sighandler_kill,
  456. /* SIGFPE */ &sighandler_kill,
  457. /* SIGKILL */ &sighandler_kill,
  458. /* SIGUSR1 */ NULL,
  459. /* SIGSEGV */ &sighandler_kill,
  460. /* SIGUSR2 */ NULL,
  461. /* SIGPIPE */ &sighandler_kill,
  462. /* SIGALRM */ NULL,
  463. /* SIGTERM */ &sighandler_kill,
  464. /* SIGSTKFLT */ NULL,
  465. /* SIGCHLD */ NULL,
  466. /* SIGCONT */ NULL,
  467. /* SIGSTOP */ NULL,
  468. /* SIGTSTP */ NULL,
  469. /* SIGTTIN */ NULL,
  470. /* SIGTTOU */ NULL,
  471. };