PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libffi/src/avr32/ffi.c

https://gitlab.com/fskolodny/ecl
C | 423 lines | 309 code | 62 blank | 52 comment | 84 complexity | 1e7f2be6a7bb2c081a285a402215a364 MD5 | raw file
  1. /* -----------------------------------------------------------------------
  2. ffi.c - Copyright (c) 2011 Anthony Green
  3. Copyright (c) 2009 Bradley Smith <brad@brad-smith.co.uk>
  4. AVR32 Foreign Function Interface
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. ``Software''), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be included
  13. in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. DEALINGS IN THE SOFTWARE.
  22. ----------------------------------------------------------------------- */
  23. #include <ffi.h>
  24. #include <ffi_common.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <asm/unistd.h>
  29. /* #define DEBUG */
  30. extern void ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *,
  31. unsigned int, unsigned int, unsigned int*, unsigned int,
  32. void (*fn)(void));
  33. extern void ffi_closure_SYSV (ffi_closure *);
  34. unsigned int pass_struct_on_stack(ffi_type *type)
  35. {
  36. if(type->type != FFI_TYPE_STRUCT)
  37. return 0;
  38. if(type->alignment < type->size &&
  39. !(type->size == 4 || type->size == 8) &&
  40. !(type->size == 8 && type->alignment >= 4))
  41. return 1;
  42. if(type->size == 3 || type->size == 5 || type->size == 6 ||
  43. type->size == 7)
  44. return 1;
  45. return 0;
  46. }
  47. /* ffi_prep_args is called by the assembly routine once stack space
  48. * has been allocated for the function's arguments
  49. *
  50. * This is annoyingly complex since we need to keep track of used
  51. * registers.
  52. */
  53. void ffi_prep_args(char *stack, extended_cif *ecif)
  54. {
  55. unsigned int i;
  56. void **p_argv;
  57. ffi_type **p_arg;
  58. char *reg_base = stack;
  59. char *stack_base = stack + 20;
  60. unsigned int stack_offset = 0;
  61. unsigned int reg_mask = 0;
  62. p_argv = ecif->avalue;
  63. /* If cif->flags is struct then we know it's not passed in registers */
  64. if(ecif->cif->flags == FFI_TYPE_STRUCT)
  65. {
  66. *(void**)reg_base = ecif->rvalue;
  67. reg_mask |= 1;
  68. }
  69. for(i = 0, p_arg = ecif->cif->arg_types; i < ecif->cif->nargs;
  70. i++, p_arg++)
  71. {
  72. size_t z = (*p_arg)->size;
  73. int alignment = (*p_arg)->alignment;
  74. int type = (*p_arg)->type;
  75. char *addr = 0;
  76. if(z % 4 != 0)
  77. z += (4 - z % 4);
  78. if(reg_mask != 0x1f)
  79. {
  80. if(pass_struct_on_stack(*p_arg))
  81. {
  82. addr = stack_base + stack_offset;
  83. stack_offset += z;
  84. }
  85. else if(z == sizeof(int))
  86. {
  87. char index = 0;
  88. while((reg_mask >> index) & 1)
  89. index++;
  90. addr = reg_base + (index * 4);
  91. reg_mask |= (1 << index);
  92. }
  93. else if(z == 2 * sizeof(int))
  94. {
  95. if(!((reg_mask >> 1) & 1))
  96. {
  97. addr = reg_base + 4;
  98. reg_mask |= (3 << 1);
  99. }
  100. else if(!((reg_mask >> 3) & 1))
  101. {
  102. addr = reg_base + 12;
  103. reg_mask |= (3 << 3);
  104. }
  105. }
  106. }
  107. if(!addr)
  108. {
  109. addr = stack_base + stack_offset;
  110. stack_offset += z;
  111. }
  112. if(type == FFI_TYPE_STRUCT && (*p_arg)->elements[1] == NULL)
  113. type = (*p_arg)->elements[0]->type;
  114. switch(type)
  115. {
  116. case FFI_TYPE_UINT8:
  117. *(unsigned int *)addr = (unsigned int)*(UINT8 *)(*p_argv);
  118. break;
  119. case FFI_TYPE_SINT8:
  120. *(signed int *)addr = (signed int)*(SINT8 *)(*p_argv);
  121. break;
  122. case FFI_TYPE_UINT16:
  123. *(unsigned int *)addr = (unsigned int)*(UINT16 *)(*p_argv);
  124. break;
  125. case FFI_TYPE_SINT16:
  126. *(signed int *)addr = (signed int)*(SINT16 *)(*p_argv);
  127. break;
  128. default:
  129. memcpy(addr, *p_argv, z);
  130. }
  131. p_argv++;
  132. }
  133. #ifdef DEBUG
  134. /* Debugging */
  135. for(i = 0; i < 5; i++)
  136. {
  137. if((reg_mask & (1 << i)) == 0)
  138. printf("r%d: (unused)\n", 12 - i);
  139. else
  140. printf("r%d: 0x%08x\n", 12 - i, ((unsigned int*)reg_base)[i]);
  141. }
  142. for(i = 0; i < stack_offset / 4; i++)
  143. {
  144. printf("sp+%d: 0x%08x\n", i*4, ((unsigned int*)stack_base)[i]);
  145. }
  146. #endif
  147. }
  148. /* Perform machine dependent cif processing */
  149. ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
  150. {
  151. /* Round the stack up to a multiple of 8 bytes. This isn't needed
  152. * everywhere, but it is on some platforms, and it doesn't harm
  153. * anything when it isn't needed. */
  154. cif->bytes = (cif->bytes + 7) & ~7;
  155. /* Flag to indicate that he return value is in fact a struct */
  156. cif->rstruct_flag = 0;
  157. /* Set the return type flag */
  158. switch(cif->rtype->type)
  159. {
  160. case FFI_TYPE_SINT8:
  161. case FFI_TYPE_UINT8:
  162. cif->flags = (unsigned)FFI_TYPE_UINT8;
  163. break;
  164. case FFI_TYPE_SINT16:
  165. case FFI_TYPE_UINT16:
  166. cif->flags = (unsigned)FFI_TYPE_UINT16;
  167. break;
  168. case FFI_TYPE_FLOAT:
  169. case FFI_TYPE_SINT32:
  170. case FFI_TYPE_UINT32:
  171. case FFI_TYPE_POINTER:
  172. cif->flags = (unsigned)FFI_TYPE_UINT32;
  173. break;
  174. case FFI_TYPE_DOUBLE:
  175. case FFI_TYPE_SINT64:
  176. case FFI_TYPE_UINT64:
  177. cif->flags = (unsigned)FFI_TYPE_UINT64;
  178. break;
  179. case FFI_TYPE_STRUCT:
  180. cif->rstruct_flag = 1;
  181. if(!pass_struct_on_stack(cif->rtype))
  182. {
  183. if(cif->rtype->size <= 1)
  184. cif->flags = (unsigned)FFI_TYPE_UINT8;
  185. else if(cif->rtype->size <= 2)
  186. cif->flags = (unsigned)FFI_TYPE_UINT16;
  187. else if(cif->rtype->size <= 4)
  188. cif->flags = (unsigned)FFI_TYPE_UINT32;
  189. else if(cif->rtype->size <= 8)
  190. cif->flags = (unsigned)FFI_TYPE_UINT64;
  191. else
  192. cif->flags = (unsigned)cif->rtype->type;
  193. }
  194. else
  195. cif->flags = (unsigned)cif->rtype->type;
  196. break;
  197. default:
  198. cif->flags = (unsigned)cif->rtype->type;
  199. break;
  200. }
  201. return FFI_OK;
  202. }
  203. void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
  204. {
  205. extended_cif ecif;
  206. unsigned int size = 0, i = 0;
  207. ffi_type **p_arg;
  208. ecif.cif = cif;
  209. ecif.avalue = avalue;
  210. for(i = 0, p_arg = cif->arg_types; i < cif->nargs; i++, p_arg++)
  211. size += (*p_arg)->size + (4 - (*p_arg)->size % 4);
  212. /* If the return value is a struct and we don't have a return value
  213. * address then we need to make one */
  214. /* If cif->flags is struct then it's not suitable for registers */
  215. if((rvalue == NULL) && (cif->flags == FFI_TYPE_STRUCT))
  216. ecif.rvalue = alloca(cif->rtype->size);
  217. else
  218. ecif.rvalue = rvalue;
  219. switch(cif->abi)
  220. {
  221. case FFI_SYSV:
  222. ffi_call_SYSV(ffi_prep_args, &ecif, size, cif->flags,
  223. ecif.rvalue, cif->rstruct_flag, fn);
  224. break;
  225. default:
  226. FFI_ASSERT(0);
  227. break;
  228. }
  229. }
  230. static void ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
  231. void **avalue, ffi_cif *cif)
  232. {
  233. register unsigned int i, reg_mask = 0;
  234. register void **p_argv;
  235. register ffi_type **p_arg;
  236. register char *reg_base = stack;
  237. register char *stack_base = stack + 20;
  238. register unsigned int stack_offset = 0;
  239. #ifdef DEBUG
  240. /* Debugging */
  241. for(i = 0; i < cif->nargs + 7; i++)
  242. {
  243. printf("sp+%d: 0x%08x\n", i*4, ((unsigned int*)stack)[i]);
  244. }
  245. #endif
  246. /* If cif->flags is struct then we know it's not passed in registers */
  247. if(cif->flags == FFI_TYPE_STRUCT)
  248. {
  249. *rvalue = *(void **)reg_base;
  250. reg_mask |= 1;
  251. }
  252. p_argv = avalue;
  253. for(i = 0, p_arg = cif->arg_types; i < cif->nargs; i++, p_arg++)
  254. {
  255. size_t z = (*p_arg)->size;
  256. int alignment = (*p_arg)->alignment;
  257. *p_argv = 0;
  258. if(z % 4 != 0)
  259. z += (4 - z % 4);
  260. if(reg_mask != 0x1f)
  261. {
  262. if(pass_struct_on_stack(*p_arg))
  263. {
  264. *p_argv = (void*)stack_base + stack_offset;
  265. stack_offset += z;
  266. }
  267. else if(z <= sizeof(int))
  268. {
  269. char index = 0;
  270. while((reg_mask >> index) & 1)
  271. index++;
  272. *p_argv = (void*)reg_base + (index * 4);
  273. reg_mask |= (1 << index);
  274. }
  275. else if(z == 2 * sizeof(int))
  276. {
  277. if(!((reg_mask >> 1) & 1))
  278. {
  279. *p_argv = (void*)reg_base + 4;
  280. reg_mask |= (3 << 1);
  281. }
  282. else if(!((reg_mask >> 3) & 1))
  283. {
  284. *p_argv = (void*)reg_base + 12;
  285. reg_mask |= (3 << 3);
  286. }
  287. }
  288. }
  289. if(!*p_argv)
  290. {
  291. *p_argv = (void*)stack_base + stack_offset;
  292. stack_offset += z;
  293. }
  294. if((*p_arg)->type != FFI_TYPE_STRUCT ||
  295. (*p_arg)->elements[1] == NULL)
  296. {
  297. if(alignment == 1)
  298. **(unsigned int**)p_argv <<= 24;
  299. else if(alignment == 2)
  300. **(unsigned int**)p_argv <<= 16;
  301. }
  302. p_argv++;
  303. }
  304. #ifdef DEBUG
  305. /* Debugging */
  306. for(i = 0; i < cif->nargs; i++)
  307. {
  308. printf("sp+%d: 0x%08x\n", i*4, *(((unsigned int**)avalue)[i]));
  309. }
  310. #endif
  311. }
  312. /* This function is jumped to by the trampoline */
  313. unsigned int ffi_closure_SYSV_inner(ffi_closure *closure, void **respp,
  314. void *args)
  315. {
  316. ffi_cif *cif;
  317. void **arg_area;
  318. unsigned int i, size = 0;
  319. ffi_type **p_arg;
  320. cif = closure->cif;
  321. for(i = 0, p_arg = cif->arg_types; i < cif->nargs; i++, p_arg++)
  322. size += (*p_arg)->size + (4 - (*p_arg)->size % 4);
  323. arg_area = (void **)alloca(size);
  324. /* this call will initialize ARG_AREA, such that each element in that
  325. * array points to the corresponding value on the stack; and if the
  326. * function returns a structure, it will re-set RESP to point to the
  327. * structure return address. */
  328. ffi_prep_incoming_args_SYSV(args, respp, arg_area, cif);
  329. (closure->fun)(cif, *respp, arg_area, closure->user_data);
  330. return cif->flags;
  331. }
  332. ffi_status ffi_prep_closure_loc(ffi_closure* closure, ffi_cif* cif,
  333. void (*fun)(ffi_cif*, void*, void**, void*), void *user_data,
  334. void *codeloc)
  335. {
  336. if (cif->abi != FFI_SYSV)
  337. return FFI_BAD_ABI;
  338. unsigned char *__tramp = (unsigned char*)(&closure->tramp[0]);
  339. unsigned int __fun = (unsigned int)(&ffi_closure_SYSV);
  340. unsigned int __ctx = (unsigned int)(codeloc);
  341. unsigned int __rstruct_flag = (unsigned int)(cif->rstruct_flag);
  342. unsigned int __inner = (unsigned int)(&ffi_closure_SYSV_inner);
  343. *(unsigned int*) &__tramp[0] = 0xebcd1f00; /* pushm r8-r12 */
  344. *(unsigned int*) &__tramp[4] = 0xfefc0010; /* ld.w r12, pc[16] */
  345. *(unsigned int*) &__tramp[8] = 0xfefb0010; /* ld.w r11, pc[16] */
  346. *(unsigned int*) &__tramp[12] = 0xfefa0010; /* ld.w r10, pc[16] */
  347. *(unsigned int*) &__tramp[16] = 0xfeff0010; /* ld.w pc, pc[16] */
  348. *(unsigned int*) &__tramp[20] = __ctx;
  349. *(unsigned int*) &__tramp[24] = __rstruct_flag;
  350. *(unsigned int*) &__tramp[28] = __inner;
  351. *(unsigned int*) &__tramp[32] = __fun;
  352. syscall(__NR_cacheflush, 0, (&__tramp[0]), 36);
  353. closure->cif = cif;
  354. closure->user_data = user_data;
  355. closure->fun = fun;
  356. return FFI_OK;
  357. }