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 DWORD CALLTO16_RetAddr_word
;
33 extern DWORD CALLTO16_RetAddr_long
;
34 extern DWORD CALLTO16_RetAddr_eax
;
36 codesel
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)CALLTO16_Start
,
37 (int)CALLTO16_End
- (int)CALLTO16_Start
,
38 0, TRUE
, TRUE
, FALSE
, NULL
);
39 if (!codesel
) return FALSE
;
41 /* Patch the return addresses for CallTo16 routines */
43 CALLTO16_RetAddr_word
=MAKELONG( (int)CALLTO16_Ret_word
-(int)CALLTO16_Start
,
45 CALLTO16_RetAddr_long
=MAKELONG( (int)CALLTO16_Ret_long
-(int)CALLTO16_Start
,
47 CALLTO16_RetAddr_eax
=MAKELONG( (int)CALLTO16_Ret_eax
-(int)CALLTO16_Start
,
50 /* Create built-in modules */
51 if (!BUILTIN_Init()) return FALSE
;
53 /* Initialize thunking */
58 /* from relay32/relay386.c */
59 extern debug_relay_includelist
;
60 extern debug_relay_excludelist
;
62 /***********************************************************************
63 * RELAY_DebugCallFrom16
65 void RELAY_DebugCallFrom16( int func_type
, char *args
,
66 void *entry_point
, CONTEXT
*context
)
73 /* from relay32/relay386.c */
74 extern int RELAY_ShowDebugmsgRelay(const char *);
76 if (!TRACE_ON(relay
)) return;
78 frame
= CURRENT_STACK16
;
79 funstr
= BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,&ordinal
);
80 if (!funstr
) return; /* happens for the two snoop register relays */
81 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
82 DPRINTF( "Call %s(",funstr
);
85 if (func_type
& 4) /* cdecl */
93 DPRINTF( "0x%04x", *(WORD
*)args16
);
97 DPRINTF( "0x%08x", *(int *)args16
);
101 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
102 if (HIWORD(*(SEGPTR
*)args16
))
103 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
107 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
111 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
112 if (HIWORD( *(SEGPTR
*)args16
))
113 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
118 if (*args
) DPRINTF( "," );
123 /* Start with the last arg */
124 for (i
= 0; args
[i
]; i
++)
148 DPRINTF( "0x%04x", *(WORD
*)args16
);
152 DPRINTF( "0x%08x", *(int *)args16
);
156 DPRINTF( "0x%08x", *(int *)args16
);
157 if (HIWORD(*(SEGPTR
*)args16
))
158 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
162 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
166 DPRINTF( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
167 if (HIWORD( *(SEGPTR
*)args16
))
168 debug_dumpstr( (LPSTR
)PTR_SEG_TO_LIN(*(SEGPTR
*)args16
));
172 if (*args
) DPRINTF( "," );
176 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame
->cs
, frame
->ip
, frame
->ds
);
179 if (func_type
& 2) /* register function */
180 DPRINTF( " AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
181 AX_reg(context
), BX_reg(context
), CX_reg(context
),
182 DX_reg(context
), SI_reg(context
), DI_reg(context
),
183 (WORD
)ES_reg(context
), EFL_reg(context
) );
187 /***********************************************************************
188 * RELAY_DebugCallFrom16Ret
190 void RELAY_DebugCallFrom16Ret( int func_type
, int ret_val
, CONTEXT
*context
)
195 /* from relay32/relay386.c */
196 extern int RELAY_ShowDebugmsgRelay(const char *);
198 if (!TRACE_ON(relay
)) return;
199 frame
= CURRENT_STACK16
;
200 funstr
= BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,&ordinal
);
202 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
203 DPRINTF( "Ret %s() ",funstr
);
207 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
208 ret_val
, frame
->cs
, frame
->ip
, frame
->ds
);
211 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
212 ret_val
& 0xffff, frame
->cs
, frame
->ip
, frame
->ds
);
215 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
216 (WORD
)CS_reg(context
), IP_reg(context
), (WORD
)DS_reg(context
));
217 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
218 AX_reg(context
), BX_reg(context
), CX_reg(context
),
219 DX_reg(context
), SI_reg(context
), DI_reg(context
),
220 (WORD
)ES_reg(context
), EFL_reg(context
) );
226 /***********************************************************************
227 * RELAY_Unimplemented16
229 * This function is called for unimplemented 16-bit entry points (declared
230 * as 'stub' in the spec file).
232 void RELAY_Unimplemented16(void)
235 STACK16FRAME
*frame
= CURRENT_STACK16
;
236 MSG("No handler for Win16 routine %s (called from %04x:%04x)\n",
237 BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,&ordinal
),
238 frame
->cs
, frame
->ip
);
239 TASK_KillCurrentTask(1);
243 /***********************************************************************
244 * RELAY_DebugCallTo16
246 * 'stack' points to the called function address on the 32-bit stack.
251 * (stack) func to call
253 void RELAY_DebugCallTo16( int* stack
, int nb_args
)
257 if (!TRACE_ON(relay
)) return;
258 thdb
= THREAD_Current();
260 if (nb_args
== -1) /* Register function */
262 CONTEXT
*context
= (CONTEXT
*)stack
[0];
263 WORD
*stack16
= (WORD
*)THREAD_STACK16(thdb
);
264 DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
265 CS_reg(context
), IP_reg(context
), DS_reg(context
) );
266 nb_args
= stack
[1] / sizeof(WORD
);
269 DPRINTF( ",0x%04x", *stack16
);
271 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(thdb
->cur_stack
),
272 OFFSETOF(thdb
->cur_stack
) );
273 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x\n",
274 AX_reg(context
), BX_reg(context
), CX_reg(context
),
275 DX_reg(context
), SI_reg(context
), DI_reg(context
),
276 BP_reg(context
), (WORD
)ES_reg(context
) );
280 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
281 HIWORD(stack
[0]), LOWORD(stack
[0]),
282 SELECTOROF(thdb
->cur_stack
) );
285 DPRINTF(",0x%04x", *stack
);
288 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(thdb
->cur_stack
),
289 OFFSETOF(thdb
->cur_stack
) );
294 /**********************************************************************
298 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
300 void WINAPI
Catch( CONTEXT
*context
)
306 VA_START16( valist
);
307 buf
= VA_ARG16( valist
, SEGPTR
);
308 lpbuf
= (LPCATCHBUF
)PTR_SEG_TO_LIN( buf
);
311 /* Note: we don't save the current ss, as the catch buffer is */
312 /* only 9 words long. Hopefully no one will have the silly */
313 /* idea to change the current stack before calling Throw()... */
327 lpbuf
[0] = IP_reg(context
);
328 lpbuf
[1] = CS_reg(context
);
329 /* Windows pushes 4 more words before saving sp */
330 lpbuf
[2] = SP_reg(context
) - 4 * sizeof(WORD
);
331 lpbuf
[3] = BP_reg(context
);
332 lpbuf
[4] = SI_reg(context
);
333 lpbuf
[5] = DI_reg(context
);
334 lpbuf
[6] = DS_reg(context
);
336 lpbuf
[8] = SS_reg(context
);
337 AX_reg(context
) = 0; /* Return 0 */
341 /**********************************************************************
345 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
347 void WINAPI
Throw( CONTEXT
*context
)
352 STACK16FRAME
*pFrame
;
353 STACK32FRAME
*frame32
;
354 THDB
*thdb
= THREAD_Current();
356 VA_START16( valist
);
357 AX_reg(context
) = VA_ARG16( valist
, WORD
); /* retval */
358 buf
= VA_ARG16( valist
, SEGPTR
);
359 lpbuf
= (LPCATCHBUF
)PTR_SEG_TO_LIN( buf
);
362 /* Find the frame32 corresponding to the frame16 we are jumping to */
363 pFrame
= THREAD_STACK16( thdb
);
364 frame32
= THREAD_STACK16( thdb
)->frame32
;
365 while (frame32
&& frame32
->frame16
)
367 if (OFFSETOF(frame32
->frame16
) < OFFSETOF(thdb
->cur_stack
))
368 break; /* Something strange is going on */
369 if (OFFSETOF(frame32
->frame16
) > lpbuf
[2])
371 /* We found the right frame */
372 pFrame
->frame32
= frame32
;
375 frame32
= ((STACK16FRAME
*)PTR_SEG_TO_LIN(frame32
->frame16
))->frame32
;
378 IP_reg(context
) = lpbuf
[0];
379 CS_reg(context
) = lpbuf
[1];
380 SP_reg(context
) = lpbuf
[2] + 4 * sizeof(WORD
) - sizeof(WORD
) /*extra arg*/;
381 BP_reg(context
) = lpbuf
[3];
382 SI_reg(context
) = lpbuf
[4];
383 DI_reg(context
) = lpbuf
[5];
384 DS_reg(context
) = lpbuf
[6];
386 if (lpbuf
[8] != SS_reg(context
))
387 ERR(relay
, "Switching stack segment with Throw() not supported; expect crash now\n" );
389 if (TRACE_ON(relay
)) /* Make sure we have a valid entry point address */
391 static FARPROC16 entryPoint
= NULL
;
393 if (!entryPoint
) /* Get entry point for Throw() */
394 entryPoint
= NE_GetEntryPoint( GetModuleHandle16("KERNEL"), 56 );
395 pFrame
->entry_cs
= SELECTOROF(entryPoint
);
396 pFrame
->entry_ip
= OFFSETOF(entryPoint
);
401 /**********************************************************************
404 * Helper for CallProc[Ex]32W
406 static DWORD
RELAY_CallProc32W(int Ex
)
408 DWORD nrofargs
, argconvmask
;
413 dbg_decl_str(relay
, 1024);
415 VA_START16( valist
);
416 nrofargs
= VA_ARG16( valist
, DWORD
);
417 argconvmask
= VA_ARG16( valist
, DWORD
);
418 proc32
= VA_ARG16( valist
, FARPROC32
);
419 dsprintf(relay
, "CallProc32W(%ld,%ld,%p, Ex%d args[",nrofargs
,argconvmask
,proc32
,Ex
);
420 args
= (DWORD
*)HEAP_xalloc( GetProcessHeap(), 0,
421 sizeof(DWORD
)*nrofargs
);
422 for (i
=0;i
<nrofargs
;i
++) {
423 if (argconvmask
& (1<<i
))
425 SEGPTR ptr
= VA_ARG16( valist
, SEGPTR
);
426 args
[nrofargs
-i
-1] = (DWORD
)PTR_SEG_TO_LIN(ptr
);
427 dsprintf(relay
,"%08lx(%p),",ptr
,PTR_SEG_TO_LIN(ptr
));
431 args
[nrofargs
-i
-1] = VA_ARG16( valist
, DWORD
);
432 dsprintf(relay
,"%ld,",args
[nrofargs
-i
-1]);
435 dsprintf(relay
,"])");
439 case 0: ret
= proc32();
441 case 1: ret
= proc32(args
[0]);
443 case 2: ret
= proc32(args
[0],args
[1]);
445 case 3: ret
= proc32(args
[0],args
[1],args
[2]);
447 case 4: ret
= proc32(args
[0],args
[1],args
[2],args
[3]);
449 case 5: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4]);
451 case 6: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5]);
453 case 7: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6]);
455 case 8: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7]);
457 case 9: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8]);
459 case 10: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]);
461 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]);
464 /* FIXME: should go up to 32 arguments */
465 ERR(relay
,"Unsupported number of arguments %ld, please report.\n",nrofargs
);
469 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
470 if (!Ex
) STACK16_POP( THREAD_Current(),
471 (3 + nrofargs
) * sizeof(DWORD
) );
473 TRACE(relay
,"%s - returns %08lx\n",dbg_str(relay
),ret
);
474 HeapFree( GetProcessHeap(), 0, args
);
479 /**********************************************************************
480 * CallProc32W (KERNEL.517)
482 DWORD WINAPI
WIN16_CallProc32W()
484 return RELAY_CallProc32W(0);
488 /**********************************************************************
489 * CallProcEx32W() (KERNEL.518)
491 * C - style linkage to CallProc32W - caller pops stack.
493 DWORD WINAPI
WIN16_CallProcEx32W()
495 return RELAY_CallProc32W(TRUE
);