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
24 #include "wine/port.h"
27 #include <sys/types.h>
40 #include "wine/winbase16.h"
42 #include "wine/debug.h"
43 #include "wine/library.h"
45 #include "stackframe.h"
46 #include "kernel_private.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
50 struct ThunkDataCommon
52 char magic[4]; /* 00 */
53 DWORD checksum; /* 04 */
58 struct ThunkDataCommon common; /* 00 */
59 SEGPTR targetTable; /* 08 */
60 DWORD firstTime; /* 0C */
65 struct ThunkDataCommon common; /* 00 */
66 DWORD * targetTable; /* 08 */
67 char lateBinding[4]; /* 0C */
69 DWORD reserved1; /* 14 */
70 DWORD reserved2; /* 18 */
71 DWORD offsetQTThunk; /* 1C */
72 DWORD offsetFTProlog; /* 20 */
77 struct ThunkDataCommon common; /* 00 */
78 DWORD flags1; /* 08 */
79 DWORD reserved1; /* 0C */
80 struct ThunkDataSL * fpData; /* 10 */
81 SEGPTR spData; /* 14 */
82 DWORD reserved2; /* 18 */
83 char lateBinding[4]; /* 1C */
84 DWORD flags2; /* 20 */
85 DWORD reserved3; /* 20 */
86 SEGPTR apiDatabase; /* 28 */
91 struct ThunkDataCommon common; /* 00 */
92 DWORD reserved1; /* 08 */
93 struct ThunkDataSL * data; /* 0C */
94 char lateBinding[4]; /* 10 */
96 DWORD reserved2; /* 18 */
97 DWORD reserved3; /* 1C */
98 DWORD offsetTargetTable; /* 20 */
104 This structure differs from the Win95 original,
105 but this should not matter since it is strictly internal to
106 the thunk handling routines in KRNL386 / KERNEL32.
108 For reference, here is the Win95 layout:
110 struct ThunkDataCommon common; /* 00 */
111 DWORD flags1; /* 08 */
112 SEGPTR apiDatabase; /* 0C */
113 WORD exePtr; /* 10 */
114 WORD segMBA; /* 12 */
115 DWORD lenMBATotal; /* 14 */
116 DWORD lenMBAUsed; /* 18 */
117 DWORD flags2; /* 1C */
118 char pszDll16[256]; /* 20 */
119 char pszDll32[256]; /*120 */
121 We do it differently since all our thunk handling is done
122 by 32-bit code. Therefore we do not need do provide
123 easy access to this data, especially the process target
124 table database, for 16-bit code.
127 struct ThunkDataCommon common;
129 struct SLApiDB * apiDB;
130 struct SLTargetDB * targetDB;
138 struct SLTargetDB * next;
146 DWORD errorReturnValue;
150 extern void __wine_call_from_16_thunk();
152 static void __wine_call_from_16_thunk() { }
155 /* Push a DWORD on the 32-bit stack */
156 static inline void stack32_push( CONTEXT86 *context, DWORD val )
158 context->Esp -= sizeof(DWORD);
159 *(DWORD *)context->Esp = val;
162 /* Pop a DWORD from the 32-bit stack */
163 static inline DWORD stack32_pop( CONTEXT86 *context )
165 DWORD ret = *(DWORD *)context->Esp;
166 context->Esp += sizeof(DWORD);
170 /***********************************************************************
172 * Win95 internal thunks *
174 ***********************************************************************/
176 /***********************************************************************
177 * LogApiThk (KERNEL.423)
179 void WINAPI LogApiThk( LPSTR func )
181 TRACE( "%s\n", debugstr_a(func) );
184 /***********************************************************************
185 * LogApiThkLSF (KERNEL32.42)
187 * NOTE: needs to preserve all registers!
189 void WINAPI __regs_LogApiThkLSF( LPSTR func, CONTEXT86 *context )
191 TRACE( "%s\n", debugstr_a(func) );
193 #ifdef DEFINE_REGS_ENTRYPOINT
194 DEFINE_REGS_ENTRYPOINT( LogApiThkLSF, 4, 4 );
197 /***********************************************************************
198 * LogApiThkSL (KERNEL32.44)
200 * NOTE: needs to preserve all registers!
202 void WINAPI __regs_LogApiThkSL( LPSTR func, CONTEXT86 *context )
204 TRACE( "%s\n", debugstr_a(func) );
206 #ifdef DEFINE_REGS_ENTRYPOINT
207 DEFINE_REGS_ENTRYPOINT( LogApiThkSL, 4, 4 );
210 /***********************************************************************
211 * LogCBThkSL (KERNEL32.47)
213 * NOTE: needs to preserve all registers!
215 void WINAPI __regs_LogCBThkSL( LPSTR func, CONTEXT86 *context )
217 TRACE( "%s\n", debugstr_a(func) );
219 #ifdef DEFINE_REGS_ENTRYPOINT
220 DEFINE_REGS_ENTRYPOINT( LogCBThkSL, 4, 4 );
223 /***********************************************************************
224 * Generates a FT_Prolog call.
226 * 0FB6D1 movzbl edx,cl
227 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
228 * 68xxxxxxxx push FT_Prolog
231 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
235 *x++ = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
236 *x++ = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
237 x+=4; /* mov edx, [4*edx + targetTable] */
238 *x++ = 0x68; *(DWORD*)x = (DWORD)GetProcAddress(kernel32_handle,"FT_Prolog");
239 x+=4; /* push FT_Prolog */
240 *x++ = 0xC3; /* lret */
241 /* fill rest with 0xCC / int 3 */
244 /***********************************************************************
245 * _write_qtthunk (internal)
246 * Generates a QT_Thunk style call.
249 * 8A4DFC mov cl , [ebp-04]
250 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
251 * B8yyyyyyyy mov eax, QT_Thunk
254 static void _write_qtthunk(
255 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
256 DWORD *targetTable /* [in] start of thunk (for index lookup) */
261 *x++ = 0x33;*x++=0xC9; /* xor ecx,ecx */
262 *x++ = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
263 *x++ = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
264 x+=4; /* mov edx, [4*ecx + targetTable */
265 *x++ = 0xB8; *(DWORD*)x = (DWORD)GetProcAddress(kernel32_handle,"QT_Thunk");
266 x+=4; /* mov eax , QT_Thunk */
267 *x++ = 0xFF; *x++ = 0xE0; /* jmp eax */
268 /* should fill the rest of the 32 bytes with 0xCC */
271 /***********************************************************************
274 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
275 struct ThunkDataCommon *TD32, DWORD checksum)
277 struct ThunkDataCommon *TD16;
281 if ((hmod = LoadLibrary16(module)) <= 32)
283 ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
284 module, func, module32, module, hmod);
288 if ( !(ordinal = NE_GetOrdinal(hmod, func))
289 || !(TD16 = MapSL((SEGPTR)NE_GetEntryPointEx(hmod, ordinal, FALSE))))
291 ERR("Unable to find thunk data '%s' in %s, required by %s (conflicting/incorrect DLL versions !?).\n",
292 func, module, module32);
296 if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
298 ERR("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
299 module, func, module32,
300 TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
301 TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
305 if (TD32 && TD16->checksum != TD32->checksum)
307 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
308 module, func, module32, TD16->checksum, TD32->checksum);
312 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
314 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
315 module, func, module32, *(LPDWORD)TD16, checksum);
322 /***********************************************************************
323 * GetThunkStuff (KERNEL32.53)
325 LPVOID WINAPI GetThunkStuff(LPSTR module, LPSTR func)
327 return _loadthunk(module, func, "<kernel>", NULL, 0L);
330 /***********************************************************************
331 * GetThunkBuff (KERNEL32.52)
332 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
334 LPVOID WINAPI GetThunkBuff(void)
336 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
339 /***********************************************************************
340 * ThunkConnect32 (KERNEL32.@)
341 * Connects a 32bit and a 16bit thunkbuffer.
343 UINT WINAPI ThunkConnect32(
344 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
345 LPSTR thunkfun16, /* [in] win16 thunkfunction */
346 LPSTR module16, /* [in] name of win16 dll */
347 LPSTR module32, /* [in] name of win32 dll */
348 HMODULE hmod32, /* [in] hmodule of win32 dll */
349 DWORD dwReason /* [in] initialisation argument */
353 if (!strncmp(TD->magic, "SL01", 4))
357 TRACE("SL01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
358 module32, (DWORD)TD, module16, thunkfun16, dwReason);
360 else if (!strncmp(TD->magic, "LS01", 4))
364 TRACE("LS01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
365 module32, (DWORD)TD, module16, thunkfun16, dwReason);
369 ERR("Invalid magic %c%c%c%c\n",
370 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
376 case DLL_PROCESS_ATTACH:
378 struct ThunkDataCommon *TD16;
379 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
384 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
385 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
386 struct SLTargetDB *tdb;
388 if (SL16->fpData == NULL)
390 ERR("ThunkConnect16 was not called!\n");
394 SL32->data = SL16->fpData;
396 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
397 tdb->process = GetCurrentProcessId();
398 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
400 tdb->next = SL32->data->targetDB; /* FIXME: not thread-safe! */
401 SL32->data->targetDB = tdb;
403 TRACE("Process %08lx allocated TargetDB entry for ThunkDataSL %08lx\n",
404 GetCurrentProcessId(), (DWORD)SL32->data);
408 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
409 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
411 LS32->targetTable = MapSL(LS16->targetTable);
413 /* write QT_Thunk and FT_Prolog stubs */
414 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk, LS32->targetTable);
415 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
420 case DLL_PROCESS_DETACH:
428 /**********************************************************************
429 * QT_Thunk (KERNEL32.@)
431 * The target address is in EDX.
432 * The 16bit arguments start at ESP.
433 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
434 * So the stack layout is 16bit argument bytes and then the 64 byte
436 * The scratch buffer is used as work space by Windows' QT_Thunk
438 * As the programs unfortunately don't always provide a fixed size
439 * scratch buffer (danger, stack corruption ahead !!), we simply resort
440 * to copying over the whole EBP-ESP range to the 16bit stack
441 * (as there's no way to safely figure out the param count
442 * due to this misbehaviour of some programs).
445 * See DDJ article 9614c for a very good description of QT_Thunk (also
446 * available online !).
448 * FIXME: DDJ talks of certain register usage rules; I'm not sure
449 * whether we cover this 100%.
451 void WINAPI __regs_QT_Thunk( CONTEXT86 *context )
456 memcpy(&context16,context,sizeof(context16));
458 context16.SegFs = wine_get_fs();
459 context16.SegGs = wine_get_gs();
460 context16.SegCs = HIWORD(context->Edx);
461 context16.Eip = LOWORD(context->Edx);
462 /* point EBP to the STACK16FRAME on the stack
463 * for the call_to_16 to set up the register content on calling */
464 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
465 + (WORD)&((STACK16FRAME*)0)->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 */
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()->cur_stack ) - argsize );
489 #ifdef DEFINE_REGS_ENTRYPOINT
490 DEFINE_REGS_ENTRYPOINT( QT_Thunk, 0, 0 );
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
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
528 * (ebp-48) saved EAX register of caller (unclear, never restored???)
529 * (ebp-52) saved EDX register, containing 16-bit thunk target
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 */
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 );
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
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
581 void WINAPI __regs_FT_Thunk( CONTEXT86 *context )
583 DWORD mapESPrelative = *(DWORD *)(context->Ebp - 20);
584 DWORD callTarget = *(DWORD *)(context->Ebp - 52);
591 memcpy(&context16,context,sizeof(context16));
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()->cur_stack )
598 + (WORD)&((STACK16FRAME*)0)->bp;
600 argsize = context->Ebp-context->Esp-0x40;
601 if (argsize > sizeof(newstack)) argsize = sizeof(newstack);
602 oldstack = (LPBYTE)context->Esp;
604 memcpy( newstack, oldstack, argsize );
606 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
607 if (mapESPrelative & (1 << i))
609 SEGPTR *arg = (SEGPTR *)newstack[i];
610 *arg = MAKESEGPTR(SELECTOROF(NtCurrentTeb()->cur_stack),
611 OFFSETOF(NtCurrentTeb()->cur_stack) - argsize
612 + (*(LPBYTE *)arg - oldstack));
615 WOWCallback16Ex( 0, WCB16_REGS, argsize, newstack, (DWORD *)&context16 );
616 context->Eax = context16.Eax;
617 context->Edx = context16.Edx;
618 context->Ecx = context16.Ecx;
620 context->Esp += LOWORD(context16.Esp) -
621 ( OFFSETOF( NtCurrentTeb()->cur_stack ) - argsize );
623 /* Copy modified buffers back to 32-bit stack */
624 memcpy( oldstack, newstack, argsize );
626 #ifdef DEFINE_REGS_ENTRYPOINT
627 DEFINE_REGS_ENTRYPOINT( FT_Thunk, 0, 0 );
630 /***********************************************************************
631 * FT_Exit0 (KERNEL32.@)
632 * FT_Exit4 (KERNEL32.@)
633 * FT_Exit8 (KERNEL32.@)
634 * FT_Exit12 (KERNEL32.@)
635 * FT_Exit16 (KERNEL32.@)
636 * FT_Exit20 (KERNEL32.@)
637 * FT_Exit24 (KERNEL32.@)
638 * FT_Exit28 (KERNEL32.@)
639 * FT_Exit32 (KERNEL32.@)
640 * FT_Exit36 (KERNEL32.@)
641 * FT_Exit40 (KERNEL32.@)
642 * FT_Exit44 (KERNEL32.@)
643 * FT_Exit48 (KERNEL32.@)
644 * FT_Exit52 (KERNEL32.@)
645 * FT_Exit56 (KERNEL32.@)
647 * One of the FT_ExitNN functions is called at the end of the thunk code.
648 * It removes the stack frame created by FT_Prolog, moves the function
649 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
650 * value, but the thunk code has moved it from EAX to EBX in the
651 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
652 * and perform a return to the CALLER of the thunk code (while removing
653 * the given number of arguments from the caller's stack).
655 static inline void FT_Exit(CONTEXT86 *context)
657 /* Return value is in EBX */
658 context->Eax = context->Ebx;
660 /* Restore EBX, ESI, and EDI registers */
661 context->Ebx = *(DWORD *)(context->Ebp - 4);
662 context->Esi = *(DWORD *)(context->Ebp - 8);
663 context->Edi = *(DWORD *)(context->Ebp - 12);
665 /* Clean up stack frame */
666 context->Esp = context->Ebp;
667 context->Ebp = stack32_pop(context);
669 /* Pop return address to CALLER of thunk code */
670 context->Eip = stack32_pop(context);
673 #ifdef DEFINE_REGS_ENTRYPOINT
675 #define DEFINE_FT_Exit(n) \
676 void WINAPI __regs_FT_Exit ## n(CONTEXT86 *context) \
681 DEFINE_REGS_ENTRYPOINT( FT_Exit ## n, 0, 0 )
699 #endif /* DEFINE_REGS_ENTRYPOINT */
702 /***********************************************************************
703 * ThunkInitLS (KERNEL32.43)
704 * A thunkbuffer link routine
705 * The thunkbuf looks like:
707 * 00: DWORD length ? don't know exactly
708 * 04: SEGPTR ptr ? where does it point to?
709 * The pointer ptr is written into the first DWORD of 'thunk'.
710 * (probably correctly implemented)
713 * segmented pointer to thunk?
715 DWORD WINAPI ThunkInitLS(
716 LPDWORD thunk, /* [in] win32 thunk */
717 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
718 DWORD len, /* [in] thkbuffer length */
719 LPCSTR dll16, /* [in] name of win16 dll */
720 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
724 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
729 *(DWORD*)thunk = addr[1];
734 /***********************************************************************
735 * Common32ThkLS (KERNEL32.45)
737 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
738 * style thunks. The basic difference is that the parameter conversion
739 * is done completely on the *16-bit* side here. Thus we do not call
740 * the 16-bit target directly, but call a common entry point instead.
741 * This entry function then calls the target according to the target
742 * number passed in the DI register.
744 * Input: EAX SEGPTR to the common 16-bit entry point
745 * CX offset in thunk table (target number * 4)
746 * DX error return value if execution fails (unclear???)
747 * EDX.HI number of DWORD parameters
749 * (Note that we need to move the thunk table offset from CX to DI !)
751 * The called 16-bit stub expects its stack to look like this:
753 * (esp+40) 32-bit arguments
755 * (esp+8) 32 byte of stack space available as buffer
756 * (esp) 8 byte return address for use with 0x66 lret
758 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
759 * and uses the EAX register to return a DWORD return value.
760 * Thus we need to use a special assembly glue routine
761 * (CallRegisterLongProc instead of CallRegisterShortProc).
763 * Finally, we return to the caller, popping the arguments off
764 * the stack. The number of arguments to be popped is returned
765 * in the BL register by the called 16-bit routine.
768 void WINAPI __regs_Common32ThkLS( CONTEXT86 *context )
773 memcpy(&context16,context,sizeof(context16));
775 context16.SegFs = wine_get_fs();
776 context16.SegGs = wine_get_gs();
777 context16.Edi = LOWORD(context->Ecx);
778 context16.SegCs = HIWORD(context->Eax);
779 context16.Eip = LOWORD(context->Eax);
780 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
781 + (WORD)&((STACK16FRAME*)0)->bp;
783 argsize = HIWORD(context->Edx) * 4;
785 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
786 if (context->Edx == context->Eip)
789 /* Note: the first 32 bytes we copy are just garbage from the 32-bit stack, in order to reserve
790 * the space. It is safe to do that since the register function prefix has reserved
791 * a lot more space than that below context->Esp.
793 WOWCallback16Ex( 0, WCB16_REGS, argsize + 32, (LPBYTE)context->Esp - 32, (DWORD *)&context16 );
794 context->Eax = context16.Eax;
796 /* Clean up caller's stack frame */
797 context->Esp += LOBYTE(context16.Ebx);
799 #ifdef DEFINE_REGS_ENTRYPOINT
800 DEFINE_REGS_ENTRYPOINT( Common32ThkLS, 0, 0 );
803 /***********************************************************************
804 * OT_32ThkLSF (KERNEL32.40)
806 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
807 * argument processing is done on both the 32-bit and the 16-bit side:
808 * The 32-bit side prepares arguments, copying them onto the stack.
810 * When this routine is called, the first word on the stack is the
811 * number of argument bytes prepared by the 32-bit code, and EDX
812 * contains the 16-bit target address.
814 * The called 16-bit routine is another relaycode, doing further
815 * argument processing and then calling the real 16-bit target
816 * whose address is stored at [bp-04].
818 * The call proceeds using a normal CallRegisterShortProc.
819 * After return from the 16-bit relaycode, the arguments need
820 * to be copied *back* to the 32-bit stack, since the 32-bit
821 * relaycode processes output parameters.
823 * Note that we copy twice the number of arguments, since some of the
824 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
825 * arguments of the caller!
827 * (Note that this function seems only to be used for
828 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
830 void WINAPI __regs_OT_32ThkLSF( CONTEXT86 *context )
835 memcpy(&context16,context,sizeof(context16));
837 context16.SegFs = wine_get_fs();
838 context16.SegGs = wine_get_gs();
839 context16.SegCs = HIWORD(context->Edx);
840 context16.Eip = LOWORD(context->Edx);
841 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
842 + (WORD)&((STACK16FRAME*)0)->bp;
844 argsize = 2 * *(WORD *)context->Esp + 2;
846 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
847 context->Eax = context16.Eax;
848 context->Edx = context16.Edx;
850 /* Copy modified buffers back to 32-bit stack */
851 memcpy( (LPBYTE)context->Esp,
852 (LPBYTE)CURRENT_STACK16 - argsize, argsize );
854 context->Esp += LOWORD(context16.Esp) -
855 ( OFFSETOF( NtCurrentTeb()->cur_stack ) - argsize );
857 #ifdef DEFINE_REGS_ENTRYPOINT
858 DEFINE_REGS_ENTRYPOINT( OT_32ThkLSF, 0, 0 );
861 /***********************************************************************
862 * ThunkInitLSF (KERNEL32.41)
863 * A thunk setup routine.
864 * Expects a pointer to a preinitialized thunkbuffer in the first argument
866 *| 00..03: unknown (pointer, check _41, _43, _46)
867 *| 04: EB1E jmp +0x20
869 *| 06..23: unknown (space for replacement code, check .90)
871 *| 24:>E800000000 call offset 29
872 *| 29:>58 pop eax ( target of call )
873 *| 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
874 *| 2F: BAxxxxxxxx mov edx,xxxxxxxx
875 *| 34: 68yyyyyyyy push KERNEL32.90
878 *| 3A: EB1E jmp +0x20
879 *| 3E ... 59: unknown (space for replacement code?)
880 *| 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
882 *| 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
884 *| 67: 68xxxxxxxx push xxxxxxxx
885 *| 6C: 68yyyyyyyy push KERNEL32.89
888 * This function checks if the code is there, and replaces the yyyyyyyy entries
889 * by the functionpointers.
890 * The thunkbuf looks like:
892 *| 00: DWORD length ? don't know exactly
893 *| 04: SEGPTR ptr ? where does it point to?
894 * The segpointer ptr is written into the first DWORD of 'thunk'.
897 * unclear, pointer to win16 thkbuffer?
899 LPVOID WINAPI ThunkInitLSF(
900 LPBYTE thunk, /* [in] win32 thunk */
901 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
902 DWORD len, /* [in] length of thkbuffer */
903 LPCSTR dll16, /* [in] name of win16 dll */
904 LPCSTR dll32 /* [in] name of win32 dll */
908 /* FIXME: add checks for valid code ... */
909 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
910 *(DWORD*)(thunk+0x35) = (DWORD)GetProcAddress(kernel32_handle,(LPSTR)90);
911 *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress(kernel32_handle,(LPSTR)89);
914 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
917 addr2 = MapSL(addr[1]);
919 *(DWORD*)thunk = (DWORD)addr2;
924 /***********************************************************************
925 * FT_PrologPrime (KERNEL32.89)
927 * This function is called from the relay code installed by
928 * ThunkInitLSF. It replaces the location from where it was
929 * called by a standard FT_Prolog call stub (which is 'primed'
930 * by inserting the correct target table pointer).
931 * Finally, it calls that stub.
933 * Input: ECX target number + flags (passed through to FT_Prolog)
934 * (ESP) offset of location where target table pointer
935 * is stored, relative to the start of the relay code
936 * (ESP+4) pointer to start of relay code
937 * (this is where the FT_Prolog call stub gets written to)
939 * Note: The two DWORD arguments get popped off the stack.
942 void WINAPI __regs_FT_PrologPrime( CONTEXT86 *context )
944 DWORD targetTableOffset;
947 /* Compensate for the fact that the Wine register relay code thought
948 we were being called, although we were in fact jumped to */
951 /* Write FT_Prolog call stub */
952 targetTableOffset = stack32_pop(context);
953 relayCode = (LPBYTE)stack32_pop(context);
954 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
956 /* Jump to the call stub just created */
957 context->Eip = (DWORD)relayCode;
959 #ifdef DEFINE_REGS_ENTRYPOINT
960 DEFINE_REGS_ENTRYPOINT( FT_PrologPrime, 0, 0 );
963 /***********************************************************************
964 * QT_ThunkPrime (KERNEL32.90)
966 * This function corresponds to FT_PrologPrime, but installs a
967 * call stub for QT_Thunk instead.
969 * Input: (EBP-4) target number (passed through to QT_Thunk)
970 * EDX target table pointer location offset
971 * EAX start of relay code
974 void WINAPI __regs_QT_ThunkPrime( CONTEXT86 *context )
976 DWORD targetTableOffset;
979 /* Compensate for the fact that the Wine register relay code thought
980 we were being called, although we were in fact jumped to */
983 /* Write QT_Thunk call stub */
984 targetTableOffset = context->Edx;
985 relayCode = (LPBYTE)context->Eax;
986 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
988 /* Jump to the call stub just created */
989 context->Eip = (DWORD)relayCode;
991 #ifdef DEFINE_REGS_ENTRYPOINT
992 DEFINE_REGS_ENTRYPOINT( QT_ThunkPrime, 0, 0 );
995 /***********************************************************************
996 * ThunkInitSL (KERNEL32.46)
997 * Another thunkbuf link routine.
998 * The start of the thunkbuf looks like this:
1000 * 04: SEGPTR address for thunkbuffer pointer
1003 VOID WINAPI ThunkInitSL(
1004 LPBYTE thunk, /* [in] start of thunkbuffer */
1005 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
1006 DWORD len, /* [in] length of thunkbuffer */
1007 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
1008 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
1012 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
1015 *(DWORD*)MapSL(addr[1]) = (DWORD)thunk;
1018 /**********************************************************************
1019 * SSInit (KERNEL.700)
1023 BOOL WINAPI SSInit16(void)
1028 /**********************************************************************
1029 * SSOnBigStack (KERNEL32.87)
1030 * Check if thunking is initialized (ss selector set up etc.)
1031 * We do that differently, so just return TRUE.
1036 BOOL WINAPI SSOnBigStack()
1038 TRACE("Yes, thunking is initialized\n");
1042 /**********************************************************************
1043 * SSConfirmSmallStack (KERNEL.704)
1045 * Abort if not on small stack.
1047 * This must be a register routine as it has to preserve *all* registers.
1049 void WINAPI SSConfirmSmallStack( CONTEXT86 *context )
1051 /* We are always on the small stack while in 16-bit code ... */
1054 /**********************************************************************
1055 * SSCall (KERNEL32.88)
1056 * One of the real thunking functions. This one seems to be for 32<->32
1057 * thunks. It should probably be capable of crossing processboundaries.
1059 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
1062 DWORD WINAPIV SSCall(
1063 DWORD nr, /* [in] number of argument bytes */
1064 DWORD flags, /* [in] FIXME: flags ? */
1065 FARPROC fun, /* [in] function to call */
1066 ... /* [in/out] arguments */
1069 DWORD *args = ((DWORD *)&fun) + 1;
1073 DPRINTF("(%ld,0x%08lx,%p,[",nr,flags,fun);
1074 for (i=0;i<nr/4;i++)
1075 DPRINTF("0x%08lx,",args[i]);
1079 case 0: ret = fun();
1081 case 4: ret = fun(args[0]);
1083 case 8: ret = fun(args[0],args[1]);
1085 case 12: ret = fun(args[0],args[1],args[2]);
1087 case 16: ret = fun(args[0],args[1],args[2],args[3]);
1089 case 20: ret = fun(args[0],args[1],args[2],args[3],args[4]);
1091 case 24: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
1093 case 28: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
1095 case 32: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
1097 case 36: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
1099 case 40: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
1101 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]);
1103 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]);
1106 WARN("Unsupported nr of arguments, %ld\n",nr);
1111 TRACE(" returning %ld ...\n",ret);
1115 /**********************************************************************
1116 * W32S_BackTo32 (KERNEL32.51)
1118 void WINAPI __regs_W32S_BackTo32( CONTEXT86 *context )
1120 LPDWORD stack = (LPDWORD)context->Esp;
1121 FARPROC proc = (FARPROC)context->Eip;
1123 context->Eax = proc( stack[1], stack[2], stack[3], stack[4], stack[5],
1124 stack[6], stack[7], stack[8], stack[9], stack[10] );
1126 context->Eip = stack32_pop(context);
1128 #ifdef DEFINE_REGS_ENTRYPOINT
1129 DEFINE_REGS_ENTRYPOINT( W32S_BackTo32, 0, 0 );
1132 /**********************************************************************
1133 * AllocSLCallback (KERNEL32.@)
1136 * Win95 uses some structchains for callbacks. It allocates them
1137 * in blocks of 100 entries, size 32 bytes each, layout:
1139 *| 0: PTR nextblockstart
1141 *| 8: WORD sel ( start points to blockstart)
1145 *| 18: PDB *owning_process;
1146 *| 1C: PTR blockstart
1148 * We ignore this for now. (Just a note for further developers)
1149 * FIXME: use this method, so we don't waste selectors...
1151 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
1152 * the 0x66 prefix switches from word->long registers.
1155 *| 6668x arg2 x pushl <arg2>
1157 *| EAx arg1 x jmpf <arg1>
1159 * returns the startaddress of this thunk.
1161 * Note, that they look very similar to the ones allocates by THUNK_Alloc.
1163 * A segmented pointer to the start of the thunk
1167 DWORD finalizer, /* [in] Finalizer function */
1168 DWORD callback /* [in] Callback function */
1170 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
1174 *x++=0x66;*x++=0x5a; /* popl edx */
1175 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
1176 *x++=0x66;*x++=0x52; /* pushl edx */
1177 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
1179 *(DWORD*)(thunk+18) = GetCurrentProcessId();
1181 sel = SELECTOR_AllocBlock( thunk, 32, WINE_LDT_FLAGS_CODE );
1185 /**********************************************************************
1186 * FreeSLCallback (KERNEL32.@)
1187 * Frees the specified 16->32 callback
1191 DWORD x /* [in] 16 bit callback (segmented pointer?) */
1193 FIXME("(0x%08lx): stub\n",x);
1197 /**********************************************************************
1198 * GetTEBSelectorFS (KERNEL.475)
1199 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1201 void WINAPI GetTEBSelectorFS16(void)
1203 CURRENT_STACK16->fs = wine_get_fs();
1206 /**********************************************************************
1207 * IsPeFormat (KERNEL.431)
1209 * Determine if a file is a PE format executable.
1213 * FALSE if the file could not be opened or is not a PE file.
1216 * If fn is given as NULL then the function expects hf16 to be valid.
1218 BOOL16 WINAPI IsPeFormat16(
1219 LPSTR fn, /* [in] Filename to the executable */
1220 HFILE16 hf16) /* [in] An open file handle */
1223 IMAGE_DOS_HEADER mzh;
1227 if (fn) hf16 = OpenFile16(fn,&ofs,OF_READ);
1228 if (hf16 == HFILE_ERROR16) return FALSE;
1229 _llseek16(hf16,0,SEEK_SET);
1230 if (sizeof(mzh)!=_lread16(hf16,&mzh,sizeof(mzh))) goto done;
1231 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) goto done;
1232 _llseek16(hf16,mzh.e_lfanew,SEEK_SET);
1233 if (sizeof(DWORD)!=_lread16(hf16,&xmagic,sizeof(DWORD))) goto done;
1234 ret = (xmagic == IMAGE_NT_SIGNATURE);
1241 /***********************************************************************
1242 * K32Thk1632Prolog (KERNEL32.@)
1244 void WINAPI __regs_K32Thk1632Prolog( CONTEXT86 *context )
1246 LPBYTE code = (LPBYTE)context->Eip - 5;
1248 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1249 of 16->32 thunks instead of using one of the standard methods!
1250 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1251 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1252 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1253 bypassed, which means it will crash the next time the 32-bit OLE
1254 code thunks down again to 16-bit (this *will* happen!).
1256 The following hack tries to recognize this situation.
1257 This is possible since the called stubs in OLECLI32/OLESVR32 all
1258 look exactly the same:
1259 00 E8xxxxxxxx call K32Thk1632Prolog
1260 05 FF55FC call [ebp-04]
1261 08 E8xxxxxxxx call K32Thk1632Epilog
1264 If we recognize this situation, we try to simulate the actions
1265 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1266 to our 32-bit stack, creating a proper STACK16FRAME and
1267 updating cur_stack. */
1269 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1270 && code[13] == 0x66 && code[14] == 0xCB)
1272 WORD stackSel = NtCurrentTeb()->stack_sel;
1273 DWORD stackBase = GetSelectorBase(stackSel);
1275 DWORD argSize = context->Ebp - context->Esp;
1276 char *stack16 = (char *)context->Esp - 4;
1277 char *stack32 = (char *)NtCurrentTeb()->cur_stack - argSize;
1278 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1280 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1281 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1283 memset(frame16, '\0', sizeof(STACK16FRAME));
1284 frame16->frame32 = (STACK32FRAME *)NtCurrentTeb()->cur_stack;
1285 frame16->ebp = context->Ebp;
1287 memcpy(stack32, stack16, argSize);
1288 NtCurrentTeb()->cur_stack = MAKESEGPTR(stackSel, (DWORD)frame16 - stackBase);
1290 context->Esp = (DWORD)stack32 + 4;
1291 context->Ebp = context->Esp + argSize;
1293 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1294 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1297 /* entry_point is never used again once the entry point has
1298 been called. Thus we re-use it to hold the Win16Lock count */
1299 ReleaseThunkLock(&CURRENT_STACK16->entry_point);
1301 #ifdef DEFINE_REGS_ENTRYPOINT
1302 DEFINE_REGS_ENTRYPOINT( K32Thk1632Prolog, 0, 0 );
1305 /***********************************************************************
1306 * K32Thk1632Epilog (KERNEL32.@)
1308 void WINAPI __regs_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);
1338 #ifdef DEFINE_REGS_ENTRYPOINT
1339 DEFINE_REGS_ENTRYPOINT( K32Thk1632Epilog, 0, 0 );
1342 /*********************************************************************
1343 * PK16FNF [KERNEL32.91]
1345 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1346 * string buffer with the 8.3 filename of a recently loaded 16-bit
1347 * module. It is unknown exactly what modules trigger this
1348 * mechanism or what purpose this serves. Win98 Explorer (and
1349 * probably also Win95 with IE 4 shell integration) calls this
1350 * several times during initialization.
1352 * FIXME: find out what this really does and make it work.
1354 void WINAPI PK16FNF(LPSTR strPtr)
1356 FIXME("(%p): stub\n", strPtr);
1358 /* fill in a fake filename that'll be easy to recognize */
1359 strcpy(strPtr, "WINESTUB.FIX");
1362 /***********************************************************************
1363 * 16->32 Flat Thunk routines:
1366 /***********************************************************************
1367 * ThunkConnect16 (KERNEL.651)
1368 * Connects a 32bit and a 16bit thunkbuffer.
1370 UINT WINAPI ThunkConnect16(
1371 LPSTR module16, /* [in] name of win16 dll */
1372 LPSTR module32, /* [in] name of win32 dll */
1373 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
1374 DWORD dwReason, /* [in] initialisation argument */
1375 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
1376 LPSTR thunkfun32, /* [in] win32 thunkfunction */
1377 WORD cs /* [in] CS of win16 dll */
1381 if (!strncmp(TD->magic, "SL01", 4))
1385 TRACE("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
1386 module16, (DWORD)TD, module32, thunkfun32, dwReason);
1388 else if (!strncmp(TD->magic, "LS01", 4))
1390 directionSL = FALSE;
1392 TRACE("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
1393 module16, (DWORD)TD, module32, thunkfun32, dwReason);
1397 ERR("Invalid magic %c%c%c%c\n",
1398 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
1404 case DLL_PROCESS_ATTACH:
1407 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
1408 struct ThunkDataSL *SL = SL16->fpData;
1412 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
1414 SL->common = SL16->common;
1415 SL->flags1 = SL16->flags1;
1416 SL->flags2 = SL16->flags2;
1418 SL->apiDB = MapSL(SL16->apiDatabase);
1419 SL->targetDB = NULL;
1421 lstrcpynA(SL->pszDll16, module16, 255);
1422 lstrcpynA(SL->pszDll32, module32, 255);
1424 /* We should create a SEGPTR to the ThunkDataSL,
1425 but since the contents are not in the original format,
1426 any access to this by 16-bit code would crash anyway. */
1432 if (SL->flags2 & 0x80000000)
1434 TRACE("Preloading 32-bit library\n");
1435 LoadLibraryA(module32);
1444 case DLL_PROCESS_DETACH:
1445 /* FIXME: cleanup */
1453 /***********************************************************************
1454 * C16ThkSL (KERNEL.630)
1457 void WINAPI C16ThkSL(CONTEXT86 *context)
1459 LPBYTE stub = MapSL(context->Eax), x = stub;
1460 WORD cs = wine_get_cs();
1461 WORD ds = wine_get_ds();
1463 /* We produce the following code:
1468 * mov edx, es:[ecx + $EDX]
1473 * call __FLATCS:__wine_call_from_16_thunk
1476 *x++ = 0xB8; *(WORD *)x = ds; x += sizeof(WORD);
1477 *x++ = 0x8E; *x++ = 0xC0;
1478 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1479 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1480 *x++ = 0x91; *(DWORD *)x = context->Edx; x += sizeof(DWORD);
1483 *x++ = 0x66; *x++ = 0x52;
1485 *x++ = 0x66; *x++ = 0x52;
1486 *x++ = 0x66; *x++ = 0x9A;
1487 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1488 *(WORD *)x = cs; x += sizeof(WORD);
1490 /* Jump to the stub code just created */
1491 context->Eip = LOWORD(context->Eax);
1492 context->SegCs = HIWORD(context->Eax);
1494 /* Since C16ThkSL got called by a jmp, we need to leave the
1495 original return address on the stack */
1499 /***********************************************************************
1500 * C16ThkSL01 (KERNEL.631)
1503 void WINAPI C16ThkSL01(CONTEXT86 *context)
1505 LPBYTE stub = MapSL(context->Eax), x = stub;
1509 struct ThunkDataSL16 *SL16 = MapSL(context->Edx);
1510 struct ThunkDataSL *td = SL16->fpData;
1512 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), (LPCSTR)631);
1513 WORD cs = wine_get_cs();
1517 ERR("ThunkConnect16 was not called!\n");
1521 TRACE("Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
1524 /* We produce the following code:
1533 * call __FLATCS:__wine_call_from_16_thunk
1536 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1537 *x++ = 0x66; *x++ = 0xBA; *(void **)x = td; x += sizeof(void *);
1538 *x++ = 0x9A; *(DWORD *)x = procAddress; x += sizeof(DWORD);
1541 *x++ = 0x66; *x++ = 0x52;
1543 *x++ = 0x66; *x++ = 0x52;
1544 *x++ = 0x66; *x++ = 0x9A;
1545 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1546 *(WORD *)x = cs; x += sizeof(WORD);
1548 /* Jump to the stub code just created */
1549 context->Eip = LOWORD(context->Eax);
1550 context->SegCs = HIWORD(context->Eax);
1552 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1553 orginal return address on the stack */
1558 struct ThunkDataSL *td = (struct ThunkDataSL *)context->Edx;
1559 DWORD targetNr = LOWORD(context->Ecx) / 4;
1560 struct SLTargetDB *tdb;
1562 TRACE("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1563 GetCurrentProcessId(), targetNr, (DWORD)td);
1565 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1566 if (tdb->process == GetCurrentProcessId())
1571 TRACE("Loading 32-bit library %s\n", td->pszDll32);
1572 LoadLibraryA(td->pszDll32);
1574 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1575 if (tdb->process == GetCurrentProcessId())
1581 context->Edx = tdb->targetTable[targetNr];
1583 TRACE("Call target is %08lx\n", context->Edx);
1587 WORD *stack = MapSL( MAKESEGPTR(context->SegSs, LOWORD(context->Esp)) );
1588 context->Edx = (context->Edx & ~0xffff) | HIWORD(td->apiDB[targetNr].errorReturnValue);
1589 context->Eax = (context->Eax & ~0xffff) | LOWORD(td->apiDB[targetNr].errorReturnValue);
1590 context->Eip = stack[2];
1591 context->SegCs = stack[3];
1592 context->Esp += td->apiDB[targetNr].nrArgBytes + 4;
1594 ERR("Process %08lx did not ThunkConnect32 %s to %s\n",
1595 GetCurrentProcessId(), td->pszDll32, td->pszDll16);
1601 /***********************************************************************
1602 * 16<->32 Thunklet/Callback API:
1605 #include "pshpack1.h"
1606 typedef struct _THUNKLET
1621 struct _THUNKLET *next;
1623 #include "poppack.h"
1625 #define THUNKLET_TYPE_LS 1
1626 #define THUNKLET_TYPE_SL 2
1628 static HANDLE ThunkletHeap = 0;
1629 static WORD ThunkletCodeSel;
1630 static THUNKLET *ThunkletAnchor = NULL;
1632 static FARPROC ThunkletSysthunkGlueLS = 0;
1633 static SEGPTR ThunkletSysthunkGlueSL = 0;
1635 static FARPROC ThunkletCallbackGlueLS = 0;
1636 static SEGPTR ThunkletCallbackGlueSL = 0;
1639 /* map a thunk allocated on ThunkletHeap to a 16-bit pointer */
1640 inline static SEGPTR get_segptr( void *thunk )
1642 if (!thunk) return 0;
1643 return MAKESEGPTR( ThunkletCodeSel, (char *)thunk - (char *)ThunkletHeap );
1646 /***********************************************************************
1649 static BOOL THUNK_Init(void)
1653 ThunkletHeap = HeapCreate( 0, 0x10000, 0x10000 );
1654 if (!ThunkletHeap) return FALSE;
1656 ThunkletCodeSel = SELECTOR_AllocBlock( (void *)ThunkletHeap, 0x10000, WINE_LDT_FLAGS_CODE );
1658 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1659 if (!thunk) return FALSE;
1661 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1662 *thunk++ = 0x58; /* popl eax */
1663 *thunk++ = 0xC3; /* ret */
1665 ThunkletSysthunkGlueSL = get_segptr( thunk );
1666 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1667 *thunk++ = 0xCB; /* lret */
1672 /***********************************************************************
1673 * SetThunkletCallbackGlue (KERNEL.560)
1675 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1677 ThunkletCallbackGlueLS = glueLS;
1678 ThunkletCallbackGlueSL = glueSL;
1682 /***********************************************************************
1683 * THUNK_FindThunklet
1685 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1686 DWORD glue, BYTE type )
1690 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1691 if ( thunk->type == type
1692 && thunk->target == target
1693 && thunk->relay == relay
1694 && ( type == THUNKLET_TYPE_LS ?
1695 ( thunk->glue == glue - (DWORD)&thunk->type )
1696 : ( thunk->glue == glue ) ) )
1702 /***********************************************************************
1703 * THUNK_AllocLSThunklet
1705 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1706 FARPROC glue, HTASK16 owner )
1708 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1712 TDB *pTask = GlobalLock16( owner );
1714 if (!ThunkletHeap) THUNK_Init();
1715 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1718 thunk->prefix_target = thunk->prefix_relay = 0x90;
1719 thunk->pushl_target = thunk->pushl_relay = 0x68;
1720 thunk->jmp_glue = 0xE9;
1722 thunk->target = (DWORD)target;
1723 thunk->relay = (DWORD)relay;
1724 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1726 thunk->type = THUNKLET_TYPE_LS;
1727 thunk->owner = pTask? pTask->hInstance : 0;
1729 thunk->next = ThunkletAnchor;
1730 ThunkletAnchor = thunk;
1733 return (FARPROC)thunk;
1736 /***********************************************************************
1737 * THUNK_AllocSLThunklet
1739 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1740 SEGPTR glue, HTASK16 owner )
1742 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1746 TDB *pTask = GlobalLock16( owner );
1748 if (!ThunkletHeap) THUNK_Init();
1749 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1752 thunk->prefix_target = thunk->prefix_relay = 0x66;
1753 thunk->pushl_target = thunk->pushl_relay = 0x68;
1754 thunk->jmp_glue = 0xEA;
1756 thunk->target = (DWORD)target;
1757 thunk->relay = (DWORD)relay;
1758 thunk->glue = (DWORD)glue;
1760 thunk->type = THUNKLET_TYPE_SL;
1761 thunk->owner = pTask? pTask->hInstance : 0;
1763 thunk->next = ThunkletAnchor;
1764 ThunkletAnchor = thunk;
1767 return get_segptr( thunk );
1770 /**********************************************************************
1773 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
1775 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1776 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1777 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1780 /**********************************************************************
1781 * IsSLThunklet (KERNEL.612)
1783 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1785 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1786 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1787 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1792 /***********************************************************************
1793 * AllocLSThunkletSysthunk (KERNEL.607)
1795 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1796 FARPROC relay, DWORD dummy )
1798 if (!ThunkletSysthunkGlueLS) THUNK_Init();
1799 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1800 ThunkletSysthunkGlueLS, GetCurrentTask() );
1803 /***********************************************************************
1804 * AllocSLThunkletSysthunk (KERNEL.608)
1806 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1807 SEGPTR relay, DWORD dummy )
1809 if (!ThunkletSysthunkGlueSL) THUNK_Init();
1810 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1811 ThunkletSysthunkGlueSL, GetCurrentTask() );
1815 /***********************************************************************
1816 * AllocLSThunkletCallbackEx (KERNEL.567)
1818 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1819 DWORD relay, HTASK16 task )
1821 THUNKLET *thunk = MapSL( target );
1822 if ( !thunk ) return NULL;
1824 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1825 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1826 return (FARPROC)thunk->target;
1828 return THUNK_AllocLSThunklet( target, relay,
1829 ThunkletCallbackGlueLS, task );
1832 /***********************************************************************
1833 * AllocSLThunkletCallbackEx (KERNEL.568)
1835 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1836 DWORD relay, HTASK16 task )
1838 THUNKLET *thunk = (THUNKLET *)target;
1839 if ( !thunk ) return 0;
1841 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1842 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1843 return (SEGPTR)thunk->target;
1845 return THUNK_AllocSLThunklet( target, relay,
1846 ThunkletCallbackGlueSL, task );
1849 /***********************************************************************
1850 * AllocLSThunkletCallback (KERNEL.561)
1851 * AllocLSThunkletCallback_dup (KERNEL.606)
1853 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1855 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1858 /***********************************************************************
1859 * AllocSLThunkletCallback (KERNEL.562)
1860 * AllocSLThunkletCallback_dup (KERNEL.605)
1862 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1864 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1867 /***********************************************************************
1868 * FindLSThunkletCallback (KERNEL.563)
1869 * FindLSThunkletCallback_dup (KERNEL.609)
1871 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1873 THUNKLET *thunk = MapSL( target );
1874 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1875 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1876 return (FARPROC)thunk->target;
1878 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1879 (DWORD)ThunkletCallbackGlueLS,
1881 return (FARPROC)thunk;
1884 /***********************************************************************
1885 * FindSLThunkletCallback (KERNEL.564)
1886 * FindSLThunkletCallback_dup (KERNEL.610)
1888 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1890 THUNKLET *thunk = (THUNKLET *)target;
1891 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1892 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1893 return (SEGPTR)thunk->target;
1895 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1896 (DWORD)ThunkletCallbackGlueSL,
1898 return get_segptr( thunk );
1902 /***********************************************************************
1903 * FreeThunklet (KERNEL.611)
1905 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1911 /***********************************************************************
1912 * Callback Client API
1915 #define N_CBC_FIXED 20
1916 #define N_CBC_VARIABLE 10
1917 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1919 static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
1920 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1922 /***********************************************************************
1923 * RegisterCBClient (KERNEL.619)
1925 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1926 SEGPTR relay16, FARPROC *relay32 )
1928 /* Search for free Callback ID */
1930 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1931 if ( !CBClientRelay16[ wCBCId ] )
1934 /* Register Callback ID */
1935 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1937 CBClientRelay16[ wCBCId ] = relay16;
1938 CBClientRelay32[ wCBCId ] = relay32;
1946 /***********************************************************************
1947 * UnRegisterCBClient (KERNEL.622)
1949 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1950 SEGPTR relay16, FARPROC *relay32 )
1952 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1953 && CBClientRelay16[ wCBCId ] == relay16
1954 && CBClientRelay32[ wCBCId ] == relay32 )
1956 CBClientRelay16[ wCBCId ] = 0;
1957 CBClientRelay32[ wCBCId ] = 0;
1966 /***********************************************************************
1967 * InitCBClient (KERNEL.623)
1969 void WINAPI InitCBClient16( FARPROC glueLS )
1971 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1972 SEGPTR glueSL = (SEGPTR)GetProcAddress16( kernel, (LPCSTR)604 );
1974 SetThunkletCallbackGlue16( glueLS, glueSL );
1977 /***********************************************************************
1978 * CBClientGlueSL (KERNEL.604)
1980 void WINAPI CBClientGlueSL( CONTEXT86 *context )
1982 /* Create stack frame */
1983 SEGPTR stackSeg = stack16_push( 12 );
1984 LPWORD stackLin = MapSL( stackSeg );
1985 SEGPTR glue, *glueTab;
1987 stackLin[3] = (WORD)context->Ebp;
1988 stackLin[2] = (WORD)context->Esi;
1989 stackLin[1] = (WORD)context->Edi;
1990 stackLin[0] = (WORD)context->SegDs;
1992 context->Ebp = OFFSETOF( stackSeg ) + 6;
1993 context->Esp = OFFSETOF( stackSeg ) - 4;
1996 /* Jump to 16-bit relay code */
1997 glueTab = MapSL( CBClientRelay16[ stackLin[5] ] );
1998 glue = glueTab[ stackLin[4] ];
1999 context->SegCs = SELECTOROF( glue );
2000 context->Eip = OFFSETOF ( glue );
2003 /***********************************************************************
2004 * CBClientThunkSL (KERNEL.620)
2006 extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi );
2007 void WINAPI CBClientThunkSL( CONTEXT86 *context )
2009 /* Call 32-bit relay code */
2011 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2012 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
2014 context->Eax = CALL32_CBClient( proc, args, &context->Esi );
2017 /***********************************************************************
2018 * CBClientThunkSLEx (KERNEL.621)
2020 extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs );
2021 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
2023 /* Call 32-bit relay code */
2025 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2026 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
2030 context->Eax = CALL32_CBClientEx( proc, args, &context->Esi, &nArgs );
2032 /* Restore registers saved by CBClientGlueSL */
2033 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
2034 context->Ebp = (context->Ebp & ~0xffff) | stackLin[3];
2035 context->Esi = (context->Esi & ~0xffff) | stackLin[2];
2036 context->Edi = (context->Edi & ~0xffff) | stackLin[1];
2037 context->SegDs = stackLin[0];
2038 context->Esp += 16+nArgs;
2040 /* Return to caller of CBClient thunklet */
2041 context->SegCs = stackLin[9];
2042 context->Eip = stackLin[8];
2046 /***********************************************************************
2047 * Get16DLLAddress (KERNEL32.@)
2049 * This function is used by a Win32s DLL if it wants to call a Win16 function.
2050 * A 16:16 segmented pointer to the function is returned.
2051 * Written without any docu.
2053 SEGPTR WINAPI Get16DLLAddress(HMODULE16 handle, LPSTR func_name)
2055 static WORD code_sel32;
2061 if (!ThunkletHeap) THUNK_Init();
2062 code_sel32 = SELECTOR_AllocBlock( (void *)ThunkletHeap, 0x10000,
2063 WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
2064 if (!code_sel32) return 0;
2066 if (!(thunk = HeapAlloc( ThunkletHeap, 0, 32 ))) return 0;
2068 if (!handle) handle = GetModuleHandle16("WIN32S16");
2069 proc_16 = GetProcAddress16(handle, func_name);
2071 /* movl proc_16, $edx */
2073 *(FARPROC16 *)thunk = proc_16;
2074 thunk += sizeof(FARPROC16);
2078 *(FARPROC *)thunk = GetProcAddress(kernel32_handle,"QT_Thunk");
2079 thunk += sizeof(FARPROC16);
2080 *(WORD *)thunk = wine_get_cs();
2082 return MAKESEGPTR( code_sel32, (char *)thunk - (char *)ThunkletHeap );
2086 /***********************************************************************
2087 * GetWin16DOSEnv (KERNEL32.34)
2088 * Returns some internal value.... probably the default environment database?
2090 DWORD WINAPI GetWin16DOSEnv()
2092 FIXME("stub, returning 0\n");
2096 /**********************************************************************
2097 * GetPK16SysVar (KERNEL32.92)
2099 LPVOID WINAPI GetPK16SysVar(void)
2101 static BYTE PK16SysVar[128];
2107 /**********************************************************************
2108 * CommonUnimpStub (KERNEL32.17)
2110 void WINAPI __regs_CommonUnimpStub( CONTEXT86 *context )
2112 FIXME("generic stub: %s\n", ((LPSTR)context->Eax ? (LPSTR)context->Eax : "?"));
2114 switch ((context->Ecx >> 4) & 0x0f)
2116 case 15: context->Eax = -1; break;
2117 case 14: context->Eax = 0x78; break;
2118 case 13: context->Eax = 0x32; break;
2119 case 1: context->Eax = 1; break;
2120 default: context->Eax = 0; break;
2123 context->Esp += (context->Ecx & 0x0f) * 4;
2125 #ifdef DEFINE_REGS_ENTRYPOINT
2126 DEFINE_REGS_ENTRYPOINT( CommonUnimpStub, 0, 0 );
2129 /**********************************************************************
2130 * HouseCleanLogicallyDeadHandles (KERNEL32.33)
2132 void WINAPI HouseCleanLogicallyDeadHandles(void)
2134 /* Whatever this is supposed to do, our handles probably
2135 don't need it :-) */
2138 /**********************************************************************
2141 BOOL WINAPI _KERNEL32_100(HANDLE threadid,DWORD exitcode,DWORD x)
2143 FIXME("(%p,%ld,0x%08lx): stub\n",threadid,exitcode,x);
2147 /**********************************************************************
2150 * Checks whether the clock has to be switched from daylight
2151 * savings time to standard time or vice versa.
2154 DWORD WINAPI _KERNEL32_99(DWORD x)
2156 FIXME("(0x%08lx): stub\n",x);
2161 /**********************************************************************
2164 * Real prototype is:
2165 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
2167 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT86 *context )
2169 /* Note: we don't save the current ss, as the catch buffer is */
2170 /* only 9 words long. Hopefully no one will have the silly */
2171 /* idea to change the current stack before calling Throw()... */
2185 lpbuf[0] = LOWORD(context->Eip);
2186 lpbuf[1] = context->SegCs;
2187 /* Windows pushes 4 more words before saving sp */
2188 lpbuf[2] = LOWORD(context->Esp) - 4 * sizeof(WORD);
2189 lpbuf[3] = LOWORD(context->Ebp);
2190 lpbuf[4] = LOWORD(context->Esi);
2191 lpbuf[5] = LOWORD(context->Edi);
2192 lpbuf[6] = context->SegDs;
2194 lpbuf[8] = context->SegSs;
2195 context->Eax &= ~0xffff; /* Return 0 */
2199 /**********************************************************************
2202 * Real prototype is:
2203 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
2205 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT86 *context )
2207 STACK16FRAME *pFrame;
2208 STACK32FRAME *frame32;
2210 context->Eax = (context->Eax & ~0xffff) | (WORD)retval;
2212 /* Find the frame32 corresponding to the frame16 we are jumping to */
2213 pFrame = CURRENT_STACK16;
2214 frame32 = pFrame->frame32;
2215 while (frame32 && frame32->frame16)
2217 if (OFFSETOF(frame32->frame16) < OFFSETOF(NtCurrentTeb()->cur_stack))
2218 break; /* Something strange is going on */
2219 if (OFFSETOF(frame32->frame16) > lpbuf[2])
2221 /* We found the right frame */
2222 pFrame->frame32 = frame32;
2225 frame32 = ((STACK16FRAME *)MapSL(frame32->frame16))->frame32;
2227 RtlUnwind( &pFrame->frame32->frame, NULL, NULL, 0 );
2229 context->Eip = lpbuf[0];
2230 context->SegCs = lpbuf[1];
2231 context->Esp = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
2232 context->Ebp = lpbuf[3];
2233 context->Esi = lpbuf[4];
2234 context->Edi = lpbuf[5];
2235 context->SegDs = lpbuf[6];
2237 if (lpbuf[8] != context->SegSs)
2238 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );