You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bluepill/exit_handler.cpp

102 lines
2.5 KiB

4 years ago
#include "vmxexit_handler.h"
auto vmresume_failure() -> void
{
size_t value;
__vmx_vmread(VMCS_VM_INSTRUCTION_ERROR, &value);
dbg::print("> vmresume error... reason = 0x%x\n", value);
}
4 years ago
auto exit_handler(hv::pguest_registers regs) -> void
{
u64 exit_reason;
__vmx_vmread(VMCS_EXIT_REASON, &exit_reason);
switch (exit_reason)
{
case VMX_EXIT_REASON_EXECUTE_CPUID:
4 years ago
{
int result[4];
__cpuid(result, regs->rax);
regs->rax = result[0];
regs->rbx = result[1];
regs->rcx = result[2];
regs->rdx = result[3];
goto advance_rip;
4 years ago
}
case VMX_EXIT_REASON_NMI_WINDOW:
{
exception::injection(interruption_type::non_maskable_interrupt, EXCEPTION_NMI);
// turn off NMI window exiting since we handled the NMI...
ia32_vmx_procbased_ctls_register procbased_ctls;
__vmx_vmread(VMCS_CTRL_PROCESSOR_BASED_VM_EXECUTION_CONTROLS, &procbased_ctls.flags);
procbased_ctls.nmi_window_exiting = false;
__vmx_vmwrite(VMCS_CTRL_PROCESSOR_BASED_VM_EXECUTION_CONTROLS, procbased_ctls.flags);
goto dont_advance;
}
4 years ago
case VMX_EXIT_REASON_EXECUTE_XSETBV:
4 years ago
{
if (handle::xsetbv(regs))
goto advance_rip;
else
goto dont_advance;
4 years ago
}
case VMX_EXIT_REASON_EXECUTE_RDMSR:
{
if (handle::rdmsr(regs))
goto advance_rip;
else
goto dont_advance;
4 years ago
}
case VMX_EXIT_REASON_EXECUTE_WRMSR:
{
if (handle::wrmsr(regs))
goto advance_rip;
else
goto dont_advance;
}
case VMX_EXIT_REASON_EXECUTE_VMCALL:
{
if (handle::vmcall(regs))
goto advance_rip;
else
goto dont_advance;
}
case VMX_EXIT_REASON_EXECUTE_INVD:
{
__wbinvd();
goto advance_rip;
}
4 years ago
case VMX_EXIT_REASON_EXECUTE_VMWRITE:
4 years ago
case VMX_EXIT_REASON_EXECUTE_VMREAD:
4 years ago
case VMX_EXIT_REASON_EXECUTE_VMPTRST:
4 years ago
case VMX_EXIT_REASON_EXECUTE_VMPTRLD:
4 years ago
case VMX_EXIT_REASON_EXECUTE_VMCLEAR:
4 years ago
case VMX_EXIT_REASON_EXECUTE_VMXOFF:
case VMX_EXIT_REASON_EXECUTE_VMXON:
case VMX_EXIT_REASON_EXECUTE_VMFUNC:
4 years ago
{
exception::injection(interruption_type::hardware_exception, EXCEPTION_INVALID_OPCODE);
goto dont_advance;
4 years ago
}
4 years ago
default:
4 years ago
// TODO: check out the vmexit reason and add support for it...
__debugbreak();
4 years ago
}
advance_rip:
4 years ago
size_t rip, exec_len;
__vmx_vmread(VMCS_GUEST_RIP, &rip);
__vmx_vmread(VMCS_VMEXIT_INSTRUCTION_LENGTH, &exec_len);
__vmx_vmwrite(VMCS_GUEST_RIP, rip + exec_len);
// since we are advancing RIP, also check if TF = 1, if so, set pending #DB...
// otherwise this #DB will fire on the wrong instruction... please refer to:
// https://howtohypervise.blogspot.com/2019/01/a-common-missight-in-most-hypervisors.html
exception::handle_debug();
dont_advance:
return;
4 years ago
}