From 6ce3597bd3925f3c42dc10e3bf8a0d28ee98bbd6 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sat, 26 Mar 2022 21:19:01 +0100 Subject: [PATCH] Prepare hypervisor --- src/driver/driver_main.cpp | 4 +++ src/driver/hypervisor.cpp | 54 ++++++++++++++++++++++++++++++++++++++ src/driver/hypervisor.hpp | 19 ++++++++++++++ src/driver/logging.hpp | 2 +- src/driver/std_include.hpp | 1 + 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/driver/hypervisor.cpp create mode 100644 src/driver/hypervisor.hpp diff --git a/src/driver/driver_main.cpp b/src/driver/driver_main.cpp index 4f95bce..8ff38b0 100644 --- a/src/driver/driver_main.cpp +++ b/src/driver/driver_main.cpp @@ -3,6 +3,7 @@ #include "sleep_callback.hpp" #include "irp.hpp" #include "exception.hpp" +#include "hypervisor.hpp" #define DOS_DEV_NAME L"\\DosDevices\\HelloDev" #define DEV_NAME L"\\Device\\HelloDev" @@ -38,17 +39,20 @@ public: private: sleep_callback sleep_callback_{}; irp irp_{}; + hypervisor hypervisor_{}; void sleep_notification(const sleep_callback::type type) { if (type == sleep_callback::type::sleep) { debug_log("Going to sleep!"); + this->hypervisor_.on_sleep(); } if (type == sleep_callback::type::wakeup) { debug_log("Waking up!"); + this->hypervisor_.on_wakeup(); } } }; diff --git a/src/driver/hypervisor.cpp b/src/driver/hypervisor.cpp new file mode 100644 index 0000000..b82c560 --- /dev/null +++ b/src/driver/hypervisor.cpp @@ -0,0 +1,54 @@ +#include "std_include.hpp" +#include "hypervisor.hpp" +#include "exception.hpp" +#include "logging.hpp" + +#define IA32_FEATURE_CONTROL_MSR 0x3A +#define IA32_FEATURE_CONTROL_MSR_LOCK 0x0001 +#define IA32_FEATURE_CONTROL_MSR_ENABLE_VMXON_OUTSIDE_SMX 0x0004 + +namespace +{ + bool is_vmx_supported() + { + int cpuid_data[4] = {0}; + __cpuid(cpuid_data, 1); + return cpuid_data[2] & 0x20; + } + + bool is_vmx_available() + { + const auto feature_control = __readmsr(IA32_FEATURE_CONTROL_MSR); + const auto is_locked = (feature_control & IA32_FEATURE_CONTROL_MSR_LOCK) != 0; + const auto is_enabled_outside_smx = (feature_control & IA32_FEATURE_CONTROL_MSR_ENABLE_VMXON_OUTSIDE_SMX) != 0; + + return is_locked && is_enabled_outside_smx; + } + + bool is_virtualization_supported() + { + return is_vmx_supported() && is_vmx_available(); + } +} + +hypervisor::hypervisor() +{ + if(!is_virtualization_supported()) + { + throw std::runtime_error("VMX not supported on this machine"); + } + + debug_log("VMX supported!\n"); +} + +hypervisor::~hypervisor() +{ +} + +void hypervisor::on_sleep() +{ +} + +void hypervisor::on_wakeup() +{ +} diff --git a/src/driver/hypervisor.hpp b/src/driver/hypervisor.hpp new file mode 100644 index 0000000..7e0219b --- /dev/null +++ b/src/driver/hypervisor.hpp @@ -0,0 +1,19 @@ +#pragma once + +class hypervisor +{ +public: + hypervisor(); + ~hypervisor(); + + hypervisor(hypervisor&& obj) noexcept = delete; + hypervisor& operator=(hypervisor&& obj) noexcept = delete; + + hypervisor(const hypervisor& obj) = delete; + hypervisor& operator=(const hypervisor& obj) = delete; + + void on_sleep(); + void on_wakeup(); + +private: +}; \ No newline at end of file diff --git a/src/driver/logging.hpp b/src/driver/logging.hpp index 0c8a601..3217732 100644 --- a/src/driver/logging.hpp +++ b/src/driver/logging.hpp @@ -3,5 +3,5 @@ #ifdef NDEBUG #define debug_log(...) #else -#define debug_log(...) DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, __VA_ARGS__) +#define debug_log(msg, ...) DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "[MOMO] " msg, __VA_ARGS__) #endif diff --git a/src/driver/std_include.hpp b/src/driver/std_include.hpp index c060a19..37b833c 100644 --- a/src/driver/std_include.hpp +++ b/src/driver/std_include.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include "nt_ext.hpp" #include "new.hpp" \ No newline at end of file