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