db_exception.c 9.9 KB

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