2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
14 #include "stackframe.h"
17 /* #define DEBUG_RELAY */
21 /* Make make_debug think these were really used */
26 /***********************************************************************
29 BOOL32
RELAY_Init(void)
32 extern BOOL32
THUNK_Init(void);
34 /* Allocate the code selector for CallTo16 routines */
36 extern void CALLTO16_Start(), CALLTO16_End();
37 extern void CALLTO16_Ret_word(), CALLTO16_Ret_long(), CALLTO16_Ret_regs();
38 extern DWORD CALLTO16_RetAddr_word
;
39 extern DWORD CALLTO16_RetAddr_long
;
40 extern DWORD CALLTO16_RetAddr_regs
;
42 codesel
= GLOBAL_CreateBlock( GMEM_FIXED
, (void *)CALLTO16_Start
,
43 (int)CALLTO16_End
- (int)CALLTO16_Start
,
44 0, TRUE
, TRUE
, FALSE
, NULL
);
45 if (!codesel
) return FALSE
;
47 /* Patch the return addresses for CallTo16 routines */
49 CALLTO16_RetAddr_word
=MAKELONG( (int)CALLTO16_Ret_word
-(int)CALLTO16_Start
,
51 CALLTO16_RetAddr_long
=MAKELONG( (int)CALLTO16_Ret_long
-(int)CALLTO16_Start
,
53 CALLTO16_RetAddr_regs
=MAKELONG( (int)CALLTO16_Ret_regs
-(int)CALLTO16_Start
,
56 /* Create built-in modules */
57 if (!BUILTIN_Init()) return FALSE
;
59 /* Initialize thunking */
64 /***********************************************************************
65 * RELAY_DebugCallFrom16
67 void RELAY_DebugCallFrom16( int func_type
, char *args
,
68 void *entry_point
, CONTEXT
*context
)
75 if (!debugging_relay
) return;
77 frame
= CURRENT_STACK16
;
78 printf( "Call %s(", BUILTIN_GetEntryPoint16( frame
->entry_cs
,
82 for (i
= 0; i
< strlen(args
); i
++)
106 printf( "0x%04x", *(WORD
*)args16
);
110 printf( "0x%08x", *(int *)args16
);
114 printf( "0x%08x", *(int *)args16
);
115 if (HIWORD(*(int *)args16
))
116 printf( " \"%s\"", (char *)PTR_SEG_TO_LIN(*(int *)args16
) );
120 printf( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
124 printf( "%04x:%04x", *(WORD
*)(args16
+2), *(WORD
*)args16
);
125 if (HIWORD(*(int *)args16
))
126 printf( " \"%s\"", (char *)PTR_SEG_TO_LIN(*(int *)args16
) );
130 if (*args
) printf( "," );
132 printf( ") ret=%04x:%04x ds=%04x\n", frame
->cs
, frame
->ip
, frame
->ds
);
135 if (func_type
== 2) /* register function */
136 printf( " AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
137 AX_reg(context
), BX_reg(context
), CX_reg(context
),
138 DX_reg(context
), SI_reg(context
), DI_reg(context
),
139 (WORD
)ES_reg(context
), EFL_reg(context
) );
143 /***********************************************************************
144 * RELAY_DebugCallFrom16Ret
146 void RELAY_DebugCallFrom16Ret( int func_type
, int ret_val
, CONTEXT
*context
)
151 if (!debugging_relay
) return;
152 frame
= CURRENT_STACK16
;
153 printf( "Ret %s() ", BUILTIN_GetEntryPoint16( frame
->entry_cs
,
159 printf( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
160 ret_val
, frame
->cs
, frame
->ip
, frame
->ds
);
163 printf( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
164 ret_val
& 0xffff, frame
->cs
, frame
->ip
, frame
->ds
);
167 printf( "retval=none ret=%04x:%04x ds=%04x\n",
168 (WORD
)CS_reg(context
), IP_reg(context
), (WORD
)DS_reg(context
));
169 printf( " AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
170 AX_reg(context
), BX_reg(context
), CX_reg(context
),
171 DX_reg(context
), SI_reg(context
), DI_reg(context
),
172 (WORD
)ES_reg(context
), EFL_reg(context
) );
178 /***********************************************************************
179 * RELAY_Unimplemented16
181 * This function is called for unimplemented 16-bit entry points (declared
182 * as 'stub' in the spec file).
184 void RELAY_Unimplemented16(void)
187 STACK16FRAME
*frame
= CURRENT_STACK16
;
188 fprintf(stderr
,"No handler for Win16 routine %s (called from %04x:%04x)\n",
189 BUILTIN_GetEntryPoint16(frame
->entry_cs
,frame
->entry_ip
,&ordinal
),
190 frame
->cs
, frame
->ip
);
191 TASK_KillCurrentTask(1);
195 /***********************************************************************
196 * RELAY_DebugCallTo16
198 * 'stack' points to the called function address on the 32-bit stack.
203 * (stack) func to call
205 void RELAY_DebugCallTo16( int* stack
, int nb_args
)
207 if (!debugging_relay
) return;
209 if (nb_args
== -1) /* Register function */
211 CONTEXT
*context
= (CONTEXT
*)stack
[0];
212 WORD
*stack16
= (WORD
*)CURRENT_STACK16
- 2 /* for saved %%esp */;
213 printf( "CallTo16(func=%04lx:%04x,ds=%04lx",
214 CS_reg(context
), IP_reg(context
), DS_reg(context
) );
215 nb_args
= -stack
[1] / sizeof(WORD
);
216 while (nb_args
--) printf( ",0x%04x", *(--stack16
) );
218 printf( " AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x\n",
219 AX_reg(context
), BX_reg(context
), CX_reg(context
),
220 DX_reg(context
), SI_reg(context
), DI_reg(context
),
221 BP_reg(context
), (WORD
)ES_reg(context
) );
225 printf( "CallTo16(func=%04x:%04x,ds=%04x",
226 HIWORD(stack
[0]), LOWORD(stack
[0]),
227 SELECTOROF(IF1632_Saved16_ss_sp
) );
229 while (nb_args
--) printf( ",0x%04x", *stack
++ );
235 /**********************************************************************
239 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
241 void WINAPI
Catch( CONTEXT
*context
)
246 STACK16FRAME
*pFrame
= CURRENT_STACK16
;
248 VA_START16( valist
);
249 buf
= VA_ARG16( valist
, SEGPTR
);
250 lpbuf
= (LPCATCHBUF
)PTR_SEG_TO_LIN( buf
);
253 /* Note: we don't save the current ss, as the catch buffer is */
254 /* only 9 words long. Hopefully no one will have the silly */
255 /* idea to change the current stack before calling Throw()... */
269 lpbuf
[0] = IP_reg(context
);
270 lpbuf
[1] = CS_reg(context
);
271 lpbuf
[2] = LOWORD(pFrame
->saved_ss_sp
);
272 lpbuf
[3] = BP_reg(context
);
273 lpbuf
[4] = SI_reg(context
);
274 lpbuf
[5] = DI_reg(context
);
275 lpbuf
[6] = DS_reg(context
);
276 lpbuf
[7] = OFFSETOF(IF1632_Saved16_ss_sp
);
277 lpbuf
[8] = HIWORD(pFrame
->saved_ss_sp
);
278 AX_reg(context
) = 0; /* Return 0 */
282 /**********************************************************************
286 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
288 void WINAPI
Throw( CONTEXT
*context
)
293 STACK16FRAME
*pFrame
;
295 VA_START16( valist
);
296 AX_reg(context
) = VA_ARG16( valist
, WORD
); /* retval */
297 buf
= VA_ARG16( valist
, SEGPTR
);
298 lpbuf
= (LPCATCHBUF
)PTR_SEG_TO_LIN( buf
);
301 IF1632_Saved16_ss_sp
= MAKELONG( lpbuf
[7] - sizeof(WORD
),
302 HIWORD(IF1632_Saved16_ss_sp
) );
303 pFrame
= CURRENT_STACK16
;
304 pFrame
->saved_ss_sp
= MAKELONG( lpbuf
[2], lpbuf
[8] );
305 IP_reg(context
) = lpbuf
[0];
306 CS_reg(context
) = lpbuf
[1];
307 BP_reg(context
) = lpbuf
[3];
308 SI_reg(context
) = lpbuf
[4];
309 DI_reg(context
) = lpbuf
[5];
310 DS_reg(context
) = lpbuf
[6];
312 if (debugging_relay
) /* Make sure we have a valid entry point address */
314 static FARPROC16 entryPoint
= NULL
;
316 if (!entryPoint
) /* Get entry point for Throw() */
317 entryPoint
= MODULE_GetEntryPoint( GetModuleHandle16("KERNEL"),
319 pFrame
->entry_cs
= SELECTOROF(entryPoint
);
320 pFrame
->entry_ip
= OFFSETOF(entryPoint
);
324 /**********************************************************************
325 * CallProc32W (KERNEL.517)
327 DWORD WINAPI
WIN16_CallProc32W()
329 DWORD nrofargs
, argconvmask
;
335 VA_START16( valist
);
336 nrofargs
= VA_ARG16( valist
, DWORD
);
337 argconvmask
= VA_ARG16( valist
, DWORD
);
338 proc32
= VA_ARG16( valist
, FARPROC32
);
339 fprintf(stderr
,"CallProc32W(%ld,%ld,%p,args[",nrofargs
,argconvmask
,proc32
);
340 args
= (DWORD
*)HEAP_xalloc( GetProcessHeap(), 0,
341 sizeof(DWORD
)*nrofargs
);
342 for (i
=0;i
<nrofargs
;i
++) {
343 if (argconvmask
& (1<<i
))
345 SEGPTR ptr
= VA_ARG16( valist
, SEGPTR
);
346 args
[nrofargs
-i
-1] = (DWORD
)PTR_SEG_TO_LIN(ptr
);
347 fprintf(stderr
,"%08lx(%p),",ptr
,PTR_SEG_TO_LIN(ptr
));
351 args
[nrofargs
-i
-1] = VA_ARG16( valist
, DWORD
);
352 fprintf(stderr
,"%ld,",args
[nrofargs
-i
-1]);
355 fprintf(stderr
,"]) - ");
359 case 0: ret
= proc32();
361 case 1: ret
= proc32(args
[0]);
363 case 2: ret
= proc32(args
[0],args
[1]);
365 case 3: ret
= proc32(args
[0],args
[1],args
[2]);
367 case 4: ret
= proc32(args
[0],args
[1],args
[2],args
[3]);
369 case 5: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4]);
371 case 6: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5]);
373 case 7: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6]);
376 /* FIXME: should go up to 32 arguments */
377 fprintf(stderr
,"CallProc32W: unsupported number of arguments %ld, please report.\n",nrofargs
);
381 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
382 STACK16_POP( (3 + nrofargs
) * sizeof(DWORD
) );
384 fprintf(stderr
,"returns %08lx\n",ret
);
385 HeapFree( GetProcessHeap(), 0, args
);