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"
23 #include "debugtools.h"
25 DEFAULT_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 WORD CALLBACK
THUNK_CallTo16_word_w (FARPROC16
,WORD
);
34 extern WORD CALLBACK
THUNK_CallTo16_word_l (FARPROC16
,LONG
);
35 extern LONG CALLBACK
THUNK_CallTo16_long_l (FARPROC16
,LONG
);
36 extern WORD CALLBACK
THUNK_CallTo16_word_ww (FARPROC16
,WORD
,WORD
);
37 extern WORD CALLBACK
THUNK_CallTo16_word_wl (FARPROC16
,WORD
,LONG
);
38 extern WORD CALLBACK
THUNK_CallTo16_word_ll (FARPROC16
,LONG
,LONG
);
39 extern LONG CALLBACK
THUNK_CallTo16_long_ll (FARPROC16
,LONG
,LONG
);
40 extern WORD CALLBACK
THUNK_CallTo16_word_www (FARPROC16
,WORD
,WORD
,WORD
);
41 extern WORD CALLBACK
THUNK_CallTo16_word_wwl (FARPROC16
,WORD
,WORD
,LONG
);
42 extern WORD CALLBACK
THUNK_CallTo16_word_wlw (FARPROC16
,WORD
,LONG
,WORD
);
43 extern WORD CALLBACK
THUNK_CallTo16_word_lllw (FARPROC16
,LONG
,LONG
,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 /* ### stop build ### */
57 typedef struct tagTHUNK
59 BYTE popl_eax
; /* 0x58 popl %eax (return address)*/
60 BYTE pushl_func
; /* 0x68 pushl $proc */
61 FARPROC16 proc WINE_PACKED
;
62 BYTE pushl_eax
; /* 0x50 pushl %eax */
63 BYTE jmp
; /* 0xe9 jmp relay (relative jump)*/
64 RELAY relay WINE_PACKED
;
65 struct tagTHUNK
*next WINE_PACKED
;
69 #define CALLTO16_THUNK_MAGIC 0x54484e4b /* "THNK" */
73 #define DECL_THUNK(aname,aproc,arelay) \
75 aname.popl_eax = 0x58; \
76 aname.pushl_func = 0x68; \
77 aname.proc = (FARPROC) (aproc); \
78 aname.pushl_eax = 0x50; \
80 aname.relay = (RELAY)((char *)(arelay) - (char *)(&(aname).next)); \
82 aname.magic = CALLTO16_THUNK_MAGIC;
84 static THUNK
*firstThunk
= NULL
;
86 static BOOL
THUNK_ThunkletInit( void );
88 /* Callbacks function table for the emulator */
89 static const CALLBACKS_TABLE CALLBACK_EmulatorTable
=
91 (void *)CallTo16RegisterShort
, /* CallRegisterShortProc */
92 (void *)CallTo16RegisterLong
, /* CallRegisterLongProc */
93 (void *)THUNK_CallTo16_word_w
, /* CallWindowsExitProc */
94 (void *)THUNK_CallTo16_word_lwww
, /* CallWordBreakProc */
95 (void *)THUNK_CallTo16_word_ww
, /* CallBootAppProc */
96 (void *)THUNK_CallTo16_word_www
, /* CallLoadAppSegProc */
97 (void *)THUNK_CallTo16_word_www
, /* CallLocalNotifyFunc */
98 (void *)THUNK_CallTo16_word_www
, /* CallResourceHandlerProc */
99 (void *)THUNK_CallTo16_long_ll
, /* CallUTProc */
100 (void *)THUNK_CallTo16_long_l
/* CallASPIPostProc */
103 const CALLBACKS_TABLE
*Callbacks
= &CALLBACK_EmulatorTable
;
105 CALLOUT_TABLE Callout
= { 0 };
108 /***********************************************************************
111 BOOL
THUNK_Init(void)
113 /* Initialize Thunklets */
114 return THUNK_ThunkletInit();
117 /***********************************************************************
120 FARPROC
THUNK_Alloc( FARPROC16 func
, RELAY relay
)
126 /* NULL maps to NULL */
127 if ( !func
) return NULL
;
130 * If we got an 16-bit built-in API entry point, retrieve the Wine
131 * 32-bit handler for that API routine.
133 * NOTE: For efficiency reasons, we only check whether the selector
134 * of 'func' points to the code segment of a built-in module.
135 * It might be theoretically possible that the offset is such
136 * that 'func' does not point, in fact, to an API entry point.
137 * In this case, however, the pointer is corrupt anyway.
139 hSeg
= GlobalHandle16( SELECTOROF( func
) );
140 pModule
= NE_GetPtr( FarGetOwner16( hSeg
) );
142 if ( pModule
&& (pModule
->flags
& NE_FFLAGS_BUILTIN
)
143 && NE_SEG_TABLE(pModule
)[0].hSeg
== hSeg
)
145 FARPROC proc
= (FARPROC
)((ENTRYPOINT16
*)PTR_SEG_TO_LIN( func
))->target
;
147 TRACE( "(%04x:%04x, %p) -> built-in API %p\n",
148 SELECTOROF( func
), OFFSETOF( func
), relay
, proc
);
152 /* Otherwise, we need to alloc a thunk */
153 thunk
= HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk
) );
156 thunk
->popl_eax
= 0x58;
157 thunk
->pushl_func
= 0x68;
159 thunk
->pushl_eax
= 0x50;
161 thunk
->relay
= (RELAY
)((char *)relay
- (char *)(&thunk
->next
));
162 thunk
->magic
= CALLTO16_THUNK_MAGIC
;
163 thunk
->next
= firstThunk
;
167 TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
168 SELECTOROF( func
), OFFSETOF( func
), relay
, thunk
);
169 return (FARPROC
)thunk
;
172 /***********************************************************************
175 void THUNK_Free( FARPROC thunk
)
177 THUNK
*t
= (THUNK
*)thunk
;
178 if ( !t
|| IsBadReadPtr( t
, sizeof(*t
) )
179 || t
->magic
!= CALLTO16_THUNK_MAGIC
)
182 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, t
))
184 THUNK
**prev
= &firstThunk
;
185 while (*prev
&& (*prev
!= t
)) prev
= &(*prev
)->next
;
189 HeapFree( GetProcessHeap(), 0, t
);
193 ERR("invalid thunk addr %p\n", thunk
);
198 /***********************************************************************
199 * THUNK_EnumObjects16 (GDI.71)
201 INT16 WINAPI
THUNK_EnumObjects16( HDC16 hdc
, INT16 nObjType
,
202 GOBJENUMPROC16 func
, LPARAM lParam
)
204 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_ll
);
205 return EnumObjects16( hdc
, nObjType
, (GOBJENUMPROC16
)&thunk
, lParam
);
209 /*************************************************************************
210 * THUNK_EnumFonts16 (GDI.70)
212 INT16 WINAPI
THUNK_EnumFonts16( HDC16 hdc
, LPCSTR lpFaceName
,
213 FONTENUMPROC16 func
, LPARAM lParam
)
215 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
216 return EnumFonts16( hdc
, lpFaceName
, (FONTENUMPROC16
)&thunk
, lParam
);
219 /******************************************************************
220 * THUNK_EnumMetaFile16 (GDI.175)
222 BOOL16 WINAPI
THUNK_EnumMetaFile16( HDC16 hdc
, HMETAFILE16 hmf
,
223 MFENUMPROC16 func
, LPARAM lParam
)
225 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wllwl
);
226 return EnumMetaFile16( hdc
, hmf
, (MFENUMPROC16
)&thunk
, lParam
);
230 /*************************************************************************
231 * THUNK_EnumFontFamilies16 (GDI.330)
233 INT16 WINAPI
THUNK_EnumFontFamilies16( HDC16 hdc
, LPCSTR lpszFamily
,
234 FONTENUMPROC16 func
, LPARAM lParam
)
236 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
237 return EnumFontFamilies16(hdc
, lpszFamily
, (FONTENUMPROC16
)&thunk
, lParam
);
241 /*************************************************************************
242 * THUNK_EnumFontFamiliesEx16 (GDI.613)
244 INT16 WINAPI
THUNK_EnumFontFamiliesEx16( HDC16 hdc
, LPLOGFONT16 lpLF
,
245 FONTENUMPROCEX16 func
, LPARAM lParam
,
248 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_llwl
);
249 return EnumFontFamiliesEx16( hdc
, lpLF
, (FONTENUMPROCEX16
)&thunk
,
254 /**********************************************************************
255 * THUNK_LineDDA16 (GDI.100)
257 void WINAPI
THUNK_LineDDA16( INT16 nXStart
, INT16 nYStart
, INT16 nXEnd
,
258 INT16 nYEnd
, LINEDDAPROC16 func
, LPARAM lParam
)
260 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wwl
);
261 LineDDA16( nXStart
, nYStart
, nXEnd
, nYEnd
, (LINEDDAPROC16
)&thunk
, lParam
);
265 /*******************************************************************
266 * THUNK_EnumWindows16 (USER.54)
268 BOOL16 WINAPI
THUNK_EnumWindows16( WNDENUMPROC16 func
, LPARAM lParam
)
270 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
271 return EnumWindows16( (WNDENUMPROC16
)&thunk
, lParam
);
275 /**********************************************************************
276 * THUNK_EnumChildWindows16 (USER.55)
278 BOOL16 WINAPI
THUNK_EnumChildWindows16( HWND16 parent
, WNDENUMPROC16 func
,
281 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
282 return EnumChildWindows16( parent
, (WNDENUMPROC16
)&thunk
, lParam
);
286 /**********************************************************************
287 * THUNK_EnumTaskWindows16 (USER.225)
289 BOOL16 WINAPI
THUNK_EnumTaskWindows16( HTASK16 hTask
, WNDENUMPROC16 func
,
292 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wl
);
293 return EnumTaskWindows16( hTask
, (WNDENUMPROC16
)&thunk
, lParam
);
297 /***********************************************************************
298 * THUNK_EnumProps16 (USER.27)
300 INT16 WINAPI
THUNK_EnumProps16( HWND16 hwnd
, PROPENUMPROC16 func
)
302 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wlw
);
303 return EnumProps16( hwnd
, (PROPENUMPROC16
)&thunk
);
307 /***********************************************************************
308 * THUNK_GrayString16 (USER.185)
310 BOOL16 WINAPI
THUNK_GrayString16( HDC16 hdc
, HBRUSH16 hbr
,
311 GRAYSTRINGPROC16 func
, LPARAM lParam
,
312 INT16 cch
, INT16 x
, INT16 y
,
315 DECL_THUNK( thunk
, func
, THUNK_CallTo16_word_wlw
);
317 return GrayString16( hdc
, hbr
, NULL
, lParam
, cch
, x
, y
, cx
, cy
);
319 return GrayString16( hdc
, hbr
, (GRAYSTRINGPROC16
)&thunk
, lParam
, cch
,
324 /***********************************************************************
325 * THUNK_GetCalloutThunk
327 * Retrieve API entry point with given name from given module.
328 * If module is builtin, return the 32-bit entry point, otherwise
329 * create a 32->16 thunk to the 16-bit entry point, using the
333 static FARPROC
THUNK_GetCalloutThunk( NE_MODULE
*pModule
, LPSTR name
, RELAY relay
)
335 FARPROC16 proc
= WIN32_GetProcAddress16( pModule
->self
, name
);
336 if ( !proc
) return 0;
338 if ( pModule
->flags
& NE_FFLAGS_BUILTIN
)
339 return (FARPROC
)((ENTRYPOINT16
*)PTR_SEG_TO_LIN( proc
))->target
;
341 return (FARPROC
)THUNK_Alloc( proc
, relay
);
344 /***********************************************************************
347 void THUNK_InitCallout(void)
352 hModule
= GetModuleHandleA( "USER32" );
355 #define GETADDR( var, name ) \
356 *(FARPROC *)&Callout.##var = GetProcAddress( hModule, name )
358 GETADDR( PeekMessageA
, "PeekMessageA" );
359 GETADDR( PeekMessageW
, "PeekMessageW" );
360 GETADDR( GetMessageA
, "GetMessageA" );
361 GETADDR( GetMessageW
, "GetMessageW" );
362 GETADDR( SendMessageA
, "SendMessageA" );
363 GETADDR( SendMessageW
, "SendMessageW" );
364 GETADDR( PostMessageA
, "PostMessageA" );
365 GETADDR( PostMessageW
, "PostMessageW" );
366 GETADDR( PostThreadMessageA
, "PostThreadMessageA" );
367 GETADDR( PostThreadMessageW
, "PostThreadMessageW" );
368 GETADDR( TranslateMessage
, "TranslateMessage" );
369 GETADDR( DispatchMessageW
, "DispatchMessageW" );
370 GETADDR( DispatchMessageA
, "DispatchMessageA" );
371 GETADDR( RedrawWindow
, "RedrawWindow" );
372 GETADDR( WaitForInputIdle
, "WaitForInputIdle" );
377 pModule
= NE_GetPtr( GetModuleHandle16( "USER" ) );
380 #define GETADDR( var, name, thk ) \
381 *(FARPROC *)&Callout.##var = THUNK_GetCalloutThunk( pModule, name, \
382 (RELAY)THUNK_CallTo16_##thk )
384 GETADDR( PeekMessage16
, "PeekMessage", word_lwwww
);
385 GETADDR( GetMessage16
, "GetMessage", word_lwww
);
386 GETADDR( SendMessage16
, "SendMessage", long_wwwl
);
387 GETADDR( PostMessage16
, "PostMessage", word_wwwl
);
388 GETADDR( PostAppMessage16
, "PostAppMessage", word_wwwl
);
389 GETADDR( TranslateMessage16
, "TranslateMessage", word_l
);
390 GETADDR( DispatchMessage16
, "DispatchMessage", long_l
);
391 GETADDR( RedrawWindow16
, "RedrawWindow", word_wlww
);
392 GETADDR( FinalUserInit16
, "FinalUserInit", word_
);
393 GETADDR( InitApp16
, "InitApp", word_w
);
394 GETADDR( InitThreadInput16
, "InitThreadInput", word_ww
);
395 GETADDR( UserYield16
, "UserYield", word_
);
396 GETADDR( DestroyIcon32
, "DestroyIcon32", word_ww
);
397 GETADDR( UserSignalProc
, "SignalProc32", word_lllw
);
403 /***********************************************************************
404 * 16->32 Flat Thunk routines:
407 /***********************************************************************
408 * ThunkConnect16 (KERNEL.651)
409 * Connects a 32bit and a 16bit thunkbuffer.
411 UINT WINAPI
ThunkConnect16(
412 LPSTR module16
, /* [in] name of win16 dll */
413 LPSTR module32
, /* [in] name of win32 dll */
414 HINSTANCE16 hInst16
, /* [in] hInst of win16 dll */
415 DWORD dwReason
, /* [in] initialisation argument */
416 struct ThunkDataCommon
*TD
, /* [in/out] thunkbuffer */
417 LPSTR thunkfun32
, /* [in] win32 thunkfunction */
418 WORD cs
/* [in] CS of win16 dll */
422 if (!strncmp(TD
->magic
, "SL01", 4))
426 TRACE("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
427 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
429 else if (!strncmp(TD
->magic
, "LS01", 4))
433 TRACE("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
434 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
438 ERR("Invalid magic %c%c%c%c\n",
439 TD
->magic
[0], TD
->magic
[1], TD
->magic
[2], TD
->magic
[3]);
445 case DLL_PROCESS_ATTACH
:
448 struct ThunkDataSL16
*SL16
= (struct ThunkDataSL16
*)TD
;
449 struct ThunkDataSL
*SL
= SL16
->fpData
;
453 SL
= HeapAlloc(GetProcessHeap(), 0, sizeof(*SL
));
455 SL
->common
= SL16
->common
;
456 SL
->flags1
= SL16
->flags1
;
457 SL
->flags2
= SL16
->flags2
;
459 SL
->apiDB
= PTR_SEG_TO_LIN(SL16
->apiDatabase
);
462 lstrcpynA(SL
->pszDll16
, module16
, 255);
463 lstrcpynA(SL
->pszDll32
, module32
, 255);
465 /* We should create a SEGPTR to the ThunkDataSL,
466 but since the contents are not in the original format,
467 any access to this by 16-bit code would crash anyway. */
473 if (SL
->flags2
& 0x80000000)
475 TRACE("Preloading 32-bit library\n");
476 LoadLibraryA(module32
);
485 case DLL_PROCESS_DETACH
:
494 /***********************************************************************
495 * C16ThkSL (KERNEL.630)
498 void WINAPI
C16ThkSL(CONTEXT86
*context
)
500 LPBYTE stub
= PTR_SEG_TO_LIN(EAX_reg(context
)), x
= stub
;
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);
559 ERR("ThunkConnect16 was not called!\n");
563 TRACE("Creating stub for ThunkDataSL %08lx\n", (DWORD
)td
);
566 /* We produce the following code:
575 * call __FLATCS:CallFrom16Thunk
578 *x
++ = 0x66; *x
++ = 0x33; *x
++ = 0xC0;
579 *x
++ = 0x66; *x
++ = 0xBA; *((DWORD
*)x
)++ = (DWORD
)td
;
580 *x
++ = 0x9A; *((DWORD
*)x
)++ = procAddress
;
583 *x
++ = 0x66; *x
++ = 0x52;
585 *x
++ = 0x66; *x
++ = 0x52;
586 *x
++ = 0x66; *x
++ = 0x9A; *((DWORD
*)x
)++ = (DWORD
)CallFrom16Thunk
;
589 /* Jump to the stub code just created */
590 EIP_reg(context
) = LOWORD(EAX_reg(context
));
591 CS_reg(context
) = HIWORD(EAX_reg(context
));
593 /* Since C16ThkSL01 got called by a jmp, we need to leave the
594 orginal return address on the stack */
595 ESP_reg(context
) -= 4;
599 struct ThunkDataSL
*td
= (struct ThunkDataSL
*)EDX_reg(context
);
600 DWORD targetNr
= CX_reg(context
) / 4;
601 struct SLTargetDB
*tdb
;
603 TRACE("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
604 (DWORD
)PROCESS_Current(), targetNr
, (DWORD
)td
);
606 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
607 if (tdb
->process
== PROCESS_Current())
612 TRACE("Loading 32-bit library %s\n", td
->pszDll32
);
613 LoadLibraryA(td
->pszDll32
);
615 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
616 if (tdb
->process
== PROCESS_Current())
622 EDX_reg(context
) = tdb
->targetTable
[targetNr
];
624 TRACE("Call target is %08lx\n", EDX_reg(context
));
628 WORD
*stack
= PTR_SEG_OFF_TO_LIN(SS_reg(context
), LOWORD(ESP_reg(context
)));
629 DX_reg(context
) = HIWORD(td
->apiDB
[targetNr
].errorReturnValue
);
630 AX_reg(context
) = LOWORD(td
->apiDB
[targetNr
].errorReturnValue
);
631 EIP_reg(context
) = stack
[2];
632 CS_reg(context
) = stack
[3];
633 ESP_reg(context
) += td
->apiDB
[targetNr
].nrArgBytes
+ 4;
635 ERR("Process %08lx did not ThunkConnect32 %s to %s\n",
636 (DWORD
)PROCESS_Current(), td
->pszDll32
, td
->pszDll16
);
643 /***********************************************************************
644 * 16<->32 Thunklet/Callback API:
647 #include "pshpack1.h"
648 typedef struct _THUNKLET
663 struct _THUNKLET
*next
;
667 #define THUNKLET_TYPE_LS 1
668 #define THUNKLET_TYPE_SL 2
670 static HANDLE ThunkletHeap
= 0;
671 static THUNKLET
*ThunkletAnchor
= NULL
;
673 static FARPROC ThunkletSysthunkGlueLS
= 0;
674 static SEGPTR ThunkletSysthunkGlueSL
= 0;
676 static FARPROC ThunkletCallbackGlueLS
= 0;
677 static SEGPTR ThunkletCallbackGlueSL
= 0;
679 /***********************************************************************
682 static BOOL
THUNK_ThunkletInit( void )
686 ThunkletHeap
= HeapCreate(HEAP_WINE_SEGPTR
| HEAP_WINE_CODE16SEG
, 0, 0);
687 if (!ThunkletHeap
) return FALSE
;
689 thunk
= HeapAlloc( ThunkletHeap
, 0, 5 );
690 if (!thunk
) return FALSE
;
692 ThunkletSysthunkGlueLS
= (FARPROC
)thunk
;
693 *thunk
++ = 0x58; /* popl eax */
694 *thunk
++ = 0xC3; /* ret */
696 ThunkletSysthunkGlueSL
= HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
697 *thunk
++ = 0x66; *thunk
++ = 0x58; /* popl eax */
698 *thunk
++ = 0xCB; /* lret */
703 /***********************************************************************
704 * SetThunkletCallbackGlue (KERNEL.560)
706 void WINAPI
SetThunkletCallbackGlue16( FARPROC glueLS
, SEGPTR glueSL
)
708 ThunkletCallbackGlueLS
= glueLS
;
709 ThunkletCallbackGlueSL
= glueSL
;
713 /***********************************************************************
716 THUNKLET
*THUNK_FindThunklet( DWORD target
, DWORD relay
,
717 DWORD glue
, BYTE type
)
721 for (thunk
= ThunkletAnchor
; thunk
; thunk
= thunk
->next
)
722 if ( thunk
->type
== type
723 && thunk
->target
== target
724 && thunk
->relay
== relay
725 && ( type
== THUNKLET_TYPE_LS
?
726 ( thunk
->glue
== glue
- (DWORD
)&thunk
->type
)
727 : ( thunk
->glue
== glue
) ) )
733 /***********************************************************************
734 * THUNK_AllocLSThunklet
736 FARPROC
THUNK_AllocLSThunklet( SEGPTR target
, DWORD relay
,
737 FARPROC glue
, HTASK16 owner
)
739 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
743 TDB
*pTask
= (TDB
*)GlobalLock16( owner
);
745 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
748 thunk
->prefix_target
= thunk
->prefix_relay
= 0x90;
749 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
750 thunk
->jmp_glue
= 0xE9;
752 thunk
->target
= (DWORD
)target
;
753 thunk
->relay
= (DWORD
)relay
;
754 thunk
->glue
= (DWORD
)glue
- (DWORD
)&thunk
->type
;
756 thunk
->type
= THUNKLET_TYPE_LS
;
757 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
759 thunk
->next
= ThunkletAnchor
;
760 ThunkletAnchor
= thunk
;
763 return (FARPROC
)thunk
;
766 /***********************************************************************
767 * THUNK_AllocSLThunklet
769 SEGPTR
THUNK_AllocSLThunklet( FARPROC target
, DWORD relay
,
770 SEGPTR glue
, HTASK16 owner
)
772 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
776 TDB
*pTask
= (TDB
*)GlobalLock16( owner
);
778 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
781 thunk
->prefix_target
= thunk
->prefix_relay
= 0x66;
782 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
783 thunk
->jmp_glue
= 0xEA;
785 thunk
->target
= (DWORD
)target
;
786 thunk
->relay
= (DWORD
)relay
;
787 thunk
->glue
= (DWORD
)glue
;
789 thunk
->type
= THUNKLET_TYPE_SL
;
790 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
792 thunk
->next
= ThunkletAnchor
;
793 ThunkletAnchor
= thunk
;
796 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
799 /**********************************************************************
802 BOOL16 WINAPI
IsLSThunklet( THUNKLET
*thunk
)
804 return thunk
->prefix_target
== 0x90 && thunk
->pushl_target
== 0x68
805 && thunk
->prefix_relay
== 0x90 && thunk
->pushl_relay
== 0x68
806 && thunk
->jmp_glue
== 0xE9 && thunk
->type
== THUNKLET_TYPE_LS
;
809 /**********************************************************************
810 * IsSLThunklet (KERNEL.612)
812 BOOL16 WINAPI
IsSLThunklet16( THUNKLET
*thunk
)
814 return thunk
->prefix_target
== 0x66 && thunk
->pushl_target
== 0x68
815 && thunk
->prefix_relay
== 0x66 && thunk
->pushl_relay
== 0x68
816 && thunk
->jmp_glue
== 0xEA && thunk
->type
== THUNKLET_TYPE_SL
;
821 /***********************************************************************
822 * AllocLSThunkletSysthunk (KERNEL.607)
824 FARPROC WINAPI
AllocLSThunkletSysthunk16( SEGPTR target
,
825 FARPROC relay
, DWORD dummy
)
827 return THUNK_AllocLSThunklet( (SEGPTR
)relay
, (DWORD
)target
,
828 ThunkletSysthunkGlueLS
, GetCurrentTask() );
831 /***********************************************************************
832 * AllocSLThunkletSysthunk (KERNEL.608)
834 SEGPTR WINAPI
AllocSLThunkletSysthunk16( FARPROC target
,
835 SEGPTR relay
, DWORD dummy
)
837 return THUNK_AllocSLThunklet( (FARPROC
)relay
, (DWORD
)target
,
838 ThunkletSysthunkGlueSL
, GetCurrentTask() );
842 /***********************************************************************
843 * AllocLSThunkletCallbackEx (KERNEL.567)
845 FARPROC WINAPI
AllocLSThunkletCallbackEx16( SEGPTR target
,
846 DWORD relay
, HTASK16 task
)
848 THUNKLET
*thunk
= (THUNKLET
*)PTR_SEG_TO_LIN( target
);
849 if ( !thunk
) return NULL
;
851 if ( IsSLThunklet16( thunk
) && thunk
->relay
== relay
852 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
853 return (FARPROC
)thunk
->target
;
855 return THUNK_AllocLSThunklet( target
, relay
,
856 ThunkletCallbackGlueLS
, task
);
859 /***********************************************************************
860 * AllocSLThunkletCallbackEx (KERNEL.568)
862 SEGPTR WINAPI
AllocSLThunkletCallbackEx16( FARPROC target
,
863 DWORD relay
, HTASK16 task
)
865 THUNKLET
*thunk
= (THUNKLET
*)target
;
866 if ( !thunk
) return 0;
868 if ( IsLSThunklet( thunk
) && thunk
->relay
== relay
869 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
870 return (SEGPTR
)thunk
->target
;
872 return THUNK_AllocSLThunklet( target
, relay
,
873 ThunkletCallbackGlueSL
, task
);
876 /***********************************************************************
877 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
879 FARPROC WINAPI
AllocLSThunkletCallback16( SEGPTR target
, DWORD relay
)
881 return AllocLSThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
884 /***********************************************************************
885 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
887 SEGPTR WINAPI
AllocSLThunkletCallback16( FARPROC target
, DWORD relay
)
889 return AllocSLThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
892 /***********************************************************************
893 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
895 FARPROC WINAPI
FindLSThunkletCallback( SEGPTR target
, DWORD relay
)
897 THUNKLET
*thunk
= (THUNKLET
*)PTR_SEG_TO_LIN( target
);
898 if ( thunk
&& IsSLThunklet16( thunk
) && thunk
->relay
== relay
899 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
900 return (FARPROC
)thunk
->target
;
902 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
903 (DWORD
)ThunkletCallbackGlueLS
,
905 return (FARPROC
)thunk
;
908 /***********************************************************************
909 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
911 SEGPTR WINAPI
FindSLThunkletCallback( FARPROC target
, DWORD relay
)
913 THUNKLET
*thunk
= (THUNKLET
*)target
;
914 if ( thunk
&& IsLSThunklet( thunk
) && thunk
->relay
== relay
915 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
916 return (SEGPTR
)thunk
->target
;
918 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
919 (DWORD
)ThunkletCallbackGlueSL
,
921 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
925 /***********************************************************************
926 * FreeThunklet16 (KERNEL.611)
928 BOOL16 WINAPI
FreeThunklet16( DWORD unused1
, DWORD unused2
)
933 /***********************************************************************
934 * Callback Client API
937 #define N_CBC_FIXED 20
938 #define N_CBC_VARIABLE 10
939 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
941 static SEGPTR CBClientRelay16
[ N_CBC_TOTAL
];
942 static FARPROC
*CBClientRelay32
[ N_CBC_TOTAL
];
944 /***********************************************************************
945 * RegisterCBClient (KERNEL.619)
947 INT16 WINAPI
RegisterCBClient16( INT16 wCBCId
,
948 SEGPTR relay16
, FARPROC
*relay32
)
950 /* Search for free Callback ID */
952 for ( wCBCId
= N_CBC_FIXED
; wCBCId
< N_CBC_TOTAL
; wCBCId
++ )
953 if ( !CBClientRelay16
[ wCBCId
] )
956 /* Register Callback ID */
957 if ( wCBCId
> 0 && wCBCId
< N_CBC_TOTAL
)
959 CBClientRelay16
[ wCBCId
] = relay16
;
960 CBClientRelay32
[ wCBCId
] = relay32
;
968 /***********************************************************************
969 * UnRegisterCBClient (KERNEL.622)
971 INT16 WINAPI
UnRegisterCBClient16( INT16 wCBCId
,
972 SEGPTR relay16
, FARPROC
*relay32
)
974 if ( wCBCId
>= N_CBC_FIXED
&& wCBCId
< N_CBC_TOTAL
975 && CBClientRelay16
[ wCBCId
] == relay16
976 && CBClientRelay32
[ wCBCId
] == relay32
)
978 CBClientRelay16
[ wCBCId
] = 0;
979 CBClientRelay32
[ wCBCId
] = 0;
988 /***********************************************************************
989 * InitCBClient (KERNEL.623)
991 void WINAPI
InitCBClient16( FARPROC glueLS
)
993 HMODULE16 kernel
= GetModuleHandle16( "KERNEL" );
994 SEGPTR glueSL
= (SEGPTR
)WIN32_GetProcAddress16( kernel
, (LPCSTR
)604 );
996 SetThunkletCallbackGlue16( glueLS
, glueSL
);
999 /***********************************************************************
1000 * CBClientGlueSL (KERNEL.604)
1002 void WINAPI
CBClientGlueSL( CONTEXT86
*context
)
1004 /* Create stack frame */
1005 SEGPTR stackSeg
= stack16_push( 12 );
1006 LPWORD stackLin
= PTR_SEG_TO_LIN( stackSeg
);
1007 SEGPTR glue
, *glueTab
;
1009 stackLin
[3] = BP_reg( context
);
1010 stackLin
[2] = SI_reg( context
);
1011 stackLin
[1] = DI_reg( context
);
1012 stackLin
[0] = DS_reg( context
);
1014 EBP_reg( context
) = OFFSETOF( stackSeg
) + 6;
1015 ESP_reg( context
) = OFFSETOF( stackSeg
) - 4;
1016 GS_reg( context
) = 0;
1018 /* Jump to 16-bit relay code */
1019 glueTab
= PTR_SEG_TO_LIN( CBClientRelay16
[ stackLin
[5] ] );
1020 glue
= glueTab
[ stackLin
[4] ];
1021 CS_reg ( context
) = SELECTOROF( glue
);
1022 EIP_reg( context
) = OFFSETOF ( glue
);
1025 /***********************************************************************
1026 * CBClientThunkSL (KERNEL.620)
1028 extern DWORD
CALL32_CBClient( FARPROC proc
, LPWORD args
, DWORD
*esi
);
1029 void WINAPI
CBClientThunkSL( CONTEXT86
*context
)
1031 /* Call 32-bit relay code */
1033 LPWORD args
= PTR_SEG_OFF_TO_LIN( SS_reg( context
), BP_reg( context
) );
1034 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1036 EAX_reg(context
) = CALL32_CBClient( proc
, args
, &ESI_reg( context
) );
1039 /***********************************************************************
1040 * CBClientThunkSLEx (KERNEL.621)
1042 extern DWORD
CALL32_CBClientEx( FARPROC proc
, LPWORD args
, DWORD
*esi
, INT
*nArgs
);
1043 void WINAPI
CBClientThunkSLEx( CONTEXT86
*context
)
1045 /* Call 32-bit relay code */
1047 LPWORD args
= PTR_SEG_OFF_TO_LIN( SS_reg( context
), BP_reg( context
) );
1048 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1052 EAX_reg(context
) = CALL32_CBClientEx( proc
, args
, &ESI_reg( context
), &nArgs
);
1054 /* Restore registers saved by CBClientGlueSL */
1055 stackLin
= (LPWORD
)((LPBYTE
)CURRENT_STACK16
+ sizeof(STACK16FRAME
) - 4);
1056 BP_reg( context
) = stackLin
[3];
1057 SI_reg( context
) = stackLin
[2];
1058 DI_reg( context
) = stackLin
[1];
1059 DS_reg( context
) = stackLin
[0];
1060 ESP_reg( context
) += 16+nArgs
;
1062 /* Return to caller of CBClient thunklet */
1063 CS_reg ( context
) = stackLin
[9];
1064 EIP_reg( context
) = stackLin
[8];