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
17 #include "stackframe.h"
23 /*******************************************************************
26 * This routine builds the core routines used in 16->32 thunks:
27 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
29 * These routines are intended to be called via a far call (with 32-bit
30 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
31 * the 32-bit entry point to be called, and the argument conversion
32 * routine to be used (see stack layout below).
34 * The core routine completes the STACK16FRAME on the 16-bit stack and
35 * switches to the 32-bit stack. Then, the argument conversion routine
36 * is called; it gets passed the 32-bit entry point and a pointer to the
37 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
38 * use conversion routines automatically generated by BuildCallFrom16,
39 * or write your own for special purposes.)
41 * The conversion routine must call the 32-bit entry point, passing it
42 * the converted arguments, and return its return value to the core.
43 * After the conversion routine has returned, the core switches back
44 * to the 16-bit stack, converts the return value to the DX:AX format
45 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
46 * including %bp, are popped off the stack.
48 * The 16-bit call stub now returns to the caller, popping the 16-bit
49 * arguments if necessary (pascal calling convention).
51 * In the case of a 'register' function, CallFrom16Register fills a
52 * CONTEXT86 structure with the values all registers had at the point
53 * the first instruction of the 16-bit call stub was about to be
54 * executed. A pointer to this CONTEXT86 is passed as third parameter
55 * to the argument conversion routine, which typically passes it on
56 * to the called 32-bit entry point.
58 * CallFrom16Thunk is a special variant used by the implementation of
59 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
60 * implemented as follows:
61 * On entry, the EBX register is set up to contain a flat pointer to the
62 * 16-bit stack such that EBX+22 points to the first argument.
63 * Then, the entry point is called, while EBP is set up to point
64 * to the return address (on the 32-bit stack).
65 * The called function returns with CX set to the number of bytes
66 * to be popped of the caller's stack.
68 * Stack layout upon entry to the core routine (STACK16FRAME):
70 * (sp+24) word first 16-bit arg
74 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
75 * (sp+12) word ip of actual entry point (necessary for relay debugging)
76 * (sp+8) long relay (argument conversion) function entry point
77 * (sp+4) long cs of 16-bit entry point
78 * (sp) long ip of 16-bit entry point
81 * (sp-2) word saved gs
82 * (sp-4) word saved fs
83 * (sp-6) word saved es
84 * (sp-8) word saved ds
85 * (sp-12) long saved ebp
86 * (sp-16) long saved ecx
87 * (sp-20) long saved edx
88 * (sp-24) long saved previous stack
90 static void BuildCallFrom16Core( FILE *outfile
, int reg_func
, int thunk
, int short_ret
)
92 char *name
= thunk
? "thunk" : reg_func
? "regs" : short_ret
? "word" : "long";
95 fprintf( outfile
, "\n\t.align 4\n" );
97 fprintf( outfile
, ".stabs \"__wine_call_from_16_%s:F1\",36,0,0," PREFIX
"__wine_call_from_16_%s\n", name
, name
);
99 fprintf( outfile
, "\t" __ASM_FUNC("__wine_call_from_16_%s") "\n", name
);
100 fprintf( outfile
, "\t.globl " PREFIX
"__wine_call_from_16_%s\n", name
);
101 fprintf( outfile
, PREFIX
"__wine_call_from_16_%s:\n", name
);
103 /* Create STACK16FRAME (except STACK32FRAME link) */
104 fprintf( outfile
, "\tpushw %%gs\n" );
105 fprintf( outfile
, "\tpushw %%fs\n" );
106 fprintf( outfile
, "\tpushw %%es\n" );
107 fprintf( outfile
, "\tpushw %%ds\n" );
108 fprintf( outfile
, "\tpushl %%ebp\n" );
109 fprintf( outfile
, "\tpushl %%ecx\n" );
110 fprintf( outfile
, "\tpushl %%edx\n" );
112 /* Save original EFlags register */
113 fprintf( outfile
, "\tpushfl\n" );
117 /* Get Global Offset Table into %ecx */
118 fprintf( outfile
, "\tcall .L__wine_call_from_16_%s.getgot1\n", name
);
119 fprintf( outfile
, ".L__wine_call_from_16_%s.getgot1:\n", name
);
120 fprintf( outfile
, "\tpopl %%ecx\n" );
121 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot1], %%ecx\n", name
);
126 fprintf( outfile
, "\t.byte 0x2e\n\tmovl " PREFIX
"CallTo16_DataSelector@GOT(%%ecx), %%edx\n" );
127 fprintf( outfile
, "\t.byte 0x2e\n\tmovl (%%edx), %%edx\n" );
130 fprintf( outfile
, "\t.byte 0x2e\n\tmovl " PREFIX
"CallTo16_DataSelector,%%edx\n" );
132 /* Load 32-bit segment registers */
134 fprintf( outfile
, "\tdata16\n");
136 fprintf( outfile
, "\tmovw %%dx, %%ds\n" );
138 fprintf( outfile
, "\tdata16\n");
140 fprintf( outfile
, "\tmovw %%dx, %%es\n" );
144 fprintf( outfile
, "\tmovl " PREFIX
"SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
145 fprintf( outfile
, "\tmovw (%%edx), %%fs\n" );
148 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb, %%fs\n" );
150 /* Get address of wine_ldt_copy array into %ecx */
152 fprintf( outfile
, "\tmovl " PREFIX
"wine_ldt_copy@GOT(%%ecx), %%ecx\n" );
154 fprintf( outfile
, "\tmovl $" PREFIX
"wine_ldt_copy, %%ecx\n" );
156 /* Translate STACK16FRAME base to flat offset in %edx */
157 fprintf( outfile
, "\tmovw %%ss, %%dx\n" );
158 fprintf( outfile
, "\tandl $0xfff8, %%edx\n" );
159 fprintf( outfile
, "\tshrl $1, %%edx\n" );
160 fprintf( outfile
, "\tmovl (%%ecx,%%edx), %%edx\n" );
161 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
162 fprintf( outfile
, "\tleal (%%ebp,%%edx), %%edx\n" );
164 /* Get saved flags into %ecx */
165 fprintf( outfile
, "\tpopl %%ecx\n" );
167 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
168 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET
);
169 fprintf( outfile
, "\tpushl %%ebp\n" );
173 fprintf( outfile
,"\tdata16\n");
175 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET
+ 2 );
176 fprintf( outfile
, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET
);
177 fprintf( outfile
, "\tpushl %%ds\n" );
178 fprintf( outfile
, "\tpopl %%ss\n" );
179 fprintf( outfile
, "\tmovl %%ebp, %%esp\n" );
180 fprintf( outfile
, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME
, ebp
) );
184 STACK16FRAME is completely set up
185 DS, ES, SS: flat data segment
187 ESP: points to last STACK32FRAME
188 EBP: points to ebp member of last STACK32FRAME
189 EDX: points to current STACK16FRAME
190 ECX: contains saved flags
191 all other registers: unchanged */
193 /* Special case: C16ThkSL stub */
196 /* Set up registers as expected and call thunk */
197 fprintf( outfile
, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME
)-22 );
198 fprintf( outfile
, "\tleal -4(%%esp), %%ebp\n" );
200 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point
) );
202 /* Switch stack back */
203 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
204 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
205 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
207 /* Restore registers and return directly to caller */
208 fprintf( outfile
, "\taddl $8, %%esp\n" );
209 fprintf( outfile
, "\tpopl %%ebp\n" );
210 fprintf( outfile
, "\tpopw %%ds\n" );
211 fprintf( outfile
, "\tpopw %%es\n" );
212 fprintf( outfile
, "\tpopw %%fs\n" );
213 fprintf( outfile
, "\tpopw %%gs\n" );
214 fprintf( outfile
, "\taddl $20, %%esp\n" );
216 fprintf( outfile
, "\txorb %%ch, %%ch\n" );
217 fprintf( outfile
, "\tpopl %%ebx\n" );
218 fprintf( outfile
, "\taddw %%cx, %%sp\n" );
219 fprintf( outfile
, "\tpush %%ebx\n" );
221 fprintf( outfile
, "\t.byte 0x66\n" );
222 fprintf( outfile
, "\tlret\n" );
228 /* Build register CONTEXT */
231 fprintf( outfile
, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86
) );
233 fprintf( outfile
, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags
) );
235 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax
) );
236 fprintf( outfile
, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx
) );
237 fprintf( outfile
, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi
) );
238 fprintf( outfile
, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi
) );
240 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp
) );
241 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp
) );
242 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx
) );
243 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx
) );
244 fprintf( outfile
, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx
) );
245 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx
) );
247 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds
) );
248 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs
) );
249 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es
) );
250 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs
) );
251 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs
) );
252 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs
) );
253 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs
) );
254 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs
) );
256 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs
) );
257 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs
) );
258 fprintf( outfile
, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip
) );
259 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip
) );
261 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
+2 );
262 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs
) );
263 fprintf( outfile
, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET
);
264 fprintf( outfile
, "\taddl $%d, %%eax\n", STACK16OFFSET(ip
) );
265 fprintf( outfile
, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp
) );
267 fprintf( outfile
, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave
) );
270 /* Push address of CONTEXT86 structure -- popped by the relay routine */
271 fprintf( outfile
, "\tpushl %%esp\n" );
275 /* Print debug info before call */
280 fprintf( outfile
, "\tpushl %%ebx\n" );
282 /* Get Global Offset Table into %ebx (for PLT call) */
283 fprintf( outfile
, "\tcall .L__wine_call_from_16_%s.getgot2\n", name
);
284 fprintf( outfile
, ".L__wine_call_from_16_%s.getgot2:\n", name
);
285 fprintf( outfile
, "\tpopl %%ebx\n" );
286 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot2], %%ebx\n", name
);
289 fprintf( outfile
, "\tpushl %%edx\n" );
291 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
292 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
294 fprintf( outfile
, "\tpushl $0\n" );
297 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16@PLT\n ");
299 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16\n ");
301 fprintf( outfile
, "\tpopl %%edx\n" );
302 fprintf( outfile
, "\tpopl %%edx\n" );
305 fprintf( outfile
, "\tpopl %%ebx\n" );
308 /* Call relay routine (which will call the API entry point) */
309 fprintf( outfile
, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME
) );
310 fprintf( outfile
, "\tpushl %%eax\n" );
311 fprintf( outfile
, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point
) );
312 fprintf( outfile
, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay
) );
314 /* Print debug info after call */
319 fprintf( outfile
, "\tpushl %%ebx\n" );
321 /* Get Global Offset Table into %ebx (for PLT call) */
322 fprintf( outfile
, "\tcall .L__wine_call_from_16_%s.getgot3\n", name
);
323 fprintf( outfile
, ".L__wine_call_from_16_%s.getgot3:\n", name
);
324 fprintf( outfile
, "\tpopl %%ebx\n" );
325 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot3], %%ebx\n", name
);
328 fprintf( outfile
, "\tpushl %%eax\n" );
330 fprintf( outfile
, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
331 sizeof(CONTEXT
) + STRUCTOFFSET(STACK32FRAME
, ebp
) );
333 fprintf( outfile
, "\tpushl $0\n" );
336 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret@PLT\n ");
338 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n ");
340 fprintf( outfile
, "\tpopl %%eax\n" );
341 fprintf( outfile
, "\tpopl %%eax\n" );
344 fprintf( outfile
, "\tpopl %%ebx\n" );
350 fprintf( outfile
, "\tmovl %%esp, %%ebx\n" );
352 /* Switch stack back */
353 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
354 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
355 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
357 /* Get return address to CallFrom16 stub */
358 fprintf( outfile
, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip
)-4 );
359 fprintf( outfile
, "\tpopl %%eax\n" );
360 fprintf( outfile
, "\tpopl %%edx\n" );
362 /* Restore all registers from CONTEXT */
363 fprintf( outfile
, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs
) );
364 fprintf( outfile
, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp
) );
365 fprintf( outfile
, "\taddl $4, %%esp\n" ); /* room for final return address */
367 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs
) );
368 fprintf( outfile
, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip
) );
369 fprintf( outfile
, "\tpushl %%edx\n" );
370 fprintf( outfile
, "\tpushl %%eax\n" );
371 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags
) );
372 fprintf( outfile
, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs
) );
374 fprintf( outfile
, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs
) );
375 fprintf( outfile
, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs
) );
376 fprintf( outfile
, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs
) );
378 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp
) );
379 fprintf( outfile
, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi
) );
380 fprintf( outfile
, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi
) );
381 fprintf( outfile
, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax
) );
382 fprintf( outfile
, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx
) );
383 fprintf( outfile
, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx
) );
384 fprintf( outfile
, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx
) );
386 fprintf( outfile
, "\tpopl %%ds\n" );
387 fprintf( outfile
, "\tpopfl\n" );
388 fprintf( outfile
, "\tlret\n" );
392 /* Switch stack back */
393 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
394 fprintf( outfile
, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET
);
395 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
397 /* Restore registers */
398 fprintf( outfile
, "\tpopl %%edx\n" );
399 fprintf( outfile
, "\tpopl %%ecx\n" );
400 fprintf( outfile
, "\tpopl %%ebp\n" );
401 fprintf( outfile
, "\tpopw %%ds\n" );
402 fprintf( outfile
, "\tpopw %%es\n" );
403 fprintf( outfile
, "\tpopw %%fs\n" );
404 fprintf( outfile
, "\tpopw %%gs\n" );
406 /* Prepare return value and set flags accordingly */
408 fprintf( outfile
, "\tshldl $16, %%eax, %%edx\n" );
409 fprintf( outfile
, "\torl %%eax, %%eax\n" );
411 /* Return to return stub which will return to caller */
412 fprintf( outfile
, "\tlret $12\n" );
417 /*******************************************************************
420 * This routine builds the core routines used in 32->16 thunks:
422 * extern void WINAPI wine_call_to_16_word( SEGPTR target, int nb_args );
423 * extern void WINAPI wine_call_to_16_long( SEGPTR target, int nb_args );
424 * extern void WINAPI wine_call_to_16_regs_short( const CONTEXT86 *context, int nb_args );
425 * extern void WINAPI wine_call_to_16_regs_long ( const CONTEXT86 *context, int nb_args );
427 * These routines can be called directly from 32-bit code.
429 * All routines expect that the 16-bit stack contents (arguments) were
430 * already set up by the caller; nb_args must contain the number of bytes
431 * to be conserved. The 16-bit SS:SP will be set accordinly.
433 * All other registers are either taken from the CONTEXT86 structure
434 * or else set to default values. The target routine address is either
435 * given directly or taken from the CONTEXT86.
437 * If you want to call a 16-bit routine taking only standard argument types
438 * (WORD and LONG), you can also have an appropriate argument conversion
439 * stub automatically generated (see BuildCallTo16); you'd then call this
440 * stub, which in turn would prepare the 16-bit stack and call the appropiate
444 static void BuildCallTo16Core( FILE *outfile
, int short_ret
, int reg_func
)
446 char *name
= reg_func
== 2 ? "regs_long" :
447 reg_func
== 1 ? "regs_short" :
448 short_ret
? "word" : "long";
450 /* Function header */
451 fprintf( outfile
, "\n\t.align 4\n" );
453 fprintf( outfile
, ".stabs \"wine_call_to_16_%s:F1\",36,0,0," PREFIX
"wine_call_to_16_%s\n",
456 fprintf( outfile
, "\t" __ASM_FUNC("wine_call_to_16_%s") "\n", name
);
457 fprintf( outfile
, "\t.globl " PREFIX
"wine_call_to_16_%s\n", name
);
458 fprintf( outfile
, PREFIX
"wine_call_to_16_%s:\n", name
);
460 /* Function entry sequence */
461 fprintf( outfile
, "\tpushl %%ebp\n" );
462 fprintf( outfile
, "\tmovl %%esp, %%ebp\n" );
464 /* Save the 32-bit registers */
465 fprintf( outfile
, "\tpushl %%ebx\n" );
466 fprintf( outfile
, "\tpushl %%ecx\n" );
467 fprintf( outfile
, "\tpushl %%edx\n" );
468 fprintf( outfile
, "\tpushl %%esi\n" );
469 fprintf( outfile
, "\tpushl %%edi\n" );
473 /* Get Global Offset Table into %ebx */
474 fprintf( outfile
, "\tcall .Lwine_call_to_16_%s.getgot1\n", name
);
475 fprintf( outfile
, ".Lwine_call_to_16_%s.getgot1:\n", name
);
476 fprintf( outfile
, "\tpopl %%ebx\n" );
477 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.Lwine_call_to_16_%s.getgot1], %%ebx\n", name
);
480 /* Enter Win16 Mutex */
482 fprintf( outfile
, "\tcall " PREFIX
"_EnterWin16Lock@PLT\n" );
484 fprintf( outfile
, "\tcall " PREFIX
"_EnterWin16Lock\n" );
486 /* Print debugging info */
489 /* Push flags, number of arguments, and target */
490 fprintf( outfile
, "\tpushl $%d\n", reg_func
);
491 fprintf( outfile
, "\tpushl 12(%%ebp)\n" );
492 fprintf( outfile
, "\tpushl 8(%%ebp)\n" );
495 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16@PLT\n" );
497 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
499 fprintf( outfile
, "\taddl $12, %%esp\n" );
502 /* Get return address */
505 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr@GOT(%%ebx), %%ecx\n" );
506 fprintf( outfile
, "\tmovl " PREFIX
"(%%ecx), %%ecx\n" );
509 fprintf( outfile
, "\tmovl " PREFIX
"CallTo16_RetAddr, %%ecx\n" );
511 /* Call the actual CallTo16 routine (simulate a lcall) */
512 fprintf( outfile
, "\tpushl %%cs\n" );
513 fprintf( outfile
, "\tcall .Lwine_call_to_16_%s\n", name
);
517 /* Convert and push return value */
520 fprintf( outfile
, "\tmovzwl %%ax, %%eax\n" );
521 fprintf( outfile
, "\tpushl %%eax\n" );
525 fprintf( outfile
, "\tshll $16,%%edx\n" );
526 fprintf( outfile
, "\tmovw %%ax,%%dx\n" );
527 fprintf( outfile
, "\tpushl %%edx\n" );
533 * Modify CONTEXT86 structure to contain new values
535 * NOTE: We restore only EAX, EBX, EDX, EDX, EBP, and ESP.
536 * The segment registers as well as ESI and EDI should
537 * not be modified by a well-behaved 16-bit routine in
538 * any case. [If necessary, we could restore them as well,
539 * at the cost of a somewhat less efficient return path.]
542 fprintf( outfile
, "\tmovl %d(%%esp), %%edi\n", STACK32OFFSET(target
)-12 );
543 fprintf( outfile
, "\tmovl %%eax, %d(%%edi)\n", CONTEXTOFFSET(Eax
) );
544 fprintf( outfile
, "\tmovl %%ebx, %d(%%edi)\n", CONTEXTOFFSET(Ebx
) );
545 fprintf( outfile
, "\tmovl %%ecx, %d(%%edi)\n", CONTEXTOFFSET(Ecx
) );
546 fprintf( outfile
, "\tmovl %%edx, %d(%%edi)\n", CONTEXTOFFSET(Edx
) );
547 fprintf( outfile
, "\tmovl %%ebp, %d(%%edi)\n", CONTEXTOFFSET(Ebp
) );
548 fprintf( outfile
, "\tmovl %%esi, %d(%%edi)\n", CONTEXTOFFSET(Esp
) );
549 /* The return glue code saved %esp into %esi */
551 fprintf( outfile
, "\tpushl %%edi\n" );
556 /* Get Global Offset Table into %ebx (might have been overwritten) */
557 fprintf( outfile
, "\tcall .Lwine_call_to_16_%s.getgot2\n", name
);
558 fprintf( outfile
, ".Lwine_call_to_16_%s.getgot2:\n", name
);
559 fprintf( outfile
, "\tpopl %%ebx\n" );
560 fprintf( outfile
, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.Lwine_call_to_16_%s.getgot2], %%ebx\n", name
);
563 /* Print debugging info */
566 fprintf( outfile
, "\tpushl $%d\n", reg_func
);
569 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret@PLT\n" );
571 fprintf( outfile
, "\tcall " PREFIX
"RELAY_DebugCallTo16Ret\n" );
573 fprintf( outfile
, "\taddl $4, %%esp\n" );
576 /* Leave Win16 Mutex */
578 fprintf( outfile
, "\tcall " PREFIX
"_LeaveWin16Lock@PLT\n" );
580 fprintf( outfile
, "\tcall " PREFIX
"_LeaveWin16Lock\n" );
582 /* Get return value */
583 fprintf( outfile
, "\tpopl %%eax\n" );
585 /* Restore the 32-bit registers */
586 fprintf( outfile
, "\tpopl %%edi\n" );
587 fprintf( outfile
, "\tpopl %%esi\n" );
588 fprintf( outfile
, "\tpopl %%edx\n" );
589 fprintf( outfile
, "\tpopl %%ecx\n" );
590 fprintf( outfile
, "\tpopl %%ebx\n" );
592 /* Function exit sequence */
593 fprintf( outfile
, "\tpopl %%ebp\n" );
594 fprintf( outfile
, "\tret $8\n" );
597 /* Start of the actual CallTo16 routine */
599 fprintf( outfile
, ".Lwine_call_to_16_%s:\n", name
);
601 /* Complete STACK32FRAME */
602 fprintf( outfile
, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET
);
603 fprintf( outfile
, "\tmovl %%esp,%%edx\n" );
605 /* Switch to the 16-bit stack */
607 fprintf( outfile
,"\tdata16\n");
609 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET
+ 2);
610 fprintf( outfile
, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET
);
611 fprintf( outfile
, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET
);
613 /* Make %bp point to the previous stackframe (built by CallFrom16) */
614 fprintf( outfile
, "\tmovzwl %%sp,%%ebp\n" );
615 fprintf( outfile
, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp
) );
617 /* Add the specified offset to the new sp */
618 fprintf( outfile
, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args
) );
620 /* Push the return address
621 * With sreg suffix, we push 16:16 address (normal lret)
622 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
625 fprintf( outfile
, "\tpushl %%ecx\n" );
628 fprintf( outfile
, "\tshldl $16, %%ecx, %%eax\n" );
629 fprintf( outfile
, "\tpushw $0\n" );
630 fprintf( outfile
, "\tpushw %%ax\n" );
631 fprintf( outfile
, "\tpushw $0\n" );
632 fprintf( outfile
, "\tpushw %%cx\n" );
637 /* Push the called routine address */
638 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target
) );
639 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs
) );
640 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip
) );
642 /* Get the registers */
643 fprintf( outfile
, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs
) );
644 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs
) );
645 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
646 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs
) );
647 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
648 fprintf( outfile
, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp
) );
649 fprintf( outfile
, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi
) );
650 fprintf( outfile
, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi
) );
651 fprintf( outfile
, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax
) );
652 fprintf( outfile
, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx
) );
653 fprintf( outfile
, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx
) );
654 fprintf( outfile
, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx
) );
656 /* Get the 16-bit ds */
657 fprintf( outfile
, "\tpopw %%ds\n" );
659 else /* not a register function */
661 /* Push the called routine address */
662 fprintf( outfile
, "\tpushl %d(%%edx)\n", STACK32OFFSET(target
) );
664 /* Set %fs to the value saved by the last CallFrom16 */
665 fprintf( outfile
, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs
)-STACK16OFFSET(bp
) );
666 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
668 /* Set %ds and %es (and %ax just in case) equal to %ss */
669 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
670 fprintf( outfile
, "\tmovw %%ax,%%ds\n" );
671 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
674 /* Jump to the called routine */
675 fprintf( outfile
, "\t.byte 0x66\n" );
676 fprintf( outfile
, "\tlret\n" );
680 /*******************************************************************
683 * Build the return code for 16-bit callbacks
685 static void BuildRet16Func( FILE *outfile
)
688 * Note: This must reside in the .data section to allow
689 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
692 fprintf( outfile
, "\n\t" __ASM_FUNC("CallTo16_Ret") "\n" );
693 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16_Ret\n" );
694 fprintf( outfile
, PREFIX
"CallTo16_Ret:\n" );
696 /* Save %esp into %esi */
697 fprintf( outfile
, "\tmovl %%esp,%%esi\n" );
699 /* Restore 32-bit segment registers */
701 fprintf( outfile
, "\t.byte 0x2e\n\tmovl " PREFIX
"CallTo16_DataSelector-" PREFIX
"Call16_Ret_Start,%%edi\n" );
703 fprintf( outfile
, "\tdata16\n");
705 fprintf( outfile
, "\tmovw %%di,%%ds\n" );
707 fprintf( outfile
, "\tdata16\n");
709 fprintf( outfile
, "\tmovw %%di,%%es\n" );
711 fprintf( outfile
, "\tmovw " PREFIX
"SYSLEVEL_Win16CurrentTeb,%%fs\n" );
713 /* Restore the 32-bit stack */
716 fprintf( outfile
, "\tdata16\n");
718 fprintf( outfile
, "\tmovw %%di,%%ss\n" );
719 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET
);
720 fprintf( outfile
, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET
);
722 /* Return to caller */
724 fprintf( outfile
, "\tlret\n" );
726 /* Declare the return address and data selector variables */
728 fprintf( outfile
, "\n\t.align 4\n" );
729 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16_DataSelector\n" );
730 fprintf( outfile
, PREFIX
"CallTo16_DataSelector:\t.long 0\n" );
731 fprintf( outfile
, "\t.globl " PREFIX
"CallTo16_RetAddr\n" );
732 fprintf( outfile
, PREFIX
"CallTo16_RetAddr:\t.long 0\n" );
736 /*******************************************************************
737 * BuildCallTo32CBClient
739 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
741 * Since the relay stub is itself 32-bit, this should not be a problem;
742 * unfortunately, the relay stubs are expected to switch back to a
743 * 16-bit stack (and 16-bit code) after completion :-(
745 * This would conflict with our 16- vs. 32-bit stack handling, so
746 * we simply switch *back* to our 32-bit stack before returning to
749 * The CBClient relay stub expects to be called with the following
750 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
751 * stack at the designated places:
754 * (ebp+14) original arguments to the callback routine
755 * (ebp+10) far return address to original caller
756 * (ebp+6) Thunklet target address
757 * (ebp+2) Thunklet relay ID code
758 * (ebp) BP (saved by CBClientGlueSL)
759 * (ebp-2) SI (saved by CBClientGlueSL)
760 * (ebp-4) DI (saved by CBClientGlueSL)
761 * (ebp-6) DS (saved by CBClientGlueSL)
763 * ... buffer space used by the 16-bit side glue for temp copies
765 * (ebx+4) far return address to 16-bit side glue code
766 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
768 * The 32-bit side glue code accesses both the original arguments (via ebp)
769 * and the temporary copies prepared by the 16-bit side glue (via ebx).
770 * After completion, the stub will load ss:sp from the buffer at ebx
771 * and perform a far return to 16-bit code.
773 * To trick the relay stub into returning to us, we replace the 16-bit
774 * return address to the glue code by a cs:ip pair pointing to our
775 * return entry point (the original return address is saved first).
776 * Our return stub thus called will then reload the 32-bit ss:esp and
777 * return to 32-bit code (by using and ss:esp value that we have also
778 * pushed onto the 16-bit stack before and a cs:eip values found at
779 * that position on the 32-bit stack). The ss:esp to be restored is
780 * found relative to the 16-bit stack pointer at:
783 * (ebx-8) sp (32-bit stack pointer)
785 * The second variant of this routine, CALL32_CBClientEx, which is used
786 * to implement KERNEL.621, has to cope with yet another problem: Here,
787 * the 32-bit side directly returns to the caller of the CBClient thunklet,
788 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
789 * As we have to return to our 32-bit code first, we have to adapt the
790 * layout of our temporary area so as to include values for the registers
791 * that are to be restored, and later (in the implementation of KERNEL.621)
792 * we *really* restore them. The return stub restores DS, DI, SI, and BP
793 * from the stack, skips the next 8 bytes (CBClient relay code / target),
794 * and then performs a lret NN, where NN is the number of arguments to be
795 * removed. Thus, we prepare our temporary area as follows:
797 * (ebx+22) 16-bit cs (this segment)
798 * (ebx+20) 16-bit ip ('16-bit' return entry point)
799 * (ebx+16) 32-bit ss (flat)
800 * (ebx+12) 32-bit sp (32-bit stack pointer)
801 * (ebx+10) 16-bit bp (points to ebx+24)
802 * (ebx+8) 16-bit si (ignored)
803 * (ebx+6) 16-bit di (ignored)
804 * (ebx+4) 16-bit ds (we actually use the flat DS here)
805 * (ebx+2) 16-bit ss (16-bit stack segment)
806 * (ebx+0) 16-bit sp (points to ebx+4)
808 * Note that we ensure that DS is not changed and remains the flat segment,
809 * and the 32-bit stack pointer our own return stub needs fits just
810 * perfectly into the 8 bytes that are skipped by the Windows stub.
811 * One problem is that we have to determine the number of removed arguments,
812 * as these have to be really removed in KERNEL.621. Thus, the BP value
813 * that we place in the temporary area to be restored, contains the value
814 * that SP would have if no arguments were removed. By comparing the actual
815 * value of SP with this value in our return stub we can compute the number
816 * of removed arguments. This is then returned to KERNEL.621.
818 * The stack layout of this function:
819 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
820 * (ebp+16) esi pointer to caller's esi value
821 * (ebp+12) arg ebp value to be set for relay stub
822 * (ebp+8) func CBClient relay stub address
826 static void BuildCallTo32CBClient( FILE *outfile
, BOOL isEx
)
828 char *name
= isEx
? "CBClientEx" : "CBClient";
829 int size
= isEx
? 24 : 12;
831 /* Function header */
833 fprintf( outfile
, "\n\t.align 4\n" );
835 fprintf( outfile
, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX
"CALL32_%s\n",
838 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_%s\n", name
);
839 fprintf( outfile
, PREFIX
"CALL32_%s:\n", name
);
843 fprintf( outfile
, "\tpushl %%ebp\n" );
844 fprintf( outfile
, "\tmovl %%esp,%%ebp\n" );
845 fprintf( outfile
, "\tpushl %%edi\n" );
846 fprintf( outfile
, "\tpushl %%esi\n" );
847 fprintf( outfile
, "\tpushl %%ebx\n" );
849 /* Get the 16-bit stack */
851 fprintf( outfile
, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET
);
853 /* Convert it to a flat address */
855 fprintf( outfile
, "\tshldl $16,%%ebx,%%eax\n" );
856 fprintf( outfile
, "\tandl $0xfff8,%%eax\n" );
857 fprintf( outfile
, "\tshrl $1,%%eax\n" );
858 fprintf( outfile
, "\tmovl " PREFIX
"wine_ldt_copy(%%eax),%%esi\n" );
859 fprintf( outfile
, "\tmovw %%bx,%%ax\n" );
860 fprintf( outfile
, "\taddl %%eax,%%esi\n" );
862 /* Allocate temporary area (simulate STACK16_PUSH) */
864 fprintf( outfile
, "\tpushf\n" );
865 fprintf( outfile
, "\tcld\n" );
866 fprintf( outfile
, "\tleal -%d(%%esi), %%edi\n", size
);
867 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
868 fprintf( outfile
, "\trep\n\tmovsb\n" );
869 fprintf( outfile
, "\tpopf\n" );
871 fprintf( outfile
, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size
, STACKOFFSET
);
873 fprintf( outfile
, "\tpushl %%edi\n" ); /* remember address */
875 /* Set up temporary area */
879 fprintf( outfile
, "\tleal 4(%%edi), %%edi\n" );
881 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
882 fprintf( outfile
, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
884 fprintf( outfile
, "\tmovw %%ss, %%ax\n" );
885 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
886 fprintf( outfile
, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
888 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 + 4 );
889 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
891 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
892 fprintf( outfile
, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
896 fprintf( outfile
, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME
)-size
+4 );
897 fprintf( outfile
, "\tmovl %%ebx, 0(%%edi)\n" );
899 fprintf( outfile
, "\tmovw %%ds, %%ax\n" );
900 fprintf( outfile
, "\tmovw %%ax, 4(%%edi)\n" );
902 fprintf( outfile
, "\taddl $20, %%ebx\n" );
903 fprintf( outfile
, "\tmovw %%bx, 10(%%edi)\n" );
905 fprintf( outfile
, "\tleal -8(%%esp), %%eax\n" );
906 fprintf( outfile
, "\tmovl %%eax, 12(%%edi)\n" );
908 fprintf( outfile
, "\tmovw %%ss, %%ax\n" );
909 fprintf( outfile
, "\tandl $0x0000ffff, %%eax\n" );
910 fprintf( outfile
, "\tmovl %%eax, 16(%%edi)\n" );
912 fprintf( outfile
, "\tmovl " PREFIX
"CALL32_%s_RetAddr, %%eax\n", name
);
913 fprintf( outfile
, "\tmovl %%eax, 20(%%edi)\n" );
916 /* Set up registers and call CBClient relay stub (simulating a far call) */
918 fprintf( outfile
, "\tmovl 16(%%ebp), %%esi\n" );
919 fprintf( outfile
, "\tmovl (%%esi), %%esi\n" );
921 fprintf( outfile
, "\tmovl %%edi, %%ebx\n" );
922 fprintf( outfile
, "\tmovl 8(%%ebp), %%eax\n" );
923 fprintf( outfile
, "\tmovl 12(%%ebp), %%ebp\n" );
925 fprintf( outfile
, "\tpushl %%cs\n" );
926 fprintf( outfile
, "\tcall *%%eax\n" );
928 /* Return new esi value to caller */
930 fprintf( outfile
, "\tmovl 32(%%esp), %%edi\n" );
931 fprintf( outfile
, "\tmovl %%esi, (%%edi)\n" );
933 /* Cleanup temporary area (simulate STACK16_POP) */
935 fprintf( outfile
, "\tpop %%esi\n" );
937 fprintf( outfile
, "\tpushf\n" );
938 fprintf( outfile
, "\tstd\n" );
939 fprintf( outfile
, "\tdec %%esi\n" );
940 fprintf( outfile
, "\tleal %d(%%esi), %%edi\n", size
);
941 fprintf( outfile
, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME
) );
942 fprintf( outfile
, "\trep\n\tmovsb\n" );
943 fprintf( outfile
, "\tpopf\n" );
945 fprintf( outfile
, "\t.byte 0x64\n\taddw $%d,(%d)\n", size
, STACKOFFSET
);
947 /* Return argument size to caller */
950 fprintf( outfile
, "\tmovl 32(%%esp), %%ebx\n" );
951 fprintf( outfile
, "\tmovl %%ebp, (%%ebx)\n" );
954 /* Restore registers and return */
956 fprintf( outfile
, "\tpopl %%ebx\n" );
957 fprintf( outfile
, "\tpopl %%esi\n" );
958 fprintf( outfile
, "\tpopl %%edi\n" );
959 fprintf( outfile
, "\tpopl %%ebp\n" );
960 fprintf( outfile
, "\tret\n" );
963 static void BuildCallTo32CBClientRet( FILE *outfile
, BOOL isEx
)
965 char *name
= isEx
? "CBClientEx" : "CBClient";
967 /* '16-bit' return stub */
969 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_Ret\n", name
);
970 fprintf( outfile
, PREFIX
"CALL32_%s_Ret:\n", name
);
974 fprintf( outfile
, "\tmovzwl %%sp, %%ebx\n" );
975 fprintf( outfile
, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
979 fprintf( outfile
, "\tmovzwl %%bp, %%ebx\n" );
980 fprintf( outfile
, "\tsubw %%bp, %%sp\n" );
981 fprintf( outfile
, "\tmovzwl %%sp, %%ebp\n" );
982 fprintf( outfile
, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
984 fprintf( outfile
, "\tlret\n" );
986 /* Declare the return address variable */
988 fprintf( outfile
, "\n\t.globl " PREFIX
"CALL32_%s_RetAddr\n", name
);
989 fprintf( outfile
, PREFIX
"CALL32_%s_RetAddr:\t.long 0\n", name
);
993 /*******************************************************************
994 * BuildCallFrom32Regs
996 * Build a 32-bit-to-Wine call-back function for a 'register' function.
997 * 'args' is the number of dword arguments.
1001 * (ebp+12) first arg
1002 * (ebp+8) ret addr to user code
1003 * (ebp+4) ret addr to relay code
1005 * (ebp-128) buffer area to allow stack frame manipulation
1006 * (ebp-332) CONTEXT86 struct
1007 * (ebp-336) CONTEXT86 *argument
1008 * .... other arguments copied from (ebp+12)
1010 * The entry point routine is called with a CONTEXT* extra argument,
1011 * following the normal args. In this context structure, EIP_reg
1012 * contains the return address to user code, and ESP_reg the stack
1013 * pointer on return (with the return address and arguments already
1016 static void BuildCallFrom32Regs( FILE *outfile
)
1018 static const int STACK_SPACE
= 128 + sizeof(CONTEXT86
);
1020 /* Function header */
1022 fprintf( outfile
, "\n\t.align 4\n" );
1024 fprintf( outfile
, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX
"CALL32_Regs\n" );
1026 fprintf( outfile
, "\t.globl " PREFIX
"CALL32_Regs\n" );
1027 fprintf( outfile
, PREFIX
"CALL32_Regs:\n" );
1029 /* Allocate some buffer space on the stack */
1031 fprintf( outfile
, "\tpushl %%ebp\n" );
1032 fprintf( outfile
, "\tmovl %%esp,%%ebp\n ");
1033 fprintf( outfile
, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE
);
1035 /* Build the context structure */
1037 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
1038 fprintf( outfile
, "\tpushfl\n" );
1039 fprintf( outfile
, "\tpopl %%eax\n" );
1040 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
1041 fprintf( outfile
, "\tmovl 0(%%ebp),%%eax\n" );
1042 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
1043 fprintf( outfile
, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
1044 fprintf( outfile
, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
1045 fprintf( outfile
, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
1046 fprintf( outfile
, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
1047 fprintf( outfile
, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
1049 fprintf( outfile
, "\txorl %%eax,%%eax\n" );
1050 fprintf( outfile
, "\tmovw %%cs,%%ax\n" );
1051 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs
) - STACK_SPACE
);
1052 fprintf( outfile
, "\tmovw %%es,%%ax\n" );
1053 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
1054 fprintf( outfile
, "\tmovw %%fs,%%ax\n" );
1055 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
1056 fprintf( outfile
, "\tmovw %%gs,%%ax\n" );
1057 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
1058 fprintf( outfile
, "\tmovw %%ss,%%ax\n" );
1059 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs
) - STACK_SPACE
);
1060 fprintf( outfile
, "\tmovw %%ds,%%ax\n" );
1061 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs
) - STACK_SPACE
);
1062 fprintf( outfile
, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
1064 fprintf( outfile
, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL
);
1065 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags
) - STACK_SPACE
);
1067 fprintf( outfile
, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
1068 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
1070 /* Transfer the arguments */
1072 fprintf( outfile
, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
1073 fprintf( outfile
, "\tpushl %%esp\n" ); /* push ptr to context struct */
1074 fprintf( outfile
, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
1075 fprintf( outfile
, "\tjecxz 1f\n" );
1076 fprintf( outfile
, "\tsubl %%ecx,%%esp\n" );
1077 fprintf( outfile
, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
1078 fprintf( outfile
, "\tmovl %%esp,%%edi\n" );
1079 fprintf( outfile
, "\tshrl $2,%%ecx\n" );
1080 fprintf( outfile
, "\tcld\n" );
1081 fprintf( outfile
, "\trep\n\tmovsl\n" ); /* copy args */
1083 fprintf( outfile
, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
1084 fprintf( outfile
, "\tleal 12(%%ebp,%%eax),%%eax\n" );
1085 fprintf( outfile
, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
1087 /* Call the entry point */
1089 fprintf( outfile
, "\tcall *0(%%ebx)\n" );
1091 /* Store %eip and %ebp onto the new stack */
1093 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
1094 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip
) - STACK_SPACE
);
1095 fprintf( outfile
, "\tmovl %%eax,-4(%%edx)\n" );
1096 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp
) - STACK_SPACE
);
1097 fprintf( outfile
, "\tmovl %%eax,-8(%%edx)\n" );
1099 /* Restore the context structure */
1101 /* Note: we don't bother to restore %cs, %ds and %ss
1102 * changing them in 32-bit code is a recipe for disaster anyway
1104 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs
) - STACK_SPACE
);
1105 fprintf( outfile
, "\tmovw %%ax,%%es\n" );
1106 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs
) - STACK_SPACE
);
1107 fprintf( outfile
, "\tmovw %%ax,%%fs\n" );
1108 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs
) - STACK_SPACE
);
1109 fprintf( outfile
, "\tmovw %%ax,%%gs\n" );
1111 fprintf( outfile
, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi
) - STACK_SPACE
);
1112 fprintf( outfile
, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi
) - STACK_SPACE
);
1113 fprintf( outfile
, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx
) - STACK_SPACE
);
1114 fprintf( outfile
, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx
) - STACK_SPACE
);
1115 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx
) - STACK_SPACE
);
1117 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags
) - STACK_SPACE
);
1118 fprintf( outfile
, "\tpushl %%eax\n" );
1119 fprintf( outfile
, "\tpopfl\n" );
1120 fprintf( outfile
, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax
) - STACK_SPACE
);
1122 fprintf( outfile
, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp
) - STACK_SPACE
);
1123 fprintf( outfile
, "\tleal -8(%%ebp),%%esp\n" );
1124 fprintf( outfile
, "\tpopl %%ebp\n" );
1125 fprintf( outfile
, "\tret\n" );
1129 /*******************************************************************
1132 * Build all the relay callbacks
1134 void BuildRelays( FILE *outfile
)
1138 fprintf( outfile
, "/* File generated automatically. Do not edit! */\n\n" );
1139 fprintf( outfile
, "\t.text\n" );
1142 if (output_file_name
)
1145 getcwd(buffer
, sizeof(buffer
));
1146 fprintf( outfile
, "\t.file\t\"%s\"\n", output_file_name
);
1149 * The stabs help the internal debugger as they are an indication that it
1150 * is sensible to step into a thunk/trampoline.
1152 fprintf( outfile
, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer
);
1153 fprintf( outfile
, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name
);
1154 fprintf( outfile
, "Code_Start:\n\n" );
1157 fprintf( outfile
, PREFIX
"Call16_Start:\n" );
1158 fprintf( outfile
, "\t.globl "PREFIX
"Call16_Start\n" );
1159 fprintf( outfile
, "\t.byte 0\n\n" );
1161 /* Standard CallFrom16 routine (WORD return) */
1162 BuildCallFrom16Core( outfile
, FALSE
, FALSE
, TRUE
);
1164 /* Standard CallFrom16 routine (DWORD return) */
1165 BuildCallFrom16Core( outfile
, FALSE
, FALSE
, FALSE
);
1167 /* Register CallFrom16 routine */
1168 BuildCallFrom16Core( outfile
, TRUE
, FALSE
, FALSE
);
1170 /* C16ThkSL CallFrom16 routine */
1171 BuildCallFrom16Core( outfile
, FALSE
, TRUE
, FALSE
);
1173 /* Standard CallTo16 routine (WORD return) */
1174 BuildCallTo16Core( outfile
, TRUE
, FALSE
);
1176 /* Standard CallTo16 routine (DWORD return) */
1177 BuildCallTo16Core( outfile
, FALSE
, FALSE
);
1179 /* Register CallTo16 routine (16:16 retf) */
1180 BuildCallTo16Core( outfile
, FALSE
, 1 );
1182 /* Register CallTo16 routine (16:32 retf) */
1183 BuildCallTo16Core( outfile
, FALSE
, 2 );
1185 /* CBClientThunkSL routine */
1186 BuildCallTo32CBClient( outfile
, FALSE
);
1188 /* CBClientThunkSLEx routine */
1189 BuildCallTo32CBClient( outfile
, TRUE
);
1191 /* 32-bit register entry point */
1192 BuildCallFrom32Regs( outfile
);
1194 fprintf( outfile
, PREFIX
"Call16_End:\n" );
1195 fprintf( outfile
, "\t.globl "PREFIX
"Call16_End\n" );
1198 fprintf( outfile
, "\t.stabs \"\",100,0,0,.Letext\n");
1199 fprintf( outfile
, ".Letext:\n");
1202 /* The whole Call16_Ret segment must lie within the .data section */
1203 fprintf( outfile
, "\n\t.data\n" );
1204 fprintf( outfile
, "\t.globl " PREFIX
"Call16_Ret_Start\n" );
1205 fprintf( outfile
, PREFIX
"Call16_Ret_Start:\n" );
1207 /* Standard CallTo16 return stub */
1208 BuildRet16Func( outfile
);
1210 /* CBClientThunkSL return stub */
1211 BuildCallTo32CBClientRet( outfile
, FALSE
);
1213 /* CBClientThunkSLEx return stub */
1214 BuildCallTo32CBClientRet( outfile
, TRUE
);
1216 /* End of Call16_Ret segment */
1217 fprintf( outfile
, "\n\t.globl " PREFIX
"Call16_Ret_End\n" );
1218 fprintf( outfile
, PREFIX
"Call16_Ret_End:\n" );
1221 #else /* __i386__ */
1223 void BuildRelays( FILE *outfile
)
1225 fprintf( outfile
, "/* File not used with this architecture. Do not edit! */\n\n" );
1228 #endif /* __i386__ */