- don't include winreg.h
[wine/multimedia.git] / dlls / kernel / dosmem.c
blob56b2d768e707293689ea5ba89b6234be1a39fd3b
1 /*
2 * DOS memory emulation
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Marcus Meissner
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
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
34 #include "windef.h"
35 #include "winbase.h"
36 #include "excpt.h"
37 #include "winternl.h"
38 #include "wine/winbase16.h"
40 #include "kernel_private.h"
41 #include "toolhelp.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dosmem);
45 WINE_DECLARE_DEBUG_CHANNEL(selector);
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 */
51 /* DOS memory highest address (including HMA) */
52 #define DOSMEM_SIZE 0x110000
53 #define DOSMEM_64KB 0x10000
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).
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;
79 static LONG WINAPI dosmem_handler(EXCEPTION_POINTERS* except);
81 struct winedos_exports winedos;
83 void load_winedos(void)
85 static HANDLE hRunOnce /* = 0 */;
86 static HMODULE hWineDos /* = 0 */;
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)
91 if (hWineDos) return;
92 if (hRunOnce == 0)
94 HANDLE hEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
95 if (InterlockedCompareExchangePointer( (PVOID)&hRunOnce, hEvent, 0 ) == 0)
97 HMODULE hModule;
99 /* ok, we're the winning thread */
100 VirtualProtect( DOSMEM_dosmem + DOSMEM_protect,
101 DOSMEM_SIZE - DOSMEM_protect,
102 PAGE_EXECUTE_READWRITE, NULL );
103 if (!(hModule = LoadLibraryA( "winedos.dll" )))
105 ERR("Could not load winedos.dll, DOS subsystem unavailable\n");
106 hWineDos = (HMODULE)1; /* not to try to load it again */
107 return;
109 #define GET_ADDR(func) winedos.func = (void *)GetProcAddress( hModule, #func );
110 GET_ADDR(AllocDosBlock);
111 GET_ADDR(FreeDosBlock);
112 GET_ADDR(ResizeDosBlock);
113 GET_ADDR(inport);
114 GET_ADDR(outport);
115 GET_ADDR(EmulateInterruptPM);
116 GET_ADDR(CallBuiltinHandler);
117 GET_ADDR(BiosTick);
118 #undef GET_ADDR
119 RtlRemoveVectoredExceptionHandler( dosmem_handler );
120 hWineDos = hModule;
121 SetEvent( hRunOnce );
122 return;
124 /* someone beat us here... */
125 CloseHandle( hEvent );
128 /* and wait for the winner to have finished */
129 WaitForSingleObject( hRunOnce, INFINITE );
132 /******************************************************************
133 * dosmem_handler
135 * Handler to catch access to our 1MB address space reserved for real memory
137 static LONG WINAPI dosmem_handler(EXCEPTION_POINTERS* except)
139 if (except->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
141 DWORD addr = except->ExceptionRecord->ExceptionInformation[1];
142 if (addr >= (ULONG_PTR)DOSMEM_dosmem + DOSMEM_protect &&
143 addr < (ULONG_PTR)DOSMEM_dosmem + DOSMEM_SIZE)
145 load_winedos();
146 return EXCEPTION_CONTINUE_EXECUTION;
149 return EXCEPTION_CONTINUE_SEARCH;
152 /**********************************************************************
153 * setup_dos_mem
155 * Setup the first megabyte for DOS memory access
157 static char* setup_dos_mem(void)
159 int sys_offset = 0;
160 int page_size = getpagesize();
161 void *addr = NULL;
163 if (wine_mmap_is_in_reserved_area( NULL, DOSMEM_SIZE ) != 1)
165 addr = wine_anon_mmap( (void *)page_size, DOSMEM_SIZE-page_size,
166 PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
167 if (addr == (void *)page_size) addr = NULL; /* we got what we wanted */
168 else munmap( addr, DOSMEM_SIZE - page_size );
171 if (!addr)
173 /* now reserve from address 0 */
174 wine_anon_mmap( NULL, DOSMEM_SIZE, 0, MAP_FIXED );
176 /* inform the memory manager that there is a mapping here, but don't commit yet */
177 VirtualAlloc( NULL, DOSMEM_SIZE, MEM_RESERVE | MEM_SYSTEM, PAGE_NOACCESS );
178 sys_offset = 0xf0000;
179 DOSMEM_protect = DOSMEM_64KB;
181 else
183 ERR("Cannot use first megabyte for DOS address space, please report\n" );
184 /* allocate the DOS area somewhere else */
185 addr = VirtualAlloc( NULL, DOSMEM_SIZE, MEM_RESERVE, PAGE_NOACCESS );
186 if (!addr)
188 ERR( "Cannot allocate DOS memory\n" );
189 ExitProcess(1);
192 DOSMEM_dosmem = addr;
193 RtlAddVectoredExceptionHandler(FALSE, dosmem_handler);
194 return (char*)addr + sys_offset;
198 /***********************************************************************
199 * DOSMEM_Init
201 * Create the dos memory segments, and store them into the KERNEL
202 * exported values.
204 BOOL DOSMEM_Init(void)
206 char* sysmem = setup_dos_mem();
208 DOSMEM_0000H = GLOBAL_CreateBlock( GMEM_FIXED, sysmem,
209 DOSMEM_64KB, 0, WINE_LDT_FLAGS_DATA );
210 DOSMEM_BiosDataSeg = GLOBAL_CreateBlock( GMEM_FIXED, sysmem + 0x400,
211 0x100, 0, WINE_LDT_FLAGS_DATA );
212 DOSMEM_BiosSysSeg = GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem + 0xf0000,
213 DOSMEM_64KB, 0, WINE_LDT_FLAGS_DATA );
215 return TRUE;
218 /***********************************************************************
219 * DOSMEM_MapLinearToDos
221 * Linear address to the DOS address space.
223 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
225 if (((char*)ptr >= DOSMEM_dosmem) &&
226 ((char*)ptr < DOSMEM_dosmem + DOSMEM_SIZE))
227 return (UINT)ptr - (UINT)DOSMEM_dosmem;
228 return (UINT)ptr;
232 /***********************************************************************
233 * DOSMEM_MapDosToLinear
235 * DOS linear address to the linear address space.
237 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
239 if (ptr < DOSMEM_SIZE) return (LPVOID)(ptr + (UINT)DOSMEM_dosmem);
240 return (LPVOID)ptr;
244 /***********************************************************************
245 * DOSMEM_MapRealToLinear
247 * Real mode DOS address into a linear pointer
249 LPVOID DOSMEM_MapRealToLinear(DWORD x)
251 LPVOID lin;
253 lin = DOSMEM_dosmem + HIWORD(x) * 16 + LOWORD(x);
254 TRACE_(selector)("(0x%08lx) returns %p.\n", x, lin );
255 return lin;
258 /***********************************************************************
259 * DOSMEM_AllocSelector
261 * Allocates a protected mode selector for a realmode segment.
263 WORD DOSMEM_AllocSelector(WORD realsel)
265 HMODULE16 hModule = GetModuleHandle16("KERNEL");
266 WORD sel;
268 sel=GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem+realsel*16, DOSMEM_64KB,
269 hModule, WINE_LDT_FLAGS_DATA );
270 TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel);
271 return sel;