2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/syspte.c
5 * PURPOSE: ARM Memory Manager System PTE Allocator
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 * Roel Messiant (roel.messiant@reactos.org)
10 /* INCLUDES *******************************************************************/
16 #define MODULE_INVOLVED_IN_ARM3
17 #include "../ARM3/miarm.h"
19 /* GLOBALS ********************************************************************/
21 PMMPTE MmSystemPteBase
;
22 PMMPTE MmSystemPtesStart
[MaximumPtePoolTypes
];
23 PMMPTE MmSystemPtesEnd
[MaximumPtePoolTypes
];
24 MMPTE MmFirstFreeSystemPte
[MaximumPtePoolTypes
];
25 ULONG MmTotalFreeSystemPtes
[MaximumPtePoolTypes
];
26 ULONG MmTotalSystemPtes
;
28 /* PRIVATE FUNCTIONS **********************************************************/
31 // The free System Page Table Entries are stored in a bunch of clusters,
32 // each consisting of one or more PTEs. These PTE clusters are connected
33 // in a singly linked list, ordered by increasing cluster size.
35 // A cluster consisting of a single PTE is marked by having the OneEntry flag
36 // of its PTE set. The forward link is contained in the NextEntry field.
38 // Clusters containing multiple PTEs have the OneEntry flag of their first PTE
39 // reset. The NextEntry field of the first PTE contains the forward link, and
40 // the size of the cluster is stored in the NextEntry field of its second PTE.
42 // Reserving PTEs currently happens by walking the linked list until a cluster
43 // is found that contains the requested amount of PTEs or more. This cluster
44 // is removed from the list, and the requested amount of PTEs is taken from the
45 // tail of this cluster. If any PTEs remain in the cluster, the linked list is
46 // walked again until a second cluster is found that contains the same amount
47 // of PTEs or more. The first cluster is then inserted in front of the second
50 // Releasing PTEs currently happens by walking the whole linked list, recording
51 // the first cluster that contains the amount of PTEs to release or more. When
52 // a cluster is found that is adjacent to the PTEs being released, this cluster
53 // is removed from the list and subsequently added to the PTEs being released.
54 // This ensures no two clusters are adjacent, which maximizes their size.
55 // After the walk is complete, a new cluster is created that contains the PTEs
56 // being released, which is then inserted in front of the recorded cluster.
61 MI_GET_CLUSTER_SIZE(IN PMMPTE Pte
)
64 // First check for a single PTE
66 if (Pte
->u
.List
.OneEntry
)
70 // Then read the size from the trailing PTE
73 return (ULONG
)Pte
->u
.List
.NextEntry
;
78 MiReserveAlignedSystemPtes(IN ULONG NumberOfPtes
,
79 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
,
83 PMMPTE PreviousPte
, NextPte
, ReturnPte
;
89 ASSERT(Alignment
<= PAGE_SIZE
);
92 // Acquire the System PTE lock
94 OldIrql
= KeAcquireQueuedSpinLock(LockQueueSystemSpaceLock
);
97 // Find the last cluster in the list that doesn't contain enough PTEs
99 PreviousPte
= &MmFirstFreeSystemPte
[SystemPtePoolType
];
101 while (PreviousPte
->u
.List
.NextEntry
!= MM_EMPTY_PTE_LIST
)
104 // Get the next cluster and its size
106 NextPte
= MmSystemPteBase
+ PreviousPte
->u
.List
.NextEntry
;
107 ClusterSize
= MI_GET_CLUSTER_SIZE(NextPte
);
110 // Check if this cluster contains enough PTEs
112 if (NumberOfPtes
<= ClusterSize
)
116 // On to the next cluster
118 PreviousPte
= NextPte
;
122 // Make sure we didn't reach the end of the cluster list
124 if (PreviousPte
->u
.List
.NextEntry
== MM_EMPTY_PTE_LIST
)
127 // Release the System PTE lock and return failure
129 KeReleaseQueuedSpinLock(LockQueueSystemSpaceLock
, OldIrql
);
134 // Unlink the cluster
136 PreviousPte
->u
.List
.NextEntry
= NextPte
->u
.List
.NextEntry
;
139 // Check if the reservation spans the whole cluster
141 if (ClusterSize
== NumberOfPtes
)
144 // Return the first PTE of this cluster
151 if (NextPte
->u
.List
.OneEntry
== 0)
161 // Divide the cluster into two parts
163 ClusterSize
-= NumberOfPtes
;
164 ReturnPte
= NextPte
+ ClusterSize
;
167 // Set the size of the first cluster, zero the second if needed
169 if (ClusterSize
== 1)
171 NextPte
->u
.List
.OneEntry
= 1;
172 ReturnPte
->u
.Long
= 0;
177 NextPte
->u
.List
.NextEntry
= ClusterSize
;
181 // Step through the cluster list to find out where to insert the first
183 PreviousPte
= &MmFirstFreeSystemPte
[SystemPtePoolType
];
185 while (PreviousPte
->u
.List
.NextEntry
!= MM_EMPTY_PTE_LIST
)
188 // Get the next cluster
190 NextPte
= MmSystemPteBase
+ PreviousPte
->u
.List
.NextEntry
;
193 // Check if the cluster to insert is smaller or of equal size
195 if (ClusterSize
<= MI_GET_CLUSTER_SIZE(NextPte
))
199 // On to the next cluster
201 PreviousPte
= NextPte
;
205 // Retrieve the first cluster and link it back into the cluster list
207 NextPte
= ReturnPte
- ClusterSize
;
209 NextPte
->u
.List
.NextEntry
= PreviousPte
->u
.List
.NextEntry
;
210 PreviousPte
->u
.List
.NextEntry
= NextPte
- MmSystemPteBase
;
214 // Decrease availability
216 MmTotalFreeSystemPtes
[SystemPtePoolType
] -= NumberOfPtes
;
219 // Release the System PTE lock
221 KeReleaseQueuedSpinLock(LockQueueSystemSpaceLock
, OldIrql
);
229 // Return the reserved PTEs
236 MiReserveSystemPtes(IN ULONG NumberOfPtes
,
237 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
)
242 // Use the extended function
244 PointerPte
= MiReserveAlignedSystemPtes(NumberOfPtes
, SystemPtePoolType
, 0);
247 // Check if allocation failed
252 // Warn that we are out of memory
254 DPRINT1("MiReserveSystemPtes: Failed to reserve %lu PTE(s)!\n", NumberOfPtes
);
258 // Return the PTE Pointer
265 MiReleaseSystemPtes(IN PMMPTE StartingPte
,
266 IN ULONG NumberOfPtes
,
267 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
)
271 PMMPTE PreviousPte
, NextPte
, InsertPte
;
274 // Check to make sure the PTE address is within bounds
276 ASSERT(NumberOfPtes
!= 0);
277 ASSERT(StartingPte
>= MmSystemPtesStart
[SystemPtePoolType
]);
278 ASSERT(StartingPte
+ NumberOfPtes
- 1 <= MmSystemPtesEnd
[SystemPtePoolType
]);
283 RtlZeroMemory(StartingPte
, NumberOfPtes
* sizeof(MMPTE
));
286 // Acquire the System PTE lock
288 OldIrql
= KeAcquireQueuedSpinLock(LockQueueSystemSpaceLock
);
291 // Increase availability
293 MmTotalFreeSystemPtes
[SystemPtePoolType
] += NumberOfPtes
;
296 // Step through the cluster list to find where to insert the PTEs
298 PreviousPte
= &MmFirstFreeSystemPte
[SystemPtePoolType
];
301 while (PreviousPte
->u
.List
.NextEntry
!= MM_EMPTY_PTE_LIST
)
304 // Get the next cluster and its size
306 NextPte
= MmSystemPteBase
+ PreviousPte
->u
.List
.NextEntry
;
307 ClusterSize
= MI_GET_CLUSTER_SIZE(NextPte
);
310 // Check if this cluster is adjacent to the PTEs being released
312 if ((NextPte
+ ClusterSize
== StartingPte
) ||
313 (StartingPte
+ NumberOfPtes
== NextPte
))
316 // Add the PTEs in the cluster to the PTEs being released
318 NumberOfPtes
+= ClusterSize
;
320 if (NextPte
< StartingPte
)
321 StartingPte
= NextPte
;
324 // Unlink this cluster and zero it
326 PreviousPte
->u
.List
.NextEntry
= NextPte
->u
.List
.NextEntry
;
328 if (NextPte
->u
.List
.OneEntry
== 0)
336 // Invalidate the previously found insertion location, if any
343 // Check if the insertion location is right before this cluster
345 if ((InsertPte
== NULL
) && (NumberOfPtes
<= ClusterSize
))
346 InsertPte
= PreviousPte
;
349 // On to the next cluster
351 PreviousPte
= NextPte
;
356 // If no insertion location was found, use the tail of the list
358 if (InsertPte
== NULL
)
359 InsertPte
= PreviousPte
;
362 // Create a new cluster using the PTEs being released
364 if (NumberOfPtes
!= 1)
366 StartingPte
->u
.List
.OneEntry
= 0;
368 NextPte
= StartingPte
+ 1;
369 NextPte
->u
.List
.NextEntry
= NumberOfPtes
;
372 StartingPte
->u
.List
.OneEntry
= 1;
375 // Link the new cluster into the cluster list at the insertion location
377 StartingPte
->u
.List
.NextEntry
= InsertPte
->u
.List
.NextEntry
;
378 InsertPte
->u
.List
.NextEntry
= StartingPte
- MmSystemPteBase
;
381 // Release the System PTE lock
383 KeReleaseQueuedSpinLock(LockQueueSystemSpaceLock
, OldIrql
);
389 MiInitializeSystemPtes(IN PMMPTE StartingPte
,
390 IN ULONG NumberOfPtes
,
391 IN MMSYSTEM_PTE_POOL_TYPE PoolType
)
396 ASSERT(NumberOfPtes
>= 1);
399 // Set the starting and ending PTE addresses for this space
401 MmSystemPteBase
= MI_SYSTEM_PTE_BASE
;
402 MmSystemPtesStart
[PoolType
] = StartingPte
;
403 MmSystemPtesEnd
[PoolType
] = StartingPte
+ NumberOfPtes
- 1;
404 DPRINT("System PTE space for %d starting at: %p and ending at: %p\n",
405 PoolType
, MmSystemPtesStart
[PoolType
], MmSystemPtesEnd
[PoolType
]);
408 // Clear all the PTEs to start with
410 RtlZeroMemory(StartingPte
, NumberOfPtes
* sizeof(MMPTE
));
413 // Make the first entry free and link it
415 StartingPte
->u
.List
.NextEntry
= MM_EMPTY_PTE_LIST
;
416 MmFirstFreeSystemPte
[PoolType
].u
.Long
= 0;
417 MmFirstFreeSystemPte
[PoolType
].u
.List
.NextEntry
= StartingPte
-
421 // The second entry stores the size of this PTE space
424 StartingPte
->u
.Long
= 0;
425 StartingPte
->u
.List
.NextEntry
= NumberOfPtes
;
428 // We also keep a global for it
430 MmTotalFreeSystemPtes
[PoolType
] = NumberOfPtes
;
433 // Check if this is the system PTE space
435 if (PoolType
== SystemPteSpace
)
438 // Remember how many PTEs we have
440 MmTotalSystemPtes
= NumberOfPtes
;