db_exception.c 17 KB

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