| 1 |
/* ----------------------------------------------------------------------- |
| 2 |
ffi.c - Copyright (c) 2013 Tensilica, Inc. |
| 3 |
|
| 4 |
XTENSA Foreign Function Interface |
| 5 |
|
| 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: |
| 13 |
|
| 14 |
The above copyright notice and this permission notice shall be included |
| 15 |
in all copies or substantial portions of the Software. |
| 16 |
|
| 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 |
----------------------------------------------------------------------- */ |
| 26 |
|
| 27 |
#include <ffi.h> |
| 28 |
#include <ffi_common.h> |
| 29 |
|
| 30 |
/* |
| 31 |
|----------------------------------------| |
| 32 |
| | |
| 33 |
on entry to ffi_call ----> |----------------------------------------| |
| 34 |
| caller stack frame for registers a0-a3 | |
| 35 |
|----------------------------------------| |
| 36 |
| | |
| 37 |
| additional arguments | |
| 38 |
entry of the function ---> |----------------------------------------| |
| 39 |
| copy of function arguments a2-a7 | |
| 40 |
| - - - - - - - - - - - - - | |
| 41 |
| | |
| 42 |
|
| 43 |
The area below the entry line becomes the new stack frame for the function. |
| 44 |
|
| 45 |
*/ |
| 46 |
|
| 47 |
|
| 48 |
#define FFI_TYPE_STRUCT_REGS FFI_TYPE_LAST |
| 49 |
|
| 50 |
|
| 51 |
extern void ffi_call_SYSV(void *rvalue, unsigned rsize, unsigned flags, |
| 52 |
void(*fn)(void), unsigned nbytes, extended_cif*); |
| 53 |
extern void ffi_closure_SYSV(void) FFI_HIDDEN; |
| 54 |
|
| 55 |
ffi_status ffi_prep_cif_machdep(ffi_cif *cif) |
| 56 |
{ |
| 57 |
switch(cif->rtype->type) { |
| 58 |
case FFI_TYPE_SINT8: |
| 59 |
case FFI_TYPE_UINT8: |
| 60 |
case FFI_TYPE_SINT16: |
| 61 |
case FFI_TYPE_UINT16: |
| 62 |
cif->flags = cif->rtype->type; |
| 63 |
break; |
| 64 |
case FFI_TYPE_VOID: |
| 65 |
case FFI_TYPE_FLOAT: |
| 66 |
cif->flags = FFI_TYPE_UINT32; |
| 67 |
break; |
| 68 |
case FFI_TYPE_DOUBLE: |
| 69 |
case FFI_TYPE_UINT64: |
| 70 |
case FFI_TYPE_SINT64: |
| 71 |
cif->flags = FFI_TYPE_UINT64; // cif->rtype->type; |
| 72 |
break; |
| 73 |
case FFI_TYPE_STRUCT: |
| 74 |
cif->flags = FFI_TYPE_STRUCT; //_REGS; |
| 75 |
/* Up to 16 bytes are returned in registers */ |
| 76 |
if (cif->rtype->size > 4 * 4) { |
| 77 |
/* returned structure is referenced by a register; use 8 bytes |
| 78 |
(including 4 bytes for potential additional alignment) */ |
| 79 |
cif->flags = FFI_TYPE_STRUCT; |
| 80 |
cif->bytes += 8; |
| 81 |
} |
| 82 |
break; |
| 83 |
|
| 84 |
default: |
| 85 |
cif->flags = FFI_TYPE_UINT32; |
| 86 |
break; |
| 87 |
} |
| 88 |
|
| 89 |
/* Round up stack size needed for arguments. |
| 90 |
Allocate FFI_REGISTER_ARGS_SPACE bytes when there are only arguments |
| 91 |
passed in registers, round space reserved for arguments passed on stack |
| 92 |
up to ABI-specified alignment. */ |
| 93 |
if (cif->bytes < FFI_REGISTER_NARGS * 4) |
| 94 |
cif->bytes = FFI_REGISTER_ARGS_SPACE; |
| 95 |
else |
| 96 |
cif->bytes = FFI_REGISTER_ARGS_SPACE + |
| 97 |
FFI_ALIGN(cif->bytes - FFI_REGISTER_NARGS * 4, |
| 98 |
XTENSA_STACK_ALIGNMENT); |
| 99 |
return FFI_OK; |
| 100 |
} |
| 101 |
|
| 102 |
void ffi_prep_args(extended_cif *ecif, unsigned char* stack) |
| 103 |
{ |
| 104 |
unsigned int i; |
| 105 |
unsigned long *addr; |
| 106 |
ffi_type **ptr; |
| 107 |
|
| 108 |
union { |
| 109 |
void **v; |
| 110 |
char **c; |
| 111 |
signed char **sc; |
| 112 |
unsigned char **uc; |
| 113 |
signed short **ss; |
| 114 |
unsigned short **us; |
| 115 |
unsigned int **i; |
| 116 |
long long **ll; |
| 117 |
float **f; |
| 118 |
double **d; |
| 119 |
} p_argv; |
| 120 |
|
| 121 |
/* Verify that everything is aligned up properly */ |
| 122 |
FFI_ASSERT (((unsigned long) stack & 0x7) == 0); |
| 123 |
|
| 124 |
p_argv.v = ecif->avalue; |
| 125 |
addr = (unsigned long*)stack; |
| 126 |
|
| 127 |
/* structures with a size greater than 16 bytes are passed in memory */ |
| 128 |
if (ecif->cif->rtype->type == FFI_TYPE_STRUCT && ecif->cif->rtype->size > 16) |
| 129 |
{ |
| 130 |
*addr++ = (unsigned long)ecif->rvalue; |
| 131 |
} |
| 132 |
|
| 133 |
for (i = ecif->cif->nargs, ptr = ecif->cif->arg_types; |
| 134 |
i > 0; |
| 135 |
i--, ptr++, p_argv.v++) |
| 136 |
{ |
| 137 |
switch ((*ptr)->type) |
| 138 |
{ |
| 139 |
case FFI_TYPE_SINT8: |
| 140 |
*addr++ = **p_argv.sc; |
| 141 |
break; |
| 142 |
case FFI_TYPE_UINT8: |
| 143 |
*addr++ = **p_argv.uc; |
| 144 |
break; |
| 145 |
case FFI_TYPE_SINT16: |
| 146 |
*addr++ = **p_argv.ss; |
| 147 |
break; |
| 148 |
case FFI_TYPE_UINT16: |
| 149 |
*addr++ = **p_argv.us; |
| 150 |
break; |
| 151 |
case FFI_TYPE_FLOAT: |
| 152 |
case FFI_TYPE_INT: |
| 153 |
case FFI_TYPE_UINT32: |
| 154 |
case FFI_TYPE_SINT32: |
| 155 |
case FFI_TYPE_POINTER: |
| 156 |
*addr++ = **p_argv.i; |
| 157 |
break; |
| 158 |
case FFI_TYPE_DOUBLE: |
| 159 |
case FFI_TYPE_UINT64: |
| 160 |
case FFI_TYPE_SINT64: |
| 161 |
if (((unsigned long)addr & 4) != 0) |
| 162 |
addr++; |
| 163 |
*(unsigned long long*)addr = **p_argv.ll; |
| 164 |
addr += sizeof(unsigned long long) / sizeof (addr); |
| 165 |
break; |
| 166 |
|
| 167 |
case FFI_TYPE_STRUCT: |
| 168 |
{ |
| 169 |
unsigned long offs; |
| 170 |
unsigned long size; |
| 171 |
|
| 172 |
if (((unsigned long)addr & 4) != 0 && (*ptr)->alignment > 4) |
| 173 |
addr++; |
| 174 |
|
| 175 |
offs = (unsigned long) addr - (unsigned long) stack; |
| 176 |
size = (*ptr)->size; |
| 177 |
|
| 178 |
/* Entire structure must fit the argument registers or referenced */ |
| 179 |
if (offs < FFI_REGISTER_NARGS * 4 |
| 180 |
&& offs + size > FFI_REGISTER_NARGS * 4) |
| 181 |
addr = (unsigned long*) (stack + FFI_REGISTER_NARGS * 4); |
| 182 |
|
| 183 |
memcpy((char*) addr, *p_argv.c, size); |
| 184 |
addr += (size + 3) / 4; |
| 185 |
break; |
| 186 |
} |
| 187 |
|
| 188 |
default: |
| 189 |
FFI_ASSERT(0); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
void ffi_call(ffi_cif* cif, void(*fn)(void), void *rvalue, void **avalue) |
| 196 |
{ |
| 197 |
extended_cif ecif; |
| 198 |
unsigned long rsize = cif->rtype->size; |
| 199 |
int flags = cif->flags; |
| 200 |
void *alloc = NULL; |
| 201 |
|
| 202 |
ecif.cif = cif; |
| 203 |
ecif.avalue = avalue; |
| 204 |
|
| 205 |
/* Note that for structures that are returned in registers (size <= 16 bytes) |
| 206 |
we allocate a temporary buffer and use memcpy to copy it to the final |
| 207 |
destination. The reason is that the target address might be misaligned or |
| 208 |
the length not a multiple of 4 bytes. Handling all those cases would be |
| 209 |
very complex. */ |
| 210 |
|
| 211 |
if (flags == FFI_TYPE_STRUCT && (rsize <= 16 || rvalue == NULL)) |
| 212 |
{ |
| 213 |
alloc = alloca(FFI_ALIGN(rsize, 4)); |
| 214 |
ecif.rvalue = alloc; |
| 215 |
} |
| 216 |
else |
| 217 |
{ |
| 218 |
ecif.rvalue = rvalue; |
| 219 |
} |
| 220 |
|
| 221 |
if (cif->abi != FFI_SYSV) |
| 222 |
FFI_ASSERT(0); |
| 223 |
|
| 224 |
ffi_call_SYSV (ecif.rvalue, rsize, cif->flags, fn, cif->bytes, &ecif); |
| 225 |
|
| 226 |
if (alloc != NULL && rvalue != NULL) |
| 227 |
memcpy(rvalue, alloc, rsize); |
| 228 |
} |
| 229 |
|
| 230 |
extern void ffi_trampoline(); |
| 231 |
extern void ffi_cacheflush(void* start, void* end); |
| 232 |
|
| 233 |
ffi_status |
| 234 |
ffi_prep_closure_loc (ffi_closure* closure, |
| 235 |
ffi_cif* cif, |
| 236 |
void (*fun)(ffi_cif*, void*, void**, void*), |
| 237 |
void *user_data, |
| 238 |
void *codeloc) |
| 239 |
{ |
| 240 |
if (cif->abi != FFI_SYSV) |
| 241 |
return FFI_BAD_ABI; |
| 242 |
|
| 243 |
/* copye trampoline to stack and patch 'ffi_closure_SYSV' pointer */ |
| 244 |
memcpy(closure->tramp, ffi_trampoline, FFI_TRAMPOLINE_SIZE); |
| 245 |
*(unsigned int*)(&closure->tramp[8]) = (unsigned int)ffi_closure_SYSV; |
| 246 |
|
| 247 |
// Do we have this function? |
| 248 |
// __builtin___clear_cache(closer->tramp, closer->tramp + FFI_TRAMPOLINE_SIZE) |
| 249 |
ffi_cacheflush(closure->tramp, closure->tramp + FFI_TRAMPOLINE_SIZE); |
| 250 |
|
| 251 |
closure->cif = cif; |
| 252 |
closure->fun = fun; |
| 253 |
closure->user_data = user_data; |
| 254 |
return FFI_OK; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
long FFI_HIDDEN |
| 259 |
ffi_closure_SYSV_inner(ffi_closure *closure, void **values, void *rvalue) |
| 260 |
{ |
| 261 |
ffi_cif *cif; |
| 262 |
ffi_type **arg_types; |
| 263 |
void **avalue; |
| 264 |
int i, areg; |
| 265 |
|
| 266 |
cif = closure->cif; |
| 267 |
if (cif->abi != FFI_SYSV) |
| 268 |
return FFI_BAD_ABI; |
| 269 |
|
| 270 |
areg = 0; |
| 271 |
|
| 272 |
int rtype = cif->rtype->type; |
| 273 |
if (rtype == FFI_TYPE_STRUCT && cif->rtype->size > 4 * 4) |
| 274 |
{ |
| 275 |
rvalue = *values; |
| 276 |
areg++; |
| 277 |
} |
| 278 |
|
| 279 |
cif = closure->cif; |
| 280 |
arg_types = cif->arg_types; |
| 281 |
avalue = alloca(cif->nargs * sizeof(void *)); |
| 282 |
|
| 283 |
for (i = 0; i < cif->nargs; i++) |
| 284 |
{ |
| 285 |
if (arg_types[i]->alignment == 8 && (areg & 1) != 0) |
| 286 |
areg++; |
| 287 |
|
| 288 |
// skip the entry a1, * framework, see ffi_trampoline |
| 289 |
if (areg == FFI_REGISTER_NARGS) |
| 290 |
areg = (FFI_REGISTER_ARGS_SPACE + 32) / 4; |
| 291 |
|
| 292 |
if (arg_types[i]->type == FFI_TYPE_STRUCT) |
| 293 |
{ |
| 294 |
int numregs = ((arg_types[i]->size + 3) & ~3) / 4; |
| 295 |
if (areg < FFI_REGISTER_NARGS && areg + numregs > FFI_REGISTER_NARGS) |
| 296 |
areg = (FFI_REGISTER_ARGS_SPACE + 32) / 4; |
| 297 |
} |
| 298 |
|
| 299 |
avalue[i] = &values[areg]; |
| 300 |
areg += (arg_types[i]->size + 3) / 4; |
| 301 |
} |
| 302 |
|
| 303 |
(closure->fun)(cif, rvalue, avalue, closure->user_data); |
| 304 |
|
| 305 |
return rtype; |
| 306 |
} |