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