glue_x86_64.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #ifndef _NSEEL_GLUE_X86_64_H_
  2. #define _NSEEL_GLUE_X86_64_H_
  3. #define GLUE_MAX_FPSTACK_SIZE 8
  4. #define GLUE_JMP_SET_OFFSET(endOfInstruction,offset) (((int *)(endOfInstruction))[-1] = (offset))
  5. #define GLUE_PREFER_NONFP_DV_ASSIGNS
  6. static const unsigned char GLUE_JMP_NC[] = { 0xE9, 0,0,0,0, }; // jmp<offset>
  7. static const unsigned char GLUE_JMP_IF_P1_Z[] = {0x85, 0xC0, 0x0F, 0x84, 0,0,0,0 }; // test eax, eax, jz
  8. static const unsigned char GLUE_JMP_IF_P1_NZ[] = {0x85, 0xC0, 0x0F, 0x85, 0,0,0,0 }; // test eax, eax, jnz
  9. #define GLUE_FUNC_ENTER_SIZE 0
  10. #define GLUE_FUNC_LEAVE_SIZE 0
  11. const static unsigned int GLUE_FUNC_ENTER[1];
  12. const static unsigned int GLUE_FUNC_LEAVE[1];
  13. // on x86-64:
  14. // stack is always 16 byte aligned
  15. // pushing values to the stack (for eel functions) has alignment pushed first, then value (value is at the lower address)
  16. // pushing pointers to the stack has the pointer pushed first, then the alignment (pointer is at the higher address)
  17. #define GLUE_MOV_PX_DIRECTVALUE_SIZE 10
  18. static void GLUE_MOV_PX_DIRECTVALUE_GEN(void *b, INT_PTR v, int wr) {
  19. const static unsigned short tab[3] =
  20. {
  21. 0xB848 /* mov rax, dv*/,
  22. 0xBF48 /* mov rdi, dv */ ,
  23. 0xB948 /* mov rcx, dv */
  24. };
  25. unsigned short *bb = (unsigned short *)b;
  26. *bb++ = tab[wr]; // mov rax, directvalue
  27. *(INT_PTR *)bb = v;
  28. }
  29. const static unsigned char GLUE_PUSH_P1[2]={ 0x50,0x50}; // push rax (pointer); push rax (alignment)
  30. #define GLUE_POP_PX_SIZE 2
  31. static void GLUE_POP_PX(void *b, int wv)
  32. {
  33. static const unsigned char tab[3][GLUE_POP_PX_SIZE]=
  34. {
  35. {0x58,/*pop eax*/ 0x58}, // pop alignment, then pop pointer
  36. {0x5F,/*pop edi*/ 0x5F},
  37. {0x59,/*pop ecx*/ 0x59},
  38. };
  39. memcpy(b,tab[wv],GLUE_POP_PX_SIZE);
  40. }
  41. static const unsigned char GLUE_PUSH_P1PTR_AS_VALUE[] =
  42. {
  43. 0x50, /*push rax - for alignment */
  44. 0xff, 0x30, /* push qword [rax] */
  45. };
  46. static int GLUE_POP_VALUE_TO_ADDR(unsigned char *buf, void *destptr) // trashes P2 (rdi) and P3 (rcx)
  47. {
  48. if (buf)
  49. {
  50. *buf++ = 0x48; *buf++ = 0xB9; *(void **) buf = destptr; buf+=8; // mov rcx, directvalue
  51. *buf++ = 0x8f; *buf++ = 0x01; // pop qword [rcx]
  52. *buf++ = 0x5F ; // pop rdi (alignment, safe to trash rdi though)
  53. }
  54. return 1+10+2;
  55. }
  56. static int GLUE_COPY_VALUE_AT_P1_TO_PTR(unsigned char *buf, void *destptr) // trashes P2/P3
  57. {
  58. if (buf)
  59. {
  60. *buf++ = 0x48; *buf++ = 0xB9; *(void **) buf = destptr; buf+=8; // mov rcx, directvalue
  61. *buf++ = 0x48; *buf++ = 0x8B; *buf++ = 0x38; // mov rdi, [rax]
  62. *buf++ = 0x48; *buf++ = 0x89; *buf++ = 0x39; // mov [rcx], rdi
  63. }
  64. return 3 + 10 + 3;
  65. }
  66. static int GLUE_POP_FPSTACK_TO_PTR(unsigned char *buf, void *destptr)
  67. {
  68. if (buf)
  69. {
  70. *buf++ = 0x48;
  71. *buf++ = 0xB8;
  72. *(void **) buf = destptr; buf+=8; // mov rax, directvalue
  73. *buf++ = 0xDD; *buf++ = 0x18; // fstp qword [rax]
  74. }
  75. return 2+8+2;
  76. }
  77. #define GLUE_SET_PX_FROM_P1_SIZE 3
  78. static void GLUE_SET_PX_FROM_P1(void *b, int wv)
  79. {
  80. static const unsigned char tab[3][GLUE_SET_PX_FROM_P1_SIZE]={
  81. {0x90,0x90,0x90}, // should never be used! (nopnop)
  82. {0x48,0x89,0xC7}, // mov rdi, rax
  83. {0x48,0x89,0xC1}, // mov rcx, rax
  84. };
  85. memcpy(b,tab[wv],GLUE_SET_PX_FROM_P1_SIZE);
  86. }
  87. #define GLUE_POP_FPSTACK_SIZE 2
  88. static const unsigned char GLUE_POP_FPSTACK[2] = { 0xDD, 0xD8 }; // fstp st0
  89. static const unsigned char GLUE_POP_FPSTACK_TOSTACK[] = {
  90. 0x48, 0x81, 0xEC, 16, 0,0,0, // sub rsp, 16
  91. 0xDD, 0x1C, 0x24 // fstp qword (%rsp)
  92. };
  93. static const unsigned char GLUE_POP_FPSTACK_TO_WTP[] = {
  94. 0xDD, 0x1E, /* fstp qword [rsi] */
  95. 0x48, 0x81, 0xC6, 8, 0,0,0,/* add rsi, 8 */
  96. };
  97. #define GLUE_SET_PX_FROM_WTP_SIZE 3
  98. static void GLUE_SET_PX_FROM_WTP(void *b, int wv)
  99. {
  100. static const unsigned char tab[3][GLUE_SET_PX_FROM_WTP_SIZE]={
  101. {0x48, 0x89,0xF0}, // mov rax, rsi
  102. {0x48, 0x89,0xF7}, // mov rdi, rsi
  103. {0x48, 0x89,0xF1}, // mov rcx, rsi
  104. };
  105. memcpy(b,tab[wv],GLUE_SET_PX_FROM_WTP_SIZE);
  106. }
  107. #define GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE 2
  108. static void GLUE_PUSH_VAL_AT_PX_TO_FPSTACK(void *b, int wv)
  109. {
  110. static const unsigned char tab[3][GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE]={
  111. {0xDD,0x00}, // fld qword [rax]
  112. {0xDD,0x07}, // fld qword [rdi]
  113. {0xDD,0x01}, // fld qword [rcx]
  114. };
  115. memcpy(b,tab[wv],GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE);
  116. }
  117. static unsigned char GLUE_POP_STACK_TO_FPSTACK[] = {
  118. 0xDD, 0x04, 0x24, // fld qword (%rsp)
  119. 0x48, 0x81, 0xC4, 16, 0,0,0, // add rsp, 16
  120. };
  121. #define GLUE_POP_FPSTACK_TO_WTP_TO_PX_SIZE (GLUE_SET_PX_FROM_WTP_SIZE + sizeof(GLUE_POP_FPSTACK_TO_WTP))
  122. static void GLUE_POP_FPSTACK_TO_WTP_TO_PX(unsigned char *buf, int wv)
  123. {
  124. GLUE_SET_PX_FROM_WTP(buf,wv);
  125. memcpy(buf + GLUE_SET_PX_FROM_WTP_SIZE,GLUE_POP_FPSTACK_TO_WTP,sizeof(GLUE_POP_FPSTACK_TO_WTP));
  126. };
  127. const static unsigned char GLUE_RET=0xC3;
  128. static int GLUE_RESET_WTP(unsigned char *out, void *ptr)
  129. {
  130. if (out)
  131. {
  132. *out++ = 0x48;
  133. *out++ = 0xBE; // mov rsi, constant64
  134. *(void **)out = ptr;
  135. out+=sizeof(void *);
  136. }
  137. return 2+sizeof(void *);
  138. }
  139. extern void win64_callcode(INT_PTR code, INT_PTR ram_tab);
  140. #define GLUE_CALL_CODE(bp, cp, rt) win64_callcode(cp, rt)
  141. static unsigned char *EEL_GLUE_set_immediate(void *_p, INT_PTR newv)
  142. {
  143. char *p=(char*)_p;
  144. INT_PTR scan = 0xFEFEFEFEFEFEFEFE;
  145. while (*(INT_PTR *)p != scan) p++;
  146. *(INT_PTR *)p = newv;
  147. return (unsigned char *) (((INT_PTR*)p)+1);
  148. }
  149. #define INT_TO_LECHARS(x) ((x)&0xff),(((x)>>8)&0xff), (((x)>>16)&0xff), (((x)>>24)&0xff)
  150. #define GLUE_INLINE_LOOPS
  151. static const unsigned char GLUE_LOOP_LOADCNT[]={
  152. 0xDD, 0x0E, //fistTp qword [rsi]
  153. 0x48, 0x8B, 0x0E, // mov rcx, [rsi]
  154. 0x48, 0x81, 0xf9, 1,0,0,0, // cmp rcx, 1
  155. 0x0F, 0x8C, 0,0,0,0, // JL <skipptr>
  156. };
  157. static const unsigned char GLUE_LOOP_CLAMPCNT[]={
  158. 0x48, 0x81, 0xf9, INT_TO_LECHARS(NSEEL_LOOPFUNC_SUPPORT_MAXLEN), // cmp rcx, NSEEL_LOOPFUNC_SUPPORT_MAXLEN
  159. 0x0F, 0x8C, 10,0,0,0, // JL over-the-mov
  160. 0x48, 0xB9, INT_TO_LECHARS(NSEEL_LOOPFUNC_SUPPORT_MAXLEN), 0,0,0,0, // mov rcx, NSEEL_LOOPFUNC_SUPPORT_MAXLEN
  161. };
  162. static const unsigned char GLUE_LOOP_BEGIN[]={
  163. 0x56, //push rsi
  164. 0x51, // push rcx
  165. };
  166. static const unsigned char GLUE_LOOP_END[]={
  167. 0x59, //pop rcx
  168. 0x5E, // pop rsi
  169. 0xff, 0xc9, // dec rcx
  170. 0x0f, 0x85, 0,0,0,0, // jnz ...
  171. };
  172. static const unsigned char GLUE_WHILE_SETUP[]={
  173. 0x48, 0xB9, INT_TO_LECHARS(NSEEL_LOOPFUNC_SUPPORT_MAXLEN), 0,0,0,0, // mov rcx, NSEEL_LOOPFUNC_SUPPORT_MAXLEN
  174. };
  175. static const unsigned char GLUE_WHILE_BEGIN[]={
  176. 0x56, //push rsi
  177. 0x51, // push rcx
  178. };
  179. static const unsigned char GLUE_WHILE_END[]={
  180. 0x59, //pop rcx
  181. 0x5E, // pop rsi
  182. 0xff, 0xc9, // dec rcx
  183. 0x0f, 0x84, 0,0,0,0, // jz endpt
  184. };
  185. static const unsigned char GLUE_WHILE_CHECK_RV[] = {
  186. 0x85, 0xC0, // test eax, eax
  187. 0x0F, 0x85, 0,0,0,0 // jnz looppt
  188. };
  189. static const unsigned char GLUE_SET_P1_Z[] = { 0x48, 0x29, 0xC0 }; // sub rax, rax
  190. static const unsigned char GLUE_SET_P1_NZ[] = { 0xb0, 0x01 }; // mov al, 1
  191. #define GLUE_HAS_FXCH
  192. static const unsigned char GLUE_FXCH[] = {0xd9, 0xc9};
  193. #define GLUE_HAS_FLDZ
  194. static const unsigned char GLUE_FLDZ[] = {0xd9, 0xee};
  195. #define GLUE_HAS_FLD1
  196. static const unsigned char GLUE_FLD1[] = {0xd9, 0xe8};
  197. static EEL_F negativezeropointfive=-0.5f;
  198. static EEL_F onepointfive=1.5f;
  199. #define GLUE_INVSQRT_NEEDREPL &negativezeropointfive, &onepointfive,
  200. #define GLUE_HAS_NATIVE_TRIGSQRTLOG
  201. static void *GLUE_realAddress(void *fn, void *fn_e, int *size)
  202. {
  203. static const unsigned char sig[12] = { 0x89, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 };
  204. unsigned char *p = (unsigned char *)fn;
  205. while (memcmp(p,sig,sizeof(sig))) p++;
  206. p+=sizeof(sig);
  207. fn = p;
  208. while (memcmp(p,sig,sizeof(sig))) p++;
  209. *size = p - (unsigned char *)fn;
  210. return fn;
  211. }
  212. // end of x86-64
  213. #endif