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 PMMSUPPORT AddressSpace
,
365 PMEMORY_AREA PreviousNode
;
368 /* Build a lame VAD if this is a user-space allocation */
369 if ((marea
->EndingAddress
< MmSystemRangeStart
) && (marea
->Type
!= MEMORY_AREA_OWNED_BY_ARM3
))
371 ASSERT(marea
->Type
== MEMORY_AREA_VIRTUAL_MEMORY
|| marea
->Type
== MEMORY_AREA_SECTION_VIEW
);
373 Vad
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(MMVAD
), 'Fake');
375 RtlZeroMemory(Vad
, sizeof(MMVAD
));
376 Vad
->StartingVpn
= PAGE_ROUND_DOWN(marea
->StartingAddress
) >> PAGE_SHIFT
;
378 * For some strange reason, it is perfectly valid to create a MAREA from 0x1000 to... 0x1000.
379 * In a normal OS/Memory Manager, this would be retarded, but ReactOS allows this (how it works
380 * I don't even want to know).
382 if (marea
->EndingAddress
!= marea
->StartingAddress
)
384 Vad
->EndingVpn
= PAGE_ROUND_DOWN((ULONG_PTR
)marea
->EndingAddress
- 1) >> PAGE_SHIFT
;
388 Vad
->EndingVpn
= Vad
->StartingVpn
;
390 Vad
->u
.VadFlags
.Spare
= 1;
391 Vad
->u
.VadFlags
.PrivateMemory
= 1;
392 MiInsertVad(Vad
, MmGetAddressSpaceOwner(AddressSpace
));
400 if (AddressSpace
->WorkingSetExpansionLinks
.Flink
== NULL
)
402 AddressSpace
->WorkingSetExpansionLinks
.Flink
= (PVOID
)marea
;
403 marea
->LeftChild
= marea
->RightChild
= marea
->Parent
= NULL
;
407 Node
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
410 DPRINT("marea->EndingAddress: %p Node->StartingAddress: %p\n",
411 marea
->EndingAddress
, Node
->StartingAddress
);
412 DPRINT("marea->StartingAddress: %p Node->EndingAddress: %p\n",
413 marea
->StartingAddress
, Node
->EndingAddress
);
414 ASSERT(marea
->EndingAddress
<= Node
->StartingAddress
||
415 marea
->StartingAddress
>= Node
->EndingAddress
);
416 ASSERT(marea
->StartingAddress
!= Node
->StartingAddress
);
420 if (marea
->StartingAddress
< Node
->StartingAddress
)
421 Node
= Node
->LeftChild
;
423 Node
= Node
->RightChild
;
430 MmRebalanceTree(AddressSpace
);
431 PreviousNode
= Node
->Parent
;
435 while (Node
!= NULL
);
437 marea
->LeftChild
= marea
->RightChild
= NULL
;
438 marea
->Parent
= PreviousNode
;
439 if (marea
->StartingAddress
< PreviousNode
->StartingAddress
)
440 PreviousNode
->LeftChild
= marea
;
442 PreviousNode
->RightChild
= marea
;
447 PMMSUPPORT AddressSpace
,
449 ULONG_PTR Granularity
)
451 PVOID LowestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ? MM_LOWEST_USER_ADDRESS
: MmSystemRangeStart
;
452 PVOID HighestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ?
453 (PVOID
)((ULONG_PTR
)MmSystemRangeStart
- 1) : (PVOID
)MAXULONG_PTR
;
454 PVOID AlignedAddress
;
456 PMEMORY_AREA FirstNode
;
457 PMEMORY_AREA PreviousNode
;
459 DPRINT("LowestAddress: %p HighestAddress: %p\n",
460 LowestAddress
, HighestAddress
);
462 AlignedAddress
= MM_ROUND_UP(LowestAddress
, Granularity
);
464 /* Special case for empty tree. */
465 if (AddressSpace
->WorkingSetExpansionLinks
.Flink
== NULL
)
467 if ((ULONG_PTR
)HighestAddress
- (ULONG_PTR
)AlignedAddress
>= Length
)
469 DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress
);
470 return AlignedAddress
;
472 DPRINT("MmFindGapBottomUp: 0\n");
476 /* Go to the node with lowest address in the tree. */
477 FirstNode
= Node
= MmIterateFirstNode((PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
);
479 /* Traverse the tree from left to right. */
483 Node
= MmIterateNextNode(Node
);
487 AlignedAddress
= MM_ROUND_UP(PreviousNode
->EndingAddress
, Granularity
);
488 if (Node
->StartingAddress
> AlignedAddress
&&
489 (ULONG_PTR
)Node
->StartingAddress
- (ULONG_PTR
)AlignedAddress
>= Length
)
491 DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress
);
492 return AlignedAddress
;
498 /* Check if there is enough space after the last memory area. */
499 AlignedAddress
= MM_ROUND_UP(PreviousNode
->EndingAddress
, Granularity
);
500 if ((ULONG_PTR
)HighestAddress
> (ULONG_PTR
)AlignedAddress
&&
501 (ULONG_PTR
)HighestAddress
- (ULONG_PTR
)AlignedAddress
>= Length
)
503 DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress
);
504 return AlignedAddress
;
507 /* Check if there is enough space before the first memory area. */
508 AlignedAddress
= MM_ROUND_UP(LowestAddress
, Granularity
);
509 if (FirstNode
->StartingAddress
> AlignedAddress
&&
510 (ULONG_PTR
)FirstNode
->StartingAddress
- (ULONG_PTR
)AlignedAddress
>= Length
)
512 DPRINT("MmFindGapBottomUp: %p\n", AlignedAddress
);
513 return AlignedAddress
;
516 DPRINT("MmFindGapBottomUp: 0\n");
523 PMMSUPPORT AddressSpace
,
525 ULONG_PTR Granularity
)
527 PVOID LowestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ? MM_LOWEST_USER_ADDRESS
: MmSystemRangeStart
;
528 PVOID HighestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ?
529 (PVOID
)((ULONG_PTR
)MmSystemRangeStart
- 1) : (PVOID
)MAXULONG_PTR
;
530 PVOID AlignedAddress
;
532 PMEMORY_AREA PreviousNode
;
534 DPRINT("LowestAddress: %p HighestAddress: %p\n",
535 LowestAddress
, HighestAddress
);
537 AlignedAddress
= MM_ROUND_DOWN((ULONG_PTR
)HighestAddress
- Length
+ 1, Granularity
);
539 /* Check for overflow. */
540 if (AlignedAddress
> HighestAddress
)
543 /* Special case for empty tree. */
544 if (AddressSpace
->WorkingSetExpansionLinks
.Flink
== NULL
)
546 if (AlignedAddress
>= LowestAddress
)
548 DPRINT("MmFindGapTopDown: %p\n", AlignedAddress
);
549 return AlignedAddress
;
551 DPRINT("MmFindGapTopDown: 0\n");
555 /* Go to the node with highest address in the tree. */
556 Node
= MmIterateLastNode((PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
);
558 /* Check if there is enough space after the last memory area. */
559 if (Node
->EndingAddress
<= AlignedAddress
)
561 DPRINT("MmFindGapTopDown: %p\n", AlignedAddress
);
562 return AlignedAddress
;
565 /* Traverse the tree from left to right. */
569 Node
= MmIteratePrevNode(Node
);
573 AlignedAddress
= MM_ROUND_DOWN((ULONG_PTR
)PreviousNode
->StartingAddress
- Length
+ 1, Granularity
);
575 /* Check for overflow. */
576 if (AlignedAddress
> PreviousNode
->StartingAddress
)
579 if (Node
->EndingAddress
<= AlignedAddress
)
581 DPRINT("MmFindGapTopDown: %p\n", AlignedAddress
);
582 return AlignedAddress
;
588 AlignedAddress
= MM_ROUND_DOWN((ULONG_PTR
)PreviousNode
->StartingAddress
- Length
+ 1, Granularity
);
590 /* Check for overflow. */
591 if (AlignedAddress
> PreviousNode
->StartingAddress
)
594 if (AlignedAddress
>= LowestAddress
)
596 DPRINT("MmFindGapTopDown: %p\n", AlignedAddress
);
597 return AlignedAddress
;
600 DPRINT("MmFindGapTopDown: 0\n");
607 PMMSUPPORT AddressSpace
,
609 ULONG_PTR Granularity
,
613 return MmFindGapTopDown(AddressSpace
, Length
, Granularity
);
615 return MmFindGapBottomUp(AddressSpace
, Length
, Granularity
);
620 PMMSUPPORT AddressSpace
,
623 PMEMORY_AREA Node
= (PMEMORY_AREA
)AddressSpace
->WorkingSetExpansionLinks
.Flink
;
624 PMEMORY_AREA RightNeighbour
= NULL
;
625 PVOID LowestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ? MM_LOWEST_USER_ADDRESS
: MmSystemRangeStart
;
626 PVOID HighestAddress
= MmGetAddressSpaceOwner(AddressSpace
) ?
627 (PVOID
)((ULONG_PTR
)MmSystemRangeStart
- 1) : (PVOID
)MAXULONG_PTR
;
629 Address
= MM_ROUND_DOWN(Address
, PAGE_SIZE
);
631 if (LowestAddress
< MmSystemRangeStart
)
633 if (Address
>= MmSystemRangeStart
)
640 if (Address
< LowestAddress
)
648 if (Address
< Node
->StartingAddress
)
650 RightNeighbour
= Node
;
651 Node
= Node
->LeftChild
;
653 else if (Address
>= Node
->EndingAddress
)
655 Node
= Node
->RightChild
;
659 DPRINT("MmFindGapAtAddress: 0\n");
666 DPRINT("MmFindGapAtAddress: %p [%p]\n", Address
,
667 (ULONG_PTR
)RightNeighbour
->StartingAddress
- (ULONG_PTR
)Address
);
668 return (ULONG_PTR
)RightNeighbour
->StartingAddress
- (ULONG_PTR
)Address
;
672 DPRINT("MmFindGapAtAddress: %p [%p]\n", Address
,
673 (ULONG_PTR
)HighestAddress
- (ULONG_PTR
)Address
);
674 return (ULONG_PTR
)HighestAddress
- (ULONG_PTR
)Address
;
680 MiRemoveNode(IN PMMADDRESS_NODE Node
,
681 IN PMM_AVL_TABLE Table
);
684 * @name MmFreeMemoryArea
686 * Free an existing memory area.
688 * @param AddressSpace
689 * Address space to free the area from.
691 * Memory area we're about to free.
693 * Callback function for each freed page.
694 * @param FreePageContext
695 * Context passed to the callback function.
699 * @remarks Lock the address space before calling this function.
704 PMMSUPPORT AddressSpace
,
705 PMEMORY_AREA MemoryArea
,
706 PMM_FREE_PAGE_FUNC FreePage
,
707 PVOID FreePageContext
)
709 PMEMORY_AREA
*ParentReplace
;
713 if (MemoryArea
->Type
!= MEMORY_AREA_OWNED_BY_ARM3
)
715 PEPROCESS CurrentProcess
= PsGetCurrentProcess();
716 PEPROCESS Process
= MmGetAddressSpaceOwner(AddressSpace
);
718 if (Process
!= NULL
&&
719 Process
!= CurrentProcess
)
721 KeAttachProcess(&Process
->Pcb
);
724 EndAddress
= MM_ROUND_UP(MemoryArea
->EndingAddress
, PAGE_SIZE
);
725 for (Address
= (ULONG_PTR
)MemoryArea
->StartingAddress
;
726 Address
< (ULONG_PTR
)EndAddress
;
727 Address
+= PAGE_SIZE
)
729 BOOLEAN Dirty
= FALSE
;
730 SWAPENTRY SwapEntry
= 0;
733 if (MmIsPageSwapEntry(Process
, (PVOID
)Address
))
735 MmDeletePageFileMapping(Process
, (PVOID
)Address
, &SwapEntry
);
739 MmDeleteVirtualMapping(Process
, (PVOID
)Address
, FALSE
, &Dirty
, &Page
);
741 if (FreePage
!= NULL
)
743 FreePage(FreePageContext
, MemoryArea
, (PVOID
)Address
,
744 Page
, SwapEntry
, (BOOLEAN
)Dirty
);
748 if (Process
!= NULL
&&
749 Process
!= CurrentProcess
)
756 ASSERT(MemoryArea
->EndingAddress
< MmSystemRangeStart
);
757 ASSERT(MemoryArea
->Type
== MEMORY_AREA_VIRTUAL_MEMORY
|| MemoryArea
->Type
== MEMORY_AREA_SECTION_VIEW
);
759 /* MmCleanProcessAddressSpace might have removed it (and this would be MmDeleteProcessAdressSpace) */
760 ASSERT(((PMMVAD
)MemoryArea
->Vad
)->u
.VadFlags
.Spare
!= 0);
761 if (((PMMVAD
)MemoryArea
->Vad
)->u
.VadFlags
.Spare
== 1)
763 MiRemoveNode(MemoryArea
->Vad
, &Process
->VadRoot
);
766 ExFreePool(MemoryArea
->Vad
);
767 MemoryArea
->Vad
= NULL
;
771 /* Remove the tree item. */
773 if (MemoryArea
->Parent
!= NULL
)
775 if (MemoryArea
->Parent
->LeftChild
== MemoryArea
)
776 ParentReplace
= &MemoryArea
->Parent
->LeftChild
;
778 ParentReplace
= &MemoryArea
->Parent
->RightChild
;
781 ParentReplace
= (PMEMORY_AREA
*)&AddressSpace
->WorkingSetExpansionLinks
.Flink
;
783 if (MemoryArea
->RightChild
== NULL
)
785 *ParentReplace
= MemoryArea
->LeftChild
;
786 if (MemoryArea
->LeftChild
)
787 MemoryArea
->LeftChild
->Parent
= MemoryArea
->Parent
;
791 if (MemoryArea
->RightChild
->LeftChild
== NULL
)
793 MemoryArea
->RightChild
->LeftChild
= MemoryArea
->LeftChild
;
794 if (MemoryArea
->LeftChild
)
795 MemoryArea
->LeftChild
->Parent
= MemoryArea
->RightChild
;
797 *ParentReplace
= MemoryArea
->RightChild
;
798 MemoryArea
->RightChild
->Parent
= MemoryArea
->Parent
;
802 PMEMORY_AREA LowestNode
;
804 LowestNode
= MemoryArea
->RightChild
->LeftChild
;
805 while (LowestNode
->LeftChild
!= NULL
)
806 LowestNode
= LowestNode
->LeftChild
;
808 LowestNode
->Parent
->LeftChild
= LowestNode
->RightChild
;
809 if (LowestNode
->RightChild
)
810 LowestNode
->RightChild
->Parent
= LowestNode
->Parent
;
812 LowestNode
->LeftChild
= MemoryArea
->LeftChild
;
813 if (MemoryArea
->LeftChild
)
814 MemoryArea
->LeftChild
->Parent
= LowestNode
;
816 LowestNode
->RightChild
= MemoryArea
->RightChild
;
817 MemoryArea
->RightChild
->Parent
= LowestNode
;
819 *ParentReplace
= LowestNode
;
820 LowestNode
->Parent
= MemoryArea
->Parent
;
825 ExFreePoolWithTag(MemoryArea
, TAG_MAREA
);
827 DPRINT("MmFreeMemoryAreaByNode() succeeded\n");
829 return STATUS_SUCCESS
;
833 * @name MmCreateMemoryArea
835 * Create a memory area.
837 * @param AddressSpace
838 * Address space to create the area in.
840 * Type of the memory area.
842 * Base address for the memory area we're about the create. On
843 * input it contains either 0 (auto-assign address) or preferred
844 * address. On output it contains the starting address of the
845 * newly created area.
847 * Length of the area to allocate.
849 * Protection attributes for the memory area.
851 * Receives a pointer to the memory area on successful exit.
855 * @remarks Lock the address space before calling this function.
859 MmCreateMemoryArea(PMMSUPPORT AddressSpace
,
864 PMEMORY_AREA
*Result
,
865 BOOLEAN FixedAddress
,
866 ULONG AllocationFlags
,
867 PHYSICAL_ADDRESS BoundaryAddressMultiple
)
872 PMEMORY_AREA MemoryArea
;
874 DPRINT("MmCreateMemoryArea(Type %d, BaseAddress %p, "
875 "*BaseAddress %p, Length %p, AllocationFlags %x, "
876 "FixedAddress %x, Result %p)\n",
877 Type
, BaseAddress
, *BaseAddress
, Length
, AllocationFlags
,
878 FixedAddress
, Result
);
880 Granularity
= (MEMORY_AREA_VIRTUAL_MEMORY
== Type
? MM_VIRTMEM_GRANULARITY
: PAGE_SIZE
);
881 if ((*BaseAddress
) == 0 && !FixedAddress
)
883 tmpLength
= PAGE_ROUND_UP(Length
);
884 *BaseAddress
= MmFindGap(AddressSpace
,
887 (AllocationFlags
& MEM_TOP_DOWN
) == MEM_TOP_DOWN
);
888 if ((*BaseAddress
) == 0)
890 DPRINT("No suitable gap\n");
891 return STATUS_NO_MEMORY
;
896 tmpLength
= Length
+ ((ULONG_PTR
) *BaseAddress
897 - (ULONG_PTR
) MM_ROUND_DOWN(*BaseAddress
, Granularity
));
898 *BaseAddress
= MM_ROUND_DOWN(*BaseAddress
, Granularity
);
900 if (!MmGetAddressSpaceOwner(AddressSpace
) && *BaseAddress
< MmSystemRangeStart
)
902 return STATUS_ACCESS_VIOLATION
;
905 if (MmGetAddressSpaceOwner(AddressSpace
) &&
906 (ULONG_PTR
)(*BaseAddress
) + tmpLength
> (ULONG_PTR
)MmSystemRangeStart
)
908 return STATUS_ACCESS_VIOLATION
;
911 if (BoundaryAddressMultiple
.QuadPart
!= 0)
913 EndAddress
= ((char*)(*BaseAddress
)) + tmpLength
-1;
914 ASSERT(((ULONG_PTR
)*BaseAddress
/BoundaryAddressMultiple
.QuadPart
) == ((DWORD_PTR
)EndAddress
/BoundaryAddressMultiple
.QuadPart
));
917 if (MmLocateMemoryAreaByRegion(AddressSpace
,
921 DPRINT("Memory area already occupied\n");
922 return STATUS_CONFLICTING_ADDRESSES
;
927 // Is this a static memory area?
929 if (Type
& MEMORY_AREA_STATIC
)
932 // Use the static array instead of the pool
934 ASSERT(MiStaticMemoryAreaCount
< MI_STATIC_MEMORY_AREAS
);
935 MemoryArea
= &MiStaticMemoryAreas
[MiStaticMemoryAreaCount
++];
936 Type
&= ~MEMORY_AREA_STATIC
;
941 // Allocate the memory area from nonpaged pool
943 MemoryArea
= ExAllocatePoolWithTag(NonPagedPool
,
948 if (!MemoryArea
) return STATUS_NO_MEMORY
;
950 RtlZeroMemory(MemoryArea
, sizeof(MEMORY_AREA
));
951 MemoryArea
->Type
= Type
;
952 MemoryArea
->StartingAddress
= *BaseAddress
;
953 MemoryArea
->EndingAddress
= (PVOID
)((ULONG_PTR
)*BaseAddress
+ tmpLength
);
954 MemoryArea
->Protect
= Protect
;
955 MemoryArea
->Flags
= AllocationFlags
;
956 //MemoryArea->LockCount = 0;
957 MemoryArea
->PageOpCount
= 0;
958 MemoryArea
->DeleteInProgress
= FALSE
;
960 MmInsertMemoryArea(AddressSpace
, MemoryArea
);
962 *Result
= MemoryArea
;
964 DPRINT("MmCreateMemoryArea() succeeded (%p)\n", *BaseAddress
);
965 return STATUS_SUCCESS
;
969 MmMapMemoryArea(PVOID BaseAddress
,
977 for (i
= 0; i
< PAGE_ROUND_UP(Length
) / PAGE_SIZE
; i
++)
981 Status
= MmRequestPageMemoryConsumer(Consumer
, TRUE
, &Page
);
982 if (!NT_SUCCESS(Status
))
984 DPRINT1("Unable to allocate page\n");
985 KeBugCheck(MEMORY_MANAGEMENT
);
987 Status
= MmCreateVirtualMapping (NULL
,
988 (PVOID
)((ULONG_PTR
)BaseAddress
+ (i
* PAGE_SIZE
)),
992 if (!NT_SUCCESS(Status
))
994 DPRINT1("Unable to create virtual mapping\n");
995 KeBugCheck(MEMORY_MANAGEMENT
);
1002 MmDeleteProcessAddressSpace(PEPROCESS Process
)
1005 PMEMORY_AREA MemoryArea
;
1007 DPRINT("MmDeleteProcessAddressSpace(Process %x (%s))\n", Process
,
1008 Process
->ImageFileName
);
1010 RemoveEntryList(&Process
->MmProcessLinks
);
1012 MmLockAddressSpace(&Process
->Vm
);
1014 while ((MemoryArea
= (PMEMORY_AREA
)Process
->Vm
.WorkingSetExpansionLinks
.Flink
) != NULL
)
1016 switch (MemoryArea
->Type
)
1018 case MEMORY_AREA_SECTION_VIEW
:
1019 Address
= (PVOID
)MemoryArea
->StartingAddress
;
1020 MmUnlockAddressSpace(&Process
->Vm
);
1021 MmUnmapViewOfSection(Process
, Address
);
1022 MmLockAddressSpace(&Process
->Vm
);
1025 case MEMORY_AREA_VIRTUAL_MEMORY
:
1026 MmFreeVirtualMemory(Process
, MemoryArea
);
1029 case MEMORY_AREA_OWNED_BY_ARM3
:
1030 MmFreeMemoryArea(&Process
->Vm
,
1037 KeBugCheck(MEMORY_MANAGEMENT
);
1041 MmUnlockAddressSpace(&Process
->Vm
);
1043 DPRINT("Finished MmReleaseMmInfo()\n");
1044 return(STATUS_SUCCESS
);