Correct test for linux 2.2 joystick API, which returned true in every
[wine/wine64.git] / if1632 / relay.c
blobede3cd82417d026427bd4dddc88fed6747f1f9f8
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 "debugtools.h"
19 #include "main.h"
21 DEFAULT_DEBUG_CHANNEL(relay);
23 /***********************************************************************
24 * RELAY_Init
26 BOOL RELAY_Init(void)
28 #ifdef __i386__
29 WORD codesel;
31 /* Allocate the code selector for CallTo16 routines */
33 extern void Call16_Ret_Start(), Call16_Ret_End();
34 extern void CallTo16_Ret();
35 extern void CALL32_CBClient_Ret();
36 extern void CALL32_CBClientEx_Ret();
37 extern DWORD CallTo16_RetAddr;
38 extern DWORD CALL32_CBClient_RetAddr;
39 extern DWORD CALL32_CBClientEx_RetAddr;
41 codesel = GLOBAL_CreateBlock( GMEM_FIXED, (void *)Call16_Ret_Start,
42 (int)Call16_Ret_End - (int)Call16_Ret_Start,
43 GetModuleHandle16( "KERNEL" ),
44 TRUE, TRUE, FALSE, NULL );
45 if (!codesel) return FALSE;
47 /* Patch the return addresses for CallTo16 routines */
49 CallTo16_RetAddr =
50 MAKELONG( (int)CallTo16_Ret -(int)Call16_Ret_Start, codesel );
51 CALL32_CBClient_RetAddr =
52 MAKELONG( (int)CALL32_CBClient_Ret -(int)Call16_Ret_Start, codesel );
53 CALL32_CBClientEx_RetAddr =
54 MAKELONG( (int)CALL32_CBClientEx_Ret -(int)Call16_Ret_Start, codesel );
55 #endif
57 /* Create built-in modules */
58 if (!BUILTIN_Init()) return FALSE;
60 /* Initialize thunking */
61 return THUNK_Init();
65 * Stubs for the CallTo16/CallFrom16 routines on non-Intel architectures
66 * (these will never be called but need to be present to satisfy the linker ...)
68 #ifndef __i386__
69 WORD CALLBACK CallTo16Word( FARPROC16 target, INT nArgs )
70 { assert( FALSE ); }
72 LONG CALLBACK CallTo16Long( FARPROC16 target, INT nArgs )
73 { assert( FALSE ); }
75 LONG CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, INT nArgs )
76 { assert( FALSE ); }
78 LONG CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, INT nArgs )
79 { assert( FALSE ); }
81 WORD CallFrom16Word( void )
82 { assert( FALSE ); }
84 LONG CallFrom16Long( void )
85 { assert( FALSE ); }
87 void CallFrom16Register( void )
88 { assert( FALSE ); }
90 void CallFrom16Thunk( void )
91 { assert( FALSE ); }
93 DWORD WINAPI CALL32_CBClient( FARPROC proc, LPWORD args, DWORD *esi )
94 { assert( FALSE ); }
96 DWORD WINAPI CALL32_CBClientEx( FARPROC proc, LPWORD args, DWORD *esi, INT *nArgs )
97 { assert( FALSE ); }
98 #endif
101 /* from relay32/relay386.c */
102 extern char **debug_relay_excludelist,**debug_relay_includelist;
104 /***********************************************************************
105 * RELAY_DebugCallFrom16
107 void RELAY_DebugCallFrom16( CONTEXT86 *context )
109 STACK16FRAME *frame;
110 WORD ordinal;
111 char *args16, funstr[80];
112 const char *args;
113 int i, usecdecl, reg_func;
115 if (!TRACE_ON(relay)) return;
117 frame = CURRENT_STACK16;
118 args = BUILTIN_GetEntryPoint16( frame, funstr, &ordinal );
119 if (!args) return; /* happens for the two snoop register relays */
120 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
121 DPRINTF( "Call %s(",funstr);
122 VA_START16( args16 );
124 usecdecl = ( *args == 'c' );
125 args += 2;
126 reg_func = ( memcmp( args, "regs_", 5 ) == 0
127 || memcmp( args, "intr_", 5 ) == 0 );
128 args += 5;
130 if (usecdecl)
132 while (*args)
134 switch(*args)
136 case 'w':
137 case 's':
138 DPRINTF( "0x%04x", *(WORD *)args16 );
139 args16 += 2;
140 break;
141 case 'l':
142 DPRINTF( "0x%08x", *(int *)args16 );
143 args16 += 4;
144 break;
145 case 'p':
146 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
147 args16 += 4;
148 break;
149 case 't':
150 case 'T':
151 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
152 debugres_a( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 )) );
153 args16 += 4;
154 break;
156 args++;
157 if (*args) DPRINTF( "," );
160 else /* not cdecl */
162 /* Start with the last arg */
163 for (i = 0; args[i]; i++)
165 switch(args[i])
167 case 'w':
168 case 's':
169 args16 += 2;
170 break;
171 case 'l':
172 case 'p':
173 case 't':
174 case 'T':
175 args16 += 4;
176 break;
180 while (*args)
182 switch(*args)
184 case 'w':
185 case 's':
186 args16 -= 2;
187 DPRINTF( "0x%04x", *(WORD *)args16 );
188 break;
189 case 'l':
190 args16 -= 4;
191 DPRINTF( "0x%08x", *(int *)args16 );
192 break;
193 case 't':
194 args16 -= 4;
195 DPRINTF( "0x%08x %s", *(int *)args16,
196 debugres_a( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 )));
197 break;
198 case 'p':
199 args16 -= 4;
200 DPRINTF( "%04x:%04x", *(WORD *)(args16+2), *(WORD *)args16 );
201 break;
202 case 'T':
203 args16 -= 4;
204 DPRINTF( "%04x:%04x %s", *(WORD *)(args16+2), *(WORD *)args16,
205 debugres_a( (LPSTR)PTR_SEG_TO_LIN(*(SEGPTR *)args16 )));
206 break;
208 args++;
209 if (*args) DPRINTF( "," );
213 DPRINTF( ") ret=%04x:%04x ds=%04x\n", frame->cs, frame->ip, frame->ds );
214 VA_END16( args16 );
216 if (reg_func)
217 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
218 AX_reg(context), BX_reg(context), CX_reg(context),
219 DX_reg(context), SI_reg(context), DI_reg(context),
220 (WORD)ES_reg(context), EFL_reg(context) );
222 SYSLEVEL_CheckNotLevel( 2 );
226 /***********************************************************************
227 * RELAY_DebugCallFrom16Ret
229 void RELAY_DebugCallFrom16Ret( CONTEXT86 *context, int ret_val )
231 STACK16FRAME *frame;
232 WORD ordinal;
233 char funstr[80];
234 const char *args;
236 if (!TRACE_ON(relay)) return;
237 frame = CURRENT_STACK16;
238 args = BUILTIN_GetEntryPoint16( frame, funstr, &ordinal );
239 if (!args) return;
240 if (!RELAY_ShowDebugmsgRelay(funstr)) return;
241 DPRINTF( "Ret %s() ",funstr);
243 if ( memcmp( args+2, "long_", 5 ) == 0 )
245 DPRINTF( "retval=0x%08x ret=%04x:%04x ds=%04x\n",
246 ret_val, frame->cs, frame->ip, frame->ds );
248 else if ( memcmp( args+2, "word_", 5 ) == 0 )
250 DPRINTF( "retval=0x%04x ret=%04x:%04x ds=%04x\n",
251 ret_val & 0xffff, frame->cs, frame->ip, frame->ds );
253 else if ( memcmp( args+2, "regs_", 5 ) == 0
254 || memcmp( args+2, "intr_", 5 ) == 0 )
256 DPRINTF("retval=none ret=%04x:%04x ds=%04x\n",
257 (WORD)CS_reg(context), LOWORD(EIP_reg(context)), (WORD)DS_reg(context));
258 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x ES=%04x EFL=%08lx\n",
259 AX_reg(context), BX_reg(context), CX_reg(context),
260 DX_reg(context), SI_reg(context), DI_reg(context),
261 (WORD)ES_reg(context), EFL_reg(context) );
264 SYSLEVEL_CheckNotLevel( 2 );
268 /***********************************************************************
269 * RELAY_Unimplemented16
271 * This function is called for unimplemented 16-bit entry points (declared
272 * as 'stub' in the spec file).
274 void RELAY_Unimplemented16(void)
276 WORD ordinal;
277 char name[80];
278 STACK16FRAME *frame = CURRENT_STACK16;
279 BUILTIN_GetEntryPoint16( frame, name, &ordinal );
280 MESSAGE("No handler for Win16 routine %s (called from %04x:%04x)\n",
281 name, frame->cs, frame->ip );
282 ExitProcess(1);
286 /***********************************************************************
287 * RELAY_DebugCallTo16
289 * 'target' contains either the function to call (normal CallTo16)
290 * or a pointer to the CONTEXT86 struct (register CallTo16).
291 * 'nb_args' is the number of argument bytes on the 16-bit stack;
292 * 'reg_func' specifies whether we have a register CallTo16 or not.
294 void RELAY_DebugCallTo16( LPVOID target, int nb_args, BOOL reg_func )
296 WORD *stack16;
297 TEB *teb;
299 if (!TRACE_ON(relay)) return;
300 teb = NtCurrentTeb();
301 stack16 = (WORD *)THREAD_STACK16(teb);
303 nb_args /= sizeof(WORD);
305 if ( reg_func )
307 CONTEXT86 *context = (CONTEXT86 *)target;
309 DPRINTF("CallTo16(func=%04lx:%04x,ds=%04lx",
310 CS_reg(context), LOWORD(EIP_reg(context)), DS_reg(context) );
311 while (nb_args--) DPRINTF( ",0x%04x", *--stack16 );
312 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
313 OFFSETOF(teb->cur_stack) );
314 DPRINTF(" AX=%04x BX=%04x CX=%04x DX=%04x SI=%04x DI=%04x BP=%04x ES=%04x FS=%04x\n",
315 AX_reg(context), BX_reg(context), CX_reg(context),
316 DX_reg(context), SI_reg(context), DI_reg(context),
317 BP_reg(context), (WORD)ES_reg(context), (WORD)FS_reg(context) );
319 else
321 DPRINTF("CallTo16(func=%04x:%04x,ds=%04x",
322 HIWORD(target), LOWORD(target), SELECTOROF(teb->cur_stack) );
323 while (nb_args--) DPRINTF( ",0x%04x", *--stack16 );
324 DPRINTF(") ss:sp=%04x:%04x\n", SELECTOROF(teb->cur_stack),
325 OFFSETOF(teb->cur_stack) );
328 SYSLEVEL_CheckNotLevel( 2 );
332 /***********************************************************************
333 * RELAY_DebugCallTo16Ret
335 void RELAY_DebugCallTo16Ret( int ret_val )
337 if (!TRACE_ON(relay)) return;
339 DPRINTF("CallTo16() ss:sp=%04x:%04x retval=0x%08x\n",
340 SELECTOROF(NtCurrentTeb()->cur_stack),
341 OFFSETOF(NtCurrentTeb()->cur_stack), ret_val);
342 SYSLEVEL_CheckNotLevel( 2 );
346 /**********************************************************************
347 * Catch (KERNEL.55)
349 * Real prototype is:
350 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
352 void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT86 *context )
354 /* Note: we don't save the current ss, as the catch buffer is */
355 /* only 9 words long. Hopefully no one will have the silly */
356 /* idea to change the current stack before calling Throw()... */
358 /* Windows uses:
359 * lpbuf[0] = ip
360 * lpbuf[1] = cs
361 * lpbuf[2] = sp
362 * lpbuf[3] = bp
363 * lpbuf[4] = si
364 * lpbuf[5] = di
365 * lpbuf[6] = ds
366 * lpbuf[7] = unused
367 * lpbuf[8] = ss
370 lpbuf[0] = LOWORD(EIP_reg(context));
371 lpbuf[1] = CS_reg(context);
372 /* Windows pushes 4 more words before saving sp */
373 lpbuf[2] = LOWORD(ESP_reg(context)) - 4 * sizeof(WORD);
374 lpbuf[3] = LOWORD(EBP_reg(context));
375 lpbuf[4] = LOWORD(ESI_reg(context));
376 lpbuf[5] = LOWORD(EDI_reg(context));
377 lpbuf[6] = DS_reg(context);
378 lpbuf[7] = 0;
379 lpbuf[8] = SS_reg(context);
380 AX_reg(context) = 0; /* Return 0 */
384 /**********************************************************************
385 * Throw (KERNEL.56)
387 * Real prototype is:
388 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
390 void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT86 *context )
392 STACK16FRAME *pFrame;
393 STACK32FRAME *frame32;
394 TEB *teb = NtCurrentTeb();
396 AX_reg(context) = retval;
398 /* Find the frame32 corresponding to the frame16 we are jumping to */
399 pFrame = THREAD_STACK16(teb);
400 frame32 = pFrame->frame32;
401 while (frame32 && frame32->frame16)
403 if (OFFSETOF(frame32->frame16) < OFFSETOF(teb->cur_stack))
404 break; /* Something strange is going on */
405 if (OFFSETOF(frame32->frame16) > lpbuf[2])
407 /* We found the right frame */
408 pFrame->frame32 = frame32;
409 break;
411 frame32 = ((STACK16FRAME *)PTR_SEG_TO_LIN(frame32->frame16))->frame32;
414 EIP_reg(context) = lpbuf[0];
415 CS_reg(context) = lpbuf[1];
416 ESP_reg(context) = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
417 EBP_reg(context) = lpbuf[3];
418 ESI_reg(context) = lpbuf[4];
419 EDI_reg(context) = lpbuf[5];
420 DS_reg(context) = lpbuf[6];
422 if (lpbuf[8] != SS_reg(context))
423 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );