Skip to content

Cleanup SGX entry code #69040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions src/libstd/sys/sgx/abi/entry.S
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ IMAGE_BASE:

/* We can store a bunch of data in the gap between MXCSR and the XSAVE header */

/* MXCSR initialization value for ABI */
.Lmxcsr_init:
.int 0x1f80

/* x87 FPU control word initialization value for ABI */
.Lfpucw_init:
.int 0x037f
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is setting one of the reserved bits (6th) to 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the default from the Intel SDM, Vol. 1, §8.1.5


/* The following symbols point at read-only data that will be filled in by the */
/* post-linker. */

Expand Down Expand Up @@ -134,6 +142,19 @@ elf_entry:
ud2 /* should not be reached */
/* end elf_entry */

/* This code needs to be called *after* the enclave stack has been setup. */
/* There are 3 places where this needs to happen, so this is put in a macro. */
.macro entry_sanitize_final
/* Sanitize rflags received from user */
/* - DF flag: x86-64 ABI requires DF to be unset at function entry/exit */
/* - AC flag: AEX on misaligned memory accesses leaks side channel info */
pushfq
andq $~0x40400, (%rsp)
popfq
bt $0,.Laborted(%rip)
jc .Lreentry_panic
.endm

.text
.global sgx_entry
.type sgx_entry,function
Expand All @@ -150,25 +171,18 @@ sgx_entry:
stmxcsr %gs:tcsls_user_mxcsr
fnstcw %gs:tcsls_user_fcw

/* reset user state */
/* - DF flag: x86-64 ABI requires DF to be unset at function entry/exit */
/* - AC flag: AEX on misaligned memory accesses leaks side channel info */
pushfq
andq $~0x40400, (%rsp)
popfq

/* check for debug buffer pointer */
testb $0xff,DEBUG(%rip)
jz .Lskip_debug_init
mov %r10,%gs:tcsls_debug_panic_buf_ptr
.Lskip_debug_init:
/* check for abort */
bt $0,.Laborted(%rip)
jc .Lreentry_panic
/* check if returning from usercall */
mov %gs:tcsls_last_rsp,%r11
test %r11,%r11
jnz .Lusercall_ret
/* reset user state */
ldmxcsr .Lmxcsr_init(%rip)
fldcw .Lfpucw_init(%rip)
/* setup stack */
mov %gs:tcsls_tos,%rsp /* initially, RSP is not set to the correct value */
/* here. This is fixed below under "adjust stack". */
Expand All @@ -179,6 +193,7 @@ sgx_entry:
lea IMAGE_BASE(%rip),%rax
add %rax,%rsp
mov %rsp,%gs:tcsls_tos
entry_sanitize_final
/* call tcs_init */
/* store caller-saved registers in callee-saved registers */
mov %rdi,%rbx
Expand All @@ -194,7 +209,10 @@ sgx_entry:
mov %r13,%rdx
mov %r14,%r8
mov %r15,%r9
jmp .Lafter_init
.Lskip_init:
entry_sanitize_final
.Lafter_init:
/* call into main entry point */
load_tcsls_flag_secondary_bool cx /* RCX = entry() argument: secondary: bool */
call entry /* RDI, RSI, RDX, R8, R9 passed in from userspace */
Expand Down Expand Up @@ -295,6 +313,7 @@ usercall:
ldmxcsr (%rsp)
fldcw 4(%rsp)
add $8, %rsp
entry_sanitize_final
pop %rbx
pop %rbp
pop %r12
Expand Down