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