|
@@ -165,7 +165,7 @@ enclave_entry:
|
|
|
je 1f
|
|
|
movq %rax, %rsi
|
|
|
1:
|
|
|
- subq $SGX_CONTEXT_SIZE, %rsi
|
|
|
+ subq $(SGX_CONTEXT_SIZE + RED_ZONE_SIZE), %rsi
|
|
|
|
|
|
# we have exitinfo in RDI, swap with the one on GPR
|
|
|
# and dump into the context
|
|
@@ -209,9 +209,13 @@ enclave_entry:
|
|
|
movq SGX_GPR_RIP(%rbx), %rdi
|
|
|
movq %rdi, SGX_CONTEXT_RIP(%rsi)
|
|
|
|
|
|
- movq %rsi, SGX_GPR_RSP(%rbx)
|
|
|
+ # Pass pointer to sgx_context_t to _DkExceptionHandler
|
|
|
movq %rsi, SGX_GPR_RSI(%rbx)
|
|
|
|
|
|
+ # Align the stack for _DkExceptionHandler
|
|
|
+ andq $STACK_ALIGN, %rsi
|
|
|
+ movq %rsi, SGX_GPR_RSP(%rbx)
|
|
|
+
|
|
|
# new RIP is the exception handler
|
|
|
leaq _DkExceptionHandler(%rip), %rdi
|
|
|
movq %rdi, SGX_GPR_RIP(%rbx)
|
|
@@ -435,3 +439,53 @@ wrfsbase:
|
|
|
|
|
|
.cfi_endproc
|
|
|
.size wrfsbase, .-wrfsbase
|
|
|
+
|
|
|
+/*
|
|
|
+ * Restore an sgx_context_t as generated by .Lhandle_exception. Execution will
|
|
|
+ * continue as specified by the rip in the context.
|
|
|
+ *
|
|
|
+ * It is required that:
|
|
|
+ *
|
|
|
+ * %rdi == *(%rdi + SGX_CONTEXT_RSP) - (SGX_CONTEXT_SIZE + RED_ZONE_SIZE)
|
|
|
+ *
|
|
|
+ * This holds for the original sgx_context allocated by .Lhandle_exception.
|
|
|
+ * restore_sgx_context is a safe wrapper which checks this.
|
|
|
+ */
|
|
|
+ .global _restore_sgx_context
|
|
|
+ .type _restore_sgx_context, @function
|
|
|
+
|
|
|
+_restore_sgx_context:
|
|
|
+ movq SGX_CONTEXT_RAX(%rdi), %rax
|
|
|
+ movq SGX_CONTEXT_RCX(%rdi), %rcx
|
|
|
+ movq SGX_CONTEXT_RDX(%rdi), %rdx
|
|
|
+ movq SGX_CONTEXT_RBX(%rdi), %rbx
|
|
|
+ # For %rsp see below.
|
|
|
+ movq SGX_CONTEXT_RBP(%rdi), %rbp
|
|
|
+ movq SGX_CONTEXT_RSI(%rdi), %rsi
|
|
|
+ # For %rdi see below.
|
|
|
+ movq SGX_CONTEXT_R8(%rdi), %r8
|
|
|
+ movq SGX_CONTEXT_R9(%rdi), %r9
|
|
|
+ movq SGX_CONTEXT_R10(%rdi), %r10
|
|
|
+ movq SGX_CONTEXT_R11(%rdi), %r11
|
|
|
+ movq SGX_CONTEXT_R12(%rdi), %r12
|
|
|
+ movq SGX_CONTEXT_R13(%rdi), %r13
|
|
|
+ movq SGX_CONTEXT_R14(%rdi), %r14
|
|
|
+ movq SGX_CONTEXT_R15(%rdi), %r15
|
|
|
+
|
|
|
+ # We need to make sure that %rsp - RED_ZONE_SIZE never points above
|
|
|
+ # anything we still need. Otherwise .Lhandle_exception might mess with
|
|
|
+ # it. SGX_CONTEXT_RDI - SGX_CONTEXT_RFLAGS <= RED_ZONE_SIZE, see
|
|
|
+ # sgx_arch.h.
|
|
|
+ leaq SGX_CONTEXT_RFLAGS(%rdi), %rsp
|
|
|
+ popfq # remember to not touch any flags after here
|
|
|
+
|
|
|
+ movq SGX_CONTEXT_RDI(%rdi), %rdi
|
|
|
+ # Now %rdi is restored so we need to use the stack to access the
|
|
|
+ # context.
|
|
|
+
|
|
|
+ # Now pop %rip and fix stack pointer in one operation (to avoid
|
|
|
+ # problems with nesting, see comment above). SGX_CONTEXT_RIP is
|
|
|
+ # directly after SGX_CONTEXT_RFLAGS, see sgx_arch.h. Note that retq
|
|
|
+ # decreases %rsp by 8 for the poped %rip additionaly to the passed
|
|
|
+ # offset.
|
|
|
+ retq $(SGX_CONTEXT_SIZE + RED_ZONE_SIZE - SGX_CONTEXT_RIP - 8)
|