Decouple pool initialization and validation from allocation logic
All checks were successful
Builds / ExectOS (i686, debug) (push) Successful in 30s
Builds / ExectOS (amd64, debug) (push) Successful in 33s
Builds / ExectOS (i686, release) (push) Successful in 46s
Builds / ExectOS (amd64, release) (push) Successful in 50s

This commit is contained in:
2026-03-18 20:31:06 +01:00
parent 876923e107
commit afb20a1796
10 changed files with 250 additions and 169 deletions

View File

@@ -2,7 +2,7 @@
* PROJECT: ExectOS
* COPYRIGHT: See COPYING.md in the top level directory
* FILE: xtoskrnl/mm/alloc.cc
* DESCRIPTION: Memory manager pool allocation
* DESCRIPTION: Memory Manager pool allocator
* DEVELOPERS: Aiken Harris <harraiken91@gmail.com>
*/
@@ -332,13 +332,17 @@ MM::Allocator::AllocatePool(IN MMPOOL_TYPE PoolType,
* @param VirtualAddress
* Supplies the base virtual address of the non-paged pool pages allocation to free.
*
* @param PagesFreed
* Supplies a pointer to a variable that will receive the number of pages freed.
*
* @return This routine returns a status code.
*
* @since XT 1.0
*/
XTAPI
XTSTATUS
MM::Allocator::FreeNonPagedPoolPages(IN PVOID VirtualAddress)
MM::Allocator::FreeNonPagedPoolPages(IN PVOID VirtualAddress,
OUT PPFN_NUMBER PagesFreed)
{
PMMFREE_POOL_ENTRY FreePage, NextPage, LastPage;
PFN_COUNT FreePages, Pages;
@@ -505,6 +509,13 @@ MM::Allocator::FreeNonPagedPoolPages(IN PVOID VirtualAddress)
NextPage = (PMMFREE_POOL_ENTRY)((ULONG_PTR)NextPage + MM_PAGE_SIZE);
}
/* Check if a page count was requested */
if(PagesFreed != NULLPTR)
{
/* Return the number of pages freed */
*PagesFreed = FreePages;
}
/* Return success */
return STATUS_SUCCESS;
}
@@ -515,13 +526,17 @@ MM::Allocator::FreeNonPagedPoolPages(IN PVOID VirtualAddress)
* @param VirtualAddress
* Supplies the base virtual address of the paged pool pages allocation to free.
*
* @param PagesFreed
* Supplies a pointer to a variable that will receive the number of pages freed.
*
* @return This routine returns a status code.
*
* @since XT 1.0
*/
XTAPI
XTSTATUS
MM::Allocator::FreePagedPoolPages(IN PVOID VirtualAddress)
MM::Allocator::FreePagedPoolPages(IN PVOID VirtualAddress,
OUT PPFN_NUMBER PagesFreed)
{
UNIMPLEMENTED;
@@ -542,6 +557,27 @@ MM::Allocator::FreePagedPoolPages(IN PVOID VirtualAddress)
XTAPI
XTSTATUS
MM::Allocator::FreePages(IN PVOID VirtualAddress)
{
return FreePages(VirtualAddress, NULLPTR);
}
/**
* Frees a previously allocated block of pages.
*
* @param VirtualAddress
* Supplies the base virtual address of the pages allocation to free.
*
* @param PagesFreed
* Supplies a pointer to a variable that will receive the number of pages freed.
*
* @return This routine returns a status code.
*
* @since XT 1.0
*/
XTAPI
XTSTATUS
MM::Allocator::FreePages(IN PVOID VirtualAddress,
OUT PPFN_NUMBER PagesFreed)
{
PMMMEMORY_LAYOUT MemoryLayout;
@@ -552,12 +588,12 @@ MM::Allocator::FreePages(IN PVOID VirtualAddress)
if(VirtualAddress >= MemoryLayout->PagedPoolStart && VirtualAddress < MemoryLayout->PagedPoolEnd)
{
/* Free pages from the paged pool */
return FreePagedPoolPages(VirtualAddress);
return FreePagedPoolPages(VirtualAddress, PagesFreed);
}
else
{
/* Free pages from the non-paged pool */
return FreeNonPagedPoolPages(VirtualAddress);
return FreeNonPagedPoolPages(VirtualAddress, PagesFreed);
}
}
@@ -602,134 +638,3 @@ MM::Allocator::FreePool(IN PVOID VirtualAddress,
/* Free pages */
return FreePages(VirtualAddress);
}
/**
* Initializes the non-paged pool for memory allocator.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTAPI
VOID
MM::Allocator::InitializeNonPagedPool(VOID)
{
PMMFREE_POOL_ENTRY FreePage, SetupPage;
PMMMEMORY_LAYOUT MemoryLayout;
ULONG Index;
/* Retrieve memory layout */
MemoryLayout = MM::Manager::GetMemoryLayout();
/* Map PTEs for the non-paged pool */
MapNonPagedPool();
/* Iterate over the free page list heads */
for(Index = 0; Index < MM_MAX_FREE_PAGE_LIST_HEADS; Index++)
{
/* Initialize a free page list head */
RTL::LinkedList::InitializeListHead(&NonPagedPoolFreeList[Index]);
}
/* Take the first free page from the pool and set its size */
FreePage = (PMMFREE_POOL_ENTRY)MemoryLayout->NonPagedPoolStart;
FreePage->Size = MemoryLayout->NonPagedPoolSize;
/* Take number of pages in the pool */
Index = (ULONG)(MemoryLayout->NonPagedPoolSize - 1);
if(Index >= MM_MAX_FREE_PAGE_LIST_HEADS)
{
/* Number of pages exceeds the number of free page list heads */
Index = MM_MAX_FREE_PAGE_LIST_HEADS - 1;
}
/* Insert the first free page into the free page list */
RTL::LinkedList::InsertHeadList(&NonPagedPoolFreeList[Index], &FreePage->List);
/* Create a free page for each page in the pool */
SetupPage = FreePage;
for(Index = 0; Index < MemoryLayout->NonPagedPoolSize; Index++)
{
/* Initialize the owner for each page */
SetupPage->Owner = FreePage;
SetupPage = (PMMFREE_POOL_ENTRY)((ULONG_PTR)SetupPage + MM_PAGE_SIZE);
}
/* Store first and last allocated non-paged pool page */
NonPagedPoolFrameStart = MM::Paging::GetPageFrameNumber(MM::Paging::GetPteAddress(MemoryLayout->NonPagedPoolStart));
NonPagedPoolFrameEnd = MM::Paging::GetPageFrameNumber(MM::Paging::GetPteAddress(MemoryLayout->NonPagedPoolEnd));
/* Initialize system PTE pool for the non-paged expansion pool */
Pte::InitializeSystemPtePool(Paging::GetNextPte(Paging::GetPteAddress(MemoryLayout->NonPagedExpansionPoolStart)),
MemoryLayout->NonPagedExpansionPoolSize - 2,
NonPagedPoolExpansion);
}
/**
* Initializes the non-paged pool for memory allocator.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTAPI
VOID
MM::Allocator::InitializePagedPool(VOID)
{
UNIMPLEMENTED;
}
/**
* Validates the run level for the specified pool. If the run level is invalid, the kernel panics.
*
* @param PoolType
* Supplies the pool type.
*
* @param Bytes
* Supplies the size of the allocation.
*
* @param Entry
* Supplies a pointer to the allocation entry.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTINLINE
VOID
MM::Allocator::VerifyRunLevel(IN MMPOOL_TYPE PoolType,
IN SIZE_T Bytes,
IN PVOID Entry)
{
KRUNLEVEL RunLevel;
/* Get current run level */
RunLevel = KE::RunLevel::GetCurrentRunLevel();
/* Validate run level */
if((PoolType & MM_POOL_TYPE_MASK) == PagedPool)
{
/* Paged pool runs up to APC level */
if(RunLevel <= APC_LEVEL)
{
/* Run level is valid */
return;
}
}
else
{
/* Non-paged pool runs up to DISPATCH_LEVEL */
if(RunLevel <= DISPATCH_LEVEL)
{
/* Run level is valid */
return;
}
}
/* Invalid run level for specified pool, kernel panic */
KE::Crash::Panic(0xC2,
(Entry ? MM_POOL_INVALID_FREE_RUNLEVEL : MM_POOL_INVALID_ALLOC_RUNLEVEL),
RunLevel,
PoolType,
(Entry ? (ULONG_PTR)Entry : Bytes));
}