Fixed/updated the file attributes defines.
[wine] / dlls / winedos / relay.c
1 /*
2  * Routines for dynamically building calls to Wine from 
3  * protected mode applications.
4  *
5  * Copyright 2002 Jukka Heinonen
6  *
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.
11  *
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.
16  *
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
20  */
21 #include "dosexe.h"
22 #include "thread.h"
23 #include "wine/debug.h"
24 #include "builtin16.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(int);
27
28 /*
29  * Magic DWORD used to check stack integrity.
30  */
31 #define RELAY_MAGIC 0xabcdef00
32
33 /*
34  * Memory block for temporary 16-bit stacks used with relay calls.
35  */
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;
46
47
48 /**********************************************************************
49  *          RELAY_GetPointer
50  *
51  * Get pointer to stack block when given esp pointing to 16-bit stack
52  * inside relay data segment.
53  */
54 static RELAY_Stack16 *RELAY_GetPointer( DWORD offset )
55 {
56     offset = offset / sizeof(RELAY_Stack16) * sizeof(RELAY_Stack16);
57     return MapSL(MAKESEGPTR(DOSVM_dpmi_segments->relay_data_sel, offset));
58 }
59
60
61 /**********************************************************************
62  *          RELAY_MakeShortContext
63  *
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.
67  *
68  * Note: This might be called from signal handler, so the stack
69  *       allocation algorithm must be signal safe.
70  */
71 static void RELAY_MakeShortContext( CONTEXT86 *context )
72 {
73     DWORD offset = offsetof(RELAY_Stack16, stack_top);
74     RELAY_Stack16 *stack = RELAY_GetPointer( 0 );
75
76     while (stack->inuse && offset < DOSVM_RELAY_DATA_SIZE) {
77         stack++;
78         offset += sizeof(RELAY_Stack16);
79     }
80
81     if (offset >= DOSVM_RELAY_DATA_SIZE)
82         ERR( "Too many nested interrupts!\n" );
83         
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;
89
90     stack->stack_bottom = RELAY_MAGIC;
91     stack->stack_top = RELAY_MAGIC;
92
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;
97 }
98
99
100 /**********************************************************************
101  *          RELAY_RelayStub
102  *
103  * This stub is called by __wine_call_from_16_regs in order to marshall
104  * relay parameters.
105  */
106 static void __stdcall RELAY_RelayStub( DOSRELAY       proc, 
107                                        unsigned char *args, 
108                                        void          *ctx86 )
109 {
110     if (proc)
111     {
112         CONTEXT86     *context    = (CONTEXT86*)ctx86;
113         RELAY_Stack16 *stack      = RELAY_GetPointer( context->Esp );
114
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;
119
120         context->SegCs = stack->seg_cs;
121         context->Eip   = stack->eip;
122         context->SegSs = stack->seg_ss;
123         context->Esp   = stack->esp;
124
125         proc( context, *(LPVOID *)args );
126
127         stack->seg_cs = context->SegCs;
128         stack->eip    = context->Eip;
129         stack->seg_ss = context->SegSs;
130         stack->esp    = context->Esp;
131
132         context->SegCs = old_seg_cs;
133         context->Eip   = old_eip;
134         context->SegSs = old_seg_ss;
135         context->Esp   = old_esp;
136     }
137 }
138
139
140 /**********************************************************************
141  *          DOSVM_RelayHandler
142  *
143  * Restore saved code and stack pointers and release stack block.
144  */
145 void DOSVM_RelayHandler( CONTEXT86 *context )
146 {
147     RELAY_Stack16 *stack = RELAY_GetPointer( context->Esp );
148
149     context->SegSs = stack->seg_ss;
150     context->Esp = stack->esp;
151     context->SegCs = stack->seg_cs;
152     context->Eip = stack->eip;
153
154     if (!stack->inuse || 
155         stack->stack_bottom != RELAY_MAGIC ||
156         stack->stack_top != RELAY_MAGIC)
157         ERR( "Stack corrupted!\n" );
158
159     stack->inuse = 0;
160 }
161
162
163 /**********************************************************************
164  *          DOSVM_BuildCallFrame
165  *
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.
168  */
169 void DOSVM_BuildCallFrame( CONTEXT86 *context, DOSRELAY relay, LPVOID data )
170 {
171     WORD *stack;
172     WORD  code_sel = DOSVM_dpmi_segments->relay_code_sel;
173
174     /*
175      * Allocate separate stack for relay call.
176      */
177     RELAY_MakeShortContext( context );
178
179     /*
180      * Get stack pointer after RELAY_MakeShortContext.
181      */
182     stack = CTX_SEG_OFF_TO_LIN(context, context->SegSs, context->Esp);
183
184     /*
185      * Build call frame.
186      */
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 */
201
202     /*
203      * Adjust stack and code pointers.
204      */
205     ADD_LOWORD( context->Esp, -28 );
206     context->SegCs = wine_get_cs();
207     context->Eip = (DWORD)__wine_call_from_16_regs;
208 }