TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / krnl386.exe16 / wowthunk.c
blobfa49a246ab80b1416820f273e83f2f7a4dbdcae5
1 /*
2 * Win32 WOW Generic Thunk API
4 * Copyright 1999 Ulrich Weigand
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <errno.h>
28 #include "wine/winbase16.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "wownt32.h"
33 #include "excpt.h"
34 #include "winternl.h"
35 #include "kernel16_private.h"
36 #include "wine/exception.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
40 WINE_DECLARE_DEBUG_CHANNEL(relay);
41 WINE_DECLARE_DEBUG_CHANNEL(snoop);
43 /* symbols exported from relay16.s */
44 extern DWORD WINAPI wine_call_to_16( FARPROC16 target, DWORD cbArgs, PEXCEPTION_HANDLER handler );
45 extern void WINAPI wine_call_to_16_regs( CONTEXT *context, DWORD cbArgs, PEXCEPTION_HANDLER handler );
46 extern void __wine_call_to_16_ret(void);
47 extern void CALL32_CBClient_Ret(void);
48 extern void CALL32_CBClientEx_Ret(void);
49 extern void DPMI_PendingEventCheck(void);
50 extern void DPMI_PendingEventCheck_Cleanup(void);
51 extern void DPMI_PendingEventCheck_Return(void);
52 extern BYTE __wine_call16_start[];
53 extern BYTE __wine_call16_end[];
55 static SEGPTR call16_ret_addr; /* segptr to __wine_call_to_16_ret routine */
57 static WORD dpmi_checker_selector;
58 static DWORD dpmi_checker_offset_call;
59 static DWORD dpmi_checker_offset_cleanup;
60 static DWORD dpmi_checker_offset_return;
62 /***********************************************************************
63 * WOWTHUNK_Init
65 BOOL WOWTHUNK_Init(void)
67 /* allocate the code selector for CallTo16 routines */
68 LDT_ENTRY entry;
69 WORD codesel = wine_ldt_alloc_entries(1);
71 if (!codesel) return FALSE;
72 wine_ldt_set_base( &entry, __wine_call16_start );
73 wine_ldt_set_limit( &entry, (BYTE *)(&CallTo16_TebSelector + 1) - __wine_call16_start - 1 );
74 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
75 wine_ldt_set_entry( codesel, &entry );
77 /* Patch the return addresses for CallTo16 routines */
79 CallTo16_DataSelector = wine_get_ds();
80 call16_ret_addr = MAKESEGPTR( codesel, (BYTE *)__wine_call_to_16_ret - __wine_call16_start );
81 CALL32_CBClient_RetAddr =
82 MAKESEGPTR( codesel, (BYTE *)CALL32_CBClient_Ret - __wine_call16_start );
83 CALL32_CBClientEx_RetAddr =
84 MAKESEGPTR( codesel, (BYTE *)CALL32_CBClientEx_Ret - __wine_call16_start );
86 /* Prepare selector and offsets for DPMI event checking. */
87 dpmi_checker_selector = codesel;
88 dpmi_checker_offset_call = (BYTE *)DPMI_PendingEventCheck - __wine_call16_start;
89 dpmi_checker_offset_cleanup = (BYTE *)DPMI_PendingEventCheck_Cleanup - __wine_call16_start;
90 dpmi_checker_offset_return = (BYTE *)DPMI_PendingEventCheck_Return - __wine_call16_start;
92 if (TRACE_ON(relay) || TRACE_ON(snoop)) RELAY16_InitDebugLists();
94 return TRUE;
98 /*************************************************************
99 * fix_selector
101 * Fix a selector load that caused an exception if it's in the
102 * 16-bit relay code.
104 static BOOL fix_selector( CONTEXT *context )
106 WORD *stack;
107 BYTE *instr = (BYTE *)context->Eip;
109 if (instr < __wine_call16_start || instr >= __wine_call16_end) return FALSE;
111 /* skip prefixes */
112 while (*instr == 0x66 || *instr == 0x67) instr++;
114 switch(instr[0])
116 case 0x07: /* pop es */
117 case 0x17: /* pop ss */
118 case 0x1f: /* pop ds */
119 break;
120 case 0x0f: /* extended instruction */
121 switch(instr[1])
123 case 0xa1: /* pop fs */
124 case 0xa9: /* pop gs */
125 break;
126 default:
127 return FALSE;
129 break;
130 default:
131 return FALSE;
133 stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
134 TRACE( "fixing up selector %x for pop instruction\n", *stack );
135 *stack = 0;
136 return TRUE;
140 /*************************************************************
141 * insert_event_check
143 * Make resuming the context check for pending DPMI events
144 * before the original context is restored. This is required
145 * because DPMI events are asynchronous, they are blocked while
146 * Wine 32-bit code is being executed and we want to prevent
147 * a race when returning back to 16-bit or 32-bit DPMI context.
149 static void insert_event_check( CONTEXT *context )
151 char *stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
153 /* don't do event check while in system code */
154 if (wine_ldt_is_system(context->SegCs))
155 return;
157 if(context->SegCs == dpmi_checker_selector &&
158 context->Eip >= dpmi_checker_offset_call &&
159 context->Eip <= dpmi_checker_offset_cleanup)
162 * Nested call. Stack will be preserved.
165 else if(context->SegCs == dpmi_checker_selector &&
166 context->Eip == dpmi_checker_offset_return)
169 * Nested call. We have just finished popping the fs
170 * register, lets put it back into stack.
173 stack -= sizeof(WORD);
174 *(WORD*)stack = context->SegFs;
176 context->Esp -= 2;
178 else
181 * Call is not nested.
182 * Push modified registers into stack.
183 * These will be popped by the assembler stub.
186 stack -= sizeof(DWORD);
187 *(DWORD*)stack = context->EFlags;
189 stack -= sizeof(DWORD);
190 *(DWORD*)stack = context->SegCs;
192 stack -= sizeof(DWORD);
193 *(DWORD*)stack = context->Eip;
195 stack -= sizeof(WORD);
196 *(WORD*)stack = context->SegFs;
198 context->Esp -= 14;
202 * Modify the context so that we jump into assembler stub.
203 * TEB access is made easier by providing the stub
204 * with the correct fs register value.
207 context->SegCs = dpmi_checker_selector;
208 context->Eip = dpmi_checker_offset_call;
209 context->SegFs = wine_get_fs();
213 /*************************************************************
214 * call16_handler
216 * Handler for exceptions occurring in 16-bit code.
218 static DWORD call16_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
219 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
221 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
223 /* unwinding: restore the stack pointer in the TEB, and leave the Win16 mutex */
224 STACK32FRAME *frame32 = CONTAINING_RECORD(frame, STACK32FRAME, frame);
225 NtCurrentTeb()->WOW32Reserved = (void *)frame32->frame16;
226 _LeaveWin16Lock();
228 else if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
229 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
231 if (wine_ldt_is_system(context->SegCs))
233 if (fix_selector( context )) return ExceptionContinueExecution;
235 else
237 SEGPTR gpHandler;
238 DWORD ret = __wine_emulate_instruction( record, context );
241 * Insert check for pending DPMI events. Note that this
242 * check must be inserted after instructions have been
243 * emulated because the instruction emulation requires
244 * original CS:IP and the emulation may change TEB.dpmi_vif.
246 if(get_vm86_teb_info()->dpmi_vif)
247 insert_event_check( context );
249 if (ret != ExceptionContinueSearch) return ret;
251 /* check for Win16 __GP handler */
252 if ((gpHandler = HasGPHandler16( MAKESEGPTR( context->SegCs, context->Eip ) )))
254 WORD *stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
255 *--stack = context->SegCs;
256 *--stack = context->Eip;
258 if (!IS_SELECTOR_32BIT(context->SegSs))
259 context->Esp = MAKELONG( LOWORD(context->Esp - 2*sizeof(WORD)),
260 HIWORD(context->Esp) );
261 else
262 context->Esp -= 2*sizeof(WORD);
264 context->SegCs = SELECTOROF( gpHandler );
265 context->Eip = OFFSETOF( gpHandler );
266 return ExceptionContinueExecution;
270 else if (record->ExceptionCode == EXCEPTION_VM86_STI)
272 insert_event_check( context );
274 return ExceptionContinueSearch;
278 /*************************************************************
279 * vm86_handler
281 * Handler for exceptions occurring in vm86 code.
283 static DWORD vm86_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
284 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
286 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
287 return ExceptionContinueSearch;
289 if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
290 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
292 return __wine_emulate_instruction( record, context );
295 return ExceptionContinueSearch;
300 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
303 /**********************************************************************
304 * K32WOWGetDescriptor (KERNEL32.70)
306 BOOL WINAPI K32WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
308 return GetThreadSelectorEntry( GetCurrentThread(),
309 segptr >> 16, ldtent );
312 /**********************************************************************
313 * K32WOWGetVDMPointer (KERNEL32.56)
315 LPVOID WINAPI K32WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
317 /* FIXME: add size check too */
319 if ( fProtectedMode )
320 return MapSL( vp );
321 else
322 return DOSMEM_MapRealToLinear( vp );
325 /**********************************************************************
326 * K32WOWGetVDMPointerFix (KERNEL32.68)
328 LPVOID WINAPI K32WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
331 * Hmmm. According to the docu, we should call:
333 * GlobalFix16( SELECTOROF(vp) );
335 * But this is unnecessary under Wine, as we never move global
336 * memory segments in linear memory anyway.
338 * (I'm not so sure what we are *supposed* to do if
339 * fProtectedMode is TRUE, anyway ...)
342 return K32WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
345 /**********************************************************************
346 * K32WOWGetVDMPointerUnfix (KERNEL32.69)
348 VOID WINAPI K32WOWGetVDMPointerUnfix( DWORD vp )
351 * See above why we don't call:
353 * GlobalUnfix16( SELECTOROF(vp) );
358 /**********************************************************************
359 * K32WOWGlobalAlloc16 (KERNEL32.59)
361 WORD WINAPI K32WOWGlobalAlloc16( WORD wFlags, DWORD cb )
363 return (WORD)GlobalAlloc16( wFlags, cb );
366 /**********************************************************************
367 * K32WOWGlobalFree16 (KERNEL32.62)
369 WORD WINAPI K32WOWGlobalFree16( WORD hMem )
371 return (WORD)GlobalFree16( (HGLOBAL16)hMem );
374 /**********************************************************************
375 * K32WOWGlobalUnlock16 (KERNEL32.61)
377 BOOL WINAPI K32WOWGlobalUnlock16( WORD hMem )
379 return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
382 /**********************************************************************
383 * K32WOWGlobalAllocLock16 (KERNEL32.63)
385 DWORD WINAPI K32WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
387 WORD hMem = K32WOWGlobalAlloc16( wFlags, cb );
388 if (phMem) *phMem = hMem;
390 return K32WOWGlobalLock16( hMem );
393 /**********************************************************************
394 * K32WOWGlobalLockSize16 (KERNEL32.65)
396 DWORD WINAPI K32WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
398 if ( pcb )
399 *pcb = GlobalSize16( (HGLOBAL16)hMem );
401 return K32WOWGlobalLock16( hMem );
404 /**********************************************************************
405 * K32WOWGlobalUnlockFree16 (KERNEL32.64)
407 WORD WINAPI K32WOWGlobalUnlockFree16( DWORD vpMem )
409 if ( !K32WOWGlobalUnlock16( HIWORD(vpMem) ) )
410 return FALSE;
412 return K32WOWGlobalFree16( HIWORD(vpMem) );
416 /**********************************************************************
417 * K32WOWYield16 (KERNEL32.66)
419 VOID WINAPI K32WOWYield16( void )
422 * This does the right thing for both Win16 and Win32 tasks.
423 * More or less, at least :-/
425 Yield16();
428 /**********************************************************************
429 * K32WOWDirectedYield16 (KERNEL32.67)
431 VOID WINAPI K32WOWDirectedYield16( WORD htask16 )
434 * Argh. Our scheduler doesn't like DirectedYield by Win32
435 * tasks at all. So we do hope that this routine is indeed
436 * only ever called by Win16 tasks that have thunked up ...
438 DirectedYield16( (HTASK16)htask16 );
442 /***********************************************************************
443 * K32WOWHandle32 (KERNEL32.57)
445 HANDLE WINAPI K32WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
447 switch ( type )
449 case WOW_TYPE_HWND:
450 case WOW_TYPE_HMENU:
451 case WOW_TYPE_HDWP:
452 case WOW_TYPE_HDROP:
453 case WOW_TYPE_HDC:
454 case WOW_TYPE_HFONT:
455 case WOW_TYPE_HRGN:
456 case WOW_TYPE_HBITMAP:
457 case WOW_TYPE_HBRUSH:
458 case WOW_TYPE_HPALETTE:
459 case WOW_TYPE_HPEN:
460 case WOW_TYPE_HACCEL:
461 return (HANDLE)(ULONG_PTR)handle;
463 case WOW_TYPE_HMETAFILE:
464 FIXME( "conversion of metafile handles not supported yet\n" );
465 return (HANDLE)(ULONG_PTR)handle;
467 case WOW_TYPE_HTASK:
468 return ((TDB *)GlobalLock16(handle))->teb->ClientId.UniqueThread;
470 case WOW_TYPE_FULLHWND:
471 FIXME( "conversion of full window handles not supported yet\n" );
472 return (HANDLE)(ULONG_PTR)handle;
474 default:
475 ERR( "handle 0x%04x of unknown type %d\n", handle, type );
476 return (HANDLE)(ULONG_PTR)handle;
480 /***********************************************************************
481 * K32WOWHandle16 (KERNEL32.58)
483 WORD WINAPI K32WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
485 switch ( type )
487 case WOW_TYPE_HWND:
488 case WOW_TYPE_HMENU:
489 case WOW_TYPE_HDWP:
490 case WOW_TYPE_HDROP:
491 case WOW_TYPE_HDC:
492 case WOW_TYPE_HFONT:
493 case WOW_TYPE_HRGN:
494 case WOW_TYPE_HBITMAP:
495 case WOW_TYPE_HBRUSH:
496 case WOW_TYPE_HPALETTE:
497 case WOW_TYPE_HPEN:
498 case WOW_TYPE_HACCEL:
499 case WOW_TYPE_FULLHWND:
500 if ( HIWORD(handle ) )
501 ERR( "handle %p of type %d has non-zero HIWORD\n", handle, type );
502 return LOWORD(handle);
504 case WOW_TYPE_HMETAFILE:
505 FIXME( "conversion of metafile handles not supported yet\n" );
506 return LOWORD(handle);
508 case WOW_TYPE_HTASK:
509 return TASK_GetTaskFromThread( (DWORD)handle );
511 default:
512 ERR( "handle %p of unknown type %d\n", handle, type );
513 return LOWORD(handle);
517 /**********************************************************************
518 * K32WOWCallback16Ex (KERNEL32.55)
520 BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
521 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
524 * Arguments must be prepared in the correct order by the caller
525 * (both for PASCAL and CDECL calling convention), so we simply
526 * copy them to the 16-bit stack ...
528 char *stack = (char *)CURRENT_STACK16 - cbArgs;
530 memcpy( stack, pArgs, cbArgs );
532 if (dwFlags & (WCB16_REGS|WCB16_REGS_LONG))
534 CONTEXT *context = (CONTEXT *)pdwRetCode;
536 if (TRACE_ON(relay))
538 DWORD count = cbArgs / sizeof(WORD);
539 WORD * wstack = (WORD *)stack;
541 DPRINTF("%04x:CallTo16(func=%04x:%04x,ds=%04x",
542 GetCurrentThreadId(),
543 context->SegCs, LOWORD(context->Eip), context->SegDs );
544 while (count) DPRINTF( ",%04x", wstack[--count] );
545 DPRINTF(") ss:sp=%04x:%04x",
546 SELECTOROF(NtCurrentTeb()->WOW32Reserved), OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
547 DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x bp=%04x es=%04x fs=%04x\n",
548 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
549 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
550 (WORD)context->Ebp, (WORD)context->SegEs, (WORD)context->SegFs );
551 SYSLEVEL_CheckNotLevel( 2 );
554 if (context->EFlags & 0x00020000) /* v86 mode */
556 EXCEPTION_REGISTRATION_RECORD frame;
557 frame.Handler = vm86_handler;
558 errno = 0;
559 __wine_push_frame( &frame );
560 __wine_enter_vm86( context );
561 __wine_pop_frame( &frame );
562 if (errno != 0) /* enter_vm86 will fail with ENOSYS on x64 kernels */
564 WARN("__wine_enter_vm86 failed (errno=%d)\n", errno);
565 if (errno == ENOSYS)
566 SetLastError(ERROR_NOT_SUPPORTED);
567 else
568 SetLastError(ERROR_GEN_FAILURE);
569 return FALSE;
572 else
574 /* push return address */
575 if (dwFlags & WCB16_REGS_LONG)
577 stack -= sizeof(DWORD);
578 *((DWORD *)stack) = HIWORD(call16_ret_addr);
579 stack -= sizeof(DWORD);
580 *((DWORD *)stack) = LOWORD(call16_ret_addr);
581 cbArgs += 2 * sizeof(DWORD);
583 else
585 stack -= sizeof(SEGPTR);
586 *((SEGPTR *)stack) = call16_ret_addr;
587 cbArgs += sizeof(SEGPTR);
591 * Start call by checking for pending events.
592 * Note that wine_call_to_16_regs overwrites context stack
593 * pointer so we may modify it here without a problem.
595 if (get_vm86_teb_info()->dpmi_vif)
597 context->SegSs = wine_get_ds();
598 context->Esp = (DWORD)stack;
599 insert_event_check( context );
600 cbArgs += (DWORD)stack - context->Esp;
603 _EnterWin16Lock();
604 wine_call_to_16_regs( context, cbArgs, call16_handler );
605 _LeaveWin16Lock();
608 if (TRACE_ON(relay))
610 DPRINTF("%04x:RetFrom16() ss:sp=%04x:%04x ",
611 GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->WOW32Reserved),
612 OFFSETOF(NtCurrentTeb()->WOW32Reserved));
613 DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x bp=%04x sp=%04x\n",
614 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
615 (WORD)context->Edx, (WORD)context->Ebp, (WORD)context->Esp );
616 SYSLEVEL_CheckNotLevel( 2 );
619 else
621 DWORD ret;
623 if (TRACE_ON(relay))
625 DWORD count = cbArgs / sizeof(WORD);
626 WORD * wstack = (WORD *)stack;
628 DPRINTF("%04x:CallTo16(func=%04x:%04x,ds=%04x",
629 GetCurrentThreadId(), HIWORD(vpfn16), LOWORD(vpfn16),
630 SELECTOROF(NtCurrentTeb()->WOW32Reserved) );
631 while (count) DPRINTF( ",%04x", wstack[--count] );
632 DPRINTF(") ss:sp=%04x:%04x\n",
633 SELECTOROF(NtCurrentTeb()->WOW32Reserved), OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
634 SYSLEVEL_CheckNotLevel( 2 );
637 /* push return address */
638 stack -= sizeof(SEGPTR);
639 *((SEGPTR *)stack) = call16_ret_addr;
640 cbArgs += sizeof(SEGPTR);
643 * Actually, we should take care whether the called routine cleans up
644 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
645 * the callee to do so; after the routine has returned, the 16-bit
646 * stack pointer is always reset to the position it had before.
648 _EnterWin16Lock();
649 ret = wine_call_to_16( (FARPROC16)vpfn16, cbArgs, call16_handler );
650 if (pdwRetCode) *pdwRetCode = ret;
651 _LeaveWin16Lock();
653 if (TRACE_ON(relay))
655 DPRINTF("%04x:RetFrom16() ss:sp=%04x:%04x retval=%08x\n",
656 GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->WOW32Reserved),
657 OFFSETOF(NtCurrentTeb()->WOW32Reserved), ret);
658 SYSLEVEL_CheckNotLevel( 2 );
662 return TRUE; /* success */
665 /**********************************************************************
666 * K32WOWCallback16 (KERNEL32.54)
668 DWORD WINAPI K32WOWCallback16( DWORD vpfn16, DWORD dwParam )
670 DWORD ret;
672 if ( !K32WOWCallback16Ex( vpfn16, WCB16_PASCAL,
673 sizeof(DWORD), &dwParam, &ret ) )
674 ret = 0L;
676 return ret;