@@ -68,6 +68,12 @@ IMAGE_BASE:
68
68
globvar EH_FRM_OFFSET 8
69
69
/* The size in bytes of enclacve .eh_frame section */
70
70
globvar EH_FRM_LEN 8
71
+ /* The base address (relative to enclave start) of the TLS initialization image */
72
+ globvar TLS_INIT_BASE 8
73
+ /* The size in bytes of the TLS initialization image */
74
+ globvar TLS_INIT_SIZE 8
75
+ /* The TLS module offset of this enclave (see the ELF-TLS specification for more details) */
76
+ globvar TLS_OFFSET 8
71
77
72
78
.org .Lxsave_clear+512
73
79
.Lxsave_header:
@@ -80,32 +86,32 @@ IMAGE_BASE:
80
86
.byte 0
81
87
82
88
/* TCS local storage section */
83
- .equ tcsls_tos, 0x00 /* initialized by loader to *offset* from image base to TOS */
84
- .equ tcsls_flags, 0x08 /* initialized by loader */
89
+ .equ tcsls_tp, 0x00 /* initialized by loader to *offset* from image base to this field */
90
+ .equ tcsls_tos, 0x08 /* initialized by loader to *offset* from image base to TOS */
91
+ .equ tcsls_flags, 0x10 /* initialized by loader */
85
92
.equ tcsls_flag_secondary, 0 /* initialized by loader; 0 = standard TCS, 1 = secondary TCS */
86
93
.equ tcsls_flag_init_once, 1 /* initialized by loader to 0 */
87
94
/* 14 unused bits */
88
- .equ tcsls_user_fcw, 0x0a
89
- .equ tcsls_user_mxcsr, 0x0c
90
- .equ tcsls_last_rsp, 0x10 /* initialized by loader to 0 */
91
- .equ tcsls_panic_last_rsp, 0x18 /* initialized by loader to 0 */
92
- .equ tcsls_debug_panic_buf_ptr, 0x20 /* initialized by loader to 0 */
93
- .equ tcsls_user_rsp, 0x28
94
- .equ tcsls_user_retip, 0x30
95
- .equ tcsls_user_rbp, 0x38
96
- .equ tcsls_user_r12, 0x40
97
- .equ tcsls_user_r13, 0x48
98
- .equ tcsls_user_r14, 0x50
99
- .equ tcsls_user_r15, 0x58
100
- .equ tcsls_tls_ptr, 0x60
95
+ .equ tcsls_user_fcw, 0x12
96
+ .equ tcsls_user_mxcsr, 0x14
97
+ .equ tcsls_last_rsp, 0x18 /* initialized by loader to 0 */
98
+ .equ tcsls_panic_last_rsp, 0x20 /* initialized by loader to 0 */
99
+ .equ tcsls_debug_panic_buf_ptr, 0x28 /* initialized by loader to 0 */
100
+ .equ tcsls_user_rsp, 0x30
101
+ .equ tcsls_user_retip, 0x38
102
+ .equ tcsls_user_rbp, 0x40
103
+ .equ tcsls_user_r12, 0x48
104
+ .equ tcsls_user_r13, 0x50
105
+ .equ tcsls_user_r14, 0x58
106
+ .equ tcsls_user_r15, 0x60
101
107
.equ tcsls_tcs_addr, 0x68
102
108
103
109
.macro load_tcsls_flag_secondary_bool reg: req comments: vararg
104
110
.ifne tcsls_flag_secondary /* to convert to a bool, must be the first bit */
105
111
.abort
106
112
.endif
107
113
mov $(1 <<tcsls_flag_secondary),%e\reg
108
- and %gs :tcsls_flags,%\reg
114
+ and %fs :tcsls_flags,%\reg
109
115
.endm
110
116
111
117
/* We place the ELF entry point in a separate section so it can be removed by
@@ -157,21 +163,21 @@ elf_entry:
157
163
.type sgx_entry,function
158
164
sgx_entry:
159
165
/* save user registers */
160
- mov %rcx ,%gs :tcsls_user_retip
161
- mov %rsp ,%gs :tcsls_user_rsp
162
- mov %rbp ,%gs :tcsls_user_rbp
163
- mov %r12 ,%gs :tcsls_user_r12
164
- mov %r13 ,%gs :tcsls_user_r13
165
- mov %r14 ,%gs :tcsls_user_r14
166
- mov %r15 ,%gs :tcsls_user_r15
167
- mov %rbx ,%gs :tcsls_tcs_addr
168
- stmxcsr %gs :tcsls_user_mxcsr
169
- fnstcw %gs :tcsls_user_fcw
166
+ mov %rcx ,%fs :tcsls_user_retip
167
+ mov %rsp ,%fs :tcsls_user_rsp
168
+ mov %rbp ,%fs :tcsls_user_rbp
169
+ mov %r12 ,%fs :tcsls_user_r12
170
+ mov %r13 ,%fs :tcsls_user_r13
171
+ mov %r14 ,%fs :tcsls_user_r14
172
+ mov %r15 ,%fs :tcsls_user_r15
173
+ mov %rbx ,%fs :tcsls_tcs_addr
174
+ stmxcsr %fs :tcsls_user_mxcsr
175
+ fnstcw %fs :tcsls_user_fcw
170
176
171
177
/* check for debug buffer pointer */
172
178
testb $0xff ,DEBUG(%rip )
173
179
jz .Lskip_debug_init
174
- mov %r10 ,%gs :tcsls_debug_panic_buf_ptr
180
+ mov %r10 ,%fs :tcsls_debug_panic_buf_ptr
175
181
.Lskip_debug_init:
176
182
/* reset cpu state */
177
183
mov %rdx , %r10
@@ -181,20 +187,23 @@ sgx_entry:
181
187
mov %r10 , %rdx
182
188
183
189
/* check if returning from usercall */
184
- mov %gs :tcsls_last_rsp,%r11
190
+ mov %fs :tcsls_last_rsp,%r11
185
191
test %r11 ,%r11
186
192
jnz .Lusercall_ret
187
193
/* setup stack */
188
- mov %gs :tcsls_tos,%rsp /* initially, RSP is not set to the correct value */
194
+ mov %fs :tcsls_tos,%rsp /* initially, RSP is not set to the correct value */
189
195
/* here. This is fixed below under "adjust stack". */
190
196
/* check for thread init */
191
- bts $tcsls_flag_init_once,%gs :tcsls_flags
197
+ bts $tcsls_flag_init_once,%fs :tcsls_flags
192
198
jc .Lskip_init
193
199
/* adjust stack */
194
200
lea IMAGE_BASE(%rip ),%rax
195
201
add %rax ,%rsp
196
- mov %rsp ,%gs :tcsls_tos
202
+ mov %rsp ,%fs :tcsls_tos
197
203
entry_sanitize_final
204
+ /* adjust thread pointer */
205
+ lea IMAGE_BASE(%rip ),%rax
206
+ add %rax ,%fs :0
198
207
/* call tcs_init */
199
208
/* store caller-saved registers in callee-saved registers */
200
209
mov %rdi ,%rbx
@@ -246,15 +255,15 @@ sgx_entry:
246
255
pushq $0
247
256
popfq
248
257
/* restore user registers */
249
- mov %gs :tcsls_user_r12,%r12
250
- mov %gs :tcsls_user_r13,%r13
251
- mov %gs :tcsls_user_r14,%r14
252
- mov %gs :tcsls_user_r15,%r15
253
- mov %gs :tcsls_user_retip,%rbx
254
- mov %gs :tcsls_user_rsp,%rsp
255
- mov %gs :tcsls_user_rbp,%rbp
256
- fldcw %gs :tcsls_user_fcw
257
- ldmxcsr %gs :tcsls_user_mxcsr
258
+ mov %fs :tcsls_user_r12,%r12
259
+ mov %fs :tcsls_user_r13,%r13
260
+ mov %fs :tcsls_user_r14,%r14
261
+ mov %fs :tcsls_user_r15,%r15
262
+ mov %fs :tcsls_user_retip,%rbx
263
+ mov %fs :tcsls_user_rsp,%rsp
264
+ mov %fs :tcsls_user_rbp,%rbp
265
+ fldcw %fs :tcsls_user_fcw
266
+ ldmxcsr %fs :tcsls_user_mxcsr
258
267
/* exit enclave */
259
268
mov $0x4 ,%eax /* EEXIT */
260
269
enclu
@@ -288,7 +297,7 @@ usercall:
288
297
sub $8 , %rsp
289
298
fstcw 4 (%rsp )
290
299
stmxcsr (%rsp )
291
- movq %rsp ,%gs :tcsls_last_rsp
300
+ movq %rsp ,%fs :tcsls_last_rsp
292
301
.Lusercall_noreturn:
293
302
/* clear general purpose register state */
294
303
/* RAX overwritten by ENCLU */
@@ -308,7 +317,7 @@ usercall:
308
317
/* exit */
309
318
jmp .Lsgx_exit
310
319
.Lusercall_ret:
311
- movq $0 ,%gs :tcsls_last_rsp
320
+ movq $0 ,%fs :tcsls_last_rsp
312
321
/* restore callee-saved state, cf. "save" above */
313
322
mov %r11 ,%rsp
314
323
ldmxcsr (%rsp )
@@ -344,29 +353,15 @@ extern "C" fn entry(p1: u64, p2: u64, p3: u64, secondary: bool, p4: u64, p5: u64
344
353
345
354
.global get_tcs_addr
346
355
get_tcs_addr:
347
- mov %gs :tcsls_tcs_addr,%rax
348
- pop %r11
349
- lfence
350
- jmp *%r11
351
-
352
- .global get_tls_ptr
353
- get_tls_ptr:
354
- mov %gs :tcsls_tls_ptr,%rax
355
- pop %r11
356
- lfence
357
- jmp *%r11
358
-
359
- .global set_tls_ptr
360
- set_tls_ptr:
361
- mov %rdi ,%gs :tcsls_tls_ptr
356
+ mov %fs :tcsls_tcs_addr,%rax
362
357
pop %r11
363
358
lfence
364
359
jmp *%r11
365
360
366
361
.global take_debug_panic_buf_ptr
367
362
take_debug_panic_buf_ptr:
368
363
xor %rax ,%rax
369
- xchg %gs :tcsls_debug_panic_buf_ptr,%rax
364
+ xchg %fs :tcsls_debug_panic_buf_ptr,%rax
370
365
pop %r11
371
366
lfence
372
367
jmp *%r11
0 commit comments