HyperDbg Debugger
Loading...
Searching...
No Matches
Vmexit.c File Reference

The functions for VM-Exit handler for different exit reasons. More...

#include "pch.h"

Functions

BOOLEAN VmxVmexitHandler (_Inout_ PGUEST_REGS GuestRegs)
 VM-Exit handler for different exit reasons.

Detailed Description

The functions for VM-Exit handler for different exit reasons.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2020-04-11

Function Documentation

◆ VmxVmexitHandler()

BOOLEAN VmxVmexitHandler ( _Inout_ PGUEST_REGS GuestRegs)

VM-Exit handler for different exit reasons.

Parameters
GuestRegsRegisters that are automatically saved by AsmVmexitHandler (HOST_RIP)
Returns
BOOLEAN Return True if VMXOFF executed (not in vmx anymore), or return false if we are still in vmx (so we should use vm resume)
23{
24 UINT32 ExitReason = 0;
25 BOOLEAN Result = FALSE;
27
28 //
29 // *********** SEND MESSAGE AFTER WE SET THE STATE ***********
30 //
31 VCpu = &g_GuestState[KeGetCurrentProcessorNumberEx(NULL)];
32
33 //
34 // Set the registers (general-purpose and XMM)
35 //
36 VCpu->Regs = GuestRegs;
37 VCpu->XmmRegs = (GUEST_XMM_REGS *)(((CHAR *)GuestRegs) + sizeof(GUEST_REGS));
38
39 //
40 // Indicates we are in Vmx root mode in this logical core
41 //
42 VCpu->IsOnVmxRootMode = TRUE;
43
44 //
45 // read the exit reason and exit qualification
46 //
47 VmxVmread32P(VMCS_EXIT_REASON, &ExitReason);
48 ExitReason &= 0xffff;
49
50 //
51 // Save the exit reason
52 //
53 VCpu->ExitReason = ExitReason;
54
55 //
56 // Increase the RIP by default
57 //
58 VCpu->IncrementRip = TRUE;
59
60 //
61 // Save the current rip
62 //
63 VmxVmread64P(VMCS_GUEST_RIP, &VCpu->LastVmexitRip);
64
65 //
66 // Set the rsp in general purpose registers structure
67 //
68 VmxVmread64P(VMCS_GUEST_RSP, &VCpu->Regs->rsp);
69
70 //
71 // Read the exit qualification
72 //
73 VmxVmread32P(VMCS_EXIT_QUALIFICATION, &VCpu->ExitQualification);
74
75 //
76 // Debugging purpose
77 //
78 // LogInfo("VM_EXIT_REASON : 0x%x", ExitReason);
79 // LogInfo("VMCS_EXIT_QUALIFICATION : 0x%llx", VCpu->ExitQualification);
80 //
81
82 switch (ExitReason)
83 {
84 case VMX_EXIT_REASON_TRIPLE_FAULT:
85 {
87
88 break;
89 }
90 //
91 // 25.1.2 Instructions That Cause VM Exits Unconditionally
92 // The following instructions cause VM exits when they are executed in VMX non-root operation: CPUID, GETSEC,
93 // INVD, and XSETBV. This is also true of instructions introduced with VMX, which include: INVEPT, INVVPID,
94 // VMCALL, VMCLEAR, VMLAUNCH, VMPTRLD, VMPTRST, VMRESUME, VMXOFF, and VMXON.
95 //
96
97 case VMX_EXIT_REASON_EXECUTE_VMCLEAR:
98 case VMX_EXIT_REASON_EXECUTE_VMPTRLD:
99 case VMX_EXIT_REASON_EXECUTE_VMPTRST:
100 case VMX_EXIT_REASON_EXECUTE_VMREAD:
101 case VMX_EXIT_REASON_EXECUTE_VMRESUME:
102 case VMX_EXIT_REASON_EXECUTE_VMWRITE:
103 case VMX_EXIT_REASON_EXECUTE_VMXOFF:
104 case VMX_EXIT_REASON_EXECUTE_VMXON:
105 case VMX_EXIT_REASON_EXECUTE_VMLAUNCH:
106 {
107 //
108 // cf=1 indicate vm instructions fail
109 //
110 // UINT64 Rflags = 0;
111 // VmxVmread64P(VMCS_GUEST_RFLAGS, &Rflags);
112 // VmxVmwrite64(VMCS_GUEST_RFLAGS, Rflags | 0x1);
113
114 //
115 // Handle unconditional vm-exits (inject #ud)
116 //
118
119 break;
120 }
121 case VMX_EXIT_REASON_EXECUTE_INVEPT:
122 case VMX_EXIT_REASON_EXECUTE_INVVPID:
123 case VMX_EXIT_REASON_EXECUTE_GETSEC:
124 case VMX_EXIT_REASON_EXECUTE_INVD:
125 {
126 //
127 // Handle unconditional vm-exits (inject #ud)
128 //
130
131 break;
132 }
133 case VMX_EXIT_REASON_MOV_CR:
134 {
135 //
136 // Handle vm-exit, events, dispatches and perform changes from CR access
137 //
139
140 break;
141 }
142 case VMX_EXIT_REASON_EXECUTE_RDMSR:
143 {
144 //
145 // Handle vm-exit, events, dispatches and perform MSR read
146 //
147 DispatchEventRdmsr(VCpu);
148
149 break;
150 }
151 case VMX_EXIT_REASON_EXECUTE_WRMSR:
152 {
153 //
154 // Handle vm-exit, events, dispatches and perform changes
155 //
156 DispatchEventWrmsr(VCpu);
157
158 break;
159 }
160 case VMX_EXIT_REASON_IO_SMI:
161 case VMX_EXIT_REASON_SMI:
162 {
163 //
164 // Handle SMI and IO-SMI (should never happen in normal cases)
165 //
166 LogInfo("VM-exit reason SMM %llx | qual: %llx", ExitReason, VCpu->ExitQualification);
167
168 break;
169 }
170 case VMX_EXIT_REASON_EXECUTE_CPUID:
171 {
172 //
173 // Dispatch and trigger the CPUID instruction events
174 //
175 DispatchEventCpuid(VCpu);
176
177 break;
178 }
179
180 case VMX_EXIT_REASON_EXECUTE_IO_INSTRUCTION:
181 {
182 //
183 // Dispatch and trigger the I/O instruction events
184 //
185 DispatchEventIO(VCpu);
186
187 break;
188 }
189 case VMX_EXIT_REASON_EPT_VIOLATION:
190 {
191 //
192 // Handle EPT violation
193 //
194 if (EptHandleEptViolation(VCpu) == FALSE)
195 {
196 LogError("Err, there were errors in handling EPT violation");
197 }
198
199 break;
200 }
201 case VMX_EXIT_REASON_EPT_MISCONFIGURATION:
202 {
203 //
204 // Handle EPT misconfiguration (should never happen)
205 //
207
208 break;
209 }
210 case VMX_EXIT_REASON_EXECUTE_VMCALL:
211 {
212 //
213 // Handle vm-exits of VMCALLs
214 //
216
217 break;
218 }
219 case VMX_EXIT_REASON_EXCEPTION_OR_NMI:
220 {
221 //
222 // Handle the EXCEPTION injection/emulation
223 //
225
226 break;
227 }
228 case VMX_EXIT_REASON_EXTERNAL_INTERRUPT:
229 {
230 //
231 // Call the external-interrupt handler
232 //
234
235 break;
236 }
237 case VMX_EXIT_REASON_INTERRUPT_WINDOW:
238 {
239 //
240 // Call the interrupt-window exiting handler to re-inject the previous
241 // interrupts or disable the interrupt-window exiting bit
242 //
244
245 break;
246 }
247 case VMX_EXIT_REASON_NMI_WINDOW:
248 {
249 //
250 // Call the NMI-window exiting handler
251 //
253
254 break;
255 }
256 case VMX_EXIT_REASON_MONITOR_TRAP_FLAG:
257 {
258 //
259 // General handler to monitor trap flags (MTF)
260 //
261 MtfHandleVmexit(VCpu);
262
263 break;
264 }
265 case VMX_EXIT_REASON_EXECUTE_HLT:
266 {
267 //
268 // We don't wanna halt
269 //
270
271 //
272 //__halt();
273 //
274 break;
275 }
276 case VMX_EXIT_REASON_EXECUTE_RDTSC:
277 case VMX_EXIT_REASON_EXECUTE_RDTSCP:
278
279 {
280 //
281 // Check whether we are allowed to change the registers
282 // and emulate rdtsc or not
283 //
284 DispatchEventTsc(VCpu, ExitReason == VMX_EXIT_REASON_EXECUTE_RDTSCP ? TRUE : FALSE);
285
286 break;
287 }
288 case VMX_EXIT_REASON_EXECUTE_RDPMC:
289 {
290 //
291 // Handle RDPMC's events, triggers and dispatches (emulate RDPMC)
292 //
293 DispatchEventRdpmc(VCpu);
294
295 break;
296 }
297 case VMX_EXIT_REASON_MOV_DR:
298 {
299 //
300 // Trigger, dispatch and handle the event
301 //
303
304 break;
305 }
306 case VMX_EXIT_REASON_EXECUTE_XSETBV:
307 {
308 //
309 // Dispatch and trigger the XSETBV instruction events
310 //
312
313 break;
314 }
315 case VMX_EXIT_REASON_VMX_PREEMPTION_TIMER_EXPIRED:
316 {
317 //
318 // Handle the VMX preemption timer vm-exit
319 //
321
322 break;
323 }
324 case VMX_EXIT_REASON_PAGE_MODIFICATION_LOG_FULL:
325 {
326 //
327 // Handle page-modification log
328 //
330
331 break;
332 }
333 default:
334 {
335 //
336 // Not handled vm-exit
337 //
338 LogError("Err, unknown vmexit, reason : 0x%llx", ExitReason);
339
340 break;
341 }
342 }
343
344 //
345 // Check whether we need to increment the guest's ip or not
346 // Also, we should not increment rip if a vmxoff executed
347 //
348 if (!VCpu->VmxoffState.IsVmxoffExecuted && VCpu->IncrementRip)
349 {
350 //
351 // If we are in transparent-mode, then we need to handle the trap flag as the result
352 // of an anti-hypervisor technique of using the trap flag after a VM-exit
353 // to detect the hypervisor
354 //
356 {
358 }
359
361 }
362
363 //
364 // Check for vmxoff request
365 //
367 {
368 Result = TRUE;
369 }
370
371 //
372 // Set indicator of Vmx non root mode to false
373 //
374 VCpu->IsOnVmxRootMode = FALSE;
375
376 //
377 // By default it's FALSE, if we want to exit vmx then it's TRUE
378 //
379 return Result;
380}
VOID VmxHandleTripleFaults(VIRTUAL_MACHINE_STATE *VCpu)
Handling triple fault VM-exits.
Definition CrossVmexits.c:70
VOID VmxHandleVmxPreemptionTimerVmexit(VIRTUAL_MACHINE_STATE *VCpu)
Handling VMX Preemption Timer vm-exits.
Definition CrossVmexits.c:53
VOID DirtyLoggingHandleVmexits(VIRTUAL_MACHINE_STATE *VCpu)
Handling vm-exits of PML.
Definition DirtyLogging.c:300
VOID DispatchEventException(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to EXCEPTION events.
Definition Dispatch.c:825
VOID DispatchEventXsetbv(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to XSETBV events.
Definition Dispatch.c:180
VOID DispatchEventRdpmc(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to RDPMC events.
Definition Dispatch.c:663
VOID DispatchEventRdmsr(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to RDMSR events.
Definition Dispatch.c:573
VOID DispatchEventCpuid(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to CPUID events.
Definition Dispatch.c:113
VOID DispatchEventTsc(VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN IsRdtscp)
Handling debugger functions related to RDTSC/RDTSCP events.
Definition Dispatch.c:248
VOID DispatchEventIO(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to IO events.
Definition Dispatch.c:499
VOID DispatchEventMov2DebugRegs(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to MOV 2 DR events.
Definition Dispatch.c:708
VOID DispatchEventVmcall(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to VMCALL events.
Definition Dispatch.c:301
VOID DispatchEventMovToFromControlRegisters(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to mov to/from CR events.
Definition Dispatch.c:761
VOID DispatchEventWrmsr(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to WRMSR events.
Definition Dispatch.c:618
VOID DispatchEventExternalInterrupts(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to external-interrupt events.
Definition Dispatch.c:920
BOOLEAN EptHandleEptViolation(VIRTUAL_MACHINE_STATE *VCpu)
Handle VM exits for EPT violations.
Definition Ept.c:1108
VOID EptHandleMisconfiguration(VOID)
Handle vm-exits for EPT Misconfiguration.
Definition Ept.c:1152
VOID EventInjectUndefinedOpcode(VIRTUAL_MACHINE_STATE *VCpu)
Inject UD to the guest (Invalid Opcode - Undefined Opcode).
Definition Events.c:79
VOID HvResumeToNextInstruction()
Add the current instruction length to guest rip to resume to next instruction.
Definition Hv.c:298
VOID IdtEmulationHandleInterruptWindowExiting(_Inout_ VIRTUAL_MACHINE_STATE *VCpu)
Handle interrupt-window exitings.
Definition IdtEmulation.c:662
VOID IdtEmulationHandleNmiWindowExiting(_Inout_ VIRTUAL_MACHINE_STATE *VCpu)
Handle NMI-window exitings.
Definition IdtEmulation.c:605
VOID MtfHandleVmexit(VIRTUAL_MACHINE_STATE *VCpu)
Handle Monitor Trap Flag vm-exits.
Definition Mtf.c:21
UCHAR VmxVmread64P(size_t Field, UINT64 *FieldValue)
VMX VMREAD instruction (64-bit, pointer variant).
Definition PlatformIntrinsicsVmx.c:207
UCHAR VmxVmread32P(size_t Field, UINT32 *FieldValue)
VMX VMREAD instruction (32-bit, pointer variant).
Definition PlatformIntrinsicsVmx.c:227
UCHAR BOOLEAN
Definition BasicTypes.h:35
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
unsigned int UINT32
Definition BasicTypes.h:54
char CHAR
Definition BasicTypes.h:33
IMPORT_EXPORT_HYPEREVADE VOID TransparentCheckAndTrapFlagAfterVmexit()
Handle anti-debugging method of a trap flag after a VM exit.
Definition VmxFootprints.c:153
#define LogError(format,...)
Log in the case of error.
Definition HyperDbgHyperLogIntrinsics.h:113
#define LogInfo(format,...)
Define log variables.
Definition HyperDbgHyperLogIntrinsics.h:71
struct _VIRTUAL_MACHINE_STATE VIRTUAL_MACHINE_STATE
The status of each core after and before VMX.
VIRTUAL_MACHINE_STATE * g_GuestState
Save the state and variables related to virtualization on each to logical core.
Definition GlobalVariables.h:38
BOOLEAN g_CheckForFootprints
Shows whether the footprints (anti-debugging and anti-hypervisor) should be checked or not.
Definition GlobalVariables.h:131
NULL()
Definition test-case-generator.py:530
UINT32 ExitQualification
Definition State.h:330
GUEST_XMM_REGS * XmmRegs
Definition State.h:327
BOOLEAN IncrementRip
Definition State.h:313
GUEST_REGS * Regs
Definition State.h:326
UINT32 ExitReason
Definition State.h:329
VMX_VMXOFF_STATE VmxoffState
Definition State.h:350
BOOLEAN IsOnVmxRootMode
Definition State.h:312
UINT64 LastVmexitRip
Definition State.h:331
BOOLEAN IsVmxoffExecuted
Definition State.h:168
Definition BasicTypes.h:136
UINT64 rsp
Definition BasicTypes.h:145
Definition BasicTypes.h:174