Release 20010216.
[wine/multimedia.git] / if1632 / relay.c
blobc4f27af32fe09ee459d40aa4cf90c6623d2a2844
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
4 */
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "wine/winbase16.h"
10 #include "winnt.h"
11 #include "heap.h"
12 #include "module.h"
13 #include "stackframe.h"
14 #include "selectors.h"
15 #include "builtin16.h"
16 #include "syslevel.h"
17 #include "debugtools.h"
18 #include "callback.h"
20 DEFAULT_DEBUG_CHANNEL(relay);
22 /***********************************************************************
23 * RELAY_Init
25 BOOL RELAY_Init(void)
27 #ifdef __i386__
28 WORD codesel;
30 /* Allocate the code selector for CallTo16 routines */
32 extern void Call16_Ret_Start(), Call16_Ret_End();
33 extern void CallTo16_Ret();
34 extern void CALL32_CBClient_Ret();
35 extern void CALL32_CBClientEx_Ret();
36 extern SEGPTR CallTo16_RetAddr;
37 extern DWORD CallTo16_DataSelector;
38 extern SEGPTR CALL32_CBClient_RetAddr;
39 extern SEGPTR CALL32_CBClientEx_RetAddr;
41 codesel = SELECTOR_AllocBlock( (void *)Call16_Ret_Start,
42 (char *)Call16_Ret_End - (char *)Call16_Ret_Start,
43 WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
44 if (!codesel) return FALSE;
46 /* Patch the return addresses for CallTo16 routines */
48 CallTo16_DataSelector = __get_ds();
49 CallTo16_RetAddr =
50 MAKESEGPTR( codesel, (char*)CallTo16_Ret - (char*)Call16_Ret_Start );
51 CALL32_CBClient_RetAddr =
52 MAKESEGPTR( codesel, (char*)CALL32_CBClient_Ret - (char*)Call16_Ret_Start );
53 CALL32_CBClientEx_RetAddr =
54 MAKESEGPTR( codesel, (char*)CALL32_CBClientEx_Ret - (char*)Call16_Ret_Start );
55 #endif
56 return TRUE;
60 * Stubs for the CallTo16/CallFrom16 routines on non-Intel architectures
61 * (these will never be called but need to be present to satisfy the linker ...)
63 #ifndef __i386__
64 /***********************************************************************
65 * wine_call_to_16_word (KERNEL32.@)
67 WORD WINAPI wine_call_to_16_word( FARPROC16 target, INT nArgs )
69 assert( FALSE );
72 /***********************************************************************
73 * wine_call_to_16_long (KERNEL32.@)
75 LONG WINAPI wine_call_to_16_long( FARPROC16 target, INT nArgs )
77 assert( FALSE );
80 /***********************************************************************
81 * wine_call_to_16_regs_short (KERNEL32.@)
83 void WINAPI wine_call_to_16_regs_short( CONTEXT86 *context, INT nArgs )
85 assert( FALSE );
88 /***********************************************************************
89 * wine_call_to_16_regs_long (KERNEL32.@)
91 void WINAPI wine_call_to_16_regs_long ( CONTEXT86 *context, INT nArgs )
93 assert( FALSE );
96 /***********************************************************************
97 * __wine_call_from_16_word (KERNEL32.@)
99 WORD __cdecl __wine_call_from_16_word(void)
101 assert( FALSE );
104 /***********************************************************************
105 * __wine_call_from_16_long (KERNEL32.@)
107 LONG __cdecl __wine_call_from_16_long(void)
109 assert( FALSE );
112 /***********************************************************************
113 * __wine_call_from_16_regs (KERNEL32.@)
115 void __cdecl __wine_call_from_16_regs(void)
117 assert( FALSE );
120 /***********************************************************************
121 * __wine_call_from_16_thunk (KERNEL32.@)
123 void __cdecl __wine_call_from_16_thunk(void)
125 assert( FALSE );
128 DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi )
129 { assert( FALSE ); }
131 DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs )
132 { assert( FALSE ); }
133 #endif
136 /* from relay32/relay386.c */
137 extern char **debug_relay_excludelist,**debug_relay_includelist;
138 extern int RELAY_ShowDebugmsgRelay(const char *func);
140 /***********************************************************************
141 * RELAY_DebugCallFrom16
143 void RELAY_DebugCallFrom16( CONTEXT86 *context )
145 STACK16FRAME *frame;
146 WORD ordinal;
147 char *args16, funstr[80];
148 const char *args;
149 int i, usecdecl, reg_func;
151 if (!TRACE_ON(relay)) return;
153 frame = CURRENT_STACK16;
154 args = BUILTIN_GetEntryPoint16( frame, funstr, &ordinal );
155 if (!args) return; /* happens for the two snoop register relays */
156 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
157 DPRINTF( "Call %s(",funstr);
158 VA_START16( args16 );
160 usecdecl = ( *args == 'c' );
161 args += 2;
162 reg_func = ( memcmp( args, "regs_", 5 ) == 0
163 || memcmp( args, "intr_", 5 ) == 0 );
164 args += 5;
166 if (usecdecl)
168 while (*args)
170 switch(*args)
172 case 'w':
173 case 's':
174 DPRINTF( "0x%04x", *(WORD *)args16 );
175 args16 += 2;
176 break;
177 case 'l':
178 DPRINTF( "0x%08x", *(int *)args16 );
179 args16 += 4;
180 break;
181 case 'p':
182 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
183 args16 += 4;
184 break;
185 case 't':
186 case 'T':
187 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
188 debugres_a( MapSL(*(SEGPTR *)args16 )) );
189 args16 += 4;
190 break;
192 args++;
193 if (*args) DPRINTF( "," );
196 else /* not cdecl */
198 /* Start with the last arg */
199 for (i = 0; args[i]; i++)
201 switch(args[i])
203 case 'w':
204 case 's':
205 args16 += 2;
206 break;
207 case 'l':
208 case 'p':
209 case 't':
210 case 'T':
211 args16 += 4;
212 break;
216 while (*args)
218 switch(*args)
220 case 'w':
221 case 's':
222 args16 -= 2;
223 DPRINTF( "0x%04x", *(WORD *)args16 );
224 break;
225 case 'l':
226 args16 -= 4;
227 DPRINTF( "0x%08x", *(int *)args16 );
228 break;
229 case 't':
230 args16 -= 4;
231 DPRINTF( "0x%08x %s", *(int *)args16,
232 debugres_a( MapSL(*(SEGPTR *)args16 )));
233 break;
234 case 'p':
235 args16 -= 4;
236 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
237 break;
238 case 'T':
239 args16 -= 4;
240 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
241 debugres_a( MapSL(*(SEGPTR *)args16 )));
242 break;
244 args++;
245 if (*args) DPRINTF( "," );
249 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
250 VA_END16( args16 );
252 if (reg_func)
253 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
254 AX_reg(context), BX_reg(context), CX_reg(context),
255 DX_reg(context), SI_reg(context), DI_reg(context),
256 (WORD)context->SegEs, context->EFlags );
258 SYSLEVEL_CheckNotLevel( 2 );
262 /***********************************************************************
263 * RELAY_DebugCallFrom16Ret
265 void RELAY_DebugCallFrom16Ret( CONTEXT86 *context, int ret_val )
267 STACK16FRAME *frame;
268 WORD ordinal;
269 char funstr[80];
270 const char *args;
272 if (!TRACE_ON(relay)) return;
273 frame = CURRENT_STACK16;
274 args = BUILTIN_GetEntryPoint16( frame, funstr, &ordinal );
275 if (!args) return;
276 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
277 DPRINTF( "Ret %s() ",funstr);
279 if ( memcmp( args+2, "long_", 5 ) == 0 )
281 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
282 ret_val, frame->cs, frame->ip, frame->ds );
284 else if ( memcmp( args+2, "word_", 5 ) == 0 )
286 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
287 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
289 else if ( memcmp( args+2, "regs_", 5 ) == 0
290 || memcmp( args+2, "intr_", 5 ) == 0 )
292 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
293 (WORD)context->SegCs, LOWORD(context->Eip), (WORD)context->SegDs);
294 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
295 AX_reg(context), BX_reg(context), CX_reg(context),
296 DX_reg(context), SI_reg(context), DI_reg(context),
297 (WORD)context->SegEs, context->EFlags );
300 SYSLEVEL_CheckNotLevel( 2 );
304 /***********************************************************************
305 * RELAY_DebugCallTo16
307 * 'target' contains either the function to call (normal CallTo16)
308 * or a pointer to the CONTEXT86 struct (register CallTo16).
309 * 'nb_args' is the number of argument bytes on the 16-bit stack;
310 * 'reg_func' specifies whether we have a register CallTo16 or not.
312 void RELAY_DebugCallTo16( LPVOID target, int nb_args, BOOL reg_func )
314 WORD *stack16;
315 TEB *teb;
317 if (!TRACE_ON(relay)) return;
318 teb = NtCurrentTeb();
319 stack16 = (WORD *)THREAD_STACK16(teb);
321 nb_args /= sizeof(WORD);
323 if ( reg_func )
325 CONTEXT86 *context = (CONTEXT86 *)target;
327 DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
328 context->SegCs, LOWORD(context->Eip), context->SegDs );
329 while (nb_args--) DPRINTF( ",0x%04x", *--stack16 );
330 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
331 OFFSETOF(teb->cur_stack) );
332 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
333 AX_reg(context), BX_reg(context), CX_reg(context),
334 DX_reg(context), SI_reg(context), DI_reg(context),
335 BP_reg(context), (WORD)context->SegEs, (WORD)context->SegFs );
337 else
339 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
340 HIWORD(target), LOWORD(target), SELECTOROF(teb->cur_stack) );
341 while (nb_args--) DPRINTF( ",0x%04x", *--stack16 );
342 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
343 OFFSETOF(teb->cur_stack) );
346 SYSLEVEL_CheckNotLevel( 2 );
350 /***********************************************************************
351 * RELAY_DebugCallTo16Ret
353 void RELAY_DebugCallTo16Ret( BOOL reg_func, int ret_val )
355 if (!TRACE_ON(relay)) return;
357 if (!reg_func)
359 DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n",
360 SELECTOROF(NtCurrentTeb()->cur_stack),
361 OFFSETOF(NtCurrentTeb()->cur_stack), ret_val);
363 else
365 CONTEXT86 *context = (CONTEXT86 *)ret_val;
367 DPRINTF("CallTo16() ss:sp=%04x:%04x\n",
368 SELECTOROF(NtCurrentTeb()->cur_stack),
369 OFFSETOF(NtCurrentTeb()->cur_stack));
370 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x BP=%04x SP=%04x\n",
371 AX_reg(context), BX_reg(context), CX_reg(context),
372 DX_reg(context), BP_reg(context), LOWORD(context->Esp));
375 SYSLEVEL_CheckNotLevel( 2 );