/Modules/_ctypes/libffi/src/x86/ffi.c

http://unladen-swallow.googlecode.com/ · C · 475 lines · 331 code · 85 blank · 59 comment · 45 complexity · 7cf89b614cc2be1b8ac7863c1c3d1ce9 MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi.c - Copyright (c) 1996, 1998, 1999, 2001, 2007, 2008 Red Hat, Inc.
  3. Copyright (c) 2002 Ranjit Mathew
  4. Copyright (c) 2002 Bo Thorsen
  5. Copyright (c) 2002 Roger Sayle
  6. Copyright (C) 2008 Free Software Foundation, Inc.
  7. x86 Foreign Function Interface
  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. The above copyright notice and this permission notice shall be included
  16. in all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. DEALINGS IN THE SOFTWARE.
  25. ----------------------------------------------------------------------- */
  26. #ifndef __x86_64__
  27. #include <ffi.h>
  28. #include <ffi_common.h>
  29. #include <stdlib.h>
  30. /* ffi_prep_args is called by the assembly routine once stack space
  31. has been allocated for the function's arguments */
  32. void ffi_prep_args(char *stack, extended_cif *ecif)
  33. {
  34. register unsigned int i;
  35. register void **p_argv;
  36. register char *argp;
  37. register ffi_type **p_arg;
  38. argp = stack;
  39. if (ecif->cif->flags == FFI_TYPE_STRUCT)
  40. {
  41. *(void **) argp = ecif->rvalue;
  42. argp += 4;
  43. }
  44. p_argv = ecif->avalue;
  45. for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
  46. i != 0;
  47. i--, p_arg++)
  48. {
  49. size_t z;
  50. /* Align if necessary */
  51. if ((sizeof(int) - 1) & (unsigned) argp)
  52. argp = (char *) ALIGN(argp, sizeof(int));
  53. z = (*p_arg)->size;
  54. if (z < sizeof(int))
  55. {
  56. z = sizeof(int);
  57. switch ((*p_arg)->type)
  58. {
  59. case FFI_TYPE_SINT8:
  60. *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
  61. break;
  62. case FFI_TYPE_UINT8:
  63. *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
  64. break;
  65. case FFI_TYPE_SINT16:
  66. *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
  67. break;
  68. case FFI_TYPE_UINT16:
  69. *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
  70. break;
  71. case FFI_TYPE_SINT32:
  72. *(signed int *) argp = (signed int)*(SINT32 *)(* p_argv);
  73. break;
  74. case FFI_TYPE_UINT32:
  75. *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
  76. break;
  77. case FFI_TYPE_STRUCT:
  78. *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
  79. break;
  80. default:
  81. FFI_ASSERT(0);
  82. }
  83. }
  84. else
  85. {
  86. memcpy(argp, *p_argv, z);
  87. }
  88. p_argv++;
  89. argp += z;
  90. }
  91. return;
  92. }
  93. /* Perform machine dependent cif processing */
  94. ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
  95. {
  96. /* Set the return type flag */
  97. switch (cif->rtype->type)
  98. {
  99. case FFI_TYPE_VOID:
  100. #ifdef X86
  101. case FFI_TYPE_STRUCT:
  102. #endif
  103. #if defined(X86) || defined(X86_DARWIN)
  104. case FFI_TYPE_UINT8:
  105. case FFI_TYPE_UINT16:
  106. case FFI_TYPE_SINT8:
  107. case FFI_TYPE_SINT16:
  108. #endif
  109. case FFI_TYPE_SINT64:
  110. case FFI_TYPE_FLOAT:
  111. case FFI_TYPE_DOUBLE:
  112. case FFI_TYPE_LONGDOUBLE:
  113. cif->flags = (unsigned) cif->rtype->type;
  114. break;
  115. case FFI_TYPE_UINT64:
  116. cif->flags = FFI_TYPE_SINT64;
  117. break;
  118. #ifndef X86
  119. case FFI_TYPE_STRUCT:
  120. if (cif->rtype->size == 1)
  121. {
  122. cif->flags = FFI_TYPE_SMALL_STRUCT_1B; /* same as char size */
  123. }
  124. else if (cif->rtype->size == 2)
  125. {
  126. cif->flags = FFI_TYPE_SMALL_STRUCT_2B; /* same as short size */
  127. }
  128. else if (cif->rtype->size == 4)
  129. {
  130. cif->flags = FFI_TYPE_INT; /* same as int type */
  131. }
  132. else if (cif->rtype->size == 8)
  133. {
  134. cif->flags = FFI_TYPE_SINT64; /* same as int64 type */
  135. }
  136. else
  137. {
  138. cif->flags = FFI_TYPE_STRUCT;
  139. }
  140. break;
  141. #endif
  142. default:
  143. cif->flags = FFI_TYPE_INT;
  144. break;
  145. }
  146. #ifdef X86_DARWIN
  147. cif->bytes = (cif->bytes + 15) & ~0xF;
  148. #endif
  149. return FFI_OK;
  150. }
  151. extern void ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *,
  152. unsigned, unsigned, unsigned *, void (*fn)(void));
  153. #ifdef X86_WIN32
  154. extern void ffi_call_STDCALL(void (*)(char *, extended_cif *), extended_cif *,
  155. unsigned, unsigned, unsigned *, void (*fn)(void));
  156. #endif /* X86_WIN32 */
  157. void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
  158. {
  159. extended_cif ecif;
  160. ecif.cif = cif;
  161. ecif.avalue = avalue;
  162. /* If the return value is a struct and we don't have a return */
  163. /* value address then we need to make one */
  164. if ((rvalue == NULL) &&
  165. (cif->flags == FFI_TYPE_STRUCT))
  166. {
  167. ecif.rvalue = alloca(cif->rtype->size);
  168. }
  169. else
  170. ecif.rvalue = rvalue;
  171. switch (cif->abi)
  172. {
  173. case FFI_SYSV:
  174. ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, cif->flags, ecif.rvalue,
  175. fn);
  176. break;
  177. #ifdef X86_WIN32
  178. case FFI_STDCALL:
  179. ffi_call_STDCALL(ffi_prep_args, &ecif, cif->bytes, cif->flags,
  180. ecif.rvalue, fn);
  181. break;
  182. #endif /* X86_WIN32 */
  183. default:
  184. FFI_ASSERT(0);
  185. break;
  186. }
  187. }
  188. /** private members **/
  189. static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
  190. void** args, ffi_cif* cif);
  191. void FFI_HIDDEN ffi_closure_SYSV (ffi_closure *)
  192. __attribute__ ((regparm(1)));
  193. unsigned int FFI_HIDDEN ffi_closure_SYSV_inner (ffi_closure *, void **, void *)
  194. __attribute__ ((regparm(1)));
  195. void FFI_HIDDEN ffi_closure_raw_SYSV (ffi_raw_closure *)
  196. __attribute__ ((regparm(1)));
  197. #ifdef X86_WIN32
  198. void FFI_HIDDEN ffi_closure_STDCALL (ffi_closure *)
  199. __attribute__ ((regparm(1)));
  200. #endif
  201. /* This function is jumped to by the trampoline */
  202. unsigned int FFI_HIDDEN
  203. ffi_closure_SYSV_inner (closure, respp, args)
  204. ffi_closure *closure;
  205. void **respp;
  206. void *args;
  207. {
  208. /* our various things... */
  209. ffi_cif *cif;
  210. void **arg_area;
  211. cif = closure->cif;
  212. arg_area = (void**) alloca (cif->nargs * sizeof (void*));
  213. /* this call will initialize ARG_AREA, such that each
  214. * element in that array points to the corresponding
  215. * value on the stack; and if the function returns
  216. * a structure, it will re-set RESP to point to the
  217. * structure return address. */
  218. ffi_prep_incoming_args_SYSV(args, respp, arg_area, cif);
  219. (closure->fun) (cif, *respp, arg_area, closure->user_data);
  220. return cif->flags;
  221. }
  222. static void
  223. ffi_prep_incoming_args_SYSV(char *stack, void **rvalue, void **avalue,
  224. ffi_cif *cif)
  225. {
  226. register unsigned int i;
  227. register void **p_argv;
  228. register char *argp;
  229. register ffi_type **p_arg;
  230. argp = stack;
  231. if ( cif->flags == FFI_TYPE_STRUCT ) {
  232. *rvalue = *(void **) argp;
  233. argp += 4;
  234. }
  235. p_argv = avalue;
  236. for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
  237. {
  238. size_t z;
  239. /* Align if necessary */
  240. if ((sizeof(int) - 1) & (unsigned) argp) {
  241. argp = (char *) ALIGN(argp, sizeof(int));
  242. }
  243. z = (*p_arg)->size;
  244. /* because we're little endian, this is what it turns into. */
  245. *p_argv = (void*) argp;
  246. p_argv++;
  247. argp += z;
  248. }
  249. return;
  250. }
  251. /* How to make a trampoline. Derived from gcc/config/i386/i386.c. */
  252. #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
  253. ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
  254. unsigned int __fun = (unsigned int)(FUN); \
  255. unsigned int __ctx = (unsigned int)(CTX); \
  256. unsigned int __dis = __fun - (__ctx + 10); \
  257. *(unsigned char*) &__tramp[0] = 0xb8; \
  258. *(unsigned int*) &__tramp[1] = __ctx; /* movl __ctx, %eax */ \
  259. *(unsigned char *) &__tramp[5] = 0xe9; \
  260. *(unsigned int*) &__tramp[6] = __dis; /* jmp __fun */ \
  261. })
  262. #define FFI_INIT_TRAMPOLINE_STDCALL(TRAMP,FUN,CTX,SIZE) \
  263. ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
  264. unsigned int __fun = (unsigned int)(FUN); \
  265. unsigned int __ctx = (unsigned int)(CTX); \
  266. unsigned int __dis = __fun - (__ctx + 10); \
  267. unsigned short __size = (unsigned short)(SIZE); \
  268. *(unsigned char*) &__tramp[0] = 0xb8; \
  269. *(unsigned int*) &__tramp[1] = __ctx; /* movl __ctx, %eax */ \
  270. *(unsigned char *) &__tramp[5] = 0xe8; \
  271. *(unsigned int*) &__tramp[6] = __dis; /* call __fun */ \
  272. *(unsigned char *) &__tramp[10] = 0xc2; \
  273. *(unsigned short*) &__tramp[11] = __size; /* ret __size */ \
  274. })
  275. /* the cif must already be prep'ed */
  276. ffi_status
  277. ffi_prep_closure_loc (ffi_closure* closure,
  278. ffi_cif* cif,
  279. void (*fun)(ffi_cif*,void*,void**,void*),
  280. void *user_data,
  281. void *codeloc)
  282. {
  283. if (cif->abi == FFI_SYSV)
  284. {
  285. FFI_INIT_TRAMPOLINE (&closure->tramp[0],
  286. &ffi_closure_SYSV,
  287. (void*)codeloc);
  288. }
  289. #ifdef X86_WIN32
  290. else if (cif->abi == FFI_STDCALL)
  291. {
  292. FFI_INIT_TRAMPOLINE_STDCALL (&closure->tramp[0],
  293. &ffi_closure_STDCALL,
  294. (void*)codeloc, cif->bytes);
  295. }
  296. #endif
  297. else
  298. {
  299. return FFI_BAD_ABI;
  300. }
  301. closure->cif = cif;
  302. closure->user_data = user_data;
  303. closure->fun = fun;
  304. return FFI_OK;
  305. }
  306. /* ------- Native raw API support -------------------------------- */
  307. #if !FFI_NO_RAW_API
  308. ffi_status
  309. ffi_prep_raw_closure_loc (ffi_raw_closure* closure,
  310. ffi_cif* cif,
  311. void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
  312. void *user_data,
  313. void *codeloc)
  314. {
  315. int i;
  316. if (cif->abi != FFI_SYSV) {
  317. return FFI_BAD_ABI;
  318. }
  319. /* we currently don't support certain kinds of arguments for raw
  320. // closures. This should be implemented by a separate assembly language
  321. // routine, since it would require argument processing, something we
  322. // don't do now for performance. */
  323. for (i = cif->nargs-1; i >= 0; i--)
  324. {
  325. FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_STRUCT);
  326. FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_LONGDOUBLE);
  327. }
  328. FFI_INIT_TRAMPOLINE (&closure->tramp[0], &ffi_closure_raw_SYSV,
  329. codeloc);
  330. closure->cif = cif;
  331. closure->user_data = user_data;
  332. closure->fun = fun;
  333. return FFI_OK;
  334. }
  335. static void
  336. ffi_prep_args_raw(char *stack, extended_cif *ecif)
  337. {
  338. memcpy (stack, ecif->avalue, ecif->cif->bytes);
  339. }
  340. /* we borrow this routine from libffi (it must be changed, though, to
  341. * actually call the function passed in the first argument. as of
  342. * libffi-1.20, this is not the case.)
  343. */
  344. extern void
  345. ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *, unsigned,
  346. unsigned, unsigned *, void (*fn)(void));
  347. #ifdef X86_WIN32
  348. extern void
  349. ffi_call_STDCALL(void (*)(char *, extended_cif *), extended_cif *, unsigned,
  350. unsigned, unsigned *, void (*fn)(void));
  351. #endif /* X86_WIN32 */
  352. void
  353. ffi_raw_call(ffi_cif *cif, void (*fn)(void), void *rvalue, ffi_raw *fake_avalue)
  354. {
  355. extended_cif ecif;
  356. void **avalue = (void **)fake_avalue;
  357. ecif.cif = cif;
  358. ecif.avalue = avalue;
  359. /* If the return value is a struct and we don't have a return */
  360. /* value address then we need to make one */
  361. if ((rvalue == NULL) &&
  362. (cif->rtype->type == FFI_TYPE_STRUCT))
  363. {
  364. ecif.rvalue = alloca(cif->rtype->size);
  365. }
  366. else
  367. ecif.rvalue = rvalue;
  368. switch (cif->abi)
  369. {
  370. case FFI_SYSV:
  371. ffi_call_SYSV(ffi_prep_args_raw, &ecif, cif->bytes, cif->flags,
  372. ecif.rvalue, fn);
  373. break;
  374. #ifdef X86_WIN32
  375. case FFI_STDCALL:
  376. ffi_call_STDCALL(ffi_prep_args_raw, &ecif, cif->bytes, cif->flags,
  377. ecif.rvalue, fn);
  378. break;
  379. #endif /* X86_WIN32 */
  380. default:
  381. FFI_ASSERT(0);
  382. break;
  383. }
  384. }
  385. #endif
  386. #endif /* __x86_64__ */