thunk.c 79.2 KB
Newer Older
1 2 3 4 5 6 7
/*
 * KERNEL32 thunks and other undocumented stuff
 *
 * Copyright 1996, 1997 Alexandre Julliard
 * Copyright 1997, 1998 Marcus Meissner
 * Copyright 1998       Ulrich Weigand
 *
8 9 10 11 12 13 14 15 16 17 18 19
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22
 */

23
#include "config.h"
24
#include "wine/port.h"
25

26 27
#include <string.h>
#include <sys/types.h>
28
#include <stdarg.h>
29
#include <stdio.h>
30 31 32
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
33 34 35 36

#include "windef.h"
#include "winbase.h"
#include "winerror.h"
37
#include "winternl.h"
38
#include "wownt32.h"
39 40
#include "wine/winbase16.h"

41
#include "wine/debug.h"
42
#include "wine/library.h"
43
#include "kernel16_private.h"
44

45
WINE_DEFAULT_DEBUG_CHANNEL(thunk);
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
struct ThunkDataCommon
{
    char                   magic[4];         /* 00 */
    DWORD                  checksum;         /* 04 */
};

struct ThunkDataLS16
{
    struct ThunkDataCommon common;           /* 00 */
    SEGPTR                 targetTable;      /* 08 */
    DWORD                  firstTime;        /* 0C */
};

struct ThunkDataLS32
{
    struct ThunkDataCommon common;           /* 00 */
    DWORD *                targetTable;      /* 08 */
    char                   lateBinding[4];   /* 0C */
    DWORD                  flags;            /* 10 */
    DWORD                  reserved1;        /* 14 */
    DWORD                  reserved2;        /* 18 */
    DWORD                  offsetQTThunk;    /* 1C */
    DWORD                  offsetFTProlog;   /* 20 */
};

struct ThunkDataSL16
{
    struct ThunkDataCommon common;            /* 00 */
    DWORD                  flags1;            /* 08 */
    DWORD                  reserved1;         /* 0C */
    struct ThunkDataSL *   fpData;            /* 10 */
    SEGPTR                 spData;            /* 14 */
    DWORD                  reserved2;         /* 18 */
    char                   lateBinding[4];    /* 1C */
    DWORD                  flags2;            /* 20 */
    DWORD                  reserved3;         /* 20 */
    SEGPTR                 apiDatabase;       /* 28 */
};

struct ThunkDataSL32
{
    struct ThunkDataCommon common;            /* 00 */
    DWORD                  reserved1;         /* 08 */
    struct ThunkDataSL *   data;              /* 0C */
    char                   lateBinding[4];    /* 10 */
    DWORD                  flags;             /* 14 */
    DWORD                  reserved2;         /* 18 */
    DWORD                  reserved3;         /* 1C */
    DWORD                  offsetTargetTable; /* 20 */
};

struct ThunkDataSL
{
#if 0
    This structure differs from the Win95 original,
    but this should not matter since it is strictly internal to
    the thunk handling routines in KRNL386 / KERNEL32.

    For reference, here is the Win95 layout:

    struct ThunkDataCommon common;            /* 00 */
    DWORD                  flags1;            /* 08 */
    SEGPTR                 apiDatabase;       /* 0C */
    WORD                   exePtr;            /* 10 */
    WORD                   segMBA;            /* 12 */
    DWORD                  lenMBATotal;       /* 14 */
    DWORD                  lenMBAUsed;        /* 18 */
    DWORD                  flags2;            /* 1C */
    char                   pszDll16[256];     /* 20 */
    char                   pszDll32[256];     /*120 */

    We do it differently since all our thunk handling is done
119
    by 32-bit code. Therefore we do not need to provide
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
    easy access to this data, especially the process target
    table database, for 16-bit code.
#endif

    struct ThunkDataCommon common;
    DWORD                  flags1;
    struct SLApiDB *       apiDB;
    struct SLTargetDB *    targetDB;
    DWORD                  flags2;
    char                   pszDll16[256];
    char                   pszDll32[256];
};

struct SLTargetDB
{
     struct SLTargetDB *   next;
     DWORD                 process;
     DWORD *               targetTable;
};

struct SLApiDB
{
    DWORD                  nrArgBytes;
    DWORD                  errorReturnValue;
};

146 147 148
SEGPTR CALL32_CBClient_RetAddr = 0;
SEGPTR CALL32_CBClientEx_RetAddr = 0;

149 150
extern int call_entry_point( void *func, int nb_args, const DWORD *args );
extern void __wine_call_from_16_thunk(void);
151 152 153 154
extern void FT_Prolog(void);
extern void FT_PrologPrime(void);
extern void QT_Thunk(void);
extern void QT_ThunkPrime(void);
155

156
/* Push a DWORD on the 32-bit stack */
157
static inline void stack32_push( CONTEXT *context, DWORD val )
158 159 160 161 162 163
{
    context->Esp -= sizeof(DWORD);
    *(DWORD *)context->Esp = val;
}

/* Pop a DWORD from the 32-bit stack */
164
static inline DWORD stack32_pop( CONTEXT *context )
165 166 167 168 169 170
{
    DWORD ret = *(DWORD *)context->Esp;
    context->Esp += sizeof(DWORD);
    return ret;
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/***********************************************************************
 *                                                                     *
 *                 Win95 internal thunks                               *
 *                                                                     *
 ***********************************************************************/

/***********************************************************************
 *           LogApiThk    (KERNEL.423)
 */
void WINAPI LogApiThk( LPSTR func )
{
    TRACE( "%s\n", debugstr_a(func) );
}

/***********************************************************************
 *           LogApiThkLSF    (KERNEL32.42)
187
 *
188 189
 * NOTE: needs to preserve all registers!
 */
190
void WINAPI __regs_LogApiThkLSF( LPSTR func, CONTEXT *context )
191 192 193
{
    TRACE( "%s\n", debugstr_a(func) );
}
194
DEFINE_REGS_ENTRYPOINT( LogApiThkLSF, 1 )
195 196 197

/***********************************************************************
 *           LogApiThkSL    (KERNEL32.44)
198
 *
199 200
 * NOTE: needs to preserve all registers!
 */
201
void WINAPI __regs_LogApiThkSL( LPSTR func, CONTEXT *context )
202 203 204
{
    TRACE( "%s\n", debugstr_a(func) );
}
205
DEFINE_REGS_ENTRYPOINT( LogApiThkSL, 1 )
206 207 208

/***********************************************************************
 *           LogCBThkSL    (KERNEL32.47)
209
 *
210 211
 * NOTE: needs to preserve all registers!
 */
212
void WINAPI __regs_LogCBThkSL( LPSTR func, CONTEXT *context )
213 214 215
{
    TRACE( "%s\n", debugstr_a(func) );
}
216
DEFINE_REGS_ENTRYPOINT( LogCBThkSL, 1 )
217 218 219

/***********************************************************************
 * Generates a FT_Prolog call.
220
 *
221 222 223 224 225 226 227 228 229 230 231 232
 *  0FB6D1                  movzbl edx,cl
 *  8B1495xxxxxxxx	    mov edx,[4*edx + targetTable]
 *  68xxxxxxxx		    push FT_Prolog
 *  C3			    lret
 */
static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
	LPBYTE	x;

	x	= relayCode;
	*x++	= 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
	*x++	= 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
	x+=4;	/* mov edx, [4*edx + targetTable] */
233
	*x++	= 0x68; *(void **)x = FT_Prolog;
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
	x+=4; 	/* push FT_Prolog */
	*x++	= 0xC3;		/* lret */
	/* fill rest with 0xCC / int 3 */
}

/***********************************************************************
 *	_write_qtthunk					(internal)
 * Generates a QT_Thunk style call.
 *
 *  33C9                    xor ecx, ecx
 *  8A4DFC                  mov cl , [ebp-04]
 *  8B148Dxxxxxxxx          mov edx, [4*ecx + targetTable]
 *  B8yyyyyyyy              mov eax, QT_Thunk
 *  FFE0                    jmp eax
 */
static void _write_qtthunk(
	LPBYTE relayCode,	/* [in] start of QT_Thunk stub */
	DWORD *targetTable	/* [in] start of thunk (for index lookup) */
) {
	LPBYTE	x;

	x	= relayCode;
	*x++	= 0x33;*x++=0xC9; /* xor ecx,ecx */
	*x++	= 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
	*x++	= 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
	x+=4;	/* mov edx, [4*ecx + targetTable */
260
	*x++	= 0xB8; *(void **)x = QT_Thunk;
261 262 263 264 265 266 267 268
	x+=4; 	/* mov eax , QT_Thunk */
	*x++	= 0xFF; *x++ = 0xE0;	/* jmp eax */
	/* should fill the rest of the 32 bytes with 0xCC */
}

/***********************************************************************
 *           _loadthunk
 */
269
static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
270 271 272
                         struct ThunkDataCommon *TD32, DWORD checksum)
{
    struct ThunkDataCommon *TD16;
273
    HMODULE16 hmod;
274
    int ordinal;
275
    static BOOL done;
276 277 278 279 280 281 282

    if (!done)
    {
        LoadLibrary16( "gdi.exe" );
        LoadLibrary16( "user.exe" );
        done = TRUE;
    }
283

284
    if ((hmod = LoadLibrary16(module)) <= 32)
285 286 287 288 289 290 291
    {
        ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
                   module, func, module32, module, hmod);
        return 0;
    }

    if (   !(ordinal = NE_GetOrdinal(hmod, func))
292
        || !(TD16 = MapSL((SEGPTR)NE_GetEntryPointEx(hmod, ordinal, FALSE))))
293 294 295 296 297 298 299 300 301
    {
        ERR("Unable to find thunk data '%s' in %s, required by %s (conflicting/incorrect DLL versions !?).\n",
                   func, module, module32);
        return 0;
    }

    if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
    {
        ERR("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
302
                   module, func, module32,
303 304 305 306 307 308 309
                   TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
                   TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
        return 0;
    }

    if (TD32 && TD16->checksum != TD32->checksum)
    {
310
        ERR("(%s, %s, %s): Wrong checksum %08x (should be %08x)\n",
311 312 313 314 315 316
                   module, func, module32, TD16->checksum, TD32->checksum);
        return 0;
    }

    if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
    {
317
        ERR("(%s, %s, %s): Wrong checksum %08x (should be %08x)\n",
318 319 320 321 322 323 324 325 326 327
                   module, func, module32, *(LPDWORD)TD16, checksum);
        return 0;
    }

    return TD16;
}

/***********************************************************************
 *           GetThunkStuff    (KERNEL32.53)
 */
328
LPVOID WINAPI GetThunkStuff(LPCSTR module, LPCSTR func)
329 330 331 332 333 334 335 336 337 338 339 340 341 342
{
    return _loadthunk(module, func, "<kernel>", NULL, 0L);
}

/***********************************************************************
 *           GetThunkBuff    (KERNEL32.52)
 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
 */
LPVOID WINAPI GetThunkBuff(void)
{
    return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
}

/***********************************************************************
343
 *		ThunkConnect32		(KERNEL32.@)
344 345
 * Connects a 32bit and a 16bit thunkbuffer.
 */
346
UINT WINAPI ThunkConnect32(
347 348 349 350 351 352 353 354 355 356 357 358 359
	struct ThunkDataCommon *TD,  /* [in/out] thunkbuffer */
	LPSTR thunkfun16,            /* [in] win16 thunkfunction */
	LPSTR module16,              /* [in] name of win16 dll */
	LPSTR module32,              /* [in] name of win32 dll */
	HMODULE hmod32,            /* [in] hmodule of win32 dll */
	DWORD dwReason               /* [in] initialisation argument */
) {
    BOOL directionSL;

    if (!strncmp(TD->magic, "SL01", 4))
    {
        directionSL = TRUE;

360
        TRACE("SL01 thunk %s (%p) <- %s (%s), Reason: %d\n",
361
              module32, TD, module16, thunkfun16, dwReason);
362 363 364 365 366
    }
    else if (!strncmp(TD->magic, "LS01", 4))
    {
        directionSL = FALSE;

367
        TRACE("LS01 thunk %s (%p) -> %s (%s), Reason: %d\n",
368
              module32, TD, module16, thunkfun16, dwReason);
369 370 371
    }
    else
    {
372
        ERR("Invalid magic %c%c%c%c\n",
373 374 375
                   TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
        return 0;
    }
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
    switch (dwReason)
    {
        case DLL_PROCESS_ATTACH:
        {
            struct ThunkDataCommon *TD16;
            if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
                return 0;

            if (directionSL)
            {
                struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
                struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
                struct SLTargetDB *tdb;

                if (SL16->fpData == NULL)
                {
                    ERR("ThunkConnect16 was not called!\n");
                    return 0;
                }

                SL32->data = SL16->fpData;

                tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
                tdb->process = GetCurrentProcessId();
                tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);

                tdb->next = SL32->data->targetDB;   /* FIXME: not thread-safe! */
                SL32->data->targetDB = tdb;

406
                TRACE("Process %08x allocated TargetDB entry for ThunkDataSL %p\n",
407
                      GetCurrentProcessId(), SL32->data);
408 409 410 411 412 413
            }
            else
            {
                struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
                struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;

414
                LS32->targetTable = MapSL(LS16->targetTable);
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

                /* write QT_Thunk and FT_Prolog stubs */
                _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk,  LS32->targetTable);
                _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
            }
            break;
        }

        case DLL_PROCESS_DETACH:
            /* FIXME: cleanup */
            break;
    }

    return 1;
}

/**********************************************************************
432
 * 		QT_Thunk			(KERNEL32.@)
433 434
 *
 * The target address is in EDX.
435
 * The 16bit arguments start at ESP.
436
 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
437 438 439 440 441 442 443 444 445
 * So the stack layout is 16bit argument bytes and then the 64 byte
 * scratch buffer.
 * The scratch buffer is used as work space by Windows' QT_Thunk
 * function.
 * As the programs unfortunately don't always provide a fixed size
 * scratch buffer (danger, stack corruption ahead !!), we simply resort
 * to copying over the whole EBP-ESP range to the 16bit stack
 * (as there's no way to safely figure out the param count
 * due to this misbehaviour of some programs).
446
 * [ok]
447 448 449 450 451 452
 *
 * See DDJ article 9614c for a very good description of QT_Thunk (also
 * available online !).
 *
 * FIXME: DDJ talks of certain register usage rules; I'm not sure
 * whether we cover this 100%.
453
 */
454
void WINAPI __regs_QT_Thunk( CONTEXT *context )
455
{
456
    CONTEXT context16;
457 458
    DWORD argsize;

459
    context16 = *context;
460

461 462
    context16.SegFs = wine_get_fs();
    context16.SegGs = wine_get_gs();
463 464
    context16.SegCs = HIWORD(context->Edx);
    context16.Eip   = LOWORD(context->Edx);
465 466
    /* point EBP to the STACK16FRAME on the stack
     * for the call_to_16 to set up the register content on calling */
467
    context16.Ebp   = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
468

469 470 471 472 473 474 475 476 477 478
    /*
     * used to be (problematic):
     * argsize = context->Ebp - context->Esp - 0x40;
     * due to some programs abusing the API, we better assume the full
     * EBP - ESP range for copying instead: */
    argsize = context->Ebp - context->Esp;

    /* ok, too much is insane; let's limit param count a bit again */
    if (argsize > 64)
	argsize = 64; /* 32 WORDs */
479

480
    WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
481 482 483
    context->Eax = context16.Eax;
    context->Edx = context16.Edx;
    context->Ecx = context16.Ecx;
484

485 486 487
    /* make sure to update the Win32 ESP, too, in order to throw away
     * the number of parameters that the Win16 function
     * accepted (that it popped from the corresponding Win16 stack) */
488
    context->Esp +=   LOWORD(context16.Esp) -
489
                        ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
490
}
491
DEFINE_REGS_ENTRYPOINT( QT_Thunk, 0 )
492 493 494


/**********************************************************************
495
 * 		FT_Prolog			(KERNEL32.@)
496
 *
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
 * The set of FT_... thunk routines is used instead of QT_Thunk,
 * if structures have to be converted from 32-bit to 16-bit
 * (change of member alignment, conversion of members).
 *
 * The thunk function (as created by the thunk compiler) calls
 * FT_Prolog at the beginning, to set up a stack frame and
 * allocate a 64 byte buffer on the stack.
 * The input parameters (target address and some flags) are
 * saved for later use by FT_Thunk.
 *
 * Input:  EDX  16-bit target address (SEGPTR)
 *         CX   bits  0..7   target number (in target table)
 *              bits  8..9   some flags (unclear???)
 *              bits 10..15  number of DWORD arguments
 *
 * Output: A new stackframe is created, and a 64 byte buffer
513
 *         allocated on the stack. The layout of the stack
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
 *         on return is as follows:
 *
 *  (ebp+4)  return address to caller of thunk function
 *  (ebp)    old EBP
 *  (ebp-4)  saved EBX register of caller
 *  (ebp-8)  saved ESI register of caller
 *  (ebp-12) saved EDI register of caller
 *  (ebp-16) saved ECX register, containing flags
 *  (ebp-20) bitmap containing parameters that are to be converted
 *           by FT_Thunk; it is initialized to 0 by FT_Prolog and
 *           filled in by the thunk code before calling FT_Thunk
 *  (ebp-24)
 *    ...    (unclear)
 *  (ebp-44)
 *  (ebp-48) saved EAX register of caller (unclear, never restored???)
 *  (ebp-52) saved EDX register, containing 16-bit thunk target
 *  (ebp-56)
 *    ...    (unclear)
 *  (ebp-64)
 *
 *  ESP is EBP-64 after return.
535
 *
536
 */
537
void WINAPI __regs_FT_Prolog( CONTEXT *context )
538 539
{
    /* Build stack frame */
540 541
    stack32_push(context, context->Ebp);
    context->Ebp = context->Esp;
542 543

    /* Allocate 64-byte Thunk Buffer */
544 545
    context->Esp -= 64;
    memset((char *)context->Esp, '\0', 64);
546 547 548

    /* Store Flags (ECX) and Target Address (EDX) */
    /* Save other registers to be restored later */
549 550 551 552
    *(DWORD *)(context->Ebp -  4) = context->Ebx;
    *(DWORD *)(context->Ebp -  8) = context->Esi;
    *(DWORD *)(context->Ebp - 12) = context->Edi;
    *(DWORD *)(context->Ebp - 16) = context->Ecx;
553

554 555
    *(DWORD *)(context->Ebp - 48) = context->Eax;
    *(DWORD *)(context->Ebp - 52) = context->Edx;
556
}
557
DEFINE_REGS_ENTRYPOINT( FT_Prolog, 0 )
558 559

/**********************************************************************
560
 * 		FT_Thunk			(KERNEL32.@)
561
 *
562
 * This routine performs the actual call to 16-bit code,
563 564 565 566 567 568 569 570
 * similar to QT_Thunk. The differences are:
 *  - The call target is taken from the buffer created by FT_Prolog
 *  - Those arguments requested by the thunk code (by setting the
 *    corresponding bit in the bitmap at EBP-20) are converted
 *    from 32-bit pointers to segmented pointers (those pointers
 *    are guaranteed to point to structures copied to the stack
 *    by the thunk code, so we always use the 16-bit stack selector
 *    for those addresses).
571
 *
572 573
 *    The bit #i of EBP-20 corresponds here to the DWORD starting at
 *    ESP+4 + 2*i.
574 575
 *
 * FIXME: It is unclear what happens if there are more than 32 WORDs
576 577 578
 *        of arguments, so that the single DWORD bitmap is no longer
 *        sufficient ...
 */
579
void WINAPI __regs_FT_Thunk( CONTEXT *context )
580
{
581 582
    DWORD mapESPrelative = *(DWORD *)(context->Ebp - 20);
    DWORD callTarget     = *(DWORD *)(context->Ebp - 52);
583

584
    CONTEXT context16;
585
    DWORD i, argsize;
586 587
    DWORD newstack[32];
    LPBYTE oldstack;
588

589
    context16 = *context;
590

591 592
    context16.SegFs = wine_get_fs();
    context16.SegGs = wine_get_gs();
593 594
    context16.SegCs = HIWORD(callTarget);
    context16.Eip   = LOWORD(callTarget);
595
    context16.Ebp   = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
596

597
    argsize  = context->Ebp-context->Esp-0x40;
598
    if (argsize > sizeof(newstack)) argsize = sizeof(newstack);
599
    oldstack = (LPBYTE)context->Esp;
600 601 602 603 604 605

    memcpy( newstack, oldstack, argsize );

    for (i = 0; i < 32; i++)	/* NOTE: What about > 32 arguments? */
	if (mapESPrelative & (1 << i))
	{
606
	    SEGPTR *arg = (SEGPTR *)newstack[i];
607 608
	    *arg = MAKESEGPTR(SELECTOROF(NtCurrentTeb()->WOW32Reserved),
                              OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize
609
                              + (*(LPBYTE *)arg - oldstack));
610 611
	}

612
    WOWCallback16Ex( 0, WCB16_REGS, argsize, newstack, (DWORD *)&context16 );
613 614 615
    context->Eax = context16.Eax;
    context->Edx = context16.Edx;
    context->Ecx = context16.Ecx;
616

617
    context->Esp +=   LOWORD(context16.Esp) -
618
                        ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
619 620 621 622

    /* Copy modified buffers back to 32-bit stack */
    memcpy( oldstack, newstack, argsize );
}
623
DEFINE_REGS_ENTRYPOINT( FT_Thunk, 0 )
624

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
/***********************************************************************
 *		FT_Exit0 (KERNEL32.@)
 *		FT_Exit4 (KERNEL32.@)
 *		FT_Exit8 (KERNEL32.@)
 *		FT_Exit12 (KERNEL32.@)
 *		FT_Exit16 (KERNEL32.@)
 *		FT_Exit20 (KERNEL32.@)
 *		FT_Exit24 (KERNEL32.@)
 *		FT_Exit28 (KERNEL32.@)
 *		FT_Exit32 (KERNEL32.@)
 *		FT_Exit36 (KERNEL32.@)
 *		FT_Exit40 (KERNEL32.@)
 *		FT_Exit44 (KERNEL32.@)
 *		FT_Exit48 (KERNEL32.@)
 *		FT_Exit52 (KERNEL32.@)
 *		FT_Exit56 (KERNEL32.@)
641 642 643
 *
 * One of the FT_ExitNN functions is called at the end of the thunk code.
 * It removes the stack frame created by FT_Prolog, moves the function
644 645
 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
 * value, but the thunk code has moved it from EAX to EBX in the
646 647 648 649
 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
 * and perform a return to the CALLER of the thunk code (while removing
 * the given number of arguments from the caller's stack).
 */
650 651 652 653 654 655
#define FT_EXIT_RESTORE_REGS \
    "movl %ebx,%eax\n\t" \
    "movl -4(%ebp),%ebx\n\t" \
    "movl -8(%ebp),%esi\n\t" \
    "movl -12(%ebp),%edi\n\t" \
    "leave\n\t"
656

657
#define DEFINE_FT_Exit(n) \
658
    __ASM_STDCALL_FUNC( FT_Exit ## n, 0, FT_EXIT_RESTORE_REGS "ret $" #n )
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

DEFINE_FT_Exit(0)
DEFINE_FT_Exit(4)
DEFINE_FT_Exit(8)
DEFINE_FT_Exit(12)
DEFINE_FT_Exit(16)
DEFINE_FT_Exit(20)
DEFINE_FT_Exit(24)
DEFINE_FT_Exit(28)
DEFINE_FT_Exit(32)
DEFINE_FT_Exit(36)
DEFINE_FT_Exit(40)
DEFINE_FT_Exit(44)
DEFINE_FT_Exit(48)
DEFINE_FT_Exit(52)
DEFINE_FT_Exit(56)
675 676 677 678


/***********************************************************************
 * 		ThunkInitLS 	(KERNEL32.43)
679
 * A thunkbuffer link routine
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
 * The thunkbuf looks like:
 *
 *	00: DWORD	length		? don't know exactly
 *	04: SEGPTR	ptr		? where does it point to?
 * The pointer ptr is written into the first DWORD of 'thunk'.
 * (probably correctly implemented)
 * [ok probably]
 * RETURNS
 *	segmented pointer to thunk?
 */
DWORD WINAPI ThunkInitLS(
	LPDWORD thunk,	/* [in] win32 thunk */
	LPCSTR thkbuf,	/* [in] thkbuffer name in win16 dll */
	DWORD len,	/* [in] thkbuffer length */
	LPCSTR dll16,	/* [in] name of win16 dll */
	LPCSTR dll32	/* [in] name of win32 dll (FIXME: not used?) */
) {
	LPDWORD		addr;

	if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
		return 0;

	if (!addr[1])
		return 0;
704
	*thunk = addr[1];
705 706 707 708 709 710

	return addr[1];
}

/***********************************************************************
 * 		Common32ThkLS 	(KERNEL32.45)
711
 *
712
 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
713
 * style thunks. The basic difference is that the parameter conversion
714 715 716 717
 * is done completely on the *16-bit* side here. Thus we do not call
 * the 16-bit target directly, but call a common entry point instead.
 * This entry function then calls the target according to the target
 * number passed in the DI register.
718
 *
719 720 721 722 723 724 725 726 727 728 729 730
 * Input:  EAX    SEGPTR to the common 16-bit entry point
 *         CX     offset in thunk table (target number * 4)
 *         DX     error return value if execution fails (unclear???)
 *         EDX.HI number of DWORD parameters
 *
 * (Note that we need to move the thunk table offset from CX to DI !)
 *
 * The called 16-bit stub expects its stack to look like this:
 *     ...
 *   (esp+40)  32-bit arguments
 *     ...
 *   (esp+8)   32 byte of stack space available as buffer
731 732
 *   (esp)     8 byte return address for use with 0x66 lret
 *
733 734
 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
 * and uses the EAX register to return a DWORD return value.
735
 * Thus we need to use a special assembly glue routine
736 737
 * (CallRegisterLongProc instead of CallRegisterShortProc).
 *
738
 * Finally, we return to the caller, popping the arguments off
739 740
 * the stack.  The number of arguments to be popped is returned
 * in the BL register by the called 16-bit routine.
741 742
 *
 */
743
void WINAPI __regs_Common32ThkLS( CONTEXT *context )
744
{
745
    CONTEXT context16;
746 747
    DWORD argsize;

748
    context16 = *context;
749

750 751
    context16.SegFs = wine_get_fs();
    context16.SegGs = wine_get_gs();
752 753 754
    context16.Edi   = LOWORD(context->Ecx);
    context16.SegCs = HIWORD(context->Eax);
    context16.Eip   = LOWORD(context->Eax);
755
    context16.Ebp   = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
756

757
    argsize = HIWORD(context->Edx) * 4;
758 759

    /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
760
    if (context->Edx == context->Eip)
761 762
        argsize = 6 * 4;

763 764 765 766 767
    /* Note: the first 32 bytes we copy are just garbage from the 32-bit stack, in order to reserve
     *       the space. It is safe to do that since the register function prefix has reserved
     *       a lot more space than that below context->Esp.
     */
    WOWCallback16Ex( 0, WCB16_REGS, argsize + 32, (LPBYTE)context->Esp - 32, (DWORD *)&context16 );
768
    context->Eax = context16.Eax;
769 770

    /* Clean up caller's stack frame */
771
    context->Esp += LOBYTE(context16.Ebx);
772
}
773
DEFINE_REGS_ENTRYPOINT( Common32ThkLS, 0 )
774 775 776 777 778 779 780

/***********************************************************************
 *		OT_32ThkLSF	(KERNEL32.40)
 *
 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
 * argument processing is done on both the 32-bit and the 16-bit side:
 * The 32-bit side prepares arguments, copying them onto the stack.
781 782
 *
 * When this routine is called, the first word on the stack is the
783 784 785
 * number of argument bytes prepared by the 32-bit code, and EDX
 * contains the 16-bit target address.
 *
786
 * The called 16-bit routine is another relaycode, doing further
787 788 789 790 791 792 793
 * argument processing and then calling the real 16-bit target
 * whose address is stored at [bp-04].
 *
 * The call proceeds using a normal CallRegisterShortProc.
 * After return from the 16-bit relaycode, the arguments need
 * to be copied *back* to the 32-bit stack, since the 32-bit
 * relaycode processes output parameters.
794
 *
795 796 797 798 799 800 801
 * Note that we copy twice the number of arguments, since some of the
 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
 * arguments of the caller!
 *
 * (Note that this function seems only to be used for
 *  OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
 */
802
void WINAPI __regs_OT_32ThkLSF( CONTEXT *context )
803
{
804
    CONTEXT context16;
805 806
    DWORD argsize;

807
    context16 = *context;
808

809 810
    context16.SegFs = wine_get_fs();
    context16.SegGs = wine_get_gs();
811 812
    context16.SegCs = HIWORD(context->Edx);
    context16.Eip   = LOWORD(context->Edx);
813
    context16.Ebp   = OFFSETOF(NtCurrentTeb()->WOW32Reserved) + FIELD_OFFSET(STACK16FRAME,bp);
814

815
    argsize = 2 * *(WORD *)context->Esp + 2;
816

817
    WOWCallback16Ex( 0, WCB16_REGS, argsize, (void *)context->Esp, (DWORD *)&context16 );
818 819
    context->Eax = context16.Eax;
    context->Edx = context16.Edx;
820

821
    /* Copy modified buffers back to 32-bit stack */
822
    memcpy( (LPBYTE)context->Esp,
823
            (LPBYTE)CURRENT_STACK16 - argsize, argsize );
824

825
    context->Esp +=   LOWORD(context16.Esp) -
826
                        ( OFFSETOF(NtCurrentTeb()->WOW32Reserved) - argsize );
827
}
828
DEFINE_REGS_ENTRYPOINT( OT_32ThkLSF, 0 )
829 830 831 832 833 834

/***********************************************************************
 *		ThunkInitLSF		(KERNEL32.41)
 * A thunk setup routine.
 * Expects a pointer to a preinitialized thunkbuffer in the first argument
 * looking like:
Jon Griffiths's avatar
Jon Griffiths committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
 *|	00..03:		unknown	(pointer, check _41, _43, _46)
 *|	04: EB1E		jmp +0x20
 *|
 *|	06..23:		unknown (space for replacement code, check .90)
 *|
 *|	24:>E800000000		call offset 29
 *|	29:>58			pop eax		   ( target of call )
 *|	2A: 2D25000000		sub eax,0x00000025 ( now points to offset 4 )
 *|	2F: BAxxxxxxxx		mov edx,xxxxxxxx
 *|	34: 68yyyyyyyy		push KERNEL32.90
 *|	39: C3			ret
 *|
 *|	3A: EB1E		jmp +0x20
 *|	3E ... 59:	unknown (space for replacement code?)
 *|	5A: E8xxxxxxxx		call <32bitoffset xxxxxxxx>
 *|	5F: 5A			pop edx
 *|	60: 81EA25xxxxxx	sub edx, 0x25xxxxxx
 *|	66: 52			push edx
 *|	67: 68xxxxxxxx		push xxxxxxxx
 *|	6C: 68yyyyyyyy		push KERNEL32.89
 *|	71: C3			ret
 *|	72: end?
857 858 859 860
 * This function checks if the code is there, and replaces the yyyyyyyy entries
 * by the functionpointers.
 * The thunkbuf looks like:
 *
Jon Griffiths's avatar
Jon Griffiths committed
861 862
 *|	00: DWORD	length		? don't know exactly
 *|	04: SEGPTR	ptr		? where does it point to?
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
 * The segpointer ptr is written into the first DWORD of 'thunk'.
 * [ok probably]
 * RETURNS
 *	unclear, pointer to win16 thkbuffer?
 */
LPVOID WINAPI ThunkInitLSF(
	LPBYTE thunk,	/* [in] win32 thunk */
	LPCSTR thkbuf,	/* [in] thkbuffer name in win16 dll */
	DWORD len,	/* [in] length of thkbuffer */
	LPCSTR dll16,	/* [in] name of win16 dll */
	LPCSTR dll32	/* [in] name of win32 dll */
) {
	LPDWORD		addr,addr2;

	/* FIXME: add checks for valid code ... */
	/* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
879 880
	*(void **)(thunk+0x35) = QT_ThunkPrime;
	*(void **)(thunk+0x6D) = FT_PrologPrime;
881

882 883 884
	if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
		return 0;

885
	addr2 = MapSL(addr[1]);
886 887 888 889 890 891 892 893
	if (HIWORD(addr2))
		*(DWORD*)thunk = (DWORD)addr2;

	return addr2;
}

/***********************************************************************
 *		FT_PrologPrime			(KERNEL32.89)
894
 *
895
 * This function is called from the relay code installed by
896
 * ThunkInitLSF. It replaces the location from where it was
897 898 899
 * called by a standard FT_Prolog call stub (which is 'primed'
 * by inserting the correct target table pointer).
 * Finally, it calls that stub.
900
 *
901
 * Input:  ECX    target number + flags (passed through to FT_Prolog)
902
 *        (ESP)   offset of location where target table pointer
903 904 905
 *                is stored, relative to the start of the relay code
 *        (ESP+4) pointer to start of relay code
 *                (this is where the FT_Prolog call stub gets written to)
906
 *
907
 * Note: The two DWORD arguments get popped off the stack.
908
 *
909
 */
910
void WINAPI __regs_FT_PrologPrime( CONTEXT *context )
911 912 913 914 915 916
{
    DWORD  targetTableOffset;
    LPBYTE relayCode;

    /* Compensate for the fact that the Wine register relay code thought
       we were being called, although we were in fact jumped to */
917
    context->Esp -= 4;
918 919 920 921 922 923 924

    /* Write FT_Prolog call stub */
    targetTableOffset = stack32_pop(context);
    relayCode = (LPBYTE)stack32_pop(context);
    _write_ftprolog( relayCode, *(DWORD **)(relayCode+targetTableOffset) );

    /* Jump to the call stub just created */
925
    context->Eip = (DWORD)relayCode;
926
}
927
DEFINE_REGS_ENTRYPOINT( FT_PrologPrime, 0 )
928 929 930 931

/***********************************************************************
 *		QT_ThunkPrime			(KERNEL32.90)
 *
932
 * This function corresponds to FT_PrologPrime, but installs a
933 934 935 936 937
 * call stub for QT_Thunk instead.
 *
 * Input: (EBP-4) target number (passed through to QT_Thunk)
 *         EDX    target table pointer location offset
 *         EAX    start of relay code
938
 *
939
 */
940
void WINAPI __regs_QT_ThunkPrime( CONTEXT *context )
941 942 943 944 945 946
{
    DWORD  targetTableOffset;
    LPBYTE relayCode;

    /* Compensate for the fact that the Wine register relay code thought
       we were being called, although we were in fact jumped to */
947
    context->Esp -= 4;
948 949

    /* Write QT_Thunk call stub */
950 951
    targetTableOffset = context->Edx;
    relayCode = (LPBYTE)context->Eax;
952 953 954
    _write_qtthunk( relayCode, *(DWORD **)(relayCode+targetTableOffset) );

    /* Jump to the call stub just created */
955
    context->Eip = (DWORD)relayCode;
956
}
957
DEFINE_REGS_ENTRYPOINT( QT_ThunkPrime, 0 )
958 959 960 961 962 963 964 965

/***********************************************************************
 *		ThunkInitSL (KERNEL32.46)
 * Another thunkbuf link routine.
 * The start of the thunkbuf looks like this:
 * 	00: DWORD	length
 *	04: SEGPTR	address for thunkbuffer pointer
 * [ok probably]
966 967 968
 *
 * RETURNS
 *  Nothing.
969 970 971 972 973 974 975 976 977 978 979 980 981
 */
VOID WINAPI ThunkInitSL(
	LPBYTE thunk,		/* [in] start of thunkbuffer */
	LPCSTR thkbuf,		/* [in] name/ordinal of thunkbuffer in win16 dll */
	DWORD len,		/* [in] length of thunkbuffer */
	LPCSTR dll16,		/* [in] name of win16 dll containing the thkbuf */
	LPCSTR dll32		/* [in] win32 dll. FIXME: strange, unused */
) {
	LPDWORD		addr;

	if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
		return;

982
	*(DWORD*)MapSL(addr[1]) = (DWORD)thunk;
983 984 985
}

/**********************************************************************
986
 *           SSInit		(KERNEL.700)
987 988 989
 * RETURNS
 *	TRUE for success.
 */
990
BOOL WINAPI SSInit16(void)
991 992 993 994 995
{
    return TRUE;
}

/**********************************************************************
996
 *           SSOnBigStack	(KERNEL32.87)
997 998 999 1000 1001 1002
 * Check if thunking is initialized (ss selector set up etc.)
 * We do that differently, so just return TRUE.
 * [ok]
 * RETURNS
 *	TRUE for success.
 */
1003
BOOL WINAPI SSOnBigStack(void)
1004 1005 1006 1007 1008 1009
{
    TRACE("Yes, thunking is initialized\n");
    return TRUE;
}

/**********************************************************************
1010
 *           SSConfirmSmallStack     (KERNEL.704)
1011 1012 1013 1014 1015
 *
 * Abort if not on small stack.
 *
 * This must be a register routine as it has to preserve *all* registers.
 */
1016
void WINAPI SSConfirmSmallStack( CONTEXT *context )
1017 1018 1019 1020 1021
{
    /* We are always on the small stack while in 16-bit code ... */
}

/**********************************************************************
1022
 *           SSCall (KERNEL32.88)
1023 1024 1025 1026 1027
 * One of the real thunking functions. This one seems to be for 32<->32
 * thunks. It should probably be capable of crossing processboundaries.
 *
 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
 * [ok]
1028 1029 1030
 *
 * RETURNS
 *  Thunked function result.
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
 */
DWORD WINAPIV SSCall(
	DWORD nr,	/* [in] number of argument bytes */
	DWORD flags,	/* [in] FIXME: flags ? */
	FARPROC fun,	/* [in] function to call */
	...		/* [in/out] arguments */
) {
    DWORD i,ret;
    DWORD *args = ((DWORD *)&fun) + 1;

    if(TRACE_ON(thunk))
    {
1043
      DPRINTF("(%d,0x%08x,%p,[",nr,flags,fun);
1044
      for (i=0;i<nr/4;i++)
1045
          DPRINTF("0x%08x,",args[i]);
1046 1047
      DPRINTF("])\n");
    }
1048
    ret = call_entry_point( fun, nr / sizeof(DWORD), args );
1049
    TRACE(" returning %d ...\n",ret);
1050 1051 1052 1053 1054 1055
    return ret;
}

/**********************************************************************
 *           W32S_BackTo32                      (KERNEL32.51)
 */
1056
void WINAPI __regs_W32S_BackTo32( CONTEXT *context )
1057
{
1058 1059
    LPDWORD stack = (LPDWORD)context->Esp;
    FARPROC proc = (FARPROC)context->Eip;
1060

1061
    context->Eax = call_entry_point( proc, 10, stack + 1 );
1062
    context->Eip = stack32_pop(context);
1063
}
1064
DEFINE_REGS_ENTRYPOINT( W32S_BackTo32, 0 )
1065 1066

/**********************************************************************
1067
 *			AllocSLCallback		(KERNEL32.@)
1068
 *
1069 1070
 * Allocate a 16->32 callback.
 *
Jon Griffiths's avatar
Jon Griffiths committed
1071
 * NOTES
1072 1073 1074
 * Win95 uses some structchains for callbacks. It allocates them
 * in blocks of 100 entries, size 32 bytes each, layout:
 * blockstart:
Jon Griffiths's avatar
Jon Griffiths committed
1075 1076 1077 1078
 *| 	0:	PTR	nextblockstart
 *|	4:	entry	*first;
 *|	8:	WORD	sel ( start points to blockstart)
 *|	A:	WORD	unknown
1079
 * 100xentry:
Jon Griffiths's avatar
Jon Griffiths committed
1080 1081 1082
 *|	00..17:		Code
 *|	18:	PDB	*owning_process;
 *|	1C:	PTR	blockstart
1083 1084 1085 1086 1087 1088 1089
 *
 * We ignore this for now. (Just a note for further developers)
 * FIXME: use this method, so we don't waste selectors...
 *
 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
 * the 0x66 prefix switches from word->long registers.
 *
Jon Griffiths's avatar
Jon Griffiths committed
1090 1091 1092 1093
 *|	665A		pop	edx
 *|	6668x arg2 x 	pushl	<arg2>
 *|	6652		push	edx
 *|	EAx arg1 x	jmpf	<arg1>
1094 1095 1096
 *
 * returns the startaddress of this thunk.
 *
Jon Griffiths's avatar
Jon Griffiths committed
1097
 * Note, that they look very similar to the ones allocates by THUNK_Alloc.
1098
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
1099
 *	A segmented pointer to the start of the thunk
1100 1101 1102
 */
DWORD WINAPI
AllocSLCallback(
Jon Griffiths's avatar
Jon Griffiths committed
1103 1104
	DWORD finalizer,	/* [in] Finalizer function */
	DWORD callback		/* [in] Callback function */
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
) {
	LPBYTE	x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
	WORD	sel;

	x=thunk;
	*x++=0x66;*x++=0x5a;				/* popl edx */
	*x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4;	/* pushl finalizer */
	*x++=0x66;*x++=0x52;				/* pushl edx */
	*x++=0xea;*(DWORD*)x=callback;x+=4;		/* jmpf callback */

	*(DWORD*)(thunk+18) = GetCurrentProcessId();

1117
	sel = SELECTOR_AllocBlock( thunk, 32, WINE_LDT_FLAGS_CODE );
1118 1119 1120 1121
	return (sel<<16)|0;
}

/**********************************************************************
1122
 * 		FreeSLCallback		(KERNEL32.@)
1123
 * Frees the specified 16->32 callback
1124 1125 1126
 *
 * RETURNS
 *  Nothing.
1127 1128 1129 1130 1131
 */
void WINAPI
FreeSLCallback(
	DWORD x	/* [in] 16 bit callback (segmented pointer?) */
) {
1132
	FIXME("(0x%08x): stub\n",x);
1133 1134
}

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
/**********************************************************************
 * 		AllocMappedBuffer	(KERNEL32.38)
 *
 * This is an undocumented KERNEL32 function that
 * SMapLS's a GlobalAlloc'ed buffer.
 *
 * RETURNS
 *       EDI register: pointer to buffer
 *
 * NOTES
 *       The buffer is preceded by 8 bytes:
 *        ...
 *       edi+0   buffer
 *       edi-4   SEGPTR to buffer
 *       edi-8   some magic Win95 needs for SUnMapLS
 *               (we use it for the memory handle)
 *
 *       The SEGPTR is used by the caller!
 */
void WINAPI __regs_AllocMappedBuffer(
1155
              CONTEXT *context /* [in] EDI register: size of buffer to allocate */
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
) {
    HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
    DWORD *buffer = GlobalLock(handle);
    DWORD ptr = 0;

    if (buffer)
        if (!(ptr = MapLS(buffer + 2)))
        {
            GlobalUnlock(handle);
            GlobalFree(handle);
        }

    if (!ptr)
        context->Eax = context->Edi = 0;
    else
    {
        buffer[0] = (DWORD)handle;
        buffer[1] = ptr;

        context->Eax = ptr;
        context->Edi = (DWORD)(buffer + 2);
    }
}
1179
DEFINE_REGS_ENTRYPOINT( AllocMappedBuffer, 0 )
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

/**********************************************************************
 * 		FreeMappedBuffer	(KERNEL32.39)
 *
 * Free a buffer allocated by AllocMappedBuffer
 *
 * RETURNS
 *  Nothing.
 */
void WINAPI __regs_FreeMappedBuffer(
1190
              CONTEXT *context /* [in] EDI register: pointer to buffer */
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
) {
    if (context->Edi)
    {
        DWORD *buffer = (DWORD *)context->Edi - 2;

        UnMapLS(buffer[1]);

        GlobalUnlock((HGLOBAL)buffer[0]);
        GlobalFree((HGLOBAL)buffer[0]);
    }
}
1202
DEFINE_REGS_ENTRYPOINT( FreeMappedBuffer, 0 )
1203 1204 1205 1206 1207

/**********************************************************************
 * 		GetTEBSelectorFS	(KERNEL.475)
 * 	Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
 */
1208
void WINAPI GetTEBSelectorFS16(void)
1209
{
1210
    CURRENT_STACK16->fs = wine_get_fs();
1211 1212 1213
}

/**********************************************************************
1214
 * 		IsPeFormat		(KERNEL.431)
Jon Griffiths's avatar
Jon Griffiths committed
1215 1216 1217
 *
 * Determine if a file is a PE format executable.
 *
1218 1219
 * RETURNS
 *  TRUE, if it is.
Jon Griffiths's avatar
Jon Griffiths committed
1220 1221 1222 1223
 *  FALSE if the file could not be opened or is not a PE file.
 *
 * NOTES
 *  If fn is given as NULL then the function expects hf16 to be valid.
1224 1225
 */
BOOL16 WINAPI IsPeFormat16(
1226
	LPSTR	fn,	/* [in] Filename to the executable */
Jon Griffiths's avatar
Jon Griffiths committed
1227 1228
	HFILE16 hf16)	/* [in] An open file handle */
{
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
    BOOL ret = FALSE;
    IMAGE_DOS_HEADER mzh;
    OFSTRUCT ofs;
    DWORD xmagic;

    if (fn) hf16 = OpenFile16(fn,&ofs,OF_READ);
    if (hf16 == HFILE_ERROR16) return FALSE;
    _llseek16(hf16,0,SEEK_SET);
    if (sizeof(mzh)!=_lread16(hf16,&mzh,sizeof(mzh))) goto done;
    if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) goto done;
    _llseek16(hf16,mzh.e_lfanew,SEEK_SET);
    if (sizeof(DWORD)!=_lread16(hf16,&xmagic,sizeof(DWORD))) goto done;
    ret = (xmagic == IMAGE_NT_SIGNATURE);
 done:
    _lclose16(hf16);
    return ret;
1245 1246 1247 1248
}


/***********************************************************************
1249
 *           K32Thk1632Prolog			(KERNEL32.@)
1250
 */
1251
void WINAPI __regs_K32Thk1632Prolog( CONTEXT *context )
1252
{
1253
   LPBYTE code = (LPBYTE)context->Eip - 5;
1254 1255 1256 1257 1258 1259

   /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
      of 16->32 thunks instead of using one of the standard methods!
      This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
      and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
      Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1260
      bypassed, which means it will crash the next time the 32-bit OLE
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
      code thunks down again to 16-bit (this *will* happen!).

      The following hack tries to recognize this situation.
      This is possible since the called stubs in OLECLI32/OLESVR32 all
      look exactly the same:
        00   E8xxxxxxxx    call K32Thk1632Prolog
        05   FF55FC        call [ebp-04]
        08   E8xxxxxxxx    call K32Thk1632Epilog
        0D   66CB          retf

      If we recognize this situation, we try to simulate the actions
      of our CallTo/CallFrom mechanism by copying the 16-bit stack
1273 1274
      to our 32-bit stack, creating a proper STACK16FRAME and
      updating cur_stack. */
1275 1276 1277 1278

   if (   code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
       && code[13] == 0x66 && code[14] == 0xCB)
   {
1279 1280
      DWORD argSize = context->Ebp - context->Esp;
      char *stack16 = (char *)context->Esp - 4;
1281
      STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1282
      STACK32FRAME *frame32 = NtCurrentTeb()->WOW32Reserved;
1283 1284 1285
      char *stack32 = (char *)frame32 - argSize;
      WORD  stackSel  = SELECTOROF(frame32->frame16);
      DWORD stackBase = GetSelectorBase(stackSel);
1286

1287
      TRACE("before SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1288
            context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1289 1290

      memset(frame16, '\0', sizeof(STACK16FRAME));
1291
      frame16->frame32 = frame32;
1292
      frame16->ebp = context->Ebp;
1293 1294

      memcpy(stack32, stack16, argSize);
1295
      NtCurrentTeb()->WOW32Reserved = (void *)MAKESEGPTR(stackSel, (DWORD)frame16 - stackBase);
1296

1297 1298
      context->Esp = (DWORD)stack32 + 4;
      context->Ebp = context->Esp + argSize;
1299

1300
      TRACE("after  SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1301
            context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1302 1303
   }

1304 1305 1306
    /* entry_point is never used again once the entry point has
       been called.  Thus we re-use it to hold the Win16Lock count */
   ReleaseThunkLock(&CURRENT_STACK16->entry_point);
1307
}
1308
DEFINE_REGS_ENTRYPOINT( K32Thk1632Prolog, 0 )
1309 1310

/***********************************************************************
1311
 *           K32Thk1632Epilog			(KERNEL32.@)
1312
 */
1313
void WINAPI __regs_K32Thk1632Epilog( CONTEXT *context )
1314
{
1315
   LPBYTE code = (LPBYTE)context->Eip - 13;
1316

1317
   RestoreThunkLock(CURRENT_STACK16->entry_point);
1318 1319 1320 1321 1322 1323

   /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */

   if (   code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
       && code[13] == 0x66 && code[14] == 0xCB)
   {
1324
      STACK16FRAME *frame16 = MapSL((SEGPTR)NtCurrentTeb()->WOW32Reserved);
1325 1326 1327 1328
      char *stack16 = (char *)(frame16 + 1);
      DWORD argSize = frame16->ebp - (DWORD)stack16;
      char *stack32 = (char *)frame16->frame32 - argSize;

1329
      DWORD nArgsPopped = context->Esp - (DWORD)stack32;
1330

1331
      TRACE("before SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1332
            context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1333

1334
      NtCurrentTeb()->WOW32Reserved = frame16->frame32;
1335

1336 1337
      context->Esp = (DWORD)stack16 + nArgsPopped;
      context->Ebp = frame16->ebp;
1338

1339
      TRACE("after  SYSTHUNK hack: EBP: %08x ESP: %08x cur_stack: %p\n",
1340
            context->Ebp, context->Esp, NtCurrentTeb()->WOW32Reserved);
1341 1342
   }
}
1343
DEFINE_REGS_ENTRYPOINT( K32Thk1632Epilog, 0 )
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361

/*********************************************************************
 *                   PK16FNF [KERNEL32.91]
 *
 *  This routine fills in the supplied 13-byte (8.3 plus terminator)
 *  string buffer with the 8.3 filename of a recently loaded 16-bit
 *  module.  It is unknown exactly what modules trigger this
 *  mechanism or what purpose this serves.  Win98 Explorer (and
 *  probably also Win95 with IE 4 shell integration) calls this
 *  several times during initialization.
 *
 *  FIXME: find out what this really does and make it work.
 */
void WINAPI PK16FNF(LPSTR strPtr)
{
       FIXME("(%p): stub\n", strPtr);

       /* fill in a fake filename that'll be easy to recognize */
1362
       strcpy(strPtr, "WINESTUB.FIX");
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
}

/***********************************************************************
 * 16->32 Flat Thunk routines:
 */

/***********************************************************************
 *              ThunkConnect16          (KERNEL.651)
 * Connects a 32bit and a 16bit thunkbuffer.
 */
UINT WINAPI ThunkConnect16(
        LPSTR module16,              /* [in] name of win16 dll */
        LPSTR module32,              /* [in] name of win32 dll */
        HINSTANCE16 hInst16,         /* [in] hInst of win16 dll */
        DWORD dwReason,              /* [in] initialisation argument */
        struct ThunkDataCommon *TD,  /* [in/out] thunkbuffer */
        LPSTR thunkfun32,            /* [in] win32 thunkfunction */
        WORD cs                      /* [in] CS of win16 dll */
) {
    BOOL directionSL;

    if (!strncmp(TD->magic, "SL01", 4))
    {
        directionSL = TRUE;

1388
        TRACE("SL01 thunk %s (%p) -> %s (%s), Reason: %d\n",
1389
              module16, TD, module32, thunkfun32, dwReason);
1390 1391 1392 1393 1394
    }
    else if (!strncmp(TD->magic, "LS01", 4))
    {
        directionSL = FALSE;

1395
        TRACE("LS01 thunk %s (%p) <- %s (%s), Reason: %d\n",
1396
              module16, TD, module32, thunkfun32, dwReason);
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
    }
    else
    {
        ERR("Invalid magic %c%c%c%c\n",
            TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
        return 0;
    }

    switch (dwReason)
    {
        case DLL_PROCESS_ATTACH:
            if (directionSL)
            {
                struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD;
                struct ThunkDataSL   *SL   = SL16->fpData;

                if (SL == NULL)
                {
                    SL = HeapAlloc(GetProcessHeap(), 0, sizeof(*SL));

                    SL->common   = SL16->common;
                    SL->flags1   = SL16->flags1;
                    SL->flags2   = SL16->flags2;

1421
                    SL->apiDB    = MapSL(SL16->apiDatabase);
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
                    SL->targetDB = NULL;

                    lstrcpynA(SL->pszDll16, module16, 255);
                    lstrcpynA(SL->pszDll32, module32, 255);

                    /* We should create a SEGPTR to the ThunkDataSL,
                       but since the contents are not in the original format,
                       any access to this by 16-bit code would crash anyway. */
                    SL16->spData = 0;
                    SL16->fpData = SL;
                }


                if (SL->flags2 & 0x80000000)
                {
                    TRACE("Preloading 32-bit library\n");
                    LoadLibraryA(module32);
                }
            }
            else
            {
                /* nothing to do */
            }
            break;

        case DLL_PROCESS_DETACH:
            /* FIXME: cleanup */
            break;
    }

    return 1;
}


/***********************************************************************
 *           C16ThkSL                           (KERNEL.630)
 */

1460
void WINAPI C16ThkSL(CONTEXT *context)
1461
{
1462
    LPBYTE stub = MapSL(context->Eax), x = stub;
1463 1464
    WORD cs = wine_get_cs();
    WORD ds = wine_get_ds();
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475

    /* We produce the following code:
     *
     *   mov ax, __FLATDS
     *   mov es, ax
     *   movzx ecx, cx
     *   mov edx, es:[ecx + $EDX]
     *   push bp
     *   push edx
     *   push dx
     *   push edx
1476
     *   call __FLATCS:__wine_call_from_16_thunk
1477 1478
     */

1479
    *x++ = 0xB8; *(WORD *)x = ds; x += sizeof(WORD);
1480 1481 1482
    *x++ = 0x8E; *x++ = 0xC0;
    *x++ = 0x66; *x++ = 0x0F; *x++ = 0xB7; *x++ = 0xC9;
    *x++ = 0x67; *x++ = 0x66; *x++ = 0x26; *x++ = 0x8B;
1483
                 *x++ = 0x91; *(DWORD *)x = context->Edx; x += sizeof(DWORD);
1484 1485 1486 1487 1488

    *x++ = 0x55;
    *x++ = 0x66; *x++ = 0x52;
    *x++ = 0x52;
    *x++ = 0x66; *x++ = 0x52;
1489 1490 1491
    *x++ = 0x66; *x++ = 0x9A;
    *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
    *(WORD *)x = cs; x += sizeof(WORD);
1492 1493

    /* Jump to the stub code just created */
1494 1495
    context->Eip = LOWORD(context->Eax);
    context->SegCs  = HIWORD(context->Eax);
1496 1497 1498

    /* Since C16ThkSL got called by a jmp, we need to leave the
       original return address on the stack */
1499
    context->Esp -= 4;
1500 1501 1502 1503 1504 1505
}

/***********************************************************************
 *           C16ThkSL01                         (KERNEL.631)
 */

1506
void WINAPI C16ThkSL01(CONTEXT *context)
1507
{
1508
    LPBYTE stub = MapSL(context->Eax), x = stub;
1509 1510 1511

    if (stub)
    {
1512
        struct ThunkDataSL16 *SL16 = MapSL(context->Edx);
1513 1514
        struct ThunkDataSL *td = SL16->fpData;

1515
        DWORD procAddress = (DWORD)GetProcAddress16(GetModuleHandle16("KERNEL"), (LPCSTR)631);
1516
        WORD cs = wine_get_cs();
1517 1518 1519 1520 1521 1522 1523

        if (!td)
        {
            ERR("ThunkConnect16 was not called!\n");
            return;
        }

1524
        TRACE("Creating stub for ThunkDataSL %p\n", td);
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535


        /* We produce the following code:
         *
         *   xor eax, eax
         *   mov edx, $td
         *   call C16ThkSL01
         *   push bp
         *   push edx
         *   push dx
         *   push edx
1536
         *   call __FLATCS:__wine_call_from_16_thunk
1537 1538 1539
         */

        *x++ = 0x66; *x++ = 0x33; *x++ = 0xC0;
1540 1541
        *x++ = 0x66; *x++ = 0xBA; *(void **)x = td; x += sizeof(void *);
        *x++ = 0x9A; *(DWORD *)x = procAddress; x += sizeof(DWORD);
1542 1543 1544 1545 1546

        *x++ = 0x55;
        *x++ = 0x66; *x++ = 0x52;
        *x++ = 0x52;
        *x++ = 0x66; *x++ = 0x52;
1547 1548 1549
        *x++ = 0x66; *x++ = 0x9A;
        *(void **)x = __wine_call_from_16_thunk; x += sizeof(void *);
        *(WORD *)x = cs; x += sizeof(WORD);
1550 1551

        /* Jump to the stub code just created */
1552 1553
        context->Eip = LOWORD(context->Eax);
        context->SegCs  = HIWORD(context->Eax);
1554 1555

        /* Since C16ThkSL01 got called by a jmp, we need to leave the
1556
           original return address on the stack */
1557
        context->Esp -= 4;
1558 1559 1560
    }
    else
    {
1561
        struct ThunkDataSL *td = (struct ThunkDataSL *)context->Edx;
1562
        DWORD targetNr = LOWORD(context->Ecx) / 4;
1563 1564
        struct SLTargetDB *tdb;

1565
        TRACE("Process %08x calling target %d of ThunkDataSL %p\n",
1566
              GetCurrentProcessId(), targetNr, td);
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583

        for (tdb = td->targetDB; tdb; tdb = tdb->next)
            if (tdb->process == GetCurrentProcessId())
                break;

        if (!tdb)
        {
            TRACE("Loading 32-bit library %s\n", td->pszDll32);
            LoadLibraryA(td->pszDll32);

            for (tdb = td->targetDB; tdb; tdb = tdb->next)
                if (tdb->process == GetCurrentProcessId())
                    break;
        }

        if (tdb)
        {
1584
            context->Edx = tdb->targetTable[targetNr];
1585

1586
            TRACE("Call target is %08x\n", context->Edx);
1587 1588 1589
        }
        else
        {
1590
            WORD *stack = MapSL( MAKESEGPTR(context->SegSs, LOWORD(context->Esp)) );
1591 1592
            context->Edx = (context->Edx & ~0xffff) | HIWORD(td->apiDB[targetNr].errorReturnValue);
            context->Eax = (context->Eax & ~0xffff) | LOWORD(td->apiDB[targetNr].errorReturnValue);
1593 1594 1595
            context->Eip = stack[2];
            context->SegCs  = stack[3];
            context->Esp += td->apiDB[targetNr].nrArgBytes + 4;
1596

1597
            ERR("Process %08x did not ThunkConnect32 %s to %s\n",
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
                GetCurrentProcessId(), td->pszDll32, td->pszDll16);
        }
    }
}


/***********************************************************************
 * 16<->32 Thunklet/Callback API:
 */

#include "pshpack1.h"
typedef struct _THUNKLET
{
    BYTE        prefix_target;
    BYTE        pushl_target;
    DWORD       target;

    BYTE        prefix_relay;
    BYTE        pushl_relay;
    DWORD       relay;

    BYTE        jmp_glue;
    DWORD       glue;

    BYTE        type;
    HINSTANCE16 owner;
    struct _THUNKLET *next;
} THUNKLET;
#include "poppack.h"

#define THUNKLET_TYPE_LS  1
#define THUNKLET_TYPE_SL  2

static HANDLE  ThunkletHeap = 0;
1632
static WORD ThunkletCodeSel;
1633 1634 1635 1636 1637 1638 1639 1640
static THUNKLET *ThunkletAnchor = NULL;

static FARPROC ThunkletSysthunkGlueLS = 0;
static SEGPTR    ThunkletSysthunkGlueSL = 0;

static FARPROC ThunkletCallbackGlueLS = 0;
static SEGPTR    ThunkletCallbackGlueSL = 0;

1641 1642

/* map a thunk allocated on ThunkletHeap to a 16-bit pointer */
1643
static inline SEGPTR get_segptr( void *thunk )
1644
{
1645
    if (!thunk) return 0;
1646 1647 1648
    return MAKESEGPTR( ThunkletCodeSel, (char *)thunk - (char *)ThunkletHeap );
}

1649 1650 1651
/***********************************************************************
 *           THUNK_Init
 */
1652
static BOOL THUNK_Init(void)
1653 1654 1655
{
    LPBYTE thunk;

1656
    ThunkletHeap = HeapCreate( HEAP_CREATE_ENABLE_EXECUTE, 0x10000, 0x10000 );
1657 1658
    if (!ThunkletHeap) return FALSE;

1659
    ThunkletCodeSel = SELECTOR_AllocBlock( ThunkletHeap, 0x10000, WINE_LDT_FLAGS_CODE );
1660

1661 1662
    thunk = HeapAlloc( ThunkletHeap, 0, 5 );
    if (!thunk) return FALSE;
1663

1664 1665 1666 1667
    ThunkletSysthunkGlueLS = (FARPROC)thunk;
    *thunk++ = 0x58;                             /* popl eax */
    *thunk++ = 0xC3;                             /* ret      */

1668
    ThunkletSysthunkGlueSL = get_segptr( thunk );
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
    *thunk++ = 0x66; *thunk++ = 0x58;            /* popl eax */
    *thunk++ = 0xCB;                             /* lret     */

    return TRUE;
}

/***********************************************************************
 *     SetThunkletCallbackGlue             (KERNEL.560)
 */
void WINAPI SetThunkletCallbackGlue16( FARPROC glueLS, SEGPTR glueSL )
{
    ThunkletCallbackGlueLS = glueLS;
    ThunkletCallbackGlueSL = glueSL;
}


/***********************************************************************
 *     THUNK_FindThunklet
 */
1688 1689
static THUNKLET *THUNK_FindThunklet( DWORD target, DWORD relay,
                                     DWORD glue, BYTE type )
1690
{
1691
    THUNKLET *thunk;
1692 1693 1694 1695

    for (thunk = ThunkletAnchor; thunk; thunk = thunk->next)
        if (    thunk->type   == type
             && thunk->target == target
1696
             && thunk->relay  == relay
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
             && ( type == THUNKLET_TYPE_LS ?
                    ( thunk->glue == glue - (DWORD)&thunk->type )
                  : ( thunk->glue == glue ) ) )
            return thunk;

     return NULL;
}

/***********************************************************************
 *     THUNK_AllocLSThunklet
 */
1708 1709
static FARPROC THUNK_AllocLSThunklet( SEGPTR target, DWORD relay,
                                      FARPROC glue, HTASK16 owner )
1710 1711 1712 1713 1714
{
    THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
                                          THUNKLET_TYPE_LS );
    if (!thunk)
    {
1715
        TDB *pTask = GlobalLock16( owner );
1716

1717
        if (!ThunkletHeap) THUNK_Init();
1718 1719 1720 1721 1722 1723 1724 1725
        if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
            return 0;

        thunk->prefix_target = thunk->prefix_relay = 0x90;
        thunk->pushl_target  = thunk->pushl_relay  = 0x68;
        thunk->jmp_glue = 0xE9;

        thunk->target  = (DWORD)target;
1726
        thunk->relay   = relay;
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
        thunk->glue    = (DWORD)glue - (DWORD)&thunk->type;

        thunk->type    = THUNKLET_TYPE_LS;
        thunk->owner   = pTask? pTask->hInstance : 0;

        thunk->next    = ThunkletAnchor;
        ThunkletAnchor = thunk;
    }

    return (FARPROC)thunk;
}

/***********************************************************************
 *     THUNK_AllocSLThunklet
 */
1742 1743
static SEGPTR THUNK_AllocSLThunklet( FARPROC target, DWORD relay,
                                     SEGPTR glue, HTASK16 owner )
1744 1745 1746 1747 1748
{
    THUNKLET *thunk = THUNK_FindThunklet( (DWORD)target, relay, (DWORD)glue,
                                          THUNKLET_TYPE_SL );
    if (!thunk)
    {
1749
        TDB *pTask = GlobalLock16( owner );
1750

1751
        if (!ThunkletHeap) THUNK_Init();
1752 1753 1754 1755 1756 1757 1758 1759
        if ( !(thunk = HeapAlloc( ThunkletHeap, 0, sizeof(THUNKLET) )) )
            return 0;

        thunk->prefix_target = thunk->prefix_relay = 0x66;
        thunk->pushl_target  = thunk->pushl_relay  = 0x68;
        thunk->jmp_glue = 0xEA;

        thunk->target  = (DWORD)target;
1760
        thunk->relay   = relay;
1761 1762 1763 1764 1765 1766 1767 1768 1769
        thunk->glue    = (DWORD)glue;

        thunk->type    = THUNKLET_TYPE_SL;
        thunk->owner   = pTask? pTask->hInstance : 0;

        thunk->next    = ThunkletAnchor;
        ThunkletAnchor = thunk;
    }

1770
    return get_segptr( thunk );
1771 1772 1773 1774 1775
}

/**********************************************************************
 *     IsLSThunklet
 */
1776
static BOOL16 IsLSThunklet( THUNKLET *thunk )
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
{
    return    thunk->prefix_target == 0x90 && thunk->pushl_target == 0x68
           && thunk->prefix_relay  == 0x90 && thunk->pushl_relay  == 0x68
           && thunk->jmp_glue == 0xE9 && thunk->type == THUNKLET_TYPE_LS;
}

/**********************************************************************
 *     IsSLThunklet                        (KERNEL.612)
 */
BOOL16 WINAPI IsSLThunklet16( THUNKLET *thunk )
{
    return    thunk->prefix_target == 0x66 && thunk->pushl_target == 0x68
           && thunk->prefix_relay  == 0x66 && thunk->pushl_relay  == 0x68
           && thunk->jmp_glue == 0xEA && thunk->type == THUNKLET_TYPE_SL;
}



/***********************************************************************
 *     AllocLSThunkletSysthunk             (KERNEL.607)
 */
1798
FARPROC WINAPI AllocLSThunkletSysthunk16( SEGPTR target,
1799 1800
                                          FARPROC relay, DWORD dummy )
{
1801
    if (!ThunkletSysthunkGlueLS) THUNK_Init();
1802
    return THUNK_AllocLSThunklet( (SEGPTR)relay, (DWORD)target,
1803 1804 1805 1806 1807 1808
                                  ThunkletSysthunkGlueLS, GetCurrentTask() );
}

/***********************************************************************
 *     AllocSLThunkletSysthunk             (KERNEL.608)
 */
1809
SEGPTR WINAPI AllocSLThunkletSysthunk16( FARPROC target,
1810 1811
                                       SEGPTR relay, DWORD dummy )
{
1812
    if (!ThunkletSysthunkGlueSL) THUNK_Init();
1813
    return THUNK_AllocSLThunklet( (FARPROC)relay, (DWORD)target,
1814 1815 1816 1817 1818 1819 1820
                                  ThunkletSysthunkGlueSL, GetCurrentTask() );
}


/***********************************************************************
 *     AllocLSThunkletCallbackEx           (KERNEL.567)
 */
1821
FARPROC WINAPI AllocLSThunkletCallbackEx16( SEGPTR target,
1822 1823
                                            DWORD relay, HTASK16 task )
{
1824
    THUNKLET *thunk = MapSL( target );
1825 1826
    if ( !thunk ) return NULL;

1827
    if (   IsSLThunklet16( thunk ) && thunk->relay == relay
1828 1829 1830
        && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
        return (FARPROC)thunk->target;

1831
    return THUNK_AllocLSThunklet( target, relay,
1832 1833 1834 1835 1836 1837
                                  ThunkletCallbackGlueLS, task );
}

/***********************************************************************
 *     AllocSLThunkletCallbackEx           (KERNEL.568)
 */
1838
SEGPTR WINAPI AllocSLThunkletCallbackEx16( FARPROC target,
1839 1840 1841 1842 1843
                                         DWORD relay, HTASK16 task )
{
    THUNKLET *thunk = (THUNKLET *)target;
    if ( !thunk ) return 0;

1844
    if (   IsLSThunklet( thunk ) && thunk->relay == relay
1845 1846 1847
        && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
        return (SEGPTR)thunk->target;

1848
    return THUNK_AllocSLThunklet( target, relay,
1849 1850 1851 1852
                                  ThunkletCallbackGlueSL, task );
}

/***********************************************************************
1853
 *     AllocLSThunkletCallback             (KERNEL.561)
1854
 *     AllocLSThunkletCallback_dup         (KERNEL.606)
1855 1856 1857 1858 1859 1860 1861
 */
FARPROC WINAPI AllocLSThunkletCallback16( SEGPTR target, DWORD relay )
{
    return AllocLSThunkletCallbackEx16( target, relay, GetCurrentTask() );
}

/***********************************************************************
1862
 *     AllocSLThunkletCallback             (KERNEL.562)
1863
 *     AllocSLThunkletCallback_dup         (KERNEL.605)
1864 1865 1866 1867 1868 1869 1870
 */
SEGPTR WINAPI AllocSLThunkletCallback16( FARPROC target, DWORD relay )
{
    return AllocSLThunkletCallbackEx16( target, relay, GetCurrentTask() );
}

/***********************************************************************
1871
 *     FindLSThunkletCallback              (KERNEL.563)
1872
 *     FindLSThunkletCallback_dup          (KERNEL.609)
1873 1874 1875
 */
FARPROC WINAPI FindLSThunkletCallback( SEGPTR target, DWORD relay )
{
1876
    THUNKLET *thunk = MapSL( target );
1877
    if (   thunk && IsSLThunklet16( thunk ) && thunk->relay == relay
1878 1879 1880
        && thunk->glue == (DWORD)ThunkletCallbackGlueSL )
        return (FARPROC)thunk->target;

1881 1882
    thunk = THUNK_FindThunklet( (DWORD)target, relay,
                                (DWORD)ThunkletCallbackGlueLS,
1883 1884 1885 1886 1887
                                THUNKLET_TYPE_LS );
    return (FARPROC)thunk;
}

/***********************************************************************
1888
 *     FindSLThunkletCallback              (KERNEL.564)
1889
 *     FindSLThunkletCallback_dup          (KERNEL.610)
1890 1891 1892 1893
 */
SEGPTR WINAPI FindSLThunkletCallback( FARPROC target, DWORD relay )
{
    THUNKLET *thunk = (THUNKLET *)target;
1894
    if (   thunk && IsLSThunklet( thunk ) && thunk->relay == relay
1895 1896 1897
        && thunk->glue == (DWORD)ThunkletCallbackGlueLS - (DWORD)&thunk->type )
        return (SEGPTR)thunk->target;

1898 1899
    thunk = THUNK_FindThunklet( (DWORD)target, relay,
                                (DWORD)ThunkletCallbackGlueSL,
1900
                                THUNKLET_TYPE_SL );
1901
    return get_segptr( thunk );
1902 1903 1904 1905
}


/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1906
 *     FreeThunklet            (KERNEL.611)
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
 */
BOOL16 WINAPI FreeThunklet16( DWORD unused1, DWORD unused2 )
{
    return FALSE;
}


/***********************************************************************
 * Callback Client API
 */

#define N_CBC_FIXED    20
#define N_CBC_VARIABLE 10
#define N_CBC_TOTAL    (N_CBC_FIXED + N_CBC_VARIABLE)

static SEGPTR CBClientRelay16[ N_CBC_TOTAL ];
static FARPROC *CBClientRelay32[ N_CBC_TOTAL ];

/***********************************************************************
 *     RegisterCBClient                    (KERNEL.619)
 */
1928
INT16 WINAPI RegisterCBClient16( INT16 wCBCId,
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
                                 SEGPTR relay16, FARPROC *relay32 )
{
    /* Search for free Callback ID */
    if ( wCBCId == -1 )
        for ( wCBCId = N_CBC_FIXED; wCBCId < N_CBC_TOTAL; wCBCId++ )
            if ( !CBClientRelay16[ wCBCId ] )
                break;

    /* Register Callback ID */
    if ( wCBCId > 0 && wCBCId < N_CBC_TOTAL )
    {
        CBClientRelay16[ wCBCId ] = relay16;
        CBClientRelay32[ wCBCId ] = relay32;
    }
    else
        wCBCId = 0;

    return wCBCId;
}

/***********************************************************************
 *     UnRegisterCBClient                  (KERNEL.622)
 */
1952
INT16 WINAPI UnRegisterCBClient16( INT16 wCBCId,
1953 1954
                                   SEGPTR relay16, FARPROC *relay32 )
{
1955 1956
    if (    wCBCId >= N_CBC_FIXED && wCBCId < N_CBC_TOTAL
         && CBClientRelay16[ wCBCId ] == relay16
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974
         && CBClientRelay32[ wCBCId ] == relay32 )
    {
        CBClientRelay16[ wCBCId ] = 0;
        CBClientRelay32[ wCBCId ] = 0;
    }
    else
        wCBCId = 0;

    return wCBCId;
}


/***********************************************************************
 *     InitCBClient                        (KERNEL.623)
 */
void WINAPI InitCBClient16( FARPROC glueLS )
{
    HMODULE16 kernel = GetModuleHandle16( "KERNEL" );
1975
    SEGPTR glueSL = (SEGPTR)GetProcAddress16( kernel, (LPCSTR)604 );
1976 1977 1978 1979 1980 1981 1982

    SetThunkletCallbackGlue16( glueLS, glueSL );
}

/***********************************************************************
 *     CBClientGlueSL                      (KERNEL.604)
 */
1983
void WINAPI CBClientGlueSL( CONTEXT *context )
1984 1985 1986
{
    /* Create stack frame */
    SEGPTR stackSeg = stack16_push( 12 );
1987
    LPWORD stackLin = MapSL( stackSeg );
1988
    SEGPTR glue, *glueTab;
1989

1990 1991 1992 1993
    stackLin[3] = (WORD)context->Ebp;
    stackLin[2] = (WORD)context->Esi;
    stackLin[1] = (WORD)context->Edi;
    stackLin[0] = (WORD)context->SegDs;
1994

1995 1996 1997
    context->Ebp = OFFSETOF( stackSeg ) + 6;
    context->Esp = OFFSETOF( stackSeg ) - 4;
    context->SegGs = 0;
1998 1999

    /* Jump to 16-bit relay code */
2000
    glueTab = MapSL( CBClientRelay16[ stackLin[5] ] );
2001
    glue = glueTab[ stackLin[4] ];
2002 2003
    context->SegCs = SELECTOROF( glue );
    context->Eip   = OFFSETOF  ( glue );
2004 2005 2006 2007 2008
}

/***********************************************************************
 *     CBClientThunkSL                      (KERNEL.620)
 */
2009
extern DWORD CALL32_CBClient( FARPROC proc, LPWORD args, WORD *stackLin, DWORD *esi );
2010
void WINAPI CBClientThunkSL( CONTEXT *context )
2011 2012 2013
{
    /* Call 32-bit relay code */

2014
    LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2015 2016
    FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];

2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028
    /* fill temporary area for the asm code (see comments in winebuild) */
    SEGPTR stack = stack16_push( 12 );
    LPWORD stackLin = MapSL(stack);
    /* stackLin[0] and stackLin[1] reserved for the 32-bit stack ptr */
    stackLin[2] = wine_get_ss();
    stackLin[3] = 0;
    stackLin[4] = OFFSETOF(stack) + 12;
    stackLin[5] = SELECTOROF(stack);
    stackLin[6] = OFFSETOF(CALL32_CBClientEx_RetAddr);  /* overwrite return address */
    stackLin[7] = SELECTOROF(CALL32_CBClientEx_RetAddr);
    context->Eax = CALL32_CBClient( proc, args, stackLin + 4, &context->Esi );
    stack16_pop( 12 );
2029 2030 2031 2032 2033
}

/***********************************************************************
 *     CBClientThunkSLEx                    (KERNEL.621)
 */
2034
extern DWORD CALL32_CBClientEx( FARPROC proc, LPWORD args, WORD *stackLin, DWORD *esi, INT *nArgs );
2035
void WINAPI CBClientThunkSLEx( CONTEXT *context )
2036 2037 2038
{
    /* Call 32-bit relay code */

2039
    LPWORD args = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ) );
2040 2041 2042 2043
    FARPROC proc = CBClientRelay32[ args[2] ][ args[1] ];
    INT nArgs;
    LPWORD stackLin;

2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058
    /* fill temporary area for the asm code (see comments in winebuild) */
    SEGPTR stack = stack16_push( 24 );
    stackLin = MapSL(stack);
    stackLin[0] = OFFSETOF(stack) + 4;
    stackLin[1] = SELECTOROF(stack);
    stackLin[2] = wine_get_ds();
    stackLin[5] = OFFSETOF(stack) + 24;
    /* stackLin[6] and stackLin[7] reserved for the 32-bit stack ptr */
    stackLin[8] = wine_get_ss();
    stackLin[9] = 0;
    stackLin[10] = OFFSETOF(CALL32_CBClientEx_RetAddr);
    stackLin[11] = SELECTOROF(CALL32_CBClientEx_RetAddr);

    context->Eax = CALL32_CBClientEx( proc, args, stackLin, &context->Esi, &nArgs );
    stack16_pop( 24 );
2059 2060 2061

    /* Restore registers saved by CBClientGlueSL */
    stackLin = (LPWORD)((LPBYTE)CURRENT_STACK16 + sizeof(STACK16FRAME) - 4);
2062
    context->Ebp = (context->Ebp & ~0xffff) | stackLin[3];
2063 2064
    context->Esi = (context->Esi & ~0xffff) | stackLin[2];
    context->Edi = (context->Edi & ~0xffff) | stackLin[1];
2065 2066
    context->SegDs = stackLin[0];
    context->Esp += 16+nArgs;
2067 2068

    /* Return to caller of CBClient thunklet */
2069 2070
    context->SegCs = stackLin[9];
    context->Eip   = stackLin[8];
2071 2072 2073 2074
}


/***********************************************************************
2075
 *           Get16DLLAddress       (KERNEL32.@)
2076 2077 2078 2079 2080
 *
 * This function is used by a Win32s DLL if it wants to call a Win16 function.
 * A 16:16 segmented pointer to the function is returned.
 * Written without any docu.
 */
2081
SEGPTR WINAPI Get16DLLAddress(HMODULE16 handle, LPSTR func_name)
2082 2083 2084 2085 2086 2087 2088
{
    static WORD code_sel32;
    FARPROC16 proc_16;
    LPBYTE thunk;

    if (!code_sel32)
    {
2089
        if (!ThunkletHeap) THUNK_Init();
2090
        code_sel32 = SELECTOR_AllocBlock( ThunkletHeap, 0x10000,
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
                                          WINE_LDT_FLAGS_CODE | WINE_LDT_FLAGS_32BIT );
        if (!code_sel32) return 0;
    }
    if (!(thunk = HeapAlloc( ThunkletHeap, 0, 32 ))) return 0;

    if (!handle) handle = GetModuleHandle16("WIN32S16");
    proc_16 = GetProcAddress16(handle, func_name);

    /* movl proc_16, $edx */
    *thunk++ = 0xba;
    *(FARPROC16 *)thunk = proc_16;
    thunk += sizeof(FARPROC16);

     /* jmpl QT_Thunk */
    *thunk++ = 0xea;
2106
    *(void **)thunk = QT_Thunk;
2107
    thunk += sizeof(FARPROC16);
2108
    *(WORD *)thunk = wine_get_cs();
2109 2110

    return MAKESEGPTR( code_sel32, (char *)thunk - (char *)ThunkletHeap );
2111 2112 2113 2114 2115 2116 2117
}


/***********************************************************************
 *		GetWin16DOSEnv			(KERNEL32.34)
 * Returns some internal value.... probably the default environment database?
 */
2118
DWORD WINAPI GetWin16DOSEnv(void)
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137
{
	FIXME("stub, returning 0\n");
	return 0;
}

/**********************************************************************
 *           GetPK16SysVar    (KERNEL32.92)
 */
LPVOID WINAPI GetPK16SysVar(void)
{
    static BYTE PK16SysVar[128];

    FIXME("()\n");
    return PK16SysVar;
}

/**********************************************************************
 *           CommonUnimpStub    (KERNEL32.17)
 */
2138
void WINAPI __regs_CommonUnimpStub( CONTEXT *context )
2139
{
2140
    FIXME("generic stub: %s\n", ((LPSTR)context->Eax ? (LPSTR)context->Eax : "?"));
2141

2142
    switch ((context->Ecx >> 4) & 0x0f)
2143
    {
2144 2145 2146 2147 2148
    case 15:  context->Eax = -1;   break;
    case 14:  context->Eax = 0x78; break;
    case 13:  context->Eax = 0x32; break;
    case 1:   context->Eax = 1;    break;
    default:  context->Eax = 0;    break;
2149 2150
    }

2151
    context->Esp += (context->Ecx & 0x0f) * 4;
2152
}
2153
DEFINE_REGS_ENTRYPOINT( CommonUnimpStub, 0 )
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164

/**********************************************************************
 *           HouseCleanLogicallyDeadHandles    (KERNEL32.33)
 */
void WINAPI HouseCleanLogicallyDeadHandles(void)
{
    /* Whatever this is supposed to do, our handles probably
       don't need it :-) */
}

/**********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
2165
 *		@ (KERNEL32.100)
2166 2167 2168
 */
BOOL WINAPI _KERNEL32_100(HANDLE threadid,DWORD exitcode,DWORD x)
{
2169
	FIXME("(%p,%d,0x%08x): stub\n",threadid,exitcode,x);
2170 2171 2172 2173
	return TRUE;
}

/**********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
2174
 *		@ (KERNEL32.99)
2175 2176 2177 2178
 *
 * Checks whether the clock has to be switched from daylight
 * savings time to standard time or vice versa.
 *
2179 2180 2181
 */
DWORD WINAPI _KERNEL32_99(DWORD x)
{
2182
	FIXME("(0x%08x): stub\n",x);
2183 2184 2185 2186
	return 1;
}


2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
/***********************************************************************
 * Helper for k32 family functions
 */
static void *user32_proc_address(const char *proc_name)
{
    static HMODULE hUser32;

    if(!hUser32) hUser32 = LoadLibraryA("user32.dll");
    return GetProcAddress(hUser32, proc_name);
}

/***********************************************************************
 *		k32CharToOemBuffA   (KERNEL32.11)
 */
BOOL WINAPI k32CharToOemBuffA(LPCSTR s, LPSTR d, DWORD len)
{
    WCHAR *bufW;

    if ((bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
    {
        MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
        WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
        HeapFree( GetProcessHeap(), 0, bufW );
    }
    return TRUE;
}

/***********************************************************************
 *		k32CharToOemA   (KERNEL32.10)
 */
BOOL WINAPI k32CharToOemA(LPCSTR s, LPSTR d)
{
    if (!s || !d) return TRUE;
    return k32CharToOemBuffA( s, d, strlen(s) + 1 );
}

/***********************************************************************
 *		k32OemToCharBuffA   (KERNEL32.13)
 */
BOOL WINAPI k32OemToCharBuffA(LPCSTR s, LPSTR d, DWORD len)
{
    WCHAR *bufW;

    if ((bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
    {
        MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
        WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
        HeapFree( GetProcessHeap(), 0, bufW );
    }
    return TRUE;
}

/***********************************************************************
 *		k32OemToCharA   (KERNEL32.12)
 */
BOOL WINAPI k32OemToCharA(LPCSTR s, LPSTR d)
{
    return k32OemToCharBuffA( s, d, strlen(s) + 1 );
}

/**********************************************************************
 *		k32LoadStringA   (KERNEL32.14)
 */
INT WINAPI k32LoadStringA(HINSTANCE instance, UINT resource_id,
                          LPSTR buffer, INT buflen)
{
    static INT (WINAPI *pLoadStringA)(HINSTANCE, UINT, LPSTR, INT);

    if(!pLoadStringA) pLoadStringA = user32_proc_address("LoadStringA");
    return pLoadStringA(instance, resource_id, buffer, buflen);
}

/***********************************************************************
 *		k32wvsprintfA   (KERNEL32.16)
 */
INT WINAPI k32wvsprintfA(LPSTR buffer, LPCSTR spec, __ms_va_list args)
{
    static INT (WINAPI *pwvsprintfA)(LPSTR, LPCSTR, __ms_va_list);

    if(!pwvsprintfA) pwvsprintfA = user32_proc_address("wvsprintfA");
    return (*pwvsprintfA)(buffer, spec, args);
}

/***********************************************************************
 *		k32wsprintfA   (KERNEL32.15)
 */
INT WINAPIV k32wsprintfA(LPSTR buffer, LPCSTR spec, ...)
{
    __ms_va_list args;
    INT res;

    __ms_va_start(args, spec);
    res = k32wvsprintfA(buffer, spec, args);
    __ms_va_end(args);
    return res;
}

2284 2285 2286 2287 2288 2289
/**********************************************************************
 *	     Catch    (KERNEL.55)
 *
 * Real prototype is:
 *   INT16 WINAPI Catch( LPCATCHBUF lpbuf );
 */
2290
void WINAPI Catch16( LPCATCHBUF lpbuf, CONTEXT *context )
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307
{
    /* Note: we don't save the current ss, as the catch buffer is */
    /* only 9 words long. Hopefully no one will have the silly    */
    /* idea to change the current stack before calling Throw()... */

    /* Windows uses:
     * lpbuf[0] = ip
     * lpbuf[1] = cs
     * lpbuf[2] = sp
     * lpbuf[3] = bp
     * lpbuf[4] = si
     * lpbuf[5] = di
     * lpbuf[6] = ds
     * lpbuf[7] = unused
     * lpbuf[8] = ss
     */

2308 2309
    lpbuf[0] = LOWORD(context->Eip);
    lpbuf[1] = context->SegCs;
2310
    /* Windows pushes 4 more words before saving sp */
2311 2312 2313 2314 2315
    lpbuf[2] = LOWORD(context->Esp) - 4 * sizeof(WORD);
    lpbuf[3] = LOWORD(context->Ebp);
    lpbuf[4] = LOWORD(context->Esi);
    lpbuf[5] = LOWORD(context->Edi);
    lpbuf[6] = context->SegDs;
2316
    lpbuf[7] = 0;
2317
    lpbuf[8] = context->SegSs;
2318
    context->Eax &= ~0xffff;  /* Return 0 */
2319 2320 2321 2322 2323 2324 2325 2326 2327
}


/**********************************************************************
 *	     Throw    (KERNEL.56)
 *
 * Real prototype is:
 *   INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
 */
2328
void WINAPI Throw16( LPCATCHBUF lpbuf, INT16 retval, CONTEXT *context )
2329 2330 2331 2332
{
    STACK16FRAME *pFrame;
    STACK32FRAME *frame32;

2333
    context->Eax = (context->Eax & ~0xffff) | (WORD)retval;
2334 2335

    /* Find the frame32 corresponding to the frame16 we are jumping to */
2336
    pFrame = CURRENT_STACK16;
2337 2338 2339
    frame32 = pFrame->frame32;
    while (frame32 && frame32->frame16)
    {
2340
        if (OFFSETOF(frame32->frame16) < OFFSETOF(NtCurrentTeb()->WOW32Reserved))
2341 2342 2343 2344 2345 2346 2347
            break;  /* Something strange is going on */
        if (OFFSETOF(frame32->frame16) > lpbuf[2])
        {
            /* We found the right frame */
            pFrame->frame32 = frame32;
            break;
        }
2348
        frame32 = ((STACK16FRAME *)MapSL(frame32->frame16))->frame32;
2349
    }
2350
    RtlUnwind( &pFrame->frame32->frame, NULL, NULL, 0 );
2351

2352 2353 2354 2355 2356 2357 2358
    context->Eip = lpbuf[0];
    context->SegCs  = lpbuf[1];
    context->Esp = lpbuf[2] + 4 * sizeof(WORD) - sizeof(WORD) /*extra arg*/;
    context->Ebp = lpbuf[3];
    context->Esi = lpbuf[4];
    context->Edi = lpbuf[5];
    context->SegDs  = lpbuf[6];
2359

2360
    if (lpbuf[8] != context->SegSs)
2361 2362
        ERR("Switching stack segment with Throw() not supported; expect crash now\n" );
}
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453


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

/**********************************************************************
 *           GetVDMPointer32W      (KERNEL.516)
 */
DWORD WINAPI GetVDMPointer32W16( SEGPTR vp, UINT16 fMode )
{
    GlobalPageLock16(GlobalHandle16(SELECTOROF(vp)));
    return (DWORD)K32WOWGetVDMPointer( vp, 0, (DWORD)fMode );
}

/***********************************************************************
 *           LoadLibraryEx32W      (KERNEL.513)
 */
DWORD WINAPI LoadLibraryEx32W16( LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags )
{
    HMODULE hModule;
    DWORD mutex_count;
    OFSTRUCT ofs;
    const char *p;

    if (!lpszLibFile)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return 0;
    }

    /* if the file cannot be found, call LoadLibraryExA anyway, since it might be
       a builtin module. This case is handled in MODULE_LoadLibraryExA */

    if ((p = strrchr( lpszLibFile, '.' )) && !strchr( p, '\\' ))  /* got an extension */
    {
        if (OpenFile16( lpszLibFile, &ofs, OF_EXIST ) != HFILE_ERROR16)
            lpszLibFile = ofs.szPathName;
    }
    else
    {
        char buffer[MAX_PATH+4];
        strcpy( buffer, lpszLibFile );
        strcat( buffer, ".dll" );
        if (OpenFile16( buffer, &ofs, OF_EXIST ) != HFILE_ERROR16)
            lpszLibFile = ofs.szPathName;
    }

    ReleaseThunkLock( &mutex_count );
    hModule = LoadLibraryExA( lpszLibFile, (HANDLE)hFile, dwFlags );
    RestoreThunkLock( mutex_count );

    return (DWORD)hModule;
}

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

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

    ReleaseThunkLock( &mutex_count );
    retv = FreeLibrary( (HMODULE)hLibModule );
    RestoreThunkLock( mutex_count );
    return (DWORD)retv;
}


#define CPEX_DEST_STDCALL   0x00000000
#define CPEX_DEST_CDECL     0x80000000

/**********************************************************************
 *           WOW_CallProc32W
 */
static DWORD WOW_CallProc32W16( FARPROC proc32, DWORD nrofargs, DWORD *args )
{
    DWORD ret;
    DWORD mutex_count;

    ReleaseThunkLock( &mutex_count );
    if (!proc32) ret = 0;
2454
    else ret = call_entry_point( proc32, nrofargs & ~CPEX_DEST_CDECL, args );
2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
    RestoreThunkLock( mutex_count );

    TRACE("returns %08x\n",ret);
    return ret;
}

/**********************************************************************
 *           CallProc32W           (KERNEL.517)
 */
DWORD WINAPIV CallProc32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
{
    DWORD args[32];
    unsigned int i;

    TRACE("(%d,%d,%p args[",nrofargs,argconvmask,proc32);

    for (i=0;i<nrofargs;i++)
    {
        if (argconvmask & (1<<i))
        {
            SEGPTR ptr = VA_ARG16( valist, SEGPTR );
            /* pascal convention, have to reverse the arguments order */
            args[nrofargs - i - 1] = (DWORD)MapSL(ptr);
            TRACE("%08x(%p),",ptr,MapSL(ptr));
        }
        else
        {
            DWORD arg = VA_ARG16( valist, DWORD );
            /* pascal convention, have to reverse the arguments order */
            args[nrofargs - i - 1] = arg;
            TRACE("%d,", arg);
        }
    }
    TRACE("])\n");

    /* POP nrofargs DWORD arguments and 3 DWORD parameters */
    stack16_pop( (3 + nrofargs) * sizeof(DWORD) );

    return WOW_CallProc32W16( proc32, nrofargs, args );
}

/**********************************************************************
 *           _CallProcEx32W         (KERNEL.518)
 */
DWORD WINAPIV CallProcEx32W16( DWORD nrofargs, DWORD argconvmask, FARPROC proc32, VA_LIST16 valist )
{
    DWORD args[32];
    unsigned int i;

    TRACE("(%d,%d,%p args[",nrofargs,argconvmask,proc32);

    for (i=0;i<nrofargs;i++)
    {
        if (argconvmask & (1<<i))
        {
            SEGPTR ptr = VA_ARG16( valist, SEGPTR );
            args[i] = (DWORD)MapSL(ptr);
            TRACE("%08x(%p),",ptr,MapSL(ptr));
        }
        else
        {
            DWORD arg = VA_ARG16( valist, DWORD );
            args[i] = arg;
            TRACE("%d,", arg);
        }
    }
    TRACE("])\n");
    return WOW_CallProc32W16( proc32, nrofargs, args );
}


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

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