1 | n/a | /* ----------------------------------------------------------------------- |
---|
2 | n/a | ffi.c - Copyright (c) 2013 Tensilica, Inc. |
---|
3 | n/a | |
---|
4 | n/a | XTENSA Foreign Function Interface |
---|
5 | n/a | |
---|
6 | n/a | Permission is hereby granted, free of charge, to any person obtaining |
---|
7 | n/a | a copy of this software and associated documentation files (the |
---|
8 | n/a | ``Software''), to deal in the Software without restriction, including |
---|
9 | n/a | without limitation the rights to use, copy, modify, merge, publish, |
---|
10 | n/a | distribute, sublicense, and/or sell copies of the Software, and to |
---|
11 | n/a | permit persons to whom the Software is furnished to do so, subject to |
---|
12 | n/a | the following conditions: |
---|
13 | n/a | |
---|
14 | n/a | The above copyright notice and this permission notice shall be included |
---|
15 | n/a | in all copies or substantial portions of the Software. |
---|
16 | n/a | |
---|
17 | n/a | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, |
---|
18 | n/a | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
---|
19 | n/a | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
---|
20 | n/a | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
---|
21 | n/a | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
---|
22 | n/a | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
23 | n/a | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
24 | n/a | DEALINGS IN THE SOFTWARE. |
---|
25 | n/a | ----------------------------------------------------------------------- */ |
---|
26 | n/a | |
---|
27 | n/a | #include <ffi.h> |
---|
28 | n/a | #include <ffi_common.h> |
---|
29 | n/a | |
---|
30 | n/a | /* |
---|
31 | n/a | |----------------------------------------| |
---|
32 | n/a | | | |
---|
33 | n/a | on entry to ffi_call ----> |----------------------------------------| |
---|
34 | n/a | | caller stack frame for registers a0-a3 | |
---|
35 | n/a | |----------------------------------------| |
---|
36 | n/a | | | |
---|
37 | n/a | | additional arguments | |
---|
38 | n/a | entry of the function ---> |----------------------------------------| |
---|
39 | n/a | | copy of function arguments a2-a7 | |
---|
40 | n/a | | - - - - - - - - - - - - - | |
---|
41 | n/a | | | |
---|
42 | n/a | |
---|
43 | n/a | The area below the entry line becomes the new stack frame for the function. |
---|
44 | n/a | |
---|
45 | n/a | */ |
---|
46 | n/a | |
---|
47 | n/a | |
---|
48 | n/a | #define FFI_TYPE_STRUCT_REGS FFI_TYPE_LAST |
---|
49 | n/a | |
---|
50 | n/a | |
---|
51 | n/a | extern void ffi_call_SYSV(void *rvalue, unsigned rsize, unsigned flags, |
---|
52 | n/a | void(*fn)(void), unsigned nbytes, extended_cif*); |
---|
53 | n/a | extern void ffi_closure_SYSV(void) FFI_HIDDEN; |
---|
54 | n/a | |
---|
55 | n/a | ffi_status ffi_prep_cif_machdep(ffi_cif *cif) |
---|
56 | n/a | { |
---|
57 | n/a | switch(cif->rtype->type) { |
---|
58 | n/a | case FFI_TYPE_SINT8: |
---|
59 | n/a | case FFI_TYPE_UINT8: |
---|
60 | n/a | case FFI_TYPE_SINT16: |
---|
61 | n/a | case FFI_TYPE_UINT16: |
---|
62 | n/a | cif->flags = cif->rtype->type; |
---|
63 | n/a | break; |
---|
64 | n/a | case FFI_TYPE_VOID: |
---|
65 | n/a | case FFI_TYPE_FLOAT: |
---|
66 | n/a | cif->flags = FFI_TYPE_UINT32; |
---|
67 | n/a | break; |
---|
68 | n/a | case FFI_TYPE_DOUBLE: |
---|
69 | n/a | case FFI_TYPE_UINT64: |
---|
70 | n/a | case FFI_TYPE_SINT64: |
---|
71 | n/a | cif->flags = FFI_TYPE_UINT64; // cif->rtype->type; |
---|
72 | n/a | break; |
---|
73 | n/a | case FFI_TYPE_STRUCT: |
---|
74 | n/a | cif->flags = FFI_TYPE_STRUCT; //_REGS; |
---|
75 | n/a | /* Up to 16 bytes are returned in registers */ |
---|
76 | n/a | if (cif->rtype->size > 4 * 4) { |
---|
77 | n/a | /* returned structure is referenced by a register; use 8 bytes |
---|
78 | n/a | (including 4 bytes for potential additional alignment) */ |
---|
79 | n/a | cif->flags = FFI_TYPE_STRUCT; |
---|
80 | n/a | cif->bytes += 8; |
---|
81 | n/a | } |
---|
82 | n/a | break; |
---|
83 | n/a | |
---|
84 | n/a | default: |
---|
85 | n/a | cif->flags = FFI_TYPE_UINT32; |
---|
86 | n/a | break; |
---|
87 | n/a | } |
---|
88 | n/a | |
---|
89 | n/a | /* Round the stack up to a full 4 register frame, just in case |
---|
90 | n/a | (we use this size in movsp). This way, it's also a multiple of |
---|
91 | n/a | 8 bytes for 64-bit arguments. */ |
---|
92 | n/a | cif->bytes = ALIGN(cif->bytes, 16); |
---|
93 | n/a | |
---|
94 | n/a | return FFI_OK; |
---|
95 | n/a | } |
---|
96 | n/a | |
---|
97 | n/a | void ffi_prep_args(extended_cif *ecif, unsigned char* stack) |
---|
98 | n/a | { |
---|
99 | n/a | unsigned int i; |
---|
100 | n/a | unsigned long *addr; |
---|
101 | n/a | ffi_type **ptr; |
---|
102 | n/a | |
---|
103 | n/a | union { |
---|
104 | n/a | void **v; |
---|
105 | n/a | char **c; |
---|
106 | n/a | signed char **sc; |
---|
107 | n/a | unsigned char **uc; |
---|
108 | n/a | signed short **ss; |
---|
109 | n/a | unsigned short **us; |
---|
110 | n/a | unsigned int **i; |
---|
111 | n/a | long long **ll; |
---|
112 | n/a | float **f; |
---|
113 | n/a | double **d; |
---|
114 | n/a | } p_argv; |
---|
115 | n/a | |
---|
116 | n/a | /* Verify that everything is aligned up properly */ |
---|
117 | n/a | FFI_ASSERT (((unsigned long) stack & 0x7) == 0); |
---|
118 | n/a | |
---|
119 | n/a | p_argv.v = ecif->avalue; |
---|
120 | n/a | addr = (unsigned long*)stack; |
---|
121 | n/a | |
---|
122 | n/a | /* structures with a size greater than 16 bytes are passed in memory */ |
---|
123 | n/a | if (ecif->cif->rtype->type == FFI_TYPE_STRUCT && ecif->cif->rtype->size > 16) |
---|
124 | n/a | { |
---|
125 | n/a | *addr++ = (unsigned long)ecif->rvalue; |
---|
126 | n/a | } |
---|
127 | n/a | |
---|
128 | n/a | for (i = ecif->cif->nargs, ptr = ecif->cif->arg_types; |
---|
129 | n/a | i > 0; |
---|
130 | n/a | i--, ptr++, p_argv.v++) |
---|
131 | n/a | { |
---|
132 | n/a | switch ((*ptr)->type) |
---|
133 | n/a | { |
---|
134 | n/a | case FFI_TYPE_SINT8: |
---|
135 | n/a | *addr++ = **p_argv.sc; |
---|
136 | n/a | break; |
---|
137 | n/a | case FFI_TYPE_UINT8: |
---|
138 | n/a | *addr++ = **p_argv.uc; |
---|
139 | n/a | break; |
---|
140 | n/a | case FFI_TYPE_SINT16: |
---|
141 | n/a | *addr++ = **p_argv.ss; |
---|
142 | n/a | break; |
---|
143 | n/a | case FFI_TYPE_UINT16: |
---|
144 | n/a | *addr++ = **p_argv.us; |
---|
145 | n/a | break; |
---|
146 | n/a | case FFI_TYPE_FLOAT: |
---|
147 | n/a | case FFI_TYPE_INT: |
---|
148 | n/a | case FFI_TYPE_UINT32: |
---|
149 | n/a | case FFI_TYPE_SINT32: |
---|
150 | n/a | case FFI_TYPE_POINTER: |
---|
151 | n/a | *addr++ = **p_argv.i; |
---|
152 | n/a | break; |
---|
153 | n/a | case FFI_TYPE_DOUBLE: |
---|
154 | n/a | case FFI_TYPE_UINT64: |
---|
155 | n/a | case FFI_TYPE_SINT64: |
---|
156 | n/a | if (((unsigned long)addr & 4) != 0) |
---|
157 | n/a | addr++; |
---|
158 | n/a | *(unsigned long long*)addr = **p_argv.ll; |
---|
159 | n/a | addr += sizeof(unsigned long long) / sizeof (addr); |
---|
160 | n/a | break; |
---|
161 | n/a | |
---|
162 | n/a | case FFI_TYPE_STRUCT: |
---|
163 | n/a | { |
---|
164 | n/a | unsigned long offs; |
---|
165 | n/a | unsigned long size; |
---|
166 | n/a | |
---|
167 | n/a | if (((unsigned long)addr & 4) != 0 && (*ptr)->alignment > 4) |
---|
168 | n/a | addr++; |
---|
169 | n/a | |
---|
170 | n/a | offs = (unsigned long) addr - (unsigned long) stack; |
---|
171 | n/a | size = (*ptr)->size; |
---|
172 | n/a | |
---|
173 | n/a | /* Entire structure must fit the argument registers or referenced */ |
---|
174 | n/a | if (offs < FFI_REGISTER_NARGS * 4 |
---|
175 | n/a | && offs + size > FFI_REGISTER_NARGS * 4) |
---|
176 | n/a | addr = (unsigned long*) (stack + FFI_REGISTER_NARGS * 4); |
---|
177 | n/a | |
---|
178 | n/a | memcpy((char*) addr, *p_argv.c, size); |
---|
179 | n/a | addr += (size + 3) / 4; |
---|
180 | n/a | break; |
---|
181 | n/a | } |
---|
182 | n/a | |
---|
183 | n/a | default: |
---|
184 | n/a | FFI_ASSERT(0); |
---|
185 | n/a | } |
---|
186 | n/a | } |
---|
187 | n/a | } |
---|
188 | n/a | |
---|
189 | n/a | |
---|
190 | n/a | void ffi_call(ffi_cif* cif, void(*fn)(void), void *rvalue, void **avalue) |
---|
191 | n/a | { |
---|
192 | n/a | extended_cif ecif; |
---|
193 | n/a | unsigned long rsize = cif->rtype->size; |
---|
194 | n/a | int flags = cif->flags; |
---|
195 | n/a | void *alloc = NULL; |
---|
196 | n/a | |
---|
197 | n/a | ecif.cif = cif; |
---|
198 | n/a | ecif.avalue = avalue; |
---|
199 | n/a | |
---|
200 | n/a | /* Note that for structures that are returned in registers (size <= 16 bytes) |
---|
201 | n/a | we allocate a temporary buffer and use memcpy to copy it to the final |
---|
202 | n/a | destination. The reason is that the target address might be misaligned or |
---|
203 | n/a | the length not a multiple of 4 bytes. Handling all those cases would be |
---|
204 | n/a | very complex. */ |
---|
205 | n/a | |
---|
206 | n/a | if (flags == FFI_TYPE_STRUCT && (rsize <= 16 || rvalue == NULL)) |
---|
207 | n/a | { |
---|
208 | n/a | alloc = alloca(ALIGN(rsize, 4)); |
---|
209 | n/a | ecif.rvalue = alloc; |
---|
210 | n/a | } |
---|
211 | n/a | else |
---|
212 | n/a | { |
---|
213 | n/a | ecif.rvalue = rvalue; |
---|
214 | n/a | } |
---|
215 | n/a | |
---|
216 | n/a | if (cif->abi != FFI_SYSV) |
---|
217 | n/a | FFI_ASSERT(0); |
---|
218 | n/a | |
---|
219 | n/a | ffi_call_SYSV (ecif.rvalue, rsize, cif->flags, fn, cif->bytes, &ecif); |
---|
220 | n/a | |
---|
221 | n/a | if (alloc != NULL && rvalue != NULL) |
---|
222 | n/a | memcpy(rvalue, alloc, rsize); |
---|
223 | n/a | } |
---|
224 | n/a | |
---|
225 | n/a | extern void ffi_trampoline(); |
---|
226 | n/a | extern void ffi_cacheflush(void* start, void* end); |
---|
227 | n/a | |
---|
228 | n/a | ffi_status |
---|
229 | n/a | ffi_prep_closure_loc (ffi_closure* closure, |
---|
230 | n/a | ffi_cif* cif, |
---|
231 | n/a | void (*fun)(ffi_cif*, void*, void**, void*), |
---|
232 | n/a | void *user_data, |
---|
233 | n/a | void *codeloc) |
---|
234 | n/a | { |
---|
235 | n/a | /* copye trampoline to stack and patch 'ffi_closure_SYSV' pointer */ |
---|
236 | n/a | memcpy(closure->tramp, ffi_trampoline, FFI_TRAMPOLINE_SIZE); |
---|
237 | n/a | *(unsigned int*)(&closure->tramp[8]) = (unsigned int)ffi_closure_SYSV; |
---|
238 | n/a | |
---|
239 | n/a | // Do we have this function? |
---|
240 | n/a | // __builtin___clear_cache(closer->tramp, closer->tramp + FFI_TRAMPOLINE_SIZE) |
---|
241 | n/a | ffi_cacheflush(closure->tramp, closure->tramp + FFI_TRAMPOLINE_SIZE); |
---|
242 | n/a | |
---|
243 | n/a | closure->cif = cif; |
---|
244 | n/a | closure->fun = fun; |
---|
245 | n/a | closure->user_data = user_data; |
---|
246 | n/a | return FFI_OK; |
---|
247 | n/a | } |
---|
248 | n/a | |
---|
249 | n/a | |
---|
250 | n/a | long FFI_HIDDEN |
---|
251 | n/a | ffi_closure_SYSV_inner(ffi_closure *closure, void **values, void *rvalue) |
---|
252 | n/a | { |
---|
253 | n/a | ffi_cif *cif; |
---|
254 | n/a | ffi_type **arg_types; |
---|
255 | n/a | void **avalue; |
---|
256 | n/a | int i, areg; |
---|
257 | n/a | |
---|
258 | n/a | cif = closure->cif; |
---|
259 | n/a | if (cif->abi != FFI_SYSV) |
---|
260 | n/a | return FFI_BAD_ABI; |
---|
261 | n/a | |
---|
262 | n/a | areg = 0; |
---|
263 | n/a | |
---|
264 | n/a | int rtype = cif->rtype->type; |
---|
265 | n/a | if (rtype == FFI_TYPE_STRUCT && cif->rtype->size > 4 * 4) |
---|
266 | n/a | { |
---|
267 | n/a | rvalue = *values; |
---|
268 | n/a | areg++; |
---|
269 | n/a | } |
---|
270 | n/a | |
---|
271 | n/a | cif = closure->cif; |
---|
272 | n/a | arg_types = cif->arg_types; |
---|
273 | n/a | avalue = alloca(cif->nargs * sizeof(void *)); |
---|
274 | n/a | |
---|
275 | n/a | for (i = 0; i < cif->nargs; i++) |
---|
276 | n/a | { |
---|
277 | n/a | if (arg_types[i]->alignment == 8 && (areg & 1) != 0) |
---|
278 | n/a | areg++; |
---|
279 | n/a | |
---|
280 | n/a | // skip the entry 16,a1 framework, add 16 bytes (4 registers) |
---|
281 | n/a | if (areg == FFI_REGISTER_NARGS) |
---|
282 | n/a | areg += 4; |
---|
283 | n/a | |
---|
284 | n/a | if (arg_types[i]->type == FFI_TYPE_STRUCT) |
---|
285 | n/a | { |
---|
286 | n/a | int numregs = ((arg_types[i]->size + 3) & ~3) / 4; |
---|
287 | n/a | if (areg < FFI_REGISTER_NARGS && areg + numregs > FFI_REGISTER_NARGS) |
---|
288 | n/a | areg = FFI_REGISTER_NARGS + 4; |
---|
289 | n/a | } |
---|
290 | n/a | |
---|
291 | n/a | avalue[i] = &values[areg]; |
---|
292 | n/a | areg += (arg_types[i]->size + 3) / 4; |
---|
293 | n/a | } |
---|
294 | n/a | |
---|
295 | n/a | (closure->fun)(cif, rvalue, avalue, closure->user_data); |
---|
296 | n/a | |
---|
297 | n/a | return rtype; |
---|
298 | n/a | } |
---|