[BOOT:MM] More work on memory manager
This commit is contained in:
@@ -17,12 +17,6 @@ Abstract:
|
||||
#include "bootlib.h"
|
||||
#include "mm.h"
|
||||
|
||||
#define MAX_STATIC_DESCRIPTOR_COUNT 1024
|
||||
|
||||
BOOT_MEMORY_DESCRIPTOR MmStaticMemoryDescriptors[MAX_STATIC_DESCRIPTOR_COUNT];
|
||||
PBOOT_MEMORY_DESCRIPTOR MmGlobalMemoryDescriptors;
|
||||
ULONG MmGlobalMemoryDescriptorCount;
|
||||
|
||||
NTSTATUS
|
||||
BlpMmInitialize (
|
||||
IN PBOOT_MEMORY_INFO MemoryInfo,
|
||||
@@ -69,14 +63,12 @@ Return Value:
|
||||
}
|
||||
|
||||
//
|
||||
// Initialize memory descriptors.
|
||||
// Initialize memory descriptor manager.
|
||||
//
|
||||
MmGlobalMemoryDescriptors = &MmStaticMemoryDescriptors[0];
|
||||
MmGlobalMemoryDescriptorCount = MAX_STATIC_DESCRIPTOR_COUNT;
|
||||
RtlZeroMemory(MmGlobalMemoryDescriptors, MAX_STATIC_DESCRIPTOR_COUNT * sizeof(BOOT_MEMORY_DESCRIPTOR));
|
||||
MmMdInitialize(0, LibraryParameters);
|
||||
|
||||
//
|
||||
// Initialize the page allocator.
|
||||
// Initialize page allocator.
|
||||
//
|
||||
Status = MmPaInitialize(MemoryInfo, LibraryParameters->MinimumPageAllocation);
|
||||
if (!NT_SUCCESS(Status)) {
|
||||
|
206
BOOT/ENVIRON/LIB/MM/mmmd.c
Normal file
206
BOOT/ENVIRON/LIB/MM/mmmd.c
Normal file
@@ -0,0 +1,206 @@
|
||||
/*++
|
||||
|
||||
Copyright (c) 2024, Quinn Stephens.
|
||||
Provided under the BSD 3-Clause license.
|
||||
|
||||
Module Name:
|
||||
|
||||
mmmd.c
|
||||
|
||||
Abstract:
|
||||
|
||||
Provides memory descriptor routines.
|
||||
|
||||
--*/
|
||||
|
||||
#include <ntrtl.h>
|
||||
#include "bootmgr.h"
|
||||
#include "mm.h"
|
||||
|
||||
#define MAX_STATIC_DESCRIPTOR_COUNT 1024
|
||||
|
||||
MEMORY_DESCRIPTOR MmStaticMemoryDescriptors[MAX_STATIC_DESCRIPTOR_COUNT];
|
||||
|
||||
PMEMORY_DESCRIPTOR MmGlobalMemoryDescriptors;
|
||||
ULONG MmGlobalMemoryDescriptorCount;
|
||||
|
||||
PMEMORY_DESCRIPTOR MmDynamicMemoryDescriptors;
|
||||
ULONG MmDynamicMemoryDescriptorCount;
|
||||
|
||||
VOID
|
||||
MmMdRemoveDescriptorFromList (
|
||||
IN PMEMORY_DESCRIPTOR_LIST Mdl,
|
||||
IN PMEMORY_DESCRIPTOR Descriptor
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Removes a descriptor from a MDL.
|
||||
|
||||
Arguments:
|
||||
|
||||
Mdl - the MDL to remove Descriptor from.
|
||||
|
||||
Descriptor - the descriptor to remove from Mdl.
|
||||
|
||||
Return Value:
|
||||
|
||||
None.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
PLIST_ENTRY Blink;
|
||||
|
||||
Blink = Descriptor->ListEntry.Blink;
|
||||
|
||||
//
|
||||
// Remove the descriptor from the MDL.
|
||||
//
|
||||
RemoveEntryList(&Descriptor->ListEntry);
|
||||
|
||||
//
|
||||
// Check if the removed descriptor was cached.
|
||||
//
|
||||
if (Mdl->Current != &Descriptor->ListEntry) {
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Cache the previous descriptor if possible.
|
||||
//
|
||||
if (
|
||||
(
|
||||
(ULONG_PTR)Blink < (ULONG_PTR)MmGlobalMemoryDescriptors
|
||||
|| (ULONG_PTR)Blink >= (ULONG_PTR)&MmGlobalMemoryDescriptors[MmGlobalMemoryDescriptorCount]
|
||||
)
|
||||
&& Blink != Mdl->Head
|
||||
) {
|
||||
Mdl->Current = Blink;
|
||||
} else {
|
||||
Mdl->Current = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
NTSTATUS
|
||||
MmMdFreeDescriptor (
|
||||
IN PMEMORY_DESCRIPTOR Descriptor
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Frees a memory descriptor.
|
||||
|
||||
Arguments:
|
||||
|
||||
Descriptor - the descriptor to free.
|
||||
|
||||
Return Value:
|
||||
|
||||
STATUS_SUCCESS if successful,
|
||||
other NTSTATUS value if an error occurs.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
if (
|
||||
(
|
||||
MmDynamicMemoryDescriptors != NULL
|
||||
&& (ULONG_PTR)Descriptor >= (ULONG_PTR)MmDynamicMemoryDescriptors
|
||||
&& (ULONG_PTR)Descriptor <= (ULONG_PTR)&MmDynamicMemoryDescriptors[MmDynamicMemoryDescriptorCount]
|
||||
)
|
||||
|| (
|
||||
(ULONG_PTR)Descriptor >= (ULONG_PTR)MmStaticMemoryDescriptors
|
||||
&& (ULONG_PTR)Descriptor <= (ULONG_PTR)&MmStaticMemoryDescriptors[MAX_STATIC_DESCRIPTOR_COUNT]
|
||||
)
|
||||
) {
|
||||
//
|
||||
// Clear the descriptor from static/dynamic MDL.
|
||||
//
|
||||
RtlZeroMemory(Descriptor, sizeof(MEMORY_DESCRIPTOR));
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
//
|
||||
// Free the descriptor from the heap.
|
||||
// TODO: Use BlMmFreeHeap()
|
||||
//
|
||||
ConsolePrint(L"Cannot free memory descriptor: BlMmFreeHeap() is not implemented\r\n");
|
||||
return STATUS_NOT_IMPLEMENTED;
|
||||
// return BlMmFreeHeap(Descriptor);
|
||||
}
|
||||
|
||||
VOID
|
||||
MmMdFreeList (
|
||||
IN PMEMORY_DESCRIPTOR_LIST Mdl
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Frees a memory descriptor list (MDL).
|
||||
|
||||
Arguments:
|
||||
|
||||
Mdl - the MDL to free.
|
||||
|
||||
Return Value:
|
||||
|
||||
None.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
PLIST_ENTRY Entry;
|
||||
|
||||
Entry = Mdl->Head->Flink;
|
||||
while (Entry != Mdl->Head) {
|
||||
MmMdRemoveDescriptorFromList(Mdl, (PMEMORY_DESCRIPTOR)Entry);
|
||||
MmMdFreeDescriptor((PMEMORY_DESCRIPTOR)Entry);
|
||||
Entry = Entry->Flink;
|
||||
}
|
||||
}
|
||||
|
||||
VOID
|
||||
MmMdInitialize (
|
||||
IN ULONG Unused,
|
||||
IN PBOOT_LIBRARY_PARAMETERS LibraryParameters
|
||||
)
|
||||
|
||||
/*++
|
||||
|
||||
Routine Description:
|
||||
|
||||
Initializes the memory descriptor manager.
|
||||
|
||||
Arguments:
|
||||
|
||||
Unused - Ignored.
|
||||
|
||||
LibraryParameters - pointer to the library parameters structure.
|
||||
|
||||
Return Value:
|
||||
|
||||
None.
|
||||
|
||||
--*/
|
||||
|
||||
{
|
||||
(VOID)Unused;
|
||||
(VOID)LibraryParameters;
|
||||
|
||||
DebugPrint(L"Initializing memory descriptor manager...\r\n");
|
||||
|
||||
//
|
||||
// Initialize global memory descriptor list.
|
||||
//
|
||||
MmGlobalMemoryDescriptors = &MmStaticMemoryDescriptors[0];
|
||||
MmGlobalMemoryDescriptorCount = MAX_STATIC_DESCRIPTOR_COUNT;
|
||||
RtlZeroMemory(MmGlobalMemoryDescriptors, MAX_STATIC_DESCRIPTOR_COUNT * sizeof(MEMORY_DESCRIPTOR));
|
||||
DebugPrintf(L"Global memory descriptor count: %x\r\n", MmGlobalMemoryDescriptorCount);
|
||||
}
|
@@ -20,20 +20,20 @@ ULONG PapMinimumAllocationCount;
|
||||
ULONGLONG PapMinimumPhysicalPage;
|
||||
ULONGLONG PapMaximumPhysicalPage;
|
||||
|
||||
BOOT_MEMORY_DESCRIPTOR_LIST MmMdlFwAllocationTracker;
|
||||
BOOT_MEMORY_DESCRIPTOR_LIST MmMdlBadMemory;
|
||||
BOOT_MEMORY_DESCRIPTOR_LIST MmMdlTruncatedMemory;
|
||||
BOOT_MEMORY_DESCRIPTOR_LIST MmMdlPersistentMemory;
|
||||
BOOT_MEMORY_DESCRIPTOR_LIST MmMdlReservedAllocated;
|
||||
BOOT_MEMORY_DESCRIPTOR_LIST MmMdlMappedAllocated;
|
||||
BOOT_MEMORY_DESCRIPTOR_LIST MmMdlMappedUnallocated;
|
||||
BOOT_MEMORY_DESCRIPTOR_LIST MmMdlUnmappedAllocated;
|
||||
BOOT_MEMORY_DESCRIPTOR_LIST MmMdlUnmappedUnallocated;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlFwAllocationTracker;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlBadMemory;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlTruncatedMemory;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlPersistentMemory;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlReservedAllocated;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlMappedAllocated;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlMappedUnallocated;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlUnmappedAllocated;
|
||||
MEMORY_DESCRIPTOR_LIST MmMdlUnmappedUnallocated;
|
||||
|
||||
FORCEINLINE
|
||||
VOID
|
||||
InitializeList (
|
||||
IN PBOOT_MEMORY_DESCRIPTOR_LIST List
|
||||
IN PMEMORY_DESCRIPTOR_LIST List
|
||||
)
|
||||
|
||||
/*++
|
||||
@@ -83,6 +83,8 @@ Return Value:
|
||||
--*/
|
||||
|
||||
{
|
||||
NTSTATUS Status;
|
||||
|
||||
(VOID)MemoryInfo;
|
||||
|
||||
DebugPrint(L"Initializing page allocator...\r\n");
|
||||
@@ -111,10 +113,10 @@ Return Value:
|
||||
//
|
||||
// Get the firmware memory map.
|
||||
//
|
||||
// Status = MmFwGetMemoryMap(&MmMdlUnmappedUnallocated, 0x03);
|
||||
// if (!NT_SUCCESS(Status)) {
|
||||
// return Status;
|
||||
// }
|
||||
Status = MmFwGetMemoryMap(&MmMdlUnmappedUnallocated, 0x03);
|
||||
if (!NT_SUCCESS(Status)) {
|
||||
return Status;
|
||||
}
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
Reference in New Issue
Block a user