2002-12-15 02:18:40 +01:00
|
|
|
/*
|
|
|
|
* Routines for dynamically building calls to Wine from
|
|
|
|
* protected mode applications.
|
|
|
|
*
|
|
|
|
* Copyright 2002 Jukka Heinonen
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
#include "dosexe.h"
|
2005-05-16 11:19:18 +02:00
|
|
|
#include "wine/winbase16.h"
|
2002-12-15 02:18:40 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(int);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Magic DWORD used to check stack integrity.
|
|
|
|
*/
|
|
|
|
#define RELAY_MAGIC 0xabcdef00
|
|
|
|
|
|
|
|
/*
|
2003-06-23 05:35:51 +02:00
|
|
|
* Memory block for temporary 16-bit stacks used with relay calls.
|
2002-12-15 02:18:40 +01:00
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
DWORD inuse; /* non-zero if stack block is in use */
|
|
|
|
DWORD eip; /* saved ip */
|
|
|
|
DWORD seg_cs; /* saved cs */
|
|
|
|
DWORD esp; /* saved sp */
|
|
|
|
DWORD seg_ss; /* saved ss */
|
|
|
|
DWORD stack_bottom; /* guard dword */
|
|
|
|
BYTE stack[256-7*4]; /* 16-bit stack */
|
|
|
|
DWORD stack_top; /* guard dword */
|
|
|
|
} RELAY_Stack16;
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* RELAY_GetPointer
|
|
|
|
*
|
|
|
|
* Get pointer to stack block when given esp pointing to 16-bit stack
|
|
|
|
* inside relay data segment.
|
|
|
|
*/
|
|
|
|
static RELAY_Stack16 *RELAY_GetPointer( DWORD offset )
|
|
|
|
{
|
|
|
|
offset = offset / sizeof(RELAY_Stack16) * sizeof(RELAY_Stack16);
|
|
|
|
return MapSL(MAKESEGPTR(DOSVM_dpmi_segments->relay_data_sel, offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* RELAY_MakeShortContext
|
|
|
|
*
|
2003-06-23 05:35:51 +02:00
|
|
|
* Allocate separate 16-bit stack, make stack pointer point to this
|
|
|
|
* stack and make code pointer point to stub that restores everything.
|
2002-12-15 02:18:40 +01:00
|
|
|
* So, after this routine, SS and CS are guaranteed to be 16-bit.
|
|
|
|
*
|
|
|
|
* Note: This might be called from signal handler, so the stack
|
|
|
|
* allocation algorithm must be signal safe.
|
|
|
|
*/
|
|
|
|
static void RELAY_MakeShortContext( CONTEXT86 *context )
|
|
|
|
{
|
2003-06-23 05:35:51 +02:00
|
|
|
DWORD offset = offsetof(RELAY_Stack16, stack_top);
|
|
|
|
RELAY_Stack16 *stack = RELAY_GetPointer( 0 );
|
2002-12-15 02:18:40 +01:00
|
|
|
|
2003-06-23 05:35:51 +02:00
|
|
|
while (stack->inuse && offset < DOSVM_RELAY_DATA_SIZE) {
|
|
|
|
stack++;
|
|
|
|
offset += sizeof(RELAY_Stack16);
|
|
|
|
}
|
2002-12-15 02:18:40 +01:00
|
|
|
|
2003-06-23 05:35:51 +02:00
|
|
|
if (offset >= DOSVM_RELAY_DATA_SIZE)
|
|
|
|
ERR( "Too many nested interrupts!\n" );
|
2002-12-15 02:18:40 +01:00
|
|
|
|
2003-06-23 05:35:51 +02:00
|
|
|
stack->inuse = 1;
|
|
|
|
stack->eip = context->Eip;
|
|
|
|
stack->seg_cs = context->SegCs;
|
|
|
|
stack->esp = context->Esp;
|
|
|
|
stack->seg_ss = context->SegSs;
|
|
|
|
|
|
|
|
stack->stack_bottom = RELAY_MAGIC;
|
|
|
|
stack->stack_top = RELAY_MAGIC;
|
|
|
|
|
|
|
|
context->SegSs = DOSVM_dpmi_segments->relay_data_sel;
|
|
|
|
context->Esp = offset;
|
|
|
|
context->SegCs = DOSVM_dpmi_segments->relay_code_sel;
|
|
|
|
context->Eip = 3;
|
2002-12-15 02:18:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* RELAY_RelayStub
|
|
|
|
*
|
|
|
|
* This stub is called by __wine_call_from_16_regs in order to marshall
|
|
|
|
* relay parameters.
|
|
|
|
*/
|
2003-08-19 02:59:23 +02:00
|
|
|
static void __stdcall RELAY_RelayStub( DOSRELAY proc,
|
2002-12-15 02:18:40 +01:00
|
|
|
unsigned char *args,
|
2003-08-19 02:59:23 +02:00
|
|
|
void *ctx86 )
|
2002-12-15 02:18:40 +01:00
|
|
|
{
|
2003-06-23 05:35:51 +02:00
|
|
|
if (proc)
|
2003-08-19 02:59:23 +02:00
|
|
|
{
|
|
|
|
CONTEXT86 *context = (CONTEXT86*)ctx86;
|
|
|
|
RELAY_Stack16 *stack = RELAY_GetPointer( context->Esp );
|
|
|
|
|
|
|
|
DWORD old_seg_cs = context->SegCs;
|
|
|
|
DWORD old_eip = context->Eip;
|
|
|
|
DWORD old_seg_ss = context->SegSs;
|
|
|
|
DWORD old_esp = context->Esp;
|
|
|
|
|
|
|
|
context->SegCs = stack->seg_cs;
|
|
|
|
context->Eip = stack->eip;
|
|
|
|
context->SegSs = stack->seg_ss;
|
|
|
|
context->Esp = stack->esp;
|
|
|
|
|
|
|
|
proc( context, *(LPVOID *)args );
|
|
|
|
|
|
|
|
stack->seg_cs = context->SegCs;
|
|
|
|
stack->eip = context->Eip;
|
|
|
|
stack->seg_ss = context->SegSs;
|
|
|
|
stack->esp = context->Esp;
|
|
|
|
|
|
|
|
context->SegCs = old_seg_cs;
|
|
|
|
context->Eip = old_eip;
|
|
|
|
context->SegSs = old_seg_ss;
|
|
|
|
context->Esp = old_esp;
|
|
|
|
}
|
2002-12-15 02:18:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* DOSVM_RelayHandler
|
|
|
|
*
|
|
|
|
* Restore saved code and stack pointers and release stack block.
|
|
|
|
*/
|
|
|
|
void DOSVM_RelayHandler( CONTEXT86 *context )
|
|
|
|
{
|
|
|
|
RELAY_Stack16 *stack = RELAY_GetPointer( context->Esp );
|
|
|
|
|
|
|
|
context->SegSs = stack->seg_ss;
|
|
|
|
context->Esp = stack->esp;
|
|
|
|
context->SegCs = stack->seg_cs;
|
|
|
|
context->Eip = stack->eip;
|
|
|
|
|
|
|
|
if (!stack->inuse ||
|
|
|
|
stack->stack_bottom != RELAY_MAGIC ||
|
|
|
|
stack->stack_top != RELAY_MAGIC)
|
|
|
|
ERR( "Stack corrupted!\n" );
|
|
|
|
|
|
|
|
stack->inuse = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* DOSVM_BuildCallFrame
|
|
|
|
*
|
|
|
|
* Modifies the context so that return to context calls DOSRELAY and
|
|
|
|
* only after return from DOSRELAY the original context will be returned to.
|
|
|
|
*/
|
|
|
|
void DOSVM_BuildCallFrame( CONTEXT86 *context, DOSRELAY relay, LPVOID data )
|
|
|
|
{
|
2005-09-18 14:25:22 +02:00
|
|
|
static void (*__wine_call_from_16_regs_ptr)();
|
2002-12-15 02:18:40 +01:00
|
|
|
WORD code_sel = DOSVM_dpmi_segments->relay_code_sel;
|
|
|
|
|
|
|
|
/*
|
2003-06-23 05:35:51 +02:00
|
|
|
* Allocate separate stack for relay call.
|
2002-12-15 02:18:40 +01:00
|
|
|
*/
|
|
|
|
RELAY_MakeShortContext( context );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build call frame.
|
|
|
|
*/
|
2004-10-19 00:34:02 +02:00
|
|
|
PUSH_WORD16( context, HIWORD(data) ); /* argument.hiword */
|
|
|
|
PUSH_WORD16( context, LOWORD(data) ); /* argument.loword */
|
|
|
|
PUSH_WORD16( context, context->SegCs ); /* STACK16FRAME.cs */
|
|
|
|
PUSH_WORD16( context, LOWORD(context->Eip) ); /* STACK16FRAME.ip */
|
|
|
|
PUSH_WORD16( context, LOWORD(context->Ebp) ); /* STACK16FRAME.bp */
|
|
|
|
PUSH_WORD16( context, HIWORD(relay) ); /* STACK16FRAME.entry_point.hiword */
|
|
|
|
PUSH_WORD16( context, LOWORD(relay) ); /* STACK16FRAME.entry_point.loword */
|
|
|
|
PUSH_WORD16( context, 0 ); /* STACK16FRAME.entry_ip */
|
|
|
|
PUSH_WORD16( context, HIWORD(RELAY_RelayStub) ); /* STACK16FRAME.relay.hiword */
|
|
|
|
PUSH_WORD16( context, LOWORD(RELAY_RelayStub) ); /* STACK16FRAME.relay.loword */
|
|
|
|
PUSH_WORD16( context, 0 ); /* STACK16FRAME.module_cs.hiword */
|
|
|
|
PUSH_WORD16( context, code_sel ); /* STACK16FRAME.module_cs.loword */
|
|
|
|
PUSH_WORD16( context, 0 ); /* STACK16FRAME.callfrom_ip.hiword */
|
|
|
|
PUSH_WORD16( context, 0 ); /* STACK16FRAME.callfrom_ip.loword */
|
2002-12-15 02:18:40 +01:00
|
|
|
|
|
|
|
/*
|
2004-10-19 00:34:02 +02:00
|
|
|
* Adjust code pointer.
|
2002-12-15 02:18:40 +01:00
|
|
|
*/
|
2005-09-18 14:25:22 +02:00
|
|
|
if (!__wine_call_from_16_regs_ptr)
|
|
|
|
__wine_call_from_16_regs_ptr = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"),
|
|
|
|
"__wine_call_from_16_regs" );
|
2002-12-15 02:18:40 +01:00
|
|
|
context->SegCs = wine_get_cs();
|
2005-09-18 14:25:22 +02:00
|
|
|
context->Eip = (DWORD)__wine_call_from_16_regs_ptr;
|
2002-12-15 02:18:40 +01:00
|
|
|
}
|