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 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. /*
  161. * return value:
  162. * true: #UD is handled.
  163. * the execution can be continued without propagating #UD.
  164. * false: #UD is not handled.
  165. * the exception needs to be raised up to LibOS or user application.
  166. */
  167. static bool handle_ud(sgx_context_t * uc)
  168. {
  169. uint8_t * instr = (uint8_t *) uc->rip;
  170. if (instr[0] == 0xcc) { /* skip int 3 */
  171. uc->rip++;
  172. return true;
  173. } else if (instr[0] == 0x0f && instr[1] == 0xa2) {
  174. /* cpuid */
  175. unsigned int values[4];
  176. if (!_DkCpuIdRetrieve(uc->rax & 0xffffffff,
  177. uc->rcx & 0xffffffff, values)) {
  178. uc->rip += 2;
  179. uc->rax = values[0];
  180. uc->rbx = values[1];
  181. uc->rcx = values[2];
  182. uc->rdx = values[3];
  183. return true;
  184. }
  185. } else if (instr[0] == 0x0f && instr[1] == 0x31) {
  186. /* rdtsc */
  187. uc->rip += 2;
  188. uc->rdx = 0;
  189. uc->rax = 0;
  190. return true;
  191. } else if (instr[0] == 0x0f && instr[1] == 0x05) {
  192. /* syscall: LibOS may know how to handle this */
  193. return false;
  194. }
  195. SGX_DBG(DBG_E, "Unknown or illegal instruction at RIP 0x%016lx\n", uc->rip);
  196. return false;
  197. }
  198. void _DkExceptionHandler (unsigned int exit_info, sgx_context_t * uc)
  199. {
  200. union {
  201. sgx_arch_exitinfo_t info;
  202. unsigned int intval;
  203. } ei = { .intval = exit_info };
  204. int event_num;
  205. PAL_CONTEXT * ctx;
  206. if (!ei.info.valid) {
  207. event_num = exit_info;
  208. } else {
  209. switch (ei.info.vector) {
  210. case SGX_EXCEPTION_VECTOR_BR:
  211. event_num = PAL_EVENT_NUM_BOUND;
  212. break;
  213. case SGX_EXCEPTION_VECTOR_UD:
  214. if (handle_ud(uc)) {
  215. restore_sgx_context(uc);
  216. return;
  217. }
  218. event_num = PAL_EVENT_ILLEGAL;
  219. break;
  220. case SGX_EXCEPTION_VECTOR_DE:
  221. case SGX_EXCEPTION_VECTOR_MF:
  222. case SGX_EXCEPTION_VECTOR_XM:
  223. event_num = PAL_EVENT_ARITHMETIC_ERROR;
  224. break;
  225. case SGX_EXCEPTION_VECTOR_AC:
  226. event_num = PAL_EVENT_MEMFAULT;
  227. break;
  228. case SGX_EXCEPTION_VECTOR_DB:
  229. case SGX_EXCEPTION_VECTOR_BP:
  230. default:
  231. restore_sgx_context(uc);
  232. return;
  233. }
  234. }
  235. if (ADDR_IN_PAL(uc->rip) &&
  236. /* event isn't asynchronous */
  237. (event_num != PAL_EVENT_QUIT &&
  238. event_num != PAL_EVENT_SUSPEND &&
  239. event_num != PAL_EVENT_RESUME)) {
  240. printf("*** An unexpected AEX vector occurred inside PAL. "
  241. "Exiting the thread. *** \n"
  242. "(vector = 0x%x, type = 0x%x valid = %d, RIP = +0x%08lx)\n"
  243. "rax: 0x%08lx rcx: 0x%08lx rdx: 0x%08lx rbx: 0x%08lx\n"
  244. "rsp: 0x%08lx rbp: 0x%08lx rsi: 0x%08lx rdi: 0x%08lx\n"
  245. "r8 : 0x%08lx r9 : 0x%08lx r10: 0x%08lx r11: 0x%08lx\n"
  246. "r12: 0x%08lx r13: 0x%08lx r14: 0x%08lx r15: 0x%08lx\n"
  247. "rflags: 0x%08lx rip: 0x%08lx\n",
  248. ei.info.vector, ei.info.type, ei.info.valid,
  249. uc->rip - (uintptr_t) TEXT_START,
  250. uc->rax, uc->rcx, uc->rdx, uc->rbx,
  251. uc->rsp, uc->rbp, uc->rsi, uc->rdi,
  252. uc->r8, uc->r9, uc->r10, uc->r11,
  253. uc->r12, uc->r13, uc->r14, uc->r15,
  254. uc->rflags, uc->rip);
  255. #ifdef DEBUG
  256. printf("pausing for debug\n");
  257. while (true)
  258. __asm__ volatile("pause");
  259. #endif
  260. _DkThreadExit();
  261. }
  262. ctx = __alloca(sizeof(PAL_CONTEXT));
  263. memset(ctx, 0, sizeof(PAL_CONTEXT));
  264. ctx->rax = uc->rax;
  265. ctx->rbx = uc->rbx;
  266. ctx->rcx = uc->rcx;
  267. ctx->rdx = uc->rdx;
  268. ctx->rsp = uc->rsp;
  269. ctx->rbp = uc->rbp;
  270. ctx->rsi = uc->rsi;
  271. ctx->rdi = uc->rdi;
  272. ctx->r8 = uc->r8;
  273. ctx->r9 = uc->r9;
  274. ctx->r10 = uc->r10;
  275. ctx->r11 = uc->r11;
  276. ctx->r12 = uc->r12;
  277. ctx->r13 = uc->r13;
  278. ctx->r14 = uc->r14;
  279. ctx->r15 = uc->r15;
  280. ctx->efl = uc->rflags;
  281. ctx->rip = uc->rip;
  282. struct pal_frame * frame = get_frame(uc);
  283. PAL_NUM arg = 0;
  284. switch (event_num) {
  285. case PAL_EVENT_ILLEGAL:
  286. arg = uc->rip;
  287. break;
  288. case PAL_EVENT_MEMFAULT:
  289. /* TODO
  290. * SGX1 doesn't provide fault address.
  291. * SGX2 gives providing page. (lower address bits are masked)
  292. */
  293. break;
  294. default:
  295. /* nothing */
  296. break;
  297. }
  298. _DkExceptionRealHandler(event_num, arg, frame, ctx);
  299. restore_sgx_context(uc);
  300. }
  301. void _DkRaiseFailure (int error)
  302. {
  303. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(PAL_EVENT_FAILURE);
  304. if (!upcall)
  305. return;
  306. PAL_EVENT event;
  307. event.event_num = PAL_EVENT_FAILURE;
  308. event.context = NULL;
  309. event.frame = NULL;
  310. (*upcall) ((PAL_PTR) &event, error, NULL);
  311. }
  312. void _DkExceptionReturn (void * event)
  313. {
  314. PAL_EVENT * e = event;
  315. sgx_context_t uc;
  316. PAL_CONTEXT * ctx = e->context;
  317. if (!ctx) {
  318. struct pal_frame * frame = e->frame;
  319. if (!frame)
  320. return;
  321. __clear_frame(frame);
  322. arch_restore_frame(&frame->arch);
  323. __asm__ volatile (
  324. "xor %%rax, %%rax\r\n"
  325. "leaveq\r\n"
  326. "retq\r\n" ::: "memory");
  327. }
  328. uc.rax = ctx->rax;
  329. uc.rbx = ctx->rbx;
  330. uc.rcx = ctx->rcx;
  331. uc.rdx = ctx->rdx;
  332. uc.rsp = ctx->rsp;
  333. uc.rbp = ctx->rbp;
  334. uc.rsi = ctx->rsi;
  335. uc.rdi = ctx->rdi;
  336. uc.r8 = ctx->r8;
  337. uc.r9 = ctx->r9;
  338. uc.r10 = ctx->r10;
  339. uc.r11 = ctx->r11;
  340. uc.r12 = ctx->r12;
  341. uc.r13 = ctx->r13;
  342. uc.r14 = ctx->r14;
  343. uc.r15 = ctx->r15;
  344. uc.rflags = ctx->efl;
  345. uc.rip = ctx->rip;
  346. restore_sgx_context(&uc);
  347. }
  348. void _DkHandleExternalEvent (PAL_NUM event, sgx_context_t * uc)
  349. {
  350. struct pal_frame * frame = get_frame(uc);
  351. if (event == PAL_EVENT_RESUME &&
  352. frame && frame->func == DkObjectsWaitAny)
  353. return;
  354. if (!frame) {
  355. frame = __alloca(sizeof(struct pal_frame));
  356. frame->identifier = PAL_FRAME_IDENTIFIER;
  357. frame->func = &_DkHandleExternalEvent;
  358. frame->funcname = "_DkHandleExternalEvent";
  359. arch_store_frame(&frame->arch);
  360. }
  361. /* We only end up in _DkHandleExternalEvent() if interrupted during
  362. * host syscall; Dk* function will be unwound, so we must inform LibOS
  363. * layer that PAL was interrupted (by setting PAL_ERRNO). */
  364. _DkRaiseFailure(PAL_ERROR_INTERRUPTED);
  365. if (!_DkGenericSignalHandle(event, 0, frame, NULL)
  366. && event != PAL_EVENT_RESUME)
  367. _DkThreadExit();
  368. }