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