2 Data structure and functions to allocate and free memory space.
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
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.
18 #include "PiSmmCore.h"
21 // Following macros are used to define and access the guarded memory bitmap
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.
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.
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
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.
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
52 // 1-page (L3) + 2-page (L4)
54 // memory (3 pages) to track the memory allocation works. In this case,
55 // there's no need to setup L0-L2 tables.
59 // Each entry occupies 8B/64b. 1-page can hold 512 entries, which spans 9
60 // bits in address. (512 = 1 << 9)
62 #define BYTE_LENGTH_SHIFT 3 // (8 = 1 << 3)
64 #define GUARDED_HEAP_MAP_TABLE_ENTRY_SHIFT \
65 (EFI_PAGE_SHIFT - BYTE_LENGTH_SHIFT)
67 #define GUARDED_HEAP_MAP_TABLE_DEPTH 5
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)
72 #define GUARDED_HEAP_MAP_ENTRY_BITS \
73 (1 << GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT)
75 #define GUARDED_HEAP_MAP_ENTRY_BYTES \
76 (GUARDED_HEAP_MAP_ENTRY_BITS / 8)
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 \
85 // L4 table address mask: (1 << 10 - 1) = 0x3FF
86 #define GUARDED_HEAP_MAP_ENTRY_MASK \
87 ((1 << GUARDED_HEAP_MAP_ENTRY_SHIFT) - 1)
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)
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)
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)
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)
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))
113 // Total bits (pages) tracked by one L4 table (65536-bit)
115 #define GUARDED_HEAP_MAP_BITS \
116 (1 << (GUARDED_HEAP_MAP_ENTRY_SHIFT \
117 + GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT))
120 // Bit indexing inside the whole L4 table (0 - 65535)
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))
128 // Memory address bit width tracked by L4 table: 10 + 6 + 12 = 28
130 #define GUARDED_HEAP_MAP_TABLE_SHIFT \
131 (GUARDED_HEAP_MAP_ENTRY_SHIFT + GUARDED_HEAP_MAP_ENTRY_BIT_SHIFT \
135 // Macro used to initialize the local array variable for map table traversing
136 // {55, 46, 37, 28, 18}
138 #define GUARDED_HEAP_MAP_TABLE_DEPTH_SHIFTS \
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 \
148 // Masks used to extract address range of each level of table
149 // {0x1FF, 0x1FF, 0x1FF, 0x1FF, 0x3FF}
151 #define GUARDED_HEAP_MAP_TABLE_DEPTH_MASKS \
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 \
161 // Memory type to guard (matching the related PCD definition)
163 #define GUARD_HEAP_TYPE_PAGE BIT2
164 #define GUARD_HEAP_TYPE_POOL BIT3
167 // Debug message level
169 #define HEAP_GUARD_DEBUG_LEVEL (DEBUG_POOL|DEBUG_PAGE)
174 EFI_PHYSICAL_ADDRESS Address
;
179 Set head Guard and tail Guard for the given memory range.
181 @param[in] Memory Base address of memory to set guard for.
182 @param[in] NumberOfPages Memory size in pages.
188 IN EFI_PHYSICAL_ADDRESS Memory
,
189 IN UINTN NumberOfPages
193 Unset head Guard and tail Guard for the given memory range.
195 @param[in] Memory Base address of memory to unset guard for.
196 @param[in] NumberOfPages Memory size in pages.
201 UnsetGuardForMemory (
202 IN EFI_PHYSICAL_ADDRESS Memory
,
203 IN UINTN NumberOfPages
207 Adjust the base and number of pages to really allocate according to Guard.
209 @param[in,out] Memory Base address of free memory.
210 @param[in,out] NumberOfPages Size of memory to allocate.
216 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
,
217 IN OUT UINTN
*NumberOfPages
221 Adjust the start address and number of pages to free according to Guard.
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.
227 @param[in,out] Memory Base address of memory to free.
228 @param[in,out] NumberOfPages Size of memory to free.
234 IN OUT EFI_PHYSICAL_ADDRESS
*Memory
,
235 IN OUT UINTN
*NumberOfPages
239 Check to see if the pool at the given address should be guarded or not.
241 @param[in] MemoryType Pool type to check.
244 @return TRUE The given type of pool should be guarded.
245 @return FALSE The given type of pool should not be guarded.
249 IN EFI_MEMORY_TYPE MemoryType
253 Check to see if the page at the given address should be guarded or not.
255 @param[in] MemoryType Page type to check.
256 @param[in] AllocateType Allocation type to check.
258 @return TRUE The given type of page should be guarded.
259 @return FALSE The given type of page should not be guarded.
263 IN EFI_MEMORY_TYPE MemoryType
,
264 IN EFI_ALLOCATE_TYPE AllocateType
268 Check to see if the page at the given address is guarded or not.
270 @param[in] Address The address to check for.
272 @return TRUE The page at Address is guarded.
273 @return FALSE The page at Address is not guarded.
278 IN EFI_PHYSICAL_ADDRESS Address
282 Check to see if the page at the given address is a Guard page or not.
284 @param[in] Address The address to check for.
286 @return TRUE The page at Address is a Guard page.
287 @return FALSE The page at Address is not a Guard page.
292 IN EFI_PHYSICAL_ADDRESS Address
296 Dump the guarded memory bit map.
300 DumpGuardedMemoryBitmap (
305 Adjust the pool head position to make sure the Guard page is adjavent to
306 pool tail or pool head.
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)
313 @return Address of pool head.
317 IN EFI_PHYSICAL_ADDRESS Memory
,
323 Get the page base address according to pool head address.
325 @param[in] Memory Head address of pool to free.
327 @return Address of pool head.
331 IN EFI_PHYSICAL_ADDRESS Memory
335 Helper function of memory allocation with Guard pages.
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.
342 @return Memory address of allocated pages.
345 InternalAllocMaxAddressWithGuard (
346 IN OUT LIST_ENTRY
*FreePageList
,
347 IN UINTN NumberOfPages
,
349 IN EFI_MEMORY_TYPE MemoryType
353 Helper function of memory free with Guard pages.
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.
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.
365 SmmInternalFreePagesExWithGuard (
366 IN EFI_PHYSICAL_ADDRESS Memory
,
367 IN UINTN NumberOfPages
,
372 Check to see if the heap guard is enabled for page and/or pool allocation.
382 Debug function used to verify if the Guard page is well set or not.
384 @param[in] BaseAddress Address of memory to check.
385 @param[in] NumberOfPages Size of memory in pages.
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.
392 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
393 IN UINTN NumberOfPages
396 extern BOOLEAN mOnGuarding
;