1 /* -----------------------------------------------------------------------
2 darwin64.S - Copyright (c) 2006 Free Software Foundation, Inc.
3 Copyright (c) 2008 Red Hat, Inc.
6 x86-64 Foreign Function Interface for Darwin.
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 ``Software''), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
16 The above copyright notice and this permission notice shall be included
17 in all copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 OTHER DEALINGS IN THE SOFTWARE.
26 ----------------------------------------------------------------------- */
30 #include <fficonfig.h>
36 /* ffi_call_unix64 (void *args, unsigned long bytes, unsigned flags,
37 void *raddr, void (*fnaddr)(void));
39 Bit o trickiness here -- ARGS+BYTES is the base of the stack frame
40 for this function. This has been allocated by ffi_call. We also
41 deallocate some of the stack that has been alloca'd. */
44 .globl _ffi_call_unix64
48 movq (%rsp), %r10 /* Load return address. */
49 leaq (%rdi, %rsi), %rax /* Find local stack base. */
50 movq %rdx, (%rax) /* Save flags. */
51 movq %rcx, 8(%rax) /* Save raddr. */
52 movq %rbp, 16(%rax) /* Save old frame pointer. */
53 movq %r10, 24(%rax) /* Relocate return address. */
54 movq %rax, %rbp /* Finalize local stack frame. */
56 movq %rdi, %r10 /* Save a copy of the register area. */
57 movq %r8, %r11 /* Save a copy of the target fn. */
58 movl %r9d, %eax /* Set number of SSE registers. */
60 /* Load up all argument registers. */
71 /* Deallocate the reg arg area. */
74 /* Call the user function. */
77 /* Deallocate stack arg area; local stack frame in redzone. */
80 movq 0(%rbp), %rcx /* Reload flags. */
81 movq 8(%rbp), %rdi /* Reload raddr. */
82 movq 16(%rbp), %rbp /* Reload old frame pointer. */
85 /* The first byte of the flags contains the FFI_TYPE. */
87 leaq Lstore_table(%rip), %r11
88 movslq (%r11, %r10, 4), %r10
93 .long Lst_void-Lstore_table /* FFI_TYPE_VOID */
94 .long Lst_sint32-Lstore_table /* FFI_TYPE_INT */
95 .long Lst_float-Lstore_table /* FFI_TYPE_FLOAT */
96 .long Lst_double-Lstore_table /* FFI_TYPE_DOUBLE */
97 .long Lst_ldouble-Lstore_table /* FFI_TYPE_LONGDOUBLE */
98 .long Lst_uint8-Lstore_table /* FFI_TYPE_UINT8 */
99 .long Lst_sint8-Lstore_table /* FFI_TYPE_SINT8 */
100 .long Lst_uint16-Lstore_table /* FFI_TYPE_UINT16 */
101 .long Lst_sint16-Lstore_table /* FFI_TYPE_SINT16 */
102 .long Lst_uint32-Lstore_table /* FFI_TYPE_UINT32 */
103 .long Lst_sint32-Lstore_table /* FFI_TYPE_SINT32 */
104 .long Lst_int64-Lstore_table /* FFI_TYPE_UINT64 */
105 .long Lst_int64-Lstore_table /* FFI_TYPE_SINT64 */
106 .long Lst_struct-Lstore_table /* FFI_TYPE_STRUCT */
107 .long Lst_int64-Lstore_table /* FFI_TYPE_POINTER */
158 leaq -20(%rsp), %rsi /* Scratch area in redzone. */
160 /* We have to locate the values now, and since we don't want to
161 write too much data into the user's return value, we spill the
162 value to a 16 byte scratch area first. Bits 8, 9, and 10
163 control where the values are located. Only one of the three
164 bits will be set; see ffi_prep_cif_machdep for the pattern. */
178 /* Bits 12-31 contain the true size of the structure. Copy from
179 the scratch area to the true destination. */
184 /* Many times we can avoid loading any SSE registers at all.
185 It's not worth an indirect jump to load the exact set of
186 SSE registers needed; zero or all is a good compromise. */
190 movdqa 48(%r10), %xmm0
191 movdqa 64(%r10), %xmm1
192 movdqa 80(%r10), %xmm2
193 movdqa 96(%r10), %xmm3
194 movdqa 112(%r10), %xmm4
195 movdqa 128(%r10), %xmm5
196 movdqa 144(%r10), %xmm6
197 movdqa 160(%r10), %xmm7
198 jmp Lret_from_load_sse
202 .globl _ffi_closure_unix64
206 /* The carry flag is set by the trampoline iff SSE registers
207 are used. Don't clobber it before the branch instruction. */
208 leaq -200(%rsp), %rsp
223 call _ffi_closure_unix64_inner
225 /* Deallocate stack frame early; return value is now in redzone. */
229 /* The first byte of the return value contains the FFI_TYPE. */
231 leaq Lload_table(%rip), %r11
232 movslq (%r11, %r10, 4), %r10
237 .long Lld_void-Lload_table /* FFI_TYPE_VOID */
238 .long Lld_int32-Lload_table /* FFI_TYPE_INT */
239 .long Lld_float-Lload_table /* FFI_TYPE_FLOAT */
240 .long Lld_double-Lload_table /* FFI_TYPE_DOUBLE */
241 .long Lld_ldouble-Lload_table /* FFI_TYPE_LONGDOUBLE */
242 .long Lld_int8-Lload_table /* FFI_TYPE_UINT8 */
243 .long Lld_int8-Lload_table /* FFI_TYPE_SINT8 */
244 .long Lld_int16-Lload_table /* FFI_TYPE_UINT16 */
245 .long Lld_int16-Lload_table /* FFI_TYPE_SINT16 */
246 .long Lld_int32-Lload_table /* FFI_TYPE_UINT32 */
247 .long Lld_int32-Lload_table /* FFI_TYPE_SINT32 */
248 .long Lld_int64-Lload_table /* FFI_TYPE_UINT64 */
249 .long Lld_int64-Lload_table /* FFI_TYPE_SINT64 */
250 .long Lld_struct-Lload_table /* FFI_TYPE_STRUCT */
251 .long Lld_int64-Lload_table /* FFI_TYPE_POINTER */
259 movzbl -24(%rsp), %eax
263 movzwl -24(%rsp), %eax
275 movss -24(%rsp), %xmm0
279 movsd -24(%rsp), %xmm0
287 /* There are four possibilities here, %rax/%rdx, %xmm0/%rax,
288 %rax/%xmm0, %xmm0/%xmm1. We collapse two by always loading
289 both rdx and xmm1 with the second word. For the remaining,
290 bit 8 set means xmm0 gets the second word, and bit 9 means
291 that rax gets the second word. */
294 movq -16(%rsp), %xmm1
303 /* See the comment above Lload_sse; the same logic applies here. */
307 movdqa %xmm0, 48(%rsp)
308 movdqa %xmm1, 64(%rsp)
309 movdqa %xmm2, 80(%rsp)
310 movdqa %xmm3, 96(%rsp)
311 movdqa %xmm4, 112(%rsp)
312 movdqa %xmm5, 128(%rsp)
313 movdqa %xmm6, 144(%rsp)
314 movdqa %xmm7, 160(%rsp)
315 jmp Lret_from_save_sse
318 .section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
320 .set L$set$0,LECIE1-LSCIE1 /* CIE Length */
323 .long 0x0 /* CIE Identifier Tag */
324 .byte 0x1 /* CIE Version */
325 .ascii "zR\0" /* CIE Augmentation */
326 .byte 0x1 /* uleb128 0x1; CIE Code Alignment Factor */
327 .byte 0x78 /* sleb128 -8; CIE Data Alignment Factor */
328 .byte 0x10 /* CIE RA Column */
329 .byte 0x1 /* uleb128 0x1; Augmentation size */
330 .byte 0x10 /* FDE Encoding (pcrel sdata4) */
331 .byte 0xc /* DW_CFA_def_cfa, %rsp offset 8 */
332 .byte 0x7 /* uleb128 0x7 */
333 .byte 0x8 /* uleb128 0x8 */
334 .byte 0x90 /* DW_CFA_offset, column 0x10 */
338 .globl _ffi_call_unix64.eh
341 .set L$set$1,LEFDE1-LASFDE1 /* FDE Length */
344 .long LASFDE1-EH_frame1 /* FDE CIE offset */
345 .quad LUW0-. /* FDE initial location */
346 .set L$set$2,LUW4-LUW0 /* FDE address range */
348 .byte 0x0 /* Augmentation size */
349 .byte 0x4 /* DW_CFA_advance_loc4 */
350 .set L$set$3,LUW1-LUW0
353 /* New stack frame based off rbp. This is a itty bit of unwind
354 trickery in that the CFA *has* changed. There is no easy way
355 to describe it correctly on entry to the function. Fortunately,
356 it doesn't matter too much since at all points we can correctly
357 unwind back to ffi_call. Note that the location to which we
358 moved the return address is (the new) CFA-8, so from the
359 perspective of the unwind info, it hasn't moved. */
360 .byte 0xc /* DW_CFA_def_cfa, %rbp offset 32 */
363 .byte 0x80+6 /* DW_CFA_offset, %rbp offset 2*-8 */
365 .byte 0xa /* DW_CFA_remember_state */
367 .byte 0x4 /* DW_CFA_advance_loc4 */
368 .set L$set$4,LUW2-LUW1
370 .byte 0xc /* DW_CFA_def_cfa, %rsp offset 8 */
373 .byte 0xc0+6 /* DW_CFA_restore, %rbp */
375 .byte 0x4 /* DW_CFA_advance_loc4 */
376 .set L$set$5,LUW3-LUW2
378 .byte 0xb /* DW_CFA_restore_state */
382 .globl _ffi_closure_unix64.eh
383 _ffi_closure_unix64.eh:
385 .set L$set$6,LEFDE3-LASFDE3 /* FDE Length */
388 .long LASFDE3-EH_frame1 /* FDE CIE offset */
389 .quad LUW5-. /* FDE initial location */
390 .set L$set$7,LUW9-LUW5 /* FDE address range */
392 .byte 0x0 /* Augmentation size */
394 .byte 0x4 /* DW_CFA_advance_loc4 */
395 .set L$set$8,LUW6-LUW5
397 .byte 0xe /* DW_CFA_def_cfa_offset */
398 .byte 208,1 /* uleb128 208 */
399 .byte 0xa /* DW_CFA_remember_state */
401 .byte 0x4 /* DW_CFA_advance_loc4 */
402 .set L$set$9,LUW7-LUW6
404 .byte 0xe /* DW_CFA_def_cfa_offset */
407 .byte 0x4 /* DW_CFA_advance_loc4 */
408 .set L$set$10,LUW8-LUW7
410 .byte 0xb /* DW_CFA_restore_state */
414 .subsections_via_symbols
416 #endif /* __x86_64__ */