]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/PiSmmCore/HeapGuard.h
MdeModulePkg/S3SmmInitDone.h: Fix copyright coding style error.
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / HeapGuard.h
CommitLineData
e63da9f0
JW
1/** @file\r
2 Data structure and functions to allocate and free memory space.\r
3\r
4Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
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#ifndef _HEAPGUARD_H_\r
16#define _HEAPGUARD_H_\r
17\r
18#include "PiSmmCore.h"\r
19\r
20//\r
21// Following macros are used to define and access the guarded memory bitmap\r
22// table.\r
23//\r
24// To simplify the access and reduce the memory used for this table, the\r
25// table is constructed in the similar way as page table structure but in\r
26// reverse direction, i.e. from bottom growing up to top.\r
27//\r
28// - 1-bit tracks 1 page (4KB)\r
29// - 1-UINT64 map entry tracks 256KB memory\r
30// - 1K-UINT64 map table tracks 256MB memory\r
31// - Five levels of tables can track any address of memory of 64-bit\r
32// system, like below.\r
33//\r
34// 512 * 512 * 512 * 512 * 1K * 64b * 4K\r
35// 111111111 111111111 111111111 111111111 1111111111 111111 111111111111\r
36// 63 54 45 36 27 17 11 0\r
37// 9b 9b 9b 9b 10b 6b 12b\r
38// L0 -> L1 -> L2 -> L3 -> L4 -> bits -> page\r
39// 1FF 1FF 1FF 1FF 3FF 3F FFF\r
40//\r
41// L4 table has 1K * sizeof(UINT64) = 8K (2-page), which can track 256MB\r
42// memory. Each table of L0-L3 will be allocated when its memory address\r
43// range is to be tracked. Only 1-page will be allocated each time. This\r
44// can save memories used to establish this map table.\r
45//\r
46// For a normal configuration of system with 4G memory, two levels of tables\r
47// can track the whole memory, because two levels (L3+L4) of map tables have\r
48// already coverred 37-bit of memory address. And for a normal UEFI BIOS,\r
49// less than 128M memory would be consumed during boot. That means we just\r
50// need\r
51//\r
52// 1-page (L3) + 2-page (L4)\r
53//\r
54// memory (3 pages) to track the memory allocation works. In this case,\r
55// there's no need to setup L0-L2 tables.\r
56//\r
57\r
58//\r
59// Each entry occupies 8B/64b. 1-page can hold 512 entries, which spans 9\r
60// bits in address. (512 = 1 << 9)\r
61//\r
62#define BYTE_LENGTH_SHIFT 3 // (8 = 1 << 3)\r
63\r
64#define GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT \\r
65 (EFI_PAGE_SHIFT - BYTE_LENGTH_SHIFT)\r
66\r
67#define GUARDED_HEAP_MAP_TABLE_DEPTH 5\r
68\r
69// Use UINT64_index + bit_index_of_UINT64 to locate the bit in may\r
70#define GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT 6 // (64 = 1 << 6)\r
71\r
72#define GUARDED_HEAP_MAP_ENTRY_BITS \\r
73 (1 << GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT)\r
74\r
75#define GUARDED_HEAP_MAP_ENTRY_BYTES \\r
76 (GUARDED_HEAP_MAP_ENTRY_BITS / 8)\r
77\r
78// L4 table address width: 64 - 9 * 4 - 6 - 12 = 10b\r
79#define GUARDED_HEAP_MAP_ENTRY_SHIFT \\r
80 (GUARDED_HEAP_MAP_ENTRY_BITS \\r
81 - GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT * 4 \\r
82 - GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT \\r
83 - EFI_PAGE_SHIFT)\r
84\r
85// L4 table address mask: (1 << 10 - 1) = 0x3FF\r
86#define GUARDED_HEAP_MAP_ENTRY_MASK \\r
87 ((1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) - 1)\r
88\r
89// Size of each L4 table: (1 << 10) * 8 = 8KB = 2-page\r
90#define GUARDED_HEAP_MAP_SIZE \\r
91 ((1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) * GUARDED_HEAP_MAP_ENTRY_BYTES)\r
92\r
93// Memory size tracked by one L4 table: 8KB * 8 * 4KB = 256MB\r
94#define GUARDED_HEAP_MAP_UNIT_SIZE \\r
95 (GUARDED_HEAP_MAP_SIZE * 8 * EFI_PAGE_SIZE)\r
96\r
97// L4 table entry number: 8KB / 8 = 1024\r
98#define GUARDED_HEAP_MAP_ENTRIES_PER_UNIT \\r
99 (GUARDED_HEAP_MAP_SIZE / GUARDED_HEAP_MAP_ENTRY_BYTES)\r
100\r
101// L4 table entry indexing\r
102#define GUARDED_HEAP_MAP_ENTRY_INDEX(Address) \\r
103 (RShiftU64 (Address, EFI_PAGE_SHIFT \\r
104 + GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT) \\r
105 & GUARDED_HEAP_MAP_ENTRY_MASK)\r
106\r
107// L4 table entry bit indexing\r
108#define GUARDED_HEAP_MAP_ENTRY_BIT_INDEX(Address) \\r
109 (RShiftU64 (Address, EFI_PAGE_SHIFT) \\r
110 & ((1 << GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT) - 1))\r
111\r
112//\r
113// Total bits (pages) tracked by one L4 table (65536-bit)\r
114//\r
115#define GUARDED_HEAP_MAP_BITS \\r
116 (1 << (GUARDED_HEAP_MAP_ENTRY_SHIFT \\r
117 + GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT))\r
118\r
119//\r
120// Bit indexing inside the whole L4 table (0 - 65535)\r
121//\r
122#define GUARDED_HEAP_MAP_BIT_INDEX(Address) \\r
123 (RShiftU64 (Address, EFI_PAGE_SHIFT) \\r
124 & ((1 << (GUARDED_HEAP_MAP_ENTRY_SHIFT \\r
125 + GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT)) - 1))\r
126\r
127//\r
128// Memory address bit width tracked by L4 table: 10 + 6 + 12 = 28\r
129//\r
130#define GUARDED_HEAP_MAP_TABLE_SHIFT \\r
131 (GUARDED_HEAP_MAP_ENTRY_SHIFT + GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT \\r
132 + EFI_PAGE_SHIFT)\r
133\r
134//\r
135// Macro used to initialize the local array variable for map table traversing\r
136// {55, 46, 37, 28, 18}\r
137//\r
138#define GUARDED_HEAP_MAP_TABLE_DEPTH_SHIFTS \\r
139 { \\r
140 GUARDED_HEAP_MAP_TABLE_SHIFT + GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT * 3, \\r
141 GUARDED_HEAP_MAP_TABLE_SHIFT + GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT * 2, \\r
142 GUARDED_HEAP_MAP_TABLE_SHIFT + GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT, \\r
143 GUARDED_HEAP_MAP_TABLE_SHIFT, \\r
144 EFI_PAGE_SHIFT + GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT \\r
145 }\r
146\r
147//\r
148// Masks used to extract address range of each level of table\r
149// {0x1FF, 0x1FF, 0x1FF, 0x1FF, 0x3FF}\r
150//\r
151#define GUARDED_HEAP_MAP_TABLE_DEPTH_MASKS \\r
152 { \\r
153 (1 << GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT) - 1, \\r
154 (1 << GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT) - 1, \\r
155 (1 << GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT) - 1, \\r
156 (1 << GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT) - 1, \\r
157 (1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) - 1 \\r
158 }\r
159\r
160//\r
161// Memory type to guard (matching the related PCD definition)\r
162//\r
38d870fc
JW
163#define GUARD_HEAP_TYPE_PAGE BIT2\r
164#define GUARD_HEAP_TYPE_POOL BIT3\r
e63da9f0
JW
165\r
166//\r
167// Debug message level\r
168//\r
169#define HEAP_GUARD_DEBUG_LEVEL (DEBUG_POOL|DEBUG_PAGE)\r
170\r
171typedef struct {\r
172 UINT32 TailMark;\r
173 UINT32 HeadMark;\r
174 EFI_PHYSICAL_ADDRESS Address;\r
175 LIST_ENTRY Link;\r
176} HEAP_GUARD_NODE;\r
177\r
178/**\r
179 Set head Guard and tail Guard for the given memory range.\r
180\r
181 @param[in] Memory Base address of memory to set guard for.\r
182 @param[in] NumberOfPages Memory size in pages.\r
183\r
184 @return VOID.\r
185**/\r
186VOID\r
187SetGuardForMemory (\r
188 IN EFI_PHYSICAL_ADDRESS Memory,\r
189 IN UINTN NumberOfPages\r
190 );\r
191\r
192/**\r
193 Unset head Guard and tail Guard for the given memory range.\r
194\r
195 @param[in] Memory Base address of memory to unset guard for.\r
196 @param[in] NumberOfPages Memory size in pages.\r
197\r
198 @return VOID.\r
199**/\r
200VOID\r
201UnsetGuardForMemory (\r
202 IN EFI_PHYSICAL_ADDRESS Memory,\r
203 IN UINTN NumberOfPages\r
204 );\r
205\r
206/**\r
207 Adjust the base and number of pages to really allocate according to Guard.\r
208\r
209 @param[in,out] Memory Base address of free memory.\r
210 @param[in,out] NumberOfPages Size of memory to allocate.\r
211\r
212 @return VOID.\r
213**/\r
214VOID\r
215AdjustMemoryA (\r
216 IN OUT EFI_PHYSICAL_ADDRESS *Memory,\r
217 IN OUT UINTN *NumberOfPages\r
218 );\r
219\r
220/**\r
221 Adjust the start address and number of pages to free according to Guard.\r
222\r
223 The purpose of this function is to keep the shared Guard page with adjacent\r
224 memory block if it's still in guard, or free it if no more sharing. Another\r
225 is to reserve pages as Guard pages in partial page free situation.\r
226\r
227 @param[in,out] Memory Base address of memory to free.\r
228 @param[in,out] NumberOfPages Size of memory to free.\r
229\r
230 @return VOID.\r
231**/\r
232VOID\r
233AdjustMemoryF (\r
234 IN OUT EFI_PHYSICAL_ADDRESS *Memory,\r
235 IN OUT UINTN *NumberOfPages\r
236 );\r
237\r
238/**\r
239 Check to see if the pool at the given address should be guarded or not.\r
240\r
241 @param[in] MemoryType Pool type to check.\r
242\r
243\r
244 @return TRUE The given type of pool should be guarded.\r
245 @return FALSE The given type of pool should not be guarded.\r
246**/\r
247BOOLEAN\r
248IsPoolTypeToGuard (\r
249 IN EFI_MEMORY_TYPE MemoryType\r
250 );\r
251\r
252/**\r
253 Check to see if the page at the given address should be guarded or not.\r
254\r
255 @param[in] MemoryType Page type to check.\r
256 @param[in] AllocateType Allocation type to check.\r
257\r
258 @return TRUE The given type of page should be guarded.\r
259 @return FALSE The given type of page should not be guarded.\r
260**/\r
261BOOLEAN\r
262IsPageTypeToGuard (\r
263 IN EFI_MEMORY_TYPE MemoryType,\r
264 IN EFI_ALLOCATE_TYPE AllocateType\r
265 );\r
266\r
267/**\r
268 Check to see if the page at the given address is guarded or not.\r
269\r
270 @param[in] Address The address to check for.\r
271\r
272 @return TRUE The page at Address is guarded.\r
273 @return FALSE The page at Address is not guarded.\r
274**/\r
275BOOLEAN\r
276EFIAPI\r
277IsMemoryGuarded (\r
278 IN EFI_PHYSICAL_ADDRESS Address\r
279 );\r
280\r
281/**\r
282 Check to see if the page at the given address is a Guard page or not.\r
283\r
284 @param[in] Address The address to check for.\r
285\r
286 @return TRUE The page at Address is a Guard page.\r
287 @return FALSE The page at Address is not a Guard page.\r
288**/\r
289BOOLEAN\r
290EFIAPI\r
291IsGuardPage (\r
292 IN EFI_PHYSICAL_ADDRESS Address\r
293 );\r
294\r
295/**\r
296 Dump the guarded memory bit map.\r
297**/\r
298VOID\r
299EFIAPI\r
300DumpGuardedMemoryBitmap (\r
301 VOID\r
302 );\r
303\r
304/**\r
305 Adjust the pool head position to make sure the Guard page is adjavent to\r
306 pool tail or pool head.\r
307\r
308 @param[in] Memory Base address of memory allocated.\r
309 @param[in] NoPages Number of pages actually allocated.\r
310 @param[in] Size Size of memory requested.\r
311 (plus pool head/tail overhead)\r
312\r
313 @return Address of pool head.\r
314**/\r
315VOID *\r
316AdjustPoolHeadA (\r
317 IN EFI_PHYSICAL_ADDRESS Memory,\r
318 IN UINTN NoPages,\r
319 IN UINTN Size\r
320 );\r
321\r
322/**\r
323 Get the page base address according to pool head address.\r
324\r
325 @param[in] Memory Head address of pool to free.\r
326\r
327 @return Address of pool head.\r
328**/\r
329VOID *\r
330AdjustPoolHeadF (\r
331 IN EFI_PHYSICAL_ADDRESS Memory\r
332 );\r
333\r
334/**\r
335 Helper function of memory allocation with Guard pages.\r
336\r
337 @param FreePageList The free page node.\r
338 @param NumberOfPages Number of pages to be allocated.\r
339 @param MaxAddress Request to allocate memory below this address.\r
340 @param MemoryType Type of memory requested.\r
341\r
342 @return Memory address of allocated pages.\r
343**/\r
344UINTN\r
345InternalAllocMaxAddressWithGuard (\r
346 IN OUT LIST_ENTRY *FreePageList,\r
347 IN UINTN NumberOfPages,\r
348 IN UINTN MaxAddress,\r
349 IN EFI_MEMORY_TYPE MemoryType\r
350 );\r
351\r
352/**\r
353 Helper function of memory free with Guard pages.\r
354\r
355 @param[in] Memory Base address of memory being freed.\r
356 @param[in] NumberOfPages The number of pages to free.\r
357 @param[in] AddRegion If this memory is new added region.\r
358\r
359 @retval EFI_NOT_FOUND Could not find the entry that covers the range.\r
360 @retval EFI_INVALID_PARAMETER Address not aligned, Address is zero or\r
361 NumberOfPages is zero.\r
362 @return EFI_SUCCESS Pages successfully freed.\r
363**/\r
364EFI_STATUS\r
365SmmInternalFreePagesExWithGuard (\r
366 IN EFI_PHYSICAL_ADDRESS Memory,\r
367 IN UINTN NumberOfPages,\r
368 IN BOOLEAN AddRegion\r
369 );\r
370\r
371/**\r
372 Check to see if the heap guard is enabled for page and/or pool allocation.\r
373\r
374 @return TRUE/FALSE.\r
375**/\r
376BOOLEAN\r
377IsHeapGuardEnabled (\r
378 VOID\r
379 );\r
380\r
381/**\r
382 Debug function used to verify if the Guard page is well set or not.\r
383\r
384 @param[in] BaseAddress Address of memory to check.\r
385 @param[in] NumberOfPages Size of memory in pages.\r
386\r
387 @return TRUE The head Guard and tail Guard are both well set.\r
388 @return FALSE The head Guard and/or tail Guard are not well set.\r
389**/\r
390BOOLEAN\r
391VerifyMemoryGuard (\r
392 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
393 IN UINTN NumberOfPages\r
394 );\r
395\r
396extern BOOLEAN mOnGuarding;\r
397\r
398#endif\r