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] ));
224 for (i
= nb_args
& 0x7fffffff, parg
= &stack
[2]; i
; parg
++, i
--)
226 printf( "%08x", *parg
);
227 if (i
> 1) printf( "," );
229 printf( ") ret=%08x\n", stack
[1] );
230 if (nb_args
& 0x80000000) /* Register function */
232 CONTEXT
*context
= (CONTEXT
*)((BYTE
*)stack
- sizeof(CONTEXT
) - 8);
233 printf( " EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx ESI=%08lx EDI=%08lx\n",
234 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
,
235 context
->Esi
, context
->Edi
);
236 printf( " EBP=%08lx ESP=%08lx EIP=%08lx DS=%04lx ES=%04lx FS=%04lx GS=%04lx EFL=%08lx\n",
237 context
->Ebp
, context
->Esp
, context
->Eip
, context
->SegDs
,
238 context
->SegEs
, context
->SegFs
, context
->SegGs
,
244 /***********************************************************************
245 * RELAY_DebugCallFrom32Ret
247 * 'stack' points to the saved ebp on the stack.
254 * (stack-4) entry point
255 * (stack-8) relay addr
257 void RELAY_DebugCallFrom32Ret( int *stack
, int nb_args
, int ret_val
)
259 if (!debugging_relay
) return;
260 printf( "Ret %s() retval=%08x ret=%08x\n",
261 BUILTIN_GetEntryPoint32( (void *)stack
[-2] ), ret_val
, stack
[1] );
262 if (nb_args
& 0x80000000) /* Register function */
264 CONTEXT
*context
= (CONTEXT
*)((BYTE
*)stack
- sizeof(CONTEXT
) - 8);
265 printf( " EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx ESI=%08lx EDI=%08lx\n",
266 context
->Eax
, context
->Ebx
, context
->Ecx
, context
->Edx
,
267 context
->Esi
, context
->Edi
);
268 printf( " EBP=%08lx ESP=%08lx EIP=%08lx DS=%04lx ES=%04lx FS=%04lx GS=%04lx EFL=%08lx\n",
269 context
->Ebp
, context
->Esp
, context
->Eip
, context
->SegDs
,
270 context
->SegEs
, context
->SegFs
, context
->SegGs
,
276 /***********************************************************************
277 * RELAY_DebugCallTo32
279 void RELAY_DebugCallTo32( unsigned int func
, int nbargs
, unsigned int arg1
)
281 unsigned int *argptr
;
283 if (!debugging_relay
) return;
285 printf( "CallTo32(func=%08x", func
);
286 for (argptr
= &arg1
; nbargs
; nbargs
--, argptr
++)
287 printf( ",%08x", *argptr
);
292 /**********************************************************************
295 INT16
Catch( LPCATCHBUF lpbuf
)
297 STACK16FRAME
*pFrame
= CURRENT_STACK16
;
299 /* Note: we don't save the current ss, as the catch buffer is */
300 /* only 9 words long. Hopefully no one will have the silly */
301 /* idea to change the current stack before calling Throw()... */
314 /* FIXME: we need to save %si and %di */
316 lpbuf
[0] = IF1632_Saved16_sp
;
317 lpbuf
[1] = LOWORD(IF1632_Saved32_esp
);
318 lpbuf
[2] = HIWORD(IF1632_Saved32_esp
);
319 lpbuf
[3] = pFrame
->saved_ss
;
320 lpbuf
[4] = pFrame
->saved_sp
;
321 lpbuf
[5] = pFrame
->ds
;
322 lpbuf
[6] = pFrame
->bp
;
323 lpbuf
[7] = pFrame
->ip
;
324 lpbuf
[8] = pFrame
->cs
;
329 /**********************************************************************
332 INT16
Throw( LPCATCHBUF lpbuf
, INT16 retval
)
334 STACK16FRAME
*pFrame
;
335 WORD es
= CURRENT_STACK16
->es
;
337 IF1632_Saved16_sp
= lpbuf
[0] - sizeof(WORD
);
338 IF1632_Saved32_esp
= MAKELONG( lpbuf
[1], lpbuf
[2] );
339 pFrame
= CURRENT_STACK16
;
340 pFrame
->saved_ss
= lpbuf
[3];
341 pFrame
->saved_sp
= lpbuf
[4];
342 pFrame
->ds
= lpbuf
[5];
343 pFrame
->bp
= lpbuf
[6];
344 pFrame
->ip
= lpbuf
[7];
345 pFrame
->cs
= lpbuf
[8];
347 if (debugging_relay
) /* Make sure we have a valid entry point address */
349 static FARPROC16 entryPoint
= NULL
;
351 if (!entryPoint
) /* Get entry point for Throw() */
352 entryPoint
= MODULE_GetEntryPoint( GetModuleHandle("KERNEL"), 56 );
353 pFrame
->entry_cs
= SELECTOROF(entryPoint
);
354 pFrame
->entry_ip
= OFFSETOF(entryPoint
);