db_exception.c 11 KB

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