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:
@ -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)
|
||||
|
||||
|
Reference in New Issue
Block a user