db_exception.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. #ifndef SA_RESTORER
  49. #define SA_RESTORER 0x04000000
  50. #endif
  51. #define DEFINE_RESTORE_RT(syscall) DEFINE_RESTORE_RT2(syscall)
  52. #define DEFINE_RESTORE_RT2(syscall) \
  53. __asm__ ( \
  54. " nop\n" \
  55. ".align 16\n" \
  56. ".LSTART_restore_rt:\n" \
  57. " .type __restore_rt,@function\n" \
  58. "__restore_rt:\n" \
  59. " movq $" #syscall ", %rax\n" \
  60. " syscall\n");
  61. DEFINE_RESTORE_RT(__NR_rt_sigreturn)
  62. /* Workaround for an old GAS (2.27) bug that incorrectly
  63. * omits relocations when referencing this symbol */
  64. __attribute__((visibility("hidden"))) void __restore_rt(void);
  65. #endif
  66. int set_sighandler (int * sigs, int nsig, void * handler)
  67. {
  68. struct sigaction action;
  69. if (handler) {
  70. action.sa_handler = (void (*)(int)) handler;
  71. action.sa_flags = SA_SIGINFO;
  72. #if !defined(__i386__)
  73. action.sa_flags |= SA_RESTORER;
  74. action.sa_restorer = __restore_rt;
  75. #endif
  76. } else {
  77. action.sa_flags = 0x0u;
  78. action.sa_handler = SIG_IGN;
  79. }
  80. #ifdef DEBUG
  81. if (!linux_state.in_gdb)
  82. #endif
  83. action.sa_flags |= SA_NOCLDWAIT;
  84. __sigemptyset((__sigset_t *) &action.sa_mask);
  85. __sigaddset((__sigset_t *) &action.sa_mask, SIGCONT);
  86. for (int i = 0 ; i < nsig ; i++) {
  87. #if defined(__i386__)
  88. int ret = INLINE_SYSCALL(sigaction, 3, sigs[i], &action, NULL)
  89. #else
  90. int ret = INLINE_SYSCALL(rt_sigaction, 4, sigs[i], &action, NULL,
  91. sizeof(sigset_t));
  92. #endif
  93. if (IS_ERR(ret))
  94. return -PAL_ERROR_DENIED;
  95. }
  96. return 0;
  97. }
  98. typedef struct {
  99. PAL_IDX event_num;
  100. PAL_CONTEXT context;
  101. ucontext_t * uc;
  102. } PAL_EVENT;
  103. static int get_event_num (int signum)
  104. {
  105. switch(signum) {
  106. case SIGFPE: return PAL_EVENT_ARITHMETIC_ERROR;
  107. case SIGSEGV: case SIGBUS: return PAL_EVENT_MEMFAULT;
  108. case SIGILL: case SIGSYS: return PAL_EVENT_ILLEGAL;
  109. case SIGTERM: return PAL_EVENT_QUIT;
  110. case SIGINT: return PAL_EVENT_SUSPEND;
  111. case SIGCONT: return PAL_EVENT_RESUME;
  112. default: return -1;
  113. }
  114. }
  115. static void _DkGenericEventTrigger (PAL_IDX event_num, PAL_EVENT_HANDLER upcall,
  116. PAL_NUM arg, ucontext_t * uc)
  117. {
  118. PAL_EVENT event;
  119. event.event_num = event_num;
  120. if (uc)
  121. memcpy(&event.context, uc->uc_mcontext.gregs, sizeof(PAL_CONTEXT));
  122. event.uc = uc;
  123. (*upcall) ((PAL_PTR) &event, arg, &event.context);
  124. }
  125. static bool _DkGenericSignalHandle (int event_num, siginfo_t * info,
  126. ucontext_t * uc)
  127. {
  128. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(event_num);
  129. if (upcall) {
  130. PAL_NUM arg = 0;
  131. if (event_num == PAL_EVENT_ARITHMETIC_ERROR ||
  132. event_num == PAL_EVENT_MEMFAULT ||
  133. event_num == PAL_EVENT_ILLEGAL)
  134. arg = (PAL_NUM) (info ? info->si_addr : 0);
  135. _DkGenericEventTrigger(event_num, upcall, arg, uc);
  136. return true;
  137. }
  138. return false;
  139. }
  140. static void _DkGenericSighandler (int signum, siginfo_t * info,
  141. struct ucontext * uc)
  142. {
  143. int event_num = get_event_num(signum);
  144. if (event_num == -1)
  145. return;
  146. uintptr_t rip = uc->uc_mcontext.gregs[REG_RIP];
  147. if (ADDR_IN_PAL(rip)) {
  148. // We expect none of the memory faults, illegal instructions, or arithmetic exceptions
  149. // will happen in PAL. If these exceptions happen in PAL, exit the thread with loud warning.
  150. int pid = INLINE_SYSCALL(getpid, 0);
  151. int tid = INLINE_SYSCALL(gettid, 0);
  152. const char * name = "exception";
  153. switch(event_num) {
  154. case PAL_EVENT_ARITHMETIC_ERROR: name = "arithmetic exception"; break;
  155. case PAL_EVENT_MEMFAULT: name = "memory fault"; break;
  156. case PAL_EVENT_ILLEGAL: name = "illegal instruction"; break;
  157. }
  158. printf("*** An unexpected %s occurred inside PAL. Exiting the thread. "
  159. "(PID = %d, TID = %d, RIP = +0x%08lx) ***\n",
  160. name, pid, tid, rip - (uintptr_t) TEXT_START);
  161. #ifdef DEBUG
  162. // Hang for debugging
  163. while (true) {
  164. struct timespec sleeptime;
  165. sleeptime.tv_sec = 36000;
  166. sleeptime.tv_nsec = 0;
  167. INLINE_SYSCALL(nanosleep, 2, &sleeptime, NULL);
  168. }
  169. #endif
  170. _DkThreadExit();
  171. return;
  172. }
  173. _DkGenericSignalHandle(event_num, info, uc);
  174. }
  175. static void _DkTerminateSighandler (int signum, siginfo_t * info,
  176. struct ucontext * uc)
  177. {
  178. __UNUSED(info);
  179. int event_num = get_event_num(signum);
  180. if (event_num == -1)
  181. return;
  182. uintptr_t rip = uc->uc_mcontext.gregs[REG_RIP];
  183. // If the signal arrives in the middle of a PAL call, add the event
  184. // to pending in the current TCB.
  185. if (ADDR_IN_PAL(rip)) {
  186. PAL_TCB * tcb = get_tcb();
  187. assert(tcb);
  188. if (!tcb->pending_event) {
  189. // Use the preserved pending event slot
  190. tcb->pending_event = event_num;
  191. } else {
  192. // If there is already a pending event, add the new event to the queue.
  193. // (a relatively rare case.)
  194. struct event_queue * ev = malloc(sizeof(*ev));
  195. if (!ev)
  196. return;
  197. INIT_LIST_HEAD(ev, list);
  198. ev->event_num = event_num;
  199. LISTP_ADD_TAIL(ev, &tcb->pending_queue, list);
  200. }
  201. return;
  202. }
  203. // Call the event handler. If there is no handler, terminate the thread
  204. // unless it is a resuming event (then ignore the event).
  205. if (!_DkGenericSignalHandle(event_num, NULL, uc) && event_num != PAL_EVENT_RESUME)
  206. _DkThreadExit();
  207. }
  208. static void _DkPipeSighandler (int signum, siginfo_t * info,
  209. struct ucontext * uc)
  210. {
  211. assert(signum == SIGPIPE);
  212. __UNUSED(info);
  213. uintptr_t rip = uc->uc_mcontext.gregs[REG_RIP];
  214. assert(ADDR_IN_PAL(rip)); // This signal can only happens inside PAL
  215. return;
  216. }
  217. /*
  218. * __check_pending_event(): checks the existence of a pending event in the TCB
  219. * and handles the event consequently.
  220. */
  221. void __check_pending_event (void)
  222. {
  223. PAL_TCB * tcb = get_tcb();
  224. assert(tcb);
  225. if (tcb->pending_event) {
  226. int event = tcb->pending_event;
  227. tcb->pending_event = 0;
  228. _DkGenericSignalHandle(event, NULL, NULL);
  229. if (!LISTP_EMPTY(&tcb->pending_queue)) {
  230. // If there are more than one pending events, process them from the queue
  231. struct event_queue * ev, * n;
  232. LISTP_FOR_EACH_ENTRY_SAFE(ev, n, &tcb->pending_queue, list) {
  233. LISTP_DEL(ev, &tcb->pending_queue, list);
  234. _DkGenericSignalHandle(ev->event_num, NULL, NULL);
  235. free(ev);
  236. }
  237. }
  238. }
  239. }
  240. void _DkRaiseFailure (int error)
  241. {
  242. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(PAL_EVENT_FAILURE);
  243. if (!upcall)
  244. return;
  245. PAL_EVENT event;
  246. event.event_num = PAL_EVENT_FAILURE;
  247. event.uc = NULL;
  248. (*upcall) ((PAL_PTR) &event, error, NULL);
  249. }
  250. struct signal_ops {
  251. int signum[3];
  252. void (*handler) (int signum, siginfo_t * info, ucontext_t * uc);
  253. };
  254. struct signal_ops on_signals[] = {
  255. [PAL_EVENT_ARITHMETIC_ERROR] = { .signum = { SIGFPE, 0 },
  256. .handler = _DkGenericSighandler },
  257. [PAL_EVENT_MEMFAULT] = { .signum = { SIGSEGV, SIGBUS, 0 },
  258. .handler = _DkGenericSighandler },
  259. [PAL_EVENT_ILLEGAL] = { .signum = { SIGILL, SIGSYS, 0 },
  260. .handler = _DkGenericSighandler },
  261. [PAL_EVENT_QUIT] = { .signum = { SIGTERM, 0, 0 },
  262. .handler = _DkTerminateSighandler },
  263. [PAL_EVENT_SUSPEND] = { .signum = { SIGINT, 0 },
  264. .handler = _DkTerminateSighandler },
  265. [PAL_EVENT_RESUME] = { .signum = { SIGCONT, 0 },
  266. .handler = _DkTerminateSighandler },
  267. };
  268. static int _DkPersistentSighandlerSetup (int event_num)
  269. {
  270. int nsigs, * sigs = on_signals[event_num].signum;
  271. for (nsigs = 0 ; sigs[nsigs] ; nsigs++);
  272. int ret = set_sighandler(sigs, nsigs, on_signals[event_num].handler);
  273. if (ret < 0)
  274. return ret;
  275. return 0;
  276. }
  277. void signal_setup (void)
  278. {
  279. int ret, sig = SIGCHLD;
  280. #ifdef DEBUG
  281. if (!linux_state.in_gdb)
  282. #endif
  283. set_sighandler(&sig, 1, NULL);
  284. sig = SIGPIPE;
  285. if ((ret = set_sighandler(&sig, 1, &_DkPipeSighandler)) < 0)
  286. goto err;
  287. int events[] = {
  288. PAL_EVENT_ARITHMETIC_ERROR,
  289. PAL_EVENT_MEMFAULT,
  290. PAL_EVENT_ILLEGAL,
  291. PAL_EVENT_QUIT,
  292. PAL_EVENT_SUSPEND,
  293. PAL_EVENT_RESUME,
  294. };
  295. for (int e = 0 ; e < sizeof(events) / sizeof(events[0]) ; e++)
  296. if ((ret = _DkPersistentSighandlerSetup(events[e])) < 0)
  297. goto err;
  298. return;
  299. err:
  300. INIT_FAIL(-ret, "cannot setup signal handlers");
  301. }
  302. void _DkExceptionReturn (void * event)
  303. {
  304. PAL_EVENT * e = event;
  305. if (e->uc) {
  306. /* copy the context back to ucontext */
  307. memcpy(e->uc->uc_mcontext.gregs, &e->context, sizeof(PAL_CONTEXT));
  308. }
  309. }