mshtml: Added IHTMLWindow2::get_length implementation.
[wine] / dlls / kernel32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <errno.h>
27
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 "winternl.h"
35 #include "kernel_private.h"
36 #include "kernel16_private.h"
37 #include "wine/exception.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(thunk);
41
42 #ifdef __i386__
43
44 WINE_DECLARE_DEBUG_CHANNEL(relay);
45 WINE_DECLARE_DEBUG_CHANNEL(snoop);
46
47 /* symbols exported from relay16.s */
48 extern DWORD WINAPI wine_call_to_16( FARPROC16 target, DWORD cbArgs, PEXCEPTION_HANDLER handler );
49 extern void WINAPI wine_call_to_16_regs( CONTEXT86 *context, DWORD cbArgs, PEXCEPTION_HANDLER handler );
50 extern void __wine_call_to_16_ret(void);
51 extern void CALL32_CBClient_Ret(void);
52 extern void CALL32_CBClientEx_Ret(void);
53 extern void DPMI_PendingEventCheck(void);
54 extern void DPMI_PendingEventCheck_Cleanup(void);
55 extern void DPMI_PendingEventCheck_Return(void);
56 extern BYTE __wine_call16_start[];
57 extern BYTE __wine_call16_end[];
58
59 extern void RELAY16_InitDebugLists(void);
60
61 static SEGPTR call16_ret_addr;  /* segptr to __wine_call_to_16_ret routine */
62
63 static WORD  dpmi_checker_selector;
64 static DWORD dpmi_checker_offset_call;
65 static DWORD dpmi_checker_offset_cleanup;
66 static DWORD dpmi_checker_offset_return;
67
68 /***********************************************************************
69  *           WOWTHUNK_Init
70  */
71 BOOL WOWTHUNK_Init(void)
72 {
73     /* allocate the code selector for CallTo16 routines */
74     LDT_ENTRY entry;
75     WORD codesel = wine_ldt_alloc_entries(1);
76
77     if (!codesel) return FALSE;
78     wine_ldt_set_base( &entry, __wine_call16_start );
79     wine_ldt_set_limit( &entry, (BYTE *)(&CallTo16_TebSelector + 1) - __wine_call16_start - 1 );
80     wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
81     wine_ldt_set_entry( codesel, &entry );
82
83       /* Patch the return addresses for CallTo16 routines */
84
85     CallTo16_DataSelector = wine_get_ds();
86     call16_ret_addr = MAKESEGPTR( codesel, (BYTE *)__wine_call_to_16_ret - __wine_call16_start );
87     CALL32_CBClient_RetAddr =
88         MAKESEGPTR( codesel, (BYTE *)CALL32_CBClient_Ret - __wine_call16_start );
89     CALL32_CBClientEx_RetAddr =
90         MAKESEGPTR( codesel, (BYTE *)CALL32_CBClientEx_Ret - __wine_call16_start );
91
92     /* Prepare selector and offsets for DPMI event checking. */
93     dpmi_checker_selector = codesel;
94     dpmi_checker_offset_call = (BYTE *)DPMI_PendingEventCheck - __wine_call16_start;
95     dpmi_checker_offset_cleanup = (BYTE *)DPMI_PendingEventCheck_Cleanup - __wine_call16_start;
96     dpmi_checker_offset_return = (BYTE *)DPMI_PendingEventCheck_Return - __wine_call16_start;
97
98     if (TRACE_ON(relay) || TRACE_ON(snoop)) RELAY16_InitDebugLists();
99
100     return TRUE;
101 }
102
103
104 /*************************************************************
105  *            fix_selector
106  *
107  * Fix a selector load that caused an exception if it's in the
108  * 16-bit relay code.
109  */
110 static BOOL fix_selector( CONTEXT *context )
111 {
112     WORD *stack;
113     BYTE *instr = (BYTE *)context->Eip;
114
115     if (instr < __wine_call16_start || instr >= __wine_call16_end) return FALSE;
116
117     /* skip prefixes */
118     while (*instr == 0x66 || *instr == 0x67) instr++;
119
120     switch(instr[0])
121     {
122     case 0x07: /* pop es */
123     case 0x17: /* pop ss */
124     case 0x1f: /* pop ds */
125         break;
126     case 0x0f: /* extended instruction */
127         switch(instr[1])
128         {
129         case 0xa1: /* pop fs */
130         case 0xa9: /* pop gs */
131             break;
132         default:
133             return FALSE;
134         }
135         break;
136     default:
137         return FALSE;
138     }
139     stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
140     TRACE( "fixing up selector %x for pop instruction\n", *stack );
141     *stack = 0;
142     return TRUE;
143 }
144
145
146 /*************************************************************
147  *            insert_event_check
148  *
149  * Make resuming the context check for pending DPMI events
150  * before the original context is restored. This is required
151  * because DPMI events are asynchronous, they are blocked while 
152  * Wine 32-bit code is being executed and we want to prevent 
153  * a race when returning back to 16-bit or 32-bit DPMI context.
154  */
155 static void insert_event_check( CONTEXT *context )
156 {
157     char *stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
158
159     /* don't do event check while in system code */
160     if (wine_ldt_is_system(context->SegCs))
161         return;
162
163     if(context->SegCs == dpmi_checker_selector &&
164        context->Eip   >= dpmi_checker_offset_call && 
165        context->Eip   <= dpmi_checker_offset_cleanup)
166     {
167         /*
168          * Nested call. Stack will be preserved. 
169          */
170     }
171     else if(context->SegCs == dpmi_checker_selector &&
172             context->Eip   == dpmi_checker_offset_return)
173     {
174         /*
175          * Nested call. We have just finished popping the fs
176          * register, lets put it back into stack.
177          */
178
179         stack -= sizeof(WORD);
180         *(WORD*)stack = context->SegFs;
181
182         context->Esp -= 2;
183     }
184     else
185     {
186         /*
187          * Call is not nested.
188          * Push modified registers into stack.
189          * These will be popped by the assembler stub.
190          */
191
192         stack -= sizeof(DWORD);
193         *(DWORD*)stack = context->EFlags;
194    
195         stack -= sizeof(DWORD);
196         *(DWORD*)stack = context->SegCs;
197
198         stack -= sizeof(DWORD);
199         *(DWORD*)stack = context->Eip;
200
201         stack -= sizeof(WORD);
202         *(WORD*)stack = context->SegFs;
203
204         context->Esp  -= 14;
205     }
206
207     /*
208      * Modify the context so that we jump into assembler stub.
209      * TEB access is made easier by providing the stub
210      * with the correct fs register value.
211      */
212
213     context->SegCs = dpmi_checker_selector;
214     context->Eip   = dpmi_checker_offset_call;
215     context->SegFs = wine_get_fs();
216 }
217
218
219 /*************************************************************
220  *            call16_handler
221  *
222  * Handler for exceptions occurring in 16-bit code.
223  */
224 static DWORD call16_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     {
229         /* unwinding: restore the stack pointer in the TEB, and leave the Win16 mutex */
230         STACK32FRAME *frame32 = (STACK32FRAME *)((char *)frame - offsetof(STACK32FRAME,frame));
231         NtCurrentTeb()->WOW32Reserved = (void *)frame32->frame16;
232         _LeaveWin16Lock();
233     }
234     else if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
235              record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
236     {
237         if (wine_ldt_is_system(context->SegCs))
238         {
239             if (fix_selector( context )) return ExceptionContinueExecution;
240         }
241         else
242         {
243             SEGPTR gpHandler;
244             DWORD ret = __wine_emulate_instruction( record, context );
245
246             /*
247              * Insert check for pending DPMI events. Note that this 
248              * check must be inserted after instructions have been 
249              * emulated because the instruction emulation requires
250              * original CS:IP and the emulation may change TEB.dpmi_vif.
251              */
252             if(get_vm86_teb_info()->dpmi_vif)
253                 insert_event_check( context );
254
255             if (ret != ExceptionContinueSearch) return ret;
256
257             /* check for Win16 __GP handler */
258             if ((gpHandler = HasGPHandler16( MAKESEGPTR( context->SegCs, context->Eip ) )))
259             {
260                 WORD *stack = wine_ldt_get_ptr( context->SegSs, context->Esp );
261                 *--stack = context->SegCs;
262                 *--stack = context->Eip;
263
264                 if (!IS_SELECTOR_32BIT(context->SegSs))
265                     context->Esp = MAKELONG( LOWORD(context->Esp - 2*sizeof(WORD)),
266                                              HIWORD(context->Esp) );
267                 else
268                     context->Esp -= 2*sizeof(WORD);
269
270                 context->SegCs = SELECTOROF( gpHandler );
271                 context->Eip   = OFFSETOF( gpHandler );
272                 return ExceptionContinueExecution;
273             }
274         }
275     }
276     else if (record->ExceptionCode == EXCEPTION_VM86_STI)
277     {
278         insert_event_check( context );
279     }
280     return ExceptionContinueSearch;
281 }
282
283
284 /*************************************************************
285  *            vm86_handler
286  *
287  * Handler for exceptions occurring in vm86 code.
288  */
289 static DWORD vm86_handler( EXCEPTION_RECORD *record, EXCEPTION_REGISTRATION_RECORD *frame,
290                            CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **pdispatcher )
291 {
292     if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
293         return ExceptionContinueSearch;
294
295     if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
296         record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
297     {
298         return __wine_emulate_instruction( record, context );
299     }
300
301     return ExceptionContinueSearch;
302 }
303
304
305 #else  /* __i386__ */
306
307 BOOL WOWTHUNK_Init(void)
308 {
309     return TRUE;
310 }
311
312 #endif  /* __i386__ */
313
314
315 /*
316  *  32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
317  */
318
319 /**********************************************************************
320  *           K32WOWGetDescriptor        (KERNEL32.70)
321  */
322 BOOL WINAPI K32WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
323 {
324     return GetThreadSelectorEntry( GetCurrentThread(),
325                                    segptr >> 16, ldtent );
326 }
327
328 /**********************************************************************
329  *           K32WOWGetVDMPointer        (KERNEL32.56)
330  */
331 LPVOID WINAPI K32WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
332 {
333     /* FIXME: add size check too */
334
335     if ( fProtectedMode )
336         return MapSL( vp );
337     else
338         return DOSMEM_MapRealToLinear( vp );
339 }
340
341 /**********************************************************************
342  *           K32WOWGetVDMPointerFix     (KERNEL32.68)
343  */
344 LPVOID WINAPI K32WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
345 {
346     /*
347      * Hmmm. According to the docu, we should call:
348      *
349      *          GlobalFix16( SELECTOROF(vp) );
350      *
351      * But this is unnecessary under Wine, as we never move global
352      * memory segments in linear memory anyway.
353      *
354      * (I'm not so sure what we are *supposed* to do if
355      *  fProtectedMode is TRUE, anyway ...)
356      */
357
358     return K32WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
359 }
360
361 /**********************************************************************
362  *           K32WOWGetVDMPointerUnfix   (KERNEL32.69)
363  */
364 VOID WINAPI K32WOWGetVDMPointerUnfix( DWORD vp )
365 {
366     /*
367      * See above why we don't call:
368      *
369      * GlobalUnfix16( SELECTOROF(vp) );
370      *
371      */
372 }
373
374 /**********************************************************************
375  *           K32WOWGlobalAlloc16        (KERNEL32.59)
376  */
377 WORD WINAPI K32WOWGlobalAlloc16( WORD wFlags, DWORD cb )
378 {
379     return (WORD)GlobalAlloc16( wFlags, cb );
380 }
381
382 /**********************************************************************
383  *           K32WOWGlobalFree16         (KERNEL32.62)
384  */
385 WORD WINAPI K32WOWGlobalFree16( WORD hMem )
386 {
387     return (WORD)GlobalFree16( (HGLOBAL16)hMem );
388 }
389
390 /**********************************************************************
391  *           K32WOWGlobalUnlock16       (KERNEL32.61)
392  */
393 BOOL WINAPI K32WOWGlobalUnlock16( WORD hMem )
394 {
395     return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
396 }
397
398 /**********************************************************************
399  *           K32WOWGlobalAllocLock16    (KERNEL32.63)
400  */
401 DWORD WINAPI K32WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
402 {
403     WORD hMem = K32WOWGlobalAlloc16( wFlags, cb );
404     if (phMem) *phMem = hMem;
405
406     return K32WOWGlobalLock16( hMem );
407 }
408
409 /**********************************************************************
410  *           K32WOWGlobalLockSize16     (KERNEL32.65)
411  */
412 DWORD WINAPI K32WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
413 {
414     if ( pcb )
415         *pcb = GlobalSize16( (HGLOBAL16)hMem );
416
417     return K32WOWGlobalLock16( hMem );
418 }
419
420 /**********************************************************************
421  *           K32WOWGlobalUnlockFree16   (KERNEL32.64)
422  */
423 WORD WINAPI K32WOWGlobalUnlockFree16( DWORD vpMem )
424 {
425     if ( !K32WOWGlobalUnlock16( HIWORD(vpMem) ) )
426         return FALSE;
427
428     return K32WOWGlobalFree16( HIWORD(vpMem) );
429 }
430
431
432 /**********************************************************************
433  *           K32WOWYield16              (KERNEL32.66)
434  */
435 VOID WINAPI K32WOWYield16( void )
436 {
437     /*
438      * This does the right thing for both Win16 and Win32 tasks.
439      * More or less, at least :-/
440      */
441     Yield16();
442 }
443
444 /**********************************************************************
445  *           K32WOWDirectedYield16       (KERNEL32.67)
446  */
447 VOID WINAPI K32WOWDirectedYield16( WORD htask16 )
448 {
449     /*
450      * Argh.  Our scheduler doesn't like DirectedYield by Win32
451      * tasks at all.  So we do hope that this routine is indeed
452      * only ever called by Win16 tasks that have thunked up ...
453      */
454     DirectedYield16( (HTASK16)htask16 );
455 }
456
457
458 /***********************************************************************
459  *           K32WOWHandle32              (KERNEL32.57)
460  */
461 HANDLE WINAPI K32WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
462 {
463     switch ( type )
464     {
465     case WOW_TYPE_HWND:
466     case WOW_TYPE_HMENU:
467     case WOW_TYPE_HDWP:
468     case WOW_TYPE_HDROP:
469     case WOW_TYPE_HDC:
470     case WOW_TYPE_HFONT:
471     case WOW_TYPE_HRGN:
472     case WOW_TYPE_HBITMAP:
473     case WOW_TYPE_HBRUSH:
474     case WOW_TYPE_HPALETTE:
475     case WOW_TYPE_HPEN:
476     case WOW_TYPE_HACCEL:
477         return (HANDLE)(ULONG_PTR)handle;
478
479     case WOW_TYPE_HMETAFILE:
480         FIXME( "conversion of metafile handles not supported yet\n" );
481         return (HANDLE)(ULONG_PTR)handle;
482
483     case WOW_TYPE_HTASK:
484         return ((TDB *)GlobalLock16(handle))->teb->ClientId.UniqueThread;
485
486     case WOW_TYPE_FULLHWND:
487         FIXME( "conversion of full window handles not supported yet\n" );
488         return (HANDLE)(ULONG_PTR)handle;
489
490     default:
491         ERR( "handle 0x%04x of unknown type %d\n", handle, type );
492         return (HANDLE)(ULONG_PTR)handle;
493     }
494 }
495
496 /***********************************************************************
497  *           K32WOWHandle16              (KERNEL32.58)
498  */
499 WORD WINAPI K32WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
500 {
501     switch ( type )
502     {
503     case WOW_TYPE_HWND:
504     case WOW_TYPE_HMENU:
505     case WOW_TYPE_HDWP:
506     case WOW_TYPE_HDROP:
507     case WOW_TYPE_HDC:
508     case WOW_TYPE_HFONT:
509     case WOW_TYPE_HRGN:
510     case WOW_TYPE_HBITMAP:
511     case WOW_TYPE_HBRUSH:
512     case WOW_TYPE_HPALETTE:
513     case WOW_TYPE_HPEN:
514     case WOW_TYPE_HACCEL:
515     case WOW_TYPE_FULLHWND:
516         if ( HIWORD(handle ) )
517                 ERR( "handle %p of type %d has non-zero HIWORD\n", handle, type );
518         return LOWORD(handle);
519
520     case WOW_TYPE_HMETAFILE:
521         FIXME( "conversion of metafile handles not supported yet\n" );
522         return LOWORD(handle);
523
524     case WOW_TYPE_HTASK:
525         return TASK_GetTaskFromThread( (DWORD)handle );
526
527     default:
528         ERR( "handle %p of unknown type %d\n", handle, type );
529         return LOWORD(handle);
530     }
531 }
532
533 /**********************************************************************
534  *           K32WOWCallback16Ex         (KERNEL32.55)
535  */
536 BOOL WINAPI K32WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
537                                 DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
538 {
539 #ifdef __i386__
540     /*
541      * Arguments must be prepared in the correct order by the caller
542      * (both for PASCAL and CDECL calling convention), so we simply
543      * copy them to the 16-bit stack ...
544      */
545     char *stack = (char *)CURRENT_STACK16 - cbArgs;
546
547     memcpy( stack, pArgs, cbArgs );
548
549     if (dwFlags & (WCB16_REGS|WCB16_REGS_LONG))
550     {
551         CONTEXT *context = (CONTEXT *)pdwRetCode;
552
553         if (TRACE_ON(relay))
554         {
555             DWORD count = cbArgs / sizeof(WORD);
556             WORD * wstack = (WORD *)stack;
557
558             DPRINTF("%04x:CallTo16(func=%04x:%04x,ds=%04x",
559                     GetCurrentThreadId(),
560                     context->SegCs, LOWORD(context->Eip), context->SegDs );
561             while (count) DPRINTF( ",%04x", wstack[--count] );
562             DPRINTF(") ss:sp=%04x:%04x",
563                     SELECTOROF(NtCurrentTeb()->WOW32Reserved), OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
564             DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x si=%04x di=%04x bp=%04x es=%04x fs=%04x\n",
565                     (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
566                     (WORD)context->Edx, (WORD)context->Esi, (WORD)context->Edi,
567                     (WORD)context->Ebp, (WORD)context->SegEs, (WORD)context->SegFs );
568             SYSLEVEL_CheckNotLevel( 2 );
569         }
570
571         if (context->EFlags & 0x00020000)  /* v86 mode */
572         {
573             EXCEPTION_REGISTRATION_RECORD frame;
574             frame.Handler = vm86_handler;
575             errno = 0;
576             __wine_push_frame( &frame );
577             __wine_enter_vm86( context );
578             __wine_pop_frame( &frame );
579             if (errno != 0)  /* enter_vm86 will fall with ENOSYS on x64 kernels */
580             {
581                 WARN("__wine_enter_vm86 failed (errno=%d)\n", errno);
582                 if (errno == ENOSYS)
583                     SetLastError(ERROR_NOT_SUPPORTED);
584                 else
585                     SetLastError(ERROR_GEN_FAILURE);
586                 return FALSE;
587             }
588         }
589         else
590         {
591             /* push return address */
592             if (dwFlags & WCB16_REGS_LONG)
593             {
594                 stack -= sizeof(DWORD);
595                 *((DWORD *)stack) = HIWORD(call16_ret_addr);
596                 stack -= sizeof(DWORD);
597                 *((DWORD *)stack) = LOWORD(call16_ret_addr);
598                 cbArgs += 2 * sizeof(DWORD);
599             }
600             else
601             {
602                 stack -= sizeof(SEGPTR);
603                 *((SEGPTR *)stack) = call16_ret_addr;
604                 cbArgs += sizeof(SEGPTR);
605             }
606
607             /*
608              * Start call by checking for pending events.
609              * Note that wine_call_to_16_regs overwrites context stack
610              * pointer so we may modify it here without a problem.
611              */
612             if (get_vm86_teb_info()->dpmi_vif)
613             {
614                 context->SegSs = wine_get_ds();
615                 context->Esp   = (DWORD)stack;
616                 insert_event_check( context );
617                 cbArgs += (DWORD)stack - context->Esp;
618             }
619
620             _EnterWin16Lock();
621             wine_call_to_16_regs( context, cbArgs, call16_handler );
622             _LeaveWin16Lock();
623         }
624
625         if (TRACE_ON(relay))
626         {
627             DPRINTF("%04x:RetFrom16() ss:sp=%04x:%04x ",
628                     GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->WOW32Reserved),
629                     OFFSETOF(NtCurrentTeb()->WOW32Reserved));
630             DPRINTF(" ax=%04x bx=%04x cx=%04x dx=%04x bp=%04x sp=%04x\n",
631                     (WORD)context->Eax, (WORD)context->Ebx, (WORD)context->Ecx,
632                     (WORD)context->Edx, (WORD)context->Ebp, (WORD)context->Esp );
633             SYSLEVEL_CheckNotLevel( 2 );
634         }
635     }
636     else
637     {
638         DWORD ret;
639
640         if (TRACE_ON(relay))
641         {
642             DWORD count = cbArgs / sizeof(WORD);
643             WORD * wstack = (WORD *)stack;
644
645             DPRINTF("%04x:CallTo16(func=%04x:%04x,ds=%04x",
646                     GetCurrentThreadId(), HIWORD(vpfn16), LOWORD(vpfn16),
647                     SELECTOROF(NtCurrentTeb()->WOW32Reserved) );
648             while (count) DPRINTF( ",%04x", wstack[--count] );
649             DPRINTF(") ss:sp=%04x:%04x\n",
650                     SELECTOROF(NtCurrentTeb()->WOW32Reserved), OFFSETOF(NtCurrentTeb()->WOW32Reserved) );
651             SYSLEVEL_CheckNotLevel( 2 );
652         }
653
654         /* push return address */
655         stack -= sizeof(SEGPTR);
656         *((SEGPTR *)stack) = call16_ret_addr;
657         cbArgs += sizeof(SEGPTR);
658
659         /*
660          * Actually, we should take care whether the called routine cleans up
661          * its stack or not.  Fortunately, our wine_call_to_16 core doesn't rely on
662          * the callee to do so; after the routine has returned, the 16-bit
663          * stack pointer is always reset to the position it had before.
664          */
665         _EnterWin16Lock();
666         ret = wine_call_to_16( (FARPROC16)vpfn16, cbArgs, call16_handler );
667         if (pdwRetCode) *pdwRetCode = ret;
668         _LeaveWin16Lock();
669
670         if (TRACE_ON(relay))
671         {
672             DPRINTF("%04x:RetFrom16() ss:sp=%04x:%04x retval=%08x\n",
673                     GetCurrentThreadId(), SELECTOROF(NtCurrentTeb()->WOW32Reserved),
674                     OFFSETOF(NtCurrentTeb()->WOW32Reserved), ret);
675             SYSLEVEL_CheckNotLevel( 2 );
676         }
677     }
678 #else
679     assert(0);  /* cannot call to 16-bit on non-Intel architectures */
680 #endif  /* __i386__ */
681
682     return TRUE;  /* success */
683 }
684
685 /**********************************************************************
686  *           K32WOWCallback16            (KERNEL32.54)
687  */
688 DWORD WINAPI K32WOWCallback16( DWORD vpfn16, DWORD dwParam )
689 {
690     DWORD ret;
691
692     if ( !K32WOWCallback16Ex( vpfn16, WCB16_PASCAL,
693                            sizeof(DWORD), &dwParam, &ret ) )
694         ret = 0L;
695
696     return ret;
697 }