kernel32: Don't test for HARDDISK and RAMDISK on win9x.
[wine] / dlls / kernel32 / dosmem.c
1 /*
2  * DOS memory emulation
3  *
4  * Copyright 1995 Alexandre Julliard
5  * Copyright 1996 Marcus Meissner
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_MMAN_H
31 # include <sys/mman.h>
32 #endif
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "excpt.h"
37 #include "winternl.h"
38 #include "wine/winbase16.h"
39
40 #include "kernel_private.h"
41 #include "toolhelp.h"
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(dosmem);
45 WINE_DECLARE_DEBUG_CHANNEL(selector);
46
47 WORD DOSMEM_0000H;        /* segment at 0:0 */
48 WORD DOSMEM_BiosDataSeg;  /* BIOS data segment at 0x40:0 */
49 WORD DOSMEM_BiosSysSeg;   /* BIOS ROM segment at 0xf000:0 */
50
51 /* DOS memory highest address (including HMA) */
52 #define DOSMEM_SIZE             0x110000
53 #define DOSMEM_64KB             0x10000
54
55 /* when looking at DOS and real mode memory, we activate in three different
56  * modes, depending the situation.
57  * 1/ By default (protected mode), the first MB of memory (actually 0x110000,
58  *    when you also look at the HMA part) is always reserved, whatever you do.
59  *    We allocated some PM selectors to this memory, even if this area is not
60  *    committed at startup
61  * 2/ if a program tries to use the memory through the selectors, we actually
62  *    commit this memory, made of: BIOS segment, but also some system 
63  *    information, usually low in memory that we map for the circumstance also
64  *    in the BIOS segment, so that we keep the low memory protected (for NULL
65  *    pointer deref catching for example). In this case, we're still in PM
66  *    mode, accessing part of the "physical" real mode memory. In fact, we don't
67  *    map all the first meg, we keep 64k uncommitted to still catch NULL 
68  *    pointers dereference
69  * 3/ if the process enters the real mode, then we (also) commit the full first
70  *    MB of memory (and also initialize the DOS structures in it).
71  */
72
73 /* DOS memory base (linear in process address space) */
74 static char *DOSMEM_dosmem;
75 /* number of bytes protected from _dosmem. 0 when DOS memory is initialized, 
76  * 64k otherwise to trap NULL pointers deref */
77 static DWORD DOSMEM_protect;
78
79 static LONG WINAPI dosmem_handler(EXCEPTION_POINTERS* except);
80
81 struct winedos_exports winedos;
82
83 BOOL load_winedos(void)
84 {
85     static HANDLE       hRunOnce /* = 0 */;
86     static HMODULE      hWineDos /* = 0 */;
87
88     /* FIXME: this isn't 100% thread safe, as we won't catch access to 1MB while
89      * loading winedos (and may return uninitialized valued)
90      */
91     if (hWineDos) goto done;
92     if (hRunOnce == 0)
93     {
94         HANDLE hEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
95         if (InterlockedCompareExchangePointer( (PVOID)&hRunOnce, hEvent, 0 ) == 0)
96         {
97             HMODULE hModule;
98
99             /* ok, we're the winning thread */
100             if (!VirtualProtect( DOSMEM_dosmem + DOSMEM_protect,
101                                  DOSMEM_SIZE - DOSMEM_protect,
102                                  PAGE_READWRITE, NULL ) ||
103                 !(hModule = LoadLibraryA( "winedos.dll" )))
104             {
105                 ERR("Could not load winedos.dll, DOS subsystem unavailable\n");
106                 hModule = (HMODULE)1; /* not to try to load it again */
107             }
108             else
109             {
110 #define GET_ADDR(func)  winedos.func = (void *)GetProcAddress( hModule, #func );
111                 GET_ADDR(AllocDosBlock);
112                 GET_ADDR(FreeDosBlock);
113                 GET_ADDR(ResizeDosBlock);
114                 GET_ADDR(inport);
115                 GET_ADDR(outport);
116                 GET_ADDR(EmulateInterruptPM);
117                 GET_ADDR(CallBuiltinHandler);
118                 GET_ADDR(BiosTick);
119 #undef GET_ADDR
120             }
121             RtlRemoveVectoredExceptionHandler( dosmem_handler );
122             hWineDos = hModule;
123             SetEvent( hRunOnce );
124             goto done;
125         }
126         /* someone beat us here... */
127         CloseHandle( hEvent );
128     }
129
130     /* and wait for the winner to have finished */
131     WaitForSingleObject( hRunOnce, INFINITE );
132  done:
133     return (hWineDos != (HMODULE)1);
134 }
135
136 /******************************************************************
137  *              dosmem_handler
138  *
139  * Handler to catch access to our 1MB address space reserved for real memory
140  */
141 static LONG WINAPI dosmem_handler(EXCEPTION_POINTERS* except)
142 {
143     if (except->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
144     {
145         char *addr = (char *)except->ExceptionRecord->ExceptionInformation[1];
146         if (addr >= DOSMEM_dosmem + DOSMEM_protect && addr < DOSMEM_dosmem + DOSMEM_SIZE)
147         {
148             if (load_winedos()) return EXCEPTION_CONTINUE_EXECUTION;
149         }
150     }
151     return EXCEPTION_CONTINUE_SEARCH;
152 }
153
154 /**********************************************************************
155  *              setup_dos_mem
156  *
157  * Setup the first megabyte for DOS memory access
158  */
159 static char* setup_dos_mem(void)
160 {
161     size_t size;
162     int page_size = getpagesize();
163     void *addr = NULL;
164     void * const low_64k = (void *)DOSMEM_64KB;
165
166     /* check without the first 64K */
167
168     if (wine_mmap_is_in_reserved_area( low_64k, DOSMEM_SIZE - DOSMEM_64KB ) != 1)
169     {
170         addr = wine_anon_mmap( low_64k, DOSMEM_SIZE - DOSMEM_64KB, PROT_READ | PROT_WRITE, 0 );
171         if (addr != low_64k)
172         {
173             if (addr != MAP_FAILED) munmap( addr, DOSMEM_SIZE - DOSMEM_64KB );
174             ERR("Cannot use first megabyte for DOS address space, please report\n" );
175             /* allocate the DOS area somewhere else */
176             if (!(DOSMEM_dosmem = VirtualAlloc( NULL, DOSMEM_SIZE, MEM_RESERVE, PAGE_NOACCESS )))
177             {
178                 ERR( "Cannot allocate DOS memory\n" );
179                 ExitProcess(1);
180             }
181             return DOSMEM_dosmem;
182         }
183     }
184
185     /* now try to allocate the low 64K too */
186
187     if (wine_mmap_is_in_reserved_area( NULL, DOSMEM_64KB ) != 1)
188     {
189         addr = wine_anon_mmap( (void *)page_size, DOSMEM_64KB - page_size, PROT_READ | PROT_WRITE, 0 );
190         if (addr == (void *)page_size)
191         {
192             addr = NULL;
193             TRACE( "successfully mapped low 64K range\n" );
194         }
195         else
196         {
197             if (addr != MAP_FAILED) munmap( addr, DOSMEM_64KB - page_size );
198             addr = low_64k;
199             TRACE( "failed to map low 64K range\n" );
200         }
201     }
202     else addr = NULL;
203
204     /* now reserve the whole range */
205     size = (char *)DOSMEM_SIZE - (char *)addr;
206     wine_anon_mmap( addr, size, PROT_NONE, MAP_FIXED );
207
208     /* inform the memory manager that there is a mapping here, but don't commit yet */
209     VirtualAlloc( addr, size, MEM_RESERVE | MEM_SYSTEM, PAGE_NOACCESS );
210     DOSMEM_protect = DOSMEM_64KB;
211     DOSMEM_dosmem = NULL;
212     return (char *)0xf0000;  /* store sysmem in high addresses for now */
213 }
214
215
216 /***********************************************************************
217  *           DOSMEM_Init
218  *
219  * Create the dos memory segments, and store them into the KERNEL
220  * exported values.
221  */
222 BOOL DOSMEM_Init(void)
223 {
224     char*       sysmem = setup_dos_mem();
225
226     RtlAddVectoredExceptionHandler(FALSE, dosmem_handler);
227     DOSMEM_0000H = GLOBAL_CreateBlock( GMEM_FIXED, sysmem,
228                                        DOSMEM_64KB, 0, WINE_LDT_FLAGS_DATA );
229     DOSMEM_BiosDataSeg = GLOBAL_CreateBlock( GMEM_FIXED, sysmem + 0x400,
230                                              0x100, 0, WINE_LDT_FLAGS_DATA );
231     DOSMEM_BiosSysSeg = GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem + 0xf0000,
232                                             DOSMEM_64KB, 0, WINE_LDT_FLAGS_DATA );
233
234     return TRUE;
235 }
236
237 /***********************************************************************
238  *           DOSMEM_MapLinearToDos
239  *
240  * Linear address to the DOS address space.
241  */
242 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
243 {
244     if (((char*)ptr >= DOSMEM_dosmem) &&
245         ((char*)ptr < DOSMEM_dosmem + DOSMEM_SIZE))
246           return (char *)ptr - DOSMEM_dosmem;
247     return (UINT)ptr;
248 }
249
250
251 /***********************************************************************
252  *           DOSMEM_MapDosToLinear
253  *
254  * DOS linear address to the linear address space.
255  */
256 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
257 {
258     if (ptr < DOSMEM_SIZE) return DOSMEM_dosmem + ptr;
259     return (LPVOID)ptr;
260 }
261
262
263 /***********************************************************************
264  *           DOSMEM_MapRealToLinear
265  *
266  * Real mode DOS address into a linear pointer
267  */
268 LPVOID DOSMEM_MapRealToLinear(DWORD x)
269 {
270    LPVOID       lin;
271
272    lin = DOSMEM_dosmem + HIWORD(x) * 16 + LOWORD(x);
273    TRACE_(selector)("(0x%08x) returns %p.\n", x, lin );
274    return lin;
275 }