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.
17 #include "HeapGuard.h"
20 // Global to avoid infinite reentrance of memory allocation when updating
21 // page table attributes, which may need allocate pages for new PDE/PTE.
23 GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mOnGuarding
= FALSE
;
26 // Pointer to table tracking the Guarded memory with bitmap, in which '1'
27 // is used to indicate memory guarded. '0' might be free memory or Guard
28 // page itself, depending on status of memory adjacent to it.
30 GLOBAL_REMOVE_IF_UNREFERENCED UINT64 mGuardedMemoryMap
= 0;
33 // Current depth level of map table pointed by mGuardedMemoryMap.
34 // mMapLevel must be initialized at least by 1. It will be automatically
35 // updated according to the address of memory just tracked.
37 GLOBAL_REMOVE_IF_UNREFERENCED UINTN mMapLevel
= 1;
40 // Shift and mask for each level of map table
42 GLOBAL_REMOVE_IF_UNREFERENCED UINTN mLevelShift
[GUARDED_HEAP_MAP_TABLE_DEPTH
]
43 = GUARDED_HEAP_MAP_TABLE_DEPTH_SHIFTS
;
44 GLOBAL_REMOVE_IF_UNREFERENCED UINTN mLevelMask
[GUARDED_HEAP_MAP_TABLE_DEPTH
]
45 = GUARDED_HEAP_MAP_TABLE_DEPTH_MASKS
;
48 Set corresponding bits in bitmap table to 1 according to the address.
50 @param[in] Address Start address to set for.
51 @param[in] BitNumber Number of bits to set.
52 @param[in] BitMap Pointer to bitmap which covers the Address.
59 IN EFI_PHYSICAL_ADDRESS Address
,
70 StartBit
= (UINTN
)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address
);
71 EndBit
= (StartBit
+ BitNumber
- 1) % GUARDED_HEAP_MAP_ENTRY_BITS
;
73 if ((StartBit
+ BitNumber
) > GUARDED_HEAP_MAP_ENTRY_BITS
) {
74 Msbs
= (GUARDED_HEAP_MAP_ENTRY_BITS
- StartBit
) %
75 GUARDED_HEAP_MAP_ENTRY_BITS
;
76 Lsbs
= (EndBit
+ 1) % GUARDED_HEAP_MAP_ENTRY_BITS
;
77 Qwords
= (BitNumber
- Msbs
) / GUARDED_HEAP_MAP_ENTRY_BITS
;
85 *BitMap
|= LShiftU64 (LShiftU64 (1, Msbs
) - 1, StartBit
);
90 SetMem64 ((VOID
*)BitMap
, Qwords
* GUARDED_HEAP_MAP_ENTRY_BYTES
,
96 *BitMap
|= (LShiftU64 (1, Lsbs
) - 1);
101 Set corresponding bits in bitmap table to 0 according to the address.
103 @param[in] Address Start address to set for.
104 @param[in] BitNumber Number of bits to set.
105 @param[in] BitMap Pointer to bitmap which covers the Address.
112 IN EFI_PHYSICAL_ADDRESS Address
,
123 StartBit
= (UINTN
)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address
);
124 EndBit
= (StartBit
+ BitNumber
- 1) % GUARDED_HEAP_MAP_ENTRY_BITS
;
126 if ((StartBit
+ BitNumber
) > GUARDED_HEAP_MAP_ENTRY_BITS
) {
127 Msbs
= (GUARDED_HEAP_MAP_ENTRY_BITS
- StartBit
) %
128 GUARDED_HEAP_MAP_ENTRY_BITS
;
129 Lsbs
= (EndBit
+ 1) % GUARDED_HEAP_MAP_ENTRY_BITS
;
130 Qwords
= (BitNumber
- Msbs
) / GUARDED_HEAP_MAP_ENTRY_BITS
;
138 *BitMap
&= ~LShiftU64 (LShiftU64 (1, Msbs
) - 1, StartBit
);
143 SetMem64 ((VOID
*)BitMap
, Qwords
* GUARDED_HEAP_MAP_ENTRY_BYTES
, 0);
148 *BitMap
&= ~(LShiftU64 (1, Lsbs
) - 1);
153 Get corresponding bits in bitmap table according to the address.
155 The value of bit 0 corresponds to the status of memory at given Address.
156 No more than 64 bits can be retrieved in one call.
158 @param[in] Address Start address to retrieve bits for.
159 @param[in] BitNumber Number of bits to get.
160 @param[in] BitMap Pointer to bitmap which covers the Address.
162 @return An integer containing the bits information.
167 IN EFI_PHYSICAL_ADDRESS Address
,
178 ASSERT (BitNumber
<= GUARDED_HEAP_MAP_ENTRY_BITS
);
180 StartBit
= (UINTN
)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address
);
181 EndBit
= (StartBit
+ BitNumber
- 1) % GUARDED_HEAP_MAP_ENTRY_BITS
;
183 if ((StartBit
+ BitNumber
) > GUARDED_HEAP_MAP_ENTRY_BITS
) {
184 Msbs
= GUARDED_HEAP_MAP_ENTRY_BITS
- StartBit
;
185 Lsbs
= (EndBit
+ 1) % GUARDED_HEAP_MAP_ENTRY_BITS
;
191 Result
= RShiftU64 ((*BitMap
), StartBit
) & (LShiftU64 (1, Msbs
) - 1);
194 Result
|= LShiftU64 ((*BitMap
) & (LShiftU64 (1, Lsbs
) - 1), Msbs
);
201 Locate the pointer of bitmap from the guarded memory bitmap tables, which
202 covers the given Address.
204 @param[in] Address Start address to search the bitmap for.
205 @param[in] AllocMapUnit Flag to indicate memory allocation for the table.
206 @param[out] BitMap Pointer to bitmap which covers the Address.
208 @return The bit number from given Address to the end of current map table.
211 FindGuardedMemoryMap (
212 IN EFI_PHYSICAL_ADDRESS Address
,
213 IN BOOLEAN AllocMapUnit
,
226 // Adjust current map table depth according to the address to access
228 while (mMapLevel
< GUARDED_HEAP_MAP_TABLE_DEPTH
232 mLevelShift
[GUARDED_HEAP_MAP_TABLE_DEPTH
- mMapLevel
- 1]
235 if (mGuardedMemoryMap
!= 0) {
236 Size
= (mLevelMask
[GUARDED_HEAP_MAP_TABLE_DEPTH
- mMapLevel
- 1] + 1)
237 * GUARDED_HEAP_MAP_ENTRY_BYTES
;
238 Status
= CoreInternalAllocatePages (
241 EFI_SIZE_TO_PAGES (Size
),
245 ASSERT_EFI_ERROR (Status
);
246 ASSERT (MapMemory
!= 0);
248 SetMem ((VOID
*)(UINTN
)MapMemory
, Size
, 0);
250 *(UINT64
*)(UINTN
)MapMemory
= mGuardedMemoryMap
;
251 mGuardedMemoryMap
= MapMemory
;
258 GuardMap
= &mGuardedMemoryMap
;
259 for (Level
= GUARDED_HEAP_MAP_TABLE_DEPTH
- mMapLevel
;
260 Level
< GUARDED_HEAP_MAP_TABLE_DEPTH
;
263 if (*GuardMap
== 0) {
269 Size
= (mLevelMask
[Level
] + 1) * GUARDED_HEAP_MAP_ENTRY_BYTES
;
270 Status
= CoreInternalAllocatePages (
273 EFI_SIZE_TO_PAGES (Size
),
277 ASSERT_EFI_ERROR (Status
);
278 ASSERT (MapMemory
!= 0);
280 SetMem ((VOID
*)(UINTN
)MapMemory
, Size
, 0);
281 *GuardMap
= MapMemory
;
284 Index
= (UINTN
)RShiftU64 (Address
, mLevelShift
[Level
]);
285 Index
&= mLevelMask
[Level
];
286 GuardMap
= (UINT64
*)(UINTN
)((*GuardMap
) + Index
* sizeof (UINT64
));
290 BitsToUnitEnd
= GUARDED_HEAP_MAP_BITS
- GUARDED_HEAP_MAP_BIT_INDEX (Address
);
293 return BitsToUnitEnd
;
297 Set corresponding bits in bitmap table to 1 according to given memory range.
299 @param[in] Address Memory address to guard from.
300 @param[in] NumberOfPages Number of pages to guard.
306 SetGuardedMemoryBits (
307 IN EFI_PHYSICAL_ADDRESS Address
,
308 IN UINTN NumberOfPages
315 while (NumberOfPages
> 0) {
316 BitsToUnitEnd
= FindGuardedMemoryMap (Address
, TRUE
, &BitMap
);
317 ASSERT (BitMap
!= NULL
);
319 if (NumberOfPages
> BitsToUnitEnd
) {
321 Bits
= BitsToUnitEnd
;
323 Bits
= NumberOfPages
;
326 SetBits (Address
, Bits
, BitMap
);
328 NumberOfPages
-= Bits
;
329 Address
+= EFI_PAGES_TO_SIZE (Bits
);
334 Clear corresponding bits in bitmap table according to given memory range.
336 @param[in] Address Memory address to unset from.
337 @param[in] NumberOfPages Number of pages to unset guard.
343 ClearGuardedMemoryBits (
344 IN EFI_PHYSICAL_ADDRESS Address
,
345 IN UINTN NumberOfPages
352 while (NumberOfPages
> 0) {
353 BitsToUnitEnd
= FindGuardedMemoryMap (Address
, TRUE
, &BitMap
);
354 ASSERT (BitMap
!= NULL
);
356 if (NumberOfPages
> BitsToUnitEnd
) {
358 Bits
= BitsToUnitEnd
;
360 Bits
= NumberOfPages
;
363 ClearBits (Address
, Bits
, BitMap
);
365 NumberOfPages
-= Bits
;
366 Address
+= EFI_PAGES_TO_SIZE (Bits
);
371 Retrieve corresponding bits in bitmap table according to given memory range.
373 @param[in] Address Memory address to retrieve from.
374 @param[in] NumberOfPages Number of pages to retrieve.
376 @return An integer containing the guarded memory bitmap.
379 GetGuardedMemoryBits (
380 IN EFI_PHYSICAL_ADDRESS Address
,
381 IN UINTN NumberOfPages
390 ASSERT (NumberOfPages
<= GUARDED_HEAP_MAP_ENTRY_BITS
);
394 while (NumberOfPages
> 0) {
395 BitsToUnitEnd
= FindGuardedMemoryMap (Address
, FALSE
, &BitMap
);
397 if (NumberOfPages
> BitsToUnitEnd
) {
399 Bits
= BitsToUnitEnd
;
401 Bits
= NumberOfPages
;
404 if (BitMap
!= NULL
) {
405 Result
|= LShiftU64 (GetBits (Address
, Bits
, BitMap
), Shift
);
409 NumberOfPages
-= Bits
;
410 Address
+= EFI_PAGES_TO_SIZE (Bits
);
417 Get bit value in bitmap table for the given address.
419 @param[in] Address The address to retrieve for.
426 IN EFI_PHYSICAL_ADDRESS Address
431 FindGuardedMemoryMap (Address
, FALSE
, &GuardMap
);
432 if (GuardMap
!= NULL
) {
433 if (RShiftU64 (*GuardMap
,
434 GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address
)) & 1) {
443 Set the bit in bitmap table for the given address.
445 @param[in] Address The address to set for.
452 IN EFI_PHYSICAL_ADDRESS Address
458 FindGuardedMemoryMap (Address
, TRUE
, &GuardMap
);
459 if (GuardMap
!= NULL
) {
460 BitMask
= LShiftU64 (1, GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address
));
461 *GuardMap
|= BitMask
;
466 Clear the bit in bitmap table for the given address.
468 @param[in] Address The address to clear for.
475 IN EFI_PHYSICAL_ADDRESS Address
481 FindGuardedMemoryMap (Address
, TRUE
, &GuardMap
);
482 if (GuardMap
!= NULL
) {
483 BitMask
= LShiftU64 (1, GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address
));
484 *GuardMap
&= ~BitMask
;
489 Check to see if the page at the given address is a Guard page or not.
491 @param[in] Address The address to check for.
493 @return TRUE The page at Address is a Guard page.
494 @return FALSE The page at Address is not a Guard page.
499 IN EFI_PHYSICAL_ADDRESS Address
505 // There must be at least one guarded page before and/or after given
506 // address if it's a Guard page. The bitmap pattern should be one of
509 BitMap
= GetGuardedMemoryBits (Address
- EFI_PAGE_SIZE
, 3);
510 return ((BitMap
== BIT0
) || (BitMap
== BIT2
) || (BitMap
== (BIT2
| BIT0
)));
514 Check to see if the page at the given address is a head Guard page or not.
516 @param[in] Address The address to check for
518 @return TRUE The page at Address is a head Guard page
519 @return FALSE The page at Address is not a head Guard page
524 IN EFI_PHYSICAL_ADDRESS Address
527 return (GetGuardedMemoryBits (Address
, 2) == BIT1
);
531 Check to see if the page at the given address is a tail Guard page or not.
533 @param[in] Address The address to check for.
535 @return TRUE The page at Address is a tail Guard page.
536 @return FALSE The page at Address is not a tail Guard page.
541 IN EFI_PHYSICAL_ADDRESS Address
544 return (GetGuardedMemoryBits (Address
- EFI_PAGE_SIZE
, 2) == BIT0
);
548 Check to see if the page at the given address is guarded or not.
550 @param[in] Address The address to check for.
552 @return TRUE The page at Address is guarded.
553 @return FALSE The page at Address is not guarded.
558 IN EFI_PHYSICAL_ADDRESS Address
561 return (GetGuardMapBit (Address
) == 1);
565 Set the page at the given address to be a Guard page.
567 This is done by changing the page table attribute to be NOT PRSENT.
569 @param[in] BaseAddress Page address to Guard at
576 IN EFI_PHYSICAL_ADDRESS BaseAddress
580 // Set flag to make sure allocating memory without GUARD for page table
581 // operation; otherwise infinite loops could be caused.
585 // Note: This might overwrite other attributes needed by other features,
586 // such as NX memory protection.
588 gCpu
->SetMemoryAttributes (gCpu
, BaseAddress
, EFI_PAGE_SIZE
, EFI_MEMORY_RP
);
593 Unset the Guard page at the given address to the normal memory.
595 This is done by changing the page table attribute to be PRSENT.
597 @param[in] BaseAddress Page address to Guard at.
604 IN EFI_PHYSICAL_ADDRESS BaseAddress
610 // Once the Guard page is unset, it will be freed back to memory pool. NX
611 // memory protection must be restored for this page if NX is enabled for free
615 if ((PcdGet64 (PcdDxeNxMemoryProtectionPolicy
) & (1 << EfiConventionalMemory
)) != 0) {
616 Attributes
|= EFI_MEMORY_XP
;
620 // Set flag to make sure allocating memory without GUARD for page table
621 // operation; otherwise infinite loops could be caused.
625 // Note: This might overwrite other attributes needed by other features,
626 // such as memory protection (NX). Please make sure they are not enabled
629 gCpu
->SetMemoryAttributes (gCpu
, BaseAddress
, EFI_PAGE_SIZE
, Attributes
);
634 Check to see if the memory at the given address should be guarded or not.
636 @param[in] MemoryType Memory type to check.
637 @param[in] AllocateType Allocation type to check.
638 @param[in] PageOrPool Indicate a page allocation or pool allocation.
641 @return TRUE The given type of memory should be guarded.
642 @return FALSE The given type of memory should not be guarded.
645 IsMemoryTypeToGuard (
646 IN EFI_MEMORY_TYPE MemoryType
,
647 IN EFI_ALLOCATE_TYPE AllocateType
,
655 if (gCpu
== NULL
|| AllocateType
== AllocateAddress
) {
660 if (gSmmBase2
!= NULL
) {
661 gSmmBase2
->InSmm (gSmmBase2
, &InSmm
);
668 if ((PcdGet8 (PcdHeapGuardPropertyMask
) & PageOrPool
) == 0) {
672 if (PageOrPool
== GUARD_HEAP_TYPE_POOL
) {
673 ConfigBit
= PcdGet64 (PcdHeapGuardPoolType
);
674 } else if (PageOrPool
== GUARD_HEAP_TYPE_PAGE
) {
675 ConfigBit
= PcdGet64 (PcdHeapGuardPageType
);
677 ConfigBit
= (UINT64
)-1;
680 if ((UINT32
)MemoryType
>= MEMORY_TYPE_OS_RESERVED_MIN
) {
682 } else if ((UINT32
) MemoryType
>= MEMORY_TYPE_OEM_RESERVED_MIN
) {
684 } else if (MemoryType
< EfiMaxMemoryType
) {
685 TestBit
= LShiftU64 (1, MemoryType
);
686 } else if (MemoryType
== EfiMaxMemoryType
) {
687 TestBit
= (UINT64
)-1;
692 return ((ConfigBit
& TestBit
) != 0);
696 Check to see if the pool at the given address should be guarded or not.
698 @param[in] MemoryType Pool type to check.
701 @return TRUE The given type of pool should be guarded.
702 @return FALSE The given type of pool should not be guarded.
706 IN EFI_MEMORY_TYPE MemoryType
709 return IsMemoryTypeToGuard (MemoryType
, AllocateAnyPages
,
710 GUARD_HEAP_TYPE_POOL
);
714 Check to see if the page at the given address should be guarded or not.
716 @param[in] MemoryType Page type to check.
717 @param[in] AllocateType Allocation type to check.
719 @return TRUE The given type of page should be guarded.
720 @return FALSE The given type of page should not be guarded.
724 IN EFI_MEMORY_TYPE MemoryType
,
725 IN EFI_ALLOCATE_TYPE AllocateType
728 return IsMemoryTypeToGuard (MemoryType
, AllocateType
, GUARD_HEAP_TYPE_PAGE
);
732 Check to see if the heap guard is enabled for page and/or pool allocation.
741 return IsMemoryTypeToGuard (EfiMaxMemoryType
, AllocateAnyPages
,
742 GUARD_HEAP_TYPE_POOL
|GUARD_HEAP_TYPE_PAGE
);
746 Set head Guard and tail Guard for the given memory range.
748 @param[in] Memory Base address of memory to set guard for.
749 @param[in] NumberOfPages Memory size in pages.
755 IN EFI_PHYSICAL_ADDRESS Memory
,
756 IN UINTN NumberOfPages
759 EFI_PHYSICAL_ADDRESS GuardPage
;
764 GuardPage
= Memory
+ EFI_PAGES_TO_SIZE (NumberOfPages
);
765 if (!IsGuardPage (GuardPage
)) {
766 SetGuardPage (GuardPage
);
770 GuardPage
= Memory
- EFI_PAGES_TO_SIZE (1);
771 if (!IsGuardPage (GuardPage
)) {
772 SetGuardPage (GuardPage
);
776 // Mark the memory range as Guarded
778 SetGuardedMemoryBits (Memory
, NumberOfPages
);
782 Unset head Guard and tail Guard for the given memory range.
784 @param[in] Memory Base address of memory to unset guard for.
785 @param[in] NumberOfPages Memory size in pages.
790 UnsetGuardForMemory (
791 IN EFI_PHYSICAL_ADDRESS Memory
,
792 IN UINTN NumberOfPages
795 EFI_PHYSICAL_ADDRESS GuardPage
;
798 if (NumberOfPages
== 0) {
803 // Head Guard must be one page before, if any.
806 // -------------------
807 // Head Guard -> 0 1 -> Don't free Head Guard (shared Guard)
808 // Head Guard -> 0 0 -> Free Head Guard either (not shared Guard)
809 // 1 X -> Don't free first page (need a new Guard)
810 // (it'll be turned into a Guard page later)
811 // -------------------
814 GuardPage
= Memory
- EFI_PAGES_TO_SIZE (1);
815 GuardBitmap
= GetGuardedMemoryBits (Memory
- EFI_PAGES_TO_SIZE (2), 2);
816 if ((GuardBitmap
& BIT1
) == 0) {
818 // Head Guard exists.
820 if ((GuardBitmap
& BIT0
) == 0) {
822 // If the head Guard is not a tail Guard of adjacent memory block,
825 UnsetGuardPage (GuardPage
);
829 // Pages before memory to free are still in Guard. It's a partial free
830 // case. Turn first page of memory block to free into a new Guard.
832 SetGuardPage (Memory
);
836 // Tail Guard must be the page after this memory block to free, if any.
839 // --------------------
840 // 1 0 <- Tail Guard -> Don't free Tail Guard (shared Guard)
841 // 0 0 <- Tail Guard -> Free Tail Guard either (not shared Guard)
842 // X 1 -> Don't free last page (need a new Guard)
843 // (it'll be turned into a Guard page later)
844 // --------------------
847 GuardPage
= Memory
+ EFI_PAGES_TO_SIZE (NumberOfPages
);
848 GuardBitmap
= GetGuardedMemoryBits (GuardPage
, 2);
849 if ((GuardBitmap
& BIT0
) == 0) {
851 // Tail Guard exists.
853 if ((GuardBitmap
& BIT1
) == 0) {
855 // If the tail Guard is not a head Guard of adjacent memory block,
856 // free it; otherwise, keep it.
858 UnsetGuardPage (GuardPage
);
862 // Pages after memory to free are still in Guard. It's a partial free
863 // case. We need to keep one page to be a head Guard.
865 SetGuardPage (GuardPage
- EFI_PAGES_TO_SIZE (1));
869 // No matter what, we just clear the mark of the Guarded memory.
871 ClearGuardedMemoryBits(Memory
, NumberOfPages
);
875 Adjust address of free memory according to existing and/or required Guard.
877 This function will check if there're existing Guard pages of adjacent
878 memory blocks, and try to use it as the Guard page of the memory to be
881 @param[in] Start Start address of free memory block.
882 @param[in] Size Size of free memory block.
883 @param[in] SizeRequested Size of memory to allocate.
885 @return The end address of memory block found.
886 @return 0 if no enough space for the required size of memory and its Guard.
892 IN UINT64 SizeRequested
898 // UEFI spec requires that allocated pool must be 8-byte aligned. If it's
899 // indicated to put the pool near the Tail Guard, we need extra bytes to
900 // make sure alignment of the returned pool address.
902 if ((PcdGet8 (PcdHeapGuardPropertyMask
) & BIT7
) == 0) {
903 SizeRequested
= ALIGN_VALUE(SizeRequested
, 8);
906 Target
= Start
+ Size
- SizeRequested
;
908 if (!IsGuardPage (Start
+ Size
)) {
909 // No Guard at tail to share. One more page is needed.
910 Target
-= EFI_PAGES_TO_SIZE (1);
914 if (Target
< Start
) {
919 if (Target
== Start
) {
920 if (!IsGuardPage (Target
- EFI_PAGES_TO_SIZE (1))) {
921 // No enough space for a new head Guard if no Guard at head to share.
926 // OK, we have enough pages for memory and its Guards. Return the End of the
928 return Target
+ SizeRequested
- 1;
932 Adjust the start address and number of pages to free according to Guard.
934 The purpose of this function is to keep the shared Guard page with adjacent
935 memory block if it's still in guard, or free it if no more sharing. Another
936 is to reserve pages as Guard pages in partial page free situation.
938 @param[in,out] Memory Base address of memory to free.
939 @param[in,out] NumberOfPages Size of memory to free.
945 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
,
946 IN OUT UINTN
*NumberOfPages
949 EFI_PHYSICAL_ADDRESS Start
;
950 EFI_PHYSICAL_ADDRESS MemoryToTest
;
954 if (Memory
== NULL
|| NumberOfPages
== NULL
|| *NumberOfPages
== 0) {
959 PagesToFree
= *NumberOfPages
;
962 // Head Guard must be one page before, if any.
965 // -------------------
966 // Head Guard -> 0 1 -> Don't free Head Guard (shared Guard)
967 // Head Guard -> 0 0 -> Free Head Guard either (not shared Guard)
968 // 1 X -> Don't free first page (need a new Guard)
969 // (it'll be turned into a Guard page later)
970 // -------------------
973 MemoryToTest
= Start
- EFI_PAGES_TO_SIZE (2);
974 GuardBitmap
= GetGuardedMemoryBits (MemoryToTest
, 2);
975 if ((GuardBitmap
& BIT1
) == 0) {
977 // Head Guard exists.
979 if ((GuardBitmap
& BIT0
) == 0) {
981 // If the head Guard is not a tail Guard of adjacent memory block,
982 // free it; otherwise, keep it.
984 Start
-= EFI_PAGES_TO_SIZE (1);
989 // No Head Guard, and pages before memory to free are still in Guard. It's a
990 // partial free case. We need to keep one page to be a tail Guard.
992 Start
+= EFI_PAGES_TO_SIZE (1);
997 // Tail Guard must be the page after this memory block to free, if any.
1000 // --------------------
1001 // 1 0 <- Tail Guard -> Don't free Tail Guard (shared Guard)
1002 // 0 0 <- Tail Guard -> Free Tail Guard either (not shared Guard)
1003 // X 1 -> Don't free last page (need a new Guard)
1004 // (it'll be turned into a Guard page later)
1005 // --------------------
1008 MemoryToTest
= Start
+ EFI_PAGES_TO_SIZE (PagesToFree
);
1009 GuardBitmap
= GetGuardedMemoryBits (MemoryToTest
, 2);
1010 if ((GuardBitmap
& BIT0
) == 0) {
1012 // Tail Guard exists.
1014 if ((GuardBitmap
& BIT1
) == 0) {
1016 // If the tail Guard is not a head Guard of adjacent memory block,
1017 // free it; otherwise, keep it.
1021 } else if (PagesToFree
> 0) {
1023 // No Tail Guard, and pages after memory to free are still in Guard. It's a
1024 // partial free case. We need to keep one page to be a head Guard.
1030 *NumberOfPages
= PagesToFree
;
1034 Adjust the base and number of pages to really allocate according to Guard.
1036 @param[in,out] Memory Base address of free memory.
1037 @param[in,out] NumberOfPages Size of memory to allocate.
1043 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
,
1044 IN OUT UINTN
*NumberOfPages
1048 // FindFreePages() has already taken the Guard into account. It's safe to
1049 // adjust the start address and/or number of pages here, to make sure that
1050 // the Guards are also "allocated".
1052 if (!IsGuardPage (*Memory
+ EFI_PAGES_TO_SIZE (*NumberOfPages
))) {
1053 // No tail Guard, add one.
1054 *NumberOfPages
+= 1;
1057 if (!IsGuardPage (*Memory
- EFI_PAGE_SIZE
)) {
1058 // No head Guard, add one.
1059 *Memory
-= EFI_PAGE_SIZE
;
1060 *NumberOfPages
+= 1;
1065 Adjust the pool head position to make sure the Guard page is adjavent to
1066 pool tail or pool head.
1068 @param[in] Memory Base address of memory allocated.
1069 @param[in] NoPages Number of pages actually allocated.
1070 @param[in] Size Size of memory requested.
1071 (plus pool head/tail overhead)
1073 @return Address of pool head.
1077 IN EFI_PHYSICAL_ADDRESS Memory
,
1082 if (Memory
== 0 || (PcdGet8 (PcdHeapGuardPropertyMask
) & BIT7
) != 0) {
1084 // Pool head is put near the head Guard
1086 return (VOID
*)(UINTN
)Memory
;
1090 // Pool head is put near the tail Guard
1092 Size
= ALIGN_VALUE (Size
, 8);
1093 return (VOID
*)(UINTN
)(Memory
+ EFI_PAGES_TO_SIZE (NoPages
) - Size
);
1097 Get the page base address according to pool head address.
1099 @param[in] Memory Head address of pool to free.
1101 @return Address of pool head.
1105 IN EFI_PHYSICAL_ADDRESS Memory
1108 if (Memory
== 0 || (PcdGet8 (PcdHeapGuardPropertyMask
) & BIT7
) != 0) {
1110 // Pool head is put near the head Guard
1112 return (VOID
*)(UINTN
)Memory
;
1116 // Pool head is put near the tail Guard
1118 return (VOID
*)(UINTN
)(Memory
& ~EFI_PAGE_MASK
);
1122 Allocate or free guarded memory.
1124 @param[in] Start Start address of memory to allocate or free.
1125 @param[in] NumberOfPages Memory size in pages.
1126 @param[in] NewType Memory type to convert to.
1131 CoreConvertPagesWithGuard (
1133 IN UINTN NumberOfPages
,
1134 IN EFI_MEMORY_TYPE NewType
1140 if (NewType
== EfiConventionalMemory
) {
1142 OldPages
= NumberOfPages
;
1144 AdjustMemoryF (&Start
, &NumberOfPages
);
1146 // It's safe to unset Guard page inside memory lock because there should
1147 // be no memory allocation occurred in updating memory page attribute at
1148 // this point. And unsetting Guard page before free will prevent Guard
1149 // page just freed back to pool from being allocated right away before
1150 // marking it usable (from non-present to present).
1152 UnsetGuardForMemory (OldStart
, OldPages
);
1153 if (NumberOfPages
== 0) {
1157 AdjustMemoryA (&Start
, &NumberOfPages
);
1160 return CoreConvertPages (Start
, NumberOfPages
, NewType
);
1164 Helper function to convert a UINT64 value in binary to a string.
1166 @param[in] Value Value of a UINT64 integer.
1167 @param[out] BinString String buffer to contain the conversion result.
1174 OUT CHAR8
*BinString
1179 if (BinString
== NULL
) {
1183 for (Index
= 64; Index
> 0; --Index
) {
1184 BinString
[Index
- 1] = '0' + (Value
& 1);
1185 Value
= RShiftU64 (Value
, 1);
1187 BinString
[64] = '\0';
1191 Dump the guarded memory bit map.
1195 DumpGuardedMemoryBitmap (
1199 UINTN Entries
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1200 UINTN Shifts
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1201 UINTN Indices
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1202 UINT64 Tables
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1203 UINT64 Addresses
[GUARDED_HEAP_MAP_TABLE_DEPTH
];
1208 CHAR8 String
[GUARDED_HEAP_MAP_ENTRY_BITS
+ 1];
1212 if (mGuardedMemoryMap
== 0 ||
1214 mMapLevel
> GUARDED_HEAP_MAP_TABLE_DEPTH
) {
1218 Ruler1
= " 3 2 1 0";
1219 Ruler2
= "FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210";
1221 DEBUG ((HEAP_GUARD_DEBUG_LEVEL
, "============================="
1222 " Guarded Memory Bitmap "
1223 "==============================\r\n"));
1224 DEBUG ((HEAP_GUARD_DEBUG_LEVEL
, " %a\r\n", Ruler1
));
1225 DEBUG ((HEAP_GUARD_DEBUG_LEVEL
, " %a\r\n", Ruler2
));
1227 CopyMem (Entries
, mLevelMask
, sizeof (Entries
));
1228 CopyMem (Shifts
, mLevelShift
, sizeof (Shifts
));
1230 SetMem (Indices
, sizeof(Indices
), 0);
1231 SetMem (Tables
, sizeof(Tables
), 0);
1232 SetMem (Addresses
, sizeof(Addresses
), 0);
1234 Level
= GUARDED_HEAP_MAP_TABLE_DEPTH
- mMapLevel
;
1235 Tables
[Level
] = mGuardedMemoryMap
;
1240 if (Indices
[Level
] > Entries
[Level
]) {
1247 HEAP_GUARD_DEBUG_LEVEL
,
1248 "========================================="
1249 "=========================================\r\n"
1254 TableEntry
= ((UINT64
*)(UINTN
)Tables
[Level
])[Indices
[Level
]];
1255 Address
= Addresses
[Level
];
1257 if (TableEntry
== 0) {
1259 if (Level
== GUARDED_HEAP_MAP_TABLE_DEPTH
- 1) {
1260 if (RepeatZero
== 0) {
1261 Uint64ToBinString(TableEntry
, String
);
1262 DEBUG ((HEAP_GUARD_DEBUG_LEVEL
, "%016lx: %a\r\n", Address
, String
));
1263 } else if (RepeatZero
== 1) {
1264 DEBUG ((HEAP_GUARD_DEBUG_LEVEL
, "... : ...\r\n"));
1269 } else if (Level
< GUARDED_HEAP_MAP_TABLE_DEPTH
- 1) {
1272 Tables
[Level
] = TableEntry
;
1273 Addresses
[Level
] = Address
;
1282 Uint64ToBinString(TableEntry
, String
);
1283 DEBUG ((HEAP_GUARD_DEBUG_LEVEL
, "%016lx: %a\r\n", Address
, String
));
1288 if (Level
< (GUARDED_HEAP_MAP_TABLE_DEPTH
- (INTN
)mMapLevel
)) {
1292 Indices
[Level
] += 1;
1293 Address
= (Level
== 0) ? 0 : Addresses
[Level
- 1];
1294 Addresses
[Level
] = Address
| LShiftU64(Indices
[Level
], Shifts
[Level
]);