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