[10376] | 1 | /* $Id: mmap.h,v 1.31 2004-01-11 11:52:18 sandervl Exp $ */
|
---|
[657] | 2 |
|
---|
| 3 | /*
|
---|
| 4 | * Memory mapped class
|
---|
| 5 | *
|
---|
[9946] | 6 | * Copyright 1999-2003 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
[657] | 7 | *
|
---|
| 8 | *
|
---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
| 10 | *
|
---|
| 11 | */
|
---|
| 12 | #ifndef __MMAP_H__
|
---|
| 13 | #define __MMAP_H__
|
---|
| 14 |
|
---|
| 15 | #include <vmutex.h>
|
---|
[1432] | 16 | #include "heapshared.h"
|
---|
[9946] | 17 | #include "asmutil.h"
|
---|
[657] | 18 |
|
---|
[9946] | 19 |
|
---|
[657] | 20 | #ifndef PAGE_SIZE
|
---|
[4796] | 21 | #define PAGE_SIZE 4096
|
---|
[657] | 22 | #endif
|
---|
[2264] | 23 | #ifndef PAGE_SHIFT
|
---|
[4796] | 24 | #define PAGE_SHIFT 12
|
---|
[2264] | 25 | #endif
|
---|
[657] | 26 |
|
---|
[8203] | 27 | #define MEMMAP_CRITSECTION_NAME "\\SEM32\\ODIN_MMAP.SEM"
|
---|
| 28 |
|
---|
[9946] | 29 | //commit 16 pages at once when the app accesses it
|
---|
[9824] | 30 | #define NRPAGES_TOCOMMIT 16
|
---|
[712] | 31 |
|
---|
[9911] | 32 | #define MEMMAP_ACCESS_INVALID 0
|
---|
| 33 | #define MEMMAP_ACCESS_READ 1
|
---|
| 34 | #define MEMMAP_ACCESS_WRITE 2
|
---|
| 35 | #define MEMMAP_ACCESS_EXECUTE 4
|
---|
| 36 | #define MEMMAP_ACCESS_COPYONWRITE 8
|
---|
[712] | 37 |
|
---|
[9911] | 38 | #define MMAP_FLUSHVIEW_ALL 0xFFFFFFFF
|
---|
| 39 |
|
---|
[10073] | 40 | #define MMAP_MAX_FILENAME_LENGTH 260
|
---|
| 41 |
|
---|
[9946] | 42 | typedef enum
|
---|
| 43 | {
|
---|
| 44 | PAGEVIEW_READONLY,
|
---|
| 45 | PAGEVIEW_VIEW,
|
---|
| 46 | PAGEVIEW_GUARD
|
---|
| 47 | } PAGEVIEW;
|
---|
| 48 |
|
---|
[10376] | 49 | #ifndef _DEF_PFNEXCEPTIONNOTIFY
|
---|
| 50 | #define _DEF_PFNEXCEPTIONNOTIFY
|
---|
| 51 | typedef BOOL (WIN32API * PFNEXCEPTIONNOTIFY)(LPVOID lpBase, ULONG offset, BOOL fWriteAccess, DWORD dwSize, DWORD dwUserData);
|
---|
| 52 | #endif
|
---|
| 53 |
|
---|
[712] | 54 | class Win32MemMapView;
|
---|
[1811] | 55 | class Win32PeLdrImage;
|
---|
[712] | 56 |
|
---|
| 57 | //******************************************************************************
|
---|
[9946] | 58 | //Memory mapping class
|
---|
[712] | 59 | //******************************************************************************
|
---|
[657] | 60 | class Win32MemMap
|
---|
| 61 | {
|
---|
| 62 | public:
|
---|
[9971] | 63 | Win32MemMap(HANDLE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName);
|
---|
[1811] | 64 | //Use by PE loader image class only:
|
---|
| 65 | Win32MemMap(Win32PeLdrImage *pImage, ULONG lpImageMem, ULONG size);
|
---|
[9946] | 66 | virtual ~Win32MemMap();
|
---|
[657] | 67 |
|
---|
[9911] | 68 | virtual BOOL Init(DWORD aMSize=0);
|
---|
| 69 | virtual BOOL flushView(ULONG viewaddr, ULONG offset, ULONG cbFlush);
|
---|
| 70 | virtual LPVOID mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess);
|
---|
| 71 | virtual BOOL unmapViewOfFile(LPVOID addr);
|
---|
[657] | 72 |
|
---|
[9946] | 73 | BOOL updateViewPages(ULONG offset, ULONG size, PAGEVIEW flags);
|
---|
| 74 | BOOL allocateMap();
|
---|
| 75 |
|
---|
[9971] | 76 | HANDLE getFileHandle() { return hMemFile; };
|
---|
[657] | 77 | LPSTR getMemName() { return lpszMapName; };
|
---|
[9946] | 78 | DWORD getMapSize() { return mSize; };
|
---|
[664] | 79 | DWORD getProtFlags() { return mProtFlags; };
|
---|
[9946] | 80 | void setProtFlags(DWORD dwNewFlags) { mProtFlags = dwNewFlags; };
|
---|
[712] | 81 | LPVOID getMappingAddr() { return pMapping; };
|
---|
[1432] | 82 | DWORD getProcessId() { return mProcessId;};
|
---|
[1811] | 83 | Win32PeLdrImage *getImage() { return image; };
|
---|
[657] | 84 |
|
---|
[1811] | 85 | BOOL isImageMap() { return image != NULL; };
|
---|
| 86 |
|
---|
[664] | 87 | void AddRef() { ++referenced; };
|
---|
[9946] | 88 | int Release();
|
---|
[664] | 89 |
|
---|
[9946] | 90 | void AddView() { ++nrMappings; };
|
---|
| 91 | void RemoveView() { --nrMappings; };
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | void markDirtyPages(int startpage, int nrpages);
|
---|
| 95 | void clearDirtyPages(int startpage, int nrpages);
|
---|
| 96 | BOOL isDirtyPage(int pagenr) { return test_bit(pagenr, pWriteBitmap) != 0; };
|
---|
| 97 |
|
---|
[9911] | 98 | virtual BOOL invalidatePages(ULONG offset, ULONG size);
|
---|
| 99 | virtual BOOL commitPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages = NRPAGES_TOCOMMIT);
|
---|
| 100 | virtual BOOL commitGuardPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess);
|
---|
| 101 | BOOL commitRange(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages);
|
---|
[678] | 102 |
|
---|
[657] | 103 | static Win32MemMap *findMap(LPSTR lpszName);
|
---|
[9824] | 104 | static Win32MemMap *findMapByFile(HANDLE hFile);
|
---|
[657] | 105 | static Win32MemMap *findMap(ULONG address);
|
---|
| 106 |
|
---|
[678] | 107 | //Should only be called in ExitProcess
|
---|
| 108 | static void deleteAll();
|
---|
| 109 |
|
---|
[1432] | 110 | #ifdef __DEBUG_ALLOC__
|
---|
[4796] | 111 | void *operator new(size_t size, const char *filename, size_t lineno)
|
---|
| 112 | {
|
---|
[8900] | 113 | return _smalloc(size);
|
---|
[4796] | 114 | }
|
---|
| 115 | void operator delete(void *location, const char *filename, size_t lineno)
|
---|
| 116 | {
|
---|
| 117 | free(location);
|
---|
| 118 | }
|
---|
[1432] | 119 | #else
|
---|
[4796] | 120 | void *operator new(size_t size)
|
---|
| 121 | {
|
---|
[8900] | 122 | return _smalloc(size);
|
---|
[4796] | 123 | }
|
---|
| 124 | void operator delete(void *location)
|
---|
| 125 | {
|
---|
| 126 | free(location);
|
---|
| 127 | }
|
---|
[1432] | 128 | #endif
|
---|
| 129 |
|
---|
[657] | 130 | protected:
|
---|
[9971] | 131 | HANDLE hMemFile;
|
---|
| 132 | HANDLE hOrgMemFile;
|
---|
[10073] | 133 | LPSTR lpszFileName;
|
---|
[657] | 134 | ULONG mSize;
|
---|
| 135 | ULONG mProtFlags;
|
---|
[1432] | 136 | ULONG mProcessId;
|
---|
[657] | 137 | ULONG mMapAccess;
|
---|
| 138 | LPSTR lpszMapName;
|
---|
| 139 | void *pMapping;
|
---|
| 140 |
|
---|
[9946] | 141 | char *pWriteBitmap;
|
---|
| 142 |
|
---|
[712] | 143 | ULONG nrMappings;
|
---|
| 144 |
|
---|
[664] | 145 | ULONG referenced;
|
---|
| 146 |
|
---|
[657] | 147 | VMutex mapMutex;
|
---|
| 148 |
|
---|
[1811] | 149 | Win32PeLdrImage *image;
|
---|
| 150 |
|
---|
[9946] | 151 | Win32MemMapView *views;
|
---|
| 152 |
|
---|
[657] | 153 | private:
|
---|
| 154 | static Win32MemMap *memmaps;
|
---|
[7550] | 155 | Win32MemMap *next;
|
---|
[657] | 156 | };
|
---|
[712] | 157 | //******************************************************************************
|
---|
[9946] | 158 | //Duplicate memory mapping class (duplicate map with different protection flags
|
---|
| 159 | //associated with an existing memory map)
|
---|
| 160 | //******************************************************************************
|
---|
| 161 | class Win32MemMapDup : public Win32MemMap
|
---|
| 162 | {
|
---|
| 163 | public:
|
---|
[9971] | 164 | Win32MemMapDup(Win32MemMap *parent, HANDLE hFile, ULONG size, ULONG fdwProtect, LPSTR lpszName);
|
---|
[9946] | 165 | virtual ~Win32MemMapDup();
|
---|
| 166 |
|
---|
| 167 | virtual BOOL Init(DWORD aMSize=0);
|
---|
| 168 | virtual BOOL flushView(ULONG viewaddr, ULONG offset, ULONG cbFlush);
|
---|
| 169 | virtual LPVOID mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess);
|
---|
| 170 | virtual BOOL unmapViewOfFile(LPVOID addr);
|
---|
| 171 |
|
---|
| 172 | virtual BOOL invalidatePages(ULONG offset, ULONG size);
|
---|
| 173 | virtual BOOL commitGuardPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess);
|
---|
| 174 | virtual BOOL commitPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages = NRPAGES_TOCOMMIT);
|
---|
| 175 |
|
---|
| 176 | protected:
|
---|
| 177 | Win32MemMap *parent;
|
---|
| 178 |
|
---|
[10073] | 179 | HANDLE hDupMemFile;
|
---|
[9946] | 180 | private:
|
---|
| 181 | };
|
---|
| 182 | //******************************************************************************
|
---|
[712] | 183 | //Memory mapped file View Class
|
---|
| 184 | //******************************************************************************
|
---|
| 185 | class Win32MemMapView
|
---|
| 186 | {
|
---|
| 187 | public:
|
---|
[9946] | 188 | Win32MemMapView(Win32MemMap *parent, ULONG offset, ULONG size, ULONG fdwAccess, Win32MemMap *owner = NULL);
|
---|
[712] | 189 | ~Win32MemMapView();
|
---|
[657] | 190 |
|
---|
[9946] | 191 | BOOL changePageFlags(ULONG offset, ULONG size, PAGEVIEW flags);
|
---|
| 192 |
|
---|
[712] | 193 | DWORD getAccessFlags() { return mfAccess; };
|
---|
| 194 | DWORD getSize() { return mSize; };
|
---|
| 195 | ULONG getOffset() { return mOffset; };
|
---|
[10357] | 196 | LPVOID getViewAddr() { return pMapView; };
|
---|
[712] | 197 |
|
---|
| 198 | BOOL everythingOk() { return errorState == 0; };
|
---|
| 199 |
|
---|
| 200 | Win32MemMap *getParentMap() { return mParentMap;};
|
---|
[9946] | 201 | Win32MemMap *getOwnerMap() { return mOwnerMap; };
|
---|
| 202 |
|
---|
[1432] | 203 | DWORD getProcessId() { return mProcessId;};
|
---|
[712] | 204 |
|
---|
[9946] | 205 | void markCOWPages(int startpage, int nrpages);
|
---|
| 206 | BOOL isCOWPage(int pagenr) { return (pCOWBitmap) ? (test_bit(pagenr, pCOWBitmap) != 0) : FALSE; };
|
---|
| 207 |
|
---|
[1432] | 208 | static void deleteViews(Win32MemMap *map);
|
---|
[9824] | 209 | static Win32MemMap *findMapByView(ULONG address, ULONG *offset = NULL,
|
---|
| 210 | ULONG accessType = MEMMAP_ACCESS_READ);
|
---|
[9946] | 211 | static int findViews(Win32MemMap *map, int nrViews, Win32MemMapView *viewarray[]);
|
---|
[9824] | 212 | static Win32MemMapView *findView(ULONG address);
|
---|
[712] | 213 |
|
---|
[1432] | 214 | #ifdef __DEBUG_ALLOC__
|
---|
[4796] | 215 | void *operator new(size_t size, const char *filename, size_t lineno)
|
---|
| 216 | {
|
---|
[8900] | 217 | return _smalloc(size);
|
---|
[4796] | 218 | }
|
---|
| 219 | void operator delete(void *location, const char *filename, size_t lineno)
|
---|
| 220 | {
|
---|
| 221 | free(location);
|
---|
| 222 | }
|
---|
[1432] | 223 | #else
|
---|
[4796] | 224 | void *operator new(size_t size)
|
---|
| 225 | {
|
---|
[8900] | 226 | return _smalloc(size);
|
---|
[4796] | 227 | }
|
---|
| 228 | void operator delete(void *location)
|
---|
| 229 | {
|
---|
| 230 | free(location);
|
---|
| 231 | }
|
---|
[1432] | 232 | #endif
|
---|
| 233 |
|
---|
[712] | 234 | protected:
|
---|
| 235 | ULONG mSize, errorState;
|
---|
[1432] | 236 | ULONG mProcessId;
|
---|
[712] | 237 | ULONG mfAccess, mOffset;
|
---|
[4796] | 238 | void *pMapView, *pShareViewAddr;
|
---|
[712] | 239 |
|
---|
[9946] | 240 | char *pCOWBitmap;
|
---|
| 241 |
|
---|
| 242 | //parent map object; memory map that contains the original memory map
|
---|
[712] | 243 | Win32MemMap *mParentMap;
|
---|
[9946] | 244 | //owner map object (can be NULL); duplicate memory map that created this view
|
---|
| 245 | Win32MemMap *mOwnerMap;
|
---|
[712] | 246 |
|
---|
| 247 | private:
|
---|
| 248 | static Win32MemMapView *mapviews;
|
---|
| 249 | Win32MemMapView *next;
|
---|
| 250 |
|
---|
| 251 | friend class Win32MemMap;
|
---|
| 252 | };
|
---|
| 253 | //******************************************************************************
|
---|
[10376] | 254 | //Notification memory mapped file Class
|
---|
[712] | 255 | //******************************************************************************
|
---|
[10376] | 256 | class Win32MemMapNotify
|
---|
| 257 | {
|
---|
| 258 | public:
|
---|
| 259 | Win32MemMapNotify(PFNEXCEPTIONNOTIFY pfnNotify, LPVOID lpViewaddr, ULONG size, ULONG dwUserData);
|
---|
| 260 | ~Win32MemMapNotify();
|
---|
[712] | 261 |
|
---|
[10376] | 262 | LPVOID getViewAddr() { return pMapView; };
|
---|
| 263 | DWORD getSize() { return mSize; };
|
---|
| 264 | DWORD getUserData() { return dwUserData; };
|
---|
| 265 |
|
---|
| 266 | BOOL notify(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess);
|
---|
| 267 |
|
---|
| 268 | static Win32MemMapNotify *findMapByView(ULONG address,
|
---|
| 269 | ULONG *offset,
|
---|
| 270 | ULONG accessType);
|
---|
| 271 | static Win32MemMapNotify *findView(ULONG address);
|
---|
| 272 |
|
---|
| 273 |
|
---|
| 274 | protected:
|
---|
| 275 | ULONG mSize, dwUserData;
|
---|
| 276 | LPVOID pMapView;
|
---|
| 277 | PFNEXCEPTIONNOTIFY pfnNotify;
|
---|
| 278 |
|
---|
| 279 | private:
|
---|
| 280 | static Win32MemMapNotify *mapviews;
|
---|
| 281 | Win32MemMapNotify *next;
|
---|
| 282 | };
|
---|
| 283 | //******************************************************************************
|
---|
| 284 | //******************************************************************************
|
---|
| 285 |
|
---|
[21916] | 286 | //
|
---|
| 287 | // Global DLL Data (keep it in sync with globaldata.asm!)
|
---|
| 288 | //
|
---|
[9946] | 289 | extern CRITICAL_SECTION_OS2 globalmapcritsect;
|
---|
| 290 |
|
---|
[8203] | 291 | void InitializeMemMaps();
|
---|
[10376] | 292 | void FinalizeMemMaps();
|
---|
[8203] | 293 |
|
---|
[657] | 294 | #endif //__MMAP_H__
|
---|