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;
26 BOOLEAN ShouldEmulateRdtscp = TRUE;
28
29 //
30 // *********** SEND MESSAGE AFTER WE SET THE STATE ***********
31 //
32 VCpu = &g_GuestState[KeGetCurrentProcessorNumberEx(NULL)];
33
34 //
35 // Set the registers
36 //
37 VCpu->Regs = GuestRegs;
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 // Check if we're operating in transparent-mode or not
52 // If yes then we start operating in transparent-mode
53 //
55 {
56 ShouldEmulateRdtscp = TransparentModeStart(VCpu, ExitReason);
57 }
58
59 //
60 // Increase the RIP by default
61 //
62 VCpu->IncrementRip = TRUE;
63
64 //
65 // Save the current rip
66 //
67 __vmx_vmread(VMCS_GUEST_RIP, &VCpu->LastVmexitRip);
68
69 //
70 // Set the rsp in general purpose registers structure
71 //
72 __vmx_vmread(VMCS_GUEST_RSP, &VCpu->Regs->rsp);
73
74 //
75 // Read the exit qualification
76 //
77 VmxVmread32P(VMCS_EXIT_QUALIFICATION, &VCpu->ExitQualification);
78
79 //
80 // Debugging purpose
81 //
82 // LogInfo("VM_EXIT_REASON : 0x%x", ExitReason);
83 // LogInfo("VMCS_EXIT_QUALIFICATION : 0x%llx", VCpu->ExitQualification);
84 //
85 switch (ExitReason)
86 {
87 case VMX_EXIT_REASON_TRIPLE_FAULT:
88 {
90
91 break;
92 }
93 //
94 // 25.1.2 Instructions That Cause VM Exits Unconditionally
95 // The following instructions cause VM exits when they are executed in VMX non-root operation: CPUID, GETSEC,
96 // INVD, and XSETBV. This is also true of instructions introduced with VMX, which include: INVEPT, INVVPID,
97 // VMCALL, VMCLEAR, VMLAUNCH, VMPTRLD, VMPTRST, VMRESUME, VMXOFF, and VMXON.
98 //
99
100 case VMX_EXIT_REASON_EXECUTE_VMCLEAR:
101 case VMX_EXIT_REASON_EXECUTE_VMPTRLD:
102 case VMX_EXIT_REASON_EXECUTE_VMPTRST:
103 case VMX_EXIT_REASON_EXECUTE_VMREAD:
104 case VMX_EXIT_REASON_EXECUTE_VMRESUME:
105 case VMX_EXIT_REASON_EXECUTE_VMWRITE:
106 case VMX_EXIT_REASON_EXECUTE_VMXOFF:
107 case VMX_EXIT_REASON_EXECUTE_VMXON:
108 case VMX_EXIT_REASON_EXECUTE_VMLAUNCH:
109 {
110 //
111 // cf=1 indicate vm instructions fail
112 //
113 // UINT64 Rflags = 0;
114 // __vmx_vmread(VMCS_GUEST_RFLAGS, &Rflags);
115 // VmxVmwrite64(VMCS_GUEST_RFLAGS, Rflags | 0x1);
116
117 //
118 // Handle unconditional vm-exits (inject #ud)
119 //
121
122 break;
123 }
124 case VMX_EXIT_REASON_EXECUTE_INVEPT:
125 case VMX_EXIT_REASON_EXECUTE_INVVPID:
126 case VMX_EXIT_REASON_EXECUTE_GETSEC:
127 case VMX_EXIT_REASON_EXECUTE_INVD:
128 {
129 //
130 // Handle unconditional vm-exits (inject #ud)
131 //
133
134 break;
135 }
136 case VMX_EXIT_REASON_MOV_CR:
137 {
138 //
139 // Handle vm-exit, events, dispatches and perform changes from CR access
140 //
142
143 break;
144 }
145 case VMX_EXIT_REASON_EXECUTE_RDMSR:
146 {
147 //
148 // Handle vm-exit, events, dispatches and perform changes
149 //
150 DispatchEventRdmsr(VCpu);
151
152 break;
153 }
154 case VMX_EXIT_REASON_EXECUTE_WRMSR:
155 {
156 //
157 // Handle vm-exit, events, dispatches and perform changes
158 //
159 DispatchEventWrmsr(VCpu);
160
161 break;
162 }
163 case VMX_EXIT_REASON_EXECUTE_CPUID:
164 {
165 //
166 // Dispatch and trigger the CPUID instruction events
167 //
168 DispatchEventCpuid(VCpu);
169
170 break;
171 }
172
173 case VMX_EXIT_REASON_EXECUTE_IO_INSTRUCTION:
174 {
175 //
176 // Dispatch and trigger the I/O instruction events
177 //
178 DispatchEventIO(VCpu);
179
180 break;
181 }
182 case VMX_EXIT_REASON_EPT_VIOLATION:
183 {
184 if (EptHandleEptViolation(VCpu) == FALSE)
185 {
186 LogError("Err, there were errors in handling EPT violation");
187 }
188
189 break;
190 }
191 case VMX_EXIT_REASON_EPT_MISCONFIGURATION:
192 {
194
195 break;
196 }
197 case VMX_EXIT_REASON_EXECUTE_VMCALL:
198 {
199 //
200 // Handle vm-exits of VMCALLs
201 //
203
204 break;
205 }
206 case VMX_EXIT_REASON_EXCEPTION_OR_NMI:
207 {
208 //
209 // Handle the EXCEPTION injection/emulation
210 //
212
213 break;
214 }
215 case VMX_EXIT_REASON_EXTERNAL_INTERRUPT:
216 {
217 //
218 // Call the external-interrupt handler
219 //
221
222 break;
223 }
224 case VMX_EXIT_REASON_INTERRUPT_WINDOW:
225 {
226 //
227 // Call the interrupt-window exiting handler to re-inject the previous
228 // interrupts or disable the interrupt-window exiting bit
229 //
231
232 break;
233 }
234 case VMX_EXIT_REASON_NMI_WINDOW:
235 {
236 //
237 // Call the NMI-window exiting handler
238 //
240
241 break;
242 }
243 case VMX_EXIT_REASON_MONITOR_TRAP_FLAG:
244 {
245 //
246 // General handler to monitor trap flags (MTF)
247 //
248 MtfHandleVmexit(VCpu);
249
250 break;
251 }
252 case VMX_EXIT_REASON_EXECUTE_HLT:
253 {
254 //
255 // We don't wanna halt
256 //
257
258 //
259 //__halt();
260 //
261 break;
262 }
263 case VMX_EXIT_REASON_EXECUTE_RDTSC:
264 case VMX_EXIT_REASON_EXECUTE_RDTSCP:
265
266 {
267 //
268 // Check whether we are allowed to change the registers
269 // and emulate rdtsc or not
270 // Note : Using !tsc command in transparent-mode is not allowed
271 //
272 if (ShouldEmulateRdtscp)
273 {
274 DispatchEventTsc(VCpu, ExitReason == VMX_EXIT_REASON_EXECUTE_RDTSCP ? TRUE : FALSE);
275 }
276
277 break;
278 }
279 case VMX_EXIT_REASON_EXECUTE_RDPMC:
280 {
281 //
282 // Handle RDPMC's events, triggers and dispatches (emulate RDPMC)
283 //
284 DispatchEventRdpmc(VCpu);
285
286 break;
287 }
288 case VMX_EXIT_REASON_MOV_DR:
289 {
290 //
291 // Trigger, dispatch and handle the event
292 //
294
295 break;
296 }
297 case VMX_EXIT_REASON_EXECUTE_XSETBV:
298 {
299 //
300 // Handle xsetbv (unconditional vm-exit)
301 //
302 VmxHandleXsetbv(VCpu);
303
304 break;
305 }
306 case VMX_EXIT_REASON_VMX_PREEMPTION_TIMER_EXPIRED:
307 {
308 //
309 // Handle the VMX preemption timer vm-exit
310 //
312
313 break;
314 }
315 case VMX_EXIT_REASON_PAGE_MODIFICATION_LOG_FULL:
316 {
317 //
318 // Handle page-modification log
319 //
321
322 break;
323 }
324 default:
325 {
326 LogError("Err, unknown vmexit, reason : 0x%llx", ExitReason);
327
328 break;
329 }
330 }
331
332 //
333 // Check whether we need to increment the guest's ip or not
334 // Also, we should not increment rip if a vmxoff executed
335 //
336 if (!VCpu->VmxoffState.IsVmxoffExecuted && VCpu->IncrementRip)
337 {
339 }
340
341 //
342 // Check for vmxoff request
343 //
345 {
346 Result = TRUE;
347 }
348
349 //
350 // Restore the previous time
351 //
353 {
354 if (ExitReason != VMX_EXIT_REASON_EXECUTE_RDTSC && ExitReason != VMX_EXIT_REASON_EXECUTE_RDTSCP && ExitReason != VMX_EXIT_REASON_EXECUTE_CPUID)
355 {
356 //
357 // We not wanna change the global timer while RDTSC and RDTSCP
358 // was the reason of vm-exit
359 //
361 }
362 }
363
364 //
365 // Set indicator of Vmx non root mode to false
366 //
367 VCpu->IsOnVmxRootMode = FALSE;
368
369 //
370 // By default it's FALSE, if we want to exit vmx then it's TRUE
371 //
372 return Result;
373}
UCHAR BOOLEAN
Definition BasicTypes.h:39
#define TRUE
Definition BasicTypes.h:55
#define FALSE
Definition BasicTypes.h:54
unsigned int UINT32
Definition BasicTypes.h:48
VOID VmxHandleTripleFaults(VIRTUAL_MACHINE_STATE *VCpu)
Handling triple fault VM-exits.
Definition CrossVmexits.c:50
VOID VmxHandleVmxPreemptionTimerVmexit(VIRTUAL_MACHINE_STATE *VCpu)
Handling VMX Preemption Timer vm-exits.
Definition CrossVmexits.c:33
VOID VmxHandleXsetbv(VIRTUAL_MACHINE_STATE *VCpu)
Handling XSETBV Instruction vm-exits.
Definition CrossVmexits.c:21
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:755
VOID DispatchEventRdpmc(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to RDPMC events.
Definition Dispatch.c:589
VOID DispatchEventRdmsr(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to RDMSR events.
Definition Dispatch.c:499
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:194
VOID DispatchEventIO(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to IO events.
Definition Dispatch.c:425
VOID DispatchEventMov2DebugRegs(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to MOV 2 DR events.
Definition Dispatch.c:634
VOID DispatchEventVmcall(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to VMCALL events.
Definition Dispatch.c:248
VOID DispatchEventMovToFromControlRegisters(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to mov to/from CR events.
Definition Dispatch.c:691
VOID DispatchEventWrmsr(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to WRMSR events.
Definition Dispatch.c:544
VOID DispatchEventExternalInterrupts(VIRTUAL_MACHINE_STATE *VCpu)
Handling debugger functions related to external-interrupt events.
Definition Dispatch.c:850
BOOLEAN EptHandleEptViolation(VIRTUAL_MACHINE_STATE *VCpu)
Handle VM exits for EPT violations.
Definition Ept.c:1002
VOID EptHandleMisconfiguration(VOID)
Handle vm-exits for EPT Misconfiguration.
Definition Ept.c:1046
VOID EventInjectUndefinedOpcode(VIRTUAL_MACHINE_STATE *VCpu)
Inject #UD to the guest (Invalid Opcode - Undefined Opcode)
Definition Events.c:79
VIRTUAL_MACHINE_STATE * g_GuestState
Save the state and variables related to virtualization on each to logical core.
Definition GlobalVariables.h:38
BOOLEAN g_TransparentMode
Shows whether the debugger transparent mode is enabled (true) or not (false)
Definition GlobalVariables.h:75
VOID HvResumeToNextInstruction()
Add the current instruction length to guest rip to resume to next instruction.
Definition Hv.c:302
#define LogError(format,...)
Log in the case of error.
Definition HyperDbgHyperLogIntrinsics.h:113
VOID IdtEmulationHandleInterruptWindowExiting(_Inout_ VIRTUAL_MACHINE_STATE *VCpu)
Handle interrupt-window exitings.
Definition IdtEmulation.c:565
VOID IdtEmulationHandleNmiWindowExiting(_Inout_ VIRTUAL_MACHINE_STATE *VCpu)
Handle NMI-window exitings.
Definition IdtEmulation.c:508
VOID MtfHandleVmexit(VIRTUAL_MACHINE_STATE *VCpu)
Handle Monitor Trap Flag vm-exits.
Definition Mtf.c:21
BOOLEAN TransparentModeStart(VIRTUAL_MACHINE_STATE *VCpu, UINT32 ExitReason)
VM-Exit handler for different exit reasons.
Definition Transparency.c:493
UCHAR VmxVmread32P(size_t Field, UINT32 *FieldValue)
VMX VMREAD instruction (32-bit)
Definition Vmx.c:86
#define MSR_IA32_TIME_STAMP_COUNTER
IA32_TIME_STAMP_COUNTER MSR (rcx)
Definition Transparency.h:29
NULL()
Definition test-case-generator.py:530
The status of each core after and before VMX.
Definition State.h:290
UINT32 ExitQualification
Definition State.h:308
BOOLEAN IncrementRip
Definition State.h:292
GUEST_REGS * Regs
Definition State.h:305
VMX_VMXOFF_STATE VmxoffState
Definition State.h:328
BOOLEAN IsOnVmxRootMode
Definition State.h:291
VM_EXIT_TRANSPARENCY TransparencyState
Definition State.h:330
UINT64 LastVmexitRip
Definition State.h:309
UINT64 PreviousTimeStampCounter
Definition State.h:138
BOOLEAN IsVmxoffExecuted
Definition State.h:152
UINT64 rsp
Definition BasicTypes.h:79