Release 961222
[wine/gsoc-2012-control.git] / if1632 / relay.c
blob63b35091902b4b537a0c42eba838b4c43c142314
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
4 */
6 #include <stdio.h>
7 #include "windows.h"
8 #include "global.h"
9 #include "module.h"
10 #include "registers.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, SIGCONTEXT *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 ES_reg(context), EFL_reg(context) );
115 /***********************************************************************
116 * RELAY_DebugCallFrom16Ret
118 void RELAY_DebugCallFrom16Ret( int func_type, int ret_val, SIGCONTEXT *context)
120 STACK16FRAME *frame;
121 WORD ordinal;
123 if (*(DWORD *)PTR_SEG_TO_LIN(IF1632_Stack32_base) != 0xDEADBEEF)
125 fprintf(stderr, "Wine wrote past the end of the 32 bit stack. Please report this.\n");
126 exit(1); /* There's probably no point in going on */
128 if (!debugging_relay) return;
130 frame = CURRENT_STACK16;
131 printf( "Ret %s() ", BUILTIN_GetEntryPoint16( frame->entry_cs,
132 frame->entry_ip,
133 &ordinal ));
134 switch(func_type)
136 case 0: /* long */
137 printf( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
138 ret_val, frame->cs, frame->ip, frame->ds );
139 break;
140 case 1: /* word */
141 printf( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
142 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
143 break;
144 case 2: /* regs */
145 printf( "retval=none ret=%04x:%04x ds=%04x\n",
146 frame->cs, frame->ip, frame->ds );
147 printf( " AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
148 AX_reg(context), BX_reg(context), CX_reg(context),
149 DX_reg(context), SI_reg(context), DI_reg(context),
150 ES_reg(context), EFL_reg(context) );
151 break;
156 /***********************************************************************
157 * RELAY_Unimplemented16
159 * This function is called for unimplemented 16-bit entry points (declared
160 * as 'stub' in the spec file).
162 void RELAY_Unimplemented16(void)
164 WORD ordinal;
165 STACK16FRAME *frame = CURRENT_STACK16;
166 fprintf(stderr,"No handler for Win16 routine %s (called from %04x:%04x)\n",
167 BUILTIN_GetEntryPoint16(frame->entry_cs,frame->entry_ip,&ordinal),
168 frame->cs, frame->ip );
169 TASK_KillCurrentTask(1);
173 /***********************************************************************
174 * RELAY_Unimplemented32
176 * This function is called for unimplemented 32-bit entry points (declared
177 * as 'stub' in the spec file).
178 * (The args are the same than for RELAY_DebugCallFrom32).
180 void RELAY_Unimplemented32( int nb_args, void *relay_addr,
181 void *entry_point, int ebp, int ret_addr )
183 fprintf( stderr, "No handler for Win32 routine %s (called from %08x)\n",
184 BUILTIN_GetEntryPoint32( relay_addr ), ret_addr );
185 TASK_KillCurrentTask(1);
189 /***********************************************************************
190 * RELAY_DebugCallTo16
192 * 'stack' points to the called function address on the 32-bit stack.
193 * Stack layout:
194 * ... ...
195 * (stack+12) arg2
196 * (stack+8) arg1
197 * (stack+4) 16-bit ds
198 * (stack) func to call
200 void RELAY_DebugCallTo16( int* stack, int nbargs )
202 if (!debugging_relay) return;
204 printf( "CallTo16(func=%04x:%04x,ds=%04x",
205 HIWORD(stack[0]), LOWORD(stack[0]), LOWORD(stack[1]) );
206 stack += 2;
207 while (nbargs--) printf( ",0x%04x", *stack++ );
208 printf( ")\n" );
212 /***********************************************************************
213 * RELAY_DebugCallFrom32
215 * 'stack' points to the saved ebp on the stack.
216 * Stack layout:
217 * ... ...
218 * (stack+12) arg2
219 * (stack+8) arg1
220 * (stack+4) ret addr
221 * (stack) ebp
222 * (stack-4) entry point
223 * (stack-8) relay addr
225 void RELAY_DebugCallFrom32( int *stack, int nb_args )
227 int *parg;
229 if (!debugging_relay) return;
230 printf( "Call %s(", BUILTIN_GetEntryPoint32( (void *)stack[-2] ));
231 if (nb_args > 0)
233 for (parg = &stack[2]; nb_args; parg++, nb_args--)
235 printf( "%08x", *parg );
236 if (nb_args > 1) printf( "," );
239 printf( ") ret=%08x\n", stack[1] );
240 if (nb_args == -1) /* Register function */
242 CONTEXT *context = (CONTEXT *)((BYTE *)stack - sizeof(CONTEXT) - 12);
243 printf( " EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx ESI=%08lx EDI=%08lx\n",
244 context->Eax, context->Ebx, context->Ecx, context->Edx,
245 context->Esi, context->Edi );
246 printf( " EBP=%08lx ESP=%08lx DS=%04lx ES=%04lx FS=%04lx GS=%04lx EFL=%08lx\n",
247 context->Ebp, context->Esp, context->SegDs, context->SegEs,
248 context->SegFs, context->SegGs, context->EFlags );
253 /***********************************************************************
254 * RELAY_DebugCallFrom32Ret
256 * 'stack' points to the saved ebp on the stack.
257 * Stack layout:
258 * ... ...
259 * (stack+12) arg2
260 * (stack+8) arg1
261 * (stack+4) ret addr
262 * (stack) ebp
263 * (stack-4) entry point
264 * (stack-8) relay addr
266 void RELAY_DebugCallFrom32Ret( int *stack, int nb_args, int ret_val )
268 if (!debugging_relay) return;
269 printf( "Ret %s() retval=%08x ret=%08x\n",
270 BUILTIN_GetEntryPoint32( (void *)stack[-2] ), ret_val, stack[1] );
271 if (nb_args == -1) /* Register function */
273 CONTEXT *context = (CONTEXT *)((BYTE *)stack - sizeof(CONTEXT) - 12);
274 printf( " EAX=%08lx EBX=%08lx ECX=%08lx EDX=%08lx ESI=%08lx EDI=%08lx\n",
275 context->Eax, context->Ebx, context->Ecx, context->Edx,
276 context->Esi, context->Edi );
277 printf( " EBP=%08lx ESP=%08lx DS=%04lx ES=%04lx FS=%04lx GS=%04lx EFL=%08lx\n",
278 context->Ebp, context->Esp, context->SegDs, context->SegEs,
279 context->SegFs, context->SegGs, context->EFlags );
284 /***********************************************************************
285 * RELAY_DebugCallTo32
287 void RELAY_DebugCallTo32( unsigned int func, int nbargs, unsigned int arg1 )
289 unsigned int *argptr;
291 if (!debugging_relay) return;
293 printf( "CallTo32(func=%08x", func );
294 for (argptr = &arg1; nbargs; nbargs--, argptr++)
295 printf( ",%08x", *argptr );
296 printf( ")\n" );
300 /**********************************************************************
301 * Catch (KERNEL.55)
303 INT16 Catch( LPCATCHBUF lpbuf )
305 STACK16FRAME *pFrame = CURRENT_STACK16;
307 /* Note: we don't save the current ss, as the catch buffer is */
308 /* only 9 words long. Hopefully no one will have the silly */
309 /* idea to change the current stack before calling Throw()... */
311 /* Windows uses:
312 * lpbuf[0] = ip
313 * lpbuf[1] = cs
314 * lpbuf[2] = sp
315 * lpbuf[3] = bp
316 * lpbuf[4] = si
317 * lpbuf[5] = di
318 * lpbuf[6] = ds
319 * lpbuf[7] = unused
320 * lpbuf[8] = ss
322 /* FIXME: we need to save %si and %di */
324 lpbuf[0] = IF1632_Saved16_sp;
325 lpbuf[1] = LOWORD(IF1632_Saved32_esp);
326 lpbuf[2] = HIWORD(IF1632_Saved32_esp);
327 lpbuf[3] = pFrame->saved_ss;
328 lpbuf[4] = pFrame->saved_sp;
329 lpbuf[5] = pFrame->ds;
330 lpbuf[6] = pFrame->bp;
331 lpbuf[7] = pFrame->ip;
332 lpbuf[8] = pFrame->cs;
333 return 0;
337 /**********************************************************************
338 * Throw (KERNEL.56)
340 INT16 Throw( LPCATCHBUF lpbuf, INT16 retval )
342 STACK16FRAME *pFrame;
343 WORD es = CURRENT_STACK16->es;
345 IF1632_Saved16_sp = lpbuf[0] - sizeof(WORD);
346 IF1632_Saved32_esp = MAKELONG( lpbuf[1], lpbuf[2] );
347 pFrame = CURRENT_STACK16;
348 pFrame->saved_ss = lpbuf[3];
349 pFrame->saved_sp = lpbuf[4];
350 pFrame->ds = lpbuf[5];
351 pFrame->bp = lpbuf[6];
352 pFrame->ip = lpbuf[7];
353 pFrame->cs = lpbuf[8];
354 pFrame->es = es;
355 if (debugging_relay) /* Make sure we have a valid entry point address */
357 static FARPROC16 entryPoint = NULL;
359 if (!entryPoint) /* Get entry point for Throw() */
360 entryPoint = MODULE_GetEntryPoint( GetModuleHandle("KERNEL"), 56 );
361 pFrame->entry_cs = SELECTOROF(entryPoint);
362 pFrame->entry_ip = OFFSETOF(entryPoint);
364 return retval;