2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
11 #include "stackframe.h"
14 /* #define DEBUG_RELAY */
18 /* Make make_debug think these were really used */
23 /***********************************************************************
26 BOOL32 RELAY_Init(void)
30 /* Allocate the code selector for CallTo16 routines */
32 extern void CALLTO16_Start(), CALLTO16_End();
33 extern void CALLTO16_Ret_word(), CALLTO16_Ret_long();
34 extern DWORD CALLTO16_RetAddr_word, CALLTO16_RetAddr_long;
36 codesel = GLOBAL_CreateBlock( GMEM_FIXED, (void *)CALLTO16_Start,
37 (int)CALLTO16_End - (int)CALLTO16_Start,
38 0, TRUE, TRUE, FALSE, NULL );
39 if (!codesel) return FALSE;
41 /* Patch the return addresses for CallTo16 routines */
43 CALLTO16_RetAddr_word=MAKELONG( (int)CALLTO16_Ret_word-(int)CALLTO16_Start,
45 CALLTO16_RetAddr_long=MAKELONG( (int)CALLTO16_Ret_long-(int)CALLTO16_Start,
51 /***********************************************************************
52 * RELAY_DebugCallFrom16
54 void RELAY_DebugCallFrom16( int func_type, char *args,
55 void *entry_point, CONTEXT *context )
62 if (!debugging_relay) return;
64 frame = CURRENT_STACK16;
65 printf( "Call %s(", BUILTIN_GetEntryPoint16( frame->entry_cs,
68 args16 = (char *)frame->args;
69 for (i = 0; i < strlen(args); i++)
91 printf( "0x%04x", *(WORD *)args16 );
95 printf( "0x%08x", *(int *)args16 );
99 printf( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
103 if (*args) printf( "," );
105 printf( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
107 if (func_type == 2) /* register function */
108 printf( " AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
109 AX_reg(context), BX_reg(context), CX_reg(context),
110 DX_reg(context), SI_reg(context), DI_reg(context),
111 (WORD)ES_reg(context), EFL_reg(context) );
115 /***********************************************************************
116 * RELAY_DebugCallFrom16Ret
118 void RELAY_DebugCallFrom16Ret( int func_type, int ret_val, CONTEXT *context)
123 if (!debugging_relay) return;
124 frame = CURRENT_STACK16;
125 printf( "Ret %s() ", BUILTIN_GetEntryPoint16( frame->entry_cs,
131 printf( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
132 ret_val, frame->cs, frame->ip, frame->ds );
135 printf( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
136 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
139 printf( "retval=none ret=%04x:%04x ds=%04x\n",
140 frame->cs, frame->ip, frame->ds );
141 printf( " AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
142 AX_reg(context), BX_reg(context), CX_reg(context),
143 DX_reg(context), SI_reg(context), DI_reg(context),
144 (WORD)ES_reg(context), EFL_reg(context) );
150 /***********************************************************************
151 * RELAY_Unimplemented16
153 * This function is called for unimplemented 16-bit entry points (declared
154 * as 'stub' in the spec file).
156 void RELAY_Unimplemented16(void)
159 STACK16FRAME *frame = CURRENT_STACK16;
160 fprintf(stderr,"No handler for Win16 routine %s (called from %04x:%04x)\n",
161 BUILTIN_GetEntryPoint16(frame->entry_cs,frame->entry_ip,&ordinal),
162 frame->cs, frame->ip );
163 TASK_KillCurrentTask(1);
167 /***********************************************************************
168 * RELAY_Unimplemented32
170 * This function is called for unimplemented 32-bit entry points (declared
171 * as 'stub' in the spec file).
172 * (The args are the same than for RELAY_DebugCallFrom32).
174 void RELAY_Unimplemented32( int nb_args, void *relay_addr,
175 void *entry_point, int ebp, int ret_addr )
177 fprintf( stderr, "No handler for Win32 routine %s (called from %08x)\n",
178 BUILTIN_GetEntryPoint32( relay_addr ), ret_addr );
179 TASK_KillCurrentTask(1);
183 /***********************************************************************
184 * RELAY_DebugCallTo16
186 * 'stack' points to the called function address on the 32-bit stack.
191 * (stack) func to call
193 void RELAY_DebugCallTo16( int* stack, int nbargs )
195 if (!debugging_relay) return;
197 printf( "CallTo16(func=%04x:%04x,ds=%04x",
198 HIWORD(stack[0]), LOWORD(stack[0]), CURRENT_DS );
200 while (nbargs--) printf( ",0x%04x", *stack++ );
205 /***********************************************************************
206 * RELAY_DebugCallFrom32
208 * 'stack' points to the saved ebp on the stack.
215 * (stack-4) entry point
216 * (stack-8) relay addr
218 void RELAY_DebugCallFrom32( int *stack, int nb_args )
222 if (!debugging_relay) return;
223 printf( "Call %s(", BUILTIN_GetEntryPoint32( (void *)stack[-2] ));
226 for (parg = &stack[2]; nb_args; parg++, nb_args--)
228 printf( "%08x", *parg );
229 if (nb_args > 1) printf( "," );
232 printf( ") ret=%08x\n", stack[1] );
233 if (nb_args == -1) /* Register function */
235 CONTEXT *context = (CONTEXT *)((BYTE *)stack - sizeof(CONTEXT) - 12);
236 printf( " EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx ESI=%08lx EDI=%08lx\n",
237 context->Eax, context->Ebx, context->Ecx, context->Edx,
238 context->Esi, context->Edi );
239 printf( " EBP=%08lx ESP=%08lx DS=%04lx ES=%04lx FS=%04lx GS=%04lx EFL=%08lx\n",
240 context->Ebp, context->Esp, context->SegDs, context->SegEs,
241 context->SegFs, context->SegGs, context->EFlags );
246 /***********************************************************************
247 * RELAY_DebugCallFrom32Ret
249 * 'stack' points to the saved ebp on the stack.
256 * (stack-4) entry point
257 * (stack-8) relay addr
259 void RELAY_DebugCallFrom32Ret( int *stack, int nb_args, int ret_val )
261 if (!debugging_relay) return;
262 printf( "Ret %s() retval=%08x ret=%08x\n",
263 BUILTIN_GetEntryPoint32( (void *)stack[-2] ), ret_val, stack[1] );
264 if (nb_args == -1) /* Register function */
266 CONTEXT *context = (CONTEXT *)((BYTE *)stack - sizeof(CONTEXT) - 12);
267 printf( " EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx ESI=%08lx EDI=%08lx\n",
268 context->Eax, context->Ebx, context->Ecx, context->Edx,
269 context->Esi, context->Edi );
270 printf( " EBP=%08lx ESP=%08lx DS=%04lx ES=%04lx FS=%04lx GS=%04lx EFL=%08lx\n",
271 context->Ebp, context->Esp, context->SegDs, context->SegEs,
272 context->SegFs, context->SegGs, context->EFlags );
277 /***********************************************************************
278 * RELAY_DebugCallTo32
280 void RELAY_DebugCallTo32( unsigned int func, int nbargs, unsigned int arg1 )
282 unsigned int *argptr;
284 if (!debugging_relay) return;
286 printf( "CallTo32(func=%08x", func );
287 for (argptr = &arg1; nbargs; nbargs--, argptr++)
288 printf( ",%08x", *argptr );
293 /**********************************************************************
296 INT16 Catch( LPCATCHBUF lpbuf )
298 STACK16FRAME *pFrame = CURRENT_STACK16;
300 /* Note: we don't save the current ss, as the catch buffer is */
301 /* only 9 words long. Hopefully no one will have the silly */
302 /* idea to change the current stack before calling Throw()... */
315 /* FIXME: we need to save %si and %di */
317 lpbuf[0] = IF1632_Saved16_sp;
318 lpbuf[1] = LOWORD(IF1632_Saved32_esp);
319 lpbuf[2] = HIWORD(IF1632_Saved32_esp);
320 lpbuf[3] = pFrame->saved_ss;
321 lpbuf[4] = pFrame->saved_sp;
322 lpbuf[5] = pFrame->ds;
323 lpbuf[6] = pFrame->bp;
324 lpbuf[7] = pFrame->ip;
325 lpbuf[8] = pFrame->cs;
330 /**********************************************************************
333 INT16 Throw( LPCATCHBUF lpbuf, INT16 retval )
335 STACK16FRAME *pFrame;
336 WORD es = CURRENT_STACK16->es;
338 IF1632_Saved16_sp = lpbuf[0] - sizeof(WORD);
339 IF1632_Saved32_esp = MAKELONG( lpbuf[1], lpbuf[2] );
340 pFrame = CURRENT_STACK16;
341 pFrame->saved_ss = lpbuf[3];
342 pFrame->saved_sp = lpbuf[4];
343 pFrame->ds = lpbuf[5];
344 pFrame->bp = lpbuf[6];
345 pFrame->ip = lpbuf[7];
346 pFrame->cs = lpbuf[8];
348 if (debugging_relay) /* Make sure we have a valid entry point address */
350 static FARPROC16 entryPoint = NULL;
352 if (!entryPoint) /* Get entry point for Throw() */
353 entryPoint = MODULE_GetEntryPoint( GetModuleHandle("KERNEL"), 56 );
354 pFrame->entry_cs = SELECTOROF(entryPoint);
355 pFrame->entry_ip = OFFSETOF(entryPoint);