From c8868ead47518d610c20d96566c675acddd76557 Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Wed, 13 May 2026 10:08:30 +0200 Subject: [PATCH] Add AllocateRealModeMemory routine for low memory allocations --- xtoskrnl/includes/mm/hlpool.hh | 2 ++ xtoskrnl/mm/hlpool.cc | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/xtoskrnl/includes/mm/hlpool.hh b/xtoskrnl/includes/mm/hlpool.hh index de53a94..41daac4 100644 --- a/xtoskrnl/includes/mm/hlpool.hh +++ b/xtoskrnl/includes/mm/hlpool.hh @@ -27,6 +27,8 @@ namespace MM IN BOOLEAN Aligned, IN ULONGLONG MaximumAddress, OUT PPHYSICAL_ADDRESS Buffer); + STATIC XTAPI XTSTATUS AllocateRealModeMemory(IN PFN_NUMBER PageCount, + OUT PVOID *MemoryAddress); STATIC XTAPI XTSTATUS MapHardwareMemory(IN PHYSICAL_ADDRESS PhysicalAddress, IN PFN_NUMBER PageCount, IN BOOLEAN FlushTlb, diff --git a/xtoskrnl/mm/hlpool.cc b/xtoskrnl/mm/hlpool.cc index 847d6ea..d35a776 100644 --- a/xtoskrnl/mm/hlpool.cc +++ b/xtoskrnl/mm/hlpool.cc @@ -4,6 +4,7 @@ * FILE: xtoskrnl/mm/hlpool.cc * DESCRIPTION: Hardware layer pool memory management * DEVELOPERS: Rafal Kupiec + * Aiken Harris */ #include @@ -146,6 +147,51 @@ MM::HardwarePool::AllocateHardwareMemory(IN PFN_NUMBER PageCount, return STATUS_SUCCESS; } +/** + * Allocates a physical page in low memory (addressable in real-mode) and maps it into the virtual address space. + * + * @param TrampolineAddress + * Supplies a pointer to a variable that receives the identity-mapped virtual address of the allocated memory. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTAPI +XTSTATUS +MM::HardwarePool::AllocateRealModeMemory(IN PFN_NUMBER PageCount, + OUT PVOID *MemoryAddress) +{ + PHYSICAL_ADDRESS PhysicalAddress; + PFN_NUMBER PageFrameNumber; + PVOID VirtualAddress; + XTSTATUS Status; + + /* Allocate physical memory in first 1MB */ + Status = AllocateHardwareMemory(PageCount, TRUE, 0x100000, &PhysicalAddress); + if(Status != STATUS_SUCCESS) + { + /* Failed to allocate memory, return error */ + return Status; + } + + /* Calculate virtual address and page frame number */ + VirtualAddress = (PVOID)(ULONG_PTR)PhysicalAddress.QuadPart; + PageFrameNumber = PhysicalAddress.QuadPart >> MM_PAGE_SHIFT; + + /* Identity map the memory to the virtual address */ + Status = MM::Paging::MapVirtualAddress(VirtualAddress, PageFrameNumber, MM_PTE_EXECUTE_READWRITE); + if(Status != STATUS_SUCCESS) + { + /* Failed to map memory, return error */ + return Status; + } + + /* Set the trampoline virtual address and return success */ + *MemoryAddress = VirtualAddress; + return STATUS_SUCCESS; +} + /** * Maps physical address to the virtual memory area used by kernel hardware layer. *