Take advantage of the new registers saved in the STACK16FRAME to turn
[wine/multimedia.git] / if1632 / thunk.c
blob9fb0b13dc6e4ba35bf00640ab310165cffa67275
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 "user.h"
14 #include "heap.h"
15 #include "neexe.h"
16 #include "process.h"
17 #include "stackframe.h"
18 #include "win.h"
19 #include "flatthunk.h"
20 #include "mouse.h"
21 #include "keyboard.h"
22 #include "debugtools.h"
24 DECLARE_DEBUG_CHANNEL(relay)
25 DECLARE_DEBUG_CHANNEL(system)
26 DECLARE_DEBUG_CHANNEL(thunk)
29 extern LONG CallTo16RegisterShort(const CONTEXT86 *context, INT offset);
30 extern LONG CallTo16RegisterLong(const CONTEXT86 *context, INT offset);
32 /* List of the 16-bit callback functions. This list is used */
33 /* by the build program to generate the file if1632/callto16.S */
35 /* ### start build ### */
36 extern WORD THUNK_CallTo16_word_ (FARPROC16);
37 extern LONG THUNK_CallTo16_long_ (FARPROC16);
38 extern WORD THUNK_CallTo16_word_w (FARPROC16,WORD);
39 extern WORD THUNK_CallTo16_word_l (FARPROC16,LONG);
40 extern LONG THUNK_CallTo16_long_l (FARPROC16,LONG);
41 extern WORD THUNK_CallTo16_word_ww (FARPROC16,WORD,WORD);
42 extern WORD THUNK_CallTo16_word_wl (FARPROC16,WORD,LONG);
43 extern WORD THUNK_CallTo16_word_ll (FARPROC16,LONG,LONG);
44 extern LONG THUNK_CallTo16_long_ll (FARPROC16,LONG,LONG);
45 extern WORD THUNK_CallTo16_word_www (FARPROC16,WORD,WORD,WORD);
46 extern WORD THUNK_CallTo16_word_wwl (FARPROC16,WORD,WORD,LONG);
47 extern WORD THUNK_CallTo16_word_wlw (FARPROC16,WORD,LONG,WORD);
48 extern LONG THUNK_CallTo16_long_wwl (FARPROC16,WORD,WORD,LONG);
49 extern LONG THUNK_CallTo16_long_lll (FARPROC16,LONG,LONG,LONG);
50 extern WORD THUNK_CallTo16_word_llwl (FARPROC16,LONG,LONG,WORD,LONG);
51 extern WORD THUNK_CallTo16_word_lwll (FARPROC16,LONG,WORD,LONG,LONG);
52 extern WORD THUNK_CallTo16_word_lwww (FARPROC16,LONG,WORD,WORD,WORD);
53 extern WORD THUNK_CallTo16_word_wlww (FARPROC16,WORD,LONG,WORD,WORD);
54 extern WORD THUNK_CallTo16_word_wwll (FARPROC16,WORD,WORD,LONG,LONG);
55 extern WORD THUNK_CallTo16_word_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
56 extern LONG THUNK_CallTo16_long_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
57 extern WORD THUNK_CallTo16_word_llll (FARPROC16,LONG,LONG,LONG,LONG);
58 extern LONG THUNK_CallTo16_long_llll (FARPROC16,LONG,LONG,LONG,LONG);
59 extern WORD THUNK_CallTo16_word_wllwl(FARPROC16,WORD,LONG,LONG,WORD,LONG);
60 extern WORD THUNK_CallTo16_word_lwwww(FARPROC16,LONG,WORD,WORD,WORD,WORD);
61 extern LONG THUNK_CallTo16_long_lwwll(FARPROC16,LONG,WORD,WORD,LONG,LONG);
62 extern WORD THUNK_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
63 extern WORD THUNK_CallTo16_word_wwwww(FARPROC16,WORD,WORD,WORD,WORD,WORD);
64 extern WORD THUNK_CallTo16_word_lwlll(FARPROC16,LONG,WORD,LONG,LONG,LONG);
65 extern LONG THUNK_CallTo16_long_lwlll(FARPROC16,LONG,WORD,LONG,LONG,LONG);
66 extern LONG THUNK_CallTo16_long_lllll(FARPROC16,LONG,LONG,LONG,LONG,LONG);
67 extern LONG THUNK_CallTo16_long_llllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,LONG);
68 extern LONG THUNK_CallTo16_long_lllllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,LONG,LONG);
69 extern WORD THUNK_CallTo16_word_llwwlll(FARPROC16,LONG,LONG,WORD,WORD,LONG,LONG,LONG);
70 extern LONG THUNK_CallTo16_word_lwwlllll(FARPROC16,LONG,WORD,WORD,LONG,LONG,
71 LONG,LONG,LONG);
72 extern LONG THUNK_CallTo16_long_llllllll(FARPROC16,LONG,LONG,LONG,LONG,LONG,
73 LONG,LONG,LONG);
74 extern LONG THUNK_CallTo16_long_lllllllll(FARPROC16,LONG,LONG,LONG,LONG,
75 LONG,LONG,LONG,LONG,LONG);
76 extern LONG THUNK_CallTo16_long_llllllllll(FARPROC16,LONG,LONG,LONG,LONG,
77 LONG,LONG,LONG,LONG,LONG,LONG);
78 extern LONG THUNK_CallTo16_long_lllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
79 LONG,LONG,LONG,LONG,LONG,LONG,
80 LONG);
81 extern LONG THUNK_CallTo16_long_llllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
82 LONG,LONG,LONG,LONG,LONG,LONG,
83 LONG,LONG);
84 extern LONG THUNK_CallTo16_long_lwwllwlllllw(FARPROC16,LONG,WORD,WORD,LONG,
85 LONG,WORD,LONG,LONG,LONG,LONG,
86 LONG,WORD);
87 extern LONG THUNK_CallTo16_long_lllllllllllll(FARPROC16,LONG,LONG,LONG,LONG,
88 LONG,LONG,LONG,LONG,LONG,LONG,
89 LONG,LONG,LONG);
90 extern LONG THUNK_CallTo16_long_llllllllllllll(FARPROC16,LONG,LONG,LONG,
91 LONG,LONG,LONG,LONG,LONG,
92 LONG,LONG,LONG,LONG,LONG,
93 LONG);
94 extern LONG THUNK_CallTo16_word_lwwwwlwwwwllll(FARPROC16,LONG,WORD,WORD,
95 WORD,WORD,LONG,WORD,WORD,
96 WORD,WORD,LONG,LONG,LONG,
97 LONG);
98 extern LONG THUNK_CallTo16_long_lllllllllllllll(FARPROC16,LONG,LONG,LONG,
99 LONG,LONG,LONG,LONG,LONG,
100 LONG,LONG,LONG,LONG,LONG,
101 LONG,LONG);
102 extern LONG THUNK_CallTo16_long_llllllllllllllll(FARPROC16,LONG,LONG,LONG,
103 LONG,LONG,LONG,LONG,LONG,
104 LONG,LONG,LONG,LONG,LONG,
105 LONG,LONG,LONG);
106 extern void THUNK_CallFrom16_p_long_wwwll();
107 /* ### stop build ### */
110 typedef void (*RELAY)();
112 #include "pshpack1.h"
114 typedef struct tagTHUNK
116 BYTE popl_eax; /* 0x58 popl %eax (return address)*/
117 BYTE pushl_func; /* 0x68 pushl $proc */
118 FARPROC proc WINE_PACKED;
119 BYTE pushl_eax; /* 0x50 pushl %eax */
120 BYTE jmp; /* 0xe9 jmp relay (relative jump)*/
121 RELAY relay WINE_PACKED;
122 struct tagTHUNK *next WINE_PACKED;
123 } THUNK;
125 #include "poppack.h"
127 #define DECL_THUNK(aname,aproc,arelay) \
128 THUNK aname; \
129 aname.popl_eax = 0x58; \
130 aname.pushl_func = 0x68; \
131 aname.proc = (FARPROC) (aproc); \
132 aname.pushl_eax = 0x50; \
133 aname.jmp = 0xe9; \
134 aname.relay = (RELAY)((char *)(arelay) - (char *)(&(aname).next)); \
135 aname.next = NULL;
137 static THUNK *firstThunk = NULL;
139 static LRESULT WINAPI THUNK_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
140 UINT16 msg, WPARAM16 wParam,
141 LPARAM lParam );
142 static BOOL WINAPI THUNK_WOWCallback16Ex( FARPROC16,DWORD,DWORD,
143 LPVOID,LPDWORD );
144 static BOOL THUNK_ThunkletInit( void );
146 /* Callbacks function table for the emulator */
147 static const CALLBACKS_TABLE CALLBACK_EmulatorTable =
149 (void *)CallTo16RegisterShort, /* CallRegisterShortProc */
150 (void *)CallTo16RegisterLong, /* CallRegisterLongProc */
151 (void*)THUNK_CallFrom16_p_long_wwwll, /* CallFrom16WndProc */
152 THUNK_CallWndProc16, /* CallWndProc */
153 (void *)THUNK_CallTo16_long_lwwll, /* CallDriverProc */
154 (void *)THUNK_CallTo16_word_wwlll, /* CallDriverCallback */
155 (void *)THUNK_CallTo16_word_wwlll, /* CallTimeFuncProc */
156 (void *)THUNK_CallTo16_word_w, /* CallWindowsExitProc */
157 (void *)THUNK_CallTo16_word_lwww, /* CallWordBreakProc */
158 (void *)THUNK_CallTo16_word_ww, /* CallBootAppProc */
159 (void *)THUNK_CallTo16_word_www, /* CallLoadAppSegProc */
160 (void *)THUNK_CallTo16_word_www, /* CallLocalNotifyFunc */
161 (void *)THUNK_CallTo16_word_www, /* CallResourceHandlerProc */
162 (void *)THUNK_CallTo16_long_l, /* CallWOWCallbackProc */
163 THUNK_WOWCallback16Ex, /* CallWOWCallback16Ex */
164 (void *)THUNK_CallTo16_long_ll, /* CallUTProc */
165 (void *)THUNK_CallTo16_long_l, /* CallASPIPostProc */
166 (void *)THUNK_CallTo16_word_lwll, /* CallDrvControlProc */
167 (void *)THUNK_CallTo16_word_lwlll, /* CallDrvEnableProc */
168 (void *)THUNK_CallTo16_word_llll, /* CallDrvEnumDFontsProc */
169 (void *)THUNK_CallTo16_word_lwll, /* CallDrvEnumObjProc */
170 (void *)THUNK_CallTo16_word_lwwlllll, /* CallDrvOutputProc */
171 (void *)THUNK_CallTo16_long_lwlll, /* CallDrvRealizeProc */
172 (void *)THUNK_CallTo16_word_lwwwwlwwwwllll, /* CallDrvStretchBltProc */
173 (void *)THUNK_CallTo16_long_lwwllwlllllw, /* CallDrvExtTextOutProc */
174 (void *)THUNK_CallTo16_word_llwwlll, /* CallDrvGetCharWidth */
175 (void *)THUNK_CallTo16_word_ww /* CallDrvAbortProc */
179 /***********************************************************************
180 * THUNK_Init
182 BOOL THUNK_Init(void)
184 /* Set the window proc calling functions */
185 Callbacks = &CALLBACK_EmulatorTable;
186 /* Initialize Thunklets */
187 return THUNK_ThunkletInit();
190 /***********************************************************************
191 * THUNK_Alloc
193 static THUNK *THUNK_Alloc( FARPROC func, RELAY relay )
195 THUNK *thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
196 if (thunk)
198 thunk->popl_eax = 0x58;
199 thunk->pushl_func = 0x68;
200 thunk->proc = func;
201 thunk->pushl_eax = 0x50;
202 thunk->jmp = 0xe9;
203 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
204 thunk->next = firstThunk;
205 firstThunk = thunk;
207 return thunk;
211 /***********************************************************************
212 * THUNK_Find
214 static THUNK *THUNK_Find( FARPROC func )
216 THUNK *thunk = firstThunk;
217 while (thunk && (thunk->proc != func)) thunk = thunk->next;
218 return thunk;
222 /***********************************************************************
223 * THUNK_Free
225 static void THUNK_Free( THUNK *thunk )
227 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, thunk ))
229 THUNK **prev = &firstThunk;
230 while (*prev && (*prev != thunk)) prev = &(*prev)->next;
231 if (*prev)
233 *prev = thunk->next;
234 HeapFree( GetProcessHeap(), 0, thunk );
235 return;
238 ERR_(thunk)("invalid thunk addr %p\n", thunk );
242 /***********************************************************************
243 * THUNK_CallWndProc16
245 * Call a 16-bit window procedure
247 static LRESULT WINAPI THUNK_CallWndProc16( WNDPROC16 proc, HWND16 hwnd,
248 UINT16 msg, WPARAM16 wParam,
249 LPARAM lParam )
251 CONTEXT86 context;
252 LRESULT ret;
253 WORD *args;
254 WND *wndPtr = WIN_FindWndPtr( hwnd );
255 DWORD offset = 0;
256 TEB *teb = NtCurrentTeb();
257 int iWndsLocks;
259 /* Window procedures want ax = hInstance, ds = es = ss */
261 memset(&context, '\0', sizeof(context));
262 DS_reg(&context) = SELECTOROF(teb->cur_stack);
263 ES_reg(&context) = DS_reg(&context);
264 EAX_reg(&context) = wndPtr ? wndPtr->hInstance : DS_reg(&context);
265 CS_reg(&context) = SELECTOROF(proc);
266 EIP_reg(&context) = OFFSETOF(proc);
267 EBP_reg(&context) = OFFSETOF(teb->cur_stack)
268 + (WORD)&((STACK16FRAME*)0)->bp;
270 WIN_ReleaseWndPtr(wndPtr);
272 if (lParam)
274 /* Some programs (eg. the "Undocumented Windows" examples, JWP) only
275 work if structures passed in lParam are placed in the stack/data
276 segment. Programmers easily make the mistake of converting lParam
277 to a near rather than a far pointer, since Windows apparently
278 allows this. We copy the structures to the 16 bit stack; this is
279 ugly but makes these programs work. */
280 switch (msg)
282 case WM_CREATE:
283 case WM_NCCREATE:
284 offset = sizeof(CREATESTRUCT16); break;
285 case WM_DRAWITEM:
286 offset = sizeof(DRAWITEMSTRUCT16); break;
287 case WM_COMPAREITEM:
288 offset = sizeof(COMPAREITEMSTRUCT16); break;
290 if (offset)
292 void *s = PTR_SEG_TO_LIN(lParam);
293 lParam = STACK16_PUSH( teb, offset );
294 memcpy( PTR_SEG_TO_LIN(lParam), s, offset );
298 iWndsLocks = WIN_SuspendWndsLock();
300 args = (WORD *)THREAD_STACK16(teb) - 5;
301 args[0] = LOWORD(lParam);
302 args[1] = HIWORD(lParam);
303 args[2] = wParam;
304 args[3] = msg;
305 args[4] = hwnd;
307 ret = CallTo16RegisterShort( &context, 5 * sizeof(WORD) );
308 if (offset) STACK16_POP( teb, offset );
310 WIN_RestoreWndsLock(iWndsLocks);
312 return ret;
316 /***********************************************************************
317 * THUNK_EnumObjects16 (GDI.71)
319 INT16 WINAPI THUNK_EnumObjects16( HDC16 hdc, INT16 nObjType,
320 GOBJENUMPROC16 func, LPARAM lParam )
322 DECL_THUNK( thunk, func, THUNK_CallTo16_word_ll );
323 return EnumObjects16( hdc, nObjType, (GOBJENUMPROC16)&thunk, lParam );
327 /*************************************************************************
328 * THUNK_EnumFonts16 (GDI.70)
330 INT16 WINAPI THUNK_EnumFonts16( HDC16 hdc, LPCSTR lpFaceName,
331 FONTENUMPROC16 func, LPARAM lParam )
333 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
334 return EnumFonts16( hdc, lpFaceName, (FONTENUMPROC16)&thunk, lParam );
337 /******************************************************************
338 * THUNK_EnumMetaFile16 (GDI.175)
340 BOOL16 WINAPI THUNK_EnumMetaFile16( HDC16 hdc, HMETAFILE16 hmf,
341 MFENUMPROC16 func, LPARAM lParam )
343 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wllwl );
344 return EnumMetaFile16( hdc, hmf, (MFENUMPROC16)&thunk, lParam );
348 /*************************************************************************
349 * THUNK_EnumFontFamilies16 (GDI.330)
351 INT16 WINAPI THUNK_EnumFontFamilies16( HDC16 hdc, LPCSTR lpszFamily,
352 FONTENUMPROC16 func, LPARAM lParam )
354 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
355 return EnumFontFamilies16(hdc, lpszFamily, (FONTENUMPROC16)&thunk, lParam);
359 /*************************************************************************
360 * THUNK_EnumFontFamiliesEx16 (GDI.613)
362 INT16 WINAPI THUNK_EnumFontFamiliesEx16( HDC16 hdc, LPLOGFONT16 lpLF,
363 FONTENUMPROCEX16 func, LPARAM lParam,
364 DWORD reserved )
366 DECL_THUNK( thunk, func, THUNK_CallTo16_word_llwl );
367 return EnumFontFamiliesEx16( hdc, lpLF, (FONTENUMPROCEX16)&thunk,
368 lParam, reserved );
372 /**********************************************************************
373 * THUNK_LineDDA16 (GDI.100)
375 void WINAPI THUNK_LineDDA16( INT16 nXStart, INT16 nYStart, INT16 nXEnd,
376 INT16 nYEnd, LINEDDAPROC16 func, LPARAM lParam )
378 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wwl );
379 LineDDA16( nXStart, nYStart, nXEnd, nYEnd, (LINEDDAPROC16)&thunk, lParam );
383 /*******************************************************************
384 * THUNK_EnumWindows16 (USER.54)
386 BOOL16 WINAPI THUNK_EnumWindows16( WNDENUMPROC16 func, LPARAM lParam )
388 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
389 return EnumWindows16( (WNDENUMPROC16)&thunk, lParam );
393 /**********************************************************************
394 * THUNK_EnumChildWindows16 (USER.55)
396 BOOL16 WINAPI THUNK_EnumChildWindows16( HWND16 parent, WNDENUMPROC16 func,
397 LPARAM lParam )
399 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
400 return EnumChildWindows16( parent, (WNDENUMPROC16)&thunk, lParam );
404 /**********************************************************************
405 * THUNK_EnumTaskWindows16 (USER.225)
407 BOOL16 WINAPI THUNK_EnumTaskWindows16( HTASK16 hTask, WNDENUMPROC16 func,
408 LPARAM lParam )
410 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wl );
411 return EnumTaskWindows16( hTask, (WNDENUMPROC16)&thunk, lParam );
415 /***********************************************************************
416 * THUNK_EnumProps16 (USER.27)
418 INT16 WINAPI THUNK_EnumProps16( HWND16 hwnd, PROPENUMPROC16 func )
420 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wlw );
421 return EnumProps16( hwnd, (PROPENUMPROC16)&thunk );
425 /***********************************************************************
426 * THUNK_GrayString16 (USER.185)
428 BOOL16 WINAPI THUNK_GrayString16( HDC16 hdc, HBRUSH16 hbr,
429 GRAYSTRINGPROC16 func, LPARAM lParam,
430 INT16 cch, INT16 x, INT16 y,
431 INT16 cx, INT16 cy )
433 DECL_THUNK( thunk, func, THUNK_CallTo16_word_wlw );
434 if (!func)
435 return GrayString16( hdc, hbr, NULL, lParam, cch, x, y, cx, cy );
436 else
437 return GrayString16( hdc, hbr, (GRAYSTRINGPROC16)&thunk, lParam, cch,
438 x, y, cx, cy );
442 /***********************************************************************
443 * THUNK_SetWindowsHook16 (USER.121)
445 FARPROC16 WINAPI THUNK_SetWindowsHook16( INT16 id, HOOKPROC16 proc )
447 HINSTANCE16 hInst = FarGetOwner16( HIWORD(proc) );
448 HTASK16 hTask = (id == WH_MSGFILTER) ? GetCurrentTask() : 0;
449 THUNK *thunk = THUNK_Alloc( (FARPROC16)proc, (RELAY)THUNK_CallTo16_long_wwl );
450 if (!thunk) return 0;
451 return (FARPROC16)SetWindowsHookEx16( id, (HOOKPROC16)thunk, hInst, hTask);
455 /***********************************************************************
456 * THUNK_UnhookWindowsHook16 (USER.234)
458 BOOL16 WINAPI THUNK_UnhookWindowsHook16( INT16 id, HOOKPROC16 proc )
460 BOOL16 ret;
461 THUNK *thunk = THUNK_Find( (FARPROC16)proc );
462 if (!thunk) return FALSE;
463 ret = UnhookWindowsHook16( id, (HOOKPROC16)thunk );
464 THUNK_Free( thunk );
465 return ret;
469 /***********************************************************************
470 * THUNK_SetWindowsHookEx16 (USER.291)
472 HHOOK WINAPI THUNK_SetWindowsHookEx16( INT16 id, HOOKPROC16 proc,
473 HINSTANCE16 hInst, HTASK16 hTask )
475 THUNK *thunk = THUNK_Alloc( (FARPROC16)proc, (RELAY)THUNK_CallTo16_long_wwl );
476 if (!thunk) return 0;
477 return SetWindowsHookEx16( id, (HOOKPROC16)thunk, hInst, hTask );
481 /***********************************************************************
482 * THUNK_UnhookWindowHookEx16 (USER.292)
484 BOOL16 WINAPI THUNK_UnhookWindowsHookEx16( HHOOK hhook )
486 THUNK *thunk = (THUNK *)HOOK_GetProc16( hhook );
487 BOOL16 ret = UnhookWindowsHookEx16( hhook );
488 if (thunk) THUNK_Free( thunk );
489 return ret;
494 static FARPROC16 defDCHookProc = NULL;
496 /***********************************************************************
497 * THUNK_SetDCHook (GDI.190)
499 BOOL16 WINAPI THUNK_SetDCHook( HDC16 hdc, FARPROC16 proc, DWORD dwHookData )
501 THUNK *thunk, *oldThunk;
503 if (!defDCHookProc) /* Get DCHook Win16 entry point */
505 HMODULE16 hModule = GetModuleHandle16( "USER" );
506 NE_MODULE *pModule = NE_GetPtr( hModule );
508 if ( pModule && (pModule->flags & NE_FFLAGS_BUILTIN) )
509 defDCHookProc = NE_GetEntryPoint( hModule, 362 );
510 else
511 defDCHookProc = (FARPROC16)-1;
514 if (!proc)
515 thunk = NULL;
516 else if (proc != defDCHookProc)
518 thunk = THUNK_Alloc( proc, (RELAY)THUNK_CallTo16_word_wwll );
519 if (!thunk) return FALSE;
521 else thunk = (THUNK *)DCHook16;
523 /* Free the previous thunk */
524 GetDCHook( hdc, (FARPROC16 *)&oldThunk );
525 if (oldThunk && (oldThunk != (THUNK *)DCHook16)) THUNK_Free( oldThunk );
527 return SetDCHook( hdc, (FARPROC16)thunk, dwHookData );
531 /***********************************************************************
532 * THUNK_GetDCHook (GDI.191)
534 DWORD WINAPI THUNK_GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
536 THUNK *thunk = NULL;
537 DWORD ret = GetDCHook( hdc, (FARPROC16 *)&thunk );
538 if (thunk)
540 if (thunk == (THUNK *)DCHook16)
542 /* Note: we can only get here when running built-in USER */
544 if (!defDCHookProc) /* Get DCHook Win16 entry point */
545 defDCHookProc = NE_GetEntryPoint(GetModuleHandle16("USER"),362);
547 *phookProc = defDCHookProc;
549 else *phookProc = thunk->proc;
551 return ret;
555 /***********************************************************************
556 * THUNK_SetTaskSignalProc (KERNEL.38)
558 FARPROC16 WINAPI THUNK_SetTaskSignalProc( HTASK16 hTask, FARPROC16 proc )
560 THUNK *thunk = THUNK_Alloc( proc, (RELAY)THUNK_CallTo16_word_wwwww );
561 if ( !thunk ) return NULL;
563 thunk = (THUNK*)SetTaskSignalProc( hTask, (FARPROC16)thunk );
564 if ( !thunk ) return NULL;
566 proc = thunk->proc;
567 THUNK_Free( thunk );
568 return proc;
571 /***********************************************************************
572 * THUNK_CreateThread16 (KERNEL.441)
574 static DWORD CALLBACK THUNK_StartThread16( LPVOID threadArgs )
576 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
577 DWORD param = ((DWORD *)threadArgs)[1];
578 HeapFree( GetProcessHeap(), 0, threadArgs );
580 return THUNK_CallTo16_long_l( start, param );
582 HANDLE WINAPI THUNK_CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
583 FARPROC16 start, SEGPTR param,
584 DWORD flags, LPDWORD id )
586 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
587 if (!threadArgs) return INVALID_HANDLE_VALUE;
588 threadArgs[0] = (DWORD)start;
589 threadArgs[1] = (DWORD)param;
591 return CreateThread( sa, stack, THUNK_StartThread16, threadArgs, flags, id );
594 /***********************************************************************
595 * THUNK_WOWCallback16Ex (WOW32.3)(KERNEL32.55)
596 * Generic thunking routine to call 16 bit functions from 32bit code.
598 * RETURNS
599 * TRUE if the call was done
601 static BOOL WINAPI THUNK_WOWCallback16Ex(
602 FARPROC16 proc, /* [in] 16bit function to call */
603 DWORD dwFlags, /* [in] flags (WCB_*) */
604 DWORD cbArgs, /* [in] number of arguments */
605 LPVOID xargs, /* [in/out] arguments */
606 LPDWORD pdwret /* [out] return value of the 16bit call */
608 LPDWORD args = (LPDWORD)xargs;
609 DWORD ret,i;
611 TRACE_(relay)("(%p,0x%08lx,%ld,%p,%p)\n",
612 proc,dwFlags,cbArgs,xargs,pdwret
614 if (dwFlags == WCB16_CDECL) {
615 /* swap the arguments */
616 args = HeapAlloc(GetProcessHeap(),0,cbArgs*sizeof(DWORD));
617 for (i=0;i<cbArgs;i++)
618 args[i] = ((DWORD*)xargs)[cbArgs-i-1];
620 switch (cbArgs) {
621 case 0: ret = THUNK_CallTo16_long_(proc);break;
622 case 1: ret = THUNK_CallTo16_long_l(proc,args[0]);break;
623 case 2: ret = THUNK_CallTo16_long_ll(proc,args[0],args[1]);break;
624 case 3: ret = THUNK_CallTo16_long_lll(proc,args[0],args[1],args[2]);break;
625 case 4: ret = THUNK_CallTo16_long_llll(proc,args[0],args[1],args[2],args[3]);
626 break;
627 case 5: ret = THUNK_CallTo16_long_lllll(proc,args[0],args[1],args[2],args[3],
628 args[4]
630 break;
631 case 6: ret = THUNK_CallTo16_long_llllll(proc,args[0],args[1],args[2],args[3],
632 args[4],args[5]
634 break;
635 case 7: ret = THUNK_CallTo16_long_lllllll(proc,args[0],args[1],args[2],args[3],
636 args[4],args[5],args[6]
638 break;
639 case 8: ret = THUNK_CallTo16_long_llllllll(proc,args[0],args[1],args[2],args[3],
640 args[4],args[5],args[6],args[7]
642 break;
643 case 9: ret = THUNK_CallTo16_long_lllllllll(proc,args[0],args[1],args[2],args[3],
644 args[4],args[5],args[6],args[7],args[8]
646 break;
647 case 10:ret = THUNK_CallTo16_long_llllllllll(proc,args[0],args[1],args[2],args[3],
648 args[4],args[5],args[6],args[7],args[8],args[9]
650 break;
651 case 11:ret = THUNK_CallTo16_long_lllllllllll(proc,args[0],args[1],args[2],
652 args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]
654 break;
655 case 12:ret = THUNK_CallTo16_long_llllllllllll(proc,args[0],args[1],args[2],
656 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
657 args[10],args[11]
659 break;
660 case 13:ret = THUNK_CallTo16_long_lllllllllllll(proc,args[0],args[1],args[2],
661 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
662 args[10],args[11],args[12]
664 break;
665 case 14:ret = THUNK_CallTo16_long_llllllllllllll(proc,args[0],args[1],args[2],
666 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
667 args[10],args[11],args[12],args[13]
669 break;
670 case 15:ret = THUNK_CallTo16_long_lllllllllllllll(proc,args[0],args[1],args[2],
671 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
672 args[10],args[11],args[12],args[13],args[14]
674 break;
675 case 16:ret = THUNK_CallTo16_long_llllllllllllllll(proc,args[0],args[1],args[2],
676 args[3],args[4],args[5],args[6],args[7],args[8],args[9],
677 args[10],args[11],args[12],args[13],args[14],args[15]
679 break;
680 default:
681 ERR_(thunk)("%ld arguments not supported.\n",cbArgs);
682 if (dwFlags == WCB16_CDECL)
683 HeapFree(GetProcessHeap(),0,args);
684 return FALSE;
686 if (dwFlags == WCB16_CDECL)
687 HeapFree(GetProcessHeap(),0,args);
688 if (pdwret)
689 *pdwret = ret;
690 return TRUE;
693 /***********************************************************************
694 * THUNK_MOUSE_Enable (MOUSE.2)
696 static VOID WINAPI THUNK_CallMouseEventProc( FARPROC16 proc,
697 DWORD dwFlags, DWORD dx, DWORD dy,
698 DWORD cButtons, DWORD dwExtraInfo )
700 CONTEXT86 context;
702 memset( &context, 0, sizeof(context) );
703 CS_reg(&context) = SELECTOROF( proc );
704 EIP_reg(&context) = OFFSETOF( proc );
705 AX_reg(&context) = (WORD)dwFlags;
706 BX_reg(&context) = (WORD)dx;
707 CX_reg(&context) = (WORD)dy;
708 DX_reg(&context) = (WORD)cButtons;
709 SI_reg(&context) = LOWORD( dwExtraInfo );
710 DI_reg(&context) = HIWORD( dwExtraInfo );
712 CallTo16RegisterShort( &context, 0 );
714 VOID WINAPI THUNK_MOUSE_Enable( FARPROC16 proc )
716 static THUNK *lastThunk = NULL;
717 static FARPROC16 lastProc = NULL;
719 if ( lastProc != proc )
721 if ( lastThunk )
722 THUNK_Free( lastThunk );
724 if ( !proc )
725 lastThunk = NULL;
726 else
727 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallMouseEventProc );
729 lastProc = proc;
732 MOUSE_Enable( (LPMOUSE_EVENT_PROC)lastThunk );
735 /***********************************************************************
736 * GetMouseEventProc (USER.337)
738 FARPROC16 WINAPI GetMouseEventProc16(void)
740 HMODULE16 hmodule = GetModuleHandle16("USER");
741 return NE_GetEntryPoint( hmodule, NE_GetOrdinal( hmodule, "mouse_event" ));
745 /***********************************************************************
746 * WIN16_mouse_event (USER.299)
748 void WINAPI WIN16_mouse_event( CONTEXT86 *context )
750 mouse_event( AX_reg(context), BX_reg(context), CX_reg(context),
751 DX_reg(context), MAKELONG(SI_reg(context), DI_reg(context)) );
755 /***********************************************************************
756 * THUNK_KEYBD_Enable (KEYBOARD.2)
758 static VOID WINAPI THUNK_CallKeybdEventProc( FARPROC16 proc,
759 BYTE bVk, BYTE bScan,
760 DWORD dwFlags, DWORD dwExtraInfo )
762 CONTEXT86 context;
764 memset( &context, 0, sizeof(context) );
765 CS_reg(&context) = SELECTOROF( proc );
766 EIP_reg(&context) = OFFSETOF( proc );
767 AH_reg(&context) = (dwFlags & KEYEVENTF_KEYUP)? 0x80 : 0;
768 AL_reg(&context) = bVk;
769 BH_reg(&context) = (dwFlags & KEYEVENTF_EXTENDEDKEY)? 1 : 0;
770 BL_reg(&context) = bScan;
771 SI_reg(&context) = LOWORD( dwExtraInfo );
772 DI_reg(&context) = HIWORD( dwExtraInfo );
774 CallTo16RegisterShort( &context, 0 );
776 VOID WINAPI THUNK_KEYBOARD_Enable( FARPROC16 proc, LPBYTE lpKeyState )
778 static THUNK *lastThunk = NULL;
779 static FARPROC16 lastProc = NULL;
781 if ( lastProc != proc )
783 if ( lastThunk )
784 THUNK_Free( lastThunk );
786 if ( !proc )
787 lastThunk = NULL;
788 else
789 lastThunk = THUNK_Alloc( proc, (RELAY)THUNK_CallKeybdEventProc );
791 lastProc = proc;
794 KEYBOARD_Enable( (LPKEYBD_EVENT_PROC)lastThunk, lpKeyState );
797 /***********************************************************************
798 * WIN16_keybd_event (USER.289)
800 void WINAPI WIN16_keybd_event( CONTEXT86 *context )
802 DWORD dwFlags = 0;
804 if (AH_reg(context) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
805 if (BH_reg(context) & 1 ) dwFlags |= KEYEVENTF_EXTENDEDKEY;
807 keybd_event( AL_reg(context), BL_reg(context),
808 dwFlags, MAKELONG(SI_reg(context), DI_reg(context)) );
812 /***********************************************************************
813 * WIN16_CreateSystemTimer (SYSTEM.2)
815 static void THUNK_CallSystemTimerProc( FARPROC16 proc, WORD timer )
817 CONTEXT86 context;
818 memset( &context, '\0', sizeof(context) );
820 CS_reg( &context ) = SELECTOROF( proc );
821 IP_reg( &context ) = OFFSETOF( proc );
822 BP_reg( &context ) = OFFSETOF( NtCurrentTeb()->cur_stack )
823 + (WORD)&((STACK16FRAME*)0)->bp;
825 AX_reg( &context ) = timer;
827 if ( _ConfirmWin16Lock() )
829 FIXME_(system)("Skipping timer %d callback because timer signal "
830 "arrived while we own the Win16Lock!\n", timer );
831 return;
834 CallTo16RegisterShort( &context, 0 );
836 /* FIXME: This does not work if the signal occurs while this thread
837 is currently in 16-bit code. With the current structure
838 of the Wine thunking code, this seems to be hard to fix ... */
840 WORD WINAPI WIN16_CreateSystemTimer( WORD rate, FARPROC16 proc )
842 THUNK *thunk = THUNK_Alloc( proc, (RELAY)THUNK_CallSystemTimerProc );
843 WORD timer = CreateSystemTimer( rate, (SYSTEMTIMERPROC)thunk );
844 if (!timer) THUNK_Free( thunk );
845 return timer;
848 /***********************************************************************
849 * THUNK_InitCallout
851 void THUNK_InitCallout(void)
853 HMODULE hModule;
854 WINE_MODREF *wm;
855 NE_MODULE *pModule;
857 hModule = GetModuleHandleA( "USER32" );
858 wm = MODULE32_LookupHMODULE( hModule );
859 if ( wm && !(wm->flags & WINE_MODREF_INTERNAL) )
861 #define GETADDR( var, name ) \
862 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
864 GETADDR( PeekMessageA, "PeekMessageA" );
865 GETADDR( PeekMessageW, "PeekMessageW" );
866 GETADDR( GetMessageA, "GetMessageA" );
867 GETADDR( GetMessageW, "GetMessageW" );
868 GETADDR( SendMessageA, "SendMessageA" );
869 GETADDR( SendMessageW, "SendMessageW" );
870 GETADDR( PostMessageA, "PostMessageA" );
871 GETADDR( PostMessageW, "PostMessageW" );
872 GETADDR( PostThreadMessageA, "PostThreadMessageA" );
873 GETADDR( PostThreadMessageW, "PostThreadMessageW" );
874 GETADDR( TranslateMessage, "TranslateMessage" );
875 GETADDR( DispatchMessageW, "DispatchMessageW" );
876 GETADDR( DispatchMessageA, "DispatchMessageA" );
877 GETADDR( RedrawWindow, "RedrawWindow" );
878 GETADDR( UserSignalProc, "UserSignalProc" );
880 #undef GETADDR
883 hModule = GetModuleHandle16( "USER" );
884 pModule = NE_GetPtr( hModule );
885 if ( pModule && !(pModule->flags & NE_FFLAGS_BUILTIN) )
887 #define GETADDR( var, name, thk ) \
888 *(FARPROC *)&Callout.##var = (FARPROC) \
889 THUNK_Alloc( WIN32_GetProcAddress16( hModule, name ), \
890 (RELAY)THUNK_CallTo16_##thk )
892 GETADDR( PeekMessage16, "PeekMessage", word_lwwww );
893 GETADDR( GetMessage16, "GetMessage", word_lwww );
894 GETADDR( SendMessage16, "SendMessage", long_wwwl );
895 GETADDR( PostMessage16, "PostMessage", word_wwwl );
896 GETADDR( PostAppMessage16, "PostAppMessage", word_wwwl );
897 GETADDR( TranslateMessage16, "TranslateMessage", word_l );
898 GETADDR( DispatchMessage16, "DispatchMessage", long_l );
899 GETADDR( RedrawWindow16, "RedrawWindow", word_wlww );
900 GETADDR( InitApp16, "InitApp", word_w );
901 GETADDR( InitThreadInput16, "InitThreadInput", word_ww );
902 GETADDR( UserYield16, "UserYield", word_ );
903 GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
905 #undef GETADDR
909 /***********************************************************************
910 * 16->32 Flat Thunk routines:
913 /***********************************************************************
914 * ThunkConnect16 (KERNEL.651)
915 * Connects a 32bit and a 16bit thunkbuffer.
917 UINT WINAPI ThunkConnect16(
918 LPSTR module16, /* [in] name of win16 dll */
919 LPSTR module32, /* [in] name of win32 dll */
920 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
921 DWORD dwReason, /* [in] initialisation argument */
922 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
923 LPSTR thunkfun32, /* [in] win32 thunkfunction */
924 WORD cs /* [in] CS of win16 dll */
926 BOOL directionSL;
928 if (!lstrncmpA(TD->magic, "SL01", 4))
930 directionSL = TRUE;
932 TRACE_(thunk)("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
933 module16, (DWORD)TD, module32, thunkfun32, dwReason);
935 else if (!lstrncmpA(TD->magic, "LS01", 4))
937 directionSL = FALSE;
939 TRACE_(thunk)("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
940 module16, (DWORD)TD, module32, thunkfun32, dwReason);
942 else
944 ERR_(thunk)("Invalid magic %c%c%c%c\n",
945 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
946 return 0;
949 switch (dwReason)
951 case DLL_PROCESS_ATTACH:
952 if (directionSL)
954 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
955 struct ThunkDataSL *SL = SL16->fpData;
957 if (SL == NULL)
959 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
961 SL->common = SL16->common;
962 SL->flags1 = SL16->flags1;
963 SL->flags2 = SL16->flags2;
965 SL->apiDB = PTR_SEG_TO_LIN(SL16->apiDatabase);
966 SL->targetDB = NULL;
968 lstrcpynA(SL->pszDll16, module16, 255);
969 lstrcpynA(SL->pszDll32, module32, 255);
971 /* We should create a SEGPTR to the ThunkDataSL,
972 but since the contents are not in the original format,
973 any access to this by 16-bit code would crash anyway. */
974 SL16->spData = 0;
975 SL16->fpData = SL;
979 if (SL->flags2 & 0x80000000)
981 TRACE_(thunk)("Preloading 32-bit library\n");
982 LoadLibraryA(module32);
985 else
987 /* nothing to do */
989 break;
991 case DLL_PROCESS_DETACH:
992 /* FIXME: cleanup */
993 break;
996 return 1;
1000 /***********************************************************************
1001 * C16ThkSL (KERNEL.630)
1004 void WINAPI C16ThkSL(CONTEXT86 *context)
1006 extern void CallFrom16Thunk(void);
1007 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
1008 WORD cs, ds;
1009 GET_CS(cs);
1010 GET_DS(ds);
1012 /* We produce the following code:
1014 * mov ax, __FLATDS
1015 * mov es, ax
1016 * movzx ecx, cx
1017 * mov edx, es:[ecx + $EDX]
1018 * push bp
1019 * push edx
1020 * call __FLATCS:CallFrom16_t_long_
1023 *x++ = 0xB8; *((WORD *)x)++ = ds;
1024 *x++ = 0x8E; *x++ = 0xC0;
1025 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
1026 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1027 *x++ = 0x91; *((DWORD *)x)++ = EDX_reg(context);
1029 *x++ = 0x55;
1030 *x++ = 0x66; *x++ = 0x52;
1031 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
1032 *((WORD *)x)++ = cs;
1034 /* Jump to the stub code just created */
1035 IP_reg(context) = LOWORD(EAX_reg(context));
1036 CS_reg(context) = HIWORD(EAX_reg(context));
1038 /* Since C16ThkSL got called by a jmp, we need to leave the
1039 orginal return address on the stack */
1040 SP_reg(context) -= 4;
1043 /***********************************************************************
1044 * C16ThkSL01 (KERNEL.631)
1047 void WINAPI C16ThkSL01(CONTEXT86 *context)
1049 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
1051 if (stub)
1053 struct ThunkDataSL16 *SL16 = PTR_SEG_TO_LIN(EDX_reg(context));
1054 struct ThunkDataSL *td = SL16->fpData;
1056 extern void CallFrom16Thunk(void);
1057 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
1058 WORD cs;
1059 GET_CS(cs);
1061 if (!td)
1063 ERR_(thunk)("ThunkConnect16 was not called!\n");
1064 return;
1067 TRACE_(thunk)("Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
1070 /* We produce the following code:
1072 * xor eax, eax
1073 * mov edx, $td
1074 * call C16ThkSL01
1075 * push bp
1076 * push edx
1077 * call __FLATCS:CallFrom16_t_long_
1080 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1081 *x++ = 0x66; *x++ = 0xBA; *((DWORD *)x)++ = (DWORD)td;
1082 *x++ = 0x9A; *((DWORD *)x)++ = procAddress;
1084 *x++ = 0x55;
1085 *x++ = 0x66; *x++ = 0x52;
1086 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
1087 *((WORD *)x)++ = cs;
1089 /* Jump to the stub code just created */
1090 IP_reg(context) = LOWORD(EAX_reg(context));
1091 CS_reg(context) = HIWORD(EAX_reg(context));
1093 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1094 orginal return address on the stack */
1095 SP_reg(context) -= 4;
1097 else
1099 struct ThunkDataSL *td = (struct ThunkDataSL *)EDX_reg(context);
1100 DWORD targetNr = CX_reg(context) / 4;
1101 struct SLTargetDB *tdb;
1103 TRACE_(thunk)("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1104 (DWORD)PROCESS_Current(), targetNr, (DWORD)td);
1106 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1107 if (tdb->process == PROCESS_Current())
1108 break;
1110 if (!tdb)
1112 TRACE_(thunk)("Loading 32-bit library %s\n", td->pszDll32);
1113 LoadLibraryA(td->pszDll32);
1115 for (tdb = td->targetDB; tdb; tdb = tdb->next)
1116 if (tdb->process == PROCESS_Current())
1117 break;
1120 if (tdb)
1122 EDX_reg(context) = tdb->targetTable[targetNr];
1124 TRACE_(thunk)("Call target is %08lx\n", EDX_reg(context));
1126 else
1128 WORD *stack = PTR_SEG_OFF_TO_LIN(SS_reg(context), SP_reg(context));
1129 DX_reg(context) = HIWORD(td->apiDB[targetNr].errorReturnValue);
1130 AX_reg(context) = LOWORD(td->apiDB[targetNr].errorReturnValue);
1131 IP_reg(context) = stack[2];
1132 CS_reg(context) = stack[3];
1133 SP_reg(context) += td->apiDB[targetNr].nrArgBytes + 4;
1135 ERR_(thunk)("Process %08lx did not ThunkConnect32 %s to %s\n",
1136 (DWORD)PROCESS_Current(), td->pszDll32, td->pszDll16);
1141 DWORD WINAPI
1142 WOW16Call(WORD x,WORD y,WORD z) {
1143 int i;
1144 DWORD calladdr;
1145 FIXME_(thunk)("(0x%04x,0x%04x,%d),calling (",x,y,z);
1147 for (i=0;i<x/2;i++) {
1148 WORD a = STACK16_POP(NtCurrentTeb(),2);
1149 DPRINTF("%04x ",a);
1151 calladdr = STACK16_POP(NtCurrentTeb(),4);
1152 DPRINTF(") calling address was 0x%08lx\n",calladdr);
1153 return 0;
1157 /***********************************************************************
1158 * 16<->32 Thunklet/Callback API:
1161 #include "pshpack1.h"
1162 typedef struct _THUNKLET
1164 BYTE prefix_target;
1165 BYTE pushl_target;
1166 DWORD target;
1168 BYTE prefix_relay;
1169 BYTE pushl_relay;
1170 DWORD relay;
1172 BYTE jmp_glue;
1173 DWORD glue;
1175 BYTE type;
1176 HINSTANCE16 owner;
1177 struct _THUNKLET *next;
1178 } THUNKLET;
1179 #include "poppack.h"
1181 #define THUNKLET_TYPE_LS 1
1182 #define THUNKLET_TYPE_SL 2
1184 static HANDLE ThunkletHeap = 0;
1185 static THUNKLET *ThunkletAnchor = NULL;
1187 static FARPROC ThunkletSysthunkGlueLS = 0;
1188 static SEGPTR ThunkletSysthunkGlueSL = 0;
1190 static FARPROC ThunkletCallbackGlueLS = 0;
1191 static SEGPTR ThunkletCallbackGlueSL = 0;
1193 /***********************************************************************
1194 * THUNK_ThunkletInit
1196 static BOOL THUNK_ThunkletInit( void )
1198 LPBYTE thunk;
1200 ThunkletHeap = HeapCreate(HEAP_WINE_SEGPTR | HEAP_WINE_CODE16SEG, 0, 0);
1201 if (!ThunkletHeap) return FALSE;
1203 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
1204 if (!thunk) return FALSE;
1206 ThunkletSysthunkGlueLS = (FARPROC)thunk;
1207 *thunk++ = 0x58; /* popl eax */
1208 *thunk++ = 0xC3; /* ret */
1210 ThunkletSysthunkGlueSL = HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1211 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
1212 *thunk++ = 0xCB; /* lret */
1214 return TRUE;
1217 /***********************************************************************
1218 * SetThunkletCallbackGlue (KERNEL.560)
1220 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
1222 ThunkletCallbackGlueLS = glueLS;
1223 ThunkletCallbackGlueSL = glueSL;
1227 /***********************************************************************
1228 * THUNK_FindThunklet
1230 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
1231 DWORD glue, BYTE type )
1233 THUNKLET *thunk;
1235 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
1236 if ( thunk->type == type
1237 && thunk->target == target
1238 && thunk->relay == relay
1239 && ( type == THUNKLET_TYPE_LS ?
1240 ( thunk->glue == glue - (DWORD)&thunk->type )
1241 : ( thunk->glue == glue ) ) )
1242 return thunk;
1244 return NULL;
1247 /***********************************************************************
1248 * THUNK_AllocLSThunklet
1250 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
1251 FARPROC glue, HTASK16 owner )
1253 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1254 THUNKLET_TYPE_LS );
1255 if (!thunk)
1257 TDB *pTask = (TDB*)GlobalLock16( owner );
1259 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1260 return 0;
1262 thunk->prefix_target = thunk->prefix_relay = 0x90;
1263 thunk->pushl_target = thunk->pushl_relay = 0x68;
1264 thunk->jmp_glue = 0xE9;
1266 thunk->target = (DWORD)target;
1267 thunk->relay = (DWORD)relay;
1268 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
1270 thunk->type = THUNKLET_TYPE_LS;
1271 thunk->owner = pTask? pTask->hInstance : 0;
1273 thunk->next = ThunkletAnchor;
1274 ThunkletAnchor = thunk;
1277 return (FARPROC)thunk;
1280 /***********************************************************************
1281 * THUNK_AllocSLThunklet
1283 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
1284 SEGPTR glue, HTASK16 owner )
1286 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
1287 THUNKLET_TYPE_SL );
1288 if (!thunk)
1290 TDB *pTask = (TDB*)GlobalLock16( owner );
1292 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
1293 return 0;
1295 thunk->prefix_target = thunk->prefix_relay = 0x66;
1296 thunk->pushl_target = thunk->pushl_relay = 0x68;
1297 thunk->jmp_glue = 0xEA;
1299 thunk->target = (DWORD)target;
1300 thunk->relay = (DWORD)relay;
1301 thunk->glue = (DWORD)glue;
1303 thunk->type = THUNKLET_TYPE_SL;
1304 thunk->owner = pTask? pTask->hInstance : 0;
1306 thunk->next = ThunkletAnchor;
1307 ThunkletAnchor = thunk;
1310 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1313 /**********************************************************************
1314 * IsLSThunklet
1316 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
1318 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
1319 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
1320 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
1323 /**********************************************************************
1324 * IsSLThunklet (KERNEL.612)
1326 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
1328 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
1329 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
1330 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
1335 /***********************************************************************
1336 * AllocLSThunkletSysthunk (KERNEL.607)
1338 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1339 FARPROC relay, DWORD dummy )
1341 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1342 ThunkletSysthunkGlueLS, GetCurrentTask() );
1345 /***********************************************************************
1346 * AllocSLThunkletSysthunk (KERNEL.608)
1348 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1349 SEGPTR relay, DWORD dummy )
1351 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1352 ThunkletSysthunkGlueSL, GetCurrentTask() );
1356 /***********************************************************************
1357 * AllocLSThunkletCallbackEx (KERNEL.567)
1359 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1360 DWORD relay, HTASK16 task )
1362 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1363 if ( !thunk ) return NULL;
1365 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
1366 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1367 return (FARPROC)thunk->target;
1369 return THUNK_AllocLSThunklet( target, relay,
1370 ThunkletCallbackGlueLS, task );
1373 /***********************************************************************
1374 * AllocSLThunkletCallbackEx (KERNEL.568)
1376 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1377 DWORD relay, HTASK16 task )
1379 THUNKLET *thunk = (THUNKLET *)target;
1380 if ( !thunk ) return 0;
1382 if ( IsLSThunklet( thunk ) && thunk->relay == relay
1383 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1384 return (SEGPTR)thunk->target;
1386 return THUNK_AllocSLThunklet( target, relay,
1387 ThunkletCallbackGlueSL, task );
1390 /***********************************************************************
1391 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
1393 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
1395 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
1398 /***********************************************************************
1399 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
1401 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
1403 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
1406 /***********************************************************************
1407 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
1409 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
1411 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
1412 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1413 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
1414 return (FARPROC)thunk->target;
1416 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1417 (DWORD)ThunkletCallbackGlueLS,
1418 THUNKLET_TYPE_LS );
1419 return (FARPROC)thunk;
1422 /***********************************************************************
1423 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
1425 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
1427 THUNKLET *thunk = (THUNKLET *)target;
1428 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1429 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
1430 return (SEGPTR)thunk->target;
1432 thunk = THUNK_FindThunklet( (DWORD)target, relay,
1433 (DWORD)ThunkletCallbackGlueSL,
1434 THUNKLET_TYPE_SL );
1435 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
1439 /***********************************************************************
1440 * FreeThunklet16 (KERNEL.611)
1442 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
1444 return FALSE;
1447 /***********************************************************************
1448 * Callback Client API
1451 #define N_CBC_FIXED 20
1452 #define N_CBC_VARIABLE 10
1453 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1455 static SEGPTR *CBClientRelay16[ N_CBC_TOTAL ];
1456 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
1458 /***********************************************************************
1459 * RegisterCBClient (KERNEL.619)
1461 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1462 SEGPTR *relay16, FARPROC *relay32 )
1464 /* Search for free Callback ID */
1465 if ( wCBCId == -1 )
1466 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
1467 if ( !CBClientRelay16[ wCBCId ] )
1468 break;
1470 /* Register Callback ID */
1471 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
1473 CBClientRelay16[ wCBCId ] = relay16;
1474 CBClientRelay32[ wCBCId ] = relay32;
1476 else
1477 wCBCId = 0;
1479 return wCBCId;
1482 /***********************************************************************
1483 * UnRegisterCBClient (KERNEL.622)
1485 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1486 SEGPTR *relay16, FARPROC *relay32 )
1488 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
1489 && CBClientRelay16[ wCBCId ] == relay16
1490 && CBClientRelay32[ wCBCId ] == relay32 )
1492 CBClientRelay16[ wCBCId ] = 0;
1493 CBClientRelay32[ wCBCId ] = 0;
1495 else
1496 wCBCId = 0;
1498 return wCBCId;
1502 /***********************************************************************
1503 * InitCBClient (KERNEL.623)
1505 void WINAPI InitCBClient16( FARPROC glueLS )
1507 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1508 SEGPTR glueSL = (SEGPTR)WIN32_GetProcAddress16( kernel, (LPCSTR)604 );
1510 SetThunkletCallbackGlue16( glueLS, glueSL );
1513 /***********************************************************************
1514 * CBClientGlueSL (KERNEL.604)
1516 void WINAPI CBClientGlueSL( CONTEXT86 *context )
1518 /* Create stack frame */
1519 SEGPTR stackSeg = STACK16_PUSH( NtCurrentTeb(), 12 );
1520 LPWORD stackLin = PTR_SEG_TO_LIN( stackSeg );
1521 SEGPTR glue;
1523 stackLin[3] = BP_reg( context );
1524 stackLin[2] = SI_reg( context );
1525 stackLin[1] = DI_reg( context );
1526 stackLin[0] = DS_reg( context );
1528 BP_reg( context ) = OFFSETOF( stackSeg ) + 6;
1529 SP_reg( context ) = OFFSETOF( stackSeg ) - 4;
1530 GS_reg( context ) = 0;
1532 /* Jump to 16-bit relay code */
1533 glue = CBClientRelay16[ stackLin[5] ][ stackLin[4] ];
1534 CS_reg ( context ) = SELECTOROF( glue );
1535 EIP_reg( context ) = OFFSETOF ( glue );
1538 /***********************************************************************
1539 * CBClientThunkSL (KERNEL.620)
1541 void WINAPI CBClientThunkSL( CONTEXT86 *context )
1543 /* Call 32-bit relay code */
1544 extern DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi );
1546 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1547 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1549 EAX_reg(context) = CALL32_CBClient( proc, args, &ESI_reg( context ) );
1552 /***********************************************************************
1553 * CBClientThunkSLEx (KERNEL.621)
1555 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
1557 /* Call 32-bit relay code */
1558 extern DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args,
1559 DWORD *esi, INT *nArgs );
1561 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
1562 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
1563 INT nArgs;
1564 LPWORD stackLin;
1566 EAX_reg(context) = CALL32_CBClientEx( proc, args, &ESI_reg( context ), &nArgs );
1568 /* Restore registers saved by CBClientGlueSL */
1569 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
1570 BP_reg( context ) = stackLin[3];
1571 SI_reg( context ) = stackLin[2];
1572 DI_reg( context ) = stackLin[1];
1573 DS_reg( context ) = stackLin[0];
1574 SP_reg( context ) += 16+nArgs;
1576 /* Return to caller of CBClient thunklet */
1577 CS_reg ( context ) = stackLin[9];
1578 EIP_reg( context ) = stackLin[8];