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