1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2009 Bradley Smith <brad@brad-smith.co.uk>
4 AVR32 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>
33 #include <asm/unistd.h>
37 extern void ffi_call_SYSV(void (*)(char *, extended_cif
*), extended_cif
*,
38 unsigned int, unsigned int, unsigned int*, unsigned int,
40 extern void ffi_closure_SYSV (ffi_closure
*);
42 unsigned int pass_struct_on_stack(ffi_type
*type
)
44 if(type
->type
!= FFI_TYPE_STRUCT
)
47 if(type
->alignment
< type
->size
&&
48 !(type
->size
== 4 || type
->size
== 8) &&
49 !(type
->size
== 8 && type
->alignment
>= 4))
52 if(type
->size
== 3 || type
->size
== 5 || type
->size
== 6 ||
59 /* ffi_prep_args is called by the assembly routine once stack space
60 * has been allocated for the function's arguments
62 * This is annoyingly complex since we need to keep track of used
66 void ffi_prep_args(char *stack
, extended_cif
*ecif
)
71 char *reg_base
= stack
;
72 char *stack_base
= stack
+ 20;
73 unsigned int stack_offset
= 0;
74 unsigned int reg_mask
= 0;
76 p_argv
= ecif
->avalue
;
78 /* If cif->flags is struct then we know it's not passed in registers */
79 if(ecif
->cif
->flags
== FFI_TYPE_STRUCT
)
81 *(void**)reg_base
= ecif
->rvalue
;
85 for(i
= 0, p_arg
= ecif
->cif
->arg_types
; i
< ecif
->cif
->nargs
;
88 size_t z
= (*p_arg
)->size
;
89 int alignment
= (*p_arg
)->alignment
;
90 int type
= (*p_arg
)->type
;
98 if(pass_struct_on_stack(*p_arg
))
100 addr
= stack_base
+ stack_offset
;
103 else if(z
== sizeof(int))
107 while((reg_mask
>> index
) & 1)
110 addr
= reg_base
+ (index
* 4);
111 reg_mask
|= (1 << index
);
113 else if(z
== 2 * sizeof(int))
115 if(!((reg_mask
>> 1) & 1))
118 reg_mask
|= (3 << 1);
120 else if(!((reg_mask
>> 3) & 1))
122 addr
= reg_base
+ 12;
123 reg_mask
|= (3 << 3);
130 addr
= stack_base
+ stack_offset
;
134 if(type
== FFI_TYPE_STRUCT
&& (*p_arg
)->elements
[1] == NULL
)
135 type
= (*p_arg
)->elements
[0]->type
;
140 *(unsigned int *)addr
= (unsigned int)*(UINT8
*)(*p_argv
);
143 *(signed int *)addr
= (signed int)*(SINT8
*)(*p_argv
);
145 case FFI_TYPE_UINT16
:
146 *(unsigned int *)addr
= (unsigned int)*(UINT16
*)(*p_argv
);
148 case FFI_TYPE_SINT16
:
149 *(signed int *)addr
= (signed int)*(SINT16
*)(*p_argv
);
152 memcpy(addr
, *p_argv
, z
);
160 for(i
= 0; i
< 5; i
++)
162 if((reg_mask
& (1 << i
)) == 0)
163 printf("r%d: (unused)\n", 12 - i
);
165 printf("r%d: 0x%08x\n", 12 - i
, ((unsigned int*)reg_base
)[i
]);
168 for(i
= 0; i
< stack_offset
/ 4; i
++)
170 printf("sp+%d: 0x%08x\n", i
*4, ((unsigned int*)stack_base
)[i
]);
175 /* Perform machine dependent cif processing */
176 ffi_status
ffi_prep_cif_machdep(ffi_cif
*cif
)
178 /* Round the stack up to a multiple of 8 bytes. This isn't needed
179 * everywhere, but it is on some platforms, and it doesn't harm
180 * anything when it isn't needed. */
181 cif
->bytes
= (cif
->bytes
+ 7) & ~7;
183 /* Flag to indicate that he return value is in fact a struct */
184 cif
->rstruct_flag
= 0;
186 /* Set the return type flag */
187 switch(cif
->rtype
->type
)
191 cif
->flags
= (unsigned)FFI_TYPE_UINT8
;
193 case FFI_TYPE_SINT16
:
194 case FFI_TYPE_UINT16
:
195 cif
->flags
= (unsigned)FFI_TYPE_UINT16
;
198 case FFI_TYPE_SINT32
:
199 case FFI_TYPE_UINT32
:
200 case FFI_TYPE_POINTER
:
201 cif
->flags
= (unsigned)FFI_TYPE_UINT32
;
203 case FFI_TYPE_DOUBLE
:
204 case FFI_TYPE_SINT64
:
205 case FFI_TYPE_UINT64
:
206 cif
->flags
= (unsigned)FFI_TYPE_UINT64
;
208 case FFI_TYPE_STRUCT
:
209 cif
->rstruct_flag
= 1;
210 if(!pass_struct_on_stack(cif
->rtype
))
212 if(cif
->rtype
->size
<= 1)
213 cif
->flags
= (unsigned)FFI_TYPE_UINT8
;
214 else if(cif
->rtype
->size
<= 2)
215 cif
->flags
= (unsigned)FFI_TYPE_UINT16
;
216 else if(cif
->rtype
->size
<= 4)
217 cif
->flags
= (unsigned)FFI_TYPE_UINT32
;
218 else if(cif
->rtype
->size
<= 8)
219 cif
->flags
= (unsigned)FFI_TYPE_UINT64
;
221 cif
->flags
= (unsigned)cif
->rtype
->type
;
224 cif
->flags
= (unsigned)cif
->rtype
->type
;
227 cif
->flags
= (unsigned)cif
->rtype
->type
;
234 void ffi_call(ffi_cif
*cif
, void (*fn
)(void), void *rvalue
, void **avalue
)
238 unsigned int size
= 0, i
= 0;
242 ecif
.avalue
= avalue
;
244 for(i
= 0, p_arg
= cif
->arg_types
; i
< cif
->nargs
; i
++, p_arg
++)
245 size
+= (*p_arg
)->size
+ (4 - (*p_arg
)->size
% 4);
247 /* If the return value is a struct and we don't have a return value
248 * address then we need to make one */
250 /* If cif->flags is struct then it's not suitable for registers */
251 if((rvalue
== NULL
) && (cif
->flags
== FFI_TYPE_STRUCT
))
252 ecif
.rvalue
= alloca(cif
->rtype
->size
);
254 ecif
.rvalue
= rvalue
;
259 ffi_call_SYSV(ffi_prep_args
, &ecif
, size
, cif
->flags
,
260 ecif
.rvalue
, cif
->rstruct_flag
, fn
);
268 static void ffi_prep_incoming_args_SYSV(char *stack
, void **rvalue
,
269 void **avalue
, ffi_cif
*cif
)
271 register unsigned int i
, reg_mask
= 0;
272 register void **p_argv
;
273 register ffi_type
**p_arg
;
274 register char *reg_base
= stack
;
275 register char *stack_base
= stack
+ 20;
276 register unsigned int stack_offset
= 0;
280 for(i
= 0; i
< cif
->nargs
+ 7; i
++)
282 printf("sp+%d: 0x%08x\n", i
*4, ((unsigned int*)stack
)[i
]);
286 /* If cif->flags is struct then we know it's not passed in registers */
287 if(cif
->flags
== FFI_TYPE_STRUCT
)
289 *rvalue
= *(void **)reg_base
;
295 for(i
= 0, p_arg
= cif
->arg_types
; i
< cif
->nargs
; i
++, p_arg
++)
297 size_t z
= (*p_arg
)->size
;
298 int alignment
= (*p_arg
)->alignment
;
307 if(pass_struct_on_stack(*p_arg
))
309 *p_argv
= (void*)stack_base
+ stack_offset
;
312 else if(z
<= sizeof(int))
316 while((reg_mask
>> index
) & 1)
319 *p_argv
= (void*)reg_base
+ (index
* 4);
320 reg_mask
|= (1 << index
);
322 else if(z
== 2 * sizeof(int))
324 if(!((reg_mask
>> 1) & 1))
326 *p_argv
= (void*)reg_base
+ 4;
327 reg_mask
|= (3 << 1);
329 else if(!((reg_mask
>> 3) & 1))
331 *p_argv
= (void*)reg_base
+ 12;
332 reg_mask
|= (3 << 3);
339 *p_argv
= (void*)stack_base
+ stack_offset
;
343 if((*p_arg
)->type
!= FFI_TYPE_STRUCT
||
344 (*p_arg
)->elements
[1] == NULL
)
347 **(unsigned int**)p_argv
<<= 24;
348 else if(alignment
== 2)
349 **(unsigned int**)p_argv
<<= 16;
357 for(i
= 0; i
< cif
->nargs
; i
++)
359 printf("sp+%d: 0x%08x\n", i
*4, *(((unsigned int**)avalue
)[i
]));
364 /* This function is jumped to by the trampoline */
366 unsigned int ffi_closure_SYSV_inner(ffi_closure
*closure
, void **respp
,
371 unsigned int i
, size
= 0;
376 for(i
= 0, p_arg
= cif
->arg_types
; i
< cif
->nargs
; i
++, p_arg
++)
377 size
+= (*p_arg
)->size
+ (4 - (*p_arg
)->size
% 4);
379 arg_area
= (void **)alloca(size
);
381 /* this call will initialize ARG_AREA, such that each element in that
382 * array points to the corresponding value on the stack; and if the
383 * function returns a structure, it will re-set RESP to point to the
384 * structure return address. */
386 ffi_prep_incoming_args_SYSV(args
, respp
, arg_area
, cif
);
388 (closure
->fun
)(cif
, *respp
, arg_area
, closure
->user_data
);
393 ffi_status
ffi_prep_closure_loc(ffi_closure
* closure
, ffi_cif
* cif
,
394 void (*fun
)(ffi_cif
*, void*, void**, void*), void *user_data
,
397 FFI_ASSERT(cif
->abi
== FFI_SYSV
);
399 unsigned char *__tramp
= (unsigned char*)(&closure
->tramp
[0]);
400 unsigned int __fun
= (unsigned int)(&ffi_closure_SYSV
);
401 unsigned int __ctx
= (unsigned int)(codeloc
);
402 unsigned int __rstruct_flag
= (unsigned int)(cif
->rstruct_flag
);
403 unsigned int __inner
= (unsigned int)(&ffi_closure_SYSV_inner
);
404 *(unsigned int*) &__tramp
[0] = 0xebcd1f00; /* pushm r8-r12 */
405 *(unsigned int*) &__tramp
[4] = 0xfefc0010; /* ld.w r12, pc[16] */
406 *(unsigned int*) &__tramp
[8] = 0xfefb0010; /* ld.w r11, pc[16] */
407 *(unsigned int*) &__tramp
[12] = 0xfefa0010; /* ld.w r10, pc[16] */
408 *(unsigned int*) &__tramp
[16] = 0xfeff0010; /* ld.w pc, pc[16] */
409 *(unsigned int*) &__tramp
[20] = __ctx
;
410 *(unsigned int*) &__tramp
[24] = __rstruct_flag
;
411 *(unsigned int*) &__tramp
[28] = __inner
;
412 *(unsigned int*) &__tramp
[32] = __fun
;
413 syscall(__NR_cacheflush
, 0, (&__tramp
[0]), 36);
416 closure
->user_data
= user_data
;