HyperPlatform Programmer's Reference
util.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2017, Satoshi Tanda. All rights reserved.
2 // Use of this source code is governed by a MIT-style license that can be
3 // found in the LICENSE file.
4 
7 
8 #ifndef HYPERPLATFORM_UTIL_H_
9 #define HYPERPLATFORM_UTIL_H_
10 
11 #include "ia32_type.h"
12 
13 extern "C" {
15 //
16 // macro utilities
17 //
18 
20 //
21 // constants and macros
22 //
23 
25 //
26 // types
27 //
28 
31  ULONG_PTR base_page;
32  ULONG_PTR page_count;
33 };
34 #if defined(_AMD64_)
35 static_assert(sizeof(PhysicalMemoryRun) == 0x10, "Size check");
36 #else
37 static_assert(sizeof(PhysicalMemoryRun) == 0x8, "Size check");
38 #endif
39 
42  PFN_COUNT number_of_runs;
43  PFN_NUMBER number_of_pages;
45 };
46 #if defined(_AMD64_)
47 static_assert(sizeof(PhysicalMemoryDescriptor) == 0x20, "Size check");
48 #else
49 static_assert(sizeof(PhysicalMemoryDescriptor) == 0x10, "Size check");
50 #endif
51 
55 enum class VmxStatus : unsigned __int8 {
56  kOk = 0,
57  kErrorWithStatus = 1,
59 };
60 
62 constexpr VmxStatus operator|=(_In_ VmxStatus lhs, _In_ VmxStatus rhs) {
63  return static_cast<VmxStatus>(static_cast<unsigned __int8>(lhs) |
64  static_cast<unsigned __int8>(rhs));
65 }
66 
68 enum class HypercallNumber : unsigned __int32 {
70  kPingVmm,
72 };
73 
75 //
76 // prototypes
77 //
78 
82 _IRQL_requires_max_(PASSIVE_LEVEL) NTSTATUS
83  UtilInitialization(_In_ PDRIVER_OBJECT driver_object);
84 
86 _IRQL_requires_max_(PASSIVE_LEVEL) void UtilTermination();
87 
91 void *UtilPcToFileHeader(_In_ void *address);
92 
96 
101 _IRQL_requires_max_(APC_LEVEL) NTSTATUS
102  UtilForEachProcessor(_In_ NTSTATUS (*callback_routine)(void *),
103  _In_opt_ void *context);
104 
112 _IRQL_requires_max_(DISPATCH_LEVEL) NTSTATUS
113  UtilForEachProcessorDpc(_In_ PKDEFERRED_ROUTINE deferred_routine,
114  _In_opt_ void *context);
115 
119 _IRQL_requires_max_(APC_LEVEL) NTSTATUS UtilSleep(_In_ LONG millisecond);
120 
128 void *UtilMemMem(_In_ const void *search_base, _In_ SIZE_T search_size,
129  _In_ const void *pattern, _In_ SIZE_T pattern_size);
130 
134 void *UtilGetSystemProcAddress(_In_ const wchar_t *proc_name);
135 
138 bool UtilIsX86Pae();
139 
143 bool UtilIsAccessibleAddress(_In_ void *address);
144 
151 ULONG64 UtilPaFromVa(_In_ void *va);
152 
159 PFN_NUMBER UtilPfnFromVa(_In_ void *va);
160 
164 PFN_NUMBER UtilPfnFromPa(_In_ ULONG64 pa);
165 
169 void *UtilVaFromPa(_In_ ULONG64 pa);
170 
174 ULONG64 UtilPaFromPfn(_In_ PFN_NUMBER pfn);
175 
179 void *UtilVaFromPfn(_In_ PFN_NUMBER pfn);
180 
186 _Must_inspect_result_ _IRQL_requires_max_(DISPATCH_LEVEL) void
187  *UtilAllocateContiguousMemory(_In_ SIZE_T number_of_bytes);
188 
191 _IRQL_requires_max_(DISPATCH_LEVEL) void UtilFreeContiguousMemory(
192  _In_ void *base_address);
193 
198 NTSTATUS UtilVmCall(_In_ HypercallNumber hypercall_number,
199  _In_opt_ void *context);
200 
204 void UtilDumpGpRegisters(_In_ const AllRegisters *all_regs,
205  _In_ ULONG_PTR stack_pointer);
206 
210 ULONG_PTR UtilVmRead(_In_ VmcsField field);
211 
215 ULONG64 UtilVmRead64(_In_ VmcsField field);
216 
221 VmxStatus UtilVmWrite(_In_ VmcsField field, _In_ ULONG_PTR field_value);
222 
227 VmxStatus UtilVmWrite64(_In_ VmcsField field, _In_ ULONG64 field_value);
228 
232 ULONG_PTR UtilReadMsr(_In_ Msr msr);
233 
237 ULONG64 UtilReadMsr64(_In_ Msr msr);
238 
242 void UtilWriteMsr(_In_ Msr msr, _In_ ULONG_PTR value);
243 
247 void UtilWriteMsr64(_In_ Msr msr, _In_ ULONG64 value);
248 
252 
255 VmxStatus UtilInvvpidIndividualAddress(_In_ USHORT vpid, _In_ void *address);
256 
259 VmxStatus UtilInvvpidSingleContext(_In_ USHORT vpid);
260 
264 
268 
271 void UtilLoadPdptes(_In_ ULONG_PTR cr3_value);
272 
278 _IRQL_requires_max_(DISPATCH_LEVEL) NTSTATUS
279  UtilForceCopyMemory(_In_ void *destination, _In_ const void *source,
280  _In_ SIZE_T length);
281 
283 //
284 // variables
285 //
286 
288 //
289 // implementations
290 //
291 
292 } // extern "C"
293 
299 template <typename T>
300 constexpr bool UtilIsInBounds(_In_ const T &value, _In_ const T &min,
301  _In_ const T &max) {
302  return (min <= value) && (value <= max);
303 }
304 
305 #endif // HYPERPLATFORM_UTIL_H_
NTSTATUS UtilForEachProcessorDpc(_In_ PKDEFERRED_ROUTINE deferred_routine, _In_opt_ void *context)
Queues deferred_routine on all processors.
bool UtilIsAccessibleAddress(_In_ void *address)
Checks is the address is present on physical memory.
PFN_NUMBER number_of_pages
A physical memory size in pages.
Definition: util.h:43
Operation failed without status available.
HypercallNumber
Available command numbers for VMCALL.
Definition: util.h:68
const PhysicalMemoryDescriptor * UtilGetPhysicalMemoryRanges()
Returns ranges of physical memory on the system.
Definition: util.cpp:403
PFN_NUMBER UtilPfnFromPa(_In_ ULONG64 pa)
PA -> PFN.
VmxStatus
Indicates a result of VMX-instructions.
Definition: util.h:55
ULONG64 UtilPaFromPfn(_In_ PFN_NUMBER pfn)
PNF -> PA.
Operation failed with extended status available.
void * UtilAllocateContiguousMemory(_In_ SIZE_T number_of_bytes)
Allocates continuous physical memory.
Operation succeeded.
VmxStatus UtilVmWrite(_In_ VmcsField field, _In_ ULONG_PTR field_value)
Writes natural-width VMCS.
ULONG_PTR base_page
A base address / PAGE_SIZE (ie, 0x1 for 0x1000)
Definition: util.h:31
Represents a physical memory ranges of the system.
Definition: util.h:41
void * UtilMemMem(_In_ const void *search_base, _In_ SIZE_T search_size, _In_ const void *pattern, _In_ SIZE_T pattern_size)
Searches a byte pattern from a given address range.
VmxStatus UtilInvvpidIndividualAddress(_In_ USHORT vpid, _In_ void *address)
Executes the INVVPID instruction (type 0)
void * UtilVaFromPfn(_In_ PFN_NUMBER pfn)
PNF -> VA.
NTSTATUS UtilForceCopyMemory(_In_ void *destination, _In_ const void *source, _In_ SIZE_T length)
Does RtlCopyMemory safely even if destination is a read only region.
void UtilFreeContiguousMemory(_In_ void *base_address)
Frees an address allocated by UtilAllocateContiguousMemory()
NTSTATUS UtilForEachProcessor(_In_ NTSTATUS(*callback_routine)(void *), _In_opt_ void *context)
Executes callback_routine on each processor.
VmxStatus UtilInvvpidAllContext()
Executes the INVVPID instruction (type 2)
Definition: util.cpp:812
VmxStatus UtilVmWrite64(_In_ VmcsField field, _In_ ULONG64 field_value)
Writes 64bit-width VMCS.
VmxStatus UtilInveptGlobal()
Executes the INVEPT instruction and invalidates EPT entry cache.
Definition: util.cpp:787
Msr
See: MODEL-SPECIFIC REGISTERS (MSRS)
Definition: ia32_type.h:576
bool UtilIsX86Pae()
Checks if the system is a PAE-enabled x86 system.
Definition: util.cpp:512
ULONG64 UtilVmRead64(_In_ VmcsField field)
Reads 64bit-width VMCS.
VmcsField
See: FIELD ENCODING IN VMCS.
Definition: ia32_type.h:648
Represents ranges of addresses.
Definition: util.h:30
NTSTATUS UtilVmCall(_In_ HypercallNumber hypercall_number, _In_opt_ void *context)
Executes VMCALL.
VmxStatus UtilInvvpidSingleContext(_In_ USHORT vpid)
Executes the INVVPID instruction (type 1)
void UtilTermination()
Frees all resources allocated for the sake of the Util functions.
Definition: util.cpp:170
VmxStatus UtilInvvpidSingleContextExceptGlobal(_In_ USHORT vpid)
Executes the INVVPID instruction (type 3)
void * UtilGetSystemProcAddress(_In_ const wchar_t *proc_name)
Get an address of an exported symbol by the kernel or HAL.
void UtilDumpGpRegisters(_In_ const AllRegisters *all_regs, _In_ ULONG_PTR stack_pointer)
Debug prints registers.
Defines constants and structures defined by the x86-64 architecture.
void * UtilVaFromPa(_In_ ULONG64 pa)
PA -> VA.
void * UtilPcToFileHeader(_In_ void *address)
Returns a module base address of address.
NTSTATUS UtilSleep(_In_ LONG millisecond)
Suspends the execution of the current thread.
ULONG_PTR UtilVmRead(_In_ VmcsField field)
Reads natural-width VMCS.
constexpr VmxStatus operator|=(_In_ VmxStatus lhs, _In_ VmxStatus rhs)
Provides |= operator for VmxStatus.
Definition: util.h:62
Sends ping to the VMM.
void UtilLoadPdptes(_In_ ULONG_PTR cr3_value)
Loads the PDPTE registers from CR3 to VMCS.
NTSTATUS UtilInitialization(_In_ PDRIVER_OBJECT driver_object)
Makes the Util functions ready for use.
ULONG_PTR page_count
A number of pages.
Definition: util.h:32
PhysicalMemoryRun run[1]
ranges of addresses
Definition: util.h:44
PFN_NUMBER UtilPfnFromVa(_In_ void *va)
VA -> PFN.
ULONG64 UtilPaFromVa(_In_ void *va)
VA -> PA.
constexpr bool UtilIsInBounds(_In_ const T &value, _In_ const T &min, _In_ const T &max)
Tests if value is in between min and max.
Definition: util.h:300
ULONG_PTR UtilReadMsr(_In_ Msr msr)
Reads natural-width MSR.
void UtilWriteMsr64(_In_ Msr msr, _In_ ULONG64 value)
Writes 64bit-width MSR.
PFN_COUNT number_of_runs
A number of PhysicalMemoryDescriptor::run.
Definition: util.h:42
ULONG64 UtilReadMsr64(_In_ Msr msr)
Reads 64bit-width MSR.
void UtilWriteMsr(_In_ Msr msr, _In_ ULONG_PTR value)
Writes natural-width MSR.
Represents a stack layout after a sequence of PUSHFx, PUSHAx.
Definition: ia32_type.h:111