| 1 | /* $Id: mmap.cpp,v 1.72 2004-04-06 14:20:20 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 | #include "asmutil.h"
|
|---|
| 39 |
|
|---|
| 40 | #define DBG_LOCALLOG DBG_mmap
|
|---|
| 41 | #include "dbglocal.h"
|
|---|
| 42 |
|
|---|
| 43 | static LPCSTR pszMMapSemName = MEMMAP_CRITSECTION_NAME;
|
|---|
| 44 |
|
|---|
| 45 | //******************************************************************************
|
|---|
| 46 | //******************************************************************************
|
|---|
| 47 | void WIN32API SetCustomMMapSemName(LPSTR pszSemName)
|
|---|
| 48 | {
|
|---|
| 49 | pszMMapSemName = pszSemName;
|
|---|
| 50 | }
|
|---|
| 51 | //******************************************************************************
|
|---|
| 52 | //******************************************************************************
|
|---|
| 53 | void InitializeMemMaps()
|
|---|
| 54 | {
|
|---|
| 55 | if(globalmapcritsect.hevLock == 0) {
|
|---|
| 56 | dprintf(("InitializeMemMaps -> create shared critical section"));
|
|---|
| 57 | }
|
|---|
| 58 | else {
|
|---|
| 59 | dprintf(("InitializeMemMaps -> access shared critical section"));
|
|---|
| 60 | }
|
|---|
| 61 | DosAccessCriticalSection(&globalmapcritsect, pszMMapSemName);
|
|---|
| 62 | }
|
|---|
| 63 | //******************************************************************************
|
|---|
| 64 | //******************************************************************************
|
|---|
| 65 | void FinalizeMemMaps()
|
|---|
| 66 | {
|
|---|
| 67 | DosDeleteCriticalSection(&globalmapcritsect);
|
|---|
| 68 | }
|
|---|
| 69 | //******************************************************************************
|
|---|
| 70 | //******************************************************************************
|
|---|
| 71 | Win32MemMap::Win32MemMap(HANDLE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName)
|
|---|
| 72 | : nrMappings(0), pMapping(NULL), mMapAccess(0), referenced(0),
|
|---|
| 73 | image(0), pWriteBitmap(NULL), lpszFileName(NULL)
|
|---|
| 74 | {
|
|---|
| 75 | DosEnterCriticalSection(&globalmapcritsect);
|
|---|
| 76 | next = memmaps;
|
|---|
| 77 | memmaps = this;
|
|---|
| 78 | DosLeaveCriticalSection(&globalmapcritsect);
|
|---|
| 79 |
|
|---|
| 80 | hMemFile = hOrgMemFile = hfile;
|
|---|
| 81 |
|
|---|
| 82 | mSize = size;
|
|---|
| 83 | mProtFlags = fdwProtect;
|
|---|
| 84 |
|
|---|
| 85 | mProcessId = GetCurrentProcessId();
|
|---|
| 86 |
|
|---|
| 87 | if(lpszName) {
|
|---|
| 88 | lpszMapName = (char *)_smalloc(strlen(lpszName)+1);
|
|---|
| 89 | strcpy(lpszMapName, lpszName);
|
|---|
| 90 | }
|
|---|
| 91 | else lpszMapName = NULL;
|
|---|
| 92 | AddRef();
|
|---|
| 93 | }
|
|---|
| 94 | //******************************************************************************
|
|---|
| 95 | //Map constructor used for executable image maps (only used internally)
|
|---|
| 96 | //******************************************************************************
|
|---|
| 97 | Win32MemMap::Win32MemMap(Win32PeLdrImage *pImage, ULONG baseAddress, ULONG size)
|
|---|
| 98 | : nrMappings(0), pMapping(NULL), mMapAccess(0), referenced(0),
|
|---|
| 99 | image(0), pWriteBitmap(NULL), lpszFileName(NULL)
|
|---|
| 100 | {
|
|---|
| 101 | DosEnterCriticalSection(&globalmapcritsect);
|
|---|
| 102 | next = memmaps;
|
|---|
| 103 | memmaps = this;
|
|---|
| 104 | DosLeaveCriticalSection(&globalmapcritsect);
|
|---|
| 105 |
|
|---|
| 106 | hMemFile = hOrgMemFile = -1;
|
|---|
| 107 |
|
|---|
| 108 | mSize = size;
|
|---|
| 109 | mProtFlags = PAGE_READWRITE;
|
|---|
| 110 | mProcessId = GetCurrentProcessId();
|
|---|
| 111 |
|
|---|
| 112 | pMapping = (LPVOID)baseAddress;
|
|---|
| 113 |
|
|---|
| 114 | image = pImage;
|
|---|
| 115 | lpszMapName= NULL;
|
|---|
| 116 | AddRef();
|
|---|
| 117 | }
|
|---|
| 118 | //******************************************************************************
|
|---|
| 119 | //******************************************************************************
|
|---|
| 120 | BOOL Win32MemMap::Init(DWORD aMSize)
|
|---|
| 121 | {
|
|---|
| 122 | mapMutex.enter();
|
|---|
| 123 | if(hMemFile != -1)
|
|---|
| 124 | {
|
|---|
| 125 | lpszFileName = (char *)_smalloc(MMAP_MAX_FILENAME_LENGTH);
|
|---|
| 126 | if(HMGetFileNameFromHandle(hMemFile, lpszFileName, MMAP_MAX_FILENAME_LENGTH) == FALSE) {
|
|---|
| 127 | return FALSE;
|
|---|
| 128 | }
|
|---|
| 129 | #if 0
|
|---|
| 130 | if(DuplicateHandle(GetCurrentProcess(), hMemFile, GetCurrentProcess(),
|
|---|
| 131 | &hMemFile, 0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE)
|
|---|
| 132 | #else
|
|---|
| 133 | if(HMDuplicateHandle(GetCurrentProcess(), hMemFile, GetCurrentProcess(),
|
|---|
| 134 | &hMemFile, 0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE)
|
|---|
| 135 | #endif
|
|---|
| 136 | {
|
|---|
| 137 | dprintf(("Win32MemMap::Init: DuplicateHandle failed!"));
|
|---|
| 138 | goto fail;
|
|---|
| 139 | }
|
|---|
| 140 | mSize = SetFilePointer(hMemFile, 0, NULL, FILE_BEGIN);
|
|---|
| 141 | mSize = SetFilePointer(hMemFile, 0, NULL, FILE_END);
|
|---|
| 142 | if(mSize == -1) {
|
|---|
| 143 | dprintf(("Win32MemMap::init: SetFilePointer failed to set pos end"));
|
|---|
| 144 | goto fail;
|
|---|
| 145 | }
|
|---|
| 146 | if (mSize < aMSize)
|
|---|
| 147 | {
|
|---|
| 148 | dprintf(("Win32MemMap::init: file size %d, memory map size %d", mSize, aMSize));
|
|---|
| 149 | //Froloff: Need to check if exist the possibility of file to memory
|
|---|
| 150 | // mapping not from the beginning of file
|
|---|
| 151 | mSize = SetFilePointer(hMemFile, aMSize, NULL, FILE_BEGIN);
|
|---|
| 152 | // Commit filesize changes onto disk
|
|---|
| 153 | SetEndOfFile(hMemFile);
|
|---|
| 154 | }
|
|---|
| 155 | #if 0
|
|---|
| 156 | //SvL: Temporary limitation of size (Warp Server Advanced doesn't allow
|
|---|
| 157 | // one to reserve more than 450 MB (unless you override the virtual
|
|---|
| 158 | // memory max limit) of continuous memory; (Warp 4 much less))
|
|---|
| 159 | if(mSize > 64*1024*1024) {
|
|---|
| 160 | mSize = 64*1024*1024;
|
|---|
| 161 | }
|
|---|
| 162 | #endif
|
|---|
| 163 | }
|
|---|
| 164 | // Allocate the memory for the map right now
|
|---|
| 165 | if(allocateMap() == FALSE) {
|
|---|
| 166 | SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|---|
| 167 | goto fail;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | dprintf(("CreateFileMappingA for file %x, prot %x size %d, name %s", hMemFile, mProtFlags, mSize, lpszMapName));
|
|---|
| 171 | mapMutex.leave();
|
|---|
| 172 | return TRUE;
|
|---|
| 173 | fail:
|
|---|
| 174 | mapMutex.leave();
|
|---|
| 175 | return FALSE;
|
|---|
| 176 | }
|
|---|
| 177 | //******************************************************************************
|
|---|
| 178 | //******************************************************************************
|
|---|
| 179 | Win32MemMap::~Win32MemMap()
|
|---|
| 180 | {
|
|---|
| 181 | Win32MemMapView::deleteViews(this); //delete all views of our memory mapped file
|
|---|
| 182 |
|
|---|
| 183 | dprintf(("Win32MemMap dtor: deleting map %x %x", pMapping, mSize));
|
|---|
| 184 |
|
|---|
| 185 | mapMutex.enter();
|
|---|
| 186 | if(lpszMapName) {
|
|---|
| 187 | free(lpszMapName);
|
|---|
| 188 | }
|
|---|
| 189 | if(lpszFileName) {
|
|---|
| 190 | free(lpszFileName);
|
|---|
| 191 | }
|
|---|
| 192 | if(pMapping && !image) {
|
|---|
| 193 | dprintf(("Free map memory"));
|
|---|
| 194 | if(lpszMapName) {
|
|---|
| 195 | OSLibDosFreeMem(pMapping);
|
|---|
| 196 | }
|
|---|
| 197 | else VirtualFree(pMapping, 0, MEM_RELEASE);
|
|---|
| 198 |
|
|---|
| 199 | pMapping = NULL;
|
|---|
| 200 | }
|
|---|
| 201 | if(hMemFile != -1) {
|
|---|
| 202 | dprintf(("Win32MemMap dtor: closing memory file %x", hMemFile));
|
|---|
| 203 | CloseHandle(hMemFile);
|
|---|
| 204 | hMemFile = -1;
|
|---|
| 205 | }
|
|---|
| 206 | if(pWriteBitmap) free(pWriteBitmap);
|
|---|
| 207 |
|
|---|
| 208 | mapMutex.leave();
|
|---|
| 209 |
|
|---|
| 210 | DosEnterCriticalSection(&globalmapcritsect);
|
|---|
| 211 | Win32MemMap *map = memmaps;
|
|---|
| 212 |
|
|---|
| 213 | if(map == this) {
|
|---|
| 214 | memmaps = next;
|
|---|
| 215 | }
|
|---|
| 216 | else {
|
|---|
| 217 | while(map->next) {
|
|---|
| 218 | if(map->next == this)
|
|---|
| 219 | break;
|
|---|
| 220 | map = map->next;
|
|---|
| 221 | }
|
|---|
| 222 | if(map->next) {
|
|---|
| 223 | map->next = next;
|
|---|
| 224 | }
|
|---|
| 225 | else dprintf(("Win32MemMap::~Win32MemMap: map not found!! (%x)", this));
|
|---|
| 226 | }
|
|---|
| 227 | DosLeaveCriticalSection(&globalmapcritsect);
|
|---|
| 228 | }
|
|---|
| 229 | //******************************************************************************
|
|---|
| 230 | //******************************************************************************
|
|---|
| 231 | int Win32MemMap::Release()
|
|---|
| 232 | {
|
|---|
| 233 | dprintf(("Win32MemMap::Release %s (%d)", lpszMapName, referenced-1));
|
|---|
| 234 | --referenced;
|
|---|
| 235 | if(referenced == 0) {
|
|---|
| 236 | delete this;
|
|---|
| 237 | return 0;
|
|---|
| 238 | }
|
|---|
| 239 | return referenced;
|
|---|
| 240 | }
|
|---|
| 241 | //******************************************************************************
|
|---|
| 242 | // Win32MemMap::commitRange
|
|---|
| 243 | //
|
|---|
| 244 | // Commit a range of pages
|
|---|
| 245 | //
|
|---|
| 246 | // Parameters:
|
|---|
| 247 | //
|
|---|
| 248 | // ULONG ulFaultAddr - exception address
|
|---|
| 249 | // ULONG ulOffset - offset in memory map
|
|---|
| 250 | // BOOL fWriteAccess - TRUE -> write exception
|
|---|
| 251 | // FALSE -> read exception
|
|---|
| 252 | // int nrpages - number of pages
|
|---|
| 253 | //
|
|---|
| 254 | // Returns:
|
|---|
| 255 | // TRUE - success
|
|---|
| 256 | // FALSE - failure
|
|---|
| 257 | //
|
|---|
| 258 | //******************************************************************************
|
|---|
| 259 | BOOL Win32MemMap::commitRange(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages)
|
|---|
| 260 | {
|
|---|
| 261 | LPVOID lpPageFaultAddr = (LPVOID)((ULONG)pMapping + offset);
|
|---|
| 262 | DWORD pageAddr = (DWORD)lpPageFaultAddr & ~0xFFF;
|
|---|
| 263 |
|
|---|
| 264 | dprintf(("Win32MemMap::commitRange %x (faultaddr %x)", pageAddr, lpPageFaultAddr));
|
|---|
| 265 |
|
|---|
| 266 | if(fWriteAccess)
|
|---|
| 267 | {//writes are handled on a per-page basis
|
|---|
| 268 | for(int i=0;i<nrpages;i++)
|
|---|
| 269 | {
|
|---|
| 270 | if(commitPage(ulFaultAddr, offset, TRUE, 1) == FALSE) {
|
|---|
| 271 | dprintf(("Win32MemMap::commit: commitPage failed!!"));
|
|---|
| 272 | return FALSE;
|
|---|
| 273 | }
|
|---|
| 274 | ulFaultAddr += PAGE_SIZE;
|
|---|
| 275 | offset += PAGE_SIZE;
|
|---|
| 276 | }
|
|---|
| 277 | return TRUE;
|
|---|
| 278 | }
|
|---|
| 279 | else return commitPage(ulFaultAddr, offset, FALSE, nrpages);
|
|---|
| 280 | }
|
|---|
| 281 | //******************************************************************************
|
|---|
| 282 | // Win32MemMap::commitPage
|
|---|
| 283 | //
|
|---|
| 284 | // Handle a pagefault for a memory map
|
|---|
| 285 | //
|
|---|
| 286 | // Parameters:
|
|---|
| 287 | //
|
|---|
| 288 | // ULONG ulFaultAddr - exception address
|
|---|
| 289 | // ULONG ulOffset - offset in memory map
|
|---|
| 290 | // BOOL fWriteAccess - TRUE -> write exception
|
|---|
| 291 | // FALSE -> read exception
|
|---|
| 292 | // int nrpages - number of pages
|
|---|
| 293 | //
|
|---|
| 294 | // Returns:
|
|---|
| 295 | // TRUE - success
|
|---|
| 296 | // FALSE - failure
|
|---|
| 297 | //
|
|---|
| 298 | // NOTE:
|
|---|
| 299 | // We handle only one pages for write access!
|
|---|
| 300 | //
|
|---|
| 301 | // REMARKS:
|
|---|
| 302 | // We determine whether a page has been modified by checking it's protection flags
|
|---|
| 303 | // If the write flag is set, this means commitPage had to enable this due to a pagefault
|
|---|
| 304 | // (all pages are readonly until the app tries to write to it)
|
|---|
| 305 | //******************************************************************************
|
|---|
| 306 | BOOL Win32MemMap::commitPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages)
|
|---|
| 307 | {
|
|---|
| 308 | MEMORY_BASIC_INFORMATION memInfo;
|
|---|
| 309 | LPVOID lpPageFaultAddr = (LPVOID)((ULONG)pMapping + offset);
|
|---|
| 310 | DWORD pageAddr = (DWORD)lpPageFaultAddr & ~0xFFF;
|
|---|
| 311 | DWORD oldProt, newProt, nrBytesRead;
|
|---|
| 312 | int i;
|
|---|
| 313 |
|
|---|
| 314 | // mapMutex.enter();
|
|---|
| 315 |
|
|---|
| 316 | if(image) {
|
|---|
| 317 | return image->commitPage(pageAddr, fWriteAccess);
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | if(fWriteAccess && (mProtFlags & PAGE_WRITECOPY))
|
|---|
| 321 | {//this is a COW map, call commitGuardPage to handle write faults
|
|---|
| 322 | return commitGuardPage(ulFaultAddr, offset, fWriteAccess);
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | dprintf(("Win32MemMap::commitPage %x (faultaddr %x)", pageAddr, lpPageFaultAddr));
|
|---|
| 326 |
|
|---|
| 327 | //align at page boundary
|
|---|
| 328 | offset &= ~0xFFF;
|
|---|
| 329 |
|
|---|
| 330 | //If it's a write access violation and the view is readonly, then fail
|
|---|
| 331 | if(fWriteAccess) {
|
|---|
| 332 | Win32MemMapView *view = Win32MemMapView::findView(ulFaultAddr);
|
|---|
| 333 | if(view) {
|
|---|
| 334 | if(!(view->getAccessFlags() & MEMMAP_ACCESS_WRITE)) {
|
|---|
| 335 | dprintf(("Write access for a readonly view!!"));
|
|---|
| 336 | return FALSE;
|
|---|
| 337 | }
|
|---|
| 338 | }
|
|---|
| 339 | else {
|
|---|
| 340 | DebugInt3(); //can't happen
|
|---|
| 341 | return FALSE;
|
|---|
| 342 | }
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | int faultsize = nrpages*PAGE_SIZE;
|
|---|
| 346 |
|
|---|
| 347 | if(fWriteAccess)
|
|---|
| 348 | {//write access needs special care, so do that on a per page basis
|
|---|
| 349 | dprintf(("Write access -> handle only one page"));
|
|---|
| 350 | faultsize = PAGE_SIZE;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | offset = pageAddr - (ULONG)pMapping;
|
|---|
| 354 | if(offset + faultsize > mSize) {
|
|---|
| 355 | faultsize = mSize - offset;
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | while(faultsize)
|
|---|
| 359 | {
|
|---|
| 360 | if(VirtualQuery((LPSTR)pageAddr, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0) {
|
|---|
| 361 | dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed for %x", pageAddr, nrpages*PAGE_SIZE));
|
|---|
| 362 | goto fail;
|
|---|
| 363 | }
|
|---|
| 364 | memInfo.RegionSize = min(memInfo.RegionSize, faultsize);
|
|---|
| 365 | //Only changes the state of the pages with the same attribute flags
|
|---|
| 366 | //(returned in memInfo.RegionSize)
|
|---|
| 367 | //If it's smaller than the mNrPages, it simply means one or more of the
|
|---|
| 368 | //other pages have already been committed
|
|---|
| 369 | if(!(memInfo.State & MEM_COMMIT))
|
|---|
| 370 | {
|
|---|
| 371 | if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE) == FALSE) {
|
|---|
| 372 | goto fail;
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | //Part of the memory map has been committed, so now we can change
|
|---|
| 376 | //the protection flags of the aliases (DosSetMem fails for reserved pages)
|
|---|
| 377 | updateViewPages(offset, memInfo.RegionSize, (fWriteAccess) ? PAGEVIEW_VIEW : PAGEVIEW_READONLY);
|
|---|
| 378 |
|
|---|
| 379 | if(hMemFile != -1)
|
|---|
| 380 | {//now read the page(s) from disk
|
|---|
| 381 | DWORD size;
|
|---|
| 382 |
|
|---|
| 383 | offset = pageAddr - (ULONG)pMapping;
|
|---|
| 384 | size = memInfo.RegionSize;
|
|---|
| 385 | if(offset + size > mSize) {
|
|---|
| 386 | dprintf(("Adjusting size from %d to %d", size, mSize - offset));
|
|---|
| 387 | size = mSize - offset;
|
|---|
| 388 | }
|
|---|
| 389 | if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
|
|---|
| 390 | dprintf(("Win32MemMap::commitPage: SetFilePointer failed to set pos to %x", offset));
|
|---|
| 391 | goto fail;
|
|---|
| 392 | }
|
|---|
| 393 | if(ReadFile(hMemFile, (LPSTR)pageAddr, size, &nrBytesRead, NULL) == FALSE) {
|
|---|
| 394 | dprintf(("Win32MemMap::commitPage: ReadFile failed for %x", pageAddr));
|
|---|
| 395 | goto fail;
|
|---|
| 396 | }
|
|---|
| 397 | if(nrBytesRead != size) {
|
|---|
| 398 | dprintf(("Win32MemMap::commitPage: ReadFile didn't read all bytes for %x", pageAddr));
|
|---|
| 399 | goto fail;
|
|---|
| 400 | }
|
|---|
| 401 | }
|
|---|
| 402 | //We set the protection flags to PAGE_READONLY, unless this pagefault
|
|---|
| 403 | //was due to a write access
|
|---|
| 404 | //This way we can track dirty pages which need to be flushed to
|
|---|
| 405 | //disk when FlushViewOfFile is called or the map is closed.
|
|---|
| 406 | if(!fWriteAccess)
|
|---|
| 407 | {
|
|---|
| 408 | if(VirtualProtect((LPVOID)pageAddr, memInfo.RegionSize, PAGE_READONLY, &oldProt) == FALSE) {
|
|---|
| 409 | dprintf(("VirtualProtect %x %x PAGE_READWRITE failed with %d!!", pageAddr, memInfo.RegionSize, GetLastError()));
|
|---|
| 410 | goto fail;
|
|---|
| 411 | }
|
|---|
| 412 | }
|
|---|
| 413 | else
|
|---|
| 414 | {//make these pages as dirty
|
|---|
| 415 | ULONG startPage = (pageAddr - (ULONG)pMapping) >> PAGE_SHIFT;
|
|---|
| 416 | ULONG nrPages = memInfo.RegionSize >> PAGE_SHIFT;
|
|---|
| 417 |
|
|---|
| 418 | if(memInfo.RegionSize & 0xFFF)
|
|---|
| 419 | nrPages++;
|
|---|
| 420 |
|
|---|
| 421 | dprintf(("Mark %d page(s) starting at %x as dirty", nrPages, pageAddr));
|
|---|
| 422 | markDirtyPages(startPage, nrPages);
|
|---|
| 423 |
|
|---|
| 424 | //Write access means that the next time the corresponding COW page
|
|---|
| 425 | //is touched, we need to reload it. So set the GUARD flags.
|
|---|
| 426 | updateViewPages(offset, memInfo.RegionSize, PAGEVIEW_GUARD);
|
|---|
| 427 | }
|
|---|
| 428 | }
|
|---|
| 429 | else
|
|---|
| 430 | if(fWriteAccess)
|
|---|
| 431 | {
|
|---|
| 432 | //mark these pages as dirty
|
|---|
| 433 | ULONG startPage = (pageAddr - (ULONG)pMapping) >> PAGE_SHIFT;
|
|---|
| 434 | ULONG nrPages = memInfo.RegionSize >> PAGE_SHIFT;
|
|---|
| 435 |
|
|---|
| 436 | if(memInfo.RegionSize & 0xFFF)
|
|---|
| 437 | nrPages++;
|
|---|
| 438 |
|
|---|
| 439 | dprintf(("Mark %d page(s) starting at %x as dirty", nrPages, pageAddr));
|
|---|
| 440 | markDirtyPages(startPage, nrPages);
|
|---|
| 441 |
|
|---|
| 442 | //and turn on a write access
|
|---|
| 443 | if(VirtualProtect((LPVOID)pageAddr, memInfo.RegionSize, PAGE_READWRITE, &oldProt) == FALSE) {
|
|---|
| 444 | dprintf(("VirtualProtect %x %x PAGE_READWRITE failed with %d!!", pageAddr, memInfo.RegionSize, GetLastError()));
|
|---|
| 445 | goto fail;
|
|---|
| 446 | }
|
|---|
| 447 | //Now change all the aliased pages according to their view protection flags
|
|---|
| 448 | updateViewPages(offset, memInfo.RegionSize, PAGEVIEW_VIEW);
|
|---|
| 449 |
|
|---|
| 450 | //Write access means that the next time the corresponding COW page
|
|---|
| 451 | //is touched, we need to reload it. So set the GUARD flags.
|
|---|
| 452 | updateViewPages(offset, memInfo.RegionSize, PAGEVIEW_GUARD);
|
|---|
| 453 | }
|
|---|
| 454 | faultsize -= memInfo.RegionSize;
|
|---|
| 455 | pageAddr += memInfo.RegionSize;
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | // mapMutex.leave();
|
|---|
| 459 | return TRUE;
|
|---|
| 460 | fail:
|
|---|
| 461 | // mapMutex.leave();
|
|---|
| 462 | return FALSE;
|
|---|
| 463 | }
|
|---|
| 464 | //******************************************************************************
|
|---|
| 465 | // Win32MemMap::commitGuardPage
|
|---|
| 466 | //
|
|---|
| 467 | // Handle a guard page exception for a copy-on-write view (one page only)
|
|---|
| 468 | //
|
|---|
| 469 | // Parameters:
|
|---|
| 470 | //
|
|---|
| 471 | // ULONG ulFaultAddr - exception address
|
|---|
| 472 | // ULONG ulOffset - offset in memory map
|
|---|
| 473 | // BOOL fWriteAccess - TRUE -> write exception
|
|---|
| 474 | // FALSE -> read exception
|
|---|
| 475 | //
|
|---|
| 476 | // Returns:
|
|---|
| 477 | // TRUE - success
|
|---|
| 478 | // FALSE - failure
|
|---|
| 479 | //
|
|---|
| 480 | //******************************************************************************
|
|---|
| 481 | BOOL Win32MemMap::commitGuardPage(ULONG ulFaultAddr, ULONG ulOffset, BOOL fWriteAccess)
|
|---|
| 482 | {
|
|---|
| 483 | MEMORY_BASIC_INFORMATION memInfo;
|
|---|
| 484 | BOOL ret;
|
|---|
| 485 | DWORD pageAddr = ulFaultAddr & ~0xFFF;
|
|---|
| 486 | DWORD dwNewProt, dwOldProt;
|
|---|
| 487 |
|
|---|
| 488 | dprintf(("Win32MemMap::commitGuardPage %x (faultaddr %x)", pageAddr, ulFaultAddr));
|
|---|
| 489 |
|
|---|
| 490 | //align at page boundary
|
|---|
| 491 | ulOffset &= ~0xFFF;
|
|---|
| 492 |
|
|---|
| 493 | //commit a single page in the original mapping (pretend read access)
|
|---|
| 494 | ret = commitPage(ulFaultAddr, ulOffset, FALSE, 1);
|
|---|
| 495 | if(ret == FALSE) {
|
|---|
| 496 | DebugInt3();
|
|---|
| 497 | goto fail;
|
|---|
| 498 | }
|
|---|
| 499 | //clear PAGE_GUARD flag, since this page must now be made accessible
|
|---|
| 500 | dwNewProt = mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY);
|
|---|
| 501 | if(dwNewProt & PAGE_WRITECOPY) {
|
|---|
| 502 | dwNewProt |= PAGE_GUARD;
|
|---|
| 503 | }
|
|---|
| 504 | dwNewProt &= ~PAGE_GUARD;
|
|---|
| 505 | if(VirtualProtect((LPVOID)pageAddr, PAGE_SIZE, dwNewProt, &dwOldProt) == FALSE) {
|
|---|
| 506 | dprintf(("Win32MemMap::commitGuardPage: VirtualProtect %x 0x1000 %x failed!!", pageAddr, dwNewProt));
|
|---|
| 507 | goto fail;
|
|---|
| 508 | }
|
|---|
| 509 | //copy the data from the original mapping to the COW page
|
|---|
| 510 | memcpy((LPVOID)pageAddr, (char *)pMapping+ulOffset, PAGE_SIZE);
|
|---|
| 511 |
|
|---|
| 512 | if(fWriteAccess)
|
|---|
| 513 | {//copy on write; mark pages as private
|
|---|
| 514 | DWORD startpage = (ulOffset) >> PAGE_SHIFT;
|
|---|
| 515 |
|
|---|
| 516 | dprintf(("Win32MemMap::commitGuardPage: write access -> mark page as private"));
|
|---|
| 517 |
|
|---|
| 518 | Win32MemMapView *view = Win32MemMapView::findView(ulFaultAddr);
|
|---|
| 519 | if(view)
|
|---|
| 520 | {
|
|---|
| 521 | view->markCOWPages(startpage, 1);
|
|---|
| 522 | }
|
|---|
| 523 | else DebugInt3(); //oh, oh
|
|---|
| 524 | }
|
|---|
| 525 | else
|
|---|
| 526 | {//read access; must set the map + all views to READONLY to track write access
|
|---|
| 527 |
|
|---|
| 528 | dprintf(("Win32MemMap::commitGuardPage: read access -> set page as READONLY in map + all views"));
|
|---|
| 529 |
|
|---|
| 530 | //first the memory map page
|
|---|
| 531 | if(VirtualProtect((LPVOID)pageAddr, PAGE_SIZE, PAGE_READONLY, &dwOldProt) == FALSE) {
|
|---|
| 532 | dprintf(("Win32MemMap::commitGuardPage: VirtualProtect %x 0x1000 %x failed!!", pageAddr, dwNewProt));
|
|---|
| 533 | goto fail;
|
|---|
| 534 | }
|
|---|
| 535 | //now for any views that exist
|
|---|
| 536 | updateViewPages(ulOffset, PAGE_SIZE, PAGEVIEW_READONLY);
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | return TRUE;
|
|---|
| 540 | fail:
|
|---|
| 541 | return FALSE;
|
|---|
| 542 | }
|
|---|
| 543 | //******************************************************************************
|
|---|
| 544 | // Win32MemMap::updateViewPages
|
|---|
| 545 | //
|
|---|
| 546 | // Update the page flags of all views
|
|---|
| 547 | //
|
|---|
| 548 | // Parameters:
|
|---|
| 549 | //
|
|---|
| 550 | // ULONG offset - offset in memory map
|
|---|
| 551 | // ULONG size - range size
|
|---|
| 552 | // PAGEVIEW flags - page flags
|
|---|
| 553 | // PAGEVIEW_READONLY -> set page flags to readonly
|
|---|
| 554 | // PAGEVIEW_VIEW -> set page flags to view default
|
|---|
| 555 | // PAGEVIEW_GUARD -> set page flags of COW view to GUARD
|
|---|
| 556 | //
|
|---|
| 557 | // Returns:
|
|---|
| 558 | // TRUE - success
|
|---|
| 559 | // FALSE - failure
|
|---|
| 560 | //
|
|---|
| 561 | //******************************************************************************
|
|---|
| 562 | BOOL Win32MemMap::updateViewPages(ULONG offset, ULONG size, PAGEVIEW flags)
|
|---|
| 563 | {
|
|---|
| 564 | Win32MemMapView **views = (Win32MemMapView **)alloca(sizeof(Win32MemMapView*)*nrMappings);
|
|---|
| 565 | int localmaps;
|
|---|
| 566 |
|
|---|
| 567 | if(views)
|
|---|
| 568 | {
|
|---|
| 569 | localmaps = Win32MemMapView::findViews(this, nrMappings, views);
|
|---|
| 570 | if(localmaps <= nrMappings)
|
|---|
| 571 | {
|
|---|
| 572 | for(int i=0;i<localmaps;i++)
|
|---|
| 573 | {
|
|---|
| 574 | views[i]->changePageFlags(offset, size, flags);
|
|---|
| 575 | }
|
|---|
| 576 | }
|
|---|
| 577 | else {
|
|---|
| 578 | dprintf(("unexpected number of views %d vs %d", localmaps, nrMappings));
|
|---|
| 579 | DebugInt3(); //oh, oh
|
|---|
| 580 | }
|
|---|
| 581 | }
|
|---|
| 582 | return TRUE;
|
|---|
| 583 | }
|
|---|
| 584 | //******************************************************************************
|
|---|
| 585 | // Win32MemMap::invalidatePages
|
|---|
| 586 | //
|
|---|
| 587 | // Invalidate map pages. (called by WriteFile)
|
|---|
| 588 | //
|
|---|
| 589 | // Parameters:
|
|---|
| 590 | //
|
|---|
| 591 | // ULONG offset - offset in memory map
|
|---|
| 592 | // ULONG size - invalid range size
|
|---|
| 593 | //
|
|---|
| 594 | // Returns:
|
|---|
| 595 | // TRUE - success
|
|---|
| 596 | // FALSE - failure
|
|---|
| 597 | //
|
|---|
| 598 | //******************************************************************************
|
|---|
| 599 | BOOL Win32MemMap::invalidatePages(ULONG offset, ULONG size)
|
|---|
| 600 | {
|
|---|
| 601 | ULONG diff = offset & 0xFFF;
|
|---|
| 602 | BOOL ret;
|
|---|
| 603 |
|
|---|
| 604 | offset &= ~0xFFF;
|
|---|
| 605 | size += diff;
|
|---|
| 606 |
|
|---|
| 607 | dprintf(("Win32MemMap::invalidatePages %x %x", offset, size));
|
|---|
| 608 | ret = VirtualFree((LPSTR)pMapping + offset, size, MEM_DECOMMIT);
|
|---|
| 609 | if(ret == FALSE) {
|
|---|
| 610 | dprintf(("ERROR: Win32MemMap::invalidatePages: VirtualFree failed!!"));
|
|---|
| 611 | }
|
|---|
| 612 | //invalidate all shared COW pages too by setting the GUARD flag
|
|---|
| 613 | //(which forces a resync the next time the app touches them)
|
|---|
| 614 | return updateViewPages(offset, size, PAGEVIEW_GUARD);
|
|---|
| 615 | }
|
|---|
| 616 | //******************************************************************************
|
|---|
| 617 | // Win32MemMap::allocateMap
|
|---|
| 618 | //
|
|---|
| 619 | // Allocate memory for the map if not yet already done.
|
|---|
| 620 | //
|
|---|
| 621 | // Returns:
|
|---|
| 622 | // FALSE - success
|
|---|
| 623 | // TRUE - failure
|
|---|
| 624 | //
|
|---|
| 625 | //******************************************************************************
|
|---|
| 626 | BOOL Win32MemMap::allocateMap()
|
|---|
| 627 | {
|
|---|
| 628 | ULONG fAlloc = 0;
|
|---|
| 629 |
|
|---|
| 630 | fAlloc = MEM_RESERVE;
|
|---|
| 631 |
|
|---|
| 632 | //Memory has already been allocated for executable image maps (only used internally)
|
|---|
| 633 | if(!pMapping && nrMappings == 0)
|
|---|
| 634 | {//if not mapped, reserve/commit entire view
|
|---|
| 635 | //SvL: Always read/write access or else ReadFile will crash once we
|
|---|
| 636 | // start committing pages.
|
|---|
| 637 | // This is most likely an OS/2 bug and doesn't happen in Aurora
|
|---|
| 638 | // when allocating memory with the PAG_ANY bit set. (without this
|
|---|
| 639 | // flag it will also crash)
|
|---|
| 640 | //NOTE: If this is ever changed, then we must update setProtFlags!!!!
|
|---|
| 641 |
|
|---|
| 642 | //All named file mappings are shared (files & memory only)
|
|---|
| 643 | if(lpszMapName) {
|
|---|
| 644 | pMapping = VirtualAllocShared(mSize, fAlloc, PAGE_READWRITE, lpszMapName);
|
|---|
| 645 | }
|
|---|
| 646 | else {
|
|---|
| 647 | pMapping = VirtualAlloc(0, mSize, fAlloc, PAGE_READWRITE);
|
|---|
| 648 | }
|
|---|
| 649 | if(pMapping == NULL) {
|
|---|
| 650 | dprintf(("Win32MemMap::mapViewOfFile: VirtualAlloc %x %x failed!", mSize, fAlloc));
|
|---|
| 651 | goto fail;
|
|---|
| 652 | }
|
|---|
| 653 | //Windows NT seems to commit memory for memory maps, regardsless of the SEC_COMMIT flag
|
|---|
| 654 | if((hMemFile == -1 && !image)) {//commit memory
|
|---|
| 655 | VirtualAlloc(pMapping, mSize, MEM_COMMIT, PAGE_READWRITE);
|
|---|
| 656 | }
|
|---|
| 657 |
|
|---|
| 658 | DWORD nrPages = mSize >> PAGE_SHIFT;
|
|---|
| 659 | if(mSize & 0xFFF)
|
|---|
| 660 | nrPages++;
|
|---|
| 661 |
|
|---|
| 662 | if(hMemFile != -1 && (mProtFlags & SEC_COMMIT)) {
|
|---|
| 663 | commitPage((ULONG)pMapping, 0, FALSE, nrPages);
|
|---|
| 664 | }
|
|---|
| 665 | //Allocate bitmap for all pages to keep track of write access (file maps only)
|
|---|
| 666 | //Necessary for FlushViewOfFile.
|
|---|
| 667 | if(hMemFile != -1) {
|
|---|
| 668 | int sizebitmap = nrPages/8 + 1;
|
|---|
| 669 |
|
|---|
| 670 | pWriteBitmap = (char *)_smalloc(sizebitmap);
|
|---|
| 671 | if(pWriteBitmap == NULL) {
|
|---|
| 672 | DebugInt3();
|
|---|
| 673 | goto fail;
|
|---|
| 674 | }
|
|---|
| 675 | memset(pWriteBitmap, 0, sizebitmap);
|
|---|
| 676 | }
|
|---|
| 677 | }
|
|---|
| 678 | return TRUE;
|
|---|
| 679 |
|
|---|
| 680 | fail:
|
|---|
| 681 | return FALSE;
|
|---|
| 682 | }
|
|---|
| 683 | //******************************************************************************
|
|---|
| 684 | // Win32MemMap::mapViewOfFile
|
|---|
| 685 | //
|
|---|
| 686 | // Map the view identified by addr
|
|---|
| 687 | //
|
|---|
| 688 | // Parameters:
|
|---|
| 689 | //
|
|---|
| 690 | // ULONG size - size of view
|
|---|
| 691 | // ULONG offset - offset in memory map
|
|---|
| 692 | // ULONG fdwAccess - access flags
|
|---|
| 693 | // FILE_MAP_WRITE, FILE_MAP_READ, FILE_MAP_COPY
|
|---|
| 694 | // FILE_MAP_ALL_ACCESS
|
|---|
| 695 | //
|
|---|
| 696 | //
|
|---|
| 697 | // Returns:
|
|---|
| 698 | // <>NULL - success, view address
|
|---|
| 699 | // NULL - failure
|
|---|
| 700 | //
|
|---|
| 701 | //******************************************************************************
|
|---|
| 702 | LPVOID Win32MemMap::mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess)
|
|---|
| 703 | {
|
|---|
| 704 | DWORD processId = GetCurrentProcessId();
|
|---|
| 705 |
|
|---|
| 706 | mapMutex.enter();
|
|---|
| 707 | ULONG memFlags = (mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY));
|
|---|
| 708 | Win32MemMapView *mapview;
|
|---|
| 709 |
|
|---|
| 710 | //@@@PH: if(fdwAccess & ~(FILE_MAP_WRITE|FILE_MAP_READ|FILE_MAP_COPY))
|
|---|
| 711 | // Docs say FILE_MAP_ALL_ACCESS is same as FILE_MAP_WRITE. Doesn't match reality though.
|
|---|
| 712 | if(fdwAccess & ~FILE_MAP_ALL_ACCESS)
|
|---|
| 713 | goto parmfail;
|
|---|
| 714 | if((fdwAccess & FILE_MAP_WRITE) && !(mProtFlags & PAGE_READWRITE))
|
|---|
| 715 | goto parmfail;
|
|---|
| 716 | // CreateFileMapping docs say that PAGE_WRITECOPY is equivalent to PAGE_READONLY and thus
|
|---|
| 717 | // is okay for FILE_MAP_READ too
|
|---|
| 718 | if((fdwAccess & FILE_MAP_READ) && !(mProtFlags & (PAGE_READWRITE|PAGE_READONLY|PAGE_WRITECOPY)))
|
|---|
| 719 | goto parmfail;
|
|---|
| 720 |
|
|---|
| 721 | if (fdwAccess != FILE_MAP_ALL_ACCESS)
|
|---|
| 722 | if((fdwAccess & FILE_MAP_COPY) && !(mProtFlags & PAGE_WRITECOPY))
|
|---|
| 723 | goto parmfail;
|
|---|
| 724 |
|
|---|
| 725 | if(offset+size > mSize && (!(fdwAccess & FILE_MAP_WRITE) || hMemFile == -1))
|
|---|
| 726 | goto parmfail;
|
|---|
| 727 |
|
|---|
| 728 | //SvL: TODO: Doesn't work for multiple views
|
|---|
| 729 | if(offset+size > mSize) {
|
|---|
| 730 | mSize = offset+size;
|
|---|
| 731 | }
|
|---|
| 732 |
|
|---|
| 733 | mapview = new Win32MemMapView(this, offset, (size == 0) ? (mSize - offset) : size, fdwAccess);
|
|---|
| 734 | if(mapview == NULL) {
|
|---|
| 735 | goto fail;
|
|---|
| 736 | }
|
|---|
| 737 | if(mapview->everythingOk() == FALSE) {
|
|---|
| 738 | dprintf(("Win32MemMap::mapViewOfFile: !mapview->everythingOk"));
|
|---|
| 739 | delete mapview;
|
|---|
| 740 | goto fail;
|
|---|
| 741 | }
|
|---|
| 742 | mapMutex.leave();
|
|---|
| 743 | SetLastError(ERROR_SUCCESS);
|
|---|
| 744 | return mapview->getViewAddr();
|
|---|
| 745 |
|
|---|
| 746 | parmfail:
|
|---|
| 747 | dprintf(("Win32MemMap::mapViewOfFile: invalid parameter (ERROR_ACCESS_DENIED)"));
|
|---|
| 748 | //NT4 SP6 returns ERROR_ACCESS_DENIED for most invalid parameters
|
|---|
| 749 | SetLastError(ERROR_ACCESS_DENIED);
|
|---|
| 750 | fail:
|
|---|
| 751 | mapMutex.leave();
|
|---|
| 752 | return 0;
|
|---|
| 753 | }
|
|---|
| 754 | //******************************************************************************
|
|---|
| 755 | // Win32MemMap::unmapViewOfFile
|
|---|
| 756 | //
|
|---|
| 757 | // Unmap the view identified by addr
|
|---|
| 758 | //
|
|---|
| 759 | // Parameters:
|
|---|
| 760 | //
|
|---|
| 761 | // LPVOID addr - view address; doesn't need to be the address
|
|---|
| 762 | // returned by MapViewOfFile(Ex) (as MSDN clearly says);
|
|---|
| 763 | // can be any address within the view range
|
|---|
| 764 | //
|
|---|
| 765 | // Returns:
|
|---|
| 766 | // TRUE - success
|
|---|
| 767 | // FALSE - failure
|
|---|
| 768 | //
|
|---|
| 769 | //******************************************************************************
|
|---|
| 770 | BOOL Win32MemMap::unmapViewOfFile(LPVOID addr)
|
|---|
| 771 | {
|
|---|
| 772 | Win32MemMapView *view;
|
|---|
| 773 |
|
|---|
| 774 | dprintf(("Win32MemMap::unmapViewOfFile %x (nrmaps=%d)", addr, nrMappings));
|
|---|
| 775 | mapMutex.enter();
|
|---|
| 776 |
|
|---|
| 777 | if(nrMappings == 0)
|
|---|
| 778 | goto fail;
|
|---|
| 779 |
|
|---|
| 780 | view = Win32MemMapView::findView((ULONG)addr);
|
|---|
| 781 | if(view == NULL)
|
|---|
| 782 | goto fail;
|
|---|
| 783 |
|
|---|
| 784 | delete view;
|
|---|
| 785 |
|
|---|
| 786 | mapMutex.leave();
|
|---|
| 787 |
|
|---|
| 788 | SetLastError(ERROR_SUCCESS);
|
|---|
| 789 | return TRUE;
|
|---|
| 790 | fail:
|
|---|
| 791 | mapMutex.leave();
|
|---|
| 792 | SetLastError(ERROR_INVALID_ADDRESS);
|
|---|
| 793 | return FALSE;
|
|---|
| 794 | }
|
|---|
| 795 | //******************************************************************************
|
|---|
| 796 | //We determine whether a page has been modified by checking it's protection flags
|
|---|
| 797 | //If the write flag is set, this means commitPage had to enable this due to a pagefault
|
|---|
| 798 | //(all pages are readonly until the app tries to modify the contents of the page)
|
|---|
| 799 | //
|
|---|
| 800 | //TODO: Are apps allowed to change the protection flags of memory mapped pages?
|
|---|
| 801 | // I'm assuming they aren't for now.
|
|---|
| 802 | //******************************************************************************
|
|---|
| 803 | BOOL Win32MemMap::flushView(ULONG viewaddr, ULONG offset, ULONG cbFlush)
|
|---|
| 804 | {
|
|---|
| 805 | ULONG nrBytesWritten, size, accessflags, oldProt;
|
|---|
| 806 | Win32MemMapView *view;
|
|---|
| 807 | int i;
|
|---|
| 808 |
|
|---|
| 809 | dprintf(("Win32MemMap::flushView: %x %x", (ULONG)pMapping+offset, cbFlush));
|
|---|
| 810 |
|
|---|
| 811 | if(image) //no flushing for image maps
|
|---|
| 812 | return TRUE;
|
|---|
| 813 |
|
|---|
| 814 | if(hMemFile == -1)
|
|---|
| 815 | goto success; //TODO: Return an error here?
|
|---|
| 816 |
|
|---|
| 817 | if(offset > mSize)
|
|---|
| 818 | goto parmfail;
|
|---|
| 819 |
|
|---|
| 820 | if(viewaddr != MMAP_FLUSHVIEW_ALL)
|
|---|
| 821 | {
|
|---|
| 822 | view = Win32MemMapView::findView(viewaddr);
|
|---|
| 823 | if(nrMappings == 0 || view == NULL) {
|
|---|
| 824 | DebugInt3(); //should never happen
|
|---|
| 825 | goto parmfail;
|
|---|
| 826 | }
|
|---|
| 827 | accessflags = view->getAccessFlags();
|
|---|
| 828 | }
|
|---|
| 829 | else {
|
|---|
| 830 | //force a flush to disk; only those pages marked dirty are flushed anyway
|
|---|
| 831 | accessflags = FILE_MAP_WRITE;
|
|---|
| 832 | }
|
|---|
| 833 | //If the view is readonly or copy on write, then the flush is ignored
|
|---|
| 834 | if(!(accessflags & MEMMAP_ACCESS_WRITE) || (accessflags & MEMMAP_ACCESS_COPYONWRITE))
|
|---|
| 835 | {
|
|---|
| 836 | dprintf(("Readonly or Copy-On-Write memory map -> ignore flush"));
|
|---|
| 837 | //this is not a failure; NT4 SP6 returns success
|
|---|
| 838 | goto success;
|
|---|
| 839 | }
|
|---|
| 840 |
|
|---|
| 841 | if(cbFlush == 0)
|
|---|
| 842 | cbFlush = mSize;
|
|---|
| 843 |
|
|---|
| 844 | if(offset + cbFlush > mSize) {
|
|---|
| 845 | cbFlush -= (offset + cbFlush - mSize);
|
|---|
| 846 | }
|
|---|
| 847 |
|
|---|
| 848 | //Check the write page bitmap for dirty pages and write them to disk
|
|---|
| 849 | while(cbFlush)
|
|---|
| 850 | {
|
|---|
| 851 | int startPage = offset >> PAGE_SHIFT;
|
|---|
| 852 | size = PAGE_SIZE;
|
|---|
| 853 |
|
|---|
| 854 | if(isDirtyPage(startPage))
|
|---|
| 855 | {
|
|---|
| 856 | if(size > cbFlush) {
|
|---|
| 857 | size = cbFlush;
|
|---|
| 858 | }
|
|---|
| 859 | dprintf(("Win32MemMap::flushView for offset %x, size %d", offset, size));
|
|---|
| 860 |
|
|---|
| 861 | if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
|
|---|
| 862 | dprintf(("Win32MemMap::flushView: SetFilePointer failed to set pos to %x", offset));
|
|---|
| 863 | goto fail;
|
|---|
| 864 | }
|
|---|
| 865 | if(WriteFile(hMemFile, (LPSTR)((ULONG)pMapping + offset), size, &nrBytesWritten, NULL) == FALSE) {
|
|---|
| 866 | dprintf(("Win32MemMap::flushView: WriteFile failed for %x", (ULONG)pMapping + offset));
|
|---|
| 867 | goto fail;
|
|---|
| 868 | }
|
|---|
| 869 | if(nrBytesWritten != size) {
|
|---|
| 870 | dprintf(("Win32MemMap::flushView: WriteFile didn't write all bytes for %x", (ULONG)pMapping + offset));
|
|---|
| 871 | goto fail;
|
|---|
| 872 | }
|
|---|
| 873 | clearDirtyPages(startPage, 1);
|
|---|
| 874 |
|
|---|
| 875 | //We've just flushed the page to disk, so we need to track future writes
|
|---|
| 876 | //again; Set page to readonly (first memory map, then alias(es))
|
|---|
| 877 | if(VirtualProtect((LPVOID)((ULONG)pMapping + offset), size, PAGE_READONLY, &oldProt) == FALSE) {
|
|---|
| 878 | dprintf(("VirtualProtect %x %x PAGE_READWRITE failed with %d!!", (ULONG)pMapping + offset, size, GetLastError()));
|
|---|
| 879 | goto fail;
|
|---|
| 880 | }
|
|---|
| 881 | updateViewPages(offset, size, PAGEVIEW_READONLY);
|
|---|
| 882 | }
|
|---|
| 883 |
|
|---|
| 884 | if(cbFlush < size)
|
|---|
| 885 | break;
|
|---|
| 886 |
|
|---|
| 887 | cbFlush -= size;
|
|---|
| 888 | offset += size;
|
|---|
| 889 | }
|
|---|
| 890 | success:
|
|---|
| 891 | SetLastError(ERROR_SUCCESS);
|
|---|
| 892 | return TRUE;
|
|---|
| 893 |
|
|---|
| 894 | parmfail:
|
|---|
| 895 | SetLastError(ERROR_INVALID_PARAMETER);
|
|---|
| 896 | return FALSE;
|
|---|
| 897 | fail:
|
|---|
| 898 | return FALSE;
|
|---|
| 899 | }
|
|---|
| 900 | //******************************************************************************
|
|---|
| 901 | //******************************************************************************
|
|---|
| 902 | Win32MemMap *Win32MemMap::findMap(LPSTR lpszName)
|
|---|
| 903 | {
|
|---|
| 904 | if(lpszName == NULL)
|
|---|
| 905 | return NULL;
|
|---|
| 906 |
|
|---|
| 907 | DosEnterCriticalSection(&globalmapcritsect);
|
|---|
| 908 | Win32MemMap *map = memmaps;
|
|---|
| 909 |
|
|---|
| 910 | if(map != NULL) {
|
|---|
| 911 | while(map) {
|
|---|
| 912 | if(map->lpszMapName && !strcmp(map->lpszMapName, lpszName))
|
|---|
| 913 | break;
|
|---|
| 914 | map = map->next;
|
|---|
| 915 | }
|
|---|
| 916 | }
|
|---|
| 917 | if(map) map->AddRef();
|
|---|
| 918 |
|
|---|
| 919 | DosLeaveCriticalSection(&globalmapcritsect);
|
|---|
| 920 | if(!map) dprintf(("Win32MemMap::findMap: couldn't find map %s", lpszName));
|
|---|
| 921 | return map;
|
|---|
| 922 | }
|
|---|
| 923 | //******************************************************************************
|
|---|
| 924 | //******************************************************************************
|
|---|
| 925 | Win32MemMap *Win32MemMap::findMapByFile(HANDLE hFile)
|
|---|
| 926 | {
|
|---|
| 927 | DWORD processId = GetCurrentProcessId();
|
|---|
| 928 | char szFileName[260];
|
|---|
| 929 |
|
|---|
| 930 | if(hFile == -1)
|
|---|
| 931 | return NULL;
|
|---|
| 932 |
|
|---|
| 933 | if(HMGetFileNameFromHandle(hFile, szFileName, sizeof(szFileName)) == FALSE)
|
|---|
| 934 | return NULL;
|
|---|
| 935 |
|
|---|
| 936 | DosEnterCriticalSection(&globalmapcritsect);
|
|---|
| 937 | Win32MemMap *map = memmaps;
|
|---|
| 938 |
|
|---|
| 939 | if(map != NULL)
|
|---|
| 940 | {
|
|---|
| 941 | while(map) {
|
|---|
| 942 | //TODO: we currently don't support sharing file maps between processes
|
|---|
| 943 | if(map->mProcessId == processId && map->lpszFileName)
|
|---|
| 944 | {
|
|---|
| 945 | if(!strcmp(map->lpszFileName, szFileName) && map->getFileHandle() != hFile)
|
|---|
| 946 | break;
|
|---|
| 947 | }
|
|---|
| 948 | map = map->next;
|
|---|
| 949 | }
|
|---|
| 950 | }
|
|---|
| 951 | if(map) map->AddRef();
|
|---|
| 952 | DosLeaveCriticalSection(&globalmapcritsect);
|
|---|
| 953 | if(!map) dprintf2(("Win32MemMap::findMapByFile: couldn't find map with file handle %x", hFile));
|
|---|
| 954 | return map;
|
|---|
| 955 | }
|
|---|
| 956 | //******************************************************************************
|
|---|
| 957 | //******************************************************************************
|
|---|
| 958 | Win32MemMap *Win32MemMap::findMap(ULONG address)
|
|---|
| 959 | {
|
|---|
| 960 | DosEnterCriticalSection(&globalmapcritsect);
|
|---|
| 961 | Win32MemMap *map = memmaps;
|
|---|
| 962 |
|
|---|
| 963 | if(map != NULL) {
|
|---|
| 964 | while(map) {
|
|---|
| 965 | if(map->pMapping && (ULONG)map->pMapping <= address &&
|
|---|
| 966 | (ULONG)map->pMapping + map->mSize > address)
|
|---|
| 967 | {
|
|---|
| 968 | break;
|
|---|
| 969 | }
|
|---|
| 970 | map = map->next;
|
|---|
| 971 | }
|
|---|
| 972 | }
|
|---|
| 973 | if(map) map->AddRef();
|
|---|
| 974 | DosLeaveCriticalSection(&globalmapcritsect);
|
|---|
| 975 | return map;
|
|---|
| 976 | }
|
|---|
| 977 | //******************************************************************************
|
|---|
| 978 | //******************************************************************************
|
|---|
| 979 | void Win32MemMap::deleteAll()
|
|---|
| 980 | {
|
|---|
| 981 | Win32MemMap *map, *nextmap;
|
|---|
| 982 | DWORD processId = GetCurrentProcessId();
|
|---|
| 983 |
|
|---|
| 984 | //delete all maps created by this process
|
|---|
| 985 | DosEnterCriticalSection(&globalmapcritsect);
|
|---|
| 986 |
|
|---|
| 987 | startdeleteviews:
|
|---|
| 988 | map = memmaps;
|
|---|
| 989 | while(map) {
|
|---|
| 990 | map->AddRef(); //make sure it doesn't get deleted
|
|---|
| 991 |
|
|---|
| 992 | //delete all views created by this process for this map
|
|---|
| 993 | Win32MemMapView::deleteViews(map);
|
|---|
| 994 |
|
|---|
| 995 | nextmap = map->next;
|
|---|
| 996 |
|
|---|
| 997 | //map->Release can delete multiple objects (duplicate memory map), so make
|
|---|
| 998 | //sure our nextmap pointer remains valid by increasing the refcount
|
|---|
| 999 | if(nextmap) nextmap->AddRef();
|
|---|
| 1000 | map->Release();
|
|---|
| 1001 |
|
|---|
| 1002 | if(nextmap && nextmap->Release() == 0) {
|
|---|
| 1003 | //oops, nextmap was just deleted and is no longer valid
|
|---|
| 1004 | //can't continue from here, so let's start again
|
|---|
| 1005 | dprintf(("oops, nextmap is invalid -> start again (1)"));
|
|---|
| 1006 | goto startdeleteviews;
|
|---|
| 1007 | }
|
|---|
| 1008 |
|
|---|
| 1009 | map = nextmap;
|
|---|
| 1010 | }
|
|---|
| 1011 | startdelete:
|
|---|
| 1012 | map = memmaps;
|
|---|
| 1013 | while(map) {
|
|---|
| 1014 | nextmap = map->next;
|
|---|
| 1015 | if(map->getProcessId() == processId)
|
|---|
| 1016 | {
|
|---|
| 1017 | //delete map can delete multiple objects (duplicate memory map), so make
|
|---|
| 1018 | //sure our nextmap pointer remains valid by increasing the refcount
|
|---|
| 1019 | if(nextmap) nextmap->AddRef();
|
|---|
| 1020 | delete map;
|
|---|
| 1021 |
|
|---|
| 1022 | if(nextmap && nextmap->Release() == 0) {
|
|---|
| 1023 | //oops, nextmap was just deleted and is no longer valid
|
|---|
| 1024 | //can't continue from here, so let's start again
|
|---|
| 1025 | dprintf(("oops, nextmap is invalid -> start again (2)"));
|
|---|
| 1026 | goto startdelete;
|
|---|
| 1027 | }
|
|---|
| 1028 | }
|
|---|
| 1029 | map = nextmap;
|
|---|
| 1030 | }
|
|---|
| 1031 | DosLeaveCriticalSection(&globalmapcritsect);
|
|---|
| 1032 | }
|
|---|
| 1033 | //******************************************************************************
|
|---|
| 1034 | // Win32MemMapView::markDirtyPages
|
|---|
| 1035 | //
|
|---|
| 1036 | // Mark pages as dirty (changed) in the write page bitmap
|
|---|
| 1037 | //
|
|---|
| 1038 | // Parameters:
|
|---|
| 1039 | //
|
|---|
| 1040 | // int startpage - start page
|
|---|
| 1041 | // int nrpages - number of pages
|
|---|
| 1042 | //
|
|---|
| 1043 | //
|
|---|
| 1044 | //******************************************************************************
|
|---|
| 1045 | void Win32MemMap::markDirtyPages(int startpage, int nrpages)
|
|---|
| 1046 | {
|
|---|
| 1047 | if(pWriteBitmap == NULL) return; //can be NULL for non-file mappings
|
|---|
| 1048 |
|
|---|
| 1049 | for(int i=startpage;i<startpage+nrpages;i++) {
|
|---|
| 1050 | set_bit(i, pWriteBitmap);
|
|---|
| 1051 | }
|
|---|
| 1052 | }
|
|---|
| 1053 | //******************************************************************************
|
|---|
| 1054 | // Win32MemMapView::clearDirtyPages
|
|---|
| 1055 | //
|
|---|
| 1056 | // Mark pages as clean in the write page bitmap
|
|---|
| 1057 | //
|
|---|
| 1058 | // Parameters:
|
|---|
| 1059 | //
|
|---|
| 1060 | // int startpage - start page
|
|---|
| 1061 | // int nrpages - number of pages
|
|---|
| 1062 | //
|
|---|
| 1063 | //
|
|---|
| 1064 | //******************************************************************************
|
|---|
| 1065 | void Win32MemMap::clearDirtyPages(int startpage, int nrpages)
|
|---|
| 1066 | {
|
|---|
| 1067 | if(pWriteBitmap == NULL) return; //can be NULL for non-file mappings
|
|---|
| 1068 |
|
|---|
| 1069 | for(int i=startpage;i<startpage+nrpages;i++) {
|
|---|
| 1070 | clear_bit(i, pWriteBitmap);
|
|---|
| 1071 | }
|
|---|
| 1072 | }
|
|---|
| 1073 | //******************************************************************************
|
|---|
| 1074 | //******************************************************************************
|
|---|