Unicodify some comm functions.
[wine] / dlls / kernel / wowthunk.c
1 /*
2  * Win32 WOW Generic Thunk API
3  *
4  * Copyright 1999 Ulrich Weigand
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdarg.h>
26
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 "miscemu.h"
37 #include "module.h"
38 #include "stackframe.h"
39 #include "kernel_private.h"
40 #include "wine/exception.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
44 WINE_DECLARE_DEBUG_CHANNEL(relay);
45 WINE_DECLARE_DEBUG_CHANNEL(snoop);
46
47 /*
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 ...
51  */
52
53 DWORD WINAPI GetVDMPointer32W16(SEGPTR,UINT16);
54
55 DWORD WINAPI LoadLibraryEx32W16(LPCSTR,DWORD,DWORD);
56 DWORD WINAPI GetProcAddress32W16(DWORD,LPCSTR);
57 DWORD WINAPI FreeLibrary32W16(DWORD);
58
59 #define CPEX_DEST_STDCALL   0x00000000L
60 #define CPEX_DEST_CDECL     0x80000000L
61
62 /* thunk for 16-bit CreateThread */
63 struct thread_args
64 {
65     FARPROC16 proc;
66     DWORD     param;
67 };
68
69 static DWORD CALLBACK start_thread16( LPVOID threadArgs )
70 {
71     struct thread_args args = *(struct thread_args *)threadArgs;
72     HeapFree( GetProcessHeap(), 0, threadArgs );
73     return K32WOWCallback16( (DWORD)args.proc, args.param );
74 }
75
76
77 #ifdef __i386__
78
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;
91
92 extern void RELAY16_InitDebugLists(void);
93
94 static LONG CALLBACK vectored_handler( EXCEPTION_POINTERS *ptrs );
95 static SEGPTR call16_ret_addr;  /* segptr to CallTo16_Ret routine */
96
97 /***********************************************************************
98  *           WOWTHUNK_Init
99  */
100 BOOL WOWTHUNK_Init(void)
101 {
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;
107
108       /* Patch the return addresses for CallTo16 routines */
109
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 );
116
117     if (TRACE_ON(relay) || TRACE_ON(snoop)) RELAY16_InitDebugLists();
118
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;
122 }
123
124
125 /*************************************************************
126  *            fix_selector
127  *
128  * Fix a selector load that caused an exception if it's in the
129  * 16-bit relay code.
130  */
131 static BOOL fix_selector( CONTEXT *context )
132 {
133     WORD *stack;
134     BYTE *instr = (BYTE *)context->Eip;
135
136     if (instr < &Call16_Start || instr >= &Call16_End) return FALSE;
137
138     /* skip prefixes */
139     while (*instr == 0x66 || *instr == 0x67) instr++;
140
141     switch(instr[0])
142     {
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])
149         {
150         case 0xa1: /* pop fs */
151         case 0xa9: /* pop gs */
152             break;
153         default:
154             return FALSE;
155         }
156         break;
157     default:
158         return FALSE;
159     }
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;
164 }
165
166
167 /*************************************************************
168  *            call16_handler
169  *
170  * Handler for exceptions occurring in 16-bit code.
171  */
172 static DWORD call16_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
173                              CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
174 {
175     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
176     {
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();
181     }
182     else if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
183              record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
184     {
185         if (wine_ldt_is_system(context->SegCs))
186         {
187             if (fix_selector( context )) return ExceptionContinueExecution;
188         }
189         else
190         {
191             SEGPTR gpHandler;
192             DWORD ret = INSTR_EmulateInstruction( record, context );
193
194             if (ret != ExceptionContinueSearch) return ret;
195
196             /* check for Win16 __GP handler */
197             if ((gpHandler = HasGPHandler16( MAKESEGPTR( context->SegCs, context->Eip ) )))
198             {
199                 WORD *stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
200                 *--stack = context->SegCs;
201                 *--stack = context->Eip;
202
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);
208
209                 context->SegCs = SELECTOROF( gpHandler );
210                 context->Eip   = OFFSETOF( gpHandler );
211                 return ExceptionContinueExecution;
212             }
213         }
214     }
215     return ExceptionContinueSearch;
216 }
217
218
219 /*************************************************************
220  *            vm86_handler
221  *
222  * Handler for exceptions occurring in vm86 code.
223  */
224 static DWORD vm86_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
225                            CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
226 {
227     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
228         return ExceptionContinueSearch;
229
230     if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
231         record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
232     {
233         return INSTR_EmulateInstruction( record, context );
234     }
235
236     return ExceptionContinueSearch;
237 }
238
239
240 /***********************************************************************
241  *           vectored_handler
242  *
243  * Vectored exception handler used to emulate protected instructions
244  * from 32-bit code.
245  */
246 static LONG CALLBACK vectored_handler( EXCEPTION_POINTERS *ptrs )
247 {
248     EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
249     CONTEXT *context = ptrs->ContextRecord;
250
251     if (wine_ldt_is_system(context->SegCs) &&
252         (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
253          record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION))
254     {
255         if (INSTR_EmulateInstruction( record, context ) == ExceptionContinueExecution)
256             return EXCEPTION_CONTINUE_EXECUTION;
257     }
258     return EXCEPTION_CONTINUE_SEARCH;
259 }
260
261
262 #else  /* __i386__ */
263
264 BOOL WOWTHUNK_Init(void)
265 {
266     return TRUE;
267 }
268
269 #endif  /* __i386__ */
270
271
272 /*
273  *  32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
274  */
275
276 /**********************************************************************
277  *           K32WOWGetDescriptor        (KERNEL32.70)
278  */
279 BOOL WINAPI K32WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
280 {
281     return GetThreadSelectorEntry( GetCurrentThread(),
282                                    segptr >> 16, ldtent );
283 }
284
285 /**********************************************************************
286  *           K32WOWGetVDMPointer        (KERNEL32.56)
287  */
288 LPVOID WINAPI K32WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
289 {
290     /* FIXME: add size check too */
291
292     if ( fProtectedMode )
293         return MapSL( vp );
294     else
295         return DOSMEM_MapRealToLinear( vp );
296 }
297
298 /**********************************************************************
299  *           K32WOWGetVDMPointerFix     (KERNEL32.68)
300  */
301 LPVOID WINAPI K32WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
302 {
303     /*
304      * Hmmm. According to the docu, we should call:
305      *
306      *          GlobalFix16( SELECTOROF(vp) );
307      *
308      * But this is unnecessary under Wine, as we never move global
309      * memory segments in linear memory anyway.
310      *
311      * (I'm not so sure what we are *supposed* to do if
312      *  fProtectedMode is TRUE, anyway ...)
313      */
314
315     return K32WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
316 }
317
318 /**********************************************************************
319  *           K32WOWGetVDMPointerUnfix   (KERNEL32.69)
320  */
321 VOID WINAPI K32WOWGetVDMPointerUnfix( DWORD vp )
322 {
323     /*
324      * See above why we don't call:
325      *
326      * GlobalUnfix16( SELECTOROF(vp) );
327      *
328      */
329 }
330
331 /**********************************************************************
332  *           K32WOWGlobalAlloc16        (KERNEL32.59)
333  */
334 WORD WINAPI K32WOWGlobalAlloc16( WORD wFlags, DWORD cb )
335 {
336     return (WORD)GlobalAlloc16( wFlags, cb );
337 }
338
339 /**********************************************************************
340  *           K32WOWGlobalFree16         (KERNEL32.62)
341  */
342 WORD WINAPI K32WOWGlobalFree16( WORD hMem )
343 {
344     return (WORD)GlobalFree16( (HGLOBAL16)hMem );
345 }
346
347 /**********************************************************************
348  *           K32WOWGlobalUnlock16       (KERNEL32.61)
349  */
350 BOOL WINAPI K32WOWGlobalUnlock16( WORD hMem )
351 {
352     return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
353 }
354
355 /**********************************************************************
356  *           K32WOWGlobalAllocLock16    (KERNEL32.63)
357  */
358 DWORD WINAPI K32WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
359 {
360     WORD hMem = K32WOWGlobalAlloc16( wFlags, cb );
361     if (phMem) *phMem = hMem;
362
363     return K32WOWGlobalLock16( hMem );
364 }
365
366 /**********************************************************************
367  *           K32WOWGlobalLockSize16     (KERNEL32.65)
368  */
369 DWORD WINAPI K32WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
370 {
371     if ( pcb )
372         *pcb = GlobalSize16( (HGLOBAL16)hMem );
373
374     return K32WOWGlobalLock16( hMem );
375 }
376
377 /**********************************************************************
378  *           K32WOWGlobalUnlockFree16   (KERNEL32.64)
379  */
380 WORD WINAPI K32WOWGlobalUnlockFree16( DWORD vpMem )
381 {
382     if ( !K32WOWGlobalUnlock16( HIWORD(vpMem) ) )
383         return FALSE;
384
385     return K32WOWGlobalFree16( HIWORD(vpMem) );
386 }
387
388
389 /**********************************************************************
390  *           K32WOWYield16              (KERNEL32.66)
391  */
392 VOID WINAPI K32WOWYield16( void )
393 {
394     /*
395      * This does the right thing for both Win16 and Win32 tasks.
396      * More or less, at least :-/
397      */
398     Yield16();
399 }
400
401 /**********************************************************************
402  *           K32WOWDirectedYield16       (KERNEL32.67)
403  */
404 VOID WINAPI K32WOWDirectedYield16( WORD htask16 )
405 {
406     /*
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 ...
410      */
411     DirectedYield16( (HTASK16)htask16 );
412 }
413
414
415 /***********************************************************************
416  *           K32WOWHandle32              (KERNEL32.57)
417  */
418 HANDLE WINAPI K32WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
419 {
420     switch ( type )
421     {
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;
435
436     case WOW_TYPE_HMETAFILE:
437         FIXME( "conversion of metafile handles not supported yet\n" );
438         return (HANDLE)(ULONG_PTR)handle;
439
440     case WOW_TYPE_HTASK:
441         return ((TDB *)GlobalLock16(handle))->teb->ClientId.UniqueThread;
442
443     case WOW_TYPE_FULLHWND:
444         FIXME( "conversion of full window handles not supported yet\n" );
445         return (HANDLE)(ULONG_PTR)handle;
446
447     default:
448         ERR( "handle 0x%04x of unknown type %d\n", handle, type );
449         return (HANDLE)(ULONG_PTR)handle;
450     }
451 }
452
453 /***********************************************************************
454  *           K32WOWHandle16              (KERNEL32.58)
455  */
456 WORD WINAPI K32WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
457 {
458     switch ( type )
459     {
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);
476
477     case WOW_TYPE_HMETAFILE:
478         FIXME( "conversion of metafile handles not supported yet\n" );
479         return LOWORD(handle);
480
481     case WOW_TYPE_HTASK:
482         return TASK_GetTaskFromThread( (DWORD)handle );
483
484     default:
485         ERR( "handle %p of unknown type %d\n", handle, type );
486         return LOWORD(handle);
487     }
488 }
489
490 /**********************************************************************
491  *           K32WOWCallback16Ex         (KERNEL32.55)
492  */
493 BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
494                                 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
495 {
496 #ifdef __i386__
497     /*
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 ...
501      */
502     WORD *stack = (WORD *)CURRENT_STACK16 - cbArgs / sizeof(WORD);
503
504     memcpy( stack, pArgs, cbArgs );
505
506     if (dwFlags & (WCB16_REGS|WCB16_REGS_LONG))
507     {
508         CONTEXT *context = (CONTEXT *)pdwRetCode;
509
510         if (TRACE_ON(relay))
511         {
512             DWORD count = cbArgs / sizeof(WORD);
513
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 );
525         }
526
527         if (context->EFlags & 0x00020000)  /* v86 mode */
528         {
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 );
534         }
535         else
536         {
537             /* push return address */
538             if (dwFlags & WCB16_REGS_LONG)
539             {
540                 *((DWORD *)stack - 1) = HIWORD(call16_ret_addr);
541                 *((DWORD *)stack - 2) = LOWORD(call16_ret_addr);
542                 cbArgs += 2 * sizeof(DWORD);
543             }
544             else
545             {
546                 *((SEGPTR *)stack - 1) = call16_ret_addr;
547                 cbArgs += sizeof(SEGPTR);
548             }
549
550             _EnterWin16Lock();
551             wine_call_to_16_regs( context, cbArgs, call16_handler );
552             _LeaveWin16Lock();
553         }
554
555         if (TRACE_ON(relay))
556         {
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 );
564         }
565     }
566     else
567     {
568         DWORD ret;
569
570         if (TRACE_ON(relay))
571         {
572             DWORD count = cbArgs / sizeof(WORD);
573
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 );
581         }
582
583         /* push return address */
584         *((SEGPTR *)stack - 1) = call16_ret_addr;
585         cbArgs += sizeof(SEGPTR);
586
587         /*
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.
592          */
593         _EnterWin16Lock();
594         ret = wine_call_to_16( (FARPROC16)vpfn16, cbArgs, call16_handler );
595         if (pdwRetCode) *pdwRetCode = ret;
596         _LeaveWin16Lock();
597
598         if (TRACE_ON(relay))
599         {
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 );
604         }
605     }
606 #else
607     assert(0);  /* cannot call to 16-bit on non-Intel architectures */
608 #endif  /* __i386__ */
609
610     return TRUE;  /* success */
611 }
612
613 /**********************************************************************
614  *           K32WOWCallback16            (KERNEL32.54)
615  */
616 DWORD WINAPI K32WOWCallback16( DWORD vpfn16, DWORD dwParam )
617 {
618     DWORD ret;
619
620     if ( !K32WOWCallback16Ex( vpfn16, WCB16_PASCAL,
621                            sizeof(DWORD), &dwParam, &ret ) )
622         ret = 0L;
623
624     return ret;
625 }
626
627
628 /*
629  *  16-bit WOW routines (in KERNEL)
630  */
631
632 /**********************************************************************
633  *           GetVDMPointer32W      (KERNEL.516)
634  */
635 DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
636 {
637     GlobalPageLock16(GlobalHandle16(SELECTOROF(vp)));
638     return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
639 }
640
641 /***********************************************************************
642  *           LoadLibraryEx32W      (KERNEL.513)
643  */
644 DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
645 {
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};
652
653     if (!lpszLibFile)
654     {
655         SetLastError(ERROR_INVALID_PARAMETER);
656         return 0;
657     }
658
659     if (!RtlCreateUnicodeStringFromAsciiz(&libfileW, lpszLibFile))
660     {
661         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
662         return 0;
663     }
664
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 */
667
668     filenameW = libfileW.Buffer;
669     if ( DIR_SearchPath( NULL, filenameW, dllW, &full_name, FALSE ) )
670         filenameW = full_name.short_name;
671
672     ReleaseThunkLock( &mutex_count );
673     hModule = LoadLibraryExW( filenameW, (HANDLE)hFile, dwFlags );
674     RestoreThunkLock( mutex_count );
675
676     RtlFreeUnicodeString(&libfileW);
677
678     return (DWORD)hModule;
679 }
680
681 /***********************************************************************
682  *           GetProcAddress32W     (KERNEL.515)
683  */
684 DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
685 {
686     return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
687 }
688
689 /***********************************************************************
690  *           FreeLibrary32W        (KERNEL.514)
691  */
692 DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
693 {
694     BOOL retv;
695     DWORD mutex_count;
696
697     ReleaseThunkLock( &mutex_count );
698     retv = FreeLibrary( (HMODULE)hLibModule );
699     RestoreThunkLock( mutex_count );
700     return (DWORD)retv;
701 }
702
703
704 /**********************************************************************
705  *           WOW_CallProc32W
706  */
707 static DWORD WOW_CallProc32W16( FARPROC proc32, DWORD nrofargs, DWORD *args )
708 {
709     DWORD ret;
710     DWORD mutex_count;
711
712     ReleaseThunkLock( &mutex_count );
713
714     /*
715      * FIXME:  If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
716      *         32-bit CDECL routine ...
717      */
718
719     if (!proc32) ret = 0;
720     else switch (nrofargs)
721     {
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;
759     }
760
761     RestoreThunkLock( mutex_count );
762
763     TRACE("returns %08lx\n",ret);
764     return ret;
765 }
766
767 /**********************************************************************
768  *           CallProc32W           (KERNEL.517)
769  */
770 DWORD WINAPIV CallProc32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
771 {
772     DWORD args[32];
773     unsigned int i;
774
775     TRACE("(%ld,%ld,%p args[",nrofargs,argconvmask,proc32);
776
777     for (i=0;i<nrofargs;i++)
778     {
779         if (argconvmask & (1<<i))
780         {
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));
785         }
786         else
787         {
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);
792         }
793     }
794     TRACE("])\n");
795
796     /* POP nrofargs DWORD arguments and 3 DWORD parameters */
797     stack16_pop( (3 + nrofargs) * sizeof(DWORD) );
798
799     return WOW_CallProc32W16( proc32, nrofargs, args );
800 }
801
802 /**********************************************************************
803  *           _CallProcEx32W         (KERNEL.518)
804  */
805 DWORD WINAPIV CallProcEx32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
806 {
807     DWORD args[32];
808     unsigned int i;
809
810     TRACE("(%ld,%ld,%p args[",nrofargs,argconvmask,proc32);
811
812     for (i=0;i<nrofargs;i++)
813     {
814         if (argconvmask & (1<<i))
815         {
816             SEGPTR ptr = VA_ARG16( valist, SEGPTR );
817             args[i] = (DWORD)MapSL(ptr);
818             TRACE("%08lx(%p),",ptr,MapSL(ptr));
819         }
820         else
821         {
822             DWORD arg = VA_ARG16( valist, DWORD );
823             args[i] = arg;
824             TRACE("%ld,", arg);
825         }
826     }
827     TRACE("])\n");
828     return WOW_CallProc32W16( proc32, nrofargs, args );
829 }
830
831
832 /**********************************************************************
833  *           WOW16Call               (KERNEL.500)
834  *
835  * FIXME!!!
836  *
837  */
838 DWORD WINAPIV WOW16Call(WORD x, WORD y, WORD z, VA_LIST16 args)
839 {
840         int     i;
841         DWORD   calladdr;
842         FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);
843
844         for (i=0;i<x/2;i++) {
845                 WORD    a = VA_ARG16(args,WORD);
846                 DPRINTF("%04x ",a);
847         }
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;
852 }
853
854
855 /***********************************************************************
856  *           CreateThread16   (KERNEL.441)
857  */
858 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
859                               FARPROC16 start, SEGPTR param,
860                               DWORD flags, LPDWORD id )
861 {
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 );
867 }