db_exception2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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_freebsd_defs.h"
  23. #include "pal.h"
  24. #include "pal_internal.h"
  25. #include "pal_freebsd.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 <ucontext.h>
  32. #include <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. void restore_rt (void) asm ("__restore_rt");
  47. #ifndef SA_RESTORER
  48. #define SA_RESTORER 0x04000000
  49. #endif
  50. #define DEFINE_RESTORE_RT(syscall) DEFINE_RESTORE_RT2(syscall)
  51. # define DEFINE_RESTORE_RT2(syscall) \
  52. asm ( \
  53. " nop\n" \
  54. ".align 16\n" \
  55. ".LSTART_restore_rt:\n" \
  56. " .type __restore_rt,@function\n" \
  57. "__restore_rt:\n" \
  58. " movq $" #syscall ", %rax\n" \
  59. " syscall\n");
  60. #endif
  61. int set_sighandler (int * sigs, int nsig, void * handler)
  62. {
  63. struct sigaction action;
  64. if (handler) {
  65. action.sa_handler = (void (*)(int)) handler;
  66. action.sa_flags = SA_SIGINFO;
  67. #if !defined(__i386__)
  68. //action.sa_flags |= SA_RESTORER;
  69. //action.sa_restorer = restore_rt;
  70. #endif
  71. } else {
  72. action.sa_handler = SIG_IGN;
  73. }
  74. #ifdef DEBUG
  75. if (!linux_state.in_gdb)
  76. #endif
  77. action.sa_flags |= SA_NOCLDWAIT;
  78. __sigemptyset((__sigset_t *) &action.sa_mask);
  79. __sigaddset((__sigset_t *) &action.sa_mask, SIGCONT);
  80. for (int i = 0 ; i < nsig ; i++) {
  81. #if defined(__i386__)
  82. int ret = INLINE_SYSCALL(sigaction, 3, sigs[i], &action, NULL)
  83. #else
  84. int ret = INLINE_SYSCALL(sigaction, 4, sigs[i], &action, NULL,
  85. sizeof(sigset_t));
  86. #endif
  87. if (IS_ERR(ret))
  88. return -PAL_ERROR_DENIED;
  89. }
  90. return 0;
  91. }
  92. typedef struct {
  93. PAL_IDX event_num;
  94. PAL_CONTEXT context;
  95. ucontext_t * uc;
  96. PAL_PTR eframe;
  97. } PAL_EVENT;
  98. #define SIGNAL_MASK_TIME 1000
  99. static int get_event_num (int signum)
  100. {
  101. switch(signum) {
  102. case SIGFPE: return PAL_EVENT_ARITHMETIC_ERROR;
  103. case SIGSEGV: case SIGBUS: return PAL_EVENT_MEMFAULT;
  104. case SIGILL: case SIGSYS: return PAL_EVENT_ILLEGAL;
  105. case SIGTERM: return PAL_EVENT_QUIT;
  106. case SIGINT: return PAL_EVENT_SUSPEND;
  107. case SIGCONT: return PAL_EVENT_RESUME;
  108. default: return -1;
  109. }
  110. }
  111. static void _DkGenericEventTrigger (PAL_IDX event_num, PAL_EVENT_HANDLER upcall,
  112. PAL_NUM arg, struct pal_frame * frame,
  113. ucontext_t * uc, void * eframe)
  114. {
  115. PAL_EVENT event;
  116. event.event_num = event_num;
  117. if (uc) {
  118. event.context.r8 = uc->uc_mcontext.mc_r8;
  119. event.context.r9 = uc->uc_mcontext.mc_r9;
  120. event.context.r10 = uc->uc_mcontext.mc_r10;
  121. event.context.r11 = uc->uc_mcontext.mc_r11;
  122. event.context.r12 = uc->uc_mcontext.mc_r12;
  123. event.context.r13 = uc->uc_mcontext.mc_r13;
  124. event.context.r14 = uc->uc_mcontext.mc_r14;
  125. event.context.r15 = uc->uc_mcontext.mc_r15;
  126. event.context.rdi = uc->uc_mcontext.mc_rdi;
  127. event.context.rsi = uc->uc_mcontext.mc_rsi;
  128. event.context.rbp = uc->uc_mcontext.mc_rbp;
  129. event.context.rbx = uc->uc_mcontext.mc_rbx;
  130. event.context.rdx = uc->uc_mcontext.mc_rdx;
  131. event.context.rax = uc->uc_mcontext.mc_rax;
  132. event.context.rcx = uc->uc_mcontext.mc_rcx;
  133. event.context.rsp = uc->uc_mcontext.mc_rsp;
  134. event.context.rip = uc->uc_mcontext.mc_rip;
  135. event.context.efl = uc->uc_mcontext.mc_flags;
  136. event.context.err = uc->uc_mcontext.mc_err;
  137. event.context.trapno = uc->uc_mcontext.mc_trapno;
  138. event.context.csgsfs = 0;
  139. event.context.oldmask = 0;
  140. event.context.cr2 = 0;
  141. }
  142. if (frame) {
  143. event.context.r15 = frame->arch.r15;
  144. event.context.r14 = frame->arch.r14;
  145. event.context.r13 = frame->arch.r13;
  146. event.context.r12 = frame->arch.r12;
  147. event.context.rdi = frame->arch.rdi;
  148. event.context.rsi = frame->arch.rsi;
  149. event.context.rbx = frame->arch.rbx;
  150. /* find last frame */
  151. event.context.rsp = frame->arch.rbp + sizeof(unsigned long) * 2;
  152. event.context.rbp = ((unsigned long *) frame->arch.rbp)[0];
  153. event.context.rip = ((unsigned long *) frame->arch.rbp)[1];
  154. /* making rax = 0 to tell the caller that this PAL call failed */
  155. event.context.rax = 0;
  156. }
  157. event.uc = uc;
  158. event.eframe = eframe;
  159. (*upcall) ((PAL_PTR) &event, arg, &event.context);
  160. }
  161. static bool _DkGenericSignalHandle (int event_num, siginfo_t * info,
  162. struct pal_frame * frame,
  163. ucontext_t * uc, void * eframe)
  164. {
  165. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(event_num);
  166. if (upcall) {
  167. PAL_NUM arg = 0;
  168. if (event_num == PAL_EVENT_ARITHMETIC_ERROR ||
  169. event_num == PAL_EVENT_MEMFAULT ||
  170. event_num == PAL_EVENT_ILLEGAL)
  171. arg = (PAL_NUM) (info ? info->si_addr : 0);
  172. _DkGenericEventTrigger(event_num, upcall, arg, frame, uc, eframe);
  173. return true;
  174. }
  175. return false;
  176. }
  177. #define ADDR_IN_PAL(addr) \
  178. ((void *) (addr) > TEXT_START && (void *) (addr) < TEXT_END)
  179. /* This function walks the stack to find the PAL_FRAME
  180. * that was saved upon entry to the PAL, if an exception/interrupt
  181. * comes in during a PAL call. This is needed to support the behavior that an
  182. * exception in the PAL has Unix-style, EAGAIN semantics.
  183. *
  184. * The PAL_FRAME is supposed to be in the first PAL frame, and we look for
  185. * it by matching a special magic number, that should only appear on the stack
  186. * once.
  187. *
  188. * If an exception comes in while we are not in the PAL, this PAL_FRAME won't
  189. * exist, and it is ok to return NULL.
  190. */
  191. static struct pal_frame * get_frame (ucontext_t * uc)
  192. {
  193. unsigned long rip = uc->uc_mcontext.mc_rip;
  194. unsigned long rbp = uc->uc_mcontext.mc_rbp;
  195. unsigned long last_rbp = rbp - 64;
  196. if (!ADDR_IN_PAL(rip))
  197. return NULL;
  198. while (ADDR_IN_PAL(((unsigned long *) rbp)[1])) {
  199. last_rbp = rbp;
  200. rbp = *(unsigned long *) rbp;
  201. }
  202. /* search frame record in the top frame of PAL */
  203. for (unsigned long ptr = rbp - sizeof(unsigned long) ;
  204. ptr > last_rbp ; ptr -= 8) {
  205. struct pal_frame * frame = (struct pal_frame *) ptr;
  206. if (frame->identifier == PAL_FRAME_IDENTIFIER)
  207. return frame;
  208. }
  209. return NULL;
  210. }
  211. static void return_frame (struct pal_frame * frame, int err)
  212. {
  213. if (err)
  214. _DkRaiseFailure(err);
  215. __clear_frame(frame);
  216. arch_restore_frame(&frame->arch);
  217. asm volatile ("xor %rax, %rax\r\n"
  218. "leaveq\r\n"
  219. "retq\r\n");
  220. }
  221. #if BLOCK_SIGFAULT == 1
  222. static char exception_msg[24] = "--- SIGSEGV --- [ ]\n";
  223. static volatile bool cont_exec = false;
  224. #endif
  225. static void _DkGenericSighandler (int signum, siginfo_t * info,
  226. struct ucontext * uc)
  227. {
  228. #if BLOCK_SIGFUALT == 1
  229. /* reseurrect this code if signal handler if giving segmentation fault */
  230. if (signum == SIGSEGV) {
  231. int pid = INLINE_SYSCALL(getpid, 0);
  232. exception_msg[17] = '0' + pid / 10000;
  233. exception_msg[18] = '0' + (pid / 1000) % 10;
  234. exception_msg[19] = '0' + (pid / 100) % 10;
  235. exception_msg[20] = '0' + (pid / 10) % 10;
  236. exception_msg[21] = '0' + pid % 10;
  237. INLINE_SYSCALL(write, 3, 1, exception_msg, 24);
  238. while(!cont_exec);
  239. }
  240. #endif
  241. struct pal_frame * frame = get_frame(uc);
  242. void * eframe;
  243. if (signum == SIGCONT && frame && frame->func == DkObjectsWaitAny)
  244. return;
  245. asm volatile ("movq %%rbp, %0" : "=r"(eframe));
  246. if (frame && frame->func != &_DkGenericSighandler &&
  247. signum != SIGCONT &&
  248. signum != SIGINT &&
  249. signum != SIGTERM) {
  250. return_frame(frame, PAL_ERROR_BADADDR);
  251. return;
  252. }
  253. int event_num = get_event_num(signum);
  254. if (event_num == -1)
  255. return;
  256. _DkGenericSignalHandle(event_num, info, frame, uc, eframe);
  257. }
  258. static void _DkTerminateSighandler (int signum, siginfo_t * info,
  259. struct ucontext * uc)
  260. {
  261. struct pal_frame * frame = get_frame(uc);
  262. void * eframe;
  263. asm volatile ("movq %%rbp, %0" : "=r"(eframe));
  264. int event_num = get_event_num(signum);
  265. if (event_num == -1)
  266. return;
  267. if (!_DkGenericSignalHandle(event_num, NULL, frame, uc, eframe))
  268. _DkThreadExit();
  269. }
  270. static void _DkPipeSighandler (int signum, siginfo_t * info,
  271. struct ucontext * uc)
  272. {
  273. return;
  274. }
  275. void _DkRaiseFailure (int error)
  276. {
  277. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(PAL_EVENT_FAILURE);
  278. if (!upcall)
  279. return;
  280. PAL_EVENT event;
  281. event.event_num = PAL_EVENT_FAILURE;
  282. event.uc = NULL;
  283. event.eframe = NULL;
  284. (*upcall) ((PAL_PTR) &event, error, NULL);
  285. }
  286. struct signal_ops {
  287. int signum[3];
  288. void (*handler) (int signum, siginfo_t * info, ucontext_t * uc);
  289. };
  290. struct signal_ops on_signals[] = {
  291. [PAL_EVENT_ARITHMETIC_ERROR] = { .signum = { SIGFPE, 0 },
  292. .handler = _DkGenericSighandler },
  293. [PAL_EVENT_MEMFAULT] = { .signum = { SIGSEGV, SIGBUS, 0 },
  294. .handler = _DkGenericSighandler },
  295. [PAL_EVENT_ILLEGAL] = { .signum = { SIGILL, SIGSYS, 0 },
  296. .handler = _DkGenericSighandler },
  297. [PAL_EVENT_QUIT] = { .signum = { SIGTERM, 0, 0 },
  298. .handler = _DkTerminateSighandler },
  299. [PAL_EVENT_SUSPEND] = { .signum = { SIGINT, 0 },
  300. .handler = _DkTerminateSighandler },
  301. [PAL_EVENT_RESUME] = { .signum = { SIGCONT, 0 },
  302. .handler = _DkGenericSighandler },
  303. };
  304. static int _DkPersistentSighandlerSetup (int event_num)
  305. {
  306. int nsigs, * sigs = on_signals[event_num].signum;
  307. for (nsigs = 0 ; sigs[nsigs] ; nsigs++);
  308. int ret = set_sighandler(sigs, nsigs, on_signals[event_num].handler);
  309. if (ret < 0)
  310. return ret;
  311. return 0;
  312. }
  313. void signal_setup (void)
  314. {
  315. int ret, sig = SIGCHLD;
  316. #ifdef DEBUG
  317. if (!linux_state.in_gdb)
  318. #endif
  319. set_sighandler(&sig, 1, NULL);
  320. sig = SIGPIPE;
  321. if ((ret = set_sighandler(&sig, 1, &_DkPipeSighandler)) < 0)
  322. goto err;
  323. int events[] = {
  324. PAL_EVENT_ARITHMETIC_ERROR,
  325. PAL_EVENT_MEMFAULT,
  326. PAL_EVENT_ILLEGAL,
  327. PAL_EVENT_QUIT,
  328. PAL_EVENT_SUSPEND,
  329. PAL_EVENT_RESUME,
  330. };
  331. for (int e = 0 ; e < sizeof(events) / sizeof(events[0]) ; e++)
  332. if ((ret = _DkPersistentSighandlerSetup(events[e])) < 0)
  333. goto err;
  334. return;
  335. err:
  336. init_fail(-ret, "cannot setup signal handlers");
  337. }
  338. void _DkExceptionReturn (void * event)
  339. {
  340. PAL_EVENT * e = event;
  341. if (e->eframe) {
  342. struct pal_frame * frame = (struct pal_frame *) e->eframe;
  343. int err = 0;
  344. switch (e->event_num) {
  345. case PAL_EVENT_MEMFAULT:
  346. err = PAL_ERROR_BADADDR;
  347. break;
  348. case PAL_EVENT_QUIT:
  349. case PAL_EVENT_SUSPEND:
  350. case PAL_EVENT_RESUME:
  351. err = PAL_ERROR_INTERRUPTED;
  352. break;
  353. }
  354. if (err)
  355. _DkRaiseFailure(err);
  356. __clear_frame(frame);
  357. }
  358. if (e->uc) {
  359. /* copy the context back to ucontext */
  360. e->uc->uc_mcontext.mc_r8 = e->context.r8;
  361. e->uc->uc_mcontext.mc_r9 = e->context.r9;
  362. e->uc->uc_mcontext.mc_r10 = e->context.r10;
  363. e->uc->uc_mcontext.mc_r11 = e->context.r11;
  364. e->uc->uc_mcontext.mc_r12 = e->context.r12;
  365. e->uc->uc_mcontext.mc_r13 = e->context.r13;
  366. e->uc->uc_mcontext.mc_r14 = e->context.r14;
  367. e->uc->uc_mcontext.mc_r15 = e->context.r15;
  368. e->uc->uc_mcontext.mc_rdi = e->context.rdi;
  369. e->uc->uc_mcontext.mc_rsi = e->context.rsi;
  370. e->uc->uc_mcontext.mc_rbp = e->context.rbp;
  371. e->uc->uc_mcontext.mc_rbx = e->context.rbx;
  372. e->uc->uc_mcontext.mc_rdx = e->context.rdx;
  373. e->uc->uc_mcontext.mc_rax = e->context.rax;
  374. e->uc->uc_mcontext.mc_rcx = e->context.rcx;
  375. e->uc->uc_mcontext.mc_rsp = e->context.rsp;
  376. e->uc->uc_mcontext.mc_rip = e->context.rip;
  377. e->uc->uc_mcontext.mc_flags = e->context.efl;
  378. e->uc->uc_mcontext.mc_err = e->context.err;
  379. e->uc->uc_mcontext.mc_trapno = e->context.trapno;
  380. /* return to the frame of exception handler */
  381. asm volatile ("movq %0, %%rbp\r\n"
  382. "leaveq\r\n"
  383. "retq\r\n" :: "r"(e->eframe) : "memory");
  384. }
  385. }