Patch flat cs of 16-bit entry points if current %cs is different from
[wine] / if1632 / relay.c
1 /*
2  * Copyright 1993 Robert J. Amstadt
3  * Copyright 1995 Alexandre Julliard
4  */
5
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 "task.h"
17 #include "syslevel.h"
18 #include "debugtools.h"
19 #include "main.h"
20 #include "callback.h"
21
22 DEFAULT_DEBUG_CHANNEL(relay);
23
24 /***********************************************************************
25  *           RELAY_Init
26  */
27 BOOL RELAY_Init(void)
28 {
29 #ifdef __i386__
30     WORD codesel;
31
32       /* Allocate the code selector for CallTo16 routines */
33
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 SEGPTR CallTo16_RetAddr;
39     extern DWORD CallTo16_DataSelector;
40     extern SEGPTR CALL32_CBClient_RetAddr;
41     extern SEGPTR CALL32_CBClientEx_RetAddr;
42
43     codesel = SELECTOR_AllocBlock( (void *)Call16_Ret_Start,
44                                    (char *)Call16_Ret_End - (char *)Call16_Ret_Start,
45                                    SEGMENT_CODE, TRUE, FALSE );
46     if (!codesel) return FALSE;
47
48       /* Patch the return addresses for CallTo16 routines */
49
50     CallTo16_DataSelector = __get_ds();
51     CallTo16_RetAddr = 
52         PTR_SEG_OFF_TO_SEGPTR( codesel, (char*)CallTo16_Ret - (char*)Call16_Ret_Start );
53     CALL32_CBClient_RetAddr = 
54         PTR_SEG_OFF_TO_SEGPTR( codesel, (char*)CALL32_CBClient_Ret - (char*)Call16_Ret_Start );
55     CALL32_CBClientEx_RetAddr = 
56         PTR_SEG_OFF_TO_SEGPTR( codesel, (char*)CALL32_CBClientEx_Ret - (char*)Call16_Ret_Start );
57 #endif
58     return TRUE;
59 }
60
61 /*
62  * Stubs for the CallTo16/CallFrom16 routines on non-Intel architectures
63  * (these will never be called but need to be present to satisfy the linker ...)
64  */
65 #ifndef __i386__
66 WORD CALLBACK CallTo16Word( FARPROC16 target, INT nArgs )
67 { assert( FALSE ); }
68
69 LONG CALLBACK CallTo16Long( FARPROC16 target, INT nArgs )
70 { assert( FALSE ); }
71
72 void CALLBACK CallTo16RegisterShort( CONTEXT86 *context, INT nArgs )
73 { assert( FALSE ); }
74
75 void CALLBACK CallTo16RegisterLong ( CONTEXT86 *context, INT nArgs )
76 { assert( FALSE ); }
77
78 WORD CallFrom16Word( void )       
79 { assert( FALSE ); }
80
81 LONG CallFrom16Long( void )       
82 { assert( FALSE ); }
83
84 void CallFrom16Register( void )   
85 { assert( FALSE ); }
86
87 void CallFrom16Thunk( void )      
88 { assert( FALSE ); }
89
90 DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi )
91 { assert( FALSE ); }
92
93 DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs )
94 { assert( FALSE ); }
95 #endif
96
97
98 /* from relay32/relay386.c */
99 extern char **debug_relay_excludelist,**debug_relay_includelist;
100
101 /***********************************************************************
102  *           RELAY_DebugCallFrom16
103  */
104 void RELAY_DebugCallFrom16( CONTEXT86 *context )
105 {
106     STACK16FRAME *frame;
107     WORD ordinal;
108     char *args16, funstr[80];
109     const char *args;
110     int i, usecdecl, reg_func;
111
112     if (!TRACE_ON(relay)) return;
113
114     frame = CURRENT_STACK16;
115     args = BUILTIN_GetEntryPoint16( frame, funstr, &ordinal );
116     if (!args) return; /* happens for the two snoop register relays */
117     if (!RELAY_ShowDebugmsgRelay(funstr)) return;
118     DPRINTF( "Call %s(",funstr);
119     VA_START16( args16 );
120
121     usecdecl = ( *args == 'c' );
122     args += 2;
123     reg_func = (    memcmp( args, "regs_", 5 ) == 0
124                  || memcmp( args, "intr_", 5 ) == 0 );
125     args += 5;
126
127     if (usecdecl)
128     {
129         while (*args)
130         {
131             switch(*args)
132             {
133             case 'w':
134             case 's':
135                 DPRINTF( "0x%04x", *(WORD *)args16 );
136                 args16 += 2;
137                 break;
138             case 'l':
139                 DPRINTF( "0x%08x", *(int *)args16 );
140                 args16 += 4;
141                 break;
142             case 'p':
143                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
144                 args16 += 4;
145                 break;
146             case 't':
147             case 'T':
148                 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
149                          debugres_a( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 )) );
150                 args16 += 4;
151                 break;
152             }
153             args++;
154             if (*args) DPRINTF( "," );
155         }
156     }
157     else  /* not cdecl */
158     {
159         /* Start with the last arg */
160         for (i = 0; args[i]; i++)
161         {
162             switch(args[i])
163             {
164             case 'w':
165             case 's':
166                 args16 += 2;
167                 break;
168             case 'l':
169             case 'p':
170             case 't':
171             case 'T':
172                 args16 += 4;
173                 break;
174             }
175         }
176
177         while (*args)
178         {
179             switch(*args)
180             {
181             case 'w':
182             case 's':
183                 args16 -= 2;
184                 DPRINTF( "0x%04x", *(WORD *)args16 );
185                 break;
186             case 'l':
187                 args16 -= 4;
188                 DPRINTF( "0x%08x", *(int *)args16 );
189                 break;
190             case 't':
191                 args16 -= 4;
192                 DPRINTF( "0x%08x %s", *(int *)args16,
193                          debugres_a( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 )));
194                 break;
195             case 'p':
196                 args16 -= 4;
197                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
198                 break;
199             case 'T':
200                 args16 -= 4;
201                 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
202                          debugres_a( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 )));
203                 break;
204             }
205             args++;
206             if (*args) DPRINTF( "," );
207         }
208     }
209
210     DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
211     VA_END16( args16 );
212
213     if (reg_func)
214         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
215                 AX_reg(context), BX_reg(context), CX_reg(context),
216                 DX_reg(context), SI_reg(context), DI_reg(context),
217                 (WORD)context->SegEs, context->EFlags );
218
219     SYSLEVEL_CheckNotLevel( 2 );
220 }
221
222
223 /***********************************************************************
224  *           RELAY_DebugCallFrom16Ret
225  */
226 void RELAY_DebugCallFrom16Ret( CONTEXT86 *context, int ret_val )
227 {
228     STACK16FRAME *frame;
229     WORD ordinal;
230     char funstr[80];
231     const char *args;
232
233     if (!TRACE_ON(relay)) return;
234     frame = CURRENT_STACK16;
235     args = BUILTIN_GetEntryPoint16( frame, funstr, &ordinal );
236     if (!args) return;
237     if (!RELAY_ShowDebugmsgRelay(funstr)) return;
238     DPRINTF( "Ret  %s() ",funstr);
239
240     if ( memcmp( args+2, "long_", 5 ) == 0 )
241     {
242         DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
243                  ret_val, frame->cs, frame->ip, frame->ds );
244     }
245     else if ( memcmp( args+2, "word_", 5 ) == 0 )
246     {
247         DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
248                  ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
249     }
250     else if (    memcmp( args+2, "regs_", 5 ) == 0 
251               || memcmp( args+2, "intr_", 5 ) == 0 )
252     {
253         DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
254                 (WORD)context->SegCs, LOWORD(context->Eip), (WORD)context->SegDs);
255         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
256                 AX_reg(context), BX_reg(context), CX_reg(context),
257                 DX_reg(context), SI_reg(context), DI_reg(context),
258                 (WORD)context->SegEs, context->EFlags );
259     }
260
261     SYSLEVEL_CheckNotLevel( 2 );
262 }
263
264
265 /***********************************************************************
266  *           RELAY_Unimplemented16
267  *
268  * This function is called for unimplemented 16-bit entry points (declared
269  * as 'stub' in the spec file).
270  */
271 void RELAY_Unimplemented16(void)
272 {
273     WORD ordinal;
274     char name[80];
275     STACK16FRAME *frame = CURRENT_STACK16;
276     BUILTIN_GetEntryPoint16( frame, name, &ordinal );
277     MESSAGE("FATAL: No handler for Win16 routine %s (called from %04x:%04x)\n",
278             name, frame->cs, frame->ip );
279     ExitProcess(1);
280 }
281
282
283 /***********************************************************************
284  *           RELAY_DebugCallTo16
285  *
286  * 'target' contains either the function to call (normal CallTo16)
287  * or a pointer to the CONTEXT86 struct (register CallTo16).
288  * 'nb_args' is the number of argument bytes on the 16-bit stack; 
289  * 'reg_func' specifies whether we have a register CallTo16 or not.
290  */
291 void RELAY_DebugCallTo16( LPVOID target, int nb_args, BOOL reg_func )
292 {
293     WORD *stack16;
294     TEB *teb;
295
296     if (!TRACE_ON(relay)) return;
297     teb = NtCurrentTeb();
298     stack16 = (WORD *)THREAD_STACK16(teb);
299
300     nb_args /= sizeof(WORD);
301
302     if ( reg_func )
303     {
304         CONTEXT86 *context = (CONTEXT86 *)target;
305
306         DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
307                 context->SegCs, LOWORD(context->Eip), context->SegDs );
308         while (nb_args--) DPRINTF( ",0x%04x", *--stack16 );
309         DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
310                 OFFSETOF(teb->cur_stack) );
311         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
312                 AX_reg(context), BX_reg(context), CX_reg(context),
313                 DX_reg(context), SI_reg(context), DI_reg(context),
314                 BP_reg(context), (WORD)context->SegEs, (WORD)context->SegFs );
315     }
316     else
317     {
318         DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
319                 HIWORD(target), LOWORD(target), SELECTOROF(teb->cur_stack) );
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     }
324
325     SYSLEVEL_CheckNotLevel( 2 );
326 }
327
328
329 /***********************************************************************
330  *           RELAY_DebugCallTo16Ret
331  */
332 void RELAY_DebugCallTo16Ret( BOOL reg_func, int ret_val )
333 {
334     if (!TRACE_ON(relay)) return;
335
336     if (!reg_func)
337     {
338         DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n", 
339                 SELECTOROF(NtCurrentTeb()->cur_stack),
340                 OFFSETOF(NtCurrentTeb()->cur_stack), ret_val);
341     }
342     else
343     {
344         CONTEXT86 *context = (CONTEXT86 *)ret_val;
345
346         DPRINTF("CallTo16() ss:sp=%04x:%04x\n", 
347                 SELECTOROF(NtCurrentTeb()->cur_stack),
348                 OFFSETOF(NtCurrentTeb()->cur_stack));
349         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x BP=%04x SP=%04x\n",
350                 AX_reg(context), BX_reg(context), CX_reg(context),
351                 DX_reg(context), BP_reg(context), LOWORD(context->Esp));
352     }
353
354     SYSLEVEL_CheckNotLevel( 2 );
355 }