1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1998 Geoffrey Keating
3 Copyright (C) 2007 Free Software Foundation, Inc
5 PowerPC Foreign Function Interface
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 ``Software''), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 OTHER DEALINGS IN THE SOFTWARE.
25 ----------------------------------------------------------------------- */
28 #include <ffi_common.h>
34 extern void ffi_closure_SYSV (void);
35 extern void FFI_HIDDEN
ffi_closure_LINUX64 (void);
38 /* The assembly depends on these exact flags. */
39 FLAG_RETURNS_SMST
= 1 << (31-31), /* Used for FFI_SYSV small structs. */
40 FLAG_RETURNS_NOTHING
= 1 << (31-30), /* These go in cr7 */
41 FLAG_RETURNS_FP
= 1 << (31-29),
42 FLAG_RETURNS_64BITS
= 1 << (31-28),
44 FLAG_RETURNS_128BITS
= 1 << (31-27), /* cr6 */
46 FLAG_ARG_NEEDS_COPY
= 1 << (31- 7),
47 FLAG_FP_ARGUMENTS
= 1 << (31- 6), /* cr1.eq; specified by ABI */
48 FLAG_4_GPR_ARGUMENTS
= 1 << (31- 5),
49 FLAG_RETVAL_REFERENCE
= 1 << (31- 4)
52 /* About the SYSV ABI. */
53 unsigned int NUM_GPR_ARG_REGISTERS
= 8;
55 unsigned int NUM_FPR_ARG_REGISTERS
= 8;
57 unsigned int NUM_FPR_ARG_REGISTERS
= 0;
60 enum { ASM_NEEDS_REGISTERS
= 4 };
62 /* ffi_prep_args_SYSV is called by the assembly routine once stack space
63 has been allocated for the function's arguments.
65 The stack layout we want looks like this:
67 | Return address from ffi_call_SYSV 4bytes | higher addresses
68 |--------------------------------------------|
69 | Previous backchain pointer 4 | stack pointer here
70 |--------------------------------------------|<+ <<< on entry to
71 | Saved r28-r31 4*4 | | ffi_call_SYSV
72 |--------------------------------------------| |
73 | GPR registers r3-r10 8*4 | | ffi_call_SYSV
74 |--------------------------------------------| |
75 | FPR registers f1-f8 (optional) 8*8 | |
76 |--------------------------------------------| | stack |
77 | Space for copied structures | | grows |
78 |--------------------------------------------| | down V
79 | Parameters that didn't fit in registers | |
80 |--------------------------------------------| | lower addresses
81 | Space for callee's LR 4 | |
82 |--------------------------------------------| | stack pointer here
83 | Current backchain pointer 4 |-/ during
84 |--------------------------------------------| <<< ffi_call_SYSV
89 ffi_prep_args_SYSV (extended_cif
*ecif
, unsigned *const stack
)
91 const unsigned bytes
= ecif
->cif
->bytes
;
92 const unsigned flags
= ecif
->cif
->flags
;
102 /* 'stacktop' points at the previous backchain pointer. */
105 /* 'gpr_base' points at the space for gpr3, and grows upwards as
106 we use GPR registers. */
110 /* 'fpr_base' points at the space for fpr1, and grows upwards as
111 we use FPR registers. */
115 /* 'copy_space' grows down as we put structures in it. It should
116 stay 16-byte aligned. */
119 /* 'next_arg' grows up as we put parameters in it. */
122 int i
, ii MAYBE_UNUSED
;
137 size_t struct_copy_size
;
140 if (ecif
->cif
->abi
== FFI_LINUX_SOFT_FLOAT
)
141 NUM_FPR_ARG_REGISTERS
= 0;
143 stacktop
.c
= (char *) stack
+ bytes
;
144 gpr_base
.u
= stacktop
.u
- ASM_NEEDS_REGISTERS
- NUM_GPR_ARG_REGISTERS
;
146 fpr_base
.d
= gpr_base
.d
- NUM_FPR_ARG_REGISTERS
;
148 copy_space
.c
= ((flags
& FLAG_FP_ARGUMENTS
) ? fpr_base
.c
: gpr_base
.c
);
149 next_arg
.u
= stack
+ 2;
151 /* Check that everything starts aligned properly. */
152 FFI_ASSERT (((unsigned) (char *) stack
& 0xF) == 0);
153 FFI_ASSERT (((unsigned) copy_space
.c
& 0xF) == 0);
154 FFI_ASSERT (((unsigned) stacktop
.c
& 0xF) == 0);
155 FFI_ASSERT ((bytes
& 0xF) == 0);
156 FFI_ASSERT (copy_space
.c
>= next_arg
.c
);
158 /* Deal with return values that are actually pass-by-reference. */
159 if (flags
& FLAG_RETVAL_REFERENCE
)
161 *gpr_base
.u
++ = (unsigned long) (char *) ecif
->rvalue
;
165 /* Now for the arguments. */
166 p_argv
.v
= ecif
->avalue
;
167 for (ptr
= ecif
->cif
->arg_types
, i
= ecif
->cif
->nargs
;
169 i
--, ptr
++, p_argv
.v
++)
171 switch ((*ptr
)->type
)
174 /* With FFI_LINUX_SOFT_FLOAT floats are handled like UINT32. */
175 if (ecif
->cif
->abi
== FFI_LINUX_SOFT_FLOAT
)
176 goto soft_float_prep
;
177 double_tmp
= **p_argv
.f
;
178 if (fparg_count
>= NUM_FPR_ARG_REGISTERS
)
180 *next_arg
.f
= (float) double_tmp
;
184 *fpr_base
.d
++ = double_tmp
;
186 FFI_ASSERT (flags
& FLAG_FP_ARGUMENTS
);
189 case FFI_TYPE_DOUBLE
:
190 /* With FFI_LINUX_SOFT_FLOAT doubles are handled like UINT64. */
191 if (ecif
->cif
->abi
== FFI_LINUX_SOFT_FLOAT
)
192 goto soft_double_prep
;
193 double_tmp
= **p_argv
.d
;
195 if (fparg_count
>= NUM_FPR_ARG_REGISTERS
)
197 if (intarg_count
>= NUM_GPR_ARG_REGISTERS
198 && intarg_count
% 2 != 0)
203 *next_arg
.d
= double_tmp
;
207 *fpr_base
.d
++ = double_tmp
;
209 FFI_ASSERT (flags
& FLAG_FP_ARGUMENTS
);
212 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
213 case FFI_TYPE_LONGDOUBLE
:
214 if ((ecif
->cif
->abi
!= FFI_LINUX
)
215 && (ecif
->cif
->abi
!= FFI_LINUX_SOFT_FLOAT
))
217 /* The soft float ABI for long doubles works like this,
218 a long double is passed in four consecutive gprs if available.
219 A maximum of 2 long doubles can be passed in gprs.
220 If we do not have 4 gprs left, the long double is passed on the
221 stack, 4-byte aligned. */
222 if (ecif
->cif
->abi
== FFI_LINUX_SOFT_FLOAT
)
224 unsigned int int_tmp
= (*p_argv
.ui
)[0];
225 if (intarg_count
>= NUM_GPR_ARG_REGISTERS
- 3)
227 if (intarg_count
< NUM_GPR_ARG_REGISTERS
)
228 intarg_count
+= NUM_GPR_ARG_REGISTERS
- intarg_count
;
229 *next_arg
.u
= int_tmp
;
231 for (ii
= 1; ii
< 4; ii
++)
233 int_tmp
= (*p_argv
.ui
)[ii
];
234 *next_arg
.u
= int_tmp
;
240 *gpr_base
.u
++ = int_tmp
;
241 for (ii
= 1; ii
< 4; ii
++)
243 int_tmp
= (*p_argv
.ui
)[ii
];
244 *gpr_base
.u
++ = int_tmp
;
251 double_tmp
= (*p_argv
.d
)[0];
253 if (fparg_count
>= NUM_FPR_ARG_REGISTERS
- 1)
255 if (intarg_count
>= NUM_GPR_ARG_REGISTERS
256 && intarg_count
% 2 != 0)
261 *next_arg
.d
= double_tmp
;
263 double_tmp
= (*p_argv
.d
)[1];
264 *next_arg
.d
= double_tmp
;
269 *fpr_base
.d
++ = double_tmp
;
270 double_tmp
= (*p_argv
.d
)[1];
271 *fpr_base
.d
++ = double_tmp
;
275 FFI_ASSERT (flags
& FLAG_FP_ARGUMENTS
);
280 case FFI_TYPE_UINT64
:
281 case FFI_TYPE_SINT64
:
283 if (intarg_count
== NUM_GPR_ARG_REGISTERS
-1)
285 if (intarg_count
>= NUM_GPR_ARG_REGISTERS
)
287 if (intarg_count
% 2 != 0)
292 *next_arg
.ll
= **p_argv
.ll
;
297 /* whoops: abi states only certain register pairs
298 * can be used for passing long long int
299 * specifically (r3,r4), (r5,r6), (r7,r8),
300 * (r9,r10) and if next arg is long long but
301 * not correct starting register of pair then skip
302 * until the proper starting register
304 if (intarg_count
% 2 != 0)
309 *gpr_base
.ll
++ = **p_argv
.ll
;
314 case FFI_TYPE_STRUCT
:
315 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
318 struct_copy_size
= ((*ptr
)->size
+ 15) & ~0xF;
319 copy_space
.c
-= struct_copy_size
;
320 memcpy (copy_space
.c
, *p_argv
.c
, (*ptr
)->size
);
322 gprvalue
= (unsigned long) copy_space
.c
;
324 FFI_ASSERT (copy_space
.c
> next_arg
.c
);
325 FFI_ASSERT (flags
& FLAG_ARG_NEEDS_COPY
);
329 gprvalue
= **p_argv
.uc
;
332 gprvalue
= **p_argv
.sc
;
334 case FFI_TYPE_UINT16
:
335 gprvalue
= **p_argv
.us
;
337 case FFI_TYPE_SINT16
:
338 gprvalue
= **p_argv
.ss
;
342 case FFI_TYPE_UINT32
:
343 case FFI_TYPE_SINT32
:
344 case FFI_TYPE_POINTER
:
347 gprvalue
= **p_argv
.ui
;
350 if (intarg_count
>= NUM_GPR_ARG_REGISTERS
)
351 *next_arg
.u
++ = gprvalue
;
353 *gpr_base
.u
++ = gprvalue
;
359 /* Check that we didn't overrun the stack... */
360 FFI_ASSERT (copy_space
.c
>= next_arg
.c
);
361 FFI_ASSERT (gpr_base
.u
<= stacktop
.u
- ASM_NEEDS_REGISTERS
);
362 FFI_ASSERT (fpr_base
.u
363 <= stacktop
.u
- ASM_NEEDS_REGISTERS
- NUM_GPR_ARG_REGISTERS
);
364 FFI_ASSERT (flags
& FLAG_4_GPR_ARGUMENTS
|| intarg_count
<= 4);
367 /* About the LINUX64 ABI. */
369 NUM_GPR_ARG_REGISTERS64
= 8,
370 NUM_FPR_ARG_REGISTERS64
= 13
372 enum { ASM_NEEDS_REGISTERS64
= 4 };
374 /* ffi_prep_args64 is called by the assembly routine once stack space
375 has been allocated for the function's arguments.
377 The stack layout we want looks like this:
379 | Ret addr from ffi_call_LINUX64 8bytes | higher addresses
380 |--------------------------------------------|
381 | CR save area 8bytes |
382 |--------------------------------------------|
383 | Previous backchain pointer 8 | stack pointer here
384 |--------------------------------------------|<+ <<< on entry to
385 | Saved r28-r31 4*8 | | ffi_call_LINUX64
386 |--------------------------------------------| |
387 | GPR registers r3-r10 8*8 | |
388 |--------------------------------------------| |
389 | FPR registers f1-f13 (optional) 13*8 | |
390 |--------------------------------------------| |
391 | Parameter save area | |
392 |--------------------------------------------| |
393 | TOC save area 8 | |
394 |--------------------------------------------| | stack |
395 | Linker doubleword 8 | | grows |
396 |--------------------------------------------| | down V
397 | Compiler doubleword 8 | |
398 |--------------------------------------------| | lower addresses
399 | Space for callee's LR 8 | |
400 |--------------------------------------------| |
402 |--------------------------------------------| | stack pointer here
403 | Current backchain pointer 8 |-/ during
404 |--------------------------------------------| <<< ffi_call_LINUX64
409 ffi_prep_args64 (extended_cif
*ecif
, unsigned long *const stack
)
411 const unsigned long bytes
= ecif
->cif
->bytes
;
412 const unsigned long flags
= ecif
->cif
->flags
;
421 /* 'stacktop' points at the previous backchain pointer. */
424 /* 'next_arg' points at the space for gpr3, and grows upwards as
425 we use GPR registers, then continues at rest. */
431 /* 'fpr_base' points at the space for fpr3, and grows upwards as
432 we use FPR registers. */
452 unsigned long gprvalue
;
454 stacktop
.c
= (char *) stack
+ bytes
;
455 gpr_base
.ul
= stacktop
.ul
- ASM_NEEDS_REGISTERS64
- NUM_GPR_ARG_REGISTERS64
;
456 gpr_end
.ul
= gpr_base
.ul
+ NUM_GPR_ARG_REGISTERS64
;
457 rest
.ul
= stack
+ 6 + NUM_GPR_ARG_REGISTERS64
;
458 fpr_base
.d
= gpr_base
.d
- NUM_FPR_ARG_REGISTERS64
;
460 next_arg
.ul
= gpr_base
.ul
;
462 /* Check that everything starts aligned properly. */
463 FFI_ASSERT (((unsigned long) (char *) stack
& 0xF) == 0);
464 FFI_ASSERT (((unsigned long) stacktop
.c
& 0xF) == 0);
465 FFI_ASSERT ((bytes
& 0xF) == 0);
467 /* Deal with return values that are actually pass-by-reference. */
468 if (flags
& FLAG_RETVAL_REFERENCE
)
469 *next_arg
.ul
++ = (unsigned long) (char *) ecif
->rvalue
;
471 /* Now for the arguments. */
472 p_argv
.v
= ecif
->avalue
;
473 for (ptr
= ecif
->cif
->arg_types
, i
= ecif
->cif
->nargs
;
475 i
--, ptr
++, p_argv
.v
++)
477 switch ((*ptr
)->type
)
480 double_tmp
= **p_argv
.f
;
481 *next_arg
.f
= (float) double_tmp
;
482 if (++next_arg
.ul
== gpr_end
.ul
)
483 next_arg
.ul
= rest
.ul
;
484 if (fparg_count
< NUM_FPR_ARG_REGISTERS64
)
485 *fpr_base
.d
++ = double_tmp
;
487 FFI_ASSERT (flags
& FLAG_FP_ARGUMENTS
);
490 case FFI_TYPE_DOUBLE
:
491 double_tmp
= **p_argv
.d
;
492 *next_arg
.d
= double_tmp
;
493 if (++next_arg
.ul
== gpr_end
.ul
)
494 next_arg
.ul
= rest
.ul
;
495 if (fparg_count
< NUM_FPR_ARG_REGISTERS64
)
496 *fpr_base
.d
++ = double_tmp
;
498 FFI_ASSERT (flags
& FLAG_FP_ARGUMENTS
);
501 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
502 case FFI_TYPE_LONGDOUBLE
:
503 double_tmp
= (*p_argv
.d
)[0];
504 *next_arg
.d
= double_tmp
;
505 if (++next_arg
.ul
== gpr_end
.ul
)
506 next_arg
.ul
= rest
.ul
;
507 if (fparg_count
< NUM_FPR_ARG_REGISTERS64
)
508 *fpr_base
.d
++ = double_tmp
;
510 double_tmp
= (*p_argv
.d
)[1];
511 *next_arg
.d
= double_tmp
;
512 if (++next_arg
.ul
== gpr_end
.ul
)
513 next_arg
.ul
= rest
.ul
;
514 if (fparg_count
< NUM_FPR_ARG_REGISTERS64
)
515 *fpr_base
.d
++ = double_tmp
;
517 FFI_ASSERT (__LDBL_MANT_DIG__
== 106);
518 FFI_ASSERT (flags
& FLAG_FP_ARGUMENTS
);
522 case FFI_TYPE_STRUCT
:
523 words
= ((*ptr
)->size
+ 7) / 8;
524 if (next_arg
.ul
>= gpr_base
.ul
&& next_arg
.ul
+ words
> gpr_end
.ul
)
526 size_t first
= gpr_end
.c
- next_arg
.c
;
527 memcpy (next_arg
.c
, *p_argv
.c
, first
);
528 memcpy (rest
.c
, *p_argv
.c
+ first
, (*ptr
)->size
- first
);
529 next_arg
.c
= rest
.c
+ words
* 8 - first
;
533 char *where
= next_arg
.c
;
535 /* Structures with size less than eight bytes are passed
537 if ((*ptr
)->size
< 8)
538 where
+= 8 - (*ptr
)->size
;
540 memcpy (where
, *p_argv
.c
, (*ptr
)->size
);
541 next_arg
.ul
+= words
;
542 if (next_arg
.ul
== gpr_end
.ul
)
543 next_arg
.ul
= rest
.ul
;
548 gprvalue
= **p_argv
.uc
;
551 gprvalue
= **p_argv
.sc
;
553 case FFI_TYPE_UINT16
:
554 gprvalue
= **p_argv
.us
;
556 case FFI_TYPE_SINT16
:
557 gprvalue
= **p_argv
.ss
;
559 case FFI_TYPE_UINT32
:
560 gprvalue
= **p_argv
.ui
;
563 case FFI_TYPE_SINT32
:
564 gprvalue
= **p_argv
.si
;
567 case FFI_TYPE_UINT64
:
568 case FFI_TYPE_SINT64
:
569 case FFI_TYPE_POINTER
:
570 gprvalue
= **p_argv
.ul
;
572 *next_arg
.ul
++ = gprvalue
;
573 if (next_arg
.ul
== gpr_end
.ul
)
574 next_arg
.ul
= rest
.ul
;
579 FFI_ASSERT (flags
& FLAG_4_GPR_ARGUMENTS
580 || (next_arg
.ul
>= gpr_base
.ul
581 && next_arg
.ul
<= gpr_base
.ul
+ 4));
586 /* Perform machine dependent cif processing */
588 ffi_prep_cif_machdep (ffi_cif
*cif
)
590 /* All this is for the SYSV and LINUX64 ABI. */
594 int fparg_count
= 0, intarg_count
= 0;
596 unsigned struct_copy_size
= 0;
597 unsigned type
= cif
->rtype
->type
;
598 unsigned size
= cif
->rtype
->size
;
600 if (cif
->abi
== FFI_LINUX_SOFT_FLOAT
)
601 NUM_FPR_ARG_REGISTERS
= 0;
603 if (cif
->abi
!= FFI_LINUX64
)
605 /* All the machine-independent calculation of cif->bytes will be wrong.
606 Redo the calculation for SYSV. */
608 /* Space for the frame pointer, callee's LR, and the asm's temp regs. */
609 bytes
= (2 + ASM_NEEDS_REGISTERS
) * sizeof (int);
611 /* Space for the GPR registers. */
612 bytes
+= NUM_GPR_ARG_REGISTERS
* sizeof (int);
618 /* Space for backchain, CR, LR, cc/ld doubleword, TOC and the asm's temp
620 bytes
= (6 + ASM_NEEDS_REGISTERS64
) * sizeof (long);
622 /* Space for the mandatory parm save area and general registers. */
623 bytes
+= 2 * NUM_GPR_ARG_REGISTERS64
* sizeof (long);
626 /* Return value handling. The rules for SYSV are as follows:
627 - 32-bit (or less) integer values are returned in gpr3;
628 - Structures of size <= 4 bytes also returned in gpr3;
629 - 64-bit integer values and structures between 5 and 8 bytes are returned
631 - Single/double FP values are returned in fpr1;
632 - Larger structures are allocated space and a pointer is passed as
634 - long doubles (if not equivalent to double) are returned in
635 fpr1,fpr2 for Linux and as for large structs for SysV.
637 - integer values in gpr3;
638 - Structures/Unions by reference;
639 - Single/double FP values in fpr1, long double in fpr1,fpr2.
640 - soft-float float/doubles are treated as UINT32/UINT64 respectivley.
641 - soft-float long doubles are returned in gpr3-gpr6. */
644 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
645 case FFI_TYPE_LONGDOUBLE
:
646 if (cif
->abi
!= FFI_LINUX
&& cif
->abi
!= FFI_LINUX64
647 && cif
->abi
!= FFI_LINUX_SOFT_FLOAT
)
649 flags
|= FLAG_RETURNS_128BITS
;
652 case FFI_TYPE_DOUBLE
:
653 flags
|= FLAG_RETURNS_64BITS
;
656 /* With FFI_LINUX_SOFT_FLOAT no fp registers are used. */
657 if (cif
->abi
!= FFI_LINUX_SOFT_FLOAT
)
658 flags
|= FLAG_RETURNS_FP
;
661 case FFI_TYPE_UINT64
:
662 case FFI_TYPE_SINT64
:
663 flags
|= FLAG_RETURNS_64BITS
;
666 case FFI_TYPE_STRUCT
:
667 if (cif
->abi
== FFI_SYSV
)
669 /* The final SYSV ABI says that structures smaller or equal 8 bytes
670 are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them
673 /* Treat structs with size <= 8 bytes. */
676 flags
|= FLAG_RETURNS_SMST
;
677 /* These structs are returned in r3. We pack the type and the
678 precalculated shift value (needed in the sysv.S) into flags.
679 The same applies for the structs returned in r3/r4. */
682 flags
|= 1 << (31 - FFI_SYSV_TYPE_SMALL_STRUCT
- 1);
683 flags
|= 8 * (4 - size
) << 4;
686 /* These structs are returned in r3 and r4. See above. */
689 flags
|= 1 << (31 - FFI_SYSV_TYPE_SMALL_STRUCT
- 2);
690 flags
|= 8 * (8 - size
) << 4;
695 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
699 flags
|= FLAG_RETVAL_REFERENCE
;
702 flags
|= FLAG_RETURNS_NOTHING
;
706 /* Returns 32-bit integer, or similar. Nothing to do here. */
710 if (cif
->abi
!= FFI_LINUX64
)
711 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
712 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
713 goes on the stack. Structures and long doubles (if not equivalent
714 to double) are passed as a pointer to a copy of the structure.
715 Stuff on the stack needs to keep proper alignment. */
716 for (ptr
= cif
->arg_types
, i
= cif
->nargs
; i
> 0; i
--, ptr
++)
718 switch ((*ptr
)->type
)
721 /* With FFI_LINUX_SOFT_FLOAT floats are handled like UINT32. */
722 if (cif
->abi
== FFI_LINUX_SOFT_FLOAT
)
725 /* floating singles are not 8-aligned on stack */
728 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
729 case FFI_TYPE_LONGDOUBLE
:
730 if (cif
->abi
!= FFI_LINUX
&& cif
->abi
!= FFI_LINUX_SOFT_FLOAT
)
732 if (cif
->abi
== FFI_LINUX_SOFT_FLOAT
)
734 if (intarg_count
>= NUM_GPR_ARG_REGISTERS
- 3
735 || intarg_count
< NUM_GPR_ARG_REGISTERS
)
736 /* A long double in FFI_LINUX_SOFT_FLOAT can use only
737 a set of four consecutive gprs. If we have not enough,
738 we have to adjust the intarg_count value. */
739 intarg_count
+= NUM_GPR_ARG_REGISTERS
- intarg_count
;
747 case FFI_TYPE_DOUBLE
:
748 /* With FFI_LINUX_SOFT_FLOAT doubles are handled like UINT64. */
749 if (cif
->abi
== FFI_LINUX_SOFT_FLOAT
)
750 goto soft_double_cif
;
752 /* If this FP arg is going on the stack, it must be
754 if (fparg_count
> NUM_FPR_ARG_REGISTERS
755 && intarg_count
>= NUM_GPR_ARG_REGISTERS
756 && intarg_count
% 2 != 0)
760 case FFI_TYPE_UINT64
:
761 case FFI_TYPE_SINT64
:
763 /* 'long long' arguments are passed as two words, but
764 either both words must fit in registers or both go
765 on the stack. If they go on the stack, they must
768 Also, only certain register pairs can be used for
769 passing long long int -- specifically (r3,r4), (r5,r6),
772 if (intarg_count
== NUM_GPR_ARG_REGISTERS
-1
773 || intarg_count
% 2 != 0)
778 case FFI_TYPE_STRUCT
:
779 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
782 /* We must allocate space for a copy of these to enforce
783 pass-by-value. Pad the space up to a multiple of 16
784 bytes (the maximum alignment required for anything under
786 struct_copy_size
+= ((*ptr
)->size
+ 15) & ~0xF;
787 /* Fall through (allocate space for the pointer). */
791 /* Everything else is passed as a 4-byte word in a GPR, either
792 the object itself or a pointer to it. */
798 for (ptr
= cif
->arg_types
, i
= cif
->nargs
; i
> 0; i
--, ptr
++)
800 switch ((*ptr
)->type
)
802 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
803 case FFI_TYPE_LONGDOUBLE
:
804 if (cif
->abi
== FFI_LINUX_SOFT_FLOAT
)
814 case FFI_TYPE_DOUBLE
:
819 case FFI_TYPE_STRUCT
:
820 intarg_count
+= ((*ptr
)->size
+ 7) / 8;
824 /* Everything else is passed as a 8-byte word in a GPR, either
825 the object itself or a pointer to it. */
831 if (fparg_count
!= 0)
832 flags
|= FLAG_FP_ARGUMENTS
;
833 if (intarg_count
> 4)
834 flags
|= FLAG_4_GPR_ARGUMENTS
;
835 if (struct_copy_size
!= 0)
836 flags
|= FLAG_ARG_NEEDS_COPY
;
838 if (cif
->abi
!= FFI_LINUX64
)
840 /* Space for the FPR registers, if needed. */
841 if (fparg_count
!= 0)
842 bytes
+= NUM_FPR_ARG_REGISTERS
* sizeof (double);
845 if (intarg_count
> NUM_GPR_ARG_REGISTERS
)
846 bytes
+= (intarg_count
- NUM_GPR_ARG_REGISTERS
) * sizeof (int);
847 if (fparg_count
> NUM_FPR_ARG_REGISTERS
)
848 bytes
+= (fparg_count
- NUM_FPR_ARG_REGISTERS
) * sizeof (double);
852 /* Space for the FPR registers, if needed. */
853 if (fparg_count
!= 0)
854 bytes
+= NUM_FPR_ARG_REGISTERS64
* sizeof (double);
857 if (intarg_count
> NUM_GPR_ARG_REGISTERS64
)
858 bytes
+= (intarg_count
- NUM_GPR_ARG_REGISTERS64
) * sizeof (long);
861 /* The stack space allocated needs to be a multiple of 16 bytes. */
862 bytes
= (bytes
+ 15) & ~0xF;
864 /* Add in the space for the copied structures. */
865 bytes
+= struct_copy_size
;
873 extern void ffi_call_SYSV(extended_cif
*, unsigned, unsigned, unsigned *,
875 extern void FFI_HIDDEN
ffi_call_LINUX64(extended_cif
*, unsigned long,
876 unsigned long, unsigned long *,
880 ffi_call(ffi_cif
*cif
, void (*fn
)(), void *rvalue
, void **avalue
)
885 ecif
.avalue
= avalue
;
887 /* If the return value is a struct and we don't have a return */
888 /* value address then we need to make one */
890 if ((rvalue
== NULL
) && (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
892 ecif
.rvalue
= alloca(cif
->rtype
->size
);
895 ecif
.rvalue
= rvalue
;
904 case FFI_LINUX_SOFT_FLOAT
:
905 ffi_call_SYSV (&ecif
, -cif
->bytes
, cif
->flags
, ecif
.rvalue
, fn
);
909 ffi_call_LINUX64 (&ecif
, -(long) cif
->bytes
, cif
->flags
, ecif
.rvalue
, fn
);
920 #define MIN_CACHE_LINE_SIZE 8
923 flush_icache (char *wraddr
, char *xaddr
, int size
)
926 for (i
= 0; i
< size
; i
+= MIN_CACHE_LINE_SIZE
)
927 __asm__
volatile ("icbi 0,%0;" "dcbf 0,%1;"
928 : : "r" (xaddr
+ i
), "r" (wraddr
+ i
) : "memory");
929 __asm__
volatile ("icbi 0,%0;" "dcbf 0,%1;" "sync;" "isync;"
930 : : "r"(xaddr
+ size
- 1), "r"(wraddr
+ size
- 1)
936 ffi_prep_closure_loc (ffi_closure
*closure
,
938 void (*fun
) (ffi_cif
*, void *, void **, void *),
943 void **tramp
= (void **) &closure
->tramp
[0];
945 FFI_ASSERT (cif
->abi
== FFI_LINUX64
);
946 /* Copy function address and TOC from ffi_closure_LINUX64. */
947 memcpy (tramp
, (char *) ffi_closure_LINUX64
, 16);
952 FFI_ASSERT (cif
->abi
== FFI_GCC_SYSV
|| cif
->abi
== FFI_SYSV
);
954 tramp
= (unsigned int *) &closure
->tramp
[0];
955 tramp
[0] = 0x7c0802a6; /* mflr r0 */
956 tramp
[1] = 0x4800000d; /* bl 10 <trampoline_initial+0x10> */
957 tramp
[4] = 0x7d6802a6; /* mflr r11 */
958 tramp
[5] = 0x7c0803a6; /* mtlr r0 */
959 tramp
[6] = 0x800b0000; /* lwz r0,0(r11) */
960 tramp
[7] = 0x816b0004; /* lwz r11,4(r11) */
961 tramp
[8] = 0x7c0903a6; /* mtctr r0 */
962 tramp
[9] = 0x4e800420; /* bctr */
963 *(void **) &tramp
[2] = (void *) ffi_closure_SYSV
; /* function */
964 *(void **) &tramp
[3] = codeloc
; /* context */
966 /* Flush the icache. */
967 flush_icache ((char *)tramp
, (char *)codeloc
, FFI_TRAMPOLINE_SIZE
);
972 closure
->user_data
= user_data
;
983 int ffi_closure_helper_SYSV (ffi_closure
*, void *, unsigned long *,
984 ffi_dblfl
*, unsigned long *);
986 /* Basically the trampoline invokes ffi_closure_SYSV, and on
987 * entry, r11 holds the address of the closure.
988 * After storing the registers that could possibly contain
989 * parameters to be passed into the stack frame and setting
990 * up space for a return value, ffi_closure_SYSV invokes the
991 * following helper function to do most of the work
995 ffi_closure_helper_SYSV (ffi_closure
*closure
, void *rvalue
,
996 unsigned long *pgr
, ffi_dblfl
*pfr
,
999 /* rvalue is the pointer to space for return value in closure assembly */
1000 /* pgr is the pointer to where r3-r10 are stored in ffi_closure_SYSV */
1001 /* pfr is the pointer to where f1-f8 are stored in ffi_closure_SYSV */
1002 /* pst is the pointer to outgoing parameter stack in original caller */
1005 ffi_type
** arg_types
;
1007 long nf
; /* number of floating registers already used */
1008 long ng
; /* number of general registers already used */
1014 avalue
= alloca (cif
->nargs
* sizeof (void *));
1015 size
= cif
->rtype
->size
;
1020 /* Copy the caller's structure return value address so that the closure
1021 returns the data directly to the caller.
1022 For FFI_SYSV the result is passed in r3/r4 if the struct size is less
1023 or equal 8 bytes. */
1025 if ((cif
->rtype
->type
== FFI_TYPE_STRUCT
1026 && !((cif
->abi
== FFI_SYSV
) && (size
<= 8)))
1027 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1028 || (cif
->rtype
->type
== FFI_TYPE_LONGDOUBLE
1029 && cif
->abi
!= FFI_LINUX
&& cif
->abi
!= FFI_LINUX_SOFT_FLOAT
)
1033 rvalue
= (void *) *pgr
;
1040 arg_types
= cif
->arg_types
;
1042 /* Grab the addresses of the arguments from the stack frame. */
1045 switch (arg_types
[i
]->type
)
1047 case FFI_TYPE_SINT8
:
1048 case FFI_TYPE_UINT8
:
1049 /* there are 8 gpr registers used to pass values */
1052 avalue
[i
] = (char *) pgr
+ 3;
1058 avalue
[i
] = (char *) pst
+ 3;
1063 case FFI_TYPE_SINT16
:
1064 case FFI_TYPE_UINT16
:
1065 /* there are 8 gpr registers used to pass values */
1068 avalue
[i
] = (char *) pgr
+ 2;
1074 avalue
[i
] = (char *) pst
+ 2;
1079 case FFI_TYPE_SINT32
:
1080 case FFI_TYPE_UINT32
:
1081 case FFI_TYPE_POINTER
:
1083 /* there are 8 gpr registers used to pass values */
1097 case FFI_TYPE_STRUCT
:
1098 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1101 /* Structs are passed by reference. The address will appear in a
1102 gpr if it is one of the first 8 arguments. */
1105 avalue
[i
] = (void *) *pgr
;
1111 avalue
[i
] = (void *) *pst
;
1116 case FFI_TYPE_SINT64
:
1117 case FFI_TYPE_UINT64
:
1118 soft_double_closure
:
1119 /* passing long long ints are complex, they must
1120 * be passed in suitable register pairs such as
1121 * (r3,r4) or (r5,r6) or (r6,r7), or (r7,r8) or (r9,r10)
1122 * and if the entire pair aren't available then the outgoing
1123 * parameter stack is used for both but an alignment of 8
1124 * must will be kept. So we must either look in pgr
1125 * or pst to find the correct address for this type
1132 /* skip r4, r6, r8 as starting points */
1142 if (((long) pst
) & 4)
1149 case FFI_TYPE_FLOAT
:
1150 /* With FFI_LINUX_SOFT_FLOAT floats are handled like UINT32. */
1151 if (cif
->abi
== FFI_LINUX_SOFT_FLOAT
)
1152 goto soft_float_closure
;
1153 /* unfortunately float values are stored as doubles
1154 * in the ffi_closure_SYSV code (since we don't check
1155 * the type in that routine).
1158 /* there are 8 64bit floating point registers */
1163 pfr
->f
= (float) temp
;
1170 /* FIXME? here we are really changing the values
1171 * stored in the original calling routines outgoing
1172 * parameter stack. This is probably a really
1173 * naughty thing to do but...
1180 case FFI_TYPE_DOUBLE
:
1181 /* With FFI_LINUX_SOFT_FLOAT doubles are handled like UINT64. */
1182 if (cif
->abi
== FFI_LINUX_SOFT_FLOAT
)
1183 goto soft_double_closure
;
1184 /* On the outgoing stack all values are aligned to 8 */
1185 /* there are 8 64bit floating point registers */
1195 if (((long) pst
) & 4)
1202 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1203 case FFI_TYPE_LONGDOUBLE
:
1204 if (cif
->abi
!= FFI_LINUX
&& cif
->abi
!= FFI_LINUX_SOFT_FLOAT
)
1206 if (cif
->abi
== FFI_LINUX_SOFT_FLOAT
)
1207 { /* Test if for the whole long double, 4 gprs are available.
1208 otherwise the stuff ends up on the stack. */
1230 if (((long) pst
) & 4)
1247 (closure
->fun
) (cif
, rvalue
, avalue
, closure
->user_data
);
1249 /* Tell ffi_closure_SYSV how to perform return type promotions.
1250 Because the FFI_SYSV ABI returns the structures <= 8 bytes in r3/r4
1251 we have to tell ffi_closure_SYSV how to treat them. */
1252 if (cif
->abi
== FFI_SYSV
&& cif
->rtype
->type
== FFI_TYPE_STRUCT
1254 return FFI_SYSV_TYPE_SMALL_STRUCT
+ size
;
1255 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1256 else if (cif
->rtype
->type
== FFI_TYPE_LONGDOUBLE
1257 && cif
->abi
!= FFI_LINUX
&& cif
->abi
!= FFI_LINUX_SOFT_FLOAT
)
1258 return FFI_TYPE_STRUCT
;
1260 /* With FFI_LINUX_SOFT_FLOAT floats and doubles are handled like UINT32
1261 respectivley UINT64. */
1262 if (cif
->abi
== FFI_LINUX_SOFT_FLOAT
)
1264 switch (cif
->rtype
->type
)
1266 case FFI_TYPE_FLOAT
:
1267 return FFI_TYPE_UINT32
;
1269 case FFI_TYPE_DOUBLE
:
1270 return FFI_TYPE_UINT64
;
1272 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1273 case FFI_TYPE_LONGDOUBLE
:
1274 return FFI_TYPE_UINT128
;
1278 return cif
->rtype
->type
;
1283 return cif
->rtype
->type
;
1287 int FFI_HIDDEN
ffi_closure_helper_LINUX64 (ffi_closure
*, void *,
1288 unsigned long *, ffi_dblfl
*);
1291 ffi_closure_helper_LINUX64 (ffi_closure
*closure
, void *rvalue
,
1292 unsigned long *pst
, ffi_dblfl
*pfr
)
1294 /* rvalue is the pointer to space for return value in closure assembly */
1295 /* pst is the pointer to parameter save area
1296 (r3-r10 are stored into its first 8 slots by ffi_closure_LINUX64) */
1297 /* pfr is the pointer to where f1-f13 are stored in ffi_closure_LINUX64 */
1300 ffi_type
**arg_types
;
1303 ffi_dblfl
*end_pfr
= pfr
+ NUM_FPR_ARG_REGISTERS64
;
1306 avalue
= alloca (cif
->nargs
* sizeof (void *));
1308 /* Copy the caller's structure return value address so that the closure
1309 returns the data directly to the caller. */
1310 if (cif
->rtype
->type
== FFI_TYPE_STRUCT
)
1312 rvalue
= (void *) *pst
;
1318 arg_types
= cif
->arg_types
;
1320 /* Grab the addresses of the arguments from the stack frame. */
1323 switch (arg_types
[i
]->type
)
1325 case FFI_TYPE_SINT8
:
1326 case FFI_TYPE_UINT8
:
1327 avalue
[i
] = (char *) pst
+ 7;
1331 case FFI_TYPE_SINT16
:
1332 case FFI_TYPE_UINT16
:
1333 avalue
[i
] = (char *) pst
+ 6;
1337 case FFI_TYPE_SINT32
:
1338 case FFI_TYPE_UINT32
:
1339 avalue
[i
] = (char *) pst
+ 4;
1343 case FFI_TYPE_SINT64
:
1344 case FFI_TYPE_UINT64
:
1345 case FFI_TYPE_POINTER
:
1350 case FFI_TYPE_STRUCT
:
1351 /* Structures with size less than eight bytes are passed
1353 if (arg_types
[i
]->size
< 8)
1354 avalue
[i
] = (char *) pst
+ 8 - arg_types
[i
]->size
;
1357 pst
+= (arg_types
[i
]->size
+ 7) / 8;
1360 case FFI_TYPE_FLOAT
:
1361 /* unfortunately float values are stored as doubles
1362 * in the ffi_closure_LINUX64 code (since we don't check
1363 * the type in that routine).
1366 /* there are 13 64bit floating point registers */
1370 double temp
= pfr
->d
;
1371 pfr
->f
= (float) temp
;
1380 case FFI_TYPE_DOUBLE
:
1381 /* On the outgoing stack all values are aligned to 8 */
1382 /* there are 13 64bit floating point registers */
1394 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1395 case FFI_TYPE_LONGDOUBLE
:
1396 if (pfr
+ 1 < end_pfr
)
1405 /* Passed partly in f13 and partly on the stack.
1406 Move it all to the stack. */
1407 *pst
= *(unsigned long *) pfr
;
1424 (closure
->fun
) (cif
, rvalue
, avalue
, closure
->user_data
);
1426 /* Tell ffi_closure_LINUX64 how to perform return type promotions. */
1427 return cif
->rtype
->type
;