]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/abi/entry.S
Rollup merge of #58400 - gnzlbg:fix_driver, r=oli-obk
[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 .Lreentry_panic_msg:
69     .asciz "Re-entered aborted enclave!"
70 .Lreentry_panic_msg_end:
71
72 .org .Lxsave_clear+512
73 .Lxsave_header:
74     .int 0, 0 /*  XSTATE_BV */
75     .int 0, 0 /*  XCOMP_BV */
76     .org .+48 /*  reserved bits */
77
78 .data
79 .Laborted:
80     .byte 0
81
82 /*  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 */
85 .equ tcsls_flag_secondary,      0    /*  initialized by loader; 0 = standard TCS, 1 = secondary TCS */
86 .equ tcsls_flag_init_once,      1    /*  initialized by loader to 0 */
87 /*  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
101 .equ tcsls_tcs_addr,            0x68
102
103 .macro load_tcsls_flag_secondary_bool reg:req comments:vararg
104     .ifne tcsls_flag_secondary /* to convert to a bool, must be the first bit */
105     .abort
106     .endif
107         mov $(1<<tcsls_flag_secondary),%e\reg
108         and %gs:tcsls_flags,%\reg
109 .endm
110
111 .text
112 .global sgx_entry
113 .type sgx_entry,function
114 sgx_entry:
115 /*  save user registers */
116     mov %rcx,%gs:tcsls_user_retip
117     mov %rsp,%gs:tcsls_user_rsp
118     mov %rbp,%gs:tcsls_user_rbp
119     mov %r12,%gs:tcsls_user_r12
120     mov %r13,%gs:tcsls_user_r13
121     mov %r14,%gs:tcsls_user_r14
122     mov %r15,%gs:tcsls_user_r15
123     mov %rbx,%gs:tcsls_tcs_addr
124     stmxcsr %gs:tcsls_user_mxcsr
125     fnstcw %gs:tcsls_user_fcw
126 /*  reset user state */
127     cld /* x86-64 ABI requires DF to be unset at function entry/exit */
128 /*  check for debug buffer pointer */
129     testb  $0xff,DEBUG(%rip)
130     jz .Lskip_debug_init
131     mov %r10,%gs:tcsls_debug_panic_buf_ptr
132 .Lskip_debug_init:
133 /*  check for abort */
134     bt $0,.Laborted(%rip)
135     jc .Lreentry_panic
136 /*  check if returning from usercall */
137     mov %gs:tcsls_last_rsp,%r11
138     test %r11,%r11
139     jnz .Lusercall_ret
140 /*  setup stack */
141     mov %gs:tcsls_tos,%rsp /*  initially, RSP is not set to the correct value */
142                            /*  here. This is fixed below under "adjust stack". */
143 /*  check for thread init */
144     bts $tcsls_flag_init_once,%gs:tcsls_flags
145     jc .Lskip_init
146 /*  adjust stack */
147     lea IMAGE_BASE(%rip),%rax
148     add %rax,%rsp
149     mov %rsp,%gs:tcsls_tos
150 /*  call tcs_init */
151 /*  store caller-saved registers in callee-saved registers */
152     mov %rdi,%rbx
153     mov %rsi,%r12
154     mov %rdx,%r13
155     mov %r8,%r14
156     mov %r9,%r15
157     load_tcsls_flag_secondary_bool di /* RDI = tcs_init() argument: secondary: bool */
158     call tcs_init
159 /*  reload caller-saved registers */
160     mov %rbx,%rdi
161     mov %r12,%rsi
162     mov %r13,%rdx
163     mov %r14,%r8
164     mov %r15,%r9
165 .Lskip_init:
166 /*  call into main entry point */
167     load_tcsls_flag_secondary_bool cx /* RCX = entry() argument: secondary: bool */
168     call entry /* RDI, RSI, RDX, R8, R9 passed in from userspace */
169     mov %rax,%rsi  /* RSI = return value */
170     /* NOP: mov %rdx,%rdx */ /*  RDX = return value */
171     xor %rdi,%rdi  /* RDI = normal exit */
172 .Lexit:
173 /*  clear general purpose register state */
174     /*  RAX overwritten by ENCLU */
175     /*  RBX set later */
176     /*  RCX overwritten by ENCLU */
177     /*  RDX contains return value */
178     /*  RSP set later */
179     /*  RBP set later */
180     /*  RDI contains exit mode */
181     /*  RSI contains return value */
182     xor %r8,%r8
183     xor %r9,%r9
184     xor %r10,%r10
185     xor %r11,%r11
186     /*  R12 ~ R15 set by sgx_exit */
187 .Lsgx_exit:
188 /*  clear extended register state */
189     mov %rdx, %rcx /*  save RDX */
190     mov $-1, %rax
191     mov %rax, %rdx
192     xrstor .Lxsave_clear(%rip)
193     mov %rcx, %rdx /*  restore RDX */
194 /*  clear flags */
195     pushq $0
196     popfq
197 /*  restore user registers */
198     mov %gs:tcsls_user_r12,%r12
199     mov %gs:tcsls_user_r13,%r13
200     mov %gs:tcsls_user_r14,%r14
201     mov %gs:tcsls_user_r15,%r15
202     mov %gs:tcsls_user_retip,%rbx
203     mov %gs:tcsls_user_rsp,%rsp
204     mov %gs:tcsls_user_rbp,%rbp
205     fldcw %gs:tcsls_user_fcw
206     ldmxcsr %gs:tcsls_user_mxcsr
207 /*  exit enclave */
208     mov $0x4,%eax /*  EEXIT */
209     enclu
210 /*  end sgx_entry */
211
212 .Lreentry_panic:
213     lea .Lreentry_panic_msg(%rip),%rdi
214     mov $.Lreentry_panic_msg_end-.Lreentry_panic_msg,%esi
215     orq $8,%rsp
216     jmp panic_msg
217
218 /*  This *MUST* be called with 6 parameters, otherwise register information */
219 /*  might leak! */
220 .global usercall
221 usercall:
222     test %rcx,%rcx            /* check `abort` function argument */
223     jnz .Lusercall_abort      /* abort is set, jump to abort code (unlikely forward conditional) */
224     jmp .Lusercall_save_state /* non-aborting usercall */
225 .Lusercall_abort:
226 /* set aborted bit */
227     movb $1,.Laborted(%rip)
228 /* save registers in DEBUG mode, so that debugger can reconstruct the stack */
229     testb $0xff,DEBUG(%rip)
230     jz .Lusercall_noreturn
231 .Lusercall_save_state:
232 /*  save callee-saved state */
233     push %r15
234     push %r14
235     push %r13
236     push %r12
237     push %rbp
238     push %rbx
239     sub $8, %rsp
240     fstcw 4(%rsp)
241     stmxcsr (%rsp)
242     movq %rsp,%gs:tcsls_last_rsp
243 .Lusercall_noreturn:
244 /*  clear general purpose register state */
245     /*  RAX overwritten by ENCLU */
246     /*  RBX set by sgx_exit */
247     /*  RCX overwritten by ENCLU */
248     /*  RDX contains parameter */
249     /*  RSP set by sgx_exit */
250     /*  RBP set by sgx_exit */
251     /*  RDI contains parameter */
252     /*  RSI contains parameter */
253     /*  R8 contains parameter */
254     /*  R9 contains parameter */
255     xor %r10,%r10
256     xor %r11,%r11
257     /*  R12 ~ R15 set by sgx_exit */
258 /*  extended registers/flags cleared by sgx_exit */
259 /*  exit */
260     jmp .Lsgx_exit
261 .Lusercall_ret:
262     movq $0,%gs:tcsls_last_rsp
263 /*  restore callee-saved state, cf. "save" above */
264     mov %r11,%rsp
265     ldmxcsr (%rsp)
266     fldcw 4(%rsp)
267     add $8, %rsp
268     pop %rbx
269     pop %rbp
270     pop %r12
271     pop %r13
272     pop %r14
273     pop %r15
274 /*  return */
275     mov %rsi,%rax /*  RAX = return value */
276     /* NOP: mov %rdx,%rdx */ /*  RDX = return value */
277     ret
278
279 /*
280 The following functions need to be defined externally:
281 ```
282 // Called by entry code when it needs to panic
283 extern "C" fn panic_msg(msg: &'static str) -> ! {
284     panic!(msg)
285 }
286
287 // Called once when a TCS is first entered
288 extern "C" fn tcs_init(secondary: bool);
289
290 // Standard TCS entrypoint
291 extern "C" fn entry(p1: u64, p2: u64, p3: u64, secondary: bool, p4: u64, p5: u64) -> (u64, u64);
292 ```
293 */
294
295 .global get_tcs_addr
296 get_tcs_addr:
297     mov %gs:tcsls_tcs_addr,%rax
298     ret
299
300 .global get_tls_ptr
301 get_tls_ptr:
302     mov %gs:tcsls_tls_ptr,%rax
303     ret
304
305 .global set_tls_ptr
306 set_tls_ptr:
307     mov %rdi,%gs:tcsls_tls_ptr
308     ret
309
310 .global take_debug_panic_buf_ptr
311 take_debug_panic_buf_ptr:
312     xor %rax,%rax
313     xchg %gs:tcsls_debug_panic_buf_ptr,%rax
314     ret