[NTOS:MM] Add MmIsAddressRangeFree helper function

Use this instead of MmLocateMemoryAreaByRegion to determine whether we can inert a section at the requested base address. This will be required to get rid of fake memory areas allocated for each ARM³ VAD.
This commit is contained in:
Timo Kreuzer 2024-04-06 14:03:48 +03:00
parent 48027a8058
commit d27f5971c5
3 changed files with 31 additions and 5 deletions

View file

@ -618,6 +618,13 @@ MmLocateMemoryAreaByRegion(
SIZE_T Length
);
BOOLEAN
NTAPI
MmIsAddressRangeFree(
_In_ PMMSUPPORT AddressSpace,
_In_ PVOID Address,
_In_ ULONG_PTR Length);
PVOID
NTAPI
MmFindGap(

View file

@ -115,6 +115,28 @@ MmLocateMemoryAreaByRegion(
return MemoryArea;
}
BOOLEAN
NTAPI
MmIsAddressRangeFree(
_In_ PMMSUPPORT AddressSpace,
_In_ PVOID Address,
_In_ ULONG_PTR Length)
{
ULONG_PTR StartVpn = (ULONG_PTR)Address / PAGE_SIZE;
ULONG_PTR EndVpn = ((ULONG_PTR)Address + Length - 1) / PAGE_SIZE;
PEPROCESS Process;
PMM_AVL_TABLE Table;
PMMADDRESS_NODE Node;
TABLE_SEARCH_RESULT Result;
Process = MmGetAddressSpaceOwner(AddressSpace);
Table = (Process != NULL) ? &Process->VadRoot : &MiRosKernelVadRoot;
Result = MiCheckForConflictingNode(StartVpn, EndVpn, Table, &Node);
return (Result != TableFoundNode);
}
VOID
NTAPI
MiInsertVad(IN PMMVAD Vad,
@ -460,9 +482,7 @@ MmCreateMemoryArea(PMMSUPPORT AddressSpace,
/* No need to check ARM3 owned memory areas, the range MUST be free */
if (MemoryArea->Type != MEMORY_AREA_OWNED_BY_ARM3)
{
if (MmLocateMemoryAreaByRegion(AddressSpace,
*BaseAddress,
tmpLength) != NULL)
if (!MmIsAddressRangeFree(AddressSpace, *BaseAddress, tmpLength))
{
DPRINT("Memory area already occupied\n");
if (!(Type & MEMORY_AREA_STATIC)) ExFreePoolWithTag(MemoryArea, TAG_MAREA);

View file

@ -4104,8 +4104,7 @@ MmMapViewOfSection(IN PVOID SectionObject,
}
/* Check there is enough space to map the section at that point. */
if (MmLocateMemoryAreaByRegion(AddressSpace, (PVOID)ImageBase,
PAGE_ROUND_UP(ImageSize)) != NULL)
if (!MmIsAddressRangeFree(AddressSpace, (PVOID)ImageBase, PAGE_ROUND_UP(ImageSize)))
{
/* Fail if the user requested a fixed base address. */
if ((*BaseAddress) != NULL)