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