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