ReactOS  0.4.14-dev-552-g2fad488
memory.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define TOTAL_PAGES   (MAX_ADDRESS / PAGE_SIZE)
 

Typedefs

typedef VOID(FASTCALLPMEMORY_READ_HANDLER) (ULONG Address, PVOID Buffer, ULONG Size)
 
typedef BOOLEAN(FASTCALLPMEMORY_WRITE_HANDLER) (ULONG Address, PVOID Buffer, ULONG Size)
 

Functions

BOOLEAN MemInitialize (VOID)
 
VOID MemCleanup (VOID)
 
VOID MemExceptionHandler (ULONG FaultAddress, BOOLEAN Writing)
 
VOID FASTCALL EmulatorReadMemory (PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
 
VOID FASTCALL EmulatorWriteMemory (PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
 
VOID FASTCALL EmulatorCopyMemory (PFAST486_STATE State, ULONG DestAddress, ULONG SrcAddress, ULONG Size)
 
VOID EmulatorSetA20 (BOOLEAN Enabled)
 
BOOLEAN EmulatorGetA20 (VOID)
 
BOOL MemInstallFastMemoryHook (PVOID Address, ULONG Size, PMEMORY_READ_HANDLER ReadHandler, PMEMORY_WRITE_HANDLER WriteHandler)
 
BOOL MemRemoveFastMemoryHook (PVOID Address, ULONG Size)
 
BOOLEAN MemQueryMemoryZone (ULONG StartAddress, PULONG Length, PBOOLEAN Hooked)
 

Macro Definition Documentation

◆ TOTAL_PAGES

#define TOTAL_PAGES   (MAX_ADDRESS / PAGE_SIZE)

Definition at line 14 of file memory.h.

Typedef Documentation

◆ PMEMORY_READ_HANDLER

typedef VOID(FASTCALL * PMEMORY_READ_HANDLER) (ULONG Address, PVOID Buffer, ULONG Size)

Definition at line 18 of file memory.h.

◆ PMEMORY_WRITE_HANDLER

typedef BOOLEAN(FASTCALL * PMEMORY_WRITE_HANDLER) (ULONG Address, PVOID Buffer, ULONG Size)

Definition at line 26 of file memory.h.

Function Documentation

◆ EmulatorCopyMemory()

VOID FASTCALL EmulatorCopyMemory ( PFAST486_STATE  State,
ULONG  DestAddress,
ULONG  SrcAddress,
ULONG  Size 
)

Definition at line 216 of file memory.c.

217 {
218  /*
219  * Guest-to-guest memory copy
220  */
221 
222  // FIXME: This is a temporary implementation of a more useful functionality
223  // which should be a merge of EmulatorReadMemory & EmulatorWriteMemory without
224  // any local external buffer.
225  // NOTE: Process heap is by default serialized (unless one specifies it shouldn't).
226  static BYTE StaticBuffer[8192]; // Smallest static buffer we can use.
227  static PVOID HeapBuffer = NULL; // Always-growing heap buffer. Use it in case StaticBuffer is too small.
228  static ULONG HeapBufferSize = 0;
229  PVOID LocalBuffer; // Points to either StaticBuffer or HeapBuffer
230 
231  if (Size <= sizeof(StaticBuffer))
232  {
233  /* Use the static buffer */
234  LocalBuffer = StaticBuffer;
235  }
236  else if (/* sizeof(StaticBuffer) <= Size && */ Size <= HeapBufferSize)
237  {
238  /* Use the heap buffer */
239  ASSERT(HeapBufferSize > 0 && HeapBuffer != NULL);
240  LocalBuffer = HeapBuffer;
241  }
242  else // if (Size > HeapBufferSize)
243  {
244  /* Enlarge the heap buffer and use it */
245 
246  if (HeapBuffer == NULL)
247  {
248  /* First allocation */
249  LocalBuffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, Size);
250  }
251  else
252  {
253  /* Reallocation */
254  LocalBuffer = RtlReAllocateHeap(RtlGetProcessHeap(), 0 /* HEAP_GENERATE_EXCEPTIONS */, HeapBuffer, Size);
255  }
256  ASSERT(LocalBuffer != NULL); // We must succeed! TODO: Handle it more properly.
257  HeapBuffer = LocalBuffer; // HeapBuffer is now reallocated.
258  HeapBufferSize = Size;
259  }
260 
261  /* Perform memory copy */
262  EmulatorReadMemory( State, SrcAddress , LocalBuffer, Size);
263  EmulatorWriteMemory(State, DestAddress, LocalBuffer, Size);
264 
265  // if (LocalBuffer != StaticBuffer)
266  // RtlFreeHeap(RtlGetProcessHeap(), 0, LocalBuffer);
267 
268  // Note that we don't free HeapBuffer since it's an always-growing buffer.
269  // It is freed when NTVDM termiantes.
270 }
VOID FASTCALL EmulatorWriteMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
Definition: memory.c:183
NTSYSAPI PVOID WINAPI RtlReAllocateHeap(HANDLE, ULONG, PVOID, SIZE_T)
Definition: heap.c:2561
smooth NULL
Definition: ftsmooth.c:416
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
unsigned char BYTE
Definition: mem.h:68
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
unsigned int ULONG
Definition: retypes.h:1
VOID FASTCALL EmulatorReadMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
Definition: memory.c:139

Referenced by VidBiosScrollWindow().

◆ EmulatorGetA20()

BOOLEAN EmulatorGetA20 ( VOID  )

Definition at line 277 of file memory.c.

278 {
279  return A20Line;
280 }
static BOOLEAN A20Line
Definition: memory.c:41

Referenced by PS2WriteControl(), and XmsBopProcedure().

◆ EmulatorReadMemory()

VOID FASTCALL EmulatorReadMemory ( PFAST486_STATE  State,
ULONG  Address,
PVOID  Buffer,
ULONG  Size 
)

Definition at line 139 of file memory.c.

140 {
141  ULONG i, Offset, Length;
142  ULONG FirstPage, LastPage;
143 
145 
146  /* Mirror 0x000FFFF0 at 0xFFFFFFF0 */
147  if (Address >= 0xFFFFFFF0) Address -= 0xFFF00000;
148 
149  /* If the A20 line is disabled, mask bit 20 */
150  if (!A20Line) Address &= ~(1 << 20);
151 
152  if ((Address + Size - 1) >= MAX_ADDRESS)
153  {
154  ULONG ExtraStart = (Address < MAX_ADDRESS) ? MAX_ADDRESS - Address : 0;
155 
156  /* Fill the memory that was above the limit with 0xFF */
157  RtlFillMemory((PVOID)((ULONG_PTR)Buffer + ExtraStart), Size - ExtraStart, 0xFF);
158 
160  else return;
161  }
162 
163  FirstPage = Address >> 12;
164  LastPage = (Address + Size - 1) >> 12;
165 
166  if (FirstPage == LastPage)
167  {
168  ReadPage(PageTable[FirstPage], Address, Buffer, Size);
169  }
170  else
171  {
172  for (i = FirstPage; i <= LastPage; i++)
173  {
174  Offset = (i == FirstPage) ? (Address & (PAGE_SIZE - 1)) : 0;
175  Length = ((i == LastPage) ? (Address + Size - (LastPage << 12)) : PAGE_SIZE) - Offset;
176 
177  ReadPage(PageTable[i], (i << 12) + Offset, Buffer, Length);
179  }
180  }
181 }
static PMEM_HOOK PageTable[TOTAL_PAGES]
Definition: memory.c:40
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:323
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define MAX_ADDRESS
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
static WCHAR Address[46]
Definition: ping.c:68
Definition: bufpool.h:45
void * PVOID
Definition: retypes.h:9
static VOID ReadPage(PMEM_HOOK Hook, ULONG Address, PVOID Buffer, ULONG Size)
Definition: memory.c:115
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define PAGE_SIZE
Definition: env_spec_w32.h:49
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
unsigned int ULONG
Definition: retypes.h:1
static BOOLEAN A20Line
Definition: memory.c:41
#define RtlFillMemory(Dest, Length, Fill)
Definition: winternl.h:593

Referenced by CpuInitialize(), DmaRequest(), DosWriteFile(), EmulatorCopyMemory(), PaintMouseCursor(), VidBiosDrawGlyph(), VidBiosVideoService(), and WriteDisk().

◆ EmulatorSetA20()

VOID EmulatorSetA20 ( BOOLEAN  Enabled)

Definition at line 272 of file memory.c.

273 {
274  A20Line = Enabled;
275 }
static BOOLEAN A20Line
Definition: memory.c:41

Referenced by PS2WriteData(), XmsLocalDisableA20(), and XmsLocalEnableA20().

◆ EmulatorWriteMemory()

VOID FASTCALL EmulatorWriteMemory ( PFAST486_STATE  State,
ULONG  Address,
PVOID  Buffer,
ULONG  Size 
)

Definition at line 183 of file memory.c.

184 {
185  ULONG i, Offset, Length;
186  ULONG FirstPage, LastPage;
187 
189 
190  /* If the A20 line is disabled, mask bit 20 */
191  if (!A20Line) Address &= ~(1 << 20);
192 
193  if (Address >= MAX_ADDRESS) return;
195 
196  FirstPage = Address >> 12;
197  LastPage = (Address + Size - 1) >> 12;
198 
199  if (FirstPage == LastPage)
200  {
201  WritePage(PageTable[FirstPage], Address, Buffer, Size);
202  }
203  else
204  {
205  for (i = FirstPage; i <= LastPage; i++)
206  {
207  Offset = (i == FirstPage) ? (Address & (PAGE_SIZE - 1)) : 0;
208  Length = ((i == LastPage) ? (Address + Size - (LastPage << 12)) : PAGE_SIZE) - Offset;
209 
210  WritePage(PageTable[i], (i << 12) + Offset, Buffer, Length);
212  }
213  }
214 }
static PMEM_HOOK PageTable[TOTAL_PAGES]
Definition: memory.c:40
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:323
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define MAX_ADDRESS
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
static WCHAR Address[46]
Definition: ping.c:68
Definition: bufpool.h:45
void * PVOID
Definition: retypes.h:9
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
static VOID WritePage(PMEM_HOOK Hook, ULONG Address, PVOID Buffer, ULONG Size)
Definition: memory.c:126
#define PAGE_SIZE
Definition: env_spec_w32.h:49
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
#define min(a, b)
Definition: monoChain.cc:55
unsigned int ULONG
Definition: retypes.h:1
static BOOLEAN A20Line
Definition: memory.c:41

Referenced by CpuInitialize(), DmaRequest(), DosReadFile(), EmulatorCopyMemory(), EraseMouseCursor(), PaintMouseCursor(), ReadDisk(), VidBiosClearScreen(), VidBiosDrawGlyph(), and VidBiosScrollWindow().

◆ MemCleanup()

VOID MemCleanup ( VOID  )

Definition at line 780 of file memory.c.

781 {
784  PLIST_ENTRY Pointer;
785 
786  while (!IsListEmpty(&HookList))
787  {
788  Pointer = RemoveHeadList(&HookList);
789  RtlFreeHeap(RtlGetProcessHeap(), 0, CONTAINING_RECORD(Pointer, MEM_HOOK, Entry));
790  }
791 
792  /* Decommit the VDM memory */
794  &BaseAddress,
795  &MemorySize,
796 #ifndef STANDALONE
798 #else
800 #endif
801  );
802  if (!NT_SUCCESS(Status))
803  {
804  DPRINT1("NTVDM: Failed to decommit VDM memory, Status 0x%08lx\n", Status);
805  }
806 }
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
#define STANDALONE
Definition: testlist.c:3
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
#define MAX_ADDRESS
#define MEM_DECOMMIT
Definition: nt_native.h:1315
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
static LIST_ENTRY HookList
Definition: memory.c:39
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
#define NtCurrentProcess()
Definition: nt_native.h:1657
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static CONST DWORD MemorySize[]
Definition: svga.c:32
Definition: typedefs.h:117
Status
Definition: gdiplustypes.h:24
ULONG_PTR SIZE_T
Definition: typedefs.h:78
#define DPRINT1
Definition: precomp.h:8
#define MEM_RELEASE
Definition: nt_native.h:1316
base of all file and directory entries
Definition: entries.h:82
NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *UBaseAddress, IN PSIZE_T URegionSize, IN ULONG FreeType)
Definition: virtual.c:5090

Referenced by EmulatorCleanup().

◆ MemExceptionHandler()

VOID MemExceptionHandler ( ULONG  FaultAddress,
BOOLEAN  Writing 
)

Definition at line 283 of file memory.c.

284 {
285  PMEM_HOOK Hook = PageTable[FaultAddress >> 12];
286  DPRINT("The memory at 0x%08X could not be %s.\n", FaultAddress, Writing ? "written" : "read");
287 
288  /* Exceptions are only supposed to happen when using VDD-style memory hooks */
289  ASSERT(FaultAddress < MAX_ADDRESS && Hook != NULL && Hook->hVdd != NULL);
290 
291  /* Call the VDD handler */
292  Hook->VddHandler(REAL_TO_PHYS(FaultAddress), (ULONG)Writing);
293 }
static PMEM_HOOK PageTable[TOTAL_PAGES]
Definition: memory.c:40
#define REAL_TO_PHYS(ptr)
Definition: emulator.h:33
smooth NULL
Definition: ftsmooth.c:416
void DPRINT(...)
Definition: polytest.cpp:61
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
unsigned int ULONG
Definition: retypes.h:1
PVDD_MEMORY_HANDLER VddHandler
Definition: memory.c:29
HANDLE hVdd
Definition: testvdd.c:87

Referenced by CpuExceptionFilter().

◆ MemInitialize()

BOOLEAN MemInitialize ( VOID  )

Definition at line 720 of file memory.c.

721 {
723  SIZE_T MemorySize = MAX_ADDRESS; // See: kernel32/client/vdm.c!BaseGetVdmConfigInfo
724 
726 
727 #ifndef STANDALONE
728 
729  /*
730  * The reserved region starts from the very first page.
731  * We need to commit the reserved first 16 MB virtual address.
732  *
733  * NOTE: NULL has another signification for NtAllocateVirtualMemory.
734  */
735  BaseAddress = (PVOID)1;
736 
737  /*
738  * Since to get NULL, we allocated from 0x1, account for this.
739  * See also: kernel32/client/proc.c!CreateProcessInternalW
740  */
741  MemorySize -= 1;
742 
743 #else
744 
745  /* Allocate it anywhere */
746  BaseAddress = NULL;
747 
748 #endif
749 
751  &BaseAddress,
752  0,
753  &MemorySize,
754 #ifndef STANDALONE
755  MEM_COMMIT,
756 #else
758 #endif
760  if (!NT_SUCCESS(Status))
761  {
762  wprintf(L"FATAL: Failed to commit VDM memory, Status 0x%08lx\n", Status);
763  return FALSE;
764  }
765 
766 #ifndef STANDALONE
767  ASSERT(BaseAddress == NULL);
768 #endif
769 
770  /*
771  * For diagnostics purposes, we fill the memory with INT 0x03 codes
772  * so that if a program wants to execute random code in memory, we can
773  * retrieve the exact CS:IP where the problem happens.
774  */
776  return TRUE;
777 }
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
#define wprintf(...)
Definition: whoami.c:18
#define MEM_COMMIT
Definition: nt_native.h:1313
#define STANDALONE
Definition: testlist.c:3
#define MAX_ADDRESS
#define MEM_RESERVE
Definition: nt_native.h:1314
smooth NULL
Definition: ftsmooth.c:416
void * PVOID
Definition: retypes.h:9
static LIST_ENTRY HookList
Definition: memory.c:39
#define NtCurrentProcess()
Definition: nt_native.h:1657
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
static const WCHAR L[]
Definition: oid.c:1250
static CONST DWORD MemorySize[]
Definition: svga.c:32
Status
Definition: gdiplustypes.h:24
ULONG_PTR SIZE_T
Definition: typedefs.h:78
NTSTATUS NTAPI NtAllocateVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UBaseAddress, IN ULONG_PTR ZeroBits, IN OUT PSIZE_T URegionSize, IN ULONG AllocationType, IN ULONG Protect)
Definition: virtual.c:4367
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
#define RtlFillMemory(Dest, Length, Fill)
Definition: winternl.h:593

Referenced by EmulatorInitialize().

◆ MemInstallFastMemoryHook()

BOOL MemInstallFastMemoryHook ( PVOID  Address,
ULONG  Size,
PMEMORY_READ_HANDLER  ReadHandler,
PMEMORY_WRITE_HANDLER  WriteHandler 
)

Definition at line 296 of file memory.c.

300 {
301  PMEM_HOOK Hook;
302  ULONG i;
303  ULONG FirstPage = (ULONG_PTR)Address >> 12;
304  ULONG LastPage = ((ULONG_PTR)Address + Size - 1) >> 12;
305  PLIST_ENTRY Pointer;
306 
307  /* Make sure none of these pages are already allocated */
308  for (i = FirstPage; i <= LastPage; i++)
309  {
310  if (PageTable[i] != NULL) return FALSE;
311  }
312 
313  for (Pointer = HookList.Flink; Pointer != &HookList; Pointer = Pointer->Flink)
314  {
315  Hook = CONTAINING_RECORD(Pointer, MEM_HOOK, Entry);
316 
317  if (Hook->hVdd == NULL
318  && Hook->FastReadHandler == ReadHandler
319  && Hook->FastWriteHandler == WriteHandler)
320  {
321  break;
322  }
323  }
324 
325  if (Pointer == &HookList)
326  {
327  /* Create and initialize a new hook entry... */
328  Hook = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(*Hook));
329  if (Hook == NULL) return FALSE;
330 
331  Hook->hVdd = NULL;
332  Hook->Count = 0;
333  Hook->FastReadHandler = ReadHandler;
334  Hook->FastWriteHandler = WriteHandler;
335 
336  /* ... and add it to the list of hooks */
337  InsertTailList(&HookList, &Hook->Entry);
338  }
339 
340  /* Increase the number of pages this hook has */
341  Hook->Count += LastPage - FirstPage + 1;
342 
343  /* Add the hook entry to the page table */
344  for (i = FirstPage; i <= LastPage; i++) PageTable[i] = Hook;
345 
346  return TRUE;
347 }
#define TRUE
Definition: types.h:120
static PMEM_HOOK PageTable[TOTAL_PAGES]
Definition: memory.c:40
ULONG Count
Definition: memory.c:25
LIST_ENTRY Entry
Definition: memory.c:23
#define InsertTailList(ListHead, Entry)
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
static LIST_ENTRY HookList
Definition: memory.c:39
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
HANDLE hVdd
Definition: memory.c:24
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
Definition: typedefs.h:117
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
PMEMORY_READ_HANDLER FastReadHandler
Definition: memory.c:33
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101
PMEMORY_WRITE_HANDLER FastWriteHandler
Definition: memory.c:34
base of all file and directory entries
Definition: entries.h:82

Referenced by EmsDrvInitialize(), VgaWriteGc(), VgaWritePort(), and WriteProtectRom().

◆ MemQueryMemoryZone()

BOOLEAN MemQueryMemoryZone ( ULONG  StartAddress,
PULONG  Length,
PBOOLEAN  Hooked 
)

Definition at line 378 of file memory.c.

379 {
380  ULONG Page = StartAddress >> 12;
381  if (Page >= TOTAL_PAGES) return FALSE;
382 
383  *Length = 0;
384  *Hooked = PageTable[Page] != NULL;
385 
386  while (Page < TOTAL_PAGES && (PageTable[Page] != NULL) == *Hooked)
387  {
388  *Length += PAGE_SIZE;
389  Page++;
390  }
391 
392  return TRUE;
393 }
#define TRUE
Definition: types.h:120
static PMEM_HOOK PageTable[TOTAL_PAGES]
Definition: memory.c:40
smooth NULL
Definition: ftsmooth.c:416
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define TOTAL_PAGES
Definition: memory.h:14
unsigned int ULONG
Definition: retypes.h:1

Referenced by BiosMiscService().

◆ MemRemoveFastMemoryHook()

BOOL MemRemoveFastMemoryHook ( PVOID  Address,
ULONG  Size 
)

Definition at line 350 of file memory.c.

351 {
352  PMEM_HOOK Hook;
353  ULONG i;
354  ULONG FirstPage = (ULONG_PTR)Address >> 12;
355  ULONG LastPage = ((ULONG_PTR)Address + Size - 1) >> 12;
356 
357  if (Size == 0) return FALSE;
358 
359  for (i = FirstPage; i <= LastPage; i++)
360  {
361  Hook = PageTable[i];
362  if (Hook == NULL || Hook->hVdd != NULL) continue;
363 
364  if (--Hook->Count == 0)
365  {
366  /* This hook has no more pages */
367  RemoveEntryList(&Hook->Entry);
368  RtlFreeHeap(RtlGetProcessHeap(), 0, Hook);
369  }
370 
371  PageTable[i] = NULL;
372  }
373 
374  return TRUE;
375 }
#define TRUE
Definition: types.h:120
static PMEM_HOOK PageTable[TOTAL_PAGES]
Definition: memory.c:40
ULONG Count
Definition: memory.c:25
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
LIST_ENTRY Entry
Definition: memory.c:23
FORCEINLINE BOOLEAN RemoveEntryList(_In_ PLIST_ENTRY Entry)
Definition: rtlfuncs.h:105
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
HANDLE hVdd
Definition: memory.c:24
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101

Referenced by EmsDrvCleanup(), VgaCleanup(), VgaWriteGc(), VgaWritePort(), and WriteUnProtectRom().