db_exception.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 "list.h"
  30. #include <atomic.h>
  31. #include <sigset.h>
  32. #include "signal.h"
  33. #include <ucontext.h>
  34. #include <errno.h>
  35. typedef void (*PAL_UPCALL) (PAL_PTR, PAL_NUM, PAL_CONTEXT *);
  36. int set_sighandler (int * sigs, int nsig, void * handler)
  37. {
  38. struct sigaction action;
  39. action.sa_handler = (void (*)(int)) handler;
  40. action.sa_flags = SA_SIGINFO;
  41. __sigemptyset((_sigset_t *) &action.sa_mask);
  42. __sigaddset((_sigset_t *) &action.sa_mask, SIGCONT);
  43. for (int i = 0 ; i < nsig ; i++) {
  44. if (sigs[i] == SIGCHLD)
  45. action.sa_flags |= SA_NOCLDSTOP|SA_NOCLDWAIT;
  46. #if defined(__i386__)
  47. int ret = INLINE_SYSCALL(sigaction, 3, sigs[i], &action, NULL)
  48. #else
  49. int ret = INLINE_SYSCALL(sigaction, 4, sigs[i], &action, NULL,
  50. sizeof(sigset_t));
  51. #endif
  52. if (IS_ERR(ret))
  53. return -PAL_ERROR_DENIED;
  54. action.sa_flags &= ~(SA_NOCLDSTOP|SA_NOCLDWAIT);
  55. }
  56. bool maskset = false;
  57. int ret = 0;
  58. _sigset_t mask;
  59. __sigemptyset(&mask);
  60. for (int i = 0 ; i < nsig ; i++)
  61. if (__sigismember(&bsd_state.sigset, sigs[i])) {
  62. __sigdelset(&bsd_state.sigset, sigs[i]);
  63. __sigaddset(&mask, sigs[i]);
  64. maskset = true;
  65. }
  66. if (maskset) {
  67. #if defined(__i386__)
  68. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_UNBLOCK, &mask, NULL)
  69. #else
  70. ret = INLINE_SYSCALL(sigprocmask, 4, SIG_UNBLOCK, &mask, NULL,
  71. sizeof(sigset_t));
  72. #endif
  73. }
  74. if (IS_ERR(ret))
  75. return -PAL_ERROR_DENIED;
  76. return 0;
  77. }
  78. int block_signals (int * sigs, int nsig)
  79. {
  80. bool maskset = false;
  81. int ret = 0;
  82. _sigset_t mask;
  83. __sigemptyset(&mask);
  84. for (int i = 0 ; i < nsig ; i++)
  85. if (!__sigismember(&bsd_state.sigset, sigs[i])) {
  86. __sigaddset(&bsd_state.sigset, sigs[i]);
  87. __sigaddset(&mask, sigs[i]);
  88. maskset = true;
  89. }
  90. if (maskset) {
  91. #if defined(__i386__)
  92. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_BLOCK, &mask, NULL)
  93. #else
  94. ret = INLINE_SYSCALL(sigprocmask, 4, SIG_BLOCK, &mask, NULL,
  95. sizeof(sigset_t));
  96. #endif
  97. }
  98. if (IS_ERR(ret))
  99. return -PAL_ERROR_DENIED;
  100. return 0;
  101. }
  102. int unblock_signals (int * sigs, int nsig)
  103. {
  104. bool maskset = false;
  105. int ret = 0;
  106. _sigset_t mask;
  107. __sigemptyset(&mask);
  108. for (int i = 0 ; i < nsig ; i++)
  109. if (__sigismember(&bsd_state.sigset, sigs[i])) {
  110. __sigdelset(&bsd_state.sigset, sigs[i]);
  111. __sigaddset(&mask, sigs[i]);
  112. maskset = true;
  113. }
  114. if (maskset) {
  115. #if defined(__i386__)
  116. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_UNBLOCK, &mask, NULL)
  117. #else
  118. ret = INLINE_SYSCALL(sigprocmask, 4, SIG_UNBLOCK, &mask, NULL,
  119. sizeof(sigset_t));
  120. #endif
  121. }
  122. if (IS_ERR(ret))
  123. return -PAL_ERROR_DENIED;
  124. return 0;
  125. }
  126. int unset_sighandler (int * sigs, int nsig)
  127. {
  128. for (int i = 0 ; i < nsig ; i++) {
  129. #if defined(__i386__)
  130. int ret = INLINE_SYSCALL(sigaction, 4, sigs[i], SIG_DFL, NULL)
  131. #else
  132. int ret = INLINE_SYSCALL(sigaction, 4, sigs[i], SIG_DFL, NULL,
  133. sizeof(_sigset_t));
  134. #endif
  135. if (IS_ERR(ret))
  136. return -PAL_ERROR_DENIED;
  137. }
  138. return 0;
  139. }
  140. struct exception_handler {
  141. struct mutex_handle lock;
  142. int flags;
  143. PAL_UPCALL upcall;
  144. } __attribute__((aligned(sizeof(int))));
  145. struct exception_event {
  146. int event_num;
  147. int flags;
  148. PAL_CONTEXT context;
  149. ucontext_t * uc;
  150. void * eframe;
  151. };
  152. #define DECLARE_HANDLER_HEAD(event) \
  153. static struct exception_handler handler_##event = \
  154. { .lock = MUTEX_HANDLE_INIT, \
  155. .upcall = NULL, \
  156. .flags = 0, };
  157. DECLARE_HANDLER_HEAD(DivZero);
  158. DECLARE_HANDLER_HEAD(MemFault);
  159. DECLARE_HANDLER_HEAD(Illegal);
  160. DECLARE_HANDLER_HEAD(Quit);
  161. DECLARE_HANDLER_HEAD(Suspend);
  162. DECLARE_HANDLER_HEAD(Resume);
  163. DECLARE_HANDLER_HEAD(Failure);
  164. struct exception_handler * pal_handlers [PAL_EVENT_NUM_BOUND] = {
  165. NULL, /* reserved */
  166. &handler_DivZero,
  167. &handler_MemFault,
  168. &handler_Illegal,
  169. &handler_Quit,
  170. &handler_Suspend,
  171. &handler_Resume,
  172. &handler_Failure,
  173. };
  174. static int get_event_num (int signum)
  175. {
  176. switch(signum) {
  177. case SIGFPE: return PAL_EVENT_ARITHMETIC_ERROR;
  178. case SIGSEGV: case SIGBUS: return PAL_EVENT_MEMFAULT;
  179. case SIGILL: return PAL_EVENT_ILLEGAL;
  180. case SIGTERM: return PAL_EVENT_QUIT;
  181. case SIGINT: return PAL_EVENT_SUSPEND;
  182. case SIGCONT: return PAL_EVENT_RESUME;
  183. default: return -1;
  184. }
  185. }
  186. static void _DkGenericEventTrigger (int event_num, PAL_UPCALL upcall,
  187. int flags, PAL_NUM arg,
  188. struct pal_frame * frame,
  189. ucontext_t * uc, void * eframe)
  190. {
  191. struct exception_event event;
  192. event.event_num = event_num;
  193. event.flags = flags;
  194. if (uc) {
  195. event.context.r8 = uc->uc_mcontext.mc_r8;
  196. event.context.r9 = uc->uc_mcontext.mc_r9;
  197. event.context.r10 = uc->uc_mcontext.mc_r10;
  198. event.context.r11 = uc->uc_mcontext.mc_r11;
  199. event.context.r12 = uc->uc_mcontext.mc_r12;
  200. event.context.r13 = uc->uc_mcontext.mc_r13;
  201. event.context.r14 = uc->uc_mcontext.mc_r14;
  202. event.context.r15 = uc->uc_mcontext.mc_r15;
  203. event.context.rdi = uc->uc_mcontext.mc_rdi;
  204. event.context.rsi = uc->uc_mcontext.mc_rsi;
  205. event.context.rbp = uc->uc_mcontext.mc_rbp;
  206. event.context.rbx = uc->uc_mcontext.mc_rbx;
  207. event.context.rdx = uc->uc_mcontext.mc_rdx;
  208. event.context.rax = uc->uc_mcontext.mc_rax;
  209. event.context.rcx = uc->uc_mcontext.mc_rcx;
  210. event.context.rsp = uc->uc_mcontext.mc_rsp;
  211. event.context.rip = uc->uc_mcontext.mc_rip;
  212. event.context.efl = uc->uc_mcontext.mc_flags;
  213. event.context.err = uc->uc_mcontext.mc_err;
  214. event.context.trapno = uc->uc_mcontext.mc_trapno;
  215. event.context.csgsfs = 0;
  216. event.context.oldmask = 0;
  217. event.context.cr2 = 0;
  218. }
  219. if (frame) {
  220. event.context.r15 = frame->arch.r15;
  221. event.context.r14 = frame->arch.r14;
  222. event.context.r13 = frame->arch.r13;
  223. event.context.r12 = frame->arch.r12;
  224. event.context.rdi = frame->arch.rdi;
  225. event.context.rsi = frame->arch.rsi;
  226. event.context.rbx = frame->arch.rbx;
  227. /* find last frame */
  228. event.context.rsp = frame->arch.rbp + sizeof(unsigned long) * 2;
  229. event.context.rbp = ((unsigned long *) frame->arch.rbp)[0];
  230. event.context.rip = ((unsigned long *) frame->arch.rbp)[1];
  231. }
  232. event.uc = uc;
  233. event.eframe = eframe;
  234. (*upcall) (&event, arg, &event.context);
  235. }
  236. static bool _DkGenericSignalHandle (int event_num, siginfo_t * info,
  237. struct pal_frame * frame,
  238. ucontext_t * uc, void * eframe)
  239. {
  240. struct exception_handler * handler = pal_handlers[event_num];
  241. _DkMutexLock(&handler->lock);
  242. PAL_UPCALL upcall = handler->upcall;
  243. int flags = handler->flags;
  244. _DkMutexUnlock(&handler->lock);
  245. if (upcall) {
  246. PAL_NUM arg = 0;
  247. if (event_num == PAL_EVENT_ARITHMETIC_ERROR ||
  248. event_num == PAL_EVENT_MEMFAULT ||
  249. event_num == PAL_EVENT_ILLEGAL)
  250. arg = (PAL_NUM) (info ? info->si_addr : 0);
  251. _DkGenericEventTrigger(event_num, upcall, flags, arg, frame,
  252. uc, eframe);
  253. return true;
  254. }
  255. return false;
  256. }
  257. #define ADDR_IN_PAL(addr) \
  258. ((void*)(addr) > TEXT_START && (void*)(addr) < TEXT_END)
  259. static struct pal_frame * get_frame (ucontext_t * uc)
  260. {
  261. unsigned long rip = uc->uc_mcontext.mc_rip;
  262. unsigned long rbp = uc->uc_mcontext.mc_rbp;
  263. unsigned long last_rbp = rbp - 1024;
  264. if (!ADDR_IN_PAL(rip))
  265. return NULL;
  266. while (ADDR_IN_PAL(((unsigned long *) rbp)[1])) {
  267. last_rbp = rbp;
  268. rbp = *(unsigned long *) rbp;
  269. }
  270. /* search frame record in the top frame of PAL */
  271. for (unsigned long ptr = rbp - sizeof(unsigned long) ;
  272. ptr > last_rbp ; ptr -= 8) {
  273. struct pal_frame * frame = (struct pal_frame *) ptr;
  274. if (frame->identifier == PAL_FRAME_IDENTIFIER)
  275. return frame;
  276. }
  277. return NULL;
  278. }
  279. static void return_frame (struct pal_frame * frame, int err)
  280. {
  281. if (err)
  282. _DkRaiseFailure(err);
  283. __clear_frame(frame);
  284. arch_restore_frame(&frame->arch);
  285. asm volatile ("xor %%rax, %%rax\r\n"
  286. "leaveq\r\n"
  287. "retq\r\n" ::: "memory");
  288. }
  289. static void _DkGenericSighandler (int signum, siginfo_t * info,
  290. ucontext_t * uc)
  291. {
  292. #if 0
  293. /* reseurrect this code if signal handler if giving segmentation fault */
  294. if (signum == SIGSEGV) {
  295. int pid = INLINE_SYSCALL(getpid, 0);
  296. char msg[24] = "--- SIGSEGV --- [ ]\n";
  297. msg[17] = '0' + pid / 10000;
  298. msg[18] = '0' + (pid / 1000) % 10;
  299. msg[19] = '0' + (pid / 100) % 10;
  300. msg[20] = '0' + (pid / 10) % 10;
  301. msg[21] = '0' + pid % 10;
  302. INLINE_SYSCALL(write, 3, 1, msg, 24);
  303. }
  304. #endif
  305. struct pal_frame * frame = get_frame(uc);
  306. void * eframe;
  307. asm volatile ("movq %%rbp, %0" : "=r"(eframe));
  308. if (frame && frame->func != &_DkGenericSighandler &&
  309. signum != SIGCONT &&
  310. signum != SIGINT &&
  311. signum != SIGTERM) {
  312. return_frame(frame, PAL_ERROR_BADADDR);
  313. return;
  314. }
  315. int event_num = get_event_num(signum);
  316. if (event_num == -1)
  317. return;
  318. _DkGenericSignalHandle(event_num, info, frame, uc, eframe);
  319. }
  320. static void _DkTerminateSighandler (int signum, siginfo_t * info,
  321. ucontext_t * uc)
  322. {
  323. struct pal_frame * frame = get_frame(uc);
  324. void * eframe;
  325. asm volatile ("movq %%rbp, %0" : "=r"(eframe));
  326. int event_num = get_event_num(signum);
  327. if (event_num == -1)
  328. return;
  329. if (!_DkGenericSignalHandle(event_num, NULL, frame, uc, eframe))
  330. _DkThreadExit();
  331. }
  332. static void _DkPipeSighandler (int signum, siginfo_t * info,
  333. ucontext_t * uc)
  334. {
  335. struct pal_frame * frame = get_frame(uc);
  336. if (frame)
  337. return_frame(frame, PAL_ERROR_CONNFAILED);
  338. }
  339. void _DkRaiseFailure (int error)
  340. {
  341. _DkMutexLock(&handler_Failure.lock);
  342. PAL_UPCALL upcall = handler_Failure.upcall;
  343. int flags = handler_Failure.flags;
  344. _DkMutexUnlock(&handler_Failure.lock);
  345. if (upcall)
  346. _DkGenericEventTrigger(PAL_EVENT_FAILURE, upcall, flags, error,
  347. NULL, NULL, NULL);
  348. }
  349. struct signal_ops {
  350. int signum[3];
  351. void (*handler) (int signum, siginfo_t * info, ucontext_t * uc);
  352. };
  353. struct signal_ops on_signals[PAL_EVENT_NUM_BOUND] = {
  354. /* DivZero */ { .signum = { SIGFPE, 0 },
  355. .handler = _DkGenericSighandler },
  356. /* MemFault */ { .signum = { SIGSEGV, SIGBUS, 0 },
  357. .handler = _DkGenericSighandler },
  358. /* Illegal */ { .signum = { SIGILL, 0 },
  359. .handler = _DkGenericSighandler },
  360. /* Quit */ { .signum = { SIGTERM, 0, 0 },
  361. .handler = _DkTerminateSighandler },
  362. /* Suspend */ { .signum = { SIGINT, 0 },
  363. .handler = _DkTerminateSighandler },
  364. /* Resume */ { .signum = { SIGCONT, 0 },
  365. .handler = _DkGenericSighandler },
  366. };
  367. static int _DkPersistentSighandlerSetup (int event_num)
  368. {
  369. int nsigs, * sigs = on_signals[event_num].signum;
  370. for (nsigs = 0 ; sigs[nsigs] ; nsigs++);
  371. void * sighandler = on_signals[event_num].handler;
  372. int ret = set_sighandler (sigs, nsigs, sighandler);
  373. if (ret < 0)
  374. return ret;
  375. return 0;
  376. }
  377. static int _DkPersistentEventUpcall (int event_num, PAL_UPCALL upcall,
  378. int flags)
  379. {
  380. struct exception_handler * handler = pal_handlers[event_num];
  381. _DkMutexLock(&handler->lock);
  382. handler->upcall = upcall;
  383. handler->flags = flags;
  384. _DkMutexUnlock(&handler->lock);
  385. return _DkPersistentSighandlerSetup(event_num);
  386. }
  387. static int _DkGenericEventUpcall (int event_num, PAL_UPCALL upcall,
  388. int flags)
  389. {
  390. int nsigs, * sigs = on_signals[event_num].signum;
  391. for (nsigs = 0 ; sigs[nsigs] ; nsigs++);
  392. void * sighandler = on_signals[event_num].handler;
  393. struct exception_handler * handler = pal_handlers[event_num];
  394. int ret = 0;
  395. _DkMutexLock(&handler->lock);
  396. handler->upcall = upcall;
  397. handler->flags = flags;
  398. _DkMutexUnlock(&handler->lock);
  399. if (upcall)
  400. ret = set_sighandler (sigs, nsigs, sighandler);
  401. else
  402. ret = block_signals (sigs, nsigs);
  403. return ret;
  404. }
  405. static int _DkDummyEventUpcall (int event_num, PAL_UPCALL upcall,
  406. int flags)
  407. {
  408. struct exception_handler * handler = pal_handlers[event_num];
  409. _DkMutexLock(&handler->lock);
  410. handler->upcall = upcall;
  411. handler->flags = flags;
  412. _DkMutexUnlock(&handler->lock);
  413. return 0;
  414. }
  415. typedef void (*PAL_UPCALL) (PAL_PTR, PAL_NUM, PAL_CONTEXT *);
  416. int (*_DkExceptionHandlers[PAL_EVENT_NUM_BOUND])
  417. (int, PAL_UPCALL, int) = {
  418. /* reserved */ NULL,
  419. /* DivZero */ &_DkPersistentEventUpcall,
  420. /* MemFault */ &_DkPersistentEventUpcall,
  421. /* Illegal */ &_DkPersistentEventUpcall,
  422. /* Quit */ &_DkGenericEventUpcall,
  423. /* Suspend */ &_DkGenericEventUpcall,
  424. /* Resume */ &_DkGenericEventUpcall,
  425. /* Failure */ &_DkDummyEventUpcall,
  426. };
  427. static void _DkCompatibilitySighandler (int signum, siginfo_t * info,
  428. ucontext_t * uc)
  429. {
  430. /* not implemented */
  431. }
  432. void signal_setup (void)
  433. {
  434. int ret, sig;
  435. __sigemptyset(&bsd_state.sigset);
  436. struct sigaction action;
  437. action.sa_handler = (void (*)(int)) SIG_IGN;
  438. action.sa_flags = 0;
  439. #if defined(__i386__)
  440. ret = INLINE_SYSCALL(sigaction, 3, SIGCHLD, &action, NULL)
  441. #else
  442. ret = INLINE_SYSCALL(sigaction, 4, SIGCHLD, &action, NULL,
  443. sizeof(sigset_t));
  444. #endif
  445. if (IS_ERR(ret)) {
  446. ret = -PAL_ERROR_DENIED;
  447. goto err;
  448. }
  449. if ((ret = _DkPersistentEventUpcall(PAL_EVENT_ARITHMETIC_ERROR, NULL, 0)) < 0)
  450. goto err;
  451. if ((ret = _DkPersistentEventUpcall(PAL_EVENT_MEMFAULT, NULL, 0)) < 0)
  452. goto err;
  453. if ((ret = _DkPersistentEventUpcall(PAL_EVENT_ILLEGAL, NULL, 0)) < 0)
  454. goto err;
  455. sig = SIGPIPE;
  456. if ((ret = set_sighandler(&sig, 1, &_DkPipeSighandler)) < 0)
  457. goto err;
  458. sig = SIGSYS;
  459. if ((ret = set_sighandler(&sig, 1, &_DkCompatibilitySighandler)) < 0)
  460. goto err;
  461. return;
  462. err:
  463. INIT_FAIL(-ret, "cannot setup signal handlers");
  464. }
  465. void _DkExceptionReturn (void * event)
  466. {
  467. const struct exception_event * e = (const struct exception_event *) event;
  468. if (e->uc) {
  469. /* copy the context back to ucontext */
  470. e->uc->uc_mcontext.mc_r8 = e->context.r8;
  471. e->uc->uc_mcontext.mc_r9 = e->context.r9;
  472. e->uc->uc_mcontext.mc_r10 = e->context.r10;
  473. e->uc->uc_mcontext.mc_r11 = e->context.r11;
  474. e->uc->uc_mcontext.mc_r12 = e->context.r12;
  475. e->uc->uc_mcontext.mc_r13 = e->context.r13;
  476. e->uc->uc_mcontext.mc_r14 = e->context.r14;
  477. e->uc->uc_mcontext.mc_r15 = e->context.r15;
  478. e->uc->uc_mcontext.mc_rdi = e->context.rdi;
  479. e->uc->uc_mcontext.mc_rsi = e->context.rsi;
  480. e->uc->uc_mcontext.mc_rbp = e->context.rbp;
  481. e->uc->uc_mcontext.mc_rbx = e->context.rbx;
  482. e->uc->uc_mcontext.mc_rdx = e->context.rdx;
  483. e->uc->uc_mcontext.mc_rax = e->context.rax;
  484. e->uc->uc_mcontext.mc_rcx = e->context.rcx;
  485. e->uc->uc_mcontext.mc_rsp = e->context.rsp;
  486. e->uc->uc_mcontext.mc_rip = e->context.rip;
  487. e->uc->uc_mcontext.mc_flags = e->context.efl;
  488. e->uc->uc_mcontext.mc_err = e->context.err;
  489. e->uc->uc_mcontext.mc_trapno = e->context.trapno;
  490. /* return to the frame of exception handler */
  491. asm volatile ("movq %0, %%rbp\r\n"
  492. "leaveq\r\n"
  493. "retq\r\n" :: "r"(e->eframe) : "memory");
  494. }
  495. }