relay.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * Relay calls helper routines
  3. *
  4. * Copyright 1993 Robert J. Amstadt
  5. * Copyright 1995 Martin von Loewis
  6. * Copyright 1995, 1996, 1997 Alexandre Julliard
  7. * Copyright 1997 Eric Youngdale
  8. * Copyright 1999 Ulrich Weigand
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include "config.h"
  25. #include "wine/port.h"
  26. #include <ctype.h>
  27. #include <stdarg.h>
  28. #include "build.h"
  29. /* offset of the stack pointer relative to %fs:(0) */
  30. #define STACKOFFSET 0xc0 /* FIELD_OFFSET(TEB,WOW32Reserved) */
  31. /* fix this if the x86_thread_data structure is changed */
  32. #define GS_OFFSET 0x1d8 /* FIELD_OFFSET(TEB,SystemReserved2) + FIELD_OFFSET(struct x86_thread_data,gs) */
  33. static void function_header( const char *name )
  34. {
  35. output( "\n\t.align %d\n", get_alignment(4) );
  36. output( "\t%s\n", func_declaration(name) );
  37. output( "%s\n", asm_globl(name) );
  38. }
  39. /*******************************************************************
  40. * BuildCallFrom16Core
  41. *
  42. * This routine builds the core routines used in 16->32 thunks:
  43. * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
  44. *
  45. * These routines are intended to be called via a far call (with 32-bit
  46. * operand size) from 16-bit code. The 16-bit code stub must push %bp,
  47. * the 32-bit entry point to be called, and the argument conversion
  48. * routine to be used (see stack layout below).
  49. *
  50. * The core routine completes the STACK16FRAME on the 16-bit stack and
  51. * switches to the 32-bit stack. Then, the argument conversion routine
  52. * is called; it gets passed the 32-bit entry point and a pointer to the
  53. * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
  54. * use conversion routines automatically generated by BuildCallFrom16,
  55. * or write your own for special purposes.)
  56. *
  57. * The conversion routine must call the 32-bit entry point, passing it
  58. * the converted arguments, and return its return value to the core.
  59. * After the conversion routine has returned, the core switches back
  60. * to the 16-bit stack, converts the return value to the DX:AX format
  61. * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
  62. * including %bp, are popped off the stack.
  63. *
  64. * The 16-bit call stub now returns to the caller, popping the 16-bit
  65. * arguments if necessary (pascal calling convention).
  66. *
  67. * In the case of a 'register' function, CallFrom16Register fills a
  68. * CONTEXT86 structure with the values all registers had at the point
  69. * the first instruction of the 16-bit call stub was about to be
  70. * executed. A pointer to this CONTEXT86 is passed as third parameter
  71. * to the argument conversion routine, which typically passes it on
  72. * to the called 32-bit entry point.
  73. *
  74. * CallFrom16Thunk is a special variant used by the implementation of
  75. * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
  76. * implemented as follows:
  77. * On entry, the EBX register is set up to contain a flat pointer to the
  78. * 16-bit stack such that EBX+22 points to the first argument.
  79. * Then, the entry point is called, while EBP is set up to point
  80. * to the return address (on the 32-bit stack).
  81. * The called function returns with CX set to the number of bytes
  82. * to be popped of the caller's stack.
  83. *
  84. * Stack layout upon entry to the core routine (STACK16FRAME):
  85. * ... ...
  86. * (sp+24) word first 16-bit arg
  87. * (sp+22) word cs
  88. * (sp+20) word ip
  89. * (sp+18) word bp
  90. * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
  91. * (sp+12) word ip of actual entry point (necessary for relay debugging)
  92. * (sp+8) long relay (argument conversion) function entry point
  93. * (sp+4) long cs of 16-bit entry point
  94. * (sp) long ip of 16-bit entry point
  95. *
  96. * Added on the stack:
  97. * (sp-2) word saved gs
  98. * (sp-4) word saved fs
  99. * (sp-6) word saved es
  100. * (sp-8) word saved ds
  101. * (sp-12) long saved ebp
  102. * (sp-16) long saved ecx
  103. * (sp-20) long saved edx
  104. * (sp-24) long saved previous stack
  105. */
  106. static void BuildCallFrom16Core( int reg_func, int thunk )
  107. {
  108. /* Function header */
  109. if (thunk) function_header( "__wine_call_from_16_thunk" );
  110. else if (reg_func) function_header( "__wine_call_from_16_regs" );
  111. else function_header( "__wine_call_from_16" );
  112. /* Create STACK16FRAME (except STACK32FRAME link) */
  113. output( "\tpushw %%gs\n" );
  114. output( "\tpushw %%fs\n" );
  115. output( "\tpushw %%es\n" );
  116. output( "\tpushw %%ds\n" );
  117. output( "\tpushl %%ebp\n" );
  118. output( "\tpushl %%ecx\n" );
  119. output( "\tpushl %%edx\n" );
  120. /* Save original EFlags register */
  121. if (reg_func) output( "\tpushfl\n" );
  122. if ( UsePIC )
  123. {
  124. output( "\tcall 1f\n" );
  125. output( "1:\tpopl %%ecx\n" );
  126. output( "\t.byte 0x2e\n\tmovl %s-1b(%%ecx),%%edx\n", asm_name("CallTo16_DataSelector") );
  127. }
  128. else
  129. output( "\t.byte 0x2e\n\tmovl %s,%%edx\n", asm_name("CallTo16_DataSelector") );
  130. /* Load 32-bit segment registers */
  131. output( "\tmovw %%dx, %%ds\n" );
  132. output( "\tmovw %%dx, %%es\n" );
  133. if ( UsePIC )
  134. output( "\tmovw %s-1b(%%ecx), %%fs\n", asm_name("CallTo16_TebSelector") );
  135. else
  136. output( "\tmovw %s, %%fs\n", asm_name("CallTo16_TebSelector") );
  137. output( "\t.byte 0x64\n\tmov (%d),%%gs\n", GS_OFFSET );
  138. /* Translate STACK16FRAME base to flat offset in %edx */
  139. output( "\tmovw %%ss, %%dx\n" );
  140. output( "\tandl $0xfff8, %%edx\n" );
  141. output( "\tshrl $1, %%edx\n" );
  142. if (UsePIC)
  143. output( "\taddl .Lwine_ldt_copy_ptr-1b(%%ecx),%%edx\n" );
  144. else
  145. output( "\taddl .Lwine_ldt_copy_ptr,%%edx\n" );
  146. output( "\tmovl (%%edx), %%edx\n" );
  147. output( "\tmovzwl %%sp, %%ebp\n" );
  148. output( "\tleal %d(%%ebp,%%edx), %%edx\n", reg_func ? 0 : -4 );
  149. /* Get saved flags into %ecx */
  150. if (reg_func) output( "\tpopl %%ecx\n" );
  151. /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
  152. output( "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
  153. output( "\tpushl %%ebp\n" );
  154. /* Switch stacks */
  155. output( "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
  156. output( "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
  157. output( "\tpushl %%ds\n" );
  158. output( "\tpopl %%ss\n" );
  159. output( "\tmovl %%ebp, %%esp\n" );
  160. output( "\taddl $0x20,%%ebp\n"); /* FIELD_OFFSET(STACK32FRAME,ebp) */
  161. /* At this point:
  162. STACK16FRAME is completely set up
  163. DS, ES, SS: flat data segment
  164. FS: current TEB
  165. ESP: points to last STACK32FRAME
  166. EBP: points to ebp member of last STACK32FRAME
  167. EDX: points to current STACK16FRAME
  168. ECX: contains saved flags
  169. all other registers: unchanged */
  170. /* Special case: C16ThkSL stub */
  171. if ( thunk )
  172. {
  173. /* Set up registers as expected and call thunk */
  174. output( "\tleal 0x1a(%%edx),%%ebx\n" ); /* sizeof(STACK16FRAME)-22 */
  175. output( "\tleal -4(%%esp), %%ebp\n" );
  176. output( "\tcall *0x26(%%edx)\n"); /* FIELD_OFFSET(STACK16FRAME,entry_point) */
  177. /* Switch stack back */
  178. output( "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
  179. output( "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
  180. output( "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
  181. /* Restore registers and return directly to caller */
  182. output( "\taddl $8, %%esp\n" );
  183. output( "\tpopl %%ebp\n" );
  184. output( "\tpopw %%ds\n" );
  185. output( "\tpopw %%es\n" );
  186. output( "\tpopw %%fs\n" );
  187. output( "\tpopw %%gs\n" );
  188. output( "\taddl $20, %%esp\n" );
  189. output( "\txorb %%ch, %%ch\n" );
  190. output( "\tpopl %%ebx\n" );
  191. output( "\taddw %%cx, %%sp\n" );
  192. output( "\tpush %%ebx\n" );
  193. output( "\t.byte 0x66\n" );
  194. output( "\tlret\n" );
  195. output_function_size( "__wine_call_from_16_thunk" );
  196. return;
  197. }
  198. /* Build register CONTEXT */
  199. if ( reg_func )
  200. {
  201. output( "\tsubl $0x2cc,%%esp\n" ); /* sizeof(CONTEXT86) */
  202. output( "\tmovl %%ecx,0xc0(%%esp)\n" ); /* EFlags */
  203. output( "\tmovl %%eax,0xb0(%%esp)\n" ); /* Eax */
  204. output( "\tmovl %%ebx,0xa4(%%esp)\n" ); /* Ebx */
  205. output( "\tmovl %%esi,0xa0(%%esp)\n" ); /* Esi */
  206. output( "\tmovl %%edi,0x9c(%%esp)\n" ); /* Edi */
  207. output( "\tmovl 0x0c(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ebp) */
  208. output( "\tmovl %%eax,0xb4(%%esp)\n" ); /* Ebp */
  209. output( "\tmovl 0x08(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ecx) */
  210. output( "\tmovl %%eax,0xac(%%esp)\n" ); /* Ecx */
  211. output( "\tmovl 0x04(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,edx) */
  212. output( "\tmovl %%eax,0xa8(%%esp)\n" ); /* Edx */
  213. output( "\tmovzwl 0x10(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ds) */
  214. output( "\tmovl %%eax,0x98(%%esp)\n" ); /* SegDs */
  215. output( "\tmovzwl 0x12(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,es) */
  216. output( "\tmovl %%eax,0x94(%%esp)\n" ); /* SegEs */
  217. output( "\tmovzwl 0x14(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,fs) */
  218. output( "\tmovl %%eax,0x90(%%esp)\n" ); /* SegFs */
  219. output( "\tmovzwl 0x16(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,gs) */
  220. output( "\tmovl %%eax,0x8c(%%esp)\n" ); /* SegGs */
  221. output( "\tmovzwl 0x2e(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,cs) */
  222. output( "\tmovl %%eax,0xbc(%%esp)\n" ); /* SegCs */
  223. output( "\tmovzwl 0x2c(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ip) */
  224. output( "\tmovl %%eax,0xb8(%%esp)\n" ); /* Eip */
  225. output( "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
  226. output( "\tmovl %%eax,0xc8(%%esp)\n" ); /* SegSs */
  227. output( "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
  228. output( "\taddl $0x2c,%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ip) */
  229. output( "\tmovl %%eax,0xc4(%%esp)\n" ); /* Esp */
  230. #if 0
  231. output( "\tfsave 0x1c(%%esp)\n" ); /* FloatSave */
  232. #endif
  233. /* Push address of CONTEXT86 structure -- popped by the relay routine */
  234. output( "\tmovl %%esp,%%eax\n" );
  235. output( "\tandl $~15,%%esp\n" );
  236. output( "\tsubl $4,%%esp\n" );
  237. output( "\tpushl %%eax\n" );
  238. }
  239. else
  240. {
  241. output( "\tsubl $8,%%esp\n" );
  242. output( "\tandl $~15,%%esp\n" );
  243. output( "\taddl $8,%%esp\n" );
  244. }
  245. /* Call relay routine (which will call the API entry point) */
  246. output( "\tleal 0x30(%%edx),%%eax\n" ); /* sizeof(STACK16FRAME) */
  247. output( "\tpushl %%eax\n" );
  248. output( "\tpushl 0x26(%%edx)\n"); /* FIELD_OFFSET(STACK16FRAME,entry_point) */
  249. output( "\tcall *0x20(%%edx)\n"); /* FIELD_OFFSET(STACK16FRAME,relay) */
  250. if ( reg_func )
  251. {
  252. output( "\tleal -748(%%ebp),%%ebx\n" ); /* sizeof(CONTEXT) + FIELD_OFFSET(STACK32FRAME,ebp) */
  253. /* Switch stack back */
  254. output( "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
  255. output( "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
  256. output( "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
  257. /* Get return address to CallFrom16 stub */
  258. output( "\taddw $0x14,%%sp\n" ); /* FIELD_OFFSET(STACK16FRAME,callfrom_ip)-4 */
  259. output( "\tpopl %%eax\n" );
  260. output( "\tpopl %%edx\n" );
  261. /* Restore all registers from CONTEXT */
  262. output( "\tmovw 0xc8(%%ebx),%%ss\n"); /* SegSs */
  263. output( "\tmovl 0xc4(%%ebx),%%esp\n"); /* Esp */
  264. output( "\taddl $4, %%esp\n" ); /* room for final return address */
  265. output( "\tpushw 0xbc(%%ebx)\n"); /* SegCs */
  266. output( "\tpushw 0xb8(%%ebx)\n"); /* Eip */
  267. output( "\tpushl %%edx\n" );
  268. output( "\tpushl %%eax\n" );
  269. output( "\tpushl 0xc0(%%ebx)\n"); /* EFlags */
  270. output( "\tpushl 0x98(%%ebx)\n"); /* SegDs */
  271. output( "\tpushl 0x94(%%ebx)\n"); /* SegEs */
  272. output( "\tpopl %%es\n" );
  273. output( "\tpushl 0x90(%%ebx)\n"); /* SegFs */
  274. output( "\tpopl %%fs\n" );
  275. output( "\tpushl 0x8c(%%ebx)\n"); /* SegGs */
  276. output( "\tpopl %%gs\n" );
  277. output( "\tmovl 0xb4(%%ebx),%%ebp\n"); /* Ebp */
  278. output( "\tmovl 0xa0(%%ebx),%%esi\n"); /* Esi */
  279. output( "\tmovl 0x9c(%%ebx),%%edi\n"); /* Edi */
  280. output( "\tmovl 0xb0(%%ebx),%%eax\n"); /* Eax */
  281. output( "\tmovl 0xa8(%%ebx),%%edx\n"); /* Edx */
  282. output( "\tmovl 0xac(%%ebx),%%ecx\n"); /* Ecx */
  283. output( "\tmovl 0xa4(%%ebx),%%ebx\n"); /* Ebx */
  284. output( "\tpopl %%ds\n" );
  285. output( "\tpopfl\n" );
  286. output( "\tlret\n" );
  287. output_function_size( "__wine_call_from_16_regs" );
  288. }
  289. else
  290. {
  291. /* Switch stack back */
  292. output( "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
  293. output( "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
  294. output( "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
  295. /* Restore registers */
  296. output( "\tpopl %%edx\n" );
  297. output( "\tpopl %%ecx\n" );
  298. output( "\tpopl %%ebp\n" );
  299. output( "\tpopw %%ds\n" );
  300. output( "\tpopw %%es\n" );
  301. output( "\tpopw %%fs\n" );
  302. output( "\tpopw %%gs\n" );
  303. /* Return to return stub which will return to caller */
  304. output( "\tlret $12\n" );
  305. output_function_size( "__wine_call_from_16" );
  306. }
  307. }
  308. /*******************************************************************
  309. * BuildCallTo16Core
  310. *
  311. * This routine builds the core routines used in 32->16 thunks:
  312. *
  313. * extern DWORD WINAPI wine_call_to_16( FARPROC16 target, DWORD cbArgs, PEXCEPTION_HANDLER handler );
  314. * extern void WINAPI wine_call_to_16_regs( CONTEXT86 *context, DWORD cbArgs, PEXCEPTION_HANDLER handler );
  315. *
  316. * These routines can be called directly from 32-bit code.
  317. *
  318. * All routines expect that the 16-bit stack contents (arguments) and the
  319. * return address (segptr to CallTo16_Ret) were already set up by the
  320. * caller; nb_args must contain the number of bytes to be conserved. The
  321. * 16-bit SS:SP will be set accordingly.
  322. *
  323. * All other registers are either taken from the CONTEXT86 structure
  324. * or else set to default values. The target routine address is either
  325. * given directly or taken from the CONTEXT86.
  326. */
  327. static void BuildCallTo16Core( int reg_func )
  328. {
  329. const char *name = reg_func ? "wine_call_to_16_regs" : "wine_call_to_16";
  330. const char *func_name = (target_platform == PLATFORM_WINDOWS ? strmake( "%s@12", name ) : name);
  331. /* Function header */
  332. function_header( func_name );
  333. /* Function entry sequence */
  334. output_cfi( ".cfi_startproc" );
  335. output( "\tpushl %%ebp\n" );
  336. output_cfi( ".cfi_adjust_cfa_offset 4" );
  337. output_cfi( ".cfi_rel_offset %%ebp,0" );
  338. output( "\tmovl %%esp, %%ebp\n" );
  339. output_cfi( ".cfi_def_cfa_register %%ebp" );
  340. /* Save the 32-bit registers */
  341. output( "\tpushl %%ebx\n" );
  342. output_cfi( ".cfi_rel_offset %%ebx,-4" );
  343. output( "\tpushl %%esi\n" );
  344. output_cfi( ".cfi_rel_offset %%esi,-8" );
  345. output( "\tpushl %%edi\n" );
  346. output_cfi( ".cfi_rel_offset %%edi,-12" );
  347. output( "\t.byte 0x64\n\tmov %%gs,(%d)\n", GS_OFFSET );
  348. /* Setup exception frame */
  349. output( "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
  350. output( "\tpushl 16(%%ebp)\n" ); /* handler */
  351. output( "\t.byte 0x64\n\tpushl (0)\n" );
  352. output( "\t.byte 0x64\n\tmovl %%esp,(0)\n" );
  353. /* Call the actual CallTo16 routine (simulate a lcall) */
  354. output( "\tpushl %%cs\n" );
  355. output( "\tcall .L%s\n", name );
  356. /* Remove exception frame */
  357. output( "\t.byte 0x64\n\tpopl (0)\n" );
  358. output( "\taddl $4, %%esp\n" );
  359. output( "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
  360. if ( !reg_func )
  361. {
  362. /* Convert return value */
  363. output( "\tandl $0xffff,%%eax\n" );
  364. output( "\tshll $16,%%edx\n" );
  365. output( "\torl %%edx,%%eax\n" );
  366. }
  367. else
  368. {
  369. /*
  370. * Modify CONTEXT86 structure to contain new values
  371. *
  372. * NOTE: We restore only EAX, EBX, EDX, EDX, EBP, and ESP.
  373. * The segment registers as well as ESI and EDI should
  374. * not be modified by a well-behaved 16-bit routine in
  375. * any case. [If necessary, we could restore them as well,
  376. * at the cost of a somewhat less efficient return path.]
  377. */
  378. output( "\tmovl 0x14(%%esp),%%edi\n" ); /* FIELD_OFFSET(STACK32FRAME,target) - FIELD_OFFSET(STACK32FRAME,edi) */
  379. /* everything above edi has been popped already */
  380. output( "\tmovl %%eax,0xb0(%%edi)\n"); /* Eax */
  381. output( "\tmovl %%ebx,0xa4(%%edi)\n"); /* Ebx */
  382. output( "\tmovl %%ecx,0xac(%%edi)\n"); /* Ecx */
  383. output( "\tmovl %%edx,0xa8(%%edi)\n"); /* Edx */
  384. output( "\tmovl %%ebp,0xb4(%%edi)\n"); /* Ebp */
  385. output( "\tmovl %%esi,0xc4(%%edi)\n"); /* Esp */
  386. /* The return glue code saved %esp into %esi */
  387. }
  388. /* Restore the 32-bit registers */
  389. output( "\tpopl %%edi\n" );
  390. output_cfi( ".cfi_same_value %%edi" );
  391. output( "\tpopl %%esi\n" );
  392. output_cfi( ".cfi_same_value %%esi" );
  393. output( "\tpopl %%ebx\n" );
  394. output_cfi( ".cfi_same_value %%ebx" );
  395. /* Function exit sequence */
  396. output( "\tpopl %%ebp\n" );
  397. output_cfi( ".cfi_def_cfa %%esp,4" );
  398. output_cfi( ".cfi_same_value %%ebp" );
  399. output( "\tret $12\n" );
  400. output_cfi( ".cfi_endproc" );
  401. /* Start of the actual CallTo16 routine */
  402. output( ".L%s:\n", name );
  403. /* Switch to the 16-bit stack */
  404. output( "\tmovl %%esp,%%edx\n" );
  405. output( "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
  406. output( "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
  407. output( "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
  408. /* Make %bp point to the previous stackframe (built by CallFrom16) */
  409. output( "\tmovzwl %%sp,%%ebp\n" );
  410. output( "\tleal 0x2a(%%ebp),%%ebp\n"); /* FIELD_OFFSET(STACK16FRAME,bp) */
  411. /* Add the specified offset to the new sp */
  412. output( "\tsubw 0x2c(%%edx), %%sp\n"); /* FIELD_OFFSET(STACK32FRAME,nb_args) */
  413. if (reg_func)
  414. {
  415. /* Push the called routine address */
  416. output( "\tmovl 0x28(%%edx),%%edx\n"); /* FIELD_OFFSET(STACK32FRAME,target) */
  417. output( "\tpushw 0xbc(%%edx)\n"); /* SegCs */
  418. output( "\tpushw 0xb8(%%edx)\n"); /* Eip */
  419. /* Get the registers */
  420. output( "\tpushw 0x98(%%edx)\n"); /* SegDs */
  421. output( "\tpushl 0x94(%%edx)\n"); /* SegEs */
  422. output( "\tpopl %%es\n" );
  423. output( "\tmovl 0xb4(%%edx),%%ebp\n"); /* Ebp */
  424. output( "\tmovl 0xa0(%%edx),%%esi\n"); /* Esi */
  425. output( "\tmovl 0x9c(%%edx),%%edi\n"); /* Edi */
  426. output( "\tmovl 0xb0(%%edx),%%eax\n"); /* Eax */
  427. output( "\tmovl 0xa4(%%edx),%%ebx\n"); /* Ebx */
  428. output( "\tmovl 0xac(%%edx),%%ecx\n"); /* Ecx */
  429. output( "\tmovl 0xa8(%%edx),%%edx\n"); /* Edx */
  430. /* Get the 16-bit ds */
  431. output( "\tpopw %%ds\n" );
  432. }
  433. else /* not a register function */
  434. {
  435. /* Push the called routine address */
  436. output( "\tpushl 0x28(%%edx)\n"); /* FIELD_OFFSET(STACK32FRAME,target) */
  437. /* Set %fs and %gs to the value saved by the last CallFrom16 */
  438. output( "\tpushw -22(%%ebp)\n" ); /* FIELD_OFFSET(STACK16FRAME,fs)-FIELD_OFFSET(STACK16FRAME,bp) */
  439. output( "\tpopw %%fs\n" );
  440. output( "\tpushw -20(%%ebp)\n" ); /* FIELD_OFFSET(STACK16FRAME,gs)-FIELD_OFFSET(STACK16FRAME,bp) */
  441. output( "\tpopw %%gs\n" );
  442. /* Set %ds and %es (and %ax just in case) equal to %ss */
  443. output( "\tmovw %%ss,%%ax\n" );
  444. output( "\tmovw %%ax,%%ds\n" );
  445. output( "\tmovw %%ax,%%es\n" );
  446. }
  447. /* Jump to the called routine */
  448. output( "\t.byte 0x66\n" );
  449. output( "\tlret\n" );
  450. /* Function footer */
  451. output_function_size( func_name );
  452. }
  453. /*******************************************************************
  454. * BuildRet16Func
  455. *
  456. * Build the return code for 16-bit callbacks
  457. */
  458. static void BuildRet16Func(void)
  459. {
  460. function_header( "__wine_call_to_16_ret" );
  461. /* Save %esp into %esi */
  462. output( "\tmovl %%esp,%%esi\n" );
  463. /* Restore 32-bit segment registers */
  464. output( "\t.byte 0x2e\n\tmovl %s", asm_name("CallTo16_DataSelector") );
  465. output( "-%s,%%edi\n", asm_name("__wine_call16_start") );
  466. output( "\tmovw %%di,%%ds\n" );
  467. output( "\tmovw %%di,%%es\n" );
  468. output( "\t.byte 0x2e\n\tmov %s", asm_name("CallTo16_TebSelector") );
  469. output( "-%s,%%fs\n", asm_name("__wine_call16_start") );
  470. output( "\t.byte 0x64\n\tmov (%d),%%gs\n", GS_OFFSET );
  471. /* Restore the 32-bit stack */
  472. output( "\tmovw %%di,%%ss\n" );
  473. output( "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
  474. /* Return to caller */
  475. output( "\tlret\n" );
  476. output_function_size( "__wine_call_to_16_ret" );
  477. }
  478. /*******************************************************************
  479. * BuildCallTo32CBClient
  480. *
  481. * Call a CBClient relay stub from 32-bit code (KERNEL.620).
  482. *
  483. * Since the relay stub is itself 32-bit, this should not be a problem;
  484. * unfortunately, the relay stubs are expected to switch back to a
  485. * 16-bit stack (and 16-bit code) after completion :-(
  486. *
  487. * This would conflict with our 16- vs. 32-bit stack handling, so
  488. * we simply switch *back* to our 32-bit stack before returning to
  489. * the caller ...
  490. *
  491. * The CBClient relay stub expects to be called with the following
  492. * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
  493. * stack at the designated places:
  494. *
  495. * ...
  496. * (ebp+14) original arguments to the callback routine
  497. * (ebp+10) far return address to original caller
  498. * (ebp+6) Thunklet target address
  499. * (ebp+2) Thunklet relay ID code
  500. * (ebp) BP (saved by CBClientGlueSL)
  501. * (ebp-2) SI (saved by CBClientGlueSL)
  502. * (ebp-4) DI (saved by CBClientGlueSL)
  503. * (ebp-6) DS (saved by CBClientGlueSL)
  504. *
  505. * ... buffer space used by the 16-bit side glue for temp copies
  506. *
  507. * (ebx+4) far return address to 16-bit side glue code
  508. * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
  509. *
  510. * The 32-bit side glue code accesses both the original arguments (via ebp)
  511. * and the temporary copies prepared by the 16-bit side glue (via ebx).
  512. * After completion, the stub will load ss:sp from the buffer at ebx
  513. * and perform a far return to 16-bit code.
  514. *
  515. * To trick the relay stub into returning to us, we replace the 16-bit
  516. * return address to the glue code by a cs:ip pair pointing to our
  517. * return entry point (the original return address is saved first).
  518. * Our return stub thus called will then reload the 32-bit ss:esp and
  519. * return to 32-bit code (by using and ss:esp value that we have also
  520. * pushed onto the 16-bit stack before and a cs:eip values found at
  521. * that position on the 32-bit stack). The ss:esp to be restored is
  522. * found relative to the 16-bit stack pointer at:
  523. *
  524. * (ebx-4) ss (flat)
  525. * (ebx-8) sp (32-bit stack pointer)
  526. *
  527. * The second variant of this routine, CALL32_CBClientEx, which is used
  528. * to implement KERNEL.621, has to cope with yet another problem: Here,
  529. * the 32-bit side directly returns to the caller of the CBClient thunklet,
  530. * restoring registers saved by CBClientGlueSL and cleaning up the stack.
  531. * As we have to return to our 32-bit code first, we have to adapt the
  532. * layout of our temporary area so as to include values for the registers
  533. * that are to be restored, and later (in the implementation of KERNEL.621)
  534. * we *really* restore them. The return stub restores DS, DI, SI, and BP
  535. * from the stack, skips the next 8 bytes (CBClient relay code / target),
  536. * and then performs a lret NN, where NN is the number of arguments to be
  537. * removed. Thus, we prepare our temporary area as follows:
  538. *
  539. * (ebx+22) 16-bit cs (this segment)
  540. * (ebx+20) 16-bit ip ('16-bit' return entry point)
  541. * (ebx+16) 32-bit ss (flat)
  542. * (ebx+12) 32-bit sp (32-bit stack pointer)
  543. * (ebx+10) 16-bit bp (points to ebx+24)
  544. * (ebx+8) 16-bit si (ignored)
  545. * (ebx+6) 16-bit di (ignored)
  546. * (ebx+4) 16-bit ds (we actually use the flat DS here)
  547. * (ebx+2) 16-bit ss (16-bit stack segment)
  548. * (ebx+0) 16-bit sp (points to ebx+4)
  549. *
  550. * Note that we ensure that DS is not changed and remains the flat segment,
  551. * and the 32-bit stack pointer our own return stub needs fits just
  552. * perfectly into the 8 bytes that are skipped by the Windows stub.
  553. * One problem is that we have to determine the number of removed arguments,
  554. * as these have to be really removed in KERNEL.621. Thus, the BP value
  555. * that we place in the temporary area to be restored, contains the value
  556. * that SP would have if no arguments were removed. By comparing the actual
  557. * value of SP with this value in our return stub we can compute the number
  558. * of removed arguments. This is then returned to KERNEL.621.
  559. *
  560. * The stack layout of this function:
  561. * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
  562. * (ebp+16) esi pointer to caller's esi value
  563. * (ebp+12) arg ebp value to be set for relay stub
  564. * (ebp+8) func CBClient relay stub address
  565. * (ebp+4) ret addr
  566. * (ebp) ebp
  567. */
  568. static void BuildCallTo32CBClient( int isEx )
  569. {
  570. function_header( isEx ? "CALL32_CBClientEx" : "CALL32_CBClient" );
  571. /* Entry code */
  572. output_cfi( ".cfi_startproc" );
  573. output( "\tpushl %%ebp\n" );
  574. output_cfi( ".cfi_adjust_cfa_offset 4" );
  575. output_cfi( ".cfi_rel_offset %%ebp,0" );
  576. output( "\tmovl %%esp,%%ebp\n" );
  577. output_cfi( ".cfi_def_cfa_register %%ebp" );
  578. output( "\tpushl %%edi\n" );
  579. output_cfi( ".cfi_rel_offset %%edi,-4" );
  580. output( "\tpushl %%esi\n" );
  581. output_cfi( ".cfi_rel_offset %%esi,-8" );
  582. output( "\tpushl %%ebx\n" );
  583. output_cfi( ".cfi_rel_offset %%ebx,-12" );
  584. /* Get pointer to temporary area and save the 32-bit stack pointer */
  585. output( "\tmovl 16(%%ebp), %%ebx\n" );
  586. output( "\tleal -8(%%esp), %%eax\n" );
  587. if ( !isEx )
  588. output( "\tmovl %%eax, -8(%%ebx)\n" );
  589. else
  590. output( "\tmovl %%eax, 12(%%ebx)\n" );
  591. /* Set up registers and call CBClient relay stub (simulating a far call) */
  592. output( "\tmovl 20(%%ebp), %%esi\n" );
  593. output( "\tmovl (%%esi), %%esi\n" );
  594. output( "\tmovl 8(%%ebp), %%eax\n" );
  595. output( "\tmovl 12(%%ebp), %%ebp\n" );
  596. output( "\tpushl %%cs\n" );
  597. output( "\tcall *%%eax\n" );
  598. /* Return new esi value to caller */
  599. output( "\tmovl 32(%%esp), %%edi\n" );
  600. output( "\tmovl %%esi, (%%edi)\n" );
  601. /* Return argument size to caller */
  602. if ( isEx )
  603. {
  604. output( "\tmovl 36(%%esp), %%ebx\n" );
  605. output( "\tmovl %%ebp, (%%ebx)\n" );
  606. }
  607. /* Restore registers and return */
  608. output( "\tpopl %%ebx\n" );
  609. output_cfi( ".cfi_same_value %%ebx" );
  610. output( "\tpopl %%esi\n" );
  611. output_cfi( ".cfi_same_value %%esi" );
  612. output( "\tpopl %%edi\n" );
  613. output_cfi( ".cfi_same_value %%edi" );
  614. output( "\tpopl %%ebp\n" );
  615. output_cfi( ".cfi_def_cfa %%esp,4" );
  616. output_cfi( ".cfi_same_value %%ebp" );
  617. output( "\tret\n" );
  618. output_cfi( ".cfi_endproc" );
  619. output_function_size( isEx ? "CALL32_CBClientEx" : "CALL32_CBClient" );
  620. /* '16-bit' return stub */
  621. function_header( isEx ? "CALL32_CBClientEx_Ret" : "CALL32_CBClient_Ret" );
  622. if ( !isEx )
  623. {
  624. output( "\tmovzwl %%sp, %%ebx\n" );
  625. output( "\tlssl %%ss:-16(%%ebx), %%esp\n" );
  626. }
  627. else
  628. {
  629. output( "\tmovzwl %%bp, %%ebx\n" );
  630. output( "\tsubw %%bp, %%sp\n" );
  631. output( "\tmovzwl %%sp, %%ebp\n" );
  632. output( "\tlssl %%ss:-12(%%ebx), %%esp\n" );
  633. }
  634. output( "\tlret\n" );
  635. output_function_size( isEx ? "CALL32_CBClientEx_Ret" : "CALL32_CBClient_Ret" );
  636. }
  637. /*******************************************************************
  638. * output_asm_relays16
  639. *
  640. * Build all the 16-bit relay callbacks
  641. */
  642. void output_asm_relays16(void)
  643. {
  644. /* File header */
  645. output( "\t.text\n" );
  646. output( "%s:\n\n", asm_name("__wine_spec_thunk_text_16") );
  647. output( "%s\n", asm_globl("__wine_call16_start") );
  648. /* Standard CallFrom16 routine */
  649. BuildCallFrom16Core( 0, 0 );
  650. /* Register CallFrom16 routine */
  651. BuildCallFrom16Core( 1, 0 );
  652. /* C16ThkSL CallFrom16 routine */
  653. BuildCallFrom16Core( 0, 1 );
  654. /* Standard CallTo16 routine */
  655. BuildCallTo16Core( 0 );
  656. /* Register CallTo16 routine */
  657. BuildCallTo16Core( 1 );
  658. /* Standard CallTo16 return stub */
  659. BuildRet16Func();
  660. /* CBClientThunkSL routine */
  661. BuildCallTo32CBClient( 0 );
  662. /* CBClientThunkSLEx routine */
  663. BuildCallTo32CBClient( 1 );
  664. output( "%s\n", asm_globl("__wine_call16_end") );
  665. output_function_size( "__wine_spec_thunk_text_16" );
  666. /* Declare the return address and data selector variables */
  667. output( "\n\t.data\n\t.align %d\n", get_alignment(4) );
  668. output( "%s\n\t.long 0\n", asm_globl("CallTo16_DataSelector") );
  669. output( "%s\n\t.long 0\n", asm_globl("CallTo16_TebSelector") );
  670. }