| 1 |
/* ----------------------------------------------------------------------- |
| 2 |
ffi.c - Copyright (c) 2013 Synopsys, Inc. (www.synopsys.com) |
| 3 |
|
| 4 |
ARC 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, EXPRESS |
| 18 |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 |
IN NO EVENT SHALL RENESAS TECHNOLOGY BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 21 |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 22 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
----------------------------------------------------------------------- */ |
| 25 |
|
| 26 |
#include <ffi.h> |
| 27 |
#include <ffi_common.h> |
| 28 |
|
| 29 |
#include <stdlib.h> |
| 30 |
#include <stdint.h> |
| 31 |
|
| 32 |
#include <sys/cachectl.h> |
| 33 |
|
| 34 |
#define NARGREG 8 |
| 35 |
#define STKALIGN 4 |
| 36 |
#define MAXCOPYARG (2 * sizeof(double)) |
| 37 |
|
| 38 |
typedef struct call_context |
| 39 |
{ |
| 40 |
size_t r[8]; |
| 41 |
/* used by the assembly code to in-place construct its own stack frame */ |
| 42 |
char frame[16]; |
| 43 |
} call_context; |
| 44 |
|
| 45 |
typedef struct call_builder |
| 46 |
{ |
| 47 |
call_context *aregs; |
| 48 |
int used_integer; |
| 49 |
//int used_float; |
| 50 |
size_t *used_stack; |
| 51 |
void *struct_stack; |
| 52 |
} call_builder; |
| 53 |
|
| 54 |
/* integer (not pointer) less than ABI XLEN */ |
| 55 |
/* FFI_TYPE_INT does not appear to be used */ |
| 56 |
#if defined(__ARC64_ARCH64__) |
| 57 |
#define IS_INT(type) ((type) >= FFI_TYPE_UINT8 && (type) <= FFI_TYPE_SINT64) |
| 58 |
#else |
| 59 |
#define IS_INT(type) ((type) >= FFI_TYPE_UINT8 && (type) <= FFI_TYPE_SINT32) |
| 60 |
#endif |
| 61 |
|
| 62 |
/* for little endian ARC, the code is in fact stored as mixed endian for |
| 63 |
performance reasons */ |
| 64 |
#if __BIG_ENDIAN__ |
| 65 |
#define CODE_ENDIAN(x) (x) |
| 66 |
#else |
| 67 |
#define CODE_ENDIAN(x) ( (((uint32_t) (x)) << 16) | (((uint32_t) (x)) >> 16)) |
| 68 |
#endif |
| 69 |
|
| 70 |
/* Perform machine dependent cif processing. */ |
| 71 |
ffi_status |
| 72 |
ffi_prep_cif_machdep (ffi_cif * cif) |
| 73 |
{ |
| 74 |
/* Set the return type flag. */ |
| 75 |
switch (cif->rtype->type) |
| 76 |
{ |
| 77 |
case FFI_TYPE_VOID: |
| 78 |
cif->flags = (unsigned) cif->rtype->type; |
| 79 |
break; |
| 80 |
|
| 81 |
case FFI_TYPE_STRUCT: |
| 82 |
cif->flags = (unsigned) cif->rtype->type; |
| 83 |
break; |
| 84 |
|
| 85 |
case FFI_TYPE_SINT64: |
| 86 |
case FFI_TYPE_UINT64: |
| 87 |
case FFI_TYPE_DOUBLE: |
| 88 |
cif->flags = FFI_TYPE_DOUBLE; |
| 89 |
break; |
| 90 |
|
| 91 |
case FFI_TYPE_FLOAT: |
| 92 |
default: |
| 93 |
cif->flags = FFI_TYPE_INT; |
| 94 |
break; |
| 95 |
} |
| 96 |
|
| 97 |
return FFI_OK; |
| 98 |
} |
| 99 |
|
| 100 |
/* allocates a single register, float register, or XLEN-sized stack slot to a datum */ |
| 101 |
static void marshal_atom(call_builder *cb, int type, void *data) { |
| 102 |
size_t value = 0; |
| 103 |
switch (type) { |
| 104 |
case FFI_TYPE_UINT8: value = *(uint8_t *)data; break; |
| 105 |
case FFI_TYPE_SINT8: value = *(int8_t *)data; break; |
| 106 |
case FFI_TYPE_UINT16: value = *(uint16_t *)data; break; |
| 107 |
case FFI_TYPE_SINT16: value = *(int16_t *)data; break; |
| 108 |
/* 32-bit quantities are always sign-extended in the ABI */ |
| 109 |
case FFI_TYPE_UINT32: value = *(int32_t *)data; break; |
| 110 |
case FFI_TYPE_SINT32: value = *(int32_t *)data; break; |
| 111 |
#if defined(__ARC64_ARCH64__) |
| 112 |
case FFI_TYPE_UINT64: value = *(uint64_t *)data; break; |
| 113 |
case FFI_TYPE_SINT64: value = *(int64_t *)data; break; |
| 114 |
#endif |
| 115 |
case FFI_TYPE_POINTER: value = *(size_t *)data; break; |
| 116 |
default: FFI_ASSERT(0); break; |
| 117 |
} |
| 118 |
|
| 119 |
if (cb->used_integer == NARGREG) { |
| 120 |
*cb->used_stack++ = value; |
| 121 |
} else { |
| 122 |
cb->aregs->r[cb->used_integer++] = value; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/* adds an argument to a call, or a not by reference return value */ |
| 127 |
static void marshal(call_builder *cb, ffi_type *type, int var, void *data) { |
| 128 |
size_t realign[2]; |
| 129 |
|
| 130 |
#if (defined(__ARC64_ARCH64__) || defined(__ARC64_ARCH32__)) |
| 131 |
if (type->size > 2 * __SIZEOF_POINTER__) { |
| 132 |
if (var) { |
| 133 |
marshal_atom(cb, FFI_TYPE_POINTER, &data); |
| 134 |
} else { |
| 135 |
/* copy to stack and pass by reference */ |
| 136 |
data = memcpy (cb->struct_stack, data, type->size); |
| 137 |
cb->struct_stack = (size_t *) FFI_ALIGN ((char *) cb->struct_stack + type->size, __SIZEOF_POINTER__); |
| 138 |
marshal_atom(cb, FFI_TYPE_POINTER, &data); |
| 139 |
} |
| 140 |
} |
| 141 |
#else |
| 142 |
if (type->type == FFI_TYPE_STRUCT) { |
| 143 |
if (var) { |
| 144 |
if (type->size > 0) |
| 145 |
marshal_atom(cb, FFI_TYPE_POINTER, data); |
| 146 |
} else { |
| 147 |
int i; |
| 148 |
|
| 149 |
for (i = 0; i < type->size; i += sizeof(size_t)) { |
| 150 |
marshal_atom(cb, FFI_TYPE_POINTER, data); |
| 151 |
data += sizeof(size_t); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
#endif |
| 156 |
else if (IS_INT(type->type) || type->type == FFI_TYPE_POINTER) { |
| 157 |
marshal_atom(cb, type->type, data); |
| 158 |
} else { |
| 159 |
memcpy(realign, data, type->size); |
| 160 |
if (type->size > 0) |
| 161 |
marshal_atom(cb, FFI_TYPE_POINTER, realign); |
| 162 |
if (type->size > __SIZEOF_POINTER__) |
| 163 |
marshal_atom(cb, FFI_TYPE_POINTER, realign + 1); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
static void unmarshal_atom(call_builder *cb, int type, void *data) { |
| 168 |
size_t value; |
| 169 |
|
| 170 |
if (cb->used_integer == NARGREG) { |
| 171 |
value = *cb->used_stack++; |
| 172 |
} else { |
| 173 |
value = cb->aregs->r[cb->used_integer++]; |
| 174 |
} |
| 175 |
|
| 176 |
switch (type) { |
| 177 |
case FFI_TYPE_UINT8: *(uint8_t *)data = value; break; |
| 178 |
case FFI_TYPE_SINT8: *(uint8_t *)data = value; break; |
| 179 |
case FFI_TYPE_UINT16: *(uint16_t *)data = value; break; |
| 180 |
case FFI_TYPE_SINT16: *(uint16_t *)data = value; break; |
| 181 |
case FFI_TYPE_UINT32: *(uint32_t *)data = value; break; |
| 182 |
case FFI_TYPE_SINT32: *(uint32_t *)data = value; break; |
| 183 |
#if defined(__ARC64_ARCH64__) |
| 184 |
case FFI_TYPE_UINT64: *(uint64_t *)data = value; break; |
| 185 |
case FFI_TYPE_SINT64: *(uint64_t *)data = value; break; |
| 186 |
#endif |
| 187 |
case FFI_TYPE_POINTER: *(size_t *)data = value; break; |
| 188 |
default: FFI_ASSERT(0); break; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/* for arguments passed by reference returns the pointer, otherwise the arg is copied (up to MAXCOPYARG bytes) */ |
| 193 |
static void *unmarshal(call_builder *cb, ffi_type *type, int var, void *data) { |
| 194 |
size_t realign[2]; |
| 195 |
|
| 196 |
#if defined(__ARC64_ARCH64__) |
| 197 |
void *pointer; |
| 198 |
|
| 199 |
if (type->size > 2 * __SIZEOF_POINTER__) { |
| 200 |
/* pass by reference */ |
| 201 |
unmarshal_atom(cb, FFI_TYPE_POINTER, (char*)&pointer); |
| 202 |
return pointer; |
| 203 |
} |
| 204 |
#elif defined(__ARC64_ARCH32__) |
| 205 |
if (type->type == FFI_TYPE_STRUCT) { |
| 206 |
if (type->size > 2 * __SIZEOF_POINTER__) { |
| 207 |
unmarshal_atom(cb, FFI_TYPE_POINTER, &realign[0]); |
| 208 |
memcpy(data, (const void*)realign[0], type->size); |
| 209 |
return data; |
| 210 |
} else { |
| 211 |
int i; |
| 212 |
void *pdata = data; |
| 213 |
|
| 214 |
for (i = 0; i < type->size; i += sizeof(size_t)) { |
| 215 |
unmarshal_atom(cb, FFI_TYPE_POINTER, pdata); |
| 216 |
pdata += sizeof(size_t); |
| 217 |
} |
| 218 |
return data; |
| 219 |
} |
| 220 |
} |
| 221 |
#else |
| 222 |
if (type->type == FFI_TYPE_STRUCT) { |
| 223 |
|
| 224 |
if (var) { |
| 225 |
int i; |
| 226 |
void *pdata = data; |
| 227 |
|
| 228 |
for (i = 0; i < type->size; i += sizeof(size_t)) { |
| 229 |
unmarshal_atom(cb, FFI_TYPE_POINTER, pdata); |
| 230 |
pdata += sizeof(size_t); |
| 231 |
} |
| 232 |
return data; |
| 233 |
} else { |
| 234 |
if (type->size > 0) |
| 235 |
unmarshal_atom(cb, FFI_TYPE_POINTER, &realign[0]); |
| 236 |
memcpy(data, (const void*)realign[0], type->size); |
| 237 |
return data; |
| 238 |
} |
| 239 |
} |
| 240 |
#endif |
| 241 |
else if (IS_INT(type->type) || type->type == FFI_TYPE_POINTER) { |
| 242 |
unmarshal_atom(cb, type->type, data); |
| 243 |
return data; |
| 244 |
} else { |
| 245 |
if (type->size > 0) |
| 246 |
unmarshal_atom(cb, FFI_TYPE_POINTER, realign); |
| 247 |
if (type->size > __SIZEOF_POINTER__) |
| 248 |
unmarshal_atom(cb, FFI_TYPE_POINTER, realign + 1); |
| 249 |
memcpy(data, realign, type->size); |
| 250 |
return data; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
static int passed_by_ref(ffi_type *type, int var) { |
| 255 |
if (type->type == FFI_TYPE_STRUCT) |
| 256 |
return 1; |
| 257 |
|
| 258 |
return type->size > 2 * __SIZEOF_POINTER__; |
| 259 |
} |
| 260 |
|
| 261 |
/* Low level routine for calling functions */ |
| 262 |
extern void ffi_call_asm (void *stack, struct call_context *regs, |
| 263 |
void (*fn) (void), void *closure) FFI_HIDDEN; |
| 264 |
|
| 265 |
static void |
| 266 |
ffi_call_int (ffi_cif *cif, void (*fn) (void), void *rvalue, void **avalue, |
| 267 |
void *closure) |
| 268 |
{ |
| 269 |
int return_by_ref = passed_by_ref(cif->rtype, 0); |
| 270 |
|
| 271 |
/* Allocate space for stack arg parameters. */ |
| 272 |
size_t arg_bytes = FFI_ALIGN(2 * sizeof(size_t) * cif->nargs, STKALIGN); |
| 273 |
/* Allocate space for copies of big structures. */ |
| 274 |
size_t struct_bytes = FFI_ALIGN(cif->bytes, STKALIGN); |
| 275 |
// size_t rval_bytes = 0; |
| 276 |
// if (rvalue == NULL && cif->rtype->size > 2*__SIZEOF_POINTER__) |
| 277 |
// rval_bytes = FFI_ALIGN(cif->rtype->size, STKALIGN); |
| 278 |
size_t alloc_size = arg_bytes + /*rval_bytes +*/ struct_bytes + sizeof(call_context); |
| 279 |
size_t alloc_base = (size_t)alloca(alloc_size); |
| 280 |
|
| 281 |
// if (rval_bytes) |
| 282 |
// rvalue = (void*)(alloc_base + arg_bytes); |
| 283 |
|
| 284 |
call_builder cb; |
| 285 |
cb.used_integer = 0; |
| 286 |
cb.aregs = (call_context*)(alloc_base + arg_bytes /*+ rval_bytes*/ + struct_bytes); |
| 287 |
cb.used_stack = (void*)alloc_base; |
| 288 |
cb.struct_stack = (void *)(alloc_base + arg_bytes /*+ rval_bytes*/); |
| 289 |
|
| 290 |
// if (cif->rtype->type == FFI_TYPE_STRUCT) |
| 291 |
// marshal(&cb, &ffi_type_pointer, 0, &rvalue); |
| 292 |
|
| 293 |
if (return_by_ref) |
| 294 |
marshal(&cb, &ffi_type_pointer, 0, &rvalue); |
| 295 |
|
| 296 |
int i; |
| 297 |
for (i = 0; i < cif->nargs; i++) |
| 298 |
marshal(&cb, cif->arg_types[i], 0, avalue[i]); |
| 299 |
|
| 300 |
ffi_call_asm ((void *) alloc_base, cb.aregs, fn, closure); |
| 301 |
|
| 302 |
cb.used_integer = 0; |
| 303 |
if (!return_by_ref && rvalue) |
| 304 |
{ |
| 305 |
if (IS_INT(cif->rtype->type) |
| 306 |
&& cif->rtype->size < sizeof (ffi_arg)) |
| 307 |
{ |
| 308 |
/* Integer types smaller than ffi_arg need to be extended. */ |
| 309 |
switch (cif->rtype->type) { |
| 310 |
case FFI_TYPE_SINT8: |
| 311 |
case FFI_TYPE_SINT16: |
| 312 |
case FFI_TYPE_SINT32: |
| 313 |
unmarshal_atom (&cb, (sizeof (ffi_arg) > 4 |
| 314 |
? FFI_TYPE_SINT64 : FFI_TYPE_SINT32), |
| 315 |
rvalue); |
| 316 |
break; |
| 317 |
case FFI_TYPE_UINT8: |
| 318 |
case FFI_TYPE_UINT16: |
| 319 |
case FFI_TYPE_UINT32: |
| 320 |
unmarshal_atom (&cb, (sizeof (ffi_arg) > 4 |
| 321 |
? FFI_TYPE_UINT64 : FFI_TYPE_UINT32), |
| 322 |
rvalue); |
| 323 |
break; |
| 324 |
} |
| 325 |
} |
| 326 |
else |
| 327 |
unmarshal(&cb, cif->rtype, 0, rvalue); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
void |
| 332 |
ffi_call (ffi_cif *cif, void (*fn) (void), void *rvalue, void **avalue) |
| 333 |
{ |
| 334 |
ffi_call_int(cif, fn, rvalue, avalue, NULL); |
| 335 |
} |
| 336 |
|
| 337 |
void |
| 338 |
ffi_call_go (ffi_cif *cif, void (*fn) (void), void *rvalue, |
| 339 |
void **avalue, void *closure) |
| 340 |
{ |
| 341 |
ffi_call_int(cif, fn, rvalue, avalue, closure); |
| 342 |
} |
| 343 |
|
| 344 |
extern void ffi_closure_asm(void) FFI_HIDDEN; |
| 345 |
|
| 346 |
ffi_status |
| 347 |
ffi_prep_closure_loc (ffi_closure * closure, ffi_cif * cif, |
| 348 |
void (*fun) (ffi_cif *, void *, void **, void *), |
| 349 |
void *user_data, void *codeloc) |
| 350 |
{ |
| 351 |
uint32_t *tramp = (uint32_t *) & (closure->tramp[0]); |
| 352 |
|
| 353 |
#if defined(__ARC64_ARCH64__) |
| 354 |
size_t address_ffi_closure = (size_t) ffi_closure_asm; |
| 355 |
#endif |
| 356 |
|
| 357 |
switch (cif->abi) |
| 358 |
{ |
| 359 |
#if defined(__ARC64_ARCH64__) |
| 360 |
case FFI_ARC64: |
| 361 |
FFI_ASSERT (tramp == codeloc); |
| 362 |
tramp[0] = CODE_ENDIAN (0x580a1fc0); /* movl r8, pcl */ |
| 363 |
tramp[1] = CODE_ENDIAN (0x5c0b1f80); /* movhl r12, limm */ |
| 364 |
tramp[2] = CODE_ENDIAN ((uint32_t)(address_ffi_closure >> 32)); |
| 365 |
tramp[3] = CODE_ENDIAN (0x5c051f8c); /* orl r12, r12, limm */ |
| 366 |
tramp[4] = CODE_ENDIAN ((uint32_t)(address_ffi_closure & 0xffffffff)); |
| 367 |
tramp[5] = CODE_ENDIAN (0x20200300); /* j [r12] */ |
| 368 |
break; |
| 369 |
#else |
| 370 |
case FFI_ARCOMPACT: |
| 371 |
FFI_ASSERT (tramp == codeloc); |
| 372 |
tramp[0] = CODE_ENDIAN (0x200a1fc0); /* mov r8, pcl */ |
| 373 |
tramp[1] = CODE_ENDIAN (0x20200f80); /* j [long imm] */ |
| 374 |
tramp[2] = CODE_ENDIAN ((uint32_t) ffi_closure_asm); |
| 375 |
break; |
| 376 |
#endif |
| 377 |
|
| 378 |
default: |
| 379 |
return FFI_BAD_ABI; |
| 380 |
} |
| 381 |
|
| 382 |
closure->cif = cif; |
| 383 |
closure->fun = fun; |
| 384 |
closure->user_data = user_data; |
| 385 |
cacheflush (codeloc, FFI_TRAMPOLINE_SIZE, BCACHE); |
| 386 |
|
| 387 |
return FFI_OK; |
| 388 |
} |
| 389 |
|
| 390 |
extern void ffi_go_closure_asm (void) FFI_HIDDEN; |
| 391 |
|
| 392 |
ffi_status |
| 393 |
ffi_prep_go_closure (ffi_go_closure *closure, ffi_cif *cif, |
| 394 |
void (*fun) (ffi_cif *, void *, void **, void *)) |
| 395 |
{ |
| 396 |
if (cif->abi <= FFI_FIRST_ABI || cif->abi >= FFI_LAST_ABI) |
| 397 |
return FFI_BAD_ABI; |
| 398 |
|
| 399 |
closure->tramp = (void *) ffi_go_closure_asm; |
| 400 |
closure->cif = cif; |
| 401 |
closure->fun = fun; |
| 402 |
|
| 403 |
return FFI_OK; |
| 404 |
} |
| 405 |
|
| 406 |
/* Called by the assembly code with aregs pointing to saved argument registers |
| 407 |
and stack pointing to the stacked arguments. Return values passed in |
| 408 |
registers will be reloaded from aregs. */ |
| 409 |
void FFI_HIDDEN |
| 410 |
ffi_closure_inner (ffi_cif *cif, |
| 411 |
void (*fun) (ffi_cif *, void *, void **, void *), |
| 412 |
void *user_data, |
| 413 |
size_t *stack, call_context *aregs) |
| 414 |
{ |
| 415 |
void **avalue = alloca(cif->nargs * sizeof(void*)); |
| 416 |
/* storage for arguments which will be copied by unmarshal(). We could |
| 417 |
theoretically avoid the copies in many cases and use at most 128 bytes |
| 418 |
of memory, but allocating disjoint storage for each argument is |
| 419 |
simpler. */ |
| 420 |
char *astorage = alloca(cif->bytes); |
| 421 |
char *ptr = astorage; |
| 422 |
void *rvalue; |
| 423 |
call_builder cb; |
| 424 |
int i; |
| 425 |
|
| 426 |
cb.aregs = aregs; |
| 427 |
cb.used_integer = 0; |
| 428 |
cb.used_stack = stack; |
| 429 |
|
| 430 |
/* handle hidden argument */ |
| 431 |
if (cif->flags == FFI_TYPE_STRUCT) |
| 432 |
unmarshal(&cb, &ffi_type_pointer, 0, &rvalue); |
| 433 |
else |
| 434 |
rvalue = alloca(cif->rtype->size); |
| 435 |
|
| 436 |
for (i = 0; i < cif->nargs; i++) { |
| 437 |
avalue[i] = unmarshal(&cb, cif->arg_types[i], 1, ptr); |
| 438 |
ptr += cif->arg_types[i]->size; |
| 439 |
} |
| 440 |
|
| 441 |
fun (cif, rvalue, avalue, user_data); |
| 442 |
|
| 443 |
if (cif->rtype->type != FFI_TYPE_VOID) { |
| 444 |
cb.used_integer = 0; |
| 445 |
marshal(&cb, cif->rtype, 1, rvalue); |
| 446 |
} |
| 447 |
} |