Release 980104
[wine/multimedia.git] / if1632 / relay.c
blob8e247ff02062c86ddd6e401dbd7fb75c0cee816a
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
4 */
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "windows.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 "stddebug.h"
17 /* #define DEBUG_RELAY */
18 #include "debug.h"
20 #if 0
21 /* Make make_debug think these were really used */
22 dprintf_relay
23 #endif
26 /***********************************************************************
27 * RELAY_Init
29 BOOL32 RELAY_Init(void)
31 WORD codesel;
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,
50 codesel );
51 CALLTO16_RetAddr_long=MAKELONG( (int)CALLTO16_Ret_long-(int)CALLTO16_Start,
52 codesel );
53 CALLTO16_RetAddr_regs=MAKELONG( (int)CALLTO16_Ret_regs-(int)CALLTO16_Start,
54 codesel );
56 /* Create built-in modules */
57 if (!BUILTIN_Init()) return FALSE;
59 /* Initialize thunking */
60 return THUNK_Init();
64 /***********************************************************************
65 * RELAY_DebugCallFrom16
67 void RELAY_DebugCallFrom16( int func_type, char *args,
68 void *entry_point, CONTEXT *context )
70 STACK16FRAME *frame;
71 WORD ordinal;
72 char *args16;
73 int i;
75 if (!debugging_relay) return;
77 frame = CURRENT_STACK16;
78 printf( "Call %s(", BUILTIN_GetEntryPoint16( frame->entry_cs,
79 frame->entry_ip,
80 &ordinal ));
81 VA_START16( args16 );
82 for (i = 0; i < strlen(args); i++)
84 switch(args[i])
86 case 'w':
87 case 's':
88 args16 += 2;
89 break;
90 case 'l':
91 case 'p':
92 case 't':
93 case 'T':
94 args16 += 4;
95 break;
99 while (*args)
101 switch(*args)
103 case 'w':
104 case 's':
105 args16 -= 2;
106 printf( "0x%04x", *(WORD *)args16 );
107 break;
108 case 'l':
109 args16 -= 4;
110 printf( "0x%08x", *(int *)args16 );
111 break;
112 case 't':
113 args16 -= 4;
114 printf( "0x%08x", *(int *)args16 );
115 if (HIWORD(*(int *)args16))
116 printf( " \"%s\"", (char *)PTR_SEG_TO_LIN(*(int *)args16) );
117 break;
118 case 'p':
119 args16 -= 4;
120 printf( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
121 break;
122 case 'T':
123 args16 -= 4;
124 printf( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
125 if (HIWORD(*(int *)args16))
126 printf( " \"%s\"", (char *)PTR_SEG_TO_LIN(*(int *)args16) );
127 break;
129 args++;
130 if (*args) printf( "," );
132 printf( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
133 VA_END16( args16 );
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)
148 STACK16FRAME *frame;
149 WORD ordinal;
151 if (!debugging_relay) return;
152 frame = CURRENT_STACK16;
153 printf( "Ret %s() ", BUILTIN_GetEntryPoint16( frame->entry_cs,
154 frame->entry_ip,
155 &ordinal ));
156 switch(func_type)
158 case 0: /* long */
159 printf( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
160 ret_val, frame->cs, frame->ip, frame->ds );
161 break;
162 case 1: /* word */
163 printf( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
164 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
165 break;
166 case 2: /* regs */
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) );
173 break;
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)
186 WORD ordinal;
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.
199 * Stack layout:
200 * ... ...
201 * (stack+8) arg2
202 * (stack+4) arg1
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) );
217 printf( ")\n" );
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) );
223 else
225 printf( "CallTo16(func=%04x:%04x,ds=%04x",
226 HIWORD(stack[0]), LOWORD(stack[0]),
227 SELECTOROF(IF1632_Saved16_ss_sp) );
228 stack++;
229 while (nb_args--) printf( ",0x%04x", *stack++ );
230 printf( ")\n" );
235 /**********************************************************************
236 * Catch (KERNEL.55)
238 * Real prototype is:
239 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
241 void WINAPI Catch( CONTEXT *context )
243 VA_LIST16 valist;
244 SEGPTR buf;
245 LPCATCHBUF lpbuf;
246 STACK16FRAME *pFrame = CURRENT_STACK16;
248 VA_START16( valist );
249 buf = VA_ARG16( valist, SEGPTR );
250 lpbuf = (LPCATCHBUF)PTR_SEG_TO_LIN( buf );
251 VA_END16( valist );
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()... */
257 /* Windows uses:
258 * lpbuf[0] = ip
259 * lpbuf[1] = cs
260 * lpbuf[2] = sp
261 * lpbuf[3] = bp
262 * lpbuf[4] = si
263 * lpbuf[5] = di
264 * lpbuf[6] = ds
265 * lpbuf[7] = unused
266 * lpbuf[8] = ss
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 /**********************************************************************
283 * Throw (KERNEL.56)
285 * Real prototype is:
286 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
288 void WINAPI Throw( CONTEXT *context )
290 VA_LIST16 valist;
291 SEGPTR buf;
292 LPCATCHBUF lpbuf;
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 );
299 VA_END16( valist );
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"),
318 56 );
319 pFrame->entry_cs = SELECTOROF(entryPoint);
320 pFrame->entry_ip = OFFSETOF(entryPoint);
325 /**********************************************************************
326 * RELAY_CallProc32W
328 * Helper for CallProc[Ex]32W
330 static DWORD RELAY_CallProc32W(int Ex)
332 DWORD nrofargs, argconvmask;
333 FARPROC32 proc32;
334 DWORD *args, ret;
335 VA_LIST16 valist;
336 int i;
338 VA_START16( valist );
339 nrofargs = VA_ARG16( valist, DWORD );
340 argconvmask = VA_ARG16( valist, DWORD );
341 proc32 = VA_ARG16( valist, FARPROC32 );
342 fprintf(stderr,"CallProc32W(%ld,%ld,%p, Ex%d args[",nrofargs,argconvmask,proc32,Ex);
343 args = (DWORD*)HEAP_xalloc( GetProcessHeap(), 0,
344 sizeof(DWORD)*nrofargs );
345 for (i=0;i<nrofargs;i++) {
346 if (argconvmask & (1<<i))
348 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
349 args[nrofargs-i-1] = (DWORD)PTR_SEG_TO_LIN(ptr);
350 fprintf(stderr,"%08lx(%p),",ptr,PTR_SEG_TO_LIN(ptr));
352 else
354 args[nrofargs-i-1] = VA_ARG16( valist, DWORD );
355 fprintf(stderr,"%ld,",args[nrofargs-i-1]);
358 fprintf(stderr,"]) - ");
359 VA_END16( valist );
361 switch (nrofargs) {
362 case 0: ret = proc32();
363 break;
364 case 1: ret = proc32(args[0]);
365 break;
366 case 2: ret = proc32(args[0],args[1]);
367 break;
368 case 3: ret = proc32(args[0],args[1],args[2]);
369 break;
370 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
371 break;
372 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
373 break;
374 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
375 break;
376 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
377 break;
378 default:
379 /* FIXME: should go up to 32 arguments */
380 fprintf(stderr,"CallProc32W: unsupported number of arguments %ld, please report.\n",nrofargs);
381 ret = 0;
382 break;
384 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
385 if (!Ex) STACK16_POP( (3 + nrofargs) * sizeof(DWORD) );
387 fprintf(stderr,"returns %08lx\n",ret);
388 HeapFree( GetProcessHeap(), 0, args );
389 return ret;
393 /**********************************************************************
394 * CallProc32W (KERNEL.517)
396 DWORD WINAPI WIN16_CallProc32W()
398 return RELAY_CallProc32W(0);
402 /**********************************************************************
403 * CallProcEx32W() (KERNEL.518)
405 * C - style linkage to CallProc32W - caller pops stack.
407 DWORD WINAPI WIN16_CallProcEx32W()
409 return RELAY_CallProc32W(TRUE);