db_exception.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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_error.h"
  27. #include "pal_security.h"
  28. #include "api.h"
  29. #include <atomic.h>
  30. #include <sigset.h>
  31. #include <linux/signal.h>
  32. #include <ucontext.h>
  33. #include <asm/errno.h>
  34. #if !defined(__i386__)
  35. /* In x86_64 kernels, sigaction is required to have a user-defined
  36. * restorer. Also, they not yet support SA_INFO. The reference:
  37. * http://lxr.linux.no/linux+v2.6.35/arch/x86/kernel/signal.c#L448
  38. *
  39. * / * x86-64 should always use SA_RESTORER. * /
  40. * if (ka->sa.sa_flags & SA_RESTORER) {
  41. * put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
  42. * } else {
  43. * / * could use a vstub here * /
  44. * err |= -EFAULT;
  45. * }
  46. */
  47. void restore_rt (void) asm ("__restore_rt");
  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. #endif
  63. int set_sighandler (int * sigs, int nsig, void * handler)
  64. {
  65. struct sigaction action;
  66. if (handler) {
  67. action.sa_handler = (void (*)(int)) handler;
  68. action.sa_flags = SA_SIGINFO;
  69. #if !defined(__i386__)
  70. action.sa_flags |= SA_RESTORER;
  71. action.sa_restorer = restore_rt;
  72. #endif
  73. } else {
  74. action.sa_handler = SIG_IGN;
  75. }
  76. #ifdef DEBUG
  77. if (!linux_state.in_gdb)
  78. #endif
  79. action.sa_flags |= SA_NOCLDWAIT;
  80. __sigemptyset((__sigset_t *) &action.sa_mask);
  81. __sigaddset((__sigset_t *) &action.sa_mask, SIGCONT);
  82. for (int i = 0 ; i < nsig ; i++) {
  83. #if defined(__i386__)
  84. int ret = INLINE_SYSCALL(sigaction, 3, sigs[i], &action, NULL)
  85. #else
  86. int ret = INLINE_SYSCALL(rt_sigaction, 4, sigs[i], &action, NULL,
  87. sizeof(sigset_t));
  88. #endif
  89. if (IS_ERR(ret))
  90. return -PAL_ERROR_DENIED;
  91. }
  92. return 0;
  93. }
  94. typedef struct {
  95. PAL_IDX event_num;
  96. PAL_CONTEXT context;
  97. ucontext_t * uc;
  98. PAL_PTR eframe;
  99. } PAL_EVENT;
  100. #define SIGNAL_MASK_TIME 1000
  101. #define save_return_point(ptr) \
  102. asm volatile ("leaq 0(%%rip), %%rax\r\n" \
  103. "movq %%rax, %0\r\n" \
  104. : "=b"(ptr) :: "memory", "rax")
  105. static int get_event_num (int signum)
  106. {
  107. switch(signum) {
  108. case SIGFPE: return PAL_EVENT_DIVZERO;
  109. case SIGSEGV: case SIGBUS: return PAL_EVENT_MEMFAULT;
  110. case SIGILL: case SIGSYS: return PAL_EVENT_ILLEGAL;
  111. case SIGTERM: return PAL_EVENT_QUIT;
  112. case SIGINT: return PAL_EVENT_SUSPEND;
  113. case SIGCONT: return PAL_EVENT_RESUME;
  114. default: return -1;
  115. }
  116. }
  117. void _DkGenericEventTrigger (PAL_IDX event_num, PAL_EVENT_HANDLER upcall,
  118. PAL_NUM arg, struct pal_frame * frame,
  119. ucontext_t * uc, void * eframe)
  120. {
  121. PAL_EVENT event;
  122. event.event_num = event_num;
  123. if (uc)
  124. memcpy(&event.context, uc->uc_mcontext.gregs, sizeof(PAL_CONTEXT));
  125. if (frame) {
  126. event.context.r15 = frame->arch.r15;
  127. event.context.r14 = frame->arch.r14;
  128. event.context.r13 = frame->arch.r13;
  129. event.context.r12 = frame->arch.r12;
  130. event.context.rdi = frame->arch.rdi;
  131. event.context.rsi = frame->arch.rsi;
  132. event.context.rbx = frame->arch.rbx;
  133. /* find last frame */
  134. event.context.rsp = frame->arch.rbp + sizeof(unsigned long) * 2;
  135. event.context.rbp = ((unsigned long *) frame->arch.rbp)[0];
  136. event.context.rip = ((unsigned long *) frame->arch.rbp)[1];
  137. /* making rax = 0 to tell the caller that this PAL call failed */
  138. event.context.rax = 0;
  139. }
  140. event.uc = uc;
  141. event.eframe = eframe;
  142. (*upcall) ((PAL_PTR) &event, arg, &event.context);
  143. }
  144. static bool _DkGenericSignalHandle (int event_num, siginfo_t * info,
  145. struct pal_frame * frame,
  146. ucontext_t * uc, void * eframe)
  147. {
  148. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(event_num);
  149. if (upcall) {
  150. PAL_NUM arg = 0;
  151. if (event_num == PAL_EVENT_DIVZERO ||
  152. event_num == PAL_EVENT_MEMFAULT ||
  153. event_num == PAL_EVENT_ILLEGAL)
  154. arg = (PAL_NUM) (info ? info->si_addr : 0);
  155. _DkGenericEventTrigger(event_num, upcall, arg, frame, uc, eframe);
  156. return true;
  157. }
  158. return false;
  159. }
  160. #define ADDR_IN_PAL(addr) \
  161. ((void *) (addr) > TEXT_START && (void *) (addr) < TEXT_END)
  162. static struct pal_frame * get_frame (ucontext_t * uc)
  163. {
  164. unsigned long rip = uc->uc_mcontext.gregs[REG_RIP];
  165. unsigned long rbp = uc->uc_mcontext.gregs[REG_RBP];
  166. unsigned long last_rbp = rbp - 64;
  167. if (!ADDR_IN_PAL(rip))
  168. return NULL;
  169. while (ADDR_IN_PAL(((unsigned long *) rbp)[1])) {
  170. last_rbp = rbp;
  171. rbp = *(unsigned long *) rbp;
  172. }
  173. /* search frame record in the top frame of PAL */
  174. for (unsigned long ptr = rbp - sizeof(unsigned long) ;
  175. ptr > last_rbp ; ptr -= 8) {
  176. struct pal_frame * frame = (struct pal_frame *) ptr;
  177. if (frame->identifier == PAL_FRAME_IDENTIFIER)
  178. return frame;
  179. }
  180. return NULL;
  181. }
  182. static void return_frame (struct pal_frame * frame, int err)
  183. {
  184. if (err)
  185. _DkRaiseFailure(err);
  186. __clear_frame(frame);
  187. arch_restore_frame(&frame->arch);
  188. asm volatile ("xor %rax, %rax\r\n"
  189. "leaveq\r\n"
  190. "retq\r\n");
  191. }
  192. #if BLOCK_SIGFAULT == 1
  193. static char exception_msg[24] = "--- SIGSEGV --- [ ]\n";
  194. static volatile bool cont_exec = false;
  195. #endif
  196. static void _DkGenericSighandler (int signum, siginfo_t * info,
  197. struct ucontext * uc)
  198. {
  199. #if BLOCK_SIGFUALT == 1
  200. /* reseurrect this code if signal handler if giving segmentation fault */
  201. if (signum == SIGSEGV) {
  202. int pid = INLINE_SYSCALL(getpid, 0);
  203. exception_msg[17] = '0' + pid / 10000;
  204. exception_msg[18] = '0' + (pid / 1000) % 10;
  205. exception_msg[19] = '0' + (pid / 100) % 10;
  206. exception_msg[20] = '0' + (pid / 10) % 10;
  207. exception_msg[21] = '0' + pid % 10;
  208. INLINE_SYSCALL(write, 3, 1, exception_msg, 24);
  209. while(!cont_exec);
  210. }
  211. #endif
  212. struct pal_frame * frame = get_frame(uc);
  213. void * eframe;
  214. if (signum == SIGCONT && frame && frame->func == DkObjectsWaitAny)
  215. return;
  216. asm volatile ("movq %%rbp, %0" : "=r"(eframe));
  217. if (frame && frame->func != &_DkGenericSighandler &&
  218. signum != SIGCONT &&
  219. signum != SIGINT &&
  220. signum != SIGTERM) {
  221. return_frame(frame, PAL_ERROR_BADADDR);
  222. return;
  223. }
  224. int event_num = get_event_num(signum);
  225. if (event_num == -1)
  226. return;
  227. _DkGenericSignalHandle(event_num, info, frame, uc, eframe);
  228. }
  229. static void _DkTerminateSighandler (int signum, siginfo_t * info,
  230. struct ucontext * uc)
  231. {
  232. struct pal_frame * frame = get_frame(uc);
  233. void * eframe;
  234. asm volatile ("movq %%rbp, %0" : "=r"(eframe));
  235. int event_num = get_event_num(signum);
  236. if (event_num == -1)
  237. return;
  238. if (!_DkGenericSignalHandle(event_num, NULL, frame, uc, eframe))
  239. _DkThreadExit();
  240. }
  241. static void _DkPipeSighandler (int signum, siginfo_t * info,
  242. struct ucontext * uc)
  243. {
  244. return;
  245. }
  246. void _DkRaiseFailure (int error)
  247. {
  248. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(PAL_EVENT_FAILURE);
  249. if (!upcall)
  250. return;
  251. PAL_EVENT event;
  252. event.event_num = PAL_EVENT_FAILURE;
  253. event.uc = NULL;
  254. event.eframe = NULL;
  255. (*upcall) ((PAL_PTR) &event, error, NULL);
  256. }
  257. struct signal_ops {
  258. int signum[3];
  259. void (*handler) (int signum, siginfo_t * info, ucontext_t * uc);
  260. };
  261. struct signal_ops on_signals[] = {
  262. [PAL_EVENT_DIVZERO] = { .signum = { SIGFPE, 0 },
  263. .handler = _DkGenericSighandler },
  264. [PAL_EVENT_MEMFAULT] = { .signum = { SIGSEGV, SIGBUS, 0 },
  265. .handler = _DkGenericSighandler },
  266. [PAL_EVENT_ILLEGAL] = { .signum = { SIGILL, SIGSYS, 0 },
  267. .handler = _DkGenericSighandler },
  268. [PAL_EVENT_QUIT] = { .signum = { SIGTERM, 0, 0 },
  269. .handler = _DkTerminateSighandler },
  270. [PAL_EVENT_SUSPEND] = { .signum = { SIGINT, 0 },
  271. .handler = _DkTerminateSighandler },
  272. [PAL_EVENT_RESUME] = { .signum = { SIGCONT, 0 },
  273. .handler = _DkGenericSighandler },
  274. };
  275. static int _DkPersistentSighandlerSetup (int event_num)
  276. {
  277. int nsigs, * sigs = on_signals[event_num].signum;
  278. for (nsigs = 0 ; sigs[nsigs] ; nsigs++);
  279. int ret = set_sighandler(sigs, nsigs, on_signals[event_num].handler);
  280. if (ret < 0)
  281. return ret;
  282. return 0;
  283. }
  284. void signal_setup (void)
  285. {
  286. int ret, sig = SIGCHLD;
  287. #ifdef DEBUG
  288. if (!linux_state.in_gdb)
  289. #endif
  290. set_sighandler(&sig, 1, NULL);
  291. sig = SIGPIPE;
  292. if ((ret = set_sighandler(&sig, 1, &_DkPipeSighandler)) < 0)
  293. goto err;
  294. int events[] = {
  295. PAL_EVENT_DIVZERO,
  296. PAL_EVENT_MEMFAULT,
  297. PAL_EVENT_ILLEGAL,
  298. PAL_EVENT_QUIT,
  299. PAL_EVENT_SUSPEND,
  300. PAL_EVENT_RESUME,
  301. };
  302. for (int e = 0 ; e < sizeof(events) / sizeof(events[0]) ; e++)
  303. if ((ret = _DkPersistentSighandlerSetup(events[e])) < 0)
  304. goto err;
  305. return;
  306. err:
  307. init_fail(-ret, "cannot setup signal handlers");
  308. }
  309. void _DkExceptionReturn (void * event)
  310. {
  311. PAL_EVENT * e = event;
  312. if (e->eframe) {
  313. struct pal_frame * frame = (struct pal_frame *) e->eframe;
  314. int err = 0;
  315. switch (e->event_num) {
  316. case PAL_EVENT_MEMFAULT:
  317. err = PAL_ERROR_BADADDR;
  318. break;
  319. case PAL_EVENT_QUIT:
  320. case PAL_EVENT_SUSPEND:
  321. case PAL_EVENT_RESUME:
  322. err = PAL_ERROR_INTERRUPTED;
  323. break;
  324. }
  325. if (err)
  326. _DkRaiseFailure(err);
  327. __clear_frame(frame);
  328. }
  329. if (e->uc) {
  330. /* copy the context back to ucontext */
  331. memcpy(e->uc->uc_mcontext.gregs, &e->context, sizeof(PAL_CONTEXT));
  332. }
  333. }