2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
9 #include "wine/winbase16.h"
14 #include "stackframe.h"
17 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(relay
)
23 /***********************************************************************
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 void CALLTO16_Ret_eax();
35 extern void CALL32_CBClient_Ret();
36 extern void CALL32_CBClientEx_Ret();
37 extern DWORD CALLTO16_RetAddr_word
;
38 extern DWORD CALLTO16_RetAddr_long
;
39 extern DWORD CALLTO16_RetAddr_eax
;
40 extern DWORD CALL32_CBClient_RetAddr
;
41 extern DWORD CALL32_CBClientEx_RetAddr
;
43 codesel
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)CALLTO16_Start
,
44 (int)CALLTO16_End
- (int)CALLTO16_Start
,
45 0, TRUE
, TRUE
, FALSE
, NULL
);
46 if (!codesel
) return FALSE
;
48 /* Patch the return addresses for CallTo16 routines */
50 CALLTO16_RetAddr_word
=MAKELONG( (int)CALLTO16_Ret_word
-(int)CALLTO16_Start
,
52 CALLTO16_RetAddr_long
=MAKELONG( (int)CALLTO16_Ret_long
-(int)CALLTO16_Start
,
54 CALLTO16_RetAddr_eax
=MAKELONG( (int)CALLTO16_Ret_eax
-(int)CALLTO16_Start
,
56 CALL32_CBClient_RetAddr
=
57 MAKELONG( (int)CALL32_CBClient_Ret
-(int)CALLTO16_Start
, codesel
);
58 CALL32_CBClientEx_RetAddr
=
59 MAKELONG( (int)CALL32_CBClientEx_Ret
-(int)CALLTO16_Start
, codesel
);
61 /* Create built-in modules */
62 if (!BUILTIN_Init()) return FALSE
;
64 /* Initialize thunking */
69 /* from relay32/relay386.c */
70 extern char **debug_relay_excludelist
,**debug_relay_includelist
;
72 /***********************************************************************
73 * RELAY_DebugCallFrom16
75 void RELAY_DebugCallFrom16( int func_type
, char *args
,
76 void *entry_point
, CONTEXT
*context
)
84 if (!TRACE_ON(relay
)) return;
86 frame
= CURRENT_STACK16
;
87 funstr
= BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,&ordinal
);
88 if (!funstr
) return; /* happens for the two snoop register relays */
89 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
90 DPRINTF( "Call %s(",funstr
);
93 if (func_type
& 4) /* cdecl */
101 DPRINTF( "0x%04x", *(WORD
*)args16
);
105 DPRINTF( "0x%08x", *(int *)args16
);
109 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
110 if (HIWORD(*(SEGPTR
*)args16
))
111 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
115 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
119 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
120 if (HIWORD( *(SEGPTR
*)args16
))
121 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
126 if (*args
) DPRINTF( "," );
131 /* Start with the last arg */
132 for (i
= 0; args
[i
]; i
++)
156 DPRINTF( "0x%04x", *(WORD
*)args16
);
160 DPRINTF( "0x%08x", *(int *)args16
);
164 DPRINTF( "0x%08x", *(int *)args16
);
165 if (HIWORD(*(SEGPTR
*)args16
))
166 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
170 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
174 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
175 if (HIWORD( *(SEGPTR
*)args16
))
176 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
180 if (*args
) DPRINTF( "," );
184 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame
->cs
, frame
->ip
, frame
->ds
);
187 if (func_type
& 2) /* register function */
188 DPRINTF( " AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
189 AX_reg(context
), BX_reg(context
), CX_reg(context
),
190 DX_reg(context
), SI_reg(context
), DI_reg(context
),
191 (WORD
)ES_reg(context
), EFL_reg(context
) );
193 SYSLEVEL_CheckNotLevel( 2 );
197 /***********************************************************************
198 * RELAY_DebugCallFrom16Ret
200 void RELAY_DebugCallFrom16Ret( int func_type
, int ret_val
, CONTEXT
*context
)
206 if (!TRACE_ON(relay
)) return;
207 frame
= CURRENT_STACK16
;
208 funstr
= BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,&ordinal
);
210 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
211 DPRINTF( "Ret %s() ",funstr
);
215 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
216 ret_val
, frame
->cs
, frame
->ip
, frame
->ds
);
219 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
220 ret_val
& 0xffff, frame
->cs
, frame
->ip
, frame
->ds
);
223 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
224 (WORD
)CS_reg(context
), IP_reg(context
), (WORD
)DS_reg(context
));
225 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
226 AX_reg(context
), BX_reg(context
), CX_reg(context
),
227 DX_reg(context
), SI_reg(context
), DI_reg(context
),
228 (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)
245 STACK16FRAME
*frame
= CURRENT_STACK16
;
246 MESSAGE("No handler for Win16 routine %s (called from %04x:%04x)\n",
247 BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,&ordinal
),
248 frame
->cs
, frame
->ip
);
253 /***********************************************************************
254 * RELAY_DebugCallTo16
256 * 'stack' points to the called function address on the 32-bit stack.
261 * (stack) func to call
263 void RELAY_DebugCallTo16( int* stack
, int nb_args
)
267 if (!TRACE_ON(relay
)) return;
268 teb
= NtCurrentTeb();
270 if (nb_args
== -1) /* Register function */
272 CONTEXT
*context
= (CONTEXT
*)stack
[0];
273 WORD
*stack16
= (WORD
*)THREAD_STACK16(teb
);
274 DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
275 CS_reg(context
), IP_reg(context
), DS_reg(context
) );
276 nb_args
= stack
[1] / sizeof(WORD
);
279 DPRINTF( ",0x%04x", *stack16
);
281 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb
->cur_stack
),
282 OFFSETOF(teb
->cur_stack
) );
283 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
284 AX_reg(context
), BX_reg(context
), CX_reg(context
),
285 DX_reg(context
), SI_reg(context
), DI_reg(context
),
286 BP_reg(context
), (WORD
)ES_reg(context
), (WORD
)FS_reg(context
) );
290 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
291 HIWORD(stack
[0]), LOWORD(stack
[0]),
292 SELECTOROF(teb
->cur_stack
) );
295 DPRINTF(",0x%04x", *stack
);
298 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb
->cur_stack
),
299 OFFSETOF(teb
->cur_stack
) );
302 SYSLEVEL_CheckNotLevel( 2 );
306 /***********************************************************************
307 * RELAY_DebugCallTo16Ret
309 void RELAY_DebugCallTo16Ret( int ret_val
)
311 if (!TRACE_ON(relay
)) return;
313 DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n",
314 SELECTOROF(NtCurrentTeb()->cur_stack
),
315 OFFSETOF(NtCurrentTeb()->cur_stack
), ret_val
);
316 SYSLEVEL_CheckNotLevel( 2 );
320 /**********************************************************************
324 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
326 void WINAPI
Catch16( CONTEXT
*context
)
332 VA_START16( valist
);
333 buf
= VA_ARG16( valist
, SEGPTR
);
334 lpbuf
= (LPCATCHBUF
)PTR_SEG_TO_LIN( buf
);
337 /* Note: we don't save the current ss, as the catch buffer is */
338 /* only 9 words long. Hopefully no one will have the silly */
339 /* idea to change the current stack before calling Throw()... */
353 lpbuf
[0] = IP_reg(context
);
354 lpbuf
[1] = CS_reg(context
);
355 /* Windows pushes 4 more words before saving sp */
356 lpbuf
[2] = SP_reg(context
) - 4 * sizeof(WORD
);
357 lpbuf
[3] = BP_reg(context
);
358 lpbuf
[4] = SI_reg(context
);
359 lpbuf
[5] = DI_reg(context
);
360 lpbuf
[6] = DS_reg(context
);
362 lpbuf
[8] = SS_reg(context
);
363 AX_reg(context
) = 0; /* Return 0 */
367 /**********************************************************************
371 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
373 void WINAPI
Throw16( CONTEXT
*context
)
378 STACK16FRAME
*pFrame
;
379 STACK32FRAME
*frame32
;
380 TEB
*teb
= NtCurrentTeb();
382 VA_START16( valist
);
383 AX_reg(context
) = VA_ARG16( valist
, WORD
); /* retval */
384 buf
= VA_ARG16( valist
, SEGPTR
);
385 lpbuf
= (LPCATCHBUF
)PTR_SEG_TO_LIN( buf
);
388 /* Find the frame32 corresponding to the frame16 we are jumping to */
389 pFrame
= THREAD_STACK16(teb
);
390 frame32
= pFrame
->frame32
;
391 while (frame32
&& frame32
->frame16
)
393 if (OFFSETOF(frame32
->frame16
) < OFFSETOF(teb
->cur_stack
))
394 break; /* Something strange is going on */
395 if (OFFSETOF(frame32
->frame16
) > lpbuf
[2])
397 /* We found the right frame */
398 pFrame
->frame32
= frame32
;
401 frame32
= ((STACK16FRAME
*)PTR_SEG_TO_LIN(frame32
->frame16
))->frame32
;
404 IP_reg(context
) = lpbuf
[0];
405 CS_reg(context
) = lpbuf
[1];
406 SP_reg(context
) = lpbuf
[2] + 4 * sizeof(WORD
) - sizeof(WORD
) /*extra arg*/;
407 BP_reg(context
) = lpbuf
[3];
408 SI_reg(context
) = lpbuf
[4];
409 DI_reg(context
) = lpbuf
[5];
410 DS_reg(context
) = lpbuf
[6];
412 if (lpbuf
[8] != SS_reg(context
))
413 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );
415 if (TRACE_ON(relay
)) /* Make sure we have a valid entry point address */
417 static FARPROC16 entryPoint
= NULL
;
419 if (!entryPoint
) /* Get entry point for Throw() */
420 entryPoint
= NE_GetEntryPoint( GetModuleHandle16("KERNEL"), 56 );
421 pFrame
->entry_cs
= SELECTOROF(entryPoint
);
422 pFrame
->entry_ip
= OFFSETOF(entryPoint
);
427 /**********************************************************************
430 * Helper for CallProc[Ex]32W
432 static DWORD
RELAY_CallProc32W(int Ex
)
434 DWORD nrofargs
, argconvmask
;
440 dbg_decl_str(relay
, 1024);
442 SYSLEVEL_ReleaseWin16Lock();
444 VA_START16( valist
);
445 nrofargs
= VA_ARG16( valist
, DWORD
);
446 argconvmask
= VA_ARG16( valist
, DWORD
);
447 proc32
= VA_ARG16( valist
, FARPROC
);
448 dsprintf(relay
, "CallProc32W(%ld,%ld,%p, Ex%d args[",nrofargs
,argconvmask
,proc32
,Ex
);
449 args
= (DWORD
*)HEAP_xalloc( GetProcessHeap(), 0,
450 sizeof(DWORD
)*nrofargs
);
451 /* CallProcEx doesn't need its args reversed */
452 for (i
=0;i
<nrofargs
;i
++) {
456 aix
= nrofargs
- i
- 1;
458 if (argconvmask
& (1<<i
))
460 SEGPTR ptr
= VA_ARG16( valist
, SEGPTR
);
461 args
[aix
] = (DWORD
)PTR_SEG_TO_LIN(ptr
);
462 dsprintf(relay
,"%08lx(%p),",ptr
,PTR_SEG_TO_LIN(ptr
));
466 args
[aix
] = VA_ARG16( valist
, DWORD
);
467 dsprintf(relay
,"%ld,",args
[aix
]);
470 dsprintf(relay
,"])");
473 if (!proc32
) ret
= 0;
474 else switch (nrofargs
)
476 case 0: ret
= proc32();
478 case 1: ret
= proc32(args
[0]);
480 case 2: ret
= proc32(args
[0],args
[1]);
482 case 3: ret
= proc32(args
[0],args
[1],args
[2]);
484 case 4: ret
= proc32(args
[0],args
[1],args
[2],args
[3]);
486 case 5: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4]);
488 case 6: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5]);
490 case 7: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6]);
492 case 8: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7]);
494 case 9: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8]);
496 case 10: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]);
498 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]);
501 /* FIXME: should go up to 32 arguments */
502 ERR("Unsupported number of arguments %ld, please report.\n",nrofargs
);
506 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
507 if (!Ex
) STACK16_POP( NtCurrentTeb(), (3 + nrofargs
) * sizeof(DWORD
) );
509 TRACE("%s - returns %08lx\n",dbg_str(relay
),ret
);
510 HeapFree( GetProcessHeap(), 0, args
);
512 SYSLEVEL_RestoreWin16Lock();
518 /**********************************************************************
519 * CallProc32W (KERNEL.517)
521 DWORD WINAPI
CallProc32W_16()
523 return RELAY_CallProc32W(0);
527 /**********************************************************************
528 * CallProcEx32W() (KERNEL.518)
530 * C - style linkage to CallProc32W - caller pops stack.
532 DWORD WINAPI
CallProcEx32W_16()
534 return RELAY_CallProc32W(TRUE
);