Draws a border around windows with old win31 style (only ws_border) in
[wine.git] / win32 / kernel32.c
blob237dabf7ab4e5e94f25828b69056cabe1dbc29c2
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 "heap.h"
23 #include "selectors.h"
24 #include "task.h"
25 #include "file.h"
26 #include "debugtools.h"
27 #include "flatthunk.h"
28 #include "syslevel.h"
29 #include "winerror.h"
31 DECLARE_DEBUG_CHANNEL(dosmem)
32 DECLARE_DEBUG_CHANNEL(thunk)
33 DECLARE_DEBUG_CHANNEL(win32)
36 /***********************************************************************
37 * *
38 * Win95 internal thunks *
39 * *
40 ***********************************************************************/
42 /***********************************************************************
43 * LogApiThk (KERNEL.423)
45 void WINAPI LogApiThk( LPSTR func )
47 TRACE_(thunk)( "%s\n", debugstr_a(func) );
50 /***********************************************************************
51 * LogApiThkLSF (KERNEL32.42)
53 * NOTE: needs to preserve all registers!
55 void WINAPI REGS_FUNC(LogApiThkLSF)( LPSTR func, CONTEXT *context )
57 TRACE_(thunk)( "%s\n", debugstr_a(func) );
60 /***********************************************************************
61 * LogApiThkSL (KERNEL32.44)
63 * NOTE: needs to preserve all registers!
65 void WINAPI REGS_FUNC(LogApiThkSL)( LPSTR func, CONTEXT *context )
67 TRACE_(thunk)( "%s\n", debugstr_a(func) );
70 /***********************************************************************
71 * LogCBThkSL (KERNEL32.47)
73 * NOTE: needs to preserve all registers!
75 void WINAPI REGS_FUNC(LogCBThkSL)( LPSTR func, CONTEXT *context )
77 TRACE_(thunk)( "%s\n", debugstr_a(func) );
80 /***********************************************************************
81 * Generates a FT_Prolog call.
83 * 0FB6D1 movzbl edx,cl
84 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
85 * 68xxxxxxxx push FT_Prolog
86 * C3 lret
88 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
89 LPBYTE x;
91 x = relayCode;
92 *x++ = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
93 *x++ = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
94 x+=4; /* mov edx, [4*edx + targetTable] */
95 *x++ = 0x68; *(DWORD*)x = (DWORD)GetProcAddress(GetModuleHandleA("KERNEL32"),"FT_Prolog");
96 x+=4; /* push FT_Prolog */
97 *x++ = 0xC3; /* lret */
98 /* fill rest with 0xCC / int 3 */
101 /***********************************************************************
102 * _write_qtthunk (internal)
103 * Generates a QT_Thunk style call.
105 * 33C9 xor ecx, ecx
106 * 8A4DFC mov cl , [ebp-04]
107 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
108 * B8yyyyyyyy mov eax, QT_Thunk
109 * FFE0 jmp eax
111 static void _write_qtthunk(
112 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
113 DWORD *targetTable /* [in] start of thunk (for index lookup) */
115 LPBYTE x;
117 x = relayCode;
118 *x++ = 0x33;*x++=0xC9; /* xor ecx,ecx */
119 *x++ = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
120 *x++ = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
121 x+=4; /* mov edx, [4*ecx + targetTable */
122 *x++ = 0xB8; *(DWORD*)x = (DWORD)GetProcAddress(GetModuleHandleA("KERNEL32"),"QT_Thunk");
123 x+=4; /* mov eax , QT_Thunk */
124 *x++ = 0xFF; *x++ = 0xE0; /* jmp eax */
125 /* should fill the rest of the 32 bytes with 0xCC */
128 /***********************************************************************
129 * _loadthunk
131 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
132 struct ThunkDataCommon *TD32, DWORD checksum)
134 struct ThunkDataCommon *TD16;
135 HMODULE hmod;
136 int ordinal;
138 if ((hmod = LoadLibrary16(module)) <= 32)
140 ERR_(thunk)("(%s, %s, %s): Unable to load '%s', error %d\n",
141 module, func, module32, module, hmod);
142 return 0;
145 if ( !(ordinal = NE_GetOrdinal(hmod, func))
146 || !(TD16 = PTR_SEG_TO_LIN(NE_GetEntryPointEx(hmod, ordinal, FALSE))))
148 ERR_(thunk)("(%s, %s, %s): Unable to find '%s'\n",
149 module, func, module32, func);
150 return 0;
153 if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
155 ERR_(thunk)("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
156 module, func, module32,
157 TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
158 TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
159 return 0;
162 if (TD32 && TD16->checksum != TD32->checksum)
164 ERR_(thunk)("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
165 module, func, module32, TD16->checksum, TD32->checksum);
166 return 0;
169 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
171 ERR_(thunk)("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
172 module, func, module32, *(LPDWORD)TD16, checksum);
173 return 0;
176 return TD16;
179 /***********************************************************************
180 * GetThunkStuff (KERNEL32.53)
182 LPVOID WINAPI GetThunkStuff(LPSTR module, LPSTR func)
184 return _loadthunk(module, func, "<kernel>", NULL, 0L);
187 /***********************************************************************
188 * GetThunkBuff (KERNEL32.52)
189 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
191 LPVOID WINAPI GetThunkBuff(void)
193 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
196 /***********************************************************************
197 * ThunkConnect32 (KERNEL32)
198 * Connects a 32bit and a 16bit thunkbuffer.
200 UINT WINAPI ThunkConnect32(
201 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
202 LPSTR thunkfun16, /* [in] win16 thunkfunction */
203 LPSTR module16, /* [in] name of win16 dll */
204 LPSTR module32, /* [in] name of win32 dll */
205 HMODULE hmod32, /* [in] hmodule of win32 dll */
206 DWORD dwReason /* [in] initialisation argument */
208 BOOL directionSL;
210 if (!lstrncmpA(TD->magic, "SL01", 4))
212 directionSL = TRUE;
214 TRACE_(thunk)("SL01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
215 module32, (DWORD)TD, module16, thunkfun16, dwReason);
217 else if (!lstrncmpA(TD->magic, "LS01", 4))
219 directionSL = FALSE;
221 TRACE_(thunk)("LS01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
222 module32, (DWORD)TD, module16, thunkfun16, dwReason);
224 else
226 ERR_(thunk)("Invalid magic %c%c%c%c\n",
227 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
228 return 0;
231 switch (dwReason)
233 case DLL_PROCESS_ATTACH:
235 struct ThunkDataCommon *TD16;
236 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
237 return 0;
239 if (directionSL)
241 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
242 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
243 struct SLTargetDB *tdb;
245 if (SL16->fpData == NULL)
247 ERR_(thunk)("ThunkConnect16 was not called!\n");
248 return 0;
251 SL32->data = SL16->fpData;
253 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
254 tdb->process = PROCESS_Current();
255 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
257 tdb->next = SL32->data->targetDB; /* FIXME: not thread-safe! */
258 SL32->data->targetDB = tdb;
260 TRACE_(thunk)("Process %08lx allocated TargetDB entry for ThunkDataSL %08lx\n",
261 (DWORD)PROCESS_Current(), (DWORD)SL32->data);
263 else
265 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
266 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
268 LS32->targetTable = PTR_SEG_TO_LIN(LS16->targetTable);
270 /* write QT_Thunk and FT_Prolog stubs */
271 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk, LS32->targetTable);
272 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
274 break;
277 case DLL_PROCESS_DETACH:
278 /* FIXME: cleanup */
279 break;
282 return 1;
285 /**********************************************************************
286 * QT_Thunk (KERNEL32)
288 * The target address is in EDX.
289 * The 16 bit arguments start at ESP.
290 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
291 * [ok]
293 void WINAPI REGS_FUNC(QT_Thunk)( CONTEXT *context )
295 CONTEXT context16;
296 DWORD argsize;
297 THDB *thdb = THREAD_Current();
299 memcpy(&context16,context,sizeof(context16));
301 CS_reg(&context16) = HIWORD(EDX_reg(context));
302 IP_reg(&context16) = LOWORD(EDX_reg(context));
303 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
304 + (WORD)&((STACK16FRAME*)0)->bp;
306 argsize = EBP_reg(context)-ESP_reg(context)-0x40;
308 memcpy( ((LPBYTE)THREAD_STACK16(thdb))-argsize,
309 (LPBYTE)ESP_reg(context), argsize );
311 EAX_reg(context) = Callbacks->CallRegisterShortProc( &context16, argsize );
312 EDX_reg(context) = HIWORD(EAX_reg(context));
313 EAX_reg(context) = LOWORD(EAX_reg(context));
317 /**********************************************************************
318 * FT_Prolog (KERNEL32.233)
320 * The set of FT_... thunk routines is used instead of QT_Thunk,
321 * if structures have to be converted from 32-bit to 16-bit
322 * (change of member alignment, conversion of members).
324 * The thunk function (as created by the thunk compiler) calls
325 * FT_Prolog at the beginning, to set up a stack frame and
326 * allocate a 64 byte buffer on the stack.
327 * The input parameters (target address and some flags) are
328 * saved for later use by FT_Thunk.
330 * Input: EDX 16-bit target address (SEGPTR)
331 * CX bits 0..7 target number (in target table)
332 * bits 8..9 some flags (unclear???)
333 * bits 10..15 number of DWORD arguments
335 * Output: A new stackframe is created, and a 64 byte buffer
336 * allocated on the stack. The layout of the stack
337 * on return is as follows:
339 * (ebp+4) return address to caller of thunk function
340 * (ebp) old EBP
341 * (ebp-4) saved EBX register of caller
342 * (ebp-8) saved ESI register of caller
343 * (ebp-12) saved EDI register of caller
344 * (ebp-16) saved ECX register, containing flags
345 * (ebp-20) bitmap containing parameters that are to be converted
346 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
347 * filled in by the thunk code before calling FT_Thunk
348 * (ebp-24)
349 * ... (unclear)
350 * (ebp-44)
351 * (ebp-48) saved EAX register of caller (unclear, never restored???)
352 * (ebp-52) saved EDX register, containing 16-bit thunk target
353 * (ebp-56)
354 * ... (unclear)
355 * (ebp-64)
357 * ESP is EBP-64 after return.
361 void WINAPI REGS_FUNC(FT_Prolog)( CONTEXT *context )
363 /* Build stack frame */
364 STACK32_PUSH(context, EBP_reg(context));
365 EBP_reg(context) = ESP_reg(context);
367 /* Allocate 64-byte Thunk Buffer */
368 ESP_reg(context) -= 64;
369 memset((char *)ESP_reg(context), '\0', 64);
371 /* Store Flags (ECX) and Target Address (EDX) */
372 /* Save other registers to be restored later */
373 *(DWORD *)(EBP_reg(context) - 4) = EBX_reg(context);
374 *(DWORD *)(EBP_reg(context) - 8) = ESI_reg(context);
375 *(DWORD *)(EBP_reg(context) - 12) = EDI_reg(context);
376 *(DWORD *)(EBP_reg(context) - 16) = ECX_reg(context);
378 *(DWORD *)(EBP_reg(context) - 48) = EAX_reg(context);
379 *(DWORD *)(EBP_reg(context) - 52) = EDX_reg(context);
382 /**********************************************************************
383 * FT_Thunk (KERNEL32.234)
385 * This routine performs the actual call to 16-bit code,
386 * similar to QT_Thunk. The differences are:
387 * - The call target is taken from the buffer created by FT_Prolog
388 * - Those arguments requested by the thunk code (by setting the
389 * corresponding bit in the bitmap at EBP-20) are converted
390 * from 32-bit pointers to segmented pointers (those pointers
391 * are guaranteed to point to structures copied to the stack
392 * by the thunk code, so we always use the 16-bit stack selector
393 * for those addresses).
395 * The bit #i of EBP-20 corresponds here to the DWORD starting at
396 * ESP+4 + 2*i.
398 * FIXME: It is unclear what happens if there are more than 32 WORDs
399 * of arguments, so that the single DWORD bitmap is no longer
400 * sufficient ...
403 void WINAPI REGS_FUNC(FT_Thunk)( CONTEXT *context )
405 DWORD mapESPrelative = *(DWORD *)(EBP_reg(context) - 20);
406 DWORD callTarget = *(DWORD *)(EBP_reg(context) - 52);
408 CONTEXT context16;
409 DWORD i, argsize;
410 LPBYTE newstack, oldstack;
411 THDB *thdb = THREAD_Current();
413 memcpy(&context16,context,sizeof(context16));
415 CS_reg(&context16) = HIWORD(callTarget);
416 IP_reg(&context16) = LOWORD(callTarget);
417 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
418 + (WORD)&((STACK16FRAME*)0)->bp;
420 argsize = EBP_reg(context)-ESP_reg(context)-0x40;
421 newstack = ((LPBYTE)THREAD_STACK16(thdb))-argsize;
422 oldstack = (LPBYTE)ESP_reg(context);
424 memcpy( newstack, oldstack, argsize );
426 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
427 if (mapESPrelative & (1 << i))
429 SEGPTR *arg = (SEGPTR *)(newstack + 2*i);
430 *arg = PTR_SEG_OFF_TO_SEGPTR(SELECTOROF(thdb->cur_stack),
431 OFFSETOF(thdb->cur_stack) - argsize
432 + (*(LPBYTE *)arg - oldstack));
435 EAX_reg(context) = Callbacks->CallRegisterShortProc( &context16, argsize );
436 EDX_reg(context) = HIWORD(EAX_reg(context));
437 EAX_reg(context) = LOWORD(EAX_reg(context));
439 /* Copy modified buffers back to 32-bit stack */
440 memcpy( oldstack, newstack, argsize );
443 /**********************************************************************
444 * FT_ExitNN (KERNEL32.218 - 232)
446 * One of the FT_ExitNN functions is called at the end of the thunk code.
447 * It removes the stack frame created by FT_Prolog, moves the function
448 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
449 * value, but the thunk code has moved it from EAX to EBX in the
450 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
451 * and perform a return to the CALLER of the thunk code (while removing
452 * the given number of arguments from the caller's stack).
455 static void FT_Exit(CONTEXT *context, int nPopArgs)
457 /* Return value is in EBX */
458 EAX_reg(context) = EBX_reg(context);
460 /* Restore EBX, ESI, and EDI registers */
461 EBX_reg(context) = *(DWORD *)(EBP_reg(context) - 4);
462 ESI_reg(context) = *(DWORD *)(EBP_reg(context) - 8);
463 EDI_reg(context) = *(DWORD *)(EBP_reg(context) - 12);
465 /* Clean up stack frame */
466 ESP_reg(context) = EBP_reg(context);
467 EBP_reg(context) = STACK32_POP(context);
469 /* Pop return address to CALLER of thunk code */
470 EIP_reg(context) = STACK32_POP(context);
471 /* Remove arguments */
472 ESP_reg(context) += nPopArgs;
475 void WINAPI REGS_FUNC(FT_Exit0)(CONTEXT *context) { FT_Exit(context, 0); }
476 void WINAPI REGS_FUNC(FT_Exit4)(CONTEXT *context) { FT_Exit(context, 4); }
477 void WINAPI REGS_FUNC(FT_Exit8)(CONTEXT *context) { FT_Exit(context, 8); }
478 void WINAPI REGS_FUNC(FT_Exit12)(CONTEXT *context) { FT_Exit(context, 12); }
479 void WINAPI REGS_FUNC(FT_Exit16)(CONTEXT *context) { FT_Exit(context, 16); }
480 void WINAPI REGS_FUNC(FT_Exit20)(CONTEXT *context) { FT_Exit(context, 20); }
481 void WINAPI REGS_FUNC(FT_Exit24)(CONTEXT *context) { FT_Exit(context, 24); }
482 void WINAPI REGS_FUNC(FT_Exit28)(CONTEXT *context) { FT_Exit(context, 28); }
483 void WINAPI REGS_FUNC(FT_Exit32)(CONTEXT *context) { FT_Exit(context, 32); }
484 void WINAPI REGS_FUNC(FT_Exit36)(CONTEXT *context) { FT_Exit(context, 36); }
485 void WINAPI REGS_FUNC(FT_Exit40)(CONTEXT *context) { FT_Exit(context, 40); }
486 void WINAPI REGS_FUNC(FT_Exit44)(CONTEXT *context) { FT_Exit(context, 44); }
487 void WINAPI REGS_FUNC(FT_Exit48)(CONTEXT *context) { FT_Exit(context, 48); }
488 void WINAPI REGS_FUNC(FT_Exit52)(CONTEXT *context) { FT_Exit(context, 52); }
489 void WINAPI REGS_FUNC(FT_Exit56)(CONTEXT *context) { FT_Exit(context, 56); }
492 /**********************************************************************
493 * WOWCallback16 (KERNEL32.62)(WOW32.2)
494 * Calls a win16 function with a single DWORD argument.
495 * RETURNS
496 * the return value
498 DWORD WINAPI WOWCallback16(
499 FARPROC16 fproc, /* [in] win16 function to call */
500 DWORD arg /* [in] single DWORD argument to function */
502 DWORD ret;
503 TRACE_(thunk)("(%p,0x%08lx)...\n",fproc,arg);
504 ret = Callbacks->CallWOWCallbackProc(fproc,arg);
505 TRACE_(thunk)("... returns %ld\n",ret);
506 return ret;
509 /**********************************************************************
510 * WOWCallback16Ex (KERNEL32.55)(WOW32.3)
511 * Calls a function in 16bit code.
512 * RETURNS
513 * TRUE for success
515 BOOL WINAPI WOWCallback16Ex(
516 FARPROC16 vpfn16, /* [in] win16 function to call */
517 DWORD dwFlags, /* [in] flags */
518 DWORD cbArgs, /* [in] nr of arguments */
519 LPVOID pArgs, /* [in] pointer to arguments (LPDWORD) */
520 LPDWORD pdwRetCode /* [out] return value of win16 function */
522 return Callbacks->CallWOWCallback16Ex(vpfn16,dwFlags,cbArgs,pArgs,pdwRetCode);
525 /***********************************************************************
526 * ThunkInitLS (KERNEL32.43)
527 * A thunkbuffer link routine
528 * The thunkbuf looks like:
530 * 00: DWORD length ? don't know exactly
531 * 04: SEGPTR ptr ? where does it point to?
532 * The pointer ptr is written into the first DWORD of 'thunk'.
533 * (probably correct implemented)
534 * [ok probably]
535 * RETURNS
536 * segmented pointer to thunk?
538 DWORD WINAPI ThunkInitLS(
539 LPDWORD thunk, /* [in] win32 thunk */
540 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
541 DWORD len, /* [in] thkbuffer length */
542 LPCSTR dll16, /* [in] name of win16 dll */
543 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
545 LPDWORD addr;
547 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
548 return 0;
550 if (!addr[1])
551 return 0;
552 *(DWORD*)thunk = addr[1];
554 return addr[1];
557 /***********************************************************************
558 * Common32ThkLS (KERNEL32.45)
560 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
561 * style thunks. The basic difference is that the parameter conversion
562 * is done completely on the *16-bit* side here. Thus we do not call
563 * the 16-bit target directly, but call a common entry point instead.
564 * This entry function then calls the target according to the target
565 * number passed in the DI register.
567 * Input: EAX SEGPTR to the common 16-bit entry point
568 * CX offset in thunk table (target number * 4)
569 * DX error return value if execution fails (unclear???)
570 * EDX.HI number of DWORD parameters
572 * (Note that we need to move the thunk table offset from CX to DI !)
574 * The called 16-bit stub expects its stack to look like this:
575 * ...
576 * (esp+40) 32-bit arguments
577 * ...
578 * (esp+8) 32 byte of stack space available as buffer
579 * (esp) 8 byte return address for use with 0x66 lret
581 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
582 * and uses the EAX register to return a DWORD return value.
583 * Thus we need to use a special assembly glue routine
584 * (CallRegisterLongProc instead of CallRegisterShortProc).
586 * Finally, we return to the caller, popping the arguments off
587 * the stack.
589 * FIXME: The called function uses EBX to return the number of
590 * arguments that are to be popped off the caller's stack.
591 * This is clobbered by the assembly glue, so we simply use
592 * the original EDX.HI to get the number of arguments.
593 * (Those two values should be equal anyway ...?)
596 void WINAPI REGS_FUNC(Common32ThkLS)( CONTEXT *context )
598 CONTEXT context16;
599 DWORD argsize;
600 THDB *thdb = THREAD_Current();
602 memcpy(&context16,context,sizeof(context16));
604 DI_reg(&context16) = CX_reg(context);
605 CS_reg(&context16) = HIWORD(EAX_reg(context));
606 IP_reg(&context16) = LOWORD(EAX_reg(context));
607 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
608 + (WORD)&((STACK16FRAME*)0)->bp;
610 argsize = HIWORD(EDX_reg(context)) * 4;
612 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
613 if (EDX_reg(context) == EIP_reg(context))
614 argsize = 6 * 4;
616 memcpy( ((LPBYTE)THREAD_STACK16(thdb))-argsize,
617 (LPBYTE)ESP_reg(context), argsize );
619 EAX_reg(context) = Callbacks->CallRegisterLongProc(&context16, argsize + 32);
621 /* Clean up caller's stack frame */
622 ESP_reg(context) += argsize;
625 /***********************************************************************
626 * OT_32ThkLSF (KERNEL32.40)
628 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
629 * argument processing is done on both the 32-bit and the 16-bit side:
630 * The 32-bit side prepares arguments, copying them onto the stack.
632 * When this routine is called, the first word on the stack is the
633 * number of argument bytes prepared by the 32-bit code, and EDX
634 * contains the 16-bit target address.
636 * The called 16-bit routine is another relaycode, doing further
637 * argument processing and then calling the real 16-bit target
638 * whose address is stored at [bp-04].
640 * The call proceeds using a normal CallRegisterShortProc.
641 * After return from the 16-bit relaycode, the arguments need
642 * to be copied *back* to the 32-bit stack, since the 32-bit
643 * relaycode processes output parameters.
645 * Note that we copy twice the number of arguments, since some of the
646 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
647 * arguments of the caller!
649 * (Note that this function seems only to be used for
650 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
652 void WINAPI REGS_FUNC(OT_32ThkLSF)( CONTEXT *context )
654 CONTEXT context16;
655 DWORD argsize;
656 THDB *thdb = THREAD_Current();
658 memcpy(&context16,context,sizeof(context16));
660 CS_reg(&context16) = HIWORD(EDX_reg(context));
661 IP_reg(&context16) = LOWORD(EDX_reg(context));
662 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
663 + (WORD)&((STACK16FRAME*)0)->bp;
665 argsize = 2 * *(WORD *)ESP_reg(context) + 2;
667 memcpy( ((LPBYTE)THREAD_STACK16(thdb))-argsize,
668 (LPBYTE)ESP_reg(context), argsize );
670 EAX_reg(context) = Callbacks->CallRegisterShortProc(&context16, argsize);
672 memcpy( (LPBYTE)ESP_reg(context),
673 ((LPBYTE)THREAD_STACK16(thdb))-argsize, argsize );
676 /***********************************************************************
677 * ThunkInitLSF (KERNEL32.41)
678 * A thunk setup routine.
679 * Expects a pointer to a preinitialized thunkbuffer in the first argument
680 * looking like:
681 * 00..03: unknown (pointer, check _41, _43, _46)
682 * 04: EB1E jmp +0x20
684 * 06..23: unknown (space for replacement code, check .90)
686 * 24:>E800000000 call offset 29
687 * 29:>58 pop eax ( target of call )
688 * 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
689 * 2F: BAxxxxxxxx mov edx,xxxxxxxx
690 * 34: 68yyyyyyyy push KERNEL32.90
691 * 39: C3 ret
693 * 3A: EB1E jmp +0x20
694 * 3E ... 59: unknown (space for replacement code?)
695 * 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
696 * 5F: 5A pop edx
697 * 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
698 * 66: 52 push edx
699 * 67: 68xxxxxxxx push xxxxxxxx
700 * 6C: 68yyyyyyyy push KERNEL32.89
701 * 71: C3 ret
702 * 72: end?
703 * This function checks if the code is there, and replaces the yyyyyyyy entries
704 * by the functionpointers.
705 * The thunkbuf looks like:
707 * 00: DWORD length ? don't know exactly
708 * 04: SEGPTR ptr ? where does it point to?
709 * The segpointer ptr is written into the first DWORD of 'thunk'.
710 * [ok probably]
711 * RETURNS
712 * unclear, pointer to win16 thkbuffer?
714 LPVOID WINAPI ThunkInitLSF(
715 LPBYTE thunk, /* [in] win32 thunk */
716 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
717 DWORD len, /* [in] length of thkbuffer */
718 LPCSTR dll16, /* [in] name of win16 dll */
719 LPCSTR dll32 /* [in] name of win32 dll */
721 HMODULE hkrnl32 = GetModuleHandleA("KERNEL32");
722 LPDWORD addr,addr2;
724 /* FIXME: add checks for valid code ... */
725 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
726 *(DWORD*)(thunk+0x35) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)90);
727 *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)89);
730 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
731 return 0;
733 addr2 = PTR_SEG_TO_LIN(addr[1]);
734 if (HIWORD(addr2))
735 *(DWORD*)thunk = (DWORD)addr2;
737 return addr2;
740 /***********************************************************************
741 * FT_PrologPrime (KERNEL32.89)
743 * This function is called from the relay code installed by
744 * ThunkInitLSF. It replaces the location from where it was
745 * called by a standard FT_Prolog call stub (which is 'primed'
746 * by inserting the correct target table pointer).
747 * Finally, it calls that stub.
749 * Input: ECX target number + flags (passed through to FT_Prolog)
750 * (ESP) offset of location where target table pointer
751 * is stored, relative to the start of the relay code
752 * (ESP+4) pointer to start of relay code
753 * (this is where the FT_Prolog call stub gets written to)
755 * Note: The two DWORD arguments get popped off the stack.
758 void WINAPI REGS_FUNC(FT_PrologPrime)( CONTEXT *context )
760 DWORD targetTableOffset;
761 LPBYTE relayCode;
763 /* Compensate for the fact that the Wine register relay code thought
764 we were being called, although we were in fact jumped to */
765 ESP_reg(context) -= 4;
767 /* Write FT_Prolog call stub */
768 targetTableOffset = STACK32_POP(context);
769 relayCode = (LPBYTE)STACK32_POP(context);
770 _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
772 /* Jump to the call stub just created */
773 EIP_reg(context) = (DWORD)relayCode;
776 /***********************************************************************
777 * QT_ThunkPrime (KERNEL32.90)
779 * This function corresponds to FT_PrologPrime, but installs a
780 * call stub for QT_Thunk instead.
782 * Input: (EBP-4) target number (passed through to QT_Thunk)
783 * EDX target table pointer location offset
784 * EAX start of relay code
787 void WINAPI REGS_FUNC(QT_ThunkPrime)( CONTEXT *context )
789 DWORD targetTableOffset;
790 LPBYTE relayCode;
792 /* Compensate for the fact that the Wine register relay code thought
793 we were being called, although we were in fact jumped to */
794 ESP_reg(context) -= 4;
796 /* Write QT_Thunk call stub */
797 targetTableOffset = EDX_reg(context);
798 relayCode = (LPBYTE)EAX_reg(context);
799 _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );
801 /* Jump to the call stub just created */
802 EIP_reg(context) = (DWORD)relayCode;
805 /***********************************************************************
806 * (KERNEL32.46)
807 * Another thunkbuf link routine.
808 * The start of the thunkbuf looks like this:
809 * 00: DWORD length
810 * 04: SEGPTR address for thunkbuffer pointer
811 * [ok probably]
813 VOID WINAPI ThunkInitSL(
814 LPBYTE thunk, /* [in] start of thunkbuffer */
815 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
816 DWORD len, /* [in] length of thunkbuffer */
817 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
818 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
820 LPDWORD addr;
822 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
823 return;
825 *(DWORD*)PTR_SEG_TO_LIN(addr[1]) = (DWORD)thunk;
828 /**********************************************************************
829 * SSInit KERNEL.700
830 * RETURNS
831 * TRUE for success.
833 BOOL WINAPI SSInit16()
835 return TRUE;
838 /**********************************************************************
839 * SSOnBigStack KERNEL32.87
840 * Check if thunking is initialized (ss selector set up etc.)
841 * We do that differently, so just return TRUE.
842 * [ok]
843 * RETURNS
844 * TRUE for success.
846 BOOL WINAPI SSOnBigStack()
848 TRACE_(thunk)("Yes, thunking is initialized\n");
849 return TRUE;
852 /**********************************************************************
853 * SSConfirmSmallStack KERNEL.704
855 * Abort if not on small stack.
857 * This must be a register routine as it has to preserve *all* registers.
859 void WINAPI SSConfirmSmallStack( CONTEXT *context )
861 /* We are always on the small stack while in 16-bit code ... */
864 /**********************************************************************
865 * SSCall
866 * One of the real thunking functions. This one seems to be for 32<->32
867 * thunks. It should probably be capable of crossing processboundaries.
869 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
870 * [ok]
872 DWORD WINAPIV SSCall(
873 DWORD nr, /* [in] number of argument bytes */
874 DWORD flags, /* [in] FIXME: flags ? */
875 FARPROC fun, /* [in] function to call */
876 ... /* [in/out] arguments */
878 DWORD i,ret;
879 DWORD *args = ((DWORD *)&fun) + 1;
881 if(TRACE_ON(thunk)){
882 dbg_decl_str(thunk, 256);
883 for (i=0;i<nr/4;i++)
884 dsprintf(thunk,"0x%08lx,",args[i]);
885 TRACE_(thunk)("(%ld,0x%08lx,%p,[%s])\n",
886 nr,flags,fun,dbg_str(thunk));
888 switch (nr) {
889 case 0: ret = fun();
890 break;
891 case 4: ret = fun(args[0]);
892 break;
893 case 8: ret = fun(args[0],args[1]);
894 break;
895 case 12: ret = fun(args[0],args[1],args[2]);
896 break;
897 case 16: ret = fun(args[0],args[1],args[2],args[3]);
898 break;
899 case 20: ret = fun(args[0],args[1],args[2],args[3],args[4]);
900 break;
901 case 24: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
902 break;
903 case 28: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
904 break;
905 case 32: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
906 break;
907 case 36: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
908 break;
909 case 40: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
910 break;
911 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]);
912 break;
913 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]);
914 break;
915 default:
916 WARN_(thunk)("Unsupported nr of arguments, %ld\n",nr);
917 ret = 0;
918 break;
921 TRACE_(thunk)(" returning %ld ...\n",ret);
922 return ret;
925 /**********************************************************************
926 * W32S_BackTo32 (KERNEL32.51)
928 void WINAPI REGS_FUNC(W32S_BackTo32)( CONTEXT *context )
930 LPDWORD stack = (LPDWORD)ESP_reg( context );
931 FARPROC proc = (FARPROC)EIP_reg(context);
933 EAX_reg( context ) = proc( stack[1], stack[2], stack[3], stack[4], stack[5],
934 stack[6], stack[7], stack[8], stack[9], stack[10] );
936 EIP_reg( context ) = STACK32_POP(context);
939 /**********************************************************************
940 * AllocSLCallback (KERNEL32)
942 * Win95 uses some structchains for callbacks. It allocates them
943 * in blocks of 100 entries, size 32 bytes each, layout:
944 * blockstart:
945 * 0: PTR nextblockstart
946 * 4: entry *first;
947 * 8: WORD sel ( start points to blockstart)
948 * A: WORD unknown
949 * 100xentry:
950 * 00..17: Code
951 * 18: PDB *owning_process;
952 * 1C: PTR blockstart
954 * We ignore this for now. (Just a note for further developers)
955 * FIXME: use this method, so we don't waste selectors...
957 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
958 * the 0x66 prefix switches from word->long registers.
960 * 665A pop edx
961 * 6668x arg2 x pushl <arg2>
962 * 6652 push edx
963 * EAx arg1 x jmpf <arg1>
965 * returns the startaddress of this thunk.
967 * Note, that they look very similair to the ones allocates by THUNK_Alloc.
968 * RETURNS
969 * segmented pointer to the start of the thunk
971 DWORD WINAPI
972 AllocSLCallback(
973 DWORD finalizer, /* [in] finalizer function */
974 DWORD callback /* [in] callback function */
976 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
977 WORD sel;
979 x=thunk;
980 *x++=0x66;*x++=0x5a; /* popl edx */
981 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
982 *x++=0x66;*x++=0x52; /* pushl edx */
983 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
985 *(PDB**)(thunk+18) = PROCESS_Current();
987 sel = SELECTOR_AllocBlock( thunk , 32, SEGMENT_CODE, FALSE, FALSE );
988 return (sel<<16)|0;
991 /**********************************************************************
992 * FreeSLCallback (KERNEL32.274)
993 * Frees the specified 16->32 callback
995 void WINAPI
996 FreeSLCallback(
997 DWORD x /* [in] 16 bit callback (segmented pointer?) */
999 FIXME_(win32)("(0x%08lx): stub\n",x);
1003 /**********************************************************************
1004 * GetTEBSelectorFS (KERNEL.475)
1005 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1007 VOID WINAPI GetTEBSelectorFS16( CONTEXT *context )
1009 GET_FS( FS_reg(context) );
1012 /**********************************************************************
1013 * KERNEL_431 (KERNEL.431)
1014 * IsPeFormat (W32SYS.2)
1015 * Checks the passed filename if it is a PE format executeable
1016 * RETURNS
1017 * TRUE, if it is.
1018 * FALSE if not.
1020 BOOL16 WINAPI IsPeFormat16(
1021 LPSTR fn, /* [in] filename to executeable */
1022 HFILE16 hf16 /* [in] open file, if filename is NULL */
1024 IMAGE_DOS_HEADER mzh;
1025 HFILE hf=FILE_GetHandle(hf16);
1026 OFSTRUCT ofs;
1027 DWORD xmagic;
1029 if (fn) {
1030 hf = OpenFile(fn,&ofs,OF_READ);
1031 if (hf==HFILE_ERROR)
1032 return FALSE;
1034 _llseek(hf,0,SEEK_SET);
1035 if (sizeof(mzh)!=_lread(hf,&mzh,sizeof(mzh))) {
1036 _lclose(hf);
1037 return FALSE;
1039 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) {
1040 WARN_(dosmem)("File has not got dos signature!\n");
1041 _lclose(hf);
1042 return FALSE;
1044 _llseek(hf,mzh.e_lfanew,SEEK_SET);
1045 if (sizeof(DWORD)!=_lread(hf,&xmagic,sizeof(DWORD))) {
1046 _lclose(hf);
1047 return FALSE;
1049 _lclose(hf);
1050 return (xmagic == IMAGE_NT_SIGNATURE);
1053 /***********************************************************************
1054 * WOWHandle32 (KERNEL32.57)(WOW32.16)
1055 * Converts a win16 handle of type into the respective win32 handle.
1056 * We currently just return this handle, since most handles are the same
1057 * for win16 and win32.
1058 * RETURNS
1059 * The new handle
1061 HANDLE WINAPI WOWHandle32(
1062 WORD handle, /* [in] win16 handle */
1063 WOW_HANDLE_TYPE type /* [in] handle type */
1065 TRACE_(win32)("(0x%04x,%d)\n",handle,type);
1066 return (HANDLE)handle;
1069 /***********************************************************************
1070 * K32Thk1632Prolog (KERNEL32.492)
1072 void WINAPI REGS_FUNC(K32Thk1632Prolog)( CONTEXT *context )
1074 LPBYTE code = (LPBYTE)EIP_reg(context) - 5;
1076 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1077 of 16->32 thunks instead of using one of the standard methods!
1078 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1079 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1080 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1081 bypassed, which means it will crash the next time the 32-bit OLE
1082 code thunks down again to 16-bit (this *will* happen!).
1084 The following hack tries to recognize this situation.
1085 This is possible since the called stubs in OLECLI32/OLESVR32 all
1086 look exactly the same:
1087 00 E8xxxxxxxx call K32Thk1632Prolog
1088 05 FF55FC call [ebp-04]
1089 08 E8xxxxxxxx call K32Thk1632Epilog
1090 0D 66CB retf
1092 If we recognize this situation, we try to simulate the actions
1093 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1094 to our 32-bit stack, creating a proper STACK16FRAME and
1095 updating thdb->cur_stack. */
1097 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1098 && code[13] == 0x66 && code[14] == 0xCB)
1100 WORD stackSel = NtCurrentTeb()->stack_sel;
1101 DWORD stackBase = GetSelectorBase(stackSel);
1103 THDB *thdb = THREAD_Current();
1104 DWORD argSize = EBP_reg(context) - ESP_reg(context);
1105 char *stack16 = (char *)ESP_reg(context) - 4;
1106 char *stack32 = (char *)thdb->cur_stack - argSize;
1107 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1109 TRACE_(thunk)("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1110 EBP_reg(context), ESP_reg(context), thdb->cur_stack);
1112 memset(frame16, '\0', sizeof(STACK16FRAME));
1113 frame16->frame32 = (STACK32FRAME *)thdb->cur_stack;
1114 frame16->ebp = EBP_reg(context);
1116 memcpy(stack32, stack16, argSize);
1117 thdb->cur_stack = PTR_SEG_OFF_TO_SEGPTR(stackSel, (DWORD)frame16 - stackBase);
1119 ESP_reg(context) = (DWORD)stack32 + 4;
1120 EBP_reg(context) = ESP_reg(context) + argSize;
1122 TRACE_(thunk)("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1123 EBP_reg(context), ESP_reg(context), thdb->cur_stack);
1126 SYSLEVEL_ReleaseWin16Lock();
1129 /***********************************************************************
1130 * K32Thk1632Epilog (KERNEL32.491)
1132 void WINAPI REGS_FUNC(K32Thk1632Epilog)( CONTEXT *context )
1134 LPBYTE code = (LPBYTE)EIP_reg(context) - 13;
1136 SYSLEVEL_RestoreWin16Lock();
1138 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1140 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1141 && code[13] == 0x66 && code[14] == 0xCB)
1143 THDB *thdb = THREAD_Current();
1144 STACK16FRAME *frame16 = (STACK16FRAME *)PTR_SEG_TO_LIN(thdb->cur_stack);
1145 char *stack16 = (char *)(frame16 + 1);
1146 DWORD argSize = frame16->ebp - (DWORD)stack16;
1147 char *stack32 = (char *)frame16->frame32 - argSize;
1149 DWORD nArgsPopped = ESP_reg(context) - (DWORD)stack32;
1151 TRACE_(thunk)("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1152 EBP_reg(context), ESP_reg(context), thdb->cur_stack);
1154 thdb->cur_stack = (DWORD)frame16->frame32;
1156 ESP_reg(context) = (DWORD)stack16 + nArgsPopped;
1157 EBP_reg(context) = frame16->ebp;
1159 TRACE_(thunk)("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1160 EBP_reg(context), ESP_reg(context), thdb->cur_stack);
1164 /***********************************************************************
1165 * UpdateResource32A (KERNEL32.707)
1167 BOOL WINAPI UpdateResourceA(
1168 HANDLE hUpdate,
1169 LPCSTR lpType,
1170 LPCSTR lpName,
1171 WORD wLanguage,
1172 LPVOID lpData,
1173 DWORD cbData) {
1175 FIXME_(win32)(": stub\n");
1176 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1177 return FALSE;
1180 /***********************************************************************
1181 * UpdateResource32W (KERNEL32.708)
1183 BOOL WINAPI UpdateResourceW(
1184 HANDLE hUpdate,
1185 LPCWSTR lpType,
1186 LPCWSTR lpName,
1187 WORD wLanguage,
1188 LPVOID lpData,
1189 DWORD cbData) {
1191 FIXME_(win32)(": stub\n");
1192 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1193 return FALSE;
1197 /***********************************************************************
1198 * WaitNamedPipe32A [KERNEL32.725]
1200 BOOL WINAPI WaitNamedPipeA (LPCSTR lpNamedPipeName, DWORD nTimeOut)
1201 { FIXME_(win32)("%s 0x%08lx\n",lpNamedPipeName,nTimeOut);
1202 SetLastError(ERROR_PIPE_NOT_CONNECTED);
1203 return FALSE;
1205 /***********************************************************************
1206 * WaitNamedPipe32W [KERNEL32.726]
1208 BOOL WINAPI WaitNamedPipeW (LPCWSTR lpNamedPipeName, DWORD nTimeOut)
1209 { FIXME_(win32)("%s 0x%08lx\n",debugstr_w(lpNamedPipeName),nTimeOut);
1210 SetLastError(ERROR_PIPE_NOT_CONNECTED);
1211 return FALSE;
1214 /*********************************************************************
1215 * PK16FNF [KERNEL32.91]
1217 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1218 * string buffer with the 8.3 filename of a recently loaded 16-bit
1219 * module. It is unknown exactly what modules trigger this
1220 * mechanism or what purpose this serves. Win98 Explorer (and
1221 * probably also Win95 with IE 4 shell integration) calls this
1222 * several times during initialization.
1224 * FIXME: find out what this really does and make it work.
1226 void WINAPI PK16FNF(LPSTR strPtr)
1228 FIXME_(win32)("(%p): stub\n", strPtr);
1230 /* fill in a fake filename that'll be easy to recognize */
1231 lstrcpyA(strPtr, "WINESTUB.FIX");