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