]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/abi/entry.S
7c273a8a83d302c3c29e60dcf97880cb340d061f
[rust.git] / src / libstd / sys / sgx / abi / entry.S
1 /*  This symbol is used at runtime to figure out the virtual address that the */
2 /*  enclave is loaded at. */
3 .section absolute
4 .global IMAGE_BASE
5 IMAGE_BASE:
6
7 .section ".note.x86_64-fortanix-unknown-sgx", "", @note
8     .align 4
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 */
13 1:  .align 4
14 2:  .long 0                    /* desc - toolchain version number, 32-bit LE */
15 3:  .align 4
16
17 .section .rodata
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: */
21 /*  */
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 */
24 /*    * XSAVE header */
25 .align 64
26 .Lxsave_clear:
27 .org .+24
28 .Lxsave_mxcsr:
29     .int 0
30
31 /*  We can store a bunch of data in the gap between MXCSR and the XSAVE header */
32
33 /*  The following symbols point at read-only data that will be filled in by the */
34 /*  post-linker. */
35
36 /*  When using this macro, don't forget to adjust the linker version script! */
37 .macro globvar name:req size:req
38     .global \name
39     .protected \name
40     .align \size
41     .size \name , \size
42     \name :
43         .org .+\size
44 .endm
45     /*  The base address (relative to enclave start) of the heap area */
46     globvar HEAP_BASE 8
47     /*  The heap size in bytes */
48     globvar HEAP_SIZE 8
49     /*  Value of the RELA entry in the dynamic table */
50     globvar RELA 8
51     /*  Value of the RELACOUNT entry in the dynamic table */
52     globvar RELACOUNT 8
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 */
58     globvar DEBUG 1
59     /*  The base address (relative to enclave start) of the enclave text section */
60     globvar TEXT_BASE 8
61     /*  The size in bytes of enclacve text section */
62     globvar TEXT_SIZE 8
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
67
68 .org .Lxsave_clear+512
69 .Lxsave_header:
70     .int 0, 0 /*  XSTATE_BV */
71     .int 0, 0 /*  XCOMP_BV */
72     .org .+48 /*  reserved bits */
73
74 .data
75 .Laborted:
76     .byte 0
77
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 */
83 /*  14 unused bits */
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
98
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 */
101     .abort
102     .endif
103         mov $(1<<tcsls_flag_secondary),%e\reg
104         and %gs:tcsls_flags,%\reg
105 .endm
106
107 /* We place the ELF entry point in a separate section so it can be removed by
108    elf2sgxs */
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"
112 .Lelf_entry_error_msg_end:
113
114 .global elf_entry
115 .type elf_entry,function
116 elf_entry:
117 /* print error message */
118     movq $2,%rdi                      /* write to stderr (fd 2) */
119     lea .Lelf_entry_error_msg(%rip),%rsi
120     movq $.Lelf_entry_error_msg_end-.Lelf_entry_error_msg,%rdx
121 .Lelf_entry_call:
122     movq $1,%rax                      /* write() syscall        */
123     syscall
124     test %rax,%rax
125     jle .Lelf_exit                    /* exit on error          */
126     add %rax,%rsi
127     sub %rax,%rdx                     /* all chars written?     */
128     jnz .Lelf_entry_call
129
130 .Lelf_exit:    
131     movq $60,%rax                     /* exit() syscall         */
132     movq $1,%rdi                      /* exit code 1            */
133     syscall
134     ud2                               /* should not be reached  */
135 /*  end elf_entry */
136
137 /* This code needs to be called *after* the enclave stack has been setup. */
138 /* There are 3 places where this needs to happen, so this is put in a macro. */
139 .macro sanitize_rflags
140 /*  Sanitize rflags received from user */
141 /*    - DF flag: x86-64 ABI requires DF to be unset at function entry/exit */
142 /*    - AC flag: AEX on misaligned memory accesses leaks side channel info */
143     pushfq
144     andq $~0x40400, (%rsp)
145     popfq
146 .endm
147
148 .text
149 .global sgx_entry
150 .type sgx_entry,function
151 sgx_entry:
152 /*  save user registers */
153     mov %rcx,%gs:tcsls_user_retip
154     mov %rsp,%gs:tcsls_user_rsp
155     mov %rbp,%gs:tcsls_user_rbp
156     mov %r12,%gs:tcsls_user_r12
157     mov %r13,%gs:tcsls_user_r13
158     mov %r14,%gs:tcsls_user_r14
159     mov %r15,%gs:tcsls_user_r15
160     mov %rbx,%gs:tcsls_tcs_addr
161     stmxcsr %gs:tcsls_user_mxcsr
162     fnstcw %gs:tcsls_user_fcw
163
164 /*  check for debug buffer pointer */
165     testb  $0xff,DEBUG(%rip)
166     jz .Lskip_debug_init
167     mov %r10,%gs:tcsls_debug_panic_buf_ptr
168 .Lskip_debug_init:
169 /*  check for abort */
170     bt $0,.Laborted(%rip)
171     jc .Lreentry_panic
172 /*  check if returning from usercall */
173     mov %gs:tcsls_last_rsp,%r11
174     test %r11,%r11
175     jnz .Lusercall_ret
176 /*  setup stack */
177     mov %gs:tcsls_tos,%rsp /*  initially, RSP is not set to the correct value */
178                            /*  here. This is fixed below under "adjust stack". */
179 /*  check for thread init */
180     bts $tcsls_flag_init_once,%gs:tcsls_flags
181     jc .Lskip_init
182 /*  adjust stack */
183     lea IMAGE_BASE(%rip),%rax
184     add %rax,%rsp
185     mov %rsp,%gs:tcsls_tos
186     sanitize_rflags
187 /*  call tcs_init */
188 /*  store caller-saved registers in callee-saved registers */
189     mov %rdi,%rbx
190     mov %rsi,%r12
191     mov %rdx,%r13
192     mov %r8,%r14
193     mov %r9,%r15
194     load_tcsls_flag_secondary_bool di /* RDI = tcs_init() argument: secondary: bool */
195     call tcs_init
196 /*  reload caller-saved registers */
197     mov %rbx,%rdi
198     mov %r12,%rsi
199     mov %r13,%rdx
200     mov %r14,%r8
201     mov %r15,%r9
202     jmp .Lafter_init
203 .Lskip_init:
204     sanitize_rflags
205 .Lafter_init:
206 /*  call into main entry point */
207     load_tcsls_flag_secondary_bool cx /* RCX = entry() argument: secondary: bool */
208     call entry /* RDI, RSI, RDX, R8, R9 passed in from userspace */
209     mov %rax,%rsi  /* RSI = return value */
210     /* NOP: mov %rdx,%rdx */ /*  RDX = return value */
211     xor %rdi,%rdi  /* RDI = normal exit */
212 .Lexit:
213 /*  clear general purpose register state */
214     /*  RAX overwritten by ENCLU */
215     /*  RBX set later */
216     /*  RCX overwritten by ENCLU */
217     /*  RDX contains return value */
218     /*  RSP set later */
219     /*  RBP set later */
220     /*  RDI contains exit mode */
221     /*  RSI contains return value */
222     xor %r8,%r8
223     xor %r9,%r9
224     xor %r10,%r10
225     xor %r11,%r11
226     /*  R12 ~ R15 set by sgx_exit */
227 .Lsgx_exit:
228 /*  clear extended register state */
229     mov %rdx, %rcx /*  save RDX */
230     mov $-1, %rax
231     mov %rax, %rdx
232     xrstor .Lxsave_clear(%rip)
233     mov %rcx, %rdx /*  restore RDX */
234 /*  clear flags */
235     pushq $0
236     popfq
237 /*  restore user registers */
238     mov %gs:tcsls_user_r12,%r12
239     mov %gs:tcsls_user_r13,%r13
240     mov %gs:tcsls_user_r14,%r14
241     mov %gs:tcsls_user_r15,%r15
242     mov %gs:tcsls_user_retip,%rbx
243     mov %gs:tcsls_user_rsp,%rsp
244     mov %gs:tcsls_user_rbp,%rbp
245     fldcw %gs:tcsls_user_fcw
246     ldmxcsr %gs:tcsls_user_mxcsr
247 /*  exit enclave */
248     mov $0x4,%eax /*  EEXIT */
249     enclu
250 /*  end sgx_entry */
251
252 .Lreentry_panic:
253     orq $8,%rsp
254     jmp abort_reentry
255
256 /*  This *MUST* be called with 6 parameters, otherwise register information */
257 /*  might leak! */
258 .global usercall
259 usercall:
260     test %rcx,%rcx            /* check `abort` function argument */
261     jnz .Lusercall_abort      /* abort is set, jump to abort code (unlikely forward conditional) */
262     jmp .Lusercall_save_state /* non-aborting usercall */
263 .Lusercall_abort:
264 /* set aborted bit */
265     movb $1,.Laborted(%rip)
266 /* save registers in DEBUG mode, so that debugger can reconstruct the stack */
267     testb $0xff,DEBUG(%rip)
268     jz .Lusercall_noreturn
269 .Lusercall_save_state:
270 /*  save callee-saved state */
271     push %r15
272     push %r14
273     push %r13
274     push %r12
275     push %rbp
276     push %rbx
277     sub $8, %rsp
278     fstcw 4(%rsp)
279     stmxcsr (%rsp)
280     movq %rsp,%gs:tcsls_last_rsp
281 .Lusercall_noreturn:
282 /*  clear general purpose register state */
283     /*  RAX overwritten by ENCLU */
284     /*  RBX set by sgx_exit */
285     /*  RCX overwritten by ENCLU */
286     /*  RDX contains parameter */
287     /*  RSP set by sgx_exit */
288     /*  RBP set by sgx_exit */
289     /*  RDI contains parameter */
290     /*  RSI contains parameter */
291     /*  R8 contains parameter */
292     /*  R9 contains parameter */
293     xor %r10,%r10
294     xor %r11,%r11
295     /*  R12 ~ R15 set by sgx_exit */
296 /*  extended registers/flags cleared by sgx_exit */
297 /*  exit */
298     jmp .Lsgx_exit
299 .Lusercall_ret:
300     movq $0,%gs:tcsls_last_rsp
301 /*  restore callee-saved state, cf. "save" above */
302     mov %r11,%rsp
303     sanitize_rflags
304     ldmxcsr (%rsp)
305     fldcw 4(%rsp)
306     add $8, %rsp
307     pop %rbx
308     pop %rbp
309     pop %r12
310     pop %r13
311     pop %r14
312     pop %r15
313 /*  return */
314     mov %rsi,%rax /*  RAX = return value */
315     /* NOP: mov %rdx,%rdx */ /*  RDX = return value */
316     ret
317
318 /*
319 The following functions need to be defined externally:
320 ```
321 // Called by entry code on re-entry after exit
322 extern "C" fn abort_reentry() -> !;
323
324 // Called once when a TCS is first entered
325 extern "C" fn tcs_init(secondary: bool);
326
327 // Standard TCS entrypoint
328 extern "C" fn entry(p1: u64, p2: u64, p3: u64, secondary: bool, p4: u64, p5: u64) -> (u64, u64);
329 ```
330 */
331
332 .global get_tcs_addr
333 get_tcs_addr:
334     mov %gs:tcsls_tcs_addr,%rax
335     ret
336
337 .global get_tls_ptr
338 get_tls_ptr:
339     mov %gs:tcsls_tls_ptr,%rax
340     ret
341
342 .global set_tls_ptr
343 set_tls_ptr:
344     mov %rdi,%gs:tcsls_tls_ptr
345     ret
346
347 .global take_debug_panic_buf_ptr
348 take_debug_panic_buf_ptr:
349     xor %rax,%rax
350     xchg %gs:tcsls_debug_panic_buf_ptr,%rax
351     ret