Move DPMI segments to winedos.
[wine.git] / dlls / winedos / himem.c
blobced0870eb7e988c510cafd3add1039933b32e5a9
1 /*
2 * DOS upper memory management.
4 * Copyright 2002 Jukka Heinonen
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.
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.
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
21 #include "dosexe.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(dosmem);
27 * Wine DOS memory layout above 640k:
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)
39 * Table of real mode segments and protected mode selectors
40 * for code stubs and other miscellaneous storage.
42 struct DPMI_segments *DOSVM_dpmi_segments = NULL;
45 * First and last address available for upper memory blocks.
47 #define DOSVM_UMB_BOTTOM 0xd0000
48 #define DOSVM_UMB_TOP 0xeffff
51 * First free address for upper memory blocks.
53 static DWORD DOSVM_umb_free = DOSVM_UMB_BOTTOM;
56 /***********************************************************************
57 * DOSVM_AllocUMB
59 * Allocate upper memory block (UMB) from upper memory.
60 * Returned pointer is aligned to 16-byte (paragraph) boundary.
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.
69 static LPVOID DOSVM_AllocUMB( DWORD size )
71 LPVOID ptr = (LPVOID)DOSVM_umb_free;
73 size = ((size + 15) >> 4) << 4;
75 if(DOSVM_umb_free + size - 1 > DOSVM_UMB_TOP) {
76 ERR("Out of upper memory area.\n");
77 return 0;
80 DOSVM_umb_free += size;
81 return ptr;
85 /***********************************************************************
86 * DOSVM_AllocCodeUMB
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.
92 static LPVOID DOSVM_AllocCodeUMB( DWORD size, WORD *segment, WORD *selector )
94 LPVOID ptr = DOSVM_AllocUMB( size );
96 if (segment)
97 *segment = (DWORD)ptr >> 4;
99 if (selector)
100 *selector = SELECTOR_AllocBlock( ptr, size, WINE_LDT_FLAGS_CODE );
102 return ptr;
106 /***********************************************************************
107 * DOSVM_InitSegments
109 * Initializes DOSVM_dpmi_segments. Allocates required memory and
110 * sets up segments and selectors for accessing the memory.
112 void DOSVM_InitSegments( void )
114 LPSTR ptr;
115 int i;
117 static const char wrap_code[]={
118 0xCD,0x31, /* int $0x31 */
119 0xCB /* lret */
122 static const char enter_xms[]=
124 /* XMS hookable entry point */
125 0xEB,0x03, /* jmp entry */
126 0x90,0x90,0x90, /* nop;nop;nop */
127 /* entry: */
128 /* real entry point */
129 /* for simplicity, we'll just use the same hook as DPMI below */
130 0xCD,0x31, /* int $0x31 */
131 0xCB /* lret */
134 static const char enter_pm[]=
136 0x50, /* pushw %ax */
137 0x52, /* pushw %dx */
138 0x55, /* pushw %bp */
139 0x89,0xE5, /* movw %sp,%bp */
140 /* get return CS */
141 0x8B,0x56,0x08, /* movw 8(%bp),%dx */
142 /* just call int 31 here to get into protected mode... */
143 /* it'll check whether it was called from dpmi_seg... */
144 0xCD,0x31, /* int $0x31 */
145 /* we are now in the context of a 16-bit relay call */
146 /* need to fixup our stack;
147 * 16-bit relay return address will be lost,
148 * but we won't worry quite yet
150 0x8E,0xD0, /* movw %ax,%ss */
151 0x66,0x0F,0xB7,0xE5, /* movzwl %bp,%esp */
152 /* set return CS */
153 0x89,0x56,0x08, /* movw %dx,8(%bp) */
154 0x5D, /* popw %bp */
155 0x5A, /* popw %dx */
156 0x58, /* popw %ax */
157 0xCB /* lret */
161 * Allocate pointer array.
163 DOSVM_dpmi_segments = DOSVM_AllocUMB( sizeof(struct DPMI_segments) );
166 * RM / offset 0: Exit from real mode.
167 * RM / offset 2: Points to lret opcode.
169 ptr = DOSVM_AllocCodeUMB( sizeof(wrap_code),
170 &DOSVM_dpmi_segments->wrap_seg, 0 );
171 memcpy( ptr, wrap_code, sizeof(wrap_code) );
174 * RM / offset 0: XMS driver entry.
176 ptr = DOSVM_AllocCodeUMB( sizeof(enter_xms),
177 &DOSVM_dpmi_segments->xms_seg, 0 );
178 memcpy( ptr, enter_xms, sizeof(enter_xms) );
181 * RM / offset 0: Switch to DPMI.
182 * PM / offset 8: DPMI raw mode switch.
184 ptr = DOSVM_AllocCodeUMB( sizeof(enter_pm),
185 &DOSVM_dpmi_segments->dpmi_seg,
186 &DOSVM_dpmi_segments->dpmi_sel );
187 memcpy( ptr, enter_pm, sizeof(enter_pm) );
190 * PM / offset N*6: Interrupt N in DPMI32.
192 ptr = DOSVM_AllocCodeUMB( 6 * 256,
193 0, &DOSVM_dpmi_segments->int48_sel );
194 for(i=0; i<256; i++) {
196 * Each 32-bit interrupt handler is 6 bytes:
197 * 0xCD,<i> = int <i> (nested 16-bit interrupt)
198 * 0x66,0xCA,0x04,0x00 = ret 4 (32-bit far return and pop 4 bytes)
200 ptr[i * 6 + 0] = 0xCD;
201 ptr[i * 6 + 1] = i;
202 ptr[i * 6 + 2] = 0x66;
203 ptr[i * 6 + 3] = 0xCA;
204 ptr[i * 6 + 4] = 0x04;
205 ptr[i * 6 + 5] = 0x00;