push 4d5485f9b89f417d46b39b93e8d940437007f325
[wine/hacks.git] / dlls / kernel32 / wowthunk.c
blob2ccfddff39cb2503396282e7197d2a60d2327a38
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 "thread.h"
35 #include "winternl.h"
36 #include "kernel_private.h"
37 #include "kernel16_private.h"
38 #include "wine/exception.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
42 WINE_DECLARE_DEBUG_CHANNEL(relay);
43 WINE_DECLARE_DEBUG_CHANNEL(snoop);
46 * These are the 16-bit side WOW routines. They reside in wownt16.h
47 * in the SDK; since we don't support Win16 source code anyway, I've
48 * placed them here for compilation with Wine ...
51 DWORD WINAPI GetVDMPointer32W16(SEGPTR,UINT16);
53 DWORD WINAPI LoadLibraryEx32W16(LPCSTR,DWORD,DWORD);
54 DWORD WINAPI GetProcAddress32W16(DWORD,LPCSTR);
55 DWORD WINAPI FreeLibrary32W16(DWORD);
57 #define CPEX_DEST_STDCALL 0x00000000L
58 #define CPEX_DEST_CDECL 0x80000000L
61 #ifdef __i386__
63 /* symbols exported from relay16.s */
64 extern DWORD WINAPI wine_call_to_16( FARPROC16 target, DWORD cbArgs, PEXCEPTION_HANDLER handler );
65 extern void WINAPI wine_call_to_16_regs( CONTEXT86 *context, DWORD cbArgs, PEXCEPTION_HANDLER handler );
66 extern void __wine_call_to_16_ret(void);
67 extern void CALL32_CBClient_Ret();
68 extern void CALL32_CBClientEx_Ret();
69 extern void DPMI_PendingEventCheck();
70 extern void DPMI_PendingEventCheck_Cleanup();
71 extern void DPMI_PendingEventCheck_Return();
72 extern BYTE __wine_call16_start[];
73 extern BYTE __wine_call16_end[];
75 extern void RELAY16_InitDebugLists(void);
77 static SEGPTR call16_ret_addr; /* segptr to __wine_call_to_16_ret routine */
79 static WORD dpmi_checker_selector;
80 static DWORD dpmi_checker_offset_call;
81 static DWORD dpmi_checker_offset_cleanup;
82 static DWORD dpmi_checker_offset_return;
84 /***********************************************************************
85 * WOWTHUNK_Init
87 BOOL WOWTHUNK_Init(void)
89 /* allocate the code selector for CallTo16 routines */
90 LDT_ENTRY entry;
91 WORD codesel = wine_ldt_alloc_entries(1);
93 if (!codesel) return FALSE;
94 wine_ldt_set_base( &entry, __wine_call16_start );
95 wine_ldt_set_limit( &entry, (BYTE *)(&CallTo16_TebSelector + 1) - __wine_call16_start - 1 );
96 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
97 wine_ldt_set_entry( codesel, &entry );
99 /* Patch the return addresses for CallTo16 routines */
101 CallTo16_DataSelector = wine_get_ds();
102 call16_ret_addr = MAKESEGPTR( codesel, (BYTE *)__wine_call_to_16_ret - __wine_call16_start );
103 CALL32_CBClient_RetAddr =
104 MAKESEGPTR( codesel, (BYTE *)CALL32_CBClient_Ret - __wine_call16_start );
105 CALL32_CBClientEx_RetAddr =
106 MAKESEGPTR( codesel, (BYTE *)CALL32_CBClientEx_Ret - __wine_call16_start );
108 /* Prepare selector and offsets for DPMI event checking. */
109 dpmi_checker_selector = codesel;
110 dpmi_checker_offset_call = (BYTE *)DPMI_PendingEventCheck - __wine_call16_start;
111 dpmi_checker_offset_cleanup = (BYTE *)DPMI_PendingEventCheck_Cleanup - __wine_call16_start;
112 dpmi_checker_offset_return = (BYTE *)DPMI_PendingEventCheck_Return - __wine_call16_start;
114 if (TRACE_ON(relay) || TRACE_ON(snoop)) RELAY16_InitDebugLists();
116 return TRUE;
120 /*************************************************************
121 * fix_selector
123 * Fix a selector load that caused an exception if it's in the
124 * 16-bit relay code.
126 static BOOL fix_selector( CONTEXT *context )
128 WORD *stack;
129 BYTE *instr = (BYTE *)context->Eip;
131 if (instr < __wine_call16_start || instr >= __wine_call16_end) return FALSE;
133 /* skip prefixes */
134 while (*instr == 0x66 || *instr == 0x67) instr++;
136 switch(instr[0])
138 case 0x07: /* pop es */
139 case 0x17: /* pop ss */
140 case 0x1f: /* pop ds */
141 break;
142 case 0x0f: /* extended instruction */
143 switch(instr[1])
145 case 0xa1: /* pop fs */
146 case 0xa9: /* pop gs */
147 break;
148 default:
149 return FALSE;
151 break;
152 default:
153 return FALSE;
155 stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
156 TRACE( "fixing up selector %x for pop instruction\n", *stack );
157 *stack = 0;
158 return TRUE;
162 /*************************************************************
163 * insert_event_check
165 * Make resuming the context check for pending DPMI events
166 * before the original context is restored. This is required
167 * because DPMI events are asynchronous, they are blocked while
168 * Wine 32-bit code is being executed and we want to prevent
169 * a race when returning back to 16-bit or 32-bit DPMI context.
171 static void insert_event_check( CONTEXT *context )
173 char *stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
175 /* don't do event check while in system code */
176 if (wine_ldt_is_system(context->SegCs))
177 return;
179 if(context->SegCs == dpmi_checker_selector &&
180 context->Eip >= dpmi_checker_offset_call &&
181 context->Eip <= dpmi_checker_offset_cleanup)
184 * Nested call. Stack will be preserved.
187 else if(context->SegCs == dpmi_checker_selector &&
188 context->Eip == dpmi_checker_offset_return)
191 * Nested call. We have just finished popping the fs
192 * register, lets put it back into stack.
195 stack -= sizeof(WORD);
196 *(WORD*)stack = context->SegFs;
198 context->Esp -= 2;
200 else
203 * Call is not nested.
204 * Push modified registers into stack.
205 * These will be popped by the assembler stub.
208 stack -= sizeof(DWORD);
209 *(DWORD*)stack = context->EFlags;
211 stack -= sizeof(DWORD);
212 *(DWORD*)stack = context->SegCs;
214 stack -= sizeof(DWORD);
215 *(DWORD*)stack = context->Eip;
217 stack -= sizeof(WORD);
218 *(WORD*)stack = context->SegFs;
220 context->Esp -= 14;
224 * Modify the context so that we jump into assembler stub.
225 * TEB access is made easier by providing the stub
226 * with the correct fs register value.
229 context->SegCs = dpmi_checker_selector;
230 context->Eip = dpmi_checker_offset_call;
231 context->SegFs = wine_get_fs();
235 /*************************************************************
236 * call16_handler
238 * Handler for exceptions occurring in 16-bit code.
240 static DWORD call16_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
241 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
243 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
245 /* unwinding: restore the stack pointer in the TEB, and leave the Win16 mutex */
246 STACK32FRAME *frame32 = (STACK32FRAME *)((char *)frame - offsetof(STACK32FRAME,frame));
247 NtCurrentTeb()->WOW32Reserved = (void *)frame32->frame16;
248 _LeaveWin16Lock();
250 else if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
251 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
253 if (wine_ldt_is_system(context->SegCs))
255 if (fix_selector( context )) return ExceptionContinueExecution;
257 else
259 SEGPTR gpHandler;
260 DWORD ret = __wine_emulate_instruction( record, context );
263 * Insert check for pending DPMI events. Note that this
264 * check must be inserted after instructions have been
265 * emulated because the instruction emulation requires
266 * original CS:IP and the emulation may change TEB.dpmi_vif.
268 if(NtCurrentTeb()->dpmi_vif)
269 insert_event_check( context );
271 if (ret != ExceptionContinueSearch) return ret;
273 /* check for Win16 __GP handler */
274 if ((gpHandler = HasGPHandler16( MAKESEGPTR( context->SegCs, context->Eip ) )))
276 WORD *stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
277 *--stack = context->SegCs;
278 *--stack = context->Eip;
280 if (!IS_SELECTOR_32BIT(context->SegSs))
281 context->Esp = MAKELONG( LOWORD(context->Esp - 2*sizeof(WORD)),
282 HIWORD(context->Esp) );
283 else
284 context->Esp -= 2*sizeof(WORD);
286 context->SegCs = SELECTOROF( gpHandler );
287 context->Eip = OFFSETOF( gpHandler );
288 return ExceptionContinueExecution;
292 else if (record->ExceptionCode == EXCEPTION_VM86_STI)
294 insert_event_check( context );
296 return ExceptionContinueSearch;
300 /*************************************************************
301 * vm86_handler
303 * Handler for exceptions occurring in vm86 code.
305 static DWORD vm86_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
306 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
308 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
309 return ExceptionContinueSearch;
311 if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
312 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
314 return __wine_emulate_instruction( record, context );
317 return ExceptionContinueSearch;
321 #else /* __i386__ */
323 BOOL WOWTHUNK_Init(void)
325 return TRUE;
328 #endif /* __i386__ */
332 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
335 /**********************************************************************
336 * K32WOWGetDescriptor (KERNEL32.70)
338 BOOL WINAPI K32WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
340 return GetThreadSelectorEntry( GetCurrentThread(),
341 segptr >> 16, ldtent );
344 /**********************************************************************
345 * K32WOWGetVDMPointer (KERNEL32.56)
347 LPVOID WINAPI K32WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
349 /* FIXME: add size check too */
351 if ( fProtectedMode )
352 return MapSL( vp );
353 else
354 return DOSMEM_MapRealToLinear( vp );
357 /**********************************************************************
358 * K32WOWGetVDMPointerFix (KERNEL32.68)
360 LPVOID WINAPI K32WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
363 * Hmmm. According to the docu, we should call:
365 * GlobalFix16( SELECTOROF(vp) );
367 * But this is unnecessary under Wine, as we never move global
368 * memory segments in linear memory anyway.
370 * (I'm not so sure what we are *supposed* to do if
371 * fProtectedMode is TRUE, anyway ...)
374 return K32WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
377 /**********************************************************************
378 * K32WOWGetVDMPointerUnfix (KERNEL32.69)
380 VOID WINAPI K32WOWGetVDMPointerUnfix( DWORD vp )
383 * See above why we don't call:
385 * GlobalUnfix16( SELECTOROF(vp) );
390 /**********************************************************************
391 * K32WOWGlobalAlloc16 (KERNEL32.59)
393 WORD WINAPI K32WOWGlobalAlloc16( WORD wFlags, DWORD cb )
395 return (WORD)GlobalAlloc16( wFlags, cb );
398 /**********************************************************************
399 * K32WOWGlobalFree16 (KERNEL32.62)
401 WORD WINAPI K32WOWGlobalFree16( WORD hMem )
403 return (WORD)GlobalFree16( (HGLOBAL16)hMem );
406 /**********************************************************************
407 * K32WOWGlobalUnlock16 (KERNEL32.61)
409 BOOL WINAPI K32WOWGlobalUnlock16( WORD hMem )
411 return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
414 /**********************************************************************
415 * K32WOWGlobalAllocLock16 (KERNEL32.63)
417 DWORD WINAPI K32WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
419 WORD hMem = K32WOWGlobalAlloc16( wFlags, cb );
420 if (phMem) *phMem = hMem;
422 return K32WOWGlobalLock16( hMem );
425 /**********************************************************************
426 * K32WOWGlobalLockSize16 (KERNEL32.65)
428 DWORD WINAPI K32WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
430 if ( pcb )
431 *pcb = GlobalSize16( (HGLOBAL16)hMem );
433 return K32WOWGlobalLock16( hMem );
436 /**********************************************************************
437 * K32WOWGlobalUnlockFree16 (KERNEL32.64)
439 WORD WINAPI K32WOWGlobalUnlockFree16( DWORD vpMem )
441 if ( !K32WOWGlobalUnlock16( HIWORD(vpMem) ) )
442 return FALSE;
444 return K32WOWGlobalFree16( HIWORD(vpMem) );
448 /**********************************************************************
449 * K32WOWYield16 (KERNEL32.66)
451 VOID WINAPI K32WOWYield16( void )
454 * This does the right thing for both Win16 and Win32 tasks.
455 * More or less, at least :-/
457 Yield16();
460 /**********************************************************************
461 * K32WOWDirectedYield16 (KERNEL32.67)
463 VOID WINAPI K32WOWDirectedYield16( WORD htask16 )
466 * Argh. Our scheduler doesn't like DirectedYield by Win32
467 * tasks at all. So we do hope that this routine is indeed
468 * only ever called by Win16 tasks that have thunked up ...
470 DirectedYield16( (HTASK16)htask16 );
474 /***********************************************************************
475 * K32WOWHandle32 (KERNEL32.57)
477 HANDLE WINAPI K32WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
479 switch ( type )
481 case WOW_TYPE_HWND:
482 case WOW_TYPE_HMENU:
483 case WOW_TYPE_HDWP:
484 case WOW_TYPE_HDROP:
485 case WOW_TYPE_HDC:
486 case WOW_TYPE_HFONT:
487 case WOW_TYPE_HRGN:
488 case WOW_TYPE_HBITMAP:
489 case WOW_TYPE_HBRUSH:
490 case WOW_TYPE_HPALETTE:
491 case WOW_TYPE_HPEN:
492 case WOW_TYPE_HACCEL:
493 return (HANDLE)(ULONG_PTR)handle;
495 case WOW_TYPE_HMETAFILE:
496 FIXME( "conversion of metafile handles not supported yet\n" );
497 return (HANDLE)(ULONG_PTR)handle;
499 case WOW_TYPE_HTASK:
500 return ((TDB *)GlobalLock16(handle))->teb->ClientId.UniqueThread;
502 case WOW_TYPE_FULLHWND:
503 FIXME( "conversion of full window handles not supported yet\n" );
504 return (HANDLE)(ULONG_PTR)handle;
506 default:
507 ERR( "handle 0x%04x of unknown type %d\n", handle, type );
508 return (HANDLE)(ULONG_PTR)handle;
512 /***********************************************************************
513 * K32WOWHandle16 (KERNEL32.58)
515 WORD WINAPI K32WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
517 switch ( type )
519 case WOW_TYPE_HWND:
520 case WOW_TYPE_HMENU:
521 case WOW_TYPE_HDWP:
522 case WOW_TYPE_HDROP:
523 case WOW_TYPE_HDC:
524 case WOW_TYPE_HFONT:
525 case WOW_TYPE_HRGN:
526 case WOW_TYPE_HBITMAP:
527 case WOW_TYPE_HBRUSH:
528 case WOW_TYPE_HPALETTE:
529 case WOW_TYPE_HPEN:
530 case WOW_TYPE_HACCEL:
531 case WOW_TYPE_FULLHWND:
532 if ( HIWORD(handle ) )
533 ERR( "handle %p of type %d has non-zero HIWORD\n", handle, type );
534 return LOWORD(handle);
536 case WOW_TYPE_HMETAFILE:
537 FIXME( "conversion of metafile handles not supported yet\n" );
538 return LOWORD(handle);
540 case WOW_TYPE_HTASK:
541 return TASK_GetTaskFromThread( (DWORD)handle );
543 default:
544 ERR( "handle %p of unknown type %d\n", handle, type );
545 return LOWORD(handle);
549 /**********************************************************************
550 * K32WOWCallback16Ex (KERNEL32.55)
552 BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
553 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
555 #ifdef __i386__
557 * Arguments must be prepared in the correct order by the caller
558 * (both for PASCAL and CDECL calling convention), so we simply
559 * copy them to the 16-bit stack ...
561 char *stack = (char *)CURRENT_STACK16 - cbArgs;
563 memcpy( stack, pArgs, cbArgs );
565 if (dwFlags & (WCB16_REGS|WCB16_REGS_LONG))
567 CONTEXT *context = (CONTEXT *)pdwRetCode;
569 if (TRACE_ON(relay))
571 DWORD count = cbArgs / sizeof(WORD);
572 WORD * wstack = (WORD *)stack;
574 DPRINTF("%04x:CallTo16(func=%04x:%04x,ds=%04x",
575 GetCurrentThreadId(),
576 context->SegCs, LOWORD(context->Eip), context->SegDs );
577 while (count) DPRINTF( ",%04x", wstack[--count] );
578 DPRINTF(") ss:sp=%04x:%04x",
579 SELECTOROF(NtCurrentTeb()->WOW32Reserved), OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
580 DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x bp=%04x es=%04x fs=%04x\n",
581 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
582 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
583 (WORD)context->Ebp, (WORD)context->SegEs, (WORD)context->SegFs );
584 SYSLEVEL_CheckNotLevel( 2 );
587 if (context->EFlags & 0x00020000) /* v86 mode */
589 EXCEPTION_REGISTRATION_RECORD frame;
590 frame.Handler = vm86_handler;
591 errno = 0;
592 __wine_push_frame( &frame );
593 __wine_enter_vm86( context );
594 __wine_pop_frame( &frame );
595 if (errno != 0) /* enter_vm86 will fall with ENOSYS on x64 kernels */
597 ERR("__wine_enter_vm86 failed (errno=%d)\n", errno);
598 if (errno == ENOSYS)
599 SetLastError(ERROR_NOT_SUPPORTED);
600 else
601 SetLastError(ERROR_GEN_FAILURE);
602 return FALSE;
605 else
607 /* push return address */
608 if (dwFlags & WCB16_REGS_LONG)
610 stack -= sizeof(DWORD);
611 *((DWORD *)stack) = HIWORD(call16_ret_addr);
612 stack -= sizeof(DWORD);
613 *((DWORD *)stack) = LOWORD(call16_ret_addr);
614 cbArgs += 2 * sizeof(DWORD);
616 else
618 stack -= sizeof(SEGPTR);
619 *((SEGPTR *)stack) = call16_ret_addr;
620 cbArgs += sizeof(SEGPTR);
624 * Start call by checking for pending events.
625 * Note that wine_call_to_16_regs overwrites context stack
626 * pointer so we may modify it here without a problem.
628 if (NtCurrentTeb()->dpmi_vif)
630 context->SegSs = wine_get_ds();
631 context->Esp = (DWORD)stack;
632 insert_event_check( context );
633 cbArgs += (DWORD)stack - context->Esp;
636 _EnterWin16Lock();
637 wine_call_to_16_regs( context, cbArgs, call16_handler );
638 _LeaveWin16Lock();
641 if (TRACE_ON(relay))
643 DPRINTF("%04x:RetFrom16() ss:sp=%04x:%04x ",
644 GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->WOW32Reserved),
645 OFFSETOF(NtCurrentTeb()->WOW32Reserved));
646 DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x bp=%04x sp=%04x\n",
647 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
648 (WORD)context->Edx, (WORD)context->Ebp, (WORD)context->Esp );
649 SYSLEVEL_CheckNotLevel( 2 );
652 else
654 DWORD ret;
656 if (TRACE_ON(relay))
658 DWORD count = cbArgs / sizeof(WORD);
659 WORD * wstack = (WORD *)stack;
661 DPRINTF("%04x:CallTo16(func=%04x:%04x,ds=%04x",
662 GetCurrentThreadId(), HIWORD(vpfn16), LOWORD(vpfn16),
663 SELECTOROF(NtCurrentTeb()->WOW32Reserved) );
664 while (count) DPRINTF( ",%04x", wstack[--count] );
665 DPRINTF(") ss:sp=%04x:%04x\n",
666 SELECTOROF(NtCurrentTeb()->WOW32Reserved), OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
667 SYSLEVEL_CheckNotLevel( 2 );
670 /* push return address */
671 stack -= sizeof(SEGPTR);
672 *((SEGPTR *)stack) = call16_ret_addr;
673 cbArgs += sizeof(SEGPTR);
676 * Actually, we should take care whether the called routine cleans up
677 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
678 * the callee to do so; after the routine has returned, the 16-bit
679 * stack pointer is always reset to the position it had before.
681 _EnterWin16Lock();
682 ret = wine_call_to_16( (FARPROC16)vpfn16, cbArgs, call16_handler );
683 if (pdwRetCode) *pdwRetCode = ret;
684 _LeaveWin16Lock();
686 if (TRACE_ON(relay))
688 DPRINTF("%04x:RetFrom16() ss:sp=%04x:%04x retval=%08x\n",
689 GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->WOW32Reserved),
690 OFFSETOF(NtCurrentTeb()->WOW32Reserved), ret);
691 SYSLEVEL_CheckNotLevel( 2 );
694 #else
695 assert(0); /* cannot call to 16-bit on non-Intel architectures */
696 #endif /* __i386__ */
698 return TRUE; /* success */
701 /**********************************************************************
702 * K32WOWCallback16 (KERNEL32.54)
704 DWORD WINAPI K32WOWCallback16( DWORD vpfn16, DWORD dwParam )
706 DWORD ret;
708 if ( !K32WOWCallback16Ex( vpfn16, WCB16_PASCAL,
709 sizeof(DWORD), &dwParam, &ret ) )
710 ret = 0L;
712 return ret;
717 * 16-bit WOW routines (in KERNEL)
720 /**********************************************************************
721 * GetVDMPointer32W (KERNEL.516)
723 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
725 GlobalPageLock16(GlobalHandle16(SELECTOROF(vp)));
726 return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
729 /***********************************************************************
730 * LoadLibraryEx32W (KERNEL.513)
732 DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
734 HMODULE hModule;
735 DWORD mutex_count;
736 OFSTRUCT ofs;
737 const char *p;
739 if (!lpszLibFile)
741 SetLastError(ERROR_INVALID_PARAMETER);
742 return 0;
745 /* if the file cannot be found, call LoadLibraryExA anyway, since it might be
746 a builtin module. This case is handled in MODULE_LoadLibraryExA */
748 if ((p = strrchr( lpszLibFile, '.' )) && !strchr( p, '\\' )) /* got an extension */
750 if (OpenFile16( lpszLibFile, &ofs, OF_EXIST ) != HFILE_ERROR16)
751 lpszLibFile = ofs.szPathName;
753 else
755 char buffer[MAX_PATH+4];
756 strcpy( buffer, lpszLibFile );
757 strcat( buffer, ".dll" );
758 if (OpenFile16( buffer, &ofs, OF_EXIST ) != HFILE_ERROR16)
759 lpszLibFile = ofs.szPathName;
762 ReleaseThunkLock( &mutex_count );
763 hModule = LoadLibraryExA( lpszLibFile, (HANDLE)hFile, dwFlags );
764 RestoreThunkLock( mutex_count );
766 return (DWORD)hModule;
769 /***********************************************************************
770 * GetProcAddress32W (KERNEL.515)
772 DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
774 return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
777 /***********************************************************************
778 * FreeLibrary32W (KERNEL.514)
780 DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
782 BOOL retv;
783 DWORD mutex_count;
785 ReleaseThunkLock( &mutex_count );
786 retv = FreeLibrary( (HMODULE)hLibModule );
787 RestoreThunkLock( mutex_count );
788 return (DWORD)retv;
792 /**********************************************************************
793 * WOW_CallProc32W
795 static DWORD WOW_CallProc32W16( FARPROC proc32, DWORD nrofargs, DWORD *args )
797 DWORD ret;
798 DWORD mutex_count;
800 ReleaseThunkLock( &mutex_count );
803 * FIXME: If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
804 * 32-bit CDECL routine ...
807 if (!proc32) ret = 0;
808 else switch (nrofargs)
810 case 0: ret = proc32();
811 break;
812 case 1: ret = proc32(args[0]);
813 break;
814 case 2: ret = proc32(args[0],args[1]);
815 break;
816 case 3: ret = proc32(args[0],args[1],args[2]);
817 break;
818 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
819 break;
820 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
821 break;
822 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
823 break;
824 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
825 break;
826 case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
827 break;
828 case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
829 break;
830 case 10:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
831 break;
832 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]);
833 break;
834 case 12:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11]);
835 break;
836 case 13:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12]);
837 break;
838 case 14:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12],args[13]);
839 break;
840 case 15:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11],args[12],args[13],args[14]);
841 break;
842 default:
843 /* FIXME: should go up to 32 arguments */
844 ERR("Unsupported number of arguments %d, please report.\n",nrofargs);
845 ret = 0;
846 break;
849 RestoreThunkLock( mutex_count );
851 TRACE("returns %08x\n",ret);
852 return ret;
855 /**********************************************************************
856 * CallProc32W (KERNEL.517)
858 DWORD WINAPIV CallProc32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
860 DWORD args[32];
861 unsigned int i;
863 TRACE("(%d,%d,%p args[",nrofargs,argconvmask,proc32);
865 for (i=0;i<nrofargs;i++)
867 if (argconvmask & (1<<i))
869 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
870 /* pascal convention, have to reverse the arguments order */
871 args[nrofargs - i - 1] = (DWORD)MapSL(ptr);
872 TRACE("%08x(%p),",ptr,MapSL(ptr));
874 else
876 DWORD arg = VA_ARG16( valist, DWORD );
877 /* pascal convention, have to reverse the arguments order */
878 args[nrofargs - i - 1] = arg;
879 TRACE("%d,", arg);
882 TRACE("])\n");
884 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
885 stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
887 return WOW_CallProc32W16( proc32, nrofargs, args );
890 /**********************************************************************
891 * _CallProcEx32W (KERNEL.518)
893 DWORD WINAPIV CallProcEx32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
895 DWORD args[32];
896 unsigned int i;
898 TRACE("(%d,%d,%p args[",nrofargs,argconvmask,proc32);
900 for (i=0;i<nrofargs;i++)
902 if (argconvmask & (1<<i))
904 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
905 args[i] = (DWORD)MapSL(ptr);
906 TRACE("%08x(%p),",ptr,MapSL(ptr));
908 else
910 DWORD arg = VA_ARG16( valist, DWORD );
911 args[i] = arg;
912 TRACE("%d,", arg);
915 TRACE("])\n");
916 return WOW_CallProc32W16( proc32, nrofargs, args );
920 /**********************************************************************
921 * WOW16Call (KERNEL.500)
923 * FIXME!!!
926 DWORD WINAPIV WOW16Call(WORD x, WORD y, WORD z, VA_LIST16 args)
928 int i;
929 DWORD calladdr;
930 FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
932 for (i=0;i<x/2;i++) {
933 WORD a = VA_ARG16(args,WORD);
934 DPRINTF("%04x ",a);
936 calladdr = VA_ARG16(args,DWORD);
937 stack16_pop( 3*sizeof(WORD) + x + sizeof(DWORD) );
938 DPRINTF(") calling address was 0x%08x\n",calladdr);
939 return 0;