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
24 #include "wine/winbase16.h"
32 #include "kernel16_private.h"
33 #include "wine/exception.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(thunk
);
37 WINE_DECLARE_DEBUG_CHANNEL(relay
);
38 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
40 /* symbols exported from relay16.s */
41 extern DWORD WINAPI
wine_call_to_16( FARPROC16 target
, DWORD cbArgs
, PEXCEPTION_HANDLER handler
);
42 extern void WINAPI
wine_call_to_16_regs( CONTEXT
*context
, DWORD cbArgs
, PEXCEPTION_HANDLER handler
);
43 extern void __wine_call_to_16_ret(void);
44 extern void CALL32_CBClient_Ret(void);
45 extern void CALL32_CBClientEx_Ret(void);
46 extern BYTE __wine_call16_start
[];
47 extern BYTE __wine_call16_end
[];
49 static SEGPTR call16_ret_addr
; /* segptr to __wine_call_to_16_ret routine */
51 /***********************************************************************
54 BOOL
WOWTHUNK_Init(void)
56 /* allocate the code selector for CallTo16 routines */
57 WORD codesel
= SELECTOR_AllocBlock( __wine_call16_start
,
58 (BYTE
*)(&CallTo16_TebSelector
+ 1) - __wine_call16_start
,
59 LDT_FLAGS_CODE
| LDT_FLAGS_32BIT
);
60 if (!codesel
) return FALSE
;
62 /* Patch the return addresses for CallTo16 routines */
64 CallTo16_DataSelector
= get_ds();
65 call16_ret_addr
= MAKESEGPTR( codesel
, (BYTE
*)__wine_call_to_16_ret
- __wine_call16_start
);
66 CALL32_CBClient_RetAddr
=
67 MAKESEGPTR( codesel
, (BYTE
*)CALL32_CBClient_Ret
- __wine_call16_start
);
68 CALL32_CBClientEx_RetAddr
=
69 MAKESEGPTR( codesel
, (BYTE
*)CALL32_CBClientEx_Ret
- __wine_call16_start
);
71 if (TRACE_ON(relay
) || TRACE_ON(snoop
)) RELAY16_InitDebugLists();
77 /*************************************************************
80 * Fix a selector load that caused an exception if it's in the
83 static BOOL
fix_selector( CONTEXT
*context
)
86 BYTE
*instr
= (BYTE
*)context
->Eip
;
88 if (instr
< __wine_call16_start
|| instr
>= __wine_call16_end
) return FALSE
;
91 while (*instr
== 0x66 || *instr
== 0x67) instr
++;
95 case 0x07: /* pop es */
96 case 0x17: /* pop ss */
97 case 0x1f: /* pop ds */
99 case 0x0f: /* extended instruction */
102 case 0xa1: /* pop fs */
103 case 0xa9: /* pop gs */
112 stack
= ldt_get_ptr( context
->SegSs
, context
->Esp
);
113 TRACE( "fixing up selector %x for pop instruction\n", *stack
);
119 /*************************************************************
122 * Handler for exceptions occurring in 16-bit code.
124 static DWORD
call16_handler( EXCEPTION_RECORD
*record
, EXCEPTION_REGISTRATION_RECORD
*frame
,
125 CONTEXT
*context
, EXCEPTION_REGISTRATION_RECORD
**pdispatcher
)
127 if (record
->ExceptionFlags
& (EH_UNWINDING
| EH_EXIT_UNWIND
))
129 /* unwinding: restore the stack pointer in the TEB, and leave the Win16 mutex */
130 STACK32FRAME
*frame32
= CONTAINING_RECORD(frame
, STACK32FRAME
, frame
);
131 kernel_get_thread_data()->stack
= frame32
->frame16
;
134 else if (record
->ExceptionCode
== EXCEPTION_ACCESS_VIOLATION
||
135 record
->ExceptionCode
== EXCEPTION_PRIV_INSTRUCTION
)
137 if (ldt_is_system(context
->SegCs
))
139 if (fix_selector( context
)) return ExceptionContinueExecution
;
144 DWORD ret
= __wine_emulate_instruction( record
, context
);
146 if (ret
!= ExceptionContinueSearch
) return ret
;
148 /* check for Win16 __GP handler */
149 if ((gpHandler
= HasGPHandler16( MAKESEGPTR( context
->SegCs
, context
->Eip
) )))
151 WORD
*stack
= ldt_get_ptr( context
->SegSs
, context
->Esp
);
152 *--stack
= context
->SegCs
;
153 *--stack
= context
->Eip
;
155 if (!IS_SELECTOR_32BIT(context
->SegSs
))
156 context
->Esp
= MAKELONG( LOWORD(context
->Esp
- 2*sizeof(WORD
)),
157 HIWORD(context
->Esp
) );
159 context
->Esp
-= 2*sizeof(WORD
);
161 context
->SegCs
= SELECTOROF( gpHandler
);
162 context
->Eip
= OFFSETOF( gpHandler
);
163 return ExceptionContinueExecution
;
167 return ExceptionContinueSearch
;
172 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
175 /**********************************************************************
176 * K32WOWGetDescriptor (KERNEL32.70)
178 BOOL WINAPI
K32WOWGetDescriptor( SEGPTR segptr
, LPLDT_ENTRY ldtent
)
180 return GetThreadSelectorEntry( GetCurrentThread(),
181 segptr
>> 16, ldtent
);
184 /**********************************************************************
185 * K32WOWGetVDMPointer (KERNEL32.56)
187 LPVOID WINAPI
K32WOWGetVDMPointer( DWORD vp
, DWORD dwBytes
, BOOL fProtectedMode
)
189 /* FIXME: add size check too */
191 if ( fProtectedMode
)
194 return DOSMEM_MapRealToLinear( vp
);
197 /**********************************************************************
198 * K32WOWGetVDMPointerFix (KERNEL32.68)
200 LPVOID WINAPI
K32WOWGetVDMPointerFix( DWORD vp
, DWORD dwBytes
, BOOL fProtectedMode
)
203 * Hmmm. According to the docu, we should call:
205 * GlobalFix16( SELECTOROF(vp) );
207 * But this is unnecessary under Wine, as we never move global
208 * memory segments in linear memory anyway.
210 * (I'm not so sure what we are *supposed* to do if
211 * fProtectedMode is TRUE, anyway ...)
214 return K32WOWGetVDMPointer( vp
, dwBytes
, fProtectedMode
);
217 /**********************************************************************
218 * K32WOWGetVDMPointerUnfix (KERNEL32.69)
220 VOID WINAPI
K32WOWGetVDMPointerUnfix( DWORD vp
)
223 * See above why we don't call:
225 * GlobalUnfix16( SELECTOROF(vp) );
230 /**********************************************************************
231 * K32WOWGlobalAlloc16 (KERNEL32.59)
233 WORD WINAPI
K32WOWGlobalAlloc16( WORD wFlags
, DWORD cb
)
235 return (WORD
)GlobalAlloc16( wFlags
, cb
);
238 /**********************************************************************
239 * K32WOWGlobalFree16 (KERNEL32.62)
241 WORD WINAPI
K32WOWGlobalFree16( WORD hMem
)
243 return (WORD
)GlobalFree16( (HGLOBAL16
)hMem
);
246 /**********************************************************************
247 * K32WOWGlobalUnlock16 (KERNEL32.61)
249 BOOL WINAPI
K32WOWGlobalUnlock16( WORD hMem
)
251 return (BOOL
)GlobalUnlock16( (HGLOBAL16
)hMem
);
254 /**********************************************************************
255 * K32WOWGlobalAllocLock16 (KERNEL32.63)
257 DWORD WINAPI
K32WOWGlobalAllocLock16( WORD wFlags
, DWORD cb
, WORD
*phMem
)
259 WORD hMem
= K32WOWGlobalAlloc16( wFlags
, cb
);
260 if (phMem
) *phMem
= hMem
;
262 return K32WOWGlobalLock16( hMem
);
265 /**********************************************************************
266 * K32WOWGlobalLockSize16 (KERNEL32.65)
268 DWORD WINAPI
K32WOWGlobalLockSize16( WORD hMem
, PDWORD pcb
)
271 *pcb
= GlobalSize16( (HGLOBAL16
)hMem
);
273 return K32WOWGlobalLock16( hMem
);
276 /**********************************************************************
277 * K32WOWGlobalUnlockFree16 (KERNEL32.64)
279 WORD WINAPI
K32WOWGlobalUnlockFree16( DWORD vpMem
)
281 if ( !K32WOWGlobalUnlock16( HIWORD(vpMem
) ) )
284 return K32WOWGlobalFree16( HIWORD(vpMem
) );
288 /**********************************************************************
289 * K32WOWYield16 (KERNEL32.66)
291 VOID WINAPI
K32WOWYield16( void )
294 * This does the right thing for both Win16 and Win32 tasks.
295 * More or less, at least :-/
300 /**********************************************************************
301 * K32WOWDirectedYield16 (KERNEL32.67)
303 VOID WINAPI
K32WOWDirectedYield16( WORD htask16
)
306 * Argh. Our scheduler doesn't like DirectedYield by Win32
307 * tasks at all. So we do hope that this routine is indeed
308 * only ever called by Win16 tasks that have thunked up ...
310 DirectedYield16( (HTASK16
)htask16
);
313 static HANDLE
gdi_handle32( WORD handle
)
315 static GDI_SHARED_MEMORY
*gdi_shared
;
319 if (NtCurrentTeb()->GdiBatchCount
)
321 TEB64
*teb64
= (TEB64
*)(UINT_PTR
)NtCurrentTeb()->GdiBatchCount
;
322 PEB64
*peb64
= (PEB64
*)(UINT_PTR
)teb64
->Peb
;
323 gdi_shared
= (GDI_SHARED_MEMORY
*)(UINT_PTR
)peb64
->GdiSharedHandleTable
;
325 else gdi_shared
= (GDI_SHARED_MEMORY
*)NtCurrentTeb()->Peb
->GdiSharedHandleTable
;
326 if (!gdi_shared
) return ULongToHandle( handle
);
329 return ULongToHandle( (gdi_shared
->Handles
[handle
].Unique
<< 16) | handle
);
332 /***********************************************************************
333 * K32WOWHandle32 (KERNEL32.57)
335 HANDLE WINAPI
K32WOWHandle32( WORD handle
, WOW_HANDLE_TYPE type
)
343 case WOW_TYPE_HACCEL
:
344 return (HANDLE
)(ULONG_PTR
)handle
;
349 case WOW_TYPE_HBITMAP
:
350 case WOW_TYPE_HBRUSH
:
351 case WOW_TYPE_HPALETTE
:
353 case WOW_TYPE_HMETAFILE
:
354 return gdi_handle32( handle
);
357 return ((TDB
*)GlobalLock16(handle
))->teb
->ClientId
.UniqueThread
;
359 case WOW_TYPE_FULLHWND
:
360 FIXME( "conversion of full window handles not supported yet\n" );
361 return (HANDLE
)(ULONG_PTR
)handle
;
364 ERR( "handle 0x%04x of unknown type %d\n", handle
, type
);
365 return (HANDLE
)(ULONG_PTR
)handle
;
369 /***********************************************************************
370 * K32WOWHandle16 (KERNEL32.58)
372 WORD WINAPI
K32WOWHandle16( HANDLE handle
, WOW_HANDLE_TYPE type
)
383 case WOW_TYPE_HBITMAP
:
384 case WOW_TYPE_HBRUSH
:
385 case WOW_TYPE_HPALETTE
:
387 case WOW_TYPE_HACCEL
:
388 case WOW_TYPE_FULLHWND
:
389 if ( HIWORD(handle
) )
390 ERR( "handle %p of type %d has non-zero HIWORD\n", handle
, type
);
391 return LOWORD(handle
);
393 case WOW_TYPE_HMETAFILE
:
394 FIXME( "conversion of metafile handles not supported yet\n" );
395 return LOWORD(handle
);
398 return TASK_GetTaskFromThread( (DWORD
)handle
);
401 ERR( "handle %p of unknown type %d\n", handle
, type
);
402 return LOWORD(handle
);
406 /**********************************************************************
407 * K32WOWCallback16Ex (KERNEL32.55)
409 BOOL WINAPI
K32WOWCallback16Ex( DWORD vpfn16
, DWORD dwFlags
,
410 DWORD cbArgs
, LPVOID pArgs
, LPDWORD pdwRetCode
)
413 * Arguments must be prepared in the correct order by the caller
414 * (both for PASCAL and CDECL calling convention), so we simply
415 * copy them to the 16-bit stack ...
417 char *stack
= (char *)CURRENT_STACK16
- cbArgs
;
419 memcpy( stack
, pArgs
, cbArgs
);
421 if (dwFlags
& WCB16_REGS
)
423 CONTEXT
*context
= (CONTEXT
*)pdwRetCode
;
427 DWORD count
= cbArgs
/ sizeof(WORD
);
428 WORD
* wstack
= (WORD
*)stack
;
430 TRACE_(relay
)( "\1CallTo16(func=%04x:%04x", context
->SegCs
, LOWORD(context
->Eip
) );
431 while (count
) TRACE_(relay
)( ",%04x", wstack
[--count
] );
432 TRACE_(relay
)( ") ss:sp=%04x:%04x ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x bp=%04x ds=%04x es=%04x\n",
433 CURRENT_SS
, CURRENT_SP
,
434 (WORD
)context
->Eax
, (WORD
)context
->Ebx
, (WORD
)context
->Ecx
,
435 (WORD
)context
->Edx
, (WORD
)context
->Esi
, (WORD
)context
->Edi
,
436 (WORD
)context
->Ebp
, (WORD
)context
->SegDs
, (WORD
)context
->SegEs
);
437 SYSLEVEL_CheckNotLevel( 2 );
440 /* push return address */
441 stack
-= sizeof(SEGPTR
);
442 *((SEGPTR
*)stack
) = call16_ret_addr
;
443 cbArgs
+= sizeof(SEGPTR
);
446 wine_call_to_16_regs( context
, cbArgs
, call16_handler
);
451 TRACE_(relay
)( "\1RetFrom16() ss:sp=%04x:%04x ax=%04x bx=%04x cx=%04x dx=%04x bp=%04x sp=%04x\n",
452 CURRENT_SS
, CURRENT_SP
,
453 (WORD
)context
->Eax
, (WORD
)context
->Ebx
, (WORD
)context
->Ecx
,
454 (WORD
)context
->Edx
, (WORD
)context
->Ebp
, (WORD
)context
->Esp
);
455 SYSLEVEL_CheckNotLevel( 2 );
464 DWORD count
= cbArgs
/ sizeof(WORD
);
465 WORD
* wstack
= (WORD
*)stack
;
467 TRACE_(relay
)( "\1CallTo16(func=%04x:%04x,ds=%04x",
468 HIWORD(vpfn16
), LOWORD(vpfn16
), CURRENT_SS
);
469 while (count
) TRACE_(relay
)( ",%04x", wstack
[--count
] );
470 TRACE_(relay
)( ") ss:sp=%04x:%04x\n", CURRENT_SS
, CURRENT_SP
);
471 SYSLEVEL_CheckNotLevel( 2 );
474 /* push return address */
475 stack
-= sizeof(SEGPTR
);
476 *((SEGPTR
*)stack
) = call16_ret_addr
;
477 cbArgs
+= sizeof(SEGPTR
);
480 * Actually, we should take care whether the called routine cleans up
481 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
482 * the callee to do so; after the routine has returned, the 16-bit
483 * stack pointer is always reset to the position it had before.
486 ret
= wine_call_to_16( (FARPROC16
)vpfn16
, cbArgs
, call16_handler
);
487 if (pdwRetCode
) *pdwRetCode
= ret
;
492 TRACE_(relay
)( "\1RetFrom16() ss:sp=%04x:%04x retval=%08x\n", CURRENT_SS
, CURRENT_SP
, ret
);
493 SYSLEVEL_CheckNotLevel( 2 );
497 return TRUE
; /* success */
500 /**********************************************************************
501 * K32WOWCallback16 (KERNEL32.54)
503 DWORD WINAPI
K32WOWCallback16( DWORD vpfn16
, DWORD dwParam
)
507 if ( !K32WOWCallback16Ex( vpfn16
, WCB16_PASCAL
,
508 sizeof(DWORD
), &dwParam
, &ret
) )