1
/* -----------------------------------------------------------------------
2
ffi.c - Copyright (c) 1998, 2008 Red Hat, Inc.
4
ARM Foreign Function Interface
6
Permission is hereby granted, free of charge, to any person obtaining
7
a copy of this software and associated documentation files (the
8
``Software''), to deal in the Software without restriction, including
9
without limitation the rights to use, copy, modify, merge, publish,
10
distribute, sublicense, and/or sell copies of the Software, and to
11
permit persons to whom the Software is furnished to do so, subject to
12
the following conditions:
14
The above copyright notice and this permission notice shall be included
15
in all copies or substantial portions of the Software.
17
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
18
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
DEALINGS IN THE SOFTWARE.
25
----------------------------------------------------------------------- */
28
#include <ffi_common.h>
32
/* ffi_prep_args is called by the assembly routine once stack space
33
has been allocated for the function's arguments */
35
void ffi_prep_args(char *stack, extended_cif *ecif)
37
register unsigned int i;
38
register void **p_argv;
40
register ffi_type **p_arg;
44
if ( ecif->cif->flags == FFI_TYPE_STRUCT ) {
45
*(void **) argp = ecif->rvalue;
49
p_argv = ecif->avalue;
51
for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
57
/* Align if necessary */
58
if (((*p_arg)->alignment - 1) & (unsigned) argp) {
59
argp = (char *) ALIGN(argp, (*p_arg)->alignment);
62
if ((*p_arg)->type == FFI_TYPE_STRUCT)
63
argp = (char *) ALIGN(argp, 4);
69
switch ((*p_arg)->type)
72
*(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
76
*(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
80
*(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
84
*(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
88
memcpy(argp, *p_argv, (*p_arg)->size);
95
else if (z == sizeof(int))
97
*(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
101
memcpy(argp, *p_argv, z);
110
/* Perform machine dependent cif processing */
111
ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
113
/* Round the stack up to a multiple of 8 bytes. This isn't needed
114
everywhere, but it is on some platforms, and it doesn't harm anything
115
when it isn't needed. */
116
cif->bytes = (cif->bytes + 7) & ~7;
118
/* Set the return type flag */
119
switch (cif->rtype->type)
123
case FFI_TYPE_DOUBLE:
124
cif->flags = (unsigned) cif->rtype->type;
127
case FFI_TYPE_SINT64:
128
case FFI_TYPE_UINT64:
129
cif->flags = (unsigned) FFI_TYPE_SINT64;
132
case FFI_TYPE_STRUCT:
133
if (cif->rtype->size <= 4)
134
/* A Composite Type not larger than 4 bytes is returned in r0. */
135
cif->flags = (unsigned)FFI_TYPE_INT;
137
/* A Composite Type larger than 4 bytes, or whose size cannot
138
be determined statically ... is stored in memory at an
139
address passed [in r0]. */
140
cif->flags = (unsigned)FFI_TYPE_STRUCT;
144
cif->flags = FFI_TYPE_INT;
151
extern void ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *,
152
unsigned, unsigned, unsigned *, void (*fn)(void));
154
void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
158
int small_struct = (cif->flags == FFI_TYPE_INT
159
&& cif->rtype->type == FFI_TYPE_STRUCT);
162
ecif.avalue = avalue;
166
/* If the return value is a struct and we don't have a return */
167
/* value address then we need to make one */
169
if ((rvalue == NULL) &&
170
(cif->flags == FFI_TYPE_STRUCT))
172
ecif.rvalue = alloca(cif->rtype->size);
174
else if (small_struct)
177
ecif.rvalue = rvalue;
182
ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, cif->flags, ecif.rvalue,
191
memcpy (rvalue, &temp, cif->rtype->size);
194
/** private members **/
196
static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
197
void** args, ffi_cif* cif);
199
void ffi_closure_SYSV (ffi_closure *);
201
/* This function is jumped to by the trampoline */
204
ffi_closure_SYSV_inner (closure, respp, args)
205
ffi_closure *closure;
209
// our various things...
214
arg_area = (void**) alloca (cif->nargs * sizeof (void*));
216
/* this call will initialize ARG_AREA, such that each
217
* element in that array points to the corresponding
218
* value on the stack; and if the function returns
219
* a structure, it will re-set RESP to point to the
220
* structure return address. */
222
ffi_prep_incoming_args_SYSV(args, respp, arg_area, cif);
224
(closure->fun) (cif, *respp, arg_area, closure->user_data);
231
ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
232
void **avalue, ffi_cif *cif)
235
register unsigned int i;
236
register void **p_argv;
238
register ffi_type **p_arg;
242
if ( cif->flags == FFI_TYPE_STRUCT ) {
243
*rvalue = *(void **) argp;
249
for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
253
size_t alignment = (*p_arg)->alignment;
256
/* Align if necessary */
257
if ((alignment - 1) & (unsigned) argp) {
258
argp = (char *) ALIGN(argp, alignment);
263
/* because we're little endian, this is what it turns into. */
265
*p_argv = (void*) argp;
274
/* How to make a trampoline. */
276
#define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
277
({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
278
unsigned int __fun = (unsigned int)(FUN); \
279
unsigned int __ctx = (unsigned int)(CTX); \
280
*(unsigned int*) &__tramp[0] = 0xe92d000f; /* stmfd sp!, {r0-r3} */ \
281
*(unsigned int*) &__tramp[4] = 0xe59f0000; /* ldr r0, [pc] */ \
282
*(unsigned int*) &__tramp[8] = 0xe59ff000; /* ldr pc, [pc] */ \
283
*(unsigned int*) &__tramp[12] = __ctx; \
284
*(unsigned int*) &__tramp[16] = __fun; \
285
__clear_cache((&__tramp[0]), (&__tramp[19])); \
289
/* the cif must already be prep'ed */
292
ffi_prep_closure_loc (ffi_closure* closure,
294
void (*fun)(ffi_cif*,void*,void**,void*),
298
FFI_ASSERT (cif->abi == FFI_SYSV);
300
FFI_INIT_TRAMPOLINE (&closure->tramp[0], \
305
closure->user_data = user_data;