2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/vadnode.c
5 * PURPOSE: ARM Memory Manager VAD Node Algorithms
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 * Timo Kreuzer (timo.kreuzer@reactos.org)
10 /* INCLUDES *******************************************************************/
16 #line 15 "ARMĀ³::VADNODE"
17 #define MODULE_INVOLVED_IN_ARM3
18 #include "../ARM3/miarm.h"
20 /* Include Mm version of AVL support */
21 #include "../ARM3/miavl.h"
22 #include "../../../lib/rtl/avlsupp.c"
24 /* FUNCTIONS ******************************************************************/
28 MiLocateAddress(IN PVOID VirtualAddress
)
32 PMM_AVL_TABLE Table
= &PsGetCurrentProcess()->VadRoot
;
33 TABLE_SEARCH_RESULT SearchResult
;
35 /* Start with the the hint */
36 FoundVad
= (PMMVAD
)Table
->NodeHint
;
37 if (!FoundVad
) return NULL
;
39 /* Check if this VPN is in the hint, if so, use it */
40 Vpn
= (ULONG_PTR
)VirtualAddress
>> PAGE_SHIFT
;
41 if ((Vpn
>= FoundVad
->StartingVpn
) && (Vpn
<= FoundVad
->EndingVpn
)) return FoundVad
;
43 /* VAD hint didn't work, go look for it */
44 SearchResult
= RtlpFindAvlTableNodeOrParent(Table
,
46 (PMMADDRESS_NODE
*)&FoundVad
);
47 if (SearchResult
!= TableFoundNode
) return NULL
;
49 /* We found it, update the hint */
50 ASSERT(FoundVad
!= NULL
);
51 ASSERT((Vpn
>= FoundVad
->StartingVpn
) && (Vpn
<= FoundVad
->EndingVpn
));
52 Table
->NodeHint
= FoundVad
;
58 MiCheckForConflictingNode(IN ULONG_PTR StartVpn
,
60 IN PMM_AVL_TABLE Table
)
62 PMMADDRESS_NODE CurrentNode
;
64 /* If the tree is empty, there is no conflict */
65 if (!Table
->NumberGenericTableElements
) return NULL
;
67 /* Start looping from the right */
68 CurrentNode
= RtlRightChildAvl(&Table
->BalancedRoot
);
69 ASSERT(CurrentNode
!= NULL
);
72 /* This address comes after */
73 if (StartVpn
> CurrentNode
->EndingVpn
)
75 /* Keep searching on the right */
76 CurrentNode
= RtlRightChildAvl(CurrentNode
);
78 else if (EndVpn
< CurrentNode
->StartingVpn
)
80 /* This address ends before the node starts, search on the left */
81 CurrentNode
= RtlLeftChildAvl(CurrentNode
);
85 /* This address is part of this node, return it */
90 /* Return either the conflicting node, or no node at all */
96 MiInsertNode(IN PMM_AVL_TABLE Table
,
97 IN PMMADDRESS_NODE NewNode
,
98 IN PMMADDRESS_NODE Parent
,
99 IN TABLE_SEARCH_RESULT Result
)
103 /* Insert it into the tree */
104 RtlpInsertAvlTreeNode(Table
, NewNode
, Parent
, Result
);
106 /* Now insert an ARM3 MEMORY_AREA for this node, unless the insert was already from the MEMORY_AREA code */
107 Vad
= (PMMVAD
)NewNode
;
108 if (Vad
->u
.VadFlags
.Spare
== 0)
111 PMEMORY_AREA MemoryArea
;
112 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
114 PEPROCESS Process
= CONTAINING_RECORD(Table
, EPROCESS
, VadRoot
);
115 PVOID AllocatedBase
= (PVOID
)(Vad
->StartingVpn
<< PAGE_SHIFT
);
116 BoundaryAddressMultiple
.QuadPart
= 0;
117 Size
= ((Vad
->EndingVpn
+ 1) - Vad
->StartingVpn
) << PAGE_SHIFT
;
118 Status
= MmCreateMemoryArea(&Process
->Vm
,
119 MEMORY_AREA_OWNED_BY_ARM3
,
126 BoundaryAddressMultiple
);
127 ASSERT(NT_SUCCESS(Status
));
129 /* Check if this is VM VAD */
130 if (Vad
->ControlArea
== NULL
)
132 /* We store the reactos MEMORY_AREA here */
133 DPRINT("Storing %p in %p\n", MemoryArea
, Vad
);
134 Vad
->FirstPrototypePte
= (PMMPTE
)MemoryArea
;
138 /* This is a section VAD. Store the MAREA here for now */
139 DPRINT("Storing %p in %p\n", MemoryArea
, Vad
);
140 Vad
->ControlArea
->WaitingForDeletion
= (PVOID
)MemoryArea
;
147 MiInsertVad(IN PMMVAD Vad
,
148 IN PEPROCESS Process
)
150 TABLE_SEARCH_RESULT Result
;
151 PMMADDRESS_NODE Parent
= NULL
;
153 /* Validate the VAD and set it as the current hint */
154 ASSERT(Vad
->EndingVpn
>= Vad
->StartingVpn
);
155 Process
->VadRoot
.NodeHint
= Vad
;
157 /* Find the parent VAD and where this child should be inserted */
158 Result
= RtlpFindAvlTableNodeOrParent(&Process
->VadRoot
, (PVOID
)Vad
->StartingVpn
, &Parent
);
159 ASSERT(Result
!= TableFoundNode
);
160 ASSERT((Parent
!= NULL
) || (Result
== TableEmptyTree
));
162 /* Do the actual insert operation */
163 MiInsertNode(&Process
->VadRoot
, (PVOID
)Vad
, Parent
, Result
);
168 MiRemoveNode(IN PMMADDRESS_NODE Node
,
169 IN PMM_AVL_TABLE Table
)
173 /* Call the AVL code */
174 RtlpDeleteAvlTreeNode(Table
, Node
);
176 /* Decrease element count */
177 Table
->NumberGenericTableElements
--;
179 /* Check if this node was the hint */
180 if (Table
->NodeHint
== Node
)
182 /* Get a new hint, unless we're empty now, in which case nothing */
183 if (!Table
->NumberGenericTableElements
) Table
->NodeHint
= NULL
;
184 else Table
->NodeHint
= Table
->BalancedRoot
.RightChild
;
187 /* Free the node from ReactOS view as well */
189 if (Vad
->u
.VadFlags
.Spare
== 0)
191 PMEMORY_AREA MemoryArea
;
194 /* Check if this is VM VAD */
195 if (Vad
->ControlArea
== NULL
)
197 /* We store the ReactOS MEMORY_AREA here */
198 MemoryArea
= (PMEMORY_AREA
)Vad
->FirstPrototypePte
;
202 /* This is a section VAD. We store the ReactOS MEMORY_AREA here */
203 MemoryArea
= (PMEMORY_AREA
)Vad
->ControlArea
->WaitingForDeletion
;
206 /* Make sure one actually still exists */
209 /* Get the process */
210 Process
= CONTAINING_RECORD(Table
, EPROCESS
, VadRoot
);
212 /* We only create fake memory-areas for ARM3 VADs */
213 ASSERT(MemoryArea
->Type
== MEMORY_AREA_OWNED_BY_ARM3
);
214 ASSERT(MemoryArea
->Vad
== NULL
);
217 MmFreeMemoryArea(&Process
->Vm
, MemoryArea
, NULL
, NULL
);
224 MiGetPreviousNode(IN PMMADDRESS_NODE Node
)
226 PMMADDRESS_NODE Parent
;
228 /* Get the left child */
229 if (RtlLeftChildAvl(Node
))
231 /* Get right-most child */
232 Node
= RtlLeftChildAvl(Node
);
233 while (RtlRightChildAvl(Node
)) Node
= RtlRightChildAvl(Node
);
237 Parent
= RtlParentAvl(Node
);
238 ASSERT(Parent
!= NULL
);
239 while (Parent
!= Node
)
241 /* The parent should be a right child, return the real predecessor */
242 if (RtlIsRightChildAvl(Node
))
244 /* Return it unless it's the root */
245 if (Parent
== RtlParentAvl(Parent
)) Parent
= NULL
;
249 /* Keep lopping until we find our parent */
251 Parent
= RtlParentAvl(Node
);
260 MiGetNextNode(IN PMMADDRESS_NODE Node
)
262 PMMADDRESS_NODE Parent
;
264 /* Get the right child */
265 if (RtlRightChildAvl(Node
))
267 /* Get left-most child */
268 Node
= RtlRightChildAvl(Node
);
269 while (RtlLeftChildAvl(Node
)) Node
= RtlLeftChildAvl(Node
);
273 Parent
= RtlParentAvl(Node
);
274 ASSERT(Parent
!= NULL
);
275 while (Parent
!= Node
)
277 /* The parent should be a left child, return the real predecessor */
278 if (RtlIsLeftChildAvl(Node
))
284 /* Keep lopping until we find our parent */
286 Parent
= RtlParentAvl(Node
);
295 MiFindEmptyAddressRangeInTree(IN SIZE_T Length
,
296 IN ULONG_PTR Alignment
,
297 IN PMM_AVL_TABLE Table
,
298 OUT PMMADDRESS_NODE
*PreviousVad
,
301 PMMADDRESS_NODE Node
;
302 PMMADDRESS_NODE NextNode
;
303 ULONG_PTR StartingVpn
, HighestVpn
, AlignmentVpn
, LengthVpn
, LowVpn
;
306 /* Precompute page numbers for the length, alignment, and starting address */
307 LengthVpn
= (Length
+ (PAGE_SIZE
- 1)) >> PAGE_SHIFT
;
308 AlignmentVpn
= Alignment
>> PAGE_SHIFT
;
309 StartingVpn
= ROUND_UP((ULONG_PTR
)MM_LOWEST_USER_ADDRESS
>> PAGE_SHIFT
,
312 /* Check if the table is free, so the lowest possible address is available */
313 if (!Table
->NumberGenericTableElements
) goto FoundAtBottom
;
315 /* Otherwise, follow the leftmost child of the right root node's child */
316 Node
= RtlRightChildAvl(&Table
->BalancedRoot
);
317 while (RtlLeftChildAvl(Node
)) Node
= RtlLeftChildAvl(Node
);
319 /* This is the node for the remaining gap at the bottom, can it be used? */
320 if ((Node
->StartingVpn
> StartingVpn
) &&
321 (LengthVpn
< Node
->StartingVpn
- StartingVpn
))
324 /* Use this VAD to store the allocation */
326 *Base
= StartingVpn
<< PAGE_SHIFT
;
327 return STATUS_SUCCESS
;
330 /* Otherwise, we start a search to find a gap */
333 /* The last aligned page number in this entry */
334 LowVpn
= ROUND_UP(Node
->EndingVpn
+ 1, AlignmentVpn
);
336 /* Keep going as long as there's still a next node */
337 NextNode
= MiGetNextNode(Node
);
338 if (!NextNode
) break;
340 /* Can this allocation fit in this node? */
341 if ((LengthVpn
<= (NextNode
->StartingVpn
- LowVpn
)) &&
342 (NextNode
->StartingVpn
> LowVpn
))
345 /* Yes! Use this VAD to store the allocation */
347 *Base
= ROUND_UP((Node
->EndingVpn
<< PAGE_SHIFT
) | (PAGE_SIZE
- 1),
349 return STATUS_SUCCESS
;
352 /* Try the next node */
356 /* We're down to the last (top) VAD, will this allocation fit inside it? */
357 HighestVpn
= ((ULONG_PTR
)MM_HIGHEST_VAD_ADDRESS
+ 1) >> PAGE_SHIFT
;
358 if ((HighestVpn
> LowVpn
) && (LengthVpn
<= HighestVpn
- LowVpn
)) goto Found
;
360 /* Nyet, there's no free address space for this allocation, so we'll fail */
361 return STATUS_NO_MEMORY
;
366 MiFindEmptyAddressRangeDownTree(IN SIZE_T Length
,
367 IN ULONG_PTR BoundaryAddress
,
368 IN ULONG_PTR Alignment
,
369 IN PMM_AVL_TABLE Table
,
371 OUT PMMADDRESS_NODE
*Parent
)
373 PMMADDRESS_NODE Node
, LowestNode
, Child
;
374 ULONG LowVpn
, HighVpn
;
375 PFN_NUMBER PageCount
;
378 ASSERT(BoundaryAddress
);
379 ASSERT(BoundaryAddress
<= ((ULONG_PTR
)MM_HIGHEST_VAD_ADDRESS
+ 1));
381 /* Compute page length, make sure the boundary address is valid */
382 Length
= ROUND_TO_PAGES(Length
);
383 PageCount
= Length
>> PAGE_SHIFT
;
384 if ((BoundaryAddress
+ 1) < Length
) return STATUS_NO_MEMORY
;
386 /* Check if the table is empty */
387 if (Table
->NumberGenericTableElements
== 0)
389 /* Tree is empty, the candidate address is already the best one */
390 *Base
= ROUND_UP(BoundaryAddress
+ 1 - Length
, Alignment
);
391 return TableEmptyTree
;
394 /* Calculate the initial upper margin */
395 HighVpn
= BoundaryAddress
>> PAGE_SHIFT
;
397 /* Starting from the root, go down until the right-most child,
398 trying to stay below the boundary. */
399 LowestNode
= Node
= RtlRightChildAvl(&Table
->BalancedRoot
);
400 while ( (Child
= RtlRightChildAvl(Node
)) &&
401 Child
->EndingVpn
< HighVpn
) Node
= Child
;
403 /* Now loop the Vad nodes */
406 /* Keep track of the lowest node */
409 /* Calculate the lower margin */
410 LowVpn
= ROUND_UP(Node
->EndingVpn
+ 1, Alignment
>> PAGE_SHIFT
);
412 /* Check if the current bounds are suitable */
413 if ((HighVpn
> LowVpn
) && ((HighVpn
- LowVpn
) >= PageCount
))
415 /* There is enough space to add our node */
416 LowVpn
= HighVpn
- PageCount
;
417 *Base
= LowVpn
<< PAGE_SHIFT
;
419 /* Can we use the current node as parent? */
420 Child
= RtlRightChildAvl(Node
);
423 /* Node has no right child, so use it as parent */
425 return TableInsertAsRight
;
429 /* Node has a right child, find most left grand child */
431 while ((Child
= RtlLeftChildAvl(Node
))) Node
= Child
;
433 return TableInsertAsLeft
;
437 /* Update the upper margin if neccessary */
438 if (Node
->StartingVpn
< HighVpn
) HighVpn
= Node
->StartingVpn
;
440 /* Go to the next lower node */
441 Node
= MiGetPreviousNode(Node
);
444 /* Check if there's enough space before the lowest Vad */
445 LowVpn
= ROUND_UP((ULONG_PTR
)MI_LOWEST_VAD_ADDRESS
, Alignment
) >> PAGE_SHIFT
;
446 if ((HighVpn
> LowVpn
) && ((HighVpn
- LowVpn
) >= PageCount
))
448 /* There is enough space to add our address */
449 LowVpn
= HighVpn
- PageCount
;
450 *Base
= LowVpn
<< PAGE_SHIFT
;
451 *Parent
= LowestNode
;
452 return TableInsertAsLeft
;
455 /* No address space left at all */
458 return TableFoundNode
;