2 * KERNEL32 thunks and other undocumented stuff
4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1997, 1998 Marcus Meissner
6 * Copyright 1998 Ulrich Weigand
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <sys/types.h>
34 #include "wine/winbase16.h"
36 #include "wine/debug.h"
37 #include "kernel16_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(thunk
);
41 struct ThunkDataCommon
43 char magic
[4]; /* 00 */
44 DWORD checksum
; /* 04 */
49 struct ThunkDataCommon common
; /* 00 */
50 SEGPTR targetTable
; /* 08 */
51 DWORD firstTime
; /* 0C */
56 struct ThunkDataCommon common
; /* 00 */
57 DWORD
* targetTable
; /* 08 */
58 char lateBinding
[4]; /* 0C */
60 DWORD reserved1
; /* 14 */
61 DWORD reserved2
; /* 18 */
62 DWORD offsetQTThunk
; /* 1C */
63 DWORD offsetFTProlog
; /* 20 */
68 struct ThunkDataCommon common
; /* 00 */
69 DWORD flags1
; /* 08 */
70 DWORD reserved1
; /* 0C */
71 struct ThunkDataSL
* fpData
; /* 10 */
72 SEGPTR spData
; /* 14 */
73 DWORD reserved2
; /* 18 */
74 char lateBinding
[4]; /* 1C */
75 DWORD flags2
; /* 20 */
76 DWORD reserved3
; /* 20 */
77 SEGPTR apiDatabase
; /* 28 */
82 struct ThunkDataCommon common
; /* 00 */
83 DWORD reserved1
; /* 08 */
84 struct ThunkDataSL
* data
; /* 0C */
85 char lateBinding
[4]; /* 10 */
87 DWORD reserved2
; /* 18 */
88 DWORD reserved3
; /* 1C */
89 DWORD offsetTargetTable
; /* 20 */
95 This structure differs from the Win95 original
,
96 but
this should
not matter since it is strictly internal to
97 the thunk handling routines in KRNL386
/ KERNEL32
.
99 For reference
, here is the Win95 layout
:
101 struct ThunkDataCommon common
; /* 00 */
102 DWORD flags1
; /* 08 */
103 SEGPTR apiDatabase
; /* 0C */
104 WORD exePtr
; /* 10 */
105 WORD segMBA
; /* 12 */
106 DWORD lenMBATotal
; /* 14 */
107 DWORD lenMBAUsed
; /* 18 */
108 DWORD flags2
; /* 1C */
109 char pszDll16
[256]; /* 20 */
110 char pszDll32
[256]; /*120 */
112 We
do it differently since all our thunk handling is done
113 by
32-bit code
. Therefore we
do not need to provide
114 easy access to
this data
, especially the process target
115 table database
, for 16-bit code
.
118 struct ThunkDataCommon common
;
120 struct SLApiDB
* apiDB
;
121 struct SLTargetDB
* targetDB
;
129 struct SLTargetDB
* next
;
137 DWORD errorReturnValue
;
140 WORD cbclient_selector
= 0;
141 WORD cbclientex_selector
= 0;
143 extern int call_entry_point( void *func
, int nb_args
, const DWORD
*args
);
144 extern void __wine_call_from_16_thunk(void);
145 extern void WINAPI
FT_Prolog(void);
146 extern void WINAPI
FT_PrologPrime(void);
147 extern void WINAPI
QT_Thunk(void);
148 extern void WINAPI
QT_ThunkPrime(void);
150 /***********************************************************************
152 * Win95 internal thunks *
154 ***********************************************************************/
156 /***********************************************************************
157 * LogApiThk (KERNEL.423)
159 void WINAPI
LogApiThk( LPSTR func
)
161 TRACE( "%s\n", debugstr_a(func
) );
164 /***********************************************************************
165 * LogApiThkLSF (KERNEL32.42)
167 * NOTE: needs to preserve all registers!
169 __ASM_STDCALL_FUNC( LogApiThkLSF
, 4, "ret $4" )
171 /***********************************************************************
172 * LogApiThkSL (KERNEL32.44)
174 * NOTE: needs to preserve all registers!
176 __ASM_STDCALL_FUNC( LogApiThkSL
, 4, "ret $4" )
178 /***********************************************************************
179 * LogCBThkSL (KERNEL32.47)
181 * NOTE: needs to preserve all registers!
183 __ASM_STDCALL_FUNC( LogCBThkSL
, 4, "ret $4" )
185 /***********************************************************************
186 * Generates a FT_Prolog call.
188 * 0FB6D1 movzbl edx,cl
189 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
190 * 68xxxxxxxx push FT_Prolog
193 static void _write_ftprolog(LPBYTE relayCode
,DWORD
*targetTable
) {
197 *x
++ = 0x0f;*x
++=0xb6;*x
++=0xd1; /* movzbl edx,cl */
198 *x
++ = 0x8B;*x
++=0x14;*x
++=0x95;*(DWORD
**)x
= targetTable
;
199 x
+=4; /* mov edx, [4*edx + targetTable] */
200 *x
++ = 0x68; *(void **)x
= FT_Prolog
;
201 x
+=4; /* push FT_Prolog */
202 *x
++ = 0xC3; /* lret */
203 /* fill rest with 0xCC / int 3 */
206 /***********************************************************************
207 * _write_qtthunk (internal)
208 * Generates a QT_Thunk style call.
211 * 8A4DFC mov cl , [ebp-04]
212 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
213 * B8yyyyyyyy mov eax, QT_Thunk
216 static void _write_qtthunk(
217 LPBYTE relayCode
, /* [in] start of QT_Thunk stub */
218 DWORD
*targetTable
/* [in] start of thunk (for index lookup) */
223 *x
++ = 0x33;*x
++=0xC9; /* xor ecx,ecx */
224 *x
++ = 0x8A;*x
++=0x4D;*x
++=0xFC; /* movb cl,[ebp-04] */
225 *x
++ = 0x8B;*x
++=0x14;*x
++=0x8D;*(DWORD
**)x
= targetTable
;
226 x
+=4; /* mov edx, [4*ecx + targetTable */
227 *x
++ = 0xB8; *(void **)x
= QT_Thunk
;
228 x
+=4; /* mov eax , QT_Thunk */
229 *x
++ = 0xFF; *x
++ = 0xE0; /* jmp eax */
230 /* should fill the rest of the 32 bytes with 0xCC */
233 /***********************************************************************
236 static LPVOID
_loadthunk(LPCSTR module
, LPCSTR func
, LPCSTR module32
,
237 struct ThunkDataCommon
*TD32
, DWORD checksum
)
239 struct ThunkDataCommon
*TD16
;
246 LoadLibrary16( "gdi.exe" );
247 LoadLibrary16( "user.exe" );
251 if ((hmod
= LoadLibrary16(module
)) <= 32)
253 ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
254 module
, func
, module32
, module
, hmod
);
258 if ( !(ordinal
= NE_GetOrdinal(hmod
, func
))
259 || !(TD16
= MapSL((SEGPTR
)NE_GetEntryPointEx(hmod
, ordinal
, FALSE
))))
261 ERR("Unable to find thunk data '%s' in %s, required by %s (conflicting/incorrect DLL versions !?).\n",
262 func
, module
, module32
);
266 if (TD32
&& memcmp(TD16
->magic
, TD32
->magic
, 4))
268 ERR("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
269 module
, func
, module32
,
270 TD16
->magic
[0], TD16
->magic
[1], TD16
->magic
[2], TD16
->magic
[3],
271 TD32
->magic
[0], TD32
->magic
[1], TD32
->magic
[2], TD32
->magic
[3]);
275 if (TD32
&& TD16
->checksum
!= TD32
->checksum
)
277 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
278 module
, func
, module32
, TD16
->checksum
, TD32
->checksum
);
282 if (!TD32
&& checksum
&& checksum
!= *(LPDWORD
)TD16
)
284 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
285 module
, func
, module32
, *(LPDWORD
)TD16
, checksum
);
292 /***********************************************************************
293 * GetThunkStuff (KERNEL32.53)
295 LPVOID WINAPI
GetThunkStuff(LPCSTR module
, LPCSTR func
)
297 return _loadthunk(module
, func
, "<kernel>", NULL
, 0L);
300 /***********************************************************************
301 * GetThunkBuff (KERNEL32.52)
302 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
304 LPVOID WINAPI
GetThunkBuff(void)
306 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
309 /***********************************************************************
310 * ThunkConnect32 (KERNEL32.@)
311 * Connects a 32bit and a 16bit thunkbuffer.
313 UINT WINAPI
ThunkConnect32(
314 struct ThunkDataCommon
*TD
, /* [in/out] thunkbuffer */
315 LPSTR thunkfun16
, /* [in] win16 thunkfunction */
316 LPSTR module16
, /* [in] name of win16 dll */
317 LPSTR module32
, /* [in] name of win32 dll */
318 HMODULE hmod32
, /* [in] hmodule of win32 dll */
319 DWORD dwReason
/* [in] initialisation argument */
323 if (!strncmp(TD
->magic
, "SL01", 4))
327 TRACE("SL01 thunk %s (%p) <- %s (%s), Reason: %ld\n",
328 module32
, TD
, module16
, thunkfun16
, dwReason
);
330 else if (!strncmp(TD
->magic
, "LS01", 4))
334 TRACE("LS01 thunk %s (%p) -> %s (%s), Reason: %ld\n",
335 module32
, TD
, module16
, thunkfun16
, dwReason
);
339 ERR("Invalid magic %c%c%c%c\n",
340 TD
->magic
[0], TD
->magic
[1], TD
->magic
[2], TD
->magic
[3]);
346 case DLL_PROCESS_ATTACH
:
348 struct ThunkDataCommon
*TD16
;
349 if (!(TD16
= _loadthunk(module16
, thunkfun16
, module32
, TD
, 0L)))
354 struct ThunkDataSL32
*SL32
= (struct ThunkDataSL32
*)TD
;
355 struct ThunkDataSL16
*SL16
= (struct ThunkDataSL16
*)TD16
;
356 struct SLTargetDB
*tdb
;
358 if (SL16
->fpData
== NULL
)
360 ERR("ThunkConnect16 was not called!\n");
364 SL32
->data
= SL16
->fpData
;
366 tdb
= HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb
));
367 tdb
->process
= GetCurrentProcessId();
368 tdb
->targetTable
= (DWORD
*)(thunkfun16
+ SL32
->offsetTargetTable
);
370 tdb
->next
= SL32
->data
->targetDB
; /* FIXME: not thread-safe! */
371 SL32
->data
->targetDB
= tdb
;
373 TRACE("Process %08lx allocated TargetDB entry for ThunkDataSL %p\n",
374 GetCurrentProcessId(), SL32
->data
);
378 struct ThunkDataLS32
*LS32
= (struct ThunkDataLS32
*)TD
;
379 struct ThunkDataLS16
*LS16
= (struct ThunkDataLS16
*)TD16
;
381 LS32
->targetTable
= MapSL(LS16
->targetTable
);
383 /* write QT_Thunk and FT_Prolog stubs */
384 _write_qtthunk ((LPBYTE
)TD
+ LS32
->offsetQTThunk
, LS32
->targetTable
);
385 _write_ftprolog((LPBYTE
)TD
+ LS32
->offsetFTProlog
, LS32
->targetTable
);
390 case DLL_PROCESS_DETACH
:
398 /**********************************************************************
399 * QT_Thunk (KERNEL32.@)
401 * The target address is in EDX.
402 * The 16bit arguments start at ESP.
403 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
404 * So the stack layout is 16bit argument bytes and then the 64 byte
406 * The scratch buffer is used as work space by Windows' QT_Thunk
408 * As the programs unfortunately don't always provide a fixed size
409 * scratch buffer (danger, stack corruption ahead !!), we simply resort
410 * to copying over the whole EBP-ESP range to the 16bit stack
411 * (as there's no way to safely figure out the param count
412 * due to this misbehaviour of some programs).
415 * See DDJ article 9614c for a very good description of QT_Thunk (also
416 * available online !).
418 * FIXME: DDJ talks of certain register usage rules; I'm not sure
419 * whether we cover this 100%.
421 void WINAPI
__regs_QT_Thunk( CONTEXT
*context
)
426 context16
= *context
;
428 context16
.SegCs
= HIWORD(context
->Edx
);
429 context16
.Eip
= LOWORD(context
->Edx
);
430 /* point EBP to the STACK16FRAME on the stack
431 * for the call_to_16 to set up the register content on calling */
432 context16
.Ebp
= CURRENT_SP
+ FIELD_OFFSET(STACK16FRAME
,bp
);
435 * used to be (problematic):
436 * argsize = context->Ebp - context->Esp - 0x40;
437 * due to some programs abusing the API, we better assume the full
438 * EBP - ESP range for copying instead: */
439 argsize
= context
->Ebp
- context
->Esp
;
441 /* ok, too much is insane; let's limit param count a bit again */
443 argsize
= 64; /* 32 WORDs */
445 WOWCallback16Ex( 0, WCB16_REGS
, argsize
, (void *)context
->Esp
, (DWORD
*)&context16
);
446 context
->Eax
= context16
.Eax
;
447 context
->Edx
= context16
.Edx
;
448 context
->Ecx
= context16
.Ecx
;
450 /* make sure to update the Win32 ESP, too, in order to throw away
451 * the number of parameters that the Win16 function
452 * accepted (that it popped from the corresponding Win16 stack) */
453 context
->Esp
+= LOWORD(context16
.Esp
) - (CURRENT_SP
- argsize
);
455 DEFINE_REGS_ENTRYPOINT( QT_Thunk
)
458 /**********************************************************************
459 * FT_Prolog (KERNEL32.@)
461 * The set of FT_... thunk routines is used instead of QT_Thunk,
462 * if structures have to be converted from 32-bit to 16-bit
463 * (change of member alignment, conversion of members).
465 * The thunk function (as created by the thunk compiler) calls
466 * FT_Prolog at the beginning, to set up a stack frame and
467 * allocate a 64 byte buffer on the stack.
468 * The input parameters (target address and some flags) are
469 * saved for later use by FT_Thunk.
471 * Input: EDX 16-bit target address (SEGPTR)
472 * CX bits 0..7 target number (in target table)
473 * bits 8..9 some flags (unclear???)
474 * bits 10..15 number of DWORD arguments
476 * Output: A new stackframe is created, and a 64 byte buffer
477 * allocated on the stack. The layout of the stack
478 * on return is as follows:
480 * (ebp+4) return address to caller of thunk function
482 * (ebp-4) saved EBX register of caller
483 * (ebp-8) saved ESI register of caller
484 * (ebp-12) saved EDI register of caller
485 * (ebp-16) saved ECX register, containing flags
486 * (ebp-20) bitmap containing parameters that are to be converted
487 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
488 * filled in by the thunk code before calling FT_Thunk
492 * (ebp-48) saved EAX register of caller (unclear, never restored???)
493 * (ebp-52) saved EDX register, containing 16-bit thunk target
498 * ESP is EBP-64 after return.
501 void WINAPI
__regs_FT_Prolog( CONTEXT
*context
)
503 /* Build stack frame */
504 stack32_push(context
, context
->Ebp
);
505 context
->Ebp
= context
->Esp
;
507 /* Allocate 64-byte Thunk Buffer */
509 memset((char *)context
->Esp
, '\0', 64);
511 /* Store Flags (ECX) and Target Address (EDX) */
512 /* Save other registers to be restored later */
513 *(DWORD
*)(context
->Ebp
- 4) = context
->Ebx
;
514 *(DWORD
*)(context
->Ebp
- 8) = context
->Esi
;
515 *(DWORD
*)(context
->Ebp
- 12) = context
->Edi
;
516 *(DWORD
*)(context
->Ebp
- 16) = context
->Ecx
;
518 *(DWORD
*)(context
->Ebp
- 48) = context
->Eax
;
519 *(DWORD
*)(context
->Ebp
- 52) = context
->Edx
;
521 DEFINE_REGS_ENTRYPOINT( FT_Prolog
)
523 /**********************************************************************
524 * FT_Thunk (KERNEL32.@)
526 * This routine performs the actual call to 16-bit code,
527 * similar to QT_Thunk. The differences are:
528 * - The call target is taken from the buffer created by FT_Prolog
529 * - Those arguments requested by the thunk code (by setting the
530 * corresponding bit in the bitmap at EBP-20) are converted
531 * from 32-bit pointers to segmented pointers (those pointers
532 * are guaranteed to point to structures copied to the stack
533 * by the thunk code, so we always use the 16-bit stack selector
534 * for those addresses).
536 * The bit #i of EBP-20 corresponds here to the DWORD starting at
539 * FIXME: It is unclear what happens if there are more than 32 WORDs
540 * of arguments, so that the single DWORD bitmap is no longer
543 void WINAPI
__regs_FT_Thunk( CONTEXT
*context
)
545 DWORD mapESPrelative
= *(DWORD
*)(context
->Ebp
- 20);
546 DWORD callTarget
= *(DWORD
*)(context
->Ebp
- 52);
553 context16
= *context
;
555 context16
.SegCs
= HIWORD(callTarget
);
556 context16
.Eip
= LOWORD(callTarget
);
557 context16
.Ebp
= CURRENT_SP
+ FIELD_OFFSET(STACK16FRAME
,bp
);
559 argsize
= context
->Ebp
-context
->Esp
-0x40;
560 if (argsize
> sizeof(newstack
)) argsize
= sizeof(newstack
);
561 oldstack
= (LPBYTE
)context
->Esp
;
563 memcpy( newstack
, oldstack
, argsize
);
565 for (i
= 0; i
< 32; i
++) /* NOTE: What about > 32 arguments? */
566 if (mapESPrelative
& (1 << i
))
568 SEGPTR
*arg
= (SEGPTR
*)newstack
[i
];
569 *arg
= MAKESEGPTR( CURRENT_SS
, CURRENT_SP
- argsize
+ (*(LPBYTE
*)arg
- oldstack
));
572 WOWCallback16Ex( 0, WCB16_REGS
, argsize
, newstack
, (DWORD
*)&context16
);
573 context
->Eax
= context16
.Eax
;
574 context
->Edx
= context16
.Edx
;
575 context
->Ecx
= context16
.Ecx
;
577 context
->Esp
+= LOWORD(context16
.Esp
) - (CURRENT_SP
- argsize
);
579 /* Copy modified buffers back to 32-bit stack */
580 memcpy( oldstack
, newstack
, argsize
);
582 DEFINE_REGS_ENTRYPOINT( FT_Thunk
)
584 /***********************************************************************
585 * FT_Exit0 (KERNEL32.@)
586 * FT_Exit4 (KERNEL32.@)
587 * FT_Exit8 (KERNEL32.@)
588 * FT_Exit12 (KERNEL32.@)
589 * FT_Exit16 (KERNEL32.@)
590 * FT_Exit20 (KERNEL32.@)
591 * FT_Exit24 (KERNEL32.@)
592 * FT_Exit28 (KERNEL32.@)
593 * FT_Exit32 (KERNEL32.@)
594 * FT_Exit36 (KERNEL32.@)
595 * FT_Exit40 (KERNEL32.@)
596 * FT_Exit44 (KERNEL32.@)
597 * FT_Exit48 (KERNEL32.@)
598 * FT_Exit52 (KERNEL32.@)
599 * FT_Exit56 (KERNEL32.@)
601 * One of the FT_ExitNN functions is called at the end of the thunk code.
602 * It removes the stack frame created by FT_Prolog, moves the function
603 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
604 * value, but the thunk code has moved it from EAX to EBX in the
605 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
606 * and perform a return to the CALLER of the thunk code (while removing
607 * the given number of arguments from the caller's stack).
609 #define FT_EXIT_RESTORE_REGS \
610 "movl %ebx,%eax\n\t" \
611 "movl -4(%ebp),%ebx\n\t" \
612 "movl -8(%ebp),%esi\n\t" \
613 "movl -12(%ebp),%edi\n\t" \
616 #define DEFINE_FT_Exit(n) \
617 __ASM_STDCALL_FUNC( FT_Exit ## n, 0, FT_EXIT_RESTORE_REGS "ret $" #n )
636 /***********************************************************************
637 * ThunkInitLS (KERNEL32.43)
638 * A thunkbuffer link routine
639 * The thunkbuf looks like:
641 * 00: DWORD length ? don't know exactly
642 * 04: SEGPTR ptr ? where does it point to?
643 * The pointer ptr is written into the first DWORD of 'thunk'.
644 * (probably correctly implemented)
647 * segmented pointer to thunk?
649 DWORD WINAPI
ThunkInitLS(
650 LPDWORD thunk
, /* [in] win32 thunk */
651 LPCSTR thkbuf
, /* [in] thkbuffer name in win16 dll */
652 DWORD len
, /* [in] thkbuffer length */
653 LPCSTR dll16
, /* [in] name of win16 dll */
654 LPCSTR dll32
/* [in] name of win32 dll (FIXME: not used?) */
658 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
668 /***********************************************************************
669 * Common32ThkLS (KERNEL32.45)
671 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
672 * style thunks. The basic difference is that the parameter conversion
673 * is done completely on the *16-bit* side here. Thus we do not call
674 * the 16-bit target directly, but call a common entry point instead.
675 * This entry function then calls the target according to the target
676 * number passed in the DI register.
678 * Input: EAX SEGPTR to the common 16-bit entry point
679 * CX offset in thunk table (target number * 4)
680 * DX error return value if execution fails (unclear???)
681 * EDX.HI number of DWORD parameters
683 * (Note that we need to move the thunk table offset from CX to DI !)
685 * The called 16-bit stub expects its stack to look like this:
687 * (esp+40) 32-bit arguments
689 * (esp+8) 32 byte of stack space available as buffer
690 * (esp) 8 byte return address for use with 0x66 lret
692 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
693 * and uses the EAX register to return a DWORD return value.
694 * Thus we need to use a special assembly glue routine
695 * (CallRegisterLongProc instead of CallRegisterShortProc).
697 * Finally, we return to the caller, popping the arguments off
698 * the stack. The number of arguments to be popped is returned
699 * in the BL register by the called 16-bit routine.
702 void WINAPI
__regs_Common32ThkLS( CONTEXT
*context
)
707 context16
= *context
;
709 context16
.Edi
= LOWORD(context
->Ecx
);
710 context16
.SegCs
= HIWORD(context
->Eax
);
711 context16
.Eip
= LOWORD(context
->Eax
);
712 context16
.Ebp
= CURRENT_SP
+ FIELD_OFFSET(STACK16FRAME
,bp
);
714 argsize
= HIWORD(context
->Edx
) * 4;
716 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
717 if (context
->Edx
== context
->Eip
)
720 /* Note: the first 32 bytes we copy are just garbage from the 32-bit stack, in order to reserve
721 * the space. It is safe to do that since the register function prefix has reserved
722 * a lot more space than that below context->Esp.
724 WOWCallback16Ex( 0, WCB16_REGS
, argsize
+ 32, (LPBYTE
)context
->Esp
- 32, (DWORD
*)&context16
);
725 context
->Eax
= context16
.Eax
;
727 /* Clean up caller's stack frame */
728 context
->Esp
+= LOBYTE(context16
.Ebx
);
730 DEFINE_REGS_ENTRYPOINT( Common32ThkLS
)
732 /***********************************************************************
733 * OT_32ThkLSF (KERNEL32.40)
735 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
736 * argument processing is done on both the 32-bit and the 16-bit side:
737 * The 32-bit side prepares arguments, copying them onto the stack.
739 * When this routine is called, the first word on the stack is the
740 * number of argument bytes prepared by the 32-bit code, and EDX
741 * contains the 16-bit target address.
743 * The called 16-bit routine is another relaycode, doing further
744 * argument processing and then calling the real 16-bit target
745 * whose address is stored at [bp-04].
747 * The call proceeds using a normal CallRegisterShortProc.
748 * After return from the 16-bit relaycode, the arguments need
749 * to be copied *back* to the 32-bit stack, since the 32-bit
750 * relaycode processes output parameters.
752 * Note that we copy twice the number of arguments, since some of the
753 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
754 * arguments of the caller!
756 * (Note that this function seems only to be used for
757 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
759 void WINAPI
__regs_OT_32ThkLSF( CONTEXT
*context
)
764 context16
= *context
;
766 context16
.SegCs
= HIWORD(context
->Edx
);
767 context16
.Eip
= LOWORD(context
->Edx
);
768 context16
.Ebp
= CURRENT_SP
+ FIELD_OFFSET(STACK16FRAME
,bp
);
770 argsize
= 2 * *(WORD
*)context
->Esp
+ 2;
772 WOWCallback16Ex( 0, WCB16_REGS
, argsize
, (void *)context
->Esp
, (DWORD
*)&context16
);
773 context
->Eax
= context16
.Eax
;
774 context
->Edx
= context16
.Edx
;
776 /* Copy modified buffers back to 32-bit stack */
777 memcpy( (LPBYTE
)context
->Esp
,
778 (LPBYTE
)CURRENT_STACK16
- argsize
, argsize
);
780 context
->Esp
+= LOWORD(context16
.Esp
) - (CURRENT_SP
- argsize
);
782 DEFINE_REGS_ENTRYPOINT( OT_32ThkLSF
)
784 /***********************************************************************
785 * ThunkInitLSF (KERNEL32.41)
786 * A thunk setup routine.
787 * Expects a pointer to a preinitialized thunkbuffer in the first argument
789 *| 00..03: unknown (pointer, check _41, _43, _46)
790 *| 04: EB1E jmp +0x20
792 *| 06..23: unknown (space for replacement code, check .90)
794 *| 24:>E800000000 call offset 29
795 *| 29:>58 pop eax ( target of call )
796 *| 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
797 *| 2F: BAxxxxxxxx mov edx,xxxxxxxx
798 *| 34: 68yyyyyyyy push KERNEL32.90
801 *| 3A: EB1E jmp +0x20
802 *| 3E ... 59: unknown (space for replacement code?)
803 *| 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
805 *| 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
807 *| 67: 68xxxxxxxx push xxxxxxxx
808 *| 6C: 68yyyyyyyy push KERNEL32.89
811 * This function checks if the code is there, and replaces the yyyyyyyy entries
812 * by the functionpointers.
813 * The thunkbuf looks like:
815 *| 00: DWORD length ? don't know exactly
816 *| 04: SEGPTR ptr ? where does it point to?
817 * The segpointer ptr is written into the first DWORD of 'thunk'.
820 * unclear, pointer to win16 thkbuffer?
822 LPVOID WINAPI
ThunkInitLSF(
823 LPBYTE thunk
, /* [in] win32 thunk */
824 LPCSTR thkbuf
, /* [in] thkbuffer name in win16 dll */
825 DWORD len
, /* [in] length of thkbuffer */
826 LPCSTR dll16
, /* [in] name of win16 dll */
827 LPCSTR dll32
/* [in] name of win32 dll */
831 /* FIXME: add checks for valid code ... */
832 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
833 *(void **)(thunk
+0x35) = QT_ThunkPrime
;
834 *(void **)(thunk
+0x6D) = FT_PrologPrime
;
836 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
839 addr2
= MapSL(addr
[1]);
841 *(DWORD
*)thunk
= (DWORD
)addr2
;
846 /***********************************************************************
847 * FT_PrologPrime (KERNEL32.89)
849 * This function is called from the relay code installed by
850 * ThunkInitLSF. It replaces the location from where it was
851 * called by a standard FT_Prolog call stub (which is 'primed'
852 * by inserting the correct target table pointer).
853 * Finally, it calls that stub.
855 * Input: ECX target number + flags (passed through to FT_Prolog)
856 * (ESP) offset of location where target table pointer
857 * is stored, relative to the start of the relay code
858 * (ESP+4) pointer to start of relay code
859 * (this is where the FT_Prolog call stub gets written to)
861 * Note: The two DWORD arguments get popped off the stack.
864 void WINAPI
__regs_FT_PrologPrime( CONTEXT
*context
)
866 DWORD targetTableOffset
;
869 /* Compensate for the fact that the Wine register relay code thought
870 we were being called, although we were in fact jumped to */
873 /* Write FT_Prolog call stub */
874 targetTableOffset
= stack32_pop(context
);
875 relayCode
= (LPBYTE
)stack32_pop(context
);
876 _write_ftprolog( relayCode
, *(DWORD
**)(relayCode
+targetTableOffset
) );
878 /* Jump to the call stub just created */
879 context
->Eip
= (DWORD
)relayCode
;
881 DEFINE_REGS_ENTRYPOINT( FT_PrologPrime
)
883 /***********************************************************************
884 * QT_ThunkPrime (KERNEL32.90)
886 * This function corresponds to FT_PrologPrime, but installs a
887 * call stub for QT_Thunk instead.
889 * Input: (EBP-4) target number (passed through to QT_Thunk)
890 * EDX target table pointer location offset
891 * EAX start of relay code
894 void WINAPI
__regs_QT_ThunkPrime( CONTEXT
*context
)
896 DWORD targetTableOffset
;
899 /* Compensate for the fact that the Wine register relay code thought
900 we were being called, although we were in fact jumped to */
903 /* Write QT_Thunk call stub */
904 targetTableOffset
= context
->Edx
;
905 relayCode
= (LPBYTE
)context
->Eax
;
906 _write_qtthunk( relayCode
, *(DWORD
**)(relayCode
+targetTableOffset
) );
908 /* Jump to the call stub just created */
909 context
->Eip
= (DWORD
)relayCode
;
911 DEFINE_REGS_ENTRYPOINT( QT_ThunkPrime
)
913 /***********************************************************************
914 * ThunkInitSL (KERNEL32.46)
915 * Another thunkbuf link routine.
916 * The start of the thunkbuf looks like this:
918 * 04: SEGPTR address for thunkbuffer pointer
924 VOID WINAPI
ThunkInitSL(
925 LPBYTE thunk
, /* [in] start of thunkbuffer */
926 LPCSTR thkbuf
, /* [in] name/ordinal of thunkbuffer in win16 dll */
927 DWORD len
, /* [in] length of thunkbuffer */
928 LPCSTR dll16
, /* [in] name of win16 dll containing the thkbuf */
929 LPCSTR dll32
/* [in] win32 dll. FIXME: strange, unused */
933 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
936 *(DWORD
*)MapSL(addr
[1]) = (DWORD
)thunk
;
939 /**********************************************************************
940 * SSInit (KERNEL.700)
944 BOOL WINAPI
SSInit16(void)
949 /**********************************************************************
950 * SSOnBigStack (KERNEL32.87)
951 * Check if thunking is initialized (ss selector set up etc.)
952 * We do that differently, so just return TRUE.
957 BOOL WINAPI
SSOnBigStack(void)
959 TRACE("Yes, thunking is initialized\n");
963 /**********************************************************************
964 * SSConfirmSmallStack (KERNEL.704)
966 * Abort if not on small stack.
968 * This must be a register routine as it has to preserve *all* registers.
970 void WINAPI
SSConfirmSmallStack( CONTEXT
*context
)
972 /* We are always on the small stack while in 16-bit code ... */
975 /**********************************************************************
976 * SSCall (KERNEL32.88)
977 * One of the real thunking functions. This one seems to be for 32<->32
978 * thunks. It should probably be capable of crossing processboundaries.
980 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
984 * Thunked function result.
986 DWORD WINAPIV
SSCall(
987 DWORD nr
, /* [in] number of argument bytes */
988 DWORD flags
, /* [in] FIXME: flags ? */
989 FARPROC fun
, /* [in] function to call */
990 ... /* [in/out] arguments */
992 return call_entry_point( fun
, nr
/ sizeof(DWORD
), (DWORD
*)&fun
+ 1 );
995 /**********************************************************************
996 * W32S_BackTo32 (KERNEL32.51)
998 void WINAPI
__regs_W32S_BackTo32( CONTEXT
*context
)
1000 LPDWORD stack
= (LPDWORD
)context
->Esp
;
1001 FARPROC proc
= (FARPROC
)context
->Eip
;
1003 context
->Eax
= call_entry_point( proc
, 10, stack
+ 1 );
1004 context
->Eip
= stack32_pop(context
);
1006 DEFINE_REGS_ENTRYPOINT( W32S_BackTo32
)
1008 /**********************************************************************
1009 * AllocSLCallback (KERNEL32.@)
1011 * Allocate a 16->32 callback.
1014 * Win95 uses some structchains for callbacks. It allocates them
1015 * in blocks of 100 entries, size 32 bytes each, layout:
1017 *| 0: PTR nextblockstart
1019 *| 8: WORD sel ( start points to blockstart)
1023 *| 18: PDB *owning_process;
1024 *| 1C: PTR blockstart
1026 * We ignore this for now. (Just a note for further developers)
1027 * FIXME: use this method, so we don't waste selectors...
1029 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
1030 * the 0x66 prefix switches from word->long registers.
1033 *| 6668x arg2 x pushl <arg2>
1035 *| EAx arg1 x jmpf <arg1>
1037 * returns the startaddress of this thunk.
1039 * Note, that they look very similar to the ones allocates by THUNK_Alloc.
1041 * A segmented pointer to the start of the thunk
1045 DWORD finalizer
, /* [in] Finalizer function */
1046 DWORD callback
/* [in] Callback function */
1048 LPBYTE x
,thunk
= HeapAlloc( GetProcessHeap(), 0, 32 );
1052 *x
++=0x66;*x
++=0x5a; /* popl edx */
1053 *x
++=0x66;*x
++=0x68;*(DWORD
*)x
=finalizer
;x
+=4; /* pushl finalizer */
1054 *x
++=0x66;*x
++=0x52; /* pushl edx */
1055 *x
++=0xea;*(DWORD
*)x
=callback
;x
+=4; /* jmpf callback */
1057 *(DWORD
*)(thunk
+18) = GetCurrentProcessId();
1059 sel
= SELECTOR_AllocBlock( thunk
, 32, LDT_FLAGS_CODE
);
1063 /**********************************************************************
1064 * FreeSLCallback (KERNEL32.@)
1065 * Frees the specified 16->32 callback
1072 DWORD x
/* [in] 16 bit callback (segmented pointer?) */
1074 FIXME("(0x%08lx): stub\n",x
);
1077 /**********************************************************************
1078 * AllocMappedBuffer (KERNEL32.38)
1080 * This is an undocumented KERNEL32 function that
1081 * SMapLS's a GlobalAlloc'ed buffer.
1084 * EDI register: pointer to buffer
1087 * The buffer is preceded by 8 bytes:
1090 * edi-4 SEGPTR to buffer
1091 * edi-8 some magic Win95 needs for SUnMapLS
1092 * (we use it for the memory handle)
1094 * The SEGPTR is used by the caller!
1096 void WINAPI
__regs_AllocMappedBuffer(
1097 CONTEXT
*context
/* [in] EDI register: size of buffer to allocate */
1099 HGLOBAL handle
= GlobalAlloc(0, context
->Edi
+ 8);
1100 DWORD
*buffer
= GlobalLock(handle
);
1104 if (!(ptr
= MapLS(buffer
+ 2)))
1106 GlobalUnlock(handle
);
1111 context
->Eax
= context
->Edi
= 0;
1114 buffer
[0] = (DWORD
)handle
;
1118 context
->Edi
= (DWORD
)(buffer
+ 2);
1121 DEFINE_REGS_ENTRYPOINT( AllocMappedBuffer
)
1123 /**********************************************************************
1124 * FreeMappedBuffer (KERNEL32.39)
1126 * Free a buffer allocated by AllocMappedBuffer
1131 void WINAPI
__regs_FreeMappedBuffer(
1132 CONTEXT
*context
/* [in] EDI register: pointer to buffer */
1136 DWORD
*buffer
= (DWORD
*)context
->Edi
- 2;
1140 GlobalUnlock((HGLOBAL
)buffer
[0]);
1141 GlobalFree((HGLOBAL
)buffer
[0]);
1144 DEFINE_REGS_ENTRYPOINT( FreeMappedBuffer
)
1146 /**********************************************************************
1147 * GetTEBSelectorFS (KERNEL.475)
1148 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1150 void WINAPI
GetTEBSelectorFS16(void)
1152 CURRENT_STACK16
->fs
= get_fs();
1155 /**********************************************************************
1156 * IsPeFormat (KERNEL.431)
1158 * Determine if a file is a PE format executable.
1162 * FALSE if the file could not be opened or is not a PE file.
1165 * If fn is given as NULL then the function expects hf16 to be valid.
1167 BOOL16 WINAPI
IsPeFormat16(
1168 LPSTR fn
, /* [in] Filename to the executable */
1169 HFILE16 hf16
) /* [in] An open file handle */
1172 IMAGE_DOS_HEADER mzh
;
1176 if (fn
) hf16
= OpenFile16(fn
,&ofs
,OF_READ
);
1177 if (hf16
== HFILE_ERROR16
) return FALSE
;
1178 _llseek16(hf16
,0,SEEK_SET
);
1179 if (sizeof(mzh
)!=_lread16(hf16
,&mzh
,sizeof(mzh
))) goto done
;
1180 if (mzh
.e_magic
!=IMAGE_DOS_SIGNATURE
) goto done
;
1181 _llseek16(hf16
,mzh
.e_lfanew
,SEEK_SET
);
1182 if (sizeof(DWORD
)!=_lread16(hf16
,&xmagic
,sizeof(DWORD
))) goto done
;
1183 ret
= (xmagic
== IMAGE_NT_SIGNATURE
);
1190 /***********************************************************************
1191 * K32Thk1632Prolog (KERNEL32.@)
1193 void WINAPI
__regs_K32Thk1632Prolog( CONTEXT
*context
)
1195 LPBYTE code
= (LPBYTE
)context
->Eip
- 5;
1197 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1198 of 16->32 thunks instead of using one of the standard methods!
1199 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1200 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1201 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1202 bypassed, which means it will crash the next time the 32-bit OLE
1203 code thunks down again to 16-bit (this *will* happen!).
1205 The following hack tries to recognize this situation.
1206 This is possible since the called stubs in OLECLI32/OLESVR32 all
1207 look exactly the same:
1208 00 E8xxxxxxxx call K32Thk1632Prolog
1209 05 FF55FC call [ebp-04]
1210 08 E8xxxxxxxx call K32Thk1632Epilog
1213 If we recognize this situation, we try to simulate the actions
1214 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1215 to our 32-bit stack, creating a proper STACK16FRAME and
1216 updating cur_stack. */
1218 if ( code
[5] == 0xFF && code
[6] == 0x55 && code
[7] == 0xFC
1219 && code
[13] == 0x66 && code
[14] == 0xCB)
1221 DWORD argSize
= context
->Ebp
- context
->Esp
;
1222 char *stack16
= (char *)context
->Esp
- 4;
1223 STACK16FRAME
*frame16
= (STACK16FRAME
*)stack16
- 1;
1224 STACK32FRAME
*frame32
= (STACK32FRAME
*)kernel_get_thread_data()->stack
;
1225 char *stack32
= (char *)frame32
- argSize
;
1226 WORD stackSel
= SELECTOROF(frame32
->frame16
);
1227 DWORD stackBase
= GetSelectorBase(stackSel
);
1229 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %04x:%04x\n",
1230 context
->Ebp
, context
->Esp
, CURRENT_SS
, CURRENT_SP
);
1232 memset(frame16
, '\0', sizeof(STACK16FRAME
));
1233 frame16
->frame32
= frame32
;
1234 frame16
->ebp
= context
->Ebp
;
1236 memcpy(stack32
, stack16
, argSize
);
1237 CURRENT_SS
= stackSel
;
1238 CURRENT_SP
= (DWORD
)frame16
- stackBase
;
1240 context
->Esp
= (DWORD
)stack32
+ 4;
1241 context
->Ebp
= context
->Esp
+ argSize
;
1243 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %04x:%04x\n",
1244 context
->Ebp
, context
->Esp
, CURRENT_SS
, CURRENT_SP
);
1247 /* entry_point is never used again once the entry point has
1248 been called. Thus we re-use it to hold the Win16Lock count */
1249 ReleaseThunkLock(&CURRENT_STACK16
->entry_point
);
1251 DEFINE_REGS_ENTRYPOINT( K32Thk1632Prolog
)
1253 /***********************************************************************
1254 * K32Thk1632Epilog (KERNEL32.@)
1256 void WINAPI
__regs_K32Thk1632Epilog( CONTEXT
*context
)
1258 LPBYTE code
= (LPBYTE
)context
->Eip
- 13;
1260 RestoreThunkLock(CURRENT_STACK16
->entry_point
);
1262 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1264 if ( code
[5] == 0xFF && code
[6] == 0x55 && code
[7] == 0xFC
1265 && code
[13] == 0x66 && code
[14] == 0xCB)
1267 STACK16FRAME
*frame16
= CURRENT_STACK16
;
1268 char *stack16
= (char *)(frame16
+ 1);
1269 DWORD argSize
= frame16
->ebp
- (DWORD
)stack16
;
1270 char *stack32
= (char *)frame16
->frame32
- argSize
;
1272 DWORD nArgsPopped
= context
->Esp
- (DWORD
)stack32
;
1274 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %04x:%04x\n",
1275 context
->Ebp
, context
->Esp
, CURRENT_SS
, CURRENT_SP
);
1277 kernel_get_thread_data()->stack
= (SEGPTR
)frame16
->frame32
;
1279 context
->Esp
= (DWORD
)stack16
+ nArgsPopped
;
1280 context
->Ebp
= frame16
->ebp
;
1282 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %04x:%04x\n",
1283 context
->Ebp
, context
->Esp
, CURRENT_SS
, CURRENT_SP
);
1286 DEFINE_REGS_ENTRYPOINT( K32Thk1632Epilog
)
1288 /*********************************************************************
1289 * PK16FNF [KERNEL32.91]
1291 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1292 * string buffer with the 8.3 filename of a recently loaded 16-bit
1293 * module. It is unknown exactly what modules trigger this
1294 * mechanism or what purpose this serves. Win98 Explorer (and
1295 * probably also Win95 with IE 4 shell integration) calls this
1296 * several times during initialization.
1298 * FIXME: find out what this really does and make it work.
1300 void WINAPI
PK16FNF(LPSTR strPtr
)
1302 FIXME("(%p): stub\n", strPtr
);
1304 /* fill in a fake filename that'll be easy to recognize */
1305 strcpy(strPtr
, "WINESTUB.FIX");
1308 /***********************************************************************
1309 * 16->32 Flat Thunk routines:
1312 /***********************************************************************
1313 * ThunkConnect16 (KERNEL.651)
1314 * Connects a 32bit and a 16bit thunkbuffer.
1316 UINT WINAPI
ThunkConnect16(
1317 LPSTR module16
, /* [in] name of win16 dll */
1318 LPSTR module32
, /* [in] name of win32 dll */
1319 HINSTANCE16 hInst16
, /* [in] hInst of win16 dll */
1320 DWORD dwReason
, /* [in] initialisation argument */
1321 struct ThunkDataCommon
*TD
, /* [in/out] thunkbuffer */
1322 LPSTR thunkfun32
, /* [in] win32 thunkfunction */
1323 WORD cs
/* [in] CS of win16 dll */
1327 if (!strncmp(TD
->magic
, "SL01", 4))
1331 TRACE("SL01 thunk %s (%p) -> %s (%s), Reason: %ld\n",
1332 module16
, TD
, module32
, thunkfun32
, dwReason
);
1334 else if (!strncmp(TD
->magic
, "LS01", 4))
1336 directionSL
= FALSE
;
1338 TRACE("LS01 thunk %s (%p) <- %s (%s), Reason: %ld\n",
1339 module16
, TD
, module32
, thunkfun32
, dwReason
);
1343 ERR("Invalid magic %c%c%c%c\n",
1344 TD
->magic
[0], TD
->magic
[1], TD
->magic
[2], TD
->magic
[3]);
1350 case DLL_PROCESS_ATTACH
:
1353 struct ThunkDataSL16
*SL16
= (struct ThunkDataSL16
*)TD
;
1354 struct ThunkDataSL
*SL
= SL16
->fpData
;
1358 SL
= HeapAlloc(GetProcessHeap(), 0, sizeof(*SL
));
1360 SL
->common
= SL16
->common
;
1361 SL
->flags1
= SL16
->flags1
;
1362 SL
->flags2
= SL16
->flags2
;
1364 SL
->apiDB
= MapSL(SL16
->apiDatabase
);
1365 SL
->targetDB
= NULL
;
1367 lstrcpynA(SL
->pszDll16
, module16
, 255);
1368 lstrcpynA(SL
->pszDll32
, module32
, 255);
1370 /* We should create a SEGPTR to the ThunkDataSL,
1371 but since the contents are not in the original format,
1372 any access to this by 16-bit code would crash anyway. */
1378 if (SL
->flags2
& 0x80000000)
1380 TRACE("Preloading 32-bit library\n");
1381 LoadLibraryA(module32
);
1390 case DLL_PROCESS_DETACH
:
1391 /* FIXME: cleanup */
1399 /***********************************************************************
1400 * C16ThkSL (KERNEL.630)
1403 void WINAPI
C16ThkSL(CONTEXT
*context
)
1405 LPBYTE stub
= MapSL(context
->Eax
), x
= stub
;
1407 /* We produce the following code:
1412 * mov edx, es:[ecx + $EDX]
1417 * call __FLATCS:__wine_call_from_16_thunk
1420 *x
++ = 0xB8; *(WORD
*)x
= get_ds(); x
+= sizeof(WORD
);
1421 *x
++ = 0x8E; *x
++ = 0xC0;
1422 *x
++ = 0x66; *x
++ = 0x0F; *x
++ = 0xB7; *x
++ = 0xC9;
1423 *x
++ = 0x67; *x
++ = 0x66; *x
++ = 0x26; *x
++ = 0x8B;
1424 *x
++ = 0x91; *(DWORD
*)x
= context
->Edx
; x
+= sizeof(DWORD
);
1427 *x
++ = 0x66; *x
++ = 0x52;
1429 *x
++ = 0x66; *x
++ = 0x52;
1430 *x
++ = 0x66; *x
++ = 0x9A;
1431 *(void **)x
= __wine_call_from_16_thunk
; x
+= sizeof(void *);
1432 *(WORD
*)x
= get_cs(); x
+= sizeof(WORD
);
1434 /* Jump to the stub code just created */
1435 context
->Eip
= LOWORD(context
->Eax
);
1436 context
->SegCs
= HIWORD(context
->Eax
);
1438 /* Since C16ThkSL got called by a jmp, we need to leave the
1439 original return address on the stack */
1443 /***********************************************************************
1444 * C16ThkSL01 (KERNEL.631)
1447 void WINAPI
C16ThkSL01(CONTEXT
*context
)
1449 LPBYTE stub
= MapSL(context
->Eax
), x
= stub
;
1453 struct ThunkDataSL16
*SL16
= MapSL(context
->Edx
);
1454 struct ThunkDataSL
*td
= SL16
->fpData
;
1456 DWORD procAddress
= (DWORD
)GetProcAddress16(GetModuleHandle16("KERNEL"), (LPCSTR
)631);
1460 ERR("ThunkConnect16 was not called!\n");
1464 TRACE("Creating stub for ThunkDataSL %p\n", td
);
1467 /* We produce the following code:
1476 * call __FLATCS:__wine_call_from_16_thunk
1479 *x
++ = 0x66; *x
++ = 0x33; *x
++ = 0xC0;
1480 *x
++ = 0x66; *x
++ = 0xBA; *(void **)x
= td
; x
+= sizeof(void *);
1481 *x
++ = 0x9A; *(DWORD
*)x
= procAddress
; x
+= sizeof(DWORD
);
1484 *x
++ = 0x66; *x
++ = 0x52;
1486 *x
++ = 0x66; *x
++ = 0x52;
1487 *x
++ = 0x66; *x
++ = 0x9A;
1488 *(void **)x
= __wine_call_from_16_thunk
; x
+= sizeof(void *);
1489 *(WORD
*)x
= get_cs(); x
+= sizeof(WORD
);
1491 /* Jump to the stub code just created */
1492 context
->Eip
= LOWORD(context
->Eax
);
1493 context
->SegCs
= HIWORD(context
->Eax
);
1495 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1496 original return address on the stack */
1501 struct ThunkDataSL
*td
= (struct ThunkDataSL
*)context
->Edx
;
1502 DWORD targetNr
= LOWORD(context
->Ecx
) / 4;
1503 struct SLTargetDB
*tdb
;
1505 TRACE("Process %08lx calling target %ld of ThunkDataSL %p\n",
1506 GetCurrentProcessId(), targetNr
, td
);
1508 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
1509 if (tdb
->process
== GetCurrentProcessId())
1514 TRACE("Loading 32-bit library %s\n", td
->pszDll32
);
1515 LoadLibraryA(td
->pszDll32
);
1517 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
1518 if (tdb
->process
== GetCurrentProcessId())
1524 context
->Edx
= tdb
->targetTable
[targetNr
];
1526 TRACE("Call target is %08lx\n", context
->Edx
);
1530 WORD
*stack
= MapSL( MAKESEGPTR(context
->SegSs
, LOWORD(context
->Esp
)) );
1531 context
->Edx
= (context
->Edx
& ~0xffff) | HIWORD(td
->apiDB
[targetNr
].errorReturnValue
);
1532 context
->Eax
= (context
->Eax
& ~0xffff) | LOWORD(td
->apiDB
[targetNr
].errorReturnValue
);
1533 context
->Eip
= stack
[2];
1534 context
->SegCs
= stack
[3];
1535 context
->Esp
+= td
->apiDB
[targetNr
].nrArgBytes
+ 4;
1537 ERR("Process %08lx did not ThunkConnect32 %s to %s\n",
1538 GetCurrentProcessId(), td
->pszDll32
, td
->pszDll16
);
1544 /***********************************************************************
1545 * 16<->32 Thunklet/Callback API:
1548 #include "pshpack1.h"
1549 typedef struct _THUNKLET
1564 struct _THUNKLET
*next
;
1566 #include "poppack.h"
1568 #define THUNKLET_TYPE_LS 1
1569 #define THUNKLET_TYPE_SL 2
1571 static HANDLE ThunkletHeap
= 0;
1572 static WORD ThunkletCodeSel
;
1573 static THUNKLET
*ThunkletAnchor
= NULL
;
1575 static FARPROC ThunkletSysthunkGlueLS
= 0;
1576 static SEGPTR ThunkletSysthunkGlueSL
= 0;
1578 static FARPROC ThunkletCallbackGlueLS
= 0;
1579 static SEGPTR ThunkletCallbackGlueSL
= 0;
1582 /* map a thunk allocated on ThunkletHeap to a 16-bit pointer */
1583 static inline SEGPTR
get_segptr( void *thunk
)
1585 if (!thunk
) return 0;
1586 return MAKESEGPTR( ThunkletCodeSel
, (char *)thunk
- (char *)ThunkletHeap
);
1589 /***********************************************************************
1592 static BOOL
THUNK_Init(void)
1596 ThunkletHeap
= HeapCreate( HEAP_CREATE_ENABLE_EXECUTE
, 0x10000, 0x10000 );
1597 if (!ThunkletHeap
) return FALSE
;
1599 ThunkletCodeSel
= SELECTOR_AllocBlock( ThunkletHeap
, 0x10000, LDT_FLAGS_CODE
);
1601 thunk
= HeapAlloc( ThunkletHeap
, 0, 5 );
1602 if (!thunk
) return FALSE
;
1604 ThunkletSysthunkGlueLS
= (FARPROC
)thunk
;
1605 *thunk
++ = 0x58; /* popl eax */
1606 *thunk
++ = 0xC3; /* ret */
1608 ThunkletSysthunkGlueSL
= get_segptr( thunk
);
1609 *thunk
++ = 0x66; *thunk
++ = 0x58; /* popl eax */
1610 *thunk
++ = 0xCB; /* lret */
1615 /***********************************************************************
1616 * SetThunkletCallbackGlue (KERNEL.560)
1618 void WINAPI
SetThunkletCallbackGlue16( FARPROC glueLS
, SEGPTR glueSL
)
1620 ThunkletCallbackGlueLS
= glueLS
;
1621 ThunkletCallbackGlueSL
= glueSL
;
1625 /***********************************************************************
1626 * THUNK_FindThunklet
1628 static THUNKLET
*THUNK_FindThunklet( DWORD target
, DWORD relay
,
1629 DWORD glue
, BYTE type
)
1633 for (thunk
= ThunkletAnchor
; thunk
; thunk
= thunk
->next
)
1634 if ( thunk
->type
== type
1635 && thunk
->target
== target
1636 && thunk
->relay
== relay
1637 && ( type
== THUNKLET_TYPE_LS
?
1638 ( thunk
->glue
== glue
- (DWORD
)&thunk
->type
)
1639 : ( thunk
->glue
== glue
) ) )
1645 /***********************************************************************
1646 * THUNK_AllocLSThunklet
1648 static FARPROC
THUNK_AllocLSThunklet( SEGPTR target
, DWORD relay
,
1649 FARPROC glue
, HTASK16 owner
)
1651 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
1655 TDB
*pTask
= GlobalLock16( owner
);
1657 if (!ThunkletHeap
) THUNK_Init();
1658 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
1661 thunk
->prefix_target
= thunk
->prefix_relay
= 0x90;
1662 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
1663 thunk
->jmp_glue
= 0xE9;
1665 thunk
->target
= (DWORD
)target
;
1666 thunk
->relay
= relay
;
1667 thunk
->glue
= (DWORD
)glue
- (DWORD
)&thunk
->type
;
1669 thunk
->type
= THUNKLET_TYPE_LS
;
1670 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
1672 thunk
->next
= ThunkletAnchor
;
1673 ThunkletAnchor
= thunk
;
1676 return (FARPROC
)thunk
;
1679 /***********************************************************************
1680 * THUNK_AllocSLThunklet
1682 static SEGPTR
THUNK_AllocSLThunklet( FARPROC target
, DWORD relay
,
1683 SEGPTR glue
, HTASK16 owner
)
1685 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
1689 TDB
*pTask
= GlobalLock16( owner
);
1691 if (!ThunkletHeap
) THUNK_Init();
1692 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
1695 thunk
->prefix_target
= thunk
->prefix_relay
= 0x66;
1696 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
1697 thunk
->jmp_glue
= 0xEA;
1699 thunk
->target
= (DWORD
)target
;
1700 thunk
->relay
= relay
;
1701 thunk
->glue
= (DWORD
)glue
;
1703 thunk
->type
= THUNKLET_TYPE_SL
;
1704 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
1706 thunk
->next
= ThunkletAnchor
;
1707 ThunkletAnchor
= thunk
;
1710 return get_segptr( thunk
);
1713 /**********************************************************************
1716 static BOOL16
IsLSThunklet( THUNKLET
*thunk
)
1718 return thunk
->prefix_target
== 0x90 && thunk
->pushl_target
== 0x68
1719 && thunk
->prefix_relay
== 0x90 && thunk
->pushl_relay
== 0x68
1720 && thunk
->jmp_glue
== 0xE9 && thunk
->type
== THUNKLET_TYPE_LS
;
1723 /**********************************************************************
1724 * IsSLThunklet (KERNEL.612)
1726 BOOL16 WINAPI
IsSLThunklet16( THUNKLET
*thunk
)
1728 return thunk
->prefix_target
== 0x66 && thunk
->pushl_target
== 0x68
1729 && thunk
->prefix_relay
== 0x66 && thunk
->pushl_relay
== 0x68
1730 && thunk
->jmp_glue
== 0xEA && thunk
->type
== THUNKLET_TYPE_SL
;
1735 /***********************************************************************
1736 * AllocLSThunkletSysthunk (KERNEL.607)
1738 FARPROC WINAPI
AllocLSThunkletSysthunk16( SEGPTR target
,
1739 FARPROC relay
, DWORD dummy
)
1741 if (!ThunkletSysthunkGlueLS
) THUNK_Init();
1742 return THUNK_AllocLSThunklet( (SEGPTR
)relay
, (DWORD
)target
,
1743 ThunkletSysthunkGlueLS
, GetCurrentTask() );
1746 /***********************************************************************
1747 * AllocSLThunkletSysthunk (KERNEL.608)
1749 SEGPTR WINAPI
AllocSLThunkletSysthunk16( FARPROC target
,
1750 SEGPTR relay
, DWORD dummy
)
1752 if (!ThunkletSysthunkGlueSL
) THUNK_Init();
1753 return THUNK_AllocSLThunklet( (FARPROC
)relay
, (DWORD
)target
,
1754 ThunkletSysthunkGlueSL
, GetCurrentTask() );
1758 /***********************************************************************
1759 * AllocLSThunkletCallbackEx (KERNEL.567)
1761 FARPROC WINAPI
AllocLSThunkletCallbackEx16( SEGPTR target
,
1762 DWORD relay
, HTASK16 task
)
1764 THUNKLET
*thunk
= MapSL( target
);
1765 if ( !thunk
) return NULL
;
1767 if ( IsSLThunklet16( thunk
) && thunk
->relay
== relay
1768 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
1769 return (FARPROC
)thunk
->target
;
1771 return THUNK_AllocLSThunklet( target
, relay
,
1772 ThunkletCallbackGlueLS
, task
);
1775 /***********************************************************************
1776 * AllocSLThunkletCallbackEx (KERNEL.568)
1778 SEGPTR WINAPI
AllocSLThunkletCallbackEx16( FARPROC target
,
1779 DWORD relay
, HTASK16 task
)
1781 THUNKLET
*thunk
= (THUNKLET
*)target
;
1782 if ( !thunk
) return 0;
1784 if ( IsLSThunklet( thunk
) && thunk
->relay
== relay
1785 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
1786 return (SEGPTR
)thunk
->target
;
1788 return THUNK_AllocSLThunklet( target
, relay
,
1789 ThunkletCallbackGlueSL
, task
);
1792 /***********************************************************************
1793 * AllocLSThunkletCallback (KERNEL.561)
1794 * AllocLSThunkletCallback_dup (KERNEL.606)
1796 FARPROC WINAPI
AllocLSThunkletCallback16( SEGPTR target
, DWORD relay
)
1798 return AllocLSThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
1801 /***********************************************************************
1802 * AllocSLThunkletCallback (KERNEL.562)
1803 * AllocSLThunkletCallback_dup (KERNEL.605)
1805 SEGPTR WINAPI
AllocSLThunkletCallback16( FARPROC target
, DWORD relay
)
1807 return AllocSLThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
1810 /***********************************************************************
1811 * FindLSThunkletCallback (KERNEL.563)
1812 * FindLSThunkletCallback_dup (KERNEL.609)
1814 FARPROC WINAPI
FindLSThunkletCallback( SEGPTR target
, DWORD relay
)
1816 THUNKLET
*thunk
= MapSL( target
);
1817 if ( thunk
&& IsSLThunklet16( thunk
) && thunk
->relay
== relay
1818 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
1819 return (FARPROC
)thunk
->target
;
1821 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
1822 (DWORD
)ThunkletCallbackGlueLS
,
1824 return (FARPROC
)thunk
;
1827 /***********************************************************************
1828 * FindSLThunkletCallback (KERNEL.564)
1829 * FindSLThunkletCallback_dup (KERNEL.610)
1831 SEGPTR WINAPI
FindSLThunkletCallback( FARPROC target
, DWORD relay
)
1833 THUNKLET
*thunk
= (THUNKLET
*)target
;
1834 if ( thunk
&& IsLSThunklet( thunk
) && thunk
->relay
== relay
1835 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
1836 return (SEGPTR
)thunk
->target
;
1838 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
1839 (DWORD
)ThunkletCallbackGlueSL
,
1841 return get_segptr( thunk
);
1845 /***********************************************************************
1846 * FreeThunklet (KERNEL.611)
1848 BOOL16 WINAPI
FreeThunklet16( DWORD unused1
, DWORD unused2
)
1854 /***********************************************************************
1855 * Callback Client API
1858 #define N_CBC_FIXED 20
1859 #define N_CBC_VARIABLE 10
1860 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1862 static SEGPTR CBClientRelay16
[ N_CBC_TOTAL
];
1863 static FARPROC
*CBClientRelay32
[ N_CBC_TOTAL
];
1865 /***********************************************************************
1866 * RegisterCBClient (KERNEL.619)
1868 INT16 WINAPI
RegisterCBClient16( INT16 wCBCId
,
1869 SEGPTR relay16
, FARPROC
*relay32
)
1871 /* Search for free Callback ID */
1873 for ( wCBCId
= N_CBC_FIXED
; wCBCId
< N_CBC_TOTAL
; wCBCId
++ )
1874 if ( !CBClientRelay16
[ wCBCId
] )
1877 /* Register Callback ID */
1878 if ( wCBCId
> 0 && wCBCId
< N_CBC_TOTAL
)
1880 CBClientRelay16
[ wCBCId
] = relay16
;
1881 CBClientRelay32
[ wCBCId
] = relay32
;
1889 /***********************************************************************
1890 * UnRegisterCBClient (KERNEL.622)
1892 INT16 WINAPI
UnRegisterCBClient16( INT16 wCBCId
,
1893 SEGPTR relay16
, FARPROC
*relay32
)
1895 if ( wCBCId
>= N_CBC_FIXED
&& wCBCId
< N_CBC_TOTAL
1896 && CBClientRelay16
[ wCBCId
] == relay16
1897 && CBClientRelay32
[ wCBCId
] == relay32
)
1899 CBClientRelay16
[ wCBCId
] = 0;
1900 CBClientRelay32
[ wCBCId
] = 0;
1909 /***********************************************************************
1910 * InitCBClient (KERNEL.623)
1912 void WINAPI
InitCBClient16( FARPROC glueLS
)
1914 HMODULE16 kernel
= GetModuleHandle16( "KERNEL" );
1915 SEGPTR glueSL
= (SEGPTR
)GetProcAddress16( kernel
, (LPCSTR
)604 );
1917 SetThunkletCallbackGlue16( glueLS
, glueSL
);
1920 /***********************************************************************
1921 * CBClientGlueSL (KERNEL.604)
1923 void WINAPI
CBClientGlueSL( CONTEXT
*context
)
1925 /* Create stack frame */
1926 SEGPTR stackSeg
= stack16_push( 12 );
1927 LPWORD stackLin
= MapSL( stackSeg
);
1928 SEGPTR glue
, *glueTab
;
1930 stackLin
[3] = (WORD
)context
->Ebp
;
1931 stackLin
[2] = (WORD
)context
->Esi
;
1932 stackLin
[1] = (WORD
)context
->Edi
;
1933 stackLin
[0] = (WORD
)context
->SegDs
;
1935 context
->Ebp
= OFFSETOF( stackSeg
) + 6;
1936 context
->Esp
= OFFSETOF( stackSeg
) - 4;
1939 /* Jump to 16-bit relay code */
1940 glueTab
= MapSL( CBClientRelay16
[ stackLin
[5] ] );
1941 glue
= glueTab
[ stackLin
[4] ];
1942 context
->SegCs
= SELECTOROF( glue
);
1943 context
->Eip
= OFFSETOF ( glue
);
1946 /*******************************************************************
1949 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
1951 * Since the relay stub is itself 32-bit, this should not be a problem;
1952 * unfortunately, the relay stubs are expected to switch back to a
1953 * 16-bit stack (and 16-bit code) after completion :-(
1955 * This would conflict with our 16- vs. 32-bit stack handling, so
1956 * we simply switch *back* to our 32-bit stack before returning to
1959 * The CBClient relay stub expects to be called with the following
1960 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
1961 * stack at the designated places:
1964 * (ebp+14) original arguments to the callback routine
1965 * (ebp+10) far return address to original caller
1966 * (ebp+6) Thunklet target address
1967 * (ebp+2) Thunklet relay ID code
1968 * (ebp) BP (saved by CBClientGlueSL)
1969 * (ebp-2) SI (saved by CBClientGlueSL)
1970 * (ebp-4) DI (saved by CBClientGlueSL)
1971 * (ebp-6) DS (saved by CBClientGlueSL)
1973 * ... buffer space used by the 16-bit side glue for temp copies
1975 * (ebx+4) far return address to 16-bit side glue code
1976 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
1978 * The 32-bit side glue code accesses both the original arguments (via ebp)
1979 * and the temporary copies prepared by the 16-bit side glue (via ebx).
1980 * After completion, the stub will load ss:sp from the buffer at ebx
1981 * and perform a far return to 16-bit code.
1983 * To trick the relay stub into returning to us, we replace the 16-bit
1984 * return address to the glue code by a cs:ip pair pointing to our
1985 * return entry point (the original return address is saved first).
1986 * Our return stub thus called will then reload the 32-bit ss:esp and
1987 * return to 32-bit code (by using and ss:esp value that we have also
1988 * pushed onto the 16-bit stack before and a cs:eip values found at
1989 * that position on the 32-bit stack). The ss:esp to be restored is
1990 * found relative to the 16-bit stack pointer at:
1993 * (ebx-8) sp (32-bit stack pointer)
1995 * The second variant of this routine, CALL32_CBClientEx, which is used
1996 * to implement KERNEL.621, has to cope with yet another problem: Here,
1997 * the 32-bit side directly returns to the caller of the CBClient thunklet,
1998 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
1999 * As we have to return to our 32-bit code first, we have to adapt the
2000 * layout of our temporary area so as to include values for the registers
2001 * that are to be restored, and later (in the implementation of KERNEL.621)
2002 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2003 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2004 * and then performs a lret NN, where NN is the number of arguments to be
2005 * removed. Thus, we prepare our temporary area as follows:
2007 * (ebx+22) 16-bit cs (this segment)
2008 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2009 * (ebx+16) 32-bit ss (flat)
2010 * (ebx+12) 32-bit sp (32-bit stack pointer)
2011 * (ebx+10) 16-bit bp (points to ebx+24)
2012 * (ebx+8) 16-bit si (ignored)
2013 * (ebx+6) 16-bit di (ignored)
2014 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2015 * (ebx+2) 16-bit ss (16-bit stack segment)
2016 * (ebx+0) 16-bit sp (points to ebx+4)
2018 * Note that we ensure that DS is not changed and remains the flat segment,
2019 * and the 32-bit stack pointer our own return stub needs fits just
2020 * perfectly into the 8 bytes that are skipped by the Windows stub.
2021 * One problem is that we have to determine the number of removed arguments,
2022 * as these have to be really removed in KERNEL.621. Thus, the BP value
2023 * that we place in the temporary area to be restored, contains the value
2024 * that SP would have if no arguments were removed. By comparing the actual
2025 * value of SP with this value in our return stub we can compute the number
2026 * of removed arguments. This is then returned to KERNEL.621.
2028 * The stack layout of this function:
2029 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2030 * (ebp+16) esi pointer to caller's esi value
2031 * (ebp+12) arg ebp value to be set for relay stub
2032 * (ebp+8) func CBClient relay stub address
2036 extern DWORD
CALL32_CBClient( FARPROC proc
, LPWORD args
, WORD
*stackLin
, DWORD
*esi
);
2037 __ASM_GLOBAL_FUNC( CALL32_CBClient
,
2039 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2040 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2041 "movl %esp,%ebp\n\t"
2042 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2044 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
2046 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2048 __ASM_CFI(".cfi_rel_offset %ebx,-12\n\t")
2049 "movl 16(%ebp),%ebx\n\t"
2050 "leal -8(%esp),%eax\n\t"
2051 "movl %eax,-8(%ebx)\n\t"
2052 "movl 20(%ebp),%esi\n\t"
2053 "movl (%esi),%esi\n\t"
2054 "movl 8(%ebp),%eax\n\t"
2055 "movl 12(%ebp),%ebp\n\t"
2058 "movl 32(%esp),%edi\n\t"
2059 "movl %esi,(%edi)\n\t"
2061 __ASM_CFI(".cfi_same_value %ebx\n\t")
2063 __ASM_CFI(".cfi_same_value %esi\n\t")
2065 __ASM_CFI(".cfi_same_value %edi\n\t")
2067 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2068 __ASM_CFI(".cfi_same_value %ebp\n\t")
2071 /***********************************************************************
2072 * CBClientThunkSL (KERNEL.620)
2074 void WINAPI
CBClientThunkSL( CONTEXT
*context
)
2076 /* Call 32-bit relay code */
2078 LPWORD args
= MapSL( MAKESEGPTR( context
->SegSs
, LOWORD(context
->Ebp
) ) );
2079 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
2081 /* fill temporary area for the asm code (see comments in winebuild) */
2082 SEGPTR stack
= stack16_push( 12 );
2083 LPWORD stackLin
= MapSL(stack
);
2084 /* stackLin[0] and stackLin[1] reserved for the 32-bit stack ptr */
2085 stackLin
[2] = get_ds();
2087 stackLin
[4] = OFFSETOF(stack
) + 12;
2088 stackLin
[5] = SELECTOROF(stack
);
2089 stackLin
[6] = 0; /* overwrite return address */
2090 stackLin
[7] = cbclient_selector
;
2091 context
->Eax
= CALL32_CBClient( proc
, args
, stackLin
+ 4, &context
->Esi
);
2095 extern DWORD
CALL32_CBClientEx( FARPROC proc
, LPWORD args
, WORD
*stackLin
, DWORD
*esi
, INT
*nArgs
);
2096 __ASM_GLOBAL_FUNC( CALL32_CBClientEx
,
2098 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2099 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2100 "movl %esp,%ebp\n\t"
2101 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2103 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
2105 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2107 __ASM_CFI(".cfi_rel_offset %ebx,-12\n\t")
2108 "movl 16(%ebp),%ebx\n\t"
2109 "leal -8(%esp),%eax\n\t"
2110 "movl %eax,12(%ebx)\n\t"
2111 "movl 20(%ebp),%esi\n\t"
2112 "movl (%esi),%esi\n\t"
2113 "movl 8(%ebp),%eax\n\t"
2114 "movl 12(%ebp),%ebp\n\t"
2117 "movl 32(%esp),%edi\n\t"
2118 "movl %esi,(%edi)\n\t"
2119 "movl 36(%esp),%ebx\n\t"
2120 "movl %ebp,(%ebx)\n\t"
2122 __ASM_CFI(".cfi_same_value %ebx\n\t")
2124 __ASM_CFI(".cfi_same_value %esi\n\t")
2126 __ASM_CFI(".cfi_same_value %edi\n\t")
2128 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2129 __ASM_CFI(".cfi_same_value %ebp\n\t")
2132 /***********************************************************************
2133 * CBClientThunkSLEx (KERNEL.621)
2135 void WINAPI
CBClientThunkSLEx( CONTEXT
*context
)
2137 /* Call 32-bit relay code */
2139 LPWORD args
= MapSL( MAKESEGPTR( context
->SegSs
, LOWORD(context
->Ebp
) ) );
2140 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
2144 /* fill temporary area for the asm code (see comments in winebuild) */
2145 SEGPTR stack
= stack16_push( 24 );
2146 stackLin
= MapSL(stack
);
2147 stackLin
[0] = OFFSETOF(stack
) + 4;
2148 stackLin
[1] = SELECTOROF(stack
);
2149 stackLin
[2] = get_ds();
2150 stackLin
[5] = OFFSETOF(stack
) + 24;
2151 /* stackLin[6] and stackLin[7] reserved for the 32-bit stack ptr */
2152 stackLin
[8] = get_ds();
2155 stackLin
[11] = cbclientex_selector
;
2157 context
->Eax
= CALL32_CBClientEx( proc
, args
, stackLin
, &context
->Esi
, &nArgs
);
2160 /* Restore registers saved by CBClientGlueSL */
2161 stackLin
= (LPWORD
)((LPBYTE
)CURRENT_STACK16
+ sizeof(STACK16FRAME
) - 4);
2162 context
->Ebp
= (context
->Ebp
& ~0xffff) | stackLin
[3];
2163 context
->Esi
= (context
->Esi
& ~0xffff) | stackLin
[2];
2164 context
->Edi
= (context
->Edi
& ~0xffff) | stackLin
[1];
2165 context
->SegDs
= stackLin
[0];
2166 context
->Esp
+= 16+nArgs
;
2168 /* Return to caller of CBClient thunklet */
2169 context
->SegCs
= stackLin
[9];
2170 context
->Eip
= stackLin
[8];
2174 /***********************************************************************
2175 * Get16DLLAddress (KERNEL32.@)
2177 * This function is used by a Win32s DLL if it wants to call a Win16 function.
2178 * A 16:16 segmented pointer to the function is returned.
2179 * Written without any docu.
2181 SEGPTR WINAPI
Get16DLLAddress(HMODULE16 handle
, LPSTR func_name
)
2183 static WORD code_sel32
;
2189 if (!ThunkletHeap
) THUNK_Init();
2190 code_sel32
= SELECTOR_AllocBlock( ThunkletHeap
, 0x10000, LDT_FLAGS_CODE
| LDT_FLAGS_32BIT
);
2191 if (!code_sel32
) return 0;
2193 if (!(thunk
= HeapAlloc( ThunkletHeap
, 0, 32 ))) return 0;
2195 if (!handle
) handle
= GetModuleHandle16("WIN32S16");
2196 proc_16
= GetProcAddress16(handle
, func_name
);
2198 /* movl proc_16, $edx */
2200 *(FARPROC16
*)thunk
= proc_16
;
2201 thunk
+= sizeof(FARPROC16
);
2205 *(void **)thunk
= QT_Thunk
;
2206 thunk
+= sizeof(FARPROC16
);
2207 *(WORD
*)thunk
= get_cs();
2209 return MAKESEGPTR( code_sel32
, (char *)thunk
- (char *)ThunkletHeap
);
2213 /***********************************************************************
2214 * GetWin16DOSEnv (KERNEL32.34)
2215 * Returns some internal value.... probably the default environment database?
2217 DWORD WINAPI
GetWin16DOSEnv(void)
2219 FIXME("stub, returning 0\n");
2223 /**********************************************************************
2224 * GetPK16SysVar (KERNEL32.92)
2226 LPVOID WINAPI
GetPK16SysVar(void)
2228 static BYTE PK16SysVar
[128];
2234 /**********************************************************************
2235 * CommonUnimpStub (KERNEL32.17)
2237 int WINAPI
__regs_CommonUnimpStub( const char *name
, int type
)
2239 FIXME("generic stub %s\n", debugstr_a(name
));
2244 case 14: return ERROR_CALL_NOT_IMPLEMENTED
;
2245 case 13: return ERROR_NOT_SUPPORTED
;
2250 __ASM_STDCALL_FUNC( CommonUnimpStub
, 0,
2252 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2254 "andl $0xf,%ecx\n\t"
2256 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2258 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2259 "call " __ASM_STDCALL("__regs_CommonUnimpStub",8) "\n\t"
2260 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
2262 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
2263 "andl $0xf,%ecx\n\t"
2264 "movl (%esp),%edx\n\t"
2265 "leal (%esp,%ecx,4),%esp\n\t"
2266 "movl %edx,(%esp)\n\t"
2269 /**********************************************************************
2270 * HouseCleanLogicallyDeadHandles (KERNEL32.33)
2272 void WINAPI
HouseCleanLogicallyDeadHandles(void)
2274 /* Whatever this is supposed to do, our handles probably
2275 don't need it :-) */
2278 /**********************************************************************
2281 BOOL WINAPI
_KERNEL32_100(HANDLE threadid
,DWORD exitcode
,DWORD x
)
2283 FIXME("(%p,%ld,0x%08lx): stub\n",threadid
,exitcode
,x
);
2287 /**********************************************************************
2290 * Checks whether the clock has to be switched from daylight
2291 * savings time to standard time or vice versa.
2294 DWORD WINAPI
_KERNEL32_99(DWORD x
)
2296 FIXME("(0x%08lx): stub\n",x
);
2301 /***********************************************************************
2302 * Helper for k32 family functions
2304 static void *user32_proc_address(const char *proc_name
)
2306 static HMODULE hUser32
;
2308 if(!hUser32
) hUser32
= LoadLibraryA("user32.dll");
2309 return GetProcAddress(hUser32
, proc_name
);
2312 /***********************************************************************
2313 * k32CharToOemBuffA (KERNEL32.11)
2315 BOOL WINAPI
k32CharToOemBuffA(LPCSTR s
, LPSTR d
, DWORD len
)
2319 if ((bufW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
2321 MultiByteToWideChar( CP_ACP
, 0, s
, len
, bufW
, len
);
2322 WideCharToMultiByte( CP_OEMCP
, 0, bufW
, len
, d
, len
, NULL
, NULL
);
2323 HeapFree( GetProcessHeap(), 0, bufW
);
2328 /***********************************************************************
2329 * k32CharToOemA (KERNEL32.10)
2331 BOOL WINAPI
k32CharToOemA(LPCSTR s
, LPSTR d
)
2333 if (!s
|| !d
) return TRUE
;
2334 return k32CharToOemBuffA( s
, d
, strlen(s
) + 1 );
2337 /***********************************************************************
2338 * k32OemToCharBuffA (KERNEL32.13)
2340 BOOL WINAPI
k32OemToCharBuffA(LPCSTR s
, LPSTR d
, DWORD len
)
2344 if ((bufW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
2346 MultiByteToWideChar( CP_OEMCP
, 0, s
, len
, bufW
, len
);
2347 WideCharToMultiByte( CP_ACP
, 0, bufW
, len
, d
, len
, NULL
, NULL
);
2348 HeapFree( GetProcessHeap(), 0, bufW
);
2353 /***********************************************************************
2354 * k32OemToCharA (KERNEL32.12)
2356 BOOL WINAPI
k32OemToCharA(LPCSTR s
, LPSTR d
)
2358 return k32OemToCharBuffA( s
, d
, strlen(s
) + 1 );
2361 /**********************************************************************
2362 * k32LoadStringA (KERNEL32.14)
2364 INT WINAPI
k32LoadStringA(HINSTANCE instance
, UINT resource_id
,
2365 LPSTR buffer
, INT buflen
)
2367 static INT (WINAPI
*pLoadStringA
)(HINSTANCE
, UINT
, LPSTR
, INT
);
2369 if(!pLoadStringA
) pLoadStringA
= user32_proc_address("LoadStringA");
2370 return pLoadStringA(instance
, resource_id
, buffer
, buflen
);
2373 /***********************************************************************
2374 * k32wvsprintfA (KERNEL32.16)
2376 INT WINAPI
k32wvsprintfA(LPSTR buffer
, LPCSTR spec
, va_list args
)
2378 static INT (WINAPI
*pwvsprintfA
)(LPSTR
, LPCSTR
, va_list);
2380 if(!pwvsprintfA
) pwvsprintfA
= user32_proc_address("wvsprintfA");
2381 return (*pwvsprintfA
)(buffer
, spec
, args
);
2384 /***********************************************************************
2385 * k32wsprintfA (KERNEL32.15)
2387 INT WINAPIV
k32wsprintfA(LPSTR buffer
, LPCSTR spec
, ...)
2392 va_start(args
, spec
);
2393 res
= k32wvsprintfA(buffer
, spec
, args
);
2398 /**********************************************************************
2401 * Real prototype is:
2402 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
2404 void WINAPI
Catch16( LPCATCHBUF lpbuf
, CONTEXT
*context
)
2406 /* Note: we don't save the current ss, as the catch buffer is */
2407 /* only 9 words long. Hopefully no one will have the silly */
2408 /* idea to change the current stack before calling Throw()... */
2422 lpbuf
[0] = LOWORD(context
->Eip
);
2423 lpbuf
[1] = context
->SegCs
;
2424 /* Windows pushes 4 more words before saving sp */
2425 lpbuf
[2] = LOWORD(context
->Esp
) - 4 * sizeof(WORD
);
2426 lpbuf
[3] = LOWORD(context
->Ebp
);
2427 lpbuf
[4] = LOWORD(context
->Esi
);
2428 lpbuf
[5] = LOWORD(context
->Edi
);
2429 lpbuf
[6] = context
->SegDs
;
2431 lpbuf
[8] = context
->SegSs
;
2432 context
->Eax
&= ~0xffff; /* Return 0 */
2436 /**********************************************************************
2439 * Real prototype is:
2440 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
2442 void WINAPI
Throw16( LPCATCHBUF lpbuf
, INT16 retval
, CONTEXT
*context
)
2444 STACK16FRAME
*pFrame
;
2445 STACK32FRAME
*frame32
;
2447 context
->Eax
= (context
->Eax
& ~0xffff) | (WORD
)retval
;
2449 /* Find the frame32 corresponding to the frame16 we are jumping to */
2450 pFrame
= CURRENT_STACK16
;
2451 frame32
= pFrame
->frame32
;
2452 while (frame32
&& frame32
->frame16
)
2454 if (OFFSETOF(frame32
->frame16
) < CURRENT_SP
)
2455 break; /* Something strange is going on */
2456 if (OFFSETOF(frame32
->frame16
) > lpbuf
[2])
2458 /* We found the right frame */
2459 pFrame
->frame32
= frame32
;
2462 frame32
= ((STACK16FRAME
*)MapSL(frame32
->frame16
))->frame32
;
2464 RtlUnwind( &pFrame
->frame32
->frame
, NULL
, NULL
, 0 );
2466 context
->Eip
= lpbuf
[0];
2467 context
->SegCs
= lpbuf
[1];
2468 context
->Esp
= lpbuf
[2] + 4 * sizeof(WORD
) - sizeof(WORD
) /*extra arg*/;
2469 context
->Ebp
= lpbuf
[3];
2470 context
->Esi
= lpbuf
[4];
2471 context
->Edi
= lpbuf
[5];
2472 context
->SegDs
= lpbuf
[6];
2474 if (lpbuf
[8] != context
->SegSs
)
2475 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );
2480 * 16-bit WOW routines (in KERNEL)
2483 /**********************************************************************
2484 * GetVDMPointer32W (KERNEL.516)
2486 DWORD WINAPI
GetVDMPointer32W16( SEGPTR vp
, UINT16 fMode
)
2488 GlobalPageLock16(GlobalHandle16(SELECTOROF(vp
)));
2489 return (DWORD
)K32WOWGetVDMPointer( vp
, 0, (DWORD
)fMode
);
2492 /***********************************************************************
2493 * LoadLibraryEx32W (KERNEL.513)
2495 DWORD WINAPI
LoadLibraryEx32W16( LPCSTR lpszLibFile
, DWORD hFile
, DWORD dwFlags
)
2504 SetLastError(ERROR_INVALID_PARAMETER
);
2508 /* if the file cannot be found, call LoadLibraryExA anyway, since it might be
2509 a builtin module. This case is handled in MODULE_LoadLibraryExA */
2511 if ((p
= strrchr( lpszLibFile
, '.' )) && !strchr( p
, '\\' )) /* got an extension */
2513 if (OpenFile16( lpszLibFile
, &ofs
, OF_EXIST
) != HFILE_ERROR16
)
2514 lpszLibFile
= ofs
.szPathName
;
2518 char buffer
[MAX_PATH
+4];
2519 strcpy( buffer
, lpszLibFile
);
2520 strcat( buffer
, ".dll" );
2521 if (OpenFile16( buffer
, &ofs
, OF_EXIST
) != HFILE_ERROR16
)
2522 lpszLibFile
= ofs
.szPathName
;
2525 ReleaseThunkLock( &mutex_count
);
2526 hModule
= LoadLibraryExA( lpszLibFile
, (HANDLE
)hFile
, dwFlags
);
2527 RestoreThunkLock( mutex_count
);
2529 return (DWORD
)hModule
;
2532 /***********************************************************************
2533 * GetProcAddress32W (KERNEL.515)
2535 DWORD WINAPI
GetProcAddress32W16( DWORD hModule
, LPCSTR lpszProc
)
2537 return (DWORD
)GetProcAddress( (HMODULE
)hModule
, lpszProc
);
2540 /***********************************************************************
2541 * FreeLibrary32W (KERNEL.514)
2543 DWORD WINAPI
FreeLibrary32W16( DWORD hLibModule
)
2548 ReleaseThunkLock( &mutex_count
);
2549 retv
= FreeLibrary( (HMODULE
)hLibModule
);
2550 RestoreThunkLock( mutex_count
);
2554 /**********************************************************************
2557 static DWORD
WOW_CallProc32W16( FARPROC proc32
, DWORD nrofargs
, DWORD
*args
)
2562 ReleaseThunkLock( &mutex_count
);
2563 if (!proc32
) ret
= 0;
2564 else ret
= call_entry_point( proc32
, nrofargs
& ~CPEX_DEST_CDECL
, args
);
2565 RestoreThunkLock( mutex_count
);
2567 TRACE("returns %08lx\n",ret
);
2571 /**********************************************************************
2572 * CallProc32W (KERNEL.517)
2574 DWORD WINAPIV
CallProc32W16( DWORD nrofargs
, DWORD argconvmask
, FARPROC proc32
, VA_LIST16 valist
)
2579 TRACE("(%ld,%ld,%p args[",nrofargs
,argconvmask
,proc32
);
2581 for (i
=0;i
<nrofargs
;i
++)
2583 if (argconvmask
& (1<<i
))
2585 SEGPTR ptr
= VA_ARG16( valist
, SEGPTR
);
2586 /* pascal convention, have to reverse the arguments order */
2587 args
[nrofargs
- i
- 1] = (DWORD
)MapSL(ptr
);
2588 TRACE("%08lx(%p),",ptr
,MapSL(ptr
));
2592 DWORD arg
= VA_ARG16( valist
, DWORD
);
2593 /* pascal convention, have to reverse the arguments order */
2594 args
[nrofargs
- i
- 1] = arg
;
2600 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
2601 stack16_pop( (3 + nrofargs
) * sizeof(DWORD
) );
2603 return WOW_CallProc32W16( proc32
, nrofargs
, args
);
2606 /**********************************************************************
2607 * _CallProcEx32W (KERNEL.518)
2609 DWORD WINAPIV
CallProcEx32W16( DWORD nrofargs
, DWORD argconvmask
, FARPROC proc32
, VA_LIST16 valist
)
2612 unsigned int i
, count
= min( 32, nrofargs
& ~CPEX_DEST_CDECL
);
2614 TRACE("(%s,%ld,%ld,%p args[", nrofargs
& CPEX_DEST_CDECL
? "cdecl": "stdcall",
2615 nrofargs
& ~CPEX_DEST_CDECL
, argconvmask
, proc32
);
2617 for (i
= 0; i
< count
; i
++)
2619 if (argconvmask
& (1<<i
))
2621 SEGPTR ptr
= VA_ARG16( valist
, SEGPTR
);
2622 args
[i
] = (DWORD
)MapSL(ptr
);
2623 TRACE("%08lx(%p),",ptr
,MapSL(ptr
));
2627 DWORD arg
= VA_ARG16( valist
, DWORD
);
2633 return WOW_CallProc32W16( proc32
, nrofargs
, args
);
2637 /**********************************************************************
2638 * WOW16Call (KERNEL.500)
2643 DWORD WINAPIV
WOW16Call(WORD x
, WORD y
, WORD z
, VA_LIST16 args
)
2647 FIXME("(0x%04x,0x%04x,%d),calling (",x
,y
,z
);
2649 for (i
=0;i
<x
/2;i
++) {
2650 WORD a
= VA_ARG16(args
,WORD
);
2653 calladdr
= VA_ARG16(args
,DWORD
);
2654 stack16_pop( 3*sizeof(WORD
) + x
+ sizeof(DWORD
) );
2655 FIXME(") calling address was 0x%08lx\n",calladdr
);