| 1 |
/* ----------------------------------------------------------------------- |
| 2 |
ffi.c - Copyright (c) 2000, 2007 Software AG |
| 3 |
Copyright (c) 2008 Red Hat, Inc |
| 4 |
|
| 5 |
S390 Foreign Function Interface |
| 6 |
|
| 7 |
Permission is hereby granted, free of charge, to any person obtaining |
| 8 |
a copy of this software and associated documentation files (the |
| 9 |
``Software''), to deal in the Software without restriction, including |
| 10 |
without limitation the rights to use, copy, modify, merge, publish, |
| 11 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 12 |
permit persons to whom the Software is furnished to do so, subject to |
| 13 |
the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included |
| 16 |
in all copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 19 |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 21 |
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 22 |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 23 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 24 |
OTHER DEALINGS IN THE SOFTWARE. |
| 25 |
----------------------------------------------------------------------- */ |
| 26 |
/*====================================================================*/ |
| 27 |
/* Includes */ |
| 28 |
/* -------- */ |
| 29 |
/*====================================================================*/ |
| 30 |
|
| 31 |
#include <ffi.h> |
| 32 |
#include <ffi_common.h> |
| 33 |
#include <stdint.h> |
| 34 |
#include "internal.h" |
| 35 |
#include <tramp.h> |
| 36 |
|
| 37 |
/*====================== End of Includes =============================*/ |
| 38 |
|
| 39 |
/*====================================================================*/ |
| 40 |
/* Defines */ |
| 41 |
/* ------- */ |
| 42 |
/*====================================================================*/ |
| 43 |
|
| 44 |
/* Maximum number of GPRs available for argument passing. */ |
| 45 |
#define MAX_GPRARGS 5 |
| 46 |
|
| 47 |
/* Maximum number of FPRs available for argument passing. */ |
| 48 |
#ifdef __s390x__ |
| 49 |
#define MAX_FPRARGS 4 |
| 50 |
#else |
| 51 |
#define MAX_FPRARGS 2 |
| 52 |
#endif |
| 53 |
|
| 54 |
/* Round to multiple of 16. */ |
| 55 |
#define ROUND_SIZE(size) (((size) + 15) & ~15) |
| 56 |
|
| 57 |
/*===================== End of Defines ===============================*/ |
| 58 |
|
| 59 |
/*====================================================================*/ |
| 60 |
/* Externals */ |
| 61 |
/* --------- */ |
| 62 |
/*====================================================================*/ |
| 63 |
|
| 64 |
struct call_frame |
| 65 |
{ |
| 66 |
void *back_chain; |
| 67 |
void *eos; |
| 68 |
unsigned long gpr_args[5]; |
| 69 |
unsigned long gpr_save[9]; |
| 70 |
unsigned long long fpr_args[4]; |
| 71 |
}; |
| 72 |
|
| 73 |
extern void FFI_HIDDEN ffi_call_SYSV(struct call_frame *, unsigned, void *, |
| 74 |
void (*fn)(void), void *); |
| 75 |
|
| 76 |
extern void ffi_closure_SYSV(void); |
| 77 |
extern void ffi_go_closure_SYSV(void); |
| 78 |
|
| 79 |
/*====================== End of Externals ============================*/ |
| 80 |
|
| 81 |
/*====================================================================*/ |
| 82 |
/* */ |
| 83 |
/* Name - ffi_check_struct_type. */ |
| 84 |
/* */ |
| 85 |
/* Function - Determine if a structure can be passed within a */ |
| 86 |
/* general purpose or floating point register. */ |
| 87 |
/* */ |
| 88 |
/*====================================================================*/ |
| 89 |
|
| 90 |
static int |
| 91 |
ffi_check_struct_type (ffi_type *arg) |
| 92 |
{ |
| 93 |
size_t size = arg->size; |
| 94 |
|
| 95 |
/* If the struct has just one element, look at that element |
| 96 |
to find out whether to consider the struct as floating point. */ |
| 97 |
while (arg->type == FFI_TYPE_STRUCT |
| 98 |
&& arg->elements[0] && !arg->elements[1]) |
| 99 |
arg = arg->elements[0]; |
| 100 |
|
| 101 |
/* Structs of size 1, 2, 4, and 8 are passed in registers, |
| 102 |
just like the corresponding int/float types. */ |
| 103 |
switch (size) |
| 104 |
{ |
| 105 |
case 1: |
| 106 |
return FFI_TYPE_UINT8; |
| 107 |
|
| 108 |
case 2: |
| 109 |
return FFI_TYPE_UINT16; |
| 110 |
|
| 111 |
case 4: |
| 112 |
if (arg->type == FFI_TYPE_FLOAT) |
| 113 |
return FFI_TYPE_FLOAT; |
| 114 |
else |
| 115 |
return FFI_TYPE_UINT32; |
| 116 |
|
| 117 |
case 8: |
| 118 |
if (arg->type == FFI_TYPE_DOUBLE) |
| 119 |
return FFI_TYPE_DOUBLE; |
| 120 |
else |
| 121 |
return FFI_TYPE_UINT64; |
| 122 |
|
| 123 |
default: |
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
/* Other structs are passed via a pointer to the data. */ |
| 128 |
return FFI_TYPE_POINTER; |
| 129 |
} |
| 130 |
|
| 131 |
/*======================== End of Routine ============================*/ |
| 132 |
|
| 133 |
/*====================================================================*/ |
| 134 |
/* */ |
| 135 |
/* Name - ffi_prep_cif_machdep. */ |
| 136 |
/* */ |
| 137 |
/* Function - Perform machine dependent CIF processing. */ |
| 138 |
/* */ |
| 139 |
/*====================================================================*/ |
| 140 |
|
| 141 |
ffi_status FFI_HIDDEN |
| 142 |
ffi_prep_cif_machdep(ffi_cif *cif) |
| 143 |
{ |
| 144 |
size_t struct_size = 0; |
| 145 |
int n_gpr = 0; |
| 146 |
int n_fpr = 0; |
| 147 |
int n_ov = 0; |
| 148 |
|
| 149 |
ffi_type **ptr; |
| 150 |
int i; |
| 151 |
|
| 152 |
/* Determine return value handling. */ |
| 153 |
|
| 154 |
switch (cif->rtype->type) |
| 155 |
{ |
| 156 |
/* Void is easy. */ |
| 157 |
case FFI_TYPE_VOID: |
| 158 |
cif->flags = FFI390_RET_VOID; |
| 159 |
break; |
| 160 |
|
| 161 |
/* Structures and complex are returned via a hidden pointer. */ |
| 162 |
case FFI_TYPE_STRUCT: |
| 163 |
case FFI_TYPE_COMPLEX: |
| 164 |
cif->flags = FFI390_RET_STRUCT; |
| 165 |
n_gpr++; /* We need one GPR to pass the pointer. */ |
| 166 |
break; |
| 167 |
|
| 168 |
/* Floating point values are returned in fpr 0. */ |
| 169 |
case FFI_TYPE_FLOAT: |
| 170 |
cif->flags = FFI390_RET_FLOAT; |
| 171 |
break; |
| 172 |
|
| 173 |
case FFI_TYPE_DOUBLE: |
| 174 |
cif->flags = FFI390_RET_DOUBLE; |
| 175 |
break; |
| 176 |
|
| 177 |
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
| 178 |
case FFI_TYPE_LONGDOUBLE: |
| 179 |
cif->flags = FFI390_RET_STRUCT; |
| 180 |
n_gpr++; |
| 181 |
break; |
| 182 |
#endif |
| 183 |
/* Integer values are returned in gpr 2 (and gpr 3 |
| 184 |
for 64-bit values on 31-bit machines). */ |
| 185 |
case FFI_TYPE_UINT64: |
| 186 |
case FFI_TYPE_SINT64: |
| 187 |
cif->flags = FFI390_RET_INT64; |
| 188 |
break; |
| 189 |
|
| 190 |
case FFI_TYPE_POINTER: |
| 191 |
case FFI_TYPE_INT: |
| 192 |
case FFI_TYPE_UINT32: |
| 193 |
case FFI_TYPE_SINT32: |
| 194 |
case FFI_TYPE_UINT16: |
| 195 |
case FFI_TYPE_SINT16: |
| 196 |
case FFI_TYPE_UINT8: |
| 197 |
case FFI_TYPE_SINT8: |
| 198 |
/* These are to be extended to word size. */ |
| 199 |
#ifdef __s390x__ |
| 200 |
cif->flags = FFI390_RET_INT64; |
| 201 |
#else |
| 202 |
cif->flags = FFI390_RET_INT32; |
| 203 |
#endif |
| 204 |
break; |
| 205 |
|
| 206 |
default: |
| 207 |
FFI_ASSERT (0); |
| 208 |
break; |
| 209 |
} |
| 210 |
|
| 211 |
/* Now for the arguments. */ |
| 212 |
|
| 213 |
for (ptr = cif->arg_types, i = cif->nargs; |
| 214 |
i > 0; |
| 215 |
i--, ptr++) |
| 216 |
{ |
| 217 |
int type = (*ptr)->type; |
| 218 |
|
| 219 |
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
| 220 |
/* 16-byte long double is passed like a struct. */ |
| 221 |
if (type == FFI_TYPE_LONGDOUBLE) |
| 222 |
type = FFI_TYPE_STRUCT; |
| 223 |
#endif |
| 224 |
|
| 225 |
/* Check how a structure type is passed. */ |
| 226 |
if (type == FFI_TYPE_STRUCT || type == FFI_TYPE_COMPLEX) |
| 227 |
{ |
| 228 |
if (type == FFI_TYPE_COMPLEX) |
| 229 |
type = FFI_TYPE_POINTER; |
| 230 |
else |
| 231 |
type = ffi_check_struct_type (*ptr); |
| 232 |
|
| 233 |
/* If we pass the struct via pointer, we must reserve space |
| 234 |
to copy its data for proper call-by-value semantics. */ |
| 235 |
if (type == FFI_TYPE_POINTER) |
| 236 |
struct_size += ROUND_SIZE ((*ptr)->size); |
| 237 |
} |
| 238 |
|
| 239 |
/* Now handle all primitive int/float data types. */ |
| 240 |
switch (type) |
| 241 |
{ |
| 242 |
/* The first MAX_FPRARGS floating point arguments |
| 243 |
go in FPRs, the rest overflow to the stack. */ |
| 244 |
|
| 245 |
case FFI_TYPE_DOUBLE: |
| 246 |
if (n_fpr < MAX_FPRARGS) |
| 247 |
n_fpr++; |
| 248 |
else |
| 249 |
n_ov += sizeof (double) / sizeof (long); |
| 250 |
break; |
| 251 |
|
| 252 |
case FFI_TYPE_FLOAT: |
| 253 |
if (n_fpr < MAX_FPRARGS) |
| 254 |
n_fpr++; |
| 255 |
else |
| 256 |
n_ov++; |
| 257 |
break; |
| 258 |
|
| 259 |
/* On 31-bit machines, 64-bit integers are passed in GPR pairs, |
| 260 |
if one is still available, or else on the stack. If only one |
| 261 |
register is free, skip the register (it won't be used for any |
| 262 |
subsequent argument either). */ |
| 263 |
|
| 264 |
#ifndef __s390x__ |
| 265 |
case FFI_TYPE_UINT64: |
| 266 |
case FFI_TYPE_SINT64: |
| 267 |
if (n_gpr == MAX_GPRARGS-1) |
| 268 |
n_gpr = MAX_GPRARGS; |
| 269 |
if (n_gpr < MAX_GPRARGS) |
| 270 |
n_gpr += 2; |
| 271 |
else |
| 272 |
n_ov += 2; |
| 273 |
break; |
| 274 |
#endif |
| 275 |
|
| 276 |
/* Everything else is passed in GPRs (until MAX_GPRARGS |
| 277 |
have been used) or overflows to the stack. */ |
| 278 |
|
| 279 |
default: |
| 280 |
if (n_gpr < MAX_GPRARGS) |
| 281 |
n_gpr++; |
| 282 |
else |
| 283 |
n_ov++; |
| 284 |
break; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
/* Total stack space as required for overflow arguments |
| 289 |
and temporary structure copies. */ |
| 290 |
|
| 291 |
cif->bytes = ROUND_SIZE (n_ov * sizeof (long)) + struct_size; |
| 292 |
|
| 293 |
return FFI_OK; |
| 294 |
} |
| 295 |
|
| 296 |
/*======================== End of Routine ============================*/ |
| 297 |
|
| 298 |
/*====================================================================*/ |
| 299 |
/* */ |
| 300 |
/* Name - ffi_call. */ |
| 301 |
/* */ |
| 302 |
/* Function - Call the FFI routine. */ |
| 303 |
/* */ |
| 304 |
/*====================================================================*/ |
| 305 |
|
| 306 |
static void |
| 307 |
ffi_call_int(ffi_cif *cif, |
| 308 |
void (*fn)(void), |
| 309 |
void *rvalue, |
| 310 |
void **avalue, |
| 311 |
void *closure) |
| 312 |
{ |
| 313 |
int ret_type = cif->flags; |
| 314 |
size_t rsize = 0, bytes = cif->bytes; |
| 315 |
unsigned char *stack, *p_struct; |
| 316 |
struct call_frame *frame; |
| 317 |
unsigned long *p_ov, *p_gpr; |
| 318 |
unsigned long long *p_fpr; |
| 319 |
int n_fpr, n_gpr, n_ov, i, n; |
| 320 |
ffi_type **arg_types; |
| 321 |
|
| 322 |
FFI_ASSERT (cif->abi == FFI_SYSV); |
| 323 |
|
| 324 |
/* If we don't have a return value, we need to fake one. */ |
| 325 |
if (rvalue == NULL) |
| 326 |
{ |
| 327 |
if (ret_type & FFI390_RET_IN_MEM) |
| 328 |
rsize = cif->rtype->size; |
| 329 |
else |
| 330 |
ret_type = FFI390_RET_VOID; |
| 331 |
} |
| 332 |
|
| 333 |
/* The stack space will be filled with those areas: |
| 334 |
|
| 335 |
dummy structure return (highest addresses) |
| 336 |
FPR argument register save area |
| 337 |
GPR argument register save area |
| 338 |
stack frame for ffi_call_SYSV |
| 339 |
temporary struct copies |
| 340 |
overflow argument area (lowest addresses) |
| 341 |
|
| 342 |
We set up the following pointers: |
| 343 |
|
| 344 |
p_fpr: bottom of the FPR area (growing upwards) |
| 345 |
p_gpr: bottom of the GPR area (growing upwards) |
| 346 |
p_ov: bottom of the overflow area (growing upwards) |
| 347 |
p_struct: top of the struct copy area (growing downwards) |
| 348 |
|
| 349 |
All areas are kept aligned to twice the word size. |
| 350 |
|
| 351 |
Note that we're going to create the stack frame for both |
| 352 |
ffi_call_SYSV _and_ the target function right here. This |
| 353 |
works because we don't make any function calls with more |
| 354 |
than 5 arguments (indeed only memcpy and ffi_call_SYSV), |
| 355 |
and thus we don't have any stacked outgoing parameters. */ |
| 356 |
|
| 357 |
stack = alloca (bytes + sizeof(struct call_frame) + rsize); |
| 358 |
frame = (struct call_frame *)(stack + bytes); |
| 359 |
if (rsize) |
| 360 |
rvalue = frame + 1; |
| 361 |
|
| 362 |
/* Link the new frame back to the one from this function. */ |
| 363 |
frame->back_chain = __builtin_frame_address (0); |
| 364 |
|
| 365 |
/* Fill in all of the argument stuff. */ |
| 366 |
p_ov = (unsigned long *)stack; |
| 367 |
p_struct = (unsigned char *)frame; |
| 368 |
p_gpr = frame->gpr_args; |
| 369 |
p_fpr = frame->fpr_args; |
| 370 |
n_fpr = n_gpr = n_ov = 0; |
| 371 |
|
| 372 |
/* If we returning a structure then we set the first parameter register |
| 373 |
to the address of where we are returning this structure. */ |
| 374 |
if (cif->flags & FFI390_RET_IN_MEM) |
| 375 |
p_gpr[n_gpr++] = (uintptr_t) rvalue; |
| 376 |
|
| 377 |
/* Now for the arguments. */ |
| 378 |
arg_types = cif->arg_types; |
| 379 |
for (i = 0, n = cif->nargs; i < n; ++i) |
| 380 |
{ |
| 381 |
ffi_type *ty = arg_types[i]; |
| 382 |
void *arg = avalue[i]; |
| 383 |
int type = ty->type; |
| 384 |
ffi_arg val; |
| 385 |
|
| 386 |
restart: |
| 387 |
switch (type) |
| 388 |
{ |
| 389 |
case FFI_TYPE_SINT8: |
| 390 |
val = *(SINT8 *)arg; |
| 391 |
goto do_int; |
| 392 |
case FFI_TYPE_UINT8: |
| 393 |
val = *(UINT8 *)arg; |
| 394 |
goto do_int; |
| 395 |
case FFI_TYPE_SINT16: |
| 396 |
val = *(SINT16 *)arg; |
| 397 |
goto do_int; |
| 398 |
case FFI_TYPE_UINT16: |
| 399 |
val = *(UINT16 *)arg; |
| 400 |
goto do_int; |
| 401 |
case FFI_TYPE_INT: |
| 402 |
case FFI_TYPE_SINT32: |
| 403 |
val = *(SINT32 *)arg; |
| 404 |
goto do_int; |
| 405 |
case FFI_TYPE_UINT32: |
| 406 |
val = *(UINT32 *)arg; |
| 407 |
goto do_int; |
| 408 |
case FFI_TYPE_POINTER: |
| 409 |
val = *(uintptr_t *)arg; |
| 410 |
do_int: |
| 411 |
*(n_gpr < MAX_GPRARGS ? p_gpr + n_gpr++ : p_ov + n_ov++) = val; |
| 412 |
break; |
| 413 |
|
| 414 |
case FFI_TYPE_UINT64: |
| 415 |
case FFI_TYPE_SINT64: |
| 416 |
#ifdef __s390x__ |
| 417 |
val = *(UINT64 *)arg; |
| 418 |
goto do_int; |
| 419 |
#else |
| 420 |
if (n_gpr == MAX_GPRARGS-1) |
| 421 |
n_gpr = MAX_GPRARGS; |
| 422 |
if (n_gpr < MAX_GPRARGS) |
| 423 |
p_gpr[n_gpr++] = ((UINT32 *) arg)[0], |
| 424 |
p_gpr[n_gpr++] = ((UINT32 *) arg)[1]; |
| 425 |
else |
| 426 |
p_ov[n_ov++] = ((UINT32 *) arg)[0], |
| 427 |
p_ov[n_ov++] = ((UINT32 *) arg)[1]; |
| 428 |
#endif |
| 429 |
break; |
| 430 |
|
| 431 |
case FFI_TYPE_DOUBLE: |
| 432 |
if (n_fpr < MAX_FPRARGS) |
| 433 |
p_fpr[n_fpr++] = *(UINT64 *) arg; |
| 434 |
else |
| 435 |
{ |
| 436 |
#ifdef __s390x__ |
| 437 |
p_ov[n_ov++] = *(UINT64 *) arg; |
| 438 |
#else |
| 439 |
p_ov[n_ov++] = ((UINT32 *) arg)[0], |
| 440 |
p_ov[n_ov++] = ((UINT32 *) arg)[1]; |
| 441 |
#endif |
| 442 |
} |
| 443 |
break; |
| 444 |
|
| 445 |
case FFI_TYPE_FLOAT: |
| 446 |
val = *(UINT32 *)arg; |
| 447 |
if (n_fpr < MAX_FPRARGS) |
| 448 |
p_fpr[n_fpr++] = (UINT64)val << 32; |
| 449 |
else |
| 450 |
p_ov[n_ov++] = val; |
| 451 |
break; |
| 452 |
|
| 453 |
case FFI_TYPE_STRUCT: |
| 454 |
/* Check how a structure type is passed. */ |
| 455 |
type = ffi_check_struct_type (ty); |
| 456 |
/* Some structures are passed via a type they contain. */ |
| 457 |
if (type != FFI_TYPE_POINTER) |
| 458 |
goto restart; |
| 459 |
/* ... otherwise, passed by reference. fallthru. */ |
| 460 |
|
| 461 |
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
| 462 |
case FFI_TYPE_LONGDOUBLE: |
| 463 |
/* 16-byte long double is passed via reference. */ |
| 464 |
#endif |
| 465 |
case FFI_TYPE_COMPLEX: |
| 466 |
/* Complex types are passed via reference. */ |
| 467 |
p_struct -= ROUND_SIZE (ty->size); |
| 468 |
memcpy (p_struct, arg, ty->size); |
| 469 |
val = (uintptr_t)p_struct; |
| 470 |
goto do_int; |
| 471 |
|
| 472 |
default: |
| 473 |
FFI_ASSERT (0); |
| 474 |
break; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
ffi_call_SYSV (frame, ret_type & FFI360_RET_MASK, rvalue, fn, closure); |
| 479 |
} |
| 480 |
|
| 481 |
void |
| 482 |
ffi_call (ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue) |
| 483 |
{ |
| 484 |
ffi_call_int(cif, fn, rvalue, avalue, NULL); |
| 485 |
} |
| 486 |
|
| 487 |
void |
| 488 |
ffi_call_go (ffi_cif *cif, void (*fn)(void), void *rvalue, |
| 489 |
void **avalue, void *closure) |
| 490 |
{ |
| 491 |
ffi_call_int(cif, fn, rvalue, avalue, closure); |
| 492 |
} |
| 493 |
|
| 494 |
/*======================== End of Routine ============================*/ |
| 495 |
|
| 496 |
/*====================================================================*/ |
| 497 |
/* */ |
| 498 |
/* Name - ffi_closure_helper_SYSV. */ |
| 499 |
/* */ |
| 500 |
/* Function - Call a FFI closure target function. */ |
| 501 |
/* */ |
| 502 |
/*====================================================================*/ |
| 503 |
|
| 504 |
void FFI_HIDDEN |
| 505 |
ffi_closure_helper_SYSV (ffi_cif *cif, |
| 506 |
void (*fun)(ffi_cif*,void*,void**,void*), |
| 507 |
void *user_data, |
| 508 |
unsigned long *p_gpr, |
| 509 |
unsigned long long *p_fpr, |
| 510 |
unsigned long *p_ov) |
| 511 |
{ |
| 512 |
unsigned long long ret_buffer; |
| 513 |
|
| 514 |
void *rvalue = &ret_buffer; |
| 515 |
void **avalue; |
| 516 |
void **p_arg; |
| 517 |
|
| 518 |
int n_gpr = 0; |
| 519 |
int n_fpr = 0; |
| 520 |
int n_ov = 0; |
| 521 |
|
| 522 |
ffi_type **ptr; |
| 523 |
int i; |
| 524 |
|
| 525 |
/* Allocate buffer for argument list pointers. */ |
| 526 |
p_arg = avalue = alloca (cif->nargs * sizeof (void *)); |
| 527 |
|
| 528 |
/* If we returning a structure, pass the structure address |
| 529 |
directly to the target function. Otherwise, have the target |
| 530 |
function store the return value to the GPR save area. */ |
| 531 |
if (cif->flags & FFI390_RET_IN_MEM) |
| 532 |
rvalue = (void *) p_gpr[n_gpr++]; |
| 533 |
|
| 534 |
/* Now for the arguments. */ |
| 535 |
for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, p_arg++, ptr++) |
| 536 |
{ |
| 537 |
int deref_struct_pointer = 0; |
| 538 |
int type = (*ptr)->type; |
| 539 |
|
| 540 |
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
| 541 |
/* 16-byte long double is passed like a struct. */ |
| 542 |
if (type == FFI_TYPE_LONGDOUBLE) |
| 543 |
type = FFI_TYPE_STRUCT; |
| 544 |
#endif |
| 545 |
|
| 546 |
/* Check how a structure type is passed. */ |
| 547 |
if (type == FFI_TYPE_STRUCT || type == FFI_TYPE_COMPLEX) |
| 548 |
{ |
| 549 |
if (type == FFI_TYPE_COMPLEX) |
| 550 |
type = FFI_TYPE_POINTER; |
| 551 |
else |
| 552 |
type = ffi_check_struct_type (*ptr); |
| 553 |
|
| 554 |
/* If we pass the struct via pointer, remember to |
| 555 |
retrieve the pointer later. */ |
| 556 |
if (type == FFI_TYPE_POINTER) |
| 557 |
deref_struct_pointer = 1; |
| 558 |
} |
| 559 |
|
| 560 |
/* Pointers are passed like UINTs of the same size. */ |
| 561 |
if (type == FFI_TYPE_POINTER) |
| 562 |
{ |
| 563 |
#ifdef __s390x__ |
| 564 |
type = FFI_TYPE_UINT64; |
| 565 |
#else |
| 566 |
type = FFI_TYPE_UINT32; |
| 567 |
#endif |
| 568 |
} |
| 569 |
|
| 570 |
/* Now handle all primitive int/float data types. */ |
| 571 |
switch (type) |
| 572 |
{ |
| 573 |
case FFI_TYPE_DOUBLE: |
| 574 |
if (n_fpr < MAX_FPRARGS) |
| 575 |
*p_arg = &p_fpr[n_fpr++]; |
| 576 |
else |
| 577 |
*p_arg = &p_ov[n_ov], |
| 578 |
n_ov += sizeof (double) / sizeof (long); |
| 579 |
break; |
| 580 |
|
| 581 |
case FFI_TYPE_FLOAT: |
| 582 |
if (n_fpr < MAX_FPRARGS) |
| 583 |
*p_arg = &p_fpr[n_fpr++]; |
| 584 |
else |
| 585 |
*p_arg = (char *)&p_ov[n_ov++] + sizeof (long) - 4; |
| 586 |
break; |
| 587 |
|
| 588 |
case FFI_TYPE_UINT64: |
| 589 |
case FFI_TYPE_SINT64: |
| 590 |
#ifdef __s390x__ |
| 591 |
if (n_gpr < MAX_GPRARGS) |
| 592 |
*p_arg = &p_gpr[n_gpr++]; |
| 593 |
else |
| 594 |
*p_arg = &p_ov[n_ov++]; |
| 595 |
#else |
| 596 |
if (n_gpr == MAX_GPRARGS-1) |
| 597 |
n_gpr = MAX_GPRARGS; |
| 598 |
if (n_gpr < MAX_GPRARGS) |
| 599 |
*p_arg = &p_gpr[n_gpr], n_gpr += 2; |
| 600 |
else |
| 601 |
*p_arg = &p_ov[n_ov], n_ov += 2; |
| 602 |
#endif |
| 603 |
break; |
| 604 |
|
| 605 |
case FFI_TYPE_INT: |
| 606 |
case FFI_TYPE_UINT32: |
| 607 |
case FFI_TYPE_SINT32: |
| 608 |
if (n_gpr < MAX_GPRARGS) |
| 609 |
*p_arg = (char *)&p_gpr[n_gpr++] + sizeof (long) - 4; |
| 610 |
else |
| 611 |
*p_arg = (char *)&p_ov[n_ov++] + sizeof (long) - 4; |
| 612 |
break; |
| 613 |
|
| 614 |
case FFI_TYPE_UINT16: |
| 615 |
case FFI_TYPE_SINT16: |
| 616 |
if (n_gpr < MAX_GPRARGS) |
| 617 |
*p_arg = (char *)&p_gpr[n_gpr++] + sizeof (long) - 2; |
| 618 |
else |
| 619 |
*p_arg = (char *)&p_ov[n_ov++] + sizeof (long) - 2; |
| 620 |
break; |
| 621 |
|
| 622 |
case FFI_TYPE_UINT8: |
| 623 |
case FFI_TYPE_SINT8: |
| 624 |
if (n_gpr < MAX_GPRARGS) |
| 625 |
*p_arg = (char *)&p_gpr[n_gpr++] + sizeof (long) - 1; |
| 626 |
else |
| 627 |
*p_arg = (char *)&p_ov[n_ov++] + sizeof (long) - 1; |
| 628 |
break; |
| 629 |
|
| 630 |
default: |
| 631 |
FFI_ASSERT (0); |
| 632 |
break; |
| 633 |
} |
| 634 |
|
| 635 |
/* If this is a struct passed via pointer, we need to |
| 636 |
actually retrieve that pointer. */ |
| 637 |
if (deref_struct_pointer) |
| 638 |
*p_arg = *(void **)*p_arg; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
/* Call the target function. */ |
| 643 |
(fun) (cif, rvalue, avalue, user_data); |
| 644 |
|
| 645 |
/* Convert the return value. */ |
| 646 |
switch (cif->rtype->type) |
| 647 |
{ |
| 648 |
/* Void is easy, and so is struct. */ |
| 649 |
case FFI_TYPE_VOID: |
| 650 |
case FFI_TYPE_STRUCT: |
| 651 |
case FFI_TYPE_COMPLEX: |
| 652 |
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
| 653 |
case FFI_TYPE_LONGDOUBLE: |
| 654 |
#endif |
| 655 |
break; |
| 656 |
|
| 657 |
/* Floating point values are returned in fpr 0. */ |
| 658 |
case FFI_TYPE_FLOAT: |
| 659 |
p_fpr[0] = (long long) *(unsigned int *) rvalue << 32; |
| 660 |
break; |
| 661 |
|
| 662 |
case FFI_TYPE_DOUBLE: |
| 663 |
p_fpr[0] = *(unsigned long long *) rvalue; |
| 664 |
break; |
| 665 |
|
| 666 |
/* Integer values are returned in gpr 2 (and gpr 3 |
| 667 |
for 64-bit values on 31-bit machines). */ |
| 668 |
case FFI_TYPE_UINT64: |
| 669 |
case FFI_TYPE_SINT64: |
| 670 |
#ifdef __s390x__ |
| 671 |
p_gpr[0] = *(unsigned long *) rvalue; |
| 672 |
#else |
| 673 |
p_gpr[0] = ((unsigned long *) rvalue)[0], |
| 674 |
p_gpr[1] = ((unsigned long *) rvalue)[1]; |
| 675 |
#endif |
| 676 |
break; |
| 677 |
|
| 678 |
case FFI_TYPE_POINTER: |
| 679 |
case FFI_TYPE_UINT32: |
| 680 |
case FFI_TYPE_UINT16: |
| 681 |
case FFI_TYPE_UINT8: |
| 682 |
p_gpr[0] = *(unsigned long *) rvalue; |
| 683 |
break; |
| 684 |
|
| 685 |
case FFI_TYPE_INT: |
| 686 |
case FFI_TYPE_SINT32: |
| 687 |
case FFI_TYPE_SINT16: |
| 688 |
case FFI_TYPE_SINT8: |
| 689 |
p_gpr[0] = *(signed long *) rvalue; |
| 690 |
break; |
| 691 |
|
| 692 |
default: |
| 693 |
FFI_ASSERT (0); |
| 694 |
break; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
/*======================== End of Routine ============================*/ |
| 699 |
|
| 700 |
/*====================================================================*/ |
| 701 |
/* */ |
| 702 |
/* Name - ffi_prep_closure_loc. */ |
| 703 |
/* */ |
| 704 |
/* Function - Prepare a FFI closure. */ |
| 705 |
/* */ |
| 706 |
/*====================================================================*/ |
| 707 |
|
| 708 |
ffi_status |
| 709 |
ffi_prep_closure_loc (ffi_closure *closure, |
| 710 |
ffi_cif *cif, |
| 711 |
void (*fun) (ffi_cif *, void *, void **, void *), |
| 712 |
void *user_data, |
| 713 |
void *codeloc) |
| 714 |
{ |
| 715 |
static unsigned short const template[] = { |
| 716 |
0x0d10, /* basr %r1,0 */ |
| 717 |
#ifndef __s390x__ |
| 718 |
0x9801, 0x1006, /* lm %r0,%r1,6(%r1) */ |
| 719 |
#else |
| 720 |
0xeb01, 0x100e, 0x0004, /* lmg %r0,%r1,14(%r1) */ |
| 721 |
#endif |
| 722 |
0x07f1 /* br %r1 */ |
| 723 |
}; |
| 724 |
void (*dest)(void); |
| 725 |
unsigned long *tramp = (unsigned long *)&closure->tramp; |
| 726 |
|
| 727 |
if (cif->abi != FFI_SYSV) |
| 728 |
return FFI_BAD_ABI; |
| 729 |
|
| 730 |
|
| 731 |
#if defined(FFI_EXEC_STATIC_TRAMP) |
| 732 |
if (ffi_tramp_is_present(closure)) |
| 733 |
{ |
| 734 |
/* Initialize the static trampoline's parameters. */ |
| 735 |
dest = ffi_closure_SYSV; |
| 736 |
ffi_tramp_set_parms (closure->ftramp, dest, closure); |
| 737 |
goto out; |
| 738 |
} |
| 739 |
#endif |
| 740 |
|
| 741 |
memcpy (tramp, template, sizeof(template)); |
| 742 |
tramp[2] = (unsigned long)codeloc; |
| 743 |
tramp[3] = (unsigned long)&ffi_closure_SYSV; |
| 744 |
|
| 745 |
#if defined(FFI_EXEC_STATIC_TRAMP) |
| 746 |
out: |
| 747 |
#endif |
| 748 |
closure->cif = cif; |
| 749 |
closure->fun = fun; |
| 750 |
closure->user_data = user_data; |
| 751 |
|
| 752 |
return FFI_OK; |
| 753 |
} |
| 754 |
|
| 755 |
/*======================== End of Routine ============================*/ |
| 756 |
|
| 757 |
/* Build a Go language closure. */ |
| 758 |
|
| 759 |
ffi_status |
| 760 |
ffi_prep_go_closure (ffi_go_closure *closure, ffi_cif *cif, |
| 761 |
void (*fun)(ffi_cif*,void*,void**,void*)) |
| 762 |
{ |
| 763 |
if (cif->abi != FFI_SYSV) |
| 764 |
return FFI_BAD_ABI; |
| 765 |
|
| 766 |
closure->tramp = ffi_go_closure_SYSV; |
| 767 |
closure->cif = cif; |
| 768 |
closure->fun = fun; |
| 769 |
|
| 770 |
return FFI_OK; |
| 771 |
} |
| 772 |
|
| 773 |
#if defined(FFI_EXEC_STATIC_TRAMP) |
| 774 |
void * |
| 775 |
ffi_tramp_arch (size_t *tramp_size, size_t *map_size) |
| 776 |
{ |
| 777 |
extern void *trampoline_code_table; |
| 778 |
|
| 779 |
*tramp_size = FFI390_TRAMP_SIZE; |
| 780 |
*map_size = FFI390_TRAMP_MAP_SIZE; |
| 781 |
return &trampoline_code_table; |
| 782 |
} |
| 783 |
#endif |