2 * COPYRIGHT: See COPYING.ARM in the top level directory
3 * PROJECT: ReactOS UEFI Boot Library
4 * FILE: boot/environ/lib/mm/descriptor.c
5 * PURPOSE: Boot Library Memory Manager Descriptor Manager
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
13 /* DATA VARIABLES ************************************************************/
15 BL_MEMORY_DESCRIPTOR MmStaticMemoryDescriptors
[512];
16 ULONG MmGlobalMemoryDescriptorCount
;
17 PBL_MEMORY_DESCRIPTOR MmGlobalMemoryDescriptors
;
18 ULONG MmGlobalMemoryDescriptorsUsed
;
19 PBL_MEMORY_DESCRIPTOR MmDynamicMemoryDescriptors
;
20 ULONG MmDynamicMemoryDescriptorCount
;
22 BL_MEMORY_TYPE MmPlatformMemoryTypePrecedence
[] =
35 /* FUNCTIONS *****************************************************************/
37 /* The order is Conventional > Other > System > Loader > Application */
40 _In_ BL_MEMORY_TYPE Type1
,
41 _In_ BL_MEMORY_TYPE Type2
44 BL_MEMORY_CLASS Class1
, Class2
;
47 /* Descriptor is free RAM -- it precedes */
48 if (Type1
== BlConventionalMemory
)
53 /* It isn't free RAM, but the comparator is -- it succeeds it */
54 if (Type2
== BlConventionalMemory
)
59 /* Descriptor is not system, application, or loader class -- it precedes */
60 Class1
= Type1
>> BL_MEMORY_CLASS_SHIFT
;
61 if ((Class1
!= BlSystemClass
) &&
62 (Class1
!= BlApplicationClass
) &&
63 (Class1
!= BlLoaderClass
))
68 /* It isn't one of those classes, but the comparator it -- it succeeds it */
69 Class2
= Type2
>> BL_MEMORY_CLASS_SHIFT
;
70 if ((Class2
!= BlSystemClass
) &&
71 (Class2
!= BlApplicationClass
) &&
72 (Class2
!= BlLoaderClass
))
77 /* Descriptor is system class */
78 if (Class1
== BlSystemClass
)
80 /* And so is the other guy... */
81 if (Class2
== BlSystemClass
)
86 /* Scan for the descriptor's system precedence index */
89 if (MmPlatformMemoryTypePrecedence
[j
] == Type1
)
93 } while (++j
< RTL_NUMBER_OF(MmPlatformMemoryTypePrecedence
));
95 /* Use an invalid index if one wasn't found */
96 if (j
== RTL_NUMBER_OF(MmPlatformMemoryTypePrecedence
))
101 /* Now scan for the comparator's system precedence index */
102 while (MmPlatformMemoryTypePrecedence
[i
] != Type2
)
104 /* Use an invalid index if one wasn't found */
105 if (++i
>= RTL_NUMBER_OF(MmPlatformMemoryTypePrecedence
))
112 /* Does the current have a valid index? */
115 /* Yes, what about the comparator? */
118 /* Let the indexes fight! */
122 /* Succeed the comparator, its index is unknown */
127 /* The comparator isn't system, so it precedes it */
131 /* Descriptor is not system class, but comparator is -- it succeeds it */
132 if (Class2
== BlSystemClass
)
137 /* Descriptor is loader class -- it precedes */
138 if (Class1
== BlLoaderClass
)
143 /* It isn't loader class -- if the other guy is, succeed it */
144 return Class2
!= BlLoaderClass
;
148 MmMdpSwitchToDynamicDescriptors (
152 EfiPrintf(L
"Dynamic switch NOT SUPPORTED!!!\r\n");
158 _In_ PBL_MEMORY_DESCRIPTOR MemoryDescriptor
163 /* Check if this is a valid static descriptor */
164 if (((MmDynamicMemoryDescriptors
) &&
165 (MemoryDescriptor
>= MmDynamicMemoryDescriptors
) &&
166 (MemoryDescriptor
< (MmDynamicMemoryDescriptors
+ MmDynamicMemoryDescriptorCount
))) ||
167 ((MemoryDescriptor
>= MmStaticMemoryDescriptors
) && (MemoryDescriptor
< &MmStaticMemoryDescriptors
[511])))
169 /* It's a global/static descriptor, so just zero it */
170 RtlZeroMemory(MemoryDescriptor
, sizeof(BL_MEMORY_DESCRIPTOR
));
171 Status
= STATUS_SUCCESS
;
175 /* It's a dynamic descriptor, so free it */
176 EfiPrintf(L
"Freeing dynamic descriptors not yet supported\r\n");
178 Status
= STATUS_NOT_IMPLEMENTED
;
186 MmMdpSaveCurrentListPointer (
187 _In_ PBL_MEMORY_DESCRIPTOR_LIST MdList
,
188 _In_ PLIST_ENTRY Current
191 /* Make sure that this is not a global descriptor and not the first one */
192 if (((Current
< &MmGlobalMemoryDescriptors
->ListEntry
) ||
193 (Current
>= &MmGlobalMemoryDescriptors
[MmGlobalMemoryDescriptorCount
].ListEntry
)) &&
194 (Current
!= MdList
->First
))
196 /* Save this as the current pointer */
197 MdList
->This
= Current
;
203 _In_ PBL_MEMORY_DESCRIPTOR_LIST MdList
206 PLIST_ENTRY First
, NextEntry
;
209 /* Iterate the list */
210 for (Count
= 0, First
= MdList
->First
, NextEntry
= First
->Flink
;
212 NextEntry
= NextEntry
->Flink
, Count
++);
214 /* Return the count */
220 _In_ PBL_MEMORY_DESCRIPTOR_LIST MdList
,
222 _In_ PLIST_ENTRY ListHead
225 /* Check if a list was specified */
229 MdList
->First
= ListHead
;
233 /* Otherwise, use the internal, built-in list */
234 InitializeListHead(&MdList
->ListHead
);
235 MdList
->First
= &MdList
->ListHead
;
241 /* Initialize current iterator to nothing */
247 _In_ PBL_MEMORY_DESCRIPTOR_LIST DestinationList
,
248 _In_ PBL_MEMORY_DESCRIPTOR_LIST SourceList
,
249 _In_opt_ PBL_MEMORY_DESCRIPTOR ListDescriptor
,
250 _Out_ PULONG ActualCount
,
257 PLIST_ENTRY First
, NextEntry
;
258 PBL_MEMORY_DESCRIPTOR Descriptor
;
260 /* Both parameters must be present */
261 if (!(DestinationList
) || !(SourceList
))
263 return STATUS_INVALID_PARAMETER
;
267 Status
= STATUS_SUCCESS
;
269 /* Check if a descriptor is being used to store the list */
272 /* See how big it is */
273 Flags
|= BL_MM_ADD_DESCRIPTOR_NEVER_COALESCE_FLAG
;
278 /* We are using our internal descriptors instead */
279 Used
= &MmGlobalMemoryDescriptorsUsed
;
280 ++MmDescriptorCallTreeCount
;
282 /* Use as many as are available */
283 Count
= MmGlobalMemoryDescriptorCount
;
284 ListDescriptor
= MmGlobalMemoryDescriptors
;
287 /* Never truncate descriptors during a list copy */
288 Flags
|= BL_MM_ADD_DESCRIPTOR_NEVER_TRUNCATE_FLAG
;
290 /* Iterate through the list */
291 First
= SourceList
->First
;
292 NextEntry
= First
->Flink
;
293 while ((NextEntry
!= First
) && (NT_SUCCESS(Status
)))
295 /* Make sure there's still space */
298 Status
= STATUS_NO_MEMORY
;
302 /* Get the current descriptor */
303 Descriptor
= CONTAINING_RECORD(NextEntry
,
304 BL_MEMORY_DESCRIPTOR
,
307 /* Copy it into one of the descriptors we have */
308 RtlCopyMemory(&ListDescriptor
[*Used
],
310 sizeof(*Descriptor
));
312 /* Add it to the list we have */
313 Status
= MmMdAddDescriptorToList(DestinationList
,
314 &ListDescriptor
[*Used
],
318 /* Move to the next entry */
319 NextEntry
= NextEntry
->Flink
;
322 /* Check if the global descriptors were used */
323 if (ListDescriptor
== MmGlobalMemoryDescriptors
)
325 /* Unwind our usage */
326 MmMdFreeGlobalDescriptors();
327 --MmDescriptorCallTreeCount
;
330 /* Return back to caller */
335 MmMdRemoveDescriptorFromList (
336 _In_ PBL_MEMORY_DESCRIPTOR_LIST MdList
,
337 _In_ PBL_MEMORY_DESCRIPTOR Entry
340 /* Remove the entry */
341 RemoveEntryList(&Entry
->ListEntry
);
343 /* Check if this was the current link */
344 if (MdList
->This
== &Entry
->ListEntry
)
346 /* Remove the current link and set the next one */
348 MmMdpSaveCurrentListPointer(MdList
, Entry
->ListEntry
.Blink
);
354 _In_ PBL_MEMORY_DESCRIPTOR_LIST MdList
357 PLIST_ENTRY FirstEntry
, NextEntry
;
358 PBL_MEMORY_DESCRIPTOR Entry
;
360 /* Go over every descriptor from the top */
361 FirstEntry
= MdList
->First
;
362 NextEntry
= FirstEntry
->Flink
;
363 while (NextEntry
!= FirstEntry
)
365 /* Remove and free each one */
366 Entry
= CONTAINING_RECORD(NextEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
367 NextEntry
= NextEntry
->Flink
;
368 MmMdRemoveDescriptorFromList(MdList
, Entry
);
369 MmMdFreeDescriptor(Entry
);
373 PBL_MEMORY_DESCRIPTOR
374 MmMdInitByteGranularDescriptor (
376 _In_ BL_MEMORY_TYPE Type
,
377 _In_ ULONGLONG BasePage
,
378 _In_ ULONGLONG VirtualPage
,
379 _In_ ULONGLONG PageCount
382 PBL_MEMORY_DESCRIPTOR MemoryDescriptor
;
384 /* If we're out of descriptors, bail out */
385 if (MmGlobalMemoryDescriptorsUsed
>= MmGlobalMemoryDescriptorCount
)
387 EfiPrintf(L
"Out of descriptors!\r\n");
391 /* Take one of the available descriptors and fill it out */
392 MemoryDescriptor
= &MmGlobalMemoryDescriptors
[MmGlobalMemoryDescriptorsUsed
];
393 MemoryDescriptor
->BaseAddress
= BasePage
;
394 MemoryDescriptor
->VirtualPage
= VirtualPage
;
395 MemoryDescriptor
->PageCount
= PageCount
;
396 MemoryDescriptor
->Flags
= Flags
;
397 MemoryDescriptor
->Type
= Type
;
398 InitializeListHead(&MemoryDescriptor
->ListEntry
);
400 /* Increment the count and return the descriptor */
401 MmGlobalMemoryDescriptorsUsed
++;
402 return MemoryDescriptor
;
406 MmMdpTruncateDescriptor (
407 __in PBL_MEMORY_DESCRIPTOR_LIST MdList
,
408 __in PBL_MEMORY_DESCRIPTOR MemoryDescriptor
,
412 PBL_MEMORY_DESCRIPTOR NextDescriptor
, PreviousDescriptor
;
413 PLIST_ENTRY NextEntry
, PreviousEntry
;
414 ULONGLONG EndPage
, PreviousEndPage
;// , NextEndPage;
416 /* Get the next descriptor */
417 NextEntry
= MemoryDescriptor
->ListEntry
.Flink
;
418 NextDescriptor
= CONTAINING_RECORD(NextEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
420 /* Get the previous descriptor */
421 PreviousEntry
= MemoryDescriptor
->ListEntry
.Blink
;
422 PreviousDescriptor
= CONTAINING_RECORD(PreviousEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
424 /* Calculate end pages */
425 EndPage
= MemoryDescriptor
->BasePage
+ MemoryDescriptor
->PageCount
;
426 //NextEndPage = NextDescriptor->BasePage + NextDescriptor->PageCount;
427 PreviousEndPage
= PreviousDescriptor
->BasePage
+ PreviousDescriptor
->PageCount
;
429 /* Check for backward overlap */
430 if ((PreviousEntry
!= MdList
->First
) && (MemoryDescriptor
->BasePage
< PreviousEndPage
))
432 EfiPrintf(L
"Overlap detected -- this is unexpected on x86/x64 platforms\r\n");
435 /* Check for forward overlap */
436 if ((NextEntry
!= MdList
->First
) && (NextDescriptor
->BasePage
< EndPage
))
438 EfiPrintf(L
"Overlap detected -- this is unexpected on x86/x64 platforms\r\n");
446 MmMdpCoalesceDescriptor (
447 __in PBL_MEMORY_DESCRIPTOR_LIST MdList
,
448 __in PBL_MEMORY_DESCRIPTOR MemoryDescriptor
,
452 PBL_MEMORY_DESCRIPTOR NextDescriptor
, PreviousDescriptor
;
453 PLIST_ENTRY NextEntry
, PreviousEntry
;
454 ULONGLONG EndPage
, PreviousEndPage
, PreviousMappedEndPage
, MappedEndPage
;
456 /* Get the next descriptor */
457 NextEntry
= MemoryDescriptor
->ListEntry
.Flink
;
458 NextDescriptor
= CONTAINING_RECORD(NextEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
460 /* Get the previous descriptor */
461 PreviousEntry
= MemoryDescriptor
->ListEntry
.Blink
;
462 PreviousDescriptor
= CONTAINING_RECORD(PreviousEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
464 /* Calculate end pages */
465 EndPage
= MemoryDescriptor
->BasePage
+ MemoryDescriptor
->PageCount
;
466 MappedEndPage
= MemoryDescriptor
->BasePage
+ MemoryDescriptor
->PageCount
;
467 PreviousMappedEndPage
= PreviousDescriptor
->VirtualPage
+ PreviousDescriptor
->PageCount
;
468 PreviousEndPage
= PreviousDescriptor
->BasePage
+ PreviousDescriptor
->PageCount
;
469 PreviousMappedEndPage
= PreviousDescriptor
->VirtualPage
+ PreviousDescriptor
->PageCount
;
471 /* Check if the previous entry touches the current entry, and is compatible */
472 if ((PreviousEntry
!= MdList
->First
) &&
473 (PreviousDescriptor
->Type
== MemoryDescriptor
->Type
) &&
474 ((PreviousDescriptor
->Flags
^ MemoryDescriptor
->Flags
) & 0x1B19FFFF) &&
475 (PreviousEndPage
== MemoryDescriptor
->BasePage
) &&
476 ((!(MemoryDescriptor
->VirtualPage
) && !(PreviousDescriptor
->VirtualPage
)) ||
477 ((MemoryDescriptor
->VirtualPage
) && (PreviousDescriptor
->VirtualPage
) &&
478 (PreviousMappedEndPage
== MemoryDescriptor
->VirtualPage
))))
480 EfiPrintf(L
"Previous descriptor coalescable!\r\n");
483 /* CHeck if the current entry touches the next entry, and is compatible */
484 if ((NextEntry
!= MdList
->First
) &&
485 (NextDescriptor
->Type
== MemoryDescriptor
->Type
) &&
486 ((NextDescriptor
->Flags
^ MemoryDescriptor
->Flags
) & 0x1B19FFFF) &&
487 (EndPage
== NextDescriptor
->BasePage
) &&
488 ((!(MemoryDescriptor
->VirtualPage
) && !(PreviousDescriptor
->VirtualPage
)) ||
489 ((MemoryDescriptor
->VirtualPage
) && (PreviousDescriptor
->VirtualPage
) &&
490 (MappedEndPage
== NextDescriptor
->VirtualPage
))))
492 EfiPrintf(L
"Next descriptor coalescable!\r\n");
500 MmMdAddDescriptorToList (
501 _In_ PBL_MEMORY_DESCRIPTOR_LIST MdList
,
502 _In_ PBL_MEMORY_DESCRIPTOR MemoryDescriptor
,
506 PLIST_ENTRY ThisEntry
, FirstEntry
;
507 PBL_MEMORY_DESCRIPTOR ThisDescriptor
;
509 /* Arguments must be present */
510 if (!(MdList
) || !(MemoryDescriptor
))
512 return STATUS_INVALID_PARAMETER
;
515 /* Check if coalescing is forcefully disabled */
516 if (Flags
& BL_MM_ADD_DESCRIPTOR_NEVER_COALESCE_FLAG
)
518 /* Then we won't be coalescing */
519 Flags
&= BL_MM_ADD_DESCRIPTOR_COALESCE_FLAG
;
523 /* Coalesce if the descriptor requires it */
524 if (MemoryDescriptor
->Flags
& BlMemoryCoalesced
)
526 Flags
|= BL_MM_ADD_DESCRIPTOR_COALESCE_FLAG
;
530 /* Check if truncation is forcefully disabled */
531 if (Flags
& BL_MM_ADD_DESCRIPTOR_NEVER_TRUNCATE_FLAG
)
533 Flags
&= ~BL_MM_ADD_DESCRIPTOR_TRUNCATE_FLAG
;
536 /* Update the current list pointer if the descriptor requires it */
537 if (MemoryDescriptor
->Flags
& BlMemoryUpdate
)
539 Flags
|= BL_MM_ADD_DESCRIPTOR_UPDATE_LIST_POINTER_FLAG
;
542 /* Get the current descriptor */
543 ThisEntry
= MdList
->This
;
544 ThisDescriptor
= CONTAINING_RECORD(ThisEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
546 /* Also get the first descriptor */
547 FirstEntry
= MdList
->First
;
549 /* Check if there's no current pointer, or if it's higher than the new one */
551 (MemoryDescriptor
->BaseAddress
<= ThisDescriptor
->BaseAddress
))
553 /* Start at the first descriptor instead, since current is past us */
554 ThisEntry
= FirstEntry
->Flink
;
555 ThisDescriptor
= CONTAINING_RECORD(ThisEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
558 /* Loop until we find the right location to insert */
561 /* Have we gotten back to the first entry? */
562 if (ThisEntry
== FirstEntry
)
564 /* Then we didn't find a good match, so insert it right here */
565 InsertTailList(FirstEntry
, &MemoryDescriptor
->ListEntry
);
567 /* Do we have to truncate? */
568 if (Flags
& BL_MM_ADD_DESCRIPTOR_TRUNCATE_FLAG
)
570 /* Do it and then exit */
571 if (MmMdpTruncateDescriptor(MdList
, MemoryDescriptor
, Flags
))
573 return STATUS_SUCCESS
;
577 /* Do we have to coalesce? */
578 if (Flags
& BL_MM_ADD_DESCRIPTOR_COALESCE_FLAG
)
580 /* Do it and then exit */
581 if (MmMdpCoalesceDescriptor(MdList
, MemoryDescriptor
, Flags
))
583 return STATUS_SUCCESS
;
587 /* Do we have to update the current pointer? */
588 if (Flags
& BL_MM_ADD_DESCRIPTOR_UPDATE_LIST_POINTER_FLAG
)
591 MmMdpSaveCurrentListPointer(MdList
, &MemoryDescriptor
->ListEntry
);
595 return STATUS_SUCCESS
;
598 /* Is the new descriptor below this address, and has precedence over it? */
599 if ((MemoryDescriptor
->BaseAddress
< ThisDescriptor
->BaseAddress
) &&
600 (MmMdpHasPrecedence(MemoryDescriptor
->Type
, ThisDescriptor
->Type
)))
602 /* Then insert right here */
603 InsertTailList(ThisEntry
, &MemoryDescriptor
->ListEntry
);
604 return STATUS_SUCCESS
;
607 /* Try the next descriptor */
608 ThisEntry
= ThisEntry
->Flink
;
609 ThisDescriptor
= CONTAINING_RECORD(ThisEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
614 MmMdRemoveRegionFromMdlEx (
615 __in PBL_MEMORY_DESCRIPTOR_LIST MdList
,
617 __in ULONGLONG BasePage
,
618 __in ULONGLONG PageCount
,
619 __in PBL_MEMORY_DESCRIPTOR_LIST NewMdList
622 BOOLEAN HaveNewList
, UseVirtualPage
;
624 PLIST_ENTRY ListHead
, NextEntry
;
625 PBL_MEMORY_DESCRIPTOR Descriptor
;
626 BL_MEMORY_DESCRIPTOR NewDescriptor
;
627 ULONGLONG RegionSize
;
628 ULONGLONG FoundBasePage
, FoundEndPage
, FoundPageCount
, EndPage
;
630 /* Set initial status */
631 Status
= STATUS_SUCCESS
;
633 /* Check if removed descriptors should go into a new list */
634 if (NewMdList
!= NULL
)
637 MmMdInitializeListHead(NewMdList
);
638 NewMdList
->Type
= MdList
->Type
;
640 /* Remember for later */
649 /* Is the region being removed physical? */
650 UseVirtualPage
= FALSE
;
651 if (!(Flags
& BL_MM_REMOVE_VIRTUAL_REGION_FLAG
))
653 /* Is this a list of virtual descriptors? */
654 if (MdList
->Type
== BlMdVirtual
)
656 /* Request is nonsensical, fail */
657 Status
= STATUS_INVALID_PARAMETER
;
663 /* Is this a list of physical descriptors? */
664 if (MdList
->Type
== BlMdPhysical
)
666 /* We'll have to use the virtual page instead */
667 UseVirtualPage
= TRUE
;
672 ListHead
= MdList
->First
;
673 NextEntry
= ListHead
->Flink
;
674 while (NextEntry
!= ListHead
)
676 /* Get the descriptor */
677 Descriptor
= CONTAINING_RECORD(NextEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
679 /* Extract range details */
680 FoundBasePage
= UseVirtualPage
? Descriptor
->VirtualPage
: Descriptor
->BasePage
;
681 FoundPageCount
= Descriptor
->PageCount
;
682 FoundEndPage
= FoundBasePage
+ FoundPageCount
;
683 EndPage
= PageCount
+ BasePage
;
685 /* Make a copy of the original descriptor */
686 RtlCopyMemory(&NewDescriptor
, NextEntry
, sizeof(NewDescriptor
));
688 /* Check if the region to be removed starts after the found region starts */
689 if ((BasePage
> FoundBasePage
) || (FoundBasePage
>= EndPage
))
691 /* Check if the region ends after the found region */
692 if ((BasePage
>= FoundEndPage
) || (FoundEndPage
> EndPage
))
694 /* Check if the found region starts after the region or ends before the region */
695 if ((FoundBasePage
>= BasePage
) || (EndPage
>= FoundEndPage
))
697 /* This descriptor doesn't cover any part of the range -- nothing to do */
702 /* This descriptor fully covers the entire allocation */
703 FoundBasePage
= Descriptor
->BasePage
;
704 FoundPageCount
= BasePage
- FoundBasePage
;
706 /* This is how many pages we will eat away from the descriptor */
707 RegionSize
= FoundPageCount
+ PageCount
;
709 /* Update the descriptor to account for the consumed pages */
710 Descriptor
->BasePage
+= RegionSize
;
711 Descriptor
->PageCount
-= RegionSize
;
712 if (Descriptor
->VirtualPage
)
714 Descriptor
->VirtualPage
+= RegionSize
;
717 /* Initialize a descriptor for the start of the region */
718 Descriptor
= MmMdInitByteGranularDescriptor(Descriptor
->Flags
,
721 Descriptor
->VirtualPage
,
725 Status
= STATUS_NO_MEMORY
;
729 /* Add it into the list */
730 Status
= MmMdAddDescriptorToList(MdList
, Descriptor
, Flags
);
731 if (!NT_SUCCESS(Status
))
733 Status
= STATUS_NO_MEMORY
;
740 /* This descriptor contains the entire allocation */
741 RegionSize
= FoundEndPage
- BasePage
;
742 Descriptor
->PageCount
-= RegionSize
;
746 NextEntry
= NextEntry
->Flink
;
751 * This descriptor contains the end of the allocation. It may:
753 * - Contain the full allocation (i.e.: the start is aligned)
754 * - Contain parts of the end of the allocation (i.e.: the end is beyond)
755 * - Contain the entire tail end of the allocation (i..e:the end is within)
757 * So first, figure out if we cover the entire end or not
759 if (EndPage
< FoundEndPage
)
761 /* The allocation goes past the end of this descriptor */
762 FoundEndPage
= EndPage
;
765 /* This is how many pages we will eat away from the descriptor */
766 RegionSize
= FoundEndPage
- FoundBasePage
;
768 /* Update the descriptor to account for the consumed pages */
769 Descriptor
->BasePage
+= RegionSize
;
770 Descriptor
->PageCount
-= RegionSize
;
771 if (Descriptor
->VirtualPage
)
773 Descriptor
->VirtualPage
+= RegionSize
;
776 /* Go to the next entry */
777 NextEntry
= NextEntry
->Flink
;
779 /* Check if the descriptor is now empty */
780 if (!Descriptor
->PageCount
)
783 MmMdRemoveDescriptorFromList(MdList
, Descriptor
);
784 MmMdFreeDescriptor(Descriptor
);
786 /* Check if we're supposed to insert it into a new list */
789 EfiPrintf(L
"Not yet implemented\r\n");
790 Status
= STATUS_NOT_IMPLEMENTED
;
798 /* Check for failure cleanup */
799 if (!NT_SUCCESS(Status
))
801 /* Did we have to build a new list? */
804 /* Free and re-initialize it */
805 MmMdFreeList(NewMdList
);
806 MmMdInitializeListHead(NewMdList
);
807 NewMdList
->Type
= MdList
->Type
;
814 PBL_MEMORY_DESCRIPTOR
815 MmMdFindDescriptorFromMdl (
816 _In_ PBL_MEMORY_DESCRIPTOR_LIST MdList
,
822 PLIST_ENTRY NextEntry
, ListHead
;
823 PBL_MEMORY_DESCRIPTOR Current
;
826 /* Assume physical */
829 /* Check if the caller wants physical memory */
830 if (!(Flags
& BL_MM_REMOVE_VIRTUAL_REGION_FLAG
))
832 /* Check if this is a virtual memory list */
833 if (MdList
->Type
== BlMdVirtual
)
835 /* We won't find anything */
839 else if (MdList
->Type
== BlMdPhysical
)
841 /* Otherwise, caller wants virtual, but this is a physical list */
843 NextEntry
= MdList
->First
->Flink
;
846 /* Check if this is a physical search */
849 /* Check if we can use the current pointer */
850 NextEntry
= MdList
->This
;
853 /* We can't -- start at the beginning */
854 NextEntry
= MdList
->First
->Flink
;
858 /* If the page is below the current pointer, restart */
859 Current
= CONTAINING_RECORD(NextEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
860 if (Page
< Current
->BasePage
)
862 NextEntry
= MdList
->First
->Flink
;
867 /* Loop the list of descriptors */
868 ListHead
= MdList
->First
;
869 while (NextEntry
!= ListHead
)
871 /* Get the current one */
872 Current
= CONTAINING_RECORD(NextEntry
, BL_MEMORY_DESCRIPTOR
, ListEntry
);
874 /* Check if we are looking for virtual memory */
877 /* Use the base address */
878 BasePage
= Current
->VirtualPage
;
883 BasePage
= Current
->BasePage
;
886 /* If this is a virtual descriptor, make sure it has a base address */
887 if ((!(IsVirtual
) || (BasePage
)) &&
888 (BasePage
<= Page
) &&
889 (Page
< (BasePage
+ Current
->PageCount
)))
891 /* The descriptor fits the page being requested */
895 /* Try the next one */
896 NextEntry
= NextEntry
->Flink
;
899 /* Nothing found if we're here */
903 PBL_MEMORY_DESCRIPTOR
905 _In_ ULONG WhichList
,
910 PBL_MEMORY_DESCRIPTOR FoundDescriptor
;
912 /* Check if the caller is looking for mapped, allocated memory */
913 if (WhichList
& BL_MM_INCLUDE_MAPPED_ALLOCATED
)
915 /* Find a descriptor in that list */
916 FoundDescriptor
= MmMdFindDescriptorFromMdl(&MmMdlMappedAllocated
, Flags
, Page
);
920 return FoundDescriptor
;
924 /* Check if the caller is looking for mapped, unallocated memory */
925 if (WhichList
& BL_MM_INCLUDE_MAPPED_UNALLOCATED
)
927 /* Find a descriptor in that list */
928 FoundDescriptor
= MmMdFindDescriptorFromMdl(&MmMdlMappedUnallocated
, Flags
, Page
);
932 return FoundDescriptor
;
936 /* Check if the caller is looking for unmapped, allocated memory */
937 if (WhichList
& BL_MM_INCLUDE_UNMAPPED_ALLOCATED
)
939 /* Find a descriptor in that list */
940 FoundDescriptor
= MmMdFindDescriptorFromMdl(&MmMdlUnmappedAllocated
, Flags
, Page
);
944 return FoundDescriptor
;
948 /* Check if the caller is looking for unmapped, unallocated memory */
949 if (WhichList
& BL_MM_INCLUDE_UNMAPPED_UNALLOCATED
)
951 /* Find a descriptor in that list */
952 FoundDescriptor
= MmMdFindDescriptorFromMdl(&MmMdlUnmappedUnallocated
, Flags
, Page
);
956 return FoundDescriptor
;
960 /* Check if the caller is looking for reserved, allocated memory */
961 if (WhichList
& BL_MM_INCLUDE_RESERVED_ALLOCATED
)
963 /* Find a descriptor in that list */
964 FoundDescriptor
= MmMdFindDescriptorFromMdl(&MmMdlReservedAllocated
, Flags
, Page
);
968 return FoundDescriptor
;
972 /* Check if the caller is looking for bad memory */
973 if (WhichList
& BL_MM_INCLUDE_BAD_MEMORY
)
975 /* Find a descriptor in that list */
976 FoundDescriptor
= MmMdFindDescriptorFromMdl(&MmMdlBadMemory
, Flags
, Page
);
980 return FoundDescriptor
;
984 /* Check if the caller is looking for truncated memory */
985 if (WhichList
& BL_MM_INCLUDE_TRUNCATED_MEMORY
)
987 /* Find a descriptor in that list */
988 FoundDescriptor
= MmMdFindDescriptorFromMdl(&MmMdlTruncatedMemory
, Flags
, Page
);
992 return FoundDescriptor
;
996 /* Check if the caller is looking for persistent memory */
997 if (WhichList
& BL_MM_INCLUDE_PERSISTENT_MEMORY
)
999 /* Find a descriptor in that list */
1000 FoundDescriptor
= MmMdFindDescriptorFromMdl(&MmMdlPersistentMemory
, Flags
, Page
);
1001 if (FoundDescriptor
)
1004 return FoundDescriptor
;
1008 /* Nothing if we got here */
1013 MmMdFindSatisfyingRegion (
1014 _In_ PBL_MEMORY_DESCRIPTOR Descriptor
,
1015 _Out_ PBL_MEMORY_DESCRIPTOR NewDescriptor
,
1016 _In_ ULONGLONG Pages
,
1017 _In_ PBL_ADDRESS_RANGE BaseRange
,
1018 _In_ PBL_ADDRESS_RANGE VirtualRange
,
1019 _In_ BOOLEAN TopDown
,
1020 _In_ BL_MEMORY_TYPE MemoryType
,
1022 _In_ ULONG Alignment
1025 ULONGLONG BaseMin
, BaseMax
;
1026 ULONGLONG VirtualPage
, BasePage
;
1028 /* Extract the minimum and maximum range */
1029 BaseMin
= BaseRange
->Minimum
;
1030 BaseMax
= BaseRange
->Maximum
;
1032 /* Don't go below where the descriptor starts */
1033 if (BaseMin
< Descriptor
->BasePage
)
1035 BaseMin
= Descriptor
->BasePage
;
1038 /* Don't go beyond where the descriptor ends */
1039 if (BaseMax
> (Descriptor
->BasePage
+ Descriptor
->PageCount
- 1))
1041 BaseMax
= (Descriptor
->BasePage
+ Descriptor
->PageCount
- 1);
1044 /* Check for start overflow */
1045 if (BaseMin
> BaseMax
)
1050 /* Align the base as required */
1053 BaseMin
= ALIGN_UP_BY(BaseMin
, Alignment
);
1056 /* Check for range overflow */
1057 if (((BaseMin
+ Pages
- 1) < BaseMin
) || ((BaseMin
+ Pages
- 1) > BaseMax
))
1062 /* Check if this was a top-down request */
1065 /* Then get the highest page possible */
1066 BasePage
= BaseMax
- Pages
+ 1;
1069 /* Align it as needed */
1070 BasePage
= ALIGN_DOWN_BY(BasePage
, Alignment
);
1075 /* Otherwise, get the lowest page possible */
1079 /* If a virtual address range was passed in, this must be a virtual descriptor */
1080 if (((VirtualRange
->Minimum
) || (VirtualRange
->Maximum
)) &&
1081 !(Descriptor
->VirtualPage
))
1086 /* Any mapped page already? */
1087 if (Descriptor
->VirtualPage
)
1089 EfiPrintf(L
"Virtual memory not yet supported\r\n");
1094 /* Nothing to worry about */
1098 /* Bail out if the memory type attributes don't match */
1099 if ((((Flags
& 0xFF) & (Descriptor
->Flags
& 0xFF)) != (Flags
& 0xFF)) ||
1100 (((Flags
& 0xFF00) & (Descriptor
->Flags
& 0xFF00)) != (Flags
& 0xFF00)))
1102 EfiPrintf(L
"Incorrect memory attributes\r\n");
1106 /* Bail out if the allocation flags don't match */
1107 if (((Flags
^ Descriptor
->Flags
) & (BlMemoryRuntime
| BlMemoryBelow1MB
| BlMemoryLargePages
)))
1109 //EfiPrintf(L"Incorrect memory allocation flags\r\n");
1113 /* Bail out if the type doesn't match */
1114 if (Descriptor
->Type
!= MemoryType
)
1116 //EfiPrintf(L"Incorrect descriptor type: %lx %lx\r\n", Descriptor->Type, MemoryType);
1120 /* We have a matching region, fill out the descriptor for it */
1121 NewDescriptor
->BasePage
= BasePage
;
1122 NewDescriptor
->PageCount
= Pages
;
1123 NewDescriptor
->Type
= Descriptor
->Type
;
1124 NewDescriptor
->VirtualPage
= VirtualPage
;
1125 NewDescriptor
->Flags
= Descriptor
->Flags
;
1126 //EfiPrintf(L"Found a matching descriptor: %08I64X with %08I64X pages\r\n", BasePage, Pages);
1131 MmMdFreeGlobalDescriptors (
1135 PBL_MEMORY_DESCRIPTOR Descriptor
, OldDescriptor
;
1137 PLIST_ENTRY OldFlink
, OldBlink
;
1139 /* Make sure we're not int middle of a call using a descriptor */
1140 if (MmDescriptorCallTreeCount
!= 1)
1145 /* Loop every current global descriptor */
1146 while (Index
< MmGlobalMemoryDescriptorsUsed
)
1148 /* Does it have any valid pages? */
1149 OldDescriptor
= &MmGlobalMemoryDescriptors
[Index
];
1150 if (OldDescriptor
->PageCount
)
1152 /* Allocate a copy of it */
1153 Descriptor
= BlMmAllocateHeap(sizeof(*Descriptor
));
1159 /* Save the links */
1160 OldBlink
= OldDescriptor
->ListEntry
.Blink
;
1161 OldFlink
= OldDescriptor
->ListEntry
.Flink
;
1164 *Descriptor
= *OldDescriptor
;
1167 OldBlink
->Flink
= &Descriptor
->ListEntry
;
1168 OldFlink
->Blink
= &Descriptor
->ListEntry
;
1170 /* Zero the descriptor */
1171 RtlZeroMemory(OldDescriptor
, sizeof(*OldDescriptor
));
1178 /* All global descriptors freed */
1179 MmGlobalMemoryDescriptorsUsed
= 0;
1185 _In_ PBL_LIBRARY_PARAMETERS LibraryParameters
1188 /* Are we in phase 1? */
1191 /* Switch to dynamic descriptors if we have too many */
1192 if (LibraryParameters
->DescriptorCount
> RTL_NUMBER_OF(MmStaticMemoryDescriptors
))
1194 MmMdpSwitchToDynamicDescriptors(LibraryParameters
->DescriptorCount
);
1199 /* In phase 0, start with a pool of 512 static descriptors */
1200 MmGlobalMemoryDescriptorCount
= RTL_NUMBER_OF(MmStaticMemoryDescriptors
);
1201 MmGlobalMemoryDescriptors
= MmStaticMemoryDescriptors
;
1202 RtlZeroMemory(MmStaticMemoryDescriptors
, sizeof(MmStaticMemoryDescriptors
));
1203 MmGlobalMemoryDescriptorsUsed
= 0;