db_exception.c 11 KB

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