@@ -30,6 +30,14 @@ IMAGE_BASE:
30
30
31
31
/* We can store a bunch of data in the gap between MXCSR and the XSAVE header */
32
32
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
+
33
41
/* The following symbols point at read-only data that will be filled in by the */
34
42
/* post-linker. */
35
43
@@ -134,6 +142,19 @@ elf_entry:
134
142
ud2 /* should not be reached */
135
143
/* end elf_entry */
136
144
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
+
137
158
.text
138
159
.global sgx_entry
139
160
.type sgx_entry,function
@@ -150,25 +171,18 @@ sgx_entry:
150
171
stmxcsr %gs :tcsls_user_mxcsr
151
172
fnstcw %gs :tcsls_user_fcw
152
173
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
-
160
174
/* check for debug buffer pointer */
161
175
testb $0xff ,DEBUG(%rip )
162
176
jz .Lskip_debug_init
163
177
mov %r10 ,%gs :tcsls_debug_panic_buf_ptr
164
178
.Lskip_debug_init:
165
- /* check for abort */
166
- bt $0 ,.Laborted(%rip )
167
- jc .Lreentry_panic
168
179
/* check if returning from usercall */
169
180
mov %gs :tcsls_last_rsp,%r11
170
181
test %r11 ,%r11
171
182
jnz .Lusercall_ret
183
+ /* reset user state */
184
+ ldmxcsr .Lmxcsr_init(%rip )
185
+ fldcw .Lfpucw_init(%rip )
172
186
/* setup stack */
173
187
mov %gs :tcsls_tos,%rsp /* initially, RSP is not set to the correct value */
174
188
/* here. This is fixed below under "adjust stack". */
@@ -179,6 +193,7 @@ sgx_entry:
179
193
lea IMAGE_BASE(%rip ),%rax
180
194
add %rax ,%rsp
181
195
mov %rsp ,%gs :tcsls_tos
196
+ entry_sanitize_final
182
197
/* call tcs_init */
183
198
/* store caller-saved registers in callee-saved registers */
184
199
mov %rdi ,%rbx
@@ -194,7 +209,10 @@ sgx_entry:
194
209
mov %r13 ,%rdx
195
210
mov %r14 ,%r8
196
211
mov %r15 ,%r9
212
+ jmp .Lafter_init
197
213
.Lskip_init:
214
+ entry_sanitize_final
215
+ .Lafter_init:
198
216
/* call into main entry point */
199
217
load_tcsls_flag_secondary_bool cx /* RCX = entry() argument: secondary: bool */
200
218
call entry /* RDI, RSI, RDX, R8, R9 passed in from userspace */
@@ -295,6 +313,7 @@ usercall:
295
313
ldmxcsr (%rsp )
296
314
fldcw 4 (%rsp )
297
315
add $8 , %rsp
316
+ entry_sanitize_final
298
317
pop %rbx
299
318
pop %rbp
300
319
pop %r12
0 commit comments