2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
9 #include "wine/winbase16.h"
14 #include "stackframe.h"
15 #include "builtin16.h"
19 #include "debugtools.h"
22 DEFAULT_DEBUG_CHANNEL(relay
)
25 /***********************************************************************
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
);
57 /* Create built-in modules */
58 if (!BUILTIN_Init()) return FALSE
;
60 /* Initialize thunking */
65 /* from relay32/relay386.c */
66 extern char **debug_relay_excludelist
,**debug_relay_includelist
;
68 /***********************************************************************
69 * RELAY_DebugCallFrom16
71 void RELAY_DebugCallFrom16( CONTEXT86
*context
)
75 char *args16
, funstr
[80];
77 int i
, usecdecl
, reg_func
;
79 if (!TRACE_ON(relay
)) return;
81 frame
= CURRENT_STACK16
;
82 args
= BUILTIN_GetEntryPoint16( frame
, funstr
, &ordinal
);
83 if (!args
) return; /* happens for the two snoop register relays */
84 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
85 DPRINTF( "Call %s(",funstr
);
88 usecdecl
= ( *args
== 'c' );
90 reg_func
= ( memcmp( args
, "regs_", 5 ) == 0
91 || memcmp( args
, "intr_", 5 ) == 0 );
102 DPRINTF( "0x%04x", *(WORD
*)args16
);
106 DPRINTF( "0x%08x", *(int *)args16
);
110 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
111 if (HIWORD(*(SEGPTR
*)args16
))
112 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
116 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
120 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
121 if (HIWORD( *(SEGPTR
*)args16
))
122 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
127 if (*args
) DPRINTF( "," );
132 /* Start with the last arg */
133 for (i
= 0; args
[i
]; i
++)
157 DPRINTF( "0x%04x", *(WORD
*)args16
);
161 DPRINTF( "0x%08x", *(int *)args16
);
165 DPRINTF( "0x%08x", *(int *)args16
);
166 if (HIWORD(*(SEGPTR
*)args16
))
167 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
171 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
175 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
176 if (HIWORD( *(SEGPTR
*)args16
))
177 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
181 if (*args
) DPRINTF( "," );
185 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame
->cs
, frame
->ip
, frame
->ds
);
189 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
190 AX_reg(context
), BX_reg(context
), CX_reg(context
),
191 DX_reg(context
), SI_reg(context
), DI_reg(context
),
192 (WORD
)ES_reg(context
), EFL_reg(context
) );
194 SYSLEVEL_CheckNotLevel( 2 );
198 /***********************************************************************
199 * RELAY_DebugCallFrom16Ret
201 void RELAY_DebugCallFrom16Ret( CONTEXT86
*context
, int ret_val
)
208 if (!TRACE_ON(relay
)) return;
209 frame
= CURRENT_STACK16
;
210 args
= BUILTIN_GetEntryPoint16( frame
, funstr
, &ordinal
);
212 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
213 DPRINTF( "Ret %s() ",funstr
);
215 if ( memcmp( args
+2, "long_", 5 ) == 0 )
217 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
218 ret_val
, frame
->cs
, frame
->ip
, frame
->ds
);
220 else if ( memcmp( args
+2, "word_", 5 ) == 0 )
222 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
223 ret_val
& 0xffff, frame
->cs
, frame
->ip
, frame
->ds
);
225 else if ( memcmp( args
+2, "regs_", 5 ) == 0
226 || memcmp( args
+2, "intr_", 5 ) == 0 )
228 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
229 (WORD
)CS_reg(context
), LOWORD(EIP_reg(context
)), (WORD
)DS_reg(context
));
230 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
231 AX_reg(context
), BX_reg(context
), CX_reg(context
),
232 DX_reg(context
), SI_reg(context
), DI_reg(context
),
233 (WORD
)ES_reg(context
), EFL_reg(context
) );
236 SYSLEVEL_CheckNotLevel( 2 );
240 /***********************************************************************
241 * RELAY_Unimplemented16
243 * This function is called for unimplemented 16-bit entry points (declared
244 * as 'stub' in the spec file).
246 void RELAY_Unimplemented16(void)
250 STACK16FRAME
*frame
= CURRENT_STACK16
;
251 BUILTIN_GetEntryPoint16( frame
, name
, &ordinal
);
252 MESSAGE("No handler for Win16 routine %s (called from %04x:%04x)\n",
253 name
, frame
->cs
, frame
->ip
);
258 /***********************************************************************
259 * RELAY_DebugCallTo16
261 * 'target' contains either the function to call (normal CallTo16)
262 * or a pointer to the CONTEXT86 struct (register CallTo16).
263 * 'nb_args' is the number of argument bytes on the 16-bit stack;
264 * 'reg_func' specifies whether we have a register CallTo16 or not.
266 void RELAY_DebugCallTo16( LPVOID target
, int nb_args
, BOOL reg_func
)
271 if (!TRACE_ON(relay
)) return;
272 teb
= NtCurrentTeb();
273 stack16
= (WORD
*)THREAD_STACK16(teb
);
275 nb_args
/= sizeof(WORD
);
279 CONTEXT86
*context
= (CONTEXT86
*)target
;
281 DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
282 CS_reg(context
), LOWORD(EIP_reg(context
)), DS_reg(context
) );
283 while (nb_args
--) DPRINTF( ",0x%04x", *--stack16
);
284 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb
->cur_stack
),
285 OFFSETOF(teb
->cur_stack
) );
286 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
287 AX_reg(context
), BX_reg(context
), CX_reg(context
),
288 DX_reg(context
), SI_reg(context
), DI_reg(context
),
289 BP_reg(context
), (WORD
)ES_reg(context
), (WORD
)FS_reg(context
) );
293 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
294 HIWORD(target
), LOWORD(target
), SELECTOROF(teb
->cur_stack
) );
295 while (nb_args
--) DPRINTF( ",0x%04x", *--stack16
);
296 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb
->cur_stack
),
297 OFFSETOF(teb
->cur_stack
) );
300 SYSLEVEL_CheckNotLevel( 2 );
304 /***********************************************************************
305 * RELAY_DebugCallTo16Ret
307 void RELAY_DebugCallTo16Ret( int ret_val
)
309 if (!TRACE_ON(relay
)) return;
311 DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n",
312 SELECTOROF(NtCurrentTeb()->cur_stack
),
313 OFFSETOF(NtCurrentTeb()->cur_stack
), ret_val
);
314 SYSLEVEL_CheckNotLevel( 2 );
318 /**********************************************************************
322 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
324 void WINAPI
Catch16( LPCATCHBUF lpbuf
, CONTEXT86
*context
)
326 /* Note: we don't save the current ss, as the catch buffer is */
327 /* only 9 words long. Hopefully no one will have the silly */
328 /* idea to change the current stack before calling Throw()... */
342 lpbuf
[0] = LOWORD(EIP_reg(context
));
343 lpbuf
[1] = CS_reg(context
);
344 /* Windows pushes 4 more words before saving sp */
345 lpbuf
[2] = LOWORD(ESP_reg(context
)) - 4 * sizeof(WORD
);
346 lpbuf
[3] = LOWORD(EBP_reg(context
));
347 lpbuf
[4] = LOWORD(ESI_reg(context
));
348 lpbuf
[5] = LOWORD(EDI_reg(context
));
349 lpbuf
[6] = DS_reg(context
);
351 lpbuf
[8] = SS_reg(context
);
352 AX_reg(context
) = 0; /* Return 0 */
356 /**********************************************************************
360 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
362 void WINAPI
Throw16( LPCATCHBUF lpbuf
, INT16 retval
, CONTEXT86
*context
)
364 STACK16FRAME
*pFrame
;
365 STACK32FRAME
*frame32
;
366 TEB
*teb
= NtCurrentTeb();
368 AX_reg(context
) = retval
;
370 /* Find the frame32 corresponding to the frame16 we are jumping to */
371 pFrame
= THREAD_STACK16(teb
);
372 frame32
= pFrame
->frame32
;
373 while (frame32
&& frame32
->frame16
)
375 if (OFFSETOF(frame32
->frame16
) < OFFSETOF(teb
->cur_stack
))
376 break; /* Something strange is going on */
377 if (OFFSETOF(frame32
->frame16
) > lpbuf
[2])
379 /* We found the right frame */
380 pFrame
->frame32
= frame32
;
383 frame32
= ((STACK16FRAME
*)PTR_SEG_TO_LIN(frame32
->frame16
))->frame32
;
386 EIP_reg(context
) = lpbuf
[0];
387 CS_reg(context
) = lpbuf
[1];
388 ESP_reg(context
) = lpbuf
[2] + 4 * sizeof(WORD
) - sizeof(WORD
) /*extra arg*/;
389 EBP_reg(context
) = lpbuf
[3];
390 ESI_reg(context
) = lpbuf
[4];
391 EDI_reg(context
) = lpbuf
[5];
392 DS_reg(context
) = lpbuf
[6];
394 if (lpbuf
[8] != SS_reg(context
))
395 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );