2 * Routines for dynamically building calls to Wine from
3 * protected mode applications.
5 * Copyright 2002 Jukka Heinonen
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/debug.h"
24 #include "builtin16.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(int);
29 * Magic DWORD used to check stack integrity.
31 #define RELAY_MAGIC 0xabcdef00
34 * Memory block for temporary 16-bit stacks used with relay calls.
37 DWORD inuse; /* non-zero if stack block is in use */
38 DWORD eip; /* saved ip */
39 DWORD seg_cs; /* saved cs */
40 DWORD esp; /* saved sp */
41 DWORD seg_ss; /* saved ss */
42 DWORD stack_bottom; /* guard dword */
43 BYTE stack[256-7*4]; /* 16-bit stack */
44 DWORD stack_top; /* guard dword */
48 /**********************************************************************
51 * Get pointer to stack block when given esp pointing to 16-bit stack
52 * inside relay data segment.
54 static RELAY_Stack16 *RELAY_GetPointer( DWORD offset )
56 offset = offset / sizeof(RELAY_Stack16) * sizeof(RELAY_Stack16);
57 return MapSL(MAKESEGPTR(DOSVM_dpmi_segments->relay_data_sel, offset));
61 /**********************************************************************
62 * RELAY_MakeShortContext
64 * Allocate separate 16-bit stack, make stack pointer point to this
65 * stack and make code pointer point to stub that restores everything.
66 * So, after this routine, SS and CS are guaranteed to be 16-bit.
68 * Note: This might be called from signal handler, so the stack
69 * allocation algorithm must be signal safe.
71 static void RELAY_MakeShortContext( CONTEXT86 *context )
73 DWORD offset = offsetof(RELAY_Stack16, stack_top);
74 RELAY_Stack16 *stack = RELAY_GetPointer( 0 );
76 while (stack->inuse && offset < DOSVM_RELAY_DATA_SIZE) {
78 offset += sizeof(RELAY_Stack16);
81 if (offset >= DOSVM_RELAY_DATA_SIZE)
82 ERR( "Too many nested interrupts!\n" );
85 stack->eip = context->Eip;
86 stack->seg_cs = context->SegCs;
87 stack->esp = context->Esp;
88 stack->seg_ss = context->SegSs;
90 stack->stack_bottom = RELAY_MAGIC;
91 stack->stack_top = RELAY_MAGIC;
93 context->SegSs = DOSVM_dpmi_segments->relay_data_sel;
94 context->Esp = offset;
95 context->SegCs = DOSVM_dpmi_segments->relay_code_sel;
100 /**********************************************************************
103 * This stub is called by __wine_call_from_16_regs in order to marshall
106 static void __stdcall RELAY_RelayStub( DOSRELAY proc,
112 CONTEXT86 *context = (CONTEXT86*)ctx86;
113 RELAY_Stack16 *stack = RELAY_GetPointer( context->Esp );
115 DWORD old_seg_cs = context->SegCs;
116 DWORD old_eip = context->Eip;
117 DWORD old_seg_ss = context->SegSs;
118 DWORD old_esp = context->Esp;
120 context->SegCs = stack->seg_cs;
121 context->Eip = stack->eip;
122 context->SegSs = stack->seg_ss;
123 context->Esp = stack->esp;
125 proc( context, *(LPVOID *)args );
127 stack->seg_cs = context->SegCs;
128 stack->eip = context->Eip;
129 stack->seg_ss = context->SegSs;
130 stack->esp = context->Esp;
132 context->SegCs = old_seg_cs;
133 context->Eip = old_eip;
134 context->SegSs = old_seg_ss;
135 context->Esp = old_esp;
140 /**********************************************************************
143 * Restore saved code and stack pointers and release stack block.
145 void DOSVM_RelayHandler( CONTEXT86 *context )
147 RELAY_Stack16 *stack = RELAY_GetPointer( context->Esp );
149 context->SegSs = stack->seg_ss;
150 context->Esp = stack->esp;
151 context->SegCs = stack->seg_cs;
152 context->Eip = stack->eip;
155 stack->stack_bottom != RELAY_MAGIC ||
156 stack->stack_top != RELAY_MAGIC)
157 ERR( "Stack corrupted!\n" );
162 * We have now restored original stack and instruction pointers.
163 * Because signals are blocked here, this is a safe place to
164 * check for pending events before we return to application context.
166 if (NtCurrentTeb()->vm86_pending && NtCurrentTeb()->dpmi_vif)
168 NtCurrentTeb()->vm86_pending = 0;
169 DOSVM_SendQueuedEvents( context );
174 /**********************************************************************
175 * DOSVM_BuildCallFrame
177 * Modifies the context so that return to context calls DOSRELAY and
178 * only after return from DOSRELAY the original context will be returned to.
180 void DOSVM_BuildCallFrame( CONTEXT86 *context, DOSRELAY relay, LPVOID data )
183 WORD code_sel = DOSVM_dpmi_segments->relay_code_sel;
186 * Allocate separate stack for relay call.
188 RELAY_MakeShortContext( context );
191 * Get stack pointer after RELAY_MakeShortContext.
193 stack = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
198 *(--stack) = HIWORD(data); /* argument.hiword */
199 *(--stack) = LOWORD(data); /* argument.loword */
200 *(--stack) = context->SegCs; /* STACK16FRAME.cs */
201 *(--stack) = LOWORD(context->Eip); /* STACK16FRAME.ip */
202 *(--stack) = LOWORD(context->Ebp); /* STACK16FRAME.bp */
203 *(--stack) = HIWORD(relay); /* STACK16FRAME.entry_point.hiword */
204 *(--stack) = LOWORD(relay); /* STACK16FRAME.entry_point.loword */
205 *(--stack) = 0; /* STACK16FRAME.entry_ip */
206 *(--stack) = HIWORD(RELAY_RelayStub); /* STACK16FRAME.relay.hiword */
207 *(--stack) = LOWORD(RELAY_RelayStub); /* STACK16FRAME.relay.loword */
208 *(--stack) = 0; /* STACK16FRAME.module_cs.hiword */
209 *(--stack) = code_sel; /* STACK16FRAME.module_cs.loword */
210 *(--stack) = 0; /* STACK16FRAME.callfrom_ip.hiword */
211 *(--stack) = 0; /* STACK16FRAME.callfrom_ip.loword */
214 * Adjust stack and code pointers.
216 ADD_LOWORD( context->Esp, -28 );
217 context->SegCs = wine_get_cs();
218 context->Eip = (DWORD)__wine_call_from_16_regs;