| 1 |
/* ----------------------------------------------------------------------- |
| 2 |
ffi.c - Copyright (c) 1996, 1998, 1999, 2001 Red Hat, Inc. |
| 3 |
Copyright (c) 2002 Ranjit Mathew |
| 4 |
Copyright (c) 2002 Bo Thorsen |
| 5 |
Copyright (c) 2002 Roger Sayle |
| 6 |
|
| 7 |
x86 Foreign Function Interface |
| 8 |
|
| 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: |
| 16 |
|
| 17 |
The above copyright notice and this permission notice shall be included |
| 18 |
in all copies or substantial portions of the Software. |
| 19 |
|
| 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 CYGNUS SOLUTIONS 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 |
----------------------------------------------------------------------- */ |
| 28 |
|
| 29 |
#include <ffi.h> |
| 30 |
#include <ffi_common.h> |
| 31 |
|
| 32 |
#include <stdlib.h> |
| 33 |
|
| 34 |
/* ffi_prep_args is called by the assembly routine once stack space |
| 35 |
has been allocated for the function's arguments */ |
| 36 |
|
| 37 |
extern void Py_FatalError(const char *msg); |
| 38 |
|
| 39 |
/*@-exportheader@*/ |
| 40 |
void ffi_prep_args(char *stack, extended_cif *ecif) |
| 41 |
/*@=exportheader@*/ |
| 42 |
{ |
| 43 |
register unsigned int i; |
| 44 |
register void **p_argv; |
| 45 |
register char *argp; |
| 46 |
register ffi_type **p_arg; |
| 47 |
|
| 48 |
argp = stack; |
| 49 |
if (ecif->cif->flags == FFI_TYPE_STRUCT) |
| 50 |
{ |
| 51 |
*(void **) argp = ecif->rvalue; |
| 52 |
argp += sizeof(void *); |
| 53 |
} |
| 54 |
|
| 55 |
p_argv = ecif->avalue; |
| 56 |
|
| 57 |
for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types; |
| 58 |
i != 0; |
| 59 |
i--, p_arg++) |
| 60 |
{ |
| 61 |
size_t z; |
| 62 |
|
| 63 |
/* Align if necessary */ |
| 64 |
if ((sizeof(void *) - 1) & (size_t) argp) |
| 65 |
argp = (char *) ALIGN(argp, sizeof(void *)); |
| 66 |
|
| 67 |
z = (*p_arg)->size; |
| 68 |
if (z < sizeof(int)) |
| 69 |
{ |
| 70 |
z = sizeof(int); |
| 71 |
switch ((*p_arg)->type) |
| 72 |
{ |
| 73 |
case FFI_TYPE_SINT8: |
| 74 |
*(signed int *) argp = (signed int)*(SINT8 *)(* p_argv); |
| 75 |
break; |
| 76 |
|
| 77 |
case FFI_TYPE_UINT8: |
| 78 |
*(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv); |
| 79 |
break; |
| 80 |
|
| 81 |
case FFI_TYPE_SINT16: |
| 82 |
*(signed int *) argp = (signed int)*(SINT16 *)(* p_argv); |
| 83 |
break; |
| 84 |
|
| 85 |
case FFI_TYPE_UINT16: |
| 86 |
*(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv); |
| 87 |
break; |
| 88 |
|
| 89 |
case FFI_TYPE_SINT32: |
| 90 |
*(signed int *) argp = (signed int)*(SINT32 *)(* p_argv); |
| 91 |
break; |
| 92 |
|
| 93 |
case FFI_TYPE_UINT32: |
| 94 |
*(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv); |
| 95 |
break; |
| 96 |
|
| 97 |
case FFI_TYPE_STRUCT: |
| 98 |
*(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv); |
| 99 |
break; |
| 100 |
|
| 101 |
default: |
| 102 |
FFI_ASSERT(0); |
| 103 |
} |
| 104 |
} |
| 105 |
#ifdef _WIN64 |
| 106 |
else if (z != 1 && z != 2 && z != 4 && z != 8) |
| 107 |
{ |
| 108 |
/* On Win64, if a single argument takes more than 8 bytes, |
| 109 |
then it is always passed by reference. */ |
| 110 |
*(void **)argp = *p_argv; |
| 111 |
z = 8; |
| 112 |
} |
| 113 |
#endif |
| 114 |
else |
| 115 |
{ |
| 116 |
memcpy(argp, *p_argv, z); |
| 117 |
} |
| 118 |
p_argv++; |
| 119 |
argp += z; |
| 120 |
} |
| 121 |
|
| 122 |
if (argp - stack > (long)ecif->cif->bytes) |
| 123 |
{ |
| 124 |
Py_FatalError("FFI BUG: not enough stack space for arguments"); |
| 125 |
} |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
/* Perform machine dependent cif processing */ |
| 130 |
ffi_status ffi_prep_cif_machdep(ffi_cif *cif) |
| 131 |
{ |
| 132 |
/* Set the return type flag */ |
| 133 |
switch (cif->rtype->type) |
| 134 |
{ |
| 135 |
case FFI_TYPE_VOID: |
| 136 |
case FFI_TYPE_SINT64: |
| 137 |
case FFI_TYPE_FLOAT: |
| 138 |
case FFI_TYPE_DOUBLE: |
| 139 |
case FFI_TYPE_LONGDOUBLE: |
| 140 |
cif->flags = (unsigned) cif->rtype->type; |
| 141 |
break; |
| 142 |
|
| 143 |
case FFI_TYPE_STRUCT: |
| 144 |
/* MSVC returns small structures in registers. Put in cif->flags |
| 145 |
the value FFI_TYPE_STRUCT only if the structure is big enough; |
| 146 |
otherwise, put the 4- or 8-bytes integer type. */ |
| 147 |
if (cif->rtype->size == 1 || |
| 148 |
cif->rtype->size == 2 || |
| 149 |
cif->rtype->size == 4) |
| 150 |
cif->flags = FFI_TYPE_INT; |
| 151 |
else if (cif->rtype->size == 8) |
| 152 |
cif->flags = FFI_TYPE_SINT64; |
| 153 |
else |
| 154 |
cif->flags = FFI_TYPE_STRUCT; |
| 155 |
break; |
| 156 |
|
| 157 |
case FFI_TYPE_UINT64: |
| 158 |
#ifdef _WIN64 |
| 159 |
case FFI_TYPE_POINTER: |
| 160 |
#endif |
| 161 |
cif->flags = FFI_TYPE_SINT64; |
| 162 |
break; |
| 163 |
|
| 164 |
default: |
| 165 |
cif->flags = FFI_TYPE_INT; |
| 166 |
break; |
| 167 |
} |
| 168 |
|
| 169 |
return FFI_OK; |
| 170 |
} |
| 171 |
|
| 172 |
#ifdef _WIN32 |
| 173 |
extern int |
| 174 |
ffi_call_x86(void (*)(char *, extended_cif *), |
| 175 |
/*@out@*/ extended_cif *, |
| 176 |
unsigned, unsigned, |
| 177 |
/*@out@*/ unsigned *, |
| 178 |
void (*fn)()); |
| 179 |
#endif |
| 180 |
|
| 181 |
#ifdef _WIN64 |
| 182 |
extern int |
| 183 |
ffi_call_AMD64(void (*)(char *, extended_cif *), |
| 184 |
/*@out@*/ extended_cif *, |
| 185 |
unsigned, unsigned, |
| 186 |
/*@out@*/ unsigned *, |
| 187 |
void (*fn)()); |
| 188 |
#endif |
| 189 |
|
| 190 |
int |
| 191 |
ffi_call(/*@dependent@*/ ffi_cif *cif, |
| 192 |
void (*fn)(), |
| 193 |
/*@out@*/ void *rvalue, |
| 194 |
/*@dependent@*/ void **avalue) |
| 195 |
{ |
| 196 |
extended_cif ecif; |
| 197 |
|
| 198 |
ecif.cif = cif; |
| 199 |
ecif.avalue = avalue; |
| 200 |
|
| 201 |
/* If the return value is a struct and we don't have a return */ |
| 202 |
/* value address then we need to make one */ |
| 203 |
|
| 204 |
if ((rvalue == NULL) && |
| 205 |
(cif->flags == FFI_TYPE_STRUCT)) |
| 206 |
{ |
| 207 |
/*@-sysunrecog@*/ |
| 208 |
ecif.rvalue = alloca(cif->rtype->size); |
| 209 |
/*@=sysunrecog@*/ |
| 210 |
} |
| 211 |
else |
| 212 |
ecif.rvalue = rvalue; |
| 213 |
|
| 214 |
|
| 215 |
switch (cif->abi) |
| 216 |
{ |
| 217 |
#if !defined(_WIN64) |
| 218 |
case FFI_SYSV: |
| 219 |
case FFI_STDCALL: |
| 220 |
return ffi_call_x86(ffi_prep_args, &ecif, cif->bytes, |
| 221 |
cif->flags, ecif.rvalue, fn); |
| 222 |
break; |
| 223 |
#else |
| 224 |
case FFI_SYSV: |
| 225 |
/*@-usedef@*/ |
| 226 |
return ffi_call_AMD64(ffi_prep_args, &ecif, cif->bytes, |
| 227 |
cif->flags, ecif.rvalue, fn); |
| 228 |
/*@=usedef@*/ |
| 229 |
break; |
| 230 |
#endif |
| 231 |
|
| 232 |
default: |
| 233 |
FFI_ASSERT(0); |
| 234 |
break; |
| 235 |
} |
| 236 |
return -1; /* theller: Hrm. */ |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
/** private members **/ |
| 241 |
|
| 242 |
static void ffi_prep_incoming_args_SYSV (char *stack, void **ret, |
| 243 |
void** args, ffi_cif* cif); |
| 244 |
/* This function is jumped to by the trampoline */ |
| 245 |
|
| 246 |
#ifdef _WIN64 |
| 247 |
void * |
| 248 |
#else |
| 249 |
static void __fastcall |
| 250 |
#endif |
| 251 |
ffi_closure_SYSV (ffi_closure *closure, char *argp) |
| 252 |
{ |
| 253 |
// this is our return value storage |
| 254 |
long double res; |
| 255 |
|
| 256 |
// our various things... |
| 257 |
ffi_cif *cif; |
| 258 |
void **arg_area; |
| 259 |
unsigned short rtype; |
| 260 |
void *resp = (void*)&res; |
| 261 |
void *args = argp + sizeof(void *); |
| 262 |
|
| 263 |
cif = closure->cif; |
| 264 |
arg_area = (void**) alloca (cif->nargs * sizeof (void*)); |
| 265 |
|
| 266 |
/* this call will initialize ARG_AREA, such that each |
| 267 |
* element in that array points to the corresponding |
| 268 |
* value on the stack; and if the function returns |
| 269 |
* a structure, it will re-set RESP to point to the |
| 270 |
* structure return address. */ |
| 271 |
|
| 272 |
ffi_prep_incoming_args_SYSV(args, (void**)&resp, arg_area, cif); |
| 273 |
|
| 274 |
(closure->fun) (cif, resp, arg_area, closure->user_data); |
| 275 |
|
| 276 |
rtype = cif->flags; |
| 277 |
|
| 278 |
#if defined(_WIN32) && !defined(_WIN64) |
| 279 |
#ifdef _MSC_VER |
| 280 |
/* now, do a generic return based on the value of rtype */ |
| 281 |
if (rtype == FFI_TYPE_INT) |
| 282 |
{ |
| 283 |
_asm mov eax, resp ; |
| 284 |
_asm mov eax, [eax] ; |
| 285 |
} |
| 286 |
else if (rtype == FFI_TYPE_FLOAT) |
| 287 |
{ |
| 288 |
_asm mov eax, resp ; |
| 289 |
_asm fld DWORD PTR [eax] ; |
| 290 |
// asm ("flds (%0)" : : "r" (resp) : "st" ); |
| 291 |
} |
| 292 |
else if (rtype == FFI_TYPE_DOUBLE || rtype == FFI_TYPE_LONGDOUBLE) |
| 293 |
{ |
| 294 |
_asm mov eax, resp ; |
| 295 |
_asm fld QWORD PTR [eax] ; |
| 296 |
// asm ("fldl (%0)" : : "r" (resp) : "st", "st(1)" ); |
| 297 |
} |
| 298 |
else if (rtype == FFI_TYPE_SINT64) |
| 299 |
{ |
| 300 |
_asm mov edx, resp ; |
| 301 |
_asm mov eax, [edx] ; |
| 302 |
_asm mov edx, [edx + 4] ; |
| 303 |
// asm ("movl 0(%0),%%eax;" |
| 304 |
// "movl 4(%0),%%edx" |
| 305 |
// : : "r"(resp) |
| 306 |
// : "eax", "edx"); |
| 307 |
} |
| 308 |
else if (rtype == FFI_TYPE_STRUCT) |
| 309 |
{ |
| 310 |
_asm mov eax, resp ; |
| 311 |
} |
| 312 |
#else |
| 313 |
/* now, do a generic return based on the value of rtype */ |
| 314 |
if (rtype == FFI_TYPE_INT) |
| 315 |
{ |
| 316 |
asm ("movl (%0),%%eax" : : "r" (resp) : "eax"); |
| 317 |
} |
| 318 |
else if (rtype == FFI_TYPE_FLOAT) |
| 319 |
{ |
| 320 |
asm ("flds (%0)" : : "r" (resp) : "st" ); |
| 321 |
} |
| 322 |
else if (rtype == FFI_TYPE_DOUBLE || rtype == FFI_TYPE_LONGDOUBLE) |
| 323 |
{ |
| 324 |
asm ("fldl (%0)" : : "r" (resp) : "st", "st(1)" ); |
| 325 |
} |
| 326 |
else if (rtype == FFI_TYPE_SINT64) |
| 327 |
{ |
| 328 |
asm ("movl 0(%0),%%eax;" |
| 329 |
"movl 4(%0),%%edx" |
| 330 |
: : "r"(resp) |
| 331 |
: "eax", "edx"); |
| 332 |
} |
| 333 |
else if (rtype == FFI_TYPE_STRUCT) |
| 334 |
{ |
| 335 |
asm ("movl %0,%%eax" : : "r" (resp) : "eax"); |
| 336 |
} |
| 337 |
#endif |
| 338 |
#endif |
| 339 |
|
| 340 |
#ifdef _WIN64 |
| 341 |
/* The result is returned in rax. This does the right thing for |
| 342 |
result types except for floats; we have to 'mov xmm0, rax' in the |
| 343 |
caller to correct this. |
| 344 |
*/ |
| 345 |
if (rtype == FFI_TYPE_STRUCT) |
| 346 |
return resp; |
| 347 |
return *(void **)resp; |
| 348 |
#endif |
| 349 |
} |
| 350 |
|
| 351 |
/*@-exportheader@*/ |
| 352 |
static void |
| 353 |
ffi_prep_incoming_args_SYSV(char *stack, void **rvalue, |
| 354 |
void **avalue, ffi_cif *cif) |
| 355 |
/*@=exportheader@*/ |
| 356 |
{ |
| 357 |
register unsigned int i; |
| 358 |
register void **p_argv; |
| 359 |
register char *argp; |
| 360 |
register ffi_type **p_arg; |
| 361 |
|
| 362 |
argp = stack; |
| 363 |
|
| 364 |
if ( cif->flags == FFI_TYPE_STRUCT ) { |
| 365 |
*rvalue = *(void **) argp; |
| 366 |
argp += 4; |
| 367 |
} |
| 368 |
|
| 369 |
p_argv = avalue; |
| 370 |
|
| 371 |
for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++) |
| 372 |
{ |
| 373 |
size_t z; |
| 374 |
|
| 375 |
/* Align if necessary */ |
| 376 |
if ((sizeof(char *) - 1) & (size_t) argp) { |
| 377 |
argp = (char *) ALIGN(argp, sizeof(char*)); |
| 378 |
} |
| 379 |
|
| 380 |
z = (*p_arg)->size; |
| 381 |
|
| 382 |
/* because we're little endian, this is what it turns into. */ |
| 383 |
|
| 384 |
#ifdef _WIN64 |
| 385 |
if (z != 1 && z != 2 && z != 4 && z != 8) |
| 386 |
{ |
| 387 |
/* On Win64, if a single argument takes more than 8 bytes, |
| 388 |
then it is always passed by reference. */ |
| 389 |
*p_argv = *((void**) argp); |
| 390 |
z = 8; |
| 391 |
} |
| 392 |
else |
| 393 |
#endif |
| 394 |
*p_argv = (void*) argp; |
| 395 |
|
| 396 |
p_argv++; |
| 397 |
argp += z; |
| 398 |
} |
| 399 |
|
| 400 |
return; |
| 401 |
} |
| 402 |
|
| 403 |
/* the cif must already be prep'ed */ |
| 404 |
extern void ffi_closure_OUTER(); |
| 405 |
|
| 406 |
ffi_status |
| 407 |
ffi_prep_closure_loc (ffi_closure* closure, |
| 408 |
ffi_cif* cif, |
| 409 |
void (*fun)(ffi_cif*,void*,void**,void*), |
| 410 |
void *user_data, |
| 411 |
void *codeloc) |
| 412 |
{ |
| 413 |
short bytes; |
| 414 |
char *tramp; |
| 415 |
#ifdef _WIN64 |
| 416 |
int mask = 0; |
| 417 |
#endif |
| 418 |
FFI_ASSERT (cif->abi == FFI_SYSV); |
| 419 |
|
| 420 |
if (cif->abi == FFI_SYSV) |
| 421 |
bytes = 0; |
| 422 |
#if !defined(_WIN64) |
| 423 |
else if (cif->abi == FFI_STDCALL) |
| 424 |
bytes = cif->bytes; |
| 425 |
#endif |
| 426 |
else |
| 427 |
return FFI_BAD_ABI; |
| 428 |
|
| 429 |
tramp = &closure->tramp[0]; |
| 430 |
|
| 431 |
#define BYTES(text) memcpy(tramp, text, sizeof(text)), tramp += sizeof(text)-1 |
| 432 |
#define POINTER(x) *(void**)tramp = (void*)(x), tramp += sizeof(void*) |
| 433 |
#define SHORT(x) *(short*)tramp = x, tramp += sizeof(short) |
| 434 |
#define INT(x) *(int*)tramp = x, tramp += sizeof(int) |
| 435 |
|
| 436 |
#ifdef _WIN64 |
| 437 |
if (cif->nargs >= 1 && |
| 438 |
(cif->arg_types[0]->type == FFI_TYPE_FLOAT |
| 439 |
|| cif->arg_types[0]->type == FFI_TYPE_DOUBLE)) |
| 440 |
mask |= 1; |
| 441 |
if (cif->nargs >= 2 && |
| 442 |
(cif->arg_types[1]->type == FFI_TYPE_FLOAT |
| 443 |
|| cif->arg_types[1]->type == FFI_TYPE_DOUBLE)) |
| 444 |
mask |= 2; |
| 445 |
if (cif->nargs >= 3 && |
| 446 |
(cif->arg_types[2]->type == FFI_TYPE_FLOAT |
| 447 |
|| cif->arg_types[2]->type == FFI_TYPE_DOUBLE)) |
| 448 |
mask |= 4; |
| 449 |
if (cif->nargs >= 4 && |
| 450 |
(cif->arg_types[3]->type == FFI_TYPE_FLOAT |
| 451 |
|| cif->arg_types[3]->type == FFI_TYPE_DOUBLE)) |
| 452 |
mask |= 8; |
| 453 |
|
| 454 |
/* if we return a non-small struct, then the first argument is a pointer |
| 455 |
* to the return area, and all real arguments are shifted by one */ |
| 456 |
if (cif->flags == FFI_TYPE_STRUCT) |
| 457 |
mask = (mask & ~8) << 1; |
| 458 |
|
| 459 |
/* 41 BB ---- mov r11d,mask */ |
| 460 |
BYTES("\x41\xBB"); INT(mask); |
| 461 |
|
| 462 |
/* 48 B8 -------- mov rax, closure */ |
| 463 |
BYTES("\x48\xB8"); POINTER(closure); |
| 464 |
|
| 465 |
/* 49 BA -------- mov r10, ffi_closure_OUTER */ |
| 466 |
BYTES("\x49\xBA"); POINTER(ffi_closure_OUTER); |
| 467 |
|
| 468 |
/* 41 FF E2 jmp r10 */ |
| 469 |
BYTES("\x41\xFF\xE2"); |
| 470 |
|
| 471 |
#else |
| 472 |
|
| 473 |
/* mov ecx, closure */ |
| 474 |
BYTES("\xb9"); POINTER(closure); |
| 475 |
|
| 476 |
/* mov edx, esp */ |
| 477 |
BYTES("\x8b\xd4"); |
| 478 |
|
| 479 |
/* call ffi_closure_SYSV */ |
| 480 |
BYTES("\xe8"); POINTER((char*)&ffi_closure_SYSV - (tramp + 4)); |
| 481 |
|
| 482 |
/* ret bytes */ |
| 483 |
BYTES("\xc2"); |
| 484 |
SHORT(bytes); |
| 485 |
|
| 486 |
#endif |
| 487 |
|
| 488 |
if (tramp - &closure->tramp[0] > FFI_TRAMPOLINE_SIZE) |
| 489 |
Py_FatalError("FFI_TRAMPOLINE_SIZE too small in " __FILE__); |
| 490 |
|
| 491 |
closure->cif = cif; |
| 492 |
closure->user_data = user_data; |
| 493 |
closure->fun = fun; |
| 494 |
return FFI_OK; |
| 495 |
} |