1 /* -----------------------------------------------------------------------
2 unix64.S - Copyright (c) 2002, 2008 Bo Thorsen <bo@suse.de>
4 x86-64 Foreign Function Interface
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 ``Software''), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 ----------------------------------------------------------------------- */
29 #include <fficonfig.h>
34 /* ffi_call_unix64 (void *args, unsigned long bytes, unsigned flags,
35 void *raddr, void (*fnaddr)());
37 Bit o trickiness here -- ARGS+BYTES is the base of the stack frame
38 for this function. This has been allocated by ffi_call. We also
39 deallocate some of the stack that has been alloca'd. */
42 .globl ffi_call_unix64
43 .type ffi_call_unix64,@function
47 movq (%rsp), %r10 /* Load return address. */
48 leaq (%rdi, %rsi), %rax /* Find local stack base. */
49 movq %rdx, (%rax) /* Save flags. */
50 movq %rcx, 8(%rax) /* Save raddr. */
51 movq %rbp, 16(%rax) /* Save old frame pointer. */
52 movq %r10, 24(%rax) /* Relocate return address. */
53 movq %rax, %rbp /* Finalize local stack frame. */
55 movq %rdi, %r10 /* Save a copy of the register area. */
56 movq %r8, %r11 /* Save a copy of the target fn. */
57 movl %r9d, %eax /* Set number of SSE registers. */
59 /* Load up all argument registers. */
70 /* Deallocate the reg arg area. */
73 /* Call the user function. */
76 /* Deallocate stack arg area; local stack frame in redzone. */
79 movq 0(%rbp), %rcx /* Reload flags. */
80 movq 8(%rbp), %rdi /* Reload raddr. */
81 movq 16(%rbp), %rbp /* Reload old frame pointer. */
84 /* The first byte of the flags contains the FFI_TYPE. */
86 leaq .Lstore_table(%rip), %r11
87 movslq (%r11, %r10, 4), %r10
92 .long .Lst_void-.Lstore_table /* FFI_TYPE_VOID */
93 .long .Lst_sint32-.Lstore_table /* FFI_TYPE_INT */
94 .long .Lst_float-.Lstore_table /* FFI_TYPE_FLOAT */
95 .long .Lst_double-.Lstore_table /* FFI_TYPE_DOUBLE */
96 .long .Lst_ldouble-.Lstore_table /* FFI_TYPE_LONGDOUBLE */
97 .long .Lst_uint8-.Lstore_table /* FFI_TYPE_UINT8 */
98 .long .Lst_sint8-.Lstore_table /* FFI_TYPE_SINT8 */
99 .long .Lst_uint16-.Lstore_table /* FFI_TYPE_UINT16 */
100 .long .Lst_sint16-.Lstore_table /* FFI_TYPE_SINT16 */
101 .long .Lst_uint32-.Lstore_table /* FFI_TYPE_UINT32 */
102 .long .Lst_sint32-.Lstore_table /* FFI_TYPE_SINT32 */
103 .long .Lst_int64-.Lstore_table /* FFI_TYPE_UINT64 */
104 .long .Lst_int64-.Lstore_table /* FFI_TYPE_SINT64 */
105 .long .Lst_struct-.Lstore_table /* FFI_TYPE_STRUCT */
106 .long .Lst_int64-.Lstore_table /* FFI_TYPE_POINTER */
159 leaq -20(%rsp), %rsi /* Scratch area in redzone. */
161 /* We have to locate the values now, and since we don't want to
162 write too much data into the user's return value, we spill the
163 value to a 16 byte scratch area first. Bits 8, 9, and 10
164 control where the values are located. Only one of the three
165 bits will be set; see ffi_prep_cif_machdep for the pattern. */
179 /* Bits 12-31 contain the true size of the structure. Copy from
180 the scratch area to the true destination. */
185 /* Many times we can avoid loading any SSE registers at all.
186 It's not worth an indirect jump to load the exact set of
187 SSE registers needed; zero or all is a good compromise. */
191 movdqa 48(%r10), %xmm0
192 movdqa 64(%r10), %xmm1
193 movdqa 80(%r10), %xmm2
194 movdqa 96(%r10), %xmm3
195 movdqa 112(%r10), %xmm4
196 movdqa 128(%r10), %xmm5
197 movdqa 144(%r10), %xmm6
198 movdqa 160(%r10), %xmm7
199 jmp .Lret_from_load_sse
202 .size ffi_call_unix64,.-ffi_call_unix64
205 .globl ffi_closure_unix64
206 .type ffi_closure_unix64,@function
210 /* The carry flag is set by the trampoline iff SSE registers
211 are used. Don't clobber it before the branch instruction. */
212 leaq -200(%rsp), %rsp
227 call ffi_closure_unix64_inner@PLT
229 /* Deallocate stack frame early; return value is now in redzone. */
233 /* The first byte of the return value contains the FFI_TYPE. */
235 leaq .Lload_table(%rip), %r11
236 movslq (%r11, %r10, 4), %r10
241 .long .Lld_void-.Lload_table /* FFI_TYPE_VOID */
242 .long .Lld_int32-.Lload_table /* FFI_TYPE_INT */
243 .long .Lld_float-.Lload_table /* FFI_TYPE_FLOAT */
244 .long .Lld_double-.Lload_table /* FFI_TYPE_DOUBLE */
245 .long .Lld_ldouble-.Lload_table /* FFI_TYPE_LONGDOUBLE */
246 .long .Lld_int8-.Lload_table /* FFI_TYPE_UINT8 */
247 .long .Lld_int8-.Lload_table /* FFI_TYPE_SINT8 */
248 .long .Lld_int16-.Lload_table /* FFI_TYPE_UINT16 */
249 .long .Lld_int16-.Lload_table /* FFI_TYPE_SINT16 */
250 .long .Lld_int32-.Lload_table /* FFI_TYPE_UINT32 */
251 .long .Lld_int32-.Lload_table /* FFI_TYPE_SINT32 */
252 .long .Lld_int64-.Lload_table /* FFI_TYPE_UINT64 */
253 .long .Lld_int64-.Lload_table /* FFI_TYPE_SINT64 */
254 .long .Lld_struct-.Lload_table /* FFI_TYPE_STRUCT */
255 .long .Lld_int64-.Lload_table /* FFI_TYPE_POINTER */
263 movzbl -24(%rsp), %eax
267 movzwl -24(%rsp), %eax
280 movss -24(%rsp), %xmm0
284 movsd -24(%rsp), %xmm0
293 /* There are four possibilities here, %rax/%rdx, %xmm0/%rax,
294 %rax/%xmm0, %xmm0/%xmm1. We collapse two by always loading
295 both rdx and xmm1 with the second word. For the remaining,
296 bit 8 set means xmm0 gets the second word, and bit 9 means
297 that rax gets the second word. */
300 movq -16(%rsp), %xmm1
309 /* See the comment above .Lload_sse; the same logic applies here. */
313 movdqa %xmm0, 48(%rsp)
314 movdqa %xmm1, 64(%rsp)
315 movdqa %xmm2, 80(%rsp)
316 movdqa %xmm3, 96(%rsp)
317 movdqa %xmm4, 112(%rsp)
318 movdqa %xmm5, 128(%rsp)
319 movdqa %xmm6, 144(%rsp)
320 movdqa %xmm7, 160(%rsp)
321 jmp .Lret_from_save_sse
324 .size ffi_closure_unix64,.-ffi_closure_unix64
326 .section .eh_frame,"a",@progbits
328 .long .LECIE1-.LSCIE1 /* CIE Length */
330 .long 0 /* CIE Identifier Tag */
331 .byte 1 /* CIE Version */
332 .ascii "zR\0" /* CIE Augmentation */
333 .uleb128 1 /* CIE Code Alignment Factor */
334 .sleb128 -8 /* CIE Data Alignment Factor */
335 .byte 0x10 /* CIE RA Column */
336 .uleb128 1 /* Augmentation size */
337 .byte 0x1b /* FDE Encoding (pcrel sdata4) */
338 .byte 0xc /* DW_CFA_def_cfa, %rsp offset 8 */
341 .byte 0x80+16 /* DW_CFA_offset, %rip offset 1*-8 */
346 .long .LEFDE1-.LASFDE1 /* FDE Length */
348 .long .LASFDE1-.Lframe1 /* FDE CIE offset */
349 #if HAVE_AS_X86_PCREL
350 .long .LUW0-. /* FDE initial location */
354 .long .LUW4-.LUW0 /* FDE address range */
355 .uleb128 0x0 /* Augmentation size */
357 .byte 0x4 /* DW_CFA_advance_loc4 */
360 /* New stack frame based off rbp. This is a itty bit of unwind
361 trickery in that the CFA *has* changed. There is no easy way
362 to describe it correctly on entry to the function. Fortunately,
363 it doesn't matter too much since at all points we can correctly
364 unwind back to ffi_call. Note that the location to which we
365 moved the return address is (the new) CFA-8, so from the
366 perspective of the unwind info, it hasn't moved. */
367 .byte 0xc /* DW_CFA_def_cfa, %rbp offset 32 */
370 .byte 0x80+6 /* DW_CFA_offset, %rbp offset 2*-8 */
372 .byte 0xa /* DW_CFA_remember_state */
374 .byte 0x4 /* DW_CFA_advance_loc4 */
376 .byte 0xc /* DW_CFA_def_cfa, %rsp offset 8 */
379 .byte 0xc0+6 /* DW_CFA_restore, %rbp */
381 .byte 0x4 /* DW_CFA_advance_loc4 */
383 .byte 0xb /* DW_CFA_restore_state */
388 .long .LEFDE3-.LASFDE3 /* FDE Length */
390 .long .LASFDE3-.Lframe1 /* FDE CIE offset */
391 #if HAVE_AS_X86_PCREL
392 .long .LUW5-. /* FDE initial location */
396 .long .LUW9-.LUW5 /* FDE address range */
397 .uleb128 0x0 /* Augmentation size */
399 .byte 0x4 /* DW_CFA_advance_loc4 */
401 .byte 0xe /* DW_CFA_def_cfa_offset */
403 .byte 0xa /* DW_CFA_remember_state */
405 .byte 0x4 /* DW_CFA_advance_loc4 */
407 .byte 0xe /* DW_CFA_def_cfa_offset */
410 .byte 0x4 /* DW_CFA_advance_loc4 */
412 .byte 0xb /* DW_CFA_restore_state */
417 #endif /* __x86_64__ */
419 #if defined __ELF__ && defined __linux__
420 .section .note.GNU-stack,"",@progbits