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