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