Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / kernel / thunk.c
blob393d8a747740d70e2a8adabcc1915489a9cd97da
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "thread.h"
38 #include "winreg.h"
39 #include "winternl.h"
40 #include "wownt32.h"
41 #include "wine/winbase16.h"
43 #include "wine/debug.h"
44 #include "wine/library.h"
45 #include "kernel_private.h"
46 #include "kernel16_private.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
50 struct ThunkDataCommon
52 char magic[4]; /* 00 */
53 DWORD checksum; /* 04 */
56 struct ThunkDataLS16
58 struct ThunkDataCommon common; /* 00 */
59 SEGPTR targetTable; /* 08 */
60 DWORD firstTime; /* 0C */
63 struct ThunkDataLS32
65 struct ThunkDataCommon common; /* 00 */
66 DWORD * targetTable; /* 08 */
67 char lateBinding[4]; /* 0C */
68 DWORD flags; /* 10 */
69 DWORD reserved1; /* 14 */
70 DWORD reserved2; /* 18 */
71 DWORD offsetQTThunk; /* 1C */
72 DWORD offsetFTProlog; /* 20 */
75 struct ThunkDataSL16
77 struct ThunkDataCommon common; /* 00 */
78 DWORD flags1; /* 08 */
79 DWORD reserved1; /* 0C */
80 struct ThunkDataSL * fpData; /* 10 */
81 SEGPTR spData; /* 14 */
82 DWORD reserved2; /* 18 */
83 char lateBinding[4]; /* 1C */
84 DWORD flags2; /* 20 */
85 DWORD reserved3; /* 20 */
86 SEGPTR apiDatabase; /* 28 */
89 struct ThunkDataSL32
91 struct ThunkDataCommon common; /* 00 */
92 DWORD reserved1; /* 08 */
93 struct ThunkDataSL * data; /* 0C */
94 char lateBinding[4]; /* 10 */
95 DWORD flags; /* 14 */
96 DWORD reserved2; /* 18 */
97 DWORD reserved3; /* 1C */
98 DWORD offsetTargetTable; /* 20 */
101 struct ThunkDataSL
103 #if 0
104 This structure differs from the Win95 original,
105 but this should not matter since it is strictly internal to
106 the thunk handling routines in KRNL386 / KERNEL32.
108 For reference, here is the Win95 layout:
110 struct ThunkDataCommon common; /* 00 */
111 DWORD flags1; /* 08 */
112 SEGPTR apiDatabase; /* 0C */
113 WORD exePtr; /* 10 */
114 WORD segMBA; /* 12 */
115 DWORD lenMBATotal; /* 14 */
116 DWORD lenMBAUsed; /* 18 */
117 DWORD flags2; /* 1C */
118 char pszDll16[256]; /* 20 */
119 char pszDll32[256]; /*120 */
121 We do it differently since all our thunk handling is done
122 by 32-bit code. Therefore we do not need do provide
123 easy access to this data, especially the process target
124 table database, for 16-bit code.
125 #endif
127 struct ThunkDataCommon common;
128 DWORD flags1;
129 struct SLApiDB * apiDB;
130 struct SLTargetDB * targetDB;
131 DWORD flags2;
132 char pszDll16[256];
133 char pszDll32[256];
136 struct SLTargetDB
138 struct SLTargetDB * next;
139 DWORD process;
140 DWORD * targetTable;
143 struct SLApiDB
145 DWORD nrArgBytes;
146 DWORD errorReturnValue;
149 #ifdef __i386__
150 extern void __wine_call_from_16_thunk();
151 #else
152 static void __wine_call_from_16_thunk() { }
153 #endif
155 /* Push a DWORD on the 32-bit stack */
156 static inline void stack32_push( CONTEXT86 *context, DWORD val )
158 context->Esp -= sizeof(DWORD);
159 *(DWORD *)context->Esp = val;
162 /* Pop a DWORD from the 32-bit stack */
163 static inline DWORD stack32_pop( CONTEXT86 *context )
165 DWORD ret = *(DWORD *)context->Esp;
166 context->Esp += sizeof(DWORD);
167 return ret;
170 /***********************************************************************
172 * Win95 internal thunks *
174 ***********************************************************************/
176 /***********************************************************************
177 * LogApiThk (KERNEL.423)
179 void WINAPI LogApiThk( LPSTR func )
181 TRACE( "%s\n", debugstr_a(func) );
184 /***********************************************************************
185 * LogApiThkLSF (KERNEL32.42)
187 * NOTE: needs to preserve all registers!
189 void WINAPI __regs_LogApiThkLSF( LPSTR func, CONTEXT86 *context )
191 TRACE( "%s\n", debugstr_a(func) );
193 #ifdef DEFINE_REGS_ENTRYPOINT
194 DEFINE_REGS_ENTRYPOINT( LogApiThkLSF, 4, 4 );
195 #endif
197 /***********************************************************************
198 * LogApiThkSL (KERNEL32.44)
200 * NOTE: needs to preserve all registers!
202 void WINAPI __regs_LogApiThkSL( LPSTR func, CONTEXT86 *context )
204 TRACE( "%s\n", debugstr_a(func) );
206 #ifdef DEFINE_REGS_ENTRYPOINT
207 DEFINE_REGS_ENTRYPOINT( LogApiThkSL, 4, 4 );
208 #endif
210 /***********************************************************************
211 * LogCBThkSL (KERNEL32.47)
213 * NOTE: needs to preserve all registers!
215 void WINAPI __regs_LogCBThkSL( LPSTR func, CONTEXT86 *context )
217 TRACE( "%s\n", debugstr_a(func) );
219 #ifdef DEFINE_REGS_ENTRYPOINT
220 DEFINE_REGS_ENTRYPOINT( LogCBThkSL, 4, 4 );
221 #endif
223 /***********************************************************************
224 * Generates a FT_Prolog call.
226 * 0FB6D1 movzbl edx,cl
227 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
228 * 68xxxxxxxx push FT_Prolog
229 * C3 lret
231 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
232 LPBYTE x;
234 x = relayCode;
235 *x++ = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
236 *x++ = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
237 x+=4; /* mov edx, [4*edx + targetTable] */
238 *x++ = 0x68; *(DWORD*)x = (DWORD)GetProcAddress(kernel32_handle,"FT_Prolog");
239 x+=4; /* push FT_Prolog */
240 *x++ = 0xC3; /* lret */
241 /* fill rest with 0xCC / int 3 */
244 /***********************************************************************
245 * _write_qtthunk (internal)
246 * Generates a QT_Thunk style call.
248 * 33C9 xor ecx, ecx
249 * 8A4DFC mov cl , [ebp-04]
250 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
251 * B8yyyyyyyy mov eax, QT_Thunk
252 * FFE0 jmp eax
254 static void _write_qtthunk(
255 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
256 DWORD *targetTable /* [in] start of thunk (for index lookup) */
258 LPBYTE x;
260 x = relayCode;
261 *x++ = 0x33;*x++=0xC9; /* xor ecx,ecx */
262 *x++ = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
263 *x++ = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
264 x+=4; /* mov edx, [4*ecx + targetTable */
265 *x++ = 0xB8; *(DWORD*)x = (DWORD)GetProcAddress(kernel32_handle,"QT_Thunk");
266 x+=4; /* mov eax , QT_Thunk */
267 *x++ = 0xFF; *x++ = 0xE0; /* jmp eax */
268 /* should fill the rest of the 32 bytes with 0xCC */
271 /***********************************************************************
272 * _loadthunk
274 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
275 struct ThunkDataCommon *TD32, DWORD checksum)
277 struct ThunkDataCommon *TD16;
278 HMODULE16 hmod;
279 int ordinal;
281 if ((hmod = LoadLibrary16(module)) <= 32)
283 ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
284 module, func, module32, module, hmod);
285 return 0;
288 if ( !(ordinal = NE_GetOrdinal(hmod, func))
289 || !(TD16 = MapSL((SEGPTR)NE_GetEntryPointEx(hmod, ordinal, FALSE))))
291 ERR("Unable to find thunk data '%s' in %s, required by %s (conflicting/incorrect DLL versions !?).\n",
292 func, module, module32);
293 return 0;
296 if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
298 ERR("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
299 module, func, module32,
300 TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
301 TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
302 return 0;
305 if (TD32 && TD16->checksum != TD32->checksum)
307 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
308 module, func, module32, TD16->checksum, TD32->checksum);
309 return 0;
312 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
314 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
315 module, func, module32, *(LPDWORD)TD16, checksum);
316 return 0;
319 return TD16;
322 /***********************************************************************
323 * GetThunkStuff (KERNEL32.53)
325 LPVOID WINAPI GetThunkStuff(LPSTR module, LPSTR func)
327 return _loadthunk(module, func, "<kernel>", NULL, 0L);
330 /***********************************************************************
331 * GetThunkBuff (KERNEL32.52)
332 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
334 LPVOID WINAPI GetThunkBuff(void)
336 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
339 /***********************************************************************
340 * ThunkConnect32 (KERNEL32.@)
341 * Connects a 32bit and a 16bit thunkbuffer.
343 UINT WINAPI ThunkConnect32(
344 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
345 LPSTR thunkfun16, /* [in] win16 thunkfunction */
346 LPSTR module16, /* [in] name of win16 dll */
347 LPSTR module32, /* [in] name of win32 dll */
348 HMODULE hmod32, /* [in] hmodule of win32 dll */
349 DWORD dwReason /* [in] initialisation argument */
351 BOOL directionSL;
353 if (!strncmp(TD->magic, "SL01", 4))
355 directionSL = TRUE;
357 TRACE("SL01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
358 module32, (DWORD)TD, module16, thunkfun16, dwReason);
360 else if (!strncmp(TD->magic, "LS01", 4))
362 directionSL = FALSE;
364 TRACE("LS01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
365 module32, (DWORD)TD, module16, thunkfun16, dwReason);
367 else
369 ERR("Invalid magic %c%c%c%c\n",
370 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
371 return 0;
374 switch (dwReason)
376 case DLL_PROCESS_ATTACH:
378 struct ThunkDataCommon *TD16;
379 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
380 return 0;
382 if (directionSL)
384 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
385 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
386 struct SLTargetDB *tdb;
388 if (SL16->fpData == NULL)
390 ERR("ThunkConnect16 was not called!\n");
391 return 0;
394 SL32->data = SL16->fpData;
396 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
397 tdb->process = GetCurrentProcessId();
398 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
400 tdb->next = SL32->data->targetDB; /* FIXME: not thread-safe! */
401 SL32->data->targetDB = tdb;
403 TRACE("Process %08lx allocated TargetDB entry for ThunkDataSL %08lx\n",
404 GetCurrentProcessId(), (DWORD)SL32->data);
406 else
408 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
409 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
411 LS32->targetTable = MapSL(LS16->targetTable);
413 /* write QT_Thunk and FT_Prolog stubs */
414 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk, LS32->targetTable);
415 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
417 break;
420 case DLL_PROCESS_DETACH:
421 /* FIXME: cleanup */
422 break;
425 return 1;
428 /**********************************************************************
429 * QT_Thunk (KERNEL32.@)
431 * The target address is in EDX.
432 * The 16bit arguments start at ESP.
433 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
434 * So the stack layout is 16bit argument bytes and then the 64 byte
435 * scratch buffer.
436 * The scratch buffer is used as work space by Windows' QT_Thunk
437 * function.
438 * As the programs unfortunately don't always provide a fixed size
439 * scratch buffer (danger, stack corruption ahead !!), we simply resort
440 * to copying over the whole EBP-ESP range to the 16bit stack
441 * (as there's no way to safely figure out the param count
442 * due to this misbehaviour of some programs).
443 * [ok]
445 * See DDJ article 9614c for a very good description of QT_Thunk (also
446 * available online !).
448 * FIXME: DDJ talks of certain register usage rules; I'm not sure
449 * whether we cover this 100%.
451 void WINAPI __regs_QT_Thunk( CONTEXT86 *context )
453 CONTEXT86 context16;
454 DWORD argsize;
456 memcpy(&context16,context,sizeof(context16));
458 context16.SegFs = wine_get_fs();
459 context16.SegGs = wine_get_gs();
460 context16.SegCs = HIWORD(context->Edx);
461 context16.Eip = LOWORD(context->Edx);
462 /* point EBP to the STACK16FRAME on the stack
463 * for the call_to_16 to set up the register content on calling */
464 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + (WORD)&((STACK16FRAME*)0)->bp;
467 * used to be (problematic):
468 * argsize = context->Ebp - context->Esp - 0x40;
469 * due to some programs abusing the API, we better assume the full
470 * EBP - ESP range for copying instead: */
471 argsize = context->Ebp - context->Esp;
473 /* ok, too much is insane; let's limit param count a bit again */
474 if (argsize > 64)
475 argsize = 64; /* 32 WORDs */
477 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
478 context->Eax = context16.Eax;
479 context->Edx = context16.Edx;
480 context->Ecx = context16.Ecx;
482 /* make sure to update the Win32 ESP, too, in order to throw away
483 * the number of parameters that the Win16 function
484 * accepted (that it popped from the corresponding Win16 stack) */
485 context->Esp += LOWORD(context16.Esp) -
486 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
488 #ifdef DEFINE_REGS_ENTRYPOINT
489 DEFINE_REGS_ENTRYPOINT( QT_Thunk, 0, 0 );
490 #endif
493 /**********************************************************************
494 * FT_Prolog (KERNEL32.@)
496 * The set of FT_... thunk routines is used instead of QT_Thunk,
497 * if structures have to be converted from 32-bit to 16-bit
498 * (change of member alignment, conversion of members).
500 * The thunk function (as created by the thunk compiler) calls
501 * FT_Prolog at the beginning, to set up a stack frame and
502 * allocate a 64 byte buffer on the stack.
503 * The input parameters (target address and some flags) are
504 * saved for later use by FT_Thunk.
506 * Input: EDX 16-bit target address (SEGPTR)
507 * CX bits 0..7 target number (in target table)
508 * bits 8..9 some flags (unclear???)
509 * bits 10..15 number of DWORD arguments
511 * Output: A new stackframe is created, and a 64 byte buffer
512 * allocated on the stack. The layout of the stack
513 * on return is as follows:
515 * (ebp+4) return address to caller of thunk function
516 * (ebp) old EBP
517 * (ebp-4) saved EBX register of caller
518 * (ebp-8) saved ESI register of caller
519 * (ebp-12) saved EDI register of caller
520 * (ebp-16) saved ECX register, containing flags
521 * (ebp-20) bitmap containing parameters that are to be converted
522 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
523 * filled in by the thunk code before calling FT_Thunk
524 * (ebp-24)
525 * ... (unclear)
526 * (ebp-44)
527 * (ebp-48) saved EAX register of caller (unclear, never restored???)
528 * (ebp-52) saved EDX register, containing 16-bit thunk target
529 * (ebp-56)
530 * ... (unclear)
531 * (ebp-64)
533 * ESP is EBP-64 after return.
536 void WINAPI __regs_FT_Prolog( CONTEXT86 *context )
538 /* Build stack frame */
539 stack32_push(context, context->Ebp);
540 context->Ebp = context->Esp;
542 /* Allocate 64-byte Thunk Buffer */
543 context->Esp -= 64;
544 memset((char *)context->Esp, '\0', 64);
546 /* Store Flags (ECX) and Target Address (EDX) */
547 /* Save other registers to be restored later */
548 *(DWORD *)(context->Ebp - 4) = context->Ebx;
549 *(DWORD *)(context->Ebp - 8) = context->Esi;
550 *(DWORD *)(context->Ebp - 12) = context->Edi;
551 *(DWORD *)(context->Ebp - 16) = context->Ecx;
553 *(DWORD *)(context->Ebp - 48) = context->Eax;
554 *(DWORD *)(context->Ebp - 52) = context->Edx;
556 #ifdef DEFINE_REGS_ENTRYPOINT
557 DEFINE_REGS_ENTRYPOINT( FT_Prolog, 0, 0 );
558 #endif
560 /**********************************************************************
561 * FT_Thunk (KERNEL32.@)
563 * This routine performs the actual call to 16-bit code,
564 * similar to QT_Thunk. The differences are:
565 * - The call target is taken from the buffer created by FT_Prolog
566 * - Those arguments requested by the thunk code (by setting the
567 * corresponding bit in the bitmap at EBP-20) are converted
568 * from 32-bit pointers to segmented pointers (those pointers
569 * are guaranteed to point to structures copied to the stack
570 * by the thunk code, so we always use the 16-bit stack selector
571 * for those addresses).
573 * The bit #i of EBP-20 corresponds here to the DWORD starting at
574 * ESP+4 + 2*i.
576 * FIXME: It is unclear what happens if there are more than 32 WORDs
577 * of arguments, so that the single DWORD bitmap is no longer
578 * sufficient ...
580 void WINAPI __regs_FT_Thunk( CONTEXT86 *context )
582 DWORD mapESPrelative = *(DWORD *)(context->Ebp - 20);
583 DWORD callTarget = *(DWORD *)(context->Ebp - 52);
585 CONTEXT86 context16;
586 DWORD i, argsize;
587 DWORD newstack[32];
588 LPBYTE oldstack;
590 memcpy(&context16,context,sizeof(context16));
592 context16.SegFs = wine_get_fs();
593 context16.SegGs = wine_get_gs();
594 context16.SegCs = HIWORD(callTarget);
595 context16.Eip = LOWORD(callTarget);
596 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + (WORD)&((STACK16FRAME*)0)->bp;
598 argsize = context->Ebp-context->Esp-0x40;
599 if (argsize > sizeof(newstack)) argsize = sizeof(newstack);
600 oldstack = (LPBYTE)context->Esp;
602 memcpy( newstack, oldstack, argsize );
604 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
605 if (mapESPrelative & (1 << i))
607 SEGPTR *arg = (SEGPTR *)newstack[i];
608 *arg = MAKESEGPTR(SELECTOROF(NtCurrentTeb()->WOW32Reserved),
609 OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize
610 + (*(LPBYTE *)arg - oldstack));
613 WOWCallback16Ex( 0, WCB16_REGS, argsize, newstack, (DWORD *)&context16 );
614 context->Eax = context16.Eax;
615 context->Edx = context16.Edx;
616 context->Ecx = context16.Ecx;
618 context->Esp += LOWORD(context16.Esp) -
619 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
621 /* Copy modified buffers back to 32-bit stack */
622 memcpy( oldstack, newstack, argsize );
624 #ifdef DEFINE_REGS_ENTRYPOINT
625 DEFINE_REGS_ENTRYPOINT( FT_Thunk, 0, 0 );
626 #endif
628 /***********************************************************************
629 * FT_Exit0 (KERNEL32.@)
630 * FT_Exit4 (KERNEL32.@)
631 * FT_Exit8 (KERNEL32.@)
632 * FT_Exit12 (KERNEL32.@)
633 * FT_Exit16 (KERNEL32.@)
634 * FT_Exit20 (KERNEL32.@)
635 * FT_Exit24 (KERNEL32.@)
636 * FT_Exit28 (KERNEL32.@)
637 * FT_Exit32 (KERNEL32.@)
638 * FT_Exit36 (KERNEL32.@)
639 * FT_Exit40 (KERNEL32.@)
640 * FT_Exit44 (KERNEL32.@)
641 * FT_Exit48 (KERNEL32.@)
642 * FT_Exit52 (KERNEL32.@)
643 * FT_Exit56 (KERNEL32.@)
645 * One of the FT_ExitNN functions is called at the end of the thunk code.
646 * It removes the stack frame created by FT_Prolog, moves the function
647 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
648 * value, but the thunk code has moved it from EAX to EBX in the
649 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
650 * and perform a return to the CALLER of the thunk code (while removing
651 * the given number of arguments from the caller's stack).
653 static inline void FT_Exit(CONTEXT86 *context)
655 /* Return value is in EBX */
656 context->Eax = context->Ebx;
658 /* Restore EBX, ESI, and EDI registers */
659 context->Ebx = *(DWORD *)(context->Ebp - 4);
660 context->Esi = *(DWORD *)(context->Ebp - 8);
661 context->Edi = *(DWORD *)(context->Ebp - 12);
663 /* Clean up stack frame */
664 context->Esp = context->Ebp;
665 context->Ebp = stack32_pop(context);
667 /* Pop return address to CALLER of thunk code */
668 context->Eip = stack32_pop(context);
671 #ifdef DEFINE_REGS_ENTRYPOINT
673 #define DEFINE_FT_Exit(n) \
674 void WINAPI __regs_FT_Exit ## n(CONTEXT86 *context) \
676 FT_Exit(context); \
677 context->Esp += n; \
679 DEFINE_REGS_ENTRYPOINT( FT_Exit ## n, 0, 0 )
681 DEFINE_FT_Exit(0);
682 DEFINE_FT_Exit(4);
683 DEFINE_FT_Exit(8);
684 DEFINE_FT_Exit(12);
685 DEFINE_FT_Exit(16);
686 DEFINE_FT_Exit(20);
687 DEFINE_FT_Exit(24);
688 DEFINE_FT_Exit(28);
689 DEFINE_FT_Exit(32);
690 DEFINE_FT_Exit(36);
691 DEFINE_FT_Exit(40);
692 DEFINE_FT_Exit(44);
693 DEFINE_FT_Exit(48);
694 DEFINE_FT_Exit(52);
695 DEFINE_FT_Exit(56);
697 #endif /* DEFINE_REGS_ENTRYPOINT */
700 /***********************************************************************
701 * ThunkInitLS (KERNEL32.43)
702 * A thunkbuffer link routine
703 * The thunkbuf looks like:
705 * 00: DWORD length ? don't know exactly
706 * 04: SEGPTR ptr ? where does it point to?
707 * The pointer ptr is written into the first DWORD of 'thunk'.
708 * (probably correctly implemented)
709 * [ok probably]
710 * RETURNS
711 * segmented pointer to thunk?
713 DWORD WINAPI ThunkInitLS(
714 LPDWORD thunk, /* [in] win32 thunk */
715 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
716 DWORD len, /* [in] thkbuffer length */
717 LPCSTR dll16, /* [in] name of win16 dll */
718 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
720 LPDWORD addr;
722 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
723 return 0;
725 if (!addr[1])
726 return 0;
727 *(DWORD*)thunk = addr[1];
729 return addr[1];
732 /***********************************************************************
733 * Common32ThkLS (KERNEL32.45)
735 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
736 * style thunks. The basic difference is that the parameter conversion
737 * is done completely on the *16-bit* side here. Thus we do not call
738 * the 16-bit target directly, but call a common entry point instead.
739 * This entry function then calls the target according to the target
740 * number passed in the DI register.
742 * Input: EAX SEGPTR to the common 16-bit entry point
743 * CX offset in thunk table (target number * 4)
744 * DX error return value if execution fails (unclear???)
745 * EDX.HI number of DWORD parameters
747 * (Note that we need to move the thunk table offset from CX to DI !)
749 * The called 16-bit stub expects its stack to look like this:
750 * ...
751 * (esp+40) 32-bit arguments
752 * ...
753 * (esp+8) 32 byte of stack space available as buffer
754 * (esp) 8 byte return address for use with 0x66 lret
756 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
757 * and uses the EAX register to return a DWORD return value.
758 * Thus we need to use a special assembly glue routine
759 * (CallRegisterLongProc instead of CallRegisterShortProc).
761 * Finally, we return to the caller, popping the arguments off
762 * the stack. The number of arguments to be popped is returned
763 * in the BL register by the called 16-bit routine.
766 void WINAPI __regs_Common32ThkLS( CONTEXT86 *context )
768 CONTEXT86 context16;
769 DWORD argsize;
771 memcpy(&context16,context,sizeof(context16));
773 context16.SegFs = wine_get_fs();
774 context16.SegGs = wine_get_gs();
775 context16.Edi = LOWORD(context->Ecx);
776 context16.SegCs = HIWORD(context->Eax);
777 context16.Eip = LOWORD(context->Eax);
778 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + (WORD)&((STACK16FRAME*)0)->bp;
780 argsize = HIWORD(context->Edx) * 4;
782 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
783 if (context->Edx == context->Eip)
784 argsize = 6 * 4;
786 /* Note: the first 32 bytes we copy are just garbage from the 32-bit stack, in order to reserve
787 * the space. It is safe to do that since the register function prefix has reserved
788 * a lot more space than that below context->Esp.
790 WOWCallback16Ex( 0, WCB16_REGS, argsize + 32, (LPBYTE)context->Esp - 32, (DWORD *)&context16 );
791 context->Eax = context16.Eax;
793 /* Clean up caller's stack frame */
794 context->Esp += LOBYTE(context16.Ebx);
796 #ifdef DEFINE_REGS_ENTRYPOINT
797 DEFINE_REGS_ENTRYPOINT( Common32ThkLS, 0, 0 );
798 #endif
800 /***********************************************************************
801 * OT_32ThkLSF (KERNEL32.40)
803 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
804 * argument processing is done on both the 32-bit and the 16-bit side:
805 * The 32-bit side prepares arguments, copying them onto the stack.
807 * When this routine is called, the first word on the stack is the
808 * number of argument bytes prepared by the 32-bit code, and EDX
809 * contains the 16-bit target address.
811 * The called 16-bit routine is another relaycode, doing further
812 * argument processing and then calling the real 16-bit target
813 * whose address is stored at [bp-04].
815 * The call proceeds using a normal CallRegisterShortProc.
816 * After return from the 16-bit relaycode, the arguments need
817 * to be copied *back* to the 32-bit stack, since the 32-bit
818 * relaycode processes output parameters.
820 * Note that we copy twice the number of arguments, since some of the
821 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
822 * arguments of the caller!
824 * (Note that this function seems only to be used for
825 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
827 void WINAPI __regs_OT_32ThkLSF( CONTEXT86 *context )
829 CONTEXT86 context16;
830 DWORD argsize;
832 memcpy(&context16,context,sizeof(context16));
834 context16.SegFs = wine_get_fs();
835 context16.SegGs = wine_get_gs();
836 context16.SegCs = HIWORD(context->Edx);
837 context16.Eip = LOWORD(context->Edx);
838 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + (WORD)&((STACK16FRAME*)0)->bp;
840 argsize = 2 * *(WORD *)context->Esp + 2;
842 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
843 context->Eax = context16.Eax;
844 context->Edx = context16.Edx;
846 /* Copy modified buffers back to 32-bit stack */
847 memcpy( (LPBYTE)context->Esp,
848 (LPBYTE)CURRENT_STACK16 - argsize, argsize );
850 context->Esp += LOWORD(context16.Esp) -
851 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
853 #ifdef DEFINE_REGS_ENTRYPOINT
854 DEFINE_REGS_ENTRYPOINT( OT_32ThkLSF, 0, 0 );
855 #endif
857 /***********************************************************************
858 * ThunkInitLSF (KERNEL32.41)
859 * A thunk setup routine.
860 * Expects a pointer to a preinitialized thunkbuffer in the first argument
861 * looking like:
862 *| 00..03: unknown (pointer, check _41, _43, _46)
863 *| 04: EB1E jmp +0x20
865 *| 06..23: unknown (space for replacement code, check .90)
867 *| 24:>E800000000 call offset 29
868 *| 29:>58 pop eax ( target of call )
869 *| 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
870 *| 2F: BAxxxxxxxx mov edx,xxxxxxxx
871 *| 34: 68yyyyyyyy push KERNEL32.90
872 *| 39: C3 ret
874 *| 3A: EB1E jmp +0x20
875 *| 3E ... 59: unknown (space for replacement code?)
876 *| 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
877 *| 5F: 5A pop edx
878 *| 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
879 *| 66: 52 push edx
880 *| 67: 68xxxxxxxx push xxxxxxxx
881 *| 6C: 68yyyyyyyy push KERNEL32.89
882 *| 71: C3 ret
883 *| 72: end?
884 * This function checks if the code is there, and replaces the yyyyyyyy entries
885 * by the functionpointers.
886 * The thunkbuf looks like:
888 *| 00: DWORD length ? don't know exactly
889 *| 04: SEGPTR ptr ? where does it point to?
890 * The segpointer ptr is written into the first DWORD of 'thunk'.
891 * [ok probably]
892 * RETURNS
893 * unclear, pointer to win16 thkbuffer?
895 LPVOID WINAPI ThunkInitLSF(
896 LPBYTE thunk, /* [in] win32 thunk */
897 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
898 DWORD len, /* [in] length of thkbuffer */
899 LPCSTR dll16, /* [in] name of win16 dll */
900 LPCSTR dll32 /* [in] name of win32 dll */
902 LPDWORD addr,addr2;
904 /* FIXME: add checks for valid code ... */
905 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
906 *(DWORD*)(thunk+0x35) = (DWORD)GetProcAddress(kernel32_handle,(LPSTR)90);
907 *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress(kernel32_handle,(LPSTR)89);
910 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
911 return 0;
913 addr2 = MapSL(addr[1]);
914 if (HIWORD(addr2))
915 *(DWORD*)thunk = (DWORD)addr2;
917 return addr2;
920 /***********************************************************************
921 * FT_PrologPrime (KERNEL32.89)
923 * This function is called from the relay code installed by
924 * ThunkInitLSF. It replaces the location from where it was
925 * called by a standard FT_Prolog call stub (which is 'primed'
926 * by inserting the correct target table pointer).
927 * Finally, it calls that stub.
929 * Input: ECX target number + flags (passed through to FT_Prolog)
930 * (ESP) offset of location where target table pointer
931 * is stored, relative to the start of the relay code
932 * (ESP+4) pointer to start of relay code
933 * (this is where the FT_Prolog call stub gets written to)
935 * Note: The two DWORD arguments get popped off the stack.
938 void WINAPI __regs_FT_PrologPrime( CONTEXT86 *context )
940 DWORD targetTableOffset;
941 LPBYTE relayCode;
943 /* Compensate for the fact that the Wine register relay code thought
944 we were being called, although we were in fact jumped to */
945 context->Esp -= 4;
947 /* Write FT_Prolog call stub */
948 targetTableOffset = stack32_pop(context);
949 relayCode = (LPBYTE)stack32_pop(context);
950 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
952 /* Jump to the call stub just created */
953 context->Eip = (DWORD)relayCode;
955 #ifdef DEFINE_REGS_ENTRYPOINT
956 DEFINE_REGS_ENTRYPOINT( FT_PrologPrime, 0, 0 );
957 #endif
959 /***********************************************************************
960 * QT_ThunkPrime (KERNEL32.90)
962 * This function corresponds to FT_PrologPrime, but installs a
963 * call stub for QT_Thunk instead.
965 * Input: (EBP-4) target number (passed through to QT_Thunk)
966 * EDX target table pointer location offset
967 * EAX start of relay code
970 void WINAPI __regs_QT_ThunkPrime( CONTEXT86 *context )
972 DWORD targetTableOffset;
973 LPBYTE relayCode;
975 /* Compensate for the fact that the Wine register relay code thought
976 we were being called, although we were in fact jumped to */
977 context->Esp -= 4;
979 /* Write QT_Thunk call stub */
980 targetTableOffset = context->Edx;
981 relayCode = (LPBYTE)context->Eax;
982 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
984 /* Jump to the call stub just created */
985 context->Eip = (DWORD)relayCode;
987 #ifdef DEFINE_REGS_ENTRYPOINT
988 DEFINE_REGS_ENTRYPOINT( QT_ThunkPrime, 0, 0 );
989 #endif
991 /***********************************************************************
992 * ThunkInitSL (KERNEL32.46)
993 * Another thunkbuf link routine.
994 * The start of the thunkbuf looks like this:
995 * 00: DWORD length
996 * 04: SEGPTR address for thunkbuffer pointer
997 * [ok probably]
999 VOID WINAPI ThunkInitSL(
1000 LPBYTE thunk, /* [in] start of thunkbuffer */
1001 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
1002 DWORD len, /* [in] length of thunkbuffer */
1003 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
1004 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
1006 LPDWORD addr;
1008 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
1009 return;
1011 *(DWORD*)MapSL(addr[1]) = (DWORD)thunk;
1014 /**********************************************************************
1015 * SSInit (KERNEL.700)
1016 * RETURNS
1017 * TRUE for success.
1019 BOOL WINAPI SSInit16(void)
1021 return TRUE;
1024 /**********************************************************************
1025 * SSOnBigStack (KERNEL32.87)
1026 * Check if thunking is initialized (ss selector set up etc.)
1027 * We do that differently, so just return TRUE.
1028 * [ok]
1029 * RETURNS
1030 * TRUE for success.
1032 BOOL WINAPI SSOnBigStack()
1034 TRACE("Yes, thunking is initialized\n");
1035 return TRUE;
1038 /**********************************************************************
1039 * SSConfirmSmallStack (KERNEL.704)
1041 * Abort if not on small stack.
1043 * This must be a register routine as it has to preserve *all* registers.
1045 void WINAPI SSConfirmSmallStack( CONTEXT86 *context )
1047 /* We are always on the small stack while in 16-bit code ... */
1050 /**********************************************************************
1051 * SSCall (KERNEL32.88)
1052 * One of the real thunking functions. This one seems to be for 32<->32
1053 * thunks. It should probably be capable of crossing processboundaries.
1055 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
1056 * [ok]
1058 DWORD WINAPIV SSCall(
1059 DWORD nr, /* [in] number of argument bytes */
1060 DWORD flags, /* [in] FIXME: flags ? */
1061 FARPROC fun, /* [in] function to call */
1062 ... /* [in/out] arguments */
1064 DWORD i,ret;
1065 DWORD *args = ((DWORD *)&fun) + 1;
1067 if(TRACE_ON(thunk))
1069 DPRINTF("(%ld,0x%08lx,%p,[",nr,flags,fun);
1070 for (i=0;i<nr/4;i++)
1071 DPRINTF("0x%08lx,",args[i]);
1072 DPRINTF("])\n");
1074 switch (nr) {
1075 case 0: ret = fun();
1076 break;
1077 case 4: ret = fun(args[0]);
1078 break;
1079 case 8: ret = fun(args[0],args[1]);
1080 break;
1081 case 12: ret = fun(args[0],args[1],args[2]);
1082 break;
1083 case 16: ret = fun(args[0],args[1],args[2],args[3]);
1084 break;
1085 case 20: ret = fun(args[0],args[1],args[2],args[3],args[4]);
1086 break;
1087 case 24: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
1088 break;
1089 case 28: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
1090 break;
1091 case 32: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
1092 break;
1093 case 36: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
1094 break;
1095 case 40: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
1096 break;
1097 case 44: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]);
1098 break;
1099 case 48: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11]);
1100 break;
1101 default:
1102 WARN("Unsupported nr of arguments, %ld\n",nr);
1103 ret = 0;
1104 break;
1107 TRACE(" returning %ld ...\n",ret);
1108 return ret;
1111 /**********************************************************************
1112 * W32S_BackTo32 (KERNEL32.51)
1114 void WINAPI __regs_W32S_BackTo32( CONTEXT86 *context )
1116 LPDWORD stack = (LPDWORD)context->Esp;
1117 FARPROC proc = (FARPROC)context->Eip;
1119 context->Eax = proc( stack[1], stack[2], stack[3], stack[4], stack[5],
1120 stack[6], stack[7], stack[8], stack[9], stack[10] );
1122 context->Eip = stack32_pop(context);
1124 #ifdef DEFINE_REGS_ENTRYPOINT
1125 DEFINE_REGS_ENTRYPOINT( W32S_BackTo32, 0, 0 );
1126 #endif
1128 /**********************************************************************
1129 * AllocSLCallback (KERNEL32.@)
1131 * NOTES
1132 * Win95 uses some structchains for callbacks. It allocates them
1133 * in blocks of 100 entries, size 32 bytes each, layout:
1134 * blockstart:
1135 *| 0: PTR nextblockstart
1136 *| 4: entry *first;
1137 *| 8: WORD sel ( start points to blockstart)
1138 *| A: WORD unknown
1139 * 100xentry:
1140 *| 00..17: Code
1141 *| 18: PDB *owning_process;
1142 *| 1C: PTR blockstart
1144 * We ignore this for now. (Just a note for further developers)
1145 * FIXME: use this method, so we don't waste selectors...
1147 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
1148 * the 0x66 prefix switches from word->long registers.
1150 *| 665A pop edx
1151 *| 6668x arg2 x pushl <arg2>
1152 *| 6652 push edx
1153 *| EAx arg1 x jmpf <arg1>
1155 * returns the startaddress of this thunk.
1157 * Note, that they look very similar to the ones allocates by THUNK_Alloc.
1158 * RETURNS
1159 * A segmented pointer to the start of the thunk
1161 DWORD WINAPI
1162 AllocSLCallback(
1163 DWORD finalizer, /* [in] Finalizer function */
1164 DWORD callback /* [in] Callback function */
1166 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
1167 WORD sel;
1169 x=thunk;
1170 *x++=0x66;*x++=0x5a; /* popl edx */
1171 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
1172 *x++=0x66;*x++=0x52; /* pushl edx */
1173 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
1175 *(DWORD*)(thunk+18) = GetCurrentProcessId();
1177 sel = SELECTOR_AllocBlock( thunk, 32, WINE_LDT_FLAGS_CODE );
1178 return (sel<<16)|0;
1181 /**********************************************************************
1182 * FreeSLCallback (KERNEL32.@)
1183 * Frees the specified 16->32 callback
1185 void WINAPI
1186 FreeSLCallback(
1187 DWORD x /* [in] 16 bit callback (segmented pointer?) */
1189 FIXME("(0x%08lx): stub\n",x);
1193 /**********************************************************************
1194 * GetTEBSelectorFS (KERNEL.475)
1195 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1197 void WINAPI GetTEBSelectorFS16(void)
1199 CURRENT_STACK16->fs = wine_get_fs();
1202 /**********************************************************************
1203 * IsPeFormat (KERNEL.431)
1205 * Determine if a file is a PE format executable.
1207 * RETURNS
1208 * TRUE, if it is.
1209 * FALSE if the file could not be opened or is not a PE file.
1211 * NOTES
1212 * If fn is given as NULL then the function expects hf16 to be valid.
1214 BOOL16 WINAPI IsPeFormat16(
1215 LPSTR fn, /* [in] Filename to the executable */
1216 HFILE16 hf16) /* [in] An open file handle */
1218 BOOL ret = FALSE;
1219 IMAGE_DOS_HEADER mzh;
1220 OFSTRUCT ofs;
1221 DWORD xmagic;
1223 if (fn) hf16 = OpenFile16(fn,&ofs,OF_READ);
1224 if (hf16 == HFILE_ERROR16) return FALSE;
1225 _llseek16(hf16,0,SEEK_SET);
1226 if (sizeof(mzh)!=_lread16(hf16,&mzh,sizeof(mzh))) goto done;
1227 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) goto done;
1228 _llseek16(hf16,mzh.e_lfanew,SEEK_SET);
1229 if (sizeof(DWORD)!=_lread16(hf16,&xmagic,sizeof(DWORD))) goto done;
1230 ret = (xmagic == IMAGE_NT_SIGNATURE);
1231 done:
1232 _lclose16(hf16);
1233 return ret;
1237 /***********************************************************************
1238 * K32Thk1632Prolog (KERNEL32.@)
1240 void WINAPI __regs_K32Thk1632Prolog( CONTEXT86 *context )
1242 LPBYTE code = (LPBYTE)context->Eip - 5;
1244 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1245 of 16->32 thunks instead of using one of the standard methods!
1246 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1247 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1248 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1249 bypassed, which means it will crash the next time the 32-bit OLE
1250 code thunks down again to 16-bit (this *will* happen!).
1252 The following hack tries to recognize this situation.
1253 This is possible since the called stubs in OLECLI32/OLESVR32 all
1254 look exactly the same:
1255 00 E8xxxxxxxx call K32Thk1632Prolog
1256 05 FF55FC call [ebp-04]
1257 08 E8xxxxxxxx call K32Thk1632Epilog
1258 0D 66CB retf
1260 If we recognize this situation, we try to simulate the actions
1261 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1262 to our 32-bit stack, creating a proper STACK16FRAME and
1263 updating cur_stack. */
1265 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1266 && code[13] == 0x66 && code[14] == 0xCB)
1268 WORD stackSel = NtCurrentTeb()->stack_sel;
1269 DWORD stackBase = GetSelectorBase(stackSel);
1271 DWORD argSize = context->Ebp - context->Esp;
1272 char *stack16 = (char *)context->Esp - 4;
1273 char *stack32 = (char *)NtCurrentTeb()->WOW32Reserved - argSize;
1274 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1276 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %p\n",
1277 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1279 memset(frame16, '\0', sizeof(STACK16FRAME));
1280 frame16->frame32 = NtCurrentTeb()->WOW32Reserved;
1281 frame16->ebp = context->Ebp;
1283 memcpy(stack32, stack16, argSize);
1284 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR(stackSel, (DWORD)frame16 - stackBase);
1286 context->Esp = (DWORD)stack32 + 4;
1287 context->Ebp = context->Esp + argSize;
1289 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %p\n",
1290 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1293 /* entry_point is never used again once the entry point has
1294 been called. Thus we re-use it to hold the Win16Lock count */
1295 ReleaseThunkLock(&CURRENT_STACK16->entry_point);
1297 #ifdef DEFINE_REGS_ENTRYPOINT
1298 DEFINE_REGS_ENTRYPOINT( K32Thk1632Prolog, 0, 0 );
1299 #endif
1301 /***********************************************************************
1302 * K32Thk1632Epilog (KERNEL32.@)
1304 void WINAPI __regs_K32Thk1632Epilog( CONTEXT86 *context )
1306 LPBYTE code = (LPBYTE)context->Eip - 13;
1308 RestoreThunkLock(CURRENT_STACK16->entry_point);
1310 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1312 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1313 && code[13] == 0x66 && code[14] == 0xCB)
1315 STACK16FRAME *frame16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1316 char *stack16 = (char *)(frame16 + 1);
1317 DWORD argSize = frame16->ebp - (DWORD)stack16;
1318 char *stack32 = (char *)frame16->frame32 - argSize;
1320 DWORD nArgsPopped = context->Esp - (DWORD)stack32;
1322 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %p\n",
1323 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1325 NtCurrentTeb()->WOW32Reserved = frame16->frame32;
1327 context->Esp = (DWORD)stack16 + nArgsPopped;
1328 context->Ebp = frame16->ebp;
1330 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %p\n",
1331 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1334 #ifdef DEFINE_REGS_ENTRYPOINT
1335 DEFINE_REGS_ENTRYPOINT( K32Thk1632Epilog, 0, 0 );
1336 #endif
1338 /*********************************************************************
1339 * PK16FNF [KERNEL32.91]
1341 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1342 * string buffer with the 8.3 filename of a recently loaded 16-bit
1343 * module. It is unknown exactly what modules trigger this
1344 * mechanism or what purpose this serves. Win98 Explorer (and
1345 * probably also Win95 with IE 4 shell integration) calls this
1346 * several times during initialization.
1348 * FIXME: find out what this really does and make it work.
1350 void WINAPI PK16FNF(LPSTR strPtr)
1352 FIXME("(%p): stub\n", strPtr);
1354 /* fill in a fake filename that'll be easy to recognize */
1355 strcpy(strPtr, "WINESTUB.FIX");
1358 /***********************************************************************
1359 * 16->32 Flat Thunk routines:
1362 /***********************************************************************
1363 * ThunkConnect16 (KERNEL.651)
1364 * Connects a 32bit and a 16bit thunkbuffer.
1366 UINT WINAPI ThunkConnect16(
1367 LPSTR module16, /* [in] name of win16 dll */
1368 LPSTR module32, /* [in] name of win32 dll */
1369 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
1370 DWORD dwReason, /* [in] initialisation argument */
1371 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
1372 LPSTR thunkfun32, /* [in] win32 thunkfunction */
1373 WORD cs /* [in] CS of win16 dll */
1375 BOOL directionSL;
1377 if (!strncmp(TD->magic, "SL01", 4))
1379 directionSL = TRUE;
1381 TRACE("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
1382 module16, (DWORD)TD, module32, thunkfun32, dwReason);
1384 else if (!strncmp(TD->magic, "LS01", 4))
1386 directionSL = FALSE;
1388 TRACE("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
1389 module16, (DWORD)TD, module32, thunkfun32, dwReason);
1391 else
1393 ERR("Invalid magic %c%c%c%c\n",
1394 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
1395 return 0;
1398 switch (dwReason)
1400 case DLL_PROCESS_ATTACH:
1401 if (directionSL)
1403 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
1404 struct ThunkDataSL *SL = SL16->fpData;
1406 if (SL == NULL)
1408 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
1410 SL->common = SL16->common;
1411 SL->flags1 = SL16->flags1;
1412 SL->flags2 = SL16->flags2;
1414 SL->apiDB = MapSL(SL16->apiDatabase);
1415 SL->targetDB = NULL;
1417 lstrcpynA(SL->pszDll16, module16, 255);
1418 lstrcpynA(SL->pszDll32, module32, 255);
1420 /* We should create a SEGPTR to the ThunkDataSL,
1421 but since the contents are not in the original format,
1422 any access to this by 16-bit code would crash anyway. */
1423 SL16->spData = 0;
1424 SL16->fpData = SL;
1428 if (SL->flags2 & 0x80000000)
1430 TRACE("Preloading 32-bit library\n");
1431 LoadLibraryA(module32);
1434 else
1436 /* nothing to do */
1438 break;
1440 case DLL_PROCESS_DETACH:
1441 /* FIXME: cleanup */
1442 break;
1445 return 1;
1449 /***********************************************************************
1450 * C16ThkSL (KERNEL.630)
1453 void WINAPI C16ThkSL(CONTEXT86 *context)
1455 LPBYTE stub = MapSL(context->Eax), x = stub;
1456 WORD cs = wine_get_cs();
1457 WORD ds = wine_get_ds();
1459 /* We produce the following code:
1461 * mov ax, __FLATDS
1462 * mov es, ax
1463 * movzx ecx, cx
1464 * mov edx, es:[ecx + $EDX]
1465 * push bp
1466 * push edx
1467 * push dx
1468 * push edx
1469 * call __FLATCS:__wine_call_from_16_thunk
1472 *x++ = 0xB8; *(WORD *)x = ds; x += sizeof(WORD);
1473 *x++ = 0x8E; *x++ = 0xC0;
1474 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1475 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1476 *x++ = 0x91; *(DWORD *)x = context->Edx; x += sizeof(DWORD);
1478 *x++ = 0x55;
1479 *x++ = 0x66; *x++ = 0x52;
1480 *x++ = 0x52;
1481 *x++ = 0x66; *x++ = 0x52;
1482 *x++ = 0x66; *x++ = 0x9A;
1483 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1484 *(WORD *)x = cs; x += sizeof(WORD);
1486 /* Jump to the stub code just created */
1487 context->Eip = LOWORD(context->Eax);
1488 context->SegCs = HIWORD(context->Eax);
1490 /* Since C16ThkSL got called by a jmp, we need to leave the
1491 original return address on the stack */
1492 context->Esp -= 4;
1495 /***********************************************************************
1496 * C16ThkSL01 (KERNEL.631)
1499 void WINAPI C16ThkSL01(CONTEXT86 *context)
1501 LPBYTE stub = MapSL(context->Eax), x = stub;
1503 if (stub)
1505 struct ThunkDataSL16 *SL16 = MapSL(context->Edx);
1506 struct ThunkDataSL *td = SL16->fpData;
1508 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), (LPCSTR)631);
1509 WORD cs = wine_get_cs();
1511 if (!td)
1513 ERR("ThunkConnect16 was not called!\n");
1514 return;
1517 TRACE("Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
1520 /* We produce the following code:
1522 * xor eax, eax
1523 * mov edx, $td
1524 * call C16ThkSL01
1525 * push bp
1526 * push edx
1527 * push dx
1528 * push edx
1529 * call __FLATCS:__wine_call_from_16_thunk
1532 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1533 *x++ = 0x66; *x++ = 0xBA; *(void **)x = td; x += sizeof(void *);
1534 *x++ = 0x9A; *(DWORD *)x = procAddress; x += sizeof(DWORD);
1536 *x++ = 0x55;
1537 *x++ = 0x66; *x++ = 0x52;
1538 *x++ = 0x52;
1539 *x++ = 0x66; *x++ = 0x52;
1540 *x++ = 0x66; *x++ = 0x9A;
1541 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1542 *(WORD *)x = cs; x += sizeof(WORD);
1544 /* Jump to the stub code just created */
1545 context->Eip = LOWORD(context->Eax);
1546 context->SegCs = HIWORD(context->Eax);
1548 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1549 orginal return address on the stack */
1550 context->Esp -= 4;
1552 else
1554 struct ThunkDataSL *td = (struct ThunkDataSL *)context->Edx;
1555 DWORD targetNr = LOWORD(context->Ecx) / 4;
1556 struct SLTargetDB *tdb;
1558 TRACE("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1559 GetCurrentProcessId(), targetNr, (DWORD)td);
1561 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1562 if (tdb->process == GetCurrentProcessId())
1563 break;
1565 if (!tdb)
1567 TRACE("Loading 32-bit library %s\n", td->pszDll32);
1568 LoadLibraryA(td->pszDll32);
1570 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1571 if (tdb->process == GetCurrentProcessId())
1572 break;
1575 if (tdb)
1577 context->Edx = tdb->targetTable[targetNr];
1579 TRACE("Call target is %08lx\n", context->Edx);
1581 else
1583 WORD *stack = MapSL( MAKESEGPTR(context->SegSs, LOWORD(context->Esp)) );
1584 context->Edx = (context->Edx & ~0xffff) | HIWORD(td->apiDB[targetNr].errorReturnValue);
1585 context->Eax = (context->Eax & ~0xffff) | LOWORD(td->apiDB[targetNr].errorReturnValue);
1586 context->Eip = stack[2];
1587 context->SegCs = stack[3];
1588 context->Esp += td->apiDB[targetNr].nrArgBytes + 4;
1590 ERR("Process %08lx did not ThunkConnect32 %s to %s\n",
1591 GetCurrentProcessId(), td->pszDll32, td->pszDll16);
1597 /***********************************************************************
1598 * 16<->32 Thunklet/Callback API:
1601 #include "pshpack1.h"
1602 typedef struct _THUNKLET
1604 BYTE prefix_target;
1605 BYTE pushl_target;
1606 DWORD target;
1608 BYTE prefix_relay;
1609 BYTE pushl_relay;
1610 DWORD relay;
1612 BYTE jmp_glue;
1613 DWORD glue;
1615 BYTE type;
1616 HINSTANCE16 owner;
1617 struct _THUNKLET *next;
1618 } THUNKLET;
1619 #include "poppack.h"
1621 #define THUNKLET_TYPE_LS 1
1622 #define THUNKLET_TYPE_SL 2
1624 static HANDLE ThunkletHeap = 0;
1625 static WORD ThunkletCodeSel;
1626 static THUNKLET *ThunkletAnchor = NULL;
1628 static FARPROC ThunkletSysthunkGlueLS = 0;
1629 static SEGPTR ThunkletSysthunkGlueSL = 0;
1631 static FARPROC ThunkletCallbackGlueLS = 0;
1632 static SEGPTR ThunkletCallbackGlueSL = 0;
1635 /* map a thunk allocated on ThunkletHeap to a 16-bit pointer */
1636 inline static SEGPTR get_segptr( void *thunk )
1638 if (!thunk) return 0;
1639 return MAKESEGPTR( ThunkletCodeSel, (char *)thunk - (char *)ThunkletHeap );
1642 /***********************************************************************
1643 * THUNK_Init
1645 static BOOL THUNK_Init(void)
1647 LPBYTE thunk;
1649 ThunkletHeap = HeapCreate( 0, 0x10000, 0x10000 );
1650 if (!ThunkletHeap) return FALSE;
1652 ThunkletCodeSel = SELECTOR_AllocBlock( (void *)ThunkletHeap, 0x10000, WINE_LDT_FLAGS_CODE );
1654 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1655 if (!thunk) return FALSE;
1657 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1658 *thunk++ = 0x58; /* popl eax */
1659 *thunk++ = 0xC3; /* ret */
1661 ThunkletSysthunkGlueSL = get_segptr( thunk );
1662 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1663 *thunk++ = 0xCB; /* lret */
1665 return TRUE;
1668 /***********************************************************************
1669 * SetThunkletCallbackGlue (KERNEL.560)
1671 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1673 ThunkletCallbackGlueLS = glueLS;
1674 ThunkletCallbackGlueSL = glueSL;
1678 /***********************************************************************
1679 * THUNK_FindThunklet
1681 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1682 DWORD glue, BYTE type )
1684 THUNKLET *thunk;
1686 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1687 if ( thunk->type == type
1688 && thunk->target == target
1689 && thunk->relay == relay
1690 && ( type == THUNKLET_TYPE_LS ?
1691 ( thunk->glue == glue - (DWORD)&thunk->type )
1692 : ( thunk->glue == glue ) ) )
1693 return thunk;
1695 return NULL;
1698 /***********************************************************************
1699 * THUNK_AllocLSThunklet
1701 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1702 FARPROC glue, HTASK16 owner )
1704 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1705 THUNKLET_TYPE_LS );
1706 if (!thunk)
1708 TDB *pTask = GlobalLock16( owner );
1710 if (!ThunkletHeap) THUNK_Init();
1711 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1712 return 0;
1714 thunk->prefix_target = thunk->prefix_relay = 0x90;
1715 thunk->pushl_target = thunk->pushl_relay = 0x68;
1716 thunk->jmp_glue = 0xE9;
1718 thunk->target = (DWORD)target;
1719 thunk->relay = (DWORD)relay;
1720 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1722 thunk->type = THUNKLET_TYPE_LS;
1723 thunk->owner = pTask? pTask->hInstance : 0;
1725 thunk->next = ThunkletAnchor;
1726 ThunkletAnchor = thunk;
1729 return (FARPROC)thunk;
1732 /***********************************************************************
1733 * THUNK_AllocSLThunklet
1735 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1736 SEGPTR glue, HTASK16 owner )
1738 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1739 THUNKLET_TYPE_SL );
1740 if (!thunk)
1742 TDB *pTask = GlobalLock16( owner );
1744 if (!ThunkletHeap) THUNK_Init();
1745 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1746 return 0;
1748 thunk->prefix_target = thunk->prefix_relay = 0x66;
1749 thunk->pushl_target = thunk->pushl_relay = 0x68;
1750 thunk->jmp_glue = 0xEA;
1752 thunk->target = (DWORD)target;
1753 thunk->relay = (DWORD)relay;
1754 thunk->glue = (DWORD)glue;
1756 thunk->type = THUNKLET_TYPE_SL;
1757 thunk->owner = pTask? pTask->hInstance : 0;
1759 thunk->next = ThunkletAnchor;
1760 ThunkletAnchor = thunk;
1763 return get_segptr( thunk );
1766 /**********************************************************************
1767 * IsLSThunklet
1769 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
1771 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1772 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1773 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1776 /**********************************************************************
1777 * IsSLThunklet (KERNEL.612)
1779 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1781 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1782 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1783 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1788 /***********************************************************************
1789 * AllocLSThunkletSysthunk (KERNEL.607)
1791 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1792 FARPROC relay, DWORD dummy )
1794 if (!ThunkletSysthunkGlueLS) THUNK_Init();
1795 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1796 ThunkletSysthunkGlueLS, GetCurrentTask() );
1799 /***********************************************************************
1800 * AllocSLThunkletSysthunk (KERNEL.608)
1802 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1803 SEGPTR relay, DWORD dummy )
1805 if (!ThunkletSysthunkGlueSL) THUNK_Init();
1806 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1807 ThunkletSysthunkGlueSL, GetCurrentTask() );
1811 /***********************************************************************
1812 * AllocLSThunkletCallbackEx (KERNEL.567)
1814 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1815 DWORD relay, HTASK16 task )
1817 THUNKLET *thunk = MapSL( target );
1818 if ( !thunk ) return NULL;
1820 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1821 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1822 return (FARPROC)thunk->target;
1824 return THUNK_AllocLSThunklet( target, relay,
1825 ThunkletCallbackGlueLS, task );
1828 /***********************************************************************
1829 * AllocSLThunkletCallbackEx (KERNEL.568)
1831 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1832 DWORD relay, HTASK16 task )
1834 THUNKLET *thunk = (THUNKLET *)target;
1835 if ( !thunk ) return 0;
1837 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1838 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1839 return (SEGPTR)thunk->target;
1841 return THUNK_AllocSLThunklet( target, relay,
1842 ThunkletCallbackGlueSL, task );
1845 /***********************************************************************
1846 * AllocLSThunkletCallback (KERNEL.561)
1847 * AllocLSThunkletCallback_dup (KERNEL.606)
1849 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1851 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1854 /***********************************************************************
1855 * AllocSLThunkletCallback (KERNEL.562)
1856 * AllocSLThunkletCallback_dup (KERNEL.605)
1858 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1860 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1863 /***********************************************************************
1864 * FindLSThunkletCallback (KERNEL.563)
1865 * FindLSThunkletCallback_dup (KERNEL.609)
1867 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1869 THUNKLET *thunk = MapSL( target );
1870 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1871 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1872 return (FARPROC)thunk->target;
1874 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1875 (DWORD)ThunkletCallbackGlueLS,
1876 THUNKLET_TYPE_LS );
1877 return (FARPROC)thunk;
1880 /***********************************************************************
1881 * FindSLThunkletCallback (KERNEL.564)
1882 * FindSLThunkletCallback_dup (KERNEL.610)
1884 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1886 THUNKLET *thunk = (THUNKLET *)target;
1887 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1888 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1889 return (SEGPTR)thunk->target;
1891 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1892 (DWORD)ThunkletCallbackGlueSL,
1893 THUNKLET_TYPE_SL );
1894 return get_segptr( thunk );
1898 /***********************************************************************
1899 * FreeThunklet (KERNEL.611)
1901 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1903 return FALSE;
1907 /***********************************************************************
1908 * Callback Client API
1911 #define N_CBC_FIXED 20
1912 #define N_CBC_VARIABLE 10
1913 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1915 static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
1916 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1918 /***********************************************************************
1919 * RegisterCBClient (KERNEL.619)
1921 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1922 SEGPTR relay16, FARPROC *relay32 )
1924 /* Search for free Callback ID */
1925 if ( wCBCId == -1 )
1926 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1927 if ( !CBClientRelay16[ wCBCId ] )
1928 break;
1930 /* Register Callback ID */
1931 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1933 CBClientRelay16[ wCBCId ] = relay16;
1934 CBClientRelay32[ wCBCId ] = relay32;
1936 else
1937 wCBCId = 0;
1939 return wCBCId;
1942 /***********************************************************************
1943 * UnRegisterCBClient (KERNEL.622)
1945 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1946 SEGPTR relay16, FARPROC *relay32 )
1948 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1949 && CBClientRelay16[ wCBCId ] == relay16
1950 && CBClientRelay32[ wCBCId ] == relay32 )
1952 CBClientRelay16[ wCBCId ] = 0;
1953 CBClientRelay32[ wCBCId ] = 0;
1955 else
1956 wCBCId = 0;
1958 return wCBCId;
1962 /***********************************************************************
1963 * InitCBClient (KERNEL.623)
1965 void WINAPI InitCBClient16( FARPROC glueLS )
1967 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1968 SEGPTR glueSL = (SEGPTR)GetProcAddress16( kernel, (LPCSTR)604 );
1970 SetThunkletCallbackGlue16( glueLS, glueSL );
1973 /***********************************************************************
1974 * CBClientGlueSL (KERNEL.604)
1976 void WINAPI CBClientGlueSL( CONTEXT86 *context )
1978 /* Create stack frame */
1979 SEGPTR stackSeg = stack16_push( 12 );
1980 LPWORD stackLin = MapSL( stackSeg );
1981 SEGPTR glue, *glueTab;
1983 stackLin[3] = (WORD)context->Ebp;
1984 stackLin[2] = (WORD)context->Esi;
1985 stackLin[1] = (WORD)context->Edi;
1986 stackLin[0] = (WORD)context->SegDs;
1988 context->Ebp = OFFSETOF( stackSeg ) + 6;
1989 context->Esp = OFFSETOF( stackSeg ) - 4;
1990 context->SegGs = 0;
1992 /* Jump to 16-bit relay code */
1993 glueTab = MapSL( CBClientRelay16[ stackLin[5] ] );
1994 glue = glueTab[ stackLin[4] ];
1995 context->SegCs = SELECTOROF( glue );
1996 context->Eip = OFFSETOF ( glue );
1999 /***********************************************************************
2000 * CBClientThunkSL (KERNEL.620)
2002 extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi );
2003 void WINAPI CBClientThunkSL( CONTEXT86 *context )
2005 /* Call 32-bit relay code */
2007 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2008 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
2010 context->Eax = CALL32_CBClient( proc, args, &context->Esi );
2013 /***********************************************************************
2014 * CBClientThunkSLEx (KERNEL.621)
2016 extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs );
2017 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
2019 /* Call 32-bit relay code */
2021 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2022 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
2023 INT nArgs;
2024 LPWORD stackLin;
2026 context->Eax = CALL32_CBClientEx( proc, args, &context->Esi, &nArgs );
2028 /* Restore registers saved by CBClientGlueSL */
2029 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
2030 context->Ebp = (context->Ebp & ~0xffff) | stackLin[3];
2031 context->Esi = (context->Esi & ~0xffff) | stackLin[2];
2032 context->Edi = (context->Edi & ~0xffff) | stackLin[1];
2033 context->SegDs = stackLin[0];
2034 context->Esp += 16+nArgs;
2036 /* Return to caller of CBClient thunklet */
2037 context->SegCs = stackLin[9];
2038 context->Eip = stackLin[8];
2042 /***********************************************************************
2043 * Get16DLLAddress (KERNEL32.@)
2045 * This function is used by a Win32s DLL if it wants to call a Win16 function.
2046 * A 16:16 segmented pointer to the function is returned.
2047 * Written without any docu.
2049 SEGPTR WINAPI Get16DLLAddress(HMODULE16 handle, LPSTR func_name)
2051 static WORD code_sel32;
2052 FARPROC16 proc_16;
2053 LPBYTE thunk;
2055 if (!code_sel32)
2057 if (!ThunkletHeap) THUNK_Init();
2058 code_sel32 = SELECTOR_AllocBlock( (void *)ThunkletHeap, 0x10000,
2059 WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
2060 if (!code_sel32) return 0;
2062 if (!(thunk = HeapAlloc( ThunkletHeap, 0, 32 ))) return 0;
2064 if (!handle) handle = GetModuleHandle16("WIN32S16");
2065 proc_16 = GetProcAddress16(handle, func_name);
2067 /* movl proc_16, $edx */
2068 *thunk++ = 0xba;
2069 *(FARPROC16 *)thunk = proc_16;
2070 thunk += sizeof(FARPROC16);
2072 /* jmpl QT_Thunk */
2073 *thunk++ = 0xea;
2074 *(FARPROC *)thunk = GetProcAddress(kernel32_handle,"QT_Thunk");
2075 thunk += sizeof(FARPROC16);
2076 *(WORD *)thunk = wine_get_cs();
2078 return MAKESEGPTR( code_sel32, (char *)thunk - (char *)ThunkletHeap );
2082 /***********************************************************************
2083 * GetWin16DOSEnv (KERNEL32.34)
2084 * Returns some internal value.... probably the default environment database?
2086 DWORD WINAPI GetWin16DOSEnv()
2088 FIXME("stub, returning 0\n");
2089 return 0;
2092 /**********************************************************************
2093 * GetPK16SysVar (KERNEL32.92)
2095 LPVOID WINAPI GetPK16SysVar(void)
2097 static BYTE PK16SysVar[128];
2099 FIXME("()\n");
2100 return PK16SysVar;
2103 /**********************************************************************
2104 * CommonUnimpStub (KERNEL32.17)
2106 void WINAPI __regs_CommonUnimpStub( CONTEXT86 *context )
2108 FIXME("generic stub: %s\n", ((LPSTR)context->Eax ? (LPSTR)context->Eax : "?"));
2110 switch ((context->Ecx >> 4) & 0x0f)
2112 case 15: context->Eax = -1; break;
2113 case 14: context->Eax = 0x78; break;
2114 case 13: context->Eax = 0x32; break;
2115 case 1: context->Eax = 1; break;
2116 default: context->Eax = 0; break;
2119 context->Esp += (context->Ecx & 0x0f) * 4;
2121 #ifdef DEFINE_REGS_ENTRYPOINT
2122 DEFINE_REGS_ENTRYPOINT( CommonUnimpStub, 0, 0 );
2123 #endif
2125 /**********************************************************************
2126 * HouseCleanLogicallyDeadHandles (KERNEL32.33)
2128 void WINAPI HouseCleanLogicallyDeadHandles(void)
2130 /* Whatever this is supposed to do, our handles probably
2131 don't need it :-) */
2134 /**********************************************************************
2135 * @ (KERNEL32.100)
2137 BOOL WINAPI _KERNEL32_100(HANDLE threadid,DWORD exitcode,DWORD x)
2139 FIXME("(%p,%ld,0x%08lx): stub\n",threadid,exitcode,x);
2140 return TRUE;
2143 /**********************************************************************
2144 * @ (KERNEL32.99)
2146 * Checks whether the clock has to be switched from daylight
2147 * savings time to standard time or vice versa.
2150 DWORD WINAPI _KERNEL32_99(DWORD x)
2152 FIXME("(0x%08lx): stub\n",x);
2153 return 1;
2157 /**********************************************************************
2158 * Catch (KERNEL.55)
2160 * Real prototype is:
2161 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
2163 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT86 *context )
2165 /* Note: we don't save the current ss, as the catch buffer is */
2166 /* only 9 words long. Hopefully no one will have the silly */
2167 /* idea to change the current stack before calling Throw()... */
2169 /* Windows uses:
2170 * lpbuf[0] = ip
2171 * lpbuf[1] = cs
2172 * lpbuf[2] = sp
2173 * lpbuf[3] = bp
2174 * lpbuf[4] = si
2175 * lpbuf[5] = di
2176 * lpbuf[6] = ds
2177 * lpbuf[7] = unused
2178 * lpbuf[8] = ss
2181 lpbuf[0] = LOWORD(context->Eip);
2182 lpbuf[1] = context->SegCs;
2183 /* Windows pushes 4 more words before saving sp */
2184 lpbuf[2] = LOWORD(context->Esp) - 4 * sizeof(WORD);
2185 lpbuf[3] = LOWORD(context->Ebp);
2186 lpbuf[4] = LOWORD(context->Esi);
2187 lpbuf[5] = LOWORD(context->Edi);
2188 lpbuf[6] = context->SegDs;
2189 lpbuf[7] = 0;
2190 lpbuf[8] = context->SegSs;
2191 context->Eax &= ~0xffff; /* Return 0 */
2195 /**********************************************************************
2196 * Throw (KERNEL.56)
2198 * Real prototype is:
2199 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
2201 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT86 *context )
2203 STACK16FRAME *pFrame;
2204 STACK32FRAME *frame32;
2206 context->Eax = (context->Eax & ~0xffff) | (WORD)retval;
2208 /* Find the frame32 corresponding to the frame16 we are jumping to */
2209 pFrame = CURRENT_STACK16;
2210 frame32 = pFrame->frame32;
2211 while (frame32 && frame32->frame16)
2213 if (OFFSETOF(frame32->frame16) < OFFSETOF(NtCurrentTeb()->WOW32Reserved))
2214 break; /* Something strange is going on */
2215 if (OFFSETOF(frame32->frame16) > lpbuf[2])
2217 /* We found the right frame */
2218 pFrame->frame32 = frame32;
2219 break;
2221 frame32 = ((STACK16FRAME *)MapSL(frame32->frame16))->frame32;
2223 RtlUnwind( &pFrame->frame32->frame, NULL, NULL, 0 );
2225 context->Eip = lpbuf[0];
2226 context->SegCs = lpbuf[1];
2227 context->Esp = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
2228 context->Ebp = lpbuf[3];
2229 context->Esi = lpbuf[4];
2230 context->Edi = lpbuf[5];
2231 context->SegDs = lpbuf[6];
2233 if (lpbuf[8] != context->SegSs)
2234 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );