| 1 |
/* ----------------------------------------------------------------------- |
| 2 |
ffi.c - Copyright (C) 2012, 2013, 2018, 2021, 2022 Anthony Green |
| 3 |
|
| 4 |
Moxie 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 |
#include <stdlib.h> |
| 31 |
|
| 32 |
/* ffi_prep_args is called by the assembly routine once stack space |
| 33 |
has been allocated for the function's arguments */ |
| 34 |
|
| 35 |
void *ffi_prep_args(char *stack, extended_cif *ecif) |
| 36 |
{ |
| 37 |
register unsigned int i; |
| 38 |
register void **p_argv; |
| 39 |
register char *argp; |
| 40 |
register ffi_type **p_arg; |
| 41 |
register int count = 0; |
| 42 |
|
| 43 |
p_argv = ecif->avalue; |
| 44 |
argp = stack; |
| 45 |
|
| 46 |
if (ecif->cif->rtype->type == FFI_TYPE_STRUCT) |
| 47 |
{ |
| 48 |
*(void **) argp = ecif->rvalue; |
| 49 |
argp += 4; |
| 50 |
} |
| 51 |
|
| 52 |
for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types; |
| 53 |
(i != 0); |
| 54 |
i--, p_arg++) |
| 55 |
{ |
| 56 |
size_t z; |
| 57 |
|
| 58 |
z = (*p_arg)->size; |
| 59 |
|
| 60 |
if ((*p_arg)->type == FFI_TYPE_STRUCT) |
| 61 |
{ |
| 62 |
z = sizeof(void*); |
| 63 |
*(void **) argp = *p_argv; |
| 64 |
} |
| 65 |
else if (z < sizeof(int)) |
| 66 |
{ |
| 67 |
z = sizeof(int); |
| 68 |
switch ((*p_arg)->type) |
| 69 |
{ |
| 70 |
case FFI_TYPE_SINT8: |
| 71 |
*(signed int *) argp = (signed int)*(SINT8 *)(* p_argv); |
| 72 |
break; |
| 73 |
|
| 74 |
case FFI_TYPE_UINT8: |
| 75 |
*(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv); |
| 76 |
break; |
| 77 |
|
| 78 |
case FFI_TYPE_SINT16: |
| 79 |
*(signed int *) argp = (signed int)*(SINT16 *)(* p_argv); |
| 80 |
break; |
| 81 |
|
| 82 |
case FFI_TYPE_UINT16: |
| 83 |
*(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv); |
| 84 |
break; |
| 85 |
|
| 86 |
default: |
| 87 |
FFI_ASSERT(0); |
| 88 |
} |
| 89 |
} |
| 90 |
else if (z == sizeof(int)) |
| 91 |
{ |
| 92 |
*(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv); |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
memcpy(argp, *p_argv, z); |
| 97 |
} |
| 98 |
p_argv++; |
| 99 |
argp += z; |
| 100 |
count += z; |
| 101 |
} |
| 102 |
|
| 103 |
return (stack + ((count > 24) ? 24 : FFI_ALIGN_DOWN(count, 8))); |
| 104 |
} |
| 105 |
|
| 106 |
/* Perform machine dependent cif processing */ |
| 107 |
ffi_status ffi_prep_cif_machdep(ffi_cif *cif) |
| 108 |
{ |
| 109 |
if (cif->rtype->type == FFI_TYPE_STRUCT) |
| 110 |
cif->flags = -1; |
| 111 |
else |
| 112 |
cif->flags = cif->rtype->size; |
| 113 |
|
| 114 |
cif->bytes = FFI_ALIGN (cif->bytes, 8); |
| 115 |
|
| 116 |
return FFI_OK; |
| 117 |
} |
| 118 |
|
| 119 |
extern void ffi_call_EABI(void *(*)(char *, extended_cif *), |
| 120 |
extended_cif *, |
| 121 |
unsigned, unsigned, |
| 122 |
unsigned *, |
| 123 |
void (*fn)(void)); |
| 124 |
|
| 125 |
void ffi_call(ffi_cif *cif, |
| 126 |
void (*fn)(void), |
| 127 |
void *rvalue, |
| 128 |
void **avalue) |
| 129 |
{ |
| 130 |
extended_cif ecif; |
| 131 |
ffi_type **arg_types = cif->arg_types; |
| 132 |
int i, nargs = cif->nargs; |
| 133 |
|
| 134 |
ecif.cif = cif; |
| 135 |
ecif.avalue = avalue; |
| 136 |
|
| 137 |
/* If the return value is a struct and we don't have a return */ |
| 138 |
/* value address then we need to make one */ |
| 139 |
|
| 140 |
if ((rvalue == NULL) && |
| 141 |
(cif->rtype->type == FFI_TYPE_STRUCT)) |
| 142 |
{ |
| 143 |
ecif.rvalue = alloca(cif->rtype->size); |
| 144 |
} |
| 145 |
else |
| 146 |
ecif.rvalue = rvalue; |
| 147 |
|
| 148 |
/* If we have any large structure arguments, make a copy so we are passing |
| 149 |
by value. */ |
| 150 |
for (i = 0; i < nargs; i++) |
| 151 |
{ |
| 152 |
ffi_type *at = arg_types[i]; |
| 153 |
int size = at->size; |
| 154 |
if (at->type == FFI_TYPE_STRUCT) /* && size > 4) All struct args?? */ |
| 155 |
{ |
| 156 |
char *argcopy = alloca (size); |
| 157 |
memcpy (argcopy, avalue[i], size); |
| 158 |
avalue[i] = argcopy; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
switch (cif->abi) |
| 163 |
{ |
| 164 |
case FFI_EABI: |
| 165 |
ffi_call_EABI(ffi_prep_args, &ecif, cif->bytes, |
| 166 |
cif->flags, ecif.rvalue, fn); |
| 167 |
break; |
| 168 |
default: |
| 169 |
FFI_ASSERT(0); |
| 170 |
break; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void ffi_closure_eabi (unsigned arg1, unsigned arg2, unsigned arg3, |
| 175 |
unsigned arg4, unsigned arg5, unsigned arg6) |
| 176 |
{ |
| 177 |
/* This function is called by a trampoline. The trampoline stows a |
| 178 |
pointer to the ffi_closure object in $r12. We must save this |
| 179 |
pointer in a place that will persist while we do our work. */ |
| 180 |
register ffi_closure *creg __asm__ ("$r12"); |
| 181 |
ffi_closure *closure = creg; |
| 182 |
|
| 183 |
/* Arguments that don't fit in registers are found on the stack |
| 184 |
at a fixed offset above the current frame pointer. */ |
| 185 |
register char *frame_pointer __asm__ ("$fp"); |
| 186 |
|
| 187 |
/* Pointer to a struct return value. */ |
| 188 |
void *struct_rvalue = (void *) arg1; |
| 189 |
|
| 190 |
/* 6 words reserved for register args + 3 words from jsr */ |
| 191 |
char *stack_args = frame_pointer + 9*4; |
| 192 |
|
| 193 |
/* Lay the register arguments down in a continuous chunk of memory. */ |
| 194 |
unsigned register_args[6] = |
| 195 |
{ arg1, arg2, arg3, arg4, arg5, arg6 }; |
| 196 |
char *register_args_ptr = (char *) register_args; |
| 197 |
|
| 198 |
ffi_cif *cif = closure->cif; |
| 199 |
ffi_type **arg_types = cif->arg_types; |
| 200 |
void **avalue = alloca (cif->nargs * sizeof(void *)); |
| 201 |
char *ptr = (char *) register_args; |
| 202 |
int i; |
| 203 |
|
| 204 |
/* preserve struct type return pointer passing */ |
| 205 |
if ((cif->rtype != NULL) && (cif->rtype->type == FFI_TYPE_STRUCT)) { |
| 206 |
ptr += 4; |
| 207 |
register_args_ptr = (char *)®ister_args[1]; |
| 208 |
} |
| 209 |
|
| 210 |
/* Find the address of each argument. */ |
| 211 |
for (i = 0; i < cif->nargs; i++) |
| 212 |
{ |
| 213 |
switch (arg_types[i]->type) |
| 214 |
{ |
| 215 |
case FFI_TYPE_SINT8: |
| 216 |
case FFI_TYPE_UINT8: |
| 217 |
avalue[i] = ptr + 3; |
| 218 |
break; |
| 219 |
case FFI_TYPE_SINT16: |
| 220 |
case FFI_TYPE_UINT16: |
| 221 |
avalue[i] = ptr + 2; |
| 222 |
break; |
| 223 |
case FFI_TYPE_SINT32: |
| 224 |
case FFI_TYPE_UINT32: |
| 225 |
case FFI_TYPE_FLOAT: |
| 226 |
case FFI_TYPE_POINTER: |
| 227 |
avalue[i] = ptr; |
| 228 |
break; |
| 229 |
case FFI_TYPE_STRUCT: |
| 230 |
{ |
| 231 |
if (arg_types[i]->size > 4) |
| 232 |
{ |
| 233 |
void *copy = alloca(arg_types[i]->size); |
| 234 |
memcpy(copy, *(void**)ptr, arg_types[i]->size); |
| 235 |
avalue[i] = copy; |
| 236 |
} |
| 237 |
else |
| 238 |
avalue[i] = *(void**)ptr; |
| 239 |
} |
| 240 |
break; |
| 241 |
default: |
| 242 |
/* This is an 8-byte value. */ |
| 243 |
if (ptr == (char *) ®ister_args[5]) |
| 244 |
{ |
| 245 |
/* The value is split across two locations */ |
| 246 |
unsigned *ip = alloca(8); |
| 247 |
avalue[i] = ip; |
| 248 |
ip[0] = *(unsigned *) ptr; |
| 249 |
ip[1] = *(unsigned *) stack_args; |
| 250 |
} |
| 251 |
else |
| 252 |
{ |
| 253 |
avalue[i] = ptr; |
| 254 |
} |
| 255 |
ptr += 4; |
| 256 |
break; |
| 257 |
} |
| 258 |
ptr += 4; |
| 259 |
|
| 260 |
/* If we've handled more arguments than fit in registers, |
| 261 |
start looking at the those passed on the stack. */ |
| 262 |
if (ptr == (char *) ®ister_args[6]) |
| 263 |
ptr = stack_args; |
| 264 |
else if (ptr == (char *) ®ister_args[7]) |
| 265 |
ptr = stack_args + 4; |
| 266 |
} |
| 267 |
|
| 268 |
/* Invoke the closure. */ |
| 269 |
if (cif->rtype && (cif->rtype->type == FFI_TYPE_STRUCT)) |
| 270 |
{ |
| 271 |
(closure->fun) (cif, struct_rvalue, avalue, closure->user_data); |
| 272 |
} |
| 273 |
else |
| 274 |
{ |
| 275 |
/* Allocate space for the return value and call the function. */ |
| 276 |
long long rvalue; |
| 277 |
(closure->fun) (cif, &rvalue, avalue, closure->user_data); |
| 278 |
asm ("mov $r12, %0\n ld.l $r0, ($r12)\n ldo.l $r1, 4($r12)" : : "r" (&rvalue)); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
ffi_status |
| 283 |
ffi_prep_closure_loc (ffi_closure* closure, |
| 284 |
ffi_cif* cif, |
| 285 |
void (*fun)(ffi_cif*, void*, void**, void*), |
| 286 |
void *user_data, |
| 287 |
void *codeloc) |
| 288 |
{ |
| 289 |
unsigned short *tramp = (unsigned short *) &closure->tramp[0]; |
| 290 |
unsigned long fn = (long) ffi_closure_eabi; |
| 291 |
unsigned long cls = (long) codeloc; |
| 292 |
|
| 293 |
if (cif->abi != FFI_EABI) |
| 294 |
return FFI_BAD_ABI; |
| 295 |
|
| 296 |
fn = (unsigned long) ffi_closure_eabi; |
| 297 |
|
| 298 |
tramp[0] = 0x01e0; /* ldi.l $r12, .... */ |
| 299 |
tramp[1] = cls >> 16; |
| 300 |
tramp[2] = cls & 0xffff; |
| 301 |
tramp[3] = 0x1a00; /* jmpa .... */ |
| 302 |
tramp[4] = fn >> 16; |
| 303 |
tramp[5] = fn & 0xffff; |
| 304 |
|
| 305 |
closure->cif = cif; |
| 306 |
closure->fun = fun; |
| 307 |
closure->user_data = user_data; |
| 308 |
|
| 309 |
return FFI_OK; |
| 310 |
} |