More refactoring

This commit is contained in:
momo5502 2022-04-02 17:44:58 +02:00
parent a284af51ec
commit 0ea1ae9643
3 changed files with 29 additions and 51 deletions

View File

@ -99,16 +99,19 @@ void hypervisor::enable()
{ {
const auto cr3 = __readcr3(); const auto cr3 = __readcr3();
bool success = true; volatile long failures = 0;
thread::dispatch_on_all_cores([&]() thread::dispatch_on_all_cores([&]()
{ {
success &= this->try_enable_core(cr3); if(!this->try_enable_core(cr3))
}, true); {
InterlockedIncrement(&failures);
}
});
if (!success) if (failures)
{ {
this->disable(); this->disable();
//throw std::runtime_error("Hypervisor initialization failed"); throw std::runtime_error("Hypervisor initialization failed");
} }
} }
@ -277,9 +280,6 @@ ShvVmxMtrrAdjustEffectiveMemoryType(
void ShvVmxEptInitialize(vmx::vm_state* VpData) void ShvVmxEptInitialize(vmx::vm_state* VpData)
{ {
UINT32 i, j;
vmx::pdpte tempEpdpte;
// //
// Fill out the EPML4E which covers the first 512GB of RAM // Fill out the EPML4E which covers the first 512GB of RAM
// //
@ -292,14 +292,17 @@ void ShvVmxEptInitialize(vmx::vm_state* VpData)
// //
// Fill out a RWX PDPTE // Fill out a RWX PDPTE
// //
tempEpdpte.full = 0; epdpte temp_epdpte;
tempEpdpte.read = tempEpdpte.write = tempEpdpte.execute = 1; temp_epdpte.flags = 0;
temp_epdpte.read_access = 1;
temp_epdpte.write_access = 1;
temp_epdpte.execute_access = 1;
// //
// Construct EPT identity map for every 1GB of RAM // Construct EPT identity map for every 1GB of RAM
// //
__stosq((UINT64*)VpData->epdpt, tempEpdpte.full, PDPTE_ENTRY_COUNT); __stosq((UINT64*)VpData->epdpt, temp_epdpte.flags, EPT_PDPTE_ENTRY_COUNT);
for (i = 0; i < PDPTE_ENTRY_COUNT; i++) for (auto i = 0; i < EPT_PDPTE_ENTRY_COUNT; i++)
{ {
// //
// Set the page frame number of the PDE table // Set the page frame number of the PDE table
@ -320,13 +323,13 @@ void ShvVmxEptInitialize(vmx::vm_state* VpData)
// //
// Loop every 1GB of RAM (described by the PDPTE) // Loop every 1GB of RAM (described by the PDPTE)
// //
__stosq((UINT64*)VpData->epde, temp_epde.flags, PDPTE_ENTRY_COUNT * PDE_ENTRY_COUNT); __stosq((UINT64*)VpData->epde, temp_epde.flags, EPT_PDPTE_ENTRY_COUNT * EPT_PDE_ENTRY_COUNT);
for (i = 0; i < PDPTE_ENTRY_COUNT; i++) for (auto i = 0; i < EPT_PDPTE_ENTRY_COUNT; i++)
{ {
// //
// Construct EPT identity map for every 2MB of RAM // Construct EPT identity map for every 2MB of RAM
// //
for (j = 0; j < PDE_ENTRY_COUNT; j++) for (auto j = 0; j < EPT_PDE_ENTRY_COUNT; j++)
{ {
VpData->epde[i][j].page_frame_number = (i * 512) + j; VpData->epde[i][j].page_frame_number = (i * 512) + j;
VpData->epde[i][j].memory_type = ShvVmxMtrrAdjustEffectiveMemoryType(VpData, VpData->epde[i][j].memory_type = ShvVmxMtrrAdjustEffectiveMemoryType(VpData,

View File

@ -3,6 +3,8 @@
#include <ntddk.h> #include <ntddk.h>
#include <intrin.h> #include <intrin.h>
#include <ia32.hpp>
#include "stdint.hpp" #include "stdint.hpp"
#include "nt_ext.hpp" #include "nt_ext.hpp"
#include "new.hpp" #include "new.hpp"

View File

@ -1,11 +1,5 @@
#pragma once #pragma once
#include <ia32.hpp>
#define PML4E_ENTRY_COUNT 512 // EPT_PML4E_ENTRY_COUNT
#define PDPTE_ENTRY_COUNT 512 // EPT_PDPTE_ENTRY_COUNT
#define PDE_ENTRY_COUNT 512 // EPT_PDE_ENTRY_COUNT
namespace vmx namespace vmx
{ {
struct vmcs struct vmcs
@ -38,29 +32,6 @@ namespace vmx
}; };
}; };
struct pdpte
{
union
{
struct
{
uint64_t read : 1;
uint64_t write : 1;
uint64_t execute : 1;
uint64_t reserved : 5;
uint64_t accessed : 1;
uint64_t software_use : 1;
uint64_t user_mode_execute : 1;
uint64_t software_use2 : 1;
uint64_t page_frame_number : 36;
uint64_t reserved_high : 4;
uint64_t software_use_high : 12;
};
uint64_t full;
};
};
struct kdescriptor struct kdescriptor
{ {
uint16_t pad[3]; uint16_t pad[3];
@ -90,11 +61,13 @@ namespace vmx
uint64_t physical_address_max; uint64_t physical_address_max;
}; };
#define DECLSPEC_PAGE_ALIGN DECLSPEC_ALIGN(PAGE_SIZE)
struct vm_state struct vm_state
{ {
union union
{ {
DECLSPEC_ALIGN(PAGE_SIZE) uint8_t stack_buffer[KERNEL_STACK_SIZE]{}; DECLSPEC_PAGE_ALIGN uint8_t stack_buffer[KERNEL_STACK_SIZE]{};
struct struct
{ {
@ -111,12 +84,12 @@ namespace vmx
}; };
}; };
DECLSPEC_ALIGN(PAGE_SIZE) uint8_t msr_bitmap[PAGE_SIZE]{}; DECLSPEC_PAGE_ALIGN uint8_t msr_bitmap[PAGE_SIZE]{};
DECLSPEC_ALIGN(PAGE_SIZE) epml4e epml4[PML4E_ENTRY_COUNT]{}; DECLSPEC_PAGE_ALIGN epml4e epml4[EPT_PML4E_ENTRY_COUNT]{};
DECLSPEC_ALIGN(PAGE_SIZE) pdpte epdpt[PDPTE_ENTRY_COUNT]{}; DECLSPEC_PAGE_ALIGN epdpte epdpt[EPT_PDPTE_ENTRY_COUNT]{};
DECLSPEC_ALIGN(PAGE_SIZE) epde_2mb epde[PDPTE_ENTRY_COUNT][PDE_ENTRY_COUNT]{}; DECLSPEC_PAGE_ALIGN epde_2mb epde[EPT_PDPTE_ENTRY_COUNT][EPT_PDE_ENTRY_COUNT]{};
DECLSPEC_ALIGN(PAGE_SIZE) vmcs vmx_on{}; DECLSPEC_PAGE_ALIGN vmcs vmx_on{};
DECLSPEC_ALIGN(PAGE_SIZE) vmcs vmcs{}; DECLSPEC_PAGE_ALIGN vmcs vmcs{};
}; };
} }