Release 970202
[wine/multimedia.git] / if1632 / relay.c
blob75b4eb8de8bccb5b872c28abc87ae9e79a29c480
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
4 */
6 #include <stdio.h>
7 #include "windows.h"
8 #include "winnt.h"
9 #include "global.h"
10 #include "module.h"
11 #include "stackframe.h"
12 #include "task.h"
13 #include "stddebug.h"
14 /* #define DEBUG_RELAY */
15 #include "debug.h"
17 #if 0
18 /* Make make_debug think these were really used */
19 dprintf_relay
20 #endif
23 /***********************************************************************
24 * RELAY_Init
26 BOOL32 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 DWORD CALLTO16_RetAddr_word, CALLTO16_RetAddr_long;
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 return TRUE;
51 /***********************************************************************
52 * RELAY_DebugCallFrom16
54 void RELAY_DebugCallFrom16( int func_type, char *args,
55 void *entry_point, CONTEXT *context )
57 STACK16FRAME *frame;
58 WORD ordinal;
59 char *args16;
60 int i;
62 if (!debugging_relay) return;
64 frame = CURRENT_STACK16;
65 printf( "Call %s(", BUILTIN_GetEntryPoint16( frame->entry_cs,
66 frame->entry_ip,
67 &ordinal ));
68 args16 = (char *)frame->args;
69 for (i = 0; i < strlen(args); i++)
71 switch(args[i])
73 case 'w':
74 case 's':
75 args16 += 2;
76 break;
77 case 'l':
78 case 'p':
79 args16 += 4;
80 break;
84 while (*args)
86 switch(*args)
88 case 'w':
89 case 's':
90 args16 -= 2;
91 printf( "0x%04x", *(WORD *)args16 );
92 break;
93 case 'l':
94 args16 -= 4;
95 printf( "0x%08x", *(int *)args16 );
96 break;
97 case 'p':
98 args16 -= 4;
99 printf( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
100 break;
102 args++;
103 if (*args) printf( "," );
105 printf( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
107 if (func_type == 2) /* register function */
108 printf( " AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
109 AX_reg(context), BX_reg(context), CX_reg(context),
110 DX_reg(context), SI_reg(context), DI_reg(context),
111 (WORD)ES_reg(context), EFL_reg(context) );
115 /***********************************************************************
116 * RELAY_DebugCallFrom16Ret
118 void RELAY_DebugCallFrom16Ret( int func_type, int ret_val, CONTEXT *context)
120 STACK16FRAME *frame;
121 WORD ordinal;
123 if (!debugging_relay) return;
124 frame = CURRENT_STACK16;
125 printf( "Ret %s() ", BUILTIN_GetEntryPoint16( frame->entry_cs,
126 frame->entry_ip,
127 &ordinal ));
128 switch(func_type)
130 case 0: /* long */
131 printf( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
132 ret_val, frame->cs, frame->ip, frame->ds );
133 break;
134 case 1: /* word */
135 printf( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
136 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
137 break;
138 case 2: /* regs */
139 printf( "retval=none ret=%04x:%04x ds=%04x\n",
140 frame->cs, frame->ip, frame->ds );
141 printf( " AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
142 AX_reg(context), BX_reg(context), CX_reg(context),
143 DX_reg(context), SI_reg(context), DI_reg(context),
144 (WORD)ES_reg(context), EFL_reg(context) );
145 break;
150 /***********************************************************************
151 * RELAY_Unimplemented16
153 * This function is called for unimplemented 16-bit entry points (declared
154 * as 'stub' in the spec file).
156 void RELAY_Unimplemented16(void)
158 WORD ordinal;
159 STACK16FRAME *frame = CURRENT_STACK16;
160 fprintf(stderr,"No handler for Win16 routine %s (called from %04x:%04x)\n",
161 BUILTIN_GetEntryPoint16(frame->entry_cs,frame->entry_ip,&ordinal),
162 frame->cs, frame->ip );
163 TASK_KillCurrentTask(1);
167 /***********************************************************************
168 * RELAY_Unimplemented32
170 * This function is called for unimplemented 32-bit entry points (declared
171 * as 'stub' in the spec file).
172 * (The args are the same than for RELAY_DebugCallFrom32).
174 void RELAY_Unimplemented32( int nb_args, void *relay_addr,
175 void *entry_point, int ebp, int ret_addr )
177 fprintf( stderr, "No handler for Win32 routine %s (called from %08x)\n",
178 BUILTIN_GetEntryPoint32( relay_addr ), ret_addr );
179 TASK_KillCurrentTask(1);
183 /***********************************************************************
184 * RELAY_DebugCallTo16
186 * 'stack' points to the called function address on the 32-bit stack.
187 * Stack layout:
188 * ... ...
189 * (stack+8) arg2
190 * (stack+4) arg1
191 * (stack) func to call
193 void RELAY_DebugCallTo16( int* stack, int nbargs )
195 if (!debugging_relay) return;
197 printf( "CallTo16(func=%04x:%04x,ds=%04x",
198 HIWORD(stack[0]), LOWORD(stack[0]), CURRENT_DS );
199 stack++;
200 while (nbargs--) printf( ",0x%04x", *stack++ );
201 printf( ")\n" );
205 /***********************************************************************
206 * RELAY_DebugCallFrom32
208 * 'stack' points to the saved ebp on the stack.
209 * Stack layout:
210 * ... ...
211 * (stack+12) arg2
212 * (stack+8) arg1
213 * (stack+4) ret addr
214 * (stack) ebp
215 * (stack-4) entry point
216 * (stack-8) relay addr
218 void RELAY_DebugCallFrom32( int *stack, int nb_args )
220 int *parg, i;
222 if (!debugging_relay) return;
223 printf( "Call %s(", BUILTIN_GetEntryPoint32( (void *)stack[-2] ));
224 for (i = nb_args & 0x7fffffff, parg = &stack[2]; i; parg++, i--)
226 printf( "%08x", *parg );
227 if (i > 1) printf( "," );
229 printf( ") ret=%08x\n", stack[1] );
230 if (nb_args & 0x80000000) /* Register function */
232 CONTEXT *context = (CONTEXT *)((BYTE *)stack - sizeof(CONTEXT) - 8);
233 printf( " EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx ESI=%08lx EDI=%08lx\n",
234 context->Eax, context->Ebx, context->Ecx, context->Edx,
235 context->Esi, context->Edi );
236 printf( " EBP=%08lx ESP=%08lx EIP=%08lx DS=%04lx ES=%04lx FS=%04lx GS=%04lx EFL=%08lx\n",
237 context->Ebp, context->Esp, context->Eip, context->SegDs,
238 context->SegEs, context->SegFs, context->SegGs,
239 context->EFlags );
244 /***********************************************************************
245 * RELAY_DebugCallFrom32Ret
247 * 'stack' points to the saved ebp on the stack.
248 * Stack layout:
249 * ... ...
250 * (stack+12) arg2
251 * (stack+8) arg1
252 * (stack+4) ret addr
253 * (stack) ebp
254 * (stack-4) entry point
255 * (stack-8) relay addr
257 void RELAY_DebugCallFrom32Ret( int *stack, int nb_args, int ret_val )
259 if (!debugging_relay) return;
260 printf( "Ret %s() retval=%08x ret=%08x\n",
261 BUILTIN_GetEntryPoint32( (void *)stack[-2] ), ret_val, stack[1] );
262 if (nb_args & 0x80000000) /* Register function */
264 CONTEXT *context = (CONTEXT *)((BYTE *)stack - sizeof(CONTEXT) - 8);
265 printf( " EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx ESI=%08lx EDI=%08lx\n",
266 context->Eax, context->Ebx, context->Ecx, context->Edx,
267 context->Esi, context->Edi );
268 printf( " EBP=%08lx ESP=%08lx EIP=%08lx DS=%04lx ES=%04lx FS=%04lx GS=%04lx EFL=%08lx\n",
269 context->Ebp, context->Esp, context->Eip, context->SegDs,
270 context->SegEs, context->SegFs, context->SegGs,
271 context->EFlags );
276 /***********************************************************************
277 * RELAY_DebugCallTo32
279 void RELAY_DebugCallTo32( unsigned int func, int nbargs, unsigned int arg1 )
281 unsigned int *argptr;
283 if (!debugging_relay) return;
285 printf( "CallTo32(func=%08x", func );
286 for (argptr = &arg1; nbargs; nbargs--, argptr++)
287 printf( ",%08x", *argptr );
288 printf( ")\n" );
292 /**********************************************************************
293 * Catch (KERNEL.55)
295 INT16 Catch( LPCATCHBUF lpbuf )
297 STACK16FRAME *pFrame = CURRENT_STACK16;
299 /* Note: we don't save the current ss, as the catch buffer is */
300 /* only 9 words long. Hopefully no one will have the silly */
301 /* idea to change the current stack before calling Throw()... */
303 /* Windows uses:
304 * lpbuf[0] = ip
305 * lpbuf[1] = cs
306 * lpbuf[2] = sp
307 * lpbuf[3] = bp
308 * lpbuf[4] = si
309 * lpbuf[5] = di
310 * lpbuf[6] = ds
311 * lpbuf[7] = unused
312 * lpbuf[8] = ss
314 /* FIXME: we need to save %si and %di */
316 lpbuf[0] = IF1632_Saved16_sp;
317 lpbuf[1] = LOWORD(IF1632_Saved32_esp);
318 lpbuf[2] = HIWORD(IF1632_Saved32_esp);
319 lpbuf[3] = pFrame->saved_ss;
320 lpbuf[4] = pFrame->saved_sp;
321 lpbuf[5] = pFrame->ds;
322 lpbuf[6] = pFrame->bp;
323 lpbuf[7] = pFrame->ip;
324 lpbuf[8] = pFrame->cs;
325 return 0;
329 /**********************************************************************
330 * Throw (KERNEL.56)
332 INT16 Throw( LPCATCHBUF lpbuf, INT16 retval )
334 STACK16FRAME *pFrame;
335 WORD es = CURRENT_STACK16->es;
337 IF1632_Saved16_sp = lpbuf[0] - sizeof(WORD);
338 IF1632_Saved32_esp = MAKELONG( lpbuf[1], lpbuf[2] );
339 pFrame = CURRENT_STACK16;
340 pFrame->saved_ss = lpbuf[3];
341 pFrame->saved_sp = lpbuf[4];
342 pFrame->ds = lpbuf[5];
343 pFrame->bp = lpbuf[6];
344 pFrame->ip = lpbuf[7];
345 pFrame->cs = lpbuf[8];
346 pFrame->es = es;
347 if (debugging_relay) /* Make sure we have a valid entry point address */
349 static FARPROC16 entryPoint = NULL;
351 if (!entryPoint) /* Get entry point for Throw() */
352 entryPoint = MODULE_GetEntryPoint( GetModuleHandle("KERNEL"), 56 );
353 pFrame->entry_cs = SELECTOROF(entryPoint);
354 pFrame->entry_ip = OFFSETOF(entryPoint);
356 return retval;