2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
9 #include "wine/winbase16.h"
13 #include "stackframe.h"
14 #include "selectors.h"
15 #include "builtin16.h"
18 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(relay
);
24 /***********************************************************************
32 /* Allocate the code selector for CallTo16 routines */
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 SEGPTR CallTo16_RetAddr
;
39 extern SEGPTR CALL32_CBClient_RetAddr
;
40 extern SEGPTR CALL32_CBClientEx_RetAddr
;
42 codesel
= SELECTOR_AllocBlock( (void *)Call16_Ret_Start
,
43 (char *)Call16_Ret_End
- (char *)Call16_Ret_Start
,
44 SEGMENT_CODE
, TRUE
, FALSE
);
45 if (!codesel
) return FALSE
;
47 /* Patch the return addresses for CallTo16 routines */
50 PTR_SEG_OFF_TO_SEGPTR( codesel
, (char*)CallTo16_Ret
- (char*)Call16_Ret_Start
);
51 CALL32_CBClient_RetAddr
=
52 PTR_SEG_OFF_TO_SEGPTR( codesel
, (char*)CALL32_CBClient_Ret
- (char*)Call16_Ret_Start
);
53 CALL32_CBClientEx_RetAddr
=
54 PTR_SEG_OFF_TO_SEGPTR( codesel
, (char*)CALL32_CBClientEx_Ret
- (char*)Call16_Ret_Start
);
57 /* Initialize thunking */
62 * Stubs for the CallTo16/CallFrom16 routines on non-Intel architectures
63 * (these will never be called but need to be present to satisfy the linker ...)
66 WORD CALLBACK
CallTo16Word( FARPROC16 target
, INT nArgs
)
69 LONG CALLBACK
CallTo16Long( FARPROC16 target
, INT nArgs
)
72 LONG CALLBACK
CallTo16RegisterShort( const CONTEXT86
*context
, INT nArgs
)
75 LONG CALLBACK
CallTo16RegisterLong ( const CONTEXT86
*context
, INT nArgs
)
78 WORD
CallFrom16Word( void )
81 LONG
CallFrom16Long( void )
84 void CallFrom16Register( void )
87 void CallFrom16Thunk( void )
90 DWORD WINAPI
CALL32_CBClient( FARPROC proc
, LPWORD args
, DWORD
*esi
)
93 DWORD WINAPI
CALL32_CBClientEx( FARPROC proc
, LPWORD args
, DWORD
*esi
, INT
*nArgs
)
98 /* from relay32/relay386.c */
99 extern char **debug_relay_excludelist
,**debug_relay_includelist
;
101 /***********************************************************************
102 * RELAY_DebugCallFrom16
104 void RELAY_DebugCallFrom16( CONTEXT86
*context
)
108 char *args16
, funstr
[80];
110 int i
, usecdecl
, reg_func
;
112 if (!TRACE_ON(relay
)) return;
114 frame
= CURRENT_STACK16
;
115 args
= BUILTIN_GetEntryPoint16( frame
, funstr
, &ordinal
);
116 if (!args
) return; /* happens for the two snoop register relays */
117 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
118 DPRINTF( "Call %s(",funstr
);
119 VA_START16( args16
);
121 usecdecl
= ( *args
== 'c' );
123 reg_func
= ( memcmp( args
, "regs_", 5 ) == 0
124 || memcmp( args
, "intr_", 5 ) == 0 );
135 DPRINTF( "0x%04x", *(WORD
*)args16
);
139 DPRINTF( "0x%08x", *(int *)args16
);
143 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
148 DPRINTF( "%04x:%04x %s", *(WORD
*)(args16
+2), *(WORD
*)args16
,
149 debugres_a( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
)) );
154 if (*args
) DPRINTF( "," );
159 /* Start with the last arg */
160 for (i
= 0; args
[i
]; i
++)
184 DPRINTF( "0x%04x", *(WORD
*)args16
);
188 DPRINTF( "0x%08x", *(int *)args16
);
192 DPRINTF( "0x%08x %s", *(int *)args16
,
193 debugres_a( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
)));
197 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
201 DPRINTF( "%04x:%04x %s", *(WORD
*)(args16
+2), *(WORD
*)args16
,
202 debugres_a( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
)));
206 if (*args
) DPRINTF( "," );
210 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame
->cs
, frame
->ip
, frame
->ds
);
214 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
215 AX_reg(context
), BX_reg(context
), CX_reg(context
),
216 DX_reg(context
), SI_reg(context
), DI_reg(context
),
217 (WORD
)ES_reg(context
), EFL_reg(context
) );
219 SYSLEVEL_CheckNotLevel( 2 );
223 /***********************************************************************
224 * RELAY_DebugCallFrom16Ret
226 void RELAY_DebugCallFrom16Ret( CONTEXT86
*context
, int ret_val
)
233 if (!TRACE_ON(relay
)) return;
234 frame
= CURRENT_STACK16
;
235 args
= BUILTIN_GetEntryPoint16( frame
, funstr
, &ordinal
);
237 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
238 DPRINTF( "Ret %s() ",funstr
);
240 if ( memcmp( args
+2, "long_", 5 ) == 0 )
242 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
243 ret_val
, frame
->cs
, frame
->ip
, frame
->ds
);
245 else if ( memcmp( args
+2, "word_", 5 ) == 0 )
247 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
248 ret_val
& 0xffff, frame
->cs
, frame
->ip
, frame
->ds
);
250 else if ( memcmp( args
+2, "regs_", 5 ) == 0
251 || memcmp( args
+2, "intr_", 5 ) == 0 )
253 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
254 (WORD
)CS_reg(context
), LOWORD(EIP_reg(context
)), (WORD
)DS_reg(context
));
255 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
256 AX_reg(context
), BX_reg(context
), CX_reg(context
),
257 DX_reg(context
), SI_reg(context
), DI_reg(context
),
258 (WORD
)ES_reg(context
), EFL_reg(context
) );
261 SYSLEVEL_CheckNotLevel( 2 );
265 /***********************************************************************
266 * RELAY_Unimplemented16
268 * This function is called for unimplemented 16-bit entry points (declared
269 * as 'stub' in the spec file).
271 void RELAY_Unimplemented16(void)
275 STACK16FRAME
*frame
= CURRENT_STACK16
;
276 BUILTIN_GetEntryPoint16( frame
, name
, &ordinal
);
277 MESSAGE("No handler for Win16 routine %s (called from %04x:%04x)\n",
278 name
, frame
->cs
, frame
->ip
);
283 /***********************************************************************
284 * RELAY_DebugCallTo16
286 * 'target' contains either the function to call (normal CallTo16)
287 * or a pointer to the CONTEXT86 struct (register CallTo16).
288 * 'nb_args' is the number of argument bytes on the 16-bit stack;
289 * 'reg_func' specifies whether we have a register CallTo16 or not.
291 void RELAY_DebugCallTo16( LPVOID target
, int nb_args
, BOOL reg_func
)
296 if (!TRACE_ON(relay
)) return;
297 teb
= NtCurrentTeb();
298 stack16
= (WORD
*)THREAD_STACK16(teb
);
300 nb_args
/= sizeof(WORD
);
304 CONTEXT86
*context
= (CONTEXT86
*)target
;
306 DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
307 CS_reg(context
), LOWORD(EIP_reg(context
)), DS_reg(context
) );
308 while (nb_args
--) DPRINTF( ",0x%04x", *--stack16
);
309 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb
->cur_stack
),
310 OFFSETOF(teb
->cur_stack
) );
311 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
312 AX_reg(context
), BX_reg(context
), CX_reg(context
),
313 DX_reg(context
), SI_reg(context
), DI_reg(context
),
314 BP_reg(context
), (WORD
)ES_reg(context
), (WORD
)FS_reg(context
) );
318 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
319 HIWORD(target
), LOWORD(target
), SELECTOROF(teb
->cur_stack
) );
320 while (nb_args
--) DPRINTF( ",0x%04x", *--stack16
);
321 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb
->cur_stack
),
322 OFFSETOF(teb
->cur_stack
) );
325 SYSLEVEL_CheckNotLevel( 2 );
329 /***********************************************************************
330 * RELAY_DebugCallTo16Ret
332 void RELAY_DebugCallTo16Ret( int ret_val
)
334 if (!TRACE_ON(relay
)) return;
336 DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n",
337 SELECTOROF(NtCurrentTeb()->cur_stack
),
338 OFFSETOF(NtCurrentTeb()->cur_stack
), ret_val
);
339 SYSLEVEL_CheckNotLevel( 2 );
343 /**********************************************************************
347 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
349 void WINAPI
Catch16( LPCATCHBUF lpbuf
, CONTEXT86
*context
)
351 /* Note: we don't save the current ss, as the catch buffer is */
352 /* only 9 words long. Hopefully no one will have the silly */
353 /* idea to change the current stack before calling Throw()... */
367 lpbuf
[0] = LOWORD(EIP_reg(context
));
368 lpbuf
[1] = CS_reg(context
);
369 /* Windows pushes 4 more words before saving sp */
370 lpbuf
[2] = LOWORD(ESP_reg(context
)) - 4 * sizeof(WORD
);
371 lpbuf
[3] = LOWORD(EBP_reg(context
));
372 lpbuf
[4] = LOWORD(ESI_reg(context
));
373 lpbuf
[5] = LOWORD(EDI_reg(context
));
374 lpbuf
[6] = DS_reg(context
);
376 lpbuf
[8] = SS_reg(context
);
377 AX_reg(context
) = 0; /* Return 0 */
381 /**********************************************************************
385 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
387 void WINAPI
Throw16( LPCATCHBUF lpbuf
, INT16 retval
, CONTEXT86
*context
)
389 STACK16FRAME
*pFrame
;
390 STACK32FRAME
*frame32
;
391 TEB
*teb
= NtCurrentTeb();
393 AX_reg(context
) = retval
;
395 /* Find the frame32 corresponding to the frame16 we are jumping to */
396 pFrame
= THREAD_STACK16(teb
);
397 frame32
= pFrame
->frame32
;
398 while (frame32
&& frame32
->frame16
)
400 if (OFFSETOF(frame32
->frame16
) < OFFSETOF(teb
->cur_stack
))
401 break; /* Something strange is going on */
402 if (OFFSETOF(frame32
->frame16
) > lpbuf
[2])
404 /* We found the right frame */
405 pFrame
->frame32
= frame32
;
408 frame32
= ((STACK16FRAME
*)PTR_SEG_TO_LIN(frame32
->frame16
))->frame32
;
411 EIP_reg(context
) = lpbuf
[0];
412 CS_reg(context
) = lpbuf
[1];
413 ESP_reg(context
) = lpbuf
[2] + 4 * sizeof(WORD
) - sizeof(WORD
) /*extra arg*/;
414 EBP_reg(context
) = lpbuf
[3];
415 ESI_reg(context
) = lpbuf
[4];
416 EDI_reg(context
) = lpbuf
[5];
417 DS_reg(context
) = lpbuf
[6];
419 if (lpbuf
[8] != SS_reg(context
))
420 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );