2 * KERNEL32 thunks and other undocumented stuff
4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1997, 1998 Marcus Meissner
6 * Copyright 1998 Ulrich Weigand
11 #include <sys/types.h>
17 #include "wine/winbase16.h"
19 #include "debugtools.h"
20 #include "flatthunk.h"
23 #include "selectors.h"
24 #include "stackframe.h"
27 DEFAULT_DEBUG_CHANNEL(thunk);
30 extern void __wine_call_from_16_thunk();
32 static void __wine_call_from_16_thunk() { }
35 /***********************************************************************
37 * Win95 internal thunks *
39 ***********************************************************************/
41 /***********************************************************************
42 * LogApiThk (KERNEL.423)
44 void WINAPI LogApiThk( LPSTR func )
46 TRACE( "%s\n", debugstr_a(func) );
49 /***********************************************************************
50 * LogApiThkLSF (KERNEL32.42)
52 * NOTE: needs to preserve all registers!
54 void WINAPI LogApiThkLSF( LPSTR func, CONTEXT86 *context )
56 TRACE( "%s\n", debugstr_a(func) );
59 /***********************************************************************
60 * LogApiThkSL (KERNEL32.44)
62 * NOTE: needs to preserve all registers!
64 void WINAPI LogApiThkSL( LPSTR func, CONTEXT86 *context )
66 TRACE( "%s\n", debugstr_a(func) );
69 /***********************************************************************
70 * LogCBThkSL (KERNEL32.47)
72 * NOTE: needs to preserve all registers!
74 void WINAPI LogCBThkSL( LPSTR func, CONTEXT86 *context )
76 TRACE( "%s\n", debugstr_a(func) );
79 /***********************************************************************
80 * Generates a FT_Prolog call.
82 * 0FB6D1 movzbl edx,cl
83 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
84 * 68xxxxxxxx push FT_Prolog
87 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
91 *x++ = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
92 *x++ = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
93 x+=4; /* mov edx, [4*edx + targetTable] */
94 *x++ = 0x68; *(DWORD*)x = (DWORD)GetProcAddress(GetModuleHandleA("KERNEL32"),"FT_Prolog");
95 x+=4; /* push FT_Prolog */
96 *x++ = 0xC3; /* lret */
97 /* fill rest with 0xCC / int 3 */
100 /***********************************************************************
101 * _write_qtthunk (internal)
102 * Generates a QT_Thunk style call.
105 * 8A4DFC mov cl , [ebp-04]
106 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
107 * B8yyyyyyyy mov eax, QT_Thunk
110 static void _write_qtthunk(
111 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
112 DWORD *targetTable /* [in] start of thunk (for index lookup) */
117 *x++ = 0x33;*x++=0xC9; /* xor ecx,ecx */
118 *x++ = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
119 *x++ = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
120 x+=4; /* mov edx, [4*ecx + targetTable */
121 *x++ = 0xB8; *(DWORD*)x = (DWORD)GetProcAddress(GetModuleHandleA("KERNEL32"),"QT_Thunk");
122 x+=4; /* mov eax , QT_Thunk */
123 *x++ = 0xFF; *x++ = 0xE0; /* jmp eax */
124 /* should fill the rest of the 32 bytes with 0xCC */
127 /***********************************************************************
130 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
131 struct ThunkDataCommon *TD32, DWORD checksum)
133 struct ThunkDataCommon *TD16;
137 if ((hmod = LoadLibrary16(module)) <= 32)
139 ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
140 module, func, module32, module, hmod);
144 if ( !(ordinal = NE_GetOrdinal(hmod, func))
145 || !(TD16 = MapSL((SEGPTR)NE_GetEntryPointEx(hmod, ordinal, FALSE))))
147 ERR("Unable to find thunk data '%s' in %s, required by %s (conflicting/incorrect DLL versions !?).\n",
148 func, module, module32);
152 if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
154 ERR("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
155 module, func, module32,
156 TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
157 TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
161 if (TD32 && TD16->checksum != TD32->checksum)
163 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
164 module, func, module32, TD16->checksum, TD32->checksum);
168 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
170 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
171 module, func, module32, *(LPDWORD)TD16, checksum);
178 /***********************************************************************
179 * GetThunkStuff (KERNEL32.53)
181 LPVOID WINAPI GetThunkStuff(LPSTR module, LPSTR func)
183 return _loadthunk(module, func, "<kernel>", NULL, 0L);
186 /***********************************************************************
187 * GetThunkBuff (KERNEL32.52)
188 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
190 LPVOID WINAPI GetThunkBuff(void)
192 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
195 /***********************************************************************
196 * ThunkConnect32 (KERNEL32.@)
197 * Connects a 32bit and a 16bit thunkbuffer.
199 UINT WINAPI ThunkConnect32(
200 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
201 LPSTR thunkfun16, /* [in] win16 thunkfunction */
202 LPSTR module16, /* [in] name of win16 dll */
203 LPSTR module32, /* [in] name of win32 dll */
204 HMODULE hmod32, /* [in] hmodule of win32 dll */
205 DWORD dwReason /* [in] initialisation argument */
209 if (!strncmp(TD->magic, "SL01", 4))
213 TRACE("SL01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
214 module32, (DWORD)TD, module16, thunkfun16, dwReason);
216 else if (!strncmp(TD->magic, "LS01", 4))
220 TRACE("LS01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
221 module32, (DWORD)TD, module16, thunkfun16, dwReason);
225 ERR("Invalid magic %c%c%c%c\n",
226 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
232 case DLL_PROCESS_ATTACH:
234 struct ThunkDataCommon *TD16;
235 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
240 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
241 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
242 struct SLTargetDB *tdb;
244 if (SL16->fpData == NULL)
246 ERR("ThunkConnect16 was not called!\n");
250 SL32->data = SL16->fpData;
252 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
253 tdb->process = GetCurrentProcessId();
254 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
256 tdb->next = SL32->data->targetDB; /* FIXME: not thread-safe! */
257 SL32->data->targetDB = tdb;
259 TRACE("Process %08lx allocated TargetDB entry for ThunkDataSL %08lx\n",
260 GetCurrentProcessId(), (DWORD)SL32->data);
264 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
265 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
267 LS32->targetTable = MapSL(LS16->targetTable);
269 /* write QT_Thunk and FT_Prolog stubs */
270 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk, LS32->targetTable);
271 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
276 case DLL_PROCESS_DETACH:
284 /**********************************************************************
285 * QT_Thunk (KERNEL32.@)
287 * The target address is in EDX.
288 * The 16 bit arguments start at ESP.
289 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
292 void WINAPI QT_Thunk( CONTEXT86 *context )
297 memcpy(&context16,context,sizeof(context16));
299 context16.SegCs = HIWORD(context->Edx);
300 context16.Eip = LOWORD(context->Edx);
301 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
302 + (WORD)&((STACK16FRAME*)0)->bp;
304 argsize = context->Ebp-context->Esp-0x40;
306 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
307 (LPBYTE)context->Esp, argsize );
309 wine_call_to_16_regs_short( &context16, argsize );
310 context->Eax = context16.Eax;
311 context->Edx = context16.Edx;
312 context->Ecx = context16.Ecx;
314 context->Esp += LOWORD(context16.Esp) -
315 ( OFFSETOF( NtCurrentTeb()->cur_stack ) - argsize );
319 /**********************************************************************
320 * FT_Prolog (KERNEL32.@)
322 * The set of FT_... thunk routines is used instead of QT_Thunk,
323 * if structures have to be converted from 32-bit to 16-bit
324 * (change of member alignment, conversion of members).
326 * The thunk function (as created by the thunk compiler) calls
327 * FT_Prolog at the beginning, to set up a stack frame and
328 * allocate a 64 byte buffer on the stack.
329 * The input parameters (target address and some flags) are
330 * saved for later use by FT_Thunk.
332 * Input: EDX 16-bit target address (SEGPTR)
333 * CX bits 0..7 target number (in target table)
334 * bits 8..9 some flags (unclear???)
335 * bits 10..15 number of DWORD arguments
337 * Output: A new stackframe is created, and a 64 byte buffer
338 * allocated on the stack. The layout of the stack
339 * on return is as follows:
341 * (ebp+4) return address to caller of thunk function
343 * (ebp-4) saved EBX register of caller
344 * (ebp-8) saved ESI register of caller
345 * (ebp-12) saved EDI register of caller
346 * (ebp-16) saved ECX register, containing flags
347 * (ebp-20) bitmap containing parameters that are to be converted
348 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
349 * filled in by the thunk code before calling FT_Thunk
353 * (ebp-48) saved EAX register of caller (unclear, never restored???)
354 * (ebp-52) saved EDX register, containing 16-bit thunk target
359 * ESP is EBP-64 after return.
363 void WINAPI FT_Prolog( CONTEXT86 *context )
365 /* Build stack frame */
366 stack32_push(context, context->Ebp);
367 context->Ebp = context->Esp;
369 /* Allocate 64-byte Thunk Buffer */
371 memset((char *)context->Esp, '\0', 64);
373 /* Store Flags (ECX) and Target Address (EDX) */
374 /* Save other registers to be restored later */
375 *(DWORD *)(context->Ebp - 4) = context->Ebx;
376 *(DWORD *)(context->Ebp - 8) = context->Esi;
377 *(DWORD *)(context->Ebp - 12) = context->Edi;
378 *(DWORD *)(context->Ebp - 16) = context->Ecx;
380 *(DWORD *)(context->Ebp - 48) = context->Eax;
381 *(DWORD *)(context->Ebp - 52) = context->Edx;
384 /**********************************************************************
385 * FT_Thunk (KERNEL32.@)
387 * This routine performs the actual call to 16-bit code,
388 * similar to QT_Thunk. The differences are:
389 * - The call target is taken from the buffer created by FT_Prolog
390 * - Those arguments requested by the thunk code (by setting the
391 * corresponding bit in the bitmap at EBP-20) are converted
392 * from 32-bit pointers to segmented pointers (those pointers
393 * are guaranteed to point to structures copied to the stack
394 * by the thunk code, so we always use the 16-bit stack selector
395 * for those addresses).
397 * The bit #i of EBP-20 corresponds here to the DWORD starting at
400 * FIXME: It is unclear what happens if there are more than 32 WORDs
401 * of arguments, so that the single DWORD bitmap is no longer
405 void WINAPI FT_Thunk( CONTEXT86 *context )
407 DWORD mapESPrelative = *(DWORD *)(context->Ebp - 20);
408 DWORD callTarget = *(DWORD *)(context->Ebp - 52);
412 LPBYTE newstack, oldstack;
414 memcpy(&context16,context,sizeof(context16));
416 context16.SegCs = HIWORD(callTarget);
417 context16.Eip = LOWORD(callTarget);
418 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
419 + (WORD)&((STACK16FRAME*)0)->bp;
421 argsize = context->Ebp-context->Esp-0x40;
422 newstack = (LPBYTE)CURRENT_STACK16 - argsize;
423 oldstack = (LPBYTE)context->Esp;
425 memcpy( newstack, oldstack, argsize );
427 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
428 if (mapESPrelative & (1 << i))
430 SEGPTR *arg = (SEGPTR *)(newstack + 2*i);
431 *arg = MAKESEGPTR(SELECTOROF(NtCurrentTeb()->cur_stack),
432 OFFSETOF(NtCurrentTeb()->cur_stack) - argsize
433 + (*(LPBYTE *)arg - oldstack));
436 wine_call_to_16_regs_short( &context16, argsize );
437 context->Eax = context16.Eax;
438 context->Edx = context16.Edx;
439 context->Ecx = context16.Ecx;
441 context->Esp += LOWORD(context16.Esp) -
442 ( OFFSETOF( NtCurrentTeb()->cur_stack ) - argsize );
444 /* Copy modified buffers back to 32-bit stack */
445 memcpy( oldstack, newstack, argsize );
448 /**********************************************************************
449 * FT_ExitNN (KERNEL32.218 - 232)
451 * One of the FT_ExitNN functions is called at the end of the thunk code.
452 * It removes the stack frame created by FT_Prolog, moves the function
453 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
454 * value, but the thunk code has moved it from EAX to EBX in the
455 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
456 * and perform a return to the CALLER of the thunk code (while removing
457 * the given number of arguments from the caller's stack).
460 static void FT_Exit(CONTEXT86 *context, int nPopArgs)
462 /* Return value is in EBX */
463 context->Eax = context->Ebx;
465 /* Restore EBX, ESI, and EDI registers */
466 context->Ebx = *(DWORD *)(context->Ebp - 4);
467 context->Esi = *(DWORD *)(context->Ebp - 8);
468 context->Edi = *(DWORD *)(context->Ebp - 12);
470 /* Clean up stack frame */
471 context->Esp = context->Ebp;
472 context->Ebp = stack32_pop(context);
474 /* Pop return address to CALLER of thunk code */
475 context->Eip = stack32_pop(context);
476 /* Remove arguments */
477 context->Esp += nPopArgs;
480 /***********************************************************************
481 * FT_Exit0 (KERNEL32.@)
483 void WINAPI FT_Exit0 (CONTEXT86 *context) { FT_Exit(context, 0); }
485 /***********************************************************************
486 * FT_Exit4 (KERNEL32.@)
488 void WINAPI FT_Exit4 (CONTEXT86 *context) { FT_Exit(context, 4); }
490 /***********************************************************************
491 * FT_Exit8 (KERNEL32.@)
493 void WINAPI FT_Exit8 (CONTEXT86 *context) { FT_Exit(context, 8); }
495 /***********************************************************************
496 * FT_Exit12 (KERNEL32.@)
498 void WINAPI FT_Exit12(CONTEXT86 *context) { FT_Exit(context, 12); }
500 /***********************************************************************
501 * FT_Exit16 (KERNEL32.@)
503 void WINAPI FT_Exit16(CONTEXT86 *context) { FT_Exit(context, 16); }
505 /***********************************************************************
506 * FT_Exit20 (KERNEL32.@)
508 void WINAPI FT_Exit20(CONTEXT86 *context) { FT_Exit(context, 20); }
510 /***********************************************************************
511 * FT_Exit24 (KERNEL32.@)
513 void WINAPI FT_Exit24(CONTEXT86 *context) { FT_Exit(context, 24); }
515 /***********************************************************************
516 * FT_Exit28 (KERNEL32.@)
518 void WINAPI FT_Exit28(CONTEXT86 *context) { FT_Exit(context, 28); }
520 /***********************************************************************
521 * FT_Exit32 (KERNEL32.@)
523 void WINAPI FT_Exit32(CONTEXT86 *context) { FT_Exit(context, 32); }
525 /***********************************************************************
526 * FT_Exit36 (KERNEL32.@)
528 void WINAPI FT_Exit36(CONTEXT86 *context) { FT_Exit(context, 36); }
530 /***********************************************************************
531 * FT_Exit40 (KERNEL32.@)
533 void WINAPI FT_Exit40(CONTEXT86 *context) { FT_Exit(context, 40); }
535 /***********************************************************************
536 * FT_Exit44 (KERNEL32.@)
538 void WINAPI FT_Exit44(CONTEXT86 *context) { FT_Exit(context, 44); }
540 /***********************************************************************
541 * FT_Exit48 (KERNEL32.@)
543 void WINAPI FT_Exit48(CONTEXT86 *context) { FT_Exit(context, 48); }
545 /***********************************************************************
546 * FT_Exit52 (KERNEL32.@)
548 void WINAPI FT_Exit52(CONTEXT86 *context) { FT_Exit(context, 52); }
550 /***********************************************************************
551 * FT_Exit56 (KERNEL32.@)
553 void WINAPI FT_Exit56(CONTEXT86 *context) { FT_Exit(context, 56); }
555 /***********************************************************************
556 * ThunkInitLS (KERNEL32.43)
557 * A thunkbuffer link routine
558 * The thunkbuf looks like:
560 * 00: DWORD length ? don't know exactly
561 * 04: SEGPTR ptr ? where does it point to?
562 * The pointer ptr is written into the first DWORD of 'thunk'.
563 * (probably correctly implemented)
566 * segmented pointer to thunk?
568 DWORD WINAPI ThunkInitLS(
569 LPDWORD thunk, /* [in] win32 thunk */
570 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
571 DWORD len, /* [in] thkbuffer length */
572 LPCSTR dll16, /* [in] name of win16 dll */
573 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
577 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
582 *(DWORD*)thunk = addr[1];
587 /***********************************************************************
588 * Common32ThkLS (KERNEL32.45)
590 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
591 * style thunks. The basic difference is that the parameter conversion
592 * is done completely on the *16-bit* side here. Thus we do not call
593 * the 16-bit target directly, but call a common entry point instead.
594 * This entry function then calls the target according to the target
595 * number passed in the DI register.
597 * Input: EAX SEGPTR to the common 16-bit entry point
598 * CX offset in thunk table (target number * 4)
599 * DX error return value if execution fails (unclear???)
600 * EDX.HI number of DWORD parameters
602 * (Note that we need to move the thunk table offset from CX to DI !)
604 * The called 16-bit stub expects its stack to look like this:
606 * (esp+40) 32-bit arguments
608 * (esp+8) 32 byte of stack space available as buffer
609 * (esp) 8 byte return address for use with 0x66 lret
611 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
612 * and uses the EAX register to return a DWORD return value.
613 * Thus we need to use a special assembly glue routine
614 * (CallRegisterLongProc instead of CallRegisterShortProc).
616 * Finally, we return to the caller, popping the arguments off
617 * the stack. The number of arguments to be popped is returned
618 * in the BL register by the called 16-bit routine.
621 void WINAPI Common32ThkLS( CONTEXT86 *context )
626 memcpy(&context16,context,sizeof(context16));
628 context16.Edi = LOWORD(context->Ecx);
629 context16.SegCs = HIWORD(context->Eax);
630 context16.Eip = LOWORD(context->Eax);
631 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
632 + (WORD)&((STACK16FRAME*)0)->bp;
634 argsize = HIWORD(context->Edx) * 4;
636 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
637 if (context->Edx == context->Eip)
640 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
641 (LPBYTE)context->Esp, argsize );
643 wine_call_to_16_regs_long(&context16, argsize + 32);
644 context->Eax = context16.Eax;
646 /* Clean up caller's stack frame */
647 context->Esp += BL_reg(&context16);
650 /***********************************************************************
651 * OT_32ThkLSF (KERNEL32.40)
653 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
654 * argument processing is done on both the 32-bit and the 16-bit side:
655 * The 32-bit side prepares arguments, copying them onto the stack.
657 * When this routine is called, the first word on the stack is the
658 * number of argument bytes prepared by the 32-bit code, and EDX
659 * contains the 16-bit target address.
661 * The called 16-bit routine is another relaycode, doing further
662 * argument processing and then calling the real 16-bit target
663 * whose address is stored at [bp-04].
665 * The call proceeds using a normal CallRegisterShortProc.
666 * After return from the 16-bit relaycode, the arguments need
667 * to be copied *back* to the 32-bit stack, since the 32-bit
668 * relaycode processes output parameters.
670 * Note that we copy twice the number of arguments, since some of the
671 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
672 * arguments of the caller!
674 * (Note that this function seems only to be used for
675 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
677 void WINAPI OT_32ThkLSF( CONTEXT86 *context )
682 memcpy(&context16,context,sizeof(context16));
684 context16.SegCs = HIWORD(context->Edx);
685 context16.Eip = LOWORD(context->Edx);
686 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
687 + (WORD)&((STACK16FRAME*)0)->bp;
689 argsize = 2 * *(WORD *)context->Esp + 2;
691 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
692 (LPBYTE)context->Esp, argsize );
694 wine_call_to_16_regs_short(&context16, argsize);
695 context->Eax = context16.Eax;
696 context->Edx = context16.Edx;
698 /* Copy modified buffers back to 32-bit stack */
699 memcpy( (LPBYTE)context->Esp,
700 (LPBYTE)CURRENT_STACK16 - argsize, argsize );
702 context->Esp += LOWORD(context16.Esp) -
703 ( OFFSETOF( NtCurrentTeb()->cur_stack ) - argsize );
706 /***********************************************************************
707 * ThunkInitLSF (KERNEL32.41)
708 * A thunk setup routine.
709 * Expects a pointer to a preinitialized thunkbuffer in the first argument
711 * 00..03: unknown (pointer, check _41, _43, _46)
714 * 06..23: unknown (space for replacement code, check .90)
716 * 24:>E800000000 call offset 29
717 * 29:>58 pop eax ( target of call )
718 * 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
719 * 2F: BAxxxxxxxx mov edx,xxxxxxxx
720 * 34: 68yyyyyyyy push KERNEL32.90
724 * 3E ... 59: unknown (space for replacement code?)
725 * 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
727 * 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
729 * 67: 68xxxxxxxx push xxxxxxxx
730 * 6C: 68yyyyyyyy push KERNEL32.89
733 * This function checks if the code is there, and replaces the yyyyyyyy entries
734 * by the functionpointers.
735 * The thunkbuf looks like:
737 * 00: DWORD length ? don't know exactly
738 * 04: SEGPTR ptr ? where does it point to?
739 * The segpointer ptr is written into the first DWORD of 'thunk'.
742 * unclear, pointer to win16 thkbuffer?
744 LPVOID WINAPI ThunkInitLSF(
745 LPBYTE thunk, /* [in] win32 thunk */
746 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
747 DWORD len, /* [in] length of thkbuffer */
748 LPCSTR dll16, /* [in] name of win16 dll */
749 LPCSTR dll32 /* [in] name of win32 dll */
751 HMODULE hkrnl32 = GetModuleHandleA("KERNEL32");
754 /* FIXME: add checks for valid code ... */
755 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
756 *(DWORD*)(thunk+0x35) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)90);
757 *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)89);
760 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
763 addr2 = MapSL(addr[1]);
765 *(DWORD*)thunk = (DWORD)addr2;
770 /***********************************************************************
771 * FT_PrologPrime (KERNEL32.89)
773 * This function is called from the relay code installed by
774 * ThunkInitLSF. It replaces the location from where it was
775 * called by a standard FT_Prolog call stub (which is 'primed'
776 * by inserting the correct target table pointer).
777 * Finally, it calls that stub.
779 * Input: ECX target number + flags (passed through to FT_Prolog)
780 * (ESP) offset of location where target table pointer
781 * is stored, relative to the start of the relay code
782 * (ESP+4) pointer to start of relay code
783 * (this is where the FT_Prolog call stub gets written to)
785 * Note: The two DWORD arguments get popped off the stack.
788 void WINAPI FT_PrologPrime( CONTEXT86 *context )
790 DWORD targetTableOffset;
793 /* Compensate for the fact that the Wine register relay code thought
794 we were being called, although we were in fact jumped to */
797 /* Write FT_Prolog call stub */
798 targetTableOffset = stack32_pop(context);
799 relayCode = (LPBYTE)stack32_pop(context);
800 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
802 /* Jump to the call stub just created */
803 context->Eip = (DWORD)relayCode;
806 /***********************************************************************
807 * QT_ThunkPrime (KERNEL32.90)
809 * This function corresponds to FT_PrologPrime, but installs a
810 * call stub for QT_Thunk instead.
812 * Input: (EBP-4) target number (passed through to QT_Thunk)
813 * EDX target table pointer location offset
814 * EAX start of relay code
817 void WINAPI QT_ThunkPrime( CONTEXT86 *context )
819 DWORD targetTableOffset;
822 /* Compensate for the fact that the Wine register relay code thought
823 we were being called, although we were in fact jumped to */
826 /* Write QT_Thunk call stub */
827 targetTableOffset = context->Edx;
828 relayCode = (LPBYTE)context->Eax;
829 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
831 /* Jump to the call stub just created */
832 context->Eip = (DWORD)relayCode;
835 /***********************************************************************
836 * ThunkInitSL (KERNEL32.46)
837 * Another thunkbuf link routine.
838 * The start of the thunkbuf looks like this:
840 * 04: SEGPTR address for thunkbuffer pointer
843 VOID WINAPI ThunkInitSL(
844 LPBYTE thunk, /* [in] start of thunkbuffer */
845 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
846 DWORD len, /* [in] length of thunkbuffer */
847 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
848 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
852 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
855 *(DWORD*)MapSL(addr[1]) = (DWORD)thunk;
858 /**********************************************************************
859 * SSInit (KERNEL.700)
863 BOOL WINAPI SSInit16()
868 /**********************************************************************
869 * SSOnBigStack (KERNEL32.87)
870 * Check if thunking is initialized (ss selector set up etc.)
871 * We do that differently, so just return TRUE.
876 BOOL WINAPI SSOnBigStack()
878 TRACE("Yes, thunking is initialized\n");
882 /**********************************************************************
883 * SSConfirmSmallStack (KERNEL.704)
885 * Abort if not on small stack.
887 * This must be a register routine as it has to preserve *all* registers.
889 void WINAPI SSConfirmSmallStack( CONTEXT86 *context )
891 /* We are always on the small stack while in 16-bit code ... */
894 /**********************************************************************
895 * SSCall (KERNEL32.88)
896 * One of the real thunking functions. This one seems to be for 32<->32
897 * thunks. It should probably be capable of crossing processboundaries.
899 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
902 DWORD WINAPIV SSCall(
903 DWORD nr, /* [in] number of argument bytes */
904 DWORD flags, /* [in] FIXME: flags ? */
905 FARPROC fun, /* [in] function to call */
906 ... /* [in/out] arguments */
909 DWORD *args = ((DWORD *)&fun) + 1;
913 DPRINTF("(%ld,0x%08lx,%p,[",nr,flags,fun);
915 DPRINTF("0x%08lx,",args[i]);
921 case 4: ret = fun(args[0]);
923 case 8: ret = fun(args[0],args[1]);
925 case 12: ret = fun(args[0],args[1],args[2]);
927 case 16: ret = fun(args[0],args[1],args[2],args[3]);
929 case 20: ret = fun(args[0],args[1],args[2],args[3],args[4]);
931 case 24: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
933 case 28: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
935 case 32: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
937 case 36: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
939 case 40: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
941 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]);
943 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]);
946 WARN("Unsupported nr of arguments, %ld\n",nr);
951 TRACE(" returning %ld ...\n",ret);
955 /**********************************************************************
956 * W32S_BackTo32 (KERNEL32.51)
958 void WINAPI W32S_BackTo32( CONTEXT86 *context )
960 LPDWORD stack = (LPDWORD)context->Esp;
961 FARPROC proc = (FARPROC)context->Eip;
963 context->Eax = proc( stack[1], stack[2], stack[3], stack[4], stack[5],
964 stack[6], stack[7], stack[8], stack[9], stack[10] );
966 context->Eip = stack32_pop(context);
969 /**********************************************************************
970 * AllocSLCallback (KERNEL32.@)
972 * Win95 uses some structchains for callbacks. It allocates them
973 * in blocks of 100 entries, size 32 bytes each, layout:
975 * 0: PTR nextblockstart
977 * 8: WORD sel ( start points to blockstart)
981 * 18: PDB *owning_process;
984 * We ignore this for now. (Just a note for further developers)
985 * FIXME: use this method, so we don't waste selectors...
987 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
988 * the 0x66 prefix switches from word->long registers.
991 * 6668x arg2 x pushl <arg2>
993 * EAx arg1 x jmpf <arg1>
995 * returns the startaddress of this thunk.
997 * Note, that they look very similair to the ones allocates by THUNK_Alloc.
999 * segmented pointer to the start of the thunk
1003 DWORD finalizer, /* [in] finalizer function */
1004 DWORD callback /* [in] callback function */
1006 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
1010 *x++=0x66;*x++=0x5a; /* popl edx */
1011 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
1012 *x++=0x66;*x++=0x52; /* pushl edx */
1013 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
1015 *(DWORD*)(thunk+18) = GetCurrentProcessId();
1017 sel = SELECTOR_AllocBlock( thunk, 32, WINE_LDT_FLAGS_CODE );
1021 /**********************************************************************
1022 * FreeSLCallback (KERNEL32.@)
1023 * Frees the specified 16->32 callback
1027 DWORD x /* [in] 16 bit callback (segmented pointer?) */
1029 FIXME("(0x%08lx): stub\n",x);
1033 /**********************************************************************
1034 * GetTEBSelectorFS (KERNEL.475)
1035 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1037 void WINAPI GetTEBSelectorFS16(void)
1039 CURRENT_STACK16->fs = __get_fs();
1042 /**********************************************************************
1043 * IsPeFormat (KERNEL.431)
1044 * Checks the passed filename if it is a PE format executeable
1049 BOOL16 WINAPI IsPeFormat16(
1050 LPSTR fn, /* [in] filename to executeable */
1051 HFILE16 hf16 /* [in] open file, if filename is NULL */
1054 IMAGE_DOS_HEADER mzh;
1058 if (fn) hf16 = OpenFile16(fn,&ofs,OF_READ);
1059 if (hf16 == HFILE_ERROR16) return FALSE;
1060 _llseek16(hf16,0,SEEK_SET);
1061 if (sizeof(mzh)!=_lread16(hf16,&mzh,sizeof(mzh))) goto done;
1062 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) goto done;
1063 _llseek16(hf16,mzh.e_lfanew,SEEK_SET);
1064 if (sizeof(DWORD)!=_lread16(hf16,&xmagic,sizeof(DWORD))) goto done;
1065 ret = (xmagic == IMAGE_NT_SIGNATURE);
1072 /***********************************************************************
1073 * K32Thk1632Prolog (KERNEL32.@)
1075 void WINAPI K32Thk1632Prolog( CONTEXT86 *context )
1077 LPBYTE code = (LPBYTE)context->Eip - 5;
1079 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1080 of 16->32 thunks instead of using one of the standard methods!
1081 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1082 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1083 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1084 bypassed, which means it will crash the next time the 32-bit OLE
1085 code thunks down again to 16-bit (this *will* happen!).
1087 The following hack tries to recognize this situation.
1088 This is possible since the called stubs in OLECLI32/OLESVR32 all
1089 look exactly the same:
1090 00 E8xxxxxxxx call K32Thk1632Prolog
1091 05 FF55FC call [ebp-04]
1092 08 E8xxxxxxxx call K32Thk1632Epilog
1095 If we recognize this situation, we try to simulate the actions
1096 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1097 to our 32-bit stack, creating a proper STACK16FRAME and
1098 updating cur_stack. */
1100 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1101 && code[13] == 0x66 && code[14] == 0xCB)
1103 WORD stackSel = NtCurrentTeb()->stack_sel;
1104 DWORD stackBase = GetSelectorBase(stackSel);
1106 DWORD argSize = context->Ebp - context->Esp;
1107 char *stack16 = (char *)context->Esp - 4;
1108 char *stack32 = (char *)NtCurrentTeb()->cur_stack - argSize;
1109 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1111 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1112 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1114 memset(frame16, '\0', sizeof(STACK16FRAME));
1115 frame16->frame32 = (STACK32FRAME *)NtCurrentTeb()->cur_stack;
1116 frame16->ebp = context->Ebp;
1118 memcpy(stack32, stack16, argSize);
1119 NtCurrentTeb()->cur_stack = MAKESEGPTR(stackSel, (DWORD)frame16 - stackBase);
1121 context->Esp = (DWORD)stack32 + 4;
1122 context->Ebp = context->Esp + argSize;
1124 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1125 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1128 /* entry_point is never used again once the entry point has
1129 been called. Thus we re-use it to hold the Win16Lock count */
1130 ReleaseThunkLock(&CURRENT_STACK16->entry_point);
1133 /***********************************************************************
1134 * K32Thk1632Epilog (KERNEL32.@)
1136 void WINAPI K32Thk1632Epilog( CONTEXT86 *context )
1138 LPBYTE code = (LPBYTE)context->Eip - 13;
1140 RestoreThunkLock(CURRENT_STACK16->entry_point);
1142 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1144 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1145 && code[13] == 0x66 && code[14] == 0xCB)
1147 STACK16FRAME *frame16 = MapSL(NtCurrentTeb()->cur_stack);
1148 char *stack16 = (char *)(frame16 + 1);
1149 DWORD argSize = frame16->ebp - (DWORD)stack16;
1150 char *stack32 = (char *)frame16->frame32 - argSize;
1152 DWORD nArgsPopped = context->Esp - (DWORD)stack32;
1154 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1155 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1157 NtCurrentTeb()->cur_stack = (DWORD)frame16->frame32;
1159 context->Esp = (DWORD)stack16 + nArgsPopped;
1160 context->Ebp = frame16->ebp;
1162 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1163 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1167 /*********************************************************************
1168 * PK16FNF [KERNEL32.91]
1170 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1171 * string buffer with the 8.3 filename of a recently loaded 16-bit
1172 * module. It is unknown exactly what modules trigger this
1173 * mechanism or what purpose this serves. Win98 Explorer (and
1174 * probably also Win95 with IE 4 shell integration) calls this
1175 * several times during initialization.
1177 * FIXME: find out what this really does and make it work.
1179 void WINAPI PK16FNF(LPSTR strPtr)
1181 FIXME("(%p): stub\n", strPtr);
1183 /* fill in a fake filename that'll be easy to recognize */
1184 strcpy(strPtr, "WINESTUB.FIX");
1187 /***********************************************************************
1188 * 16->32 Flat Thunk routines:
1191 /***********************************************************************
1192 * ThunkConnect16 (KERNEL.651)
1193 * Connects a 32bit and a 16bit thunkbuffer.
1195 UINT WINAPI ThunkConnect16(
1196 LPSTR module16, /* [in] name of win16 dll */
1197 LPSTR module32, /* [in] name of win32 dll */
1198 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
1199 DWORD dwReason, /* [in] initialisation argument */
1200 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
1201 LPSTR thunkfun32, /* [in] win32 thunkfunction */
1202 WORD cs /* [in] CS of win16 dll */
1206 if (!strncmp(TD->magic, "SL01", 4))
1210 TRACE("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
1211 module16, (DWORD)TD, module32, thunkfun32, dwReason);
1213 else if (!strncmp(TD->magic, "LS01", 4))
1215 directionSL = FALSE;
1217 TRACE("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
1218 module16, (DWORD)TD, module32, thunkfun32, dwReason);
1222 ERR("Invalid magic %c%c%c%c\n",
1223 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
1229 case DLL_PROCESS_ATTACH:
1232 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
1233 struct ThunkDataSL *SL = SL16->fpData;
1237 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
1239 SL->common = SL16->common;
1240 SL->flags1 = SL16->flags1;
1241 SL->flags2 = SL16->flags2;
1243 SL->apiDB = MapSL(SL16->apiDatabase);
1244 SL->targetDB = NULL;
1246 lstrcpynA(SL->pszDll16, module16, 255);
1247 lstrcpynA(SL->pszDll32, module32, 255);
1249 /* We should create a SEGPTR to the ThunkDataSL,
1250 but since the contents are not in the original format,
1251 any access to this by 16-bit code would crash anyway. */
1257 if (SL->flags2 & 0x80000000)
1259 TRACE("Preloading 32-bit library\n");
1260 LoadLibraryA(module32);
1269 case DLL_PROCESS_DETACH:
1270 /* FIXME: cleanup */
1278 /***********************************************************************
1279 * C16ThkSL (KERNEL.630)
1282 void WINAPI C16ThkSL(CONTEXT86 *context)
1284 LPBYTE stub = MapSL(context->Eax), x = stub;
1285 WORD cs = __get_cs();
1286 WORD ds = __get_ds();
1288 /* We produce the following code:
1293 * mov edx, es:[ecx + $EDX]
1298 * call __FLATCS:__wine_call_from_16_thunk
1301 *x++ = 0xB8; *((WORD *)x)++ = ds;
1302 *x++ = 0x8E; *x++ = 0xC0;
1303 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1304 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1305 *x++ = 0x91; *((DWORD *)x)++ = context->Edx;
1308 *x++ = 0x66; *x++ = 0x52;
1310 *x++ = 0x66; *x++ = 0x52;
1311 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)__wine_call_from_16_thunk;
1312 *((WORD *)x)++ = cs;
1314 /* Jump to the stub code just created */
1315 context->Eip = LOWORD(context->Eax);
1316 context->SegCs = HIWORD(context->Eax);
1318 /* Since C16ThkSL got called by a jmp, we need to leave the
1319 original return address on the stack */
1323 /***********************************************************************
1324 * C16ThkSL01 (KERNEL.631)
1327 void WINAPI C16ThkSL01(CONTEXT86 *context)
1329 LPBYTE stub = MapSL(context->Eax), x = stub;
1333 struct ThunkDataSL16 *SL16 = MapSL(context->Edx);
1334 struct ThunkDataSL *td = SL16->fpData;
1336 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), (LPCSTR)631);
1337 WORD cs = __get_cs();
1341 ERR("ThunkConnect16 was not called!\n");
1345 TRACE("Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
1348 /* We produce the following code:
1357 * call __FLATCS:__wine_call_from_16_thunk
1360 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1361 *x++ = 0x66; *x++ = 0xBA; *((DWORD *)x)++ = (DWORD)td;
1362 *x++ = 0x9A; *((DWORD *)x)++ = procAddress;
1365 *x++ = 0x66; *x++ = 0x52;
1367 *x++ = 0x66; *x++ = 0x52;
1368 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)__wine_call_from_16_thunk;
1369 *((WORD *)x)++ = cs;
1371 /* Jump to the stub code just created */
1372 context->Eip = LOWORD(context->Eax);
1373 context->SegCs = HIWORD(context->Eax);
1375 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1376 orginal return address on the stack */
1381 struct ThunkDataSL *td = (struct ThunkDataSL *)context->Edx;
1382 DWORD targetNr = CX_reg(context) / 4;
1383 struct SLTargetDB *tdb;
1385 TRACE("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1386 GetCurrentProcessId(), targetNr, (DWORD)td);
1388 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1389 if (tdb->process == GetCurrentProcessId())
1394 TRACE("Loading 32-bit library %s\n", td->pszDll32);
1395 LoadLibraryA(td->pszDll32);
1397 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1398 if (tdb->process == GetCurrentProcessId())
1404 context->Edx = tdb->targetTable[targetNr];
1406 TRACE("Call target is %08lx\n", context->Edx);
1410 WORD *stack = MapSL( MAKESEGPTR(context->SegSs, LOWORD(context->Esp)) );
1411 DX_reg(context) = HIWORD(td->apiDB[targetNr].errorReturnValue);
1412 AX_reg(context) = LOWORD(td->apiDB[targetNr].errorReturnValue);
1413 context->Eip = stack[2];
1414 context->SegCs = stack[3];
1415 context->Esp += td->apiDB[targetNr].nrArgBytes + 4;
1417 ERR("Process %08lx did not ThunkConnect32 %s to %s\n",
1418 GetCurrentProcessId(), td->pszDll32, td->pszDll16);
1424 /***********************************************************************
1425 * 16<->32 Thunklet/Callback API:
1428 #include "pshpack1.h"
1429 typedef struct _THUNKLET
1444 struct _THUNKLET *next;
1446 #include "poppack.h"
1448 #define THUNKLET_TYPE_LS 1
1449 #define THUNKLET_TYPE_SL 2
1451 static HANDLE ThunkletHeap = 0;
1452 static WORD ThunkletCodeSel;
1453 static THUNKLET *ThunkletAnchor = NULL;
1455 static FARPROC ThunkletSysthunkGlueLS = 0;
1456 static SEGPTR ThunkletSysthunkGlueSL = 0;
1458 static FARPROC ThunkletCallbackGlueLS = 0;
1459 static SEGPTR ThunkletCallbackGlueSL = 0;
1462 /* map a thunk allocated on ThunkletHeap to a 16-bit pointer */
1463 inline static SEGPTR get_segptr( void *thunk )
1465 if (!thunk) return 0;
1466 return MAKESEGPTR( ThunkletCodeSel, (char *)thunk - (char *)ThunkletHeap );
1469 /***********************************************************************
1472 static BOOL THUNK_Init(void)
1476 ThunkletHeap = HeapCreate( 0, 0x10000, 0x10000 );
1477 if (!ThunkletHeap) return FALSE;
1479 ThunkletCodeSel = SELECTOR_AllocBlock( (void *)ThunkletHeap, 0x10000, WINE_LDT_FLAGS_CODE );
1481 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1482 if (!thunk) return FALSE;
1484 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1485 *thunk++ = 0x58; /* popl eax */
1486 *thunk++ = 0xC3; /* ret */
1488 ThunkletSysthunkGlueSL = get_segptr( thunk );
1489 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1490 *thunk++ = 0xCB; /* lret */
1495 /***********************************************************************
1496 * SetThunkletCallbackGlue (KERNEL.560)
1498 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1500 ThunkletCallbackGlueLS = glueLS;
1501 ThunkletCallbackGlueSL = glueSL;
1505 /***********************************************************************
1506 * THUNK_FindThunklet
1508 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1509 DWORD glue, BYTE type )
1513 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1514 if ( thunk->type == type
1515 && thunk->target == target
1516 && thunk->relay == relay
1517 && ( type == THUNKLET_TYPE_LS ?
1518 ( thunk->glue == glue - (DWORD)&thunk->type )
1519 : ( thunk->glue == glue ) ) )
1525 /***********************************************************************
1526 * THUNK_AllocLSThunklet
1528 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1529 FARPROC glue, HTASK16 owner )
1531 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1535 TDB *pTask = TASK_GetPtr( owner );
1537 if (!ThunkletHeap) THUNK_Init();
1538 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1541 thunk->prefix_target = thunk->prefix_relay = 0x90;
1542 thunk->pushl_target = thunk->pushl_relay = 0x68;
1543 thunk->jmp_glue = 0xE9;
1545 thunk->target = (DWORD)target;
1546 thunk->relay = (DWORD)relay;
1547 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1549 thunk->type = THUNKLET_TYPE_LS;
1550 thunk->owner = pTask? pTask->hInstance : 0;
1552 thunk->next = ThunkletAnchor;
1553 ThunkletAnchor = thunk;
1556 return (FARPROC)thunk;
1559 /***********************************************************************
1560 * THUNK_AllocSLThunklet
1562 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1563 SEGPTR glue, HTASK16 owner )
1565 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1569 TDB *pTask = TASK_GetPtr( owner );
1571 if (!ThunkletHeap) THUNK_Init();
1572 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1575 thunk->prefix_target = thunk->prefix_relay = 0x66;
1576 thunk->pushl_target = thunk->pushl_relay = 0x68;
1577 thunk->jmp_glue = 0xEA;
1579 thunk->target = (DWORD)target;
1580 thunk->relay = (DWORD)relay;
1581 thunk->glue = (DWORD)glue;
1583 thunk->type = THUNKLET_TYPE_SL;
1584 thunk->owner = pTask? pTask->hInstance : 0;
1586 thunk->next = ThunkletAnchor;
1587 ThunkletAnchor = thunk;
1590 return get_segptr( thunk );
1593 /**********************************************************************
1596 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
1598 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1599 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1600 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1603 /**********************************************************************
1604 * IsSLThunklet (KERNEL.612)
1606 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1608 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1609 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1610 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1615 /***********************************************************************
1616 * AllocLSThunkletSysthunk (KERNEL.607)
1618 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1619 FARPROC relay, DWORD dummy )
1621 if (!ThunkletSysthunkGlueLS) THUNK_Init();
1622 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1623 ThunkletSysthunkGlueLS, GetCurrentTask() );
1626 /***********************************************************************
1627 * AllocSLThunkletSysthunk (KERNEL.608)
1629 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1630 SEGPTR relay, DWORD dummy )
1632 if (!ThunkletSysthunkGlueSL) THUNK_Init();
1633 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1634 ThunkletSysthunkGlueSL, GetCurrentTask() );
1638 /***********************************************************************
1639 * AllocLSThunkletCallbackEx (KERNEL.567)
1641 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1642 DWORD relay, HTASK16 task )
1644 THUNKLET *thunk = MapSL( target );
1645 if ( !thunk ) return NULL;
1647 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1648 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1649 return (FARPROC)thunk->target;
1651 return THUNK_AllocLSThunklet( target, relay,
1652 ThunkletCallbackGlueLS, task );
1655 /***********************************************************************
1656 * AllocSLThunkletCallbackEx (KERNEL.568)
1658 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1659 DWORD relay, HTASK16 task )
1661 THUNKLET *thunk = (THUNKLET *)target;
1662 if ( !thunk ) return 0;
1664 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1665 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1666 return (SEGPTR)thunk->target;
1668 return THUNK_AllocSLThunklet( target, relay,
1669 ThunkletCallbackGlueSL, task );
1672 /***********************************************************************
1673 * AllocLSThunkletCallback (KERNEL.561)
1674 * AllocLSThunkletCallback_dup (KERNEL.606)
1676 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1678 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1681 /***********************************************************************
1682 * AllocSLThunkletCallback (KERNEL.562)
1683 * AllocSLThunkletCallback_dup (KERNEL.605)
1685 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1687 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1690 /***********************************************************************
1691 * FindLSThunkletCallback (KERNEL.563)
1692 * FindLSThunkletCallback_dup (KERNEL.609)
1694 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1696 THUNKLET *thunk = MapSL( target );
1697 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1698 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1699 return (FARPROC)thunk->target;
1701 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1702 (DWORD)ThunkletCallbackGlueLS,
1704 return (FARPROC)thunk;
1707 /***********************************************************************
1708 * FindSLThunkletCallback (KERNEL.564)
1709 * FindSLThunkletCallback_dup (KERNEL.610)
1711 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1713 THUNKLET *thunk = (THUNKLET *)target;
1714 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1715 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1716 return (SEGPTR)thunk->target;
1718 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1719 (DWORD)ThunkletCallbackGlueSL,
1721 return get_segptr( thunk );
1725 /***********************************************************************
1726 * FreeThunklet (KERNEL.611)
1728 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1734 /***********************************************************************
1735 * Callback Client API
1738 #define N_CBC_FIXED 20
1739 #define N_CBC_VARIABLE 10
1740 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1742 static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
1743 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1745 /***********************************************************************
1746 * RegisterCBClient (KERNEL.619)
1748 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1749 SEGPTR relay16, FARPROC *relay32 )
1751 /* Search for free Callback ID */
1753 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1754 if ( !CBClientRelay16[ wCBCId ] )
1757 /* Register Callback ID */
1758 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1760 CBClientRelay16[ wCBCId ] = relay16;
1761 CBClientRelay32[ wCBCId ] = relay32;
1769 /***********************************************************************
1770 * UnRegisterCBClient (KERNEL.622)
1772 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1773 SEGPTR relay16, FARPROC *relay32 )
1775 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1776 && CBClientRelay16[ wCBCId ] == relay16
1777 && CBClientRelay32[ wCBCId ] == relay32 )
1779 CBClientRelay16[ wCBCId ] = 0;
1780 CBClientRelay32[ wCBCId ] = 0;
1789 /***********************************************************************
1790 * InitCBClient (KERNEL.623)
1792 void WINAPI InitCBClient16( FARPROC glueLS )
1794 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1795 SEGPTR glueSL = (SEGPTR)GetProcAddress16( kernel, (LPCSTR)604 );
1797 SetThunkletCallbackGlue16( glueLS, glueSL );
1800 /***********************************************************************
1801 * CBClientGlueSL (KERNEL.604)
1803 void WINAPI CBClientGlueSL( CONTEXT86 *context )
1805 /* Create stack frame */
1806 SEGPTR stackSeg = stack16_push( 12 );
1807 LPWORD stackLin = MapSL( stackSeg );
1808 SEGPTR glue, *glueTab;
1810 stackLin[3] = BP_reg( context );
1811 stackLin[2] = SI_reg( context );
1812 stackLin[1] = DI_reg( context );
1813 stackLin[0] = context->SegDs;
1815 context->Ebp = OFFSETOF( stackSeg ) + 6;
1816 context->Esp = OFFSETOF( stackSeg ) - 4;
1819 /* Jump to 16-bit relay code */
1820 glueTab = MapSL( CBClientRelay16[ stackLin[5] ] );
1821 glue = glueTab[ stackLin[4] ];
1822 context->SegCs = SELECTOROF( glue );
1823 context->Eip = OFFSETOF ( glue );
1826 /***********************************************************************
1827 * CBClientThunkSL (KERNEL.620)
1829 extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi );
1830 void WINAPI CBClientThunkSL( CONTEXT86 *context )
1832 /* Call 32-bit relay code */
1834 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, BP_reg( context ) ) );
1835 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1837 context->Eax = CALL32_CBClient( proc, args, &context->Esi );
1840 /***********************************************************************
1841 * CBClientThunkSLEx (KERNEL.621)
1843 extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs );
1844 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
1846 /* Call 32-bit relay code */
1848 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, BP_reg( context ) ) );
1849 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1853 context->Eax = CALL32_CBClientEx( proc, args, &context->Esi, &nArgs );
1855 /* Restore registers saved by CBClientGlueSL */
1856 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
1857 BP_reg( context ) = stackLin[3];
1858 SI_reg( context ) = stackLin[2];
1859 DI_reg( context ) = stackLin[1];
1860 context->SegDs = stackLin[0];
1861 context->Esp += 16+nArgs;
1863 /* Return to caller of CBClient thunklet */
1864 context->SegCs = stackLin[9];
1865 context->Eip = stackLin[8];
1869 /***********************************************************************
1870 * Get16DLLAddress (KERNEL32.@)
1872 * This function is used by a Win32s DLL if it wants to call a Win16 function.
1873 * A 16:16 segmented pointer to the function is returned.
1874 * Written without any docu.
1876 SEGPTR WINAPI Get16DLLAddress(HMODULE handle, LPSTR func_name)
1878 static WORD code_sel32;
1884 if (!ThunkletHeap) THUNK_Init();
1885 code_sel32 = SELECTOR_AllocBlock( (void *)ThunkletHeap, 0x10000,
1886 WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
1887 if (!code_sel32) return 0;
1889 if (!(thunk = HeapAlloc( ThunkletHeap, 0, 32 ))) return 0;
1891 if (!handle) handle = GetModuleHandle16("WIN32S16");
1892 proc_16 = GetProcAddress16(handle, func_name);
1894 /* movl proc_16, $edx */
1896 *(FARPROC16 *)thunk = proc_16;
1897 thunk += sizeof(FARPROC16);
1901 *(FARPROC *)thunk = GetProcAddress(GetModuleHandleA("KERNEL32"),"QT_Thunk");
1902 thunk += sizeof(FARPROC16);
1903 *(WORD *)thunk = __get_cs();
1905 return MAKESEGPTR( code_sel32, (char *)thunk - (char *)ThunkletHeap );
1909 /***********************************************************************
1910 * GetWin16DOSEnv (KERNEL32.34)
1911 * Returns some internal value.... probably the default environment database?
1913 DWORD WINAPI GetWin16DOSEnv()
1915 FIXME("stub, returning 0\n");
1919 /**********************************************************************
1920 * GetPK16SysVar (KERNEL32.92)
1922 LPVOID WINAPI GetPK16SysVar(void)
1924 static BYTE PK16SysVar[128];
1930 /**********************************************************************
1931 * CommonUnimpStub (KERNEL32.17)
1933 void WINAPI CommonUnimpStub( CONTEXT86 *context )
1936 MESSAGE( "*** Unimplemented Win32 API: %s\n", (LPSTR)context->Eax );
1938 switch ((context->Ecx >> 4) & 0x0f)
1940 case 15: context->Eax = -1; break;
1941 case 14: context->Eax = 0x78; break;
1942 case 13: context->Eax = 0x32; break;
1943 case 1: context->Eax = 1; break;
1944 default: context->Eax = 0; break;
1947 context->Esp += (context->Ecx & 0x0f) * 4;
1950 /**********************************************************************
1951 * HouseCleanLogicallyDeadHandles (KERNEL32.33)
1953 void WINAPI HouseCleanLogicallyDeadHandles(void)
1955 /* Whatever this is supposed to do, our handles probably
1956 don't need it :-) */
1959 /**********************************************************************
1962 BOOL WINAPI _KERNEL32_100(HANDLE threadid,DWORD exitcode,DWORD x)
1964 FIXME("(%d,%ld,0x%08lx): stub\n",threadid,exitcode,x);
1968 /**********************************************************************
1971 * Checks whether the clock has to be switched from daylight
1972 * savings time to standard time or vice versa.
1975 DWORD WINAPI _KERNEL32_99(DWORD x)
1977 FIXME("(0x%08lx): stub\n",x);
1982 /**********************************************************************
1985 * Real prototype is:
1986 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
1988 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT86 *context )
1990 /* Note: we don't save the current ss, as the catch buffer is */
1991 /* only 9 words long. Hopefully no one will have the silly */
1992 /* idea to change the current stack before calling Throw()... */
2006 lpbuf[0] = LOWORD(context->Eip);
2007 lpbuf[1] = context->SegCs;
2008 /* Windows pushes 4 more words before saving sp */
2009 lpbuf[2] = LOWORD(context->Esp) - 4 * sizeof(WORD);
2010 lpbuf[3] = LOWORD(context->Ebp);
2011 lpbuf[4] = LOWORD(context->Esi);
2012 lpbuf[5] = LOWORD(context->Edi);
2013 lpbuf[6] = context->SegDs;
2015 lpbuf[8] = context->SegSs;
2016 AX_reg(context) = 0; /* Return 0 */
2020 /**********************************************************************
2023 * Real prototype is:
2024 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
2026 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT86 *context )
2028 STACK16FRAME *pFrame;
2029 STACK32FRAME *frame32;
2030 TEB *teb = NtCurrentTeb();
2032 AX_reg(context) = retval;
2034 /* Find the frame32 corresponding to the frame16 we are jumping to */
2035 pFrame = THREAD_STACK16(teb);
2036 frame32 = pFrame->frame32;
2037 while (frame32 && frame32->frame16)
2039 if (OFFSETOF(frame32->frame16) < OFFSETOF(teb->cur_stack))
2040 break; /* Something strange is going on */
2041 if (OFFSETOF(frame32->frame16) > lpbuf[2])
2043 /* We found the right frame */
2044 pFrame->frame32 = frame32;
2047 frame32 = ((STACK16FRAME *)MapSL(frame32->frame16))->frame32;
2050 context->Eip = lpbuf[0];
2051 context->SegCs = lpbuf[1];
2052 context->Esp = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
2053 context->Ebp = lpbuf[3];
2054 context->Esi = lpbuf[4];
2055 context->Edi = lpbuf[5];
2056 context->SegDs = lpbuf[6];
2058 if (lpbuf[8] != context->SegSs)
2059 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );