db_exception.c 9.8 KB

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