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