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
22 #include "wine/port.h"
28 #include "wine/winbase16.h"
35 #include "kernel_private.h"
36 #include "kernel16_private.h"
37 #include "wine/exception.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
44 WINE_DECLARE_DEBUG_CHANNEL(relay);
45 WINE_DECLARE_DEBUG_CHANNEL(snoop);
47 /* symbols exported from relay16.s */
48 extern DWORD WINAPI wine_call_to_16( FARPROC16 target, DWORD cbArgs, PEXCEPTION_HANDLER handler );
49 extern void WINAPI wine_call_to_16_regs( CONTEXT86 *context, DWORD cbArgs, PEXCEPTION_HANDLER handler );
50 extern void __wine_call_to_16_ret(void);
51 extern void CALL32_CBClient_Ret(void);
52 extern void CALL32_CBClientEx_Ret(void);
53 extern void DPMI_PendingEventCheck(void);
54 extern void DPMI_PendingEventCheck_Cleanup(void);
55 extern void DPMI_PendingEventCheck_Return(void);
56 extern BYTE __wine_call16_start[];
57 extern BYTE __wine_call16_end[];
59 extern void RELAY16_InitDebugLists(void);
61 static SEGPTR call16_ret_addr; /* segptr to __wine_call_to_16_ret routine */
63 static WORD dpmi_checker_selector;
64 static DWORD dpmi_checker_offset_call;
65 static DWORD dpmi_checker_offset_cleanup;
66 static DWORD dpmi_checker_offset_return;
68 /***********************************************************************
71 BOOL WOWTHUNK_Init(void)
73 /* allocate the code selector for CallTo16 routines */
75 WORD codesel = wine_ldt_alloc_entries(1);
77 if (!codesel) return FALSE;
78 wine_ldt_set_base( &entry, __wine_call16_start );
79 wine_ldt_set_limit( &entry, (BYTE *)(&CallTo16_TebSelector + 1) - __wine_call16_start - 1 );
80 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
81 wine_ldt_set_entry( codesel, &entry );
83 /* Patch the return addresses for CallTo16 routines */
85 CallTo16_DataSelector = wine_get_ds();
86 call16_ret_addr = MAKESEGPTR( codesel, (BYTE *)__wine_call_to_16_ret - __wine_call16_start );
87 CALL32_CBClient_RetAddr =
88 MAKESEGPTR( codesel, (BYTE *)CALL32_CBClient_Ret - __wine_call16_start );
89 CALL32_CBClientEx_RetAddr =
90 MAKESEGPTR( codesel, (BYTE *)CALL32_CBClientEx_Ret - __wine_call16_start );
92 /* Prepare selector and offsets for DPMI event checking. */
93 dpmi_checker_selector = codesel;
94 dpmi_checker_offset_call = (BYTE *)DPMI_PendingEventCheck - __wine_call16_start;
95 dpmi_checker_offset_cleanup = (BYTE *)DPMI_PendingEventCheck_Cleanup - __wine_call16_start;
96 dpmi_checker_offset_return = (BYTE *)DPMI_PendingEventCheck_Return - __wine_call16_start;
98 if (TRACE_ON(relay) || TRACE_ON(snoop)) RELAY16_InitDebugLists();
104 /*************************************************************
107 * Fix a selector load that caused an exception if it's in the
110 static BOOL fix_selector( CONTEXT *context )
113 BYTE *instr = (BYTE *)context->Eip;
115 if (instr < __wine_call16_start || instr >= __wine_call16_end) return FALSE;
118 while (*instr == 0x66 || *instr == 0x67) instr++;
122 case 0x07: /* pop es */
123 case 0x17: /* pop ss */
124 case 0x1f: /* pop ds */
126 case 0x0f: /* extended instruction */
129 case 0xa1: /* pop fs */
130 case 0xa9: /* pop gs */
139 stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
140 TRACE( "fixing up selector %x for pop instruction\n", *stack );
146 /*************************************************************
149 * Make resuming the context check for pending DPMI events
150 * before the original context is restored. This is required
151 * because DPMI events are asynchronous, they are blocked while
152 * Wine 32-bit code is being executed and we want to prevent
153 * a race when returning back to 16-bit or 32-bit DPMI context.
155 static void insert_event_check( CONTEXT *context )
157 char *stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
159 /* don't do event check while in system code */
160 if (wine_ldt_is_system(context->SegCs))
163 if(context->SegCs == dpmi_checker_selector &&
164 context->Eip >= dpmi_checker_offset_call &&
165 context->Eip <= dpmi_checker_offset_cleanup)
168 * Nested call. Stack will be preserved.
171 else if(context->SegCs == dpmi_checker_selector &&
172 context->Eip == dpmi_checker_offset_return)
175 * Nested call. We have just finished popping the fs
176 * register, lets put it back into stack.
179 stack -= sizeof(WORD);
180 *(WORD*)stack = context->SegFs;
187 * Call is not nested.
188 * Push modified registers into stack.
189 * These will be popped by the assembler stub.
192 stack -= sizeof(DWORD);
193 *(DWORD*)stack = context->EFlags;
195 stack -= sizeof(DWORD);
196 *(DWORD*)stack = context->SegCs;
198 stack -= sizeof(DWORD);
199 *(DWORD*)stack = context->Eip;
201 stack -= sizeof(WORD);
202 *(WORD*)stack = context->SegFs;
208 * Modify the context so that we jump into assembler stub.
209 * TEB access is made easier by providing the stub
210 * with the correct fs register value.
213 context->SegCs = dpmi_checker_selector;
214 context->Eip = dpmi_checker_offset_call;
215 context->SegFs = wine_get_fs();
219 /*************************************************************
222 * Handler for exceptions occurring in 16-bit code.
224 static DWORD call16_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
225 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
227 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
229 /* unwinding: restore the stack pointer in the TEB, and leave the Win16 mutex */
230 STACK32FRAME *frame32 = (STACK32FRAME *)((char *)frame - offsetof(STACK32FRAME,frame));
231 NtCurrentTeb()->WOW32Reserved = (void *)frame32->frame16;
234 else if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
235 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
237 if (wine_ldt_is_system(context->SegCs))
239 if (fix_selector( context )) return ExceptionContinueExecution;
244 DWORD ret = __wine_emulate_instruction( record, context );
247 * Insert check for pending DPMI events. Note that this
248 * check must be inserted after instructions have been
249 * emulated because the instruction emulation requires
250 * original CS:IP and the emulation may change TEB.dpmi_vif.
252 if(get_vm86_teb_info()->dpmi_vif)
253 insert_event_check( context );
255 if (ret != ExceptionContinueSearch) return ret;
257 /* check for Win16 __GP handler */
258 if ((gpHandler = HasGPHandler16( MAKESEGPTR( context->SegCs, context->Eip ) )))
260 WORD *stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
261 *--stack = context->SegCs;
262 *--stack = context->Eip;
264 if (!IS_SELECTOR_32BIT(context->SegSs))
265 context->Esp = MAKELONG( LOWORD(context->Esp - 2*sizeof(WORD)),
266 HIWORD(context->Esp) );
268 context->Esp -= 2*sizeof(WORD);
270 context->SegCs = SELECTOROF( gpHandler );
271 context->Eip = OFFSETOF( gpHandler );
272 return ExceptionContinueExecution;
276 else if (record->ExceptionCode == EXCEPTION_VM86_STI)
278 insert_event_check( context );
280 return ExceptionContinueSearch;
284 /*************************************************************
287 * Handler for exceptions occurring in vm86 code.
289 static DWORD vm86_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
290 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
292 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
293 return ExceptionContinueSearch;
295 if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
296 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
298 return __wine_emulate_instruction( record, context );
301 return ExceptionContinueSearch;
307 BOOL WOWTHUNK_Init(void)
312 #endif /* __i386__ */
316 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
319 /**********************************************************************
320 * K32WOWGetDescriptor (KERNEL32.70)
322 BOOL WINAPI K32WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
324 return GetThreadSelectorEntry( GetCurrentThread(),
325 segptr >> 16, ldtent );
328 /**********************************************************************
329 * K32WOWGetVDMPointer (KERNEL32.56)
331 LPVOID WINAPI K32WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
333 /* FIXME: add size check too */
335 if ( fProtectedMode )
338 return DOSMEM_MapRealToLinear( vp );
341 /**********************************************************************
342 * K32WOWGetVDMPointerFix (KERNEL32.68)
344 LPVOID WINAPI K32WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
347 * Hmmm. According to the docu, we should call:
349 * GlobalFix16( SELECTOROF(vp) );
351 * But this is unnecessary under Wine, as we never move global
352 * memory segments in linear memory anyway.
354 * (I'm not so sure what we are *supposed* to do if
355 * fProtectedMode is TRUE, anyway ...)
358 return K32WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
361 /**********************************************************************
362 * K32WOWGetVDMPointerUnfix (KERNEL32.69)
364 VOID WINAPI K32WOWGetVDMPointerUnfix( DWORD vp )
367 * See above why we don't call:
369 * GlobalUnfix16( SELECTOROF(vp) );
374 /**********************************************************************
375 * K32WOWGlobalAlloc16 (KERNEL32.59)
377 WORD WINAPI K32WOWGlobalAlloc16( WORD wFlags, DWORD cb )
379 return (WORD)GlobalAlloc16( wFlags, cb );
382 /**********************************************************************
383 * K32WOWGlobalFree16 (KERNEL32.62)
385 WORD WINAPI K32WOWGlobalFree16( WORD hMem )
387 return (WORD)GlobalFree16( (HGLOBAL16)hMem );
390 /**********************************************************************
391 * K32WOWGlobalUnlock16 (KERNEL32.61)
393 BOOL WINAPI K32WOWGlobalUnlock16( WORD hMem )
395 return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
398 /**********************************************************************
399 * K32WOWGlobalAllocLock16 (KERNEL32.63)
401 DWORD WINAPI K32WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
403 WORD hMem = K32WOWGlobalAlloc16( wFlags, cb );
404 if (phMem) *phMem = hMem;
406 return K32WOWGlobalLock16( hMem );
409 /**********************************************************************
410 * K32WOWGlobalLockSize16 (KERNEL32.65)
412 DWORD WINAPI K32WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
415 *pcb = GlobalSize16( (HGLOBAL16)hMem );
417 return K32WOWGlobalLock16( hMem );
420 /**********************************************************************
421 * K32WOWGlobalUnlockFree16 (KERNEL32.64)
423 WORD WINAPI K32WOWGlobalUnlockFree16( DWORD vpMem )
425 if ( !K32WOWGlobalUnlock16( HIWORD(vpMem) ) )
428 return K32WOWGlobalFree16( HIWORD(vpMem) );
432 /**********************************************************************
433 * K32WOWYield16 (KERNEL32.66)
435 VOID WINAPI K32WOWYield16( void )
438 * This does the right thing for both Win16 and Win32 tasks.
439 * More or less, at least :-/
444 /**********************************************************************
445 * K32WOWDirectedYield16 (KERNEL32.67)
447 VOID WINAPI K32WOWDirectedYield16( WORD htask16 )
450 * Argh. Our scheduler doesn't like DirectedYield by Win32
451 * tasks at all. So we do hope that this routine is indeed
452 * only ever called by Win16 tasks that have thunked up ...
454 DirectedYield16( (HTASK16)htask16 );
458 /***********************************************************************
459 * K32WOWHandle32 (KERNEL32.57)
461 HANDLE WINAPI K32WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
472 case WOW_TYPE_HBITMAP:
473 case WOW_TYPE_HBRUSH:
474 case WOW_TYPE_HPALETTE:
476 case WOW_TYPE_HACCEL:
477 return (HANDLE)(ULONG_PTR)handle;
479 case WOW_TYPE_HMETAFILE:
480 FIXME( "conversion of metafile handles not supported yet\n" );
481 return (HANDLE)(ULONG_PTR)handle;
484 return ((TDB *)GlobalLock16(handle))->teb->ClientId.UniqueThread;
486 case WOW_TYPE_FULLHWND:
487 FIXME( "conversion of full window handles not supported yet\n" );
488 return (HANDLE)(ULONG_PTR)handle;
491 ERR( "handle 0x%04x of unknown type %d\n", handle, type );
492 return (HANDLE)(ULONG_PTR)handle;
496 /***********************************************************************
497 * K32WOWHandle16 (KERNEL32.58)
499 WORD WINAPI K32WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
510 case WOW_TYPE_HBITMAP:
511 case WOW_TYPE_HBRUSH:
512 case WOW_TYPE_HPALETTE:
514 case WOW_TYPE_HACCEL:
515 case WOW_TYPE_FULLHWND:
516 if ( HIWORD(handle ) )
517 ERR( "handle %p of type %d has non-zero HIWORD\n", handle, type );
518 return LOWORD(handle);
520 case WOW_TYPE_HMETAFILE:
521 FIXME( "conversion of metafile handles not supported yet\n" );
522 return LOWORD(handle);
525 return TASK_GetTaskFromThread( (DWORD)handle );
528 ERR( "handle %p of unknown type %d\n", handle, type );
529 return LOWORD(handle);
533 /**********************************************************************
534 * K32WOWCallback16Ex (KERNEL32.55)
536 BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
537 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
541 * Arguments must be prepared in the correct order by the caller
542 * (both for PASCAL and CDECL calling convention), so we simply
543 * copy them to the 16-bit stack ...
545 char *stack = (char *)CURRENT_STACK16 - cbArgs;
547 memcpy( stack, pArgs, cbArgs );
549 if (dwFlags & (WCB16_REGS|WCB16_REGS_LONG))
551 CONTEXT *context = (CONTEXT *)pdwRetCode;
555 DWORD count = cbArgs / sizeof(WORD);
556 WORD * wstack = (WORD *)stack;
558 DPRINTF("%04x:CallTo16(func=%04x:%04x,ds=%04x",
559 GetCurrentThreadId(),
560 context->SegCs, LOWORD(context->Eip), context->SegDs );
561 while (count) DPRINTF( ",%04x", wstack[--count] );
562 DPRINTF(") ss:sp=%04x:%04x",
563 SELECTOROF(NtCurrentTeb()->WOW32Reserved), OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
564 DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x bp=%04x es=%04x fs=%04x\n",
565 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
566 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
567 (WORD)context->Ebp, (WORD)context->SegEs, (WORD)context->SegFs );
568 SYSLEVEL_CheckNotLevel( 2 );
571 if (context->EFlags & 0x00020000) /* v86 mode */
573 EXCEPTION_REGISTRATION_RECORD frame;
574 frame.Handler = vm86_handler;
576 __wine_push_frame( &frame );
577 __wine_enter_vm86( context );
578 __wine_pop_frame( &frame );
579 if (errno != 0) /* enter_vm86 will fall with ENOSYS on x64 kernels */
581 WARN("__wine_enter_vm86 failed (errno=%d)\n", errno);
583 SetLastError(ERROR_NOT_SUPPORTED);
585 SetLastError(ERROR_GEN_FAILURE);
591 /* push return address */
592 if (dwFlags & WCB16_REGS_LONG)
594 stack -= sizeof(DWORD);
595 *((DWORD *)stack) = HIWORD(call16_ret_addr);
596 stack -= sizeof(DWORD);
597 *((DWORD *)stack) = LOWORD(call16_ret_addr);
598 cbArgs += 2 * sizeof(DWORD);
602 stack -= sizeof(SEGPTR);
603 *((SEGPTR *)stack) = call16_ret_addr;
604 cbArgs += sizeof(SEGPTR);
608 * Start call by checking for pending events.
609 * Note that wine_call_to_16_regs overwrites context stack
610 * pointer so we may modify it here without a problem.
612 if (get_vm86_teb_info()->dpmi_vif)
614 context->SegSs = wine_get_ds();
615 context->Esp = (DWORD)stack;
616 insert_event_check( context );
617 cbArgs += (DWORD)stack - context->Esp;
621 wine_call_to_16_regs( context, cbArgs, call16_handler );
627 DPRINTF("%04x:RetFrom16() ss:sp=%04x:%04x ",
628 GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->WOW32Reserved),
629 OFFSETOF(NtCurrentTeb()->WOW32Reserved));
630 DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x bp=%04x sp=%04x\n",
631 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
632 (WORD)context->Edx, (WORD)context->Ebp, (WORD)context->Esp );
633 SYSLEVEL_CheckNotLevel( 2 );
642 DWORD count = cbArgs / sizeof(WORD);
643 WORD * wstack = (WORD *)stack;
645 DPRINTF("%04x:CallTo16(func=%04x:%04x,ds=%04x",
646 GetCurrentThreadId(), HIWORD(vpfn16), LOWORD(vpfn16),
647 SELECTOROF(NtCurrentTeb()->WOW32Reserved) );
648 while (count) DPRINTF( ",%04x", wstack[--count] );
649 DPRINTF(") ss:sp=%04x:%04x\n",
650 SELECTOROF(NtCurrentTeb()->WOW32Reserved), OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
651 SYSLEVEL_CheckNotLevel( 2 );
654 /* push return address */
655 stack -= sizeof(SEGPTR);
656 *((SEGPTR *)stack) = call16_ret_addr;
657 cbArgs += sizeof(SEGPTR);
660 * Actually, we should take care whether the called routine cleans up
661 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
662 * the callee to do so; after the routine has returned, the 16-bit
663 * stack pointer is always reset to the position it had before.
666 ret = wine_call_to_16( (FARPROC16)vpfn16, cbArgs, call16_handler );
667 if (pdwRetCode) *pdwRetCode = ret;
672 DPRINTF("%04x:RetFrom16() ss:sp=%04x:%04x retval=%08x\n",
673 GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->WOW32Reserved),
674 OFFSETOF(NtCurrentTeb()->WOW32Reserved), ret);
675 SYSLEVEL_CheckNotLevel( 2 );
679 assert(0); /* cannot call to 16-bit on non-Intel architectures */
680 #endif /* __i386__ */
682 return TRUE; /* success */
685 /**********************************************************************
686 * K32WOWCallback16 (KERNEL32.54)
688 DWORD WINAPI K32WOWCallback16( DWORD vpfn16, DWORD dwParam )
692 if ( !K32WOWCallback16Ex( vpfn16, WCB16_PASCAL,
693 sizeof(DWORD), &dwParam, &ret ) )