1 | /* $Id: mmapview.cpp,v 1.3 2003-12-12 11:18:04 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 Memory mapped file & view classes
|
---|
5 | *
|
---|
6 | * Copyright 1999-2003 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 | * TODO: Suspend all threads when a page is committed (possible that another thread
|
---|
20 | * accesses the same memory before the page is read from disk
|
---|
21 | * TODO: File maps for new files (must select an initial size)!
|
---|
22 | *
|
---|
23 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
24 | *
|
---|
25 | */
|
---|
26 | #include <os2win.h>
|
---|
27 | #include <stdio.h>
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include <string.h>
|
---|
30 | #include <win\virtual.h>
|
---|
31 | #include <odincrt.h>
|
---|
32 | #include <handlemanager.h>
|
---|
33 | #include "mmap.h"
|
---|
34 | #include "oslibdos.h"
|
---|
35 | #include "oslibmem.h"
|
---|
36 | #include <winimagepeldr.h>
|
---|
37 | #include <custombuild.h>
|
---|
38 |
|
---|
39 | #define DBG_LOCALLOG DBG_mmapview
|
---|
40 | #include "dbglocal.h"
|
---|
41 |
|
---|
42 | Win32MemMapView *Win32MemMapView::mapviews = NULL;
|
---|
43 |
|
---|
44 | //******************************************************************************
|
---|
45 | // Class Win32MemMapView
|
---|
46 | //
|
---|
47 | // Memory map view
|
---|
48 | //
|
---|
49 | // View parent = memory map that contains the original memory map
|
---|
50 | // View owner = duplicate memory map that created this view (can be NULL)
|
---|
51 | //
|
---|
52 | //******************************************************************************
|
---|
53 | Win32MemMapView::Win32MemMapView(Win32MemMap *map, ULONG offset, ULONG size,
|
---|
54 | ULONG fdwAccess, Win32MemMap *owner)
|
---|
55 | {
|
---|
56 | LPVOID viewaddr = (LPVOID)((ULONG)map->getMappingAddr()+offset);
|
---|
57 | ULONG accessAttr = 0;
|
---|
58 | Win32MemMapView *tmpview = mapviews;
|
---|
59 |
|
---|
60 | errorState = 0;
|
---|
61 | mParentMap = map;
|
---|
62 | mOwnerMap = NULL;
|
---|
63 | pCOWBitmap = NULL;
|
---|
64 | mSize = size;
|
---|
65 | mOffset = offset;
|
---|
66 | mProcessId = GetCurrentProcessId();
|
---|
67 | pShareViewAddr = NULL;
|
---|
68 |
|
---|
69 | switch(fdwAccess) {
|
---|
70 | case FILE_MAP_READ:
|
---|
71 | accessAttr = PAG_READ;
|
---|
72 | mfAccess = MEMMAP_ACCESS_READ;
|
---|
73 | break;
|
---|
74 | case FILE_MAP_ALL_ACCESS:
|
---|
75 | case FILE_MAP_WRITE:
|
---|
76 | case FILE_MAP_WRITE|FILE_MAP_READ:
|
---|
77 | accessAttr = (PAG_READ|PAG_WRITE);
|
---|
78 | mfAccess = MEMMAP_ACCESS_READ | MEMMAP_ACCESS_WRITE;
|
---|
79 | break;
|
---|
80 | case FILE_MAP_COPY:
|
---|
81 | accessAttr = (PAG_READ|PAG_WRITE);
|
---|
82 | mfAccess = MEMMAP_ACCESS_READ | MEMMAP_ACCESS_WRITE | MEMMAP_ACCESS_COPYONWRITE;
|
---|
83 | break;
|
---|
84 | }
|
---|
85 | //Named file mappings from other processes are always shared;
|
---|
86 | //map into our address space
|
---|
87 | if(map->getMemName() != NULL && map->getProcessId() != mProcessId)
|
---|
88 | {
|
---|
89 | //shared memory map, so map it into our address space
|
---|
90 | if(OSLibDosGetNamedSharedMem((LPVOID *)&viewaddr, map->getMemName()) != OSLIB_NOERROR)
|
---|
91 | {
|
---|
92 | dprintf(("new OSLibDosGetNamedSharedMem FAILED"));
|
---|
93 | SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
---|
94 | errorState = 1;
|
---|
95 | return;
|
---|
96 | }
|
---|
97 | pShareViewAddr = viewaddr;
|
---|
98 | viewaddr = (LPVOID)((char *)viewaddr + mOffset);
|
---|
99 | }
|
---|
100 |
|
---|
101 | //view == memory mapping for executable images (only used internally)
|
---|
102 | if(map->getImage()) {
|
---|
103 | pMapView = map->getMappingAddr();
|
---|
104 | pMapView = (LPVOID)((char *)pMapView + mOffset);
|
---|
105 | }
|
---|
106 | else {
|
---|
107 | if(mfAccess & MEMMAP_ACCESS_COPYONWRITE)
|
---|
108 | {
|
---|
109 | SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
---|
110 | errorState = 1;
|
---|
111 | return;
|
---|
112 | }
|
---|
113 | else
|
---|
114 | if(OSLibDosAliasMem(viewaddr, size, &pMapView, accessAttr) != OSLIB_NOERROR) {
|
---|
115 | dprintf(("new OSLibDosAliasMem FAILED"));
|
---|
116 | SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
---|
117 | errorState = 1;
|
---|
118 | return;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | dprintf(("Win32MemMapView::Win32MemMapView: created %x (alias for %x), size %d", pMapView, viewaddr, size));
|
---|
123 | mParentMap->AddRef();
|
---|
124 | mParentMap->AddView();
|
---|
125 |
|
---|
126 | DosEnterCriticalSection(&globalmapcritsect);
|
---|
127 | if(tmpview == NULL || tmpview->getViewAddr() > pMapView) {
|
---|
128 | next = mapviews;
|
---|
129 | mapviews = this;
|
---|
130 | }
|
---|
131 | else {
|
---|
132 | while(tmpview->next) {
|
---|
133 | if(tmpview->next->getViewAddr() > pMapView) {
|
---|
134 | break;
|
---|
135 | }
|
---|
136 | tmpview = tmpview->next;
|
---|
137 | }
|
---|
138 | next = tmpview->next;
|
---|
139 | tmpview->next = this;
|
---|
140 | }
|
---|
141 | DosLeaveCriticalSection(&globalmapcritsect);
|
---|
142 |
|
---|
143 | if(owner) {
|
---|
144 | mOwnerMap = owner;
|
---|
145 | mOwnerMap->AddRef();
|
---|
146 | mOwnerMap->AddView();
|
---|
147 | }
|
---|
148 | }
|
---|
149 | //******************************************************************************
|
---|
150 | //******************************************************************************
|
---|
151 | Win32MemMapView::~Win32MemMapView()
|
---|
152 | {
|
---|
153 | if(errorState != 0)
|
---|
154 | return;
|
---|
155 |
|
---|
156 | dprintf(("Win32MemMapView dtor: deleting view %x %x", mOffset, mSize));
|
---|
157 |
|
---|
158 | if(mfAccess & MEMMAP_ACCESS_WRITE)
|
---|
159 | mParentMap->flushView(MMAP_FLUSHVIEW_ALL, mOffset, mSize);
|
---|
160 |
|
---|
161 | //Don't free memory for executable image map views (only used internally)
|
---|
162 | if(!mParentMap->getImage())
|
---|
163 | OSLibDosFreeMem(pMapView);
|
---|
164 |
|
---|
165 | if(pShareViewAddr) {
|
---|
166 | OSLibDosFreeMem(pShareViewAddr);
|
---|
167 | }
|
---|
168 | if(pCOWBitmap) free(pCOWBitmap);
|
---|
169 |
|
---|
170 | DosEnterCriticalSection(&globalmapcritsect);
|
---|
171 | Win32MemMapView *view = mapviews;
|
---|
172 |
|
---|
173 | if(view == this) {
|
---|
174 | mapviews = next;
|
---|
175 | }
|
---|
176 | else {
|
---|
177 | while(view->next) {
|
---|
178 | if(view->next == this)
|
---|
179 | break;
|
---|
180 | view = view->next;
|
---|
181 | }
|
---|
182 | if(view->next) {
|
---|
183 | view->next = next;
|
---|
184 | }
|
---|
185 | else dprintf(("Win32MemMapView::~Win32MemMapView: map not found!! (%x)", this));
|
---|
186 | }
|
---|
187 | DosLeaveCriticalSection(&globalmapcritsect);
|
---|
188 |
|
---|
189 | mParentMap->RemoveView();
|
---|
190 | mParentMap->Release();
|
---|
191 | if(mOwnerMap) {
|
---|
192 | mOwnerMap->RemoveView();
|
---|
193 | mOwnerMap->Release();
|
---|
194 | }
|
---|
195 | }
|
---|
196 | //******************************************************************************
|
---|
197 | // Win32MemMapView::markCOWPages
|
---|
198 | //
|
---|
199 | // Mark pages as private in the COW page bitmap
|
---|
200 | //
|
---|
201 | // Parameters:
|
---|
202 | //
|
---|
203 | // int startpage - start page
|
---|
204 | // int nrpages - number of pages
|
---|
205 | //
|
---|
206 | //
|
---|
207 | //******************************************************************************
|
---|
208 | void Win32MemMapView::markCOWPages(int startpage, int nrpages)
|
---|
209 | {
|
---|
210 | int viewpagestart, nrviewpages;
|
---|
211 |
|
---|
212 | if(pCOWBitmap == NULL) {
|
---|
213 | //not a COW view; ignore
|
---|
214 | return;
|
---|
215 | }
|
---|
216 | //check if this page is part of our view
|
---|
217 | viewpagestart = mOffset >> PAGE_SHIFT;
|
---|
218 | nrviewpages = mSize >> PAGE_SHIFT;
|
---|
219 | if(mSize & 0xFFF)
|
---|
220 | nrviewpages++;
|
---|
221 |
|
---|
222 | if(startpage < viewpagestart || startpage >= viewpagestart+nrviewpages) {
|
---|
223 | return; //outside this view
|
---|
224 | }
|
---|
225 | if(startpage + nrpages > viewpagestart + nrviewpages) {
|
---|
226 | nrpages -= ((startpage + nrpages) - (viewpagestart + nrviewpages));
|
---|
227 | }
|
---|
228 |
|
---|
229 | for(int i=startpage;i<startpage+nrpages;i++) {
|
---|
230 | set_bit(i, pCOWBitmap);
|
---|
231 | }
|
---|
232 | }
|
---|
233 | //******************************************************************************
|
---|
234 | // Win32MemMapView::changePageFlags
|
---|
235 | //
|
---|
236 | // Change the protection flags of our alias. Called when a range of pages has
|
---|
237 | // been committed.
|
---|
238 | //
|
---|
239 | // Parameters:
|
---|
240 | //
|
---|
241 | // ULONG offset - offset in memory map (page aligned!)
|
---|
242 | // ULONG size - size of committed page range
|
---|
243 | // PAGEVIEW flags - page flags
|
---|
244 | // PAGEVIEW_READONLY -> set page flags to readonly
|
---|
245 | // PAGEVIEW_VIEW -> set page flags to view default
|
---|
246 | //
|
---|
247 | // Returns:
|
---|
248 | // TRUE - success
|
---|
249 | // FALSE - failure
|
---|
250 | //
|
---|
251 | //******************************************************************************
|
---|
252 | BOOL Win32MemMapView::changePageFlags(ULONG offset, ULONG size, PAGEVIEW flags)
|
---|
253 | {
|
---|
254 | ULONG accessAttr = 0, rc;
|
---|
255 |
|
---|
256 | //offset must be page aligned
|
---|
257 | if(offset & 0xFFF) {
|
---|
258 | DebugInt3();
|
---|
259 | return FALSE;
|
---|
260 | }
|
---|
261 |
|
---|
262 | if( ( (mfAccess & MEMMAP_ACCESS_COPYONWRITE) && (flags != PAGEVIEW_GUARD) ) ||
|
---|
263 | ( (flags == PAGEVIEW_GUARD) && !(mfAccess & MEMMAP_ACCESS_COPYONWRITE) ) )
|
---|
264 | {
|
---|
265 | //PAGEVIEW_VIEW/READONLY does not apply to COW views
|
---|
266 | return TRUE;
|
---|
267 | }
|
---|
268 | if(mOffset + mSize <= offset || mOffset >= offset + size) {
|
---|
269 | return TRUE; //not part of this view
|
---|
270 | }
|
---|
271 | if(offset < mOffset) {
|
---|
272 | size -= mOffset - offset;
|
---|
273 | offset = mOffset;
|
---|
274 | }
|
---|
275 | if(mOffset + mSize < offset + size) {
|
---|
276 | size -= ((offset + size) - (mOffset + mSize));
|
---|
277 | }
|
---|
278 |
|
---|
279 | if(flags == PAGEVIEW_READONLY) {
|
---|
280 | accessAttr = PAG_READ;
|
---|
281 | }
|
---|
282 | else
|
---|
283 | {//use view attributes
|
---|
284 | if(mfAccess & MEMMAP_ACCESS_READ) {
|
---|
285 | accessAttr |= PAG_READ;
|
---|
286 | }
|
---|
287 | if(mfAccess & MEMMAP_ACCESS_WRITE) {
|
---|
288 | accessAttr |= PAG_WRITE;
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | {
|
---|
293 | rc = OSLibDosSetMem((char *)pMapView+(offset - mOffset), size, accessAttr);
|
---|
294 | if(rc) {
|
---|
295 | dprintf(("Win32MemMapView::changePageFlags: OSLibDosSetMem %x %x %x failed with %d", (char *)pMapView+(offset - mOffset), size, accessAttr, rc));
|
---|
296 | return FALSE;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | return TRUE;
|
---|
300 | }
|
---|
301 | //******************************************************************************
|
---|
302 | //******************************************************************************
|
---|
303 | int Win32MemMapView::findViews(Win32MemMap *map, int nrViews,
|
---|
304 | Win32MemMapView *viewarray[])
|
---|
305 | {
|
---|
306 | int i=0;
|
---|
307 |
|
---|
308 | DosEnterCriticalSection(&globalmapcritsect);
|
---|
309 | Win32MemMapView *view = mapviews, *nextview;
|
---|
310 |
|
---|
311 | if(view != NULL)
|
---|
312 | {
|
---|
313 | while(view && i < nrViews)
|
---|
314 | {
|
---|
315 | if(view->getParentMap() == map)
|
---|
316 | {
|
---|
317 | viewarray[i] = view;
|
---|
318 | i++;
|
---|
319 | }
|
---|
320 | view = view->next;
|
---|
321 | }
|
---|
322 | }
|
---|
323 | DosLeaveCriticalSection(&globalmapcritsect);
|
---|
324 | return i;
|
---|
325 | }
|
---|
326 | //******************************************************************************
|
---|
327 | //******************************************************************************
|
---|
328 | void Win32MemMapView::deleteViews(Win32MemMap *map)
|
---|
329 | {
|
---|
330 | DosEnterCriticalSection(&globalmapcritsect);
|
---|
331 | Win32MemMapView *view = mapviews, *nextview;
|
---|
332 |
|
---|
333 | if(view != NULL)
|
---|
334 | {
|
---|
335 | while(view)
|
---|
336 | {
|
---|
337 | nextview = view->next;
|
---|
338 | if(view->getParentMap() == map)
|
---|
339 | {
|
---|
340 | DosLeaveCriticalSection(&globalmapcritsect);
|
---|
341 | delete view;
|
---|
342 | DosEnterCriticalSection(&globalmapcritsect);
|
---|
343 | }
|
---|
344 | view = nextview;
|
---|
345 | }
|
---|
346 | }
|
---|
347 | DosLeaveCriticalSection(&globalmapcritsect);
|
---|
348 | }
|
---|
349 | //******************************************************************************
|
---|
350 | //******************************************************************************
|
---|
351 | // Win32MemMap::findMapByView
|
---|
352 | //
|
---|
353 | // Find the map of the view that contains the specified starting address
|
---|
354 | // and has the specified access type
|
---|
355 | //
|
---|
356 | // Parameters:
|
---|
357 | //
|
---|
358 | // ULONG address - view address
|
---|
359 | // ULONG *offset - address of ULONG that receives the offset
|
---|
360 | // in the returned memory map
|
---|
361 | // ULONG accessType - access type:
|
---|
362 | // MEMMAP_ACCESS_READ
|
---|
363 | // MEMMAP_ACCESS_WRITE
|
---|
364 | // MEMMAP_ACCESS_EXECUTE
|
---|
365 | //
|
---|
366 | // Returns:
|
---|
367 | // <> NULL - success, address of parent map object
|
---|
368 | // NULL - failure
|
---|
369 | //
|
---|
370 | //******************************************************************************
|
---|
371 | //******************************************************************************
|
---|
372 | Win32MemMap *Win32MemMapView::findMapByView(ULONG address,
|
---|
373 | ULONG *offset,
|
---|
374 | ULONG accessType)
|
---|
375 | {
|
---|
376 | Win32MemMap *map = NULL;
|
---|
377 | ULONG ulOffset;
|
---|
378 |
|
---|
379 | if(mapviews == NULL) return NULL;
|
---|
380 |
|
---|
381 | DosEnterCriticalSection(&globalmapcritsect);
|
---|
382 | Win32MemMapView *view = mapviews;
|
---|
383 | ULONG ulViewAddr;
|
---|
384 |
|
---|
385 | if(!offset) offset = &ulOffset;
|
---|
386 |
|
---|
387 | *offset = 0;
|
---|
388 |
|
---|
389 | if(view != NULL)
|
---|
390 | {
|
---|
391 | do
|
---|
392 | {
|
---|
393 | ulViewAddr = (ULONG)view->getViewAddr();
|
---|
394 |
|
---|
395 | // if ulViewAddr is > address, we've exhausted
|
---|
396 | // the sorted list already and can abort search.
|
---|
397 | if(ulViewAddr <= address)
|
---|
398 | {
|
---|
399 | if(ulViewAddr + view->getSize() > address &&
|
---|
400 | view->getAccessFlags() >= accessType)
|
---|
401 | {
|
---|
402 | *offset = view->getOffset() + (address - ulViewAddr);
|
---|
403 | goto success;
|
---|
404 | }
|
---|
405 |
|
---|
406 | // Not found yet, continue search with next map
|
---|
407 | view = view->next;
|
---|
408 | }
|
---|
409 | else
|
---|
410 | {
|
---|
411 | // list is exhausted, abort loop
|
---|
412 | view = NULL;
|
---|
413 | }
|
---|
414 | }
|
---|
415 | while(view);
|
---|
416 |
|
---|
417 | //failure if we get here
|
---|
418 | view = NULL;
|
---|
419 | }
|
---|
420 | success:
|
---|
421 | #ifdef DEBUG
|
---|
422 | if(view && !view->getParentMap()->isImageMap())
|
---|
423 | dprintf(("findMapByView %x %x -> %x off %x",
|
---|
424 | address,
|
---|
425 | accessType,
|
---|
426 | view->getViewAddr(),
|
---|
427 | *offset));
|
---|
428 | #endif
|
---|
429 |
|
---|
430 | if(view) {
|
---|
431 | //first look at the owner (duplicate map), then the real parent
|
---|
432 | map = view->getOwnerMap();
|
---|
433 | if(!map) map = view->getParentMap();
|
---|
434 |
|
---|
435 | if(map) map->AddRef();
|
---|
436 | }
|
---|
437 |
|
---|
438 | DosLeaveCriticalSection(&globalmapcritsect);
|
---|
439 |
|
---|
440 | return map;
|
---|
441 | }
|
---|
442 | //******************************************************************************
|
---|
443 | // Win32MemMap::findView
|
---|
444 | //
|
---|
445 | // Find the view that contains the specified starting address
|
---|
446 | //
|
---|
447 | // Parameters:
|
---|
448 | //
|
---|
449 | // LPVOID address - view address
|
---|
450 | //
|
---|
451 | // Returns:
|
---|
452 | // <> NULL - success, address view object
|
---|
453 | // NULL - failure
|
---|
454 | //
|
---|
455 | //******************************************************************************
|
---|
456 | Win32MemMapView *Win32MemMapView::findView(ULONG address)
|
---|
457 | {
|
---|
458 | ULONG ulViewAddr;
|
---|
459 |
|
---|
460 | DosEnterCriticalSection(&globalmapcritsect);
|
---|
461 | Win32MemMapView *view = mapviews;
|
---|
462 |
|
---|
463 | if(view != NULL) {
|
---|
464 | while(view) {
|
---|
465 | ulViewAddr = (ULONG)view->getViewAddr();
|
---|
466 | if(ulViewAddr <= address && ulViewAddr + view->getSize() > address)
|
---|
467 | {
|
---|
468 | break;
|
---|
469 | }
|
---|
470 | view = view->next;
|
---|
471 | }
|
---|
472 | }
|
---|
473 | DosLeaveCriticalSection(&globalmapcritsect);
|
---|
474 | return view;
|
---|
475 | }
|
---|
476 | //******************************************************************************
|
---|
477 | //******************************************************************************
|
---|