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),
41 FLAG_RETURNS_128BITS
= 1 << (31-31),
43 FLAG_ARG_NEEDS_COPY
= 1 << (31- 7),
44 FLAG_FP_ARGUMENTS
= 1 << (31- 6), /* cr1.eq; specified by ABI */
45 FLAG_4_GPR_ARGUMENTS
= 1 << (31- 5),
46 FLAG_RETVAL_REFERENCE
= 1 << (31- 4)
49 /* About the DARWIN ABI. */
51 NUM_GPR_ARG_REGISTERS
= 8,
52 NUM_FPR_ARG_REGISTERS
= 13
54 enum { ASM_NEEDS_REGISTERS
= 4 };
56 /* ffi_prep_args is called by the assembly routine once stack space
57 has been allocated for the function's arguments.
59 The stack layout we want looks like this:
61 | Return address from ffi_call_DARWIN | higher addresses
62 |--------------------------------------------|
63 | Previous backchain pointer 4 | stack pointer here
64 |--------------------------------------------|<+ <<< on entry to
65 | Saved r28-r31 4*4 | | ffi_call_DARWIN
66 |--------------------------------------------| |
67 | Parameters (at least 8*4=32) | |
68 |--------------------------------------------| |
69 | Space for GPR2 4 | |
70 |--------------------------------------------| | stack |
71 | Reserved 2*4 | | grows |
72 |--------------------------------------------| | down V
73 | Space for callee's LR 4 | |
74 |--------------------------------------------| | lower addresses
76 |--------------------------------------------| | stack pointer here
77 | Current backchain pointer 4 |-/ during
78 |--------------------------------------------| <<< ffi_call_DARWIN
83 void ffi_prep_args(extended_cif
*ecif
, unsigned *const stack
)
86 const unsigned bytes
= ecif
->cif
->bytes
;
87 const unsigned flags
= ecif
->cif
->flags
;
89 /* 'stacktop' points at the previous backchain pointer. */
90 unsigned *const stacktop
= stack
+ (bytes
/ sizeof(unsigned));
92 /* 'fpr_base' points at the space for fpr1, and grows upwards as
93 we use FPR registers. */
94 double *fpr_base
= (double*) (stacktop
- ASM_NEEDS_REGISTERS
) - NUM_FPR_ARG_REGISTERS
;
98 /* 'next_arg' grows up as we put parameters in it. */
99 unsigned *next_arg
= stack
+ 6; /* 6 reserved positions. */
101 int i
= ecif
->cif
->nargs
;
103 void **p_argv
= ecif
->avalue
;
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)(char *)ecif
->rvalue
;
121 /* Now for the arguments. */
124 i
--, ptr
++, p_argv
++)
126 switch ((*ptr
)->type
)
128 /* If a floating-point parameter appears before all of the general-
129 purpose registers are filled, the corresponding GPRs that match
130 the size of the floating-point parameter are skipped. */
132 double_tmp
= *(float *)*p_argv
;
133 if (fparg_count
>= NUM_FPR_ARG_REGISTERS
)
134 *(double *)next_arg
= double_tmp
;
136 *fpr_base
++ = double_tmp
;
139 FFI_ASSERT(flags
& FLAG_FP_ARGUMENTS
);
142 case FFI_TYPE_DOUBLE
:
143 double_tmp
= *(double *)*p_argv
;
144 if (fparg_count
>= NUM_FPR_ARG_REGISTERS
)
145 *(double *)next_arg
= double_tmp
;
147 *fpr_base
++ = double_tmp
;
150 FFI_ASSERT(flags
& FLAG_FP_ARGUMENTS
);
153 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
155 case FFI_TYPE_LONGDOUBLE
:
156 double_tmp
= ((double *)*p_argv
)[0];
157 if (fparg_count
>= NUM_FPR_ARG_REGISTERS
)
158 *(double *)next_arg
= double_tmp
;
160 *fpr_base
++ = double_tmp
;
163 double_tmp
= ((double *)*p_argv
)[1];
164 if (fparg_count
>= NUM_FPR_ARG_REGISTERS
)
165 *(double *)next_arg
= double_tmp
;
167 *fpr_base
++ = double_tmp
;
170 FFI_ASSERT(flags
& FLAG_FP_ARGUMENTS
);
173 case FFI_TYPE_UINT64
:
174 case FFI_TYPE_SINT64
:
175 *(long long *)next_arg
= *(long long *)*p_argv
;
179 gprvalue
= *(unsigned char *)*p_argv
;
182 gprvalue
= *(signed char *)*p_argv
;
184 case FFI_TYPE_UINT16
:
185 gprvalue
= *(unsigned short *)*p_argv
;
187 case FFI_TYPE_SINT16
:
188 gprvalue
= *(signed short *)*p_argv
;
191 case FFI_TYPE_STRUCT
:
192 dest_cpy
= (char *) next_arg
;
194 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
195 SI 4 bytes) are aligned as if they were those modes.
196 Structures with 3 byte in size are padded upwards. */
197 size_al
= (*ptr
)->size
;
198 /* If the first member of the struct is a double, then align
199 the struct to double-word.
200 Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3. */
201 if ((*ptr
)->elements
[0]->type
== 3)
202 size_al
= ALIGN((*ptr
)->size
, 8);
203 if (size_al
< 3 && ecif
->cif
->abi
== FFI_DARWIN
)
204 dest_cpy
+= 4 - size_al
;
206 memcpy((char *)dest_cpy
, (char *)*p_argv
, size_al
);
207 next_arg
+= (size_al
+ 3) / 4;
211 case FFI_TYPE_UINT32
:
212 case FFI_TYPE_SINT32
:
213 case FFI_TYPE_POINTER
:
214 gprvalue
= *(unsigned *)*p_argv
;
216 *next_arg
++ = gprvalue
;
223 /* Check that we didn't overrun the stack... */
224 //FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS);
225 //FFI_ASSERT((unsigned *)fpr_base
226 // <= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS);
227 //FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4);
230 /* Perform machine dependent cif processing. */
231 ffi_status
ffi_prep_cif_machdep(ffi_cif
*cif
)
233 /* All this is for the DARWIN ABI. */
237 int fparg_count
= 0, intarg_count
= 0;
239 unsigned size_al
= 0;
241 /* All the machine-independent calculation of cif->bytes will be wrong.
242 Redo the calculation for DARWIN. */
244 /* Space for the frame pointer, callee's LR, CR, etc, and for
245 the asm's temp regs. */
247 bytes
= (6 + ASM_NEEDS_REGISTERS
) * sizeof(long);
249 /* Return value handling. The rules are as follows:
250 - 32-bit (or less) integer values are returned in gpr3;
251 - Structures of size <= 4 bytes also returned in gpr3;
252 - 64-bit integer values and structures between 5 and 8 bytes are returned
254 - Single/double FP values are returned in fpr1;
255 - Long double FP (if not equivalent to double) values are returned in
257 - Larger structures values are allocated space and a pointer is passed
258 as the first argument. */
259 switch (cif
->rtype
->type
)
262 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
263 case FFI_TYPE_LONGDOUBLE
:
264 flags
|= FLAG_RETURNS_128BITS
;
265 flags
|= FLAG_RETURNS_FP
;
269 case FFI_TYPE_DOUBLE
:
270 flags
|= FLAG_RETURNS_64BITS
;
273 flags
|= FLAG_RETURNS_FP
;
276 case FFI_TYPE_UINT64
:
277 case FFI_TYPE_SINT64
:
278 flags
|= FLAG_RETURNS_64BITS
;
281 case FFI_TYPE_STRUCT
:
282 flags
|= FLAG_RETVAL_REFERENCE
;
283 flags
|= FLAG_RETURNS_NOTHING
;
287 flags
|= FLAG_RETURNS_NOTHING
;
291 /* Returns 32-bit integer, or similar. Nothing to do here. */
295 /* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the
296 first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest
297 goes on the stack. Structures are passed as a pointer to a copy of
298 the structure. Stuff on the stack needs to keep proper alignment. */
299 for (ptr
= cif
->arg_types
, i
= cif
->nargs
; i
> 0; i
--, ptr
++)
301 switch ((*ptr
)->type
)
304 case FFI_TYPE_DOUBLE
:
306 /* If this FP arg is going on the stack, it must be
308 if (fparg_count
> NUM_FPR_ARG_REGISTERS
309 && intarg_count
%2 != 0)
313 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
315 case FFI_TYPE_LONGDOUBLE
:
317 /* If this FP arg is going on the stack, it must be
319 if (fparg_count
> NUM_FPR_ARG_REGISTERS
320 && intarg_count
%2 != 0)
326 case FFI_TYPE_UINT64
:
327 case FFI_TYPE_SINT64
:
328 /* 'long long' arguments are passed as two words, but
329 either both words must fit in registers or both go
330 on the stack. If they go on the stack, they must
331 be 8-byte-aligned. */
332 if (intarg_count
== NUM_GPR_ARG_REGISTERS
-1
333 || (intarg_count
>= NUM_GPR_ARG_REGISTERS
&& intarg_count
%2 != 0))
338 case FFI_TYPE_STRUCT
:
339 size_al
= (*ptr
)->size
;
340 /* If the first member of the struct is a double, then align
341 the struct to double-word.
342 Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3. */
343 if ((*ptr
)->elements
[0]->type
== 3)
344 size_al
= ALIGN((*ptr
)->size
, 8);
345 intarg_count
+= (size_al
+ 3) / 4;
349 /* Everything else is passed as a 4-byte word in a GPR, either
350 the object itself or a pointer to it. */
356 if (fparg_count
!= 0)
357 flags
|= FLAG_FP_ARGUMENTS
;
359 /* Space for the FPR registers, if needed. */
360 if (fparg_count
!= 0)
361 bytes
+= NUM_FPR_ARG_REGISTERS
* sizeof(double);
364 if ((intarg_count
+ 2 * fparg_count
) > NUM_GPR_ARG_REGISTERS
)
365 bytes
+= (intarg_count
+ 2 * fparg_count
) * sizeof(long);
367 bytes
+= NUM_GPR_ARG_REGISTERS
* sizeof(long);
369 /* The stack space allocated needs to be a multiple of 16 bytes. */
370 bytes
= (bytes
+ 15) & ~0xF;
380 extern void ffi_call_AIX(/*@out@*/ extended_cif
*,
382 /*@out@*/ unsigned *,
385 extern void ffi_call_DARWIN(/*@out@*/ extended_cif
*,
387 /*@out@*/ unsigned *,
393 void ffi_call(/*@dependent@*/ ffi_cif
*cif
,
395 /*@out@*/ void *rvalue
,
396 /*@dependent@*/ void **avalue
)
401 ecif
.avalue
= avalue
;
403 /* If the return value is a struct and we don't have a return
404 value address then we need to make one. */
406 if ((rvalue
== NULL
) &&
407 (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
410 ecif
.rvalue
= alloca(cif
->rtype
->size
);
414 ecif
.rvalue
= rvalue
;
420 ffi_call_AIX(&ecif
, -cif
->bytes
,
421 cif
->flags
, ecif
.rvalue
, fn
, ffi_prep_args
);
426 ffi_call_DARWIN(&ecif
, -cif
->bytes
,
427 cif
->flags
, ecif
.rvalue
, fn
, ffi_prep_args
);
436 static void flush_icache(char *);
437 static void flush_range(char *, int);
439 /* The layout of a function descriptor. A C function pointer really
440 points to one of these. */
442 typedef struct aix_fd_struct
{
447 /* here I'd like to add the stack frame layout we use in darwin_closure.S
450 SP previous -> +---------------------------------------+ <--- child frame
451 | back chain to caller 4 |
452 +---------------------------------------+ 4
454 +---------------------------------------+ 8
456 +---------------------------------------+ 12
457 | reserved for compilers 4 |
458 +---------------------------------------+ 16
459 | reserved for binders 4 |
460 +---------------------------------------+ 20
461 | saved TOC pointer 4 |
462 +---------------------------------------+ 24
463 | always reserved 8*4=32 (previous GPRs)|
464 | according to the linkage convention |
466 +---------------------------------------+ 56
467 | our FPR area 13*8=104 |
471 +---------------------------------------+ 160
473 +---------------------------------------+ 168
474 | alignement to the next multiple of 16 |
475 SP current --> +---------------------------------------+ 176 <- parent frame
476 | back chain to caller 4 |
477 +---------------------------------------+ 180
479 +---------------------------------------+ 184
481 +---------------------------------------+ 188
482 | reserved for compilers 4 |
483 +---------------------------------------+ 192
484 | reserved for binders 4 |
485 +---------------------------------------+ 196
486 | saved TOC pointer 4 |
487 +---------------------------------------+ 200
488 | always reserved 8*4=32 we store our |
493 +---------------------------------------+ 232
495 +---------------------------------------+ xxx
497 +---------------------------------------+ xxx
501 ffi_prep_closure (ffi_closure
* closure
,
503 void (*fun
)(ffi_cif
*, void*, void**, void*),
507 struct ffi_aix_trampoline_struct
*tramp_aix
;
514 FFI_ASSERT (cif
->abi
== FFI_DARWIN
);
516 tramp
= (unsigned int *) &closure
->tramp
[0];
517 tramp
[0] = 0x7c0802a6; /* mflr r0 */
518 tramp
[1] = 0x429f000d; /* bcl- 20,4*cr7+so,0x10 */
519 tramp
[4] = 0x7d6802a6; /* mflr r11 */
520 tramp
[5] = 0x818b0000; /* lwz r12,0(r11) function address */
521 tramp
[6] = 0x7c0803a6; /* mtlr r0 */
522 tramp
[7] = 0x7d8903a6; /* mtctr r12 */
523 tramp
[8] = 0x816b0004; /* lwz r11,4(r11) static chain */
524 tramp
[9] = 0x4e800420; /* bctr */
525 tramp
[2] = (unsigned long) ffi_closure_ASM
; /* function */
526 tramp
[3] = (unsigned long) closure
; /* context */
530 closure
->user_data
= user_data
;
532 /* Flush the icache. Only necessary on Darwin. */
533 flush_range(&closure
->tramp
[0],FFI_TRAMPOLINE_SIZE
);
539 tramp_aix
= (struct ffi_aix_trampoline_struct
*) (closure
->tramp
);
540 fd
= (aix_fd
*)(void *)ffi_closure_ASM
;
542 FFI_ASSERT (cif
->abi
== FFI_AIX
);
544 tramp_aix
->code_pointer
= fd
->code_pointer
;
545 tramp_aix
->toc
= fd
->toc
;
546 tramp_aix
->static_chain
= closure
;
549 closure
->user_data
= user_data
;
560 flush_icache(char *addr
)
569 : : "r"(addr
) : "memory");
574 flush_range(char * addr1
, int size
)
576 #define MIN_LINE_SIZE 32
578 for (i
= 0; i
< size
; i
+= MIN_LINE_SIZE
)
579 flush_icache(addr1
+i
);
580 flush_icache(addr1
+size
-1);
589 int ffi_closure_helper_DARWIN (ffi_closure
*, void*,
590 unsigned long*, ffi_dblfl
*);
592 /* Basically the trampoline invokes ffi_closure_ASM, and on
593 entry, r11 holds the address of the closure.
594 After storing the registers that could possibly contain
595 parameters to be passed into the stack frame and setting
596 up space for a return value, ffi_closure_ASM invokes the
597 following helper function to do most of the work. */
599 int ffi_closure_helper_DARWIN (ffi_closure
* closure
, void * rvalue
,
600 unsigned long * pgr
, ffi_dblfl
* pfr
)
602 /* rvalue is the pointer to space for return value in closure assembly
603 pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM
604 pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM. */
606 typedef double ldbits
[2];
615 ffi_type
** arg_types
;
617 long nf
; /* number of floating registers already used. */
618 long ng
; /* number of general registers already used. */
625 avalue
= alloca(cif
->nargs
* sizeof(void *));
630 /* Copy the caller's structure return value address so that the closure
631 returns the data directly to the caller. */
632 if (cif
->rtype
->type
== FFI_TYPE_STRUCT
)
634 rvalue
= (void *) *pgr
;
641 arg_types
= cif
->arg_types
;
643 /* Grab the addresses of the arguments from the stack frame. */
646 switch (arg_types
[i
]->type
)
650 avalue
[i
] = (char *) pgr
+ 3;
655 case FFI_TYPE_SINT16
:
656 case FFI_TYPE_UINT16
:
657 avalue
[i
] = (char *) pgr
+ 2;
662 case FFI_TYPE_SINT32
:
663 case FFI_TYPE_UINT32
:
664 case FFI_TYPE_POINTER
:
670 case FFI_TYPE_STRUCT
:
671 /* Structures that match the basic modes (QI 1 byte, HI 2 bytes,
672 SI 4 bytes) are aligned as if they were those modes. */
673 size_al
= arg_types
[i
]->size
;
674 /* If the first member of the struct is a double, then align
675 the struct to double-word.
676 Type 3 is defined in include/ffi.h. #define FFI_TYPE_DOUBLE 3. */
677 if (arg_types
[i
]->elements
[0]->type
== 3)
678 size_al
= ALIGN(arg_types
[i
]->size
, 8);
679 if (size_al
< 3 && cif
->abi
== FFI_DARWIN
)
680 avalue
[i
] = (void*) pgr
+ 4 - size_al
;
682 avalue
[i
] = (void*) pgr
;
683 ng
+= (size_al
+ 3) / 4;
684 pgr
+= (size_al
+ 3) / 4;
687 case FFI_TYPE_SINT64
:
688 case FFI_TYPE_UINT64
:
689 /* Long long ints are passed in two gpr's. */
696 /* A float value consumes a GPR.
697 There are 13 64bit floating point registers. */
698 if (nf
< NUM_FPR_ARG_REGISTERS
)
701 pfr
->f
= (float)temp
;
714 case FFI_TYPE_DOUBLE
:
715 /* A double value consumes two GPRs.
716 There are 13 64bit floating point registers. */
717 if (nf
< NUM_FPR_ARG_REGISTERS
)
731 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
733 case FFI_TYPE_LONGDOUBLE
:
734 /* A long double value consumes four GPRs and two FPRs.
735 There are 13 64bit floating point registers. */
736 if (nf
< NUM_FPR_ARG_REGISTERS
- 1)
741 /* Here we have the situation where one part of the long double
742 is stored in fpr13 and the other part is already on the stack.
743 We use a union to pass the long double to avalue[i]. */
744 else if (nf
== NUM_FPR_ARG_REGISTERS
- 1)
746 memcpy (&temp_ld
.lb
[0], pfr
, sizeof(ldbits
));
747 memcpy (&temp_ld
.lb
[1], pgr
+ 2, sizeof(ldbits
));
748 avalue
[i
] = &temp_ld
.ld
;
765 (closure
->fun
) (cif
, rvalue
, avalue
, closure
->user_data
);
767 /* Tell ffi_closure_ASM to perform return type promotions. */
768 return cif
->rtype
->type
;