Update types checked by winapi_check.
[wine/dcerpc.git] / dlls / kernel / thunk.c
blob20ad9fb8bb7b671d0719c711e293c3fec2970cba
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"
25 #include <string.h>
26 #include <sys/types.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "winreg.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 "module.h"
44 #include "stackframe.h"
45 #include "kernel_private.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
49 struct ThunkDataCommon
51 char magic[4]; /* 00 */
52 DWORD checksum; /* 04 */
55 struct ThunkDataLS16
57 struct ThunkDataCommon common; /* 00 */
58 SEGPTR targetTable; /* 08 */
59 DWORD firstTime; /* 0C */
62 struct ThunkDataLS32
64 struct ThunkDataCommon common; /* 00 */
65 DWORD * targetTable; /* 08 */
66 char lateBinding[4]; /* 0C */
67 DWORD flags; /* 10 */
68 DWORD reserved1; /* 14 */
69 DWORD reserved2; /* 18 */
70 DWORD offsetQTThunk; /* 1C */
71 DWORD offsetFTProlog; /* 20 */
74 struct ThunkDataSL16
76 struct ThunkDataCommon common; /* 00 */
77 DWORD flags1; /* 08 */
78 DWORD reserved1; /* 0C */
79 struct ThunkDataSL * fpData; /* 10 */
80 SEGPTR spData; /* 14 */
81 DWORD reserved2; /* 18 */
82 char lateBinding[4]; /* 1C */
83 DWORD flags2; /* 20 */
84 DWORD reserved3; /* 20 */
85 SEGPTR apiDatabase; /* 28 */
88 struct ThunkDataSL32
90 struct ThunkDataCommon common; /* 00 */
91 DWORD reserved1; /* 08 */
92 struct ThunkDataSL * data; /* 0C */
93 char lateBinding[4]; /* 10 */
94 DWORD flags; /* 14 */
95 DWORD reserved2; /* 18 */
96 DWORD reserved3; /* 1C */
97 DWORD offsetTargetTable; /* 20 */
100 struct ThunkDataSL
102 #if 0
103 This structure differs from the Win95 original,
104 but this should not matter since it is strictly internal to
105 the thunk handling routines in KRNL386 / KERNEL32.
107 For reference, here is the Win95 layout:
109 struct ThunkDataCommon common; /* 00 */
110 DWORD flags1; /* 08 */
111 SEGPTR apiDatabase; /* 0C */
112 WORD exePtr; /* 10 */
113 WORD segMBA; /* 12 */
114 DWORD lenMBATotal; /* 14 */
115 DWORD lenMBAUsed; /* 18 */
116 DWORD flags2; /* 1C */
117 char pszDll16[256]; /* 20 */
118 char pszDll32[256]; /*120 */
120 We do it differently since all our thunk handling is done
121 by 32-bit code. Therefore we do not need do provide
122 easy access to this data, especially the process target
123 table database, for 16-bit code.
124 #endif
126 struct ThunkDataCommon common;
127 DWORD flags1;
128 struct SLApiDB * apiDB;
129 struct SLTargetDB * targetDB;
130 DWORD flags2;
131 char pszDll16[256];
132 char pszDll32[256];
135 struct SLTargetDB
137 struct SLTargetDB * next;
138 DWORD process;
139 DWORD * targetTable;
142 struct SLApiDB
144 DWORD nrArgBytes;
145 DWORD errorReturnValue;
148 #ifdef __i386__
149 extern void __wine_call_from_16_thunk();
150 #else
151 static void __wine_call_from_16_thunk() { }
152 #endif
154 /* Push a DWORD on the 32-bit stack */
155 static inline void stack32_push( CONTEXT86 *context, DWORD val )
157 context->Esp -= sizeof(DWORD);
158 *(DWORD *)context->Esp = val;
161 /* Pop a DWORD from the 32-bit stack */
162 static inline DWORD stack32_pop( CONTEXT86 *context )
164 DWORD ret = *(DWORD *)context->Esp;
165 context->Esp += sizeof(DWORD);
166 return ret;
169 /***********************************************************************
171 * Win95 internal thunks *
173 ***********************************************************************/
175 /***********************************************************************
176 * LogApiThk (KERNEL.423)
178 void WINAPI LogApiThk( LPSTR func )
180 TRACE( "%s\n", debugstr_a(func) );
183 /***********************************************************************
184 * LogApiThkLSF (KERNEL32.42)
186 * NOTE: needs to preserve all registers!
188 void WINAPI LogApiThkLSF( LPSTR func, CONTEXT86 *context )
190 TRACE( "%s\n", debugstr_a(func) );
193 /***********************************************************************
194 * LogApiThkSL (KERNEL32.44)
196 * NOTE: needs to preserve all registers!
198 void WINAPI LogApiThkSL( LPSTR func, CONTEXT86 *context )
200 TRACE( "%s\n", debugstr_a(func) );
203 /***********************************************************************
204 * LogCBThkSL (KERNEL32.47)
206 * NOTE: needs to preserve all registers!
208 void WINAPI LogCBThkSL( LPSTR func, CONTEXT86 *context )
210 TRACE( "%s\n", debugstr_a(func) );
213 /***********************************************************************
214 * Generates a FT_Prolog call.
216 * 0FB6D1 movzbl edx,cl
217 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
218 * 68xxxxxxxx push FT_Prolog
219 * C3 lret
221 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
222 LPBYTE x;
224 x = relayCode;
225 *x++ = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
226 *x++ = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
227 x+=4; /* mov edx, [4*edx + targetTable] */
228 *x++ = 0x68; *(DWORD*)x = (DWORD)GetProcAddress(kernel32_handle,"FT_Prolog");
229 x+=4; /* push FT_Prolog */
230 *x++ = 0xC3; /* lret */
231 /* fill rest with 0xCC / int 3 */
234 /***********************************************************************
235 * _write_qtthunk (internal)
236 * Generates a QT_Thunk style call.
238 * 33C9 xor ecx, ecx
239 * 8A4DFC mov cl , [ebp-04]
240 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
241 * B8yyyyyyyy mov eax, QT_Thunk
242 * FFE0 jmp eax
244 static void _write_qtthunk(
245 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
246 DWORD *targetTable /* [in] start of thunk (for index lookup) */
248 LPBYTE x;
250 x = relayCode;
251 *x++ = 0x33;*x++=0xC9; /* xor ecx,ecx */
252 *x++ = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
253 *x++ = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
254 x+=4; /* mov edx, [4*ecx + targetTable */
255 *x++ = 0xB8; *(DWORD*)x = (DWORD)GetProcAddress(kernel32_handle,"QT_Thunk");
256 x+=4; /* mov eax , QT_Thunk */
257 *x++ = 0xFF; *x++ = 0xE0; /* jmp eax */
258 /* should fill the rest of the 32 bytes with 0xCC */
261 /***********************************************************************
262 * _loadthunk
264 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
265 struct ThunkDataCommon *TD32, DWORD checksum)
267 struct ThunkDataCommon *TD16;
268 HMODULE16 hmod;
269 int ordinal;
271 if ((hmod = LoadLibrary16(module)) <= 32)
273 ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
274 module, func, module32, module, hmod);
275 return 0;
278 if ( !(ordinal = NE_GetOrdinal(hmod, func))
279 || !(TD16 = MapSL((SEGPTR)NE_GetEntryPointEx(hmod, ordinal, FALSE))))
281 ERR("Unable to find thunk data '%s' in %s, required by %s (conflicting/incorrect DLL versions !?).\n",
282 func, module, module32);
283 return 0;
286 if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
288 ERR("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
289 module, func, module32,
290 TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
291 TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
292 return 0;
295 if (TD32 && TD16->checksum != TD32->checksum)
297 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
298 module, func, module32, TD16->checksum, TD32->checksum);
299 return 0;
302 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
304 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
305 module, func, module32, *(LPDWORD)TD16, checksum);
306 return 0;
309 return TD16;
312 /***********************************************************************
313 * GetThunkStuff (KERNEL32.53)
315 LPVOID WINAPI GetThunkStuff(LPSTR module, LPSTR func)
317 return _loadthunk(module, func, "<kernel>", NULL, 0L);
320 /***********************************************************************
321 * GetThunkBuff (KERNEL32.52)
322 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
324 LPVOID WINAPI GetThunkBuff(void)
326 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
329 /***********************************************************************
330 * ThunkConnect32 (KERNEL32.@)
331 * Connects a 32bit and a 16bit thunkbuffer.
333 UINT WINAPI ThunkConnect32(
334 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
335 LPSTR thunkfun16, /* [in] win16 thunkfunction */
336 LPSTR module16, /* [in] name of win16 dll */
337 LPSTR module32, /* [in] name of win32 dll */
338 HMODULE hmod32, /* [in] hmodule of win32 dll */
339 DWORD dwReason /* [in] initialisation argument */
341 BOOL directionSL;
343 if (!strncmp(TD->magic, "SL01", 4))
345 directionSL = TRUE;
347 TRACE("SL01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
348 module32, (DWORD)TD, module16, thunkfun16, dwReason);
350 else if (!strncmp(TD->magic, "LS01", 4))
352 directionSL = FALSE;
354 TRACE("LS01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
355 module32, (DWORD)TD, module16, thunkfun16, dwReason);
357 else
359 ERR("Invalid magic %c%c%c%c\n",
360 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
361 return 0;
364 switch (dwReason)
366 case DLL_PROCESS_ATTACH:
368 struct ThunkDataCommon *TD16;
369 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
370 return 0;
372 if (directionSL)
374 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
375 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
376 struct SLTargetDB *tdb;
378 if (SL16->fpData == NULL)
380 ERR("ThunkConnect16 was not called!\n");
381 return 0;
384 SL32->data = SL16->fpData;
386 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
387 tdb->process = GetCurrentProcessId();
388 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
390 tdb->next = SL32->data->targetDB; /* FIXME: not thread-safe! */
391 SL32->data->targetDB = tdb;
393 TRACE("Process %08lx allocated TargetDB entry for ThunkDataSL %08lx\n",
394 GetCurrentProcessId(), (DWORD)SL32->data);
396 else
398 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
399 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
401 LS32->targetTable = MapSL(LS16->targetTable);
403 /* write QT_Thunk and FT_Prolog stubs */
404 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk, LS32->targetTable);
405 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
407 break;
410 case DLL_PROCESS_DETACH:
411 /* FIXME: cleanup */
412 break;
415 return 1;
418 /**********************************************************************
419 * QT_Thunk (KERNEL32.@)
421 * The target address is in EDX.
422 * The 16bit arguments start at ESP.
423 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
424 * So the stack layout is 16bit argument bytes and then the 64 byte
425 * scratch buffer.
426 * The scratch buffer is used as work space by Windows' QT_Thunk
427 * function.
428 * As the programs unfortunately don't always provide a fixed size
429 * scratch buffer (danger, stack corruption ahead !!), we simply resort
430 * to copying over the whole EBP-ESP range to the 16bit stack
431 * (as there's no way to safely figure out the param count
432 * due to this misbehaviour of some programs).
433 * [ok]
435 * See DDJ article 9614c for a very good description of QT_Thunk (also
436 * available online !).
438 * FIXME: DDJ talks of certain register usage rules; I'm not sure
439 * whether we cover this 100%.
441 void WINAPI QT_Thunk( CONTEXT86 *context )
443 CONTEXT86 context16;
444 DWORD argsize;
446 memcpy(&context16,context,sizeof(context16));
448 context16.SegFs = wine_get_fs();
449 context16.SegGs = wine_get_gs();
450 context16.SegCs = HIWORD(context->Edx);
451 context16.Eip = LOWORD(context->Edx);
452 /* point EBP to the STACK16FRAME on the stack
453 * for the call_to_16 to set up the register content on calling */
454 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
455 + (WORD)&((STACK16FRAME*)0)->bp;
458 * used to be (problematic):
459 * argsize = context->Ebp - context->Esp - 0x40;
460 * due to some programs abusing the API, we better assume the full
461 * EBP - ESP range for copying instead: */
462 argsize = context->Ebp - context->Esp;
464 /* ok, too much is insane; let's limit param count a bit again */
465 if (argsize > 64)
466 argsize = 64; /* 32 WORDs */
468 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
469 context->Eax = context16.Eax;
470 context->Edx = context16.Edx;
471 context->Ecx = context16.Ecx;
473 /* make sure to update the Win32 ESP, too, in order to throw away
474 * the number of parameters that the Win16 function
475 * accepted (that it popped from the corresponding Win16 stack) */
476 context->Esp += LOWORD(context16.Esp) -
477 ( OFFSETOF( NtCurrentTeb()->cur_stack ) - argsize );
481 /**********************************************************************
482 * FT_Prolog (KERNEL32.@)
484 * The set of FT_... thunk routines is used instead of QT_Thunk,
485 * if structures have to be converted from 32-bit to 16-bit
486 * (change of member alignment, conversion of members).
488 * The thunk function (as created by the thunk compiler) calls
489 * FT_Prolog at the beginning, to set up a stack frame and
490 * allocate a 64 byte buffer on the stack.
491 * The input parameters (target address and some flags) are
492 * saved for later use by FT_Thunk.
494 * Input: EDX 16-bit target address (SEGPTR)
495 * CX bits 0..7 target number (in target table)
496 * bits 8..9 some flags (unclear???)
497 * bits 10..15 number of DWORD arguments
499 * Output: A new stackframe is created, and a 64 byte buffer
500 * allocated on the stack. The layout of the stack
501 * on return is as follows:
503 * (ebp+4) return address to caller of thunk function
504 * (ebp) old EBP
505 * (ebp-4) saved EBX register of caller
506 * (ebp-8) saved ESI register of caller
507 * (ebp-12) saved EDI register of caller
508 * (ebp-16) saved ECX register, containing flags
509 * (ebp-20) bitmap containing parameters that are to be converted
510 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
511 * filled in by the thunk code before calling FT_Thunk
512 * (ebp-24)
513 * ... (unclear)
514 * (ebp-44)
515 * (ebp-48) saved EAX register of caller (unclear, never restored???)
516 * (ebp-52) saved EDX register, containing 16-bit thunk target
517 * (ebp-56)
518 * ... (unclear)
519 * (ebp-64)
521 * ESP is EBP-64 after return.
525 void WINAPI FT_Prolog( CONTEXT86 *context )
527 /* Build stack frame */
528 stack32_push(context, context->Ebp);
529 context->Ebp = context->Esp;
531 /* Allocate 64-byte Thunk Buffer */
532 context->Esp -= 64;
533 memset((char *)context->Esp, '\0', 64);
535 /* Store Flags (ECX) and Target Address (EDX) */
536 /* Save other registers to be restored later */
537 *(DWORD *)(context->Ebp - 4) = context->Ebx;
538 *(DWORD *)(context->Ebp - 8) = context->Esi;
539 *(DWORD *)(context->Ebp - 12) = context->Edi;
540 *(DWORD *)(context->Ebp - 16) = context->Ecx;
542 *(DWORD *)(context->Ebp - 48) = context->Eax;
543 *(DWORD *)(context->Ebp - 52) = context->Edx;
546 /**********************************************************************
547 * FT_Thunk (KERNEL32.@)
549 * This routine performs the actual call to 16-bit code,
550 * similar to QT_Thunk. The differences are:
551 * - The call target is taken from the buffer created by FT_Prolog
552 * - Those arguments requested by the thunk code (by setting the
553 * corresponding bit in the bitmap at EBP-20) are converted
554 * from 32-bit pointers to segmented pointers (those pointers
555 * are guaranteed to point to structures copied to the stack
556 * by the thunk code, so we always use the 16-bit stack selector
557 * for those addresses).
559 * The bit #i of EBP-20 corresponds here to the DWORD starting at
560 * ESP+4 + 2*i.
562 * FIXME: It is unclear what happens if there are more than 32 WORDs
563 * of arguments, so that the single DWORD bitmap is no longer
564 * sufficient ...
567 void WINAPI FT_Thunk( CONTEXT86 *context )
569 DWORD mapESPrelative = *(DWORD *)(context->Ebp - 20);
570 DWORD callTarget = *(DWORD *)(context->Ebp - 52);
572 CONTEXT86 context16;
573 DWORD i, argsize;
574 DWORD newstack[32];
575 LPBYTE oldstack;
577 memcpy(&context16,context,sizeof(context16));
579 context16.SegFs = wine_get_fs();
580 context16.SegGs = wine_get_gs();
581 context16.SegCs = HIWORD(callTarget);
582 context16.Eip = LOWORD(callTarget);
583 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
584 + (WORD)&((STACK16FRAME*)0)->bp;
586 argsize = context->Ebp-context->Esp-0x40;
587 if (argsize > sizeof(newstack)) argsize = sizeof(newstack);
588 oldstack = (LPBYTE)context->Esp;
590 memcpy( newstack, oldstack, argsize );
592 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
593 if (mapESPrelative & (1 << i))
595 SEGPTR *arg = (SEGPTR *)newstack[i];
596 *arg = MAKESEGPTR(SELECTOROF(NtCurrentTeb()->cur_stack),
597 OFFSETOF(NtCurrentTeb()->cur_stack) - argsize
598 + (*(LPBYTE *)arg - oldstack));
601 WOWCallback16Ex( 0, WCB16_REGS, argsize, newstack, (DWORD *)&context16 );
602 context->Eax = context16.Eax;
603 context->Edx = context16.Edx;
604 context->Ecx = context16.Ecx;
606 context->Esp += LOWORD(context16.Esp) -
607 ( OFFSETOF( NtCurrentTeb()->cur_stack ) - argsize );
609 /* Copy modified buffers back to 32-bit stack */
610 memcpy( oldstack, newstack, argsize );
613 /**********************************************************************
614 * FT_ExitNN (KERNEL32.218 - 232)
616 * One of the FT_ExitNN functions is called at the end of the thunk code.
617 * It removes the stack frame created by FT_Prolog, moves the function
618 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
619 * value, but the thunk code has moved it from EAX to EBX in the
620 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
621 * and perform a return to the CALLER of the thunk code (while removing
622 * the given number of arguments from the caller's stack).
625 static void FT_Exit(CONTEXT86 *context, int nPopArgs)
627 /* Return value is in EBX */
628 context->Eax = context->Ebx;
630 /* Restore EBX, ESI, and EDI registers */
631 context->Ebx = *(DWORD *)(context->Ebp - 4);
632 context->Esi = *(DWORD *)(context->Ebp - 8);
633 context->Edi = *(DWORD *)(context->Ebp - 12);
635 /* Clean up stack frame */
636 context->Esp = context->Ebp;
637 context->Ebp = stack32_pop(context);
639 /* Pop return address to CALLER of thunk code */
640 context->Eip = stack32_pop(context);
641 /* Remove arguments */
642 context->Esp += nPopArgs;
645 /***********************************************************************
646 * FT_Exit0 (KERNEL32.@)
648 void WINAPI FT_Exit0 (CONTEXT86 *context) { FT_Exit(context, 0); }
650 /***********************************************************************
651 * FT_Exit4 (KERNEL32.@)
653 void WINAPI FT_Exit4 (CONTEXT86 *context) { FT_Exit(context, 4); }
655 /***********************************************************************
656 * FT_Exit8 (KERNEL32.@)
658 void WINAPI FT_Exit8 (CONTEXT86 *context) { FT_Exit(context, 8); }
660 /***********************************************************************
661 * FT_Exit12 (KERNEL32.@)
663 void WINAPI FT_Exit12(CONTEXT86 *context) { FT_Exit(context, 12); }
665 /***********************************************************************
666 * FT_Exit16 (KERNEL32.@)
668 void WINAPI FT_Exit16(CONTEXT86 *context) { FT_Exit(context, 16); }
670 /***********************************************************************
671 * FT_Exit20 (KERNEL32.@)
673 void WINAPI FT_Exit20(CONTEXT86 *context) { FT_Exit(context, 20); }
675 /***********************************************************************
676 * FT_Exit24 (KERNEL32.@)
678 void WINAPI FT_Exit24(CONTEXT86 *context) { FT_Exit(context, 24); }
680 /***********************************************************************
681 * FT_Exit28 (KERNEL32.@)
683 void WINAPI FT_Exit28(CONTEXT86 *context) { FT_Exit(context, 28); }
685 /***********************************************************************
686 * FT_Exit32 (KERNEL32.@)
688 void WINAPI FT_Exit32(CONTEXT86 *context) { FT_Exit(context, 32); }
690 /***********************************************************************
691 * FT_Exit36 (KERNEL32.@)
693 void WINAPI FT_Exit36(CONTEXT86 *context) { FT_Exit(context, 36); }
695 /***********************************************************************
696 * FT_Exit40 (KERNEL32.@)
698 void WINAPI FT_Exit40(CONTEXT86 *context) { FT_Exit(context, 40); }
700 /***********************************************************************
701 * FT_Exit44 (KERNEL32.@)
703 void WINAPI FT_Exit44(CONTEXT86 *context) { FT_Exit(context, 44); }
705 /***********************************************************************
706 * FT_Exit48 (KERNEL32.@)
708 void WINAPI FT_Exit48(CONTEXT86 *context) { FT_Exit(context, 48); }
710 /***********************************************************************
711 * FT_Exit52 (KERNEL32.@)
713 void WINAPI FT_Exit52(CONTEXT86 *context) { FT_Exit(context, 52); }
715 /***********************************************************************
716 * FT_Exit56 (KERNEL32.@)
718 void WINAPI FT_Exit56(CONTEXT86 *context) { FT_Exit(context, 56); }
720 /***********************************************************************
721 * ThunkInitLS (KERNEL32.43)
722 * A thunkbuffer link routine
723 * The thunkbuf looks like:
725 * 00: DWORD length ? don't know exactly
726 * 04: SEGPTR ptr ? where does it point to?
727 * The pointer ptr is written into the first DWORD of 'thunk'.
728 * (probably correctly implemented)
729 * [ok probably]
730 * RETURNS
731 * segmented pointer to thunk?
733 DWORD WINAPI ThunkInitLS(
734 LPDWORD thunk, /* [in] win32 thunk */
735 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
736 DWORD len, /* [in] thkbuffer length */
737 LPCSTR dll16, /* [in] name of win16 dll */
738 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
740 LPDWORD addr;
742 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
743 return 0;
745 if (!addr[1])
746 return 0;
747 *(DWORD*)thunk = addr[1];
749 return addr[1];
752 /***********************************************************************
753 * Common32ThkLS (KERNEL32.45)
755 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
756 * style thunks. The basic difference is that the parameter conversion
757 * is done completely on the *16-bit* side here. Thus we do not call
758 * the 16-bit target directly, but call a common entry point instead.
759 * This entry function then calls the target according to the target
760 * number passed in the DI register.
762 * Input: EAX SEGPTR to the common 16-bit entry point
763 * CX offset in thunk table (target number * 4)
764 * DX error return value if execution fails (unclear???)
765 * EDX.HI number of DWORD parameters
767 * (Note that we need to move the thunk table offset from CX to DI !)
769 * The called 16-bit stub expects its stack to look like this:
770 * ...
771 * (esp+40) 32-bit arguments
772 * ...
773 * (esp+8) 32 byte of stack space available as buffer
774 * (esp) 8 byte return address for use with 0x66 lret
776 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
777 * and uses the EAX register to return a DWORD return value.
778 * Thus we need to use a special assembly glue routine
779 * (CallRegisterLongProc instead of CallRegisterShortProc).
781 * Finally, we return to the caller, popping the arguments off
782 * the stack. The number of arguments to be popped is returned
783 * in the BL register by the called 16-bit routine.
786 void WINAPI Common32ThkLS( CONTEXT86 *context )
788 CONTEXT86 context16;
789 DWORD argsize;
791 memcpy(&context16,context,sizeof(context16));
793 context16.SegFs = wine_get_fs();
794 context16.SegGs = wine_get_gs();
795 context16.Edi = LOWORD(context->Ecx);
796 context16.SegCs = HIWORD(context->Eax);
797 context16.Eip = LOWORD(context->Eax);
798 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
799 + (WORD)&((STACK16FRAME*)0)->bp;
801 argsize = HIWORD(context->Edx) * 4;
803 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
804 if (context->Edx == context->Eip)
805 argsize = 6 * 4;
807 /* Note: the first 32 bytes we copy are just garbage from the 32-bit stack, in order to reserve
808 * the space. It is safe to do that since the register function prefix has reserved
809 * a lot more space than that below context->Esp.
811 WOWCallback16Ex( 0, WCB16_REGS, argsize + 32, (LPBYTE)context->Esp - 32, (DWORD *)&context16 );
812 context->Eax = context16.Eax;
814 /* Clean up caller's stack frame */
815 context->Esp += LOBYTE(context16.Ebx);
818 /***********************************************************************
819 * OT_32ThkLSF (KERNEL32.40)
821 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
822 * argument processing is done on both the 32-bit and the 16-bit side:
823 * The 32-bit side prepares arguments, copying them onto the stack.
825 * When this routine is called, the first word on the stack is the
826 * number of argument bytes prepared by the 32-bit code, and EDX
827 * contains the 16-bit target address.
829 * The called 16-bit routine is another relaycode, doing further
830 * argument processing and then calling the real 16-bit target
831 * whose address is stored at [bp-04].
833 * The call proceeds using a normal CallRegisterShortProc.
834 * After return from the 16-bit relaycode, the arguments need
835 * to be copied *back* to the 32-bit stack, since the 32-bit
836 * relaycode processes output parameters.
838 * Note that we copy twice the number of arguments, since some of the
839 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
840 * arguments of the caller!
842 * (Note that this function seems only to be used for
843 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
845 void WINAPI OT_32ThkLSF( CONTEXT86 *context )
847 CONTEXT86 context16;
848 DWORD argsize;
850 memcpy(&context16,context,sizeof(context16));
852 context16.SegFs = wine_get_fs();
853 context16.SegGs = wine_get_gs();
854 context16.SegCs = HIWORD(context->Edx);
855 context16.Eip = LOWORD(context->Edx);
856 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
857 + (WORD)&((STACK16FRAME*)0)->bp;
859 argsize = 2 * *(WORD *)context->Esp + 2;
861 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
862 context->Eax = context16.Eax;
863 context->Edx = context16.Edx;
865 /* Copy modified buffers back to 32-bit stack */
866 memcpy( (LPBYTE)context->Esp,
867 (LPBYTE)CURRENT_STACK16 - argsize, argsize );
869 context->Esp += LOWORD(context16.Esp) -
870 ( OFFSETOF( NtCurrentTeb()->cur_stack ) - argsize );
873 /***********************************************************************
874 * ThunkInitLSF (KERNEL32.41)
875 * A thunk setup routine.
876 * Expects a pointer to a preinitialized thunkbuffer in the first argument
877 * looking like:
878 *| 00..03: unknown (pointer, check _41, _43, _46)
879 *| 04: EB1E jmp +0x20
881 *| 06..23: unknown (space for replacement code, check .90)
883 *| 24:>E800000000 call offset 29
884 *| 29:>58 pop eax ( target of call )
885 *| 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
886 *| 2F: BAxxxxxxxx mov edx,xxxxxxxx
887 *| 34: 68yyyyyyyy push KERNEL32.90
888 *| 39: C3 ret
890 *| 3A: EB1E jmp +0x20
891 *| 3E ... 59: unknown (space for replacement code?)
892 *| 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
893 *| 5F: 5A pop edx
894 *| 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
895 *| 66: 52 push edx
896 *| 67: 68xxxxxxxx push xxxxxxxx
897 *| 6C: 68yyyyyyyy push KERNEL32.89
898 *| 71: C3 ret
899 *| 72: end?
900 * This function checks if the code is there, and replaces the yyyyyyyy entries
901 * by the functionpointers.
902 * The thunkbuf looks like:
904 *| 00: DWORD length ? don't know exactly
905 *| 04: SEGPTR ptr ? where does it point to?
906 * The segpointer ptr is written into the first DWORD of 'thunk'.
907 * [ok probably]
908 * RETURNS
909 * unclear, pointer to win16 thkbuffer?
911 LPVOID WINAPI ThunkInitLSF(
912 LPBYTE thunk, /* [in] win32 thunk */
913 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
914 DWORD len, /* [in] length of thkbuffer */
915 LPCSTR dll16, /* [in] name of win16 dll */
916 LPCSTR dll32 /* [in] name of win32 dll */
918 LPDWORD addr,addr2;
920 /* FIXME: add checks for valid code ... */
921 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
922 *(DWORD*)(thunk+0x35) = (DWORD)GetProcAddress(kernel32_handle,(LPSTR)90);
923 *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress(kernel32_handle,(LPSTR)89);
926 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
927 return 0;
929 addr2 = MapSL(addr[1]);
930 if (HIWORD(addr2))
931 *(DWORD*)thunk = (DWORD)addr2;
933 return addr2;
936 /***********************************************************************
937 * FT_PrologPrime (KERNEL32.89)
939 * This function is called from the relay code installed by
940 * ThunkInitLSF. It replaces the location from where it was
941 * called by a standard FT_Prolog call stub (which is 'primed'
942 * by inserting the correct target table pointer).
943 * Finally, it calls that stub.
945 * Input: ECX target number + flags (passed through to FT_Prolog)
946 * (ESP) offset of location where target table pointer
947 * is stored, relative to the start of the relay code
948 * (ESP+4) pointer to start of relay code
949 * (this is where the FT_Prolog call stub gets written to)
951 * Note: The two DWORD arguments get popped off the stack.
954 void WINAPI FT_PrologPrime( CONTEXT86 *context )
956 DWORD targetTableOffset;
957 LPBYTE relayCode;
959 /* Compensate for the fact that the Wine register relay code thought
960 we were being called, although we were in fact jumped to */
961 context->Esp -= 4;
963 /* Write FT_Prolog call stub */
964 targetTableOffset = stack32_pop(context);
965 relayCode = (LPBYTE)stack32_pop(context);
966 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
968 /* Jump to the call stub just created */
969 context->Eip = (DWORD)relayCode;
972 /***********************************************************************
973 * QT_ThunkPrime (KERNEL32.90)
975 * This function corresponds to FT_PrologPrime, but installs a
976 * call stub for QT_Thunk instead.
978 * Input: (EBP-4) target number (passed through to QT_Thunk)
979 * EDX target table pointer location offset
980 * EAX start of relay code
983 void WINAPI QT_ThunkPrime( CONTEXT86 *context )
985 DWORD targetTableOffset;
986 LPBYTE relayCode;
988 /* Compensate for the fact that the Wine register relay code thought
989 we were being called, although we were in fact jumped to */
990 context->Esp -= 4;
992 /* Write QT_Thunk call stub */
993 targetTableOffset = context->Edx;
994 relayCode = (LPBYTE)context->Eax;
995 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
997 /* Jump to the call stub just created */
998 context->Eip = (DWORD)relayCode;
1001 /***********************************************************************
1002 * ThunkInitSL (KERNEL32.46)
1003 * Another thunkbuf link routine.
1004 * The start of the thunkbuf looks like this:
1005 * 00: DWORD length
1006 * 04: SEGPTR address for thunkbuffer pointer
1007 * [ok probably]
1009 VOID WINAPI ThunkInitSL(
1010 LPBYTE thunk, /* [in] start of thunkbuffer */
1011 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
1012 DWORD len, /* [in] length of thunkbuffer */
1013 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
1014 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
1016 LPDWORD addr;
1018 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
1019 return;
1021 *(DWORD*)MapSL(addr[1]) = (DWORD)thunk;
1024 /**********************************************************************
1025 * SSInit (KERNEL.700)
1026 * RETURNS
1027 * TRUE for success.
1029 BOOL WINAPI SSInit16(void)
1031 return TRUE;
1034 /**********************************************************************
1035 * SSOnBigStack (KERNEL32.87)
1036 * Check if thunking is initialized (ss selector set up etc.)
1037 * We do that differently, so just return TRUE.
1038 * [ok]
1039 * RETURNS
1040 * TRUE for success.
1042 BOOL WINAPI SSOnBigStack()
1044 TRACE("Yes, thunking is initialized\n");
1045 return TRUE;
1048 /**********************************************************************
1049 * SSConfirmSmallStack (KERNEL.704)
1051 * Abort if not on small stack.
1053 * This must be a register routine as it has to preserve *all* registers.
1055 void WINAPI SSConfirmSmallStack( CONTEXT86 *context )
1057 /* We are always on the small stack while in 16-bit code ... */
1060 /**********************************************************************
1061 * SSCall (KERNEL32.88)
1062 * One of the real thunking functions. This one seems to be for 32<->32
1063 * thunks. It should probably be capable of crossing processboundaries.
1065 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
1066 * [ok]
1068 DWORD WINAPIV SSCall(
1069 DWORD nr, /* [in] number of argument bytes */
1070 DWORD flags, /* [in] FIXME: flags ? */
1071 FARPROC fun, /* [in] function to call */
1072 ... /* [in/out] arguments */
1074 DWORD i,ret;
1075 DWORD *args = ((DWORD *)&fun) + 1;
1077 if(TRACE_ON(thunk))
1079 DPRINTF("(%ld,0x%08lx,%p,[",nr,flags,fun);
1080 for (i=0;i<nr/4;i++)
1081 DPRINTF("0x%08lx,",args[i]);
1082 DPRINTF("])\n");
1084 switch (nr) {
1085 case 0: ret = fun();
1086 break;
1087 case 4: ret = fun(args[0]);
1088 break;
1089 case 8: ret = fun(args[0],args[1]);
1090 break;
1091 case 12: ret = fun(args[0],args[1],args[2]);
1092 break;
1093 case 16: ret = fun(args[0],args[1],args[2],args[3]);
1094 break;
1095 case 20: ret = fun(args[0],args[1],args[2],args[3],args[4]);
1096 break;
1097 case 24: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
1098 break;
1099 case 28: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
1100 break;
1101 case 32: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
1102 break;
1103 case 36: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
1104 break;
1105 case 40: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
1106 break;
1107 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]);
1108 break;
1109 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]);
1110 break;
1111 default:
1112 WARN("Unsupported nr of arguments, %ld\n",nr);
1113 ret = 0;
1114 break;
1117 TRACE(" returning %ld ...\n",ret);
1118 return ret;
1121 /**********************************************************************
1122 * W32S_BackTo32 (KERNEL32.51)
1124 void WINAPI W32S_BackTo32( CONTEXT86 *context )
1126 LPDWORD stack = (LPDWORD)context->Esp;
1127 FARPROC proc = (FARPROC)context->Eip;
1129 context->Eax = proc( stack[1], stack[2], stack[3], stack[4], stack[5],
1130 stack[6], stack[7], stack[8], stack[9], stack[10] );
1132 context->Eip = stack32_pop(context);
1135 /**********************************************************************
1136 * AllocSLCallback (KERNEL32.@)
1138 * NOTES
1139 * Win95 uses some structchains for callbacks. It allocates them
1140 * in blocks of 100 entries, size 32 bytes each, layout:
1141 * blockstart:
1142 *| 0: PTR nextblockstart
1143 *| 4: entry *first;
1144 *| 8: WORD sel ( start points to blockstart)
1145 *| A: WORD unknown
1146 * 100xentry:
1147 *| 00..17: Code
1148 *| 18: PDB *owning_process;
1149 *| 1C: PTR blockstart
1151 * We ignore this for now. (Just a note for further developers)
1152 * FIXME: use this method, so we don't waste selectors...
1154 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
1155 * the 0x66 prefix switches from word->long registers.
1157 *| 665A pop edx
1158 *| 6668x arg2 x pushl <arg2>
1159 *| 6652 push edx
1160 *| EAx arg1 x jmpf <arg1>
1162 * returns the startaddress of this thunk.
1164 * Note, that they look very similar to the ones allocates by THUNK_Alloc.
1165 * RETURNS
1166 * A segmented pointer to the start of the thunk
1168 DWORD WINAPI
1169 AllocSLCallback(
1170 DWORD finalizer, /* [in] Finalizer function */
1171 DWORD callback /* [in] Callback function */
1173 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
1174 WORD sel;
1176 x=thunk;
1177 *x++=0x66;*x++=0x5a; /* popl edx */
1178 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
1179 *x++=0x66;*x++=0x52; /* pushl edx */
1180 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
1182 *(DWORD*)(thunk+18) = GetCurrentProcessId();
1184 sel = SELECTOR_AllocBlock( thunk, 32, WINE_LDT_FLAGS_CODE );
1185 return (sel<<16)|0;
1188 /**********************************************************************
1189 * FreeSLCallback (KERNEL32.@)
1190 * Frees the specified 16->32 callback
1192 void WINAPI
1193 FreeSLCallback(
1194 DWORD x /* [in] 16 bit callback (segmented pointer?) */
1196 FIXME("(0x%08lx): stub\n",x);
1200 /**********************************************************************
1201 * GetTEBSelectorFS (KERNEL.475)
1202 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1204 void WINAPI GetTEBSelectorFS16(void)
1206 CURRENT_STACK16->fs = wine_get_fs();
1209 /**********************************************************************
1210 * IsPeFormat (KERNEL.431)
1212 * Determine if a file is a PE format executable.
1214 * RETURNS
1215 * TRUE, if it is.
1216 * FALSE if the file could not be opened or is not a PE file.
1218 * NOTES
1219 * If fn is given as NULL then the function expects hf16 to be valid.
1221 BOOL16 WINAPI IsPeFormat16(
1222 LPSTR fn, /* [in] Filename to the executeable */
1223 HFILE16 hf16) /* [in] An open file handle */
1225 BOOL ret = FALSE;
1226 IMAGE_DOS_HEADER mzh;
1227 OFSTRUCT ofs;
1228 DWORD xmagic;
1230 if (fn) hf16 = OpenFile16(fn,&ofs,OF_READ);
1231 if (hf16 == HFILE_ERROR16) return FALSE;
1232 _llseek16(hf16,0,SEEK_SET);
1233 if (sizeof(mzh)!=_lread16(hf16,&mzh,sizeof(mzh))) goto done;
1234 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) goto done;
1235 _llseek16(hf16,mzh.e_lfanew,SEEK_SET);
1236 if (sizeof(DWORD)!=_lread16(hf16,&xmagic,sizeof(DWORD))) goto done;
1237 ret = (xmagic == IMAGE_NT_SIGNATURE);
1238 done:
1239 _lclose16(hf16);
1240 return ret;
1244 /***********************************************************************
1245 * K32Thk1632Prolog (KERNEL32.@)
1247 void WINAPI K32Thk1632Prolog( CONTEXT86 *context )
1249 LPBYTE code = (LPBYTE)context->Eip - 5;
1251 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1252 of 16->32 thunks instead of using one of the standard methods!
1253 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1254 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1255 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1256 bypassed, which means it will crash the next time the 32-bit OLE
1257 code thunks down again to 16-bit (this *will* happen!).
1259 The following hack tries to recognize this situation.
1260 This is possible since the called stubs in OLECLI32/OLESVR32 all
1261 look exactly the same:
1262 00 E8xxxxxxxx call K32Thk1632Prolog
1263 05 FF55FC call [ebp-04]
1264 08 E8xxxxxxxx call K32Thk1632Epilog
1265 0D 66CB retf
1267 If we recognize this situation, we try to simulate the actions
1268 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1269 to our 32-bit stack, creating a proper STACK16FRAME and
1270 updating cur_stack. */
1272 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1273 && code[13] == 0x66 && code[14] == 0xCB)
1275 WORD stackSel = NtCurrentTeb()->stack_sel;
1276 DWORD stackBase = GetSelectorBase(stackSel);
1278 DWORD argSize = context->Ebp - context->Esp;
1279 char *stack16 = (char *)context->Esp - 4;
1280 char *stack32 = (char *)NtCurrentTeb()->cur_stack - argSize;
1281 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1283 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1284 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1286 memset(frame16, '\0', sizeof(STACK16FRAME));
1287 frame16->frame32 = (STACK32FRAME *)NtCurrentTeb()->cur_stack;
1288 frame16->ebp = context->Ebp;
1290 memcpy(stack32, stack16, argSize);
1291 NtCurrentTeb()->cur_stack = MAKESEGPTR(stackSel, (DWORD)frame16 - stackBase);
1293 context->Esp = (DWORD)stack32 + 4;
1294 context->Ebp = context->Esp + argSize;
1296 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1297 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1300 /* entry_point is never used again once the entry point has
1301 been called. Thus we re-use it to hold the Win16Lock count */
1302 ReleaseThunkLock(&CURRENT_STACK16->entry_point);
1305 /***********************************************************************
1306 * K32Thk1632Epilog (KERNEL32.@)
1308 void WINAPI K32Thk1632Epilog( CONTEXT86 *context )
1310 LPBYTE code = (LPBYTE)context->Eip - 13;
1312 RestoreThunkLock(CURRENT_STACK16->entry_point);
1314 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1316 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1317 && code[13] == 0x66 && code[14] == 0xCB)
1319 STACK16FRAME *frame16 = MapSL(NtCurrentTeb()->cur_stack);
1320 char *stack16 = (char *)(frame16 + 1);
1321 DWORD argSize = frame16->ebp - (DWORD)stack16;
1322 char *stack32 = (char *)frame16->frame32 - argSize;
1324 DWORD nArgsPopped = context->Esp - (DWORD)stack32;
1326 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1327 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1329 NtCurrentTeb()->cur_stack = (DWORD)frame16->frame32;
1331 context->Esp = (DWORD)stack16 + nArgsPopped;
1332 context->Ebp = frame16->ebp;
1334 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1335 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1339 /*********************************************************************
1340 * PK16FNF [KERNEL32.91]
1342 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1343 * string buffer with the 8.3 filename of a recently loaded 16-bit
1344 * module. It is unknown exactly what modules trigger this
1345 * mechanism or what purpose this serves. Win98 Explorer (and
1346 * probably also Win95 with IE 4 shell integration) calls this
1347 * several times during initialization.
1349 * FIXME: find out what this really does and make it work.
1351 void WINAPI PK16FNF(LPSTR strPtr)
1353 FIXME("(%p): stub\n", strPtr);
1355 /* fill in a fake filename that'll be easy to recognize */
1356 strcpy(strPtr, "WINESTUB.FIX");
1359 /***********************************************************************
1360 * 16->32 Flat Thunk routines:
1363 /***********************************************************************
1364 * ThunkConnect16 (KERNEL.651)
1365 * Connects a 32bit and a 16bit thunkbuffer.
1367 UINT WINAPI ThunkConnect16(
1368 LPSTR module16, /* [in] name of win16 dll */
1369 LPSTR module32, /* [in] name of win32 dll */
1370 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
1371 DWORD dwReason, /* [in] initialisation argument */
1372 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
1373 LPSTR thunkfun32, /* [in] win32 thunkfunction */
1374 WORD cs /* [in] CS of win16 dll */
1376 BOOL directionSL;
1378 if (!strncmp(TD->magic, "SL01", 4))
1380 directionSL = TRUE;
1382 TRACE("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
1383 module16, (DWORD)TD, module32, thunkfun32, dwReason);
1385 else if (!strncmp(TD->magic, "LS01", 4))
1387 directionSL = FALSE;
1389 TRACE("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
1390 module16, (DWORD)TD, module32, thunkfun32, dwReason);
1392 else
1394 ERR("Invalid magic %c%c%c%c\n",
1395 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
1396 return 0;
1399 switch (dwReason)
1401 case DLL_PROCESS_ATTACH:
1402 if (directionSL)
1404 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
1405 struct ThunkDataSL *SL = SL16->fpData;
1407 if (SL == NULL)
1409 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
1411 SL->common = SL16->common;
1412 SL->flags1 = SL16->flags1;
1413 SL->flags2 = SL16->flags2;
1415 SL->apiDB = MapSL(SL16->apiDatabase);
1416 SL->targetDB = NULL;
1418 lstrcpynA(SL->pszDll16, module16, 255);
1419 lstrcpynA(SL->pszDll32, module32, 255);
1421 /* We should create a SEGPTR to the ThunkDataSL,
1422 but since the contents are not in the original format,
1423 any access to this by 16-bit code would crash anyway. */
1424 SL16->spData = 0;
1425 SL16->fpData = SL;
1429 if (SL->flags2 & 0x80000000)
1431 TRACE("Preloading 32-bit library\n");
1432 LoadLibraryA(module32);
1435 else
1437 /* nothing to do */
1439 break;
1441 case DLL_PROCESS_DETACH:
1442 /* FIXME: cleanup */
1443 break;
1446 return 1;
1450 /***********************************************************************
1451 * C16ThkSL (KERNEL.630)
1454 void WINAPI C16ThkSL(CONTEXT86 *context)
1456 LPBYTE stub = MapSL(context->Eax), x = stub;
1457 WORD cs = wine_get_cs();
1458 WORD ds = wine_get_ds();
1460 /* We produce the following code:
1462 * mov ax, __FLATDS
1463 * mov es, ax
1464 * movzx ecx, cx
1465 * mov edx, es:[ecx + $EDX]
1466 * push bp
1467 * push edx
1468 * push dx
1469 * push edx
1470 * call __FLATCS:__wine_call_from_16_thunk
1473 *x++ = 0xB8; *(WORD *)x = ds; x += sizeof(WORD);
1474 *x++ = 0x8E; *x++ = 0xC0;
1475 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1476 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1477 *x++ = 0x91; *(DWORD *)x = context->Edx; x += sizeof(DWORD);
1479 *x++ = 0x55;
1480 *x++ = 0x66; *x++ = 0x52;
1481 *x++ = 0x52;
1482 *x++ = 0x66; *x++ = 0x52;
1483 *x++ = 0x66; *x++ = 0x9A;
1484 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1485 *(WORD *)x = cs; x += sizeof(WORD);
1487 /* Jump to the stub code just created */
1488 context->Eip = LOWORD(context->Eax);
1489 context->SegCs = HIWORD(context->Eax);
1491 /* Since C16ThkSL got called by a jmp, we need to leave the
1492 original return address on the stack */
1493 context->Esp -= 4;
1496 /***********************************************************************
1497 * C16ThkSL01 (KERNEL.631)
1500 void WINAPI C16ThkSL01(CONTEXT86 *context)
1502 LPBYTE stub = MapSL(context->Eax), x = stub;
1504 if (stub)
1506 struct ThunkDataSL16 *SL16 = MapSL(context->Edx);
1507 struct ThunkDataSL *td = SL16->fpData;
1509 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), (LPCSTR)631);
1510 WORD cs = wine_get_cs();
1512 if (!td)
1514 ERR("ThunkConnect16 was not called!\n");
1515 return;
1518 TRACE("Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
1521 /* We produce the following code:
1523 * xor eax, eax
1524 * mov edx, $td
1525 * call C16ThkSL01
1526 * push bp
1527 * push edx
1528 * push dx
1529 * push edx
1530 * call __FLATCS:__wine_call_from_16_thunk
1533 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1534 *x++ = 0x66; *x++ = 0xBA; *(void **)x = td; x += sizeof(void *);
1535 *x++ = 0x9A; *(DWORD *)x = procAddress; x += sizeof(DWORD);
1537 *x++ = 0x55;
1538 *x++ = 0x66; *x++ = 0x52;
1539 *x++ = 0x52;
1540 *x++ = 0x66; *x++ = 0x52;
1541 *x++ = 0x66; *x++ = 0x9A;
1542 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1543 *(WORD *)x = cs; x += sizeof(WORD);
1545 /* Jump to the stub code just created */
1546 context->Eip = LOWORD(context->Eax);
1547 context->SegCs = HIWORD(context->Eax);
1549 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1550 orginal return address on the stack */
1551 context->Esp -= 4;
1553 else
1555 struct ThunkDataSL *td = (struct ThunkDataSL *)context->Edx;
1556 DWORD targetNr = LOWORD(context->Ecx) / 4;
1557 struct SLTargetDB *tdb;
1559 TRACE("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1560 GetCurrentProcessId(), targetNr, (DWORD)td);
1562 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1563 if (tdb->process == GetCurrentProcessId())
1564 break;
1566 if (!tdb)
1568 TRACE("Loading 32-bit library %s\n", td->pszDll32);
1569 LoadLibraryA(td->pszDll32);
1571 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1572 if (tdb->process == GetCurrentProcessId())
1573 break;
1576 if (tdb)
1578 context->Edx = tdb->targetTable[targetNr];
1580 TRACE("Call target is %08lx\n", context->Edx);
1582 else
1584 WORD *stack = MapSL( MAKESEGPTR(context->SegSs, LOWORD(context->Esp)) );
1585 context->Edx = (context->Edx & ~0xffff) | HIWORD(td->apiDB[targetNr].errorReturnValue);
1586 context->Eax = (context->Eax & ~0xffff) | LOWORD(td->apiDB[targetNr].errorReturnValue);
1587 context->Eip = stack[2];
1588 context->SegCs = stack[3];
1589 context->Esp += td->apiDB[targetNr].nrArgBytes + 4;
1591 ERR("Process %08lx did not ThunkConnect32 %s to %s\n",
1592 GetCurrentProcessId(), td->pszDll32, td->pszDll16);
1598 /***********************************************************************
1599 * 16<->32 Thunklet/Callback API:
1602 #include "pshpack1.h"
1603 typedef struct _THUNKLET
1605 BYTE prefix_target;
1606 BYTE pushl_target;
1607 DWORD target;
1609 BYTE prefix_relay;
1610 BYTE pushl_relay;
1611 DWORD relay;
1613 BYTE jmp_glue;
1614 DWORD glue;
1616 BYTE type;
1617 HINSTANCE16 owner;
1618 struct _THUNKLET *next;
1619 } THUNKLET;
1620 #include "poppack.h"
1622 #define THUNKLET_TYPE_LS 1
1623 #define THUNKLET_TYPE_SL 2
1625 static HANDLE ThunkletHeap = 0;
1626 static WORD ThunkletCodeSel;
1627 static THUNKLET *ThunkletAnchor = NULL;
1629 static FARPROC ThunkletSysthunkGlueLS = 0;
1630 static SEGPTR ThunkletSysthunkGlueSL = 0;
1632 static FARPROC ThunkletCallbackGlueLS = 0;
1633 static SEGPTR ThunkletCallbackGlueSL = 0;
1636 /* map a thunk allocated on ThunkletHeap to a 16-bit pointer */
1637 inline static SEGPTR get_segptr( void *thunk )
1639 if (!thunk) return 0;
1640 return MAKESEGPTR( ThunkletCodeSel, (char *)thunk - (char *)ThunkletHeap );
1643 /***********************************************************************
1644 * THUNK_Init
1646 static BOOL THUNK_Init(void)
1648 LPBYTE thunk;
1650 ThunkletHeap = HeapCreate( 0, 0x10000, 0x10000 );
1651 if (!ThunkletHeap) return FALSE;
1653 ThunkletCodeSel = SELECTOR_AllocBlock( (void *)ThunkletHeap, 0x10000, WINE_LDT_FLAGS_CODE );
1655 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1656 if (!thunk) return FALSE;
1658 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1659 *thunk++ = 0x58; /* popl eax */
1660 *thunk++ = 0xC3; /* ret */
1662 ThunkletSysthunkGlueSL = get_segptr( thunk );
1663 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1664 *thunk++ = 0xCB; /* lret */
1666 return TRUE;
1669 /***********************************************************************
1670 * SetThunkletCallbackGlue (KERNEL.560)
1672 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1674 ThunkletCallbackGlueLS = glueLS;
1675 ThunkletCallbackGlueSL = glueSL;
1679 /***********************************************************************
1680 * THUNK_FindThunklet
1682 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1683 DWORD glue, BYTE type )
1685 THUNKLET *thunk;
1687 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1688 if ( thunk->type == type
1689 && thunk->target == target
1690 && thunk->relay == relay
1691 && ( type == THUNKLET_TYPE_LS ?
1692 ( thunk->glue == glue - (DWORD)&thunk->type )
1693 : ( thunk->glue == glue ) ) )
1694 return thunk;
1696 return NULL;
1699 /***********************************************************************
1700 * THUNK_AllocLSThunklet
1702 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1703 FARPROC glue, HTASK16 owner )
1705 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1706 THUNKLET_TYPE_LS );
1707 if (!thunk)
1709 TDB *pTask = GlobalLock16( owner );
1711 if (!ThunkletHeap) THUNK_Init();
1712 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1713 return 0;
1715 thunk->prefix_target = thunk->prefix_relay = 0x90;
1716 thunk->pushl_target = thunk->pushl_relay = 0x68;
1717 thunk->jmp_glue = 0xE9;
1719 thunk->target = (DWORD)target;
1720 thunk->relay = (DWORD)relay;
1721 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1723 thunk->type = THUNKLET_TYPE_LS;
1724 thunk->owner = pTask? pTask->hInstance : 0;
1726 thunk->next = ThunkletAnchor;
1727 ThunkletAnchor = thunk;
1730 return (FARPROC)thunk;
1733 /***********************************************************************
1734 * THUNK_AllocSLThunklet
1736 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1737 SEGPTR glue, HTASK16 owner )
1739 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1740 THUNKLET_TYPE_SL );
1741 if (!thunk)
1743 TDB *pTask = GlobalLock16( owner );
1745 if (!ThunkletHeap) THUNK_Init();
1746 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1747 return 0;
1749 thunk->prefix_target = thunk->prefix_relay = 0x66;
1750 thunk->pushl_target = thunk->pushl_relay = 0x68;
1751 thunk->jmp_glue = 0xEA;
1753 thunk->target = (DWORD)target;
1754 thunk->relay = (DWORD)relay;
1755 thunk->glue = (DWORD)glue;
1757 thunk->type = THUNKLET_TYPE_SL;
1758 thunk->owner = pTask? pTask->hInstance : 0;
1760 thunk->next = ThunkletAnchor;
1761 ThunkletAnchor = thunk;
1764 return get_segptr( thunk );
1767 /**********************************************************************
1768 * IsLSThunklet
1770 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
1772 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1773 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1774 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1777 /**********************************************************************
1778 * IsSLThunklet (KERNEL.612)
1780 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1782 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1783 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1784 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1789 /***********************************************************************
1790 * AllocLSThunkletSysthunk (KERNEL.607)
1792 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1793 FARPROC relay, DWORD dummy )
1795 if (!ThunkletSysthunkGlueLS) THUNK_Init();
1796 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1797 ThunkletSysthunkGlueLS, GetCurrentTask() );
1800 /***********************************************************************
1801 * AllocSLThunkletSysthunk (KERNEL.608)
1803 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1804 SEGPTR relay, DWORD dummy )
1806 if (!ThunkletSysthunkGlueSL) THUNK_Init();
1807 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1808 ThunkletSysthunkGlueSL, GetCurrentTask() );
1812 /***********************************************************************
1813 * AllocLSThunkletCallbackEx (KERNEL.567)
1815 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1816 DWORD relay, HTASK16 task )
1818 THUNKLET *thunk = MapSL( target );
1819 if ( !thunk ) return NULL;
1821 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1822 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1823 return (FARPROC)thunk->target;
1825 return THUNK_AllocLSThunklet( target, relay,
1826 ThunkletCallbackGlueLS, task );
1829 /***********************************************************************
1830 * AllocSLThunkletCallbackEx (KERNEL.568)
1832 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1833 DWORD relay, HTASK16 task )
1835 THUNKLET *thunk = (THUNKLET *)target;
1836 if ( !thunk ) return 0;
1838 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1839 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1840 return (SEGPTR)thunk->target;
1842 return THUNK_AllocSLThunklet( target, relay,
1843 ThunkletCallbackGlueSL, task );
1846 /***********************************************************************
1847 * AllocLSThunkletCallback (KERNEL.561)
1848 * AllocLSThunkletCallback_dup (KERNEL.606)
1850 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1852 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1855 /***********************************************************************
1856 * AllocSLThunkletCallback (KERNEL.562)
1857 * AllocSLThunkletCallback_dup (KERNEL.605)
1859 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1861 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1864 /***********************************************************************
1865 * FindLSThunkletCallback (KERNEL.563)
1866 * FindLSThunkletCallback_dup (KERNEL.609)
1868 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1870 THUNKLET *thunk = MapSL( target );
1871 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1872 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1873 return (FARPROC)thunk->target;
1875 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1876 (DWORD)ThunkletCallbackGlueLS,
1877 THUNKLET_TYPE_LS );
1878 return (FARPROC)thunk;
1881 /***********************************************************************
1882 * FindSLThunkletCallback (KERNEL.564)
1883 * FindSLThunkletCallback_dup (KERNEL.610)
1885 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1887 THUNKLET *thunk = (THUNKLET *)target;
1888 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1889 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1890 return (SEGPTR)thunk->target;
1892 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1893 (DWORD)ThunkletCallbackGlueSL,
1894 THUNKLET_TYPE_SL );
1895 return get_segptr( thunk );
1899 /***********************************************************************
1900 * FreeThunklet (KERNEL.611)
1902 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1904 return FALSE;
1908 /***********************************************************************
1909 * Callback Client API
1912 #define N_CBC_FIXED 20
1913 #define N_CBC_VARIABLE 10
1914 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1916 static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
1917 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1919 /***********************************************************************
1920 * RegisterCBClient (KERNEL.619)
1922 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1923 SEGPTR relay16, FARPROC *relay32 )
1925 /* Search for free Callback ID */
1926 if ( wCBCId == -1 )
1927 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1928 if ( !CBClientRelay16[ wCBCId ] )
1929 break;
1931 /* Register Callback ID */
1932 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1934 CBClientRelay16[ wCBCId ] = relay16;
1935 CBClientRelay32[ wCBCId ] = relay32;
1937 else
1938 wCBCId = 0;
1940 return wCBCId;
1943 /***********************************************************************
1944 * UnRegisterCBClient (KERNEL.622)
1946 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1947 SEGPTR relay16, FARPROC *relay32 )
1949 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1950 && CBClientRelay16[ wCBCId ] == relay16
1951 && CBClientRelay32[ wCBCId ] == relay32 )
1953 CBClientRelay16[ wCBCId ] = 0;
1954 CBClientRelay32[ wCBCId ] = 0;
1956 else
1957 wCBCId = 0;
1959 return wCBCId;
1963 /***********************************************************************
1964 * InitCBClient (KERNEL.623)
1966 void WINAPI InitCBClient16( FARPROC glueLS )
1968 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1969 SEGPTR glueSL = (SEGPTR)GetProcAddress16( kernel, (LPCSTR)604 );
1971 SetThunkletCallbackGlue16( glueLS, glueSL );
1974 /***********************************************************************
1975 * CBClientGlueSL (KERNEL.604)
1977 void WINAPI CBClientGlueSL( CONTEXT86 *context )
1979 /* Create stack frame */
1980 SEGPTR stackSeg = stack16_push( 12 );
1981 LPWORD stackLin = MapSL( stackSeg );
1982 SEGPTR glue, *glueTab;
1984 stackLin[3] = (WORD)context->Ebp;
1985 stackLin[2] = (WORD)context->Esi;
1986 stackLin[1] = (WORD)context->Edi;
1987 stackLin[0] = (WORD)context->SegDs;
1989 context->Ebp = OFFSETOF( stackSeg ) + 6;
1990 context->Esp = OFFSETOF( stackSeg ) - 4;
1991 context->SegGs = 0;
1993 /* Jump to 16-bit relay code */
1994 glueTab = MapSL( CBClientRelay16[ stackLin[5] ] );
1995 glue = glueTab[ stackLin[4] ];
1996 context->SegCs = SELECTOROF( glue );
1997 context->Eip = OFFSETOF ( glue );
2000 /***********************************************************************
2001 * CBClientThunkSL (KERNEL.620)
2003 extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi );
2004 void WINAPI CBClientThunkSL( CONTEXT86 *context )
2006 /* Call 32-bit relay code */
2008 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2009 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
2011 context->Eax = CALL32_CBClient( proc, args, &context->Esi );
2014 /***********************************************************************
2015 * CBClientThunkSLEx (KERNEL.621)
2017 extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs );
2018 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
2020 /* Call 32-bit relay code */
2022 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2023 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
2024 INT nArgs;
2025 LPWORD stackLin;
2027 context->Eax = CALL32_CBClientEx( proc, args, &context->Esi, &nArgs );
2029 /* Restore registers saved by CBClientGlueSL */
2030 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
2031 context->Ebp = (context->Ebp & ~0xffff) | stackLin[3];
2032 context->Esi = (context->Esi & ~0xffff) | stackLin[2];
2033 context->Edi = (context->Edi & ~0xffff) | stackLin[1];
2034 context->SegDs = stackLin[0];
2035 context->Esp += 16+nArgs;
2037 /* Return to caller of CBClient thunklet */
2038 context->SegCs = stackLin[9];
2039 context->Eip = stackLin[8];
2043 /***********************************************************************
2044 * Get16DLLAddress (KERNEL32.@)
2046 * This function is used by a Win32s DLL if it wants to call a Win16 function.
2047 * A 16:16 segmented pointer to the function is returned.
2048 * Written without any docu.
2050 SEGPTR WINAPI Get16DLLAddress(HMODULE16 handle, LPSTR func_name)
2052 static WORD code_sel32;
2053 FARPROC16 proc_16;
2054 LPBYTE thunk;
2056 if (!code_sel32)
2058 if (!ThunkletHeap) THUNK_Init();
2059 code_sel32 = SELECTOR_AllocBlock( (void *)ThunkletHeap, 0x10000,
2060 WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
2061 if (!code_sel32) return 0;
2063 if (!(thunk = HeapAlloc( ThunkletHeap, 0, 32 ))) return 0;
2065 if (!handle) handle = GetModuleHandle16("WIN32S16");
2066 proc_16 = GetProcAddress16(handle, func_name);
2068 /* movl proc_16, $edx */
2069 *thunk++ = 0xba;
2070 *(FARPROC16 *)thunk = proc_16;
2071 thunk += sizeof(FARPROC16);
2073 /* jmpl QT_Thunk */
2074 *thunk++ = 0xea;
2075 *(FARPROC *)thunk = GetProcAddress(kernel32_handle,"QT_Thunk");
2076 thunk += sizeof(FARPROC16);
2077 *(WORD *)thunk = wine_get_cs();
2079 return MAKESEGPTR( code_sel32, (char *)thunk - (char *)ThunkletHeap );
2083 /***********************************************************************
2084 * GetWin16DOSEnv (KERNEL32.34)
2085 * Returns some internal value.... probably the default environment database?
2087 DWORD WINAPI GetWin16DOSEnv()
2089 FIXME("stub, returning 0\n");
2090 return 0;
2093 /**********************************************************************
2094 * GetPK16SysVar (KERNEL32.92)
2096 LPVOID WINAPI GetPK16SysVar(void)
2098 static BYTE PK16SysVar[128];
2100 FIXME("()\n");
2101 return PK16SysVar;
2104 /**********************************************************************
2105 * CommonUnimpStub (KERNEL32.17)
2107 void WINAPI CommonUnimpStub( CONTEXT86 *context )
2109 FIXME("generic stub: %s\n", ((LPSTR)context->Eax ? (LPSTR)context->Eax : "?"));
2111 switch ((context->Ecx >> 4) & 0x0f)
2113 case 15: context->Eax = -1; break;
2114 case 14: context->Eax = 0x78; break;
2115 case 13: context->Eax = 0x32; break;
2116 case 1: context->Eax = 1; break;
2117 default: context->Eax = 0; break;
2120 context->Esp += (context->Ecx & 0x0f) * 4;
2123 /**********************************************************************
2124 * HouseCleanLogicallyDeadHandles (KERNEL32.33)
2126 void WINAPI HouseCleanLogicallyDeadHandles(void)
2128 /* Whatever this is supposed to do, our handles probably
2129 don't need it :-) */
2132 /**********************************************************************
2133 * @ (KERNEL32.100)
2135 BOOL WINAPI _KERNEL32_100(HANDLE threadid,DWORD exitcode,DWORD x)
2137 FIXME("(%p,%ld,0x%08lx): stub\n",threadid,exitcode,x);
2138 return TRUE;
2141 /**********************************************************************
2142 * @ (KERNEL32.99)
2144 * Checks whether the clock has to be switched from daylight
2145 * savings time to standard time or vice versa.
2148 DWORD WINAPI _KERNEL32_99(DWORD x)
2150 FIXME("(0x%08lx): stub\n",x);
2151 return 1;
2155 /**********************************************************************
2156 * Catch (KERNEL.55)
2158 * Real prototype is:
2159 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
2161 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT86 *context )
2163 /* Note: we don't save the current ss, as the catch buffer is */
2164 /* only 9 words long. Hopefully no one will have the silly */
2165 /* idea to change the current stack before calling Throw()... */
2167 /* Windows uses:
2168 * lpbuf[0] = ip
2169 * lpbuf[1] = cs
2170 * lpbuf[2] = sp
2171 * lpbuf[3] = bp
2172 * lpbuf[4] = si
2173 * lpbuf[5] = di
2174 * lpbuf[6] = ds
2175 * lpbuf[7] = unused
2176 * lpbuf[8] = ss
2179 lpbuf[0] = LOWORD(context->Eip);
2180 lpbuf[1] = context->SegCs;
2181 /* Windows pushes 4 more words before saving sp */
2182 lpbuf[2] = LOWORD(context->Esp) - 4 * sizeof(WORD);
2183 lpbuf[3] = LOWORD(context->Ebp);
2184 lpbuf[4] = LOWORD(context->Esi);
2185 lpbuf[5] = LOWORD(context->Edi);
2186 lpbuf[6] = context->SegDs;
2187 lpbuf[7] = 0;
2188 lpbuf[8] = context->SegSs;
2189 context->Eax &= ~0xffff; /* Return 0 */
2193 /**********************************************************************
2194 * Throw (KERNEL.56)
2196 * Real prototype is:
2197 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
2199 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT86 *context )
2201 STACK16FRAME *pFrame;
2202 STACK32FRAME *frame32;
2204 context->Eax = (context->Eax & ~0xffff) | (WORD)retval;
2206 /* Find the frame32 corresponding to the frame16 we are jumping to */
2207 pFrame = CURRENT_STACK16;
2208 frame32 = pFrame->frame32;
2209 while (frame32 && frame32->frame16)
2211 if (OFFSETOF(frame32->frame16) < OFFSETOF(NtCurrentTeb()->cur_stack))
2212 break; /* Something strange is going on */
2213 if (OFFSETOF(frame32->frame16) > lpbuf[2])
2215 /* We found the right frame */
2216 pFrame->frame32 = frame32;
2217 break;
2219 frame32 = ((STACK16FRAME *)MapSL(frame32->frame16))->frame32;
2221 RtlUnwind( &pFrame->frame32->frame, NULL, NULL, 0 );
2223 context->Eip = lpbuf[0];
2224 context->SegCs = lpbuf[1];
2225 context->Esp = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
2226 context->Ebp = lpbuf[3];
2227 context->Esi = lpbuf[4];
2228 context->Edi = lpbuf[5];
2229 context->SegDs = lpbuf[6];
2231 if (lpbuf[8] != context->SegSs)
2232 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );