gdiplus: Support GdipSetClipRegion in metafiles.
[wine.git] / dlls / krnl386.exe16 / thunk.c
blob737f6a8057443924c5708df5691761d3ca371c3e
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 "config.h"
24 #include "wine/port.h"
26 #include <string.h>
27 #include <sys/types.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winternl.h"
38 #include "wownt32.h"
39 #include "wine/winbase16.h"
41 #include "wine/debug.h"
42 #include "wine/library.h"
43 #include "kernel16_private.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
47 struct ThunkDataCommon
49 char magic[4]; /* 00 */
50 DWORD checksum; /* 04 */
53 struct ThunkDataLS16
55 struct ThunkDataCommon common; /* 00 */
56 SEGPTR targetTable; /* 08 */
57 DWORD firstTime; /* 0C */
60 struct ThunkDataLS32
62 struct ThunkDataCommon common; /* 00 */
63 DWORD * targetTable; /* 08 */
64 char lateBinding[4]; /* 0C */
65 DWORD flags; /* 10 */
66 DWORD reserved1; /* 14 */
67 DWORD reserved2; /* 18 */
68 DWORD offsetQTThunk; /* 1C */
69 DWORD offsetFTProlog; /* 20 */
72 struct ThunkDataSL16
74 struct ThunkDataCommon common; /* 00 */
75 DWORD flags1; /* 08 */
76 DWORD reserved1; /* 0C */
77 struct ThunkDataSL * fpData; /* 10 */
78 SEGPTR spData; /* 14 */
79 DWORD reserved2; /* 18 */
80 char lateBinding[4]; /* 1C */
81 DWORD flags2; /* 20 */
82 DWORD reserved3; /* 20 */
83 SEGPTR apiDatabase; /* 28 */
86 struct ThunkDataSL32
88 struct ThunkDataCommon common; /* 00 */
89 DWORD reserved1; /* 08 */
90 struct ThunkDataSL * data; /* 0C */
91 char lateBinding[4]; /* 10 */
92 DWORD flags; /* 14 */
93 DWORD reserved2; /* 18 */
94 DWORD reserved3; /* 1C */
95 DWORD offsetTargetTable; /* 20 */
98 struct ThunkDataSL
100 #if 0
101 This structure differs from the Win95 original,
102 but this should not matter since it is strictly internal to
103 the thunk handling routines in KRNL386 / KERNEL32.
105 For reference, here is the Win95 layout:
107 struct ThunkDataCommon common; /* 00 */
108 DWORD flags1; /* 08 */
109 SEGPTR apiDatabase; /* 0C */
110 WORD exePtr; /* 10 */
111 WORD segMBA; /* 12 */
112 DWORD lenMBATotal; /* 14 */
113 DWORD lenMBAUsed; /* 18 */
114 DWORD flags2; /* 1C */
115 char pszDll16[256]; /* 20 */
116 char pszDll32[256]; /*120 */
118 We do it differently since all our thunk handling is done
119 by 32-bit code. Therefore we do not need to provide
120 easy access to this data, especially the process target
121 table database, for 16-bit code.
122 #endif
124 struct ThunkDataCommon common;
125 DWORD flags1;
126 struct SLApiDB * apiDB;
127 struct SLTargetDB * targetDB;
128 DWORD flags2;
129 char pszDll16[256];
130 char pszDll32[256];
133 struct SLTargetDB
135 struct SLTargetDB * next;
136 DWORD process;
137 DWORD * targetTable;
140 struct SLApiDB
142 DWORD nrArgBytes;
143 DWORD errorReturnValue;
146 SEGPTR CALL32_CBClient_RetAddr = 0;
147 SEGPTR CALL32_CBClientEx_RetAddr = 0;
149 extern int call_entry_point( void *func, int nb_args, const DWORD *args );
150 extern void __wine_call_from_16_thunk(void);
151 extern void FT_Prolog(void);
152 extern void FT_PrologPrime(void);
153 extern void QT_Thunk(void);
154 extern void QT_ThunkPrime(void);
156 /***********************************************************************
158 * Win95 internal thunks *
160 ***********************************************************************/
162 /***********************************************************************
163 * LogApiThk (KERNEL.423)
165 void WINAPI LogApiThk( LPSTR func )
167 TRACE( "%s\n", debugstr_a(func) );
170 /***********************************************************************
171 * LogApiThkLSF (KERNEL32.42)
173 * NOTE: needs to preserve all registers!
175 __ASM_STDCALL_FUNC( LogApiThkLSF, 4, "ret $4" )
177 /***********************************************************************
178 * LogApiThkSL (KERNEL32.44)
180 * NOTE: needs to preserve all registers!
182 __ASM_STDCALL_FUNC( LogApiThkSL, 4, "ret $4" )
184 /***********************************************************************
185 * LogCBThkSL (KERNEL32.47)
187 * NOTE: needs to preserve all registers!
189 __ASM_STDCALL_FUNC( LogCBThkSL, 4, "ret $4" )
191 /***********************************************************************
192 * Generates a FT_Prolog call.
194 * 0FB6D1 movzbl edx,cl
195 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
196 * 68xxxxxxxx push FT_Prolog
197 * C3 lret
199 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
200 LPBYTE x;
202 x = relayCode;
203 *x++ = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
204 *x++ = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
205 x+=4; /* mov edx, [4*edx + targetTable] */
206 *x++ = 0x68; *(void **)x = FT_Prolog;
207 x+=4; /* push FT_Prolog */
208 *x++ = 0xC3; /* lret */
209 /* fill rest with 0xCC / int 3 */
212 /***********************************************************************
213 * _write_qtthunk (internal)
214 * Generates a QT_Thunk style call.
216 * 33C9 xor ecx, ecx
217 * 8A4DFC mov cl , [ebp-04]
218 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
219 * B8yyyyyyyy mov eax, QT_Thunk
220 * FFE0 jmp eax
222 static void _write_qtthunk(
223 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
224 DWORD *targetTable /* [in] start of thunk (for index lookup) */
226 LPBYTE x;
228 x = relayCode;
229 *x++ = 0x33;*x++=0xC9; /* xor ecx,ecx */
230 *x++ = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
231 *x++ = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
232 x+=4; /* mov edx, [4*ecx + targetTable */
233 *x++ = 0xB8; *(void **)x = QT_Thunk;
234 x+=4; /* mov eax , QT_Thunk */
235 *x++ = 0xFF; *x++ = 0xE0; /* jmp eax */
236 /* should fill the rest of the 32 bytes with 0xCC */
239 /***********************************************************************
240 * _loadthunk
242 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
243 struct ThunkDataCommon *TD32, DWORD checksum)
245 struct ThunkDataCommon *TD16;
246 HMODULE16 hmod;
247 int ordinal;
248 static BOOL done;
250 if (!done)
252 LoadLibrary16( "gdi.exe" );
253 LoadLibrary16( "user.exe" );
254 done = TRUE;
257 if ((hmod = LoadLibrary16(module)) <= 32)
259 ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
260 module, func, module32, module, hmod);
261 return 0;
264 if ( !(ordinal = NE_GetOrdinal(hmod, func))
265 || !(TD16 = MapSL((SEGPTR)NE_GetEntryPointEx(hmod, ordinal, FALSE))))
267 ERR("Unable to find thunk data '%s' in %s, required by %s (conflicting/incorrect DLL versions !?).\n",
268 func, module, module32);
269 return 0;
272 if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
274 ERR("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
275 module, func, module32,
276 TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
277 TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
278 return 0;
281 if (TD32 && TD16->checksum != TD32->checksum)
283 ERR("(%s, %s, %s): Wrong checksum %08x (should be %08x)\n",
284 module, func, module32, TD16->checksum, TD32->checksum);
285 return 0;
288 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
290 ERR("(%s, %s, %s): Wrong checksum %08x (should be %08x)\n",
291 module, func, module32, *(LPDWORD)TD16, checksum);
292 return 0;
295 return TD16;
298 /***********************************************************************
299 * GetThunkStuff (KERNEL32.53)
301 LPVOID WINAPI GetThunkStuff(LPCSTR module, LPCSTR func)
303 return _loadthunk(module, func, "<kernel>", NULL, 0L);
306 /***********************************************************************
307 * GetThunkBuff (KERNEL32.52)
308 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
310 LPVOID WINAPI GetThunkBuff(void)
312 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
315 /***********************************************************************
316 * ThunkConnect32 (KERNEL32.@)
317 * Connects a 32bit and a 16bit thunkbuffer.
319 UINT WINAPI ThunkConnect32(
320 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
321 LPSTR thunkfun16, /* [in] win16 thunkfunction */
322 LPSTR module16, /* [in] name of win16 dll */
323 LPSTR module32, /* [in] name of win32 dll */
324 HMODULE hmod32, /* [in] hmodule of win32 dll */
325 DWORD dwReason /* [in] initialisation argument */
327 BOOL directionSL;
329 if (!strncmp(TD->magic, "SL01", 4))
331 directionSL = TRUE;
333 TRACE("SL01 thunk %s (%p) <- %s (%s), Reason: %d\n",
334 module32, TD, module16, thunkfun16, dwReason);
336 else if (!strncmp(TD->magic, "LS01", 4))
338 directionSL = FALSE;
340 TRACE("LS01 thunk %s (%p) -> %s (%s), Reason: %d\n",
341 module32, TD, module16, thunkfun16, dwReason);
343 else
345 ERR("Invalid magic %c%c%c%c\n",
346 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
347 return 0;
350 switch (dwReason)
352 case DLL_PROCESS_ATTACH:
354 struct ThunkDataCommon *TD16;
355 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
356 return 0;
358 if (directionSL)
360 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
361 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
362 struct SLTargetDB *tdb;
364 if (SL16->fpData == NULL)
366 ERR("ThunkConnect16 was not called!\n");
367 return 0;
370 SL32->data = SL16->fpData;
372 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
373 tdb->process = GetCurrentProcessId();
374 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
376 tdb->next = SL32->data->targetDB; /* FIXME: not thread-safe! */
377 SL32->data->targetDB = tdb;
379 TRACE("Process %08x allocated TargetDB entry for ThunkDataSL %p\n",
380 GetCurrentProcessId(), SL32->data);
382 else
384 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
385 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
387 LS32->targetTable = MapSL(LS16->targetTable);
389 /* write QT_Thunk and FT_Prolog stubs */
390 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk, LS32->targetTable);
391 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
393 break;
396 case DLL_PROCESS_DETACH:
397 /* FIXME: cleanup */
398 break;
401 return 1;
404 /**********************************************************************
405 * QT_Thunk (KERNEL32.@)
407 * The target address is in EDX.
408 * The 16bit arguments start at ESP.
409 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
410 * So the stack layout is 16bit argument bytes and then the 64 byte
411 * scratch buffer.
412 * The scratch buffer is used as work space by Windows' QT_Thunk
413 * function.
414 * As the programs unfortunately don't always provide a fixed size
415 * scratch buffer (danger, stack corruption ahead !!), we simply resort
416 * to copying over the whole EBP-ESP range to the 16bit stack
417 * (as there's no way to safely figure out the param count
418 * due to this misbehaviour of some programs).
419 * [ok]
421 * See DDJ article 9614c for a very good description of QT_Thunk (also
422 * available online !).
424 * FIXME: DDJ talks of certain register usage rules; I'm not sure
425 * whether we cover this 100%.
427 void WINAPI __regs_QT_Thunk( CONTEXT *context )
429 CONTEXT context16;
430 DWORD argsize;
432 context16 = *context;
434 context16.SegFs = wine_get_fs();
435 context16.SegGs = wine_get_gs();
436 context16.SegCs = HIWORD(context->Edx);
437 context16.Eip = LOWORD(context->Edx);
438 /* point EBP to the STACK16FRAME on the stack
439 * for the call_to_16 to set up the register content on calling */
440 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
443 * used to be (problematic):
444 * argsize = context->Ebp - context->Esp - 0x40;
445 * due to some programs abusing the API, we better assume the full
446 * EBP - ESP range for copying instead: */
447 argsize = context->Ebp - context->Esp;
449 /* ok, too much is insane; let's limit param count a bit again */
450 if (argsize > 64)
451 argsize = 64; /* 32 WORDs */
453 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
454 context->Eax = context16.Eax;
455 context->Edx = context16.Edx;
456 context->Ecx = context16.Ecx;
458 /* make sure to update the Win32 ESP, too, in order to throw away
459 * the number of parameters that the Win16 function
460 * accepted (that it popped from the corresponding Win16 stack) */
461 context->Esp += LOWORD(context16.Esp) -
462 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
464 DEFINE_REGS_ENTRYPOINT( QT_Thunk )
467 /**********************************************************************
468 * FT_Prolog (KERNEL32.@)
470 * The set of FT_... thunk routines is used instead of QT_Thunk,
471 * if structures have to be converted from 32-bit to 16-bit
472 * (change of member alignment, conversion of members).
474 * The thunk function (as created by the thunk compiler) calls
475 * FT_Prolog at the beginning, to set up a stack frame and
476 * allocate a 64 byte buffer on the stack.
477 * The input parameters (target address and some flags) are
478 * saved for later use by FT_Thunk.
480 * Input: EDX 16-bit target address (SEGPTR)
481 * CX bits 0..7 target number (in target table)
482 * bits 8..9 some flags (unclear???)
483 * bits 10..15 number of DWORD arguments
485 * Output: A new stackframe is created, and a 64 byte buffer
486 * allocated on the stack. The layout of the stack
487 * on return is as follows:
489 * (ebp+4) return address to caller of thunk function
490 * (ebp) old EBP
491 * (ebp-4) saved EBX register of caller
492 * (ebp-8) saved ESI register of caller
493 * (ebp-12) saved EDI register of caller
494 * (ebp-16) saved ECX register, containing flags
495 * (ebp-20) bitmap containing parameters that are to be converted
496 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
497 * filled in by the thunk code before calling FT_Thunk
498 * (ebp-24)
499 * ... (unclear)
500 * (ebp-44)
501 * (ebp-48) saved EAX register of caller (unclear, never restored???)
502 * (ebp-52) saved EDX register, containing 16-bit thunk target
503 * (ebp-56)
504 * ... (unclear)
505 * (ebp-64)
507 * ESP is EBP-64 after return.
510 void WINAPI __regs_FT_Prolog( CONTEXT *context )
512 /* Build stack frame */
513 stack32_push(context, context->Ebp);
514 context->Ebp = context->Esp;
516 /* Allocate 64-byte Thunk Buffer */
517 context->Esp -= 64;
518 memset((char *)context->Esp, '\0', 64);
520 /* Store Flags (ECX) and Target Address (EDX) */
521 /* Save other registers to be restored later */
522 *(DWORD *)(context->Ebp - 4) = context->Ebx;
523 *(DWORD *)(context->Ebp - 8) = context->Esi;
524 *(DWORD *)(context->Ebp - 12) = context->Edi;
525 *(DWORD *)(context->Ebp - 16) = context->Ecx;
527 *(DWORD *)(context->Ebp - 48) = context->Eax;
528 *(DWORD *)(context->Ebp - 52) = context->Edx;
530 DEFINE_REGS_ENTRYPOINT( FT_Prolog )
532 /**********************************************************************
533 * FT_Thunk (KERNEL32.@)
535 * This routine performs the actual call to 16-bit code,
536 * similar to QT_Thunk. The differences are:
537 * - The call target is taken from the buffer created by FT_Prolog
538 * - Those arguments requested by the thunk code (by setting the
539 * corresponding bit in the bitmap at EBP-20) are converted
540 * from 32-bit pointers to segmented pointers (those pointers
541 * are guaranteed to point to structures copied to the stack
542 * by the thunk code, so we always use the 16-bit stack selector
543 * for those addresses).
545 * The bit #i of EBP-20 corresponds here to the DWORD starting at
546 * ESP+4 + 2*i.
548 * FIXME: It is unclear what happens if there are more than 32 WORDs
549 * of arguments, so that the single DWORD bitmap is no longer
550 * sufficient ...
552 void WINAPI __regs_FT_Thunk( CONTEXT *context )
554 DWORD mapESPrelative = *(DWORD *)(context->Ebp - 20);
555 DWORD callTarget = *(DWORD *)(context->Ebp - 52);
557 CONTEXT context16;
558 DWORD i, argsize;
559 DWORD newstack[32];
560 LPBYTE oldstack;
562 context16 = *context;
564 context16.SegFs = wine_get_fs();
565 context16.SegGs = wine_get_gs();
566 context16.SegCs = HIWORD(callTarget);
567 context16.Eip = LOWORD(callTarget);
568 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
570 argsize = context->Ebp-context->Esp-0x40;
571 if (argsize > sizeof(newstack)) argsize = sizeof(newstack);
572 oldstack = (LPBYTE)context->Esp;
574 memcpy( newstack, oldstack, argsize );
576 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
577 if (mapESPrelative & (1 << i))
579 SEGPTR *arg = (SEGPTR *)newstack[i];
580 *arg = MAKESEGPTR(SELECTOROF(NtCurrentTeb()->WOW32Reserved),
581 OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize
582 + (*(LPBYTE *)arg - oldstack));
585 WOWCallback16Ex( 0, WCB16_REGS, argsize, newstack, (DWORD *)&context16 );
586 context->Eax = context16.Eax;
587 context->Edx = context16.Edx;
588 context->Ecx = context16.Ecx;
590 context->Esp += LOWORD(context16.Esp) -
591 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
593 /* Copy modified buffers back to 32-bit stack */
594 memcpy( oldstack, newstack, argsize );
596 DEFINE_REGS_ENTRYPOINT( FT_Thunk )
598 /***********************************************************************
599 * FT_Exit0 (KERNEL32.@)
600 * FT_Exit4 (KERNEL32.@)
601 * FT_Exit8 (KERNEL32.@)
602 * FT_Exit12 (KERNEL32.@)
603 * FT_Exit16 (KERNEL32.@)
604 * FT_Exit20 (KERNEL32.@)
605 * FT_Exit24 (KERNEL32.@)
606 * FT_Exit28 (KERNEL32.@)
607 * FT_Exit32 (KERNEL32.@)
608 * FT_Exit36 (KERNEL32.@)
609 * FT_Exit40 (KERNEL32.@)
610 * FT_Exit44 (KERNEL32.@)
611 * FT_Exit48 (KERNEL32.@)
612 * FT_Exit52 (KERNEL32.@)
613 * FT_Exit56 (KERNEL32.@)
615 * One of the FT_ExitNN functions is called at the end of the thunk code.
616 * It removes the stack frame created by FT_Prolog, moves the function
617 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
618 * value, but the thunk code has moved it from EAX to EBX in the
619 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
620 * and perform a return to the CALLER of the thunk code (while removing
621 * the given number of arguments from the caller's stack).
623 #define FT_EXIT_RESTORE_REGS \
624 "movl %ebx,%eax\n\t" \
625 "movl -4(%ebp),%ebx\n\t" \
626 "movl -8(%ebp),%esi\n\t" \
627 "movl -12(%ebp),%edi\n\t" \
628 "leave\n\t"
630 #define DEFINE_FT_Exit(n) \
631 __ASM_STDCALL_FUNC( FT_Exit ## n, 0, FT_EXIT_RESTORE_REGS "ret $" #n )
633 DEFINE_FT_Exit(0)
634 DEFINE_FT_Exit(4)
635 DEFINE_FT_Exit(8)
636 DEFINE_FT_Exit(12)
637 DEFINE_FT_Exit(16)
638 DEFINE_FT_Exit(20)
639 DEFINE_FT_Exit(24)
640 DEFINE_FT_Exit(28)
641 DEFINE_FT_Exit(32)
642 DEFINE_FT_Exit(36)
643 DEFINE_FT_Exit(40)
644 DEFINE_FT_Exit(44)
645 DEFINE_FT_Exit(48)
646 DEFINE_FT_Exit(52)
647 DEFINE_FT_Exit(56)
650 /***********************************************************************
651 * ThunkInitLS (KERNEL32.43)
652 * A thunkbuffer link routine
653 * The thunkbuf looks like:
655 * 00: DWORD length ? don't know exactly
656 * 04: SEGPTR ptr ? where does it point to?
657 * The pointer ptr is written into the first DWORD of 'thunk'.
658 * (probably correctly implemented)
659 * [ok probably]
660 * RETURNS
661 * segmented pointer to thunk?
663 DWORD WINAPI ThunkInitLS(
664 LPDWORD thunk, /* [in] win32 thunk */
665 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
666 DWORD len, /* [in] thkbuffer length */
667 LPCSTR dll16, /* [in] name of win16 dll */
668 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
670 LPDWORD addr;
672 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
673 return 0;
675 if (!addr[1])
676 return 0;
677 *thunk = addr[1];
679 return addr[1];
682 /***********************************************************************
683 * Common32ThkLS (KERNEL32.45)
685 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
686 * style thunks. The basic difference is that the parameter conversion
687 * is done completely on the *16-bit* side here. Thus we do not call
688 * the 16-bit target directly, but call a common entry point instead.
689 * This entry function then calls the target according to the target
690 * number passed in the DI register.
692 * Input: EAX SEGPTR to the common 16-bit entry point
693 * CX offset in thunk table (target number * 4)
694 * DX error return value if execution fails (unclear???)
695 * EDX.HI number of DWORD parameters
697 * (Note that we need to move the thunk table offset from CX to DI !)
699 * The called 16-bit stub expects its stack to look like this:
700 * ...
701 * (esp+40) 32-bit arguments
702 * ...
703 * (esp+8) 32 byte of stack space available as buffer
704 * (esp) 8 byte return address for use with 0x66 lret
706 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
707 * and uses the EAX register to return a DWORD return value.
708 * Thus we need to use a special assembly glue routine
709 * (CallRegisterLongProc instead of CallRegisterShortProc).
711 * Finally, we return to the caller, popping the arguments off
712 * the stack. The number of arguments to be popped is returned
713 * in the BL register by the called 16-bit routine.
716 void WINAPI __regs_Common32ThkLS( CONTEXT *context )
718 CONTEXT context16;
719 DWORD argsize;
721 context16 = *context;
723 context16.SegFs = wine_get_fs();
724 context16.SegGs = wine_get_gs();
725 context16.Edi = LOWORD(context->Ecx);
726 context16.SegCs = HIWORD(context->Eax);
727 context16.Eip = LOWORD(context->Eax);
728 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
730 argsize = HIWORD(context->Edx) * 4;
732 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
733 if (context->Edx == context->Eip)
734 argsize = 6 * 4;
736 /* Note: the first 32 bytes we copy are just garbage from the 32-bit stack, in order to reserve
737 * the space. It is safe to do that since the register function prefix has reserved
738 * a lot more space than that below context->Esp.
740 WOWCallback16Ex( 0, WCB16_REGS, argsize + 32, (LPBYTE)context->Esp - 32, (DWORD *)&context16 );
741 context->Eax = context16.Eax;
743 /* Clean up caller's stack frame */
744 context->Esp += LOBYTE(context16.Ebx);
746 DEFINE_REGS_ENTRYPOINT( Common32ThkLS )
748 /***********************************************************************
749 * OT_32ThkLSF (KERNEL32.40)
751 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
752 * argument processing is done on both the 32-bit and the 16-bit side:
753 * The 32-bit side prepares arguments, copying them onto the stack.
755 * When this routine is called, the first word on the stack is the
756 * number of argument bytes prepared by the 32-bit code, and EDX
757 * contains the 16-bit target address.
759 * The called 16-bit routine is another relaycode, doing further
760 * argument processing and then calling the real 16-bit target
761 * whose address is stored at [bp-04].
763 * The call proceeds using a normal CallRegisterShortProc.
764 * After return from the 16-bit relaycode, the arguments need
765 * to be copied *back* to the 32-bit stack, since the 32-bit
766 * relaycode processes output parameters.
768 * Note that we copy twice the number of arguments, since some of the
769 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
770 * arguments of the caller!
772 * (Note that this function seems only to be used for
773 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
775 void WINAPI __regs_OT_32ThkLSF( CONTEXT *context )
777 CONTEXT context16;
778 DWORD argsize;
780 context16 = *context;
782 context16.SegFs = wine_get_fs();
783 context16.SegGs = wine_get_gs();
784 context16.SegCs = HIWORD(context->Edx);
785 context16.Eip = LOWORD(context->Edx);
786 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
788 argsize = 2 * *(WORD *)context->Esp + 2;
790 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
791 context->Eax = context16.Eax;
792 context->Edx = context16.Edx;
794 /* Copy modified buffers back to 32-bit stack */
795 memcpy( (LPBYTE)context->Esp,
796 (LPBYTE)CURRENT_STACK16 - argsize, argsize );
798 context->Esp += LOWORD(context16.Esp) -
799 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
801 DEFINE_REGS_ENTRYPOINT( OT_32ThkLSF )
803 /***********************************************************************
804 * ThunkInitLSF (KERNEL32.41)
805 * A thunk setup routine.
806 * Expects a pointer to a preinitialized thunkbuffer in the first argument
807 * looking like:
808 *| 00..03: unknown (pointer, check _41, _43, _46)
809 *| 04: EB1E jmp +0x20
811 *| 06..23: unknown (space for replacement code, check .90)
813 *| 24:>E800000000 call offset 29
814 *| 29:>58 pop eax ( target of call )
815 *| 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
816 *| 2F: BAxxxxxxxx mov edx,xxxxxxxx
817 *| 34: 68yyyyyyyy push KERNEL32.90
818 *| 39: C3 ret
820 *| 3A: EB1E jmp +0x20
821 *| 3E ... 59: unknown (space for replacement code?)
822 *| 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
823 *| 5F: 5A pop edx
824 *| 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
825 *| 66: 52 push edx
826 *| 67: 68xxxxxxxx push xxxxxxxx
827 *| 6C: 68yyyyyyyy push KERNEL32.89
828 *| 71: C3 ret
829 *| 72: end?
830 * This function checks if the code is there, and replaces the yyyyyyyy entries
831 * by the functionpointers.
832 * The thunkbuf looks like:
834 *| 00: DWORD length ? don't know exactly
835 *| 04: SEGPTR ptr ? where does it point to?
836 * The segpointer ptr is written into the first DWORD of 'thunk'.
837 * [ok probably]
838 * RETURNS
839 * unclear, pointer to win16 thkbuffer?
841 LPVOID WINAPI ThunkInitLSF(
842 LPBYTE thunk, /* [in] win32 thunk */
843 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
844 DWORD len, /* [in] length of thkbuffer */
845 LPCSTR dll16, /* [in] name of win16 dll */
846 LPCSTR dll32 /* [in] name of win32 dll */
848 LPDWORD addr,addr2;
850 /* FIXME: add checks for valid code ... */
851 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
852 *(void **)(thunk+0x35) = QT_ThunkPrime;
853 *(void **)(thunk+0x6D) = FT_PrologPrime;
855 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
856 return 0;
858 addr2 = MapSL(addr[1]);
859 if (HIWORD(addr2))
860 *(DWORD*)thunk = (DWORD)addr2;
862 return addr2;
865 /***********************************************************************
866 * FT_PrologPrime (KERNEL32.89)
868 * This function is called from the relay code installed by
869 * ThunkInitLSF. It replaces the location from where it was
870 * called by a standard FT_Prolog call stub (which is 'primed'
871 * by inserting the correct target table pointer).
872 * Finally, it calls that stub.
874 * Input: ECX target number + flags (passed through to FT_Prolog)
875 * (ESP) offset of location where target table pointer
876 * is stored, relative to the start of the relay code
877 * (ESP+4) pointer to start of relay code
878 * (this is where the FT_Prolog call stub gets written to)
880 * Note: The two DWORD arguments get popped off the stack.
883 void WINAPI __regs_FT_PrologPrime( CONTEXT *context )
885 DWORD targetTableOffset;
886 LPBYTE relayCode;
888 /* Compensate for the fact that the Wine register relay code thought
889 we were being called, although we were in fact jumped to */
890 context->Esp -= 4;
892 /* Write FT_Prolog call stub */
893 targetTableOffset = stack32_pop(context);
894 relayCode = (LPBYTE)stack32_pop(context);
895 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
897 /* Jump to the call stub just created */
898 context->Eip = (DWORD)relayCode;
900 DEFINE_REGS_ENTRYPOINT( FT_PrologPrime )
902 /***********************************************************************
903 * QT_ThunkPrime (KERNEL32.90)
905 * This function corresponds to FT_PrologPrime, but installs a
906 * call stub for QT_Thunk instead.
908 * Input: (EBP-4) target number (passed through to QT_Thunk)
909 * EDX target table pointer location offset
910 * EAX start of relay code
913 void WINAPI __regs_QT_ThunkPrime( CONTEXT *context )
915 DWORD targetTableOffset;
916 LPBYTE relayCode;
918 /* Compensate for the fact that the Wine register relay code thought
919 we were being called, although we were in fact jumped to */
920 context->Esp -= 4;
922 /* Write QT_Thunk call stub */
923 targetTableOffset = context->Edx;
924 relayCode = (LPBYTE)context->Eax;
925 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
927 /* Jump to the call stub just created */
928 context->Eip = (DWORD)relayCode;
930 DEFINE_REGS_ENTRYPOINT( QT_ThunkPrime )
932 /***********************************************************************
933 * ThunkInitSL (KERNEL32.46)
934 * Another thunkbuf link routine.
935 * The start of the thunkbuf looks like this:
936 * 00: DWORD length
937 * 04: SEGPTR address for thunkbuffer pointer
938 * [ok probably]
940 * RETURNS
941 * Nothing.
943 VOID WINAPI ThunkInitSL(
944 LPBYTE thunk, /* [in] start of thunkbuffer */
945 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
946 DWORD len, /* [in] length of thunkbuffer */
947 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
948 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
950 LPDWORD addr;
952 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
953 return;
955 *(DWORD*)MapSL(addr[1]) = (DWORD)thunk;
958 /**********************************************************************
959 * SSInit (KERNEL.700)
960 * RETURNS
961 * TRUE for success.
963 BOOL WINAPI SSInit16(void)
965 return TRUE;
968 /**********************************************************************
969 * SSOnBigStack (KERNEL32.87)
970 * Check if thunking is initialized (ss selector set up etc.)
971 * We do that differently, so just return TRUE.
972 * [ok]
973 * RETURNS
974 * TRUE for success.
976 BOOL WINAPI SSOnBigStack(void)
978 TRACE("Yes, thunking is initialized\n");
979 return TRUE;
982 /**********************************************************************
983 * SSConfirmSmallStack (KERNEL.704)
985 * Abort if not on small stack.
987 * This must be a register routine as it has to preserve *all* registers.
989 void WINAPI SSConfirmSmallStack( CONTEXT *context )
991 /* We are always on the small stack while in 16-bit code ... */
994 /**********************************************************************
995 * SSCall (KERNEL32.88)
996 * One of the real thunking functions. This one seems to be for 32<->32
997 * thunks. It should probably be capable of crossing processboundaries.
999 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
1000 * [ok]
1002 * RETURNS
1003 * Thunked function result.
1005 DWORD WINAPIV SSCall(
1006 DWORD nr, /* [in] number of argument bytes */
1007 DWORD flags, /* [in] FIXME: flags ? */
1008 FARPROC fun, /* [in] function to call */
1009 ... /* [in/out] arguments */
1011 DWORD i,ret;
1012 DWORD *args = ((DWORD *)&fun) + 1;
1014 if(TRACE_ON(thunk))
1016 DPRINTF("(%d,0x%08x,%p,[",nr,flags,fun);
1017 for (i=0;i<nr/4;i++)
1018 DPRINTF("0x%08x,",args[i]);
1019 DPRINTF("])\n");
1021 ret = call_entry_point( fun, nr / sizeof(DWORD), args );
1022 TRACE(" returning %d ...\n",ret);
1023 return ret;
1026 /**********************************************************************
1027 * W32S_BackTo32 (KERNEL32.51)
1029 void WINAPI __regs_W32S_BackTo32( CONTEXT *context )
1031 LPDWORD stack = (LPDWORD)context->Esp;
1032 FARPROC proc = (FARPROC)context->Eip;
1034 context->Eax = call_entry_point( proc, 10, stack + 1 );
1035 context->Eip = stack32_pop(context);
1037 DEFINE_REGS_ENTRYPOINT( W32S_BackTo32 )
1039 /**********************************************************************
1040 * AllocSLCallback (KERNEL32.@)
1042 * Allocate a 16->32 callback.
1044 * NOTES
1045 * Win95 uses some structchains for callbacks. It allocates them
1046 * in blocks of 100 entries, size 32 bytes each, layout:
1047 * blockstart:
1048 *| 0: PTR nextblockstart
1049 *| 4: entry *first;
1050 *| 8: WORD sel ( start points to blockstart)
1051 *| A: WORD unknown
1052 * 100xentry:
1053 *| 00..17: Code
1054 *| 18: PDB *owning_process;
1055 *| 1C: PTR blockstart
1057 * We ignore this for now. (Just a note for further developers)
1058 * FIXME: use this method, so we don't waste selectors...
1060 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
1061 * the 0x66 prefix switches from word->long registers.
1063 *| 665A pop edx
1064 *| 6668x arg2 x pushl <arg2>
1065 *| 6652 push edx
1066 *| EAx arg1 x jmpf <arg1>
1068 * returns the startaddress of this thunk.
1070 * Note, that they look very similar to the ones allocates by THUNK_Alloc.
1071 * RETURNS
1072 * A segmented pointer to the start of the thunk
1074 DWORD WINAPI
1075 AllocSLCallback(
1076 DWORD finalizer, /* [in] Finalizer function */
1077 DWORD callback /* [in] Callback function */
1079 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
1080 WORD sel;
1082 x=thunk;
1083 *x++=0x66;*x++=0x5a; /* popl edx */
1084 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
1085 *x++=0x66;*x++=0x52; /* pushl edx */
1086 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
1088 *(DWORD*)(thunk+18) = GetCurrentProcessId();
1090 sel = SELECTOR_AllocBlock( thunk, 32, WINE_LDT_FLAGS_CODE );
1091 return (sel<<16)|0;
1094 /**********************************************************************
1095 * FreeSLCallback (KERNEL32.@)
1096 * Frees the specified 16->32 callback
1098 * RETURNS
1099 * Nothing.
1101 void WINAPI
1102 FreeSLCallback(
1103 DWORD x /* [in] 16 bit callback (segmented pointer?) */
1105 FIXME("(0x%08x): stub\n",x);
1108 /**********************************************************************
1109 * AllocMappedBuffer (KERNEL32.38)
1111 * This is an undocumented KERNEL32 function that
1112 * SMapLS's a GlobalAlloc'ed buffer.
1114 * RETURNS
1115 * EDI register: pointer to buffer
1117 * NOTES
1118 * The buffer is preceded by 8 bytes:
1119 * ...
1120 * edi+0 buffer
1121 * edi-4 SEGPTR to buffer
1122 * edi-8 some magic Win95 needs for SUnMapLS
1123 * (we use it for the memory handle)
1125 * The SEGPTR is used by the caller!
1127 void WINAPI __regs_AllocMappedBuffer(
1128 CONTEXT *context /* [in] EDI register: size of buffer to allocate */
1130 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
1131 DWORD *buffer = GlobalLock(handle);
1132 DWORD ptr = 0;
1134 if (buffer)
1135 if (!(ptr = MapLS(buffer + 2)))
1137 GlobalUnlock(handle);
1138 GlobalFree(handle);
1141 if (!ptr)
1142 context->Eax = context->Edi = 0;
1143 else
1145 buffer[0] = (DWORD)handle;
1146 buffer[1] = ptr;
1148 context->Eax = ptr;
1149 context->Edi = (DWORD)(buffer + 2);
1152 DEFINE_REGS_ENTRYPOINT( AllocMappedBuffer )
1154 /**********************************************************************
1155 * FreeMappedBuffer (KERNEL32.39)
1157 * Free a buffer allocated by AllocMappedBuffer
1159 * RETURNS
1160 * Nothing.
1162 void WINAPI __regs_FreeMappedBuffer(
1163 CONTEXT *context /* [in] EDI register: pointer to buffer */
1165 if (context->Edi)
1167 DWORD *buffer = (DWORD *)context->Edi - 2;
1169 UnMapLS(buffer[1]);
1171 GlobalUnlock((HGLOBAL)buffer[0]);
1172 GlobalFree((HGLOBAL)buffer[0]);
1175 DEFINE_REGS_ENTRYPOINT( FreeMappedBuffer )
1177 /**********************************************************************
1178 * GetTEBSelectorFS (KERNEL.475)
1179 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1181 void WINAPI GetTEBSelectorFS16(void)
1183 CURRENT_STACK16->fs = wine_get_fs();
1186 /**********************************************************************
1187 * IsPeFormat (KERNEL.431)
1189 * Determine if a file is a PE format executable.
1191 * RETURNS
1192 * TRUE, if it is.
1193 * FALSE if the file could not be opened or is not a PE file.
1195 * NOTES
1196 * If fn is given as NULL then the function expects hf16 to be valid.
1198 BOOL16 WINAPI IsPeFormat16(
1199 LPSTR fn, /* [in] Filename to the executable */
1200 HFILE16 hf16) /* [in] An open file handle */
1202 BOOL ret = FALSE;
1203 IMAGE_DOS_HEADER mzh;
1204 OFSTRUCT ofs;
1205 DWORD xmagic;
1207 if (fn) hf16 = OpenFile16(fn,&ofs,OF_READ);
1208 if (hf16 == HFILE_ERROR16) return FALSE;
1209 _llseek16(hf16,0,SEEK_SET);
1210 if (sizeof(mzh)!=_lread16(hf16,&mzh,sizeof(mzh))) goto done;
1211 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) goto done;
1212 _llseek16(hf16,mzh.e_lfanew,SEEK_SET);
1213 if (sizeof(DWORD)!=_lread16(hf16,&xmagic,sizeof(DWORD))) goto done;
1214 ret = (xmagic == IMAGE_NT_SIGNATURE);
1215 done:
1216 _lclose16(hf16);
1217 return ret;
1221 /***********************************************************************
1222 * K32Thk1632Prolog (KERNEL32.@)
1224 void WINAPI __regs_K32Thk1632Prolog( CONTEXT *context )
1226 LPBYTE code = (LPBYTE)context->Eip - 5;
1228 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1229 of 16->32 thunks instead of using one of the standard methods!
1230 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1231 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1232 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1233 bypassed, which means it will crash the next time the 32-bit OLE
1234 code thunks down again to 16-bit (this *will* happen!).
1236 The following hack tries to recognize this situation.
1237 This is possible since the called stubs in OLECLI32/OLESVR32 all
1238 look exactly the same:
1239 00 E8xxxxxxxx call K32Thk1632Prolog
1240 05 FF55FC call [ebp-04]
1241 08 E8xxxxxxxx call K32Thk1632Epilog
1242 0D 66CB retf
1244 If we recognize this situation, we try to simulate the actions
1245 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1246 to our 32-bit stack, creating a proper STACK16FRAME and
1247 updating cur_stack. */
1249 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1250 && code[13] == 0x66 && code[14] == 0xCB)
1252 DWORD argSize = context->Ebp - context->Esp;
1253 char *stack16 = (char *)context->Esp - 4;
1254 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1255 STACK32FRAME *frame32 = NtCurrentTeb()->WOW32Reserved;
1256 char *stack32 = (char *)frame32 - argSize;
1257 WORD stackSel = SELECTOROF(frame32->frame16);
1258 DWORD stackBase = GetSelectorBase(stackSel);
1260 TRACE("before SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1261 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1263 memset(frame16, '\0', sizeof(STACK16FRAME));
1264 frame16->frame32 = frame32;
1265 frame16->ebp = context->Ebp;
1267 memcpy(stack32, stack16, argSize);
1268 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR(stackSel, (DWORD)frame16 - stackBase);
1270 context->Esp = (DWORD)stack32 + 4;
1271 context->Ebp = context->Esp + argSize;
1273 TRACE("after SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1274 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1277 /* entry_point is never used again once the entry point has
1278 been called. Thus we re-use it to hold the Win16Lock count */
1279 ReleaseThunkLock(&CURRENT_STACK16->entry_point);
1281 DEFINE_REGS_ENTRYPOINT( K32Thk1632Prolog )
1283 /***********************************************************************
1284 * K32Thk1632Epilog (KERNEL32.@)
1286 void WINAPI __regs_K32Thk1632Epilog( CONTEXT *context )
1288 LPBYTE code = (LPBYTE)context->Eip - 13;
1290 RestoreThunkLock(CURRENT_STACK16->entry_point);
1292 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1294 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1295 && code[13] == 0x66 && code[14] == 0xCB)
1297 STACK16FRAME *frame16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1298 char *stack16 = (char *)(frame16 + 1);
1299 DWORD argSize = frame16->ebp - (DWORD)stack16;
1300 char *stack32 = (char *)frame16->frame32 - argSize;
1302 DWORD nArgsPopped = context->Esp - (DWORD)stack32;
1304 TRACE("before SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1305 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1307 NtCurrentTeb()->WOW32Reserved = frame16->frame32;
1309 context->Esp = (DWORD)stack16 + nArgsPopped;
1310 context->Ebp = frame16->ebp;
1312 TRACE("after SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1313 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1316 DEFINE_REGS_ENTRYPOINT( K32Thk1632Epilog )
1318 /*********************************************************************
1319 * PK16FNF [KERNEL32.91]
1321 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1322 * string buffer with the 8.3 filename of a recently loaded 16-bit
1323 * module. It is unknown exactly what modules trigger this
1324 * mechanism or what purpose this serves. Win98 Explorer (and
1325 * probably also Win95 with IE 4 shell integration) calls this
1326 * several times during initialization.
1328 * FIXME: find out what this really does and make it work.
1330 void WINAPI PK16FNF(LPSTR strPtr)
1332 FIXME("(%p): stub\n", strPtr);
1334 /* fill in a fake filename that'll be easy to recognize */
1335 strcpy(strPtr, "WINESTUB.FIX");
1338 /***********************************************************************
1339 * 16->32 Flat Thunk routines:
1342 /***********************************************************************
1343 * ThunkConnect16 (KERNEL.651)
1344 * Connects a 32bit and a 16bit thunkbuffer.
1346 UINT WINAPI ThunkConnect16(
1347 LPSTR module16, /* [in] name of win16 dll */
1348 LPSTR module32, /* [in] name of win32 dll */
1349 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
1350 DWORD dwReason, /* [in] initialisation argument */
1351 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
1352 LPSTR thunkfun32, /* [in] win32 thunkfunction */
1353 WORD cs /* [in] CS of win16 dll */
1355 BOOL directionSL;
1357 if (!strncmp(TD->magic, "SL01", 4))
1359 directionSL = TRUE;
1361 TRACE("SL01 thunk %s (%p) -> %s (%s), Reason: %d\n",
1362 module16, TD, module32, thunkfun32, dwReason);
1364 else if (!strncmp(TD->magic, "LS01", 4))
1366 directionSL = FALSE;
1368 TRACE("LS01 thunk %s (%p) <- %s (%s), Reason: %d\n",
1369 module16, TD, module32, thunkfun32, dwReason);
1371 else
1373 ERR("Invalid magic %c%c%c%c\n",
1374 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
1375 return 0;
1378 switch (dwReason)
1380 case DLL_PROCESS_ATTACH:
1381 if (directionSL)
1383 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
1384 struct ThunkDataSL *SL = SL16->fpData;
1386 if (SL == NULL)
1388 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
1390 SL->common = SL16->common;
1391 SL->flags1 = SL16->flags1;
1392 SL->flags2 = SL16->flags2;
1394 SL->apiDB = MapSL(SL16->apiDatabase);
1395 SL->targetDB = NULL;
1397 lstrcpynA(SL->pszDll16, module16, 255);
1398 lstrcpynA(SL->pszDll32, module32, 255);
1400 /* We should create a SEGPTR to the ThunkDataSL,
1401 but since the contents are not in the original format,
1402 any access to this by 16-bit code would crash anyway. */
1403 SL16->spData = 0;
1404 SL16->fpData = SL;
1408 if (SL->flags2 & 0x80000000)
1410 TRACE("Preloading 32-bit library\n");
1411 LoadLibraryA(module32);
1414 else
1416 /* nothing to do */
1418 break;
1420 case DLL_PROCESS_DETACH:
1421 /* FIXME: cleanup */
1422 break;
1425 return 1;
1429 /***********************************************************************
1430 * C16ThkSL (KERNEL.630)
1433 void WINAPI C16ThkSL(CONTEXT *context)
1435 LPBYTE stub = MapSL(context->Eax), x = stub;
1436 WORD cs = wine_get_cs();
1437 WORD ds = wine_get_ds();
1439 /* We produce the following code:
1441 * mov ax, __FLATDS
1442 * mov es, ax
1443 * movzx ecx, cx
1444 * mov edx, es:[ecx + $EDX]
1445 * push bp
1446 * push edx
1447 * push dx
1448 * push edx
1449 * call __FLATCS:__wine_call_from_16_thunk
1452 *x++ = 0xB8; *(WORD *)x = ds; x += sizeof(WORD);
1453 *x++ = 0x8E; *x++ = 0xC0;
1454 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1455 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1456 *x++ = 0x91; *(DWORD *)x = context->Edx; x += sizeof(DWORD);
1458 *x++ = 0x55;
1459 *x++ = 0x66; *x++ = 0x52;
1460 *x++ = 0x52;
1461 *x++ = 0x66; *x++ = 0x52;
1462 *x++ = 0x66; *x++ = 0x9A;
1463 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1464 *(WORD *)x = cs; x += sizeof(WORD);
1466 /* Jump to the stub code just created */
1467 context->Eip = LOWORD(context->Eax);
1468 context->SegCs = HIWORD(context->Eax);
1470 /* Since C16ThkSL got called by a jmp, we need to leave the
1471 original return address on the stack */
1472 context->Esp -= 4;
1475 /***********************************************************************
1476 * C16ThkSL01 (KERNEL.631)
1479 void WINAPI C16ThkSL01(CONTEXT *context)
1481 LPBYTE stub = MapSL(context->Eax), x = stub;
1483 if (stub)
1485 struct ThunkDataSL16 *SL16 = MapSL(context->Edx);
1486 struct ThunkDataSL *td = SL16->fpData;
1488 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), (LPCSTR)631);
1489 WORD cs = wine_get_cs();
1491 if (!td)
1493 ERR("ThunkConnect16 was not called!\n");
1494 return;
1497 TRACE("Creating stub for ThunkDataSL %p\n", td);
1500 /* We produce the following code:
1502 * xor eax, eax
1503 * mov edx, $td
1504 * call C16ThkSL01
1505 * push bp
1506 * push edx
1507 * push dx
1508 * push edx
1509 * call __FLATCS:__wine_call_from_16_thunk
1512 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1513 *x++ = 0x66; *x++ = 0xBA; *(void **)x = td; x += sizeof(void *);
1514 *x++ = 0x9A; *(DWORD *)x = procAddress; x += sizeof(DWORD);
1516 *x++ = 0x55;
1517 *x++ = 0x66; *x++ = 0x52;
1518 *x++ = 0x52;
1519 *x++ = 0x66; *x++ = 0x52;
1520 *x++ = 0x66; *x++ = 0x9A;
1521 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1522 *(WORD *)x = cs; x += sizeof(WORD);
1524 /* Jump to the stub code just created */
1525 context->Eip = LOWORD(context->Eax);
1526 context->SegCs = HIWORD(context->Eax);
1528 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1529 original return address on the stack */
1530 context->Esp -= 4;
1532 else
1534 struct ThunkDataSL *td = (struct ThunkDataSL *)context->Edx;
1535 DWORD targetNr = LOWORD(context->Ecx) / 4;
1536 struct SLTargetDB *tdb;
1538 TRACE("Process %08x calling target %d of ThunkDataSL %p\n",
1539 GetCurrentProcessId(), targetNr, td);
1541 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1542 if (tdb->process == GetCurrentProcessId())
1543 break;
1545 if (!tdb)
1547 TRACE("Loading 32-bit library %s\n", td->pszDll32);
1548 LoadLibraryA(td->pszDll32);
1550 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1551 if (tdb->process == GetCurrentProcessId())
1552 break;
1555 if (tdb)
1557 context->Edx = tdb->targetTable[targetNr];
1559 TRACE("Call target is %08x\n", context->Edx);
1561 else
1563 WORD *stack = MapSL( MAKESEGPTR(context->SegSs, LOWORD(context->Esp)) );
1564 context->Edx = (context->Edx & ~0xffff) | HIWORD(td->apiDB[targetNr].errorReturnValue);
1565 context->Eax = (context->Eax & ~0xffff) | LOWORD(td->apiDB[targetNr].errorReturnValue);
1566 context->Eip = stack[2];
1567 context->SegCs = stack[3];
1568 context->Esp += td->apiDB[targetNr].nrArgBytes + 4;
1570 ERR("Process %08x did not ThunkConnect32 %s to %s\n",
1571 GetCurrentProcessId(), td->pszDll32, td->pszDll16);
1577 /***********************************************************************
1578 * 16<->32 Thunklet/Callback API:
1581 #include "pshpack1.h"
1582 typedef struct _THUNKLET
1584 BYTE prefix_target;
1585 BYTE pushl_target;
1586 DWORD target;
1588 BYTE prefix_relay;
1589 BYTE pushl_relay;
1590 DWORD relay;
1592 BYTE jmp_glue;
1593 DWORD glue;
1595 BYTE type;
1596 HINSTANCE16 owner;
1597 struct _THUNKLET *next;
1598 } THUNKLET;
1599 #include "poppack.h"
1601 #define THUNKLET_TYPE_LS 1
1602 #define THUNKLET_TYPE_SL 2
1604 static HANDLE ThunkletHeap = 0;
1605 static WORD ThunkletCodeSel;
1606 static THUNKLET *ThunkletAnchor = NULL;
1608 static FARPROC ThunkletSysthunkGlueLS = 0;
1609 static SEGPTR ThunkletSysthunkGlueSL = 0;
1611 static FARPROC ThunkletCallbackGlueLS = 0;
1612 static SEGPTR ThunkletCallbackGlueSL = 0;
1615 /* map a thunk allocated on ThunkletHeap to a 16-bit pointer */
1616 static inline SEGPTR get_segptr( void *thunk )
1618 if (!thunk) return 0;
1619 return MAKESEGPTR( ThunkletCodeSel, (char *)thunk - (char *)ThunkletHeap );
1622 /***********************************************************************
1623 * THUNK_Init
1625 static BOOL THUNK_Init(void)
1627 LPBYTE thunk;
1629 ThunkletHeap = HeapCreate( HEAP_CREATE_ENABLE_EXECUTE, 0x10000, 0x10000 );
1630 if (!ThunkletHeap) return FALSE;
1632 ThunkletCodeSel = SELECTOR_AllocBlock( ThunkletHeap, 0x10000, WINE_LDT_FLAGS_CODE );
1634 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1635 if (!thunk) return FALSE;
1637 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1638 *thunk++ = 0x58; /* popl eax */
1639 *thunk++ = 0xC3; /* ret */
1641 ThunkletSysthunkGlueSL = get_segptr( thunk );
1642 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1643 *thunk++ = 0xCB; /* lret */
1645 return TRUE;
1648 /***********************************************************************
1649 * SetThunkletCallbackGlue (KERNEL.560)
1651 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1653 ThunkletCallbackGlueLS = glueLS;
1654 ThunkletCallbackGlueSL = glueSL;
1658 /***********************************************************************
1659 * THUNK_FindThunklet
1661 static THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1662 DWORD glue, BYTE type )
1664 THUNKLET *thunk;
1666 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1667 if ( thunk->type == type
1668 && thunk->target == target
1669 && thunk->relay == relay
1670 && ( type == THUNKLET_TYPE_LS ?
1671 ( thunk->glue == glue - (DWORD)&thunk->type )
1672 : ( thunk->glue == glue ) ) )
1673 return thunk;
1675 return NULL;
1678 /***********************************************************************
1679 * THUNK_AllocLSThunklet
1681 static FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1682 FARPROC glue, HTASK16 owner )
1684 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1685 THUNKLET_TYPE_LS );
1686 if (!thunk)
1688 TDB *pTask = GlobalLock16( owner );
1690 if (!ThunkletHeap) THUNK_Init();
1691 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1692 return 0;
1694 thunk->prefix_target = thunk->prefix_relay = 0x90;
1695 thunk->pushl_target = thunk->pushl_relay = 0x68;
1696 thunk->jmp_glue = 0xE9;
1698 thunk->target = (DWORD)target;
1699 thunk->relay = relay;
1700 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1702 thunk->type = THUNKLET_TYPE_LS;
1703 thunk->owner = pTask? pTask->hInstance : 0;
1705 thunk->next = ThunkletAnchor;
1706 ThunkletAnchor = thunk;
1709 return (FARPROC)thunk;
1712 /***********************************************************************
1713 * THUNK_AllocSLThunklet
1715 static SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1716 SEGPTR glue, HTASK16 owner )
1718 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1719 THUNKLET_TYPE_SL );
1720 if (!thunk)
1722 TDB *pTask = GlobalLock16( owner );
1724 if (!ThunkletHeap) THUNK_Init();
1725 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1726 return 0;
1728 thunk->prefix_target = thunk->prefix_relay = 0x66;
1729 thunk->pushl_target = thunk->pushl_relay = 0x68;
1730 thunk->jmp_glue = 0xEA;
1732 thunk->target = (DWORD)target;
1733 thunk->relay = relay;
1734 thunk->glue = (DWORD)glue;
1736 thunk->type = THUNKLET_TYPE_SL;
1737 thunk->owner = pTask? pTask->hInstance : 0;
1739 thunk->next = ThunkletAnchor;
1740 ThunkletAnchor = thunk;
1743 return get_segptr( thunk );
1746 /**********************************************************************
1747 * IsLSThunklet
1749 static BOOL16 IsLSThunklet( THUNKLET *thunk )
1751 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1752 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1753 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1756 /**********************************************************************
1757 * IsSLThunklet (KERNEL.612)
1759 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1761 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1762 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1763 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1768 /***********************************************************************
1769 * AllocLSThunkletSysthunk (KERNEL.607)
1771 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1772 FARPROC relay, DWORD dummy )
1774 if (!ThunkletSysthunkGlueLS) THUNK_Init();
1775 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1776 ThunkletSysthunkGlueLS, GetCurrentTask() );
1779 /***********************************************************************
1780 * AllocSLThunkletSysthunk (KERNEL.608)
1782 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1783 SEGPTR relay, DWORD dummy )
1785 if (!ThunkletSysthunkGlueSL) THUNK_Init();
1786 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1787 ThunkletSysthunkGlueSL, GetCurrentTask() );
1791 /***********************************************************************
1792 * AllocLSThunkletCallbackEx (KERNEL.567)
1794 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1795 DWORD relay, HTASK16 task )
1797 THUNKLET *thunk = MapSL( target );
1798 if ( !thunk ) return NULL;
1800 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1801 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1802 return (FARPROC)thunk->target;
1804 return THUNK_AllocLSThunklet( target, relay,
1805 ThunkletCallbackGlueLS, task );
1808 /***********************************************************************
1809 * AllocSLThunkletCallbackEx (KERNEL.568)
1811 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1812 DWORD relay, HTASK16 task )
1814 THUNKLET *thunk = (THUNKLET *)target;
1815 if ( !thunk ) return 0;
1817 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1818 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1819 return (SEGPTR)thunk->target;
1821 return THUNK_AllocSLThunklet( target, relay,
1822 ThunkletCallbackGlueSL, task );
1825 /***********************************************************************
1826 * AllocLSThunkletCallback (KERNEL.561)
1827 * AllocLSThunkletCallback_dup (KERNEL.606)
1829 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1831 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1834 /***********************************************************************
1835 * AllocSLThunkletCallback (KERNEL.562)
1836 * AllocSLThunkletCallback_dup (KERNEL.605)
1838 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1840 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1843 /***********************************************************************
1844 * FindLSThunkletCallback (KERNEL.563)
1845 * FindLSThunkletCallback_dup (KERNEL.609)
1847 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1849 THUNKLET *thunk = MapSL( target );
1850 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1851 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1852 return (FARPROC)thunk->target;
1854 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1855 (DWORD)ThunkletCallbackGlueLS,
1856 THUNKLET_TYPE_LS );
1857 return (FARPROC)thunk;
1860 /***********************************************************************
1861 * FindSLThunkletCallback (KERNEL.564)
1862 * FindSLThunkletCallback_dup (KERNEL.610)
1864 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1866 THUNKLET *thunk = (THUNKLET *)target;
1867 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1868 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1869 return (SEGPTR)thunk->target;
1871 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1872 (DWORD)ThunkletCallbackGlueSL,
1873 THUNKLET_TYPE_SL );
1874 return get_segptr( thunk );
1878 /***********************************************************************
1879 * FreeThunklet (KERNEL.611)
1881 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1883 return FALSE;
1887 /***********************************************************************
1888 * Callback Client API
1891 #define N_CBC_FIXED 20
1892 #define N_CBC_VARIABLE 10
1893 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1895 static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
1896 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1898 /***********************************************************************
1899 * RegisterCBClient (KERNEL.619)
1901 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1902 SEGPTR relay16, FARPROC *relay32 )
1904 /* Search for free Callback ID */
1905 if ( wCBCId == -1 )
1906 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1907 if ( !CBClientRelay16[ wCBCId ] )
1908 break;
1910 /* Register Callback ID */
1911 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1913 CBClientRelay16[ wCBCId ] = relay16;
1914 CBClientRelay32[ wCBCId ] = relay32;
1916 else
1917 wCBCId = 0;
1919 return wCBCId;
1922 /***********************************************************************
1923 * UnRegisterCBClient (KERNEL.622)
1925 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1926 SEGPTR relay16, FARPROC *relay32 )
1928 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1929 && CBClientRelay16[ wCBCId ] == relay16
1930 && CBClientRelay32[ wCBCId ] == relay32 )
1932 CBClientRelay16[ wCBCId ] = 0;
1933 CBClientRelay32[ wCBCId ] = 0;
1935 else
1936 wCBCId = 0;
1938 return wCBCId;
1942 /***********************************************************************
1943 * InitCBClient (KERNEL.623)
1945 void WINAPI InitCBClient16( FARPROC glueLS )
1947 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1948 SEGPTR glueSL = (SEGPTR)GetProcAddress16( kernel, (LPCSTR)604 );
1950 SetThunkletCallbackGlue16( glueLS, glueSL );
1953 /***********************************************************************
1954 * CBClientGlueSL (KERNEL.604)
1956 void WINAPI CBClientGlueSL( CONTEXT *context )
1958 /* Create stack frame */
1959 SEGPTR stackSeg = stack16_push( 12 );
1960 LPWORD stackLin = MapSL( stackSeg );
1961 SEGPTR glue, *glueTab;
1963 stackLin[3] = (WORD)context->Ebp;
1964 stackLin[2] = (WORD)context->Esi;
1965 stackLin[1] = (WORD)context->Edi;
1966 stackLin[0] = (WORD)context->SegDs;
1968 context->Ebp = OFFSETOF( stackSeg ) + 6;
1969 context->Esp = OFFSETOF( stackSeg ) - 4;
1970 context->SegGs = 0;
1972 /* Jump to 16-bit relay code */
1973 glueTab = MapSL( CBClientRelay16[ stackLin[5] ] );
1974 glue = glueTab[ stackLin[4] ];
1975 context->SegCs = SELECTOROF( glue );
1976 context->Eip = OFFSETOF ( glue );
1979 /***********************************************************************
1980 * CBClientThunkSL (KERNEL.620)
1982 extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, WORD *stackLin, DWORD *esi );
1983 void WINAPI CBClientThunkSL( CONTEXT *context )
1985 /* Call 32-bit relay code */
1987 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
1988 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1990 /* fill temporary area for the asm code (see comments in winebuild) */
1991 SEGPTR stack = stack16_push( 12 );
1992 LPWORD stackLin = MapSL(stack);
1993 /* stackLin[0] and stackLin[1] reserved for the 32-bit stack ptr */
1994 stackLin[2] = wine_get_ss();
1995 stackLin[3] = 0;
1996 stackLin[4] = OFFSETOF(stack) + 12;
1997 stackLin[5] = SELECTOROF(stack);
1998 stackLin[6] = OFFSETOF(CALL32_CBClientEx_RetAddr); /* overwrite return address */
1999 stackLin[7] = SELECTOROF(CALL32_CBClientEx_RetAddr);
2000 context->Eax = CALL32_CBClient( proc, args, stackLin + 4, &context->Esi );
2001 stack16_pop( 12 );
2004 /***********************************************************************
2005 * CBClientThunkSLEx (KERNEL.621)
2007 extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, WORD *stackLin, DWORD *esi, INT *nArgs );
2008 void WINAPI CBClientThunkSLEx( CONTEXT *context )
2010 /* Call 32-bit relay code */
2012 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2013 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
2014 INT nArgs;
2015 LPWORD stackLin;
2017 /* fill temporary area for the asm code (see comments in winebuild) */
2018 SEGPTR stack = stack16_push( 24 );
2019 stackLin = MapSL(stack);
2020 stackLin[0] = OFFSETOF(stack) + 4;
2021 stackLin[1] = SELECTOROF(stack);
2022 stackLin[2] = wine_get_ds();
2023 stackLin[5] = OFFSETOF(stack) + 24;
2024 /* stackLin[6] and stackLin[7] reserved for the 32-bit stack ptr */
2025 stackLin[8] = wine_get_ss();
2026 stackLin[9] = 0;
2027 stackLin[10] = OFFSETOF(CALL32_CBClientEx_RetAddr);
2028 stackLin[11] = SELECTOROF(CALL32_CBClientEx_RetAddr);
2030 context->Eax = CALL32_CBClientEx( proc, args, stackLin, &context->Esi, &nArgs );
2031 stack16_pop( 24 );
2033 /* Restore registers saved by CBClientGlueSL */
2034 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
2035 context->Ebp = (context->Ebp & ~0xffff) | stackLin[3];
2036 context->Esi = (context->Esi & ~0xffff) | stackLin[2];
2037 context->Edi = (context->Edi & ~0xffff) | stackLin[1];
2038 context->SegDs = stackLin[0];
2039 context->Esp += 16+nArgs;
2041 /* Return to caller of CBClient thunklet */
2042 context->SegCs = stackLin[9];
2043 context->Eip = stackLin[8];
2047 /***********************************************************************
2048 * Get16DLLAddress (KERNEL32.@)
2050 * This function is used by a Win32s DLL if it wants to call a Win16 function.
2051 * A 16:16 segmented pointer to the function is returned.
2052 * Written without any docu.
2054 SEGPTR WINAPI Get16DLLAddress(HMODULE16 handle, LPSTR func_name)
2056 static WORD code_sel32;
2057 FARPROC16 proc_16;
2058 LPBYTE thunk;
2060 if (!code_sel32)
2062 if (!ThunkletHeap) THUNK_Init();
2063 code_sel32 = SELECTOR_AllocBlock( ThunkletHeap, 0x10000,
2064 WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
2065 if (!code_sel32) return 0;
2067 if (!(thunk = HeapAlloc( ThunkletHeap, 0, 32 ))) return 0;
2069 if (!handle) handle = GetModuleHandle16("WIN32S16");
2070 proc_16 = GetProcAddress16(handle, func_name);
2072 /* movl proc_16, $edx */
2073 *thunk++ = 0xba;
2074 *(FARPROC16 *)thunk = proc_16;
2075 thunk += sizeof(FARPROC16);
2077 /* jmpl QT_Thunk */
2078 *thunk++ = 0xea;
2079 *(void **)thunk = QT_Thunk;
2080 thunk += sizeof(FARPROC16);
2081 *(WORD *)thunk = wine_get_cs();
2083 return MAKESEGPTR( code_sel32, (char *)thunk - (char *)ThunkletHeap );
2087 /***********************************************************************
2088 * GetWin16DOSEnv (KERNEL32.34)
2089 * Returns some internal value.... probably the default environment database?
2091 DWORD WINAPI GetWin16DOSEnv(void)
2093 FIXME("stub, returning 0\n");
2094 return 0;
2097 /**********************************************************************
2098 * GetPK16SysVar (KERNEL32.92)
2100 LPVOID WINAPI GetPK16SysVar(void)
2102 static BYTE PK16SysVar[128];
2104 FIXME("()\n");
2105 return PK16SysVar;
2108 /**********************************************************************
2109 * CommonUnimpStub (KERNEL32.17)
2111 int WINAPI DECLSPEC_HIDDEN __regs_CommonUnimpStub( const char *name, int type )
2113 FIXME("generic stub %s\n", debugstr_a(name));
2115 switch (type)
2117 case 15: return -1;
2118 case 14: return ERROR_CALL_NOT_IMPLEMENTED;
2119 case 13: return ERROR_NOT_SUPPORTED;
2120 case 1: return 1;
2121 default: return 0;
2124 __ASM_STDCALL_FUNC( CommonUnimpStub, 0,
2125 "pushl %ecx\n\t"
2126 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2127 "shrl $4,%ecx\n\t"
2128 "andl $0xf,%ecx\n\t"
2129 "pushl %ecx\n\t"
2130 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2131 "pushl %eax\n\t"
2132 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2133 "call " __ASM_NAME("__regs_CommonUnimpStub") __ASM_STDCALL(8) "\n\t"
2134 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
2135 "popl %ecx\n\t"
2136 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
2137 "andl $0xf,%ecx\n\t"
2138 "movl (%esp),%edx\n\t"
2139 "leal (%esp,%ecx,4),%esp\n\t"
2140 "movl %edx,(%esp)\n\t"
2141 "ret" )
2143 /**********************************************************************
2144 * HouseCleanLogicallyDeadHandles (KERNEL32.33)
2146 void WINAPI HouseCleanLogicallyDeadHandles(void)
2148 /* Whatever this is supposed to do, our handles probably
2149 don't need it :-) */
2152 /**********************************************************************
2153 * @ (KERNEL32.100)
2155 BOOL WINAPI _KERNEL32_100(HANDLE threadid,DWORD exitcode,DWORD x)
2157 FIXME("(%p,%d,0x%08x): stub\n",threadid,exitcode,x);
2158 return TRUE;
2161 /**********************************************************************
2162 * @ (KERNEL32.99)
2164 * Checks whether the clock has to be switched from daylight
2165 * savings time to standard time or vice versa.
2168 DWORD WINAPI _KERNEL32_99(DWORD x)
2170 FIXME("(0x%08x): stub\n",x);
2171 return 1;
2175 /***********************************************************************
2176 * Helper for k32 family functions
2178 static void *user32_proc_address(const char *proc_name)
2180 static HMODULE hUser32;
2182 if(!hUser32) hUser32 = LoadLibraryA("user32.dll");
2183 return GetProcAddress(hUser32, proc_name);
2186 /***********************************************************************
2187 * k32CharToOemBuffA (KERNEL32.11)
2189 BOOL WINAPI k32CharToOemBuffA(LPCSTR s, LPSTR d, DWORD len)
2191 WCHAR *bufW;
2193 if ((bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
2195 MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
2196 WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
2197 HeapFree( GetProcessHeap(), 0, bufW );
2199 return TRUE;
2202 /***********************************************************************
2203 * k32CharToOemA (KERNEL32.10)
2205 BOOL WINAPI k32CharToOemA(LPCSTR s, LPSTR d)
2207 if (!s || !d) return TRUE;
2208 return k32CharToOemBuffA( s, d, strlen(s) + 1 );
2211 /***********************************************************************
2212 * k32OemToCharBuffA (KERNEL32.13)
2214 BOOL WINAPI k32OemToCharBuffA(LPCSTR s, LPSTR d, DWORD len)
2216 WCHAR *bufW;
2218 if ((bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
2220 MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
2221 WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
2222 HeapFree( GetProcessHeap(), 0, bufW );
2224 return TRUE;
2227 /***********************************************************************
2228 * k32OemToCharA (KERNEL32.12)
2230 BOOL WINAPI k32OemToCharA(LPCSTR s, LPSTR d)
2232 return k32OemToCharBuffA( s, d, strlen(s) + 1 );
2235 /**********************************************************************
2236 * k32LoadStringA (KERNEL32.14)
2238 INT WINAPI k32LoadStringA(HINSTANCE instance, UINT resource_id,
2239 LPSTR buffer, INT buflen)
2241 static INT (WINAPI *pLoadStringA)(HINSTANCE, UINT, LPSTR, INT);
2243 if(!pLoadStringA) pLoadStringA = user32_proc_address("LoadStringA");
2244 return pLoadStringA(instance, resource_id, buffer, buflen);
2247 /***********************************************************************
2248 * k32wvsprintfA (KERNEL32.16)
2250 INT WINAPI k32wvsprintfA(LPSTR buffer, LPCSTR spec, __ms_va_list args)
2252 static INT (WINAPI *pwvsprintfA)(LPSTR, LPCSTR, __ms_va_list);
2254 if(!pwvsprintfA) pwvsprintfA = user32_proc_address("wvsprintfA");
2255 return (*pwvsprintfA)(buffer, spec, args);
2258 /***********************************************************************
2259 * k32wsprintfA (KERNEL32.15)
2261 INT WINAPIV k32wsprintfA(LPSTR buffer, LPCSTR spec, ...)
2263 __ms_va_list args;
2264 INT res;
2266 __ms_va_start(args, spec);
2267 res = k32wvsprintfA(buffer, spec, args);
2268 __ms_va_end(args);
2269 return res;
2272 /**********************************************************************
2273 * Catch (KERNEL.55)
2275 * Real prototype is:
2276 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
2278 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT *context )
2280 /* Note: we don't save the current ss, as the catch buffer is */
2281 /* only 9 words long. Hopefully no one will have the silly */
2282 /* idea to change the current stack before calling Throw()... */
2284 /* Windows uses:
2285 * lpbuf[0] = ip
2286 * lpbuf[1] = cs
2287 * lpbuf[2] = sp
2288 * lpbuf[3] = bp
2289 * lpbuf[4] = si
2290 * lpbuf[5] = di
2291 * lpbuf[6] = ds
2292 * lpbuf[7] = unused
2293 * lpbuf[8] = ss
2296 lpbuf[0] = LOWORD(context->Eip);
2297 lpbuf[1] = context->SegCs;
2298 /* Windows pushes 4 more words before saving sp */
2299 lpbuf[2] = LOWORD(context->Esp) - 4 * sizeof(WORD);
2300 lpbuf[3] = LOWORD(context->Ebp);
2301 lpbuf[4] = LOWORD(context->Esi);
2302 lpbuf[5] = LOWORD(context->Edi);
2303 lpbuf[6] = context->SegDs;
2304 lpbuf[7] = 0;
2305 lpbuf[8] = context->SegSs;
2306 context->Eax &= ~0xffff; /* Return 0 */
2310 /**********************************************************************
2311 * Throw (KERNEL.56)
2313 * Real prototype is:
2314 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
2316 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT *context )
2318 STACK16FRAME *pFrame;
2319 STACK32FRAME *frame32;
2321 context->Eax = (context->Eax & ~0xffff) | (WORD)retval;
2323 /* Find the frame32 corresponding to the frame16 we are jumping to */
2324 pFrame = CURRENT_STACK16;
2325 frame32 = pFrame->frame32;
2326 while (frame32 && frame32->frame16)
2328 if (OFFSETOF(frame32->frame16) < OFFSETOF(NtCurrentTeb()->WOW32Reserved))
2329 break; /* Something strange is going on */
2330 if (OFFSETOF(frame32->frame16) > lpbuf[2])
2332 /* We found the right frame */
2333 pFrame->frame32 = frame32;
2334 break;
2336 frame32 = ((STACK16FRAME *)MapSL(frame32->frame16))->frame32;
2338 RtlUnwind( &pFrame->frame32->frame, NULL, NULL, 0 );
2340 context->Eip = lpbuf[0];
2341 context->SegCs = lpbuf[1];
2342 context->Esp = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
2343 context->Ebp = lpbuf[3];
2344 context->Esi = lpbuf[4];
2345 context->Edi = lpbuf[5];
2346 context->SegDs = lpbuf[6];
2348 if (lpbuf[8] != context->SegSs)
2349 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );
2354 * 16-bit WOW routines (in KERNEL)
2357 /**********************************************************************
2358 * GetVDMPointer32W (KERNEL.516)
2360 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
2362 GlobalPageLock16(GlobalHandle16(SELECTOROF(vp)));
2363 return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
2366 /***********************************************************************
2367 * LoadLibraryEx32W (KERNEL.513)
2369 DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
2371 HMODULE hModule;
2372 DWORD mutex_count;
2373 OFSTRUCT ofs;
2374 const char *p;
2376 if (!lpszLibFile)
2378 SetLastError(ERROR_INVALID_PARAMETER);
2379 return 0;
2382 /* if the file cannot be found, call LoadLibraryExA anyway, since it might be
2383 a builtin module. This case is handled in MODULE_LoadLibraryExA */
2385 if ((p = strrchr( lpszLibFile, '.' )) && !strchr( p, '\\' )) /* got an extension */
2387 if (OpenFile16( lpszLibFile, &ofs, OF_EXIST ) != HFILE_ERROR16)
2388 lpszLibFile = ofs.szPathName;
2390 else
2392 char buffer[MAX_PATH+4];
2393 strcpy( buffer, lpszLibFile );
2394 strcat( buffer, ".dll" );
2395 if (OpenFile16( buffer, &ofs, OF_EXIST ) != HFILE_ERROR16)
2396 lpszLibFile = ofs.szPathName;
2399 ReleaseThunkLock( &mutex_count );
2400 hModule = LoadLibraryExA( lpszLibFile, (HANDLE)hFile, dwFlags );
2401 RestoreThunkLock( mutex_count );
2403 return (DWORD)hModule;
2406 /***********************************************************************
2407 * GetProcAddress32W (KERNEL.515)
2409 DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
2411 return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
2414 /***********************************************************************
2415 * FreeLibrary32W (KERNEL.514)
2417 DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
2419 BOOL retv;
2420 DWORD mutex_count;
2422 ReleaseThunkLock( &mutex_count );
2423 retv = FreeLibrary( (HMODULE)hLibModule );
2424 RestoreThunkLock( mutex_count );
2425 return (DWORD)retv;
2429 #define CPEX_DEST_STDCALL 0x00000000
2430 #define CPEX_DEST_CDECL 0x80000000
2432 /**********************************************************************
2433 * WOW_CallProc32W
2435 static DWORD WOW_CallProc32W16( FARPROC proc32, DWORD nrofargs, DWORD *args )
2437 DWORD ret;
2438 DWORD mutex_count;
2440 ReleaseThunkLock( &mutex_count );
2441 if (!proc32) ret = 0;
2442 else ret = call_entry_point( proc32, nrofargs & ~CPEX_DEST_CDECL, args );
2443 RestoreThunkLock( mutex_count );
2445 TRACE("returns %08x\n",ret);
2446 return ret;
2449 /**********************************************************************
2450 * CallProc32W (KERNEL.517)
2452 DWORD WINAPIV CallProc32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
2454 DWORD args[32];
2455 unsigned int i;
2457 TRACE("(%d,%d,%p args[",nrofargs,argconvmask,proc32);
2459 for (i=0;i<nrofargs;i++)
2461 if (argconvmask & (1<<i))
2463 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
2464 /* pascal convention, have to reverse the arguments order */
2465 args[nrofargs - i - 1] = (DWORD)MapSL(ptr);
2466 TRACE("%08x(%p),",ptr,MapSL(ptr));
2468 else
2470 DWORD arg = VA_ARG16( valist, DWORD );
2471 /* pascal convention, have to reverse the arguments order */
2472 args[nrofargs - i - 1] = arg;
2473 TRACE("%d,", arg);
2476 TRACE("])\n");
2478 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
2479 stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
2481 return WOW_CallProc32W16( proc32, nrofargs, args );
2484 /**********************************************************************
2485 * _CallProcEx32W (KERNEL.518)
2487 DWORD WINAPIV CallProcEx32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
2489 DWORD args[32];
2490 unsigned int i;
2492 TRACE("(%d,%d,%p args[",nrofargs,argconvmask,proc32);
2494 for (i=0;i<nrofargs;i++)
2496 if (argconvmask & (1<<i))
2498 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
2499 args[i] = (DWORD)MapSL(ptr);
2500 TRACE("%08x(%p),",ptr,MapSL(ptr));
2502 else
2504 DWORD arg = VA_ARG16( valist, DWORD );
2505 args[i] = arg;
2506 TRACE("%d,", arg);
2509 TRACE("])\n");
2510 return WOW_CallProc32W16( proc32, nrofargs, args );
2514 /**********************************************************************
2515 * WOW16Call (KERNEL.500)
2517 * FIXME!!!
2520 DWORD WINAPIV WOW16Call(WORD x, WORD y, WORD z, VA_LIST16 args)
2522 int i;
2523 DWORD calladdr;
2524 FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
2526 for (i=0;i<x/2;i++) {
2527 WORD a = VA_ARG16(args,WORD);
2528 DPRINTF("%04x ",a);
2530 calladdr = VA_ARG16(args,DWORD);
2531 stack16_pop( 3*sizeof(WORD) + x + sizeof(DWORD) );
2532 DPRINTF(") calling address was 0x%08x\n",calladdr);
2533 return 0;