1 /* This symbol is used at runtime to figure out the virtual address that the */
2 /* enclave is loaded at. */
7 .section ".note.x86_64-fortanix-unknown-sgx", "", @note
9 .long 1f - 0f /* name length (not including padding) */
10 .long 3f - 2f /* desc length (not including padding) */
11 .long 1 /* type = NT_VERSION */
12 0: .asciz "toolchain-version" /* name */
14 2: .long 0 /* desc - toolchain version number, 32-bit LE */
18 /* The XSAVE area needs to be a large chunk of readable memory, but since we are */
19 /* going to restore everything to its initial state (XSTATE_BV=0), only certain */
20 /* parts need to have a defined value. In particular: */
22 /* * MXCSR in the legacy area. This register is always restored if RFBM[1] or */
23 /* RFBM[2] is set, regardless of the value of XSTATE_BV */
31 /* We can store a bunch of data in the gap between MXCSR and the XSAVE header */
33 /* The following symbols point at read-only data that will be filled in by the */
36 /* When using this macro, don't forget to adjust the linker version script! */
37 .macro globvar name:req size:req
45 /* The base address (relative to enclave start) of the heap area */
47 /* The heap size in bytes */
49 /* Value of the RELA entry in the dynamic table */
51 /* Value of the RELACOUNT entry in the dynamic table */
53 /* The enclave size in bytes */
54 globvar ENCLAVE_SIZE 8
55 /* The base address (relative to enclave start) of the enclave configuration area */
56 globvar CFGDATA_BASE 8
57 /* Non-zero if debugging is enabled, zero otherwise */
59 /* The base address (relative to enclave start) of the enclave text section */
61 /* The size in bytes of enclacve text section */
63 /* The base address (relative to enclave start) of the enclave EH_FRM_HDR section */
64 globvar EH_FRM_HDR_BASE 8
65 /* The size in bytes of enclacve EH_FRM_HDR section */
66 globvar EH_FRM_HDR_SIZE 8
69 .asciz "Re-entered panicked enclave!"
70 .Lreentry_panic_msg_end:
73 .asciz "Invalid usercall#!"
74 .Lusercall_panic_msg_end:
76 .org .Lxsave_clear+512
78 .int 0, 0 /* XSTATE_BV */
79 .int 0, 0 /* XCOMP_BV */
80 .org .+48 /* reserved bits */
86 /* TCS local storage section */
87 .equ tcsls_tos, 0x00 /* initialized by loader to *offset* from image base to TOS */
88 .equ tcsls_flags, 0x08 /* initialized by loader */
89 .equ tcsls_flag_secondary, 0 /* initialized by loader; 0 = standard TCS, 1 = secondary TCS */
90 .equ tcsls_flag_init_once, 1 /* initialized by loader to 0 */
92 .equ tcsls_user_fcw, 0x0a
93 .equ tcsls_user_mxcsr, 0x0c
94 .equ tcsls_last_rsp, 0x10 /* initialized by loader to 0 */
95 .equ tcsls_panic_last_rsp, 0x18 /* initialized by loader to 0 */
96 .equ tcsls_debug_panic_buf_ptr, 0x20 /* initialized by loader to 0 */
97 .equ tcsls_user_rsp, 0x28
98 .equ tcsls_user_retip, 0x30
99 .equ tcsls_user_rbp, 0x38
100 .equ tcsls_user_r12, 0x40
101 .equ tcsls_user_r13, 0x48
102 .equ tcsls_user_r14, 0x50
103 .equ tcsls_user_r15, 0x58
104 .equ tcsls_tls_ptr, 0x60
105 .equ tcsls_tcs_addr, 0x68
107 .macro load_tcsls_flag_secondary_bool reg:req comments:vararg
108 .ifne tcsls_flag_secondary /* to convert to a bool, must be the first bit */
111 mov $(1<<tcsls_flag_secondary),%e\reg
112 and %gs:tcsls_flags,%\reg
117 .type sgx_entry,function
119 /* save user registers */
120 mov %rcx,%gs:tcsls_user_retip
121 mov %rsp,%gs:tcsls_user_rsp
122 mov %rbp,%gs:tcsls_user_rbp
123 mov %r12,%gs:tcsls_user_r12
124 mov %r13,%gs:tcsls_user_r13
125 mov %r14,%gs:tcsls_user_r14
126 mov %r15,%gs:tcsls_user_r15
127 mov %rbx,%gs:tcsls_tcs_addr
128 stmxcsr %gs:tcsls_user_mxcsr
129 fnstcw %gs:tcsls_user_fcw
130 /* reset user state */
131 cld /* x86-64 ABI requires DF to be unset at function entry/exit */
132 /* check for debug buffer pointer */
133 testb $0xff,DEBUG(%rip)
135 mov %r10,%gs:tcsls_debug_panic_buf_ptr
137 /* check if returning from usercall */
138 mov %gs:tcsls_last_rsp,%r11
142 mov %gs:tcsls_tos,%rsp /* initially, RSP is not set to the correct value */
143 /* here. This is fixed below under "adjust stack". */
144 /* check for thread init */
145 bts $tcsls_flag_init_once,%gs:tcsls_flags
148 lea IMAGE_BASE(%rip),%rax
150 mov %rsp,%gs:tcsls_tos
152 /* store caller-saved registers in callee-saved registers */
158 load_tcsls_flag_secondary_bool di /* RDI = tcs_init() argument: secondary: bool */
160 /* reload caller-saved registers */
167 /* check for panic */
168 bt $0,.Lpanicked(%rip)
170 /* call into main entry point */
171 load_tcsls_flag_secondary_bool cx /* RCX = entry() argument: secondary: bool */
172 call entry /* RDI, RSI, RDX, R8, R9 passed in from userspace */
173 mov %rax,%rsi /* RSI = return value */
174 /* NOP: mov %rdx,%rdx */ /* RDX = return value */
175 xor %rdi,%rdi /* RDI = normal exit */
177 /* clear general purpose register state */
178 /* RAX overwritten by ENCLU */
180 /* RCX overwritten by ENCLU */
181 /* RDX contains return value */
184 /* RDI contains exit mode */
185 /* RSI contains return value */
190 /* R12 ~ R15 set by sgx_exit */
192 /* clear extended register state */
193 mov %rdx, %rcx /* save RDX */
196 xrstor .Lxsave_clear(%rip)
197 mov %rcx, %rdx /* restore RDX */
201 /* restore user registers */
202 mov %gs:tcsls_user_r12,%r12
203 mov %gs:tcsls_user_r13,%r13
204 mov %gs:tcsls_user_r14,%r14
205 mov %gs:tcsls_user_r15,%r15
206 mov %gs:tcsls_user_retip,%rbx
207 mov %gs:tcsls_user_rsp,%rsp
208 mov %gs:tcsls_user_rbp,%rbp
209 fldcw %gs:tcsls_user_fcw
210 ldmxcsr %gs:tcsls_user_mxcsr
212 mov $0x4,%eax /* EEXIT */
217 lea .Lreentry_panic_msg(%rip),%rdi
218 mov $.Lreentry_panic_msg_end-.Lreentry_panic_msg,%esi
223 lea .Lusercall_panic_msg(%rip),%rdi
224 mov $.Lusercall_panic_msg_end-.Lusercall_panic_msg,%esi
228 .macro push_callee_saved_registers
242 /* save registers in DEBUG mode, so that debugger can reconstruct the stack */
243 testb $0xff,DEBUG(%rip)
244 jz .Lskip_save_registers
245 push_callee_saved_registers
246 movq %rsp,%gs:tcsls_panic_last_rsp
247 .Lskip_save_registers:
248 /* set panicked bit */
249 movb $1,.Lpanicked(%rip)
250 /* call usercall exit(true) */
251 mov $1,%esi /* RSI = usercall() argument: panic = true */
252 xor %rdx,%rdx /* RDX cleared */
253 movq $usercall_nr_exit,%rdi /* RDI = usercall exit */
256 /* This *MUST* be called with 6 parameters, otherwise register information */
262 /* save callee-saved state */
263 push_callee_saved_registers
264 movq %rsp,%gs:tcsls_last_rsp
265 /* clear general purpose register state */
266 /* RAX overwritten by ENCLU */
267 /* RBX set by sgx_exit */
268 /* RCX overwritten by ENCLU */
269 /* RDX contains parameter */
270 /* RSP set by sgx_exit */
271 /* RBP set by sgx_exit */
272 /* RDI contains parameter */
273 /* RSI contains parameter */
274 /* R8 contains parameter */
275 /* R9 contains parameter */
278 /* R12 ~ R15 set by sgx_exit */
279 /* extended registers/flags cleared by sgx_exit */
283 movq $0,%gs:tcsls_last_rsp
284 /* restore callee-saved state, cf. push_callee_saved_registers */
296 mov %rsi,%rax /* RAX = return value */
297 /* NOP: mov %rdx,%rdx */ /* RDX = return value */
301 The following functions need to be defined externally:
303 // Called by entry code when it needs to panic
304 extern "C" fn panic_msg(msg: &'static str) -> ! {
308 // Called once when a TCS is first entered
309 extern "C" fn tcs_init(secondary: bool);
311 // Standard TCS entrypoint
312 extern "C" fn entry(p1: u64, p2: u64, p3: u64, secondary: bool, p4: u64, p5: u64) -> (u64, u64);
318 mov %gs:tcsls_tcs_addr,%rax
323 mov %gs:tcsls_tls_ptr,%rax
328 mov %rdi,%gs:tcsls_tls_ptr
331 .global take_debug_panic_buf_ptr
332 take_debug_panic_buf_ptr:
334 xchg %gs:tcsls_debug_panic_buf_ptr,%rax