source: trunk/src/kernel32/mmap.h@ 10367

Last change on this file since 10367 was 10357, checked in by sandervl, 22 years ago

DT: Fix for memory map offset

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