Restored emulation of protected instructions in 32-bit code when
[wine/wine64.git] / dlls / kernel / wowthunk.c
bloba95e8f84b57c522ca9bab45340c8f33fe9acadc4
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
27 #include "wine/winbase16.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "wownt32.h"
32 #include "excpt.h"
33 #include "winreg.h"
34 #include "winternl.h"
35 #include "file.h"
36 #include "task.h"
37 #include "miscemu.h"
38 #include "stackframe.h"
39 #include "kernel_private.h"
40 #include "wine/exception.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
44 WINE_DECLARE_DEBUG_CHANNEL(relay);
45 WINE_DECLARE_DEBUG_CHANNEL(snoop);
48 * These are the 16-bit side WOW routines. They reside in wownt16.h
49 * in the SDK; since we don't support Win16 source code anyway, I've
50 * placed them here for compilation with Wine ...
53 DWORD WINAPI GetVDMPointer32W16(SEGPTR,UINT16);
55 DWORD WINAPI LoadLibraryEx32W16(LPCSTR,DWORD,DWORD);
56 DWORD WINAPI GetProcAddress32W16(DWORD,LPCSTR);
57 DWORD WINAPI FreeLibrary32W16(DWORD);
59 #define CPEX_DEST_STDCALL 0x00000000L
60 #define CPEX_DEST_CDECL 0x80000000L
62 /* thunk for 16-bit CreateThread */
63 struct thread_args
65 FARPROC16 proc;
66 DWORD param;
69 static DWORD CALLBACK start_thread16( LPVOID threadArgs )
71 struct thread_args args = *(struct thread_args *)threadArgs;
72 HeapFree( GetProcessHeap(), 0, threadArgs );
73 return K32WOWCallback16( (DWORD)args.proc, args.param );
77 #ifdef __i386__
79 /* symbols exported from relay16.s */
80 extern DWORD WINAPI wine_call_to_16( FARPROC16 target, DWORD cbArgs, PEXCEPTION_HANDLER handler );
81 extern void WINAPI wine_call_to_16_regs( CONTEXT86 *context, DWORD cbArgs, PEXCEPTION_HANDLER handler );
82 extern void Call16_Ret_Start(), Call16_Ret_End();
83 extern void CallTo16_Ret();
84 extern void CALL32_CBClient_Ret();
85 extern void CALL32_CBClientEx_Ret();
86 extern DWORD CallTo16_DataSelector;
87 extern SEGPTR CALL32_CBClient_RetAddr;
88 extern SEGPTR CALL32_CBClientEx_RetAddr;
89 extern BYTE Call16_Start;
90 extern BYTE Call16_End;
92 extern void RELAY16_InitDebugLists(void);
94 static LONG CALLBACK vectored_handler( EXCEPTION_POINTERS *ptrs );
95 static SEGPTR call16_ret_addr; /* segptr to CallTo16_Ret routine */
97 /***********************************************************************
98 * WOWTHUNK_Init
100 BOOL WOWTHUNK_Init(void)
102 /* allocate the code selector for CallTo16 routines */
103 WORD codesel = SELECTOR_AllocBlock( (void *)Call16_Ret_Start,
104 (char *)Call16_Ret_End - (char *)Call16_Ret_Start,
105 WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
106 if (!codesel) return FALSE;
108 /* Patch the return addresses for CallTo16 routines */
110 CallTo16_DataSelector = wine_get_ds();
111 call16_ret_addr = MAKESEGPTR( codesel, (char*)CallTo16_Ret - (char*)Call16_Ret_Start );
112 CALL32_CBClient_RetAddr =
113 MAKESEGPTR( codesel, (char*)CALL32_CBClient_Ret - (char*)Call16_Ret_Start );
114 CALL32_CBClientEx_RetAddr =
115 MAKESEGPTR( codesel, (char*)CALL32_CBClientEx_Ret - (char*)Call16_Ret_Start );
117 if (TRACE_ON(relay) || TRACE_ON(snoop)) RELAY16_InitDebugLists();
119 /* setup emulation of protected instructions from 32-bit code (only for Win9x versions) */
120 if (GetVersion() & 0x80000000) RtlAddVectoredExceptionHandler( TRUE, vectored_handler );
121 return TRUE;
125 /*************************************************************
126 * fix_selector
128 * Fix a selector load that caused an exception if it's in the
129 * 16-bit relay code.
131 static BOOL fix_selector( CONTEXT *context )
133 WORD *stack;
134 BYTE *instr = (BYTE *)context->Eip;
136 if (instr < &Call16_Start || instr >= &Call16_End) return FALSE;
138 /* skip prefixes */
139 while (*instr == 0x66 || *instr == 0x67) instr++;
141 switch(instr[0])
143 case 0x07: /* pop es */
144 case 0x17: /* pop ss */
145 case 0x1f: /* pop ds */
146 break;
147 case 0x0f: /* extended instruction */
148 switch(instr[1])
150 case 0xa1: /* pop fs */
151 case 0xa9: /* pop gs */
152 break;
153 default:
154 return FALSE;
156 break;
157 default:
158 return FALSE;
160 stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
161 TRACE( "fixing up selector %x for pop instruction\n", *stack );
162 *stack = 0;
163 return TRUE;
167 /*************************************************************
168 * call16_handler
170 * Handler for exceptions occurring in 16-bit code.
172 static DWORD call16_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
173 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
175 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
177 /* unwinding: restore the stack pointer in the TEB, and leave the Win16 mutex */
178 STACK32FRAME *frame32 = (STACK32FRAME *)((char *)frame - offsetof(STACK32FRAME,frame));
179 NtCurrentTeb()->cur_stack = frame32->frame16;
180 _LeaveWin16Lock();
182 else if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
183 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
185 if (IS_SELECTOR_SYSTEM(context->SegCs))
187 if (fix_selector( context )) return ExceptionContinueExecution;
189 else
191 SEGPTR gpHandler;
192 DWORD ret = INSTR_EmulateInstruction( record, context );
194 if (ret != ExceptionContinueSearch) return ret;
196 /* check for Win16 __GP handler */
197 if ((gpHandler = HasGPHandler16( MAKESEGPTR( context->SegCs, context->Eip ) )))
199 WORD *stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
200 *--stack = context->SegCs;
201 *--stack = context->Eip;
203 if (!IS_SELECTOR_32BIT(context->SegSs))
204 context->Esp = MAKELONG( LOWORD(context->Esp - 2*sizeof(WORD)),
205 HIWORD(context->Esp) );
206 else
207 context->Esp -= 2*sizeof(WORD);
209 context->SegCs = SELECTOROF( gpHandler );
210 context->Eip = OFFSETOF( gpHandler );
211 return ExceptionContinueExecution;
215 return ExceptionContinueSearch;
219 /*************************************************************
220 * vm86_handler
222 * Handler for exceptions occurring in vm86 code.
224 static DWORD vm86_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
225 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
227 if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
228 return ExceptionContinueSearch;
230 if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
231 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
233 return INSTR_EmulateInstruction( record, context );
236 return ExceptionContinueSearch;
240 /***********************************************************************
241 * vectored_handler
243 * Vectored exception handler used to emulate protected instructions
244 * from 32-bit code.
246 static LONG CALLBACK vectored_handler( EXCEPTION_POINTERS *ptrs )
248 EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
249 CONTEXT *context = ptrs->ContextRecord;
251 if (IS_SELECTOR_SYSTEM(context->SegCs) &&
252 (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
253 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION))
255 if (INSTR_EmulateInstruction( record, context ) == ExceptionContinueExecution)
256 return EXCEPTION_CONTINUE_EXECUTION;
258 return EXCEPTION_CONTINUE_SEARCH;
262 #else /* __i386__ */
264 BOOL WOWTHUNK_Init(void)
266 return TRUE;
269 #endif /* __i386__ */
273 * 32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
276 /**********************************************************************
277 * K32WOWGetDescriptor (KERNEL32.70)
279 BOOL WINAPI K32WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
281 return GetThreadSelectorEntry( GetCurrentThread(),
282 segptr >> 16, ldtent );
285 /**********************************************************************
286 * K32WOWGetVDMPointer (KERNEL32.56)
288 LPVOID WINAPI K32WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
290 /* FIXME: add size check too */
292 if ( fProtectedMode )
293 return MapSL( vp );
294 else
295 return DOSMEM_MapRealToLinear( vp );
298 /**********************************************************************
299 * K32WOWGetVDMPointerFix (KERNEL32.68)
301 LPVOID WINAPI K32WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
304 * Hmmm. According to the docu, we should call:
306 * GlobalFix16( SELECTOROF(vp) );
308 * But this is unnecessary under Wine, as we never move global
309 * memory segments in linear memory anyway.
311 * (I'm not so sure what we are *supposed* to do if
312 * fProtectedMode is TRUE, anyway ...)
315 return K32WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
318 /**********************************************************************
319 * K32WOWGetVDMPointerUnfix (KERNEL32.69)
321 VOID WINAPI K32WOWGetVDMPointerUnfix( DWORD vp )
324 * See above why we don't call:
326 * GlobalUnfix16( SELECTOROF(vp) );
331 /**********************************************************************
332 * K32WOWGlobalAlloc16 (KERNEL32.59)
334 WORD WINAPI K32WOWGlobalAlloc16( WORD wFlags, DWORD cb )
336 return (WORD)GlobalAlloc16( wFlags, cb );
339 /**********************************************************************
340 * K32WOWGlobalFree16 (KERNEL32.62)
342 WORD WINAPI K32WOWGlobalFree16( WORD hMem )
344 return (WORD)GlobalFree16( (HGLOBAL16)hMem );
347 /**********************************************************************
348 * K32WOWGlobalUnlock16 (KERNEL32.61)
350 BOOL WINAPI K32WOWGlobalUnlock16( WORD hMem )
352 return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
355 /**********************************************************************
356 * K32WOWGlobalAllocLock16 (KERNEL32.63)
358 DWORD WINAPI K32WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
360 WORD hMem = K32WOWGlobalAlloc16( wFlags, cb );
361 if (phMem) *phMem = hMem;
363 return K32WOWGlobalLock16( hMem );
366 /**********************************************************************
367 * K32WOWGlobalLockSize16 (KERNEL32.65)
369 DWORD WINAPI K32WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
371 if ( pcb )
372 *pcb = GlobalSize16( (HGLOBAL16)hMem );
374 return K32WOWGlobalLock16( hMem );
377 /**********************************************************************
378 * K32WOWGlobalUnlockFree16 (KERNEL32.64)
380 WORD WINAPI K32WOWGlobalUnlockFree16( DWORD vpMem )
382 if ( !K32WOWGlobalUnlock16( HIWORD(vpMem) ) )
383 return FALSE;
385 return K32WOWGlobalFree16( HIWORD(vpMem) );
389 /**********************************************************************
390 * K32WOWYield16 (KERNEL32.66)
392 VOID WINAPI K32WOWYield16( void )
395 * This does the right thing for both Win16 and Win32 tasks.
396 * More or less, at least :-/
398 Yield16();
401 /**********************************************************************
402 * K32WOWDirectedYield16 (KERNEL32.67)
404 VOID WINAPI K32WOWDirectedYield16( WORD htask16 )
407 * Argh. Our scheduler doesn't like DirectedYield by Win32
408 * tasks at all. So we do hope that this routine is indeed
409 * only ever called by Win16 tasks that have thunked up ...
411 DirectedYield16( (HTASK16)htask16 );
415 /***********************************************************************
416 * K32WOWHandle32 (KERNEL32.57)
418 HANDLE WINAPI K32WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
420 switch ( type )
422 case WOW_TYPE_HWND:
423 case WOW_TYPE_HMENU:
424 case WOW_TYPE_HDWP:
425 case WOW_TYPE_HDROP:
426 case WOW_TYPE_HDC:
427 case WOW_TYPE_HFONT:
428 case WOW_TYPE_HRGN:
429 case WOW_TYPE_HBITMAP:
430 case WOW_TYPE_HBRUSH:
431 case WOW_TYPE_HPALETTE:
432 case WOW_TYPE_HPEN:
433 case WOW_TYPE_HACCEL:
434 return (HANDLE)(ULONG_PTR)handle;
436 case WOW_TYPE_HMETAFILE:
437 FIXME( "conversion of metafile handles not supported yet\n" );
438 return (HANDLE)(ULONG_PTR)handle;
440 case WOW_TYPE_HTASK:
441 return ((TDB *)GlobalLock16(handle))->teb->ClientId.UniqueThread;
443 case WOW_TYPE_FULLHWND:
444 FIXME( "conversion of full window handles not supported yet\n" );
445 return (HANDLE)(ULONG_PTR)handle;
447 default:
448 ERR( "handle 0x%04x of unknown type %d\n", handle, type );
449 return (HANDLE)(ULONG_PTR)handle;
453 /***********************************************************************
454 * K32WOWHandle16 (KERNEL32.58)
456 WORD WINAPI K32WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
458 switch ( type )
460 case WOW_TYPE_HWND:
461 case WOW_TYPE_HMENU:
462 case WOW_TYPE_HDWP:
463 case WOW_TYPE_HDROP:
464 case WOW_TYPE_HDC:
465 case WOW_TYPE_HFONT:
466 case WOW_TYPE_HRGN:
467 case WOW_TYPE_HBITMAP:
468 case WOW_TYPE_HBRUSH:
469 case WOW_TYPE_HPALETTE:
470 case WOW_TYPE_HPEN:
471 case WOW_TYPE_HACCEL:
472 case WOW_TYPE_FULLHWND:
473 if ( HIWORD(handle ) )
474 ERR( "handle %p of type %d has non-zero HIWORD\n", handle, type );
475 return LOWORD(handle);
477 case WOW_TYPE_HMETAFILE:
478 FIXME( "conversion of metafile handles not supported yet\n" );
479 return LOWORD(handle);
481 case WOW_TYPE_HTASK:
482 return TASK_GetTaskFromThread( (DWORD)handle );
484 default:
485 ERR( "handle %p of unknown type %d\n", handle, type );
486 return LOWORD(handle);
490 /**********************************************************************
491 * K32WOWCallback16Ex (KERNEL32.55)
493 BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
494 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
496 #ifdef __i386__
498 * Arguments must be prepared in the correct order by the caller
499 * (both for PASCAL and CDECL calling convention), so we simply
500 * copy them to the 16-bit stack ...
502 WORD *stack = (WORD *)CURRENT_STACK16 - cbArgs / sizeof(WORD);
504 memcpy( stack, pArgs, cbArgs );
506 if (dwFlags & (WCB16_REGS|WCB16_REGS_LONG))
508 CONTEXT *context = (CONTEXT *)pdwRetCode;
510 if (TRACE_ON(relay))
512 DWORD count = cbArgs / sizeof(WORD);
514 DPRINTF("%04lx:CallTo16(func=%04lx:%04x,ds=%04lx",
515 GetCurrentThreadId(),
516 context->SegCs, LOWORD(context->Eip), context->SegDs );
517 while (count) DPRINTF( ",%04x", stack[--count] );
518 DPRINTF(") ss:sp=%04x:%04x",
519 SELECTOROF(NtCurrentTeb()->cur_stack), OFFSETOF(NtCurrentTeb()->cur_stack) );
520 DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x bp=%04x es=%04x fs=%04x\n",
521 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
522 (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
523 (WORD)context->Ebp, (WORD)context->SegEs, (WORD)context->SegFs );
524 SYSLEVEL_CheckNotLevel( 2 );
527 if (ISV86(context))
529 EXCEPTION_REGISTRATION_RECORD frame;
530 frame.Handler = vm86_handler;
531 __wine_push_frame( &frame );
532 __wine_enter_vm86( context );
533 __wine_pop_frame( &frame );
535 else
537 /* push return address */
538 if (dwFlags & WCB16_REGS_LONG)
540 *((DWORD *)stack - 1) = HIWORD(call16_ret_addr);
541 *((DWORD *)stack - 2) = LOWORD(call16_ret_addr);
542 cbArgs += 2 * sizeof(DWORD);
544 else
546 *((SEGPTR *)stack - 1) = call16_ret_addr;
547 cbArgs += sizeof(SEGPTR);
550 _EnterWin16Lock();
551 wine_call_to_16_regs( context, cbArgs, call16_handler );
552 _LeaveWin16Lock();
555 if (TRACE_ON(relay))
557 DPRINTF("%04lx:RetFrom16() ss:sp=%04x:%04x ",
558 GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->cur_stack),
559 OFFSETOF(NtCurrentTeb()->cur_stack));
560 DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x bp=%04x sp=%04x\n",
561 (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
562 (WORD)context->Edx, (WORD)context->Ebp, (WORD)context->Esp );
563 SYSLEVEL_CheckNotLevel( 2 );
566 else
568 DWORD ret;
570 if (TRACE_ON(relay))
572 DWORD count = cbArgs / sizeof(WORD);
574 DPRINTF("%04lx:CallTo16(func=%04x:%04x,ds=%04x",
575 GetCurrentThreadId(), HIWORD(vpfn16), LOWORD(vpfn16),
576 SELECTOROF(NtCurrentTeb()->cur_stack) );
577 while (count) DPRINTF( ",%04x", stack[--count] );
578 DPRINTF(") ss:sp=%04x:%04x\n",
579 SELECTOROF(NtCurrentTeb()->cur_stack), OFFSETOF(NtCurrentTeb()->cur_stack) );
580 SYSLEVEL_CheckNotLevel( 2 );
583 /* push return address */
584 *((SEGPTR *)stack - 1) = call16_ret_addr;
585 cbArgs += sizeof(SEGPTR);
588 * Actually, we should take care whether the called routine cleans up
589 * its stack or not. Fortunately, our wine_call_to_16 core doesn't rely on
590 * the callee to do so; after the routine has returned, the 16-bit
591 * stack pointer is always reset to the position it had before.
593 _EnterWin16Lock();
594 ret = wine_call_to_16( (FARPROC16)vpfn16, cbArgs, call16_handler );
595 if (pdwRetCode) *pdwRetCode = ret;
596 _LeaveWin16Lock();
598 if (TRACE_ON(relay))
600 DPRINTF("%04lx:RetFrom16() ss:sp=%04x:%04x retval=%08lx\n",
601 GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->cur_stack),
602 OFFSETOF(NtCurrentTeb()->cur_stack), ret);
603 SYSLEVEL_CheckNotLevel( 2 );
606 #else
607 assert(0); /* cannot call to 16-bit on non-Intel architectures */
608 #endif /* __i386__ */
610 return TRUE; /* success */
613 /**********************************************************************
614 * K32WOWCallback16 (KERNEL32.54)
616 DWORD WINAPI K32WOWCallback16( DWORD vpfn16, DWORD dwParam )
618 DWORD ret;
620 if ( !K32WOWCallback16Ex( vpfn16, WCB16_PASCAL,
621 sizeof(DWORD), &dwParam, &ret ) )
622 ret = 0L;
624 return ret;
629 * 16-bit WOW routines (in KERNEL)
632 /**********************************************************************
633 * GetVDMPointer32W (KERNEL.516)
635 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
637 GlobalPageLock16(GlobalHandle16(SELECTOROF(vp)));
638 return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
641 /***********************************************************************
642 * LoadLibraryEx32W (KERNEL.513)
644 DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
646 HMODULE hModule;
647 DOS_FULL_NAME full_name;
648 DWORD mutex_count;
649 UNICODE_STRING libfileW;
650 LPCWSTR filenameW;
651 static const WCHAR dllW[] = {'.','D','L','L',0};
653 if (!lpszLibFile)
655 SetLastError(ERROR_INVALID_PARAMETER);
656 return 0;
659 if (!RtlCreateUnicodeStringFromAsciiz(&libfileW, lpszLibFile))
661 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
662 return 0;
665 /* if the file can not be found, call LoadLibraryExA anyway, since it might be
666 a buildin module. This case is handled in MODULE_LoadLibraryExA */
668 filenameW = libfileW.Buffer;
669 if ( DIR_SearchPath( NULL, filenameW, dllW, &full_name, FALSE ) )
670 filenameW = full_name.short_name;
672 ReleaseThunkLock( &mutex_count );
673 hModule = LoadLibraryExW( filenameW, (HANDLE)hFile, dwFlags );
674 RestoreThunkLock( mutex_count );
676 RtlFreeUnicodeString(&libfileW);
678 return (DWORD)hModule;
681 /***********************************************************************
682 * GetProcAddress32W (KERNEL.515)
684 DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
686 return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
689 /***********************************************************************
690 * FreeLibrary32W (KERNEL.514)
692 DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
694 BOOL retv;
695 DWORD mutex_count;
697 ReleaseThunkLock( &mutex_count );
698 retv = FreeLibrary( (HMODULE)hLibModule );
699 RestoreThunkLock( mutex_count );
700 return (DWORD)retv;
704 /**********************************************************************
705 * WOW_CallProc32W
707 static DWORD WOW_CallProc32W16( FARPROC proc32, DWORD nrofargs, DWORD *args )
709 DWORD ret;
710 DWORD mutex_count;
712 ReleaseThunkLock( &mutex_count );
715 * FIXME: If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
716 * 32-bit CDECL routine ...
719 if (!proc32) ret = 0;
720 else switch (nrofargs)
722 case 0: ret = proc32();
723 break;
724 case 1: ret = proc32(args[0]);
725 break;
726 case 2: ret = proc32(args[0],args[1]);
727 break;
728 case 3: ret = proc32(args[0],args[1],args[2]);
729 break;
730 case 4: ret = proc32(args[0],args[1],args[2],args[3]);
731 break;
732 case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
733 break;
734 case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
735 break;
736 case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
737 break;
738 case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
739 break;
740 case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
741 break;
742 case 10:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
743 break;
744 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]);
745 break;
746 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]);
747 break;
748 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]);
749 break;
750 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]);
751 break;
752 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]);
753 break;
754 default:
755 /* FIXME: should go up to 32 arguments */
756 ERR("Unsupported number of arguments %ld, please report.\n",nrofargs);
757 ret = 0;
758 break;
761 RestoreThunkLock( mutex_count );
763 TRACE("returns %08lx\n",ret);
764 return ret;
767 /**********************************************************************
768 * CallProc32W (KERNEL.517)
770 DWORD WINAPIV CallProc32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
772 DWORD args[32];
773 unsigned int i;
775 TRACE("(%ld,%ld,%p args[",nrofargs,argconvmask,proc32);
777 for (i=0;i<nrofargs;i++)
779 if (argconvmask & (1<<i))
781 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
782 /* pascal convention, have to reverse the arguments order */
783 args[nrofargs - i - 1] = (DWORD)MapSL(ptr);
784 TRACE("%08lx(%p),",ptr,MapSL(ptr));
786 else
788 DWORD arg = VA_ARG16( valist, DWORD );
789 /* pascal convention, have to reverse the arguments order */
790 args[nrofargs - i - 1] = arg;
791 TRACE("%ld,", arg);
794 TRACE("])\n");
796 /* POP nrofargs DWORD arguments and 3 DWORD parameters */
797 stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
799 return WOW_CallProc32W16( proc32, nrofargs, args );
802 /**********************************************************************
803 * _CallProcEx32W (KERNEL.518)
805 DWORD WINAPIV CallProcEx32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
807 DWORD args[32];
808 unsigned int i;
810 TRACE("(%ld,%ld,%p args[",nrofargs,argconvmask,proc32);
812 for (i=0;i<nrofargs;i++)
814 if (argconvmask & (1<<i))
816 SEGPTR ptr = VA_ARG16( valist, SEGPTR );
817 args[i] = (DWORD)MapSL(ptr);
818 TRACE("%08lx(%p),",ptr,MapSL(ptr));
820 else
822 DWORD arg = VA_ARG16( valist, DWORD );
823 args[i] = arg;
824 TRACE("%ld,", arg);
827 TRACE("])\n");
828 return WOW_CallProc32W16( proc32, nrofargs, args );
832 /**********************************************************************
833 * WOW16Call (KERNEL.500)
835 * FIXME!!!
838 DWORD WINAPIV WOW16Call(WORD x, WORD y, WORD z, VA_LIST16 args)
840 int i;
841 DWORD calladdr;
842 FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
844 for (i=0;i<x/2;i++) {
845 WORD a = VA_ARG16(args,WORD);
846 DPRINTF("%04x ",a);
848 calladdr = VA_ARG16(args,DWORD);
849 stack16_pop( 3*sizeof(WORD) + x + sizeof(DWORD) );
850 DPRINTF(") calling address was 0x%08lx\n",calladdr);
851 return 0;
855 /***********************************************************************
856 * CreateThread16 (KERNEL.441)
858 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
859 FARPROC16 start, SEGPTR param,
860 DWORD flags, LPDWORD id )
862 struct thread_args *args = HeapAlloc( GetProcessHeap(), 0, sizeof(*args) );
863 if (!args) return INVALID_HANDLE_VALUE;
864 args->proc = start;
865 args->param = param;
866 return CreateThread( sa, stack, start_thread16, args, flags, id );