- added some debug code and cleaned-up the mouse warping code
[wine.git] / win32 / kernel32.c
blobc8e8fbe277fbab7bf314cd022e89ad6f278ca385
1 /*
2 * KERNEL32 thunks and other undocumented stuff
4 * Copyright 1997-1998 Marcus Meissner
5 * Copyright 1998 Ulrich Weigand
7 */
9 #include <string.h>
10 #include <sys/types.h>
11 #include <unistd.h>
13 #include "windef.h"
14 #include "winbase.h"
15 #include "wine/winbase16.h"
16 #include "callback.h"
17 #include "task.h"
18 #include "user.h"
19 #include "heap.h"
20 #include "module.h"
21 #include "neexe.h"
22 #include "process.h"
23 #include "stackframe.h"
24 #include "selectors.h"
25 #include "file.h"
26 #include "debugtools.h"
27 #include "flatthunk.h"
28 #include "syslevel.h"
29 #include "winerror.h"
31 DEFAULT_DEBUG_CHANNEL(thunk);
32 DECLARE_DEBUG_CHANNEL(win32);
35 /***********************************************************************
36 * *
37 * Win95 internal thunks *
38 * *
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
85 * C3 lret
87 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
88 LPBYTE x;
90 x = relayCode;
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.
104 * 33C9 xor ecx, ecx
105 * 8A4DFC mov cl , [ebp-04]
106 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
107 * B8yyyyyyyy mov eax, QT_Thunk
108 * FFE0 jmp eax
110 static void _write_qtthunk(
111 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
112 DWORD *targetTable /* [in] start of thunk (for index lookup) */
114 LPBYTE x;
116 x = relayCode;
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 /***********************************************************************
128 * _loadthunk
130 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
131 struct ThunkDataCommon *TD32, DWORD checksum)
133 struct ThunkDataCommon *TD16;
134 HMODULE hmod;
135 int ordinal;
137 if ((hmod = LoadLibrary16(module)) <= 32)
139 ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
140 module, func, module32, module, hmod);
141 return 0;
144 if ( !(ordinal = NE_GetOrdinal(hmod, func))
145 || !(TD16 = PTR_SEG_TO_LIN(NE_GetEntryPointEx(hmod, ordinal, FALSE))))
147 ERR("(%s, %s, %s): Unable to find '%s'\n",
148 module, func, module32, func);
149 return 0;
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]);
158 return 0;
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);
165 return 0;
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);
172 return 0;
175 return TD16;
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 */
207 BOOL directionSL;
209 if (!strncmp(TD->magic, "SL01", 4))
211 directionSL = TRUE;
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))
218 directionSL = FALSE;
220 TRACE("LS01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
221 module32, (DWORD)TD, module16, thunkfun16, dwReason);
223 else
225 ERR("Invalid magic %c%c%c%c\n",
226 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
227 return 0;
230 switch (dwReason)
232 case DLL_PROCESS_ATTACH:
234 struct ThunkDataCommon *TD16;
235 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
236 return 0;
238 if (directionSL)
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");
247 return 0;
250 SL32->data = SL16->fpData;
252 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
253 tdb->process = PROCESS_Current();
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 (DWORD)PROCESS_Current(), (DWORD)SL32->data);
262 else
264 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
265 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
267 LS32->targetTable = PTR_SEG_TO_LIN(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);
273 break;
276 case DLL_PROCESS_DETACH:
277 /* FIXME: cleanup */
278 break;
281 return 1;
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).
290 * [ok]
292 void WINAPI QT_Thunk( CONTEXT86 *context )
294 CONTEXT86 context16;
295 DWORD argsize;
297 memcpy(&context16,context,sizeof(context16));
299 CS_reg(&context16) = HIWORD(EDX_reg(context));
300 EIP_reg(&context16) = LOWORD(EDX_reg(context));
301 EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
302 + (WORD)&((STACK16FRAME*)0)->bp;
304 argsize = EBP_reg(context)-ESP_reg(context)-0x40;
306 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
307 (LPBYTE)ESP_reg(context), argsize );
309 EAX_reg(context) = Callbacks->CallRegisterShortProc( &context16, argsize );
310 EDX_reg(context) = HIWORD(EAX_reg(context));
311 EAX_reg(context) = LOWORD(EAX_reg(context));
315 /**********************************************************************
316 * FT_Prolog (KERNEL32.233)
318 * The set of FT_... thunk routines is used instead of QT_Thunk,
319 * if structures have to be converted from 32-bit to 16-bit
320 * (change of member alignment, conversion of members).
322 * The thunk function (as created by the thunk compiler) calls
323 * FT_Prolog at the beginning, to set up a stack frame and
324 * allocate a 64 byte buffer on the stack.
325 * The input parameters (target address and some flags) are
326 * saved for later use by FT_Thunk.
328 * Input: EDX 16-bit target address (SEGPTR)
329 * CX bits 0..7 target number (in target table)
330 * bits 8..9 some flags (unclear???)
331 * bits 10..15 number of DWORD arguments
333 * Output: A new stackframe is created, and a 64 byte buffer
334 * allocated on the stack. The layout of the stack
335 * on return is as follows:
337 * (ebp+4) return address to caller of thunk function
338 * (ebp) old EBP
339 * (ebp-4) saved EBX register of caller
340 * (ebp-8) saved ESI register of caller
341 * (ebp-12) saved EDI register of caller
342 * (ebp-16) saved ECX register, containing flags
343 * (ebp-20) bitmap containing parameters that are to be converted
344 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
345 * filled in by the thunk code before calling FT_Thunk
346 * (ebp-24)
347 * ... (unclear)
348 * (ebp-44)
349 * (ebp-48) saved EAX register of caller (unclear, never restored???)
350 * (ebp-52) saved EDX register, containing 16-bit thunk target
351 * (ebp-56)
352 * ... (unclear)
353 * (ebp-64)
355 * ESP is EBP-64 after return.
359 void WINAPI FT_Prolog( CONTEXT86 *context )
361 /* Build stack frame */
362 stack32_push(context, EBP_reg(context));
363 EBP_reg(context) = ESP_reg(context);
365 /* Allocate 64-byte Thunk Buffer */
366 ESP_reg(context) -= 64;
367 memset((char *)ESP_reg(context), '\0', 64);
369 /* Store Flags (ECX) and Target Address (EDX) */
370 /* Save other registers to be restored later */
371 *(DWORD *)(EBP_reg(context) - 4) = EBX_reg(context);
372 *(DWORD *)(EBP_reg(context) - 8) = ESI_reg(context);
373 *(DWORD *)(EBP_reg(context) - 12) = EDI_reg(context);
374 *(DWORD *)(EBP_reg(context) - 16) = ECX_reg(context);
376 *(DWORD *)(EBP_reg(context) - 48) = EAX_reg(context);
377 *(DWORD *)(EBP_reg(context) - 52) = EDX_reg(context);
380 /**********************************************************************
381 * FT_Thunk (KERNEL32.234)
383 * This routine performs the actual call to 16-bit code,
384 * similar to QT_Thunk. The differences are:
385 * - The call target is taken from the buffer created by FT_Prolog
386 * - Those arguments requested by the thunk code (by setting the
387 * corresponding bit in the bitmap at EBP-20) are converted
388 * from 32-bit pointers to segmented pointers (those pointers
389 * are guaranteed to point to structures copied to the stack
390 * by the thunk code, so we always use the 16-bit stack selector
391 * for those addresses).
393 * The bit #i of EBP-20 corresponds here to the DWORD starting at
394 * ESP+4 + 2*i.
396 * FIXME: It is unclear what happens if there are more than 32 WORDs
397 * of arguments, so that the single DWORD bitmap is no longer
398 * sufficient ...
401 void WINAPI FT_Thunk( CONTEXT86 *context )
403 DWORD mapESPrelative = *(DWORD *)(EBP_reg(context) - 20);
404 DWORD callTarget = *(DWORD *)(EBP_reg(context) - 52);
406 CONTEXT86 context16;
407 DWORD i, argsize;
408 LPBYTE newstack, oldstack;
410 memcpy(&context16,context,sizeof(context16));
412 CS_reg(&context16) = HIWORD(callTarget);
413 EIP_reg(&context16) = LOWORD(callTarget);
414 EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
415 + (WORD)&((STACK16FRAME*)0)->bp;
417 argsize = EBP_reg(context)-ESP_reg(context)-0x40;
418 newstack = (LPBYTE)CURRENT_STACK16 - argsize;
419 oldstack = (LPBYTE)ESP_reg(context);
421 memcpy( newstack, oldstack, argsize );
423 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
424 if (mapESPrelative & (1 << i))
426 SEGPTR *arg = (SEGPTR *)(newstack + 2*i);
427 *arg = PTR_SEG_OFF_TO_SEGPTR(SELECTOROF(NtCurrentTeb()->cur_stack),
428 OFFSETOF(NtCurrentTeb()->cur_stack) - argsize
429 + (*(LPBYTE *)arg - oldstack));
432 EAX_reg(context) = Callbacks->CallRegisterShortProc( &context16, argsize );
433 EDX_reg(context) = HIWORD(EAX_reg(context));
434 EAX_reg(context) = LOWORD(EAX_reg(context));
436 /* Copy modified buffers back to 32-bit stack */
437 memcpy( oldstack, newstack, argsize );
440 /**********************************************************************
441 * FT_ExitNN (KERNEL32.218 - 232)
443 * One of the FT_ExitNN functions is called at the end of the thunk code.
444 * It removes the stack frame created by FT_Prolog, moves the function
445 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
446 * value, but the thunk code has moved it from EAX to EBX in the
447 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
448 * and perform a return to the CALLER of the thunk code (while removing
449 * the given number of arguments from the caller's stack).
452 static void FT_Exit(CONTEXT86 *context, int nPopArgs)
454 /* Return value is in EBX */
455 EAX_reg(context) = EBX_reg(context);
457 /* Restore EBX, ESI, and EDI registers */
458 EBX_reg(context) = *(DWORD *)(EBP_reg(context) - 4);
459 ESI_reg(context) = *(DWORD *)(EBP_reg(context) - 8);
460 EDI_reg(context) = *(DWORD *)(EBP_reg(context) - 12);
462 /* Clean up stack frame */
463 ESP_reg(context) = EBP_reg(context);
464 EBP_reg(context) = stack32_pop(context);
466 /* Pop return address to CALLER of thunk code */
467 EIP_reg(context) = stack32_pop(context);
468 /* Remove arguments */
469 ESP_reg(context) += nPopArgs;
472 void WINAPI FT_Exit0 (CONTEXT86 *context) { FT_Exit(context, 0); }
473 void WINAPI FT_Exit4 (CONTEXT86 *context) { FT_Exit(context, 4); }
474 void WINAPI FT_Exit8 (CONTEXT86 *context) { FT_Exit(context, 8); }
475 void WINAPI FT_Exit12(CONTEXT86 *context) { FT_Exit(context, 12); }
476 void WINAPI FT_Exit16(CONTEXT86 *context) { FT_Exit(context, 16); }
477 void WINAPI FT_Exit20(CONTEXT86 *context) { FT_Exit(context, 20); }
478 void WINAPI FT_Exit24(CONTEXT86 *context) { FT_Exit(context, 24); }
479 void WINAPI FT_Exit28(CONTEXT86 *context) { FT_Exit(context, 28); }
480 void WINAPI FT_Exit32(CONTEXT86 *context) { FT_Exit(context, 32); }
481 void WINAPI FT_Exit36(CONTEXT86 *context) { FT_Exit(context, 36); }
482 void WINAPI FT_Exit40(CONTEXT86 *context) { FT_Exit(context, 40); }
483 void WINAPI FT_Exit44(CONTEXT86 *context) { FT_Exit(context, 44); }
484 void WINAPI FT_Exit48(CONTEXT86 *context) { FT_Exit(context, 48); }
485 void WINAPI FT_Exit52(CONTEXT86 *context) { FT_Exit(context, 52); }
486 void WINAPI FT_Exit56(CONTEXT86 *context) { FT_Exit(context, 56); }
489 /***********************************************************************
490 * ThunkInitLS (KERNEL32.43)
491 * A thunkbuffer link routine
492 * The thunkbuf looks like:
494 * 00: DWORD length ? don't know exactly
495 * 04: SEGPTR ptr ? where does it point to?
496 * The pointer ptr is written into the first DWORD of 'thunk'.
497 * (probably correct implemented)
498 * [ok probably]
499 * RETURNS
500 * segmented pointer to thunk?
502 DWORD WINAPI ThunkInitLS(
503 LPDWORD thunk, /* [in] win32 thunk */
504 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
505 DWORD len, /* [in] thkbuffer length */
506 LPCSTR dll16, /* [in] name of win16 dll */
507 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
509 LPDWORD addr;
511 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
512 return 0;
514 if (!addr[1])
515 return 0;
516 *(DWORD*)thunk = addr[1];
518 return addr[1];
521 /***********************************************************************
522 * Common32ThkLS (KERNEL32.45)
524 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
525 * style thunks. The basic difference is that the parameter conversion
526 * is done completely on the *16-bit* side here. Thus we do not call
527 * the 16-bit target directly, but call a common entry point instead.
528 * This entry function then calls the target according to the target
529 * number passed in the DI register.
531 * Input: EAX SEGPTR to the common 16-bit entry point
532 * CX offset in thunk table (target number * 4)
533 * DX error return value if execution fails (unclear???)
534 * EDX.HI number of DWORD parameters
536 * (Note that we need to move the thunk table offset from CX to DI !)
538 * The called 16-bit stub expects its stack to look like this:
539 * ...
540 * (esp+40) 32-bit arguments
541 * ...
542 * (esp+8) 32 byte of stack space available as buffer
543 * (esp) 8 byte return address for use with 0x66 lret
545 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
546 * and uses the EAX register to return a DWORD return value.
547 * Thus we need to use a special assembly glue routine
548 * (CallRegisterLongProc instead of CallRegisterShortProc).
550 * Finally, we return to the caller, popping the arguments off
551 * the stack.
553 * FIXME: The called function uses EBX to return the number of
554 * arguments that are to be popped off the caller's stack.
555 * This is clobbered by the assembly glue, so we simply use
556 * the original EDX.HI to get the number of arguments.
557 * (Those two values should be equal anyway ...?)
560 void WINAPI Common32ThkLS( CONTEXT86 *context )
562 CONTEXT86 context16;
563 DWORD argsize;
565 memcpy(&context16,context,sizeof(context16));
567 DI_reg(&context16) = CX_reg(context);
568 CS_reg(&context16) = HIWORD(EAX_reg(context));
569 EIP_reg(&context16) = LOWORD(EAX_reg(context));
570 EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
571 + (WORD)&((STACK16FRAME*)0)->bp;
573 argsize = HIWORD(EDX_reg(context)) * 4;
575 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
576 if (EDX_reg(context) == EIP_reg(context))
577 argsize = 6 * 4;
579 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
580 (LPBYTE)ESP_reg(context), argsize );
582 EAX_reg(context) = Callbacks->CallRegisterLongProc(&context16, argsize + 32);
584 /* Clean up caller's stack frame */
585 ESP_reg(context) += argsize;
588 /***********************************************************************
589 * OT_32ThkLSF (KERNEL32.40)
591 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
592 * argument processing is done on both the 32-bit and the 16-bit side:
593 * The 32-bit side prepares arguments, copying them onto the stack.
595 * When this routine is called, the first word on the stack is the
596 * number of argument bytes prepared by the 32-bit code, and EDX
597 * contains the 16-bit target address.
599 * The called 16-bit routine is another relaycode, doing further
600 * argument processing and then calling the real 16-bit target
601 * whose address is stored at [bp-04].
603 * The call proceeds using a normal CallRegisterShortProc.
604 * After return from the 16-bit relaycode, the arguments need
605 * to be copied *back* to the 32-bit stack, since the 32-bit
606 * relaycode processes output parameters.
608 * Note that we copy twice the number of arguments, since some of the
609 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
610 * arguments of the caller!
612 * (Note that this function seems only to be used for
613 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
615 void WINAPI OT_32ThkLSF( CONTEXT86 *context )
617 CONTEXT86 context16;
618 DWORD argsize;
620 memcpy(&context16,context,sizeof(context16));
622 CS_reg(&context16) = HIWORD(EDX_reg(context));
623 EIP_reg(&context16) = LOWORD(EDX_reg(context));
624 EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
625 + (WORD)&((STACK16FRAME*)0)->bp;
627 argsize = 2 * *(WORD *)ESP_reg(context) + 2;
629 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
630 (LPBYTE)ESP_reg(context), argsize );
632 EAX_reg(context) = Callbacks->CallRegisterShortProc(&context16, argsize);
634 memcpy( (LPBYTE)ESP_reg(context),
635 (LPBYTE)CURRENT_STACK16 - argsize, argsize );
638 /***********************************************************************
639 * ThunkInitLSF (KERNEL32.41)
640 * A thunk setup routine.
641 * Expects a pointer to a preinitialized thunkbuffer in the first argument
642 * looking like:
643 * 00..03: unknown (pointer, check _41, _43, _46)
644 * 04: EB1E jmp +0x20
646 * 06..23: unknown (space for replacement code, check .90)
648 * 24:>E800000000 call offset 29
649 * 29:>58 pop eax ( target of call )
650 * 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
651 * 2F: BAxxxxxxxx mov edx,xxxxxxxx
652 * 34: 68yyyyyyyy push KERNEL32.90
653 * 39: C3 ret
655 * 3A: EB1E jmp +0x20
656 * 3E ... 59: unknown (space for replacement code?)
657 * 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
658 * 5F: 5A pop edx
659 * 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
660 * 66: 52 push edx
661 * 67: 68xxxxxxxx push xxxxxxxx
662 * 6C: 68yyyyyyyy push KERNEL32.89
663 * 71: C3 ret
664 * 72: end?
665 * This function checks if the code is there, and replaces the yyyyyyyy entries
666 * by the functionpointers.
667 * The thunkbuf looks like:
669 * 00: DWORD length ? don't know exactly
670 * 04: SEGPTR ptr ? where does it point to?
671 * The segpointer ptr is written into the first DWORD of 'thunk'.
672 * [ok probably]
673 * RETURNS
674 * unclear, pointer to win16 thkbuffer?
676 LPVOID WINAPI ThunkInitLSF(
677 LPBYTE thunk, /* [in] win32 thunk */
678 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
679 DWORD len, /* [in] length of thkbuffer */
680 LPCSTR dll16, /* [in] name of win16 dll */
681 LPCSTR dll32 /* [in] name of win32 dll */
683 HMODULE hkrnl32 = GetModuleHandleA("KERNEL32");
684 LPDWORD addr,addr2;
686 /* FIXME: add checks for valid code ... */
687 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
688 *(DWORD*)(thunk+0x35) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)90);
689 *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)89);
692 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
693 return 0;
695 addr2 = PTR_SEG_TO_LIN(addr[1]);
696 if (HIWORD(addr2))
697 *(DWORD*)thunk = (DWORD)addr2;
699 return addr2;
702 /***********************************************************************
703 * FT_PrologPrime (KERNEL32.89)
705 * This function is called from the relay code installed by
706 * ThunkInitLSF. It replaces the location from where it was
707 * called by a standard FT_Prolog call stub (which is 'primed'
708 * by inserting the correct target table pointer).
709 * Finally, it calls that stub.
711 * Input: ECX target number + flags (passed through to FT_Prolog)
712 * (ESP) offset of location where target table pointer
713 * is stored, relative to the start of the relay code
714 * (ESP+4) pointer to start of relay code
715 * (this is where the FT_Prolog call stub gets written to)
717 * Note: The two DWORD arguments get popped off the stack.
720 void WINAPI FT_PrologPrime( CONTEXT86 *context )
722 DWORD targetTableOffset;
723 LPBYTE relayCode;
725 /* Compensate for the fact that the Wine register relay code thought
726 we were being called, although we were in fact jumped to */
727 ESP_reg(context) -= 4;
729 /* Write FT_Prolog call stub */
730 targetTableOffset = stack32_pop(context);
731 relayCode = (LPBYTE)stack32_pop(context);
732 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
734 /* Jump to the call stub just created */
735 EIP_reg(context) = (DWORD)relayCode;
738 /***********************************************************************
739 * QT_ThunkPrime (KERNEL32.90)
741 * This function corresponds to FT_PrologPrime, but installs a
742 * call stub for QT_Thunk instead.
744 * Input: (EBP-4) target number (passed through to QT_Thunk)
745 * EDX target table pointer location offset
746 * EAX start of relay code
749 void WINAPI QT_ThunkPrime( CONTEXT86 *context )
751 DWORD targetTableOffset;
752 LPBYTE relayCode;
754 /* Compensate for the fact that the Wine register relay code thought
755 we were being called, although we were in fact jumped to */
756 ESP_reg(context) -= 4;
758 /* Write QT_Thunk call stub */
759 targetTableOffset = EDX_reg(context);
760 relayCode = (LPBYTE)EAX_reg(context);
761 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
763 /* Jump to the call stub just created */
764 EIP_reg(context) = (DWORD)relayCode;
767 /***********************************************************************
768 * (KERNEL32.46)
769 * Another thunkbuf link routine.
770 * The start of the thunkbuf looks like this:
771 * 00: DWORD length
772 * 04: SEGPTR address for thunkbuffer pointer
773 * [ok probably]
775 VOID WINAPI ThunkInitSL(
776 LPBYTE thunk, /* [in] start of thunkbuffer */
777 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
778 DWORD len, /* [in] length of thunkbuffer */
779 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
780 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
782 LPDWORD addr;
784 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
785 return;
787 *(DWORD*)PTR_SEG_TO_LIN(addr[1]) = (DWORD)thunk;
790 /**********************************************************************
791 * SSInit KERNEL.700
792 * RETURNS
793 * TRUE for success.
795 BOOL WINAPI SSInit16()
797 return TRUE;
800 /**********************************************************************
801 * SSOnBigStack KERNEL32.87
802 * Check if thunking is initialized (ss selector set up etc.)
803 * We do that differently, so just return TRUE.
804 * [ok]
805 * RETURNS
806 * TRUE for success.
808 BOOL WINAPI SSOnBigStack()
810 TRACE("Yes, thunking is initialized\n");
811 return TRUE;
814 /**********************************************************************
815 * SSConfirmSmallStack KERNEL.704
817 * Abort if not on small stack.
819 * This must be a register routine as it has to preserve *all* registers.
821 void WINAPI SSConfirmSmallStack( CONTEXT86 *context )
823 /* We are always on the small stack while in 16-bit code ... */
826 /**********************************************************************
827 * SSCall
828 * One of the real thunking functions. This one seems to be for 32<->32
829 * thunks. It should probably be capable of crossing processboundaries.
831 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
832 * [ok]
834 DWORD WINAPIV SSCall(
835 DWORD nr, /* [in] number of argument bytes */
836 DWORD flags, /* [in] FIXME: flags ? */
837 FARPROC fun, /* [in] function to call */
838 ... /* [in/out] arguments */
840 DWORD i,ret;
841 DWORD *args = ((DWORD *)&fun) + 1;
843 if(TRACE_ON(thunk))
845 DPRINTF("(%ld,0x%08lx,%p,[",nr,flags,fun);
846 for (i=0;i<nr/4;i++)
847 DPRINTF("0x%08lx,",args[i]);
848 DPRINTF("])\n");
850 switch (nr) {
851 case 0: ret = fun();
852 break;
853 case 4: ret = fun(args[0]);
854 break;
855 case 8: ret = fun(args[0],args[1]);
856 break;
857 case 12: ret = fun(args[0],args[1],args[2]);
858 break;
859 case 16: ret = fun(args[0],args[1],args[2],args[3]);
860 break;
861 case 20: ret = fun(args[0],args[1],args[2],args[3],args[4]);
862 break;
863 case 24: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
864 break;
865 case 28: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
866 break;
867 case 32: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
868 break;
869 case 36: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
870 break;
871 case 40: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
872 break;
873 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]);
874 break;
875 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]);
876 break;
877 default:
878 WARN("Unsupported nr of arguments, %ld\n",nr);
879 ret = 0;
880 break;
883 TRACE(" returning %ld ...\n",ret);
884 return ret;
887 /**********************************************************************
888 * W32S_BackTo32 (KERNEL32.51)
890 void WINAPI W32S_BackTo32( CONTEXT86 *context )
892 LPDWORD stack = (LPDWORD)ESP_reg( context );
893 FARPROC proc = (FARPROC)EIP_reg(context);
895 EAX_reg( context ) = proc( stack[1], stack[2], stack[3], stack[4], stack[5],
896 stack[6], stack[7], stack[8], stack[9], stack[10] );
898 EIP_reg( context ) = stack32_pop(context);
901 /**********************************************************************
902 * AllocSLCallback (KERNEL32)
904 * Win95 uses some structchains for callbacks. It allocates them
905 * in blocks of 100 entries, size 32 bytes each, layout:
906 * blockstart:
907 * 0: PTR nextblockstart
908 * 4: entry *first;
909 * 8: WORD sel ( start points to blockstart)
910 * A: WORD unknown
911 * 100xentry:
912 * 00..17: Code
913 * 18: PDB *owning_process;
914 * 1C: PTR blockstart
916 * We ignore this for now. (Just a note for further developers)
917 * FIXME: use this method, so we don't waste selectors...
919 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
920 * the 0x66 prefix switches from word->long registers.
922 * 665A pop edx
923 * 6668x arg2 x pushl <arg2>
924 * 6652 push edx
925 * EAx arg1 x jmpf <arg1>
927 * returns the startaddress of this thunk.
929 * Note, that they look very similair to the ones allocates by THUNK_Alloc.
930 * RETURNS
931 * segmented pointer to the start of the thunk
933 DWORD WINAPI
934 AllocSLCallback(
935 DWORD finalizer, /* [in] finalizer function */
936 DWORD callback /* [in] callback function */
938 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
939 WORD sel;
941 x=thunk;
942 *x++=0x66;*x++=0x5a; /* popl edx */
943 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
944 *x++=0x66;*x++=0x52; /* pushl edx */
945 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
947 *(PDB**)(thunk+18) = PROCESS_Current();
949 sel = SELECTOR_AllocBlock( thunk , 32, SEGMENT_CODE, FALSE, FALSE );
950 return (sel<<16)|0;
953 /**********************************************************************
954 * FreeSLCallback (KERNEL32.274)
955 * Frees the specified 16->32 callback
957 void WINAPI
958 FreeSLCallback(
959 DWORD x /* [in] 16 bit callback (segmented pointer?) */
961 FIXME_(win32)("(0x%08lx): stub\n",x);
965 /**********************************************************************
966 * GetTEBSelectorFS (KERNEL.475)
967 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
969 void WINAPI GetTEBSelectorFS16(void)
971 CURRENT_STACK16->fs = __get_fs();
974 /**********************************************************************
975 * KERNEL_431 (KERNEL.431)
976 * IsPeFormat (W32SYS.2)
977 * Checks the passed filename if it is a PE format executeable
978 * RETURNS
979 * TRUE, if it is.
980 * FALSE if not.
982 BOOL16 WINAPI IsPeFormat16(
983 LPSTR fn, /* [in] filename to executeable */
984 HFILE16 hf16 /* [in] open file, if filename is NULL */
986 IMAGE_DOS_HEADER mzh;
987 HFILE hf=FILE_GetHandle(hf16);
988 OFSTRUCT ofs;
989 DWORD xmagic;
991 if (fn) {
992 hf = OpenFile(fn,&ofs,OF_READ);
993 if (hf==HFILE_ERROR)
994 return FALSE;
996 _llseek(hf,0,SEEK_SET);
997 if (sizeof(mzh)!=_lread(hf,&mzh,sizeof(mzh))) {
998 _lclose(hf);
999 return FALSE;
1001 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) {
1002 WARN("File has not got dos signature!\n");
1003 _lclose(hf);
1004 return FALSE;
1006 _llseek(hf,mzh.e_lfanew,SEEK_SET);
1007 if (sizeof(DWORD)!=_lread(hf,&xmagic,sizeof(DWORD))) {
1008 _lclose(hf);
1009 return FALSE;
1011 _lclose(hf);
1012 return (xmagic == IMAGE_NT_SIGNATURE);
1016 /***********************************************************************
1017 * K32Thk1632Prolog (KERNEL32.492)
1019 void WINAPI K32Thk1632Prolog( CONTEXT86 *context )
1021 LPBYTE code = (LPBYTE)EIP_reg(context) - 5;
1023 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1024 of 16->32 thunks instead of using one of the standard methods!
1025 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1026 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1027 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1028 bypassed, which means it will crash the next time the 32-bit OLE
1029 code thunks down again to 16-bit (this *will* happen!).
1031 The following hack tries to recognize this situation.
1032 This is possible since the called stubs in OLECLI32/OLESVR32 all
1033 look exactly the same:
1034 00 E8xxxxxxxx call K32Thk1632Prolog
1035 05 FF55FC call [ebp-04]
1036 08 E8xxxxxxxx call K32Thk1632Epilog
1037 0D 66CB retf
1039 If we recognize this situation, we try to simulate the actions
1040 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1041 to our 32-bit stack, creating a proper STACK16FRAME and
1042 updating cur_stack. */
1044 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1045 && code[13] == 0x66 && code[14] == 0xCB)
1047 WORD stackSel = NtCurrentTeb()->stack_sel;
1048 DWORD stackBase = GetSelectorBase(stackSel);
1050 DWORD argSize = EBP_reg(context) - ESP_reg(context);
1051 char *stack16 = (char *)ESP_reg(context) - 4;
1052 char *stack32 = (char *)NtCurrentTeb()->cur_stack - argSize;
1053 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1055 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1056 EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1058 memset(frame16, '\0', sizeof(STACK16FRAME));
1059 frame16->frame32 = (STACK32FRAME *)NtCurrentTeb()->cur_stack;
1060 frame16->ebp = EBP_reg(context);
1062 memcpy(stack32, stack16, argSize);
1063 NtCurrentTeb()->cur_stack = PTR_SEG_OFF_TO_SEGPTR(stackSel, (DWORD)frame16 - stackBase);
1065 ESP_reg(context) = (DWORD)stack32 + 4;
1066 EBP_reg(context) = ESP_reg(context) + argSize;
1068 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1069 EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1072 SYSLEVEL_ReleaseWin16Lock();
1075 /***********************************************************************
1076 * K32Thk1632Epilog (KERNEL32.491)
1078 void WINAPI K32Thk1632Epilog( CONTEXT86 *context )
1080 LPBYTE code = (LPBYTE)EIP_reg(context) - 13;
1082 SYSLEVEL_RestoreWin16Lock();
1084 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1086 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1087 && code[13] == 0x66 && code[14] == 0xCB)
1089 STACK16FRAME *frame16 = (STACK16FRAME *)PTR_SEG_TO_LIN(NtCurrentTeb()->cur_stack);
1090 char *stack16 = (char *)(frame16 + 1);
1091 DWORD argSize = frame16->ebp - (DWORD)stack16;
1092 char *stack32 = (char *)frame16->frame32 - argSize;
1094 DWORD nArgsPopped = ESP_reg(context) - (DWORD)stack32;
1096 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1097 EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1099 NtCurrentTeb()->cur_stack = (DWORD)frame16->frame32;
1101 ESP_reg(context) = (DWORD)stack16 + nArgsPopped;
1102 EBP_reg(context) = frame16->ebp;
1104 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1105 EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1109 /***********************************************************************
1110 * UpdateResource32A (KERNEL32.707)
1112 BOOL WINAPI UpdateResourceA(
1113 HANDLE hUpdate,
1114 LPCSTR lpType,
1115 LPCSTR lpName,
1116 WORD wLanguage,
1117 LPVOID lpData,
1118 DWORD cbData) {
1120 FIXME_(win32)(": stub\n");
1121 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1122 return FALSE;
1125 /***********************************************************************
1126 * UpdateResource32W (KERNEL32.708)
1128 BOOL WINAPI UpdateResourceW(
1129 HANDLE hUpdate,
1130 LPCWSTR lpType,
1131 LPCWSTR lpName,
1132 WORD wLanguage,
1133 LPVOID lpData,
1134 DWORD cbData) {
1136 FIXME_(win32)(": stub\n");
1137 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1138 return FALSE;
1142 /***********************************************************************
1143 * WaitNamedPipe32A [KERNEL32.725]
1145 BOOL WINAPI WaitNamedPipeA (LPCSTR lpNamedPipeName, DWORD nTimeOut)
1146 { FIXME_(win32)("%s 0x%08lx\n",lpNamedPipeName,nTimeOut);
1147 SetLastError(ERROR_PIPE_NOT_CONNECTED);
1148 return FALSE;
1150 /***********************************************************************
1151 * WaitNamedPipe32W [KERNEL32.726]
1153 BOOL WINAPI WaitNamedPipeW (LPCWSTR lpNamedPipeName, DWORD nTimeOut)
1154 { FIXME_(win32)("%s 0x%08lx\n",debugstr_w(lpNamedPipeName),nTimeOut);
1155 SetLastError(ERROR_PIPE_NOT_CONNECTED);
1156 return FALSE;
1159 /*********************************************************************
1160 * PK16FNF [KERNEL32.91]
1162 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1163 * string buffer with the 8.3 filename of a recently loaded 16-bit
1164 * module. It is unknown exactly what modules trigger this
1165 * mechanism or what purpose this serves. Win98 Explorer (and
1166 * probably also Win95 with IE 4 shell integration) calls this
1167 * several times during initialization.
1169 * FIXME: find out what this really does and make it work.
1171 void WINAPI PK16FNF(LPSTR strPtr)
1173 FIXME_(win32)("(%p): stub\n", strPtr);
1175 /* fill in a fake filename that'll be easy to recognize */
1176 lstrcpyA(strPtr, "WINESTUB.FIX");