2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
13 #include "stackframe.h"
19 /***********************************************************************
22 BOOL32
RELAY_Init(void)
25 extern BOOL32
THUNK_Init(void);
27 /* Allocate the code selector for CallTo16 routines */
29 extern void CALLTO16_Start(), CALLTO16_End();
30 extern void CALLTO16_Ret_word(), CALLTO16_Ret_long();
31 extern void CALLTO16_Ret_eax();
32 extern void CALL32_CBClient_Ret();
33 extern DWORD CALLTO16_RetAddr_word
;
34 extern DWORD CALLTO16_RetAddr_long
;
35 extern DWORD CALLTO16_RetAddr_eax
;
36 extern DWORD CALL32_CBClient_RetAddr
;
38 codesel
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)CALLTO16_Start
,
39 (int)CALLTO16_End
- (int)CALLTO16_Start
,
40 0, TRUE
, TRUE
, FALSE
, NULL
);
41 if (!codesel
) return FALSE
;
43 /* Patch the return addresses for CallTo16 routines */
45 CALLTO16_RetAddr_word
=MAKELONG( (int)CALLTO16_Ret_word
-(int)CALLTO16_Start
,
47 CALLTO16_RetAddr_long
=MAKELONG( (int)CALLTO16_Ret_long
-(int)CALLTO16_Start
,
49 CALLTO16_RetAddr_eax
=MAKELONG( (int)CALLTO16_Ret_eax
-(int)CALLTO16_Start
,
51 CALL32_CBClient_RetAddr
=
52 MAKELONG( (int)CALL32_CBClient_Ret
-(int)CALLTO16_Start
, codesel
);
54 /* Create built-in modules */
55 if (!BUILTIN_Init()) return FALSE
;
57 /* Initialize thunking */
62 /* from relay32/relay386.c */
63 extern debug_relay_includelist
;
64 extern debug_relay_excludelist
;
66 /***********************************************************************
67 * RELAY_DebugCallFrom16
69 void RELAY_DebugCallFrom16( int func_type
, char *args
,
70 void *entry_point
, CONTEXT
*context
)
77 /* from relay32/relay386.c */
78 extern int RELAY_ShowDebugmsgRelay(const char *);
80 if (!TRACE_ON(relay
)) return;
82 frame
= CURRENT_STACK16
;
83 funstr
= BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,&ordinal
);
84 if (!funstr
) return; /* happens for the two snoop register relays */
85 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
86 DPRINTF( "Call %s(",funstr
);
89 if (func_type
& 4) /* cdecl */
97 DPRINTF( "0x%04x", *(WORD
*)args16
);
101 DPRINTF( "0x%08x", *(int *)args16
);
105 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
106 if (HIWORD(*(SEGPTR
*)args16
))
107 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
111 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
115 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
116 if (HIWORD( *(SEGPTR
*)args16
))
117 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
122 if (*args
) DPRINTF( "," );
127 /* Start with the last arg */
128 for (i
= 0; args
[i
]; i
++)
152 DPRINTF( "0x%04x", *(WORD
*)args16
);
156 DPRINTF( "0x%08x", *(int *)args16
);
160 DPRINTF( "0x%08x", *(int *)args16
);
161 if (HIWORD(*(SEGPTR
*)args16
))
162 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
166 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
170 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
171 if (HIWORD( *(SEGPTR
*)args16
))
172 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
176 if (*args
) DPRINTF( "," );
180 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame
->cs
, frame
->ip
, frame
->ds
);
183 if (func_type
& 2) /* register function */
184 DPRINTF( " AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
185 AX_reg(context
), BX_reg(context
), CX_reg(context
),
186 DX_reg(context
), SI_reg(context
), DI_reg(context
),
187 (WORD
)ES_reg(context
), EFL_reg(context
) );
191 /***********************************************************************
192 * RELAY_DebugCallFrom16Ret
194 void RELAY_DebugCallFrom16Ret( int func_type
, int ret_val
, CONTEXT
*context
)
199 /* from relay32/relay386.c */
200 extern int RELAY_ShowDebugmsgRelay(const char *);
202 if (!TRACE_ON(relay
)) return;
203 frame
= CURRENT_STACK16
;
204 funstr
= BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,&ordinal
);
206 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
207 DPRINTF( "Ret %s() ",funstr
);
211 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
212 ret_val
, frame
->cs
, frame
->ip
, frame
->ds
);
215 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
216 ret_val
& 0xffff, frame
->cs
, frame
->ip
, frame
->ds
);
219 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
220 (WORD
)CS_reg(context
), IP_reg(context
), (WORD
)DS_reg(context
));
221 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
222 AX_reg(context
), BX_reg(context
), CX_reg(context
),
223 DX_reg(context
), SI_reg(context
), DI_reg(context
),
224 (WORD
)ES_reg(context
), EFL_reg(context
) );
230 /***********************************************************************
231 * RELAY_Unimplemented16
233 * This function is called for unimplemented 16-bit entry points (declared
234 * as 'stub' in the spec file).
236 void RELAY_Unimplemented16(void)
239 STACK16FRAME
*frame
= CURRENT_STACK16
;
240 MSG("No handler for Win16 routine %s (called from %04x:%04x)\n",
241 BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,&ordinal
),
242 frame
->cs
, frame
->ip
);
247 /***********************************************************************
248 * RELAY_DebugCallTo16
250 * 'stack' points to the called function address on the 32-bit stack.
255 * (stack) func to call
257 void RELAY_DebugCallTo16( int* stack
, int nb_args
)
261 if (!TRACE_ON(relay
)) return;
262 thdb
= THREAD_Current();
264 if (nb_args
== -1) /* Register function */
266 CONTEXT
*context
= (CONTEXT
*)stack
[0];
267 WORD
*stack16
= (WORD
*)THREAD_STACK16(thdb
);
268 DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
269 CS_reg(context
), IP_reg(context
), DS_reg(context
) );
270 nb_args
= stack
[1] / sizeof(WORD
);
273 DPRINTF( ",0x%04x", *stack16
);
275 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(thdb
->cur_stack
),
276 OFFSETOF(thdb
->cur_stack
) );
277 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
278 AX_reg(context
), BX_reg(context
), CX_reg(context
),
279 DX_reg(context
), SI_reg(context
), DI_reg(context
),
280 BP_reg(context
), (WORD
)ES_reg(context
), (WORD
)FS_reg(context
) );
284 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
285 HIWORD(stack
[0]), LOWORD(stack
[0]),
286 SELECTOROF(thdb
->cur_stack
) );
289 DPRINTF(",0x%04x", *stack
);
292 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(thdb
->cur_stack
),
293 OFFSETOF(thdb
->cur_stack
) );
298 /***********************************************************************
299 * RELAY_DebugCallTo16Ret
301 void RELAY_DebugCallTo16Ret( int ret_val
)
305 if (!TRACE_ON(relay
)) return;
306 thdb
= THREAD_Current();
308 DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n",
309 SELECTOROF(thdb
->cur_stack
), OFFSETOF(thdb
->cur_stack
), ret_val
);
313 /**********************************************************************
317 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
319 void WINAPI
Catch( CONTEXT
*context
)
325 VA_START16( valist
);
326 buf
= VA_ARG16( valist
, SEGPTR
);
327 lpbuf
= (LPCATCHBUF
)PTR_SEG_TO_LIN( buf
);
330 /* Note: we don't save the current ss, as the catch buffer is */
331 /* only 9 words long. Hopefully no one will have the silly */
332 /* idea to change the current stack before calling Throw()... */
346 lpbuf
[0] = IP_reg(context
);
347 lpbuf
[1] = CS_reg(context
);
348 /* Windows pushes 4 more words before saving sp */
349 lpbuf
[2] = SP_reg(context
) - 4 * sizeof(WORD
);
350 lpbuf
[3] = BP_reg(context
);
351 lpbuf
[4] = SI_reg(context
);
352 lpbuf
[5] = DI_reg(context
);
353 lpbuf
[6] = DS_reg(context
);
355 lpbuf
[8] = SS_reg(context
);
356 AX_reg(context
) = 0; /* Return 0 */
360 /**********************************************************************
364 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
366 void WINAPI
Throw( CONTEXT
*context
)
371 STACK16FRAME
*pFrame
;
372 STACK32FRAME
*frame32
;
373 THDB
*thdb
= THREAD_Current();
375 VA_START16( valist
);
376 AX_reg(context
) = VA_ARG16( valist
, WORD
); /* retval */
377 buf
= VA_ARG16( valist
, SEGPTR
);
378 lpbuf
= (LPCATCHBUF
)PTR_SEG_TO_LIN( buf
);
381 /* Find the frame32 corresponding to the frame16 we are jumping to */
382 pFrame
= THREAD_STACK16( thdb
);
383 frame32
= THREAD_STACK16( thdb
)->frame32
;
384 while (frame32
&& frame32
->frame16
)
386 if (OFFSETOF(frame32
->frame16
) < OFFSETOF(thdb
->cur_stack
))
387 break; /* Something strange is going on */
388 if (OFFSETOF(frame32
->frame16
) > lpbuf
[2])
390 /* We found the right frame */
391 pFrame
->frame32
= frame32
;
394 frame32
= ((STACK16FRAME
*)PTR_SEG_TO_LIN(frame32
->frame16
))->frame32
;
397 IP_reg(context
) = lpbuf
[0];
398 CS_reg(context
) = lpbuf
[1];
399 SP_reg(context
) = lpbuf
[2] + 4 * sizeof(WORD
) - sizeof(WORD
) /*extra arg*/;
400 BP_reg(context
) = lpbuf
[3];
401 SI_reg(context
) = lpbuf
[4];
402 DI_reg(context
) = lpbuf
[5];
403 DS_reg(context
) = lpbuf
[6];
405 if (lpbuf
[8] != SS_reg(context
))
406 ERR(relay
, "Switching stack segment with Throw() not supported; expect crash now\n" );
408 if (TRACE_ON(relay
)) /* Make sure we have a valid entry point address */
410 static FARPROC16 entryPoint
= NULL
;
412 if (!entryPoint
) /* Get entry point for Throw() */
413 entryPoint
= NE_GetEntryPoint( GetModuleHandle16("KERNEL"), 56 );
414 pFrame
->entry_cs
= SELECTOROF(entryPoint
);
415 pFrame
->entry_ip
= OFFSETOF(entryPoint
);
420 /**********************************************************************
423 * Helper for CallProc[Ex]32W
425 static DWORD
RELAY_CallProc32W(int Ex
)
427 DWORD nrofargs
, argconvmask
;
433 dbg_decl_str(relay
, 1024);
435 VA_START16( valist
);
436 nrofargs
= VA_ARG16( valist
, DWORD
);
437 argconvmask
= VA_ARG16( valist
, DWORD
);
438 proc32
= VA_ARG16( valist
, FARPROC32
);
439 dsprintf(relay
, "CallProc32W(%ld,%ld,%p, Ex%d args[",nrofargs
,argconvmask
,proc32
,Ex
);
440 args
= (DWORD
*)HEAP_xalloc( GetProcessHeap(), 0,
441 sizeof(DWORD
)*nrofargs
);
442 /* CallProcEx doesn't need its args reversed */
443 for (i
=0;i
<nrofargs
;i
++) {
447 aix
= nrofargs
- i
- 1;
449 if (argconvmask
& (1<<i
))
451 SEGPTR ptr
= VA_ARG16( valist
, SEGPTR
);
452 args
[aix
] = (DWORD
)PTR_SEG_TO_LIN(ptr
);
453 dsprintf(relay
,"%08lx(%p),",ptr
,PTR_SEG_TO_LIN(ptr
));
457 args
[aix
] = VA_ARG16( valist
, DWORD
);
458 dsprintf(relay
,"%ld,",args
[aix
]);
461 dsprintf(relay
,"])");
464 if (!proc32
) ret
= 0;
465 else switch (nrofargs
)
467 case 0: ret
= proc32();
469 case 1: ret
= proc32(args
[0]);
471 case 2: ret
= proc32(args
[0],args
[1]);
473 case 3: ret
= proc32(args
[0],args
[1],args
[2]);
475 case 4: ret
= proc32(args
[0],args
[1],args
[2],args
[3]);
477 case 5: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4]);
479 case 6: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5]);
481 case 7: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6]);
483 case 8: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7]);
485 case 9: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8]);
487 case 10: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]);
489 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]);
492 /* FIXME: should go up to 32 arguments */
493 ERR(relay
,"Unsupported number of arguments %ld, please report.\n",nrofargs
);
497 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
498 if (!Ex
) STACK16_POP( THREAD_Current(),
499 (3 + nrofargs
) * sizeof(DWORD
) );
501 TRACE(relay
,"%s - returns %08lx\n",dbg_str(relay
),ret
);
502 HeapFree( GetProcessHeap(), 0, args
);
507 /**********************************************************************
508 * CallProc32W (KERNEL.517)
510 DWORD WINAPI
WIN16_CallProc32W()
512 return RELAY_CallProc32W(0);
516 /**********************************************************************
517 * CallProcEx32W() (KERNEL.518)
519 * C - style linkage to CallProc32W - caller pops stack.
521 DWORD WINAPI
WIN16_CallProcEx32W()
523 return RELAY_CallProc32W(TRUE
);