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