db_exception.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 "ecall_types.h"
  30. #include <atomic.h>
  31. #include <sigset.h>
  32. #include <linux/signal.h>
  33. #include <ucontext.h>
  34. struct exception_handler {
  35. struct spinlock lock;
  36. int flags;
  37. PAL_UPCALL upcall;
  38. } __attribute__((aligned(sizeof(int))));
  39. struct exception_event {
  40. int event_num;
  41. int flags;
  42. PAL_CONTEXT * context;
  43. struct pal_frame * frame;
  44. };
  45. #define DECLARE_HANDLER_HEAD(event) \
  46. static struct exception_handler handler_##event = \
  47. { .lock = LOCK_INIT, \
  48. .upcall = NULL, \
  49. .flags = 0, }
  50. DECLARE_HANDLER_HEAD(DivZero);
  51. DECLARE_HANDLER_HEAD(MemFault);
  52. DECLARE_HANDLER_HEAD(Illegal);
  53. DECLARE_HANDLER_HEAD(Quit);
  54. DECLARE_HANDLER_HEAD(Suspend);
  55. DECLARE_HANDLER_HEAD(Resume);
  56. DECLARE_HANDLER_HEAD(Failure);
  57. struct exception_handler * pal_handlers [PAL_EVENT_NUM_BOUND] = {
  58. NULL, /* reserved */
  59. &handler_DivZero,
  60. &handler_MemFault,
  61. &handler_Illegal,
  62. &handler_Quit,
  63. &handler_Suspend,
  64. &handler_Resume,
  65. &handler_Failure,
  66. };
  67. #define SIGNAL_MASK_TIME 1000
  68. #define save_return_point(ptr) \
  69. asm volatile ("leaq 0(%%rip), %%rax\r\n" \
  70. "movq %%rax, %0\r\n" \
  71. : "=b"(ptr) :: "memory", "rax")
  72. void _DkGenericEventTrigger (int event_num, PAL_UPCALL upcall,
  73. int flags, PAL_NUM arg, struct pal_frame * frame,
  74. PAL_CONTEXT * context)
  75. {
  76. struct exception_event event;
  77. event.event_num = event_num;
  78. event.flags = flags;
  79. event.context = context;
  80. event.frame = frame;
  81. (*upcall) ((PAL_PTR) &event, arg, context);
  82. }
  83. static bool _DkGenericSignalHandle (int event_num, PAL_NUM arg,
  84. struct pal_frame * frame,
  85. PAL_CONTEXT * context)
  86. {
  87. struct exception_handler * handler = pal_handlers[event_num];
  88. _DkSpinLock(&handler->lock);
  89. PAL_UPCALL upcall = handler->upcall;
  90. int flags = handler->flags;
  91. _DkSpinUnlock(&handler->lock);
  92. if (upcall) {
  93. _DkGenericEventTrigger(event_num, upcall, flags, arg, frame, context);
  94. return true;
  95. }
  96. return false;
  97. }
  98. #define ADDR_IN_PAL(addr) \
  99. ((void *) (addr) > TEXT_START && (void *) (addr) < TEXT_END)
  100. static struct pal_frame * get_frame (sgx_context_t * uc)
  101. {
  102. unsigned long rbp;
  103. if (uc) {
  104. unsigned long rip = uc->rip;
  105. rbp = uc->rbp;
  106. if (!ADDR_IN_PAL(rip))
  107. return NULL;
  108. } else {
  109. asm volatile ("movq %%rbp, %0" : "=r"(rbp) :: "memory");
  110. }
  111. while (ADDR_IN_PAL(((unsigned long *) rbp)[1]))
  112. rbp = *(unsigned long *) rbp;
  113. struct pal_frame * frame = (struct pal_frame *) rbp - 1;
  114. for (int i = 0 ; i < 8 ; i++) {
  115. if (frame->self == frame)
  116. return frame;
  117. frame = (struct pal_frame *) ((void *) frame - 8);
  118. }
  119. return NULL;
  120. }
  121. static int _DkEventUpcall (int event_num, PAL_UPCALL upcall, int flags)
  122. {
  123. struct exception_handler * handler = pal_handlers[event_num];
  124. _DkSpinLock(&handler->lock);
  125. handler->upcall = upcall;
  126. handler->flags = flags;
  127. _DkSpinUnlock(&handler->lock);
  128. return 0;
  129. }
  130. typedef void (*PAL_UPCALL) (PAL_PTR, PAL_NUM, PAL_CONTEXT *);
  131. int (*_DkExceptionHandlers[PAL_EVENT_NUM_BOUND])
  132. (int, PAL_UPCALL, int) = {
  133. /* reserved */ NULL,
  134. /* DivZero */ &_DkEventUpcall,
  135. /* MemFault */ &_DkEventUpcall,
  136. /* Illegal */ &_DkEventUpcall,
  137. /* Quit */ &_DkEventUpcall,
  138. /* Suspend */ &_DkEventUpcall,
  139. /* Resume */ &_DkEventUpcall,
  140. /* Failure */ &_DkEventUpcall,
  141. };
  142. asm (".type arch_exception_return_asm, @function;"
  143. "arch_exception_return_asm:"
  144. " pop %rax;"
  145. " pop %rbx;"
  146. " pop %rcx;"
  147. " pop %rdx;"
  148. " pop %rsi;"
  149. " pop %rdi;"
  150. " pop %r8;"
  151. " pop %r9;"
  152. " pop %r10;"
  153. " pop %r11;"
  154. " pop %r12;"
  155. " pop %r13;"
  156. " pop %r14;"
  157. " pop %r15;"
  158. " retq;");
  159. extern void arch_exception_return (void) asm ("arch_exception_return_asm");
  160. void _DkExceptionRealHandler (int event, PAL_CONTEXT * context, PAL_NUM arg,
  161. struct pal_frame * frame)
  162. {
  163. if (frame) {
  164. frame = __alloca(sizeof(struct pal_frame));
  165. frame->self = frame;
  166. frame->func = &_DkExceptionRealHandler;
  167. frame->funcname = "_DkExceptionRealHandler";
  168. store_register(rsp, frame->arch.rsp);
  169. store_register(rbp, frame->arch.rbp);
  170. unsigned long * last_frame = ((unsigned long *) frame->arch.rbp) + 1;
  171. last_frame[0] = (unsigned long) arch_exception_return;
  172. last_frame[1] = context->rax;
  173. last_frame[2] = context->rbx;
  174. last_frame[3] = context->rcx;
  175. last_frame[4] = context->rdx;
  176. last_frame[5] = context->rsi;
  177. last_frame[6] = context->rdi;
  178. last_frame[7] = context->r8;
  179. last_frame[8] = context->r9;
  180. last_frame[9] = context->r10;
  181. last_frame[10] = context->r11;
  182. last_frame[11] = context->r12;
  183. last_frame[12] = context->r13;
  184. last_frame[13] = context->r14;
  185. last_frame[14] = context->r15;
  186. last_frame[15] = context->rip;
  187. }
  188. _DkGenericSignalHandle(event, arg, frame, context);
  189. }
  190. void restore_sgx_context (sgx_context_t * uc)
  191. {
  192. /* prepare the return address */
  193. uc->rsp -= 8;
  194. *(uint64_t *) uc->rsp = uc->rip;
  195. /* now pop the stack */
  196. asm volatile ("mov %0, %%rsp\n"
  197. "pop %%rax\n"
  198. "pop %%rcx\n"
  199. "pop %%rdx\n"
  200. "pop %%rbx\n"
  201. "add $8, %%rsp\n" /* don't pop RSP yet */
  202. "pop %%rbp\n"
  203. "pop %%rsi\n"
  204. "pop %%rdi\n"
  205. "pop %%r8\n"
  206. "pop %%r9\n"
  207. "pop %%r10\n"
  208. "pop %%r11\n"
  209. "pop %%r12\n"
  210. "pop %%r13\n"
  211. "pop %%r14\n"
  212. "pop %%r15\n"
  213. "popfq\n"
  214. "mov -104(%%rsp), %%rsp\n"
  215. "ret\n"
  216. :: "r"(uc) : "memory");
  217. }
  218. void _DkExceptionHandler (unsigned int exit_info, sgx_context_t * uc)
  219. {
  220. #if SGX_HAS_FSGSBASE == 0
  221. /* set the FS first if necessary */
  222. uint64_t fsbase = (uint64_t) ENCLAVE_TLS(fsbase);
  223. if (fsbase)
  224. wrfsbase(fsbase);
  225. #endif
  226. union {
  227. sgx_arch_exitinfo_t info;
  228. int intval;
  229. } ei = { .intval = exit_info };
  230. int event_num;
  231. PAL_CONTEXT * ctx;
  232. if (!ei.info.valid) {
  233. event_num = exit_info;
  234. goto handle_event;
  235. }
  236. if (ei.info.vector == SGX_EXCEPTION_VECTOR_UD) {
  237. unsigned char * instr = (unsigned char *) uc->rip;
  238. if (instr[0] == 0xcc) { /* skip int 3 */
  239. uc->rip++;
  240. restore_sgx_context(uc);
  241. return;
  242. }
  243. if (instr[0] == 0x0f && instr[1] == 0xa2) {
  244. unsigned int values[4];
  245. if (!_DkCpuIdRetrieve(uc->rax & 0xffffffff,
  246. uc->rcx & 0xffffffff, values)) {
  247. uc->rip += 2;
  248. uc->rax = values[0];
  249. uc->rbx = values[1];
  250. uc->rcx = values[2];
  251. uc->rdx = values[3];
  252. restore_sgx_context(uc);
  253. return;
  254. }
  255. }
  256. if (instr[0] == 0x0f && instr[1] == 0x31) {
  257. uc->rip += 2;
  258. uc->rdx = 0;
  259. uc->rax = 0;
  260. restore_sgx_context(uc);
  261. return;
  262. }
  263. }
  264. switch (ei.info.vector) {
  265. case SGX_EXCEPTION_VECTOR_DE:
  266. event_num = PAL_EVENT_DIVZERO;
  267. break;
  268. case SGX_EXCEPTION_VECTOR_AC:
  269. event_num = PAL_EVENT_MEMFAULT;
  270. break;
  271. default:
  272. restore_sgx_context(uc);
  273. return;
  274. }
  275. handle_event:
  276. ctx = __alloca(sizeof(PAL_CONTEXT));
  277. memset(ctx, 0, sizeof(PAL_CONTEXT));
  278. ctx->rax = uc->rax;
  279. ctx->rbx = uc->rbx;
  280. ctx->rcx = uc->rcx;
  281. ctx->rdx = uc->rdx;
  282. ctx->rsp = uc->rsp;
  283. ctx->rbp = uc->rbp;
  284. ctx->rsi = uc->rsi;
  285. ctx->rdi = uc->rdi;
  286. ctx->r8 = uc->r8;
  287. ctx->r9 = uc->r9;
  288. ctx->r10 = uc->r10;
  289. ctx->r11 = uc->r11;
  290. ctx->r12 = uc->r12;
  291. ctx->r13 = uc->r13;
  292. ctx->r14 = uc->r14;
  293. ctx->r15 = uc->r15;
  294. ctx->efl = uc->rflags;
  295. ctx->rip = uc->rip;
  296. struct pal_frame * frame = get_frame(uc);
  297. PAL_NUM arg = 0;
  298. _DkExceptionRealHandler(event_num, ctx, arg, frame);
  299. restore_sgx_context(uc);
  300. }
  301. void _DkRaiseFailure (int error)
  302. {
  303. _DkSpinLock(&handler_Failure.lock);
  304. PAL_UPCALL upcall = handler_Failure.upcall;
  305. int flags = handler_Failure.flags;
  306. _DkSpinUnlock(&handler_Failure.lock);
  307. if (upcall)
  308. _DkGenericEventTrigger(PAL_EVENT_FAILURE, upcall, flags, error,
  309. NULL, NULL);
  310. }
  311. void _DkExceptionReturn (void * event)
  312. {
  313. struct exception_event * e = (struct exception_event *) event;
  314. sgx_context_t uc;
  315. PAL_CONTEXT * ctx = e->context;
  316. if (!ctx) {
  317. struct pal_frame * frame = e->frame;
  318. if (!frame)
  319. return;
  320. __clear_frame(frame);
  321. arch_restore_frame(&frame->arch);
  322. asm volatile ("xor %%rax, %%rax\r\n"
  323. "leaveq\r\n"
  324. "retq\r\n" ::: "memory");
  325. }
  326. uc.rax = ctx->rax;
  327. uc.rbx = ctx->rbx;
  328. uc.rcx = ctx->rcx;
  329. uc.rdx = ctx->rdx;
  330. uc.rsp = ctx->rsp;
  331. uc.rbp = ctx->rbp;
  332. uc.rsi = ctx->rsi;
  333. uc.rdi = ctx->rdi;
  334. uc.r8 = ctx->r8;
  335. uc.r9 = ctx->r9;
  336. uc.r10 = ctx->r10;
  337. uc.r11 = ctx->r11;
  338. uc.r12 = ctx->r12;
  339. uc.r13 = ctx->r13;
  340. uc.r14 = ctx->r14;
  341. uc.r15 = ctx->r15;
  342. uc.rflags = ctx->efl;
  343. uc.rip = ctx->rip;
  344. restore_sgx_context(&uc);
  345. }
  346. void _DkCheckExternalEvent (void)
  347. {
  348. if (!ENCLAVE_TLS(external_event))
  349. return;
  350. unsigned long event = ENCLAVE_TLS(external_event);
  351. ENCLAVE_TLS(external_event) = 0;
  352. struct pal_frame * frame = get_frame(NULL);
  353. if (!frame) {
  354. frame = __alloca(sizeof(struct pal_frame));
  355. frame->self = frame;
  356. frame->func = &_DkCheckExternalEvent;
  357. frame->funcname = "DkCheckExternalEvent";
  358. arch_store_frame(&frame->arch);
  359. }
  360. if (!_DkGenericSignalHandle(event, 0, frame, NULL)
  361. && event != PAL_EVENT_RESUME)
  362. _DkThreadExit();
  363. }