Debian Codegraph @

src/x86/ffiw64.c

raw
1 /* -----------------------------------------------------------------------
2 ffiw64.c - Copyright (c) 2018 Anthony Green
3 Copyright (c) 2014 Red Hat, Inc.
4
5 x86 win64 Foreign Function Interface
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 ``Software''), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 ----------------------------------------------------------------------- */
27
28 #if defined(__x86_64__) || defined(_M_AMD64)
29 #include <ffi.h>
30 #include <ffi_common.h>
31 #include <stdlib.h>
32 #include <stdint.h>
33 #include <tramp.h>
34
35 #ifdef X86_WIN64
36 #define EFI64(name) name
37 #else
38 #define EFI64(name) FFI_HIDDEN name##_efi64
39 #endif
40
41 struct win64_call_frame
42 {
43 UINT64 rbp; /* 0 */
44 UINT64 retaddr; /* 8 */
45 UINT64 fn; /* 16 */
46 UINT64 flags; /* 24 */
47 UINT64 rvalue; /* 32 */
48 };
49
50 extern void ffi_call_win64 (void *stack, struct win64_call_frame *,
51 void *closure) FFI_HIDDEN;
52
53 ffi_status FFI_HIDDEN
54 EFI64(ffi_prep_cif_machdep)(ffi_cif *cif)
55 {
56 int flags, n;
57
58 switch (cif->abi)
59 {
60 case FFI_WIN64:
61 case FFI_GNUW64:
62 break;
63 default:
64 return FFI_BAD_ABI;
65 }
66
67 flags = cif->rtype->type;
68 switch (flags)
69 {
70 default:
71 break;
72 case FFI_TYPE_LONGDOUBLE:
73 /* GCC returns long double values by reference, like a struct */
74 if (cif->abi == FFI_GNUW64)
75 flags = FFI_TYPE_STRUCT;
76 break;
77 case FFI_TYPE_COMPLEX:
78 flags = FFI_TYPE_STRUCT;
79 /* FALLTHRU */
80 case FFI_TYPE_STRUCT:
81 switch (cif->rtype->size)
82 {
83 case 8:
84 flags = FFI_TYPE_UINT64;
85 break;
86 case 4:
87 flags = FFI_TYPE_SMALL_STRUCT_4B;
88 break;
89 case 2:
90 flags = FFI_TYPE_SMALL_STRUCT_2B;
91 break;
92 case 1:
93 flags = FFI_TYPE_SMALL_STRUCT_1B;
94 break;
95 }
96 break;
97 }
98 cif->flags = flags;
99
100 /* Each argument either fits in a register, an 8 byte slot, or is
101 passed by reference with the pointer in the 8 byte slot. */
102 n = cif->nargs;
103 n += (flags == FFI_TYPE_STRUCT);
104 if (n < 4)
105 n = 4;
106 cif->bytes = n * 8;
107
108 return FFI_OK;
109 }
110
111 /* We perform some black magic here to use some of the parent's stack frame in
112 * ffi_call_win64() that breaks with the MSVC compiler with the /RTCs or /GZ
113 * flags. Disable the 'Stack frame run time error checking' for this function
114 * so we don't hit weird exceptions in debug builds. */
115 #if defined(_MSC_VER)
116 #pragma runtime_checks("s", off)
117 #endif
118 static void
119 ffi_call_int (ffi_cif *cif, void (*fn)(void), void *rvalue,
120 void **avalue, void *closure)
121 {
122 int i, j, n, flags;
123 UINT64 *stack;
124 size_t rsize;
125 struct win64_call_frame *frame;
126 ffi_type **arg_types = cif->arg_types;
127 int nargs = cif->nargs;
128
129 FFI_ASSERT(cif->abi == FFI_GNUW64 || cif->abi == FFI_WIN64);
130
131 /* If we have any large structure arguments, make a copy so we are passing
132 by value. */
133 for (i = 0; i < nargs; i++)
134 {
135 ffi_type *at = arg_types[i];
136 int size = at->size;
137 if (at->type == FFI_TYPE_STRUCT && size > 8)
138 {
139 char *argcopy = alloca (size);
140 memcpy (argcopy, avalue[i], size);
141 avalue[i] = argcopy;
142 }
143 }
144
145 flags = cif->flags;
146 rsize = 0;
147
148 /* If we have no return value for a structure, we need to create one.
149 Otherwise we can ignore the return type entirely. */
150 if (rvalue == NULL)
151 {
152 if (flags == FFI_TYPE_STRUCT)
153 rsize = cif->rtype->size;
154 else
155 flags = FFI_TYPE_VOID;
156 }
157
158 stack = alloca(cif->bytes + sizeof(struct win64_call_frame) + rsize);
159 frame = (struct win64_call_frame *)((char *)stack + cif->bytes);
160 if (rsize)
161 rvalue = frame + 1;
162
163 frame->fn = (uintptr_t)fn;
164 frame->flags = flags;
165 frame->rvalue = (uintptr_t)rvalue;
166
167 j = 0;
168 if (flags == FFI_TYPE_STRUCT)
169 {
170 stack[0] = (uintptr_t)rvalue;
171 j = 1;
172 }
173
174 for (i = 0, n = cif->nargs; i < n; ++i, ++j)
175 {
176 switch (cif->arg_types[i]->size)
177 {
178 case 8:
179 stack[j] = *(UINT64 *)avalue[i];
180 break;
181 case 4:
182 stack[j] = *(UINT32 *)avalue[i];
183 break;
184 case 2:
185 stack[j] = *(UINT16 *)avalue[i];
186 break;
187 case 1:
188 stack[j] = *(UINT8 *)avalue[i];
189 break;
190 default:
191 stack[j] = (uintptr_t)avalue[i];
192 break;
193 }
194 }
195
196 ffi_call_win64 (stack, frame, closure);
197 }
198 #if defined(_MSC_VER)
199 #pragma runtime_checks("s", restore)
200 #endif
201
202 void
203 EFI64(ffi_call)(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
204 {
205 ffi_call_int (cif, fn, rvalue, avalue, NULL);
206 }
207
208 void
209 EFI64(ffi_call_go)(ffi_cif *cif, void (*fn)(void), void *rvalue,
210 void **avalue, void *closure)
211 {
212 ffi_call_int (cif, fn, rvalue, avalue, closure);
213 }
214
215
216 extern void ffi_closure_win64(void) FFI_HIDDEN;
217 #if defined(FFI_EXEC_STATIC_TRAMP)
218 extern void ffi_closure_win64_alt(void) FFI_HIDDEN;
219 #endif
220
221 #ifdef FFI_GO_CLOSURES
222 extern void ffi_go_closure_win64(void) FFI_HIDDEN;
223 #endif
224
225 ffi_status
226 EFI64(ffi_prep_closure_loc)(ffi_closure* closure,
227 ffi_cif* cif,
228 void (*fun)(ffi_cif*, void*, void**, void*),
229 void *user_data,
230 void *codeloc MAYBE_UNUSED)
231 {
232 static const unsigned char trampoline[FFI_TRAMPOLINE_SIZE - 8] = {
233 /* endbr64 */
234 0xf3, 0x0f, 0x1e, 0xfa,
235 /* leaq -0xb(%rip),%r10 # 0x0 */
236 0x4c, 0x8d, 0x15, 0xf5, 0xff, 0xff, 0xff,
237 /* jmpq *0x7(%rip) # 0x18 */
238 0xff, 0x25, 0x07, 0x00, 0x00, 0x00,
239 /* nopl 0(%rax) */
240 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00
241 };
242 char *tramp = closure->tramp;
243
244 switch (cif->abi)
245 {
246 case FFI_WIN64:
247 case FFI_GNUW64:
248 break;
249 default:
250 return FFI_BAD_ABI;
251 }
252
253 #if defined(FFI_EXEC_STATIC_TRAMP)
254 if (ffi_tramp_is_present(closure))
255 {
256 /* Initialize the static trampoline's parameters. */
257 ffi_tramp_set_parms (closure->ftramp, ffi_closure_win64_alt, closure);
258 goto out;
259 }
260 #endif
261
262 /* Initialize the dynamic trampoline. */
263 memcpy (tramp, trampoline, sizeof(trampoline));
264 *(UINT64 *)(tramp + sizeof (trampoline)) = (uintptr_t)ffi_closure_win64;
265
266 #if defined(FFI_EXEC_STATIC_TRAMP)
267 out:
268 #endif
269 closure->cif = cif;
270 closure->fun = fun;
271 closure->user_data = user_data;
272
273 return FFI_OK;
274 }
275
276 #ifdef FFI_GO_CLOSURES
277 ffi_status
278 EFI64(ffi_prep_go_closure)(ffi_go_closure* closure, ffi_cif* cif,
279 void (*fun)(ffi_cif*, void*, void**, void*))
280 {
281 switch (cif->abi)
282 {
283 case FFI_WIN64:
284 case FFI_GNUW64:
285 break;
286 default:
287 return FFI_BAD_ABI;
288 }
289
290 closure->tramp = ffi_go_closure_win64;
291 closure->cif = cif;
292 closure->fun = fun;
293
294 return FFI_OK;
295 }
296 #endif
297
298 struct win64_closure_frame
299 {
300 UINT64 rvalue[2];
301 UINT64 fargs[4];
302 UINT64 retaddr;
303 UINT64 args[];
304 };
305
306 /* Force the inner function to use the MS ABI. When compiling on win64
307 this is a nop. When compiling on unix, this simplifies the assembly,
308 and places the burden of saving the extra call-saved registers on
309 the compiler. */
310 int FFI_HIDDEN __attribute__((ms_abi))
311 ffi_closure_win64_inner(ffi_cif *cif,
312 void (*fun)(ffi_cif*, void*, void**, void*),
313 void *user_data,
314 struct win64_closure_frame *frame)
315 {
316 void **avalue;
317 void *rvalue;
318 int i, n, nreg, flags;
319
320 avalue = alloca(cif->nargs * sizeof(void *));
321 rvalue = frame->rvalue;
322 nreg = 0;
323
324 /* When returning a structure, the address is in the first argument.
325 We must also be prepared to return the same address in eax, so
326 install that address in the frame and pretend we return a pointer. */
327 flags = cif->flags;
328 if (flags == FFI_TYPE_STRUCT)
329 {
330 rvalue = (void *)(uintptr_t)frame->args[0];
331 frame->rvalue[0] = frame->args[0];
332 nreg = 1;
333 }
334
335 for (i = 0, n = cif->nargs; i < n; ++i, ++nreg)
336 {
337 size_t size = cif->arg_types[i]->size;
338 size_t type = cif->arg_types[i]->type;
339 void *a;
340
341 if (type == FFI_TYPE_DOUBLE || type == FFI_TYPE_FLOAT)
342 {
343 if (nreg < 4)
344 a = &frame->fargs[nreg];
345 else
346 a = &frame->args[nreg];
347 }
348 else if (size == 1 || size == 2 || size == 4 || size == 8)
349 a = &frame->args[nreg];
350 else
351 a = (void *)(uintptr_t)frame->args[nreg];
352
353 avalue[i] = a;
354 }
355
356 /* Invoke the closure. */
357 fun (cif, rvalue, avalue, user_data);
358 return flags;
359 }
360
361 #endif /* __x86_64__ */