2 * Win32 WOW Generic Thunk API
4 * Copyright 1999 Ulrich Weigand
7 #include "wine/winbase16.h"
13 #include "stackframe.h"
14 #include "builtin16.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(thunk
)
20 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
23 /**********************************************************************
24 * WOWGetDescriptor (WOW32.1) (KERNEL32.70)
26 BOOL WINAPI
WOWGetDescriptor( SEGPTR segptr
, LPLDT_ENTRY ldtent
)
28 return GetThreadSelectorEntry( GetCurrentThreadId(),
29 segptr
>> 16, ldtent
);
32 /**********************************************************************
33 * WOWGetVDMPointer (WOW32.5) (KERNEL32.56)
35 LPVOID WINAPI
WOWGetVDMPointer( DWORD vp
, DWORD dwBytes
, BOOL fProtectedMode
)
37 /* FIXME: add size check too */
40 return PTR_SEG_TO_LIN( vp
);
42 return DOSMEM_MapRealToLinear( vp
);
45 /**********************************************************************
46 * WOWGetVDMPointerFix (WOW32.6) (KERNEL32.68)
48 LPVOID WINAPI
WOWGetVDMPointerFix( DWORD vp
, DWORD dwBytes
, BOOL fProtectedMode
)
51 * Hmmm. According to the docu, we should call:
53 * GlobalFix16( SELECTOROF(vp) );
55 * But this is unnecessary under Wine, as we never move global
56 * memory segments in linear memory anyway.
58 * (I'm not so sure what we are *supposed* to do if
59 * fProtectedMode is TRUE, anyway ...)
62 return WOWGetVDMPointer( vp
, dwBytes
, fProtectedMode
);
65 /**********************************************************************
66 * WOWGetVDMPointerUnFix (WOW32.7) (KERNEL32.69)
68 VOID WINAPI
WOWGetVDMPointerUnfix( DWORD vp
)
71 * See above why we don't call:
73 * GlobalUnfix16( SELECTOROF(vp) );
78 /**********************************************************************
79 * WOWGlobalAlloc16 (WOW32.8) (KERNEL32.59)
81 WORD WINAPI
WOWGlobalAlloc16( WORD wFlags
, DWORD cb
)
83 return (WORD
)GlobalAlloc16( wFlags
, cb
);
86 /**********************************************************************
87 * WOWGlobalFree16 (WOW32.10) (KERNEL32.62)
89 WORD WINAPI
WOWGlobalFree16( WORD hMem
)
91 return (WORD
)GlobalFree16( (HGLOBAL16
)hMem
);
94 /**********************************************************************
95 * WOWGlobalLock16 (WOW32.11) (KERNEL32.60)
97 DWORD WINAPI
WOWGlobalLock16( WORD hMem
)
99 return (DWORD
)WIN16_GlobalLock16( (HGLOBAL16
)hMem
);
102 /**********************************************************************
103 * WOWGlobalUnlock16 (WOW32.13) (KERNEL32.61)
105 BOOL WINAPI
WOWGlobalUnlock16( WORD hMem
)
107 return (BOOL
)GlobalUnlock16( (HGLOBAL16
)hMem
);
110 /**********************************************************************
111 * WOWGlobalAllocLock16 (WOW32.9) (KERNEL32.63)
113 DWORD WINAPI
WOWGlobalAllocLock16( WORD wFlags
, DWORD cb
, WORD
*phMem
)
115 WORD hMem
= WOWGlobalAlloc16( wFlags
, cb
);
116 if (phMem
) *phMem
= hMem
;
118 return WOWGlobalLock16( hMem
);
121 /**********************************************************************
122 * WOWGlobalLockSize16 (WOW32.12) (KERNEL32.65)
124 DWORD WINAPI
WOWGlobalLockSize16( WORD hMem
, PDWORD pcb
)
127 *pcb
= GlobalSize16( (HGLOBAL16
)hMem
);
129 return WOWGlobalLock16( hMem
);
132 /**********************************************************************
133 * WOWGlobalUnlockFree16 (WOW32.14) (KERNEL32.64)
135 WORD WINAPI
WOWGlobalUnlockFree16( DWORD vpMem
)
137 if ( !WOWGlobalUnlock16( HIWORD(vpMem
) ) )
140 return WOWGlobalFree16( HIWORD(vpMem
) );
144 /**********************************************************************
145 * WOWYield16 (WOW32.17) (KERNEL32.66)
147 VOID WINAPI
WOWYield16( void )
150 * This does the right thing for both Win16 and Win32 tasks.
151 * More or less, at least :-/
156 /**********************************************************************
157 * WOWDirectedYield16 (WOW32.4) (KERNEL32.67)
159 VOID WINAPI
WOWDirectedYield16( WORD htask16
)
162 * Argh. Our scheduler doesn't like DirectedYield by Win32
163 * tasks at all. So we do hope that this routine is indeed
164 * only ever called by Win16 tasks that have thunked up ...
166 DirectedYield16( (HTASK16
)htask16
);
170 /***********************************************************************
171 * WOWHandle32 (WOW32.16) (KERNEL32.57)
173 HANDLE WINAPI
WOWHandle32( WORD handle
, WOW_HANDLE_TYPE type
)
183 case WOW_TYPE_HMETAFILE
:
185 case WOW_TYPE_HBITMAP
:
186 case WOW_TYPE_HBRUSH
:
187 case WOW_TYPE_HPALETTE
:
189 case WOW_TYPE_HACCEL
:
191 case WOW_TYPE_FULLHWND
:
192 return (HANDLE
)handle
;
195 ERR( "handle 0x%04x of unknown type %d\n", handle
, type
);
196 return (HANDLE
)handle
;
200 /***********************************************************************
201 * WOWHandle16 (WOW32.15) (KERNEL32.58)
203 WORD WINAPI
WOWHandle16( HANDLE handle
, WOW_HANDLE_TYPE type
)
205 if ( HIWORD(handle
) )
206 ERR( "handle 0x%08x of type %d has non-zero HIWORD\n", handle
, type
);
216 case WOW_TYPE_HMETAFILE
:
218 case WOW_TYPE_HBITMAP
:
219 case WOW_TYPE_HBRUSH
:
220 case WOW_TYPE_HPALETTE
:
222 case WOW_TYPE_HACCEL
:
224 case WOW_TYPE_FULLHWND
:
225 return LOWORD(handle
);
228 ERR( "handle 0x%08x of unknown type %d\n", handle
, type
);
229 return LOWORD(handle
);
233 /**********************************************************************
234 * WOWCallback16 (WOW32.2) (KERNEL32.54)
236 DWORD WINAPI
WOWCallback16( DWORD vpfn16
, DWORD dwParam
)
240 if ( !WOWCallback16Ex( vpfn16
, WCB16_PASCAL
,
241 sizeof(DWORD
), &dwParam
, &ret
) )
247 /**********************************************************************
248 * WOWCallback16Ex (WOW32.3) (KERNEL32.55)
250 BOOL WINAPI
WOWCallback16Ex( DWORD vpfn16
, DWORD dwFlags
,
251 DWORD cbArgs
, LPVOID pArgs
, LPDWORD pdwRetCode
)
256 * Arguments must be prepared in the correct order by the caller
257 * (both for PASCAL and CDECL calling convention), so we simply
258 * copy them to the 16-bit stack ...
260 memcpy( (LPBYTE
)CURRENT_STACK16
- cbArgs
, (LPBYTE
)pArgs
, cbArgs
);
264 * Actually, we should take care whether the called routine cleans up
265 * its stack or not. Fortunately, our CallTo16 core doesn't rely on
266 * the callee to do so; after the routine has returned, the 16-bit
267 * stack pointer is always reset to the position it had before.
270 ret
= CallTo16Long( (FARPROC16
)vpfn16
, cbArgs
);
275 return TRUE
; /* success */
281 * 16-bit WOW routines (in KERNEL)
284 /**********************************************************************
285 * GetVDMPointer32W16 (KERNEL.516)
287 DWORD WINAPI
GetVDMPointer32W16( SEGPTR vp
, UINT16 fMode
)
289 return (DWORD
)WOWGetVDMPointer( vp
, 0, (DWORD
)fMode
);
292 /***********************************************************************
293 * LoadLibraryEx32W16 (KERNEL.513)
295 DWORD WINAPI
LoadLibraryEx32W16( LPCSTR lpszLibFile
, DWORD hFile
, DWORD dwFlags
)
299 SYSLEVEL_ReleaseWin16Lock();
300 hModule
= LoadLibraryExA( lpszLibFile
, (HANDLE
)hFile
, dwFlags
);
301 SYSLEVEL_RestoreWin16Lock();
303 return (DWORD
)hModule
;
306 /***********************************************************************
307 * GetProcAddress32W16 (KERNEL.515)
309 DWORD WINAPI
GetProcAddress32W16( DWORD hModule
, LPCSTR lpszProc
)
311 return (DWORD
)GetProcAddress( (HMODULE
)hModule
, lpszProc
);
314 /***********************************************************************
315 * FreeLibrary32W16 (KERNEL.514)
317 DWORD WINAPI
FreeLibrary32W16( DWORD hLibModule
)
321 SYSLEVEL_ReleaseWin16Lock();
322 retv
= FreeLibrary( (HMODULE
)hLibModule
);
323 SYSLEVEL_RestoreWin16Lock();
329 /**********************************************************************
332 static DWORD
WOW_CallProc32W16( BOOL Ex
)
334 DWORD nrofargs
, argconvmask
;
341 SYSLEVEL_ReleaseWin16Lock();
343 VA_START16( valist
);
344 nrofargs
= VA_ARG16( valist
, DWORD
);
345 argconvmask
= VA_ARG16( valist
, DWORD
);
346 proc32
= VA_ARG16( valist
, FARPROC
);
347 TRACE("(%ld,%ld,%p, Ex%d args[",nrofargs
,argconvmask
,proc32
,Ex
);
348 args
= (DWORD
*)HEAP_xalloc( GetProcessHeap(), 0,
349 sizeof(DWORD
)*nrofargs
);
350 /* CallProcEx doesn't need its args reversed */
351 for (i
=0;i
<nrofargs
;i
++) {
355 aix
= nrofargs
- i
- 1;
357 if (argconvmask
& (1<<i
))
359 SEGPTR ptr
= VA_ARG16( valist
, SEGPTR
);
360 args
[aix
] = (DWORD
)PTR_SEG_TO_LIN(ptr
);
361 if (TRACE_ON(thunk
)) DPRINTF("%08lx(%p),",ptr
,PTR_SEG_TO_LIN(ptr
));
365 args
[aix
] = VA_ARG16( valist
, DWORD
);
366 if (TRACE_ON(thunk
)) DPRINTF("%ld,",args
[aix
]);
369 if (TRACE_ON(thunk
)) DPRINTF("])\n");
373 * FIXME: If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
374 * 32-bit CDECL routine ...
377 if (!proc32
) ret
= 0;
378 else switch (nrofargs
)
380 case 0: ret
= proc32();
382 case 1: ret
= proc32(args
[0]);
384 case 2: ret
= proc32(args
[0],args
[1]);
386 case 3: ret
= proc32(args
[0],args
[1],args
[2]);
388 case 4: ret
= proc32(args
[0],args
[1],args
[2],args
[3]);
390 case 5: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4]);
392 case 6: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5]);
394 case 7: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6]);
396 case 8: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7]);
398 case 9: ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8]);
400 case 10:ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]);
402 case 11:ret
= proc32(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],args
[10]);
405 /* FIXME: should go up to 32 arguments */
406 ERR("Unsupported number of arguments %ld, please report.\n",nrofargs
);
410 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
411 if (!Ex
) stack16_pop( (3 + nrofargs
) * sizeof(DWORD
) );
413 TRACE("returns %08lx\n",ret
);
414 HeapFree( GetProcessHeap(), 0, args
);
416 SYSLEVEL_RestoreWin16Lock();
421 /**********************************************************************
422 * CallProc32W16 (KERNEL.517)
424 * DWORD PASCAL CallProc32W( DWORD p1, ... , DWORD lpProcAddress,
425 * DWORD fAddressConvert, DWORD cParams );
427 DWORD WINAPI
CallProc32W16( void )
429 return WOW_CallProc32W16( FALSE
);
432 /**********************************************************************
433 * CallProcEx32W16 (KERNEL.518)
435 * DWORD CallProcEx32W( DWORD cParams, DWORD fAddressConvert,
436 * DWORD lpProcAddress, DWORD p1, ... );
438 DWORD WINAPI
CallProcEx32W16( void )
440 return WOW_CallProc32W16( TRUE
);
444 /**********************************************************************
445 * WOW16Call (KERNEL.501)
450 DWORD WINAPI
WOW16Call(WORD x
,WORD y
,WORD z
)
455 FIXME("(0x%04x,0x%04x,%d),calling (",x
,y
,z
);
458 for (i
=0;i
<x
/2;i
++) {
459 WORD a
= VA_ARG16(args
,WORD
);
462 calladdr
= VA_ARG16(args
,DWORD
);
464 stack16_pop( x
+ sizeof(DWORD
) );
465 DPRINTF(") calling address was 0x%08lx\n",calladdr
);