Moved the selector access functions out of winnt.h into libwine.
[wine] / if1632 / relay.c
1 /*
2  * Copyright 1993 Robert J. Amstadt
3  * Copyright 1995 Alexandre Julliard
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24
25 #include "wine/winbase16.h"
26 #include "winnt.h"
27 #include "module.h"
28 #include "stackframe.h"
29 #include "selectors.h"
30 #include "builtin16.h"
31 #include "syslevel.h"
32 #include "wine/library.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(relay);
36
37 /***********************************************************************
38  *           RELAY_Init
39  */
40 BOOL RELAY_Init(void)
41 {
42 #ifdef __i386__
43     WORD codesel;
44
45       /* Allocate the code selector for CallTo16 routines */
46
47     extern void Call16_Ret_Start(), Call16_Ret_End();
48     extern void CallTo16_Ret();
49     extern void CALL32_CBClient_Ret();
50     extern void CALL32_CBClientEx_Ret();
51     extern SEGPTR CallTo16_RetAddr;
52     extern DWORD CallTo16_DataSelector;
53     extern SEGPTR CALL32_CBClient_RetAddr;
54     extern SEGPTR CALL32_CBClientEx_RetAddr;
55
56     codesel = SELECTOR_AllocBlock( (void *)Call16_Ret_Start,
57                                    (char *)Call16_Ret_End - (char *)Call16_Ret_Start,
58                                    WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
59     if (!codesel) return FALSE;
60
61       /* Patch the return addresses for CallTo16 routines */
62
63     CallTo16_DataSelector = wine_get_ds();
64     CallTo16_RetAddr = 
65         MAKESEGPTR( codesel, (char*)CallTo16_Ret - (char*)Call16_Ret_Start );
66     CALL32_CBClient_RetAddr = 
67         MAKESEGPTR( codesel, (char*)CALL32_CBClient_Ret - (char*)Call16_Ret_Start );
68     CALL32_CBClientEx_RetAddr = 
69         MAKESEGPTR( codesel, (char*)CALL32_CBClientEx_Ret - (char*)Call16_Ret_Start );
70 #endif
71     return TRUE;
72 }
73
74 /*
75  * Stubs for the CallTo16/CallFrom16 routines on non-Intel architectures
76  * (these will never be called but need to be present to satisfy the linker ...)
77  */
78 #ifndef __i386__
79 /***********************************************************************
80  *              wine_call_to_16_word (KERNEL32.@)
81  */
82 WORD WINAPI wine_call_to_16_word( FARPROC16 target, INT nArgs )
83 {
84     assert( FALSE );
85 }
86
87 /***********************************************************************
88  *              wine_call_to_16_long (KERNEL32.@)
89  */
90 LONG WINAPI wine_call_to_16_long( FARPROC16 target, INT nArgs )
91 {
92     assert( FALSE );
93 }
94
95 /***********************************************************************
96  *              wine_call_to_16_regs_short (KERNEL32.@)
97  */
98 void WINAPI wine_call_to_16_regs_short( CONTEXT86 *context, INT nArgs )
99 {
100     assert( FALSE );
101 }
102
103 /***********************************************************************
104  *              wine_call_to_16_regs_long (KERNEL32.@)
105  */
106 void WINAPI wine_call_to_16_regs_long ( CONTEXT86 *context, INT nArgs )
107 {
108     assert( FALSE );
109 }
110
111 /***********************************************************************
112  *              __wine_call_from_16_word (KERNEL32.@)
113  */
114 WORD __wine_call_from_16_word()
115 {
116     assert( FALSE );
117 }
118
119 /***********************************************************************
120  *              __wine_call_from_16_long (KERNEL32.@)
121  */
122 LONG __wine_call_from_16_long()
123 {
124     assert( FALSE );
125 }
126
127 /***********************************************************************
128  *              __wine_call_from_16_regs (KERNEL32.@)
129  */
130 void __wine_call_from_16_regs()
131 {
132     assert( FALSE );
133 }
134
135 DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi )
136 { assert( FALSE ); }
137
138 DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs )
139 { assert( FALSE ); }
140 #endif
141
142
143 /* from relay32/relay386.c */
144 extern char **debug_relay_excludelist,**debug_relay_includelist;
145 extern int RELAY_ShowDebugmsgRelay(const char *func);
146
147
148 /***********************************************************************
149  *           get_entry_point
150  *
151  * Return the ordinal, name, and type info corresponding to a CS:IP address.
152  */
153 static const CALLFROM16 *get_entry_point( STACK16FRAME *frame, LPSTR name, WORD *pOrd )
154 {
155     WORD i, max_offset;
156     register BYTE *p;
157     NE_MODULE *pModule;
158     ET_BUNDLE *bundle;
159     ET_ENTRY *entry;
160
161     if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16( frame->module_cs ) ))))
162         return NULL;
163
164     max_offset = 0;
165     *pOrd = 0;
166     bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
167     do 
168     {
169         entry = (ET_ENTRY *)((BYTE *)bundle+6);
170         for (i = bundle->first + 1; i <= bundle->last; i++)
171         {
172             if ((entry->offs < frame->entry_ip)
173             && (entry->segnum == 1) /* code segment ? */
174             && (entry->offs >= max_offset))
175             {
176                 max_offset = entry->offs;
177                 *pOrd = i;
178             }
179             entry++;
180         }
181     } while ( (bundle->next)
182            && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
183
184     /* Search for the name in the resident names table */
185     /* (built-in modules have no non-resident table)   */
186     
187     p = (BYTE *)pModule + pModule->name_table;
188     while (*p)
189     {
190         p += *p + 1 + sizeof(WORD);
191         if (*(WORD *)(p + *p + 1) == *pOrd) break;
192     }
193
194     sprintf( name, "%.*s.%d: %.*s",
195              *((BYTE *)pModule + pModule->name_table),
196              (char *)pModule + pModule->name_table + 1,
197              *pOrd, *p, (char *)(p + 1) );
198
199     /* Retrieve entry point call structure */
200     p = MapSL( MAKESEGPTR( frame->module_cs, frame->callfrom_ip ) );
201     /* p now points to lret, get the start of CALLFROM16 structure */
202     return (CALLFROM16 *)(p - (BYTE *)&((CALLFROM16 *)0)->lret);
203 }
204
205
206 /***********************************************************************
207  *           RELAY_DebugCallFrom16
208  */
209 void RELAY_DebugCallFrom16( CONTEXT86 *context )
210 {
211     STACK16FRAME *frame;
212     WORD ordinal;
213     char *args16, funstr[80];
214     const CALLFROM16 *call;
215     int i;
216
217     if (!TRACE_ON(relay)) return;
218
219     frame = CURRENT_STACK16;
220     call = get_entry_point( frame, funstr, &ordinal );
221     if (!call) return; /* happens for the two snoop register relays */
222     if (!RELAY_ShowDebugmsgRelay(funstr)) return;
223     DPRINTF( "%08lx:Call %s(",GetCurrentThreadId(),funstr);
224     VA_START16( args16 );
225
226     if (call->lret == 0xcb66)  /* cdecl */
227     {
228         for (i = 0; i < 20; i++)
229         {
230             int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
231
232             if (type == ARG_NONE) break;
233             if (i) DPRINTF( "," );
234             switch(type)
235             {
236             case ARG_WORD:
237             case ARG_SWORD:
238                 DPRINTF( "%04x", *(WORD *)args16 );
239                 args16 += sizeof(WORD);
240                 break;
241             case ARG_LONG:
242                 DPRINTF( "%08x", *(int *)args16 );
243                 args16 += sizeof(int);
244                 break;
245             case ARG_PTR:
246                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
247                 args16 += sizeof(SEGPTR);
248                 break;
249             case ARG_STR:
250                 DPRINTF( "%08x %s", *(int *)args16,
251                          debugres_a( MapSL(*(SEGPTR *)args16 )));
252                 args16 += sizeof(int);
253                 break;
254             case ARG_SEGSTR:
255                 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
256                          debugres_a( MapSL(*(SEGPTR *)args16 )) );
257                 args16 += sizeof(SEGPTR);
258                 break;
259             default:
260                 break;
261             }
262         }
263     }
264     else  /* not cdecl */
265     {
266         /* Start with the last arg */
267         args16 += call->nArgs;
268         for (i = 0; i < 20; i++)
269         {
270             int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
271
272             if (type == ARG_NONE) break;
273             if (i) DPRINTF( "," );
274             switch(type)
275             {
276             case ARG_WORD:
277             case ARG_SWORD:
278                 args16 -= sizeof(WORD);
279                 DPRINTF( "%04x", *(WORD *)args16 );
280                 break;
281             case ARG_LONG:
282                 args16 -= sizeof(int);
283                 DPRINTF( "%08x", *(int *)args16 );
284                 break;
285             case ARG_PTR:
286                 args16 -= sizeof(SEGPTR);
287                 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
288                 break;
289             case ARG_STR:
290                 args16 -= sizeof(int);
291                 DPRINTF( "%08x %s", *(int *)args16,
292                          debugres_a( MapSL(*(SEGPTR *)args16 )));
293                 break;
294             case ARG_SEGSTR:
295                 args16 -= sizeof(SEGPTR);
296                 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
297                          debugres_a( MapSL(*(SEGPTR *)args16 )) );
298                 break;
299             default:
300                 break;
301             }
302         }
303     }
304
305     DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
306     VA_END16( args16 );
307
308     if (call->arg_types[0] & ARG_REGISTER)
309         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
310                 AX_reg(context), BX_reg(context), CX_reg(context),
311                 DX_reg(context), SI_reg(context), DI_reg(context),
312                 (WORD)context->SegEs, context->EFlags );
313
314     SYSLEVEL_CheckNotLevel( 2 );
315 }
316
317
318 /***********************************************************************
319  *           RELAY_DebugCallFrom16Ret
320  */
321 void RELAY_DebugCallFrom16Ret( CONTEXT86 *context, int ret_val )
322 {
323     STACK16FRAME *frame;
324     WORD ordinal;
325     char funstr[80];
326     const CALLFROM16 *call;
327
328     if (!TRACE_ON(relay)) return;
329     frame = CURRENT_STACK16;
330     call = get_entry_point( frame, funstr, &ordinal );
331     if (!call) return;
332     if (!RELAY_ShowDebugmsgRelay(funstr)) return;
333     DPRINTF( "%08lx:Ret  %s() ",GetCurrentThreadId(),funstr);
334
335     if (call->arg_types[0] & ARG_REGISTER)
336     {
337         DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
338                 (WORD)context->SegCs, LOWORD(context->Eip), (WORD)context->SegDs);
339         DPRINTF("     AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
340                 AX_reg(context), BX_reg(context), CX_reg(context),
341                 DX_reg(context), SI_reg(context), DI_reg(context),
342                 (WORD)context->SegEs, context->EFlags );
343     }
344     else if (call->arg_types[0] & ARG_RET16)
345     {
346         DPRINTF( "retval=%04x ret=%04x:%04x ds=%04x\n",
347                  ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
348     }
349     else
350     {
351         DPRINTF( "retval=%08x ret=%04x:%04x ds=%04x\n",
352                  ret_val, frame->cs, frame->ip, frame->ds );
353     }
354     SYSLEVEL_CheckNotLevel( 2 );
355 }
356
357
358 /***********************************************************************
359  *           RELAY_DebugCallTo16
360  *
361  * 'target' contains either the function to call (normal CallTo16)
362  * or a pointer to the CONTEXT86 struct (register CallTo16).
363  * 'nb_args' is the number of argument bytes on the 16-bit stack; 
364  * 'reg_func' specifies whether we have a register CallTo16 or not.
365  */
366 void RELAY_DebugCallTo16( LPVOID target, int nb_args, BOOL reg_func )
367 {
368     WORD *stack16;
369     TEB *teb;
370
371     if (!TRACE_ON(relay)) return;
372     teb = NtCurrentTeb();
373     stack16 = (WORD *)THREAD_STACK16(teb);
374
375     nb_args /= sizeof(WORD);
376
377     if ( reg_func )
378     {
379         CONTEXT86 *context = (CONTEXT86 *)target;
380
381         DPRINTF("%08lx:CallTo16(func=%04lx:%04x,ds=%04lx",
382                 GetCurrentThreadId(),
383                 context->SegCs, LOWORD(context->Eip), context->SegDs );
384         while (nb_args--) DPRINTF( ",%04x", *--stack16 );
385         DPRINTF(") ss:sp=%04x:%04x", SELECTOROF(teb->cur_stack),
386                 OFFSETOF(teb->cur_stack) );
387         DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x bp=%04x es=%04x fs=%04x\n",
388                 AX_reg(context), BX_reg(context), CX_reg(context),
389                 DX_reg(context), SI_reg(context), DI_reg(context),
390                 BP_reg(context), (WORD)context->SegEs, (WORD)context->SegFs );
391     }
392     else
393     {
394         DPRINTF("%08lx:CallTo16(func=%04x:%04x,ds=%04x",
395                 GetCurrentThreadId(),
396                 HIWORD(target), LOWORD(target), SELECTOROF(teb->cur_stack) );
397         while (nb_args--) DPRINTF( ",%04x", *--stack16 );
398         DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
399                 OFFSETOF(teb->cur_stack) );
400     }
401
402     SYSLEVEL_CheckNotLevel( 2 );
403 }
404
405
406 /***********************************************************************
407  *           RELAY_DebugCallTo16Ret
408  */
409 void RELAY_DebugCallTo16Ret( BOOL reg_func, int ret_val )
410 {
411     if (!TRACE_ON(relay)) return;
412
413     if (!reg_func)
414     {
415         DPRINTF("%08lx:RetFrom16() ss:sp=%04x:%04x retval=%08x\n",
416                 GetCurrentThreadId(),
417                 SELECTOROF(NtCurrentTeb()->cur_stack),
418                 OFFSETOF(NtCurrentTeb()->cur_stack), ret_val);
419     }
420     else
421     {
422         CONTEXT86 *context = (CONTEXT86 *)ret_val;
423
424         DPRINTF("%08lx:RetFrom16() ss:sp=%04x:%04x ",
425                 GetCurrentThreadId(),
426                 SELECTOROF(NtCurrentTeb()->cur_stack),
427                 OFFSETOF(NtCurrentTeb()->cur_stack));
428         DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x bp=%04x sp=%04x\n",
429                 AX_reg(context), BX_reg(context), CX_reg(context),
430                 DX_reg(context), BP_reg(context), LOWORD(context->Esp));
431     }
432
433     SYSLEVEL_CheckNotLevel( 2 );
434 }