1
0
mirror of https://github.com/momo5502/hypervisor.git synced 2025-07-05 10:41:50 +00:00

Add win7 compatibility

This commit is contained in:
momo5502
2022-03-27 12:31:13 +02:00
parent 1bbd9e9c73
commit 2c48c4b595
9 changed files with 116 additions and 27 deletions

View File

@ -1,26 +1,46 @@
#include "std_include.hpp"
#include "memory.hpp"
#include "string.hpp"
namespace memory
{
namespace
{
using mm_allocate_contiguous_node_memory = decltype(MmAllocateContiguousNodeMemory)*;
mm_allocate_contiguous_node_memory get_mm_allocate_contiguous_node_memory()
{
static bool fetched{false};
static mm_allocate_contiguous_node_memory address{nullptr};
if (!fetched)
{
fetched = true;
auto function_name = string::get_unicode_string(L"MmAllocateContiguousNodeMemory");
address = static_cast<mm_allocate_contiguous_node_memory>(MmGetSystemRoutineAddress(&function_name));
}
return address;
}
void* allocate_aligned_memory_internal(const size_t size)
{
PHYSICAL_ADDRESS lowest{}, highest{};
lowest.QuadPart = 0;
highest.QuadPart = lowest.QuadPart - 1;
#if (NTDDI_VERSION >= NTDDI_VISTA)
return MmAllocateContiguousNodeMemory(size,
lowest,
highest,
lowest,
PAGE_READWRITE,
KeGetCurrentNodeNumber());
#else
return MmAllocateContiguousMemory(size, highest);
#endif
const auto allocate_node_mem = get_mm_allocate_contiguous_node_memory();
if (allocate_node_mem)
{
return allocate_node_mem(size,
lowest,
highest,
lowest,
PAGE_READWRITE,
KeGetCurrentNodeNumber());
}
return MmAllocateContiguousMemory(size, highest);
}
}
@ -48,6 +68,18 @@ namespace memory
return memory;
}
void* get_physical_address(void* address)
{
return reinterpret_cast<void*>(MmGetPhysicalAddress(address).QuadPart);
}
void* get_virtual_address(void* address)
{
PHYSICAL_ADDRESS physical_address{};
physical_address.QuadPart = reinterpret_cast<LONGLONG>(address);
return MmGetVirtualForPhysical(physical_address);
}
_Must_inspect_result_
_IRQL_requires_max_(DISPATCH_LEVEL)