2 * KERNEL32 thunks and other undocumented stuff
4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1997, 1998 Marcus Meissner
6 * Copyright 1998 Ulrich Weigand
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
27 #include <sys/types.h>
41 #include "wine/winbase16.h"
43 #include "wine/debug.h"
44 #include "wine/library.h"
45 #include "kernel_private.h"
46 #include "kernel16_private.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
50 struct ThunkDataCommon
52 char magic[4]; /* 00 */
53 DWORD checksum; /* 04 */
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 to 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;
149 SEGPTR CALL32_CBClient_RetAddr = 0;
150 SEGPTR CALL32_CBClientEx_RetAddr = 0;
152 extern void __wine_call_from_16_thunk();
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);
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 __regs_LogApiThkLSF( LPSTR func, CONTEXT86 *context )
190 TRACE( "%s\n", debugstr_a(func) );
192 DEFINE_REGS_ENTRYPOINT( LogApiThkLSF, 1 )
194 /***********************************************************************
195 * LogApiThkSL (KERNEL32.44)
197 * NOTE: needs to preserve all registers!
199 void WINAPI __regs_LogApiThkSL( LPSTR func, CONTEXT86 *context )
201 TRACE( "%s\n", debugstr_a(func) );
203 DEFINE_REGS_ENTRYPOINT( LogApiThkSL, 1 )
205 /***********************************************************************
206 * LogCBThkSL (KERNEL32.47)
208 * NOTE: needs to preserve all registers!
210 void WINAPI __regs_LogCBThkSL( LPSTR func, CONTEXT86 *context )
212 TRACE( "%s\n", debugstr_a(func) );
214 DEFINE_REGS_ENTRYPOINT( LogCBThkSL, 1 )
216 /***********************************************************************
217 * Generates a FT_Prolog call.
219 * 0FB6D1 movzbl edx,cl
220 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
221 * 68xxxxxxxx push FT_Prolog
224 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
228 *x++ = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
229 *x++ = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
230 x+=4; /* mov edx, [4*edx + targetTable] */
231 *x++ = 0x68; *(DWORD*)x = (DWORD)GetProcAddress(kernel32_handle,"FT_Prolog");
232 x+=4; /* push FT_Prolog */
233 *x++ = 0xC3; /* lret */
234 /* fill rest with 0xCC / int 3 */
237 /***********************************************************************
238 * _write_qtthunk (internal)
239 * Generates a QT_Thunk style call.
242 * 8A4DFC mov cl , [ebp-04]
243 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
244 * B8yyyyyyyy mov eax, QT_Thunk
247 static void _write_qtthunk(
248 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
249 DWORD *targetTable /* [in] start of thunk (for index lookup) */
254 *x++ = 0x33;*x++=0xC9; /* xor ecx,ecx */
255 *x++ = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
256 *x++ = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
257 x+=4; /* mov edx, [4*ecx + targetTable */
258 *x++ = 0xB8; *(DWORD*)x = (DWORD)GetProcAddress(kernel32_handle,"QT_Thunk");
259 x+=4; /* mov eax , QT_Thunk */
260 *x++ = 0xFF; *x++ = 0xE0; /* jmp eax */
261 /* should fill the rest of the 32 bytes with 0xCC */
264 /***********************************************************************
267 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
268 struct ThunkDataCommon *TD32, DWORD checksum)
270 struct ThunkDataCommon *TD16;
274 if ((hmod = LoadLibrary16(module)) <= 32)
276 ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
277 module, func, module32, module, hmod);
281 if ( !(ordinal = NE_GetOrdinal(hmod, func))
282 || !(TD16 = MapSL((SEGPTR)NE_GetEntryPointEx(hmod, ordinal, FALSE))))
284 ERR("Unable to find thunk data '%s' in %s, required by %s (conflicting/incorrect DLL versions !?).\n",
285 func, module, module32);
289 if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
291 ERR("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
292 module, func, module32,
293 TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
294 TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
298 if (TD32 && TD16->checksum != TD32->checksum)
300 ERR("(%s, %s, %s): Wrong checksum %08x (should be %08x)\n",
301 module, func, module32, TD16->checksum, TD32->checksum);
305 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
307 ERR("(%s, %s, %s): Wrong checksum %08x (should be %08x)\n",
308 module, func, module32, *(LPDWORD)TD16, checksum);
315 /***********************************************************************
316 * GetThunkStuff (KERNEL32.53)
318 LPVOID WINAPI GetThunkStuff(LPCSTR module, LPCSTR func)
320 return _loadthunk(module, func, "<kernel>", NULL, 0L);
323 /***********************************************************************
324 * GetThunkBuff (KERNEL32.52)
325 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
327 LPVOID WINAPI GetThunkBuff(void)
329 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
332 /***********************************************************************
333 * ThunkConnect32 (KERNEL32.@)
334 * Connects a 32bit and a 16bit thunkbuffer.
336 UINT WINAPI ThunkConnect32(
337 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
338 LPSTR thunkfun16, /* [in] win16 thunkfunction */
339 LPSTR module16, /* [in] name of win16 dll */
340 LPSTR module32, /* [in] name of win32 dll */
341 HMODULE hmod32, /* [in] hmodule of win32 dll */
342 DWORD dwReason /* [in] initialisation argument */
346 if (!strncmp(TD->magic, "SL01", 4))
350 TRACE("SL01 thunk %s (%p) <- %s (%s), Reason: %d\n",
351 module32, TD, module16, thunkfun16, dwReason);
353 else if (!strncmp(TD->magic, "LS01", 4))
357 TRACE("LS01 thunk %s (%p) -> %s (%s), Reason: %d\n",
358 module32, TD, module16, thunkfun16, dwReason);
362 ERR("Invalid magic %c%c%c%c\n",
363 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
369 case DLL_PROCESS_ATTACH:
371 struct ThunkDataCommon *TD16;
372 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
377 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
378 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
379 struct SLTargetDB *tdb;
381 if (SL16->fpData == NULL)
383 ERR("ThunkConnect16 was not called!\n");
387 SL32->data = SL16->fpData;
389 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
390 tdb->process = GetCurrentProcessId();
391 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
393 tdb->next = SL32->data->targetDB; /* FIXME: not thread-safe! */
394 SL32->data->targetDB = tdb;
396 TRACE("Process %08x allocated TargetDB entry for ThunkDataSL %p\n",
397 GetCurrentProcessId(), SL32->data);
401 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
402 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
404 LS32->targetTable = MapSL(LS16->targetTable);
406 /* write QT_Thunk and FT_Prolog stubs */
407 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk, LS32->targetTable);
408 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
413 case DLL_PROCESS_DETACH:
421 /**********************************************************************
422 * QT_Thunk (KERNEL32.@)
424 * The target address is in EDX.
425 * The 16bit arguments start at ESP.
426 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
427 * So the stack layout is 16bit argument bytes and then the 64 byte
429 * The scratch buffer is used as work space by Windows' QT_Thunk
431 * As the programs unfortunately don't always provide a fixed size
432 * scratch buffer (danger, stack corruption ahead !!), we simply resort
433 * to copying over the whole EBP-ESP range to the 16bit stack
434 * (as there's no way to safely figure out the param count
435 * due to this misbehaviour of some programs).
438 * See DDJ article 9614c for a very good description of QT_Thunk (also
439 * available online !).
441 * FIXME: DDJ talks of certain register usage rules; I'm not sure
442 * whether we cover this 100%.
444 void WINAPI __regs_QT_Thunk( CONTEXT86 *context )
449 context16 = *context;
451 context16.SegFs = wine_get_fs();
452 context16.SegGs = wine_get_gs();
453 context16.SegCs = HIWORD(context->Edx);
454 context16.Eip = LOWORD(context->Edx);
455 /* point EBP to the STACK16FRAME on the stack
456 * for the call_to_16 to set up the register content on calling */
457 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
460 * used to be (problematic):
461 * argsize = context->Ebp - context->Esp - 0x40;
462 * due to some programs abusing the API, we better assume the full
463 * EBP - ESP range for copying instead: */
464 argsize = context->Ebp - context->Esp;
466 /* ok, too much is insane; let's limit param count a bit again */
468 argsize = 64; /* 32 WORDs */
470 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
471 context->Eax = context16.Eax;
472 context->Edx = context16.Edx;
473 context->Ecx = context16.Ecx;
475 /* make sure to update the Win32 ESP, too, in order to throw away
476 * the number of parameters that the Win16 function
477 * accepted (that it popped from the corresponding Win16 stack) */
478 context->Esp += LOWORD(context16.Esp) -
479 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
481 DEFINE_REGS_ENTRYPOINT( QT_Thunk, 0 )
484 /**********************************************************************
485 * FT_Prolog (KERNEL32.@)
487 * The set of FT_... thunk routines is used instead of QT_Thunk,
488 * if structures have to be converted from 32-bit to 16-bit
489 * (change of member alignment, conversion of members).
491 * The thunk function (as created by the thunk compiler) calls
492 * FT_Prolog at the beginning, to set up a stack frame and
493 * allocate a 64 byte buffer on the stack.
494 * The input parameters (target address and some flags) are
495 * saved for later use by FT_Thunk.
497 * Input: EDX 16-bit target address (SEGPTR)
498 * CX bits 0..7 target number (in target table)
499 * bits 8..9 some flags (unclear???)
500 * bits 10..15 number of DWORD arguments
502 * Output: A new stackframe is created, and a 64 byte buffer
503 * allocated on the stack. The layout of the stack
504 * on return is as follows:
506 * (ebp+4) return address to caller of thunk function
508 * (ebp-4) saved EBX register of caller
509 * (ebp-8) saved ESI register of caller
510 * (ebp-12) saved EDI register of caller
511 * (ebp-16) saved ECX register, containing flags
512 * (ebp-20) bitmap containing parameters that are to be converted
513 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
514 * filled in by the thunk code before calling FT_Thunk
518 * (ebp-48) saved EAX register of caller (unclear, never restored???)
519 * (ebp-52) saved EDX register, containing 16-bit thunk target
524 * ESP is EBP-64 after return.
527 void WINAPI __regs_FT_Prolog( CONTEXT86 *context )
529 /* Build stack frame */
530 stack32_push(context, context->Ebp);
531 context->Ebp = context->Esp;
533 /* Allocate 64-byte Thunk Buffer */
535 memset((char *)context->Esp, '\0', 64);
537 /* Store Flags (ECX) and Target Address (EDX) */
538 /* Save other registers to be restored later */
539 *(DWORD *)(context->Ebp - 4) = context->Ebx;
540 *(DWORD *)(context->Ebp - 8) = context->Esi;
541 *(DWORD *)(context->Ebp - 12) = context->Edi;
542 *(DWORD *)(context->Ebp - 16) = context->Ecx;
544 *(DWORD *)(context->Ebp - 48) = context->Eax;
545 *(DWORD *)(context->Ebp - 52) = context->Edx;
547 DEFINE_REGS_ENTRYPOINT( FT_Prolog, 0 )
549 /**********************************************************************
550 * FT_Thunk (KERNEL32.@)
552 * This routine performs the actual call to 16-bit code,
553 * similar to QT_Thunk. The differences are:
554 * - The call target is taken from the buffer created by FT_Prolog
555 * - Those arguments requested by the thunk code (by setting the
556 * corresponding bit in the bitmap at EBP-20) are converted
557 * from 32-bit pointers to segmented pointers (those pointers
558 * are guaranteed to point to structures copied to the stack
559 * by the thunk code, so we always use the 16-bit stack selector
560 * for those addresses).
562 * The bit #i of EBP-20 corresponds here to the DWORD starting at
565 * FIXME: It is unclear what happens if there are more than 32 WORDs
566 * of arguments, so that the single DWORD bitmap is no longer
569 void WINAPI __regs_FT_Thunk( CONTEXT86 *context )
571 DWORD mapESPrelative = *(DWORD *)(context->Ebp - 20);
572 DWORD callTarget = *(DWORD *)(context->Ebp - 52);
579 context16 = *context;
581 context16.SegFs = wine_get_fs();
582 context16.SegGs = wine_get_gs();
583 context16.SegCs = HIWORD(callTarget);
584 context16.Eip = LOWORD(callTarget);
585 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
587 argsize = context->Ebp-context->Esp-0x40;
588 if (argsize > sizeof(newstack)) argsize = sizeof(newstack);
589 oldstack = (LPBYTE)context->Esp;
591 memcpy( newstack, oldstack, argsize );
593 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
594 if (mapESPrelative & (1 << i))
596 SEGPTR *arg = (SEGPTR *)newstack[i];
597 *arg = MAKESEGPTR(SELECTOROF(NtCurrentTeb()->WOW32Reserved),
598 OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize
599 + (*(LPBYTE *)arg - oldstack));
602 WOWCallback16Ex( 0, WCB16_REGS, argsize, newstack, (DWORD *)&context16 );
603 context->Eax = context16.Eax;
604 context->Edx = context16.Edx;
605 context->Ecx = context16.Ecx;
607 context->Esp += LOWORD(context16.Esp) -
608 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
610 /* Copy modified buffers back to 32-bit stack */
611 memcpy( oldstack, newstack, argsize );
613 DEFINE_REGS_ENTRYPOINT( FT_Thunk, 0 )
615 /***********************************************************************
616 * FT_Exit0 (KERNEL32.@)
617 * FT_Exit4 (KERNEL32.@)
618 * FT_Exit8 (KERNEL32.@)
619 * FT_Exit12 (KERNEL32.@)
620 * FT_Exit16 (KERNEL32.@)
621 * FT_Exit20 (KERNEL32.@)
622 * FT_Exit24 (KERNEL32.@)
623 * FT_Exit28 (KERNEL32.@)
624 * FT_Exit32 (KERNEL32.@)
625 * FT_Exit36 (KERNEL32.@)
626 * FT_Exit40 (KERNEL32.@)
627 * FT_Exit44 (KERNEL32.@)
628 * FT_Exit48 (KERNEL32.@)
629 * FT_Exit52 (KERNEL32.@)
630 * FT_Exit56 (KERNEL32.@)
632 * One of the FT_ExitNN functions is called at the end of the thunk code.
633 * It removes the stack frame created by FT_Prolog, moves the function
634 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
635 * value, but the thunk code has moved it from EAX to EBX in the
636 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
637 * and perform a return to the CALLER of the thunk code (while removing
638 * the given number of arguments from the caller's stack).
640 #define FT_EXIT_RESTORE_REGS \
641 "movl %ebx,%eax\n\t" \
642 "movl -4(%ebp),%ebx\n\t" \
643 "movl -8(%ebp),%esi\n\t" \
644 "movl -12(%ebp),%edi\n\t" \
647 #define DEFINE_FT_Exit(n) \
648 __ASM_GLOBAL_FUNC( FT_Exit ## n, FT_EXIT_RESTORE_REGS "ret $" #n )
667 /***********************************************************************
668 * ThunkInitLS (KERNEL32.43)
669 * A thunkbuffer link routine
670 * The thunkbuf looks like:
672 * 00: DWORD length ? don't know exactly
673 * 04: SEGPTR ptr ? where does it point to?
674 * The pointer ptr is written into the first DWORD of 'thunk'.
675 * (probably correctly implemented)
678 * segmented pointer to thunk?
680 DWORD WINAPI ThunkInitLS(
681 LPDWORD thunk, /* [in] win32 thunk */
682 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
683 DWORD len, /* [in] thkbuffer length */
684 LPCSTR dll16, /* [in] name of win16 dll */
685 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
689 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
699 /***********************************************************************
700 * Common32ThkLS (KERNEL32.45)
702 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
703 * style thunks. The basic difference is that the parameter conversion
704 * is done completely on the *16-bit* side here. Thus we do not call
705 * the 16-bit target directly, but call a common entry point instead.
706 * This entry function then calls the target according to the target
707 * number passed in the DI register.
709 * Input: EAX SEGPTR to the common 16-bit entry point
710 * CX offset in thunk table (target number * 4)
711 * DX error return value if execution fails (unclear???)
712 * EDX.HI number of DWORD parameters
714 * (Note that we need to move the thunk table offset from CX to DI !)
716 * The called 16-bit stub expects its stack to look like this:
718 * (esp+40) 32-bit arguments
720 * (esp+8) 32 byte of stack space available as buffer
721 * (esp) 8 byte return address for use with 0x66 lret
723 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
724 * and uses the EAX register to return a DWORD return value.
725 * Thus we need to use a special assembly glue routine
726 * (CallRegisterLongProc instead of CallRegisterShortProc).
728 * Finally, we return to the caller, popping the arguments off
729 * the stack. The number of arguments to be popped is returned
730 * in the BL register by the called 16-bit routine.
733 void WINAPI __regs_Common32ThkLS( CONTEXT86 *context )
738 context16 = *context;
740 context16.SegFs = wine_get_fs();
741 context16.SegGs = wine_get_gs();
742 context16.Edi = LOWORD(context->Ecx);
743 context16.SegCs = HIWORD(context->Eax);
744 context16.Eip = LOWORD(context->Eax);
745 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
747 argsize = HIWORD(context->Edx) * 4;
749 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
750 if (context->Edx == context->Eip)
753 /* Note: the first 32 bytes we copy are just garbage from the 32-bit stack, in order to reserve
754 * the space. It is safe to do that since the register function prefix has reserved
755 * a lot more space than that below context->Esp.
757 WOWCallback16Ex( 0, WCB16_REGS, argsize + 32, (LPBYTE)context->Esp - 32, (DWORD *)&context16 );
758 context->Eax = context16.Eax;
760 /* Clean up caller's stack frame */
761 context->Esp += LOBYTE(context16.Ebx);
763 DEFINE_REGS_ENTRYPOINT( Common32ThkLS, 0 )
765 /***********************************************************************
766 * OT_32ThkLSF (KERNEL32.40)
768 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
769 * argument processing is done on both the 32-bit and the 16-bit side:
770 * The 32-bit side prepares arguments, copying them onto the stack.
772 * When this routine is called, the first word on the stack is the
773 * number of argument bytes prepared by the 32-bit code, and EDX
774 * contains the 16-bit target address.
776 * The called 16-bit routine is another relaycode, doing further
777 * argument processing and then calling the real 16-bit target
778 * whose address is stored at [bp-04].
780 * The call proceeds using a normal CallRegisterShortProc.
781 * After return from the 16-bit relaycode, the arguments need
782 * to be copied *back* to the 32-bit stack, since the 32-bit
783 * relaycode processes output parameters.
785 * Note that we copy twice the number of arguments, since some of the
786 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
787 * arguments of the caller!
789 * (Note that this function seems only to be used for
790 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
792 void WINAPI __regs_OT_32ThkLSF( CONTEXT86 *context )
797 context16 = *context;
799 context16.SegFs = wine_get_fs();
800 context16.SegGs = wine_get_gs();
801 context16.SegCs = HIWORD(context->Edx);
802 context16.Eip = LOWORD(context->Edx);
803 context16.Ebp = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
805 argsize = 2 * *(WORD *)context->Esp + 2;
807 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
808 context->Eax = context16.Eax;
809 context->Edx = context16.Edx;
811 /* Copy modified buffers back to 32-bit stack */
812 memcpy( (LPBYTE)context->Esp,
813 (LPBYTE)CURRENT_STACK16 - argsize, argsize );
815 context->Esp += LOWORD(context16.Esp) -
816 ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
818 DEFINE_REGS_ENTRYPOINT( OT_32ThkLSF, 0 )
820 /***********************************************************************
821 * ThunkInitLSF (KERNEL32.41)
822 * A thunk setup routine.
823 * Expects a pointer to a preinitialized thunkbuffer in the first argument
825 *| 00..03: unknown (pointer, check _41, _43, _46)
826 *| 04: EB1E jmp +0x20
828 *| 06..23: unknown (space for replacement code, check .90)
830 *| 24:>E800000000 call offset 29
831 *| 29:>58 pop eax ( target of call )
832 *| 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
833 *| 2F: BAxxxxxxxx mov edx,xxxxxxxx
834 *| 34: 68yyyyyyyy push KERNEL32.90
837 *| 3A: EB1E jmp +0x20
838 *| 3E ... 59: unknown (space for replacement code?)
839 *| 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
841 *| 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
843 *| 67: 68xxxxxxxx push xxxxxxxx
844 *| 6C: 68yyyyyyyy push KERNEL32.89
847 * This function checks if the code is there, and replaces the yyyyyyyy entries
848 * by the functionpointers.
849 * The thunkbuf looks like:
851 *| 00: DWORD length ? don't know exactly
852 *| 04: SEGPTR ptr ? where does it point to?
853 * The segpointer ptr is written into the first DWORD of 'thunk'.
856 * unclear, pointer to win16 thkbuffer?
858 LPVOID WINAPI ThunkInitLSF(
859 LPBYTE thunk, /* [in] win32 thunk */
860 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
861 DWORD len, /* [in] length of thkbuffer */
862 LPCSTR dll16, /* [in] name of win16 dll */
863 LPCSTR dll32 /* [in] name of win32 dll */
867 /* FIXME: add checks for valid code ... */
868 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
869 *(DWORD*)(thunk+0x35) = (DWORD)GetProcAddress(kernel32_handle,(LPSTR)90);
870 *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress(kernel32_handle,(LPSTR)89);
873 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
876 addr2 = MapSL(addr[1]);
878 *(DWORD*)thunk = (DWORD)addr2;
883 /***********************************************************************
884 * FT_PrologPrime (KERNEL32.89)
886 * This function is called from the relay code installed by
887 * ThunkInitLSF. It replaces the location from where it was
888 * called by a standard FT_Prolog call stub (which is 'primed'
889 * by inserting the correct target table pointer).
890 * Finally, it calls that stub.
892 * Input: ECX target number + flags (passed through to FT_Prolog)
893 * (ESP) offset of location where target table pointer
894 * is stored, relative to the start of the relay code
895 * (ESP+4) pointer to start of relay code
896 * (this is where the FT_Prolog call stub gets written to)
898 * Note: The two DWORD arguments get popped off the stack.
901 void WINAPI __regs_FT_PrologPrime( CONTEXT86 *context )
903 DWORD targetTableOffset;
906 /* Compensate for the fact that the Wine register relay code thought
907 we were being called, although we were in fact jumped to */
910 /* Write FT_Prolog call stub */
911 targetTableOffset = stack32_pop(context);
912 relayCode = (LPBYTE)stack32_pop(context);
913 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
915 /* Jump to the call stub just created */
916 context->Eip = (DWORD)relayCode;
918 DEFINE_REGS_ENTRYPOINT( FT_PrologPrime, 0 )
920 /***********************************************************************
921 * QT_ThunkPrime (KERNEL32.90)
923 * This function corresponds to FT_PrologPrime, but installs a
924 * call stub for QT_Thunk instead.
926 * Input: (EBP-4) target number (passed through to QT_Thunk)
927 * EDX target table pointer location offset
928 * EAX start of relay code
931 void WINAPI __regs_QT_ThunkPrime( CONTEXT86 *context )
933 DWORD targetTableOffset;
936 /* Compensate for the fact that the Wine register relay code thought
937 we were being called, although we were in fact jumped to */
940 /* Write QT_Thunk call stub */
941 targetTableOffset = context->Edx;
942 relayCode = (LPBYTE)context->Eax;
943 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
945 /* Jump to the call stub just created */
946 context->Eip = (DWORD)relayCode;
948 DEFINE_REGS_ENTRYPOINT( QT_ThunkPrime, 0 )
950 /***********************************************************************
951 * ThunkInitSL (KERNEL32.46)
952 * Another thunkbuf link routine.
953 * The start of the thunkbuf looks like this:
955 * 04: SEGPTR address for thunkbuffer pointer
961 VOID WINAPI ThunkInitSL(
962 LPBYTE thunk, /* [in] start of thunkbuffer */
963 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
964 DWORD len, /* [in] length of thunkbuffer */
965 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
966 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
970 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
973 *(DWORD*)MapSL(addr[1]) = (DWORD)thunk;
976 /**********************************************************************
977 * SSInit (KERNEL.700)
981 BOOL WINAPI SSInit16(void)
986 /**********************************************************************
987 * SSOnBigStack (KERNEL32.87)
988 * Check if thunking is initialized (ss selector set up etc.)
989 * We do that differently, so just return TRUE.
994 BOOL WINAPI SSOnBigStack(void)
996 TRACE("Yes, thunking is initialized\n");
1000 /**********************************************************************
1001 * SSConfirmSmallStack (KERNEL.704)
1003 * Abort if not on small stack.
1005 * This must be a register routine as it has to preserve *all* registers.
1007 void WINAPI SSConfirmSmallStack( CONTEXT86 *context )
1009 /* We are always on the small stack while in 16-bit code ... */
1012 /**********************************************************************
1013 * SSCall (KERNEL32.88)
1014 * One of the real thunking functions. This one seems to be for 32<->32
1015 * thunks. It should probably be capable of crossing processboundaries.
1017 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
1021 * Thunked function result.
1023 DWORD WINAPIV SSCall(
1024 DWORD nr, /* [in] number of argument bytes */
1025 DWORD flags, /* [in] FIXME: flags ? */
1026 FARPROC fun, /* [in] function to call */
1027 ... /* [in/out] arguments */
1030 DWORD *args = ((DWORD *)&fun) + 1;
1034 DPRINTF("(%d,0x%08x,%p,[",nr,flags,fun);
1035 for (i=0;i<nr/4;i++)
1036 DPRINTF("0x%08x,",args[i]);
1040 case 0: ret = fun();
1042 case 4: ret = fun(args[0]);
1044 case 8: ret = fun(args[0],args[1]);
1046 case 12: ret = fun(args[0],args[1],args[2]);
1048 case 16: ret = fun(args[0],args[1],args[2],args[3]);
1050 case 20: ret = fun(args[0],args[1],args[2],args[3],args[4]);
1052 case 24: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
1054 case 28: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
1056 case 32: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
1058 case 36: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
1060 case 40: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
1062 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]);
1064 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]);
1067 WARN("Unsupported nr of arguments, %d\n",nr);
1072 TRACE(" returning %d ...\n",ret);
1076 /**********************************************************************
1077 * W32S_BackTo32 (KERNEL32.51)
1079 void WINAPI __regs_W32S_BackTo32( CONTEXT86 *context )
1081 LPDWORD stack = (LPDWORD)context->Esp;
1082 FARPROC proc = (FARPROC)context->Eip;
1084 context->Eax = proc( stack[1], stack[2], stack[3], stack[4], stack[5],
1085 stack[6], stack[7], stack[8], stack[9], stack[10] );
1087 context->Eip = stack32_pop(context);
1089 DEFINE_REGS_ENTRYPOINT( W32S_BackTo32, 0 )
1091 /**********************************************************************
1092 * AllocSLCallback (KERNEL32.@)
1094 * Allocate a 16->32 callback.
1097 * Win95 uses some structchains for callbacks. It allocates them
1098 * in blocks of 100 entries, size 32 bytes each, layout:
1100 *| 0: PTR nextblockstart
1102 *| 8: WORD sel ( start points to blockstart)
1106 *| 18: PDB *owning_process;
1107 *| 1C: PTR blockstart
1109 * We ignore this for now. (Just a note for further developers)
1110 * FIXME: use this method, so we don't waste selectors...
1112 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
1113 * the 0x66 prefix switches from word->long registers.
1116 *| 6668x arg2 x pushl <arg2>
1118 *| EAx arg1 x jmpf <arg1>
1120 * returns the startaddress of this thunk.
1122 * Note, that they look very similar to the ones allocates by THUNK_Alloc.
1124 * A segmented pointer to the start of the thunk
1128 DWORD finalizer, /* [in] Finalizer function */
1129 DWORD callback /* [in] Callback function */
1131 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
1135 *x++=0x66;*x++=0x5a; /* popl edx */
1136 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
1137 *x++=0x66;*x++=0x52; /* pushl edx */
1138 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
1140 *(DWORD*)(thunk+18) = GetCurrentProcessId();
1142 sel = SELECTOR_AllocBlock( thunk, 32, WINE_LDT_FLAGS_CODE );
1146 /**********************************************************************
1147 * FreeSLCallback (KERNEL32.@)
1148 * Frees the specified 16->32 callback
1155 DWORD x /* [in] 16 bit callback (segmented pointer?) */
1157 FIXME("(0x%08x): stub\n",x);
1160 /**********************************************************************
1161 * AllocMappedBuffer (KERNEL32.38)
1163 * This is an undocumented KERNEL32 function that
1164 * SMapLS's a GlobalAlloc'ed buffer.
1167 * EDI register: pointer to buffer
1170 * The buffer is preceded by 8 bytes:
1173 * edi-4 SEGPTR to buffer
1174 * edi-8 some magic Win95 needs for SUnMapLS
1175 * (we use it for the memory handle)
1177 * The SEGPTR is used by the caller!
1179 void WINAPI __regs_AllocMappedBuffer(
1180 CONTEXT86 *context /* [in] EDI register: size of buffer to allocate */
1182 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
1183 DWORD *buffer = GlobalLock(handle);
1187 if (!(ptr = MapLS(buffer + 2)))
1189 GlobalUnlock(handle);
1194 context->Eax = context->Edi = 0;
1197 buffer[0] = (DWORD)handle;
1201 context->Edi = (DWORD)(buffer + 2);
1204 DEFINE_REGS_ENTRYPOINT( AllocMappedBuffer, 0 )
1206 /**********************************************************************
1207 * FreeMappedBuffer (KERNEL32.39)
1209 * Free a buffer allocated by AllocMappedBuffer
1214 void WINAPI __regs_FreeMappedBuffer(
1215 CONTEXT86 *context /* [in] EDI register: pointer to buffer */
1219 DWORD *buffer = (DWORD *)context->Edi - 2;
1223 GlobalUnlock((HGLOBAL)buffer[0]);
1224 GlobalFree((HGLOBAL)buffer[0]);
1227 DEFINE_REGS_ENTRYPOINT( FreeMappedBuffer, 0 )
1229 /**********************************************************************
1230 * GetTEBSelectorFS (KERNEL.475)
1231 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1233 void WINAPI GetTEBSelectorFS16(void)
1235 CURRENT_STACK16->fs = wine_get_fs();
1238 /**********************************************************************
1239 * IsPeFormat (KERNEL.431)
1241 * Determine if a file is a PE format executable.
1245 * FALSE if the file could not be opened or is not a PE file.
1248 * If fn is given as NULL then the function expects hf16 to be valid.
1250 BOOL16 WINAPI IsPeFormat16(
1251 LPSTR fn, /* [in] Filename to the executable */
1252 HFILE16 hf16) /* [in] An open file handle */
1255 IMAGE_DOS_HEADER mzh;
1259 if (fn) hf16 = OpenFile16(fn,&ofs,OF_READ);
1260 if (hf16 == HFILE_ERROR16) return FALSE;
1261 _llseek16(hf16,0,SEEK_SET);
1262 if (sizeof(mzh)!=_lread16(hf16,&mzh,sizeof(mzh))) goto done;
1263 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) goto done;
1264 _llseek16(hf16,mzh.e_lfanew,SEEK_SET);
1265 if (sizeof(DWORD)!=_lread16(hf16,&xmagic,sizeof(DWORD))) goto done;
1266 ret = (xmagic == IMAGE_NT_SIGNATURE);
1273 /***********************************************************************
1274 * K32Thk1632Prolog (KERNEL32.@)
1276 void WINAPI __regs_K32Thk1632Prolog( CONTEXT86 *context )
1278 LPBYTE code = (LPBYTE)context->Eip - 5;
1280 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1281 of 16->32 thunks instead of using one of the standard methods!
1282 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1283 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1284 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1285 bypassed, which means it will crash the next time the 32-bit OLE
1286 code thunks down again to 16-bit (this *will* happen!).
1288 The following hack tries to recognize this situation.
1289 This is possible since the called stubs in OLECLI32/OLESVR32 all
1290 look exactly the same:
1291 00 E8xxxxxxxx call K32Thk1632Prolog
1292 05 FF55FC call [ebp-04]
1293 08 E8xxxxxxxx call K32Thk1632Epilog
1296 If we recognize this situation, we try to simulate the actions
1297 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1298 to our 32-bit stack, creating a proper STACK16FRAME and
1299 updating cur_stack. */
1301 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1302 && code[13] == 0x66 && code[14] == 0xCB)
1304 DWORD argSize = context->Ebp - context->Esp;
1305 char *stack16 = (char *)context->Esp - 4;
1306 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1307 STACK32FRAME *frame32 = (STACK32FRAME *)NtCurrentTeb()->WOW32Reserved;
1308 char *stack32 = (char *)frame32 - argSize;
1309 WORD stackSel = SELECTOROF(frame32->frame16);
1310 DWORD stackBase = GetSelectorBase(stackSel);
1312 TRACE("before SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1313 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1315 memset(frame16, '\0', sizeof(STACK16FRAME));
1316 frame16->frame32 = frame32;
1317 frame16->ebp = context->Ebp;
1319 memcpy(stack32, stack16, argSize);
1320 NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR(stackSel, (DWORD)frame16 - stackBase);
1322 context->Esp = (DWORD)stack32 + 4;
1323 context->Ebp = context->Esp + argSize;
1325 TRACE("after SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1326 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1329 /* entry_point is never used again once the entry point has
1330 been called. Thus we re-use it to hold the Win16Lock count */
1331 ReleaseThunkLock(&CURRENT_STACK16->entry_point);
1333 DEFINE_REGS_ENTRYPOINT( K32Thk1632Prolog, 0 )
1335 /***********************************************************************
1336 * K32Thk1632Epilog (KERNEL32.@)
1338 void WINAPI __regs_K32Thk1632Epilog( CONTEXT86 *context )
1340 LPBYTE code = (LPBYTE)context->Eip - 13;
1342 RestoreThunkLock(CURRENT_STACK16->entry_point);
1344 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1346 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1347 && code[13] == 0x66 && code[14] == 0xCB)
1349 STACK16FRAME *frame16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1350 char *stack16 = (char *)(frame16 + 1);
1351 DWORD argSize = frame16->ebp - (DWORD)stack16;
1352 char *stack32 = (char *)frame16->frame32 - argSize;
1354 DWORD nArgsPopped = context->Esp - (DWORD)stack32;
1356 TRACE("before SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1357 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1359 NtCurrentTeb()->WOW32Reserved = frame16->frame32;
1361 context->Esp = (DWORD)stack16 + nArgsPopped;
1362 context->Ebp = frame16->ebp;
1364 TRACE("after SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1365 context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1368 DEFINE_REGS_ENTRYPOINT( K32Thk1632Epilog, 0 )
1370 /*********************************************************************
1371 * PK16FNF [KERNEL32.91]
1373 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1374 * string buffer with the 8.3 filename of a recently loaded 16-bit
1375 * module. It is unknown exactly what modules trigger this
1376 * mechanism or what purpose this serves. Win98 Explorer (and
1377 * probably also Win95 with IE 4 shell integration) calls this
1378 * several times during initialization.
1380 * FIXME: find out what this really does and make it work.
1382 void WINAPI PK16FNF(LPSTR strPtr)
1384 FIXME("(%p): stub\n", strPtr);
1386 /* fill in a fake filename that'll be easy to recognize */
1387 strcpy(strPtr, "WINESTUB.FIX");
1390 /***********************************************************************
1391 * 16->32 Flat Thunk routines:
1394 /***********************************************************************
1395 * ThunkConnect16 (KERNEL.651)
1396 * Connects a 32bit and a 16bit thunkbuffer.
1398 UINT WINAPI ThunkConnect16(
1399 LPSTR module16, /* [in] name of win16 dll */
1400 LPSTR module32, /* [in] name of win32 dll */
1401 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
1402 DWORD dwReason, /* [in] initialisation argument */
1403 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
1404 LPSTR thunkfun32, /* [in] win32 thunkfunction */
1405 WORD cs /* [in] CS of win16 dll */
1409 if (!strncmp(TD->magic, "SL01", 4))
1413 TRACE("SL01 thunk %s (%p) -> %s (%s), Reason: %d\n",
1414 module16, TD, module32, thunkfun32, dwReason);
1416 else if (!strncmp(TD->magic, "LS01", 4))
1418 directionSL = FALSE;
1420 TRACE("LS01 thunk %s (%p) <- %s (%s), Reason: %d\n",
1421 module16, TD, module32, thunkfun32, dwReason);
1425 ERR("Invalid magic %c%c%c%c\n",
1426 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
1432 case DLL_PROCESS_ATTACH:
1435 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
1436 struct ThunkDataSL *SL = SL16->fpData;
1440 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
1442 SL->common = SL16->common;
1443 SL->flags1 = SL16->flags1;
1444 SL->flags2 = SL16->flags2;
1446 SL->apiDB = MapSL(SL16->apiDatabase);
1447 SL->targetDB = NULL;
1449 lstrcpynA(SL->pszDll16, module16, 255);
1450 lstrcpynA(SL->pszDll32, module32, 255);
1452 /* We should create a SEGPTR to the ThunkDataSL,
1453 but since the contents are not in the original format,
1454 any access to this by 16-bit code would crash anyway. */
1460 if (SL->flags2 & 0x80000000)
1462 TRACE("Preloading 32-bit library\n");
1463 LoadLibraryA(module32);
1472 case DLL_PROCESS_DETACH:
1473 /* FIXME: cleanup */
1481 /***********************************************************************
1482 * C16ThkSL (KERNEL.630)
1485 void WINAPI C16ThkSL(CONTEXT86 *context)
1487 LPBYTE stub = MapSL(context->Eax), x = stub;
1488 WORD cs = wine_get_cs();
1489 WORD ds = wine_get_ds();
1491 /* We produce the following code:
1496 * mov edx, es:[ecx + $EDX]
1501 * call __FLATCS:__wine_call_from_16_thunk
1504 *x++ = 0xB8; *(WORD *)x = ds; x += sizeof(WORD);
1505 *x++ = 0x8E; *x++ = 0xC0;
1506 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1507 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1508 *x++ = 0x91; *(DWORD *)x = context->Edx; x += sizeof(DWORD);
1511 *x++ = 0x66; *x++ = 0x52;
1513 *x++ = 0x66; *x++ = 0x52;
1514 *x++ = 0x66; *x++ = 0x9A;
1515 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1516 *(WORD *)x = cs; x += sizeof(WORD);
1518 /* Jump to the stub code just created */
1519 context->Eip = LOWORD(context->Eax);
1520 context->SegCs = HIWORD(context->Eax);
1522 /* Since C16ThkSL got called by a jmp, we need to leave the
1523 original return address on the stack */
1527 /***********************************************************************
1528 * C16ThkSL01 (KERNEL.631)
1531 void WINAPI C16ThkSL01(CONTEXT86 *context)
1533 LPBYTE stub = MapSL(context->Eax), x = stub;
1537 struct ThunkDataSL16 *SL16 = MapSL(context->Edx);
1538 struct ThunkDataSL *td = SL16->fpData;
1540 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), (LPCSTR)631);
1541 WORD cs = wine_get_cs();
1545 ERR("ThunkConnect16 was not called!\n");
1549 TRACE("Creating stub for ThunkDataSL %p\n", td);
1552 /* We produce the following code:
1561 * call __FLATCS:__wine_call_from_16_thunk
1564 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1565 *x++ = 0x66; *x++ = 0xBA; *(void **)x = td; x += sizeof(void *);
1566 *x++ = 0x9A; *(DWORD *)x = procAddress; x += sizeof(DWORD);
1569 *x++ = 0x66; *x++ = 0x52;
1571 *x++ = 0x66; *x++ = 0x52;
1572 *x++ = 0x66; *x++ = 0x9A;
1573 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1574 *(WORD *)x = cs; x += sizeof(WORD);
1576 /* Jump to the stub code just created */
1577 context->Eip = LOWORD(context->Eax);
1578 context->SegCs = HIWORD(context->Eax);
1580 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1581 original return address on the stack */
1586 struct ThunkDataSL *td = (struct ThunkDataSL *)context->Edx;
1587 DWORD targetNr = LOWORD(context->Ecx) / 4;
1588 struct SLTargetDB *tdb;
1590 TRACE("Process %08x calling target %d of ThunkDataSL %p\n",
1591 GetCurrentProcessId(), targetNr, td);
1593 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1594 if (tdb->process == GetCurrentProcessId())
1599 TRACE("Loading 32-bit library %s\n", td->pszDll32);
1600 LoadLibraryA(td->pszDll32);
1602 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1603 if (tdb->process == GetCurrentProcessId())
1609 context->Edx = tdb->targetTable[targetNr];
1611 TRACE("Call target is %08x\n", context->Edx);
1615 WORD *stack = MapSL( MAKESEGPTR(context->SegSs, LOWORD(context->Esp)) );
1616 context->Edx = (context->Edx & ~0xffff) | HIWORD(td->apiDB[targetNr].errorReturnValue);
1617 context->Eax = (context->Eax & ~0xffff) | LOWORD(td->apiDB[targetNr].errorReturnValue);
1618 context->Eip = stack[2];
1619 context->SegCs = stack[3];
1620 context->Esp += td->apiDB[targetNr].nrArgBytes + 4;
1622 ERR("Process %08x did not ThunkConnect32 %s to %s\n",
1623 GetCurrentProcessId(), td->pszDll32, td->pszDll16);
1629 /***********************************************************************
1630 * 16<->32 Thunklet/Callback API:
1633 #include "pshpack1.h"
1634 typedef struct _THUNKLET
1649 struct _THUNKLET *next;
1651 #include "poppack.h"
1653 #define THUNKLET_TYPE_LS 1
1654 #define THUNKLET_TYPE_SL 2
1656 static HANDLE ThunkletHeap = 0;
1657 static WORD ThunkletCodeSel;
1658 static THUNKLET *ThunkletAnchor = NULL;
1660 static FARPROC ThunkletSysthunkGlueLS = 0;
1661 static SEGPTR ThunkletSysthunkGlueSL = 0;
1663 static FARPROC ThunkletCallbackGlueLS = 0;
1664 static SEGPTR ThunkletCallbackGlueSL = 0;
1667 /* map a thunk allocated on ThunkletHeap to a 16-bit pointer */
1668 static inline SEGPTR get_segptr( void *thunk )
1670 if (!thunk) return 0;
1671 return MAKESEGPTR( ThunkletCodeSel, (char *)thunk - (char *)ThunkletHeap );
1674 /***********************************************************************
1677 static BOOL THUNK_Init(void)
1681 ThunkletHeap = HeapCreate( 0, 0x10000, 0x10000 );
1682 if (!ThunkletHeap) return FALSE;
1684 ThunkletCodeSel = SELECTOR_AllocBlock( ThunkletHeap, 0x10000, WINE_LDT_FLAGS_CODE );
1686 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1687 if (!thunk) return FALSE;
1689 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1690 *thunk++ = 0x58; /* popl eax */
1691 *thunk++ = 0xC3; /* ret */
1693 ThunkletSysthunkGlueSL = get_segptr( thunk );
1694 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1695 *thunk++ = 0xCB; /* lret */
1700 /***********************************************************************
1701 * SetThunkletCallbackGlue (KERNEL.560)
1703 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1705 ThunkletCallbackGlueLS = glueLS;
1706 ThunkletCallbackGlueSL = glueSL;
1710 /***********************************************************************
1711 * THUNK_FindThunklet
1713 static THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1714 DWORD glue, BYTE type )
1718 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1719 if ( thunk->type == type
1720 && thunk->target == target
1721 && thunk->relay == relay
1722 && ( type == THUNKLET_TYPE_LS ?
1723 ( thunk->glue == glue - (DWORD)&thunk->type )
1724 : ( thunk->glue == glue ) ) )
1730 /***********************************************************************
1731 * THUNK_AllocLSThunklet
1733 static FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1734 FARPROC glue, HTASK16 owner )
1736 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1740 TDB *pTask = GlobalLock16( owner );
1742 if (!ThunkletHeap) THUNK_Init();
1743 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1746 thunk->prefix_target = thunk->prefix_relay = 0x90;
1747 thunk->pushl_target = thunk->pushl_relay = 0x68;
1748 thunk->jmp_glue = 0xE9;
1750 thunk->target = (DWORD)target;
1751 thunk->relay = relay;
1752 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1754 thunk->type = THUNKLET_TYPE_LS;
1755 thunk->owner = pTask? pTask->hInstance : 0;
1757 thunk->next = ThunkletAnchor;
1758 ThunkletAnchor = thunk;
1761 return (FARPROC)thunk;
1764 /***********************************************************************
1765 * THUNK_AllocSLThunklet
1767 static SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1768 SEGPTR glue, HTASK16 owner )
1770 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1774 TDB *pTask = GlobalLock16( owner );
1776 if (!ThunkletHeap) THUNK_Init();
1777 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1780 thunk->prefix_target = thunk->prefix_relay = 0x66;
1781 thunk->pushl_target = thunk->pushl_relay = 0x68;
1782 thunk->jmp_glue = 0xEA;
1784 thunk->target = (DWORD)target;
1785 thunk->relay = relay;
1786 thunk->glue = (DWORD)glue;
1788 thunk->type = THUNKLET_TYPE_SL;
1789 thunk->owner = pTask? pTask->hInstance : 0;
1791 thunk->next = ThunkletAnchor;
1792 ThunkletAnchor = thunk;
1795 return get_segptr( thunk );
1798 /**********************************************************************
1801 static BOOL16 IsLSThunklet( THUNKLET *thunk )
1803 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1804 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1805 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1808 /**********************************************************************
1809 * IsSLThunklet (KERNEL.612)
1811 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1813 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1814 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1815 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1820 /***********************************************************************
1821 * AllocLSThunkletSysthunk (KERNEL.607)
1823 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1824 FARPROC relay, DWORD dummy )
1826 if (!ThunkletSysthunkGlueLS) THUNK_Init();
1827 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1828 ThunkletSysthunkGlueLS, GetCurrentTask() );
1831 /***********************************************************************
1832 * AllocSLThunkletSysthunk (KERNEL.608)
1834 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1835 SEGPTR relay, DWORD dummy )
1837 if (!ThunkletSysthunkGlueSL) THUNK_Init();
1838 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1839 ThunkletSysthunkGlueSL, GetCurrentTask() );
1843 /***********************************************************************
1844 * AllocLSThunkletCallbackEx (KERNEL.567)
1846 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1847 DWORD relay, HTASK16 task )
1849 THUNKLET *thunk = MapSL( target );
1850 if ( !thunk ) return NULL;
1852 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1853 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1854 return (FARPROC)thunk->target;
1856 return THUNK_AllocLSThunklet( target, relay,
1857 ThunkletCallbackGlueLS, task );
1860 /***********************************************************************
1861 * AllocSLThunkletCallbackEx (KERNEL.568)
1863 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1864 DWORD relay, HTASK16 task )
1866 THUNKLET *thunk = (THUNKLET *)target;
1867 if ( !thunk ) return 0;
1869 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1870 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1871 return (SEGPTR)thunk->target;
1873 return THUNK_AllocSLThunklet( target, relay,
1874 ThunkletCallbackGlueSL, task );
1877 /***********************************************************************
1878 * AllocLSThunkletCallback (KERNEL.561)
1879 * AllocLSThunkletCallback_dup (KERNEL.606)
1881 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1883 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1886 /***********************************************************************
1887 * AllocSLThunkletCallback (KERNEL.562)
1888 * AllocSLThunkletCallback_dup (KERNEL.605)
1890 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1892 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1895 /***********************************************************************
1896 * FindLSThunkletCallback (KERNEL.563)
1897 * FindLSThunkletCallback_dup (KERNEL.609)
1899 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1901 THUNKLET *thunk = MapSL( target );
1902 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1903 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1904 return (FARPROC)thunk->target;
1906 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1907 (DWORD)ThunkletCallbackGlueLS,
1909 return (FARPROC)thunk;
1912 /***********************************************************************
1913 * FindSLThunkletCallback (KERNEL.564)
1914 * FindSLThunkletCallback_dup (KERNEL.610)
1916 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1918 THUNKLET *thunk = (THUNKLET *)target;
1919 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1920 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1921 return (SEGPTR)thunk->target;
1923 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1924 (DWORD)ThunkletCallbackGlueSL,
1926 return get_segptr( thunk );
1930 /***********************************************************************
1931 * FreeThunklet (KERNEL.611)
1933 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1939 /***********************************************************************
1940 * Callback Client API
1943 #define N_CBC_FIXED 20
1944 #define N_CBC_VARIABLE 10
1945 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1947 static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
1948 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1950 /***********************************************************************
1951 * RegisterCBClient (KERNEL.619)
1953 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1954 SEGPTR relay16, FARPROC *relay32 )
1956 /* Search for free Callback ID */
1958 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1959 if ( !CBClientRelay16[ wCBCId ] )
1962 /* Register Callback ID */
1963 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1965 CBClientRelay16[ wCBCId ] = relay16;
1966 CBClientRelay32[ wCBCId ] = relay32;
1974 /***********************************************************************
1975 * UnRegisterCBClient (KERNEL.622)
1977 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1978 SEGPTR relay16, FARPROC *relay32 )
1980 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1981 && CBClientRelay16[ wCBCId ] == relay16
1982 && CBClientRelay32[ wCBCId ] == relay32 )
1984 CBClientRelay16[ wCBCId ] = 0;
1985 CBClientRelay32[ wCBCId ] = 0;
1994 /***********************************************************************
1995 * InitCBClient (KERNEL.623)
1997 void WINAPI InitCBClient16( FARPROC glueLS )
1999 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
2000 SEGPTR glueSL = (SEGPTR)GetProcAddress16( kernel, (LPCSTR)604 );
2002 SetThunkletCallbackGlue16( glueLS, glueSL );
2005 /***********************************************************************
2006 * CBClientGlueSL (KERNEL.604)
2008 void WINAPI CBClientGlueSL( CONTEXT86 *context )
2010 /* Create stack frame */
2011 SEGPTR stackSeg = stack16_push( 12 );
2012 LPWORD stackLin = MapSL( stackSeg );
2013 SEGPTR glue, *glueTab;
2015 stackLin[3] = (WORD)context->Ebp;
2016 stackLin[2] = (WORD)context->Esi;
2017 stackLin[1] = (WORD)context->Edi;
2018 stackLin[0] = (WORD)context->SegDs;
2020 context->Ebp = OFFSETOF( stackSeg ) + 6;
2021 context->Esp = OFFSETOF( stackSeg ) - 4;
2024 /* Jump to 16-bit relay code */
2025 glueTab = MapSL( CBClientRelay16[ stackLin[5] ] );
2026 glue = glueTab[ stackLin[4] ];
2027 context->SegCs = SELECTOROF( glue );
2028 context->Eip = OFFSETOF ( glue );
2031 /***********************************************************************
2032 * CBClientThunkSL (KERNEL.620)
2034 extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, WORD *stackLin, DWORD *esi );
2035 void WINAPI CBClientThunkSL( CONTEXT86 *context )
2037 /* Call 32-bit relay code */
2039 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2040 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
2042 /* fill temporary area for the asm code (see comments in winebuild) */
2043 SEGPTR stack = stack16_push( 12 );
2044 LPWORD stackLin = MapSL(stack);
2045 /* stackLin[0] and stackLin[1] reserved for the 32-bit stack ptr */
2046 stackLin[2] = wine_get_ss();
2048 stackLin[4] = OFFSETOF(stack) + 12;
2049 stackLin[5] = SELECTOROF(stack);
2050 stackLin[6] = OFFSETOF(CALL32_CBClientEx_RetAddr); /* overwrite return address */
2051 stackLin[7] = SELECTOROF(CALL32_CBClientEx_RetAddr);
2052 context->Eax = CALL32_CBClient( proc, args, stackLin + 4, &context->Esi );
2056 /***********************************************************************
2057 * CBClientThunkSLEx (KERNEL.621)
2059 extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, WORD *stackLin, DWORD *esi, INT *nArgs );
2060 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
2062 /* Call 32-bit relay code */
2064 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2065 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
2069 /* fill temporary area for the asm code (see comments in winebuild) */
2070 SEGPTR stack = stack16_push( 24 );
2071 stackLin = MapSL(stack);
2072 stackLin[0] = OFFSETOF(stack) + 4;
2073 stackLin[1] = SELECTOROF(stack);
2074 stackLin[2] = wine_get_ds();
2075 stackLin[5] = OFFSETOF(stack) + 24;
2076 /* stackLin[6] and stackLin[7] reserved for the 32-bit stack ptr */
2077 stackLin[8] = wine_get_ss();
2079 stackLin[10] = OFFSETOF(CALL32_CBClientEx_RetAddr);
2080 stackLin[11] = SELECTOROF(CALL32_CBClientEx_RetAddr);
2082 context->Eax = CALL32_CBClientEx( proc, args, stackLin, &context->Esi, &nArgs );
2085 /* Restore registers saved by CBClientGlueSL */
2086 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
2087 context->Ebp = (context->Ebp & ~0xffff) | stackLin[3];
2088 context->Esi = (context->Esi & ~0xffff) | stackLin[2];
2089 context->Edi = (context->Edi & ~0xffff) | stackLin[1];
2090 context->SegDs = stackLin[0];
2091 context->Esp += 16+nArgs;
2093 /* Return to caller of CBClient thunklet */
2094 context->SegCs = stackLin[9];
2095 context->Eip = stackLin[8];
2099 /***********************************************************************
2100 * Get16DLLAddress (KERNEL32.@)
2102 * This function is used by a Win32s DLL if it wants to call a Win16 function.
2103 * A 16:16 segmented pointer to the function is returned.
2104 * Written without any docu.
2106 SEGPTR WINAPI Get16DLLAddress(HMODULE16 handle, LPSTR func_name)
2108 static WORD code_sel32;
2114 if (!ThunkletHeap) THUNK_Init();
2115 code_sel32 = SELECTOR_AllocBlock( ThunkletHeap, 0x10000,
2116 WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
2117 if (!code_sel32) return 0;
2119 if (!(thunk = HeapAlloc( ThunkletHeap, 0, 32 ))) return 0;
2121 if (!handle) handle = GetModuleHandle16("WIN32S16");
2122 proc_16 = GetProcAddress16(handle, func_name);
2124 /* movl proc_16, $edx */
2126 *(FARPROC16 *)thunk = proc_16;
2127 thunk += sizeof(FARPROC16);
2131 *(FARPROC *)thunk = GetProcAddress(kernel32_handle,"QT_Thunk");
2132 thunk += sizeof(FARPROC16);
2133 *(WORD *)thunk = wine_get_cs();
2135 return MAKESEGPTR( code_sel32, (char *)thunk - (char *)ThunkletHeap );
2139 /***********************************************************************
2140 * GetWin16DOSEnv (KERNEL32.34)
2141 * Returns some internal value.... probably the default environment database?
2143 DWORD WINAPI GetWin16DOSEnv(void)
2145 FIXME("stub, returning 0\n");
2149 /**********************************************************************
2150 * GetPK16SysVar (KERNEL32.92)
2152 LPVOID WINAPI GetPK16SysVar(void)
2154 static BYTE PK16SysVar[128];
2160 /**********************************************************************
2161 * CommonUnimpStub (KERNEL32.17)
2163 void WINAPI __regs_CommonUnimpStub( CONTEXT86 *context )
2165 FIXME("generic stub: %s\n", ((LPSTR)context->Eax ? (LPSTR)context->Eax : "?"));
2167 switch ((context->Ecx >> 4) & 0x0f)
2169 case 15: context->Eax = -1; break;
2170 case 14: context->Eax = 0x78; break;
2171 case 13: context->Eax = 0x32; break;
2172 case 1: context->Eax = 1; break;
2173 default: context->Eax = 0; break;
2176 context->Esp += (context->Ecx & 0x0f) * 4;
2178 DEFINE_REGS_ENTRYPOINT( CommonUnimpStub, 0 )
2180 /**********************************************************************
2181 * HouseCleanLogicallyDeadHandles (KERNEL32.33)
2183 void WINAPI HouseCleanLogicallyDeadHandles(void)
2185 /* Whatever this is supposed to do, our handles probably
2186 don't need it :-) */
2189 /**********************************************************************
2192 BOOL WINAPI _KERNEL32_100(HANDLE threadid,DWORD exitcode,DWORD x)
2194 FIXME("(%p,%d,0x%08x): stub\n",threadid,exitcode,x);
2198 /**********************************************************************
2201 * Checks whether the clock has to be switched from daylight
2202 * savings time to standard time or vice versa.
2205 DWORD WINAPI _KERNEL32_99(DWORD x)
2207 FIXME("(0x%08x): stub\n",x);
2212 /**********************************************************************
2215 * Real prototype is:
2216 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
2218 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT86 *context )
2220 /* Note: we don't save the current ss, as the catch buffer is */
2221 /* only 9 words long. Hopefully no one will have the silly */
2222 /* idea to change the current stack before calling Throw()... */
2236 lpbuf[0] = LOWORD(context->Eip);
2237 lpbuf[1] = context->SegCs;
2238 /* Windows pushes 4 more words before saving sp */
2239 lpbuf[2] = LOWORD(context->Esp) - 4 * sizeof(WORD);
2240 lpbuf[3] = LOWORD(context->Ebp);
2241 lpbuf[4] = LOWORD(context->Esi);
2242 lpbuf[5] = LOWORD(context->Edi);
2243 lpbuf[6] = context->SegDs;
2245 lpbuf[8] = context->SegSs;
2246 context->Eax &= ~0xffff; /* Return 0 */
2250 /**********************************************************************
2253 * Real prototype is:
2254 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
2256 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT86 *context )
2258 STACK16FRAME *pFrame;
2259 STACK32FRAME *frame32;
2261 context->Eax = (context->Eax & ~0xffff) | (WORD)retval;
2263 /* Find the frame32 corresponding to the frame16 we are jumping to */
2264 pFrame = CURRENT_STACK16;
2265 frame32 = pFrame->frame32;
2266 while (frame32 && frame32->frame16)
2268 if (OFFSETOF(frame32->frame16) < OFFSETOF(NtCurrentTeb()->WOW32Reserved))
2269 break; /* Something strange is going on */
2270 if (OFFSETOF(frame32->frame16) > lpbuf[2])
2272 /* We found the right frame */
2273 pFrame->frame32 = frame32;
2276 frame32 = ((STACK16FRAME *)MapSL(frame32->frame16))->frame32;
2278 RtlUnwind( &pFrame->frame32->frame, NULL, NULL, 0 );
2280 context->Eip = lpbuf[0];
2281 context->SegCs = lpbuf[1];
2282 context->Esp = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
2283 context->Ebp = lpbuf[3];
2284 context->Esi = lpbuf[4];
2285 context->Edi = lpbuf[5];
2286 context->SegDs = lpbuf[6];
2288 if (lpbuf[8] != context->SegSs)
2289 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );
2294 * 16-bit WOW routines (in KERNEL)
2297 /**********************************************************************
2298 * GetVDMPointer32W (KERNEL.516)
2300 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
2302 GlobalPageLock16(GlobalHandle16(SELECTOROF(vp)));
2303 return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
2306 /***********************************************************************
2307 * LoadLibraryEx32W (KERNEL.513)
2309 DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
2318 SetLastError(ERROR_INVALID_PARAMETER);
2322 /* if the file cannot be found, call LoadLibraryExA anyway, since it might be
2323 a builtin module. This case is handled in MODULE_LoadLibraryExA */
2325 if ((p = strrchr( lpszLibFile, '.' )) && !strchr( p, '\\' )) /* got an extension */
2327 if (OpenFile16( lpszLibFile, &ofs, OF_EXIST ) != HFILE_ERROR16)
2328 lpszLibFile = ofs.szPathName;
2332 char buffer[MAX_PATH+4];
2333 strcpy( buffer, lpszLibFile );
2334 strcat( buffer, ".dll" );
2335 if (OpenFile16( buffer, &ofs, OF_EXIST ) != HFILE_ERROR16)
2336 lpszLibFile = ofs.szPathName;
2339 ReleaseThunkLock( &mutex_count );
2340 hModule = LoadLibraryExA( lpszLibFile, (HANDLE)hFile, dwFlags );
2341 RestoreThunkLock( mutex_count );
2343 return (DWORD)hModule;
2346 /***********************************************************************
2347 * GetProcAddress32W (KERNEL.515)
2349 DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
2351 return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
2354 /***********************************************************************
2355 * FreeLibrary32W (KERNEL.514)
2357 DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
2362 ReleaseThunkLock( &mutex_count );
2363 retv = FreeLibrary( (HMODULE)hLibModule );
2364 RestoreThunkLock( mutex_count );
2369 #define CPEX_DEST_STDCALL 0x00000000
2370 #define CPEX_DEST_CDECL 0x80000000
2372 /**********************************************************************
2375 static DWORD WOW_CallProc32W16( FARPROC proc32, DWORD nrofargs, DWORD *args )
2380 ReleaseThunkLock( &mutex_count );
2383 * FIXME: If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
2384 * 32-bit CDECL routine ...
2387 if (!proc32) ret = 0;
2388 else switch (nrofargs)
2390 case 0: ret = proc32();
2392 case 1: ret = proc32(args[0]);
2394 case 2: ret = proc32(args[0],args[1]);
2396 case 3: ret = proc32(args[0],args[1],args[2]);
2398 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
2400 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
2402 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
2404 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
2406 case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
2408 case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
2410 case 10:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
2412 case 11:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]);
2414 case 12:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11]);
2416 case 13:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12]);
2418 case 14:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12],args[13]);
2420 case 15:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12],args[13],args[14]);
2423 /* FIXME: should go up to 32 arguments */
2424 ERR("Unsupported number of arguments %d, please report.\n",nrofargs);
2429 RestoreThunkLock( mutex_count );
2431 TRACE("returns %08x\n",ret);
2435 /**********************************************************************
2436 * CallProc32W (KERNEL.517)
2438 DWORD WINAPIV CallProc32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
2443 TRACE("(%d,%d,%p args[",nrofargs,argconvmask,proc32);
2445 for (i=0;i<nrofargs;i++)
2447 if (argconvmask & (1<<i))
2449 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
2450 /* pascal convention, have to reverse the arguments order */
2451 args[nrofargs - i - 1] = (DWORD)MapSL(ptr);
2452 TRACE("%08x(%p),",ptr,MapSL(ptr));
2456 DWORD arg = VA_ARG16( valist, DWORD );
2457 /* pascal convention, have to reverse the arguments order */
2458 args[nrofargs - i - 1] = arg;
2464 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
2465 stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
2467 return WOW_CallProc32W16( proc32, nrofargs, args );
2470 /**********************************************************************
2471 * _CallProcEx32W (KERNEL.518)
2473 DWORD WINAPIV CallProcEx32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
2478 TRACE("(%d,%d,%p args[",nrofargs,argconvmask,proc32);
2480 for (i=0;i<nrofargs;i++)
2482 if (argconvmask & (1<<i))
2484 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
2485 args[i] = (DWORD)MapSL(ptr);
2486 TRACE("%08x(%p),",ptr,MapSL(ptr));
2490 DWORD arg = VA_ARG16( valist, DWORD );
2496 return WOW_CallProc32W16( proc32, nrofargs, args );
2500 /**********************************************************************
2501 * WOW16Call (KERNEL.500)
2506 DWORD WINAPIV WOW16Call(WORD x, WORD y, WORD z, VA_LIST16 args)
2510 FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
2512 for (i=0;i<x/2;i++) {
2513 WORD a = VA_ARG16(args,WORD);
2516 calladdr = VA_ARG16(args,DWORD);
2517 stack16_pop( 3*sizeof(WORD) + x + sizeof(DWORD) );
2518 DPRINTF(") calling address was 0x%08x\n",calladdr);
2522 #endif /* __i386__ */