2 UEFI Heap Guard functions.
4 Copyright (c) 2017-2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this 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 "HeapGuard.h"
18 // Global to avoid infinite reentrance of memory allocation when updating
19 // page table attributes, which may need allocating pages for new PDE/PTE.
21 GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mOnGuarding
= FALSE
;
24 // Pointer to table tracking the Guarded memory with bitmap, in which '1'
25 // is used to indicate memory guarded. '0' might be free memory or Guard
26 // page itself, depending on status of memory adjacent to it.
28 GLOBAL_REMOVE_IF_UNREFERENCED UINT64 mGuardedMemoryMap
= 0;
31 // Current depth level of map table pointed by mGuardedMemoryMap.
32 // mMapLevel must be initialized at least by 1. It will be automatically
33 // updated according to the address of memory just tracked.
35 GLOBAL_REMOVE_IF_UNREFERENCED UINTN mMapLevel
= 1;
38 // Shift and mask for each level of map table
40 GLOBAL_REMOVE_IF_UNREFERENCED UINTN mLevelShift
[GUARDED_HEAP_MAP_TABLE_DEPTH
]
41 = GUARDED_HEAP_MAP_TABLE_DEPTH_SHIFTS
;
42 GLOBAL_REMOVE_IF_UNREFERENCED UINTN mLevelMask
[GUARDED_HEAP_MAP_TABLE_DEPTH
]
43 = GUARDED_HEAP_MAP_TABLE_DEPTH_MASKS
;
46 // SMM memory attribute protocol
48 EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL
*mSmmMemoryAttribute
= NULL
;
51 Set corresponding bits in bitmap table to 1 according to the address.
53 @param[in] Address Start address to set for.
54 @param[in] BitNumber Number of bits to set.
55 @param[in] BitMap Pointer to bitmap which covers the Address.
62 IN EFI_PHYSICAL_ADDRESS Address
,
73 StartBit
= (UINTN
)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address
);
74 EndBit
= (StartBit
+ BitNumber
- 1) % GUARDED_HEAP_MAP_ENTRY_BITS
;
76 if ((StartBit
+ BitNumber
) > GUARDED_HEAP_MAP_ENTRY_BITS
) {
77 Msbs
= (GUARDED_HEAP_MAP_ENTRY_BITS
- StartBit
) %
78 GUARDED_HEAP_MAP_ENTRY_BITS
;
79 Lsbs
= (EndBit
+ 1) % GUARDED_HEAP_MAP_ENTRY_BITS
;
80 Qwords
= (BitNumber
- Msbs
) / GUARDED_HEAP_MAP_ENTRY_BITS
;
88 *BitMap
|= LShiftU64 (LShiftU64 (1, Msbs
) - 1, StartBit
);
93 SetMem64 ((VOID
*)BitMap
, Qwords
* GUARDED_HEAP_MAP_ENTRY_BYTES
,
99 *BitMap
|= (LShiftU64 (1, Lsbs
) - 1);
104 Set corresponding bits in bitmap table to 0 according to the address.
106 @param[in] Address Start address to set for.
107 @param[in] BitNumber Number of bits to set.
108 @param[in] BitMap Pointer to bitmap which covers the Address.
115 IN EFI_PHYSICAL_ADDRESS Address
,
126 StartBit
= (UINTN
)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address
);
127 EndBit
= (StartBit
+ BitNumber
- 1) % GUARDED_HEAP_MAP_ENTRY_BITS
;
129 if ((StartBit
+ BitNumber
) > GUARDED_HEAP_MAP_ENTRY_BITS
) {
130 Msbs
= (GUARDED_HEAP_MAP_ENTRY_BITS
- StartBit
) %
131 GUARDED_HEAP_MAP_ENTRY_BITS
;
132 Lsbs
= (EndBit
+ 1) % GUARDED_HEAP_MAP_ENTRY_BITS
;
133 Qwords
= (BitNumber
- Msbs
) / GUARDED_HEAP_MAP_ENTRY_BITS
;
141 *BitMap
&= ~LShiftU64 (LShiftU64 (1, Msbs
) - 1, StartBit
);
146 SetMem64 ((VOID
*)BitMap
, Qwords
* GUARDED_HEAP_MAP_ENTRY_BYTES
, 0);
151 *BitMap
&= ~(LShiftU64 (1, Lsbs
) - 1);
156 Get corresponding bits in bitmap table according to the address.
158 The value of bit 0 corresponds to the status of memory at given Address.
159 No more than 64 bits can be retrieved in one call.
161 @param[in] Address Start address to retrieve bits for.
162 @param[in] BitNumber Number of bits to get.
163 @param[in] BitMap Pointer to bitmap which covers the Address.
165 @return An integer containing the bits information.
170 IN EFI_PHYSICAL_ADDRESS Address
,
181 ASSERT (BitNumber
<= GUARDED_HEAP_MAP_ENTRY_BITS
);
183 StartBit
= (UINTN
)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address
);
184 EndBit
= (StartBit
+ BitNumber
- 1) % GUARDED_HEAP_MAP_ENTRY_BITS
;
186 if ((StartBit
+ BitNumber
) > GUARDED_HEAP_MAP_ENTRY_BITS
) {
187 Msbs
= GUARDED_HEAP_MAP_ENTRY_BITS
- StartBit
;
188 Lsbs
= (EndBit
+ 1) % GUARDED_HEAP_MAP_ENTRY_BITS
;
194 Result
= RShiftU64 ((*BitMap
), StartBit
) & (LShiftU64 (1, Msbs
) - 1);
197 Result
|= LShiftU64 ((*BitMap
) & (LShiftU64 (1, Lsbs
) - 1), Msbs
);
204 Helper function to allocate pages without Guard for internal uses.
206 @param[in] Pages Page number.
208 @return Address of memory allocated.
216 EFI_PHYSICAL_ADDRESS Memory
;
218 Status
= SmmInternalAllocatePages (AllocateAnyPages
, EfiRuntimeServicesData
,
219 Pages
, &Memory
, FALSE
);
220 if (EFI_ERROR (Status
)) {
224 return (VOID
*)(UINTN
)Memory
;
228 Locate the pointer of bitmap from the guarded memory bitmap tables, which
229 covers the given Address.
231 @param[in] Address Start address to search the bitmap for.
232 @param[in] AllocMapUnit Flag to indicate memory allocation for the table.
233 @param[out] BitMap Pointer to bitmap which covers the Address.
235 @return The bit number from given Address to the end of current map table.
238 FindGuardedMemoryMap (
239 IN EFI_PHYSICAL_ADDRESS Address
,
240 IN BOOLEAN AllocMapUnit
,
252 // Adjust current map table depth according to the address to access
254 while (AllocMapUnit
&&
255 mMapLevel
< GUARDED_HEAP_MAP_TABLE_DEPTH
&&
258 mLevelShift
[GUARDED_HEAP_MAP_TABLE_DEPTH
- mMapLevel
- 1]
261 if (mGuardedMemoryMap
!= 0) {
262 Size
= (mLevelMask
[GUARDED_HEAP_MAP_TABLE_DEPTH
- mMapLevel
- 1] + 1)
263 * GUARDED_HEAP_MAP_ENTRY_BYTES
;
264 MapMemory
= (UINT64
)(UINTN
)PageAlloc (EFI_SIZE_TO_PAGES (Size
));
265 ASSERT (MapMemory
!= 0);
267 SetMem ((VOID
*)(UINTN
)MapMemory
, Size
, 0);
269 *(UINT64
*)(UINTN
)MapMemory
= mGuardedMemoryMap
;
270 mGuardedMemoryMap
= MapMemory
;
277 GuardMap
= &mGuardedMemoryMap
;
278 for (Level
= GUARDED_HEAP_MAP_TABLE_DEPTH
- mMapLevel
;
279 Level
< GUARDED_HEAP_MAP_TABLE_DEPTH
;
282 if (*GuardMap
== 0) {
288 Size
= (mLevelMask
[Level
] + 1) * GUARDED_HEAP_MAP_ENTRY_BYTES
;
289 MapMemory
= (UINT64
)(UINTN
)PageAlloc (EFI_SIZE_TO_PAGES (Size
));
290 ASSERT (MapMemory
!= 0);
292 SetMem ((VOID
*)(UINTN
)MapMemory
, Size
, 0);
293 *GuardMap
= MapMemory
;
296 Index
= (UINTN
)RShiftU64 (Address
, mLevelShift
[Level
]);
297 Index
&= mLevelMask
[Level
];
298 GuardMap
= (UINT64
*)(UINTN
)((*GuardMap
) + Index
* sizeof (UINT64
));
302 BitsToUnitEnd
= GUARDED_HEAP_MAP_BITS
- GUARDED_HEAP_MAP_BIT_INDEX (Address
);
305 return BitsToUnitEnd
;
309 Set corresponding bits in bitmap table to 1 according to given memory range.
311 @param[in] Address Memory address to guard from.
312 @param[in] NumberOfPages Number of pages to guard.
318 SetGuardedMemoryBits (
319 IN EFI_PHYSICAL_ADDRESS Address
,
320 IN UINTN NumberOfPages
327 while (NumberOfPages
> 0) {
328 BitsToUnitEnd
= FindGuardedMemoryMap (Address
, TRUE
, &BitMap
);
329 ASSERT (BitMap
!= NULL
);
331 if (NumberOfPages
> BitsToUnitEnd
) {
333 Bits
= BitsToUnitEnd
;
335 Bits
= NumberOfPages
;
338 SetBits (Address
, Bits
, BitMap
);
340 NumberOfPages
-= Bits
;
341 Address
+= EFI_PAGES_TO_SIZE (Bits
);
346 Clear corresponding bits in bitmap table according to given memory range.
348 @param[in] Address Memory address to unset from.
349 @param[in] NumberOfPages Number of pages to unset guard.
355 ClearGuardedMemoryBits (
356 IN EFI_PHYSICAL_ADDRESS Address
,
357 IN UINTN NumberOfPages
364 while (NumberOfPages
> 0) {
365 BitsToUnitEnd
= FindGuardedMemoryMap (Address
, TRUE
, &BitMap
);
366 ASSERT (BitMap
!= NULL
);
368 if (NumberOfPages
> BitsToUnitEnd
) {
370 Bits
= BitsToUnitEnd
;
372 Bits
= NumberOfPages
;
375 ClearBits (Address
, Bits
, BitMap
);
377 NumberOfPages
-= Bits
;
378 Address
+= EFI_PAGES_TO_SIZE (Bits
);
383 Retrieve corresponding bits in bitmap table according to given memory range.
385 @param[in] Address Memory address to retrieve from.
386 @param[in] NumberOfPages Number of pages to retrieve.
388 @return An integer containing the guarded memory bitmap.
391 GetGuardedMemoryBits (
392 IN EFI_PHYSICAL_ADDRESS Address
,
393 IN UINTN NumberOfPages
402 ASSERT (NumberOfPages
<= GUARDED_HEAP_MAP_ENTRY_BITS
);
406 while (NumberOfPages
> 0) {
407 BitsToUnitEnd
= FindGuardedMemoryMap (Address
, FALSE
, &BitMap
);
409 if (NumberOfPages
> BitsToUnitEnd
) {
411 Bits
= BitsToUnitEnd
;
413 Bits
= NumberOfPages
;
416 if (BitMap
!= NULL
) {
417 Result
|= LShiftU64 (GetBits (Address
, Bits
, BitMap
), Shift
);
421 NumberOfPages
-= Bits
;
422 Address
+= EFI_PAGES_TO_SIZE (Bits
);
429 Get bit value in bitmap table for the given address.
431 @param[in] Address The address to retrieve for.
438 IN EFI_PHYSICAL_ADDRESS Address
443 FindGuardedMemoryMap (Address
, FALSE
, &GuardMap
);
444 if (GuardMap
!= NULL
) {
445 if (RShiftU64 (*GuardMap
,
446 GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address
)) & 1) {
455 Set the bit in bitmap table for the given address.
457 @param[in] Address The address to set for.
464 IN EFI_PHYSICAL_ADDRESS Address
470 FindGuardedMemoryMap (Address
, TRUE
, &GuardMap
);
471 if (GuardMap
!= NULL
) {
472 BitMask
= LShiftU64 (1, GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address
));
473 *GuardMap
|= BitMask
;
478 Clear the bit in bitmap table for the given address.
480 @param[in] Address The address to clear for.
487 IN EFI_PHYSICAL_ADDRESS Address
493 FindGuardedMemoryMap (Address
, TRUE
, &GuardMap
);
494 if (GuardMap
!= NULL
) {
495 BitMask
= LShiftU64 (1, GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address
));
496 *GuardMap
&= ~BitMask
;
501 Check to see if the page at the given address is a Guard page or not.
503 @param[in] Address The address to check for.
505 @return TRUE The page at Address is a Guard page.
506 @return FALSE The page at Address is not a Guard page.
511 IN EFI_PHYSICAL_ADDRESS Address
517 // There must be at least one guarded page before and/or after given
518 // address if it's a Guard page. The bitmap pattern should be one of
521 BitMap
= GetGuardedMemoryBits (Address
- EFI_PAGE_SIZE
, 3);
522 return ((BitMap
== BIT0
) || (BitMap
== BIT2
) || (BitMap
== (BIT2
| BIT0
)));
526 Check to see if the page at the given address is a head Guard page or not.
528 @param[in] Address The address to check for.
530 @return TRUE The page at Address is a head Guard page.
531 @return FALSE The page at Address is not a head Guard page.
536 IN EFI_PHYSICAL_ADDRESS Address
539 return (GetGuardedMemoryBits (Address
, 2) == BIT1
);
543 Check to see if the page at the given address is a tail Guard page or not.
545 @param[in] Address The address to check for.
547 @return TRUE The page at Address is a tail Guard page.
548 @return FALSE The page at Address is not a tail Guard page.
553 IN EFI_PHYSICAL_ADDRESS Address
556 return (GetGuardedMemoryBits (Address
- EFI_PAGE_SIZE
, 2) == BIT0
);
560 Check to see if the page at the given address is guarded or not.
562 @param[in] Address The address to check for.
564 @return TRUE The page at Address is guarded.
565 @return FALSE The page at Address is not guarded.
570 IN EFI_PHYSICAL_ADDRESS Address
573 return (GetGuardMapBit (Address
) == 1);
577 Set the page at the given address to be a Guard page.
579 This is done by changing the page table attribute to be NOT PRSENT.
581 @param[in] BaseAddress Page address to Guard at.
588 IN EFI_PHYSICAL_ADDRESS BaseAddress
591 if (mSmmMemoryAttribute
!= NULL
) {
593 mSmmMemoryAttribute
->SetMemoryAttributes (
604 Unset the Guard page at the given address to the normal memory.
606 This is done by changing the page table attribute to be PRSENT.
608 @param[in] BaseAddress Page address to Guard at.
615 IN EFI_PHYSICAL_ADDRESS BaseAddress
618 if (mSmmMemoryAttribute
!= NULL
) {
620 mSmmMemoryAttribute
->ClearMemoryAttributes (
631 Check to see if the memory at the given address should be guarded or not.
633 @param[in] MemoryType Memory type to check.
634 @param[in] AllocateType Allocation type to check.
635 @param[in] PageOrPool Indicate a page allocation or pool allocation.
638 @return TRUE The given type of memory should be guarded.
639 @return FALSE The given type of memory should not be guarded.
642 IsMemoryTypeToGuard (
643 IN EFI_MEMORY_TYPE MemoryType
,
644 IN EFI_ALLOCATE_TYPE AllocateType
,
651 if ((PcdGet8 (PcdHeapGuardPropertyMask
) & PageOrPool
) == 0
653 || AllocateType
== AllocateAddress
) {
658 if ((PageOrPool
& GUARD_HEAP_TYPE_POOL
) != 0) {
659 ConfigBit
|= PcdGet64 (PcdHeapGuardPoolType
);
662 if ((PageOrPool
& GUARD_HEAP_TYPE_PAGE
) != 0) {
663 ConfigBit
|= PcdGet64 (PcdHeapGuardPageType
);
666 if (MemoryType
== EfiRuntimeServicesData
||
667 MemoryType
== EfiRuntimeServicesCode
) {
668 TestBit
= LShiftU64 (1, MemoryType
);
669 } else if (MemoryType
== EfiMaxMemoryType
) {
670 TestBit
= (UINT64
)-1;
675 return ((ConfigBit
& TestBit
) != 0);
679 Check to see if the pool at the given address should be guarded or not.
681 @param[in] MemoryType Pool type to check.
684 @return TRUE The given type of pool should be guarded.
685 @return FALSE The given type of pool should not be guarded.
689 IN EFI_MEMORY_TYPE MemoryType
692 return IsMemoryTypeToGuard (MemoryType
, AllocateAnyPages
,
693 GUARD_HEAP_TYPE_POOL
);
697 Check to see if the page at the given address should be guarded or not.
699 @param[in] MemoryType Page type to check.
700 @param[in] AllocateType Allocation type to check.
702 @return TRUE The given type of page should be guarded.
703 @return FALSE The given type of page should not be guarded.
707 IN EFI_MEMORY_TYPE MemoryType
,
708 IN EFI_ALLOCATE_TYPE AllocateType
711 return IsMemoryTypeToGuard (MemoryType
, AllocateType
, GUARD_HEAP_TYPE_PAGE
);
715 Check to see if the heap guard is enabled for page and/or pool allocation.
724 return IsMemoryTypeToGuard (EfiMaxMemoryType
, AllocateAnyPages
,
725 GUARD_HEAP_TYPE_POOL
|GUARD_HEAP_TYPE_PAGE
);
729 Set head Guard and tail Guard for the given memory range.
731 @param[in] Memory Base address of memory to set guard for.
732 @param[in] NumberOfPages Memory size in pages.
738 IN EFI_PHYSICAL_ADDRESS Memory
,
739 IN UINTN NumberOfPages
742 EFI_PHYSICAL_ADDRESS GuardPage
;
747 GuardPage
= Memory
+ EFI_PAGES_TO_SIZE (NumberOfPages
);
748 if (!IsGuardPage (GuardPage
)) {
749 SetGuardPage (GuardPage
);
753 GuardPage
= Memory
- EFI_PAGES_TO_SIZE (1);
754 if (!IsGuardPage (GuardPage
)) {
755 SetGuardPage (GuardPage
);
759 // Mark the memory range as Guarded
761 SetGuardedMemoryBits (Memory
, NumberOfPages
);
765 Unset head Guard and tail Guard for the given memory range.
767 @param[in] Memory Base address of memory to unset guard for.
768 @param[in] NumberOfPages Memory size in pages.
773 UnsetGuardForMemory (
774 IN EFI_PHYSICAL_ADDRESS Memory
,
775 IN UINTN NumberOfPages
778 EFI_PHYSICAL_ADDRESS GuardPage
;
781 if (NumberOfPages
== 0) {
786 // Head Guard must be one page before, if any.
789 // -------------------
790 // Head Guard -> 0 1 -> Don't free Head Guard (shared Guard)
791 // Head Guard -> 0 0 -> Free Head Guard either (not shared Guard)
792 // 1 X -> Don't free first page (need a new Guard)
793 // (it'll be turned into a Guard page later)
794 // -------------------
797 GuardPage
= Memory
- EFI_PAGES_TO_SIZE (1);
798 GuardBitmap
= GetGuardedMemoryBits (Memory
- EFI_PAGES_TO_SIZE (2), 2);
799 if ((GuardBitmap
& BIT1
) == 0) {
801 // Head Guard exists.
803 if ((GuardBitmap
& BIT0
) == 0) {
805 // If the head Guard is not a tail Guard of adjacent memory block,
808 UnsetGuardPage (GuardPage
);
812 // Pages before memory to free are still in Guard. It's a partial free
813 // case. Turn first page of memory block to free into a new Guard.
815 SetGuardPage (Memory
);
819 // Tail Guard must be the page after this memory block to free, if any.
822 // --------------------
823 // 1 0 <- Tail Guard -> Don't free Tail Guard (shared Guard)
824 // 0 0 <- Tail Guard -> Free Tail Guard either (not shared Guard)
825 // X 1 -> Don't free last page (need a new Guard)
826 // (it'll be turned into a Guard page later)
827 // --------------------
830 GuardPage
= Memory
+ EFI_PAGES_TO_SIZE (NumberOfPages
);
831 GuardBitmap
= GetGuardedMemoryBits (GuardPage
, 2);
832 if ((GuardBitmap
& BIT0
) == 0) {
834 // Tail Guard exists.
836 if ((GuardBitmap
& BIT1
) == 0) {
838 // If the tail Guard is not a head Guard of adjacent memory block,
839 // free it; otherwise, keep it.
841 UnsetGuardPage (GuardPage
);
845 // Pages after memory to free are still in Guard. It's a partial free
846 // case. We need to keep one page to be a head Guard.
848 SetGuardPage (GuardPage
- EFI_PAGES_TO_SIZE (1));
852 // No matter what, we just clear the mark of the Guarded memory.
854 ClearGuardedMemoryBits(Memory
, NumberOfPages
);
858 Adjust address of free memory according to existing and/or required Guard.
860 This function will check if there're existing Guard pages of adjacent
861 memory blocks, and try to use it as the Guard page of the memory to be
864 @param[in] Start Start address of free memory block.
865 @param[in] Size Size of free memory block.
866 @param[in] SizeRequested Size of memory to allocate.
868 @return The end address of memory block found.
869 @return 0 if no enough space for the required size of memory and its Guard.
875 IN UINT64 SizeRequested
881 // UEFI spec requires that allocated pool must be 8-byte aligned. If it's
882 // indicated to put the pool near the Tail Guard, we need extra bytes to
883 // make sure alignment of the returned pool address.
885 if ((PcdGet8 (PcdHeapGuardPropertyMask
) & BIT7
) == 0) {
886 SizeRequested
= ALIGN_VALUE(SizeRequested
, 8);
889 Target
= Start
+ Size
- SizeRequested
;
890 ASSERT (Target
>= Start
);
895 if (!IsGuardPage (Start
+ Size
)) {
896 // No Guard at tail to share. One more page is needed.
897 Target
-= EFI_PAGES_TO_SIZE (1);
901 if (Target
< Start
) {
906 if (Target
== Start
) {
907 if (!IsGuardPage (Target
- EFI_PAGES_TO_SIZE (1))) {
908 // No enough space for a new head Guard if no Guard at head to share.
913 // OK, we have enough pages for memory and its Guards. Return the End of the
915 return Target
+ SizeRequested
- 1;
919 Adjust the start address and number of pages to free according to Guard.
921 The purpose of this function is to keep the shared Guard page with adjacent
922 memory block if it's still in guard, or free it if no more sharing. Another
923 is to reserve pages as Guard pages in partial page free situation.
925 @param[in,out] Memory Base address of memory to free.
926 @param[in,out] NumberOfPages Size of memory to free.
932 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
,
933 IN OUT UINTN
*NumberOfPages
936 EFI_PHYSICAL_ADDRESS Start
;
937 EFI_PHYSICAL_ADDRESS MemoryToTest
;
941 if (Memory
== NULL
|| NumberOfPages
== NULL
|| *NumberOfPages
== 0) {
946 PagesToFree
= *NumberOfPages
;
949 // Head Guard must be one page before, if any.
952 // -------------------
953 // Head Guard -> 0 1 -> Don't free Head Guard (shared Guard)
954 // Head Guard -> 0 0 -> Free Head Guard either (not shared Guard)
955 // 1 X -> Don't free first page (need a new Guard)
956 // (it'll be turned into a Guard page later)
957 // -------------------
960 MemoryToTest
= Start
- EFI_PAGES_TO_SIZE (2);
961 GuardBitmap
= GetGuardedMemoryBits (MemoryToTest
, 2);
962 if ((GuardBitmap
& BIT1
) == 0) {
964 // Head Guard exists.
966 if ((GuardBitmap
& BIT0
) == 0) {
968 // If the head Guard is not a tail Guard of adjacent memory block,
969 // free it; otherwise, keep it.
971 Start
-= EFI_PAGES_TO_SIZE (1);
976 // No Head Guard, and pages before memory to free are still in Guard. It's a
977 // partial free case. We need to keep one page to be a tail Guard.
979 Start
+= EFI_PAGES_TO_SIZE (1);
984 // Tail Guard must be the page after this memory block to free, if any.
987 // --------------------
988 // 1 0 <- Tail Guard -> Don't free Tail Guard (shared Guard)
989 // 0 0 <- Tail Guard -> Free Tail Guard either (not shared Guard)
990 // X 1 -> Don't free last page (need a new Guard)
991 // (it'll be turned into a Guard page later)
992 // --------------------
995 MemoryToTest
= Start
+ EFI_PAGES_TO_SIZE (PagesToFree
);
996 GuardBitmap
= GetGuardedMemoryBits (MemoryToTest
, 2);
997 if ((GuardBitmap
& BIT0
) == 0) {
999 // Tail Guard exists.
1001 if ((GuardBitmap
& BIT1
) == 0) {
1003 // If the tail Guard is not a head Guard of adjacent memory block,
1004 // free it; otherwise, keep it.
1008 } else if (PagesToFree
> 0) {
1010 // No Tail Guard, and pages after memory to free are still in Guard. It's a
1011 // partial free case. We need to keep one page to be a head Guard.
1017 *NumberOfPages
= PagesToFree
;
1021 Adjust the base and number of pages to really allocate according to Guard.
1023 @param[in,out] Memory Base address of free memory.
1024 @param[in,out] NumberOfPages Size of memory to allocate.
1030 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
,
1031 IN OUT UINTN
*NumberOfPages
1035 // FindFreePages() has already taken the Guard into account. It's safe to
1036 // adjust the start address and/or number of pages here, to make sure that
1037 // the Guards are also "allocated".
1039 if (!IsGuardPage (*Memory
+ EFI_PAGES_TO_SIZE (*NumberOfPages
))) {
1040 // No tail Guard, add one.
1041 *NumberOfPages
+= 1;
1044 if (!IsGuardPage (*Memory
- EFI_PAGE_SIZE
)) {
1045 // No head Guard, add one.
1046 *Memory
-= EFI_PAGE_SIZE
;
1047 *NumberOfPages
+= 1;
1052 Adjust the pool head position to make sure the Guard page is adjavent to
1053 pool tail or pool head.
1055 @param[in] Memory Base address of memory allocated.
1056 @param[in] NoPages Number of pages actually allocated.
1057 @param[in] Size Size of memory requested.
1058 (plus pool head/tail overhead)
1060 @return Address of pool head
1064 IN EFI_PHYSICAL_ADDRESS Memory
,
1069 if (Memory
== 0 || (PcdGet8 (PcdHeapGuardPropertyMask
) & BIT7
) != 0) {
1071 // Pool head is put near the head Guard
1073 return (VOID
*)(UINTN
)Memory
;
1077 // Pool head is put near the tail Guard
1079 Size
= ALIGN_VALUE (Size
, 8);
1080 return (VOID
*)(UINTN
)(Memory
+ EFI_PAGES_TO_SIZE (NoPages
) - Size
);
1084 Get the page base address according to pool head address.
1086 @param[in] Memory Head address of pool to free.
1088 @return Address of pool head.
1092 IN EFI_PHYSICAL_ADDRESS Memory
1095 if (Memory
== 0 || (PcdGet8 (PcdHeapGuardPropertyMask
) & BIT7
) != 0) {
1097 // Pool head is put near the head Guard
1099 return (VOID
*)(UINTN
)Memory
;
1103 // Pool head is put near the tail Guard
1105 return (VOID
*)(UINTN
)(Memory
& ~EFI_PAGE_MASK
);
1109 Helper function of memory allocation with Guard pages.
1111 @param FreePageList The free page node.
1112 @param NumberOfPages Number of pages to be allocated.
1113 @param MaxAddress Request to allocate memory below this address.
1114 @param MemoryType Type of memory requested.
1116 @return Memory address of allocated pages.
1119 InternalAllocMaxAddressWithGuard (
1120 IN OUT LIST_ENTRY
*FreePageList
,
1121 IN UINTN NumberOfPages
,
1122 IN UINTN MaxAddress
,
1123 IN EFI_MEMORY_TYPE MemoryType
1128 FREE_PAGE_LIST
*Pages
;
1134 for (Node
= FreePageList
->BackLink
; Node
!= FreePageList
;
1135 Node
= Node
->BackLink
) {
1136 Pages
= BASE_CR (Node
, FREE_PAGE_LIST
, Link
);
1137 if (Pages
->NumberOfPages
>= NumberOfPages
&&
1138 (UINTN
)Pages
+ EFI_PAGES_TO_SIZE (NumberOfPages
) - 1 <= MaxAddress
) {
1141 // We may need 1 or 2 more pages for Guard. Check it out.
1143 PagesToAlloc
= NumberOfPages
;
1144 TailGuard
= (UINTN
)Pages
+ EFI_PAGES_TO_SIZE (Pages
->NumberOfPages
);
1145 if (!IsGuardPage (TailGuard
)) {
1147 // Add one if no Guard at the end of current free memory block.
1153 HeadGuard
= (UINTN
)Pages
+
1154 EFI_PAGES_TO_SIZE (Pages
->NumberOfPages
- PagesToAlloc
) -
1156 if (!IsGuardPage (HeadGuard
)) {
1158 // Add one if no Guard at the page before the address to allocate
1164 if (Pages
->NumberOfPages
< PagesToAlloc
) {
1165 // Not enough space to allocate memory with Guards? Try next block.
1169 Address
= InternalAllocPagesOnOneNode (Pages
, PagesToAlloc
, MaxAddress
);
1170 ConvertSmmMemoryMapEntry(MemoryType
, Address
, PagesToAlloc
, FALSE
);
1171 CoreFreeMemoryMapStack();
1172 if (HeadGuard
== 0) {
1173 // Don't pass the Guard page to user.
1174 Address
+= EFI_PAGE_SIZE
;
1176 SetGuardForMemory (Address
, NumberOfPages
);
1185 Helper function of memory free with Guard pages.
1187 @param[in] Memory Base address of memory being freed.
1188 @param[in] NumberOfPages The number of pages to free.
1189 @param[in] AddRegion If this memory is new added region.
1191 @retval EFI_NOT_FOUND Could not find the entry that covers the range.
1192 @retval EFI_INVALID_PARAMETER Address not aligned, Address is zero or NumberOfPages is zero.
1193 @return EFI_SUCCESS Pages successfully freed.
1196 SmmInternalFreePagesExWithGuard (
1197 IN EFI_PHYSICAL_ADDRESS Memory
,
1198 IN UINTN NumberOfPages
,
1199 IN BOOLEAN AddRegion
1202 EFI_PHYSICAL_ADDRESS MemoryToFree
;
1205 if (((Memory
& EFI_PAGE_MASK
) != 0) || (Memory
== 0) || (NumberOfPages
== 0)) {
1206 return EFI_INVALID_PARAMETER
;
1209 MemoryToFree
= Memory
;
1210 PagesToFree
= NumberOfPages
;
1212 AdjustMemoryF (&MemoryToFree
, &PagesToFree
);
1213 UnsetGuardForMemory (Memory
, NumberOfPages
);
1214 if (PagesToFree
== 0) {
1218 return SmmInternalFreePagesEx (MemoryToFree
, PagesToFree
, AddRegion
);
1222 Set all Guard pages which cannot be set during the non-SMM mode time.
1229 UINTN Entries
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1230 UINTN Shifts
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1231 UINTN Indices
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1232 UINT64 Tables
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1233 UINT64 Addresses
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1241 if (mGuardedMemoryMap
== 0 ||
1243 mMapLevel
> GUARDED_HEAP_MAP_TABLE_DEPTH
) {
1247 CopyMem (Entries
, mLevelMask
, sizeof (Entries
));
1248 CopyMem (Shifts
, mLevelShift
, sizeof (Shifts
));
1250 SetMem (Tables
, sizeof(Tables
), 0);
1251 SetMem (Addresses
, sizeof(Addresses
), 0);
1252 SetMem (Indices
, sizeof(Indices
), 0);
1254 Level
= GUARDED_HEAP_MAP_TABLE_DEPTH
- mMapLevel
;
1255 Tables
[Level
] = mGuardedMemoryMap
;
1260 DumpGuardedMemoryBitmap ();
1264 if (Indices
[Level
] > Entries
[Level
]) {
1269 TableEntry
= ((UINT64
*)(UINTN
)(Tables
[Level
]))[Indices
[Level
]];
1270 Address
= Addresses
[Level
];
1272 if (TableEntry
== 0) {
1276 } else if (Level
< GUARDED_HEAP_MAP_TABLE_DEPTH
- 1) {
1279 Tables
[Level
] = TableEntry
;
1280 Addresses
[Level
] = Address
;
1288 while (Index
< GUARDED_HEAP_MAP_ENTRY_BITS
) {
1289 if ((TableEntry
& 1) == 1) {
1293 GuardPage
= Address
- EFI_PAGE_SIZE
;
1298 GuardPage
= Address
;
1305 if (GuardPage
!= 0) {
1306 SetGuardPage (GuardPage
);
1309 if (TableEntry
== 0) {
1313 TableEntry
= RShiftU64 (TableEntry
, 1);
1314 Address
+= EFI_PAGE_SIZE
;
1320 if (Level
< (GUARDED_HEAP_MAP_TABLE_DEPTH
- (INTN
)mMapLevel
)) {
1324 Indices
[Level
] += 1;
1325 Address
= (Level
== 0) ? 0 : Addresses
[Level
- 1];
1326 Addresses
[Level
] = Address
| LShiftU64(Indices
[Level
], Shifts
[Level
]);
1332 Hook function used to set all Guard pages after entering SMM mode.
1335 SmmEntryPointMemoryManagementHook (
1341 if (mSmmMemoryAttribute
== NULL
) {
1342 Status
= SmmLocateProtocol (
1343 &gEdkiiSmmMemoryAttributeProtocolGuid
,
1345 (VOID
**)&mSmmMemoryAttribute
1347 if (!EFI_ERROR(Status
)) {
1348 SetAllGuardPages ();
1354 Helper function to convert a UINT64 value in binary to a string.
1356 @param[in] Value Value of a UINT64 integer.
1357 @param[out] BinString String buffer to contain the conversion result.
1364 OUT CHAR8
*BinString
1369 if (BinString
== NULL
) {
1373 for (Index
= 64; Index
> 0; --Index
) {
1374 BinString
[Index
- 1] = '0' + (Value
& 1);
1375 Value
= RShiftU64 (Value
, 1);
1377 BinString
[64] = '\0';
1381 Dump the guarded memory bit map.
1385 DumpGuardedMemoryBitmap (
1389 UINTN Entries
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1390 UINTN Shifts
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1391 UINTN Indices
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1392 UINT64 Tables
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1393 UINT64 Addresses
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1398 CHAR8 String
[GUARDED_HEAP_MAP_ENTRY_BITS
+ 1];
1402 if (mGuardedMemoryMap
== 0 ||
1404 mMapLevel
> GUARDED_HEAP_MAP_TABLE_DEPTH
) {
1408 Ruler1
= " 3 2 1 0";
1409 Ruler2
= "FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210";
1411 DEBUG ((HEAP_GUARD_DEBUG_LEVEL
, "============================="
1412 " Guarded Memory Bitmap "
1413 "==============================\r\n"));
1414 DEBUG ((HEAP_GUARD_DEBUG_LEVEL
, " %a\r\n", Ruler1
));
1415 DEBUG ((HEAP_GUARD_DEBUG_LEVEL
, " %a\r\n", Ruler2
));
1417 CopyMem (Entries
, mLevelMask
, sizeof (Entries
));
1418 CopyMem (Shifts
, mLevelShift
, sizeof (Shifts
));
1420 SetMem (Indices
, sizeof(Indices
), 0);
1421 SetMem (Tables
, sizeof(Tables
), 0);
1422 SetMem (Addresses
, sizeof(Addresses
), 0);
1424 Level
= GUARDED_HEAP_MAP_TABLE_DEPTH
- mMapLevel
;
1425 Tables
[Level
] = mGuardedMemoryMap
;
1430 if (Indices
[Level
] > Entries
[Level
]) {
1437 HEAP_GUARD_DEBUG_LEVEL
,
1438 "========================================="
1439 "=========================================\r\n"
1444 TableEntry
= ((UINT64
*)(UINTN
)Tables
[Level
])[Indices
[Level
]];
1445 Address
= Addresses
[Level
];
1447 if (TableEntry
== 0) {
1449 if (Level
== GUARDED_HEAP_MAP_TABLE_DEPTH
- 1) {
1450 if (RepeatZero
== 0) {
1451 Uint64ToBinString(TableEntry
, String
);
1452 DEBUG ((HEAP_GUARD_DEBUG_LEVEL
, "%016lx: %a\r\n", Address
, String
));
1453 } else if (RepeatZero
== 1) {
1454 DEBUG ((HEAP_GUARD_DEBUG_LEVEL
, "... : ...\r\n"));
1459 } else if (Level
< GUARDED_HEAP_MAP_TABLE_DEPTH
- 1) {
1462 Tables
[Level
] = TableEntry
;
1463 Addresses
[Level
] = Address
;
1472 Uint64ToBinString(TableEntry
, String
);
1473 DEBUG ((HEAP_GUARD_DEBUG_LEVEL
, "%016lx: %a\r\n", Address
, String
));
1478 if (Level
< (GUARDED_HEAP_MAP_TABLE_DEPTH
- (INTN
)mMapLevel
)) {
1482 Indices
[Level
] += 1;
1483 Address
= (Level
== 0) ? 0 : Addresses
[Level
- 1];
1484 Addresses
[Level
] = Address
| LShiftU64(Indices
[Level
], Shifts
[Level
]);
1490 Debug function used to verify if the Guard page is well set or not.
1492 @param[in] BaseAddress Address of memory to check.
1493 @param[in] NumberOfPages Size of memory in pages.
1495 @return TRUE The head Guard and tail Guard are both well set.
1496 @return FALSE The head Guard and/or tail Guard are not well set.
1500 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
1501 IN UINTN NumberOfPages
1506 EFI_PHYSICAL_ADDRESS Address
;
1508 if (mSmmMemoryAttribute
== NULL
) {
1513 Address
= BaseAddress
- EFI_PAGE_SIZE
;
1514 Status
= mSmmMemoryAttribute
->GetMemoryAttributes (
1515 mSmmMemoryAttribute
,
1520 if (EFI_ERROR (Status
) || (Attribute
& EFI_MEMORY_RP
) == 0) {
1521 DEBUG ((DEBUG_ERROR
, "Head Guard is not set at: %016lx (%016lX)!!!\r\n",
1522 Address
, Attribute
));
1523 DumpGuardedMemoryBitmap ();
1528 Address
= BaseAddress
+ EFI_PAGES_TO_SIZE (NumberOfPages
);
1529 Status
= mSmmMemoryAttribute
->GetMemoryAttributes (
1530 mSmmMemoryAttribute
,
1535 if (EFI_ERROR (Status
) || (Attribute
& EFI_MEMORY_RP
) == 0) {
1536 DEBUG ((DEBUG_ERROR
, "Tail Guard is not set at: %016lx (%016lX)!!!\r\n",
1537 Address
, Attribute
));
1538 DumpGuardedMemoryBitmap ();