1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1998 Geoffrey Keating
3 Copyright (C) 2007, 2008 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_SYSV_SMST_R4 = 1 << (31-16), /* cr4, use r4 for FFI_SYSV 8 byte
48 FLAG_SYSV_SMST_R3 = 1 << (31-15), /* cr3, use r3 for FFI_SYSV 4 byte
50 FLAG_ARG_NEEDS_COPY = 1 << (31- 7),
51 FLAG_FP_ARGUMENTS = 1 << (31- 6), /* cr1.eq; specified by ABI */
52 FLAG_4_GPR_ARGUMENTS = 1 << (31- 5),
53 FLAG_RETVAL_REFERENCE = 1 << (31- 4)
56 /* About the SYSV ABI. */
57 unsigned int NUM_GPR_ARG_REGISTERS = 8;
59 unsigned int NUM_FPR_ARG_REGISTERS = 8;
61 unsigned int NUM_FPR_ARG_REGISTERS = 0;
64 enum { ASM_NEEDS_REGISTERS = 4 };
66 /* ffi_prep_args_SYSV is called by the assembly routine once stack space
67 has been allocated for the function's arguments.
69 The stack layout we want looks like this:
71 | Return address from ffi_call_SYSV 4bytes | higher addresses
72 |--------------------------------------------|
73 | Previous backchain pointer 4 | stack pointer here
74 |--------------------------------------------|<+ <<< on entry to
75 | Saved r28-r31 4*4 | | ffi_call_SYSV
76 |--------------------------------------------| |
77 | GPR registers r3-r10 8*4 | | ffi_call_SYSV
78 |--------------------------------------------| |
79 | FPR registers f1-f8 (optional) 8*8 | |
80 |--------------------------------------------| | stack |
81 | Space for copied structures | | grows |
82 |--------------------------------------------| | down V
83 | Parameters that didn't fit in registers | |
84 |--------------------------------------------| | lower addresses
85 | Space for callee's LR 4 | |
86 |--------------------------------------------| | stack pointer here
87 | Current backchain pointer 4 |-/ during
88 |--------------------------------------------| <<< ffi_call_SYSV
93 ffi_prep_args_SYSV (extended_cif *ecif, unsigned *const stack)
95 const unsigned bytes = ecif->cif->bytes;
96 const unsigned flags = ecif->cif->flags;
106 /* 'stacktop' points at the previous backchain pointer. */
109 /* 'gpr_base' points at the space for gpr3, and grows upwards as
110 we use GPR registers. */
114 /* 'fpr_base' points at the space for fpr1, and grows upwards as
115 we use FPR registers. */
119 /* 'copy_space' grows down as we put structures in it. It should
120 stay 16-byte aligned. */
123 /* 'next_arg' grows up as we put parameters in it. */
126 int i, ii MAYBE_UNUSED;
141 size_t struct_copy_size;
144 if (ecif->cif->abi == FFI_LINUX_SOFT_FLOAT)
145 NUM_FPR_ARG_REGISTERS = 0;
147 stacktop.c = (char *) stack + bytes;
148 gpr_base.u = stacktop.u - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS;
150 fpr_base.d = gpr_base.d - NUM_FPR_ARG_REGISTERS;
152 copy_space.c = ((flags & FLAG_FP_ARGUMENTS) ? fpr_base.c : gpr_base.c);
153 next_arg.u = stack + 2;
155 /* Check that everything starts aligned properly. */
156 FFI_ASSERT (((unsigned) (char *) stack & 0xF) == 0);
157 FFI_ASSERT (((unsigned) copy_space.c & 0xF) == 0);
158 FFI_ASSERT (((unsigned) stacktop.c & 0xF) == 0);
159 FFI_ASSERT ((bytes & 0xF) == 0);
160 FFI_ASSERT (copy_space.c >= next_arg.c);
162 /* Deal with return values that are actually pass-by-reference. */
163 if (flags & FLAG_RETVAL_REFERENCE)
165 *gpr_base.u++ = (unsigned long) (char *) ecif->rvalue;
169 /* Now for the arguments. */
170 p_argv.v = ecif->avalue;
171 for (ptr = ecif->cif->arg_types, i = ecif->cif->nargs;
173 i--, ptr++, p_argv.v++)
175 switch ((*ptr)->type)
178 /* With FFI_LINUX_SOFT_FLOAT floats are handled like UINT32. */
179 if (ecif->cif->abi == FFI_LINUX_SOFT_FLOAT)
180 goto soft_float_prep;
181 double_tmp = **p_argv.f;
182 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
184 *next_arg.f = (float) double_tmp;
188 *fpr_base.d++ = double_tmp;
190 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
193 case FFI_TYPE_DOUBLE:
194 /* With FFI_LINUX_SOFT_FLOAT doubles are handled like UINT64. */
195 if (ecif->cif->abi == FFI_LINUX_SOFT_FLOAT)
196 goto soft_double_prep;
197 double_tmp = **p_argv.d;
199 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
201 if (intarg_count >= NUM_GPR_ARG_REGISTERS
202 && intarg_count % 2 != 0)
207 *next_arg.d = double_tmp;
211 *fpr_base.d++ = double_tmp;
213 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
216 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
217 case FFI_TYPE_LONGDOUBLE:
218 if ((ecif->cif->abi != FFI_LINUX)
219 && (ecif->cif->abi != FFI_LINUX_SOFT_FLOAT))
221 /* The soft float ABI for long doubles works like this,
222 a long double is passed in four consecutive gprs if available.
223 A maximum of 2 long doubles can be passed in gprs.
224 If we do not have 4 gprs left, the long double is passed on the
225 stack, 4-byte aligned. */
226 if (ecif->cif->abi == FFI_LINUX_SOFT_FLOAT)
228 unsigned int int_tmp = (*p_argv.ui)[0];
229 if (intarg_count >= NUM_GPR_ARG_REGISTERS - 3)
231 if (intarg_count < NUM_GPR_ARG_REGISTERS)
232 intarg_count += NUM_GPR_ARG_REGISTERS - intarg_count;
233 *next_arg.u = int_tmp;
235 for (ii = 1; ii < 4; ii++)
237 int_tmp = (*p_argv.ui)[ii];
238 *next_arg.u = int_tmp;
244 *gpr_base.u++ = int_tmp;
245 for (ii = 1; ii < 4; ii++)
247 int_tmp = (*p_argv.ui)[ii];
248 *gpr_base.u++ = int_tmp;
255 double_tmp = (*p_argv.d)[0];
257 if (fparg_count >= NUM_FPR_ARG_REGISTERS - 1)
259 if (intarg_count >= NUM_GPR_ARG_REGISTERS
260 && intarg_count % 2 != 0)
265 *next_arg.d = double_tmp;
267 double_tmp = (*p_argv.d)[1];
268 *next_arg.d = double_tmp;
273 *fpr_base.d++ = double_tmp;
274 double_tmp = (*p_argv.d)[1];
275 *fpr_base.d++ = double_tmp;
279 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
284 case FFI_TYPE_UINT64:
285 case FFI_TYPE_SINT64:
287 if (intarg_count == NUM_GPR_ARG_REGISTERS-1)
289 if (intarg_count >= NUM_GPR_ARG_REGISTERS)
291 if (intarg_count % 2 != 0)
296 *next_arg.ll = **p_argv.ll;
301 /* whoops: abi states only certain register pairs
302 * can be used for passing long long int
303 * specifically (r3,r4), (r5,r6), (r7,r8),
304 * (r9,r10) and if next arg is long long but
305 * not correct starting register of pair then skip
306 * until the proper starting register
308 if (intarg_count % 2 != 0)
313 *gpr_base.ll++ = **p_argv.ll;
318 case FFI_TYPE_STRUCT:
319 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
322 struct_copy_size = ((*ptr)->size + 15) & ~0xF;
323 copy_space.c -= struct_copy_size;
324 memcpy (copy_space.c, *p_argv.c, (*ptr)->size);
326 gprvalue = (unsigned long) copy_space.c;
328 FFI_ASSERT (copy_space.c > next_arg.c);
329 FFI_ASSERT (flags & FLAG_ARG_NEEDS_COPY);
333 gprvalue = **p_argv.uc;
336 gprvalue = **p_argv.sc;
338 case FFI_TYPE_UINT16:
339 gprvalue = **p_argv.us;
341 case FFI_TYPE_SINT16:
342 gprvalue = **p_argv.ss;
346 case FFI_TYPE_UINT32:
347 case FFI_TYPE_SINT32:
348 case FFI_TYPE_POINTER:
351 gprvalue = **p_argv.ui;
354 if (intarg_count >= NUM_GPR_ARG_REGISTERS)
355 *next_arg.u++ = gprvalue;
357 *gpr_base.u++ = gprvalue;
363 /* Check that we didn't overrun the stack... */
364 FFI_ASSERT (copy_space.c >= next_arg.c);
365 FFI_ASSERT (gpr_base.u <= stacktop.u - ASM_NEEDS_REGISTERS);
366 FFI_ASSERT (fpr_base.u
367 <= stacktop.u - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
368 FFI_ASSERT (flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
371 /* About the LINUX64 ABI. */
373 NUM_GPR_ARG_REGISTERS64 = 8,
374 NUM_FPR_ARG_REGISTERS64 = 13
376 enum { ASM_NEEDS_REGISTERS64 = 4 };
378 /* ffi_prep_args64 is called by the assembly routine once stack space
379 has been allocated for the function's arguments.
381 The stack layout we want looks like this:
383 | Ret addr from ffi_call_LINUX64 8bytes | higher addresses
384 |--------------------------------------------|
385 | CR save area 8bytes |
386 |--------------------------------------------|
387 | Previous backchain pointer 8 | stack pointer here
388 |--------------------------------------------|<+ <<< on entry to
389 | Saved r28-r31 4*8 | | ffi_call_LINUX64
390 |--------------------------------------------| |
391 | GPR registers r3-r10 8*8 | |
392 |--------------------------------------------| |
393 | FPR registers f1-f13 (optional) 13*8 | |
394 |--------------------------------------------| |
395 | Parameter save area | |
396 |--------------------------------------------| |
397 | TOC save area 8 | |
398 |--------------------------------------------| | stack |
399 | Linker doubleword 8 | | grows |
400 |--------------------------------------------| | down V
401 | Compiler doubleword 8 | |
402 |--------------------------------------------| | lower addresses
403 | Space for callee's LR 8 | |
404 |--------------------------------------------| |
406 |--------------------------------------------| | stack pointer here
407 | Current backchain pointer 8 |-/ during
408 |--------------------------------------------| <<< ffi_call_LINUX64
413 ffi_prep_args64 (extended_cif *ecif, unsigned long *const stack)
415 const unsigned long bytes = ecif->cif->bytes;
416 const unsigned long flags = ecif->cif->flags;
425 /* 'stacktop' points at the previous backchain pointer. */
428 /* 'next_arg' points at the space for gpr3, and grows upwards as
429 we use GPR registers, then continues at rest. */
435 /* 'fpr_base' points at the space for fpr3, and grows upwards as
436 we use FPR registers. */
456 unsigned long gprvalue;
458 stacktop.c = (char *) stack + bytes;
459 gpr_base.ul = stacktop.ul - ASM_NEEDS_REGISTERS64 - NUM_GPR_ARG_REGISTERS64;
460 gpr_end.ul = gpr_base.ul + NUM_GPR_ARG_REGISTERS64;
461 rest.ul = stack + 6 + NUM_GPR_ARG_REGISTERS64;
462 fpr_base.d = gpr_base.d - NUM_FPR_ARG_REGISTERS64;
464 next_arg.ul = gpr_base.ul;
466 /* Check that everything starts aligned properly. */
467 FFI_ASSERT (((unsigned long) (char *) stack & 0xF) == 0);
468 FFI_ASSERT (((unsigned long) stacktop.c & 0xF) == 0);
469 FFI_ASSERT ((bytes & 0xF) == 0);
471 /* Deal with return values that are actually pass-by-reference. */
472 if (flags & FLAG_RETVAL_REFERENCE)
473 *next_arg.ul++ = (unsigned long) (char *) ecif->rvalue;
475 /* Now for the arguments. */
476 p_argv.v = ecif->avalue;
477 for (ptr = ecif->cif->arg_types, i = ecif->cif->nargs;
479 i--, ptr++, p_argv.v++)
481 switch ((*ptr)->type)
484 double_tmp = **p_argv.f;
485 *next_arg.f = (float) double_tmp;
486 if (++next_arg.ul == gpr_end.ul)
487 next_arg.ul = rest.ul;
488 if (fparg_count < NUM_FPR_ARG_REGISTERS64)
489 *fpr_base.d++ = double_tmp;
491 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
494 case FFI_TYPE_DOUBLE:
495 double_tmp = **p_argv.d;
496 *next_arg.d = double_tmp;
497 if (++next_arg.ul == gpr_end.ul)
498 next_arg.ul = rest.ul;
499 if (fparg_count < NUM_FPR_ARG_REGISTERS64)
500 *fpr_base.d++ = double_tmp;
502 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
505 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
506 case FFI_TYPE_LONGDOUBLE:
507 double_tmp = (*p_argv.d)[0];
508 *next_arg.d = double_tmp;
509 if (++next_arg.ul == gpr_end.ul)
510 next_arg.ul = rest.ul;
511 if (fparg_count < NUM_FPR_ARG_REGISTERS64)
512 *fpr_base.d++ = double_tmp;
514 double_tmp = (*p_argv.d)[1];
515 *next_arg.d = double_tmp;
516 if (++next_arg.ul == gpr_end.ul)
517 next_arg.ul = rest.ul;
518 if (fparg_count < NUM_FPR_ARG_REGISTERS64)
519 *fpr_base.d++ = double_tmp;
521 FFI_ASSERT (__LDBL_MANT_DIG__ == 106);
522 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
526 case FFI_TYPE_STRUCT:
527 words = ((*ptr)->size + 7) / 8;
528 if (next_arg.ul >= gpr_base.ul && next_arg.ul + words > gpr_end.ul)
530 size_t first = gpr_end.c - next_arg.c;
531 memcpy (next_arg.c, *p_argv.c, first);
532 memcpy (rest.c, *p_argv.c + first, (*ptr)->size - first);
533 next_arg.c = rest.c + words * 8 - first;
537 char *where = next_arg.c;
539 /* Structures with size less than eight bytes are passed
541 if ((*ptr)->size < 8)
542 where += 8 - (*ptr)->size;
544 memcpy (where, *p_argv.c, (*ptr)->size);
545 next_arg.ul += words;
546 if (next_arg.ul == gpr_end.ul)
547 next_arg.ul = rest.ul;
552 gprvalue = **p_argv.uc;
555 gprvalue = **p_argv.sc;
557 case FFI_TYPE_UINT16:
558 gprvalue = **p_argv.us;
560 case FFI_TYPE_SINT16:
561 gprvalue = **p_argv.ss;
563 case FFI_TYPE_UINT32:
564 gprvalue = **p_argv.ui;
567 case FFI_TYPE_SINT32:
568 gprvalue = **p_argv.si;
571 case FFI_TYPE_UINT64:
572 case FFI_TYPE_SINT64:
573 case FFI_TYPE_POINTER:
574 gprvalue = **p_argv.ul;
576 *next_arg.ul++ = gprvalue;
577 if (next_arg.ul == gpr_end.ul)
578 next_arg.ul = rest.ul;
583 FFI_ASSERT (flags & FLAG_4_GPR_ARGUMENTS
584 || (next_arg.ul >= gpr_base.ul
585 && next_arg.ul <= gpr_base.ul + 4));
590 /* Perform machine dependent cif processing */
592 ffi_prep_cif_machdep (ffi_cif *cif)
594 /* All this is for the SYSV and LINUX64 ABI. */
598 int fparg_count = 0, intarg_count = 0;
600 unsigned struct_copy_size = 0;
601 unsigned type = cif->rtype->type;
602 unsigned size = cif->rtype->size;
604 if (cif->abi == FFI_LINUX_SOFT_FLOAT)
605 NUM_FPR_ARG_REGISTERS = 0;
607 if (cif->abi != FFI_LINUX64)
609 /* All the machine-independent calculation of cif->bytes will be wrong.
610 Redo the calculation for SYSV. */
612 /* Space for the frame pointer, callee's LR, and the asm's temp regs. */
613 bytes = (2 + ASM_NEEDS_REGISTERS) * sizeof (int);
615 /* Space for the GPR registers. */
616 bytes += NUM_GPR_ARG_REGISTERS * sizeof (int);
622 /* Space for backchain, CR, LR, cc/ld doubleword, TOC and the asm's temp
624 bytes = (6 + ASM_NEEDS_REGISTERS64) * sizeof (long);
626 /* Space for the mandatory parm save area and general registers. */
627 bytes += 2 * NUM_GPR_ARG_REGISTERS64 * sizeof (long);
630 /* Return value handling. The rules for SYSV are as follows:
631 - 32-bit (or less) integer values are returned in gpr3;
632 - Structures of size <= 4 bytes also returned in gpr3;
633 - 64-bit integer values and structures between 5 and 8 bytes are returned
635 - Single/double FP values are returned in fpr1;
636 - Larger structures are allocated space and a pointer is passed as
638 - long doubles (if not equivalent to double) are returned in
639 fpr1,fpr2 for Linux and as for large structs for SysV.
641 - integer values in gpr3;
642 - Structures/Unions by reference;
643 - Single/double FP values in fpr1, long double in fpr1,fpr2.
644 - soft-float float/doubles are treated as UINT32/UINT64 respectivley.
645 - soft-float long doubles are returned in gpr3-gpr6. */
648 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
649 case FFI_TYPE_LONGDOUBLE:
650 if (cif->abi != FFI_LINUX && cif->abi != FFI_LINUX64
651 && cif->abi != FFI_LINUX_SOFT_FLOAT)
653 flags |= FLAG_RETURNS_128BITS;
656 case FFI_TYPE_DOUBLE:
657 flags |= FLAG_RETURNS_64BITS;
660 /* With FFI_LINUX_SOFT_FLOAT no fp registers are used. */
661 if (cif->abi != FFI_LINUX_SOFT_FLOAT)
662 flags |= FLAG_RETURNS_FP;
665 case FFI_TYPE_UINT64:
666 case FFI_TYPE_SINT64:
667 flags |= FLAG_RETURNS_64BITS;
670 case FFI_TYPE_STRUCT:
671 if (cif->abi == FFI_SYSV)
673 /* The final SYSV ABI says that structures smaller or equal 8 bytes
674 are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them
677 /* Treat structs with size <= 8 bytes. */
680 flags |= FLAG_RETURNS_SMST;
681 /* These structs are returned in r3. We pack the type and the
682 precalculated shift value (needed in the sysv.S) into flags.
683 The same applies for the structs returned in r3/r4. */
686 flags |= FLAG_SYSV_SMST_R3;
687 flags |= 8 * (4 - size) << 4;
690 /* These structs are returned in r3 and r4. See above. */
693 flags |= FLAG_SYSV_SMST_R4;
694 flags |= 8 * (8 - size) << 4;
699 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
703 flags |= FLAG_RETVAL_REFERENCE;
706 flags |= FLAG_RETURNS_NOTHING;
710 /* Returns 32-bit integer, or similar. Nothing to do here. */
714 if (cif->abi != FFI_LINUX64)
715 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
716 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
717 goes on the stack. Structures and long doubles (if not equivalent
718 to double) are passed as a pointer to a copy of the structure.
719 Stuff on the stack needs to keep proper alignment. */
720 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
722 switch ((*ptr)->type)
725 /* With FFI_LINUX_SOFT_FLOAT floats are handled like UINT32. */
726 if (cif->abi == FFI_LINUX_SOFT_FLOAT)
729 /* floating singles are not 8-aligned on stack */
732 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
733 case FFI_TYPE_LONGDOUBLE:
734 if (cif->abi != FFI_LINUX && cif->abi != FFI_LINUX_SOFT_FLOAT)
736 if (cif->abi == FFI_LINUX_SOFT_FLOAT)
738 if (intarg_count >= NUM_GPR_ARG_REGISTERS - 3
739 || intarg_count < NUM_GPR_ARG_REGISTERS)
740 /* A long double in FFI_LINUX_SOFT_FLOAT can use only
741 a set of four consecutive gprs. If we have not enough,
742 we have to adjust the intarg_count value. */
743 intarg_count += NUM_GPR_ARG_REGISTERS - intarg_count;
751 case FFI_TYPE_DOUBLE:
752 /* With FFI_LINUX_SOFT_FLOAT doubles are handled like UINT64. */
753 if (cif->abi == FFI_LINUX_SOFT_FLOAT)
754 goto soft_double_cif;
756 /* If this FP arg is going on the stack, it must be
758 if (fparg_count > NUM_FPR_ARG_REGISTERS
759 && intarg_count >= NUM_GPR_ARG_REGISTERS
760 && intarg_count % 2 != 0)
764 case FFI_TYPE_UINT64:
765 case FFI_TYPE_SINT64:
767 /* 'long long' arguments are passed as two words, but
768 either both words must fit in registers or both go
769 on the stack. If they go on the stack, they must
772 Also, only certain register pairs can be used for
773 passing long long int -- specifically (r3,r4), (r5,r6),
776 if (intarg_count == NUM_GPR_ARG_REGISTERS-1
777 || intarg_count % 2 != 0)
782 case FFI_TYPE_STRUCT:
783 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
786 /* We must allocate space for a copy of these to enforce
787 pass-by-value. Pad the space up to a multiple of 16
788 bytes (the maximum alignment required for anything under
790 struct_copy_size += ((*ptr)->size + 15) & ~0xF;
791 /* Fall through (allocate space for the pointer). */
795 /* Everything else is passed as a 4-byte word in a GPR, either
796 the object itself or a pointer to it. */
802 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
804 switch ((*ptr)->type)
806 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
807 case FFI_TYPE_LONGDOUBLE:
808 if (cif->abi == FFI_LINUX_SOFT_FLOAT)
818 case FFI_TYPE_DOUBLE:
823 case FFI_TYPE_STRUCT:
824 intarg_count += ((*ptr)->size + 7) / 8;
828 /* Everything else is passed as a 8-byte word in a GPR, either
829 the object itself or a pointer to it. */
835 if (fparg_count != 0)
836 flags |= FLAG_FP_ARGUMENTS;
837 if (intarg_count > 4)
838 flags |= FLAG_4_GPR_ARGUMENTS;
839 if (struct_copy_size != 0)
840 flags |= FLAG_ARG_NEEDS_COPY;
842 if (cif->abi != FFI_LINUX64)
844 /* Space for the FPR registers, if needed. */
845 if (fparg_count != 0)
846 bytes += NUM_FPR_ARG_REGISTERS * sizeof (double);
849 if (intarg_count > NUM_GPR_ARG_REGISTERS)
850 bytes += (intarg_count - NUM_GPR_ARG_REGISTERS) * sizeof (int);
851 if (fparg_count > NUM_FPR_ARG_REGISTERS)
852 bytes += (fparg_count - NUM_FPR_ARG_REGISTERS) * sizeof (double);
856 /* Space for the FPR registers, if needed. */
857 if (fparg_count != 0)
858 bytes += NUM_FPR_ARG_REGISTERS64 * sizeof (double);
861 if (intarg_count > NUM_GPR_ARG_REGISTERS64)
862 bytes += (intarg_count - NUM_GPR_ARG_REGISTERS64) * sizeof (long);
865 /* The stack space allocated needs to be a multiple of 16 bytes. */
866 bytes = (bytes + 15) & ~0xF;
868 /* Add in the space for the copied structures. */
869 bytes += struct_copy_size;
877 extern void ffi_call_SYSV(extended_cif *, unsigned, unsigned, unsigned *,
879 extern void FFI_HIDDEN ffi_call_LINUX64(extended_cif *, unsigned long,
880 unsigned long, unsigned long *,
884 ffi_call(ffi_cif *cif, void (*fn)(), void *rvalue, void **avalue)
889 ecif.avalue = avalue;
891 /* If the return value is a struct and we don't have a return */
892 /* value address then we need to make one */
894 if ((rvalue == NULL) && (cif->rtype->type == FFI_TYPE_STRUCT))
896 ecif.rvalue = alloca(cif->rtype->size);
899 ecif.rvalue = rvalue;
908 case FFI_LINUX_SOFT_FLOAT:
909 ffi_call_SYSV (&ecif, -cif->bytes, cif->flags, ecif.rvalue, fn);
913 ffi_call_LINUX64 (&ecif, -(long) cif->bytes, cif->flags, ecif.rvalue, fn);
924 #define MIN_CACHE_LINE_SIZE 8
927 flush_icache (char *wraddr, char *xaddr, int size)
930 for (i = 0; i < size; i += MIN_CACHE_LINE_SIZE)
931 __asm__ volatile ("icbi 0,%0;" "dcbf 0,%1;"
932 : : "r" (xaddr + i), "r" (wraddr + i) : "memory");
933 __asm__ volatile ("icbi 0,%0;" "dcbf 0,%1;" "sync;" "isync;"
934 : : "r"(xaddr + size - 1), "r"(wraddr + size - 1)
940 ffi_prep_closure_loc (ffi_closure *closure,
942 void (*fun) (ffi_cif *, void *, void **, void *),
947 void **tramp = (void **) &closure->tramp[0];
949 FFI_ASSERT (cif->abi == FFI_LINUX64);
950 /* Copy function address and TOC from ffi_closure_LINUX64. */
951 memcpy (tramp, (char *) ffi_closure_LINUX64, 16);
956 FFI_ASSERT (cif->abi == FFI_GCC_SYSV || cif->abi == FFI_SYSV);
958 tramp = (unsigned int *) &closure->tramp[0];
959 tramp[0] = 0x7c0802a6; /* mflr r0 */
960 tramp[1] = 0x4800000d; /* bl 10 <trampoline_initial+0x10> */
961 tramp[4] = 0x7d6802a6; /* mflr r11 */
962 tramp[5] = 0x7c0803a6; /* mtlr r0 */
963 tramp[6] = 0x800b0000; /* lwz r0,0(r11) */
964 tramp[7] = 0x816b0004; /* lwz r11,4(r11) */
965 tramp[8] = 0x7c0903a6; /* mtctr r0 */
966 tramp[9] = 0x4e800420; /* bctr */
967 *(void **) &tramp[2] = (void *) ffi_closure_SYSV; /* function */
968 *(void **) &tramp[3] = codeloc; /* context */
970 /* Flush the icache. */
971 flush_icache ((char *)tramp, (char *)codeloc, FFI_TRAMPOLINE_SIZE);
976 closure->user_data = user_data;
987 int ffi_closure_helper_SYSV (ffi_closure *, void *, unsigned long *,
988 ffi_dblfl *, unsigned long *);
990 /* Basically the trampoline invokes ffi_closure_SYSV, and on
991 * entry, r11 holds the address of the closure.
992 * After storing the registers that could possibly contain
993 * parameters to be passed into the stack frame and setting
994 * up space for a return value, ffi_closure_SYSV invokes the
995 * following helper function to do most of the work
999 ffi_closure_helper_SYSV (ffi_closure *closure, void *rvalue,
1000 unsigned long *pgr, ffi_dblfl *pfr,
1003 /* rvalue is the pointer to space for return value in closure assembly */
1004 /* pgr is the pointer to where r3-r10 are stored in ffi_closure_SYSV */
1005 /* pfr is the pointer to where f1-f8 are stored in ffi_closure_SYSV */
1006 /* pst is the pointer to outgoing parameter stack in original caller */
1009 ffi_type ** arg_types;
1011 long nf; /* number of floating registers already used */
1012 long ng; /* number of general registers already used */
1018 avalue = alloca (cif->nargs * sizeof (void *));
1019 size = cif->rtype->size;
1024 /* Copy the caller's structure return value address so that the closure
1025 returns the data directly to the caller.
1026 For FFI_SYSV the result is passed in r3/r4 if the struct size is less
1027 or equal 8 bytes. */
1029 if ((cif->rtype->type == FFI_TYPE_STRUCT
1030 && !((cif->abi == FFI_SYSV) && (size <= 8)))
1031 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1032 || (cif->rtype->type == FFI_TYPE_LONGDOUBLE
1033 && cif->abi != FFI_LINUX && cif->abi != FFI_LINUX_SOFT_FLOAT)
1037 rvalue = (void *) *pgr;
1044 arg_types = cif->arg_types;
1046 /* Grab the addresses of the arguments from the stack frame. */
1049 switch (arg_types[i]->type)
1051 case FFI_TYPE_SINT8:
1052 case FFI_TYPE_UINT8:
1053 /* there are 8 gpr registers used to pass values */
1056 avalue[i] = (char *) pgr + 3;
1062 avalue[i] = (char *) pst + 3;
1067 case FFI_TYPE_SINT16:
1068 case FFI_TYPE_UINT16:
1069 /* there are 8 gpr registers used to pass values */
1072 avalue[i] = (char *) pgr + 2;
1078 avalue[i] = (char *) pst + 2;
1083 case FFI_TYPE_SINT32:
1084 case FFI_TYPE_UINT32:
1085 case FFI_TYPE_POINTER:
1087 /* there are 8 gpr registers used to pass values */
1101 case FFI_TYPE_STRUCT:
1102 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1105 /* Structs are passed by reference. The address will appear in a
1106 gpr if it is one of the first 8 arguments. */
1109 avalue[i] = (void *) *pgr;
1115 avalue[i] = (void *) *pst;
1120 case FFI_TYPE_SINT64:
1121 case FFI_TYPE_UINT64:
1122 soft_double_closure:
1123 /* passing long long ints are complex, they must
1124 * be passed in suitable register pairs such as
1125 * (r3,r4) or (r5,r6) or (r6,r7), or (r7,r8) or (r9,r10)
1126 * and if the entire pair aren't available then the outgoing
1127 * parameter stack is used for both but an alignment of 8
1128 * must will be kept. So we must either look in pgr
1129 * or pst to find the correct address for this type
1136 /* skip r4, r6, r8 as starting points */
1146 if (((long) pst) & 4)
1153 case FFI_TYPE_FLOAT:
1154 /* With FFI_LINUX_SOFT_FLOAT floats are handled like UINT32. */
1155 if (cif->abi == FFI_LINUX_SOFT_FLOAT)
1156 goto soft_float_closure;
1157 /* unfortunately float values are stored as doubles
1158 * in the ffi_closure_SYSV code (since we don't check
1159 * the type in that routine).
1162 /* there are 8 64bit floating point registers */
1167 pfr->f = (float) temp;
1174 /* FIXME? here we are really changing the values
1175 * stored in the original calling routines outgoing
1176 * parameter stack. This is probably a really
1177 * naughty thing to do but...
1184 case FFI_TYPE_DOUBLE:
1185 /* With FFI_LINUX_SOFT_FLOAT doubles are handled like UINT64. */
1186 if (cif->abi == FFI_LINUX_SOFT_FLOAT)
1187 goto soft_double_closure;
1188 /* On the outgoing stack all values are aligned to 8 */
1189 /* there are 8 64bit floating point registers */
1199 if (((long) pst) & 4)
1206 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1207 case FFI_TYPE_LONGDOUBLE:
1208 if (cif->abi != FFI_LINUX && cif->abi != FFI_LINUX_SOFT_FLOAT)
1210 if (cif->abi == FFI_LINUX_SOFT_FLOAT)
1211 { /* Test if for the whole long double, 4 gprs are available.
1212 otherwise the stuff ends up on the stack. */
1234 if (((long) pst) & 4)
1251 (closure->fun) (cif, rvalue, avalue, closure->user_data);
1253 /* Tell ffi_closure_SYSV how to perform return type promotions.
1254 Because the FFI_SYSV ABI returns the structures <= 8 bytes in r3/r4
1255 we have to tell ffi_closure_SYSV how to treat them. We combine the base
1256 type FFI_SYSV_TYPE_SMALL_STRUCT - 1 with the size of the struct.
1257 So a one byte struct gets the return type 16. Return type 1 to 15 are
1258 already used and we never have a struct with size zero. That is the reason
1259 for the subtraction of 1. See the comment in ffitarget.h about ordering.
1261 if (cif->abi == FFI_SYSV && cif->rtype->type == FFI_TYPE_STRUCT
1263 return (FFI_SYSV_TYPE_SMALL_STRUCT - 1) + size;
1264 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1265 else if (cif->rtype->type == FFI_TYPE_LONGDOUBLE
1266 && cif->abi != FFI_LINUX && cif->abi != FFI_LINUX_SOFT_FLOAT)
1267 return FFI_TYPE_STRUCT;
1269 /* With FFI_LINUX_SOFT_FLOAT floats and doubles are handled like UINT32
1270 respectivley UINT64. */
1271 if (cif->abi == FFI_LINUX_SOFT_FLOAT)
1273 switch (cif->rtype->type)
1275 case FFI_TYPE_FLOAT:
1276 return FFI_TYPE_UINT32;
1278 case FFI_TYPE_DOUBLE:
1279 return FFI_TYPE_UINT64;
1281 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1282 case FFI_TYPE_LONGDOUBLE:
1283 return FFI_TYPE_UINT128;
1287 return cif->rtype->type;
1292 return cif->rtype->type;
1296 int FFI_HIDDEN ffi_closure_helper_LINUX64 (ffi_closure *, void *,
1297 unsigned long *, ffi_dblfl *);
1300 ffi_closure_helper_LINUX64 (ffi_closure *closure, void *rvalue,
1301 unsigned long *pst, ffi_dblfl *pfr)
1303 /* rvalue is the pointer to space for return value in closure assembly */
1304 /* pst is the pointer to parameter save area
1305 (r3-r10 are stored into its first 8 slots by ffi_closure_LINUX64) */
1306 /* pfr is the pointer to where f1-f13 are stored in ffi_closure_LINUX64 */
1309 ffi_type **arg_types;
1312 ffi_dblfl *end_pfr = pfr + NUM_FPR_ARG_REGISTERS64;
1315 avalue = alloca (cif->nargs * sizeof (void *));
1317 /* Copy the caller's structure return value address so that the closure
1318 returns the data directly to the caller. */
1319 if (cif->rtype->type == FFI_TYPE_STRUCT)
1321 rvalue = (void *) *pst;
1327 arg_types = cif->arg_types;
1329 /* Grab the addresses of the arguments from the stack frame. */
1332 switch (arg_types[i]->type)
1334 case FFI_TYPE_SINT8:
1335 case FFI_TYPE_UINT8:
1336 avalue[i] = (char *) pst + 7;
1340 case FFI_TYPE_SINT16:
1341 case FFI_TYPE_UINT16:
1342 avalue[i] = (char *) pst + 6;
1346 case FFI_TYPE_SINT32:
1347 case FFI_TYPE_UINT32:
1348 avalue[i] = (char *) pst + 4;
1352 case FFI_TYPE_SINT64:
1353 case FFI_TYPE_UINT64:
1354 case FFI_TYPE_POINTER:
1359 case FFI_TYPE_STRUCT:
1360 /* Structures with size less than eight bytes are passed
1362 if (arg_types[i]->size < 8)
1363 avalue[i] = (char *) pst + 8 - arg_types[i]->size;
1366 pst += (arg_types[i]->size + 7) / 8;
1369 case FFI_TYPE_FLOAT:
1370 /* unfortunately float values are stored as doubles
1371 * in the ffi_closure_LINUX64 code (since we don't check
1372 * the type in that routine).
1375 /* there are 13 64bit floating point registers */
1379 double temp = pfr->d;
1380 pfr->f = (float) temp;
1389 case FFI_TYPE_DOUBLE:
1390 /* On the outgoing stack all values are aligned to 8 */
1391 /* there are 13 64bit floating point registers */
1403 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1404 case FFI_TYPE_LONGDOUBLE:
1405 if (pfr + 1 < end_pfr)
1414 /* Passed partly in f13 and partly on the stack.
1415 Move it all to the stack. */
1416 *pst = *(unsigned long *) pfr;
1433 (closure->fun) (cif, rvalue, avalue, closure->user_data);
1435 /* Tell ffi_closure_LINUX64 how to perform return type promotions. */
1436 return cif->rtype->type;