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 *****************************************************************/
48 MEMORY_AREA MiStaticMemoryAreas
[MI_STATIC_MEMORY_AREAS
];
49 ULONG MiStaticMemoryAreaCount
;
51 /* FUNCTIONS *****************************************************************/
54 * @name MmIterateFirstNode
57 * Head node of the MEMORY_AREA tree.
59 * @return The leftmost MEMORY_AREA node (ie. the one with lowest
63 static PMEMORY_AREA
MmIterateFirstNode(PMEMORY_AREA Node
)
65 while (Node
->LeftChild
!= NULL
)
66 Node
= Node
->LeftChild
;
72 * @name MmIterateNextNode
75 * Current node in the tree.
77 * @return Next node in the tree (sorted by address).
80 static PMEMORY_AREA
MmIterateNextNode(PMEMORY_AREA Node
)
82 if (Node
->RightChild
!= NULL
)
84 Node
= Node
->RightChild
;
85 while (Node
->LeftChild
!= NULL
)
86 Node
= Node
->LeftChild
;
90 PMEMORY_AREA TempNode
= NULL
;
94 /* Check if we're at the end of tree. */
95 if (Node
->Parent
== NULL
)
101 while (TempNode
== Node
->RightChild
);
107 * @name MmIterateLastNode
110 * Head node of the MEMORY_AREA tree.
112 * @return The rightmost MEMORY_AREA node (ie. the one with highest
116 static PMEMORY_AREA
MmIterateLastNode(PMEMORY_AREA Node
)
118 while (Node
->RightChild
!= NULL
)
119 Node
= Node
->RightChild
;
125 * @name MmIteratePreviousNode
128 * Current node in the tree.
130 * @return Previous node in the tree (sorted by address).
133 static PMEMORY_AREA
MmIteratePrevNode(PMEMORY_AREA Node
)
135 if (Node
->LeftChild
!= NULL
)
137 Node
= Node
->LeftChild
;
138 while (Node
->RightChild
!= NULL
)
139 Node
= Node
->RightChild
;
143 PMEMORY_AREA TempNode
= NULL
;
147 /* Check if we're at the end of tree. */
148 if (Node
->Parent
== NULL
)
154 while (TempNode
== Node
->LeftChild
);
160 MmLocateMemoryAreaByAddress(
161 PMMSUPPORT AddressSpace
,
164 PMEMORY_AREA Node
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
166 DPRINT("MmLocateMemoryAreaByAddress(AddressSpace %p, Address %p)\n",
167 AddressSpace
, Address
);
171 if (Address
< Node
->StartingAddress
)
172 Node
= Node
->LeftChild
;
173 else if (Address
>= Node
->EndingAddress
)
174 Node
= Node
->RightChild
;
177 DPRINT("MmLocateMemoryAreaByAddress(%p): %p [%p - %p]\n",
178 Address
, Node
, Node
->StartingAddress
, Node
->EndingAddress
);
183 DPRINT("MmLocateMemoryAreaByAddress(%p): 0\n", Address
);
188 MmLocateMemoryAreaByRegion(
189 PMMSUPPORT AddressSpace
,
194 PVOID Extent
= (PVOID
)((ULONG_PTR
)Address
+ Length
);
196 /* Special case for empty tree. */
197 if (AddressSpace
->WorkingSetExpansionLinks
.Flink
== NULL
)
200 /* Traverse the tree from left to right. */
201 for (Node
= MmIterateFirstNode((PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
);
203 Node
= MmIterateNextNode(Node
))
205 if (Node
->StartingAddress
>= Address
&&
206 Node
->StartingAddress
< Extent
)
208 DPRINT("MmLocateMemoryAreaByRegion(%p - %p): %p - %p\n",
209 Address
, (ULONG_PTR
)Address
+ Length
, Node
->StartingAddress
,
210 Node
->EndingAddress
);
213 if (Node
->EndingAddress
> Address
&&
214 Node
->EndingAddress
< Extent
)
216 DPRINT("MmLocateMemoryAreaByRegion(%p - %p): %p - %p\n",
217 Address
, (ULONG_PTR
)Address
+ Length
, Node
->StartingAddress
,
218 Node
->EndingAddress
);
221 if (Node
->StartingAddress
<= Address
&&
222 Node
->EndingAddress
>= Extent
)
224 DPRINT("MmLocateMemoryAreaByRegion(%p - %p): %p - %p\n",
225 Address
, (ULONG_PTR
)Address
+ Length
, Node
->StartingAddress
,
226 Node
->EndingAddress
);
229 if (Node
->StartingAddress
>= Extent
)
231 DPRINT("Finished MmLocateMemoryAreaByRegion() = NULL\n");
240 * @name MmCompressHelper
242 * This is helper of MmRebalanceTree. Performs a compression transformation
243 * count times, starting at root.
248 PMMSUPPORT AddressSpace
,
251 PMEMORY_AREA Root
= NULL
;
252 PMEMORY_AREA Red
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
253 PMEMORY_AREA Black
= Red
->LeftChild
;
258 Root
->LeftChild
= Black
;
260 AddressSpace
->WorkingSetExpansionLinks
.Flink
= (PVOID
)Black
;
261 Black
->Parent
= Root
;
262 Red
->LeftChild
= Black
->RightChild
;
263 if (Black
->RightChild
)
264 Black
->RightChild
->Parent
= Red
;
265 Black
->RightChild
= Red
;
271 Red
= Root
->LeftChild
;
272 Black
= Red
->LeftChild
;
278 * @name MmRebalanceTree
280 * Rebalance a memory area tree using the Tree->Vine->Balanced Tree
281 * method described in libavl documentation in chapter 4.12.
282 * (http://www.stanford.edu/~blp/avl/libavl.html/)
287 PMMSUPPORT AddressSpace
)
289 PMEMORY_AREA PreviousNode
;
290 PMEMORY_AREA CurrentNode
;
291 PMEMORY_AREA TempNode
;
293 ULONG Vine
; /* Number of nodes in main vine. */
294 ULONG Leaves
; /* Nodes in incomplete bottom level, if any. */
295 INT Height
; /* Height of produced balanced tree. */
297 /* Transform the tree into Vine. */
300 CurrentNode
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
301 while (CurrentNode
!= NULL
)
303 if (CurrentNode
->RightChild
== NULL
)
305 PreviousNode
= CurrentNode
;
306 CurrentNode
= CurrentNode
->LeftChild
;
311 TempNode
= CurrentNode
->RightChild
;
313 CurrentNode
->RightChild
= TempNode
->LeftChild
;
314 if (TempNode
->LeftChild
)
315 TempNode
->LeftChild
->Parent
= CurrentNode
;
317 TempNode
->LeftChild
= CurrentNode
;
318 CurrentNode
->Parent
= TempNode
;
320 CurrentNode
= TempNode
;
322 if (PreviousNode
!= NULL
)
323 PreviousNode
->LeftChild
= TempNode
;
325 AddressSpace
->WorkingSetExpansionLinks
.Flink
= (PVOID
)TempNode
;
326 TempNode
->Parent
= PreviousNode
;
330 /* Transform Vine back into a balanced tree. */
332 Leaves
= NodeCount
+ 1;
335 ULONG Next
= Leaves
& (Leaves
- 1);
340 Leaves
= NodeCount
+ 1 - Leaves
;
342 MmCompressHelper(AddressSpace
, Leaves
);
344 Vine
= NodeCount
- Leaves
;
345 Height
= 1 + (Leaves
> 0);
348 MmCompressHelper(AddressSpace
, Vine
/ 2);
356 MiInsertVad(IN PMMVAD Vad
,
357 IN PEPROCESS Process
);
361 MiMakeProtectionMask(
367 PMMSUPPORT AddressSpace
,
371 PMEMORY_AREA PreviousNode
;
374 /* Build a lame VAD if this is a user-space allocation */
375 if ((marea
->EndingAddress
< MmSystemRangeStart
) && (marea
->Type
!= MEMORY_AREA_OWNED_BY_ARM3
))
379 ASSERT(marea
->Type
== MEMORY_AREA_VIRTUAL_MEMORY
|| marea
->Type
== MEMORY_AREA_SECTION_VIEW
);
380 Vad
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(MMVAD
), 'Fake');
382 RtlZeroMemory(Vad
, sizeof(MMVAD
));
383 Vad
->StartingVpn
= PAGE_ROUND_DOWN(marea
->StartingAddress
) >> PAGE_SHIFT
;
385 * For some strange reason, it is perfectly valid to create a MAREA from 0x1000 to... 0x1000.
386 * In a normal OS/Memory Manager, this would be retarded, but ReactOS allows this (how it works
387 * I don't even want to know).
389 if (marea
->EndingAddress
!= marea
->StartingAddress
)
391 Vad
->EndingVpn
= PAGE_ROUND_DOWN((ULONG_PTR
)marea
->EndingAddress
- 1) >> PAGE_SHIFT
;
395 Vad
->EndingVpn
= Vad
->StartingVpn
;
397 Vad
->u
.VadFlags
.Spare
= 1;
398 Vad
->u
.VadFlags
.PrivateMemory
= 1;
399 Vad
->u
.VadFlags
.Protection
= MiMakeProtectionMask(marea
->Protect
);
400 MiInsertVad(Vad
, MmGetAddressSpaceOwner(AddressSpace
));
408 if (AddressSpace
->WorkingSetExpansionLinks
.Flink
== NULL
)
410 AddressSpace
->WorkingSetExpansionLinks
.Flink
= (PVOID
)marea
;
411 marea
->LeftChild
= marea
->RightChild
= marea
->Parent
= NULL
;
415 Node
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
418 DPRINT("marea->EndingAddress: %p Node->StartingAddress: %p\n",
419 marea
->EndingAddress
, Node
->StartingAddress
);
420 DPRINT("marea->StartingAddress: %p Node->EndingAddress: %p\n",
421 marea
->StartingAddress
, Node
->EndingAddress
);
422 ASSERT(marea
->EndingAddress
<= Node
->StartingAddress
||
423 marea
->StartingAddress
>= Node
->EndingAddress
);
424 ASSERT(marea
->StartingAddress
!= Node
->StartingAddress
);
428 if (marea
->StartingAddress
< Node
->StartingAddress
)
429 Node
= Node
->LeftChild
;
431 Node
= Node
->RightChild
;
438 MmRebalanceTree(AddressSpace
);
439 PreviousNode
= Node
->Parent
;
443 while (Node
!= NULL
);
445 marea
->LeftChild
= marea
->RightChild
= NULL
;
446 marea
->Parent
= PreviousNode
;
447 if (marea
->StartingAddress
< PreviousNode
->StartingAddress
)
448 PreviousNode
->LeftChild
= marea
;
450 PreviousNode
->RightChild
= marea
;
455 PMMSUPPORT AddressSpace
,
457 ULONG_PTR Granularity
)
459 PVOID LowestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ? MM_LOWEST_USER_ADDRESS
: MmSystemRangeStart
;
460 PVOID HighestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ?
461 (PVOID
)((ULONG_PTR
)MmSystemRangeStart
- 1) : (PVOID
)MAXULONG_PTR
;
462 PVOID AlignedAddress
;
464 PMEMORY_AREA FirstNode
;
465 PMEMORY_AREA PreviousNode
;
467 DPRINT("LowestAddress: %p HighestAddress: %p\n",
468 LowestAddress
, HighestAddress
);
470 AlignedAddress
= MM_ROUND_UP(LowestAddress
, Granularity
);
472 /* Special case for empty tree. */
473 if (AddressSpace
->WorkingSetExpansionLinks
.Flink
== NULL
)
475 if ((ULONG_PTR
)HighestAddress
- (ULONG_PTR
)AlignedAddress
>= Length
)
477 DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress
);
478 return AlignedAddress
;
480 DPRINT("MmFindGapBottomUp: 0\n");
484 /* Go to the node with lowest address in the tree. */
485 FirstNode
= Node
= MmIterateFirstNode((PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
);
487 /* Traverse the tree from left to right. */
491 Node
= MmIterateNextNode(Node
);
495 AlignedAddress
= MM_ROUND_UP(PreviousNode
->EndingAddress
, Granularity
);
496 if (Node
->StartingAddress
> AlignedAddress
&&
497 (ULONG_PTR
)Node
->StartingAddress
- (ULONG_PTR
)AlignedAddress
>= Length
)
499 DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress
);
500 return AlignedAddress
;
506 /* Check if there is enough space after the last memory area. */
507 AlignedAddress
= MM_ROUND_UP(PreviousNode
->EndingAddress
, Granularity
);
508 if ((ULONG_PTR
)HighestAddress
> (ULONG_PTR
)AlignedAddress
&&
509 (ULONG_PTR
)HighestAddress
- (ULONG_PTR
)AlignedAddress
>= Length
)
511 DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress
);
512 return AlignedAddress
;
515 /* Check if there is enough space before the first memory area. */
516 AlignedAddress
= MM_ROUND_UP(LowestAddress
, Granularity
);
517 if (FirstNode
->StartingAddress
> AlignedAddress
&&
518 (ULONG_PTR
)FirstNode
->StartingAddress
- (ULONG_PTR
)AlignedAddress
>= Length
)
520 DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress
);
521 return AlignedAddress
;
524 DPRINT("MmFindGapBottomUp: 0\n");
531 PMMSUPPORT AddressSpace
,
533 ULONG_PTR Granularity
)
535 PVOID LowestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ? MM_LOWEST_USER_ADDRESS
: MmSystemRangeStart
;
536 PVOID HighestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ?
537 (PVOID
)((ULONG_PTR
)MmSystemRangeStart
- 1) : (PVOID
)MAXULONG_PTR
;
538 PVOID AlignedAddress
;
540 PMEMORY_AREA PreviousNode
;
542 DPRINT("LowestAddress: %p HighestAddress: %p\n",
543 LowestAddress
, HighestAddress
);
545 AlignedAddress
= MM_ROUND_DOWN((ULONG_PTR
)HighestAddress
- Length
+ 1, Granularity
);
547 /* Check for overflow. */
548 if (AlignedAddress
> HighestAddress
)
551 /* Special case for empty tree. */
552 if (AddressSpace
->WorkingSetExpansionLinks
.Flink
== NULL
)
554 if (AlignedAddress
>= LowestAddress
)
556 DPRINT("MmFindGapTopDown: %p\n", AlignedAddress
);
557 return AlignedAddress
;
559 DPRINT("MmFindGapTopDown: 0\n");
563 /* Go to the node with highest address in the tree. */
564 Node
= MmIterateLastNode((PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
);
566 /* Check if there is enough space after the last memory area. */
567 if (Node
->EndingAddress
<= AlignedAddress
)
569 DPRINT("MmFindGapTopDown: %p\n", AlignedAddress
);
570 return AlignedAddress
;
573 /* Traverse the tree from left to right. */
577 Node
= MmIteratePrevNode(Node
);
581 AlignedAddress
= MM_ROUND_DOWN((ULONG_PTR
)PreviousNode
->StartingAddress
- Length
+ 1, Granularity
);
583 /* Check for overflow. */
584 if (AlignedAddress
> PreviousNode
->StartingAddress
)
587 if (Node
->EndingAddress
<= AlignedAddress
)
589 DPRINT("MmFindGapTopDown: %p\n", AlignedAddress
);
590 return AlignedAddress
;
596 AlignedAddress
= MM_ROUND_DOWN((ULONG_PTR
)PreviousNode
->StartingAddress
- Length
+ 1, Granularity
);
598 /* Check for overflow. */
599 if (AlignedAddress
> PreviousNode
->StartingAddress
)
602 if (AlignedAddress
>= LowestAddress
)
604 DPRINT("MmFindGapTopDown: %p\n", AlignedAddress
);
605 return AlignedAddress
;
608 DPRINT("MmFindGapTopDown: 0\n");
615 PMMSUPPORT AddressSpace
,
617 ULONG_PTR Granularity
,
621 return MmFindGapTopDown(AddressSpace
, Length
, Granularity
);
623 return MmFindGapBottomUp(AddressSpace
, Length
, Granularity
);
628 PMMSUPPORT AddressSpace
,
631 PMEMORY_AREA Node
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
632 PMEMORY_AREA RightNeighbour
= NULL
;
633 PVOID LowestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ? MM_LOWEST_USER_ADDRESS
: MmSystemRangeStart
;
634 PVOID HighestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ?
635 (PVOID
)((ULONG_PTR
)MmSystemRangeStart
- 1) : (PVOID
)MAXULONG_PTR
;
637 Address
= MM_ROUND_DOWN(Address
, PAGE_SIZE
);
639 if (LowestAddress
< MmSystemRangeStart
)
641 if (Address
>= MmSystemRangeStart
)
648 if (Address
< LowestAddress
)
656 if (Address
< Node
->StartingAddress
)
658 RightNeighbour
= Node
;
659 Node
= Node
->LeftChild
;
661 else if (Address
>= Node
->EndingAddress
)
663 Node
= Node
->RightChild
;
667 DPRINT("MmFindGapAtAddress: 0\n");
674 DPRINT("MmFindGapAtAddress: %p [%p]\n", Address
,
675 (ULONG_PTR
)RightNeighbour
->StartingAddress
- (ULONG_PTR
)Address
);
676 return (ULONG_PTR
)RightNeighbour
->StartingAddress
- (ULONG_PTR
)Address
;
680 DPRINT("MmFindGapAtAddress: %p [%p]\n", Address
,
681 (ULONG_PTR
)HighestAddress
- (ULONG_PTR
)Address
);
682 return (ULONG_PTR
)HighestAddress
- (ULONG_PTR
)Address
;
688 MiRemoveNode(IN PMMADDRESS_NODE Node
,
689 IN PMM_AVL_TABLE Table
);
692 * @name MmFreeMemoryArea
694 * Free an existing memory area.
696 * @param AddressSpace
697 * Address space to free the area from.
699 * Memory area we're about to free.
701 * Callback function for each freed page.
702 * @param FreePageContext
703 * Context passed to the callback function.
707 * @remarks Lock the address space before calling this function.
712 PMMSUPPORT AddressSpace
,
713 PMEMORY_AREA MemoryArea
,
714 PMM_FREE_PAGE_FUNC FreePage
,
715 PVOID FreePageContext
)
717 PMEMORY_AREA
*ParentReplace
;
721 if (MemoryArea
->Type
!= MEMORY_AREA_OWNED_BY_ARM3
)
723 PEPROCESS CurrentProcess
= PsGetCurrentProcess();
724 PEPROCESS Process
= MmGetAddressSpaceOwner(AddressSpace
);
726 if (Process
!= NULL
&&
727 Process
!= CurrentProcess
)
729 KeAttachProcess(&Process
->Pcb
);
732 EndAddress
= MM_ROUND_UP(MemoryArea
->EndingAddress
, PAGE_SIZE
);
733 for (Address
= (ULONG_PTR
)MemoryArea
->StartingAddress
;
734 Address
< (ULONG_PTR
)EndAddress
;
735 Address
+= PAGE_SIZE
)
737 BOOLEAN Dirty
= FALSE
;
738 SWAPENTRY SwapEntry
= 0;
741 if (MmIsPageSwapEntry(Process
, (PVOID
)Address
))
743 MmDeletePageFileMapping(Process
, (PVOID
)Address
, &SwapEntry
);
747 MmDeleteVirtualMapping(Process
, (PVOID
)Address
, FALSE
, &Dirty
, &Page
);
749 if (FreePage
!= NULL
)
751 FreePage(FreePageContext
, MemoryArea
, (PVOID
)Address
,
752 Page
, SwapEntry
, (BOOLEAN
)Dirty
);
756 if (Process
!= NULL
&&
757 Process
!= CurrentProcess
)
764 ASSERT(MemoryArea
->EndingAddress
< MmSystemRangeStart
);
765 ASSERT(MemoryArea
->Type
== MEMORY_AREA_VIRTUAL_MEMORY
|| MemoryArea
->Type
== MEMORY_AREA_SECTION_VIEW
);
767 /* MmCleanProcessAddressSpace might have removed it (and this would be MmDeleteProcessAdressSpace) */
768 ASSERT(((PMMVAD
)MemoryArea
->Vad
)->u
.VadFlags
.Spare
!= 0);
769 if (((PMMVAD
)MemoryArea
->Vad
)->u
.VadFlags
.Spare
== 1)
771 MiRemoveNode(MemoryArea
->Vad
, &Process
->VadRoot
);
774 ExFreePool(MemoryArea
->Vad
);
775 MemoryArea
->Vad
= NULL
;
779 /* Remove the tree item. */
781 if (MemoryArea
->Parent
!= NULL
)
783 if (MemoryArea
->Parent
->LeftChild
== MemoryArea
)
784 ParentReplace
= &MemoryArea
->Parent
->LeftChild
;
786 ParentReplace
= &MemoryArea
->Parent
->RightChild
;
789 ParentReplace
= (PMEMORY_AREA
*)&AddressSpace
->WorkingSetExpansionLinks
.Flink
;
791 if (MemoryArea
->RightChild
== NULL
)
793 *ParentReplace
= MemoryArea
->LeftChild
;
794 if (MemoryArea
->LeftChild
)
795 MemoryArea
->LeftChild
->Parent
= MemoryArea
->Parent
;
799 if (MemoryArea
->RightChild
->LeftChild
== NULL
)
801 MemoryArea
->RightChild
->LeftChild
= MemoryArea
->LeftChild
;
802 if (MemoryArea
->LeftChild
)
803 MemoryArea
->LeftChild
->Parent
= MemoryArea
->RightChild
;
805 *ParentReplace
= MemoryArea
->RightChild
;
806 MemoryArea
->RightChild
->Parent
= MemoryArea
->Parent
;
810 PMEMORY_AREA LowestNode
;
812 LowestNode
= MemoryArea
->RightChild
->LeftChild
;
813 while (LowestNode
->LeftChild
!= NULL
)
814 LowestNode
= LowestNode
->LeftChild
;
816 LowestNode
->Parent
->LeftChild
= LowestNode
->RightChild
;
817 if (LowestNode
->RightChild
)
818 LowestNode
->RightChild
->Parent
= LowestNode
->Parent
;
820 LowestNode
->LeftChild
= MemoryArea
->LeftChild
;
821 if (MemoryArea
->LeftChild
)
822 MemoryArea
->LeftChild
->Parent
= LowestNode
;
824 LowestNode
->RightChild
= MemoryArea
->RightChild
;
825 MemoryArea
->RightChild
->Parent
= LowestNode
;
827 *ParentReplace
= LowestNode
;
828 LowestNode
->Parent
= MemoryArea
->Parent
;
833 ExFreePoolWithTag(MemoryArea
, TAG_MAREA
);
835 DPRINT("MmFreeMemoryAreaByNode() succeeded\n");
837 return STATUS_SUCCESS
;
841 * @name MmCreateMemoryArea
843 * Create a memory area.
845 * @param AddressSpace
846 * Address space to create the area in.
848 * Type of the memory area.
850 * Base address for the memory area we're about the create. On
851 * input it contains either 0 (auto-assign address) or preferred
852 * address. On output it contains the starting address of the
853 * newly created area.
855 * Length of the area to allocate.
857 * Protection attributes for the memory area.
859 * Receives a pointer to the memory area on successful exit.
863 * @remarks Lock the address space before calling this function.
867 MmCreateMemoryArea(PMMSUPPORT AddressSpace
,
872 PMEMORY_AREA
*Result
,
873 BOOLEAN FixedAddress
,
874 ULONG AllocationFlags
,
875 PHYSICAL_ADDRESS BoundaryAddressMultiple
)
880 PMEMORY_AREA MemoryArea
;
882 DPRINT("MmCreateMemoryArea(Type %d, BaseAddress %p, "
883 "*BaseAddress %p, Length %p, AllocationFlags %x, "
884 "FixedAddress %x, Result %p)\n",
885 Type
, BaseAddress
, *BaseAddress
, Length
, AllocationFlags
,
886 FixedAddress
, Result
);
888 Granularity
= (MEMORY_AREA_VIRTUAL_MEMORY
== Type
? MM_VIRTMEM_GRANULARITY
: PAGE_SIZE
);
889 if ((*BaseAddress
) == 0 && !FixedAddress
)
891 tmpLength
= PAGE_ROUND_UP(Length
);
892 *BaseAddress
= MmFindGap(AddressSpace
,
895 (AllocationFlags
& MEM_TOP_DOWN
) == MEM_TOP_DOWN
);
896 if ((*BaseAddress
) == 0)
898 DPRINT("No suitable gap\n");
899 return STATUS_NO_MEMORY
;
904 tmpLength
= Length
+ ((ULONG_PTR
) *BaseAddress
905 - (ULONG_PTR
) MM_ROUND_DOWN(*BaseAddress
, Granularity
));
906 *BaseAddress
= MM_ROUND_DOWN(*BaseAddress
, Granularity
);
908 if (!MmGetAddressSpaceOwner(AddressSpace
) && *BaseAddress
< MmSystemRangeStart
)
910 return STATUS_ACCESS_VIOLATION
;
913 if (MmGetAddressSpaceOwner(AddressSpace
) &&
914 (ULONG_PTR
)(*BaseAddress
) + tmpLength
> (ULONG_PTR
)MmSystemRangeStart
)
916 return STATUS_ACCESS_VIOLATION
;
919 if (BoundaryAddressMultiple
.QuadPart
!= 0)
921 EndAddress
= ((char*)(*BaseAddress
)) + tmpLength
-1;
922 ASSERT(((ULONG_PTR
)*BaseAddress
/BoundaryAddressMultiple
.QuadPart
) == ((DWORD_PTR
)EndAddress
/BoundaryAddressMultiple
.QuadPart
));
925 if (MmLocateMemoryAreaByRegion(AddressSpace
,
929 DPRINT("Memory area already occupied\n");
930 return STATUS_CONFLICTING_ADDRESSES
;
935 // Is this a static memory area?
937 if (Type
& MEMORY_AREA_STATIC
)
940 // Use the static array instead of the pool
942 ASSERT(MiStaticMemoryAreaCount
< MI_STATIC_MEMORY_AREAS
);
943 MemoryArea
= &MiStaticMemoryAreas
[MiStaticMemoryAreaCount
++];
944 Type
&= ~MEMORY_AREA_STATIC
;
949 // Allocate the memory area from nonpaged pool
951 MemoryArea
= ExAllocatePoolWithTag(NonPagedPool
,
956 if (!MemoryArea
) return STATUS_NO_MEMORY
;
958 RtlZeroMemory(MemoryArea
, sizeof(MEMORY_AREA
));
959 MemoryArea
->Type
= Type
;
960 MemoryArea
->StartingAddress
= *BaseAddress
;
961 MemoryArea
->EndingAddress
= (PVOID
)((ULONG_PTR
)*BaseAddress
+ tmpLength
);
962 MemoryArea
->Protect
= Protect
;
963 MemoryArea
->Flags
= AllocationFlags
;
964 //MemoryArea->LockCount = 0;
965 MemoryArea
->PageOpCount
= 0;
966 MemoryArea
->DeleteInProgress
= FALSE
;
968 MmInsertMemoryArea(AddressSpace
, MemoryArea
);
970 *Result
= MemoryArea
;
972 DPRINT("MmCreateMemoryArea() succeeded (%p)\n", *BaseAddress
);
973 return STATUS_SUCCESS
;
977 MmMapMemoryArea(PVOID BaseAddress
,
985 for (i
= 0; i
< PAGE_ROUND_UP(Length
) / PAGE_SIZE
; i
++)
989 Status
= MmRequestPageMemoryConsumer(Consumer
, TRUE
, &Page
);
990 if (!NT_SUCCESS(Status
))
992 DPRINT1("Unable to allocate page\n");
993 KeBugCheck(MEMORY_MANAGEMENT
);
995 Status
= MmCreateVirtualMapping (NULL
,
996 (PVOID
)((ULONG_PTR
)BaseAddress
+ (i
* PAGE_SIZE
)),
1000 if (!NT_SUCCESS(Status
))
1002 DPRINT1("Unable to create virtual mapping\n");
1003 KeBugCheck(MEMORY_MANAGEMENT
);
1010 MmDeleteProcessAddressSpace(PEPROCESS Process
)
1013 PMEMORY_AREA MemoryArea
;
1015 DPRINT("MmDeleteProcessAddressSpace(Process %x (%s))\n", Process
,
1016 Process
->ImageFileName
);
1018 RemoveEntryList(&Process
->MmProcessLinks
);
1020 MmLockAddressSpace(&Process
->Vm
);
1022 while ((MemoryArea
= (PMEMORY_AREA
)Process
->Vm
.WorkingSetExpansionLinks
.Flink
) != NULL
)
1024 switch (MemoryArea
->Type
)
1026 case MEMORY_AREA_SECTION_VIEW
:
1027 Address
= (PVOID
)MemoryArea
->StartingAddress
;
1028 MmUnlockAddressSpace(&Process
->Vm
);
1029 MmUnmapViewOfSection(Process
, Address
);
1030 MmLockAddressSpace(&Process
->Vm
);
1033 case MEMORY_AREA_VIRTUAL_MEMORY
:
1034 MmFreeVirtualMemory(Process
, MemoryArea
);
1037 case MEMORY_AREA_OWNED_BY_ARM3
:
1038 MmFreeMemoryArea(&Process
->Vm
,
1045 KeBugCheck(MEMORY_MANAGEMENT
);
1049 MmUnlockAddressSpace(&Process
->Vm
);
1051 DPRINT("Finished MmReleaseMmInfo()\n");
1052 return(STATUS_SUCCESS
);