Moved idle event handling to the server.
[wine.git] / win32 / kernel32.c
blob8b391e7aa745a7a70062177e1a64d204202b749f
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 /***********************************************************************
473 * FT_Exit0 (KERNEL32.218)
475 void WINAPI FT_Exit0 (CONTEXT86 *context) { FT_Exit(context, 0); }
477 /***********************************************************************
478 * FT_Exit4 (KERNEL32.219)
480 void WINAPI FT_Exit4 (CONTEXT86 *context) { FT_Exit(context, 4); }
482 /***********************************************************************
483 * FT_Exit8 (KERNEL32.220)
485 void WINAPI FT_Exit8 (CONTEXT86 *context) { FT_Exit(context, 8); }
487 /***********************************************************************
488 * FT_Exit12 (KERNEL32.221)
490 void WINAPI FT_Exit12(CONTEXT86 *context) { FT_Exit(context, 12); }
492 /***********************************************************************
493 * FT_Exit16 (KERNEL32.222)
495 void WINAPI FT_Exit16(CONTEXT86 *context) { FT_Exit(context, 16); }
497 /***********************************************************************
498 * FT_Exit20 (KERNEL32.223)
500 void WINAPI FT_Exit20(CONTEXT86 *context) { FT_Exit(context, 20); }
502 /***********************************************************************
503 * FT_Exit24 (KERNEL32.224)
505 void WINAPI FT_Exit24(CONTEXT86 *context) { FT_Exit(context, 24); }
507 /***********************************************************************
508 * FT_Exit28 (KERNEL32.225)
510 void WINAPI FT_Exit28(CONTEXT86 *context) { FT_Exit(context, 28); }
512 /***********************************************************************
513 * FT_Exit32 (KERNEL32.226)
515 void WINAPI FT_Exit32(CONTEXT86 *context) { FT_Exit(context, 32); }
517 /***********************************************************************
518 * FT_Exit36 (KERNEL32.227)
520 void WINAPI FT_Exit36(CONTEXT86 *context) { FT_Exit(context, 36); }
522 /***********************************************************************
523 * FT_Exit40 (KERNEL32.228)
525 void WINAPI FT_Exit40(CONTEXT86 *context) { FT_Exit(context, 40); }
527 /***********************************************************************
528 * FT_Exit44 (KERNEL32.229)
530 void WINAPI FT_Exit44(CONTEXT86 *context) { FT_Exit(context, 44); }
532 /***********************************************************************
533 * FT_Exit48 (KERNEL32.230)
535 void WINAPI FT_Exit48(CONTEXT86 *context) { FT_Exit(context, 48); }
537 /***********************************************************************
538 * FT_Exit52 (KERNEL32.231)
540 void WINAPI FT_Exit52(CONTEXT86 *context) { FT_Exit(context, 52); }
542 /***********************************************************************
543 * FT_Exit56 (KERNEL32.232)
545 void WINAPI FT_Exit56(CONTEXT86 *context) { FT_Exit(context, 56); }
547 /***********************************************************************
548 * ThunkInitLS (KERNEL32.43)
549 * A thunkbuffer link routine
550 * The thunkbuf looks like:
552 * 00: DWORD length ? don't know exactly
553 * 04: SEGPTR ptr ? where does it point to?
554 * The pointer ptr is written into the first DWORD of 'thunk'.
555 * (probably correct implemented)
556 * [ok probably]
557 * RETURNS
558 * segmented pointer to thunk?
560 DWORD WINAPI ThunkInitLS(
561 LPDWORD thunk, /* [in] win32 thunk */
562 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
563 DWORD len, /* [in] thkbuffer length */
564 LPCSTR dll16, /* [in] name of win16 dll */
565 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
567 LPDWORD addr;
569 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
570 return 0;
572 if (!addr[1])
573 return 0;
574 *(DWORD*)thunk = addr[1];
576 return addr[1];
579 /***********************************************************************
580 * Common32ThkLS (KERNEL32.45)
582 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
583 * style thunks. The basic difference is that the parameter conversion
584 * is done completely on the *16-bit* side here. Thus we do not call
585 * the 16-bit target directly, but call a common entry point instead.
586 * This entry function then calls the target according to the target
587 * number passed in the DI register.
589 * Input: EAX SEGPTR to the common 16-bit entry point
590 * CX offset in thunk table (target number * 4)
591 * DX error return value if execution fails (unclear???)
592 * EDX.HI number of DWORD parameters
594 * (Note that we need to move the thunk table offset from CX to DI !)
596 * The called 16-bit stub expects its stack to look like this:
597 * ...
598 * (esp+40) 32-bit arguments
599 * ...
600 * (esp+8) 32 byte of stack space available as buffer
601 * (esp) 8 byte return address for use with 0x66 lret
603 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
604 * and uses the EAX register to return a DWORD return value.
605 * Thus we need to use a special assembly glue routine
606 * (CallRegisterLongProc instead of CallRegisterShortProc).
608 * Finally, we return to the caller, popping the arguments off
609 * the stack.
611 * FIXME: The called function uses EBX to return the number of
612 * arguments that are to be popped off the caller's stack.
613 * This is clobbered by the assembly glue, so we simply use
614 * the original EDX.HI to get the number of arguments.
615 * (Those two values should be equal anyway ...?)
618 void WINAPI Common32ThkLS( CONTEXT86 *context )
620 CONTEXT86 context16;
621 DWORD argsize;
623 memcpy(&context16,context,sizeof(context16));
625 DI_reg(&context16) = CX_reg(context);
626 CS_reg(&context16) = HIWORD(EAX_reg(context));
627 EIP_reg(&context16) = LOWORD(EAX_reg(context));
628 EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
629 + (WORD)&((STACK16FRAME*)0)->bp;
631 argsize = HIWORD(EDX_reg(context)) * 4;
633 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
634 if (EDX_reg(context) == EIP_reg(context))
635 argsize = 6 * 4;
637 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
638 (LPBYTE)ESP_reg(context), argsize );
640 EAX_reg(context) = Callbacks->CallRegisterLongProc(&context16, argsize + 32);
642 /* Clean up caller's stack frame */
643 ESP_reg(context) += argsize;
646 /***********************************************************************
647 * OT_32ThkLSF (KERNEL32.40)
649 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
650 * argument processing is done on both the 32-bit and the 16-bit side:
651 * The 32-bit side prepares arguments, copying them onto the stack.
653 * When this routine is called, the first word on the stack is the
654 * number of argument bytes prepared by the 32-bit code, and EDX
655 * contains the 16-bit target address.
657 * The called 16-bit routine is another relaycode, doing further
658 * argument processing and then calling the real 16-bit target
659 * whose address is stored at [bp-04].
661 * The call proceeds using a normal CallRegisterShortProc.
662 * After return from the 16-bit relaycode, the arguments need
663 * to be copied *back* to the 32-bit stack, since the 32-bit
664 * relaycode processes output parameters.
666 * Note that we copy twice the number of arguments, since some of the
667 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
668 * arguments of the caller!
670 * (Note that this function seems only to be used for
671 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
673 void WINAPI OT_32ThkLSF( CONTEXT86 *context )
675 CONTEXT86 context16;
676 DWORD argsize;
678 memcpy(&context16,context,sizeof(context16));
680 CS_reg(&context16) = HIWORD(EDX_reg(context));
681 EIP_reg(&context16) = LOWORD(EDX_reg(context));
682 EBP_reg(&context16) = OFFSETOF( NtCurrentTeb()->cur_stack )
683 + (WORD)&((STACK16FRAME*)0)->bp;
685 argsize = 2 * *(WORD *)ESP_reg(context) + 2;
687 memcpy( (LPBYTE)CURRENT_STACK16 - argsize,
688 (LPBYTE)ESP_reg(context), argsize );
690 EAX_reg(context) = Callbacks->CallRegisterShortProc(&context16, argsize);
692 memcpy( (LPBYTE)ESP_reg(context),
693 (LPBYTE)CURRENT_STACK16 - argsize, argsize );
696 /***********************************************************************
697 * ThunkInitLSF (KERNEL32.41)
698 * A thunk setup routine.
699 * Expects a pointer to a preinitialized thunkbuffer in the first argument
700 * looking like:
701 * 00..03: unknown (pointer, check _41, _43, _46)
702 * 04: EB1E jmp +0x20
704 * 06..23: unknown (space for replacement code, check .90)
706 * 24:>E800000000 call offset 29
707 * 29:>58 pop eax ( target of call )
708 * 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
709 * 2F: BAxxxxxxxx mov edx,xxxxxxxx
710 * 34: 68yyyyyyyy push KERNEL32.90
711 * 39: C3 ret
713 * 3A: EB1E jmp +0x20
714 * 3E ... 59: unknown (space for replacement code?)
715 * 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
716 * 5F: 5A pop edx
717 * 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
718 * 66: 52 push edx
719 * 67: 68xxxxxxxx push xxxxxxxx
720 * 6C: 68yyyyyyyy push KERNEL32.89
721 * 71: C3 ret
722 * 72: end?
723 * This function checks if the code is there, and replaces the yyyyyyyy entries
724 * by the functionpointers.
725 * The thunkbuf looks like:
727 * 00: DWORD length ? don't know exactly
728 * 04: SEGPTR ptr ? where does it point to?
729 * The segpointer ptr is written into the first DWORD of 'thunk'.
730 * [ok probably]
731 * RETURNS
732 * unclear, pointer to win16 thkbuffer?
734 LPVOID WINAPI ThunkInitLSF(
735 LPBYTE thunk, /* [in] win32 thunk */
736 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
737 DWORD len, /* [in] length of thkbuffer */
738 LPCSTR dll16, /* [in] name of win16 dll */
739 LPCSTR dll32 /* [in] name of win32 dll */
741 HMODULE hkrnl32 = GetModuleHandleA("KERNEL32");
742 LPDWORD addr,addr2;
744 /* FIXME: add checks for valid code ... */
745 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
746 *(DWORD*)(thunk+0x35) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)90);
747 *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)89);
750 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
751 return 0;
753 addr2 = PTR_SEG_TO_LIN(addr[1]);
754 if (HIWORD(addr2))
755 *(DWORD*)thunk = (DWORD)addr2;
757 return addr2;
760 /***********************************************************************
761 * FT_PrologPrime (KERNEL32.89)
763 * This function is called from the relay code installed by
764 * ThunkInitLSF. It replaces the location from where it was
765 * called by a standard FT_Prolog call stub (which is 'primed'
766 * by inserting the correct target table pointer).
767 * Finally, it calls that stub.
769 * Input: ECX target number + flags (passed through to FT_Prolog)
770 * (ESP) offset of location where target table pointer
771 * is stored, relative to the start of the relay code
772 * (ESP+4) pointer to start of relay code
773 * (this is where the FT_Prolog call stub gets written to)
775 * Note: The two DWORD arguments get popped off the stack.
778 void WINAPI FT_PrologPrime( CONTEXT86 *context )
780 DWORD targetTableOffset;
781 LPBYTE relayCode;
783 /* Compensate for the fact that the Wine register relay code thought
784 we were being called, although we were in fact jumped to */
785 ESP_reg(context) -= 4;
787 /* Write FT_Prolog call stub */
788 targetTableOffset = stack32_pop(context);
789 relayCode = (LPBYTE)stack32_pop(context);
790 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
792 /* Jump to the call stub just created */
793 EIP_reg(context) = (DWORD)relayCode;
796 /***********************************************************************
797 * QT_ThunkPrime (KERNEL32.90)
799 * This function corresponds to FT_PrologPrime, but installs a
800 * call stub for QT_Thunk instead.
802 * Input: (EBP-4) target number (passed through to QT_Thunk)
803 * EDX target table pointer location offset
804 * EAX start of relay code
807 void WINAPI QT_ThunkPrime( CONTEXT86 *context )
809 DWORD targetTableOffset;
810 LPBYTE relayCode;
812 /* Compensate for the fact that the Wine register relay code thought
813 we were being called, although we were in fact jumped to */
814 ESP_reg(context) -= 4;
816 /* Write QT_Thunk call stub */
817 targetTableOffset = EDX_reg(context);
818 relayCode = (LPBYTE)EAX_reg(context);
819 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
821 /* Jump to the call stub just created */
822 EIP_reg(context) = (DWORD)relayCode;
825 /***********************************************************************
826 * ThunkInitSL (KERNEL32.46)
827 * Another thunkbuf link routine.
828 * The start of the thunkbuf looks like this:
829 * 00: DWORD length
830 * 04: SEGPTR address for thunkbuffer pointer
831 * [ok probably]
833 VOID WINAPI ThunkInitSL(
834 LPBYTE thunk, /* [in] start of thunkbuffer */
835 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
836 DWORD len, /* [in] length of thunkbuffer */
837 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
838 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
840 LPDWORD addr;
842 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
843 return;
845 *(DWORD*)PTR_SEG_TO_LIN(addr[1]) = (DWORD)thunk;
848 /**********************************************************************
849 * SSInit KERNEL.700
850 * RETURNS
851 * TRUE for success.
853 BOOL WINAPI SSInit16()
855 return TRUE;
858 /**********************************************************************
859 * SSOnBigStack KERNEL32.87
860 * Check if thunking is initialized (ss selector set up etc.)
861 * We do that differently, so just return TRUE.
862 * [ok]
863 * RETURNS
864 * TRUE for success.
866 BOOL WINAPI SSOnBigStack()
868 TRACE("Yes, thunking is initialized\n");
869 return TRUE;
872 /**********************************************************************
873 * SSConfirmSmallStack KERNEL.704
875 * Abort if not on small stack.
877 * This must be a register routine as it has to preserve *all* registers.
879 void WINAPI SSConfirmSmallStack( CONTEXT86 *context )
881 /* We are always on the small stack while in 16-bit code ... */
884 /**********************************************************************
885 * SSCall
886 * One of the real thunking functions. This one seems to be for 32<->32
887 * thunks. It should probably be capable of crossing processboundaries.
889 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
890 * [ok]
892 DWORD WINAPIV SSCall(
893 DWORD nr, /* [in] number of argument bytes */
894 DWORD flags, /* [in] FIXME: flags ? */
895 FARPROC fun, /* [in] function to call */
896 ... /* [in/out] arguments */
898 DWORD i,ret;
899 DWORD *args = ((DWORD *)&fun) + 1;
901 if(TRACE_ON(thunk))
903 DPRINTF("(%ld,0x%08lx,%p,[",nr,flags,fun);
904 for (i=0;i<nr/4;i++)
905 DPRINTF("0x%08lx,",args[i]);
906 DPRINTF("])\n");
908 switch (nr) {
909 case 0: ret = fun();
910 break;
911 case 4: ret = fun(args[0]);
912 break;
913 case 8: ret = fun(args[0],args[1]);
914 break;
915 case 12: ret = fun(args[0],args[1],args[2]);
916 break;
917 case 16: ret = fun(args[0],args[1],args[2],args[3]);
918 break;
919 case 20: ret = fun(args[0],args[1],args[2],args[3],args[4]);
920 break;
921 case 24: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
922 break;
923 case 28: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
924 break;
925 case 32: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
926 break;
927 case 36: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
928 break;
929 case 40: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
930 break;
931 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]);
932 break;
933 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]);
934 break;
935 default:
936 WARN("Unsupported nr of arguments, %ld\n",nr);
937 ret = 0;
938 break;
941 TRACE(" returning %ld ...\n",ret);
942 return ret;
945 /**********************************************************************
946 * W32S_BackTo32 (KERNEL32.51)
948 void WINAPI W32S_BackTo32( CONTEXT86 *context )
950 LPDWORD stack = (LPDWORD)ESP_reg( context );
951 FARPROC proc = (FARPROC)EIP_reg(context);
953 EAX_reg( context ) = proc( stack[1], stack[2], stack[3], stack[4], stack[5],
954 stack[6], stack[7], stack[8], stack[9], stack[10] );
956 EIP_reg( context ) = stack32_pop(context);
959 /**********************************************************************
960 * AllocSLCallback (KERNEL32)
962 * Win95 uses some structchains for callbacks. It allocates them
963 * in blocks of 100 entries, size 32 bytes each, layout:
964 * blockstart:
965 * 0: PTR nextblockstart
966 * 4: entry *first;
967 * 8: WORD sel ( start points to blockstart)
968 * A: WORD unknown
969 * 100xentry:
970 * 00..17: Code
971 * 18: PDB *owning_process;
972 * 1C: PTR blockstart
974 * We ignore this for now. (Just a note for further developers)
975 * FIXME: use this method, so we don't waste selectors...
977 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
978 * the 0x66 prefix switches from word->long registers.
980 * 665A pop edx
981 * 6668x arg2 x pushl <arg2>
982 * 6652 push edx
983 * EAx arg1 x jmpf <arg1>
985 * returns the startaddress of this thunk.
987 * Note, that they look very similair to the ones allocates by THUNK_Alloc.
988 * RETURNS
989 * segmented pointer to the start of the thunk
991 DWORD WINAPI
992 AllocSLCallback(
993 DWORD finalizer, /* [in] finalizer function */
994 DWORD callback /* [in] callback function */
996 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
997 WORD sel;
999 x=thunk;
1000 *x++=0x66;*x++=0x5a; /* popl edx */
1001 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
1002 *x++=0x66;*x++=0x52; /* pushl edx */
1003 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
1005 *(PDB**)(thunk+18) = PROCESS_Current();
1007 sel = SELECTOR_AllocBlock( thunk , 32, SEGMENT_CODE, FALSE, FALSE );
1008 return (sel<<16)|0;
1011 /**********************************************************************
1012 * FreeSLCallback (KERNEL32.274)
1013 * Frees the specified 16->32 callback
1015 void WINAPI
1016 FreeSLCallback(
1017 DWORD x /* [in] 16 bit callback (segmented pointer?) */
1019 FIXME_(win32)("(0x%08lx): stub\n",x);
1023 /**********************************************************************
1024 * GetTEBSelectorFS (KERNEL.475)
1025 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1027 void WINAPI GetTEBSelectorFS16(void)
1029 CURRENT_STACK16->fs = __get_fs();
1032 /**********************************************************************
1033 * KERNEL_431 (KERNEL.431)
1034 * IsPeFormat (W32SYS.2)
1035 * Checks the passed filename if it is a PE format executeable
1036 * RETURNS
1037 * TRUE, if it is.
1038 * FALSE if not.
1040 BOOL16 WINAPI IsPeFormat16(
1041 LPSTR fn, /* [in] filename to executeable */
1042 HFILE16 hf16 /* [in] open file, if filename is NULL */
1044 IMAGE_DOS_HEADER mzh;
1045 HFILE hf=FILE_GetHandle(hf16);
1046 OFSTRUCT ofs;
1047 DWORD xmagic;
1049 if (fn) {
1050 hf = OpenFile(fn,&ofs,OF_READ);
1051 if (hf==HFILE_ERROR)
1052 return FALSE;
1054 _llseek(hf,0,SEEK_SET);
1055 if (sizeof(mzh)!=_lread(hf,&mzh,sizeof(mzh))) {
1056 _lclose(hf);
1057 return FALSE;
1059 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) {
1060 WARN("File has not got dos signature!\n");
1061 _lclose(hf);
1062 return FALSE;
1064 _llseek(hf,mzh.e_lfanew,SEEK_SET);
1065 if (sizeof(DWORD)!=_lread(hf,&xmagic,sizeof(DWORD))) {
1066 _lclose(hf);
1067 return FALSE;
1069 _lclose(hf);
1070 return (xmagic == IMAGE_NT_SIGNATURE);
1074 /***********************************************************************
1075 * K32Thk1632Prolog (KERNEL32.492)
1077 void WINAPI K32Thk1632Prolog( CONTEXT86 *context )
1079 LPBYTE code = (LPBYTE)EIP_reg(context) - 5;
1081 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1082 of 16->32 thunks instead of using one of the standard methods!
1083 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1084 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1085 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1086 bypassed, which means it will crash the next time the 32-bit OLE
1087 code thunks down again to 16-bit (this *will* happen!).
1089 The following hack tries to recognize this situation.
1090 This is possible since the called stubs in OLECLI32/OLESVR32 all
1091 look exactly the same:
1092 00 E8xxxxxxxx call K32Thk1632Prolog
1093 05 FF55FC call [ebp-04]
1094 08 E8xxxxxxxx call K32Thk1632Epilog
1095 0D 66CB retf
1097 If we recognize this situation, we try to simulate the actions
1098 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1099 to our 32-bit stack, creating a proper STACK16FRAME and
1100 updating cur_stack. */
1102 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1103 && code[13] == 0x66 && code[14] == 0xCB)
1105 WORD stackSel = NtCurrentTeb()->stack_sel;
1106 DWORD stackBase = GetSelectorBase(stackSel);
1108 DWORD argSize = EBP_reg(context) - ESP_reg(context);
1109 char *stack16 = (char *)ESP_reg(context) - 4;
1110 char *stack32 = (char *)NtCurrentTeb()->cur_stack - argSize;
1111 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1113 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1114 EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1116 memset(frame16, '\0', sizeof(STACK16FRAME));
1117 frame16->frame32 = (STACK32FRAME *)NtCurrentTeb()->cur_stack;
1118 frame16->ebp = EBP_reg(context);
1120 memcpy(stack32, stack16, argSize);
1121 NtCurrentTeb()->cur_stack = PTR_SEG_OFF_TO_SEGPTR(stackSel, (DWORD)frame16 - stackBase);
1123 ESP_reg(context) = (DWORD)stack32 + 4;
1124 EBP_reg(context) = ESP_reg(context) + argSize;
1126 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1127 EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1130 SYSLEVEL_ReleaseWin16Lock();
1133 /***********************************************************************
1134 * K32Thk1632Epilog (KERNEL32.491)
1136 void WINAPI K32Thk1632Epilog( CONTEXT86 *context )
1138 LPBYTE code = (LPBYTE)EIP_reg(context) - 13;
1140 SYSLEVEL_RestoreWin16Lock();
1142 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1144 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1145 && code[13] == 0x66 && code[14] == 0xCB)
1147 STACK16FRAME *frame16 = (STACK16FRAME *)PTR_SEG_TO_LIN(NtCurrentTeb()->cur_stack);
1148 char *stack16 = (char *)(frame16 + 1);
1149 DWORD argSize = frame16->ebp - (DWORD)stack16;
1150 char *stack32 = (char *)frame16->frame32 - argSize;
1152 DWORD nArgsPopped = ESP_reg(context) - (DWORD)stack32;
1154 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1155 EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1157 NtCurrentTeb()->cur_stack = (DWORD)frame16->frame32;
1159 ESP_reg(context) = (DWORD)stack16 + nArgsPopped;
1160 EBP_reg(context) = frame16->ebp;
1162 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1163 EBP_reg(context), ESP_reg(context), NtCurrentTeb()->cur_stack);
1167 /***********************************************************************
1168 * UpdateResourceA (KERNEL32.707)
1170 BOOL WINAPI UpdateResourceA(
1171 HANDLE hUpdate,
1172 LPCSTR lpType,
1173 LPCSTR lpName,
1174 WORD wLanguage,
1175 LPVOID lpData,
1176 DWORD cbData) {
1178 FIXME_(win32)(": stub\n");
1179 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1180 return FALSE;
1183 /***********************************************************************
1184 * UpdateResourceW (KERNEL32.708)
1186 BOOL WINAPI UpdateResourceW(
1187 HANDLE hUpdate,
1188 LPCWSTR lpType,
1189 LPCWSTR lpName,
1190 WORD wLanguage,
1191 LPVOID lpData,
1192 DWORD cbData) {
1194 FIXME_(win32)(": stub\n");
1195 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1196 return FALSE;
1200 /***********************************************************************
1201 * WaitNamedPipeA [KERNEL32.725]
1203 BOOL WINAPI WaitNamedPipeA (LPCSTR lpNamedPipeName, DWORD nTimeOut)
1204 { FIXME_(win32)("%s 0x%08lx\n",lpNamedPipeName,nTimeOut);
1205 SetLastError(ERROR_PIPE_NOT_CONNECTED);
1206 return FALSE;
1208 /***********************************************************************
1209 * WaitNamedPipeW [KERNEL32.726]
1211 BOOL WINAPI WaitNamedPipeW (LPCWSTR lpNamedPipeName, DWORD nTimeOut)
1212 { FIXME_(win32)("%s 0x%08lx\n",debugstr_w(lpNamedPipeName),nTimeOut);
1213 SetLastError(ERROR_PIPE_NOT_CONNECTED);
1214 return FALSE;
1217 /*********************************************************************
1218 * PK16FNF [KERNEL32.91]
1220 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1221 * string buffer with the 8.3 filename of a recently loaded 16-bit
1222 * module. It is unknown exactly what modules trigger this
1223 * mechanism or what purpose this serves. Win98 Explorer (and
1224 * probably also Win95 with IE 4 shell integration) calls this
1225 * several times during initialization.
1227 * FIXME: find out what this really does and make it work.
1229 void WINAPI PK16FNF(LPSTR strPtr)
1231 FIXME_(win32)("(%p): stub\n", strPtr);
1233 /* fill in a fake filename that'll be easy to recognize */
1234 lstrcpyA(strPtr, "WINESTUB.FIX");