HyperDbg Debugger
Loading...
Searching...
No Matches
MemoryMapper.h File Reference

Go to the source code of this file.

Classes

struct  _PAGE_ENTRY
 Page Entries. More...
 
struct  _MEMORY_MAPPER_ADDRESSES
 Memory mapper PTE and reserved virtual address. More...
 

Macros

#define PAGE_4KB_OFFSET   ((UINT64)(1 << 12) - 1)
 
#define PAGE_2MB_OFFSET   ((UINT64)(1 << 21) - 1)
 
#define PAGE_4MB_OFFSET   ((UINT64)(1 << 22) - 1)
 
#define PAGE_1GB_OFFSET   ((UINT64)(1 << 30) - 1)
 

Typedefs

typedef enum _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ
 Memory wrapper for reading safe from the memory.
 
typedef enum _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE
 Memory wrapper for writing safe into the memory.
 
typedef struct _PAGE_ENTRY PAGE_ENTRY
 Page Entries.
 
typedef struct _PAGE_ENTRYPPAGE_ENTRY
 
typedef struct _MEMORY_MAPPER_ADDRESSES MEMORY_MAPPER_ADDRESSES
 Memory mapper PTE and reserved virtual address.
 
typedef struct _MEMORY_MAPPER_ADDRESSESPMEMORY_MAPPER_ADDRESSES
 

Enumerations

enum  _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ { MEMORY_MAPPER_WRAPPER_READ_PHYSICAL_MEMORY , MEMORY_MAPPER_WRAPPER_READ_VIRTUAL_MEMORY }
 Memory wrapper for reading safe from the memory. More...
 
enum  _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE { MEMORY_MAPPER_WRAPPER_WRITE_PHYSICAL_MEMORY , MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_SAFE , MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_UNSAFE }
 Memory wrapper for writing safe into the memory. More...
 

Functions

VOID MemoryMapperInitialize ()
 Initialize the Memory Mapper.
 
VOID MemoryMapperUninitialize ()
 uninitialize the Memory Mapper
 
BOOLEAN MemoryMapperCheckIfPageIsPresentByCr3 (_In_ PVOID Va, _In_ CR3_TYPE TargetCr3)
 
VOID MemoryMapperMapPhysicalAddressToPte (_In_ PHYSICAL_ADDRESS PhysicalAddress, _In_ PVOID TargetProcessVirtualAddress, _In_ CR3_TYPE TargetProcessKernelCr3)
 

Macro Definition Documentation

◆ PAGE_1GB_OFFSET

#define PAGE_1GB_OFFSET   ((UINT64)(1 << 30) - 1)

◆ PAGE_2MB_OFFSET

#define PAGE_2MB_OFFSET   ((UINT64)(1 << 21) - 1)

◆ PAGE_4KB_OFFSET

#define PAGE_4KB_OFFSET   ((UINT64)(1 << 12) - 1)

◆ PAGE_4MB_OFFSET

#define PAGE_4MB_OFFSET   ((UINT64)(1 << 22) - 1)

Typedef Documentation

◆ MEMORY_MAPPER_ADDRESSES

Memory mapper PTE and reserved virtual address.

Memory mapper details for each core, contains PTE Virtual Address, Actual Kernel Virtual Address

◆ MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ

Memory wrapper for reading safe from the memory.

◆ MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE

Memory wrapper for writing safe into the memory.

◆ PAGE_ENTRY

typedef struct _PAGE_ENTRY PAGE_ENTRY

Page Entries.

◆ PMEMORY_MAPPER_ADDRESSES

◆ PPAGE_ENTRY

typedef struct _PAGE_ENTRY * PPAGE_ENTRY

Enumeration Type Documentation

◆ _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ

Memory wrapper for reading safe from the memory.

Enumerator
MEMORY_MAPPER_WRAPPER_READ_PHYSICAL_MEMORY 
MEMORY_MAPPER_WRAPPER_READ_VIRTUAL_MEMORY 
35{
enum _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ MEMORY_MAPPER_WRAPPER_FOR_MEMORY_READ
Memory wrapper for reading safe from the memory.
@ MEMORY_MAPPER_WRAPPER_READ_VIRTUAL_MEMORY
Definition MemoryMapper.h:37
@ MEMORY_MAPPER_WRAPPER_READ_PHYSICAL_MEMORY
Definition MemoryMapper.h:36

◆ _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE

Memory wrapper for writing safe into the memory.

Enumerator
MEMORY_MAPPER_WRAPPER_WRITE_PHYSICAL_MEMORY 
MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_SAFE 
MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_UNSAFE 
45{
49
enum _MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE MEMORY_MAPPER_WRAPPER_FOR_MEMORY_WRITE
Memory wrapper for writing safe into the memory.
@ MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_SAFE
Definition MemoryMapper.h:47
@ MEMORY_MAPPER_WRAPPER_WRITE_PHYSICAL_MEMORY
Definition MemoryMapper.h:46
@ MEMORY_MAPPER_WRAPPER_WRITE_VIRTUAL_MEMORY_UNSAFE
Definition MemoryMapper.h:48

Function Documentation

◆ MemoryMapperCheckIfPageIsPresentByCr3()

BOOLEAN MemoryMapperCheckIfPageIsPresentByCr3 ( _In_ PVOID Va,
_In_ CR3_TYPE TargetCr3 )

◆ MemoryMapperInitialize()

VOID MemoryMapperInitialize ( )

Initialize the Memory Mapper.

This function should be called in vmx non-root in a IRQL <= APC_LEVEL

Returns
VOID
662{
663 UINT64 TempPte;
664 ULONG ProcessorsCount;
665
666 ProcessorsCount = KeQueryActiveProcessorCount(0);
667
668 //
669 // *** Reserve the address for all cores (read pte and va) ***
670 //
671
672 if (g_MemoryMapper != NULL)
673 {
674 //
675 // It's already initialized
676 //
677 return;
678 }
679
680 //
681 // Allocate the memory buffer structure
682 //
684
685 //
686 // Set the core's id and initialize memory mapper
687 //
688 for (size_t i = 0; i < ProcessorsCount; i++)
689 {
690 //
691 // *** Initialize memory mapper for each core ***
692 //
693
694 //
695 // Initial and reserve for read operations
696 //
699
700 //
701 // Initial and reserve for write operations
702 //
705 }
706}
unsigned __int64 UINT64
Definition BasicTypes.h:21
unsigned long ULONG
Definition BasicTypes.h:37
MEMORY_MAPPER_ADDRESSES * g_MemoryMapper
Save the state of memory mapper.
Definition GlobalVariables.h:44
PVOID PlatformMemAllocateZeroedNonPagedPool(SIZE_T NumberOfBytes)
Allocate a non-paged buffer (zeroed)
Definition Mem.c:69
_Use_decl_annotations_ PVOID MemoryMapperMapPageAndGetPte(PUINT64 PteAddress)
This function MAPs one resreved page (4096) and returns its virtual adrresss and also PTE virtual add...
Definition MemoryMapper.c:633
Memory mapper PTE and reserved virtual address.
Definition MemoryMapper.h:103
UINT64 PteVirtualAddressForRead
Definition MemoryMapper.h:104
UINT64 VirualAddressForRead
Definition MemoryMapper.h:105
UINT64 VirualAddressForWrite
Definition MemoryMapper.h:108
UINT64 PteVirtualAddressForWrite
Definition MemoryMapper.h:107

◆ MemoryMapperMapPhysicalAddressToPte()

VOID MemoryMapperMapPhysicalAddressToPte ( _In_ PHYSICAL_ADDRESS PhysicalAddress,
_In_ PVOID TargetProcessVirtualAddress,
_In_ CR3_TYPE TargetProcessKernelCr3 )

◆ MemoryMapperUninitialize()

VOID MemoryMapperUninitialize ( )

uninitialize the Memory Mapper

This function should be called in vmx non-root in a IRQL <= APC_LEVEL

Returns
VOID
717{
718 ULONG ProcessorsCount = KeQueryActiveProcessorCount(0);
719
720 for (size_t i = 0; i < ProcessorsCount; i++)
721 {
722 //
723 // Unmap and free the reserved buffer
724 //
725 if (g_MemoryMapper[i].VirualAddressForRead != NULL64_ZERO)
726 {
727 MemoryMapperUnmapReservedPageRange((PVOID)g_MemoryMapper[i].VirualAddressForRead);
728 }
729
730 if (g_MemoryMapper[i].VirualAddressForWrite != NULL64_ZERO)
731 {
732 MemoryMapperUnmapReservedPageRange((PVOID)g_MemoryMapper[i].VirualAddressForWrite);
733 }
734
737
740 }
741
742 //
743 // Set the g_MemoryMapper to null
744 //
746}
#define NULL64_ZERO
Definition BasicTypes.h:52
_Use_decl_annotations_ VOID MemoryMapperUnmapReservedPageRange(PVOID VirtualAddress)
This function frees the memory that was previously allocated from system range (without physically al...
Definition MemoryMapper.c:591
NULL()
Definition test-case-generator.py:530