2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
13 #include "stackframe.h"
20 /***********************************************************************
23 BOOL32 RELAY_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 void CALL32_CBClientEx_Ret();
34 extern DWORD CALLTO16_RetAddr_word;
35 extern DWORD CALLTO16_RetAddr_long;
36 extern DWORD CALLTO16_RetAddr_eax;
37 extern DWORD CALL32_CBClient_RetAddr;
38 extern DWORD CALL32_CBClientEx_RetAddr;
40 codesel = GLOBAL_CreateBlock( GMEM_FIXED, (void *)CALLTO16_Start,
41 (int)CALLTO16_End - (int)CALLTO16_Start,
42 0, TRUE, TRUE, FALSE, NULL );
43 if (!codesel) return FALSE;
45 /* Patch the return addresses for CallTo16 routines */
47 CALLTO16_RetAddr_word=MAKELONG( (int)CALLTO16_Ret_word-(int)CALLTO16_Start,
49 CALLTO16_RetAddr_long=MAKELONG( (int)CALLTO16_Ret_long-(int)CALLTO16_Start,
51 CALLTO16_RetAddr_eax =MAKELONG( (int)CALLTO16_Ret_eax -(int)CALLTO16_Start,
53 CALL32_CBClient_RetAddr =
54 MAKELONG( (int)CALL32_CBClient_Ret -(int)CALLTO16_Start, codesel );
55 CALL32_CBClientEx_RetAddr =
56 MAKELONG( (int)CALL32_CBClientEx_Ret -(int)CALLTO16_Start, codesel );
58 /* Create built-in modules */
59 if (!BUILTIN_Init()) return FALSE;
61 /* Initialize thunking */
66 /* from relay32/relay386.c */
67 extern char **debug_relay_excludelist,**debug_relay_includelist;
69 /***********************************************************************
70 * RELAY_DebugCallFrom16
72 void RELAY_DebugCallFrom16( int func_type, char *args,
73 void *entry_point, CONTEXT *context )
81 if (!TRACE_ON(relay)) return;
83 frame = CURRENT_STACK16;
84 funstr = BUILTIN_GetEntryPoint16(frame->entry_cs,frame->entry_ip,&ordinal);
85 if (!funstr) return; /* happens for the two snoop register relays */
86 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
87 DPRINTF( "Call %s(",funstr);
90 if (func_type & 4) /* cdecl */
98 DPRINTF( "0x%04x", *(WORD *)args16 );
102 DPRINTF( "0x%08x", *(int *)args16 );
106 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
107 if (HIWORD(*(SEGPTR *)args16))
108 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
112 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
116 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
117 if (HIWORD( *(SEGPTR *)args16 ))
118 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
123 if (*args) DPRINTF( "," );
128 /* Start with the last arg */
129 for (i = 0; args[i]; i++)
153 DPRINTF( "0x%04x", *(WORD *)args16 );
157 DPRINTF( "0x%08x", *(int *)args16 );
161 DPRINTF( "0x%08x", *(int *)args16 );
162 if (HIWORD(*(SEGPTR *)args16))
163 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
167 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
171 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
172 if (HIWORD( *(SEGPTR *)args16 ))
173 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
177 if (*args) DPRINTF( "," );
181 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
184 if (func_type & 2) /* register function */
185 DPRINTF( " AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
186 AX_reg(context), BX_reg(context), CX_reg(context),
187 DX_reg(context), SI_reg(context), DI_reg(context),
188 (WORD)ES_reg(context), EFL_reg(context) );
192 /***********************************************************************
193 * RELAY_DebugCallFrom16Ret
195 void RELAY_DebugCallFrom16Ret( int func_type, int ret_val, CONTEXT *context)
201 if (!TRACE_ON(relay)) return;
202 frame = CURRENT_STACK16;
203 funstr = BUILTIN_GetEntryPoint16(frame->entry_cs,frame->entry_ip,&ordinal);
205 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
206 DPRINTF( "Ret %s() ",funstr);
210 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
211 ret_val, frame->cs, frame->ip, frame->ds );
214 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
215 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
218 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
219 (WORD)CS_reg(context), IP_reg(context), (WORD)DS_reg(context));
220 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
221 AX_reg(context), BX_reg(context), CX_reg(context),
222 DX_reg(context), SI_reg(context), DI_reg(context),
223 (WORD)ES_reg(context), EFL_reg(context) );
229 /***********************************************************************
230 * RELAY_Unimplemented16
232 * This function is called for unimplemented 16-bit entry points (declared
233 * as 'stub' in the spec file).
235 void RELAY_Unimplemented16(void)
238 STACK16FRAME *frame = CURRENT_STACK16;
239 MSG("No handler for Win16 routine %s (called from %04x:%04x)\n",
240 BUILTIN_GetEntryPoint16(frame->entry_cs,frame->entry_ip,&ordinal),
241 frame->cs, frame->ip );
246 /***********************************************************************
247 * RELAY_DebugCallTo16
249 * 'stack' points to the called function address on the 32-bit stack.
254 * (stack) func to call
256 void RELAY_DebugCallTo16( int* stack, int nb_args )
260 if (!TRACE_ON(relay)) return;
261 thdb = THREAD_Current();
263 if (nb_args == -1) /* Register function */
265 CONTEXT *context = (CONTEXT *)stack[0];
266 WORD *stack16 = (WORD *)THREAD_STACK16(thdb);
267 DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
268 CS_reg(context), IP_reg(context), DS_reg(context) );
269 nb_args = stack[1] / sizeof(WORD);
272 DPRINTF( ",0x%04x", *stack16 );
274 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(thdb->cur_stack),
275 OFFSETOF(thdb->cur_stack) );
276 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
277 AX_reg(context), BX_reg(context), CX_reg(context),
278 DX_reg(context), SI_reg(context), DI_reg(context),
279 BP_reg(context), (WORD)ES_reg(context), (WORD)FS_reg(context) );
283 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
284 HIWORD(stack[0]), LOWORD(stack[0]),
285 SELECTOROF(thdb->cur_stack) );
288 DPRINTF(",0x%04x", *stack );
291 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(thdb->cur_stack),
292 OFFSETOF(thdb->cur_stack) );
297 /***********************************************************************
298 * RELAY_DebugCallTo16Ret
300 void RELAY_DebugCallTo16Ret( int ret_val )
304 if (!TRACE_ON(relay)) return;
305 thdb = THREAD_Current();
307 DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n",
308 SELECTOROF(thdb->cur_stack), OFFSETOF(thdb->cur_stack), ret_val);
312 /**********************************************************************
316 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
318 void WINAPI Catch( CONTEXT *context )
324 VA_START16( valist );
325 buf = VA_ARG16( valist, SEGPTR );
326 lpbuf = (LPCATCHBUF)PTR_SEG_TO_LIN( buf );
329 /* Note: we don't save the current ss, as the catch buffer is */
330 /* only 9 words long. Hopefully no one will have the silly */
331 /* idea to change the current stack before calling Throw()... */
345 lpbuf[0] = IP_reg(context);
346 lpbuf[1] = CS_reg(context);
347 /* Windows pushes 4 more words before saving sp */
348 lpbuf[2] = SP_reg(context) - 4 * sizeof(WORD);
349 lpbuf[3] = BP_reg(context);
350 lpbuf[4] = SI_reg(context);
351 lpbuf[5] = DI_reg(context);
352 lpbuf[6] = DS_reg(context);
354 lpbuf[8] = SS_reg(context);
355 AX_reg(context) = 0; /* Return 0 */
359 /**********************************************************************
363 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
365 void WINAPI Throw( CONTEXT *context )
370 STACK16FRAME *pFrame;
371 STACK32FRAME *frame32;
372 THDB *thdb = THREAD_Current();
374 VA_START16( valist );
375 AX_reg(context) = VA_ARG16( valist, WORD ); /* retval */
376 buf = VA_ARG16( valist, SEGPTR );
377 lpbuf = (LPCATCHBUF)PTR_SEG_TO_LIN( buf );
380 /* Find the frame32 corresponding to the frame16 we are jumping to */
381 pFrame = THREAD_STACK16( thdb );
382 frame32 = THREAD_STACK16( thdb )->frame32;
383 while (frame32 && frame32->frame16)
385 if (OFFSETOF(frame32->frame16) < OFFSETOF(thdb->cur_stack))
386 break; /* Something strange is going on */
387 if (OFFSETOF(frame32->frame16) > lpbuf[2])
389 /* We found the right frame */
390 pFrame->frame32 = frame32;
393 frame32 = ((STACK16FRAME *)PTR_SEG_TO_LIN(frame32->frame16))->frame32;
396 IP_reg(context) = lpbuf[0];
397 CS_reg(context) = lpbuf[1];
398 SP_reg(context) = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
399 BP_reg(context) = lpbuf[3];
400 SI_reg(context) = lpbuf[4];
401 DI_reg(context) = lpbuf[5];
402 DS_reg(context) = lpbuf[6];
404 if (lpbuf[8] != SS_reg(context))
405 ERR(relay, "Switching stack segment with Throw() not supported; expect crash now\n" );
407 if (TRACE_ON(relay)) /* Make sure we have a valid entry point address */
409 static FARPROC16 entryPoint = NULL;
411 if (!entryPoint) /* Get entry point for Throw() */
412 entryPoint = NE_GetEntryPoint( GetModuleHandle16("KERNEL"), 56 );
413 pFrame->entry_cs = SELECTOROF(entryPoint);
414 pFrame->entry_ip = OFFSETOF(entryPoint);
419 /**********************************************************************
422 * Helper for CallProc[Ex]32W
424 static DWORD RELAY_CallProc32W(int Ex)
426 DWORD nrofargs, argconvmask;
432 dbg_decl_str(relay, 1024);
434 VA_START16( valist );
435 nrofargs = VA_ARG16( valist, DWORD );
436 argconvmask = VA_ARG16( valist, DWORD );
437 proc32 = VA_ARG16( valist, FARPROC32 );
438 dsprintf(relay, "CallProc32W(%ld,%ld,%p, Ex%d args[",nrofargs,argconvmask,proc32,Ex);
439 args = (DWORD*)HEAP_xalloc( GetProcessHeap(), 0,
440 sizeof(DWORD)*nrofargs );
441 /* CallProcEx doesn't need its args reversed */
442 for (i=0;i<nrofargs;i++) {
446 aix = nrofargs - i - 1;
448 if (argconvmask & (1<<i))
450 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
451 args[aix] = (DWORD)PTR_SEG_TO_LIN(ptr);
452 dsprintf(relay,"%08lx(%p),",ptr,PTR_SEG_TO_LIN(ptr));
456 args[aix] = VA_ARG16( valist, DWORD );
457 dsprintf(relay,"%ld,",args[aix]);
460 dsprintf(relay,"])");
463 if (!proc32) ret = 0;
464 else switch (nrofargs)
466 case 0: ret = proc32();
468 case 1: ret = proc32(args[0]);
470 case 2: ret = proc32(args[0],args[1]);
472 case 3: ret = proc32(args[0],args[1],args[2]);
474 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
476 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
478 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
480 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
482 case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
484 case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
486 case 10: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
488 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]);
491 /* FIXME: should go up to 32 arguments */
492 ERR(relay,"Unsupported number of arguments %ld, please report.\n",nrofargs);
496 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
497 if (!Ex) STACK16_POP( THREAD_Current(),
498 (3 + nrofargs) * sizeof(DWORD) );
500 TRACE(relay,"%s - returns %08lx\n",dbg_str(relay),ret);
501 HeapFree( GetProcessHeap(), 0, args );
506 /**********************************************************************
507 * CallProc32W (KERNEL.517)
509 DWORD WINAPI WIN16_CallProc32W()
511 return RELAY_CallProc32W(0);
515 /**********************************************************************
516 * CallProcEx32W() (KERNEL.518)
518 * C - style linkage to CallProc32W - caller pops stack.
520 DWORD WINAPI WIN16_CallProcEx32W()
522 return RELAY_CallProc32W(TRUE);