Fixed potential crash in fd_dump function.
[wine/multimedia.git] / tools / winebuild / relay.c
blobf4a4f5e10232fc77aeeafbff3b8f53fc0a81944a
1 /*
2 * Relay calls helper routines
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
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <ctype.h>
30 #include "thread.h"
31 #include "stackframe.h"
33 #include "build.h"
35 #ifdef __i386__
37 static void function_header( FILE *outfile, const char *name )
39 fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
40 fprintf( outfile, "\t" __ASM_FUNC("%s") "\n", name );
41 fprintf( outfile, "\t.globl " __ASM_NAME("%s") "\n", name );
42 fprintf( outfile, __ASM_NAME("%s") ":\n", name );
46 static void function_footer( FILE *outfile, const char *name )
48 fprintf( outfile, ".size " __ASM_NAME("%s") ", . - " __ASM_NAME("%s") "\n", name, name );
51 /*******************************************************************
52 * BuildCallFrom16Core
54 * This routine builds the core routines used in 16->32 thunks:
55 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
57 * These routines are intended to be called via a far call (with 32-bit
58 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
59 * the 32-bit entry point to be called, and the argument conversion
60 * routine to be used (see stack layout below).
62 * The core routine completes the STACK16FRAME on the 16-bit stack and
63 * switches to the 32-bit stack. Then, the argument conversion routine
64 * is called; it gets passed the 32-bit entry point and a pointer to the
65 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
66 * use conversion routines automatically generated by BuildCallFrom16,
67 * or write your own for special purposes.)
69 * The conversion routine must call the 32-bit entry point, passing it
70 * the converted arguments, and return its return value to the core.
71 * After the conversion routine has returned, the core switches back
72 * to the 16-bit stack, converts the return value to the DX:AX format
73 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
74 * including %bp, are popped off the stack.
76 * The 16-bit call stub now returns to the caller, popping the 16-bit
77 * arguments if necessary (pascal calling convention).
79 * In the case of a 'register' function, CallFrom16Register fills a
80 * CONTEXT86 structure with the values all registers had at the point
81 * the first instruction of the 16-bit call stub was about to be
82 * executed. A pointer to this CONTEXT86 is passed as third parameter
83 * to the argument conversion routine, which typically passes it on
84 * to the called 32-bit entry point.
86 * CallFrom16Thunk is a special variant used by the implementation of
87 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
88 * implemented as follows:
89 * On entry, the EBX register is set up to contain a flat pointer to the
90 * 16-bit stack such that EBX+22 points to the first argument.
91 * Then, the entry point is called, while EBP is set up to point
92 * to the return address (on the 32-bit stack).
93 * The called function returns with CX set to the number of bytes
94 * to be popped of the caller's stack.
96 * Stack layout upon entry to the core routine (STACK16FRAME):
97 * ... ...
98 * (sp+24) word first 16-bit arg
99 * (sp+22) word cs
100 * (sp+20) word ip
101 * (sp+18) word bp
102 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
103 * (sp+12) word ip of actual entry point (necessary for relay debugging)
104 * (sp+8) long relay (argument conversion) function entry point
105 * (sp+4) long cs of 16-bit entry point
106 * (sp) long ip of 16-bit entry point
108 * Added on the stack:
109 * (sp-2) word saved gs
110 * (sp-4) word saved fs
111 * (sp-6) word saved es
112 * (sp-8) word saved ds
113 * (sp-12) long saved ebp
114 * (sp-16) long saved ecx
115 * (sp-20) long saved edx
116 * (sp-24) long saved previous stack
118 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret )
120 const char *name = thunk? "thunk" : reg_func? "regs" : short_ret? "word" : "long";
122 /* Function header */
123 if (thunk) function_header( outfile, "__wine_call_from_16_thunk" );
124 else if (reg_func) function_header( outfile, "__wine_call_from_16_regs" );
125 else if (short_ret) function_header( outfile, "__wine_call_from_16_word" );
126 else function_header( outfile, "__wine_call_from_16_long" );
128 /* Create STACK16FRAME (except STACK32FRAME link) */
129 fprintf( outfile, "\tpushw %%gs\n" );
130 fprintf( outfile, "\tpushw %%fs\n" );
131 fprintf( outfile, "\tpushw %%es\n" );
132 fprintf( outfile, "\tpushw %%ds\n" );
133 fprintf( outfile, "\tpushl %%ebp\n" );
134 fprintf( outfile, "\tpushl %%ecx\n" );
135 fprintf( outfile, "\tpushl %%edx\n" );
137 /* Save original EFlags register */
138 fprintf( outfile, "\tpushfl\n" );
140 if ( UsePIC )
142 /* Get Global Offset Table into %ecx */
143 fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot1\n", name );
144 fprintf( outfile, ".L__wine_call_from_16_%s.getgot1:\n", name );
145 fprintf( outfile, "\tpopl %%ecx\n" );
146 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot1], %%ecx\n", name );
149 if (UsePIC)
151 fprintf( outfile, "\t.byte 0x2e\n\tmovl " __ASM_NAME("CallTo16_DataSelector@GOT") "(%%ecx), %%edx\n" );
152 fprintf( outfile, "\t.byte 0x2e\n\tmovl (%%edx), %%edx\n" );
154 else
155 fprintf( outfile, "\t.byte 0x2e\n\tmovl " __ASM_NAME("CallTo16_DataSelector") ",%%edx\n" );
157 /* Load 32-bit segment registers */
158 #ifdef __svr4__
159 fprintf( outfile, "\tdata16\n");
160 #endif
161 fprintf( outfile, "\tmovw %%dx, %%ds\n" );
162 #ifdef __svr4__
163 fprintf( outfile, "\tdata16\n");
164 #endif
165 fprintf( outfile, "\tmovw %%dx, %%es\n" );
167 if ( UsePIC )
169 fprintf( outfile, "\tmovl " __ASM_NAME("CallTo16_TebSelector@GOT") "(%%ecx), %%edx\n" );
170 fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
172 else
173 fprintf( outfile, "\tmovw " __ASM_NAME("CallTo16_TebSelector") ", %%fs\n" );
175 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%gs\n", STRUCTOFFSET(TEB,gs_sel) );
177 /* Get address of wine_ldt_copy array into %ecx */
178 if ( UsePIC )
179 fprintf( outfile, "\tmovl " __ASM_NAME("wine_ldt_copy@GOT") "(%%ecx), %%ecx\n" );
180 else
181 fprintf( outfile, "\tmovl $" __ASM_NAME("wine_ldt_copy") ", %%ecx\n" );
183 /* Translate STACK16FRAME base to flat offset in %edx */
184 fprintf( outfile, "\tmovw %%ss, %%dx\n" );
185 fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
186 fprintf( outfile, "\tshrl $1, %%edx\n" );
187 fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" );
188 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
189 fprintf( outfile, "\tleal (%%ebp,%%edx), %%edx\n" );
191 /* Get saved flags into %ecx */
192 fprintf( outfile, "\tpopl %%ecx\n" );
194 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
195 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
196 fprintf( outfile, "\tpushl %%ebp\n" );
198 /* Switch stacks */
199 #ifdef __svr4__
200 fprintf( outfile,"\tdata16\n");
201 #endif
202 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
203 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
204 fprintf( outfile, "\tpushl %%ds\n" );
205 fprintf( outfile, "\tpopl %%ss\n" );
206 fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
207 fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
210 /* At this point:
211 STACK16FRAME is completely set up
212 DS, ES, SS: flat data segment
213 FS: current TEB
214 ESP: points to last STACK32FRAME
215 EBP: points to ebp member of last STACK32FRAME
216 EDX: points to current STACK16FRAME
217 ECX: contains saved flags
218 all other registers: unchanged */
220 /* Special case: C16ThkSL stub */
221 if ( thunk )
223 /* Set up registers as expected and call thunk */
224 fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
225 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
227 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
229 /* Switch stack back */
230 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
231 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
232 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
234 /* Restore registers and return directly to caller */
235 fprintf( outfile, "\taddl $8, %%esp\n" );
236 fprintf( outfile, "\tpopl %%ebp\n" );
237 fprintf( outfile, "\tpopw %%ds\n" );
238 fprintf( outfile, "\tpopw %%es\n" );
239 fprintf( outfile, "\tpopw %%fs\n" );
240 fprintf( outfile, "\tpopw %%gs\n" );
241 fprintf( outfile, "\taddl $20, %%esp\n" );
243 fprintf( outfile, "\txorb %%ch, %%ch\n" );
244 fprintf( outfile, "\tpopl %%ebx\n" );
245 fprintf( outfile, "\taddw %%cx, %%sp\n" );
246 fprintf( outfile, "\tpush %%ebx\n" );
248 fprintf( outfile, "\t.byte 0x66\n" );
249 fprintf( outfile, "\tlret\n" );
251 return;
255 /* Build register CONTEXT */
256 if ( reg_func )
258 fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
260 fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
262 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
263 fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
264 fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
265 fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
267 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
268 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
269 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
270 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
271 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
272 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
274 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
275 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
276 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
277 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
278 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
279 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
280 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
281 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
283 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
284 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
285 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
286 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
288 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
289 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
290 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
291 fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
292 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
293 #if 0
294 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
295 #endif
297 /* Push address of CONTEXT86 structure -- popped by the relay routine */
298 fprintf( outfile, "\tpushl %%esp\n" );
302 /* Print debug info before call */
303 if ( debugging )
305 if ( UsePIC )
307 fprintf( outfile, "\tpushl %%ebx\n" );
309 /* Get Global Offset Table into %ebx (for PLT call) */
310 fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot2\n", name );
311 fprintf( outfile, ".L__wine_call_from_16_%s.getgot2:\n", name );
312 fprintf( outfile, "\tpopl %%ebx\n" );
313 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot2], %%ebx\n", name );
316 fprintf( outfile, "\tpushl %%edx\n" );
317 if ( reg_func )
318 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
319 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
320 else
321 fprintf( outfile, "\tpushl $0\n" );
323 if ( UsePIC )
324 fprintf( outfile, "\tcall " __ASM_NAME("RELAY_DebugCallFrom16@PLT") "\n ");
325 else
326 fprintf( outfile, "\tcall " __ASM_NAME("RELAY_DebugCallFrom16") "\n ");
328 fprintf( outfile, "\tpopl %%edx\n" );
329 fprintf( outfile, "\tpopl %%edx\n" );
331 if ( UsePIC )
332 fprintf( outfile, "\tpopl %%ebx\n" );
335 /* Call relay routine (which will call the API entry point) */
336 fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
337 fprintf( outfile, "\tpushl %%eax\n" );
338 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
339 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
341 /* Print debug info after call */
342 if ( debugging )
344 if ( UsePIC )
346 fprintf( outfile, "\tpushl %%ebx\n" );
348 /* Get Global Offset Table into %ebx (for PLT call) */
349 fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot3\n", name );
350 fprintf( outfile, ".L__wine_call_from_16_%s.getgot3:\n", name );
351 fprintf( outfile, "\tpopl %%ebx\n" );
352 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot3], %%ebx\n", name );
355 fprintf( outfile, "\tpushl %%eax\n" );
356 if ( reg_func )
357 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
358 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
359 else
360 fprintf( outfile, "\tpushl $0\n" );
362 if ( UsePIC )
363 fprintf( outfile, "\tcall " __ASM_NAME("RELAY_DebugCallFrom16Ret@PLT") "\n ");
364 else
365 fprintf( outfile, "\tcall " __ASM_NAME("RELAY_DebugCallFrom16Ret") "\n ");
367 fprintf( outfile, "\tpopl %%eax\n" );
368 fprintf( outfile, "\tpopl %%eax\n" );
370 if ( UsePIC )
371 fprintf( outfile, "\tpopl %%ebx\n" );
375 if ( reg_func )
377 fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
379 /* Switch stack back */
380 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
381 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
382 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
384 /* Get return address to CallFrom16 stub */
385 fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
386 fprintf( outfile, "\tpopl %%eax\n" );
387 fprintf( outfile, "\tpopl %%edx\n" );
389 /* Restore all registers from CONTEXT */
390 fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
391 fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
392 fprintf( outfile, "\taddl $4, %%esp\n" ); /* room for final return address */
394 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
395 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
396 fprintf( outfile, "\tpushl %%edx\n" );
397 fprintf( outfile, "\tpushl %%eax\n" );
398 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
399 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
401 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegEs) );
402 fprintf( outfile, "\tpopl %%es\n" );
403 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegFs) );
404 fprintf( outfile, "\tpopl %%fs\n" );
405 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegGs) );
406 fprintf( outfile, "\tpopl %%gs\n" );
408 fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
409 fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
410 fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
411 fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
412 fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
413 fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
414 fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
416 fprintf( outfile, "\tpopl %%ds\n" );
417 fprintf( outfile, "\tpopfl\n" );
418 fprintf( outfile, "\tlret\n" );
420 else
422 /* Switch stack back */
423 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
424 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
425 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
427 /* Restore registers */
428 fprintf( outfile, "\tpopl %%edx\n" );
429 fprintf( outfile, "\tpopl %%ecx\n" );
430 fprintf( outfile, "\tpopl %%ebp\n" );
431 fprintf( outfile, "\tpopw %%ds\n" );
432 fprintf( outfile, "\tpopw %%es\n" );
433 fprintf( outfile, "\tpopw %%fs\n" );
434 fprintf( outfile, "\tpopw %%gs\n" );
436 /* Prepare return value and set flags accordingly */
437 if ( !short_ret )
438 fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
439 fprintf( outfile, "\torl %%eax, %%eax\n" );
441 /* Return to return stub which will return to caller */
442 fprintf( outfile, "\tlret $12\n" );
444 if (thunk) function_footer( outfile, "__wine_call_from_16_thunk" );
445 else if (reg_func) function_footer( outfile, "__wine_call_from_16_regs" );
446 else if (short_ret) function_footer( outfile, "__wine_call_from_16_word" );
447 else function_footer( outfile, "__wine_call_from_16_long" );
451 /*******************************************************************
452 * BuildCallTo16Core
454 * This routine builds the core routines used in 32->16 thunks:
456 * extern DWORD WINAPI wine_call_to_16( FARPROC16 target, DWORD cbArgs, PEXCEPTION_HANDLER handler );
457 * extern void WINAPI wine_call_to_16_regs( CONTEXT86 *context, DWORD cbArgs, PEXCEPTION_HANDLER handler );
459 * These routines can be called directly from 32-bit code.
461 * All routines expect that the 16-bit stack contents (arguments) and the
462 * return address (segptr to CallTo16_Ret) were already set up by the
463 * caller; nb_args must contain the number of bytes to be conserved. The
464 * 16-bit SS:SP will be set accordinly.
466 * All other registers are either taken from the CONTEXT86 structure
467 * or else set to default values. The target routine address is either
468 * given directly or taken from the CONTEXT86.
470 static void BuildCallTo16Core( FILE *outfile, int reg_func )
472 const char *name = reg_func ? "wine_call_to_16_regs" : "wine_call_to_16";
474 /* Function header */
475 function_header( outfile, name );
477 /* Function entry sequence */
478 fprintf( outfile, "\tpushl %%ebp\n" );
479 fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
481 /* Save the 32-bit registers */
482 fprintf( outfile, "\tpushl %%ebx\n" );
483 fprintf( outfile, "\tpushl %%esi\n" );
484 fprintf( outfile, "\tpushl %%edi\n" );
485 fprintf( outfile, "\t.byte 0x64\n\tmovl %%gs,(%d)\n", STRUCTOFFSET(TEB,gs_sel) );
487 /* Setup exception frame */
488 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
489 fprintf( outfile, "\tpushl 16(%%ebp)\n" ); /* handler */
490 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STRUCTOFFSET(TEB,Tib.ExceptionList) );
491 fprintf( outfile, "\t.byte 0x64\n\tmovl %%esp,(%d)\n", STRUCTOFFSET(TEB,Tib.ExceptionList) );
493 /* Call the actual CallTo16 routine (simulate a lcall) */
494 fprintf( outfile, "\tpushl %%cs\n" );
495 fprintf( outfile, "\tcall .L%s\n", name );
497 /* Remove exception frame */
498 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STRUCTOFFSET(TEB,Tib.ExceptionList) );
499 fprintf( outfile, "\taddl $4, %%esp\n" );
500 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
502 if ( !reg_func )
504 /* Convert return value */
505 fprintf( outfile, "\tandl $0xffff,%%eax\n" );
506 fprintf( outfile, "\tshll $16,%%edx\n" );
507 fprintf( outfile, "\torl %%edx,%%eax\n" );
509 else
512 * Modify CONTEXT86 structure to contain new values
514 * NOTE: We restore only EAX, EBX, EDX, EDX, EBP, and ESP.
515 * The segment registers as well as ESI and EDI should
516 * not be modified by a well-behaved 16-bit routine in
517 * any case. [If necessary, we could restore them as well,
518 * at the cost of a somewhat less efficient return path.]
521 fprintf( outfile, "\tmovl %d(%%esp), %%edi\n", STACK32OFFSET(target) - STACK32OFFSET(edi));
522 /* everything above edi has been popped already */
524 fprintf( outfile, "\tmovl %%eax, %d(%%edi)\n", CONTEXTOFFSET(Eax) );
525 fprintf( outfile, "\tmovl %%ebx, %d(%%edi)\n", CONTEXTOFFSET(Ebx) );
526 fprintf( outfile, "\tmovl %%ecx, %d(%%edi)\n", CONTEXTOFFSET(Ecx) );
527 fprintf( outfile, "\tmovl %%edx, %d(%%edi)\n", CONTEXTOFFSET(Edx) );
528 fprintf( outfile, "\tmovl %%ebp, %d(%%edi)\n", CONTEXTOFFSET(Ebp) );
529 fprintf( outfile, "\tmovl %%esi, %d(%%edi)\n", CONTEXTOFFSET(Esp) );
530 /* The return glue code saved %esp into %esi */
533 /* Restore the 32-bit registers */
534 fprintf( outfile, "\tpopl %%edi\n" );
535 fprintf( outfile, "\tpopl %%esi\n" );
536 fprintf( outfile, "\tpopl %%ebx\n" );
538 /* Function exit sequence */
539 fprintf( outfile, "\tpopl %%ebp\n" );
540 fprintf( outfile, "\tret $12\n" );
543 /* Start of the actual CallTo16 routine */
545 fprintf( outfile, ".L%s:\n", name );
547 /* Switch to the 16-bit stack */
548 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
549 #ifdef __svr4__
550 fprintf( outfile,"\tdata16\n");
551 #endif
552 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
553 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
554 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
556 /* Make %bp point to the previous stackframe (built by CallFrom16) */
557 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
558 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
560 /* Add the specified offset to the new sp */
561 fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
563 if (reg_func)
565 /* Push the called routine address */
566 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
567 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
568 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
570 /* Get the registers */
571 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
572 fprintf( outfile, "\tpushl %d(%%edx)\n", CONTEXTOFFSET(SegEs) );
573 fprintf( outfile, "\tpopl %%es\n" );
574 fprintf( outfile, "\tpushl %d(%%edx)\n", CONTEXTOFFSET(SegFs) );
575 fprintf( outfile, "\tpopl %%fs\n" );
576 fprintf( outfile, "\tpushl %d(%%edx)\n", CONTEXTOFFSET(SegGs) );
577 fprintf( outfile, "\tpopl %%gs\n" );
578 fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
579 fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
580 fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
581 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
582 fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
583 fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
584 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
586 /* Get the 16-bit ds */
587 fprintf( outfile, "\tpopw %%ds\n" );
589 else /* not a register function */
591 /* Push the called routine address */
592 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
594 /* Set %fs and %gs to the value saved by the last CallFrom16 */
595 fprintf( outfile, "\tpushw %d(%%ebp)\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
596 fprintf( outfile, "\tpopw %%fs\n" );
597 fprintf( outfile, "\tpushw %d(%%ebp)\n", STACK16OFFSET(gs)-STACK16OFFSET(bp) );
598 fprintf( outfile, "\tpopw %%gs\n" );
600 /* Set %ds and %es (and %ax just in case) equal to %ss */
601 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
602 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
603 fprintf( outfile, "\tmovw %%ax,%%es\n" );
606 /* Jump to the called routine */
607 fprintf( outfile, "\t.byte 0x66\n" );
608 fprintf( outfile, "\tlret\n" );
610 /* Function footer */
611 function_footer( outfile, name );
615 /*******************************************************************
616 * BuildRet16Func
618 * Build the return code for 16-bit callbacks
620 static void BuildRet16Func( FILE *outfile )
622 function_header( outfile, "CallTo16_Ret" );
624 /* Save %esp into %esi */
625 fprintf( outfile, "\tmovl %%esp,%%esi\n" );
627 /* Restore 32-bit segment registers */
629 fprintf( outfile, "\t.byte 0x2e\n\tmovl " __ASM_NAME("CallTo16_DataSelector") "-" __ASM_NAME("Call16_Ret_Start") ",%%edi\n" );
630 #ifdef __svr4__
631 fprintf( outfile, "\tdata16\n");
632 #endif
633 fprintf( outfile, "\tmovw %%di,%%ds\n" );
634 #ifdef __svr4__
635 fprintf( outfile, "\tdata16\n");
636 #endif
637 fprintf( outfile, "\tmovw %%di,%%es\n" );
639 fprintf( outfile, "\t.byte 0x2e\n\tmovl " __ASM_NAME("CallTo16_TebSelector") "-" __ASM_NAME("Call16_Ret_Start") ",%%fs\n" );
641 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%gs\n", STRUCTOFFSET(TEB,gs_sel) );
643 /* Restore the 32-bit stack */
645 #ifdef __svr4__
646 fprintf( outfile, "\tdata16\n");
647 #endif
648 fprintf( outfile, "\tmovw %%di,%%ss\n" );
649 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
651 /* Return to caller */
653 fprintf( outfile, "\tlret\n" );
655 /* Function footer */
656 function_footer( outfile, "CallTo16_Ret" );
658 /* Declare the return address and data selector variables */
660 fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
661 fprintf( outfile, "\t.globl " __ASM_NAME("CallTo16_DataSelector") "\n" );
662 fprintf( outfile, __ASM_NAME("CallTo16_DataSelector") ":\t.long 0\n" );
663 fprintf( outfile, "\t.globl " __ASM_NAME("CallTo16_TebSelector") "\n" );
664 fprintf( outfile, __ASM_NAME("CallTo16_TebSelector") ":\t.long 0\n" );
668 /*******************************************************************
669 * BuildCallTo32CBClient
671 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
673 * Since the relay stub is itself 32-bit, this should not be a problem;
674 * unfortunately, the relay stubs are expected to switch back to a
675 * 16-bit stack (and 16-bit code) after completion :-(
677 * This would conflict with our 16- vs. 32-bit stack handling, so
678 * we simply switch *back* to our 32-bit stack before returning to
679 * the caller ...
681 * The CBClient relay stub expects to be called with the following
682 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
683 * stack at the designated places:
685 * ...
686 * (ebp+14) original arguments to the callback routine
687 * (ebp+10) far return address to original caller
688 * (ebp+6) Thunklet target address
689 * (ebp+2) Thunklet relay ID code
690 * (ebp) BP (saved by CBClientGlueSL)
691 * (ebp-2) SI (saved by CBClientGlueSL)
692 * (ebp-4) DI (saved by CBClientGlueSL)
693 * (ebp-6) DS (saved by CBClientGlueSL)
695 * ... buffer space used by the 16-bit side glue for temp copies
697 * (ebx+4) far return address to 16-bit side glue code
698 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
700 * The 32-bit side glue code accesses both the original arguments (via ebp)
701 * and the temporary copies prepared by the 16-bit side glue (via ebx).
702 * After completion, the stub will load ss:sp from the buffer at ebx
703 * and perform a far return to 16-bit code.
705 * To trick the relay stub into returning to us, we replace the 16-bit
706 * return address to the glue code by a cs:ip pair pointing to our
707 * return entry point (the original return address is saved first).
708 * Our return stub thus called will then reload the 32-bit ss:esp and
709 * return to 32-bit code (by using and ss:esp value that we have also
710 * pushed onto the 16-bit stack before and a cs:eip values found at
711 * that position on the 32-bit stack). The ss:esp to be restored is
712 * found relative to the 16-bit stack pointer at:
714 * (ebx-4) ss (flat)
715 * (ebx-8) sp (32-bit stack pointer)
717 * The second variant of this routine, CALL32_CBClientEx, which is used
718 * to implement KERNEL.621, has to cope with yet another problem: Here,
719 * the 32-bit side directly returns to the caller of the CBClient thunklet,
720 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
721 * As we have to return to our 32-bit code first, we have to adapt the
722 * layout of our temporary area so as to include values for the registers
723 * that are to be restored, and later (in the implementation of KERNEL.621)
724 * we *really* restore them. The return stub restores DS, DI, SI, and BP
725 * from the stack, skips the next 8 bytes (CBClient relay code / target),
726 * and then performs a lret NN, where NN is the number of arguments to be
727 * removed. Thus, we prepare our temporary area as follows:
729 * (ebx+22) 16-bit cs (this segment)
730 * (ebx+20) 16-bit ip ('16-bit' return entry point)
731 * (ebx+16) 32-bit ss (flat)
732 * (ebx+12) 32-bit sp (32-bit stack pointer)
733 * (ebx+10) 16-bit bp (points to ebx+24)
734 * (ebx+8) 16-bit si (ignored)
735 * (ebx+6) 16-bit di (ignored)
736 * (ebx+4) 16-bit ds (we actually use the flat DS here)
737 * (ebx+2) 16-bit ss (16-bit stack segment)
738 * (ebx+0) 16-bit sp (points to ebx+4)
740 * Note that we ensure that DS is not changed and remains the flat segment,
741 * and the 32-bit stack pointer our own return stub needs fits just
742 * perfectly into the 8 bytes that are skipped by the Windows stub.
743 * One problem is that we have to determine the number of removed arguments,
744 * as these have to be really removed in KERNEL.621. Thus, the BP value
745 * that we place in the temporary area to be restored, contains the value
746 * that SP would have if no arguments were removed. By comparing the actual
747 * value of SP with this value in our return stub we can compute the number
748 * of removed arguments. This is then returned to KERNEL.621.
750 * The stack layout of this function:
751 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
752 * (ebp+16) esi pointer to caller's esi value
753 * (ebp+12) arg ebp value to be set for relay stub
754 * (ebp+8) func CBClient relay stub address
755 * (ebp+4) ret addr
756 * (ebp) ebp
758 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
760 const char *name = isEx? "CBClientEx" : "CBClient";
761 int size = isEx? 24 : 12;
763 /* Function header */
765 fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
766 fprintf( outfile, "\t.globl " __ASM_NAME("CALL32_%s") "\n", name );
767 fprintf( outfile, __ASM_NAME("CALL32_%s") ":\n", name );
769 /* Entry code */
771 fprintf( outfile, "\tpushl %%ebp\n" );
772 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
773 fprintf( outfile, "\tpushl %%edi\n" );
774 fprintf( outfile, "\tpushl %%esi\n" );
775 fprintf( outfile, "\tpushl %%ebx\n" );
777 /* Get the 16-bit stack */
779 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
781 /* Convert it to a flat address */
783 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
784 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
785 fprintf( outfile, "\tshrl $1,%%eax\n" );
786 fprintf( outfile, "\tmovl " __ASM_NAME("wine_ldt_copy") "(%%eax),%%esi\n" );
787 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
788 fprintf( outfile, "\taddl %%eax,%%esi\n" );
790 /* Allocate temporary area (simulate STACK16_PUSH) */
792 fprintf( outfile, "\tpushf\n" );
793 fprintf( outfile, "\tcld\n" );
794 fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
795 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
796 fprintf( outfile, "\trep\n\tmovsb\n" );
797 fprintf( outfile, "\tpopf\n" );
799 fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
801 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
803 /* Set up temporary area */
805 if ( !isEx )
807 fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
809 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
810 fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
812 fprintf( outfile, "\tmovw %%ss, %%ax\n" );
813 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
814 fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
816 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
817 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
819 fprintf( outfile, "\tmovl " __ASM_NAME("CALL32_%s_RetAddr") ", %%eax\n", name );
820 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
822 else
824 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
825 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
827 fprintf( outfile, "\tmovw %%ds, %%ax\n" );
828 fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
830 fprintf( outfile, "\taddl $20, %%ebx\n" );
831 fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
833 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
834 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
836 fprintf( outfile, "\tmovw %%ss, %%ax\n" );
837 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
838 fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
840 fprintf( outfile, "\tmovl " __ASM_NAME("CALL32_%s_RetAddr") ", %%eax\n", name );
841 fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
844 /* Set up registers and call CBClient relay stub (simulating a far call) */
846 fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
847 fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
849 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
850 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
851 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
853 fprintf( outfile, "\tpushl %%cs\n" );
854 fprintf( outfile, "\tcall *%%eax\n" );
856 /* Return new esi value to caller */
858 fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
859 fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
861 /* Cleanup temporary area (simulate STACK16_POP) */
863 fprintf( outfile, "\tpop %%esi\n" );
865 fprintf( outfile, "\tpushf\n" );
866 fprintf( outfile, "\tstd\n" );
867 fprintf( outfile, "\tdec %%esi\n" );
868 fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
869 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
870 fprintf( outfile, "\trep\n\tmovsb\n" );
871 fprintf( outfile, "\tpopf\n" );
873 fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
875 /* Return argument size to caller */
876 if ( isEx )
878 fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
879 fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
882 /* Restore registers and return */
884 fprintf( outfile, "\tpopl %%ebx\n" );
885 fprintf( outfile, "\tpopl %%esi\n" );
886 fprintf( outfile, "\tpopl %%edi\n" );
887 fprintf( outfile, "\tpopl %%ebp\n" );
888 fprintf( outfile, "\tret\n" );
889 fprintf( outfile, ".size " __ASM_NAME("CALL32_%s") ", . - " __ASM_NAME("CALL32_%s") "\n", name, name );
892 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
894 const char *name = isEx? "CBClientEx" : "CBClient";
896 /* '16-bit' return stub */
898 fprintf( outfile, "\n\t.globl " __ASM_NAME("CALL32_%s_Ret") "\n", name );
899 fprintf( outfile, __ASM_NAME("CALL32_%s_Ret") ":\n", name );
901 if ( !isEx )
903 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
904 fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
906 else
908 fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
909 fprintf( outfile, "\tsubw %%bp, %%sp\n" );
910 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
911 fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
913 fprintf( outfile, "\tlret\n" );
915 fprintf( outfile, ".size " __ASM_NAME("CALL32_%s_Ret") ", . - " __ASM_NAME("CALL32_%s_Ret") "\n", name, name );
917 /* Declare the return address variable */
919 fprintf( outfile, "\n\t.globl " __ASM_NAME("CALL32_%s_RetAddr") "\n", name );
920 fprintf( outfile, __ASM_NAME("CALL32_%s_RetAddr") ":\t.long 0\n", name );
924 /*******************************************************************
925 * BuildCallFrom32Regs
927 * Build a 32-bit-to-Wine call-back function for a 'register' function.
928 * 'args' is the number of dword arguments.
930 * Stack layout:
931 * ...
932 * (ebp+12) first arg
933 * (ebp+8) ret addr to user code
934 * (ebp+4) ret addr to relay code
935 * (ebp+0) saved ebp
936 * (ebp-128) buffer area to allow stack frame manipulation
937 * (ebp-332) CONTEXT86 struct
938 * (ebp-336) CONTEXT86 *argument
939 * .... other arguments copied from (ebp+12)
941 * The entry point routine is called with a CONTEXT* extra argument,
942 * following the normal args. In this context structure, EIP_reg
943 * contains the return address to user code, and ESP_reg the stack
944 * pointer on return (with the return address and arguments already
945 * removed).
947 static void BuildCallFrom32Regs( FILE *outfile )
949 static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
951 /* Function header */
953 function_header( outfile, "__wine_call_from_32_regs" );
955 /* Allocate some buffer space on the stack */
957 fprintf( outfile, "\tpushl %%ebp\n" );
958 fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
959 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
961 /* Build the context structure */
963 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
964 fprintf( outfile, "\tpushfl\n" );
965 fprintf( outfile, "\tpopl %%eax\n" );
966 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
967 fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
968 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
969 fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
970 fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
971 fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
972 fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
973 fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
975 fprintf( outfile, "\txorl %%eax,%%eax\n" );
976 fprintf( outfile, "\tmovw %%cs,%%ax\n" );
977 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
978 fprintf( outfile, "\tmovw %%es,%%ax\n" );
979 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
980 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
981 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
982 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
983 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
984 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
985 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
986 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
987 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
988 fprintf( outfile, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
990 fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
991 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
993 fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
994 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
996 /* Transfer the arguments */
998 fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
999 fprintf( outfile, "\tpushl %%esp\n" ); /* push ptr to context struct */
1000 fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
1001 fprintf( outfile, "\tjecxz 1f\n" );
1002 fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
1003 fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
1004 fprintf( outfile, "\tmovl %%esp,%%edi\n" );
1005 fprintf( outfile, "\tshrl $2,%%ecx\n" );
1006 fprintf( outfile, "\tcld\n" );
1007 fprintf( outfile, "\trep\n\tmovsl\n" ); /* copy args */
1009 fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
1010 fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
1011 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1013 /* Call the entry point */
1015 fprintf( outfile, "\tcall *0(%%ebx)\n" );
1016 fprintf( outfile, "\tleal -%d(%%ebp),%%ecx\n", STACK_SPACE );
1018 /* Restore the context structure */
1020 fprintf( outfile, "2:\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegEs) );
1021 fprintf( outfile, "\tpopl %%es\n" );
1022 fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegFs) );
1023 fprintf( outfile, "\tpopl %%fs\n" );
1024 fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegGs) );
1025 fprintf( outfile, "\tpopl %%gs\n" );
1027 fprintf( outfile, "\tmovl %d(%%ecx),%%edi\n", CONTEXTOFFSET(Edi) );
1028 fprintf( outfile, "\tmovl %d(%%ecx),%%esi\n", CONTEXTOFFSET(Esi) );
1029 fprintf( outfile, "\tmovl %d(%%ecx),%%edx\n", CONTEXTOFFSET(Edx) );
1030 fprintf( outfile, "\tmovl %d(%%ecx),%%ebx\n", CONTEXTOFFSET(Ebx) );
1031 fprintf( outfile, "\tmovl %d(%%ecx),%%eax\n", CONTEXTOFFSET(Eax) );
1032 fprintf( outfile, "\tmovl %d(%%ecx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1034 fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegSs) );
1035 fprintf( outfile, "\tpopl %%ss\n" );
1036 fprintf( outfile, "\tmovl %d(%%ecx),%%esp\n", CONTEXTOFFSET(Esp) );
1038 fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(EFlags) );
1039 fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegCs) );
1040 fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(Eip) );
1041 fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegDs) );
1042 fprintf( outfile, "\tmovl %d(%%ecx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1044 fprintf( outfile, "\tpopl %%ds\n" );
1045 fprintf( outfile, "\tiret\n" );
1046 function_footer( outfile, "__wine_call_from_32_regs" );
1048 function_header( outfile, "__wine_call_from_32_restore_regs" );
1049 fprintf( outfile, "\tleal 4(%%esp),%%ecx\n" );
1050 fprintf( outfile, "\tjmp 2b\n" );
1051 function_footer( outfile, "__wine_call_from_32_restore_regs" );
1055 /*******************************************************************
1056 * BuildPendingEventCheck
1058 * Build a function that checks whether there are any
1059 * pending DPMI events.
1061 * Stack layout:
1063 * (sp+12) long eflags
1064 * (sp+6) long cs
1065 * (sp+2) long ip
1066 * (sp) word fs
1068 * On entry to function, fs register points to a valid TEB.
1069 * On exit from function, stack will be popped.
1071 static void BuildPendingEventCheck( FILE *outfile )
1073 /* Function header */
1075 function_header( outfile, "DPMI_PendingEventCheck" );
1077 /* Check for pending events. */
1079 fprintf( outfile, "\t.byte 0x64\n\ttestl $0xffffffff,(%d)\n",
1080 STRUCTOFFSET(TEB,vm86_pending) );
1081 fprintf( outfile, "\tje " __ASM_NAME("DPMI_PendingEventCheck_Cleanup") "\n" );
1083 fprintf( outfile, "\t.byte 0x64\n\ttestl $0xffffffff,(%d)\n",
1084 STRUCTOFFSET(TEB,dpmi_vif) );
1086 fprintf( outfile, "\tje " __ASM_NAME("DPMI_PendingEventCheck_Cleanup") "\n" );
1088 /* Process pending events. */
1090 fprintf( outfile, "\tsti\n" );
1092 /* Start cleanup. Restore fs register. */
1094 fprintf( outfile, ".globl " __ASM_NAME("DPMI_PendingEventCheck_Cleanup") "\n" );
1095 fprintf( outfile, __ASM_NAME("DPMI_PendingEventCheck_Cleanup") ":\n" );
1096 fprintf( outfile, "\tpopw %%fs\n" );
1098 /* Return from function. */
1100 fprintf( outfile, ".globl " __ASM_NAME("DPMI_PendingEventCheck_Return") "\n" );
1101 fprintf( outfile, __ASM_NAME("DPMI_PendingEventCheck_Return") ":\n" );
1102 fprintf( outfile, "\tiret\n" );
1104 function_footer( outfile, "DPMI_PendingEventCheck" );
1108 /*******************************************************************
1109 * BuildRelays16
1111 * Build all the 16-bit relay callbacks
1113 void BuildRelays16( FILE *outfile )
1115 /* File header */
1117 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
1118 fprintf( outfile, "\t.text\n" );
1120 fprintf( outfile, __ASM_NAME("__wine_spec_thunk_text_16") ":\n\n" );
1122 fprintf( outfile, __ASM_NAME("Call16_Start") ":\n" );
1123 fprintf( outfile, "\t.globl " __ASM_NAME("Call16_Start") "\n" );
1124 fprintf( outfile, "\t.byte 0\n\n" );
1126 /* Standard CallFrom16 routine (WORD return) */
1127 BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
1129 /* Standard CallFrom16 routine (DWORD return) */
1130 BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
1132 /* Register CallFrom16 routine */
1133 BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
1135 /* C16ThkSL CallFrom16 routine */
1136 BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
1138 /* Standard CallTo16 routine */
1139 BuildCallTo16Core( outfile, 0 );
1141 /* Register CallTo16 routine */
1142 BuildCallTo16Core( outfile, 1 );
1144 /* CBClientThunkSL routine */
1145 BuildCallTo32CBClient( outfile, FALSE );
1147 /* CBClientThunkSLEx routine */
1148 BuildCallTo32CBClient( outfile, TRUE );
1150 fprintf( outfile, __ASM_NAME("Call16_End") ":\n" );
1151 fprintf( outfile, "\t.globl " __ASM_NAME("Call16_End") "\n" );
1152 fprintf( outfile, "\t.size " __ASM_NAME("__wine_spec_thunk_text_16") ",. - " __ASM_NAME("__wine_spec_thunk_text_16") "\n" );
1154 /* The whole Call16_Ret segment must lie within the .data section */
1155 fprintf( outfile, "\n\t.data\n" );
1156 fprintf( outfile, __ASM_NAME("__wine_spec_thunk_data_16") ":\n\n" );
1157 fprintf( outfile, "\t.globl " __ASM_NAME("Call16_Ret_Start") "\n" );
1158 fprintf( outfile, __ASM_NAME("Call16_Ret_Start") ":\n" );
1160 /* Standard CallTo16 return stub */
1161 BuildRet16Func( outfile );
1163 /* CBClientThunkSL return stub */
1164 BuildCallTo32CBClientRet( outfile, FALSE );
1166 /* CBClientThunkSLEx return stub */
1167 BuildCallTo32CBClientRet( outfile, TRUE );
1169 /* Pending DPMI events check stub */
1170 BuildPendingEventCheck( outfile );
1172 /* End of Call16_Ret segment */
1173 fprintf( outfile, "\n\t.globl " __ASM_NAME("Call16_Ret_End") "\n" );
1174 fprintf( outfile, __ASM_NAME("Call16_Ret_End") ":\n" );
1175 fprintf( outfile, "\t.size " __ASM_NAME("__wine_spec_thunk_data_16") ",. - " __ASM_NAME("__wine_spec_thunk_data_16") "\n" );
1178 /*******************************************************************
1179 * BuildRelays32
1181 * Build all the 32-bit relay callbacks
1183 void BuildRelays32( FILE *outfile )
1185 /* File header */
1187 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
1188 fprintf( outfile, "\t.text\n" );
1189 fprintf( outfile, __ASM_NAME("__wine_spec_thunk_text_32") ":\n\n" );
1191 /* 32-bit register entry point */
1192 BuildCallFrom32Regs( outfile );
1194 fprintf( outfile, "\t.size " __ASM_NAME("__wine_spec_thunk_text_32") ",. - " __ASM_NAME("__wine_spec_thunk_text_32") "\n" );
1197 #else /* __i386__ */
1199 void BuildRelays16( FILE *outfile )
1201 fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" );
1204 void BuildRelays32( FILE *outfile )
1206 fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" );
1209 #endif /* __i386__ */