2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
9 #include "wine/winbase16.h"
14 #include "stackframe.h"
18 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(relay
)
24 /***********************************************************************
31 /* Allocate the code selector for CallTo16 routines */
33 extern void Call16_Ret_Start(), Call16_Ret_End();
34 extern void CallTo16_Ret();
35 extern void CALL32_CBClient_Ret();
36 extern void CALL32_CBClientEx_Ret();
37 extern DWORD CallTo16_RetAddr
;
38 extern DWORD CALL32_CBClient_RetAddr
;
39 extern DWORD CALL32_CBClientEx_RetAddr
;
41 codesel
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)Call16_Ret_Start
,
42 (int)Call16_Ret_End
- (int)Call16_Ret_Start
,
43 0, TRUE
, TRUE
, FALSE
, NULL
);
44 if (!codesel
) return FALSE
;
46 /* Patch the return addresses for CallTo16 routines */
49 MAKELONG( (int)CallTo16_Ret
-(int)Call16_Ret_Start
, codesel
);
50 CALL32_CBClient_RetAddr
=
51 MAKELONG( (int)CALL32_CBClient_Ret
-(int)Call16_Ret_Start
, codesel
);
52 CALL32_CBClientEx_RetAddr
=
53 MAKELONG( (int)CALL32_CBClientEx_Ret
-(int)Call16_Ret_Start
, codesel
);
55 /* Create built-in modules */
56 if (!BUILTIN_Init()) return FALSE
;
58 /* Initialize thunking */
63 /* from relay32/relay386.c */
64 extern char **debug_relay_excludelist
,**debug_relay_includelist
;
66 /***********************************************************************
67 * RELAY_DebugCallFrom16
69 void RELAY_DebugCallFrom16( CONTEXT86
*context
)
73 char *args16
, funstr
[80];
75 int i
, usecdecl
, reg_func
;
77 if (!TRACE_ON(relay
)) return;
79 frame
= CURRENT_STACK16
;
80 args
= BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,funstr
,&ordinal
);
81 if (!args
) return; /* happens for the two snoop register relays */
82 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
83 DPRINTF( "Call %s(",funstr
);
86 usecdecl
= ( *args
== 'c' );
88 reg_func
= ( memcmp( args
, "regs_", 5 ) == 0 );
99 DPRINTF( "0x%04x", *(WORD
*)args16
);
103 DPRINTF( "0x%08x", *(int *)args16
);
107 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
108 if (HIWORD(*(SEGPTR
*)args16
))
109 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
113 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
117 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
118 if (HIWORD( *(SEGPTR
*)args16
))
119 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
124 if (*args
) DPRINTF( "," );
129 /* Start with the last arg */
130 for (i
= 0; args
[i
]; i
++)
154 DPRINTF( "0x%04x", *(WORD
*)args16
);
158 DPRINTF( "0x%08x", *(int *)args16
);
162 DPRINTF( "0x%08x", *(int *)args16
);
163 if (HIWORD(*(SEGPTR
*)args16
))
164 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
168 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
172 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
173 if (HIWORD( *(SEGPTR
*)args16
))
174 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
178 if (*args
) DPRINTF( "," );
182 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame
->cs
, frame
->ip
, frame
->ds
);
186 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
187 AX_reg(context
), BX_reg(context
), CX_reg(context
),
188 DX_reg(context
), SI_reg(context
), DI_reg(context
),
189 (WORD
)ES_reg(context
), EFL_reg(context
) );
191 SYSLEVEL_CheckNotLevel( 2 );
195 /***********************************************************************
196 * RELAY_DebugCallFrom16Ret
198 void RELAY_DebugCallFrom16Ret( CONTEXT86
*context
, int ret_val
)
205 if (!TRACE_ON(relay
)) return;
206 frame
= CURRENT_STACK16
;
207 args
= BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,funstr
,&ordinal
);
209 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
210 DPRINTF( "Ret %s() ",funstr
);
212 if ( memcmp( args
+2, "long_", 5 ) == 0 )
214 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
215 ret_val
, frame
->cs
, frame
->ip
, frame
->ds
);
217 else if ( memcmp( args
+2, "word_", 5 ) == 0 )
219 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
220 ret_val
& 0xffff, frame
->cs
, frame
->ip
, frame
->ds
);
222 else if ( memcmp( args
+2, "regs_", 5 ) == 0 )
224 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
225 (WORD
)CS_reg(context
), IP_reg(context
), (WORD
)DS_reg(context
));
226 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
227 AX_reg(context
), BX_reg(context
), CX_reg(context
),
228 DX_reg(context
), SI_reg(context
), DI_reg(context
),
229 (WORD
)ES_reg(context
), EFL_reg(context
) );
232 SYSLEVEL_CheckNotLevel( 2 );
236 /***********************************************************************
237 * RELAY_Unimplemented16
239 * This function is called for unimplemented 16-bit entry points (declared
240 * as 'stub' in the spec file).
242 void RELAY_Unimplemented16(void)
246 STACK16FRAME
*frame
= CURRENT_STACK16
;
247 BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,name
,&ordinal
);
248 MESSAGE("No handler for Win16 routine %s (called from %04x:%04x)\n",
249 name
, frame
->cs
, frame
->ip
);
254 /***********************************************************************
255 * RELAY_DebugCallTo16
257 * 'stack' points to the called function address on the 32-bit stack.
262 * (stack) func to call
264 void RELAY_DebugCallTo16( int* stack
, int nb_args
)
268 if (!TRACE_ON(relay
)) return;
269 teb
= NtCurrentTeb();
271 if (nb_args
== -1) /* Register function */
273 CONTEXT86
*context
= (CONTEXT86
*)stack
[0];
274 WORD
*stack16
= (WORD
*)THREAD_STACK16(teb
);
275 DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
276 CS_reg(context
), IP_reg(context
), DS_reg(context
) );
277 nb_args
= stack
[1] / sizeof(WORD
);
280 DPRINTF( ",0x%04x", *stack16
);
282 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb
->cur_stack
),
283 OFFSETOF(teb
->cur_stack
) );
284 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
285 AX_reg(context
), BX_reg(context
), CX_reg(context
),
286 DX_reg(context
), SI_reg(context
), DI_reg(context
),
287 BP_reg(context
), (WORD
)ES_reg(context
), (WORD
)FS_reg(context
) );
291 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
292 HIWORD(stack
[0]), LOWORD(stack
[0]),
293 SELECTOROF(teb
->cur_stack
) );
296 DPRINTF(",0x%04x", *stack
);
299 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb
->cur_stack
),
300 OFFSETOF(teb
->cur_stack
) );
303 SYSLEVEL_CheckNotLevel( 2 );
307 /***********************************************************************
308 * RELAY_DebugCallTo16Ret
310 void RELAY_DebugCallTo16Ret( int ret_val
)
312 if (!TRACE_ON(relay
)) return;
314 DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n",
315 SELECTOROF(NtCurrentTeb()->cur_stack
),
316 OFFSETOF(NtCurrentTeb()->cur_stack
), ret_val
);
317 SYSLEVEL_CheckNotLevel( 2 );
321 /**********************************************************************
325 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
327 void WINAPI
Catch16( CONTEXT86
*context
)
333 VA_START16( valist
);
334 buf
= VA_ARG16( valist
, SEGPTR
);
335 lpbuf
= (LPCATCHBUF
)PTR_SEG_TO_LIN( buf
);
338 /* Note: we don't save the current ss, as the catch buffer is */
339 /* only 9 words long. Hopefully no one will have the silly */
340 /* idea to change the current stack before calling Throw()... */
354 lpbuf
[0] = IP_reg(context
);
355 lpbuf
[1] = CS_reg(context
);
356 /* Windows pushes 4 more words before saving sp */
357 lpbuf
[2] = SP_reg(context
) - 4 * sizeof(WORD
);
358 lpbuf
[3] = BP_reg(context
);
359 lpbuf
[4] = SI_reg(context
);
360 lpbuf
[5] = DI_reg(context
);
361 lpbuf
[6] = DS_reg(context
);
363 lpbuf
[8] = SS_reg(context
);
364 AX_reg(context
) = 0; /* Return 0 */
368 /**********************************************************************
372 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
374 void WINAPI
Throw16( CONTEXT86
*context
)
379 STACK16FRAME
*pFrame
;
380 STACK32FRAME
*frame32
;
381 TEB
*teb
= NtCurrentTeb();
383 VA_START16( valist
);
384 AX_reg(context
) = VA_ARG16( valist
, WORD
); /* retval */
385 buf
= VA_ARG16( valist
, SEGPTR
);
386 lpbuf
= (LPCATCHBUF
)PTR_SEG_TO_LIN( buf
);
389 /* Find the frame32 corresponding to the frame16 we are jumping to */
390 pFrame
= THREAD_STACK16(teb
);
391 frame32
= pFrame
->frame32
;
392 while (frame32
&& frame32
->frame16
)
394 if (OFFSETOF(frame32
->frame16
) < OFFSETOF(teb
->cur_stack
))
395 break; /* Something strange is going on */
396 if (OFFSETOF(frame32
->frame16
) > lpbuf
[2])
398 /* We found the right frame */
399 pFrame
->frame32
= frame32
;
402 frame32
= ((STACK16FRAME
*)PTR_SEG_TO_LIN(frame32
->frame16
))->frame32
;
405 IP_reg(context
) = lpbuf
[0];
406 CS_reg(context
) = lpbuf
[1];
407 SP_reg(context
) = lpbuf
[2] + 4 * sizeof(WORD
) - sizeof(WORD
) /*extra arg*/;
408 BP_reg(context
) = lpbuf
[3];
409 SI_reg(context
) = lpbuf
[4];
410 DI_reg(context
) = lpbuf
[5];
411 DS_reg(context
) = lpbuf
[6];
413 if (lpbuf
[8] != SS_reg(context
))
414 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );
416 if (TRACE_ON(relay
)) /* Make sure we have a valid entry point address */
418 static FARPROC16 entryPoint
= NULL
;
420 if (!entryPoint
) /* Get entry point for Throw() */
421 entryPoint
= NE_GetEntryPoint( GetModuleHandle16("KERNEL"), 56 );
422 pFrame
->entry_cs
= SELECTOROF(entryPoint
);
423 pFrame
->entry_ip
= OFFSETOF(entryPoint
);
428 /**********************************************************************
431 * Helper for CallProc[Ex]32W
433 static DWORD
RELAY_CallProc32W(int Ex
)
435 DWORD nrofargs
, argconvmask
;
441 dbg_decl_str(relay
, 1024);
443 SYSLEVEL_ReleaseWin16Lock();
445 VA_START16( valist
);
446 nrofargs
= VA_ARG16( valist
, DWORD
);
447 argconvmask
= VA_ARG16( valist
, DWORD
);
448 proc32
= VA_ARG16( valist
, FARPROC
);
449 dsprintf(relay
, "CallProc32W(%ld,%ld,%p, Ex%d args[",nrofargs
,argconvmask
,proc32
,Ex
);
450 args
= (DWORD
*)HEAP_xalloc( GetProcessHeap(), 0,
451 sizeof(DWORD
)*nrofargs
);
452 /* CallProcEx doesn't need its args reversed */
453 for (i
=0;i
<nrofargs
;i
++) {
457 aix
= nrofargs
- i
- 1;
459 if (argconvmask
& (1<<i
))
461 SEGPTR ptr
= VA_ARG16( valist
, SEGPTR
);
462 args
[aix
] = (DWORD
)PTR_SEG_TO_LIN(ptr
);
463 dsprintf(relay
,"%08lx(%p),",ptr
,PTR_SEG_TO_LIN(ptr
));
467 args
[aix
] = VA_ARG16( valist
, DWORD
);
468 dsprintf(relay
,"%ld,",args
[aix
]);
471 dsprintf(relay
,"])");
474 if (!proc32
) ret
= 0;
475 else switch (nrofargs
)
477 case 0: ret
= proc32();
479 case 1: ret
= proc32(args
[0]);
481 case 2: ret
= proc32(args
[0],args
[1]);
483 case 3: ret
= proc32(args
[0],args
[1],args
[2]);
485 case 4: ret
= proc32(args
[0],args
[1],args
[2],args
[3]);
487 case 5: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4]);
489 case 6: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5]);
491 case 7: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6]);
493 case 8: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7]);
495 case 9: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8]);
497 case 10: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]);
499 case 11: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],args
[10]);
502 /* FIXME: should go up to 32 arguments */
503 ERR("Unsupported number of arguments %ld, please report.\n",nrofargs
);
507 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
508 if (!Ex
) STACK16_POP( NtCurrentTeb(), (3 + nrofargs
) * sizeof(DWORD
) );
510 TRACE("%s - returns %08lx\n",dbg_str(relay
),ret
);
511 HeapFree( GetProcessHeap(), 0, args
);
513 SYSLEVEL_RestoreWin16Lock();
519 /**********************************************************************
520 * CallProc32W (KERNEL.517)
522 DWORD WINAPI
CallProc32W_16()
524 return RELAY_CallProc32W(0);
528 /**********************************************************************
529 * CallProcEx32W() (KERNEL.518)
531 * C - style linkage to CallProc32W - caller pops stack.
533 DWORD WINAPI
CallProcEx32W_16()
535 return RELAY_CallProc32W(TRUE
);