ffi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* -----------------------------------------------------------------------
  2. ffi.c
  3. CSKY Foreign Function Interface
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. ``Software''), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be included
  12. in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. DEALINGS IN THE SOFTWARE.
  21. ----------------------------------------------------------------------- */
  22. #include <ffi.h>
  23. #include <ffi_common.h>
  24. #include <stdlib.h>
  25. /* ffi_prep_args is called by the assembly routine once stack space
  26. has been allocated for the function's arguments
  27. */
  28. void ffi_prep_args(char *stack, extended_cif *ecif)
  29. {
  30. register unsigned int i;
  31. register void **p_argv;
  32. register char *argp;
  33. register ffi_type **p_arg;
  34. argp = stack;
  35. if ( ecif->cif->flags == FFI_TYPE_STRUCT ) {
  36. *(void **) argp = ecif->rvalue;
  37. argp += 4;
  38. }
  39. p_argv = ecif->avalue;
  40. for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
  41. (i != 0);
  42. i--, p_arg++)
  43. {
  44. size_t z;
  45. size_t alignment;
  46. /* Align if necessary */
  47. alignment = (*p_arg)->alignment;
  48. #ifdef __CSKYABIV1__
  49. /*
  50. * Adapt ABIV1 bug.
  51. * If struct's size is larger than 8 bytes, then it always alignment as 4 bytes.
  52. */
  53. if (((*p_arg)->type == FFI_TYPE_STRUCT) && ((*p_arg)->size > 8) && (alignment == 8)) {
  54. alignment = 4;
  55. }
  56. #endif
  57. if ((alignment - 1) & (unsigned) argp) {
  58. argp = (char *) FFI_ALIGN(argp, alignment);
  59. }
  60. if ((*p_arg)->type == FFI_TYPE_STRUCT)
  61. argp = (char *) FFI_ALIGN(argp, 4);
  62. z = (*p_arg)->size;
  63. if (z < sizeof(int))
  64. {
  65. z = sizeof(int);
  66. switch ((*p_arg)->type)
  67. {
  68. case FFI_TYPE_SINT8:
  69. *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
  70. break;
  71. case FFI_TYPE_UINT8:
  72. *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
  73. break;
  74. case FFI_TYPE_SINT16:
  75. *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
  76. break;
  77. case FFI_TYPE_UINT16:
  78. *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
  79. break;
  80. case FFI_TYPE_STRUCT:
  81. #ifdef __CSKYBE__
  82. memcpy((argp + 4 - (*p_arg)->size), *p_argv, (*p_arg)->size);
  83. #else
  84. memcpy(argp, *p_argv, (*p_arg)->size);
  85. #endif
  86. break;
  87. default:
  88. FFI_ASSERT(0);
  89. }
  90. }
  91. else if (z == sizeof(int))
  92. {
  93. *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
  94. }
  95. else
  96. {
  97. memcpy(argp, *p_argv, z);
  98. }
  99. p_argv++;
  100. argp += z;
  101. }
  102. return;
  103. }
  104. /* Perform machine dependent cif processing */
  105. ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
  106. {
  107. /* Round the stack up to a multiple of 8 bytes. This isn't needed
  108. everywhere, but it is on some platforms, and it doesn't hcsky anything
  109. when it isn't needed. */
  110. cif->bytes = (cif->bytes + 7) & ~7;
  111. /* Set the return type flag */
  112. switch (cif->rtype->type)
  113. {
  114. case FFI_TYPE_DOUBLE:
  115. case FFI_TYPE_SINT64:
  116. case FFI_TYPE_UINT64:
  117. cif->flags = (unsigned) FFI_TYPE_SINT64;
  118. break;
  119. case FFI_TYPE_STRUCT:
  120. if (cif->rtype->size <= 4)
  121. /* A Composite Type not larger than 4 bytes is returned in r0. */
  122. cif->flags = (unsigned)FFI_TYPE_INT;
  123. else if (cif->rtype->size <= 8)
  124. /* A Composite Type not larger than 8 bytes is returned in r0, r1. */
  125. cif->flags = (unsigned)FFI_TYPE_SINT64;
  126. else
  127. /* A Composite Type larger than 8 bytes, or whose size cannot
  128. be determined statically ... is stored in memory at an
  129. address passed [in r0]. */
  130. cif->flags = (unsigned)FFI_TYPE_STRUCT;
  131. break;
  132. default:
  133. cif->flags = FFI_TYPE_INT;
  134. break;
  135. }
  136. return FFI_OK;
  137. }
  138. /* Perform machine dependent cif processing for variadic calls */
  139. ffi_status ffi_prep_cif_machdep_var(ffi_cif *cif,
  140. unsigned int nfixedargs,
  141. unsigned int ntotalargs)
  142. {
  143. return ffi_prep_cif_machdep(cif);
  144. }
  145. /* Prototypes for assembly functions, in sysv.S */
  146. extern void ffi_call_SYSV (void (*fn)(void), extended_cif *, unsigned, unsigned, unsigned *);
  147. void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
  148. {
  149. extended_cif ecif;
  150. int small_struct = (cif->flags == FFI_TYPE_INT
  151. && cif->rtype->type == FFI_TYPE_STRUCT);
  152. ecif.cif = cif;
  153. ecif.avalue = avalue;
  154. unsigned int temp;
  155. /* If the return value is a struct and we don't have a return */
  156. /* value address then we need to make one */
  157. if ((rvalue == NULL) &&
  158. (cif->flags == FFI_TYPE_STRUCT))
  159. {
  160. ecif.rvalue = alloca(cif->rtype->size);
  161. }
  162. else if (small_struct)
  163. ecif.rvalue = &temp;
  164. else
  165. ecif.rvalue = rvalue;
  166. switch (cif->abi)
  167. {
  168. case FFI_SYSV:
  169. ffi_call_SYSV (fn, &ecif, cif->bytes, cif->flags, ecif.rvalue);
  170. break;
  171. default:
  172. FFI_ASSERT(0);
  173. break;
  174. }
  175. if (small_struct)
  176. #ifdef __CSKYBE__
  177. memcpy (rvalue, ((unsigned char *)&temp + (4 - cif->rtype->size)), cif->rtype->size);
  178. #else
  179. memcpy (rvalue, &temp, cif->rtype->size);
  180. #endif
  181. }
  182. /** private members **/
  183. static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
  184. void** args, ffi_cif* cif);
  185. void ffi_closure_SYSV (ffi_closure *);
  186. /* This function is jumped to by the trampoline */
  187. unsigned int
  188. ffi_closure_SYSV_inner (closure, respp, args)
  189. ffi_closure *closure;
  190. void **respp;
  191. void *args;
  192. {
  193. // our various things...
  194. ffi_cif *cif;
  195. void **arg_area;
  196. cif = closure->cif;
  197. arg_area = (void**) alloca (cif->nargs * sizeof (void*));
  198. /* this call will initialize ARG_AREA, such that each
  199. * element in that array points to the corresponding
  200. * value on the stack; and if the function returns
  201. * a structure, it will re-set RESP to point to the
  202. * structure return address. */
  203. ffi_prep_incoming_args_SYSV(args, respp, arg_area, cif);
  204. (closure->fun) (cif, *respp, arg_area, closure->user_data);
  205. #ifdef __CSKYBE__
  206. if (cif->flags == FFI_TYPE_INT && cif->rtype->type == FFI_TYPE_STRUCT) {
  207. unsigned int tmp = 0;
  208. tmp = *(unsigned int *)(*respp);
  209. *(unsigned int *)(*respp) = (tmp >> ((4 - cif->rtype->size) * 8));
  210. }
  211. #endif
  212. return cif->flags;
  213. }
  214. static void
  215. ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
  216. void **avalue, ffi_cif *cif)
  217. {
  218. register unsigned int i;
  219. register void **p_argv;
  220. register char *argp;
  221. register ffi_type **p_arg;
  222. argp = stack;
  223. if ( cif->flags == FFI_TYPE_STRUCT ) {
  224. *rvalue = *(void **) argp;
  225. argp += 4;
  226. }
  227. p_argv = avalue;
  228. for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
  229. {
  230. size_t z;
  231. size_t alignment;
  232. alignment = (*p_arg)->alignment;
  233. if (alignment < 4)
  234. alignment = 4;
  235. #ifdef __CSKYABIV1__
  236. /*
  237. * Adapt ABIV1 bug.
  238. * If struct's size is larger than 8 bytes, then it always alignment as 4 bytes.
  239. */
  240. if (((*p_arg)->type == FFI_TYPE_STRUCT) && ((*p_arg)->size > 8) && (alignment == 8)) {
  241. alignment = 4;
  242. }
  243. #endif
  244. /* Align if necessary */
  245. if ((alignment - 1) & (unsigned) argp) {
  246. argp = (char *) FFI_ALIGN(argp, alignment);
  247. }
  248. z = (*p_arg)->size;
  249. #ifdef __CSKYBE__
  250. unsigned int tmp = 0;
  251. if ((*p_arg)->size < 4) {
  252. tmp = *(unsigned int *)argp;
  253. memcpy(argp, ((unsigned char *)&tmp + (4 - (*p_arg)->size)), (*p_arg)->size);
  254. }
  255. #else
  256. /* because we're little endian, this is what it turns into. */
  257. #endif
  258. *p_argv = (void*) argp;
  259. p_argv++;
  260. argp += z;
  261. }
  262. return;
  263. }
  264. /* How to make a trampoline. */
  265. extern unsigned char ffi_csky_trampoline[TRAMPOLINE_SIZE];
  266. /*
  267. * Since there is no __clear_cache in libgcc in csky toolchain.
  268. * define ffi_csky_cacheflush in sysv.S.
  269. * void ffi_csky_cacheflush(uint32 start_addr, uint32 size, int cache)
  270. */
  271. #define CACHEFLUSH_IN_FFI 1
  272. #if CACHEFLUSH_IN_FFI
  273. extern void ffi_csky_cacheflush(unsigned char *__tramp, unsigned int k,
  274. int i);
  275. #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
  276. ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
  277. unsigned int __fun = (unsigned int)(FUN); \
  278. unsigned int __ctx = (unsigned int)(CTX); \
  279. unsigned char *insns = (unsigned char *)(CTX); \
  280. memcpy (__tramp, ffi_csky_trampoline, TRAMPOLINE_SIZE); \
  281. *(unsigned int*) &__tramp[TRAMPOLINE_SIZE] = __ctx; \
  282. *(unsigned int*) &__tramp[TRAMPOLINE_SIZE + 4] = __fun; \
  283. ffi_csky_cacheflush(&__tramp[0], TRAMPOLINE_SIZE, 3); /* Clear data mapping. */ \
  284. ffi_csky_cacheflush(insns, TRAMPOLINE_SIZE, 3); \
  285. /* Clear instruction \
  286. mapping. */ \
  287. })
  288. #else
  289. #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
  290. ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
  291. unsigned int __fun = (unsigned int)(FUN); \
  292. unsigned int __ctx = (unsigned int)(CTX); \
  293. unsigned char *insns = (unsigned char *)(CTX); \
  294. memcpy (__tramp, ffi_csky_trampoline, TRAMPOLINE_SIZE); \
  295. *(unsigned int*) &__tramp[TRAMPOLINE_SIZE] = __ctx; \
  296. *(unsigned int*) &__tramp[TRAMPOLINE_SIZE + 4] = __fun; \
  297. __clear_cache((&__tramp[0]), (&__tramp[TRAMPOLINE_SIZE-1])); /* Clear data mapping. */ \
  298. __clear_cache(insns, insns + TRAMPOLINE_SIZE); \
  299. /* Clear instruction \
  300. mapping. */ \
  301. })
  302. #endif
  303. /* the cif must already be prep'ed */
  304. ffi_status
  305. ffi_prep_closure_loc (ffi_closure* closure,
  306. ffi_cif* cif,
  307. void (*fun)(ffi_cif*,void*,void**,void*),
  308. void *user_data,
  309. void *codeloc)
  310. {
  311. void (*closure_func)(ffi_closure*) = NULL;
  312. if (cif->abi == FFI_SYSV)
  313. closure_func = &ffi_closure_SYSV;
  314. else
  315. return FFI_BAD_ABI;
  316. FFI_INIT_TRAMPOLINE (&closure->tramp[0], \
  317. closure_func, \
  318. codeloc);
  319. closure->cif = cif;
  320. closure->user_data = user_data;
  321. closure->fun = fun;
  322. return FFI_OK;
  323. }