[BOOT] Execution contexts and more refactoring

This commit is contained in:
2024-10-05 15:44:25 -04:00
parent 7c3dafc051
commit 24a31cab26
13 changed files with 317 additions and 118 deletions

View File

@@ -79,8 +79,6 @@ Return Value:
{
NTSTATUS Status;
DebugPrint(L"Initializing memory manager...\r\n");
//
// Check TranslationType.
//
@@ -105,5 +103,9 @@ Return Value:
return Status;
}
//
// TODO: Finish this routine.
//
return STATUS_SUCCESS;
}

View File

@@ -27,6 +27,8 @@ ULONG MmGlobalMemoryDescriptorCount, MmGlobalMemoryDescriptorsUsed;
PMEMORY_DESCRIPTOR MmDynamicMemoryDescriptors;
ULONG MmDynamicMemoryDescriptorCount, MmDynamicMemoryDescriptorsUsed;
#define MAX_PRECEDENCE_INDEX sizeof(MmPlatformMemoryTypePrecedence) / sizeof(MmPlatformMemoryTypePrecedence[0])
MEMORY_TYPE MmPlatformMemoryTypePrecedence[] = {
MEMORY_TYPE_RESERVED,
MEMORY_TYPE_UNUSABLE,
@@ -44,6 +46,40 @@ MEMORY_TYPE MmPlatformMemoryTypePrecedence[] = {
MEMORY_TYPE_FREE_ZEROED
};
ULONG
GetPrecedenceIndex (
IN MEMORY_TYPE Type
)
/*++
Routine Description:
Finds the index into MmPlatformMemoryTypePrecedence for Type.
Arguments:
Type - The memory type.
Return Value:
The precedence index if found.
MAX_PRECEDENCE_INDEX if not found.
--*/
{
for (ULONG Index = 0;
Index < MAX_PRECEDENCE_INDEX;
Index++) {
if (MmPlatformMemoryTypePrecedence[Index] == Type) {
return Index;
}
}
return MAX_PRECEDENCE_INDEX;
}
BOOLEAN
MmMdpHasPrecedence (
IN MEMORY_TYPE TypeA,
@@ -58,9 +94,9 @@ Routine Description:
Arguments:
TypeA - memory type A.
TypeA - Memory type A.
TypeB - memory type B.
TypeB - Memory type B.
Return Value:
@@ -71,7 +107,7 @@ Return Value:
{
ULONG ClassA, ClassB;
ULONG IndexA, IndexB;
ULONG PrecedenceIndexA, PrecedenceIndexB;
if (TypeB == MEMORY_TYPE_FREE_ZEROED) {
return TRUE;
@@ -103,26 +139,17 @@ Return Value:
return TRUE;
}
for (IndexA = 0;
IndexA < sizeof(MmPlatformMemoryTypePrecedence) / sizeof(MmPlatformMemoryTypePrecedence[0]);
IndexA++) {
if (TypeA == MmPlatformMemoryTypePrecedence[IndexA]) {
goto CheckTypeBPrecedence;
}
PrecedenceIndexA = GetPrecedenceIndex(TypeA);
if (PrecedenceIndexA == MAX_PRECEDENCE_INDEX) {
return TRUE;
}
return TRUE;
CheckTypeBPrecedence:
for (IndexB = 0;
IndexB < sizeof(MmPlatformMemoryTypePrecedence) / sizeof(MmPlatformMemoryTypePrecedence[0]);
IndexB++) {
if (TypeB == MmPlatformMemoryTypePrecedence[IndexB]) {
return IndexA <= IndexB ? TRUE:FALSE;
}
PrecedenceIndexB = GetPrecedenceIndex(TypeB);
if (PrecedenceIndexB == MAX_PRECEDENCE_INDEX) {
return FALSE;
}
return FALSE;
return PrecedenceIndexA <= PrecedenceIndexB ? TRUE:FALSE;
}
BOOLEAN
@@ -690,6 +717,41 @@ Return Value:
return Status;
}
NTSTATUS
MmMdRemoveRegionFromMdl (
IN PMEMORY_DESCRIPTOR_LIST Mdl,
IN ULONGLONG RemoveStart,
IN ULONGLONG PageCount,
IN ULONG Flags
)
/*++
Routine Description:
Removes a region from a MDL.
Wrapper around MmMdRemoveRegionFromMdlEx().
Arguments:
Same as MmMdRemoveRegionFromMdlEx(), except for Unused.
Return Value:
Any status code returned by MmMdRemoveRegionFromMdlEx().
--*/
{
return MmMdRemoveRegionFromMdlEx(
Mdl,
RemoveStart,
PageCount,
Flags,
NULL
);
}
NTSTATUS
MmMdFreeDescriptor (
IN PMEMORY_DESCRIPTOR Descriptor
@@ -732,10 +794,10 @@ Return Value:
}
//
// Free the descriptor from the heap.
// TODO: Use BlMmFreeHeap().
// TODO: Free the descriptor from the heap.
// Need BlMmFreeHeap().
//
ConsolePrint(L"MmMdFreeDescriptor(): Heap not available\r\n");
DebugPrint(L"MmMdFreeDescriptor(): Heap not available\r\n");
return STATUS_NOT_IMPLEMENTED;
// return BlMmFreeHeap(Descriptor);
}
@@ -825,7 +887,7 @@ Return Value:
VOID
MmMdInitialize (
IN ULONG Unused,
IN ULONG Stage,
IN PBOOT_LIBRARY_PARAMETERS LibraryParameters
)
@@ -837,9 +899,11 @@ Routine Description:
Arguments:
Unused - Ignored.
Stage - Which stage of initialization to perform.
LibraryParameters - pointer to the library parameters structure.
Stage 0: Initializes the static memory descriptor pool.
LibraryParameters - Pointer to the library parameters structure.
Return Value:
@@ -848,17 +912,19 @@ Return Value:
--*/
{
(VOID)Unused;
(VOID)LibraryParameters;
DebugPrint(L"Initializing memory descriptor manager...\r\n");
if (Stage == 0) {
//
// Initialize static memory descriptor pool.
//
MmGlobalMemoryDescriptors = &MmStaticMemoryDescriptors[0];
MmGlobalMemoryDescriptorCount = MAX_STATIC_DESCRIPTOR_COUNT;
MmGlobalMemoryDescriptorsUsed = 0;
RtlZeroMemory(MmGlobalMemoryDescriptors, MAX_STATIC_DESCRIPTOR_COUNT * sizeof(MEMORY_DESCRIPTOR));
}
//
// Initialize global memory descriptor list.
// TODO: Implement stage 1 initialization.
//
MmGlobalMemoryDescriptors = &MmStaticMemoryDescriptors[0];
MmGlobalMemoryDescriptorCount = MAX_STATIC_DESCRIPTOR_COUNT;
MmGlobalMemoryDescriptorsUsed = 0;
RtlZeroMemory(MmGlobalMemoryDescriptors, MAX_STATIC_DESCRIPTOR_COUNT * sizeof(MEMORY_DESCRIPTOR));
DebugPrintf(L"Global memory descriptor count: %x\r\n", MmGlobalMemoryDescriptorCount);
}

View File

@@ -88,14 +88,13 @@ Return Value:
NTSTATUS Status;
PMEMORY_DESCRIPTOR Descriptor, NewDescriptor;
(VOID)MemoryInfo;
DebugPrint(L"Initializing page allocator...\r\n");
PapMinimumAllocationCount = MinimumAllocation;
PapMinimumPhysicalPage = 1;
PapMaximumPhysicalPage = MAXULONGLONG >> PAGE_SHIFT;
//
// Initialize Memory Descriptor Lists
//
InitializeMdl(&MmMdlFwAllocationTracker);
InitializeMdl(&MmMdlBadMemory);
InitializeMdl(&MmMdlTruncatedMemory);
@@ -116,11 +115,18 @@ Return Value:
// MDL the memory manager will use for allocation.
//
Descriptor = (PMEMORY_DESCRIPTOR)((PUCHAR)MemoryInfo + MemoryInfo->MdlOffset);
for (ULONG DescriptorCount = MemoryInfo->DescriptorCount; DescriptorCount > 0; DescriptorCount--) {
for (ULONG DescriptorCount = MemoryInfo->DescriptorCount;
DescriptorCount > 0;
DescriptorCount--) {
//
// Remove from the usable MDL.
//
Status = MmMdRemoveRegionFromMdlEx(&MmMdlUnmappedUnallocated, Descriptor->FirstPage, Descriptor->PageCount, MDL_OPERATION_FLAGS_PHYSICAL, NULL);
Status = MmMdRemoveRegionFromMdl(
&MmMdlUnmappedUnallocated,
Descriptor->FirstPage,
Descriptor->PageCount,
MDL_OPERATION_FLAGS_PHYSICAL
);
if (!NT_SUCCESS(Status)) {
return STATUS_INVALID_PARAMETER;
}