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
26 #include "wine/port.h"
31 #define __WINESRC__ /* FIXME: for WINE_VM86_TEB_INFO */
33 #include "wine/winbase16.h"
37 /* offset of a structure field relative to the start of the struct */
38 #define STRUCTOFFSET(type,field) ((int)FIELD_OFFSET(type,field))
40 /* offset of register relative to the start of the CONTEXT struct */
41 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT86,reg)
43 /* offset of register relative to the start of the STACK16FRAME struct */
44 #define STACK16OFFSET(reg) STRUCTOFFSET(STACK16FRAME,reg)
46 /* offset of register relative to the start of the STACK32FRAME struct */
47 #define STACK32OFFSET(reg) STRUCTOFFSET(STACK32FRAME,reg)
49 /* offset of the stack pointer relative to %fs:(0) */
50 #define STACKOFFSET 0xc0 /* STRUCTOFFSET(TEB,WOW32Reserved) */
52 /* fix this if the ntdll_thread_regs structure is changed */
53 #define GS_OFFSET 0x1d8 /* STRUCTOFFSET(TEB,SystemReserved2) + STRUCTOFFSET(ntdll_thread_data,gs) */
55 static void function_header( const char *name
)
57 output( "\n\t.align %d\n", get_alignment(4) );
58 output( "\t%s\n", func_declaration(name
) );
59 output( "%s\n", asm_globl(name
) );
63 /*******************************************************************
66 * This routine builds the core routines used in 16->32 thunks:
67 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
69 * These routines are intended to be called via a far call (with 32-bit
70 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
71 * the 32-bit entry point to be called, and the argument conversion
72 * routine to be used (see stack layout below).
74 * The core routine completes the STACK16FRAME on the 16-bit stack and
75 * switches to the 32-bit stack. Then, the argument conversion routine
76 * is called; it gets passed the 32-bit entry point and a pointer to the
77 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
78 * use conversion routines automatically generated by BuildCallFrom16,
79 * or write your own for special purposes.)
81 * The conversion routine must call the 32-bit entry point, passing it
82 * the converted arguments, and return its return value to the core.
83 * After the conversion routine has returned, the core switches back
84 * to the 16-bit stack, converts the return value to the DX:AX format
85 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
86 * including %bp, are popped off the stack.
88 * The 16-bit call stub now returns to the caller, popping the 16-bit
89 * arguments if necessary (pascal calling convention).
91 * In the case of a 'register' function, CallFrom16Register fills a
92 * CONTEXT86 structure with the values all registers had at the point
93 * the first instruction of the 16-bit call stub was about to be
94 * executed. A pointer to this CONTEXT86 is passed as third parameter
95 * to the argument conversion routine, which typically passes it on
96 * to the called 32-bit entry point.
98 * CallFrom16Thunk is a special variant used by the implementation of
99 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
100 * implemented as follows:
101 * On entry, the EBX register is set up to contain a flat pointer to the
102 * 16-bit stack such that EBX+22 points to the first argument.
103 * Then, the entry point is called, while EBP is set up to point
104 * to the return address (on the 32-bit stack).
105 * The called function returns with CX set to the number of bytes
106 * to be popped of the caller's stack.
108 * Stack layout upon entry to the core routine (STACK16FRAME):
110 * (sp+24) word first 16-bit arg
114 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
115 * (sp+12) word ip of actual entry point (necessary for relay debugging)
116 * (sp+8) long relay (argument conversion) function entry point
117 * (sp+4) long cs of 16-bit entry point
118 * (sp) long ip of 16-bit entry point
120 * Added on the stack:
121 * (sp-2) word saved gs
122 * (sp-4) word saved fs
123 * (sp-6) word saved es
124 * (sp-8) word saved ds
125 * (sp-12) long saved ebp
126 * (sp-16) long saved ecx
127 * (sp-20) long saved edx
128 * (sp-24) long saved previous stack
130 static void BuildCallFrom16Core( int reg_func
, int thunk
)
132 /* Function header */
133 if (thunk
) function_header( "__wine_call_from_16_thunk" );
134 else if (reg_func
) function_header( "__wine_call_from_16_regs" );
135 else function_header( "__wine_call_from_16" );
137 /* Create STACK16FRAME (except STACK32FRAME link) */
138 output( "\tpushw %%gs\n" );
139 output( "\tpushw %%fs\n" );
140 output( "\tpushw %%es\n" );
141 output( "\tpushw %%ds\n" );
142 output( "\tpushl %%ebp\n" );
143 output( "\tpushl %%ecx\n" );
144 output( "\tpushl %%edx\n" );
146 /* Save original EFlags register */
147 if (reg_func
) output( "\tpushfl\n" );
151 output( "\tcall 1f\n" );
152 output( "1:\tpopl %%ecx\n" );
153 output( "\t.byte 0x2e\n\tmovl %s-1b(%%ecx),%%edx\n", asm_name("CallTo16_DataSelector") );
156 output( "\t.byte 0x2e\n\tmovl %s,%%edx\n", asm_name("CallTo16_DataSelector") );
158 /* Load 32-bit segment registers */
159 output( "\tmovw %%dx, %%ds\n" );
160 output( "\tmovw %%dx, %%es\n" );
163 output( "\tmovw %s-1b(%%ecx), %%fs\n", asm_name("CallTo16_TebSelector") );
165 output( "\tmovw %s, %%fs\n", asm_name("CallTo16_TebSelector") );
167 output( "\t.byte 0x64\n\tmov (%d),%%gs\n", GS_OFFSET
);
169 /* Translate STACK16FRAME base to flat offset in %edx */
170 output( "\tmovw %%ss, %%dx\n" );
171 output( "\tandl $0xfff8, %%edx\n" );
172 output( "\tshrl $1, %%edx\n" );
175 output( "\taddl wine_ldt_copy_ptr-1b(%%ecx),%%edx\n" );
176 output( "\tmovl (%%edx), %%edx\n" );
179 output( "\tmovl %s(%%edx), %%edx\n", asm_name("wine_ldt_copy") );
180 output( "\tmovzwl %%sp, %%ebp\n" );
181 output( "\tleal %d(%%ebp,%%edx), %%edx\n", reg_func
? 0 : -4 );
183 /* Get saved flags into %ecx */
184 if (reg_func
) output( "\tpopl %%ecx\n" );
186 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
187 output( "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET
);
188 output( "\tpushl %%ebp\n" );
191 output( "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET
+ 2 );
192 output( "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET
);
193 output( "\tpushl %%ds\n" );
194 output( "\tpopl %%ss\n" );
195 output( "\tmovl %%ebp, %%esp\n" );
196 output( "\taddl $%d, %%ebp\n", STACK32OFFSET(ebp
) );
200 STACK16FRAME is completely set up
201 DS, ES, SS: flat data segment
203 ESP: points to last STACK32FRAME
204 EBP: points to ebp member of last STACK32FRAME
205 EDX: points to current STACK16FRAME
206 ECX: contains saved flags
207 all other registers: unchanged */
209 /* Special case: C16ThkSL stub */
212 /* Set up registers as expected and call thunk */
213 output( "\tleal %d(%%edx), %%ebx\n", (int)sizeof(STACK16FRAME
)-22 );
214 output( "\tleal -4(%%esp), %%ebp\n" );
216 output( "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point
) );
218 /* Switch stack back */
219 output( "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET
+2 );
220 output( "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET
);
221 output( "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
223 /* Restore registers and return directly to caller */
224 output( "\taddl $8, %%esp\n" );
225 output( "\tpopl %%ebp\n" );
226 output( "\tpopw %%ds\n" );
227 output( "\tpopw %%es\n" );
228 output( "\tpopw %%fs\n" );
229 output( "\tpopw %%gs\n" );
230 output( "\taddl $20, %%esp\n" );
232 output( "\txorb %%ch, %%ch\n" );
233 output( "\tpopl %%ebx\n" );
234 output( "\taddw %%cx, %%sp\n" );
235 output( "\tpush %%ebx\n" );
237 output( "\t.byte 0x66\n" );
238 output( "\tlret\n" );
244 /* Build register CONTEXT */
247 output( "\tsubl $%d, %%esp\n", (int)sizeof(CONTEXT86
) );
249 output( "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags
) );
251 output( "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax
) );
252 output( "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx
) );
253 output( "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi
) );
254 output( "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi
) );
256 output( "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp
) );
257 output( "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp
) );
258 output( "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx
) );
259 output( "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx
) );
260 output( "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx
) );
261 output( "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx
) );
263 output( "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds
) );
264 output( "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs
) );
265 output( "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es
) );
266 output( "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs
) );
267 output( "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs
) );
268 output( "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs
) );
269 output( "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs
) );
270 output( "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs
) );
272 output( "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs
) );
273 output( "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs
) );
274 output( "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip
) );
275 output( "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip
) );
277 output( "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
+2 );
278 output( "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs
) );
279 output( "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
);
280 output( "\taddl $%d, %%eax\n", STACK16OFFSET(ip
) );
281 output( "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp
) );
283 output( "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
286 /* Push address of CONTEXT86 structure -- popped by the relay routine */
287 output( "\tmovl %%esp,%%eax\n" );
288 output( "\tandl $~15,%%esp\n" );
289 output( "\tsubl $4,%%esp\n" );
290 output( "\tpushl %%eax\n" );
294 output( "\tsubl $8,%%esp\n" );
295 output( "\tandl $~15,%%esp\n" );
296 output( "\taddl $8,%%esp\n" );
299 /* Call relay routine (which will call the API entry point) */
300 output( "\tleal %d(%%edx), %%eax\n", (int)sizeof(STACK16FRAME
) );
301 output( "\tpushl %%eax\n" );
302 output( "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point
) );
303 output( "\tcall *%d(%%edx)\n", STACK16OFFSET(relay
) );
307 output( "\tleal -%d(%%ebp), %%ebx\n", (int)sizeof(CONTEXT
) + STACK32OFFSET(ebp
) );
309 /* Switch stack back */
310 output( "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET
+2 );
311 output( "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET
);
312 output( "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
314 /* Get return address to CallFrom16 stub */
315 output( "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip
)-4 );
316 output( "\tpopl %%eax\n" );
317 output( "\tpopl %%edx\n" );
319 /* Restore all registers from CONTEXT */
320 output( "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs
) );
321 output( "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp
) );
322 output( "\taddl $4, %%esp\n" ); /* room for final return address */
324 output( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
325 output( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
326 output( "\tpushl %%edx\n" );
327 output( "\tpushl %%eax\n" );
328 output( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags
) );
329 output( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
331 output( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegEs
) );
332 output( "\tpopl %%es\n" );
333 output( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegFs
) );
334 output( "\tpopl %%fs\n" );
335 output( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegGs
) );
336 output( "\tpopl %%gs\n" );
338 output( "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp
) );
339 output( "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi
) );
340 output( "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi
) );
341 output( "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax
) );
342 output( "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx
) );
343 output( "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx
) );
344 output( "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx
) );
346 output( "\tpopl %%ds\n" );
347 output( "\tpopfl\n" );
348 output( "\tlret\n" );
352 /* Switch stack back */
353 output( "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET
+2 );
354 output( "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET
);
355 output( "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
357 /* Restore registers */
358 output( "\tpopl %%edx\n" );
359 output( "\tpopl %%ecx\n" );
360 output( "\tpopl %%ebp\n" );
361 output( "\tpopw %%ds\n" );
362 output( "\tpopw %%es\n" );
363 output( "\tpopw %%fs\n" );
364 output( "\tpopw %%gs\n" );
366 /* Return to return stub which will return to caller */
367 output( "\tlret $12\n" );
369 if (thunk
) output_function_size( "__wine_call_from_16_thunk" );
370 else if (reg_func
) output_function_size( "__wine_call_from_16_regs" );
371 else output_function_size( "__wine_call_from_16" );
375 /*******************************************************************
378 * This routine builds the core routines used in 32->16 thunks:
380 * extern DWORD WINAPI wine_call_to_16( FARPROC16 target, DWORD cbArgs, PEXCEPTION_HANDLER handler );
381 * extern void WINAPI wine_call_to_16_regs( CONTEXT86 *context, DWORD cbArgs, PEXCEPTION_HANDLER handler );
383 * These routines can be called directly from 32-bit code.
385 * All routines expect that the 16-bit stack contents (arguments) and the
386 * return address (segptr to CallTo16_Ret) were already set up by the
387 * caller; nb_args must contain the number of bytes to be conserved. The
388 * 16-bit SS:SP will be set accordingly.
390 * All other registers are either taken from the CONTEXT86 structure
391 * or else set to default values. The target routine address is either
392 * given directly or taken from the CONTEXT86.
394 static void BuildCallTo16Core( int reg_func
)
396 const char *name
= reg_func
? "wine_call_to_16_regs" : "wine_call_to_16";
398 /* Function header */
399 function_header( name
);
401 /* Function entry sequence */
402 output( "\tpushl %%ebp\n" );
403 output( "\tmovl %%esp, %%ebp\n" );
405 /* Save the 32-bit registers */
406 output( "\tpushl %%ebx\n" );
407 output( "\tpushl %%esi\n" );
408 output( "\tpushl %%edi\n" );
409 output( "\t.byte 0x64\n\tmov %%gs,(%d)\n", GS_OFFSET
);
411 /* Setup exception frame */
412 output( "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET
);
413 output( "\tpushl 16(%%ebp)\n" ); /* handler */
414 output( "\t.byte 0x64\n\tpushl (0)\n" );
415 output( "\t.byte 0x64\n\tmovl %%esp,(0)\n" );
417 /* Call the actual CallTo16 routine (simulate a lcall) */
418 output( "\tpushl %%cs\n" );
419 output( "\tcall .L%s\n", name
);
421 /* Remove exception frame */
422 output( "\t.byte 0x64\n\tpopl (0)\n" );
423 output( "\taddl $4, %%esp\n" );
424 output( "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
428 /* Convert return value */
429 output( "\tandl $0xffff,%%eax\n" );
430 output( "\tshll $16,%%edx\n" );
431 output( "\torl %%edx,%%eax\n" );
436 * Modify CONTEXT86 structure to contain new values
438 * NOTE: We restore only EAX, EBX, EDX, EDX, EBP, and ESP.
439 * The segment registers as well as ESI and EDI should
440 * not be modified by a well-behaved 16-bit routine in
441 * any case. [If necessary, we could restore them as well,
442 * at the cost of a somewhat less efficient return path.]
445 output( "\tmovl %d(%%esp), %%edi\n", STACK32OFFSET(target
) - STACK32OFFSET(edi
));
446 /* everything above edi has been popped already */
448 output( "\tmovl %%eax, %d(%%edi)\n", CONTEXTOFFSET(Eax
) );
449 output( "\tmovl %%ebx, %d(%%edi)\n", CONTEXTOFFSET(Ebx
) );
450 output( "\tmovl %%ecx, %d(%%edi)\n", CONTEXTOFFSET(Ecx
) );
451 output( "\tmovl %%edx, %d(%%edi)\n", CONTEXTOFFSET(Edx
) );
452 output( "\tmovl %%ebp, %d(%%edi)\n", CONTEXTOFFSET(Ebp
) );
453 output( "\tmovl %%esi, %d(%%edi)\n", CONTEXTOFFSET(Esp
) );
454 /* The return glue code saved %esp into %esi */
457 /* Restore the 32-bit registers */
458 output( "\tpopl %%edi\n" );
459 output( "\tpopl %%esi\n" );
460 output( "\tpopl %%ebx\n" );
462 /* Function exit sequence */
463 output( "\tpopl %%ebp\n" );
464 output( "\tret $12\n" );
467 /* Start of the actual CallTo16 routine */
469 output( ".L%s:\n", name
);
471 /* Switch to the 16-bit stack */
472 output( "\tmovl %%esp,%%edx\n" );
473 output( "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
474 output( "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
475 output( "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
477 /* Make %bp point to the previous stackframe (built by CallFrom16) */
478 output( "\tmovzwl %%sp,%%ebp\n" );
479 output( "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp
) );
481 /* Add the specified offset to the new sp */
482 output( "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args
) );
486 /* Push the called routine address */
487 output( "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target
) );
488 output( "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs
) );
489 output( "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip
) );
491 /* Get the registers */
492 output( "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs
) );
493 output( "\tpushl %d(%%edx)\n", CONTEXTOFFSET(SegEs
) );
494 output( "\tpopl %%es\n" );
495 output( "\tpushl %d(%%edx)\n", CONTEXTOFFSET(SegFs
) );
496 output( "\tpopl %%fs\n" );
497 output( "\tpushl %d(%%edx)\n", CONTEXTOFFSET(SegGs
) );
498 output( "\tpopl %%gs\n" );
499 output( "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
500 output( "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi
) );
501 output( "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi
) );
502 output( "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax
) );
503 output( "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
504 output( "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
505 output( "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx
) );
507 /* Get the 16-bit ds */
508 output( "\tpopw %%ds\n" );
510 else /* not a register function */
512 /* Push the called routine address */
513 output( "\tpushl %d(%%edx)\n", STACK32OFFSET(target
) );
515 /* Set %fs and %gs to the value saved by the last CallFrom16 */
516 output( "\tpushw %d(%%ebp)\n", STACK16OFFSET(fs
)-STACK16OFFSET(bp
) );
517 output( "\tpopw %%fs\n" );
518 output( "\tpushw %d(%%ebp)\n", STACK16OFFSET(gs
)-STACK16OFFSET(bp
) );
519 output( "\tpopw %%gs\n" );
521 /* Set %ds and %es (and %ax just in case) equal to %ss */
522 output( "\tmovw %%ss,%%ax\n" );
523 output( "\tmovw %%ax,%%ds\n" );
524 output( "\tmovw %%ax,%%es\n" );
527 /* Jump to the called routine */
528 output( "\t.byte 0x66\n" );
529 output( "\tlret\n" );
531 /* Function footer */
532 output_function_size( name
);
536 /*******************************************************************
539 * Build the return code for 16-bit callbacks
541 static void BuildRet16Func(void)
543 function_header( "__wine_call_to_16_ret" );
545 /* Save %esp into %esi */
546 output( "\tmovl %%esp,%%esi\n" );
548 /* Restore 32-bit segment registers */
550 output( "\t.byte 0x2e\n\tmovl %s", asm_name("CallTo16_DataSelector") );
551 output( "-%s,%%edi\n", asm_name("__wine_call16_start") );
552 output( "\tmovw %%di,%%ds\n" );
553 output( "\tmovw %%di,%%es\n" );
555 output( "\t.byte 0x2e\n\tmov %s", asm_name("CallTo16_TebSelector") );
556 output( "-%s,%%fs\n", asm_name("__wine_call16_start") );
558 output( "\t.byte 0x64\n\tmov (%d),%%gs\n", GS_OFFSET
);
560 /* Restore the 32-bit stack */
562 output( "\tmovw %%di,%%ss\n" );
563 output( "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET
);
565 /* Return to caller */
567 output( "\tlret\n" );
568 output_function_size( "__wine_call_to_16_ret" );
572 /*******************************************************************
573 * BuildCallTo32CBClient
575 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
577 * Since the relay stub is itself 32-bit, this should not be a problem;
578 * unfortunately, the relay stubs are expected to switch back to a
579 * 16-bit stack (and 16-bit code) after completion :-(
581 * This would conflict with our 16- vs. 32-bit stack handling, so
582 * we simply switch *back* to our 32-bit stack before returning to
585 * The CBClient relay stub expects to be called with the following
586 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
587 * stack at the designated places:
590 * (ebp+14) original arguments to the callback routine
591 * (ebp+10) far return address to original caller
592 * (ebp+6) Thunklet target address
593 * (ebp+2) Thunklet relay ID code
594 * (ebp) BP (saved by CBClientGlueSL)
595 * (ebp-2) SI (saved by CBClientGlueSL)
596 * (ebp-4) DI (saved by CBClientGlueSL)
597 * (ebp-6) DS (saved by CBClientGlueSL)
599 * ... buffer space used by the 16-bit side glue for temp copies
601 * (ebx+4) far return address to 16-bit side glue code
602 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
604 * The 32-bit side glue code accesses both the original arguments (via ebp)
605 * and the temporary copies prepared by the 16-bit side glue (via ebx).
606 * After completion, the stub will load ss:sp from the buffer at ebx
607 * and perform a far return to 16-bit code.
609 * To trick the relay stub into returning to us, we replace the 16-bit
610 * return address to the glue code by a cs:ip pair pointing to our
611 * return entry point (the original return address is saved first).
612 * Our return stub thus called will then reload the 32-bit ss:esp and
613 * return to 32-bit code (by using and ss:esp value that we have also
614 * pushed onto the 16-bit stack before and a cs:eip values found at
615 * that position on the 32-bit stack). The ss:esp to be restored is
616 * found relative to the 16-bit stack pointer at:
619 * (ebx-8) sp (32-bit stack pointer)
621 * The second variant of this routine, CALL32_CBClientEx, which is used
622 * to implement KERNEL.621, has to cope with yet another problem: Here,
623 * the 32-bit side directly returns to the caller of the CBClient thunklet,
624 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
625 * As we have to return to our 32-bit code first, we have to adapt the
626 * layout of our temporary area so as to include values for the registers
627 * that are to be restored, and later (in the implementation of KERNEL.621)
628 * we *really* restore them. The return stub restores DS, DI, SI, and BP
629 * from the stack, skips the next 8 bytes (CBClient relay code / target),
630 * and then performs a lret NN, where NN is the number of arguments to be
631 * removed. Thus, we prepare our temporary area as follows:
633 * (ebx+22) 16-bit cs (this segment)
634 * (ebx+20) 16-bit ip ('16-bit' return entry point)
635 * (ebx+16) 32-bit ss (flat)
636 * (ebx+12) 32-bit sp (32-bit stack pointer)
637 * (ebx+10) 16-bit bp (points to ebx+24)
638 * (ebx+8) 16-bit si (ignored)
639 * (ebx+6) 16-bit di (ignored)
640 * (ebx+4) 16-bit ds (we actually use the flat DS here)
641 * (ebx+2) 16-bit ss (16-bit stack segment)
642 * (ebx+0) 16-bit sp (points to ebx+4)
644 * Note that we ensure that DS is not changed and remains the flat segment,
645 * and the 32-bit stack pointer our own return stub needs fits just
646 * perfectly into the 8 bytes that are skipped by the Windows stub.
647 * One problem is that we have to determine the number of removed arguments,
648 * as these have to be really removed in KERNEL.621. Thus, the BP value
649 * that we place in the temporary area to be restored, contains the value
650 * that SP would have if no arguments were removed. By comparing the actual
651 * value of SP with this value in our return stub we can compute the number
652 * of removed arguments. This is then returned to KERNEL.621.
654 * The stack layout of this function:
655 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
656 * (ebp+16) esi pointer to caller's esi value
657 * (ebp+12) arg ebp value to be set for relay stub
658 * (ebp+8) func CBClient relay stub address
662 static void BuildCallTo32CBClient( BOOL isEx
)
664 function_header( isEx
? "CALL32_CBClientEx" : "CALL32_CBClient" );
668 output( "\tpushl %%ebp\n" );
669 output( "\tmovl %%esp,%%ebp\n" );
670 output( "\tpushl %%edi\n" );
671 output( "\tpushl %%esi\n" );
672 output( "\tpushl %%ebx\n" );
674 /* Get pointer to temporary area and save the 32-bit stack pointer */
676 output( "\tmovl 16(%%ebp), %%ebx\n" );
677 output( "\tleal -8(%%esp), %%eax\n" );
680 output( "\tmovl %%eax, -8(%%ebx)\n" );
682 output( "\tmovl %%eax, 12(%%ebx)\n" );
684 /* Set up registers and call CBClient relay stub (simulating a far call) */
686 output( "\tmovl 20(%%ebp), %%esi\n" );
687 output( "\tmovl (%%esi), %%esi\n" );
689 output( "\tmovl 8(%%ebp), %%eax\n" );
690 output( "\tmovl 12(%%ebp), %%ebp\n" );
692 output( "\tpushl %%cs\n" );
693 output( "\tcall *%%eax\n" );
695 /* Return new esi value to caller */
697 output( "\tmovl 32(%%esp), %%edi\n" );
698 output( "\tmovl %%esi, (%%edi)\n" );
700 /* Return argument size to caller */
703 output( "\tmovl 36(%%esp), %%ebx\n" );
704 output( "\tmovl %%ebp, (%%ebx)\n" );
707 /* Restore registers and return */
709 output( "\tpopl %%ebx\n" );
710 output( "\tpopl %%esi\n" );
711 output( "\tpopl %%edi\n" );
712 output( "\tpopl %%ebp\n" );
714 output_function_size( isEx
? "CALL32_CBClientEx" : "CALL32_CBClient" );
716 /* '16-bit' return stub */
718 function_header( isEx
? "CALL32_CBClientEx_Ret" : "CALL32_CBClient_Ret" );
721 output( "\tmovzwl %%sp, %%ebx\n" );
722 output( "\tlssl %%ss:-16(%%ebx), %%esp\n" );
726 output( "\tmovzwl %%bp, %%ebx\n" );
727 output( "\tsubw %%bp, %%sp\n" );
728 output( "\tmovzwl %%sp, %%ebp\n" );
729 output( "\tlssl %%ss:-12(%%ebx), %%esp\n" );
731 output( "\tlret\n" );
732 output_function_size( isEx
? "CALL32_CBClientEx_Ret" : "CALL32_CBClient_Ret" );
736 /*******************************************************************
737 * BuildCallFrom32Regs
739 * Build a 32-bit-to-Wine call-back function for a 'register' function.
740 * 'args' is the number of dword arguments.
745 * (ebp+12) ret addr to user code
746 * (ebp+8) eax saved by relay code
747 * (ebp+4) ret addr to relay code
749 * (ebp-128) buffer area to allow stack frame manipulation
750 * (ebp-332) CONTEXT86 struct
751 * (ebp-336) padding for stack alignment
752 * (ebp-336-n) CONTEXT86 *argument
753 * .... other arguments copied from (ebp+12)
755 * The entry point routine is called with a CONTEXT* extra argument,
756 * following the normal args. In this context structure, EIP_reg
757 * contains the return address to user code, and ESP_reg the stack
758 * pointer on return (with the return address and arguments already
761 static void BuildCallFrom32Regs(void)
763 static const int STACK_SPACE
= 128 + sizeof(CONTEXT86
);
765 /* Function header */
767 function_header( "__wine_call_from_32_regs" );
769 /* Allocate some buffer space on the stack */
771 output( "\tpushl %%ebp\n" );
772 output( "\tmovl %%esp,%%ebp\n ");
773 output( "\tleal -%d(%%esp), %%esp\n", STACK_SPACE
+ 4 /* for context arg */);
775 /* Build the context structure */
777 output( "\tpushfl\n" );
778 output( "\tpopl %%eax\n" );
779 output( "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
780 output( "\tmovl 0(%%ebp),%%eax\n" );
781 output( "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
782 output( "\tmovl 8(%%ebp),%%eax\n" );
783 output( "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
784 output( "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
785 output( "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
786 output( "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
787 output( "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
788 output( "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
790 output( "\txorl %%eax,%%eax\n" );
791 output( "\tmovw %%cs,%%ax\n" );
792 output( "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs
) - STACK_SPACE
);
793 output( "\tmovw %%es,%%ax\n" );
794 output( "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
795 output( "\tmovw %%fs,%%ax\n" );
796 output( "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
797 output( "\tmovw %%gs,%%ax\n" );
798 output( "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
799 output( "\tmovw %%ss,%%ax\n" );
800 output( "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs
) - STACK_SPACE
);
801 output( "\tmovw %%ds,%%ax\n" );
802 output( "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs
) - STACK_SPACE
);
803 output( "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
805 output( "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL
);
806 output( "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags
) - STACK_SPACE
);
808 output( "\tmovl 12(%%ebp),%%eax\n" ); /* Get %eip at time of call */
809 output( "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
811 /* Transfer the arguments */
813 output( "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
814 output( "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
815 output( "\tsubl %%ecx,%%esp\n" );
816 output( "\tandl $~15,%%esp\n" );
817 output( "\tleal 16(%%ebp),%%esi\n" ); /* get %esp at time of call */
818 output( "\tmovl %%esp,%%edi\n" );
819 output( "\tshrl $2,%%ecx\n" );
820 output( "\tjz 1f\n" );
822 output( "\trep\n\tmovsl\n" ); /* copy args */
823 output( "1:\tleal %d(%%ebp),%%eax\n", -STACK_SPACE
); /* get addr of context struct */
824 output( "\tmovl %%eax,(%%edi)\n" ); /* and pass it as extra arg */
825 output( "\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
826 output( "\tleal 16(%%ebp,%%eax),%%eax\n" );
827 output( "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
829 /* Call the entry point */
831 output( "\taddl (%%ebx),%%ebx\n" );
832 output( "\tcall *%%ebx\n" );
833 output( "\tleal -%d(%%ebp),%%ecx\n", STACK_SPACE
);
835 /* Restore the context structure */
837 output( "2:\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegEs
) );
838 output( "\tpopl %%es\n" );
839 output( "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegFs
) );
840 output( "\tpopl %%fs\n" );
841 output( "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegGs
) );
842 output( "\tpopl %%gs\n" );
844 output( "\tmovl %d(%%ecx),%%edi\n", CONTEXTOFFSET(Edi
) );
845 output( "\tmovl %d(%%ecx),%%esi\n", CONTEXTOFFSET(Esi
) );
846 output( "\tmovl %d(%%ecx),%%edx\n", CONTEXTOFFSET(Edx
) );
847 output( "\tmovl %d(%%ecx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
848 output( "\tmovl %d(%%ecx),%%eax\n", CONTEXTOFFSET(Eax
) );
849 output( "\tmovl %d(%%ecx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
851 output( "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegSs
) );
852 output( "\tpopl %%ss\n" );
853 output( "\tmovl %d(%%ecx),%%esp\n", CONTEXTOFFSET(Esp
) );
855 output( "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(EFlags
) );
856 output( "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegCs
) );
857 output( "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(Eip
) );
858 output( "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegDs
) );
859 output( "\tmovl %d(%%ecx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
861 output( "\tpopl %%ds\n" );
862 output( "\tiret\n" );
863 output_function_size( "__wine_call_from_32_regs" );
865 function_header( "__wine_call_from_32_restore_regs" );
866 output( "\tmovl 4(%%esp),%%ecx\n" );
867 output( "\tjmp 2b\n" );
868 output_function_size( "__wine_call_from_32_restore_regs" );
872 /*******************************************************************
873 * BuildPendingEventCheck
875 * Build a function that checks whether there are any
876 * pending DPMI events.
880 * (sp+12) long eflags
885 * On entry to function, fs register points to a valid TEB.
886 * On exit from function, stack will be popped.
888 static void BuildPendingEventCheck(void)
890 /* Function header */
892 function_header( "DPMI_PendingEventCheck" );
894 /* Check for pending events. */
896 output( "\t.byte 0x64\n\ttestl $0xffffffff,(%d)\n",
897 STRUCTOFFSET(TEB
,GdiTebBatch
) + STRUCTOFFSET(WINE_VM86_TEB_INFO
,vm86_pending
) );
898 output( "\tje %s\n", asm_name("DPMI_PendingEventCheck_Cleanup") );
899 output( "\t.byte 0x64\n\ttestl $0xffffffff,(%d)\n",
900 STRUCTOFFSET(TEB
,GdiTebBatch
) + STRUCTOFFSET(WINE_VM86_TEB_INFO
,dpmi_vif
) );
901 output( "\tje %s\n", asm_name("DPMI_PendingEventCheck_Cleanup") );
903 /* Process pending events. */
907 /* Start cleanup. Restore fs register. */
909 output( "%s\n", asm_globl("DPMI_PendingEventCheck_Cleanup") );
910 output( "\tpopw %%fs\n" );
912 /* Return from function. */
914 output( "%s\n", asm_globl("DPMI_PendingEventCheck_Return") );
915 output( "\tiret\n" );
917 output_function_size( "DPMI_PendingEventCheck" );
921 /*******************************************************************
924 * Build all the 16-bit relay callbacks
926 void BuildRelays16(void)
928 if (target_cpu
!= CPU_x86
)
930 output( "/* File not used with this architecture. Do not edit! */\n\n" );
936 output( "/* File generated automatically. Do not edit! */\n\n" );
937 output( "\t.text\n" );
939 output( "%s:\n\n", asm_name("__wine_spec_thunk_text_16") );
941 output( "%s\n", asm_globl("__wine_call16_start") );
943 /* Standard CallFrom16 routine */
944 BuildCallFrom16Core( FALSE
, FALSE
);
946 /* Register CallFrom16 routine */
947 BuildCallFrom16Core( TRUE
, FALSE
);
949 /* C16ThkSL CallFrom16 routine */
950 BuildCallFrom16Core( FALSE
, TRUE
);
952 /* Standard CallTo16 routine */
953 BuildCallTo16Core( 0 );
955 /* Register CallTo16 routine */
956 BuildCallTo16Core( 1 );
958 /* Standard CallTo16 return stub */
961 /* CBClientThunkSL routine */
962 BuildCallTo32CBClient( FALSE
);
964 /* CBClientThunkSLEx routine */
965 BuildCallTo32CBClient( TRUE
);
967 /* Pending DPMI events check stub */
968 BuildPendingEventCheck();
970 output( "%s\n", asm_globl("__wine_call16_end") );
971 output_function_size( "__wine_spec_thunk_text_16" );
973 /* Declare the return address and data selector variables */
974 output( "\n\t.data\n\t.align %d\n", get_alignment(4) );
975 output( "%s\n\t.long 0\n", asm_globl("CallTo16_DataSelector") );
976 output( "%s\n\t.long 0\n", asm_globl("CallTo16_TebSelector") );
977 if (UsePIC
) output( "wine_ldt_copy_ptr:\t.long %s\n", asm_name("wine_ldt_copy") );
978 output_gnu_stack_note();
981 /*******************************************************************
984 * Build all the 32-bit relay callbacks
986 void BuildRelays32(void)
988 if (target_cpu
!= CPU_x86
)
990 output( "/* File not used with this architecture. Do not edit! */\n\n" );
996 output( "/* File generated automatically. Do not edit! */\n\n" );
997 output( "\t.text\n" );
998 output( "%s:\n\n", asm_name("__wine_spec_thunk_text_32") );
1000 /* 32-bit register entry point */
1001 BuildCallFrom32Regs();
1003 output_function_size( "__wine_spec_thunk_text_32" );
1004 output_gnu_stack_note();