wowthunk.c 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
/*
 * Win32 WOW Generic Thunk API
 *
 * Copyright 1999 Ulrich Weigand 
 */

#include "wine/winbase16.h"
#include "winbase.h"
#include "wownt32.h"
#include "heap.h"
#include "miscemu.h"
#include "syslevel.h"
#include "stackframe.h"
#include "builtin16.h"
#include "debugtools.h"

DEFAULT_DEBUG_CHANNEL(thunk)

/*
 *  32-bit WOW routines (in WOW32, but actually forwarded to KERNEL32)
 */

/**********************************************************************
 *           WOWGetDescriptor        (WOW32.1) (KERNEL32.70)
 */
BOOL WINAPI WOWGetDescriptor( SEGPTR segptr, LPLDT_ENTRY ldtent )
{
    return GetThreadSelectorEntry( GetCurrentThreadId(), 
                                   segptr >> 16, ldtent );
}

/**********************************************************************
 *           WOWGetVDMPointer        (WOW32.5) (KERNEL32.56)
 */
LPVOID WINAPI WOWGetVDMPointer( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
{
    /* FIXME: add size check too */

    if ( fProtectedMode )
        return PTR_SEG_TO_LIN( vp );
    else
        return DOSMEM_MapRealToLinear( vp );
}

/**********************************************************************
 *           WOWGetVDMPointerFix     (WOW32.6) (KERNEL32.68)
 */
LPVOID WINAPI WOWGetVDMPointerFix( DWORD vp, DWORD dwBytes, BOOL fProtectedMode )
{
    /* 
     * Hmmm. According to the docu, we should call:
     *
     *          GlobalFix16( SELECTOROF(vp) );
     *
     * But this is unnecessary under Wine, as we never move global
     * memory segments in linear memory anyway. 
     *
     * (I'm not so sure what we are *supposed* to do if 
     *  fProtectedMode is TRUE, anyway ...)
     */

    return WOWGetVDMPointer( vp, dwBytes, fProtectedMode );
}

/**********************************************************************
 *           WOWGetVDMPointerUnFix   (WOW32.7) (KERNEL32.69)
 */
VOID WINAPI WOWGetVDMPointerUnfix( DWORD vp )
{
    /*
     * See above why we don't call:
     *
     * GlobalUnfix16( SELECTOROF(vp) );
     *
     */
}

/**********************************************************************
 *           WOWGlobalAlloc16        (WOW32.8) (KERNEL32.59)
 */
WORD WINAPI WOWGlobalAlloc16( WORD wFlags, DWORD cb )
{
    return (WORD)GlobalAlloc16( wFlags, cb );
}

/**********************************************************************
 *           WOWGlobalFree16         (WOW32.10) (KERNEL32.62)
 */
WORD WINAPI WOWGlobalFree16( WORD hMem )
{
    return (WORD)GlobalFree16( (HGLOBAL16)hMem );
}

/**********************************************************************
 *           WOWGlobalLock16         (WOW32.11) (KERNEL32.60)
 */
DWORD WINAPI WOWGlobalLock16( WORD hMem )
{
    return (DWORD)WIN16_GlobalLock16( (HGLOBAL16)hMem );
}

/**********************************************************************
 *           WOWGlobalUnlock16       (WOW32.13) (KERNEL32.61)
 */
BOOL WINAPI WOWGlobalUnlock16( WORD hMem )
{
    return (BOOL)GlobalUnlock16( (HGLOBAL16)hMem );
}

/**********************************************************************
 *           WOWGlobalAllocLock16    (WOW32.9) (KERNEL32.63)
 */
DWORD WINAPI WOWGlobalAllocLock16( WORD wFlags, DWORD cb, WORD *phMem )
{
    WORD hMem = WOWGlobalAlloc16( wFlags, cb );
    if (phMem) *phMem = hMem;

    return WOWGlobalLock16( hMem );
}

/**********************************************************************
 *           WOWGlobalLockSize16     (WOW32.12) (KERNEL32.65)
 */
DWORD WINAPI WOWGlobalLockSize16( WORD hMem, PDWORD pcb )
{
    if ( pcb ) 
        *pcb = GlobalSize16( (HGLOBAL16)hMem );

    return WOWGlobalLock16( hMem );
}

/**********************************************************************
 *           WOWGlobalUnlockFree16   (WOW32.14) (KERNEL32.64)
 */
WORD WINAPI WOWGlobalUnlockFree16( DWORD vpMem )
{
    if ( !WOWGlobalUnlock16( HIWORD(vpMem) ) )
        return FALSE;

    return WOWGlobalFree16( HIWORD(vpMem) );
}


/**********************************************************************
 *           WOWYield16              (WOW32.17) (KERNEL32.66)
 */
VOID WINAPI WOWYield16( void )
{
    /*
     * This does the right thing for both Win16 and Win32 tasks.  
     * More or less, at least :-/
     */
    Yield16();
}

/**********************************************************************
 *           WOWDirectedYield16       (WOW32.4) (KERNEL32.67)
 */
VOID WINAPI WOWDirectedYield16( WORD htask16 )
{
    /*
     * Argh.  Our scheduler doesn't like DirectedYield by Win32
     * tasks at all.  So we do hope that this routine is indeed 
     * only ever called by Win16 tasks that have thunked up ...
     */
    DirectedYield16( (HTASK16)htask16 );
}


/***********************************************************************
 *           WOWHandle32              (WOW32.16) (KERNEL32.57)
 */
HANDLE WINAPI WOWHandle32( WORD handle, WOW_HANDLE_TYPE type )
{
    switch ( type )
    {
    case WOW_TYPE_HWND:
    case WOW_TYPE_HMENU:
    case WOW_TYPE_HDWP:
    case WOW_TYPE_HDROP:
    case WOW_TYPE_HDC:
    case WOW_TYPE_HFONT:
    case WOW_TYPE_HMETAFILE:
    case WOW_TYPE_HRGN:
    case WOW_TYPE_HBITMAP:
    case WOW_TYPE_HBRUSH:
    case WOW_TYPE_HPALETTE:
    case WOW_TYPE_HPEN:
    case WOW_TYPE_HACCEL:
    case WOW_TYPE_HTASK:
    case WOW_TYPE_FULLHWND:
        return (HANDLE)handle;

    default:
        ERR( "handle 0x%04x of unknown type %d\n", handle, type );
        return (HANDLE)handle;
    }
}

/***********************************************************************
 *           WOWHandle16              (WOW32.15) (KERNEL32.58)
 */
WORD WINAPI WOWHandle16( HANDLE handle, WOW_HANDLE_TYPE type )
{
    if ( HIWORD(handle ) )
        ERR( "handle 0x%08x of type %d has non-zero HIWORD\n", handle, type );

    switch ( type )
    {
    case WOW_TYPE_HWND:
    case WOW_TYPE_HMENU:
    case WOW_TYPE_HDWP:
    case WOW_TYPE_HDROP:
    case WOW_TYPE_HDC:
    case WOW_TYPE_HFONT:
    case WOW_TYPE_HMETAFILE:
    case WOW_TYPE_HRGN:
    case WOW_TYPE_HBITMAP:
    case WOW_TYPE_HBRUSH:
    case WOW_TYPE_HPALETTE:
    case WOW_TYPE_HPEN:
    case WOW_TYPE_HACCEL:
    case WOW_TYPE_HTASK:
    case WOW_TYPE_FULLHWND:
        return LOWORD(handle);

    default:
        ERR( "handle 0x%08x of unknown type %d\n", handle, type );
        return LOWORD(handle);
    }
}

/**********************************************************************
 *           WOWCallback16            (WOW32.2) (KERNEL32.54)
 */
DWORD WINAPI WOWCallback16( DWORD vpfn16, DWORD dwParam )
{
    DWORD ret;

    if ( !WOWCallback16Ex( vpfn16, WCB16_PASCAL, 
                           sizeof(DWORD), &dwParam, &ret ) )
        ret = 0L;

    return ret;
}

/**********************************************************************
 *           WOWCallback16Ex         (WOW32.3) (KERNEL32.55)
 */
BOOL WINAPI WOWCallback16Ex( DWORD vpfn16, DWORD dwFlags,
                             DWORD cbArgs, LPVOID pArgs, LPDWORD pdwRetCode )
{
    DWORD ret;

    /*
     * Arguments must be prepared in the correct order by the caller
     * (both for PASCAL and CDECL calling convention), so we simply
     * copy them to the 16-bit stack ... 
     */
    memcpy( (LPBYTE)CURRENT_STACK16 - cbArgs, (LPBYTE)pArgs, cbArgs );


    /*
     * Actually, we should take care whether the called routine cleans up
     * its stack or not.  Fortunately, our CallTo16 core doesn't rely on 
     * the callee to do so; after the routine has returned, the 16-bit 
     * stack pointer is always reset to the position it had before. 
     */

    ret = CallTo16Long( (FARPROC16)vpfn16, cbArgs );

    if ( pdwRetCode )
        *pdwRetCode = ret;

    return TRUE;  /* success */
}



/*
 *  16-bit WOW routines (in KERNEL)
 */

/**********************************************************************
 *           GetVDMPointer32W16      (KERNEL.516)
 */
DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
{
    return (DWORD)WOWGetVDMPointer( vp, 0, (DWORD)fMode );
}

/***********************************************************************
 *           LoadLibraryEx32W16      (KERNEL.513)
 */
DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
{
    HMODULE hModule;

    SYSLEVEL_ReleaseWin16Lock();
    hModule = LoadLibraryExA( lpszLibFile, (HANDLE)hFile, dwFlags );
    SYSLEVEL_RestoreWin16Lock();

    return (DWORD)hModule;
}

/***********************************************************************
 *           GetProcAddress32W16     (KERNEL.515)
 */
DWORD WINAPI GetProcAddress32W16( DWORD hModule, LPCSTR lpszProc )
{
    return (DWORD)GetProcAddress( (HMODULE)hModule, lpszProc );
}

/***********************************************************************
 *           FreeLibrary32W16        (KERNEL.514)
 */
DWORD WINAPI FreeLibrary32W16( DWORD hLibModule )
{
    BOOL retv;

    SYSLEVEL_ReleaseWin16Lock();
    retv = FreeLibrary( (HMODULE)hLibModule );
    SYSLEVEL_RestoreWin16Lock();

    return (DWORD)retv;
}


/**********************************************************************
 *           WOW_CallProc32W
 */
static DWORD WOW_CallProc32W16( BOOL Ex )
{
    DWORD nrofargs, argconvmask;
    FARPROC proc32;
    DWORD *args, ret;
    VA_LIST16 valist;
    int i;
    int aix;

    SYSLEVEL_ReleaseWin16Lock();

    VA_START16( valist );
    nrofargs    = VA_ARG16( valist, DWORD );
    argconvmask = VA_ARG16( valist, DWORD );
    proc32      = VA_ARG16( valist, FARPROC );
    TRACE("(%ld,%ld,%p, Ex%d args[",nrofargs,argconvmask,proc32,Ex);
    args = (DWORD*)HEAP_xalloc( GetProcessHeap(), 0,
                                sizeof(DWORD)*nrofargs );
    /* CallProcEx doesn't need its args reversed */
    for (i=0;i<nrofargs;i++) {
            if (Ex) {
               aix = i;
            } else {
               aix = nrofargs - i - 1;
            }
            if (argconvmask & (1<<i))
            {
                SEGPTR ptr = VA_ARG16( valist, SEGPTR );
                args[aix] = (DWORD)PTR_SEG_TO_LIN(ptr);
                if (TRACE_ON(thunk)) DPRINTF("%08lx(%p),",ptr,PTR_SEG_TO_LIN(ptr));
            }
            else
            {
                args[aix] = VA_ARG16( valist, DWORD );
                if (TRACE_ON(thunk)) DPRINTF("%ld,",args[aix]);
            }
    }
    if (TRACE_ON(thunk)) DPRINTF("])\n");
    VA_END16( valist );

    /*
     * FIXME:  If ( nrofargs & CPEX_DEST_CDECL ) != 0, we should call a
     *         32-bit CDECL routine ...
     */

    if (!proc32) ret = 0;
    else switch (nrofargs)
    {
    case 0: ret = proc32();
            break;
    case 1: ret = proc32(args[0]);
            break;
    case 2: ret = proc32(args[0],args[1]);
            break;
    case 3: ret = proc32(args[0],args[1],args[2]);
            break;
    case 4: ret = proc32(args[0],args[1],args[2],args[3]);
            break;
    case 5: ret = proc32(args[0],args[1],args[2],args[3],args[4]);
            break;
    case 6: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5]);
            break;
    case 7: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
            break;
    case 8: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
            break;
    case 9: ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
            break;
    case 10:ret = proc32(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
            break;
    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]);
            break;
    default:
            /* FIXME: should go up to 32  arguments */
            ERR("Unsupported number of arguments %ld, please report.\n",nrofargs);
            ret = 0;
            break;
    }
    /* POP nrofargs DWORD arguments and 3 DWORD parameters */
    if (!Ex) stack16_pop( (3 + nrofargs) * sizeof(DWORD) );

    TRACE("returns %08lx\n",ret);
    HeapFree( GetProcessHeap(), 0, args );

    SYSLEVEL_RestoreWin16Lock();

    return ret;
}

/**********************************************************************
 *           CallProc32W16           (KERNEL.517)
 *
 * DWORD PASCAL CallProc32W( DWORD p1, ... , DWORD lpProcAddress,
 *                           DWORD fAddressConvert, DWORD cParams );
 */
DWORD WINAPI CallProc32W16( void )
{
    return WOW_CallProc32W16( FALSE );
}

/**********************************************************************
 *           CallProcEx32W16         (KERNEL.518)
 *
 * DWORD CallProcEx32W( DWORD cParams, DWORD fAddressConvert, 
 *                      DWORD lpProcAddress, DWORD p1, ... );
 */
DWORD WINAPI CallProcEx32W16( void )
{
    return WOW_CallProc32W16( TRUE );
}


/**********************************************************************
 *           WOW16Call               (KERNEL.501)
 *
 * FIXME!!!
 *
 */
DWORD WINAPI WOW16Call(WORD x,WORD y,WORD z) 
{
        int     i;
        DWORD   calladdr;
        VA_LIST16 args;
        FIXME("(0x%04x,0x%04x,%d),calling (",x,y,z);

        VA_START16(args);
        for (i=0;i<x/2;i++) {
                WORD    a = VA_ARG16(args,WORD);
                DPRINTF("%04x ",a);
        }
        calladdr = VA_ARG16(args,DWORD);
        VA_END16(args);
        stack16_pop( x + sizeof(DWORD) );
        DPRINTF(") calling address was 0x%08lx\n",calladdr);
        return 0;
}