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)
94 - NUM_FPR_ARG_REGISTERS);
98 /* 'next_arg' grows up as we put parameters in it. */
99 unsigned long *next_arg = (unsigned long *) stack + 6; /* 6 reserved positions. */
103 void **p_argv = ecif->avalue;
104 unsigned long gprvalue;
105 ffi_type** ptr = ecif->cif->arg_types;
107 unsigned size_al = 0;
109 /* Check that everything starts aligned properly. */
110 FFI_ASSERT(((unsigned)(char *)stack & 0xF) == 0);
111 FFI_ASSERT(((unsigned)(char *)stacktop & 0xF) == 0);
112 FFI_ASSERT((bytes & 0xF) == 0);
114 /* Deal with return values that are actually pass-by-reference.
116 Return values are referenced by r3, so r4 is the first parameter. */
118 if (flags & FLAG_RETVAL_REFERENCE)
119 *next_arg++ = (unsigned long)(char *)ecif->rvalue;
121 /* Now for the arguments. */
122 for (i = ecif->cif->nargs; i > 0; i--, ptr++, p_argv++)
124 switch ((*ptr)->type)
126 /* If a floating-point parameter appears before all of the general-
127 purpose registers are filled, the corresponding GPRs that match
128 the size of the floating-point parameter are skipped. */
130 double_tmp = *(float *)*p_argv;
131 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
132 *(double *)next_arg = double_tmp;
134 *fpr_base++ = double_tmp;
137 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
140 case FFI_TYPE_DOUBLE:
141 double_tmp = *(double *)*p_argv;
142 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
143 *(double *)next_arg = double_tmp;
145 *fpr_base++ = double_tmp;
152 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
155 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
157 case FFI_TYPE_LONGDOUBLE:
159 if (fparg_count < NUM_FPR_ARG_REGISTERS)
160 *(long double *) fpr_base++ = *(long double *) *p_argv;
162 *(long double *) next_arg = *(long double *) *p_argv;
166 double_tmp = *((double *) *p_argv);
167 if (fparg_count < NUM_FPR_ARG_REGISTERS)
168 *fpr_base++ = double_tmp;
170 *(double *) next_arg = double_tmp;
172 double_tmp = ((double *) *p_argv)[1];
173 if (fparg_count < NUM_FPR_ARG_REGISTERS)
174 *fpr_base++ = double_tmp;
176 *(double *) next_arg = double_tmp;
180 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
183 case FFI_TYPE_UINT64:
184 case FFI_TYPE_SINT64:
186 gprvalue = *(long long *) *p_argv;
189 *(long long *) next_arg = *(long long *) *p_argv;
193 case FFI_TYPE_POINTER:
194 gprvalue = *(unsigned long *) *p_argv;
197 gprvalue = *(unsigned char *) *p_argv;
200 gprvalue = *(signed char *) *p_argv;
202 case FFI_TYPE_UINT16:
203 gprvalue = *(unsigned short *) *p_argv;
205 case FFI_TYPE_SINT16:
206 gprvalue = *(signed short *) *p_argv;
209 case FFI_TYPE_STRUCT:
211 dest_cpy = (char *) next_arg;
212 size_al = (*ptr)->size;
213 if ((*ptr)->elements[0]->type == 3)
214 size_al = ALIGN((*ptr)->size, 8);
215 if (size_al < 3 && ecif->cif->abi == FFI_DARWIN)
216 dest_cpy += 4 - size_al;
218 memcpy ((char *) dest_cpy, (char *) *p_argv, size_al);
219 next_arg += (size_al + 7) / 8;
221 dest_cpy = (char *) next_arg;
223 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
224 SI 4 bytes) are aligned as if they were those modes.
225 Structures with 3 byte in size are padded upwards. */
226 size_al = (*ptr)->size;
227 /* If the first member of the struct is a double, then align
228 the struct to double-word.
229 Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3. */
230 if ((*ptr)->elements[0]->type == 3)
231 size_al = ALIGN((*ptr)->size, 8);
232 if (size_al < 3 && ecif->cif->abi == FFI_DARWIN)
233 dest_cpy += 4 - size_al;
235 memcpy((char *)dest_cpy, (char *)*p_argv, size_al);
236 next_arg += (size_al + 3) / 4;
241 case FFI_TYPE_SINT32:
242 gprvalue = *(signed int *) *p_argv;
245 case FFI_TYPE_UINT32:
246 gprvalue = *(unsigned int *) *p_argv;
248 *next_arg++ = gprvalue;
255 /* Check that we didn't overrun the stack... */
256 //FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS);
257 //FFI_ASSERT((unsigned *)fpr_base
258 // <= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
259 //FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
262 /* Adjust the size of S to be correct for Darwin.
263 On Darwin, the first field of a structure has natural alignment. */
266 darwin_adjust_aggregate_sizes (ffi_type *s)
270 if (s->type != FFI_TYPE_STRUCT)
274 for (i = 0; s->elements[i] != NULL; i++)
280 darwin_adjust_aggregate_sizes (p);
282 && (p->type == FFI_TYPE_UINT64
283 || p->type == FFI_TYPE_SINT64
284 || p->type == FFI_TYPE_DOUBLE
285 || p->alignment == 8))
287 else if (p->alignment == 16 || p->alignment < 4)
288 align = p->alignment;
291 s->size = ALIGN(s->size, align) + p->size;
294 s->size = ALIGN(s->size, s->alignment);
296 if (s->elements[0]->type == FFI_TYPE_UINT64
297 || s->elements[0]->type == FFI_TYPE_SINT64
298 || s->elements[0]->type == FFI_TYPE_DOUBLE
299 || s->elements[0]->alignment == 8)
300 s->alignment = s->alignment > 8 ? s->alignment : 8;
301 /* Do not add additional tail padding. */
304 /* Perform machine dependent cif processing. */
305 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
307 /* All this is for the DARWIN ABI. */
311 int fparg_count = 0, intarg_count = 0;
313 unsigned size_al = 0;
315 /* All the machine-independent calculation of cif->bytes will be wrong.
316 All the calculation of structure sizes will also be wrong.
317 Redo the calculation for DARWIN. */
319 if (cif->abi == FFI_DARWIN)
321 darwin_adjust_aggregate_sizes (cif->rtype);
322 for (i = 0; i < cif->nargs; i++)
323 darwin_adjust_aggregate_sizes (cif->arg_types[i]);
326 /* Space for the frame pointer, callee's LR, CR, etc, and for
327 the asm's temp regs. */
329 bytes = (6 + ASM_NEEDS_REGISTERS) * sizeof(long);
331 /* Return value handling. The rules are as follows:
332 - 32-bit (or less) integer values are returned in gpr3;
333 - Structures of size <= 4 bytes also returned in gpr3;
334 - 64-bit integer values and structures between 5 and 8 bytes are returned
336 - Single/double FP values are returned in fpr1;
337 - Long double FP (if not equivalent to double) values are returned in
339 - Larger structures values are allocated space and a pointer is passed
340 as the first argument. */
341 switch (cif->rtype->type)
344 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
345 case FFI_TYPE_LONGDOUBLE:
346 flags |= FLAG_RETURNS_128BITS;
347 flags |= FLAG_RETURNS_FP;
351 case FFI_TYPE_DOUBLE:
352 flags |= FLAG_RETURNS_64BITS;
355 flags |= FLAG_RETURNS_FP;
358 case FFI_TYPE_UINT64:
359 case FFI_TYPE_SINT64:
361 case FFI_TYPE_POINTER:
363 flags |= FLAG_RETURNS_64BITS;
366 case FFI_TYPE_STRUCT:
367 flags |= FLAG_RETVAL_REFERENCE;
368 flags |= FLAG_RETURNS_NOTHING;
372 flags |= FLAG_RETURNS_NOTHING;
376 /* Returns 32-bit integer, or similar. Nothing to do here. */
380 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
381 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
382 goes on the stack. Structures are passed as a pointer to a copy of
383 the structure. Stuff on the stack needs to keep proper alignment. */
384 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
386 switch ((*ptr)->type)
389 case FFI_TYPE_DOUBLE:
391 /* If this FP arg is going on the stack, it must be
393 if (fparg_count > NUM_FPR_ARG_REGISTERS
394 && intarg_count%2 != 0)
398 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
400 case FFI_TYPE_LONGDOUBLE:
402 /* If this FP arg is going on the stack, it must be
404 if (fparg_count > NUM_FPR_ARG_REGISTERS
405 && intarg_count%2 != 0)
411 case FFI_TYPE_UINT64:
412 case FFI_TYPE_SINT64:
413 /* 'long long' arguments are passed as two words, but
414 either both words must fit in registers or both go
415 on the stack. If they go on the stack, they must
416 be 8-byte-aligned. */
417 if (intarg_count == NUM_GPR_ARG_REGISTERS-1
418 || (intarg_count >= NUM_GPR_ARG_REGISTERS && intarg_count%2 != 0))
423 case FFI_TYPE_STRUCT:
424 size_al = (*ptr)->size;
425 /* If the first member of the struct is a double, then align
426 the struct to double-word. */
427 if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE)
428 size_al = ALIGN((*ptr)->size, 8);
430 intarg_count += (size_al + 7) / 8;
432 intarg_count += (size_al + 3) / 4;
437 /* Everything else is passed as a 4-byte word in a GPR, either
438 the object itself or a pointer to it. */
444 if (fparg_count != 0)
445 flags |= FLAG_FP_ARGUMENTS;
447 /* Space for the FPR registers, if needed. */
448 if (fparg_count != 0)
449 bytes += NUM_FPR_ARG_REGISTERS * sizeof(double);
453 if ((intarg_count + fparg_count) > NUM_GPR_ARG_REGISTERS)
454 bytes += (intarg_count + fparg_count) * sizeof(long);
456 if ((intarg_count + 2 * fparg_count) > NUM_GPR_ARG_REGISTERS)
457 bytes += (intarg_count + 2 * fparg_count) * sizeof(long);
460 bytes += NUM_GPR_ARG_REGISTERS * sizeof(long);
462 /* The stack space allocated needs to be a multiple of 16 bytes. */
463 bytes = (bytes + 15) & ~0xF;
471 extern void ffi_call_AIX(extended_cif *, long, unsigned, unsigned *,
472 void (*fn)(void), void (*fn2)(void));
473 extern void ffi_call_DARWIN(extended_cif *, long, unsigned, unsigned *,
474 void (*fn)(void), void (*fn2)(void));
476 void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
481 ecif.avalue = avalue;
483 /* If the return value is a struct and we don't have a return
484 value address then we need to make one. */
486 if ((rvalue == NULL) &&
487 (cif->rtype->type == FFI_TYPE_STRUCT))
489 ecif.rvalue = alloca(cif->rtype->size);
492 ecif.rvalue = rvalue;
497 ffi_call_AIX(&ecif, -(long)cif->bytes, cif->flags, ecif.rvalue, fn,
501 ffi_call_DARWIN(&ecif, -(long)cif->bytes, cif->flags, ecif.rvalue, fn,
510 static void flush_icache(char *);
511 static void flush_range(char *, int);
513 /* The layout of a function descriptor. A C function pointer really
514 points to one of these. */
516 typedef struct aix_fd_struct {
521 /* here I'd like to add the stack frame layout we use in darwin_closure.S
524 SP previous -> +---------------------------------------+ <--- child frame
525 | back chain to caller 4 |
526 +---------------------------------------+ 4
528 +---------------------------------------+ 8
530 +---------------------------------------+ 12
531 | reserved for compilers 4 |
532 +---------------------------------------+ 16
533 | reserved for binders 4 |
534 +---------------------------------------+ 20
535 | saved TOC pointer 4 |
536 +---------------------------------------+ 24
537 | always reserved 8*4=32 (previous GPRs)|
538 | according to the linkage convention |
540 +---------------------------------------+ 56
541 | our FPR area 13*8=104 |
545 +---------------------------------------+ 160
547 +---------------------------------------+ 168
548 | alignement to the next multiple of 16 |
549 SP current --> +---------------------------------------+ 176 <- parent frame
550 | back chain to caller 4 |
551 +---------------------------------------+ 180
553 +---------------------------------------+ 184
555 +---------------------------------------+ 188
556 | reserved for compilers 4 |
557 +---------------------------------------+ 192
558 | reserved for binders 4 |
559 +---------------------------------------+ 196
560 | saved TOC pointer 4 |
561 +---------------------------------------+ 200
562 | always reserved 8*4=32 we store our |
567 +---------------------------------------+ 232
569 +---------------------------------------+ xxx
571 +---------------------------------------+ xxx
575 ffi_prep_closure_loc (ffi_closure* closure,
577 void (*fun)(ffi_cif*, void*, void**, void*),
582 struct ffi_aix_trampoline_struct *tramp_aix;
589 FFI_ASSERT (cif->abi == FFI_DARWIN);
591 tramp = (unsigned int *) &closure->tramp[0];
592 tramp[0] = 0x7c0802a6; /* mflr r0 */
593 tramp[1] = 0x429f000d; /* bcl- 20,4*cr7+so,0x10 */
594 tramp[4] = 0x7d6802a6; /* mflr r11 */
595 tramp[5] = 0x818b0000; /* lwz r12,0(r11) function address */
596 tramp[6] = 0x7c0803a6; /* mtlr r0 */
597 tramp[7] = 0x7d8903a6; /* mtctr r12 */
598 tramp[8] = 0x816b0004; /* lwz r11,4(r11) static chain */
599 tramp[9] = 0x4e800420; /* bctr */
600 tramp[2] = (unsigned long) ffi_closure_ASM; /* function */
601 tramp[3] = (unsigned long) codeloc; /* context */
605 closure->user_data = user_data;
607 /* Flush the icache. Only necessary on Darwin. */
608 flush_range(codeloc, FFI_TRAMPOLINE_SIZE);
614 tramp_aix = (struct ffi_aix_trampoline_struct *) (closure->tramp);
615 fd = (aix_fd *)(void *)ffi_closure_ASM;
617 FFI_ASSERT (cif->abi == FFI_AIX);
619 tramp_aix->code_pointer = fd->code_pointer;
620 tramp_aix->toc = fd->toc;
621 tramp_aix->static_chain = codeloc;
624 closure->user_data = user_data;
635 flush_icache(char *addr)
644 : : "r"(addr) : "memory");
649 flush_range(char * addr1, int size)
651 #define MIN_LINE_SIZE 32
653 for (i = 0; i < size; i += MIN_LINE_SIZE)
654 flush_icache(addr1+i);
655 flush_icache(addr1+size-1);
664 int ffi_closure_helper_DARWIN (ffi_closure*, void*,
665 unsigned long*, ffi_dblfl*);
667 /* Basically the trampoline invokes ffi_closure_ASM, and on
668 entry, r11 holds the address of the closure.
669 After storing the registers that could possibly contain
670 parameters to be passed into the stack frame and setting
671 up space for a return value, ffi_closure_ASM invokes the
672 following helper function to do most of the work. */
674 int ffi_closure_helper_DARWIN (ffi_closure* closure, void * rvalue,
675 unsigned long * pgr, ffi_dblfl * pfr)
677 /* rvalue is the pointer to space for return value in closure assembly
678 pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM
679 pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM. */
681 typedef double ldbits[2];
690 ffi_type ** arg_types;
693 ffi_dblfl *end_pfr = pfr + NUM_FPR_ARG_REGISTERS;
697 avalue = alloca(cif->nargs * sizeof(void *));
699 /* Copy the caller's structure return value address so that the closure
700 returns the data directly to the caller. */
701 if (cif->rtype->type == FFI_TYPE_STRUCT)
703 rvalue = (void *) *pgr;
709 arg_types = cif->arg_types;
711 /* Grab the addresses of the arguments from the stack frame. */
714 switch (arg_types[i]->type)
719 avalue[i] = (char *) pgr + 7;
721 avalue[i] = (char *) pgr + 3;
726 case FFI_TYPE_SINT16:
727 case FFI_TYPE_UINT16:
729 avalue[i] = (char *) pgr + 6;
731 avalue[i] = (char *) pgr + 2;
736 case FFI_TYPE_SINT32:
737 case FFI_TYPE_UINT32:
739 avalue[i] = (char *) pgr + 4;
741 case FFI_TYPE_POINTER:
747 case FFI_TYPE_STRUCT:
749 size_al = arg_types[i]->size;
750 if (arg_types[i]->elements[0]->type == FFI_TYPE_DOUBLE)
751 size_al = ALIGN (arg_types[i]->size, 8);
752 if (size_al < 3 && cif->abi == FFI_DARWIN)
753 avalue[i] = (void *) pgr + 8 - size_al;
755 avalue[i] = (void *) pgr;
756 pgr += (size_al + 7) / 8;
758 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
759 SI 4 bytes) are aligned as if they were those modes. */
760 size_al = arg_types[i]->size;
761 /* If the first member of the struct is a double, then align
762 the struct to double-word. */
763 if (arg_types[i]->elements[0]->type == FFI_TYPE_DOUBLE)
764 size_al = ALIGN(arg_types[i]->size, 8);
765 if (size_al < 3 && cif->abi == FFI_DARWIN)
766 avalue[i] = (void*) pgr + 4 - size_al;
768 avalue[i] = (void*) pgr;
769 pgr += (size_al + 3) / 4;
773 case FFI_TYPE_SINT64:
774 case FFI_TYPE_UINT64:
776 case FFI_TYPE_POINTER:
781 /* Long long ints are passed in two gpr's. */
788 /* A float value consumes a GPR.
789 There are 13 64bit floating point registers. */
792 double temp = pfr->d;
793 pfr->f = (float) temp;
804 case FFI_TYPE_DOUBLE:
805 /* A double value consumes two GPRs.
806 There are 13 64bit floating point registers. */
823 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
825 case FFI_TYPE_LONGDOUBLE:
827 if (pfr + 1 < end_pfr)
836 *pgr = *(unsigned long *) pfr;
842 #else /* POWERPC64 */
843 /* A long double value consumes four GPRs and two FPRs.
844 There are 13 64bit floating point registers. */
845 if (pfr + 1 < end_pfr)
850 /* Here we have the situation where one part of the long double
851 is stored in fpr13 and the other part is already on the stack.
852 We use a union to pass the long double to avalue[i]. */
853 else if (pfr + 1 == end_pfr)
856 memcpy (&temp_ld.lb[0], pfr, sizeof(ldbits));
857 memcpy (&temp_ld.lb[1], pgr + 2, sizeof(ldbits));
858 avalue[i] = &temp_ld.ld;
865 #endif /* POWERPC64 */
874 (closure->fun) (cif, rvalue, avalue, closure->user_data);
876 /* Tell ffi_closure_ASM to perform return type promotions. */
877 return cif->rtype->type;