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
68 .org .Lxsave_clear+512
70 .int 0, 0 /* XSTATE_BV */
71 .int 0, 0 /* XCOMP_BV */
72 .org .+48 /* reserved bits */
78 /* TCS local storage section */
79 .equ tcsls_tos, 0x00 /* initialized by loader to *offset* from image base to TOS */
80 .equ tcsls_flags, 0x08 /* initialized by loader */
81 .equ tcsls_flag_secondary, 0 /* initialized by loader; 0 = standard TCS, 1 = secondary TCS */
82 .equ tcsls_flag_init_once, 1 /* initialized by loader to 0 */
84 .equ tcsls_user_fcw, 0x0a
85 .equ tcsls_user_mxcsr, 0x0c
86 .equ tcsls_last_rsp, 0x10 /* initialized by loader to 0 */
87 .equ tcsls_panic_last_rsp, 0x18 /* initialized by loader to 0 */
88 .equ tcsls_debug_panic_buf_ptr, 0x20 /* initialized by loader to 0 */
89 .equ tcsls_user_rsp, 0x28
90 .equ tcsls_user_retip, 0x30
91 .equ tcsls_user_rbp, 0x38
92 .equ tcsls_user_r12, 0x40
93 .equ tcsls_user_r13, 0x48
94 .equ tcsls_user_r14, 0x50
95 .equ tcsls_user_r15, 0x58
96 .equ tcsls_tls_ptr, 0x60
97 .equ tcsls_tcs_addr, 0x68
99 .macro load_tcsls_flag_secondary_bool reg:req comments:vararg
100 .ifne tcsls_flag_secondary /* to convert to a bool, must be the first bit */
103 mov $(1<<tcsls_flag_secondary),%e\reg
104 and %gs:tcsls_flags,%\reg
107 /* We place the ELF entry point in a separate section so it can be removed by
109 .section .text_no_sgx, "ax"
110 .Lelf_entry_error_msg:
111 .ascii "Error: This file is an SGX enclave which cannot be executed as a standard Linux binary.\nSee the installation guide at https://edp.fortanix.com/docs/installation/guide/ on how to use 'cargo run' or follow the steps at https://edp.fortanix.com/docs/tasks/deployment/ for manual deployment.\n"
113 .type elf_entry,function
115 /* print error message */
116 movq $1, %rax /* write() syscall */
117 movq $2, %rdi /* write to stderr */
118 lea .Lelf_entry_error_msg(%rip), %rsi
119 movq $288, %rdx /* num chars to write */
122 movq $60, %rax /* exit() syscall */
123 movq $0, %rdi /* error code */
129 .type sgx_entry,function
131 /* save user registers */
132 mov %rcx,%gs:tcsls_user_retip
133 mov %rsp,%gs:tcsls_user_rsp
134 mov %rbp,%gs:tcsls_user_rbp
135 mov %r12,%gs:tcsls_user_r12
136 mov %r13,%gs:tcsls_user_r13
137 mov %r14,%gs:tcsls_user_r14
138 mov %r15,%gs:tcsls_user_r15
139 mov %rbx,%gs:tcsls_tcs_addr
140 stmxcsr %gs:tcsls_user_mxcsr
141 fnstcw %gs:tcsls_user_fcw
143 /* reset user state */
144 /* - DF flag: x86-64 ABI requires DF to be unset at function entry/exit */
145 /* - AC flag: AEX on misaligned memory accesses leaks side channel info */
147 andq $~0x40400, (%rsp)
150 /* check for debug buffer pointer */
151 testb $0xff,DEBUG(%rip)
153 mov %r10,%gs:tcsls_debug_panic_buf_ptr
155 /* check for abort */
156 bt $0,.Laborted(%rip)
158 /* check if returning from usercall */
159 mov %gs:tcsls_last_rsp,%r11
163 mov %gs:tcsls_tos,%rsp /* initially, RSP is not set to the correct value */
164 /* here. This is fixed below under "adjust stack". */
165 /* check for thread init */
166 bts $tcsls_flag_init_once,%gs:tcsls_flags
169 lea IMAGE_BASE(%rip),%rax
171 mov %rsp,%gs:tcsls_tos
173 /* store caller-saved registers in callee-saved registers */
179 load_tcsls_flag_secondary_bool di /* RDI = tcs_init() argument: secondary: bool */
181 /* reload caller-saved registers */
188 /* call into main entry point */
189 load_tcsls_flag_secondary_bool cx /* RCX = entry() argument: secondary: bool */
190 call entry /* RDI, RSI, RDX, R8, R9 passed in from userspace */
191 mov %rax,%rsi /* RSI = return value */
192 /* NOP: mov %rdx,%rdx */ /* RDX = return value */
193 xor %rdi,%rdi /* RDI = normal exit */
195 /* clear general purpose register state */
196 /* RAX overwritten by ENCLU */
198 /* RCX overwritten by ENCLU */
199 /* RDX contains return value */
202 /* RDI contains exit mode */
203 /* RSI contains return value */
208 /* R12 ~ R15 set by sgx_exit */
210 /* clear extended register state */
211 mov %rdx, %rcx /* save RDX */
214 xrstor .Lxsave_clear(%rip)
215 mov %rcx, %rdx /* restore RDX */
219 /* restore user registers */
220 mov %gs:tcsls_user_r12,%r12
221 mov %gs:tcsls_user_r13,%r13
222 mov %gs:tcsls_user_r14,%r14
223 mov %gs:tcsls_user_r15,%r15
224 mov %gs:tcsls_user_retip,%rbx
225 mov %gs:tcsls_user_rsp,%rsp
226 mov %gs:tcsls_user_rbp,%rbp
227 fldcw %gs:tcsls_user_fcw
228 ldmxcsr %gs:tcsls_user_mxcsr
230 mov $0x4,%eax /* EEXIT */
238 /* This *MUST* be called with 6 parameters, otherwise register information */
242 test %rcx,%rcx /* check `abort` function argument */
243 jnz .Lusercall_abort /* abort is set, jump to abort code (unlikely forward conditional) */
244 jmp .Lusercall_save_state /* non-aborting usercall */
246 /* set aborted bit */
247 movb $1,.Laborted(%rip)
248 /* save registers in DEBUG mode, so that debugger can reconstruct the stack */
249 testb $0xff,DEBUG(%rip)
250 jz .Lusercall_noreturn
251 .Lusercall_save_state:
252 /* save callee-saved state */
262 movq %rsp,%gs:tcsls_last_rsp
264 /* clear general purpose register state */
265 /* RAX overwritten by ENCLU */
266 /* RBX set by sgx_exit */
267 /* RCX overwritten by ENCLU */
268 /* RDX contains parameter */
269 /* RSP set by sgx_exit */
270 /* RBP set by sgx_exit */
271 /* RDI contains parameter */
272 /* RSI contains parameter */
273 /* R8 contains parameter */
274 /* R9 contains parameter */
277 /* R12 ~ R15 set by sgx_exit */
278 /* extended registers/flags cleared by sgx_exit */
282 movq $0,%gs:tcsls_last_rsp
283 /* restore callee-saved state, cf. "save" above */
295 mov %rsi,%rax /* RAX = return value */
296 /* NOP: mov %rdx,%rdx */ /* RDX = return value */
300 The following functions need to be defined externally:
302 // Called by entry code on re-entry after exit
303 extern "C" fn abort_reentry() -> !;
305 // Called once when a TCS is first entered
306 extern "C" fn tcs_init(secondary: bool);
308 // Standard TCS entrypoint
309 extern "C" fn entry(p1: u64, p2: u64, p3: u64, secondary: bool, p4: u64, p5: u64) -> (u64, u64);
315 mov %gs:tcsls_tcs_addr,%rax
320 mov %gs:tcsls_tls_ptr,%rax
325 mov %rdi,%gs:tcsls_tls_ptr
328 .global take_debug_panic_buf_ptr
329 take_debug_panic_buf_ptr:
331 xchg %gs:tcsls_debug_panic_buf_ptr,%rax