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
))
377 ASSERT(marea
->Type
== MEMORY_AREA_VIRTUAL_MEMORY
|| marea
->Type
== MEMORY_AREA_SECTION_VIEW
);
379 Vad
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(MMVAD
), 'Fake');
381 RtlZeroMemory(Vad
, sizeof(MMVAD
));
382 Vad
->StartingVpn
= PAGE_ROUND_DOWN(marea
->StartingAddress
) >> PAGE_SHIFT
;
384 * For some strange reason, it is perfectly valid to create a MAREA from 0x1000 to... 0x1000.
385 * In a normal OS/Memory Manager, this would be retarded, but ReactOS allows this (how it works
386 * I don't even want to know).
388 if (marea
->EndingAddress
!= marea
->StartingAddress
)
390 Vad
->EndingVpn
= PAGE_ROUND_DOWN((ULONG_PTR
)marea
->EndingAddress
- 1) >> PAGE_SHIFT
;
394 Vad
->EndingVpn
= Vad
->StartingVpn
;
396 Vad
->u
.VadFlags
.Spare
= 1;
397 Vad
->u
.VadFlags
.PrivateMemory
= 1;
398 Vad
->u
.VadFlags
.Protection
= MiMakeProtectionMask(marea
->Protect
);
399 MiInsertVad(Vad
, MmGetAddressSpaceOwner(AddressSpace
));
407 if (AddressSpace
->WorkingSetExpansionLinks
.Flink
== NULL
)
409 AddressSpace
->WorkingSetExpansionLinks
.Flink
= (PVOID
)marea
;
410 marea
->LeftChild
= marea
->RightChild
= marea
->Parent
= NULL
;
414 Node
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
417 DPRINT("marea->EndingAddress: %p Node->StartingAddress: %p\n",
418 marea
->EndingAddress
, Node
->StartingAddress
);
419 DPRINT("marea->StartingAddress: %p Node->EndingAddress: %p\n",
420 marea
->StartingAddress
, Node
->EndingAddress
);
421 ASSERT(marea
->EndingAddress
<= Node
->StartingAddress
||
422 marea
->StartingAddress
>= Node
->EndingAddress
);
423 ASSERT(marea
->StartingAddress
!= Node
->StartingAddress
);
427 if (marea
->StartingAddress
< Node
->StartingAddress
)
428 Node
= Node
->LeftChild
;
430 Node
= Node
->RightChild
;
437 MmRebalanceTree(AddressSpace
);
438 PreviousNode
= Node
->Parent
;
442 while (Node
!= NULL
);
444 marea
->LeftChild
= marea
->RightChild
= NULL
;
445 marea
->Parent
= PreviousNode
;
446 if (marea
->StartingAddress
< PreviousNode
->StartingAddress
)
447 PreviousNode
->LeftChild
= marea
;
449 PreviousNode
->RightChild
= marea
;
454 PMMSUPPORT AddressSpace
,
456 ULONG_PTR Granularity
)
458 PVOID LowestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ? MM_LOWEST_USER_ADDRESS
: MmSystemRangeStart
;
459 PVOID HighestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ?
460 (PVOID
)((ULONG_PTR
)MmSystemRangeStart
- 1) : (PVOID
)MAXULONG_PTR
;
461 PVOID AlignedAddress
;
463 PMEMORY_AREA FirstNode
;
464 PMEMORY_AREA PreviousNode
;
466 DPRINT("LowestAddress: %p HighestAddress: %p\n",
467 LowestAddress
, HighestAddress
);
469 AlignedAddress
= MM_ROUND_UP(LowestAddress
, Granularity
);
471 /* Special case for empty tree. */
472 if (AddressSpace
->WorkingSetExpansionLinks
.Flink
== NULL
)
474 if ((ULONG_PTR
)HighestAddress
- (ULONG_PTR
)AlignedAddress
>= Length
)
476 DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress
);
477 return AlignedAddress
;
479 DPRINT("MmFindGapBottomUp: 0\n");
483 /* Go to the node with lowest address in the tree. */
484 FirstNode
= Node
= MmIterateFirstNode((PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
);
486 /* Traverse the tree from left to right. */
490 Node
= MmIterateNextNode(Node
);
494 AlignedAddress
= MM_ROUND_UP(PreviousNode
->EndingAddress
, Granularity
);
495 if (Node
->StartingAddress
> AlignedAddress
&&
496 (ULONG_PTR
)Node
->StartingAddress
- (ULONG_PTR
)AlignedAddress
>= Length
)
498 DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress
);
499 return AlignedAddress
;
505 /* Check if there is enough space after the last memory area. */
506 AlignedAddress
= MM_ROUND_UP(PreviousNode
->EndingAddress
, Granularity
);
507 if ((ULONG_PTR
)HighestAddress
> (ULONG_PTR
)AlignedAddress
&&
508 (ULONG_PTR
)HighestAddress
- (ULONG_PTR
)AlignedAddress
>= Length
)
510 DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress
);
511 return AlignedAddress
;
514 /* Check if there is enough space before the first memory area. */
515 AlignedAddress
= MM_ROUND_UP(LowestAddress
, Granularity
);
516 if (FirstNode
->StartingAddress
> AlignedAddress
&&
517 (ULONG_PTR
)FirstNode
->StartingAddress
- (ULONG_PTR
)AlignedAddress
>= Length
)
519 DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress
);
520 return AlignedAddress
;
523 DPRINT("MmFindGapBottomUp: 0\n");
530 PMMSUPPORT AddressSpace
,
532 ULONG_PTR Granularity
)
534 PVOID LowestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ? MM_LOWEST_USER_ADDRESS
: MmSystemRangeStart
;
535 PVOID HighestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ?
536 (PVOID
)((ULONG_PTR
)MmSystemRangeStart
- 1) : (PVOID
)MAXULONG_PTR
;
537 PVOID AlignedAddress
;
539 PMEMORY_AREA PreviousNode
;
541 DPRINT("LowestAddress: %p HighestAddress: %p\n",
542 LowestAddress
, HighestAddress
);
544 AlignedAddress
= MM_ROUND_DOWN((ULONG_PTR
)HighestAddress
- Length
+ 1, Granularity
);
546 /* Check for overflow. */
547 if (AlignedAddress
> HighestAddress
)
550 /* Special case for empty tree. */
551 if (AddressSpace
->WorkingSetExpansionLinks
.Flink
== NULL
)
553 if (AlignedAddress
>= LowestAddress
)
555 DPRINT("MmFindGapTopDown: %p\n", AlignedAddress
);
556 return AlignedAddress
;
558 DPRINT("MmFindGapTopDown: 0\n");
562 /* Go to the node with highest address in the tree. */
563 Node
= MmIterateLastNode((PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
);
565 /* Check if there is enough space after the last memory area. */
566 if (Node
->EndingAddress
<= AlignedAddress
)
568 DPRINT("MmFindGapTopDown: %p\n", AlignedAddress
);
569 return AlignedAddress
;
572 /* Traverse the tree from left to right. */
576 Node
= MmIteratePrevNode(Node
);
580 AlignedAddress
= MM_ROUND_DOWN((ULONG_PTR
)PreviousNode
->StartingAddress
- Length
+ 1, Granularity
);
582 /* Check for overflow. */
583 if (AlignedAddress
> PreviousNode
->StartingAddress
)
586 if (Node
->EndingAddress
<= AlignedAddress
)
588 DPRINT("MmFindGapTopDown: %p\n", AlignedAddress
);
589 return AlignedAddress
;
595 AlignedAddress
= MM_ROUND_DOWN((ULONG_PTR
)PreviousNode
->StartingAddress
- Length
+ 1, Granularity
);
597 /* Check for overflow. */
598 if (AlignedAddress
> PreviousNode
->StartingAddress
)
601 if (AlignedAddress
>= LowestAddress
)
603 DPRINT("MmFindGapTopDown: %p\n", AlignedAddress
);
604 return AlignedAddress
;
607 DPRINT("MmFindGapTopDown: 0\n");
614 PMMSUPPORT AddressSpace
,
616 ULONG_PTR Granularity
,
620 return MmFindGapTopDown(AddressSpace
, Length
, Granularity
);
622 return MmFindGapBottomUp(AddressSpace
, Length
, Granularity
);
627 PMMSUPPORT AddressSpace
,
630 PMEMORY_AREA Node
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
631 PMEMORY_AREA RightNeighbour
= NULL
;
632 PVOID LowestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ? MM_LOWEST_USER_ADDRESS
: MmSystemRangeStart
;
633 PVOID HighestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ?
634 (PVOID
)((ULONG_PTR
)MmSystemRangeStart
- 1) : (PVOID
)MAXULONG_PTR
;
636 Address
= MM_ROUND_DOWN(Address
, PAGE_SIZE
);
638 if (LowestAddress
< MmSystemRangeStart
)
640 if (Address
>= MmSystemRangeStart
)
647 if (Address
< LowestAddress
)
655 if (Address
< Node
->StartingAddress
)
657 RightNeighbour
= Node
;
658 Node
= Node
->LeftChild
;
660 else if (Address
>= Node
->EndingAddress
)
662 Node
= Node
->RightChild
;
666 DPRINT("MmFindGapAtAddress: 0\n");
673 DPRINT("MmFindGapAtAddress: %p [%p]\n", Address
,
674 (ULONG_PTR
)RightNeighbour
->StartingAddress
- (ULONG_PTR
)Address
);
675 return (ULONG_PTR
)RightNeighbour
->StartingAddress
- (ULONG_PTR
)Address
;
679 DPRINT("MmFindGapAtAddress: %p [%p]\n", Address
,
680 (ULONG_PTR
)HighestAddress
- (ULONG_PTR
)Address
);
681 return (ULONG_PTR
)HighestAddress
- (ULONG_PTR
)Address
;
687 MiRemoveNode(IN PMMADDRESS_NODE Node
,
688 IN PMM_AVL_TABLE Table
);
691 * @name MmFreeMemoryArea
693 * Free an existing memory area.
695 * @param AddressSpace
696 * Address space to free the area from.
698 * Memory area we're about to free.
700 * Callback function for each freed page.
701 * @param FreePageContext
702 * Context passed to the callback function.
706 * @remarks Lock the address space before calling this function.
711 PMMSUPPORT AddressSpace
,
712 PMEMORY_AREA MemoryArea
,
713 PMM_FREE_PAGE_FUNC FreePage
,
714 PVOID FreePageContext
)
716 PMEMORY_AREA
*ParentReplace
;
720 if (MemoryArea
->Type
!= MEMORY_AREA_OWNED_BY_ARM3
)
722 PEPROCESS CurrentProcess
= PsGetCurrentProcess();
723 PEPROCESS Process
= MmGetAddressSpaceOwner(AddressSpace
);
725 if (Process
!= NULL
&&
726 Process
!= CurrentProcess
)
728 KeAttachProcess(&Process
->Pcb
);
731 EndAddress
= MM_ROUND_UP(MemoryArea
->EndingAddress
, PAGE_SIZE
);
732 for (Address
= (ULONG_PTR
)MemoryArea
->StartingAddress
;
733 Address
< (ULONG_PTR
)EndAddress
;
734 Address
+= PAGE_SIZE
)
736 BOOLEAN Dirty
= FALSE
;
737 SWAPENTRY SwapEntry
= 0;
740 if (MmIsPageSwapEntry(Process
, (PVOID
)Address
))
742 MmDeletePageFileMapping(Process
, (PVOID
)Address
, &SwapEntry
);
746 MmDeleteVirtualMapping(Process
, (PVOID
)Address
, FALSE
, &Dirty
, &Page
);
748 if (FreePage
!= NULL
)
750 FreePage(FreePageContext
, MemoryArea
, (PVOID
)Address
,
751 Page
, SwapEntry
, (BOOLEAN
)Dirty
);
755 if (Process
!= NULL
&&
756 Process
!= CurrentProcess
)
763 ASSERT(MemoryArea
->EndingAddress
< MmSystemRangeStart
);
764 ASSERT(MemoryArea
->Type
== MEMORY_AREA_VIRTUAL_MEMORY
|| MemoryArea
->Type
== MEMORY_AREA_SECTION_VIEW
);
766 /* MmCleanProcessAddressSpace might have removed it (and this would be MmDeleteProcessAdressSpace) */
767 ASSERT(((PMMVAD
)MemoryArea
->Vad
)->u
.VadFlags
.Spare
!= 0);
768 if (((PMMVAD
)MemoryArea
->Vad
)->u
.VadFlags
.Spare
== 1)
770 MiRemoveNode(MemoryArea
->Vad
, &Process
->VadRoot
);
773 ExFreePool(MemoryArea
->Vad
);
774 MemoryArea
->Vad
= NULL
;
778 /* Remove the tree item. */
780 if (MemoryArea
->Parent
!= NULL
)
782 if (MemoryArea
->Parent
->LeftChild
== MemoryArea
)
783 ParentReplace
= &MemoryArea
->Parent
->LeftChild
;
785 ParentReplace
= &MemoryArea
->Parent
->RightChild
;
788 ParentReplace
= (PMEMORY_AREA
*)&AddressSpace
->WorkingSetExpansionLinks
.Flink
;
790 if (MemoryArea
->RightChild
== NULL
)
792 *ParentReplace
= MemoryArea
->LeftChild
;
793 if (MemoryArea
->LeftChild
)
794 MemoryArea
->LeftChild
->Parent
= MemoryArea
->Parent
;
798 if (MemoryArea
->RightChild
->LeftChild
== NULL
)
800 MemoryArea
->RightChild
->LeftChild
= MemoryArea
->LeftChild
;
801 if (MemoryArea
->LeftChild
)
802 MemoryArea
->LeftChild
->Parent
= MemoryArea
->RightChild
;
804 *ParentReplace
= MemoryArea
->RightChild
;
805 MemoryArea
->RightChild
->Parent
= MemoryArea
->Parent
;
809 PMEMORY_AREA LowestNode
;
811 LowestNode
= MemoryArea
->RightChild
->LeftChild
;
812 while (LowestNode
->LeftChild
!= NULL
)
813 LowestNode
= LowestNode
->LeftChild
;
815 LowestNode
->Parent
->LeftChild
= LowestNode
->RightChild
;
816 if (LowestNode
->RightChild
)
817 LowestNode
->RightChild
->Parent
= LowestNode
->Parent
;
819 LowestNode
->LeftChild
= MemoryArea
->LeftChild
;
820 if (MemoryArea
->LeftChild
)
821 MemoryArea
->LeftChild
->Parent
= LowestNode
;
823 LowestNode
->RightChild
= MemoryArea
->RightChild
;
824 MemoryArea
->RightChild
->Parent
= LowestNode
;
826 *ParentReplace
= LowestNode
;
827 LowestNode
->Parent
= MemoryArea
->Parent
;
832 ExFreePoolWithTag(MemoryArea
, TAG_MAREA
);
834 DPRINT("MmFreeMemoryAreaByNode() succeeded\n");
836 return STATUS_SUCCESS
;
840 * @name MmCreateMemoryArea
842 * Create a memory area.
844 * @param AddressSpace
845 * Address space to create the area in.
847 * Type of the memory area.
849 * Base address for the memory area we're about the create. On
850 * input it contains either 0 (auto-assign address) or preferred
851 * address. On output it contains the starting address of the
852 * newly created area.
854 * Length of the area to allocate.
856 * Protection attributes for the memory area.
858 * Receives a pointer to the memory area on successful exit.
862 * @remarks Lock the address space before calling this function.
866 MmCreateMemoryArea(PMMSUPPORT AddressSpace
,
871 PMEMORY_AREA
*Result
,
872 BOOLEAN FixedAddress
,
873 ULONG AllocationFlags
,
874 PHYSICAL_ADDRESS BoundaryAddressMultiple
)
879 PMEMORY_AREA MemoryArea
;
881 DPRINT("MmCreateMemoryArea(Type %d, BaseAddress %p, "
882 "*BaseAddress %p, Length %p, AllocationFlags %x, "
883 "FixedAddress %x, Result %p)\n",
884 Type
, BaseAddress
, *BaseAddress
, Length
, AllocationFlags
,
885 FixedAddress
, Result
);
887 Granularity
= (MEMORY_AREA_VIRTUAL_MEMORY
== Type
? MM_VIRTMEM_GRANULARITY
: PAGE_SIZE
);
888 if ((*BaseAddress
) == 0 && !FixedAddress
)
890 tmpLength
= PAGE_ROUND_UP(Length
);
891 *BaseAddress
= MmFindGap(AddressSpace
,
894 (AllocationFlags
& MEM_TOP_DOWN
) == MEM_TOP_DOWN
);
895 if ((*BaseAddress
) == 0)
897 DPRINT("No suitable gap\n");
898 return STATUS_NO_MEMORY
;
903 tmpLength
= Length
+ ((ULONG_PTR
) *BaseAddress
904 - (ULONG_PTR
) MM_ROUND_DOWN(*BaseAddress
, Granularity
));
905 *BaseAddress
= MM_ROUND_DOWN(*BaseAddress
, Granularity
);
907 if (!MmGetAddressSpaceOwner(AddressSpace
) && *BaseAddress
< MmSystemRangeStart
)
909 return STATUS_ACCESS_VIOLATION
;
912 if (MmGetAddressSpaceOwner(AddressSpace
) &&
913 (ULONG_PTR
)(*BaseAddress
) + tmpLength
> (ULONG_PTR
)MmSystemRangeStart
)
915 return STATUS_ACCESS_VIOLATION
;
918 if (BoundaryAddressMultiple
.QuadPart
!= 0)
920 EndAddress
= ((char*)(*BaseAddress
)) + tmpLength
-1;
921 ASSERT(((ULONG_PTR
)*BaseAddress
/BoundaryAddressMultiple
.QuadPart
) == ((DWORD_PTR
)EndAddress
/BoundaryAddressMultiple
.QuadPart
));
924 if (MmLocateMemoryAreaByRegion(AddressSpace
,
928 DPRINT("Memory area already occupied\n");
929 return STATUS_CONFLICTING_ADDRESSES
;
934 // Is this a static memory area?
936 if (Type
& MEMORY_AREA_STATIC
)
939 // Use the static array instead of the pool
941 ASSERT(MiStaticMemoryAreaCount
< MI_STATIC_MEMORY_AREAS
);
942 MemoryArea
= &MiStaticMemoryAreas
[MiStaticMemoryAreaCount
++];
943 Type
&= ~MEMORY_AREA_STATIC
;
948 // Allocate the memory area from nonpaged pool
950 MemoryArea
= ExAllocatePoolWithTag(NonPagedPool
,
955 if (!MemoryArea
) return STATUS_NO_MEMORY
;
957 RtlZeroMemory(MemoryArea
, sizeof(MEMORY_AREA
));
958 MemoryArea
->Type
= Type
;
959 MemoryArea
->StartingAddress
= *BaseAddress
;
960 MemoryArea
->EndingAddress
= (PVOID
)((ULONG_PTR
)*BaseAddress
+ tmpLength
);
961 MemoryArea
->Protect
= Protect
;
962 MemoryArea
->Flags
= AllocationFlags
;
963 //MemoryArea->LockCount = 0;
964 MemoryArea
->PageOpCount
= 0;
965 MemoryArea
->DeleteInProgress
= FALSE
;
967 MmInsertMemoryArea(AddressSpace
, MemoryArea
);
969 *Result
= MemoryArea
;
971 DPRINT("MmCreateMemoryArea() succeeded (%p)\n", *BaseAddress
);
972 return STATUS_SUCCESS
;
976 MmMapMemoryArea(PVOID BaseAddress
,
984 for (i
= 0; i
< PAGE_ROUND_UP(Length
) / PAGE_SIZE
; i
++)
988 Status
= MmRequestPageMemoryConsumer(Consumer
, TRUE
, &Page
);
989 if (!NT_SUCCESS(Status
))
991 DPRINT1("Unable to allocate page\n");
992 KeBugCheck(MEMORY_MANAGEMENT
);
994 Status
= MmCreateVirtualMapping (NULL
,
995 (PVOID
)((ULONG_PTR
)BaseAddress
+ (i
* PAGE_SIZE
)),
999 if (!NT_SUCCESS(Status
))
1001 DPRINT1("Unable to create virtual mapping\n");
1002 KeBugCheck(MEMORY_MANAGEMENT
);
1009 MmDeleteProcessAddressSpace(PEPROCESS Process
)
1012 PMEMORY_AREA MemoryArea
;
1014 DPRINT("MmDeleteProcessAddressSpace(Process %x (%s))\n", Process
,
1015 Process
->ImageFileName
);
1017 RemoveEntryList(&Process
->MmProcessLinks
);
1019 MmLockAddressSpace(&Process
->Vm
);
1021 while ((MemoryArea
= (PMEMORY_AREA
)Process
->Vm
.WorkingSetExpansionLinks
.Flink
) != NULL
)
1023 switch (MemoryArea
->Type
)
1025 case MEMORY_AREA_SECTION_VIEW
:
1026 Address
= (PVOID
)MemoryArea
->StartingAddress
;
1027 MmUnlockAddressSpace(&Process
->Vm
);
1028 MmUnmapViewOfSection(Process
, Address
);
1029 MmLockAddressSpace(&Process
->Vm
);
1032 case MEMORY_AREA_VIRTUAL_MEMORY
:
1033 MmFreeVirtualMemory(Process
, MemoryArea
);
1036 case MEMORY_AREA_OWNED_BY_ARM3
:
1037 MmFreeMemoryArea(&Process
->Vm
,
1044 KeBugCheck(MEMORY_MANAGEMENT
);
1048 MmUnlockAddressSpace(&Process
->Vm
);
1050 DPRINT("Finished MmReleaseMmInfo()\n");
1051 return(STATUS_SUCCESS
);