lj_ccallback.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. ** FFI C callback handling.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #include "lj_obj.h"
  6. #if LJ_HASFFI
  7. #include "lj_gc.h"
  8. #include "lj_err.h"
  9. #include "lj_tab.h"
  10. #include "lj_state.h"
  11. #include "lj_frame.h"
  12. #include "lj_ctype.h"
  13. #include "lj_cconv.h"
  14. #include "lj_ccall.h"
  15. #include "lj_ccallback.h"
  16. #include "lj_target.h"
  17. #include "lj_mcode.h"
  18. #include "lj_trace.h"
  19. #include "lj_vm.h"
  20. /* -- Target-specific handling of callback slots -------------------------- */
  21. #define CALLBACK_MCODE_SIZE (LJ_PAGESIZE * LJ_NUM_CBPAGE)
  22. #if LJ_OS_NOJIT
  23. /* Callbacks disabled. */
  24. #define CALLBACK_SLOT2OFS(slot) (0*(slot))
  25. #define CALLBACK_OFS2SLOT(ofs) (0*(ofs))
  26. #define CALLBACK_MAX_SLOT 0
  27. #elif LJ_TARGET_X86ORX64
  28. #define CALLBACK_MCODE_HEAD (LJ_64 ? 8 : 0)
  29. #define CALLBACK_MCODE_GROUP (-2+1+2+(LJ_GC64 ? 10 : 5)+(LJ_64 ? 6 : 5))
  30. #define CALLBACK_SLOT2OFS(slot) \
  31. (CALLBACK_MCODE_HEAD + CALLBACK_MCODE_GROUP*((slot)/32) + 4*(slot))
  32. static MSize CALLBACK_OFS2SLOT(MSize ofs)
  33. {
  34. MSize group;
  35. ofs -= CALLBACK_MCODE_HEAD;
  36. group = ofs / (32*4 + CALLBACK_MCODE_GROUP);
  37. return (ofs % (32*4 + CALLBACK_MCODE_GROUP))/4 + group*32;
  38. }
  39. #define CALLBACK_MAX_SLOT \
  40. (((CALLBACK_MCODE_SIZE-CALLBACK_MCODE_HEAD)/(CALLBACK_MCODE_GROUP+4*32))*32)
  41. #elif LJ_TARGET_ARM
  42. #define CALLBACK_MCODE_HEAD 32
  43. #elif LJ_TARGET_ARM64
  44. #define CALLBACK_MCODE_HEAD 32
  45. #elif LJ_TARGET_PPC
  46. #define CALLBACK_MCODE_HEAD 24
  47. #elif LJ_TARGET_MIPS32
  48. #define CALLBACK_MCODE_HEAD 20
  49. #elif LJ_TARGET_MIPS64
  50. #define CALLBACK_MCODE_HEAD 52
  51. #else
  52. /* Missing support for this architecture. */
  53. #define CALLBACK_SLOT2OFS(slot) (0*(slot))
  54. #define CALLBACK_OFS2SLOT(ofs) (0*(ofs))
  55. #define CALLBACK_MAX_SLOT 0
  56. #endif
  57. #ifndef CALLBACK_SLOT2OFS
  58. #define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot))
  59. #define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8)
  60. #define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE))
  61. #endif
  62. /* Convert callback slot number to callback function pointer. */
  63. static void *callback_slot2ptr(CTState *cts, MSize slot)
  64. {
  65. return (uint8_t *)cts->cb.mcode + CALLBACK_SLOT2OFS(slot);
  66. }
  67. /* Convert callback function pointer to slot number. */
  68. MSize lj_ccallback_ptr2slot(CTState *cts, void *p)
  69. {
  70. uintptr_t ofs = (uintptr_t)((uint8_t *)p -(uint8_t *)cts->cb.mcode);
  71. if (ofs < CALLBACK_MCODE_SIZE) {
  72. MSize slot = CALLBACK_OFS2SLOT((MSize)ofs);
  73. if (CALLBACK_SLOT2OFS(slot) == (MSize)ofs)
  74. return slot;
  75. }
  76. return ~0u; /* Not a known callback function pointer. */
  77. }
  78. /* Initialize machine code for callback function pointers. */
  79. #if LJ_OS_NOJIT
  80. /* Disabled callback support. */
  81. #define callback_mcode_init(g, p) (p)
  82. #elif LJ_TARGET_X86ORX64
  83. static void *callback_mcode_init(global_State *g, uint8_t *page)
  84. {
  85. uint8_t *p = page;
  86. uint8_t *target = (uint8_t *)(void *)lj_vm_ffi_callback;
  87. MSize slot;
  88. #if LJ_64
  89. *(void **)p = target; p += 8;
  90. #endif
  91. for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
  92. /* mov al, slot; jmp group */
  93. *p++ = XI_MOVrib | RID_EAX; *p++ = (uint8_t)slot;
  94. if ((slot & 31) == 31 || slot == CALLBACK_MAX_SLOT-1) {
  95. /* push ebp/rbp; mov ah, slot>>8; mov ebp, &g. */
  96. *p++ = XI_PUSH + RID_EBP;
  97. *p++ = XI_MOVrib | (RID_EAX+4); *p++ = (uint8_t)(slot >> 8);
  98. #if LJ_GC64
  99. *p++ = 0x48; *p++ = XI_MOVri | RID_EBP;
  100. *(uint64_t *)p = (uint64_t)(g); p += 8;
  101. #else
  102. *p++ = XI_MOVri | RID_EBP;
  103. *(int32_t *)p = i32ptr(g); p += 4;
  104. #endif
  105. #if LJ_64
  106. /* jmp [rip-pageofs] where lj_vm_ffi_callback is stored. */
  107. *p++ = XI_GROUP5; *p++ = XM_OFS0 + (XOg_JMP<<3) + RID_EBP;
  108. *(int32_t *)p = (int32_t)(page-(p+4)); p += 4;
  109. #else
  110. /* jmp lj_vm_ffi_callback. */
  111. *p++ = XI_JMP; *(int32_t *)p = target-(p+4); p += 4;
  112. #endif
  113. } else {
  114. *p++ = XI_JMPs; *p++ = (uint8_t)((2+2)*(31-(slot&31)) - 2);
  115. }
  116. }
  117. return p;
  118. }
  119. #elif LJ_TARGET_ARM
  120. static void *callback_mcode_init(global_State *g, uint32_t *page)
  121. {
  122. uint32_t *p = page;
  123. void *target = (void *)lj_vm_ffi_callback;
  124. MSize slot;
  125. /* This must match with the saveregs macro in buildvm_arm.dasc. */
  126. *p++ = ARMI_SUB|ARMF_D(RID_R12)|ARMF_N(RID_R12)|ARMF_M(RID_PC);
  127. *p++ = ARMI_PUSH|ARMF_N(RID_SP)|RSET_RANGE(RID_R4,RID_R11+1)|RID2RSET(RID_LR);
  128. *p++ = ARMI_SUB|ARMI_K12|ARMF_D(RID_R12)|ARMF_N(RID_R12)|CALLBACK_MCODE_HEAD;
  129. *p++ = ARMI_STR|ARMI_LS_P|ARMI_LS_W|ARMF_D(RID_R12)|ARMF_N(RID_SP)|(CFRAME_SIZE-4*9);
  130. *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_R12)|ARMF_N(RID_PC);
  131. *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_PC)|ARMF_N(RID_PC);
  132. *p++ = u32ptr(g);
  133. *p++ = u32ptr(target);
  134. for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
  135. *p++ = ARMI_MOV|ARMF_D(RID_R12)|ARMF_M(RID_PC);
  136. *p = ARMI_B | ((page-p-2) & 0x00ffffffu);
  137. p++;
  138. }
  139. return p;
  140. }
  141. #elif LJ_TARGET_ARM64
  142. static void *callback_mcode_init(global_State *g, uint32_t *page)
  143. {
  144. uint32_t *p = page;
  145. ASMFunction target = lj_vm_ffi_callback;
  146. MSize slot;
  147. *p++ = A64I_LE(A64I_LDRLx | A64F_D(RID_X11) | A64F_S19(4));
  148. *p++ = A64I_LE(A64I_LDRLx | A64F_D(RID_X10) | A64F_S19(5));
  149. *p++ = A64I_LE(A64I_BR_AUTH | A64F_N(RID_X11));
  150. *p++ = A64I_LE(A64I_NOP);
  151. ((ASMFunction *)p)[0] = target;
  152. ((void **)p)[1] = g;
  153. p += 4;
  154. for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
  155. *p++ = A64I_LE(A64I_MOVZw | A64F_D(RID_X9) | A64F_U16(slot));
  156. *p = A64I_LE(A64I_B | A64F_S26((page-p) & 0x03ffffffu));
  157. p++;
  158. }
  159. return p;
  160. }
  161. #elif LJ_TARGET_PPC
  162. static void *callback_mcode_init(global_State *g, uint32_t *page)
  163. {
  164. uint32_t *p = page;
  165. void *target = (void *)lj_vm_ffi_callback;
  166. MSize slot;
  167. *p++ = PPCI_LIS | PPCF_T(RID_TMP) | (u32ptr(target) >> 16);
  168. *p++ = PPCI_LIS | PPCF_T(RID_R12) | (u32ptr(g) >> 16);
  169. *p++ = PPCI_ORI | PPCF_A(RID_TMP)|PPCF_T(RID_TMP) | (u32ptr(target) & 0xffff);
  170. *p++ = PPCI_ORI | PPCF_A(RID_R12)|PPCF_T(RID_R12) | (u32ptr(g) & 0xffff);
  171. *p++ = PPCI_MTCTR | PPCF_T(RID_TMP);
  172. *p++ = PPCI_BCTR;
  173. for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
  174. *p++ = PPCI_LI | PPCF_T(RID_R11) | slot;
  175. *p = PPCI_B | (((page-p) & 0x00ffffffu) << 2);
  176. p++;
  177. }
  178. return p;
  179. }
  180. #elif LJ_TARGET_MIPS
  181. static void *callback_mcode_init(global_State *g, uint32_t *page)
  182. {
  183. uint32_t *p = page;
  184. uintptr_t target = (uintptr_t)(void *)lj_vm_ffi_callback;
  185. uintptr_t ug = (uintptr_t)(void *)g;
  186. MSize slot;
  187. #if LJ_TARGET_MIPS32
  188. *p++ = MIPSI_LUI | MIPSF_T(RID_R3) | (target >> 16);
  189. *p++ = MIPSI_LUI | MIPSF_T(RID_R2) | (ug >> 16);
  190. #else
  191. *p++ = MIPSI_LUI | MIPSF_T(RID_R3) | (target >> 48);
  192. *p++ = MIPSI_LUI | MIPSF_T(RID_R2) | (ug >> 48);
  193. *p++ = MIPSI_ORI | MIPSF_T(RID_R3)|MIPSF_S(RID_R3) | ((target >> 32) & 0xffff);
  194. *p++ = MIPSI_ORI | MIPSF_T(RID_R2)|MIPSF_S(RID_R2) | ((ug >> 32) & 0xffff);
  195. *p++ = MIPSI_DSLL | MIPSF_D(RID_R3)|MIPSF_T(RID_R3) | MIPSF_A(16);
  196. *p++ = MIPSI_DSLL | MIPSF_D(RID_R2)|MIPSF_T(RID_R2) | MIPSF_A(16);
  197. *p++ = MIPSI_ORI | MIPSF_T(RID_R3)|MIPSF_S(RID_R3) | ((target >> 16) & 0xffff);
  198. *p++ = MIPSI_ORI | MIPSF_T(RID_R2)|MIPSF_S(RID_R2) | ((ug >> 16) & 0xffff);
  199. *p++ = MIPSI_DSLL | MIPSF_D(RID_R3)|MIPSF_T(RID_R3) | MIPSF_A(16);
  200. *p++ = MIPSI_DSLL | MIPSF_D(RID_R2)|MIPSF_T(RID_R2) | MIPSF_A(16);
  201. #endif
  202. *p++ = MIPSI_ORI | MIPSF_T(RID_R3)|MIPSF_S(RID_R3) | (target & 0xffff);
  203. *p++ = MIPSI_JR | MIPSF_S(RID_R3);
  204. *p++ = MIPSI_ORI | MIPSF_T(RID_R2)|MIPSF_S(RID_R2) | (ug & 0xffff);
  205. for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) {
  206. *p = MIPSI_B | ((page-p-1) & 0x0000ffffu);
  207. p++;
  208. *p++ = MIPSI_LI | MIPSF_T(RID_R1) | slot;
  209. }
  210. return p;
  211. }
  212. #else
  213. /* Missing support for this architecture. */
  214. #define callback_mcode_init(g, p) (p)
  215. #endif
  216. /* -- Machine code management --------------------------------------------- */
  217. #if LJ_TARGET_WINDOWS
  218. #define WIN32_LEAN_AND_MEAN
  219. #include <windows.h>
  220. #elif LJ_TARGET_POSIX
  221. #include <sys/mman.h>
  222. #ifndef MAP_ANONYMOUS
  223. #define MAP_ANONYMOUS MAP_ANON
  224. #endif
  225. #ifdef PROT_MPROTECT
  226. #define CCPROT_CREATE (PROT_MPROTECT(PROT_EXEC))
  227. #else
  228. #define CCPROT_CREATE 0
  229. #endif
  230. #endif
  231. /* Allocate and initialize area for callback function pointers. */
  232. static void callback_mcode_new(CTState *cts)
  233. {
  234. size_t sz = (size_t)CALLBACK_MCODE_SIZE;
  235. void *p, *pe;
  236. if (CALLBACK_MAX_SLOT == 0)
  237. lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
  238. #if LJ_TARGET_WINDOWS
  239. p = LJ_WIN_VALLOC(NULL, sz, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
  240. if (!p)
  241. lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
  242. #elif LJ_TARGET_POSIX
  243. p = mmap(NULL, sz, (PROT_READ|PROT_WRITE|CCPROT_CREATE), MAP_PRIVATE|MAP_ANONYMOUS,
  244. -1, 0);
  245. if (p == MAP_FAILED)
  246. lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
  247. #else
  248. /* Fallback allocator. Fails if memory is not executable by default. */
  249. p = lj_mem_new(cts->L, sz);
  250. #endif
  251. cts->cb.mcode = p;
  252. pe = callback_mcode_init(cts->g, p);
  253. UNUSED(pe);
  254. lj_assertCTS((size_t)((char *)pe - (char *)p) <= sz,
  255. "miscalculated CALLBACK_MAX_SLOT");
  256. lj_mcode_sync(p, (char *)p + sz);
  257. #if LJ_TARGET_WINDOWS
  258. {
  259. DWORD oprot;
  260. LJ_WIN_VPROTECT(p, sz, PAGE_EXECUTE_READ, &oprot);
  261. }
  262. #elif LJ_TARGET_POSIX
  263. mprotect(p, sz, (PROT_READ|PROT_EXEC));
  264. #endif
  265. }
  266. /* Free area for callback function pointers. */
  267. void lj_ccallback_mcode_free(CTState *cts)
  268. {
  269. size_t sz = (size_t)CALLBACK_MCODE_SIZE;
  270. void *p = cts->cb.mcode;
  271. if (p == NULL) return;
  272. #if LJ_TARGET_WINDOWS
  273. VirtualFree(p, 0, MEM_RELEASE);
  274. UNUSED(sz);
  275. #elif LJ_TARGET_POSIX
  276. munmap(p, sz);
  277. #else
  278. lj_mem_free(cts->g, p, sz);
  279. #endif
  280. }
  281. /* -- C callback entry ---------------------------------------------------- */
  282. /* Target-specific handling of register arguments. Similar to lj_ccall.c. */
  283. #if LJ_TARGET_X86
  284. #define CALLBACK_HANDLE_REGARG \
  285. if (!isfp) { /* Only non-FP values may be passed in registers. */ \
  286. if (n > 1) { /* Anything > 32 bit is passed on the stack. */ \
  287. if (!LJ_ABI_WIN) ngpr = maxgpr; /* Prevent reordering. */ \
  288. } else if (ngpr + 1 <= maxgpr) { \
  289. sp = &cts->cb.gpr[ngpr]; \
  290. ngpr += n; \
  291. goto done; \
  292. } \
  293. }
  294. #elif LJ_TARGET_X64 && LJ_ABI_WIN
  295. /* Windows/x64 argument registers are strictly positional (use ngpr). */
  296. #define CALLBACK_HANDLE_REGARG \
  297. if (isfp) { \
  298. if (ngpr < maxgpr) { sp = &cts->cb.fpr[ngpr++]; UNUSED(nfpr); goto done; } \
  299. } else { \
  300. if (ngpr < maxgpr) { sp = &cts->cb.gpr[ngpr++]; goto done; } \
  301. }
  302. #elif LJ_TARGET_X64
  303. #define CALLBACK_HANDLE_REGARG \
  304. if (isfp) { \
  305. if (nfpr + n <= CCALL_NARG_FPR) { \
  306. sp = &cts->cb.fpr[nfpr]; \
  307. nfpr += n; \
  308. goto done; \
  309. } \
  310. } else { \
  311. if (ngpr + n <= maxgpr) { \
  312. sp = &cts->cb.gpr[ngpr]; \
  313. ngpr += n; \
  314. goto done; \
  315. } \
  316. }
  317. #elif LJ_TARGET_ARM
  318. #if LJ_ABI_SOFTFP
  319. #define CALLBACK_HANDLE_REGARG_FP1 UNUSED(isfp);
  320. #define CALLBACK_HANDLE_REGARG_FP2
  321. #else
  322. #define CALLBACK_HANDLE_REGARG_FP1 \
  323. if (isfp) { \
  324. if (n == 1) { \
  325. if (fprodd) { \
  326. sp = &cts->cb.fpr[fprodd-1]; \
  327. fprodd = 0; \
  328. goto done; \
  329. } else if (nfpr + 1 <= CCALL_NARG_FPR) { \
  330. sp = &cts->cb.fpr[nfpr++]; \
  331. fprodd = nfpr; \
  332. goto done; \
  333. } \
  334. } else { \
  335. if (nfpr + 1 <= CCALL_NARG_FPR) { \
  336. sp = &cts->cb.fpr[nfpr++]; \
  337. goto done; \
  338. } \
  339. } \
  340. fprodd = 0; /* No reordering after the first FP value is on stack. */ \
  341. } else {
  342. #define CALLBACK_HANDLE_REGARG_FP2 }
  343. #endif
  344. #define CALLBACK_HANDLE_REGARG \
  345. CALLBACK_HANDLE_REGARG_FP1 \
  346. if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \
  347. if (ngpr + n <= maxgpr) { \
  348. sp = &cts->cb.gpr[ngpr]; \
  349. ngpr += n; \
  350. goto done; \
  351. } CALLBACK_HANDLE_REGARG_FP2
  352. #elif LJ_TARGET_ARM64
  353. #define CALLBACK_HANDLE_REGARG \
  354. if (isfp) { \
  355. if (nfpr + n <= CCALL_NARG_FPR) { \
  356. sp = &cts->cb.fpr[nfpr]; \
  357. nfpr += n; \
  358. goto done; \
  359. } else { \
  360. nfpr = CCALL_NARG_FPR; /* Prevent reordering. */ \
  361. } \
  362. } else { \
  363. if (!LJ_TARGET_OSX && n > 1) \
  364. ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \
  365. if (ngpr + n <= maxgpr) { \
  366. sp = &cts->cb.gpr[ngpr]; \
  367. ngpr += n; \
  368. goto done; \
  369. } else { \
  370. ngpr = CCALL_NARG_GPR; /* Prevent reordering. */ \
  371. } \
  372. }
  373. #elif LJ_TARGET_PPC
  374. #define CALLBACK_HANDLE_GPR \
  375. if (n > 1) { \
  376. lj_assertCTS(((LJ_ABI_SOFTFP && ctype_isnum(cta->info)) || /* double. */ \
  377. ctype_isinteger(cta->info)) && n == 2, /* int64_t. */ \
  378. "bad GPR type"); \
  379. ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \
  380. } \
  381. if (ngpr + n <= maxgpr) { \
  382. sp = &cts->cb.gpr[ngpr]; \
  383. ngpr += n; \
  384. goto done; \
  385. }
  386. #if LJ_ABI_SOFTFP
  387. #define CALLBACK_HANDLE_REGARG \
  388. CALLBACK_HANDLE_GPR \
  389. UNUSED(isfp);
  390. #else
  391. #define CALLBACK_HANDLE_REGARG \
  392. if (isfp) { \
  393. if (nfpr + 1 <= CCALL_NARG_FPR) { \
  394. sp = &cts->cb.fpr[nfpr++]; \
  395. cta = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ \
  396. goto done; \
  397. } \
  398. } else { /* Try to pass argument in GPRs. */ \
  399. CALLBACK_HANDLE_GPR \
  400. }
  401. #endif
  402. #if !LJ_ABI_SOFTFP
  403. #define CALLBACK_HANDLE_RET \
  404. if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
  405. *(double *)dp = *(float *)dp; /* FPRs always hold doubles. */
  406. #endif
  407. #elif LJ_TARGET_MIPS32
  408. #define CALLBACK_HANDLE_GPR \
  409. if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \
  410. if (ngpr + n <= maxgpr) { \
  411. sp = &cts->cb.gpr[ngpr]; \
  412. ngpr += n; \
  413. goto done; \
  414. }
  415. #if !LJ_ABI_SOFTFP /* MIPS32 hard-float */
  416. #define CALLBACK_HANDLE_REGARG \
  417. if (isfp && nfpr < CCALL_NARG_FPR) { /* Try to pass argument in FPRs. */ \
  418. sp = (void *)((uint8_t *)&cts->cb.fpr[nfpr] + ((LJ_BE && n==1) ? 4 : 0)); \
  419. nfpr++; ngpr += n; \
  420. goto done; \
  421. } else { /* Try to pass argument in GPRs. */ \
  422. nfpr = CCALL_NARG_FPR; \
  423. CALLBACK_HANDLE_GPR \
  424. }
  425. #else /* MIPS32 soft-float */
  426. #define CALLBACK_HANDLE_REGARG \
  427. CALLBACK_HANDLE_GPR \
  428. UNUSED(isfp);
  429. #endif
  430. #define CALLBACK_HANDLE_RET \
  431. if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
  432. ((float *)dp)[1] = *(float *)dp;
  433. #elif LJ_TARGET_MIPS64
  434. #if !LJ_ABI_SOFTFP /* MIPS64 hard-float */
  435. #define CALLBACK_HANDLE_REGARG \
  436. if (ngpr + n <= maxgpr) { \
  437. sp = isfp ? (void*) &cts->cb.fpr[ngpr] : (void*) &cts->cb.gpr[ngpr]; \
  438. ngpr += n; \
  439. goto done; \
  440. }
  441. #else /* MIPS64 soft-float */
  442. #define CALLBACK_HANDLE_REGARG \
  443. if (ngpr + n <= maxgpr) { \
  444. UNUSED(isfp); \
  445. sp = (void*) &cts->cb.gpr[ngpr]; \
  446. ngpr += n; \
  447. goto done; \
  448. }
  449. #endif
  450. #define CALLBACK_HANDLE_RET \
  451. if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \
  452. ((float *)dp)[1] = *(float *)dp;
  453. #else
  454. #error "Missing calling convention definitions for this architecture"
  455. #endif
  456. /* Convert and push callback arguments to Lua stack. */
  457. static void callback_conv_args(CTState *cts, lua_State *L)
  458. {
  459. TValue *o = L->top;
  460. intptr_t *stack = cts->cb.stack;
  461. MSize slot = cts->cb.slot;
  462. CTypeID id = 0, rid, fid;
  463. int gcsteps = 0;
  464. CType *ct;
  465. GCfunc *fn;
  466. int fntp;
  467. MSize ngpr = 0, nsp = 0, maxgpr = CCALL_NARG_GPR;
  468. #if CCALL_NARG_FPR
  469. MSize nfpr = 0;
  470. #if LJ_TARGET_ARM
  471. MSize fprodd = 0;
  472. #endif
  473. #endif
  474. if (slot < cts->cb.sizeid && (id = cts->cb.cbid[slot]) != 0) {
  475. ct = ctype_get(cts, id);
  476. rid = ctype_cid(ct->info); /* Return type. x86: +(spadj<<16). */
  477. fn = funcV(lj_tab_getint(cts->miscmap, (int32_t)slot));
  478. fntp = LJ_TFUNC;
  479. } else { /* Must set up frame first, before throwing the error. */
  480. ct = NULL;
  481. rid = 0;
  482. fn = (GCfunc *)L;
  483. fntp = LJ_TTHREAD;
  484. }
  485. /* Continuation returns from callback. */
  486. if (LJ_FR2) {
  487. (o++)->u64 = LJ_CONT_FFI_CALLBACK;
  488. (o++)->u64 = rid;
  489. } else {
  490. o->u32.lo = LJ_CONT_FFI_CALLBACK;
  491. o->u32.hi = rid;
  492. o++;
  493. }
  494. setframe_gc(o, obj2gco(fn), fntp);
  495. if (LJ_FR2) o++;
  496. setframe_ftsz(o, ((char *)(o+1) - (char *)L->base) + FRAME_CONT);
  497. L->top = L->base = ++o;
  498. if (!ct)
  499. lj_err_caller(cts->L, LJ_ERR_FFI_BADCBACK);
  500. if (isluafunc(fn))
  501. setcframe_pc(L->cframe, proto_bc(funcproto(fn))+1);
  502. lj_state_checkstack(L, LUA_MINSTACK); /* May throw. */
  503. o = L->base; /* Might have been reallocated. */
  504. #if LJ_TARGET_X86
  505. /* x86 has several different calling conventions. */
  506. switch (ctype_cconv(ct->info)) {
  507. case CTCC_FASTCALL: maxgpr = 2; break;
  508. case CTCC_THISCALL: maxgpr = 1; break;
  509. default: maxgpr = 0; break;
  510. }
  511. #endif
  512. fid = ct->sib;
  513. while (fid) {
  514. CType *ctf = ctype_get(cts, fid);
  515. if (!ctype_isattrib(ctf->info)) {
  516. CType *cta;
  517. void *sp;
  518. CTSize sz;
  519. int isfp;
  520. MSize n;
  521. lj_assertCTS(ctype_isfield(ctf->info), "field expected");
  522. cta = ctype_rawchild(cts, ctf);
  523. isfp = ctype_isfp(cta->info);
  524. sz = (cta->size + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1);
  525. n = sz / CTSIZE_PTR; /* Number of GPRs or stack slots needed. */
  526. CALLBACK_HANDLE_REGARG /* Handle register arguments. */
  527. /* Otherwise pass argument on stack. */
  528. if (CCALL_ALIGN_STACKARG && LJ_32 && sz == 8)
  529. nsp = (nsp + 1) & ~1u; /* Align 64 bit argument on stack. */
  530. sp = &stack[nsp];
  531. nsp += n;
  532. done:
  533. if (LJ_BE && cta->size < CTSIZE_PTR
  534. #if LJ_TARGET_MIPS64
  535. && !(isfp && nsp)
  536. #endif
  537. )
  538. sp = (void *)((uint8_t *)sp + CTSIZE_PTR-cta->size);
  539. gcsteps += lj_cconv_tv_ct(cts, cta, 0, o++, sp);
  540. }
  541. fid = ctf->sib;
  542. }
  543. L->top = o;
  544. #if LJ_TARGET_X86
  545. /* Store stack adjustment for returns from non-cdecl callbacks. */
  546. if (ctype_cconv(ct->info) != CTCC_CDECL) {
  547. #if LJ_FR2
  548. (L->base-3)->u64 |= (nsp << (16+2));
  549. #else
  550. (L->base-2)->u32.hi |= (nsp << (16+2));
  551. #endif
  552. }
  553. #endif
  554. while (gcsteps-- > 0)
  555. lj_gc_check(L);
  556. }
  557. /* Convert Lua object to callback result. */
  558. static void callback_conv_result(CTState *cts, lua_State *L, TValue *o)
  559. {
  560. #if LJ_FR2
  561. CType *ctr = ctype_raw(cts, (uint16_t)(L->base-3)->u64);
  562. #else
  563. CType *ctr = ctype_raw(cts, (uint16_t)(L->base-2)->u32.hi);
  564. #endif
  565. #if LJ_TARGET_X86
  566. cts->cb.gpr[2] = 0;
  567. #endif
  568. if (!ctype_isvoid(ctr->info)) {
  569. uint8_t *dp = (uint8_t *)&cts->cb.gpr[0];
  570. #if CCALL_NUM_FPR
  571. if (ctype_isfp(ctr->info))
  572. dp = (uint8_t *)&cts->cb.fpr[0];
  573. #endif
  574. #if LJ_TARGET_ARM64 && LJ_BE
  575. if (ctype_isfp(ctr->info) && ctr->size == sizeof(float))
  576. dp = (uint8_t *)&cts->cb.fpr[0].f[1];
  577. #endif
  578. lj_cconv_ct_tv(cts, ctr, dp, o, 0);
  579. #ifdef CALLBACK_HANDLE_RET
  580. CALLBACK_HANDLE_RET
  581. #endif
  582. /* Extend returned integers to (at least) 32 bits. */
  583. if (ctype_isinteger_or_bool(ctr->info) && ctr->size < 4) {
  584. if (ctr->info & CTF_UNSIGNED)
  585. *(uint32_t *)dp = ctr->size == 1 ? (uint32_t)*(uint8_t *)dp :
  586. (uint32_t)*(uint16_t *)dp;
  587. else
  588. *(int32_t *)dp = ctr->size == 1 ? (int32_t)*(int8_t *)dp :
  589. (int32_t)*(int16_t *)dp;
  590. }
  591. #if LJ_TARGET_MIPS64 || (LJ_TARGET_ARM64 && LJ_BE)
  592. /* Always sign-extend results to 64 bits. Even a soft-fp 'float'. */
  593. if (ctr->size <= 4 &&
  594. (LJ_ABI_SOFTFP || ctype_isinteger_or_bool(ctr->info)))
  595. *(int64_t *)dp = (int64_t)*(int32_t *)dp;
  596. #endif
  597. #if LJ_TARGET_X86
  598. if (ctype_isfp(ctr->info))
  599. cts->cb.gpr[2] = ctr->size == sizeof(float) ? 1 : 2;
  600. #endif
  601. }
  602. }
  603. /* Enter callback. */
  604. lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf)
  605. {
  606. lua_State *L = cts->L;
  607. global_State *g = cts->g;
  608. lj_assertG(L != NULL, "uninitialized cts->L in callback");
  609. if (tvref(g->jit_base)) {
  610. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_FFI_BADCBACK));
  611. if (g->panic) g->panic(L);
  612. exit(EXIT_FAILURE);
  613. }
  614. lj_trace_abort(g); /* Never record across callback. */
  615. /* Setup C frame. */
  616. cframe_prev(cf) = L->cframe;
  617. setcframe_L(cf, L);
  618. cframe_errfunc(cf) = -1;
  619. cframe_nres(cf) = 0;
  620. L->cframe = cf;
  621. callback_conv_args(cts, L);
  622. return L; /* Now call the function on this stack. */
  623. }
  624. /* Leave callback. */
  625. void LJ_FASTCALL lj_ccallback_leave(CTState *cts, TValue *o)
  626. {
  627. lua_State *L = cts->L;
  628. GCfunc *fn;
  629. TValue *obase = L->base;
  630. L->base = L->top; /* Keep continuation frame for throwing errors. */
  631. if (o >= L->base) {
  632. /* PC of RET* is lost. Point to last line for result conv. errors. */
  633. fn = curr_func(L);
  634. if (isluafunc(fn)) {
  635. GCproto *pt = funcproto(fn);
  636. setcframe_pc(L->cframe, proto_bc(pt)+pt->sizebc+1);
  637. }
  638. }
  639. callback_conv_result(cts, L, o);
  640. /* Finally drop C frame and continuation frame. */
  641. L->top -= 2+2*LJ_FR2;
  642. L->base = obase;
  643. L->cframe = cframe_prev(L->cframe);
  644. cts->cb.slot = 0; /* Blacklist C function that called the callback. */
  645. }
  646. /* -- C callback management ----------------------------------------------- */
  647. /* Get an unused slot in the callback slot table. */
  648. static MSize callback_slot_new(CTState *cts, CType *ct)
  649. {
  650. CTypeID id = ctype_typeid(cts, ct);
  651. CTypeID1 *cbid = cts->cb.cbid;
  652. MSize top;
  653. for (top = cts->cb.topid; top < cts->cb.sizeid; top++)
  654. if (LJ_LIKELY(cbid[top] == 0))
  655. goto found;
  656. #if CALLBACK_MAX_SLOT
  657. if (top >= CALLBACK_MAX_SLOT)
  658. #endif
  659. lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
  660. if (!cts->cb.mcode)
  661. callback_mcode_new(cts);
  662. lj_mem_growvec(cts->L, cbid, cts->cb.sizeid, CALLBACK_MAX_SLOT, CTypeID1);
  663. cts->cb.cbid = cbid;
  664. memset(cbid+top, 0, (cts->cb.sizeid-top)*sizeof(CTypeID1));
  665. found:
  666. cbid[top] = id;
  667. cts->cb.topid = top+1;
  668. return top;
  669. }
  670. /* Check for function pointer and supported argument/result types. */
  671. static CType *callback_checkfunc(CTState *cts, CType *ct)
  672. {
  673. int narg = 0;
  674. if (!ctype_isptr(ct->info) || (LJ_64 && ct->size != CTSIZE_PTR))
  675. return NULL;
  676. ct = ctype_rawchild(cts, ct);
  677. if (ctype_isfunc(ct->info)) {
  678. CType *ctr = ctype_rawchild(cts, ct);
  679. CTypeID fid = ct->sib;
  680. if (!(ctype_isvoid(ctr->info) || ctype_isenum(ctr->info) ||
  681. ctype_isptr(ctr->info) || (ctype_isnum(ctr->info) && ctr->size <= 8)))
  682. return NULL;
  683. if ((ct->info & CTF_VARARG))
  684. return NULL;
  685. while (fid) {
  686. CType *ctf = ctype_get(cts, fid);
  687. if (!ctype_isattrib(ctf->info)) {
  688. CType *cta;
  689. lj_assertCTS(ctype_isfield(ctf->info), "field expected");
  690. cta = ctype_rawchild(cts, ctf);
  691. if (!(ctype_isenum(cta->info) || ctype_isptr(cta->info) ||
  692. (ctype_isnum(cta->info) && cta->size <= 8)) ||
  693. ++narg >= LUA_MINSTACK-3)
  694. return NULL;
  695. }
  696. fid = ctf->sib;
  697. }
  698. return ct;
  699. }
  700. return NULL;
  701. }
  702. /* Create a new callback and return the callback function pointer. */
  703. void *lj_ccallback_new(CTState *cts, CType *ct, GCfunc *fn)
  704. {
  705. ct = callback_checkfunc(cts, ct);
  706. if (ct) {
  707. MSize slot = callback_slot_new(cts, ct);
  708. GCtab *t = cts->miscmap;
  709. setfuncV(cts->L, lj_tab_setint(cts->L, t, (int32_t)slot), fn);
  710. lj_gc_anybarriert(cts->L, t);
  711. return callback_slot2ptr(cts, slot);
  712. }
  713. return NULL; /* Bad conversion. */
  714. }
  715. #endif