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