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),
43 FLAG_RETURNS_128BITS = 1 << (31-27),
45 FLAG_ARG_NEEDS_COPY = 1 << (31- 7),
46 FLAG_FP_ARGUMENTS = 1 << (31- 6), /* cr1.eq; specified by ABI */
47 FLAG_4_GPR_ARGUMENTS = 1 << (31- 5),
48 FLAG_RETVAL_REFERENCE = 1 << (31- 4)
51 /* About the SYSV ABI. */
53 NUM_GPR_ARG_REGISTERS = 8,
54 NUM_FPR_ARG_REGISTERS = 8
56 enum { ASM_NEEDS_REGISTERS = 4 };
58 /* ffi_prep_args_SYSV is called by the assembly routine once stack space
59 has been allocated for the function's arguments.
61 The stack layout we want looks like this:
63 | Return address from ffi_call_SYSV 4bytes | higher addresses
64 |--------------------------------------------|
65 | Previous backchain pointer 4 | stack pointer here
66 |--------------------------------------------|<+ <<< on entry to
67 | Saved r28-r31 4*4 | | ffi_call_SYSV
68 |--------------------------------------------| |
69 | GPR registers r3-r10 8*4 | | ffi_call_SYSV
70 |--------------------------------------------| |
71 | FPR registers f1-f8 (optional) 8*8 | |
72 |--------------------------------------------| | stack |
73 | Space for copied structures | | grows |
74 |--------------------------------------------| | down V
75 | Parameters that didn't fit in registers | |
76 |--------------------------------------------| | lower addresses
77 | Space for callee's LR 4 | |
78 |--------------------------------------------| | stack pointer here
79 | Current backchain pointer 4 |-/ during
80 |--------------------------------------------| <<< ffi_call_SYSV
85 ffi_prep_args_SYSV (extended_cif *ecif, unsigned *const stack)
87 const unsigned bytes = ecif->cif->bytes;
88 const unsigned flags = ecif->cif->flags;
98 /* 'stacktop' points at the previous backchain pointer. */
101 /* 'gpr_base' points at the space for gpr3, and grows upwards as
102 we use GPR registers. */
106 /* 'fpr_base' points at the space for fpr1, and grows upwards as
107 we use FPR registers. */
111 /* 'copy_space' grows down as we put structures in it. It should
112 stay 16-byte aligned. */
115 /* 'next_arg' grows up as we put parameters in it. */
133 size_t struct_copy_size;
136 stacktop.c = (char *) stack + bytes;
137 gpr_base.u = stacktop.u - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS;
139 fpr_base.d = gpr_base.d - NUM_FPR_ARG_REGISTERS;
141 copy_space.c = ((flags & FLAG_FP_ARGUMENTS) ? fpr_base.c : gpr_base.c);
142 next_arg.u = stack + 2;
144 /* Check that everything starts aligned properly. */
145 FFI_ASSERT (((unsigned) (char *) stack & 0xF) == 0);
146 FFI_ASSERT (((unsigned) copy_space.c & 0xF) == 0);
147 FFI_ASSERT (((unsigned) stacktop.c & 0xF) == 0);
148 FFI_ASSERT ((bytes & 0xF) == 0);
149 FFI_ASSERT (copy_space.c >= next_arg.c);
151 /* Deal with return values that are actually pass-by-reference. */
152 if (flags & FLAG_RETVAL_REFERENCE)
154 *gpr_base.u++ = (unsigned long) (char *) ecif->rvalue;
158 /* Now for the arguments. */
159 p_argv.v = ecif->avalue;
160 for (ptr = ecif->cif->arg_types, i = ecif->cif->nargs;
162 i--, ptr++, p_argv.v++)
164 switch ((*ptr)->type)
167 double_tmp = **p_argv.f;
168 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
170 *next_arg.f = (float) double_tmp;
174 *fpr_base.d++ = double_tmp;
176 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
179 case FFI_TYPE_DOUBLE:
180 double_tmp = **p_argv.d;
182 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
184 if (intarg_count >= NUM_GPR_ARG_REGISTERS
185 && intarg_count % 2 != 0)
190 *next_arg.d = double_tmp;
194 *fpr_base.d++ = double_tmp;
196 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
199 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
200 case FFI_TYPE_LONGDOUBLE:
201 if (ecif->cif->abi != FFI_LINUX)
203 double_tmp = (*p_argv.d)[0];
205 if (fparg_count >= NUM_FPR_ARG_REGISTERS - 1)
207 if (intarg_count >= NUM_GPR_ARG_REGISTERS
208 && intarg_count % 2 != 0)
213 *next_arg.d = double_tmp;
215 double_tmp = (*p_argv.d)[1];
216 *next_arg.d = double_tmp;
221 *fpr_base.d++ = double_tmp;
222 double_tmp = (*p_argv.d)[1];
223 *fpr_base.d++ = double_tmp;
227 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
231 case FFI_TYPE_UINT64:
232 case FFI_TYPE_SINT64:
233 if (intarg_count == NUM_GPR_ARG_REGISTERS-1)
235 if (intarg_count >= NUM_GPR_ARG_REGISTERS)
237 if (intarg_count % 2 != 0)
242 *next_arg.ll = **p_argv.ll;
247 /* whoops: abi states only certain register pairs
248 * can be used for passing long long int
249 * specifically (r3,r4), (r5,r6), (r7,r8),
250 * (r9,r10) and if next arg is long long but
251 * not correct starting register of pair then skip
252 * until the proper starting register
254 if (intarg_count % 2 != 0)
259 *gpr_base.ll++ = **p_argv.ll;
264 case FFI_TYPE_STRUCT:
265 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
268 struct_copy_size = ((*ptr)->size + 15) & ~0xF;
269 copy_space.c -= struct_copy_size;
270 memcpy (copy_space.c, *p_argv.c, (*ptr)->size);
272 gprvalue = (unsigned long) copy_space.c;
274 FFI_ASSERT (copy_space.c > next_arg.c);
275 FFI_ASSERT (flags & FLAG_ARG_NEEDS_COPY);
279 gprvalue = **p_argv.uc;
282 gprvalue = **p_argv.sc;
284 case FFI_TYPE_UINT16:
285 gprvalue = **p_argv.us;
287 case FFI_TYPE_SINT16:
288 gprvalue = **p_argv.ss;
292 case FFI_TYPE_UINT32:
293 case FFI_TYPE_SINT32:
294 case FFI_TYPE_POINTER:
295 gprvalue = **p_argv.ui;
298 if (intarg_count >= NUM_GPR_ARG_REGISTERS)
299 *next_arg.u++ = gprvalue;
301 *gpr_base.u++ = gprvalue;
307 /* Check that we didn't overrun the stack... */
308 FFI_ASSERT (copy_space.c >= next_arg.c);
309 FFI_ASSERT (gpr_base.u <= stacktop.u - ASM_NEEDS_REGISTERS);
310 FFI_ASSERT (fpr_base.u
311 <= stacktop.u - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
312 FFI_ASSERT (flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
315 /* About the LINUX64 ABI. */
317 NUM_GPR_ARG_REGISTERS64 = 8,
318 NUM_FPR_ARG_REGISTERS64 = 13
320 enum { ASM_NEEDS_REGISTERS64 = 4 };
322 /* ffi_prep_args64 is called by the assembly routine once stack space
323 has been allocated for the function's arguments.
325 The stack layout we want looks like this:
327 | Ret addr from ffi_call_LINUX64 8bytes | higher addresses
328 |--------------------------------------------|
329 | CR save area 8bytes |
330 |--------------------------------------------|
331 | Previous backchain pointer 8 | stack pointer here
332 |--------------------------------------------|<+ <<< on entry to
333 | Saved r28-r31 4*8 | | ffi_call_LINUX64
334 |--------------------------------------------| |
335 | GPR registers r3-r10 8*8 | |
336 |--------------------------------------------| |
337 | FPR registers f1-f13 (optional) 13*8 | |
338 |--------------------------------------------| |
339 | Parameter save area | |
340 |--------------------------------------------| |
341 | TOC save area 8 | |
342 |--------------------------------------------| | stack |
343 | Linker doubleword 8 | | grows |
344 |--------------------------------------------| | down V
345 | Compiler doubleword 8 | |
346 |--------------------------------------------| | lower addresses
347 | Space for callee's LR 8 | |
348 |--------------------------------------------| |
350 |--------------------------------------------| | stack pointer here
351 | Current backchain pointer 8 |-/ during
352 |--------------------------------------------| <<< ffi_call_LINUX64
357 ffi_prep_args64 (extended_cif *ecif, unsigned long *const stack)
359 const unsigned long bytes = ecif->cif->bytes;
360 const unsigned long flags = ecif->cif->flags;
369 /* 'stacktop' points at the previous backchain pointer. */
372 /* 'next_arg' points at the space for gpr3, and grows upwards as
373 we use GPR registers, then continues at rest. */
379 /* 'fpr_base' points at the space for fpr3, and grows upwards as
380 we use FPR registers. */
400 unsigned long gprvalue;
402 stacktop.c = (char *) stack + bytes;
403 gpr_base.ul = stacktop.ul - ASM_NEEDS_REGISTERS64 - NUM_GPR_ARG_REGISTERS64;
404 gpr_end.ul = gpr_base.ul + NUM_GPR_ARG_REGISTERS64;
405 rest.ul = stack + 6 + NUM_GPR_ARG_REGISTERS64;
406 fpr_base.d = gpr_base.d - NUM_FPR_ARG_REGISTERS64;
408 next_arg.ul = gpr_base.ul;
410 /* Check that everything starts aligned properly. */
411 FFI_ASSERT (((unsigned long) (char *) stack & 0xF) == 0);
412 FFI_ASSERT (((unsigned long) stacktop.c & 0xF) == 0);
413 FFI_ASSERT ((bytes & 0xF) == 0);
415 /* Deal with return values that are actually pass-by-reference. */
416 if (flags & FLAG_RETVAL_REFERENCE)
417 *next_arg.ul++ = (unsigned long) (char *) ecif->rvalue;
419 /* Now for the arguments. */
420 p_argv.v = ecif->avalue;
421 for (ptr = ecif->cif->arg_types, i = ecif->cif->nargs;
423 i--, ptr++, p_argv.v++)
425 switch ((*ptr)->type)
428 double_tmp = **p_argv.f;
429 *next_arg.f = (float) double_tmp;
430 if (++next_arg.ul == gpr_end.ul)
431 next_arg.ul = rest.ul;
432 if (fparg_count < NUM_FPR_ARG_REGISTERS64)
433 *fpr_base.d++ = double_tmp;
435 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
438 case FFI_TYPE_DOUBLE:
439 double_tmp = **p_argv.d;
440 *next_arg.d = double_tmp;
441 if (++next_arg.ul == gpr_end.ul)
442 next_arg.ul = rest.ul;
443 if (fparg_count < NUM_FPR_ARG_REGISTERS64)
444 *fpr_base.d++ = double_tmp;
446 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
449 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
450 case FFI_TYPE_LONGDOUBLE:
451 double_tmp = (*p_argv.d)[0];
452 *next_arg.d = double_tmp;
453 if (++next_arg.ul == gpr_end.ul)
454 next_arg.ul = rest.ul;
455 if (fparg_count < NUM_FPR_ARG_REGISTERS64)
456 *fpr_base.d++ = double_tmp;
458 double_tmp = (*p_argv.d)[1];
459 *next_arg.d = double_tmp;
460 if (++next_arg.ul == gpr_end.ul)
461 next_arg.ul = rest.ul;
462 if (fparg_count < NUM_FPR_ARG_REGISTERS64)
463 *fpr_base.d++ = double_tmp;
465 FFI_ASSERT (__LDBL_MANT_DIG__ == 106);
466 FFI_ASSERT (flags & FLAG_FP_ARGUMENTS);
470 case FFI_TYPE_STRUCT:
471 words = ((*ptr)->size + 7) / 8;
472 if (next_arg.ul >= gpr_base.ul && next_arg.ul + words > gpr_end.ul)
474 size_t first = gpr_end.c - next_arg.c;
475 memcpy (next_arg.c, *p_argv.c, first);
476 memcpy (rest.c, *p_argv.c + first, (*ptr)->size - first);
477 next_arg.c = rest.c + words * 8 - first;
481 char *where = next_arg.c;
483 /* Structures with size less than eight bytes are passed
485 if ((*ptr)->size < 8)
486 where += 8 - (*ptr)->size;
488 memcpy (where, *p_argv.c, (*ptr)->size);
489 next_arg.ul += words;
490 if (next_arg.ul == gpr_end.ul)
491 next_arg.ul = rest.ul;
496 gprvalue = **p_argv.uc;
499 gprvalue = **p_argv.sc;
501 case FFI_TYPE_UINT16:
502 gprvalue = **p_argv.us;
504 case FFI_TYPE_SINT16:
505 gprvalue = **p_argv.ss;
507 case FFI_TYPE_UINT32:
508 gprvalue = **p_argv.ui;
511 case FFI_TYPE_SINT32:
512 gprvalue = **p_argv.si;
515 case FFI_TYPE_UINT64:
516 case FFI_TYPE_SINT64:
517 case FFI_TYPE_POINTER:
518 gprvalue = **p_argv.ul;
520 *next_arg.ul++ = gprvalue;
521 if (next_arg.ul == gpr_end.ul)
522 next_arg.ul = rest.ul;
527 FFI_ASSERT (flags & FLAG_4_GPR_ARGUMENTS
528 || (next_arg.ul >= gpr_base.ul
529 && next_arg.ul <= gpr_base.ul + 4));
534 /* Perform machine dependent cif processing */
536 ffi_prep_cif_machdep (ffi_cif *cif)
538 /* All this is for the SYSV and LINUX64 ABI. */
542 int fparg_count = 0, intarg_count = 0;
544 unsigned struct_copy_size = 0;
545 unsigned type = cif->rtype->type;
546 unsigned size = cif->rtype->size;
548 if (cif->abi != FFI_LINUX64)
550 /* All the machine-independent calculation of cif->bytes will be wrong.
551 Redo the calculation for SYSV. */
553 /* Space for the frame pointer, callee's LR, and the asm's temp regs. */
554 bytes = (2 + ASM_NEEDS_REGISTERS) * sizeof (int);
556 /* Space for the GPR registers. */
557 bytes += NUM_GPR_ARG_REGISTERS * sizeof (int);
563 /* Space for backchain, CR, LR, cc/ld doubleword, TOC and the asm's temp
565 bytes = (6 + ASM_NEEDS_REGISTERS64) * sizeof (long);
567 /* Space for the mandatory parm save area and general registers. */
568 bytes += 2 * NUM_GPR_ARG_REGISTERS64 * sizeof (long);
571 /* Return value handling. The rules for SYSV are as follows:
572 - 32-bit (or less) integer values are returned in gpr3;
573 - Structures of size <= 4 bytes also returned in gpr3;
574 - 64-bit integer values and structures between 5 and 8 bytes are returned
576 - Single/double FP values are returned in fpr1;
577 - Larger structures are allocated space and a pointer is passed as
579 - long doubles (if not equivalent to double) are returned in
580 fpr1,fpr2 for Linux and as for large structs for SysV.
582 - integer values in gpr3;
583 - Structures/Unions by reference;
584 - Single/double FP values in fpr1, long double in fpr1,fpr2. */
587 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
588 case FFI_TYPE_LONGDOUBLE:
589 if (cif->abi != FFI_LINUX && cif->abi != FFI_LINUX64)
592 flags |= FLAG_RETURNS_128BITS;
595 case FFI_TYPE_DOUBLE:
596 flags |= FLAG_RETURNS_64BITS;
599 flags |= FLAG_RETURNS_FP;
602 case FFI_TYPE_UINT64:
603 case FFI_TYPE_SINT64:
604 flags |= FLAG_RETURNS_64BITS;
607 case FFI_TYPE_STRUCT:
608 if (cif->abi == FFI_SYSV)
610 /* The final SYSV ABI says that structures smaller or equal 8 bytes
611 are returned in r3/r4. The FFI_GCC_SYSV ABI instead returns them
614 /* Treat structs with size <= 8 bytes. */
617 flags |= FLAG_RETURNS_SMST;
618 /* These structs are returned in r3. We pack the type and the
619 precalculated shift value (needed in the sysv.S) into flags.
620 The same applies for the structs returned in r3/r4. */
623 flags |= 1 << (31 - FFI_SYSV_TYPE_SMALL_STRUCT - 1);
624 flags |= 8 * (4 - size) << 4;
627 /* These structs are returned in r3 and r4. See above. */
630 flags |= 1 << (31 - FFI_SYSV_TYPE_SMALL_STRUCT - 2);
631 flags |= 8 * (8 - size) << 4;
636 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
640 flags |= FLAG_RETVAL_REFERENCE;
643 flags |= FLAG_RETURNS_NOTHING;
647 /* Returns 32-bit integer, or similar. Nothing to do here. */
651 if (cif->abi != FFI_LINUX64)
652 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
653 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
654 goes on the stack. Structures and long doubles (if not equivalent
655 to double) are passed as a pointer to a copy of the structure.
656 Stuff on the stack needs to keep proper alignment. */
657 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
659 switch ((*ptr)->type)
663 /* floating singles are not 8-aligned on stack */
666 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
667 case FFI_TYPE_LONGDOUBLE:
668 if (cif->abi != FFI_LINUX)
673 case FFI_TYPE_DOUBLE:
675 /* If this FP arg is going on the stack, it must be
677 if (fparg_count > NUM_FPR_ARG_REGISTERS
678 && intarg_count >= NUM_GPR_ARG_REGISTERS
679 && intarg_count % 2 != 0)
683 case FFI_TYPE_UINT64:
684 case FFI_TYPE_SINT64:
685 /* 'long long' arguments are passed as two words, but
686 either both words must fit in registers or both go
687 on the stack. If they go on the stack, they must
690 Also, only certain register pairs can be used for
691 passing long long int -- specifically (r3,r4), (r5,r6),
694 if (intarg_count == NUM_GPR_ARG_REGISTERS-1
695 || intarg_count % 2 != 0)
700 case FFI_TYPE_STRUCT:
701 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
704 /* We must allocate space for a copy of these to enforce
705 pass-by-value. Pad the space up to a multiple of 16
706 bytes (the maximum alignment required for anything under
708 struct_copy_size += ((*ptr)->size + 15) & ~0xF;
709 /* Fall through (allocate space for the pointer). */
712 /* Everything else is passed as a 4-byte word in a GPR, either
713 the object itself or a pointer to it. */
719 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
721 switch ((*ptr)->type)
723 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
724 case FFI_TYPE_LONGDOUBLE:
730 case FFI_TYPE_DOUBLE:
735 case FFI_TYPE_STRUCT:
736 intarg_count += ((*ptr)->size + 7) / 8;
740 /* Everything else is passed as a 8-byte word in a GPR, either
741 the object itself or a pointer to it. */
747 if (fparg_count != 0)
748 flags |= FLAG_FP_ARGUMENTS;
749 if (intarg_count > 4)
750 flags |= FLAG_4_GPR_ARGUMENTS;
751 if (struct_copy_size != 0)
752 flags |= FLAG_ARG_NEEDS_COPY;
754 if (cif->abi != FFI_LINUX64)
756 /* Space for the FPR registers, if needed. */
757 if (fparg_count != 0)
758 bytes += NUM_FPR_ARG_REGISTERS * sizeof (double);
761 if (intarg_count > NUM_GPR_ARG_REGISTERS)
762 bytes += (intarg_count - NUM_GPR_ARG_REGISTERS) * sizeof (int);
763 if (fparg_count > NUM_FPR_ARG_REGISTERS)
764 bytes += (fparg_count - NUM_FPR_ARG_REGISTERS) * sizeof (double);
768 /* Space for the FPR registers, if needed. */
769 if (fparg_count != 0)
770 bytes += NUM_FPR_ARG_REGISTERS64 * sizeof (double);
773 if (intarg_count > NUM_GPR_ARG_REGISTERS64)
774 bytes += (intarg_count - NUM_GPR_ARG_REGISTERS64) * sizeof (long);
777 /* The stack space allocated needs to be a multiple of 16 bytes. */
778 bytes = (bytes + 15) & ~0xF;
780 /* Add in the space for the copied structures. */
781 bytes += struct_copy_size;
789 extern void ffi_call_SYSV(extended_cif *, unsigned, unsigned, unsigned *,
791 extern void FFI_HIDDEN ffi_call_LINUX64(extended_cif *, unsigned long,
792 unsigned long, unsigned long *,
796 ffi_call(ffi_cif *cif, void (*fn)(), void *rvalue, void **avalue)
801 ecif.avalue = avalue;
803 /* If the return value is a struct and we don't have a return */
804 /* value address then we need to make one */
806 if ((rvalue == NULL) && (cif->rtype->type == FFI_TYPE_STRUCT))
808 ecif.rvalue = alloca(cif->rtype->size);
811 ecif.rvalue = rvalue;
820 ffi_call_SYSV (&ecif, -cif->bytes, cif->flags, ecif.rvalue, fn);
824 ffi_call_LINUX64 (&ecif, -(long) cif->bytes, cif->flags, ecif.rvalue, fn);
835 #define MIN_CACHE_LINE_SIZE 8
838 flush_icache (char *wraddr, char *xaddr, int size)
841 for (i = 0; i < size; i += MIN_CACHE_LINE_SIZE)
844 __asm__ volatile ("icbi 0,%0;" "dcbf 0,%1;"
845 : : "r" (xaddr + i), "r" (wraddr + i) : "memory");
847 __asm__ volatile ("icbi 0,%0;" "dcbf 0,%1;" "sync;" "isync;"
848 : : "r"(xaddr + size - 1), "r"(wraddr + size - 1)
854 ffi_prep_closure_loc (ffi_closure *closure,
856 void (*fun) (ffi_cif *, void *, void **, void *),
861 void **tramp = (void **) &closure->tramp[0];
863 FFI_ASSERT (cif->abi == FFI_LINUX64);
864 /* Copy function address and TOC from ffi_closure_LINUX64. */
865 memcpy (tramp, (char *) ffi_closure_LINUX64, 16);
866 tramp[2] = (void *) codeloc;
870 FFI_ASSERT (cif->abi == FFI_GCC_SYSV || cif->abi == FFI_SYSV);
872 tramp = (unsigned int *) &closure->tramp[0];
873 tramp[0] = 0x7c0802a6; /* mflr r0 */
874 tramp[1] = 0x4800000d; /* bl 10 <trampoline_initial+0x10> */
875 tramp[4] = 0x7d6802a6; /* mflr r11 */
876 tramp[5] = 0x7c0803a6; /* mtlr r0 */
877 tramp[6] = 0x800b0000; /* lwz r0,0(r11) */
878 tramp[7] = 0x816b0004; /* lwz r11,4(r11) */
879 tramp[8] = 0x7c0903a6; /* mtctr r0 */
880 tramp[9] = 0x4e800420; /* bctr */
881 *(void **) &tramp[2] = (void *) ffi_closure_SYSV; /* function */
882 *(void **) &tramp[3] = (void *) codeloc; /* context */
884 /* Flush the icache. */
885 flush_icache (tramp, codeloc, FFI_TRAMPOLINE_SIZE);
890 closure->user_data = user_data;
901 int ffi_closure_helper_SYSV (ffi_closure *, void *, unsigned long *,
902 ffi_dblfl *, unsigned long *);
904 /* Basically the trampoline invokes ffi_closure_SYSV, and on
905 * entry, r11 holds the address of the closure.
906 * After storing the registers that could possibly contain
907 * parameters to be passed into the stack frame and setting
908 * up space for a return value, ffi_closure_SYSV invokes the
909 * following helper function to do most of the work
913 ffi_closure_helper_SYSV (ffi_closure *closure, void *rvalue,
914 unsigned long *pgr, ffi_dblfl *pfr,
917 /* rvalue is the pointer to space for return value in closure assembly */
918 /* pgr is the pointer to where r3-r10 are stored in ffi_closure_SYSV */
919 /* pfr is the pointer to where f1-f8 are stored in ffi_closure_SYSV */
920 /* pst is the pointer to outgoing parameter stack in original caller */
923 ffi_type ** arg_types;
925 long nf; /* number of floating registers already used */
926 long ng; /* number of general registers already used */
932 avalue = alloca (cif->nargs * sizeof (void *));
933 size = cif->rtype->size;
938 /* Copy the caller's structure return value address so that the closure
939 returns the data directly to the caller.
940 For FFI_SYSV the result is passed in r3/r4 if the struct size is less
943 if ((cif->rtype->type == FFI_TYPE_STRUCT
944 && !((cif->abi == FFI_SYSV) && (size <= 8)))
945 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
946 || (cif->rtype->type == FFI_TYPE_LONGDOUBLE
947 && cif->abi != FFI_LINUX)
951 rvalue = (void *) *pgr;
958 arg_types = cif->arg_types;
960 /* Grab the addresses of the arguments from the stack frame. */
963 switch (arg_types[i]->type)
967 /* there are 8 gpr registers used to pass values */
970 avalue[i] = (char *) pgr + 3;
976 avalue[i] = (char *) pst + 3;
981 case FFI_TYPE_SINT16:
982 case FFI_TYPE_UINT16:
983 /* there are 8 gpr registers used to pass values */
986 avalue[i] = (char *) pgr + 2;
992 avalue[i] = (char *) pst + 2;
997 case FFI_TYPE_SINT32:
998 case FFI_TYPE_UINT32:
999 case FFI_TYPE_POINTER:
1000 /* there are 8 gpr registers used to pass values */
1014 case FFI_TYPE_STRUCT:
1015 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1018 /* Structs are passed by reference. The address will appear in a
1019 gpr if it is one of the first 8 arguments. */
1022 avalue[i] = (void *) *pgr;
1028 avalue[i] = (void *) *pst;
1033 case FFI_TYPE_SINT64:
1034 case FFI_TYPE_UINT64:
1035 /* passing long long ints are complex, they must
1036 * be passed in suitable register pairs such as
1037 * (r3,r4) or (r5,r6) or (r6,r7), or (r7,r8) or (r9,r10)
1038 * and if the entire pair aren't available then the outgoing
1039 * parameter stack is used for both but an alignment of 8
1040 * must will be kept. So we must either look in pgr
1041 * or pst to find the correct address for this type
1048 /* skip r4, r6, r8 as starting points */
1058 if (((long) pst) & 4)
1065 case FFI_TYPE_FLOAT:
1066 /* unfortunately float values are stored as doubles
1067 * in the ffi_closure_SYSV code (since we don't check
1068 * the type in that routine).
1071 /* there are 8 64bit floating point registers */
1076 pfr->f = (float) temp;
1083 /* FIXME? here we are really changing the values
1084 * stored in the original calling routines outgoing
1085 * parameter stack. This is probably a really
1086 * naughty thing to do but...
1093 case FFI_TYPE_DOUBLE:
1094 /* On the outgoing stack all values are aligned to 8 */
1095 /* there are 8 64bit floating point registers */
1105 if (((long) pst) & 4)
1112 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1113 case FFI_TYPE_LONGDOUBLE:
1114 if (cif->abi != FFI_LINUX)
1125 if (((long) pst) & 4)
1142 (closure->fun) (cif, rvalue, avalue, closure->user_data);
1144 /* Tell ffi_closure_SYSV how to perform return type promotions.
1145 Because the FFI_SYSV ABI returns the structures <= 8 bytes in r3/r4
1146 we have to tell ffi_closure_SYSV how to treat them. */
1147 if (cif->abi == FFI_SYSV && cif->rtype->type == FFI_TYPE_STRUCT
1149 return FFI_SYSV_TYPE_SMALL_STRUCT + size;
1150 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1151 else if (cif->rtype->type == FFI_TYPE_LONGDOUBLE
1152 && cif->abi != FFI_LINUX)
1153 return FFI_TYPE_STRUCT;
1155 return cif->rtype->type;
1158 int FFI_HIDDEN ffi_closure_helper_LINUX64 (ffi_closure *, void *,
1159 unsigned long *, ffi_dblfl *);
1162 ffi_closure_helper_LINUX64 (ffi_closure *closure, void *rvalue,
1163 unsigned long *pst, ffi_dblfl *pfr)
1165 /* rvalue is the pointer to space for return value in closure assembly */
1166 /* pst is the pointer to parameter save area
1167 (r3-r10 are stored into its first 8 slots by ffi_closure_LINUX64) */
1168 /* pfr is the pointer to where f1-f13 are stored in ffi_closure_LINUX64 */
1171 ffi_type **arg_types;
1174 ffi_dblfl *end_pfr = pfr + NUM_FPR_ARG_REGISTERS64;
1177 avalue = alloca (cif->nargs * sizeof (void *));
1179 /* Copy the caller's structure return value address so that the closure
1180 returns the data directly to the caller. */
1181 if (cif->rtype->type == FFI_TYPE_STRUCT)
1183 rvalue = (void *) *pst;
1189 arg_types = cif->arg_types;
1191 /* Grab the addresses of the arguments from the stack frame. */
1194 switch (arg_types[i]->type)
1196 case FFI_TYPE_SINT8:
1197 case FFI_TYPE_UINT8:
1198 avalue[i] = (char *) pst + 7;
1202 case FFI_TYPE_SINT16:
1203 case FFI_TYPE_UINT16:
1204 avalue[i] = (char *) pst + 6;
1208 case FFI_TYPE_SINT32:
1209 case FFI_TYPE_UINT32:
1210 avalue[i] = (char *) pst + 4;
1214 case FFI_TYPE_SINT64:
1215 case FFI_TYPE_UINT64:
1216 case FFI_TYPE_POINTER:
1221 case FFI_TYPE_STRUCT:
1222 /* Structures with size less than eight bytes are passed
1224 if (arg_types[i]->size < 8)
1225 avalue[i] = (char *) pst + 8 - arg_types[i]->size;
1228 pst += (arg_types[i]->size + 7) / 8;
1231 case FFI_TYPE_FLOAT:
1232 /* unfortunately float values are stored as doubles
1233 * in the ffi_closure_LINUX64 code (since we don't check
1234 * the type in that routine).
1237 /* there are 13 64bit floating point registers */
1241 double temp = pfr->d;
1242 pfr->f = (float) temp;
1251 case FFI_TYPE_DOUBLE:
1252 /* On the outgoing stack all values are aligned to 8 */
1253 /* there are 13 64bit floating point registers */
1265 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
1266 case FFI_TYPE_LONGDOUBLE:
1267 if (pfr + 1 < end_pfr)
1276 /* Passed partly in f13 and partly on the stack.
1277 Move it all to the stack. */
1278 *pst = *(unsigned long *) pfr;
1295 (closure->fun) (cif, rvalue, avalue, closure->user_data);
1297 /* Tell ffi_closure_LINUX64 how to perform return type promotions. */
1298 return cif->rtype->type;