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