Fixed -debugmsg +server.
[wine/wine-kai.git] / if1632 / thunk.c
blob10b6223ead0414285f4c7436017ffb53d291ab3e
1 /*
2 * Emulator thunks
4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
6 */
8 #include <string.h>
9 #include "wine/winbase16.h"
10 #include "task.h"
11 #include "hook.h"
12 #include "callback.h"
13 #include "builtin16.h"
14 #include "user.h"
15 #include "heap.h"
16 #include "neexe.h"
17 #include "process.h"
18 #include "stackframe.h"
19 #include "win.h"
20 #include "flatthunk.h"
21 #include "mouse.h"
22 #include "keyboard.h"
23 #include "debugtools.h"
25 DECLARE_DEBUG_CHANNEL(thunk)
28 /* List of the 16-bit callback functions. This list is used */
29 /* by the build program to generate the file if1632/callto16.S */
31 /* ### start build ### */
32 extern WORD CALLBACK THUNK_CallTo16_word_ (FARPROC16);
33 extern LONG CALLBACK THUNK_CallTo16_long_ (FARPROC16);
34 extern WORD CALLBACK THUNK_CallTo16_word_w (FARPROC16,WORD);
35 extern WORD CALLBACK THUNK_CallTo16_word_l (FARPROC16,LONG);
36 extern LONG CALLBACK THUNK_CallTo16_long_l (FARPROC16,LONG);
37 extern WORD CALLBACK THUNK_CallTo16_word_ww (FARPROC16,WORD,WORD);
38 extern WORD CALLBACK THUNK_CallTo16_word_wl (FARPROC16,WORD,LONG);
39 extern WORD CALLBACK THUNK_CallTo16_word_ll (FARPROC16,LONG,LONG);
40 extern LONG CALLBACK THUNK_CallTo16_long_ll (FARPROC16,LONG,LONG);
41 extern WORD CALLBACK THUNK_CallTo16_word_www (FARPROC16,WORD,WORD,WORD);
42 extern WORD CALLBACK THUNK_CallTo16_word_wwl (FARPROC16,WORD,WORD,LONG);
43 extern WORD CALLBACK THUNK_CallTo16_word_wlw (FARPROC16,WORD,LONG,WORD);
44 extern WORD CALLBACK THUNK_CallTo16_word_llwl (FARPROC16,LONG,LONG,WORD,LONG);
45 extern WORD CALLBACK THUNK_CallTo16_word_lwww (FARPROC16,LONG,WORD,WORD,WORD);
46 extern WORD CALLBACK THUNK_CallTo16_word_wlww (FARPROC16,WORD,LONG,WORD,WORD);
47 extern WORD CALLBACK THUNK_CallTo16_word_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
48 extern LONG CALLBACK THUNK_CallTo16_long_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
49 extern WORD CALLBACK THUNK_CallTo16_word_wllwl(FARPROC16,WORD,LONG,LONG,WORD,LONG);
50 extern WORD CALLBACK THUNK_CallTo16_word_lwwww(FARPROC16,LONG,WORD,WORD,WORD,WORD);
51 extern LONG CALLBACK THUNK_CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
52 extern WORD CALLBACK THUNK_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
53 /* ### stop build ### */
57 #include "pshpack1.h"
59 typedef struct tagTHUNK
61 BYTE popl_eax; /* 0x58 popl %eax (return address)*/
62 BYTE pushl_func; /* 0x68 pushl $proc */
63 FARPROC16 proc WINE_PACKED;
64 BYTE pushl_eax; /* 0x50 pushl %eax */
65 BYTE jmp; /* 0xe9 jmp relay (relative jump)*/
66 RELAY relay WINE_PACKED;
67 struct tagTHUNK *next WINE_PACKED;
68 DWORD magic;
69 } THUNK;
71 #define CALLTO16_THUNK_MAGIC 0x54484e4b /* "THNK" */
73 #include "poppack.h"
75 #define DECL_THUNK(aname,aproc,arelay) \
76 THUNK aname; \
77 aname.popl_eax = 0x58; \
78 aname.pushl_func = 0x68; \
79 aname.proc = (FARPROC) (aproc); \
80 aname.pushl_eax = 0x50; \
81 aname.jmp = 0xe9; \
82 aname.relay = (RELAY)((char *)(arelay) - (char *)(&(aname).next)); \
83 aname.next = NULL; \
84 aname.magic = CALLTO16_THUNK_MAGIC;
86 static THUNK *firstThunk = NULL;
88 static BOOL THUNK_ThunkletInit( void );
90 /* Callbacks function table for the emulator */
91 static const CALLBACKS_TABLE CALLBACK_EmulatorTable =
93 (void *)CallTo16RegisterShort, /* CallRegisterShortProc */
94 (void *)CallTo16RegisterLong, /* CallRegisterLongProc */
95 (void *)THUNK_CallTo16_long_lwwll, /* CallDriverProc */
96 (void *)THUNK_CallTo16_word_wwlll, /* CallDriverCallback */
97 (void *)THUNK_CallTo16_word_wwlll, /* CallTimeFuncProc */
98 (void *)THUNK_CallTo16_word_w, /* CallWindowsExitProc */
99 (void *)THUNK_CallTo16_word_lwww, /* CallWordBreakProc */
100 (void *)THUNK_CallTo16_word_ww, /* CallBootAppProc */
101 (void *)THUNK_CallTo16_word_www, /* CallLoadAppSegProc */
102 (void *)THUNK_CallTo16_word_www, /* CallLocalNotifyFunc */
103 (void *)THUNK_CallTo16_word_www, /* CallResourceHandlerProc */
104 (void *)THUNK_CallTo16_long_ll, /* CallUTProc */
105 (void *)THUNK_CallTo16_long_l /* CallASPIPostProc */
108 const CALLBACKS_TABLE *Callbacks = &CALLBACK_EmulatorTable;
110 CALLOUT_TABLE Callout = { 0 };
113 /***********************************************************************
114 * THUNK_Init
116 BOOL THUNK_Init(void)
118 /* Initialize Thunklets */
119 return THUNK_ThunkletInit();
122 /***********************************************************************
123 * THUNK_Alloc
125 FARPROC THUNK_Alloc( FARPROC16 func, RELAY relay )
127 HANDLE16 hSeg;
128 NE_MODULE *pModule;
129 THUNK *thunk;
131 /* NULL maps to NULL */
132 if ( !func ) return NULL;
135 * If we got an 16-bit built-in API entry point, retrieve the Wine
136 * 32-bit handler for that API routine.
138 * NOTE: For efficiency reasons, we only check whether the selector
139 * of 'func' points to the code segment of a built-in module.
140 * It might be theoretically possible that the offset is such
141 * that 'func' does not point, in fact, to an API entry point.
142 * In this case, however, the pointer is corrupt anyway.
144 hSeg = GlobalHandle16( SELECTOROF( func ) );
145 pModule = NE_GetPtr( FarGetOwner16( hSeg ) );
147 if ( pModule && (pModule->flags & NE_FFLAGS_BUILTIN)
148 && NE_SEG_TABLE(pModule)[0].hSeg == hSeg )
150 FARPROC proc = (FARPROC)((ENTRYPOINT16 *)PTR_SEG_TO_LIN( func ))->target;
152 TRACE_(thunk)( "(%04x:%04x, %p) -> built-in API %p\n",
153 SELECTOROF( func ), OFFSETOF( func ), relay, proc );
154 return proc;
157 /* Otherwise, we need to alloc a thunk */
158 thunk = HeapAlloc( SystemHeap, 0, sizeof(*thunk) );
159 if (thunk)
161 thunk->popl_eax = 0x58;
162 thunk->pushl_func = 0x68;
163 thunk->proc = func;
164 thunk->pushl_eax = 0x50;
165 thunk->jmp = 0xe9;
166 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
167 thunk->magic = CALLTO16_THUNK_MAGIC;
168 thunk->next = firstThunk;
169 firstThunk = thunk;
172 TRACE_(thunk)( "(%04x:%04x, %p) -> allocated thunk %p\n",
173 SELECTOROF( func ), OFFSETOF( func ), relay, thunk );
174 return (FARPROC)thunk;
177 /***********************************************************************
178 * THUNK_Free
180 void THUNK_Free( FARPROC thunk )
182 THUNK *t = (THUNK*)thunk;
183 if ( !t || IsBadReadPtr( t, sizeof(*t) )
184 || t->magic != CALLTO16_THUNK_MAGIC )
185 return;
187 if (HEAP_IsInsideHeap( SystemHeap, 0, t ))
189 THUNK **prev = &firstThunk;
190 while (*prev && (*prev != t)) prev = &(*prev)->next;
191 if (*prev)
193 *prev = t->next;
194 HeapFree( SystemHeap, 0, t );
195 return;
198 ERR_(thunk)("invalid thunk addr %p\n", thunk );
199 return;
203 /***********************************************************************
204 * THUNK_EnumObjects16 (GDI.71)
206 INT16 WINAPI THUNK_EnumObjects16( HDC16 hdc, INT16 nObjType,
207 GOBJENUMPROC16 func, LPARAM lParam )
209 DECL_THUNK( thunk, func, THUNK_CallTo16_word_ll );
210 return EnumObjects16( hdc, nObjType, (GOBJENUMPROC16)&thunk, lParam );
214 /*************************************************************************
215 * THUNK_EnumFonts16 (GDI.70)
217 INT16 WINAPI THUNK_EnumFonts16( HDC16 hdc, LPCSTR lpFaceName,
218 FONTENUMPROC16 func, LPARAM lParam )
220 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
221 return EnumFonts16( hdc, lpFaceName, (FONTENUMPROC16)&thunk, lParam );
224 /******************************************************************
225 * THUNK_EnumMetaFile16 (GDI.175)
227 BOOL16 WINAPI THUNK_EnumMetaFile16( HDC16 hdc, HMETAFILE16 hmf,
228 MFENUMPROC16 func, LPARAM lParam )
230 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wllwl );
231 return EnumMetaFile16( hdc, hmf, (MFENUMPROC16)&thunk, lParam );
235 /*************************************************************************
236 * THUNK_EnumFontFamilies16 (GDI.330)
238 INT16 WINAPI THUNK_EnumFontFamilies16( HDC16 hdc, LPCSTR lpszFamily,
239 FONTENUMPROC16 func, LPARAM lParam )
241 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
242 return EnumFontFamilies16(hdc, lpszFamily, (FONTENUMPROC16)&thunk, lParam);
246 /*************************************************************************
247 * THUNK_EnumFontFamiliesEx16 (GDI.613)
249 INT16 WINAPI THUNK_EnumFontFamiliesEx16( HDC16 hdc, LPLOGFONT16 lpLF,
250 FONTENUMPROCEX16 func, LPARAM lParam,
251 DWORD reserved )
253 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
254 return EnumFontFamiliesEx16( hdc, lpLF, (FONTENUMPROCEX16)&thunk,
255 lParam, reserved );
259 /**********************************************************************
260 * THUNK_LineDDA16 (GDI.100)
262 void WINAPI THUNK_LineDDA16( INT16 nXStart, INT16 nYStart, INT16 nXEnd,
263 INT16 nYEnd, LINEDDAPROC16 func, LPARAM lParam )
265 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wwl );
266 LineDDA16( nXStart, nYStart, nXEnd, nYEnd, (LINEDDAPROC16)&thunk, lParam );
270 /*******************************************************************
271 * THUNK_EnumWindows16 (USER.54)
273 BOOL16 WINAPI THUNK_EnumWindows16( WNDENUMPROC16 func, LPARAM lParam )
275 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
276 return EnumWindows16( (WNDENUMPROC16)&thunk, lParam );
280 /**********************************************************************
281 * THUNK_EnumChildWindows16 (USER.55)
283 BOOL16 WINAPI THUNK_EnumChildWindows16( HWND16 parent, WNDENUMPROC16 func,
284 LPARAM lParam )
286 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
287 return EnumChildWindows16( parent, (WNDENUMPROC16)&thunk, lParam );
291 /**********************************************************************
292 * THUNK_EnumTaskWindows16 (USER.225)
294 BOOL16 WINAPI THUNK_EnumTaskWindows16( HTASK16 hTask, WNDENUMPROC16 func,
295 LPARAM lParam )
297 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
298 return EnumTaskWindows16( hTask, (WNDENUMPROC16)&thunk, lParam );
302 /***********************************************************************
303 * THUNK_EnumProps16 (USER.27)
305 INT16 WINAPI THUNK_EnumProps16( HWND16 hwnd, PROPENUMPROC16 func )
307 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wlw );
308 return EnumProps16( hwnd, (PROPENUMPROC16)&thunk );
312 /***********************************************************************
313 * THUNK_GrayString16 (USER.185)
315 BOOL16 WINAPI THUNK_GrayString16( HDC16 hdc, HBRUSH16 hbr,
316 GRAYSTRINGPROC16 func, LPARAM lParam,
317 INT16 cch, INT16 x, INT16 y,
318 INT16 cx, INT16 cy )
320 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wlw );
321 if (!func)
322 return GrayString16( hdc, hbr, NULL, lParam, cch, x, y, cx, cy );
323 else
324 return GrayString16( hdc, hbr, (GRAYSTRINGPROC16)&thunk, lParam, cch,
325 x, y, cx, cy );
329 /***********************************************************************
330 * THUNK_GetCalloutThunk
332 * Retrieve API entry point with given name from given module.
333 * If module is builtin, return the 32-bit entry point, otherwise
334 * create a 32->16 thunk to the 16-bit entry point, using the
335 * given relay code.
338 static FARPROC THUNK_GetCalloutThunk( NE_MODULE *pModule, LPSTR name, RELAY relay )
340 FARPROC16 proc = WIN32_GetProcAddress16( pModule->self, name );
341 if ( !proc ) return 0;
343 if ( pModule->flags & NE_FFLAGS_BUILTIN )
344 return (FARPROC)((ENTRYPOINT16 *)PTR_SEG_TO_LIN( proc ))->target;
345 else
346 return (FARPROC)THUNK_Alloc( proc, relay );
349 /***********************************************************************
350 * THUNK_InitCallout
352 void THUNK_InitCallout(void)
354 HMODULE hModule;
355 NE_MODULE *pModule;
357 hModule = GetModuleHandleA( "USER32" );
358 if ( hModule )
360 #define GETADDR( var, name ) \
361 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
363 GETADDR( PeekMessageA, "PeekMessageA" );
364 GETADDR( PeekMessageW, "PeekMessageW" );
365 GETADDR( GetMessageA, "GetMessageA" );
366 GETADDR( GetMessageW, "GetMessageW" );
367 GETADDR( SendMessageA, "SendMessageA" );
368 GETADDR( SendMessageW, "SendMessageW" );
369 GETADDR( PostMessageA, "PostMessageA" );
370 GETADDR( PostMessageW, "PostMessageW" );
371 GETADDR( PostThreadMessageA, "PostThreadMessageA" );
372 GETADDR( PostThreadMessageW, "PostThreadMessageW" );
373 GETADDR( TranslateMessage, "TranslateMessage" );
374 GETADDR( DispatchMessageW, "DispatchMessageW" );
375 GETADDR( DispatchMessageA, "DispatchMessageA" );
376 GETADDR( RedrawWindow, "RedrawWindow" );
377 GETADDR( UserSignalProc, "UserSignalProc" );
379 #undef GETADDR
382 pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
383 if ( pModule )
385 #define GETADDR( var, name, thk ) \
386 *(FARPROC *)&Callout.##var = THUNK_GetCalloutThunk( pModule, name, \
387 (RELAY)THUNK_CallTo16_##thk )
389 GETADDR( PeekMessage16, "PeekMessage", word_lwwww );
390 GETADDR( GetMessage16, "GetMessage", word_lwww );
391 GETADDR( SendMessage16, "SendMessage", long_wwwl );
392 GETADDR( PostMessage16, "PostMessage", word_wwwl );
393 GETADDR( PostAppMessage16, "PostAppMessage", word_wwwl );
394 GETADDR( TranslateMessage16, "TranslateMessage", word_l );
395 GETADDR( DispatchMessage16, "DispatchMessage", long_l );
396 GETADDR( RedrawWindow16, "RedrawWindow", word_wlww );
397 GETADDR( FinalUserInit16, "FinalUserInit", word_ );
398 GETADDR( InitApp16, "InitApp", word_w );
399 GETADDR( InitThreadInput16, "InitThreadInput", word_ww );
400 GETADDR( UserYield16, "UserYield", word_ );
401 GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
403 #undef GETADDR
407 /***********************************************************************
408 * 16->32 Flat Thunk routines:
411 /***********************************************************************
412 * ThunkConnect16 (KERNEL.651)
413 * Connects a 32bit and a 16bit thunkbuffer.
415 UINT WINAPI ThunkConnect16(
416 LPSTR module16, /* [in] name of win16 dll */
417 LPSTR module32, /* [in] name of win32 dll */
418 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
419 DWORD dwReason, /* [in] initialisation argument */
420 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
421 LPSTR thunkfun32, /* [in] win32 thunkfunction */
422 WORD cs /* [in] CS of win16 dll */
424 BOOL directionSL;
426 if (!strncmp(TD->magic, "SL01", 4))
428 directionSL = TRUE;
430 TRACE_(thunk)("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
431 module16, (DWORD)TD, module32, thunkfun32, dwReason);
433 else if (!strncmp(TD->magic, "LS01", 4))
435 directionSL = FALSE;
437 TRACE_(thunk)("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
438 module16, (DWORD)TD, module32, thunkfun32, dwReason);
440 else
442 ERR_(thunk)("Invalid magic %c%c%c%c\n",
443 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
444 return 0;
447 switch (dwReason)
449 case DLL_PROCESS_ATTACH:
450 if (directionSL)
452 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
453 struct ThunkDataSL *SL = SL16->fpData;
455 if (SL == NULL)
457 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
459 SL->common = SL16->common;
460 SL->flags1 = SL16->flags1;
461 SL->flags2 = SL16->flags2;
463 SL->apiDB = PTR_SEG_TO_LIN(SL16->apiDatabase);
464 SL->targetDB = NULL;
466 lstrcpynA(SL->pszDll16, module16, 255);
467 lstrcpynA(SL->pszDll32, module32, 255);
469 /* We should create a SEGPTR to the ThunkDataSL,
470 but since the contents are not in the original format,
471 any access to this by 16-bit code would crash anyway. */
472 SL16->spData = 0;
473 SL16->fpData = SL;
477 if (SL->flags2 & 0x80000000)
479 TRACE_(thunk)("Preloading 32-bit library\n");
480 LoadLibraryA(module32);
483 else
485 /* nothing to do */
487 break;
489 case DLL_PROCESS_DETACH:
490 /* FIXME: cleanup */
491 break;
494 return 1;
498 /***********************************************************************
499 * C16ThkSL (KERNEL.630)
502 void WINAPI C16ThkSL(CONTEXT86 *context)
504 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
505 WORD cs, ds;
506 GET_CS(cs);
507 GET_DS(ds);
509 /* We produce the following code:
511 * mov ax, __FLATDS
512 * mov es, ax
513 * movzx ecx, cx
514 * mov edx, es:[ecx + $EDX]
515 * push bp
516 * push edx
517 * push dx
518 * push edx
519 * call __FLATCS:CallFrom16Thunk
522 *x++ = 0xB8; *((WORD *)x)++ = ds;
523 *x++ = 0x8E; *x++ = 0xC0;
524 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
525 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
526 *x++ = 0x91; *((DWORD *)x)++ = EDX_reg(context);
528 *x++ = 0x55;
529 *x++ = 0x66; *x++ = 0x52;
530 *x++ = 0x52;
531 *x++ = 0x66; *x++ = 0x52;
532 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
533 *((WORD *)x)++ = cs;
535 /* Jump to the stub code just created */
536 EIP_reg(context) = LOWORD(EAX_reg(context));
537 CS_reg(context) = HIWORD(EAX_reg(context));
539 /* Since C16ThkSL got called by a jmp, we need to leave the
540 original return address on the stack */
541 ESP_reg(context) -= 4;
544 /***********************************************************************
545 * C16ThkSL01 (KERNEL.631)
548 void WINAPI C16ThkSL01(CONTEXT86 *context)
550 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
552 if (stub)
554 struct ThunkDataSL16 *SL16 = PTR_SEG_TO_LIN(EDX_reg(context));
555 struct ThunkDataSL *td = SL16->fpData;
557 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
558 WORD cs;
559 GET_CS(cs);
561 if (!td)
563 ERR_(thunk)("ThunkConnect16 was not called!\n");
564 return;
567 TRACE_(thunk)("Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
570 /* We produce the following code:
572 * xor eax, eax
573 * mov edx, $td
574 * call C16ThkSL01
575 * push bp
576 * push edx
577 * push dx
578 * push edx
579 * call __FLATCS:CallFrom16Thunk
582 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
583 *x++ = 0x66; *x++ = 0xBA; *((DWORD *)x)++ = (DWORD)td;
584 *x++ = 0x9A; *((DWORD *)x)++ = procAddress;
586 *x++ = 0x55;
587 *x++ = 0x66; *x++ = 0x52;
588 *x++ = 0x52;
589 *x++ = 0x66; *x++ = 0x52;
590 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
591 *((WORD *)x)++ = cs;
593 /* Jump to the stub code just created */
594 EIP_reg(context) = LOWORD(EAX_reg(context));
595 CS_reg(context) = HIWORD(EAX_reg(context));
597 /* Since C16ThkSL01 got called by a jmp, we need to leave the
598 orginal return address on the stack */
599 ESP_reg(context) -= 4;
601 else
603 struct ThunkDataSL *td = (struct ThunkDataSL *)EDX_reg(context);
604 DWORD targetNr = CX_reg(context) / 4;
605 struct SLTargetDB *tdb;
607 TRACE_(thunk)("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
608 (DWORD)PROCESS_Current(), targetNr, (DWORD)td);
610 for (tdb = td->targetDB; tdb; tdb = tdb->next)
611 if (tdb->process == PROCESS_Current())
612 break;
614 if (!tdb)
616 TRACE_(thunk)("Loading 32-bit library %s\n", td->pszDll32);
617 LoadLibraryA(td->pszDll32);
619 for (tdb = td->targetDB; tdb; tdb = tdb->next)
620 if (tdb->process == PROCESS_Current())
621 break;
624 if (tdb)
626 EDX_reg(context) = tdb->targetTable[targetNr];
628 TRACE_(thunk)("Call target is %08lx\n", EDX_reg(context));
630 else
632 WORD *stack = PTR_SEG_OFF_TO_LIN(SS_reg(context), LOWORD(ESP_reg(context)));
633 DX_reg(context) = HIWORD(td->apiDB[targetNr].errorReturnValue);
634 AX_reg(context) = LOWORD(td->apiDB[targetNr].errorReturnValue);
635 EIP_reg(context) = stack[2];
636 CS_reg(context) = stack[3];
637 ESP_reg(context) += td->apiDB[targetNr].nrArgBytes + 4;
639 ERR_(thunk)("Process %08lx did not ThunkConnect32 %s to %s\n",
640 (DWORD)PROCESS_Current(), td->pszDll32, td->pszDll16);
647 /***********************************************************************
648 * 16<->32 Thunklet/Callback API:
651 #include "pshpack1.h"
652 typedef struct _THUNKLET
654 BYTE prefix_target;
655 BYTE pushl_target;
656 DWORD target;
658 BYTE prefix_relay;
659 BYTE pushl_relay;
660 DWORD relay;
662 BYTE jmp_glue;
663 DWORD glue;
665 BYTE type;
666 HINSTANCE16 owner;
667 struct _THUNKLET *next;
668 } THUNKLET;
669 #include "poppack.h"
671 #define THUNKLET_TYPE_LS 1
672 #define THUNKLET_TYPE_SL 2
674 static HANDLE ThunkletHeap = 0;
675 static THUNKLET *ThunkletAnchor = NULL;
677 static FARPROC ThunkletSysthunkGlueLS = 0;
678 static SEGPTR ThunkletSysthunkGlueSL = 0;
680 static FARPROC ThunkletCallbackGlueLS = 0;
681 static SEGPTR ThunkletCallbackGlueSL = 0;
683 /***********************************************************************
684 * THUNK_ThunkletInit
686 static BOOL THUNK_ThunkletInit( void )
688 LPBYTE thunk;
690 ThunkletHeap = HeapCreate(HEAP_WINE_SEGPTR | HEAP_WINE_CODE16SEG, 0, 0);
691 if (!ThunkletHeap) return FALSE;
693 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
694 if (!thunk) return FALSE;
696 ThunkletSysthunkGlueLS = (FARPROC)thunk;
697 *thunk++ = 0x58; /* popl eax */
698 *thunk++ = 0xC3; /* ret */
700 ThunkletSysthunkGlueSL = HEAP_GetSegptr( ThunkletHeap, 0, thunk );
701 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
702 *thunk++ = 0xCB; /* lret */
704 return TRUE;
707 /***********************************************************************
708 * SetThunkletCallbackGlue (KERNEL.560)
710 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
712 ThunkletCallbackGlueLS = glueLS;
713 ThunkletCallbackGlueSL = glueSL;
717 /***********************************************************************
718 * THUNK_FindThunklet
720 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
721 DWORD glue, BYTE type )
723 THUNKLET *thunk;
725 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
726 if ( thunk->type == type
727 && thunk->target == target
728 && thunk->relay == relay
729 && ( type == THUNKLET_TYPE_LS ?
730 ( thunk->glue == glue - (DWORD)&thunk->type )
731 : ( thunk->glue == glue ) ) )
732 return thunk;
734 return NULL;
737 /***********************************************************************
738 * THUNK_AllocLSThunklet
740 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
741 FARPROC glue, HTASK16 owner )
743 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
744 THUNKLET_TYPE_LS );
745 if (!thunk)
747 TDB *pTask = (TDB*)GlobalLock16( owner );
749 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
750 return 0;
752 thunk->prefix_target = thunk->prefix_relay = 0x90;
753 thunk->pushl_target = thunk->pushl_relay = 0x68;
754 thunk->jmp_glue = 0xE9;
756 thunk->target = (DWORD)target;
757 thunk->relay = (DWORD)relay;
758 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
760 thunk->type = THUNKLET_TYPE_LS;
761 thunk->owner = pTask? pTask->hInstance : 0;
763 thunk->next = ThunkletAnchor;
764 ThunkletAnchor = thunk;
767 return (FARPROC)thunk;
770 /***********************************************************************
771 * THUNK_AllocSLThunklet
773 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
774 SEGPTR glue, HTASK16 owner )
776 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
777 THUNKLET_TYPE_SL );
778 if (!thunk)
780 TDB *pTask = (TDB*)GlobalLock16( owner );
782 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
783 return 0;
785 thunk->prefix_target = thunk->prefix_relay = 0x66;
786 thunk->pushl_target = thunk->pushl_relay = 0x68;
787 thunk->jmp_glue = 0xEA;
789 thunk->target = (DWORD)target;
790 thunk->relay = (DWORD)relay;
791 thunk->glue = (DWORD)glue;
793 thunk->type = THUNKLET_TYPE_SL;
794 thunk->owner = pTask? pTask->hInstance : 0;
796 thunk->next = ThunkletAnchor;
797 ThunkletAnchor = thunk;
800 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
803 /**********************************************************************
804 * IsLSThunklet
806 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
808 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
809 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
810 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
813 /**********************************************************************
814 * IsSLThunklet (KERNEL.612)
816 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
818 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
819 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
820 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
825 /***********************************************************************
826 * AllocLSThunkletSysthunk (KERNEL.607)
828 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
829 FARPROC relay, DWORD dummy )
831 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
832 ThunkletSysthunkGlueLS, GetCurrentTask() );
835 /***********************************************************************
836 * AllocSLThunkletSysthunk (KERNEL.608)
838 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
839 SEGPTR relay, DWORD dummy )
841 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
842 ThunkletSysthunkGlueSL, GetCurrentTask() );
846 /***********************************************************************
847 * AllocLSThunkletCallbackEx (KERNEL.567)
849 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
850 DWORD relay, HTASK16 task )
852 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
853 if ( !thunk ) return NULL;
855 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
856 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
857 return (FARPROC)thunk->target;
859 return THUNK_AllocLSThunklet( target, relay,
860 ThunkletCallbackGlueLS, task );
863 /***********************************************************************
864 * AllocSLThunkletCallbackEx (KERNEL.568)
866 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
867 DWORD relay, HTASK16 task )
869 THUNKLET *thunk = (THUNKLET *)target;
870 if ( !thunk ) return 0;
872 if ( IsLSThunklet( thunk ) && thunk->relay == relay
873 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
874 return (SEGPTR)thunk->target;
876 return THUNK_AllocSLThunklet( target, relay,
877 ThunkletCallbackGlueSL, task );
880 /***********************************************************************
881 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
883 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
885 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
888 /***********************************************************************
889 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
891 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
893 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
896 /***********************************************************************
897 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
899 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
901 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
902 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
903 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
904 return (FARPROC)thunk->target;
906 thunk = THUNK_FindThunklet( (DWORD)target, relay,
907 (DWORD)ThunkletCallbackGlueLS,
908 THUNKLET_TYPE_LS );
909 return (FARPROC)thunk;
912 /***********************************************************************
913 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
915 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
917 THUNKLET *thunk = (THUNKLET *)target;
918 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
919 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
920 return (SEGPTR)thunk->target;
922 thunk = THUNK_FindThunklet( (DWORD)target, relay,
923 (DWORD)ThunkletCallbackGlueSL,
924 THUNKLET_TYPE_SL );
925 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
929 /***********************************************************************
930 * FreeThunklet16 (KERNEL.611)
932 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
934 return FALSE;
937 /***********************************************************************
938 * Callback Client API
941 #define N_CBC_FIXED 20
942 #define N_CBC_VARIABLE 10
943 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
945 static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
946 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
948 /***********************************************************************
949 * RegisterCBClient (KERNEL.619)
951 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
952 SEGPTR relay16, FARPROC *relay32 )
954 /* Search for free Callback ID */
955 if ( wCBCId == -1 )
956 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
957 if ( !CBClientRelay16[ wCBCId ] )
958 break;
960 /* Register Callback ID */
961 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
963 CBClientRelay16[ wCBCId ] = relay16;
964 CBClientRelay32[ wCBCId ] = relay32;
966 else
967 wCBCId = 0;
969 return wCBCId;
972 /***********************************************************************
973 * UnRegisterCBClient (KERNEL.622)
975 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
976 SEGPTR relay16, FARPROC *relay32 )
978 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
979 && CBClientRelay16[ wCBCId ] == relay16
980 && CBClientRelay32[ wCBCId ] == relay32 )
982 CBClientRelay16[ wCBCId ] = 0;
983 CBClientRelay32[ wCBCId ] = 0;
985 else
986 wCBCId = 0;
988 return wCBCId;
992 /***********************************************************************
993 * InitCBClient (KERNEL.623)
995 void WINAPI InitCBClient16( FARPROC glueLS )
997 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
998 SEGPTR glueSL = (SEGPTR)WIN32_GetProcAddress16( kernel, (LPCSTR)604 );
1000 SetThunkletCallbackGlue16( glueLS, glueSL );
1003 /***********************************************************************
1004 * CBClientGlueSL (KERNEL.604)
1006 void WINAPI CBClientGlueSL( CONTEXT86 *context )
1008 /* Create stack frame */
1009 SEGPTR stackSeg = stack16_push( 12 );
1010 LPWORD stackLin = PTR_SEG_TO_LIN( stackSeg );
1011 SEGPTR glue, *glueTab;
1013 stackLin[3] = BP_reg( context );
1014 stackLin[2] = SI_reg( context );
1015 stackLin[1] = DI_reg( context );
1016 stackLin[0] = DS_reg( context );
1018 EBP_reg( context ) = OFFSETOF( stackSeg ) + 6;
1019 ESP_reg( context ) = OFFSETOF( stackSeg ) - 4;
1020 GS_reg( context ) = 0;
1022 /* Jump to 16-bit relay code */
1023 glueTab = PTR_SEG_TO_LIN( CBClientRelay16[ stackLin[5] ] );
1024 glue = glueTab[ stackLin[4] ];
1025 CS_reg ( context ) = SELECTOROF( glue );
1026 EIP_reg( context ) = OFFSETOF ( glue );
1029 /***********************************************************************
1030 * CBClientThunkSL (KERNEL.620)
1032 extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi );
1033 void WINAPI CBClientThunkSL( CONTEXT86 *context )
1035 /* Call 32-bit relay code */
1037 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1038 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1040 EAX_reg(context) = CALL32_CBClient( proc, args, &ESI_reg( context ) );
1043 /***********************************************************************
1044 * CBClientThunkSLEx (KERNEL.621)
1046 extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs );
1047 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
1049 /* Call 32-bit relay code */
1051 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1052 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1053 INT nArgs;
1054 LPWORD stackLin;
1056 EAX_reg(context) = CALL32_CBClientEx( proc, args, &ESI_reg( context ), &nArgs );
1058 /* Restore registers saved by CBClientGlueSL */
1059 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
1060 BP_reg( context ) = stackLin[3];
1061 SI_reg( context ) = stackLin[2];
1062 DI_reg( context ) = stackLin[1];
1063 DS_reg( context ) = stackLin[0];
1064 ESP_reg( context ) += 16+nArgs;
1066 /* Return to caller of CBClient thunklet */
1067 CS_reg ( context ) = stackLin[9];
1068 EIP_reg( context ) = stackLin[8];