db_exception.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. * db_signal.c
  15. *
  16. * This file contains APIs to set up handlers of exceptions issued by the
  17. * host, and the methods to pass the exceptions to the upcalls.
  18. */
  19. #include "pal_defs.h"
  20. #include "pal_linux_defs.h"
  21. #include "pal.h"
  22. #include "pal_internal.h"
  23. #include "pal_linux.h"
  24. #include "pal_debug.h"
  25. #include "pal_error.h"
  26. #include "pal_security.h"
  27. #include "api.h"
  28. #include <atomic.h>
  29. #include <sigset.h>
  30. #include <linux/signal.h>
  31. #include <ucontext.h>
  32. #include <asm/errno.h>
  33. #if !defined(__i386__)
  34. /* In x86_64 kernels, sigaction is required to have a user-defined
  35. * restorer. Also, they not yet support SA_INFO. The reference:
  36. * http://lxr.linux.no/linux+v2.6.35/arch/x86/kernel/signal.c#L448
  37. *
  38. * / * x86-64 should always use SA_RESTORER. * /
  39. * if (ka->sa.sa_flags & SA_RESTORER) {
  40. * put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
  41. * } else {
  42. * / * could use a vstub here * /
  43. * err |= -EFAULT;
  44. * }
  45. */
  46. #ifndef SA_RESTORER
  47. #define SA_RESTORER 0x04000000
  48. #endif
  49. #define DEFINE_RESTORE_RT(syscall) DEFINE_RESTORE_RT2(syscall)
  50. #define DEFINE_RESTORE_RT2(syscall) \
  51. __asm__ ( \
  52. " nop\n" \
  53. ".align 16\n" \
  54. ".LSTART_restore_rt:\n" \
  55. " .type __restore_rt,@function\n" \
  56. "__restore_rt:\n" \
  57. " movq $" #syscall ", %rax\n" \
  58. " syscall\n");
  59. DEFINE_RESTORE_RT(__NR_rt_sigreturn)
  60. /* Workaround for an old GAS (2.27) bug that incorrectly
  61. * omits relocations when referencing this symbol */
  62. __attribute__((visibility("hidden"))) void __restore_rt(void);
  63. #endif
  64. static const int async_signals[] =
  65. {
  66. SIGTERM,
  67. SIGINT,
  68. SIGCONT,
  69. };
  70. static const int nasync_signals = ARRAY_SIZE(async_signals);
  71. int set_sighandler (int * sigs, int nsig, void * handler)
  72. {
  73. struct sigaction action;
  74. if (handler) {
  75. action.sa_handler = (void (*)(int)) handler;
  76. action.sa_flags = SA_SIGINFO;
  77. #if !defined(__i386__)
  78. action.sa_flags |= SA_RESTORER;
  79. action.sa_restorer = __restore_rt;
  80. #endif
  81. } else {
  82. action.sa_flags = 0x0u;
  83. action.sa_handler = SIG_IGN;
  84. }
  85. #ifdef DEBUG
  86. if (!linux_state.in_gdb)
  87. #endif
  88. action.sa_flags |= SA_NOCLDWAIT;
  89. __sigemptyset((__sigset_t *) &action.sa_mask);
  90. __sigaddset((__sigset_t *) &action.sa_mask, SIGCONT);
  91. for (int i = 0 ; i < nsig ; i++) {
  92. #if defined(__i386__)
  93. int ret = INLINE_SYSCALL(sigaction, 3, sigs[i], &action, NULL)
  94. #else
  95. int ret = INLINE_SYSCALL(rt_sigaction, 4, sigs[i], &action, NULL,
  96. sizeof(sigset_t));
  97. #endif
  98. if (IS_ERR(ret))
  99. return -PAL_ERROR_DENIED;
  100. }
  101. int ret = 0;
  102. __sigset_t mask;
  103. __sigemptyset(&mask);
  104. for (int i = 0 ; i < nsig ; i++)
  105. __sigaddset(&mask, sigs[i]);
  106. #if defined(__i386__)
  107. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_UNBLOCK, &mask, NULL)
  108. #else
  109. ret = INLINE_SYSCALL(rt_sigprocmask, 4, SIG_UNBLOCK, &mask, NULL,
  110. sizeof(sigset_t));
  111. #endif
  112. if (IS_ERR(ret))
  113. return unix_to_pal_error(ERRNO(ret));
  114. return 0;
  115. }
  116. int block_signals (bool block, const int * sigs, int nsig)
  117. {
  118. int how = block? SIG_BLOCK: SIG_UNBLOCK;
  119. int ret = 0;
  120. __sigset_t mask;
  121. __sigemptyset(&mask);
  122. for (int i = 0 ; i < nsig ; i++)
  123. __sigaddset(&mask, sigs[i]);
  124. #if defined(__i386__)
  125. ret = INLINE_SYSCALL(sigprocmask, 3, how, &mask, NULL)
  126. #else
  127. ret = INLINE_SYSCALL(rt_sigprocmask, 4, how, &mask, NULL,
  128. sizeof(sigset_t));
  129. #endif
  130. if (IS_ERR(ret))
  131. return unix_to_pal_error(ERRNO(ret));
  132. return 0;
  133. }
  134. int block_async_signals (bool block)
  135. {
  136. return block_signals(block, async_signals, nasync_signals);
  137. }
  138. static int get_event_num (int signum)
  139. {
  140. switch(signum) {
  141. case SIGFPE: return PAL_EVENT_ARITHMETIC_ERROR;
  142. case SIGSEGV: case SIGBUS: return PAL_EVENT_MEMFAULT;
  143. case SIGILL: case SIGSYS: return PAL_EVENT_ILLEGAL;
  144. case SIGTERM: return PAL_EVENT_QUIT;
  145. case SIGINT: return PAL_EVENT_SUSPEND;
  146. case SIGCONT: return PAL_EVENT_RESUME;
  147. default: return -1;
  148. }
  149. }
  150. static void _DkGenericEventTrigger(PAL_EVENT_HANDLER upcall,
  151. PAL_NUM arg, ucontext_t* uc) {
  152. if (!uc) {
  153. (*upcall)(NULL, arg, NULL);
  154. return;
  155. }
  156. PAL_CONTEXT context;
  157. memcpy(&context, uc->uc_mcontext.gregs, sizeof(context));
  158. context.fpregs = (PAL_XREGS_STATE*)uc->uc_mcontext.fpregs;
  159. (*upcall)(NULL, arg, &context);
  160. /* copy the context back to ucontext */
  161. memcpy(uc->uc_mcontext.gregs, &context, sizeof(context));
  162. uc->uc_mcontext.fpregs = (struct _libc_fpstate*)context.fpregs;
  163. }
  164. static bool _DkGenericSignalHandle (int event_num, siginfo_t * info,
  165. ucontext_t * uc)
  166. {
  167. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(event_num);
  168. if (upcall) {
  169. PAL_NUM arg = 0;
  170. if (event_num == PAL_EVENT_ARITHMETIC_ERROR ||
  171. event_num == PAL_EVENT_MEMFAULT ||
  172. event_num == PAL_EVENT_ILLEGAL)
  173. arg = (PAL_NUM) (info ? info->si_addr : 0);
  174. _DkGenericEventTrigger(upcall, arg, uc);
  175. return true;
  176. }
  177. return false;
  178. }
  179. static void _DkGenericSighandler (int signum, siginfo_t * info,
  180. struct ucontext * uc)
  181. {
  182. int event_num = get_event_num(signum);
  183. if (event_num == -1)
  184. return;
  185. uintptr_t rip = uc->uc_mcontext.gregs[REG_RIP];
  186. if (ADDR_IN_PAL(rip)) {
  187. // We expect none of the memory faults, illegal instructions, or arithmetic exceptions
  188. // will happen in PAL. If these exceptions happen in PAL, exit the thread with loud warning.
  189. int pid = INLINE_SYSCALL(getpid, 0);
  190. int tid = INLINE_SYSCALL(gettid, 0);
  191. const char * name = "exception";
  192. switch(event_num) {
  193. case PAL_EVENT_ARITHMETIC_ERROR: name = "arithmetic exception"; break;
  194. case PAL_EVENT_MEMFAULT: name = "memory fault"; break;
  195. case PAL_EVENT_ILLEGAL: name = "illegal instruction"; break;
  196. }
  197. printf("*** An unexpected %s occurred inside PAL. Exiting the thread. "
  198. "(PID = %d, TID = %d, RIP = +0x%08lx) ***\n",
  199. name, pid, tid, rip - (uintptr_t) TEXT_START);
  200. #ifdef DEBUG
  201. // Hang for debugging
  202. while (true) {
  203. struct timespec sleeptime;
  204. sleeptime.tv_sec = 36000;
  205. sleeptime.tv_nsec = 0;
  206. INLINE_SYSCALL(nanosleep, 2, &sleeptime, NULL);
  207. }
  208. #endif
  209. _DkThreadExit(/*clear_child_tid=*/NULL);
  210. return;
  211. }
  212. _DkGenericSignalHandle(event_num, info, uc);
  213. }
  214. static void _DkTerminateSighandler (int signum, siginfo_t * info,
  215. struct ucontext * uc)
  216. {
  217. __UNUSED(info);
  218. int event_num = get_event_num(signum);
  219. if (event_num == -1)
  220. return;
  221. uintptr_t rip = uc->uc_mcontext.gregs[REG_RIP];
  222. // If the signal arrives in the middle of a PAL call, add the event
  223. // to pending in the current TCB.
  224. if (ADDR_IN_PAL(rip)) {
  225. PAL_TCB_LINUX * tcb = get_tcb_linux();
  226. assert(tcb);
  227. if (!tcb->pending_event) {
  228. // Use the preserved pending event slot
  229. tcb->pending_event = event_num;
  230. } else {
  231. // If there is already a pending event, add the new event to the queue.
  232. // (a relatively rare case.)
  233. struct event_queue * ev = malloc(sizeof(*ev));
  234. if (!ev)
  235. return;
  236. INIT_LIST_HEAD(ev, list);
  237. ev->event_num = event_num;
  238. LISTP_ADD_TAIL(ev, &tcb->pending_queue, list);
  239. }
  240. return;
  241. }
  242. // Call the event handler. If there is no handler, terminate the thread
  243. // unless it is a resuming event (then ignore the event).
  244. if (!_DkGenericSignalHandle(event_num, NULL, uc) && event_num != PAL_EVENT_RESUME)
  245. _DkThreadExit(/*clear_child_tid=*/NULL);
  246. }
  247. static void _DkPipeSighandler (int signum, siginfo_t * info,
  248. struct ucontext * uc)
  249. {
  250. __UNUSED(signum);
  251. __UNUSED(info);
  252. assert(signum == SIGPIPE);
  253. uintptr_t rip = uc->uc_mcontext.gregs[REG_RIP];
  254. __UNUSED(rip);
  255. assert(ADDR_IN_PAL(rip)); // This signal can only happens inside PAL
  256. return;
  257. }
  258. /*
  259. * __check_pending_event(): checks the existence of a pending event in the TCB
  260. * and handles the event consequently.
  261. */
  262. void __check_pending_event (void)
  263. {
  264. PAL_TCB_LINUX * tcb = get_tcb_linux();
  265. assert(tcb);
  266. if (tcb->pending_event) {
  267. int event = tcb->pending_event;
  268. tcb->pending_event = 0;
  269. _DkGenericSignalHandle(event, NULL, NULL);
  270. if (!LISTP_EMPTY(&tcb->pending_queue)) {
  271. // If there are more than one pending events, process them from the queue
  272. struct event_queue * ev, * n;
  273. LISTP_FOR_EACH_ENTRY_SAFE(ev, n, &tcb->pending_queue, list) {
  274. LISTP_DEL(ev, &tcb->pending_queue, list);
  275. _DkGenericSignalHandle(ev->event_num, NULL, NULL);
  276. free(ev);
  277. }
  278. }
  279. }
  280. }
  281. void _DkRaiseFailure(int error) {
  282. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(PAL_EVENT_FAILURE);
  283. if (upcall) {
  284. _DkGenericEventTrigger(upcall, error, NULL);
  285. }
  286. }
  287. struct signal_ops {
  288. int signum[3];
  289. void (*handler) (int signum, siginfo_t * info, ucontext_t * uc);
  290. };
  291. struct signal_ops on_signals[] = {
  292. [PAL_EVENT_ARITHMETIC_ERROR] = { .signum = { SIGFPE, 0 },
  293. .handler = _DkGenericSighandler },
  294. [PAL_EVENT_MEMFAULT] = { .signum = { SIGSEGV, SIGBUS, 0 },
  295. .handler = _DkGenericSighandler },
  296. [PAL_EVENT_ILLEGAL] = { .signum = { SIGILL, SIGSYS, 0 },
  297. .handler = _DkGenericSighandler },
  298. [PAL_EVENT_QUIT] = { .signum = { SIGTERM, 0, 0 },
  299. .handler = _DkTerminateSighandler },
  300. [PAL_EVENT_SUSPEND] = { .signum = { SIGINT, 0 },
  301. .handler = _DkTerminateSighandler },
  302. [PAL_EVENT_RESUME] = { .signum = { SIGCONT, 0 },
  303. .handler = _DkTerminateSighandler },
  304. };
  305. static int _DkPersistentSighandlerSetup (int event_num)
  306. {
  307. int nsigs, * sigs = on_signals[event_num].signum;
  308. for (nsigs = 0 ; sigs[nsigs] ; nsigs++);
  309. int ret = set_sighandler(sigs, nsigs, on_signals[event_num].handler);
  310. if (ret < 0)
  311. return ret;
  312. return 0;
  313. }
  314. void signal_setup (void)
  315. {
  316. int ret, sig = SIGCHLD;
  317. #ifdef DEBUG
  318. if (!linux_state.in_gdb)
  319. #endif
  320. set_sighandler(&sig, 1, NULL);
  321. sig = SIGPIPE;
  322. if ((ret = set_sighandler(&sig, 1, &_DkPipeSighandler)) < 0)
  323. goto err;
  324. int events[] = {
  325. PAL_EVENT_ARITHMETIC_ERROR,
  326. PAL_EVENT_MEMFAULT,
  327. PAL_EVENT_ILLEGAL,
  328. PAL_EVENT_QUIT,
  329. PAL_EVENT_SUSPEND,
  330. PAL_EVENT_RESUME,
  331. };
  332. for (size_t e = 0; e < ARRAY_SIZE(events); e++)
  333. if ((ret = _DkPersistentSighandlerSetup(events[e])) < 0)
  334. goto err;
  335. return;
  336. err:
  337. INIT_FAIL(-ret, "cannot setup signal handlers");
  338. }
  339. void _DkExceptionReturn(void* event) {
  340. __UNUSED(event);
  341. }