1 /* -----------------------------------------------------------------------
4 Copyright (C) 1998 Geoffrey Keating
5 Copyright (C) 2001 John Hornkvist
6 Copyright (C) 2002, 2006, 2007, 2009 Free Software Foundation, Inc.
8 FFI support for Darwin and AIX.
10 Permission is hereby granted, free of charge, to any person obtaining
11 a copy of this software and associated documentation files (the
12 ``Software''), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sublicense, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
18 The above copyright notice and this permission notice shall be included
19 in all copies or substantial portions of the Software.
21 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 OTHER DEALINGS IN THE SOFTWARE.
28 ----------------------------------------------------------------------- */
31 #include <ffi_common.h>
35 extern void ffi_closure_ASM(void);
38 /* The assembly depends on these exact flags. */
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-31),
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 DARWIN ABI. */
52 NUM_GPR_ARG_REGISTERS = 8,
53 NUM_FPR_ARG_REGISTERS = 13
55 enum { ASM_NEEDS_REGISTERS = 4 };
57 /* ffi_prep_args 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_DARWIN | higher addresses
63 |--------------------------------------------|
64 | Previous backchain pointer 4 | stack pointer here
65 |--------------------------------------------|<+ <<< on entry to
66 | Saved r28-r31 4*4 | | ffi_call_DARWIN
67 |--------------------------------------------| |
68 | Parameters (at least 8*4=32) | |
69 |--------------------------------------------| |
70 | Space for GPR2 4 | |
71 |--------------------------------------------| | stack |
72 | Reserved 2*4 | | grows |
73 |--------------------------------------------| | down V
74 | Space for callee's LR 4 | |
75 |--------------------------------------------| | lower addresses
77 |--------------------------------------------| | stack pointer here
78 | Current backchain pointer 4 |-/ during
79 |--------------------------------------------| <<< ffi_call_DARWIN
83 void ffi_prep_args(extended_cif *ecif, unsigned long *const stack)
85 const unsigned bytes = ecif->cif->bytes;
86 const unsigned flags = ecif->cif->flags;
88 /* 'stacktop' points at the previous backchain pointer. */
89 unsigned long *const stacktop = stack + (bytes / sizeof(unsigned long));
91 /* 'fpr_base' points at the space for fpr1, and grows upwards as
92 we use FPR registers. */
93 double *fpr_base = (double *) (stacktop - ASM_NEEDS_REGISTERS) - NUM_FPR_ARG_REGISTERS;
97 /* 'next_arg' grows up as we put parameters in it. */
98 unsigned long *next_arg = stack + 6; /* 6 reserved positions. */
102 void **p_argv = ecif->avalue;
103 unsigned long gprvalue;
104 ffi_type** ptr = ecif->cif->arg_types;
106 unsigned size_al = 0;
108 /* Check that everything starts aligned properly. */
109 FFI_ASSERT(((unsigned) (char *) stack & 0xF) == 0);
110 FFI_ASSERT(((unsigned) (char *) stacktop & 0xF) == 0);
111 FFI_ASSERT((bytes & 0xF) == 0);
113 /* Deal with return values that are actually pass-by-reference.
115 Return values are referenced by r3, so r4 is the first parameter. */
117 if (flags & FLAG_RETVAL_REFERENCE)
118 *next_arg++ = (unsigned long) (char *) ecif->rvalue;
120 /* Now for the arguments. */
121 for (i = ecif->cif->nargs; i > 0; i--, ptr++, p_argv++)
123 switch ((*ptr)->type)
125 /* If a floating-point parameter appears before all of the general-
126 purpose registers are filled, the corresponding GPRs that match
127 the size of the floating-point parameter are skipped. */
129 double_tmp = *(float *) *p_argv;
130 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
131 *(double *)next_arg = double_tmp;
133 *fpr_base++ = double_tmp;
136 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
139 case FFI_TYPE_DOUBLE:
140 double_tmp = *(double *) *p_argv;
141 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
142 *(double *)next_arg = double_tmp;
144 *fpr_base++ = double_tmp;
151 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
154 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
156 case FFI_TYPE_LONGDOUBLE:
158 if (fparg_count < NUM_FPR_ARG_REGISTERS)
159 *(long double *) fpr_base++ = *(long double *) *p_argv;
161 *(long double *) next_arg = *(long double *) *p_argv;
165 double_tmp = ((double *) *p_argv)[0];
166 if (fparg_count < NUM_FPR_ARG_REGISTERS)
167 *fpr_base++ = double_tmp;
169 *(double *) next_arg = double_tmp;
171 double_tmp = ((double *) *p_argv)[1];
172 if (fparg_count < NUM_FPR_ARG_REGISTERS)
173 *fpr_base++ = double_tmp;
175 *(double *) next_arg = double_tmp;
179 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
182 case FFI_TYPE_UINT64:
183 case FFI_TYPE_SINT64:
185 gprvalue = *(long long *) *p_argv;
188 *(long long *) next_arg = *(long long *) *p_argv;
192 case FFI_TYPE_POINTER:
193 gprvalue = *(unsigned long *) *p_argv;
196 gprvalue = *(unsigned char *) *p_argv;
199 gprvalue = *(signed char *) *p_argv;
201 case FFI_TYPE_UINT16:
202 gprvalue = *(unsigned short *) *p_argv;
204 case FFI_TYPE_SINT16:
205 gprvalue = *(signed short *) *p_argv;
208 case FFI_TYPE_STRUCT:
210 dest_cpy = (char *) next_arg;
211 size_al = (*ptr)->size;
212 if ((*ptr)->elements[0]->type == 3)
213 size_al = ALIGN((*ptr)->size, 8);
214 if (size_al < 3 && ecif->cif->abi == FFI_DARWIN)
215 dest_cpy += 4 - size_al;
217 memcpy ((char *) dest_cpy, (char *) *p_argv, size_al);
218 next_arg += (size_al + 7) / 8;
220 dest_cpy = (char *) next_arg;
222 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
223 SI 4 bytes) are aligned as if they were those modes.
224 Structures with 3 byte in size are padded upwards. */
225 size_al = (*ptr)->size;
226 /* If the first member of the struct is a double, then align
227 the struct to double-word. */
228 if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE)
229 size_al = ALIGN((*ptr)->size, 8);
230 if (size_al < 3 && ecif->cif->abi == FFI_DARWIN)
231 dest_cpy += 4 - size_al;
233 memcpy((char *) dest_cpy, (char *) *p_argv, size_al);
234 next_arg += (size_al + 3) / 4;
239 case FFI_TYPE_SINT32:
240 gprvalue = *(signed int *) *p_argv;
243 case FFI_TYPE_UINT32:
244 gprvalue = *(unsigned int *) *p_argv;
246 *next_arg++ = gprvalue;
253 /* Check that we didn't overrun the stack... */
254 //FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS);
255 //FFI_ASSERT((unsigned *)fpr_base
256 // <= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
257 //FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
260 /* Adjust the size of S to be correct for Darwin.
261 On Darwin, the first field of a structure has natural alignment. */
264 darwin_adjust_aggregate_sizes (ffi_type *s)
268 if (s->type != FFI_TYPE_STRUCT)
272 for (i = 0; s->elements[i] != NULL; i++)
278 darwin_adjust_aggregate_sizes (p);
280 && (p->type == FFI_TYPE_UINT64
281 || p->type == FFI_TYPE_SINT64
282 || p->type == FFI_TYPE_DOUBLE
283 || p->alignment == 8))
285 else if (p->alignment == 16 || p->alignment < 4)
286 align = p->alignment;
289 s->size = ALIGN(s->size, align) + p->size;
292 s->size = ALIGN(s->size, s->alignment);
294 if (s->elements[0]->type == FFI_TYPE_UINT64
295 || s->elements[0]->type == FFI_TYPE_SINT64
296 || s->elements[0]->type == FFI_TYPE_DOUBLE
297 || s->elements[0]->alignment == 8)
298 s->alignment = s->alignment > 8 ? s->alignment : 8;
299 /* Do not add additional tail padding. */
302 /* Perform machine dependent cif processing. */
303 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
305 /* All this is for the DARWIN ABI. */
309 int fparg_count = 0, intarg_count = 0;
311 unsigned size_al = 0;
313 /* All the machine-independent calculation of cif->bytes will be wrong.
314 All the calculation of structure sizes will also be wrong.
315 Redo the calculation for DARWIN. */
317 if (cif->abi == FFI_DARWIN)
319 darwin_adjust_aggregate_sizes (cif->rtype);
320 for (i = 0; i < cif->nargs; i++)
321 darwin_adjust_aggregate_sizes (cif->arg_types[i]);
324 /* Space for the frame pointer, callee's LR, CR, etc, and for
325 the asm's temp regs. */
327 bytes = (6 + ASM_NEEDS_REGISTERS) * sizeof(long);
329 /* Return value handling. The rules are as follows:
330 - 32-bit (or less) integer values are returned in gpr3;
331 - Structures of size <= 4 bytes also returned in gpr3;
332 - 64-bit integer values and structures between 5 and 8 bytes are returned
334 - Single/double FP values are returned in fpr1;
335 - Long double FP (if not equivalent to double) values are returned in
337 - Larger structures values are allocated space and a pointer is passed
338 as the first argument. */
339 switch (cif->rtype->type)
342 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
343 case FFI_TYPE_LONGDOUBLE:
344 flags |= FLAG_RETURNS_128BITS;
345 flags |= FLAG_RETURNS_FP;
349 case FFI_TYPE_DOUBLE:
350 flags |= FLAG_RETURNS_64BITS;
353 flags |= FLAG_RETURNS_FP;
356 case FFI_TYPE_UINT64:
357 case FFI_TYPE_SINT64:
359 case FFI_TYPE_POINTER:
361 flags |= FLAG_RETURNS_64BITS;
364 case FFI_TYPE_STRUCT:
365 flags |= FLAG_RETVAL_REFERENCE;
366 flags |= FLAG_RETURNS_NOTHING;
370 flags |= FLAG_RETURNS_NOTHING;
374 /* Returns 32-bit integer, or similar. Nothing to do here. */
378 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
379 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
380 goes on the stack. Structures are passed as a pointer to a copy of
381 the structure. Stuff on the stack needs to keep proper alignment. */
382 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
384 switch ((*ptr)->type)
387 case FFI_TYPE_DOUBLE:
389 /* If this FP arg is going on the stack, it must be
391 if (fparg_count > NUM_FPR_ARG_REGISTERS
392 && intarg_count%2 != 0)
396 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
398 case FFI_TYPE_LONGDOUBLE:
400 /* If this FP arg is going on the stack, it must be
402 if (fparg_count > NUM_FPR_ARG_REGISTERS
403 && intarg_count%2 != 0)
409 case FFI_TYPE_UINT64:
410 case FFI_TYPE_SINT64:
411 /* 'long long' arguments are passed as two words, but
412 either both words must fit in registers or both go
413 on the stack. If they go on the stack, they must
414 be 8-byte-aligned. */
415 if (intarg_count == NUM_GPR_ARG_REGISTERS-1
416 || (intarg_count >= NUM_GPR_ARG_REGISTERS && intarg_count%2 != 0))
421 case FFI_TYPE_STRUCT:
422 size_al = (*ptr)->size;
423 /* If the first member of the struct is a double, then align
424 the struct to double-word. */
425 if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE)
426 size_al = ALIGN((*ptr)->size, 8);
428 intarg_count += (size_al + 7) / 8;
430 intarg_count += (size_al + 3) / 4;
435 /* Everything else is passed as a 4-byte word in a GPR, either
436 the object itself or a pointer to it. */
442 if (fparg_count != 0)
443 flags |= FLAG_FP_ARGUMENTS;
445 /* Space for the FPR registers, if needed. */
446 if (fparg_count != 0)
447 bytes += NUM_FPR_ARG_REGISTERS * sizeof(double);
451 if ((intarg_count + fparg_count) > NUM_GPR_ARG_REGISTERS)
452 bytes += (intarg_count + fparg_count) * sizeof(long);
454 if ((intarg_count + 2 * fparg_count) > NUM_GPR_ARG_REGISTERS)
455 bytes += (intarg_count + 2 * fparg_count) * sizeof(long);
458 bytes += NUM_GPR_ARG_REGISTERS * sizeof(long);
460 /* The stack space allocated needs to be a multiple of 16 bytes. */
461 bytes = (bytes + 15) & ~0xF;
469 extern void ffi_call_AIX(extended_cif *, long, unsigned, unsigned *,
470 void (*fn)(void), void (*fn2)(void));
471 extern void ffi_call_DARWIN(extended_cif *, long, unsigned, unsigned *,
472 void (*fn)(void), void (*fn2)(void));
474 void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
479 ecif.avalue = avalue;
481 /* If the return value is a struct and we don't have a return
482 value address then we need to make one. */
484 if ((rvalue == NULL) &&
485 (cif->rtype->type == FFI_TYPE_STRUCT))
487 ecif.rvalue = alloca(cif->rtype->size);
490 ecif.rvalue = rvalue;
495 ffi_call_AIX(&ecif, -(long)cif->bytes, cif->flags, ecif.rvalue, fn,
499 ffi_call_DARWIN(&ecif, -(long)cif->bytes, cif->flags, ecif.rvalue, fn,
508 static void flush_icache(char *);
509 static void flush_range(char *, int);
511 /* The layout of a function descriptor. A C function pointer really
512 points to one of these. */
514 typedef struct aix_fd_struct {
519 /* here I'd like to add the stack frame layout we use in darwin_closure.S
522 SP previous -> +---------------------------------------+ <--- child frame
523 | back chain to caller 4 |
524 +---------------------------------------+ 4
526 +---------------------------------------+ 8
528 +---------------------------------------+ 12
529 | reserved for compilers 4 |
530 +---------------------------------------+ 16
531 | reserved for binders 4 |
532 +---------------------------------------+ 20
533 | saved TOC pointer 4 |
534 +---------------------------------------+ 24
535 | always reserved 8*4=32 (previous GPRs)|
536 | according to the linkage convention |
538 +---------------------------------------+ 56
539 | our FPR area 13*8=104 |
543 +---------------------------------------+ 160
545 +---------------------------------------+ 168
546 | alignement to the next multiple of 16 |
547 SP current --> +---------------------------------------+ 176 <- parent frame
548 | back chain to caller 4 |
549 +---------------------------------------+ 180
551 +---------------------------------------+ 184
553 +---------------------------------------+ 188
554 | reserved for compilers 4 |
555 +---------------------------------------+ 192
556 | reserved for binders 4 |
557 +---------------------------------------+ 196
558 | saved TOC pointer 4 |
559 +---------------------------------------+ 200
560 | always reserved 8*4=32 we store our |
565 +---------------------------------------+ 232
567 +---------------------------------------+ xxx
569 +---------------------------------------+ xxx
573 ffi_prep_closure_loc (ffi_closure* closure,
575 void (*fun)(ffi_cif*, void*, void**, void*),
580 struct ffi_aix_trampoline_struct *tramp_aix;
587 FFI_ASSERT (cif->abi == FFI_DARWIN);
589 tramp = (unsigned int *) &closure->tramp[0];
590 tramp[0] = 0x7c0802a6; /* mflr r0 */
591 tramp[1] = 0x429f000d; /* bcl- 20,4*cr7+so,0x10 */
592 tramp[4] = 0x7d6802a6; /* mflr r11 */
593 tramp[5] = 0x818b0000; /* lwz r12,0(r11) function address */
594 tramp[6] = 0x7c0803a6; /* mtlr r0 */
595 tramp[7] = 0x7d8903a6; /* mtctr r12 */
596 tramp[8] = 0x816b0004; /* lwz r11,4(r11) static chain */
597 tramp[9] = 0x4e800420; /* bctr */
598 tramp[2] = (unsigned long) ffi_closure_ASM; /* function */
599 tramp[3] = (unsigned long) codeloc; /* context */
603 closure->user_data = user_data;
605 /* Flush the icache. Only necessary on Darwin. */
606 flush_range(codeloc, FFI_TRAMPOLINE_SIZE);
612 tramp_aix = (struct ffi_aix_trampoline_struct *) (closure->tramp);
613 fd = (aix_fd *)(void *)ffi_closure_ASM;
615 FFI_ASSERT (cif->abi == FFI_AIX);
617 tramp_aix->code_pointer = fd->code_pointer;
618 tramp_aix->toc = fd->toc;
619 tramp_aix->static_chain = codeloc;
622 closure->user_data = user_data;
633 flush_icache(char *addr)
642 : : "r"(addr) : "memory");
647 flush_range(char * addr1, int size)
649 #define MIN_LINE_SIZE 32
651 for (i = 0; i < size; i += MIN_LINE_SIZE)
652 flush_icache(addr1+i);
653 flush_icache(addr1+size-1);
662 int ffi_closure_helper_DARWIN (ffi_closure*, void*,
663 unsigned long*, ffi_dblfl*);
665 /* Basically the trampoline invokes ffi_closure_ASM, and on
666 entry, r11 holds the address of the closure.
667 After storing the registers that could possibly contain
668 parameters to be passed into the stack frame and setting
669 up space for a return value, ffi_closure_ASM invokes the
670 following helper function to do most of the work. */
672 int ffi_closure_helper_DARWIN (ffi_closure* closure, void * rvalue,
673 unsigned long * pgr, ffi_dblfl * pfr)
675 /* rvalue is the pointer to space for return value in closure assembly
676 pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM
677 pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM. */
679 typedef double ldbits[2];
688 ffi_type ** arg_types;
691 ffi_dblfl *end_pfr = pfr + NUM_FPR_ARG_REGISTERS;
695 avalue = alloca(cif->nargs * sizeof(void *));
697 /* Copy the caller's structure return value address so that the closure
698 returns the data directly to the caller. */
699 if (cif->rtype->type == FFI_TYPE_STRUCT)
701 rvalue = (void *) *pgr;
707 arg_types = cif->arg_types;
709 /* Grab the addresses of the arguments from the stack frame. */
712 switch (arg_types[i]->type)
717 avalue[i] = (char *) pgr + 7;
719 avalue[i] = (char *) pgr + 3;
724 case FFI_TYPE_SINT16:
725 case FFI_TYPE_UINT16:
727 avalue[i] = (char *) pgr + 6;
729 avalue[i] = (char *) pgr + 2;
734 case FFI_TYPE_SINT32:
735 case FFI_TYPE_UINT32:
737 avalue[i] = (char *) pgr + 4;
739 case FFI_TYPE_POINTER:
745 case FFI_TYPE_STRUCT:
747 size_al = arg_types[i]->size;
748 if (arg_types[i]->elements[0]->type == FFI_TYPE_DOUBLE)
749 size_al = ALIGN (arg_types[i]->size, 8);
750 if (size_al < 3 && cif->abi == FFI_DARWIN)
751 avalue[i] = (void *) pgr + 8 - size_al;
753 avalue[i] = (void *) pgr;
754 pgr += (size_al + 7) / 8;
756 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
757 SI 4 bytes) are aligned as if they were those modes. */
758 size_al = arg_types[i]->size;
759 /* If the first member of the struct is a double, then align
760 the struct to double-word. */
761 if (arg_types[i]->elements[0]->type == FFI_TYPE_DOUBLE)
762 size_al = ALIGN(arg_types[i]->size, 8);
763 if (size_al < 3 && cif->abi == FFI_DARWIN)
764 avalue[i] = (void*) pgr + 4 - size_al;
766 avalue[i] = (void*) pgr;
767 pgr += (size_al + 3) / 4;
771 case FFI_TYPE_SINT64:
772 case FFI_TYPE_UINT64:
774 case FFI_TYPE_POINTER:
779 /* Long long ints are passed in two gpr's. */
786 /* A float value consumes a GPR.
787 There are 13 64bit floating point registers. */
790 double temp = pfr->d;
791 pfr->f = (float) temp;
802 case FFI_TYPE_DOUBLE:
803 /* A double value consumes two GPRs.
804 There are 13 64bit floating point registers. */
821 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
823 case FFI_TYPE_LONGDOUBLE:
825 if (pfr + 1 < end_pfr)
834 *pgr = *(unsigned long *) pfr;
840 #else /* POWERPC64 */
841 /* A long double value consumes four GPRs and two FPRs.
842 There are 13 64bit floating point registers. */
843 if (pfr + 1 < end_pfr)
848 /* Here we have the situation where one part of the long double
849 is stored in fpr13 and the other part is already on the stack.
850 We use a union to pass the long double to avalue[i]. */
851 else if (pfr + 1 == end_pfr)
854 memcpy (&temp_ld.lb[0], pfr, sizeof(ldbits));
855 memcpy (&temp_ld.lb[1], pgr + 2, sizeof(ldbits));
856 avalue[i] = &temp_ld.ld;
863 #endif /* POWERPC64 */
872 (closure->fun) (cif, rvalue, avalue, closure->user_data);
874 /* Tell ffi_closure_ASM to perform return type promotions. */
875 return cif->rtype->type;