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