1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1996, 2007, 2008 Red Hat, Inc.
3 Copyright (c) 2008 David Daney
5 MIPS Foreign Function Interface
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:
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
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 ----------------------------------------------------------------------- */
29 #include <ffi_common.h>
34 # if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3))
35 # define USE__BUILTIN___CLEAR_CACHE 1
39 #ifndef USE__BUILTIN___CLEAR_CACHE
40 #include <sys/cachectl.h>
44 # define FFI_MIPS_STOP_HERE() ffi_stop_here()
46 # define FFI_MIPS_STOP_HERE() do {} while(0)
51 FFI_ASSERT(argp <= &stack[bytes]); \
52 if (argp == &stack[bytes]) \
55 FFI_MIPS_STOP_HERE(); \
62 /* ffi_prep_args is called by the assembly routine once stack space
63 has been allocated for the function's arguments */
65 static void ffi_prep_args(char *stack,
76 /* If more than 8 double words are used, the remainder go
77 on the stack. We reorder stuff on the stack here to
78 support this easily. */
79 if (bytes > 8 * sizeof(ffi_arg))
80 argp = &stack[bytes - (8 * sizeof(ffi_arg))];
87 memset(stack, 0, bytes);
90 if ( ecif->cif->rstruct_flag != 0 )
92 if ( ecif->cif->rtype->type == FFI_TYPE_STRUCT )
95 *(ffi_arg *) argp = (ffi_arg) ecif->rvalue;
96 argp += sizeof(ffi_arg);
100 p_argv = ecif->avalue;
102 for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types; i; i--, p_arg++)
107 /* Align if necessary. */
108 a = (*p_arg)->alignment;
109 if (a < sizeof(ffi_arg))
112 if ((a - 1) & (unsigned long) argp)
114 argp = (char *) ALIGN(argp, a);
119 if (z <= sizeof(ffi_arg))
121 int type = (*p_arg)->type;
124 /* The size of a pointer depends on the ABI */
125 if (type == FFI_TYPE_POINTER)
127 (ecif->cif->abi == FFI_N64) ? FFI_TYPE_SINT64 : FFI_TYPE_SINT32;
132 *(ffi_arg *)argp = *(SINT8 *)(* p_argv);
136 *(ffi_arg *)argp = *(UINT8 *)(* p_argv);
139 case FFI_TYPE_SINT16:
140 *(ffi_arg *)argp = *(SINT16 *)(* p_argv);
143 case FFI_TYPE_UINT16:
144 *(ffi_arg *)argp = *(UINT16 *)(* p_argv);
147 case FFI_TYPE_SINT32:
148 *(ffi_arg *)argp = *(SINT32 *)(* p_argv);
151 case FFI_TYPE_UINT32:
152 *(ffi_arg *)argp = *(UINT32 *)(* p_argv);
155 /* This can only happen with 64bit slots. */
157 *(float *) argp = *(float *)(* p_argv);
160 /* Handle structures. */
162 memcpy(argp, *p_argv, (*p_arg)->size);
169 memcpy(argp, *p_argv, z);
172 unsigned long end = (unsigned long) argp + z;
173 unsigned long cap = (unsigned long) stack + bytes;
175 /* Check if the data will fit within the register space.
176 Handle it if it doesn't. */
179 memcpy(argp, *p_argv, z);
182 unsigned long portion = cap - (unsigned long)argp;
184 memcpy(argp, *p_argv, portion);
187 memcpy(argp, (void*)((unsigned long)(*p_argv) + portion),
201 /* The n32 spec says that if "a chunk consists solely of a double
202 float field (but not a double, which is part of a union), it
203 is passed in a floating point register. Any other chunk is
204 passed in an integer register". This code traverses structure
205 definitions and generates the appropriate flags. */
208 calc_n32_struct_flags(ffi_type *arg, unsigned *loc, unsigned *arg_reg)
215 while ((e = arg->elements[index]))
217 /* Align this object. */
218 *loc = ALIGN(*loc, e->alignment);
219 if (e->type == FFI_TYPE_DOUBLE)
221 /* Already aligned to FFI_SIZEOF_ARG. */
222 *arg_reg = *loc / FFI_SIZEOF_ARG;
225 flags += (FFI_TYPE_DOUBLE << (*arg_reg * FFI_FLAG_BITS));
232 /* Next Argument register at alignment of FFI_SIZEOF_ARG. */
233 *arg_reg = ALIGN(*loc, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
239 calc_n32_return_struct_flags(ffi_type *arg)
242 unsigned small = FFI_TYPE_SMALLSTRUCT;
245 /* Returning structures under n32 is a tricky thing.
246 A struct with only one or two floating point fields
247 is returned in $f0 (and $f2 if necessary). Any other
248 struct results at most 128 bits are returned in $2
249 (the first 64 bits) and $3 (remainder, if necessary).
250 Larger structs are handled normally. */
256 small = FFI_TYPE_SMALLSTRUCT2;
258 e = arg->elements[0];
259 if (e->type == FFI_TYPE_DOUBLE)
260 flags = FFI_TYPE_DOUBLE;
261 else if (e->type == FFI_TYPE_FLOAT)
262 flags = FFI_TYPE_FLOAT;
264 if (flags && (e = arg->elements[1]))
266 if (e->type == FFI_TYPE_DOUBLE)
267 flags += FFI_TYPE_DOUBLE << FFI_FLAG_BITS;
268 else if (e->type == FFI_TYPE_FLOAT)
269 flags += FFI_TYPE_FLOAT << FFI_FLAG_BITS;
273 if (flags && (arg->elements[2]))
275 /* There are three arguments and the first two are
276 floats! This must be passed the old way. */
289 /* Perform machine dependent cif processing */
290 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
295 /* Set the flags necessary for O32 processing. FFI_O32_SOFT_FLOAT
296 * does not have special handling for floating point args.
299 if (cif->rtype->type != FFI_TYPE_STRUCT && cif->abi == FFI_O32)
303 switch ((cif->arg_types)[0]->type)
306 case FFI_TYPE_DOUBLE:
307 cif->flags += (cif->arg_types)[0]->type;
316 /* Only handle the second argument if the first
317 is a float or double. */
320 switch ((cif->arg_types)[1]->type)
323 case FFI_TYPE_DOUBLE:
324 cif->flags += (cif->arg_types)[1]->type << FFI_FLAG_BITS;
335 /* Set the return type flag */
337 if (cif->abi == FFI_O32_SOFT_FLOAT)
339 switch (cif->rtype->type)
342 case FFI_TYPE_STRUCT:
343 cif->flags += cif->rtype->type << (FFI_FLAG_BITS * 2);
346 case FFI_TYPE_SINT64:
347 case FFI_TYPE_UINT64:
348 case FFI_TYPE_DOUBLE:
349 cif->flags += FFI_TYPE_UINT64 << (FFI_FLAG_BITS * 2);
354 cif->flags += FFI_TYPE_INT << (FFI_FLAG_BITS * 2);
361 switch (cif->rtype->type)
364 case FFI_TYPE_STRUCT:
366 case FFI_TYPE_DOUBLE:
367 cif->flags += cif->rtype->type << (FFI_FLAG_BITS * 2);
370 case FFI_TYPE_SINT64:
371 case FFI_TYPE_UINT64:
372 cif->flags += FFI_TYPE_UINT64 << (FFI_FLAG_BITS * 2);
376 cif->flags += FFI_TYPE_INT << (FFI_FLAG_BITS * 2);
383 /* Set the flags necessary for N32 processing */
385 unsigned arg_reg = 0;
387 unsigned count = (cif->nargs < 8) ? cif->nargs : 8;
390 unsigned struct_flags = 0;
392 if (cif->rtype->type == FFI_TYPE_STRUCT)
394 struct_flags = calc_n32_return_struct_flags(cif->rtype);
396 if (struct_flags == 0)
398 /* This means that the structure is being passed as
402 count = (cif->nargs < 7) ? cif->nargs : 7;
404 cif->rstruct_flag = !0;
407 cif->rstruct_flag = 0;
410 cif->rstruct_flag = 0;
412 while (count-- > 0 && arg_reg < 8)
414 switch ((cif->arg_types)[index]->type)
417 case FFI_TYPE_DOUBLE:
419 ((cif->arg_types)[index]->type << (arg_reg * FFI_FLAG_BITS));
422 case FFI_TYPE_LONGDOUBLE:
424 arg_reg = ALIGN(arg_reg, 2);
425 /* Treat it as two adjacent doubles. */
427 (FFI_TYPE_DOUBLE << (arg_reg * FFI_FLAG_BITS));
430 (FFI_TYPE_DOUBLE << (arg_reg * FFI_FLAG_BITS));
434 case FFI_TYPE_STRUCT:
435 loc = arg_reg * FFI_SIZEOF_ARG;
436 cif->flags += calc_n32_struct_flags((cif->arg_types)[index],
448 /* Set the return type flag */
449 switch (cif->rtype->type)
451 case FFI_TYPE_STRUCT:
453 if (struct_flags == 0)
455 /* The structure is returned through a hidden
456 first argument. Do nothing, 'cause FFI_TYPE_VOID
461 /* The structure is returned via some tricky
463 cif->flags += FFI_TYPE_STRUCT << (FFI_FLAG_BITS * 8);
464 cif->flags += struct_flags << (4 + (FFI_FLAG_BITS * 8));
470 /* Do nothing, 'cause FFI_TYPE_VOID is 0 */
474 case FFI_TYPE_DOUBLE:
475 cif->flags += cif->rtype->type << (FFI_FLAG_BITS * 8);
477 case FFI_TYPE_LONGDOUBLE:
478 /* Long double is returned as if it were a struct containing
480 cif->flags += FFI_TYPE_STRUCT << (FFI_FLAG_BITS * 8);
481 cif->flags += (FFI_TYPE_DOUBLE + (FFI_TYPE_DOUBLE << FFI_FLAG_BITS))
482 << (4 + (FFI_FLAG_BITS * 8));
485 cif->flags += FFI_TYPE_INT << (FFI_FLAG_BITS * 8);
494 /* Low level routine for calling O32 functions */
495 extern int ffi_call_O32(void (*)(char *, extended_cif *, int, int),
496 extended_cif *, unsigned,
497 unsigned, unsigned *, void (*)(void));
499 /* Low level routine for calling N32 functions */
500 extern int ffi_call_N32(void (*)(char *, extended_cif *, int, int),
501 extended_cif *, unsigned,
502 unsigned, unsigned *, void (*)(void));
504 void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
509 ecif.avalue = avalue;
511 /* If the return value is a struct and we don't have a return */
512 /* value address then we need to make one */
514 if ((rvalue == NULL) &&
515 (cif->rtype->type == FFI_TYPE_STRUCT))
516 ecif.rvalue = alloca(cif->rtype->size);
518 ecif.rvalue = rvalue;
524 case FFI_O32_SOFT_FLOAT:
525 ffi_call_O32(ffi_prep_args, &ecif, cif->bytes,
526 cif->flags, ecif.rvalue, fn);
535 void *rvalue_copy = ecif.rvalue;
536 if (cif->rtype->type == FFI_TYPE_STRUCT && cif->rtype->size < 16)
538 /* For structures smaller than 16 bytes we clobber memory
539 in 8 byte increments. Make a copy so we don't clobber
540 the callers memory outside of the struct bounds. */
541 rvalue_copy = alloca(16);
544 ffi_call_N32(ffi_prep_args, &ecif, cif->bytes,
545 cif->flags, rvalue_copy, fn);
547 memcpy(ecif.rvalue, rvalue_copy, cif->rtype->size);
559 #if defined(FFI_MIPS_O32)
560 extern void ffi_closure_O32(void);
562 extern void ffi_closure_N32(void);
563 #endif /* FFI_MIPS_O32 */
566 ffi_prep_closure_loc (ffi_closure *closure,
568 void (*fun)(ffi_cif*,void*,void**,void*),
572 unsigned int *tramp = (unsigned int *) &closure->tramp[0];
574 char *clear_location = (char *) codeloc;
576 #if defined(FFI_MIPS_O32)
577 FFI_ASSERT(cif->abi == FFI_O32 || cif->abi == FFI_O32_SOFT_FLOAT);
578 fn = ffi_closure_O32;
579 #else /* FFI_MIPS_N32 */
580 FFI_ASSERT(cif->abi == FFI_N32 || cif->abi == FFI_N64);
581 fn = ffi_closure_N32;
582 #endif /* FFI_MIPS_O32 */
584 #if defined(FFI_MIPS_O32) || (_MIPS_SIM ==_ABIN32)
585 /* lui $25,high(fn) */
586 tramp[0] = 0x3c190000 | ((unsigned)fn >> 16);
587 /* ori $25,low(fn) */
588 tramp[1] = 0x37390000 | ((unsigned)fn & 0xffff);
589 /* lui $12,high(codeloc) */
590 tramp[2] = 0x3c0c0000 | ((unsigned)codeloc >> 16);
592 tramp[3] = 0x03200008;
593 /* ori $12,low(codeloc) */
594 tramp[4] = 0x358c0000 | ((unsigned)codeloc & 0xffff);
596 /* N64 has a somewhat larger trampoline. */
597 /* lui $25,high(fn) */
598 tramp[0] = 0x3c190000 | ((unsigned long)fn >> 48);
599 /* lui $12,high(codeloc) */
600 tramp[1] = 0x3c0c0000 | ((unsigned long)codeloc >> 48);
601 /* ori $25,mid-high(fn) */
602 tramp[2] = 0x37390000 | (((unsigned long)fn >> 32 ) & 0xffff);
603 /* ori $12,mid-high(codeloc) */
604 tramp[3] = 0x358c0000 | (((unsigned long)codeloc >> 32) & 0xffff);
605 /* dsll $25,$25,16 */
606 tramp[4] = 0x0019cc38;
607 /* dsll $12,$12,16 */
608 tramp[5] = 0x000c6438;
609 /* ori $25,mid-low(fn) */
610 tramp[6] = 0x37390000 | (((unsigned long)fn >> 16 ) & 0xffff);
611 /* ori $12,mid-low(codeloc) */
612 tramp[7] = 0x358c0000 | (((unsigned long)codeloc >> 16) & 0xffff);
613 /* dsll $25,$25,16 */
614 tramp[8] = 0x0019cc38;
615 /* dsll $12,$12,16 */
616 tramp[9] = 0x000c6438;
617 /* ori $25,low(fn) */
618 tramp[10] = 0x37390000 | ((unsigned long)fn & 0xffff);
620 tramp[11] = 0x03200008;
621 /* ori $12,low(codeloc) */
622 tramp[12] = 0x358c0000 | ((unsigned long)codeloc & 0xffff);
628 closure->user_data = user_data;
630 #ifdef USE__BUILTIN___CLEAR_CACHE
631 __builtin___clear_cache(clear_location, clear_location + FFI_TRAMPOLINE_SIZE);
633 cacheflush (clear_location, FFI_TRAMPOLINE_SIZE, ICACHE);
639 * Decodes the arguments to a function, which will be stored on the
640 * stack. AR is the pointer to the beginning of the integer arguments
641 * (and, depending upon the arguments, some floating-point arguments
642 * as well). FPR is a pointer to the area where floating point
643 * registers have been saved, if any.
645 * RVALUE is the location where the function return value will be
646 * stored. CLOSURE is the prepared closure to invoke.
648 * This function should only be called from assembly, which is in
649 * turn called from a trampoline.
651 * Returns the function return type.
653 * Based on the similar routine for sparc.
656 ffi_closure_mips_inner_O32 (ffi_closure *closure,
657 void *rvalue, ffi_arg *ar,
663 ffi_type **arg_types;
664 int i, avn, argn, seen_int;
667 avalue = alloca (cif->nargs * sizeof (ffi_arg));
668 avaluep = alloca (cif->nargs * sizeof (ffi_arg));
670 seen_int = (cif->abi == FFI_O32_SOFT_FLOAT);
673 if ((cif->flags >> (FFI_FLAG_BITS * 2)) == FFI_TYPE_STRUCT)
675 rvalue = (void *)(UINT32)ar[0];
681 arg_types = cif->arg_types;
685 if (i < 2 && !seen_int &&
686 (arg_types[i]->type == FFI_TYPE_FLOAT ||
687 arg_types[i]->type == FFI_TYPE_DOUBLE))
690 if (arg_types[i]->type == FFI_TYPE_FLOAT)
691 avaluep[i] = ((char *) &fpr[i]) + sizeof (float);
694 avaluep[i] = (char *) &fpr[i];
698 if (arg_types[i]->alignment == 8 && (argn & 0x1))
700 switch (arg_types[i]->type)
703 avaluep[i] = &avalue[i];
704 *(SINT8 *) &avalue[i] = (SINT8) ar[argn];
708 avaluep[i] = &avalue[i];
709 *(UINT8 *) &avalue[i] = (UINT8) ar[argn];
712 case FFI_TYPE_SINT16:
713 avaluep[i] = &avalue[i];
714 *(SINT16 *) &avalue[i] = (SINT16) ar[argn];
717 case FFI_TYPE_UINT16:
718 avaluep[i] = &avalue[i];
719 *(UINT16 *) &avalue[i] = (UINT16) ar[argn];
723 avaluep[i] = (char *) &ar[argn];
728 argn += ALIGN(arg_types[i]->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
732 /* Invoke the closure. */
733 (closure->fun) (cif, rvalue, avaluep, closure->user_data);
735 if (cif->abi == FFI_O32_SOFT_FLOAT)
737 switch (cif->rtype->type)
741 case FFI_TYPE_DOUBLE:
742 return FFI_TYPE_UINT64;
744 return cif->rtype->type;
749 return cif->rtype->type;
753 #if defined(FFI_MIPS_N32)
756 copy_struct_N32(char *target, unsigned offset, ffi_abi abi, ffi_type *type,
757 int argn, unsigned arg_offset, ffi_arg *ar,
760 ffi_type **elt_typep = type->elements;
763 ffi_type *elt_type = *elt_typep;
769 o = ALIGN(offset, elt_type->alignment);
770 arg_offset += o - offset;
772 argn += arg_offset / sizeof(ffi_arg);
773 arg_offset = arg_offset % sizeof(ffi_arg);
775 argp = (char *)(ar + argn);
776 fpp = (char *)(argn >= 8 ? ar + argn : fpr + argn);
778 tp = target + offset;
780 if (elt_type->type == FFI_TYPE_DOUBLE)
781 *(double *)tp = *(double *)fpp;
783 memcpy(tp, argp + arg_offset, elt_type->size);
785 offset += elt_type->size;
786 arg_offset += elt_type->size;
788 argn += arg_offset / sizeof(ffi_arg);
789 arg_offset = arg_offset % sizeof(ffi_arg);
794 * Decodes the arguments to a function, which will be stored on the
795 * stack. AR is the pointer to the beginning of the integer
796 * arguments. FPR is a pointer to the area where floating point
797 * registers have been saved.
799 * RVALUE is the location where the function return value will be
800 * stored. CLOSURE is the prepared closure to invoke.
802 * This function should only be called from assembly, which is in
803 * turn called from a trampoline.
805 * Returns the function return flags.
809 ffi_closure_mips_inner_N32 (ffi_closure *closure,
810 void *rvalue, ffi_arg *ar,
816 ffi_type **arg_types;
820 avalue = alloca (cif->nargs * sizeof (ffi_arg));
821 avaluep = alloca (cif->nargs * sizeof (ffi_arg));
825 if (cif->rstruct_flag)
827 #if _MIPS_SIM==_ABIN32
828 rvalue = (void *)(UINT32)ar[0];
830 rvalue = (void *)ar[0];
837 arg_types = cif->arg_types;
841 if (arg_types[i]->type == FFI_TYPE_FLOAT
842 || arg_types[i]->type == FFI_TYPE_DOUBLE)
844 ffi_arg *argp = argn >= 8 ? ar + argn : fpr + argn;
846 if (arg_types[i]->type == FFI_TYPE_FLOAT && argn < 8)
847 avaluep[i] = ((char *) argp) + sizeof (float);
850 avaluep[i] = (char *) argp;
854 unsigned type = arg_types[i]->type;
856 if (arg_types[i]->alignment > sizeof(ffi_arg))
857 argn = ALIGN(argn, arg_types[i]->alignment / sizeof(ffi_arg));
859 ffi_arg *argp = ar + argn;
861 /* The size of a pointer depends on the ABI */
862 if (type == FFI_TYPE_POINTER)
863 type = (cif->abi == FFI_N64) ? FFI_TYPE_SINT64 : FFI_TYPE_SINT32;
868 avaluep[i] = &avalue[i];
869 *(SINT8 *) &avalue[i] = (SINT8) *argp;
873 avaluep[i] = &avalue[i];
874 *(UINT8 *) &avalue[i] = (UINT8) *argp;
877 case FFI_TYPE_SINT16:
878 avaluep[i] = &avalue[i];
879 *(SINT16 *) &avalue[i] = (SINT16) *argp;
882 case FFI_TYPE_UINT16:
883 avaluep[i] = &avalue[i];
884 *(UINT16 *) &avalue[i] = (UINT16) *argp;
887 case FFI_TYPE_SINT32:
888 avaluep[i] = &avalue[i];
889 *(SINT32 *) &avalue[i] = (SINT32) *argp;
892 case FFI_TYPE_UINT32:
893 avaluep[i] = &avalue[i];
894 *(UINT32 *) &avalue[i] = (UINT32) *argp;
897 case FFI_TYPE_STRUCT:
900 /* Allocate space for the struct as at least part of
901 it was passed in registers. */
902 avaluep[i] = alloca(arg_types[i]->size);
903 copy_struct_N32(avaluep[i], 0, cif->abi, arg_types[i],
908 /* Else fall through. */
910 avaluep[i] = (char *) argp;
914 argn += ALIGN(arg_types[i]->size, sizeof(ffi_arg)) / sizeof(ffi_arg);
918 /* Invoke the closure. */
919 (closure->fun) (cif, rvalue, avaluep, closure->user_data);
921 return cif->flags >> (FFI_FLAG_BITS * 8);
924 #endif /* FFI_MIPS_N32 */
926 #endif /* FFI_CLOSURES */