Implement allocations for VMX Regions (VMXON Region, VMCS, MSR Bitmap and etc.)
More...
#include "pch.h"
Implement allocations for VMX Regions (VMXON Region, VMCS, MSR Bitmap and etc.)
- Author
- Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
- Version
- 0.1
- Date
- 2020-04-11
- Copyright
- This project is released under the GNU Public License v3.
◆ VmxAllocateHostGdt()
Allocate a buffer for host GDT.
- Parameters
-
- Returns
- BOOLEAN Returns true if allocation was successful otherwise returns false
333{
335
336
337
338
340 {
342 }
343
344
345
346
348
350 {
351 LogError(
"Err, insufficient memory in allocating host GDT");
353 }
354
355 LogDebugInfo(
"Host GDT virtual address : 0x%llx", VCpu->HostGdt);
356
358}
#define NULL64_ZERO
Definition BasicTypes.h:52
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54
unsigned __int64 UINT64
Definition BasicTypes.h:21
unsigned int UINT32
Definition BasicTypes.h:48
#define LogDebugInfo(format,...)
Log, initialize boot information and debug information.
Definition HyperDbgHyperLogIntrinsics.h:155
#define LogError(format,...)
Log in the case of error.
Definition HyperDbgHyperLogIntrinsics.h:113
PVOID PlatformMemAllocateZeroedNonPagedPool(SIZE_T NumberOfBytes)
Allocate a non-paged buffer (zeroed)
Definition Mem.c:69
#define HOST_GDT_DESCRIPTOR_COUNT
Maximum number of entries in GDT.
Definition Segmentation.h:29
◆ VmxAllocateHostIdt()
Allocate a buffer for host IDT.
- Parameters
-
- Returns
- BOOLEAN Returns true if allocation was successful otherwise returns false
298{
300
301
302
303
305 {
307 }
308
309
310
311
313
315 {
316 LogError(
"Err, insufficient memory in allocating host IDT");
318 }
319
320 LogDebugInfo(
"Host IDT virtual address : 0x%llx", VCpu->HostIdt);
321
323}
#define HOST_IDT_DESCRIPTOR_COUNT
Maximum number of interrupt entries in IDT.
Definition IdtEmulation.h:29
◆ VmxAllocateHostInterruptStack()
Allocate a buffer for host interrupt stack.
- Parameters
-
- Returns
- BOOLEAN Returns true if allocation was successful otherwise returns false
395{
397
399 {
400 LogError(
"Err, insufficient memory in allocating host interrupt stack");
402 }
403
404 LogDebugInfo(
"Host interrupt stack virtual address : 0x%llx", VCpu->HostInterruptStack);
405
407}
#define HOST_INTERRUPT_STACK_SIZE
Size of host interrupt stack.
Definition Segmentation.h:35
◆ VmxAllocateHostTss()
Allocate a buffer for host TSS.
- Parameters
-
- Returns
- BOOLEAN Returns true if allocation was successful otherwise returns false
368{
370
371
372
373
375
377 {
378 LogError(
"Err, insufficient memory in allocating host TSS");
380 }
381
382 LogDebugInfo(
"Host TSS virtual address : 0x%llx", VCpu->HostTss);
383
385}
◆ VmxAllocateInvalidMsrBimap()
UINT64 * VmxAllocateInvalidMsrBimap |
( |
| ) |
|
Allocates a buffer and tests for the MSRs that cause #GP.
- Returns
- UINT64 Allocated buffer for MSR Bitmap
265{
266 UINT64 * InvalidMsrBitmap;
267
269
270 if (InvalidMsrBitmap == NULL)
271 {
273 }
274
275 for (
UINT32 i = 0; i < 0x1000; ++i)
276 {
277 __try
278 {
279 __readmsr(i);
280 }
281 __except (EXCEPTION_EXECUTE_HANDLER)
282 {
283 SetBit(i, (
unsigned long *)InvalidMsrBitmap);
284 }
285 }
286
287 return InvalidMsrBitmap;
288}
void SetBit(int BitNumber, unsigned long *addr)
set the bit
Definition Bitwise.c:46
NULL()
Definition test-case-generator.py:530
◆ VmxAllocateIoBitmaps()
Allocate a buffer for I/O Bitmap.
- Parameters
-
- Returns
- BOOLEAN Returns true if allocation was successful otherwise returns false
222{
223
224
225
227
229 {
230 LogError(
"Err, insufficient memory in allocating I/O Bitmaps A");
232 }
233
235
236 LogDebugInfo(
"I/O Bitmap A Virtual Address : 0x%llx", VCpu->IoBitmapVirtualAddressA);
237 LogDebugInfo(
"I/O Bitmap A Physical Address : 0x%llx", VCpu->IoBitmapPhysicalAddressA);
238
239
240
241
243
245 {
246 LogError(
"Err, insufficient memory in allocating I/O Bitmaps B");
248 }
249
251
252 LogDebugInfo(
"I/O Bitmap B virtual address : 0x%llx", VCpu->IoBitmapVirtualAddressB);
253 LogDebugInfo(
"I/O Bitmap B physical address : 0x%llx", VCpu->IoBitmapPhysicalAddressB);
254
256}
_Use_decl_annotations_ UINT64 VirtualAddressToPhysicalAddress(_In_ PVOID VirtualAddress)
Converts Virtual Address to Physical Address.
Definition Conversion.c:154
◆ VmxAllocateMsrBitmap()
Allocate a buffer for Msr Bitmap.
- Parameters
-
VCpu | The virtual processor's state |
- Returns
- BOOLEAN Returns true if allocation was successful otherwise returns false
193{
194
195
196
197
199
201 {
202 LogError(
"Err, insufficient memory in allocating MSR Bitmaps");
204 }
205
207
208 LogDebugInfo(
"MSR Bitmap virtual address : 0x%llx", VCpu->MsrBitmapVirtualAddress);
209 LogDebugInfo(
"MSR Bitmap physical address : 0x%llx", VCpu->MsrBitmapPhysicalAddress);
210
212}
◆ VmxAllocateVmcsRegion()
Allocate Vmcs region and set the Revision ID based on IA32_VMX_BASIC_MSR.
- Parameters
-
- Returns
- BOOLEAN Returns true if allocation was successful and vmptrld executed without error otherwise returns false
104{
105 IA32_VMX_BASIC_REGISTER VmxBasicMsr = {0};
106 SIZE_T VmcsSize;
110 UINT64 AlignedVmcsRegionPhysicalAddr;
111
112#ifdef ENV_WINDOWS
113
114
115
117 KeRaiseIrqlToDpcLevel();
118#endif
119
120
121
122
125 if (VmcsRegion == NULL)
126 {
127 LogError(
"Err, couldn't allocate Buffer for VMCS region");
129 }
130
132
134 LogDebugInfo(
"VMCS region address : %llx", AlignedVmcsRegion);
135
137 LogDebugInfo(
"VMCS region physical address : %llx", AlignedVmcsRegionPhysicalAddr);
138
139
140
141
142 VmxBasicMsr.AsUInt = __readmsr(IA32_VMX_BASIC);
143 LogDebugInfo(
"Revision Identifier (IA32_VMX_BASIC - MSR 0x480) : 0x%x", VmxBasicMsr.VmcsRevisionId);
144
145
146
147
148 *(
UINT64 *)AlignedVmcsRegion = VmxBasicMsr.VmcsRevisionId;
149
151
152
153
154
156
158}
unsigned char UINT8
Definition BasicTypes.h:46
PVOID PlatformMemAllocateContiguousZeroedMemory(SIZE_T NumberOfBytes)
Allocate a contiguous zeroed memory.
Definition Mem.c:22
#define VMCS_SIZE
VMCS Region Size.
Definition Vmx.h:22
UINT64 VmcsRegionPhysicalAddress
Definition State.h:312
UINT64 VmcsRegionVirtualAddress
Definition State.h:313
◆ VmxAllocateVmmStack()
Allocate VMM Stack.
- Parameters
-
VCpu | The virtual processor's state |
- Returns
- BOOLEAN Returns true if allocation was successful otherwise returns false
168{
169
170
171
173
175 {
176 LogError(
"Err, insufficient memory in allocating vmm stack");
178 }
179
180 LogDebugInfo(
"VMM Stack for logical processor : 0x%llx", VCpu->VmmStack);
181
183}
#define VMM_STACK_SIZE
Stack Size.
Definition Vmx.h:140
◆ VmxAllocateVmxonRegion()
Allocates Vmxon region and set the Revision ID based on IA32_VMX_BASIC_MSR.
- Parameters
-
- Returns
- BOOLEAN Returns true if allocation was successful and vmxon executed without error otherwise returns false
24{
25 IA32_VMX_BASIC_REGISTER VmxBasicMsr = {0};
26 SIZE_T VmxonSize;
29 UINT64 VmxonRegionPhysicalAddr;
31 UINT64 AlignedVmxonRegionPhysicalAddr;
32
33#ifdef ENV_WINDOWS
34
35
36
38 KeRaiseIrqlToDpcLevel();
39#endif
40
41
42
43
46 if (VmxonRegion == NULL)
47 {
48 LogError(
"Err, couldn't allocate buffer for VMXON region");
50 }
51
53
55 LogDebugInfo(
"VMXON Region Address : %llx", AlignedVmxonRegion);
56
57
58
59
61 LogDebugInfo(
"VMXON Region Physical Address : %llx", AlignedVmxonRegionPhysicalAddr);
62
63
64
65
66 VmxBasicMsr.AsUInt = __readmsr(IA32_VMX_BASIC);
67 LogDebugInfo(
"Revision Identifier (IA32_VMX_BASIC - MSR 0x480) : 0x%x", VmxBasicMsr.VmcsRevisionId);
68
69
70
71
72 *(
UINT64 *)AlignedVmxonRegion = VmxBasicMsr.VmcsRevisionId;
73
74
75
76
77 VmxonStatus = __vmx_on(&AlignedVmxonRegionPhysicalAddr);
78 if (VmxonStatus)
79 {
80 LogError(
"Err, executing vmxon instruction failed with status : %d", VmxonStatus);
82 }
83
85
86
87
88
90
92}
#define VMXON_SIZE
VMXON Region Size.
Definition Vmx.h:28
UINT64 VmxonRegionVirtualAddress
Definition State.h:311
UINT64 VmxonRegionPhysicalAddress
Definition State.h:310