]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Mem/HeapGuard.c
f6068c459ca75385c242da9639146a67508c5bba
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / HeapGuard.c
1 /** @file
2 UEFI Heap Guard functions.
3
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
9
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.
12
13 **/
14
15 #include "DxeMain.h"
16 #include "Imem.h"
17 #include "HeapGuard.h"
18
19 //
20 // Global to avoid infinite reentrance of memory allocation when updating
21 // page table attributes, which may need allocate pages for new PDE/PTE.
22 //
23 GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mOnGuarding = FALSE;
24
25 //
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.
29 //
30 GLOBAL_REMOVE_IF_UNREFERENCED UINT64 mGuardedMemoryMap = 0;
31
32 //
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.
36 //
37 GLOBAL_REMOVE_IF_UNREFERENCED UINTN mMapLevel = 1;
38
39 //
40 // Shift and mask for each level of map table
41 //
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;
46
47 /**
48 Set corresponding bits in bitmap table to 1 according to the address.
49
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.
53
54 @return VOID.
55 **/
56 STATIC
57 VOID
58 SetBits (
59 IN EFI_PHYSICAL_ADDRESS Address,
60 IN UINTN BitNumber,
61 IN UINT64 *BitMap
62 )
63 {
64 UINTN Lsbs;
65 UINTN Qwords;
66 UINTN Msbs;
67 UINTN StartBit;
68 UINTN EndBit;
69
70 StartBit = (UINTN)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address);
71 EndBit = (StartBit + BitNumber - 1) % GUARDED_HEAP_MAP_ENTRY_BITS;
72
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;
78 } else {
79 Msbs = BitNumber;
80 Lsbs = 0;
81 Qwords = 0;
82 }
83
84 if (Msbs > 0) {
85 *BitMap |= LShiftU64 (LShiftU64 (1, Msbs) - 1, StartBit);
86 BitMap += 1;
87 }
88
89 if (Qwords > 0) {
90 SetMem64 ((VOID *)BitMap, Qwords * GUARDED_HEAP_MAP_ENTRY_BYTES,
91 (UINT64)-1);
92 BitMap += Qwords;
93 }
94
95 if (Lsbs > 0) {
96 *BitMap |= (LShiftU64 (1, Lsbs) - 1);
97 }
98 }
99
100 /**
101 Set corresponding bits in bitmap table to 0 according to the address.
102
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.
106
107 @return VOID.
108 **/
109 STATIC
110 VOID
111 ClearBits (
112 IN EFI_PHYSICAL_ADDRESS Address,
113 IN UINTN BitNumber,
114 IN UINT64 *BitMap
115 )
116 {
117 UINTN Lsbs;
118 UINTN Qwords;
119 UINTN Msbs;
120 UINTN StartBit;
121 UINTN EndBit;
122
123 StartBit = (UINTN)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address);
124 EndBit = (StartBit + BitNumber - 1) % GUARDED_HEAP_MAP_ENTRY_BITS;
125
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;
131 } else {
132 Msbs = BitNumber;
133 Lsbs = 0;
134 Qwords = 0;
135 }
136
137 if (Msbs > 0) {
138 *BitMap &= ~LShiftU64 (LShiftU64 (1, Msbs) - 1, StartBit);
139 BitMap += 1;
140 }
141
142 if (Qwords > 0) {
143 SetMem64 ((VOID *)BitMap, Qwords * GUARDED_HEAP_MAP_ENTRY_BYTES, 0);
144 BitMap += Qwords;
145 }
146
147 if (Lsbs > 0) {
148 *BitMap &= ~(LShiftU64 (1, Lsbs) - 1);
149 }
150 }
151
152 /**
153 Get corresponding bits in bitmap table according to the address.
154
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.
157
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.
161
162 @return An integer containing the bits information.
163 **/
164 STATIC
165 UINT64
166 GetBits (
167 IN EFI_PHYSICAL_ADDRESS Address,
168 IN UINTN BitNumber,
169 IN UINT64 *BitMap
170 )
171 {
172 UINTN StartBit;
173 UINTN EndBit;
174 UINTN Lsbs;
175 UINTN Msbs;
176 UINT64 Result;
177
178 ASSERT (BitNumber <= GUARDED_HEAP_MAP_ENTRY_BITS);
179
180 StartBit = (UINTN)GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address);
181 EndBit = (StartBit + BitNumber - 1) % GUARDED_HEAP_MAP_ENTRY_BITS;
182
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;
186 } else {
187 Msbs = BitNumber;
188 Lsbs = 0;
189 }
190
191 Result = RShiftU64 ((*BitMap), StartBit) & (LShiftU64 (1, Msbs) - 1);
192 if (Lsbs > 0) {
193 BitMap += 1;
194 Result |= LShiftU64 ((*BitMap) & (LShiftU64 (1, Lsbs) - 1), Msbs);
195 }
196
197 return Result;
198 }
199
200 /**
201 Locate the pointer of bitmap from the guarded memory bitmap tables, which
202 covers the given Address.
203
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.
207
208 @return The bit number from given Address to the end of current map table.
209 **/
210 UINTN
211 FindGuardedMemoryMap (
212 IN EFI_PHYSICAL_ADDRESS Address,
213 IN BOOLEAN AllocMapUnit,
214 OUT UINT64 **BitMap
215 )
216 {
217 UINTN Level;
218 UINT64 *GuardMap;
219 UINT64 MapMemory;
220 UINTN Index;
221 UINTN Size;
222 UINTN BitsToUnitEnd;
223 EFI_STATUS Status;
224
225 //
226 // Adjust current map table depth according to the address to access
227 //
228 while (AllocMapUnit &&
229 mMapLevel < GUARDED_HEAP_MAP_TABLE_DEPTH &&
230 RShiftU64 (
231 Address,
232 mLevelShift[GUARDED_HEAP_MAP_TABLE_DEPTH - mMapLevel - 1]
233 ) != 0) {
234
235 if (mGuardedMemoryMap != 0) {
236 Size = (mLevelMask[GUARDED_HEAP_MAP_TABLE_DEPTH - mMapLevel - 1] + 1)
237 * GUARDED_HEAP_MAP_ENTRY_BYTES;
238 Status = CoreInternalAllocatePages (
239 AllocateAnyPages,
240 EfiBootServicesData,
241 EFI_SIZE_TO_PAGES (Size),
242 &MapMemory,
243 FALSE
244 );
245 ASSERT_EFI_ERROR (Status);
246 ASSERT (MapMemory != 0);
247
248 SetMem ((VOID *)(UINTN)MapMemory, Size, 0);
249
250 *(UINT64 *)(UINTN)MapMemory = mGuardedMemoryMap;
251 mGuardedMemoryMap = MapMemory;
252 }
253
254 mMapLevel++;
255
256 }
257
258 GuardMap = &mGuardedMemoryMap;
259 for (Level = GUARDED_HEAP_MAP_TABLE_DEPTH - mMapLevel;
260 Level < GUARDED_HEAP_MAP_TABLE_DEPTH;
261 ++Level) {
262
263 if (*GuardMap == 0) {
264 if (!AllocMapUnit) {
265 GuardMap = NULL;
266 break;
267 }
268
269 Size = (mLevelMask[Level] + 1) * GUARDED_HEAP_MAP_ENTRY_BYTES;
270 Status = CoreInternalAllocatePages (
271 AllocateAnyPages,
272 EfiBootServicesData,
273 EFI_SIZE_TO_PAGES (Size),
274 &MapMemory,
275 FALSE
276 );
277 ASSERT_EFI_ERROR (Status);
278 ASSERT (MapMemory != 0);
279
280 SetMem ((VOID *)(UINTN)MapMemory, Size, 0);
281 *GuardMap = MapMemory;
282 }
283
284 Index = (UINTN)RShiftU64 (Address, mLevelShift[Level]);
285 Index &= mLevelMask[Level];
286 GuardMap = (UINT64 *)(UINTN)((*GuardMap) + Index * sizeof (UINT64));
287
288 }
289
290 BitsToUnitEnd = GUARDED_HEAP_MAP_BITS - GUARDED_HEAP_MAP_BIT_INDEX (Address);
291 *BitMap = GuardMap;
292
293 return BitsToUnitEnd;
294 }
295
296 /**
297 Set corresponding bits in bitmap table to 1 according to given memory range.
298
299 @param[in] Address Memory address to guard from.
300 @param[in] NumberOfPages Number of pages to guard.
301
302 @return VOID.
303 **/
304 VOID
305 EFIAPI
306 SetGuardedMemoryBits (
307 IN EFI_PHYSICAL_ADDRESS Address,
308 IN UINTN NumberOfPages
309 )
310 {
311 UINT64 *BitMap;
312 UINTN Bits;
313 UINTN BitsToUnitEnd;
314
315 while (NumberOfPages > 0) {
316 BitsToUnitEnd = FindGuardedMemoryMap (Address, TRUE, &BitMap);
317 ASSERT (BitMap != NULL);
318
319 if (NumberOfPages > BitsToUnitEnd) {
320 // Cross map unit
321 Bits = BitsToUnitEnd;
322 } else {
323 Bits = NumberOfPages;
324 }
325
326 SetBits (Address, Bits, BitMap);
327
328 NumberOfPages -= Bits;
329 Address += EFI_PAGES_TO_SIZE (Bits);
330 }
331 }
332
333 /**
334 Clear corresponding bits in bitmap table according to given memory range.
335
336 @param[in] Address Memory address to unset from.
337 @param[in] NumberOfPages Number of pages to unset guard.
338
339 @return VOID.
340 **/
341 VOID
342 EFIAPI
343 ClearGuardedMemoryBits (
344 IN EFI_PHYSICAL_ADDRESS Address,
345 IN UINTN NumberOfPages
346 )
347 {
348 UINT64 *BitMap;
349 UINTN Bits;
350 UINTN BitsToUnitEnd;
351
352 while (NumberOfPages > 0) {
353 BitsToUnitEnd = FindGuardedMemoryMap (Address, TRUE, &BitMap);
354 ASSERT (BitMap != NULL);
355
356 if (NumberOfPages > BitsToUnitEnd) {
357 // Cross map unit
358 Bits = BitsToUnitEnd;
359 } else {
360 Bits = NumberOfPages;
361 }
362
363 ClearBits (Address, Bits, BitMap);
364
365 NumberOfPages -= Bits;
366 Address += EFI_PAGES_TO_SIZE (Bits);
367 }
368 }
369
370 /**
371 Retrieve corresponding bits in bitmap table according to given memory range.
372
373 @param[in] Address Memory address to retrieve from.
374 @param[in] NumberOfPages Number of pages to retrieve.
375
376 @return An integer containing the guarded memory bitmap.
377 **/
378 UINTN
379 GetGuardedMemoryBits (
380 IN EFI_PHYSICAL_ADDRESS Address,
381 IN UINTN NumberOfPages
382 )
383 {
384 UINT64 *BitMap;
385 UINTN Bits;
386 UINTN Result;
387 UINTN Shift;
388 UINTN BitsToUnitEnd;
389
390 ASSERT (NumberOfPages <= GUARDED_HEAP_MAP_ENTRY_BITS);
391
392 Result = 0;
393 Shift = 0;
394 while (NumberOfPages > 0) {
395 BitsToUnitEnd = FindGuardedMemoryMap (Address, FALSE, &BitMap);
396
397 if (NumberOfPages > BitsToUnitEnd) {
398 // Cross map unit
399 Bits = BitsToUnitEnd;
400 } else {
401 Bits = NumberOfPages;
402 }
403
404 if (BitMap != NULL) {
405 Result |= LShiftU64 (GetBits (Address, Bits, BitMap), Shift);
406 }
407
408 Shift += Bits;
409 NumberOfPages -= Bits;
410 Address += EFI_PAGES_TO_SIZE (Bits);
411 }
412
413 return Result;
414 }
415
416 /**
417 Get bit value in bitmap table for the given address.
418
419 @param[in] Address The address to retrieve for.
420
421 @return 1 or 0.
422 **/
423 UINTN
424 EFIAPI
425 GetGuardMapBit (
426 IN EFI_PHYSICAL_ADDRESS Address
427 )
428 {
429 UINT64 *GuardMap;
430
431 FindGuardedMemoryMap (Address, FALSE, &GuardMap);
432 if (GuardMap != NULL) {
433 if (RShiftU64 (*GuardMap,
434 GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address)) & 1) {
435 return 1;
436 }
437 }
438
439 return 0;
440 }
441
442 /**
443 Set the bit in bitmap table for the given address.
444
445 @param[in] Address The address to set for.
446
447 @return VOID.
448 **/
449 VOID
450 EFIAPI
451 SetGuardMapBit (
452 IN EFI_PHYSICAL_ADDRESS Address
453 )
454 {
455 UINT64 *GuardMap;
456 UINT64 BitMask;
457
458 FindGuardedMemoryMap (Address, TRUE, &GuardMap);
459 if (GuardMap != NULL) {
460 BitMask = LShiftU64 (1, GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address));
461 *GuardMap |= BitMask;
462 }
463 }
464
465 /**
466 Clear the bit in bitmap table for the given address.
467
468 @param[in] Address The address to clear for.
469
470 @return VOID.
471 **/
472 VOID
473 EFIAPI
474 ClearGuardMapBit (
475 IN EFI_PHYSICAL_ADDRESS Address
476 )
477 {
478 UINT64 *GuardMap;
479 UINT64 BitMask;
480
481 FindGuardedMemoryMap (Address, TRUE, &GuardMap);
482 if (GuardMap != NULL) {
483 BitMask = LShiftU64 (1, GUARDED_HEAP_MAP_ENTRY_BIT_INDEX (Address));
484 *GuardMap &= ~BitMask;
485 }
486 }
487
488 /**
489 Check to see if the page at the given address is a Guard page or not.
490
491 @param[in] Address The address to check for.
492
493 @return TRUE The page at Address is a Guard page.
494 @return FALSE The page at Address is not a Guard page.
495 **/
496 BOOLEAN
497 EFIAPI
498 IsGuardPage (
499 IN EFI_PHYSICAL_ADDRESS Address
500 )
501 {
502 UINTN BitMap;
503
504 //
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
507 // 001, 100 and 101
508 //
509 BitMap = GetGuardedMemoryBits (Address - EFI_PAGE_SIZE, 3);
510 return ((BitMap == BIT0) || (BitMap == BIT2) || (BitMap == (BIT2 | BIT0)));
511 }
512
513 /**
514 Check to see if the page at the given address is a head Guard page or not.
515
516 @param[in] Address The address to check for
517
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
520 **/
521 BOOLEAN
522 EFIAPI
523 IsHeadGuard (
524 IN EFI_PHYSICAL_ADDRESS Address
525 )
526 {
527 return (GetGuardedMemoryBits (Address, 2) == BIT1);
528 }
529
530 /**
531 Check to see if the page at the given address is a tail Guard page or not.
532
533 @param[in] Address The address to check for.
534
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.
537 **/
538 BOOLEAN
539 EFIAPI
540 IsTailGuard (
541 IN EFI_PHYSICAL_ADDRESS Address
542 )
543 {
544 return (GetGuardedMemoryBits (Address - EFI_PAGE_SIZE, 2) == BIT0);
545 }
546
547 /**
548 Check to see if the page at the given address is guarded or not.
549
550 @param[in] Address The address to check for.
551
552 @return TRUE The page at Address is guarded.
553 @return FALSE The page at Address is not guarded.
554 **/
555 BOOLEAN
556 EFIAPI
557 IsMemoryGuarded (
558 IN EFI_PHYSICAL_ADDRESS Address
559 )
560 {
561 return (GetGuardMapBit (Address) == 1);
562 }
563
564 /**
565 Set the page at the given address to be a Guard page.
566
567 This is done by changing the page table attribute to be NOT PRSENT.
568
569 @param[in] BaseAddress Page address to Guard at
570
571 @return VOID
572 **/
573 VOID
574 EFIAPI
575 SetGuardPage (
576 IN EFI_PHYSICAL_ADDRESS BaseAddress
577 )
578 {
579 if (gCpu == NULL) {
580 return;
581 }
582
583 //
584 // Set flag to make sure allocating memory without GUARD for page table
585 // operation; otherwise infinite loops could be caused.
586 //
587 mOnGuarding = TRUE;
588 //
589 // Note: This might overwrite other attributes needed by other features,
590 // such as NX memory protection.
591 //
592 gCpu->SetMemoryAttributes (gCpu, BaseAddress, EFI_PAGE_SIZE, EFI_MEMORY_RP);
593 mOnGuarding = FALSE;
594 }
595
596 /**
597 Unset the Guard page at the given address to the normal memory.
598
599 This is done by changing the page table attribute to be PRSENT.
600
601 @param[in] BaseAddress Page address to Guard at.
602
603 @return VOID.
604 **/
605 VOID
606 EFIAPI
607 UnsetGuardPage (
608 IN EFI_PHYSICAL_ADDRESS BaseAddress
609 )
610 {
611 UINT64 Attributes;
612
613 if (gCpu == NULL) {
614 return;
615 }
616
617 //
618 // Once the Guard page is unset, it will be freed back to memory pool. NX
619 // memory protection must be restored for this page if NX is enabled for free
620 // memory.
621 //
622 Attributes = 0;
623 if ((PcdGet64 (PcdDxeNxMemoryProtectionPolicy) & (1 << EfiConventionalMemory)) != 0) {
624 Attributes |= EFI_MEMORY_XP;
625 }
626
627 //
628 // Set flag to make sure allocating memory without GUARD for page table
629 // operation; otherwise infinite loops could be caused.
630 //
631 mOnGuarding = TRUE;
632 //
633 // Note: This might overwrite other attributes needed by other features,
634 // such as memory protection (NX). Please make sure they are not enabled
635 // at the same time.
636 //
637 gCpu->SetMemoryAttributes (gCpu, BaseAddress, EFI_PAGE_SIZE, Attributes);
638 mOnGuarding = FALSE;
639 }
640
641 /**
642 Check to see if the memory at the given address should be guarded or not.
643
644 @param[in] MemoryType Memory type to check.
645 @param[in] AllocateType Allocation type to check.
646 @param[in] PageOrPool Indicate a page allocation or pool allocation.
647
648
649 @return TRUE The given type of memory should be guarded.
650 @return FALSE The given type of memory should not be guarded.
651 **/
652 BOOLEAN
653 IsMemoryTypeToGuard (
654 IN EFI_MEMORY_TYPE MemoryType,
655 IN EFI_ALLOCATE_TYPE AllocateType,
656 IN UINT8 PageOrPool
657 )
658 {
659 UINT64 TestBit;
660 UINT64 ConfigBit;
661 BOOLEAN InSmm;
662
663 if (AllocateType == AllocateAddress) {
664 return FALSE;
665 }
666
667 InSmm = FALSE;
668 if (gSmmBase2 != NULL) {
669 gSmmBase2->InSmm (gSmmBase2, &InSmm);
670 }
671
672 if (InSmm) {
673 return FALSE;
674 }
675
676 if ((PcdGet8 (PcdHeapGuardPropertyMask) & PageOrPool) == 0) {
677 return FALSE;
678 }
679
680 if (PageOrPool == GUARD_HEAP_TYPE_POOL) {
681 ConfigBit = PcdGet64 (PcdHeapGuardPoolType);
682 } else if (PageOrPool == GUARD_HEAP_TYPE_PAGE) {
683 ConfigBit = PcdGet64 (PcdHeapGuardPageType);
684 } else {
685 ConfigBit = (UINT64)-1;
686 }
687
688 if ((UINT32)MemoryType >= MEMORY_TYPE_OS_RESERVED_MIN) {
689 TestBit = BIT63;
690 } else if ((UINT32) MemoryType >= MEMORY_TYPE_OEM_RESERVED_MIN) {
691 TestBit = BIT62;
692 } else if (MemoryType < EfiMaxMemoryType) {
693 TestBit = LShiftU64 (1, MemoryType);
694 } else if (MemoryType == EfiMaxMemoryType) {
695 TestBit = (UINT64)-1;
696 } else {
697 TestBit = 0;
698 }
699
700 return ((ConfigBit & TestBit) != 0);
701 }
702
703 /**
704 Check to see if the pool at the given address should be guarded or not.
705
706 @param[in] MemoryType Pool type to check.
707
708
709 @return TRUE The given type of pool should be guarded.
710 @return FALSE The given type of pool should not be guarded.
711 **/
712 BOOLEAN
713 IsPoolTypeToGuard (
714 IN EFI_MEMORY_TYPE MemoryType
715 )
716 {
717 return IsMemoryTypeToGuard (MemoryType, AllocateAnyPages,
718 GUARD_HEAP_TYPE_POOL);
719 }
720
721 /**
722 Check to see if the page at the given address should be guarded or not.
723
724 @param[in] MemoryType Page type to check.
725 @param[in] AllocateType Allocation type to check.
726
727 @return TRUE The given type of page should be guarded.
728 @return FALSE The given type of page should not be guarded.
729 **/
730 BOOLEAN
731 IsPageTypeToGuard (
732 IN EFI_MEMORY_TYPE MemoryType,
733 IN EFI_ALLOCATE_TYPE AllocateType
734 )
735 {
736 return IsMemoryTypeToGuard (MemoryType, AllocateType, GUARD_HEAP_TYPE_PAGE);
737 }
738
739 /**
740 Check to see if the heap guard is enabled for page and/or pool allocation.
741
742 @return TRUE/FALSE.
743 **/
744 BOOLEAN
745 IsHeapGuardEnabled (
746 VOID
747 )
748 {
749 return IsMemoryTypeToGuard (EfiMaxMemoryType, AllocateAnyPages,
750 GUARD_HEAP_TYPE_POOL|GUARD_HEAP_TYPE_PAGE);
751 }
752
753 /**
754 Set head Guard and tail Guard for the given memory range.
755
756 @param[in] Memory Base address of memory to set guard for.
757 @param[in] NumberOfPages Memory size in pages.
758
759 @return VOID
760 **/
761 VOID
762 SetGuardForMemory (
763 IN EFI_PHYSICAL_ADDRESS Memory,
764 IN UINTN NumberOfPages
765 )
766 {
767 EFI_PHYSICAL_ADDRESS GuardPage;
768
769 //
770 // Set tail Guard
771 //
772 GuardPage = Memory + EFI_PAGES_TO_SIZE (NumberOfPages);
773 if (!IsGuardPage (GuardPage)) {
774 SetGuardPage (GuardPage);
775 }
776
777 // Set head Guard
778 GuardPage = Memory - EFI_PAGES_TO_SIZE (1);
779 if (!IsGuardPage (GuardPage)) {
780 SetGuardPage (GuardPage);
781 }
782
783 //
784 // Mark the memory range as Guarded
785 //
786 SetGuardedMemoryBits (Memory, NumberOfPages);
787 }
788
789 /**
790 Unset head Guard and tail Guard for the given memory range.
791
792 @param[in] Memory Base address of memory to unset guard for.
793 @param[in] NumberOfPages Memory size in pages.
794
795 @return VOID
796 **/
797 VOID
798 UnsetGuardForMemory (
799 IN EFI_PHYSICAL_ADDRESS Memory,
800 IN UINTN NumberOfPages
801 )
802 {
803 EFI_PHYSICAL_ADDRESS GuardPage;
804 UINT64 GuardBitmap;
805
806 if (NumberOfPages == 0) {
807 return;
808 }
809
810 //
811 // Head Guard must be one page before, if any.
812 //
813 // MSB-> 1 0 <-LSB
814 // -------------------
815 // Head Guard -> 0 1 -> Don't free Head Guard (shared Guard)
816 // Head Guard -> 0 0 -> Free Head Guard either (not shared Guard)
817 // 1 X -> Don't free first page (need a new Guard)
818 // (it'll be turned into a Guard page later)
819 // -------------------
820 // Start -> -1 -2
821 //
822 GuardPage = Memory - EFI_PAGES_TO_SIZE (1);
823 GuardBitmap = GetGuardedMemoryBits (Memory - EFI_PAGES_TO_SIZE (2), 2);
824 if ((GuardBitmap & BIT1) == 0) {
825 //
826 // Head Guard exists.
827 //
828 if ((GuardBitmap & BIT0) == 0) {
829 //
830 // If the head Guard is not a tail Guard of adjacent memory block,
831 // unset it.
832 //
833 UnsetGuardPage (GuardPage);
834 }
835 } else {
836 //
837 // Pages before memory to free are still in Guard. It's a partial free
838 // case. Turn first page of memory block to free into a new Guard.
839 //
840 SetGuardPage (Memory);
841 }
842
843 //
844 // Tail Guard must be the page after this memory block to free, if any.
845 //
846 // MSB-> 1 0 <-LSB
847 // --------------------
848 // 1 0 <- Tail Guard -> Don't free Tail Guard (shared Guard)
849 // 0 0 <- Tail Guard -> Free Tail Guard either (not shared Guard)
850 // X 1 -> Don't free last page (need a new Guard)
851 // (it'll be turned into a Guard page later)
852 // --------------------
853 // +1 +0 <- End
854 //
855 GuardPage = Memory + EFI_PAGES_TO_SIZE (NumberOfPages);
856 GuardBitmap = GetGuardedMemoryBits (GuardPage, 2);
857 if ((GuardBitmap & BIT0) == 0) {
858 //
859 // Tail Guard exists.
860 //
861 if ((GuardBitmap & BIT1) == 0) {
862 //
863 // If the tail Guard is not a head Guard of adjacent memory block,
864 // free it; otherwise, keep it.
865 //
866 UnsetGuardPage (GuardPage);
867 }
868 } else {
869 //
870 // Pages after memory to free are still in Guard. It's a partial free
871 // case. We need to keep one page to be a head Guard.
872 //
873 SetGuardPage (GuardPage - EFI_PAGES_TO_SIZE (1));
874 }
875
876 //
877 // No matter what, we just clear the mark of the Guarded memory.
878 //
879 ClearGuardedMemoryBits(Memory, NumberOfPages);
880 }
881
882 /**
883 Adjust address of free memory according to existing and/or required Guard.
884
885 This function will check if there're existing Guard pages of adjacent
886 memory blocks, and try to use it as the Guard page of the memory to be
887 allocated.
888
889 @param[in] Start Start address of free memory block.
890 @param[in] Size Size of free memory block.
891 @param[in] SizeRequested Size of memory to allocate.
892
893 @return The end address of memory block found.
894 @return 0 if no enough space for the required size of memory and its Guard.
895 **/
896 UINT64
897 AdjustMemoryS (
898 IN UINT64 Start,
899 IN UINT64 Size,
900 IN UINT64 SizeRequested
901 )
902 {
903 UINT64 Target;
904
905 //
906 // UEFI spec requires that allocated pool must be 8-byte aligned. If it's
907 // indicated to put the pool near the Tail Guard, we need extra bytes to
908 // make sure alignment of the returned pool address.
909 //
910 if ((PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) == 0) {
911 SizeRequested = ALIGN_VALUE(SizeRequested, 8);
912 }
913
914 Target = Start + Size - SizeRequested;
915 ASSERT (Target >= Start);
916 if (Target == 0) {
917 return 0;
918 }
919
920 if (!IsGuardPage (Start + Size)) {
921 // No Guard at tail to share. One more page is needed.
922 Target -= EFI_PAGES_TO_SIZE (1);
923 }
924
925 // Out of range?
926 if (Target < Start) {
927 return 0;
928 }
929
930 // At the edge?
931 if (Target == Start) {
932 if (!IsGuardPage (Target - EFI_PAGES_TO_SIZE (1))) {
933 // No enough space for a new head Guard if no Guard at head to share.
934 return 0;
935 }
936 }
937
938 // OK, we have enough pages for memory and its Guards. Return the End of the
939 // free space.
940 return Target + SizeRequested - 1;
941 }
942
943 /**
944 Adjust the start address and number of pages to free according to Guard.
945
946 The purpose of this function is to keep the shared Guard page with adjacent
947 memory block if it's still in guard, or free it if no more sharing. Another
948 is to reserve pages as Guard pages in partial page free situation.
949
950 @param[in,out] Memory Base address of memory to free.
951 @param[in,out] NumberOfPages Size of memory to free.
952
953 @return VOID.
954 **/
955 VOID
956 AdjustMemoryF (
957 IN OUT EFI_PHYSICAL_ADDRESS *Memory,
958 IN OUT UINTN *NumberOfPages
959 )
960 {
961 EFI_PHYSICAL_ADDRESS Start;
962 EFI_PHYSICAL_ADDRESS MemoryToTest;
963 UINTN PagesToFree;
964 UINT64 GuardBitmap;
965
966 if (Memory == NULL || NumberOfPages == NULL || *NumberOfPages == 0) {
967 return;
968 }
969
970 Start = *Memory;
971 PagesToFree = *NumberOfPages;
972
973 //
974 // Head Guard must be one page before, if any.
975 //
976 // MSB-> 1 0 <-LSB
977 // -------------------
978 // Head Guard -> 0 1 -> Don't free Head Guard (shared Guard)
979 // Head Guard -> 0 0 -> Free Head Guard either (not shared Guard)
980 // 1 X -> Don't free first page (need a new Guard)
981 // (it'll be turned into a Guard page later)
982 // -------------------
983 // Start -> -1 -2
984 //
985 MemoryToTest = Start - EFI_PAGES_TO_SIZE (2);
986 GuardBitmap = GetGuardedMemoryBits (MemoryToTest, 2);
987 if ((GuardBitmap & BIT1) == 0) {
988 //
989 // Head Guard exists.
990 //
991 if ((GuardBitmap & BIT0) == 0) {
992 //
993 // If the head Guard is not a tail Guard of adjacent memory block,
994 // free it; otherwise, keep it.
995 //
996 Start -= EFI_PAGES_TO_SIZE (1);
997 PagesToFree += 1;
998 }
999 } else {
1000 //
1001 // No Head Guard, and pages before memory to free are still in Guard. It's a
1002 // partial free case. We need to keep one page to be a tail Guard.
1003 //
1004 Start += EFI_PAGES_TO_SIZE (1);
1005 PagesToFree -= 1;
1006 }
1007
1008 //
1009 // Tail Guard must be the page after this memory block to free, if any.
1010 //
1011 // MSB-> 1 0 <-LSB
1012 // --------------------
1013 // 1 0 <- Tail Guard -> Don't free Tail Guard (shared Guard)
1014 // 0 0 <- Tail Guard -> Free Tail Guard either (not shared Guard)
1015 // X 1 -> Don't free last page (need a new Guard)
1016 // (it'll be turned into a Guard page later)
1017 // --------------------
1018 // +1 +0 <- End
1019 //
1020 MemoryToTest = Start + EFI_PAGES_TO_SIZE (PagesToFree);
1021 GuardBitmap = GetGuardedMemoryBits (MemoryToTest, 2);
1022 if ((GuardBitmap & BIT0) == 0) {
1023 //
1024 // Tail Guard exists.
1025 //
1026 if ((GuardBitmap & BIT1) == 0) {
1027 //
1028 // If the tail Guard is not a head Guard of adjacent memory block,
1029 // free it; otherwise, keep it.
1030 //
1031 PagesToFree += 1;
1032 }
1033 } else if (PagesToFree > 0) {
1034 //
1035 // No Tail Guard, and pages after memory to free are still in Guard. It's a
1036 // partial free case. We need to keep one page to be a head Guard.
1037 //
1038 PagesToFree -= 1;
1039 }
1040
1041 *Memory = Start;
1042 *NumberOfPages = PagesToFree;
1043 }
1044
1045 /**
1046 Adjust the base and number of pages to really allocate according to Guard.
1047
1048 @param[in,out] Memory Base address of free memory.
1049 @param[in,out] NumberOfPages Size of memory to allocate.
1050
1051 @return VOID.
1052 **/
1053 VOID
1054 AdjustMemoryA (
1055 IN OUT EFI_PHYSICAL_ADDRESS *Memory,
1056 IN OUT UINTN *NumberOfPages
1057 )
1058 {
1059 //
1060 // FindFreePages() has already taken the Guard into account. It's safe to
1061 // adjust the start address and/or number of pages here, to make sure that
1062 // the Guards are also "allocated".
1063 //
1064 if (!IsGuardPage (*Memory + EFI_PAGES_TO_SIZE (*NumberOfPages))) {
1065 // No tail Guard, add one.
1066 *NumberOfPages += 1;
1067 }
1068
1069 if (!IsGuardPage (*Memory - EFI_PAGE_SIZE)) {
1070 // No head Guard, add one.
1071 *Memory -= EFI_PAGE_SIZE;
1072 *NumberOfPages += 1;
1073 }
1074 }
1075
1076 /**
1077 Adjust the pool head position to make sure the Guard page is adjavent to
1078 pool tail or pool head.
1079
1080 @param[in] Memory Base address of memory allocated.
1081 @param[in] NoPages Number of pages actually allocated.
1082 @param[in] Size Size of memory requested.
1083 (plus pool head/tail overhead)
1084
1085 @return Address of pool head.
1086 **/
1087 VOID *
1088 AdjustPoolHeadA (
1089 IN EFI_PHYSICAL_ADDRESS Memory,
1090 IN UINTN NoPages,
1091 IN UINTN Size
1092 )
1093 {
1094 if (Memory == 0 || (PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) != 0) {
1095 //
1096 // Pool head is put near the head Guard
1097 //
1098 return (VOID *)(UINTN)Memory;
1099 }
1100
1101 //
1102 // Pool head is put near the tail Guard
1103 //
1104 Size = ALIGN_VALUE (Size, 8);
1105 return (VOID *)(UINTN)(Memory + EFI_PAGES_TO_SIZE (NoPages) - Size);
1106 }
1107
1108 /**
1109 Get the page base address according to pool head address.
1110
1111 @param[in] Memory Head address of pool to free.
1112
1113 @return Address of pool head.
1114 **/
1115 VOID *
1116 AdjustPoolHeadF (
1117 IN EFI_PHYSICAL_ADDRESS Memory
1118 )
1119 {
1120 if (Memory == 0 || (PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) != 0) {
1121 //
1122 // Pool head is put near the head Guard
1123 //
1124 return (VOID *)(UINTN)Memory;
1125 }
1126
1127 //
1128 // Pool head is put near the tail Guard
1129 //
1130 return (VOID *)(UINTN)(Memory & ~EFI_PAGE_MASK);
1131 }
1132
1133 /**
1134 Allocate or free guarded memory.
1135
1136 @param[in] Start Start address of memory to allocate or free.
1137 @param[in] NumberOfPages Memory size in pages.
1138 @param[in] NewType Memory type to convert to.
1139
1140 @return VOID.
1141 **/
1142 EFI_STATUS
1143 CoreConvertPagesWithGuard (
1144 IN UINT64 Start,
1145 IN UINTN NumberOfPages,
1146 IN EFI_MEMORY_TYPE NewType
1147 )
1148 {
1149 UINT64 OldStart;
1150 UINTN OldPages;
1151
1152 if (NewType == EfiConventionalMemory) {
1153 OldStart = Start;
1154 OldPages = NumberOfPages;
1155
1156 AdjustMemoryF (&Start, &NumberOfPages);
1157 //
1158 // It's safe to unset Guard page inside memory lock because there should
1159 // be no memory allocation occurred in updating memory page attribute at
1160 // this point. And unsetting Guard page before free will prevent Guard
1161 // page just freed back to pool from being allocated right away before
1162 // marking it usable (from non-present to present).
1163 //
1164 UnsetGuardForMemory (OldStart, OldPages);
1165 if (NumberOfPages == 0) {
1166 return EFI_SUCCESS;
1167 }
1168 } else {
1169 AdjustMemoryA (&Start, &NumberOfPages);
1170 }
1171
1172 return CoreConvertPages (Start, NumberOfPages, NewType);
1173 }
1174
1175 /**
1176 Set all Guard pages which cannot be set before CPU Arch Protocol installed.
1177 **/
1178 VOID
1179 SetAllGuardPages (
1180 VOID
1181 )
1182 {
1183 UINTN Entries[GUARDED_HEAP_MAP_TABLE_DEPTH];
1184 UINTN Shifts[GUARDED_HEAP_MAP_TABLE_DEPTH];
1185 UINTN Indices[GUARDED_HEAP_MAP_TABLE_DEPTH];
1186 UINT64 Tables[GUARDED_HEAP_MAP_TABLE_DEPTH];
1187 UINT64 Addresses[GUARDED_HEAP_MAP_TABLE_DEPTH];
1188 UINT64 TableEntry;
1189 UINT64 Address;
1190 UINT64 GuardPage;
1191 INTN Level;
1192 UINTN Index;
1193 BOOLEAN OnGuarding;
1194
1195 if (mGuardedMemoryMap == 0 ||
1196 mMapLevel == 0 ||
1197 mMapLevel > GUARDED_HEAP_MAP_TABLE_DEPTH) {
1198 return;
1199 }
1200
1201 CopyMem (Entries, mLevelMask, sizeof (Entries));
1202 CopyMem (Shifts, mLevelShift, sizeof (Shifts));
1203
1204 SetMem (Tables, sizeof(Tables), 0);
1205 SetMem (Addresses, sizeof(Addresses), 0);
1206 SetMem (Indices, sizeof(Indices), 0);
1207
1208 Level = GUARDED_HEAP_MAP_TABLE_DEPTH - mMapLevel;
1209 Tables[Level] = mGuardedMemoryMap;
1210 Address = 0;
1211 OnGuarding = FALSE;
1212
1213 DEBUG_CODE (
1214 DumpGuardedMemoryBitmap ();
1215 );
1216
1217 while (TRUE) {
1218 if (Indices[Level] > Entries[Level]) {
1219 Tables[Level] = 0;
1220 Level -= 1;
1221 } else {
1222
1223 TableEntry = ((UINT64 *)(UINTN)(Tables[Level]))[Indices[Level]];
1224 Address = Addresses[Level];
1225
1226 if (TableEntry == 0) {
1227
1228 OnGuarding = FALSE;
1229
1230 } else if (Level < GUARDED_HEAP_MAP_TABLE_DEPTH - 1) {
1231
1232 Level += 1;
1233 Tables[Level] = TableEntry;
1234 Addresses[Level] = Address;
1235 Indices[Level] = 0;
1236
1237 continue;
1238
1239 } else {
1240
1241 Index = 0;
1242 while (Index < GUARDED_HEAP_MAP_ENTRY_BITS) {
1243 if ((TableEntry & 1) == 1) {
1244 if (OnGuarding) {
1245 GuardPage = 0;
1246 } else {
1247 GuardPage = Address - EFI_PAGE_SIZE;
1248 }
1249 OnGuarding = TRUE;
1250 } else {
1251 if (OnGuarding) {
1252 GuardPage = Address;
1253 } else {
1254 GuardPage = 0;
1255 }
1256 OnGuarding = FALSE;
1257 }
1258
1259 if (GuardPage != 0) {
1260 SetGuardPage (GuardPage);
1261 }
1262
1263 if (TableEntry == 0) {
1264 break;
1265 }
1266
1267 TableEntry = RShiftU64 (TableEntry, 1);
1268 Address += EFI_PAGE_SIZE;
1269 Index += 1;
1270 }
1271 }
1272 }
1273
1274 if (Level < (GUARDED_HEAP_MAP_TABLE_DEPTH - (INTN)mMapLevel)) {
1275 break;
1276 }
1277
1278 Indices[Level] += 1;
1279 Address = (Level == 0) ? 0 : Addresses[Level - 1];
1280 Addresses[Level] = Address | LShiftU64(Indices[Level], Shifts[Level]);
1281
1282 }
1283 }
1284
1285 /**
1286 Notify function used to set all Guard pages before CPU Arch Protocol installed.
1287 **/
1288 VOID
1289 HeapGuardCpuArchProtocolNotify (
1290 VOID
1291 )
1292 {
1293 ASSERT (gCpu != NULL);
1294 SetAllGuardPages ();
1295 }
1296
1297 /**
1298 Helper function to convert a UINT64 value in binary to a string.
1299
1300 @param[in] Value Value of a UINT64 integer.
1301 @param[out] BinString String buffer to contain the conversion result.
1302
1303 @return VOID.
1304 **/
1305 VOID
1306 Uint64ToBinString (
1307 IN UINT64 Value,
1308 OUT CHAR8 *BinString
1309 )
1310 {
1311 UINTN Index;
1312
1313 if (BinString == NULL) {
1314 return;
1315 }
1316
1317 for (Index = 64; Index > 0; --Index) {
1318 BinString[Index - 1] = '0' + (Value & 1);
1319 Value = RShiftU64 (Value, 1);
1320 }
1321 BinString[64] = '\0';
1322 }
1323
1324 /**
1325 Dump the guarded memory bit map.
1326 **/
1327 VOID
1328 EFIAPI
1329 DumpGuardedMemoryBitmap (
1330 VOID
1331 )
1332 {
1333 UINTN Entries[GUARDED_HEAP_MAP_TABLE_DEPTH];
1334 UINTN Shifts[GUARDED_HEAP_MAP_TABLE_DEPTH];
1335 UINTN Indices[GUARDED_HEAP_MAP_TABLE_DEPTH];
1336 UINT64 Tables[GUARDED_HEAP_MAP_TABLE_DEPTH];
1337 UINT64 Addresses[GUARDED_HEAP_MAP_TABLE_DEPTH];
1338 UINT64 TableEntry;
1339 UINT64 Address;
1340 INTN Level;
1341 UINTN RepeatZero;
1342 CHAR8 String[GUARDED_HEAP_MAP_ENTRY_BITS + 1];
1343 CHAR8 *Ruler1;
1344 CHAR8 *Ruler2;
1345
1346 if (mGuardedMemoryMap == 0 ||
1347 mMapLevel == 0 ||
1348 mMapLevel > GUARDED_HEAP_MAP_TABLE_DEPTH) {
1349 return;
1350 }
1351
1352 Ruler1 = " 3 2 1 0";
1353 Ruler2 = "FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210";
1354
1355 DEBUG ((HEAP_GUARD_DEBUG_LEVEL, "============================="
1356 " Guarded Memory Bitmap "
1357 "==============================\r\n"));
1358 DEBUG ((HEAP_GUARD_DEBUG_LEVEL, " %a\r\n", Ruler1));
1359 DEBUG ((HEAP_GUARD_DEBUG_LEVEL, " %a\r\n", Ruler2));
1360
1361 CopyMem (Entries, mLevelMask, sizeof (Entries));
1362 CopyMem (Shifts, mLevelShift, sizeof (Shifts));
1363
1364 SetMem (Indices, sizeof(Indices), 0);
1365 SetMem (Tables, sizeof(Tables), 0);
1366 SetMem (Addresses, sizeof(Addresses), 0);
1367
1368 Level = GUARDED_HEAP_MAP_TABLE_DEPTH - mMapLevel;
1369 Tables[Level] = mGuardedMemoryMap;
1370 Address = 0;
1371 RepeatZero = 0;
1372
1373 while (TRUE) {
1374 if (Indices[Level] > Entries[Level]) {
1375
1376 Tables[Level] = 0;
1377 Level -= 1;
1378 RepeatZero = 0;
1379
1380 DEBUG ((
1381 HEAP_GUARD_DEBUG_LEVEL,
1382 "========================================="
1383 "=========================================\r\n"
1384 ));
1385
1386 } else {
1387
1388 TableEntry = ((UINT64 *)(UINTN)Tables[Level])[Indices[Level]];
1389 Address = Addresses[Level];
1390
1391 if (TableEntry == 0) {
1392
1393 if (Level == GUARDED_HEAP_MAP_TABLE_DEPTH - 1) {
1394 if (RepeatZero == 0) {
1395 Uint64ToBinString(TableEntry, String);
1396 DEBUG ((HEAP_GUARD_DEBUG_LEVEL, "%016lx: %a\r\n", Address, String));
1397 } else if (RepeatZero == 1) {
1398 DEBUG ((HEAP_GUARD_DEBUG_LEVEL, "... : ...\r\n"));
1399 }
1400 RepeatZero += 1;
1401 }
1402
1403 } else if (Level < GUARDED_HEAP_MAP_TABLE_DEPTH - 1) {
1404
1405 Level += 1;
1406 Tables[Level] = TableEntry;
1407 Addresses[Level] = Address;
1408 Indices[Level] = 0;
1409 RepeatZero = 0;
1410
1411 continue;
1412
1413 } else {
1414
1415 RepeatZero = 0;
1416 Uint64ToBinString(TableEntry, String);
1417 DEBUG ((HEAP_GUARD_DEBUG_LEVEL, "%016lx: %a\r\n", Address, String));
1418
1419 }
1420 }
1421
1422 if (Level < (GUARDED_HEAP_MAP_TABLE_DEPTH - (INTN)mMapLevel)) {
1423 break;
1424 }
1425
1426 Indices[Level] += 1;
1427 Address = (Level == 0) ? 0 : Addresses[Level - 1];
1428 Addresses[Level] = Address | LShiftU64(Indices[Level], Shifts[Level]);
1429
1430 }
1431 }
1432