2 * Copyright (C) 1998-2005 ReactOS Team (and the authors from the programmers section)
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * PROJECT: ReactOS kernel
20 * FILE: ntoskrnl/mm/marea.c
21 * PURPOSE: Implements memory areas
23 * PROGRAMMERS: Rex Jolliff
33 * Thomas Weidenmueller
34 * Gunnar Andre' Dalsnes
42 /* INCLUDES *****************************************************************/
46 #include "../cache/section/newmm.h"
49 #include "ARM3/miarm.h"
51 MEMORY_AREA MiStaticMemoryAreas
[MI_STATIC_MEMORY_AREAS
];
52 ULONG MiStaticMemoryAreaCount
;
54 /* FUNCTIONS *****************************************************************/
57 * @name MmIterateFirstNode
60 * Head node of the MEMORY_AREA tree.
62 * @return The leftmost MEMORY_AREA node (ie. the one with lowest
66 static PMEMORY_AREA
MmIterateFirstNode(PMEMORY_AREA Node
)
68 while (Node
->LeftChild
!= NULL
)
69 Node
= Node
->LeftChild
;
75 * @name MmIterateNextNode
78 * Current node in the tree.
80 * @return Next node in the tree (sorted by address).
83 static PMEMORY_AREA
MmIterateNextNode(PMEMORY_AREA Node
)
85 if (Node
->RightChild
!= NULL
)
87 Node
= Node
->RightChild
;
88 while (Node
->LeftChild
!= NULL
)
89 Node
= Node
->LeftChild
;
93 PMEMORY_AREA TempNode
= NULL
;
97 /* Check if we're at the end of tree. */
98 if (Node
->Parent
== NULL
)
104 while (TempNode
== Node
->RightChild
);
110 * @name MmIterateLastNode
113 * Head node of the MEMORY_AREA tree.
115 * @return The rightmost MEMORY_AREA node (ie. the one with highest
119 static PMEMORY_AREA
MmIterateLastNode(PMEMORY_AREA Node
)
121 while (Node
->RightChild
!= NULL
)
122 Node
= Node
->RightChild
;
128 * @name MmIteratePreviousNode
131 * Current node in the tree.
133 * @return Previous node in the tree (sorted by address).
136 static PMEMORY_AREA
MmIteratePrevNode(PMEMORY_AREA Node
)
138 if (Node
->LeftChild
!= NULL
)
140 Node
= Node
->LeftChild
;
141 while (Node
->RightChild
!= NULL
)
142 Node
= Node
->RightChild
;
146 PMEMORY_AREA TempNode
= NULL
;
150 /* Check if we're at the end of tree. */
151 if (Node
->Parent
== NULL
)
157 while (TempNode
== Node
->LeftChild
);
163 MmLocateMemoryAreaByAddress(
164 PMMSUPPORT AddressSpace
,
167 PMEMORY_AREA Node
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
169 DPRINT("MmLocateMemoryAreaByAddress(AddressSpace %p, Address %p)\n",
170 AddressSpace
, Address
);
174 if (Address
< Node
->StartingAddress
)
175 Node
= Node
->LeftChild
;
176 else if (Address
>= Node
->EndingAddress
)
177 Node
= Node
->RightChild
;
180 DPRINT("MmLocateMemoryAreaByAddress(%p): %p [%p - %p]\n",
181 Address
, Node
, Node
->StartingAddress
, Node
->EndingAddress
);
186 DPRINT("MmLocateMemoryAreaByAddress(%p): 0\n", Address
);
191 MmLocateMemoryAreaByRegion(
192 PMMSUPPORT AddressSpace
,
197 PVOID Extent
= (PVOID
)((ULONG_PTR
)Address
+ Length
);
199 /* Special case for empty tree. */
200 if (AddressSpace
->WorkingSetExpansionLinks
.Flink
== NULL
)
203 /* Traverse the tree from left to right. */
204 for (Node
= MmIterateFirstNode((PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
);
206 Node
= MmIterateNextNode(Node
))
208 if (Node
->StartingAddress
>= Address
&&
209 Node
->StartingAddress
< Extent
)
211 DPRINT("MmLocateMemoryAreaByRegion(%p - %p): %p - %p\n",
212 Address
, (ULONG_PTR
)Address
+ Length
, Node
->StartingAddress
,
213 Node
->EndingAddress
);
216 if (Node
->EndingAddress
> Address
&&
217 Node
->EndingAddress
< Extent
)
219 DPRINT("MmLocateMemoryAreaByRegion(%p - %p): %p - %p\n",
220 Address
, (ULONG_PTR
)Address
+ Length
, Node
->StartingAddress
,
221 Node
->EndingAddress
);
224 if (Node
->StartingAddress
<= Address
&&
225 Node
->EndingAddress
>= Extent
)
227 DPRINT("MmLocateMemoryAreaByRegion(%p - %p): %p - %p\n",
228 Address
, (ULONG_PTR
)Address
+ Length
, Node
->StartingAddress
,
229 Node
->EndingAddress
);
232 if (Node
->StartingAddress
>= Extent
)
234 DPRINT("Finished MmLocateMemoryAreaByRegion() = NULL\n");
243 * @name MmCompressHelper
245 * This is helper of MmRebalanceTree. Performs a compression transformation
246 * count times, starting at root.
251 PMMSUPPORT AddressSpace
,
254 PMEMORY_AREA Root
= NULL
;
255 PMEMORY_AREA Red
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
256 PMEMORY_AREA Black
= Red
->LeftChild
;
261 Root
->LeftChild
= Black
;
263 AddressSpace
->WorkingSetExpansionLinks
.Flink
= (PVOID
)Black
;
264 Black
->Parent
= Root
;
265 Red
->LeftChild
= Black
->RightChild
;
266 if (Black
->RightChild
)
267 Black
->RightChild
->Parent
= Red
;
268 Black
->RightChild
= Red
;
274 Red
= Root
->LeftChild
;
275 Black
= Red
->LeftChild
;
281 * @name MmRebalanceTree
283 * Rebalance a memory area tree using the Tree->Vine->Balanced Tree
284 * method described in libavl documentation in chapter 4.12.
285 * (http://www.stanford.edu/~blp/avl/libavl.html/)
290 PMMSUPPORT AddressSpace
)
292 PMEMORY_AREA PreviousNode
;
293 PMEMORY_AREA CurrentNode
;
294 PMEMORY_AREA TempNode
;
296 ULONG Vine
; /* Number of nodes in main vine. */
297 ULONG Leaves
; /* Nodes in incomplete bottom level, if any. */
298 INT Height
; /* Height of produced balanced tree. */
300 /* Transform the tree into Vine. */
303 CurrentNode
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
304 while (CurrentNode
!= NULL
)
306 if (CurrentNode
->RightChild
== NULL
)
308 PreviousNode
= CurrentNode
;
309 CurrentNode
= CurrentNode
->LeftChild
;
314 TempNode
= CurrentNode
->RightChild
;
316 CurrentNode
->RightChild
= TempNode
->LeftChild
;
317 if (TempNode
->LeftChild
)
318 TempNode
->LeftChild
->Parent
= CurrentNode
;
320 TempNode
->LeftChild
= CurrentNode
;
321 CurrentNode
->Parent
= TempNode
;
323 CurrentNode
= TempNode
;
325 if (PreviousNode
!= NULL
)
326 PreviousNode
->LeftChild
= TempNode
;
328 AddressSpace
->WorkingSetExpansionLinks
.Flink
= (PVOID
)TempNode
;
329 TempNode
->Parent
= PreviousNode
;
333 /* Transform Vine back into a balanced tree. */
335 Leaves
= NodeCount
+ 1;
338 ULONG Next
= Leaves
& (Leaves
- 1);
343 Leaves
= NodeCount
+ 1 - Leaves
;
345 MmCompressHelper(AddressSpace
, Leaves
);
347 Vine
= NodeCount
- Leaves
;
348 Height
= 1 + (Leaves
> 0);
351 MmCompressHelper(AddressSpace
, Vine
/ 2);
359 MiInsertVad(IN PMMVAD Vad
,
360 IN PEPROCESS Process
);
364 MiMakeProtectionMask(
370 PMMSUPPORT AddressSpace
,
374 PMEMORY_AREA PreviousNode
;
376 PEPROCESS Process
= MmGetAddressSpaceOwner(AddressSpace
);
378 /* Build a lame VAD if this is a user-space allocation */
379 if ((marea
->EndingAddress
< MmSystemRangeStart
) && (marea
->Type
!= MEMORY_AREA_OWNED_BY_ARM3
))
383 ASSERT(marea
->Type
== MEMORY_AREA_SECTION_VIEW
|| marea
->Type
== MEMORY_AREA_CACHE
);
384 Vad
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(MMVAD
), TAG_MVAD
);
386 RtlZeroMemory(Vad
, sizeof(MMVAD
));
387 Vad
->StartingVpn
= PAGE_ROUND_DOWN(marea
->StartingAddress
) >> PAGE_SHIFT
;
389 * For some strange reason, it is perfectly valid to create a MAREA from 0x1000 to... 0x1000.
390 * In a normal OS/Memory Manager, this would be retarded, but ReactOS allows this (how it works
391 * I don't even want to know).
393 if (marea
->EndingAddress
!= marea
->StartingAddress
)
395 Vad
->EndingVpn
= PAGE_ROUND_DOWN((ULONG_PTR
)marea
->EndingAddress
- 1) >> PAGE_SHIFT
;
399 Vad
->EndingVpn
= Vad
->StartingVpn
;
401 Vad
->u
.VadFlags
.Spare
= 1;
402 Vad
->u
.VadFlags
.PrivateMemory
= 1;
403 Vad
->u
.VadFlags
.Protection
= MiMakeProtectionMask(marea
->Protect
);
406 MiInsertVad(Vad
, Process
);
414 if (AddressSpace
->WorkingSetExpansionLinks
.Flink
== NULL
)
416 AddressSpace
->WorkingSetExpansionLinks
.Flink
= (PVOID
)marea
;
417 marea
->LeftChild
= marea
->RightChild
= marea
->Parent
= NULL
;
421 Node
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
424 DPRINT("marea->EndingAddress: %p Node->StartingAddress: %p\n",
425 marea
->EndingAddress
, Node
->StartingAddress
);
426 DPRINT("marea->StartingAddress: %p Node->EndingAddress: %p\n",
427 marea
->StartingAddress
, Node
->EndingAddress
);
428 ASSERT(marea
->EndingAddress
<= Node
->StartingAddress
||
429 marea
->StartingAddress
>= Node
->EndingAddress
);
430 ASSERT(marea
->StartingAddress
!= Node
->StartingAddress
);
434 if (marea
->StartingAddress
< Node
->StartingAddress
)
435 Node
= Node
->LeftChild
;
437 Node
= Node
->RightChild
;
444 MmRebalanceTree(AddressSpace
);
445 PreviousNode
= Node
->Parent
;
449 while (Node
!= NULL
);
451 marea
->LeftChild
= marea
->RightChild
= NULL
;
452 marea
->Parent
= PreviousNode
;
453 if (marea
->StartingAddress
< PreviousNode
->StartingAddress
)
454 PreviousNode
->LeftChild
= marea
;
456 PreviousNode
->RightChild
= marea
;
461 PMMSUPPORT AddressSpace
,
463 ULONG_PTR Granularity
)
465 ULONG_PTR LowestAddress
, HighestAddress
, Candidate
;
466 PMEMORY_AREA Root
, Node
;
468 /* Get the margins of the address space */
469 if (MmGetAddressSpaceOwner(AddressSpace
) != NULL
)
471 LowestAddress
= (ULONG_PTR
)MM_LOWEST_USER_ADDRESS
;
472 HighestAddress
= (ULONG_PTR
)MmHighestUserAddress
;
476 LowestAddress
= (ULONG_PTR
)MmSystemRangeStart
;
477 HighestAddress
= MAXULONG_PTR
;
480 /* Start with the lowest address */
481 Candidate
= LowestAddress
;
483 /* Check for overflow */
484 if ((Candidate
+ Length
) < Candidate
) return NULL
;
486 /* Get the root of the address space tree */
487 Root
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
489 /* Go to the node with lowest address in the tree. */
490 Node
= Root
? MmIterateFirstNode(Root
) : NULL
;
491 while (Node
&& ((ULONG_PTR
)Node
->EndingAddress
< LowestAddress
))
493 Node
= MmIterateNextNode(Node
);
496 /* Traverse the tree from low to high addresses */
497 while (Node
&& ((ULONG_PTR
)Node
->EndingAddress
< HighestAddress
))
499 /* Check if the memory area fits before the current node */
500 if ((ULONG_PTR
)Node
->StartingAddress
>= (Candidate
+ Length
))
502 DPRINT("MmFindGapBottomUp: %p\n", Candidate
);
503 ASSERT(Candidate
>= LowestAddress
);
504 return (PVOID
)Candidate
;
507 /* Calculate next possible adress above this node */
508 Candidate
= ALIGN_UP_BY((ULONG_PTR
)Node
->EndingAddress
, Granularity
);
510 /* Check for overflow */
511 if ((Candidate
+ Length
) < (ULONG_PTR
)Node
->EndingAddress
) return NULL
;
513 /* Go to the next higher node */
514 Node
= MmIterateNextNode(Node
);
517 /* Check if there is enough space after the last memory area. */
518 if ((Candidate
+ Length
) <= HighestAddress
)
520 DPRINT("MmFindGapBottomUp: %p\n", Candidate
);
521 ASSERT(Candidate
>= LowestAddress
);
522 return (PVOID
)Candidate
;
525 DPRINT("MmFindGapBottomUp: 0\n");
532 PMMSUPPORT AddressSpace
,
534 ULONG_PTR Granularity
)
536 ULONG_PTR LowestAddress
, HighestAddress
, Candidate
;
537 PMEMORY_AREA Root
, Node
;
539 /* Get the margins of the address space */
540 if (MmGetAddressSpaceOwner(AddressSpace
) != NULL
)
542 LowestAddress
= (ULONG_PTR
)MM_LOWEST_USER_ADDRESS
;
543 HighestAddress
= (ULONG_PTR
)MmHighestUserAddress
;
547 LowestAddress
= (ULONG_PTR
)MmSystemRangeStart
;
548 HighestAddress
= MAXULONG_PTR
;
551 /* Calculate the highest candidate */
552 Candidate
= ALIGN_DOWN_BY(HighestAddress
+ 1 - Length
, Granularity
);
554 /* Check for overflow. */
555 if (Candidate
> HighestAddress
) return NULL
;
557 /* Get the root of the address space tree */
558 Root
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
560 /* Go to the node with highest address in the tree. */
561 Node
= Root
? MmIterateLastNode(Root
) : NULL
;
562 while (Node
&& ((ULONG_PTR
)Node
->StartingAddress
> HighestAddress
))
564 Node
= MmIteratePrevNode(Node
);
567 /* Traverse the tree from high to low addresses */
568 while (Node
&& ((ULONG_PTR
)Node
->StartingAddress
> LowestAddress
))
570 /* Check if the memory area fits after the current node */
571 if ((ULONG_PTR
)Node
->EndingAddress
<= Candidate
)
573 DPRINT("MmFindGapTopDown: %p\n", Candidate
);
574 return (PVOID
)Candidate
;
577 /* Calculate next possible adress below this node */
578 Candidate
= ALIGN_DOWN_BY((ULONG_PTR
)Node
->StartingAddress
- Length
,
581 /* Check for overflow. */
582 if (Candidate
> (ULONG_PTR
)Node
->StartingAddress
)
585 /* Go to the next lower node */
586 Node
= MmIteratePrevNode(Node
);
589 /* Check if the last candidate is inside the given range */
590 if (Candidate
>= LowestAddress
)
592 DPRINT("MmFindGapTopDown: %p\n", Candidate
);
593 return (PVOID
)Candidate
;
596 DPRINT("MmFindGapTopDown: 0\n");
603 PMMSUPPORT AddressSpace
,
605 ULONG_PTR Granularity
,
609 return MmFindGapTopDown(AddressSpace
, Length
, Granularity
);
611 return MmFindGapBottomUp(AddressSpace
, Length
, Granularity
);
616 PMMSUPPORT AddressSpace
,
619 PMEMORY_AREA Node
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
620 PMEMORY_AREA RightNeighbour
= NULL
;
621 PVOID LowestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ? MM_LOWEST_USER_ADDRESS
: MmSystemRangeStart
;
622 PVOID HighestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ?
623 (PVOID
)((ULONG_PTR
)MmSystemRangeStart
- 1) : (PVOID
)MAXULONG_PTR
;
625 Address
= MM_ROUND_DOWN(Address
, PAGE_SIZE
);
627 if (LowestAddress
< MmSystemRangeStart
)
629 if (Address
>= MmSystemRangeStart
)
636 if (Address
< LowestAddress
)
644 if (Address
< Node
->StartingAddress
)
646 RightNeighbour
= Node
;
647 Node
= Node
->LeftChild
;
649 else if (Address
>= Node
->EndingAddress
)
651 Node
= Node
->RightChild
;
655 DPRINT("MmFindGapAtAddress: 0\n");
662 DPRINT("MmFindGapAtAddress: %p [%p]\n", Address
,
663 (ULONG_PTR
)RightNeighbour
->StartingAddress
- (ULONG_PTR
)Address
);
664 return (ULONG_PTR
)RightNeighbour
->StartingAddress
- (ULONG_PTR
)Address
;
668 DPRINT("MmFindGapAtAddress: %p [%p]\n", Address
,
669 (ULONG_PTR
)HighestAddress
- (ULONG_PTR
)Address
);
670 return (ULONG_PTR
)HighestAddress
- (ULONG_PTR
)Address
;
676 MiRemoveNode(IN PMMADDRESS_NODE Node
,
677 IN PMM_AVL_TABLE Table
);
680 * @name MmFreeMemoryArea
682 * Free an existing memory area.
684 * @param AddressSpace
685 * Address space to free the area from.
687 * Memory area we're about to free.
689 * Callback function for each freed page.
690 * @param FreePageContext
691 * Context passed to the callback function.
695 * @remarks Lock the address space before calling this function.
699 MiDeletePte(IN PMMPTE PointerPte
,
700 IN PVOID VirtualAddress
,
701 IN PEPROCESS CurrentProcess
,
702 IN PMMPTE PrototypePte
);
706 PMMSUPPORT AddressSpace
,
707 PMEMORY_AREA MemoryArea
,
708 PMM_FREE_PAGE_FUNC FreePage
,
709 PVOID FreePageContext
)
711 PMEMORY_AREA
*ParentReplace
;
715 if (MemoryArea
->Type
!= MEMORY_AREA_OWNED_BY_ARM3
)
717 PEPROCESS CurrentProcess
= PsGetCurrentProcess();
718 PEPROCESS Process
= MmGetAddressSpaceOwner(AddressSpace
);
720 if (Process
!= NULL
&&
721 Process
!= CurrentProcess
)
723 KeAttachProcess(&Process
->Pcb
);
726 EndAddress
= MM_ROUND_UP(MemoryArea
->EndingAddress
, PAGE_SIZE
);
727 for (Address
= (ULONG_PTR
)MemoryArea
->StartingAddress
;
728 Address
< (ULONG_PTR
)EndAddress
;
729 Address
+= PAGE_SIZE
)
731 BOOLEAN Dirty
= FALSE
;
732 SWAPENTRY SwapEntry
= 0;
735 if (MmIsPageSwapEntry(Process
, (PVOID
)Address
))
737 MmDeletePageFileMapping(Process
, (PVOID
)Address
, &SwapEntry
);
741 MmDeleteVirtualMapping(Process
, (PVOID
)Address
, FALSE
, &Dirty
, &Page
);
743 if (FreePage
!= NULL
)
745 FreePage(FreePageContext
, MemoryArea
, (PVOID
)Address
,
746 Page
, SwapEntry
, (BOOLEAN
)Dirty
);
748 #if (_MI_PAGING_LEVELS == 2)
749 /* Remove page table reference */
750 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
751 if((SwapEntry
|| Page
) && ((PVOID
)Address
< MmSystemRangeStart
))
753 ASSERT(AddressSpace
!= MmGetKernelAddressSpace());
754 if(MmWorkingSetList
->UsedPageTableEntries
[MiGetPdeOffset(Address
)] == 0)
756 /* No PTE relies on this PDE. Release it */
757 KIRQL OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
758 PMMPDE PointerPde
= MiAddressToPde(Address
);
759 ASSERT(PointerPde
->u
.Hard
.Valid
== 1);
760 MiDeletePte(PointerPde
, MiPdeToPte(PointerPde
), Process
, NULL
);
761 ASSERT(PointerPde
->u
.Hard
.Valid
== 0);
762 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
768 if (Process
!= NULL
&&
769 Process
!= CurrentProcess
)
776 ASSERT(MemoryArea
->EndingAddress
< MmSystemRangeStart
);
777 ASSERT(MemoryArea
->Type
== MEMORY_AREA_SECTION_VIEW
|| MemoryArea
->Type
== MEMORY_AREA_CACHE
);
779 /* MmCleanProcessAddressSpace might have removed it (and this would be MmDeleteProcessAdressSpace) */
780 ASSERT(((PMMVAD
)MemoryArea
->Vad
)->u
.VadFlags
.Spare
!= 0);
781 if (((PMMVAD
)MemoryArea
->Vad
)->u
.VadFlags
.Spare
== 1)
783 MiRemoveNode(MemoryArea
->Vad
, &Process
->VadRoot
);
786 ExFreePoolWithTag(MemoryArea
->Vad
, TAG_MVAD
);
787 MemoryArea
->Vad
= NULL
;
791 /* There must be no page ops in progress */
792 ASSERT(MemoryArea
->PageOpCount
== 0);
794 /* Remove the tree item. */
796 if (MemoryArea
->Parent
!= NULL
)
798 if (MemoryArea
->Parent
->LeftChild
== MemoryArea
)
799 ParentReplace
= &MemoryArea
->Parent
->LeftChild
;
801 ParentReplace
= &MemoryArea
->Parent
->RightChild
;
804 ParentReplace
= (PMEMORY_AREA
*)&AddressSpace
->WorkingSetExpansionLinks
.Flink
;
806 if (MemoryArea
->RightChild
== NULL
)
808 *ParentReplace
= MemoryArea
->LeftChild
;
809 if (MemoryArea
->LeftChild
)
810 MemoryArea
->LeftChild
->Parent
= MemoryArea
->Parent
;
814 if (MemoryArea
->RightChild
->LeftChild
== NULL
)
816 MemoryArea
->RightChild
->LeftChild
= MemoryArea
->LeftChild
;
817 if (MemoryArea
->LeftChild
)
818 MemoryArea
->LeftChild
->Parent
= MemoryArea
->RightChild
;
820 *ParentReplace
= MemoryArea
->RightChild
;
821 MemoryArea
->RightChild
->Parent
= MemoryArea
->Parent
;
825 PMEMORY_AREA LowestNode
;
827 LowestNode
= MemoryArea
->RightChild
->LeftChild
;
828 while (LowestNode
->LeftChild
!= NULL
)
829 LowestNode
= LowestNode
->LeftChild
;
831 LowestNode
->Parent
->LeftChild
= LowestNode
->RightChild
;
832 if (LowestNode
->RightChild
)
833 LowestNode
->RightChild
->Parent
= LowestNode
->Parent
;
835 LowestNode
->LeftChild
= MemoryArea
->LeftChild
;
836 if (MemoryArea
->LeftChild
)
837 MemoryArea
->LeftChild
->Parent
= LowestNode
;
839 LowestNode
->RightChild
= MemoryArea
->RightChild
;
840 MemoryArea
->RightChild
->Parent
= LowestNode
;
842 *ParentReplace
= LowestNode
;
843 LowestNode
->Parent
= MemoryArea
->Parent
;
848 ExFreePoolWithTag(MemoryArea
, TAG_MAREA
);
850 DPRINT("MmFreeMemoryAreaByNode() succeeded\n");
852 return STATUS_SUCCESS
;
856 * @name MmCreateMemoryArea
858 * Create a memory area.
860 * @param AddressSpace
861 * Address space to create the area in.
863 * Type of the memory area.
865 * Base address for the memory area we're about the create. On
866 * input it contains either 0 (auto-assign address) or preferred
867 * address. On output it contains the starting address of the
868 * newly created area.
870 * Length of the area to allocate.
872 * Protection attributes for the memory area.
874 * Receives a pointer to the memory area on successful exit.
878 * @remarks Lock the address space before calling this function.
882 MmCreateMemoryArea(PMMSUPPORT AddressSpace
,
887 PMEMORY_AREA
*Result
,
888 BOOLEAN FixedAddress
,
889 ULONG AllocationFlags
,
890 PHYSICAL_ADDRESS BoundaryAddressMultiple
)
895 PMEMORY_AREA MemoryArea
;
897 DPRINT("MmCreateMemoryArea(Type 0x%lx, BaseAddress %p, "
898 "*BaseAddress %p, Length %p, AllocationFlags %x, "
899 "FixedAddress %x, Result %p)\n",
900 Type
, BaseAddress
, *BaseAddress
, Length
, AllocationFlags
,
901 FixedAddress
, Result
);
903 Granularity
= PAGE_SIZE
;
904 if ((*BaseAddress
) == 0 && !FixedAddress
)
906 tmpLength
= (ULONG_PTR
)MM_ROUND_UP(Length
, Granularity
);
907 *BaseAddress
= MmFindGap(AddressSpace
,
910 (AllocationFlags
& MEM_TOP_DOWN
) == MEM_TOP_DOWN
);
911 if ((*BaseAddress
) == 0)
913 DPRINT("No suitable gap\n");
914 return STATUS_NO_MEMORY
;
919 tmpLength
= Length
+ ((ULONG_PTR
) *BaseAddress
920 - (ULONG_PTR
) MM_ROUND_DOWN(*BaseAddress
, Granularity
));
921 tmpLength
= (ULONG_PTR
)MM_ROUND_UP(tmpLength
, Granularity
);
922 *BaseAddress
= MM_ROUND_DOWN(*BaseAddress
, Granularity
);
924 if (!MmGetAddressSpaceOwner(AddressSpace
) && *BaseAddress
< MmSystemRangeStart
)
926 return STATUS_ACCESS_VIOLATION
;
929 if (MmGetAddressSpaceOwner(AddressSpace
) &&
930 (ULONG_PTR
)(*BaseAddress
) + tmpLength
> (ULONG_PTR
)MmSystemRangeStart
)
932 DPRINT("Memory area for user mode address space exceeds MmSystemRangeStart\n");
933 return STATUS_ACCESS_VIOLATION
;
936 if (BoundaryAddressMultiple
.QuadPart
!= 0)
938 EndAddress
= ((char*)(*BaseAddress
)) + tmpLength
-1;
939 ASSERT(((ULONG_PTR
)*BaseAddress
/BoundaryAddressMultiple
.QuadPart
) == ((DWORD_PTR
)EndAddress
/BoundaryAddressMultiple
.QuadPart
));
942 if (MmLocateMemoryAreaByRegion(AddressSpace
,
946 DPRINT("Memory area already occupied\n");
947 return STATUS_CONFLICTING_ADDRESSES
;
952 // Is this a static memory area?
954 if (Type
& MEMORY_AREA_STATIC
)
957 // Use the static array instead of the pool
959 ASSERT(MiStaticMemoryAreaCount
< MI_STATIC_MEMORY_AREAS
);
960 MemoryArea
= &MiStaticMemoryAreas
[MiStaticMemoryAreaCount
++];
961 Type
&= ~MEMORY_AREA_STATIC
;
966 // Allocate the memory area from nonpaged pool
968 MemoryArea
= ExAllocatePoolWithTag(NonPagedPool
,
973 if (!MemoryArea
) return STATUS_NO_MEMORY
;
975 RtlZeroMemory(MemoryArea
, sizeof(MEMORY_AREA
));
976 MemoryArea
->Type
= Type
;
977 MemoryArea
->StartingAddress
= *BaseAddress
;
978 MemoryArea
->EndingAddress
= (PVOID
)((ULONG_PTR
)*BaseAddress
+ tmpLength
);
979 MemoryArea
->Protect
= Protect
;
980 MemoryArea
->Flags
= AllocationFlags
;
981 //MemoryArea->LockCount = 0;
982 MemoryArea
->PageOpCount
= 0;
983 MemoryArea
->DeleteInProgress
= FALSE
;
985 MmInsertMemoryArea(AddressSpace
, MemoryArea
);
987 *Result
= MemoryArea
;
989 DPRINT("MmCreateMemoryArea() succeeded (%p)\n", *BaseAddress
);
990 return STATUS_SUCCESS
;
994 MmMapMemoryArea(PVOID BaseAddress
,
1002 ASSERT(((ULONG_PTR
)BaseAddress
% PAGE_SIZE
) == 0);
1004 for (i
= 0; i
< PAGE_ROUND_UP(Length
) / PAGE_SIZE
; i
++)
1008 Status
= MmRequestPageMemoryConsumer(Consumer
, TRUE
, &Page
);
1009 if (!NT_SUCCESS(Status
))
1011 DPRINT1("Unable to allocate page\n");
1012 KeBugCheck(MEMORY_MANAGEMENT
);
1014 Status
= MmCreateVirtualMapping (NULL
,
1015 (PVOID
)((ULONG_PTR
)BaseAddress
+ (i
* PAGE_SIZE
)),
1019 if (!NT_SUCCESS(Status
))
1021 DPRINT1("Unable to create virtual mapping\n");
1022 KeBugCheck(MEMORY_MANAGEMENT
);
1029 MmDeleteProcessAddressSpace2(IN PEPROCESS Process
);
1033 MmDeleteProcessAddressSpace(PEPROCESS Process
)
1036 PMEMORY_AREA MemoryArea
;
1038 DPRINT("MmDeleteProcessAddressSpace(Process %x (%s))\n", Process
,
1039 Process
->ImageFileName
);
1042 RemoveEntryList(&Process
->MmProcessLinks
);
1044 MmLockAddressSpace(&Process
->Vm
);
1046 while ((MemoryArea
= (PMEMORY_AREA
)Process
->Vm
.WorkingSetExpansionLinks
.Flink
) != NULL
)
1048 switch (MemoryArea
->Type
)
1050 case MEMORY_AREA_SECTION_VIEW
:
1051 Address
= (PVOID
)MemoryArea
->StartingAddress
;
1052 MmUnlockAddressSpace(&Process
->Vm
);
1053 MmUnmapViewOfSection(Process
, Address
);
1054 MmLockAddressSpace(&Process
->Vm
);
1057 case MEMORY_AREA_CACHE
:
1058 Address
= (PVOID
)MemoryArea
->StartingAddress
;
1059 MmUnlockAddressSpace(&Process
->Vm
);
1060 MmUnmapViewOfCacheSegment(&Process
->Vm
, Address
);
1061 MmLockAddressSpace(&Process
->Vm
);
1064 case MEMORY_AREA_OWNED_BY_ARM3
:
1065 MmFreeMemoryArea(&Process
->Vm
,
1072 KeBugCheck(MEMORY_MANAGEMENT
);
1076 #if (_MI_PAGING_LEVELS == 2)
1080 /* Attach to Process */
1081 KeAttachProcess(&Process
->Pcb
);
1083 /* Acquire PFN lock */
1084 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1086 for(Address
= MI_LOWEST_VAD_ADDRESS
;
1087 Address
< MM_HIGHEST_VAD_ADDRESS
;
1088 Address
=(PVOID
)((ULONG_PTR
)Address
+ (PAGE_SIZE
* PTE_COUNT
)))
1090 /* At this point all references should be dead */
1091 ASSERT(MmWorkingSetList
->UsedPageTableEntries
[MiGetPdeOffset(Address
)] == 0);
1092 pointerPde
= MiAddressToPde(Address
);
1093 /* Unlike in ARM3, we don't necesarrily free the PDE page as soon as reference reaches 0,
1094 * so we must clean up a bit when process closes */
1095 if(pointerPde
->u
.Hard
.Valid
)
1096 MiDeletePte(pointerPde
, MiPdeToPte(pointerPde
), Process
, NULL
);
1097 ASSERT(pointerPde
->u
.Hard
.Valid
== 0);
1100 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1107 MmUnlockAddressSpace(&Process
->Vm
);
1109 DPRINT("Finished MmReleaseMmInfo()\n");
1110 MmDeleteProcessAddressSpace2(Process
);
1111 return(STATUS_SUCCESS
);