Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/winebuild/relay.c
8693 views
1
/*
2
* Relay calls helper routines
3
*
4
* Copyright 1993 Robert J. Amstadt
5
* Copyright 1995 Martin von Loewis
6
* Copyright 1995, 1996, 1997 Alexandre Julliard
7
* Copyright 1997 Eric Youngdale
8
* Copyright 1999 Ulrich Weigand
9
*
10
* This library is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public
12
* License as published by the Free Software Foundation; either
13
* version 2.1 of the License, or (at your option) any later version.
14
*
15
* This library is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* Lesser General Public License for more details.
19
*
20
* You should have received a copy of the GNU Lesser General Public
21
* License along with this library; if not, write to the Free Software
22
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23
*/
24
25
#include "config.h"
26
27
#include <ctype.h>
28
#include <stdarg.h>
29
30
#include "build.h"
31
32
/* offset of the stack pointer relative to %fs:(0) */
33
#define STACKOFFSET 0x10c /* FIELD_OFFSET(TEB,SystemReserved1) */
34
35
/* fix this if the x86_thread_data structure is changed */
36
#define GS_OFFSET 0x1d8 /* FIELD_OFFSET(TEB,SystemReserved2) + FIELD_OFFSET(struct x86_thread_data,gs) */
37
38
/* offset of the ldt pointer */
39
#define LDT_OFFSET 0x220 /* FIELD_OFFSET(PEB,SpareUlongs[0]) */
40
41
42
/*******************************************************************
43
* BuildCallFrom16Core
44
*
45
* This routine builds the core routines used in 16->32 thunks:
46
* CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
47
*
48
* These routines are intended to be called via a far call (with 32-bit
49
* operand size) from 16-bit code. The 16-bit code stub must push %bp,
50
* the 32-bit entry point to be called, and the argument conversion
51
* routine to be used (see stack layout below).
52
*
53
* The core routine completes the STACK16FRAME on the 16-bit stack and
54
* switches to the 32-bit stack. Then, the argument conversion routine
55
* is called; it gets passed the 32-bit entry point and a pointer to the
56
* 16-bit arguments (on the 16-bit stack) as parameters. (You can either
57
* use conversion routines automatically generated by BuildCallFrom16,
58
* or write your own for special purposes.)
59
*
60
* The conversion routine must call the 32-bit entry point, passing it
61
* the converted arguments, and return its return value to the core.
62
* After the conversion routine has returned, the core switches back
63
* to the 16-bit stack, converts the return value to the DX:AX format
64
* (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
65
* including %bp, are popped off the stack.
66
*
67
* The 16-bit call stub now returns to the caller, popping the 16-bit
68
* arguments if necessary (pascal calling convention).
69
*
70
* In the case of a 'register' function, CallFrom16Register fills a
71
* CONTEXT86 structure with the values all registers had at the point
72
* the first instruction of the 16-bit call stub was about to be
73
* executed. A pointer to this CONTEXT86 is passed as third parameter
74
* to the argument conversion routine, which typically passes it on
75
* to the called 32-bit entry point.
76
*
77
* CallFrom16Thunk is a special variant used by the implementation of
78
* the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
79
* implemented as follows:
80
* On entry, the EBX register is set up to contain a flat pointer to the
81
* 16-bit stack such that EBX+22 points to the first argument.
82
* Then, the entry point is called, while EBP is set up to point
83
* to the return address (on the 32-bit stack).
84
* The called function returns with CX set to the number of bytes
85
* to be popped of the caller's stack.
86
*
87
* Stack layout upon entry to the core routine (STACK16FRAME):
88
* ... ...
89
* (sp+24) word first 16-bit arg
90
* (sp+22) word cs
91
* (sp+20) word ip
92
* (sp+18) word bp
93
* (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
94
* (sp+12) word ip of actual entry point (necessary for relay debugging)
95
* (sp+8) long relay (argument conversion) function entry point
96
* (sp+4) long cs of 16-bit entry point
97
* (sp) long ip of 16-bit entry point
98
*
99
* Added on the stack:
100
* (sp-2) word saved gs
101
* (sp-4) word saved fs
102
* (sp-6) word saved es
103
* (sp-8) word saved ds
104
* (sp-12) long saved ebp
105
* (sp-16) long saved ecx
106
* (sp-20) long saved edx
107
* (sp-24) long saved previous stack
108
*/
109
static void BuildCallFrom16Core( int reg_func, int thunk )
110
{
111
/* Function header */
112
if (thunk) output_function_header( "__wine_call_from_16_thunk", 1 );
113
else if (reg_func) output_function_header( "__wine_call_from_16_regs", 1 );
114
else output_function_header( "__wine_call_from_16", 1 );
115
116
/* Create STACK16FRAME (except STACK32FRAME link) */
117
output( "\tpushw %%gs\n" );
118
output( "\tpushw %%fs\n" );
119
output( "\tpushw %%es\n" );
120
output( "\tpushw %%ds\n" );
121
output( "\tpushl %%ebp\n" );
122
output( "\tpushl %%ecx\n" );
123
output( "\tpushl %%edx\n" );
124
125
/* Save original EFlags register */
126
if (reg_func) output( "\tpushfl\n" );
127
128
if ( UsePIC )
129
{
130
output( "\tcall 1f\n" );
131
output( "1:\tpopl %%ecx\n" );
132
output( "\tmovl %%cs:%s-1b(%%ecx),%%edx\n", asm_name("CallTo16_DataSelector") );
133
}
134
else
135
output( "\tmovl %%cs:%s,%%edx\n", asm_name("CallTo16_DataSelector") );
136
137
/* Load 32-bit segment registers */
138
output( "\tmovw %%dx, %%ds\n" );
139
output( "\tmovw %%dx, %%es\n" );
140
141
if ( UsePIC )
142
output( "\tmovw %s-1b(%%ecx), %%fs\n", asm_name("CallTo16_TebSelector") );
143
else
144
output( "\tmovw %s, %%fs\n", asm_name("CallTo16_TebSelector") );
145
146
output( "\tmov %%fs:(%d),%%gs\n", GS_OFFSET );
147
148
/* Translate STACK16FRAME base to flat offset in %edx */
149
output( "\tmovw %%ss, %%dx\n" );
150
output( "\tandl $0xfff8, %%edx\n" );
151
output( "\tshrl $1, %%edx\n" );
152
output( "\tmovl %%fs:(0x30), %%ecx\n" ); /* peb */
153
output( "\tmovl %u(%%ecx), %%ecx\n", LDT_OFFSET ); /* ldt_copy */
154
output( "\tmovl (%%ecx,%%edx), %%edx\n" );
155
output( "\tmovzwl %%sp, %%ebp\n" );
156
output( "\tleal %d(%%ebp,%%edx), %%edx\n", reg_func ? 0 : -4 );
157
158
/* Get saved flags into %ecx */
159
if (reg_func) output( "\tpopl %%ecx\n" );
160
161
/* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
162
output( "\tmovl %%fs:(%d), %%ebp\n", STACKOFFSET );
163
output( "\tpushl %%ebp\n" );
164
165
/* Switch stacks */
166
output( "\tmovw %%ss, %%fs:(%d)\n", STACKOFFSET + 2 );
167
output( "\tmovw %%sp, %%fs:(%d)\n", STACKOFFSET );
168
output( "\tpushl %%ds\n" );
169
output( "\tpopl %%ss\n" );
170
output( "\tmovl %%ebp, %%esp\n" );
171
output( "\taddl $0x20,%%ebp\n"); /* FIELD_OFFSET(STACK32FRAME,ebp) */
172
173
174
/* At this point:
175
STACK16FRAME is completely set up
176
DS, ES, SS: flat data segment
177
FS: current TEB
178
ESP: points to last STACK32FRAME
179
EBP: points to ebp member of last STACK32FRAME
180
EDX: points to current STACK16FRAME
181
ECX: contains saved flags
182
all other registers: unchanged */
183
184
/* Special case: C16ThkSL stub */
185
if ( thunk )
186
{
187
/* Set up registers as expected and call thunk */
188
output( "\tleal 0x1a(%%edx),%%ebx\n" ); /* sizeof(STACK16FRAME)-22 */
189
output( "\tleal -4(%%esp), %%ebp\n" );
190
191
output( "\tcall *0x26(%%edx)\n"); /* FIELD_OFFSET(STACK16FRAME,entry_point) */
192
193
/* Switch stack back */
194
output( "\tmovw %%fs:(%d), %%ss\n", STACKOFFSET+2 );
195
output( "\tmovzwl %%fs:(%d), %%esp\n", STACKOFFSET );
196
output( "\tpopl %%fs:(%d)\n", STACKOFFSET );
197
198
/* Restore registers and return directly to caller */
199
output( "\taddl $8, %%esp\n" );
200
output( "\tpopl %%ebp\n" );
201
output( "\tpopw %%ds\n" );
202
output( "\tpopw %%es\n" );
203
output( "\tpopw %%fs\n" );
204
output( "\tpopw %%gs\n" );
205
output( "\taddl $20, %%esp\n" );
206
207
output( "\txorb %%ch, %%ch\n" );
208
output( "\tpopl %%ebx\n" );
209
output( "\taddw %%cx, %%sp\n" );
210
output( "\tpush %%ebx\n" );
211
212
output( "\t.byte 0x66\n" );
213
output( "\tlret\n" );
214
215
output_function_size( "__wine_call_from_16_thunk" );
216
return;
217
}
218
219
220
/* Build register CONTEXT */
221
if ( reg_func )
222
{
223
output( "\tsubl $0x2cc,%%esp\n" ); /* sizeof(CONTEXT86) */
224
225
output( "\tmovl %%ecx,0xc0(%%esp)\n" ); /* EFlags */
226
227
output( "\tmovl %%eax,0xb0(%%esp)\n" ); /* Eax */
228
output( "\tmovl %%ebx,0xa4(%%esp)\n" ); /* Ebx */
229
output( "\tmovl %%esi,0xa0(%%esp)\n" ); /* Esi */
230
output( "\tmovl %%edi,0x9c(%%esp)\n" ); /* Edi */
231
232
output( "\tmovl 0x0c(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ebp) */
233
output( "\tmovl %%eax,0xb4(%%esp)\n" ); /* Ebp */
234
output( "\tmovl 0x08(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ecx) */
235
output( "\tmovl %%eax,0xac(%%esp)\n" ); /* Ecx */
236
output( "\tmovl 0x04(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,edx) */
237
output( "\tmovl %%eax,0xa8(%%esp)\n" ); /* Edx */
238
239
output( "\tmovzwl 0x10(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ds) */
240
output( "\tmovl %%eax,0x98(%%esp)\n" ); /* SegDs */
241
output( "\tmovzwl 0x12(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,es) */
242
output( "\tmovl %%eax,0x94(%%esp)\n" ); /* SegEs */
243
output( "\tmovzwl 0x14(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,fs) */
244
output( "\tmovl %%eax,0x90(%%esp)\n" ); /* SegFs */
245
output( "\tmovzwl 0x16(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,gs) */
246
output( "\tmovl %%eax,0x8c(%%esp)\n" ); /* SegGs */
247
248
output( "\tmovzwl 0x2e(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,cs) */
249
output( "\tmovl %%eax,0xbc(%%esp)\n" ); /* SegCs */
250
output( "\tmovzwl 0x2c(%%edx),%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ip) */
251
output( "\tmovl %%eax,0xb8(%%esp)\n" ); /* Eip */
252
253
output( "\tmovzwl %%fs:(%d), %%eax\n", STACKOFFSET+2 );
254
output( "\tmovl %%eax,0xc8(%%esp)\n" ); /* SegSs */
255
output( "\tmovzwl %%fs:(%d), %%eax\n", STACKOFFSET );
256
output( "\taddl $0x2c,%%eax\n"); /* FIELD_OFFSET(STACK16FRAME,ip) */
257
output( "\tmovl %%eax,0xc4(%%esp)\n" ); /* Esp */
258
#if 0
259
output( "\tfsave 0x1c(%%esp)\n" ); /* FloatSave */
260
#endif
261
262
/* Push address of CONTEXT86 structure -- popped by the relay routine */
263
output( "\tmovl %%esp,%%eax\n" );
264
output( "\tandl $~15,%%esp\n" );
265
output( "\tsubl $4,%%esp\n" );
266
output( "\tpushl %%eax\n" );
267
}
268
else
269
{
270
output( "\tsubl $8,%%esp\n" );
271
output( "\tandl $~15,%%esp\n" );
272
output( "\taddl $8,%%esp\n" );
273
}
274
275
/* Call relay routine (which will call the API entry point) */
276
output( "\tleal 0x30(%%edx),%%eax\n" ); /* sizeof(STACK16FRAME) */
277
output( "\tpushl %%eax\n" );
278
output( "\tpushl 0x26(%%edx)\n"); /* FIELD_OFFSET(STACK16FRAME,entry_point) */
279
output( "\tcall *0x20(%%edx)\n"); /* FIELD_OFFSET(STACK16FRAME,relay) */
280
281
if ( reg_func )
282
{
283
output( "\tleal -748(%%ebp),%%ebx\n" ); /* sizeof(CONTEXT) + FIELD_OFFSET(STACK32FRAME,ebp) */
284
285
/* Switch stack back */
286
output( "\tmovw %%fs:(%d), %%ss\n", STACKOFFSET+2 );
287
output( "\tmovzwl %%fs:(%d), %%esp\n", STACKOFFSET );
288
output( "\tpopl %%fs:(%d)\n", STACKOFFSET );
289
290
/* Get return address to CallFrom16 stub */
291
output( "\taddw $0x14,%%sp\n" ); /* FIELD_OFFSET(STACK16FRAME,callfrom_ip)-4 */
292
output( "\tpopl %%eax\n" );
293
output( "\tpopl %%edx\n" );
294
295
/* Restore all registers from CONTEXT */
296
output( "\tmovw 0xc8(%%ebx),%%ss\n"); /* SegSs */
297
output( "\tmovl 0xc4(%%ebx),%%esp\n"); /* Esp */
298
output( "\taddl $4, %%esp\n" ); /* room for final return address */
299
300
output( "\tpushw 0xbc(%%ebx)\n"); /* SegCs */
301
output( "\tpushw 0xb8(%%ebx)\n"); /* Eip */
302
output( "\tpushl %%edx\n" );
303
output( "\tpushl %%eax\n" );
304
output( "\tpushl 0xc0(%%ebx)\n"); /* EFlags */
305
output( "\tpushl 0x98(%%ebx)\n"); /* SegDs */
306
307
output( "\tpushl 0x94(%%ebx)\n"); /* SegEs */
308
output( "\tpopl %%es\n" );
309
output( "\tpushl 0x90(%%ebx)\n"); /* SegFs */
310
output( "\tpopl %%fs\n" );
311
output( "\tpushl 0x8c(%%ebx)\n"); /* SegGs */
312
output( "\tpopl %%gs\n" );
313
314
output( "\tmovl 0xb4(%%ebx),%%ebp\n"); /* Ebp */
315
output( "\tmovl 0xa0(%%ebx),%%esi\n"); /* Esi */
316
output( "\tmovl 0x9c(%%ebx),%%edi\n"); /* Edi */
317
output( "\tmovl 0xb0(%%ebx),%%eax\n"); /* Eax */
318
output( "\tmovl 0xa8(%%ebx),%%edx\n"); /* Edx */
319
output( "\tmovl 0xac(%%ebx),%%ecx\n"); /* Ecx */
320
output( "\tmovl 0xa4(%%ebx),%%ebx\n"); /* Ebx */
321
322
output( "\tpopl %%ds\n" );
323
output( "\tpopfl\n" );
324
output( "\tlret\n" );
325
326
output_function_size( "__wine_call_from_16_regs" );
327
}
328
else
329
{
330
/* Switch stack back */
331
output( "\tmovw %%fs:(%d), %%ss\n", STACKOFFSET+2 );
332
output( "\tmovzwl %%fs:(%d), %%esp\n", STACKOFFSET );
333
output( "\tpopl %%fs:(%d)\n", STACKOFFSET );
334
335
/* Restore registers */
336
output( "\tpopl %%edx\n" );
337
output( "\tpopl %%ecx\n" );
338
output( "\tpopl %%ebp\n" );
339
output( "\tpopw %%ds\n" );
340
output( "\tpopw %%es\n" );
341
output( "\tpopw %%fs\n" );
342
output( "\tpopw %%gs\n" );
343
344
/* Return to return stub which will return to caller */
345
output( "\tlret $12\n" );
346
347
output_function_size( "__wine_call_from_16" );
348
}
349
}
350
351
352
/*******************************************************************
353
* BuildCallTo16Core
354
*
355
* This routine builds the core routines used in 32->16 thunks:
356
*
357
* extern DWORD WINAPI wine_call_to_16( FARPROC16 target, DWORD cbArgs, PEXCEPTION_HANDLER handler );
358
* extern void WINAPI wine_call_to_16_regs( CONTEXT86 *context, DWORD cbArgs, PEXCEPTION_HANDLER handler );
359
*
360
* These routines can be called directly from 32-bit code.
361
*
362
* All routines expect that the 16-bit stack contents (arguments) and the
363
* return address (segptr to CallTo16_Ret) were already set up by the
364
* caller; nb_args must contain the number of bytes to be conserved. The
365
* 16-bit SS:SP will be set accordingly.
366
*
367
* All other registers are either taken from the CONTEXT86 structure
368
* or else set to default values. The target routine address is either
369
* given directly or taken from the CONTEXT86.
370
*/
371
static void BuildCallTo16Core( int reg_func )
372
{
373
const char *name = reg_func ? "wine_call_to_16_regs" : "wine_call_to_16";
374
const char *func_name = is_pe() ? strmake( "%s@12", name ) : name;
375
376
/* Function header */
377
output_function_header( func_name, 1 );
378
379
/* Function entry sequence */
380
output_cfi( ".cfi_startproc" );
381
output( "\tpushl %%ebp\n" );
382
output_cfi( ".cfi_adjust_cfa_offset 4" );
383
output_cfi( ".cfi_rel_offset %%ebp,0" );
384
output( "\tmovl %%esp, %%ebp\n" );
385
output_cfi( ".cfi_def_cfa_register %%ebp" );
386
387
/* Save the 32-bit registers */
388
output( "\tpushl %%ebx\n" );
389
output_cfi( ".cfi_rel_offset %%ebx,-4" );
390
output( "\tpushl %%esi\n" );
391
output_cfi( ".cfi_rel_offset %%esi,-8" );
392
output( "\tpushl %%edi\n" );
393
output_cfi( ".cfi_rel_offset %%edi,-12" );
394
output( "\tmov %%gs,%%fs:(%d)\n", GS_OFFSET );
395
396
/* Setup exception frame */
397
output( "\tpushl %%fs:(%d)\n", STACKOFFSET );
398
output( "\tpushl 16(%%ebp)\n" ); /* handler */
399
output( "\tpushl %%fs:(0)\n" );
400
output( "\tmovl %%esp,%%fs:(0)\n" );
401
402
/* Call the actual CallTo16 routine (simulate a lcall) */
403
output( "\tpushl %%cs\n" );
404
output( "\tcall .L%s\n", name );
405
406
/* Remove exception frame */
407
output( "\tpopl %%fs:(0)\n" );
408
output( "\taddl $4, %%esp\n" );
409
output( "\tpopl %%fs:(%d)\n", STACKOFFSET );
410
411
if ( !reg_func )
412
{
413
/* Convert return value */
414
output( "\tandl $0xffff,%%eax\n" );
415
output( "\tshll $16,%%edx\n" );
416
output( "\torl %%edx,%%eax\n" );
417
}
418
else
419
{
420
/*
421
* Modify CONTEXT86 structure to contain new values
422
*
423
* NOTE: We restore only EAX, EBX, ECX, EDX, EBP, and ESP.
424
* The segment registers as well as ESI and EDI should
425
* not be modified by a well-behaved 16-bit routine in
426
* any case. [If necessary, we could restore them as well,
427
* at the cost of a somewhat less efficient return path.]
428
*/
429
430
output( "\tmovl 0x14(%%esp),%%edi\n" ); /* FIELD_OFFSET(STACK32FRAME,target) - FIELD_OFFSET(STACK32FRAME,edi) */
431
/* everything above edi has been popped already */
432
433
output( "\tmovl %%eax,0xb0(%%edi)\n"); /* Eax */
434
output( "\tmovl %%ebx,0xa4(%%edi)\n"); /* Ebx */
435
output( "\tmovl %%ecx,0xac(%%edi)\n"); /* Ecx */
436
output( "\tmovl %%edx,0xa8(%%edi)\n"); /* Edx */
437
output( "\tmovl %%ebp,0xb4(%%edi)\n"); /* Ebp */
438
output( "\tmovl %%esi,0xc4(%%edi)\n"); /* Esp */
439
/* The return glue code saved %esp into %esi */
440
}
441
442
/* Restore the 32-bit registers */
443
output( "\tpopl %%edi\n" );
444
output_cfi( ".cfi_same_value %%edi" );
445
output( "\tpopl %%esi\n" );
446
output_cfi( ".cfi_same_value %%esi" );
447
output( "\tpopl %%ebx\n" );
448
output_cfi( ".cfi_same_value %%ebx" );
449
450
/* Function exit sequence */
451
output( "\tpopl %%ebp\n" );
452
output_cfi( ".cfi_def_cfa %%esp,4" );
453
output_cfi( ".cfi_same_value %%ebp" );
454
output( "\tret $12\n" );
455
output_cfi( ".cfi_endproc" );
456
457
458
/* Start of the actual CallTo16 routine */
459
460
output( ".L%s:\n", name );
461
462
/* Switch to the 16-bit stack */
463
output( "\tmovl %%esp,%%edx\n" );
464
output( "\tmovw %%fs:(%d),%%ss\n", STACKOFFSET + 2);
465
output( "\tmovw %%fs:(%d),%%sp\n", STACKOFFSET );
466
output( "\tmovl %%edx,%%fs:(%d)\n", STACKOFFSET );
467
468
/* Make %bp point to the previous stackframe (built by CallFrom16) */
469
output( "\tmovzwl %%sp,%%ebp\n" );
470
output( "\tleal 0x2a(%%ebp),%%ebp\n"); /* FIELD_OFFSET(STACK16FRAME,bp) */
471
472
/* Add the specified offset to the new sp */
473
output( "\tsubw 0x2c(%%edx), %%sp\n"); /* FIELD_OFFSET(STACK32FRAME,nb_args) */
474
475
if (reg_func)
476
{
477
/* Push the called routine address */
478
output( "\tmovl 0x28(%%edx),%%edx\n"); /* FIELD_OFFSET(STACK32FRAME,target) */
479
output( "\tpushw 0xbc(%%edx)\n"); /* SegCs */
480
output( "\tpushw 0xb8(%%edx)\n"); /* Eip */
481
482
/* Get the registers */
483
output( "\tpushw 0x98(%%edx)\n"); /* SegDs */
484
output( "\tpushl 0x94(%%edx)\n"); /* SegEs */
485
output( "\tpopl %%es\n" );
486
output( "\tmovl 0xb4(%%edx),%%ebp\n"); /* Ebp */
487
output( "\tmovl 0xa0(%%edx),%%esi\n"); /* Esi */
488
output( "\tmovl 0x9c(%%edx),%%edi\n"); /* Edi */
489
output( "\tmovl 0xb0(%%edx),%%eax\n"); /* Eax */
490
output( "\tmovl 0xa4(%%edx),%%ebx\n"); /* Ebx */
491
output( "\tmovl 0xac(%%edx),%%ecx\n"); /* Ecx */
492
output( "\tmovl 0xa8(%%edx),%%edx\n"); /* Edx */
493
494
/* Get the 16-bit ds */
495
output( "\tpopw %%ds\n" );
496
}
497
else /* not a register function */
498
{
499
/* Push the called routine address */
500
output( "\tpushl 0x28(%%edx)\n"); /* FIELD_OFFSET(STACK32FRAME,target) */
501
502
/* Set %fs and %gs to the value saved by the last CallFrom16 */
503
output( "\tpushw -22(%%ebp)\n" ); /* FIELD_OFFSET(STACK16FRAME,fs)-FIELD_OFFSET(STACK16FRAME,bp) */
504
output( "\tpopw %%fs\n" );
505
output( "\tpushw -20(%%ebp)\n" ); /* FIELD_OFFSET(STACK16FRAME,gs)-FIELD_OFFSET(STACK16FRAME,bp) */
506
output( "\tpopw %%gs\n" );
507
508
/* Set %ds and %es (and %ax just in case) equal to %ss */
509
output( "\tmovw %%ss,%%ax\n" );
510
output( "\tmovw %%ax,%%ds\n" );
511
output( "\tmovw %%ax,%%es\n" );
512
}
513
514
/* Jump to the called routine */
515
output( "\tlretw\n" );
516
517
/* Function footer */
518
output_function_size( func_name );
519
}
520
521
522
/*******************************************************************
523
* BuildRet16Func
524
*
525
* Build the return code for 16-bit callbacks
526
*/
527
static void BuildRet16Func(void)
528
{
529
output_function_header( "__wine_call_to_16_ret", 1 );
530
531
/* Save %esp into %esi */
532
output( "\tmovl %%esp,%%esi\n" );
533
534
/* Restore 32-bit segment registers */
535
536
output( "\tmovl %%cs:%s", asm_name("CallTo16_DataSelector") );
537
output( "-%s,%%edi\n", asm_name("__wine_call16_start") );
538
output( "\tmovw %%di,%%ds\n" );
539
output( "\tmovw %%di,%%es\n" );
540
541
output( "\tmov %%cs:%s", asm_name("CallTo16_TebSelector") );
542
output( "-%s,%%fs\n", asm_name("__wine_call16_start") );
543
544
output( "\tmov %%fs:(%d),%%gs\n", GS_OFFSET );
545
546
/* Restore the 32-bit stack */
547
548
output( "\tmovw %%di,%%ss\n" );
549
output( "\tmovl %%fs:(%d),%%esp\n", STACKOFFSET );
550
551
/* Return to caller */
552
553
output( "\tlret\n" );
554
output_function_size( "__wine_call_to_16_ret" );
555
}
556
557
558
/*******************************************************************
559
* output_asm_relays16
560
*
561
* Build all the 16-bit relay callbacks
562
*/
563
void output_asm_relays16(void)
564
{
565
/* File header */
566
567
output( "\t.text\n" );
568
output( "%s:\n\n", asm_name("__wine_spec_thunk_text_16") );
569
570
output( "%s\n", asm_globl("__wine_call16_start") );
571
572
/* Standard CallFrom16 routine */
573
BuildCallFrom16Core( 0, 0 );
574
575
/* Register CallFrom16 routine */
576
BuildCallFrom16Core( 1, 0 );
577
578
/* C16ThkSL CallFrom16 routine */
579
BuildCallFrom16Core( 0, 1 );
580
581
/* Standard CallTo16 routine */
582
BuildCallTo16Core( 0 );
583
584
/* Register CallTo16 routine */
585
BuildCallTo16Core( 1 );
586
587
/* Standard CallTo16 return stub */
588
BuildRet16Func();
589
590
output( "%s\n", asm_globl("__wine_call16_end") );
591
output_function_size( "__wine_spec_thunk_text_16" );
592
593
/* Declare the return address and data selector variables */
594
output( "\n\t.data\n\t.balign 4\n" );
595
output( "%s\n\t.long 0\n", asm_globl("CallTo16_DataSelector") );
596
output( "%s\n\t.long 0\n", asm_globl("CallTo16_TebSelector") );
597
}
598
599