1 | /* $Id: mmap.cpp,v 1.33 2000-02-16 14:25:42 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 Memory mapped file & view classes
|
---|
5 | *
|
---|
6 | * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | *
|
---|
8 | * NOTE: Memory mapping DOES NOT work when kernel-mode code causes
|
---|
9 | * a pagefault in the memory mapped object. (exceptions aren't
|
---|
10 | * dispatched to our exception handler until after the kernel mode
|
---|
11 | * call returns (too late))
|
---|
12 | *
|
---|
13 | * NOTE: Are apps allowed to change the protection flags of memory mapped pages?
|
---|
14 | * I'm assuming they aren't for now.
|
---|
15 | *
|
---|
16 | * TODO: Handles returned should be usable by all apis that accept file handles
|
---|
17 | * TODO: Sharing memory mapped files between multiple processes
|
---|
18 | * TODO: Memory mapped files with views that extend the file (not 100% correct now)
|
---|
19 | *
|
---|
20 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
21 | *
|
---|
22 | */
|
---|
23 | #include <os2win.h>
|
---|
24 | #include <stdio.h>
|
---|
25 | #include <stdlib.h>
|
---|
26 | #include <string.h>
|
---|
27 | #include <win\virtual.h>
|
---|
28 | #include <vmutex.h>
|
---|
29 | #include <handlemanager.h>
|
---|
30 | #include "mmap.h"
|
---|
31 | #include "oslibdos.h"
|
---|
32 | #include <winimagepeldr.h>
|
---|
33 |
|
---|
34 | #define DBG_LOCALLOG DBG_mmap
|
---|
35 | #include "dbglocal.h"
|
---|
36 |
|
---|
37 | //Global DLL Data
|
---|
38 | #pragma data_seg(_GLOBALDATA)
|
---|
39 | Win32MemMap *Win32MemMap::memmaps = NULL;
|
---|
40 | VMutex globalmapMutex(VMUTEX_SHARED);
|
---|
41 | #pragma data_seg()
|
---|
42 | VMutex globalviewMutex;
|
---|
43 | Win32MemMapView *Win32MemMapView::mapviews = NULL;
|
---|
44 |
|
---|
45 | //******************************************************************************
|
---|
46 | //TODO: sharing between processes
|
---|
47 | //******************************************************************************
|
---|
48 | Win32MemMap::Win32MemMap(HFILE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName)
|
---|
49 | : nrMappings(0), pMapping(NULL), mMapAccess(0), referenced(0), image(0)
|
---|
50 | {
|
---|
51 | globalmapMutex.enter();
|
---|
52 | next = memmaps;
|
---|
53 | memmaps = this;
|
---|
54 | globalmapMutex.leave();
|
---|
55 |
|
---|
56 | hMemFile = hfile;
|
---|
57 |
|
---|
58 | mSize = size;
|
---|
59 | mProtFlags = fdwProtect;
|
---|
60 | mProcessId = GetCurrentProcess();
|
---|
61 |
|
---|
62 | if(lpszName) {
|
---|
63 | lpszMapName = (char *)_smalloc(strlen(lpszName)+1);
|
---|
64 | strcpy(lpszMapName, lpszName);
|
---|
65 | }
|
---|
66 | else lpszMapName = NULL;
|
---|
67 | }
|
---|
68 | //******************************************************************************
|
---|
69 | //Map constructor used for executable image maps (only used internally)
|
---|
70 | //******************************************************************************
|
---|
71 | Win32MemMap::Win32MemMap(Win32PeLdrImage *pImage, ULONG baseAddress, ULONG size)
|
---|
72 | : nrMappings(0), pMapping(NULL), mMapAccess(0), referenced(0)
|
---|
73 | {
|
---|
74 | globalmapMutex.enter();
|
---|
75 | next = memmaps;
|
---|
76 | memmaps = this;
|
---|
77 | globalmapMutex.leave();
|
---|
78 |
|
---|
79 | hMemFile = -1;
|
---|
80 |
|
---|
81 | mSize = size;
|
---|
82 | mProtFlags = PAGE_READWRITE;
|
---|
83 | mProcessId = GetCurrentProcess();
|
---|
84 |
|
---|
85 | pMapping = (LPVOID)baseAddress;
|
---|
86 |
|
---|
87 | image = pImage;
|
---|
88 | lpszMapName= NULL;
|
---|
89 | }
|
---|
90 | //******************************************************************************
|
---|
91 | //******************************************************************************
|
---|
92 | BOOL Win32MemMap::Init(HANDLE hMemMap)
|
---|
93 | {
|
---|
94 | mapMutex.enter();
|
---|
95 | if(hMemFile != -1)
|
---|
96 | {
|
---|
97 | if(DuplicateHandle(mProcessId, hMemFile, GetCurrentProcess(),
|
---|
98 | &hMemFile, 0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE)
|
---|
99 | {
|
---|
100 | dprintf(("Win32MemMap::Init: DuplicateHandle failed!"));
|
---|
101 | goto fail;
|
---|
102 | }
|
---|
103 | mSize = SetFilePointer(hMemFile, 0, NULL, FILE_END);
|
---|
104 | if(mSize == -1) {
|
---|
105 | dprintf(("Win32MemMap::init: SetFilePointer failed to set pos end"));
|
---|
106 | goto fail;
|
---|
107 | }
|
---|
108 | //SvL: Temporary limitation of size (Warp Server Advanced doesn't allow
|
---|
109 | // one to reserve more than 450 MB (unless you override the virtual
|
---|
110 | // memory max limit) of continuous memory; (Warp 4 much less))
|
---|
111 | if(mSize > 64*1024*1024) {
|
---|
112 | mSize = 64*1024*1024;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | dprintf(("CreateFileMappingA for file %x, prot %x size %d, name %s", hMemFile, mProtFlags, mSize, lpszMapName));
|
---|
117 | this->hMemMap = hMemMap;
|
---|
118 | mapMutex.leave();
|
---|
119 | return TRUE;
|
---|
120 | fail:
|
---|
121 | mapMutex.leave();
|
---|
122 | return FALSE;
|
---|
123 | }
|
---|
124 | //******************************************************************************
|
---|
125 | //******************************************************************************
|
---|
126 | Win32MemMap::~Win32MemMap()
|
---|
127 | {
|
---|
128 | Win32MemMapView::deleteViews(this); //delete all views of our memory mapped file
|
---|
129 |
|
---|
130 | dprintf(("Win32MemMap dtor: deleting view %x %x", pMapping, mSize));
|
---|
131 |
|
---|
132 | mapMutex.enter();
|
---|
133 | if(lpszMapName) {
|
---|
134 | free(lpszMapName);
|
---|
135 | }
|
---|
136 | if(pMapping && !image) {
|
---|
137 | if(lpszMapName) {
|
---|
138 | OSLibDosFreeMem(pMapping);
|
---|
139 | }
|
---|
140 | else VirtualFree(pMapping, mSize, MEM_RELEASE);
|
---|
141 |
|
---|
142 | pMapping = NULL;
|
---|
143 | }
|
---|
144 | if(hMemFile != -1) {
|
---|
145 | CloseHandle(hMemFile);
|
---|
146 | hMemFile = -1;
|
---|
147 | }
|
---|
148 | mapMutex.leave();
|
---|
149 |
|
---|
150 | globalmapMutex.enter();
|
---|
151 | Win32MemMap *map = memmaps;
|
---|
152 |
|
---|
153 | if(map == this) {
|
---|
154 | memmaps = next;
|
---|
155 | }
|
---|
156 | else {
|
---|
157 | while(map->next) {
|
---|
158 | if(map->next == this)
|
---|
159 | break;
|
---|
160 | map = map->next;
|
---|
161 | }
|
---|
162 | if(map->next) {
|
---|
163 | map->next = next;
|
---|
164 | }
|
---|
165 | else dprintf(("Win32MemMap::~Win32MemMap: map not found!! (%x)", this));
|
---|
166 | }
|
---|
167 | globalmapMutex.leave();
|
---|
168 | }
|
---|
169 | //******************************************************************************
|
---|
170 | //We determine whether a page has been modified by checking it's protection flags
|
---|
171 | //If the write flag is set, this means commitPage had to enable this due to a pagefault
|
---|
172 | //(all pages are readonly until the app tries to write to it)
|
---|
173 | //******************************************************************************
|
---|
174 | BOOL Win32MemMap::commitPage(ULONG offset, BOOL fWriteAccess, int nrpages)
|
---|
175 | {
|
---|
176 | MEMORY_BASIC_INFORMATION memInfo;
|
---|
177 | LPVOID lpPageFaultAddr = (LPVOID)((ULONG)pMapping + offset);
|
---|
178 | DWORD pageAddr = (DWORD)lpPageFaultAddr & ~0xFFF;
|
---|
179 | DWORD oldProt, newProt, nrBytesRead, size;
|
---|
180 | int i;
|
---|
181 |
|
---|
182 | // mapMutex.enter();
|
---|
183 |
|
---|
184 | if(image) {
|
---|
185 | return image->commitPage(pageAddr, fWriteAccess);
|
---|
186 | }
|
---|
187 | newProt = mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY);
|
---|
188 |
|
---|
189 | dprintf(("Win32MemMap::commitPage %x (faultaddr %x)", pageAddr, lpPageFaultAddr));
|
---|
190 | if(hMemFile != -1) {
|
---|
191 | // for(i=0;i<nrpages;i++) {
|
---|
192 | if(VirtualQuery((LPSTR)pageAddr, &memInfo, nrpages*PAGE_SIZE) == 0) {
|
---|
193 | dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed for %x", pageAddr, nrpages*PAGE_SIZE));
|
---|
194 | goto fail;
|
---|
195 | }
|
---|
196 | //Only changes the state of the pages with the same attribute flags
|
---|
197 | //(returned in memInfo.RegionSize)
|
---|
198 | //If it's smaller than the mNrPages, it simply means one or more of the
|
---|
199 | //other pages have already been committed
|
---|
200 | if(memInfo.State & MEM_COMMIT)
|
---|
201 | {//if it's already committed, then the app tried to write to it
|
---|
202 | if(!fWriteAccess) {
|
---|
203 | dprintf(("Win32MemMap::commitPage: Huh? Already committed and not trying to write (%x,%x) failed for %x", pageAddr, memInfo.RegionSize));
|
---|
204 | goto fail;
|
---|
205 | }
|
---|
206 | if(VirtualProtect((LPVOID)pageAddr, memInfo.RegionSize, newProt, &oldProt) == FALSE) {
|
---|
207 | dprintf(("Win32MemMap::commitPage: Failed to set write flag on page (%x,%x) failed for %x", pageAddr, memInfo.RegionSize));
|
---|
208 | goto fail;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | else {
|
---|
212 | if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE) == FALSE) {
|
---|
213 | goto fail;
|
---|
214 | }
|
---|
215 | if(!fWriteAccess) {
|
---|
216 | offset = pageAddr - (ULONG)pMapping;
|
---|
217 | size = memInfo.RegionSize;
|
---|
218 | if(offset + size > mSize) {
|
---|
219 | dprintf(("Adjusting size from %d to %d", size, mSize - offset));
|
---|
220 | size = mSize - offset;
|
---|
221 | }
|
---|
222 | if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
|
---|
223 | dprintf(("Win32MemMap::commitPage: SetFilePointer failed to set pos to %x", offset));
|
---|
224 | goto fail;
|
---|
225 | }
|
---|
226 | if(ReadFile(hMemFile, (LPSTR)pageAddr, size, &nrBytesRead, NULL) == FALSE) {
|
---|
227 | dprintf(("Win32MemMap::commitPage: ReadFile failed for %x", pageAddr));
|
---|
228 | goto fail;
|
---|
229 | }
|
---|
230 | if(nrBytesRead != size) {
|
---|
231 | dprintf(("Win32MemMap::commitPage: ReadFile didn't read all bytes for %x", pageAddr));
|
---|
232 | goto fail;
|
---|
233 | }
|
---|
234 | }
|
---|
235 | if(mProtFlags != PAGE_READWRITE) {
|
---|
236 | if(VirtualProtect((LPVOID)pageAddr, memInfo.RegionSize, newProt, &oldProt) == FALSE) {
|
---|
237 | goto fail;
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|
241 | // pageAddr += PAGE_SIZE;
|
---|
242 | // }
|
---|
243 | }
|
---|
244 | else {
|
---|
245 | ULONG sizeleft = nrpages*PAGE_SIZE;
|
---|
246 | while(sizeleft) {
|
---|
247 | if(VirtualQuery((LPSTR)pageAddr, &memInfo, sizeleft) == 0) {
|
---|
248 | dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed", pageAddr, sizeleft));
|
---|
249 | goto fail;
|
---|
250 | }
|
---|
251 | if(!(memInfo.State & MEM_COMMIT))
|
---|
252 | {//if it's already committed, then the app tried to write to it
|
---|
253 | if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, newProt) == FALSE)
|
---|
254 | goto fail;
|
---|
255 | }
|
---|
256 | memInfo.RegionSize = (memInfo.RegionSize+PAGE_SIZE-1) & ~0xfff;
|
---|
257 | pageAddr += memInfo.RegionSize;
|
---|
258 | sizeleft -= memInfo.RegionSize;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | // mapMutex.leave();
|
---|
263 | return TRUE;
|
---|
264 | fail:
|
---|
265 | // mapMutex.leave();
|
---|
266 | return FALSE;
|
---|
267 | }
|
---|
268 | //******************************************************************************
|
---|
269 | //******************************************************************************
|
---|
270 | BOOL Win32MemMap::unmapViewOfFile(Win32MemMapView *view)
|
---|
271 | {
|
---|
272 | mapMutex.enter();
|
---|
273 |
|
---|
274 | if(nrMappings == 0)
|
---|
275 | goto fail;
|
---|
276 |
|
---|
277 | delete view;
|
---|
278 |
|
---|
279 | if(--nrMappings) {
|
---|
280 | VirtualFree(pMapping, mSize, MEM_RELEASE);
|
---|
281 | pMapping = NULL;
|
---|
282 | }
|
---|
283 | mapMutex.leave();
|
---|
284 | return TRUE;
|
---|
285 | fail:
|
---|
286 | mapMutex.leave();
|
---|
287 | return FALSE;
|
---|
288 | }
|
---|
289 | //******************************************************************************
|
---|
290 | //******************************************************************************
|
---|
291 | LPVOID Win32MemMap::mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess)
|
---|
292 | {
|
---|
293 | DWORD processId = GetCurrentProcess();
|
---|
294 |
|
---|
295 | mapMutex.enter();
|
---|
296 | ULONG memFlags = (mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY));
|
---|
297 | ULONG fAlloc = 0;
|
---|
298 | Win32MemMapView *mapview;
|
---|
299 |
|
---|
300 | //@@@PH: if(fdwAccess & ~(FILE_MAP_WRITE|FILE_MAP_READ|FILE_MAP_COPY))
|
---|
301 | // Docs say FILE_MAP_ALL_ACCESS is same as FILE_MAP_WRITE. Doesn't match reality though.
|
---|
302 | if(fdwAccess & ~FILE_MAP_ALL_ACCESS)
|
---|
303 | goto parmfail;
|
---|
304 | if((fdwAccess & FILE_MAP_WRITE) && !(mProtFlags & PAGE_READWRITE))
|
---|
305 | goto parmfail;
|
---|
306 | if((fdwAccess & FILE_MAP_READ) && !(mProtFlags & (PAGE_READWRITE|PAGE_READONLY)))
|
---|
307 | goto parmfail;
|
---|
308 |
|
---|
309 | //@@@PH
|
---|
310 | if (fdwAccess != FILE_MAP_ALL_ACCESS)
|
---|
311 | if((fdwAccess & FILE_MAP_COPY) && !(mProtFlags & PAGE_WRITECOPY))
|
---|
312 | goto parmfail;
|
---|
313 |
|
---|
314 | if(offset+size > mSize && (!(fdwAccess & FILE_MAP_WRITE) || !hMemFile))
|
---|
315 | goto parmfail;
|
---|
316 |
|
---|
317 | //SvL: TODO: Doesn't work for multiple views
|
---|
318 | if(offset+size > mSize) {
|
---|
319 | mSize = offset+size;
|
---|
320 | }
|
---|
321 |
|
---|
322 | //TODO: If committed, read file into memory
|
---|
323 | #if 0
|
---|
324 | if(mProtFlags & SEC_COMMIT)
|
---|
325 | fAlloc |= MEM_COMMIT;
|
---|
326 | else
|
---|
327 | if(mProtFlags & SEC_RESERVE)
|
---|
328 | fAlloc |= MEM_RESERVE;
|
---|
329 | #else
|
---|
330 | fAlloc = MEM_RESERVE;
|
---|
331 | #endif
|
---|
332 |
|
---|
333 | //Memory has already been allocated for executable image maps (only used internally)
|
---|
334 | if(!pMapping && nrMappings == 0) {//if not mapped, reserve/commit entire view
|
---|
335 | //SvL: Always read/write access or else ReadFile will crash once we
|
---|
336 | // start committing pages.
|
---|
337 | // This is most likely an OS/2 bug and doesn't happen in Aurora
|
---|
338 | // when allocating memory with the PAG_ANY bit set. (without this
|
---|
339 | // flag it will also crash)
|
---|
340 | if(!hMemFile && lpszMapName) {
|
---|
341 | pMapping = VirtualAllocShared(mSize, fAlloc, PAGE_READWRITE, lpszMapName);
|
---|
342 | }
|
---|
343 | else {
|
---|
344 | pMapping = VirtualAlloc(0, mSize, fAlloc, PAGE_READWRITE);
|
---|
345 | }
|
---|
346 | if(pMapping == NULL) {
|
---|
347 | dprintf(("Win32MemMap::mapFileView: VirtualAlloc %x %x %x failed!", mSize, fAlloc, memFlags));
|
---|
348 | goto fail;
|
---|
349 | }
|
---|
350 | //Windows NT seems to commit memory for memory maps, regardsless of the SEC_COMMIT flag
|
---|
351 | if((hMemFile == -1 && !image)) {//commit memory
|
---|
352 | VirtualAlloc(pMapping, mSize, MEM_COMMIT, PAGE_READWRITE);
|
---|
353 | }
|
---|
354 | if(hMemFile && (mProtFlags & SEC_COMMIT)) {
|
---|
355 | DWORD nrPages = mSize >> PAGE_SHIFT;
|
---|
356 | if(mSize & 0xFFF)
|
---|
357 | nrPages++;
|
---|
358 |
|
---|
359 | commitPage(0, FALSE, nrPages);
|
---|
360 | }
|
---|
361 | }
|
---|
362 | mapview = new Win32MemMapView(this, offset, (size == 0) ? mSize : size, fdwAccess);
|
---|
363 | if(mapview == NULL) {
|
---|
364 | goto fail;
|
---|
365 | }
|
---|
366 | if(mapview->everythingOk() == FALSE) {
|
---|
367 | dprintf(("Win32MemMap::mapFileView: !mapview->everythingOk"));
|
---|
368 | delete mapview;
|
---|
369 | goto fail;
|
---|
370 | }
|
---|
371 | nrMappings++;
|
---|
372 | mapMutex.leave();
|
---|
373 | return mapview->getViewAddr();
|
---|
374 |
|
---|
375 | parmfail:
|
---|
376 | dprintf(("Win32MemMap::mapFileView: ERROR_INVALID_PARAMETER"));
|
---|
377 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
378 | fail:
|
---|
379 | mapMutex.leave();
|
---|
380 | return 0;
|
---|
381 | }
|
---|
382 | //******************************************************************************
|
---|
383 | //We determine whether a page has been modified by checking it's protection flags
|
---|
384 | //If the write flag is set, this means commitPage had to enable this due to a pagefault
|
---|
385 | //(all pages are readonly until the app tries to modify the contents of the page)
|
---|
386 | //
|
---|
387 | //TODO: Are apps allowed to change the protection flags of memory mapped pages?
|
---|
388 | // I'm assuming they aren't for now.
|
---|
389 | //******************************************************************************
|
---|
390 | BOOL Win32MemMap::flushView(ULONG offset, ULONG cbFlush)
|
---|
391 | {
|
---|
392 | LPVOID lpvBase = (LPVOID)((ULONG)pMapping+offset);
|
---|
393 | MEMORY_BASIC_INFORMATION memInfo;
|
---|
394 | ULONG nrBytesWritten, size;
|
---|
395 | int i;
|
---|
396 |
|
---|
397 | if(image) //no flushing for image maps
|
---|
398 | return TRUE;
|
---|
399 |
|
---|
400 | dprintf(("Win32MemMap::flushView: %x %x", lpvBase, cbFlush));
|
---|
401 | if(nrMappings == 0)
|
---|
402 | goto parmfail;
|
---|
403 |
|
---|
404 | if(cbFlush == 0)
|
---|
405 | cbFlush = mSize;
|
---|
406 |
|
---|
407 | if(lpvBase < pMapping || (ULONG)lpvBase+cbFlush > (ULONG)pMapping+mSize)
|
---|
408 | goto parmfail;
|
---|
409 |
|
---|
410 | if(mProtFlags & PAGE_READONLY)
|
---|
411 | goto parmfail;
|
---|
412 |
|
---|
413 | if(hMemFile == -1)
|
---|
414 | goto success; //TODO: Return an error here?
|
---|
415 |
|
---|
416 | while(cbFlush) {
|
---|
417 | if(VirtualQuery((LPSTR)lpvBase, &memInfo, cbFlush) == 0) {
|
---|
418 | dprintf(("Win32MemMap::flushView: VirtualQuery (%x,%x) failed for %x", lpvBase, cbFlush, (ULONG)lpvBase+i*PAGE_SIZE));
|
---|
419 | goto fail;
|
---|
420 | }
|
---|
421 | //If a page (or range of pages) is reserved or write protected, we
|
---|
422 | //won't bother flushing it to disk
|
---|
423 | if(memInfo.State & MEM_COMMIT &&
|
---|
424 | memInfo.AllocationProtect & (PAGE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY))
|
---|
425 | {//committed and allowed for writing?
|
---|
426 | offset = (ULONG)lpvBase - (ULONG)pMapping;
|
---|
427 | size = memInfo.RegionSize;
|
---|
428 | if(size > cbFlush) {
|
---|
429 | size = cbFlush;
|
---|
430 | }
|
---|
431 | dprintf(("Win32MemMap::flushView for offset %x, size %d", offset, size));
|
---|
432 |
|
---|
433 | if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
|
---|
434 | dprintf(("Win32MemMap::flushView: SetFilePointer failed to set pos to %x", offset));
|
---|
435 | goto fail;
|
---|
436 | }
|
---|
437 | if(WriteFile(hMemFile, (LPSTR)lpvBase, size, &nrBytesWritten, NULL) == FALSE) {
|
---|
438 | dprintf(("Win32MemMap::flushView: WriteFile failed for %x", (ULONG)lpvBase));
|
---|
439 | goto fail;
|
---|
440 | }
|
---|
441 | if(nrBytesWritten != size) {
|
---|
442 | dprintf(("Win32MemMap::flushView: WriteFile didn't write all bytes for %x", (ULONG)lpvBase));
|
---|
443 | goto fail;
|
---|
444 | }
|
---|
445 | }
|
---|
446 | lpvBase = (LPVOID)((ULONG)lpvBase + memInfo.RegionSize);
|
---|
447 |
|
---|
448 | if(cbFlush < memInfo.RegionSize)
|
---|
449 | break;
|
---|
450 |
|
---|
451 | cbFlush -= memInfo.RegionSize;
|
---|
452 | }
|
---|
453 | success:
|
---|
454 | return TRUE;
|
---|
455 | parmfail:
|
---|
456 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
457 | return FALSE;
|
---|
458 | fail:
|
---|
459 | return FALSE;
|
---|
460 | }
|
---|
461 | //******************************************************************************
|
---|
462 | //******************************************************************************
|
---|
463 | Win32MemMap *Win32MemMap::findMap(LPSTR lpszName)
|
---|
464 | {
|
---|
465 | if(lpszName == NULL)
|
---|
466 | return NULL;
|
---|
467 |
|
---|
468 | globalmapMutex.enter();
|
---|
469 | Win32MemMap *map = memmaps;
|
---|
470 |
|
---|
471 | if(map != NULL) {
|
---|
472 | while(map) {
|
---|
473 | if(map->lpszMapName && !strcmp(map->lpszMapName, lpszName))
|
---|
474 | break;
|
---|
475 | map = map->next;
|
---|
476 | }
|
---|
477 | }
|
---|
478 | globalmapMutex.leave();
|
---|
479 | if(!map) dprintf(("Win32MemMap::findMap: couldn't find map %s", lpszName));
|
---|
480 | return map;
|
---|
481 | }
|
---|
482 | //******************************************************************************
|
---|
483 | //******************************************************************************
|
---|
484 | Win32MemMap *Win32MemMap::findMap(ULONG address)
|
---|
485 | {
|
---|
486 | globalmapMutex.enter();
|
---|
487 | Win32MemMap *map = memmaps;
|
---|
488 |
|
---|
489 | if(map != NULL) {
|
---|
490 | while(map) {
|
---|
491 | if(map->pMapping && (ULONG)map->pMapping <= address &&
|
---|
492 | (ULONG)map->pMapping + map->mSize > address)
|
---|
493 | {
|
---|
494 | break;
|
---|
495 | }
|
---|
496 | map = map->next;
|
---|
497 | }
|
---|
498 | }
|
---|
499 | globalmapMutex.leave();
|
---|
500 | return map;
|
---|
501 | }
|
---|
502 | //******************************************************************************
|
---|
503 | //Assumes mutex has been acquired
|
---|
504 | //******************************************************************************
|
---|
505 | void Win32MemMap::deleteAll()
|
---|
506 | {
|
---|
507 | Win32MemMap *map = memmaps, *nextmap;
|
---|
508 | DWORD processId = GetCurrentProcess();
|
---|
509 |
|
---|
510 | //delete all maps created by this process
|
---|
511 | while(map) {
|
---|
512 | nextmap = map->next;
|
---|
513 | if(map->getProcessId() == processId) {
|
---|
514 | //Delete map directly for executable images (only used internally)
|
---|
515 | if(map->getImage()) {
|
---|
516 | delete map;
|
---|
517 | }
|
---|
518 | else CloseHandle(memmaps->hMemMap);
|
---|
519 | }
|
---|
520 | else {
|
---|
521 | //delete all views created by this process for this map
|
---|
522 | Win32MemMapView::deleteViews(map);
|
---|
523 | }
|
---|
524 | map = nextmap;
|
---|
525 | }
|
---|
526 | }
|
---|
527 | //******************************************************************************
|
---|
528 | //******************************************************************************
|
---|
529 | Win32MemMapView::Win32MemMapView(Win32MemMap *map, ULONG offset, ULONG size,
|
---|
530 | ULONG fdwAccess)
|
---|
531 | {
|
---|
532 | LPVOID viewaddr = (LPVOID)((ULONG)map->getMappingAddr()+offset);
|
---|
533 | ULONG accessAttr = 0;
|
---|
534 | Win32MemMapView *tmpview = mapviews;
|
---|
535 |
|
---|
536 | errorState = 0;
|
---|
537 | mParentMap = map;
|
---|
538 | mSize = size;
|
---|
539 | mOffset = offset;
|
---|
540 | mProcessId = GetCurrentProcess();
|
---|
541 |
|
---|
542 | switch(fdwAccess) {
|
---|
543 | case FILE_MAP_READ:
|
---|
544 | accessAttr = PAG_READ;
|
---|
545 | mfAccess = MEMMAP_ACCESS_READ;
|
---|
546 | break;
|
---|
547 | case FILE_MAP_ALL_ACCESS:
|
---|
548 | case FILE_MAP_WRITE:
|
---|
549 | case FILE_MAP_WRITE|FILE_MAP_READ:
|
---|
550 | case FILE_MAP_COPY:
|
---|
551 | accessAttr = (PAG_READ|PAG_WRITE);
|
---|
552 | mfAccess = MEMMAP_ACCESS_READ | MEMMAP_ACCESS_WRITE;
|
---|
553 | break;
|
---|
554 | }
|
---|
555 | if(map->getMemName() != NULL && !map->getFileHandle()) {
|
---|
556 | //shared memory map, so map it into our address space
|
---|
557 | if(OSLibDosGetNamedSharedMem((LPVOID *)&viewaddr, map->getMemName()) != OSLIB_NOERROR) {
|
---|
558 | dprintf(("new OSLibDosGetNamedSharedMem FAILED"));
|
---|
559 | SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
---|
560 | errorState = 1;
|
---|
561 | return;
|
---|
562 | }
|
---|
563 | }
|
---|
564 |
|
---|
565 | //view == memory mapping for executable images (only used internally)
|
---|
566 | if(map->getImage()) {
|
---|
567 | pMapView = map->getMappingAddr();
|
---|
568 | }
|
---|
569 | else {
|
---|
570 | if(OSLibDosAliasMem(viewaddr, size, &pMapView, accessAttr) != OSLIB_NOERROR) {
|
---|
571 | dprintf(("new OSLibDosAliasMem FAILED"));
|
---|
572 | SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
---|
573 | errorState = 1;
|
---|
574 | return;
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 | dprintf(("Win32MemMapView::Win32MemMapView: created %x (alias for %x), size %d", pMapView, viewaddr, size));
|
---|
579 |
|
---|
580 | globalviewMutex.enter();
|
---|
581 | if(tmpview == NULL || tmpview->getViewAddr() > pMapView) {
|
---|
582 | next = mapviews;
|
---|
583 | mapviews = this;
|
---|
584 | }
|
---|
585 | else {
|
---|
586 | while(tmpview->next) {
|
---|
587 | if(tmpview->next->getViewAddr() > pMapView) {
|
---|
588 | break;
|
---|
589 | }
|
---|
590 | tmpview = tmpview->next;
|
---|
591 | }
|
---|
592 | next = tmpview->next;
|
---|
593 | tmpview->next = this;
|
---|
594 | }
|
---|
595 | globalviewMutex.leave();
|
---|
596 | }
|
---|
597 | //******************************************************************************
|
---|
598 | //******************************************************************************
|
---|
599 | Win32MemMapView::~Win32MemMapView()
|
---|
600 | {
|
---|
601 | if(errorState != 0)
|
---|
602 | return;
|
---|
603 |
|
---|
604 | dprintf(("Win32MemMapView dtor: deleting view %x %x", mOffset, mSize));
|
---|
605 |
|
---|
606 | if(mfAccess & MEMMAP_ACCESS_WRITE)
|
---|
607 | mParentMap->flushView(mOffset, mSize);
|
---|
608 |
|
---|
609 | //Don't free memory for executable image map views (only used internally)
|
---|
610 | if(!mParentMap->getImage())
|
---|
611 | OSLibDosFreeMem(pMapView);
|
---|
612 |
|
---|
613 | globalviewMutex.enter();
|
---|
614 | Win32MemMapView *view = mapviews;
|
---|
615 |
|
---|
616 | if(view == this) {
|
---|
617 | mapviews = next;
|
---|
618 | }
|
---|
619 | else {
|
---|
620 | while(view->next) {
|
---|
621 | if(view->next == this)
|
---|
622 | break;
|
---|
623 | view = view->next;
|
---|
624 | }
|
---|
625 | if(view->next) {
|
---|
626 | view->next = next;
|
---|
627 | }
|
---|
628 | else dprintf(("Win32MemMapView::~Win32MemMapView: map not found!! (%x)", this));
|
---|
629 | }
|
---|
630 | globalviewMutex.leave();
|
---|
631 | }
|
---|
632 | //******************************************************************************
|
---|
633 | //******************************************************************************
|
---|
634 | void Win32MemMapView::deleteViews(Win32MemMap *map)
|
---|
635 | {
|
---|
636 | globalviewMutex.enter();
|
---|
637 | Win32MemMapView *view = mapviews, *nextview;
|
---|
638 |
|
---|
639 | if(view != NULL) {
|
---|
640 | while(view) {
|
---|
641 | nextview = view->next;
|
---|
642 | if(view->getParentMap() == map)
|
---|
643 | {
|
---|
644 | globalviewMutex.leave();
|
---|
645 | delete view;
|
---|
646 | globalviewMutex.enter();
|
---|
647 | }
|
---|
648 | view = nextview;
|
---|
649 | }
|
---|
650 | }
|
---|
651 | globalviewMutex.leave();
|
---|
652 | }
|
---|
653 | //******************************************************************************
|
---|
654 | //******************************************************************************
|
---|
655 | Win32MemMap *Win32MemMapView::findMapByView(ULONG address, ULONG *offset,
|
---|
656 | ULONG accessType,
|
---|
657 | Win32MemMapView **pView)
|
---|
658 | {
|
---|
659 | globalviewMutex.enter();
|
---|
660 | Win32MemMapView *view = mapviews;
|
---|
661 |
|
---|
662 | *offset = 0;
|
---|
663 |
|
---|
664 | if(view != NULL) {
|
---|
665 | while(view && (ULONG)view->getViewAddr() <= address) {
|
---|
666 | if((ULONG)view->getViewAddr() <= address &&
|
---|
667 | (ULONG)view->getViewAddr() + view->getSize() > address &&
|
---|
668 | view->getAccessFlags() >= accessType)
|
---|
669 | {
|
---|
670 | *offset = view->getOffset() + (address - (ULONG)view->getViewAddr());
|
---|
671 | goto success;
|
---|
672 | }
|
---|
673 | view = view->next;
|
---|
674 | }
|
---|
675 | //failure if we get here
|
---|
676 | view = NULL;
|
---|
677 | }
|
---|
678 | success:
|
---|
679 | if(view && !view->getParentMap()->isImageMap())
|
---|
680 | dprintf(("findMapByView %x %x -> %x off %x", address, accessType, view->getViewAddr(), *offset));
|
---|
681 |
|
---|
682 | globalviewMutex.leave();
|
---|
683 | if(pView) *pView = view;
|
---|
684 | return (view) ? view->getParentMap() : NULL;
|
---|
685 | }
|
---|
686 | //******************************************************************************
|
---|
687 | //******************************************************************************
|
---|
688 | Win32MemMapView *Win32MemMapView::findView(LPVOID address)
|
---|
689 | {
|
---|
690 | Win32MemMapView *view = mapviews;
|
---|
691 |
|
---|
692 | if(view != NULL) {
|
---|
693 | while(view) {
|
---|
694 | if(view->getViewAddr() == address)
|
---|
695 | {
|
---|
696 | break;
|
---|
697 | }
|
---|
698 | view = view->next;
|
---|
699 | }
|
---|
700 | }
|
---|
701 | return view;
|
---|
702 | }
|
---|
703 | //******************************************************************************
|
---|
704 | //******************************************************************************
|
---|
705 |
|
---|