mirror of
https://github.com/wbenny/hvpp
synced 2026-08-17 04:26:04 -04:00
add initial version of C-only interface, hvpp.h
add example of how to use this interface (hvppdrv_c)
This commit is contained in:
parent
6e182b3225
commit
eee6513abc
22 changed files with 2610 additions and 24 deletions
18
hvpp.sln
18
hvpp.sln
|
|
@ -14,6 +14,12 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hvppdrv", "src\hvppdrv\hvpp
|
|||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hvppctrl", "src\hvppctrl\hvppctrl.vcxproj", "{A72DAEF5-C739-4E70-B57E-4310ABA03749}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hvpp-entry", "src\hvpp\hvpp-entry.vcxproj", "{D16E66B1-31BC-465F-916E-430803FFDE99}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "hvpp-lib", "hvpp-lib", "{AD8F16CD-1F36-4BF7-91FF-E04713C7EC63}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hvppdrv_c", "src\hvppdrv_c\hvppdrv_c.vcxproj", "{9D8BC3BA-1749-4974-9BEC-00231849A63C}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
|
|
@ -32,10 +38,22 @@ Global
|
|||
{A72DAEF5-C739-4E70-B57E-4310ABA03749}.Debug|x64.Build.0 = Debug|x64
|
||||
{A72DAEF5-C739-4E70-B57E-4310ABA03749}.Release|x64.ActiveCfg = Release|x64
|
||||
{A72DAEF5-C739-4E70-B57E-4310ABA03749}.Release|x64.Build.0 = Release|x64
|
||||
{D16E66B1-31BC-465F-916E-430803FFDE99}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{D16E66B1-31BC-465F-916E-430803FFDE99}.Debug|x64.Build.0 = Debug|x64
|
||||
{D16E66B1-31BC-465F-916E-430803FFDE99}.Release|x64.ActiveCfg = Release|x64
|
||||
{D16E66B1-31BC-465F-916E-430803FFDE99}.Release|x64.Build.0 = Release|x64
|
||||
{9D8BC3BA-1749-4974-9BEC-00231849A63C}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{9D8BC3BA-1749-4974-9BEC-00231849A63C}.Debug|x64.Build.0 = Debug|x64
|
||||
{9D8BC3BA-1749-4974-9BEC-00231849A63C}.Release|x64.ActiveCfg = Release|x64
|
||||
{9D8BC3BA-1749-4974-9BEC-00231849A63C}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{2FC6C155-2BDF-4761-B9B5-7DFE9C2BB4F4} = {AD8F16CD-1F36-4BF7-91FF-E04713C7EC63}
|
||||
{D16E66B1-31BC-465F-916E-430803FFDE99} = {AD8F16CD-1F36-4BF7-91FF-E04713C7EC63}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {B2F42D07-8CF5-40C5-924F-6EAB82D9ABF0}
|
||||
EndGlobalSection
|
||||
|
|
|
|||
111
src/hvpp/hvpp-entry.vcxproj
Normal file
111
src/hvpp/hvpp-entry.vcxproj
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{D16E66B1-31BC-465F-916E-430803FFDE99}</ProjectGuid>
|
||||
<TemplateGuid>{1bc93793-694f-48fe-9372-81e2b05556fd}</TemplateGuid>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<MinimumVisualStudioVersion>12.0</MinimumVisualStudioVersion>
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform Condition="'$(Platform)' == ''">Win32</Platform>
|
||||
<RootNamespace>hvpp-entry</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>$(LatestTargetPlatformVersion)</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<TargetVersion>Windows7</TargetVersion>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>WindowsKernelModeDriver10.0</PlatformToolset>
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<DriverType>WDM</DriverType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<TargetVersion>Windows7</TargetVersion>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>WindowsKernelModeDriver10.0</PlatformToolset>
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<DriverType>WDM</DriverType>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor>
|
||||
<IncludePath>$(ProjectDir);$(VC_IncludePath);$(IncludePath);$(KMDF_INC_PATH)$(KMDF_VER_PATH)</IncludePath>
|
||||
<OutDir>$(SolutionDir)bin\$(PlatformShortName)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)bin\obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\</IntDir>
|
||||
<EnableInf2cat>false</EnableInf2cat>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor>
|
||||
<IncludePath>$(ProjectDir);$(VC_IncludePath);$(IncludePath);$(KMDF_INC_PATH)$(KMDF_VER_PATH)</IncludePath>
|
||||
<OutDir>$(SolutionDir)bin\$(PlatformShortName)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)bin\obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\</IntDir>
|
||||
<Inf2CatUseLocalTime>true</Inf2CatUseLocalTime>
|
||||
<EnableInf2cat>false</EnableInf2cat>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<DisableSpecificWarnings>4201;4748;%(DisableSpecificWarnings)</DisableSpecificWarnings>
|
||||
<LanguageStandard>stdcpplatest</LanguageStandard>
|
||||
<ObjectFileName>$(IntDir)%(RelativeDir)%(Filename)%(Extension).obj</ObjectFileName>
|
||||
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||
<SupportJustMyCode>false</SupportJustMyCode>
|
||||
</ClCompile>
|
||||
<Inf />
|
||||
<MASM />
|
||||
<Link>
|
||||
<GenerateDebugInformation>DebugFull</GenerateDebugInformation>
|
||||
</Link>
|
||||
<PostBuildEvent />
|
||||
<Lib />
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<DisableSpecificWarnings>4201;4603;4627;4986;4987;4996;%(DisableSpecificWarnings)</DisableSpecificWarnings>
|
||||
<LanguageStandard>stdcpplatest</LanguageStandard>
|
||||
<ObjectFileName>$(IntDir)%(RelativeDir)%(Filename)%(Extension).obj</ObjectFileName>
|
||||
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
</ClCompile>
|
||||
<Inf />
|
||||
<MASM />
|
||||
<Link>
|
||||
<GenerateDebugInformation>DebugFull</GenerateDebugInformation>
|
||||
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
|
||||
</Link>
|
||||
<PostBuildEvent />
|
||||
<Lib>
|
||||
<LinkTimeCodeGeneration>true</LinkTimeCodeGeneration>
|
||||
</Lib>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<FilesToPackage Include="$(TargetPath)" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="hvpp\lib\win32\driver.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="hvpp.vcxproj">
|
||||
<Project>{2fc6c155-2bdf-4761-b9b5-7dfe9c2bb4f4}</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
31
src/hvpp/hvpp-entry.vcxproj.filters
Normal file
31
src/hvpp/hvpp-entry.vcxproj.filters
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\hvpp">
|
||||
<UniqueIdentifier>{6e4d9600-74cd-4df7-a644-8958c9188801}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\hvpp\lib">
|
||||
<UniqueIdentifier>{621e920d-d9e9-47e9-a5de-aa77f6d02d7c}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Source Files\hvpp\lib\win32">
|
||||
<UniqueIdentifier>{9672ddae-e529-45ac-bb8b-049cf5f33c1b}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="hvpp\lib\win32\driver.cpp">
|
||||
<Filter>Source Files\hvpp\lib\win32</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
4
src/hvpp/hvpp-entry.vcxproj.user
Normal file
4
src/hvpp/hvpp-entry.vcxproj.user
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup />
|
||||
</Project>
|
||||
|
|
@ -99,12 +99,14 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="hvpp\ept.cpp" />
|
||||
<ClCompile Include="hvpp\hvpp.cpp" />
|
||||
<ClCompile Include="hvpp\hypervisor.cpp" />
|
||||
<ClCompile Include="hvpp\vcpu.cpp" />
|
||||
<ClCompile Include="hvpp\vmexit.cpp">
|
||||
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(IntDir)/$(RelativeDir)/%(Filename)%(Extension).obj</ObjectFileName>
|
||||
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(IntDir)/$(RelativeDir)/%(Filename)%(Extension).obj</ObjectFileName>
|
||||
</ClCompile>
|
||||
<ClCompile Include="hvpp\vmexit\vmexit_c_wrapper.cpp" />
|
||||
<ClCompile Include="hvpp\vmexit\vmexit_dbgbreak.cpp" />
|
||||
<ClCompile Include="hvpp\vmexit\vmexit_passthrough.cpp" />
|
||||
<ClCompile Include="hvpp\vmexit\vmexit_stats.cpp" />
|
||||
|
|
@ -117,13 +119,13 @@
|
|||
<ClCompile Include="hvpp\lib\win32\cr3_guard.cpp" />
|
||||
<ClCompile Include="hvpp\lib\win32\debugger.cpp" />
|
||||
<ClCompile Include="hvpp\lib\win32\device.cpp" />
|
||||
<ClCompile Include="hvpp\lib\win32\driver.cpp" />
|
||||
<ClCompile Include="hvpp\lib\win32\log.cpp" />
|
||||
<ClCompile Include="hvpp\lib\win32\mm.cpp" />
|
||||
<ClCompile Include="hvpp\lib\win32\mp.cpp" />
|
||||
<ClCompile Include="hvpp\lib\win32\tracelog.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="hvpp\hvpp.h" />
|
||||
<ClInclude Include="hvpp\lib\debugger.h" />
|
||||
<ClInclude Include="hvpp\lib\device.h" />
|
||||
<ClInclude Include="hvpp\lib\ioctl.h" />
|
||||
|
|
@ -132,6 +134,7 @@
|
|||
<ClInclude Include="hvpp\hypervisor.h" />
|
||||
<ClInclude Include="hvpp\vcpu.h" />
|
||||
<ClInclude Include="hvpp\vmexit.h" />
|
||||
<ClInclude Include="hvpp\vmexit\vmexit_c_wrapper.h" />
|
||||
<ClInclude Include="hvpp\vmexit\vmexit_dbgbreak.h" />
|
||||
<ClInclude Include="hvpp\vmexit\vmexit_passthrough.h" />
|
||||
<ClInclude Include="hvpp\vmexit\vmexit_stats.h" />
|
||||
|
|
|
|||
|
|
@ -108,9 +108,6 @@
|
|||
<ClCompile Include="hvpp\lib\win32\mm.cpp">
|
||||
<Filter>Source Files\hvpp\lib\win32</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="hvpp\lib\win32\driver.cpp">
|
||||
<Filter>Source Files\hvpp\lib\win32</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="hvpp\lib\driver.cpp">
|
||||
<Filter>Source Files\hvpp\lib</Filter>
|
||||
</ClCompile>
|
||||
|
|
@ -132,6 +129,12 @@
|
|||
<ClCompile Include="hvpp\lib\win32\device.cpp">
|
||||
<Filter>Source Files\hvpp\lib\win32</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="hvpp\hvpp.cpp">
|
||||
<Filter>Source Files\hvpp</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="hvpp\vmexit\vmexit_c_wrapper.cpp">
|
||||
<Filter>Source Files\hvpp\vmexit</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="hvpp\lib\bitmap.h">
|
||||
|
|
@ -290,6 +293,12 @@
|
|||
<ClInclude Include="hvpp\lib\ioctl.h">
|
||||
<Filter>Header Files\hvpp\lib</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="hvpp\hvpp.h">
|
||||
<Filter>Header Files\hvpp</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="hvpp\vmexit\vmexit_c_wrapper.h">
|
||||
<Filter>Header Files\hvpp\vmexit</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<MASM Include="hvpp\ia32\context.asm">
|
||||
|
|
|
|||
460
src/hvpp/hvpp/hvpp.cpp
Normal file
460
src/hvpp/hvpp/hvpp.cpp
Normal file
|
|
@ -0,0 +1,460 @@
|
|||
#include <ntddk.h>
|
||||
#include "hvpp.h"
|
||||
|
||||
#include "hypervisor.h"
|
||||
#include "vcpu.h"
|
||||
#include "lib/cr3_guard.h"
|
||||
#include "lib/driver.h"
|
||||
#include "lib/mm.h"
|
||||
#include "vmexit/vmexit_c_wrapper.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
using namespace ia32;
|
||||
using namespace hvpp;
|
||||
|
||||
#define hvpp_ ((hypervisor*)Hvpp)
|
||||
#define vcpu_ ((vcpu_t*)Vcpu)
|
||||
#define ept_ ((ept_t*)Ept)
|
||||
|
||||
extern "C" {
|
||||
|
||||
static
|
||||
NTSTATUS
|
||||
NTAPI
|
||||
ErrorCodeToNtStatus(
|
||||
error_code_t error
|
||||
)
|
||||
{
|
||||
//
|
||||
// TODO: Something meaningful...
|
||||
//
|
||||
return !error
|
||||
? STATUS_SUCCESS
|
||||
: STATUS_UNSUCCESSFUL;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// ept.h
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma region ept.h
|
||||
|
||||
PEPTE
|
||||
NTAPI
|
||||
HvppEptMap(
|
||||
_In_ PEPT Ept,
|
||||
_In_ PHYSICAL_ADDRESS GuestPhysicalAddress,
|
||||
_In_ PHYSICAL_ADDRESS HostPhysicalAddress,
|
||||
_In_ ULONG Access
|
||||
)
|
||||
{
|
||||
return (PEPTE)
|
||||
ept_->map(
|
||||
pa_t{ (uint64_t)GuestPhysicalAddress.QuadPart },
|
||||
pa_t{ (uint64_t)HostPhysicalAddress.QuadPart },
|
||||
(epte_t::access_type)Access
|
||||
);
|
||||
}
|
||||
|
||||
PEPTE
|
||||
NTAPI
|
||||
HvppEptMapEx(
|
||||
_In_ PEPT Ept,
|
||||
_In_ PHYSICAL_ADDRESS GuestPhysicalAddress,
|
||||
_In_ PHYSICAL_ADDRESS HostPhysicalAddress,
|
||||
_In_ ULONG Access,
|
||||
_In_ ULONG Large
|
||||
)
|
||||
{
|
||||
return (PEPTE)
|
||||
ept_->map(
|
||||
pa_t{ (uint64_t)GuestPhysicalAddress.QuadPart },
|
||||
pa_t{ (uint64_t)HostPhysicalAddress.QuadPart },
|
||||
(epte_t::access_type)Access,
|
||||
(pml)Large
|
||||
);
|
||||
}
|
||||
|
||||
PEPTE
|
||||
NTAPI
|
||||
HvppEptMap4Kb(
|
||||
_In_ PEPT Ept,
|
||||
_In_ PHYSICAL_ADDRESS GuestPhysicalAddress,
|
||||
_In_ PHYSICAL_ADDRESS HostPhysicalAddress,
|
||||
_In_ ULONG Access
|
||||
)
|
||||
{
|
||||
return (PEPTE)
|
||||
ept_->map_4kb(
|
||||
pa_t{ (uint64_t)GuestPhysicalAddress.QuadPart },
|
||||
pa_t{ (uint64_t)HostPhysicalAddress.QuadPart },
|
||||
(epte_t::access_type)Access
|
||||
);
|
||||
}
|
||||
|
||||
PEPTE
|
||||
NTAPI
|
||||
HvppEptMap2Mb(
|
||||
_In_ PEPT Ept,
|
||||
_In_ PHYSICAL_ADDRESS GuestPhysicalAddress,
|
||||
_In_ PHYSICAL_ADDRESS HostPhysicalAddress,
|
||||
_In_ ULONG Access
|
||||
)
|
||||
{
|
||||
return (PEPTE)
|
||||
ept_->map_2mb(
|
||||
pa_t{ (uint64_t)GuestPhysicalAddress.QuadPart },
|
||||
pa_t{ (uint64_t)HostPhysicalAddress.QuadPart },
|
||||
(epte_t::access_type)Access
|
||||
);
|
||||
}
|
||||
|
||||
PEPTE
|
||||
NTAPI
|
||||
HvppEptMap1Gb(
|
||||
_In_ PEPT Ept,
|
||||
_In_ PHYSICAL_ADDRESS GuestPhysicalAddress,
|
||||
_In_ PHYSICAL_ADDRESS HostPhysicalAddress,
|
||||
_In_ ULONG Access
|
||||
)
|
||||
{
|
||||
return (PEPTE)
|
||||
ept_->map_1gb(
|
||||
pa_t{ (uint64_t)GuestPhysicalAddress.QuadPart },
|
||||
pa_t{ (uint64_t)HostPhysicalAddress.QuadPart },
|
||||
(epte_t::access_type)Access
|
||||
);
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppEptSplit1GbTo2Mb(
|
||||
_In_ PEPT Ept,
|
||||
_In_ PHYSICAL_ADDRESS GuestPhysicalAddress,
|
||||
_In_ PHYSICAL_ADDRESS HostPhysicalAddress
|
||||
)
|
||||
{
|
||||
ept_->split_1gb_to_2mb(
|
||||
pa_t{ (uint64_t)GuestPhysicalAddress.QuadPart },
|
||||
pa_t{ (uint64_t)HostPhysicalAddress.QuadPart }
|
||||
);
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppEptSplit2MbTo4Kb(
|
||||
_In_ PEPT Ept,
|
||||
_In_ PHYSICAL_ADDRESS GuestPhysicalAddress,
|
||||
_In_ PHYSICAL_ADDRESS HostPhysicalAddress
|
||||
)
|
||||
{
|
||||
ept_->split_2mb_to_4kb(
|
||||
pa_t{ (uint64_t)GuestPhysicalAddress.QuadPart },
|
||||
pa_t{ (uint64_t)HostPhysicalAddress.QuadPart }
|
||||
);
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppEptJoin2MbTo1Gb(
|
||||
_In_ PEPT Ept,
|
||||
_In_ PHYSICAL_ADDRESS GuestPhysicalAddress,
|
||||
_In_ PHYSICAL_ADDRESS HostPhysicalAddress
|
||||
)
|
||||
{
|
||||
ept_->join_2mb_to_1gb(
|
||||
pa_t{ (uint64_t)GuestPhysicalAddress.QuadPart },
|
||||
pa_t{ (uint64_t)HostPhysicalAddress.QuadPart }
|
||||
);
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppEptJoin4KbTo2Mb(
|
||||
_In_ PEPT Ept,
|
||||
_In_ PHYSICAL_ADDRESS GuestPhysicalAddress,
|
||||
_In_ PHYSICAL_ADDRESS HostPhysicalAddress
|
||||
)
|
||||
{
|
||||
ept_->join_4kb_to_2mb(
|
||||
pa_t{ (uint64_t)GuestPhysicalAddress.QuadPart },
|
||||
pa_t{ (uint64_t)HostPhysicalAddress.QuadPart }
|
||||
);
|
||||
}
|
||||
|
||||
EPT_PTR
|
||||
NTAPI
|
||||
HvppEptGetEptPointer(
|
||||
_In_ PEPT Ept
|
||||
)
|
||||
{
|
||||
return EPT_PTR { ept_->ept_pointer().flags };
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// hypervisor.h
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma region hypervisor.h
|
||||
|
||||
NTSTATUS
|
||||
NTAPI
|
||||
HvppInitialize(
|
||||
_Out_ PHVPP* Hvpp
|
||||
)
|
||||
{
|
||||
//
|
||||
// Initialize the memory manager and logger.
|
||||
//
|
||||
|
||||
driver::common::initialize();
|
||||
|
||||
//
|
||||
// Allocate memory for the hypervisor instance.
|
||||
//
|
||||
|
||||
*Hvpp = memory_manager::system_allocate(sizeof(hypervisor));
|
||||
|
||||
if (!*Hvpp)
|
||||
{
|
||||
//
|
||||
// Allocation failed - exit.
|
||||
//
|
||||
|
||||
driver::common::destroy();
|
||||
return STATUS_INSUFFICIENT_RESOURCES;
|
||||
}
|
||||
|
||||
//
|
||||
// Initialize the hypervisor.
|
||||
//
|
||||
|
||||
return ErrorCodeToNtStatus(((hypervisor*)(*Hvpp))->initialize());
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppDestroy(
|
||||
_In_ PHVPP Hvpp
|
||||
)
|
||||
{
|
||||
//
|
||||
// Destroy the hypervisor.
|
||||
//
|
||||
|
||||
hvpp_->destroy();
|
||||
memory_manager::system_free(hvpp_);
|
||||
|
||||
//
|
||||
// Destroy the memory manager and logger.
|
||||
//
|
||||
|
||||
driver::common::destroy();
|
||||
}
|
||||
|
||||
NTSTATUS
|
||||
NTAPI
|
||||
HvppStart(
|
||||
_In_ PHVPP Hvpp,
|
||||
_In_ PVMEXIT_HANDLER VmExitHandler
|
||||
)
|
||||
{
|
||||
//
|
||||
// Create the VM-exit handler instance.
|
||||
//
|
||||
|
||||
auto exit_handler = new vmexit_c_wrapper_handler();
|
||||
|
||||
//
|
||||
// Initialize the C-handlers array.
|
||||
//
|
||||
|
||||
vmexit_c_wrapper_handler::c_handler_array_t c_handlers;
|
||||
memcpy(c_handlers.data(), VmExitHandler->HandlerRoutine, sizeof(VmExitHandler->HandlerRoutine));
|
||||
|
||||
//
|
||||
// Initialize the VM-exit handler.
|
||||
//
|
||||
|
||||
exit_handler->initialize(c_handlers);
|
||||
|
||||
//
|
||||
// Start the hypervisor.
|
||||
//
|
||||
|
||||
return ErrorCodeToNtStatus(hvpp_->start(exit_handler));
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppStop(
|
||||
_In_ PHVPP Hvpp
|
||||
)
|
||||
{
|
||||
hvpp_->stop();
|
||||
}
|
||||
|
||||
BOOLEAN
|
||||
NTAPI
|
||||
HvppIsStarted(
|
||||
_In_ PHVPP Hvpp
|
||||
)
|
||||
{
|
||||
return hvpp_->is_started();
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// vcpu.h
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma region vcpu.h
|
||||
|
||||
PEPT
|
||||
NTAPI
|
||||
HvppVcpuGetEpt(
|
||||
_In_ PVCPU Vcpu
|
||||
)
|
||||
{
|
||||
return (PEPT)&vcpu_->ept();
|
||||
}
|
||||
|
||||
PVCPU_CONTEXT
|
||||
NTAPI
|
||||
HvppVcpuExitContext(
|
||||
_In_ PVCPU Vcpu
|
||||
)
|
||||
{
|
||||
return (PVCPU_CONTEXT)&vcpu_->exit_context();
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppVcpuSuppressRipAdjust(
|
||||
_In_ PVCPU Vcpu
|
||||
)
|
||||
{
|
||||
vcpu_->suppress_rip_adjust();
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// Helpers
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma region Helpers
|
||||
|
||||
PVOID
|
||||
NTAPI
|
||||
HvppAllocate(
|
||||
ULONG Size
|
||||
)
|
||||
{
|
||||
return new uint8_t[Size];
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppFree(
|
||||
PVOID Address
|
||||
)
|
||||
{
|
||||
delete[] (uint8_t*)Address;
|
||||
}
|
||||
|
||||
ULONG64
|
||||
NTAPI
|
||||
HvppVmRead(
|
||||
_In_ VMCS_FIELD VmcsField
|
||||
)
|
||||
{
|
||||
ULONG64 Result = 0;
|
||||
vmx::vmread((vmx::vmcs_t::field)VmcsField, Result);
|
||||
return Result;
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppVmWrite(
|
||||
_In_ VMCS_FIELD VmcsField,
|
||||
_In_ ULONG64 VmcsValue
|
||||
)
|
||||
{
|
||||
vmx::vmwrite((vmx::vmcs_t::field)VmcsField, VmcsValue);
|
||||
}
|
||||
|
||||
ULONG_PTR
|
||||
NTAPI
|
||||
HvppVmCall(
|
||||
_In_ ULONG_PTR Rcx,
|
||||
_In_ ULONG_PTR Rdx,
|
||||
_In_ ULONG_PTR R8,
|
||||
_In_ ULONG_PTR R9
|
||||
)
|
||||
{
|
||||
return vmx::vmcall(Rcx, Rdx, R8, R9);
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppInveptAll(
|
||||
VOID
|
||||
)
|
||||
{
|
||||
vmx::invept_all_contexts();
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppInveptSingleContext(
|
||||
_In_ EPT_PTR EptPointer
|
||||
)
|
||||
{
|
||||
vmx::invept_single_context(ept_ptr_t{ EptPointer.Flags });
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppAttachAddressSpace(
|
||||
_Inout_ ULONG_PTR* Cr3
|
||||
)
|
||||
{
|
||||
ULONG_PTR NewCr3 = *Cr3;
|
||||
ULONG_PTR PreviousCr3 = ia32::read<ia32::cr3_t>().flags;
|
||||
|
||||
ia32::write<ia32::cr3_t>(::detail::kernel_cr3(ia32::cr3_t{ NewCr3 }));
|
||||
|
||||
*Cr3 = PreviousCr3;
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppDetachAddressSpace(
|
||||
_In_ ULONG_PTR Cr3
|
||||
)
|
||||
{
|
||||
ia32::write<ia32::cr3_t>(ia32::cr3_t{ Cr3 });
|
||||
}
|
||||
|
||||
VOID
|
||||
HvppTrace(
|
||||
_In_ const CHAR* Format,
|
||||
...
|
||||
)
|
||||
{
|
||||
va_list Args;
|
||||
va_start(Args, Format);
|
||||
|
||||
logger::detail::vprint_trace(logger::level_t::trace, "__UNKNOWN__", Format, Args);
|
||||
|
||||
va_end(Args);
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
}
|
||||
1351
src/hvpp/hvpp/hvpp.h
Normal file
1351
src/hvpp/hvpp/hvpp.h
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -9,13 +9,28 @@
|
|||
|
||||
namespace driver::common
|
||||
{
|
||||
void* system_memory = nullptr;
|
||||
size_t system_memory_size = 0;
|
||||
void* system_memory_ = nullptr;
|
||||
size_t system_memory_size_ = 0;
|
||||
|
||||
auto initialize() noexcept -> error_code_t
|
||||
driver_initialize_fn driver_initialize_;
|
||||
driver_destroy_fn driver_destroy_;
|
||||
|
||||
auto initialize(
|
||||
driver_initialize_fn driver_initialize,
|
||||
driver_destroy_fn driver_destroy
|
||||
) noexcept -> error_code_t
|
||||
{
|
||||
hvpp_assert(system_memory == nullptr);
|
||||
hvpp_assert(system_memory_size == 0);
|
||||
hvpp_assert(system_memory_ == nullptr);
|
||||
hvpp_assert(system_memory_size_ == 0);
|
||||
|
||||
//
|
||||
// Either both must be set or both must be nullptr,
|
||||
// nothing else.
|
||||
//
|
||||
hvpp_assert(!(!!driver_initialize ^ !!driver_destroy));
|
||||
|
||||
driver_initialize_ = driver_initialize;
|
||||
driver_destroy_ = driver_destroy;
|
||||
|
||||
//
|
||||
// Initialize logger and memory manager.
|
||||
|
|
@ -63,18 +78,18 @@ namespace driver::common
|
|||
//
|
||||
// Round up to page boundary.
|
||||
//
|
||||
system_memory_size = ia32::round_to_pages(required_memory_size);
|
||||
system_memory_size_ = ia32::round_to_pages(required_memory_size);
|
||||
|
||||
hvpp_info("Number of processors: %u", mp::cpu_count());
|
||||
hvpp_info("Reserved memory: %" PRIu64 " MB",
|
||||
system_memory_size / 1024 / 1024);
|
||||
system_memory_size_ / 1024 / 1024);
|
||||
|
||||
//
|
||||
// Allocate memory.
|
||||
//
|
||||
system_memory = memory_manager::system_allocate(required_memory_size);
|
||||
system_memory_ = memory_manager::system_allocate(required_memory_size);
|
||||
|
||||
if (!system_memory)
|
||||
if (!system_memory_)
|
||||
{
|
||||
return make_error_code_t(std::errc::not_enough_memory);
|
||||
}
|
||||
|
|
@ -82,20 +97,25 @@ namespace driver::common
|
|||
//
|
||||
// Assign allocated memory to the memory manager.
|
||||
//
|
||||
if (auto err = memory_manager::assign(system_memory, system_memory_size))
|
||||
if (auto err = memory_manager::assign(system_memory_, system_memory_size_))
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
return ::driver::initialize();
|
||||
return driver_initialize_
|
||||
? driver_initialize_()
|
||||
: error_code_t{};
|
||||
}
|
||||
|
||||
void destroy() noexcept
|
||||
{
|
||||
//
|
||||
// Call driver's destroy() function.
|
||||
// Call driver's destroy() function, if provided.
|
||||
//
|
||||
::driver::destroy();
|
||||
if (driver_destroy_)
|
||||
{
|
||||
driver_destroy_();
|
||||
}
|
||||
|
||||
//
|
||||
// Destroy memory manager and logger.
|
||||
|
|
@ -106,9 +126,9 @@ namespace driver::common
|
|||
//
|
||||
// Return allocated memory back to the system.
|
||||
//
|
||||
if (system_memory)
|
||||
if (system_memory_)
|
||||
{
|
||||
memory_manager::system_free(system_memory);
|
||||
memory_manager::system_free(system_memory_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,14 @@ namespace driver
|
|||
{
|
||||
namespace common
|
||||
{
|
||||
auto initialize() noexcept -> error_code_t;
|
||||
using driver_initialize_fn = error_code_t(*)();
|
||||
using driver_destroy_fn = void(*)();
|
||||
|
||||
auto initialize(
|
||||
driver_initialize_fn driver_initialize = nullptr,
|
||||
driver_destroy_fn driver_destroy = nullptr
|
||||
) noexcept -> error_code_t;
|
||||
|
||||
void destroy() noexcept;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -170,7 +170,8 @@ DriverEntry(
|
|||
DriverObject->MajorFunction[IRP_MJ_WRITE] = &DriverDispatch;
|
||||
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = &DriverDispatch;
|
||||
|
||||
auto err = driver::common::initialize();
|
||||
auto err = driver::common::initialize(&driver::initialize,
|
||||
&driver::destroy);
|
||||
|
||||
return ErrorCodeToNtStatus(err);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -240,7 +240,7 @@ class vmexit_handler
|
|||
//
|
||||
virtual void handle_vm_fallback(vcpu_t& vp) noexcept;
|
||||
|
||||
private:
|
||||
protected:
|
||||
using handler_fn_t = void (vmexit_handler::*)(vcpu_t&);
|
||||
std::array<handler_fn_t, 65> handlers_;
|
||||
};
|
||||
|
|
|
|||
71
src/hvpp/hvpp/vmexit/vmexit_c_wrapper.cpp
Normal file
71
src/hvpp/hvpp/vmexit/vmexit_c_wrapper.cpp
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
#include "vmexit_c_wrapper.h"
|
||||
|
||||
#include "hvpp/vcpu.h"
|
||||
|
||||
namespace hvpp {
|
||||
|
||||
auto vmexit_c_wrapper_handler::initialize(const c_handler_array_t& c_handlers, void* context) noexcept -> error_code_t
|
||||
{
|
||||
//
|
||||
// Make local copy of the C-handlers.
|
||||
//
|
||||
|
||||
c_handlers_ = c_handlers;
|
||||
context_ = context;
|
||||
|
||||
return error_code_t{};
|
||||
}
|
||||
|
||||
void vmexit_c_wrapper_handler::destroy() noexcept
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void vmexit_c_wrapper_handler::handle(vcpu_t& vp) noexcept
|
||||
{
|
||||
auto exit_reason = vp.exit_reason();
|
||||
auto exit_reason_index = static_cast<int>(exit_reason);
|
||||
|
||||
auto cpp_handler = handlers_[exit_reason_index];
|
||||
auto c_handler = c_handlers_[exit_reason_index];
|
||||
|
||||
if (c_handler)
|
||||
{
|
||||
//
|
||||
// C-handler has been defined - call that routine.
|
||||
//
|
||||
|
||||
passthrough_context context;
|
||||
context.passthrough_routine = (passthrough_fn_t)&vmexit_c_wrapper_handler::handle_passthrough;
|
||||
context.context = context_;
|
||||
context.handler_instance = this;
|
||||
context.handler_method = cpp_handler;
|
||||
context.vcpu = &vp;
|
||||
|
||||
c_handler(&vp, &context);
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// C-handler has not been defined - call the pass-through handler.
|
||||
//
|
||||
|
||||
(this->*cpp_handler)(vp);
|
||||
}
|
||||
}
|
||||
|
||||
void vmexit_c_wrapper_handler::handle_passthrough(passthrough_context* context) noexcept
|
||||
{
|
||||
//
|
||||
// Fetch the handler instance, method and vcpu_t reference
|
||||
// from the pass-trough context and call that method.
|
||||
//
|
||||
|
||||
auto handler_instance = context->handler_instance;
|
||||
auto handler_method = context->handler_method;
|
||||
auto& vp = *context->vcpu;
|
||||
|
||||
(handler_instance->*handler_method)(vp);
|
||||
}
|
||||
|
||||
}
|
||||
42
src/hvpp/hvpp/vmexit/vmexit_c_wrapper.h
Normal file
42
src/hvpp/hvpp/vmexit/vmexit_c_wrapper.h
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#pragma once
|
||||
#include "vmexit_passthrough.h"
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace hvpp {
|
||||
|
||||
class vmexit_c_wrapper_handler
|
||||
: public vmexit_passthrough_handler
|
||||
{
|
||||
public:
|
||||
using c_handler_fn_t = void(*)(
|
||||
void* /* vcpu_t* */,
|
||||
void* /* passthrough_context* */
|
||||
);
|
||||
|
||||
using c_handler_array_t = std::array<c_handler_fn_t, 65>;
|
||||
|
||||
auto initialize(const c_handler_array_t& c_handlers, void* context = nullptr) noexcept -> error_code_t;
|
||||
void destroy() noexcept;
|
||||
|
||||
void handle(vcpu_t& vp) noexcept override;
|
||||
|
||||
private:
|
||||
using passthrough_fn_t = void(*)(void*);
|
||||
|
||||
struct passthrough_context
|
||||
{
|
||||
passthrough_fn_t passthrough_routine;
|
||||
void* context;
|
||||
vmexit_c_wrapper_handler* handler_instance;
|
||||
handler_fn_t handler_method;
|
||||
vcpu_t* vcpu;
|
||||
};
|
||||
|
||||
static void handle_passthrough(passthrough_context* context) noexcept;
|
||||
|
||||
c_handler_array_t c_handlers_;
|
||||
void* context_;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -108,8 +108,8 @@
|
|||
<ClInclude Include="vmexit_custom.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\hvpp\hvpp.vcxproj">
|
||||
<Project>{2fc6c155-2bdf-4761-b9b5-7dfe9c2bb4f4}</Project>
|
||||
<ProjectReference Include="..\hvpp\hvpp-entry.vcxproj">
|
||||
<Project>{d16e66b1-31bc-465f-916e-430803ffde99}</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
|
|
|
|||
71
src/hvppdrv_c/hvppdrv_c.inf
Normal file
71
src/hvppdrv_c/hvppdrv_c.inf
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
;
|
||||
; hvppdrv_c.inf
|
||||
;
|
||||
|
||||
[Version]
|
||||
Signature="$WINDOWS NT$"
|
||||
Class=Sample ; TODO: edit Class
|
||||
ClassGuid={78A1C341-4539-11d3-B88D-00C04FAD5171} ; TODO: edit ClassGuid
|
||||
Provider=%ManufacturerName%
|
||||
CatalogFile=hvppdrv_c.cat
|
||||
DriverVer= ; TODO: set DriverVer in stampinf property pages
|
||||
|
||||
[DestinationDirs]
|
||||
DefaultDestDir = 12
|
||||
hvppdrv_c_Device_CoInstaller_CopyFiles = 11
|
||||
|
||||
; ================= Class section =====================
|
||||
|
||||
[ClassInstall32]
|
||||
Addreg=SampleClassReg
|
||||
|
||||
[SampleClassReg]
|
||||
HKR,,,0,%ClassName%
|
||||
HKR,,Icon,,-5
|
||||
|
||||
[SourceDisksNames]
|
||||
1 = %DiskName%,,,""
|
||||
|
||||
[SourceDisksFiles]
|
||||
hvppdrv_c.sys = 1,,
|
||||
WdfCoInstaller$KMDFCOINSTALLERVERSION$.dll=1 ; make sure the number matches with SourceDisksNames
|
||||
|
||||
;*****************************************
|
||||
; Install Section
|
||||
;*****************************************
|
||||
|
||||
[Manufacturer]
|
||||
%ManufacturerName%=Standard,NT$ARCH$
|
||||
|
||||
[Standard.NT$ARCH$]
|
||||
%hvppdrv_c.DeviceDesc%=hvppdrv_c_Device, Root\hvppdrv_c ; TODO: edit hw-id
|
||||
|
||||
[hvppdrv_c_Device.NT]
|
||||
CopyFiles=Drivers_Dir
|
||||
|
||||
[Drivers_Dir]
|
||||
hvppdrv_c.sys
|
||||
|
||||
;-------------- Service installation
|
||||
[hvppdrv_c_Device.NT.Services]
|
||||
AddService = hvppdrv_c,%SPSVCINST_ASSOCSERVICE%, hvppdrv_c_Service_Inst
|
||||
|
||||
; -------------- hvppdrv_c driver install sections
|
||||
[hvppdrv_c_Service_Inst]
|
||||
DisplayName = %hvppdrv_c.SVCDESC%
|
||||
ServiceType = 1 ; SERVICE_KERNEL_DRIVER
|
||||
StartType = 3 ; SERVICE_DEMAND_START
|
||||
ErrorControl = 1 ; SERVICE_ERROR_NORMAL
|
||||
ServiceBinary = %12%\hvppdrv_c.sys
|
||||
|
||||
;
|
||||
;--- hvppdrv_c_Device Coinstaller installation ------
|
||||
;
|
||||
|
||||
[Strings]
|
||||
SPSVCINST_ASSOCSERVICE= 0x00000002
|
||||
ManufacturerName="<Your manufacturer name>" ;TODO: Replace with your manufacturer name
|
||||
ClassName="Samples" ; TODO: edit ClassName
|
||||
DiskName = "hvppdrv_c Installation Disk"
|
||||
hvppdrv_c.DeviceDesc = "hvppdrv_c Device"
|
||||
hvppdrv_c.SVCDESC = "hvppdrv_c Service"
|
||||
116
src/hvppdrv_c/hvppdrv_c.vcxproj
Normal file
116
src/hvppdrv_c/hvppdrv_c.vcxproj
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{9D8BC3BA-1749-4974-9BEC-00231849A63C}</ProjectGuid>
|
||||
<TemplateGuid>{1bc93793-694f-48fe-9372-81e2b05556fd}</TemplateGuid>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<MinimumVisualStudioVersion>12.0</MinimumVisualStudioVersion>
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform Condition="'$(Platform)' == ''">Win32</Platform>
|
||||
<RootNamespace>hvppdrv_c</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>$(LatestTargetPlatformVersion)</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<TargetVersion>Windows7</TargetVersion>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>WindowsKernelModeDriver10.0</PlatformToolset>
|
||||
<ConfigurationType>Driver</ConfigurationType>
|
||||
<DriverType>WDM</DriverType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<TargetVersion>Windows7</TargetVersion>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>WindowsKernelModeDriver10.0</PlatformToolset>
|
||||
<ConfigurationType>Driver</ConfigurationType>
|
||||
<DriverType>WDM</DriverType>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor>
|
||||
<IncludePath>$(ProjectDir);$(ProjectDir)..\hvpp;$(VC_IncludePath);$(IncludePath);$(KMDF_INC_PATH)$(KMDF_VER_PATH)</IncludePath>
|
||||
<OutDir>$(SolutionDir)bin\$(PlatformShortName)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)bin\obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\</IntDir>
|
||||
<EnableInf2cat>false</EnableInf2cat>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor>
|
||||
<IncludePath>$(ProjectDir);$(ProjectDir)..\hvpp;$(VC_IncludePath);$(IncludePath);$(KMDF_INC_PATH)$(KMDF_VER_PATH)</IncludePath>
|
||||
<OutDir>$(SolutionDir)bin\$(PlatformShortName)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)bin\obj\$(PlatformShortName)\$(Configuration)\$(ProjectName)\</IntDir>
|
||||
<Inf2CatUseLocalTime>true</Inf2CatUseLocalTime>
|
||||
<EnableInf2cat>false</EnableInf2cat>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<DisableSpecificWarnings>4201;4748;%(DisableSpecificWarnings)</DisableSpecificWarnings>
|
||||
<LanguageStandard>stdcpplatest</LanguageStandard>
|
||||
<ObjectFileName>$(IntDir)%(RelativeDir)%(Filename)%(Extension).obj</ObjectFileName>
|
||||
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||
<SupportJustMyCode>false</SupportJustMyCode>
|
||||
</ClCompile>
|
||||
<Inf />
|
||||
<MASM />
|
||||
<Link>
|
||||
<GenerateDebugInformation>DebugFull</GenerateDebugInformation>
|
||||
</Link>
|
||||
<PostBuildEvent />
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<DisableSpecificWarnings>4201;4603;4627;4986;4987;4996;%(DisableSpecificWarnings)</DisableSpecificWarnings>
|
||||
<LanguageStandard>stdcpplatest</LanguageStandard>
|
||||
<ObjectFileName>$(IntDir)%(RelativeDir)%(Filename)%(Extension).obj</ObjectFileName>
|
||||
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
||||
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
</ClCompile>
|
||||
<Inf />
|
||||
<MASM />
|
||||
<Link>
|
||||
<GenerateDebugInformation>DebugFull</GenerateDebugInformation>
|
||||
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
|
||||
</Link>
|
||||
<PostBuildEvent />
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<Inf Include="hvppdrv_c.inf">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
</Inf>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<FilesToPackage Include="$(TargetPath)" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.c" />
|
||||
<ClCompile Include="vmexit_custom.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="vmexit_custom.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\hvpp\hvpp.vcxproj">
|
||||
<Project>{2fc6c155-2bdf-4761-b9b5-7dfe9c2bb4f4}</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
39
src/hvppdrv_c/hvppdrv_c.vcxproj.filters
Normal file
39
src/hvppdrv_c/hvppdrv_c.vcxproj.filters
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Driver Files">
|
||||
<UniqueIdentifier>{8E41214B-6785-4CFE-B992-037D68949A14}</UniqueIdentifier>
|
||||
<Extensions>inf;inv;inx;mof;mc;</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="vmexit_custom.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="vmexit_custom.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Inf Include="hvppdrv_c.inf">
|
||||
<Filter>Driver Files</Filter>
|
||||
</Inf>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
4
src/hvppdrv_c/hvppdrv_c.vcxproj.user
Normal file
4
src/hvppdrv_c/hvppdrv_c.vcxproj.user
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup />
|
||||
</Project>
|
||||
61
src/hvppdrv_c/main.c
Normal file
61
src/hvppdrv_c/main.c
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
#include <ntddk.h>
|
||||
#include <hvpp/hvpp.h>
|
||||
|
||||
#include "vmexit_custom.h"
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// hvppdrv, but rewritten to use C interface.
|
||||
// Note that support for IOCTL is missing.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
PHVPP Hypervisor;
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
DriverUnload(
|
||||
_In_ PDRIVER_OBJECT DriverObject
|
||||
)
|
||||
{
|
||||
UNREFERENCED_PARAMETER(DriverObject);
|
||||
|
||||
HvppDestroy(Hypervisor);
|
||||
}
|
||||
|
||||
NTSTATUS
|
||||
NTAPI
|
||||
DriverEntry(
|
||||
_In_ PDRIVER_OBJECT DriverObject,
|
||||
_In_ PUNICODE_STRING RegistryPath
|
||||
)
|
||||
{
|
||||
UNREFERENCED_PARAMETER(RegistryPath);
|
||||
|
||||
NTSTATUS Status;
|
||||
|
||||
DriverObject->DriverUnload = &DriverUnload;
|
||||
|
||||
Status = HvppInitialize(&Hypervisor);
|
||||
|
||||
if (!NT_SUCCESS(Status))
|
||||
{
|
||||
return Status;
|
||||
}
|
||||
|
||||
VMEXIT_HANDLER VmExitHandler = { {
|
||||
[VMEXIT_REASON_EXECUTE_CPUID] = &HvppHandleExecuteCpuid,
|
||||
[VMEXIT_REASON_EXECUTE_VMCALL] = &HvppHandleExecuteVmcall,
|
||||
[VMEXIT_REASON_EPT_VIOLATION] = &HvppHandleEptViolation,
|
||||
} };
|
||||
|
||||
Status = HvppStart(Hypervisor, &VmExitHandler);
|
||||
|
||||
if (!NT_SUCCESS(Status))
|
||||
{
|
||||
HvppDestroy(Hypervisor);
|
||||
return Status;
|
||||
}
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
143
src/hvppdrv_c/vmexit_custom.c
Normal file
143
src/hvppdrv_c/vmexit_custom.c
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
#include "vmexit_custom.h"
|
||||
|
||||
#pragma warning(disable : 4204)
|
||||
|
||||
#define EPT_PD_MASK (~((ULONG64)(0x200000 - 1)))
|
||||
#define EPT_PD_PAGE_ALIGN(PhysicalAddress) \
|
||||
((PHYSICAL_ADDRESS) { \
|
||||
.QuadPart = ((PhysicalAddress).QuadPart & EPT_PD_MASK) \
|
||||
})
|
||||
|
||||
typedef struct _PER_VCPU_DATA
|
||||
{
|
||||
PHYSICAL_ADDRESS PageRead;
|
||||
PHYSICAL_ADDRESS PageExec;
|
||||
} PER_VCPU_DATA, *PPER_VCPU_DATA;
|
||||
|
||||
PER_VCPU_DATA PerVcpuData[32];
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppHandleExecuteCpuid(
|
||||
_In_ PVCPU Vcpu,
|
||||
_In_ PVOID Passthrough
|
||||
)
|
||||
{
|
||||
PVCPU_CONTEXT Context = HvppVcpuExitContext(Vcpu);
|
||||
|
||||
if (Context->Eax == 'ppvh')
|
||||
{
|
||||
Context->Rax = 'lleh';
|
||||
Context->Rbx = 'rf o';
|
||||
Context->Rcx = 'h mo';
|
||||
Context->Rdx = 'ppv';
|
||||
}
|
||||
else
|
||||
{
|
||||
HvppVmExitPassthrough(Passthrough);
|
||||
}
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppHandleExecuteVmcall(
|
||||
_In_ PVCPU Vcpu,
|
||||
_In_ PVOID Passthrough
|
||||
)
|
||||
{
|
||||
UNREFERENCED_PARAMETER(Vcpu);
|
||||
|
||||
PVCPU_CONTEXT Context = HvppVcpuExitContext(Vcpu);
|
||||
PEPT Ept = HvppVcpuGetEpt(Vcpu);
|
||||
|
||||
PPER_VCPU_DATA Data = &PerVcpuData[KeGetCurrentProcessorNumberEx(NULL)];
|
||||
|
||||
switch (Context->Rcx)
|
||||
{
|
||||
case 0xC1:
|
||||
{
|
||||
ULONG_PTR Cr3;
|
||||
HvppAttachAddressSpace(&Cr3);
|
||||
Data->PageRead = MmGetPhysicalAddress(Context->RdxAsPointer);
|
||||
Data->PageExec = MmGetPhysicalAddress(Context->R8AsPointer);
|
||||
HvppDetachAddressSpace(Cr3);
|
||||
}
|
||||
|
||||
HvppTrace("vmcall (hook) EXEC: 0x%p READ: 0x%p",
|
||||
Data->PageExec.QuadPart,
|
||||
Data->PageRead.QuadPart);
|
||||
|
||||
HvppEptSplit2MbTo4Kb(Ept,
|
||||
EPT_PD_PAGE_ALIGN(Data->PageExec),
|
||||
EPT_PD_PAGE_ALIGN(Data->PageExec));
|
||||
|
||||
HvppEptMap4Kb(Ept,
|
||||
Data->PageExec,
|
||||
Data->PageExec,
|
||||
EPT_ACCESS_EXECUTE);
|
||||
|
||||
HvppInveptSingleContext(HvppEptGetEptPointer(Ept));
|
||||
break;
|
||||
|
||||
case 0xC2:
|
||||
HvppTrace("vmcall (unhook)");
|
||||
|
||||
HvppEptJoin4KbTo2Mb(Ept,
|
||||
EPT_PD_PAGE_ALIGN(Data->PageExec),
|
||||
EPT_PD_PAGE_ALIGN(Data->PageExec));
|
||||
|
||||
HvppInveptSingleContext(HvppEptGetEptPointer(Ept));
|
||||
break;
|
||||
|
||||
default:
|
||||
HvppVmExitPassthrough(Passthrough);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppHandleEptViolation(
|
||||
_In_ PVCPU Vcpu,
|
||||
_In_ PVOID Passthrough
|
||||
)
|
||||
{
|
||||
UNREFERENCED_PARAMETER(Passthrough);
|
||||
|
||||
VMX_EXIT_QUALIFICATION_EPT_VIOLATION EptViolation;
|
||||
PHYSICAL_ADDRESS GuestPhysicalAddress;
|
||||
PVOID GuestLinearAddress;
|
||||
|
||||
EptViolation.Flags = HvppVmRead(VMCS_VMEXIT_QUALIFICATION);
|
||||
GuestPhysicalAddress.QuadPart = (LONGLONG)HvppVmRead(VMCS_VMEXIT_GUEST_PHYSICAL_ADDRESS);
|
||||
GuestLinearAddress = (PVOID) HvppVmRead(VMCS_VMEXIT_GUEST_LINEAR_ADDRESS);
|
||||
|
||||
PEPT Ept = HvppVcpuGetEpt(Vcpu);
|
||||
|
||||
PPER_VCPU_DATA Data = &PerVcpuData[KeGetCurrentProcessorNumberEx(NULL)];
|
||||
|
||||
if (EptViolation.DataRead || EptViolation.DataWrite)
|
||||
{
|
||||
HvppTrace("data_read LA: 0x%p PA: 0x%p",
|
||||
GuestLinearAddress,
|
||||
GuestPhysicalAddress.QuadPart);
|
||||
|
||||
HvppEptMap4Kb(Ept,
|
||||
Data->PageExec,
|
||||
Data->PageRead,
|
||||
EPT_ACCESS_READ_WRITE);
|
||||
}
|
||||
else if (EptViolation.DataExecute)
|
||||
{
|
||||
HvppTrace("data_execute LA: 0x%p PA: 0x%p",
|
||||
GuestLinearAddress,
|
||||
GuestPhysicalAddress.QuadPart);
|
||||
|
||||
HvppEptMap4Kb(Ept,
|
||||
Data->PageExec,
|
||||
Data->PageExec,
|
||||
EPT_ACCESS_EXECUTE);
|
||||
}
|
||||
|
||||
HvppVcpuSuppressRipAdjust(Vcpu);
|
||||
}
|
||||
24
src/hvppdrv_c/vmexit_custom.h
Normal file
24
src/hvppdrv_c/vmexit_custom.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
#include <ntddk.h>
|
||||
#include <hvpp/hvpp.h>
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppHandleExecuteCpuid(
|
||||
_In_ PVCPU Vcpu,
|
||||
_In_ PVOID Passthrough
|
||||
);
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppHandleExecuteVmcall(
|
||||
_In_ PVCPU Vcpu,
|
||||
_In_ PVOID Passthrough
|
||||
);
|
||||
|
||||
VOID
|
||||
NTAPI
|
||||
HvppHandleEptViolation(
|
||||
_In_ PVCPU Vcpu,
|
||||
_In_ PVOID Passthrough
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue