2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
9 #include "wine/winbase16.h"
14 #include "stackframe.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 DWORD CallTo16_RetAddr
;
39 extern DWORD CALL32_CBClient_RetAddr
;
40 extern DWORD CALL32_CBClientEx_RetAddr
;
42 codesel
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)Call16_Ret_Start
,
43 (int)Call16_Ret_End
- (int)Call16_Ret_Start
,
44 GetModuleHandle16( "KERNEL" ),
45 TRUE
, TRUE
, FALSE
, NULL
);
46 if (!codesel
) return FALSE
;
48 /* Patch the return addresses for CallTo16 routines */
51 MAKELONG( (int)CallTo16_Ret
-(int)Call16_Ret_Start
, codesel
);
52 CALL32_CBClient_RetAddr
=
53 MAKELONG( (int)CALL32_CBClient_Ret
-(int)Call16_Ret_Start
, codesel
);
54 CALL32_CBClientEx_RetAddr
=
55 MAKELONG( (int)CALL32_CBClientEx_Ret
-(int)Call16_Ret_Start
, codesel
);
58 /* Create built-in modules */
59 if (!BUILTIN_Init()) return FALSE
;
61 /* Initialize thunking */
66 * Stubs for the CallTo16/CallFrom16 routines on non-Intel architectures
67 * (these will never be called but need to be present to satisfy the linker ...)
70 WORD CALLBACK
CallTo16Word( FARPROC16 target
, INT nArgs
)
73 LONG CALLBACK
CallTo16Long( FARPROC16 target
, INT nArgs
)
76 LONG CALLBACK
CallTo16RegisterShort( const CONTEXT86
*context
, INT nArgs
)
79 LONG CALLBACK
CallTo16RegisterLong ( const CONTEXT86
*context
, INT nArgs
)
82 WORD
CallFrom16Word( void )
85 LONG
CallFrom16Long( void )
88 void CallFrom16Register( void )
91 void CallFrom16Thunk( void )
94 DWORD WINAPI
CALL32_CBClient( FARPROC proc
, LPWORD args
, DWORD
*esi
)
97 DWORD WINAPI
CALL32_CBClientEx( FARPROC proc
, LPWORD args
, DWORD
*esi
, INT
*nArgs
)
102 /* from relay32/relay386.c */
103 extern char **debug_relay_excludelist
,**debug_relay_includelist
;
105 /***********************************************************************
106 * RELAY_DebugCallFrom16
108 void RELAY_DebugCallFrom16( CONTEXT86
*context
)
112 char *args16
, funstr
[80];
114 int i
, usecdecl
, reg_func
;
116 if (!TRACE_ON(relay
)) return;
118 frame
= CURRENT_STACK16
;
119 args
= BUILTIN_GetEntryPoint16( frame
, funstr
, &ordinal
);
120 if (!args
) return; /* happens for the two snoop register relays */
121 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
122 DPRINTF( "Call %s(",funstr
);
123 VA_START16( args16
);
125 usecdecl
= ( *args
== 'c' );
127 reg_func
= ( memcmp( args
, "regs_", 5 ) == 0
128 || memcmp( args
, "intr_", 5 ) == 0 );
139 DPRINTF( "0x%04x", *(WORD
*)args16
);
143 DPRINTF( "0x%08x", *(int *)args16
);
147 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
152 DPRINTF( "%04x:%04x %s", *(WORD
*)(args16
+2), *(WORD
*)args16
,
153 debugres_a( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
)) );
158 if (*args
) DPRINTF( "," );
163 /* Start with the last arg */
164 for (i
= 0; args
[i
]; i
++)
188 DPRINTF( "0x%04x", *(WORD
*)args16
);
192 DPRINTF( "0x%08x", *(int *)args16
);
196 DPRINTF( "0x%08x %s", *(int *)args16
,
197 debugres_a( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
)));
201 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
205 DPRINTF( "%04x:%04x %s", *(WORD
*)(args16
+2), *(WORD
*)args16
,
206 debugres_a( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
)));
210 if (*args
) DPRINTF( "," );
214 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame
->cs
, frame
->ip
, frame
->ds
);
218 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
219 AX_reg(context
), BX_reg(context
), CX_reg(context
),
220 DX_reg(context
), SI_reg(context
), DI_reg(context
),
221 (WORD
)ES_reg(context
), EFL_reg(context
) );
223 SYSLEVEL_CheckNotLevel( 2 );
227 /***********************************************************************
228 * RELAY_DebugCallFrom16Ret
230 void RELAY_DebugCallFrom16Ret( CONTEXT86
*context
, int ret_val
)
237 if (!TRACE_ON(relay
)) return;
238 frame
= CURRENT_STACK16
;
239 args
= BUILTIN_GetEntryPoint16( frame
, funstr
, &ordinal
);
241 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
242 DPRINTF( "Ret %s() ",funstr
);
244 if ( memcmp( args
+2, "long_", 5 ) == 0 )
246 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
247 ret_val
, frame
->cs
, frame
->ip
, frame
->ds
);
249 else if ( memcmp( args
+2, "word_", 5 ) == 0 )
251 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
252 ret_val
& 0xffff, frame
->cs
, frame
->ip
, frame
->ds
);
254 else if ( memcmp( args
+2, "regs_", 5 ) == 0
255 || memcmp( args
+2, "intr_", 5 ) == 0 )
257 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
258 (WORD
)CS_reg(context
), LOWORD(EIP_reg(context
)), (WORD
)DS_reg(context
));
259 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
260 AX_reg(context
), BX_reg(context
), CX_reg(context
),
261 DX_reg(context
), SI_reg(context
), DI_reg(context
),
262 (WORD
)ES_reg(context
), EFL_reg(context
) );
265 SYSLEVEL_CheckNotLevel( 2 );
269 /***********************************************************************
270 * RELAY_Unimplemented16
272 * This function is called for unimplemented 16-bit entry points (declared
273 * as 'stub' in the spec file).
275 void RELAY_Unimplemented16(void)
279 STACK16FRAME
*frame
= CURRENT_STACK16
;
280 BUILTIN_GetEntryPoint16( frame
, name
, &ordinal
);
281 MESSAGE("No handler for Win16 routine %s (called from %04x:%04x)\n",
282 name
, frame
->cs
, frame
->ip
);
287 /***********************************************************************
288 * RELAY_DebugCallTo16
290 * 'target' contains either the function to call (normal CallTo16)
291 * or a pointer to the CONTEXT86 struct (register CallTo16).
292 * 'nb_args' is the number of argument bytes on the 16-bit stack;
293 * 'reg_func' specifies whether we have a register CallTo16 or not.
295 void RELAY_DebugCallTo16( LPVOID target
, int nb_args
, BOOL reg_func
)
300 if (!TRACE_ON(relay
)) return;
301 teb
= NtCurrentTeb();
302 stack16
= (WORD
*)THREAD_STACK16(teb
);
304 nb_args
/= sizeof(WORD
);
308 CONTEXT86
*context
= (CONTEXT86
*)target
;
310 DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
311 CS_reg(context
), LOWORD(EIP_reg(context
)), DS_reg(context
) );
312 while (nb_args
--) DPRINTF( ",0x%04x", *--stack16
);
313 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb
->cur_stack
),
314 OFFSETOF(teb
->cur_stack
) );
315 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
316 AX_reg(context
), BX_reg(context
), CX_reg(context
),
317 DX_reg(context
), SI_reg(context
), DI_reg(context
),
318 BP_reg(context
), (WORD
)ES_reg(context
), (WORD
)FS_reg(context
) );
322 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
323 HIWORD(target
), LOWORD(target
), SELECTOROF(teb
->cur_stack
) );
324 while (nb_args
--) DPRINTF( ",0x%04x", *--stack16
);
325 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb
->cur_stack
),
326 OFFSETOF(teb
->cur_stack
) );
329 SYSLEVEL_CheckNotLevel( 2 );
333 /***********************************************************************
334 * RELAY_DebugCallTo16Ret
336 void RELAY_DebugCallTo16Ret( int ret_val
)
338 if (!TRACE_ON(relay
)) return;
340 DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n",
341 SELECTOROF(NtCurrentTeb()->cur_stack
),
342 OFFSETOF(NtCurrentTeb()->cur_stack
), ret_val
);
343 SYSLEVEL_CheckNotLevel( 2 );
347 /**********************************************************************
351 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
353 void WINAPI
Catch16( LPCATCHBUF lpbuf
, CONTEXT86
*context
)
355 /* Note: we don't save the current ss, as the catch buffer is */
356 /* only 9 words long. Hopefully no one will have the silly */
357 /* idea to change the current stack before calling Throw()... */
371 lpbuf
[0] = LOWORD(EIP_reg(context
));
372 lpbuf
[1] = CS_reg(context
);
373 /* Windows pushes 4 more words before saving sp */
374 lpbuf
[2] = LOWORD(ESP_reg(context
)) - 4 * sizeof(WORD
);
375 lpbuf
[3] = LOWORD(EBP_reg(context
));
376 lpbuf
[4] = LOWORD(ESI_reg(context
));
377 lpbuf
[5] = LOWORD(EDI_reg(context
));
378 lpbuf
[6] = DS_reg(context
);
380 lpbuf
[8] = SS_reg(context
);
381 AX_reg(context
) = 0; /* Return 0 */
385 /**********************************************************************
389 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
391 void WINAPI
Throw16( LPCATCHBUF lpbuf
, INT16 retval
, CONTEXT86
*context
)
393 STACK16FRAME
*pFrame
;
394 STACK32FRAME
*frame32
;
395 TEB
*teb
= NtCurrentTeb();
397 AX_reg(context
) = retval
;
399 /* Find the frame32 corresponding to the frame16 we are jumping to */
400 pFrame
= THREAD_STACK16(teb
);
401 frame32
= pFrame
->frame32
;
402 while (frame32
&& frame32
->frame16
)
404 if (OFFSETOF(frame32
->frame16
) < OFFSETOF(teb
->cur_stack
))
405 break; /* Something strange is going on */
406 if (OFFSETOF(frame32
->frame16
) > lpbuf
[2])
408 /* We found the right frame */
409 pFrame
->frame32
= frame32
;
412 frame32
= ((STACK16FRAME
*)PTR_SEG_TO_LIN(frame32
->frame16
))->frame32
;
415 EIP_reg(context
) = lpbuf
[0];
416 CS_reg(context
) = lpbuf
[1];
417 ESP_reg(context
) = lpbuf
[2] + 4 * sizeof(WORD
) - sizeof(WORD
) /*extra arg*/;
418 EBP_reg(context
) = lpbuf
[3];
419 ESI_reg(context
) = lpbuf
[4];
420 EDI_reg(context
) = lpbuf
[5];
421 DS_reg(context
) = lpbuf
[6];
423 if (lpbuf
[8] != SS_reg(context
))
424 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );