Portability fix.
[wine/wine64.git] / if1632 / relay.c
blob1263cc24b59d1f4d5ce74de597923c9aaf322eff
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
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.
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.
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
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
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"
35 WINE_DEFAULT_DEBUG_CHANNEL(relay);
37 /***********************************************************************
38 * RELAY_Init
40 BOOL RELAY_Init(void)
42 #ifdef __i386__
43 WORD codesel;
45 /* Allocate the code selector for CallTo16 routines */
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;
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;
61 /* Patch the return addresses for CallTo16 routines */
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;
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 ...)
78 #ifndef __i386__
79 /***********************************************************************
80 * wine_call_to_16_word (KERNEL32.@)
82 WORD WINAPI wine_call_to_16_word( FARPROC16 target, INT nArgs )
84 assert( FALSE );
87 /***********************************************************************
88 * wine_call_to_16_long (KERNEL32.@)
90 LONG WINAPI wine_call_to_16_long( FARPROC16 target, INT nArgs )
92 assert( FALSE );
95 /***********************************************************************
96 * wine_call_to_16_regs_short (KERNEL32.@)
98 void WINAPI wine_call_to_16_regs_short( CONTEXT86 *context, INT nArgs )
100 assert( FALSE );
103 /***********************************************************************
104 * wine_call_to_16_regs_long (KERNEL32.@)
106 void WINAPI wine_call_to_16_regs_long ( CONTEXT86 *context, INT nArgs )
108 assert( FALSE );
111 /***********************************************************************
112 * __wine_call_from_16_word (KERNEL32.@)
114 WORD __wine_call_from_16_word()
116 assert( FALSE );
119 /***********************************************************************
120 * __wine_call_from_16_long (KERNEL32.@)
122 LONG __wine_call_from_16_long()
124 assert( FALSE );
127 /***********************************************************************
128 * __wine_call_from_16_regs (KERNEL32.@)
130 void __wine_call_from_16_regs()
132 assert( FALSE );
135 DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi )
136 { assert( FALSE ); }
138 DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs )
139 { assert( FALSE ); }
140 #endif
143 /* from relay32/relay386.c */
144 extern char **debug_relay_excludelist,**debug_relay_includelist;
145 extern int RELAY_ShowDebugmsgRelay(const char *func);
148 /***********************************************************************
149 * get_entry_point
151 * Return the ordinal, name, and type info corresponding to a CS:IP address.
153 static const CALLFROM16 *get_entry_point( STACK16FRAME *frame, LPSTR name, WORD *pOrd )
155 WORD i, max_offset;
156 register BYTE *p;
157 NE_MODULE *pModule;
158 ET_BUNDLE *bundle;
159 ET_ENTRY *entry;
161 if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16( frame->module_cs ) ))))
162 return NULL;
164 max_offset = 0;
165 *pOrd = 0;
166 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
169 entry = (ET_ENTRY *)((BYTE *)bundle+6);
170 for (i = bundle->first + 1; i <= bundle->last; i++)
172 if ((entry->offs < frame->entry_ip)
173 && (entry->segnum == 1) /* code segment ? */
174 && (entry->offs >= max_offset))
176 max_offset = entry->offs;
177 *pOrd = i;
179 entry++;
181 } while ( (bundle->next)
182 && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
184 /* Search for the name in the resident names table */
185 /* (built-in modules have no non-resident table) */
187 p = (BYTE *)pModule + pModule->name_table;
188 while (*p)
190 p += *p + 1 + sizeof(WORD);
191 if (*(WORD *)(p + *p + 1) == *pOrd) break;
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) );
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);
206 /***********************************************************************
207 * RELAY_DebugCallFrom16
209 void RELAY_DebugCallFrom16( CONTEXT86 *context )
211 STACK16FRAME *frame;
212 WORD ordinal;
213 char *args16, funstr[80];
214 const CALLFROM16 *call;
215 int i;
217 if (!TRACE_ON(relay)) return;
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 );
226 if (call->lret == 0xcb66) /* cdecl */
228 for (i = 0; i < 20; i++)
230 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
232 if (type == ARG_NONE) break;
233 if (i) DPRINTF( "," );
234 switch(type)
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 debugstr_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 debugstr_a( MapSL(*(SEGPTR *)args16 )) );
257 args16 += sizeof(SEGPTR);
258 break;
259 default:
260 break;
264 else /* not cdecl */
266 /* Start with the last arg */
267 args16 += call->nArgs;
268 for (i = 0; i < 20; i++)
270 int type = (call->arg_types[i / 10] >> (3 * (i % 10))) & 7;
272 if (type == ARG_NONE) break;
273 if (i) DPRINTF( "," );
274 switch(type)
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 debugstr_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 debugstr_a( MapSL(*(SEGPTR *)args16 )) );
298 break;
299 default:
300 break;
305 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
306 VA_END16( args16 );
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 );
314 SYSLEVEL_CheckNotLevel( 2 );
318 /***********************************************************************
319 * RELAY_DebugCallFrom16Ret
321 void RELAY_DebugCallFrom16Ret( CONTEXT86 *context, int ret_val )
323 STACK16FRAME *frame;
324 WORD ordinal;
325 char funstr[80];
326 const CALLFROM16 *call;
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);
335 if (call->arg_types[0] & ARG_REGISTER)
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 );
344 else if (call->arg_types[0] & ARG_RET16)
346 DPRINTF( "retval=%04x ret=%04x:%04x ds=%04x\n",
347 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
349 else
351 DPRINTF( "retval=%08x ret=%04x:%04x ds=%04x\n",
352 ret_val, frame->cs, frame->ip, frame->ds );
354 SYSLEVEL_CheckNotLevel( 2 );
358 /***********************************************************************
359 * RELAY_DebugCallTo16
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.
366 void RELAY_DebugCallTo16( LPVOID target, int nb_args, BOOL reg_func )
368 WORD *stack16;
369 TEB *teb;
371 if (!TRACE_ON(relay)) return;
372 teb = NtCurrentTeb();
373 stack16 = (WORD *)THREAD_STACK16(teb);
375 nb_args /= sizeof(WORD);
377 if ( reg_func )
379 CONTEXT86 *context = (CONTEXT86 *)target;
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 );
392 else
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) );
402 SYSLEVEL_CheckNotLevel( 2 );
406 /***********************************************************************
407 * RELAY_DebugCallTo16Ret
409 void RELAY_DebugCallTo16Ret( BOOL reg_func, int ret_val )
411 if (!TRACE_ON(relay)) return;
413 if (!reg_func)
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);
420 else
422 CONTEXT86 *context = (CONTEXT86 *)ret_val;
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));
433 SYSLEVEL_CheckNotLevel( 2 );