Debian Codegraph @

libffi/src/loongarch64/ffi.c

raw
1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2022 Xu Chenghua <[email protected]>
3 2022 Cheng Lulu <[email protected]>
4 Based on RISC-V port
5
6 LoongArch Foreign Function Interface
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 ``Software''), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be included
17 in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 ----------------------------------------------------------------------- */
28
29 #include <ffi.h>
30 #include <ffi_common.h>
31
32 #include <stdlib.h>
33 #include <stdint.h>
34
35 #if defined(__loongarch_soft_float)
36 # define ABI_FRLEN 0
37 #elif defined(__loongarch_single_float)
38 # define ABI_FRLEN 32
39 # define ABI_FLOAT float
40 #elif defined(__loongarch_double_float)
41 # define ABI_FRLEN 64
42 # define ABI_FLOAT double
43 #else
44 #error unsupported LoongArch floating-point ABI
45 #endif
46
47 #define NARGREG 8
48 #define STKALIGN 16
49 #define MAXCOPYARG (2 * sizeof (double))
50
51 /* call_context registers
52 - 8 floating point parameter/result registers.
53 - 8 integer parameter/result registers.
54 - 2 registers used by the assembly code to in-place construct its own
55 stack frame
56 - frame register
57 - return register
58 */
59 typedef struct call_context
60 {
61 #if !defined(__loongarch_soft_float)
62 ABI_FLOAT fa[8];
63 #endif
64 size_t a[10];
65 } call_context;
66
67 typedef struct call_builder
68 {
69 call_context *aregs;
70 int used_integer;
71 int used_float;
72 size_t *used_stack;
73 size_t *stack;
74 size_t next_struct_area;
75 } call_builder;
76
77 /* Integer (not pointer) less than ABI GRLEN. */
78 /* FFI_TYPE_INT does not appear to be used. */
79 #if __SIZEOF_POINTER__ == 8
80 # define IS_INT(type) ((type) >= FFI_TYPE_UINT8 && (type) <= FFI_TYPE_SINT64)
81 #else
82 # define IS_INT(type) ((type) >= FFI_TYPE_UINT8 && (type) <= FFI_TYPE_SINT32)
83 #endif
84
85 #if ABI_FRLEN
86 typedef struct float_struct_info
87 {
88 char as_elements;
89 char type1;
90 char offset2;
91 char type2;
92 } float_struct_info;
93
94 #if ABI_FRLEN >= 64
95 # define IS_FLOAT(type) ((type) >= FFI_TYPE_FLOAT && (type) <= FFI_TYPE_DOUBLE)
96 #else
97 # define IS_FLOAT(type) ((type) == FFI_TYPE_FLOAT)
98 #endif
99
100 static ffi_type **
101 flatten_struct (ffi_type *in, ffi_type **out, ffi_type **out_end)
102 {
103 int i;
104
105 if (out == out_end)
106 return out;
107 if (in->type != FFI_TYPE_STRUCT)
108 *(out++) = in;
109 else
110 for (i = 0; in->elements[i]; i++)
111 out = flatten_struct (in->elements[i], out, out_end);
112 return out;
113 }
114
115 /* Structs with at most two fields after flattening, one of which is of
116 floating point type, are passed in multiple registers if sufficient
117 registers are available. */
118 static float_struct_info
119 struct_passed_as_elements (call_builder *cb, ffi_type *top)
120 {
121 float_struct_info ret = {0, 0, 0, 0};
122 ffi_type *fields[3];
123 int num_floats, num_ints;
124 int num_fields = flatten_struct (top, fields, fields + 3) - fields;
125
126 if (num_fields == 1)
127 {
128 if (IS_FLOAT (fields[0]->type))
129 {
130 ret.as_elements = 1;
131 ret.type1 = fields[0]->type;
132 }
133 }
134 else if (num_fields == 2)
135 {
136 num_floats = IS_FLOAT (fields[0]->type) + IS_FLOAT (fields[1]->type);
137 num_ints = IS_INT (fields[0]->type) + IS_INT (fields[1]->type);
138 if (num_floats == 0 || num_floats + num_ints != 2)
139 return ret;
140 if (cb->used_float + num_floats > NARGREG
141 || cb->used_integer + (2 - num_floats) > NARGREG)
142 return ret;
143 if (!IS_FLOAT (fields[0]->type) && !IS_FLOAT (fields[1]->type))
144 return ret;
145
146 ret.type1 = fields[0]->type;
147 ret.type2 = fields[1]->type;
148 ret.offset2 = FFI_ALIGN (fields[0]->size, fields[1]->alignment);
149 ret.as_elements = 1;
150 }
151 return ret;
152 }
153 #endif
154
155 /* Allocates a single register, float register, or GRLEN-sized stack slot to a
156 datum. */
157 static void
158 marshal_atom (call_builder *cb, int type, void *data)
159 {
160 size_t value = 0;
161 switch (type)
162 {
163 case FFI_TYPE_UINT8:
164 value = *(uint8_t *) data;
165 break;
166 case FFI_TYPE_SINT8:
167 value = *(int8_t *) data;
168 break;
169 case FFI_TYPE_UINT16:
170 value = *(uint16_t *) data;
171 break;
172 case FFI_TYPE_SINT16:
173 value = *(int16_t *) data;
174 break;
175 /* 32-bit quantities are always sign-extended in the ABI. */
176 case FFI_TYPE_UINT32:
177 value = *(int32_t *) data;
178 break;
179 case FFI_TYPE_SINT32:
180 value = *(int32_t *) data;
181 break;
182 #if __SIZEOF_POINTER__ == 8
183 case FFI_TYPE_UINT64:
184 value = *(uint64_t *) data;
185 break;
186 case FFI_TYPE_SINT64:
187 value = *(int64_t *) data;
188 break;
189 #endif
190 case FFI_TYPE_POINTER:
191 value = *(size_t *) data;
192 break;
193
194 #if ABI_FRLEN >= 32
195 case FFI_TYPE_FLOAT:
196 *(float *)(cb->aregs->fa + cb->used_float++) = *(float *) data;
197 return;
198 #endif
199 #if ABI_FRLEN >= 64
200 case FFI_TYPE_DOUBLE:
201 (cb->aregs->fa[cb->used_float++]) = *(double *) data;
202 return;
203 #endif
204 default:
205 FFI_ASSERT (0);
206 break;
207 }
208
209 if (cb->used_integer == NARGREG)
210 *cb->used_stack++ = value;
211 else
212 cb->aregs->a[cb->used_integer++] = value;
213 }
214
215 static void
216 unmarshal_atom (call_builder *cb, int type, void *data)
217 {
218 size_t value;
219 switch (type)
220 {
221 #if ABI_FRLEN >= 32
222 case FFI_TYPE_FLOAT:
223 *(float *) data = *(float *)(cb->aregs->fa + cb->used_float++);
224 return;
225 #endif
226 #if ABI_FRLEN >= 64
227 case FFI_TYPE_DOUBLE:
228 *(double *) data = cb->aregs->fa[cb->used_float++];
229 return;
230 #endif
231 }
232
233 if (cb->used_integer == NARGREG)
234 value = *cb->used_stack++;
235 else
236 value = cb->aregs->a[cb->used_integer++];
237
238 switch (type)
239 {
240 case FFI_TYPE_UINT8:
241 case FFI_TYPE_SINT8:
242 case FFI_TYPE_UINT16:
243 case FFI_TYPE_SINT16:
244 case FFI_TYPE_UINT32:
245 case FFI_TYPE_SINT32:
246 #if __SIZEOF_POINTER__ == 8
247 case FFI_TYPE_UINT64:
248 case FFI_TYPE_SINT64:
249 #endif
250 case FFI_TYPE_POINTER:
251 *(ffi_arg *)data = value;
252 break;
253 default:
254 FFI_ASSERT (0);
255 break;
256 }
257 }
258
259 /* Allocate and copy a structure that is passed by value on the stack and
260 return a pointer to it. */
261 static void *
262 allocate_and_copy_struct_to_stack (call_builder *cb, void *data,
263 ffi_type *type)
264 {
265 size_t dest = cb->next_struct_area - type->size;
266
267 dest = FFI_ALIGN_DOWN (dest, type->alignment);
268 cb->next_struct_area = dest;
269
270 return memcpy ((char *)cb->stack + dest, data, type->size);
271 }
272
273 /* Adds an argument to a call, or a not by reference return value. */
274 static void
275 marshal (call_builder *cb, ffi_type *type, int var, void *data)
276 {
277 size_t realign[2];
278
279 #if ABI_FRLEN
280 if (!var && type->type == FFI_TYPE_STRUCT)
281 {
282 float_struct_info fsi = struct_passed_as_elements (cb, type);
283 if (fsi.as_elements)
284 {
285 marshal_atom (cb, fsi.type1, data);
286 if (fsi.offset2)
287 marshal_atom (cb, fsi.type2, ((char *) data) + fsi.offset2);
288 return;
289 }
290 }
291
292 if (!var && cb->used_float < NARGREG
293 && IS_FLOAT (type->type))
294 {
295 marshal_atom (cb, type->type, data);
296 return;
297 }
298
299 double promoted;
300 if (var && type->type == FFI_TYPE_FLOAT)
301 {
302 /* C standard requires promoting float -> double for variable arg. */
303 promoted = *(float *) data;
304 type = &ffi_type_double;
305 data = &promoted;
306 }
307 #endif
308
309 if (type->size > 2 * __SIZEOF_POINTER__)
310 /* Pass by reference. */
311 {
312 allocate_and_copy_struct_to_stack (cb, data, type);
313 data = (char *)cb->stack + cb->next_struct_area;
314 marshal_atom (cb, FFI_TYPE_POINTER, &data);
315 }
316 else if (IS_INT (type->type) || type->type == FFI_TYPE_POINTER)
317 marshal_atom (cb, type->type, data);
318 else
319 {
320 /* Overlong integers, soft-float floats, and structs without special
321 float handling are treated identically from this point on. */
322
323 /* Variadics are aligned even in registers. */
324 if (type->alignment > __SIZEOF_POINTER__)
325 {
326 if (var)
327 cb->used_integer = FFI_ALIGN (cb->used_integer, 2);
328 cb->used_stack
329 = (size_t *) FFI_ALIGN (cb->used_stack, 2 * __SIZEOF_POINTER__);
330 }
331
332 memcpy (realign, data, type->size);
333 if (type->size > 0)
334 marshal_atom (cb, FFI_TYPE_POINTER, realign);
335 if (type->size > __SIZEOF_POINTER__)
336 marshal_atom (cb, FFI_TYPE_POINTER, realign + 1);
337 }
338 }
339
340 /* For arguments passed by reference returns the pointer, otherwise the arg
341 is copied (up to MAXCOPYARG bytes). */
342 static void *
343 unmarshal (call_builder *cb, ffi_type *type, int var, void *data)
344 {
345 size_t realign[2];
346 void *pointer;
347
348 #if ABI_FRLEN
349 if (!var && type->type == FFI_TYPE_STRUCT)
350 {
351 float_struct_info fsi = struct_passed_as_elements (cb, type);
352 if (fsi.as_elements)
353 {
354 unmarshal_atom (cb, fsi.type1, data);
355 if (fsi.offset2)
356 unmarshal_atom (cb, fsi.type2, ((char *) data) + fsi.offset2);
357 return data;
358 }
359 }
360
361 if (!var && cb->used_float < NARGREG
362 && IS_FLOAT (type->type))
363 {
364 unmarshal_atom (cb, type->type, data);
365 return data;
366 }
367
368 if (var && type->type == FFI_TYPE_FLOAT)
369 {
370 int m = cb->used_integer;
371 void *promoted
372 = m < NARGREG ? cb->aregs->a + m : cb->used_stack + m - NARGREG + 1;
373 *(float *) promoted = *(double *) promoted;
374 }
375 #endif
376
377 if (type->size > 2 * __SIZEOF_POINTER__)
378 {
379 /* Pass by reference. */
380 unmarshal_atom (cb, FFI_TYPE_POINTER, (char *) &pointer);
381 return pointer;
382 }
383 else if (IS_INT (type->type) || type->type == FFI_TYPE_POINTER)
384 {
385 unmarshal_atom (cb, type->type, data);
386 return data;
387 }
388 else
389 {
390 /* Overlong integers, soft-float floats, and structs without special
391 float handling are treated identically from this point on. */
392
393 /* Variadics are aligned even in registers. */
394 if (type->alignment > __SIZEOF_POINTER__)
395 {
396 if (var)
397 cb->used_integer = FFI_ALIGN (cb->used_integer, 2);
398 cb->used_stack
399 = (size_t *) FFI_ALIGN (cb->used_stack, 2 * __SIZEOF_POINTER__);
400 }
401
402 if (type->size > 0)
403 unmarshal_atom (cb, FFI_TYPE_POINTER, realign);
404 if (type->size > __SIZEOF_POINTER__)
405 unmarshal_atom (cb, FFI_TYPE_POINTER, realign + 1);
406 memcpy (data, realign, type->size);
407 return data;
408 }
409 }
410
411 static int
412 passed_by_ref (call_builder *cb, ffi_type *type, int var)
413 {
414 #if ABI_FRLEN
415 if (!var && type->type == FFI_TYPE_STRUCT)
416 {
417 float_struct_info fsi = struct_passed_as_elements (cb, type);
418 if (fsi.as_elements)
419 return 0;
420 }
421 #endif
422
423 return type->size > 2 * __SIZEOF_POINTER__;
424 }
425
426 /* Perform machine dependent cif processing. */
427 ffi_status
428 ffi_prep_cif_machdep (ffi_cif *cif)
429 {
430 cif->loongarch_nfixedargs = cif->nargs;
431 return FFI_OK;
432 }
433
434 /* Perform machine dependent cif processing when we have a variadic
435 function. */
436 ffi_status
437 ffi_prep_cif_machdep_var (ffi_cif *cif, unsigned int nfixedargs,
438 unsigned int ntotalargs)
439 {
440 cif->loongarch_nfixedargs = nfixedargs;
441 return FFI_OK;
442 }
443
444 /* Low level routine for calling functions. */
445 extern void ffi_call_asm (void *stack, struct call_context *regs,
446 void (*fn) (void), void *closure) FFI_HIDDEN;
447
448 static void
449 ffi_call_int (ffi_cif *cif, void (*fn) (void), void *rvalue, void **avalue,
450 void *closure)
451 {
452 /* This is a conservative estimate, assuming a complex return value and
453 that all remaining arguments are long long / __int128 */
454 size_t arg_bytes = cif->bytes;
455 size_t rval_bytes = 0;
456 if (rvalue == NULL && cif->rtype->size > 2 * __SIZEOF_POINTER__)
457 rval_bytes = FFI_ALIGN (cif->rtype->size, STKALIGN);
458 size_t alloc_size = arg_bytes + rval_bytes + sizeof (call_context);
459
460 /* The assembly code will deallocate all stack data at lower addresses
461 than the argument region, so we need to allocate the frame and the
462 return value after the arguments in a single allocation. */
463 size_t alloc_base;
464 /* Argument region must be 16-byte aligned in LP64 ABIs. */
465 if (_Alignof(max_align_t) >= STKALIGN)
466 /* Since sizeof long double is normally 16, the compiler will
467 guarantee alloca alignment to at least that much. */
468 alloc_base = (size_t) alloca (alloc_size);
469 else
470 alloc_base = FFI_ALIGN (alloca (alloc_size + STKALIGN - 1), STKALIGN);
471
472 if (rval_bytes)
473 rvalue = (void *) (alloc_base + arg_bytes);
474
475 call_builder cb;
476 cb.used_float = cb.used_integer = 0;
477 cb.aregs = (call_context *) (alloc_base + arg_bytes + rval_bytes);
478 cb.used_stack = (void *) alloc_base;
479 cb.stack = (void *) alloc_base;
480 cb.next_struct_area = arg_bytes;
481
482 int return_by_ref = passed_by_ref (&cb, cif->rtype, 0);
483 if (return_by_ref)
484 cb.aregs->a[cb.used_integer++] = (size_t)rvalue;
485
486 int i;
487 for (i = 0; i < cif->nargs; i++)
488 marshal (&cb, cif->arg_types[i], i >= cif->loongarch_nfixedargs,
489 avalue[i]);
490
491 ffi_call_asm ((void *) alloc_base, cb.aregs, fn, closure);
492
493 cb.used_float = cb.used_integer = 0;
494 if (!return_by_ref && rvalue)
495 unmarshal (&cb, cif->rtype, 0, rvalue);
496 }
497
498 void
499 ffi_call (ffi_cif *cif, void (*fn) (void), void *rvalue, void **avalue)
500 {
501 ffi_call_int (cif, fn, rvalue, avalue, NULL);
502 }
503
504 void
505 ffi_call_go (ffi_cif *cif, void (*fn) (void), void *rvalue, void **avalue,
506 void *closure)
507 {
508 ffi_call_int (cif, fn, rvalue, avalue, closure);
509 }
510
511 extern void ffi_closure_asm (void) FFI_HIDDEN;
512
513 ffi_status
514 ffi_prep_closure_loc (ffi_closure *closure, ffi_cif *cif,
515 void (*fun) (ffi_cif *, void *, void **, void *),
516 void *user_data, void *codeloc)
517 {
518 uint32_t *tramp = (uint32_t *) &closure->tramp[0];
519 uint64_t fn = (uint64_t) (uintptr_t) ffi_closure_asm;
520
521 if (cif->abi <= FFI_FIRST_ABI || cif->abi >= FFI_LAST_ABI)
522 return FFI_BAD_ABI;
523
524 #if defined(FFI_EXEC_STATIC_TRAMP)
525 if (ffi_tramp_is_present(closure))
526 {
527 ffi_tramp_set_parms (closure->ftramp, ffi_closure_asm, closure);
528 goto out;
529 }
530 #endif
531
532 /* Fill the dynamic trampoline. We will call ffi_closure_inner with codeloc,
533 not closure, but as long as the memory is readable it should work. */
534 tramp[0] = 0x1800000c; /* pcaddi $t0, 0 (i.e. $t0 <- tramp) */
535 tramp[1] = 0x28c0418d; /* ld.d $t1, $t0, 16 */
536 tramp[2] = 0x4c0001a0; /* jirl $zero, $t1, 0 */
537 tramp[3] = 0x03400000; /* nop */
538 tramp[4] = fn;
539 tramp[5] = fn >> 32;
540
541 __builtin___clear_cache (codeloc, codeloc + FFI_TRAMPOLINE_SIZE);
542
543 #if defined(FFI_EXEC_STATIC_TRAMP)
544 out:
545 #endif
546 closure->cif = cif;
547 closure->fun = fun;
548 closure->user_data = user_data;
549
550 return FFI_OK;
551 }
552
553 extern void ffi_go_closure_asm (void) FFI_HIDDEN;
554
555 ffi_status
556 ffi_prep_go_closure (ffi_go_closure *closure, ffi_cif *cif,
557 void (*fun) (ffi_cif *, void *, void **, void *))
558 {
559 if (cif->abi <= FFI_FIRST_ABI || cif->abi >= FFI_LAST_ABI)
560 return FFI_BAD_ABI;
561
562 closure->tramp = (void *) ffi_go_closure_asm;
563 closure->cif = cif;
564 closure->fun = fun;
565 return FFI_OK;
566 }
567
568 /* Called by the assembly code with aregs pointing to saved argument registers
569 and stack pointing to the stacked arguments. Return values passed in
570 registers will be reloaded from aregs. */
571 void FFI_HIDDEN
572 ffi_closure_inner (ffi_cif *cif,
573 void (*fun) (ffi_cif *, void *, void **, void *),
574 void *user_data, size_t *stack, call_context *aregs)
575 {
576 void **avalue = alloca (cif->nargs * sizeof (void *));
577 /* Storage for arguments which will be copied by unmarshal(). We could
578 theoretically avoid the copies in many cases and use at most 128 bytes
579 of memory, but allocating disjoint storage for each argument is
580 simpler. */
581 char *astorage = alloca (cif->nargs * MAXCOPYARG);
582 void *rvalue;
583 call_builder cb;
584 int return_by_ref;
585 int i;
586
587 cb.aregs = aregs;
588 cb.used_integer = cb.used_float = 0;
589 cb.used_stack = stack;
590
591 return_by_ref = passed_by_ref (&cb, cif->rtype, 0);
592 if (return_by_ref)
593 unmarshal (&cb, &ffi_type_pointer, 0, &rvalue);
594 else
595 rvalue = alloca (cif->rtype->size);
596
597 for (i = 0; i < cif->nargs; i++)
598 avalue[i]
599 = unmarshal (&cb, cif->arg_types[i], i >= cif->loongarch_nfixedargs,
600 astorage + i * MAXCOPYARG);
601
602 fun (cif, rvalue, avalue, user_data);
603
604 if (!return_by_ref && cif->rtype->type != FFI_TYPE_VOID)
605 {
606 cb.used_integer = cb.used_float = 0;
607 marshal (&cb, cif->rtype, 0, rvalue);
608 }
609 }
610
611 #if defined(FFI_EXEC_STATIC_TRAMP)
612 void *
613 ffi_tramp_arch (size_t *tramp_size, size_t *map_size)
614 {
615 extern void *trampoline_code_table;
616
617 *tramp_size = 16;
618 /* A mapping size of 64K is chosen to cover the page sizes of 4K, 16K, and
619 64K. */
620 *map_size = 1 << 16;
621 return &trampoline_code_table;
622 }
623 #endif