Fixed typo.
[wine.git] / dlls / winedos / relay.c
blobae6ca09362d8ae8a474a244b1abc99aa47dee011
1 /*
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
21 #include "dosexe.h"
22 #include "thread.h"
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.
36 typedef struct {
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 */
45 } RELAY_Stack16;
48 /**********************************************************************
49 * RELAY_GetPointer
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) {
77 stack++;
78 offset += sizeof(RELAY_Stack16);
81 if (offset >= DOSVM_RELAY_DATA_SIZE)
82 ERR( "Too many nested interrupts!\n" );
84 stack->inuse = 1;
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;
96 context->Eip = 3;
100 /**********************************************************************
101 * RELAY_RelayStub
103 * This stub is called by __wine_call_from_16_regs in order to marshall
104 * relay parameters.
106 static void __stdcall RELAY_RelayStub( DOSRELAY proc,
107 unsigned char *args,
108 void *ctx86 )
110 if (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 /**********************************************************************
141 * DOSVM_RelayHandler
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;
154 if (!stack->inuse ||
155 stack->stack_bottom != RELAY_MAGIC ||
156 stack->stack_top != RELAY_MAGIC)
157 ERR( "Stack corrupted!\n" );
159 stack->inuse = 0;
163 /**********************************************************************
164 * DOSVM_BuildCallFrame
166 * Modifies the context so that return to context calls DOSRELAY and
167 * only after return from DOSRELAY the original context will be returned to.
169 void DOSVM_BuildCallFrame( CONTEXT86 *context, DOSRELAY relay, LPVOID data )
171 WORD *stack;
172 WORD code_sel = DOSVM_dpmi_segments->relay_code_sel;
175 * Allocate separate stack for relay call.
177 RELAY_MakeShortContext( context );
180 * Get stack pointer after RELAY_MakeShortContext.
182 stack = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
185 * Build call frame.
187 *(--stack) = HIWORD(data); /* argument.hiword */
188 *(--stack) = LOWORD(data); /* argument.loword */
189 *(--stack) = context->SegCs; /* STACK16FRAME.cs */
190 *(--stack) = LOWORD(context->Eip); /* STACK16FRAME.ip */
191 *(--stack) = LOWORD(context->Ebp); /* STACK16FRAME.bp */
192 *(--stack) = HIWORD(relay); /* STACK16FRAME.entry_point.hiword */
193 *(--stack) = LOWORD(relay); /* STACK16FRAME.entry_point.loword */
194 *(--stack) = 0; /* STACK16FRAME.entry_ip */
195 *(--stack) = HIWORD(RELAY_RelayStub); /* STACK16FRAME.relay.hiword */
196 *(--stack) = LOWORD(RELAY_RelayStub); /* STACK16FRAME.relay.loword */
197 *(--stack) = 0; /* STACK16FRAME.module_cs.hiword */
198 *(--stack) = code_sel; /* STACK16FRAME.module_cs.loword */
199 *(--stack) = 0; /* STACK16FRAME.callfrom_ip.hiword */
200 *(--stack) = 0; /* STACK16FRAME.callfrom_ip.loword */
203 * Adjust stack and code pointers.
205 ADD_LOWORD( context->Esp, -28 );
206 context->SegCs = wine_get_cs();
207 context->Eip = (DWORD)__wine_call_from_16_regs;