2010-06-04 10:17:55 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS HAL
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: hal/halx86/generic/memory.c
|
|
|
|
* PURPOSE: HAL memory management
|
|
|
|
* PROGRAMMERS: ReactOS Portable Systems Group
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
|
|
|
#include <hal.h>
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/* Share with Mm headers? */
|
2011-09-15 10:46:02 +00:00
|
|
|
#define MM_HAL_HEAP_START (PVOID)(MM_HAL_VA_START + (1024 * 1024))
|
2010-06-04 10:17:55 +00:00
|
|
|
|
|
|
|
/* GLOBALS *******************************************************************/
|
|
|
|
|
|
|
|
ULONG HalpUsedAllocDescriptors;
|
|
|
|
MEMORY_ALLOCATION_DESCRIPTOR HalpAllocationDescriptorArray[64];
|
|
|
|
PVOID HalpHeapStart = MM_HAL_HEAP_START;
|
|
|
|
|
|
|
|
|
|
|
|
/* PRIVATE FUNCTIONS *********************************************************/
|
|
|
|
|
2011-09-15 10:46:02 +00:00
|
|
|
ULONG_PTR
|
2010-06-04 10:17:55 +00:00
|
|
|
NTAPI
|
|
|
|
HalpAllocPhysicalMemory(IN PLOADER_PARAMETER_BLOCK LoaderBlock,
|
2011-09-15 10:46:02 +00:00
|
|
|
IN ULONG_PTR MaxAddress,
|
|
|
|
IN PFN_NUMBER PageCount,
|
2010-06-04 10:17:55 +00:00
|
|
|
IN BOOLEAN Aligned)
|
|
|
|
{
|
2012-03-28 12:15:54 +00:00
|
|
|
ULONG UsedDescriptors;
|
2011-09-15 10:46:02 +00:00
|
|
|
ULONG_PTR PhysicalAddress;
|
2012-03-28 12:15:54 +00:00
|
|
|
PFN_NUMBER MaxPage, BasePage, Alignment;
|
2010-06-04 10:17:55 +00:00
|
|
|
PLIST_ENTRY NextEntry;
|
|
|
|
PMEMORY_ALLOCATION_DESCRIPTOR MdBlock, NewBlock, FreeBlock;
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* Highest page we'll go */
|
|
|
|
MaxPage = MaxAddress >> PAGE_SHIFT;
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* We need at least two blocks */
|
|
|
|
if ((HalpUsedAllocDescriptors + 2) > 64) return 0;
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* Remember how many we have now */
|
|
|
|
UsedDescriptors = HalpUsedAllocDescriptors;
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* Loop the loader block memory descriptors */
|
|
|
|
NextEntry = LoaderBlock->MemoryDescriptorListHead.Flink;
|
|
|
|
while (NextEntry != &LoaderBlock->MemoryDescriptorListHead)
|
|
|
|
{
|
|
|
|
/* Get the block */
|
|
|
|
MdBlock = CONTAINING_RECORD(NextEntry,
|
|
|
|
MEMORY_ALLOCATION_DESCRIPTOR,
|
|
|
|
ListEntry);
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* No alignment by default */
|
|
|
|
Alignment = 0;
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* Unless requested, in which case we use a 64KB block alignment */
|
|
|
|
if (Aligned) Alignment = ((MdBlock->BasePage + 0x0F) & ~0x0F) - MdBlock->BasePage;
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* Search for free memory */
|
|
|
|
if ((MdBlock->MemoryType == LoaderFree) ||
|
|
|
|
(MdBlock->MemoryType == LoaderFirmwareTemporary))
|
|
|
|
{
|
|
|
|
/* Make sure the page is within bounds, including alignment */
|
|
|
|
BasePage = MdBlock->BasePage;
|
|
|
|
if ((BasePage) &&
|
|
|
|
(MdBlock->PageCount >= PageCount + Alignment) &&
|
|
|
|
(BasePage + PageCount + Alignment < MaxPage))
|
|
|
|
{
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* We found an address */
|
|
|
|
PhysicalAddress = (BasePage + Alignment) << PAGE_SHIFT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* Keep trying */
|
|
|
|
NextEntry = NextEntry->Flink;
|
|
|
|
}
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* If we didn't find anything, get out of here */
|
|
|
|
if (NextEntry == &LoaderBlock->MemoryDescriptorListHead) return 0;
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* Okay, now get a descriptor */
|
|
|
|
NewBlock = &HalpAllocationDescriptorArray[HalpUsedAllocDescriptors];
|
2011-09-15 10:46:02 +00:00
|
|
|
NewBlock->PageCount = (ULONG)PageCount;
|
2010-06-04 10:17:55 +00:00
|
|
|
NewBlock->BasePage = MdBlock->BasePage + Alignment;
|
|
|
|
NewBlock->MemoryType = LoaderHALCachedMemory;
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* Update count */
|
|
|
|
UsedDescriptors++;
|
|
|
|
HalpUsedAllocDescriptors = UsedDescriptors;
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* Check if we had any alignment */
|
|
|
|
if (Alignment)
|
|
|
|
{
|
|
|
|
/* Check if we had leftovers */
|
2011-09-26 15:01:11 +00:00
|
|
|
if (MdBlock->PageCount > (PageCount + Alignment))
|
2010-06-04 10:17:55 +00:00
|
|
|
{
|
|
|
|
/* Get the next descriptor */
|
|
|
|
FreeBlock = &HalpAllocationDescriptorArray[UsedDescriptors];
|
2011-09-15 10:46:02 +00:00
|
|
|
FreeBlock->PageCount = MdBlock->PageCount - Alignment - (ULONG)PageCount;
|
|
|
|
FreeBlock->BasePage = MdBlock->BasePage + Alignment + (ULONG)PageCount;
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* One more */
|
|
|
|
HalpUsedAllocDescriptors++;
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2010-06-04 10:17:55 +00:00
|
|
|
/* Insert it into the list */
|
|
|
|
InsertHeadList(&MdBlock->ListEntry, &FreeBlock->ListEntry);
|
|
|
|
}
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2011-09-26 15:01:11 +00:00
|
|
|
/* Trim the original block to the alignment only */
|
|
|
|
MdBlock->PageCount = Alignment;
|
|
|
|
|
|
|
|
/* Insert the descriptor after the original one */
|
2010-06-04 10:17:55 +00:00
|
|
|
InsertHeadList(&MdBlock->ListEntry, &NewBlock->ListEntry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Consume memory from this block */
|
2011-09-15 10:46:02 +00:00
|
|
|
MdBlock->BasePage += (ULONG)PageCount;
|
|
|
|
MdBlock->PageCount -= (ULONG)PageCount;
|
2012-03-28 12:15:54 +00:00
|
|
|
|
2011-09-26 15:01:11 +00:00
|
|
|
/* Insert the descriptor before the original one */
|
2010-06-04 10:17:55 +00:00
|
|
|
InsertTailList(&MdBlock->ListEntry, &NewBlock->ListEntry);
|
|
|
|
|
|
|
|
/* Remove the entry if the whole block was allocated */
|
2011-09-26 15:01:11 +00:00
|
|
|
if (MdBlock->PageCount == 0) RemoveEntryList(&MdBlock->ListEntry);
|
2010-06-04 10:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the address */
|
|
|
|
return PhysicalAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
PVOID
|
|
|
|
NTAPI
|
|
|
|
HalpMapPhysicalMemory64(IN PHYSICAL_ADDRESS PhysicalAddress,
|
2012-03-28 12:15:54 +00:00
|
|
|
IN PFN_COUNT PageCount)
|
2010-06-04 10:17:55 +00:00
|
|
|
{
|
|
|
|
PHARDWARE_PTE PointerPte;
|
2011-09-15 10:46:02 +00:00
|
|
|
PFN_NUMBER UsedPages = 0;
|
2010-06-04 10:17:55 +00:00
|
|
|
PVOID VirtualAddress, BaseAddress;
|
|
|
|
|
|
|
|
/* Start at the current HAL heap base */
|
|
|
|
BaseAddress = HalpHeapStart;
|
|
|
|
VirtualAddress = BaseAddress;
|
|
|
|
|
|
|
|
/* Loop until we have all the pages required */
|
|
|
|
while (UsedPages < PageCount)
|
|
|
|
{
|
|
|
|
/* If this overflows past the HAL heap, it means there's no space */
|
|
|
|
if (VirtualAddress == NULL) return NULL;
|
|
|
|
|
|
|
|
/* Get the PTE for this address */
|
|
|
|
PointerPte = HalAddressToPte(VirtualAddress);
|
|
|
|
|
|
|
|
/* Go to the next page */
|
|
|
|
VirtualAddress = (PVOID)((ULONG_PTR)VirtualAddress + PAGE_SIZE);
|
|
|
|
|
|
|
|
/* Check if the page is available */
|
|
|
|
if (PointerPte->Valid)
|
|
|
|
{
|
|
|
|
/* PTE has data, skip it and start with a new base address */
|
|
|
|
BaseAddress = VirtualAddress;
|
|
|
|
UsedPages = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PTE is available, keep going on this run */
|
|
|
|
UsedPages++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Take the base address of the page plus the actual offset in the address */
|
|
|
|
VirtualAddress = (PVOID)((ULONG_PTR)BaseAddress +
|
|
|
|
BYTE_OFFSET(PhysicalAddress.LowPart));
|
|
|
|
|
|
|
|
/* If we are starting at the heap, move the heap */
|
|
|
|
if (BaseAddress == HalpHeapStart)
|
|
|
|
{
|
|
|
|
/* Past this allocation */
|
|
|
|
HalpHeapStart = (PVOID)((ULONG_PTR)BaseAddress + (PageCount * PAGE_SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop pages that can be mapped */
|
|
|
|
while (UsedPages--)
|
|
|
|
{
|
|
|
|
/* Fill out the PTE */
|
|
|
|
PointerPte = HalAddressToPte(BaseAddress);
|
2011-11-21 12:30:01 +00:00
|
|
|
PointerPte->PageFrameNumber = (PFN_NUMBER)(PhysicalAddress.QuadPart >> PAGE_SHIFT);
|
2010-06-04 10:17:55 +00:00
|
|
|
PointerPte->Valid = 1;
|
|
|
|
PointerPte->Write = 1;
|
|
|
|
|
|
|
|
/* Move to the next address */
|
|
|
|
PhysicalAddress.QuadPart += PAGE_SIZE;
|
|
|
|
BaseAddress = (PVOID)((ULONG_PTR)BaseAddress + PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush the TLB and return the address */
|
|
|
|
HalpFlushTLB();
|
|
|
|
return VirtualAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
HalpUnmapVirtualAddress(IN PVOID VirtualAddress,
|
2012-03-28 12:15:54 +00:00
|
|
|
IN PFN_COUNT PageCount)
|
2010-06-04 10:17:55 +00:00
|
|
|
{
|
|
|
|
PHARDWARE_PTE PointerPte;
|
|
|
|
ULONG i;
|
|
|
|
|
|
|
|
/* Only accept valid addresses */
|
2011-09-15 10:46:02 +00:00
|
|
|
if (VirtualAddress < (PVOID)MM_HAL_VA_START) return;
|
2010-06-04 10:17:55 +00:00
|
|
|
|
|
|
|
/* Align it down to page size */
|
|
|
|
VirtualAddress = (PVOID)((ULONG_PTR)VirtualAddress & ~(PAGE_SIZE - 1));
|
|
|
|
|
|
|
|
/* Loop PTEs */
|
|
|
|
PointerPte = HalAddressToPte(VirtualAddress);
|
|
|
|
for (i = 0; i < PageCount; i++)
|
|
|
|
{
|
|
|
|
*(PULONG)PointerPte = 0;
|
|
|
|
PointerPte++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush the TLB */
|
|
|
|
HalpFlushTLB();
|
|
|
|
|
|
|
|
/* Put the heap back */
|
|
|
|
if (HalpHeapStart > VirtualAddress) HalpHeapStart = VirtualAddress;
|
|
|
|
}
|
|
|
|
|