Release 980726
[wine/multimedia.git] / relay32 / relay386.c
blobbb6297f062c3019d2f8cb66dd1ba1ea2088768fe
1 /*
2 * 386-specific Win32 relay functions
4 * Copyright 1997 Alexandre Julliard
5 */
7 #ifdef __i386__
9 #include <assert.h>
10 #include <string.h>
11 #include "winnt.h"
12 #include "windows.h"
13 #include "builtin32.h"
14 #include "selectors.h"
15 #include "debugstr.h"
16 #include "debug.h"
18 char **debug_relay_excludelist = NULL, **debug_relay_includelist = NULL;
20 /***********************************************************************
21 * RELAY_ShowDebugmsgRelay
23 * Simple function to decide if a particular debugging message is
24 * wanted. Called from RELAY_CallFrom32 and from in if1632/relay.c
26 int RELAY_ShowDebugmsgRelay(const char *func) {
28 if(debug_relay_excludelist || debug_relay_includelist) {
29 const char *term = strchr(func, ':');
30 char **listitem;
31 int len, len2, itemlen, show;
33 if(debug_relay_excludelist) {
34 show = 1;
35 listitem = debug_relay_excludelist;
36 } else {
37 show = 0;
38 listitem = debug_relay_includelist;
40 assert(term);
41 assert(strlen(term) > 2);
42 len = term - func;
43 len2 = strchr(func, '.') - func;
44 assert(len2 && len2 > 0 && len2 < 64);
45 term += 2;
46 for(; *listitem; listitem++) {
47 itemlen = strlen(*listitem);
48 if((itemlen == len && !strncmp(*listitem, func, len)) ||
49 (itemlen == len2 && !strncmp(*listitem, func, len2)) ||
50 !strcmp(*listitem, term)) {
51 show = !show;
52 break;
55 return show;
57 return 1;
60 /***********************************************************************
61 * RELAY_CallFrom32
63 * Stack layout on entry to this function:
64 * ... ...
65 * (esp+12) arg2
66 * (esp+8) arg1
67 * (esp+4) ret_addr
68 * (esp) return addr to relay code
70 int RELAY_CallFrom32( int ret_addr, ... )
72 int i, ret, show;
73 char buffer[80];
74 FARPROC32 func;
75 unsigned int mask, typemask;
76 WORD fs;
78 int *args = &ret_addr;
79 /* Relay addr is the return address for this function */
80 BYTE *relay_addr = (BYTE *)args[-1];
81 WORD nb_args = *(WORD *)(relay_addr + 1) / sizeof(int);
83 assert(TRACE_ON(relay));
84 func = (FARPROC32)BUILTIN32_GetEntryPoint( buffer, relay_addr - 5,
85 &typemask );
86 show = RELAY_ShowDebugmsgRelay(buffer);
87 if(show) {
88 DPRINTF( "Call %s(", buffer );
89 args++;
90 for (i = 0, mask = 3; i < nb_args; i++, mask <<= 2)
92 if (i) DPRINTF( "," );
93 if ((typemask & mask) && HIWORD(args[i]))
95 if (typemask & (2<<(2*i)))
96 DPRINTF( "%08x L%s", args[i], debugstr_w((LPWSTR)args[i]) );
97 else
98 DPRINTF( "%08x %s", args[i], debugstr_a((LPCSTR)args[i]) );
100 else DPRINTF( "%08x", args[i] );
102 GET_FS( fs );
103 DPRINTF( ") ret=%08x fs=%04x\n", ret_addr, fs );
104 } else {
105 args++;
106 fs = 0; /* quieten gcc */
108 if (*relay_addr == 0xc3) /* cdecl */
110 LRESULT (*cfunc)() = (LRESULT(*)())func;
111 switch(nb_args)
113 case 0: ret = cfunc(); break;
114 case 1: ret = cfunc(args[0]); break;
115 case 2: ret = cfunc(args[0],args[1]); break;
116 case 3: ret = cfunc(args[0],args[1],args[2]); break;
117 case 4: ret = cfunc(args[0],args[1],args[2],args[3]); break;
118 case 5: ret = cfunc(args[0],args[1],args[2],args[3],args[4]); break;
119 case 6: ret = cfunc(args[0],args[1],args[2],args[3],args[4],
120 args[5]); break;
121 case 7: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
122 args[6]); break;
123 case 8: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
124 args[6],args[7]); break;
125 case 9: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
126 args[6],args[7],args[8]); break;
127 case 10: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
128 args[6],args[7],args[8],args[9]); break;
129 case 11: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
130 args[6],args[7],args[8],args[9],args[10]); break;
131 case 12: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
132 args[6],args[7],args[8],args[9],args[10],
133 args[11]); break;
134 case 13: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
135 args[6],args[7],args[8],args[9],args[10],args[11],
136 args[12]); break;
137 case 14: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
138 args[6],args[7],args[8],args[9],args[10],args[11],
139 args[12],args[13]); break;
140 case 15: ret = cfunc(args[0],args[1],args[2],args[3],args[4],args[5],
141 args[6],args[7],args[8],args[9],args[10],args[11],
142 args[12],args[13],args[14]); break;
143 default:
144 ERR(relay, "Unsupported nb args %d\n",
145 nb_args );
146 assert(FALSE);
149 else /* stdcall */
151 switch(nb_args)
153 case 0: ret = func(); break;
154 case 1: ret = func(args[0]); break;
155 case 2: ret = func(args[0],args[1]); break;
156 case 3: ret = func(args[0],args[1],args[2]); break;
157 case 4: ret = func(args[0],args[1],args[2],args[3]); break;
158 case 5: ret = func(args[0],args[1],args[2],args[3],args[4]); break;
159 case 6: ret = func(args[0],args[1],args[2],args[3],args[4],
160 args[5]); break;
161 case 7: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
162 args[6]); break;
163 case 8: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
164 args[6],args[7]); break;
165 case 9: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
166 args[6],args[7],args[8]); break;
167 case 10: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
168 args[6],args[7],args[8],args[9]); break;
169 case 11: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
170 args[6],args[7],args[8],args[9],args[10]); break;
171 case 12: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
172 args[6],args[7],args[8],args[9],args[10],
173 args[11]); break;
174 case 13: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
175 args[6],args[7],args[8],args[9],args[10],args[11],
176 args[12]); break;
177 case 14: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
178 args[6],args[7],args[8],args[9],args[10],args[11],
179 args[12],args[13]); break;
180 case 15: ret = func(args[0],args[1],args[2],args[3],args[4],args[5],
181 args[6],args[7],args[8],args[9],args[10],args[11],
182 args[12],args[13],args[14]); break;
183 default:
184 ERR(relay, "Unsupported nb args %d\n",nb_args );
185 assert(FALSE);
188 if(show)
189 DPRINTF( "Ret %s() retval=%08x ret=%08x fs=%04x\n",
190 buffer, ret, ret_addr, fs );
191 return ret;
195 /***********************************************************************
196 * RELAY_CallFrom32Regs
198 * 'context' contains the register contents at the point of call of
199 * the REG_ENTRY_POINT. The stack layout of the stack pointed to by
200 * ESP_reg(&context) is as follows:
202 * If debugmsg(relay) is OFF:
203 * ... ...
204 * (esp+4) args
205 * (esp) return addr to caller
206 * (esp-4) function entry point
208 * If debugmsg(relay) is ON:
209 * ... ...
210 * (esp+8) args
211 * (esp+4) return addr to caller
212 * (esp) return addr to DEBUG_ENTRY_POINT
213 * (esp-4) function entry point
215 * As the called function might change the stack layout
216 * (e.g. FT_Prolog, FT_ExitNN), we remove all modifications to the stack,
217 * so that the called function sees (in both cases):
219 * ... ...
220 * (esp+4) args
221 * (esp) return addr to caller
222 * ... >128 bytes space free to be modified (ensured by the assembly glue)
224 * NOTE: This routine makes no assumption about the relative position of
225 * its own stack to the stack pointed to by ESP_reg(&context),
226 * except that the latter must have >128 bytes space to grow.
227 * This means the assembly glue could even switch stacks completely
228 * (e.g. to allow for large stacks).
232 void RELAY_CallFrom32Regs( CONTEXT context )
234 typedef void (CALLBACK *entry_point_t)(CONTEXT *);
235 entry_point_t entry_point = *(entry_point_t*) (ESP_reg(&context) - 4);
237 __RESTORE_ES;
239 if (!TRACE_ON(relay))
241 /* Simply call the entry point */
242 entry_point( &context );
244 else
246 char buffer[80];
247 unsigned int typemask;
248 BYTE *relay_addr;
251 * Fixup the context structure because of the extra parameter
252 * pushed by the relay debugging code.
253 * Note that this implicitly does a RET on the CALL from the
254 * DEBUG_ENTRY_POINT to the REG_ENTRY_POINT; setting the EIP register
255 * ensures that the assembly glue will directly return to the
256 * caller, just as in the non-debugging case.
259 relay_addr = *(BYTE **) ESP_reg(&context);
260 if (BUILTIN32_GetEntryPoint( buffer, relay_addr - 5, &typemask )) {
261 /* correct win32 spec generated register function found.
262 * remove extra call stuff from stack
264 ESP_reg(&context) += sizeof(BYTE *);
265 EIP_reg(&context) = *(DWORD *)ESP_reg(&context);
266 DPRINTF("Call %s(regs) ret=%08x\n", buffer, *(int *)ESP_reg(&context) );
267 DPRINTF(" EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx ESI=%08lx EDI=%08lx\n",
268 EAX_reg(&context), EBX_reg(&context), ECX_reg(&context),
269 EDX_reg(&context), ESI_reg(&context), EDI_reg(&context) );
270 DPRINTF(" EBP=%08lx ESP=%08lx EIP=%08lx DS=%04lx ES=%04lx FS=%04lx GS=%04lx EFL=%08lx\n",
271 EBP_reg(&context), ESP_reg(&context), EIP_reg(&context),
272 DS_reg(&context), ES_reg(&context), FS_reg(&context),
273 GS_reg(&context), EFL_reg(&context) );
275 /* Now call the real function */
276 entry_point( &context );
279 DPRINTF("Ret %s() retval=regs ret=%08x\n", buffer, *(int *)ESP_reg(&context) );
280 DPRINTF(" EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx ESI=%08lx EDI=%08lx\n",
281 EAX_reg(&context), EBX_reg(&context), ECX_reg(&context),
282 EDX_reg(&context), ESI_reg(&context), EDI_reg(&context) );
283 DPRINTF(" EBP=%08lx ESP=%08lx EIP=%08lx DS=%04lx ES=%04lx FS=%04lx GS=%04lx EFL=%08lx\n",
284 EBP_reg(&context), ESP_reg(&context), EIP_reg(&context),
285 DS_reg(&context), ES_reg(&context), FS_reg(&context),
286 GS_reg(&context), EFL_reg(&context) );
287 } else
288 /* WINE internal register function found. Do not remove anything.
289 * Do not print any debuginfo (it is not a normal relayed one).
290 * Currently only used for snooping.
292 entry_point( &context );
295 #endif /* __i386__ */