2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
9 #include "wine/winbase16.h"
14 #include "stackframe.h"
21 /***********************************************************************
28 /* Allocate the code selector for CallTo16 routines */
30 extern void CALLTO16_Start(), CALLTO16_End();
31 extern void CALLTO16_Ret_word(), CALLTO16_Ret_long();
32 extern void CALLTO16_Ret_eax();
33 extern void CALL32_CBClient_Ret();
34 extern void CALL32_CBClientEx_Ret();
35 extern DWORD CALLTO16_RetAddr_word
;
36 extern DWORD CALLTO16_RetAddr_long
;
37 extern DWORD CALLTO16_RetAddr_eax
;
38 extern DWORD CALL32_CBClient_RetAddr
;
39 extern DWORD CALL32_CBClientEx_RetAddr
;
41 codesel
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)CALLTO16_Start
,
42 (int)CALLTO16_End
- (int)CALLTO16_Start
,
43 0, TRUE
, TRUE
, FALSE
, NULL
);
44 if (!codesel
) return FALSE
;
46 /* Patch the return addresses for CallTo16 routines */
48 CALLTO16_RetAddr_word
=MAKELONG( (int)CALLTO16_Ret_word
-(int)CALLTO16_Start
,
50 CALLTO16_RetAddr_long
=MAKELONG( (int)CALLTO16_Ret_long
-(int)CALLTO16_Start
,
52 CALLTO16_RetAddr_eax
=MAKELONG( (int)CALLTO16_Ret_eax
-(int)CALLTO16_Start
,
54 CALL32_CBClient_RetAddr
=
55 MAKELONG( (int)CALL32_CBClient_Ret
-(int)CALLTO16_Start
, codesel
);
56 CALL32_CBClientEx_RetAddr
=
57 MAKELONG( (int)CALL32_CBClientEx_Ret
-(int)CALLTO16_Start
, codesel
);
59 /* Create built-in modules */
60 if (!BUILTIN_Init()) return FALSE
;
62 /* Initialize thunking */
67 /* from relay32/relay386.c */
68 extern char **debug_relay_excludelist
,**debug_relay_includelist
;
70 /***********************************************************************
71 * RELAY_DebugCallFrom16
73 void RELAY_DebugCallFrom16( int func_type
, char *args
,
74 void *entry_point
, CONTEXT
*context
)
82 if (!TRACE_ON(relay
)) return;
84 frame
= CURRENT_STACK16
;
85 funstr
= BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,&ordinal
);
86 if (!funstr
) return; /* happens for the two snoop register relays */
87 if (!RELAY_ShowDebugmsgRelay(funstr
)) return;
88 DPRINTF( "Call %s(",funstr
);
91 if (func_type
& 4) /* cdecl */
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
);
185 if (func_type
& 2) /* register function */
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
) );
193 /***********************************************************************
194 * RELAY_DebugCallFrom16Ret
196 void RELAY_DebugCallFrom16Ret( int func_type
, int ret_val
, CONTEXT
*context
)
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
Catch16( 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
Throw16( 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
, FARPROC
);
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
CallProc32W_16()
512 return RELAY_CallProc32W(0);
516 /**********************************************************************
517 * CallProcEx32W() (KERNEL.518)
519 * C - style linkage to CallProc32W - caller pops stack.
521 DWORD WINAPI
CallProcEx32W_16()
523 return RELAY_CallProc32W(TRUE
);