Better _ultoa().
[wine/multimedia.git] / win32 / kernel32.c
blob80a785b76ce63fc91ad590ae54b3669e77975389
1 /*
2 * KERNEL32 thunks and other undocumented stuff
4 * Copyright 1997-1998 Marcus Meissner
5 * Copyright 1998 Ulrich Weigand
6 */
8 #include "windows.h"
9 #include "callback.h"
10 #include "resource.h"
11 #include "task.h"
12 #include "user.h"
13 #include "heap.h"
14 #include "module.h"
15 #include "process.h"
16 #include "stackframe.h"
17 #include "heap.h"
18 #include "selectors.h"
19 #include "task.h"
20 #include "win.h"
21 #include "file.h"
22 #include "debug.h"
23 #include "flatthunk.h"
24 #include "syslevel.h"
25 #include "winerror.h"
28 /***********************************************************************
29 * *
30 * Win95 internal thunks *
31 * *
32 ***********************************************************************/
34 /***********************************************************************
35 * Generates a FT_Prolog call.
37 * 0FB6D1 movzbl edx,cl
38 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
39 * 68xxxxxxxx push FT_Prolog
40 * C3 lret
42 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
43 LPBYTE x;
45 x = relayCode;
46 *x++ = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
47 *x++ = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
48 x+=4; /* mov edx, [4*edx + targetTable] */
49 *x++ = 0x68; *(DWORD*)x = (DWORD)GetProcAddress32(GetModuleHandle32A("KERNEL32"),"FT_Prolog");
50 x+=4; /* push FT_Prolog */
51 *x++ = 0xC3; /* lret */
52 /* fill rest with 0xCC / int 3 */
55 /***********************************************************************
56 * _write_qtthunk (internal)
57 * Generates a QT_Thunk style call.
59 * 33C9 xor ecx, ecx
60 * 8A4DFC mov cl , [ebp-04]
61 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
62 * B8yyyyyyyy mov eax, QT_Thunk
63 * FFE0 jmp eax
65 static void _write_qtthunk(
66 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
67 DWORD *targetTable /* [in] start of thunk (for index lookup) */
68 ) {
69 LPBYTE x;
71 x = relayCode;
72 *x++ = 0x33;*x++=0xC9; /* xor ecx,ecx */
73 *x++ = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
74 *x++ = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
75 x+=4; /* mov edx, [4*ecx + targetTable */
76 *x++ = 0xB8; *(DWORD*)x = (DWORD)GetProcAddress32(GetModuleHandle32A("KERNEL32"),"QT_Thunk");
77 x+=4; /* mov eax , QT_Thunk */
78 *x++ = 0xFF; *x++ = 0xE0; /* jmp eax */
79 /* should fill the rest of the 32 bytes with 0xCC */
82 /***********************************************************************
83 * _loadthunk
85 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
86 struct ThunkDataCommon *TD32, DWORD checksum)
88 struct ThunkDataCommon *TD16;
89 HMODULE32 hmod;
90 int ordinal;
92 if ((hmod = LoadLibrary16(module)) <= 32)
94 ERR(thunk, "(%s, %s, %s): Unable to load '%s', error %d\n",
95 module, func, module32, module, hmod);
96 return 0;
99 if ( !(ordinal = NE_GetOrdinal(hmod, func))
100 || !(TD16 = PTR_SEG_TO_LIN(NE_GetEntryPointEx(hmod, ordinal, FALSE))))
102 ERR(thunk, "(%s, %s, %s): Unable to find '%s'\n",
103 module, func, module32, func);
104 return 0;
107 if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
109 ERR(thunk, "(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
110 module, func, module32,
111 TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
112 TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
113 return 0;
116 if (TD32 && TD16->checksum != TD32->checksum)
118 ERR(thunk, "(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
119 module, func, module32, TD16->checksum, TD32->checksum);
120 return 0;
123 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
125 ERR(thunk, "(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
126 module, func, module32, *(LPDWORD)TD16, checksum);
127 return 0;
130 return TD16;
133 /***********************************************************************
134 * GetThunkStuff (KERNEL32.53)
136 LPVOID WINAPI GetThunkStuff(LPSTR module, LPSTR func)
138 return _loadthunk(module, func, "<kernel>", NULL, 0L);
141 /***********************************************************************
142 * GetThunkBuff (KERNEL32.52)
143 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
145 LPVOID WINAPI GetThunkBuff(void)
147 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
150 /***********************************************************************
151 * ThunkConnect32 (KERNEL32)
152 * Connects a 32bit and a 16bit thunkbuffer.
154 UINT32 WINAPI ThunkConnect32(
155 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
156 LPSTR thunkfun16, /* [in] win16 thunkfunction */
157 LPSTR module16, /* [in] name of win16 dll */
158 LPSTR module32, /* [in] name of win32 dll */
159 HMODULE32 hmod32, /* [in] hmodule of win32 dll */
160 DWORD dwReason /* [in] initialisation argument */
162 BOOL32 directionSL;
164 if (!lstrncmp32A(TD->magic, "SL01", 4))
166 directionSL = TRUE;
168 TRACE(thunk, "SL01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
169 module32, (DWORD)TD, module16, thunkfun16, dwReason);
171 else if (!lstrncmp32A(TD->magic, "LS01", 4))
173 directionSL = FALSE;
175 TRACE(thunk, "LS01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
176 module32, (DWORD)TD, module16, thunkfun16, dwReason);
178 else
180 ERR(thunk, "Invalid magic %c%c%c%c\n",
181 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
182 return 0;
185 switch (dwReason)
187 case DLL_PROCESS_ATTACH:
189 struct ThunkDataCommon *TD16;
190 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
191 return 0;
193 if (directionSL)
195 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
196 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
197 struct SLTargetDB *tdb;
199 if (SL16->fpData == NULL)
201 ERR(thunk, "ThunkConnect16 was not called!\n");
202 return 0;
205 SL32->data = SL16->fpData;
207 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
208 tdb->process = PROCESS_Current();
209 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
211 tdb->next = SL32->data->targetDB; /* FIXME: not thread-safe! */
212 SL32->data->targetDB = tdb;
214 TRACE(thunk, "Process %08lx allocated TargetDB entry for ThunkDataSL %08lx\n",
215 (DWORD)PROCESS_Current(), (DWORD)SL32->data);
217 else
219 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
220 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
222 LS32->targetTable = PTR_SEG_TO_LIN(LS16->targetTable);
224 /* write QT_Thunk and FT_Prolog stubs */
225 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk, LS32->targetTable);
226 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
228 break;
231 case DLL_PROCESS_DETACH:
232 /* FIXME: cleanup */
233 break;
236 return 1;
239 /**********************************************************************
240 * QT_Thunk (KERNEL32)
242 * The target address is in EDX.
243 * The 16 bit arguments start at ESP+4.
244 * The number of 16bit argumentbytes is EBP-ESP-0x44 (68 Byte thunksetup).
245 * [ok]
247 REGS_ENTRYPOINT(QT_Thunk)
249 CONTEXT context16;
250 DWORD argsize;
251 THDB *thdb = THREAD_Current();
253 memcpy(&context16,context,sizeof(context16));
255 CS_reg(&context16) = HIWORD(EDX_reg(context));
256 IP_reg(&context16) = LOWORD(EDX_reg(context));
257 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
258 + (WORD)&((STACK16FRAME*)0)->bp;
260 argsize = EBP_reg(context)-ESP_reg(context)-0x44;
262 memcpy( ((LPBYTE)THREAD_STACK16(thdb))-argsize,
263 (LPBYTE)ESP_reg(context)+4, argsize );
265 EAX_reg(context) = Callbacks->CallRegisterShortProc( &context16, argsize );
266 EDX_reg(context) = HIWORD(EAX_reg(context));
267 EAX_reg(context) = LOWORD(EAX_reg(context));
271 /**********************************************************************
272 * FT_Prolog (KERNEL32.233)
274 * The set of FT_... thunk routines is used instead of QT_Thunk,
275 * if structures have to be converted from 32-bit to 16-bit
276 * (change of member alignment, conversion of members).
278 * The thunk function (as created by the thunk compiler) calls
279 * FT_Prolog at the beginning, to set up a stack frame and
280 * allocate a 64 byte buffer on the stack.
281 * The input parameters (target address and some flags) are
282 * saved for later use by FT_Thunk.
284 * Input: EDX 16-bit target address (SEGPTR)
285 * CX bits 0..7 target number (in target table)
286 * bits 8..9 some flags (unclear???)
287 * bits 10..15 number of DWORD arguments
289 * Output: A new stackframe is created, and a 64 byte buffer
290 * allocated on the stack. The layout of the stack
291 * on return is as follows:
293 * (ebp+4) return address to caller of thunk function
294 * (ebp) old EBP
295 * (ebp-4) saved EBX register of caller
296 * (ebp-8) saved ESI register of caller
297 * (ebp-12) saved EDI register of caller
298 * (ebp-16) saved ECX register, containing flags
299 * (ebp-20) bitmap containing parameters that are to be converted
300 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
301 * filled in by the thunk code before calling FT_Thunk
302 * (ebp-24)
303 * ... (unclear)
304 * (ebp-44)
305 * (ebp-48) saved EAX register of caller (unclear, never restored???)
306 * (ebp-52) saved EDX register, containing 16-bit thunk target
307 * (ebp-56)
308 * ... (unclear)
309 * (ebp-64)
311 * ESP is EBP-68 on return.
315 REGS_ENTRYPOINT(FT_Prolog)
317 /* Pop return address to thunk code */
318 EIP_reg(context) = STACK32_POP(context);
320 /* Build stack frame */
321 STACK32_PUSH(context, EBP_reg(context));
322 EBP_reg(context) = ESP_reg(context);
324 /* Allocate 64-byte Thunk Buffer */
325 ESP_reg(context) -= 64;
326 memset((char *)ESP_reg(context), '\0', 64);
328 /* Store Flags (ECX) and Target Address (EDX) */
329 /* Save other registers to be restored later */
330 *(DWORD *)(EBP_reg(context) - 4) = EBX_reg(context);
331 *(DWORD *)(EBP_reg(context) - 8) = ESI_reg(context);
332 *(DWORD *)(EBP_reg(context) - 12) = EDI_reg(context);
333 *(DWORD *)(EBP_reg(context) - 16) = ECX_reg(context);
335 *(DWORD *)(EBP_reg(context) - 48) = EAX_reg(context);
336 *(DWORD *)(EBP_reg(context) - 52) = EDX_reg(context);
338 /* Push return address back onto stack */
339 STACK32_PUSH(context, EIP_reg(context));
342 /**********************************************************************
343 * FT_Thunk (KERNEL32.234)
345 * This routine performs the actual call to 16-bit code,
346 * similar to QT_Thunk. The differences are:
347 * - The call target is taken from the buffer created by FT_Prolog
348 * - Those arguments requested by the thunk code (by setting the
349 * corresponding bit in the bitmap at EBP-20) are converted
350 * from 32-bit pointers to segmented pointers (those pointers
351 * are guaranteed to point to structures copied to the stack
352 * by the thunk code, so we always use the 16-bit stack selector
353 * for those addresses).
355 * The bit #i of EBP-20 corresponds here to the DWORD starting at
356 * ESP+4 + 2*i.
358 * FIXME: It is unclear what happens if there are more than 32 WORDs
359 * of arguments, so that the single DWORD bitmap is no longer
360 * sufficient ...
363 REGS_ENTRYPOINT(FT_Thunk)
365 DWORD mapESPrelative = *(DWORD *)(EBP_reg(context) - 20);
366 DWORD callTarget = *(DWORD *)(EBP_reg(context) - 52);
368 CONTEXT context16;
369 DWORD i, argsize;
370 LPBYTE newstack, oldstack;
371 THDB *thdb = THREAD_Current();
373 memcpy(&context16,context,sizeof(context16));
375 CS_reg(&context16) = HIWORD(callTarget);
376 IP_reg(&context16) = LOWORD(callTarget);
377 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
378 + (WORD)&((STACK16FRAME*)0)->bp;
380 argsize = EBP_reg(context)-ESP_reg(context)-0x44;
381 newstack = ((LPBYTE)THREAD_STACK16(thdb))-argsize;
382 oldstack = (LPBYTE)ESP_reg(context)+4;
384 memcpy( newstack, oldstack, argsize );
386 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
387 if (mapESPrelative & (1 << i))
389 SEGPTR *arg = (SEGPTR *)(newstack + 2*i);
390 *arg = PTR_SEG_OFF_TO_SEGPTR(SELECTOROF(thdb->cur_stack),
391 OFFSETOF(thdb->cur_stack) - argsize
392 + (*(LPBYTE *)arg - oldstack));
395 EAX_reg(context) = Callbacks->CallRegisterShortProc( &context16, argsize );
396 EDX_reg(context) = HIWORD(EAX_reg(context));
397 EAX_reg(context) = LOWORD(EAX_reg(context));
400 /**********************************************************************
401 * FT_ExitNN (KERNEL32.218 - 232)
403 * One of the FT_ExitNN functions is called at the end of the thunk code.
404 * It removes the stack frame created by FT_Prolog, moves the function
405 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
406 * value, but the thunk code has moved it from EAX to EBX in the
407 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
408 * and perform a return to the CALLER of the thunk code (while removing
409 * the given number of arguments from the caller's stack).
412 static void FT_Exit(CONTEXT *context, int nPopArgs)
414 /* Return value is in EBX */
415 EAX_reg(context) = EBX_reg(context);
417 /* Restore EBX, ESI, and EDI registers */
418 EBX_reg(context) = *(DWORD *)(EBP_reg(context) - 4);
419 ESI_reg(context) = *(DWORD *)(EBP_reg(context) - 8);
420 EDI_reg(context) = *(DWORD *)(EBP_reg(context) - 12);
422 /* Clean up stack frame */
423 ESP_reg(context) = EBP_reg(context);
424 EBP_reg(context) = STACK32_POP(context);
426 /* Pop return address to CALLER of thunk code */
427 EIP_reg(context) = STACK32_POP(context);
428 /* Remove arguments */
429 ESP_reg(context) += nPopArgs;
430 /* Push return address back onto stack */
431 STACK32_PUSH(context, EIP_reg(context));
434 REGS_ENTRYPOINT(FT_Exit0) { FT_Exit(context, 0); }
435 REGS_ENTRYPOINT(FT_Exit4) { FT_Exit(context, 4); }
436 REGS_ENTRYPOINT(FT_Exit8) { FT_Exit(context, 8); }
437 REGS_ENTRYPOINT(FT_Exit12) { FT_Exit(context, 12); }
438 REGS_ENTRYPOINT(FT_Exit16) { FT_Exit(context, 16); }
439 REGS_ENTRYPOINT(FT_Exit20) { FT_Exit(context, 20); }
440 REGS_ENTRYPOINT(FT_Exit24) { FT_Exit(context, 24); }
441 REGS_ENTRYPOINT(FT_Exit28) { FT_Exit(context, 28); }
442 REGS_ENTRYPOINT(FT_Exit32) { FT_Exit(context, 32); }
443 REGS_ENTRYPOINT(FT_Exit36) { FT_Exit(context, 36); }
444 REGS_ENTRYPOINT(FT_Exit40) { FT_Exit(context, 40); }
445 REGS_ENTRYPOINT(FT_Exit44) { FT_Exit(context, 44); }
446 REGS_ENTRYPOINT(FT_Exit48) { FT_Exit(context, 48); }
447 REGS_ENTRYPOINT(FT_Exit52) { FT_Exit(context, 52); }
448 REGS_ENTRYPOINT(FT_Exit56) { FT_Exit(context, 56); }
451 /**********************************************************************
452 * WOWCallback16 (KERNEL32.62)(WOW32.2)
453 * Calls a win16 function with a single DWORD argument.
454 * RETURNS
455 * the return value
457 DWORD WINAPI WOWCallback16(
458 FARPROC16 fproc, /* [in] win16 function to call */
459 DWORD arg /* [in] single DWORD argument to function */
461 DWORD ret;
462 TRACE(thunk,"(%p,0x%08lx)...\n",fproc,arg);
463 ret = Callbacks->CallWOWCallbackProc(fproc,arg);
464 TRACE(thunk,"... returns %ld\n",ret);
465 return ret;
468 /**********************************************************************
469 * WOWCallback16Ex (KERNEL32.55)(WOW32.3)
470 * Calls a function in 16bit code.
471 * RETURNS
472 * TRUE for success
474 BOOL32 WINAPI WOWCallback16Ex(
475 FARPROC16 vpfn16, /* [in] win16 function to call */
476 DWORD dwFlags, /* [in] flags */
477 DWORD cbArgs, /* [in] nr of arguments */
478 LPVOID pArgs, /* [in] pointer to arguments (LPDWORD) */
479 LPDWORD pdwRetCode /* [out] return value of win16 function */
481 return Callbacks->CallWOWCallback16Ex(vpfn16,dwFlags,cbArgs,pArgs,pdwRetCode);
484 /***********************************************************************
485 * ThunkInitLS (KERNEL32.43)
486 * A thunkbuffer link routine
487 * The thunkbuf looks like:
489 * 00: DWORD length ? don't know exactly
490 * 04: SEGPTR ptr ? where does it point to?
491 * The pointer ptr is written into the first DWORD of 'thunk'.
492 * (probably correct implemented)
493 * [ok probably]
494 * RETURNS
495 * segmented pointer to thunk?
497 DWORD WINAPI ThunkInitLS(
498 LPDWORD thunk, /* [in] win32 thunk */
499 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
500 DWORD len, /* [in] thkbuffer length */
501 LPCSTR dll16, /* [in] name of win16 dll */
502 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
504 LPDWORD addr;
506 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
507 return 0;
509 if (!addr[1])
510 return 0;
511 *(DWORD*)thunk = addr[1];
513 return addr[1];
516 /***********************************************************************
517 * Common32ThkLS (KERNEL32.45)
519 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
520 * style thunks. The basic difference is that the parameter conversion
521 * is done completely on the *16-bit* side here. Thus we do not call
522 * the 16-bit target directly, but call a common entry point instead.
523 * This entry function then calls the target according to the target
524 * number passed in the DI register.
526 * Input: EAX SEGPTR to the common 16-bit entry point
527 * CX offset in thunk table (target number * 4)
528 * DX error return value if execution fails (unclear???)
529 * EDX.HI number of DWORD parameters
531 * (Note that we need to move the thunk table offset from CX to DI !)
533 * The called 16-bit stub expects its stack to look like this:
534 * ...
535 * (esp+40) 32-bit arguments
536 * ...
537 * (esp+8) 32 byte of stack space available as buffer
538 * (esp) 8 byte return address for use with 0x66 lret
540 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
541 * and uses the EAX register to return a DWORD return value.
542 * Thus we need to use a special assembly glue routine
543 * (CallRegisterLongProc instead of CallRegisterShortProc).
545 * Finally, we return to the caller, popping the arguments off
546 * the stack.
548 * FIXME: The called function uses EBX to return the number of
549 * arguments that are to be popped off the caller's stack.
550 * This is clobbered by the assembly glue, so we simply use
551 * the original EDX.HI to get the number of arguments.
552 * (Those two values should be equal anyway ...?)
555 REGS_ENTRYPOINT(Common32ThkLS)
557 CONTEXT context16;
558 DWORD argsize;
559 THDB *thdb = THREAD_Current();
561 memcpy(&context16,context,sizeof(context16));
563 DI_reg(&context16) = CX_reg(context);
564 CS_reg(&context16) = HIWORD(EAX_reg(context));
565 IP_reg(&context16) = LOWORD(EAX_reg(context));
566 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
567 + (WORD)&((STACK16FRAME*)0)->bp;
569 argsize = HIWORD(EDX_reg(context)) * 4;
571 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
572 if (EDX_reg(context) == EIP_reg(context))
573 argsize = 6 * 4;
575 memcpy( ((LPBYTE)THREAD_STACK16(thdb))-argsize,
576 (LPBYTE)ESP_reg(context)+4, argsize );
578 EAX_reg(context) = Callbacks->CallRegisterLongProc(&context16, argsize + 32);
580 /* Clean up caller's stack frame */
582 EIP_reg(context) = STACK32_POP(context);
583 ESP_reg(context) += argsize;
584 STACK32_PUSH(context, EIP_reg(context));
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 REGS_ENTRYPOINT(OT_32ThkLSF)
616 CONTEXT context16;
617 DWORD argsize;
618 THDB *thdb = THREAD_Current();
620 memcpy(&context16,context,sizeof(context16));
622 CS_reg(&context16) = HIWORD(EDX_reg(context));
623 IP_reg(&context16) = LOWORD(EDX_reg(context));
624 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
625 + (WORD)&((STACK16FRAME*)0)->bp;
627 argsize = 2 * *(WORD *)(ESP_reg(context) + 4) + 2;
629 memcpy( ((LPBYTE)THREAD_STACK16(thdb))-argsize,
630 (LPBYTE)ESP_reg(context)+4, argsize );
632 EAX_reg(context) = Callbacks->CallRegisterShortProc(&context16, argsize);
634 memcpy( (LPBYTE)ESP_reg(context)+4,
635 ((LPBYTE)THREAD_STACK16(thdb))-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 HMODULE32 hkrnl32 = GetModuleHandle32A("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)GetProcAddress32(hkrnl32,(LPSTR)90);
689 *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress32(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 from the stack.
720 REGS_ENTRYPOINT(FT_PrologPrime)
722 DWORD targetTableOffset = STACK32_POP(context);
723 LPBYTE relayCode = (LPBYTE)STACK32_POP(context);
724 DWORD *targetTable = *(DWORD **)(relayCode+targetTableOffset);
725 DWORD targetNr = LOBYTE(ECX_reg(context));
727 _write_ftprolog(relayCode, targetTable);
729 /* We should actually call the relay code now, */
730 /* but we skip it and go directly to FT_Prolog */
731 EDX_reg(context) = targetTable[targetNr];
732 __regs_FT_Prolog(context);
735 /***********************************************************************
736 * QT_ThunkPrime (KERNEL32.90)
738 * This function corresponds to FT_PrologPrime, but installs a
739 * call stub for QT_Thunk instead.
741 * Input: (EBP-4) target number (passed through to QT_Thunk)
742 * EDX target table pointer location offset
743 * EAX start of relay code
746 REGS_ENTRYPOINT(QT_ThunkPrime)
748 DWORD targetTableOffset = EDX_reg(context);
749 LPBYTE relayCode = (LPBYTE)EAX_reg(context);
750 DWORD *targetTable = *(DWORD **)(relayCode+targetTableOffset);
751 DWORD targetNr = LOBYTE(*(DWORD *)(EBP_reg(context) - 4));
753 _write_qtthunk(relayCode, targetTable);
755 /* We should actually call the relay code now, */
756 /* but we skip it and go directly to QT_Thunk */
757 EDX_reg(context) = targetTable[targetNr];
758 __regs_QT_Thunk(context);
761 /***********************************************************************
762 * (KERNEL32.46)
763 * Another thunkbuf link routine.
764 * The start of the thunkbuf looks like this:
765 * 00: DWORD length
766 * 04: SEGPTR address for thunkbuffer pointer
767 * [ok probably]
769 VOID WINAPI ThunkInitSL(
770 LPBYTE thunk, /* [in] start of thunkbuffer */
771 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
772 DWORD len, /* [in] length of thunkbuffer */
773 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
774 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
776 LPDWORD addr;
778 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
779 return;
781 *(DWORD*)PTR_SEG_TO_LIN(addr[1]) = (DWORD)thunk;
784 /**********************************************************************
785 * SSInit KERNEL.700
786 * RETURNS
787 * TRUE for success.
789 BOOL32 WINAPI SSInit()
791 return TRUE;
794 /**********************************************************************
795 * SSOnBigStack KERNEL32.87
796 * Check if thunking is initialized (ss selector set up etc.)
797 * We do that differently, so just return TRUE.
798 * [ok]
799 * RETURNS
800 * TRUE for success.
802 BOOL32 WINAPI SSOnBigStack()
804 TRACE(thunk, "Yes, thunking is initialized\n");
805 return TRUE;
808 /**********************************************************************
809 * SSCall
810 * One of the real thunking functions. This one seems to be for 32<->32
811 * thunks. It should probably be capable of crossing processboundaries.
813 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
814 * [ok]
816 DWORD WINAPIV SSCall(
817 DWORD nr, /* [in] number of argument bytes */
818 DWORD flags, /* [in] FIXME: flags ? */
819 FARPROC32 fun, /* [in] function to call */
820 ... /* [in/out] arguments */
822 DWORD i,ret;
823 DWORD *args = ((DWORD *)&fun) + 1;
825 if(TRACE_ON(thunk)){
826 dbg_decl_str(thunk, 256);
827 for (i=0;i<nr/4;i++)
828 dsprintf(thunk,"0x%08lx,",args[i]);
829 TRACE(thunk,"(%ld,0x%08lx,%p,[%s])\n",
830 nr,flags,fun,dbg_str(thunk));
832 switch (nr) {
833 case 0: ret = fun();
834 break;
835 case 4: ret = fun(args[0]);
836 break;
837 case 8: ret = fun(args[0],args[1]);
838 break;
839 case 12: ret = fun(args[0],args[1],args[2]);
840 break;
841 case 16: ret = fun(args[0],args[1],args[2],args[3]);
842 break;
843 case 20: ret = fun(args[0],args[1],args[2],args[3],args[4]);
844 break;
845 case 24: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
846 break;
847 case 28: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
848 break;
849 case 32: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
850 break;
851 case 36: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
852 break;
853 case 40: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
854 break;
855 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]);
856 break;
857 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]);
858 break;
859 default:
860 WARN(thunk,"Unsupported nr of arguments, %ld\n",nr);
861 ret = 0;
862 break;
865 TRACE(thunk," returning %ld ...\n",ret);
866 return ret;
869 /**********************************************************************
870 * W32S_BackTo32 (KERNEL32.51)
872 REGS_ENTRYPOINT(W32S_BackTo32)
874 LPDWORD stack = (LPDWORD)ESP_reg( context );
875 FARPROC32 proc = (FARPROC32) stack[0];
877 EAX_reg( context ) = proc( stack[2], stack[3], stack[4], stack[5], stack[6],
878 stack[7], stack[8], stack[9], stack[10], stack[11] );
880 EIP_reg( context ) = stack[1];
883 /**********************************************************************
884 * AllocSLCallback (KERNEL32)
886 * Win95 uses some structchains for callbacks. It allocates them
887 * in blocks of 100 entries, size 32 bytes each, layout:
888 * blockstart:
889 * 0: PTR nextblockstart
890 * 4: entry *first;
891 * 8: WORD sel ( start points to blockstart)
892 * A: WORD unknown
893 * 100xentry:
894 * 00..17: Code
895 * 18: PDB *owning_process;
896 * 1C: PTR blockstart
898 * We ignore this for now. (Just a note for further developers)
899 * FIXME: use this method, so we don't waste selectors...
901 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
902 * the 0x66 prefix switches from word->long registers.
904 * 665A pop edx
905 * 6668x arg2 x pushl <arg2>
906 * 6652 push edx
907 * EAx arg1 x jmpf <arg1>
909 * returns the startaddress of this thunk.
911 * Note, that they look very similair to the ones allocates by THUNK_Alloc.
912 * RETURNS
913 * segmented pointer to the start of the thunk
915 DWORD WINAPI
916 AllocSLCallback(
917 DWORD finalizer, /* [in] finalizer function */
918 DWORD callback /* [in] callback function */
920 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
921 WORD sel;
923 x=thunk;
924 *x++=0x66;*x++=0x5a; /* popl edx */
925 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
926 *x++=0x66;*x++=0x52; /* pushl edx */
927 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
929 *(PDB32**)(thunk+18) = PROCESS_Current();
931 sel = SELECTOR_AllocBlock( thunk , 32, SEGMENT_CODE, FALSE, FALSE );
932 return (sel<<16)|0;
935 /**********************************************************************
936 * FreeSLCallback (KERNEL32.274)
937 * Frees the specified 16->32 callback
939 void WINAPI
940 FreeSLCallback(
941 DWORD x /* [in] 16 bit callback (segmented pointer?) */
943 FIXME(win32,"(0x%08lx): stub\n",x);
947 /**********************************************************************
948 * GetTEBSelectorFS (KERNEL.475)
949 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
951 VOID WINAPI GetTEBSelectorFS( CONTEXT *context )
953 GET_FS( FS_reg(context) );
956 /**********************************************************************
957 * KERNEL_431 (KERNEL.431)
958 * IsPeFormat (W32SYS.2)
959 * Checks the passed filename if it is a PE format executeable
960 * RETURNS
961 * TRUE, if it is.
962 * FALSE if not.
964 BOOL16 WINAPI IsPeFormat(
965 LPSTR fn, /* [in] filename to executeable */
966 HFILE16 hf16 /* [in] open file, if filename is NULL */
968 IMAGE_DOS_HEADER mzh;
969 HFILE32 hf=HFILE16_TO_HFILE32(hf16);
970 OFSTRUCT ofs;
971 DWORD xmagic;
973 if (fn) {
974 hf = OpenFile32(fn,&ofs,OF_READ);
975 if (hf==HFILE_ERROR32)
976 return FALSE;
978 _llseek32(hf,0,SEEK_SET);
979 if (sizeof(mzh)!=_lread32(hf,&mzh,sizeof(mzh))) {
980 _lclose32(hf);
981 return FALSE;
983 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) {
984 WARN(dosmem,"File has not got dos signature!\n");
985 _lclose32(hf);
986 return FALSE;
988 _llseek32(hf,mzh.e_lfanew,SEEK_SET);
989 if (sizeof(DWORD)!=_lread32(hf,&xmagic,sizeof(DWORD))) {
990 _lclose32(hf);
991 return FALSE;
993 _lclose32(hf);
994 return (xmagic == IMAGE_NT_SIGNATURE);
997 /***********************************************************************
998 * WOWHandle32 (KERNEL32.57)(WOW32.16)
999 * Converts a win16 handle of type into the respective win32 handle.
1000 * We currently just return this handle, since most handles are the same
1001 * for win16 and win32.
1002 * RETURNS
1003 * The new handle
1005 HANDLE32 WINAPI WOWHandle32(
1006 WORD handle, /* [in] win16 handle */
1007 WOW_HANDLE_TYPE type /* [in] handle type */
1009 TRACE(win32,"(0x%04x,%d)\n",handle,type);
1010 return (HANDLE32)handle;
1013 /***********************************************************************
1014 * K32Thk1632Prolog (KERNEL32.492)
1016 REGS_ENTRYPOINT(K32Thk1632Prolog)
1018 LPBYTE code = (LPBYTE)EIP_reg(context) - 5;
1020 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1021 of 16->32 thunks instead of using one of the standard methods!
1022 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1023 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1024 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1025 bypassed, which means it will crash the next time the 32-bit OLE
1026 code thunks down again to 16-bit (this *will* happen!).
1028 The following hack tries to recognize this situation.
1029 This is possible since the called stubs in OLECLI32/OLESVR32 all
1030 look exactly the same:
1031 00 E8xxxxxxxx call K32Thk1632Prolog
1032 05 FF55FC call [ebp-04]
1033 08 E8xxxxxxxx call K32Thk1632Epilog
1034 0D 66CB retf
1036 If we recognize this situation, we try to simulate the actions
1037 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1038 to our 32-bit stack, creating a proper STACK16FRAME and
1039 updating thdb->cur_stack. */
1041 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1042 && code[13] == 0x66 && code[14] == 0xCB)
1044 WORD stackSel = NtCurrentTeb()->stack_sel;
1045 DWORD stackBase = GetSelectorBase(stackSel);
1047 THDB *thdb = THREAD_Current();
1048 DWORD argSize = EBP_reg(context) - ESP_reg(context);
1049 char *stack16 = (char *)ESP_reg(context);
1050 char *stack32 = (char *)thdb->cur_stack - argSize;
1051 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1053 TRACE(thunk, "before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1054 EBP_reg(context), ESP_reg(context), thdb->cur_stack);
1056 memset(frame16, '\0', sizeof(STACK16FRAME));
1057 frame16->frame32 = (STACK32FRAME *)thdb->cur_stack;
1058 frame16->ebp = EBP_reg(context);
1060 memcpy(stack32, stack16, argSize);
1061 thdb->cur_stack = PTR_SEG_OFF_TO_SEGPTR(stackSel, (DWORD)frame16 - stackBase);
1063 ESP_reg(context) = (DWORD)stack32;
1064 EBP_reg(context) = ESP_reg(context) + argSize;
1066 TRACE(thunk, "after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1067 EBP_reg(context), ESP_reg(context), thdb->cur_stack);
1070 SYSLEVEL_ReleaseWin16Lock();
1073 /***********************************************************************
1074 * K32Thk1632Epilog (KERNEL32.491)
1076 REGS_ENTRYPOINT(K32Thk1632Epilog)
1078 LPBYTE code = (LPBYTE)EIP_reg(context) - 13;
1080 SYSLEVEL_RestoreWin16Lock();
1082 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1084 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1085 && code[13] == 0x66 && code[14] == 0xCB)
1087 THDB *thdb = THREAD_Current();
1088 STACK16FRAME *frame16 = (STACK16FRAME *)PTR_SEG_TO_LIN(thdb->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), thdb->cur_stack);
1098 thdb->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), thdb->cur_stack);
1108 /***********************************************************************
1109 * UpdateResource32A (KERNEL32.707)
1111 BOOL32 WINAPI UpdateResource32A(
1112 HANDLE32 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 BOOL32 WINAPI UpdateResource32W(
1128 HANDLE32 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 BOOL32 WINAPI WaitNamedPipe32A (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 BOOL32 WINAPI WaitNamedPipe32W (LPCWSTR lpNamedPipeName, DWORD nTimeOut)
1153 { FIXME (win32,"%s 0x%08lx\n",debugstr_w(lpNamedPipeName),nTimeOut);
1154 SetLastError(ERROR_PIPE_NOT_CONNECTED);
1155 return FALSE;