1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1998 Geoffrey Keating
4 PowerPC Foreign Function Interface
6 Darwin ABI support (c) 2001 John Hornkvist
7 AIX ABI support (c) 2002 Free Software Foundation, Inc.
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 ``Software''), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
17 The above copyright notice and this permission notice shall be included
18 in all copies or substantial portions of the Software.
20 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 OTHER DEALINGS IN THE SOFTWARE.
27 ----------------------------------------------------------------------- */
30 #include <ffi_common.h>
34 extern void ffi_closure_ASM(void);
37 /* The assembly depends on these exact flags. */
38 FLAG_RETURNS_NOTHING = 1 << (31-30), /* These go in cr7 */
39 FLAG_RETURNS_FP = 1 << (31-29),
40 FLAG_RETURNS_64BITS = 1 << (31-28),
42 FLAG_ARG_NEEDS_COPY = 1 << (31- 7),
43 FLAG_FP_ARGUMENTS = 1 << (31- 6), /* cr1.eq; specified by ABI */
44 FLAG_4_GPR_ARGUMENTS = 1 << (31- 5),
45 FLAG_RETVAL_REFERENCE = 1 << (31- 4)
48 /* About the DARWIN ABI. */
50 NUM_GPR_ARG_REGISTERS = 8,
51 NUM_FPR_ARG_REGISTERS = 13
53 enum { ASM_NEEDS_REGISTERS = 4 };
55 /* ffi_prep_args is called by the assembly routine once stack space
56 has been allocated for the function's arguments.
58 The stack layout we want looks like this:
60 | Return address from ffi_call_DARWIN | higher addresses
61 |--------------------------------------------|
62 | Previous backchain pointer 4 | stack pointer here
63 |--------------------------------------------|<+ <<< on entry to
64 | Saved r28-r31 4*4 | | ffi_call_DARWIN
65 |--------------------------------------------| |
66 | Parameters (at least 8*4=32) | |
67 |--------------------------------------------| |
68 | Space for GPR2 4 | |
69 |--------------------------------------------| | stack |
70 | Reserved 2*4 | | grows |
71 |--------------------------------------------| | down V
72 | Space for callee's LR 4 | |
73 |--------------------------------------------| | lower addresses
75 |--------------------------------------------| | stack pointer here
76 | Current backchain pointer 4 |-/ during
77 |--------------------------------------------| <<< ffi_call_DARWIN
82 void ffi_prep_args(extended_cif *ecif, unsigned *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 *const stacktop = stack + (ecif->cif->bytes / sizeof(unsigned));
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 *next_arg = stack + 6; /* 6 reserved posistions. */
100 int i = ecif->cif->nargs;
102 void **p_argv = ecif->avalue;
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)(char *)ecif->rvalue;
120 /* Now for the arguments. */
123 i--, ptr++, p_argv++)
125 switch ((*ptr)->type)
127 /* If a floating-point parameter appears before all of the general-
128 purpose registers are filled, the corresponding GPRs that match
129 the size of the floating-point parameter are skipped. */
131 double_tmp = *(float *)*p_argv;
132 if (fparg_count >= NUM_FPR_ARG_REGISTERS)
133 *(double *)next_arg = double_tmp;
135 *fpr_base++ = double_tmp;
138 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;
148 FFI_ASSERT(flags & FLAG_FP_ARGUMENTS);
151 case FFI_TYPE_UINT64:
152 case FFI_TYPE_SINT64:
153 *(long long *)next_arg = *(long long *)*p_argv;
157 gprvalue = *(unsigned char *)*p_argv;
160 gprvalue = *(signed char *)*p_argv;
162 case FFI_TYPE_UINT16:
163 gprvalue = *(unsigned short *)*p_argv;
165 case FFI_TYPE_SINT16:
166 gprvalue = *(signed short *)*p_argv;
169 case FFI_TYPE_STRUCT:
171 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
172 case FFI_TYPE_LONGDOUBLE:
174 dest_cpy = (char *) next_arg;
176 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
177 SI 4 bytes) are aligned as if they were those modes.
178 Structures with 3 byte in size are padded upwards. */
179 size_al = (*ptr)->size;
180 /* If the first member of the struct is a double, then align
181 the struct to double-word.
182 Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3. */
183 if ((*ptr)->elements[0]->type == 3)
184 size_al = ALIGN((*ptr)->size, 8);
185 if (size_al < 3 && ecif->cif->abi == FFI_DARWIN)
186 dest_cpy += 4 - size_al;
188 memcpy((char *)dest_cpy, (char *)*p_argv, size_al);
189 next_arg += (size_al + 3) / 4;
193 case FFI_TYPE_UINT32:
194 case FFI_TYPE_SINT32:
195 case FFI_TYPE_POINTER:
196 gprvalue = *(unsigned *)*p_argv;
198 *next_arg++ = gprvalue;
205 /* Check that we didn't overrun the stack... */
206 //FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS);
207 //FFI_ASSERT((unsigned *)fpr_base
208 // <= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
209 //FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
212 /* Perform machine dependent cif processing. */
213 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
215 /* All this is for the DARWIN ABI. */
219 int fparg_count = 0, intarg_count = 0;
221 unsigned size_al = 0;
223 /* All the machine-independent calculation of cif->bytes will be wrong.
224 Redo the calculation for DARWIN. */
226 /* Space for the frame pointer, callee's LR, CR, etc, and for
227 the asm's temp regs. */
229 bytes = (6 + ASM_NEEDS_REGISTERS) * sizeof(long);
231 /* Return value handling. The rules are as follows:
232 - 32-bit (or less) integer values are returned in gpr3;
233 - Structures of size <= 4 bytes also returned in gpr3;
234 - 64-bit integer values and structures between 5 and 8 bytes are returned
236 - Single/double FP values are returned in fpr1;
237 - Long double FP (if not equivalent to double) values are returned in
239 - Larger structures values are allocated space and a pointer is passed
240 as the first argument. */
241 switch (cif->rtype->type)
243 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
244 case FFI_TYPE_LONGDOUBLE:
247 case FFI_TYPE_DOUBLE:
248 flags |= FLAG_RETURNS_64BITS;
251 flags |= FLAG_RETURNS_FP;
254 case FFI_TYPE_UINT64:
255 case FFI_TYPE_SINT64:
256 flags |= FLAG_RETURNS_64BITS;
259 case FFI_TYPE_STRUCT:
260 flags |= FLAG_RETVAL_REFERENCE;
261 flags |= FLAG_RETURNS_NOTHING;
265 flags |= FLAG_RETURNS_NOTHING;
269 /* Returns 32-bit integer, or similar. Nothing to do here. */
273 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
274 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
275 goes on the stack. Structures and long doubles (if not equivalent
276 to double) are passed as a pointer to a copy of the structure.
277 Stuff on the stack needs to keep proper alignment. */
278 for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++)
280 switch ((*ptr)->type)
283 case FFI_TYPE_DOUBLE:
285 /* If this FP arg is going on the stack, it must be
287 if (fparg_count > NUM_FPR_ARG_REGISTERS
288 && intarg_count%2 != 0)
292 case FFI_TYPE_UINT64:
293 case FFI_TYPE_SINT64:
294 /* 'long long' arguments are passed as two words, but
295 either both words must fit in registers or both go
296 on the stack. If they go on the stack, they must
297 be 8-byte-aligned. */
298 if (intarg_count == NUM_GPR_ARG_REGISTERS-1
299 || (intarg_count >= NUM_GPR_ARG_REGISTERS && intarg_count%2 != 0))
304 case FFI_TYPE_STRUCT:
305 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
306 case FFI_TYPE_LONGDOUBLE:
308 size_al = (*ptr)->size;
309 /* If the first member of the struct is a double, then align
310 the struct to double-word.
311 Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3. */
312 if ((*ptr)->elements[0]->type == 3)
313 size_al = ALIGN((*ptr)->size, 8);
314 intarg_count += (size_al + 3) / 4;
318 /* Everything else is passed as a 4-byte word in a GPR, either
319 the object itself or a pointer to it. */
325 if (fparg_count != 0)
326 flags |= FLAG_FP_ARGUMENTS;
328 /* Space for the FPR registers, if needed. */
329 if (fparg_count != 0)
330 bytes += NUM_FPR_ARG_REGISTERS * sizeof(double);
333 if ((intarg_count + 2 * fparg_count) > NUM_GPR_ARG_REGISTERS)
334 bytes += (intarg_count + 2 * fparg_count) * sizeof(long);
336 bytes += NUM_GPR_ARG_REGISTERS * sizeof(long);
338 /* The stack space allocated needs to be a multiple of 16 bytes. */
339 bytes = (bytes + 15) & ~0xF;
349 extern void ffi_call_AIX(/*@out@*/ extended_cif *,
351 /*@out@*/ unsigned *,
354 extern void ffi_call_DARWIN(/*@out@*/ extended_cif *,
356 /*@out@*/ unsigned *,
362 void ffi_call(/*@dependent@*/ ffi_cif *cif,
364 /*@out@*/ void *rvalue,
365 /*@dependent@*/ void **avalue)
370 ecif.avalue = avalue;
372 /* If the return value is a struct and we don't have a return
373 value address then we need to make one. */
375 if ((rvalue == NULL) &&
376 (cif->rtype->type == FFI_TYPE_STRUCT))
379 ecif.rvalue = alloca(cif->rtype->size);
383 ecif.rvalue = rvalue;
389 ffi_call_AIX(&ecif, -cif->bytes,
390 cif->flags, ecif.rvalue, fn, ffi_prep_args);
395 ffi_call_DARWIN(&ecif, -cif->bytes,
396 cif->flags, ecif.rvalue, fn, ffi_prep_args);
405 static void flush_icache(char *);
406 static void flush_range(char *, int);
408 /* The layout of a function descriptor. A C function pointer really
409 points to one of these. */
411 typedef struct aix_fd_struct {
416 /* here I'd like to add the stack frame layout we use in darwin_closure.S
419 SP previous -> +---------------------------------------+ <--- child frame
420 | back chain to caller 4 |
421 +---------------------------------------+ 4
423 +---------------------------------------+ 8
425 +---------------------------------------+ 12
426 | reserved for compilers 4 |
427 +---------------------------------------+ 16
428 | reserved for binders 4 |
429 +---------------------------------------+ 20
430 | saved TOC pointer 4 |
431 +---------------------------------------+ 24
432 | always reserved 8*4=32 (previous GPRs)|
433 | according to the linkage convention |
435 +---------------------------------------+ 56
436 | our FPR area 13*8=104 |
440 +---------------------------------------+ 160
442 +---------------------------------------+ 168
443 | alignement to the next multiple of 16 |
444 SP current --> +---------------------------------------+ 176 <- parent frame
445 | back chain to caller 4 |
446 +---------------------------------------+ 180
448 +---------------------------------------+ 184
450 +---------------------------------------+ 188
451 | reserved for compilers 4 |
452 +---------------------------------------+ 192
453 | reserved for binders 4 |
454 +---------------------------------------+ 196
455 | saved TOC pointer 4 |
456 +---------------------------------------+ 200
457 | always reserved 8*4=32 we store our |
462 +---------------------------------------+ 232
464 +---------------------------------------+ xxx
466 +---------------------------------------+ xxx
470 ffi_prep_closure (ffi_closure* closure,
472 void (*fun)(ffi_cif*, void*, void**, void*),
476 struct ffi_aix_trampoline_struct *tramp_aix;
483 FFI_ASSERT (cif->abi == FFI_DARWIN);
485 tramp = (unsigned int *) &closure->tramp[0];
486 tramp[0] = 0x7c0802a6; /* mflr r0 */
487 tramp[1] = 0x429f000d; /* bcl- 20,4*cr7+so,0x10 */
488 tramp[4] = 0x7d6802a6; /* mflr r11 */
489 tramp[5] = 0x818b0000; /* lwz r12,0(r11) function address */
490 tramp[6] = 0x7c0803a6; /* mtlr r0 */
491 tramp[7] = 0x7d8903a6; /* mtctr r12 */
492 tramp[8] = 0x816b0004; /* lwz r11,4(r11) static chain */
493 tramp[9] = 0x4e800420; /* bctr */
494 tramp[2] = (unsigned long) ffi_closure_ASM; /* function */
495 tramp[3] = (unsigned long) closure; /* context */
499 closure->user_data = user_data;
501 /* Flush the icache. Only necessary on Darwin. */
502 flush_range(&closure->tramp[0],FFI_TRAMPOLINE_SIZE);
508 tramp_aix = (struct ffi_aix_trampoline_struct *) (closure->tramp);
509 fd = (aix_fd *)(void *)ffi_closure_ASM;
511 FFI_ASSERT (cif->abi == FFI_AIX);
513 tramp_aix->code_pointer = fd->code_pointer;
514 tramp_aix->toc = fd->toc;
515 tramp_aix->static_chain = closure;
518 closure->user_data = user_data;
529 flush_icache(char *addr)
538 : : "r"(addr) : "memory");
543 flush_range(char * addr1, int size)
545 #define MIN_LINE_SIZE 32
547 for (i = 0; i < size; i += MIN_LINE_SIZE)
548 flush_icache(addr1+i);
549 flush_icache(addr1+size-1);
558 int ffi_closure_helper_DARWIN (ffi_closure*, void*,
559 unsigned long*, ffi_dblfl*);
561 /* Basically the trampoline invokes ffi_closure_ASM, and on
562 entry, r11 holds the address of the closure.
563 After storing the registers that could possibly contain
564 parameters to be passed into the stack frame and setting
565 up space for a return value, ffi_closure_ASM invokes the
566 following helper function to do most of the work. */
568 int ffi_closure_helper_DARWIN (ffi_closure* closure, void * rvalue,
569 unsigned long * pgr, ffi_dblfl * pfr)
571 /* rvalue is the pointer to space for return value in closure assembly
572 pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM
573 pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM. */
577 ffi_type ** arg_types;
579 long nf; /* number of floating registers already used. */
580 long ng; /* number of general registers already used. */
586 avalue = alloca(cif->nargs * sizeof(void *));
591 /* Copy the caller's structure return value address so that the closure
592 returns the data directly to the caller. */
593 if (cif->rtype->type == FFI_TYPE_STRUCT)
595 rvalue = (void *) *pgr;
602 arg_types = cif->arg_types;
604 /* Grab the addresses of the arguments from the stack frame. */
607 switch (arg_types[i]->type)
611 avalue[i] = (char *) pgr + 3;
616 case FFI_TYPE_SINT16:
617 case FFI_TYPE_UINT16:
618 avalue[i] = (char *) pgr + 2;
623 case FFI_TYPE_SINT32:
624 case FFI_TYPE_UINT32:
625 case FFI_TYPE_POINTER:
631 case FFI_TYPE_STRUCT:
632 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
633 SI 4 bytes) are aligned as if they were those modes. */
634 size_al = arg_types[i]->size;
635 /* If the first member of the struct is a double, then align
636 the struct to double-word.
637 Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3. */
638 if (arg_types[i]->elements[0]->type == 3)
639 size_al = ALIGN(arg_types[i]->size, 8);
640 if (size_al < 3 && cif->abi == FFI_DARWIN)
641 avalue[i] = (void*) pgr + 4 - size_al;
643 avalue[i] = (void*) pgr;
644 ng += (size_al + 3) / 4;
645 pgr += (size_al + 3) / 4;
648 case FFI_TYPE_SINT64:
649 case FFI_TYPE_UINT64:
650 /* Long long ints are passed in two gpr's. */
657 /* A float value consumes a GPR.
658 There are 13 64bit floating point registers. */
659 if (nf < NUM_FPR_ARG_REGISTERS)
662 pfr->f = (float)temp;
675 case FFI_TYPE_DOUBLE:
676 /* A double value consumes two GPRs.
677 There are 13 64bit floating point registers. */
678 if (nf < NUM_FPR_ARG_REGISTERS)
698 (closure->fun) (cif, rvalue, avalue, closure->user_data);
700 /* Tell ffi_closure_ASM to perform return type promotions. */
701 return cif->rtype->type;