include: Add missing enum XHR_PROP_ values.
[wine.git] / dlls / krnl386.exe16 / thunk.c
blob6b289eac65e049f0683ab81c5d7d88e9abb976e7
1 /*
2 * KERNEL32 thunks and other undocumented stuff
4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1997, 1998 Marcus Meissner
6 * Copyright 1998 Ulrich Weigand
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <string.h>
24 #include <sys/types.h>
25 #include <stdarg.h>
26 #include <stdio.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "winternl.h"
32 #include "wownt16.h"
33 #include "wownt32.h"
34 #include "wine/winbase16.h"
36 #include "wine/debug.h"
37 #include "kernel16_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
41 struct ThunkDataCommon
43 char magic[4]; /* 00 */
44 DWORD checksum; /* 04 */
47 struct ThunkDataLS16
49 struct ThunkDataCommon common; /* 00 */
50 SEGPTR targetTable; /* 08 */
51 DWORD firstTime; /* 0C */
54 struct ThunkDataLS32
56 struct ThunkDataCommon common; /* 00 */
57 DWORD * targetTable; /* 08 */
58 char lateBinding[4]; /* 0C */
59 DWORD flags; /* 10 */
60 DWORD reserved1; /* 14 */
61 DWORD reserved2; /* 18 */
62 DWORD offsetQTThunk; /* 1C */
63 DWORD offsetFTProlog; /* 20 */
66 struct ThunkDataSL16
68 struct ThunkDataCommon common; /* 00 */
69 DWORD flags1; /* 08 */
70 DWORD reserved1; /* 0C */
71 struct ThunkDataSL * fpData; /* 10 */
72 SEGPTR spData; /* 14 */
73 DWORD reserved2; /* 18 */
74 char lateBinding[4]; /* 1C */
75 DWORD flags2; /* 20 */
76 DWORD reserved3; /* 20 */
77 SEGPTR apiDatabase; /* 28 */
80 struct ThunkDataSL32
82 struct ThunkDataCommon common; /* 00 */
83 DWORD reserved1; /* 08 */
84 struct ThunkDataSL * data; /* 0C */
85 char lateBinding[4]; /* 10 */
86 DWORD flags; /* 14 */
87 DWORD reserved2; /* 18 */
88 DWORD reserved3; /* 1C */
89 DWORD offsetTargetTable; /* 20 */
92 struct ThunkDataSL
94 #if 0
95 This structure differs from the Win95 original,
96 but this should not matter since it is strictly internal to
97 the thunk handling routines in KRNL386 / KERNEL32.
99 For reference, here is the Win95 layout:
101 struct ThunkDataCommon common; /* 00 */
102 DWORD flags1; /* 08 */
103 SEGPTR apiDatabase; /* 0C */
104 WORD exePtr; /* 10 */
105 WORD segMBA; /* 12 */
106 DWORD lenMBATotal; /* 14 */
107 DWORD lenMBAUsed; /* 18 */
108 DWORD flags2; /* 1C */
109 char pszDll16[256]; /* 20 */
110 char pszDll32[256]; /*120 */
112 We do it differently since all our thunk handling is done
113 by 32-bit code. Therefore we do not need to provide
114 easy access to this data, especially the process target
115 table database, for 16-bit code.
116 #endif
118 struct ThunkDataCommon common;
119 DWORD flags1;
120 struct SLApiDB * apiDB;
121 struct SLTargetDB * targetDB;
122 DWORD flags2;
123 char pszDll16[256];
124 char pszDll32[256];
127 struct SLTargetDB
129 struct SLTargetDB * next;
130 DWORD process;
131 DWORD * targetTable;
134 struct SLApiDB
136 DWORD nrArgBytes;
137 DWORD errorReturnValue;
140 WORD cbclient_selector = 0;
141 WORD cbclientex_selector = 0;
143 extern int call_entry_point( void *func, int nb_args, const DWORD *args );
144 extern void __wine_call_from_16_thunk(void);
145 extern void WINAPI FT_Prolog(void);
146 extern void WINAPI FT_PrologPrime(void);
147 extern void WINAPI QT_Thunk(void);
148 extern void WINAPI QT_ThunkPrime(void);
150 /***********************************************************************
152 * Win95 internal thunks *
154 ***********************************************************************/
156 /***********************************************************************
157 * LogApiThk (KERNEL.423)
159 void WINAPI LogApiThk( LPSTR func )
161 TRACE( "%s\n", debugstr_a(func) );
164 /***********************************************************************
165 * LogApiThkLSF (KERNEL32.42)
167 * NOTE: needs to preserve all registers!
169 __ASM_STDCALL_FUNC( LogApiThkLSF, 4, "ret $4" )
171 /***********************************************************************
172 * LogApiThkSL (KERNEL32.44)
174 * NOTE: needs to preserve all registers!
176 __ASM_STDCALL_FUNC( LogApiThkSL, 4, "ret $4" )
178 /***********************************************************************
179 * LogCBThkSL (KERNEL32.47)
181 * NOTE: needs to preserve all registers!
183 __ASM_STDCALL_FUNC( LogCBThkSL, 4, "ret $4" )
185 /***********************************************************************
186 * Generates a FT_Prolog call.
188 * 0FB6D1 movzbl edx,cl
189 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
190 * 68xxxxxxxx push FT_Prolog
191 * C3 lret
193 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
194 LPBYTE x;
196 x = relayCode;
197 *x++ = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
198 *x++ = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
199 x+=4; /* mov edx, [4*edx + targetTable] */
200 *x++ = 0x68; *(void **)x = FT_Prolog;
201 x+=4; /* push FT_Prolog */
202 *x++ = 0xC3; /* lret */
203 /* fill rest with 0xCC / int 3 */
206 /***********************************************************************
207 * _write_qtthunk (internal)
208 * Generates a QT_Thunk style call.
210 * 33C9 xor ecx, ecx
211 * 8A4DFC mov cl , [ebp-04]
212 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
213 * B8yyyyyyyy mov eax, QT_Thunk
214 * FFE0 jmp eax
216 static void _write_qtthunk(
217 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
218 DWORD *targetTable /* [in] start of thunk (for index lookup) */
220 LPBYTE x;
222 x = relayCode;
223 *x++ = 0x33;*x++=0xC9; /* xor ecx,ecx */
224 *x++ = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
225 *x++ = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
226 x+=4; /* mov edx, [4*ecx + targetTable */
227 *x++ = 0xB8; *(void **)x = QT_Thunk;
228 x+=4; /* mov eax , QT_Thunk */
229 *x++ = 0xFF; *x++ = 0xE0; /* jmp eax */
230 /* should fill the rest of the 32 bytes with 0xCC */
233 /***********************************************************************
234 * _loadthunk
236 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
237 struct ThunkDataCommon *TD32, DWORD checksum)
239 struct ThunkDataCommon *TD16;
240 HMODULE16 hmod;
241 int ordinal;
242 static BOOL done;
244 if (!done)
246 LoadLibrary16( "gdi.exe" );
247 LoadLibrary16( "user.exe" );
248 done = TRUE;
251 if ((hmod = LoadLibrary16(module)) <= 32)
253 ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
254 module, func, module32, module, hmod);
255 return 0;
258 if ( !(ordinal = NE_GetOrdinal(hmod, func))
259 || !(TD16 = MapSL((SEGPTR)NE_GetEntryPointEx(hmod, ordinal, FALSE))))
261 ERR("Unable to find thunk data '%s' in %s, required by %s (conflicting/incorrect DLL versions !?).\n",
262 func, module, module32);
263 return 0;
266 if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
268 ERR("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
269 module, func, module32,
270 TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
271 TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
272 return 0;
275 if (TD32 && TD16->checksum != TD32->checksum)
277 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
278 module, func, module32, TD16->checksum, TD32->checksum);
279 return 0;
282 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
284 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
285 module, func, module32, *(LPDWORD)TD16, checksum);
286 return 0;
289 return TD16;
292 /***********************************************************************
293 * GetThunkStuff (KERNEL32.53)
295 LPVOID WINAPI GetThunkStuff(LPCSTR module, LPCSTR func)
297 return _loadthunk(module, func, "<kernel>", NULL, 0L);
300 /***********************************************************************
301 * GetThunkBuff (KERNEL32.52)
302 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
304 LPVOID WINAPI GetThunkBuff(void)
306 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
309 /***********************************************************************
310 * ThunkConnect32 (KERNEL32.@)
311 * Connects a 32bit and a 16bit thunkbuffer.
313 UINT WINAPI ThunkConnect32(
314 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
315 LPSTR thunkfun16, /* [in] win16 thunkfunction */
316 LPSTR module16, /* [in] name of win16 dll */
317 LPSTR module32, /* [in] name of win32 dll */
318 HMODULE hmod32, /* [in] hmodule of win32 dll */
319 DWORD dwReason /* [in] initialisation argument */
321 BOOL directionSL;
323 if (!strncmp(TD->magic, "SL01", 4))
325 directionSL = TRUE;
327 TRACE("SL01 thunk %s (%p) <- %s (%s), Reason: %ld\n",
328 module32, TD, module16, thunkfun16, dwReason);
330 else if (!strncmp(TD->magic, "LS01", 4))
332 directionSL = FALSE;
334 TRACE("LS01 thunk %s (%p) -> %s (%s), Reason: %ld\n",
335 module32, TD, module16, thunkfun16, dwReason);
337 else
339 ERR("Invalid magic %c%c%c%c\n",
340 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
341 return 0;
344 switch (dwReason)
346 case DLL_PROCESS_ATTACH:
348 struct ThunkDataCommon *TD16;
349 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
350 return 0;
352 if (directionSL)
354 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
355 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
356 struct SLTargetDB *tdb;
358 if (SL16->fpData == NULL)
360 ERR("ThunkConnect16 was not called!\n");
361 return 0;
364 SL32->data = SL16->fpData;
366 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
367 tdb->process = GetCurrentProcessId();
368 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
370 tdb->next = SL32->data->targetDB; /* FIXME: not thread-safe! */
371 SL32->data->targetDB = tdb;
373 TRACE("Process %08lx allocated TargetDB entry for ThunkDataSL %p\n",
374 GetCurrentProcessId(), SL32->data);
376 else
378 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
379 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
381 LS32->targetTable = MapSL(LS16->targetTable);
383 /* write QT_Thunk and FT_Prolog stubs */
384 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk, LS32->targetTable);
385 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
387 break;
390 case DLL_PROCESS_DETACH:
391 /* FIXME: cleanup */
392 break;
395 return 1;
398 /**********************************************************************
399 * QT_Thunk (KERNEL32.@)
401 * The target address is in EDX.
402 * The 16bit arguments start at ESP.
403 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
404 * So the stack layout is 16bit argument bytes and then the 64 byte
405 * scratch buffer.
406 * The scratch buffer is used as work space by Windows' QT_Thunk
407 * function.
408 * As the programs unfortunately don't always provide a fixed size
409 * scratch buffer (danger, stack corruption ahead !!), we simply resort
410 * to copying over the whole EBP-ESP range to the 16bit stack
411 * (as there's no way to safely figure out the param count
412 * due to this misbehaviour of some programs).
413 * [ok]
415 * See DDJ article 9614c for a very good description of QT_Thunk (also
416 * available online !).
418 * FIXME: DDJ talks of certain register usage rules; I'm not sure
419 * whether we cover this 100%.
421 void WINAPI __regs_QT_Thunk( CONTEXT *context )
423 CONTEXT context16;
424 DWORD argsize;
426 context16 = *context;
428 context16.SegCs = HIWORD(context->Edx);
429 context16.Eip = LOWORD(context->Edx);
430 /* point EBP to the STACK16FRAME on the stack
431 * for the call_to_16 to set up the register content on calling */
432 context16.Ebp = CURRENT_SP + FIELD_OFFSET(STACK16FRAME,bp);
435 * used to be (problematic):
436 * argsize = context->Ebp - context->Esp - 0x40;
437 * due to some programs abusing the API, we better assume the full
438 * EBP - ESP range for copying instead: */
439 argsize = context->Ebp - context->Esp;
441 /* ok, too much is insane; let's limit param count a bit again */
442 if (argsize > 64)
443 argsize = 64; /* 32 WORDs */
445 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
446 context->Eax = context16.Eax;
447 context->Edx = context16.Edx;
448 context->Ecx = context16.Ecx;
450 /* make sure to update the Win32 ESP, too, in order to throw away
451 * the number of parameters that the Win16 function
452 * accepted (that it popped from the corresponding Win16 stack) */
453 context->Esp += LOWORD(context16.Esp) - (CURRENT_SP - argsize);
455 DEFINE_REGS_ENTRYPOINT( QT_Thunk )
458 /**********************************************************************
459 * FT_Prolog (KERNEL32.@)
461 * The set of FT_... thunk routines is used instead of QT_Thunk,
462 * if structures have to be converted from 32-bit to 16-bit
463 * (change of member alignment, conversion of members).
465 * The thunk function (as created by the thunk compiler) calls
466 * FT_Prolog at the beginning, to set up a stack frame and
467 * allocate a 64 byte buffer on the stack.
468 * The input parameters (target address and some flags) are
469 * saved for later use by FT_Thunk.
471 * Input: EDX 16-bit target address (SEGPTR)
472 * CX bits 0..7 target number (in target table)
473 * bits 8..9 some flags (unclear???)
474 * bits 10..15 number of DWORD arguments
476 * Output: A new stackframe is created, and a 64 byte buffer
477 * allocated on the stack. The layout of the stack
478 * on return is as follows:
480 * (ebp+4) return address to caller of thunk function
481 * (ebp) old EBP
482 * (ebp-4) saved EBX register of caller
483 * (ebp-8) saved ESI register of caller
484 * (ebp-12) saved EDI register of caller
485 * (ebp-16) saved ECX register, containing flags
486 * (ebp-20) bitmap containing parameters that are to be converted
487 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
488 * filled in by the thunk code before calling FT_Thunk
489 * (ebp-24)
490 * ... (unclear)
491 * (ebp-44)
492 * (ebp-48) saved EAX register of caller (unclear, never restored???)
493 * (ebp-52) saved EDX register, containing 16-bit thunk target
494 * (ebp-56)
495 * ... (unclear)
496 * (ebp-64)
498 * ESP is EBP-64 after return.
501 void WINAPI __regs_FT_Prolog( CONTEXT *context )
503 /* Build stack frame */
504 stack32_push(context, context->Ebp);
505 context->Ebp = context->Esp;
507 /* Allocate 64-byte Thunk Buffer */
508 context->Esp -= 64;
509 memset((char *)context->Esp, '\0', 64);
511 /* Store Flags (ECX) and Target Address (EDX) */
512 /* Save other registers to be restored later */
513 *(DWORD *)(context->Ebp - 4) = context->Ebx;
514 *(DWORD *)(context->Ebp - 8) = context->Esi;
515 *(DWORD *)(context->Ebp - 12) = context->Edi;
516 *(DWORD *)(context->Ebp - 16) = context->Ecx;
518 *(DWORD *)(context->Ebp - 48) = context->Eax;
519 *(DWORD *)(context->Ebp - 52) = context->Edx;
521 DEFINE_REGS_ENTRYPOINT( FT_Prolog )
523 /**********************************************************************
524 * FT_Thunk (KERNEL32.@)
526 * This routine performs the actual call to 16-bit code,
527 * similar to QT_Thunk. The differences are:
528 * - The call target is taken from the buffer created by FT_Prolog
529 * - Those arguments requested by the thunk code (by setting the
530 * corresponding bit in the bitmap at EBP-20) are converted
531 * from 32-bit pointers to segmented pointers (those pointers
532 * are guaranteed to point to structures copied to the stack
533 * by the thunk code, so we always use the 16-bit stack selector
534 * for those addresses).
536 * The bit #i of EBP-20 corresponds here to the DWORD starting at
537 * ESP+4 + 2*i.
539 * FIXME: It is unclear what happens if there are more than 32 WORDs
540 * of arguments, so that the single DWORD bitmap is no longer
541 * sufficient ...
543 void WINAPI __regs_FT_Thunk( CONTEXT *context )
545 DWORD mapESPrelative = *(DWORD *)(context->Ebp - 20);
546 DWORD callTarget = *(DWORD *)(context->Ebp - 52);
548 CONTEXT context16;
549 DWORD i, argsize;
550 DWORD newstack[32];
551 LPBYTE oldstack;
553 context16 = *context;
555 context16.SegCs = HIWORD(callTarget);
556 context16.Eip = LOWORD(callTarget);
557 context16.Ebp = CURRENT_SP + FIELD_OFFSET(STACK16FRAME,bp);
559 argsize = context->Ebp-context->Esp-0x40;
560 if (argsize > sizeof(newstack)) argsize = sizeof(newstack);
561 oldstack = (LPBYTE)context->Esp;
563 memcpy( newstack, oldstack, argsize );
565 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
566 if (mapESPrelative & (1 << i))
568 SEGPTR *arg = (SEGPTR *)newstack[i];
569 *arg = MAKESEGPTR( CURRENT_SS, CURRENT_SP - argsize + (*(LPBYTE *)arg - oldstack));
572 WOWCallback16Ex( 0, WCB16_REGS, argsize, newstack, (DWORD *)&context16 );
573 context->Eax = context16.Eax;
574 context->Edx = context16.Edx;
575 context->Ecx = context16.Ecx;
577 context->Esp += LOWORD(context16.Esp) - (CURRENT_SP - argsize);
579 /* Copy modified buffers back to 32-bit stack */
580 memcpy( oldstack, newstack, argsize );
582 DEFINE_REGS_ENTRYPOINT( FT_Thunk )
584 /***********************************************************************
585 * FT_Exit0 (KERNEL32.@)
586 * FT_Exit4 (KERNEL32.@)
587 * FT_Exit8 (KERNEL32.@)
588 * FT_Exit12 (KERNEL32.@)
589 * FT_Exit16 (KERNEL32.@)
590 * FT_Exit20 (KERNEL32.@)
591 * FT_Exit24 (KERNEL32.@)
592 * FT_Exit28 (KERNEL32.@)
593 * FT_Exit32 (KERNEL32.@)
594 * FT_Exit36 (KERNEL32.@)
595 * FT_Exit40 (KERNEL32.@)
596 * FT_Exit44 (KERNEL32.@)
597 * FT_Exit48 (KERNEL32.@)
598 * FT_Exit52 (KERNEL32.@)
599 * FT_Exit56 (KERNEL32.@)
601 * One of the FT_ExitNN functions is called at the end of the thunk code.
602 * It removes the stack frame created by FT_Prolog, moves the function
603 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
604 * value, but the thunk code has moved it from EAX to EBX in the
605 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
606 * and perform a return to the CALLER of the thunk code (while removing
607 * the given number of arguments from the caller's stack).
609 #define FT_EXIT_RESTORE_REGS \
610 "movl %ebx,%eax\n\t" \
611 "movl -4(%ebp),%ebx\n\t" \
612 "movl -8(%ebp),%esi\n\t" \
613 "movl -12(%ebp),%edi\n\t" \
614 "leave\n\t"
616 #define DEFINE_FT_Exit(n) \
617 __ASM_STDCALL_FUNC( FT_Exit ## n, 0, FT_EXIT_RESTORE_REGS "ret $" #n )
619 DEFINE_FT_Exit(0)
620 DEFINE_FT_Exit(4)
621 DEFINE_FT_Exit(8)
622 DEFINE_FT_Exit(12)
623 DEFINE_FT_Exit(16)
624 DEFINE_FT_Exit(20)
625 DEFINE_FT_Exit(24)
626 DEFINE_FT_Exit(28)
627 DEFINE_FT_Exit(32)
628 DEFINE_FT_Exit(36)
629 DEFINE_FT_Exit(40)
630 DEFINE_FT_Exit(44)
631 DEFINE_FT_Exit(48)
632 DEFINE_FT_Exit(52)
633 DEFINE_FT_Exit(56)
636 /***********************************************************************
637 * ThunkInitLS (KERNEL32.43)
638 * A thunkbuffer link routine
639 * The thunkbuf looks like:
641 * 00: DWORD length ? don't know exactly
642 * 04: SEGPTR ptr ? where does it point to?
643 * The pointer ptr is written into the first DWORD of 'thunk'.
644 * (probably correctly implemented)
645 * [ok probably]
646 * RETURNS
647 * segmented pointer to thunk?
649 DWORD WINAPI ThunkInitLS(
650 LPDWORD thunk, /* [in] win32 thunk */
651 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
652 DWORD len, /* [in] thkbuffer length */
653 LPCSTR dll16, /* [in] name of win16 dll */
654 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
656 LPDWORD addr;
658 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
659 return 0;
661 if (!addr[1])
662 return 0;
663 *thunk = addr[1];
665 return addr[1];
668 /***********************************************************************
669 * Common32ThkLS (KERNEL32.45)
671 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
672 * style thunks. The basic difference is that the parameter conversion
673 * is done completely on the *16-bit* side here. Thus we do not call
674 * the 16-bit target directly, but call a common entry point instead.
675 * This entry function then calls the target according to the target
676 * number passed in the DI register.
678 * Input: EAX SEGPTR to the common 16-bit entry point
679 * CX offset in thunk table (target number * 4)
680 * DX error return value if execution fails (unclear???)
681 * EDX.HI number of DWORD parameters
683 * (Note that we need to move the thunk table offset from CX to DI !)
685 * The called 16-bit stub expects its stack to look like this:
686 * ...
687 * (esp+40) 32-bit arguments
688 * ...
689 * (esp+8) 32 byte of stack space available as buffer
690 * (esp) 8 byte return address for use with 0x66 lret
692 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
693 * and uses the EAX register to return a DWORD return value.
694 * Thus we need to use a special assembly glue routine
695 * (CallRegisterLongProc instead of CallRegisterShortProc).
697 * Finally, we return to the caller, popping the arguments off
698 * the stack. The number of arguments to be popped is returned
699 * in the BL register by the called 16-bit routine.
702 void WINAPI __regs_Common32ThkLS( CONTEXT *context )
704 CONTEXT context16;
705 DWORD argsize;
707 context16 = *context;
709 context16.Edi = LOWORD(context->Ecx);
710 context16.SegCs = HIWORD(context->Eax);
711 context16.Eip = LOWORD(context->Eax);
712 context16.Ebp = CURRENT_SP + FIELD_OFFSET(STACK16FRAME,bp);
714 argsize = HIWORD(context->Edx) * 4;
716 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
717 if (context->Edx == context->Eip)
718 argsize = 6 * 4;
720 /* Note: the first 32 bytes we copy are just garbage from the 32-bit stack, in order to reserve
721 * the space. It is safe to do that since the register function prefix has reserved
722 * a lot more space than that below context->Esp.
724 WOWCallback16Ex( 0, WCB16_REGS, argsize + 32, (LPBYTE)context->Esp - 32, (DWORD *)&context16 );
725 context->Eax = context16.Eax;
727 /* Clean up caller's stack frame */
728 context->Esp += LOBYTE(context16.Ebx);
730 DEFINE_REGS_ENTRYPOINT( Common32ThkLS )
732 /***********************************************************************
733 * OT_32ThkLSF (KERNEL32.40)
735 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
736 * argument processing is done on both the 32-bit and the 16-bit side:
737 * The 32-bit side prepares arguments, copying them onto the stack.
739 * When this routine is called, the first word on the stack is the
740 * number of argument bytes prepared by the 32-bit code, and EDX
741 * contains the 16-bit target address.
743 * The called 16-bit routine is another relaycode, doing further
744 * argument processing and then calling the real 16-bit target
745 * whose address is stored at [bp-04].
747 * The call proceeds using a normal CallRegisterShortProc.
748 * After return from the 16-bit relaycode, the arguments need
749 * to be copied *back* to the 32-bit stack, since the 32-bit
750 * relaycode processes output parameters.
752 * Note that we copy twice the number of arguments, since some of the
753 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
754 * arguments of the caller!
756 * (Note that this function seems only to be used for
757 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
759 void WINAPI __regs_OT_32ThkLSF( CONTEXT *context )
761 CONTEXT context16;
762 DWORD argsize;
764 context16 = *context;
766 context16.SegCs = HIWORD(context->Edx);
767 context16.Eip = LOWORD(context->Edx);
768 context16.Ebp = CURRENT_SP + FIELD_OFFSET(STACK16FRAME,bp);
770 argsize = 2 * *(WORD *)context->Esp + 2;
772 WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
773 context->Eax = context16.Eax;
774 context->Edx = context16.Edx;
776 /* Copy modified buffers back to 32-bit stack */
777 memcpy( (LPBYTE)context->Esp,
778 (LPBYTE)CURRENT_STACK16 - argsize, argsize );
780 context->Esp += LOWORD(context16.Esp) - (CURRENT_SP - argsize);
782 DEFINE_REGS_ENTRYPOINT( OT_32ThkLSF )
784 /***********************************************************************
785 * ThunkInitLSF (KERNEL32.41)
786 * A thunk setup routine.
787 * Expects a pointer to a preinitialized thunkbuffer in the first argument
788 * looking like:
789 *| 00..03: unknown (pointer, check _41, _43, _46)
790 *| 04: EB1E jmp +0x20
792 *| 06..23: unknown (space for replacement code, check .90)
794 *| 24:>E800000000 call offset 29
795 *| 29:>58 pop eax ( target of call )
796 *| 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
797 *| 2F: BAxxxxxxxx mov edx,xxxxxxxx
798 *| 34: 68yyyyyyyy push KERNEL32.90
799 *| 39: C3 ret
801 *| 3A: EB1E jmp +0x20
802 *| 3E ... 59: unknown (space for replacement code?)
803 *| 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
804 *| 5F: 5A pop edx
805 *| 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
806 *| 66: 52 push edx
807 *| 67: 68xxxxxxxx push xxxxxxxx
808 *| 6C: 68yyyyyyyy push KERNEL32.89
809 *| 71: C3 ret
810 *| 72: end?
811 * This function checks if the code is there, and replaces the yyyyyyyy entries
812 * by the functionpointers.
813 * The thunkbuf looks like:
815 *| 00: DWORD length ? don't know exactly
816 *| 04: SEGPTR ptr ? where does it point to?
817 * The segpointer ptr is written into the first DWORD of 'thunk'.
818 * [ok probably]
819 * RETURNS
820 * unclear, pointer to win16 thkbuffer?
822 LPVOID WINAPI ThunkInitLSF(
823 LPBYTE thunk, /* [in] win32 thunk */
824 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
825 DWORD len, /* [in] length of thkbuffer */
826 LPCSTR dll16, /* [in] name of win16 dll */
827 LPCSTR dll32 /* [in] name of win32 dll */
829 LPDWORD addr,addr2;
831 /* FIXME: add checks for valid code ... */
832 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
833 *(void **)(thunk+0x35) = QT_ThunkPrime;
834 *(void **)(thunk+0x6D) = FT_PrologPrime;
836 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
837 return 0;
839 addr2 = MapSL(addr[1]);
840 if (HIWORD(addr2))
841 *(DWORD*)thunk = (DWORD)addr2;
843 return addr2;
846 /***********************************************************************
847 * FT_PrologPrime (KERNEL32.89)
849 * This function is called from the relay code installed by
850 * ThunkInitLSF. It replaces the location from where it was
851 * called by a standard FT_Prolog call stub (which is 'primed'
852 * by inserting the correct target table pointer).
853 * Finally, it calls that stub.
855 * Input: ECX target number + flags (passed through to FT_Prolog)
856 * (ESP) offset of location where target table pointer
857 * is stored, relative to the start of the relay code
858 * (ESP+4) pointer to start of relay code
859 * (this is where the FT_Prolog call stub gets written to)
861 * Note: The two DWORD arguments get popped off the stack.
864 void WINAPI __regs_FT_PrologPrime( CONTEXT *context )
866 DWORD targetTableOffset;
867 LPBYTE relayCode;
869 /* Compensate for the fact that the Wine register relay code thought
870 we were being called, although we were in fact jumped to */
871 context->Esp -= 4;
873 /* Write FT_Prolog call stub */
874 targetTableOffset = stack32_pop(context);
875 relayCode = (LPBYTE)stack32_pop(context);
876 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
878 /* Jump to the call stub just created */
879 context->Eip = (DWORD)relayCode;
881 DEFINE_REGS_ENTRYPOINT( FT_PrologPrime )
883 /***********************************************************************
884 * QT_ThunkPrime (KERNEL32.90)
886 * This function corresponds to FT_PrologPrime, but installs a
887 * call stub for QT_Thunk instead.
889 * Input: (EBP-4) target number (passed through to QT_Thunk)
890 * EDX target table pointer location offset
891 * EAX start of relay code
894 void WINAPI __regs_QT_ThunkPrime( CONTEXT *context )
896 DWORD targetTableOffset;
897 LPBYTE relayCode;
899 /* Compensate for the fact that the Wine register relay code thought
900 we were being called, although we were in fact jumped to */
901 context->Esp -= 4;
903 /* Write QT_Thunk call stub */
904 targetTableOffset = context->Edx;
905 relayCode = (LPBYTE)context->Eax;
906 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
908 /* Jump to the call stub just created */
909 context->Eip = (DWORD)relayCode;
911 DEFINE_REGS_ENTRYPOINT( QT_ThunkPrime )
913 /***********************************************************************
914 * ThunkInitSL (KERNEL32.46)
915 * Another thunkbuf link routine.
916 * The start of the thunkbuf looks like this:
917 * 00: DWORD length
918 * 04: SEGPTR address for thunkbuffer pointer
919 * [ok probably]
921 * RETURNS
922 * Nothing.
924 VOID WINAPI ThunkInitSL(
925 LPBYTE thunk, /* [in] start of thunkbuffer */
926 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
927 DWORD len, /* [in] length of thunkbuffer */
928 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
929 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
931 LPDWORD addr;
933 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
934 return;
936 *(DWORD*)MapSL(addr[1]) = (DWORD)thunk;
939 /**********************************************************************
940 * SSInit (KERNEL.700)
941 * RETURNS
942 * TRUE for success.
944 BOOL WINAPI SSInit16(void)
946 return TRUE;
949 /**********************************************************************
950 * SSOnBigStack (KERNEL32.87)
951 * Check if thunking is initialized (ss selector set up etc.)
952 * We do that differently, so just return TRUE.
953 * [ok]
954 * RETURNS
955 * TRUE for success.
957 BOOL WINAPI SSOnBigStack(void)
959 TRACE("Yes, thunking is initialized\n");
960 return TRUE;
963 /**********************************************************************
964 * SSConfirmSmallStack (KERNEL.704)
966 * Abort if not on small stack.
968 * This must be a register routine as it has to preserve *all* registers.
970 void WINAPI SSConfirmSmallStack( CONTEXT *context )
972 /* We are always on the small stack while in 16-bit code ... */
975 /**********************************************************************
976 * SSCall (KERNEL32.88)
977 * One of the real thunking functions. This one seems to be for 32<->32
978 * thunks. It should probably be capable of crossing processboundaries.
980 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
981 * [ok]
983 * RETURNS
984 * Thunked function result.
986 DWORD WINAPIV SSCall(
987 DWORD nr, /* [in] number of argument bytes */
988 DWORD flags, /* [in] FIXME: flags ? */
989 FARPROC fun, /* [in] function to call */
990 ... /* [in/out] arguments */
992 DWORD i,ret;
993 DWORD *args = ((DWORD *)&fun) + 1;
995 TRACE("(%ld,0x%08lx,%p,[",nr,flags,fun);
996 for (i = 0; i < nr/4; i++) TRACE("0x%08lx,",args[i]);
997 TRACE("])\n");
998 ret = call_entry_point( fun, nr / sizeof(DWORD), args );
999 TRACE(" returning %ld ...\n",ret);
1000 return ret;
1003 /**********************************************************************
1004 * W32S_BackTo32 (KERNEL32.51)
1006 void WINAPI __regs_W32S_BackTo32( CONTEXT *context )
1008 LPDWORD stack = (LPDWORD)context->Esp;
1009 FARPROC proc = (FARPROC)context->Eip;
1011 context->Eax = call_entry_point( proc, 10, stack + 1 );
1012 context->Eip = stack32_pop(context);
1014 DEFINE_REGS_ENTRYPOINT( W32S_BackTo32 )
1016 /**********************************************************************
1017 * AllocSLCallback (KERNEL32.@)
1019 * Allocate a 16->32 callback.
1021 * NOTES
1022 * Win95 uses some structchains for callbacks. It allocates them
1023 * in blocks of 100 entries, size 32 bytes each, layout:
1024 * blockstart:
1025 *| 0: PTR nextblockstart
1026 *| 4: entry *first;
1027 *| 8: WORD sel ( start points to blockstart)
1028 *| A: WORD unknown
1029 * 100xentry:
1030 *| 00..17: Code
1031 *| 18: PDB *owning_process;
1032 *| 1C: PTR blockstart
1034 * We ignore this for now. (Just a note for further developers)
1035 * FIXME: use this method, so we don't waste selectors...
1037 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
1038 * the 0x66 prefix switches from word->long registers.
1040 *| 665A pop edx
1041 *| 6668x arg2 x pushl <arg2>
1042 *| 6652 push edx
1043 *| EAx arg1 x jmpf <arg1>
1045 * returns the startaddress of this thunk.
1047 * Note, that they look very similar to the ones allocates by THUNK_Alloc.
1048 * RETURNS
1049 * A segmented pointer to the start of the thunk
1051 DWORD WINAPI
1052 AllocSLCallback(
1053 DWORD finalizer, /* [in] Finalizer function */
1054 DWORD callback /* [in] Callback function */
1056 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
1057 WORD sel;
1059 x=thunk;
1060 *x++=0x66;*x++=0x5a; /* popl edx */
1061 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
1062 *x++=0x66;*x++=0x52; /* pushl edx */
1063 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
1065 *(DWORD*)(thunk+18) = GetCurrentProcessId();
1067 sel = SELECTOR_AllocBlock( thunk, 32, LDT_FLAGS_CODE );
1068 return (sel<<16)|0;
1071 /**********************************************************************
1072 * FreeSLCallback (KERNEL32.@)
1073 * Frees the specified 16->32 callback
1075 * RETURNS
1076 * Nothing.
1078 void WINAPI
1079 FreeSLCallback(
1080 DWORD x /* [in] 16 bit callback (segmented pointer?) */
1082 FIXME("(0x%08lx): stub\n",x);
1085 /**********************************************************************
1086 * AllocMappedBuffer (KERNEL32.38)
1088 * This is an undocumented KERNEL32 function that
1089 * SMapLS's a GlobalAlloc'ed buffer.
1091 * RETURNS
1092 * EDI register: pointer to buffer
1094 * NOTES
1095 * The buffer is preceded by 8 bytes:
1096 * ...
1097 * edi+0 buffer
1098 * edi-4 SEGPTR to buffer
1099 * edi-8 some magic Win95 needs for SUnMapLS
1100 * (we use it for the memory handle)
1102 * The SEGPTR is used by the caller!
1104 void WINAPI __regs_AllocMappedBuffer(
1105 CONTEXT *context /* [in] EDI register: size of buffer to allocate */
1107 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
1108 DWORD *buffer = GlobalLock(handle);
1109 DWORD ptr = 0;
1111 if (buffer)
1112 if (!(ptr = MapLS(buffer + 2)))
1114 GlobalUnlock(handle);
1115 GlobalFree(handle);
1118 if (!ptr)
1119 context->Eax = context->Edi = 0;
1120 else
1122 buffer[0] = (DWORD)handle;
1123 buffer[1] = ptr;
1125 context->Eax = ptr;
1126 context->Edi = (DWORD)(buffer + 2);
1129 DEFINE_REGS_ENTRYPOINT( AllocMappedBuffer )
1131 /**********************************************************************
1132 * FreeMappedBuffer (KERNEL32.39)
1134 * Free a buffer allocated by AllocMappedBuffer
1136 * RETURNS
1137 * Nothing.
1139 void WINAPI __regs_FreeMappedBuffer(
1140 CONTEXT *context /* [in] EDI register: pointer to buffer */
1142 if (context->Edi)
1144 DWORD *buffer = (DWORD *)context->Edi - 2;
1146 UnMapLS(buffer[1]);
1148 GlobalUnlock((HGLOBAL)buffer[0]);
1149 GlobalFree((HGLOBAL)buffer[0]);
1152 DEFINE_REGS_ENTRYPOINT( FreeMappedBuffer )
1154 /**********************************************************************
1155 * GetTEBSelectorFS (KERNEL.475)
1156 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1158 void WINAPI GetTEBSelectorFS16(void)
1160 CURRENT_STACK16->fs = get_fs();
1163 /**********************************************************************
1164 * IsPeFormat (KERNEL.431)
1166 * Determine if a file is a PE format executable.
1168 * RETURNS
1169 * TRUE, if it is.
1170 * FALSE if the file could not be opened or is not a PE file.
1172 * NOTES
1173 * If fn is given as NULL then the function expects hf16 to be valid.
1175 BOOL16 WINAPI IsPeFormat16(
1176 LPSTR fn, /* [in] Filename to the executable */
1177 HFILE16 hf16) /* [in] An open file handle */
1179 BOOL ret = FALSE;
1180 IMAGE_DOS_HEADER mzh;
1181 OFSTRUCT ofs;
1182 DWORD xmagic;
1184 if (fn) hf16 = OpenFile16(fn,&ofs,OF_READ);
1185 if (hf16 == HFILE_ERROR16) return FALSE;
1186 _llseek16(hf16,0,SEEK_SET);
1187 if (sizeof(mzh)!=_lread16(hf16,&mzh,sizeof(mzh))) goto done;
1188 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) goto done;
1189 _llseek16(hf16,mzh.e_lfanew,SEEK_SET);
1190 if (sizeof(DWORD)!=_lread16(hf16,&xmagic,sizeof(DWORD))) goto done;
1191 ret = (xmagic == IMAGE_NT_SIGNATURE);
1192 done:
1193 _lclose16(hf16);
1194 return ret;
1198 /***********************************************************************
1199 * K32Thk1632Prolog (KERNEL32.@)
1201 void WINAPI __regs_K32Thk1632Prolog( CONTEXT *context )
1203 LPBYTE code = (LPBYTE)context->Eip - 5;
1205 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1206 of 16->32 thunks instead of using one of the standard methods!
1207 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1208 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1209 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1210 bypassed, which means it will crash the next time the 32-bit OLE
1211 code thunks down again to 16-bit (this *will* happen!).
1213 The following hack tries to recognize this situation.
1214 This is possible since the called stubs in OLECLI32/OLESVR32 all
1215 look exactly the same:
1216 00 E8xxxxxxxx call K32Thk1632Prolog
1217 05 FF55FC call [ebp-04]
1218 08 E8xxxxxxxx call K32Thk1632Epilog
1219 0D 66CB retf
1221 If we recognize this situation, we try to simulate the actions
1222 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1223 to our 32-bit stack, creating a proper STACK16FRAME and
1224 updating cur_stack. */
1226 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1227 && code[13] == 0x66 && code[14] == 0xCB)
1229 DWORD argSize = context->Ebp - context->Esp;
1230 char *stack16 = (char *)context->Esp - 4;
1231 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1232 STACK32FRAME *frame32 = (STACK32FRAME *)kernel_get_thread_data()->stack;
1233 char *stack32 = (char *)frame32 - argSize;
1234 WORD stackSel = SELECTOROF(frame32->frame16);
1235 DWORD stackBase = GetSelectorBase(stackSel);
1237 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %04x:%04x\n",
1238 context->Ebp, context->Esp, CURRENT_SS, CURRENT_SP);
1240 memset(frame16, '\0', sizeof(STACK16FRAME));
1241 frame16->frame32 = frame32;
1242 frame16->ebp = context->Ebp;
1244 memcpy(stack32, stack16, argSize);
1245 CURRENT_SS = stackSel;
1246 CURRENT_SP = (DWORD)frame16 - stackBase;
1248 context->Esp = (DWORD)stack32 + 4;
1249 context->Ebp = context->Esp + argSize;
1251 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %04x:%04x\n",
1252 context->Ebp, context->Esp, CURRENT_SS, CURRENT_SP);
1255 /* entry_point is never used again once the entry point has
1256 been called. Thus we re-use it to hold the Win16Lock count */
1257 ReleaseThunkLock(&CURRENT_STACK16->entry_point);
1259 DEFINE_REGS_ENTRYPOINT( K32Thk1632Prolog )
1261 /***********************************************************************
1262 * K32Thk1632Epilog (KERNEL32.@)
1264 void WINAPI __regs_K32Thk1632Epilog( CONTEXT *context )
1266 LPBYTE code = (LPBYTE)context->Eip - 13;
1268 RestoreThunkLock(CURRENT_STACK16->entry_point);
1270 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1272 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1273 && code[13] == 0x66 && code[14] == 0xCB)
1275 STACK16FRAME *frame16 = CURRENT_STACK16;
1276 char *stack16 = (char *)(frame16 + 1);
1277 DWORD argSize = frame16->ebp - (DWORD)stack16;
1278 char *stack32 = (char *)frame16->frame32 - argSize;
1280 DWORD nArgsPopped = context->Esp - (DWORD)stack32;
1282 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %04x:%04x\n",
1283 context->Ebp, context->Esp, CURRENT_SS, CURRENT_SP);
1285 kernel_get_thread_data()->stack = (SEGPTR)frame16->frame32;
1287 context->Esp = (DWORD)stack16 + nArgsPopped;
1288 context->Ebp = frame16->ebp;
1290 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %04x:%04x\n",
1291 context->Ebp, context->Esp, CURRENT_SS, CURRENT_SP);
1294 DEFINE_REGS_ENTRYPOINT( K32Thk1632Epilog )
1296 /*********************************************************************
1297 * PK16FNF [KERNEL32.91]
1299 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1300 * string buffer with the 8.3 filename of a recently loaded 16-bit
1301 * module. It is unknown exactly what modules trigger this
1302 * mechanism or what purpose this serves. Win98 Explorer (and
1303 * probably also Win95 with IE 4 shell integration) calls this
1304 * several times during initialization.
1306 * FIXME: find out what this really does and make it work.
1308 void WINAPI PK16FNF(LPSTR strPtr)
1310 FIXME("(%p): stub\n", strPtr);
1312 /* fill in a fake filename that'll be easy to recognize */
1313 strcpy(strPtr, "WINESTUB.FIX");
1316 /***********************************************************************
1317 * 16->32 Flat Thunk routines:
1320 /***********************************************************************
1321 * ThunkConnect16 (KERNEL.651)
1322 * Connects a 32bit and a 16bit thunkbuffer.
1324 UINT WINAPI ThunkConnect16(
1325 LPSTR module16, /* [in] name of win16 dll */
1326 LPSTR module32, /* [in] name of win32 dll */
1327 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
1328 DWORD dwReason, /* [in] initialisation argument */
1329 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
1330 LPSTR thunkfun32, /* [in] win32 thunkfunction */
1331 WORD cs /* [in] CS of win16 dll */
1333 BOOL directionSL;
1335 if (!strncmp(TD->magic, "SL01", 4))
1337 directionSL = TRUE;
1339 TRACE("SL01 thunk %s (%p) -> %s (%s), Reason: %ld\n",
1340 module16, TD, module32, thunkfun32, dwReason);
1342 else if (!strncmp(TD->magic, "LS01", 4))
1344 directionSL = FALSE;
1346 TRACE("LS01 thunk %s (%p) <- %s (%s), Reason: %ld\n",
1347 module16, TD, module32, thunkfun32, dwReason);
1349 else
1351 ERR("Invalid magic %c%c%c%c\n",
1352 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
1353 return 0;
1356 switch (dwReason)
1358 case DLL_PROCESS_ATTACH:
1359 if (directionSL)
1361 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
1362 struct ThunkDataSL *SL = SL16->fpData;
1364 if (SL == NULL)
1366 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
1368 SL->common = SL16->common;
1369 SL->flags1 = SL16->flags1;
1370 SL->flags2 = SL16->flags2;
1372 SL->apiDB = MapSL(SL16->apiDatabase);
1373 SL->targetDB = NULL;
1375 lstrcpynA(SL->pszDll16, module16, 255);
1376 lstrcpynA(SL->pszDll32, module32, 255);
1378 /* We should create a SEGPTR to the ThunkDataSL,
1379 but since the contents are not in the original format,
1380 any access to this by 16-bit code would crash anyway. */
1381 SL16->spData = 0;
1382 SL16->fpData = SL;
1386 if (SL->flags2 & 0x80000000)
1388 TRACE("Preloading 32-bit library\n");
1389 LoadLibraryA(module32);
1392 else
1394 /* nothing to do */
1396 break;
1398 case DLL_PROCESS_DETACH:
1399 /* FIXME: cleanup */
1400 break;
1403 return 1;
1407 /***********************************************************************
1408 * C16ThkSL (KERNEL.630)
1411 void WINAPI C16ThkSL(CONTEXT *context)
1413 LPBYTE stub = MapSL(context->Eax), x = stub;
1415 /* We produce the following code:
1417 * mov ax, __FLATDS
1418 * mov es, ax
1419 * movzx ecx, cx
1420 * mov edx, es:[ecx + $EDX]
1421 * push bp
1422 * push edx
1423 * push dx
1424 * push edx
1425 * call __FLATCS:__wine_call_from_16_thunk
1428 *x++ = 0xB8; *(WORD *)x = get_ds(); x += sizeof(WORD);
1429 *x++ = 0x8E; *x++ = 0xC0;
1430 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1431 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1432 *x++ = 0x91; *(DWORD *)x = context->Edx; x += sizeof(DWORD);
1434 *x++ = 0x55;
1435 *x++ = 0x66; *x++ = 0x52;
1436 *x++ = 0x52;
1437 *x++ = 0x66; *x++ = 0x52;
1438 *x++ = 0x66; *x++ = 0x9A;
1439 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1440 *(WORD *)x = get_cs(); x += sizeof(WORD);
1442 /* Jump to the stub code just created */
1443 context->Eip = LOWORD(context->Eax);
1444 context->SegCs = HIWORD(context->Eax);
1446 /* Since C16ThkSL got called by a jmp, we need to leave the
1447 original return address on the stack */
1448 context->Esp -= 4;
1451 /***********************************************************************
1452 * C16ThkSL01 (KERNEL.631)
1455 void WINAPI C16ThkSL01(CONTEXT *context)
1457 LPBYTE stub = MapSL(context->Eax), x = stub;
1459 if (stub)
1461 struct ThunkDataSL16 *SL16 = MapSL(context->Edx);
1462 struct ThunkDataSL *td = SL16->fpData;
1464 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), (LPCSTR)631);
1466 if (!td)
1468 ERR("ThunkConnect16 was not called!\n");
1469 return;
1472 TRACE("Creating stub for ThunkDataSL %p\n", td);
1475 /* We produce the following code:
1477 * xor eax, eax
1478 * mov edx, $td
1479 * call C16ThkSL01
1480 * push bp
1481 * push edx
1482 * push dx
1483 * push edx
1484 * call __FLATCS:__wine_call_from_16_thunk
1487 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1488 *x++ = 0x66; *x++ = 0xBA; *(void **)x = td; x += sizeof(void *);
1489 *x++ = 0x9A; *(DWORD *)x = procAddress; x += sizeof(DWORD);
1491 *x++ = 0x55;
1492 *x++ = 0x66; *x++ = 0x52;
1493 *x++ = 0x52;
1494 *x++ = 0x66; *x++ = 0x52;
1495 *x++ = 0x66; *x++ = 0x9A;
1496 *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
1497 *(WORD *)x = get_cs(); x += sizeof(WORD);
1499 /* Jump to the stub code just created */
1500 context->Eip = LOWORD(context->Eax);
1501 context->SegCs = HIWORD(context->Eax);
1503 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1504 original return address on the stack */
1505 context->Esp -= 4;
1507 else
1509 struct ThunkDataSL *td = (struct ThunkDataSL *)context->Edx;
1510 DWORD targetNr = LOWORD(context->Ecx) / 4;
1511 struct SLTargetDB *tdb;
1513 TRACE("Process %08lx calling target %ld of ThunkDataSL %p\n",
1514 GetCurrentProcessId(), targetNr, td);
1516 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1517 if (tdb->process == GetCurrentProcessId())
1518 break;
1520 if (!tdb)
1522 TRACE("Loading 32-bit library %s\n", td->pszDll32);
1523 LoadLibraryA(td->pszDll32);
1525 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1526 if (tdb->process == GetCurrentProcessId())
1527 break;
1530 if (tdb)
1532 context->Edx = tdb->targetTable[targetNr];
1534 TRACE("Call target is %08lx\n", context->Edx);
1536 else
1538 WORD *stack = MapSL( MAKESEGPTR(context->SegSs, LOWORD(context->Esp)) );
1539 context->Edx = (context->Edx & ~0xffff) | HIWORD(td->apiDB[targetNr].errorReturnValue);
1540 context->Eax = (context->Eax & ~0xffff) | LOWORD(td->apiDB[targetNr].errorReturnValue);
1541 context->Eip = stack[2];
1542 context->SegCs = stack[3];
1543 context->Esp += td->apiDB[targetNr].nrArgBytes + 4;
1545 ERR("Process %08lx did not ThunkConnect32 %s to %s\n",
1546 GetCurrentProcessId(), td->pszDll32, td->pszDll16);
1552 /***********************************************************************
1553 * 16<->32 Thunklet/Callback API:
1556 #include "pshpack1.h"
1557 typedef struct _THUNKLET
1559 BYTE prefix_target;
1560 BYTE pushl_target;
1561 DWORD target;
1563 BYTE prefix_relay;
1564 BYTE pushl_relay;
1565 DWORD relay;
1567 BYTE jmp_glue;
1568 DWORD glue;
1570 BYTE type;
1571 HINSTANCE16 owner;
1572 struct _THUNKLET *next;
1573 } THUNKLET;
1574 #include "poppack.h"
1576 #define THUNKLET_TYPE_LS 1
1577 #define THUNKLET_TYPE_SL 2
1579 static HANDLE ThunkletHeap = 0;
1580 static WORD ThunkletCodeSel;
1581 static THUNKLET *ThunkletAnchor = NULL;
1583 static FARPROC ThunkletSysthunkGlueLS = 0;
1584 static SEGPTR ThunkletSysthunkGlueSL = 0;
1586 static FARPROC ThunkletCallbackGlueLS = 0;
1587 static SEGPTR ThunkletCallbackGlueSL = 0;
1590 /* map a thunk allocated on ThunkletHeap to a 16-bit pointer */
1591 static inline SEGPTR get_segptr( void *thunk )
1593 if (!thunk) return 0;
1594 return MAKESEGPTR( ThunkletCodeSel, (char *)thunk - (char *)ThunkletHeap );
1597 /***********************************************************************
1598 * THUNK_Init
1600 static BOOL THUNK_Init(void)
1602 LPBYTE thunk;
1604 ThunkletHeap = HeapCreate( HEAP_CREATE_ENABLE_EXECUTE, 0x10000, 0x10000 );
1605 if (!ThunkletHeap) return FALSE;
1607 ThunkletCodeSel = SELECTOR_AllocBlock( ThunkletHeap, 0x10000, LDT_FLAGS_CODE );
1609 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1610 if (!thunk) return FALSE;
1612 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1613 *thunk++ = 0x58; /* popl eax */
1614 *thunk++ = 0xC3; /* ret */
1616 ThunkletSysthunkGlueSL = get_segptr( thunk );
1617 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1618 *thunk++ = 0xCB; /* lret */
1620 return TRUE;
1623 /***********************************************************************
1624 * SetThunkletCallbackGlue (KERNEL.560)
1626 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1628 ThunkletCallbackGlueLS = glueLS;
1629 ThunkletCallbackGlueSL = glueSL;
1633 /***********************************************************************
1634 * THUNK_FindThunklet
1636 static THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1637 DWORD glue, BYTE type )
1639 THUNKLET *thunk;
1641 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1642 if ( thunk->type == type
1643 && thunk->target == target
1644 && thunk->relay == relay
1645 && ( type == THUNKLET_TYPE_LS ?
1646 ( thunk->glue == glue - (DWORD)&thunk->type )
1647 : ( thunk->glue == glue ) ) )
1648 return thunk;
1650 return NULL;
1653 /***********************************************************************
1654 * THUNK_AllocLSThunklet
1656 static FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1657 FARPROC glue, HTASK16 owner )
1659 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1660 THUNKLET_TYPE_LS );
1661 if (!thunk)
1663 TDB *pTask = GlobalLock16( owner );
1665 if (!ThunkletHeap) THUNK_Init();
1666 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1667 return 0;
1669 thunk->prefix_target = thunk->prefix_relay = 0x90;
1670 thunk->pushl_target = thunk->pushl_relay = 0x68;
1671 thunk->jmp_glue = 0xE9;
1673 thunk->target = (DWORD)target;
1674 thunk->relay = relay;
1675 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1677 thunk->type = THUNKLET_TYPE_LS;
1678 thunk->owner = pTask? pTask->hInstance : 0;
1680 thunk->next = ThunkletAnchor;
1681 ThunkletAnchor = thunk;
1684 return (FARPROC)thunk;
1687 /***********************************************************************
1688 * THUNK_AllocSLThunklet
1690 static SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1691 SEGPTR glue, HTASK16 owner )
1693 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1694 THUNKLET_TYPE_SL );
1695 if (!thunk)
1697 TDB *pTask = GlobalLock16( owner );
1699 if (!ThunkletHeap) THUNK_Init();
1700 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1701 return 0;
1703 thunk->prefix_target = thunk->prefix_relay = 0x66;
1704 thunk->pushl_target = thunk->pushl_relay = 0x68;
1705 thunk->jmp_glue = 0xEA;
1707 thunk->target = (DWORD)target;
1708 thunk->relay = relay;
1709 thunk->glue = (DWORD)glue;
1711 thunk->type = THUNKLET_TYPE_SL;
1712 thunk->owner = pTask? pTask->hInstance : 0;
1714 thunk->next = ThunkletAnchor;
1715 ThunkletAnchor = thunk;
1718 return get_segptr( thunk );
1721 /**********************************************************************
1722 * IsLSThunklet
1724 static BOOL16 IsLSThunklet( THUNKLET *thunk )
1726 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1727 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1728 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1731 /**********************************************************************
1732 * IsSLThunklet (KERNEL.612)
1734 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1736 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1737 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1738 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1743 /***********************************************************************
1744 * AllocLSThunkletSysthunk (KERNEL.607)
1746 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1747 FARPROC relay, DWORD dummy )
1749 if (!ThunkletSysthunkGlueLS) THUNK_Init();
1750 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1751 ThunkletSysthunkGlueLS, GetCurrentTask() );
1754 /***********************************************************************
1755 * AllocSLThunkletSysthunk (KERNEL.608)
1757 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1758 SEGPTR relay, DWORD dummy )
1760 if (!ThunkletSysthunkGlueSL) THUNK_Init();
1761 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1762 ThunkletSysthunkGlueSL, GetCurrentTask() );
1766 /***********************************************************************
1767 * AllocLSThunkletCallbackEx (KERNEL.567)
1769 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1770 DWORD relay, HTASK16 task )
1772 THUNKLET *thunk = MapSL( target );
1773 if ( !thunk ) return NULL;
1775 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1776 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1777 return (FARPROC)thunk->target;
1779 return THUNK_AllocLSThunklet( target, relay,
1780 ThunkletCallbackGlueLS, task );
1783 /***********************************************************************
1784 * AllocSLThunkletCallbackEx (KERNEL.568)
1786 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1787 DWORD relay, HTASK16 task )
1789 THUNKLET *thunk = (THUNKLET *)target;
1790 if ( !thunk ) return 0;
1792 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1793 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1794 return (SEGPTR)thunk->target;
1796 return THUNK_AllocSLThunklet( target, relay,
1797 ThunkletCallbackGlueSL, task );
1800 /***********************************************************************
1801 * AllocLSThunkletCallback (KERNEL.561)
1802 * AllocLSThunkletCallback_dup (KERNEL.606)
1804 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1806 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1809 /***********************************************************************
1810 * AllocSLThunkletCallback (KERNEL.562)
1811 * AllocSLThunkletCallback_dup (KERNEL.605)
1813 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1815 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1818 /***********************************************************************
1819 * FindLSThunkletCallback (KERNEL.563)
1820 * FindLSThunkletCallback_dup (KERNEL.609)
1822 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1824 THUNKLET *thunk = MapSL( target );
1825 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1826 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1827 return (FARPROC)thunk->target;
1829 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1830 (DWORD)ThunkletCallbackGlueLS,
1831 THUNKLET_TYPE_LS );
1832 return (FARPROC)thunk;
1835 /***********************************************************************
1836 * FindSLThunkletCallback (KERNEL.564)
1837 * FindSLThunkletCallback_dup (KERNEL.610)
1839 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1841 THUNKLET *thunk = (THUNKLET *)target;
1842 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1843 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1844 return (SEGPTR)thunk->target;
1846 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1847 (DWORD)ThunkletCallbackGlueSL,
1848 THUNKLET_TYPE_SL );
1849 return get_segptr( thunk );
1853 /***********************************************************************
1854 * FreeThunklet (KERNEL.611)
1856 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1858 return FALSE;
1862 /***********************************************************************
1863 * Callback Client API
1866 #define N_CBC_FIXED 20
1867 #define N_CBC_VARIABLE 10
1868 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1870 static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
1871 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1873 /***********************************************************************
1874 * RegisterCBClient (KERNEL.619)
1876 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1877 SEGPTR relay16, FARPROC *relay32 )
1879 /* Search for free Callback ID */
1880 if ( wCBCId == -1 )
1881 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1882 if ( !CBClientRelay16[ wCBCId ] )
1883 break;
1885 /* Register Callback ID */
1886 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1888 CBClientRelay16[ wCBCId ] = relay16;
1889 CBClientRelay32[ wCBCId ] = relay32;
1891 else
1892 wCBCId = 0;
1894 return wCBCId;
1897 /***********************************************************************
1898 * UnRegisterCBClient (KERNEL.622)
1900 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1901 SEGPTR relay16, FARPROC *relay32 )
1903 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1904 && CBClientRelay16[ wCBCId ] == relay16
1905 && CBClientRelay32[ wCBCId ] == relay32 )
1907 CBClientRelay16[ wCBCId ] = 0;
1908 CBClientRelay32[ wCBCId ] = 0;
1910 else
1911 wCBCId = 0;
1913 return wCBCId;
1917 /***********************************************************************
1918 * InitCBClient (KERNEL.623)
1920 void WINAPI InitCBClient16( FARPROC glueLS )
1922 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1923 SEGPTR glueSL = (SEGPTR)GetProcAddress16( kernel, (LPCSTR)604 );
1925 SetThunkletCallbackGlue16( glueLS, glueSL );
1928 /***********************************************************************
1929 * CBClientGlueSL (KERNEL.604)
1931 void WINAPI CBClientGlueSL( CONTEXT *context )
1933 /* Create stack frame */
1934 SEGPTR stackSeg = stack16_push( 12 );
1935 LPWORD stackLin = MapSL( stackSeg );
1936 SEGPTR glue, *glueTab;
1938 stackLin[3] = (WORD)context->Ebp;
1939 stackLin[2] = (WORD)context->Esi;
1940 stackLin[1] = (WORD)context->Edi;
1941 stackLin[0] = (WORD)context->SegDs;
1943 context->Ebp = OFFSETOF( stackSeg ) + 6;
1944 context->Esp = OFFSETOF( stackSeg ) - 4;
1945 context->SegGs = 0;
1947 /* Jump to 16-bit relay code */
1948 glueTab = MapSL( CBClientRelay16[ stackLin[5] ] );
1949 glue = glueTab[ stackLin[4] ];
1950 context->SegCs = SELECTOROF( glue );
1951 context->Eip = OFFSETOF ( glue );
1954 /*******************************************************************
1955 * CALL32_CBClient
1957 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
1959 * Since the relay stub is itself 32-bit, this should not be a problem;
1960 * unfortunately, the relay stubs are expected to switch back to a
1961 * 16-bit stack (and 16-bit code) after completion :-(
1963 * This would conflict with our 16- vs. 32-bit stack handling, so
1964 * we simply switch *back* to our 32-bit stack before returning to
1965 * the caller ...
1967 * The CBClient relay stub expects to be called with the following
1968 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
1969 * stack at the designated places:
1971 * ...
1972 * (ebp+14) original arguments to the callback routine
1973 * (ebp+10) far return address to original caller
1974 * (ebp+6) Thunklet target address
1975 * (ebp+2) Thunklet relay ID code
1976 * (ebp) BP (saved by CBClientGlueSL)
1977 * (ebp-2) SI (saved by CBClientGlueSL)
1978 * (ebp-4) DI (saved by CBClientGlueSL)
1979 * (ebp-6) DS (saved by CBClientGlueSL)
1981 * ... buffer space used by the 16-bit side glue for temp copies
1983 * (ebx+4) far return address to 16-bit side glue code
1984 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
1986 * The 32-bit side glue code accesses both the original arguments (via ebp)
1987 * and the temporary copies prepared by the 16-bit side glue (via ebx).
1988 * After completion, the stub will load ss:sp from the buffer at ebx
1989 * and perform a far return to 16-bit code.
1991 * To trick the relay stub into returning to us, we replace the 16-bit
1992 * return address to the glue code by a cs:ip pair pointing to our
1993 * return entry point (the original return address is saved first).
1994 * Our return stub thus called will then reload the 32-bit ss:esp and
1995 * return to 32-bit code (by using and ss:esp value that we have also
1996 * pushed onto the 16-bit stack before and a cs:eip values found at
1997 * that position on the 32-bit stack). The ss:esp to be restored is
1998 * found relative to the 16-bit stack pointer at:
2000 * (ebx-4) ss (flat)
2001 * (ebx-8) sp (32-bit stack pointer)
2003 * The second variant of this routine, CALL32_CBClientEx, which is used
2004 * to implement KERNEL.621, has to cope with yet another problem: Here,
2005 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2006 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2007 * As we have to return to our 32-bit code first, we have to adapt the
2008 * layout of our temporary area so as to include values for the registers
2009 * that are to be restored, and later (in the implementation of KERNEL.621)
2010 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2011 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2012 * and then performs a lret NN, where NN is the number of arguments to be
2013 * removed. Thus, we prepare our temporary area as follows:
2015 * (ebx+22) 16-bit cs (this segment)
2016 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2017 * (ebx+16) 32-bit ss (flat)
2018 * (ebx+12) 32-bit sp (32-bit stack pointer)
2019 * (ebx+10) 16-bit bp (points to ebx+24)
2020 * (ebx+8) 16-bit si (ignored)
2021 * (ebx+6) 16-bit di (ignored)
2022 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2023 * (ebx+2) 16-bit ss (16-bit stack segment)
2024 * (ebx+0) 16-bit sp (points to ebx+4)
2026 * Note that we ensure that DS is not changed and remains the flat segment,
2027 * and the 32-bit stack pointer our own return stub needs fits just
2028 * perfectly into the 8 bytes that are skipped by the Windows stub.
2029 * One problem is that we have to determine the number of removed arguments,
2030 * as these have to be really removed in KERNEL.621. Thus, the BP value
2031 * that we place in the temporary area to be restored, contains the value
2032 * that SP would have if no arguments were removed. By comparing the actual
2033 * value of SP with this value in our return stub we can compute the number
2034 * of removed arguments. This is then returned to KERNEL.621.
2036 * The stack layout of this function:
2037 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2038 * (ebp+16) esi pointer to caller's esi value
2039 * (ebp+12) arg ebp value to be set for relay stub
2040 * (ebp+8) func CBClient relay stub address
2041 * (ebp+4) ret addr
2042 * (ebp) ebp
2044 extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, WORD *stackLin, DWORD *esi );
2045 __ASM_GLOBAL_FUNC( CALL32_CBClient,
2046 "pushl %ebp\n\t"
2047 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2048 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2049 "movl %esp,%ebp\n\t"
2050 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2051 "pushl %edi\n\t"
2052 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
2053 "pushl %esi\n\t"
2054 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2055 "pushl %ebx\n\t"
2056 __ASM_CFI(".cfi_rel_offset %ebx,-12\n\t")
2057 "movl 16(%ebp),%ebx\n\t"
2058 "leal -8(%esp),%eax\n\t"
2059 "movl %eax,-8(%ebx)\n\t"
2060 "movl 20(%ebp),%esi\n\t"
2061 "movl (%esi),%esi\n\t"
2062 "movl 8(%ebp),%eax\n\t"
2063 "movl 12(%ebp),%ebp\n\t"
2064 "pushl %cs\n\t"
2065 "call *%eax\n\t"
2066 "movl 32(%esp),%edi\n\t"
2067 "movl %esi,(%edi)\n\t"
2068 "popl %ebx\n\t"
2069 __ASM_CFI(".cfi_same_value %ebx\n\t")
2070 "popl %esi\n\t"
2071 __ASM_CFI(".cfi_same_value %esi\n\t")
2072 "popl %edi\n\t"
2073 __ASM_CFI(".cfi_same_value %edi\n\t")
2074 "popl %ebp\n\t"
2075 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2076 __ASM_CFI(".cfi_same_value %ebp\n\t")
2077 "ret\n\t" )
2079 /***********************************************************************
2080 * CBClientThunkSL (KERNEL.620)
2082 void WINAPI CBClientThunkSL( CONTEXT *context )
2084 /* Call 32-bit relay code */
2086 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2087 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
2089 /* fill temporary area for the asm code (see comments in winebuild) */
2090 SEGPTR stack = stack16_push( 12 );
2091 LPWORD stackLin = MapSL(stack);
2092 /* stackLin[0] and stackLin[1] reserved for the 32-bit stack ptr */
2093 stackLin[2] = get_ds();
2094 stackLin[3] = 0;
2095 stackLin[4] = OFFSETOF(stack) + 12;
2096 stackLin[5] = SELECTOROF(stack);
2097 stackLin[6] = 0; /* overwrite return address */
2098 stackLin[7] = cbclient_selector;
2099 context->Eax = CALL32_CBClient( proc, args, stackLin + 4, &context->Esi );
2100 stack16_pop( 12 );
2103 extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, WORD *stackLin, DWORD *esi, INT *nArgs );
2104 __ASM_GLOBAL_FUNC( CALL32_CBClientEx,
2105 "pushl %ebp\n\t"
2106 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2107 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
2108 "movl %esp,%ebp\n\t"
2109 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
2110 "pushl %edi\n\t"
2111 __ASM_CFI(".cfi_rel_offset %edi,-4\n\t")
2112 "pushl %esi\n\t"
2113 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
2114 "pushl %ebx\n\t"
2115 __ASM_CFI(".cfi_rel_offset %ebx,-12\n\t")
2116 "movl 16(%ebp),%ebx\n\t"
2117 "leal -8(%esp),%eax\n\t"
2118 "movl %eax,12(%ebx)\n\t"
2119 "movl 20(%ebp),%esi\n\t"
2120 "movl (%esi),%esi\n\t"
2121 "movl 8(%ebp),%eax\n\t"
2122 "movl 12(%ebp),%ebp\n\t"
2123 "pushl %cs\n\t"
2124 "call *%eax\n\t"
2125 "movl 32(%esp),%edi\n\t"
2126 "movl %esi,(%edi)\n\t"
2127 "movl 36(%esp),%ebx\n\t"
2128 "movl %ebp,(%ebx)\n\t"
2129 "popl %ebx\n\t"
2130 __ASM_CFI(".cfi_same_value %ebx\n\t")
2131 "popl %esi\n\t"
2132 __ASM_CFI(".cfi_same_value %esi\n\t")
2133 "popl %edi\n\t"
2134 __ASM_CFI(".cfi_same_value %edi\n\t")
2135 "popl %ebp\n\t"
2136 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
2137 __ASM_CFI(".cfi_same_value %ebp\n\t")
2138 "ret\n\t" )
2140 /***********************************************************************
2141 * CBClientThunkSLEx (KERNEL.621)
2143 void WINAPI CBClientThunkSLEx( CONTEXT *context )
2145 /* Call 32-bit relay code */
2147 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2148 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
2149 INT nArgs;
2150 LPWORD stackLin;
2152 /* fill temporary area for the asm code (see comments in winebuild) */
2153 SEGPTR stack = stack16_push( 24 );
2154 stackLin = MapSL(stack);
2155 stackLin[0] = OFFSETOF(stack) + 4;
2156 stackLin[1] = SELECTOROF(stack);
2157 stackLin[2] = get_ds();
2158 stackLin[5] = OFFSETOF(stack) + 24;
2159 /* stackLin[6] and stackLin[7] reserved for the 32-bit stack ptr */
2160 stackLin[8] = get_ds();
2161 stackLin[9] = 0;
2162 stackLin[10] = 0;
2163 stackLin[11] = cbclientex_selector;
2165 context->Eax = CALL32_CBClientEx( proc, args, stackLin, &context->Esi, &nArgs );
2166 stack16_pop( 24 );
2168 /* Restore registers saved by CBClientGlueSL */
2169 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
2170 context->Ebp = (context->Ebp & ~0xffff) | stackLin[3];
2171 context->Esi = (context->Esi & ~0xffff) | stackLin[2];
2172 context->Edi = (context->Edi & ~0xffff) | stackLin[1];
2173 context->SegDs = stackLin[0];
2174 context->Esp += 16+nArgs;
2176 /* Return to caller of CBClient thunklet */
2177 context->SegCs = stackLin[9];
2178 context->Eip = stackLin[8];
2182 /***********************************************************************
2183 * Get16DLLAddress (KERNEL32.@)
2185 * This function is used by a Win32s DLL if it wants to call a Win16 function.
2186 * A 16:16 segmented pointer to the function is returned.
2187 * Written without any docu.
2189 SEGPTR WINAPI Get16DLLAddress(HMODULE16 handle, LPSTR func_name)
2191 static WORD code_sel32;
2192 FARPROC16 proc_16;
2193 LPBYTE thunk;
2195 if (!code_sel32)
2197 if (!ThunkletHeap) THUNK_Init();
2198 code_sel32 = SELECTOR_AllocBlock( ThunkletHeap, 0x10000, LDT_FLAGS_CODE | LDT_FLAGS_32BIT );
2199 if (!code_sel32) return 0;
2201 if (!(thunk = HeapAlloc( ThunkletHeap, 0, 32 ))) return 0;
2203 if (!handle) handle = GetModuleHandle16("WIN32S16");
2204 proc_16 = GetProcAddress16(handle, func_name);
2206 /* movl proc_16, $edx */
2207 *thunk++ = 0xba;
2208 *(FARPROC16 *)thunk = proc_16;
2209 thunk += sizeof(FARPROC16);
2211 /* jmpl QT_Thunk */
2212 *thunk++ = 0xea;
2213 *(void **)thunk = QT_Thunk;
2214 thunk += sizeof(FARPROC16);
2215 *(WORD *)thunk = get_cs();
2217 return MAKESEGPTR( code_sel32, (char *)thunk - (char *)ThunkletHeap );
2221 /***********************************************************************
2222 * GetWin16DOSEnv (KERNEL32.34)
2223 * Returns some internal value.... probably the default environment database?
2225 DWORD WINAPI GetWin16DOSEnv(void)
2227 FIXME("stub, returning 0\n");
2228 return 0;
2231 /**********************************************************************
2232 * GetPK16SysVar (KERNEL32.92)
2234 LPVOID WINAPI GetPK16SysVar(void)
2236 static BYTE PK16SysVar[128];
2238 FIXME("()\n");
2239 return PK16SysVar;
2242 /**********************************************************************
2243 * CommonUnimpStub (KERNEL32.17)
2245 int WINAPI DECLSPEC_HIDDEN __regs_CommonUnimpStub( const char *name, int type )
2247 FIXME("generic stub %s\n", debugstr_a(name));
2249 switch (type)
2251 case 15: return -1;
2252 case 14: return ERROR_CALL_NOT_IMPLEMENTED;
2253 case 13: return ERROR_NOT_SUPPORTED;
2254 case 1: return 1;
2255 default: return 0;
2258 __ASM_STDCALL_FUNC( CommonUnimpStub, 0,
2259 "pushl %ecx\n\t"
2260 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2261 "shrl $4,%ecx\n\t"
2262 "andl $0xf,%ecx\n\t"
2263 "pushl %ecx\n\t"
2264 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2265 "pushl %eax\n\t"
2266 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
2267 "call " __ASM_STDCALL("__regs_CommonUnimpStub",8) "\n\t"
2268 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
2269 "popl %ecx\n\t"
2270 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
2271 "andl $0xf,%ecx\n\t"
2272 "movl (%esp),%edx\n\t"
2273 "leal (%esp,%ecx,4),%esp\n\t"
2274 "movl %edx,(%esp)\n\t"
2275 "ret" )
2277 /**********************************************************************
2278 * HouseCleanLogicallyDeadHandles (KERNEL32.33)
2280 void WINAPI HouseCleanLogicallyDeadHandles(void)
2282 /* Whatever this is supposed to do, our handles probably
2283 don't need it :-) */
2286 /**********************************************************************
2287 * @ (KERNEL32.100)
2289 BOOL WINAPI _KERNEL32_100(HANDLE threadid,DWORD exitcode,DWORD x)
2291 FIXME("(%p,%ld,0x%08lx): stub\n",threadid,exitcode,x);
2292 return TRUE;
2295 /**********************************************************************
2296 * @ (KERNEL32.99)
2298 * Checks whether the clock has to be switched from daylight
2299 * savings time to standard time or vice versa.
2302 DWORD WINAPI _KERNEL32_99(DWORD x)
2304 FIXME("(0x%08lx): stub\n",x);
2305 return 1;
2309 /***********************************************************************
2310 * Helper for k32 family functions
2312 static void *user32_proc_address(const char *proc_name)
2314 static HMODULE hUser32;
2316 if(!hUser32) hUser32 = LoadLibraryA("user32.dll");
2317 return GetProcAddress(hUser32, proc_name);
2320 /***********************************************************************
2321 * k32CharToOemBuffA (KERNEL32.11)
2323 BOOL WINAPI k32CharToOemBuffA(LPCSTR s, LPSTR d, DWORD len)
2325 WCHAR *bufW;
2327 if ((bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
2329 MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
2330 WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
2331 HeapFree( GetProcessHeap(), 0, bufW );
2333 return TRUE;
2336 /***********************************************************************
2337 * k32CharToOemA (KERNEL32.10)
2339 BOOL WINAPI k32CharToOemA(LPCSTR s, LPSTR d)
2341 if (!s || !d) return TRUE;
2342 return k32CharToOemBuffA( s, d, strlen(s) + 1 );
2345 /***********************************************************************
2346 * k32OemToCharBuffA (KERNEL32.13)
2348 BOOL WINAPI k32OemToCharBuffA(LPCSTR s, LPSTR d, DWORD len)
2350 WCHAR *bufW;
2352 if ((bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
2354 MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
2355 WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
2356 HeapFree( GetProcessHeap(), 0, bufW );
2358 return TRUE;
2361 /***********************************************************************
2362 * k32OemToCharA (KERNEL32.12)
2364 BOOL WINAPI k32OemToCharA(LPCSTR s, LPSTR d)
2366 return k32OemToCharBuffA( s, d, strlen(s) + 1 );
2369 /**********************************************************************
2370 * k32LoadStringA (KERNEL32.14)
2372 INT WINAPI k32LoadStringA(HINSTANCE instance, UINT resource_id,
2373 LPSTR buffer, INT buflen)
2375 static INT (WINAPI *pLoadStringA)(HINSTANCE, UINT, LPSTR, INT);
2377 if(!pLoadStringA) pLoadStringA = user32_proc_address("LoadStringA");
2378 return pLoadStringA(instance, resource_id, buffer, buflen);
2381 /***********************************************************************
2382 * k32wvsprintfA (KERNEL32.16)
2384 INT WINAPI k32wvsprintfA(LPSTR buffer, LPCSTR spec, va_list args)
2386 static INT (WINAPI *pwvsprintfA)(LPSTR, LPCSTR, va_list);
2388 if(!pwvsprintfA) pwvsprintfA = user32_proc_address("wvsprintfA");
2389 return (*pwvsprintfA)(buffer, spec, args);
2392 /***********************************************************************
2393 * k32wsprintfA (KERNEL32.15)
2395 INT WINAPIV k32wsprintfA(LPSTR buffer, LPCSTR spec, ...)
2397 va_list args;
2398 INT res;
2400 va_start(args, spec);
2401 res = k32wvsprintfA(buffer, spec, args);
2402 va_end(args);
2403 return res;
2406 /**********************************************************************
2407 * Catch (KERNEL.55)
2409 * Real prototype is:
2410 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
2412 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT *context )
2414 /* Note: we don't save the current ss, as the catch buffer is */
2415 /* only 9 words long. Hopefully no one will have the silly */
2416 /* idea to change the current stack before calling Throw()... */
2418 /* Windows uses:
2419 * lpbuf[0] = ip
2420 * lpbuf[1] = cs
2421 * lpbuf[2] = sp
2422 * lpbuf[3] = bp
2423 * lpbuf[4] = si
2424 * lpbuf[5] = di
2425 * lpbuf[6] = ds
2426 * lpbuf[7] = unused
2427 * lpbuf[8] = ss
2430 lpbuf[0] = LOWORD(context->Eip);
2431 lpbuf[1] = context->SegCs;
2432 /* Windows pushes 4 more words before saving sp */
2433 lpbuf[2] = LOWORD(context->Esp) - 4 * sizeof(WORD);
2434 lpbuf[3] = LOWORD(context->Ebp);
2435 lpbuf[4] = LOWORD(context->Esi);
2436 lpbuf[5] = LOWORD(context->Edi);
2437 lpbuf[6] = context->SegDs;
2438 lpbuf[7] = 0;
2439 lpbuf[8] = context->SegSs;
2440 context->Eax &= ~0xffff; /* Return 0 */
2444 /**********************************************************************
2445 * Throw (KERNEL.56)
2447 * Real prototype is:
2448 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
2450 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT *context )
2452 STACK16FRAME *pFrame;
2453 STACK32FRAME *frame32;
2455 context->Eax = (context->Eax & ~0xffff) | (WORD)retval;
2457 /* Find the frame32 corresponding to the frame16 we are jumping to */
2458 pFrame = CURRENT_STACK16;
2459 frame32 = pFrame->frame32;
2460 while (frame32 && frame32->frame16)
2462 if (OFFSETOF(frame32->frame16) < CURRENT_SP)
2463 break; /* Something strange is going on */
2464 if (OFFSETOF(frame32->frame16) > lpbuf[2])
2466 /* We found the right frame */
2467 pFrame->frame32 = frame32;
2468 break;
2470 frame32 = ((STACK16FRAME *)MapSL(frame32->frame16))->frame32;
2472 RtlUnwind( &pFrame->frame32->frame, NULL, NULL, 0 );
2474 context->Eip = lpbuf[0];
2475 context->SegCs = lpbuf[1];
2476 context->Esp = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
2477 context->Ebp = lpbuf[3];
2478 context->Esi = lpbuf[4];
2479 context->Edi = lpbuf[5];
2480 context->SegDs = lpbuf[6];
2482 if (lpbuf[8] != context->SegSs)
2483 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );
2488 * 16-bit WOW routines (in KERNEL)
2491 /**********************************************************************
2492 * GetVDMPointer32W (KERNEL.516)
2494 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
2496 GlobalPageLock16(GlobalHandle16(SELECTOROF(vp)));
2497 return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
2500 /***********************************************************************
2501 * LoadLibraryEx32W (KERNEL.513)
2503 DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
2505 HMODULE hModule;
2506 DWORD mutex_count;
2507 OFSTRUCT ofs;
2508 const char *p;
2510 if (!lpszLibFile)
2512 SetLastError(ERROR_INVALID_PARAMETER);
2513 return 0;
2516 /* if the file cannot be found, call LoadLibraryExA anyway, since it might be
2517 a builtin module. This case is handled in MODULE_LoadLibraryExA */
2519 if ((p = strrchr( lpszLibFile, '.' )) && !strchr( p, '\\' )) /* got an extension */
2521 if (OpenFile16( lpszLibFile, &ofs, OF_EXIST ) != HFILE_ERROR16)
2522 lpszLibFile = ofs.szPathName;
2524 else
2526 char buffer[MAX_PATH+4];
2527 strcpy( buffer, lpszLibFile );
2528 strcat( buffer, ".dll" );
2529 if (OpenFile16( buffer, &ofs, OF_EXIST ) != HFILE_ERROR16)
2530 lpszLibFile = ofs.szPathName;
2533 ReleaseThunkLock( &mutex_count );
2534 hModule = LoadLibraryExA( lpszLibFile, (HANDLE)hFile, dwFlags );
2535 RestoreThunkLock( mutex_count );
2537 return (DWORD)hModule;
2540 /***********************************************************************
2541 * GetProcAddress32W (KERNEL.515)
2543 DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
2545 return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
2548 /***********************************************************************
2549 * FreeLibrary32W (KERNEL.514)
2551 DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
2553 BOOL retv;
2554 DWORD mutex_count;
2556 ReleaseThunkLock( &mutex_count );
2557 retv = FreeLibrary( (HMODULE)hLibModule );
2558 RestoreThunkLock( mutex_count );
2559 return (DWORD)retv;
2562 /**********************************************************************
2563 * WOW_CallProc32W
2565 static DWORD WOW_CallProc32W16( FARPROC proc32, DWORD nrofargs, DWORD *args )
2567 DWORD ret;
2568 DWORD mutex_count;
2570 ReleaseThunkLock( &mutex_count );
2571 if (!proc32) ret = 0;
2572 else ret = call_entry_point( proc32, nrofargs & ~CPEX_DEST_CDECL, args );
2573 RestoreThunkLock( mutex_count );
2575 TRACE("returns %08lx\n",ret);
2576 return ret;
2579 /**********************************************************************
2580 * CallProc32W (KERNEL.517)
2582 DWORD WINAPIV CallProc32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
2584 DWORD args[32];
2585 unsigned int i;
2587 TRACE("(%ld,%ld,%p args[",nrofargs,argconvmask,proc32);
2589 for (i=0;i<nrofargs;i++)
2591 if (argconvmask & (1<<i))
2593 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
2594 /* pascal convention, have to reverse the arguments order */
2595 args[nrofargs - i - 1] = (DWORD)MapSL(ptr);
2596 TRACE("%08lx(%p),",ptr,MapSL(ptr));
2598 else
2600 DWORD arg = VA_ARG16( valist, DWORD );
2601 /* pascal convention, have to reverse the arguments order */
2602 args[nrofargs - i - 1] = arg;
2603 TRACE("%ld,", arg);
2606 TRACE("])\n");
2608 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
2609 stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
2611 return WOW_CallProc32W16( proc32, nrofargs, args );
2614 /**********************************************************************
2615 * _CallProcEx32W (KERNEL.518)
2617 DWORD WINAPIV CallProcEx32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
2619 DWORD args[32];
2620 unsigned int i, count = min( 32, nrofargs & ~CPEX_DEST_CDECL );
2622 TRACE("(%s,%ld,%ld,%p args[", nrofargs & CPEX_DEST_CDECL ? "cdecl": "stdcall",
2623 nrofargs & ~CPEX_DEST_CDECL, argconvmask, proc32);
2625 for (i = 0; i < count; i++)
2627 if (argconvmask & (1<<i))
2629 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
2630 args[i] = (DWORD)MapSL(ptr);
2631 TRACE("%08lx(%p),",ptr,MapSL(ptr));
2633 else
2635 DWORD arg = VA_ARG16( valist, DWORD );
2636 args[i] = arg;
2637 TRACE("%ld,", arg);
2640 TRACE("])\n");
2641 return WOW_CallProc32W16( proc32, nrofargs, args );
2645 /**********************************************************************
2646 * WOW16Call (KERNEL.500)
2648 * FIXME!!!
2651 DWORD WINAPIV WOW16Call(WORD x, WORD y, WORD z, VA_LIST16 args)
2653 int i;
2654 DWORD calladdr;
2655 FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
2657 for (i=0;i<x/2;i++) {
2658 WORD a = VA_ARG16(args,WORD);
2659 FIXME("%04x ",a);
2661 calladdr = VA_ARG16(args,DWORD);
2662 stack16_pop( 3*sizeof(WORD) + x + sizeof(DWORD) );
2663 FIXME(") calling address was 0x%08lx\n",calladdr);
2664 return 0;