2 SMM Memory page management functions.
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "PiSmmCore.h"
16 #include <Library/SmmServicesTableLib.h>
18 #define TRUNCATE_TO_PAGES(a) ((a) >> EFI_PAGE_SHIFT)
20 LIST_ENTRY mSmmMemoryMap
= INITIALIZE_LIST_HEAD_VARIABLE (mSmmMemoryMap
);
26 #define MEMORY_MAP_SIGNATURE SIGNATURE_32('m','m','a','p')
38 LIST_ENTRY gMemoryMap
= INITIALIZE_LIST_HEAD_VARIABLE (gMemoryMap
);
41 #define MAX_MAP_DEPTH 6
44 /// mMapDepth - depth of new descriptor stack
48 /// mMapStack - space to use as temp storage to build new map descriptors
50 MEMORY_MAP mMapStack
[MAX_MAP_DEPTH
];
51 UINTN mFreeMapStack
= 0;
53 /// This list maintain the free memory map list
55 LIST_ENTRY mFreeMemoryMapEntryList
= INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList
);
58 Allocates pages from the memory map.
60 @param[in] Type The type of allocation to perform.
61 @param[in] MemoryType The type of memory to turn the allocated pages
63 @param[in] NumberOfPages The number of pages to allocate.
64 @param[out] Memory A pointer to receive the base allocated memory
66 @param[in] AddRegion If this memory is new added region.
67 @param[in] NeedGuard Flag to indicate Guard page is needed
70 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.
71 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
72 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
73 @retval EFI_SUCCESS Pages successfully allocated.
77 SmmInternalAllocatePagesEx (
78 IN EFI_ALLOCATE_TYPE Type
,
79 IN EFI_MEMORY_TYPE MemoryType
,
80 IN UINTN NumberOfPages
,
81 OUT EFI_PHYSICAL_ADDRESS
*Memory
,
87 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
88 If the list is emtry, then allocate a new page to refuel the list.
89 Please Note this algorithm to allocate the memory map descriptor has a property
90 that the memory allocated for memory entries always grows, and will never really be freed.
92 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList
96 AllocateMemoryMapEntry (
100 EFI_PHYSICAL_ADDRESS Mem
;
102 MEMORY_MAP
* FreeDescriptorEntries
;
106 //DEBUG((DEBUG_INFO, "AllocateMemoryMapEntry\n"));
108 if (IsListEmpty (&mFreeMemoryMapEntryList
)) {
109 //DEBUG((DEBUG_INFO, "mFreeMemoryMapEntryList is empty\n"));
111 // The list is empty, to allocate one page to refuel the list
113 Status
= SmmInternalAllocatePagesEx (
115 EfiRuntimeServicesData
,
116 EFI_SIZE_TO_PAGES (RUNTIME_PAGE_ALLOCATION_GRANULARITY
),
121 ASSERT_EFI_ERROR (Status
);
122 if(!EFI_ERROR (Status
)) {
123 FreeDescriptorEntries
= (MEMORY_MAP
*)(UINTN
)Mem
;
124 //DEBUG((DEBUG_INFO, "New FreeDescriptorEntries - 0x%x\n", FreeDescriptorEntries));
126 // Enque the free memmory map entries into the list
128 for (Index
= 0; Index
< RUNTIME_PAGE_ALLOCATION_GRANULARITY
/ sizeof(MEMORY_MAP
); Index
++) {
129 FreeDescriptorEntries
[Index
].Signature
= MEMORY_MAP_SIGNATURE
;
130 InsertTailList (&mFreeMemoryMapEntryList
, &FreeDescriptorEntries
[Index
].Link
);
137 // dequeue the first descriptor from the list
139 Entry
= CR (mFreeMemoryMapEntryList
.ForwardLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
140 RemoveEntryList (&Entry
->Link
);
147 Internal function. Moves any memory descriptors that are on the
148 temporary descriptor stack to heap.
152 CoreFreeMemoryMapStack (
159 // If already freeing the map stack, then return
161 if (mFreeMapStack
!= 0) {
167 // Move the temporary memory descriptor stack into pool
171 while (mMapDepth
!= 0) {
173 // Deque an memory map entry from mFreeMemoryMapEntryList
175 Entry
= AllocateMemoryMapEntry ();
179 // Update to proper entry
183 if (mMapStack
[mMapDepth
].Link
.ForwardLink
!= NULL
) {
185 CopyMem (Entry
, &mMapStack
[mMapDepth
], sizeof (MEMORY_MAP
));
186 Entry
->FromStack
= FALSE
;
189 // Move this entry to general memory
191 InsertTailList (&mMapStack
[mMapDepth
].Link
, &Entry
->Link
);
192 RemoveEntryList (&mMapStack
[mMapDepth
].Link
);
193 mMapStack
[mMapDepth
].Link
.ForwardLink
= NULL
;
201 Insert new entry from memory map.
203 @param[in] Link The old memory map entry to be linked.
204 @param[in] Start The start address of new memory map entry.
205 @param[in] End The end address of new memory map entry.
206 @param[in] Type The type of new memory map entry.
207 @param[in] Next If new entry is inserted to the next of old entry.
208 @param[in] AddRegion If this memory is new added region.
215 IN EFI_MEMORY_TYPE Type
,
222 Entry
= &mMapStack
[mMapDepth
];
224 ASSERT (mMapDepth
< MAX_MAP_DEPTH
);
225 Entry
->FromStack
= TRUE
;
227 Entry
->Signature
= MEMORY_MAP_SIGNATURE
;
229 Entry
->Start
= Start
;
232 InsertHeadList (Link
, &Entry
->Link
);
234 InsertTailList (Link
, &Entry
->Link
);
239 Remove old entry from memory map.
241 @param[in] Entry Memory map entry to be removed.
248 RemoveEntryList (&Entry
->Link
);
249 Entry
->Link
.ForwardLink
= NULL
;
251 if (!Entry
->FromStack
) {
252 InsertTailList (&mFreeMemoryMapEntryList
, &Entry
->Link
);
257 Update SMM memory map entry.
259 @param[in] Type The type of allocation to perform.
260 @param[in] Memory The base of memory address.
261 @param[in] NumberOfPages The number of pages to allocate.
262 @param[in] AddRegion If this memory is new added region.
265 ConvertSmmMemoryMapEntry (
266 IN EFI_MEMORY_TYPE Type
,
267 IN EFI_PHYSICAL_ADDRESS Memory
,
268 IN UINTN NumberOfPages
,
274 MEMORY_MAP
*NextEntry
;
275 LIST_ENTRY
*NextLink
;
276 MEMORY_MAP
*PreviousEntry
;
277 LIST_ENTRY
*PreviousLink
;
278 EFI_PHYSICAL_ADDRESS Start
;
279 EFI_PHYSICAL_ADDRESS End
;
282 End
= Memory
+ EFI_PAGES_TO_SIZE(NumberOfPages
) - 1;
285 // Exclude memory region
287 Link
= gMemoryMap
.ForwardLink
;
288 while (Link
!= &gMemoryMap
) {
289 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
290 Link
= Link
->ForwardLink
;
293 // ---------------------------------------------------
294 // | +----------+ +------+ +------+ +------+ |
295 // ---|gMemoryMep|---|Entry1|---|Entry2|---|Entry3|---
296 // +----------+ ^ +------+ +------+ +------+
302 if (Entry
->Start
> End
) {
303 if ((Entry
->Start
== End
+ 1) && (Entry
->Type
== Type
)) {
304 Entry
->Start
= Start
;
318 if ((Entry
->Start
<= Start
) && (Entry
->End
>= End
)) {
319 if (Entry
->Type
!= Type
) {
320 if (Entry
->Start
< Start
) {
322 // ---------------------------------------------------
323 // | +----------+ +------+ +------+ +------+ |
324 // ---|gMemoryMep|---|Entry1|---|EntryX|---|Entry3|---
325 // +----------+ +------+ ^ +------+ +------+
340 if (Entry
->End
> End
) {
342 // ---------------------------------------------------
343 // | +----------+ +------+ +------+ +------+ |
344 // ---|gMemoryMep|---|Entry1|---|EntryX|---|Entry3|---
345 // +----------+ +------+ +------+ ^ +------+
363 Entry
->Start
= Start
;
370 NextLink
= Entry
->Link
.ForwardLink
;
371 if (NextLink
!= &gMemoryMap
) {
372 NextEntry
= CR (NextLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
374 // ---------------------------------------------------
375 // | +----------+ +------+ +-----------------+ |
376 // ---|gMemoryMep|---|Entry1|---|EntryX Entry3|---
377 // +----------+ +------+ +-----------------+
379 if ((Entry
->Type
== NextEntry
->Type
) && (Entry
->End
+ 1 == NextEntry
->Start
)) {
380 Entry
->End
= NextEntry
->End
;
381 RemoveOldEntry (NextEntry
);
384 PreviousLink
= Entry
->Link
.BackLink
;
385 if (PreviousLink
!= &gMemoryMap
) {
386 PreviousEntry
= CR (PreviousLink
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
388 // ---------------------------------------------------
389 // | +----------+ +-----------------+ +------+ |
390 // ---|gMemoryMep|---|Entry1 EntryX|---|Entry3|---
391 // +----------+ +-----------------+ +------+
393 if ((PreviousEntry
->Type
== Entry
->Type
) && (PreviousEntry
->End
+ 1 == Entry
->Start
)) {
394 PreviousEntry
->End
= Entry
->End
;
395 RemoveOldEntry (Entry
);
404 // ---------------------------------------------------
405 // | +----------+ +------+ +------+ +------+ |
406 // ---|gMemoryMep|---|Entry1|---|Entry2|---|Entry3|---
407 // +----------+ +------+ +------+ +------+ ^
413 Link
= gMemoryMap
.BackLink
;
414 if (Link
!= &gMemoryMap
) {
415 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
416 if ((Entry
->End
+ 1 == Start
) && (Entry
->Type
== Type
)) {
433 Return the count of Smm memory map entry.
435 @return The count of Smm memory map entry.
438 GetSmmMemoryMapEntryCount (
446 Link
= gMemoryMap
.ForwardLink
;
447 while (Link
!= &gMemoryMap
) {
448 Link
= Link
->ForwardLink
;
455 Dump Smm memory map entry.
458 DumpSmmMemoryMapEntry (
464 EFI_PHYSICAL_ADDRESS Last
;
467 DEBUG ((DEBUG_INFO
, "DumpSmmMemoryMapEntry:\n"));
468 Link
= gMemoryMap
.ForwardLink
;
469 while (Link
!= &gMemoryMap
) {
470 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
471 Link
= Link
->ForwardLink
;
473 if ((Last
!= 0) && (Last
!= (UINT64
)-1)) {
474 if (Last
+ 1 != Entry
->Start
) {
479 } else if (Last
== 0) {
483 DEBUG ((DEBUG_INFO
, "Entry (Link - 0x%x)\n", &Entry
->Link
));
484 DEBUG ((DEBUG_INFO
, " Signature - 0x%x\n", Entry
->Signature
));
485 DEBUG ((DEBUG_INFO
, " Link.ForwardLink - 0x%x\n", Entry
->Link
.ForwardLink
));
486 DEBUG ((DEBUG_INFO
, " Link.BackLink - 0x%x\n", Entry
->Link
.BackLink
));
487 DEBUG ((DEBUG_INFO
, " Type - 0x%x\n", Entry
->Type
));
488 DEBUG ((DEBUG_INFO
, " Start - 0x%016lx\n", Entry
->Start
));
489 DEBUG ((DEBUG_INFO
, " End - 0x%016lx\n", Entry
->End
));
492 ASSERT (Last
!= (UINT64
)-1);
504 FREE_PAGE_LIST
*Pages
;
506 DEBUG ((DEBUG_INFO
, "DumpSmmMemoryMap\n"));
509 Node
= mSmmMemoryMap
.ForwardLink
;
510 while (Node
!= &mSmmMemoryMap
) {
511 Pages
= BASE_CR (Node
, FREE_PAGE_LIST
, Link
);
512 DEBUG ((DEBUG_INFO
, "Pages - 0x%x\n", Pages
));
513 DEBUG ((DEBUG_INFO
, "Pages->NumberOfPages - 0x%x\n", Pages
->NumberOfPages
));
514 Node
= Node
->ForwardLink
;
519 Check if a Smm base~length is in Smm memory map.
521 @param[in] Base The base address of Smm memory to be checked.
522 @param[in] Length THe length of Smm memory to be checked.
524 @retval TRUE Smm base~length is in smm memory map.
525 @retval FALSE Smm base~length is in smm memory map.
528 SmmMemoryMapConsistencyCheckRange (
529 IN EFI_PHYSICAL_ADDRESS Base
,
538 Link
= gMemoryMap
.ForwardLink
;
539 while (Link
!= &gMemoryMap
) {
540 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
541 Link
= Link
->ForwardLink
;
543 if (Entry
->Type
!= EfiConventionalMemory
) {
546 if (Entry
->Start
== Base
&& Entry
->End
== Base
+ Length
- 1) {
556 Check the consistency of Smm memory map.
559 SmmMemoryMapConsistencyCheck (
564 FREE_PAGE_LIST
*Pages
;
568 Node
= mSmmMemoryMap
.ForwardLink
;
569 while (Node
!= &mSmmMemoryMap
) {
570 Pages
= BASE_CR (Node
, FREE_PAGE_LIST
, Link
);
571 Result
= SmmMemoryMapConsistencyCheckRange ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Pages
, (UINTN
)EFI_PAGES_TO_SIZE(Pages
->NumberOfPages
));
573 Node
= Node
->ForwardLink
;
578 Internal Function. Allocate n pages from given free page node.
580 @param Pages The free page node.
581 @param NumberOfPages Number of pages to be allocated.
582 @param MaxAddress Request to allocate memory below this address.
584 @return Memory address of allocated pages.
588 InternalAllocPagesOnOneNode (
589 IN OUT FREE_PAGE_LIST
*Pages
,
590 IN UINTN NumberOfPages
,
596 FREE_PAGE_LIST
*Node
;
598 Top
= TRUNCATE_TO_PAGES (MaxAddress
+ 1 - (UINTN
)Pages
);
599 if (Top
> Pages
->NumberOfPages
) {
600 Top
= Pages
->NumberOfPages
;
602 Bottom
= Top
- NumberOfPages
;
604 if (Top
< Pages
->NumberOfPages
) {
605 Node
= (FREE_PAGE_LIST
*)((UINTN
)Pages
+ EFI_PAGES_TO_SIZE (Top
));
606 Node
->NumberOfPages
= Pages
->NumberOfPages
- Top
;
607 InsertHeadList (&Pages
->Link
, &Node
->Link
);
611 Pages
->NumberOfPages
= Bottom
;
613 RemoveEntryList (&Pages
->Link
);
616 return (UINTN
)Pages
+ EFI_PAGES_TO_SIZE (Bottom
);
620 Internal Function. Allocate n pages from free page list below MaxAddress.
622 @param FreePageList The free page node.
623 @param NumberOfPages Number of pages to be allocated.
624 @param MaxAddress Request to allocate memory below this address.
626 @return Memory address of allocated pages.
630 InternalAllocMaxAddress (
631 IN OUT LIST_ENTRY
*FreePageList
,
632 IN UINTN NumberOfPages
,
637 FREE_PAGE_LIST
*Pages
;
639 for (Node
= FreePageList
->BackLink
; Node
!= FreePageList
; Node
= Node
->BackLink
) {
640 Pages
= BASE_CR (Node
, FREE_PAGE_LIST
, Link
);
641 if (Pages
->NumberOfPages
>= NumberOfPages
&&
642 (UINTN
)Pages
+ EFI_PAGES_TO_SIZE (NumberOfPages
) - 1 <= MaxAddress
) {
643 return InternalAllocPagesOnOneNode (Pages
, NumberOfPages
, MaxAddress
);
650 Internal Function. Allocate n pages from free page list at given address.
652 @param FreePageList The free page node.
653 @param NumberOfPages Number of pages to be allocated.
654 @param MaxAddress Request to allocate memory below this address.
656 @return Memory address of allocated pages.
660 InternalAllocAddress (
661 IN OUT LIST_ENTRY
*FreePageList
,
662 IN UINTN NumberOfPages
,
668 FREE_PAGE_LIST
*Pages
;
670 if ((Address
& EFI_PAGE_MASK
) != 0) {
674 EndAddress
= Address
+ EFI_PAGES_TO_SIZE (NumberOfPages
);
675 for (Node
= FreePageList
->BackLink
; Node
!= FreePageList
; Node
= Node
->BackLink
) {
676 Pages
= BASE_CR (Node
, FREE_PAGE_LIST
, Link
);
677 if ((UINTN
)Pages
<= Address
) {
678 if ((UINTN
)Pages
+ EFI_PAGES_TO_SIZE (Pages
->NumberOfPages
) < EndAddress
) {
681 return InternalAllocPagesOnOneNode (Pages
, NumberOfPages
, EndAddress
);
688 Allocates pages from the memory map.
690 @param[in] Type The type of allocation to perform.
691 @param[in] MemoryType The type of memory to turn the allocated pages
693 @param[in] NumberOfPages The number of pages to allocate.
694 @param[out] Memory A pointer to receive the base allocated memory
696 @param[in] AddRegion If this memory is new added region.
697 @param[in] NeedGuard Flag to indicate Guard page is needed
700 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.
701 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
702 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
703 @retval EFI_SUCCESS Pages successfully allocated.
707 SmmInternalAllocatePagesEx (
708 IN EFI_ALLOCATE_TYPE Type
,
709 IN EFI_MEMORY_TYPE MemoryType
,
710 IN UINTN NumberOfPages
,
711 OUT EFI_PHYSICAL_ADDRESS
*Memory
,
712 IN BOOLEAN AddRegion
,
716 UINTN RequestedAddress
;
718 if (MemoryType
!= EfiRuntimeServicesCode
&&
719 MemoryType
!= EfiRuntimeServicesData
) {
720 return EFI_INVALID_PARAMETER
;
723 if (NumberOfPages
> TRUNCATE_TO_PAGES ((UINTN
)-1) + 1) {
724 return EFI_OUT_OF_RESOURCES
;
728 // We don't track memory type in SMM
730 RequestedAddress
= (UINTN
)*Memory
;
732 case AllocateAnyPages
:
733 RequestedAddress
= (UINTN
)(-1);
734 case AllocateMaxAddress
:
736 *Memory
= InternalAllocMaxAddressWithGuard (
742 if (*Memory
== (UINTN
)-1) {
743 return EFI_OUT_OF_RESOURCES
;
745 ASSERT (VerifyMemoryGuard (*Memory
, NumberOfPages
) == TRUE
);
750 *Memory
= InternalAllocMaxAddress (
755 if (*Memory
== (UINTN
)-1) {
756 return EFI_OUT_OF_RESOURCES
;
759 case AllocateAddress
:
760 *Memory
= InternalAllocAddress (
765 if (*Memory
!= RequestedAddress
) {
766 return EFI_NOT_FOUND
;
770 return EFI_INVALID_PARAMETER
;
774 // Update SmmMemoryMap here.
776 ConvertSmmMemoryMapEntry (MemoryType
, *Memory
, NumberOfPages
, AddRegion
);
778 CoreFreeMemoryMapStack();
785 Allocates pages from the memory map.
787 @param[in] Type The type of allocation to perform.
788 @param[in] MemoryType The type of memory to turn the allocated pages
790 @param[in] NumberOfPages The number of pages to allocate.
791 @param[out] Memory A pointer to receive the base allocated memory
793 @param[in] NeedGuard Flag to indicate Guard page is needed
796 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.
797 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
798 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
799 @retval EFI_SUCCESS Pages successfully allocated.
804 SmmInternalAllocatePages (
805 IN EFI_ALLOCATE_TYPE Type
,
806 IN EFI_MEMORY_TYPE MemoryType
,
807 IN UINTN NumberOfPages
,
808 OUT EFI_PHYSICAL_ADDRESS
*Memory
,
812 return SmmInternalAllocatePagesEx (Type
, MemoryType
, NumberOfPages
, Memory
,
817 Allocates pages from the memory map.
819 @param Type The type of allocation to perform.
820 @param MemoryType The type of memory to turn the allocated pages
822 @param NumberOfPages The number of pages to allocate.
823 @param Memory A pointer to receive the base allocated memory
826 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.
827 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
828 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
829 @retval EFI_SUCCESS Pages successfully allocated.
835 IN EFI_ALLOCATE_TYPE Type
,
836 IN EFI_MEMORY_TYPE MemoryType
,
837 IN UINTN NumberOfPages
,
838 OUT EFI_PHYSICAL_ADDRESS
*Memory
844 NeedGuard
= IsPageTypeToGuard (MemoryType
, Type
);
845 Status
= SmmInternalAllocatePages (Type
, MemoryType
, NumberOfPages
, Memory
,
847 if (!EFI_ERROR (Status
)) {
848 SmmCoreUpdateProfile (
849 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
850 MemoryProfileActionAllocatePages
,
852 EFI_PAGES_TO_SIZE (NumberOfPages
),
853 (VOID
*) (UINTN
) *Memory
,
861 Internal Function. Merge two adjacent nodes.
863 @param First The first of two nodes to merge.
865 @return Pointer to node after merge (if success) or pointer to next node (if fail).
870 IN FREE_PAGE_LIST
*First
873 FREE_PAGE_LIST
*Next
;
875 Next
= BASE_CR (First
->Link
.ForwardLink
, FREE_PAGE_LIST
, Link
);
877 TRUNCATE_TO_PAGES ((UINTN
)Next
- (UINTN
)First
) >= First
->NumberOfPages
);
879 if (TRUNCATE_TO_PAGES ((UINTN
)Next
- (UINTN
)First
) == First
->NumberOfPages
) {
880 First
->NumberOfPages
+= Next
->NumberOfPages
;
881 RemoveEntryList (&Next
->Link
);
888 Frees previous allocated pages.
890 @param[in] Memory Base address of memory being freed.
891 @param[in] NumberOfPages The number of pages to free.
892 @param[in] AddRegion If this memory is new added region.
894 @retval EFI_NOT_FOUND Could not find the entry that covers the range.
895 @retval EFI_INVALID_PARAMETER Address not aligned, Address is zero or NumberOfPages is zero.
896 @return EFI_SUCCESS Pages successfully freed.
900 SmmInternalFreePagesEx (
901 IN EFI_PHYSICAL_ADDRESS Memory
,
902 IN UINTN NumberOfPages
,
907 FREE_PAGE_LIST
*Pages
;
909 if (((Memory
& EFI_PAGE_MASK
) != 0) || (Memory
== 0) || (NumberOfPages
== 0)) {
910 return EFI_INVALID_PARAMETER
;
914 Node
= mSmmMemoryMap
.ForwardLink
;
915 while (Node
!= &mSmmMemoryMap
) {
916 Pages
= BASE_CR (Node
, FREE_PAGE_LIST
, Link
);
917 if (Memory
< (UINTN
)Pages
) {
920 Node
= Node
->ForwardLink
;
923 if (Node
!= &mSmmMemoryMap
&&
924 Memory
+ EFI_PAGES_TO_SIZE (NumberOfPages
) > (UINTN
)Pages
) {
925 return EFI_INVALID_PARAMETER
;
928 if (Node
->BackLink
!= &mSmmMemoryMap
) {
929 Pages
= BASE_CR (Node
->BackLink
, FREE_PAGE_LIST
, Link
);
930 if ((UINTN
)Pages
+ EFI_PAGES_TO_SIZE (Pages
->NumberOfPages
) > Memory
) {
931 return EFI_INVALID_PARAMETER
;
935 Pages
= (FREE_PAGE_LIST
*)(UINTN
)Memory
;
936 Pages
->NumberOfPages
= NumberOfPages
;
937 InsertTailList (Node
, &Pages
->Link
);
939 if (Pages
->Link
.BackLink
!= &mSmmMemoryMap
) {
940 Pages
= InternalMergeNodes (
941 BASE_CR (Pages
->Link
.BackLink
, FREE_PAGE_LIST
, Link
)
945 if (Node
!= &mSmmMemoryMap
) {
946 InternalMergeNodes (Pages
);
950 // Update SmmMemoryMap here.
952 ConvertSmmMemoryMapEntry (EfiConventionalMemory
, Memory
, NumberOfPages
, AddRegion
);
954 CoreFreeMemoryMapStack();
961 Frees previous allocated pages.
963 @param[in] Memory Base address of memory being freed.
964 @param[in] NumberOfPages The number of pages to free.
965 @param[in] IsGuarded Is the memory to free guarded or not.
967 @retval EFI_NOT_FOUND Could not find the entry that covers the range.
968 @retval EFI_INVALID_PARAMETER Address not aligned, Address is zero or NumberOfPages is zero.
969 @return EFI_SUCCESS Pages successfully freed.
974 SmmInternalFreePages (
975 IN EFI_PHYSICAL_ADDRESS Memory
,
976 IN UINTN NumberOfPages
,
981 return SmmInternalFreePagesExWithGuard (Memory
, NumberOfPages
, FALSE
);
983 return SmmInternalFreePagesEx (Memory
, NumberOfPages
, FALSE
);
987 Frees previous allocated pages.
989 @param Memory Base address of memory being freed.
990 @param NumberOfPages The number of pages to free.
992 @retval EFI_NOT_FOUND Could not find the entry that covers the range.
993 @retval EFI_INVALID_PARAMETER Address not aligned, Address is zero or NumberOfPages is zero.
994 @return EFI_SUCCESS Pages successfully freed.
1000 IN EFI_PHYSICAL_ADDRESS Memory
,
1001 IN UINTN NumberOfPages
1007 IsGuarded
= IsHeapGuardEnabled () && IsMemoryGuarded (Memory
);
1008 Status
= SmmInternalFreePages (Memory
, NumberOfPages
, IsGuarded
);
1009 if (!EFI_ERROR (Status
)) {
1010 SmmCoreUpdateProfile (
1011 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS (0),
1012 MemoryProfileActionFreePages
,
1014 EFI_PAGES_TO_SIZE (NumberOfPages
),
1015 (VOID
*) (UINTN
) Memory
,
1023 Add free SMRAM region for use by memory service.
1025 @param MemBase Base address of memory region.
1026 @param MemLength Length of the memory region.
1027 @param Type Memory type.
1028 @param Attributes Memory region state.
1032 SmmAddMemoryRegion (
1033 IN EFI_PHYSICAL_ADDRESS MemBase
,
1034 IN UINT64 MemLength
,
1035 IN EFI_MEMORY_TYPE Type
,
1036 IN UINT64 Attributes
1039 UINTN AlignedMemBase
;
1042 // Add EfiRuntimeServicesData for memory regions that is already allocated, needs testing, or needs ECC initialization
1044 if ((Attributes
& (EFI_ALLOCATED
| EFI_NEEDS_TESTING
| EFI_NEEDS_ECC_INITIALIZATION
)) != 0) {
1045 Type
= EfiRuntimeServicesData
;
1047 Type
= EfiConventionalMemory
;
1050 DEBUG ((DEBUG_INFO
, "SmmAddMemoryRegion\n"));
1051 DEBUG ((DEBUG_INFO
, " MemBase - 0x%lx\n", MemBase
));
1052 DEBUG ((DEBUG_INFO
, " MemLength - 0x%lx\n", MemLength
));
1053 DEBUG ((DEBUG_INFO
, " Type - 0x%x\n", Type
));
1054 DEBUG ((DEBUG_INFO
, " Attributes - 0x%lx\n", Attributes
));
1057 // Align range on an EFI_PAGE_SIZE boundary
1059 AlignedMemBase
= (UINTN
)(MemBase
+ EFI_PAGE_MASK
) & ~EFI_PAGE_MASK
;
1060 MemLength
-= AlignedMemBase
- MemBase
;
1061 if (Type
== EfiConventionalMemory
) {
1062 SmmInternalFreePagesEx (AlignedMemBase
, TRUNCATE_TO_PAGES ((UINTN
)MemLength
), TRUE
);
1064 ConvertSmmMemoryMapEntry (EfiRuntimeServicesData
, AlignedMemBase
, TRUNCATE_TO_PAGES ((UINTN
)MemLength
), TRUE
);
1067 CoreFreeMemoryMapStack ();
1071 This function returns a copy of the current memory map. The map is an array of
1072 memory descriptors, each of which describes a contiguous block of memory.
1074 @param[in, out] MemoryMapSize A pointer to the size, in bytes, of the
1075 MemoryMap buffer. On input, this is the size of
1076 the buffer allocated by the caller. On output,
1077 it is the size of the buffer returned by the
1078 firmware if the buffer was large enough, or the
1079 size of the buffer needed to contain the map if
1080 the buffer was too small.
1081 @param[in, out] MemoryMap A pointer to the buffer in which firmware places
1082 the current memory map.
1083 @param[out] MapKey A pointer to the location in which firmware
1084 returns the key for the current memory map.
1085 @param[out] DescriptorSize A pointer to the location in which firmware
1086 returns the size, in bytes, of an individual
1087 EFI_MEMORY_DESCRIPTOR.
1088 @param[out] DescriptorVersion A pointer to the location in which firmware
1089 returns the version number associated with the
1090 EFI_MEMORY_DESCRIPTOR.
1092 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1094 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1095 buffer size needed to hold the memory map is
1096 returned in MemoryMapSize.
1097 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1102 SmmCoreGetMemoryMap (
1103 IN OUT UINTN
*MemoryMapSize
,
1104 IN OUT EFI_MEMORY_DESCRIPTOR
*MemoryMap
,
1106 OUT UINTN
*DescriptorSize
,
1107 OUT UINT32
*DescriptorVersion
1116 Size
= sizeof (EFI_MEMORY_DESCRIPTOR
);
1119 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1120 // prevent people from having pointer math bugs in their code.
1121 // now you have to use *DescriptorSize to make things work.
1123 Size
+= sizeof(UINT64
) - (Size
% sizeof (UINT64
));
1125 if (DescriptorSize
!= NULL
) {
1126 *DescriptorSize
= Size
;
1129 if (DescriptorVersion
!= NULL
) {
1130 *DescriptorVersion
= EFI_MEMORY_DESCRIPTOR_VERSION
;
1133 Count
= GetSmmMemoryMapEntryCount ();
1134 BufferSize
= Size
* Count
;
1135 if (*MemoryMapSize
< BufferSize
) {
1136 *MemoryMapSize
= BufferSize
;
1137 return EFI_BUFFER_TOO_SMALL
;
1140 *MemoryMapSize
= BufferSize
;
1141 if (MemoryMap
== NULL
) {
1142 return EFI_INVALID_PARAMETER
;
1145 ZeroMem (MemoryMap
, BufferSize
);
1146 Link
= gMemoryMap
.ForwardLink
;
1147 while (Link
!= &gMemoryMap
) {
1148 Entry
= CR (Link
, MEMORY_MAP
, Link
, MEMORY_MAP_SIGNATURE
);
1149 Link
= Link
->ForwardLink
;
1151 MemoryMap
->Type
= Entry
->Type
;
1152 MemoryMap
->PhysicalStart
= Entry
->Start
;
1153 MemoryMap
->NumberOfPages
= RShiftU64 (Entry
->End
- Entry
->Start
+ 1, EFI_PAGE_SHIFT
);
1155 MemoryMap
= NEXT_MEMORY_DESCRIPTOR (MemoryMap
, Size
);