Release 980628
[wine/gsoc-2012-control.git] / if1632 / relay.c
blobcc904e373c1261d7ab18aa8a182f5a1f0c06395e
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 TASK_KillCurrentTask(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\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) );
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 * Catch (KERNEL.55)
297 * Real prototype is:
298 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
300 void WINAPI Catch( CONTEXT *context )
302 VA_LIST16 valist;
303 SEGPTR buf;
304 LPCATCHBUF lpbuf;
306 VA_START16( valist );
307 buf = VA_ARG16( valist, SEGPTR );
308 lpbuf = (LPCATCHBUF)PTR_SEG_TO_LIN( buf );
309 VA_END16( valist );
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()... */
315 /* Windows uses:
316 * lpbuf[0] = ip
317 * lpbuf[1] = cs
318 * lpbuf[2] = sp
319 * lpbuf[3] = bp
320 * lpbuf[4] = si
321 * lpbuf[5] = di
322 * lpbuf[6] = ds
323 * lpbuf[7] = unused
324 * lpbuf[8] = ss
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);
335 lpbuf[7] = 0;
336 lpbuf[8] = SS_reg(context);
337 AX_reg(context) = 0; /* Return 0 */
341 /**********************************************************************
342 * Throw (KERNEL.56)
344 * Real prototype is:
345 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
347 void WINAPI Throw( CONTEXT *context )
349 VA_LIST16 valist;
350 SEGPTR buf;
351 LPCATCHBUF lpbuf;
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 );
360 VA_END16( valist );
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;
373 break;
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 /**********************************************************************
402 * RELAY_CallProc32W
404 * Helper for CallProc[Ex]32W
406 static DWORD RELAY_CallProc32W(int Ex)
408 DWORD nrofargs, argconvmask;
409 FARPROC32 proc32;
410 DWORD *args, ret;
411 VA_LIST16 valist;
412 int i;
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));
429 else
431 args[nrofargs-i-1] = VA_ARG16( valist, DWORD );
432 dsprintf(relay,"%ld,",args[nrofargs-i-1]);
435 dsprintf(relay,"])");
436 VA_END16( valist );
438 switch (nrofargs) {
439 case 0: ret = proc32();
440 break;
441 case 1: ret = proc32(args[0]);
442 break;
443 case 2: ret = proc32(args[0],args[1]);
444 break;
445 case 3: ret = proc32(args[0],args[1],args[2]);
446 break;
447 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
448 break;
449 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
450 break;
451 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
452 break;
453 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
454 break;
455 case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
456 break;
457 case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
458 break;
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]);
460 break;
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]);
462 break;
463 default:
464 /* FIXME: should go up to 32 arguments */
465 ERR(relay,"Unsupported number of arguments %ld, please report.\n",nrofargs);
466 ret = 0;
467 break;
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 );
475 return ret;
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);