server: Don't round up the header size for image mappings.
[wine] / tools / winebuild / relay.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <ctype.h>
29
30 #include "thread.h"
31 #include "wine/winbase16.h"
32
33 #include "build.h"
34
35 static void function_header( FILE *outfile, const char *name )
36 {
37     fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
38     fprintf( outfile, "\t%s\n", func_declaration(name) );
39     fprintf( outfile, "%s\n", asm_globl(name) );
40 }
41
42
43 static inline const char *data16_prefix(void)
44 {
45     return (target_platform == PLATFORM_SVR4) ? "\tdata16\n" : "";
46 }
47
48 /*******************************************************************
49  *         BuildCallFrom16Core
50  *
51  * This routine builds the core routines used in 16->32 thunks:
52  * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
53  *
54  * These routines are intended to be called via a far call (with 32-bit
55  * operand size) from 16-bit code.  The 16-bit code stub must push %bp,
56  * the 32-bit entry point to be called, and the argument conversion
57  * routine to be used (see stack layout below).
58  *
59  * The core routine completes the STACK16FRAME on the 16-bit stack and
60  * switches to the 32-bit stack.  Then, the argument conversion routine
61  * is called; it gets passed the 32-bit entry point and a pointer to the
62  * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
63  * use conversion routines automatically generated by BuildCallFrom16,
64  * or write your own for special purposes.)
65  *
66  * The conversion routine must call the 32-bit entry point, passing it
67  * the converted arguments, and return its return value to the core.
68  * After the conversion routine has returned, the core switches back
69  * to the 16-bit stack, converts the return value to the DX:AX format
70  * (CallFrom16Long), and returns to the 16-bit call stub.  All parameters,
71  * including %bp, are popped off the stack.
72  *
73  * The 16-bit call stub now returns to the caller, popping the 16-bit
74  * arguments if necessary (pascal calling convention).
75  *
76  * In the case of a 'register' function, CallFrom16Register fills a
77  * CONTEXT86 structure with the values all registers had at the point
78  * the first instruction of the 16-bit call stub was about to be
79  * executed.  A pointer to this CONTEXT86 is passed as third parameter
80  * to the argument conversion routine, which typically passes it on
81  * to the called 32-bit entry point.
82  *
83  * CallFrom16Thunk is a special variant used by the implementation of
84  * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
85  * implemented as follows:
86  * On entry, the EBX register is set up to contain a flat pointer to the
87  * 16-bit stack such that EBX+22 points to the first argument.
88  * Then, the entry point is called, while EBP is set up to point
89  * to the return address (on the 32-bit stack).
90  * The called function returns with CX set to the number of bytes
91  * to be popped of the caller's stack.
92  *
93  * Stack layout upon entry to the core routine (STACK16FRAME):
94  *  ...           ...
95  * (sp+24) word   first 16-bit arg
96  * (sp+22) word   cs
97  * (sp+20) word   ip
98  * (sp+18) word   bp
99  * (sp+14) long   32-bit entry point (reused for Win16 mutex recursion count)
100  * (sp+12) word   ip of actual entry point (necessary for relay debugging)
101  * (sp+8)  long   relay (argument conversion) function entry point
102  * (sp+4)  long   cs of 16-bit entry point
103  * (sp)    long   ip of 16-bit entry point
104  *
105  * Added on the stack:
106  * (sp-2)  word   saved gs
107  * (sp-4)  word   saved fs
108  * (sp-6)  word   saved es
109  * (sp-8)  word   saved ds
110  * (sp-12) long   saved ebp
111  * (sp-16) long   saved ecx
112  * (sp-20) long   saved edx
113  * (sp-24) long   saved previous stack
114  */
115 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk )
116 {
117     /* Function header */
118     if (thunk) function_header( outfile, "__wine_call_from_16_thunk" );
119     else if (reg_func) function_header( outfile, "__wine_call_from_16_regs" );
120     else function_header( outfile, "__wine_call_from_16" );
121
122     /* Create STACK16FRAME (except STACK32FRAME link) */
123     fprintf( outfile, "\tpushw %%gs\n" );
124     fprintf( outfile, "\tpushw %%fs\n" );
125     fprintf( outfile, "\tpushw %%es\n" );
126     fprintf( outfile, "\tpushw %%ds\n" );
127     fprintf( outfile, "\tpushl %%ebp\n" );
128     fprintf( outfile, "\tpushl %%ecx\n" );
129     fprintf( outfile, "\tpushl %%edx\n" );
130
131     /* Save original EFlags register */
132     if (reg_func) fprintf( outfile, "\tpushfl\n" );
133
134     if ( UsePIC )
135     {
136         fprintf( outfile, "\tcall 1f\n" );
137         fprintf( outfile, "1:\tpopl %%ecx\n" );
138         fprintf( outfile, "\t.byte 0x2e\n\tmovl %s-1b(%%ecx),%%edx\n",
139                  asm_name("CallTo16_DataSelector") );
140     }
141     else
142         fprintf( outfile, "\t.byte 0x2e\n\tmovl %s,%%edx\n", asm_name("CallTo16_DataSelector") );
143
144     /* Load 32-bit segment registers */
145     fprintf( outfile, "%s\tmovw %%dx, %%ds\n", data16_prefix() );
146     fprintf( outfile, "%s\tmovw %%dx, %%es\n", data16_prefix() );
147
148     if ( UsePIC )
149         fprintf( outfile, "\tmovw %s-1b(%%ecx), %%fs\n", asm_name("CallTo16_TebSelector") );
150     else
151         fprintf( outfile, "\tmovw %s, %%fs\n", asm_name("CallTo16_TebSelector") );
152
153     fprintf( outfile, "\t.byte 0x64\n\tmov (%d),%%gs\n", STRUCTOFFSET(TEB,gs_sel) );
154
155     /* Translate STACK16FRAME base to flat offset in %edx */
156     fprintf( outfile, "\tmovw %%ss, %%dx\n" );
157     fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
158     fprintf( outfile, "\tshrl $1, %%edx\n" );
159     if (UsePIC)
160     {
161         fprintf( outfile, "\taddl wine_ldt_copy_ptr-1b(%%ecx),%%edx\n" );
162         fprintf( outfile, "\tmovl (%%edx), %%edx\n" );
163     }
164     else
165         fprintf( outfile, "\tmovl %s(%%edx), %%edx\n", asm_name("wine_ldt_copy") );
166     fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
167     fprintf( outfile, "\tleal %d(%%ebp,%%edx), %%edx\n", reg_func ? 0 : -4 );
168
169     /* Get saved flags into %ecx */
170     if (reg_func) fprintf( outfile, "\tpopl %%ecx\n" );
171
172     /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
173     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
174     fprintf( outfile, "\tpushl %%ebp\n" );
175
176     /* Switch stacks */
177     fprintf( outfile, "%s\t.byte 0x64\n\tmovw %%ss, (%d)\n", data16_prefix(), STACKOFFSET + 2 );
178     fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
179     fprintf( outfile, "\tpushl %%ds\n" );
180     fprintf( outfile, "\tpopl %%ss\n" );
181     fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
182     fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
183
184
185     /* At this point:
186        STACK16FRAME is completely set up
187        DS, ES, SS: flat data segment
188        FS: current TEB
189        ESP: points to last STACK32FRAME
190        EBP: points to ebp member of last STACK32FRAME
191        EDX: points to current STACK16FRAME
192        ECX: contains saved flags
193        all other registers: unchanged */
194
195     /* Special case: C16ThkSL stub */
196     if ( thunk )
197     {
198         /* Set up registers as expected and call thunk */
199         fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
200         fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
201
202         fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
203
204         /* Switch stack back */
205         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
206         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
207         fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
208
209         /* Restore registers and return directly to caller */
210         fprintf( outfile, "\taddl $8, %%esp\n" );
211         fprintf( outfile, "\tpopl %%ebp\n" );
212         fprintf( outfile, "\tpopw %%ds\n" );
213         fprintf( outfile, "\tpopw %%es\n" );
214         fprintf( outfile, "\tpopw %%fs\n" );
215         fprintf( outfile, "\tpopw %%gs\n" );
216         fprintf( outfile, "\taddl $20, %%esp\n" );
217
218         fprintf( outfile, "\txorb %%ch, %%ch\n" );
219         fprintf( outfile, "\tpopl %%ebx\n" );
220         fprintf( outfile, "\taddw %%cx, %%sp\n" );
221         fprintf( outfile, "\tpush %%ebx\n" );
222
223         fprintf( outfile, "\t.byte 0x66\n" );
224         fprintf( outfile, "\tlret\n" );
225
226         return;
227     }
228
229
230     /* Build register CONTEXT */
231     if ( reg_func )
232     {
233         fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
234
235         fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
236
237         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
238         fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
239         fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
240         fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
241
242         fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
243         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
244         fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
245         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
246         fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
247         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
248
249         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
250         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
251         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
252         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
253         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
254         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
255         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
256         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
257
258         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
259         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
260         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
261         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
262
263         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
264         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
265         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
266         fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
267         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
268 #if 0
269         fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
270 #endif
271
272         /* Push address of CONTEXT86 structure -- popped by the relay routine */
273         fprintf( outfile, "\tmovl %%esp,%%eax\n" );
274         fprintf( outfile, "\tandl $~15,%%esp\n" );
275         fprintf( outfile, "\tsubl $4,%%esp\n" );
276         fprintf( outfile, "\tpushl %%eax\n" );
277     }
278     else
279     {
280         fprintf( outfile, "\tsubl $8,%%esp\n" );
281         fprintf( outfile, "\tandl $~15,%%esp\n" );
282         fprintf( outfile, "\taddl $8,%%esp\n" );
283     }
284
285     /* Call relay routine (which will call the API entry point) */
286     fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
287     fprintf( outfile, "\tpushl %%eax\n" );
288     fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
289     fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
290
291     if ( reg_func )
292     {
293         fprintf( outfile, "\tleal -%d(%%ebp), %%ebx\n",
294                  sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
295
296         /* Switch stack back */
297         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
298         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
299         fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
300
301         /* Get return address to CallFrom16 stub */
302         fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
303         fprintf( outfile, "\tpopl %%eax\n" );
304         fprintf( outfile, "\tpopl %%edx\n" );
305
306         /* Restore all registers from CONTEXT */
307         fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
308         fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
309         fprintf( outfile, "\taddl $4, %%esp\n" );  /* room for final return address */
310
311         fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
312         fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
313         fprintf( outfile, "\tpushl %%edx\n" );
314         fprintf( outfile, "\tpushl %%eax\n" );
315         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
316         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
317
318         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegEs) );
319         fprintf( outfile, "\tpopl %%es\n" );
320         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegFs) );
321         fprintf( outfile, "\tpopl %%fs\n" );
322         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegGs) );
323         fprintf( outfile, "\tpopl %%gs\n" );
324
325         fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
326         fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
327         fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
328         fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
329         fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
330         fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
331         fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
332
333         fprintf( outfile, "\tpopl %%ds\n" );
334         fprintf( outfile, "\tpopfl\n" );
335         fprintf( outfile, "\tlret\n" );
336     }
337     else
338     {
339         /* Switch stack back */
340         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
341         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
342         fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
343
344         /* Restore registers */
345         fprintf( outfile, "\tpopl %%edx\n" );
346         fprintf( outfile, "\tpopl %%ecx\n" );
347         fprintf( outfile, "\tpopl %%ebp\n" );
348         fprintf( outfile, "\tpopw %%ds\n" );
349         fprintf( outfile, "\tpopw %%es\n" );
350         fprintf( outfile, "\tpopw %%fs\n" );
351         fprintf( outfile, "\tpopw %%gs\n" );
352
353         /* Return to return stub which will return to caller */
354         fprintf( outfile, "\tlret $12\n" );
355     }
356     if (thunk) output_function_size( outfile, "__wine_call_from_16_thunk" );
357     else if (reg_func) output_function_size( outfile, "__wine_call_from_16_regs" );
358     else output_function_size( outfile, "__wine_call_from_16" );
359 }
360
361
362 /*******************************************************************
363  *         BuildCallTo16Core
364  *
365  * This routine builds the core routines used in 32->16 thunks:
366  *
367  * extern DWORD WINAPI wine_call_to_16( FARPROC16 target, DWORD cbArgs, PEXCEPTION_HANDLER handler );
368  * extern void WINAPI wine_call_to_16_regs( CONTEXT86 *context, DWORD cbArgs, PEXCEPTION_HANDLER handler );
369  *
370  * These routines can be called directly from 32-bit code.
371  *
372  * All routines expect that the 16-bit stack contents (arguments) and the
373  * return address (segptr to CallTo16_Ret) were already set up by the
374  * caller; nb_args must contain the number of bytes to be conserved.  The
375  * 16-bit SS:SP will be set accordinly.
376  *
377  * All other registers are either taken from the CONTEXT86 structure
378  * or else set to default values.  The target routine address is either
379  * given directly or taken from the CONTEXT86.
380  */
381 static void BuildCallTo16Core( FILE *outfile, int reg_func )
382 {
383     const char *name = reg_func ? "wine_call_to_16_regs" : "wine_call_to_16";
384
385     /* Function header */
386     function_header( outfile, name );
387
388     /* Function entry sequence */
389     fprintf( outfile, "\tpushl %%ebp\n" );
390     fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
391
392     /* Save the 32-bit registers */
393     fprintf( outfile, "\tpushl %%ebx\n" );
394     fprintf( outfile, "\tpushl %%esi\n" );
395     fprintf( outfile, "\tpushl %%edi\n" );
396     fprintf( outfile, "\t.byte 0x64\n\tmov %%gs,(%d)\n", STRUCTOFFSET(TEB,gs_sel) );
397
398     /* Setup exception frame */
399     fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
400     fprintf( outfile, "\tpushl 16(%%ebp)\n" ); /* handler */
401     fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STRUCTOFFSET(TEB,Tib.ExceptionList) );
402     fprintf( outfile, "\t.byte 0x64\n\tmovl %%esp,(%d)\n", STRUCTOFFSET(TEB,Tib.ExceptionList) );
403
404     /* Call the actual CallTo16 routine (simulate a lcall) */
405     fprintf( outfile, "\tpushl %%cs\n" );
406     fprintf( outfile, "\tcall .L%s\n", name );
407
408     /* Remove exception frame */
409     fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STRUCTOFFSET(TEB,Tib.ExceptionList) );
410     fprintf( outfile, "\taddl $4, %%esp\n" );
411     fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
412
413     if ( !reg_func )
414     {
415         /* Convert return value */
416         fprintf( outfile, "\tandl $0xffff,%%eax\n" );
417         fprintf( outfile, "\tshll $16,%%edx\n" );
418         fprintf( outfile, "\torl %%edx,%%eax\n" );
419     }
420     else
421     {
422         /*
423          * Modify CONTEXT86 structure to contain new values
424          *
425          * NOTE:  We restore only EAX, EBX, EDX, EDX, EBP, and ESP.
426          *        The segment registers as well as ESI and EDI should
427          *        not be modified by a well-behaved 16-bit routine in
428          *        any case.  [If necessary, we could restore them as well,
429          *        at the cost of a somewhat less efficient return path.]
430          */
431
432         fprintf( outfile, "\tmovl %d(%%esp), %%edi\n", STACK32OFFSET(target) - STACK32OFFSET(edi));
433                 /* everything above edi has been popped already */
434
435         fprintf( outfile, "\tmovl %%eax, %d(%%edi)\n", CONTEXTOFFSET(Eax) );
436         fprintf( outfile, "\tmovl %%ebx, %d(%%edi)\n", CONTEXTOFFSET(Ebx) );
437         fprintf( outfile, "\tmovl %%ecx, %d(%%edi)\n", CONTEXTOFFSET(Ecx) );
438         fprintf( outfile, "\tmovl %%edx, %d(%%edi)\n", CONTEXTOFFSET(Edx) );
439         fprintf( outfile, "\tmovl %%ebp, %d(%%edi)\n", CONTEXTOFFSET(Ebp) );
440         fprintf( outfile, "\tmovl %%esi, %d(%%edi)\n", CONTEXTOFFSET(Esp) );
441                  /* The return glue code saved %esp into %esi */
442     }
443
444     /* Restore the 32-bit registers */
445     fprintf( outfile, "\tpopl %%edi\n" );
446     fprintf( outfile, "\tpopl %%esi\n" );
447     fprintf( outfile, "\tpopl %%ebx\n" );
448
449     /* Function exit sequence */
450     fprintf( outfile, "\tpopl %%ebp\n" );
451     fprintf( outfile, "\tret $12\n" );
452
453
454     /* Start of the actual CallTo16 routine */
455
456     fprintf( outfile, ".L%s:\n", name );
457
458     /* Switch to the 16-bit stack */
459     fprintf( outfile, "\tmovl %%esp,%%edx\n" );
460     fprintf( outfile, "%s\t.byte 0x64\n\tmovw (%d),%%ss\n", data16_prefix(), STACKOFFSET + 2);
461     fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
462     fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
463
464     /* Make %bp point to the previous stackframe (built by CallFrom16) */
465     fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
466     fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
467
468     /* Add the specified offset to the new sp */
469     fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
470
471     if (reg_func)
472     {
473         /* Push the called routine address */
474         fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
475         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
476         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
477
478         /* Get the registers */
479         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
480         fprintf( outfile, "\tpushl %d(%%edx)\n", CONTEXTOFFSET(SegEs) );
481         fprintf( outfile, "\tpopl %%es\n" );
482         fprintf( outfile, "\tpushl %d(%%edx)\n", CONTEXTOFFSET(SegFs) );
483         fprintf( outfile, "\tpopl %%fs\n" );
484         fprintf( outfile, "\tpushl %d(%%edx)\n", CONTEXTOFFSET(SegGs) );
485         fprintf( outfile, "\tpopl %%gs\n" );
486         fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
487         fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
488         fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
489         fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
490         fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
491         fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
492         fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
493
494         /* Get the 16-bit ds */
495         fprintf( outfile, "\tpopw %%ds\n" );
496     }
497     else  /* not a register function */
498     {
499         /* Push the called routine address */
500         fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
501
502         /* Set %fs and %gs to the value saved by the last CallFrom16 */
503         fprintf( outfile, "\tpushw %d(%%ebp)\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
504         fprintf( outfile, "\tpopw %%fs\n" );
505         fprintf( outfile, "\tpushw %d(%%ebp)\n", STACK16OFFSET(gs)-STACK16OFFSET(bp) );
506         fprintf( outfile, "\tpopw %%gs\n" );
507
508         /* Set %ds and %es (and %ax just in case) equal to %ss */
509         fprintf( outfile, "\tmovw %%ss,%%ax\n" );
510         fprintf( outfile, "\tmovw %%ax,%%ds\n" );
511         fprintf( outfile, "\tmovw %%ax,%%es\n" );
512     }
513
514     /* Jump to the called routine */
515     fprintf( outfile, "\t.byte 0x66\n" );
516     fprintf( outfile, "\tlret\n" );
517
518     /* Function footer */
519     output_function_size( outfile, name );
520 }
521
522
523 /*******************************************************************
524  *         BuildRet16Func
525  *
526  * Build the return code for 16-bit callbacks
527  */
528 static void BuildRet16Func( FILE *outfile )
529 {
530     function_header( outfile, "__wine_call_to_16_ret" );
531
532     /* Save %esp into %esi */
533     fprintf( outfile, "\tmovl %%esp,%%esi\n" );
534
535     /* Restore 32-bit segment registers */
536
537     fprintf( outfile, "\t.byte 0x2e\n\tmovl %s", asm_name("CallTo16_DataSelector") );
538     fprintf( outfile, "-%s,%%edi\n", asm_name("__wine_call16_start") );
539     fprintf( outfile, "%s\tmovw %%di,%%ds\n", data16_prefix() );
540     fprintf( outfile, "%s\tmovw %%di,%%es\n", data16_prefix() );
541
542     fprintf( outfile, "\t.byte 0x2e\n\tmov %s", asm_name("CallTo16_TebSelector") );
543     fprintf( outfile, "-%s,%%fs\n", asm_name("__wine_call16_start") );
544
545     fprintf( outfile, "\t.byte 0x64\n\tmov (%d),%%gs\n", STRUCTOFFSET(TEB,gs_sel) );
546
547     /* Restore the 32-bit stack */
548
549     fprintf( outfile, "%s\tmovw %%di,%%ss\n", data16_prefix() );
550     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
551
552     /* Return to caller */
553
554     fprintf( outfile, "\tlret\n" );
555     output_function_size( outfile, "__wine_call_to_16_ret" );
556 }
557
558
559 /*******************************************************************
560  *         BuildCallTo32CBClient
561  *
562  * Call a CBClient relay stub from 32-bit code (KERNEL.620).
563  *
564  * Since the relay stub is itself 32-bit, this should not be a problem;
565  * unfortunately, the relay stubs are expected to switch back to a
566  * 16-bit stack (and 16-bit code) after completion :-(
567  *
568  * This would conflict with our 16- vs. 32-bit stack handling, so
569  * we simply switch *back* to our 32-bit stack before returning to
570  * the caller ...
571  *
572  * The CBClient relay stub expects to be called with the following
573  * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
574  * stack at the designated places:
575  *
576  *    ...
577  *  (ebp+14) original arguments to the callback routine
578  *  (ebp+10) far return address to original caller
579  *  (ebp+6)  Thunklet target address
580  *  (ebp+2)  Thunklet relay ID code
581  *  (ebp)    BP (saved by CBClientGlueSL)
582  *  (ebp-2)  SI (saved by CBClientGlueSL)
583  *  (ebp-4)  DI (saved by CBClientGlueSL)
584  *  (ebp-6)  DS (saved by CBClientGlueSL)
585  *
586  *   ...     buffer space used by the 16-bit side glue for temp copies
587  *
588  *  (ebx+4)  far return address to 16-bit side glue code
589  *  (ebx)    saved 16-bit ss:sp (pointing to ebx+4)
590  *
591  * The 32-bit side glue code accesses both the original arguments (via ebp)
592  * and the temporary copies prepared by the 16-bit side glue (via ebx).
593  * After completion, the stub will load ss:sp from the buffer at ebx
594  * and perform a far return to 16-bit code.
595  *
596  * To trick the relay stub into returning to us, we replace the 16-bit
597  * return address to the glue code by a cs:ip pair pointing to our
598  * return entry point (the original return address is saved first).
599  * Our return stub thus called will then reload the 32-bit ss:esp and
600  * return to 32-bit code (by using and ss:esp value that we have also
601  * pushed onto the 16-bit stack before and a cs:eip values found at
602  * that position on the 32-bit stack).  The ss:esp to be restored is
603  * found relative to the 16-bit stack pointer at:
604  *
605  *  (ebx-4)   ss  (flat)
606  *  (ebx-8)   sp  (32-bit stack pointer)
607  *
608  * The second variant of this routine, CALL32_CBClientEx, which is used
609  * to implement KERNEL.621, has to cope with yet another problem: Here,
610  * the 32-bit side directly returns to the caller of the CBClient thunklet,
611  * restoring registers saved by CBClientGlueSL and cleaning up the stack.
612  * As we have to return to our 32-bit code first, we have to adapt the
613  * layout of our temporary area so as to include values for the registers
614  * that are to be restored, and later (in the implementation of KERNEL.621)
615  * we *really* restore them. The return stub restores DS, DI, SI, and BP
616  * from the stack, skips the next 8 bytes (CBClient relay code / target),
617  * and then performs a lret NN, where NN is the number of arguments to be
618  * removed. Thus, we prepare our temporary area as follows:
619  *
620  *     (ebx+22) 16-bit cs  (this segment)
621  *     (ebx+20) 16-bit ip  ('16-bit' return entry point)
622  *     (ebx+16) 32-bit ss  (flat)
623  *     (ebx+12) 32-bit sp  (32-bit stack pointer)
624  *     (ebx+10) 16-bit bp  (points to ebx+24)
625  *     (ebx+8)  16-bit si  (ignored)
626  *     (ebx+6)  16-bit di  (ignored)
627  *     (ebx+4)  16-bit ds  (we actually use the flat DS here)
628  *     (ebx+2)  16-bit ss  (16-bit stack segment)
629  *     (ebx+0)  16-bit sp  (points to ebx+4)
630  *
631  * Note that we ensure that DS is not changed and remains the flat segment,
632  * and the 32-bit stack pointer our own return stub needs fits just
633  * perfectly into the 8 bytes that are skipped by the Windows stub.
634  * One problem is that we have to determine the number of removed arguments,
635  * as these have to be really removed in KERNEL.621. Thus, the BP value
636  * that we place in the temporary area to be restored, contains the value
637  * that SP would have if no arguments were removed. By comparing the actual
638  * value of SP with this value in our return stub we can compute the number
639  * of removed arguments. This is then returned to KERNEL.621.
640  *
641  * The stack layout of this function:
642  * (ebp+20)  nArgs     pointer to variable receiving nr. of args (Ex only)
643  * (ebp+16)  esi       pointer to caller's esi value
644  * (ebp+12)  arg       ebp value to be set for relay stub
645  * (ebp+8)   func      CBClient relay stub address
646  * (ebp+4)   ret addr
647  * (ebp)     ebp
648  */
649 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
650 {
651     function_header( outfile, isEx ? "CALL32_CBClientEx" : "CALL32_CBClient" );
652
653     /* Entry code */
654
655     fprintf( outfile, "\tpushl %%ebp\n" );
656     fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
657     fprintf( outfile, "\tpushl %%edi\n" );
658     fprintf( outfile, "\tpushl %%esi\n" );
659     fprintf( outfile, "\tpushl %%ebx\n" );
660
661     /* Get pointer to temporary area and save the 32-bit stack pointer */
662
663     fprintf( outfile, "\tmovl 16(%%ebp), %%ebx\n" );
664     fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
665
666     if ( !isEx )
667         fprintf( outfile, "\tmovl %%eax, -8(%%ebx)\n" );
668     else
669         fprintf( outfile, "\tmovl %%eax, 12(%%ebx)\n" );
670
671     /* Set up registers and call CBClient relay stub (simulating a far call) */
672
673     fprintf( outfile, "\tmovl 20(%%ebp), %%esi\n" );
674     fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
675
676     fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
677     fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
678
679     fprintf( outfile, "\tpushl %%cs\n" );
680     fprintf( outfile, "\tcall *%%eax\n" );
681
682     /* Return new esi value to caller */
683
684     fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
685     fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
686
687     /* Return argument size to caller */
688     if ( isEx )
689     {
690         fprintf( outfile, "\tmovl 36(%%esp), %%ebx\n" );
691         fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
692     }
693
694     /* Restore registers and return */
695
696     fprintf( outfile, "\tpopl %%ebx\n" );
697     fprintf( outfile, "\tpopl %%esi\n" );
698     fprintf( outfile, "\tpopl %%edi\n" );
699     fprintf( outfile, "\tpopl %%ebp\n" );
700     fprintf( outfile, "\tret\n" );
701     output_function_size( outfile, isEx ? "CALL32_CBClientEx" : "CALL32_CBClient" );
702
703     /* '16-bit' return stub */
704
705     function_header( outfile, isEx ? "CALL32_CBClientEx_Ret" : "CALL32_CBClient_Ret" );
706     if ( !isEx )
707     {
708         fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
709         fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
710     }
711     else
712     {
713         fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
714         fprintf( outfile, "\tsubw %%bp, %%sp\n" );
715         fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
716         fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
717     }
718     fprintf( outfile, "\tlret\n" );
719     output_function_size( outfile, isEx ? "CALL32_CBClientEx_Ret" : "CALL32_CBClient_Ret" );
720 }
721
722
723 /*******************************************************************
724  *         BuildCallFrom32Regs
725  *
726  * Build a 32-bit-to-Wine call-back function for a 'register' function.
727  * 'args' is the number of dword arguments.
728  *
729  * Stack layout:
730  *   ...
731  * (ebp+16)  first arg
732  * (ebp+12)  ret addr to user code
733  * (ebp+8)   eax saved by relay code
734  * (ebp+4)   ret addr to relay code
735  * (ebp+0)   saved ebp
736  * (ebp-128) buffer area to allow stack frame manipulation
737  * (ebp-332) CONTEXT86 struct
738  * (ebp-336) padding for stack alignment
739  * (ebp-336-n) CONTEXT86 *argument
740  *  ....     other arguments copied from (ebp+12)
741  *
742  * The entry point routine is called with a CONTEXT* extra argument,
743  * following the normal args. In this context structure, EIP_reg
744  * contains the return address to user code, and ESP_reg the stack
745  * pointer on return (with the return address and arguments already
746  * removed).
747  */
748 static void BuildCallFrom32Regs( FILE *outfile )
749 {
750     static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
751
752     /* Function header */
753
754     function_header( outfile, "__wine_call_from_32_regs" );
755
756     /* Allocate some buffer space on the stack */
757
758     fprintf( outfile, "\tpushl %%ebp\n" );
759     fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
760     fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE + 4 /* for context arg */);
761
762     /* Build the context structure */
763
764     fprintf( outfile, "\tpushfl\n" );
765     fprintf( outfile, "\tpopl %%eax\n" );
766     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
767     fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
768     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
769     fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" );
770     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
771     fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
772     fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
773     fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
774     fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
775     fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
776
777     fprintf( outfile, "\txorl %%eax,%%eax\n" );
778     fprintf( outfile, "\tmovw %%cs,%%ax\n" );
779     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
780     fprintf( outfile, "\tmovw %%es,%%ax\n" );
781     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
782     fprintf( outfile, "\tmovw %%fs,%%ax\n" );
783     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
784     fprintf( outfile, "\tmovw %%gs,%%ax\n" );
785     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
786     fprintf( outfile, "\tmovw %%ss,%%ax\n" );
787     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
788     fprintf( outfile, "\tmovw %%ds,%%ax\n" );
789     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
790     fprintf( outfile, "\tmovw %%ax,%%es\n" );  /* set %es equal to %ds just in case */
791
792     fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
793     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
794
795     fprintf( outfile, "\tmovl 12(%%ebp),%%eax\n" ); /* Get %eip at time of call */
796     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
797
798     /* Transfer the arguments */
799
800     fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" );   /* get relay code addr */
801     fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
802     fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
803     fprintf( outfile, "\tandl $~15,%%esp\n" );
804     fprintf( outfile, "\tleal 16(%%ebp),%%esi\n" );  /* get %esp at time of call */
805     fprintf( outfile, "\tmovl %%esp,%%edi\n" );
806     fprintf( outfile, "\tshrl $2,%%ecx\n" );
807     fprintf( outfile, "\tjz 1f\n" );
808     fprintf( outfile, "\tcld\n" );
809     fprintf( outfile, "\trep\n\tmovsl\n" );  /* copy args */
810     fprintf( outfile, "1:\tleal %d(%%ebp),%%eax\n", -STACK_SPACE );  /* get addr of context struct */
811     fprintf( outfile, "\tmovl %%eax,(%%edi)\n" );    /* and pass it as extra arg */
812     fprintf( outfile, "\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
813     fprintf( outfile, "\tleal 16(%%ebp,%%eax),%%eax\n" );
814     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
815
816     /* Call the entry point */
817
818     fprintf( outfile, "\taddl (%%ebx),%%ebx\n" );
819     fprintf( outfile, "\tcall *%%ebx\n" );
820     fprintf( outfile, "\tleal -%d(%%ebp),%%ecx\n", STACK_SPACE );
821
822     /* Restore the context structure */
823
824     fprintf( outfile, "2:\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegEs) );
825     fprintf( outfile, "\tpopl %%es\n" );
826     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegFs) );
827     fprintf( outfile, "\tpopl %%fs\n" );
828     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegGs) );
829     fprintf( outfile, "\tpopl %%gs\n" );
830
831     fprintf( outfile, "\tmovl %d(%%ecx),%%edi\n", CONTEXTOFFSET(Edi) );
832     fprintf( outfile, "\tmovl %d(%%ecx),%%esi\n", CONTEXTOFFSET(Esi) );
833     fprintf( outfile, "\tmovl %d(%%ecx),%%edx\n", CONTEXTOFFSET(Edx) );
834     fprintf( outfile, "\tmovl %d(%%ecx),%%ebx\n", CONTEXTOFFSET(Ebx) );
835     fprintf( outfile, "\tmovl %d(%%ecx),%%eax\n", CONTEXTOFFSET(Eax) );
836     fprintf( outfile, "\tmovl %d(%%ecx),%%ebp\n", CONTEXTOFFSET(Ebp) );
837
838     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegSs) );
839     fprintf( outfile, "\tpopl %%ss\n" );
840     fprintf( outfile, "\tmovl %d(%%ecx),%%esp\n", CONTEXTOFFSET(Esp) );
841
842     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(EFlags) );
843     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegCs) );
844     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(Eip) );
845     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegDs) );
846     fprintf( outfile, "\tmovl %d(%%ecx),%%ecx\n", CONTEXTOFFSET(Ecx) );
847
848     fprintf( outfile, "\tpopl %%ds\n" );
849     fprintf( outfile, "\tiret\n" );
850     output_function_size( outfile, "__wine_call_from_32_regs" );
851
852     function_header( outfile, "__wine_call_from_32_restore_regs" );
853     fprintf( outfile, "\tmovl 4(%%esp),%%ecx\n" );
854     fprintf( outfile, "\tjmp 2b\n" );
855     output_function_size( outfile, "__wine_call_from_32_restore_regs" );
856 }
857
858
859 /*******************************************************************
860  *         BuildPendingEventCheck
861  *
862  * Build a function that checks whether there are any
863  * pending DPMI events.
864  *
865  * Stack layout:
866  *   
867  * (sp+12) long   eflags
868  * (sp+6)  long   cs
869  * (sp+2)  long   ip
870  * (sp)    word   fs
871  *
872  * On entry to function, fs register points to a valid TEB.
873  * On exit from function, stack will be popped.
874  */
875 static void BuildPendingEventCheck( FILE *outfile )
876 {
877     /* Function header */
878
879     function_header( outfile, "DPMI_PendingEventCheck" );
880
881     /* Check for pending events. */
882
883     fprintf( outfile, "\t.byte 0x64\n\ttestl $0xffffffff,(%d)\n",
884              STRUCTOFFSET(TEB,vm86_pending) );
885     fprintf( outfile, "\tje %s\n", asm_name("DPMI_PendingEventCheck_Cleanup") );
886
887     fprintf( outfile, "\t.byte 0x64\n\ttestl $0xffffffff,(%d)\n",
888              STRUCTOFFSET(TEB,dpmi_vif) );
889
890     fprintf( outfile, "\tje %s\n", asm_name("DPMI_PendingEventCheck_Cleanup") );
891
892     /* Process pending events. */
893
894     fprintf( outfile, "\tsti\n" );
895
896     /* Start cleanup. Restore fs register. */
897
898     fprintf( outfile, "%s\n", asm_globl("DPMI_PendingEventCheck_Cleanup") );
899     fprintf( outfile, "\tpopw %%fs\n" );
900
901     /* Return from function. */
902
903     fprintf( outfile, "%s\n", asm_globl("DPMI_PendingEventCheck_Return") );
904     fprintf( outfile, "\tiret\n" );
905
906     output_function_size( outfile, "DPMI_PendingEventCheck" );
907 }
908
909
910 /*******************************************************************
911  *         BuildRelays16
912  *
913  * Build all the 16-bit relay callbacks
914  */
915 void BuildRelays16( FILE *outfile )
916 {
917     if (target_cpu != CPU_x86)
918     {
919         fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" );
920         return;
921     }
922
923     /* File header */
924
925     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
926     fprintf( outfile, "\t.text\n" );
927
928     fprintf( outfile, "%s:\n\n", asm_name("__wine_spec_thunk_text_16") );
929
930     fprintf( outfile, "%s\n", asm_globl("__wine_call16_start") );
931
932     /* Standard CallFrom16 routine */
933     BuildCallFrom16Core( outfile, FALSE, FALSE );
934
935     /* Register CallFrom16 routine */
936     BuildCallFrom16Core( outfile, TRUE, FALSE );
937
938     /* C16ThkSL CallFrom16 routine */
939     BuildCallFrom16Core( outfile, FALSE, TRUE );
940
941     /* Standard CallTo16 routine */
942     BuildCallTo16Core( outfile, 0 );
943
944     /* Register CallTo16 routine */
945     BuildCallTo16Core( outfile, 1 );
946
947     /* Standard CallTo16 return stub */
948     BuildRet16Func( outfile );
949
950     /* CBClientThunkSL routine */
951     BuildCallTo32CBClient( outfile, FALSE );
952
953     /* CBClientThunkSLEx routine */
954     BuildCallTo32CBClient( outfile, TRUE  );
955
956     /* Pending DPMI events check stub */
957     BuildPendingEventCheck( outfile );
958
959     fprintf( outfile, "%s\n", asm_globl("__wine_call16_end") );
960     output_function_size( outfile, "__wine_spec_thunk_text_16" );
961
962     /* Declare the return address and data selector variables */
963     fprintf( outfile, "\n\t.data\n\t.align %d\n", get_alignment(4) );
964     fprintf( outfile, "%s\n\t.long 0\n", asm_globl("CallTo16_DataSelector") );
965     fprintf( outfile, "%s\n\t.long 0\n", asm_globl("CallTo16_TebSelector") );
966     if (UsePIC) fprintf( outfile, "wine_ldt_copy_ptr:\t.long %s\n", asm_name("wine_ldt_copy") );
967 }
968
969 /*******************************************************************
970  *         BuildRelays32
971  *
972  * Build all the 32-bit relay callbacks
973  */
974 void BuildRelays32( FILE *outfile )
975 {
976     if (target_cpu != CPU_x86)
977     {
978         fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" );
979         return;
980     }
981
982     /* File header */
983
984     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
985     fprintf( outfile, "\t.text\n" );
986     fprintf( outfile, "%s:\n\n", asm_name("__wine_spec_thunk_text_32") );
987
988     /* 32-bit register entry point */
989     BuildCallFrom32Regs( outfile );
990
991     output_function_size( outfile, "__wine_spec_thunk_text_32" );
992 }