user32: Fix user_thread_info for 64-bits
[wine/wine64.git] / dlls / kernel32 / thunk.c
blob29093dec30717ef46366186cfd9c1ad437daa823
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 "kernel_private.h"
44 #include "kernel16_private.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
48 struct ThunkDataCommon
50 char magic[4]; /* 00 */
51 DWORD checksum; /* 04 */
54 struct ThunkDataLS16
56 struct ThunkDataCommon common; /* 00 */
57 SEGPTR targetTable; /* 08 */
58 DWORD firstTime; /* 0C */
61 struct ThunkDataLS32
63 struct ThunkDataCommon common; /* 00 */
64 DWORD * targetTable; /* 08 */
65 char lateBinding[4]; /* 0C */
66 DWORD flags; /* 10 */
67 DWORD reserved1; /* 14 */
68 DWORD reserved2; /* 18 */
69 DWORD offsetQTThunk; /* 1C */
70 DWORD offsetFTProlog; /* 20 */
73 struct ThunkDataSL16
75 struct ThunkDataCommon common; /* 00 */
76 DWORD flags1; /* 08 */
77 DWORD reserved1; /* 0C */
78 struct ThunkDataSL * fpData; /* 10 */
79 SEGPTR spData; /* 14 */
80 DWORD reserved2; /* 18 */
81 char lateBinding[4]; /* 1C */
82 DWORD flags2; /* 20 */
83 DWORD reserved3; /* 20 */
84 SEGPTR apiDatabase; /* 28 */
87 struct ThunkDataSL32
89 struct ThunkDataCommon common; /* 00 */
90 DWORD reserved1; /* 08 */
91 struct ThunkDataSL * data; /* 0C */
92 char lateBinding[4]; /* 10 */
93 DWORD flags; /* 14 */
94 DWORD reserved2; /* 18 */
95 DWORD reserved3; /* 1C */
96 DWORD offsetTargetTable; /* 20 */
99 struct ThunkDataSL
101 #if 0
102 This structure differs from the Win95 original,
103 but this should not matter since it is strictly internal to
104 the thunk handling routines in KRNL386 / KERNEL32.
106 For reference, here is the Win95 layout:
108 struct ThunkDataCommon common; /* 00 */
109 DWORD flags1; /* 08 */
110 SEGPTR apiDatabase; /* 0C */
111 WORD exePtr; /* 10 */
112 WORD segMBA; /* 12 */
113 DWORD lenMBATotal; /* 14 */
114 DWORD lenMBAUsed; /* 18 */
115 DWORD flags2; /* 1C */
116 char pszDll16[256]; /* 20 */
117 char pszDll32[256]; /*120 */
119 We do it differently since all our thunk handling is done
120 by 32-bit code. Therefore we do not need to provide
121 easy access to this data, especially the process target
122 table database, for 16-bit code.
123 #endif
125 struct ThunkDataCommon common;
126 DWORD flags1;
127 struct SLApiDB * apiDB;
128 struct SLTargetDB * targetDB;
129 DWORD flags2;
130 char pszDll16[256];
131 char pszDll32[256];
134 struct SLTargetDB
136 struct SLTargetDB * next;
137 DWORD process;
138 DWORD * targetTable;
141 struct SLApiDB
143 DWORD nrArgBytes;
144 DWORD errorReturnValue;
147 SEGPTR CALL32_CBClient_RetAddr = 0;
148 SEGPTR CALL32_CBClientEx_RetAddr = 0;
150 #ifdef __i386__
151 extern void __wine_call_from_16_thunk();
152 #else
153 static void __wine_call_from_16_thunk() { }
154 #endif
156 /* Push a DWORD on the 32-bit stack */
157 static inline void stack32_push( CONTEXT86 *context, DWORD val )
159 context->Esp -= sizeof(DWORD);
160 *(DWORD *)context->Esp = val;
163 /* Pop a DWORD from the 32-bit stack */
164 static inline DWORD stack32_pop( CONTEXT86 *context )
166 DWORD ret = *(DWORD *)context->Esp;
167 context->Esp += sizeof(DWORD);
168 return ret;
171 /***********************************************************************
173 * Win95 internal thunks *
175 ***********************************************************************/
177 /***********************************************************************
178 * LogApiThk (KERNEL.423)
180 void WINAPI LogApiThk( LPSTR func )
182 TRACE( "%s\n", debugstr_a(func) );
185 /***********************************************************************
186 * LogApiThkLSF (KERNEL32.42)
188 * NOTE: needs to preserve all registers!
190 void WINAPI __regs_LogApiThkLSF( LPSTR func, CONTEXT86 *context )
192 TRACE( "%s\n", debugstr_a(func) );
194 #ifdef DEFINE_REGS_ENTRYPOINT
195 DEFINE_REGS_ENTRYPOINT( LogApiThkLSF, 4, 4 )
196 #endif
198 /***********************************************************************
199 * LogApiThkSL (KERNEL32.44)
201 * NOTE: needs to preserve all registers!
203 void WINAPI __regs_LogApiThkSL( LPSTR func, CONTEXT86 *context )
205 TRACE( "%s\n", debugstr_a(func) );
207 #ifdef DEFINE_REGS_ENTRYPOINT
208 DEFINE_REGS_ENTRYPOINT( LogApiThkSL, 4, 4 )
209 #endif
211 /***********************************************************************
212 * LogCBThkSL (KERNEL32.47)
214 * NOTE: needs to preserve all registers!
216 void WINAPI __regs_LogCBThkSL( LPSTR func, CONTEXT86 *context )
218 TRACE( "%s\n", debugstr_a(func) );
220 #ifdef DEFINE_REGS_ENTRYPOINT
221 DEFINE_REGS_ENTRYPOINT( LogCBThkSL, 4, 4 )
222 #endif
224 /***********************************************************************
225 * Generates a FT_Prolog call.
227 * 0FB6D1 movzbl edx,cl
228 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
229 * 68xxxxxxxx push FT_Prolog
230 * C3 lret
232 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
233 LPBYTE x;
235 x = relayCode;
236 *x++ = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
237 *x++ = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
238 x+=4; /* mov edx, [4*edx + targetTable] */
239 *x++ = 0x68; *(DWORD*)x = (DWORD)GetProcAddress(kernel32_handle,"FT_Prolog");
240 x+=4; /* push FT_Prolog */
241 *x++ = 0xC3; /* lret */
242 /* fill rest with 0xCC / int 3 */
245 /***********************************************************************
246 * _write_qtthunk (internal)
247 * Generates a QT_Thunk style call.
249 * 33C9 xor ecx, ecx
250 * 8A4DFC mov cl , [ebp-04]
251 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
252 * B8yyyyyyyy mov eax, QT_Thunk
253 * FFE0 jmp eax
255 static void _write_qtthunk(
256 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
257 DWORD *targetTable /* [in] start of thunk (for index lookup) */
259 LPBYTE x;
261 x = relayCode;
262 *x++ = 0x33;*x++=0xC9; /* xor ecx,ecx */
263 *x++ = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
264 *x++ = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
265 x+=4; /* mov edx, [4*ecx + targetTable */
266 *x++ = 0xB8; *(DWORD*)x = (DWORD)GetProcAddress(kernel32_handle,"QT_Thunk");
267 x+=4; /* mov eax , QT_Thunk */
268 *x++ = 0xFF; *x++ = 0xE0; /* jmp eax */
269 /* should fill the rest of the 32 bytes with 0xCC */
272 /***********************************************************************
273 * _loadthunk
275 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
276 struct ThunkDataCommon *TD32, DWORD checksum)
278 struct ThunkDataCommon *TD16;
279 HMODULE16 hmod;
280 int ordinal;
282 if ((hmod = LoadLibrary16(module)) <= 32)
284 ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
285 module, func, module32, module, hmod);
286 return 0;
289 if ( !(ordinal = NE_GetOrdinal(hmod, func))
290 || !(TD16 = MapSL((SEGPTR)NE_GetEntryPointEx(hmod, ordinal, FALSE))))
292 ERR("Unable to find thunk data '%s' in %s, required by %s (conflicting/incorrect DLL versions !?).\n",
293 func, module, module32);
294 return 0;
297 if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
299 ERR("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
300 module, func, module32,
301 TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
302 TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
303 return 0;
306 if (TD32 && TD16->checksum != TD32->checksum)
308 ERR("(%s, %s, %s): Wrong checksum %08x (should be %08x)\n",
309 module, func, module32, TD16->checksum, TD32->checksum);
310 return 0;
313 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
315 ERR("(%s, %s, %s): Wrong checksum %08x (should be %08x)\n",
316 module, func, module32, *(LPDWORD)TD16, checksum);
317 return 0;
320 return TD16;
323 /***********************************************************************
324 * GetThunkStuff (KERNEL32.53)
326 LPVOID WINAPI GetThunkStuff(LPCSTR module, LPCSTR func)
328 return _loadthunk(module, func, "<kernel>", NULL, 0L);
331 /***********************************************************************
332 * GetThunkBuff (KERNEL32.52)
333 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
335 LPVOID WINAPI GetThunkBuff(void)
337 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
340 /***********************************************************************
341 * ThunkConnect32 (KERNEL32.@)
342 * Connects a 32bit and a 16bit thunkbuffer.
344 UINT WINAPI ThunkConnect32(
345 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
346 LPSTR thunkfun16, /* [in] win16 thunkfunction */
347 LPSTR module16, /* [in] name of win16 dll */
348 LPSTR module32, /* [in] name of win32 dll */
349 HMODULE hmod32, /* [in] hmodule of win32 dll */
350 DWORD dwReason /* [in] initialisation argument */
352 BOOL directionSL;
354 if (!strncmp(TD->magic, "SL01", 4))
356 directionSL = TRUE;
358 TRACE("SL01 thunk %s (%p) <- %s (%s), Reason: %d\n",
359 module32, TD, module16, thunkfun16, dwReason);
361 else if (!strncmp(TD->magic, "LS01", 4))
363 directionSL = FALSE;
365 TRACE("LS01 thunk %s (%p) -> %s (%s), Reason: %d\n",
366 module32, TD, module16, thunkfun16, dwReason);
368 else
370 ERR("Invalid magic %c%c%c%c\n",
371 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
372 return 0;
375 switch (dwReason)
377 case DLL_PROCESS_ATTACH:
379 struct ThunkDataCommon *TD16;
380 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
381 return 0;
383 if (directionSL)
385 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
386 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
387 struct SLTargetDB *tdb;
389 if (SL16->fpData == NULL)
391 ERR("ThunkConnect16 was not called!\n");
392 return 0;
395 SL32->data = SL16->fpData;
397 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
398 tdb->process = GetCurrentProcessId();
399 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
401 tdb->next = SL32->data->targetDB; /* FIXME: not thread-safe! */
402 SL32->data->targetDB = tdb;
404 TRACE("Process %08x allocated TargetDB entry for ThunkDataSL %p\n",
405 GetCurrentProcessId(), SL32->data);
407 else
409 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
410 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
412 LS32->targetTable = MapSL(LS16->targetTable);
414 /* write QT_Thunk and FT_Prolog stubs */
415 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk, LS32->targetTable);
416 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
418 break;
421 case DLL_PROCESS_DETACH:
422 /* FIXME: cleanup */
423 break;
426 return 1;
429 /**********************************************************************
430 * QT_Thunk (KERNEL32.@)
432 * The target address is in EDX.
433 * The 16bit arguments start at ESP.
434 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
435 * So the stack layout is 16bit argument bytes and then the 64 byte
436 * scratch buffer.
437 * The scratch buffer is used as work space by Windows' QT_Thunk
438 * function.
439 * As the programs unfortunately don't always provide a fixed size
440 * scratch buffer (danger, stack corruption ahead !!), we simply resort
441 * to copying over the whole EBP-ESP range to the 16bit stack
442 * (as there's no way to safely figure out the param count
443 * due to this misbehaviour of some programs).
444 * [ok]
446 * See DDJ article 9614c for a very good description of QT_Thunk (also
447 * available online !).
449 * FIXME: DDJ talks of certain register usage rules; I'm not sure
450 * whether we cover this 100%.
452 void WINAPI __regs_QT_Thunk( CONTEXT86 *context )
454 CONTEXT86 context16;
455 DWORD argsize;
457 context16 = *context;
459 context16.SegFs = wine_get_fs();
460 context16.SegGs = wine_get_gs();
461 context16.SegCs = HIWORD(context->Edx);
462 context16.Eip = LOWORD(context->Edx);
463 /* point EBP to the STACK16FRAME on the stack
464 * for the call_to_16 to set up the register content on calling */
465 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
468 * used to be (problematic):
469 * argsize = context->Ebp - context->Esp - 0x40;
470 * due to some programs abusing the API, we better assume the full
471 * EBP - ESP range for copying instead: */
472 argsize = context->Ebp - context->Esp;
474 /* ok, too much is insane; let's limit param count a bit again */
475 if (argsize > 64)
476 argsize = 64; /* 32 WORDs */
478 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
479 context->Eax = context16.Eax;
480 context->Edx = context16.Edx;
481 context->Ecx = context16.Ecx;
483 /* make sure to update the Win32 ESP, too, in order to throw away
484 * the number of parameters that the Win16 function
485 * accepted (that it popped from the corresponding Win16 stack) */
486 context->Esp += LOWORD(context16.Esp) -
487 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
489 #ifdef DEFINE_REGS_ENTRYPOINT
490 DEFINE_REGS_ENTRYPOINT( QT_Thunk, 0, 0 )
491 #endif
494 /**********************************************************************
495 * FT_Prolog (KERNEL32.@)
497 * The set of FT_... thunk routines is used instead of QT_Thunk,
498 * if structures have to be converted from 32-bit to 16-bit
499 * (change of member alignment, conversion of members).
501 * The thunk function (as created by the thunk compiler) calls
502 * FT_Prolog at the beginning, to set up a stack frame and
503 * allocate a 64 byte buffer on the stack.
504 * The input parameters (target address and some flags) are
505 * saved for later use by FT_Thunk.
507 * Input: EDX 16-bit target address (SEGPTR)
508 * CX bits 0..7 target number (in target table)
509 * bits 8..9 some flags (unclear???)
510 * bits 10..15 number of DWORD arguments
512 * Output: A new stackframe is created, and a 64 byte buffer
513 * allocated on the stack. The layout of the stack
514 * on return is as follows:
516 * (ebp+4) return address to caller of thunk function
517 * (ebp) old EBP
518 * (ebp-4) saved EBX register of caller
519 * (ebp-8) saved ESI register of caller
520 * (ebp-12) saved EDI register of caller
521 * (ebp-16) saved ECX register, containing flags
522 * (ebp-20) bitmap containing parameters that are to be converted
523 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
524 * filled in by the thunk code before calling FT_Thunk
525 * (ebp-24)
526 * ... (unclear)
527 * (ebp-44)
528 * (ebp-48) saved EAX register of caller (unclear, never restored???)
529 * (ebp-52) saved EDX register, containing 16-bit thunk target
530 * (ebp-56)
531 * ... (unclear)
532 * (ebp-64)
534 * ESP is EBP-64 after return.
537 void WINAPI __regs_FT_Prolog( CONTEXT86 *context )
539 /* Build stack frame */
540 stack32_push(context, context->Ebp);
541 context->Ebp = context->Esp;
543 /* Allocate 64-byte Thunk Buffer */
544 context->Esp -= 64;
545 memset((char *)context->Esp, '\0', 64);
547 /* Store Flags (ECX) and Target Address (EDX) */
548 /* Save other registers to be restored later */
549 *(DWORD *)(context->Ebp - 4) = context->Ebx;
550 *(DWORD *)(context->Ebp - 8) = context->Esi;
551 *(DWORD *)(context->Ebp - 12) = context->Edi;
552 *(DWORD *)(context->Ebp - 16) = context->Ecx;
554 *(DWORD *)(context->Ebp - 48) = context->Eax;
555 *(DWORD *)(context->Ebp - 52) = context->Edx;
557 #ifdef DEFINE_REGS_ENTRYPOINT
558 DEFINE_REGS_ENTRYPOINT( FT_Prolog, 0, 0 )
559 #endif
561 /**********************************************************************
562 * FT_Thunk (KERNEL32.@)
564 * This routine performs the actual call to 16-bit code,
565 * similar to QT_Thunk. The differences are:
566 * - The call target is taken from the buffer created by FT_Prolog
567 * - Those arguments requested by the thunk code (by setting the
568 * corresponding bit in the bitmap at EBP-20) are converted
569 * from 32-bit pointers to segmented pointers (those pointers
570 * are guaranteed to point to structures copied to the stack
571 * by the thunk code, so we always use the 16-bit stack selector
572 * for those addresses).
574 * The bit #i of EBP-20 corresponds here to the DWORD starting at
575 * ESP+4 + 2*i.
577 * FIXME: It is unclear what happens if there are more than 32 WORDs
578 * of arguments, so that the single DWORD bitmap is no longer
579 * sufficient ...
581 void WINAPI __regs_FT_Thunk( CONTEXT86 *context )
583 DWORD mapESPrelative = *(DWORD *)(context->Ebp - 20);
584 DWORD callTarget = *(DWORD *)(context->Ebp - 52);
586 CONTEXT86 context16;
587 DWORD i, argsize;
588 DWORD newstack[32];
589 LPBYTE oldstack;
591 context16 = *context;
593 context16.SegFs = wine_get_fs();
594 context16.SegGs = wine_get_gs();
595 context16.SegCs = HIWORD(callTarget);
596 context16.Eip = LOWORD(callTarget);
597 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
599 argsize = context->Ebp-context->Esp-0x40;
600 if (argsize > sizeof(newstack)) argsize = sizeof(newstack);
601 oldstack = (LPBYTE)context->Esp;
603 memcpy( newstack, oldstack, argsize );
605 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
606 if (mapESPrelative & (1 << i))
608 SEGPTR *arg = (SEGPTR *)newstack[i];
609 *arg = MAKESEGPTR(SELECTOROF(NtCurrentTeb()->WOW32Reserved),
610 OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize
611 + (*(LPBYTE *)arg - oldstack));
614 WOWCallback16Ex( 0, WCB16_REGS, argsize, newstack, (DWORD *)&context16 );
615 context->Eax = context16.Eax;
616 context->Edx = context16.Edx;
617 context->Ecx = context16.Ecx;
619 context->Esp += LOWORD(context16.Esp) -
620 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
622 /* Copy modified buffers back to 32-bit stack */
623 memcpy( oldstack, newstack, argsize );
625 #ifdef DEFINE_REGS_ENTRYPOINT
626 DEFINE_REGS_ENTRYPOINT( FT_Thunk, 0, 0 )
627 #endif
629 #ifdef __i386__
631 /***********************************************************************
632 * FT_Exit0 (KERNEL32.@)
633 * FT_Exit4 (KERNEL32.@)
634 * FT_Exit8 (KERNEL32.@)
635 * FT_Exit12 (KERNEL32.@)
636 * FT_Exit16 (KERNEL32.@)
637 * FT_Exit20 (KERNEL32.@)
638 * FT_Exit24 (KERNEL32.@)
639 * FT_Exit28 (KERNEL32.@)
640 * FT_Exit32 (KERNEL32.@)
641 * FT_Exit36 (KERNEL32.@)
642 * FT_Exit40 (KERNEL32.@)
643 * FT_Exit44 (KERNEL32.@)
644 * FT_Exit48 (KERNEL32.@)
645 * FT_Exit52 (KERNEL32.@)
646 * FT_Exit56 (KERNEL32.@)
648 * One of the FT_ExitNN functions is called at the end of the thunk code.
649 * It removes the stack frame created by FT_Prolog, moves the function
650 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
651 * value, but the thunk code has moved it from EAX to EBX in the
652 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
653 * and perform a return to the CALLER of the thunk code (while removing
654 * the given number of arguments from the caller's stack).
656 #define FT_EXIT_RESTORE_REGS \
657 "movl %ebx,%eax\n\t" \
658 "movl -4(%ebp),%ebx\n\t" \
659 "movl -8(%ebp),%esi\n\t" \
660 "movl -12(%ebp),%edi\n\t" \
661 "leave\n\t"
663 #define DEFINE_FT_Exit(n) \
664 __ASM_GLOBAL_FUNC( FT_Exit ## n, FT_EXIT_RESTORE_REGS "ret $" #n )
666 DEFINE_FT_Exit(0)
667 DEFINE_FT_Exit(4)
668 DEFINE_FT_Exit(8)
669 DEFINE_FT_Exit(12)
670 DEFINE_FT_Exit(16)
671 DEFINE_FT_Exit(20)
672 DEFINE_FT_Exit(24)
673 DEFINE_FT_Exit(28)
674 DEFINE_FT_Exit(32)
675 DEFINE_FT_Exit(36)
676 DEFINE_FT_Exit(40)
677 DEFINE_FT_Exit(44)
678 DEFINE_FT_Exit(48)
679 DEFINE_FT_Exit(52)
680 DEFINE_FT_Exit(56)
682 #endif /* __i386__ */
685 /***********************************************************************
686 * ThunkInitLS (KERNEL32.43)
687 * A thunkbuffer link routine
688 * The thunkbuf looks like:
690 * 00: DWORD length ? don't know exactly
691 * 04: SEGPTR ptr ? where does it point to?
692 * The pointer ptr is written into the first DWORD of 'thunk'.
693 * (probably correctly implemented)
694 * [ok probably]
695 * RETURNS
696 * segmented pointer to thunk?
698 DWORD WINAPI ThunkInitLS(
699 LPDWORD thunk, /* [in] win32 thunk */
700 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
701 DWORD len, /* [in] thkbuffer length */
702 LPCSTR dll16, /* [in] name of win16 dll */
703 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
705 LPDWORD addr;
707 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
708 return 0;
710 if (!addr[1])
711 return 0;
712 *thunk = addr[1];
714 return addr[1];
717 /***********************************************************************
718 * Common32ThkLS (KERNEL32.45)
720 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
721 * style thunks. The basic difference is that the parameter conversion
722 * is done completely on the *16-bit* side here. Thus we do not call
723 * the 16-bit target directly, but call a common entry point instead.
724 * This entry function then calls the target according to the target
725 * number passed in the DI register.
727 * Input: EAX SEGPTR to the common 16-bit entry point
728 * CX offset in thunk table (target number * 4)
729 * DX error return value if execution fails (unclear???)
730 * EDX.HI number of DWORD parameters
732 * (Note that we need to move the thunk table offset from CX to DI !)
734 * The called 16-bit stub expects its stack to look like this:
735 * ...
736 * (esp+40) 32-bit arguments
737 * ...
738 * (esp+8) 32 byte of stack space available as buffer
739 * (esp) 8 byte return address for use with 0x66 lret
741 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
742 * and uses the EAX register to return a DWORD return value.
743 * Thus we need to use a special assembly glue routine
744 * (CallRegisterLongProc instead of CallRegisterShortProc).
746 * Finally, we return to the caller, popping the arguments off
747 * the stack. The number of arguments to be popped is returned
748 * in the BL register by the called 16-bit routine.
751 void WINAPI __regs_Common32ThkLS( CONTEXT86 *context )
753 CONTEXT86 context16;
754 DWORD argsize;
756 context16 = *context;
758 context16.SegFs = wine_get_fs();
759 context16.SegGs = wine_get_gs();
760 context16.Edi = LOWORD(context->Ecx);
761 context16.SegCs = HIWORD(context->Eax);
762 context16.Eip = LOWORD(context->Eax);
763 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
765 argsize = HIWORD(context->Edx) * 4;
767 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
768 if (context->Edx == context->Eip)
769 argsize = 6 * 4;
771 /* Note: the first 32 bytes we copy are just garbage from the 32-bit stack, in order to reserve
772 * the space. It is safe to do that since the register function prefix has reserved
773 * a lot more space than that below context->Esp.
775 WOWCallback16Ex( 0, WCB16_REGS, argsize + 32, (LPBYTE)context->Esp - 32, (DWORD *)&context16 );
776 context->Eax = context16.Eax;
778 /* Clean up caller's stack frame */
779 context->Esp += LOBYTE(context16.Ebx);
781 #ifdef DEFINE_REGS_ENTRYPOINT
782 DEFINE_REGS_ENTRYPOINT( Common32ThkLS, 0, 0 )
783 #endif
785 /***********************************************************************
786 * OT_32ThkLSF (KERNEL32.40)
788 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
789 * argument processing is done on both the 32-bit and the 16-bit side:
790 * The 32-bit side prepares arguments, copying them onto the stack.
792 * When this routine is called, the first word on the stack is the
793 * number of argument bytes prepared by the 32-bit code, and EDX
794 * contains the 16-bit target address.
796 * The called 16-bit routine is another relaycode, doing further
797 * argument processing and then calling the real 16-bit target
798 * whose address is stored at [bp-04].
800 * The call proceeds using a normal CallRegisterShortProc.
801 * After return from the 16-bit relaycode, the arguments need
802 * to be copied *back* to the 32-bit stack, since the 32-bit
803 * relaycode processes output parameters.
805 * Note that we copy twice the number of arguments, since some of the
806 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
807 * arguments of the caller!
809 * (Note that this function seems only to be used for
810 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
812 void WINAPI __regs_OT_32ThkLSF( CONTEXT86 *context )
814 CONTEXT86 context16;
815 DWORD argsize;
817 context16 = *context;
819 context16.SegFs = wine_get_fs();
820 context16.SegGs = wine_get_gs();
821 context16.SegCs = HIWORD(context->Edx);
822 context16.Eip = LOWORD(context->Edx);
823 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
825 argsize = 2 * *(WORD *)context->Esp + 2;
827 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
828 context->Eax = context16.Eax;
829 context->Edx = context16.Edx;
831 /* Copy modified buffers back to 32-bit stack */
832 memcpy( (LPBYTE)context->Esp,
833 (LPBYTE)CURRENT_STACK16 - argsize, argsize );
835 context->Esp += LOWORD(context16.Esp) -
836 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
838 #ifdef DEFINE_REGS_ENTRYPOINT
839 DEFINE_REGS_ENTRYPOINT( OT_32ThkLSF, 0, 0 )
840 #endif
842 /***********************************************************************
843 * ThunkInitLSF (KERNEL32.41)
844 * A thunk setup routine.
845 * Expects a pointer to a preinitialized thunkbuffer in the first argument
846 * looking like:
847 *| 00..03: unknown (pointer, check _41, _43, _46)
848 *| 04: EB1E jmp +0x20
850 *| 06..23: unknown (space for replacement code, check .90)
852 *| 24:>E800000000 call offset 29
853 *| 29:>58 pop eax ( target of call )
854 *| 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
855 *| 2F: BAxxxxxxxx mov edx,xxxxxxxx
856 *| 34: 68yyyyyyyy push KERNEL32.90
857 *| 39: C3 ret
859 *| 3A: EB1E jmp +0x20
860 *| 3E ... 59: unknown (space for replacement code?)
861 *| 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
862 *| 5F: 5A pop edx
863 *| 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
864 *| 66: 52 push edx
865 *| 67: 68xxxxxxxx push xxxxxxxx
866 *| 6C: 68yyyyyyyy push KERNEL32.89
867 *| 71: C3 ret
868 *| 72: end?
869 * This function checks if the code is there, and replaces the yyyyyyyy entries
870 * by the functionpointers.
871 * The thunkbuf looks like:
873 *| 00: DWORD length ? don't know exactly
874 *| 04: SEGPTR ptr ? where does it point to?
875 * The segpointer ptr is written into the first DWORD of 'thunk'.
876 * [ok probably]
877 * RETURNS
878 * unclear, pointer to win16 thkbuffer?
880 LPVOID WINAPI ThunkInitLSF(
881 LPBYTE thunk, /* [in] win32 thunk */
882 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
883 DWORD len, /* [in] length of thkbuffer */
884 LPCSTR dll16, /* [in] name of win16 dll */
885 LPCSTR dll32 /* [in] name of win32 dll */
887 LPDWORD addr,addr2;
889 /* FIXME: add checks for valid code ... */
890 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
891 *(DWORD*)(thunk+0x35) = (DWORD)GetProcAddress(kernel32_handle,(LPSTR)90);
892 *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress(kernel32_handle,(LPSTR)89);
895 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
896 return 0;
898 addr2 = MapSL(addr[1]);
899 if (HIWORD(addr2))
900 *(DWORD*)thunk = (DWORD)addr2;
902 return addr2;
905 /***********************************************************************
906 * FT_PrologPrime (KERNEL32.89)
908 * This function is called from the relay code installed by
909 * ThunkInitLSF. It replaces the location from where it was
910 * called by a standard FT_Prolog call stub (which is 'primed'
911 * by inserting the correct target table pointer).
912 * Finally, it calls that stub.
914 * Input: ECX target number + flags (passed through to FT_Prolog)
915 * (ESP) offset of location where target table pointer
916 * is stored, relative to the start of the relay code
917 * (ESP+4) pointer to start of relay code
918 * (this is where the FT_Prolog call stub gets written to)
920 * Note: The two DWORD arguments get popped off the stack.
923 void WINAPI __regs_FT_PrologPrime( CONTEXT86 *context )
925 DWORD targetTableOffset;
926 LPBYTE relayCode;
928 /* Compensate for the fact that the Wine register relay code thought
929 we were being called, although we were in fact jumped to */
930 context->Esp -= 4;
932 /* Write FT_Prolog call stub */
933 targetTableOffset = stack32_pop(context);
934 relayCode = (LPBYTE)stack32_pop(context);
935 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
937 /* Jump to the call stub just created */
938 context->Eip = (DWORD)relayCode;
940 #ifdef DEFINE_REGS_ENTRYPOINT
941 DEFINE_REGS_ENTRYPOINT( FT_PrologPrime, 0, 0 )
942 #endif
944 /***********************************************************************
945 * QT_ThunkPrime (KERNEL32.90)
947 * This function corresponds to FT_PrologPrime, but installs a
948 * call stub for QT_Thunk instead.
950 * Input: (EBP-4) target number (passed through to QT_Thunk)
951 * EDX target table pointer location offset
952 * EAX start of relay code
955 void WINAPI __regs_QT_ThunkPrime( CONTEXT86 *context )
957 DWORD targetTableOffset;
958 LPBYTE relayCode;
960 /* Compensate for the fact that the Wine register relay code thought
961 we were being called, although we were in fact jumped to */
962 context->Esp -= 4;
964 /* Write QT_Thunk call stub */
965 targetTableOffset = context->Edx;
966 relayCode = (LPBYTE)context->Eax;
967 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
969 /* Jump to the call stub just created */
970 context->Eip = (DWORD)relayCode;
972 #ifdef DEFINE_REGS_ENTRYPOINT
973 DEFINE_REGS_ENTRYPOINT( QT_ThunkPrime, 0, 0 )
974 #endif
976 /***********************************************************************
977 * ThunkInitSL (KERNEL32.46)
978 * Another thunkbuf link routine.
979 * The start of the thunkbuf looks like this:
980 * 00: DWORD length
981 * 04: SEGPTR address for thunkbuffer pointer
982 * [ok probably]
984 * RETURNS
985 * Nothing.
987 VOID WINAPI ThunkInitSL(
988 LPBYTE thunk, /* [in] start of thunkbuffer */
989 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
990 DWORD len, /* [in] length of thunkbuffer */
991 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
992 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
994 LPDWORD addr;
996 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
997 return;
999 *(DWORD*)MapSL(addr[1]) = (DWORD)thunk;
1002 /**********************************************************************
1003 * SSInit (KERNEL.700)
1004 * RETURNS
1005 * TRUE for success.
1007 BOOL WINAPI SSInit16(void)
1009 return TRUE;
1012 /**********************************************************************
1013 * SSOnBigStack (KERNEL32.87)
1014 * Check if thunking is initialized (ss selector set up etc.)
1015 * We do that differently, so just return TRUE.
1016 * [ok]
1017 * RETURNS
1018 * TRUE for success.
1020 BOOL WINAPI SSOnBigStack(void)
1022 TRACE("Yes, thunking is initialized\n");
1023 return TRUE;
1026 /**********************************************************************
1027 * SSConfirmSmallStack (KERNEL.704)
1029 * Abort if not on small stack.
1031 * This must be a register routine as it has to preserve *all* registers.
1033 void WINAPI SSConfirmSmallStack( CONTEXT86 *context )
1035 /* We are always on the small stack while in 16-bit code ... */
1038 /**********************************************************************
1039 * SSCall (KERNEL32.88)
1040 * One of the real thunking functions. This one seems to be for 32<->32
1041 * thunks. It should probably be capable of crossing processboundaries.
1043 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
1044 * [ok]
1046 * RETURNS
1047 * Thunked function result.
1049 DWORD WINAPIV SSCall(
1050 DWORD nr, /* [in] number of argument bytes */
1051 DWORD flags, /* [in] FIXME: flags ? */
1052 FARPROC fun, /* [in] function to call */
1053 ... /* [in/out] arguments */
1055 DWORD i,ret;
1056 DWORD *args = ((DWORD *)&fun) + 1;
1058 if(TRACE_ON(thunk))
1060 DPRINTF("(%d,0x%08x,%p,[",nr,flags,fun);
1061 for (i=0;i<nr/4;i++)
1062 DPRINTF("0x%08x,",args[i]);
1063 DPRINTF("])\n");
1065 switch (nr) {
1066 case 0: ret = fun();
1067 break;
1068 case 4: ret = fun(args[0]);
1069 break;
1070 case 8: ret = fun(args[0],args[1]);
1071 break;
1072 case 12: ret = fun(args[0],args[1],args[2]);
1073 break;
1074 case 16: ret = fun(args[0],args[1],args[2],args[3]);
1075 break;
1076 case 20: ret = fun(args[0],args[1],args[2],args[3],args[4]);
1077 break;
1078 case 24: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
1079 break;
1080 case 28: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
1081 break;
1082 case 32: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
1083 break;
1084 case 36: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
1085 break;
1086 case 40: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
1087 break;
1088 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]);
1089 break;
1090 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]);
1091 break;
1092 default:
1093 WARN("Unsupported nr of arguments, %d\n",nr);
1094 ret = 0;
1095 break;
1098 TRACE(" returning %d ...\n",ret);
1099 return ret;
1102 /**********************************************************************
1103 * W32S_BackTo32 (KERNEL32.51)
1105 void WINAPI __regs_W32S_BackTo32( CONTEXT86 *context )
1107 LPDWORD stack = (LPDWORD)context->Esp;
1108 FARPROC proc = (FARPROC)context->Eip;
1110 context->Eax = proc( stack[1], stack[2], stack[3], stack[4], stack[5],
1111 stack[6], stack[7], stack[8], stack[9], stack[10] );
1113 context->Eip = stack32_pop(context);
1115 #ifdef DEFINE_REGS_ENTRYPOINT
1116 DEFINE_REGS_ENTRYPOINT( W32S_BackTo32, 0, 0 )
1117 #endif
1119 /**********************************************************************
1120 * AllocSLCallback (KERNEL32.@)
1122 * Allocate a 16->32 callback.
1124 * NOTES
1125 * Win95 uses some structchains for callbacks. It allocates them
1126 * in blocks of 100 entries, size 32 bytes each, layout:
1127 * blockstart:
1128 *| 0: PTR nextblockstart
1129 *| 4: entry *first;
1130 *| 8: WORD sel ( start points to blockstart)
1131 *| A: WORD unknown
1132 * 100xentry:
1133 *| 00..17: Code
1134 *| 18: PDB *owning_process;
1135 *| 1C: PTR blockstart
1137 * We ignore this for now. (Just a note for further developers)
1138 * FIXME: use this method, so we don't waste selectors...
1140 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
1141 * the 0x66 prefix switches from word->long registers.
1143 *| 665A pop edx
1144 *| 6668x arg2 x pushl <arg2>
1145 *| 6652 push edx
1146 *| EAx arg1 x jmpf <arg1>
1148 * returns the startaddress of this thunk.
1150 * Note, that they look very similar to the ones allocates by THUNK_Alloc.
1151 * RETURNS
1152 * A segmented pointer to the start of the thunk
1154 DWORD WINAPI
1155 AllocSLCallback(
1156 DWORD finalizer, /* [in] Finalizer function */
1157 DWORD callback /* [in] Callback function */
1159 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
1160 WORD sel;
1162 x=thunk;
1163 *x++=0x66;*x++=0x5a; /* popl edx */
1164 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
1165 *x++=0x66;*x++=0x52; /* pushl edx */
1166 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
1168 *(DWORD*)(thunk+18) = GetCurrentProcessId();
1170 sel = SELECTOR_AllocBlock( thunk, 32, WINE_LDT_FLAGS_CODE );
1171 return (sel<<16)|0;
1174 /**********************************************************************
1175 * FreeSLCallback (KERNEL32.@)
1176 * Frees the specified 16->32 callback
1178 * RETURNS
1179 * Nothing.
1181 void WINAPI
1182 FreeSLCallback(
1183 DWORD x /* [in] 16 bit callback (segmented pointer?) */
1185 FIXME("(0x%08x): stub\n",x);
1188 /**********************************************************************
1189 * AllocMappedBuffer (KERNEL32.38)
1191 * This is an undocumented KERNEL32 function that
1192 * SMapLS's a GlobalAlloc'ed buffer.
1194 * RETURNS
1195 * EDI register: pointer to buffer
1197 * NOTES
1198 * The buffer is preceded by 8 bytes:
1199 * ...
1200 * edi+0 buffer
1201 * edi-4 SEGPTR to buffer
1202 * edi-8 some magic Win95 needs for SUnMapLS
1203 * (we use it for the memory handle)
1205 * The SEGPTR is used by the caller!
1207 void WINAPI __regs_AllocMappedBuffer(
1208 CONTEXT86 *context /* [in] EDI register: size of buffer to allocate */
1210 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
1211 DWORD *buffer = GlobalLock(handle);
1212 DWORD ptr = 0;
1214 if (buffer)
1215 if (!(ptr = MapLS(buffer + 2)))
1217 GlobalUnlock(handle);
1218 GlobalFree(handle);
1221 if (!ptr)
1222 context->Eax = context->Edi = 0;
1223 else
1225 buffer[0] = (DWORD)handle;
1226 buffer[1] = ptr;
1228 context->Eax = ptr;
1229 context->Edi = (DWORD)(buffer + 2);
1232 #ifdef DEFINE_REGS_ENTRYPOINT
1233 DEFINE_REGS_ENTRYPOINT( AllocMappedBuffer, 0, 0 )
1234 #endif
1236 /**********************************************************************
1237 * FreeMappedBuffer (KERNEL32.39)
1239 * Free a buffer allocated by AllocMappedBuffer
1241 * RETURNS
1242 * Nothing.
1244 void WINAPI __regs_FreeMappedBuffer(
1245 CONTEXT86 *context /* [in] EDI register: pointer to buffer */
1247 if (context->Edi)
1249 DWORD *buffer = (DWORD *)context->Edi - 2;
1251 UnMapLS(buffer[1]);
1253 GlobalUnlock((HGLOBAL)buffer[0]);
1254 GlobalFree((HGLOBAL)buffer[0]);
1257 #ifdef DEFINE_REGS_ENTRYPOINT
1258 DEFINE_REGS_ENTRYPOINT( FreeMappedBuffer, 0, 0 )
1259 #endif
1261 /**********************************************************************
1262 * GetTEBSelectorFS (KERNEL.475)
1263 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1265 void WINAPI GetTEBSelectorFS16(void)
1267 CURRENT_STACK16->fs = wine_get_fs();
1270 /**********************************************************************
1271 * IsPeFormat (KERNEL.431)
1273 * Determine if a file is a PE format executable.
1275 * RETURNS
1276 * TRUE, if it is.
1277 * FALSE if the file could not be opened or is not a PE file.
1279 * NOTES
1280 * If fn is given as NULL then the function expects hf16 to be valid.
1282 BOOL16 WINAPI IsPeFormat16(
1283 LPSTR fn, /* [in] Filename to the executable */
1284 HFILE16 hf16) /* [in] An open file handle */
1286 BOOL ret = FALSE;
1287 IMAGE_DOS_HEADER mzh;
1288 OFSTRUCT ofs;
1289 DWORD xmagic;
1291 if (fn) hf16 = OpenFile16(fn,&ofs,OF_READ);
1292 if (hf16 == HFILE_ERROR16) return FALSE;
1293 _llseek16(hf16,0,SEEK_SET);
1294 if (sizeof(mzh)!=_lread16(hf16,&mzh,sizeof(mzh))) goto done;
1295 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) goto done;
1296 _llseek16(hf16,mzh.e_lfanew,SEEK_SET);
1297 if (sizeof(DWORD)!=_lread16(hf16,&xmagic,sizeof(DWORD))) goto done;
1298 ret = (xmagic == IMAGE_NT_SIGNATURE);
1299 done:
1300 _lclose16(hf16);
1301 return ret;
1305 /***********************************************************************
1306 * K32Thk1632Prolog (KERNEL32.@)
1308 void WINAPI __regs_K32Thk1632Prolog( CONTEXT86 *context )
1310 LPBYTE code = (LPBYTE)context->Eip - 5;
1312 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1313 of 16->32 thunks instead of using one of the standard methods!
1314 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1315 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1316 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1317 bypassed, which means it will crash the next time the 32-bit OLE
1318 code thunks down again to 16-bit (this *will* happen!).
1320 The following hack tries to recognize this situation.
1321 This is possible since the called stubs in OLECLI32/OLESVR32 all
1322 look exactly the same:
1323 00 E8xxxxxxxx call K32Thk1632Prolog
1324 05 FF55FC call [ebp-04]
1325 08 E8xxxxxxxx call K32Thk1632Epilog
1326 0D 66CB retf
1328 If we recognize this situation, we try to simulate the actions
1329 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1330 to our 32-bit stack, creating a proper STACK16FRAME and
1331 updating cur_stack. */
1333 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1334 && code[13] == 0x66 && code[14] == 0xCB)
1336 DWORD argSize = context->Ebp - context->Esp;
1337 char *stack16 = (char *)context->Esp - 4;
1338 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1339 STACK32FRAME *frame32 = (STACK32FRAME *)NtCurrentTeb()->WOW32Reserved;
1340 char *stack32 = (char *)frame32 - argSize;
1341 WORD stackSel = SELECTOROF(frame32->frame16);
1342 DWORD stackBase = GetSelectorBase(stackSel);
1344 TRACE("before SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1345 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1347 memset(frame16, '\0', sizeof(STACK16FRAME));
1348 frame16->frame32 = frame32;
1349 frame16->ebp = context->Ebp;
1351 memcpy(stack32, stack16, argSize);
1352 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR(stackSel, (DWORD)frame16 - stackBase);
1354 context->Esp = (DWORD)stack32 + 4;
1355 context->Ebp = context->Esp + argSize;
1357 TRACE("after SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1358 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1361 /* entry_point is never used again once the entry point has
1362 been called. Thus we re-use it to hold the Win16Lock count */
1363 ReleaseThunkLock(&CURRENT_STACK16->entry_point);
1365 #ifdef DEFINE_REGS_ENTRYPOINT
1366 DEFINE_REGS_ENTRYPOINT( K32Thk1632Prolog, 0, 0 )
1367 #endif
1369 /***********************************************************************
1370 * K32Thk1632Epilog (KERNEL32.@)
1372 void WINAPI __regs_K32Thk1632Epilog( CONTEXT86 *context )
1374 LPBYTE code = (LPBYTE)context->Eip - 13;
1376 RestoreThunkLock(CURRENT_STACK16->entry_point);
1378 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1380 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1381 && code[13] == 0x66 && code[14] == 0xCB)
1383 STACK16FRAME *frame16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1384 char *stack16 = (char *)(frame16 + 1);
1385 DWORD argSize = frame16->ebp - (DWORD)stack16;
1386 char *stack32 = (char *)frame16->frame32 - argSize;
1388 DWORD nArgsPopped = context->Esp - (DWORD)stack32;
1390 TRACE("before SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1391 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1393 NtCurrentTeb()->WOW32Reserved = frame16->frame32;
1395 context->Esp = (DWORD)stack16 + nArgsPopped;
1396 context->Ebp = frame16->ebp;
1398 TRACE("after SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1399 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1402 #ifdef DEFINE_REGS_ENTRYPOINT
1403 DEFINE_REGS_ENTRYPOINT( K32Thk1632Epilog, 0, 0 )
1404 #endif
1406 /*********************************************************************
1407 * PK16FNF [KERNEL32.91]
1409 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1410 * string buffer with the 8.3 filename of a recently loaded 16-bit
1411 * module. It is unknown exactly what modules trigger this
1412 * mechanism or what purpose this serves. Win98 Explorer (and
1413 * probably also Win95 with IE 4 shell integration) calls this
1414 * several times during initialization.
1416 * FIXME: find out what this really does and make it work.
1418 void WINAPI PK16FNF(LPSTR strPtr)
1420 FIXME("(%p): stub\n", strPtr);
1422 /* fill in a fake filename that'll be easy to recognize */
1423 strcpy(strPtr, "WINESTUB.FIX");
1426 /***********************************************************************
1427 * 16->32 Flat Thunk routines:
1430 /***********************************************************************
1431 * ThunkConnect16 (KERNEL.651)
1432 * Connects a 32bit and a 16bit thunkbuffer.
1434 UINT WINAPI ThunkConnect16(
1435 LPSTR module16, /* [in] name of win16 dll */
1436 LPSTR module32, /* [in] name of win32 dll */
1437 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
1438 DWORD dwReason, /* [in] initialisation argument */
1439 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
1440 LPSTR thunkfun32, /* [in] win32 thunkfunction */
1441 WORD cs /* [in] CS of win16 dll */
1443 BOOL directionSL;
1445 if (!strncmp(TD->magic, "SL01", 4))
1447 directionSL = TRUE;
1449 TRACE("SL01 thunk %s (%p) -> %s (%s), Reason: %d\n",
1450 module16, TD, module32, thunkfun32, dwReason);
1452 else if (!strncmp(TD->magic, "LS01", 4))
1454 directionSL = FALSE;
1456 TRACE("LS01 thunk %s (%p) <- %s (%s), Reason: %d\n",
1457 module16, TD, module32, thunkfun32, dwReason);
1459 else
1461 ERR("Invalid magic %c%c%c%c\n",
1462 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
1463 return 0;
1466 switch (dwReason)
1468 case DLL_PROCESS_ATTACH:
1469 if (directionSL)
1471 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
1472 struct ThunkDataSL *SL = SL16->fpData;
1474 if (SL == NULL)
1476 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
1478 SL->common = SL16->common;
1479 SL->flags1 = SL16->flags1;
1480 SL->flags2 = SL16->flags2;
1482 SL->apiDB = MapSL(SL16->apiDatabase);
1483 SL->targetDB = NULL;
1485 lstrcpynA(SL->pszDll16, module16, 255);
1486 lstrcpynA(SL->pszDll32, module32, 255);
1488 /* We should create a SEGPTR to the ThunkDataSL,
1489 but since the contents are not in the original format,
1490 any access to this by 16-bit code would crash anyway. */
1491 SL16->spData = 0;
1492 SL16->fpData = SL;
1496 if (SL->flags2 & 0x80000000)
1498 TRACE("Preloading 32-bit library\n");
1499 LoadLibraryA(module32);
1502 else
1504 /* nothing to do */
1506 break;
1508 case DLL_PROCESS_DETACH:
1509 /* FIXME: cleanup */
1510 break;
1513 return 1;
1517 /***********************************************************************
1518 * C16ThkSL (KERNEL.630)
1521 void WINAPI C16ThkSL(CONTEXT86 *context)
1523 LPBYTE stub = MapSL(context->Eax), x = stub;
1524 WORD cs = wine_get_cs();
1525 WORD ds = wine_get_ds();
1527 /* We produce the following code:
1529 * mov ax, __FLATDS
1530 * mov es, ax
1531 * movzx ecx, cx
1532 * mov edx, es:[ecx + $EDX]
1533 * push bp
1534 * push edx
1535 * push dx
1536 * push edx
1537 * call __FLATCS:__wine_call_from_16_thunk
1540 *x++ = 0xB8; *(WORD *)x = ds; x += sizeof(WORD);
1541 *x++ = 0x8E; *x++ = 0xC0;
1542 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1543 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1544 *x++ = 0x91; *(DWORD *)x = context->Edx; x += sizeof(DWORD);
1546 *x++ = 0x55;
1547 *x++ = 0x66; *x++ = 0x52;
1548 *x++ = 0x52;
1549 *x++ = 0x66; *x++ = 0x52;
1550 *x++ = 0x66; *x++ = 0x9A;
1551 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1552 *(WORD *)x = cs; x += sizeof(WORD);
1554 /* Jump to the stub code just created */
1555 context->Eip = LOWORD(context->Eax);
1556 context->SegCs = HIWORD(context->Eax);
1558 /* Since C16ThkSL got called by a jmp, we need to leave the
1559 original return address on the stack */
1560 context->Esp -= 4;
1563 /***********************************************************************
1564 * C16ThkSL01 (KERNEL.631)
1567 void WINAPI C16ThkSL01(CONTEXT86 *context)
1569 LPBYTE stub = MapSL(context->Eax), x = stub;
1571 if (stub)
1573 struct ThunkDataSL16 *SL16 = MapSL(context->Edx);
1574 struct ThunkDataSL *td = SL16->fpData;
1576 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), (LPCSTR)631);
1577 WORD cs = wine_get_cs();
1579 if (!td)
1581 ERR("ThunkConnect16 was not called!\n");
1582 return;
1585 TRACE("Creating stub for ThunkDataSL %p\n", td);
1588 /* We produce the following code:
1590 * xor eax, eax
1591 * mov edx, $td
1592 * call C16ThkSL01
1593 * push bp
1594 * push edx
1595 * push dx
1596 * push edx
1597 * call __FLATCS:__wine_call_from_16_thunk
1600 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1601 *x++ = 0x66; *x++ = 0xBA; *(void **)x = td; x += sizeof(void *);
1602 *x++ = 0x9A; *(DWORD *)x = procAddress; x += sizeof(DWORD);
1604 *x++ = 0x55;
1605 *x++ = 0x66; *x++ = 0x52;
1606 *x++ = 0x52;
1607 *x++ = 0x66; *x++ = 0x52;
1608 *x++ = 0x66; *x++ = 0x9A;
1609 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1610 *(WORD *)x = cs; x += sizeof(WORD);
1612 /* Jump to the stub code just created */
1613 context->Eip = LOWORD(context->Eax);
1614 context->SegCs = HIWORD(context->Eax);
1616 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1617 original return address on the stack */
1618 context->Esp -= 4;
1620 else
1622 struct ThunkDataSL *td = (struct ThunkDataSL *)context->Edx;
1623 DWORD targetNr = LOWORD(context->Ecx) / 4;
1624 struct SLTargetDB *tdb;
1626 TRACE("Process %08x calling target %d of ThunkDataSL %p\n",
1627 GetCurrentProcessId(), targetNr, td);
1629 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1630 if (tdb->process == GetCurrentProcessId())
1631 break;
1633 if (!tdb)
1635 TRACE("Loading 32-bit library %s\n", td->pszDll32);
1636 LoadLibraryA(td->pszDll32);
1638 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1639 if (tdb->process == GetCurrentProcessId())
1640 break;
1643 if (tdb)
1645 context->Edx = tdb->targetTable[targetNr];
1647 TRACE("Call target is %08x\n", context->Edx);
1649 else
1651 WORD *stack = MapSL( MAKESEGPTR(context->SegSs, LOWORD(context->Esp)) );
1652 context->Edx = (context->Edx & ~0xffff) | HIWORD(td->apiDB[targetNr].errorReturnValue);
1653 context->Eax = (context->Eax & ~0xffff) | LOWORD(td->apiDB[targetNr].errorReturnValue);
1654 context->Eip = stack[2];
1655 context->SegCs = stack[3];
1656 context->Esp += td->apiDB[targetNr].nrArgBytes + 4;
1658 ERR("Process %08x did not ThunkConnect32 %s to %s\n",
1659 GetCurrentProcessId(), td->pszDll32, td->pszDll16);
1665 /***********************************************************************
1666 * 16<->32 Thunklet/Callback API:
1669 #include "pshpack1.h"
1670 typedef struct _THUNKLET
1672 BYTE prefix_target;
1673 BYTE pushl_target;
1674 DWORD target;
1676 BYTE prefix_relay;
1677 BYTE pushl_relay;
1678 DWORD relay;
1680 BYTE jmp_glue;
1681 DWORD glue;
1683 BYTE type;
1684 HINSTANCE16 owner;
1685 struct _THUNKLET *next;
1686 } THUNKLET;
1687 #include "poppack.h"
1689 #define THUNKLET_TYPE_LS 1
1690 #define THUNKLET_TYPE_SL 2
1692 static HANDLE ThunkletHeap = 0;
1693 static WORD ThunkletCodeSel;
1694 static THUNKLET *ThunkletAnchor = NULL;
1696 static FARPROC ThunkletSysthunkGlueLS = 0;
1697 static SEGPTR ThunkletSysthunkGlueSL = 0;
1699 static FARPROC ThunkletCallbackGlueLS = 0;
1700 static SEGPTR ThunkletCallbackGlueSL = 0;
1703 /* map a thunk allocated on ThunkletHeap to a 16-bit pointer */
1704 static inline SEGPTR get_segptr( void *thunk )
1706 if (!thunk) return 0;
1707 return MAKESEGPTR( ThunkletCodeSel, (char *)thunk - (char *)ThunkletHeap );
1710 /***********************************************************************
1711 * THUNK_Init
1713 static BOOL THUNK_Init(void)
1715 LPBYTE thunk;
1717 ThunkletHeap = HeapCreate( 0, 0x10000, 0x10000 );
1718 if (!ThunkletHeap) return FALSE;
1720 ThunkletCodeSel = SELECTOR_AllocBlock( ThunkletHeap, 0x10000, WINE_LDT_FLAGS_CODE );
1722 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1723 if (!thunk) return FALSE;
1725 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1726 *thunk++ = 0x58; /* popl eax */
1727 *thunk++ = 0xC3; /* ret */
1729 ThunkletSysthunkGlueSL = get_segptr( thunk );
1730 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1731 *thunk++ = 0xCB; /* lret */
1733 return TRUE;
1736 /***********************************************************************
1737 * SetThunkletCallbackGlue (KERNEL.560)
1739 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1741 ThunkletCallbackGlueLS = glueLS;
1742 ThunkletCallbackGlueSL = glueSL;
1746 /***********************************************************************
1747 * THUNK_FindThunklet
1749 static THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1750 DWORD glue, BYTE type )
1752 THUNKLET *thunk;
1754 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1755 if ( thunk->type == type
1756 && thunk->target == target
1757 && thunk->relay == relay
1758 && ( type == THUNKLET_TYPE_LS ?
1759 ( thunk->glue == glue - (DWORD)&thunk->type )
1760 : ( thunk->glue == glue ) ) )
1761 return thunk;
1763 return NULL;
1766 /***********************************************************************
1767 * THUNK_AllocLSThunklet
1769 static FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1770 FARPROC glue, HTASK16 owner )
1772 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1773 THUNKLET_TYPE_LS );
1774 if (!thunk)
1776 TDB *pTask = GlobalLock16( owner );
1778 if (!ThunkletHeap) THUNK_Init();
1779 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1780 return 0;
1782 thunk->prefix_target = thunk->prefix_relay = 0x90;
1783 thunk->pushl_target = thunk->pushl_relay = 0x68;
1784 thunk->jmp_glue = 0xE9;
1786 thunk->target = (DWORD)target;
1787 thunk->relay = relay;
1788 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1790 thunk->type = THUNKLET_TYPE_LS;
1791 thunk->owner = pTask? pTask->hInstance : 0;
1793 thunk->next = ThunkletAnchor;
1794 ThunkletAnchor = thunk;
1797 return (FARPROC)thunk;
1800 /***********************************************************************
1801 * THUNK_AllocSLThunklet
1803 static SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1804 SEGPTR glue, HTASK16 owner )
1806 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1807 THUNKLET_TYPE_SL );
1808 if (!thunk)
1810 TDB *pTask = GlobalLock16( owner );
1812 if (!ThunkletHeap) THUNK_Init();
1813 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1814 return 0;
1816 thunk->prefix_target = thunk->prefix_relay = 0x66;
1817 thunk->pushl_target = thunk->pushl_relay = 0x68;
1818 thunk->jmp_glue = 0xEA;
1820 thunk->target = (DWORD)target;
1821 thunk->relay = relay;
1822 thunk->glue = (DWORD)glue;
1824 thunk->type = THUNKLET_TYPE_SL;
1825 thunk->owner = pTask? pTask->hInstance : 0;
1827 thunk->next = ThunkletAnchor;
1828 ThunkletAnchor = thunk;
1831 return get_segptr( thunk );
1834 /**********************************************************************
1835 * IsLSThunklet
1837 static BOOL16 IsLSThunklet( THUNKLET *thunk )
1839 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1840 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1841 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1844 /**********************************************************************
1845 * IsSLThunklet (KERNEL.612)
1847 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1849 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1850 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1851 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1856 /***********************************************************************
1857 * AllocLSThunkletSysthunk (KERNEL.607)
1859 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1860 FARPROC relay, DWORD dummy )
1862 if (!ThunkletSysthunkGlueLS) THUNK_Init();
1863 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1864 ThunkletSysthunkGlueLS, GetCurrentTask() );
1867 /***********************************************************************
1868 * AllocSLThunkletSysthunk (KERNEL.608)
1870 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1871 SEGPTR relay, DWORD dummy )
1873 if (!ThunkletSysthunkGlueSL) THUNK_Init();
1874 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1875 ThunkletSysthunkGlueSL, GetCurrentTask() );
1879 /***********************************************************************
1880 * AllocLSThunkletCallbackEx (KERNEL.567)
1882 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1883 DWORD relay, HTASK16 task )
1885 THUNKLET *thunk = MapSL( target );
1886 if ( !thunk ) return NULL;
1888 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1889 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1890 return (FARPROC)thunk->target;
1892 return THUNK_AllocLSThunklet( target, relay,
1893 ThunkletCallbackGlueLS, task );
1896 /***********************************************************************
1897 * AllocSLThunkletCallbackEx (KERNEL.568)
1899 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1900 DWORD relay, HTASK16 task )
1902 THUNKLET *thunk = (THUNKLET *)target;
1903 if ( !thunk ) return 0;
1905 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1906 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1907 return (SEGPTR)thunk->target;
1909 return THUNK_AllocSLThunklet( target, relay,
1910 ThunkletCallbackGlueSL, task );
1913 /***********************************************************************
1914 * AllocLSThunkletCallback (KERNEL.561)
1915 * AllocLSThunkletCallback_dup (KERNEL.606)
1917 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1919 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1922 /***********************************************************************
1923 * AllocSLThunkletCallback (KERNEL.562)
1924 * AllocSLThunkletCallback_dup (KERNEL.605)
1926 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1928 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1931 /***********************************************************************
1932 * FindLSThunkletCallback (KERNEL.563)
1933 * FindLSThunkletCallback_dup (KERNEL.609)
1935 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1937 THUNKLET *thunk = MapSL( target );
1938 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1939 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1940 return (FARPROC)thunk->target;
1942 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1943 (DWORD)ThunkletCallbackGlueLS,
1944 THUNKLET_TYPE_LS );
1945 return (FARPROC)thunk;
1948 /***********************************************************************
1949 * FindSLThunkletCallback (KERNEL.564)
1950 * FindSLThunkletCallback_dup (KERNEL.610)
1952 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1954 THUNKLET *thunk = (THUNKLET *)target;
1955 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1956 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1957 return (SEGPTR)thunk->target;
1959 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1960 (DWORD)ThunkletCallbackGlueSL,
1961 THUNKLET_TYPE_SL );
1962 return get_segptr( thunk );
1966 /***********************************************************************
1967 * FreeThunklet (KERNEL.611)
1969 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1971 return FALSE;
1975 /***********************************************************************
1976 * Callback Client API
1979 #define N_CBC_FIXED 20
1980 #define N_CBC_VARIABLE 10
1981 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1983 static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
1984 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1986 /***********************************************************************
1987 * RegisterCBClient (KERNEL.619)
1989 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1990 SEGPTR relay16, FARPROC *relay32 )
1992 /* Search for free Callback ID */
1993 if ( wCBCId == -1 )
1994 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1995 if ( !CBClientRelay16[ wCBCId ] )
1996 break;
1998 /* Register Callback ID */
1999 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
2001 CBClientRelay16[ wCBCId ] = relay16;
2002 CBClientRelay32[ wCBCId ] = relay32;
2004 else
2005 wCBCId = 0;
2007 return wCBCId;
2010 /***********************************************************************
2011 * UnRegisterCBClient (KERNEL.622)
2013 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
2014 SEGPTR relay16, FARPROC *relay32 )
2016 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
2017 && CBClientRelay16[ wCBCId ] == relay16
2018 && CBClientRelay32[ wCBCId ] == relay32 )
2020 CBClientRelay16[ wCBCId ] = 0;
2021 CBClientRelay32[ wCBCId ] = 0;
2023 else
2024 wCBCId = 0;
2026 return wCBCId;
2030 /***********************************************************************
2031 * InitCBClient (KERNEL.623)
2033 void WINAPI InitCBClient16( FARPROC glueLS )
2035 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
2036 SEGPTR glueSL = (SEGPTR)GetProcAddress16( kernel, (LPCSTR)604 );
2038 SetThunkletCallbackGlue16( glueLS, glueSL );
2041 /***********************************************************************
2042 * CBClientGlueSL (KERNEL.604)
2044 void WINAPI CBClientGlueSL( CONTEXT86 *context )
2046 /* Create stack frame */
2047 SEGPTR stackSeg = stack16_push( 12 );
2048 LPWORD stackLin = MapSL( stackSeg );
2049 SEGPTR glue, *glueTab;
2051 stackLin[3] = (WORD)context->Ebp;
2052 stackLin[2] = (WORD)context->Esi;
2053 stackLin[1] = (WORD)context->Edi;
2054 stackLin[0] = (WORD)context->SegDs;
2056 context->Ebp = OFFSETOF( stackSeg ) + 6;
2057 context->Esp = OFFSETOF( stackSeg ) - 4;
2058 context->SegGs = 0;
2060 /* Jump to 16-bit relay code */
2061 glueTab = MapSL( CBClientRelay16[ stackLin[5] ] );
2062 glue = glueTab[ stackLin[4] ];
2063 context->SegCs = SELECTOROF( glue );
2064 context->Eip = OFFSETOF ( glue );
2067 /***********************************************************************
2068 * CBClientThunkSL (KERNEL.620)
2070 extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, WORD *stackLin, DWORD *esi );
2071 void WINAPI CBClientThunkSL( CONTEXT86 *context )
2073 /* Call 32-bit relay code */
2075 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2076 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
2078 /* fill temporary area for the asm code (see comments in winebuild) */
2079 SEGPTR stack = stack16_push( 12 );
2080 LPWORD stackLin = MapSL(stack);
2081 /* stackLin[0] and stackLin[1] reserved for the 32-bit stack ptr */
2082 stackLin[2] = wine_get_ss();
2083 stackLin[3] = 0;
2084 stackLin[4] = OFFSETOF(stack) + 12;
2085 stackLin[5] = SELECTOROF(stack);
2086 stackLin[6] = OFFSETOF(CALL32_CBClientEx_RetAddr); /* overwrite return address */
2087 stackLin[7] = SELECTOROF(CALL32_CBClientEx_RetAddr);
2088 context->Eax = CALL32_CBClient( proc, args, stackLin + 4, &context->Esi );
2089 stack16_pop( 12 );
2092 /***********************************************************************
2093 * CBClientThunkSLEx (KERNEL.621)
2095 extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, WORD *stackLin, DWORD *esi, INT *nArgs );
2096 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
2098 /* Call 32-bit relay code */
2100 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2101 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
2102 INT nArgs;
2103 LPWORD stackLin;
2105 /* fill temporary area for the asm code (see comments in winebuild) */
2106 SEGPTR stack = stack16_push( 24 );
2107 stackLin = MapSL(stack);
2108 stackLin[0] = OFFSETOF(stack) + 4;
2109 stackLin[1] = SELECTOROF(stack);
2110 stackLin[2] = wine_get_ds();
2111 stackLin[5] = OFFSETOF(stack) + 24;
2112 /* stackLin[6] and stackLin[7] reserved for the 32-bit stack ptr */
2113 stackLin[8] = wine_get_ss();
2114 stackLin[9] = 0;
2115 stackLin[10] = OFFSETOF(CALL32_CBClientEx_RetAddr);
2116 stackLin[11] = SELECTOROF(CALL32_CBClientEx_RetAddr);
2118 context->Eax = CALL32_CBClientEx( proc, args, stackLin, &context->Esi, &nArgs );
2119 stack16_pop( 24 );
2121 /* Restore registers saved by CBClientGlueSL */
2122 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
2123 context->Ebp = (context->Ebp & ~0xffff) | stackLin[3];
2124 context->Esi = (context->Esi & ~0xffff) | stackLin[2];
2125 context->Edi = (context->Edi & ~0xffff) | stackLin[1];
2126 context->SegDs = stackLin[0];
2127 context->Esp += 16+nArgs;
2129 /* Return to caller of CBClient thunklet */
2130 context->SegCs = stackLin[9];
2131 context->Eip = stackLin[8];
2135 /***********************************************************************
2136 * Get16DLLAddress (KERNEL32.@)
2138 * This function is used by a Win32s DLL if it wants to call a Win16 function.
2139 * A 16:16 segmented pointer to the function is returned.
2140 * Written without any docu.
2142 SEGPTR WINAPI Get16DLLAddress(HMODULE16 handle, LPSTR func_name)
2144 static WORD code_sel32;
2145 FARPROC16 proc_16;
2146 LPBYTE thunk;
2148 if (!code_sel32)
2150 if (!ThunkletHeap) THUNK_Init();
2151 code_sel32 = SELECTOR_AllocBlock( ThunkletHeap, 0x10000,
2152 WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
2153 if (!code_sel32) return 0;
2155 if (!(thunk = HeapAlloc( ThunkletHeap, 0, 32 ))) return 0;
2157 if (!handle) handle = GetModuleHandle16("WIN32S16");
2158 proc_16 = GetProcAddress16(handle, func_name);
2160 /* movl proc_16, $edx */
2161 *thunk++ = 0xba;
2162 *(FARPROC16 *)thunk = proc_16;
2163 thunk += sizeof(FARPROC16);
2165 /* jmpl QT_Thunk */
2166 *thunk++ = 0xea;
2167 *(FARPROC *)thunk = GetProcAddress(kernel32_handle,"QT_Thunk");
2168 thunk += sizeof(FARPROC16);
2169 *(WORD *)thunk = wine_get_cs();
2171 return MAKESEGPTR( code_sel32, (char *)thunk - (char *)ThunkletHeap );
2175 /***********************************************************************
2176 * GetWin16DOSEnv (KERNEL32.34)
2177 * Returns some internal value.... probably the default environment database?
2179 DWORD WINAPI GetWin16DOSEnv(void)
2181 FIXME("stub, returning 0\n");
2182 return 0;
2185 /**********************************************************************
2186 * GetPK16SysVar (KERNEL32.92)
2188 LPVOID WINAPI GetPK16SysVar(void)
2190 static BYTE PK16SysVar[128];
2192 FIXME("()\n");
2193 return PK16SysVar;
2196 /**********************************************************************
2197 * CommonUnimpStub (KERNEL32.17)
2199 void WINAPI __regs_CommonUnimpStub( CONTEXT86 *context )
2201 FIXME("generic stub: %s\n", ((LPSTR)context->Eax ? (LPSTR)context->Eax : "?"));
2203 switch ((context->Ecx >> 4) & 0x0f)
2205 case 15: context->Eax = -1; break;
2206 case 14: context->Eax = 0x78; break;
2207 case 13: context->Eax = 0x32; break;
2208 case 1: context->Eax = 1; break;
2209 default: context->Eax = 0; break;
2212 context->Esp += (context->Ecx & 0x0f) * 4;
2214 #ifdef DEFINE_REGS_ENTRYPOINT
2215 DEFINE_REGS_ENTRYPOINT( CommonUnimpStub, 0, 0 )
2216 #endif
2218 /**********************************************************************
2219 * HouseCleanLogicallyDeadHandles (KERNEL32.33)
2221 void WINAPI HouseCleanLogicallyDeadHandles(void)
2223 /* Whatever this is supposed to do, our handles probably
2224 don't need it :-) */
2227 /**********************************************************************
2228 * @ (KERNEL32.100)
2230 BOOL WINAPI _KERNEL32_100(HANDLE threadid,DWORD exitcode,DWORD x)
2232 FIXME("(%p,%d,0x%08x): stub\n",threadid,exitcode,x);
2233 return TRUE;
2236 /**********************************************************************
2237 * @ (KERNEL32.99)
2239 * Checks whether the clock has to be switched from daylight
2240 * savings time to standard time or vice versa.
2243 DWORD WINAPI _KERNEL32_99(DWORD x)
2245 FIXME("(0x%08x): stub\n",x);
2246 return 1;
2250 /**********************************************************************
2251 * Catch (KERNEL.55)
2253 * Real prototype is:
2254 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
2256 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT86 *context )
2258 /* Note: we don't save the current ss, as the catch buffer is */
2259 /* only 9 words long. Hopefully no one will have the silly */
2260 /* idea to change the current stack before calling Throw()... */
2262 /* Windows uses:
2263 * lpbuf[0] = ip
2264 * lpbuf[1] = cs
2265 * lpbuf[2] = sp
2266 * lpbuf[3] = bp
2267 * lpbuf[4] = si
2268 * lpbuf[5] = di
2269 * lpbuf[6] = ds
2270 * lpbuf[7] = unused
2271 * lpbuf[8] = ss
2274 lpbuf[0] = LOWORD(context->Eip);
2275 lpbuf[1] = context->SegCs;
2276 /* Windows pushes 4 more words before saving sp */
2277 lpbuf[2] = LOWORD(context->Esp) - 4 * sizeof(WORD);
2278 lpbuf[3] = LOWORD(context->Ebp);
2279 lpbuf[4] = LOWORD(context->Esi);
2280 lpbuf[5] = LOWORD(context->Edi);
2281 lpbuf[6] = context->SegDs;
2282 lpbuf[7] = 0;
2283 lpbuf[8] = context->SegSs;
2284 context->Eax &= ~0xffff; /* Return 0 */
2288 /**********************************************************************
2289 * Throw (KERNEL.56)
2291 * Real prototype is:
2292 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
2294 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT86 *context )
2296 STACK16FRAME *pFrame;
2297 STACK32FRAME *frame32;
2299 context->Eax = (context->Eax & ~0xffff) | (WORD)retval;
2301 /* Find the frame32 corresponding to the frame16 we are jumping to */
2302 pFrame = CURRENT_STACK16;
2303 frame32 = pFrame->frame32;
2304 while (frame32 && frame32->frame16)
2306 if (OFFSETOF(frame32->frame16) < OFFSETOF(NtCurrentTeb()->WOW32Reserved))
2307 break; /* Something strange is going on */
2308 if (OFFSETOF(frame32->frame16) > lpbuf[2])
2310 /* We found the right frame */
2311 pFrame->frame32 = frame32;
2312 break;
2314 frame32 = ((STACK16FRAME *)MapSL(frame32->frame16))->frame32;
2316 RtlUnwind( &pFrame->frame32->frame, NULL, NULL, 0 );
2318 context->Eip = lpbuf[0];
2319 context->SegCs = lpbuf[1];
2320 context->Esp = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
2321 context->Ebp = lpbuf[3];
2322 context->Esi = lpbuf[4];
2323 context->Edi = lpbuf[5];
2324 context->SegDs = lpbuf[6];
2326 if (lpbuf[8] != context->SegSs)
2327 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );