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

http://unladen-swallow.googlecode.com/ · C · 709 lines · 477 code · 92 blank · 140 comment · 59 complexity · 7584ecb991cb652c6935e9f0069dfda3 MD5 · raw file

  1. /* -----------------------------------------------------------------------
  2. ffi.c - (c) 2003-2004 Randolph Chung <tausq@debian.org>
  3. (c) 2008 Red Hat, Inc.
  4. HPPA Foreign Function Interface
  5. HP-UX PA ABI support (c) 2006 Free Software Foundation, Inc.
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. ``Software''), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be included
  14. in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. DEALINGS IN THE SOFTWARE.
  23. ----------------------------------------------------------------------- */
  24. #include <ffi.h>
  25. #include <ffi_common.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #define ROUND_UP(v, a) (((size_t)(v) + (a) - 1) & ~((a) - 1))
  29. #define MIN_STACK_SIZE 64
  30. #define FIRST_ARG_SLOT 9
  31. #define DEBUG_LEVEL 0
  32. #define fldw(addr, fpreg) \
  33. __asm__ volatile ("fldw 0(%0), %%" #fpreg "L" : : "r"(addr) : #fpreg)
  34. #define fstw(fpreg, addr) \
  35. __asm__ volatile ("fstw %%" #fpreg "L, 0(%0)" : : "r"(addr))
  36. #define fldd(addr, fpreg) \
  37. __asm__ volatile ("fldd 0(%0), %%" #fpreg : : "r"(addr) : #fpreg)
  38. #define fstd(fpreg, addr) \
  39. __asm__ volatile ("fstd %%" #fpreg "L, 0(%0)" : : "r"(addr))
  40. #define debug(lvl, x...) do { if (lvl <= DEBUG_LEVEL) { printf(x); } } while (0)
  41. static inline int ffi_struct_type(ffi_type *t)
  42. {
  43. size_t sz = t->size;
  44. /* Small structure results are passed in registers,
  45. larger ones are passed by pointer. Note that
  46. small structures of size 2, 4 and 8 differ from
  47. the corresponding integer types in that they have
  48. different alignment requirements. */
  49. if (sz <= 1)
  50. return FFI_TYPE_UINT8;
  51. else if (sz == 2)
  52. return FFI_TYPE_SMALL_STRUCT2;
  53. else if (sz == 3)
  54. return FFI_TYPE_SMALL_STRUCT3;
  55. else if (sz == 4)
  56. return FFI_TYPE_SMALL_STRUCT4;
  57. else if (sz == 5)
  58. return FFI_TYPE_SMALL_STRUCT5;
  59. else if (sz == 6)
  60. return FFI_TYPE_SMALL_STRUCT6;
  61. else if (sz == 7)
  62. return FFI_TYPE_SMALL_STRUCT7;
  63. else if (sz <= 8)
  64. return FFI_TYPE_SMALL_STRUCT8;
  65. else
  66. return FFI_TYPE_STRUCT; /* else, we pass it by pointer. */
  67. }
  68. /* PA has a downward growing stack, which looks like this:
  69. Offset
  70. [ Variable args ]
  71. SP = (4*(n+9)) arg word N
  72. ...
  73. SP-52 arg word 4
  74. [ Fixed args ]
  75. SP-48 arg word 3
  76. SP-44 arg word 2
  77. SP-40 arg word 1
  78. SP-36 arg word 0
  79. [ Frame marker ]
  80. ...
  81. SP-20 RP
  82. SP-4 previous SP
  83. The first four argument words on the stack are reserved for use by
  84. the callee. Instead, the general and floating registers replace
  85. the first four argument slots. Non FP arguments are passed solely
  86. in the general registers. FP arguments are passed in both general
  87. and floating registers when using libffi.
  88. Non-FP 32-bit args are passed in gr26, gr25, gr24 and gr23.
  89. Non-FP 64-bit args are passed in register pairs, starting
  90. on an odd numbered register (i.e. r25+r26 and r23+r24).
  91. FP 32-bit arguments are passed in fr4L, fr5L, fr6L and fr7L.
  92. FP 64-bit arguments are passed in fr5 and fr7.
  93. The registers are allocated in the same manner as stack slots.
  94. This allows the callee to save its arguments on the stack if
  95. necessary:
  96. arg word 3 -> gr23 or fr7L
  97. arg word 2 -> gr24 or fr6L or fr7R
  98. arg word 1 -> gr25 or fr5L
  99. arg word 0 -> gr26 or fr4L or fr5R
  100. Note that fr4R and fr6R are never used for arguments (i.e.,
  101. doubles are not passed in fr4 or fr6).
  102. The rest of the arguments are passed on the stack starting at SP-52,
  103. but 64-bit arguments need to be aligned to an 8-byte boundary
  104. This means we can have holes either in the register allocation,
  105. or in the stack. */
  106. /* ffi_prep_args is called by the assembly routine once stack space
  107. has been allocated for the function's arguments
  108. The following code will put everything into the stack frame
  109. (which was allocated by the asm routine), and on return
  110. the asm routine will load the arguments that should be
  111. passed by register into the appropriate registers
  112. NOTE: We load floating point args in this function... that means we
  113. assume gcc will not mess with fp regs in here. */
  114. void ffi_prep_args_pa32(UINT32 *stack, extended_cif *ecif, unsigned bytes)
  115. {
  116. register unsigned int i;
  117. register ffi_type **p_arg;
  118. register void **p_argv;
  119. unsigned int slot = FIRST_ARG_SLOT;
  120. char *dest_cpy;
  121. size_t len;
  122. debug(1, "%s: stack = %p, ecif = %p, bytes = %u\n", __FUNCTION__, stack,
  123. ecif, bytes);
  124. p_arg = ecif->cif->arg_types;
  125. p_argv = ecif->avalue;
  126. for (i = 0; i < ecif->cif->nargs; i++)
  127. {
  128. int type = (*p_arg)->type;
  129. switch (type)
  130. {
  131. case FFI_TYPE_SINT8:
  132. *(SINT32 *)(stack - slot) = *(SINT8 *)(*p_argv);
  133. break;
  134. case FFI_TYPE_UINT8:
  135. *(UINT32 *)(stack - slot) = *(UINT8 *)(*p_argv);
  136. break;
  137. case FFI_TYPE_SINT16:
  138. *(SINT32 *)(stack - slot) = *(SINT16 *)(*p_argv);
  139. break;
  140. case FFI_TYPE_UINT16:
  141. *(UINT32 *)(stack - slot) = *(UINT16 *)(*p_argv);
  142. break;
  143. case FFI_TYPE_UINT32:
  144. case FFI_TYPE_SINT32:
  145. case FFI_TYPE_POINTER:
  146. debug(3, "Storing UINT32 %u in slot %u\n", *(UINT32 *)(*p_argv),
  147. slot);
  148. *(UINT32 *)(stack - slot) = *(UINT32 *)(*p_argv);
  149. break;
  150. case FFI_TYPE_UINT64:
  151. case FFI_TYPE_SINT64:
  152. /* Align slot for 64-bit type. */
  153. slot += (slot & 1) ? 1 : 2;
  154. *(UINT64 *)(stack - slot) = *(UINT64 *)(*p_argv);
  155. break;
  156. case FFI_TYPE_FLOAT:
  157. /* First 4 args go in fr4L - fr7L. */
  158. debug(3, "Storing UINT32(float) in slot %u\n", slot);
  159. *(UINT32 *)(stack - slot) = *(UINT32 *)(*p_argv);
  160. switch (slot - FIRST_ARG_SLOT)
  161. {
  162. /* First 4 args go in fr4L - fr7L. */
  163. case 0: fldw(stack - slot, fr4); break;
  164. case 1: fldw(stack - slot, fr5); break;
  165. case 2: fldw(stack - slot, fr6); break;
  166. case 3: fldw(stack - slot, fr7); break;
  167. }
  168. break;
  169. case FFI_TYPE_DOUBLE:
  170. /* Align slot for 64-bit type. */
  171. slot += (slot & 1) ? 1 : 2;
  172. debug(3, "Storing UINT64(double) at slot %u\n", slot);
  173. *(UINT64 *)(stack - slot) = *(UINT64 *)(*p_argv);
  174. switch (slot - FIRST_ARG_SLOT)
  175. {
  176. /* First 2 args go in fr5, fr7. */
  177. case 1: fldd(stack - slot, fr5); break;
  178. case 3: fldd(stack - slot, fr7); break;
  179. }
  180. break;
  181. #ifdef PA_HPUX
  182. case FFI_TYPE_LONGDOUBLE:
  183. /* Long doubles are passed in the same manner as structures
  184. larger than 8 bytes. */
  185. *(UINT32 *)(stack - slot) = (UINT32)(*p_argv);
  186. break;
  187. #endif
  188. case FFI_TYPE_STRUCT:
  189. /* Structs smaller or equal than 4 bytes are passed in one
  190. register. Structs smaller or equal 8 bytes are passed in two
  191. registers. Larger structures are passed by pointer. */
  192. len = (*p_arg)->size;
  193. if (len <= 4)
  194. {
  195. dest_cpy = (char *)(stack - slot) + 4 - len;
  196. memcpy(dest_cpy, (char *)*p_argv, len);
  197. }
  198. else if (len <= 8)
  199. {
  200. slot += (slot & 1) ? 1 : 2;
  201. dest_cpy = (char *)(stack - slot) + 8 - len;
  202. memcpy(dest_cpy, (char *)*p_argv, len);
  203. }
  204. else
  205. *(UINT32 *)(stack - slot) = (UINT32)(*p_argv);
  206. break;
  207. default:
  208. FFI_ASSERT(0);
  209. }
  210. slot++;
  211. p_arg++;
  212. p_argv++;
  213. }
  214. /* Make sure we didn't mess up and scribble on the stack. */
  215. {
  216. unsigned int n;
  217. debug(5, "Stack setup:\n");
  218. for (n = 0; n < (bytes + 3) / 4; n++)
  219. {
  220. if ((n%4) == 0) { debug(5, "\n%08x: ", (unsigned int)(stack - n)); }
  221. debug(5, "%08x ", *(stack - n));
  222. }
  223. debug(5, "\n");
  224. }
  225. FFI_ASSERT(slot * 4 <= bytes);
  226. return;
  227. }
  228. static void ffi_size_stack_pa32(ffi_cif *cif)
  229. {
  230. ffi_type **ptr;
  231. int i;
  232. int z = 0; /* # stack slots */
  233. for (ptr = cif->arg_types, i = 0; i < cif->nargs; ptr++, i++)
  234. {
  235. int type = (*ptr)->type;
  236. switch (type)
  237. {
  238. case FFI_TYPE_DOUBLE:
  239. case FFI_TYPE_UINT64:
  240. case FFI_TYPE_SINT64:
  241. z += 2 + (z & 1); /* must start on even regs, so we may waste one */
  242. break;
  243. #ifdef PA_HPUX
  244. case FFI_TYPE_LONGDOUBLE:
  245. #endif
  246. case FFI_TYPE_STRUCT:
  247. z += 1; /* pass by ptr, callee will copy */
  248. break;
  249. default: /* <= 32-bit values */
  250. z++;
  251. }
  252. }
  253. /* We can fit up to 6 args in the default 64-byte stack frame,
  254. if we need more, we need more stack. */
  255. if (z <= 6)
  256. cif->bytes = MIN_STACK_SIZE; /* min stack size */
  257. else
  258. cif->bytes = 64 + ROUND_UP((z - 6) * sizeof(UINT32), MIN_STACK_SIZE);
  259. debug(3, "Calculated stack size is %u bytes\n", cif->bytes);
  260. }
  261. /* Perform machine dependent cif processing. */
  262. ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
  263. {
  264. /* Set the return type flag */
  265. switch (cif->rtype->type)
  266. {
  267. case FFI_TYPE_VOID:
  268. case FFI_TYPE_FLOAT:
  269. case FFI_TYPE_DOUBLE:
  270. cif->flags = (unsigned) cif->rtype->type;
  271. break;
  272. #ifdef PA_HPUX
  273. case FFI_TYPE_LONGDOUBLE:
  274. /* Long doubles are treated like a structure. */
  275. cif->flags = FFI_TYPE_STRUCT;
  276. break;
  277. #endif
  278. case FFI_TYPE_STRUCT:
  279. /* For the return type we have to check the size of the structures.
  280. If the size is smaller or equal 4 bytes, the result is given back
  281. in one register. If the size is smaller or equal 8 bytes than we
  282. return the result in two registers. But if the size is bigger than
  283. 8 bytes, we work with pointers. */
  284. cif->flags = ffi_struct_type(cif->rtype);
  285. break;
  286. case FFI_TYPE_UINT64:
  287. case FFI_TYPE_SINT64:
  288. cif->flags = FFI_TYPE_UINT64;
  289. break;
  290. default:
  291. cif->flags = FFI_TYPE_INT;
  292. break;
  293. }
  294. /* Lucky us, because of the unique PA ABI we get to do our
  295. own stack sizing. */
  296. switch (cif->abi)
  297. {
  298. case FFI_PA32:
  299. ffi_size_stack_pa32(cif);
  300. break;
  301. default:
  302. FFI_ASSERT(0);
  303. break;
  304. }
  305. return FFI_OK;
  306. }
  307. extern void ffi_call_pa32(void (*)(UINT32 *, extended_cif *, unsigned),
  308. extended_cif *, unsigned, unsigned, unsigned *,
  309. void (*fn)(void));
  310. void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
  311. {
  312. extended_cif ecif;
  313. ecif.cif = cif;
  314. ecif.avalue = avalue;
  315. /* If the return value is a struct and we don't have a return
  316. value address then we need to make one. */
  317. if (rvalue == NULL
  318. #ifdef PA_HPUX
  319. && (cif->rtype->type == FFI_TYPE_STRUCT
  320. || cif->rtype->type == FFI_TYPE_LONGDOUBLE))
  321. #else
  322. && cif->rtype->type == FFI_TYPE_STRUCT)
  323. #endif
  324. {
  325. ecif.rvalue = alloca(cif->rtype->size);
  326. }
  327. else
  328. ecif.rvalue = rvalue;
  329. switch (cif->abi)
  330. {
  331. case FFI_PA32:
  332. debug(3, "Calling ffi_call_pa32: ecif=%p, bytes=%u, flags=%u, rvalue=%p, fn=%p\n", &ecif, cif->bytes, cif->flags, ecif.rvalue, (void *)fn);
  333. ffi_call_pa32(ffi_prep_args_pa32, &ecif, cif->bytes,
  334. cif->flags, ecif.rvalue, fn);
  335. break;
  336. default:
  337. FFI_ASSERT(0);
  338. break;
  339. }
  340. }
  341. #if FFI_CLOSURES
  342. /* This is more-or-less an inverse of ffi_call -- we have arguments on
  343. the stack, and we need to fill them into a cif structure and invoke
  344. the user function. This really ought to be in asm to make sure
  345. the compiler doesn't do things we don't expect. */
  346. ffi_status ffi_closure_inner_pa32(ffi_closure *closure, UINT32 *stack)
  347. {
  348. ffi_cif *cif;
  349. void **avalue;
  350. void *rvalue;
  351. UINT32 ret[2]; /* function can return up to 64-bits in registers */
  352. ffi_type **p_arg;
  353. char *tmp;
  354. int i, avn;
  355. unsigned int slot = FIRST_ARG_SLOT;
  356. register UINT32 r28 asm("r28");
  357. cif = closure->cif;
  358. /* If returning via structure, callee will write to our pointer. */
  359. if (cif->flags == FFI_TYPE_STRUCT)
  360. rvalue = (void *)r28;
  361. else
  362. rvalue = &ret[0];
  363. avalue = (void **)alloca(cif->nargs * FFI_SIZEOF_ARG);
  364. avn = cif->nargs;
  365. p_arg = cif->arg_types;
  366. for (i = 0; i < avn; i++)
  367. {
  368. int type = (*p_arg)->type;
  369. switch (type)
  370. {
  371. case FFI_TYPE_SINT8:
  372. case FFI_TYPE_UINT8:
  373. case FFI_TYPE_SINT16:
  374. case FFI_TYPE_UINT16:
  375. case FFI_TYPE_SINT32:
  376. case FFI_TYPE_UINT32:
  377. case FFI_TYPE_POINTER:
  378. avalue[i] = (char *)(stack - slot) + sizeof(UINT32) - (*p_arg)->size;
  379. break;
  380. case FFI_TYPE_SINT64:
  381. case FFI_TYPE_UINT64:
  382. slot += (slot & 1) ? 1 : 2;
  383. avalue[i] = (void *)(stack - slot);
  384. break;
  385. case FFI_TYPE_FLOAT:
  386. #ifdef PA_LINUX
  387. /* The closure call is indirect. In Linux, floating point
  388. arguments in indirect calls with a prototype are passed
  389. in the floating point registers instead of the general
  390. registers. So, we need to replace what was previously
  391. stored in the current slot with the value in the
  392. corresponding floating point register. */
  393. switch (slot - FIRST_ARG_SLOT)
  394. {
  395. case 0: fstw(fr4, (void *)(stack - slot)); break;
  396. case 1: fstw(fr5, (void *)(stack - slot)); break;
  397. case 2: fstw(fr6, (void *)(stack - slot)); break;
  398. case 3: fstw(fr7, (void *)(stack - slot)); break;
  399. }
  400. #endif
  401. avalue[i] = (void *)(stack - slot);
  402. break;
  403. case FFI_TYPE_DOUBLE:
  404. slot += (slot & 1) ? 1 : 2;
  405. #ifdef PA_LINUX
  406. /* See previous comment for FFI_TYPE_FLOAT. */
  407. switch (slot - FIRST_ARG_SLOT)
  408. {
  409. case 1: fstd(fr5, (void *)(stack - slot)); break;
  410. case 3: fstd(fr7, (void *)(stack - slot)); break;
  411. }
  412. #endif
  413. avalue[i] = (void *)(stack - slot);
  414. break;
  415. case FFI_TYPE_STRUCT:
  416. /* Structs smaller or equal than 4 bytes are passed in one
  417. register. Structs smaller or equal 8 bytes are passed in two
  418. registers. Larger structures are passed by pointer. */
  419. if((*p_arg)->size <= 4)
  420. {
  421. avalue[i] = (void *)(stack - slot) + sizeof(UINT32) -
  422. (*p_arg)->size;
  423. }
  424. else if ((*p_arg)->size <= 8)
  425. {
  426. slot += (slot & 1) ? 1 : 2;
  427. avalue[i] = (void *)(stack - slot) + sizeof(UINT64) -
  428. (*p_arg)->size;
  429. }
  430. else
  431. avalue[i] = (void *) *(stack - slot);
  432. break;
  433. default:
  434. FFI_ASSERT(0);
  435. }
  436. slot++;
  437. p_arg++;
  438. }
  439. /* Invoke the closure. */
  440. (closure->fun) (cif, rvalue, avalue, closure->user_data);
  441. debug(3, "after calling function, ret[0] = %08x, ret[1] = %08x\n", ret[0],
  442. ret[1]);
  443. /* Store the result using the lower 2 bytes of the flags. */
  444. switch (cif->flags)
  445. {
  446. case FFI_TYPE_UINT8:
  447. *(stack - FIRST_ARG_SLOT) = (UINT8)(ret[0] >> 24);
  448. break;
  449. case FFI_TYPE_SINT8:
  450. *(stack - FIRST_ARG_SLOT) = (SINT8)(ret[0] >> 24);
  451. break;
  452. case FFI_TYPE_UINT16:
  453. *(stack - FIRST_ARG_SLOT) = (UINT16)(ret[0] >> 16);
  454. break;
  455. case FFI_TYPE_SINT16:
  456. *(stack - FIRST_ARG_SLOT) = (SINT16)(ret[0] >> 16);
  457. break;
  458. case FFI_TYPE_INT:
  459. case FFI_TYPE_SINT32:
  460. case FFI_TYPE_UINT32:
  461. *(stack - FIRST_ARG_SLOT) = ret[0];
  462. break;
  463. case FFI_TYPE_SINT64:
  464. case FFI_TYPE_UINT64:
  465. *(stack - FIRST_ARG_SLOT) = ret[0];
  466. *(stack - FIRST_ARG_SLOT - 1) = ret[1];
  467. break;
  468. case FFI_TYPE_DOUBLE:
  469. fldd(rvalue, fr4);
  470. break;
  471. case FFI_TYPE_FLOAT:
  472. fldw(rvalue, fr4);
  473. break;
  474. case FFI_TYPE_STRUCT:
  475. /* Don't need a return value, done by caller. */
  476. break;
  477. case FFI_TYPE_SMALL_STRUCT2:
  478. case FFI_TYPE_SMALL_STRUCT3:
  479. case FFI_TYPE_SMALL_STRUCT4:
  480. tmp = (void*)(stack - FIRST_ARG_SLOT);
  481. tmp += 4 - cif->rtype->size;
  482. memcpy((void*)tmp, &ret[0], cif->rtype->size);
  483. break;
  484. case FFI_TYPE_SMALL_STRUCT5:
  485. case FFI_TYPE_SMALL_STRUCT6:
  486. case FFI_TYPE_SMALL_STRUCT7:
  487. case FFI_TYPE_SMALL_STRUCT8:
  488. {
  489. unsigned int ret2[2];
  490. int off;
  491. /* Right justify ret[0] and ret[1] */
  492. switch (cif->flags)
  493. {
  494. case FFI_TYPE_SMALL_STRUCT5: off = 3; break;
  495. case FFI_TYPE_SMALL_STRUCT6: off = 2; break;
  496. case FFI_TYPE_SMALL_STRUCT7: off = 1; break;
  497. default: off = 0; break;
  498. }
  499. memset (ret2, 0, sizeof (ret2));
  500. memcpy ((char *)ret2 + off, ret, 8 - off);
  501. *(stack - FIRST_ARG_SLOT) = ret2[0];
  502. *(stack - FIRST_ARG_SLOT - 1) = ret2[1];
  503. }
  504. break;
  505. case FFI_TYPE_POINTER:
  506. case FFI_TYPE_VOID:
  507. break;
  508. default:
  509. debug(0, "assert with cif->flags: %d\n",cif->flags);
  510. FFI_ASSERT(0);
  511. break;
  512. }
  513. return FFI_OK;
  514. }
  515. /* Fill in a closure to refer to the specified fun and user_data.
  516. cif specifies the argument and result types for fun.
  517. The cif must already be prep'ed. */
  518. extern void ffi_closure_pa32(void);
  519. ffi_status
  520. ffi_prep_closure_loc (ffi_closure* closure,
  521. ffi_cif* cif,
  522. void (*fun)(ffi_cif*,void*,void**,void*),
  523. void *user_data,
  524. void *codeloc)
  525. {
  526. UINT32 *tramp = (UINT32 *)(closure->tramp);
  527. #ifdef PA_HPUX
  528. UINT32 *tmp;
  529. #endif
  530. FFI_ASSERT (cif->abi == FFI_PA32);
  531. /* Make a small trampoline that will branch to our
  532. handler function. Use PC-relative addressing. */
  533. #ifdef PA_LINUX
  534. tramp[0] = 0xeaa00000; /* b,l .+8,%r21 ; %r21 <- pc+8 */
  535. tramp[1] = 0xd6a01c1e; /* depi 0,31,2,%r21 ; mask priv bits */
  536. tramp[2] = 0x4aa10028; /* ldw 20(%r21),%r1 ; load plabel */
  537. tramp[3] = 0x36b53ff1; /* ldo -8(%r21),%r21 ; get closure addr */
  538. tramp[4] = 0x0c201096; /* ldw 0(%r1),%r22 ; address of handler */
  539. tramp[5] = 0xeac0c000; /* bv%r0(%r22) ; branch to handler */
  540. tramp[6] = 0x0c281093; /* ldw 4(%r1),%r19 ; GP of handler */
  541. tramp[7] = ((UINT32)(ffi_closure_pa32) & ~2);
  542. /* Flush d/icache -- have to flush up 2 two lines because of
  543. alignment. */
  544. __asm__ volatile(
  545. "fdc 0(%0)\n\t"
  546. "fdc %1(%0)\n\t"
  547. "fic 0(%%sr4, %0)\n\t"
  548. "fic %1(%%sr4, %0)\n\t"
  549. "sync\n\t"
  550. "nop\n\t"
  551. "nop\n\t"
  552. "nop\n\t"
  553. "nop\n\t"
  554. "nop\n\t"
  555. "nop\n\t"
  556. "nop\n"
  557. :
  558. : "r"((unsigned long)tramp & ~31),
  559. "r"(32 /* stride */)
  560. : "memory");
  561. #endif
  562. #ifdef PA_HPUX
  563. tramp[0] = 0xeaa00000; /* b,l .+8,%r21 ; %r21 <- pc+8 */
  564. tramp[1] = 0xd6a01c1e; /* depi 0,31,2,%r21 ; mask priv bits */
  565. tramp[2] = 0x4aa10038; /* ldw 28(%r21),%r1 ; load plabel */
  566. tramp[3] = 0x36b53ff1; /* ldo -8(%r21),%r21 ; get closure addr */
  567. tramp[4] = 0x0c201096; /* ldw 0(%r1),%r22 ; address of handler */
  568. tramp[5] = 0x02c010b4; /* ldsid (%r22),%r20 ; load space id */
  569. tramp[6] = 0x00141820; /* mtsp %r20,%sr0 ; into %sr0 */
  570. tramp[7] = 0xe2c00000; /* be 0(%sr0,%r22) ; branch to handler */
  571. tramp[8] = 0x0c281093; /* ldw 4(%r1),%r19 ; GP of handler */
  572. tramp[9] = ((UINT32)(ffi_closure_pa32) & ~2);
  573. /* Flush d/icache -- have to flush three lines because of alignment. */
  574. __asm__ volatile(
  575. "copy %1,%0\n\t"
  576. "fdc,m %2(%0)\n\t"
  577. "fdc,m %2(%0)\n\t"
  578. "fdc,m %2(%0)\n\t"
  579. "ldsid (%1),%0\n\t"
  580. "mtsp %0,%%sr0\n\t"
  581. "copy %1,%0\n\t"
  582. "fic,m %2(%%sr0,%0)\n\t"
  583. "fic,m %2(%%sr0,%0)\n\t"
  584. "fic,m %2(%%sr0,%0)\n\t"
  585. "sync\n\t"
  586. "nop\n\t"
  587. "nop\n\t"
  588. "nop\n\t"
  589. "nop\n\t"
  590. "nop\n\t"
  591. "nop\n\t"
  592. "nop\n"
  593. : "=&r" ((unsigned long)tmp)
  594. : "r" ((unsigned long)tramp & ~31),
  595. "r" (32/* stride */)
  596. : "memory");
  597. #endif
  598. closure->cif = cif;
  599. closure->user_data = user_data;
  600. closure->fun = fun;
  601. return FFI_OK;
  602. }
  603. #endif