/Modules/_ctypes/libffi_osx/x86/x86-ffi64.c

http://unladen-swallow.googlecode.com/ · C · 624 lines · 422 code · 105 blank · 97 comment · 141 complexity · e7f83d9c9dea00d92bd1e8f0b1ee0983 MD5 · raw file

  1. #ifdef __x86_64__
  2. /* -----------------------------------------------------------------------
  3. x86-ffi64.c - Copyright (c) 2002 Bo Thorsen <bo@suse.de>
  4. x86-64 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, EXPRESS
  15. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. ----------------------------------------------------------------------- */
  22. #include <ffi.h>
  23. #include <ffi_common.h>
  24. #include <stdlib.h>
  25. #include <stdarg.h>
  26. #define MAX_GPR_REGS 6
  27. #define MAX_SSE_REGS 8
  28. typedef struct RegisterArgs {
  29. /* Registers for argument passing. */
  30. UINT64 gpr[MAX_GPR_REGS];
  31. __int128_t sse[MAX_SSE_REGS];
  32. } RegisterArgs;
  33. extern void
  34. ffi_call_unix64(
  35. void* args,
  36. unsigned long bytes,
  37. unsigned flags,
  38. void* raddr,
  39. void (*fnaddr)(),
  40. unsigned ssecount);
  41. /* All reference to register classes here is identical to the code in
  42. gcc/config/i386/i386.c. Do *not* change one without the other. */
  43. /* Register class used for passing given 64bit part of the argument.
  44. These represent classes as documented by the PS ABI, with the exception
  45. of SSESF, SSEDF classes, that are basically SSE class, just gcc will
  46. use SF or DFmode move instead of DImode to avoid reformating penalties.
  47. Similary we play games with INTEGERSI_CLASS to use cheaper SImode moves
  48. whenever possible (upper half does contain padding). */
  49. enum x86_64_reg_class
  50. {
  51. X86_64_NO_CLASS,
  52. X86_64_INTEGER_CLASS,
  53. X86_64_INTEGERSI_CLASS,
  54. X86_64_SSE_CLASS,
  55. X86_64_SSESF_CLASS,
  56. X86_64_SSEDF_CLASS,
  57. X86_64_SSEUP_CLASS,
  58. X86_64_X87_CLASS,
  59. X86_64_X87UP_CLASS,
  60. X86_64_COMPLEX_X87_CLASS,
  61. X86_64_MEMORY_CLASS
  62. };
  63. #define MAX_CLASSES 4
  64. #define SSE_CLASS_P(X) ((X) >= X86_64_SSE_CLASS && X <= X86_64_SSEUP_CLASS)
  65. /* x86-64 register passing implementation. See x86-64 ABI for details. Goal
  66. of this code is to classify each 8bytes of incoming argument by the register
  67. class and assign registers accordingly. */
  68. /* Return the union class of CLASS1 and CLASS2.
  69. See the x86-64 PS ABI for details. */
  70. static enum x86_64_reg_class
  71. merge_classes(
  72. enum x86_64_reg_class class1,
  73. enum x86_64_reg_class class2)
  74. {
  75. /* Rule #1: If both classes are equal, this is the resulting class. */
  76. if (class1 == class2)
  77. return class1;
  78. /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
  79. the other class. */
  80. if (class1 == X86_64_NO_CLASS)
  81. return class2;
  82. if (class2 == X86_64_NO_CLASS)
  83. return class1;
  84. /* Rule #3: If one of the classes is MEMORY, the result is MEMORY. */
  85. if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
  86. return X86_64_MEMORY_CLASS;
  87. /* Rule #4: If one of the classes is INTEGER, the result is INTEGER. */
  88. if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
  89. || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
  90. return X86_64_INTEGERSI_CLASS;
  91. if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
  92. || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
  93. return X86_64_INTEGER_CLASS;
  94. /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
  95. MEMORY is used. */
  96. if (class1 == X86_64_X87_CLASS
  97. || class1 == X86_64_X87UP_CLASS
  98. || class1 == X86_64_COMPLEX_X87_CLASS
  99. || class2 == X86_64_X87_CLASS
  100. || class2 == X86_64_X87UP_CLASS
  101. || class2 == X86_64_COMPLEX_X87_CLASS)
  102. return X86_64_MEMORY_CLASS;
  103. /* Rule #6: Otherwise class SSE is used. */
  104. return X86_64_SSE_CLASS;
  105. }
  106. /* Classify the argument of type TYPE and mode MODE.
  107. CLASSES will be filled by the register class used to pass each word
  108. of the operand. The number of words is returned. In case the parameter
  109. should be passed in memory, 0 is returned. As a special case for zero
  110. sized containers, classes[0] will be NO_CLASS and 1 is returned.
  111. See the x86-64 PS ABI for details. */
  112. static int
  113. classify_argument(
  114. ffi_type* type,
  115. enum x86_64_reg_class classes[],
  116. size_t byte_offset)
  117. {
  118. switch (type->type)
  119. {
  120. case FFI_TYPE_UINT8:
  121. case FFI_TYPE_SINT8:
  122. case FFI_TYPE_UINT16:
  123. case FFI_TYPE_SINT16:
  124. case FFI_TYPE_UINT32:
  125. case FFI_TYPE_SINT32:
  126. case FFI_TYPE_UINT64:
  127. case FFI_TYPE_SINT64:
  128. case FFI_TYPE_POINTER:
  129. if (byte_offset + type->size <= 4)
  130. classes[0] = X86_64_INTEGERSI_CLASS;
  131. else
  132. classes[0] = X86_64_INTEGER_CLASS;
  133. return 1;
  134. case FFI_TYPE_FLOAT:
  135. if (byte_offset == 0)
  136. classes[0] = X86_64_SSESF_CLASS;
  137. else
  138. classes[0] = X86_64_SSE_CLASS;
  139. return 1;
  140. case FFI_TYPE_DOUBLE:
  141. classes[0] = X86_64_SSEDF_CLASS;
  142. return 1;
  143. case FFI_TYPE_LONGDOUBLE:
  144. classes[0] = X86_64_X87_CLASS;
  145. classes[1] = X86_64_X87UP_CLASS;
  146. return 2;
  147. case FFI_TYPE_STRUCT:
  148. {
  149. ffi_type** ptr;
  150. int i;
  151. enum x86_64_reg_class subclasses[MAX_CLASSES];
  152. const int UNITS_PER_WORD = 8;
  153. int words =
  154. (type->size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
  155. /* If the struct is larger than 16 bytes, pass it on the stack. */
  156. if (type->size > 16)
  157. return 0;
  158. for (i = 0; i < words; i++)
  159. classes[i] = X86_64_NO_CLASS;
  160. /* Merge the fields of structure. */
  161. for (ptr = type->elements; *ptr != NULL; ptr++)
  162. {
  163. byte_offset = ALIGN(byte_offset, (*ptr)->alignment);
  164. int num = classify_argument(*ptr, subclasses, byte_offset % 8);
  165. if (num == 0)
  166. return 0;
  167. int pos = byte_offset / 8;
  168. for (i = 0; i < num; i++)
  169. {
  170. classes[i + pos] =
  171. merge_classes(subclasses[i], classes[i + pos]);
  172. }
  173. byte_offset += (*ptr)->size;
  174. }
  175. /* Final merger cleanup. */
  176. for (i = 0; i < words; i++)
  177. {
  178. /* If one class is MEMORY, everything should be passed in
  179. memory. */
  180. if (classes[i] == X86_64_MEMORY_CLASS)
  181. return 0;
  182. /* The X86_64_SSEUP_CLASS should be always preceded by
  183. X86_64_SSE_CLASS. */
  184. if (classes[i] == X86_64_SSEUP_CLASS
  185. && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
  186. classes[i] = X86_64_SSE_CLASS;
  187. /* X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS. */
  188. if (classes[i] == X86_64_X87UP_CLASS
  189. && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
  190. classes[i] = X86_64_SSE_CLASS;
  191. }
  192. return words;
  193. }
  194. default:
  195. FFI_ASSERT(0);
  196. }
  197. return 0; /* Never reached. */
  198. }
  199. /* Examine the argument and return set number of register required in each
  200. class. Return zero if parameter should be passed in memory, otherwise
  201. the number of registers. */
  202. static int
  203. examine_argument(
  204. ffi_type* type,
  205. enum x86_64_reg_class classes[MAX_CLASSES],
  206. _Bool in_return,
  207. int* pngpr,
  208. int* pnsse)
  209. {
  210. int n = classify_argument(type, classes, 0);
  211. int ngpr = 0;
  212. int nsse = 0;
  213. int i;
  214. if (n == 0)
  215. return 0;
  216. for (i = 0; i < n; ++i)
  217. {
  218. switch (classes[i])
  219. {
  220. case X86_64_INTEGER_CLASS:
  221. case X86_64_INTEGERSI_CLASS:
  222. ngpr++;
  223. break;
  224. case X86_64_SSE_CLASS:
  225. case X86_64_SSESF_CLASS:
  226. case X86_64_SSEDF_CLASS:
  227. nsse++;
  228. break;
  229. case X86_64_NO_CLASS:
  230. case X86_64_SSEUP_CLASS:
  231. break;
  232. case X86_64_X87_CLASS:
  233. case X86_64_X87UP_CLASS:
  234. case X86_64_COMPLEX_X87_CLASS:
  235. return in_return != 0;
  236. default:
  237. abort();
  238. }
  239. }
  240. *pngpr = ngpr;
  241. *pnsse = nsse;
  242. return n;
  243. }
  244. /* Perform machine dependent cif processing. */
  245. ffi_status
  246. ffi_prep_cif_machdep(
  247. ffi_cif* cif)
  248. {
  249. int gprcount = 0;
  250. int ssecount = 0;
  251. int flags = cif->rtype->type;
  252. int i, avn, n, ngpr, nsse;
  253. enum x86_64_reg_class classes[MAX_CLASSES];
  254. size_t bytes;
  255. if (flags != FFI_TYPE_VOID)
  256. {
  257. n = examine_argument (cif->rtype, classes, 1, &ngpr, &nsse);
  258. if (n == 0)
  259. {
  260. /* The return value is passed in memory. A pointer to that
  261. memory is the first argument. Allocate a register for it. */
  262. gprcount++;
  263. /* We don't have to do anything in asm for the return. */
  264. flags = FFI_TYPE_VOID;
  265. }
  266. else if (flags == FFI_TYPE_STRUCT)
  267. {
  268. /* Mark which registers the result appears in. */
  269. _Bool sse0 = SSE_CLASS_P(classes[0]);
  270. _Bool sse1 = n == 2 && SSE_CLASS_P(classes[1]);
  271. if (sse0 && !sse1)
  272. flags |= 1 << 8;
  273. else if (!sse0 && sse1)
  274. flags |= 1 << 9;
  275. else if (sse0 && sse1)
  276. flags |= 1 << 10;
  277. /* Mark the true size of the structure. */
  278. flags |= cif->rtype->size << 12;
  279. }
  280. }
  281. /* Go over all arguments and determine the way they should be passed.
  282. If it's in a register and there is space for it, let that be so. If
  283. not, add it's size to the stack byte count. */
  284. for (bytes = 0, i = 0, avn = cif->nargs; i < avn; i++)
  285. {
  286. if (examine_argument(cif->arg_types[i], classes, 0, &ngpr, &nsse) == 0
  287. || gprcount + ngpr > MAX_GPR_REGS
  288. || ssecount + nsse > MAX_SSE_REGS)
  289. {
  290. long align = cif->arg_types[i]->alignment;
  291. if (align < 8)
  292. align = 8;
  293. bytes = ALIGN(bytes, align);
  294. bytes += cif->arg_types[i]->size;
  295. }
  296. else
  297. {
  298. gprcount += ngpr;
  299. ssecount += nsse;
  300. }
  301. }
  302. if (ssecount)
  303. flags |= 1 << 11;
  304. cif->flags = flags;
  305. cif->bytes = bytes;
  306. return FFI_OK;
  307. }
  308. void
  309. ffi_call(
  310. ffi_cif* cif,
  311. void (*fn)(),
  312. void* rvalue,
  313. void** avalue)
  314. {
  315. enum x86_64_reg_class classes[MAX_CLASSES];
  316. char* stack;
  317. char* argp;
  318. ffi_type** arg_types;
  319. int gprcount, ssecount, ngpr, nsse, i, avn;
  320. _Bool ret_in_memory;
  321. RegisterArgs* reg_args;
  322. /* Can't call 32-bit mode from 64-bit mode. */
  323. FFI_ASSERT(cif->abi == FFI_UNIX64);
  324. /* If the return value is a struct and we don't have a return value
  325. address then we need to make one. Note the setting of flags to
  326. VOID above in ffi_prep_cif_machdep. */
  327. ret_in_memory = (cif->rtype->type == FFI_TYPE_STRUCT
  328. && (cif->flags & 0xff) == FFI_TYPE_VOID);
  329. if (rvalue == NULL && ret_in_memory)
  330. rvalue = alloca (cif->rtype->size);
  331. /* Allocate the space for the arguments, plus 4 words of temp space. */
  332. stack = alloca(sizeof(RegisterArgs) + cif->bytes + 4 * 8);
  333. reg_args = (RegisterArgs*)stack;
  334. argp = stack + sizeof(RegisterArgs);
  335. gprcount = ssecount = 0;
  336. /* If the return value is passed in memory, add the pointer as the
  337. first integer argument. */
  338. if (ret_in_memory)
  339. reg_args->gpr[gprcount++] = (long) rvalue;
  340. avn = cif->nargs;
  341. arg_types = cif->arg_types;
  342. for (i = 0; i < avn; ++i)
  343. {
  344. size_t size = arg_types[i]->size;
  345. int n;
  346. n = examine_argument (arg_types[i], classes, 0, &ngpr, &nsse);
  347. if (n == 0
  348. || gprcount + ngpr > MAX_GPR_REGS
  349. || ssecount + nsse > MAX_SSE_REGS)
  350. {
  351. long align = arg_types[i]->alignment;
  352. /* Stack arguments are *always* at least 8 byte aligned. */
  353. if (align < 8)
  354. align = 8;
  355. /* Pass this argument in memory. */
  356. argp = (void *) ALIGN (argp, align);
  357. memcpy (argp, avalue[i], size);
  358. argp += size;
  359. }
  360. else
  361. { /* The argument is passed entirely in registers. */
  362. char *a = (char *) avalue[i];
  363. int j;
  364. for (j = 0; j < n; j++, a += 8, size -= 8)
  365. {
  366. switch (classes[j])
  367. {
  368. case X86_64_INTEGER_CLASS:
  369. case X86_64_INTEGERSI_CLASS:
  370. reg_args->gpr[gprcount] = 0;
  371. memcpy (&reg_args->gpr[gprcount], a, size < 8 ? size : 8);
  372. gprcount++;
  373. break;
  374. case X86_64_SSE_CLASS:
  375. case X86_64_SSEDF_CLASS:
  376. reg_args->sse[ssecount++] = *(UINT64 *) a;
  377. break;
  378. case X86_64_SSESF_CLASS:
  379. reg_args->sse[ssecount++] = *(UINT32 *) a;
  380. break;
  381. default:
  382. abort();
  383. }
  384. }
  385. }
  386. }
  387. ffi_call_unix64 (stack, cif->bytes + sizeof(RegisterArgs),
  388. cif->flags, rvalue, fn, ssecount);
  389. }
  390. extern void ffi_closure_unix64(void);
  391. ffi_status
  392. ffi_prep_closure(
  393. ffi_closure* closure,
  394. ffi_cif* cif,
  395. void (*fun)(ffi_cif*, void*, void**, void*),
  396. void* user_data)
  397. {
  398. if (cif->abi != FFI_UNIX64)
  399. return FFI_BAD_ABI;
  400. volatile unsigned short* tramp =
  401. (volatile unsigned short*)&closure->tramp[0];
  402. tramp[0] = 0xbb49; /* mov <code>, %r11 */
  403. *(void* volatile*)&tramp[1] = ffi_closure_unix64;
  404. tramp[5] = 0xba49; /* mov <data>, %r10 */
  405. *(void* volatile*)&tramp[6] = closure;
  406. /* Set the carry bit if the function uses any sse registers.
  407. This is clc or stc, together with the first byte of the jmp. */
  408. tramp[10] = cif->flags & (1 << 11) ? 0x49f9 : 0x49f8;
  409. tramp[11] = 0xe3ff; /* jmp *%r11 */
  410. closure->cif = cif;
  411. closure->fun = fun;
  412. closure->user_data = user_data;
  413. return FFI_OK;
  414. }
  415. int
  416. ffi_closure_unix64_inner(
  417. ffi_closure* closure,
  418. void* rvalue,
  419. RegisterArgs* reg_args,
  420. char* argp)
  421. {
  422. ffi_cif* cif = closure->cif;
  423. void** avalue = alloca(cif->nargs * sizeof(void *));
  424. ffi_type** arg_types;
  425. long i, avn;
  426. int gprcount = 0;
  427. int ssecount = 0;
  428. int ngpr, nsse;
  429. int ret;
  430. ret = cif->rtype->type;
  431. if (ret != FFI_TYPE_VOID)
  432. {
  433. enum x86_64_reg_class classes[MAX_CLASSES];
  434. int n = examine_argument (cif->rtype, classes, 1, &ngpr, &nsse);
  435. if (n == 0)
  436. {
  437. /* The return value goes in memory. Arrange for the closure
  438. return value to go directly back to the original caller. */
  439. rvalue = (void *) reg_args->gpr[gprcount++];
  440. /* We don't have to do anything in asm for the return. */
  441. ret = FFI_TYPE_VOID;
  442. }
  443. else if (ret == FFI_TYPE_STRUCT && n == 2)
  444. {
  445. /* Mark which register the second word of the structure goes in. */
  446. _Bool sse0 = SSE_CLASS_P (classes[0]);
  447. _Bool sse1 = SSE_CLASS_P (classes[1]);
  448. if (!sse0 && sse1)
  449. ret |= 1 << 8;
  450. else if (sse0 && !sse1)
  451. ret |= 1 << 9;
  452. }
  453. }
  454. avn = cif->nargs;
  455. arg_types = cif->arg_types;
  456. for (i = 0; i < avn; ++i)
  457. {
  458. enum x86_64_reg_class classes[MAX_CLASSES];
  459. int n;
  460. n = examine_argument (arg_types[i], classes, 0, &ngpr, &nsse);
  461. if (n == 0
  462. || gprcount + ngpr > MAX_GPR_REGS
  463. || ssecount + nsse > MAX_SSE_REGS)
  464. {
  465. long align = arg_types[i]->alignment;
  466. /* Stack arguments are *always* at least 8 byte aligned. */
  467. if (align < 8)
  468. align = 8;
  469. /* Pass this argument in memory. */
  470. argp = (void *) ALIGN (argp, align);
  471. avalue[i] = argp;
  472. argp += arg_types[i]->size;
  473. }
  474. #if !defined(X86_DARWIN)
  475. /* If the argument is in a single register, or two consecutive
  476. registers, then we can use that address directly. */
  477. else if (n == 1 || (n == 2 &&
  478. SSE_CLASS_P (classes[0]) == SSE_CLASS_P (classes[1])))
  479. {
  480. // The argument is in a single register.
  481. if (SSE_CLASS_P (classes[0]))
  482. {
  483. avalue[i] = &reg_args->sse[ssecount];
  484. ssecount += n;
  485. }
  486. else
  487. {
  488. avalue[i] = &reg_args->gpr[gprcount];
  489. gprcount += n;
  490. }
  491. }
  492. #endif
  493. /* Otherwise, allocate space to make them consecutive. */
  494. else
  495. {
  496. char *a = alloca (16);
  497. int j;
  498. avalue[i] = a;
  499. for (j = 0; j < n; j++, a += 8)
  500. {
  501. if (SSE_CLASS_P (classes[j]))
  502. memcpy (a, &reg_args->sse[ssecount++], 8);
  503. else
  504. memcpy (a, &reg_args->gpr[gprcount++], 8);
  505. }
  506. }
  507. }
  508. /* Invoke the closure. */
  509. closure->fun (cif, rvalue, avalue, closure->user_data);
  510. /* Tell assembly how to perform return type promotions. */
  511. return ret;
  512. }
  513. #endif /* __x86_64__ */