Fixed a couple of compiler warnings.
[wine.git] / if1632 / relay.c
blobc07ebb961d8c6e0008903a52dab3c02930734d11
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 "builtin16.h"
16 #include "task.h"
17 #include "syslevel.h"
18 #include "debugstr.h"
19 #include "debugtools.h"
20 #include "main.h"
22 DEFAULT_DEBUG_CHANNEL(relay)
25 /***********************************************************************
26 * RELAY_Init
28 BOOL RELAY_Init(void)
30 WORD codesel;
32 /* Allocate the code selector for CallTo16 routines */
34 extern void Call16_Ret_Start(), Call16_Ret_End();
35 extern void CallTo16_Ret();
36 extern void CALL32_CBClient_Ret();
37 extern void CALL32_CBClientEx_Ret();
38 extern DWORD CallTo16_RetAddr;
39 extern DWORD CALL32_CBClient_RetAddr;
40 extern DWORD CALL32_CBClientEx_RetAddr;
42 codesel = GLOBAL_CreateBlock( GMEM_FIXED, (void *)Call16_Ret_Start,
43 (int)Call16_Ret_End - (int)Call16_Ret_Start,
44 GetModuleHandle16( "KERNEL" ),
45 TRUE, TRUE, FALSE, NULL );
46 if (!codesel) return FALSE;
48 /* Patch the return addresses for CallTo16 routines */
50 CallTo16_RetAddr =
51 MAKELONG( (int)CallTo16_Ret -(int)Call16_Ret_Start, codesel );
52 CALL32_CBClient_RetAddr =
53 MAKELONG( (int)CALL32_CBClient_Ret -(int)Call16_Ret_Start, codesel );
54 CALL32_CBClientEx_RetAddr =
55 MAKELONG( (int)CALL32_CBClientEx_Ret -(int)Call16_Ret_Start, codesel );
57 /* Create built-in modules */
58 if (!BUILTIN_Init()) return FALSE;
60 /* Initialize thunking */
61 return THUNK_Init();
65 /* from relay32/relay386.c */
66 extern char **debug_relay_excludelist,**debug_relay_includelist;
68 /***********************************************************************
69 * RELAY_DebugCallFrom16
71 void RELAY_DebugCallFrom16( CONTEXT86 *context )
73 STACK16FRAME *frame;
74 WORD ordinal;
75 char *args16, funstr[80];
76 const char *args;
77 int i, usecdecl, reg_func;
79 if (!TRACE_ON(relay)) return;
81 frame = CURRENT_STACK16;
82 args = BUILTIN_GetEntryPoint16( frame, funstr, &ordinal );
83 if (!args) return; /* happens for the two snoop register relays */
84 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
85 DPRINTF( "Call %s(",funstr);
86 VA_START16( args16 );
88 usecdecl = ( *args == 'c' );
89 args += 2;
90 reg_func = ( memcmp( args, "regs_", 5 ) == 0
91 || memcmp( args, "intr_", 5 ) == 0 );
92 args += 5;
94 if (usecdecl)
96 while (*args)
98 switch(*args)
100 case 'w':
101 case 's':
102 DPRINTF( "0x%04x", *(WORD *)args16 );
103 args16 += 2;
104 break;
105 case 'l':
106 DPRINTF( "0x%08x", *(int *)args16 );
107 args16 += 4;
108 break;
109 case 't':
110 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
111 if (HIWORD(*(SEGPTR *)args16))
112 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
113 args16 += 4;
114 break;
115 case 'p':
116 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
117 args16 += 4;
118 break;
119 case 'T':
120 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
121 if (HIWORD( *(SEGPTR *)args16 ))
122 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
123 args16 += 4;
124 break;
126 args++;
127 if (*args) DPRINTF( "," );
130 else /* not cdecl */
132 /* Start with the last arg */
133 for (i = 0; args[i]; i++)
135 switch(args[i])
137 case 'w':
138 case 's':
139 args16 += 2;
140 break;
141 case 'l':
142 case 'p':
143 case 't':
144 case 'T':
145 args16 += 4;
146 break;
150 while (*args)
152 switch(*args)
154 case 'w':
155 case 's':
156 args16 -= 2;
157 DPRINTF( "0x%04x", *(WORD *)args16 );
158 break;
159 case 'l':
160 args16 -= 4;
161 DPRINTF( "0x%08x", *(int *)args16 );
162 break;
163 case 't':
164 args16 -= 4;
165 DPRINTF( "0x%08x", *(int *)args16 );
166 if (HIWORD(*(SEGPTR *)args16))
167 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
168 break;
169 case 'p':
170 args16 -= 4;
171 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
172 break;
173 case 'T':
174 args16 -= 4;
175 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
176 if (HIWORD( *(SEGPTR *)args16 ))
177 debug_dumpstr( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 ));
178 break;
180 args++;
181 if (*args) DPRINTF( "," );
185 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
186 VA_END16( args16 );
188 if (reg_func)
189 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
190 AX_reg(context), BX_reg(context), CX_reg(context),
191 DX_reg(context), SI_reg(context), DI_reg(context),
192 (WORD)ES_reg(context), EFL_reg(context) );
194 SYSLEVEL_CheckNotLevel( 2 );
198 /***********************************************************************
199 * RELAY_DebugCallFrom16Ret
201 void RELAY_DebugCallFrom16Ret( CONTEXT86 *context, int ret_val )
203 STACK16FRAME *frame;
204 WORD ordinal;
205 char funstr[80];
206 const char *args;
208 if (!TRACE_ON(relay)) return;
209 frame = CURRENT_STACK16;
210 args = BUILTIN_GetEntryPoint16( frame, funstr, &ordinal );
211 if (!args) return;
212 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
213 DPRINTF( "Ret %s() ",funstr);
215 if ( memcmp( args+2, "long_", 5 ) == 0 )
217 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
218 ret_val, frame->cs, frame->ip, frame->ds );
220 else if ( memcmp( args+2, "word_", 5 ) == 0 )
222 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
223 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
225 else if ( memcmp( args+2, "regs_", 5 ) == 0
226 || memcmp( args+2, "intr_", 5 ) == 0 )
228 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
229 (WORD)CS_reg(context), LOWORD(EIP_reg(context)), (WORD)DS_reg(context));
230 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
231 AX_reg(context), BX_reg(context), CX_reg(context),
232 DX_reg(context), SI_reg(context), DI_reg(context),
233 (WORD)ES_reg(context), EFL_reg(context) );
236 SYSLEVEL_CheckNotLevel( 2 );
240 /***********************************************************************
241 * RELAY_Unimplemented16
243 * This function is called for unimplemented 16-bit entry points (declared
244 * as 'stub' in the spec file).
246 void RELAY_Unimplemented16(void)
248 WORD ordinal;
249 char name[80];
250 STACK16FRAME *frame = CURRENT_STACK16;
251 BUILTIN_GetEntryPoint16( frame, name, &ordinal );
252 MESSAGE("No handler for Win16 routine %s (called from %04x:%04x)\n",
253 name, frame->cs, frame->ip );
254 ExitProcess(1);
258 /***********************************************************************
259 * RELAY_DebugCallTo16
261 * 'target' contains either the function to call (normal CallTo16)
262 * or a pointer to the CONTEXT86 struct (register CallTo16).
263 * 'nb_args' is the number of argument bytes on the 16-bit stack;
264 * 'reg_func' specifies whether we have a register CallTo16 or not.
266 void RELAY_DebugCallTo16( LPVOID target, int nb_args, BOOL reg_func )
268 WORD *stack16;
269 TEB *teb;
271 if (!TRACE_ON(relay)) return;
272 teb = NtCurrentTeb();
273 stack16 = (WORD *)THREAD_STACK16(teb);
275 nb_args /= sizeof(WORD);
277 if ( reg_func )
279 CONTEXT86 *context = (CONTEXT86 *)target;
281 DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
282 CS_reg(context), LOWORD(EIP_reg(context)), DS_reg(context) );
283 while (nb_args--) DPRINTF( ",0x%04x", *--stack16 );
284 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
285 OFFSETOF(teb->cur_stack) );
286 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
287 AX_reg(context), BX_reg(context), CX_reg(context),
288 DX_reg(context), SI_reg(context), DI_reg(context),
289 BP_reg(context), (WORD)ES_reg(context), (WORD)FS_reg(context) );
291 else
293 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
294 HIWORD(target), LOWORD(target), SELECTOROF(teb->cur_stack) );
295 while (nb_args--) DPRINTF( ",0x%04x", *--stack16 );
296 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
297 OFFSETOF(teb->cur_stack) );
300 SYSLEVEL_CheckNotLevel( 2 );
304 /***********************************************************************
305 * RELAY_DebugCallTo16Ret
307 void RELAY_DebugCallTo16Ret( int ret_val )
309 if (!TRACE_ON(relay)) return;
311 DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n",
312 SELECTOROF(NtCurrentTeb()->cur_stack),
313 OFFSETOF(NtCurrentTeb()->cur_stack), ret_val);
314 SYSLEVEL_CheckNotLevel( 2 );
318 /**********************************************************************
319 * Catch (KERNEL.55)
321 * Real prototype is:
322 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
324 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT86 *context )
326 /* Note: we don't save the current ss, as the catch buffer is */
327 /* only 9 words long. Hopefully no one will have the silly */
328 /* idea to change the current stack before calling Throw()... */
330 /* Windows uses:
331 * lpbuf[0] = ip
332 * lpbuf[1] = cs
333 * lpbuf[2] = sp
334 * lpbuf[3] = bp
335 * lpbuf[4] = si
336 * lpbuf[5] = di
337 * lpbuf[6] = ds
338 * lpbuf[7] = unused
339 * lpbuf[8] = ss
342 lpbuf[0] = LOWORD(EIP_reg(context));
343 lpbuf[1] = CS_reg(context);
344 /* Windows pushes 4 more words before saving sp */
345 lpbuf[2] = LOWORD(ESP_reg(context)) - 4 * sizeof(WORD);
346 lpbuf[3] = LOWORD(EBP_reg(context));
347 lpbuf[4] = LOWORD(ESI_reg(context));
348 lpbuf[5] = LOWORD(EDI_reg(context));
349 lpbuf[6] = DS_reg(context);
350 lpbuf[7] = 0;
351 lpbuf[8] = SS_reg(context);
352 AX_reg(context) = 0; /* Return 0 */
356 /**********************************************************************
357 * Throw (KERNEL.56)
359 * Real prototype is:
360 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
362 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT86 *context )
364 STACK16FRAME *pFrame;
365 STACK32FRAME *frame32;
366 TEB *teb = NtCurrentTeb();
368 AX_reg(context) = retval;
370 /* Find the frame32 corresponding to the frame16 we are jumping to */
371 pFrame = THREAD_STACK16(teb);
372 frame32 = pFrame->frame32;
373 while (frame32 && frame32->frame16)
375 if (OFFSETOF(frame32->frame16) < OFFSETOF(teb->cur_stack))
376 break; /* Something strange is going on */
377 if (OFFSETOF(frame32->frame16) > lpbuf[2])
379 /* We found the right frame */
380 pFrame->frame32 = frame32;
381 break;
383 frame32 = ((STACK16FRAME *)PTR_SEG_TO_LIN(frame32->frame16))->frame32;
386 EIP_reg(context) = lpbuf[0];
387 CS_reg(context) = lpbuf[1];
388 ESP_reg(context) = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
389 EBP_reg(context) = lpbuf[3];
390 ESI_reg(context) = lpbuf[4];
391 EDI_reg(context) = lpbuf[5];
392 DS_reg(context) = lpbuf[6];
394 if (lpbuf[8] != SS_reg(context))
395 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );