Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / kernel / dosmem.c
bloba27c1865ebaf3a81eebc5a818eed022fa324afd3
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 "winreg.h"
37 #include "excpt.h"
38 #include "winternl.h"
39 #include "wine/winbase16.h"
41 #include "kernel_private.h"
42 #include "toolhelp.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(dosmem);
46 WINE_DECLARE_DEBUG_CHANNEL(selector);
48 WORD DOSMEM_0000H; /* segment at 0:0 */
49 WORD DOSMEM_BiosDataSeg; /* BIOS data segment at 0x40:0 */
50 WORD DOSMEM_BiosSysSeg; /* BIOS ROM segment at 0xf000:0 */
52 /* DOS memory highest address (including HMA) */
53 #define DOSMEM_SIZE 0x110000
54 #define DOSMEM_64KB 0x10000
56 /* when looking at DOS and real mode memory, we activate in three different
57 * modes, depending the situation.
58 * 1/ By default (protected mode), the first MB of memory (actually 0x110000,
59 * when you also look at the HMA part) is always reserved, whatever you do.
60 * We allocated some PM selectors to this memory, even if this area is not
61 * committed at startup
62 * 2/ if a program tries to use the memory through the selectors, we actually
63 * commit this memory, made of: BIOS segment, but also some system
64 * information, usually low in memory that we map for the circumstance also
65 * in the BIOS segment, so that we keep the low memory protected (for NULL
66 * pointer deref catching for example). In this case, we're still in PM
67 * mode, accessing part of the "physical" real mode memory. In fact, we don't
68 * map all the first meg, we keep 64k uncommitted to still catch NULL
69 * pointers dereference
70 * 3/ if the process enters the real mode, then we (also) commit the full first
71 * MB of memory (and also initialize the DOS structures in it).
74 /* DOS memory base (linear in process address space) */
75 static char *DOSMEM_dosmem;
76 /* number of bytes protected from _dosmem. 0 when DOS memory is initialized,
77 * 64k otherwise to trap NULL pointers deref */
78 static DWORD DOSMEM_protect;
80 static LONG WINAPI dosmem_handler(EXCEPTION_POINTERS* except);
82 struct winedos_exports winedos;
84 void load_winedos(void)
86 static HANDLE hRunOnce /* = 0 */;
87 static HMODULE hWineDos /* = 0 */;
89 /* FIXME: this isn't 100% thread safe, as we won't catch access to 1MB while
90 * loading winedos (and may return uninitialized valued)
92 if (hWineDos) return;
93 if (hRunOnce == 0)
95 HANDLE hEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
96 if (InterlockedCompareExchangePointer( (PVOID)&hRunOnce, hEvent, 0 ) == 0)
98 HMODULE hModule;
100 /* ok, we're the winning thread */
101 VirtualProtect( DOSMEM_dosmem + DOSMEM_protect,
102 DOSMEM_SIZE - DOSMEM_protect,
103 PAGE_EXECUTE_READWRITE, NULL );
104 if (!(hModule = LoadLibraryA( "winedos.dll" )))
106 ERR("Could not load winedos.dll, DOS subsystem unavailable\n");
107 hWineDos = (HMODULE)1; /* not to try to load it again */
108 return;
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 RtlRemoveVectoredExceptionHandler( dosmem_handler );
121 hWineDos = hModule;
122 SetEvent( hRunOnce );
123 return;
125 /* someone beat us here... */
126 CloseHandle( hEvent );
129 /* and wait for the winner to have finished */
130 WaitForSingleObject( hRunOnce, INFINITE );
133 /******************************************************************
134 * dosmem_handler
136 * Handler to catch access to our 1MB address space reserved for real memory
138 static LONG WINAPI dosmem_handler(EXCEPTION_POINTERS* except)
140 if (except->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
142 DWORD addr = except->ExceptionRecord->ExceptionInformation[1];
143 if (addr >= (ULONG_PTR)DOSMEM_dosmem + DOSMEM_protect &&
144 addr < (ULONG_PTR)DOSMEM_dosmem + DOSMEM_SIZE)
146 load_winedos();
147 return EXCEPTION_CONTINUE_EXECUTION;
150 return EXCEPTION_CONTINUE_SEARCH;
153 /**********************************************************************
154 * setup_dos_mem
156 * Setup the first megabyte for DOS memory access
158 static char* setup_dos_mem(void)
160 int sys_offset = 0;
161 int page_size = getpagesize();
162 void *addr = NULL;
164 if (wine_mmap_is_in_reserved_area( NULL, DOSMEM_SIZE ) != 1)
166 addr = wine_anon_mmap( (void *)page_size, DOSMEM_SIZE-page_size,
167 PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
168 if (addr == (void *)page_size) addr = NULL; /* we got what we wanted */
169 else munmap( addr, DOSMEM_SIZE - page_size );
172 if (!addr)
174 /* now reserve from address 0 */
175 wine_anon_mmap( NULL, DOSMEM_SIZE, 0, MAP_FIXED );
177 /* inform the memory manager that there is a mapping here, but don't commit yet */
178 VirtualAlloc( NULL, DOSMEM_SIZE, MEM_RESERVE | MEM_SYSTEM, PAGE_NOACCESS );
179 sys_offset = 0xf0000;
180 DOSMEM_protect = DOSMEM_64KB;
182 else
184 ERR("Cannot use first megabyte for DOS address space, please report\n" );
185 /* allocate the DOS area somewhere else */
186 addr = VirtualAlloc( NULL, DOSMEM_SIZE, MEM_RESERVE, PAGE_NOACCESS );
187 if (!addr)
189 ERR( "Cannot allocate DOS memory\n" );
190 ExitProcess(1);
193 DOSMEM_dosmem = addr;
194 RtlAddVectoredExceptionHandler(FALSE, dosmem_handler);
195 return (char*)addr + sys_offset;
199 /***********************************************************************
200 * DOSMEM_Init
202 * Create the dos memory segments, and store them into the KERNEL
203 * exported values.
205 BOOL DOSMEM_Init(void)
207 char* sysmem = setup_dos_mem();
209 DOSMEM_0000H = GLOBAL_CreateBlock( GMEM_FIXED, sysmem,
210 DOSMEM_64KB, 0, WINE_LDT_FLAGS_DATA );
211 DOSMEM_BiosDataSeg = GLOBAL_CreateBlock( GMEM_FIXED, sysmem + 0x400,
212 0x100, 0, WINE_LDT_FLAGS_DATA );
213 DOSMEM_BiosSysSeg = GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem + 0xf0000,
214 DOSMEM_64KB, 0, WINE_LDT_FLAGS_DATA );
216 return TRUE;
219 /***********************************************************************
220 * DOSMEM_MapLinearToDos
222 * Linear address to the DOS address space.
224 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
226 if (((char*)ptr >= DOSMEM_dosmem) &&
227 ((char*)ptr < DOSMEM_dosmem + DOSMEM_SIZE))
228 return (UINT)ptr - (UINT)DOSMEM_dosmem;
229 return (UINT)ptr;
233 /***********************************************************************
234 * DOSMEM_MapDosToLinear
236 * DOS linear address to the linear address space.
238 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
240 if (ptr < DOSMEM_SIZE) return (LPVOID)(ptr + (UINT)DOSMEM_dosmem);
241 return (LPVOID)ptr;
245 /***********************************************************************
246 * DOSMEM_MapRealToLinear
248 * Real mode DOS address into a linear pointer
250 LPVOID DOSMEM_MapRealToLinear(DWORD x)
252 LPVOID lin;
254 lin = DOSMEM_dosmem + HIWORD(x) * 16 + LOWORD(x);
255 TRACE_(selector)("(0x%08lx) returns %p.\n", x, lin );
256 return lin;
259 /***********************************************************************
260 * DOSMEM_AllocSelector
262 * Allocates a protected mode selector for a realmode segment.
264 WORD DOSMEM_AllocSelector(WORD realsel)
266 HMODULE16 hModule = GetModuleHandle16("KERNEL");
267 WORD sel;
269 sel=GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem+realsel*16, DOSMEM_64KB,
270 hModule, WINE_LDT_FLAGS_DATA );
271 TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel);
272 return sel;