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 "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 extern void RELAY16_InitDebugLists(void);
57 static SEGPTR call16_ret_addr
; /* segptr to __wine_call_to_16_ret routine */
59 static WORD dpmi_checker_selector
;
60 static DWORD dpmi_checker_offset_call
;
61 static DWORD dpmi_checker_offset_cleanup
;
62 static DWORD dpmi_checker_offset_return
;
64 /***********************************************************************
67 BOOL
WOWTHUNK_Init(void)
69 /* allocate the code selector for CallTo16 routines */
71 WORD codesel
= wine_ldt_alloc_entries(1);
73 if (!codesel
) return FALSE
;
74 wine_ldt_set_base( &entry
, __wine_call16_start
);
75 wine_ldt_set_limit( &entry
, (BYTE
*)(&CallTo16_TebSelector
+ 1) - __wine_call16_start
- 1 );
76 wine_ldt_set_flags( &entry
, WINE_LDT_FLAGS_CODE
| WINE_LDT_FLAGS_32BIT
);
77 wine_ldt_set_entry( codesel
, &entry
);
79 /* Patch the return addresses for CallTo16 routines */
81 CallTo16_DataSelector
= wine_get_ds();
82 call16_ret_addr
= MAKESEGPTR( codesel
, (BYTE
*)__wine_call_to_16_ret
- __wine_call16_start
);
83 CALL32_CBClient_RetAddr
=
84 MAKESEGPTR( codesel
, (BYTE
*)CALL32_CBClient_Ret
- __wine_call16_start
);
85 CALL32_CBClientEx_RetAddr
=
86 MAKESEGPTR( codesel
, (BYTE
*)CALL32_CBClientEx_Ret
- __wine_call16_start
);
88 /* Prepare selector and offsets for DPMI event checking. */
89 dpmi_checker_selector
= codesel
;
90 dpmi_checker_offset_call
= (BYTE
*)DPMI_PendingEventCheck
- __wine_call16_start
;
91 dpmi_checker_offset_cleanup
= (BYTE
*)DPMI_PendingEventCheck_Cleanup
- __wine_call16_start
;
92 dpmi_checker_offset_return
= (BYTE
*)DPMI_PendingEventCheck_Return
- __wine_call16_start
;
94 if (TRACE_ON(relay
) || TRACE_ON(snoop
)) RELAY16_InitDebugLists();
100 /*************************************************************
103 * Fix a selector load that caused an exception if it's in the
106 static BOOL
fix_selector( CONTEXT
*context
)
109 BYTE
*instr
= (BYTE
*)context
->Eip
;
111 if (instr
< __wine_call16_start
|| instr
>= __wine_call16_end
) return FALSE
;
114 while (*instr
== 0x66 || *instr
== 0x67) instr
++;
118 case 0x07: /* pop es */
119 case 0x17: /* pop ss */
120 case 0x1f: /* pop ds */
122 case 0x0f: /* extended instruction */
125 case 0xa1: /* pop fs */
126 case 0xa9: /* pop gs */
135 stack
= wine_ldt_get_ptr( context
->SegSs
, context
->Esp
);
136 TRACE( "fixing up selector %x for pop instruction\n", *stack
);
142 /*************************************************************
145 * Make resuming the context check for pending DPMI events
146 * before the original context is restored. This is required
147 * because DPMI events are asynchronous, they are blocked while
148 * Wine 32-bit code is being executed and we want to prevent
149 * a race when returning back to 16-bit or 32-bit DPMI context.
151 static void insert_event_check( CONTEXT
*context
)
153 char *stack
= wine_ldt_get_ptr( context
->SegSs
, context
->Esp
);
155 /* don't do event check while in system code */
156 if (wine_ldt_is_system(context
->SegCs
))
159 if(context
->SegCs
== dpmi_checker_selector
&&
160 context
->Eip
>= dpmi_checker_offset_call
&&
161 context
->Eip
<= dpmi_checker_offset_cleanup
)
164 * Nested call. Stack will be preserved.
167 else if(context
->SegCs
== dpmi_checker_selector
&&
168 context
->Eip
== dpmi_checker_offset_return
)
171 * Nested call. We have just finished popping the fs
172 * register, lets put it back into stack.
175 stack
-= sizeof(WORD
);
176 *(WORD
*)stack
= context
->SegFs
;
183 * Call is not nested.
184 * Push modified registers into stack.
185 * These will be popped by the assembler stub.
188 stack
-= sizeof(DWORD
);
189 *(DWORD
*)stack
= context
->EFlags
;
191 stack
-= sizeof(DWORD
);
192 *(DWORD
*)stack
= context
->SegCs
;
194 stack
-= sizeof(DWORD
);
195 *(DWORD
*)stack
= context
->Eip
;
197 stack
-= sizeof(WORD
);
198 *(WORD
*)stack
= context
->SegFs
;
204 * Modify the context so that we jump into assembler stub.
205 * TEB access is made easier by providing the stub
206 * with the correct fs register value.
209 context
->SegCs
= dpmi_checker_selector
;
210 context
->Eip
= dpmi_checker_offset_call
;
211 context
->SegFs
= wine_get_fs();
215 /*************************************************************
218 * Handler for exceptions occurring in 16-bit code.
220 static DWORD
call16_handler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
221 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
223 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
225 /* unwinding: restore the stack pointer in the TEB, and leave the Win16 mutex */
226 STACK32FRAME
*frame32
= CONTAINING_RECORD(frame
, STACK32FRAME
, frame
);
227 NtCurrentTeb()->WOW32Reserved
= (void *)frame32
->frame16
;
230 else if (record
->ExceptionCode
== EXCEPTION_ACCESS_VIOLATION
||
231 record
->ExceptionCode
== EXCEPTION_PRIV_INSTRUCTION
)
233 if (wine_ldt_is_system(context
->SegCs
))
235 if (fix_selector( context
)) return ExceptionContinueExecution
;
240 DWORD ret
= __wine_emulate_instruction( record
, context
);
243 * Insert check for pending DPMI events. Note that this
244 * check must be inserted after instructions have been
245 * emulated because the instruction emulation requires
246 * original CS:IP and the emulation may change TEB.dpmi_vif.
248 if(get_vm86_teb_info()->dpmi_vif
)
249 insert_event_check( context
);
251 if (ret
!= ExceptionContinueSearch
) return ret
;
253 /* check for Win16 __GP handler */
254 if ((gpHandler
= HasGPHandler16( MAKESEGPTR( context
->SegCs
, context
->Eip
) )))
256 WORD
*stack
= wine_ldt_get_ptr( context
->SegSs
, context
->Esp
);
257 *--stack
= context
->SegCs
;
258 *--stack
= context
->Eip
;
260 if (!IS_SELECTOR_32BIT(context
->SegSs
))
261 context
->Esp
= MAKELONG( LOWORD(context
->Esp
- 2*sizeof(WORD
)),
262 HIWORD(context
->Esp
) );
264 context
->Esp
-= 2*sizeof(WORD
);
266 context
->SegCs
= SELECTOROF( gpHandler
);
267 context
->Eip
= OFFSETOF( gpHandler
);
268 return ExceptionContinueExecution
;
272 else if (record
->ExceptionCode
== EXCEPTION_VM86_STI
)
274 insert_event_check( context
);
276 return ExceptionContinueSearch
;
280 /*************************************************************
283 * Handler for exceptions occurring in vm86 code.
285 static DWORD
vm86_handler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
286 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
288 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
289 return ExceptionContinueSearch
;
291 if (record
->ExceptionCode
== EXCEPTION_ACCESS_VIOLATION
||
292 record
->ExceptionCode
== EXCEPTION_PRIV_INSTRUCTION
)
294 return __wine_emulate_instruction( record
, context
);
297 return ExceptionContinueSearch
;
302 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
305 /**********************************************************************
306 * K32WOWGetDescriptor (KERNEL32.70)
308 BOOL WINAPI
K32WOWGetDescriptor( SEGPTR segptr
, LPLDT_ENTRY ldtent
)
310 return GetThreadSelectorEntry( GetCurrentThread(),
311 segptr
>> 16, ldtent
);
314 /**********************************************************************
315 * K32WOWGetVDMPointer (KERNEL32.56)
317 LPVOID WINAPI
K32WOWGetVDMPointer( DWORD vp
, DWORD dwBytes
, BOOL fProtectedMode
)
319 /* FIXME: add size check too */
321 if ( fProtectedMode
)
324 return DOSMEM_MapRealToLinear( vp
);
327 /**********************************************************************
328 * K32WOWGetVDMPointerFix (KERNEL32.68)
330 LPVOID WINAPI
K32WOWGetVDMPointerFix( DWORD vp
, DWORD dwBytes
, BOOL fProtectedMode
)
333 * Hmmm. According to the docu, we should call:
335 * GlobalFix16( SELECTOROF(vp) );
337 * But this is unnecessary under Wine, as we never move global
338 * memory segments in linear memory anyway.
340 * (I'm not so sure what we are *supposed* to do if
341 * fProtectedMode is TRUE, anyway ...)
344 return K32WOWGetVDMPointer( vp
, dwBytes
, fProtectedMode
);
347 /**********************************************************************
348 * K32WOWGetVDMPointerUnfix (KERNEL32.69)
350 VOID WINAPI
K32WOWGetVDMPointerUnfix( DWORD vp
)
353 * See above why we don't call:
355 * GlobalUnfix16( SELECTOROF(vp) );
360 /**********************************************************************
361 * K32WOWGlobalAlloc16 (KERNEL32.59)
363 WORD WINAPI
K32WOWGlobalAlloc16( WORD wFlags
, DWORD cb
)
365 return (WORD
)GlobalAlloc16( wFlags
, cb
);
368 /**********************************************************************
369 * K32WOWGlobalFree16 (KERNEL32.62)
371 WORD WINAPI
K32WOWGlobalFree16( WORD hMem
)
373 return (WORD
)GlobalFree16( (HGLOBAL16
)hMem
);
376 /**********************************************************************
377 * K32WOWGlobalUnlock16 (KERNEL32.61)
379 BOOL WINAPI
K32WOWGlobalUnlock16( WORD hMem
)
381 return (BOOL
)GlobalUnlock16( (HGLOBAL16
)hMem
);
384 /**********************************************************************
385 * K32WOWGlobalAllocLock16 (KERNEL32.63)
387 DWORD WINAPI
K32WOWGlobalAllocLock16( WORD wFlags
, DWORD cb
, WORD
*phMem
)
389 WORD hMem
= K32WOWGlobalAlloc16( wFlags
, cb
);
390 if (phMem
) *phMem
= hMem
;
392 return K32WOWGlobalLock16( hMem
);
395 /**********************************************************************
396 * K32WOWGlobalLockSize16 (KERNEL32.65)
398 DWORD WINAPI
K32WOWGlobalLockSize16( WORD hMem
, PDWORD pcb
)
401 *pcb
= GlobalSize16( (HGLOBAL16
)hMem
);
403 return K32WOWGlobalLock16( hMem
);
406 /**********************************************************************
407 * K32WOWGlobalUnlockFree16 (KERNEL32.64)
409 WORD WINAPI
K32WOWGlobalUnlockFree16( DWORD vpMem
)
411 if ( !K32WOWGlobalUnlock16( HIWORD(vpMem
) ) )
414 return K32WOWGlobalFree16( HIWORD(vpMem
) );
418 /**********************************************************************
419 * K32WOWYield16 (KERNEL32.66)
421 VOID WINAPI
K32WOWYield16( void )
424 * This does the right thing for both Win16 and Win32 tasks.
425 * More or less, at least :-/
430 /**********************************************************************
431 * K32WOWDirectedYield16 (KERNEL32.67)
433 VOID WINAPI
K32WOWDirectedYield16( WORD htask16
)
436 * Argh. Our scheduler doesn't like DirectedYield by Win32
437 * tasks at all. So we do hope that this routine is indeed
438 * only ever called by Win16 tasks that have thunked up ...
440 DirectedYield16( (HTASK16
)htask16
);
444 /***********************************************************************
445 * K32WOWHandle32 (KERNEL32.57)
447 HANDLE WINAPI
K32WOWHandle32( WORD handle
, WOW_HANDLE_TYPE type
)
458 case WOW_TYPE_HBITMAP
:
459 case WOW_TYPE_HBRUSH
:
460 case WOW_TYPE_HPALETTE
:
462 case WOW_TYPE_HACCEL
:
463 return (HANDLE
)(ULONG_PTR
)handle
;
465 case WOW_TYPE_HMETAFILE
:
466 FIXME( "conversion of metafile handles not supported yet\n" );
467 return (HANDLE
)(ULONG_PTR
)handle
;
470 return ((TDB
*)GlobalLock16(handle
))->teb
->ClientId
.UniqueThread
;
472 case WOW_TYPE_FULLHWND
:
473 FIXME( "conversion of full window handles not supported yet\n" );
474 return (HANDLE
)(ULONG_PTR
)handle
;
477 ERR( "handle 0x%04x of unknown type %d\n", handle
, type
);
478 return (HANDLE
)(ULONG_PTR
)handle
;
482 /***********************************************************************
483 * K32WOWHandle16 (KERNEL32.58)
485 WORD WINAPI
K32WOWHandle16( HANDLE handle
, WOW_HANDLE_TYPE type
)
496 case WOW_TYPE_HBITMAP
:
497 case WOW_TYPE_HBRUSH
:
498 case WOW_TYPE_HPALETTE
:
500 case WOW_TYPE_HACCEL
:
501 case WOW_TYPE_FULLHWND
:
502 if ( HIWORD(handle
) )
503 ERR( "handle %p of type %d has non-zero HIWORD\n", handle
, type
);
504 return LOWORD(handle
);
506 case WOW_TYPE_HMETAFILE
:
507 FIXME( "conversion of metafile handles not supported yet\n" );
508 return LOWORD(handle
);
511 return TASK_GetTaskFromThread( (DWORD
)handle
);
514 ERR( "handle %p of unknown type %d\n", handle
, type
);
515 return LOWORD(handle
);
519 /**********************************************************************
520 * K32WOWCallback16Ex (KERNEL32.55)
522 BOOL WINAPI
K32WOWCallback16Ex( DWORD vpfn16
, DWORD dwFlags
,
523 DWORD cbArgs
, LPVOID pArgs
, LPDWORD pdwRetCode
)
526 * Arguments must be prepared in the correct order by the caller
527 * (both for PASCAL and CDECL calling convention), so we simply
528 * copy them to the 16-bit stack ...
530 char *stack
= (char *)CURRENT_STACK16
- cbArgs
;
532 memcpy( stack
, pArgs
, cbArgs
);
534 if (dwFlags
& (WCB16_REGS
|WCB16_REGS_LONG
))
536 CONTEXT
*context
= (CONTEXT
*)pdwRetCode
;
540 DWORD count
= cbArgs
/ sizeof(WORD
);
541 WORD
* wstack
= (WORD
*)stack
;
543 DPRINTF("%04x:CallTo16(func=%04x:%04x,ds=%04x",
544 GetCurrentThreadId(),
545 context
->SegCs
, LOWORD(context
->Eip
), context
->SegDs
);
546 while (count
) DPRINTF( ",%04x", wstack
[--count
] );
547 DPRINTF(") ss:sp=%04x:%04x",
548 SELECTOROF(NtCurrentTeb()->WOW32Reserved
), OFFSETOF(NtCurrentTeb()->WOW32Reserved
) );
549 DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x bp=%04x es=%04x fs=%04x\n",
550 (WORD
)context
->Eax
, (WORD
)context
->Ebx
, (WORD
)context
->Ecx
,
551 (WORD
)context
->Edx
, (WORD
)context
->Esi
, (WORD
)context
->Edi
,
552 (WORD
)context
->Ebp
, (WORD
)context
->SegEs
, (WORD
)context
->SegFs
);
553 SYSLEVEL_CheckNotLevel( 2 );
556 if (context
->EFlags
& 0x00020000) /* v86 mode */
558 EXCEPTION_REGISTRATION_RECORD frame
;
559 frame
.Handler
= vm86_handler
;
561 __wine_push_frame( &frame
);
562 __wine_enter_vm86( context
);
563 __wine_pop_frame( &frame
);
564 if (errno
!= 0) /* enter_vm86 will fall with ENOSYS on x64 kernels */
566 WARN("__wine_enter_vm86 failed (errno=%d)\n", errno
);
568 SetLastError(ERROR_NOT_SUPPORTED
);
570 SetLastError(ERROR_GEN_FAILURE
);
576 /* push return address */
577 if (dwFlags
& WCB16_REGS_LONG
)
579 stack
-= sizeof(DWORD
);
580 *((DWORD
*)stack
) = HIWORD(call16_ret_addr
);
581 stack
-= sizeof(DWORD
);
582 *((DWORD
*)stack
) = LOWORD(call16_ret_addr
);
583 cbArgs
+= 2 * sizeof(DWORD
);
587 stack
-= sizeof(SEGPTR
);
588 *((SEGPTR
*)stack
) = call16_ret_addr
;
589 cbArgs
+= sizeof(SEGPTR
);
593 * Start call by checking for pending events.
594 * Note that wine_call_to_16_regs overwrites context stack
595 * pointer so we may modify it here without a problem.
597 if (get_vm86_teb_info()->dpmi_vif
)
599 context
->SegSs
= wine_get_ds();
600 context
->Esp
= (DWORD
)stack
;
601 insert_event_check( context
);
602 cbArgs
+= (DWORD
)stack
- context
->Esp
;
606 wine_call_to_16_regs( context
, cbArgs
, call16_handler
);
612 DPRINTF("%04x:RetFrom16() ss:sp=%04x:%04x ",
613 GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->WOW32Reserved
),
614 OFFSETOF(NtCurrentTeb()->WOW32Reserved
));
615 DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x bp=%04x sp=%04x\n",
616 (WORD
)context
->Eax
, (WORD
)context
->Ebx
, (WORD
)context
->Ecx
,
617 (WORD
)context
->Edx
, (WORD
)context
->Ebp
, (WORD
)context
->Esp
);
618 SYSLEVEL_CheckNotLevel( 2 );
627 DWORD count
= cbArgs
/ sizeof(WORD
);
628 WORD
* wstack
= (WORD
*)stack
;
630 DPRINTF("%04x:CallTo16(func=%04x:%04x,ds=%04x",
631 GetCurrentThreadId(), HIWORD(vpfn16
), LOWORD(vpfn16
),
632 SELECTOROF(NtCurrentTeb()->WOW32Reserved
) );
633 while (count
) DPRINTF( ",%04x", wstack
[--count
] );
634 DPRINTF(") ss:sp=%04x:%04x\n",
635 SELECTOROF(NtCurrentTeb()->WOW32Reserved
), OFFSETOF(NtCurrentTeb()->WOW32Reserved
) );
636 SYSLEVEL_CheckNotLevel( 2 );
639 /* push return address */
640 stack
-= sizeof(SEGPTR
);
641 *((SEGPTR
*)stack
) = call16_ret_addr
;
642 cbArgs
+= sizeof(SEGPTR
);
645 * Actually, we should take care whether the called routine cleans up
646 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
647 * the callee to do so; after the routine has returned, the 16-bit
648 * stack pointer is always reset to the position it had before.
651 ret
= wine_call_to_16( (FARPROC16
)vpfn16
, cbArgs
, call16_handler
);
652 if (pdwRetCode
) *pdwRetCode
= ret
;
657 DPRINTF("%04x:RetFrom16() ss:sp=%04x:%04x retval=%08x\n",
658 GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->WOW32Reserved
),
659 OFFSETOF(NtCurrentTeb()->WOW32Reserved
), ret
);
660 SYSLEVEL_CheckNotLevel( 2 );
664 return TRUE
; /* success */
667 /**********************************************************************
668 * K32WOWCallback16 (KERNEL32.54)
670 DWORD WINAPI
K32WOWCallback16( DWORD vpfn16
, DWORD dwParam
)
674 if ( !K32WOWCallback16Ex( vpfn16
, WCB16_PASCAL
,
675 sizeof(DWORD
), &dwParam
, &ret
) )