source: trunk/src/kernel32/mmapview.cpp@ 9946

Last change on this file since 9946 was 9946, checked in by sandervl, 22 years ago

memory map updates

File size: 14.1 KB
Line 
1/* $Id: mmapview.cpp,v 1.1 2003-03-27 14:13:11 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
42Win32MemMapView *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//******************************************************************************
53Win32MemMapView::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 }
99
100 //view == memory mapping for executable images (only used internally)
101 if(map->getImage()) {
102 pMapView = map->getMappingAddr();
103 }
104 else {
105 if(mfAccess & MEMMAP_ACCESS_COPYONWRITE)
106 {
107 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
108 errorState = 1;
109 return;
110 }
111 else
112 if(OSLibDosAliasMem(viewaddr, size, &pMapView, accessAttr) != OSLIB_NOERROR) {
113 dprintf(("new OSLibDosAliasMem FAILED"));
114 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
115 errorState = 1;
116 return;
117 }
118 }
119
120 dprintf(("Win32MemMapView::Win32MemMapView: created %x (alias for %x), size %d", pMapView, viewaddr, size));
121 mParentMap->AddRef();
122 mParentMap->AddView();
123
124 DosEnterCriticalSection(&globalmapcritsect);
125 if(tmpview == NULL || tmpview->getViewAddr() > pMapView) {
126 next = mapviews;
127 mapviews = this;
128 }
129 else {
130 while(tmpview->next) {
131 if(tmpview->next->getViewAddr() > pMapView) {
132 break;
133 }
134 tmpview = tmpview->next;
135 }
136 next = tmpview->next;
137 tmpview->next = this;
138 }
139 DosLeaveCriticalSection(&globalmapcritsect);
140
141 if(owner) {
142 mOwnerMap = owner;
143 mOwnerMap->AddRef();
144 mOwnerMap->AddView();
145 }
146}
147//******************************************************************************
148//******************************************************************************
149Win32MemMapView::~Win32MemMapView()
150{
151 if(errorState != 0)
152 return;
153
154 dprintf(("Win32MemMapView dtor: deleting view %x %x", mOffset, mSize));
155
156 if(mfAccess & MEMMAP_ACCESS_WRITE)
157 mParentMap->flushView(MMAP_FLUSHVIEW_ALL, mOffset, mSize);
158
159 //Don't free memory for executable image map views (only used internally)
160 if(!mParentMap->getImage())
161 OSLibDosFreeMem(pMapView);
162
163 if(pShareViewAddr) {
164 OSLibDosFreeMem(pShareViewAddr);
165 }
166 if(pCOWBitmap) free(pCOWBitmap);
167
168 DosEnterCriticalSection(&globalmapcritsect);
169 Win32MemMapView *view = mapviews;
170
171 if(view == this) {
172 mapviews = next;
173 }
174 else {
175 while(view->next) {
176 if(view->next == this)
177 break;
178 view = view->next;
179 }
180 if(view->next) {
181 view->next = next;
182 }
183 else dprintf(("Win32MemMapView::~Win32MemMapView: map not found!! (%x)", this));
184 }
185 DosLeaveCriticalSection(&globalmapcritsect);
186
187 mParentMap->RemoveView();
188 mParentMap->Release();
189 if(mOwnerMap) {
190 mOwnerMap->RemoveView();
191 mOwnerMap->Release();
192 }
193}
194//******************************************************************************
195// Win32MemMapView::markCOWPages
196//
197// Mark pages as private in the COW page bitmap
198//
199// Parameters:
200//
201// int startpage - start page
202// int nrpages - number of pages
203//
204//
205//******************************************************************************
206void Win32MemMapView::markCOWPages(int startpage, int nrpages)
207{
208 int viewpagestart, nrviewpages;
209
210 if(pCOWBitmap == NULL) {
211 //not a COW view; ignore
212 return;
213 }
214 //check if this page is part of our view
215 viewpagestart = mOffset >> PAGE_SHIFT;
216 nrviewpages = mSize >> PAGE_SHIFT;
217 if(mSize & 0xFFF)
218 nrviewpages++;
219
220 if(startpage < viewpagestart || startpage >= viewpagestart+nrviewpages) {
221 return; //outside this view
222 }
223 if(startpage + nrpages > viewpagestart + nrviewpages) {
224 nrpages -= ((startpage + nrpages) - (viewpagestart + nrviewpages));
225 }
226
227 for(int i=startpage;i<startpage+nrpages;i++) {
228 set_bit(i, pCOWBitmap);
229 }
230}
231//******************************************************************************
232// Win32MemMapView::changePageFlags
233//
234// Change the protection flags of our alias. Called when a range of pages has
235// been committed.
236//
237// Parameters:
238//
239// ULONG offset - offset in memory map (page aligned!)
240// ULONG size - size of committed page range
241// PAGEVIEW flags - page flags
242// PAGEVIEW_READONLY -> set page flags to readonly
243// PAGEVIEW_VIEW -> set page flags to view default
244//
245// Returns:
246// TRUE - success
247// FALSE - failure
248//
249//******************************************************************************
250BOOL Win32MemMapView::changePageFlags(ULONG offset, ULONG size, PAGEVIEW flags)
251{
252 ULONG accessAttr = 0, rc;
253
254 //offset must be page aligned
255 if(offset & 0xFFF) {
256 DebugInt3();
257 return FALSE;
258 }
259
260 if( ( (mfAccess & MEMMAP_ACCESS_COPYONWRITE) && (flags != PAGEVIEW_GUARD) ) ||
261 ( (flags == PAGEVIEW_GUARD) && !(mfAccess & MEMMAP_ACCESS_COPYONWRITE) ) )
262 {
263 //PAGEVIEW_VIEW/READONLY does not apply to COW views
264 return TRUE;
265 }
266 if(mOffset + mSize <= offset || mOffset >= offset + size) {
267 return TRUE; //not part of this view
268 }
269 if(offset < mOffset) {
270 size -= mOffset - offset;
271 offset = mOffset;
272 }
273 if(mOffset + mSize < offset + size) {
274 size -= ((offset + size) - (mOffset + mSize));
275 }
276
277 if(flags == PAGEVIEW_READONLY) {
278 accessAttr = PAG_READ;
279 }
280 else
281 {//use view attributes
282 if(mfAccess & MEMMAP_ACCESS_READ) {
283 accessAttr |= PAG_READ;
284 }
285 if(mfAccess & MEMMAP_ACCESS_WRITE) {
286 accessAttr |= PAG_WRITE;
287 }
288 }
289
290 {
291 rc = OSLibDosSetMem((char *)pMapView+(offset - mOffset), size, accessAttr);
292 if(rc) {
293 dprintf(("Win32MemMapView::changePageFlags: OSLibDosSetMem %x %x %x failed with %d", (char *)pMapView+(offset - mOffset), size, accessAttr, rc));
294 return FALSE;
295 }
296 }
297 return TRUE;
298}
299//******************************************************************************
300//******************************************************************************
301int Win32MemMapView::findViews(Win32MemMap *map, int nrViews,
302 Win32MemMapView *viewarray[])
303{
304 int i=0;
305
306 DosEnterCriticalSection(&globalmapcritsect);
307 Win32MemMapView *view = mapviews, *nextview;
308
309 if(view != NULL)
310 {
311 while(view && i < nrViews)
312 {
313 if(view->getParentMap() == map)
314 {
315 viewarray[i] = view;
316 i++;
317 }
318 view = view->next;
319 }
320 }
321 DosLeaveCriticalSection(&globalmapcritsect);
322 return i;
323}
324//******************************************************************************
325//******************************************************************************
326void Win32MemMapView::deleteViews(Win32MemMap *map)
327{
328 DosEnterCriticalSection(&globalmapcritsect);
329 Win32MemMapView *view = mapviews, *nextview;
330
331 if(view != NULL)
332 {
333 while(view)
334 {
335 nextview = view->next;
336 if(view->getParentMap() == map)
337 {
338 DosLeaveCriticalSection(&globalmapcritsect);
339 delete view;
340 DosEnterCriticalSection(&globalmapcritsect);
341 }
342 view = nextview;
343 }
344 }
345 DosLeaveCriticalSection(&globalmapcritsect);
346}
347//******************************************************************************
348//******************************************************************************
349// Win32MemMap::findMapByView
350//
351// Find the map of the view that contains the specified starting address
352// and has the specified access type
353//
354// Parameters:
355//
356// ULONG address - view address
357// ULONG *offset - address of ULONG that receives the offset
358// in the returned memory map
359// ULONG accessType - access type:
360// MEMMAP_ACCESS_READ
361// MEMMAP_ACCESS_WRITE
362// MEMMAP_ACCESS_EXECUTE
363//
364// Returns:
365// <> NULL - success, address of parent map object
366// NULL - failure
367//
368//******************************************************************************
369//******************************************************************************
370Win32MemMap *Win32MemMapView::findMapByView(ULONG address,
371 ULONG *offset,
372 ULONG accessType)
373{
374 Win32MemMap *map = NULL;
375 ULONG ulOffset;
376
377 if(mapviews == NULL) return NULL;
378
379 DosEnterCriticalSection(&globalmapcritsect);
380 Win32MemMapView *view = mapviews;
381 ULONG ulViewAddr;
382
383 if(!offset) offset = &ulOffset;
384
385 *offset = 0;
386
387 if(view != NULL)
388 {
389 do
390 {
391 ulViewAddr = (ULONG)view->getViewAddr();
392
393 // if ulViewAddr is > address, we've exhausted
394 // the sorted list already and can abort search.
395 if(ulViewAddr <= address)
396 {
397 if(ulViewAddr + view->getSize() > address &&
398 view->getAccessFlags() >= accessType)
399 {
400 *offset = view->getOffset() + (address - ulViewAddr);
401 goto success;
402 }
403
404 // Not found yet, continue search with next map
405 view = view->next;
406 }
407 else
408 {
409 // list is exhausted, abort loop
410 view = NULL;
411 }
412 }
413 while(view);
414
415 //failure if we get here
416 view = NULL;
417 }
418success:
419#ifdef DEBUG
420 if(view && !view->getParentMap()->isImageMap())
421 dprintf(("findMapByView %x %x -> %x off %x",
422 address,
423 accessType,
424 view->getViewAddr(),
425 *offset));
426#endif
427
428 if(view) {
429 //first look at the owner (duplicate map), then the real parent
430 map = view->getOwnerMap();
431 if(!map) map = view->getParentMap();
432
433 if(map) map->AddRef();
434 }
435
436 DosLeaveCriticalSection(&globalmapcritsect);
437
438 return map;
439}
440//******************************************************************************
441// Win32MemMap::findView
442//
443// Find the view that contains the specified starting address
444//
445// Parameters:
446//
447// LPVOID address - view address
448//
449// Returns:
450// <> NULL - success, address view object
451// NULL - failure
452//
453//******************************************************************************
454Win32MemMapView *Win32MemMapView::findView(ULONG address)
455{
456 ULONG ulViewAddr;
457
458 DosEnterCriticalSection(&globalmapcritsect);
459 Win32MemMapView *view = mapviews;
460
461 if(view != NULL) {
462 while(view) {
463 ulViewAddr = (ULONG)view->getViewAddr();
464 if(ulViewAddr <= address && ulViewAddr + view->getSize() > address)
465 {
466 break;
467 }
468 view = view->next;
469 }
470 }
471 DosLeaveCriticalSection(&globalmapcritsect);
472 return view;
473}
474//******************************************************************************
475//******************************************************************************
Note: See TracBrowser for help on using the repository browser.