db_exception2.c 14 KB

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