HyperPlatform Programmer's Reference
Classes | Typedefs | Functions | Variables
util.cpp File Reference

Implements primitive utility functions. More...

#include "util.h"
#include <intrin.h>
#include "asm.h"
#include "common.h"
#include "log.h"
#include "util_page_constants.h"
Include dependency graph for util.cpp:

Go to the source code of this file.

Classes

struct  LdrDataTableEntry
 

Typedefs

using RtlPcToFileHeaderType = decltype(RtlPcToFileHeader)
 
using MmAllocateContiguousNodeMemoryType = decltype(MmAllocateContiguousNodeMemory)
 

Functions

NTKERNELAPI PVOID NTAPI RtlPcToFileHeader (_In_ PVOID PcValue, _Out_ PVOID *BaseOfImage)
 
NTKERNELAPI _Post_writable_byte_size_ (NumberOfBytes)) PVOID MmAllocateContiguousNodeMemory(_In_ SIZE_T NumberOfBytes
 
static NTSTATUS UtilpInitializePageTableVariables ()
 
static NTSTATUS UtilpInitializeRtlPcToFileHeader (_In_ PDRIVER_OBJECT driver_object)
 
static PVOID NTAPI UtilpUnsafePcToFileHeader (_In_ PVOID pc_value, _Out_ PVOID *base_of_image)
 
static NTSTATUS UtilpInitializePhysicalMemoryRanges ()
 
static PhysicalMemoryDescriptorUtilpBuildPhysicalMemoryRanges ()
 
static bool UtilpIsCanonicalFormAddress (_In_ void *address)
 
static HardwarePte * UtilpAddressToPxe (_In_ const void *address)
 
static HardwarePte * UtilpAddressToPpe (_In_ const void *address)
 
static HardwarePte * UtilpAddressToPde (_In_ const void *address)
 
static HardwarePte * UtilpAddressToPte (_In_ const void *address)
 
NTSTATUS UtilInitialization (PDRIVER_OBJECT driver_object)
 
void UtilTermination ()
 Frees all resources allocated for the sake of the Util functions. More...
 
static NTSTATUS UtilpInitializeRtlPcToFileHeader (PDRIVER_OBJECT driver_object)
 
static PVOID NTAPI UtilpUnsafePcToFileHeader (PVOID pc_value, PVOID *base_of_image)
 
void * UtilPcToFileHeader (void *pc_value)
 
const PhysicalMemoryDescriptorUtilGetPhysicalMemoryRanges ()
 Returns ranges of physical memory on the system. More...
 
NTSTATUS UtilForEachProcessor (NTSTATUS(*callback_routine)(void *), void *context)
 
NTSTATUS UtilForEachProcessorDpc (PKDEFERRED_ROUTINE deferred_routine, void *context)
 
NTSTATUS UtilSleep (LONG Millisecond)
 
void * UtilMemMem (const void *search_base, SIZE_T search_size, const void *pattern, SIZE_T pattern_size)
 
void * UtilGetSystemProcAddress (const wchar_t *proc_name)
 
bool UtilIsX86Pae ()
 Checks if the system is a PAE-enabled x86 system. More...
 
bool UtilIsAccessibleAddress (void *address)
 
static bool UtilpIsCanonicalFormAddress (void *address)
 
static HardwarePte * UtilpAddressToPxe (const void *address)
 
static HardwarePte * UtilpAddressToPpe (const void *address)
 
static HardwarePte * UtilpAddressToPde (const void *address)
 
static HardwarePte * UtilpAddressToPte (const void *address)
 
ULONG64 UtilPaFromVa (void *va)
 
PFN_NUMBER UtilPfnFromVa (void *va)
 
PFN_NUMBER UtilPfnFromPa (ULONG64 pa)
 
void * UtilVaFromPa (ULONG64 pa)
 
ULONG64 UtilPaFromPfn (PFN_NUMBER pfn)
 
void * UtilVaFromPfn (PFN_NUMBER pfn)
 
void * UtilAllocateContiguousMemory (SIZE_T number_of_bytes)
 
void UtilFreeContiguousMemory (void *base_address)
 
NTSTATUS UtilVmCall (HypercallNumber hypercall_number, void *context)
 
void UtilDumpGpRegisters (const AllRegisters *all_regs, ULONG_PTR stack_pointer)
 
ULONG_PTR UtilVmRead (VmcsField field)
 
ULONG64 UtilVmRead64 (VmcsField field)
 
VmxStatus UtilVmWrite (VmcsField field, ULONG_PTR field_value)
 
VmxStatus UtilVmWrite64 (VmcsField field, ULONG64 field_value)
 
ULONG_PTR UtilReadMsr (Msr msr)
 
ULONG64 UtilReadMsr64 (Msr msr)
 
void UtilWriteMsr (Msr msr, ULONG_PTR value)
 
void UtilWriteMsr64 (Msr msr, ULONG64 value)
 
VmxStatus UtilInveptGlobal ()
 Executes the INVEPT instruction and invalidates EPT entry cache. More...
 
VmxStatus UtilInvvpidIndividualAddress (USHORT vpid, void *address)
 
VmxStatus UtilInvvpidSingleContext (USHORT vpid)
 
VmxStatus UtilInvvpidAllContext ()
 Executes the INVVPID instruction (type 2) More...
 
VmxStatus UtilInvvpidSingleContextExceptGlobal (USHORT vpid)
 
void UtilLoadPdptes (ULONG_PTR cr3_value)
 
NTSTATUS UtilForceCopyMemory (void *destination, const void *source, SIZE_T length)
 

Variables

static const auto kUtilpUseRtlPcToFileHeader = false
 
NTKERNELAPI _In_ PHYSICAL_ADDRESS LowestAcceptableAddress
 
NTKERNELAPI _In_ PHYSICAL_ADDRESS _In_ PHYSICAL_ADDRESS HighestAcceptableAddress
 
NTKERNELAPI _In_ PHYSICAL_ADDRESS _In_ PHYSICAL_ADDRESS _In_opt_ PHYSICAL_ADDRESS BoundaryAddressMultiple
 
NTKERNELAPI _In_ PHYSICAL_ADDRESS _In_ PHYSICAL_ADDRESS _In_opt_ PHYSICAL_ADDRESS _In_ ULONG Protect
 
NTKERNELAPI _In_ PHYSICAL_ADDRESS _In_ PHYSICAL_ADDRESS _In_opt_ PHYSICAL_ADDRESS _In_ ULONG _In_ NODE_REQUIREMENT PreferredNode
 
static RtlPcToFileHeaderTypeg_utilp_RtlPcToFileHeader
 
static LIST_ENTRY * g_utilp_PsLoadedModuleList
 
static PhysicalMemoryDescriptorg_utilp_physical_memory_ranges
 
static MmAllocateContiguousNodeMemoryTypeg_utilp_MmAllocateContiguousNodeMemory
 
static ULONG_PTR g_utilp_pxe_base = 0
 
static ULONG_PTR g_utilp_ppe_base = 0
 
static ULONG_PTR g_utilp_pde_base = 0
 
static ULONG_PTR g_utilp_pte_base = 0
 
static ULONG_PTR g_utilp_pxi_shift = 0
 
static ULONG_PTR g_utilp_ppi_shift = 0
 
static ULONG_PTR g_utilp_pdi_shift = 0
 
static ULONG_PTR g_utilp_pti_shift = 0
 
static ULONG_PTR g_utilp_pxi_mask = 0
 
static ULONG_PTR g_utilp_ppi_mask = 0
 
static ULONG_PTR g_utilp_pdi_mask = 0
 
static ULONG_PTR g_utilp_pti_mask = 0
 

Detailed Description

Implements primitive utility functions.

Definition in file util.cpp.

Typedef Documentation

◆ MmAllocateContiguousNodeMemoryType

using MmAllocateContiguousNodeMemoryType = decltype(MmAllocateContiguousNodeMemory)

Definition at line 50 of file util.cpp.

◆ RtlPcToFileHeaderType

Definition at line 38 of file util.cpp.

Function Documentation

◆ _Post_writable_byte_size_()

NTKERNELAPI _Post_writable_byte_size_ ( NumberOfBytes  )

◆ RtlPcToFileHeader()

NTKERNELAPI PVOID NTAPI RtlPcToFileHeader ( _In_ PVOID  PcValue,
_Out_ PVOID *  BaseOfImage 
)

◆ UtilAllocateContiguousMemory()

void* UtilAllocateContiguousMemory ( SIZE_T  number_of_bytes)

Definition at line 623 of file util.cpp.

Here is the caller graph for this function:

◆ UtilDumpGpRegisters()

void UtilDumpGpRegisters ( const AllRegisters all_regs,
ULONG_PTR  stack_pointer 
)

Definition at line 667 of file util.cpp.

◆ UtilForceCopyMemory()

NTSTATUS UtilForceCopyMemory ( void *  destination,
const void *  source,
SIZE_T  length 
)

Definition at line 851 of file util.cpp.

◆ UtilForEachProcessor()

NTSTATUS UtilForEachProcessor ( NTSTATUS(*)(void *)  callback_routine,
void *  context 
)

Definition at line 412 of file util.cpp.

Here is the caller graph for this function:

◆ UtilForEachProcessorDpc()

NTSTATUS UtilForEachProcessorDpc ( PKDEFERRED_ROUTINE  deferred_routine,
void *  context 
)

Definition at line 447 of file util.cpp.

◆ UtilFreeContiguousMemory()

void UtilFreeContiguousMemory ( void *  base_address)

Definition at line 644 of file util.cpp.

Here is the caller graph for this function:

◆ UtilGetPhysicalMemoryRanges()

const PhysicalMemoryDescriptor* UtilGetPhysicalMemoryRanges ( )

Returns ranges of physical memory on the system.

Returns
Physical memory ranges; never fails

Definition at line 403 of file util.cpp.

Here is the caller graph for this function:

◆ UtilGetSystemProcAddress()

void* UtilGetSystemProcAddress ( const wchar_t *  proc_name)

Definition at line 502 of file util.cpp.

Here is the caller graph for this function:

◆ UtilInitialization()

NTSTATUS UtilInitialization ( PDRIVER_OBJECT  driver_object)

Definition at line 142 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilInveptGlobal()

VmxStatus UtilInveptGlobal ( )

Executes the INVEPT instruction and invalidates EPT entry cache.

Returns
A result of the INVEPT instruction

Definition at line 787 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilInvvpidAllContext()

VmxStatus UtilInvvpidAllContext ( )

Executes the INVVPID instruction (type 2)

Returns
A result of the INVVPID instruction

Definition at line 812 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilInvvpidIndividualAddress()

VmxStatus UtilInvvpidIndividualAddress ( USHORT  vpid,
void *  address 
)

Definition at line 794 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilInvvpidSingleContext()

VmxStatus UtilInvvpidSingleContext ( USHORT  vpid)

Definition at line 804 of file util.cpp.

Here is the call graph for this function:

◆ UtilInvvpidSingleContextExceptGlobal()

VmxStatus UtilInvvpidSingleContextExceptGlobal ( USHORT  vpid)

Definition at line 820 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilIsAccessibleAddress()

bool UtilIsAccessibleAddress ( void *  address)

Definition at line 517 of file util.cpp.

Here is the call graph for this function:

◆ UtilIsX86Pae()

bool UtilIsX86Pae ( )

Checks if the system is a PAE-enabled x86 system.

Returns
true if the system is a PAE-enabled x86 system

Definition at line 512 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilLoadPdptes()

void UtilLoadPdptes ( ULONG_PTR  cr3_value)

Definition at line 828 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilMemMem()

void* UtilMemMem ( const void *  search_base,
SIZE_T  search_size,
const void *  pattern,
SIZE_T  pattern_size 
)

Definition at line 486 of file util.cpp.

◆ UtilpAddressToPde() [1/2]

static HardwarePte* UtilpAddressToPde ( _In_ const void *  address)
static
Here is the caller graph for this function:

◆ UtilpAddressToPde() [2/2]

static HardwarePte* UtilpAddressToPde ( const void *  address)
static

Definition at line 572 of file util.cpp.

◆ UtilpAddressToPpe() [1/2]

static HardwarePte* UtilpAddressToPpe ( _In_ const void *  address)
static
Here is the caller graph for this function:

◆ UtilpAddressToPpe() [2/2]

static HardwarePte* UtilpAddressToPpe ( const void *  address)
static

Definition at line 563 of file util.cpp.

◆ UtilpAddressToPte() [1/2]

static HardwarePte* UtilpAddressToPte ( _In_ const void *  address)
static
Here is the caller graph for this function:

◆ UtilpAddressToPte() [2/2]

static HardwarePte* UtilpAddressToPte ( const void *  address)
static

Definition at line 581 of file util.cpp.

◆ UtilpAddressToPxe() [1/2]

static HardwarePte* UtilpAddressToPxe ( _In_ const void *  address)
static
Here is the caller graph for this function:

◆ UtilpAddressToPxe() [2/2]

static HardwarePte* UtilpAddressToPxe ( const void *  address)
static

Definition at line 554 of file util.cpp.

◆ UtilPaFromPfn()

ULONG64 UtilPaFromPfn ( PFN_NUMBER  pfn)

Definition at line 613 of file util.cpp.

Here is the caller graph for this function:

◆ UtilPaFromVa()

ULONG64 UtilPaFromVa ( void *  va)

Definition at line 590 of file util.cpp.

Here is the caller graph for this function:

◆ UtilpBuildPhysicalMemoryRanges()

static PhysicalMemoryDescriptor * UtilpBuildPhysicalMemoryRanges ( )
static

Definition at line 350 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilPcToFileHeader()

void* UtilPcToFileHeader ( void *  pc_value)

Definition at line 317 of file util.cpp.

◆ UtilPfnFromPa()

PFN_NUMBER UtilPfnFromPa ( ULONG64  pa)

Definition at line 601 of file util.cpp.

Here is the caller graph for this function:

◆ UtilPfnFromVa()

PFN_NUMBER UtilPfnFromVa ( void *  va)

Definition at line 596 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilpInitializePageTableVariables()

static NTSTATUS UtilpInitializePageTableVariables ( )
static
Here is the caller graph for this function:

◆ UtilpInitializePhysicalMemoryRanges()

static NTSTATUS UtilpInitializePhysicalMemoryRanges ( )
static

Definition at line 323 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilpInitializeRtlPcToFileHeader() [1/2]

static NTSTATUS UtilpInitializeRtlPcToFileHeader ( _In_ PDRIVER_OBJECT  driver_object)
static
Here is the caller graph for this function:

◆ UtilpInitializeRtlPcToFileHeader() [2/2]

static NTSTATUS UtilpInitializeRtlPcToFileHeader ( PDRIVER_OBJECT  driver_object)
static

Definition at line 269 of file util.cpp.

Here is the call graph for this function:

◆ UtilpIsCanonicalFormAddress() [1/2]

static bool UtilpIsCanonicalFormAddress ( _In_ void *  address)
static
Here is the caller graph for this function:

◆ UtilpIsCanonicalFormAddress() [2/2]

static bool UtilpIsCanonicalFormAddress ( void *  address)
static

Definition at line 545 of file util.cpp.

Here is the call graph for this function:

◆ UtilpUnsafePcToFileHeader() [1/2]

static PVOID NTAPI UtilpUnsafePcToFileHeader ( _In_ PVOID  pc_value,
_Out_ PVOID *  base_of_image 
)
static
Here is the caller graph for this function:

◆ UtilpUnsafePcToFileHeader() [2/2]

static PVOID NTAPI UtilpUnsafePcToFileHeader ( PVOID  pc_value,
PVOID *  base_of_image 
)
static

Definition at line 297 of file util.cpp.

Here is the call graph for this function:

◆ UtilReadMsr()

ULONG_PTR UtilReadMsr ( Msr  msr)

Definition at line 767 of file util.cpp.

Here is the caller graph for this function:

◆ UtilReadMsr64()

ULONG64 UtilReadMsr64 ( Msr  msr)

Definition at line 772 of file util.cpp.

Here is the caller graph for this function:

◆ UtilSleep()

NTSTATUS UtilSleep ( LONG  Millisecond)

Definition at line 477 of file util.cpp.

◆ UtilTermination()

void UtilTermination ( )

Frees all resources allocated for the sake of the Util functions.

Definition at line 170 of file util.cpp.

Here is the caller graph for this function:

◆ UtilVaFromPa()

void* UtilVaFromPa ( ULONG64  pa)

Definition at line 606 of file util.cpp.

Here is the caller graph for this function:

◆ UtilVaFromPfn()

void* UtilVaFromPfn ( PFN_NUMBER  pfn)

Definition at line 618 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilVmCall()

NTSTATUS UtilVmCall ( HypercallNumber  hypercall_number,
void *  context 
)

Definition at line 649 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilVmRead()

ULONG_PTR UtilVmRead ( VmcsField  field)

Definition at line 705 of file util.cpp.

Here is the caller graph for this function:

◆ UtilVmRead64()

ULONG64 UtilVmRead64 ( VmcsField  field)

Definition at line 718 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilVmWrite()

VmxStatus UtilVmWrite ( VmcsField  field,
ULONG_PTR  field_value 
)

Definition at line 737 of file util.cpp.

Here is the caller graph for this function:

◆ UtilVmWrite64()

VmxStatus UtilVmWrite64 ( VmcsField  field,
ULONG64  field_value 
)

Definition at line 744 of file util.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UtilWriteMsr()

void UtilWriteMsr ( Msr  msr,
ULONG_PTR  value 
)

Definition at line 777 of file util.cpp.

◆ UtilWriteMsr64()

void UtilWriteMsr64 ( Msr  msr,
ULONG64  value 
)

Definition at line 782 of file util.cpp.

Here is the caller graph for this function:

Variable Documentation

◆ BoundaryAddressMultiple

NTKERNELAPI _In_ PHYSICAL_ADDRESS _In_ PHYSICAL_ADDRESS _In_opt_ PHYSICAL_ADDRESS BoundaryAddressMultiple

Definition at line 44 of file util.cpp.

◆ g_utilp_MmAllocateContiguousNodeMemory

MmAllocateContiguousNodeMemoryType* g_utilp_MmAllocateContiguousNodeMemory
static

Definition at line 118 of file util.cpp.

◆ g_utilp_pde_base

ULONG_PTR g_utilp_pde_base = 0
static

Definition at line 122 of file util.cpp.

◆ g_utilp_pdi_mask

ULONG_PTR g_utilp_pdi_mask = 0
static

Definition at line 132 of file util.cpp.

◆ g_utilp_pdi_shift

ULONG_PTR g_utilp_pdi_shift = 0
static

Definition at line 127 of file util.cpp.

◆ g_utilp_physical_memory_ranges

PhysicalMemoryDescriptor* g_utilp_physical_memory_ranges
static

Definition at line 115 of file util.cpp.

◆ g_utilp_ppe_base

ULONG_PTR g_utilp_ppe_base = 0
static

Definition at line 121 of file util.cpp.

◆ g_utilp_ppi_mask

ULONG_PTR g_utilp_ppi_mask = 0
static

Definition at line 131 of file util.cpp.

◆ g_utilp_ppi_shift

ULONG_PTR g_utilp_ppi_shift = 0
static

Definition at line 126 of file util.cpp.

◆ g_utilp_PsLoadedModuleList

LIST_ENTRY* g_utilp_PsLoadedModuleList
static

Definition at line 113 of file util.cpp.

◆ g_utilp_pte_base

ULONG_PTR g_utilp_pte_base = 0
static

Definition at line 123 of file util.cpp.

◆ g_utilp_pti_mask

ULONG_PTR g_utilp_pti_mask = 0
static

Definition at line 133 of file util.cpp.

◆ g_utilp_pti_shift

ULONG_PTR g_utilp_pti_shift = 0
static

Definition at line 128 of file util.cpp.

◆ g_utilp_pxe_base

ULONG_PTR g_utilp_pxe_base = 0
static

Definition at line 120 of file util.cpp.

◆ g_utilp_pxi_mask

ULONG_PTR g_utilp_pxi_mask = 0
static

Definition at line 130 of file util.cpp.

◆ g_utilp_pxi_shift

ULONG_PTR g_utilp_pxi_shift = 0
static

Definition at line 125 of file util.cpp.

◆ g_utilp_RtlPcToFileHeader

RtlPcToFileHeaderType* g_utilp_RtlPcToFileHeader
static

Definition at line 111 of file util.cpp.

◆ HighestAcceptableAddress

NTKERNELAPI _In_ PHYSICAL_ADDRESS _In_ PHYSICAL_ADDRESS HighestAcceptableAddress

Definition at line 44 of file util.cpp.

◆ kUtilpUseRtlPcToFileHeader

const auto kUtilpUseRtlPcToFileHeader = false
static

Definition at line 28 of file util.cpp.

◆ LowestAcceptableAddress

NTKERNELAPI _In_ PHYSICAL_ADDRESS LowestAcceptableAddress

Definition at line 44 of file util.cpp.

◆ PreferredNode

NTKERNELAPI _In_ PHYSICAL_ADDRESS _In_ PHYSICAL_ADDRESS _In_opt_ PHYSICAL_ADDRESS _In_ ULONG _In_ NODE_REQUIREMENT PreferredNode

Definition at line 44 of file util.cpp.

◆ Protect

NTKERNELAPI _In_ PHYSICAL_ADDRESS _In_ PHYSICAL_ADDRESS _In_opt_ PHYSICAL_ADDRESS _In_ ULONG Protect

Definition at line 44 of file util.cpp.