2008-03-09 14:11:42 +00:00
|
|
|
/*
|
2005-01-26 13:58:37 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
|
|
|
* FILE: ntoskrnl/mm/region.c
|
|
|
|
* PURPOSE: No purpose listed.
|
2005-05-09 01:38:29 +00:00
|
|
|
*
|
2005-01-26 13:58:37 +00:00
|
|
|
* PROGRAMMERS: David Welch
|
2002-08-10 16:41:20 +00:00
|
|
|
*/
|
2004-04-10 22:36:07 +00:00
|
|
|
|
2002-08-10 16:41:20 +00:00
|
|
|
/* INCLUDE *****************************************************************/
|
|
|
|
|
2004-08-15 16:39:12 +00:00
|
|
|
#include <ntoskrnl.h>
|
2002-08-10 16:41:20 +00:00
|
|
|
#define NDEBUG
|
2008-08-30 16:31:06 +00:00
|
|
|
#include <debug.h>
|
2002-08-10 16:41:20 +00:00
|
|
|
|
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
2008-12-03 17:28:59 +00:00
|
|
|
static VOID
|
2002-08-10 16:41:20 +00:00
|
|
|
InsertAfterEntry(PLIST_ENTRY Previous,
|
2004-04-10 22:36:07 +00:00
|
|
|
PLIST_ENTRY Entry)
|
2002-08-10 16:41:20 +00:00
|
|
|
/*
|
|
|
|
* FUNCTION: Insert a list entry after another entry in the list
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
Previous->Flink->Blink = Entry;
|
2004-04-10 22:36:07 +00:00
|
|
|
|
2002-08-10 16:41:20 +00:00
|
|
|
Entry->Flink = Previous->Flink;
|
|
|
|
Entry->Blink = Previous;
|
2004-04-10 22:36:07 +00:00
|
|
|
|
2002-08-10 16:41:20 +00:00
|
|
|
Previous->Flink = Entry;
|
|
|
|
}
|
|
|
|
|
2008-12-03 17:28:59 +00:00
|
|
|
static PMM_REGION
|
2002-08-10 16:41:20 +00:00
|
|
|
MmSplitRegion(PMM_REGION InitialRegion, PVOID InitialBaseAddress,
|
2004-04-10 22:36:07 +00:00
|
|
|
PVOID StartAddress, ULONG Length, ULONG NewType,
|
2009-04-27 10:12:57 +00:00
|
|
|
ULONG NewProtect, PMMSUPPORT AddressSpace,
|
2004-04-10 22:36:07 +00:00
|
|
|
PMM_ALTER_REGION_FUNC AlterFunc)
|
2002-08-10 16:41:20 +00:00
|
|
|
{
|
2004-04-10 22:36:07 +00:00
|
|
|
PMM_REGION NewRegion1;
|
|
|
|
PMM_REGION NewRegion2;
|
|
|
|
ULONG InternalLength;
|
|
|
|
|
|
|
|
/* Allocate this in front otherwise the failure case is too difficult. */
|
|
|
|
NewRegion2 = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_REGION),
|
|
|
|
TAG_MM_REGION);
|
|
|
|
if (NewRegion2 == NULL)
|
|
|
|
{
|
2002-08-10 16:41:20 +00:00
|
|
|
return(NULL);
|
2004-04-10 22:36:07 +00:00
|
|
|
}
|
2002-08-10 16:41:20 +00:00
|
|
|
|
2004-04-10 22:36:07 +00:00
|
|
|
/* Create the new region. */
|
|
|
|
NewRegion1 = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_REGION),
|
|
|
|
TAG_MM_REGION);
|
|
|
|
if (NewRegion1 == NULL)
|
|
|
|
{
|
2002-08-10 16:41:20 +00:00
|
|
|
ExFreePool(NewRegion2);
|
|
|
|
return(NULL);
|
2004-04-10 22:36:07 +00:00
|
|
|
}
|
|
|
|
NewRegion1->Type = NewType;
|
|
|
|
NewRegion1->Protect = NewProtect;
|
|
|
|
InternalLength = ((char*)InitialBaseAddress + InitialRegion->Length) - (char*)StartAddress;
|
|
|
|
InternalLength = min(InternalLength, Length);
|
|
|
|
NewRegion1->Length = InternalLength;
|
|
|
|
InsertAfterEntry(&InitialRegion->RegionListEntry,
|
|
|
|
&NewRegion1->RegionListEntry);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call our helper function to do the changes on the addresses contained
|
|
|
|
* in the initial region.
|
|
|
|
*/
|
|
|
|
AlterFunc(AddressSpace, StartAddress, InternalLength, InitialRegion->Type,
|
|
|
|
InitialRegion->Protect, NewType, NewProtect);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If necessary create a new region for the portion of the initial region
|
|
|
|
* beyond the range of addresses to alter.
|
|
|
|
*/
|
|
|
|
if (((char*)InitialBaseAddress + InitialRegion->Length) > ((char*)StartAddress + Length))
|
|
|
|
{
|
2002-08-10 16:41:20 +00:00
|
|
|
NewRegion2->Type = InitialRegion->Type;
|
|
|
|
NewRegion2->Protect = InitialRegion->Protect;
|
2004-04-10 22:36:07 +00:00
|
|
|
NewRegion2->Length = ((char*)InitialBaseAddress + InitialRegion->Length) -
|
|
|
|
((char*)StartAddress + Length);
|
|
|
|
InsertAfterEntry(&NewRegion1->RegionListEntry,
|
|
|
|
&NewRegion2->RegionListEntry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-08-10 16:41:20 +00:00
|
|
|
ExFreePool(NewRegion2);
|
2004-04-10 22:36:07 +00:00
|
|
|
}
|
2002-08-10 16:41:20 +00:00
|
|
|
|
2004-04-10 22:36:07 +00:00
|
|
|
/* Either remove or shrink the initial region. */
|
|
|
|
if (InitialBaseAddress == StartAddress)
|
|
|
|
{
|
2002-08-10 16:41:20 +00:00
|
|
|
RemoveEntryList(&InitialRegion->RegionListEntry);
|
|
|
|
ExFreePool(InitialRegion);
|
2004-04-10 22:36:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-12-30 18:52:06 +00:00
|
|
|
InitialRegion->Length = (char*)StartAddress - (char*)InitialBaseAddress;
|
2004-04-10 22:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(NewRegion1);
|
2002-08-10 16:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
2005-09-14 01:05:50 +00:00
|
|
|
NTAPI
|
2009-04-27 10:12:57 +00:00
|
|
|
MmAlterRegion(PMMSUPPORT AddressSpace, PVOID BaseAddress,
|
2004-04-10 22:36:07 +00:00
|
|
|
PLIST_ENTRY RegionListHead, PVOID StartAddress, ULONG Length,
|
|
|
|
ULONG NewType, ULONG NewProtect, PMM_ALTER_REGION_FUNC AlterFunc)
|
2002-08-10 16:41:20 +00:00
|
|
|
{
|
2004-04-10 22:36:07 +00:00
|
|
|
PMM_REGION InitialRegion;
|
2005-07-31 23:39:05 +00:00
|
|
|
PVOID InitialBaseAddress = NULL;
|
2004-04-10 22:36:07 +00:00
|
|
|
PMM_REGION NewRegion;
|
|
|
|
PLIST_ENTRY CurrentEntry;
|
|
|
|
PMM_REGION CurrentRegion = NULL;
|
|
|
|
PVOID CurrentBaseAddress;
|
|
|
|
ULONG RemainingLength;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the first region containing part of the range of addresses to
|
|
|
|
* be altered.
|
|
|
|
*/
|
|
|
|
InitialRegion = MmFindRegion(BaseAddress, RegionListHead, StartAddress,
|
|
|
|
&InitialBaseAddress);
|
|
|
|
if (((char*)StartAddress + Length) >
|
|
|
|
((char*)InitialBaseAddress + InitialRegion->Length))
|
|
|
|
{
|
|
|
|
RemainingLength = ((char*)StartAddress + Length) -
|
|
|
|
((char*)InitialBaseAddress + InitialRegion->Length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-08-10 16:41:20 +00:00
|
|
|
RemainingLength = 0;
|
2004-04-10 22:36:07 +00:00
|
|
|
}
|
2004-06-13 14:05:53 +00:00
|
|
|
|
2004-04-10 22:36:07 +00:00
|
|
|
/*
|
|
|
|
* If necessary then split the region into the affected and unaffected parts.
|
|
|
|
*/
|
|
|
|
if (InitialRegion->Type != NewType || InitialRegion->Protect != NewProtect)
|
|
|
|
{
|
|
|
|
NewRegion = MmSplitRegion(InitialRegion, InitialBaseAddress,
|
|
|
|
StartAddress, Length, NewType, NewProtect,
|
|
|
|
AddressSpace, AlterFunc);
|
2002-08-10 16:41:20 +00:00
|
|
|
if (NewRegion == NULL)
|
2004-04-10 22:36:07 +00:00
|
|
|
{
|
|
|
|
return(STATUS_NO_MEMORY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-08-10 16:41:20 +00:00
|
|
|
NewRegion = InitialRegion;
|
2004-04-10 22:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free any complete regions that are containing in the range of addresses
|
|
|
|
* and call the helper function to actually do the changes.
|
|
|
|
*/
|
|
|
|
CurrentEntry = NewRegion->RegionListEntry.Flink;
|
|
|
|
CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
|
|
|
|
RegionListEntry);
|
|
|
|
CurrentBaseAddress = (char*)StartAddress + NewRegion->Length;
|
2004-06-13 14:05:53 +00:00
|
|
|
while (RemainingLength > 0 && CurrentRegion->Length <= RemainingLength &&
|
|
|
|
CurrentEntry != RegionListHead)
|
2004-04-10 22:36:07 +00:00
|
|
|
{
|
2002-08-10 16:41:20 +00:00
|
|
|
if (CurrentRegion->Type != NewType &&
|
2004-04-10 22:36:07 +00:00
|
|
|
CurrentRegion->Protect != NewProtect)
|
|
|
|
{
|
|
|
|
AlterFunc(AddressSpace, CurrentBaseAddress, CurrentRegion->Length,
|
|
|
|
CurrentRegion->Type, CurrentRegion->Protect,
|
|
|
|
NewType, NewProtect);
|
|
|
|
}
|
|
|
|
|
2005-07-05 00:57:47 +00:00
|
|
|
CurrentBaseAddress = (PVOID)((ULONG_PTR)CurrentBaseAddress + CurrentRegion->Length);
|
2002-08-10 16:41:20 +00:00
|
|
|
NewRegion->Length += CurrentRegion->Length;
|
|
|
|
RemainingLength -= CurrentRegion->Length;
|
2004-04-10 22:36:07 +00:00
|
|
|
CurrentEntry = CurrentEntry->Flink;
|
2002-08-10 16:41:20 +00:00
|
|
|
RemoveEntryList(&CurrentRegion->RegionListEntry);
|
|
|
|
ExFreePool(CurrentRegion);
|
2004-04-10 22:36:07 +00:00
|
|
|
CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
|
|
|
|
RegionListEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Split any final region.
|
|
|
|
*/
|
2004-06-13 14:05:53 +00:00
|
|
|
if (RemainingLength > 0 && CurrentEntry != RegionListHead)
|
2004-04-10 22:36:07 +00:00
|
|
|
{
|
|
|
|
CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
|
|
|
|
RegionListEntry);
|
2002-08-10 16:41:20 +00:00
|
|
|
if (CurrentRegion->Type != NewType &&
|
2004-04-10 22:36:07 +00:00
|
|
|
CurrentRegion->Protect != NewProtect)
|
|
|
|
{
|
|
|
|
AlterFunc(AddressSpace, CurrentBaseAddress, CurrentRegion->Length,
|
|
|
|
CurrentRegion->Type, CurrentRegion->Protect,
|
|
|
|
NewType, NewProtect);
|
|
|
|
}
|
2002-08-10 16:41:20 +00:00
|
|
|
NewRegion->Length += RemainingLength;
|
|
|
|
CurrentRegion->Length -= RemainingLength;
|
2004-04-10 22:36:07 +00:00
|
|
|
}
|
2002-08-10 16:41:20 +00:00
|
|
|
|
2004-04-10 22:36:07 +00:00
|
|
|
/*
|
|
|
|
* If the region after the new region has the same type then merge them.
|
|
|
|
*/
|
|
|
|
if (NewRegion->RegionListEntry.Flink != RegionListHead)
|
|
|
|
{
|
2002-08-10 16:41:20 +00:00
|
|
|
CurrentEntry = NewRegion->RegionListEntry.Flink;
|
2004-04-10 22:36:07 +00:00
|
|
|
CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
|
|
|
|
RegionListEntry);
|
2002-08-10 16:41:20 +00:00
|
|
|
if (CurrentRegion->Type == NewRegion->Type &&
|
2004-04-10 22:36:07 +00:00
|
|
|
CurrentRegion->Protect == NewRegion->Protect)
|
|
|
|
{
|
|
|
|
NewRegion->Length += CurrentRegion->Length;
|
|
|
|
RemoveEntryList(&CurrentRegion->RegionListEntry);
|
|
|
|
ExFreePool(CurrentRegion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the region before the new region has the same type then merge them.
|
|
|
|
*/
|
|
|
|
if (NewRegion->RegionListEntry.Blink != RegionListHead)
|
|
|
|
{
|
2002-08-10 16:41:20 +00:00
|
|
|
CurrentEntry = NewRegion->RegionListEntry.Blink;
|
2004-04-10 22:36:07 +00:00
|
|
|
CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION,
|
|
|
|
RegionListEntry);
|
2002-08-10 16:41:20 +00:00
|
|
|
if (CurrentRegion->Type == NewRegion->Type &&
|
2004-04-10 22:36:07 +00:00
|
|
|
CurrentRegion->Protect == NewRegion->Protect)
|
|
|
|
{
|
|
|
|
NewRegion->Length += CurrentRegion->Length;
|
|
|
|
RemoveEntryList(&CurrentRegion->RegionListEntry);
|
|
|
|
ExFreePool(CurrentRegion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(STATUS_SUCCESS);
|
2002-08-10 16:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
2005-09-14 01:05:50 +00:00
|
|
|
NTAPI
|
2009-12-06 03:24:18 +00:00
|
|
|
MmInitializeRegion(PLIST_ENTRY RegionListHead, SIZE_T Length, ULONG Type,
|
2004-04-10 22:36:07 +00:00
|
|
|
ULONG Protect)
|
2002-08-10 16:41:20 +00:00
|
|
|
{
|
2004-04-10 22:36:07 +00:00
|
|
|
PMM_REGION Region;
|
|
|
|
|
|
|
|
Region = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_REGION),
|
|
|
|
TAG_MM_REGION);
|
2009-09-02 13:02:30 +00:00
|
|
|
if (!Region) return;
|
|
|
|
|
2004-04-10 22:36:07 +00:00
|
|
|
Region->Type = Type;
|
|
|
|
Region->Protect = Protect;
|
|
|
|
Region->Length = Length;
|
|
|
|
InitializeListHead(RegionListHead);
|
|
|
|
InsertHeadList(RegionListHead, &Region->RegionListEntry);
|
2002-08-10 16:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PMM_REGION
|
2005-09-14 01:05:50 +00:00
|
|
|
NTAPI
|
2002-08-10 16:41:20 +00:00
|
|
|
MmFindRegion(PVOID BaseAddress, PLIST_ENTRY RegionListHead, PVOID Address,
|
2004-04-10 22:36:07 +00:00
|
|
|
PVOID* RegionBaseAddress)
|
2002-08-10 16:41:20 +00:00
|
|
|
{
|
2004-04-10 22:36:07 +00:00
|
|
|
PLIST_ENTRY current_entry;
|
|
|
|
PMM_REGION current;
|
|
|
|
PVOID StartAddress = BaseAddress;
|
|
|
|
|
|
|
|
current_entry = RegionListHead->Flink;
|
|
|
|
while (current_entry != RegionListHead)
|
|
|
|
{
|
2002-08-10 16:41:20 +00:00
|
|
|
current = CONTAINING_RECORD(current_entry, MM_REGION, RegionListEntry);
|
|
|
|
|
2004-04-10 22:36:07 +00:00
|
|
|
if (StartAddress <= Address &&
|
|
|
|
((char*)StartAddress + current->Length) > (char*)Address)
|
|
|
|
{
|
|
|
|
if (RegionBaseAddress != NULL)
|
|
|
|
{
|
|
|
|
*RegionBaseAddress = StartAddress;
|
|
|
|
}
|
|
|
|
return(current);
|
|
|
|
}
|
2002-08-10 16:41:20 +00:00
|
|
|
|
|
|
|
current_entry = current_entry->Flink;
|
2004-04-10 22:36:07 +00:00
|
|
|
|
2005-07-05 00:57:47 +00:00
|
|
|
StartAddress = (PVOID)((ULONG_PTR)StartAddress + current->Length);
|
2004-04-10 22:36:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return(NULL);
|
2002-08-10 16:41:20 +00:00
|
|
|
}
|