1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1998 Geoffrey Keating
4 PowerPC 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, EXPRESS
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 ----------------------------------------------------------------------- */
27 #include <ffi_common.h>
33 extern void ffi_closure_SYSV (void);
34 extern void FFI_HIDDEN
ffi_closure_LINUX64 (void);
37 /* The assembly depends on these exact flags. */
38 FLAG_RETURNS_SMST
= 1 << (31-31), /* Used for FFI_SYSV small structs. */
39 FLAG_RETURNS_NOTHING
= 1 << (31-30), /* These go in cr7 */
40 FLAG_RETURNS_FP
= 1 << (31-29),
41 FLAG_RETURNS_64BITS
= 1 << (31-28),
42 FLAG_RETURNS_128BITS
= 1 << (31-27),
44 FLAG_ARG_NEEDS_COPY
= 1 << (31- 7),
45 FLAG_FP_ARGUMENTS
= 1 << (31- 6), /* cr1.eq; specified by ABI */
46 FLAG_4_GPR_ARGUMENTS
= 1 << (31- 5),
47 FLAG_RETVAL_REFERENCE
= 1 << (31- 4)
50 /* About the SYSV ABI. */
52 NUM_GPR_ARG_REGISTERS
= 8,
53 NUM_FPR_ARG_REGISTERS
= 8
55 enum { ASM_NEEDS_REGISTERS
= 4 };
57 /* ffi_prep_args_SYSV is called by the assembly routine once stack space
58 has been allocated for the function's arguments.
60 The stack layout we want looks like this:
62 | Return address from ffi_call_SYSV 4bytes | higher addresses
63 |--------------------------------------------|
64 | Previous backchain pointer 4 | stack pointer here
65 |--------------------------------------------|<+ <<< on entry to
66 | Saved r28-r31 4*4 | | ffi_call_SYSV
67 |--------------------------------------------| |
68 | GPR registers r3-r10 8*4 | | ffi_call_SYSV
69 |--------------------------------------------| |
70 | FPR registers f1-f8 (optional) 8*8 | |
71 |--------------------------------------------| | stack |
72 | Space for copied structures | | grows |
73 |--------------------------------------------| | down V
74 | Parameters that didn't fit in registers | |
75 |--------------------------------------------| | lower addresses
76 | Space for callee's LR 4 | |
77 |--------------------------------------------| | stack pointer here
78 | Current backchain pointer 4 |-/ during
79 |--------------------------------------------| <<< ffi_call_SYSV
85 ffi_prep_args_SYSV (extended_cif
*ecif
, unsigned *const stack
)
88 const unsigned bytes
= ecif
->cif
->bytes
;
89 const unsigned flags
= ecif
->cif
->flags
;
99 /* 'stacktop' points at the previous backchain pointer. */
102 /* 'gpr_base' points at the space for gpr3, and grows upwards as
103 we use GPR registers. */
107 /* 'fpr_base' points at the space for fpr1, and grows upwards as
108 we use FPR registers. */
112 /* 'copy_space' grows down as we put structures in it. It should
113 stay 16-byte aligned. */
116 /* 'next_arg' grows up as we put parameters in it. */
134 size_t struct_copy_size
;
137 stacktop
.c
= (char *) stack
+ bytes
;
138 gpr_base
.u
= stacktop
.u
- ASM_NEEDS_REGISTERS
- NUM_GPR_ARG_REGISTERS
;
140 fpr_base
.d
= gpr_base
.d
- NUM_FPR_ARG_REGISTERS
;
142 copy_space
.c
= ((flags
& FLAG_FP_ARGUMENTS
) ? fpr_base
.c
: gpr_base
.c
);
143 next_arg
.u
= stack
+ 2;
145 /* Check that everything starts aligned properly. */
146 FFI_ASSERT (((unsigned) (char *) stack
& 0xF) == 0);
147 FFI_ASSERT (((unsigned) copy_space
.c
& 0xF) == 0);
148 FFI_ASSERT (((unsigned) stacktop
.c
& 0xF) == 0);
149 FFI_ASSERT ((bytes
& 0xF) == 0);
150 FFI_ASSERT (copy_space
.c
>= next_arg
.c
);
152 /* Deal with return values that are actually pass-by-reference. */
153 if (flags
& FLAG_RETVAL_REFERENCE
)
155 *gpr_base
.u
++ = (unsigned long) (char *) ecif
->rvalue
;
159 /* Now for the arguments. */
160 p_argv
.v
= ecif
->avalue
;
161 for (ptr
= ecif
->cif
->arg_types
, i
= ecif
->cif
->nargs
;
163 i
--, ptr
++, p_argv
.v
++)
165 switch ((*ptr
)->type
)
168 double_tmp
= **p_argv
.f
;
169 if (fparg_count
>= NUM_FPR_ARG_REGISTERS
)
171 *next_arg
.f
= (float) double_tmp
;
175 *fpr_base
.d
++ = double_tmp
;
177 FFI_ASSERT (flags
& FLAG_FP_ARGUMENTS
);
180 case FFI_TYPE_DOUBLE
:
181 double_tmp
= **p_argv
.d
;
183 if (fparg_count
>= NUM_FPR_ARG_REGISTERS
)
185 if (intarg_count
>= NUM_GPR_ARG_REGISTERS
186 && intarg_count
% 2 != 0)
191 *next_arg
.d
= double_tmp
;
195 *fpr_base
.d
++ = double_tmp
;
197 FFI_ASSERT (flags
& FLAG_FP_ARGUMENTS
);
200 case FFI_TYPE_UINT64
:
201 case FFI_TYPE_SINT64
:
202 if (intarg_count
== NUM_GPR_ARG_REGISTERS
-1)
204 if (intarg_count
>= NUM_GPR_ARG_REGISTERS
)
206 if (intarg_count
% 2 != 0)
211 *next_arg
.ll
= **p_argv
.ll
;
216 /* whoops: abi states only certain register pairs
217 * can be used for passing long long int
218 * specifically (r3,r4), (r5,r6), (r7,r8),
219 * (r9,r10) and if next arg is long long but
220 * not correct starting register of pair then skip
221 * until the proper starting register
223 if (intarg_count
% 2 != 0)
228 *gpr_base
.ll
++ = **p_argv
.ll
;
233 case FFI_TYPE_STRUCT
:
234 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
235 case FFI_TYPE_LONGDOUBLE
:
237 struct_copy_size
= ((*ptr
)->size
+ 15) & ~0xF;
238 copy_space
.c
-= struct_copy_size
;
239 memcpy (copy_space
.c
, *p_argv
.c
, (*ptr
)->size
);
241 gprvalue
= (unsigned long) copy_space
.c
;
243 FFI_ASSERT (copy_space
.c
> next_arg
.c
);
244 FFI_ASSERT (flags
& FLAG_ARG_NEEDS_COPY
);
248 gprvalue
= **p_argv
.uc
;
251 gprvalue
= **p_argv
.sc
;
253 case FFI_TYPE_UINT16
:
254 gprvalue
= **p_argv
.us
;
256 case FFI_TYPE_SINT16
:
257 gprvalue
= **p_argv
.ss
;
261 case FFI_TYPE_UINT32
:
262 case FFI_TYPE_SINT32
:
263 case FFI_TYPE_POINTER
:
264 gprvalue
= **p_argv
.ui
;
267 if (intarg_count
>= NUM_GPR_ARG_REGISTERS
)
268 *next_arg
.u
++ = gprvalue
;
270 *gpr_base
.u
++ = gprvalue
;
276 /* Check that we didn't overrun the stack... */
277 FFI_ASSERT (copy_space
.c
>= next_arg
.c
);
278 FFI_ASSERT (gpr_base
.u
<= stacktop
.u
- ASM_NEEDS_REGISTERS
);
279 FFI_ASSERT (fpr_base
.u
280 <= stacktop
.u
- ASM_NEEDS_REGISTERS
- NUM_GPR_ARG_REGISTERS
);
281 FFI_ASSERT (flags
& FLAG_4_GPR_ARGUMENTS
|| intarg_count
<= 4);
284 /* About the LINUX64 ABI. */
286 NUM_GPR_ARG_REGISTERS64
= 8,
287 NUM_FPR_ARG_REGISTERS64
= 13
289 enum { ASM_NEEDS_REGISTERS64
= 4 };
291 /* ffi_prep_args64 is called by the assembly routine once stack space
292 has been allocated for the function's arguments.
294 The stack layout we want looks like this:
296 | Ret addr from ffi_call_LINUX64 8bytes | higher addresses
297 |--------------------------------------------|
298 | CR save area 8bytes |
299 |--------------------------------------------|
300 | Previous backchain pointer 8 | stack pointer here
301 |--------------------------------------------|<+ <<< on entry to
302 | Saved r28-r31 4*8 | | ffi_call_LINUX64
303 |--------------------------------------------| |
304 | GPR registers r3-r10 8*8 | |
305 |--------------------------------------------| |
306 | FPR registers f1-f13 (optional) 13*8 | |
307 |--------------------------------------------| |
308 | Parameter save area | |
309 |--------------------------------------------| |
310 | TOC save area 8 | |
311 |--------------------------------------------| | stack |
312 | Linker doubleword 8 | | grows |
313 |--------------------------------------------| | down V
314 | Compiler doubleword 8 | |
315 |--------------------------------------------| | lower addresses
316 | Space for callee's LR 8 | |
317 |--------------------------------------------| |
319 |--------------------------------------------| | stack pointer here
320 | Current backchain pointer 8 |-/ during
321 |--------------------------------------------| <<< ffi_call_LINUX64
327 ffi_prep_args64 (extended_cif
*ecif
, unsigned long *const stack
)
330 const unsigned long bytes
= ecif
->cif
->bytes
;
331 const unsigned long flags
= ecif
->cif
->flags
;
340 /* 'stacktop' points at the previous backchain pointer. */
343 /* 'next_arg' points at the space for gpr3, and grows upwards as
344 we use GPR registers, then continues at rest. */
350 /* 'fpr_base' points at the space for fpr3, and grows upwards as
351 we use FPR registers. */
371 unsigned long gprvalue
;
373 stacktop
.c
= (char *) stack
+ bytes
;
374 gpr_base
.ul
= stacktop
.ul
- ASM_NEEDS_REGISTERS64
- NUM_GPR_ARG_REGISTERS64
;
375 gpr_end
.ul
= gpr_base
.ul
+ NUM_GPR_ARG_REGISTERS64
;
376 rest
.ul
= stack
+ 6 + NUM_GPR_ARG_REGISTERS64
;
377 fpr_base
.d
= gpr_base
.d
- NUM_FPR_ARG_REGISTERS64
;
379 next_arg
.ul
= gpr_base
.ul
;
381 /* Check that everything starts aligned properly. */
382 FFI_ASSERT (((unsigned long) (char *) stack
& 0xF) == 0);
383 FFI_ASSERT (((unsigned long) stacktop
.c
& 0xF) == 0);
384 FFI_ASSERT ((bytes
& 0xF) == 0);
386 /* Deal with return values that are actually pass-by-reference. */
387 if (flags
& FLAG_RETVAL_REFERENCE
)
388 *next_arg
.ul
++ = (unsigned long) (char *) ecif
->rvalue
;
390 /* Now for the arguments. */
391 p_argv
.v
= ecif
->avalue
;
392 for (ptr
= ecif
->cif
->arg_types
, i
= ecif
->cif
->nargs
;
394 i
--, ptr
++, p_argv
.v
++)
396 switch ((*ptr
)->type
)
399 double_tmp
= **p_argv
.f
;
400 *next_arg
.f
= (float) double_tmp
;
401 if (++next_arg
.ul
== gpr_end
.ul
)
402 next_arg
.ul
= rest
.ul
;
403 if (fparg_count
< NUM_FPR_ARG_REGISTERS64
)
404 *fpr_base
.d
++ = double_tmp
;
406 FFI_ASSERT (flags
& FLAG_FP_ARGUMENTS
);
409 case FFI_TYPE_DOUBLE
:
410 double_tmp
= **p_argv
.d
;
411 *next_arg
.d
= double_tmp
;
412 if (++next_arg
.ul
== gpr_end
.ul
)
413 next_arg
.ul
= rest
.ul
;
414 if (fparg_count
< NUM_FPR_ARG_REGISTERS64
)
415 *fpr_base
.d
++ = double_tmp
;
417 FFI_ASSERT (flags
& FLAG_FP_ARGUMENTS
);
420 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
421 case FFI_TYPE_LONGDOUBLE
:
422 double_tmp
= (*p_argv
.d
)[0];
423 *next_arg
.d
= double_tmp
;
424 if (++next_arg
.ul
== gpr_end
.ul
)
425 next_arg
.ul
= rest
.ul
;
426 if (fparg_count
< NUM_FPR_ARG_REGISTERS64
)
427 *fpr_base
.d
++ = double_tmp
;
429 double_tmp
= (*p_argv
.d
)[1];
430 *next_arg
.d
= double_tmp
;
431 if (++next_arg
.ul
== gpr_end
.ul
)
432 next_arg
.ul
= rest
.ul
;
433 if (fparg_count
< NUM_FPR_ARG_REGISTERS64
)
434 *fpr_base
.d
++ = double_tmp
;
436 FFI_ASSERT (flags
& FLAG_FP_ARGUMENTS
);
440 case FFI_TYPE_STRUCT
:
441 words
= ((*ptr
)->size
+ 7) / 8;
442 if (next_arg
.ul
>= gpr_base
.ul
&& next_arg
.ul
+ words
> gpr_end
.ul
)
444 size_t first
= gpr_end
.c
- next_arg
.c
;
445 memcpy (next_arg
.c
, *p_argv
.c
, first
);
446 memcpy (rest
.c
, *p_argv
.c
+ first
, (*ptr
)->size
- first
);
447 next_arg
.c
= rest
.c
+ words
* 8 - first
;
451 char *where
= next_arg
.c
;
453 /* Structures with size less than eight bytes are passed
455 if ((*ptr
)->size
< 8)
456 where
+= 8 - (*ptr
)->size
;
458 memcpy (where
, *p_argv
.c
, (*ptr
)->size
);
459 next_arg
.ul
+= words
;
460 if (next_arg
.ul
== gpr_end
.ul
)
461 next_arg
.ul
= rest
.ul
;
466 gprvalue
= **p_argv
.uc
;
469 gprvalue
= **p_argv
.sc
;
471 case FFI_TYPE_UINT16
:
472 gprvalue
= **p_argv
.us
;
474 case FFI_TYPE_SINT16
:
475 gprvalue
= **p_argv
.ss
;
477 case FFI_TYPE_UINT32
:
478 gprvalue
= **p_argv
.ui
;
481 case FFI_TYPE_SINT32
:
482 gprvalue
= **p_argv
.si
;
485 case FFI_TYPE_UINT64
:
486 case FFI_TYPE_SINT64
:
487 case FFI_TYPE_POINTER
:
488 gprvalue
= **p_argv
.ul
;
490 *next_arg
.ul
++ = gprvalue
;
491 if (next_arg
.ul
== gpr_end
.ul
)
492 next_arg
.ul
= rest
.ul
;
497 FFI_ASSERT (flags
& FLAG_4_GPR_ARGUMENTS
498 || (next_arg
.ul
>= gpr_base
.ul
499 && next_arg
.ul
<= gpr_base
.ul
+ 4));
504 /* Perform machine dependent cif processing */
506 ffi_prep_cif_machdep (ffi_cif
*cif
)
508 /* All this is for the SYSV and LINUX64 ABI. */
512 int fparg_count
= 0, intarg_count
= 0;
514 unsigned struct_copy_size
= 0;
515 unsigned type
= cif
->rtype
->type
;
516 unsigned size
= cif
->rtype
->size
;
518 if (cif
->abi
!= FFI_LINUX64
)
520 /* All the machine-independent calculation of cif->bytes will be wrong.
521 Redo the calculation for SYSV. */
523 /* Space for the frame pointer, callee's LR, and the asm's temp regs. */
524 bytes
= (2 + ASM_NEEDS_REGISTERS
) * sizeof (int);
526 /* Space for the GPR registers. */
527 bytes
+= NUM_GPR_ARG_REGISTERS
* sizeof (int);
533 /* Space for backchain, CR, LR, cc/ld doubleword, TOC and the asm's temp
535 bytes
= (6 + ASM_NEEDS_REGISTERS64
) * sizeof (long);
537 /* Space for the mandatory parm save area and general registers. */
538 bytes
+= 2 * NUM_GPR_ARG_REGISTERS64
* sizeof (long);
540 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
541 if (type
== FFI_TYPE_LONGDOUBLE
)
542 type
= FFI_TYPE_DOUBLE
;
546 /* Return value handling. The rules for SYSV are as follows:
547 - 32-bit (or less) integer values are returned in gpr3;
548 - Structures of size <= 4 bytes also returned in gpr3;
549 - 64-bit integer values and structures between 5 and 8 bytes are returned
551 - Single/double FP values are returned in fpr1;
552 - Larger structures and long double (if not equivalent to double) values
553 are allocated space and a pointer is passed as the first argument.
555 - integer values in gpr3;
556 - Structures/Unions by reference;
557 - Single/double FP values in fpr1, long double in fpr1,fpr2. */
560 case FFI_TYPE_DOUBLE
:
561 flags
|= FLAG_RETURNS_64BITS
;
564 flags
|= FLAG_RETURNS_FP
;
567 case FFI_TYPE_UINT64
:
568 case FFI_TYPE_SINT64
:
569 flags
|= FLAG_RETURNS_64BITS
;
572 case FFI_TYPE_STRUCT
:
573 if (cif
->abi
== FFI_SYSV
)
575 /* The final SYSV ABI says that structures smaller or equal 8 bytes
576 are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them
579 /* Treat structs with size <= 8 bytes. */
582 flags
|= FLAG_RETURNS_SMST
;
583 /* These structs are returned in r3. We pack the type and the
584 precalculated shift value (needed in the sysv.S) into flags.
585 The same applies for the structs returned in r3/r4. */
588 flags
|= 1 << (31 - FFI_SYSV_TYPE_SMALL_STRUCT
- 1);
589 flags
|= 8 * (4 - size
) << 4;
592 /* These structs are returned in r3 and r4. See above. */
595 flags
|= 1 << (31 - FFI_SYSV_TYPE_SMALL_STRUCT
- 2);
596 flags
|= 8 * (8 - size
) << 4;
601 /* else fall through. */
602 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
603 case FFI_TYPE_LONGDOUBLE
:
604 if (type
== FFI_TYPE_LONGDOUBLE
&& cif
->abi
== FFI_LINUX64
)
606 flags
|= FLAG_RETURNS_128BITS
;
607 flags
|= FLAG_RETURNS_FP
;
612 flags
|= FLAG_RETVAL_REFERENCE
;
615 flags
|= FLAG_RETURNS_NOTHING
;
619 /* Returns 32-bit integer, or similar. Nothing to do here. */
623 if (cif
->abi
!= FFI_LINUX64
)
624 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
625 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
626 goes on the stack. Structures and long doubles (if not equivalent
627 to double) are passed as a pointer to a copy of the structure.
628 Stuff on the stack needs to keep proper alignment. */
629 for (ptr
= cif
->arg_types
, i
= cif
->nargs
; i
> 0; i
--, ptr
++)
631 switch ((*ptr
)->type
)
635 /* floating singles are not 8-aligned on stack */
638 case FFI_TYPE_DOUBLE
:
640 /* If this FP arg is going on the stack, it must be
642 if (fparg_count
> NUM_FPR_ARG_REGISTERS
643 && intarg_count
>= NUM_GPR_ARG_REGISTERS
644 && intarg_count
% 2 != 0)
648 case FFI_TYPE_UINT64
:
649 case FFI_TYPE_SINT64
:
650 /* 'long long' arguments are passed as two words, but
651 either both words must fit in registers or both go
652 on the stack. If they go on the stack, they must
655 Also, only certain register pairs can be used for
656 passing long long int -- specifically (r3,r4), (r5,r6),
659 if (intarg_count
== NUM_GPR_ARG_REGISTERS
-1
660 || intarg_count
% 2 != 0)
665 case FFI_TYPE_STRUCT
:
666 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
667 case FFI_TYPE_LONGDOUBLE
:
669 /* We must allocate space for a copy of these to enforce
670 pass-by-value. Pad the space up to a multiple of 16
671 bytes (the maximum alignment required for anything under
673 struct_copy_size
+= ((*ptr
)->size
+ 15) & ~0xF;
674 /* Fall through (allocate space for the pointer). */
677 /* Everything else is passed as a 4-byte word in a GPR, either
678 the object itself or a pointer to it. */
684 for (ptr
= cif
->arg_types
, i
= cif
->nargs
; i
> 0; i
--, ptr
++)
686 switch ((*ptr
)->type
)
688 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
689 case FFI_TYPE_LONGDOUBLE
:
695 case FFI_TYPE_DOUBLE
:
700 case FFI_TYPE_STRUCT
:
701 intarg_count
+= ((*ptr
)->size
+ 7) / 8;
705 /* Everything else is passed as a 8-byte word in a GPR, either
706 the object itself or a pointer to it. */
712 if (fparg_count
!= 0)
713 flags
|= FLAG_FP_ARGUMENTS
;
714 if (intarg_count
> 4)
715 flags
|= FLAG_4_GPR_ARGUMENTS
;
716 if (struct_copy_size
!= 0)
717 flags
|= FLAG_ARG_NEEDS_COPY
;
719 if (cif
->abi
!= FFI_LINUX64
)
721 /* Space for the FPR registers, if needed. */
722 if (fparg_count
!= 0)
723 bytes
+= NUM_FPR_ARG_REGISTERS
* sizeof (double);
726 if (intarg_count
> NUM_GPR_ARG_REGISTERS
)
727 bytes
+= (intarg_count
- NUM_GPR_ARG_REGISTERS
) * sizeof (int);
728 if (fparg_count
> NUM_FPR_ARG_REGISTERS
)
729 bytes
+= (fparg_count
- NUM_FPR_ARG_REGISTERS
) * sizeof (double);
733 /* Space for the FPR registers, if needed. */
734 if (fparg_count
!= 0)
735 bytes
+= NUM_FPR_ARG_REGISTERS64
* sizeof (double);
738 if (intarg_count
> NUM_GPR_ARG_REGISTERS64
)
739 bytes
+= (intarg_count
- NUM_GPR_ARG_REGISTERS64
) * sizeof (long);
742 /* The stack space allocated needs to be a multiple of 16 bytes. */
743 bytes
= (bytes
+ 15) & ~0xF;
745 /* Add in the space for the copied structures. */
746 bytes
+= struct_copy_size
;
756 extern void ffi_call_SYSV(/*@out@*/ extended_cif
*,
758 /*@out@*/ unsigned *,
760 extern void FFI_HIDDEN
ffi_call_LINUX64(/*@out@*/ extended_cif
*,
761 unsigned long, unsigned long,
762 /*@out@*/ unsigned long *,
768 ffi_call(/*@dependent@*/ ffi_cif
*cif
,
770 /*@out@*/ void *rvalue
,
771 /*@dependent@*/ void **avalue
)
776 ecif
.avalue
= avalue
;
778 /* If the return value is a struct and we don't have a return */
779 /* value address then we need to make one */
781 if ((rvalue
== NULL
) && (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
784 ecif
.rvalue
= alloca(cif
->rtype
->size
);
788 ecif
.rvalue
= rvalue
;
797 ffi_call_SYSV (&ecif
, -cif
->bytes
, cif
->flags
, ecif
.rvalue
, fn
);
803 ffi_call_LINUX64 (&ecif
, -(long) cif
->bytes
, cif
->flags
, ecif
.rvalue
, fn
);
815 #define MIN_CACHE_LINE_SIZE 8
818 flush_icache (char *addr1
, int size
)
822 for (i
= 0; i
< size
; i
+= MIN_CACHE_LINE_SIZE
)
825 __asm__
volatile ("icbi 0,%0;" "dcbf 0,%0;"
826 : : "r" (addr
) : "memory");
828 addr
= addr1
+ size
- 1;
829 __asm__
volatile ("icbi 0,%0;" "dcbf 0,%0;" "sync;" "isync;"
830 : : "r"(addr
) : "memory");
835 ffi_prep_closure (ffi_closure
*closure
,
837 void (*fun
) (ffi_cif
*, void *, void **, void *),
841 void **tramp
= (void **) &closure
->tramp
[0];
843 FFI_ASSERT (cif
->abi
== FFI_LINUX64
);
844 /* Copy function address and TOC from ffi_closure_LINUX64. */
845 memcpy (tramp
, (char *) ffi_closure_LINUX64
, 16);
846 tramp
[2] = (void *) closure
;
850 FFI_ASSERT (cif
->abi
== FFI_GCC_SYSV
|| cif
->abi
== FFI_SYSV
);
852 tramp
= (unsigned int *) &closure
->tramp
[0];
853 tramp
[0] = 0x7c0802a6; /* mflr r0 */
854 tramp
[1] = 0x4800000d; /* bl 10 <trampoline_initial+0x10> */
855 tramp
[4] = 0x7d6802a6; /* mflr r11 */
856 tramp
[5] = 0x7c0803a6; /* mtlr r0 */
857 tramp
[6] = 0x800b0000; /* lwz r0,0(r11) */
858 tramp
[7] = 0x816b0004; /* lwz r11,4(r11) */
859 tramp
[8] = 0x7c0903a6; /* mtctr r0 */
860 tramp
[9] = 0x4e800420; /* bctr */
861 *(void **) &tramp
[2] = (void *) ffi_closure_SYSV
; /* function */
862 *(void **) &tramp
[3] = (void *) closure
; /* context */
864 /* Flush the icache. */
865 flush_icache (&closure
->tramp
[0],FFI_TRAMPOLINE_SIZE
);
870 closure
->user_data
= user_data
;
881 int ffi_closure_helper_SYSV (ffi_closure
*, void *, unsigned long *,
882 ffi_dblfl
*, unsigned long *);
884 /* Basically the trampoline invokes ffi_closure_SYSV, and on
885 * entry, r11 holds the address of the closure.
886 * After storing the registers that could possibly contain
887 * parameters to be passed into the stack frame and setting
888 * up space for a return value, ffi_closure_SYSV invokes the
889 * following helper function to do most of the work
893 ffi_closure_helper_SYSV (ffi_closure
*closure
, void *rvalue
,
894 unsigned long *pgr
, ffi_dblfl
*pfr
,
897 /* rvalue is the pointer to space for return value in closure assembly */
898 /* pgr is the pointer to where r3-r10 are stored in ffi_closure_SYSV */
899 /* pfr is the pointer to where f1-f8 are stored in ffi_closure_SYSV */
900 /* pst is the pointer to outgoing parameter stack in original caller */
903 ffi_type
** arg_types
;
905 long nf
; /* number of floating registers already used */
906 long ng
; /* number of general registers already used */
912 avalue
= alloca (cif
->nargs
* sizeof (void *));
913 size
= cif
->rtype
->size
;
918 /* Copy the caller's structure return value address so that the closure
919 returns the data directly to the caller.
920 For FFI_SYSV the result is passed in r3/r4 if the struct size is less
923 if (cif
->rtype
->type
== FFI_TYPE_STRUCT
)
925 if (!((cif
->abi
== FFI_SYSV
) && (size
<= 8)))
927 rvalue
= (void *) *pgr
;
935 arg_types
= cif
->arg_types
;
937 /* Grab the addresses of the arguments from the stack frame. */
940 switch (arg_types
[i
]->type
)
944 /* there are 8 gpr registers used to pass values */
947 avalue
[i
] = (char *) pgr
+ 3;
953 avalue
[i
] = (char *) pst
+ 3;
958 case FFI_TYPE_SINT16
:
959 case FFI_TYPE_UINT16
:
960 /* there are 8 gpr registers used to pass values */
963 avalue
[i
] = (char *) pgr
+ 2;
969 avalue
[i
] = (char *) pst
+ 2;
974 case FFI_TYPE_SINT32
:
975 case FFI_TYPE_UINT32
:
976 case FFI_TYPE_POINTER
:
977 /* there are 8 gpr registers used to pass values */
991 case FFI_TYPE_STRUCT
:
992 /* Structs are passed by reference. The address will appear in a
993 gpr if it is one of the first 8 arguments. */
996 avalue
[i
] = (void *) *pgr
;
1002 avalue
[i
] = (void *) *pst
;
1007 case FFI_TYPE_SINT64
:
1008 case FFI_TYPE_UINT64
:
1009 /* passing long long ints are complex, they must
1010 * be passed in suitable register pairs such as
1011 * (r3,r4) or (r5,r6) or (r6,r7), or (r7,r8) or (r9,r10)
1012 * and if the entire pair aren't available then the outgoing
1013 * parameter stack is used for both but an alignment of 8
1014 * must will be kept. So we must either look in pgr
1015 * or pst to find the correct address for this type
1022 /* skip r4, r6, r8 as starting points */
1032 if (((long) pst
) & 4)
1039 case FFI_TYPE_FLOAT
:
1040 /* unfortunately float values are stored as doubles
1041 * in the ffi_closure_SYSV code (since we don't check
1042 * the type in that routine).
1045 /* there are 8 64bit floating point registers */
1050 pfr
->f
= (float) temp
;
1057 /* FIXME? here we are really changing the values
1058 * stored in the original calling routines outgoing
1059 * parameter stack. This is probably a really
1060 * naughty thing to do but...
1068 case FFI_TYPE_DOUBLE
:
1069 /* On the outgoing stack all values are aligned to 8 */
1070 /* there are 8 64bit floating point registers */
1080 if (((long) pst
) & 4)
1096 (closure
->fun
) (cif
, rvalue
, avalue
, closure
->user_data
);
1098 /* Tell ffi_closure_SYSV how to perform return type promotions.
1099 Because the FFI_SYSV ABI returns the structures <= 8 bytes in r3/r4
1100 we have to tell ffi_closure_SYSV how to treat them. */
1101 if (cif
->abi
== FFI_SYSV
&& cif
->rtype
->type
== FFI_TYPE_STRUCT
1103 return FFI_SYSV_TYPE_SMALL_STRUCT
+ size
;
1104 return cif
->rtype
->type
;
1108 int FFI_HIDDEN
ffi_closure_helper_LINUX64 (ffi_closure
*, void *,
1109 unsigned long *, ffi_dblfl
*);
1112 ffi_closure_helper_LINUX64 (ffi_closure
*closure
, void *rvalue
,
1113 unsigned long *pst
, ffi_dblfl
*pfr
)
1115 /* rvalue is the pointer to space for return value in closure assembly */
1116 /* pst is the pointer to parameter save area
1117 (r3-r10 are stored into its first 8 slots by ffi_closure_LINUX64) */
1118 /* pfr is the pointer to where f1-f13 are stored in ffi_closure_LINUX64 */
1121 ffi_type
**arg_types
;
1124 ffi_dblfl
*end_pfr
= pfr
+ NUM_FPR_ARG_REGISTERS64
;
1127 avalue
= alloca (cif
->nargs
* sizeof (void *));
1129 /* Copy the caller's structure return value address so that the closure
1130 returns the data directly to the caller. */
1131 if (cif
->rtype
->type
== FFI_TYPE_STRUCT
)
1133 rvalue
= (void *) *pst
;
1139 arg_types
= cif
->arg_types
;
1141 /* Grab the addresses of the arguments from the stack frame. */
1144 switch (arg_types
[i
]->type
)
1146 case FFI_TYPE_SINT8
:
1147 case FFI_TYPE_UINT8
:
1148 avalue
[i
] = (char *) pst
+ 7;
1152 case FFI_TYPE_SINT16
:
1153 case FFI_TYPE_UINT16
:
1154 avalue
[i
] = (char *) pst
+ 6;
1158 case FFI_TYPE_SINT32
:
1159 case FFI_TYPE_UINT32
:
1160 avalue
[i
] = (char *) pst
+ 4;
1164 case FFI_TYPE_SINT64
:
1165 case FFI_TYPE_UINT64
:
1166 case FFI_TYPE_POINTER
:
1171 case FFI_TYPE_STRUCT
:
1172 /* Structures with size less than eight bytes are passed
1174 if (arg_types
[i
]->size
< 8)
1175 avalue
[i
] = (char *) pst
+ 8 - arg_types
[i
]->size
;
1178 pst
+= (arg_types
[i
]->size
+ 7) / 8;
1181 case FFI_TYPE_FLOAT
:
1182 /* unfortunately float values are stored as doubles
1183 * in the ffi_closure_LINUX64 code (since we don't check
1184 * the type in that routine).
1187 /* there are 13 64bit floating point registers */
1191 double temp
= pfr
->d
;
1192 pfr
->f
= (float) temp
;
1201 case FFI_TYPE_DOUBLE
:
1202 /* On the outgoing stack all values are aligned to 8 */
1203 /* there are 13 64bit floating point registers */
1215 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1216 case FFI_TYPE_LONGDOUBLE
:
1217 if (pfr
+ 1 < end_pfr
)
1226 /* Passed partly in f13 and partly on the stack.
1227 Move it all to the stack. */
1228 *pst
= *(unsigned long *) pfr
;
1245 (closure
->fun
) (cif
, rvalue
, avalue
, closure
->user_data
);
1247 /* Tell ffi_closure_LINUX64 how to perform return type promotions. */
1248 return cif
->rtype
->type
;