Pass the main exe name in the CREATE_PROCESS debug event.
[wine.git] / if1632 / thunk.c
blob57c1928f713eba0d54ab788458b41f8ddfa9a3ba
1 /*
2 * Emulator thunks
4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
6 */
8 #include <string.h>
9 #include "wine/winbase16.h"
10 #include "task.h"
11 #include "hook.h"
12 #include "callback.h"
13 #include "builtin16.h"
14 #include "user.h"
15 #include "heap.h"
16 #include "neexe.h"
17 #include "process.h"
18 #include "stackframe.h"
19 #include "win.h"
20 #include "flatthunk.h"
21 #include "mouse.h"
22 #include "selectors.h"
23 #include "keyboard.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 LONG CALLBACK THUNK_CallTo16_long_ll (FARPROC16,LONG,LONG);
39 extern WORD CALLBACK THUNK_CallTo16_word_www (FARPROC16,WORD,WORD,WORD);
40 extern WORD CALLBACK THUNK_CallTo16_word_lllw (FARPROC16,LONG,LONG,LONG,WORD);
41 extern WORD CALLBACK THUNK_CallTo16_word_lwww (FARPROC16,LONG,WORD,WORD,WORD);
42 extern WORD CALLBACK THUNK_CallTo16_word_wlww (FARPROC16,WORD,LONG,WORD,WORD);
43 extern WORD CALLBACK THUNK_CallTo16_word_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
44 extern LONG CALLBACK THUNK_CallTo16_long_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
45 extern WORD CALLBACK THUNK_CallTo16_word_lwwww(FARPROC16,LONG,WORD,WORD,WORD,WORD);
46 /* ### stop build ### */
48 static THUNK *firstThunk = NULL;
50 static BOOL THUNK_ThunkletInit( void );
52 /* Callbacks function table for the emulator */
53 static const CALLBACKS_TABLE CALLBACK_EmulatorTable =
55 (void *)CallTo16RegisterShort, /* CallRegisterShortProc */
56 (void *)CallTo16RegisterLong, /* CallRegisterLongProc */
57 (void *)THUNK_CallTo16_word_w, /* CallWindowsExitProc */
58 (void *)THUNK_CallTo16_word_lwww, /* CallWordBreakProc */
59 (void *)THUNK_CallTo16_word_ww, /* CallBootAppProc */
60 (void *)THUNK_CallTo16_word_www, /* CallLoadAppSegProc */
61 (void *)THUNK_CallTo16_word_www, /* CallLocalNotifyFunc */
62 (void *)THUNK_CallTo16_word_www, /* CallResourceHandlerProc */
63 (void *)THUNK_CallTo16_long_ll, /* CallUTProc */
64 (void *)THUNK_CallTo16_long_l /* CallASPIPostProc */
67 const CALLBACKS_TABLE *Callbacks = &CALLBACK_EmulatorTable;
69 CALLOUT_TABLE Callout = { 0 };
72 /***********************************************************************
73 * THUNK_Init
75 BOOL THUNK_Init(void)
77 /* Initialize Thunklets */
78 return THUNK_ThunkletInit();
81 /***********************************************************************
82 * THUNK_Alloc
84 FARPROC THUNK_Alloc( FARPROC16 func, RELAY relay )
86 HANDLE16 hSeg;
87 NE_MODULE *pModule;
88 THUNK *thunk;
90 /* NULL maps to NULL */
91 if ( !func ) return NULL;
93 /*
94 * If we got an 16-bit built-in API entry point, retrieve the Wine
95 * 32-bit handler for that API routine.
97 * NOTE: For efficiency reasons, we only check whether the selector
98 * of 'func' points to the code segment of a built-in module.
99 * It might be theoretically possible that the offset is such
100 * that 'func' does not point, in fact, to an API entry point.
101 * In this case, however, the pointer is corrupt anyway.
103 hSeg = GlobalHandle16( SELECTOROF( func ) );
104 pModule = NE_GetPtr( FarGetOwner16( hSeg ) );
106 if ( pModule && (pModule->flags & NE_FFLAGS_BUILTIN)
107 && NE_SEG_TABLE(pModule)[0].hSeg == hSeg )
109 FARPROC proc = (FARPROC)((ENTRYPOINT16 *)PTR_SEG_TO_LIN( func ))->target;
111 TRACE( "(%04x:%04x, %p) -> built-in API %p\n",
112 SELECTOROF( func ), OFFSETOF( func ), relay, proc );
113 return proc;
116 /* Otherwise, we need to alloc a thunk */
117 thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
118 if (thunk)
120 thunk->popl_eax = 0x58;
121 thunk->pushl_func = 0x68;
122 thunk->proc = func;
123 thunk->pushl_eax = 0x50;
124 thunk->jmp = 0xe9;
125 thunk->relay = (RELAY)((char *)relay - (char *)(&thunk->next));
126 thunk->magic = CALLTO16_THUNK_MAGIC;
127 thunk->next = firstThunk;
128 firstThunk = thunk;
131 TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
132 SELECTOROF( func ), OFFSETOF( func ), relay, thunk );
133 return (FARPROC)thunk;
136 /***********************************************************************
137 * THUNK_Free
139 void THUNK_Free( FARPROC thunk )
141 THUNK *t = (THUNK*)thunk;
142 if ( !t || IsBadReadPtr( t, sizeof(*t) )
143 || t->magic != CALLTO16_THUNK_MAGIC )
144 return;
146 if (HEAP_IsInsideHeap( GetProcessHeap(), 0, t ))
148 THUNK **prev = &firstThunk;
149 while (*prev && (*prev != t)) prev = &(*prev)->next;
150 if (*prev)
152 *prev = t->next;
153 HeapFree( GetProcessHeap(), 0, t );
154 return;
157 ERR("invalid thunk addr %p\n", thunk );
158 return;
162 /***********************************************************************
163 * THUNK_GetCalloutThunk
165 * Retrieve API entry point with given name from given module.
166 * If module is builtin, return the 32-bit entry point, otherwise
167 * create a 32->16 thunk to the 16-bit entry point, using the
168 * given relay code.
171 static FARPROC THUNK_GetCalloutThunk( NE_MODULE *pModule, LPSTR name, RELAY relay )
173 FARPROC16 proc = WIN32_GetProcAddress16( pModule->self, name );
174 if ( !proc ) return 0;
176 if ( pModule->flags & NE_FFLAGS_BUILTIN )
177 return (FARPROC)((ENTRYPOINT16 *)PTR_SEG_TO_LIN( proc ))->target;
178 else
179 return (FARPROC)THUNK_Alloc( proc, relay );
182 /***********************************************************************
183 * THUNK_InitCallout
185 void THUNK_InitCallout(void)
187 HMODULE hModule;
188 NE_MODULE *pModule;
190 hModule = GetModuleHandleA( "USER32" );
191 if ( hModule )
193 #define GETADDR( name ) \
194 *(FARPROC *)&Callout.##name = GetProcAddress( hModule, #name )
196 GETADDR( PeekMessageA );
197 GETADDR( PeekMessageW );
198 GETADDR( GetMessageA );
199 GETADDR( GetMessageW );
200 GETADDR( SendMessageA );
201 GETADDR( SendMessageW );
202 GETADDR( PostMessageA );
203 GETADDR( PostMessageW );
204 GETADDR( PostThreadMessageA );
205 GETADDR( PostThreadMessageW );
206 GETADDR( TranslateMessage );
207 GETADDR( DispatchMessageW );
208 GETADDR( DispatchMessageA );
209 GETADDR( RedrawWindow );
210 GETADDR( WaitForInputIdle );
211 GETADDR( MessageBoxA );
212 GETADDR( MessageBoxW );
213 #undef GETADDR
216 pModule = NE_GetPtr( GetModuleHandle16( "USER" ) );
217 if ( pModule )
219 #define GETADDR( var, name, thk ) \
220 *(FARPROC *)&Callout.##var = THUNK_GetCalloutThunk( pModule, name, \
221 (RELAY)THUNK_CallTo16_##thk )
223 GETADDR( PeekMessage16, "PeekMessage", word_lwwww );
224 GETADDR( GetMessage16, "GetMessage", word_lwww );
225 GETADDR( SendMessage16, "SendMessage", long_wwwl );
226 GETADDR( PostMessage16, "PostMessage", word_wwwl );
227 GETADDR( PostAppMessage16, "PostAppMessage", word_wwwl );
228 GETADDR( TranslateMessage16, "TranslateMessage", word_l );
229 GETADDR( DispatchMessage16, "DispatchMessage", long_l );
230 GETADDR( RedrawWindow16, "RedrawWindow", word_wlww );
231 GETADDR( FinalUserInit16, "FinalUserInit", word_ );
232 GETADDR( InitApp16, "InitApp", word_w );
233 GETADDR( InitThreadInput16, "InitThreadInput", word_ww );
234 GETADDR( UserYield16, "UserYield", word_ );
235 GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
236 GETADDR( UserSignalProc, "SignalProc32", word_lllw );
238 #undef GETADDR
242 /***********************************************************************
243 * 16->32 Flat Thunk routines:
246 /***********************************************************************
247 * ThunkConnect16 (KERNEL.651)
248 * Connects a 32bit and a 16bit thunkbuffer.
250 UINT WINAPI ThunkConnect16(
251 LPSTR module16, /* [in] name of win16 dll */
252 LPSTR module32, /* [in] name of win32 dll */
253 HINSTANCE16 hInst16, /* [in] hInst of win16 dll */
254 DWORD dwReason, /* [in] initialisation argument */
255 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
256 LPSTR thunkfun32, /* [in] win32 thunkfunction */
257 WORD cs /* [in] CS of win16 dll */
259 BOOL directionSL;
261 if (!strncmp(TD->magic, "SL01", 4))
263 directionSL = TRUE;
265 TRACE("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
266 module16, (DWORD)TD, module32, thunkfun32, dwReason);
268 else if (!strncmp(TD->magic, "LS01", 4))
270 directionSL = FALSE;
272 TRACE("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
273 module16, (DWORD)TD, module32, thunkfun32, dwReason);
275 else
277 ERR("Invalid magic %c%c%c%c\n",
278 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
279 return 0;
282 switch (dwReason)
284 case DLL_PROCESS_ATTACH:
285 if (directionSL)
287 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
288 struct ThunkDataSL *SL = SL16->fpData;
290 if (SL == NULL)
292 SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));
294 SL->common = SL16->common;
295 SL->flags1 = SL16->flags1;
296 SL->flags2 = SL16->flags2;
298 SL->apiDB = PTR_SEG_TO_LIN(SL16->apiDatabase);
299 SL->targetDB = NULL;
301 lstrcpynA(SL->pszDll16, module16, 255);
302 lstrcpynA(SL->pszDll32, module32, 255);
304 /* We should create a SEGPTR to the ThunkDataSL,
305 but since the contents are not in the original format,
306 any access to this by 16-bit code would crash anyway. */
307 SL16->spData = 0;
308 SL16->fpData = SL;
312 if (SL->flags2 & 0x80000000)
314 TRACE("Preloading 32-bit library\n");
315 LoadLibraryA(module32);
318 else
320 /* nothing to do */
322 break;
324 case DLL_PROCESS_DETACH:
325 /* FIXME: cleanup */
326 break;
329 return 1;
333 /***********************************************************************
334 * C16ThkSL (KERNEL.630)
337 void WINAPI C16ThkSL(CONTEXT86 *context)
339 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
340 WORD cs = __get_cs();
341 WORD ds = __get_ds();
343 /* We produce the following code:
345 * mov ax, __FLATDS
346 * mov es, ax
347 * movzx ecx, cx
348 * mov edx, es:[ecx + $EDX]
349 * push bp
350 * push edx
351 * push dx
352 * push edx
353 * call __FLATCS:CallFrom16Thunk
356 *x++ = 0xB8; *((WORD *)x)++ = ds;
357 *x++ = 0x8E; *x++ = 0xC0;
358 *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
359 *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
360 *x++ = 0x91; *((DWORD *)x)++ = EDX_reg(context);
362 *x++ = 0x55;
363 *x++ = 0x66; *x++ = 0x52;
364 *x++ = 0x52;
365 *x++ = 0x66; *x++ = 0x52;
366 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
367 *((WORD *)x)++ = cs;
369 /* Jump to the stub code just created */
370 EIP_reg(context) = LOWORD(EAX_reg(context));
371 CS_reg(context) = HIWORD(EAX_reg(context));
373 /* Since C16ThkSL got called by a jmp, we need to leave the
374 original return address on the stack */
375 ESP_reg(context) -= 4;
378 /***********************************************************************
379 * C16ThkSL01 (KERNEL.631)
382 void WINAPI C16ThkSL01(CONTEXT86 *context)
384 LPBYTE stub = PTR_SEG_TO_LIN(EAX_reg(context)), x = stub;
386 if (stub)
388 struct ThunkDataSL16 *SL16 = PTR_SEG_TO_LIN(EDX_reg(context));
389 struct ThunkDataSL *td = SL16->fpData;
391 DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), 631);
392 WORD cs = __get_cs();
394 if (!td)
396 ERR("ThunkConnect16 was not called!\n");
397 return;
400 TRACE("Creating stub for ThunkDataSL %08lx\n", (DWORD)td);
403 /* We produce the following code:
405 * xor eax, eax
406 * mov edx, $td
407 * call C16ThkSL01
408 * push bp
409 * push edx
410 * push dx
411 * push edx
412 * call __FLATCS:CallFrom16Thunk
415 *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
416 *x++ = 0x66; *x++ = 0xBA; *((DWORD *)x)++ = (DWORD)td;
417 *x++ = 0x9A; *((DWORD *)x)++ = procAddress;
419 *x++ = 0x55;
420 *x++ = 0x66; *x++ = 0x52;
421 *x++ = 0x52;
422 *x++ = 0x66; *x++ = 0x52;
423 *x++ = 0x66; *x++ = 0x9A; *((DWORD *)x)++ = (DWORD)CallFrom16Thunk;
424 *((WORD *)x)++ = cs;
426 /* Jump to the stub code just created */
427 EIP_reg(context) = LOWORD(EAX_reg(context));
428 CS_reg(context) = HIWORD(EAX_reg(context));
430 /* Since C16ThkSL01 got called by a jmp, we need to leave the
431 orginal return address on the stack */
432 ESP_reg(context) -= 4;
434 else
436 struct ThunkDataSL *td = (struct ThunkDataSL *)EDX_reg(context);
437 DWORD targetNr = CX_reg(context) / 4;
438 struct SLTargetDB *tdb;
440 TRACE("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
441 (DWORD)PROCESS_Current(), targetNr, (DWORD)td);
443 for (tdb = td->targetDB; tdb; tdb = tdb->next)
444 if (tdb->process == PROCESS_Current())
445 break;
447 if (!tdb)
449 TRACE("Loading 32-bit library %s\n", td->pszDll32);
450 LoadLibraryA(td->pszDll32);
452 for (tdb = td->targetDB; tdb; tdb = tdb->next)
453 if (tdb->process == PROCESS_Current())
454 break;
457 if (tdb)
459 EDX_reg(context) = tdb->targetTable[targetNr];
461 TRACE("Call target is %08lx\n", EDX_reg(context));
463 else
465 WORD *stack = PTR_SEG_OFF_TO_LIN(SS_reg(context), LOWORD(ESP_reg(context)));
466 DX_reg(context) = HIWORD(td->apiDB[targetNr].errorReturnValue);
467 AX_reg(context) = LOWORD(td->apiDB[targetNr].errorReturnValue);
468 EIP_reg(context) = stack[2];
469 CS_reg(context) = stack[3];
470 ESP_reg(context) += td->apiDB[targetNr].nrArgBytes + 4;
472 ERR("Process %08lx did not ThunkConnect32 %s to %s\n",
473 (DWORD)PROCESS_Current(), td->pszDll32, td->pszDll16);
480 /***********************************************************************
481 * 16<->32 Thunklet/Callback API:
484 #include "pshpack1.h"
485 typedef struct _THUNKLET
487 BYTE prefix_target;
488 BYTE pushl_target;
489 DWORD target;
491 BYTE prefix_relay;
492 BYTE pushl_relay;
493 DWORD relay;
495 BYTE jmp_glue;
496 DWORD glue;
498 BYTE type;
499 HINSTANCE16 owner;
500 struct _THUNKLET *next;
501 } THUNKLET;
502 #include "poppack.h"
504 #define THUNKLET_TYPE_LS 1
505 #define THUNKLET_TYPE_SL 2
507 static HANDLE ThunkletHeap = 0;
508 static THUNKLET *ThunkletAnchor = NULL;
510 static FARPROC ThunkletSysthunkGlueLS = 0;
511 static SEGPTR ThunkletSysthunkGlueSL = 0;
513 static FARPROC ThunkletCallbackGlueLS = 0;
514 static SEGPTR ThunkletCallbackGlueSL = 0;
516 /***********************************************************************
517 * THUNK_ThunkletInit
519 static BOOL THUNK_ThunkletInit( void )
521 LPBYTE thunk;
523 ThunkletHeap = HeapCreate(HEAP_WINE_SEGPTR | HEAP_WINE_CODE16SEG, 0, 0);
524 if (!ThunkletHeap) return FALSE;
526 thunk = HeapAlloc( ThunkletHeap, 0, 5 );
527 if (!thunk) return FALSE;
529 ThunkletSysthunkGlueLS = (FARPROC)thunk;
530 *thunk++ = 0x58; /* popl eax */
531 *thunk++ = 0xC3; /* ret */
533 ThunkletSysthunkGlueSL = HEAP_GetSegptr( ThunkletHeap, 0, thunk );
534 *thunk++ = 0x66; *thunk++ = 0x58; /* popl eax */
535 *thunk++ = 0xCB; /* lret */
537 return TRUE;
540 /***********************************************************************
541 * SetThunkletCallbackGlue (KERNEL.560)
543 void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
545 ThunkletCallbackGlueLS = glueLS;
546 ThunkletCallbackGlueSL = glueSL;
550 /***********************************************************************
551 * THUNK_FindThunklet
553 THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
554 DWORD glue, BYTE type )
556 THUNKLET *thunk;
558 for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
559 if ( thunk->type == type
560 && thunk->target == target
561 && thunk->relay == relay
562 && ( type == THUNKLET_TYPE_LS ?
563 ( thunk->glue == glue - (DWORD)&thunk->type )
564 : ( thunk->glue == glue ) ) )
565 return thunk;
567 return NULL;
570 /***********************************************************************
571 * THUNK_AllocLSThunklet
573 FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
574 FARPROC glue, HTASK16 owner )
576 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
577 THUNKLET_TYPE_LS );
578 if (!thunk)
580 TDB *pTask = (TDB*)GlobalLock16( owner );
582 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
583 return 0;
585 thunk->prefix_target = thunk->prefix_relay = 0x90;
586 thunk->pushl_target = thunk->pushl_relay = 0x68;
587 thunk->jmp_glue = 0xE9;
589 thunk->target = (DWORD)target;
590 thunk->relay = (DWORD)relay;
591 thunk->glue = (DWORD)glue - (DWORD)&thunk->type;
593 thunk->type = THUNKLET_TYPE_LS;
594 thunk->owner = pTask? pTask->hInstance : 0;
596 thunk->next = ThunkletAnchor;
597 ThunkletAnchor = thunk;
600 return (FARPROC)thunk;
603 /***********************************************************************
604 * THUNK_AllocSLThunklet
606 SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
607 SEGPTR glue, HTASK16 owner )
609 THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
610 THUNKLET_TYPE_SL );
611 if (!thunk)
613 TDB *pTask = (TDB*)GlobalLock16( owner );
615 if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
616 return 0;
618 thunk->prefix_target = thunk->prefix_relay = 0x66;
619 thunk->pushl_target = thunk->pushl_relay = 0x68;
620 thunk->jmp_glue = 0xEA;
622 thunk->target = (DWORD)target;
623 thunk->relay = (DWORD)relay;
624 thunk->glue = (DWORD)glue;
626 thunk->type = THUNKLET_TYPE_SL;
627 thunk->owner = pTask? pTask->hInstance : 0;
629 thunk->next = ThunkletAnchor;
630 ThunkletAnchor = thunk;
633 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
636 /**********************************************************************
637 * IsLSThunklet
639 BOOL16 WINAPI IsLSThunklet( THUNKLET *thunk )
641 return thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
642 && thunk->prefix_relay == 0x90 && thunk->pushl_relay == 0x68
643 && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
646 /**********************************************************************
647 * IsSLThunklet (KERNEL.612)
649 BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
651 return thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
652 && thunk->prefix_relay == 0x66 && thunk->pushl_relay == 0x68
653 && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
658 /***********************************************************************
659 * AllocLSThunkletSysthunk (KERNEL.607)
661 FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
662 FARPROC relay, DWORD dummy )
664 return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
665 ThunkletSysthunkGlueLS, GetCurrentTask() );
668 /***********************************************************************
669 * AllocSLThunkletSysthunk (KERNEL.608)
671 SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
672 SEGPTR relay, DWORD dummy )
674 return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
675 ThunkletSysthunkGlueSL, GetCurrentTask() );
679 /***********************************************************************
680 * AllocLSThunkletCallbackEx (KERNEL.567)
682 FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
683 DWORD relay, HTASK16 task )
685 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
686 if ( !thunk ) return NULL;
688 if ( IsSLThunklet16( thunk ) && thunk->relay == relay
689 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
690 return (FARPROC)thunk->target;
692 return THUNK_AllocLSThunklet( target, relay,
693 ThunkletCallbackGlueLS, task );
696 /***********************************************************************
697 * AllocSLThunkletCallbackEx (KERNEL.568)
699 SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
700 DWORD relay, HTASK16 task )
702 THUNKLET *thunk = (THUNKLET *)target;
703 if ( !thunk ) return 0;
705 if ( IsLSThunklet( thunk ) && thunk->relay == relay
706 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
707 return (SEGPTR)thunk->target;
709 return THUNK_AllocSLThunklet( target, relay,
710 ThunkletCallbackGlueSL, task );
713 /***********************************************************************
714 * AllocLSThunkletCallback (KERNEL.561) (KERNEL.606)
716 FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
718 return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
721 /***********************************************************************
722 * AllocSLThunkletCallback (KERNEL.562) (KERNEL.605)
724 SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
726 return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
729 /***********************************************************************
730 * FindLSThunkletCallback (KERNEL.563) (KERNEL.609)
732 FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
734 THUNKLET *thunk = (THUNKLET *)PTR_SEG_TO_LIN( target );
735 if ( thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
736 && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
737 return (FARPROC)thunk->target;
739 thunk = THUNK_FindThunklet( (DWORD)target, relay,
740 (DWORD)ThunkletCallbackGlueLS,
741 THUNKLET_TYPE_LS );
742 return (FARPROC)thunk;
745 /***********************************************************************
746 * FindSLThunkletCallback (KERNEL.564) (KERNEL.610)
748 SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
750 THUNKLET *thunk = (THUNKLET *)target;
751 if ( thunk && IsLSThunklet( thunk ) && thunk->relay == relay
752 && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
753 return (SEGPTR)thunk->target;
755 thunk = THUNK_FindThunklet( (DWORD)target, relay,
756 (DWORD)ThunkletCallbackGlueSL,
757 THUNKLET_TYPE_SL );
758 return HEAP_GetSegptr( ThunkletHeap, 0, thunk );
762 /***********************************************************************
763 * FreeThunklet16 (KERNEL.611)
765 BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
767 return FALSE;
770 /***********************************************************************
771 * Callback Client API
774 #define N_CBC_FIXED 20
775 #define N_CBC_VARIABLE 10
776 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
778 static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
779 static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];
781 /***********************************************************************
782 * RegisterCBClient (KERNEL.619)
784 INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
785 SEGPTR relay16, FARPROC *relay32 )
787 /* Search for free Callback ID */
788 if ( wCBCId == -1 )
789 for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
790 if ( !CBClientRelay16[ wCBCId ] )
791 break;
793 /* Register Callback ID */
794 if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
796 CBClientRelay16[ wCBCId ] = relay16;
797 CBClientRelay32[ wCBCId ] = relay32;
799 else
800 wCBCId = 0;
802 return wCBCId;
805 /***********************************************************************
806 * UnRegisterCBClient (KERNEL.622)
808 INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
809 SEGPTR relay16, FARPROC *relay32 )
811 if ( wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
812 && CBClientRelay16[ wCBCId ] == relay16
813 && CBClientRelay32[ wCBCId ] == relay32 )
815 CBClientRelay16[ wCBCId ] = 0;
816 CBClientRelay32[ wCBCId ] = 0;
818 else
819 wCBCId = 0;
821 return wCBCId;
825 /***********************************************************************
826 * InitCBClient (KERNEL.623)
828 void WINAPI InitCBClient16( FARPROC glueLS )
830 HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
831 SEGPTR glueSL = (SEGPTR)WIN32_GetProcAddress16( kernel, (LPCSTR)604 );
833 SetThunkletCallbackGlue16( glueLS, glueSL );
836 /***********************************************************************
837 * CBClientGlueSL (KERNEL.604)
839 void WINAPI CBClientGlueSL( CONTEXT86 *context )
841 /* Create stack frame */
842 SEGPTR stackSeg = stack16_push( 12 );
843 LPWORD stackLin = PTR_SEG_TO_LIN( stackSeg );
844 SEGPTR glue, *glueTab;
846 stackLin[3] = BP_reg( context );
847 stackLin[2] = SI_reg( context );
848 stackLin[1] = DI_reg( context );
849 stackLin[0] = DS_reg( context );
851 EBP_reg( context ) = OFFSETOF( stackSeg ) + 6;
852 ESP_reg( context ) = OFFSETOF( stackSeg ) - 4;
853 GS_reg( context ) = 0;
855 /* Jump to 16-bit relay code */
856 glueTab = PTR_SEG_TO_LIN( CBClientRelay16[ stackLin[5] ] );
857 glue = glueTab[ stackLin[4] ];
858 CS_reg ( context ) = SELECTOROF( glue );
859 EIP_reg( context ) = OFFSETOF ( glue );
862 /***********************************************************************
863 * CBClientThunkSL (KERNEL.620)
865 extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi );
866 void WINAPI CBClientThunkSL( CONTEXT86 *context )
868 /* Call 32-bit relay code */
870 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
871 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
873 EAX_reg(context) = CALL32_CBClient( proc, args, &ESI_reg( context ) );
876 /***********************************************************************
877 * CBClientThunkSLEx (KERNEL.621)
879 extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs );
880 void WINAPI CBClientThunkSLEx( CONTEXT86 *context )
882 /* Call 32-bit relay code */
884 LPWORD args = PTR_SEG_OFF_TO_LIN( SS_reg( context ), BP_reg( context ) );
885 FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
886 INT nArgs;
887 LPWORD stackLin;
889 EAX_reg(context) = CALL32_CBClientEx( proc, args, &ESI_reg( context ), &nArgs );
891 /* Restore registers saved by CBClientGlueSL */
892 stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
893 BP_reg( context ) = stackLin[3];
894 SI_reg( context ) = stackLin[2];
895 DI_reg( context ) = stackLin[1];
896 DS_reg( context ) = stackLin[0];
897 ESP_reg( context ) += 16+nArgs;
899 /* Return to caller of CBClient thunklet */
900 CS_reg ( context ) = stackLin[9];
901 EIP_reg( context ) = stackLin[8];