Release 980927
[wine.git] / if1632 / relay.c
blobee7af275d1d2dc16c1da7ef5f865bf2f8094dd43
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
4 */
6 #include <assert.h>
7 #include <stdlib.h>
8 #include "windows.h"
9 #include "winnt.h"
10 #include "global.h"
11 #include "heap.h"
12 #include "module.h"
13 #include "stackframe.h"
14 #include "task.h"
15 #include "debugstr.h"
16 #include "debug.h"
19 /***********************************************************************
20 * RELAY_Init
22 BOOL32 RELAY_Init(void)
24 WORD codesel;
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,
44 codesel );
45 CALLTO16_RetAddr_long=MAKELONG( (int)CALLTO16_Ret_long-(int)CALLTO16_Start,
46 codesel );
47 CALLTO16_RetAddr_eax =MAKELONG( (int)CALLTO16_Ret_eax -(int)CALLTO16_Start,
48 codesel );
50 /* Create built-in modules */
51 if (!BUILTIN_Init()) return FALSE;
53 /* Initialize thunking */
54 return THUNK_Init();
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 )
68 STACK16FRAME *frame;
69 WORD ordinal;
70 char *args16;
71 const char *funstr;
72 int i;
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);
83 VA_START16( args16 );
85 if (func_type & 4) /* cdecl */
87 while (*args)
89 switch(*args)
91 case 'w':
92 case 's':
93 DPRINTF( "0x%04x", *(WORD *)args16 );
94 args16 += 2;
95 break;
96 case 'l':
97 DPRINTF( "0x%08x", *(int *)args16 );
98 args16 += 4;
99 break;
100 case 't':
101 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
102 if (HIWORD(*(SEGPTR *)args16))
103 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
104 args16 += 4;
105 break;
106 case 'p':
107 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
108 args16 += 4;
109 break;
110 case 'T':
111 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
112 if (HIWORD( *(SEGPTR *)args16 ))
113 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
114 args16 += 4;
115 break;
117 args++;
118 if (*args) DPRINTF( "," );
121 else /* not cdecl */
123 /* Start with the last arg */
124 for (i = 0; args[i]; i++)
126 switch(args[i])
128 case 'w':
129 case 's':
130 args16 += 2;
131 break;
132 case 'l':
133 case 'p':
134 case 't':
135 case 'T':
136 args16 += 4;
137 break;
141 while (*args)
143 switch(*args)
145 case 'w':
146 case 's':
147 args16 -= 2;
148 DPRINTF( "0x%04x", *(WORD *)args16 );
149 break;
150 case 'l':
151 args16 -= 4;
152 DPRINTF( "0x%08x", *(int *)args16 );
153 break;
154 case 't':
155 args16 -= 4;
156 DPRINTF( "0x%08x", *(int *)args16 );
157 if (HIWORD(*(SEGPTR *)args16))
158 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
159 break;
160 case 'p':
161 args16 -= 4;
162 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
163 break;
164 case 'T':
165 args16 -= 4;
166 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
167 if (HIWORD( *(SEGPTR *)args16 ))
168 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
169 break;
171 args++;
172 if (*args) DPRINTF( "," );
176 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
177 VA_END16( args16 );
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)
192 STACK16FRAME *frame;
193 WORD ordinal;
194 const char *funstr;
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);
201 if (!funstr) return;
202 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
203 DPRINTF( "Ret %s() ",funstr);
204 switch(func_type)
206 case 0: /* long */
207 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
208 ret_val, frame->cs, frame->ip, frame->ds );
209 break;
210 case 1: /* word */
211 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
212 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
213 break;
214 case 2: /* regs */
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) );
221 break;
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)
234 WORD ordinal;
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 ExitProcess(1);
243 /***********************************************************************
244 * RELAY_DebugCallTo16
246 * 'stack' points to the called function address on the 32-bit stack.
247 * Stack layout:
248 * ... ...
249 * (stack+8) arg2
250 * (stack+4) arg1
251 * (stack) func to call
253 void RELAY_DebugCallTo16( int* stack, int nb_args )
255 THDB *thdb;
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);
267 while (nb_args--) {
268 --stack16;
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 FS=%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), (WORD)FS_reg(context) );
278 else
280 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
281 HIWORD(stack[0]), LOWORD(stack[0]),
282 SELECTOROF(thdb->cur_stack) );
283 stack++;
284 while (nb_args--) {
285 DPRINTF(",0x%04x", *stack );
286 stack++;
288 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(thdb->cur_stack),
289 OFFSETOF(thdb->cur_stack) );
294 /***********************************************************************
295 * RELAY_DebugCallTo16Ret
297 void RELAY_DebugCallTo16Ret( int ret_val )
299 THDB *thdb;
301 if (!TRACE_ON(relay)) return;
302 thdb = THREAD_Current();
304 DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n",
305 SELECTOROF(thdb->cur_stack), OFFSETOF(thdb->cur_stack), ret_val);
309 /**********************************************************************
310 * Catch (KERNEL.55)
312 * Real prototype is:
313 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
315 void WINAPI Catch( CONTEXT *context )
317 VA_LIST16 valist;
318 SEGPTR buf;
319 LPCATCHBUF lpbuf;
321 VA_START16( valist );
322 buf = VA_ARG16( valist, SEGPTR );
323 lpbuf = (LPCATCHBUF)PTR_SEG_TO_LIN( buf );
324 VA_END16( valist );
326 /* Note: we don't save the current ss, as the catch buffer is */
327 /* only 9 words long. Hopefully no one will have the silly */
328 /* idea to change the current stack before calling Throw()... */
330 /* Windows uses:
331 * lpbuf[0] = ip
332 * lpbuf[1] = cs
333 * lpbuf[2] = sp
334 * lpbuf[3] = bp
335 * lpbuf[4] = si
336 * lpbuf[5] = di
337 * lpbuf[6] = ds
338 * lpbuf[7] = unused
339 * lpbuf[8] = ss
342 lpbuf[0] = IP_reg(context);
343 lpbuf[1] = CS_reg(context);
344 /* Windows pushes 4 more words before saving sp */
345 lpbuf[2] = SP_reg(context) - 4 * sizeof(WORD);
346 lpbuf[3] = BP_reg(context);
347 lpbuf[4] = SI_reg(context);
348 lpbuf[5] = DI_reg(context);
349 lpbuf[6] = DS_reg(context);
350 lpbuf[7] = 0;
351 lpbuf[8] = SS_reg(context);
352 AX_reg(context) = 0; /* Return 0 */
356 /**********************************************************************
357 * Throw (KERNEL.56)
359 * Real prototype is:
360 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
362 void WINAPI Throw( CONTEXT *context )
364 VA_LIST16 valist;
365 SEGPTR buf;
366 LPCATCHBUF lpbuf;
367 STACK16FRAME *pFrame;
368 STACK32FRAME *frame32;
369 THDB *thdb = THREAD_Current();
371 VA_START16( valist );
372 AX_reg(context) = VA_ARG16( valist, WORD ); /* retval */
373 buf = VA_ARG16( valist, SEGPTR );
374 lpbuf = (LPCATCHBUF)PTR_SEG_TO_LIN( buf );
375 VA_END16( valist );
377 /* Find the frame32 corresponding to the frame16 we are jumping to */
378 pFrame = THREAD_STACK16( thdb );
379 frame32 = THREAD_STACK16( thdb )->frame32;
380 while (frame32 && frame32->frame16)
382 if (OFFSETOF(frame32->frame16) < OFFSETOF(thdb->cur_stack))
383 break; /* Something strange is going on */
384 if (OFFSETOF(frame32->frame16) > lpbuf[2])
386 /* We found the right frame */
387 pFrame->frame32 = frame32;
388 break;
390 frame32 = ((STACK16FRAME *)PTR_SEG_TO_LIN(frame32->frame16))->frame32;
393 IP_reg(context) = lpbuf[0];
394 CS_reg(context) = lpbuf[1];
395 SP_reg(context) = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
396 BP_reg(context) = lpbuf[3];
397 SI_reg(context) = lpbuf[4];
398 DI_reg(context) = lpbuf[5];
399 DS_reg(context) = lpbuf[6];
401 if (lpbuf[8] != SS_reg(context))
402 ERR(relay, "Switching stack segment with Throw() not supported; expect crash now\n" );
404 if (TRACE_ON(relay)) /* Make sure we have a valid entry point address */
406 static FARPROC16 entryPoint = NULL;
408 if (!entryPoint) /* Get entry point for Throw() */
409 entryPoint = NE_GetEntryPoint( GetModuleHandle16("KERNEL"), 56 );
410 pFrame->entry_cs = SELECTOROF(entryPoint);
411 pFrame->entry_ip = OFFSETOF(entryPoint);
416 /**********************************************************************
417 * RELAY_CallProc32W
419 * Helper for CallProc[Ex]32W
421 static DWORD RELAY_CallProc32W(int Ex)
423 DWORD nrofargs, argconvmask;
424 FARPROC32 proc32;
425 DWORD *args, ret;
426 VA_LIST16 valist;
427 int i;
428 int aix;
429 dbg_decl_str(relay, 1024);
431 VA_START16( valist );
432 nrofargs = VA_ARG16( valist, DWORD );
433 argconvmask = VA_ARG16( valist, DWORD );
434 proc32 = VA_ARG16( valist, FARPROC32 );
435 dsprintf(relay, "CallProc32W(%ld,%ld,%p, Ex%d args[",nrofargs,argconvmask,proc32,Ex);
436 args = (DWORD*)HEAP_xalloc( GetProcessHeap(), 0,
437 sizeof(DWORD)*nrofargs );
438 /* CallProcEx doesn't need its args reversed */
439 for (i=0;i<nrofargs;i++) {
440 if (Ex) {
441 aix = i;
442 } else {
443 aix = nrofargs - i - 1;
445 if (argconvmask & (1<<i))
447 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
448 args[aix] = (DWORD)PTR_SEG_TO_LIN(ptr);
449 dsprintf(relay,"%08lx(%p),",ptr,PTR_SEG_TO_LIN(ptr));
451 else
453 args[aix] = VA_ARG16( valist, DWORD );
454 dsprintf(relay,"%ld,",args[aix]);
457 dsprintf(relay,"])");
458 VA_END16( valist );
460 switch (nrofargs) {
461 case 0: ret = proc32();
462 break;
463 case 1: ret = proc32(args[0]);
464 break;
465 case 2: ret = proc32(args[0],args[1]);
466 break;
467 case 3: ret = proc32(args[0],args[1],args[2]);
468 break;
469 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
470 break;
471 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
472 break;
473 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
474 break;
475 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
476 break;
477 case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
478 break;
479 case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
480 break;
481 case 10: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
482 break;
483 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]);
484 break;
485 default:
486 /* FIXME: should go up to 32 arguments */
487 ERR(relay,"Unsupported number of arguments %ld, please report.\n",nrofargs);
488 ret = 0;
489 break;
491 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
492 if (!Ex) STACK16_POP( THREAD_Current(),
493 (3 + nrofargs) * sizeof(DWORD) );
495 TRACE(relay,"%s - returns %08lx\n",dbg_str(relay),ret);
496 HeapFree( GetProcessHeap(), 0, args );
497 return ret;
501 /**********************************************************************
502 * CallProc32W (KERNEL.517)
504 DWORD WINAPI WIN16_CallProc32W()
506 return RELAY_CallProc32W(0);
510 /**********************************************************************
511 * CallProcEx32W() (KERNEL.518)
513 * C - style linkage to CallProc32W - caller pops stack.
515 DWORD WINAPI WIN16_CallProcEx32W()
517 return RELAY_CallProc32W(TRUE);