| 1 |
/* ----------------------------------------------------------------------- |
| 2 |
ffi.c - Copyright (c) 2014 Sebastian Macke <[email protected]> |
| 3 |
|
| 4 |
OpenRISC 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 |
/* ffi_prep_args is called by the assembly routine once stack space |
| 31 |
has been allocated for the function's arguments */ |
| 32 |
|
| 33 |
void* ffi_prep_args(char *stack, extended_cif *ecif) |
| 34 |
{ |
| 35 |
char *stacktemp = stack; |
| 36 |
int i, s; |
| 37 |
ffi_type **arg; |
| 38 |
int count = 0; |
| 39 |
int nfixedargs; |
| 40 |
|
| 41 |
nfixedargs = ecif->cif->nfixedargs; |
| 42 |
arg = ecif->cif->arg_types; |
| 43 |
void **argv = ecif->avalue; |
| 44 |
|
| 45 |
if (ecif->cif->rtype->type == FFI_TYPE_STRUCT) |
| 46 |
{ |
| 47 |
*(void **) stack = ecif->rvalue; |
| 48 |
stack += 4; |
| 49 |
count = 4; |
| 50 |
} |
| 51 |
for(i=0; i<ecif->cif->nargs; i++) |
| 52 |
{ |
| 53 |
|
| 54 |
/* variadic args are saved on stack */ |
| 55 |
if ((nfixedargs == 0) && (count < 24)) |
| 56 |
{ |
| 57 |
count = 24; |
| 58 |
stack = stacktemp + 24; |
| 59 |
} |
| 60 |
nfixedargs--; |
| 61 |
|
| 62 |
s = 4; |
| 63 |
switch((*arg)->type) |
| 64 |
{ |
| 65 |
case FFI_TYPE_STRUCT: |
| 66 |
*(void **)stack = *argv; |
| 67 |
break; |
| 68 |
|
| 69 |
case FFI_TYPE_SINT8: |
| 70 |
*(signed int *) stack = (signed int)*(SINT8 *)(* argv); |
| 71 |
break; |
| 72 |
|
| 73 |
case FFI_TYPE_UINT8: |
| 74 |
*(unsigned int *) stack = (unsigned int)*(UINT8 *)(* argv); |
| 75 |
break; |
| 76 |
|
| 77 |
case FFI_TYPE_SINT16: |
| 78 |
*(signed int *) stack = (signed int)*(SINT16 *)(* argv); |
| 79 |
break; |
| 80 |
|
| 81 |
case FFI_TYPE_UINT16: |
| 82 |
*(unsigned int *) stack = (unsigned int)*(UINT16 *)(* argv); |
| 83 |
break; |
| 84 |
|
| 85 |
case FFI_TYPE_SINT32: |
| 86 |
case FFI_TYPE_UINT32: |
| 87 |
case FFI_TYPE_FLOAT: |
| 88 |
case FFI_TYPE_POINTER: |
| 89 |
*(int *)stack = *(int*)(*argv); |
| 90 |
break; |
| 91 |
|
| 92 |
default: /* 8 byte types */ |
| 93 |
if (count == 20) /* never split arguments */ |
| 94 |
{ |
| 95 |
stack += 4; |
| 96 |
count += 4; |
| 97 |
} |
| 98 |
s = (*arg)->size; |
| 99 |
memcpy(stack, *argv, s); |
| 100 |
break; |
| 101 |
} |
| 102 |
|
| 103 |
stack += s; |
| 104 |
count += s; |
| 105 |
argv++; |
| 106 |
arg++; |
| 107 |
} |
| 108 |
return stacktemp + ((count>24)?24:0); |
| 109 |
} |
| 110 |
|
| 111 |
extern void ffi_call_SYSV(unsigned, |
| 112 |
extended_cif *, |
| 113 |
void *(*)(char *, extended_cif *), |
| 114 |
unsigned *, |
| 115 |
void (*fn)(void), |
| 116 |
unsigned); |
| 117 |
|
| 118 |
|
| 119 |
void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue) |
| 120 |
{ |
| 121 |
int i; |
| 122 |
int size; |
| 123 |
ffi_type **arg; |
| 124 |
|
| 125 |
/* Calculate size to allocate on stack */ |
| 126 |
|
| 127 |
for(i = 0, arg = cif->arg_types, size=0; i < cif->nargs; i++, arg++) |
| 128 |
{ |
| 129 |
if ((*arg)->type == FFI_TYPE_STRUCT) |
| 130 |
size += 4; |
| 131 |
else |
| 132 |
if ((*arg)->size <= 4) |
| 133 |
size += 4; |
| 134 |
else |
| 135 |
size += 8; |
| 136 |
|
| 137 |
/* If we have any large structure arguments, make a copy so we are passing |
| 138 |
by value. */ |
| 139 |
{ |
| 140 |
ffi_type *at = cif->arg_types[i]; |
| 141 |
int size = at->size; |
| 142 |
if (at->type == FFI_TYPE_STRUCT) /* && size > 4) All struct args? */ |
| 143 |
{ |
| 144 |
char *argcopy = alloca (size); |
| 145 |
memcpy (argcopy, avalue[i], size); |
| 146 |
avalue[i] = argcopy; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/* for variadic functions more space is needed on the stack */ |
| 152 |
if (cif->nargs != cif->nfixedargs) |
| 153 |
size += 24; |
| 154 |
|
| 155 |
if (cif->rtype->type == FFI_TYPE_STRUCT) |
| 156 |
size += 4; |
| 157 |
|
| 158 |
|
| 159 |
extended_cif ecif; |
| 160 |
ecif.cif = cif; |
| 161 |
ecif.avalue = avalue; |
| 162 |
ecif.rvalue = rvalue; |
| 163 |
|
| 164 |
switch (cif->abi) |
| 165 |
{ |
| 166 |
case FFI_SYSV: |
| 167 |
ffi_call_SYSV(size, &ecif, ffi_prep_args, rvalue, fn, cif->flags); |
| 168 |
break; |
| 169 |
default: |
| 170 |
FFI_ASSERT(0); |
| 171 |
break; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
void ffi_closure_SYSV(unsigned long r3, unsigned long r4, unsigned long r5, |
| 177 |
unsigned long r6, unsigned long r7, unsigned long r8) |
| 178 |
{ |
| 179 |
register int *sp __asm__ ("r17"); |
| 180 |
register int *r13 __asm__ ("r13"); |
| 181 |
|
| 182 |
ffi_closure* closure = (ffi_closure*) r13; |
| 183 |
char *stack_args = (char*) sp; |
| 184 |
|
| 185 |
/* Lay the register arguments down in a continuous chunk of memory. */ |
| 186 |
unsigned register_args[6] = |
| 187 |
{ r3, r4, r5, r6, r7, r8 }; |
| 188 |
|
| 189 |
/* Pointer to a struct return value. */ |
| 190 |
void *struct_rvalue = (void *) r3; |
| 191 |
|
| 192 |
ffi_cif *cif = closure->cif; |
| 193 |
ffi_type **arg_types = cif->arg_types; |
| 194 |
void **avalue = alloca (cif->nargs * sizeof(void *)); |
| 195 |
char *ptr = (char *) register_args; |
| 196 |
int count = 0; |
| 197 |
int nfixedargs = cif->nfixedargs; |
| 198 |
int i; |
| 199 |
|
| 200 |
/* preserve struct type return pointer passing */ |
| 201 |
|
| 202 |
if ((cif->rtype != NULL) && (cif->rtype->type == FFI_TYPE_STRUCT)) |
| 203 |
{ |
| 204 |
ptr += 4; |
| 205 |
count = 4; |
| 206 |
} |
| 207 |
|
| 208 |
/* Find the address of each argument. */ |
| 209 |
for (i = 0; i < cif->nargs; i++) |
| 210 |
{ |
| 211 |
|
| 212 |
/* variadic args are saved on stack */ |
| 213 |
if ((nfixedargs == 0) && (count < 24)) |
| 214 |
{ |
| 215 |
ptr = stack_args; |
| 216 |
count = 24; |
| 217 |
} |
| 218 |
nfixedargs--; |
| 219 |
|
| 220 |
switch (arg_types[i]->type) |
| 221 |
{ |
| 222 |
case FFI_TYPE_SINT8: |
| 223 |
case FFI_TYPE_UINT8: |
| 224 |
avalue[i] = ptr + 3; |
| 225 |
break; |
| 226 |
|
| 227 |
case FFI_TYPE_SINT16: |
| 228 |
case FFI_TYPE_UINT16: |
| 229 |
avalue[i] = ptr + 2; |
| 230 |
break; |
| 231 |
|
| 232 |
case FFI_TYPE_SINT32: |
| 233 |
case FFI_TYPE_UINT32: |
| 234 |
case FFI_TYPE_FLOAT: |
| 235 |
case FFI_TYPE_POINTER: |
| 236 |
avalue[i] = ptr; |
| 237 |
break; |
| 238 |
|
| 239 |
case FFI_TYPE_STRUCT: |
| 240 |
avalue[i] = *(void**)ptr; |
| 241 |
break; |
| 242 |
|
| 243 |
default: |
| 244 |
/* 8-byte values */ |
| 245 |
|
| 246 |
/* arguments are never splitted */ |
| 247 |
if (ptr == ®ister_args[5]) |
| 248 |
ptr = stack_args; |
| 249 |
avalue[i] = ptr; |
| 250 |
ptr += 4; |
| 251 |
count += 4; |
| 252 |
break; |
| 253 |
} |
| 254 |
ptr += 4; |
| 255 |
count += 4; |
| 256 |
|
| 257 |
/* If we've handled more arguments than fit in registers, |
| 258 |
start looking at the those passed on the stack. */ |
| 259 |
|
| 260 |
if (count == 24) |
| 261 |
ptr = stack_args; |
| 262 |
} |
| 263 |
|
| 264 |
if (cif->rtype && (cif->rtype->type == FFI_TYPE_STRUCT)) |
| 265 |
{ |
| 266 |
(closure->fun) (cif, struct_rvalue, avalue, closure->user_data); |
| 267 |
} else |
| 268 |
{ |
| 269 |
long long rvalue; |
| 270 |
(closure->fun) (cif, &rvalue, avalue, closure->user_data); |
| 271 |
if (cif->rtype) |
| 272 |
asm ("l.ori r12, %0, 0x0\n l.lwz r11, 0(r12)\n l.lwz r12, 4(r12)" : : "r" (&rvalue)); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
ffi_status |
| 278 |
ffi_prep_closure_loc (ffi_closure* closure, |
| 279 |
ffi_cif* cif, |
| 280 |
void (*fun)(ffi_cif*,void*,void**,void*), |
| 281 |
void *user_data, |
| 282 |
void *codeloc) |
| 283 |
{ |
| 284 |
unsigned short *tramp = (unsigned short *) closure->tramp; |
| 285 |
unsigned long fn = (unsigned long) ffi_closure_SYSV; |
| 286 |
unsigned long cls = (unsigned long) codeloc; |
| 287 |
|
| 288 |
if (cif->abi != FFI_SYSV) |
| 289 |
return FFI_BAD_ABI; |
| 290 |
|
| 291 |
closure->cif = cif; |
| 292 |
closure->user_data = user_data; |
| 293 |
closure->fun = fun; |
| 294 |
|
| 295 |
/* write pointers to temporary registers */ |
| 296 |
tramp[0] = (0x6 << 10) | (13 << 5); /* l.movhi r13, ... */ |
| 297 |
tramp[1] = cls >> 16; |
| 298 |
tramp[2] = (0x2a << 10) | (13 << 5) | 13; /* l.ori r13, r13, ... */ |
| 299 |
tramp[3] = cls & 0xFFFF; |
| 300 |
|
| 301 |
tramp[4] = (0x6 << 10) | (15 << 5); /* l.movhi r15, ... */ |
| 302 |
tramp[5] = fn >> 16; |
| 303 |
tramp[6] = (0x2a << 10) | (15 << 5) | 15; /* l.ori r15, r15 ... */ |
| 304 |
tramp[7] = fn & 0xFFFF; |
| 305 |
|
| 306 |
tramp[8] = (0x11 << 10); /* l.jr r15 */ |
| 307 |
tramp[9] = 15 << 11; |
| 308 |
|
| 309 |
tramp[10] = (0x2a << 10) | (17 << 5) | 1; /* l.ori r17, r1, ... */ |
| 310 |
tramp[11] = 0x0; |
| 311 |
|
| 312 |
return FFI_OK; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
ffi_status ffi_prep_cif_machdep (ffi_cif *cif) |
| 317 |
{ |
| 318 |
cif->flags = 0; |
| 319 |
|
| 320 |
/* structures are returned as pointers */ |
| 321 |
if (cif->rtype->type == FFI_TYPE_STRUCT) |
| 322 |
cif->flags = FFI_TYPE_STRUCT; |
| 323 |
else |
| 324 |
if (cif->rtype->size > 4) |
| 325 |
cif->flags = FFI_TYPE_UINT64; |
| 326 |
|
| 327 |
cif->nfixedargs = cif->nargs; |
| 328 |
|
| 329 |
return FFI_OK; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
ffi_status ffi_prep_cif_machdep_var(ffi_cif *cif, |
| 334 |
unsigned int nfixedargs, unsigned int ntotalargs) |
| 335 |
{ |
| 336 |
ffi_status status; |
| 337 |
|
| 338 |
status = ffi_prep_cif_machdep (cif); |
| 339 |
cif->nfixedargs = nfixedargs; |
| 340 |
return status; |
| 341 |
} |