Browse Source

[Pal/Linux-SGX] Save/restore callee-saved regs on sgx_{ecall,raise}

The EEXIT instruction does not restore registers of untrusted part of
the program. Thus, sgx_ecall() and sgx_raise() functions must save
callee-saved regs before EENTER and restore them after EEXIT. This
fixes the bug of a build with DEBUG=0 when the untrusted part of the
program uses RBP and other regs (in contrast to undefined DEBUG when
these registers are optimized away from the untrusted part).
Isaku Yamahata 4 years ago
parent
commit
4bcf3a6287
1 changed files with 17 additions and 8 deletions
  1. 17 8
      Pal/src/host/Linux-SGX/sgx_entry.S

+ 17 - 8
Pal/src/host/Linux-SGX/sgx_entry.S

@@ -9,13 +9,19 @@
 	.type sgx_ecall, @function
 
 sgx_ecall:
-	pushq %rbx
-
 	# put entry address in RDX
-	leaq sgx_entry(%rip), %rdx
+	leaq .Lsgx_entry(%rip), %rdx
 
 	# other arguments: RDI - code, RSI - ms
 
+.Ldo_ecall_callee_save:
+	pushq %rbx
+	pushq %rbp
+	pushq %r12
+	pushq %r13
+	pushq %r14
+	pushq %r15
+
 .Ldo_ecall:
 	# RBX has to be the TCS of the thread
 	movq %gs:PAL_TCB_URTS_TCS, %rbx
@@ -27,6 +33,12 @@ sgx_ecall:
 	ENCLU
 
 	# currently only ECALL_THREAD_RESET returns
+.Lafter_resume:
+	popq %r15
+	popq %r14
+	popq %r13
+	popq %r12
+	popq %rbp
 	popq %rbx
 	retq
 
@@ -41,12 +53,9 @@ async_exit_pointer:
 
 sgx_raise:
 	leaq .Lafter_resume(%rip), %rdx
-	jmp .Ldo_ecall
-
-.Lafter_resume:
-	retq
+	jmp .Ldo_ecall_callee_save
 
-sgx_entry:
+.Lsgx_entry:
 	# arguments: RDI - code, RSI - ms
 
 	.cfi_startproc