The new filedialog stuff does not handle template. So do not call if
[wine/hacks.git] / if1632 / relay.c
blobc161d4fc9a88687f695c51359a0465021d82d0c0
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 "global.h"
12 #include "heap.h"
13 #include "module.h"
14 #include "stackframe.h"
15 #include "builtin16.h"
16 #include "task.h"
17 #include "syslevel.h"
18 #include "debugstr.h"
19 #include "debugtools.h"
20 #include "main.h"
22 DEFAULT_DEBUG_CHANNEL(relay)
24 /***********************************************************************
25 * RELAY_Init
27 BOOL RELAY_Init(void)
29 #ifdef __i386__
30 WORD codesel;
32 /* Allocate the code selector for CallTo16 routines */
34 extern void Call16_Ret_Start(), Call16_Ret_End();
35 extern void CallTo16_Ret();
36 extern void CALL32_CBClient_Ret();
37 extern void CALL32_CBClientEx_Ret();
38 extern DWORD CallTo16_RetAddr;
39 extern DWORD CALL32_CBClient_RetAddr;
40 extern DWORD CALL32_CBClientEx_RetAddr;
42 codesel = GLOBAL_CreateBlock( GMEM_FIXED, (void *)Call16_Ret_Start,
43 (int)Call16_Ret_End - (int)Call16_Ret_Start,
44 GetModuleHandle16( "KERNEL" ),
45 TRUE, TRUE, FALSE, NULL );
46 if (!codesel) return FALSE;
48 /* Patch the return addresses for CallTo16 routines */
50 CallTo16_RetAddr =
51 MAKELONG( (int)CallTo16_Ret -(int)Call16_Ret_Start, codesel );
52 CALL32_CBClient_RetAddr =
53 MAKELONG( (int)CALL32_CBClient_Ret -(int)Call16_Ret_Start, codesel );
54 CALL32_CBClientEx_RetAddr =
55 MAKELONG( (int)CALL32_CBClientEx_Ret -(int)Call16_Ret_Start, codesel );
56 #endif
58 /* Create built-in modules */
59 if (!BUILTIN_Init()) return FALSE;
61 /* Initialize thunking */
62 return THUNK_Init();
66 * Stubs for the CallTo16/CallFrom16 routines on non-Intel architectures
67 * (these will never be called but need to be present to satisfy the linker ...)
69 #ifndef __i386__
70 WORD CALLBACK CallTo16Word( FARPROC16 target, INT nArgs )
71 { assert( FALSE ); }
73 LONG CALLBACK CallTo16Long( FARPROC16 target, INT nArgs )
74 { assert( FALSE ); }
76 LONG CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, INT nArgs )
77 { assert( FALSE ); }
79 LONG CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, INT nArgs )
80 { assert( FALSE ); }
82 WORD CallFrom16Word( void )
83 { assert( FALSE ); }
85 LONG CallFrom16Long( void )
86 { assert( FALSE ); }
88 void CallFrom16Register( void )
89 { assert( FALSE ); }
91 void CallFrom16Thunk( void )
92 { assert( FALSE ); }
94 DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi )
95 { assert( FALSE ); }
97 DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs )
98 { assert( FALSE ); }
99 #endif
102 /* from relay32/relay386.c */
103 extern char **debug_relay_excludelist,**debug_relay_includelist;
105 /***********************************************************************
106 * RELAY_DebugCallFrom16
108 void RELAY_DebugCallFrom16( CONTEXT86 *context )
110 STACK16FRAME *frame;
111 WORD ordinal;
112 char *args16, funstr[80];
113 const char *args;
114 int i, usecdecl, reg_func;
116 if (!TRACE_ON(relay)) return;
118 frame = CURRENT_STACK16;
119 args = BUILTIN_GetEntryPoint16( frame, funstr, &ordinal );
120 if (!args) return; /* happens for the two snoop register relays */
121 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
122 DPRINTF( "Call %s(",funstr);
123 VA_START16( args16 );
125 usecdecl = ( *args == 'c' );
126 args += 2;
127 reg_func = ( memcmp( args, "regs_", 5 ) == 0
128 || memcmp( args, "intr_", 5 ) == 0 );
129 args += 5;
131 if (usecdecl)
133 while (*args)
135 switch(*args)
137 case 'w':
138 case 's':
139 DPRINTF( "0x%04x", *(WORD *)args16 );
140 args16 += 2;
141 break;
142 case 'l':
143 DPRINTF( "0x%08x", *(int *)args16 );
144 args16 += 4;
145 break;
146 case 't':
147 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
148 if (HIWORD(*(SEGPTR *)args16))
149 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
150 args16 += 4;
151 break;
152 case 'p':
153 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
154 args16 += 4;
155 break;
156 case 'T':
157 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
158 if (HIWORD( *(SEGPTR *)args16 ))
159 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
160 args16 += 4;
161 break;
163 args++;
164 if (*args) DPRINTF( "," );
167 else /* not cdecl */
169 /* Start with the last arg */
170 for (i = 0; args[i]; i++)
172 switch(args[i])
174 case 'w':
175 case 's':
176 args16 += 2;
177 break;
178 case 'l':
179 case 'p':
180 case 't':
181 case 'T':
182 args16 += 4;
183 break;
187 while (*args)
189 switch(*args)
191 case 'w':
192 case 's':
193 args16 -= 2;
194 DPRINTF( "0x%04x", *(WORD *)args16 );
195 break;
196 case 'l':
197 args16 -= 4;
198 DPRINTF( "0x%08x", *(int *)args16 );
199 break;
200 case 't':
201 args16 -= 4;
202 DPRINTF( "0x%08x", *(int *)args16 );
203 if (HIWORD(*(SEGPTR *)args16))
204 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
205 break;
206 case 'p':
207 args16 -= 4;
208 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
209 break;
210 case 'T':
211 args16 -= 4;
212 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
213 if (HIWORD( *(SEGPTR *)args16 ))
214 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
215 break;
217 args++;
218 if (*args) DPRINTF( "," );
222 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
223 VA_END16( args16 );
225 if (reg_func)
226 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
227 AX_reg(context), BX_reg(context), CX_reg(context),
228 DX_reg(context), SI_reg(context), DI_reg(context),
229 (WORD)ES_reg(context), EFL_reg(context) );
231 SYSLEVEL_CheckNotLevel( 2 );
235 /***********************************************************************
236 * RELAY_DebugCallFrom16Ret
238 void RELAY_DebugCallFrom16Ret( CONTEXT86 *context, int ret_val )
240 STACK16FRAME *frame;
241 WORD ordinal;
242 char funstr[80];
243 const char *args;
245 if (!TRACE_ON(relay)) return;
246 frame = CURRENT_STACK16;
247 args = BUILTIN_GetEntryPoint16( frame, funstr, &ordinal );
248 if (!args) return;
249 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
250 DPRINTF( "Ret %s() ",funstr);
252 if ( memcmp( args+2, "long_", 5 ) == 0 )
254 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
255 ret_val, frame->cs, frame->ip, frame->ds );
257 else if ( memcmp( args+2, "word_", 5 ) == 0 )
259 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
260 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
262 else if ( memcmp( args+2, "regs_", 5 ) == 0
263 || memcmp( args+2, "intr_", 5 ) == 0 )
265 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
266 (WORD)CS_reg(context), LOWORD(EIP_reg(context)), (WORD)DS_reg(context));
267 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
268 AX_reg(context), BX_reg(context), CX_reg(context),
269 DX_reg(context), SI_reg(context), DI_reg(context),
270 (WORD)ES_reg(context), EFL_reg(context) );
273 SYSLEVEL_CheckNotLevel( 2 );
277 /***********************************************************************
278 * RELAY_Unimplemented16
280 * This function is called for unimplemented 16-bit entry points (declared
281 * as 'stub' in the spec file).
283 void RELAY_Unimplemented16(void)
285 WORD ordinal;
286 char name[80];
287 STACK16FRAME *frame = CURRENT_STACK16;
288 BUILTIN_GetEntryPoint16( frame, name, &ordinal );
289 MESSAGE("No handler for Win16 routine %s (called from %04x:%04x)\n",
290 name, frame->cs, frame->ip );
291 ExitProcess(1);
295 /***********************************************************************
296 * RELAY_DebugCallTo16
298 * 'target' contains either the function to call (normal CallTo16)
299 * or a pointer to the CONTEXT86 struct (register CallTo16).
300 * 'nb_args' is the number of argument bytes on the 16-bit stack;
301 * 'reg_func' specifies whether we have a register CallTo16 or not.
303 void RELAY_DebugCallTo16( LPVOID target, int nb_args, BOOL reg_func )
305 WORD *stack16;
306 TEB *teb;
308 if (!TRACE_ON(relay)) return;
309 teb = NtCurrentTeb();
310 stack16 = (WORD *)THREAD_STACK16(teb);
312 nb_args /= sizeof(WORD);
314 if ( reg_func )
316 CONTEXT86 *context = (CONTEXT86 *)target;
318 DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
319 CS_reg(context), LOWORD(EIP_reg(context)), DS_reg(context) );
320 while (nb_args--) DPRINTF( ",0x%04x", *--stack16 );
321 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
322 OFFSETOF(teb->cur_stack) );
323 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
324 AX_reg(context), BX_reg(context), CX_reg(context),
325 DX_reg(context), SI_reg(context), DI_reg(context),
326 BP_reg(context), (WORD)ES_reg(context), (WORD)FS_reg(context) );
328 else
330 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
331 HIWORD(target), LOWORD(target), SELECTOROF(teb->cur_stack) );
332 while (nb_args--) DPRINTF( ",0x%04x", *--stack16 );
333 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
334 OFFSETOF(teb->cur_stack) );
337 SYSLEVEL_CheckNotLevel( 2 );
341 /***********************************************************************
342 * RELAY_DebugCallTo16Ret
344 void RELAY_DebugCallTo16Ret( int ret_val )
346 if (!TRACE_ON(relay)) return;
348 DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n",
349 SELECTOROF(NtCurrentTeb()->cur_stack),
350 OFFSETOF(NtCurrentTeb()->cur_stack), ret_val);
351 SYSLEVEL_CheckNotLevel( 2 );
355 /**********************************************************************
356 * Catch (KERNEL.55)
358 * Real prototype is:
359 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
361 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT86 *context )
363 /* Note: we don't save the current ss, as the catch buffer is */
364 /* only 9 words long. Hopefully no one will have the silly */
365 /* idea to change the current stack before calling Throw()... */
367 /* Windows uses:
368 * lpbuf[0] = ip
369 * lpbuf[1] = cs
370 * lpbuf[2] = sp
371 * lpbuf[3] = bp
372 * lpbuf[4] = si
373 * lpbuf[5] = di
374 * lpbuf[6] = ds
375 * lpbuf[7] = unused
376 * lpbuf[8] = ss
379 lpbuf[0] = LOWORD(EIP_reg(context));
380 lpbuf[1] = CS_reg(context);
381 /* Windows pushes 4 more words before saving sp */
382 lpbuf[2] = LOWORD(ESP_reg(context)) - 4 * sizeof(WORD);
383 lpbuf[3] = LOWORD(EBP_reg(context));
384 lpbuf[4] = LOWORD(ESI_reg(context));
385 lpbuf[5] = LOWORD(EDI_reg(context));
386 lpbuf[6] = DS_reg(context);
387 lpbuf[7] = 0;
388 lpbuf[8] = SS_reg(context);
389 AX_reg(context) = 0; /* Return 0 */
393 /**********************************************************************
394 * Throw (KERNEL.56)
396 * Real prototype is:
397 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
399 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT86 *context )
401 STACK16FRAME *pFrame;
402 STACK32FRAME *frame32;
403 TEB *teb = NtCurrentTeb();
405 AX_reg(context) = retval;
407 /* Find the frame32 corresponding to the frame16 we are jumping to */
408 pFrame = THREAD_STACK16(teb);
409 frame32 = pFrame->frame32;
410 while (frame32 && frame32->frame16)
412 if (OFFSETOF(frame32->frame16) < OFFSETOF(teb->cur_stack))
413 break; /* Something strange is going on */
414 if (OFFSETOF(frame32->frame16) > lpbuf[2])
416 /* We found the right frame */
417 pFrame->frame32 = frame32;
418 break;
420 frame32 = ((STACK16FRAME *)PTR_SEG_TO_LIN(frame32->frame16))->frame32;
423 EIP_reg(context) = lpbuf[0];
424 CS_reg(context) = lpbuf[1];
425 ESP_reg(context) = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
426 EBP_reg(context) = lpbuf[3];
427 ESI_reg(context) = lpbuf[4];
428 EDI_reg(context) = lpbuf[5];
429 DS_reg(context) = lpbuf[6];
431 if (lpbuf[8] != SS_reg(context))
432 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );