- Fix a typo in TBSAVEPARAMS A/W declaration.
[wine] / if1632 / thunk.c
1 /*
2  * Emulator thunks
3  *
4  * Copyright 1996, 1997 Alexandre Julliard
5  * Copyright 1998       Ulrich Weigand
6  */
7
8 #include <string.h>
9 #include "wine/winbase16.h"
10 #include "callback.h"
11 #include "builtin16.h"
12 #include "heap.h"
13 #include "module.h"
14 #include "stackframe.h"
15 #include "debugtools.h"
16
17 DEFAULT_DEBUG_CHANNEL(thunk);
18
19
20 /* List of the 16-bit callback functions. This list is used  */
21 /* by the build program to generate the file if1632/callto16.S */
22
23 /* ### start build ### */
24 extern WORD CALLBACK THUNK_CallTo16_word_     (FARPROC16);
25 extern WORD CALLBACK THUNK_CallTo16_word_l    (FARPROC16,LONG);
26 extern LONG CALLBACK THUNK_CallTo16_long_l    (FARPROC16,LONG);
27 extern WORD CALLBACK THUNK_CallTo16_word_lllw (FARPROC16,LONG,LONG,LONG,WORD);
28 extern WORD CALLBACK THUNK_CallTo16_word_lwww (FARPROC16,LONG,WORD,WORD,WORD);
29 extern LONG CALLBACK THUNK_CallTo16_long_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
30 extern WORD CALLBACK THUNK_CallTo16_word_lwwww(FARPROC16,LONG,WORD,WORD,WORD,WORD);
31 extern WORD CALLBACK THUNK_CallTo16_word_w    (FARPROC16,WORD);
32 extern WORD CALLBACK THUNK_CallTo16_word_wlww (FARPROC16,WORD,LONG,WORD,WORD);
33 extern WORD CALLBACK THUNK_CallTo16_word_ww   (FARPROC16,WORD,WORD);
34 extern WORD CALLBACK THUNK_CallTo16_word_wwwl (FARPROC16,WORD,WORD,WORD,LONG);
35 /* ### stop build ### */
36
37 static THUNK *firstThunk = NULL;
38
39 CALLOUT_TABLE Callout = {
40     /* PeekMessageA */ NULL,
41     /* GetMessageA */ NULL,
42     /* SendMessageA */ NULL,
43     /* PostMessageA */ NULL,
44     /* TranslateMessage */ NULL,
45     /* DispatchMessageA */ NULL,
46     /* RedrawWindow */ NULL,
47     /* UserSignalProc */ NULL,
48     /* FinalUserInit16 */ NULL,
49     /* InitThreadInput16 */ NULL,
50     /* UserYield16) */ NULL,
51     /* DestroyIcon32 */ NULL,
52     /* WaitForInputIdle */ NULL,
53     /* MsgWaitForMultipleObjects */ NULL,
54     /* WindowFromDC */ NULL,
55     /* MessageBoxA */ NULL,
56     /* MessageBoxW */ NULL
57 };
58
59 /***********************************************************************
60  *           THUNK_Alloc
61  */
62 FARPROC THUNK_Alloc( FARPROC16 func, RELAY relay )
63 {
64     HANDLE16 hSeg;
65     NE_MODULE *pModule;
66     THUNK *thunk;
67
68     /* NULL maps to NULL */
69     if ( !func ) return NULL;
70
71     /* 
72      * If we got an 16-bit built-in API entry point, retrieve the Wine
73      * 32-bit handler for that API routine.
74      *
75      * NOTE: For efficiency reasons, we only check whether the selector
76      *       of 'func' points to the code segment of a built-in module.
77      *       It might be theoretically possible that the offset is such
78      *       that 'func' does not point, in fact, to an API entry point.
79      *       In this case, however, the pointer is corrupt anyway.
80      */
81     hSeg = GlobalHandle16( SELECTOROF( func ) );
82     pModule = NE_GetPtr( FarGetOwner16( hSeg ) );
83
84     if ( pModule && (pModule->flags & NE_FFLAGS_BUILTIN) 
85                  && NE_SEG_TABLE(pModule)[0].hSeg == hSeg )
86     {
87         FARPROC proc = (FARPROC)((ENTRYPOINT16 *)MapSL( (SEGPTR)func ))->target;
88
89         TRACE( "(%04x:%04x, %p) -> built-in API %p\n",
90                SELECTOROF( func ), OFFSETOF( func ), relay, proc );
91         return proc;
92     }
93
94     /* Otherwise, we need to alloc a thunk */
95     thunk = HeapAlloc( GetProcessHeap(), 0, sizeof(*thunk) );
96     if (thunk)
97     {
98         thunk->popl_eax   = 0x58;
99         thunk->pushl_func = 0x68;
100         thunk->proc       = func;
101         thunk->pushl_eax  = 0x50;
102         thunk->jmp        = 0xe9;
103         thunk->relay      = (RELAY)((char *)relay - (char *)(&thunk->next));
104         thunk->magic      = CALLTO16_THUNK_MAGIC;
105         thunk->next       = firstThunk;
106         firstThunk = thunk;
107     }
108
109     TRACE( "(%04x:%04x, %p) -> allocated thunk %p\n",
110            SELECTOROF( func ), OFFSETOF( func ), relay, thunk );
111     return (FARPROC)thunk;
112 }
113
114 /***********************************************************************
115  *           THUNK_Free
116  */
117 void THUNK_Free( FARPROC thunk )
118 {
119     THUNK *t = (THUNK*)thunk;
120     if ( !t || IsBadReadPtr( t, sizeof(*t) ) 
121             || t->magic != CALLTO16_THUNK_MAGIC )
122          return;
123
124     if (HeapValidate( GetProcessHeap(), 0, t ))
125     {
126         THUNK **prev = &firstThunk;
127         while (*prev && (*prev != t)) prev = &(*prev)->next;
128         if (*prev)
129         {
130             *prev = t->next;
131             HeapFree( GetProcessHeap(), 0, t );
132             return;
133         }
134     }
135     ERR("invalid thunk addr %p\n", thunk );
136     return;
137 }
138
139
140 /***********************************************************************
141  *           THUNK_GetCalloutThunk
142  *
143  * Retrieve API entry point with given name from given module.
144  * If module is builtin, return the 32-bit entry point, otherwise
145  * create a 32->16 thunk to the 16-bit entry point, using the 
146  * given relay code.
147  *
148  */
149 static FARPROC THUNK_GetCalloutThunk( NE_MODULE *pModule, LPSTR name, RELAY relay )
150 {
151     FARPROC16 proc = GetProcAddress16( pModule->self, name );
152     if ( !proc ) return 0;
153
154     if ( pModule->flags & NE_FFLAGS_BUILTIN )
155         return (FARPROC)((ENTRYPOINT16 *)MapSL( (SEGPTR)proc ))->target;
156     else
157         return (FARPROC)THUNK_Alloc( proc, relay );
158 }
159
160 /***********************************************************************
161  *           THUNK_InitCallout
162  */
163 void THUNK_InitCallout(void)
164 {
165     HMODULE hModule;
166     NE_MODULE *pModule;
167
168     hModule = GetModuleHandleA( "user32.dll" );
169     if ( hModule )
170     {
171 #define GETADDR( name )  \
172         *(FARPROC *)&Callout.name = GetProcAddress( hModule, #name )
173
174         GETADDR( PeekMessageA );
175         GETADDR( GetMessageA );
176         GETADDR( SendMessageA );
177         GETADDR( PostMessageA );
178         GETADDR( TranslateMessage );
179         GETADDR( DispatchMessageA );
180         GETADDR( RedrawWindow );
181         GETADDR( WaitForInputIdle );
182         GETADDR( MsgWaitForMultipleObjects );
183         GETADDR( WindowFromDC );
184         GETADDR( MessageBoxA );
185         GETADDR( MessageBoxW );
186 #undef GETADDR
187     }
188     else WARN("no 32-bit USER\n");
189
190     pModule = NE_GetPtr( GetModuleHandle16( "USER.EXE" ) );
191     if ( pModule )
192     {
193 #define GETADDR( var, name, thk )  \
194         *(FARPROC *)&Callout.var = THUNK_GetCalloutThunk( pModule, name, \
195                                                (RELAY)THUNK_CallTo16_##thk )
196
197         GETADDR( FinalUserInit16, "FinalUserInit", word_ );
198         GETADDR( InitThreadInput16, "InitThreadInput", word_ww );
199         GETADDR( UserYield16, "UserYield", word_ );
200         GETADDR( DestroyIcon32, "DestroyIcon32", word_ww );
201         GETADDR( UserSignalProc, "SignalProc32", word_lllw );
202 #undef GETADDR
203     }
204     else WARN("no 16-bit USER\n");
205 }