Rearrange winver detection code and cache the winver value we
[wine.git] / if1632 / relay.c
blobe21bbd350b908e5da8fab530baff21bee29f7a10
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
4 */
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include "wine/winbase16.h"
10 #include "winnt.h"
11 #include "global.h"
12 #include "heap.h"
13 #include "module.h"
14 #include "stackframe.h"
15 #include "task.h"
16 #include "debugstr.h"
17 #include "debug.h"
18 #include "main.h"
20 DEFAULT_DEBUG_CHANNEL(relay)
23 /***********************************************************************
24 * RELAY_Init
26 BOOL RELAY_Init(void)
28 WORD codesel;
30 /* Allocate the code selector for CallTo16 routines */
32 extern void CALLTO16_Start(), CALLTO16_End();
33 extern void CALLTO16_Ret_word(), CALLTO16_Ret_long();
34 extern void CALLTO16_Ret_eax();
35 extern void CALL32_CBClient_Ret();
36 extern void CALL32_CBClientEx_Ret();
37 extern DWORD CALLTO16_RetAddr_word;
38 extern DWORD CALLTO16_RetAddr_long;
39 extern DWORD CALLTO16_RetAddr_eax;
40 extern DWORD CALL32_CBClient_RetAddr;
41 extern DWORD CALL32_CBClientEx_RetAddr;
43 codesel = GLOBAL_CreateBlock( GMEM_FIXED, (void *)CALLTO16_Start,
44 (int)CALLTO16_End - (int)CALLTO16_Start,
45 0, TRUE, TRUE, FALSE, NULL );
46 if (!codesel) return FALSE;
48 /* Patch the return addresses for CallTo16 routines */
50 CALLTO16_RetAddr_word=MAKELONG( (int)CALLTO16_Ret_word-(int)CALLTO16_Start,
51 codesel );
52 CALLTO16_RetAddr_long=MAKELONG( (int)CALLTO16_Ret_long-(int)CALLTO16_Start,
53 codesel );
54 CALLTO16_RetAddr_eax =MAKELONG( (int)CALLTO16_Ret_eax -(int)CALLTO16_Start,
55 codesel );
56 CALL32_CBClient_RetAddr =
57 MAKELONG( (int)CALL32_CBClient_Ret -(int)CALLTO16_Start, codesel );
58 CALL32_CBClientEx_RetAddr =
59 MAKELONG( (int)CALL32_CBClientEx_Ret -(int)CALLTO16_Start, codesel );
61 /* Create built-in modules */
62 if (!BUILTIN_Init()) return FALSE;
64 /* Initialize thunking */
65 return THUNK_Init();
69 /* from relay32/relay386.c */
70 extern char **debug_relay_excludelist,**debug_relay_includelist;
72 /***********************************************************************
73 * RELAY_DebugCallFrom16
75 void RELAY_DebugCallFrom16( int func_type, char *args,
76 void *entry_point, CONTEXT *context )
78 STACK16FRAME *frame;
79 WORD ordinal;
80 char *args16;
81 const char *funstr;
82 int i;
84 if (!TRACE_ON(relay)) return;
86 frame = CURRENT_STACK16;
87 funstr = BUILTIN_GetEntryPoint16(frame->entry_cs,frame->entry_ip,&ordinal);
88 if (!funstr) return; /* happens for the two snoop register relays */
89 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
90 DPRINTF( "Call %s(",funstr);
91 VA_START16( args16 );
93 if (func_type & 4) /* cdecl */
95 while (*args)
97 switch(*args)
99 case 'w':
100 case 's':
101 DPRINTF( "0x%04x", *(WORD *)args16 );
102 args16 += 2;
103 break;
104 case 'l':
105 DPRINTF( "0x%08x", *(int *)args16 );
106 args16 += 4;
107 break;
108 case 't':
109 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
110 if (HIWORD(*(SEGPTR *)args16))
111 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
112 args16 += 4;
113 break;
114 case 'p':
115 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
116 args16 += 4;
117 break;
118 case 'T':
119 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
120 if (HIWORD( *(SEGPTR *)args16 ))
121 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
122 args16 += 4;
123 break;
125 args++;
126 if (*args) DPRINTF( "," );
129 else /* not cdecl */
131 /* Start with the last arg */
132 for (i = 0; args[i]; i++)
134 switch(args[i])
136 case 'w':
137 case 's':
138 args16 += 2;
139 break;
140 case 'l':
141 case 'p':
142 case 't':
143 case 'T':
144 args16 += 4;
145 break;
149 while (*args)
151 switch(*args)
153 case 'w':
154 case 's':
155 args16 -= 2;
156 DPRINTF( "0x%04x", *(WORD *)args16 );
157 break;
158 case 'l':
159 args16 -= 4;
160 DPRINTF( "0x%08x", *(int *)args16 );
161 break;
162 case 't':
163 args16 -= 4;
164 DPRINTF( "0x%08x", *(int *)args16 );
165 if (HIWORD(*(SEGPTR *)args16))
166 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
167 break;
168 case 'p':
169 args16 -= 4;
170 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
171 break;
172 case 'T':
173 args16 -= 4;
174 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
175 if (HIWORD( *(SEGPTR *)args16 ))
176 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
177 break;
179 args++;
180 if (*args) DPRINTF( "," );
184 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
185 VA_END16( args16 );
187 if (func_type & 2) /* register function */
188 DPRINTF( " AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
189 AX_reg(context), BX_reg(context), CX_reg(context),
190 DX_reg(context), SI_reg(context), DI_reg(context),
191 (WORD)ES_reg(context), EFL_reg(context) );
193 SYSLEVEL_CheckNotLevel( 2 );
197 /***********************************************************************
198 * RELAY_DebugCallFrom16Ret
200 void RELAY_DebugCallFrom16Ret( int func_type, int ret_val, CONTEXT *context)
202 STACK16FRAME *frame;
203 WORD ordinal;
204 const char *funstr;
206 if (!TRACE_ON(relay)) return;
207 frame = CURRENT_STACK16;
208 funstr = BUILTIN_GetEntryPoint16(frame->entry_cs,frame->entry_ip,&ordinal);
209 if (!funstr) return;
210 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
211 DPRINTF( "Ret %s() ",funstr);
212 switch(func_type)
214 case 0: /* long */
215 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
216 ret_val, frame->cs, frame->ip, frame->ds );
217 break;
218 case 1: /* word */
219 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
220 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
221 break;
222 case 2: /* regs */
223 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
224 (WORD)CS_reg(context), IP_reg(context), (WORD)DS_reg(context));
225 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
226 AX_reg(context), BX_reg(context), CX_reg(context),
227 DX_reg(context), SI_reg(context), DI_reg(context),
228 (WORD)ES_reg(context), EFL_reg(context) );
229 break;
232 SYSLEVEL_CheckNotLevel( 2 );
236 /***********************************************************************
237 * RELAY_Unimplemented16
239 * This function is called for unimplemented 16-bit entry points (declared
240 * as 'stub' in the spec file).
242 void RELAY_Unimplemented16(void)
244 WORD ordinal;
245 STACK16FRAME *frame = CURRENT_STACK16;
246 MSG("No handler for Win16 routine %s (called from %04x:%04x)\n",
247 BUILTIN_GetEntryPoint16(frame->entry_cs,frame->entry_ip,&ordinal),
248 frame->cs, frame->ip );
249 ExitProcess(1);
253 /***********************************************************************
254 * RELAY_DebugCallTo16
256 * 'stack' points to the called function address on the 32-bit stack.
257 * Stack layout:
258 * ... ...
259 * (stack+8) arg2
260 * (stack+4) arg1
261 * (stack) func to call
263 void RELAY_DebugCallTo16( int* stack, int nb_args )
265 THDB *thdb;
267 if (!TRACE_ON(relay)) return;
268 thdb = THREAD_Current();
270 if (nb_args == -1) /* Register function */
272 CONTEXT *context = (CONTEXT *)stack[0];
273 WORD *stack16 = (WORD *)THREAD_STACK16(thdb);
274 DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
275 CS_reg(context), IP_reg(context), DS_reg(context) );
276 nb_args = stack[1] / sizeof(WORD);
277 while (nb_args--) {
278 --stack16;
279 DPRINTF( ",0x%04x", *stack16 );
281 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(thdb->cur_stack),
282 OFFSETOF(thdb->cur_stack) );
283 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
284 AX_reg(context), BX_reg(context), CX_reg(context),
285 DX_reg(context), SI_reg(context), DI_reg(context),
286 BP_reg(context), (WORD)ES_reg(context), (WORD)FS_reg(context) );
288 else
290 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
291 HIWORD(stack[0]), LOWORD(stack[0]),
292 SELECTOROF(thdb->cur_stack) );
293 stack++;
294 while (nb_args--) {
295 DPRINTF(",0x%04x", *stack );
296 stack++;
298 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(thdb->cur_stack),
299 OFFSETOF(thdb->cur_stack) );
302 SYSLEVEL_CheckNotLevel( 2 );
306 /***********************************************************************
307 * RELAY_DebugCallTo16Ret
309 void RELAY_DebugCallTo16Ret( int ret_val )
311 THDB *thdb;
313 if (!TRACE_ON(relay)) return;
314 thdb = THREAD_Current();
316 DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n",
317 SELECTOROF(thdb->cur_stack), OFFSETOF(thdb->cur_stack), ret_val);
319 SYSLEVEL_CheckNotLevel( 2 );
323 /**********************************************************************
324 * Catch (KERNEL.55)
326 * Real prototype is:
327 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
329 void WINAPI Catch16( CONTEXT *context )
331 VA_LIST16 valist;
332 SEGPTR buf;
333 LPCATCHBUF lpbuf;
335 VA_START16( valist );
336 buf = VA_ARG16( valist, SEGPTR );
337 lpbuf = (LPCATCHBUF)PTR_SEG_TO_LIN( buf );
338 VA_END16( valist );
340 /* Note: we don't save the current ss, as the catch buffer is */
341 /* only 9 words long. Hopefully no one will have the silly */
342 /* idea to change the current stack before calling Throw()... */
344 /* Windows uses:
345 * lpbuf[0] = ip
346 * lpbuf[1] = cs
347 * lpbuf[2] = sp
348 * lpbuf[3] = bp
349 * lpbuf[4] = si
350 * lpbuf[5] = di
351 * lpbuf[6] = ds
352 * lpbuf[7] = unused
353 * lpbuf[8] = ss
356 lpbuf[0] = IP_reg(context);
357 lpbuf[1] = CS_reg(context);
358 /* Windows pushes 4 more words before saving sp */
359 lpbuf[2] = SP_reg(context) - 4 * sizeof(WORD);
360 lpbuf[3] = BP_reg(context);
361 lpbuf[4] = SI_reg(context);
362 lpbuf[5] = DI_reg(context);
363 lpbuf[6] = DS_reg(context);
364 lpbuf[7] = 0;
365 lpbuf[8] = SS_reg(context);
366 AX_reg(context) = 0; /* Return 0 */
370 /**********************************************************************
371 * Throw (KERNEL.56)
373 * Real prototype is:
374 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
376 void WINAPI Throw16( CONTEXT *context )
378 VA_LIST16 valist;
379 SEGPTR buf;
380 LPCATCHBUF lpbuf;
381 STACK16FRAME *pFrame;
382 STACK32FRAME *frame32;
383 THDB *thdb = THREAD_Current();
385 VA_START16( valist );
386 AX_reg(context) = VA_ARG16( valist, WORD ); /* retval */
387 buf = VA_ARG16( valist, SEGPTR );
388 lpbuf = (LPCATCHBUF)PTR_SEG_TO_LIN( buf );
389 VA_END16( valist );
391 /* Find the frame32 corresponding to the frame16 we are jumping to */
392 pFrame = THREAD_STACK16( thdb );
393 frame32 = THREAD_STACK16( thdb )->frame32;
394 while (frame32 && frame32->frame16)
396 if (OFFSETOF(frame32->frame16) < OFFSETOF(thdb->cur_stack))
397 break; /* Something strange is going on */
398 if (OFFSETOF(frame32->frame16) > lpbuf[2])
400 /* We found the right frame */
401 pFrame->frame32 = frame32;
402 break;
404 frame32 = ((STACK16FRAME *)PTR_SEG_TO_LIN(frame32->frame16))->frame32;
407 IP_reg(context) = lpbuf[0];
408 CS_reg(context) = lpbuf[1];
409 SP_reg(context) = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
410 BP_reg(context) = lpbuf[3];
411 SI_reg(context) = lpbuf[4];
412 DI_reg(context) = lpbuf[5];
413 DS_reg(context) = lpbuf[6];
415 if (lpbuf[8] != SS_reg(context))
416 ERR(relay, "Switching stack segment with Throw() not supported; expect crash now\n" );
418 if (TRACE_ON(relay)) /* Make sure we have a valid entry point address */
420 static FARPROC16 entryPoint = NULL;
422 if (!entryPoint) /* Get entry point for Throw() */
423 entryPoint = NE_GetEntryPoint( GetModuleHandle16("KERNEL"), 56 );
424 pFrame->entry_cs = SELECTOROF(entryPoint);
425 pFrame->entry_ip = OFFSETOF(entryPoint);
430 /**********************************************************************
431 * RELAY_CallProc32W
433 * Helper for CallProc[Ex]32W
435 static DWORD RELAY_CallProc32W(int Ex)
437 DWORD nrofargs, argconvmask;
438 FARPROC proc32;
439 DWORD *args, ret;
440 VA_LIST16 valist;
441 int i;
442 int aix;
443 dbg_decl_str(relay, 1024);
445 SYSLEVEL_ReleaseWin16Lock();
447 VA_START16( valist );
448 nrofargs = VA_ARG16( valist, DWORD );
449 argconvmask = VA_ARG16( valist, DWORD );
450 proc32 = VA_ARG16( valist, FARPROC );
451 dsprintf(relay, "CallProc32W(%ld,%ld,%p, Ex%d args[",nrofargs,argconvmask,proc32,Ex);
452 args = (DWORD*)HEAP_xalloc( GetProcessHeap(), 0,
453 sizeof(DWORD)*nrofargs );
454 /* CallProcEx doesn't need its args reversed */
455 for (i=0;i<nrofargs;i++) {
456 if (Ex) {
457 aix = i;
458 } else {
459 aix = nrofargs - i - 1;
461 if (argconvmask & (1<<i))
463 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
464 args[aix] = (DWORD)PTR_SEG_TO_LIN(ptr);
465 dsprintf(relay,"%08lx(%p),",ptr,PTR_SEG_TO_LIN(ptr));
467 else
469 args[aix] = VA_ARG16( valist, DWORD );
470 dsprintf(relay,"%ld,",args[aix]);
473 dsprintf(relay,"])");
474 VA_END16( valist );
476 if (!proc32) ret = 0;
477 else switch (nrofargs)
479 case 0: ret = proc32();
480 break;
481 case 1: ret = proc32(args[0]);
482 break;
483 case 2: ret = proc32(args[0],args[1]);
484 break;
485 case 3: ret = proc32(args[0],args[1],args[2]);
486 break;
487 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
488 break;
489 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
490 break;
491 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
492 break;
493 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
494 break;
495 case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
496 break;
497 case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
498 break;
499 case 10: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
500 break;
501 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]);
502 break;
503 default:
504 /* FIXME: should go up to 32 arguments */
505 ERR(relay,"Unsupported number of arguments %ld, please report.\n",nrofargs);
506 ret = 0;
507 break;
509 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
510 if (!Ex) STACK16_POP( THREAD_Current(),
511 (3 + nrofargs) * sizeof(DWORD) );
513 TRACE(relay,"%s - returns %08lx\n",dbg_str(relay),ret);
514 HeapFree( GetProcessHeap(), 0, args );
516 SYSLEVEL_RestoreWin16Lock();
518 return ret;
522 /**********************************************************************
523 * CallProc32W (KERNEL.517)
525 DWORD WINAPI CallProc32W_16()
527 return RELAY_CallProc32W(0);
531 /**********************************************************************
532 * CallProcEx32W() (KERNEL.518)
534 * C - style linkage to CallProc32W - caller pops stack.
536 DWORD WINAPI CallProcEx32W_16()
538 return RELAY_CallProc32W(TRUE);