winedos: Implement INT2F/AX=1501 (get drive device list).
[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 "wine/winbase16.h"
23 #include "wine/debug.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(int);
26
27 /*
28  * Magic DWORD used to check stack integrity.
29  */
30 #define RELAY_MAGIC 0xabcdef00
31
32 /*
33  * Memory block for temporary 16-bit stacks used with relay calls.
34  */
35 typedef struct {
36     DWORD inuse;          /* non-zero if stack block is in use */
37     DWORD eip;            /* saved ip */
38     DWORD seg_cs;         /* saved cs */
39     DWORD esp;            /* saved sp */
40     DWORD seg_ss;         /* saved ss */
41     DWORD stack_bottom;   /* guard dword */
42     BYTE  stack[256-7*4]; /* 16-bit stack */
43     DWORD stack_top;      /* guard dword */
44 } RELAY_Stack16;
45
46
47 /**********************************************************************
48  *          RELAY_GetPointer
49  *
50  * Get pointer to stack block when given esp pointing to 16-bit stack
51  * inside relay data segment.
52  */
53 static RELAY_Stack16 *RELAY_GetPointer( DWORD offset )
54 {
55     offset = offset / sizeof(RELAY_Stack16) * sizeof(RELAY_Stack16);
56     return MapSL(MAKESEGPTR(DOSVM_dpmi_segments->relay_data_sel, offset));
57 }
58
59
60 /**********************************************************************
61  *          RELAY_MakeShortContext
62  *
63  * Allocate separate 16-bit stack, make stack pointer point to this 
64  * stack and make code pointer point to stub that restores everything.
65  * So, after this routine, SS and CS are guaranteed to be 16-bit.
66  *
67  * Note: This might be called from signal handler, so the stack
68  *       allocation algorithm must be signal safe.
69  */
70 static void RELAY_MakeShortContext( CONTEXT86 *context )
71 {
72     DWORD offset = offsetof(RELAY_Stack16, stack_top);
73     RELAY_Stack16 *stack = RELAY_GetPointer( 0 );
74
75     while (stack->inuse && offset < DOSVM_RELAY_DATA_SIZE) {
76         stack++;
77         offset += sizeof(RELAY_Stack16);
78     }
79
80     if (offset >= DOSVM_RELAY_DATA_SIZE)
81         ERR( "Too many nested interrupts!\n" );
82         
83     stack->inuse = 1;
84     stack->eip = context->Eip;
85     stack->seg_cs = context->SegCs;
86     stack->esp = context->Esp;
87     stack->seg_ss = context->SegSs;
88
89     stack->stack_bottom = RELAY_MAGIC;
90     stack->stack_top = RELAY_MAGIC;
91
92     context->SegSs = DOSVM_dpmi_segments->relay_data_sel;
93     context->Esp = offset;
94     context->SegCs = DOSVM_dpmi_segments->relay_code_sel;
95     context->Eip = 3;
96 }
97
98
99 /**********************************************************************
100  *          RELAY_RelayStub
101  *
102  * This stub is called by __wine_call_from_16_regs in order to marshall
103  * relay parameters.
104  */
105 static void __stdcall RELAY_RelayStub( DOSRELAY       proc, 
106                                        unsigned char *args, 
107                                        void          *ctx86 )
108 {
109     if (proc)
110     {
111         CONTEXT86     *context    = (CONTEXT86*)ctx86;
112         RELAY_Stack16 *stack      = RELAY_GetPointer( context->Esp );
113
114         DWORD          old_seg_cs = context->SegCs;
115         DWORD          old_eip    = context->Eip;
116         DWORD          old_seg_ss = context->SegSs;
117         DWORD          old_esp    = context->Esp;
118
119         context->SegCs = stack->seg_cs;
120         context->Eip   = stack->eip;
121         context->SegSs = stack->seg_ss;
122         context->Esp   = stack->esp;
123
124         proc( context, *(LPVOID *)args );
125
126         stack->seg_cs = context->SegCs;
127         stack->eip    = context->Eip;
128         stack->seg_ss = context->SegSs;
129         stack->esp    = context->Esp;
130
131         context->SegCs = old_seg_cs;
132         context->Eip   = old_eip;
133         context->SegSs = old_seg_ss;
134         context->Esp   = old_esp;
135     }
136 }
137
138
139 /**********************************************************************
140  *          DOSVM_RelayHandler
141  *
142  * Restore saved code and stack pointers and release stack block.
143  */
144 void DOSVM_RelayHandler( CONTEXT86 *context )
145 {
146     RELAY_Stack16 *stack = RELAY_GetPointer( context->Esp );
147
148     context->SegSs = stack->seg_ss;
149     context->Esp = stack->esp;
150     context->SegCs = stack->seg_cs;
151     context->Eip = stack->eip;
152
153     if (!stack->inuse || 
154         stack->stack_bottom != RELAY_MAGIC ||
155         stack->stack_top != RELAY_MAGIC)
156         ERR( "Stack corrupted!\n" );
157
158     stack->inuse = 0;
159 }
160
161
162 /**********************************************************************
163  *          DOSVM_BuildCallFrame
164  *
165  * Modifies the context so that return to context calls DOSRELAY and 
166  * only after return from DOSRELAY the original context will be returned to.
167  */
168 void DOSVM_BuildCallFrame( CONTEXT86 *context, DOSRELAY relay, LPVOID data )
169 {
170     static void (*__wine_call_from_16_regs_ptr)();
171     WORD  code_sel = DOSVM_dpmi_segments->relay_code_sel;
172
173     /*
174      * Allocate separate stack for relay call.
175      */
176     RELAY_MakeShortContext( context );
177
178     /*
179      * Build call frame.
180      */
181     PUSH_WORD16( context, HIWORD(data) );            /* argument.hiword */ 
182     PUSH_WORD16( context, LOWORD(data) );            /* argument.loword */
183     PUSH_WORD16( context, context->SegCs );          /* STACK16FRAME.cs */
184     PUSH_WORD16( context, LOWORD(context->Eip) );    /* STACK16FRAME.ip */
185     PUSH_WORD16( context, LOWORD(context->Ebp) );    /* STACK16FRAME.bp */
186     PUSH_WORD16( context, HIWORD(relay) );           /* STACK16FRAME.entry_point.hiword */
187     PUSH_WORD16( context, LOWORD(relay) );           /* STACK16FRAME.entry_point.loword */
188     PUSH_WORD16( context, 0 );                       /* STACK16FRAME.entry_ip */
189     PUSH_WORD16( context, HIWORD(RELAY_RelayStub) ); /* STACK16FRAME.relay.hiword */
190     PUSH_WORD16( context, LOWORD(RELAY_RelayStub) ); /* STACK16FRAME.relay.loword */
191     PUSH_WORD16( context, 0 );                       /* STACK16FRAME.module_cs.hiword */
192     PUSH_WORD16( context, code_sel );                /* STACK16FRAME.module_cs.loword */
193     PUSH_WORD16( context, 0 );                       /* STACK16FRAME.callfrom_ip.hiword */
194     PUSH_WORD16( context, 0 );                       /* STACK16FRAME.callfrom_ip.loword */
195
196     /*
197      * Adjust code pointer.
198      */
199     if (!__wine_call_from_16_regs_ptr)
200         __wine_call_from_16_regs_ptr = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"),
201                                                               "__wine_call_from_16_regs" );
202     context->SegCs = wine_get_cs();
203     context->Eip = (DWORD)__wine_call_from_16_regs_ptr;
204 }