msvcrt: Use the public standard type definitions where possible in msvcrt.h.
[wine.git] / dlls / krnl386.exe16 / thunk.c
blobe913a08f88cca0cd84ac39fd6abab016a8f613a0
1 /*
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
23 #include <string.h>
24 #include <sys/types.h>
25 #include <stdarg.h>
26 #include <stdio.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "winternl.h"
32 #include "wownt16.h"
33 #include "wownt32.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 */
47 struct ThunkDataLS16
49 struct ThunkDataCommon common; /* 00 */
50 SEGPTR targetTable; /* 08 */
51 DWORD firstTime; /* 0C */
54 struct ThunkDataLS32
56 struct ThunkDataCommon common; /* 00 */
57 DWORD * targetTable; /* 08 */
58 char lateBinding[4]; /* 0C */
59 DWORD flags; /* 10 */
60 DWORD reserved1; /* 14 */
61 DWORD reserved2; /* 18 */
62 DWORD offsetQTThunk; /* 1C */
63 DWORD offsetFTProlog; /* 20 */
66 struct ThunkDataSL16
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 */
80 struct ThunkDataSL32
82 struct ThunkDataCommon common; /* 00 */
83 DWORD reserved1; /* 08 */
84 struct ThunkDataSL * data; /* 0C */
85 char lateBinding[4]; /* 10 */
86 DWORD flags; /* 14 */
87 DWORD reserved2; /* 18 */
88 DWORD reserved3; /* 1C */
89 DWORD offsetTargetTable; /* 20 */
92 struct ThunkDataSL
94 #if 0
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.
116 #endif
118 struct ThunkDataCommon common;
119 DWORD flags1;
120 struct SLApiDB * apiDB;
121 struct SLTargetDB * targetDB;
122 DWORD flags2;
123 char pszDll16[256];
124 char pszDll32[256];
127 struct SLTargetDB
129 struct SLTargetDB * next;
130 DWORD process;
131 DWORD * targetTable;
134 struct SLApiDB
136 DWORD nrArgBytes;
137 DWORD errorReturnValue;
140 SEGPTR CALL32_CBClient_RetAddr = 0;
141 SEGPTR CALL32_CBClientEx_RetAddr = 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
191 * C3 lret
193 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
194 LPBYTE x;
196 x = relayCode;
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.
210 * 33C9 xor ecx, ecx
211 * 8A4DFC mov cl , [ebp-04]
212 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
213 * B8yyyyyyyy mov eax, QT_Thunk
214 * FFE0 jmp eax
216 static void _write_qtthunk(
217 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
218 DWORD *targetTable /* [in] start of thunk (for index lookup) */
220 LPBYTE x;
222 x = relayCode;
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 /***********************************************************************
234 * _loadthunk
236 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
237 struct ThunkDataCommon *TD32, DWORD checksum)
239 struct ThunkDataCommon *TD16;
240 HMODULE16 hmod;
241 int ordinal;
242 static BOOL done;
244 if (!done)
246 LoadLibrary16( "gdi.exe" );
247 LoadLibrary16( "user.exe" );
248 done = TRUE;
251 if ((hmod = LoadLibrary16(module)) <= 32)
253 ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
254 module, func, module32, module, hmod);
255 return 0;
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);
263 return 0;
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]);
272 return 0;
275 if (TD32 && TD16->checksum != TD32->checksum)
277 ERR("(%s, %s, %s): Wrong checksum %08x (should be %08x)\n",
278 module, func, module32, TD16->checksum, TD32->checksum);
279 return 0;
282 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
284 ERR("(%s, %s, %s): Wrong checksum %08x (should be %08x)\n",
285 module, func, module32, *(LPDWORD)TD16, checksum);
286 return 0;
289 return TD16;
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 */
321 BOOL directionSL;
323 if (!strncmp(TD->magic, "SL01", 4))
325 directionSL = TRUE;
327 TRACE("SL01 thunk %s (%p) <- %s (%s), Reason: %d\n",
328 module32, TD, module16, thunkfun16, dwReason);
330 else if (!strncmp(TD->magic, "LS01", 4))
332 directionSL = FALSE;
334 TRACE("LS01 thunk %s (%p) -> %s (%s), Reason: %d\n",
335 module32, TD, module16, thunkfun16, dwReason);
337 else
339 ERR("Invalid magic %c%c%c%c\n",
340 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
341 return 0;
344 switch (dwReason)
346 case DLL_PROCESS_ATTACH:
348 struct ThunkDataCommon *TD16;
349 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
350 return 0;
352 if (directionSL)
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");
361 return 0;
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 %08x allocated TargetDB entry for ThunkDataSL %p\n",
374 GetCurrentProcessId(), SL32->data);
376 else
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);
387 break;
390 case DLL_PROCESS_DETACH:
391 /* FIXME: cleanup */
392 break;
395 return 1;
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
405 * scratch buffer.
406 * The scratch buffer is used as work space by Windows' QT_Thunk
407 * function.
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).
413 * [ok]
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 )
423 CONTEXT context16;
424 DWORD argsize;
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 = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + 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 */
442 if (argsize > 64)
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) -
454 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
456 DEFINE_REGS_ENTRYPOINT( QT_Thunk )
459 /**********************************************************************
460 * FT_Prolog (KERNEL32.@)
462 * The set of FT_... thunk routines is used instead of QT_Thunk,
463 * if structures have to be converted from 32-bit to 16-bit
464 * (change of member alignment, conversion of members).
466 * The thunk function (as created by the thunk compiler) calls
467 * FT_Prolog at the beginning, to set up a stack frame and
468 * allocate a 64 byte buffer on the stack.
469 * The input parameters (target address and some flags) are
470 * saved for later use by FT_Thunk.
472 * Input: EDX 16-bit target address (SEGPTR)
473 * CX bits 0..7 target number (in target table)
474 * bits 8..9 some flags (unclear???)
475 * bits 10..15 number of DWORD arguments
477 * Output: A new stackframe is created, and a 64 byte buffer
478 * allocated on the stack. The layout of the stack
479 * on return is as follows:
481 * (ebp+4) return address to caller of thunk function
482 * (ebp) old EBP
483 * (ebp-4) saved EBX register of caller
484 * (ebp-8) saved ESI register of caller
485 * (ebp-12) saved EDI register of caller
486 * (ebp-16) saved ECX register, containing flags
487 * (ebp-20) bitmap containing parameters that are to be converted
488 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
489 * filled in by the thunk code before calling FT_Thunk
490 * (ebp-24)
491 * ... (unclear)
492 * (ebp-44)
493 * (ebp-48) saved EAX register of caller (unclear, never restored???)
494 * (ebp-52) saved EDX register, containing 16-bit thunk target
495 * (ebp-56)
496 * ... (unclear)
497 * (ebp-64)
499 * ESP is EBP-64 after return.
502 void WINAPI __regs_FT_Prolog( CONTEXT *context )
504 /* Build stack frame */
505 stack32_push(context, context->Ebp);
506 context->Ebp = context->Esp;
508 /* Allocate 64-byte Thunk Buffer */
509 context->Esp -= 64;
510 memset((char *)context->Esp, '\0', 64);
512 /* Store Flags (ECX) and Target Address (EDX) */
513 /* Save other registers to be restored later */
514 *(DWORD *)(context->Ebp - 4) = context->Ebx;
515 *(DWORD *)(context->Ebp - 8) = context->Esi;
516 *(DWORD *)(context->Ebp - 12) = context->Edi;
517 *(DWORD *)(context->Ebp - 16) = context->Ecx;
519 *(DWORD *)(context->Ebp - 48) = context->Eax;
520 *(DWORD *)(context->Ebp - 52) = context->Edx;
522 DEFINE_REGS_ENTRYPOINT( FT_Prolog )
524 /**********************************************************************
525 * FT_Thunk (KERNEL32.@)
527 * This routine performs the actual call to 16-bit code,
528 * similar to QT_Thunk. The differences are:
529 * - The call target is taken from the buffer created by FT_Prolog
530 * - Those arguments requested by the thunk code (by setting the
531 * corresponding bit in the bitmap at EBP-20) are converted
532 * from 32-bit pointers to segmented pointers (those pointers
533 * are guaranteed to point to structures copied to the stack
534 * by the thunk code, so we always use the 16-bit stack selector
535 * for those addresses).
537 * The bit #i of EBP-20 corresponds here to the DWORD starting at
538 * ESP+4 + 2*i.
540 * FIXME: It is unclear what happens if there are more than 32 WORDs
541 * of arguments, so that the single DWORD bitmap is no longer
542 * sufficient ...
544 void WINAPI __regs_FT_Thunk( CONTEXT *context )
546 DWORD mapESPrelative = *(DWORD *)(context->Ebp - 20);
547 DWORD callTarget = *(DWORD *)(context->Ebp - 52);
549 CONTEXT context16;
550 DWORD i, argsize;
551 DWORD newstack[32];
552 LPBYTE oldstack;
554 context16 = *context;
556 context16.SegCs = HIWORD(callTarget);
557 context16.Eip = LOWORD(callTarget);
558 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
560 argsize = context->Ebp-context->Esp-0x40;
561 if (argsize > sizeof(newstack)) argsize = sizeof(newstack);
562 oldstack = (LPBYTE)context->Esp;
564 memcpy( newstack, oldstack, argsize );
566 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
567 if (mapESPrelative & (1 << i))
569 SEGPTR *arg = (SEGPTR *)newstack[i];
570 *arg = MAKESEGPTR(SELECTOROF(NtCurrentTeb()->WOW32Reserved),
571 OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize
572 + (*(LPBYTE *)arg - oldstack));
575 WOWCallback16Ex( 0, WCB16_REGS, argsize, newstack, (DWORD *)&context16 );
576 context->Eax = context16.Eax;
577 context->Edx = context16.Edx;
578 context->Ecx = context16.Ecx;
580 context->Esp += LOWORD(context16.Esp) -
581 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
583 /* Copy modified buffers back to 32-bit stack */
584 memcpy( oldstack, newstack, argsize );
586 DEFINE_REGS_ENTRYPOINT( FT_Thunk )
588 /***********************************************************************
589 * FT_Exit0 (KERNEL32.@)
590 * FT_Exit4 (KERNEL32.@)
591 * FT_Exit8 (KERNEL32.@)
592 * FT_Exit12 (KERNEL32.@)
593 * FT_Exit16 (KERNEL32.@)
594 * FT_Exit20 (KERNEL32.@)
595 * FT_Exit24 (KERNEL32.@)
596 * FT_Exit28 (KERNEL32.@)
597 * FT_Exit32 (KERNEL32.@)
598 * FT_Exit36 (KERNEL32.@)
599 * FT_Exit40 (KERNEL32.@)
600 * FT_Exit44 (KERNEL32.@)
601 * FT_Exit48 (KERNEL32.@)
602 * FT_Exit52 (KERNEL32.@)
603 * FT_Exit56 (KERNEL32.@)
605 * One of the FT_ExitNN functions is called at the end of the thunk code.
606 * It removes the stack frame created by FT_Prolog, moves the function
607 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
608 * value, but the thunk code has moved it from EAX to EBX in the
609 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
610 * and perform a return to the CALLER of the thunk code (while removing
611 * the given number of arguments from the caller's stack).
613 #define FT_EXIT_RESTORE_REGS \
614 "movl %ebx,%eax\n\t" \
615 "movl -4(%ebp),%ebx\n\t" \
616 "movl -8(%ebp),%esi\n\t" \
617 "movl -12(%ebp),%edi\n\t" \
618 "leave\n\t"
620 #define DEFINE_FT_Exit(n) \
621 __ASM_STDCALL_FUNC( FT_Exit ## n, 0, FT_EXIT_RESTORE_REGS "ret $" #n )
623 DEFINE_FT_Exit(0)
624 DEFINE_FT_Exit(4)
625 DEFINE_FT_Exit(8)
626 DEFINE_FT_Exit(12)
627 DEFINE_FT_Exit(16)
628 DEFINE_FT_Exit(20)
629 DEFINE_FT_Exit(24)
630 DEFINE_FT_Exit(28)
631 DEFINE_FT_Exit(32)
632 DEFINE_FT_Exit(36)
633 DEFINE_FT_Exit(40)
634 DEFINE_FT_Exit(44)
635 DEFINE_FT_Exit(48)
636 DEFINE_FT_Exit(52)
637 DEFINE_FT_Exit(56)
640 /***********************************************************************
641 * ThunkInitLS (KERNEL32.43)
642 * A thunkbuffer link routine
643 * The thunkbuf looks like:
645 * 00: DWORD length ? don't know exactly
646 * 04: SEGPTR ptr ? where does it point to?
647 * The pointer ptr is written into the first DWORD of 'thunk'.
648 * (probably correctly implemented)
649 * [ok probably]
650 * RETURNS
651 * segmented pointer to thunk?
653 DWORD WINAPI ThunkInitLS(
654 LPDWORD thunk, /* [in] win32 thunk */
655 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
656 DWORD len, /* [in] thkbuffer length */
657 LPCSTR dll16, /* [in] name of win16 dll */
658 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
660 LPDWORD addr;
662 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
663 return 0;
665 if (!addr[1])
666 return 0;
667 *thunk = addr[1];
669 return addr[1];
672 /***********************************************************************
673 * Common32ThkLS (KERNEL32.45)
675 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
676 * style thunks. The basic difference is that the parameter conversion
677 * is done completely on the *16-bit* side here. Thus we do not call
678 * the 16-bit target directly, but call a common entry point instead.
679 * This entry function then calls the target according to the target
680 * number passed in the DI register.
682 * Input: EAX SEGPTR to the common 16-bit entry point
683 * CX offset in thunk table (target number * 4)
684 * DX error return value if execution fails (unclear???)
685 * EDX.HI number of DWORD parameters
687 * (Note that we need to move the thunk table offset from CX to DI !)
689 * The called 16-bit stub expects its stack to look like this:
690 * ...
691 * (esp+40) 32-bit arguments
692 * ...
693 * (esp+8) 32 byte of stack space available as buffer
694 * (esp) 8 byte return address for use with 0x66 lret
696 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
697 * and uses the EAX register to return a DWORD return value.
698 * Thus we need to use a special assembly glue routine
699 * (CallRegisterLongProc instead of CallRegisterShortProc).
701 * Finally, we return to the caller, popping the arguments off
702 * the stack. The number of arguments to be popped is returned
703 * in the BL register by the called 16-bit routine.
706 void WINAPI __regs_Common32ThkLS( CONTEXT *context )
708 CONTEXT context16;
709 DWORD argsize;
711 context16 = *context;
713 context16.Edi = LOWORD(context->Ecx);
714 context16.SegCs = HIWORD(context->Eax);
715 context16.Eip = LOWORD(context->Eax);
716 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
718 argsize = HIWORD(context->Edx) * 4;
720 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
721 if (context->Edx == context->Eip)
722 argsize = 6 * 4;
724 /* Note: the first 32 bytes we copy are just garbage from the 32-bit stack, in order to reserve
725 * the space. It is safe to do that since the register function prefix has reserved
726 * a lot more space than that below context->Esp.
728 WOWCallback16Ex( 0, WCB16_REGS, argsize + 32, (LPBYTE)context->Esp - 32, (DWORD *)&context16 );
729 context->Eax = context16.Eax;
731 /* Clean up caller's stack frame */
732 context->Esp += LOBYTE(context16.Ebx);
734 DEFINE_REGS_ENTRYPOINT( Common32ThkLS )
736 /***********************************************************************
737 * OT_32ThkLSF (KERNEL32.40)
739 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
740 * argument processing is done on both the 32-bit and the 16-bit side:
741 * The 32-bit side prepares arguments, copying them onto the stack.
743 * When this routine is called, the first word on the stack is the
744 * number of argument bytes prepared by the 32-bit code, and EDX
745 * contains the 16-bit target address.
747 * The called 16-bit routine is another relaycode, doing further
748 * argument processing and then calling the real 16-bit target
749 * whose address is stored at [bp-04].
751 * The call proceeds using a normal CallRegisterShortProc.
752 * After return from the 16-bit relaycode, the arguments need
753 * to be copied *back* to the 32-bit stack, since the 32-bit
754 * relaycode processes output parameters.
756 * Note that we copy twice the number of arguments, since some of the
757 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
758 * arguments of the caller!
760 * (Note that this function seems only to be used for
761 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
763 void WINAPI __regs_OT_32ThkLSF( CONTEXT *context )
765 CONTEXT context16;
766 DWORD argsize;
768 context16 = *context;
770 context16.SegCs = HIWORD(context->Edx);
771 context16.Eip = LOWORD(context->Edx);
772 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
774 argsize = 2 * *(WORD *)context->Esp + 2;
776 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
777 context->Eax = context16.Eax;
778 context->Edx = context16.Edx;
780 /* Copy modified buffers back to 32-bit stack */
781 memcpy( (LPBYTE)context->Esp,
782 (LPBYTE)CURRENT_STACK16 - argsize, argsize );
784 context->Esp += LOWORD(context16.Esp) -
785 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
787 DEFINE_REGS_ENTRYPOINT( OT_32ThkLSF )
789 /***********************************************************************
790 * ThunkInitLSF (KERNEL32.41)
791 * A thunk setup routine.
792 * Expects a pointer to a preinitialized thunkbuffer in the first argument
793 * looking like:
794 *| 00..03: unknown (pointer, check _41, _43, _46)
795 *| 04: EB1E jmp +0x20
797 *| 06..23: unknown (space for replacement code, check .90)
799 *| 24:>E800000000 call offset 29
800 *| 29:>58 pop eax ( target of call )
801 *| 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
802 *| 2F: BAxxxxxxxx mov edx,xxxxxxxx
803 *| 34: 68yyyyyyyy push KERNEL32.90
804 *| 39: C3 ret
806 *| 3A: EB1E jmp +0x20
807 *| 3E ... 59: unknown (space for replacement code?)
808 *| 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
809 *| 5F: 5A pop edx
810 *| 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
811 *| 66: 52 push edx
812 *| 67: 68xxxxxxxx push xxxxxxxx
813 *| 6C: 68yyyyyyyy push KERNEL32.89
814 *| 71: C3 ret
815 *| 72: end?
816 * This function checks if the code is there, and replaces the yyyyyyyy entries
817 * by the functionpointers.
818 * The thunkbuf looks like:
820 *| 00: DWORD length ? don't know exactly
821 *| 04: SEGPTR ptr ? where does it point to?
822 * The segpointer ptr is written into the first DWORD of 'thunk'.
823 * [ok probably]
824 * RETURNS
825 * unclear, pointer to win16 thkbuffer?
827 LPVOID WINAPI ThunkInitLSF(
828 LPBYTE thunk, /* [in] win32 thunk */
829 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
830 DWORD len, /* [in] length of thkbuffer */
831 LPCSTR dll16, /* [in] name of win16 dll */
832 LPCSTR dll32 /* [in] name of win32 dll */
834 LPDWORD addr,addr2;
836 /* FIXME: add checks for valid code ... */
837 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
838 *(void **)(thunk+0x35) = QT_ThunkPrime;
839 *(void **)(thunk+0x6D) = FT_PrologPrime;
841 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
842 return 0;
844 addr2 = MapSL(addr[1]);
845 if (HIWORD(addr2))
846 *(DWORD*)thunk = (DWORD)addr2;
848 return addr2;
851 /***********************************************************************
852 * FT_PrologPrime (KERNEL32.89)
854 * This function is called from the relay code installed by
855 * ThunkInitLSF. It replaces the location from where it was
856 * called by a standard FT_Prolog call stub (which is 'primed'
857 * by inserting the correct target table pointer).
858 * Finally, it calls that stub.
860 * Input: ECX target number + flags (passed through to FT_Prolog)
861 * (ESP) offset of location where target table pointer
862 * is stored, relative to the start of the relay code
863 * (ESP+4) pointer to start of relay code
864 * (this is where the FT_Prolog call stub gets written to)
866 * Note: The two DWORD arguments get popped off the stack.
869 void WINAPI __regs_FT_PrologPrime( CONTEXT *context )
871 DWORD targetTableOffset;
872 LPBYTE relayCode;
874 /* Compensate for the fact that the Wine register relay code thought
875 we were being called, although we were in fact jumped to */
876 context->Esp -= 4;
878 /* Write FT_Prolog call stub */
879 targetTableOffset = stack32_pop(context);
880 relayCode = (LPBYTE)stack32_pop(context);
881 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
883 /* Jump to the call stub just created */
884 context->Eip = (DWORD)relayCode;
886 DEFINE_REGS_ENTRYPOINT( FT_PrologPrime )
888 /***********************************************************************
889 * QT_ThunkPrime (KERNEL32.90)
891 * This function corresponds to FT_PrologPrime, but installs a
892 * call stub for QT_Thunk instead.
894 * Input: (EBP-4) target number (passed through to QT_Thunk)
895 * EDX target table pointer location offset
896 * EAX start of relay code
899 void WINAPI __regs_QT_ThunkPrime( CONTEXT *context )
901 DWORD targetTableOffset;
902 LPBYTE relayCode;
904 /* Compensate for the fact that the Wine register relay code thought
905 we were being called, although we were in fact jumped to */
906 context->Esp -= 4;
908 /* Write QT_Thunk call stub */
909 targetTableOffset = context->Edx;
910 relayCode = (LPBYTE)context->Eax;
911 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
913 /* Jump to the call stub just created */
914 context->Eip = (DWORD)relayCode;
916 DEFINE_REGS_ENTRYPOINT( QT_ThunkPrime )
918 /***********************************************************************
919 * ThunkInitSL (KERNEL32.46)
920 * Another thunkbuf link routine.
921 * The start of the thunkbuf looks like this:
922 * 00: DWORD length
923 * 04: SEGPTR address for thunkbuffer pointer
924 * [ok probably]
926 * RETURNS
927 * Nothing.
929 VOID WINAPI ThunkInitSL(
930 LPBYTE thunk, /* [in] start of thunkbuffer */
931 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
932 DWORD len, /* [in] length of thunkbuffer */
933 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
934 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
936 LPDWORD addr;
938 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
939 return;
941 *(DWORD*)MapSL(addr[1]) = (DWORD)thunk;
944 /**********************************************************************
945 * SSInit (KERNEL.700)
946 * RETURNS
947 * TRUE for success.
949 BOOL WINAPI SSInit16(void)
951 return TRUE;
954 /**********************************************************************
955 * SSOnBigStack (KERNEL32.87)
956 * Check if thunking is initialized (ss selector set up etc.)
957 * We do that differently, so just return TRUE.
958 * [ok]
959 * RETURNS
960 * TRUE for success.
962 BOOL WINAPI SSOnBigStack(void)
964 TRACE("Yes, thunking is initialized\n");
965 return TRUE;
968 /**********************************************************************
969 * SSConfirmSmallStack (KERNEL.704)
971 * Abort if not on small stack.
973 * This must be a register routine as it has to preserve *all* registers.
975 void WINAPI SSConfirmSmallStack( CONTEXT *context )
977 /* We are always on the small stack while in 16-bit code ... */
980 /**********************************************************************
981 * SSCall (KERNEL32.88)
982 * One of the real thunking functions. This one seems to be for 32<->32
983 * thunks. It should probably be capable of crossing processboundaries.
985 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
986 * [ok]
988 * RETURNS
989 * Thunked function result.
991 DWORD WINAPIV SSCall(
992 DWORD nr, /* [in] number of argument bytes */
993 DWORD flags, /* [in] FIXME: flags ? */
994 FARPROC fun, /* [in] function to call */
995 ... /* [in/out] arguments */
997 DWORD i,ret;
998 DWORD *args = ((DWORD *)&fun) + 1;
1000 TRACE("(%d,0x%08x,%p,[",nr,flags,fun);
1001 for (i = 0; i < nr/4; i++) TRACE("0x%08x,",args[i]);
1002 TRACE("])\n");
1003 ret = call_entry_point( fun, nr / sizeof(DWORD), args );
1004 TRACE(" returning %d ...\n",ret);
1005 return ret;
1008 /**********************************************************************
1009 * W32S_BackTo32 (KERNEL32.51)
1011 void WINAPI __regs_W32S_BackTo32( CONTEXT *context )
1013 LPDWORD stack = (LPDWORD)context->Esp;
1014 FARPROC proc = (FARPROC)context->Eip;
1016 context->Eax = call_entry_point( proc, 10, stack + 1 );
1017 context->Eip = stack32_pop(context);
1019 DEFINE_REGS_ENTRYPOINT( W32S_BackTo32 )
1021 /**********************************************************************
1022 * AllocSLCallback (KERNEL32.@)
1024 * Allocate a 16->32 callback.
1026 * NOTES
1027 * Win95 uses some structchains for callbacks. It allocates them
1028 * in blocks of 100 entries, size 32 bytes each, layout:
1029 * blockstart:
1030 *| 0: PTR nextblockstart
1031 *| 4: entry *first;
1032 *| 8: WORD sel ( start points to blockstart)
1033 *| A: WORD unknown
1034 * 100xentry:
1035 *| 00..17: Code
1036 *| 18: PDB *owning_process;
1037 *| 1C: PTR blockstart
1039 * We ignore this for now. (Just a note for further developers)
1040 * FIXME: use this method, so we don't waste selectors...
1042 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
1043 * the 0x66 prefix switches from word->long registers.
1045 *| 665A pop edx
1046 *| 6668x arg2 x pushl <arg2>
1047 *| 6652 push edx
1048 *| EAx arg1 x jmpf <arg1>
1050 * returns the startaddress of this thunk.
1052 * Note, that they look very similar to the ones allocates by THUNK_Alloc.
1053 * RETURNS
1054 * A segmented pointer to the start of the thunk
1056 DWORD WINAPI
1057 AllocSLCallback(
1058 DWORD finalizer, /* [in] Finalizer function */
1059 DWORD callback /* [in] Callback function */
1061 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
1062 WORD sel;
1064 x=thunk;
1065 *x++=0x66;*x++=0x5a; /* popl edx */
1066 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
1067 *x++=0x66;*x++=0x52; /* pushl edx */
1068 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
1070 *(DWORD*)(thunk+18) = GetCurrentProcessId();
1072 sel = SELECTOR_AllocBlock( thunk, 32, LDT_FLAGS_CODE );
1073 return (sel<<16)|0;
1076 /**********************************************************************
1077 * FreeSLCallback (KERNEL32.@)
1078 * Frees the specified 16->32 callback
1080 * RETURNS
1081 * Nothing.
1083 void WINAPI
1084 FreeSLCallback(
1085 DWORD x /* [in] 16 bit callback (segmented pointer?) */
1087 FIXME("(0x%08x): stub\n",x);
1090 /**********************************************************************
1091 * AllocMappedBuffer (KERNEL32.38)
1093 * This is an undocumented KERNEL32 function that
1094 * SMapLS's a GlobalAlloc'ed buffer.
1096 * RETURNS
1097 * EDI register: pointer to buffer
1099 * NOTES
1100 * The buffer is preceded by 8 bytes:
1101 * ...
1102 * edi+0 buffer
1103 * edi-4 SEGPTR to buffer
1104 * edi-8 some magic Win95 needs for SUnMapLS
1105 * (we use it for the memory handle)
1107 * The SEGPTR is used by the caller!
1109 void WINAPI __regs_AllocMappedBuffer(
1110 CONTEXT *context /* [in] EDI register: size of buffer to allocate */
1112 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
1113 DWORD *buffer = GlobalLock(handle);
1114 DWORD ptr = 0;
1116 if (buffer)
1117 if (!(ptr = MapLS(buffer + 2)))
1119 GlobalUnlock(handle);
1120 GlobalFree(handle);
1123 if (!ptr)
1124 context->Eax = context->Edi = 0;
1125 else
1127 buffer[0] = (DWORD)handle;
1128 buffer[1] = ptr;
1130 context->Eax = ptr;
1131 context->Edi = (DWORD)(buffer + 2);
1134 DEFINE_REGS_ENTRYPOINT( AllocMappedBuffer )
1136 /**********************************************************************
1137 * FreeMappedBuffer (KERNEL32.39)
1139 * Free a buffer allocated by AllocMappedBuffer
1141 * RETURNS
1142 * Nothing.
1144 void WINAPI __regs_FreeMappedBuffer(
1145 CONTEXT *context /* [in] EDI register: pointer to buffer */
1147 if (context->Edi)
1149 DWORD *buffer = (DWORD *)context->Edi - 2;
1151 UnMapLS(buffer[1]);
1153 GlobalUnlock((HGLOBAL)buffer[0]);
1154 GlobalFree((HGLOBAL)buffer[0]);
1157 DEFINE_REGS_ENTRYPOINT( FreeMappedBuffer )
1159 /**********************************************************************
1160 * GetTEBSelectorFS (KERNEL.475)
1161 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1163 void WINAPI GetTEBSelectorFS16(void)
1165 CURRENT_STACK16->fs = get_fs();
1168 /**********************************************************************
1169 * IsPeFormat (KERNEL.431)
1171 * Determine if a file is a PE format executable.
1173 * RETURNS
1174 * TRUE, if it is.
1175 * FALSE if the file could not be opened or is not a PE file.
1177 * NOTES
1178 * If fn is given as NULL then the function expects hf16 to be valid.
1180 BOOL16 WINAPI IsPeFormat16(
1181 LPSTR fn, /* [in] Filename to the executable */
1182 HFILE16 hf16) /* [in] An open file handle */
1184 BOOL ret = FALSE;
1185 IMAGE_DOS_HEADER mzh;
1186 OFSTRUCT ofs;
1187 DWORD xmagic;
1189 if (fn) hf16 = OpenFile16(fn,&ofs,OF_READ);
1190 if (hf16 == HFILE_ERROR16) return FALSE;
1191 _llseek16(hf16,0,SEEK_SET);
1192 if (sizeof(mzh)!=_lread16(hf16,&mzh,sizeof(mzh))) goto done;
1193 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) goto done;
1194 _llseek16(hf16,mzh.e_lfanew,SEEK_SET);
1195 if (sizeof(DWORD)!=_lread16(hf16,&xmagic,sizeof(DWORD))) goto done;
1196 ret = (xmagic == IMAGE_NT_SIGNATURE);
1197 done:
1198 _lclose16(hf16);
1199 return ret;
1203 /***********************************************************************
1204 * K32Thk1632Prolog (KERNEL32.@)
1206 void WINAPI __regs_K32Thk1632Prolog( CONTEXT *context )
1208 LPBYTE code = (LPBYTE)context->Eip - 5;
1210 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1211 of 16->32 thunks instead of using one of the standard methods!
1212 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1213 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1214 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1215 bypassed, which means it will crash the next time the 32-bit OLE
1216 code thunks down again to 16-bit (this *will* happen!).
1218 The following hack tries to recognize this situation.
1219 This is possible since the called stubs in OLECLI32/OLESVR32 all
1220 look exactly the same:
1221 00 E8xxxxxxxx call K32Thk1632Prolog
1222 05 FF55FC call [ebp-04]
1223 08 E8xxxxxxxx call K32Thk1632Epilog
1224 0D 66CB retf
1226 If we recognize this situation, we try to simulate the actions
1227 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1228 to our 32-bit stack, creating a proper STACK16FRAME and
1229 updating cur_stack. */
1231 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1232 && code[13] == 0x66 && code[14] == 0xCB)
1234 DWORD argSize = context->Ebp - context->Esp;
1235 char *stack16 = (char *)context->Esp - 4;
1236 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1237 STACK32FRAME *frame32 = NtCurrentTeb()->WOW32Reserved;
1238 char *stack32 = (char *)frame32 - argSize;
1239 WORD stackSel = SELECTOROF(frame32->frame16);
1240 DWORD stackBase = GetSelectorBase(stackSel);
1242 TRACE("before SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1243 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1245 memset(frame16, '\0', sizeof(STACK16FRAME));
1246 frame16->frame32 = frame32;
1247 frame16->ebp = context->Ebp;
1249 memcpy(stack32, stack16, argSize);
1250 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR(stackSel, (DWORD)frame16 - stackBase);
1252 context->Esp = (DWORD)stack32 + 4;
1253 context->Ebp = context->Esp + argSize;
1255 TRACE("after SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1256 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1259 /* entry_point is never used again once the entry point has
1260 been called. Thus we re-use it to hold the Win16Lock count */
1261 ReleaseThunkLock(&CURRENT_STACK16->entry_point);
1263 DEFINE_REGS_ENTRYPOINT( K32Thk1632Prolog )
1265 /***********************************************************************
1266 * K32Thk1632Epilog (KERNEL32.@)
1268 void WINAPI __regs_K32Thk1632Epilog( CONTEXT *context )
1270 LPBYTE code = (LPBYTE)context->Eip - 13;
1272 RestoreThunkLock(CURRENT_STACK16->entry_point);
1274 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1276 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1277 && code[13] == 0x66 && code[14] == 0xCB)
1279 STACK16FRAME *frame16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1280 char *stack16 = (char *)(frame16 + 1);
1281 DWORD argSize = frame16->ebp - (DWORD)stack16;
1282 char *stack32 = (char *)frame16->frame32 - argSize;
1284 DWORD nArgsPopped = context->Esp - (DWORD)stack32;
1286 TRACE("before SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1287 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1289 NtCurrentTeb()->WOW32Reserved = frame16->frame32;
1291 context->Esp = (DWORD)stack16 + nArgsPopped;
1292 context->Ebp = frame16->ebp;
1294 TRACE("after SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1295 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1298 DEFINE_REGS_ENTRYPOINT( K32Thk1632Epilog )
1300 /*********************************************************************
1301 * PK16FNF [KERNEL32.91]
1303 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1304 * string buffer with the 8.3 filename of a recently loaded 16-bit
1305 * module. It is unknown exactly what modules trigger this
1306 * mechanism or what purpose this serves. Win98 Explorer (and
1307 * probably also Win95 with IE 4 shell integration) calls this
1308 * several times during initialization.
1310 * FIXME: find out what this really does and make it work.
1312 void WINAPI PK16FNF(LPSTR strPtr)
1314 FIXME("(%p): stub\n", strPtr);
1316 /* fill in a fake filename that'll be easy to recognize */
1317 strcpy(strPtr, "WINESTUB.FIX");
1320 /***********************************************************************
1321 * 16->32 Flat Thunk routines:
1324 /***********************************************************************
1325 * ThunkConnect16 (KERNEL.651)
1326 * Connects a 32bit and a 16bit thunkbuffer.
1328 UINT WINAPI ThunkConnect16(
1329 LPSTR module16, /* [in] name of win16 dll */
1330 LPSTR module32, /* [in] name of win32 dll */
1331 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
1332 DWORD dwReason, /* [in] initialisation argument */
1333 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
1334 LPSTR thunkfun32, /* [in] win32 thunkfunction */
1335 WORD cs /* [in] CS of win16 dll */
1337 BOOL directionSL;
1339 if (!strncmp(TD->magic, "SL01", 4))
1341 directionSL = TRUE;
1343 TRACE("SL01 thunk %s (%p) -> %s (%s), Reason: %d\n",
1344 module16, TD, module32, thunkfun32, dwReason);
1346 else if (!strncmp(TD->magic, "LS01", 4))
1348 directionSL = FALSE;
1350 TRACE("LS01 thunk %s (%p) <- %s (%s), Reason: %d\n",
1351 module16, TD, module32, thunkfun32, dwReason);
1353 else
1355 ERR("Invalid magic %c%c%c%c\n",
1356 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
1357 return 0;
1360 switch (dwReason)
1362 case DLL_PROCESS_ATTACH:
1363 if (directionSL)
1365 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
1366 struct ThunkDataSL *SL = SL16->fpData;
1368 if (SL == NULL)
1370 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
1372 SL->common = SL16->common;
1373 SL->flags1 = SL16->flags1;
1374 SL->flags2 = SL16->flags2;
1376 SL->apiDB = MapSL(SL16->apiDatabase);
1377 SL->targetDB = NULL;
1379 lstrcpynA(SL->pszDll16, module16, 255);
1380 lstrcpynA(SL->pszDll32, module32, 255);
1382 /* We should create a SEGPTR to the ThunkDataSL,
1383 but since the contents are not in the original format,
1384 any access to this by 16-bit code would crash anyway. */
1385 SL16->spData = 0;
1386 SL16->fpData = SL;
1390 if (SL->flags2 & 0x80000000)
1392 TRACE("Preloading 32-bit library\n");
1393 LoadLibraryA(module32);
1396 else
1398 /* nothing to do */
1400 break;
1402 case DLL_PROCESS_DETACH:
1403 /* FIXME: cleanup */
1404 break;
1407 return 1;
1411 /***********************************************************************
1412 * C16ThkSL (KERNEL.630)
1415 void WINAPI C16ThkSL(CONTEXT *context)
1417 LPBYTE stub = MapSL(context->Eax), x = stub;
1419 /* We produce the following code:
1421 * mov ax, __FLATDS
1422 * mov es, ax
1423 * movzx ecx, cx
1424 * mov edx, es:[ecx + $EDX]
1425 * push bp
1426 * push edx
1427 * push dx
1428 * push edx
1429 * call __FLATCS:__wine_call_from_16_thunk
1432 *x++ = 0xB8; *(WORD *)x = get_ds(); x += sizeof(WORD);
1433 *x++ = 0x8E; *x++ = 0xC0;
1434 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1435 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1436 *x++ = 0x91; *(DWORD *)x = context->Edx; x += sizeof(DWORD);
1438 *x++ = 0x55;
1439 *x++ = 0x66; *x++ = 0x52;
1440 *x++ = 0x52;
1441 *x++ = 0x66; *x++ = 0x52;
1442 *x++ = 0x66; *x++ = 0x9A;
1443 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1444 *(WORD *)x = get_cs(); x += sizeof(WORD);
1446 /* Jump to the stub code just created */
1447 context->Eip = LOWORD(context->Eax);
1448 context->SegCs = HIWORD(context->Eax);
1450 /* Since C16ThkSL got called by a jmp, we need to leave the
1451 original return address on the stack */
1452 context->Esp -= 4;
1455 /***********************************************************************
1456 * C16ThkSL01 (KERNEL.631)
1459 void WINAPI C16ThkSL01(CONTEXT *context)
1461 LPBYTE stub = MapSL(context->Eax), x = stub;
1463 if (stub)
1465 struct ThunkDataSL16 *SL16 = MapSL(context->Edx);
1466 struct ThunkDataSL *td = SL16->fpData;
1468 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), (LPCSTR)631);
1470 if (!td)
1472 ERR("ThunkConnect16 was not called!\n");
1473 return;
1476 TRACE("Creating stub for ThunkDataSL %p\n", td);
1479 /* We produce the following code:
1481 * xor eax, eax
1482 * mov edx, $td
1483 * call C16ThkSL01
1484 * push bp
1485 * push edx
1486 * push dx
1487 * push edx
1488 * call __FLATCS:__wine_call_from_16_thunk
1491 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1492 *x++ = 0x66; *x++ = 0xBA; *(void **)x = td; x += sizeof(void *);
1493 *x++ = 0x9A; *(DWORD *)x = procAddress; x += sizeof(DWORD);
1495 *x++ = 0x55;
1496 *x++ = 0x66; *x++ = 0x52;
1497 *x++ = 0x52;
1498 *x++ = 0x66; *x++ = 0x52;
1499 *x++ = 0x66; *x++ = 0x9A;
1500 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1501 *(WORD *)x = get_cs(); x += sizeof(WORD);
1503 /* Jump to the stub code just created */
1504 context->Eip = LOWORD(context->Eax);
1505 context->SegCs = HIWORD(context->Eax);
1507 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1508 original return address on the stack */
1509 context->Esp -= 4;
1511 else
1513 struct ThunkDataSL *td = (struct ThunkDataSL *)context->Edx;
1514 DWORD targetNr = LOWORD(context->Ecx) / 4;
1515 struct SLTargetDB *tdb;
1517 TRACE("Process %08x calling target %d of ThunkDataSL %p\n",
1518 GetCurrentProcessId(), targetNr, td);
1520 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1521 if (tdb->process == GetCurrentProcessId())
1522 break;
1524 if (!tdb)
1526 TRACE("Loading 32-bit library %s\n", td->pszDll32);
1527 LoadLibraryA(td->pszDll32);
1529 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1530 if (tdb->process == GetCurrentProcessId())
1531 break;
1534 if (tdb)
1536 context->Edx = tdb->targetTable[targetNr];
1538 TRACE("Call target is %08x\n", context->Edx);
1540 else
1542 WORD *stack = MapSL( MAKESEGPTR(context->SegSs, LOWORD(context->Esp)) );
1543 context->Edx = (context->Edx & ~0xffff) | HIWORD(td->apiDB[targetNr].errorReturnValue);
1544 context->Eax = (context->Eax & ~0xffff) | LOWORD(td->apiDB[targetNr].errorReturnValue);
1545 context->Eip = stack[2];
1546 context->SegCs = stack[3];
1547 context->Esp += td->apiDB[targetNr].nrArgBytes + 4;
1549 ERR("Process %08x did not ThunkConnect32 %s to %s\n",
1550 GetCurrentProcessId(), td->pszDll32, td->pszDll16);
1556 /***********************************************************************
1557 * 16<->32 Thunklet/Callback API:
1560 #include "pshpack1.h"
1561 typedef struct _THUNKLET
1563 BYTE prefix_target;
1564 BYTE pushl_target;
1565 DWORD target;
1567 BYTE prefix_relay;
1568 BYTE pushl_relay;
1569 DWORD relay;
1571 BYTE jmp_glue;
1572 DWORD glue;
1574 BYTE type;
1575 HINSTANCE16 owner;
1576 struct _THUNKLET *next;
1577 } THUNKLET;
1578 #include "poppack.h"
1580 #define THUNKLET_TYPE_LS 1
1581 #define THUNKLET_TYPE_SL 2
1583 static HANDLE ThunkletHeap = 0;
1584 static WORD ThunkletCodeSel;
1585 static THUNKLET *ThunkletAnchor = NULL;
1587 static FARPROC ThunkletSysthunkGlueLS = 0;
1588 static SEGPTR ThunkletSysthunkGlueSL = 0;
1590 static FARPROC ThunkletCallbackGlueLS = 0;
1591 static SEGPTR ThunkletCallbackGlueSL = 0;
1594 /* map a thunk allocated on ThunkletHeap to a 16-bit pointer */
1595 static inline SEGPTR get_segptr( void *thunk )
1597 if (!thunk) return 0;
1598 return MAKESEGPTR( ThunkletCodeSel, (char *)thunk - (char *)ThunkletHeap );
1601 /***********************************************************************
1602 * THUNK_Init
1604 static BOOL THUNK_Init(void)
1606 LPBYTE thunk;
1608 ThunkletHeap = HeapCreate( HEAP_CREATE_ENABLE_EXECUTE, 0x10000, 0x10000 );
1609 if (!ThunkletHeap) return FALSE;
1611 ThunkletCodeSel = SELECTOR_AllocBlock( ThunkletHeap, 0x10000, LDT_FLAGS_CODE );
1613 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1614 if (!thunk) return FALSE;
1616 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1617 *thunk++ = 0x58; /* popl eax */
1618 *thunk++ = 0xC3; /* ret */
1620 ThunkletSysthunkGlueSL = get_segptr( thunk );
1621 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1622 *thunk++ = 0xCB; /* lret */
1624 return TRUE;
1627 /***********************************************************************
1628 * SetThunkletCallbackGlue (KERNEL.560)
1630 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1632 ThunkletCallbackGlueLS = glueLS;
1633 ThunkletCallbackGlueSL = glueSL;
1637 /***********************************************************************
1638 * THUNK_FindThunklet
1640 static THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1641 DWORD glue, BYTE type )
1643 THUNKLET *thunk;
1645 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1646 if ( thunk->type == type
1647 && thunk->target == target
1648 && thunk->relay == relay
1649 && ( type == THUNKLET_TYPE_LS ?
1650 ( thunk->glue == glue - (DWORD)&thunk->type )
1651 : ( thunk->glue == glue ) ) )
1652 return thunk;
1654 return NULL;
1657 /***********************************************************************
1658 * THUNK_AllocLSThunklet
1660 static FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1661 FARPROC glue, HTASK16 owner )
1663 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1664 THUNKLET_TYPE_LS );
1665 if (!thunk)
1667 TDB *pTask = GlobalLock16( owner );
1669 if (!ThunkletHeap) THUNK_Init();
1670 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1671 return 0;
1673 thunk->prefix_target = thunk->prefix_relay = 0x90;
1674 thunk->pushl_target = thunk->pushl_relay = 0x68;
1675 thunk->jmp_glue = 0xE9;
1677 thunk->target = (DWORD)target;
1678 thunk->relay = relay;
1679 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1681 thunk->type = THUNKLET_TYPE_LS;
1682 thunk->owner = pTask? pTask->hInstance : 0;
1684 thunk->next = ThunkletAnchor;
1685 ThunkletAnchor = thunk;
1688 return (FARPROC)thunk;
1691 /***********************************************************************
1692 * THUNK_AllocSLThunklet
1694 static SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1695 SEGPTR glue, HTASK16 owner )
1697 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1698 THUNKLET_TYPE_SL );
1699 if (!thunk)
1701 TDB *pTask = GlobalLock16( owner );
1703 if (!ThunkletHeap) THUNK_Init();
1704 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1705 return 0;
1707 thunk->prefix_target = thunk->prefix_relay = 0x66;
1708 thunk->pushl_target = thunk->pushl_relay = 0x68;
1709 thunk->jmp_glue = 0xEA;
1711 thunk->target = (DWORD)target;
1712 thunk->relay = relay;
1713 thunk->glue = (DWORD)glue;
1715 thunk->type = THUNKLET_TYPE_SL;
1716 thunk->owner = pTask? pTask->hInstance : 0;
1718 thunk->next = ThunkletAnchor;
1719 ThunkletAnchor = thunk;
1722 return get_segptr( thunk );
1725 /**********************************************************************
1726 * IsLSThunklet
1728 static BOOL16 IsLSThunklet( THUNKLET *thunk )
1730 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1731 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1732 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1735 /**********************************************************************
1736 * IsSLThunklet (KERNEL.612)
1738 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1740 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1741 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1742 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1747 /***********************************************************************
1748 * AllocLSThunkletSysthunk (KERNEL.607)
1750 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1751 FARPROC relay, DWORD dummy )
1753 if (!ThunkletSysthunkGlueLS) THUNK_Init();
1754 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1755 ThunkletSysthunkGlueLS, GetCurrentTask() );
1758 /***********************************************************************
1759 * AllocSLThunkletSysthunk (KERNEL.608)
1761 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1762 SEGPTR relay, DWORD dummy )
1764 if (!ThunkletSysthunkGlueSL) THUNK_Init();
1765 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1766 ThunkletSysthunkGlueSL, GetCurrentTask() );
1770 /***********************************************************************
1771 * AllocLSThunkletCallbackEx (KERNEL.567)
1773 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1774 DWORD relay, HTASK16 task )
1776 THUNKLET *thunk = MapSL( target );
1777 if ( !thunk ) return NULL;
1779 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1780 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1781 return (FARPROC)thunk->target;
1783 return THUNK_AllocLSThunklet( target, relay,
1784 ThunkletCallbackGlueLS, task );
1787 /***********************************************************************
1788 * AllocSLThunkletCallbackEx (KERNEL.568)
1790 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1791 DWORD relay, HTASK16 task )
1793 THUNKLET *thunk = (THUNKLET *)target;
1794 if ( !thunk ) return 0;
1796 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1797 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1798 return (SEGPTR)thunk->target;
1800 return THUNK_AllocSLThunklet( target, relay,
1801 ThunkletCallbackGlueSL, task );
1804 /***********************************************************************
1805 * AllocLSThunkletCallback (KERNEL.561)
1806 * AllocLSThunkletCallback_dup (KERNEL.606)
1808 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1810 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1813 /***********************************************************************
1814 * AllocSLThunkletCallback (KERNEL.562)
1815 * AllocSLThunkletCallback_dup (KERNEL.605)
1817 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1819 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1822 /***********************************************************************
1823 * FindLSThunkletCallback (KERNEL.563)
1824 * FindLSThunkletCallback_dup (KERNEL.609)
1826 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1828 THUNKLET *thunk = MapSL( target );
1829 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1830 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1831 return (FARPROC)thunk->target;
1833 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1834 (DWORD)ThunkletCallbackGlueLS,
1835 THUNKLET_TYPE_LS );
1836 return (FARPROC)thunk;
1839 /***********************************************************************
1840 * FindSLThunkletCallback (KERNEL.564)
1841 * FindSLThunkletCallback_dup (KERNEL.610)
1843 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1845 THUNKLET *thunk = (THUNKLET *)target;
1846 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1847 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1848 return (SEGPTR)thunk->target;
1850 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1851 (DWORD)ThunkletCallbackGlueSL,
1852 THUNKLET_TYPE_SL );
1853 return get_segptr( thunk );
1857 /***********************************************************************
1858 * FreeThunklet (KERNEL.611)
1860 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1862 return FALSE;
1866 /***********************************************************************
1867 * Callback Client API
1870 #define N_CBC_FIXED 20
1871 #define N_CBC_VARIABLE 10
1872 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1874 static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
1875 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1877 /***********************************************************************
1878 * RegisterCBClient (KERNEL.619)
1880 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1881 SEGPTR relay16, FARPROC *relay32 )
1883 /* Search for free Callback ID */
1884 if ( wCBCId == -1 )
1885 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1886 if ( !CBClientRelay16[ wCBCId ] )
1887 break;
1889 /* Register Callback ID */
1890 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1892 CBClientRelay16[ wCBCId ] = relay16;
1893 CBClientRelay32[ wCBCId ] = relay32;
1895 else
1896 wCBCId = 0;
1898 return wCBCId;
1901 /***********************************************************************
1902 * UnRegisterCBClient (KERNEL.622)
1904 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1905 SEGPTR relay16, FARPROC *relay32 )
1907 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1908 && CBClientRelay16[ wCBCId ] == relay16
1909 && CBClientRelay32[ wCBCId ] == relay32 )
1911 CBClientRelay16[ wCBCId ] = 0;
1912 CBClientRelay32[ wCBCId ] = 0;
1914 else
1915 wCBCId = 0;
1917 return wCBCId;
1921 /***********************************************************************
1922 * InitCBClient (KERNEL.623)
1924 void WINAPI InitCBClient16( FARPROC glueLS )
1926 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1927 SEGPTR glueSL = (SEGPTR)GetProcAddress16( kernel, (LPCSTR)604 );
1929 SetThunkletCallbackGlue16( glueLS, glueSL );
1932 /***********************************************************************
1933 * CBClientGlueSL (KERNEL.604)
1935 void WINAPI CBClientGlueSL( CONTEXT *context )
1937 /* Create stack frame */
1938 SEGPTR stackSeg = stack16_push( 12 );
1939 LPWORD stackLin = MapSL( stackSeg );
1940 SEGPTR glue, *glueTab;
1942 stackLin[3] = (WORD)context->Ebp;
1943 stackLin[2] = (WORD)context->Esi;
1944 stackLin[1] = (WORD)context->Edi;
1945 stackLin[0] = (WORD)context->SegDs;
1947 context->Ebp = OFFSETOF( stackSeg ) + 6;
1948 context->Esp = OFFSETOF( stackSeg ) - 4;
1949 context->SegGs = 0;
1951 /* Jump to 16-bit relay code */
1952 glueTab = MapSL( CBClientRelay16[ stackLin[5] ] );
1953 glue = glueTab[ stackLin[4] ];
1954 context->SegCs = SELECTOROF( glue );
1955 context->Eip = OFFSETOF ( glue );
1958 /***********************************************************************
1959 * CBClientThunkSL (KERNEL.620)
1961 extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, WORD *stackLin, DWORD *esi );
1962 void WINAPI CBClientThunkSL( CONTEXT *context )
1964 /* Call 32-bit relay code */
1966 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
1967 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1969 /* fill temporary area for the asm code (see comments in winebuild) */
1970 SEGPTR stack = stack16_push( 12 );
1971 LPWORD stackLin = MapSL(stack);
1972 /* stackLin[0] and stackLin[1] reserved for the 32-bit stack ptr */
1973 stackLin[2] = get_ds();
1974 stackLin[3] = 0;
1975 stackLin[4] = OFFSETOF(stack) + 12;
1976 stackLin[5] = SELECTOROF(stack);
1977 stackLin[6] = OFFSETOF(CALL32_CBClientEx_RetAddr); /* overwrite return address */
1978 stackLin[7] = SELECTOROF(CALL32_CBClientEx_RetAddr);
1979 context->Eax = CALL32_CBClient( proc, args, stackLin + 4, &context->Esi );
1980 stack16_pop( 12 );
1983 /***********************************************************************
1984 * CBClientThunkSLEx (KERNEL.621)
1986 extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, WORD *stackLin, DWORD *esi, INT *nArgs );
1987 void WINAPI CBClientThunkSLEx( CONTEXT *context )
1989 /* Call 32-bit relay code */
1991 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
1992 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1993 INT nArgs;
1994 LPWORD stackLin;
1996 /* fill temporary area for the asm code (see comments in winebuild) */
1997 SEGPTR stack = stack16_push( 24 );
1998 stackLin = MapSL(stack);
1999 stackLin[0] = OFFSETOF(stack) + 4;
2000 stackLin[1] = SELECTOROF(stack);
2001 stackLin[2] = get_ds();
2002 stackLin[5] = OFFSETOF(stack) + 24;
2003 /* stackLin[6] and stackLin[7] reserved for the 32-bit stack ptr */
2004 stackLin[8] = get_ds();
2005 stackLin[9] = 0;
2006 stackLin[10] = OFFSETOF(CALL32_CBClientEx_RetAddr);
2007 stackLin[11] = SELECTOROF(CALL32_CBClientEx_RetAddr);
2009 context->Eax = CALL32_CBClientEx( proc, args, stackLin, &context->Esi, &nArgs );
2010 stack16_pop( 24 );
2012 /* Restore registers saved by CBClientGlueSL */
2013 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
2014 context->Ebp = (context->Ebp & ~0xffff) | stackLin[3];
2015 context->Esi = (context->Esi & ~0xffff) | stackLin[2];
2016 context->Edi = (context->Edi & ~0xffff) | stackLin[1];
2017 context->SegDs = stackLin[0];
2018 context->Esp += 16+nArgs;
2020 /* Return to caller of CBClient thunklet */
2021 context->SegCs = stackLin[9];
2022 context->Eip = stackLin[8];
2026 /***********************************************************************
2027 * Get16DLLAddress (KERNEL32.@)
2029 * This function is used by a Win32s DLL if it wants to call a Win16 function.
2030 * A 16:16 segmented pointer to the function is returned.
2031 * Written without any docu.
2033 SEGPTR WINAPI Get16DLLAddress(HMODULE16 handle, LPSTR func_name)
2035 static WORD code_sel32;
2036 FARPROC16 proc_16;
2037 LPBYTE thunk;
2039 if (!code_sel32)
2041 if (!ThunkletHeap) THUNK_Init();
2042 code_sel32 = SELECTOR_AllocBlock( ThunkletHeap, 0x10000, LDT_FLAGS_CODE | LDT_FLAGS_32BIT );
2043 if (!code_sel32) return 0;
2045 if (!(thunk = HeapAlloc( ThunkletHeap, 0, 32 ))) return 0;
2047 if (!handle) handle = GetModuleHandle16("WIN32S16");
2048 proc_16 = GetProcAddress16(handle, func_name);
2050 /* movl proc_16, $edx */
2051 *thunk++ = 0xba;
2052 *(FARPROC16 *)thunk = proc_16;
2053 thunk += sizeof(FARPROC16);
2055 /* jmpl QT_Thunk */
2056 *thunk++ = 0xea;
2057 *(void **)thunk = QT_Thunk;
2058 thunk += sizeof(FARPROC16);
2059 *(WORD *)thunk = get_cs();
2061 return MAKESEGPTR( code_sel32, (char *)thunk - (char *)ThunkletHeap );
2065 /***********************************************************************
2066 * GetWin16DOSEnv (KERNEL32.34)
2067 * Returns some internal value.... probably the default environment database?
2069 DWORD WINAPI GetWin16DOSEnv(void)
2071 FIXME("stub, returning 0\n");
2072 return 0;
2075 /**********************************************************************
2076 * GetPK16SysVar (KERNEL32.92)
2078 LPVOID WINAPI GetPK16SysVar(void)
2080 static BYTE PK16SysVar[128];
2082 FIXME("()\n");
2083 return PK16SysVar;
2086 /**********************************************************************
2087 * CommonUnimpStub (KERNEL32.17)
2089 int WINAPI DECLSPEC_HIDDEN __regs_CommonUnimpStub( const char *name, int type )
2091 FIXME("generic stub %s\n", debugstr_a(name));
2093 switch (type)
2095 case 15: return -1;
2096 case 14: return ERROR_CALL_NOT_IMPLEMENTED;
2097 case 13: return ERROR_NOT_SUPPORTED;
2098 case 1: return 1;
2099 default: return 0;
2102 __ASM_STDCALL_FUNC( CommonUnimpStub, 0,
2103 "pushl %ecx\n\t"
2104 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2105 "shrl $4,%ecx\n\t"
2106 "andl $0xf,%ecx\n\t"
2107 "pushl %ecx\n\t"
2108 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2109 "pushl %eax\n\t"
2110 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2111 "call " __ASM_STDCALL("__regs_CommonUnimpStub",8) "\n\t"
2112 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
2113 "popl %ecx\n\t"
2114 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
2115 "andl $0xf,%ecx\n\t"
2116 "movl (%esp),%edx\n\t"
2117 "leal (%esp,%ecx,4),%esp\n\t"
2118 "movl %edx,(%esp)\n\t"
2119 "ret" )
2121 /**********************************************************************
2122 * HouseCleanLogicallyDeadHandles (KERNEL32.33)
2124 void WINAPI HouseCleanLogicallyDeadHandles(void)
2126 /* Whatever this is supposed to do, our handles probably
2127 don't need it :-) */
2130 /**********************************************************************
2131 * @ (KERNEL32.100)
2133 BOOL WINAPI _KERNEL32_100(HANDLE threadid,DWORD exitcode,DWORD x)
2135 FIXME("(%p,%d,0x%08x): stub\n",threadid,exitcode,x);
2136 return TRUE;
2139 /**********************************************************************
2140 * @ (KERNEL32.99)
2142 * Checks whether the clock has to be switched from daylight
2143 * savings time to standard time or vice versa.
2146 DWORD WINAPI _KERNEL32_99(DWORD x)
2148 FIXME("(0x%08x): stub\n",x);
2149 return 1;
2153 /***********************************************************************
2154 * Helper for k32 family functions
2156 static void *user32_proc_address(const char *proc_name)
2158 static HMODULE hUser32;
2160 if(!hUser32) hUser32 = LoadLibraryA("user32.dll");
2161 return GetProcAddress(hUser32, proc_name);
2164 /***********************************************************************
2165 * k32CharToOemBuffA (KERNEL32.11)
2167 BOOL WINAPI k32CharToOemBuffA(LPCSTR s, LPSTR d, DWORD len)
2169 WCHAR *bufW;
2171 if ((bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
2173 MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
2174 WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
2175 HeapFree( GetProcessHeap(), 0, bufW );
2177 return TRUE;
2180 /***********************************************************************
2181 * k32CharToOemA (KERNEL32.10)
2183 BOOL WINAPI k32CharToOemA(LPCSTR s, LPSTR d)
2185 if (!s || !d) return TRUE;
2186 return k32CharToOemBuffA( s, d, strlen(s) + 1 );
2189 /***********************************************************************
2190 * k32OemToCharBuffA (KERNEL32.13)
2192 BOOL WINAPI k32OemToCharBuffA(LPCSTR s, LPSTR d, DWORD len)
2194 WCHAR *bufW;
2196 if ((bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
2198 MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
2199 WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
2200 HeapFree( GetProcessHeap(), 0, bufW );
2202 return TRUE;
2205 /***********************************************************************
2206 * k32OemToCharA (KERNEL32.12)
2208 BOOL WINAPI k32OemToCharA(LPCSTR s, LPSTR d)
2210 return k32OemToCharBuffA( s, d, strlen(s) + 1 );
2213 /**********************************************************************
2214 * k32LoadStringA (KERNEL32.14)
2216 INT WINAPI k32LoadStringA(HINSTANCE instance, UINT resource_id,
2217 LPSTR buffer, INT buflen)
2219 static INT (WINAPI *pLoadStringA)(HINSTANCE, UINT, LPSTR, INT);
2221 if(!pLoadStringA) pLoadStringA = user32_proc_address("LoadStringA");
2222 return pLoadStringA(instance, resource_id, buffer, buflen);
2225 /***********************************************************************
2226 * k32wvsprintfA (KERNEL32.16)
2228 INT WINAPI k32wvsprintfA(LPSTR buffer, LPCSTR spec, __ms_va_list args)
2230 static INT (WINAPI *pwvsprintfA)(LPSTR, LPCSTR, __ms_va_list);
2232 if(!pwvsprintfA) pwvsprintfA = user32_proc_address("wvsprintfA");
2233 return (*pwvsprintfA)(buffer, spec, args);
2236 /***********************************************************************
2237 * k32wsprintfA (KERNEL32.15)
2239 INT WINAPIV k32wsprintfA(LPSTR buffer, LPCSTR spec, ...)
2241 __ms_va_list args;
2242 INT res;
2244 __ms_va_start(args, spec);
2245 res = k32wvsprintfA(buffer, spec, args);
2246 __ms_va_end(args);
2247 return res;
2250 /**********************************************************************
2251 * Catch (KERNEL.55)
2253 * Real prototype is:
2254 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
2256 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT *context )
2258 /* Note: we don't save the current ss, as the catch buffer is */
2259 /* only 9 words long. Hopefully no one will have the silly */
2260 /* idea to change the current stack before calling Throw()... */
2262 /* Windows uses:
2263 * lpbuf[0] = ip
2264 * lpbuf[1] = cs
2265 * lpbuf[2] = sp
2266 * lpbuf[3] = bp
2267 * lpbuf[4] = si
2268 * lpbuf[5] = di
2269 * lpbuf[6] = ds
2270 * lpbuf[7] = unused
2271 * lpbuf[8] = ss
2274 lpbuf[0] = LOWORD(context->Eip);
2275 lpbuf[1] = context->SegCs;
2276 /* Windows pushes 4 more words before saving sp */
2277 lpbuf[2] = LOWORD(context->Esp) - 4 * sizeof(WORD);
2278 lpbuf[3] = LOWORD(context->Ebp);
2279 lpbuf[4] = LOWORD(context->Esi);
2280 lpbuf[5] = LOWORD(context->Edi);
2281 lpbuf[6] = context->SegDs;
2282 lpbuf[7] = 0;
2283 lpbuf[8] = context->SegSs;
2284 context->Eax &= ~0xffff; /* Return 0 */
2288 /**********************************************************************
2289 * Throw (KERNEL.56)
2291 * Real prototype is:
2292 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
2294 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT *context )
2296 STACK16FRAME *pFrame;
2297 STACK32FRAME *frame32;
2299 context->Eax = (context->Eax & ~0xffff) | (WORD)retval;
2301 /* Find the frame32 corresponding to the frame16 we are jumping to */
2302 pFrame = CURRENT_STACK16;
2303 frame32 = pFrame->frame32;
2304 while (frame32 && frame32->frame16)
2306 if (OFFSETOF(frame32->frame16) < OFFSETOF(NtCurrentTeb()->WOW32Reserved))
2307 break; /* Something strange is going on */
2308 if (OFFSETOF(frame32->frame16) > lpbuf[2])
2310 /* We found the right frame */
2311 pFrame->frame32 = frame32;
2312 break;
2314 frame32 = ((STACK16FRAME *)MapSL(frame32->frame16))->frame32;
2316 RtlUnwind( &pFrame->frame32->frame, NULL, NULL, 0 );
2318 context->Eip = lpbuf[0];
2319 context->SegCs = lpbuf[1];
2320 context->Esp = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
2321 context->Ebp = lpbuf[3];
2322 context->Esi = lpbuf[4];
2323 context->Edi = lpbuf[5];
2324 context->SegDs = lpbuf[6];
2326 if (lpbuf[8] != context->SegSs)
2327 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );
2332 * 16-bit WOW routines (in KERNEL)
2335 /**********************************************************************
2336 * GetVDMPointer32W (KERNEL.516)
2338 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
2340 GlobalPageLock16(GlobalHandle16(SELECTOROF(vp)));
2341 return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
2344 /***********************************************************************
2345 * LoadLibraryEx32W (KERNEL.513)
2347 DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
2349 HMODULE hModule;
2350 DWORD mutex_count;
2351 OFSTRUCT ofs;
2352 const char *p;
2354 if (!lpszLibFile)
2356 SetLastError(ERROR_INVALID_PARAMETER);
2357 return 0;
2360 /* if the file cannot be found, call LoadLibraryExA anyway, since it might be
2361 a builtin module. This case is handled in MODULE_LoadLibraryExA */
2363 if ((p = strrchr( lpszLibFile, '.' )) && !strchr( p, '\\' )) /* got an extension */
2365 if (OpenFile16( lpszLibFile, &ofs, OF_EXIST ) != HFILE_ERROR16)
2366 lpszLibFile = ofs.szPathName;
2368 else
2370 char buffer[MAX_PATH+4];
2371 strcpy( buffer, lpszLibFile );
2372 strcat( buffer, ".dll" );
2373 if (OpenFile16( buffer, &ofs, OF_EXIST ) != HFILE_ERROR16)
2374 lpszLibFile = ofs.szPathName;
2377 ReleaseThunkLock( &mutex_count );
2378 hModule = LoadLibraryExA( lpszLibFile, (HANDLE)hFile, dwFlags );
2379 RestoreThunkLock( mutex_count );
2381 return (DWORD)hModule;
2384 /***********************************************************************
2385 * GetProcAddress32W (KERNEL.515)
2387 DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
2389 return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
2392 /***********************************************************************
2393 * FreeLibrary32W (KERNEL.514)
2395 DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
2397 BOOL retv;
2398 DWORD mutex_count;
2400 ReleaseThunkLock( &mutex_count );
2401 retv = FreeLibrary( (HMODULE)hLibModule );
2402 RestoreThunkLock( mutex_count );
2403 return (DWORD)retv;
2406 /**********************************************************************
2407 * WOW_CallProc32W
2409 static DWORD WOW_CallProc32W16( FARPROC proc32, DWORD nrofargs, DWORD *args )
2411 DWORD ret;
2412 DWORD mutex_count;
2414 ReleaseThunkLock( &mutex_count );
2415 if (!proc32) ret = 0;
2416 else ret = call_entry_point( proc32, nrofargs & ~CPEX_DEST_CDECL, args );
2417 RestoreThunkLock( mutex_count );
2419 TRACE("returns %08x\n",ret);
2420 return ret;
2423 /**********************************************************************
2424 * CallProc32W (KERNEL.517)
2426 DWORD WINAPIV CallProc32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
2428 DWORD args[32];
2429 unsigned int i;
2431 TRACE("(%d,%d,%p args[",nrofargs,argconvmask,proc32);
2433 for (i=0;i<nrofargs;i++)
2435 if (argconvmask & (1<<i))
2437 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
2438 /* pascal convention, have to reverse the arguments order */
2439 args[nrofargs - i - 1] = (DWORD)MapSL(ptr);
2440 TRACE("%08x(%p),",ptr,MapSL(ptr));
2442 else
2444 DWORD arg = VA_ARG16( valist, DWORD );
2445 /* pascal convention, have to reverse the arguments order */
2446 args[nrofargs - i - 1] = arg;
2447 TRACE("%d,", arg);
2450 TRACE("])\n");
2452 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
2453 stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
2455 return WOW_CallProc32W16( proc32, nrofargs, args );
2458 /**********************************************************************
2459 * _CallProcEx32W (KERNEL.518)
2461 DWORD WINAPIV CallProcEx32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
2463 DWORD args[32];
2464 unsigned int i, count = min( 32, nrofargs & ~CPEX_DEST_CDECL );
2466 TRACE("(%s,%d,%d,%p args[", nrofargs & CPEX_DEST_CDECL ? "cdecl": "stdcall",
2467 nrofargs & ~CPEX_DEST_CDECL, argconvmask, proc32);
2469 for (i = 0; i < count; i++)
2471 if (argconvmask & (1<<i))
2473 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
2474 args[i] = (DWORD)MapSL(ptr);
2475 TRACE("%08x(%p),",ptr,MapSL(ptr));
2477 else
2479 DWORD arg = VA_ARG16( valist, DWORD );
2480 args[i] = arg;
2481 TRACE("%d,", arg);
2484 TRACE("])\n");
2485 return WOW_CallProc32W16( proc32, nrofargs, args );
2489 /**********************************************************************
2490 * WOW16Call (KERNEL.500)
2492 * FIXME!!!
2495 DWORD WINAPIV WOW16Call(WORD x, WORD y, WORD z, VA_LIST16 args)
2497 int i;
2498 DWORD calladdr;
2499 FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
2501 for (i=0;i<x/2;i++) {
2502 WORD a = VA_ARG16(args,WORD);
2503 FIXME("%04x ",a);
2505 calladdr = VA_ARG16(args,DWORD);
2506 stack16_pop( 3*sizeof(WORD) + x + sizeof(DWORD) );
2507 FIXME(") calling address was 0x%08x\n",calladdr);
2508 return 0;