Use DInput's internal mouse event handler to handle the GetDeviceState
[wine/multimedia.git] / win32 / kernel32.c
blob7e303165f0906b4da4bab1f2d1dd1f67ac859174
1 /*
2 * KERNEL32 thunks and other undocumented stuff
4 * Copyright 1997-1998 Marcus Meissner
5 * Copyright 1998 Ulrich Weigand
7 */
9 #include <string.h>
11 #include "windef.h"
12 #include "winbase.h"
13 #include "wine/winbase16.h"
14 #include "callback.h"
15 #include "task.h"
16 #include "user.h"
17 #include "heap.h"
18 #include "module.h"
19 #include "neexe.h"
20 #include "process.h"
21 #include "stackframe.h"
22 #include "selectors.h"
23 #include "file.h"
24 #include "debugtools.h"
25 #include "flatthunk.h"
26 #include "syslevel.h"
27 #include "winerror.h"
29 DECLARE_DEBUG_CHANNEL(dosmem)
30 DECLARE_DEBUG_CHANNEL(thunk)
31 DECLARE_DEBUG_CHANNEL(win32)
34 /***********************************************************************
35 * *
36 * Win95 internal thunks *
37 * *
38 ***********************************************************************/
40 /***********************************************************************
41 * LogApiThk (KERNEL.423)
43 void WINAPI LogApiThk( LPSTR func )
45 TRACE_(thunk)( "%s\n", debugstr_a(func) );
48 /***********************************************************************
49 * LogApiThkLSF (KERNEL32.42)
51 * NOTE: needs to preserve all registers!
53 void WINAPI LogApiThkLSF( LPSTR func, CONTEXT86 *context )
55 TRACE_(thunk)( "%s\n", debugstr_a(func) );
58 /***********************************************************************
59 * LogApiThkSL (KERNEL32.44)
61 * NOTE: needs to preserve all registers!
63 void WINAPI LogApiThkSL( LPSTR func, CONTEXT86 *context )
65 TRACE_(thunk)( "%s\n", debugstr_a(func) );
68 /***********************************************************************
69 * LogCBThkSL (KERNEL32.47)
71 * NOTE: needs to preserve all registers!
73 void WINAPI LogCBThkSL( LPSTR func, CONTEXT86 *context )
75 TRACE_(thunk)( "%s\n", debugstr_a(func) );
78 /***********************************************************************
79 * Generates a FT_Prolog call.
81 * 0FB6D1 movzbl edx,cl
82 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
83 * 68xxxxxxxx push FT_Prolog
84 * C3 lret
86 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
87 LPBYTE x;
89 x = relayCode;
90 *x++ = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
91 *x++ = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
92 x+=4; /* mov edx, [4*edx + targetTable] */
93 *x++ = 0x68; *(DWORD*)x = (DWORD)GetProcAddress(GetModuleHandleA("KERNEL32"),"FT_Prolog");
94 x+=4; /* push FT_Prolog */
95 *x++ = 0xC3; /* lret */
96 /* fill rest with 0xCC / int 3 */
99 /***********************************************************************
100 * _write_qtthunk (internal)
101 * Generates a QT_Thunk style call.
103 * 33C9 xor ecx, ecx
104 * 8A4DFC mov cl , [ebp-04]
105 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
106 * B8yyyyyyyy mov eax, QT_Thunk
107 * FFE0 jmp eax
109 static void _write_qtthunk(
110 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
111 DWORD *targetTable /* [in] start of thunk (for index lookup) */
113 LPBYTE x;
115 x = relayCode;
116 *x++ = 0x33;*x++=0xC9; /* xor ecx,ecx */
117 *x++ = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
118 *x++ = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
119 x+=4; /* mov edx, [4*ecx + targetTable */
120 *x++ = 0xB8; *(DWORD*)x = (DWORD)GetProcAddress(GetModuleHandleA("KERNEL32"),"QT_Thunk");
121 x+=4; /* mov eax , QT_Thunk */
122 *x++ = 0xFF; *x++ = 0xE0; /* jmp eax */
123 /* should fill the rest of the 32 bytes with 0xCC */
126 /***********************************************************************
127 * _loadthunk
129 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
130 struct ThunkDataCommon *TD32, DWORD checksum)
132 struct ThunkDataCommon *TD16;
133 HMODULE hmod;
134 int ordinal;
136 if ((hmod = LoadLibrary16(module)) <= 32)
138 ERR_(thunk)("(%s, %s, %s): Unable to load '%s', error %d\n",
139 module, func, module32, module, hmod);
140 return 0;
143 if ( !(ordinal = NE_GetOrdinal(hmod, func))
144 || !(TD16 = PTR_SEG_TO_LIN(NE_GetEntryPointEx(hmod, ordinal, FALSE))))
146 ERR_(thunk)("(%s, %s, %s): Unable to find '%s'\n",
147 module, func, module32, func);
148 return 0;
151 if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
153 ERR_(thunk)("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
154 module, func, module32,
155 TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
156 TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
157 return 0;
160 if (TD32 && TD16->checksum != TD32->checksum)
162 ERR_(thunk)("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
163 module, func, module32, TD16->checksum, TD32->checksum);
164 return 0;
167 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
169 ERR_(thunk)("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
170 module, func, module32, *(LPDWORD)TD16, checksum);
171 return 0;
174 return TD16;
177 /***********************************************************************
178 * GetThunkStuff (KERNEL32.53)
180 LPVOID WINAPI GetThunkStuff(LPSTR module, LPSTR func)
182 return _loadthunk(module, func, "<kernel>", NULL, 0L);
185 /***********************************************************************
186 * GetThunkBuff (KERNEL32.52)
187 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
189 LPVOID WINAPI GetThunkBuff(void)
191 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
194 /***********************************************************************
195 * ThunkConnect32 (KERNEL32)
196 * Connects a 32bit and a 16bit thunkbuffer.
198 UINT WINAPI ThunkConnect32(
199 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
200 LPSTR thunkfun16, /* [in] win16 thunkfunction */
201 LPSTR module16, /* [in] name of win16 dll */
202 LPSTR module32, /* [in] name of win32 dll */
203 HMODULE hmod32, /* [in] hmodule of win32 dll */
204 DWORD dwReason /* [in] initialisation argument */
206 BOOL directionSL;
208 if (!strncmp(TD->magic, "SL01", 4))
210 directionSL = TRUE;
212 TRACE_(thunk)("SL01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
213 module32, (DWORD)TD, module16, thunkfun16, dwReason);
215 else if (!strncmp(TD->magic, "LS01", 4))
217 directionSL = FALSE;
219 TRACE_(thunk)("LS01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
220 module32, (DWORD)TD, module16, thunkfun16, dwReason);
222 else
224 ERR_(thunk)("Invalid magic %c%c%c%c\n",
225 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
226 return 0;
229 switch (dwReason)
231 case DLL_PROCESS_ATTACH:
233 struct ThunkDataCommon *TD16;
234 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
235 return 0;
237 if (directionSL)
239 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
240 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
241 struct SLTargetDB *tdb;
243 if (SL16->fpData == NULL)
245 ERR_(thunk)("ThunkConnect16 was not called!\n");
246 return 0;
249 SL32->data = SL16->fpData;
251 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
252 tdb->process = PROCESS_Current();
253 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
255 tdb->next = SL32->data->targetDB; /* FIXME: not thread-safe! */
256 SL32->data->targetDB = tdb;
258 TRACE_(thunk)("Process %08lx allocated TargetDB entry for ThunkDataSL %08lx\n",
259 (DWORD)PROCESS_Current(), (DWORD)SL32->data);
261 else
263 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
264 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
266 LS32->targetTable = PTR_SEG_TO_LIN(LS16->targetTable);
268 /* write QT_Thunk and FT_Prolog stubs */
269 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk, LS32->targetTable);
270 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
272 break;
275 case DLL_PROCESS_DETACH:
276 /* FIXME: cleanup */
277 break;
280 return 1;
283 /**********************************************************************
284 * QT_Thunk (KERNEL32)
286 * The target address is in EDX.
287 * The 16 bit arguments start at ESP.
288 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
289 * [ok]
291 void WINAPI QT_Thunk( CONTEXT86 *context )
293 CONTEXT86 context16;
294 DWORD argsize;
296 memcpy(&context16,context,sizeof(context16));
298 CS_reg(&context16) = HIWORD(EDX_reg(context));
299 EIP_reg(&context16) = LOWORD(EDX_reg(context));
300 EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
301 + (WORD)&((STACK16FRAME*)0)->bp;
303 argsize = EBP_reg(context)-ESP_reg(context)-0x40;
305 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
306 (LPBYTE)ESP_reg(context), argsize );
308 EAX_reg(context) = Callbacks->CallRegisterShortProc( &context16, argsize );
309 EDX_reg(context) = HIWORD(EAX_reg(context));
310 EAX_reg(context) = LOWORD(EAX_reg(context));
314 /**********************************************************************
315 * FT_Prolog (KERNEL32.233)
317 * The set of FT_... thunk routines is used instead of QT_Thunk,
318 * if structures have to be converted from 32-bit to 16-bit
319 * (change of member alignment, conversion of members).
321 * The thunk function (as created by the thunk compiler) calls
322 * FT_Prolog at the beginning, to set up a stack frame and
323 * allocate a 64 byte buffer on the stack.
324 * The input parameters (target address and some flags) are
325 * saved for later use by FT_Thunk.
327 * Input: EDX 16-bit target address (SEGPTR)
328 * CX bits 0..7 target number (in target table)
329 * bits 8..9 some flags (unclear???)
330 * bits 10..15 number of DWORD arguments
332 * Output: A new stackframe is created, and a 64 byte buffer
333 * allocated on the stack. The layout of the stack
334 * on return is as follows:
336 * (ebp+4) return address to caller of thunk function
337 * (ebp) old EBP
338 * (ebp-4) saved EBX register of caller
339 * (ebp-8) saved ESI register of caller
340 * (ebp-12) saved EDI register of caller
341 * (ebp-16) saved ECX register, containing flags
342 * (ebp-20) bitmap containing parameters that are to be converted
343 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
344 * filled in by the thunk code before calling FT_Thunk
345 * (ebp-24)
346 * ... (unclear)
347 * (ebp-44)
348 * (ebp-48) saved EAX register of caller (unclear, never restored???)
349 * (ebp-52) saved EDX register, containing 16-bit thunk target
350 * (ebp-56)
351 * ... (unclear)
352 * (ebp-64)
354 * ESP is EBP-64 after return.
358 void WINAPI FT_Prolog( CONTEXT86 *context )
360 /* Build stack frame */
361 stack32_push(context, EBP_reg(context));
362 EBP_reg(context) = ESP_reg(context);
364 /* Allocate 64-byte Thunk Buffer */
365 ESP_reg(context) -= 64;
366 memset((char *)ESP_reg(context), '\0', 64);
368 /* Store Flags (ECX) and Target Address (EDX) */
369 /* Save other registers to be restored later */
370 *(DWORD *)(EBP_reg(context) - 4) = EBX_reg(context);
371 *(DWORD *)(EBP_reg(context) - 8) = ESI_reg(context);
372 *(DWORD *)(EBP_reg(context) - 12) = EDI_reg(context);
373 *(DWORD *)(EBP_reg(context) - 16) = ECX_reg(context);
375 *(DWORD *)(EBP_reg(context) - 48) = EAX_reg(context);
376 *(DWORD *)(EBP_reg(context) - 52) = EDX_reg(context);
379 /**********************************************************************
380 * FT_Thunk (KERNEL32.234)
382 * This routine performs the actual call to 16-bit code,
383 * similar to QT_Thunk. The differences are:
384 * - The call target is taken from the buffer created by FT_Prolog
385 * - Those arguments requested by the thunk code (by setting the
386 * corresponding bit in the bitmap at EBP-20) are converted
387 * from 32-bit pointers to segmented pointers (those pointers
388 * are guaranteed to point to structures copied to the stack
389 * by the thunk code, so we always use the 16-bit stack selector
390 * for those addresses).
392 * The bit #i of EBP-20 corresponds here to the DWORD starting at
393 * ESP+4 + 2*i.
395 * FIXME: It is unclear what happens if there are more than 32 WORDs
396 * of arguments, so that the single DWORD bitmap is no longer
397 * sufficient ...
400 void WINAPI FT_Thunk( CONTEXT86 *context )
402 DWORD mapESPrelative = *(DWORD *)(EBP_reg(context) - 20);
403 DWORD callTarget = *(DWORD *)(EBP_reg(context) - 52);
405 CONTEXT86 context16;
406 DWORD i, argsize;
407 LPBYTE newstack, oldstack;
409 memcpy(&context16,context,sizeof(context16));
411 CS_reg(&context16) = HIWORD(callTarget);
412 EIP_reg(&context16) = LOWORD(callTarget);
413 EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
414 + (WORD)&((STACK16FRAME*)0)->bp;
416 argsize = EBP_reg(context)-ESP_reg(context)-0x40;
417 newstack = (LPBYTE)CURRENT_STACK16 - argsize;
418 oldstack = (LPBYTE)ESP_reg(context);
420 memcpy( newstack, oldstack, argsize );
422 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
423 if (mapESPrelative & (1 << i))
425 SEGPTR *arg = (SEGPTR *)(newstack + 2*i);
426 *arg = PTR_SEG_OFF_TO_SEGPTR(SELECTOROF(NtCurrentTeb()->cur_stack),
427 OFFSETOF(NtCurrentTeb()->cur_stack) - argsize
428 + (*(LPBYTE *)arg - oldstack));
431 EAX_reg(context) = Callbacks->CallRegisterShortProc( &context16, argsize );
432 EDX_reg(context) = HIWORD(EAX_reg(context));
433 EAX_reg(context) = LOWORD(EAX_reg(context));
435 /* Copy modified buffers back to 32-bit stack */
436 memcpy( oldstack, newstack, argsize );
439 /**********************************************************************
440 * FT_ExitNN (KERNEL32.218 - 232)
442 * One of the FT_ExitNN functions is called at the end of the thunk code.
443 * It removes the stack frame created by FT_Prolog, moves the function
444 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
445 * value, but the thunk code has moved it from EAX to EBX in the
446 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
447 * and perform a return to the CALLER of the thunk code (while removing
448 * the given number of arguments from the caller's stack).
451 static void FT_Exit(CONTEXT86 *context, int nPopArgs)
453 /* Return value is in EBX */
454 EAX_reg(context) = EBX_reg(context);
456 /* Restore EBX, ESI, and EDI registers */
457 EBX_reg(context) = *(DWORD *)(EBP_reg(context) - 4);
458 ESI_reg(context) = *(DWORD *)(EBP_reg(context) - 8);
459 EDI_reg(context) = *(DWORD *)(EBP_reg(context) - 12);
461 /* Clean up stack frame */
462 ESP_reg(context) = EBP_reg(context);
463 EBP_reg(context) = stack32_pop(context);
465 /* Pop return address to CALLER of thunk code */
466 EIP_reg(context) = stack32_pop(context);
467 /* Remove arguments */
468 ESP_reg(context) += nPopArgs;
471 void WINAPI FT_Exit0 (CONTEXT86 *context) { FT_Exit(context, 0); }
472 void WINAPI FT_Exit4 (CONTEXT86 *context) { FT_Exit(context, 4); }
473 void WINAPI FT_Exit8 (CONTEXT86 *context) { FT_Exit(context, 8); }
474 void WINAPI FT_Exit12(CONTEXT86 *context) { FT_Exit(context, 12); }
475 void WINAPI FT_Exit16(CONTEXT86 *context) { FT_Exit(context, 16); }
476 void WINAPI FT_Exit20(CONTEXT86 *context) { FT_Exit(context, 20); }
477 void WINAPI FT_Exit24(CONTEXT86 *context) { FT_Exit(context, 24); }
478 void WINAPI FT_Exit28(CONTEXT86 *context) { FT_Exit(context, 28); }
479 void WINAPI FT_Exit32(CONTEXT86 *context) { FT_Exit(context, 32); }
480 void WINAPI FT_Exit36(CONTEXT86 *context) { FT_Exit(context, 36); }
481 void WINAPI FT_Exit40(CONTEXT86 *context) { FT_Exit(context, 40); }
482 void WINAPI FT_Exit44(CONTEXT86 *context) { FT_Exit(context, 44); }
483 void WINAPI FT_Exit48(CONTEXT86 *context) { FT_Exit(context, 48); }
484 void WINAPI FT_Exit52(CONTEXT86 *context) { FT_Exit(context, 52); }
485 void WINAPI FT_Exit56(CONTEXT86 *context) { FT_Exit(context, 56); }
488 /***********************************************************************
489 * ThunkInitLS (KERNEL32.43)
490 * A thunkbuffer link routine
491 * The thunkbuf looks like:
493 * 00: DWORD length ? don't know exactly
494 * 04: SEGPTR ptr ? where does it point to?
495 * The pointer ptr is written into the first DWORD of 'thunk'.
496 * (probably correct implemented)
497 * [ok probably]
498 * RETURNS
499 * segmented pointer to thunk?
501 DWORD WINAPI ThunkInitLS(
502 LPDWORD thunk, /* [in] win32 thunk */
503 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
504 DWORD len, /* [in] thkbuffer length */
505 LPCSTR dll16, /* [in] name of win16 dll */
506 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
508 LPDWORD addr;
510 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
511 return 0;
513 if (!addr[1])
514 return 0;
515 *(DWORD*)thunk = addr[1];
517 return addr[1];
520 /***********************************************************************
521 * Common32ThkLS (KERNEL32.45)
523 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
524 * style thunks. The basic difference is that the parameter conversion
525 * is done completely on the *16-bit* side here. Thus we do not call
526 * the 16-bit target directly, but call a common entry point instead.
527 * This entry function then calls the target according to the target
528 * number passed in the DI register.
530 * Input: EAX SEGPTR to the common 16-bit entry point
531 * CX offset in thunk table (target number * 4)
532 * DX error return value if execution fails (unclear???)
533 * EDX.HI number of DWORD parameters
535 * (Note that we need to move the thunk table offset from CX to DI !)
537 * The called 16-bit stub expects its stack to look like this:
538 * ...
539 * (esp+40) 32-bit arguments
540 * ...
541 * (esp+8) 32 byte of stack space available as buffer
542 * (esp) 8 byte return address for use with 0x66 lret
544 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
545 * and uses the EAX register to return a DWORD return value.
546 * Thus we need to use a special assembly glue routine
547 * (CallRegisterLongProc instead of CallRegisterShortProc).
549 * Finally, we return to the caller, popping the arguments off
550 * the stack.
552 * FIXME: The called function uses EBX to return the number of
553 * arguments that are to be popped off the caller's stack.
554 * This is clobbered by the assembly glue, so we simply use
555 * the original EDX.HI to get the number of arguments.
556 * (Those two values should be equal anyway ...?)
559 void WINAPI Common32ThkLS( CONTEXT86 *context )
561 CONTEXT86 context16;
562 DWORD argsize;
564 memcpy(&context16,context,sizeof(context16));
566 DI_reg(&context16) = CX_reg(context);
567 CS_reg(&context16) = HIWORD(EAX_reg(context));
568 EIP_reg(&context16) = LOWORD(EAX_reg(context));
569 EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
570 + (WORD)&((STACK16FRAME*)0)->bp;
572 argsize = HIWORD(EDX_reg(context)) * 4;
574 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
575 if (EDX_reg(context) == EIP_reg(context))
576 argsize = 6 * 4;
578 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
579 (LPBYTE)ESP_reg(context), argsize );
581 EAX_reg(context) = Callbacks->CallRegisterLongProc(&context16, argsize + 32);
583 /* Clean up caller's stack frame */
584 ESP_reg(context) += argsize;
587 /***********************************************************************
588 * OT_32ThkLSF (KERNEL32.40)
590 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
591 * argument processing is done on both the 32-bit and the 16-bit side:
592 * The 32-bit side prepares arguments, copying them onto the stack.
594 * When this routine is called, the first word on the stack is the
595 * number of argument bytes prepared by the 32-bit code, and EDX
596 * contains the 16-bit target address.
598 * The called 16-bit routine is another relaycode, doing further
599 * argument processing and then calling the real 16-bit target
600 * whose address is stored at [bp-04].
602 * The call proceeds using a normal CallRegisterShortProc.
603 * After return from the 16-bit relaycode, the arguments need
604 * to be copied *back* to the 32-bit stack, since the 32-bit
605 * relaycode processes output parameters.
607 * Note that we copy twice the number of arguments, since some of the
608 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
609 * arguments of the caller!
611 * (Note that this function seems only to be used for
612 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
614 void WINAPI OT_32ThkLSF( CONTEXT86 *context )
616 CONTEXT86 context16;
617 DWORD argsize;
619 memcpy(&context16,context,sizeof(context16));
621 CS_reg(&context16) = HIWORD(EDX_reg(context));
622 EIP_reg(&context16) = LOWORD(EDX_reg(context));
623 EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
624 + (WORD)&((STACK16FRAME*)0)->bp;
626 argsize = 2 * *(WORD *)ESP_reg(context) + 2;
628 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
629 (LPBYTE)ESP_reg(context), argsize );
631 EAX_reg(context) = Callbacks->CallRegisterShortProc(&context16, argsize);
633 memcpy( (LPBYTE)ESP_reg(context),
634 (LPBYTE)CURRENT_STACK16 - argsize, argsize );
637 /***********************************************************************
638 * ThunkInitLSF (KERNEL32.41)
639 * A thunk setup routine.
640 * Expects a pointer to a preinitialized thunkbuffer in the first argument
641 * looking like:
642 * 00..03: unknown (pointer, check _41, _43, _46)
643 * 04: EB1E jmp +0x20
645 * 06..23: unknown (space for replacement code, check .90)
647 * 24:>E800000000 call offset 29
648 * 29:>58 pop eax ( target of call )
649 * 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
650 * 2F: BAxxxxxxxx mov edx,xxxxxxxx
651 * 34: 68yyyyyyyy push KERNEL32.90
652 * 39: C3 ret
654 * 3A: EB1E jmp +0x20
655 * 3E ... 59: unknown (space for replacement code?)
656 * 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
657 * 5F: 5A pop edx
658 * 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
659 * 66: 52 push edx
660 * 67: 68xxxxxxxx push xxxxxxxx
661 * 6C: 68yyyyyyyy push KERNEL32.89
662 * 71: C3 ret
663 * 72: end?
664 * This function checks if the code is there, and replaces the yyyyyyyy entries
665 * by the functionpointers.
666 * The thunkbuf looks like:
668 * 00: DWORD length ? don't know exactly
669 * 04: SEGPTR ptr ? where does it point to?
670 * The segpointer ptr is written into the first DWORD of 'thunk'.
671 * [ok probably]
672 * RETURNS
673 * unclear, pointer to win16 thkbuffer?
675 LPVOID WINAPI ThunkInitLSF(
676 LPBYTE thunk, /* [in] win32 thunk */
677 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
678 DWORD len, /* [in] length of thkbuffer */
679 LPCSTR dll16, /* [in] name of win16 dll */
680 LPCSTR dll32 /* [in] name of win32 dll */
682 HMODULE hkrnl32 = GetModuleHandleA("KERNEL32");
683 LPDWORD addr,addr2;
685 /* FIXME: add checks for valid code ... */
686 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
687 *(DWORD*)(thunk+0x35) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)90);
688 *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)89);
691 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
692 return 0;
694 addr2 = PTR_SEG_TO_LIN(addr[1]);
695 if (HIWORD(addr2))
696 *(DWORD*)thunk = (DWORD)addr2;
698 return addr2;
701 /***********************************************************************
702 * FT_PrologPrime (KERNEL32.89)
704 * This function is called from the relay code installed by
705 * ThunkInitLSF. It replaces the location from where it was
706 * called by a standard FT_Prolog call stub (which is 'primed'
707 * by inserting the correct target table pointer).
708 * Finally, it calls that stub.
710 * Input: ECX target number + flags (passed through to FT_Prolog)
711 * (ESP) offset of location where target table pointer
712 * is stored, relative to the start of the relay code
713 * (ESP+4) pointer to start of relay code
714 * (this is where the FT_Prolog call stub gets written to)
716 * Note: The two DWORD arguments get popped off the stack.
719 void WINAPI FT_PrologPrime( CONTEXT86 *context )
721 DWORD targetTableOffset;
722 LPBYTE relayCode;
724 /* Compensate for the fact that the Wine register relay code thought
725 we were being called, although we were in fact jumped to */
726 ESP_reg(context) -= 4;
728 /* Write FT_Prolog call stub */
729 targetTableOffset = stack32_pop(context);
730 relayCode = (LPBYTE)stack32_pop(context);
731 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
733 /* Jump to the call stub just created */
734 EIP_reg(context) = (DWORD)relayCode;
737 /***********************************************************************
738 * QT_ThunkPrime (KERNEL32.90)
740 * This function corresponds to FT_PrologPrime, but installs a
741 * call stub for QT_Thunk instead.
743 * Input: (EBP-4) target number (passed through to QT_Thunk)
744 * EDX target table pointer location offset
745 * EAX start of relay code
748 void WINAPI QT_ThunkPrime( CONTEXT86 *context )
750 DWORD targetTableOffset;
751 LPBYTE relayCode;
753 /* Compensate for the fact that the Wine register relay code thought
754 we were being called, although we were in fact jumped to */
755 ESP_reg(context) -= 4;
757 /* Write QT_Thunk call stub */
758 targetTableOffset = EDX_reg(context);
759 relayCode = (LPBYTE)EAX_reg(context);
760 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
762 /* Jump to the call stub just created */
763 EIP_reg(context) = (DWORD)relayCode;
766 /***********************************************************************
767 * (KERNEL32.46)
768 * Another thunkbuf link routine.
769 * The start of the thunkbuf looks like this:
770 * 00: DWORD length
771 * 04: SEGPTR address for thunkbuffer pointer
772 * [ok probably]
774 VOID WINAPI ThunkInitSL(
775 LPBYTE thunk, /* [in] start of thunkbuffer */
776 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
777 DWORD len, /* [in] length of thunkbuffer */
778 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
779 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
781 LPDWORD addr;
783 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
784 return;
786 *(DWORD*)PTR_SEG_TO_LIN(addr[1]) = (DWORD)thunk;
789 /**********************************************************************
790 * SSInit KERNEL.700
791 * RETURNS
792 * TRUE for success.
794 BOOL WINAPI SSInit16()
796 return TRUE;
799 /**********************************************************************
800 * SSOnBigStack KERNEL32.87
801 * Check if thunking is initialized (ss selector set up etc.)
802 * We do that differently, so just return TRUE.
803 * [ok]
804 * RETURNS
805 * TRUE for success.
807 BOOL WINAPI SSOnBigStack()
809 TRACE_(thunk)("Yes, thunking is initialized\n");
810 return TRUE;
813 /**********************************************************************
814 * SSConfirmSmallStack KERNEL.704
816 * Abort if not on small stack.
818 * This must be a register routine as it has to preserve *all* registers.
820 void WINAPI SSConfirmSmallStack( CONTEXT86 *context )
822 /* We are always on the small stack while in 16-bit code ... */
825 /**********************************************************************
826 * SSCall
827 * One of the real thunking functions. This one seems to be for 32<->32
828 * thunks. It should probably be capable of crossing processboundaries.
830 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
831 * [ok]
833 DWORD WINAPIV SSCall(
834 DWORD nr, /* [in] number of argument bytes */
835 DWORD flags, /* [in] FIXME: flags ? */
836 FARPROC fun, /* [in] function to call */
837 ... /* [in/out] arguments */
839 DWORD i,ret;
840 DWORD *args = ((DWORD *)&fun) + 1;
842 if(TRACE_ON(thunk))
844 DPRINTF("(%ld,0x%08lx,%p,[",nr,flags,fun);
845 for (i=0;i<nr/4;i++)
846 DPRINTF("0x%08lx,",args[i]);
847 DPRINTF("])\n");
849 switch (nr) {
850 case 0: ret = fun();
851 break;
852 case 4: ret = fun(args[0]);
853 break;
854 case 8: ret = fun(args[0],args[1]);
855 break;
856 case 12: ret = fun(args[0],args[1],args[2]);
857 break;
858 case 16: ret = fun(args[0],args[1],args[2],args[3]);
859 break;
860 case 20: ret = fun(args[0],args[1],args[2],args[3],args[4]);
861 break;
862 case 24: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
863 break;
864 case 28: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
865 break;
866 case 32: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
867 break;
868 case 36: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
869 break;
870 case 40: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
871 break;
872 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]);
873 break;
874 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]);
875 break;
876 default:
877 WARN_(thunk)("Unsupported nr of arguments, %ld\n",nr);
878 ret = 0;
879 break;
882 TRACE_(thunk)(" returning %ld ...\n",ret);
883 return ret;
886 /**********************************************************************
887 * W32S_BackTo32 (KERNEL32.51)
889 void WINAPI W32S_BackTo32( CONTEXT86 *context )
891 LPDWORD stack = (LPDWORD)ESP_reg( context );
892 FARPROC proc = (FARPROC)EIP_reg(context);
894 EAX_reg( context ) = proc( stack[1], stack[2], stack[3], stack[4], stack[5],
895 stack[6], stack[7], stack[8], stack[9], stack[10] );
897 EIP_reg( context ) = stack32_pop(context);
900 /**********************************************************************
901 * AllocSLCallback (KERNEL32)
903 * Win95 uses some structchains for callbacks. It allocates them
904 * in blocks of 100 entries, size 32 bytes each, layout:
905 * blockstart:
906 * 0: PTR nextblockstart
907 * 4: entry *first;
908 * 8: WORD sel ( start points to blockstart)
909 * A: WORD unknown
910 * 100xentry:
911 * 00..17: Code
912 * 18: PDB *owning_process;
913 * 1C: PTR blockstart
915 * We ignore this for now. (Just a note for further developers)
916 * FIXME: use this method, so we don't waste selectors...
918 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
919 * the 0x66 prefix switches from word->long registers.
921 * 665A pop edx
922 * 6668x arg2 x pushl <arg2>
923 * 6652 push edx
924 * EAx arg1 x jmpf <arg1>
926 * returns the startaddress of this thunk.
928 * Note, that they look very similair to the ones allocates by THUNK_Alloc.
929 * RETURNS
930 * segmented pointer to the start of the thunk
932 DWORD WINAPI
933 AllocSLCallback(
934 DWORD finalizer, /* [in] finalizer function */
935 DWORD callback /* [in] callback function */
937 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
938 WORD sel;
940 x=thunk;
941 *x++=0x66;*x++=0x5a; /* popl edx */
942 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
943 *x++=0x66;*x++=0x52; /* pushl edx */
944 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
946 *(PDB**)(thunk+18) = PROCESS_Current();
948 sel = SELECTOR_AllocBlock( thunk , 32, SEGMENT_CODE, FALSE, FALSE );
949 return (sel<<16)|0;
952 /**********************************************************************
953 * FreeSLCallback (KERNEL32.274)
954 * Frees the specified 16->32 callback
956 void WINAPI
957 FreeSLCallback(
958 DWORD x /* [in] 16 bit callback (segmented pointer?) */
960 FIXME_(win32)("(0x%08lx): stub\n",x);
964 /**********************************************************************
965 * GetTEBSelectorFS (KERNEL.475)
966 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
968 void WINAPI GetTEBSelectorFS16(void)
970 GET_FS( CURRENT_STACK16->fs );
973 /**********************************************************************
974 * KERNEL_431 (KERNEL.431)
975 * IsPeFormat (W32SYS.2)
976 * Checks the passed filename if it is a PE format executeable
977 * RETURNS
978 * TRUE, if it is.
979 * FALSE if not.
981 BOOL16 WINAPI IsPeFormat16(
982 LPSTR fn, /* [in] filename to executeable */
983 HFILE16 hf16 /* [in] open file, if filename is NULL */
985 IMAGE_DOS_HEADER mzh;
986 HFILE hf=FILE_GetHandle(hf16);
987 OFSTRUCT ofs;
988 DWORD xmagic;
990 if (fn) {
991 hf = OpenFile(fn,&ofs,OF_READ);
992 if (hf==HFILE_ERROR)
993 return FALSE;
995 _llseek(hf,0,SEEK_SET);
996 if (sizeof(mzh)!=_lread(hf,&mzh,sizeof(mzh))) {
997 _lclose(hf);
998 return FALSE;
1000 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) {
1001 WARN_(dosmem)("File has not got dos signature!\n");
1002 _lclose(hf);
1003 return FALSE;
1005 _llseek(hf,mzh.e_lfanew,SEEK_SET);
1006 if (sizeof(DWORD)!=_lread(hf,&xmagic,sizeof(DWORD))) {
1007 _lclose(hf);
1008 return FALSE;
1010 _lclose(hf);
1011 return (xmagic == IMAGE_NT_SIGNATURE);
1015 /***********************************************************************
1016 * K32Thk1632Prolog (KERNEL32.492)
1018 void WINAPI K32Thk1632Prolog( CONTEXT86 *context )
1020 LPBYTE code = (LPBYTE)EIP_reg(context) - 5;
1022 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1023 of 16->32 thunks instead of using one of the standard methods!
1024 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1025 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1026 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1027 bypassed, which means it will crash the next time the 32-bit OLE
1028 code thunks down again to 16-bit (this *will* happen!).
1030 The following hack tries to recognize this situation.
1031 This is possible since the called stubs in OLECLI32/OLESVR32 all
1032 look exactly the same:
1033 00 E8xxxxxxxx call K32Thk1632Prolog
1034 05 FF55FC call [ebp-04]
1035 08 E8xxxxxxxx call K32Thk1632Epilog
1036 0D 66CB retf
1038 If we recognize this situation, we try to simulate the actions
1039 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1040 to our 32-bit stack, creating a proper STACK16FRAME and
1041 updating cur_stack. */
1043 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1044 && code[13] == 0x66 && code[14] == 0xCB)
1046 WORD stackSel = NtCurrentTeb()->stack_sel;
1047 DWORD stackBase = GetSelectorBase(stackSel);
1049 DWORD argSize = EBP_reg(context) - ESP_reg(context);
1050 char *stack16 = (char *)ESP_reg(context) - 4;
1051 char *stack32 = (char *)NtCurrentTeb()->cur_stack - argSize;
1052 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1054 TRACE_(thunk)("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1055 EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1057 memset(frame16, '\0', sizeof(STACK16FRAME));
1058 frame16->frame32 = (STACK32FRAME *)NtCurrentTeb()->cur_stack;
1059 frame16->ebp = EBP_reg(context);
1061 memcpy(stack32, stack16, argSize);
1062 NtCurrentTeb()->cur_stack = PTR_SEG_OFF_TO_SEGPTR(stackSel, (DWORD)frame16 - stackBase);
1064 ESP_reg(context) = (DWORD)stack32 + 4;
1065 EBP_reg(context) = ESP_reg(context) + argSize;
1067 TRACE_(thunk)("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1068 EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1071 SYSLEVEL_ReleaseWin16Lock();
1074 /***********************************************************************
1075 * K32Thk1632Epilog (KERNEL32.491)
1077 void WINAPI K32Thk1632Epilog( CONTEXT86 *context )
1079 LPBYTE code = (LPBYTE)EIP_reg(context) - 13;
1081 SYSLEVEL_RestoreWin16Lock();
1083 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1085 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1086 && code[13] == 0x66 && code[14] == 0xCB)
1088 STACK16FRAME *frame16 = (STACK16FRAME *)PTR_SEG_TO_LIN(NtCurrentTeb()->cur_stack);
1089 char *stack16 = (char *)(frame16 + 1);
1090 DWORD argSize = frame16->ebp - (DWORD)stack16;
1091 char *stack32 = (char *)frame16->frame32 - argSize;
1093 DWORD nArgsPopped = ESP_reg(context) - (DWORD)stack32;
1095 TRACE_(thunk)("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1096 EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1098 NtCurrentTeb()->cur_stack = (DWORD)frame16->frame32;
1100 ESP_reg(context) = (DWORD)stack16 + nArgsPopped;
1101 EBP_reg(context) = frame16->ebp;
1103 TRACE_(thunk)("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1104 EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1108 /***********************************************************************
1109 * UpdateResource32A (KERNEL32.707)
1111 BOOL WINAPI UpdateResourceA(
1112 HANDLE hUpdate,
1113 LPCSTR lpType,
1114 LPCSTR lpName,
1115 WORD wLanguage,
1116 LPVOID lpData,
1117 DWORD cbData) {
1119 FIXME_(win32)(": stub\n");
1120 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1121 return FALSE;
1124 /***********************************************************************
1125 * UpdateResource32W (KERNEL32.708)
1127 BOOL WINAPI UpdateResourceW(
1128 HANDLE hUpdate,
1129 LPCWSTR lpType,
1130 LPCWSTR lpName,
1131 WORD wLanguage,
1132 LPVOID lpData,
1133 DWORD cbData) {
1135 FIXME_(win32)(": stub\n");
1136 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1137 return FALSE;
1141 /***********************************************************************
1142 * WaitNamedPipe32A [KERNEL32.725]
1144 BOOL WINAPI WaitNamedPipeA (LPCSTR lpNamedPipeName, DWORD nTimeOut)
1145 { FIXME_(win32)("%s 0x%08lx\n",lpNamedPipeName,nTimeOut);
1146 SetLastError(ERROR_PIPE_NOT_CONNECTED);
1147 return FALSE;
1149 /***********************************************************************
1150 * WaitNamedPipe32W [KERNEL32.726]
1152 BOOL WINAPI WaitNamedPipeW (LPCWSTR lpNamedPipeName, DWORD nTimeOut)
1153 { FIXME_(win32)("%s 0x%08lx\n",debugstr_w(lpNamedPipeName),nTimeOut);
1154 SetLastError(ERROR_PIPE_NOT_CONNECTED);
1155 return FALSE;
1158 /*********************************************************************
1159 * PK16FNF [KERNEL32.91]
1161 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1162 * string buffer with the 8.3 filename of a recently loaded 16-bit
1163 * module. It is unknown exactly what modules trigger this
1164 * mechanism or what purpose this serves. Win98 Explorer (and
1165 * probably also Win95 with IE 4 shell integration) calls this
1166 * several times during initialization.
1168 * FIXME: find out what this really does and make it work.
1170 void WINAPI PK16FNF(LPSTR strPtr)
1172 FIXME_(win32)("(%p): stub\n", strPtr);
1174 /* fill in a fake filename that'll be easy to recognize */
1175 lstrcpyA(strPtr, "WINESTUB.FIX");