1 | /* $Id: mmap.h,v 1.31 2004-01-11 11:52:18 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 | #define MMAP_MAX_FILENAME_LENGTH 260
|
---|
41 |
|
---|
42 | typedef enum
|
---|
43 | {
|
---|
44 | PAGEVIEW_READONLY,
|
---|
45 | PAGEVIEW_VIEW,
|
---|
46 | PAGEVIEW_GUARD
|
---|
47 | } PAGEVIEW;
|
---|
48 |
|
---|
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 |
|
---|
54 | class Win32MemMapView;
|
---|
55 | class Win32PeLdrImage;
|
---|
56 |
|
---|
57 | //******************************************************************************
|
---|
58 | //Memory mapping class
|
---|
59 | //******************************************************************************
|
---|
60 | class Win32MemMap
|
---|
61 | {
|
---|
62 | public:
|
---|
63 | Win32MemMap(HANDLE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName);
|
---|
64 | //Use by PE loader image class only:
|
---|
65 | Win32MemMap(Win32PeLdrImage *pImage, ULONG lpImageMem, ULONG size);
|
---|
66 | virtual ~Win32MemMap();
|
---|
67 |
|
---|
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);
|
---|
72 |
|
---|
73 | BOOL updateViewPages(ULONG offset, ULONG size, PAGEVIEW flags);
|
---|
74 | BOOL allocateMap();
|
---|
75 |
|
---|
76 | HANDLE getFileHandle() { return hMemFile; };
|
---|
77 | LPSTR getMemName() { return lpszMapName; };
|
---|
78 | DWORD getMapSize() { return mSize; };
|
---|
79 | DWORD getProtFlags() { return mProtFlags; };
|
---|
80 | void setProtFlags(DWORD dwNewFlags) { mProtFlags = dwNewFlags; };
|
---|
81 | LPVOID getMappingAddr() { return pMapping; };
|
---|
82 | DWORD getProcessId() { return mProcessId;};
|
---|
83 | Win32PeLdrImage *getImage() { return image; };
|
---|
84 |
|
---|
85 | BOOL isImageMap() { return image != NULL; };
|
---|
86 |
|
---|
87 | void AddRef() { ++referenced; };
|
---|
88 | int Release();
|
---|
89 |
|
---|
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 |
|
---|
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);
|
---|
102 |
|
---|
103 | static Win32MemMap *findMap(LPSTR lpszName);
|
---|
104 | static Win32MemMap *findMapByFile(HANDLE hFile);
|
---|
105 | static Win32MemMap *findMap(ULONG address);
|
---|
106 |
|
---|
107 | //Should only be called in ExitProcess
|
---|
108 | static void deleteAll();
|
---|
109 |
|
---|
110 | #ifdef __DEBUG_ALLOC__
|
---|
111 | void *operator new(size_t size, const char *filename, size_t lineno)
|
---|
112 | {
|
---|
113 | return _smalloc(size);
|
---|
114 | }
|
---|
115 | void operator delete(void *location, const char *filename, size_t lineno)
|
---|
116 | {
|
---|
117 | free(location);
|
---|
118 | }
|
---|
119 | #else
|
---|
120 | void *operator new(size_t size)
|
---|
121 | {
|
---|
122 | return _smalloc(size);
|
---|
123 | }
|
---|
124 | void operator delete(void *location)
|
---|
125 | {
|
---|
126 | free(location);
|
---|
127 | }
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | protected:
|
---|
131 | HANDLE hMemFile;
|
---|
132 | HANDLE hOrgMemFile;
|
---|
133 | LPSTR lpszFileName;
|
---|
134 | ULONG mSize;
|
---|
135 | ULONG mProtFlags;
|
---|
136 | ULONG mProcessId;
|
---|
137 | ULONG mMapAccess;
|
---|
138 | LPSTR lpszMapName;
|
---|
139 | void *pMapping;
|
---|
140 |
|
---|
141 | char *pWriteBitmap;
|
---|
142 |
|
---|
143 | ULONG nrMappings;
|
---|
144 |
|
---|
145 | ULONG referenced;
|
---|
146 |
|
---|
147 | VMutex mapMutex;
|
---|
148 |
|
---|
149 | Win32PeLdrImage *image;
|
---|
150 |
|
---|
151 | Win32MemMapView *views;
|
---|
152 |
|
---|
153 | private:
|
---|
154 | static Win32MemMap *memmaps;
|
---|
155 | Win32MemMap *next;
|
---|
156 | };
|
---|
157 | //******************************************************************************
|
---|
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:
|
---|
164 | Win32MemMapDup(Win32MemMap *parent, HANDLE hFile, ULONG size, ULONG fdwProtect, LPSTR lpszName);
|
---|
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 |
|
---|
179 | HANDLE hDupMemFile;
|
---|
180 | private:
|
---|
181 | };
|
---|
182 | //******************************************************************************
|
---|
183 | //Memory mapped file View Class
|
---|
184 | //******************************************************************************
|
---|
185 | class Win32MemMapView
|
---|
186 | {
|
---|
187 | public:
|
---|
188 | Win32MemMapView(Win32MemMap *parent, ULONG offset, ULONG size, ULONG fdwAccess, Win32MemMap *owner = NULL);
|
---|
189 | ~Win32MemMapView();
|
---|
190 |
|
---|
191 | BOOL changePageFlags(ULONG offset, ULONG size, PAGEVIEW flags);
|
---|
192 |
|
---|
193 | DWORD getAccessFlags() { return mfAccess; };
|
---|
194 | DWORD getSize() { return mSize; };
|
---|
195 | ULONG getOffset() { return mOffset; };
|
---|
196 | LPVOID getViewAddr() { return pMapView; };
|
---|
197 |
|
---|
198 | BOOL everythingOk() { return errorState == 0; };
|
---|
199 |
|
---|
200 | Win32MemMap *getParentMap() { return mParentMap;};
|
---|
201 | Win32MemMap *getOwnerMap() { return mOwnerMap; };
|
---|
202 |
|
---|
203 | DWORD getProcessId() { return mProcessId;};
|
---|
204 |
|
---|
205 | void markCOWPages(int startpage, int nrpages);
|
---|
206 | BOOL isCOWPage(int pagenr) { return (pCOWBitmap) ? (test_bit(pagenr, pCOWBitmap) != 0) : FALSE; };
|
---|
207 |
|
---|
208 | static void deleteViews(Win32MemMap *map);
|
---|
209 | static Win32MemMap *findMapByView(ULONG address, ULONG *offset = NULL,
|
---|
210 | ULONG accessType = MEMMAP_ACCESS_READ);
|
---|
211 | static int findViews(Win32MemMap *map, int nrViews, Win32MemMapView *viewarray[]);
|
---|
212 | static Win32MemMapView *findView(ULONG address);
|
---|
213 |
|
---|
214 | #ifdef __DEBUG_ALLOC__
|
---|
215 | void *operator new(size_t size, const char *filename, size_t lineno)
|
---|
216 | {
|
---|
217 | return _smalloc(size);
|
---|
218 | }
|
---|
219 | void operator delete(void *location, const char *filename, size_t lineno)
|
---|
220 | {
|
---|
221 | free(location);
|
---|
222 | }
|
---|
223 | #else
|
---|
224 | void *operator new(size_t size)
|
---|
225 | {
|
---|
226 | return _smalloc(size);
|
---|
227 | }
|
---|
228 | void operator delete(void *location)
|
---|
229 | {
|
---|
230 | free(location);
|
---|
231 | }
|
---|
232 | #endif
|
---|
233 |
|
---|
234 | protected:
|
---|
235 | ULONG mSize, errorState;
|
---|
236 | ULONG mProcessId;
|
---|
237 | ULONG mfAccess, mOffset;
|
---|
238 | void *pMapView, *pShareViewAddr;
|
---|
239 |
|
---|
240 | char *pCOWBitmap;
|
---|
241 |
|
---|
242 | //parent map object; memory map that contains the original memory map
|
---|
243 | Win32MemMap *mParentMap;
|
---|
244 | //owner map object (can be NULL); duplicate memory map that created this view
|
---|
245 | Win32MemMap *mOwnerMap;
|
---|
246 |
|
---|
247 | private:
|
---|
248 | static Win32MemMapView *mapviews;
|
---|
249 | Win32MemMapView *next;
|
---|
250 |
|
---|
251 | friend class Win32MemMap;
|
---|
252 | };
|
---|
253 | //******************************************************************************
|
---|
254 | //Notification memory mapped file Class
|
---|
255 | //******************************************************************************
|
---|
256 | class Win32MemMapNotify
|
---|
257 | {
|
---|
258 | public:
|
---|
259 | Win32MemMapNotify(PFNEXCEPTIONNOTIFY pfnNotify, LPVOID lpViewaddr, ULONG size, ULONG dwUserData);
|
---|
260 | ~Win32MemMapNotify();
|
---|
261 |
|
---|
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 |
|
---|
286 | //
|
---|
287 | // Global DLL Data (keep it in sync with globaldata.asm!)
|
---|
288 | //
|
---|
289 | extern CRITICAL_SECTION_OS2 globalmapcritsect;
|
---|
290 |
|
---|
291 | void InitializeMemMaps();
|
---|
292 | void FinalizeMemMaps();
|
---|
293 |
|
---|
294 | #endif //__MMAP_H__
|
---|