db_exception.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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_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. typedef struct exception_event {
  35. PAL_IDX event_num;
  36. PAL_CONTEXT * context;
  37. struct pal_frame * frame;
  38. } PAL_EVENT;
  39. static void _DkGenericEventTrigger (PAL_IDX event_num, PAL_EVENT_HANDLER upcall,
  40. PAL_NUM arg, struct pal_frame * frame,
  41. PAL_CONTEXT * context)
  42. {
  43. struct exception_event event;
  44. event.event_num = event_num;
  45. event.context = context;
  46. event.frame = frame;
  47. (*upcall) ((PAL_PTR) &event, arg, context);
  48. }
  49. static bool
  50. _DkGenericSignalHandle (int event_num, PAL_NUM arg, struct pal_frame * frame,
  51. PAL_CONTEXT * context)
  52. {
  53. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(event_num);
  54. if (upcall) {
  55. _DkGenericEventTrigger(event_num, upcall, arg, frame, context);
  56. return true;
  57. }
  58. return false;
  59. }
  60. #define ADDR_IN_PAL(addr) \
  61. ((void *) (addr) > TEXT_START && (void *) (addr) < TEXT_END)
  62. static struct pal_frame * get_frame (sgx_context_t * uc)
  63. {
  64. unsigned long rbp;
  65. if (uc) {
  66. unsigned long rip = uc->rip;
  67. rbp = uc->rbp;
  68. if (!ADDR_IN_PAL(rip))
  69. return NULL;
  70. } else {
  71. __asm__ volatile ("movq %%rbp, %0" : "=r"(rbp) :: "memory");
  72. }
  73. while (ADDR_IN_PAL(((unsigned long *) rbp)[1]))
  74. rbp = *(unsigned long *) rbp;
  75. struct pal_frame * frame = (struct pal_frame *) rbp - 1;
  76. for (int i = 0 ; i < 8 ; i++) {
  77. if (frame->identifier == PAL_FRAME_IDENTIFIER)
  78. return frame;
  79. frame = (struct pal_frame *) ((void *) frame - 8);
  80. }
  81. return NULL;
  82. }
  83. __asm__ (".type arch_exception_return_asm, @function;"
  84. "arch_exception_return_asm:"
  85. " pop %rax;"
  86. " pop %rbx;"
  87. " pop %rcx;"
  88. " pop %rdx;"
  89. " pop %rsi;"
  90. " pop %rdi;"
  91. " pop %r8;"
  92. " pop %r9;"
  93. " pop %r10;"
  94. " pop %r11;"
  95. " pop %r12;"
  96. " pop %r13;"
  97. " pop %r14;"
  98. " pop %r15;"
  99. " retq;");
  100. extern void arch_exception_return (void) __asm__ ("arch_exception_return_asm");
  101. void _DkExceptionRealHandler (int event, PAL_NUM arg, struct pal_frame * frame,
  102. PAL_CONTEXT * context)
  103. {
  104. if (frame) {
  105. frame = __alloca(sizeof(struct pal_frame));
  106. frame->identifier = PAL_FRAME_IDENTIFIER;
  107. frame->func = &_DkExceptionRealHandler;
  108. frame->funcname = "_DkExceptionRealHandler";
  109. store_register(rsp, frame->arch.rsp);
  110. store_register(rbp, frame->arch.rbp);
  111. unsigned long * last_frame = ((unsigned long *) frame->arch.rbp) + 1;
  112. last_frame[0] = (unsigned long) arch_exception_return;
  113. last_frame[1] = context->rax;
  114. last_frame[2] = context->rbx;
  115. last_frame[3] = context->rcx;
  116. last_frame[4] = context->rdx;
  117. last_frame[5] = context->rsi;
  118. last_frame[6] = context->rdi;
  119. last_frame[7] = context->r8;
  120. last_frame[8] = context->r9;
  121. last_frame[9] = context->r10;
  122. last_frame[10] = context->r11;
  123. last_frame[11] = context->r12;
  124. last_frame[12] = context->r13;
  125. last_frame[13] = context->r14;
  126. last_frame[14] = context->r15;
  127. last_frame[15] = context->rip;
  128. }
  129. _DkGenericSignalHandle(event, arg, frame, context);
  130. }
  131. void restore_sgx_context (sgx_context_t * uc)
  132. {
  133. /* prepare the return address */
  134. uc->rsp -= 8;
  135. *(uint64_t *) uc->rsp = uc->rip;
  136. /* now pop the stack */
  137. __asm__ volatile (
  138. "mov %0, %%rsp\n"
  139. "pop %%rax\n"
  140. "pop %%rcx\n"
  141. "pop %%rdx\n"
  142. "pop %%rbx\n"
  143. "add $8, %%rsp\n" /* don't pop RSP yet */
  144. "pop %%rbp\n"
  145. "pop %%rsi\n"
  146. "pop %%rdi\n"
  147. "pop %%r8\n"
  148. "pop %%r9\n"
  149. "pop %%r10\n"
  150. "pop %%r11\n"
  151. "pop %%r12\n"
  152. "pop %%r13\n"
  153. "pop %%r14\n"
  154. "pop %%r15\n"
  155. "popfq\n"
  156. "mov -104(%%rsp), %%rsp\n"
  157. "ret\n"
  158. :: "r"(uc) : "memory");
  159. }
  160. void _DkExceptionHandler (unsigned int exit_info, sgx_context_t * uc)
  161. {
  162. union {
  163. sgx_arch_exitinfo_t info;
  164. int intval;
  165. } ei = { .intval = exit_info };
  166. int event_num;
  167. PAL_CONTEXT * ctx;
  168. if (!ei.info.valid) {
  169. event_num = exit_info;
  170. goto handle_event;
  171. }
  172. if (ei.info.vector == SGX_EXCEPTION_VECTOR_UD) {
  173. unsigned char * instr = (unsigned char *) uc->rip;
  174. if (instr[0] == 0xcc) { /* skip int 3 */
  175. uc->rip++;
  176. restore_sgx_context(uc);
  177. return;
  178. }
  179. if (instr[0] == 0x0f && instr[1] == 0xa2) {
  180. unsigned int values[4];
  181. if (!_DkCpuIdRetrieve(uc->rax & 0xffffffff,
  182. uc->rcx & 0xffffffff, values)) {
  183. uc->rip += 2;
  184. uc->rax = values[0];
  185. uc->rbx = values[1];
  186. uc->rcx = values[2];
  187. uc->rdx = values[3];
  188. restore_sgx_context(uc);
  189. return;
  190. }
  191. }
  192. if (instr[0] == 0x0f && instr[1] == 0x31) {
  193. uc->rip += 2;
  194. uc->rdx = 0;
  195. uc->rax = 0;
  196. restore_sgx_context(uc);
  197. return;
  198. }
  199. SGX_DBG(DBG_E, "Illegal instruction executed in enclave\n");
  200. ocall_exit(1);
  201. }
  202. switch (ei.info.vector) {
  203. case SGX_EXCEPTION_VECTOR_DE:
  204. event_num = PAL_EVENT_ARITHMETIC_ERROR;
  205. break;
  206. case SGX_EXCEPTION_VECTOR_AC:
  207. event_num = PAL_EVENT_MEMFAULT;
  208. break;
  209. default:
  210. restore_sgx_context(uc);
  211. return;
  212. }
  213. handle_event:
  214. ctx = __alloca(sizeof(PAL_CONTEXT));
  215. memset(ctx, 0, sizeof(PAL_CONTEXT));
  216. ctx->rax = uc->rax;
  217. ctx->rbx = uc->rbx;
  218. ctx->rcx = uc->rcx;
  219. ctx->rdx = uc->rdx;
  220. ctx->rsp = uc->rsp;
  221. ctx->rbp = uc->rbp;
  222. ctx->rsi = uc->rsi;
  223. ctx->rdi = uc->rdi;
  224. ctx->r8 = uc->r8;
  225. ctx->r9 = uc->r9;
  226. ctx->r10 = uc->r10;
  227. ctx->r11 = uc->r11;
  228. ctx->r12 = uc->r12;
  229. ctx->r13 = uc->r13;
  230. ctx->r14 = uc->r14;
  231. ctx->r15 = uc->r15;
  232. ctx->efl = uc->rflags;
  233. ctx->rip = uc->rip;
  234. struct pal_frame * frame = get_frame(uc);
  235. PAL_NUM arg = 0;
  236. _DkExceptionRealHandler(event_num, arg, frame, ctx);
  237. restore_sgx_context(uc);
  238. }
  239. void _DkRaiseFailure (int error)
  240. {
  241. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(PAL_EVENT_FAILURE);
  242. if (!upcall)
  243. return;
  244. PAL_EVENT event;
  245. event.event_num = PAL_EVENT_FAILURE;
  246. event.context = NULL;
  247. event.frame = NULL;
  248. (*upcall) ((PAL_PTR) &event, error, NULL);
  249. }
  250. void _DkExceptionReturn (void * event)
  251. {
  252. PAL_EVENT * e = event;
  253. sgx_context_t uc;
  254. PAL_CONTEXT * ctx = e->context;
  255. if (!ctx) {
  256. struct pal_frame * frame = e->frame;
  257. if (!frame)
  258. return;
  259. __clear_frame(frame);
  260. arch_restore_frame(&frame->arch);
  261. __asm__ volatile (
  262. "xor %%rax, %%rax\r\n"
  263. "leaveq\r\n"
  264. "retq\r\n" ::: "memory");
  265. }
  266. uc.rax = ctx->rax;
  267. uc.rbx = ctx->rbx;
  268. uc.rcx = ctx->rcx;
  269. uc.rdx = ctx->rdx;
  270. uc.rsp = ctx->rsp;
  271. uc.rbp = ctx->rbp;
  272. uc.rsi = ctx->rsi;
  273. uc.rdi = ctx->rdi;
  274. uc.r8 = ctx->r8;
  275. uc.r9 = ctx->r9;
  276. uc.r10 = ctx->r10;
  277. uc.r11 = ctx->r11;
  278. uc.r12 = ctx->r12;
  279. uc.r13 = ctx->r13;
  280. uc.r14 = ctx->r14;
  281. uc.r15 = ctx->r15;
  282. uc.rflags = ctx->efl;
  283. uc.rip = ctx->rip;
  284. restore_sgx_context(&uc);
  285. }
  286. void _DkHandleExternalEvent (PAL_NUM event, sgx_context_t * uc)
  287. {
  288. struct pal_frame * frame = get_frame(uc);
  289. if (event == PAL_EVENT_RESUME &&
  290. frame && frame->func == DkObjectsWaitAny)
  291. return;
  292. if (!frame) {
  293. frame = __alloca(sizeof(struct pal_frame));
  294. frame->identifier = PAL_FRAME_IDENTIFIER;
  295. frame->func = &_DkHandleExternalEvent;
  296. frame->funcname = "_DkHandleExternalEvent";
  297. arch_store_frame(&frame->arch);
  298. }
  299. if (!_DkGenericSignalHandle(event, 0, frame, NULL)
  300. && event != PAL_EVENT_RESUME)
  301. _DkThreadExit();
  302. }