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