[NTOSKRNL]
[reactos.git] / reactos / ntoskrnl / mm / ARM3 / pool.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/pool.c
5 * PURPOSE: ARM Memory Manager Pool Allocator
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include <ntoskrnl.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 #define MODULE_INVOLVED_IN_ARM3
16 #include "../ARM3/miarm.h"
17
18 /* GLOBALS ********************************************************************/
19
20 LIST_ENTRY MmNonPagedPoolFreeListHead[MI_MAX_FREE_PAGE_LISTS];
21 PFN_COUNT MmNumberOfFreeNonPagedPool, MiExpansionPoolPagesInitialCharge;
22 PVOID MmNonPagedPoolEnd0;
23 PFN_NUMBER MiStartOfInitialPoolFrame, MiEndOfInitialPoolFrame;
24 KGUARDED_MUTEX MmPagedPoolMutex;
25 MM_PAGED_POOL_INFO MmPagedPoolInfo;
26 SIZE_T MmAllocatedNonPagedPool;
27 ULONG MmSpecialPoolTag;
28 ULONG MmConsumedPoolPercentage;
29 BOOLEAN MmProtectFreedNonPagedPool;
30 SLIST_HEADER MiNonPagedPoolSListHead;
31 ULONG MiNonPagedPoolSListMaximum = 4;
32 SLIST_HEADER MiPagedPoolSListHead;
33 ULONG MiPagedPoolSListMaximum = 8;
34
35 /* PRIVATE FUNCTIONS **********************************************************/
36
37 VOID
38 NTAPI
39 MiProtectFreeNonPagedPool(IN PVOID VirtualAddress,
40 IN ULONG PageCount)
41 {
42 PMMPTE PointerPte, LastPte;
43 MMPTE TempPte;
44
45 /* If pool is physical, can't protect PTEs */
46 if (MI_IS_PHYSICAL_ADDRESS(VirtualAddress)) return;
47
48 /* Get PTE pointers and loop */
49 PointerPte = MiAddressToPte(VirtualAddress);
50 LastPte = PointerPte + PageCount;
51 do
52 {
53 /* Capture the PTE for safety */
54 TempPte = *PointerPte;
55
56 /* Mark it as an invalid PTE, set proto bit to recognize it as pool */
57 TempPte.u.Hard.Valid = 0;
58 TempPte.u.Soft.Prototype = 1;
59 MI_WRITE_INVALID_PTE(PointerPte, TempPte);
60 } while (++PointerPte < LastPte);
61
62 /* Flush the TLB */
63 KeFlushEntireTb(TRUE, TRUE);
64 }
65
66 BOOLEAN
67 NTAPI
68 MiUnProtectFreeNonPagedPool(IN PVOID VirtualAddress,
69 IN ULONG PageCount)
70 {
71 PMMPTE PointerPte;
72 MMPTE TempPte;
73 PFN_NUMBER UnprotectedPages = 0;
74
75 /* If pool is physical, can't protect PTEs */
76 if (MI_IS_PHYSICAL_ADDRESS(VirtualAddress)) return FALSE;
77
78 /* Get, and capture the PTE */
79 PointerPte = MiAddressToPte(VirtualAddress);
80 TempPte = *PointerPte;
81
82 /* Loop protected PTEs */
83 while ((TempPte.u.Hard.Valid == 0) && (TempPte.u.Soft.Prototype == 1))
84 {
85 /* Unprotect the PTE */
86 TempPte.u.Hard.Valid = 1;
87 TempPte.u.Soft.Prototype = 0;
88 MI_WRITE_VALID_PTE(PointerPte, TempPte);
89
90 /* One more page */
91 if (++UnprotectedPages == PageCount) break;
92
93 /* Capture next PTE */
94 TempPte = *(++PointerPte);
95 }
96
97 /* Return if any pages were unprotected */
98 return UnprotectedPages ? TRUE : FALSE;
99 }
100
101 VOID
102 FORCEINLINE
103 MiProtectedPoolUnProtectLinks(IN PLIST_ENTRY Links,
104 OUT PVOID* PoolFlink,
105 OUT PVOID* PoolBlink)
106 {
107 BOOLEAN Safe;
108 PVOID PoolVa;
109
110 /* Initialize variables */
111 *PoolFlink = *PoolBlink = NULL;
112
113 /* Check if the list has entries */
114 if (IsListEmpty(Links) == FALSE)
115 {
116 /* We are going to need to forward link to do an insert */
117 PoolVa = Links->Flink;
118
119 /* So make it safe to access */
120 Safe = MiUnProtectFreeNonPagedPool(PoolVa, 1);
121 if (Safe) *PoolFlink = PoolVa;
122 }
123
124 /* Are we going to need a backward link too? */
125 if (Links != Links->Blink)
126 {
127 /* Get the head's backward link for the insert */
128 PoolVa = Links->Blink;
129
130 /* Make it safe to access */
131 Safe = MiUnProtectFreeNonPagedPool(PoolVa, 1);
132 if (Safe) *PoolBlink = PoolVa;
133 }
134 }
135
136 VOID
137 FORCEINLINE
138 MiProtectedPoolProtectLinks(IN PVOID PoolFlink,
139 IN PVOID PoolBlink)
140 {
141 /* Reprotect the pages, if they got unprotected earlier */
142 if (PoolFlink) MiProtectFreeNonPagedPool(PoolFlink, 1);
143 if (PoolBlink) MiProtectFreeNonPagedPool(PoolBlink, 1);
144 }
145
146 VOID
147 NTAPI
148 MiProtectedPoolInsertList(IN PLIST_ENTRY ListHead,
149 IN PLIST_ENTRY Entry,
150 IN BOOLEAN Critical)
151 {
152 PVOID PoolFlink, PoolBlink;
153
154 /* Make the list accessible */
155 MiProtectedPoolUnProtectLinks(ListHead, &PoolFlink, &PoolBlink);
156
157 /* Now insert in the right position */
158 Critical ? InsertHeadList(ListHead, Entry) : InsertTailList(ListHead, Entry);
159
160 /* And reprotect the pages containing the free links */
161 MiProtectedPoolProtectLinks(PoolFlink, PoolBlink);
162 }
163
164 VOID
165 NTAPI
166 MiProtectedPoolRemoveEntryList(IN PLIST_ENTRY Entry)
167 {
168 PVOID PoolFlink, PoolBlink;
169
170 /* Make the list accessible */
171 MiProtectedPoolUnProtectLinks(Entry, &PoolFlink, &PoolBlink);
172
173 /* Now remove */
174 RemoveEntryList(Entry);
175
176 /* And reprotect the pages containing the free links */
177 if (PoolFlink) MiProtectFreeNonPagedPool(PoolFlink, 1);
178 if (PoolBlink) MiProtectFreeNonPagedPool(PoolBlink, 1);
179 }
180
181 VOID
182 NTAPI
183 INIT_FUNCTION
184 MiInitializeNonPagedPoolThresholds(VOID)
185 {
186 PFN_NUMBER Size = MmMaximumNonPagedPoolInPages;
187
188 /* Default low threshold of 8MB or one third of nonpaged pool */
189 MiLowNonPagedPoolThreshold = (8 * _1MB) >> PAGE_SHIFT;
190 MiLowNonPagedPoolThreshold = min(MiLowNonPagedPoolThreshold, Size / 3);
191
192 /* Default high threshold of 20MB or 50% */
193 MiHighNonPagedPoolThreshold = (20 * _1MB) >> PAGE_SHIFT;
194 MiHighNonPagedPoolThreshold = min(MiHighNonPagedPoolThreshold, Size / 2);
195 ASSERT(MiLowNonPagedPoolThreshold < MiHighNonPagedPoolThreshold);
196 }
197
198 VOID
199 NTAPI
200 INIT_FUNCTION
201 MiInitializePoolEvents(VOID)
202 {
203 KIRQL OldIrql;
204 PFN_NUMBER FreePoolInPages;
205
206 /* Lock paged pool */
207 KeAcquireGuardedMutex(&MmPagedPoolMutex);
208
209 /* Total size of the paged pool minus the allocated size, is free */
210 FreePoolInPages = MmSizeOfPagedPoolInPages - MmPagedPoolInfo.AllocatedPagedPool;
211
212 /* Check the initial state high state */
213 if (FreePoolInPages >= MiHighPagedPoolThreshold)
214 {
215 /* We have plenty of pool */
216 KeSetEvent(MiHighPagedPoolEvent, 0, FALSE);
217 }
218 else
219 {
220 /* We don't */
221 KeClearEvent(MiHighPagedPoolEvent);
222 }
223
224 /* Check the initial low state */
225 if (FreePoolInPages <= MiLowPagedPoolThreshold)
226 {
227 /* We're very low in free pool memory */
228 KeSetEvent(MiLowPagedPoolEvent, 0, FALSE);
229 }
230 else
231 {
232 /* We're not */
233 KeClearEvent(MiLowPagedPoolEvent);
234 }
235
236 /* Release the paged pool lock */
237 KeReleaseGuardedMutex(&MmPagedPoolMutex);
238
239 /* Now it's time for the nonpaged pool lock */
240 OldIrql = KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock);
241
242 /* Free pages are the maximum minus what's been allocated */
243 FreePoolInPages = MmMaximumNonPagedPoolInPages - MmAllocatedNonPagedPool;
244
245 /* Check if we have plenty */
246 if (FreePoolInPages >= MiHighNonPagedPoolThreshold)
247 {
248 /* We do, set the event */
249 KeSetEvent(MiHighNonPagedPoolEvent, 0, FALSE);
250 }
251 else
252 {
253 /* We don't, clear the event */
254 KeClearEvent(MiHighNonPagedPoolEvent);
255 }
256
257 /* Check if we have very little */
258 if (FreePoolInPages <= MiLowNonPagedPoolThreshold)
259 {
260 /* We do, set the event */
261 KeSetEvent(MiLowNonPagedPoolEvent, 0, FALSE);
262 }
263 else
264 {
265 /* We don't, clear it */
266 KeClearEvent(MiLowNonPagedPoolEvent);
267 }
268
269 /* We're done, release the nonpaged pool lock */
270 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock, OldIrql);
271 }
272
273 VOID
274 NTAPI
275 INIT_FUNCTION
276 MiInitializeNonPagedPool(VOID)
277 {
278 ULONG i;
279 PFN_COUNT PoolPages;
280 PMMFREE_POOL_ENTRY FreeEntry, FirstEntry;
281 PMMPTE PointerPte;
282 PAGED_CODE();
283
284 //
285 // Initialize the pool S-LISTs as well as their maximum count. In general,
286 // we'll allow 8 times the default on a 2GB system, and two times the default
287 // on a 1GB system.
288 //
289 InitializeSListHead(&MiPagedPoolSListHead);
290 InitializeSListHead(&MiNonPagedPoolSListHead);
291 if (MmNumberOfPhysicalPages >= ((2 * _1GB) /PAGE_SIZE))
292 {
293 MiNonPagedPoolSListMaximum *= 8;
294 MiPagedPoolSListMaximum *= 8;
295 }
296 else if (MmNumberOfPhysicalPages >= (_1GB /PAGE_SIZE))
297 {
298 MiNonPagedPoolSListMaximum *= 2;
299 MiPagedPoolSListMaximum *= 2;
300 }
301
302 //
303 // However if debugging options for the pool are enabled, turn off the S-LIST
304 // to reduce the risk of messing things up even more
305 //
306 if (MmProtectFreedNonPagedPool)
307 {
308 MiNonPagedPoolSListMaximum = 0;
309 MiPagedPoolSListMaximum = 0;
310 }
311
312 //
313 // We keep 4 lists of free pages (4 lists help avoid contention)
314 //
315 for (i = 0; i < MI_MAX_FREE_PAGE_LISTS; i++)
316 {
317 //
318 // Initialize each of them
319 //
320 InitializeListHead(&MmNonPagedPoolFreeListHead[i]);
321 }
322
323 //
324 // Calculate how many pages the initial nonpaged pool has
325 //
326 PoolPages = (PFN_COUNT)BYTES_TO_PAGES(MmSizeOfNonPagedPoolInBytes);
327 MmNumberOfFreeNonPagedPool = PoolPages;
328
329 //
330 // Initialize the first free entry
331 //
332 FreeEntry = MmNonPagedPoolStart;
333 FirstEntry = FreeEntry;
334 FreeEntry->Size = PoolPages;
335 FreeEntry->Signature = MM_FREE_POOL_SIGNATURE;
336 FreeEntry->Owner = FirstEntry;
337
338 //
339 // Insert it into the last list
340 //
341 InsertHeadList(&MmNonPagedPoolFreeListHead[MI_MAX_FREE_PAGE_LISTS - 1],
342 &FreeEntry->List);
343
344 //
345 // Now create free entries for every single other page
346 //
347 while (PoolPages-- > 1)
348 {
349 //
350 // Link them all back to the original entry
351 //
352 FreeEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)FreeEntry + PAGE_SIZE);
353 FreeEntry->Owner = FirstEntry;
354 FreeEntry->Signature = MM_FREE_POOL_SIGNATURE;
355 }
356
357 //
358 // Validate and remember first allocated pool page
359 //
360 PointerPte = MiAddressToPte(MmNonPagedPoolStart);
361 ASSERT(PointerPte->u.Hard.Valid == 1);
362 MiStartOfInitialPoolFrame = PFN_FROM_PTE(PointerPte);
363
364 //
365 // Keep track of where initial nonpaged pool ends
366 //
367 MmNonPagedPoolEnd0 = (PVOID)((ULONG_PTR)MmNonPagedPoolStart +
368 MmSizeOfNonPagedPoolInBytes);
369
370 //
371 // Validate and remember last allocated pool page
372 //
373 PointerPte = MiAddressToPte((PVOID)((ULONG_PTR)MmNonPagedPoolEnd0 - 1));
374 ASSERT(PointerPte->u.Hard.Valid == 1);
375 MiEndOfInitialPoolFrame = PFN_FROM_PTE(PointerPte);
376
377 //
378 // Validate the first nonpaged pool expansion page (which is a guard page)
379 //
380 PointerPte = MiAddressToPte(MmNonPagedPoolExpansionStart);
381 ASSERT(PointerPte->u.Hard.Valid == 0);
382
383 //
384 // Calculate the size of the expansion region alone
385 //
386 MiExpansionPoolPagesInitialCharge = (PFN_COUNT)
387 BYTES_TO_PAGES(MmMaximumNonPagedPoolInBytes - MmSizeOfNonPagedPoolInBytes);
388
389 //
390 // Remove 2 pages, since there's a guard page on top and on the bottom
391 //
392 MiExpansionPoolPagesInitialCharge -= 2;
393
394 //
395 // Now initialize the nonpaged pool expansion PTE space. Remember there's a
396 // guard page on top so make sure to skip it. The bottom guard page will be
397 // guaranteed by the fact our size is off by one.
398 //
399 MiInitializeSystemPtes(PointerPte + 1,
400 MiExpansionPoolPagesInitialCharge,
401 NonPagedPoolExpansion);
402 }
403
404 POOL_TYPE
405 NTAPI
406 MmDeterminePoolType(IN PVOID PoolAddress)
407 {
408 //
409 // Use a simple bounds check
410 //
411 if (PoolAddress >= MmPagedPoolStart && PoolAddress <= MmPagedPoolEnd)
412 return PagedPool;
413 else if (PoolAddress >= MmNonPagedPoolStart && PoolAddress <= MmNonPagedPoolEnd)
414 return NonPagedPool;
415 KeBugCheckEx(BAD_POOL_CALLER, 0x42, (ULONG_PTR)PoolAddress, 0, 0);
416 }
417
418 PVOID
419 NTAPI
420 MiAllocatePoolPages(IN POOL_TYPE PoolType,
421 IN SIZE_T SizeInBytes)
422 {
423 PFN_NUMBER PageFrameNumber;
424 PFN_COUNT SizeInPages, PageTableCount;
425 ULONG i;
426 KIRQL OldIrql;
427 PLIST_ENTRY NextEntry, NextHead, LastHead;
428 PMMPTE PointerPte, StartPte;
429 PMMPDE PointerPde;
430 ULONG EndAllocation;
431 MMPTE TempPte;
432 MMPDE TempPde;
433 PMMPFN Pfn1;
434 PVOID BaseVa, BaseVaStart;
435 PMMFREE_POOL_ENTRY FreeEntry;
436 PKSPIN_LOCK_QUEUE LockQueue;
437
438 //
439 // Figure out how big the allocation is in pages
440 //
441 SizeInPages = (PFN_COUNT)BYTES_TO_PAGES(SizeInBytes);
442
443 //
444 // Check for overflow
445 //
446 if (SizeInPages == 0)
447 {
448 //
449 // Fail
450 //
451 return NULL;
452 }
453
454 //
455 // Handle paged pool
456 //
457 if ((PoolType & BASE_POOL_TYPE_MASK) == PagedPool)
458 {
459 //
460 // If only one page is being requested, try to grab it from the S-LIST
461 //
462 if ((SizeInPages == 1) && (ExQueryDepthSList(&MiPagedPoolSListHead)))
463 {
464 BaseVa = InterlockedPopEntrySList(&MiPagedPoolSListHead);
465 if (BaseVa) return BaseVa;
466 }
467
468 //
469 // Lock the paged pool mutex
470 //
471 KeAcquireGuardedMutex(&MmPagedPoolMutex);
472
473 //
474 // Find some empty allocation space
475 //
476 i = RtlFindClearBitsAndSet(MmPagedPoolInfo.PagedPoolAllocationMap,
477 SizeInPages,
478 MmPagedPoolInfo.PagedPoolHint);
479 if (i == 0xFFFFFFFF)
480 {
481 //
482 // Get the page bit count
483 //
484 i = ((SizeInPages - 1) / PTE_COUNT) + 1;
485 DPRINT("Paged pool expansion: %lu %x\n", i, SizeInPages);
486
487 //
488 // Check if there is enougn paged pool expansion space left
489 //
490 if (MmPagedPoolInfo.NextPdeForPagedPoolExpansion >
491 (PMMPDE)MiAddressToPte(MmPagedPoolInfo.LastPteForPagedPool))
492 {
493 //
494 // Out of memory!
495 //
496 DPRINT1("OUT OF PAGED POOL!!!\n");
497 KeReleaseGuardedMutex(&MmPagedPoolMutex);
498 return NULL;
499 }
500
501 //
502 // Check if we'll have to expand past the last PTE we have available
503 //
504 if (((i - 1) + MmPagedPoolInfo.NextPdeForPagedPoolExpansion) >
505 (PMMPDE)MiAddressToPte(MmPagedPoolInfo.LastPteForPagedPool))
506 {
507 //
508 // We can only support this much then
509 //
510 PointerPde = MiAddressToPte(MmPagedPoolInfo.LastPteForPagedPool);
511 PageTableCount = (PFN_COUNT)(PointerPde + 1 -
512 MmPagedPoolInfo.NextPdeForPagedPoolExpansion);
513 ASSERT(PageTableCount < i);
514 i = PageTableCount;
515 }
516 else
517 {
518 //
519 // Otherwise, there is plenty of space left for this expansion
520 //
521 PageTableCount = i;
522 }
523
524 //
525 // Get the template PDE we'll use to expand
526 //
527 TempPde = ValidKernelPde;
528
529 //
530 // Get the first PTE in expansion space
531 //
532 PointerPde = MmPagedPoolInfo.NextPdeForPagedPoolExpansion;
533 BaseVa = MiPdeToPte(PointerPde);
534 BaseVaStart = BaseVa;
535
536 //
537 // Lock the PFN database and loop pages
538 //
539 OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
540 do
541 {
542 //
543 // It should not already be valid
544 //
545 ASSERT(PointerPde->u.Hard.Valid == 0);
546
547 /* Request a page */
548 MI_SET_USAGE(MI_USAGE_PAGED_POOL);
549 MI_SET_PROCESS2("Kernel");
550 PageFrameNumber = MiRemoveAnyPage(MI_GET_NEXT_COLOR());
551 TempPde.u.Hard.PageFrameNumber = PageFrameNumber;
552 #if (_MI_PAGING_LEVELS >= 3)
553 /* On PAE/x64 systems, there's no double-buffering */
554 ASSERT(FALSE);
555 #else
556 //
557 // Save it into our double-buffered system page directory
558 //
559 MmSystemPagePtes[((ULONG_PTR)PointerPde & (SYSTEM_PD_SIZE - 1)) / sizeof(MMPTE)] = TempPde;
560
561 /* Initialize the PFN */
562 MiInitializePfnForOtherProcess(PageFrameNumber,
563 (PMMPTE)PointerPde,
564 MmSystemPageDirectory[(PointerPde - MiAddressToPde(NULL)) / PDE_COUNT]);
565
566 /* Write the actual PDE now */
567 // MI_WRITE_VALID_PDE(PointerPde, TempPde);
568 #endif
569 //
570 // Move on to the next expansion address
571 //
572 PointerPde++;
573 BaseVa = (PVOID)((ULONG_PTR)BaseVa + PAGE_SIZE);
574 i--;
575 } while (i > 0);
576
577 //
578 // Release the PFN database lock
579 //
580 KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
581
582 //
583 // These pages are now available, clear their availablity bits
584 //
585 EndAllocation = (ULONG)(MmPagedPoolInfo.NextPdeForPagedPoolExpansion -
586 (PMMPDE)MiAddressToPte(MmPagedPoolInfo.FirstPteForPagedPool)) *
587 PTE_COUNT;
588 RtlClearBits(MmPagedPoolInfo.PagedPoolAllocationMap,
589 EndAllocation,
590 PageTableCount * PTE_COUNT);
591
592 //
593 // Update the next expansion location
594 //
595 MmPagedPoolInfo.NextPdeForPagedPoolExpansion += PageTableCount;
596
597 //
598 // Zero out the newly available memory
599 //
600 RtlZeroMemory(BaseVaStart, PageTableCount * PAGE_SIZE);
601
602 //
603 // Now try consuming the pages again
604 //
605 i = RtlFindClearBitsAndSet(MmPagedPoolInfo.PagedPoolAllocationMap,
606 SizeInPages,
607 0);
608 if (i == 0xFFFFFFFF)
609 {
610 //
611 // Out of memory!
612 //
613 DPRINT1("OUT OF PAGED POOL!!!\n");
614 KeReleaseGuardedMutex(&MmPagedPoolMutex);
615 return NULL;
616 }
617 }
618
619 //
620 // Update the pool hint if the request was just one page
621 //
622 if (SizeInPages == 1) MmPagedPoolInfo.PagedPoolHint = i + 1;
623
624 //
625 // Update the end bitmap so we know the bounds of this allocation when
626 // the time comes to free it
627 //
628 EndAllocation = i + SizeInPages - 1;
629 RtlSetBit(MmPagedPoolInfo.EndOfPagedPoolBitmap, EndAllocation);
630
631 //
632 // Now we can release the lock (it mainly protects the bitmap)
633 //
634 KeReleaseGuardedMutex(&MmPagedPoolMutex);
635
636 //
637 // Now figure out where this allocation starts
638 //
639 BaseVa = (PVOID)((ULONG_PTR)MmPagedPoolStart + (i << PAGE_SHIFT));
640
641 //
642 // Flush the TLB
643 //
644 KeFlushEntireTb(TRUE, TRUE);
645
646 /* Setup a demand-zero writable PTE */
647 MI_MAKE_SOFTWARE_PTE(&TempPte, MM_READWRITE);
648
649 //
650 // Find the first and last PTE, then loop them all
651 //
652 PointerPte = MiAddressToPte(BaseVa);
653 StartPte = PointerPte + SizeInPages;
654 do
655 {
656 //
657 // Write the demand zero PTE and keep going
658 //
659 MI_WRITE_INVALID_PTE(PointerPte, TempPte);
660 } while (++PointerPte < StartPte);
661
662 //
663 // Return the allocation address to the caller
664 //
665 return BaseVa;
666 }
667
668 //
669 // If only one page is being requested, try to grab it from the S-LIST
670 //
671 if ((SizeInPages == 1) && (ExQueryDepthSList(&MiNonPagedPoolSListHead)))
672 {
673 BaseVa = InterlockedPopEntrySList(&MiNonPagedPoolSListHead);
674 if (BaseVa) return BaseVa;
675 }
676
677 //
678 // Allocations of less than 4 pages go into their individual buckets
679 //
680 i = SizeInPages - 1;
681 if (i >= MI_MAX_FREE_PAGE_LISTS) i = MI_MAX_FREE_PAGE_LISTS - 1;
682
683 //
684 // Loop through all the free page lists based on the page index
685 //
686 NextHead = &MmNonPagedPoolFreeListHead[i];
687 LastHead = &MmNonPagedPoolFreeListHead[MI_MAX_FREE_PAGE_LISTS];
688
689 //
690 // Acquire the nonpaged pool lock
691 //
692 OldIrql = KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock);
693 do
694 {
695 //
696 // Now loop through all the free page entries in this given list
697 //
698 NextEntry = NextHead->Flink;
699 while (NextEntry != NextHead)
700 {
701 /* Is freed non paged pool enabled */
702 if (MmProtectFreedNonPagedPool)
703 {
704 /* We need to be able to touch this page, unprotect it */
705 MiUnProtectFreeNonPagedPool(NextEntry, 0);
706 }
707
708 //
709 // Grab the entry and see if it can handle our allocation
710 //
711 FreeEntry = CONTAINING_RECORD(NextEntry, MMFREE_POOL_ENTRY, List);
712 ASSERT(FreeEntry->Signature == MM_FREE_POOL_SIGNATURE);
713 if (FreeEntry->Size >= SizeInPages)
714 {
715 //
716 // It does, so consume the pages from here
717 //
718 FreeEntry->Size -= SizeInPages;
719
720 //
721 // The allocation will begin in this free page area
722 //
723 BaseVa = (PVOID)((ULONG_PTR)FreeEntry +
724 (FreeEntry->Size << PAGE_SHIFT));
725
726 /* Remove the item from the list, depending if pool is protected */
727 MmProtectFreedNonPagedPool ?
728 MiProtectedPoolRemoveEntryList(&FreeEntry->List) :
729 RemoveEntryList(&FreeEntry->List);
730
731 //
732 // However, check if its' still got space left
733 //
734 if (FreeEntry->Size != 0)
735 {
736 /* Check which list to insert this entry into */
737 i = FreeEntry->Size - 1;
738 if (i >= MI_MAX_FREE_PAGE_LISTS) i = MI_MAX_FREE_PAGE_LISTS - 1;
739
740 /* Insert the entry into the free list head, check for prot. pool */
741 MmProtectFreedNonPagedPool ?
742 MiProtectedPoolInsertList(&MmNonPagedPoolFreeListHead[i], &FreeEntry->List, TRUE) :
743 InsertTailList(&MmNonPagedPoolFreeListHead[i], &FreeEntry->List);
744
745 /* Is freed non paged pool protected? */
746 if (MmProtectFreedNonPagedPool)
747 {
748 /* Protect the freed pool! */
749 MiProtectFreeNonPagedPool(FreeEntry, FreeEntry->Size);
750 }
751 }
752
753 //
754 // Grab the PTE for this allocation
755 //
756 PointerPte = MiAddressToPte(BaseVa);
757 ASSERT(PointerPte->u.Hard.Valid == 1);
758
759 //
760 // Grab the PFN NextEntry and index
761 //
762 Pfn1 = MiGetPfnEntry(PFN_FROM_PTE(PointerPte));
763
764 //
765 // Now mark it as the beginning of an allocation
766 //
767 ASSERT(Pfn1->u3.e1.StartOfAllocation == 0);
768 Pfn1->u3.e1.StartOfAllocation = 1;
769
770 /* Mark it as special pool if needed */
771 ASSERT(Pfn1->u4.VerifierAllocation == 0);
772 if (PoolType & VERIFIER_POOL_MASK)
773 {
774 Pfn1->u4.VerifierAllocation = 1;
775 }
776
777 //
778 // Check if the allocation is larger than one page
779 //
780 if (SizeInPages != 1)
781 {
782 //
783 // Navigate to the last PFN entry and PTE
784 //
785 PointerPte += SizeInPages - 1;
786 ASSERT(PointerPte->u.Hard.Valid == 1);
787 Pfn1 = MiGetPfnEntry(PointerPte->u.Hard.PageFrameNumber);
788 }
789
790 //
791 // Mark this PFN as the last (might be the same as the first)
792 //
793 ASSERT(Pfn1->u3.e1.EndOfAllocation == 0);
794 Pfn1->u3.e1.EndOfAllocation = 1;
795
796 //
797 // Release the nonpaged pool lock, and return the allocation
798 //
799 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock, OldIrql);
800 return BaseVa;
801 }
802
803 //
804 // Try the next free page entry
805 //
806 NextEntry = FreeEntry->List.Flink;
807
808 /* Is freed non paged pool protected? */
809 if (MmProtectFreedNonPagedPool)
810 {
811 /* Protect the freed pool! */
812 MiProtectFreeNonPagedPool(FreeEntry, FreeEntry->Size);
813 }
814 }
815 } while (++NextHead < LastHead);
816
817 //
818 // If we got here, we're out of space.
819 // Start by releasing the lock
820 //
821 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock, OldIrql);
822
823 //
824 // Allocate some system PTEs
825 //
826 StartPte = MiReserveSystemPtes(SizeInPages, NonPagedPoolExpansion);
827 PointerPte = StartPte;
828 if (StartPte == NULL)
829 {
830 //
831 // Ran out of memory
832 //
833 DPRINT1("Out of NP Expansion Pool\n");
834 return NULL;
835 }
836
837 //
838 // Acquire the pool lock now
839 //
840 OldIrql = KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock);
841
842 //
843 // Lock the PFN database too
844 //
845 LockQueue = &KeGetCurrentPrcb()->LockQueue[LockQueuePfnLock];
846 KeAcquireQueuedSpinLockAtDpcLevel(LockQueue);
847
848 //
849 // Loop the pages
850 //
851 TempPte = ValidKernelPte;
852 do
853 {
854 /* Allocate a page */
855 MI_SET_USAGE(MI_USAGE_PAGED_POOL);
856 MI_SET_PROCESS2("Kernel");
857 PageFrameNumber = MiRemoveAnyPage(MI_GET_NEXT_COLOR());
858
859 /* Get the PFN entry for it and fill it out */
860 Pfn1 = MiGetPfnEntry(PageFrameNumber);
861 Pfn1->u3.e2.ReferenceCount = 1;
862 Pfn1->u2.ShareCount = 1;
863 Pfn1->PteAddress = PointerPte;
864 Pfn1->u3.e1.PageLocation = ActiveAndValid;
865 Pfn1->u4.VerifierAllocation = 0;
866
867 /* Write the PTE for it */
868 TempPte.u.Hard.PageFrameNumber = PageFrameNumber;
869 MI_WRITE_VALID_PTE(PointerPte++, TempPte);
870 } while (--SizeInPages > 0);
871
872 //
873 // This is the last page
874 //
875 Pfn1->u3.e1.EndOfAllocation = 1;
876
877 //
878 // Get the first page and mark it as such
879 //
880 Pfn1 = MiGetPfnEntry(StartPte->u.Hard.PageFrameNumber);
881 Pfn1->u3.e1.StartOfAllocation = 1;
882
883 /* Mark it as a verifier allocation if needed */
884 ASSERT(Pfn1->u4.VerifierAllocation == 0);
885 if (PoolType & VERIFIER_POOL_MASK) Pfn1->u4.VerifierAllocation = 1;
886
887 //
888 // Release the PFN and nonpaged pool lock
889 //
890 KeReleaseQueuedSpinLockFromDpcLevel(LockQueue);
891 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock, OldIrql);
892
893 //
894 // Return the address
895 //
896 return MiPteToAddress(StartPte);
897 }
898
899 ULONG
900 NTAPI
901 MiFreePoolPages(IN PVOID StartingVa)
902 {
903 PMMPTE PointerPte, StartPte;
904 PMMPFN Pfn1, StartPfn;
905 PFN_COUNT FreePages, NumberOfPages;
906 KIRQL OldIrql;
907 PMMFREE_POOL_ENTRY FreeEntry, NextEntry, LastEntry;
908 ULONG i, End;
909 ULONG_PTR Offset;
910
911 //
912 // Handle paged pool
913 //
914 if ((StartingVa >= MmPagedPoolStart) && (StartingVa <= MmPagedPoolEnd))
915 {
916 //
917 // Calculate the offset from the beginning of paged pool, and convert it
918 // into pages
919 //
920 Offset = (ULONG_PTR)StartingVa - (ULONG_PTR)MmPagedPoolStart;
921 i = (ULONG)(Offset >> PAGE_SHIFT);
922 End = i;
923
924 //
925 // Now use the end bitmap to scan until we find a set bit, meaning that
926 // this allocation finishes here
927 //
928 while (!RtlTestBit(MmPagedPoolInfo.EndOfPagedPoolBitmap, End)) End++;
929
930 //
931 // Now calculate the total number of pages this allocation spans. If it's
932 // only one page, add it to the S-LIST instead of freeing it
933 //
934 NumberOfPages = End - i + 1;
935 if ((NumberOfPages == 1) &&
936 (ExQueryDepthSList(&MiPagedPoolSListHead) < MiPagedPoolSListMaximum))
937 {
938 InterlockedPushEntrySList(&MiPagedPoolSListHead, StartingVa);
939 return 1;
940 }
941
942 /* Delete the actual pages */
943 PointerPte = MmPagedPoolInfo.FirstPteForPagedPool + i;
944 FreePages = MiDeleteSystemPageableVm(PointerPte, NumberOfPages, 0, NULL);
945 ASSERT(FreePages == NumberOfPages);
946
947 //
948 // Acquire the paged pool lock
949 //
950 KeAcquireGuardedMutex(&MmPagedPoolMutex);
951
952 //
953 // Clear the allocation and free bits
954 //
955 RtlClearBit(MmPagedPoolInfo.EndOfPagedPoolBitmap, End);
956 RtlClearBits(MmPagedPoolInfo.PagedPoolAllocationMap, i, NumberOfPages);
957
958 //
959 // Update the hint if we need to
960 //
961 if (i < MmPagedPoolInfo.PagedPoolHint) MmPagedPoolInfo.PagedPoolHint = i;
962
963 //
964 // Release the lock protecting the bitmaps
965 //
966 KeReleaseGuardedMutex(&MmPagedPoolMutex);
967
968 //
969 // And finally return the number of pages freed
970 //
971 return NumberOfPages;
972 }
973
974 //
975 // Get the first PTE and its corresponding PFN entry. If this is also the
976 // last PTE, meaning that this allocation was only for one page, push it into
977 // the S-LIST instead of freeing it
978 //
979 StartPte = PointerPte = MiAddressToPte(StartingVa);
980 StartPfn = Pfn1 = MiGetPfnEntry(PointerPte->u.Hard.PageFrameNumber);
981 if ((Pfn1->u3.e1.EndOfAllocation == 1) &&
982 (ExQueryDepthSList(&MiNonPagedPoolSListHead) < MiNonPagedPoolSListMaximum))
983 {
984 InterlockedPushEntrySList(&MiNonPagedPoolSListHead, StartingVa);
985 return 1;
986 }
987
988 //
989 // Loop until we find the last PTE
990 //
991 while (Pfn1->u3.e1.EndOfAllocation == 0)
992 {
993 //
994 // Keep going
995 //
996 PointerPte++;
997 Pfn1 = MiGetPfnEntry(PointerPte->u.Hard.PageFrameNumber);
998 }
999
1000 //
1001 // Now we know how many pages we have
1002 //
1003 NumberOfPages = (PFN_COUNT)(PointerPte - StartPte + 1);
1004
1005 //
1006 // Acquire the nonpaged pool lock
1007 //
1008 OldIrql = KeAcquireQueuedSpinLock(LockQueueMmNonPagedPoolLock);
1009
1010 //
1011 // Mark the first and last PTEs as not part of an allocation anymore
1012 //
1013 StartPfn->u3.e1.StartOfAllocation = 0;
1014 Pfn1->u3.e1.EndOfAllocation = 0;
1015
1016 //
1017 // Assume we will free as many pages as the allocation was
1018 //
1019 FreePages = NumberOfPages;
1020
1021 //
1022 // Peek one page past the end of the allocation
1023 //
1024 PointerPte++;
1025
1026 //
1027 // Guard against going past initial nonpaged pool
1028 //
1029 if (MiGetPfnEntryIndex(Pfn1) == MiEndOfInitialPoolFrame)
1030 {
1031 //
1032 // This page is on the outskirts of initial nonpaged pool, so ignore it
1033 //
1034 Pfn1 = NULL;
1035 }
1036 else
1037 {
1038 /* Sanity check */
1039 ASSERT((ULONG_PTR)StartingVa + NumberOfPages <= (ULONG_PTR)MmNonPagedPoolEnd);
1040
1041 /* Check if protected pool is enabled */
1042 if (MmProtectFreedNonPagedPool)
1043 {
1044 /* The freed block will be merged, it must be made accessible */
1045 MiUnProtectFreeNonPagedPool(MiPteToAddress(PointerPte), 0);
1046 }
1047
1048 //
1049 // Otherwise, our entire allocation must've fit within the initial non
1050 // paged pool, or the expansion nonpaged pool, so get the PFN entry of
1051 // the next allocation
1052 //
1053 if (PointerPte->u.Hard.Valid == 1)
1054 {
1055 //
1056 // It's either expansion or initial: get the PFN entry
1057 //
1058 Pfn1 = MiGetPfnEntry(PointerPte->u.Hard.PageFrameNumber);
1059 }
1060 else
1061 {
1062 //
1063 // This means we've reached the guard page that protects the end of
1064 // the expansion nonpaged pool
1065 //
1066 Pfn1 = NULL;
1067 }
1068
1069 }
1070
1071 //
1072 // Check if this allocation actually exists
1073 //
1074 if ((Pfn1) && (Pfn1->u3.e1.StartOfAllocation == 0))
1075 {
1076 //
1077 // It doesn't, so we should actually locate a free entry descriptor
1078 //
1079 FreeEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)StartingVa +
1080 (NumberOfPages << PAGE_SHIFT));
1081 ASSERT(FreeEntry->Signature == MM_FREE_POOL_SIGNATURE);
1082 ASSERT(FreeEntry->Owner == FreeEntry);
1083
1084 /* Consume this entry's pages */
1085 FreePages += FreeEntry->Size;
1086
1087 /* Remove the item from the list, depending if pool is protected */
1088 MmProtectFreedNonPagedPool ?
1089 MiProtectedPoolRemoveEntryList(&FreeEntry->List) :
1090 RemoveEntryList(&FreeEntry->List);
1091 }
1092
1093 //
1094 // Now get the official free entry we'll create for the caller's allocation
1095 //
1096 FreeEntry = StartingVa;
1097
1098 //
1099 // Check if the our allocation is the very first page
1100 //
1101 if (MiGetPfnEntryIndex(StartPfn) == MiStartOfInitialPoolFrame)
1102 {
1103 //
1104 // Then we can't do anything or we'll risk underflowing
1105 //
1106 Pfn1 = NULL;
1107 }
1108 else
1109 {
1110 //
1111 // Otherwise, get the PTE for the page right before our allocation
1112 //
1113 PointerPte -= NumberOfPages + 1;
1114
1115 /* Check if protected pool is enabled */
1116 if (MmProtectFreedNonPagedPool)
1117 {
1118 /* The freed block will be merged, it must be made accessible */
1119 MiUnProtectFreeNonPagedPool(MiPteToAddress(PointerPte), 0);
1120 }
1121
1122 /* Check if this is valid pool, or a guard page */
1123 if (PointerPte->u.Hard.Valid == 1)
1124 {
1125 //
1126 // It's either expansion or initial nonpaged pool, get the PFN entry
1127 //
1128 Pfn1 = MiGetPfnEntry(PointerPte->u.Hard.PageFrameNumber);
1129 }
1130 else
1131 {
1132 //
1133 // We must've reached the guard page, so don't risk touching it
1134 //
1135 Pfn1 = NULL;
1136 }
1137 }
1138
1139 //
1140 // Check if there is a valid PFN entry for the page before the allocation
1141 // and then check if this page was actually the end of an allocation.
1142 // If it wasn't, then we know for sure it's a free page
1143 //
1144 if ((Pfn1) && (Pfn1->u3.e1.EndOfAllocation == 0))
1145 {
1146 //
1147 // Get the free entry descriptor for that given page range
1148 //
1149 FreeEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)StartingVa - PAGE_SIZE);
1150 ASSERT(FreeEntry->Signature == MM_FREE_POOL_SIGNATURE);
1151 FreeEntry = FreeEntry->Owner;
1152
1153 /* Check if protected pool is enabled */
1154 if (MmProtectFreedNonPagedPool)
1155 {
1156 /* The freed block will be merged, it must be made accessible */
1157 MiUnProtectFreeNonPagedPool(FreeEntry, 0);
1158 }
1159
1160 //
1161 // Check if the entry is small enough to be indexed on a free list
1162 // If it is, we'll want to re-insert it, since we're about to
1163 // collapse our pages on top of it, which will change its count
1164 //
1165 if (FreeEntry->Size < (MI_MAX_FREE_PAGE_LISTS - 1))
1166 {
1167 /* Remove the item from the list, depending if pool is protected */
1168 MmProtectFreedNonPagedPool ?
1169 MiProtectedPoolRemoveEntryList(&FreeEntry->List) :
1170 RemoveEntryList(&FreeEntry->List);
1171
1172 //
1173 // Update its size
1174 //
1175 FreeEntry->Size += FreePages;
1176
1177 //
1178 // And now find the new appropriate list to place it in
1179 //
1180 i = (ULONG)(FreeEntry->Size - 1);
1181 if (i >= MI_MAX_FREE_PAGE_LISTS) i = MI_MAX_FREE_PAGE_LISTS - 1;
1182
1183 /* Insert the entry into the free list head, check for prot. pool */
1184 MmProtectFreedNonPagedPool ?
1185 MiProtectedPoolInsertList(&MmNonPagedPoolFreeListHead[i], &FreeEntry->List, TRUE) :
1186 InsertTailList(&MmNonPagedPoolFreeListHead[i], &FreeEntry->List);
1187 }
1188 else
1189 {
1190 //
1191 // Otherwise, just combine our free pages into this entry
1192 //
1193 FreeEntry->Size += FreePages;
1194 }
1195 }
1196
1197 //
1198 // Check if we were unable to do any compaction, and we'll stick with this
1199 //
1200 if (FreeEntry == StartingVa)
1201 {
1202 //
1203 // Well, now we are a free entry. At worse we just have our newly freed
1204 // pages, at best we have our pages plus whatever entry came after us
1205 //
1206 FreeEntry->Size = FreePages;
1207
1208 //
1209 // Find the appropriate list we should be on
1210 //
1211 i = FreeEntry->Size - 1;
1212 if (i >= MI_MAX_FREE_PAGE_LISTS) i = MI_MAX_FREE_PAGE_LISTS - 1;
1213
1214 /* Insert the entry into the free list head, check for prot. pool */
1215 MmProtectFreedNonPagedPool ?
1216 MiProtectedPoolInsertList(&MmNonPagedPoolFreeListHead[i], &FreeEntry->List, TRUE) :
1217 InsertTailList(&MmNonPagedPoolFreeListHead[i], &FreeEntry->List);
1218 }
1219
1220 //
1221 // Just a sanity check
1222 //
1223 ASSERT(FreePages != 0);
1224
1225 //
1226 // Get all the pages between our allocation and its end. These will all now
1227 // become free page chunks.
1228 //
1229 NextEntry = StartingVa;
1230 LastEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)NextEntry + (FreePages << PAGE_SHIFT));
1231 do
1232 {
1233 //
1234 // Link back to the parent free entry, and keep going
1235 //
1236 NextEntry->Owner = FreeEntry;
1237 NextEntry->Signature = MM_FREE_POOL_SIGNATURE;
1238 NextEntry = (PMMFREE_POOL_ENTRY)((ULONG_PTR)NextEntry + PAGE_SIZE);
1239 } while (NextEntry != LastEntry);
1240
1241 /* Is freed non paged pool protected? */
1242 if (MmProtectFreedNonPagedPool)
1243 {
1244 /* Protect the freed pool! */
1245 MiProtectFreeNonPagedPool(FreeEntry, FreeEntry->Size);
1246 }
1247
1248 //
1249 // We're done, release the lock and let the caller know how much we freed
1250 //
1251 KeReleaseQueuedSpinLock(LockQueueMmNonPagedPoolLock, OldIrql);
1252 return NumberOfPages;
1253 }
1254
1255
1256 BOOLEAN
1257 NTAPI
1258 MiRaisePoolQuota(IN POOL_TYPE PoolType,
1259 IN ULONG CurrentMaxQuota,
1260 OUT PULONG NewMaxQuota)
1261 {
1262 //
1263 // Not implemented
1264 //
1265 UNIMPLEMENTED;
1266 *NewMaxQuota = CurrentMaxQuota + 65536;
1267 return TRUE;
1268 }
1269
1270 NTSTATUS
1271 NTAPI
1272 MiInitializeSessionPool(VOID)
1273 {
1274 PMMPTE PointerPde, PointerPte, LastPte, LastPde;
1275 PFN_NUMBER PageFrameIndex, PdeCount;
1276 PPOOL_DESCRIPTOR PoolDescriptor;
1277 PMM_SESSION_SPACE SessionGlobal;
1278 PMM_PAGED_POOL_INFO PagedPoolInfo;
1279 NTSTATUS Status;
1280 ULONG Index, PoolSize, BitmapSize;
1281 PAGED_CODE();
1282
1283 /* Lock session pool */
1284 SessionGlobal = MmSessionSpace->GlobalVirtualAddress;
1285 KeInitializeGuardedMutex(&SessionGlobal->PagedPoolMutex);
1286
1287 /* Setup a valid pool descriptor */
1288 PoolDescriptor = &MmSessionSpace->PagedPool;
1289 ExInitializePoolDescriptor(PoolDescriptor,
1290 PagedPoolSession,
1291 0,
1292 0,
1293 &SessionGlobal->PagedPoolMutex);
1294
1295 /* Setup the pool addresses */
1296 MmSessionSpace->PagedPoolStart = (PVOID)MiSessionPoolStart;
1297 MmSessionSpace->PagedPoolEnd = (PVOID)((ULONG_PTR)MiSessionPoolEnd - 1);
1298 DPRINT1("Session Pool Start: 0x%p End: 0x%p\n",
1299 MmSessionSpace->PagedPoolStart, MmSessionSpace->PagedPoolEnd);
1300
1301 /* Reset all the counters */
1302 PagedPoolInfo = &MmSessionSpace->PagedPoolInfo;
1303 PagedPoolInfo->PagedPoolCommit = 0;
1304 PagedPoolInfo->PagedPoolHint = 0;
1305 PagedPoolInfo->AllocatedPagedPool = 0;
1306
1307 /* Compute PDE and PTE addresses */
1308 PointerPde = MiAddressToPde(MmSessionSpace->PagedPoolStart);
1309 PointerPte = MiAddressToPte(MmSessionSpace->PagedPoolStart);
1310 LastPde = MiAddressToPde(MmSessionSpace->PagedPoolEnd);
1311 LastPte = MiAddressToPte(MmSessionSpace->PagedPoolEnd);
1312
1313 /* Write them down */
1314 MmSessionSpace->PagedPoolBasePde = PointerPde;
1315 PagedPoolInfo->FirstPteForPagedPool = PointerPte;
1316 PagedPoolInfo->LastPteForPagedPool = LastPte;
1317 PagedPoolInfo->NextPdeForPagedPoolExpansion = PointerPde + 1;
1318
1319 /* Zero the PDEs */
1320 PdeCount = LastPde - PointerPde;
1321 RtlZeroMemory(PointerPde, (PdeCount + 1) * sizeof(MMPTE));
1322
1323 /* Initialize the PFN for the PDE */
1324 Status = MiInitializeAndChargePfn(&PageFrameIndex,
1325 PointerPde,
1326 MmSessionSpace->SessionPageDirectoryIndex,
1327 TRUE);
1328 ASSERT(NT_SUCCESS(Status) == TRUE);
1329
1330 /* Initialize the first page table */
1331 Index = (ULONG_PTR)MmSessionSpace->PagedPoolStart - (ULONG_PTR)MmSessionBase;
1332 Index >>= 22;
1333 #ifndef _M_AMD64 // FIXME
1334 ASSERT(MmSessionSpace->PageTables[Index].u.Long == 0);
1335 MmSessionSpace->PageTables[Index] = *PointerPde;
1336 #endif
1337
1338 /* Bump up counters */
1339 InterlockedIncrementSizeT(&MmSessionSpace->NonPageablePages);
1340 InterlockedIncrementSizeT(&MmSessionSpace->CommittedPages);
1341
1342 /* Compute the size of the pool in pages, and of the bitmap for it */
1343 PoolSize = MmSessionPoolSize >> PAGE_SHIFT;
1344 BitmapSize = sizeof(RTL_BITMAP) + ((PoolSize + 31) / 32) * sizeof(ULONG);
1345
1346 /* Allocate and initialize the bitmap to track allocations */
1347 PagedPoolInfo->PagedPoolAllocationMap = ExAllocatePoolWithTag(NonPagedPool,
1348 BitmapSize,
1349 ' mM');
1350 ASSERT(PagedPoolInfo->PagedPoolAllocationMap != NULL);
1351 RtlInitializeBitMap(PagedPoolInfo->PagedPoolAllocationMap,
1352 (PULONG)(PagedPoolInfo->PagedPoolAllocationMap + 1),
1353 PoolSize);
1354
1355 /* Set all bits, but clear the first page table's worth */
1356 RtlSetAllBits(PagedPoolInfo->PagedPoolAllocationMap);
1357 RtlClearBits(PagedPoolInfo->PagedPoolAllocationMap, 0, PTE_PER_PAGE);
1358
1359 /* Allocate and initialize the bitmap to track free space */
1360 PagedPoolInfo->EndOfPagedPoolBitmap = ExAllocatePoolWithTag(NonPagedPool,
1361 BitmapSize,
1362 ' mM');
1363 ASSERT(PagedPoolInfo->EndOfPagedPoolBitmap != NULL);
1364 RtlInitializeBitMap(PagedPoolInfo->EndOfPagedPoolBitmap,
1365 (PULONG)(PagedPoolInfo->EndOfPagedPoolBitmap + 1),
1366 PoolSize);
1367
1368 /* Clear all the bits and return success */
1369 RtlClearAllBits(PagedPoolInfo->EndOfPagedPoolBitmap);
1370 return STATUS_SUCCESS;
1371 }
1372
1373 /* PUBLIC FUNCTIONS ***********************************************************/
1374
1375 /*
1376 * @unimplemented
1377 */
1378 PVOID
1379 NTAPI
1380 MmAllocateMappingAddress(IN SIZE_T NumberOfBytes,
1381 IN ULONG PoolTag)
1382 {
1383 UNIMPLEMENTED;
1384 return NULL;
1385 }
1386
1387 /*
1388 * @unimplemented
1389 */
1390 VOID
1391 NTAPI
1392 MmFreeMappingAddress(IN PVOID BaseAddress,
1393 IN ULONG PoolTag)
1394 {
1395 UNIMPLEMENTED;
1396 }
1397
1398 /* EOF */