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