relay.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 <ctype.h>
  26. #include <stdarg.h>
  27. #include "build.h"
  28. /* offset of the stack pointer relative to %fs:(0) */
  29. #define STACKOFFSET 0x10c /* FIELD_OFFSET(TEB,SystemReserved1) */
  30. /* fix this if the x86_thread_data structure is changed */
  31. #define GS_OFFSET 0x1d8 /* FIELD_OFFSET(TEB,SystemReserved2) + FIELD_OFFSET(struct x86_thread_data,gs) */
  32. static void function_header( const char *name )
  33. {
  34. output( "\n\t.align %d\n", get_alignment(4) );
  35. output( "\t%s\n", func_declaration(name) );
  36. output( "%s\n", asm_globl(name) );
  37. }
  38. /*******************************************************************
  39. * BuildCallFrom16Core
  40. *
  41. * This routine builds the core routines used in 16->32 thunks:
  42. * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
  43. *
  44. * These routines are intended to be called via a far call (with 32-bit
  45. * operand size) from 16-bit code. The 16-bit code stub must push %bp,
  46. * the 32-bit entry point to be called, and the argument conversion
  47. * routine to be used (see stack layout below).
  48. *
  49. * The core routine completes the STACK16FRAME on the 16-bit stack and
  50. * switches to the 32-bit stack. Then, the argument conversion routine
  51. * is called; it gets passed the 32-bit entry point and a pointer to the
  52. * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
  53. * use conversion routines automatically generated by BuildCallFrom16,
  54. * or write your own for special purposes.)
  55. *
  56. * The conversion routine must call the 32-bit entry point, passing it
  57. * the converted arguments, and return its return value to the core.
  58. * After the conversion routine has returned, the core switches back
  59. * to the 16-bit stack, converts the return value to the DX:AX format
  60. * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
  61. * including %bp, are popped off the stack.
  62. *
  63. * The 16-bit call stub now returns to the caller, popping the 16-bit
  64. * arguments if necessary (pascal calling convention).
  65. *
  66. * In the case of a 'register' function, CallFrom16Register fills a
  67. * CONTEXT86 structure with the values all registers had at the point
  68. * the first instruction of the 16-bit call stub was about to be
  69. * executed. A pointer to this CONTEXT86 is passed as third parameter
  70. * to the argument conversion routine, which typically passes it on
  71. * to the called 32-bit entry point.
  72. *
  73. * CallFrom16Thunk is a special variant used by the implementation of
  74. * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
  75. * implemented as follows:
  76. * On entry, the EBX register is set up to contain a flat pointer to the
  77. * 16-bit stack such that EBX+22 points to the first argument.
  78. * Then, the entry point is called, while EBP is set up to point
  79. * to the return address (on the 32-bit stack).
  80. * The called function returns with CX set to the number of bytes
  81. * to be popped of the caller's stack.
  82. *
  83. * Stack layout upon entry to the core routine (STACK16FRAME):
  84. * ... ...
  85. * (sp+24) word first 16-bit arg
  86. * (sp+22) word cs
  87. * (sp+20) word ip
  88. * (sp+18) word bp
  89. * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
  90. * (sp+12) word ip of actual entry point (necessary for relay debugging)
  91. * (sp+8) long relay (argument conversion) function entry point
  92. * (sp+4) long cs of 16-bit entry point
  93. * (sp) long ip of 16-bit entry point
  94. *
  95. * Added on the stack:
  96. * (sp-2) word saved gs
  97. * (sp-4) word saved fs
  98. * (sp-6) word saved es
  99. * (sp-8) word saved ds
  100. * (sp-12) long saved ebp
  101. * (sp-16) long saved ecx
  102. * (sp-20) long saved edx
  103. * (sp-24) long saved previous stack
  104. */
  105. static void BuildCallFrom16Core( int reg_func, int thunk )
  106. {
  107. /* Function header */
  108. if (thunk) function_header( "__wine_call_from_16_thunk" );
  109. else if (reg_func) function_header( "__wine_call_from_16_regs" );
  110. else function_header( "__wine_call_from_16" );
  111. /* Create STACK16FRAME (except STACK32FRAME link) */
  112. output( "\tpushw %%gs\n" );
  113. output( "\tpushw %%fs\n" );
  114. output( "\tpushw %%es\n" );
  115. output( "\tpushw %%ds\n" );
  116. output( "\tpushl %%ebp\n" );
  117. output( "\tpushl %%ecx\n" );
  118. output( "\tpushl %%edx\n" );
  119. /* Save original EFlags register */
  120. if (reg_func) output( "\tpushfl\n" );
  121. if ( UsePIC )
  122. {
  123. output( "\tcall 1f\n" );
  124. output( "1:\tpopl %%ecx\n" );
  125. output( "\t.byte 0x2e\n\tmovl %s-1b(%%ecx),%%edx\n", asm_name("CallTo16_DataSelector") );
  126. }
  127. else
  128. output( "\t.byte 0x2e\n\tmovl %s,%%edx\n", asm_name("CallTo16_DataSelector") );
  129. /* Load 32-bit segment registers */
  130. output( "\tmovw %%dx, %%ds\n" );
  131. output( "\tmovw %%dx, %%es\n" );
  132. if ( UsePIC )
  133. output( "\tmovw %s-1b(%%ecx), %%fs\n", asm_name("CallTo16_TebSelector") );
  134. else
  135. output( "\tmovw %s, %%fs\n", asm_name("CallTo16_TebSelector") );
  136. output( "\t.byte 0x64\n\tmov (%d),%%gs\n", GS_OFFSET );
  137. /* Translate STACK16FRAME base to flat offset in %edx */
  138. output( "\tmovw %%ss, %%dx\n" );
  139. output( "\tandl $0xfff8, %%edx\n" );
  140. output( "\tshrl $1, %%edx\n" );
  141. if (UsePIC)
  142. output( "\taddl .Lwine_ldt_copy_ptr-1b(%%ecx),%%edx\n" );
  143. else
  144. output( "\taddl .Lwine_ldt_copy_ptr,%%edx\n" );
  145. output( "\tmovl (%%edx), %%edx\n" );
  146. output( "\tmovzwl %%sp, %%ebp\n" );
  147. output( "\tleal %d(%%ebp,%%edx), %%edx\n", reg_func ? 0 : -4 );
  148. /* Get saved flags into %ecx */
  149. if (reg_func) output( "\tpopl %%ecx\n" );
  150. /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
  151. output( "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
  152. output( "\tpushl %%ebp\n" );
  153. /* Switch stacks */
  154. output( "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
  155. output( "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
  156. output( "\tpushl %%ds\n" );
  157. output( "\tpopl %%ss\n" );
  158. output( "\tmovl %%ebp, %%esp\n" );
  159. output( "\taddl $0x20,%%ebp\n"); /* FIELD_OFFSET(STACK32FRAME,ebp) */
  160. /* At this point:
  161. STACK16FRAME is completely set up
  162. DS, ES, SS: flat data segment
  163. FS: current TEB
  164. ESP: points to last STACK32FRAME
  165. EBP: points to ebp member of last STACK32FRAME
  166. EDX: points to current STACK16FRAME
  167. ECX: contains saved flags
  168. all other registers: unchanged */
  169. /* Special case: C16ThkSL stub */
  170. if ( thunk )
  171. {
  172. /* Set up registers as expected and call thunk */
  173. output( "\tleal 0x1a(%%edx),%%ebx\n" ); /* sizeof(STACK16FRAME)-22 */
  174. output( "\tleal -4(%%esp), %%ebp\n" );
  175. output( "\tcall *0x26(%%edx)\n"); /* FIELD_OFFSET(STACK16FRAME,entry_point) */
  176. /* Switch stack back */
  177. output( "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
  178. output( "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
  179. output( "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
  180. /* Restore registers and return directly to caller */
  181. output( "\taddl $8, %%esp\n" );
  182. output( "\tpopl %%ebp\n" );
  183. output( "\tpopw %%ds\n" );
  184. output( "\tpopw %%es\n" );
  185. output( "\tpopw %%fs\n" );
  186. output( "\tpopw %%gs\n" );
  187. output( "\taddl $20, %%esp\n" );
  188. output( "\txorb %%ch, %%ch\n" );
  189. output( "\tpopl %%ebx\n" );
  190. output( "\taddw %%cx, %%sp\n" );
  191. output( "\tpush %%ebx\n" );
  192. output( "\t.byte 0x66\n" );
  193. output( "\tlret\n" );
  194. output_function_size( "__wine_call_from_16_thunk" );
  195. return;
  196. }
  197. /* Build register CONTEXT */
  198. if ( reg_func )
  199. {
  200. output( "\tsubl $0x2cc,%%esp\n" ); /* sizeof(CONTEXT86) */
  201. output( "\tmovl %%ecx,0xc0(%%esp)\n" ); /* EFlags */
  202. output( "\tmovl %%eax,0xb0(%%esp)\n" ); /* Eax */
  203. output( "\tmovl %%ebx,0xa4(%%esp)\n" ); /* Ebx */
  204. output( "\tmovl %%esi,0xa0(%%esp)\n" ); /* Esi */
  205. output( "\tmovl %%edi,0x9c(%%esp)\n" ); /* Edi */
  206. output( "\tmovl 0x0c(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ebp) */
  207. output( "\tmovl %%eax,0xb4(%%esp)\n" ); /* Ebp */
  208. output( "\tmovl 0x08(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ecx) */
  209. output( "\tmovl %%eax,0xac(%%esp)\n" ); /* Ecx */
  210. output( "\tmovl 0x04(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,edx) */
  211. output( "\tmovl %%eax,0xa8(%%esp)\n" ); /* Edx */
  212. output( "\tmovzwl 0x10(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ds) */
  213. output( "\tmovl %%eax,0x98(%%esp)\n" ); /* SegDs */
  214. output( "\tmovzwl 0x12(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,es) */
  215. output( "\tmovl %%eax,0x94(%%esp)\n" ); /* SegEs */
  216. output( "\tmovzwl 0x14(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,fs) */
  217. output( "\tmovl %%eax,0x90(%%esp)\n" ); /* SegFs */
  218. output( "\tmovzwl 0x16(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,gs) */
  219. output( "\tmovl %%eax,0x8c(%%esp)\n" ); /* SegGs */
  220. output( "\tmovzwl 0x2e(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,cs) */
  221. output( "\tmovl %%eax,0xbc(%%esp)\n" ); /* SegCs */
  222. output( "\tmovzwl 0x2c(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ip) */
  223. output( "\tmovl %%eax,0xb8(%%esp)\n" ); /* Eip */
  224. output( "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
  225. output( "\tmovl %%eax,0xc8(%%esp)\n" ); /* SegSs */
  226. output( "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
  227. output( "\taddl $0x2c,%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ip) */
  228. output( "\tmovl %%eax,0xc4(%%esp)\n" ); /* Esp */
  229. #if 0
  230. output( "\tfsave 0x1c(%%esp)\n" ); /* FloatSave */
  231. #endif
  232. /* Push address of CONTEXT86 structure -- popped by the relay routine */
  233. output( "\tmovl %%esp,%%eax\n" );
  234. output( "\tandl $~15,%%esp\n" );
  235. output( "\tsubl $4,%%esp\n" );
  236. output( "\tpushl %%eax\n" );
  237. }
  238. else
  239. {
  240. output( "\tsubl $8,%%esp\n" );
  241. output( "\tandl $~15,%%esp\n" );
  242. output( "\taddl $8,%%esp\n" );
  243. }
  244. /* Call relay routine (which will call the API entry point) */
  245. output( "\tleal 0x30(%%edx),%%eax\n" ); /* sizeof(STACK16FRAME) */
  246. output( "\tpushl %%eax\n" );
  247. output( "\tpushl 0x26(%%edx)\n"); /* FIELD_OFFSET(STACK16FRAME,entry_point) */
  248. output( "\tcall *0x20(%%edx)\n"); /* FIELD_OFFSET(STACK16FRAME,relay) */
  249. if ( reg_func )
  250. {
  251. output( "\tleal -748(%%ebp),%%ebx\n" ); /* sizeof(CONTEXT) + FIELD_OFFSET(STACK32FRAME,ebp) */
  252. /* Switch stack back */
  253. output( "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
  254. output( "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
  255. output( "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
  256. /* Get return address to CallFrom16 stub */
  257. output( "\taddw $0x14,%%sp\n" ); /* FIELD_OFFSET(STACK16FRAME,callfrom_ip)-4 */
  258. output( "\tpopl %%eax\n" );
  259. output( "\tpopl %%edx\n" );
  260. /* Restore all registers from CONTEXT */
  261. output( "\tmovw 0xc8(%%ebx),%%ss\n"); /* SegSs */
  262. output( "\tmovl 0xc4(%%ebx),%%esp\n"); /* Esp */
  263. output( "\taddl $4, %%esp\n" ); /* room for final return address */
  264. output( "\tpushw 0xbc(%%ebx)\n"); /* SegCs */
  265. output( "\tpushw 0xb8(%%ebx)\n"); /* Eip */
  266. output( "\tpushl %%edx\n" );
  267. output( "\tpushl %%eax\n" );
  268. output( "\tpushl 0xc0(%%ebx)\n"); /* EFlags */
  269. output( "\tpushl 0x98(%%ebx)\n"); /* SegDs */
  270. output( "\tpushl 0x94(%%ebx)\n"); /* SegEs */
  271. output( "\tpopl %%es\n" );
  272. output( "\tpushl 0x90(%%ebx)\n"); /* SegFs */
  273. output( "\tpopl %%fs\n" );
  274. output( "\tpushl 0x8c(%%ebx)\n"); /* SegGs */
  275. output( "\tpopl %%gs\n" );
  276. output( "\tmovl 0xb4(%%ebx),%%ebp\n"); /* Ebp */
  277. output( "\tmovl 0xa0(%%ebx),%%esi\n"); /* Esi */
  278. output( "\tmovl 0x9c(%%ebx),%%edi\n"); /* Edi */
  279. output( "\tmovl 0xb0(%%ebx),%%eax\n"); /* Eax */
  280. output( "\tmovl 0xa8(%%ebx),%%edx\n"); /* Edx */
  281. output( "\tmovl 0xac(%%ebx),%%ecx\n"); /* Ecx */
  282. output( "\tmovl 0xa4(%%ebx),%%ebx\n"); /* Ebx */
  283. output( "\tpopl %%ds\n" );
  284. output( "\tpopfl\n" );
  285. output( "\tlret\n" );
  286. output_function_size( "__wine_call_from_16_regs" );
  287. }
  288. else
  289. {
  290. /* Switch stack back */
  291. output( "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
  292. output( "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
  293. output( "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
  294. /* Restore registers */
  295. output( "\tpopl %%edx\n" );
  296. output( "\tpopl %%ecx\n" );
  297. output( "\tpopl %%ebp\n" );
  298. output( "\tpopw %%ds\n" );
  299. output( "\tpopw %%es\n" );
  300. output( "\tpopw %%fs\n" );
  301. output( "\tpopw %%gs\n" );
  302. /* Return to return stub which will return to caller */
  303. output( "\tlret $12\n" );
  304. output_function_size( "__wine_call_from_16" );
  305. }
  306. }
  307. /*******************************************************************
  308. * BuildCallTo16Core
  309. *
  310. * This routine builds the core routines used in 32->16 thunks:
  311. *
  312. * extern DWORD WINAPI wine_call_to_16( FARPROC16 target, DWORD cbArgs, PEXCEPTION_HANDLER handler );
  313. * extern void WINAPI wine_call_to_16_regs( CONTEXT86 *context, DWORD cbArgs, PEXCEPTION_HANDLER handler );
  314. *
  315. * These routines can be called directly from 32-bit code.
  316. *
  317. * All routines expect that the 16-bit stack contents (arguments) and the
  318. * return address (segptr to CallTo16_Ret) were already set up by the
  319. * caller; nb_args must contain the number of bytes to be conserved. The
  320. * 16-bit SS:SP will be set accordingly.
  321. *
  322. * All other registers are either taken from the CONTEXT86 structure
  323. * or else set to default values. The target routine address is either
  324. * given directly or taken from the CONTEXT86.
  325. */
  326. static void BuildCallTo16Core( int reg_func )
  327. {
  328. const char *name = reg_func ? "wine_call_to_16_regs" : "wine_call_to_16";
  329. const char *func_name = is_pe() ? strmake( "%s@12", name ) : name;
  330. /* Function header */
  331. function_header( func_name );
  332. /* Function entry sequence */
  333. output_cfi( ".cfi_startproc" );
  334. output( "\tpushl %%ebp\n" );
  335. output_cfi( ".cfi_adjust_cfa_offset 4" );
  336. output_cfi( ".cfi_rel_offset %%ebp,0" );
  337. output( "\tmovl %%esp, %%ebp\n" );
  338. output_cfi( ".cfi_def_cfa_register %%ebp" );
  339. /* Save the 32-bit registers */
  340. output( "\tpushl %%ebx\n" );
  341. output_cfi( ".cfi_rel_offset %%ebx,-4" );
  342. output( "\tpushl %%esi\n" );
  343. output_cfi( ".cfi_rel_offset %%esi,-8" );
  344. output( "\tpushl %%edi\n" );
  345. output_cfi( ".cfi_rel_offset %%edi,-12" );
  346. output( "\t.byte 0x64\n\tmov %%gs,(%d)\n", GS_OFFSET );
  347. /* Setup exception frame */
  348. output( "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
  349. output( "\tpushl 16(%%ebp)\n" ); /* handler */
  350. output( "\t.byte 0x64\n\tpushl (0)\n" );
  351. output( "\t.byte 0x64\n\tmovl %%esp,(0)\n" );
  352. /* Call the actual CallTo16 routine (simulate a lcall) */
  353. output( "\tpushl %%cs\n" );
  354. output( "\tcall .L%s\n", name );
  355. /* Remove exception frame */
  356. output( "\t.byte 0x64\n\tpopl (0)\n" );
  357. output( "\taddl $4, %%esp\n" );
  358. output( "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
  359. if ( !reg_func )
  360. {
  361. /* Convert return value */
  362. output( "\tandl $0xffff,%%eax\n" );
  363. output( "\tshll $16,%%edx\n" );
  364. output( "\torl %%edx,%%eax\n" );
  365. }
  366. else
  367. {
  368. /*
  369. * Modify CONTEXT86 structure to contain new values
  370. *
  371. * NOTE: We restore only EAX, EBX, ECX, EDX, EBP, and ESP.
  372. * The segment registers as well as ESI and EDI should
  373. * not be modified by a well-behaved 16-bit routine in
  374. * any case. [If necessary, we could restore them as well,
  375. * at the cost of a somewhat less efficient return path.]
  376. */
  377. output( "\tmovl 0x14(%%esp),%%edi\n" ); /* FIELD_OFFSET(STACK32FRAME,target) - FIELD_OFFSET(STACK32FRAME,edi) */
  378. /* everything above edi has been popped already */
  379. output( "\tmovl %%eax,0xb0(%%edi)\n"); /* Eax */
  380. output( "\tmovl %%ebx,0xa4(%%edi)\n"); /* Ebx */
  381. output( "\tmovl %%ecx,0xac(%%edi)\n"); /* Ecx */
  382. output( "\tmovl %%edx,0xa8(%%edi)\n"); /* Edx */
  383. output( "\tmovl %%ebp,0xb4(%%edi)\n"); /* Ebp */
  384. output( "\tmovl %%esi,0xc4(%%edi)\n"); /* Esp */
  385. /* The return glue code saved %esp into %esi */
  386. }
  387. /* Restore the 32-bit registers */
  388. output( "\tpopl %%edi\n" );
  389. output_cfi( ".cfi_same_value %%edi" );
  390. output( "\tpopl %%esi\n" );
  391. output_cfi( ".cfi_same_value %%esi" );
  392. output( "\tpopl %%ebx\n" );
  393. output_cfi( ".cfi_same_value %%ebx" );
  394. /* Function exit sequence */
  395. output( "\tpopl %%ebp\n" );
  396. output_cfi( ".cfi_def_cfa %%esp,4" );
  397. output_cfi( ".cfi_same_value %%ebp" );
  398. output( "\tret $12\n" );
  399. output_cfi( ".cfi_endproc" );
  400. /* Start of the actual CallTo16 routine */
  401. output( ".L%s:\n", name );
  402. /* Switch to the 16-bit stack */
  403. output( "\tmovl %%esp,%%edx\n" );
  404. output( "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
  405. output( "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
  406. output( "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
  407. /* Make %bp point to the previous stackframe (built by CallFrom16) */
  408. output( "\tmovzwl %%sp,%%ebp\n" );
  409. output( "\tleal 0x2a(%%ebp),%%ebp\n"); /* FIELD_OFFSET(STACK16FRAME,bp) */
  410. /* Add the specified offset to the new sp */
  411. output( "\tsubw 0x2c(%%edx), %%sp\n"); /* FIELD_OFFSET(STACK32FRAME,nb_args) */
  412. if (reg_func)
  413. {
  414. /* Push the called routine address */
  415. output( "\tmovl 0x28(%%edx),%%edx\n"); /* FIELD_OFFSET(STACK32FRAME,target) */
  416. output( "\tpushw 0xbc(%%edx)\n"); /* SegCs */
  417. output( "\tpushw 0xb8(%%edx)\n"); /* Eip */
  418. /* Get the registers */
  419. output( "\tpushw 0x98(%%edx)\n"); /* SegDs */
  420. output( "\tpushl 0x94(%%edx)\n"); /* SegEs */
  421. output( "\tpopl %%es\n" );
  422. output( "\tmovl 0xb4(%%edx),%%ebp\n"); /* Ebp */
  423. output( "\tmovl 0xa0(%%edx),%%esi\n"); /* Esi */
  424. output( "\tmovl 0x9c(%%edx),%%edi\n"); /* Edi */
  425. output( "\tmovl 0xb0(%%edx),%%eax\n"); /* Eax */
  426. output( "\tmovl 0xa4(%%edx),%%ebx\n"); /* Ebx */
  427. output( "\tmovl 0xac(%%edx),%%ecx\n"); /* Ecx */
  428. output( "\tmovl 0xa8(%%edx),%%edx\n"); /* Edx */
  429. /* Get the 16-bit ds */
  430. output( "\tpopw %%ds\n" );
  431. }
  432. else /* not a register function */
  433. {
  434. /* Push the called routine address */
  435. output( "\tpushl 0x28(%%edx)\n"); /* FIELD_OFFSET(STACK32FRAME,target) */
  436. /* Set %fs and %gs to the value saved by the last CallFrom16 */
  437. output( "\tpushw -22(%%ebp)\n" ); /* FIELD_OFFSET(STACK16FRAME,fs)-FIELD_OFFSET(STACK16FRAME,bp) */
  438. output( "\tpopw %%fs\n" );
  439. output( "\tpushw -20(%%ebp)\n" ); /* FIELD_OFFSET(STACK16FRAME,gs)-FIELD_OFFSET(STACK16FRAME,bp) */
  440. output( "\tpopw %%gs\n" );
  441. /* Set %ds and %es (and %ax just in case) equal to %ss */
  442. output( "\tmovw %%ss,%%ax\n" );
  443. output( "\tmovw %%ax,%%ds\n" );
  444. output( "\tmovw %%ax,%%es\n" );
  445. }
  446. /* Jump to the called routine */
  447. output( "\t.byte 0x66\n" );
  448. output( "\tlret\n" );
  449. /* Function footer */
  450. output_function_size( func_name );
  451. }
  452. /*******************************************************************
  453. * BuildRet16Func
  454. *
  455. * Build the return code for 16-bit callbacks
  456. */
  457. static void BuildRet16Func(void)
  458. {
  459. function_header( "__wine_call_to_16_ret" );
  460. /* Save %esp into %esi */
  461. output( "\tmovl %%esp,%%esi\n" );
  462. /* Restore 32-bit segment registers */
  463. output( "\t.byte 0x2e\n\tmovl %s", asm_name("CallTo16_DataSelector") );
  464. output( "-%s,%%edi\n", asm_name("__wine_call16_start") );
  465. output( "\tmovw %%di,%%ds\n" );
  466. output( "\tmovw %%di,%%es\n" );
  467. output( "\t.byte 0x2e\n\tmov %s", asm_name("CallTo16_TebSelector") );
  468. output( "-%s,%%fs\n", asm_name("__wine_call16_start") );
  469. output( "\t.byte 0x64\n\tmov (%d),%%gs\n", GS_OFFSET );
  470. /* Restore the 32-bit stack */
  471. output( "\tmovw %%di,%%ss\n" );
  472. output( "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
  473. /* Return to caller */
  474. output( "\tlret\n" );
  475. output_function_size( "__wine_call_to_16_ret" );
  476. }
  477. /*******************************************************************
  478. * output_asm_relays16
  479. *
  480. * Build all the 16-bit relay callbacks
  481. */
  482. void output_asm_relays16(void)
  483. {
  484. /* File header */
  485. output( "\t.text\n" );
  486. output( "%s:\n\n", asm_name("__wine_spec_thunk_text_16") );
  487. output( "%s\n", asm_globl("__wine_call16_start") );
  488. /* Standard CallFrom16 routine */
  489. BuildCallFrom16Core( 0, 0 );
  490. /* Register CallFrom16 routine */
  491. BuildCallFrom16Core( 1, 0 );
  492. /* C16ThkSL CallFrom16 routine */
  493. BuildCallFrom16Core( 0, 1 );
  494. /* Standard CallTo16 routine */
  495. BuildCallTo16Core( 0 );
  496. /* Register CallTo16 routine */
  497. BuildCallTo16Core( 1 );
  498. /* Standard CallTo16 return stub */
  499. BuildRet16Func();
  500. output( "%s\n", asm_globl("__wine_call16_end") );
  501. output_function_size( "__wine_spec_thunk_text_16" );
  502. /* Declare the return address and data selector variables */
  503. output( "\n\t.data\n\t.align %d\n", get_alignment(4) );
  504. output( "%s\n\t.long 0\n", asm_globl("CallTo16_DataSelector") );
  505. output( "%s\n\t.long 0\n", asm_globl("CallTo16_TebSelector") );
  506. }