| 1 | n/a | /* ----------------------------------------------------------------------- |
|---|
| 2 | n/a | ffi.c - Copyright (C) 2013 IBM |
|---|
| 3 | n/a | Copyright (C) 2011 Anthony Green |
|---|
| 4 | n/a | Copyright (C) 2011 Kyle Moffett |
|---|
| 5 | n/a | Copyright (C) 2008 Red Hat, Inc |
|---|
| 6 | n/a | Copyright (C) 2007, 2008 Free Software Foundation, Inc |
|---|
| 7 | n/a | Copyright (c) 1998 Geoffrey Keating |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | PowerPC Foreign Function Interface |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | Permission is hereby granted, free of charge, to any person obtaining |
|---|
| 12 | n/a | a copy of this software and associated documentation files (the |
|---|
| 13 | n/a | ``Software''), to deal in the Software without restriction, including |
|---|
| 14 | n/a | without limitation the rights to use, copy, modify, merge, publish, |
|---|
| 15 | n/a | distribute, sublicense, and/or sell copies of the Software, and to |
|---|
| 16 | n/a | permit persons to whom the Software is furnished to do so, subject to |
|---|
| 17 | n/a | the following conditions: |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | The above copyright notice and this permission notice shall be included |
|---|
| 20 | n/a | in all copies or substantial portions of the Software. |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS |
|---|
| 23 | n/a | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|---|
| 24 | n/a | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|---|
| 25 | n/a | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR |
|---|
| 26 | n/a | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
|---|
| 27 | n/a | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|---|
| 28 | n/a | OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 29 | n/a | ----------------------------------------------------------------------- */ |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | #include "ffi.h" |
|---|
| 32 | n/a | #include "ffi_common.h" |
|---|
| 33 | n/a | #include "ffi_powerpc.h" |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | #if HAVE_LONG_DOUBLE_VARIANT |
|---|
| 36 | n/a | /* Adjust ffi_type_longdouble. */ |
|---|
| 37 | n/a | void FFI_HIDDEN |
|---|
| 38 | n/a | ffi_prep_types (ffi_abi abi) |
|---|
| 39 | n/a | { |
|---|
| 40 | n/a | # if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
|---|
| 41 | n/a | # ifdef POWERPC64 |
|---|
| 42 | n/a | ffi_prep_types_linux64 (abi); |
|---|
| 43 | n/a | # else |
|---|
| 44 | n/a | ffi_prep_types_sysv (abi); |
|---|
| 45 | n/a | # endif |
|---|
| 46 | n/a | # endif |
|---|
| 47 | n/a | } |
|---|
| 48 | n/a | #endif |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | /* Perform machine dependent cif processing */ |
|---|
| 51 | n/a | ffi_status FFI_HIDDEN |
|---|
| 52 | n/a | ffi_prep_cif_machdep (ffi_cif *cif) |
|---|
| 53 | n/a | { |
|---|
| 54 | n/a | #ifdef POWERPC64 |
|---|
| 55 | n/a | return ffi_prep_cif_linux64 (cif); |
|---|
| 56 | n/a | #else |
|---|
| 57 | n/a | return ffi_prep_cif_sysv (cif); |
|---|
| 58 | n/a | #endif |
|---|
| 59 | n/a | } |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | ffi_status FFI_HIDDEN |
|---|
| 62 | n/a | ffi_prep_cif_machdep_var (ffi_cif *cif, |
|---|
| 63 | n/a | unsigned int nfixedargs MAYBE_UNUSED, |
|---|
| 64 | n/a | unsigned int ntotalargs MAYBE_UNUSED) |
|---|
| 65 | n/a | { |
|---|
| 66 | n/a | #ifdef POWERPC64 |
|---|
| 67 | n/a | return ffi_prep_cif_linux64_var (cif, nfixedargs, ntotalargs); |
|---|
| 68 | n/a | #else |
|---|
| 69 | n/a | return ffi_prep_cif_sysv (cif); |
|---|
| 70 | n/a | #endif |
|---|
| 71 | n/a | } |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | void |
|---|
| 74 | n/a | ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue) |
|---|
| 75 | n/a | { |
|---|
| 76 | n/a | /* The final SYSV ABI says that structures smaller or equal 8 bytes |
|---|
| 77 | n/a | are returned in r3/r4. A draft ABI used by linux instead returns |
|---|
| 78 | n/a | them in memory. |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | We bounce-buffer SYSV small struct return values so that sysv.S |
|---|
| 81 | n/a | can write r3 and r4 to memory without worrying about struct size. |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | For ELFv2 ABI, use a bounce buffer for homogeneous structs too, |
|---|
| 84 | n/a | for similar reasons. */ |
|---|
| 85 | n/a | unsigned long smst_buffer[8]; |
|---|
| 86 | n/a | extended_cif ecif; |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | ecif.cif = cif; |
|---|
| 89 | n/a | ecif.avalue = avalue; |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | ecif.rvalue = rvalue; |
|---|
| 92 | n/a | if ((cif->flags & FLAG_RETURNS_SMST) != 0) |
|---|
| 93 | n/a | ecif.rvalue = smst_buffer; |
|---|
| 94 | n/a | /* Ensure that we have a valid struct return value. |
|---|
| 95 | n/a | FIXME: Isn't this just papering over a user problem? */ |
|---|
| 96 | n/a | else if (!rvalue && cif->rtype->type == FFI_TYPE_STRUCT) |
|---|
| 97 | n/a | ecif.rvalue = alloca (cif->rtype->size); |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | #ifdef POWERPC64 |
|---|
| 100 | n/a | ffi_call_LINUX64 (&ecif, -(long) cif->bytes, cif->flags, ecif.rvalue, fn); |
|---|
| 101 | n/a | #else |
|---|
| 102 | n/a | ffi_call_SYSV (&ecif, -cif->bytes, cif->flags, ecif.rvalue, fn); |
|---|
| 103 | n/a | #endif |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | /* Check for a bounce-buffered return value */ |
|---|
| 106 | n/a | if (rvalue && ecif.rvalue == smst_buffer) |
|---|
| 107 | n/a | { |
|---|
| 108 | n/a | unsigned int rsize = cif->rtype->size; |
|---|
| 109 | n/a | #ifndef __LITTLE_ENDIAN__ |
|---|
| 110 | n/a | /* The SYSV ABI returns a structure of up to 4 bytes in size |
|---|
| 111 | n/a | left-padded in r3. */ |
|---|
| 112 | n/a | # ifndef POWERPC64 |
|---|
| 113 | n/a | if (rsize <= 4) |
|---|
| 114 | n/a | memcpy (rvalue, (char *) smst_buffer + 4 - rsize, rsize); |
|---|
| 115 | n/a | else |
|---|
| 116 | n/a | # endif |
|---|
| 117 | n/a | /* The SYSV ABI returns a structure of up to 8 bytes in size |
|---|
| 118 | n/a | left-padded in r3/r4, and the ELFv2 ABI similarly returns a |
|---|
| 119 | n/a | structure of up to 8 bytes in size left-padded in r3. */ |
|---|
| 120 | n/a | if (rsize <= 8) |
|---|
| 121 | n/a | memcpy (rvalue, (char *) smst_buffer + 8 - rsize, rsize); |
|---|
| 122 | n/a | else |
|---|
| 123 | n/a | #endif |
|---|
| 124 | n/a | memcpy (rvalue, smst_buffer, rsize); |
|---|
| 125 | n/a | } |
|---|
| 126 | n/a | } |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | ffi_status |
|---|
| 130 | n/a | ffi_prep_closure_loc (ffi_closure *closure, |
|---|
| 131 | n/a | ffi_cif *cif, |
|---|
| 132 | n/a | void (*fun) (ffi_cif *, void *, void **, void *), |
|---|
| 133 | n/a | void *user_data, |
|---|
| 134 | n/a | void *codeloc) |
|---|
| 135 | n/a | { |
|---|
| 136 | n/a | #ifdef POWERPC64 |
|---|
| 137 | n/a | return ffi_prep_closure_loc_linux64 (closure, cif, fun, user_data, codeloc); |
|---|
| 138 | n/a | #else |
|---|
| 139 | n/a | return ffi_prep_closure_loc_sysv (closure, cif, fun, user_data, codeloc); |
|---|
| 140 | n/a | #endif |
|---|
| 141 | n/a | } |
|---|