Moved implementation of WOW routines to single file.
[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 "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"
21
22 DEFAULT_DEBUG_CHANNEL(relay)
23
24
25 /***********************************************************************
26  *           RELAY_Init
27  */
28 BOOL RELAY_Init(void)
29 {
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 DWORD CallTo16_RetAddr;
39     extern DWORD CALL32_CBClient_RetAddr;
40     extern DWORD CALL32_CBClientEx_RetAddr;
41
42     codesel = GLOBAL_CreateBlock( GMEM_FIXED, (void *)Call16_Ret_Start,
43                                    (int)Call16_Ret_End - (int)Call16_Ret_Start,
44                                    0, TRUE, TRUE, FALSE, NULL );
45     if (!codesel) return FALSE;
46
47       /* Patch the return addresses for CallTo16 routines */
48
49     CallTo16_RetAddr = 
50         MAKELONG( (int)CallTo16_Ret -(int)Call16_Ret_Start, codesel );
51     CALL32_CBClient_RetAddr = 
52         MAKELONG( (int)CALL32_CBClient_Ret -(int)Call16_Ret_Start, codesel );
53     CALL32_CBClientEx_RetAddr = 
54         MAKELONG( (int)CALL32_CBClientEx_Ret -(int)Call16_Ret_Start, codesel );
55
56     /* Create built-in modules */
57     if (!BUILTIN_Init()) return FALSE;
58
59     /* Initialize thunking */
60     return THUNK_Init();
61 }
62
63
64 /* from relay32/relay386.c */
65 extern char **debug_relay_excludelist,**debug_relay_includelist;
66
67 /***********************************************************************
68  *           RELAY_DebugCallFrom16
69  */
70 void RELAY_DebugCallFrom16( CONTEXT86 *context )
71 {
72     STACK16FRAME *frame;
73     WORD ordinal;
74     char *args16, funstr[80];
75     const char *args;
76     int i, usecdecl, reg_func;
77
78     if (!TRACE_ON(relay)) return;
79
80     frame = CURRENT_STACK16;
81     args = BUILTIN_GetEntryPoint16( frame, funstr, &ordinal );
82     if (!args) return; /* happens for the two snoop register relays */
83     if (!RELAY_ShowDebugmsgRelay(funstr)) return;
84     DPRINTF( "Call %s(",funstr);
85     VA_START16( args16 );
86
87     usecdecl = ( *args == 'c' );
88     args += 2;
89     reg_func = (    memcmp( args, "regs_", 5 ) == 0
90                  || memcmp( args, "intr_", 5 ) == 0 );
91     args += 5;
92
93     if (usecdecl)
94     {
95         while (*args)
96         {
97             switch(*args)
98             {
99             case 'w':
100             case 's':
101                 DPRINTF( "0x%04x", *(WORD *)args16 );
102                 args16 += 2;
103                 break;
104             case 'l':
105                 DPRINTF( "0x%08x", *(int *)args16 );
106                 args16 += 4;
107                 break;
108             case 't':
109                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
110                 if (HIWORD(*(SEGPTR *)args16))
111                     debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
112                 args16 += 4;
113                 break;
114             case 'p':
115                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
116                 args16 += 4;
117                 break;
118             case 'T':
119                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
120                 if (HIWORD( *(SEGPTR *)args16 ))
121                     debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
122                 args16 += 4;
123                 break;
124             }
125             args++;
126             if (*args) DPRINTF( "," );
127         }
128     }
129     else  /* not cdecl */
130     {
131         /* Start with the last arg */
132         for (i = 0; args[i]; i++)
133         {
134             switch(args[i])
135             {
136             case 'w':
137             case 's':
138                 args16 += 2;
139                 break;
140             case 'l':
141             case 'p':
142             case 't':
143             case 'T':
144                 args16 += 4;
145                 break;
146             }
147         }
148
149         while (*args)
150         {
151             switch(*args)
152             {
153             case 'w':
154             case 's':
155                 args16 -= 2;
156                 DPRINTF( "0x%04x", *(WORD *)args16 );
157                 break;
158             case 'l':
159                 args16 -= 4;
160                 DPRINTF( "0x%08x", *(int *)args16 );
161                 break;
162             case 't':
163                 args16 -= 4;
164                 DPRINTF( "0x%08x", *(int *)args16 );
165                 if (HIWORD(*(SEGPTR *)args16))
166                     debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
167                 break;
168             case 'p':
169                 args16 -= 4;
170                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
171                 break;
172             case 'T':
173                 args16 -= 4;
174                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
175                 if (HIWORD( *(SEGPTR *)args16 ))
176                     debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
177                 break;
178             }
179             args++;
180             if (*args) DPRINTF( "," );
181         }
182     }
183
184     DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
185     VA_END16( args16 );
186
187     if (reg_func)
188         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
189                 AX_reg(context), BX_reg(context), CX_reg(context),
190                 DX_reg(context), SI_reg(context), DI_reg(context),
191                 (WORD)ES_reg(context), EFL_reg(context) );
192
193     SYSLEVEL_CheckNotLevel( 2 );
194 }
195
196
197 /***********************************************************************
198  *           RELAY_DebugCallFrom16Ret
199  */
200 void RELAY_DebugCallFrom16Ret( CONTEXT86 *context, int ret_val )
201 {
202     STACK16FRAME *frame;
203     WORD ordinal;
204     char funstr[80];
205     const char *args;
206
207     if (!TRACE_ON(relay)) return;
208     frame = CURRENT_STACK16;
209     args = BUILTIN_GetEntryPoint16( frame, funstr, &ordinal );
210     if (!args) return;
211     if (!RELAY_ShowDebugmsgRelay(funstr)) return;
212     DPRINTF( "Ret  %s() ",funstr);
213
214     if ( memcmp( args+2, "long_", 5 ) == 0 )
215     {
216         DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
217                  ret_val, frame->cs, frame->ip, frame->ds );
218     }
219     else if ( memcmp( args+2, "word_", 5 ) == 0 )
220     {
221         DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
222                  ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
223     }
224     else if (    memcmp( args+2, "regs_", 5 ) == 0 
225               || memcmp( args+2, "intr_", 5 ) == 0 )
226     {
227         DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
228                 (WORD)CS_reg(context), LOWORD(EIP_reg(context)), (WORD)DS_reg(context));
229         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
230                 AX_reg(context), BX_reg(context), CX_reg(context),
231                 DX_reg(context), SI_reg(context), DI_reg(context),
232                 (WORD)ES_reg(context), EFL_reg(context) );
233     }
234
235     SYSLEVEL_CheckNotLevel( 2 );
236 }
237
238
239 /***********************************************************************
240  *           RELAY_Unimplemented16
241  *
242  * This function is called for unimplemented 16-bit entry points (declared
243  * as 'stub' in the spec file).
244  */
245 void RELAY_Unimplemented16(void)
246 {
247     WORD ordinal;
248     char name[80];
249     STACK16FRAME *frame = CURRENT_STACK16;
250     BUILTIN_GetEntryPoint16( frame, name, &ordinal );
251     MESSAGE("No handler for Win16 routine %s (called from %04x:%04x)\n",
252             name, frame->cs, frame->ip );
253     ExitProcess(1);
254 }
255
256
257 /***********************************************************************
258  *           RELAY_DebugCallTo16
259  *
260  * 'target' contains either the function to call (normal CallTo16)
261  * or a pointer to the CONTEXT86 struct (register CallTo16).
262  * 'nb_args' is the number of argument bytes on the 16-bit stack; 
263  * 'reg_func' specifies whether we have a register CallTo16 or not.
264  */
265 void RELAY_DebugCallTo16( LPVOID target, int nb_args, BOOL reg_func )
266 {
267     WORD *stack16;
268     TEB *teb;
269
270     if (!TRACE_ON(relay)) return;
271     teb = NtCurrentTeb();
272     stack16 = (WORD *)THREAD_STACK16(teb);
273
274     nb_args /= sizeof(WORD);
275
276     if ( reg_func )
277     {
278         CONTEXT86 *context = (CONTEXT86 *)target;
279
280         DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
281                 CS_reg(context), LOWORD(EIP_reg(context)), DS_reg(context) );
282         while (nb_args--) DPRINTF( ",0x%04x", *--stack16 );
283         DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
284                 OFFSETOF(teb->cur_stack) );
285         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
286                 AX_reg(context), BX_reg(context), CX_reg(context),
287                 DX_reg(context), SI_reg(context), DI_reg(context),
288                 BP_reg(context), (WORD)ES_reg(context), (WORD)FS_reg(context) );
289     }
290     else
291     {
292         DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
293                 HIWORD(target), LOWORD(target), SELECTOROF(teb->cur_stack) );
294         while (nb_args--) DPRINTF( ",0x%04x", *--stack16 );
295         DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
296                 OFFSETOF(teb->cur_stack) );
297     }
298
299     SYSLEVEL_CheckNotLevel( 2 );
300 }
301
302
303 /***********************************************************************
304  *           RELAY_DebugCallTo16Ret
305  */
306 void RELAY_DebugCallTo16Ret( int ret_val )
307 {
308     if (!TRACE_ON(relay)) return;
309
310     DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n", 
311             SELECTOROF(NtCurrentTeb()->cur_stack),
312             OFFSETOF(NtCurrentTeb()->cur_stack), ret_val);
313     SYSLEVEL_CheckNotLevel( 2 );
314 }
315
316
317 /**********************************************************************
318  *           Catch    (KERNEL.55)
319  *
320  * Real prototype is:
321  *   INT16 WINAPI Catch( LPCATCHBUF lpbuf );
322  */
323 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT86 *context )
324 {
325     /* Note: we don't save the current ss, as the catch buffer is */
326     /* only 9 words long. Hopefully no one will have the silly    */
327     /* idea to change the current stack before calling Throw()... */
328
329     /* Windows uses:
330      * lpbuf[0] = ip
331      * lpbuf[1] = cs
332      * lpbuf[2] = sp
333      * lpbuf[3] = bp
334      * lpbuf[4] = si
335      * lpbuf[5] = di
336      * lpbuf[6] = ds
337      * lpbuf[7] = unused
338      * lpbuf[8] = ss
339      */
340
341     lpbuf[0] = LOWORD(EIP_reg(context));
342     lpbuf[1] = CS_reg(context);
343     /* Windows pushes 4 more words before saving sp */
344     lpbuf[2] = LOWORD(ESP_reg(context)) - 4 * sizeof(WORD);
345     lpbuf[3] = LOWORD(EBP_reg(context));
346     lpbuf[4] = LOWORD(ESI_reg(context));
347     lpbuf[5] = LOWORD(EDI_reg(context));
348     lpbuf[6] = DS_reg(context);
349     lpbuf[7] = 0;
350     lpbuf[8] = SS_reg(context);
351     AX_reg(context) = 0;  /* Return 0 */
352 }
353
354
355 /**********************************************************************
356  *           Throw    (KERNEL.56)
357  *
358  * Real prototype is:
359  *   INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
360  */
361 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT86 *context )
362 {
363     STACK16FRAME *pFrame;
364     STACK32FRAME *frame32;
365     TEB *teb = NtCurrentTeb();
366
367     AX_reg(context) = retval;
368
369     /* Find the frame32 corresponding to the frame16 we are jumping to */
370     pFrame = THREAD_STACK16(teb);
371     frame32 = pFrame->frame32;
372     while (frame32 && frame32->frame16)
373     {
374         if (OFFSETOF(frame32->frame16) < OFFSETOF(teb->cur_stack))
375             break;  /* Something strange is going on */
376         if (OFFSETOF(frame32->frame16) > lpbuf[2])
377         {
378             /* We found the right frame */
379             pFrame->frame32 = frame32;
380             break;
381         }
382         frame32 = ((STACK16FRAME *)PTR_SEG_TO_LIN(frame32->frame16))->frame32;
383     }
384
385     EIP_reg(context) = lpbuf[0];
386     CS_reg(context)  = lpbuf[1];
387     ESP_reg(context) = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
388     EBP_reg(context) = lpbuf[3];
389     ESI_reg(context) = lpbuf[4];
390     EDI_reg(context) = lpbuf[5];
391     DS_reg(context)  = lpbuf[6];
392
393     if (lpbuf[8] != SS_reg(context))
394         ERR("Switching stack segment with Throw() not supported; expect crash now\n" );
395 }
396