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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_MMAN_H
31 # include <sys/mman.h>
38 #include "wine/winbase16.h"
40 #include "kernel16_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(dosmem
);
44 WINE_DECLARE_DEBUG_CHANNEL(selector
);
46 WORD DOSMEM_0000H
; /* segment at 0:0 */
47 WORD DOSMEM_BiosDataSeg
; /* BIOS data segment at 0x40:0 */
48 WORD DOSMEM_BiosSysSeg
; /* BIOS ROM segment at 0xf000:0 */
50 /* DOS memory highest address (including HMA) */
51 #define DOSMEM_SIZE 0x110000
52 #define DOSMEM_64KB 0x10000
54 /* when looking at DOS and real mode memory, we activate in three different
55 * modes, depending the situation.
56 * 1/ By default (protected mode), the first MB of memory (actually 0x110000,
57 * when you also look at the HMA part) is always reserved, whatever you do.
58 * We allocated some PM selectors to this memory, even if this area is not
59 * committed at startup
60 * 2/ if a program tries to use the memory through the selectors, we actually
61 * commit this memory, made of: BIOS segment, but also some system
62 * information, usually low in memory that we map for the circumstance also
63 * in the BIOS segment, so that we keep the low memory protected (for NULL
64 * pointer deref catching for example). In this case, we're still in PM
65 * mode, accessing part of the "physical" real mode memory. In fact, we don't
66 * map all the first meg, we keep 64k uncommitted to still catch NULL
67 * pointers dereference
68 * 3/ if the process enters the real mode, then we (also) commit the full first
69 * MB of memory (and also initialize the DOS structures in it).
72 /* DOS memory base (linear in process address space) */
73 static char *DOSMEM_dosmem
;
74 /* number of bytes protected from _dosmem. 0 when DOS memory is initialized,
75 * 64k otherwise to trap NULL pointers deref */
76 static DWORD DOSMEM_protect
;
78 static LONG WINAPI
dosmem_handler(EXCEPTION_POINTERS
* except
);
80 struct winedos_exports winedos
;
82 BOOL
load_winedos(void)
84 static HANDLE hRunOnce
/* = 0 */;
85 static HMODULE hWineDos
/* = 0 */;
87 /* FIXME: this isn't 100% thread safe, as we won't catch access to 1MB while
88 * loading winedos (and may return uninitialized valued)
90 if (hWineDos
) goto done
;
93 HANDLE hEvent
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
94 if (InterlockedCompareExchangePointer( &hRunOnce
, hEvent
, 0 ) == 0)
98 /* ok, we're the winning thread */
99 if (!VirtualProtect( DOSMEM_dosmem
+ DOSMEM_protect
,
100 DOSMEM_SIZE
- DOSMEM_protect
,
101 PAGE_READWRITE
, NULL
) ||
102 !(hModule
= LoadLibraryA( "winedos.dll" )))
104 ERR("Could not load winedos.dll, DOS subsystem unavailable\n");
105 hModule
= (HMODULE
)1; /* not to try to load it again */
109 #define GET_ADDR(func) winedos.func = (void *)GetProcAddress( hModule, #func );
110 GET_ADDR(AllocDosBlock
);
111 GET_ADDR(FreeDosBlock
);
112 GET_ADDR(ResizeDosBlock
);
115 GET_ADDR(EmulateInterruptPM
);
116 GET_ADDR(CallBuiltinHandler
);
119 RtlRemoveVectoredExceptionHandler( dosmem_handler
);
121 SetEvent( hRunOnce
);
124 /* someone beat us here... */
125 CloseHandle( hEvent
);
128 /* and wait for the winner to have finished */
129 WaitForSingleObject( hRunOnce
, INFINITE
);
131 return (hWineDos
!= (HMODULE
)1);
134 /******************************************************************
137 * Handler to catch access to our 1MB address space reserved for real memory
139 static LONG WINAPI
dosmem_handler(EXCEPTION_POINTERS
* except
)
141 if (except
->ExceptionRecord
->ExceptionCode
== EXCEPTION_ACCESS_VIOLATION
)
143 char *addr
= (char *)except
->ExceptionRecord
->ExceptionInformation
[1];
144 if (addr
>= DOSMEM_dosmem
+ DOSMEM_protect
&& addr
< DOSMEM_dosmem
+ DOSMEM_SIZE
)
146 if (load_winedos()) return EXCEPTION_CONTINUE_EXECUTION
;
149 return EXCEPTION_CONTINUE_SEARCH
;
152 /***********************************************************************
155 * Create the dos memory segments, and store them into the KERNEL
158 BOOL
DOSMEM_Init(void)
161 void *addr
= (void *)1;
162 SIZE_T size
= DOSMEM_SIZE
- 1;
164 if (NtAllocateVirtualMemory( GetCurrentProcess(), &addr
, 0, &size
,
165 MEM_RESERVE
| MEM_COMMIT
, PAGE_NOACCESS
))
167 ERR( "Cannot allocate DOS memory\n" );
171 if (addr
<= (void *)DOSMEM_64KB
)
174 DOSMEM_protect
= DOSMEM_64KB
;
175 sysmem
= (char *)0xf0000; /* store sysmem in high addresses for now */
179 WARN( "First megabyte not available for DOS address space.\n" );
180 DOSMEM_dosmem
= addr
;
182 sysmem
= DOSMEM_dosmem
;
185 RtlAddVectoredExceptionHandler(FALSE
, dosmem_handler
);
186 DOSMEM_0000H
= GLOBAL_CreateBlock( GMEM_FIXED
, sysmem
,
187 DOSMEM_64KB
, 0, WINE_LDT_FLAGS_DATA
);
188 DOSMEM_BiosDataSeg
= GLOBAL_CreateBlock( GMEM_FIXED
, sysmem
+ 0x400,
189 0x100, 0, WINE_LDT_FLAGS_DATA
);
190 DOSMEM_BiosSysSeg
= GLOBAL_CreateBlock( GMEM_FIXED
, DOSMEM_dosmem
+ 0xf0000,
191 DOSMEM_64KB
, 0, WINE_LDT_FLAGS_DATA
);
196 /***********************************************************************
197 * DOSMEM_MapLinearToDos
199 * Linear address to the DOS address space.
201 UINT
DOSMEM_MapLinearToDos(LPVOID ptr
)
203 if (((char*)ptr
>= DOSMEM_dosmem
) &&
204 ((char*)ptr
< DOSMEM_dosmem
+ DOSMEM_SIZE
))
205 return (char *)ptr
- DOSMEM_dosmem
;
210 /***********************************************************************
211 * DOSMEM_MapDosToLinear
213 * DOS linear address to the linear address space.
215 LPVOID
DOSMEM_MapDosToLinear(UINT ptr
)
217 if (ptr
< DOSMEM_SIZE
) return DOSMEM_dosmem
+ ptr
;
222 /***********************************************************************
223 * DOSMEM_MapRealToLinear
225 * Real mode DOS address into a linear pointer
227 LPVOID
DOSMEM_MapRealToLinear(DWORD x
)
231 lin
= DOSMEM_dosmem
+ HIWORD(x
) * 16 + LOWORD(x
);
232 TRACE_(selector
)("(0x%08x) returns %p.\n", x
, lin
);