db_exception.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. typedef struct {
  139. PAL_IDX event_num;
  140. PAL_CONTEXT context;
  141. ucontext_t * uc;
  142. } PAL_EVENT;
  143. static int get_event_num (int signum)
  144. {
  145. switch(signum) {
  146. case SIGFPE: return PAL_EVENT_ARITHMETIC_ERROR;
  147. case SIGSEGV: case SIGBUS: return PAL_EVENT_MEMFAULT;
  148. case SIGILL: case SIGSYS: return PAL_EVENT_ILLEGAL;
  149. case SIGTERM: return PAL_EVENT_QUIT;
  150. case SIGINT: return PAL_EVENT_SUSPEND;
  151. case SIGCONT: return PAL_EVENT_RESUME;
  152. default: return -1;
  153. }
  154. }
  155. static void _DkGenericEventTrigger (PAL_IDX event_num, PAL_EVENT_HANDLER upcall,
  156. PAL_NUM arg, ucontext_t * uc)
  157. {
  158. PAL_EVENT event;
  159. event.event_num = event_num;
  160. if (uc)
  161. memcpy(&event.context, uc->uc_mcontext.gregs, sizeof(PAL_CONTEXT));
  162. event.uc = uc;
  163. (*upcall) ((PAL_PTR) &event, arg, &event.context);
  164. }
  165. static bool _DkGenericSignalHandle (int event_num, siginfo_t * info,
  166. ucontext_t * uc)
  167. {
  168. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(event_num);
  169. if (upcall) {
  170. PAL_NUM arg = 0;
  171. if (event_num == PAL_EVENT_ARITHMETIC_ERROR ||
  172. event_num == PAL_EVENT_MEMFAULT ||
  173. event_num == PAL_EVENT_ILLEGAL)
  174. arg = (PAL_NUM) (info ? info->si_addr : 0);
  175. _DkGenericEventTrigger(event_num, upcall, arg, uc);
  176. return true;
  177. }
  178. return false;
  179. }
  180. static void _DkGenericSighandler (int signum, siginfo_t * info,
  181. struct ucontext * uc)
  182. {
  183. int event_num = get_event_num(signum);
  184. if (event_num == -1)
  185. return;
  186. uintptr_t rip = uc->uc_mcontext.gregs[REG_RIP];
  187. if (ADDR_IN_PAL(rip)) {
  188. // We expect none of the memory faults, illegal instructions, or arithmetic exceptions
  189. // will happen in PAL. If these exceptions happen in PAL, exit the thread with loud warning.
  190. int pid = INLINE_SYSCALL(getpid, 0);
  191. int tid = INLINE_SYSCALL(gettid, 0);
  192. const char * name = "exception";
  193. switch(event_num) {
  194. case PAL_EVENT_ARITHMETIC_ERROR: name = "arithmetic exception"; break;
  195. case PAL_EVENT_MEMFAULT: name = "memory fault"; break;
  196. case PAL_EVENT_ILLEGAL: name = "illegal instruction"; break;
  197. }
  198. printf("*** An unexpected %s occurred inside PAL. Exiting the thread. "
  199. "(PID = %d, TID = %d, RIP = +0x%08lx) ***\n",
  200. name, pid, tid, rip - (uintptr_t) TEXT_START);
  201. #ifdef DEBUG
  202. // Hang for debugging
  203. while (true) {
  204. struct timespec sleeptime;
  205. sleeptime.tv_sec = 36000;
  206. sleeptime.tv_nsec = 0;
  207. INLINE_SYSCALL(nanosleep, 2, &sleeptime, NULL);
  208. }
  209. #endif
  210. _DkThreadExit();
  211. return;
  212. }
  213. _DkGenericSignalHandle(event_num, info, uc);
  214. }
  215. static void _DkTerminateSighandler (int signum, siginfo_t * info,
  216. struct ucontext * uc)
  217. {
  218. __UNUSED(info);
  219. int event_num = get_event_num(signum);
  220. if (event_num == -1)
  221. return;
  222. uintptr_t rip = uc->uc_mcontext.gregs[REG_RIP];
  223. // If the signal arrives in the middle of a PAL call, add the event
  224. // to pending in the current TCB.
  225. if (ADDR_IN_PAL(rip)) {
  226. PAL_TCB_LINUX * tcb = get_tcb_linux();
  227. assert(tcb);
  228. if (!tcb->pending_event) {
  229. // Use the preserved pending event slot
  230. tcb->pending_event = event_num;
  231. } else {
  232. // If there is already a pending event, add the new event to the queue.
  233. // (a relatively rare case.)
  234. struct event_queue * ev = malloc(sizeof(*ev));
  235. if (!ev)
  236. return;
  237. INIT_LIST_HEAD(ev, list);
  238. ev->event_num = event_num;
  239. LISTP_ADD_TAIL(ev, &tcb->pending_queue, list);
  240. }
  241. return;
  242. }
  243. // Call the event handler. If there is no handler, terminate the thread
  244. // unless it is a resuming event (then ignore the event).
  245. if (!_DkGenericSignalHandle(event_num, NULL, uc) && event_num != PAL_EVENT_RESUME)
  246. _DkThreadExit();
  247. }
  248. static void _DkPipeSighandler (int signum, siginfo_t * info,
  249. struct ucontext * uc)
  250. {
  251. __UNUSED(signum);
  252. __UNUSED(info);
  253. assert(signum == SIGPIPE);
  254. uintptr_t rip = uc->uc_mcontext.gregs[REG_RIP];
  255. __UNUSED(rip);
  256. assert(ADDR_IN_PAL(rip)); // This signal can only happens inside PAL
  257. return;
  258. }
  259. /*
  260. * __check_pending_event(): checks the existence of a pending event in the TCB
  261. * and handles the event consequently.
  262. */
  263. void __check_pending_event (void)
  264. {
  265. PAL_TCB_LINUX * tcb = get_tcb_linux();
  266. assert(tcb);
  267. if (tcb->pending_event) {
  268. int event = tcb->pending_event;
  269. tcb->pending_event = 0;
  270. _DkGenericSignalHandle(event, NULL, NULL);
  271. if (!LISTP_EMPTY(&tcb->pending_queue)) {
  272. // If there are more than one pending events, process them from the queue
  273. struct event_queue * ev, * n;
  274. LISTP_FOR_EACH_ENTRY_SAFE(ev, n, &tcb->pending_queue, list) {
  275. LISTP_DEL(ev, &tcb->pending_queue, list);
  276. _DkGenericSignalHandle(ev->event_num, NULL, NULL);
  277. free(ev);
  278. }
  279. }
  280. }
  281. }
  282. void _DkRaiseFailure (int error)
  283. {
  284. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(PAL_EVENT_FAILURE);
  285. if (!upcall)
  286. return;
  287. PAL_EVENT event;
  288. event.event_num = PAL_EVENT_FAILURE;
  289. event.uc = NULL;
  290. (*upcall) ((PAL_PTR) &event, error, NULL);
  291. }
  292. struct signal_ops {
  293. int signum[3];
  294. void (*handler) (int signum, siginfo_t * info, ucontext_t * uc);
  295. };
  296. struct signal_ops on_signals[] = {
  297. [PAL_EVENT_ARITHMETIC_ERROR] = { .signum = { SIGFPE, 0 },
  298. .handler = _DkGenericSighandler },
  299. [PAL_EVENT_MEMFAULT] = { .signum = { SIGSEGV, SIGBUS, 0 },
  300. .handler = _DkGenericSighandler },
  301. [PAL_EVENT_ILLEGAL] = { .signum = { SIGILL, SIGSYS, 0 },
  302. .handler = _DkGenericSighandler },
  303. [PAL_EVENT_QUIT] = { .signum = { SIGTERM, 0, 0 },
  304. .handler = _DkTerminateSighandler },
  305. [PAL_EVENT_SUSPEND] = { .signum = { SIGINT, 0 },
  306. .handler = _DkTerminateSighandler },
  307. [PAL_EVENT_RESUME] = { .signum = { SIGCONT, 0 },
  308. .handler = _DkTerminateSighandler },
  309. };
  310. static int _DkPersistentSighandlerSetup (int event_num)
  311. {
  312. int nsigs, * sigs = on_signals[event_num].signum;
  313. for (nsigs = 0 ; sigs[nsigs] ; nsigs++);
  314. int ret = set_sighandler(sigs, nsigs, on_signals[event_num].handler);
  315. if (ret < 0)
  316. return ret;
  317. return 0;
  318. }
  319. void signal_setup (void)
  320. {
  321. int ret, sig = SIGCHLD;
  322. #ifdef DEBUG
  323. if (!linux_state.in_gdb)
  324. #endif
  325. set_sighandler(&sig, 1, NULL);
  326. sig = SIGPIPE;
  327. if ((ret = set_sighandler(&sig, 1, &_DkPipeSighandler)) < 0)
  328. goto err;
  329. int events[] = {
  330. PAL_EVENT_ARITHMETIC_ERROR,
  331. PAL_EVENT_MEMFAULT,
  332. PAL_EVENT_ILLEGAL,
  333. PAL_EVENT_QUIT,
  334. PAL_EVENT_SUSPEND,
  335. PAL_EVENT_RESUME,
  336. };
  337. for (size_t e = 0; e < ARRAY_SIZE(events); e++)
  338. if ((ret = _DkPersistentSighandlerSetup(events[e])) < 0)
  339. goto err;
  340. return;
  341. err:
  342. INIT_FAIL(-ret, "cannot setup signal handlers");
  343. }
  344. void _DkExceptionReturn (void * event)
  345. {
  346. PAL_EVENT * e = event;
  347. if (e->uc) {
  348. /* copy the context back to ucontext */
  349. memcpy(e->uc->uc_mcontext.gregs, &e->context, sizeof(PAL_CONTEXT));
  350. }
  351. }