db_exception.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_signal.c
  15. *
  16. * This file contains APIs to set up handlers of exceptions issued by the
  17. * host, and the methods to pass the exceptions to the upcalls.
  18. */
  19. #include "pal_defs.h"
  20. #include "pal_linux_defs.h"
  21. #include "pal.h"
  22. #include "pal_internal.h"
  23. #include "pal_linux.h"
  24. #include "pal_error.h"
  25. #include "pal_security.h"
  26. #include "api.h"
  27. #include "ecall_types.h"
  28. #include <atomic.h>
  29. #include <sigset.h>
  30. #include <linux/signal.h>
  31. #include <ucontext.h>
  32. typedef struct exception_event {
  33. PAL_IDX event_num;
  34. PAL_CONTEXT * context;
  35. struct pal_frame * frame;
  36. } PAL_EVENT;
  37. static void _DkGenericEventTrigger (PAL_IDX event_num, PAL_EVENT_HANDLER upcall,
  38. PAL_NUM arg, struct pal_frame * frame,
  39. PAL_CONTEXT * context)
  40. {
  41. struct exception_event event;
  42. event.event_num = event_num;
  43. event.context = context;
  44. event.frame = frame;
  45. (*upcall) ((PAL_PTR) &event, arg, context);
  46. }
  47. static bool
  48. _DkGenericSignalHandle (int event_num, PAL_NUM arg, struct pal_frame * frame,
  49. PAL_CONTEXT * context)
  50. {
  51. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(event_num);
  52. if (upcall) {
  53. _DkGenericEventTrigger(event_num, upcall, arg, frame, context);
  54. return true;
  55. }
  56. return false;
  57. }
  58. #define ADDR_IN_PAL(addr) \
  59. ((void*)(addr) > TEXT_START && (void*)(addr) < TEXT_END)
  60. static struct pal_frame * get_frame (sgx_cpu_context_t * uc)
  61. {
  62. unsigned long rbp;
  63. if (uc) {
  64. unsigned long rip = uc->rip;
  65. rbp = uc->rbp;
  66. if (!ADDR_IN_PAL(rip))
  67. return NULL;
  68. } else {
  69. __asm__ volatile ("movq %%rbp, %0" : "=r"(rbp) :: "memory");
  70. }
  71. while (ADDR_IN_PAL(((unsigned long *) rbp)[1]))
  72. rbp = *(unsigned long *) rbp;
  73. struct pal_frame * frame = (struct pal_frame *) rbp - 1;
  74. for (int i = 0 ; i < 8 ; i++) {
  75. if (frame->identifier == PAL_FRAME_IDENTIFIER)
  76. return frame;
  77. frame = (struct pal_frame *) ((void *) frame - 8);
  78. }
  79. return NULL;
  80. }
  81. __asm__ (".type arch_exception_return_asm, @function;"
  82. "arch_exception_return_asm:"
  83. " pop %rax;"
  84. " pop %rbx;"
  85. " pop %rcx;"
  86. " pop %rdx;"
  87. " pop %rsi;"
  88. " pop %rdi;"
  89. " pop %r8;"
  90. " pop %r9;"
  91. " pop %r10;"
  92. " pop %r11;"
  93. " pop %r12;"
  94. " pop %r13;"
  95. " pop %r14;"
  96. " pop %r15;"
  97. " retq;");
  98. extern void arch_exception_return (void) __asm__ ("arch_exception_return_asm");
  99. void _DkExceptionRealHandler (int event, PAL_NUM arg, struct pal_frame * frame,
  100. PAL_CONTEXT * context)
  101. {
  102. if (frame) {
  103. frame = __alloca(sizeof(struct pal_frame));
  104. frame->identifier = PAL_FRAME_IDENTIFIER;
  105. frame->func = &_DkExceptionRealHandler;
  106. frame->funcname = "_DkExceptionRealHandler";
  107. store_register(rsp, frame->arch.rsp);
  108. store_register(rbp, frame->arch.rbp);
  109. unsigned long * last_frame = ((unsigned long *) frame->arch.rbp) + 1;
  110. last_frame[0] = (unsigned long) arch_exception_return;
  111. last_frame[1] = context->rax;
  112. last_frame[2] = context->rbx;
  113. last_frame[3] = context->rcx;
  114. last_frame[4] = context->rdx;
  115. last_frame[5] = context->rsi;
  116. last_frame[6] = context->rdi;
  117. last_frame[7] = context->r8;
  118. last_frame[8] = context->r9;
  119. last_frame[9] = context->r10;
  120. last_frame[10] = context->r11;
  121. last_frame[11] = context->r12;
  122. last_frame[12] = context->r13;
  123. last_frame[13] = context->r14;
  124. last_frame[14] = context->r15;
  125. last_frame[15] = context->rip;
  126. }
  127. _DkGenericSignalHandle(event, arg, frame, context);
  128. }
  129. /*
  130. * return value:
  131. * true: #UD is handled.
  132. * the execution can be continued without propagating #UD.
  133. * false: #UD is not handled.
  134. * the exception needs to be raised up to LibOS or user application.
  135. */
  136. static bool handle_ud(sgx_cpu_context_t * uc)
  137. {
  138. uint8_t * instr = (uint8_t *) uc->rip;
  139. if (instr[0] == 0xcc) { /* skip int 3 */
  140. uc->rip++;
  141. return true;
  142. } else if (instr[0] == 0x0f && instr[1] == 0xa2) {
  143. /* cpuid */
  144. unsigned int values[4];
  145. if (!_DkCpuIdRetrieve(uc->rax & 0xffffffff,
  146. uc->rcx & 0xffffffff, values)) {
  147. uc->rip += 2;
  148. uc->rax = values[0];
  149. uc->rbx = values[1];
  150. uc->rcx = values[2];
  151. uc->rdx = values[3];
  152. return true;
  153. }
  154. } else if (instr[0] == 0x0f && instr[1] == 0x31) {
  155. /* rdtsc */
  156. uc->rip += 2;
  157. uc->rdx = 0;
  158. uc->rax = 0;
  159. return true;
  160. } else if (instr[0] == 0x0f && instr[1] == 0x05) {
  161. /* syscall: LibOS may know how to handle this */
  162. return false;
  163. }
  164. SGX_DBG(DBG_E, "Unknown or illegal instruction at RIP 0x%016lx\n", uc->rip);
  165. return false;
  166. }
  167. void _DkExceptionHandler (unsigned int exit_info, sgx_cpu_context_t * uc)
  168. {
  169. union {
  170. sgx_arch_exit_info_t info;
  171. unsigned int intval;
  172. } ei = { .intval = exit_info };
  173. int event_num;
  174. if (!ei.info.valid) {
  175. event_num = exit_info;
  176. } else {
  177. switch (ei.info.vector) {
  178. case SGX_EXCEPTION_VECTOR_BR:
  179. event_num = PAL_EVENT_NUM_BOUND;
  180. break;
  181. case SGX_EXCEPTION_VECTOR_UD:
  182. if (handle_ud(uc)) {
  183. restore_sgx_context(uc);
  184. return;
  185. }
  186. event_num = PAL_EVENT_ILLEGAL;
  187. break;
  188. case SGX_EXCEPTION_VECTOR_DE:
  189. case SGX_EXCEPTION_VECTOR_MF:
  190. case SGX_EXCEPTION_VECTOR_XM:
  191. event_num = PAL_EVENT_ARITHMETIC_ERROR;
  192. break;
  193. case SGX_EXCEPTION_VECTOR_AC:
  194. event_num = PAL_EVENT_MEMFAULT;
  195. break;
  196. case SGX_EXCEPTION_VECTOR_DB:
  197. case SGX_EXCEPTION_VECTOR_BP:
  198. default:
  199. restore_sgx_context(uc);
  200. return;
  201. }
  202. }
  203. if (ADDR_IN_PAL(uc->rip) &&
  204. /* event isn't asynchronous */
  205. (event_num != PAL_EVENT_QUIT &&
  206. event_num != PAL_EVENT_SUSPEND &&
  207. event_num != PAL_EVENT_RESUME)) {
  208. printf("*** An unexpected AEX vector occurred inside PAL. "
  209. "Exiting the thread. *** \n"
  210. "(vector = 0x%x, type = 0x%x valid = %d, RIP = +0x%08lx)\n"
  211. "rax: 0x%08lx rcx: 0x%08lx rdx: 0x%08lx rbx: 0x%08lx\n"
  212. "rsp: 0x%08lx rbp: 0x%08lx rsi: 0x%08lx rdi: 0x%08lx\n"
  213. "r8 : 0x%08lx r9 : 0x%08lx r10: 0x%08lx r11: 0x%08lx\n"
  214. "r12: 0x%08lx r13: 0x%08lx r14: 0x%08lx r15: 0x%08lx\n"
  215. "rflags: 0x%08lx rip: 0x%08lx\n",
  216. ei.info.vector, ei.info.exit_type, ei.info.valid,
  217. uc->rip - (uintptr_t) TEXT_START,
  218. uc->rax, uc->rcx, uc->rdx, uc->rbx,
  219. uc->rsp, uc->rbp, uc->rsi, uc->rdi,
  220. uc->r8, uc->r9, uc->r10, uc->r11,
  221. uc->r12, uc->r13, uc->r14, uc->r15,
  222. uc->rflags, uc->rip);
  223. #ifdef DEBUG
  224. printf("pausing for debug\n");
  225. while (true)
  226. __asm__ volatile("pause");
  227. #endif
  228. _DkThreadExit();
  229. }
  230. PAL_CONTEXT ctx;
  231. memset(&ctx, 0, sizeof(ctx));
  232. ctx.rax = uc->rax;
  233. ctx.rbx = uc->rbx;
  234. ctx.rcx = uc->rcx;
  235. ctx.rdx = uc->rdx;
  236. ctx.rsp = uc->rsp;
  237. ctx.rbp = uc->rbp;
  238. ctx.rsi = uc->rsi;
  239. ctx.rdi = uc->rdi;
  240. ctx.r8 = uc->r8;
  241. ctx.r9 = uc->r9;
  242. ctx.r10 = uc->r10;
  243. ctx.r11 = uc->r11;
  244. ctx.r12 = uc->r12;
  245. ctx.r13 = uc->r13;
  246. ctx.r14 = uc->r14;
  247. ctx.r15 = uc->r15;
  248. ctx.efl = uc->rflags;
  249. ctx.rip = uc->rip;
  250. struct pal_frame * frame = get_frame(uc);
  251. PAL_NUM arg = 0;
  252. switch (event_num) {
  253. case PAL_EVENT_ILLEGAL:
  254. arg = uc->rip;
  255. break;
  256. case PAL_EVENT_MEMFAULT:
  257. /* TODO
  258. * SGX1 doesn't provide fault address.
  259. * SGX2 gives providing page. (lower address bits are masked)
  260. */
  261. break;
  262. default:
  263. /* nothing */
  264. break;
  265. }
  266. _DkExceptionRealHandler(event_num, arg, frame, &ctx);
  267. restore_sgx_context(uc);
  268. }
  269. void _DkRaiseFailure (int error)
  270. {
  271. PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(PAL_EVENT_FAILURE);
  272. if (!upcall)
  273. return;
  274. PAL_EVENT event;
  275. event.event_num = PAL_EVENT_FAILURE;
  276. event.context = NULL;
  277. event.frame = NULL;
  278. (*upcall) ((PAL_PTR) &event, error, NULL);
  279. }
  280. void _DkExceptionReturn (void * event)
  281. {
  282. PAL_EVENT * e = event;
  283. PAL_CONTEXT * ctx = e->context;
  284. if (!ctx) {
  285. struct pal_frame * frame = e->frame;
  286. if (!frame)
  287. return;
  288. __clear_frame(frame);
  289. arch_restore_frame(&frame->arch);
  290. __asm__ volatile (
  291. "xor %%rax, %%rax\r\n"
  292. "leaveq\r\n"
  293. "retq\r\n" ::: "memory");
  294. }
  295. // Allocate sgx_cpu_context_t just below the "normal" stack (honoring the red
  296. // zone) and then copy the content of ctx there. This is needed by
  297. // restore_sgx_context.
  298. sgx_cpu_context_t * uc = (void *)ctx->rsp - sizeof(sgx_cpu_context_t) - RED_ZONE_SIZE;
  299. uc->rax = ctx->rax;
  300. uc->rbx = ctx->rbx;
  301. uc->rcx = ctx->rcx;
  302. uc->rdx = ctx->rdx;
  303. uc->rsp = ctx->rsp;
  304. uc->rbp = ctx->rbp;
  305. uc->rsi = ctx->rsi;
  306. uc->rdi = ctx->rdi;
  307. uc->r8 = ctx->r8;
  308. uc->r9 = ctx->r9;
  309. uc->r10 = ctx->r10;
  310. uc->r11 = ctx->r11;
  311. uc->r12 = ctx->r12;
  312. uc->r13 = ctx->r13;
  313. uc->r14 = ctx->r14;
  314. uc->r15 = ctx->r15;
  315. uc->rflags = ctx->efl;
  316. uc->rip = ctx->rip;
  317. restore_sgx_context(uc);
  318. }
  319. void _DkHandleExternalEvent (PAL_NUM event, sgx_cpu_context_t * uc)
  320. {
  321. struct pal_frame * frame = get_frame(uc);
  322. if (event == PAL_EVENT_RESUME &&
  323. frame && frame->func == DkObjectsWaitAny)
  324. return;
  325. if (!frame) {
  326. frame = __alloca(sizeof(struct pal_frame));
  327. frame->identifier = PAL_FRAME_IDENTIFIER;
  328. frame->func = &_DkHandleExternalEvent;
  329. frame->funcname = "_DkHandleExternalEvent";
  330. arch_store_frame(&frame->arch);
  331. }
  332. /* We only end up in _DkHandleExternalEvent() if interrupted during
  333. * host syscall; Dk* function will be unwound, so we must inform LibOS
  334. * layer that PAL was interrupted (by setting PAL_ERRNO). */
  335. _DkRaiseFailure(PAL_ERROR_INTERRUPTED);
  336. if (!_DkGenericSignalHandle(event, 0, frame, NULL)
  337. && event != PAL_EVENT_RESUME)
  338. _DkThreadExit();
  339. }