4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
9 #include "wine/winbase16.h"
13 #include "builtin16.h"
18 #include "stackframe.h"
20 #include "flatthunk.h"
22 #include "selectors.h"
24 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(thunk
)
29 /* List of the 16-bit callback functions. This list is used */
30 /* by the build program to generate the file if1632/callto16.S */
32 /* ### start build ### */
33 extern WORD CALLBACK
THUNK_CallTo16_word_ (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_lllw (FARPROC16
,LONG
,LONG
,LONG
,WORD
);
45 extern WORD CALLBACK
THUNK_CallTo16_word_llwl (FARPROC16
,LONG
,LONG
,WORD
,LONG
);
46 extern WORD CALLBACK
THUNK_CallTo16_word_lwww (FARPROC16
,LONG
,WORD
,WORD
,WORD
);
47 extern WORD CALLBACK
THUNK_CallTo16_word_wlww (FARPROC16
,WORD
,LONG
,WORD
,WORD
);
48 extern WORD CALLBACK
THUNK_CallTo16_word_wwwl (FARPROC16
,WORD
,WORD
,WORD
,LONG
);
49 extern LONG CALLBACK
THUNK_CallTo16_long_wwwl (FARPROC16
,WORD
,WORD
,WORD
,LONG
);
50 extern WORD CALLBACK
THUNK_CallTo16_word_wllwl(FARPROC16
,WORD
,LONG
,LONG
,WORD
,LONG
);
51 extern WORD CALLBACK
THUNK_CallTo16_word_lwwww(FARPROC16
,LONG
,WORD
,WORD
,WORD
,WORD
);
52 /* ### stop build ### */
58 typedef struct tagTHUNK
60 BYTE popl_eax
; /* 0x58 popl %eax (return address)*/
61 BYTE pushl_func
; /* 0x68 pushl $proc */
62 FARPROC16 proc WINE_PACKED
;
63 BYTE pushl_eax
; /* 0x50 pushl %eax */
64 BYTE jmp
; /* 0xe9 jmp relay (relative jump)*/
65 RELAY relay WINE_PACKED
;
66 struct tagTHUNK
*next WINE_PACKED
;
70 #define CALLTO16_THUNK_MAGIC 0x54484e4b /* "THNK" */
74 #define DECL_THUNK(aname,aproc,arelay) \
76 aname.popl_eax = 0x58; \
77 aname.pushl_func = 0x68; \
78 aname.proc = (FARPROC) (aproc); \
79 aname.pushl_eax = 0x50; \
81 aname.relay = (RELAY)((char *)(arelay) - (char *)(&(aname).next)); \
83 aname.magic = CALLTO16_THUNK_MAGIC;
85 static THUNK
*firstThunk
= NULL
;
87 static BOOL
THUNK_ThunkletInit( void );
89 /* Callbacks function table for the emulator */
90 static const CALLBACKS_TABLE CALLBACK_EmulatorTable
=
92 (void *)CallTo16RegisterShort
, /* CallRegisterShortProc */
93 (void *)CallTo16RegisterLong
, /* CallRegisterLongProc */
94 (void *)THUNK_CallTo16_word_w
, /* CallWindowsExitProc */
95 (void *)THUNK_CallTo16_word_lwww
, /* CallWordBreakProc */
96 (void *)THUNK_CallTo16_word_ww
, /* CallBootAppProc */
97 (void *)THUNK_CallTo16_word_www
, /* CallLoadAppSegProc */
98 (void *)THUNK_CallTo16_word_www
, /* CallLocalNotifyFunc */
99 (void *)THUNK_CallTo16_word_www
, /* CallResourceHandlerProc */
100 (void *)THUNK_CallTo16_long_ll
, /* CallUTProc */
101 (void *)THUNK_CallTo16_long_l
/* CallASPIPostProc */
104 const CALLBACKS_TABLE
*Callbacks
= &CALLBACK_EmulatorTable
;
106 CALLOUT_TABLE Callout
= { 0 };
109 /***********************************************************************
112 BOOL
THUNK_Init(void)
114 /* Initialize Thunklets */
115 return THUNK_ThunkletInit();
118 /***********************************************************************
121 FARPROC
THUNK_Alloc( FARPROC16 func
, RELAY relay
)
127 /* NULL maps to NULL */
128 if ( !func
) return NULL
;
131 * If we got an 16-bit built-in API entry point, retrieve the Wine
132 * 32-bit handler for that API routine.
134 * NOTE: For efficiency reasons, we only check whether the selector
135 * of 'func' points to the code segment of a built-in module.
136 * It might be theoretically possible that the offset is such
137 * that 'func' does not point, in fact, to an API entry point.
138 * In this case, however, the pointer is corrupt anyway.
140 hSeg
= GlobalHandle16( SELECTOROF( func
) );
141 pModule
= NE_GetPtr( FarGetOwner16( hSeg
) );
143 if ( pModule
&& (pModule
->flags
& NE_FFLAGS_BUILTIN
)
144 && NE_SEG_TABLE(pModule
)[0].hSeg
== hSeg
)
146 FARPROC proc
= (FARPROC
)((ENTRYPOINT16
*)PTR_SEG_TO_LIN( func
))->target
;
148 TRACE( "(%04x:%04x, %p) -> built-in API %p\n",
149 SELECTOROF( func
), OFFSETOF( func
), relay
, proc
);
153 /* Otherwise, we need to alloc a thunk */
154 thunk
= HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk
) );
157 thunk
->popl_eax
= 0x58;
158 thunk
->pushl_func
= 0x68;
160 thunk
->pushl_eax
= 0x50;
162 thunk
->relay
= (RELAY
)((char *)relay
- (char *)(&thunk
->next
));
163 thunk
->magic
= CALLTO16_THUNK_MAGIC
;
164 thunk
->next
= firstThunk
;
168 TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
169 SELECTOROF( func
), OFFSETOF( func
), relay
, thunk
);
170 return (FARPROC
)thunk
;
173 /***********************************************************************
176 void THUNK_Free( FARPROC thunk
)
178 THUNK
*t
= (THUNK
*)thunk
;
179 if ( !t
|| IsBadReadPtr( t
, sizeof(*t
) )
180 || t
->magic
!= CALLTO16_THUNK_MAGIC
)
183 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, t
))
185 THUNK
**prev
= &firstThunk
;
186 while (*prev
&& (*prev
!= t
)) prev
= &(*prev
)->next
;
190 HeapFree( GetProcessHeap(), 0, t
);
194 ERR("invalid thunk addr %p\n", thunk
);
199 /***********************************************************************
200 * THUNK_EnumObjects16 (GDI.71)
202 INT16 WINAPI
THUNK_EnumObjects16( HDC16 hdc
, INT16 nObjType
,
203 GOBJENUMPROC16 func
, LPARAM lParam
)
205 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_ll
);
206 return EnumObjects16( hdc
, nObjType
, (GOBJENUMPROC16
)&thunk
, lParam
);
210 /*************************************************************************
211 * THUNK_EnumFonts16 (GDI.70)
213 INT16 WINAPI
THUNK_EnumFonts16( HDC16 hdc
, LPCSTR lpFaceName
,
214 FONTENUMPROC16 func
, LPARAM lParam
)
216 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
217 return EnumFonts16( hdc
, lpFaceName
, (FONTENUMPROC16
)&thunk
, lParam
);
220 /******************************************************************
221 * THUNK_EnumMetaFile16 (GDI.175)
223 BOOL16 WINAPI
THUNK_EnumMetaFile16( HDC16 hdc
, HMETAFILE16 hmf
,
224 MFENUMPROC16 func
, LPARAM lParam
)
226 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wllwl
);
227 return EnumMetaFile16( hdc
, hmf
, (MFENUMPROC16
)&thunk
, lParam
);
231 /*************************************************************************
232 * THUNK_EnumFontFamilies16 (GDI.330)
234 INT16 WINAPI
THUNK_EnumFontFamilies16( HDC16 hdc
, LPCSTR lpszFamily
,
235 FONTENUMPROC16 func
, LPARAM lParam
)
237 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
238 return EnumFontFamilies16(hdc
, lpszFamily
, (FONTENUMPROC16
)&thunk
, lParam
);
242 /*************************************************************************
243 * THUNK_EnumFontFamiliesEx16 (GDI.613)
245 INT16 WINAPI
THUNK_EnumFontFamiliesEx16( HDC16 hdc
, LPLOGFONT16 lpLF
,
246 FONTENUMPROCEX16 func
, LPARAM lParam
,
249 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
250 return EnumFontFamiliesEx16( hdc
, lpLF
, (FONTENUMPROCEX16
)&thunk
,
255 /**********************************************************************
256 * THUNK_LineDDA16 (GDI.100)
258 void WINAPI
THUNK_LineDDA16( INT16 nXStart
, INT16 nYStart
, INT16 nXEnd
,
259 INT16 nYEnd
, LINEDDAPROC16 func
, LPARAM lParam
)
261 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wwl
);
262 LineDDA16( nXStart
, nYStart
, nXEnd
, nYEnd
, (LINEDDAPROC16
)&thunk
, lParam
);
266 /*******************************************************************
267 * THUNK_EnumWindows16 (USER.54)
269 BOOL16 WINAPI
THUNK_EnumWindows16( WNDENUMPROC16 func
, LPARAM lParam
)
271 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
272 return EnumWindows16( (WNDENUMPROC16
)&thunk
, lParam
);
276 /**********************************************************************
277 * THUNK_EnumChildWindows16 (USER.55)
279 BOOL16 WINAPI
THUNK_EnumChildWindows16( HWND16 parent
, WNDENUMPROC16 func
,
282 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
283 return EnumChildWindows16( parent
, (WNDENUMPROC16
)&thunk
, lParam
);
287 /**********************************************************************
288 * THUNK_EnumTaskWindows16 (USER.225)
290 BOOL16 WINAPI
THUNK_EnumTaskWindows16( HTASK16 hTask
, WNDENUMPROC16 func
,
293 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
294 return EnumTaskWindows16( hTask
, (WNDENUMPROC16
)&thunk
, lParam
);
298 /***********************************************************************
299 * THUNK_EnumProps16 (USER.27)
301 INT16 WINAPI
THUNK_EnumProps16( HWND16 hwnd
, PROPENUMPROC16 func
)
303 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wlw
);
304 return EnumProps16( hwnd
, (PROPENUMPROC16
)&thunk
);
308 /***********************************************************************
309 * THUNK_GrayString16 (USER.185)
311 BOOL16 WINAPI
THUNK_GrayString16( HDC16 hdc
, HBRUSH16 hbr
,
312 GRAYSTRINGPROC16 func
, LPARAM lParam
,
313 INT16 cch
, INT16 x
, INT16 y
,
316 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wlw
);
318 return GrayString16( hdc
, hbr
, NULL
, lParam
, cch
, x
, y
, cx
, cy
);
320 return GrayString16( hdc
, hbr
, (GRAYSTRINGPROC16
)&thunk
, lParam
, cch
,
325 /***********************************************************************
326 * THUNK_GetCalloutThunk
328 * Retrieve API entry point with given name from given module.
329 * If module is builtin, return the 32-bit entry point, otherwise
330 * create a 32->16 thunk to the 16-bit entry point, using the
334 static FARPROC
THUNK_GetCalloutThunk( NE_MODULE
*pModule
, LPSTR name
, RELAY relay
)
336 FARPROC16 proc
= WIN32_GetProcAddress16( pModule
->self
, name
);
337 if ( !proc
) return 0;
339 if ( pModule
->flags
& NE_FFLAGS_BUILTIN
)
340 return (FARPROC
)((ENTRYPOINT16
*)PTR_SEG_TO_LIN( proc
))->target
;
342 return (FARPROC
)THUNK_Alloc( proc
, relay
);
345 /***********************************************************************
348 void THUNK_InitCallout(void)
353 hModule
= GetModuleHandleA( "USER32" );
356 #define GETADDR( var, name ) \
357 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
359 GETADDR( PeekMessageA
, "PeekMessageA" );
360 GETADDR( PeekMessageW
, "PeekMessageW" );
361 GETADDR( GetMessageA
, "GetMessageA" );
362 GETADDR( GetMessageW
, "GetMessageW" );
363 GETADDR( SendMessageA
, "SendMessageA" );
364 GETADDR( SendMessageW
, "SendMessageW" );
365 GETADDR( PostMessageA
, "PostMessageA" );
366 GETADDR( PostMessageW
, "PostMessageW" );
367 GETADDR( PostThreadMessageA
, "PostThreadMessageA" );
368 GETADDR( PostThreadMessageW
, "PostThreadMessageW" );
369 GETADDR( TranslateMessage
, "TranslateMessage" );
370 GETADDR( DispatchMessageW
, "DispatchMessageW" );
371 GETADDR( DispatchMessageA
, "DispatchMessageA" );
372 GETADDR( RedrawWindow
, "RedrawWindow" );
373 GETADDR( WaitForInputIdle
, "WaitForInputIdle" );
378 pModule
= NE_GetPtr( GetModuleHandle16( "USER" ) );
381 #define GETADDR( var, name, thk ) \
382 *(FARPROC *)&Callout.##var = THUNK_GetCalloutThunk( pModule, name, \
383 (RELAY)THUNK_CallTo16_##thk )
385 GETADDR( PeekMessage16
, "PeekMessage", word_lwwww
);
386 GETADDR( GetMessage16
, "GetMessage", word_lwww
);
387 GETADDR( SendMessage16
, "SendMessage", long_wwwl
);
388 GETADDR( PostMessage16
, "PostMessage", word_wwwl
);
389 GETADDR( PostAppMessage16
, "PostAppMessage", word_wwwl
);
390 GETADDR( TranslateMessage16
, "TranslateMessage", word_l
);
391 GETADDR( DispatchMessage16
, "DispatchMessage", long_l
);
392 GETADDR( RedrawWindow16
, "RedrawWindow", word_wlww
);
393 GETADDR( FinalUserInit16
, "FinalUserInit", word_
);
394 GETADDR( InitApp16
, "InitApp", word_w
);
395 GETADDR( InitThreadInput16
, "InitThreadInput", word_ww
);
396 GETADDR( UserYield16
, "UserYield", word_
);
397 GETADDR( DestroyIcon32
, "DestroyIcon32", word_ww
);
398 GETADDR( UserSignalProc
, "SignalProc32", word_lllw
);
404 /***********************************************************************
405 * 16->32 Flat Thunk routines:
408 /***********************************************************************
409 * ThunkConnect16 (KERNEL.651)
410 * Connects a 32bit and a 16bit thunkbuffer.
412 UINT WINAPI
ThunkConnect16(
413 LPSTR module16
, /* [in] name of win16 dll */
414 LPSTR module32
, /* [in] name of win32 dll */
415 HINSTANCE16 hInst16
, /* [in] hInst of win16 dll */
416 DWORD dwReason
, /* [in] initialisation argument */
417 struct ThunkDataCommon
*TD
, /* [in/out] thunkbuffer */
418 LPSTR thunkfun32
, /* [in] win32 thunkfunction */
419 WORD cs
/* [in] CS of win16 dll */
423 if (!strncmp(TD
->magic
, "SL01", 4))
427 TRACE("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
428 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
430 else if (!strncmp(TD
->magic
, "LS01", 4))
434 TRACE("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
435 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
439 ERR("Invalid magic %c%c%c%c\n",
440 TD
->magic
[0], TD
->magic
[1], TD
->magic
[2], TD
->magic
[3]);
446 case DLL_PROCESS_ATTACH
:
449 struct ThunkDataSL16
*SL16
= (struct ThunkDataSL16
*)TD
;
450 struct ThunkDataSL
*SL
= SL16
->fpData
;
454 SL
= HeapAlloc(GetProcessHeap(), 0, sizeof(*SL
));
456 SL
->common
= SL16
->common
;
457 SL
->flags1
= SL16
->flags1
;
458 SL
->flags2
= SL16
->flags2
;
460 SL
->apiDB
= PTR_SEG_TO_LIN(SL16
->apiDatabase
);
463 lstrcpynA(SL
->pszDll16
, module16
, 255);
464 lstrcpynA(SL
->pszDll32
, module32
, 255);
466 /* We should create a SEGPTR to the ThunkDataSL,
467 but since the contents are not in the original format,
468 any access to this by 16-bit code would crash anyway. */
474 if (SL
->flags2
& 0x80000000)
476 TRACE("Preloading 32-bit library\n");
477 LoadLibraryA(module32
);
486 case DLL_PROCESS_DETACH
:
495 /***********************************************************************
496 * C16ThkSL (KERNEL.630)
499 void WINAPI
C16ThkSL(CONTEXT86
*context
)
501 LPBYTE stub
= PTR_SEG_TO_LIN(EAX_reg(context
)), x
= stub
;
502 WORD cs
= __get_cs();
503 WORD ds
= __get_ds();
505 /* We produce the following code:
510 * mov edx, es:[ecx + $EDX]
515 * call __FLATCS:CallFrom16Thunk
518 *x
++ = 0xB8; *((WORD
*)x
)++ = ds
;
519 *x
++ = 0x8E; *x
++ = 0xC0;
520 *x
++ = 0x66; *x
++ = 0x0F; *x
++ = 0xB7; *x
++ = 0xC9;
521 *x
++ = 0x67; *x
++ = 0x66; *x
++ = 0x26; *x
++ = 0x8B;
522 *x
++ = 0x91; *((DWORD
*)x
)++ = EDX_reg(context
);
525 *x
++ = 0x66; *x
++ = 0x52;
527 *x
++ = 0x66; *x
++ = 0x52;
528 *x
++ = 0x66; *x
++ = 0x9A; *((DWORD
*)x
)++ = (DWORD
)CallFrom16Thunk
;
531 /* Jump to the stub code just created */
532 EIP_reg(context
) = LOWORD(EAX_reg(context
));
533 CS_reg(context
) = HIWORD(EAX_reg(context
));
535 /* Since C16ThkSL got called by a jmp, we need to leave the
536 original return address on the stack */
537 ESP_reg(context
) -= 4;
540 /***********************************************************************
541 * C16ThkSL01 (KERNEL.631)
544 void WINAPI
C16ThkSL01(CONTEXT86
*context
)
546 LPBYTE stub
= PTR_SEG_TO_LIN(EAX_reg(context
)), x
= stub
;
550 struct ThunkDataSL16
*SL16
= PTR_SEG_TO_LIN(EDX_reg(context
));
551 struct ThunkDataSL
*td
= SL16
->fpData
;
553 DWORD procAddress
= (DWORD
)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
554 WORD cs
= __get_cs();
558 ERR("ThunkConnect16 was not called!\n");
562 TRACE("Creating stub for ThunkDataSL %08lx\n", (DWORD
)td
);
565 /* We produce the following code:
574 * call __FLATCS:CallFrom16Thunk
577 *x
++ = 0x66; *x
++ = 0x33; *x
++ = 0xC0;
578 *x
++ = 0x66; *x
++ = 0xBA; *((DWORD
*)x
)++ = (DWORD
)td
;
579 *x
++ = 0x9A; *((DWORD
*)x
)++ = procAddress
;
582 *x
++ = 0x66; *x
++ = 0x52;
584 *x
++ = 0x66; *x
++ = 0x52;
585 *x
++ = 0x66; *x
++ = 0x9A; *((DWORD
*)x
)++ = (DWORD
)CallFrom16Thunk
;
588 /* Jump to the stub code just created */
589 EIP_reg(context
) = LOWORD(EAX_reg(context
));
590 CS_reg(context
) = HIWORD(EAX_reg(context
));
592 /* Since C16ThkSL01 got called by a jmp, we need to leave the
593 orginal return address on the stack */
594 ESP_reg(context
) -= 4;
598 struct ThunkDataSL
*td
= (struct ThunkDataSL
*)EDX_reg(context
);
599 DWORD targetNr
= CX_reg(context
) / 4;
600 struct SLTargetDB
*tdb
;
602 TRACE("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
603 (DWORD
)PROCESS_Current(), targetNr
, (DWORD
)td
);
605 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
606 if (tdb
->process
== PROCESS_Current())
611 TRACE("Loading 32-bit library %s\n", td
->pszDll32
);
612 LoadLibraryA(td
->pszDll32
);
614 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
615 if (tdb
->process
== PROCESS_Current())
621 EDX_reg(context
) = tdb
->targetTable
[targetNr
];
623 TRACE("Call target is %08lx\n", EDX_reg(context
));
627 WORD
*stack
= PTR_SEG_OFF_TO_LIN(SS_reg(context
), LOWORD(ESP_reg(context
)));
628 DX_reg(context
) = HIWORD(td
->apiDB
[targetNr
].errorReturnValue
);
629 AX_reg(context
) = LOWORD(td
->apiDB
[targetNr
].errorReturnValue
);
630 EIP_reg(context
) = stack
[2];
631 CS_reg(context
) = stack
[3];
632 ESP_reg(context
) += td
->apiDB
[targetNr
].nrArgBytes
+ 4;
634 ERR("Process %08lx did not ThunkConnect32 %s to %s\n",
635 (DWORD
)PROCESS_Current(), td
->pszDll32
, td
->pszDll16
);
642 /***********************************************************************
643 * 16<->32 Thunklet/Callback API:
646 #include "pshpack1.h"
647 typedef struct _THUNKLET
662 struct _THUNKLET
*next
;
666 #define THUNKLET_TYPE_LS 1
667 #define THUNKLET_TYPE_SL 2
669 static HANDLE ThunkletHeap
= 0;
670 static THUNKLET
*ThunkletAnchor
= NULL
;
672 static FARPROC ThunkletSysthunkGlueLS
= 0;
673 static SEGPTR ThunkletSysthunkGlueSL
= 0;
675 static FARPROC ThunkletCallbackGlueLS
= 0;
676 static SEGPTR ThunkletCallbackGlueSL
= 0;
678 /***********************************************************************
681 static BOOL
THUNK_ThunkletInit( void )
685 ThunkletHeap
= HeapCreate(HEAP_WINE_SEGPTR
| HEAP_WINE_CODE16SEG
, 0, 0);
686 if (!ThunkletHeap
) return FALSE
;
688 thunk
= HeapAlloc( ThunkletHeap
, 0, 5 );
689 if (!thunk
) return FALSE
;
691 ThunkletSysthunkGlueLS
= (FARPROC
)thunk
;
692 *thunk
++ = 0x58; /* popl eax */
693 *thunk
++ = 0xC3; /* ret */
695 ThunkletSysthunkGlueSL
= HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
696 *thunk
++ = 0x66; *thunk
++ = 0x58; /* popl eax */
697 *thunk
++ = 0xCB; /* lret */
702 /***********************************************************************
703 * SetThunkletCallbackGlue (KERNEL.560)
705 void WINAPI
SetThunkletCallbackGlue16( FARPROC glueLS
, SEGPTR glueSL
)
707 ThunkletCallbackGlueLS
= glueLS
;
708 ThunkletCallbackGlueSL
= glueSL
;
712 /***********************************************************************
715 THUNKLET
*THUNK_FindThunklet( DWORD target
, DWORD relay
,
716 DWORD glue
, BYTE type
)
720 for (thunk
= ThunkletAnchor
; thunk
; thunk
= thunk
->next
)
721 if ( thunk
->type
== type
722 && thunk
->target
== target
723 && thunk
->relay
== relay
724 && ( type
== THUNKLET_TYPE_LS
?
725 ( thunk
->glue
== glue
- (DWORD
)&thunk
->type
)
726 : ( thunk
->glue
== glue
) ) )
732 /***********************************************************************
733 * THUNK_AllocLSThunklet
735 FARPROC
THUNK_AllocLSThunklet( SEGPTR target
, DWORD relay
,
736 FARPROC glue
, HTASK16 owner
)
738 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
742 TDB
*pTask
= (TDB
*)GlobalLock16( owner
);
744 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
747 thunk
->prefix_target
= thunk
->prefix_relay
= 0x90;
748 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
749 thunk
->jmp_glue
= 0xE9;
751 thunk
->target
= (DWORD
)target
;
752 thunk
->relay
= (DWORD
)relay
;
753 thunk
->glue
= (DWORD
)glue
- (DWORD
)&thunk
->type
;
755 thunk
->type
= THUNKLET_TYPE_LS
;
756 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
758 thunk
->next
= ThunkletAnchor
;
759 ThunkletAnchor
= thunk
;
762 return (FARPROC
)thunk
;
765 /***********************************************************************
766 * THUNK_AllocSLThunklet
768 SEGPTR
THUNK_AllocSLThunklet( FARPROC target
, DWORD relay
,
769 SEGPTR glue
, HTASK16 owner
)
771 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
775 TDB
*pTask
= (TDB
*)GlobalLock16( owner
);
777 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
780 thunk
->prefix_target
= thunk
->prefix_relay
= 0x66;
781 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
782 thunk
->jmp_glue
= 0xEA;
784 thunk
->target
= (DWORD
)target
;
785 thunk
->relay
= (DWORD
)relay
;
786 thunk
->glue
= (DWORD
)glue
;
788 thunk
->type
= THUNKLET_TYPE_SL
;
789 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
791 thunk
->next
= ThunkletAnchor
;
792 ThunkletAnchor
= thunk
;
795 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
798 /**********************************************************************
801 BOOL16 WINAPI
IsLSThunklet( THUNKLET
*thunk
)
803 return thunk
->prefix_target
== 0x90 && thunk
->pushl_target
== 0x68
804 && thunk
->prefix_relay
== 0x90 && thunk
->pushl_relay
== 0x68
805 && thunk
->jmp_glue
== 0xE9 && thunk
->type
== THUNKLET_TYPE_LS
;
808 /**********************************************************************
809 * IsSLThunklet (KERNEL.612)
811 BOOL16 WINAPI
IsSLThunklet16( THUNKLET
*thunk
)
813 return thunk
->prefix_target
== 0x66 && thunk
->pushl_target
== 0x68
814 && thunk
->prefix_relay
== 0x66 && thunk
->pushl_relay
== 0x68
815 && thunk
->jmp_glue
== 0xEA && thunk
->type
== THUNKLET_TYPE_SL
;
820 /***********************************************************************
821 * AllocLSThunkletSysthunk (KERNEL.607)
823 FARPROC WINAPI
AllocLSThunkletSysthunk16( SEGPTR target
,
824 FARPROC relay
, DWORD dummy
)
826 return THUNK_AllocLSThunklet( (SEGPTR
)relay
, (DWORD
)target
,
827 ThunkletSysthunkGlueLS
, GetCurrentTask() );
830 /***********************************************************************
831 * AllocSLThunkletSysthunk (KERNEL.608)
833 SEGPTR WINAPI
AllocSLThunkletSysthunk16( FARPROC target
,
834 SEGPTR relay
, DWORD dummy
)
836 return THUNK_AllocSLThunklet( (FARPROC
)relay
, (DWORD
)target
,
837 ThunkletSysthunkGlueSL
, GetCurrentTask() );
841 /***********************************************************************
842 * AllocLSThunkletCallbackEx (KERNEL.567)
844 FARPROC WINAPI
AllocLSThunkletCallbackEx16( SEGPTR target
,
845 DWORD relay
, HTASK16 task
)
847 THUNKLET
*thunk
= (THUNKLET
*)PTR_SEG_TO_LIN( target
);
848 if ( !thunk
) return NULL
;
850 if ( IsSLThunklet16( thunk
) && thunk
->relay
== relay
851 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
852 return (FARPROC
)thunk
->target
;
854 return THUNK_AllocLSThunklet( target
, relay
,
855 ThunkletCallbackGlueLS
, task
);
858 /***********************************************************************
859 * AllocSLThunkletCallbackEx (KERNEL.568)
861 SEGPTR WINAPI
AllocSLThunkletCallbackEx16( FARPROC target
,
862 DWORD relay
, HTASK16 task
)
864 THUNKLET
*thunk
= (THUNKLET
*)target
;
865 if ( !thunk
) return 0;
867 if ( IsLSThunklet( thunk
) && thunk
->relay
== relay
868 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
869 return (SEGPTR
)thunk
->target
;
871 return THUNK_AllocSLThunklet( target
, relay
,
872 ThunkletCallbackGlueSL
, task
);
875 /***********************************************************************
876 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
878 FARPROC WINAPI
AllocLSThunkletCallback16( SEGPTR target
, DWORD relay
)
880 return AllocLSThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
883 /***********************************************************************
884 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
886 SEGPTR WINAPI
AllocSLThunkletCallback16( FARPROC target
, DWORD relay
)
888 return AllocSLThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
891 /***********************************************************************
892 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
894 FARPROC WINAPI
FindLSThunkletCallback( SEGPTR target
, DWORD relay
)
896 THUNKLET
*thunk
= (THUNKLET
*)PTR_SEG_TO_LIN( target
);
897 if ( thunk
&& IsSLThunklet16( thunk
) && thunk
->relay
== relay
898 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
899 return (FARPROC
)thunk
->target
;
901 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
902 (DWORD
)ThunkletCallbackGlueLS
,
904 return (FARPROC
)thunk
;
907 /***********************************************************************
908 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
910 SEGPTR WINAPI
FindSLThunkletCallback( FARPROC target
, DWORD relay
)
912 THUNKLET
*thunk
= (THUNKLET
*)target
;
913 if ( thunk
&& IsLSThunklet( thunk
) && thunk
->relay
== relay
914 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
915 return (SEGPTR
)thunk
->target
;
917 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
918 (DWORD
)ThunkletCallbackGlueSL
,
920 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
924 /***********************************************************************
925 * FreeThunklet16 (KERNEL.611)
927 BOOL16 WINAPI
FreeThunklet16( DWORD unused1
, DWORD unused2
)
932 /***********************************************************************
933 * Callback Client API
936 #define N_CBC_FIXED 20
937 #define N_CBC_VARIABLE 10
938 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
940 static SEGPTR CBClientRelay16
[ N_CBC_TOTAL
];
941 static FARPROC
*CBClientRelay32
[ N_CBC_TOTAL
];
943 /***********************************************************************
944 * RegisterCBClient (KERNEL.619)
946 INT16 WINAPI
RegisterCBClient16( INT16 wCBCId
,
947 SEGPTR relay16
, FARPROC
*relay32
)
949 /* Search for free Callback ID */
951 for ( wCBCId
= N_CBC_FIXED
; wCBCId
< N_CBC_TOTAL
; wCBCId
++ )
952 if ( !CBClientRelay16
[ wCBCId
] )
955 /* Register Callback ID */
956 if ( wCBCId
> 0 && wCBCId
< N_CBC_TOTAL
)
958 CBClientRelay16
[ wCBCId
] = relay16
;
959 CBClientRelay32
[ wCBCId
] = relay32
;
967 /***********************************************************************
968 * UnRegisterCBClient (KERNEL.622)
970 INT16 WINAPI
UnRegisterCBClient16( INT16 wCBCId
,
971 SEGPTR relay16
, FARPROC
*relay32
)
973 if ( wCBCId
>= N_CBC_FIXED
&& wCBCId
< N_CBC_TOTAL
974 && CBClientRelay16
[ wCBCId
] == relay16
975 && CBClientRelay32
[ wCBCId
] == relay32
)
977 CBClientRelay16
[ wCBCId
] = 0;
978 CBClientRelay32
[ wCBCId
] = 0;
987 /***********************************************************************
988 * InitCBClient (KERNEL.623)
990 void WINAPI
InitCBClient16( FARPROC glueLS
)
992 HMODULE16 kernel
= GetModuleHandle16( "KERNEL" );
993 SEGPTR glueSL
= (SEGPTR
)WIN32_GetProcAddress16( kernel
, (LPCSTR
)604 );
995 SetThunkletCallbackGlue16( glueLS
, glueSL
);
998 /***********************************************************************
999 * CBClientGlueSL (KERNEL.604)
1001 void WINAPI
CBClientGlueSL( CONTEXT86
*context
)
1003 /* Create stack frame */
1004 SEGPTR stackSeg
= stack16_push( 12 );
1005 LPWORD stackLin
= PTR_SEG_TO_LIN( stackSeg
);
1006 SEGPTR glue
, *glueTab
;
1008 stackLin
[3] = BP_reg( context
);
1009 stackLin
[2] = SI_reg( context
);
1010 stackLin
[1] = DI_reg( context
);
1011 stackLin
[0] = DS_reg( context
);
1013 EBP_reg( context
) = OFFSETOF( stackSeg
) + 6;
1014 ESP_reg( context
) = OFFSETOF( stackSeg
) - 4;
1015 GS_reg( context
) = 0;
1017 /* Jump to 16-bit relay code */
1018 glueTab
= PTR_SEG_TO_LIN( CBClientRelay16
[ stackLin
[5] ] );
1019 glue
= glueTab
[ stackLin
[4] ];
1020 CS_reg ( context
) = SELECTOROF( glue
);
1021 EIP_reg( context
) = OFFSETOF ( glue
);
1024 /***********************************************************************
1025 * CBClientThunkSL (KERNEL.620)
1027 extern DWORD
CALL32_CBClient( FARPROC proc
, LPWORD args
, DWORD
*esi
);
1028 void WINAPI
CBClientThunkSL( CONTEXT86
*context
)
1030 /* Call 32-bit relay code */
1032 LPWORD args
= PTR_SEG_OFF_TO_LIN( SS_reg( context
), BP_reg( context
) );
1033 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1035 EAX_reg(context
) = CALL32_CBClient( proc
, args
, &ESI_reg( context
) );
1038 /***********************************************************************
1039 * CBClientThunkSLEx (KERNEL.621)
1041 extern DWORD
CALL32_CBClientEx( FARPROC proc
, LPWORD args
, DWORD
*esi
, INT
*nArgs
);
1042 void WINAPI
CBClientThunkSLEx( CONTEXT86
*context
)
1044 /* Call 32-bit relay code */
1046 LPWORD args
= PTR_SEG_OFF_TO_LIN( SS_reg( context
), BP_reg( context
) );
1047 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1051 EAX_reg(context
) = CALL32_CBClientEx( proc
, args
, &ESI_reg( context
), &nArgs
);
1053 /* Restore registers saved by CBClientGlueSL */
1054 stackLin
= (LPWORD
)((LPBYTE
)CURRENT_STACK16
+ sizeof(STACK16FRAME
) - 4);
1055 BP_reg( context
) = stackLin
[3];
1056 SI_reg( context
) = stackLin
[2];
1057 DI_reg( context
) = stackLin
[1];
1058 DS_reg( context
) = stackLin
[0];
1059 ESP_reg( context
) += 16+nArgs
;
1061 /* Return to caller of CBClient thunklet */
1062 CS_reg ( context
) = stackLin
[9];
1063 EIP_reg( context
) = stackLin
[8];