Skip to content

Commit a908716

Browse files
authored
Rollup merge of #69040 - jethrogb:jb/cleanup-sgx-entry, r=nagisa
Cleanup SGX entry code cc @aandyl
2 parents 5a800d7 + 71b9ed4 commit a908716

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

Diff for: src/libstd/sys/sgx/abi/entry.S

+29-10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ IMAGE_BASE:
3030

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

33+
/* MXCSR initialization value for ABI */
34+
.Lmxcsr_init:
35+
.int 0x1f80
36+
37+
/* x87 FPU control word initialization value for ABI */
38+
.Lfpucw_init:
39+
.int 0x037f
40+
3341
/* The following symbols point at read-only data that will be filled in by the */
3442
/* post-linker. */
3543

@@ -134,6 +142,19 @@ elf_entry:
134142
ud2 /* should not be reached */
135143
/* end elf_entry */
136144

145+
/* This code needs to be called *after* the enclave stack has been setup. */
146+
/* There are 3 places where this needs to happen, so this is put in a macro. */
147+
.macro entry_sanitize_final
148+
/* Sanitize rflags received from user */
149+
/* - DF flag: x86-64 ABI requires DF to be unset at function entry/exit */
150+
/* - AC flag: AEX on misaligned memory accesses leaks side channel info */
151+
pushfq
152+
andq $~0x40400, (%rsp)
153+
popfq
154+
bt $0,.Laborted(%rip)
155+
jc .Lreentry_panic
156+
.endm
157+
137158
.text
138159
.global sgx_entry
139160
.type sgx_entry,function
@@ -150,25 +171,18 @@ sgx_entry:
150171
stmxcsr %gs:tcsls_user_mxcsr
151172
fnstcw %gs:tcsls_user_fcw
152173

153-
/* reset user state */
154-
/* - DF flag: x86-64 ABI requires DF to be unset at function entry/exit */
155-
/* - AC flag: AEX on misaligned memory accesses leaks side channel info */
156-
pushfq
157-
andq $~0x40400, (%rsp)
158-
popfq
159-
160174
/* check for debug buffer pointer */
161175
testb $0xff,DEBUG(%rip)
162176
jz .Lskip_debug_init
163177
mov %r10,%gs:tcsls_debug_panic_buf_ptr
164178
.Lskip_debug_init:
165-
/* check for abort */
166-
bt $0,.Laborted(%rip)
167-
jc .Lreentry_panic
168179
/* check if returning from usercall */
169180
mov %gs:tcsls_last_rsp,%r11
170181
test %r11,%r11
171182
jnz .Lusercall_ret
183+
/* reset user state */
184+
ldmxcsr .Lmxcsr_init(%rip)
185+
fldcw .Lfpucw_init(%rip)
172186
/* setup stack */
173187
mov %gs:tcsls_tos,%rsp /* initially, RSP is not set to the correct value */
174188
/* here. This is fixed below under "adjust stack". */
@@ -179,6 +193,7 @@ sgx_entry:
179193
lea IMAGE_BASE(%rip),%rax
180194
add %rax,%rsp
181195
mov %rsp,%gs:tcsls_tos
196+
entry_sanitize_final
182197
/* call tcs_init */
183198
/* store caller-saved registers in callee-saved registers */
184199
mov %rdi,%rbx
@@ -194,7 +209,10 @@ sgx_entry:
194209
mov %r13,%rdx
195210
mov %r14,%r8
196211
mov %r15,%r9
212+
jmp .Lafter_init
197213
.Lskip_init:
214+
entry_sanitize_final
215+
.Lafter_init:
198216
/* call into main entry point */
199217
load_tcsls_flag_secondary_bool cx /* RCX = entry() argument: secondary: bool */
200218
call entry /* RDI, RSI, RDX, R8, R9 passed in from userspace */
@@ -295,6 +313,7 @@ usercall:
295313
ldmxcsr (%rsp)
296314
fldcw 4(%rsp)
297315
add $8, %rsp
316+
entry_sanitize_final
298317
pop %rbx
299318
pop %rbp
300319
pop %r12

0 commit comments

Comments
 (0)