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

File for protected hypervisor resources. More...

#include "pch.h"

Functions

VOID ProtectedHvChangeExceptionBitmapWithIntegrityCheck (VIRTUAL_MACHINE_STATE *VCpu, UINT32 CurrentMask, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
 Add extra mask to this resource and write it.
VOID ProtectedHvSetExceptionBitmap (VIRTUAL_MACHINE_STATE *VCpu, UINT32 IdtIndex)
 Set exception bitmap in VMCS.
VOID ProtectedHvUnsetExceptionBitmap (VIRTUAL_MACHINE_STATE *VCpu, UINT32 IdtIndex)
 Unset exception bitmap in VMCS.
VOID ProtectedHvResetExceptionBitmapToClearEvents (VIRTUAL_MACHINE_STATE *VCpu)
 Reset exception bitmap in VMCS because of clearing !exception commands.
VOID ProtectedHvRemoveUndefinedInstructionForDisablingSyscallSysretCommands (VIRTUAL_MACHINE_STATE *VCpu)
 Reset exception bitmap in VMCS because of clearing !exception commands.
VOID ProtectedHvApplySetExternalInterruptExiting (VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
 Set the External Interrupt Exiting.
VOID ProtectedHvSetExternalInterruptExiting (VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set)
 Set the External Interrupt Exiting.
VOID ProtectedHvExternalInterruptExitingForDisablingInterruptCommands (VIRTUAL_MACHINE_STATE *VCpu)
 Clear events of !interrupt.
VOID ProtectedHvSetTscVmexit (VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
 Set vm-exit for tsc instructions (rdtsc/rdtscp).
VOID ProtectedHvSetMovDebugRegsVmexit (VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
 Set vm-exit for mov to debug registers.
VOID ProtectedHvSetMovToCrVmexit (BOOLEAN Set, UINT64 ControlRegister, UINT64 MaskRegister)
 Set vm-exit for mov to cr0 / cr4 register.
VOID ProtectedHvSetMovControlRegsVmexit (VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver, UINT64 ControlRegister, UINT64 MaskRegister)
 Set vm-exit for mov to control registers.
VOID ProtectedHvSetMovToCr3Vmexit (VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
 Set vm-exit for mov to cr3 register.
VOID ProtectedHvSetLoadDebugControlsIntegrityCheck (VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
 Set LOAD DEBUG CONTROLS on Vm-entry controls.
VOID ProtectedHvSetSaveDebugControlsIntegrityCheck (VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
 Set SAVE DEBUG CONTROLS on Vm-exit controls.
VOID ProtectedHvSetSaveDebugControls (VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set)
 Set LOAD DEBUG CONTROLS on VM-entry controls.
VOID ProtectedHvSetLoadDebugControls (VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set)
 Set SAVE DEBUG CONTROLS on VM-exit controls.
VOID ProtectedHvSetRdtscExiting (VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set)
 Set the RDTSC/P Exiting.
VOID ProtectedHvDisableRdtscExitingForDisablingTscCommands (VIRTUAL_MACHINE_STATE *VCpu)
 Clear events of !tsc.
VOID ProtectedHvSetMovDebugRegsExiting (VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set)
 Set MOV to HW Debug Regs Exiting.
VOID ProtectedHvDisableMovDebugRegsExitingForDisablingDrCommands (VIRTUAL_MACHINE_STATE *VCpu)
 Clear events of !dr.
VOID ProtectedHvDisableMovControlRegsExitingForDisablingCrCommands (VIRTUAL_MACHINE_STATE *VCpu, UINT64 ControlRegister, UINT64 MaskRegister)
 Clear events of !crwrite.
VOID ProtectedHvSetMov2Cr3Exiting (VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set)
 Set MOV to CR3 Exiting.
VOID ProtectedHvSetMov2CrExiting (BOOLEAN Set, UINT64 ControlRegister, UINT64 MaskRegister)
 Set MOV to CR0/4 Exiting.

Detailed Description

File for protected hypervisor resources.

Author
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)

Protected Hypervisor Routines are those resource that are used in different parts of the debugger or hypervisor, these resources need extra checks to avoid integrity problems

Version
0.1
Date
2021-10-04

Function Documentation

◆ ProtectedHvApplySetExternalInterruptExiting()

VOID ProtectedHvApplySetExternalInterruptExiting ( VIRTUAL_MACHINE_STATE * VCpu,
BOOLEAN Set,
PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver )

Set the External Interrupt Exiting.

Parameters
VCpuThe virtual processor's state
SetSet or unset the External Interrupt Exiting
PassOverAdds some pass over to the checks thus we won't check for interrupts
Returns
VOID
193{
194 UINT32 PinBasedControls = 0;
195 UINT32 VmExitControls = 0;
196
197 //
198 // The protected checks are only performed if the "Set" is "FALSE",
199 // because if sb wants to set it to "TRUE" then we're no need to
200 // worry about it as it remains enabled
201 //
202 if (Set == FALSE)
203 {
204 //
205 // Ask the top-level driver whether to terminate this operation or not
206 //
209 NULL,
210 PassOver))
211 {
212 return;
213 }
214 }
215
216 //
217 // In order to enable External Interrupt Exiting we have to set
218 // IA32_VMX_PINBASED_CTLS_EXTERNAL_INTERRUPT_EXITING_FLAG in vmx
219 // pin-based controls (PIN_BASED_VM_EXEC_CONTROL) and also
220 // we should enable IA32_VMX_EXIT_CTLS_ACKNOWLEDGE_INTERRUPT_ON_EXIT_FLAG
221 // on vmx vm-exit controls (VMCS_CTRL_VMEXIT_CONTROLS), also this function
222 // might not always be successful if the guest is not in the interruptible
223 // state so it wait for and interrupt-window exiting to re-inject
224 // the interrupt into the guest
225 //
226
227 //
228 // Read the previous flags
229 //
230 VmxVmread32P(VMCS_CTRL_PIN_BASED_VM_EXECUTION_CONTROLS, &PinBasedControls);
231 VmxVmread32P(VMCS_CTRL_PRIMARY_VMEXIT_CONTROLS, &VmExitControls);
232
233 if (Set)
234 {
235 PinBasedControls |= IA32_VMX_PINBASED_CTLS_EXTERNAL_INTERRUPT_EXITING_FLAG;
236 VmExitControls |= IA32_VMX_EXIT_CTLS_ACKNOWLEDGE_INTERRUPT_ON_EXIT_FLAG;
237 }
238 else
239 {
240 PinBasedControls &= ~IA32_VMX_PINBASED_CTLS_EXTERNAL_INTERRUPT_EXITING_FLAG;
241 VmExitControls &= ~IA32_VMX_EXIT_CTLS_ACKNOWLEDGE_INTERRUPT_ON_EXIT_FLAG;
242 }
243
244 //
245 // Set the new value
246 //
247 VmxVmwrite64(VMCS_CTRL_PIN_BASED_VM_EXECUTION_CONTROLS, PinBasedControls);
248 VmxVmwrite64(VMCS_CTRL_PRIMARY_VMEXIT_CONTROLS, VmExitControls);
249}
BOOLEAN VmmCallbackQueryTerminateProtectedResource(UINT32 CoreId, PROTECTED_HV_RESOURCES_TYPE ResourceType, PVOID Context, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
routine callback to query for termination of protected resources
Definition Callback.c:122
UCHAR VmxVmwrite64(size_t Field, UINT64 FieldValue)
VMX VMWRITE instruction (64-bit).
Definition PlatformIntrinsicsVmx.c:273
UCHAR VmxVmread32P(size_t Field, UINT32 *FieldValue)
VMX VMREAD instruction (32-bit, pointer variant).
Definition PlatformIntrinsicsVmx.c:227
#define FALSE
Definition BasicTypes.h:113
unsigned int UINT32
Definition BasicTypes.h:54
@ PROTECTED_HV_RESOURCES_EXTERNAL_INTERRUPT_EXITING
Definition Events.h:333
UINT32 CoreId
Definition State.h:328

◆ ProtectedHvChangeExceptionBitmapWithIntegrityCheck()

VOID ProtectedHvChangeExceptionBitmapWithIntegrityCheck ( VIRTUAL_MACHINE_STATE * VCpu,
UINT32 CurrentMask,
PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver )

Add extra mask to this resource and write it.

As exception bitmap is a protected resource, this routine makes sure that modifying exception bitmap won't break the debugger's integrity

Parameters
VCpuThe virtual processor's state
CurrentMaskThe mask that debugger wants to write
PassOverAdds some pass over to the checks thus we won't check for exceptions
Returns
VOID
32{
33 //
34 // Ask the top-level module to reshape the mask
35 //
38 &CurrentMask,
39 PassOver))
40 {
41 return;
42 }
43
44 //
45 // Check for syscall callback (masking #DBs and #BPs)
46 //
48 {
49 CurrentMask |= 1 << EXCEPTION_VECTOR_DEBUG_BREAKPOINT;
50 CurrentMask |= 1 << EXCEPTION_VECTOR_BREAKPOINT;
51 }
52
53 //
54 // Check for possible EPT Hooks (Hidden Breakpoints)
55 //
57 {
58 CurrentMask |= 1 << EXCEPTION_VECTOR_BREAKPOINT;
59 }
60
61 //
62 // Write the final value
63 //
64 HvWriteExceptionBitmap(CurrentMask);
65}
UINT32 EptHookGetCountOfEpthooks(BOOLEAN IsEptHook2)
get the length of active EPT hooks (!epthook and !epthook2)
Definition EptHook.c:1885
VOID HvWriteExceptionBitmap(UINT32 BitmapMask)
Write on exception bitmap in VMCS DO NOT CALL IT DIRECTLY, instead use HvSetExceptionBitmap.
Definition Hv.c:616
@ PROTECTED_HV_RESOURCES_EXCEPTION_BITMAP
Definition Events.h:331
@ EXCEPTION_VECTOR_DEBUG_BREAKPOINT
Definition Events.h:25
@ EXCEPTION_VECTOR_BREAKPOINT
Definition Events.h:27
BOOLEAN g_SyscallCallbackStatus
Shows whether the syscall callback is enabled or not.
Definition GlobalVariables.h:118

◆ ProtectedHvDisableMovControlRegsExitingForDisablingCrCommands()

VOID ProtectedHvDisableMovControlRegsExitingForDisablingCrCommands ( VIRTUAL_MACHINE_STATE * VCpu,
UINT64 ControlRegister,
UINT64 MaskRegister )

Clear events of !crwrite.

Parameters
VCpuThe virtual processor's state
ControlRegister
MaskRegister
Returns
VOID
723{
725}
VOID ProtectedHvSetMovControlRegsVmexit(VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver, UINT64 ControlRegister, UINT64 MaskRegister)
Set vm-exit for mov to control registers.
Definition ProtectedHv.c:438
@ PASSING_OVER_MOV_TO_CONTROL_REGS_EVENTS
Definition Events.h:321

◆ ProtectedHvDisableMovDebugRegsExitingForDisablingDrCommands()

VOID ProtectedHvDisableMovDebugRegsExitingForDisablingDrCommands ( VIRTUAL_MACHINE_STATE * VCpu)

Clear events of !dr.

Parameters
VCpuThe virtual processor's state
Returns
VOID
709{
711}
VOID ProtectedHvSetMovDebugRegsVmexit(VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
Set vm-exit for mov to debug registers.
Definition ProtectedHv.c:341
@ PASSING_OVER_MOV_TO_HW_DEBUG_REGS_EVENTS
Definition Events.h:316

◆ ProtectedHvDisableRdtscExitingForDisablingTscCommands()

VOID ProtectedHvDisableRdtscExitingForDisablingTscCommands ( VIRTUAL_MACHINE_STATE * VCpu)

Clear events of !tsc.

Parameters
VCpuThe virtual processor's state
Returns
VOID
684{
686}
VOID ProtectedHvSetTscVmexit(VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
Set vm-exit for tsc instructions (rdtsc/rdtscp).
Definition ProtectedHv.c:287
@ PASSING_OVER_TSC_EVENTS
Definition Events.h:311

◆ ProtectedHvExternalInterruptExitingForDisablingInterruptCommands()

VOID ProtectedHvExternalInterruptExitingForDisablingInterruptCommands ( VIRTUAL_MACHINE_STATE * VCpu)

Clear events of !interrupt.

Returns
VOID
271{
273}
VOID ProtectedHvApplySetExternalInterruptExiting(VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
Set the External Interrupt Exiting.
Definition ProtectedHv.c:192
@ PASSING_OVER_INTERRUPT_EVENTS
Definition Events.h:306

◆ ProtectedHvRemoveUndefinedInstructionForDisablingSyscallSysretCommands()

VOID ProtectedHvRemoveUndefinedInstructionForDisablingSyscallSysretCommands ( VIRTUAL_MACHINE_STATE * VCpu)

Reset exception bitmap in VMCS because of clearing !exception commands.

Should be called in vmx-root

Parameters
VCpuThe virtual processor's state
Returns
VOID
162{
163 UINT32 ExceptionBitmap = 0;
164
165 //
166 // Read the current bitmap
167 //
168 ExceptionBitmap = HvReadExceptionBitmap();
169
170 //
171 // Unset exception bitmap for #UD
172 //
173 ExceptionBitmap &= ~(1 << EXCEPTION_VECTOR_UNDEFINED_OPCODE);
174
175 //
176 // Set the new value
177 //
179}
UINT32 HvReadExceptionBitmap()
Read exception bitmap in VMCS.
Definition Hv.c:631
VOID ProtectedHvChangeExceptionBitmapWithIntegrityCheck(VIRTUAL_MACHINE_STATE *VCpu, UINT32 CurrentMask, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
Add extra mask to this resource and write it.
Definition ProtectedHv.c:31
@ PASSING_OVER_UD_EXCEPTIONS_FOR_SYSCALL_SYSRET_HOOK
Definition Events.h:300
@ EXCEPTION_VECTOR_UNDEFINED_OPCODE
Definition Events.h:30

◆ ProtectedHvResetExceptionBitmapToClearEvents()

VOID ProtectedHvResetExceptionBitmapToClearEvents ( VIRTUAL_MACHINE_STATE * VCpu)

Reset exception bitmap in VMCS because of clearing !exception commands.

Should be called in vmx-root

Parameters
VCpuThe virtual processor's state
Returns
VOID
143{
144 UINT32 ExceptionBitmap = 0;
145
146 //
147 // Set the new value
148 //
150}
@ PASSING_OVER_EXCEPTION_EVENTS
Definition Events.h:301

◆ ProtectedHvSetExceptionBitmap()

VOID ProtectedHvSetExceptionBitmap ( VIRTUAL_MACHINE_STATE * VCpu,
UINT32 IdtIndex )

Set exception bitmap in VMCS.

Should be called in vmx-root

Parameters
VCpuThe virtual processor's state
IdtIndexInterrupt Descriptor Table index of exception
Returns
VOID
77{
78 UINT32 ExceptionBitmap = 0;
79
80 //
81 // Read the current bitmap
82 //
83 ExceptionBitmap = HvReadExceptionBitmap();
84
86 {
87 ExceptionBitmap = 0xffffffff;
88 }
89 else
90 {
91 ExceptionBitmap |= 1 << IdtIndex;
92 }
93
94 //
95 // Set the new value
96 //
98}
#define DEBUGGER_EVENT_EXCEPTIONS_ALL_FIRST_32_ENTRIES
Apply to all first 32 exceptions.
Definition Constants.h:757
@ PASSING_OVER_NONE
Definition Events.h:299

◆ ProtectedHvSetExternalInterruptExiting()

VOID ProtectedHvSetExternalInterruptExiting ( VIRTUAL_MACHINE_STATE * VCpu,
BOOLEAN Set )

Set the External Interrupt Exiting.

Parameters
VCpuThe virtual processor's state
SetSet or unset the External Interrupt Exiting
Returns
VOID

◆ ProtectedHvSetLoadDebugControls()

VOID ProtectedHvSetLoadDebugControls ( VIRTUAL_MACHINE_STATE * VCpu,
BOOLEAN Set )

Set SAVE DEBUG CONTROLS on VM-exit controls.

Parameters
VCpu
SetSet or unset
Returns
VOID
659{
661}
VOID ProtectedHvSetLoadDebugControlsIntegrityCheck(VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
Set LOAD DEBUG CONTROLS on Vm-entry controls.
Definition ProtectedHv.c:539

◆ ProtectedHvSetLoadDebugControlsIntegrityCheck()

VOID ProtectedHvSetLoadDebugControlsIntegrityCheck ( VIRTUAL_MACHINE_STATE * VCpu,
BOOLEAN Set,
PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver )

Set LOAD DEBUG CONTROLS on Vm-entry controls.

Parameters
VCpu
SetSet or unset
PassOver
Returns
VOID
540{
541 UINT32 VmentryControls = 0;
542
543 //
544 // The protected checks are only performed if the "Set" is "FALSE",
545 // because if sb wants to set it to "TRUE" then we're no need to
546 // worry about it as it remains enabled
547 //
548 if (Set == FALSE)
549 {
550 //
551 // Check the top-level driver's state
552 //
555 NULL,
556 PassOver))
557 {
558 return;
559 }
560 }
561
562 //
563 // Read the previous flags
564 //
565 VmxVmread32P(VMCS_CTRL_VMENTRY_CONTROLS, &VmentryControls);
566
567 if (Set)
568 {
569 VmentryControls |= IA32_VMX_ENTRY_CTLS_LOAD_DEBUG_CONTROLS_FLAG;
570 }
571 else
572 {
573 VmentryControls &= ~IA32_VMX_ENTRY_CTLS_LOAD_DEBUG_CONTROLS_FLAG;
574 }
575
576 //
577 // Set the new value
578 //
579 VmxVmwrite32(VMCS_CTRL_VMENTRY_CONTROLS, VmentryControls);
580}
UCHAR VmxVmwrite32(size_t Field, UINT32 FieldValue)
VMX VMWRITE instruction (32-bit).
Definition PlatformIntrinsicsVmx.c:293
@ PROTECTED_HV_RESOURCES_SAVE_AND_LOAD_DEBUG_CONTROLS
Definition Events.h:343

◆ ProtectedHvSetMov2Cr3Exiting()

VOID ProtectedHvSetMov2Cr3Exiting ( VIRTUAL_MACHINE_STATE * VCpu,
BOOLEAN Set )

Set MOV to CR3 Exiting.

Parameters
VCpuThe virtual processor's state
SetSet or unset the MOV to CR3 Exiting
Returns
VOID
736{
738}
VOID ProtectedHvSetMovToCr3Vmexit(VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
Set vm-exit for mov to cr3 register.
Definition ProtectedHv.c:474

◆ ProtectedHvSetMov2CrExiting()

VOID ProtectedHvSetMov2CrExiting ( BOOLEAN Set,
UINT64 ControlRegister,
UINT64 MaskRegister )

Set MOV to CR0/4 Exiting.

Parameters
Setor unset the MOV to CR0/4 Exiting
ControlRegister
MaskRegister
Returns
VOID
750{
751 ProtectedHvSetMovToCrVmexit(Set, ControlRegister, MaskRegister);
752}
VOID ProtectedHvSetMovToCrVmexit(BOOLEAN Set, UINT64 ControlRegister, UINT64 MaskRegister)
Set vm-exit for mov to cr0 / cr4 register.
Definition ProtectedHv.c:395

◆ ProtectedHvSetMovControlRegsVmexit()

VOID ProtectedHvSetMovControlRegsVmexit ( VIRTUAL_MACHINE_STATE * VCpu,
BOOLEAN Set,
PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver,
UINT64 ControlRegister,
UINT64 MaskRegister )

Set vm-exit for mov to control registers.

Should be called in vmx-root

Parameters
VCpuThe virtual processor's state
Setor unset the vm-exits
PassOverAdds some pass over to the checks
ControlRegister
MaskRegister
Returns
VOID
439{
440 //
441 // The protected checks are only performed if the "Set" is "FALSE",
442 // because if sb wants to set it to "TRUE" then we're no need to
443 // worry about it as it remains enabled
444 //
445 if (Set == FALSE)
446 {
447 //
448 // Check the state of top-level driver
449 //
452 NULL,
453 PassOver))
454 {
455 return;
456 }
457 }
458
459 ProtectedHvSetMovToCrVmexit(Set, ControlRegister, MaskRegister);
460}
@ PROTECTED_HV_RESOURCES_MOV_CONTROL_REGISTER_EXITING
Definition Events.h:339

◆ ProtectedHvSetMovDebugRegsExiting()

VOID ProtectedHvSetMovDebugRegsExiting ( VIRTUAL_MACHINE_STATE * VCpu,
BOOLEAN Set )

Set MOV to HW Debug Regs Exiting.

Parameters
VCpuThe virtual processor's state
SetSet or unset the MOV to HW Debug Regs Exiting
Returns
VOID

◆ ProtectedHvSetMovDebugRegsVmexit()

VOID ProtectedHvSetMovDebugRegsVmexit ( VIRTUAL_MACHINE_STATE * VCpu,
BOOLEAN Set,
PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver )

Set vm-exit for mov to debug registers.

Should be called in vmx-root

Parameters
VCpuThe virtual processor's state
SetSet or unset the vm-exits
PassOverAdds some pass over to the checks thus we won't check for dr
Returns
VOID
342{
343 UINT32 CpuBasedVmExecControls = 0;
344
345 //
346 // The protected checks are only performed if the "Set" is "FALSE",
347 // because if sb wants to set it to "TRUE" then we're no need to
348 // worry about it as it remains enabled
349 //
350 if (Set == FALSE)
351 {
352 //
353 // Check the top-level driver's state
354 //
357 NULL,
358 PassOver))
359 {
360 return;
361 }
362 }
363
364 //
365 // Read the previous flags
366 //
367 VmxVmread32P(VMCS_CTRL_PROCESSOR_BASED_VM_EXECUTION_CONTROLS, &CpuBasedVmExecControls);
368
369 if (Set)
370 {
371 CpuBasedVmExecControls |= IA32_VMX_PROCBASED_CTLS_MOV_DR_EXITING_FLAG;
372 }
373 else
374 {
375 CpuBasedVmExecControls &= ~IA32_VMX_PROCBASED_CTLS_MOV_DR_EXITING_FLAG;
376 }
377
378 //
379 // Set the new value
380 //
381 VmxVmwrite64(VMCS_CTRL_PROCESSOR_BASED_VM_EXECUTION_CONTROLS, CpuBasedVmExecControls);
382}
@ PROTECTED_HV_RESOURCES_MOV_TO_DEBUG_REGISTER_EXITING
Definition Events.h:337

◆ ProtectedHvSetMovToCr3Vmexit()

VOID ProtectedHvSetMovToCr3Vmexit ( VIRTUAL_MACHINE_STATE * VCpu,
BOOLEAN Set,
PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver )

Set vm-exit for mov to cr3 register.

Should be called in vmx-root

Parameters
VCpuThe virtual processor's state
SetSet or unset the vm-exits
PassOverAdds some pass over to the checks thus we won't check for dr
Returns
VOID
475{
476 UINT32 CpuBasedVmExecControls = 0;
477
478 //
479 // The protected checks are only performed if the "Set" is "FALSE",
480 // because if sb wants to set it to "TRUE" then we're no need to
481 // worry about it as it remains enabled
482 //
483 if (Set == FALSE)
484 {
485 //
486 // Check the top-level driver's state
487 //
490 NULL,
491 PassOver))
492 {
493 return;
494 }
495
496 //
497 // Check if the trap execution is enabled or not, and whether the
498 // uninitialization phase started or not
499 //
501 {
502 //
503 // The VMM needs mov2cr3s
504 //
505 return;
506 }
507 }
508
509 //
510 // Read the previous flags
511 //
512 VmxVmread32P(VMCS_CTRL_PROCESSOR_BASED_VM_EXECUTION_CONTROLS, &CpuBasedVmExecControls);
513
514 if (Set)
515 {
516 CpuBasedVmExecControls |= IA32_VMX_PROCBASED_CTLS_CR3_LOAD_EXITING_FLAG;
517 }
518 else
519 {
520 CpuBasedVmExecControls &= ~IA32_VMX_PROCBASED_CTLS_CR3_LOAD_EXITING_FLAG;
521 }
522
523 //
524 // Set the new value
525 //
526 VmxVmwrite64(VMCS_CTRL_PROCESSOR_BASED_VM_EXECUTION_CONTROLS, CpuBasedVmExecControls);
527}
@ PROTECTED_HV_RESOURCES_MOV_TO_CR3_EXITING
Definition Events.h:341
BOOLEAN g_ExecTrapUnInitializationStarted
Showes whether the uninitialization of the exec trap is started or not.
Definition GlobalVariables.h:169
BOOLEAN g_ExecTrapInitialized
Showes whether the execution trap handler is allowed to trigger an event or not.
Definition GlobalVariables.h:162

◆ ProtectedHvSetMovToCrVmexit()

VOID ProtectedHvSetMovToCrVmexit ( BOOLEAN Set,
UINT64 ControlRegister,
UINT64 MaskRegister )

Set vm-exit for mov to cr0 / cr4 register.

Should be called in vmx-root

Parameters
Setor unset the vm-exits
ControlRegister
MaskRegister
Returns
VOID
396{
397 if (ControlRegister == VMX_EXIT_QUALIFICATION_REGISTER_CR0)
398 {
399 if (Set)
400 {
401 VmxVmwrite64(VMCS_CTRL_CR0_GUEST_HOST_MASK, MaskRegister);
402 VmxVmwrite64(VMCS_CTRL_CR0_READ_SHADOW, CpuReadCr0());
403 }
404 else
405 {
406 VmxVmwrite64(VMCS_CTRL_CR0_GUEST_HOST_MASK, 0);
407 VmxVmwrite64(VMCS_CTRL_CR0_READ_SHADOW, 0);
408 }
409 }
410 else if (ControlRegister == VMX_EXIT_QUALIFICATION_REGISTER_CR4)
411 {
412 if (Set)
413 {
414 VmxVmwrite64(VMCS_CTRL_CR4_GUEST_HOST_MASK, MaskRegister);
415 VmxVmwrite64(VMCS_CTRL_CR4_READ_SHADOW, CpuReadCr0());
416 }
417 else
418 {
419 VmxVmwrite64(VMCS_CTRL_CR4_GUEST_HOST_MASK, 0);
420 VmxVmwrite64(VMCS_CTRL_CR4_READ_SHADOW, 0);
421 }
422 }
423}
ULONG_PTR CpuReadCr0(VOID)
Read CR0.
Definition PlatformIntrinsics.c:28

◆ ProtectedHvSetRdtscExiting()

VOID ProtectedHvSetRdtscExiting ( VIRTUAL_MACHINE_STATE * VCpu,
BOOLEAN Set )

Set the RDTSC/P Exiting.

Parameters
VCpuThe virtual processor's state
SetSet or unset the RDTSC/P Exiting
Returns
VOID
672{
674}

◆ ProtectedHvSetSaveDebugControls()

VOID ProtectedHvSetSaveDebugControls ( VIRTUAL_MACHINE_STATE * VCpu,
BOOLEAN Set )

Set LOAD DEBUG CONTROLS on VM-entry controls.

Parameters
VCpu
SetSet or unset
Returns
VOID
645{
647}
VOID ProtectedHvSetSaveDebugControlsIntegrityCheck(VIRTUAL_MACHINE_STATE *VCpu, BOOLEAN Set, PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver)
Set SAVE DEBUG CONTROLS on Vm-exit controls.
Definition ProtectedHv.c:592

◆ ProtectedHvSetSaveDebugControlsIntegrityCheck()

VOID ProtectedHvSetSaveDebugControlsIntegrityCheck ( VIRTUAL_MACHINE_STATE * VCpu,
BOOLEAN Set,
PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver )

Set SAVE DEBUG CONTROLS on Vm-exit controls.

Parameters
VCpu
SetSet or unset
PassOver
Returns
VOID
593{
594 UINT32 VmexitControls = 0;
595
596 //
597 // The protected checks are only performed if the "Set" is "FALSE",
598 // because if sb wants to set it to "TRUE" then we're no need to
599 // worry about it as it remains enabled
600 //
601 if (Set == FALSE)
602 {
603 //
604 // Check the top-level driver's state
605 //
608 NULL,
609 PassOver))
610 {
611 return;
612 }
613 }
614
615 //
616 // Read the previous flags
617 //
618 VmxVmread32P(VMCS_CTRL_PRIMARY_VMEXIT_CONTROLS, &VmexitControls);
619
620 if (Set)
621 {
622 VmexitControls |= IA32_VMX_EXIT_CTLS_SAVE_DEBUG_CONTROLS_FLAG;
623 }
624 else
625 {
626 VmexitControls &= ~IA32_VMX_EXIT_CTLS_SAVE_DEBUG_CONTROLS_FLAG;
627 }
628
629 //
630 // Set the new value
631 //
632 VmxVmwrite32(VMCS_CTRL_PRIMARY_VMEXIT_CONTROLS, VmexitControls);
633}

◆ ProtectedHvSetTscVmexit()

VOID ProtectedHvSetTscVmexit ( VIRTUAL_MACHINE_STATE * VCpu,
BOOLEAN Set,
PROTECTED_HV_RESOURCES_PASSING_OVERS PassOver )

Set vm-exit for tsc instructions (rdtsc/rdtscp).

Should be called in vmx-root

Parameters
VCpuThe virtual processor's state
SetSet or unset the vm-exits
PassOverAdds some pass over to the checks thus we won't check for tsc
Returns
VOID
288{
289 UINT32 CpuBasedVmExecControls = 0;
290
291 //
292 // The protected checks are only performed if the "Set" is "FALSE",
293 // because if sb wants to set it to "TRUE" then we're no need to
294 // worry about it as it remains enabled
295 //
296 if (Set == FALSE)
297 {
298 //
299 // Check the top-level driver's state
300 //
303 NULL,
304 PassOver))
305 {
306 return;
307 }
308 }
309
310 //
311 // Read the previous flags
312 //
313 VmxVmread32P(VMCS_CTRL_PROCESSOR_BASED_VM_EXECUTION_CONTROLS, &CpuBasedVmExecControls);
314
315 if (Set)
316 {
317 CpuBasedVmExecControls |= IA32_VMX_PROCBASED_CTLS_RDTSC_EXITING_FLAG;
318 }
319 else
320 {
321 CpuBasedVmExecControls &= ~IA32_VMX_PROCBASED_CTLS_RDTSC_EXITING_FLAG;
322 }
323 //
324 // Set the new value
325 //
326 VmxVmwrite64(VMCS_CTRL_PROCESSOR_BASED_VM_EXECUTION_CONTROLS, CpuBasedVmExecControls);
327}
@ PROTECTED_HV_RESOURCES_RDTSC_RDTSCP_EXITING
Definition Events.h:335

◆ ProtectedHvUnsetExceptionBitmap()

VOID ProtectedHvUnsetExceptionBitmap ( VIRTUAL_MACHINE_STATE * VCpu,
UINT32 IdtIndex )

Unset exception bitmap in VMCS.

Should be called in vmx-root

Parameters
VCpuThe virtual processor's state
IdtIndexInterrupt Descriptor Table index of exception
Returns
VOID
110{
111 UINT32 ExceptionBitmap = 0;
112
113 //
114 // Read the current bitmap
115 //
116 ExceptionBitmap = HvReadExceptionBitmap();
117
119 {
120 ExceptionBitmap = 0x0;
121 }
122 else
123 {
124 ExceptionBitmap &= ~(1 << IdtIndex);
125 }
126
127 //
128 // Set the new value
129 //
131}