Added LGPL standard comment, and copyright notices where necessary.
[wine/multimedia.git] / dlls / kernel / thunk.c
blobb4c623748ed3ad5857bb9f5bff2942c0b38e2dcc
1 /*
2 * KERNEL32 thunks and other undocumented stuff
4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1997, 1998 Marcus Meissner
6 * Copyright 1998 Ulrich Weigand
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <string.h>
24 #include <sys/types.h>
25 #include <unistd.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "wine/winbase16.h"
32 #include "wine/debug.h"
33 #include "flatthunk.h"
34 #include "heap.h"
35 #include "module.h"
36 #include "selectors.h"
37 #include "stackframe.h"
38 #include "task.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
42 #ifdef __i386__
43 extern void __wine_call_from_16_thunk();
44 #else
45 static void __wine_call_from_16_thunk() { }
46 #endif
48 /***********************************************************************
49 * *
50 * Win95 internal thunks *
51 * *
52 ***********************************************************************/
54 /***********************************************************************
55 * LogApiThk (KERNEL.423)
57 void WINAPI LogApiThk( LPSTR func )
59 TRACE( "%s\n", debugstr_a(func) );
62 /***********************************************************************
63 * LogApiThkLSF (KERNEL32.42)
65 * NOTE: needs to preserve all registers!
67 void WINAPI LogApiThkLSF( LPSTR func, CONTEXT86 *context )
69 TRACE( "%s\n", debugstr_a(func) );
72 /***********************************************************************
73 * LogApiThkSL (KERNEL32.44)
75 * NOTE: needs to preserve all registers!
77 void WINAPI LogApiThkSL( LPSTR func, CONTEXT86 *context )
79 TRACE( "%s\n", debugstr_a(func) );
82 /***********************************************************************
83 * LogCBThkSL (KERNEL32.47)
85 * NOTE: needs to preserve all registers!
87 void WINAPI LogCBThkSL( LPSTR func, CONTEXT86 *context )
89 TRACE( "%s\n", debugstr_a(func) );
92 /***********************************************************************
93 * Generates a FT_Prolog call.
95 * 0FB6D1 movzbl edx,cl
96 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
97 * 68xxxxxxxx push FT_Prolog
98 * C3 lret
100 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
101 LPBYTE x;
103 x = relayCode;
104 *x++ = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
105 *x++ = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
106 x+=4; /* mov edx, [4*edx + targetTable] */
107 *x++ = 0x68; *(DWORD*)x = (DWORD)GetProcAddress(GetModuleHandleA("KERNEL32"),"FT_Prolog");
108 x+=4; /* push FT_Prolog */
109 *x++ = 0xC3; /* lret */
110 /* fill rest with 0xCC / int 3 */
113 /***********************************************************************
114 * _write_qtthunk (internal)
115 * Generates a QT_Thunk style call.
117 * 33C9 xor ecx, ecx
118 * 8A4DFC mov cl , [ebp-04]
119 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
120 * B8yyyyyyyy mov eax, QT_Thunk
121 * FFE0 jmp eax
123 static void _write_qtthunk(
124 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
125 DWORD *targetTable /* [in] start of thunk (for index lookup) */
127 LPBYTE x;
129 x = relayCode;
130 *x++ = 0x33;*x++=0xC9; /* xor ecx,ecx */
131 *x++ = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
132 *x++ = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
133 x+=4; /* mov edx, [4*ecx + targetTable */
134 *x++ = 0xB8; *(DWORD*)x = (DWORD)GetProcAddress(GetModuleHandleA("KERNEL32"),"QT_Thunk");
135 x+=4; /* mov eax , QT_Thunk */
136 *x++ = 0xFF; *x++ = 0xE0; /* jmp eax */
137 /* should fill the rest of the 32 bytes with 0xCC */
140 /***********************************************************************
141 * _loadthunk
143 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
144 struct ThunkDataCommon *TD32, DWORD checksum)
146 struct ThunkDataCommon *TD16;
147 HMODULE hmod;
148 int ordinal;
150 if ((hmod = LoadLibrary16(module)) <= 32)
152 ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
153 module, func, module32, module, hmod);
154 return 0;
157 if ( !(ordinal = NE_GetOrdinal(hmod, func))
158 || !(TD16 = MapSL((SEGPTR)NE_GetEntryPointEx(hmod, ordinal, FALSE))))
160 ERR("Unable to find thunk data '%s' in %s, required by %s (conflicting/incorrect DLL versions !?).\n",
161 func, module, module32);
162 return 0;
165 if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
167 ERR("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
168 module, func, module32,
169 TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
170 TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
171 return 0;
174 if (TD32 && TD16->checksum != TD32->checksum)
176 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
177 module, func, module32, TD16->checksum, TD32->checksum);
178 return 0;
181 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
183 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
184 module, func, module32, *(LPDWORD)TD16, checksum);
185 return 0;
188 return TD16;
191 /***********************************************************************
192 * GetThunkStuff (KERNEL32.53)
194 LPVOID WINAPI GetThunkStuff(LPSTR module, LPSTR func)
196 return _loadthunk(module, func, "<kernel>", NULL, 0L);
199 /***********************************************************************
200 * GetThunkBuff (KERNEL32.52)
201 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
203 LPVOID WINAPI GetThunkBuff(void)
205 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
208 /***********************************************************************
209 * ThunkConnect32 (KERNEL32.@)
210 * Connects a 32bit and a 16bit thunkbuffer.
212 UINT WINAPI ThunkConnect32(
213 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
214 LPSTR thunkfun16, /* [in] win16 thunkfunction */
215 LPSTR module16, /* [in] name of win16 dll */
216 LPSTR module32, /* [in] name of win32 dll */
217 HMODULE hmod32, /* [in] hmodule of win32 dll */
218 DWORD dwReason /* [in] initialisation argument */
220 BOOL directionSL;
222 if (!strncmp(TD->magic, "SL01", 4))
224 directionSL = TRUE;
226 TRACE("SL01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
227 module32, (DWORD)TD, module16, thunkfun16, dwReason);
229 else if (!strncmp(TD->magic, "LS01", 4))
231 directionSL = FALSE;
233 TRACE("LS01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
234 module32, (DWORD)TD, module16, thunkfun16, dwReason);
236 else
238 ERR("Invalid magic %c%c%c%c\n",
239 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
240 return 0;
243 switch (dwReason)
245 case DLL_PROCESS_ATTACH:
247 struct ThunkDataCommon *TD16;
248 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
249 return 0;
251 if (directionSL)
253 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
254 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
255 struct SLTargetDB *tdb;
257 if (SL16->fpData == NULL)
259 ERR("ThunkConnect16 was not called!\n");
260 return 0;
263 SL32->data = SL16->fpData;
265 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
266 tdb->process = GetCurrentProcessId();
267 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
269 tdb->next = SL32->data->targetDB; /* FIXME: not thread-safe! */
270 SL32->data->targetDB = tdb;
272 TRACE("Process %08lx allocated TargetDB entry for ThunkDataSL %08lx\n",
273 GetCurrentProcessId(), (DWORD)SL32->data);
275 else
277 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
278 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
280 LS32->targetTable = MapSL(LS16->targetTable);
282 /* write QT_Thunk and FT_Prolog stubs */
283 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk, LS32->targetTable);
284 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
286 break;
289 case DLL_PROCESS_DETACH:
290 /* FIXME: cleanup */
291 break;
294 return 1;
297 /**********************************************************************
298 * QT_Thunk (KERNEL32.@)
300 * The target address is in EDX.
301 * The 16 bit arguments start at ESP.
302 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
303 * [ok]
305 void WINAPI QT_Thunk( CONTEXT86 *context )
307 CONTEXT86 context16;
308 DWORD argsize;
310 memcpy(&context16,context,sizeof(context16));
312 context16.SegCs = HIWORD(context->Edx);
313 context16.Eip = LOWORD(context->Edx);
314 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
315 + (WORD)&((STACK16FRAME*)0)->bp;
317 argsize = context->Ebp-context->Esp-0x40;
319 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
320 (LPBYTE)context->Esp, argsize );
322 wine_call_to_16_regs_short( &context16, argsize );
323 context->Eax = context16.Eax;
324 context->Edx = context16.Edx;
325 context->Ecx = context16.Ecx;
327 context->Esp += LOWORD(context16.Esp) -
328 ( OFFSETOF( NtCurrentTeb()->cur_stack ) - argsize );
332 /**********************************************************************
333 * FT_Prolog (KERNEL32.@)
335 * The set of FT_... thunk routines is used instead of QT_Thunk,
336 * if structures have to be converted from 32-bit to 16-bit
337 * (change of member alignment, conversion of members).
339 * The thunk function (as created by the thunk compiler) calls
340 * FT_Prolog at the beginning, to set up a stack frame and
341 * allocate a 64 byte buffer on the stack.
342 * The input parameters (target address and some flags) are
343 * saved for later use by FT_Thunk.
345 * Input: EDX 16-bit target address (SEGPTR)
346 * CX bits 0..7 target number (in target table)
347 * bits 8..9 some flags (unclear???)
348 * bits 10..15 number of DWORD arguments
350 * Output: A new stackframe is created, and a 64 byte buffer
351 * allocated on the stack. The layout of the stack
352 * on return is as follows:
354 * (ebp+4) return address to caller of thunk function
355 * (ebp) old EBP
356 * (ebp-4) saved EBX register of caller
357 * (ebp-8) saved ESI register of caller
358 * (ebp-12) saved EDI register of caller
359 * (ebp-16) saved ECX register, containing flags
360 * (ebp-20) bitmap containing parameters that are to be converted
361 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
362 * filled in by the thunk code before calling FT_Thunk
363 * (ebp-24)
364 * ... (unclear)
365 * (ebp-44)
366 * (ebp-48) saved EAX register of caller (unclear, never restored???)
367 * (ebp-52) saved EDX register, containing 16-bit thunk target
368 * (ebp-56)
369 * ... (unclear)
370 * (ebp-64)
372 * ESP is EBP-64 after return.
376 void WINAPI FT_Prolog( CONTEXT86 *context )
378 /* Build stack frame */
379 stack32_push(context, context->Ebp);
380 context->Ebp = context->Esp;
382 /* Allocate 64-byte Thunk Buffer */
383 context->Esp -= 64;
384 memset((char *)context->Esp, '\0', 64);
386 /* Store Flags (ECX) and Target Address (EDX) */
387 /* Save other registers to be restored later */
388 *(DWORD *)(context->Ebp - 4) = context->Ebx;
389 *(DWORD *)(context->Ebp - 8) = context->Esi;
390 *(DWORD *)(context->Ebp - 12) = context->Edi;
391 *(DWORD *)(context->Ebp - 16) = context->Ecx;
393 *(DWORD *)(context->Ebp - 48) = context->Eax;
394 *(DWORD *)(context->Ebp - 52) = context->Edx;
397 /**********************************************************************
398 * FT_Thunk (KERNEL32.@)
400 * This routine performs the actual call to 16-bit code,
401 * similar to QT_Thunk. The differences are:
402 * - The call target is taken from the buffer created by FT_Prolog
403 * - Those arguments requested by the thunk code (by setting the
404 * corresponding bit in the bitmap at EBP-20) are converted
405 * from 32-bit pointers to segmented pointers (those pointers
406 * are guaranteed to point to structures copied to the stack
407 * by the thunk code, so we always use the 16-bit stack selector
408 * for those addresses).
410 * The bit #i of EBP-20 corresponds here to the DWORD starting at
411 * ESP+4 + 2*i.
413 * FIXME: It is unclear what happens if there are more than 32 WORDs
414 * of arguments, so that the single DWORD bitmap is no longer
415 * sufficient ...
418 void WINAPI FT_Thunk( CONTEXT86 *context )
420 DWORD mapESPrelative = *(DWORD *)(context->Ebp - 20);
421 DWORD callTarget = *(DWORD *)(context->Ebp - 52);
423 CONTEXT86 context16;
424 DWORD i, argsize;
425 LPBYTE newstack, oldstack;
427 memcpy(&context16,context,sizeof(context16));
429 context16.SegCs = HIWORD(callTarget);
430 context16.Eip = LOWORD(callTarget);
431 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
432 + (WORD)&((STACK16FRAME*)0)->bp;
434 argsize = context->Ebp-context->Esp-0x40;
435 newstack = (LPBYTE)CURRENT_STACK16 - argsize;
436 oldstack = (LPBYTE)context->Esp;
438 memcpy( newstack, oldstack, argsize );
440 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
441 if (mapESPrelative & (1 << i))
443 SEGPTR *arg = (SEGPTR *)(newstack + 2*i);
444 *arg = MAKESEGPTR(SELECTOROF(NtCurrentTeb()->cur_stack),
445 OFFSETOF(NtCurrentTeb()->cur_stack) - argsize
446 + (*(LPBYTE *)arg - oldstack));
449 wine_call_to_16_regs_short( &context16, argsize );
450 context->Eax = context16.Eax;
451 context->Edx = context16.Edx;
452 context->Ecx = context16.Ecx;
454 context->Esp += LOWORD(context16.Esp) -
455 ( OFFSETOF( NtCurrentTeb()->cur_stack ) - argsize );
457 /* Copy modified buffers back to 32-bit stack */
458 memcpy( oldstack, newstack, argsize );
461 /**********************************************************************
462 * FT_ExitNN (KERNEL32.218 - 232)
464 * One of the FT_ExitNN functions is called at the end of the thunk code.
465 * It removes the stack frame created by FT_Prolog, moves the function
466 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
467 * value, but the thunk code has moved it from EAX to EBX in the
468 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
469 * and perform a return to the CALLER of the thunk code (while removing
470 * the given number of arguments from the caller's stack).
473 static void FT_Exit(CONTEXT86 *context, int nPopArgs)
475 /* Return value is in EBX */
476 context->Eax = context->Ebx;
478 /* Restore EBX, ESI, and EDI registers */
479 context->Ebx = *(DWORD *)(context->Ebp - 4);
480 context->Esi = *(DWORD *)(context->Ebp - 8);
481 context->Edi = *(DWORD *)(context->Ebp - 12);
483 /* Clean up stack frame */
484 context->Esp = context->Ebp;
485 context->Ebp = stack32_pop(context);
487 /* Pop return address to CALLER of thunk code */
488 context->Eip = stack32_pop(context);
489 /* Remove arguments */
490 context->Esp += nPopArgs;
493 /***********************************************************************
494 * FT_Exit0 (KERNEL32.@)
496 void WINAPI FT_Exit0 (CONTEXT86 *context) { FT_Exit(context, 0); }
498 /***********************************************************************
499 * FT_Exit4 (KERNEL32.@)
501 void WINAPI FT_Exit4 (CONTEXT86 *context) { FT_Exit(context, 4); }
503 /***********************************************************************
504 * FT_Exit8 (KERNEL32.@)
506 void WINAPI FT_Exit8 (CONTEXT86 *context) { FT_Exit(context, 8); }
508 /***********************************************************************
509 * FT_Exit12 (KERNEL32.@)
511 void WINAPI FT_Exit12(CONTEXT86 *context) { FT_Exit(context, 12); }
513 /***********************************************************************
514 * FT_Exit16 (KERNEL32.@)
516 void WINAPI FT_Exit16(CONTEXT86 *context) { FT_Exit(context, 16); }
518 /***********************************************************************
519 * FT_Exit20 (KERNEL32.@)
521 void WINAPI FT_Exit20(CONTEXT86 *context) { FT_Exit(context, 20); }
523 /***********************************************************************
524 * FT_Exit24 (KERNEL32.@)
526 void WINAPI FT_Exit24(CONTEXT86 *context) { FT_Exit(context, 24); }
528 /***********************************************************************
529 * FT_Exit28 (KERNEL32.@)
531 void WINAPI FT_Exit28(CONTEXT86 *context) { FT_Exit(context, 28); }
533 /***********************************************************************
534 * FT_Exit32 (KERNEL32.@)
536 void WINAPI FT_Exit32(CONTEXT86 *context) { FT_Exit(context, 32); }
538 /***********************************************************************
539 * FT_Exit36 (KERNEL32.@)
541 void WINAPI FT_Exit36(CONTEXT86 *context) { FT_Exit(context, 36); }
543 /***********************************************************************
544 * FT_Exit40 (KERNEL32.@)
546 void WINAPI FT_Exit40(CONTEXT86 *context) { FT_Exit(context, 40); }
548 /***********************************************************************
549 * FT_Exit44 (KERNEL32.@)
551 void WINAPI FT_Exit44(CONTEXT86 *context) { FT_Exit(context, 44); }
553 /***********************************************************************
554 * FT_Exit48 (KERNEL32.@)
556 void WINAPI FT_Exit48(CONTEXT86 *context) { FT_Exit(context, 48); }
558 /***********************************************************************
559 * FT_Exit52 (KERNEL32.@)
561 void WINAPI FT_Exit52(CONTEXT86 *context) { FT_Exit(context, 52); }
563 /***********************************************************************
564 * FT_Exit56 (KERNEL32.@)
566 void WINAPI FT_Exit56(CONTEXT86 *context) { FT_Exit(context, 56); }
568 /***********************************************************************
569 * ThunkInitLS (KERNEL32.43)
570 * A thunkbuffer link routine
571 * The thunkbuf looks like:
573 * 00: DWORD length ? don't know exactly
574 * 04: SEGPTR ptr ? where does it point to?
575 * The pointer ptr is written into the first DWORD of 'thunk'.
576 * (probably correctly implemented)
577 * [ok probably]
578 * RETURNS
579 * segmented pointer to thunk?
581 DWORD WINAPI ThunkInitLS(
582 LPDWORD thunk, /* [in] win32 thunk */
583 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
584 DWORD len, /* [in] thkbuffer length */
585 LPCSTR dll16, /* [in] name of win16 dll */
586 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
588 LPDWORD addr;
590 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
591 return 0;
593 if (!addr[1])
594 return 0;
595 *(DWORD*)thunk = addr[1];
597 return addr[1];
600 /***********************************************************************
601 * Common32ThkLS (KERNEL32.45)
603 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
604 * style thunks. The basic difference is that the parameter conversion
605 * is done completely on the *16-bit* side here. Thus we do not call
606 * the 16-bit target directly, but call a common entry point instead.
607 * This entry function then calls the target according to the target
608 * number passed in the DI register.
610 * Input: EAX SEGPTR to the common 16-bit entry point
611 * CX offset in thunk table (target number * 4)
612 * DX error return value if execution fails (unclear???)
613 * EDX.HI number of DWORD parameters
615 * (Note that we need to move the thunk table offset from CX to DI !)
617 * The called 16-bit stub expects its stack to look like this:
618 * ...
619 * (esp+40) 32-bit arguments
620 * ...
621 * (esp+8) 32 byte of stack space available as buffer
622 * (esp) 8 byte return address for use with 0x66 lret
624 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
625 * and uses the EAX register to return a DWORD return value.
626 * Thus we need to use a special assembly glue routine
627 * (CallRegisterLongProc instead of CallRegisterShortProc).
629 * Finally, we return to the caller, popping the arguments off
630 * the stack. The number of arguments to be popped is returned
631 * in the BL register by the called 16-bit routine.
634 void WINAPI Common32ThkLS( CONTEXT86 *context )
636 CONTEXT86 context16;
637 DWORD argsize;
639 memcpy(&context16,context,sizeof(context16));
641 context16.Edi = LOWORD(context->Ecx);
642 context16.SegCs = HIWORD(context->Eax);
643 context16.Eip = LOWORD(context->Eax);
644 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
645 + (WORD)&((STACK16FRAME*)0)->bp;
647 argsize = HIWORD(context->Edx) * 4;
649 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
650 if (context->Edx == context->Eip)
651 argsize = 6 * 4;
653 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
654 (LPBYTE)context->Esp, argsize );
656 wine_call_to_16_regs_long(&context16, argsize + 32);
657 context->Eax = context16.Eax;
659 /* Clean up caller's stack frame */
660 context->Esp += BL_reg(&context16);
663 /***********************************************************************
664 * OT_32ThkLSF (KERNEL32.40)
666 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
667 * argument processing is done on both the 32-bit and the 16-bit side:
668 * The 32-bit side prepares arguments, copying them onto the stack.
670 * When this routine is called, the first word on the stack is the
671 * number of argument bytes prepared by the 32-bit code, and EDX
672 * contains the 16-bit target address.
674 * The called 16-bit routine is another relaycode, doing further
675 * argument processing and then calling the real 16-bit target
676 * whose address is stored at [bp-04].
678 * The call proceeds using a normal CallRegisterShortProc.
679 * After return from the 16-bit relaycode, the arguments need
680 * to be copied *back* to the 32-bit stack, since the 32-bit
681 * relaycode processes output parameters.
683 * Note that we copy twice the number of arguments, since some of the
684 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
685 * arguments of the caller!
687 * (Note that this function seems only to be used for
688 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
690 void WINAPI OT_32ThkLSF( CONTEXT86 *context )
692 CONTEXT86 context16;
693 DWORD argsize;
695 memcpy(&context16,context,sizeof(context16));
697 context16.SegCs = HIWORD(context->Edx);
698 context16.Eip = LOWORD(context->Edx);
699 context16.Ebp = OFFSETOF( NtCurrentTeb()->cur_stack )
700 + (WORD)&((STACK16FRAME*)0)->bp;
702 argsize = 2 * *(WORD *)context->Esp + 2;
704 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
705 (LPBYTE)context->Esp, argsize );
707 wine_call_to_16_regs_short(&context16, argsize);
708 context->Eax = context16.Eax;
709 context->Edx = context16.Edx;
711 /* Copy modified buffers back to 32-bit stack */
712 memcpy( (LPBYTE)context->Esp,
713 (LPBYTE)CURRENT_STACK16 - argsize, argsize );
715 context->Esp += LOWORD(context16.Esp) -
716 ( OFFSETOF( NtCurrentTeb()->cur_stack ) - argsize );
719 /***********************************************************************
720 * ThunkInitLSF (KERNEL32.41)
721 * A thunk setup routine.
722 * Expects a pointer to a preinitialized thunkbuffer in the first argument
723 * looking like:
724 * 00..03: unknown (pointer, check _41, _43, _46)
725 * 04: EB1E jmp +0x20
727 * 06..23: unknown (space for replacement code, check .90)
729 * 24:>E800000000 call offset 29
730 * 29:>58 pop eax ( target of call )
731 * 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
732 * 2F: BAxxxxxxxx mov edx,xxxxxxxx
733 * 34: 68yyyyyyyy push KERNEL32.90
734 * 39: C3 ret
736 * 3A: EB1E jmp +0x20
737 * 3E ... 59: unknown (space for replacement code?)
738 * 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
739 * 5F: 5A pop edx
740 * 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
741 * 66: 52 push edx
742 * 67: 68xxxxxxxx push xxxxxxxx
743 * 6C: 68yyyyyyyy push KERNEL32.89
744 * 71: C3 ret
745 * 72: end?
746 * This function checks if the code is there, and replaces the yyyyyyyy entries
747 * by the functionpointers.
748 * The thunkbuf looks like:
750 * 00: DWORD length ? don't know exactly
751 * 04: SEGPTR ptr ? where does it point to?
752 * The segpointer ptr is written into the first DWORD of 'thunk'.
753 * [ok probably]
754 * RETURNS
755 * unclear, pointer to win16 thkbuffer?
757 LPVOID WINAPI ThunkInitLSF(
758 LPBYTE thunk, /* [in] win32 thunk */
759 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
760 DWORD len, /* [in] length of thkbuffer */
761 LPCSTR dll16, /* [in] name of win16 dll */
762 LPCSTR dll32 /* [in] name of win32 dll */
764 HMODULE hkrnl32 = GetModuleHandleA("KERNEL32");
765 LPDWORD addr,addr2;
767 /* FIXME: add checks for valid code ... */
768 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
769 *(DWORD*)(thunk+0x35) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)90);
770 *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)89);
773 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
774 return 0;
776 addr2 = MapSL(addr[1]);
777 if (HIWORD(addr2))
778 *(DWORD*)thunk = (DWORD)addr2;
780 return addr2;
783 /***********************************************************************
784 * FT_PrologPrime (KERNEL32.89)
786 * This function is called from the relay code installed by
787 * ThunkInitLSF. It replaces the location from where it was
788 * called by a standard FT_Prolog call stub (which is 'primed'
789 * by inserting the correct target table pointer).
790 * Finally, it calls that stub.
792 * Input: ECX target number + flags (passed through to FT_Prolog)
793 * (ESP) offset of location where target table pointer
794 * is stored, relative to the start of the relay code
795 * (ESP+4) pointer to start of relay code
796 * (this is where the FT_Prolog call stub gets written to)
798 * Note: The two DWORD arguments get popped off the stack.
801 void WINAPI FT_PrologPrime( CONTEXT86 *context )
803 DWORD targetTableOffset;
804 LPBYTE relayCode;
806 /* Compensate for the fact that the Wine register relay code thought
807 we were being called, although we were in fact jumped to */
808 context->Esp -= 4;
810 /* Write FT_Prolog call stub */
811 targetTableOffset = stack32_pop(context);
812 relayCode = (LPBYTE)stack32_pop(context);
813 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
815 /* Jump to the call stub just created */
816 context->Eip = (DWORD)relayCode;
819 /***********************************************************************
820 * QT_ThunkPrime (KERNEL32.90)
822 * This function corresponds to FT_PrologPrime, but installs a
823 * call stub for QT_Thunk instead.
825 * Input: (EBP-4) target number (passed through to QT_Thunk)
826 * EDX target table pointer location offset
827 * EAX start of relay code
830 void WINAPI QT_ThunkPrime( CONTEXT86 *context )
832 DWORD targetTableOffset;
833 LPBYTE relayCode;
835 /* Compensate for the fact that the Wine register relay code thought
836 we were being called, although we were in fact jumped to */
837 context->Esp -= 4;
839 /* Write QT_Thunk call stub */
840 targetTableOffset = context->Edx;
841 relayCode = (LPBYTE)context->Eax;
842 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
844 /* Jump to the call stub just created */
845 context->Eip = (DWORD)relayCode;
848 /***********************************************************************
849 * ThunkInitSL (KERNEL32.46)
850 * Another thunkbuf link routine.
851 * The start of the thunkbuf looks like this:
852 * 00: DWORD length
853 * 04: SEGPTR address for thunkbuffer pointer
854 * [ok probably]
856 VOID WINAPI ThunkInitSL(
857 LPBYTE thunk, /* [in] start of thunkbuffer */
858 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
859 DWORD len, /* [in] length of thunkbuffer */
860 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
861 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
863 LPDWORD addr;
865 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
866 return;
868 *(DWORD*)MapSL(addr[1]) = (DWORD)thunk;
871 /**********************************************************************
872 * SSInit (KERNEL.700)
873 * RETURNS
874 * TRUE for success.
876 BOOL WINAPI SSInit16()
878 return TRUE;
881 /**********************************************************************
882 * SSOnBigStack (KERNEL32.87)
883 * Check if thunking is initialized (ss selector set up etc.)
884 * We do that differently, so just return TRUE.
885 * [ok]
886 * RETURNS
887 * TRUE for success.
889 BOOL WINAPI SSOnBigStack()
891 TRACE("Yes, thunking is initialized\n");
892 return TRUE;
895 /**********************************************************************
896 * SSConfirmSmallStack (KERNEL.704)
898 * Abort if not on small stack.
900 * This must be a register routine as it has to preserve *all* registers.
902 void WINAPI SSConfirmSmallStack( CONTEXT86 *context )
904 /* We are always on the small stack while in 16-bit code ... */
907 /**********************************************************************
908 * SSCall (KERNEL32.88)
909 * One of the real thunking functions. This one seems to be for 32<->32
910 * thunks. It should probably be capable of crossing processboundaries.
912 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
913 * [ok]
915 DWORD WINAPIV SSCall(
916 DWORD nr, /* [in] number of argument bytes */
917 DWORD flags, /* [in] FIXME: flags ? */
918 FARPROC fun, /* [in] function to call */
919 ... /* [in/out] arguments */
921 DWORD i,ret;
922 DWORD *args = ((DWORD *)&fun) + 1;
924 if(TRACE_ON(thunk))
926 DPRINTF("(%ld,0x%08lx,%p,[",nr,flags,fun);
927 for (i=0;i<nr/4;i++)
928 DPRINTF("0x%08lx,",args[i]);
929 DPRINTF("])\n");
931 switch (nr) {
932 case 0: ret = fun();
933 break;
934 case 4: ret = fun(args[0]);
935 break;
936 case 8: ret = fun(args[0],args[1]);
937 break;
938 case 12: ret = fun(args[0],args[1],args[2]);
939 break;
940 case 16: ret = fun(args[0],args[1],args[2],args[3]);
941 break;
942 case 20: ret = fun(args[0],args[1],args[2],args[3],args[4]);
943 break;
944 case 24: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
945 break;
946 case 28: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
947 break;
948 case 32: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
949 break;
950 case 36: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
951 break;
952 case 40: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
953 break;
954 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]);
955 break;
956 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]);
957 break;
958 default:
959 WARN("Unsupported nr of arguments, %ld\n",nr);
960 ret = 0;
961 break;
964 TRACE(" returning %ld ...\n",ret);
965 return ret;
968 /**********************************************************************
969 * W32S_BackTo32 (KERNEL32.51)
971 void WINAPI W32S_BackTo32( CONTEXT86 *context )
973 LPDWORD stack = (LPDWORD)context->Esp;
974 FARPROC proc = (FARPROC)context->Eip;
976 context->Eax = proc( stack[1], stack[2], stack[3], stack[4], stack[5],
977 stack[6], stack[7], stack[8], stack[9], stack[10] );
979 context->Eip = stack32_pop(context);
982 /**********************************************************************
983 * AllocSLCallback (KERNEL32.@)
985 * Win95 uses some structchains for callbacks. It allocates them
986 * in blocks of 100 entries, size 32 bytes each, layout:
987 * blockstart:
988 * 0: PTR nextblockstart
989 * 4: entry *first;
990 * 8: WORD sel ( start points to blockstart)
991 * A: WORD unknown
992 * 100xentry:
993 * 00..17: Code
994 * 18: PDB *owning_process;
995 * 1C: PTR blockstart
997 * We ignore this for now. (Just a note for further developers)
998 * FIXME: use this method, so we don't waste selectors...
1000 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
1001 * the 0x66 prefix switches from word->long registers.
1003 * 665A pop edx
1004 * 6668x arg2 x pushl <arg2>
1005 * 6652 push edx
1006 * EAx arg1 x jmpf <arg1>
1008 * returns the startaddress of this thunk.
1010 * Note, that they look very similair to the ones allocates by THUNK_Alloc.
1011 * RETURNS
1012 * segmented pointer to the start of the thunk
1014 DWORD WINAPI
1015 AllocSLCallback(
1016 DWORD finalizer, /* [in] finalizer function */
1017 DWORD callback /* [in] callback function */
1019 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
1020 WORD sel;
1022 x=thunk;
1023 *x++=0x66;*x++=0x5a; /* popl edx */
1024 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
1025 *x++=0x66;*x++=0x52; /* pushl edx */
1026 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
1028 *(DWORD*)(thunk+18) = GetCurrentProcessId();
1030 sel = SELECTOR_AllocBlock( thunk, 32, WINE_LDT_FLAGS_CODE );
1031 return (sel<<16)|0;
1034 /**********************************************************************
1035 * FreeSLCallback (KERNEL32.@)
1036 * Frees the specified 16->32 callback
1038 void WINAPI
1039 FreeSLCallback(
1040 DWORD x /* [in] 16 bit callback (segmented pointer?) */
1042 FIXME("(0x%08lx): stub\n",x);
1046 /**********************************************************************
1047 * GetTEBSelectorFS (KERNEL.475)
1048 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1050 void WINAPI GetTEBSelectorFS16(void)
1052 CURRENT_STACK16->fs = __get_fs();
1055 /**********************************************************************
1056 * IsPeFormat (KERNEL.431)
1057 * Checks the passed filename if it is a PE format executeable
1058 * RETURNS
1059 * TRUE, if it is.
1060 * FALSE if not.
1062 BOOL16 WINAPI IsPeFormat16(
1063 LPSTR fn, /* [in] filename to executeable */
1064 HFILE16 hf16 /* [in] open file, if filename is NULL */
1066 BOOL ret = FALSE;
1067 IMAGE_DOS_HEADER mzh;
1068 OFSTRUCT ofs;
1069 DWORD xmagic;
1071 if (fn) hf16 = OpenFile16(fn,&ofs,OF_READ);
1072 if (hf16 == HFILE_ERROR16) return FALSE;
1073 _llseek16(hf16,0,SEEK_SET);
1074 if (sizeof(mzh)!=_lread16(hf16,&mzh,sizeof(mzh))) goto done;
1075 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) goto done;
1076 _llseek16(hf16,mzh.e_lfanew,SEEK_SET);
1077 if (sizeof(DWORD)!=_lread16(hf16,&xmagic,sizeof(DWORD))) goto done;
1078 ret = (xmagic == IMAGE_NT_SIGNATURE);
1079 done:
1080 _lclose16(hf16);
1081 return ret;
1085 /***********************************************************************
1086 * K32Thk1632Prolog (KERNEL32.@)
1088 void WINAPI K32Thk1632Prolog( CONTEXT86 *context )
1090 LPBYTE code = (LPBYTE)context->Eip - 5;
1092 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1093 of 16->32 thunks instead of using one of the standard methods!
1094 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1095 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1096 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1097 bypassed, which means it will crash the next time the 32-bit OLE
1098 code thunks down again to 16-bit (this *will* happen!).
1100 The following hack tries to recognize this situation.
1101 This is possible since the called stubs in OLECLI32/OLESVR32 all
1102 look exactly the same:
1103 00 E8xxxxxxxx call K32Thk1632Prolog
1104 05 FF55FC call [ebp-04]
1105 08 E8xxxxxxxx call K32Thk1632Epilog
1106 0D 66CB retf
1108 If we recognize this situation, we try to simulate the actions
1109 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1110 to our 32-bit stack, creating a proper STACK16FRAME and
1111 updating cur_stack. */
1113 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1114 && code[13] == 0x66 && code[14] == 0xCB)
1116 WORD stackSel = NtCurrentTeb()->stack_sel;
1117 DWORD stackBase = GetSelectorBase(stackSel);
1119 DWORD argSize = context->Ebp - context->Esp;
1120 char *stack16 = (char *)context->Esp - 4;
1121 char *stack32 = (char *)NtCurrentTeb()->cur_stack - argSize;
1122 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1124 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1125 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1127 memset(frame16, '\0', sizeof(STACK16FRAME));
1128 frame16->frame32 = (STACK32FRAME *)NtCurrentTeb()->cur_stack;
1129 frame16->ebp = context->Ebp;
1131 memcpy(stack32, stack16, argSize);
1132 NtCurrentTeb()->cur_stack = MAKESEGPTR(stackSel, (DWORD)frame16 - stackBase);
1134 context->Esp = (DWORD)stack32 + 4;
1135 context->Ebp = context->Esp + argSize;
1137 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1138 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1141 /* entry_point is never used again once the entry point has
1142 been called. Thus we re-use it to hold the Win16Lock count */
1143 ReleaseThunkLock(&CURRENT_STACK16->entry_point);
1146 /***********************************************************************
1147 * K32Thk1632Epilog (KERNEL32.@)
1149 void WINAPI K32Thk1632Epilog( CONTEXT86 *context )
1151 LPBYTE code = (LPBYTE)context->Eip - 13;
1153 RestoreThunkLock(CURRENT_STACK16->entry_point);
1155 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1157 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1158 && code[13] == 0x66 && code[14] == 0xCB)
1160 STACK16FRAME *frame16 = MapSL(NtCurrentTeb()->cur_stack);
1161 char *stack16 = (char *)(frame16 + 1);
1162 DWORD argSize = frame16->ebp - (DWORD)stack16;
1163 char *stack32 = (char *)frame16->frame32 - argSize;
1165 DWORD nArgsPopped = context->Esp - (DWORD)stack32;
1167 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1168 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1170 NtCurrentTeb()->cur_stack = (DWORD)frame16->frame32;
1172 context->Esp = (DWORD)stack16 + nArgsPopped;
1173 context->Ebp = frame16->ebp;
1175 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1176 context->Ebp, context->Esp, NtCurrentTeb()->cur_stack);
1180 /*********************************************************************
1181 * PK16FNF [KERNEL32.91]
1183 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1184 * string buffer with the 8.3 filename of a recently loaded 16-bit
1185 * module. It is unknown exactly what modules trigger this
1186 * mechanism or what purpose this serves. Win98 Explorer (and
1187 * probably also Win95 with IE 4 shell integration) calls this
1188 * several times during initialization.
1190 * FIXME: find out what this really does and make it work.
1192 void WINAPI PK16FNF(LPSTR strPtr)
1194 FIXME("(%p): stub\n", strPtr);
1196 /* fill in a fake filename that'll be easy to recognize */
1197 strcpy(strPtr, "WINESTUB.FIX");
1200 /***********************************************************************
1201 * 16->32 Flat Thunk routines:
1204 /***********************************************************************
1205 * ThunkConnect16 (KERNEL.651)
1206 * Connects a 32bit and a 16bit thunkbuffer.
1208 UINT WINAPI ThunkConnect16(
1209 LPSTR module16, /* [in] name of win16 dll */
1210 LPSTR module32, /* [in] name of win32 dll */
1211 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
1212 DWORD dwReason, /* [in] initialisation argument */
1213 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
1214 LPSTR thunkfun32, /* [in] win32 thunkfunction */
1215 WORD cs /* [in] CS of win16 dll */
1217 BOOL directionSL;
1219 if (!strncmp(TD->magic, "SL01", 4))
1221 directionSL = TRUE;
1223 TRACE("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
1224 module16, (DWORD)TD, module32, thunkfun32, dwReason);
1226 else if (!strncmp(TD->magic, "LS01", 4))
1228 directionSL = FALSE;
1230 TRACE("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
1231 module16, (DWORD)TD, module32, thunkfun32, dwReason);
1233 else
1235 ERR("Invalid magic %c%c%c%c\n",
1236 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
1237 return 0;
1240 switch (dwReason)
1242 case DLL_PROCESS_ATTACH:
1243 if (directionSL)
1245 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
1246 struct ThunkDataSL *SL = SL16->fpData;
1248 if (SL == NULL)
1250 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
1252 SL->common = SL16->common;
1253 SL->flags1 = SL16->flags1;
1254 SL->flags2 = SL16->flags2;
1256 SL->apiDB = MapSL(SL16->apiDatabase);
1257 SL->targetDB = NULL;
1259 lstrcpynA(SL->pszDll16, module16, 255);
1260 lstrcpynA(SL->pszDll32, module32, 255);
1262 /* We should create a SEGPTR to the ThunkDataSL,
1263 but since the contents are not in the original format,
1264 any access to this by 16-bit code would crash anyway. */
1265 SL16->spData = 0;
1266 SL16->fpData = SL;
1270 if (SL->flags2 & 0x80000000)
1272 TRACE("Preloading 32-bit library\n");
1273 LoadLibraryA(module32);
1276 else
1278 /* nothing to do */
1280 break;
1282 case DLL_PROCESS_DETACH:
1283 /* FIXME: cleanup */
1284 break;
1287 return 1;
1291 /***********************************************************************
1292 * C16ThkSL (KERNEL.630)
1295 void WINAPI C16ThkSL(CONTEXT86 *context)
1297 LPBYTE stub = MapSL(context->Eax), x = stub;
1298 WORD cs = __get_cs();
1299 WORD ds = __get_ds();
1301 /* We produce the following code:
1303 * mov ax, __FLATDS
1304 * mov es, ax
1305 * movzx ecx, cx
1306 * mov edx, es:[ecx + $EDX]
1307 * push bp
1308 * push edx
1309 * push dx
1310 * push edx
1311 * call __FLATCS:__wine_call_from_16_thunk
1314 *x++ = 0xB8; *((WORD *)x)++ = ds;
1315 *x++ = 0x8E; *x++ = 0xC0;
1316 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1317 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1318 *x++ = 0x91; *((DWORD *)x)++ = context->Edx;
1320 *x++ = 0x55;
1321 *x++ = 0x66; *x++ = 0x52;
1322 *x++ = 0x52;
1323 *x++ = 0x66; *x++ = 0x52;
1324 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)__wine_call_from_16_thunk;
1325 *((WORD *)x)++ = cs;
1327 /* Jump to the stub code just created */
1328 context->Eip = LOWORD(context->Eax);
1329 context->SegCs = HIWORD(context->Eax);
1331 /* Since C16ThkSL got called by a jmp, we need to leave the
1332 original return address on the stack */
1333 context->Esp -= 4;
1336 /***********************************************************************
1337 * C16ThkSL01 (KERNEL.631)
1340 void WINAPI C16ThkSL01(CONTEXT86 *context)
1342 LPBYTE stub = MapSL(context->Eax), x = stub;
1344 if (stub)
1346 struct ThunkDataSL16 *SL16 = MapSL(context->Edx);
1347 struct ThunkDataSL *td = SL16->fpData;
1349 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), (LPCSTR)631);
1350 WORD cs = __get_cs();
1352 if (!td)
1354 ERR("ThunkConnect16 was not called!\n");
1355 return;
1358 TRACE("Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
1361 /* We produce the following code:
1363 * xor eax, eax
1364 * mov edx, $td
1365 * call C16ThkSL01
1366 * push bp
1367 * push edx
1368 * push dx
1369 * push edx
1370 * call __FLATCS:__wine_call_from_16_thunk
1373 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1374 *x++ = 0x66; *x++ = 0xBA; *((DWORD *)x)++ = (DWORD)td;
1375 *x++ = 0x9A; *((DWORD *)x)++ = procAddress;
1377 *x++ = 0x55;
1378 *x++ = 0x66; *x++ = 0x52;
1379 *x++ = 0x52;
1380 *x++ = 0x66; *x++ = 0x52;
1381 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)__wine_call_from_16_thunk;
1382 *((WORD *)x)++ = cs;
1384 /* Jump to the stub code just created */
1385 context->Eip = LOWORD(context->Eax);
1386 context->SegCs = HIWORD(context->Eax);
1388 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1389 orginal return address on the stack */
1390 context->Esp -= 4;
1392 else
1394 struct ThunkDataSL *td = (struct ThunkDataSL *)context->Edx;
1395 DWORD targetNr = CX_reg(context) / 4;
1396 struct SLTargetDB *tdb;
1398 TRACE("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1399 GetCurrentProcessId(), targetNr, (DWORD)td);
1401 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1402 if (tdb->process == GetCurrentProcessId())
1403 break;
1405 if (!tdb)
1407 TRACE("Loading 32-bit library %s\n", td->pszDll32);
1408 LoadLibraryA(td->pszDll32);
1410 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1411 if (tdb->process == GetCurrentProcessId())
1412 break;
1415 if (tdb)
1417 context->Edx = tdb->targetTable[targetNr];
1419 TRACE("Call target is %08lx\n", context->Edx);
1421 else
1423 WORD *stack = MapSL( MAKESEGPTR(context->SegSs, LOWORD(context->Esp)) );
1424 DX_reg(context) = HIWORD(td->apiDB[targetNr].errorReturnValue);
1425 AX_reg(context) = LOWORD(td->apiDB[targetNr].errorReturnValue);
1426 context->Eip = stack[2];
1427 context->SegCs = stack[3];
1428 context->Esp += td->apiDB[targetNr].nrArgBytes + 4;
1430 ERR("Process %08lx did not ThunkConnect32 %s to %s\n",
1431 GetCurrentProcessId(), td->pszDll32, td->pszDll16);
1437 /***********************************************************************
1438 * 16<->32 Thunklet/Callback API:
1441 #include "pshpack1.h"
1442 typedef struct _THUNKLET
1444 BYTE prefix_target;
1445 BYTE pushl_target;
1446 DWORD target;
1448 BYTE prefix_relay;
1449 BYTE pushl_relay;
1450 DWORD relay;
1452 BYTE jmp_glue;
1453 DWORD glue;
1455 BYTE type;
1456 HINSTANCE16 owner;
1457 struct _THUNKLET *next;
1458 } THUNKLET;
1459 #include "poppack.h"
1461 #define THUNKLET_TYPE_LS 1
1462 #define THUNKLET_TYPE_SL 2
1464 static HANDLE ThunkletHeap = 0;
1465 static WORD ThunkletCodeSel;
1466 static THUNKLET *ThunkletAnchor = NULL;
1468 static FARPROC ThunkletSysthunkGlueLS = 0;
1469 static SEGPTR ThunkletSysthunkGlueSL = 0;
1471 static FARPROC ThunkletCallbackGlueLS = 0;
1472 static SEGPTR ThunkletCallbackGlueSL = 0;
1475 /* map a thunk allocated on ThunkletHeap to a 16-bit pointer */
1476 inline static SEGPTR get_segptr( void *thunk )
1478 if (!thunk) return 0;
1479 return MAKESEGPTR( ThunkletCodeSel, (char *)thunk - (char *)ThunkletHeap );
1482 /***********************************************************************
1483 * THUNK_Init
1485 static BOOL THUNK_Init(void)
1487 LPBYTE thunk;
1489 ThunkletHeap = HeapCreate( 0, 0x10000, 0x10000 );
1490 if (!ThunkletHeap) return FALSE;
1492 ThunkletCodeSel = SELECTOR_AllocBlock( (void *)ThunkletHeap, 0x10000, WINE_LDT_FLAGS_CODE );
1494 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1495 if (!thunk) return FALSE;
1497 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1498 *thunk++ = 0x58; /* popl eax */
1499 *thunk++ = 0xC3; /* ret */
1501 ThunkletSysthunkGlueSL = get_segptr( thunk );
1502 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1503 *thunk++ = 0xCB; /* lret */
1505 return TRUE;
1508 /***********************************************************************
1509 * SetThunkletCallbackGlue (KERNEL.560)
1511 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1513 ThunkletCallbackGlueLS = glueLS;
1514 ThunkletCallbackGlueSL = glueSL;
1518 /***********************************************************************
1519 * THUNK_FindThunklet
1521 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1522 DWORD glue, BYTE type )
1524 THUNKLET *thunk;
1526 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1527 if ( thunk->type == type
1528 && thunk->target == target
1529 && thunk->relay == relay
1530 && ( type == THUNKLET_TYPE_LS ?
1531 ( thunk->glue == glue - (DWORD)&thunk->type )
1532 : ( thunk->glue == glue ) ) )
1533 return thunk;
1535 return NULL;
1538 /***********************************************************************
1539 * THUNK_AllocLSThunklet
1541 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1542 FARPROC glue, HTASK16 owner )
1544 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1545 THUNKLET_TYPE_LS );
1546 if (!thunk)
1548 TDB *pTask = TASK_GetPtr( owner );
1550 if (!ThunkletHeap) THUNK_Init();
1551 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1552 return 0;
1554 thunk->prefix_target = thunk->prefix_relay = 0x90;
1555 thunk->pushl_target = thunk->pushl_relay = 0x68;
1556 thunk->jmp_glue = 0xE9;
1558 thunk->target = (DWORD)target;
1559 thunk->relay = (DWORD)relay;
1560 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1562 thunk->type = THUNKLET_TYPE_LS;
1563 thunk->owner = pTask? pTask->hInstance : 0;
1565 thunk->next = ThunkletAnchor;
1566 ThunkletAnchor = thunk;
1569 return (FARPROC)thunk;
1572 /***********************************************************************
1573 * THUNK_AllocSLThunklet
1575 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1576 SEGPTR glue, HTASK16 owner )
1578 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1579 THUNKLET_TYPE_SL );
1580 if (!thunk)
1582 TDB *pTask = TASK_GetPtr( owner );
1584 if (!ThunkletHeap) THUNK_Init();
1585 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1586 return 0;
1588 thunk->prefix_target = thunk->prefix_relay = 0x66;
1589 thunk->pushl_target = thunk->pushl_relay = 0x68;
1590 thunk->jmp_glue = 0xEA;
1592 thunk->target = (DWORD)target;
1593 thunk->relay = (DWORD)relay;
1594 thunk->glue = (DWORD)glue;
1596 thunk->type = THUNKLET_TYPE_SL;
1597 thunk->owner = pTask? pTask->hInstance : 0;
1599 thunk->next = ThunkletAnchor;
1600 ThunkletAnchor = thunk;
1603 return get_segptr( thunk );
1606 /**********************************************************************
1607 * IsLSThunklet
1609 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
1611 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1612 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1613 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1616 /**********************************************************************
1617 * IsSLThunklet (KERNEL.612)
1619 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1621 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1622 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1623 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1628 /***********************************************************************
1629 * AllocLSThunkletSysthunk (KERNEL.607)
1631 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1632 FARPROC relay, DWORD dummy )
1634 if (!ThunkletSysthunkGlueLS) THUNK_Init();
1635 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1636 ThunkletSysthunkGlueLS, GetCurrentTask() );
1639 /***********************************************************************
1640 * AllocSLThunkletSysthunk (KERNEL.608)
1642 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1643 SEGPTR relay, DWORD dummy )
1645 if (!ThunkletSysthunkGlueSL) THUNK_Init();
1646 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1647 ThunkletSysthunkGlueSL, GetCurrentTask() );
1651 /***********************************************************************
1652 * AllocLSThunkletCallbackEx (KERNEL.567)
1654 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1655 DWORD relay, HTASK16 task )
1657 THUNKLET *thunk = MapSL( target );
1658 if ( !thunk ) return NULL;
1660 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1661 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1662 return (FARPROC)thunk->target;
1664 return THUNK_AllocLSThunklet( target, relay,
1665 ThunkletCallbackGlueLS, task );
1668 /***********************************************************************
1669 * AllocSLThunkletCallbackEx (KERNEL.568)
1671 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1672 DWORD relay, HTASK16 task )
1674 THUNKLET *thunk = (THUNKLET *)target;
1675 if ( !thunk ) return 0;
1677 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1678 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1679 return (SEGPTR)thunk->target;
1681 return THUNK_AllocSLThunklet( target, relay,
1682 ThunkletCallbackGlueSL, task );
1685 /***********************************************************************
1686 * AllocLSThunkletCallback (KERNEL.561)
1687 * AllocLSThunkletCallback_dup (KERNEL.606)
1689 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1691 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1694 /***********************************************************************
1695 * AllocSLThunkletCallback (KERNEL.562)
1696 * AllocSLThunkletCallback_dup (KERNEL.605)
1698 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1700 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1703 /***********************************************************************
1704 * FindLSThunkletCallback (KERNEL.563)
1705 * FindLSThunkletCallback_dup (KERNEL.609)
1707 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1709 THUNKLET *thunk = MapSL( target );
1710 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1711 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1712 return (FARPROC)thunk->target;
1714 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1715 (DWORD)ThunkletCallbackGlueLS,
1716 THUNKLET_TYPE_LS );
1717 return (FARPROC)thunk;
1720 /***********************************************************************
1721 * FindSLThunkletCallback (KERNEL.564)
1722 * FindSLThunkletCallback_dup (KERNEL.610)
1724 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1726 THUNKLET *thunk = (THUNKLET *)target;
1727 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1728 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1729 return (SEGPTR)thunk->target;
1731 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1732 (DWORD)ThunkletCallbackGlueSL,
1733 THUNKLET_TYPE_SL );
1734 return get_segptr( thunk );
1738 /***********************************************************************
1739 * FreeThunklet (KERNEL.611)
1741 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1743 return FALSE;
1747 /***********************************************************************
1748 * Callback Client API
1751 #define N_CBC_FIXED 20
1752 #define N_CBC_VARIABLE 10
1753 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1755 static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
1756 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1758 /***********************************************************************
1759 * RegisterCBClient (KERNEL.619)
1761 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1762 SEGPTR relay16, FARPROC *relay32 )
1764 /* Search for free Callback ID */
1765 if ( wCBCId == -1 )
1766 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1767 if ( !CBClientRelay16[ wCBCId ] )
1768 break;
1770 /* Register Callback ID */
1771 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1773 CBClientRelay16[ wCBCId ] = relay16;
1774 CBClientRelay32[ wCBCId ] = relay32;
1776 else
1777 wCBCId = 0;
1779 return wCBCId;
1782 /***********************************************************************
1783 * UnRegisterCBClient (KERNEL.622)
1785 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1786 SEGPTR relay16, FARPROC *relay32 )
1788 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1789 && CBClientRelay16[ wCBCId ] == relay16
1790 && CBClientRelay32[ wCBCId ] == relay32 )
1792 CBClientRelay16[ wCBCId ] = 0;
1793 CBClientRelay32[ wCBCId ] = 0;
1795 else
1796 wCBCId = 0;
1798 return wCBCId;
1802 /***********************************************************************
1803 * InitCBClient (KERNEL.623)
1805 void WINAPI InitCBClient16( FARPROC glueLS )
1807 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1808 SEGPTR glueSL = (SEGPTR)GetProcAddress16( kernel, (LPCSTR)604 );
1810 SetThunkletCallbackGlue16( glueLS, glueSL );
1813 /***********************************************************************
1814 * CBClientGlueSL (KERNEL.604)
1816 void WINAPI CBClientGlueSL( CONTEXT86 *context )
1818 /* Create stack frame */
1819 SEGPTR stackSeg = stack16_push( 12 );
1820 LPWORD stackLin = MapSL( stackSeg );
1821 SEGPTR glue, *glueTab;
1823 stackLin[3] = BP_reg( context );
1824 stackLin[2] = SI_reg( context );
1825 stackLin[1] = DI_reg( context );
1826 stackLin[0] = context->SegDs;
1828 context->Ebp = OFFSETOF( stackSeg ) + 6;
1829 context->Esp = OFFSETOF( stackSeg ) - 4;
1830 context->SegGs = 0;
1832 /* Jump to 16-bit relay code */
1833 glueTab = MapSL( CBClientRelay16[ stackLin[5] ] );
1834 glue = glueTab[ stackLin[4] ];
1835 context->SegCs = SELECTOROF( glue );
1836 context->Eip = OFFSETOF ( glue );
1839 /***********************************************************************
1840 * CBClientThunkSL (KERNEL.620)
1842 extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi );
1843 void WINAPI CBClientThunkSL( CONTEXT86 *context )
1845 /* Call 32-bit relay code */
1847 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, BP_reg( context ) ) );
1848 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1850 context->Eax = CALL32_CBClient( proc, args, &context->Esi );
1853 /***********************************************************************
1854 * CBClientThunkSLEx (KERNEL.621)
1856 extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs );
1857 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
1859 /* Call 32-bit relay code */
1861 LPWORD args = MapSL( MAKESEGPTR( context->SegSs, BP_reg( context ) ) );
1862 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1863 INT nArgs;
1864 LPWORD stackLin;
1866 context->Eax = CALL32_CBClientEx( proc, args, &context->Esi, &nArgs );
1868 /* Restore registers saved by CBClientGlueSL */
1869 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
1870 BP_reg( context ) = stackLin[3];
1871 SI_reg( context ) = stackLin[2];
1872 DI_reg( context ) = stackLin[1];
1873 context->SegDs = stackLin[0];
1874 context->Esp += 16+nArgs;
1876 /* Return to caller of CBClient thunklet */
1877 context->SegCs = stackLin[9];
1878 context->Eip = stackLin[8];
1882 /***********************************************************************
1883 * Get16DLLAddress (KERNEL32.@)
1885 * This function is used by a Win32s DLL if it wants to call a Win16 function.
1886 * A 16:16 segmented pointer to the function is returned.
1887 * Written without any docu.
1889 SEGPTR WINAPI Get16DLLAddress(HMODULE handle, LPSTR func_name)
1891 static WORD code_sel32;
1892 FARPROC16 proc_16;
1893 LPBYTE thunk;
1895 if (!code_sel32)
1897 if (!ThunkletHeap) THUNK_Init();
1898 code_sel32 = SELECTOR_AllocBlock( (void *)ThunkletHeap, 0x10000,
1899 WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
1900 if (!code_sel32) return 0;
1902 if (!(thunk = HeapAlloc( ThunkletHeap, 0, 32 ))) return 0;
1904 if (!handle) handle = GetModuleHandle16("WIN32S16");
1905 proc_16 = GetProcAddress16(handle, func_name);
1907 /* movl proc_16, $edx */
1908 *thunk++ = 0xba;
1909 *(FARPROC16 *)thunk = proc_16;
1910 thunk += sizeof(FARPROC16);
1912 /* jmpl QT_Thunk */
1913 *thunk++ = 0xea;
1914 *(FARPROC *)thunk = GetProcAddress(GetModuleHandleA("KERNEL32"),"QT_Thunk");
1915 thunk += sizeof(FARPROC16);
1916 *(WORD *)thunk = __get_cs();
1918 return MAKESEGPTR( code_sel32, (char *)thunk - (char *)ThunkletHeap );
1922 /***********************************************************************
1923 * GetWin16DOSEnv (KERNEL32.34)
1924 * Returns some internal value.... probably the default environment database?
1926 DWORD WINAPI GetWin16DOSEnv()
1928 FIXME("stub, returning 0\n");
1929 return 0;
1932 /**********************************************************************
1933 * GetPK16SysVar (KERNEL32.92)
1935 LPVOID WINAPI GetPK16SysVar(void)
1937 static BYTE PK16SysVar[128];
1939 FIXME("()\n");
1940 return PK16SysVar;
1943 /**********************************************************************
1944 * CommonUnimpStub (KERNEL32.17)
1946 void WINAPI CommonUnimpStub( CONTEXT86 *context )
1948 if (context->Eax)
1949 MESSAGE( "*** Unimplemented Win32 API: %s\n", (LPSTR)context->Eax );
1951 switch ((context->Ecx >> 4) & 0x0f)
1953 case 15: context->Eax = -1; break;
1954 case 14: context->Eax = 0x78; break;
1955 case 13: context->Eax = 0x32; break;
1956 case 1: context->Eax = 1; break;
1957 default: context->Eax = 0; break;
1960 context->Esp += (context->Ecx & 0x0f) * 4;
1963 /**********************************************************************
1964 * HouseCleanLogicallyDeadHandles (KERNEL32.33)
1966 void WINAPI HouseCleanLogicallyDeadHandles(void)
1968 /* Whatever this is supposed to do, our handles probably
1969 don't need it :-) */
1972 /**********************************************************************
1973 * @ (KERNEL32.100)
1975 BOOL WINAPI _KERNEL32_100(HANDLE threadid,DWORD exitcode,DWORD x)
1977 FIXME("(%d,%ld,0x%08lx): stub\n",threadid,exitcode,x);
1978 return TRUE;
1981 /**********************************************************************
1982 * @ (KERNEL32.99)
1984 * Checks whether the clock has to be switched from daylight
1985 * savings time to standard time or vice versa.
1988 DWORD WINAPI _KERNEL32_99(DWORD x)
1990 FIXME("(0x%08lx): stub\n",x);
1991 return 1;
1995 /**********************************************************************
1996 * Catch (KERNEL.55)
1998 * Real prototype is:
1999 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
2001 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT86 *context )
2003 /* Note: we don't save the current ss, as the catch buffer is */
2004 /* only 9 words long. Hopefully no one will have the silly */
2005 /* idea to change the current stack before calling Throw()... */
2007 /* Windows uses:
2008 * lpbuf[0] = ip
2009 * lpbuf[1] = cs
2010 * lpbuf[2] = sp
2011 * lpbuf[3] = bp
2012 * lpbuf[4] = si
2013 * lpbuf[5] = di
2014 * lpbuf[6] = ds
2015 * lpbuf[7] = unused
2016 * lpbuf[8] = ss
2019 lpbuf[0] = LOWORD(context->Eip);
2020 lpbuf[1] = context->SegCs;
2021 /* Windows pushes 4 more words before saving sp */
2022 lpbuf[2] = LOWORD(context->Esp) - 4 * sizeof(WORD);
2023 lpbuf[3] = LOWORD(context->Ebp);
2024 lpbuf[4] = LOWORD(context->Esi);
2025 lpbuf[5] = LOWORD(context->Edi);
2026 lpbuf[6] = context->SegDs;
2027 lpbuf[7] = 0;
2028 lpbuf[8] = context->SegSs;
2029 AX_reg(context) = 0; /* Return 0 */
2033 /**********************************************************************
2034 * Throw (KERNEL.56)
2036 * Real prototype is:
2037 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
2039 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT86 *context )
2041 STACK16FRAME *pFrame;
2042 STACK32FRAME *frame32;
2043 TEB *teb = NtCurrentTeb();
2045 AX_reg(context) = retval;
2047 /* Find the frame32 corresponding to the frame16 we are jumping to */
2048 pFrame = THREAD_STACK16(teb);
2049 frame32 = pFrame->frame32;
2050 while (frame32 && frame32->frame16)
2052 if (OFFSETOF(frame32->frame16) < OFFSETOF(teb->cur_stack))
2053 break; /* Something strange is going on */
2054 if (OFFSETOF(frame32->frame16) > lpbuf[2])
2056 /* We found the right frame */
2057 pFrame->frame32 = frame32;
2058 break;
2060 frame32 = ((STACK16FRAME *)MapSL(frame32->frame16))->frame32;
2063 context->Eip = lpbuf[0];
2064 context->SegCs = lpbuf[1];
2065 context->Esp = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
2066 context->Ebp = lpbuf[3];
2067 context->Esi = lpbuf[4];
2068 context->Edi = lpbuf[5];
2069 context->SegDs = lpbuf[6];
2071 if (lpbuf[8] != context->SegSs)
2072 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );