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