Framework for the doppler effect.
[wine] / dlls / winedos / himem.c
1 /*
2  * DOS upper memory management.
3  *
4  * Copyright 2002 Jukka Heinonen
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "dosexe.h"
22 #include "wine/debug.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(dosmem);
25
26 /*
27  * Wine DOS memory layout above 640k:
28  *
29  *   a0000 - affff : VGA graphics         (vga.c)
30  *   b0000 - bffff : Monochrome text      (unused)
31  *   b8000 - bffff : VGA text             (vga.c)
32  *   c0000 - cffff : EMS frame            (int67.c)
33  *   d0000 - effff : Free memory for UMBs (himem.c)
34  *   f0000 - fffff : BIOS stuff           (msdos/dosmem.c)
35  *  100000 -10ffff : High memory area     (unused)
36  */
37
38 /*
39  * Table of real mode segments and protected mode selectors 
40  * for code stubs and other miscellaneous storage.
41  */
42 struct DPMI_segments *DOSVM_dpmi_segments = NULL;
43
44 /*
45  * First and last address available for upper memory blocks.
46  */
47 #define DOSVM_UMB_BOTTOM 0xd0000
48 #define DOSVM_UMB_TOP    0xeffff
49
50 /*
51  * First free address for upper memory blocks.
52  */
53 static DWORD DOSVM_umb_free = DOSVM_UMB_BOTTOM;
54
55
56 /***********************************************************************
57  *           DOSVM_AllocUMB
58  *
59  * Allocate upper memory block (UMB) from upper memory.
60  * Returned pointer is aligned to 16-byte (paragraph) boundary.
61  *
62  * This routine is only for allocating static storage for 
63  * Wine internal uses. Allocated memory can be accessed from
64  * real mode, memory is taken from area already mapped and reserved
65  * by Wine and the allocation has very little memory and speed
66  * overhead. Use of this routine also preserves precious DOS
67  * conventional memory.
68  */
69 LPVOID DOSVM_AllocUMB( DWORD size )
70 {
71   LPVOID ptr = (LPVOID)DOSVM_umb_free;
72   
73   size = ((size + 15) >> 4) << 4;
74   
75   if(DOSVM_umb_free + size - 1 > DOSVM_UMB_TOP) {
76     ERR("Out of upper memory area.\n");
77     return 0;
78   }
79
80   DOSVM_umb_free += size;
81   return ptr;
82 }
83
84
85 /***********************************************************************
86  *           DOSVM_AllocCodeUMB
87  *
88  * Allocate upper memory block for storing code stubs.
89  * Initializes real mode segment and 16-bit protected mode selector 
90  * for the allocated code block.
91  */
92 LPVOID DOSVM_AllocCodeUMB( DWORD size, WORD *segment, WORD *selector )
93 {
94   LPVOID ptr = DOSVM_AllocUMB( size );
95   
96   if (segment)
97     *segment = (DWORD)ptr >> 4;
98
99   if (selector)
100     *selector = SELECTOR_AllocBlock( ptr, size, WINE_LDT_FLAGS_CODE );
101
102   return ptr;
103 }
104
105
106 /***********************************************************************
107  *           DOSVM_AllocDataUMB
108  *
109  * Allocate upper memory block for storing data.
110  * Initializes real mode segment and 16-bit protected mode selector 
111  * for the allocated data block.
112  */
113 LPVOID DOSVM_AllocDataUMB( DWORD size, WORD *segment, WORD *selector )
114 {
115   LPVOID ptr = DOSVM_AllocUMB( size );
116   
117   if (segment)
118     *segment = (DWORD)ptr >> 4;
119
120   if (selector)
121     *selector = SELECTOR_AllocBlock( ptr, size, WINE_LDT_FLAGS_DATA );
122
123   return ptr;
124 }
125
126
127 /***********************************************************************
128  *           DOSVM_InitSegments
129  *
130  * Initializes DOSVM_dpmi_segments. Allocates required memory and
131  * sets up segments and selectors for accessing the memory.
132  */
133 void DOSVM_InitSegments( void )
134 {
135     LPSTR ptr;
136     int   i;
137
138     static const char wrap_code[]={
139      0xCD,0x31, /* int $0x31 */
140      0xCB       /* lret */
141     };
142
143     static const char enter_xms[]=
144     {
145         /* XMS hookable entry point */
146         0xEB,0x03,           /* jmp entry */
147         0x90,0x90,0x90,      /* nop;nop;nop */
148                              /* entry: */
149         /* real entry point */
150         /* for simplicity, we'll just use the same hook as DPMI below */
151         0xCD,0x31,           /* int $0x31 */
152         0xCB                 /* lret */
153     };
154
155     static const char enter_pm[]=
156     {
157         0x50,                /* pushw %ax */
158         0x52,                /* pushw %dx */
159         0x55,                /* pushw %bp */
160         0x89,0xE5,           /* movw %sp,%bp */
161         /* get return CS */
162         0x8B,0x56,0x08,      /* movw 8(%bp),%dx */
163         /* just call int 31 here to get into protected mode... */
164         /* it'll check whether it was called from dpmi_seg... */
165         0xCD,0x31,           /* int $0x31 */
166         /* we are now in the context of a 16-bit relay call */
167         /* need to fixup our stack;
168          * 16-bit relay return address will be lost, 
169          * but we won't worry quite yet 
170          */
171         0x8E,0xD0,           /* movw %ax,%ss */
172         0x66,0x0F,0xB7,0xE5, /* movzwl %bp,%esp */
173         /* set return CS */
174         0x89,0x56,0x08,      /* movw %dx,8(%bp) */
175         0x5D,                /* popw %bp */
176         0x5A,                /* popw %dx */
177         0x58,                /* popw %ax */
178         0xCB                 /* lret */
179     };
180
181     static const char relay[]=
182     {
183         0xca, 0x04, 0x00, /* 16-bit far return and pop 4 bytes (relay void* arg) */
184         0xcd, 0x31        /* int 31 */
185     };
186
187     /*
188      * Allocate pointer array.
189      */
190     DOSVM_dpmi_segments = DOSVM_AllocUMB( sizeof(struct DPMI_segments) );
191
192     /*
193      * RM / offset 0: Exit from real mode.
194      * RM / offset 2: Points to lret opcode.
195      */
196     ptr = DOSVM_AllocCodeUMB( sizeof(wrap_code),
197                               &DOSVM_dpmi_segments->wrap_seg, 0 );
198     memcpy( ptr, wrap_code, sizeof(wrap_code) );
199
200     /*
201      * RM / offset 0: XMS driver entry.
202      */
203     ptr = DOSVM_AllocCodeUMB( sizeof(enter_xms), 
204                               &DOSVM_dpmi_segments->xms_seg, 0 );
205     memcpy( ptr, enter_xms, sizeof(enter_xms) );
206
207     /*
208      * RM / offset 0: Switch to DPMI.
209      * PM / offset 8: DPMI raw mode switch.
210      */
211     ptr = DOSVM_AllocCodeUMB( sizeof(enter_pm), 
212                               &DOSVM_dpmi_segments->dpmi_seg,
213                               &DOSVM_dpmi_segments->dpmi_sel );
214     memcpy( ptr, enter_pm, sizeof(enter_pm) );
215
216     /*
217      * PM / offset N*6: Interrupt N in DPMI32.
218      */
219     ptr = DOSVM_AllocCodeUMB( 6 * 256,
220                               0, &DOSVM_dpmi_segments->int48_sel );
221     for(i=0; i<256; i++) {
222         /*
223          * Each 32-bit interrupt handler is 6 bytes:
224          * 0xCD,<i>            = int <i> (nested 16-bit interrupt)
225          * 0x66,0xCA,0x04,0x00 = ret 4   (32-bit far return and pop 4 bytes / eflags)
226          */
227         ptr[i * 6 + 0] = 0xCD;
228         ptr[i * 6 + 1] = i;
229         ptr[i * 6 + 2] = 0x66;
230         ptr[i * 6 + 3] = 0xCA;
231         ptr[i * 6 + 4] = 0x04;
232         ptr[i * 6 + 5] = 0x00;
233     }
234
235     /*
236      * PM / offset N*5: Interrupt N in 16-bit protected mode.
237      */
238     ptr = DOSVM_AllocCodeUMB( 5 * 256,
239                               0, &DOSVM_dpmi_segments->int16_sel );
240     for(i=0; i<256; i++) {
241         /*
242          * Each 16-bit interrupt handler is 5 bytes:
243          * 0xCD,<i>       = int <i> (interrupt)
244          * 0xCA,0x02,0x00 = ret 2   (16-bit far return and pop 2 bytes / eflags)
245          */
246         ptr[i * 5 + 0] = 0xCD;
247         ptr[i * 5 + 1] = i;
248         ptr[i * 5 + 2] = 0xCA;
249         ptr[i * 5 + 3] = 0x02;
250         ptr[i * 5 + 4] = 0x00;
251     }
252
253     /*
254      * PM / offset 0: Stub where __wine_call_from_16_regs returns.
255      * PM / offset 3: Stub which swaps back to 32-bit application code/stack.
256      */
257     ptr = DOSVM_AllocCodeUMB( sizeof(relay), 
258                               0,  &DOSVM_dpmi_segments->relay_code_sel);
259     memcpy( ptr, relay, sizeof(relay) );
260
261     /*
262      * Space for 16-bit stack used by relay code.
263      */
264     ptr = DOSVM_AllocDataUMB( DOSVM_RELAY_DATA_SIZE, 
265                               0, &DOSVM_dpmi_segments->relay_data_sel);
266     memset( ptr, 0, DOSVM_RELAY_DATA_SIZE );
267 }