]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/Mem/Page.c
MdeModulePkg/DxeCore: Fix issues in Heap Guard
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Page.c
CommitLineData
504214c4 1/** @file\r
504214c4
LG
2 UEFI Memory page management functions.\r
3\r
9a701955 4Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 5This program and the accompanying materials\r
23c98c94 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
28a00297 12\r
504214c4 13**/\r
28a00297 14\r
9c4ac31c 15#include "DxeMain.h"\r
ec90508b 16#include "Imem.h"\r
e63da9f0 17#include "HeapGuard.h"\r
28a00297 18\r
28a00297 19//\r
d45fd260 20// Entry for tracking the memory regions for each memory type to coalesce similar memory types\r
28a00297 21//\r
22typedef struct {\r
23 EFI_PHYSICAL_ADDRESS BaseAddress;\r
24 EFI_PHYSICAL_ADDRESS MaximumAddress;\r
25 UINT64 CurrentNumberOfPages;\r
b74350e9 26 UINT64 NumberOfPages;\r
28a00297 27 UINTN InformationIndex;\r
b74350e9 28 BOOLEAN Special;\r
29 BOOLEAN Runtime;\r
d613c2a8 30} EFI_MEMORY_TYPE_STATISTICS;\r
28a00297 31\r
32//\r
33// MemoryMap - The current memory map\r
34//\r
35UINTN mMemoryMapKey = 0;\r
36\r
28a00297 37#define MAX_MAP_DEPTH 6\r
dc8d93ca 38\r
39///\r
40/// mMapDepth - depth of new descriptor stack\r
41///\r
28a00297 42UINTN mMapDepth = 0;\r
dc8d93ca 43///\r
44/// mMapStack - space to use as temp storage to build new map descriptors\r
45///\r
28a00297 46MEMORY_MAP mMapStack[MAX_MAP_DEPTH];\r
47UINTN mFreeMapStack = 0;\r
dc8d93ca 48///\r
49/// This list maintain the free memory map list\r
50///\r
e94a9ff7 51LIST_ENTRY mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList);\r
52BOOLEAN mMemoryTypeInformationInitialized = FALSE;\r
28a00297 53\r
d613c2a8 54EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {\r
f3f2e05d 55 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiReservedMemoryType\r
56 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderCode\r
57 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderData\r
58 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesCode\r
59 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesData\r
60 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesCode\r
61 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesData\r
62 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiConventionalMemory\r
63 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiUnusableMemory\r
64 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIReclaimMemory\r
65 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIMemoryNVS\r
66 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIO\r
67 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIOPortSpace\r
68 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiPalCode\r
a671a012 69 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiPersistentMemory\r
f3f2e05d 70 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE } // EfiMaxMemoryType\r
28a00297 71};\r
72\r
f3f2e05d 73EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress = MAX_ADDRESS;\r
2345e7d4 74EFI_PHYSICAL_ADDRESS mDefaultBaseAddress = MAX_ADDRESS;\r
28a00297 75\r
76EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {\r
77 { EfiReservedMemoryType, 0 },\r
78 { EfiLoaderCode, 0 },\r
79 { EfiLoaderData, 0 },\r
80 { EfiBootServicesCode, 0 },\r
81 { EfiBootServicesData, 0 },\r
82 { EfiRuntimeServicesCode, 0 },\r
83 { EfiRuntimeServicesData, 0 },\r
84 { EfiConventionalMemory, 0 },\r
85 { EfiUnusableMemory, 0 },\r
86 { EfiACPIReclaimMemory, 0 },\r
87 { EfiACPIMemoryNVS, 0 },\r
88 { EfiMemoryMappedIO, 0 },\r
89 { EfiMemoryMappedIOPortSpace, 0 },\r
90 { EfiPalCode, 0 },\r
a671a012 91 { EfiPersistentMemory, 0 },\r
28a00297 92 { EfiMaxMemoryType, 0 }\r
93};\r
54ea99a7 94//\r
95// Only used when load module at fixed address feature is enabled. True means the memory is alreay successfully allocated\r
96// and ready to load the module in to specified address.or else, the memory is not ready and module will be loaded at a \r
97// address assigned by DXE core.\r
98//\r
99GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN gLoadFixedAddressCodeMemoryReady = FALSE;\r
d9177625 100\r
162ed594 101/**\r
d9177625 102 Enter critical section by gaining lock on gMemoryLock.\r
162ed594 103\r
104**/\r
23c98c94 105VOID\r
d9177625 106CoreAcquireMemoryLock (\r
107 VOID\r
108 )\r
109{\r
110 CoreAcquireLock (&gMemoryLock);\r
111}\r
112\r
113\r
114\r
115/**\r
116 Exit critical section by releasing lock on gMemoryLock.\r
117\r
118**/\r
119VOID\r
120CoreReleaseMemoryLock (\r
28a00297 121 VOID\r
d9177625 122 )\r
123{\r
124 CoreReleaseLock (&gMemoryLock);\r
125}\r
126\r
127\r
128\r
129\r
130/**\r
131 Internal function. Removes a descriptor entry.\r
132\r
133 @param Entry The entry to remove\r
134\r
135**/\r
136VOID\r
137RemoveMemoryMapEntry (\r
138 IN OUT MEMORY_MAP *Entry\r
139 )\r
140{\r
141 RemoveEntryList (&Entry->Link);\r
142 Entry->Link.ForwardLink = NULL;\r
143\r
144 if (Entry->FromPages) {\r
145 //\r
146 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList\r
147 //\r
148 InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
149 }\r
150}\r
162ed594 151\r
152/**\r
153 Internal function. Adds a ranges to the memory map.\r
154 The range must not already exist in the map.\r
155\r
022c6d45 156 @param Type The type of memory range to add\r
157 @param Start The starting address in the memory range Must be\r
158 paged aligned\r
159 @param End The last address in the range Must be the last\r
160 byte of a page\r
161 @param Attribute The attributes of the memory range to add\r
28a00297 162\r
162ed594 163**/\r
28a00297 164VOID\r
165CoreAddRange (\r
166 IN EFI_MEMORY_TYPE Type,\r
167 IN EFI_PHYSICAL_ADDRESS Start,\r
168 IN EFI_PHYSICAL_ADDRESS End,\r
169 IN UINT64 Attribute\r
d9177625 170 )\r
171{\r
172 LIST_ENTRY *Link;\r
173 MEMORY_MAP *Entry;\r
28a00297 174\r
d9177625 175 ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
176 ASSERT (End > Start) ;\r
162ed594 177\r
d9177625 178 ASSERT_LOCKED (&gMemoryLock);\r
28a00297 179\r
d9177625 180 DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type));\r
d436d5ca 181 \r
182 //\r
183 // If memory of type EfiConventionalMemory is being added that includes the page \r
184 // starting at address 0, then zero the page starting at address 0. This has \r
185 // two benifits. It helps find NULL pointer bugs and it also maximizes \r
186 // compatibility with operating systems that may evaluate memory in this page \r
187 // for legacy data structures. If memory of any other type is added starting \r
188 // at address 0, then do not zero the page at address 0 because the page is being \r
189 // used for other purposes.\r
190 // \r
191 if (Type == EfiConventionalMemory && Start == 0 && (End >= EFI_PAGE_SIZE - 1)) {\r
a7181d95
JW
192 if ((PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT0) == 0) {\r
193 SetMem ((VOID *)(UINTN)Start, EFI_PAGE_SIZE, 0);\r
194 }\r
d436d5ca 195 }\r
196 \r
d9177625 197 //\r
198 // Memory map being altered so updated key\r
199 //\r
200 mMemoryMapKey += 1;\r
162ed594 201\r
d9177625 202 //\r
203 // UEFI 2.0 added an event group for notificaiton on memory map changes.\r
204 // So we need to signal this Event Group every time the memory map changes.\r
205 // If we are in EFI 1.10 compatability mode no event groups will be\r
206 // found and nothing will happen we we call this function. These events\r
207 // will get signaled but since a lock is held around the call to this\r
6393d9c8 208 // function the notificaiton events will only be called after this function\r
d9177625 209 // returns and the lock is released.\r
210 //\r
211 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid);\r
162ed594 212\r
d9177625 213 //\r
214 // Look for adjoining memory descriptor\r
215 //\r
28a00297 216\r
d9177625 217 // Two memory descriptors can only be merged if they have the same Type\r
218 // and the same Attribute\r
219 //\r
162ed594 220\r
d9177625 221 Link = gMemoryMap.ForwardLink;\r
222 while (Link != &gMemoryMap) {\r
223 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
224 Link = Link->ForwardLink;\r
162ed594 225\r
d9177625 226 if (Entry->Type != Type) {\r
227 continue;\r
228 }\r
229\r
230 if (Entry->Attribute != Attribute) {\r
231 continue;\r
232 }\r
233\r
234 if (Entry->End + 1 == Start) {\r
235\r
236 Start = Entry->Start;\r
237 RemoveMemoryMapEntry (Entry);\r
238\r
239 } else if (Entry->Start == End + 1) {\r
240\r
241 End = Entry->End;\r
242 RemoveMemoryMapEntry (Entry);\r
243 }\r
244 }\r
245\r
246 //\r
247 // Add descriptor\r
248 //\r
249\r
250 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
251 mMapStack[mMapDepth].FromPages = FALSE;\r
252 mMapStack[mMapDepth].Type = Type;\r
253 mMapStack[mMapDepth].Start = Start;\r
254 mMapStack[mMapDepth].End = End;\r
255 mMapStack[mMapDepth].VirtualStart = 0;\r
256 mMapStack[mMapDepth].Attribute = Attribute;\r
257 InsertTailList (&gMemoryMap, &mMapStack[mMapDepth].Link);\r
258\r
259 mMapDepth += 1;\r
260 ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
261\r
262 return ;\r
263}\r
022c6d45 264\r
162ed594 265/**\r
266 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.\r
267 If the list is emtry, then allocate a new page to refuel the list.\r
268 Please Note this algorithm to allocate the memory map descriptor has a property\r
269 that the memory allocated for memory entries always grows, and will never really be freed\r
270 For example, if the current boot uses 2000 memory map entries at the maximum point, but\r
271 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950\r
272 memory map entries is still allocated from EfiBootServicesMemory.\r
273\r
274\r
275 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList\r
276\r
277**/\r
28a00297 278MEMORY_MAP *\r
279AllocateMemoryMapEntry (\r
280 VOID\r
d9177625 281 )\r
282{\r
283 MEMORY_MAP* FreeDescriptorEntries;\r
284 MEMORY_MAP* Entry;\r
285 UINTN Index;\r
286\r
287 if (IsListEmpty (&mFreeMemoryMapEntryList)) {\r
288 //\r
289 // The list is empty, to allocate one page to refuel the list\r
290 //\r
e63da9f0
JW
291 FreeDescriptorEntries = CoreAllocatePoolPages (\r
292 EfiBootServicesData,\r
d4731a98 293 EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION_GRANULARITY),\r
e63da9f0
JW
294 DEFAULT_PAGE_ALLOCATION_GRANULARITY,\r
295 FALSE\r
296 );\r
d4731a98 297 if (FreeDescriptorEntries != NULL) {\r
d9177625 298 //\r
299 // Enque the free memmory map entries into the list\r
300 //\r
d4731a98 301 for (Index = 0; Index < DEFAULT_PAGE_ALLOCATION_GRANULARITY / sizeof(MEMORY_MAP); Index++) {\r
d9177625 302 FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;\r
303 InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);\r
304 }\r
305 } else {\r
306 return NULL;\r
307 }\r
308 }\r
309 //\r
310 // dequeue the first descriptor from the list\r
311 //\r
312 Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
313 RemoveEntryList (&Entry->Link);\r
314\r
315 return Entry;\r
316}\r
022c6d45 317\r
162ed594 318\r
319/**\r
d9177625 320 Internal function. Moves any memory descriptors that are on the\r
321 temporary descriptor stack to heap.\r
162ed594 322\r
323**/\r
28a00297 324VOID\r
d9177625 325CoreFreeMemoryMapStack (\r
28a00297 326 VOID\r
327 )\r
28a00297 328{\r
d9177625 329 MEMORY_MAP *Entry;\r
330 MEMORY_MAP *Entry2;\r
331 LIST_ENTRY *Link2;\r
28a00297 332\r
d9177625 333 ASSERT_LOCKED (&gMemoryLock);\r
28a00297 334\r
d9177625 335 //\r
336 // If already freeing the map stack, then return\r
337 //\r
338 if (mFreeMapStack != 0) {\r
339 return ;\r
340 }\r
162ed594 341\r
d9177625 342 //\r
343 // Move the temporary memory descriptor stack into pool\r
344 //\r
345 mFreeMapStack += 1;\r
162ed594 346\r
d9177625 347 while (mMapDepth != 0) {\r
348 //\r
349 // Deque an memory map entry from mFreeMemoryMapEntryList\r
350 //\r
351 Entry = AllocateMemoryMapEntry ();\r
352\r
353 ASSERT (Entry);\r
354\r
355 //\r
356 // Update to proper entry\r
357 //\r
358 mMapDepth -= 1;\r
359\r
360 if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {\r
361\r
362 //\r
363 // Move this entry to general memory\r
364 //\r
365 RemoveEntryList (&mMapStack[mMapDepth].Link);\r
366 mMapStack[mMapDepth].Link.ForwardLink = NULL;\r
367\r
368 CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));\r
369 Entry->FromPages = TRUE;\r
370\r
371 //\r
372 // Find insertion location\r
373 //\r
374 for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) {\r
375 Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
376 if (Entry2->FromPages && Entry2->Start > Entry->Start) {\r
377 break;\r
378 }\r
379 }\r
380\r
381 InsertTailList (Link2, &Entry->Link);\r
382\r
383 } else {\r
384 //\r
385 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,\r
386 // so here no need to move it to memory.\r
387 //\r
388 InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
389 }\r
390 }\r
28a00297 391\r
d9177625 392 mFreeMapStack -= 1;\r
393}\r
162ed594 394\r
395/**\r
396 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.\r
397\r
398**/\r
2345e7d4 399BOOLEAN\r
28a00297 400PromoteMemoryResource (\r
401 VOID\r
402 )\r
28a00297 403{\r
2345e7d4 404 LIST_ENTRY *Link;\r
405 EFI_GCD_MAP_ENTRY *Entry;\r
406 BOOLEAN Promoted;\r
28a00297 407\r
d45fd260 408 DEBUG ((DEBUG_PAGE, "Promote the memory resource\n"));\r
022c6d45 409\r
28a00297 410 CoreAcquireGcdMemoryLock ();\r
022c6d45 411\r
2345e7d4 412 Promoted = FALSE;\r
28a00297 413 Link = mGcdMemorySpaceMap.ForwardLink;\r
414 while (Link != &mGcdMemorySpaceMap) {\r
415\r
416 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
417\r
418 if (Entry->GcdMemoryType == EfiGcdMemoryTypeReserved &&\r
f3f2e05d 419 Entry->EndAddress < MAX_ADDRESS &&\r
28a00297 420 (Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r
421 (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) {\r
422 //\r
423 // Update the GCD map\r
424 //\r
74705ca5
SZ
425 if ((Entry->Capabilities & EFI_MEMORY_MORE_RELIABLE) == EFI_MEMORY_MORE_RELIABLE) {\r
426 Entry->GcdMemoryType = EfiGcdMemoryTypeMoreReliable;\r
427 } else {\r
428 Entry->GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r
429 }\r
28a00297 430 Entry->Capabilities |= EFI_MEMORY_TESTED;\r
431 Entry->ImageHandle = gDxeCoreImageHandle;\r
432 Entry->DeviceHandle = NULL;\r
433\r
434 //\r
435 // Add to allocable system memory resource\r
022c6d45 436 //\r
28a00297 437\r
438 CoreAddRange (\r
022c6d45 439 EfiConventionalMemory,\r
440 Entry->BaseAddress,\r
441 Entry->EndAddress,\r
28a00297 442 Entry->Capabilities & ~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r
443 );\r
444 CoreFreeMemoryMapStack ();\r
022c6d45 445\r
2345e7d4 446 Promoted = TRUE;\r
28a00297 447 }\r
448\r
449 Link = Link->ForwardLink;\r
450 }\r
022c6d45 451\r
28a00297 452 CoreReleaseGcdMemoryLock ();\r
022c6d45 453\r
2345e7d4 454 return Promoted;\r
28a00297 455}\r
54ea99a7 456/**\r
457 This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD \r
458 PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the \r
459 size of boot time and runtime code.\r
28a00297 460\r
54ea99a7 461**/\r
462VOID\r
463CoreLoadingFixedAddressHook (\r
464 VOID\r
465 )\r
466{\r
467 UINT32 RuntimeCodePageNumber;\r
468 UINT32 BootTimeCodePageNumber;\r
469 EFI_PHYSICAL_ADDRESS RuntimeCodeBase;\r
470 EFI_PHYSICAL_ADDRESS BootTimeCodeBase;\r
471 EFI_STATUS Status;\r
472\r
473 //\r
474 // Make sure these 2 areas are not initialzied.\r
475 //\r
476 if (!gLoadFixedAddressCodeMemoryReady) { \r
477 RuntimeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
478 BootTimeCodePageNumber= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
479 RuntimeCodeBase = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - EFI_PAGES_TO_SIZE (RuntimeCodePageNumber));\r
480 BootTimeCodeBase = (EFI_PHYSICAL_ADDRESS)(RuntimeCodeBase - EFI_PAGES_TO_SIZE (BootTimeCodePageNumber));\r
481 //\r
482 // Try to allocate runtime memory.\r
483 //\r
484 Status = CoreAllocatePages (\r
485 AllocateAddress,\r
486 EfiRuntimeServicesCode,\r
487 RuntimeCodePageNumber,\r
488 &RuntimeCodeBase\r
489 );\r
490 if (EFI_ERROR(Status)) {\r
491 //\r
492 // Runtime memory allocation failed \r
493 //\r
494 return;\r
495 }\r
496 //\r
497 // Try to allocate boot memory.\r
498 //\r
499 Status = CoreAllocatePages (\r
500 AllocateAddress,\r
501 EfiBootServicesCode,\r
502 BootTimeCodePageNumber,\r
503 &BootTimeCodeBase\r
504 );\r
505 if (EFI_ERROR(Status)) {\r
506 //\r
507 // boot memory allocation failed. Free Runtime code range and will try the allocation again when \r
508 // new memory range is installed.\r
509 //\r
510 CoreFreePages (\r
511 RuntimeCodeBase,\r
512 RuntimeCodePageNumber\r
513 );\r
514 return;\r
515 }\r
516 gLoadFixedAddressCodeMemoryReady = TRUE;\r
517 } \r
518 return;\r
519} \r
28a00297 520\r
162ed594 521/**\r
28a00297 522 Called to initialize the memory map and add descriptors to\r
523 the current descriptor list.\r
28a00297 524 The first descriptor that is added must be general usable\r
525 memory as the addition allocates heap.\r
526\r
022c6d45 527 @param Type The type of memory to add\r
528 @param Start The starting address in the memory range Must be\r
529 page aligned\r
530 @param NumberOfPages The number of pages in the range\r
531 @param Attribute Attributes of the memory to add\r
28a00297 532\r
162ed594 533 @return None. The range is added to the memory map\r
28a00297 534\r
162ed594 535**/\r
536VOID\r
537CoreAddMemoryDescriptor (\r
538 IN EFI_MEMORY_TYPE Type,\r
539 IN EFI_PHYSICAL_ADDRESS Start,\r
540 IN UINT64 NumberOfPages,\r
541 IN UINT64 Attribute\r
542 )\r
28a00297 543{\r
544 EFI_PHYSICAL_ADDRESS End;\r
545 EFI_STATUS Status;\r
546 UINTN Index;\r
547 UINTN FreeIndex;\r
54ea99a7 548 \r
28a00297 549 if ((Start & EFI_PAGE_MASK) != 0) {\r
550 return;\r
551 }\r
552\r
8ee25f48 553 if (Type >= EfiMaxMemoryType && Type < MEMORY_TYPE_OEM_RESERVED_MIN) {\r
28a00297 554 return;\r
555 }\r
28a00297 556 CoreAcquireMemoryLock ();\r
557 End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
558 CoreAddRange (Type, Start, End, Attribute);\r
559 CoreFreeMemoryMapStack ();\r
560 CoreReleaseMemoryLock ();\r
561\r
7eb927db 562 ApplyMemoryProtectionPolicy (EfiMaxMemoryType, Type, Start,\r
4879e130 563 LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT));\r
7eb927db 564\r
54ea99a7 565 //\r
566 // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type\r
567 //\r
852081fc 568 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
54ea99a7 569 CoreLoadingFixedAddressHook();\r
570 }\r
571 \r
28a00297 572 //\r
573 // Check to see if the statistics for the different memory types have already been established\r
574 //\r
575 if (mMemoryTypeInformationInitialized) {\r
576 return;\r
577 }\r
578\r
54ea99a7 579 \r
28a00297 580 //\r
581 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array\r
582 //\r
583 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
584 //\r
585 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
586 //\r
587 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
3d78c020 588 if ((UINT32)Type > EfiMaxMemoryType) {\r
28a00297 589 continue;\r
590 }\r
28a00297 591 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
592 //\r
593 // Allocate pages for the current memory type from the top of available memory\r
594 //\r
595 Status = CoreAllocatePages (\r
596 AllocateAnyPages,\r
597 Type,\r
598 gMemoryTypeInformation[Index].NumberOfPages,\r
599 &mMemoryTypeStatistics[Type].BaseAddress\r
600 );\r
601 if (EFI_ERROR (Status)) {\r
602 //\r
022c6d45 603 // If an error occurs allocating the pages for the current memory type, then\r
28a00297 604 // free all the pages allocates for the previous memory types and return. This\r
605 // operation with be retied when/if more memory is added to the system\r
606 //\r
607 for (FreeIndex = 0; FreeIndex < Index; FreeIndex++) {\r
608 //\r
609 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
610 //\r
611 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type);\r
3d78c020 612 if ((UINT32)Type > EfiMaxMemoryType) {\r
28a00297 613 continue;\r
614 }\r
615\r
616 if (gMemoryTypeInformation[FreeIndex].NumberOfPages != 0) {\r
617 CoreFreePages (\r
022c6d45 618 mMemoryTypeStatistics[Type].BaseAddress,\r
28a00297 619 gMemoryTypeInformation[FreeIndex].NumberOfPages\r
620 );\r
621 mMemoryTypeStatistics[Type].BaseAddress = 0;\r
f3f2e05d 622 mMemoryTypeStatistics[Type].MaximumAddress = MAX_ADDRESS;\r
28a00297 623 }\r
624 }\r
625 return;\r
626 }\r
627\r
628 //\r
629 // Compute the address at the top of the current statistics\r
630 //\r
022c6d45 631 mMemoryTypeStatistics[Type].MaximumAddress =\r
632 mMemoryTypeStatistics[Type].BaseAddress +\r
28a00297 633 LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
634\r
635 //\r
022c6d45 636 // If the current base address is the lowest address so far, then update the default\r
28a00297 637 // maximum address\r
638 //\r
639 if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) {\r
640 mDefaultMaximumAddress = mMemoryTypeStatistics[Type].BaseAddress - 1;\r
641 }\r
642 }\r
643 }\r
644\r
645 //\r
646 // There was enough system memory for all the the memory types were allocated. So,\r
647 // those memory areas can be freed for future allocations, and all future memory\r
648 // allocations can occur within their respective bins\r
649 //\r
650 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
651 //\r
652 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
653 //\r
654 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
3d78c020 655 if ((UINT32)Type > EfiMaxMemoryType) {\r
28a00297 656 continue;\r
657 }\r
28a00297 658 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
659 CoreFreePages (\r
022c6d45 660 mMemoryTypeStatistics[Type].BaseAddress,\r
28a00297 661 gMemoryTypeInformation[Index].NumberOfPages\r
662 );\r
b74350e9 663 mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages;\r
28a00297 664 gMemoryTypeInformation[Index].NumberOfPages = 0;\r
665 }\r
666 }\r
667\r
668 //\r
669 // If the number of pages reserved for a memory type is 0, then all allocations for that type\r
670 // should be in the default range.\r
671 //\r
672 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
673 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
674 if (Type == (EFI_MEMORY_TYPE)gMemoryTypeInformation[Index].Type) {\r
675 mMemoryTypeStatistics[Type].InformationIndex = Index;\r
676 }\r
677 }\r
678 mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0;\r
f3f2e05d 679 if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ADDRESS) {\r
28a00297 680 mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress;\r
681 }\r
682 }\r
683\r
684 mMemoryTypeInformationInitialized = TRUE;\r
685}\r
686\r
687\r
162ed594 688/**\r
771ee501
EC
689 Internal function. Converts a memory range to the specified type or attributes.\r
690 The range must exist in the memory map. Either ChangingType or\r
691 ChangingAttributes must be set, but not both.\r
162ed594 692\r
022c6d45 693 @param Start The first address of the range Must be page\r
694 aligned\r
695 @param NumberOfPages The number of pages to convert\r
771ee501 696 @param ChangingType Boolean indicating that type value should be changed\r
022c6d45 697 @param NewType The new type for the memory range\r
771ee501
EC
698 @param ChangingAttributes Boolean indicating that attributes value should be changed\r
699 @param NewAttributes The new attributes for the memory range\r
162ed594 700\r
022c6d45 701 @retval EFI_INVALID_PARAMETER Invalid parameter\r
702 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r
703 range or convertion not allowed.\r
704 @retval EFI_SUCCESS Successfully converts the memory range to the\r
162ed594 705 specified type.\r
706\r
707**/\r
28a00297 708EFI_STATUS\r
771ee501 709CoreConvertPagesEx (\r
28a00297 710 IN UINT64 Start,\r
711 IN UINT64 NumberOfPages,\r
771ee501
EC
712 IN BOOLEAN ChangingType,\r
713 IN EFI_MEMORY_TYPE NewType,\r
714 IN BOOLEAN ChangingAttributes,\r
715 IN UINT64 NewAttributes\r
28a00297 716 )\r
28a00297 717{\r
718\r
719 UINT64 NumberOfBytes;\r
720 UINT64 End;\r
721 UINT64 RangeEnd;\r
722 UINT64 Attribute;\r
771ee501 723 EFI_MEMORY_TYPE MemType;\r
28a00297 724 LIST_ENTRY *Link;\r
725 MEMORY_MAP *Entry;\r
726\r
727 Entry = NULL;\r
728 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
729 End = Start + NumberOfBytes - 1;\r
730\r
731 ASSERT (NumberOfPages);\r
732 ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
733 ASSERT (End > Start) ;\r
734 ASSERT_LOCKED (&gMemoryLock);\r
771ee501 735 ASSERT ( (ChangingType == FALSE) || (ChangingAttributes == FALSE) );\r
28a00297 736\r
f0459afe 737 if (NumberOfPages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start >= End)) {\r
28a00297 738 return EFI_INVALID_PARAMETER;\r
739 }\r
740\r
741 //\r
742 // Convert the entire range\r
743 //\r
744\r
745 while (Start < End) {\r
746\r
747 //\r
748 // Find the entry that the covers the range\r
749 //\r
750 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
751 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
752\r
753 if (Entry->Start <= Start && Entry->End > Start) {\r
754 break;\r
755 }\r
756 }\r
757\r
758 if (Link == &gMemoryMap) {\r
162ed594 759 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: failed to find range %lx - %lx\n", Start, End));\r
28a00297 760 return EFI_NOT_FOUND;\r
761 }\r
762\r
3f2ae009
AB
763 //\r
764 // If we are converting the type of the range from EfiConventionalMemory to\r
765 // another type, we have to ensure that the entire range is covered by a\r
766 // single entry.\r
767 //\r
768 if (ChangingType && (NewType != EfiConventionalMemory)) {\r
769 if (Entry->End < End) {\r
770 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: range %lx - %lx covers multiple entries\n", Start, End));\r
771 return EFI_NOT_FOUND;\r
772 }\r
773 }\r
28a00297 774 //\r
775 // Convert range to the end, or to the end of the descriptor\r
776 // if that's all we've got\r
777 //\r
778 RangeEnd = End;\r
525aded9 779\r
780 ASSERT (Entry != NULL);\r
28a00297 781 if (Entry->End < End) {\r
782 RangeEnd = Entry->End;\r
783 }\r
784\r
771ee501
EC
785 if (ChangingType) {\r
786 DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to type %d\n", Start, RangeEnd, NewType));\r
787 }\r
788 if (ChangingAttributes) {\r
789 DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to attr %lx\n", Start, RangeEnd, NewAttributes));\r
022c6d45 790 }\r
28a00297 791\r
771ee501
EC
792 if (ChangingType) {\r
793 //\r
794 // Debug code - verify conversion is allowed\r
795 //\r
796 if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) {\r
9a701955
SZ
797 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types, "));\r
798 if (Entry->Type == EfiConventionalMemory) {\r
799 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "the pages to free have been freed\n"));\r
800 } else {\r
801 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "the pages to allocate have been allocated\n"));\r
802 }\r
771ee501
EC
803 return EFI_NOT_FOUND;\r
804 }\r
805\r
806 //\r
807 // Update counters for the number of pages allocated to each memory type\r
808 //\r
809 if ((UINT32)Entry->Type < EfiMaxMemoryType) {\r
810 if ((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) ||\r
811 (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {\r
812 if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {\r
813 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;\r
814 } else {\r
815 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;\r
816 }\r
28a00297 817 }\r
818 }\r
28a00297 819\r
771ee501
EC
820 if ((UINT32)NewType < EfiMaxMemoryType) {\r
821 if ((Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) ||\r
822 (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {\r
823 mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;\r
824 if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {\r
825 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;\r
826 }\r
28a00297 827 }\r
828 }\r
829 }\r
830\r
831 //\r
832 // Pull range out of descriptor\r
833 //\r
834 if (Entry->Start == Start) {\r
022c6d45 835\r
28a00297 836 //\r
837 // Clip start\r
838 //\r
839 Entry->Start = RangeEnd + 1;\r
840\r
841 } else if (Entry->End == RangeEnd) {\r
022c6d45 842\r
28a00297 843 //\r
844 // Clip end\r
845 //\r
846 Entry->End = Start - 1;\r
847\r
848 } else {\r
849\r
850 //\r
851 // Pull it out of the center, clip current\r
852 //\r
022c6d45 853\r
28a00297 854 //\r
855 // Add a new one\r
856 //\r
857 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
858 mMapStack[mMapDepth].FromPages = FALSE;\r
859 mMapStack[mMapDepth].Type = Entry->Type;\r
860 mMapStack[mMapDepth].Start = RangeEnd+1;\r
861 mMapStack[mMapDepth].End = Entry->End;\r
862\r
863 //\r
864 // Inherit Attribute from the Memory Descriptor that is being clipped\r
865 //\r
866 mMapStack[mMapDepth].Attribute = Entry->Attribute;\r
867\r
868 Entry->End = Start - 1;\r
869 ASSERT (Entry->Start < Entry->End);\r
870\r
871 Entry = &mMapStack[mMapDepth];\r
872 InsertTailList (&gMemoryMap, &Entry->Link);\r
873\r
874 mMapDepth += 1;\r
875 ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
876 }\r
877\r
878 //\r
022c6d45 879 // The new range inherits the same Attribute as the Entry\r
771ee501 880 // it is being cut out of unless attributes are being changed\r
28a00297 881 //\r
771ee501
EC
882 if (ChangingType) {\r
883 Attribute = Entry->Attribute;\r
884 MemType = NewType;\r
885 } else {\r
886 Attribute = NewAttributes;\r
887 MemType = Entry->Type;\r
888 }\r
28a00297 889\r
890 //\r
891 // If the descriptor is empty, then remove it from the map\r
892 //\r
893 if (Entry->Start == Entry->End + 1) {\r
894 RemoveMemoryMapEntry (Entry);\r
895 Entry = NULL;\r
896 }\r
022c6d45 897\r
28a00297 898 //\r
899 // Add our new range in\r
900 //\r
771ee501
EC
901 CoreAddRange (MemType, Start, RangeEnd, Attribute);\r
902 if (ChangingType && (MemType == EfiConventionalMemory)) {\r
9a340872 903 if (Start == 0) {\r
e63da9f0
JW
904 //\r
905 // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this\r
906 // macro will ASSERT() if address is 0. Instead, CoreAddRange()\r
907 // guarantees that the page starting at address 0 is always filled\r
908 // with zeros.\r
909 //\r
9a340872 910 if (RangeEnd > EFI_PAGE_SIZE) {\r
911 DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) EFI_PAGE_SIZE, (UINTN) (RangeEnd - EFI_PAGE_SIZE + 1));\r
912 }\r
913 } else {\r
e63da9f0
JW
914 //\r
915 // If Heap Guard is enabled, the page at the top and/or bottom of\r
916 // this memory block to free might be inaccessible. Skipping them\r
917 // to avoid page fault exception.\r
918 //\r
919 UINT64 StartToClear;\r
920 UINT64 EndToClear;\r
921\r
922 StartToClear = Start;\r
6cf0a677 923 EndToClear = RangeEnd + 1;\r
e63da9f0
JW
924 if (PcdGet8 (PcdHeapGuardPropertyMask) & (BIT1|BIT0)) {\r
925 if (IsGuardPage(StartToClear)) {\r
926 StartToClear += EFI_PAGE_SIZE;\r
927 }\r
6cf0a677 928 if (IsGuardPage (EndToClear - 1)) {\r
e63da9f0
JW
929 EndToClear -= EFI_PAGE_SIZE;\r
930 }\r
e63da9f0
JW
931 }\r
932\r
6cf0a677
JW
933 if (EndToClear > StartToClear) {\r
934 DEBUG_CLEAR_MEMORY(\r
935 (VOID *)(UINTN)StartToClear,\r
936 (UINTN)(EndToClear - StartToClear)\r
937 );\r
938 }\r
9a340872 939 }\r
38c7df98 940 }\r
28a00297 941\r
942 //\r
943 // Move any map descriptor stack to general pool\r
944 //\r
945 CoreFreeMemoryMapStack ();\r
946\r
947 //\r
948 // Bump the starting address, and convert the next range\r
949 //\r
950 Start = RangeEnd + 1;\r
951 }\r
952\r
953 //\r
954 // Converted the whole range, done\r
955 //\r
956\r
957 return EFI_SUCCESS;\r
958}\r
959\r
960\r
771ee501
EC
961/**\r
962 Internal function. Converts a memory range to the specified type.\r
963 The range must exist in the memory map.\r
964\r
965 @param Start The first address of the range Must be page\r
966 aligned\r
967 @param NumberOfPages The number of pages to convert\r
968 @param NewType The new type for the memory range\r
969\r
970 @retval EFI_INVALID_PARAMETER Invalid parameter\r
971 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r
972 range or convertion not allowed.\r
973 @retval EFI_SUCCESS Successfully converts the memory range to the\r
974 specified type.\r
975\r
976**/\r
977EFI_STATUS\r
978CoreConvertPages (\r
979 IN UINT64 Start,\r
980 IN UINT64 NumberOfPages,\r
981 IN EFI_MEMORY_TYPE NewType\r
982 )\r
983{\r
984 return CoreConvertPagesEx(Start, NumberOfPages, TRUE, NewType, FALSE, 0);\r
985}\r
986\r
987\r
988/**\r
989 Internal function. Converts a memory range to use new attributes.\r
990\r
991 @param Start The first address of the range Must be page\r
992 aligned\r
993 @param NumberOfPages The number of pages to convert\r
994 @param NewAttributes The new attributes value for the range.\r
995\r
771ee501
EC
996**/\r
997VOID\r
998CoreUpdateMemoryAttributes (\r
999 IN EFI_PHYSICAL_ADDRESS Start,\r
1000 IN UINT64 NumberOfPages,\r
1001 IN UINT64 NewAttributes\r
1002 )\r
1003{\r
1004 CoreAcquireMemoryLock ();\r
1005\r
1006 //\r
1007 // Update the attributes to the new value\r
1008 //\r
1009 CoreConvertPagesEx(Start, NumberOfPages, FALSE, (EFI_MEMORY_TYPE)0, TRUE, NewAttributes);\r
1010\r
1011 CoreReleaseMemoryLock ();\r
1012}\r
1013\r
162ed594 1014\r
1015/**\r
1016 Internal function. Finds a consecutive free page range below\r
1017 the requested address.\r
1018\r
022c6d45 1019 @param MaxAddress The address that the range must be below\r
35f9e94e 1020 @param MinAddress The address that the range must be above\r
022c6d45 1021 @param NumberOfPages Number of pages needed\r
1022 @param NewType The type of memory the range is going to be\r
1023 turned into\r
1024 @param Alignment Bits to align with\r
e63da9f0 1025 @param NeedGuard Flag to indicate Guard page is needed or not\r
162ed594 1026\r
1027 @return The base address of the range, or 0 if the range was not found\r
1028\r
1029**/\r
28a00297 1030UINT64\r
1031CoreFindFreePagesI (\r
1032 IN UINT64 MaxAddress,\r
2345e7d4 1033 IN UINT64 MinAddress,\r
28a00297 1034 IN UINT64 NumberOfPages,\r
1035 IN EFI_MEMORY_TYPE NewType,\r
e63da9f0
JW
1036 IN UINTN Alignment,\r
1037 IN BOOLEAN NeedGuard\r
28a00297 1038 )\r
28a00297 1039{\r
1040 UINT64 NumberOfBytes;\r
1041 UINT64 Target;\r
1042 UINT64 DescStart;\r
1043 UINT64 DescEnd;\r
1044 UINT64 DescNumberOfBytes;\r
1045 LIST_ENTRY *Link;\r
1046 MEMORY_MAP *Entry;\r
1047\r
1048 if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages == 0)) {\r
1049 return 0;\r
1050 }\r
1051\r
1052 if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {\r
022c6d45 1053\r
28a00297 1054 //\r
1055 // If MaxAddress is not aligned to the end of a page\r
1056 //\r
022c6d45 1057\r
28a00297 1058 //\r
1059 // Change MaxAddress to be 1 page lower\r
1060 //\r
1061 MaxAddress -= (EFI_PAGE_MASK + 1);\r
022c6d45 1062\r
28a00297 1063 //\r
1064 // Set MaxAddress to a page boundary\r
1065 //\r
6e1e5405 1066 MaxAddress &= ~(UINT64)EFI_PAGE_MASK;\r
022c6d45 1067\r
28a00297 1068 //\r
1069 // Set MaxAddress to end of the page\r
1070 //\r
1071 MaxAddress |= EFI_PAGE_MASK;\r
1072 }\r
1073\r
1074 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
1075 Target = 0;\r
1076\r
1077 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1078 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
022c6d45 1079\r
28a00297 1080 //\r
1081 // If it's not a free entry, don't bother with it\r
1082 //\r
1083 if (Entry->Type != EfiConventionalMemory) {\r
1084 continue;\r
1085 }\r
1086\r
1087 DescStart = Entry->Start;\r
1088 DescEnd = Entry->End;\r
1089\r
1090 //\r
2345e7d4 1091 // If desc is past max allowed address or below min allowed address, skip it\r
28a00297 1092 //\r
2345e7d4 1093 if ((DescStart >= MaxAddress) || (DescEnd < MinAddress)) {\r
28a00297 1094 continue;\r
1095 }\r
1096\r
1097 //\r
1098 // If desc ends past max allowed address, clip the end\r
1099 //\r
1100 if (DescEnd >= MaxAddress) {\r
1101 DescEnd = MaxAddress;\r
1102 }\r
1103\r
1104 DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;\r
1105\r
1365bedd
HG
1106 // Skip if DescEnd is less than DescStart after alignment clipping\r
1107 if (DescEnd < DescStart) {\r
1108 continue;\r
1109 }\r
1110\r
28a00297 1111 //\r
022c6d45 1112 // Compute the number of bytes we can used from this\r
28a00297 1113 // descriptor, and see it's enough to satisfy the request\r
1114 //\r
1115 DescNumberOfBytes = DescEnd - DescStart + 1;\r
1116\r
1117 if (DescNumberOfBytes >= NumberOfBytes) {\r
2345e7d4 1118 //\r
1119 // If the start of the allocated range is below the min address allowed, skip it\r
1120 //\r
1121 if ((DescEnd - NumberOfBytes + 1) < MinAddress) {\r
1122 continue;\r
1123 }\r
28a00297 1124\r
1125 //\r
1126 // If this is the best match so far remember it\r
1127 //\r
1128 if (DescEnd > Target) {\r
e63da9f0
JW
1129 if (NeedGuard) {\r
1130 DescEnd = AdjustMemoryS (\r
1131 DescEnd + 1 - DescNumberOfBytes,\r
1132 DescNumberOfBytes,\r
1133 NumberOfBytes\r
1134 );\r
1135 if (DescEnd == 0) {\r
1136 continue;\r
1137 }\r
1138 }\r
1139\r
28a00297 1140 Target = DescEnd;\r
1141 }\r
1142 }\r
022c6d45 1143 }\r
28a00297 1144\r
1145 //\r
1146 // If this is a grow down, adjust target to be the allocation base\r
1147 //\r
1148 Target -= NumberOfBytes - 1;\r
1149\r
1150 //\r
1151 // If we didn't find a match, return 0\r
1152 //\r
1153 if ((Target & EFI_PAGE_MASK) != 0) {\r
1154 return 0;\r
1155 }\r
1156\r
1157 return Target;\r
1158}\r
1159\r
162ed594 1160\r
1161/**\r
1162 Internal function. Finds a consecutive free page range below\r
1163 the requested address\r
1164\r
022c6d45 1165 @param MaxAddress The address that the range must be below\r
1166 @param NoPages Number of pages needed\r
1167 @param NewType The type of memory the range is going to be\r
1168 turned into\r
1169 @param Alignment Bits to align with\r
e63da9f0 1170 @param NeedGuard Flag to indicate Guard page is needed or not\r
162ed594 1171\r
1172 @return The base address of the range, or 0 if the range was not found.\r
1173\r
1174**/\r
28a00297 1175UINT64\r
1176FindFreePages (\r
1177 IN UINT64 MaxAddress,\r
1178 IN UINT64 NoPages,\r
1179 IN EFI_MEMORY_TYPE NewType,\r
e63da9f0
JW
1180 IN UINTN Alignment,\r
1181 IN BOOLEAN NeedGuard\r
28a00297 1182 )\r
28a00297 1183{\r
2345e7d4 1184 UINT64 Start;\r
28a00297 1185\r
2345e7d4 1186 //\r
1187 // Attempt to find free pages in the preferred bin based on the requested memory type\r
1188 //\r
3d78c020 1189 if ((UINT32)NewType < EfiMaxMemoryType && MaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {\r
2345e7d4 1190 Start = CoreFindFreePagesI (\r
1191 mMemoryTypeStatistics[NewType].MaximumAddress, \r
1192 mMemoryTypeStatistics[NewType].BaseAddress, \r
1193 NoPages, \r
1194 NewType, \r
e63da9f0
JW
1195 Alignment,\r
1196 NeedGuard\r
2345e7d4 1197 );\r
1198 if (Start != 0) {\r
1199 return Start;\r
1200 }\r
1201 }\r
28a00297 1202\r
2345e7d4 1203 //\r
1204 // Attempt to find free pages in the default allocation bin\r
1205 //\r
1206 if (MaxAddress >= mDefaultMaximumAddress) {\r
e63da9f0
JW
1207 Start = CoreFindFreePagesI (mDefaultMaximumAddress, 0, NoPages, NewType,\r
1208 Alignment, NeedGuard);\r
2345e7d4 1209 if (Start != 0) {\r
1210 if (Start < mDefaultBaseAddress) {\r
1211 mDefaultBaseAddress = Start;\r
1212 }\r
1213 return Start;\r
28a00297 1214 }\r
1215 }\r
1216\r
2345e7d4 1217 //\r
1218 // The allocation did not succeed in any of the prefered bins even after \r
1219 // promoting resources. Attempt to find free pages anywhere is the requested \r
1220 // address range. If this allocation fails, then there are not enough \r
1221 // resources anywhere to satisfy the request.\r
1222 //\r
e63da9f0
JW
1223 Start = CoreFindFreePagesI (MaxAddress, 0, NoPages, NewType, Alignment,\r
1224 NeedGuard);\r
2345e7d4 1225 if (Start != 0) {\r
1226 return Start;\r
1227 }\r
28a00297 1228\r
2345e7d4 1229 //\r
1230 // If allocations from the preferred bins fail, then attempt to promote memory resources.\r
1231 //\r
1232 if (!PromoteMemoryResource ()) {\r
1233 return 0;\r
28a00297 1234 }\r
1235\r
2345e7d4 1236 //\r
1237 // If any memory resources were promoted, then re-attempt the allocation\r
1238 //\r
e63da9f0 1239 return FindFreePages (MaxAddress, NoPages, NewType, Alignment, NeedGuard);\r
28a00297 1240}\r
1241\r
1242\r
162ed594 1243/**\r
1244 Allocates pages from the memory map.\r
1245\r
022c6d45 1246 @param Type The type of allocation to perform\r
1247 @param MemoryType The type of memory to turn the allocated pages\r
1248 into\r
1249 @param NumberOfPages The number of pages to allocate\r
1250 @param Memory A pointer to receive the base allocated memory\r
1251 address\r
e63da9f0 1252 @param NeedGuard Flag to indicate Guard page is needed or not\r
162ed594 1253\r
1254 @return Status. On success, Memory is filled in with the base address allocated\r
022c6d45 1255 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in\r
1256 spec.\r
1257 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r
1258 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r
162ed594 1259 @retval EFI_SUCCESS Pages successfully allocated.\r
1260\r
1261**/\r
28a00297 1262EFI_STATUS\r
1263EFIAPI\r
84edd20b 1264CoreInternalAllocatePages (\r
28a00297 1265 IN EFI_ALLOCATE_TYPE Type,\r
1266 IN EFI_MEMORY_TYPE MemoryType,\r
1267 IN UINTN NumberOfPages,\r
e63da9f0
JW
1268 IN OUT EFI_PHYSICAL_ADDRESS *Memory,\r
1269 IN BOOLEAN NeedGuard\r
28a00297 1270 )\r
28a00297 1271{\r
1272 EFI_STATUS Status;\r
1273 UINT64 Start;\r
c2a07a10
SZ
1274 UINT64 NumberOfBytes;\r
1275 UINT64 End;\r
28a00297 1276 UINT64 MaxAddress;\r
1277 UINTN Alignment;\r
1278\r
3d78c020 1279 if ((UINT32)Type >= MaxAllocateType) {\r
28a00297 1280 return EFI_INVALID_PARAMETER;\r
1281 }\r
1282\r
8ee25f48 1283 if ((MemoryType >= EfiMaxMemoryType && MemoryType < MEMORY_TYPE_OEM_RESERVED_MIN) ||\r
a671a012 1284 (MemoryType == EfiConventionalMemory) || (MemoryType == EfiPersistentMemory)) {\r
28a00297 1285 return EFI_INVALID_PARAMETER;\r
1286 }\r
1287\r
3e058701
ED
1288 if (Memory == NULL) {\r
1289 return EFI_INVALID_PARAMETER;\r
1290 }\r
1291\r
d4731a98 1292 Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r
28a00297 1293\r
1294 if (MemoryType == EfiACPIReclaimMemory ||\r
1295 MemoryType == EfiACPIMemoryNVS ||\r
1296 MemoryType == EfiRuntimeServicesCode ||\r
1297 MemoryType == EfiRuntimeServicesData) {\r
1298\r
d4731a98 1299 Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r
28a00297 1300 }\r
1301\r
1302 if (Type == AllocateAddress) {\r
1303 if ((*Memory & (Alignment - 1)) != 0) {\r
1304 return EFI_NOT_FOUND;\r
1305 }\r
1306 }\r
1307\r
1308 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1309 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1310\r
1311 //\r
022c6d45 1312 // If this is for below a particular address, then\r
28a00297 1313 //\r
1314 Start = *Memory;\r
022c6d45 1315\r
28a00297 1316 //\r
1317 // The max address is the max natively addressable address for the processor\r
1318 //\r
f3f2e05d 1319 MaxAddress = MAX_ADDRESS;\r
022c6d45 1320\r
c2a07a10
SZ
1321 //\r
1322 // Check for Type AllocateAddress,\r
1323 // if NumberOfPages is 0 or\r
1324 // if (NumberOfPages << EFI_PAGE_SHIFT) is above MAX_ADDRESS or\r
1325 // if (Start + NumberOfBytes) rolls over 0 or\r
1326 // if Start is above MAX_ADDRESS or\r
1327 // if End is above MAX_ADDRESS,\r
1328 // return EFI_NOT_FOUND.\r
1329 //\r
1330 if (Type == AllocateAddress) {\r
1331 if ((NumberOfPages == 0) ||\r
1332 (NumberOfPages > RShiftU64 (MaxAddress, EFI_PAGE_SHIFT))) {\r
1333 return EFI_NOT_FOUND;\r
1334 }\r
1335 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
1336 End = Start + NumberOfBytes - 1;\r
1337\r
1338 if ((Start >= End) ||\r
1339 (Start > MaxAddress) || \r
1340 (End > MaxAddress)) {\r
1341 return EFI_NOT_FOUND;\r
1342 }\r
1343 }\r
1344\r
28a00297 1345 if (Type == AllocateMaxAddress) {\r
1346 MaxAddress = Start;\r
1347 }\r
1348\r
1349 CoreAcquireMemoryLock ();\r
022c6d45 1350\r
28a00297 1351 //\r
1352 // If not a specific address, then find an address to allocate\r
1353 //\r
1354 if (Type != AllocateAddress) {\r
e63da9f0
JW
1355 Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment,\r
1356 NeedGuard);\r
28a00297 1357 if (Start == 0) {\r
1358 Status = EFI_OUT_OF_RESOURCES;\r
1359 goto Done;\r
1360 }\r
1361 }\r
1362\r
1363 //\r
1364 // Convert pages from FreeMemory to the requested type\r
1365 //\r
e63da9f0
JW
1366 if (NeedGuard) {\r
1367 Status = CoreConvertPagesWithGuard(Start, NumberOfPages, MemoryType);\r
1368 } else {\r
1369 Status = CoreConvertPages(Start, NumberOfPages, MemoryType);\r
1370 }\r
28a00297 1371\r
1372Done:\r
1373 CoreReleaseMemoryLock ();\r
1374\r
1375 if (!EFI_ERROR (Status)) {\r
e63da9f0
JW
1376 if (NeedGuard) {\r
1377 SetGuardForMemory (Start, NumberOfPages);\r
1378 }\r
28a00297 1379 *Memory = Start;\r
1380 }\r
1381\r
1382 return Status;\r
1383}\r
1384\r
84edd20b
SZ
1385/**\r
1386 Allocates pages from the memory map.\r
1387\r
1388 @param Type The type of allocation to perform\r
1389 @param MemoryType The type of memory to turn the allocated pages\r
1390 into\r
1391 @param NumberOfPages The number of pages to allocate\r
1392 @param Memory A pointer to receive the base allocated memory\r
1393 address\r
1394\r
1395 @return Status. On success, Memory is filled in with the base address allocated\r
1396 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in\r
1397 spec.\r
1398 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r
1399 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r
1400 @retval EFI_SUCCESS Pages successfully allocated.\r
1401\r
1402**/\r
1403EFI_STATUS\r
1404EFIAPI\r
1405CoreAllocatePages (\r
1406 IN EFI_ALLOCATE_TYPE Type,\r
1407 IN EFI_MEMORY_TYPE MemoryType,\r
1408 IN UINTN NumberOfPages,\r
1409 OUT EFI_PHYSICAL_ADDRESS *Memory\r
1410 )\r
1411{\r
1412 EFI_STATUS Status;\r
e63da9f0 1413 BOOLEAN NeedGuard;\r
84edd20b 1414\r
e63da9f0
JW
1415 NeedGuard = IsPageTypeToGuard (MemoryType, Type) && !mOnGuarding;\r
1416 Status = CoreInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory,\r
1417 NeedGuard);\r
84edd20b 1418 if (!EFI_ERROR (Status)) {\r
1d60fe96
SZ
1419 CoreUpdateProfile (\r
1420 (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r
1421 MemoryProfileActionAllocatePages,\r
1422 MemoryType,\r
1423 EFI_PAGES_TO_SIZE (NumberOfPages),\r
1424 (VOID *) (UINTN) *Memory,\r
1425 NULL\r
1426 );\r
74a88770 1427 InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);\r
7eb927db
AB
1428 ApplyMemoryProtectionPolicy (EfiConventionalMemory, MemoryType, *Memory,\r
1429 EFI_PAGES_TO_SIZE (NumberOfPages));\r
84edd20b
SZ
1430 }\r
1431 return Status;\r
1432}\r
28a00297 1433\r
162ed594 1434/**\r
1435 Frees previous allocated pages.\r
1436\r
022c6d45 1437 @param Memory Base address of memory being freed\r
1438 @param NumberOfPages The number of pages to free\r
925f0d1a 1439 @param MemoryType Pointer to memory type\r
162ed594 1440\r
022c6d45 1441 @retval EFI_NOT_FOUND Could not find the entry that covers the range\r
1442 @retval EFI_INVALID_PARAMETER Address not aligned\r
162ed594 1443 @return EFI_SUCCESS -Pages successfully freed.\r
1444\r
1445**/\r
022c6d45 1446EFI_STATUS\r
28a00297 1447EFIAPI\r
84edd20b 1448CoreInternalFreePages (\r
28a00297 1449 IN EFI_PHYSICAL_ADDRESS Memory,\r
925f0d1a
SZ
1450 IN UINTN NumberOfPages,\r
1451 OUT EFI_MEMORY_TYPE *MemoryType OPTIONAL\r
28a00297 1452 )\r
28a00297 1453{\r
1454 EFI_STATUS Status;\r
1455 LIST_ENTRY *Link;\r
1456 MEMORY_MAP *Entry;\r
1457 UINTN Alignment;\r
e63da9f0 1458 BOOLEAN IsGuarded;\r
28a00297 1459\r
1460 //\r
1461 // Free the range\r
1462 //\r
1463 CoreAcquireMemoryLock ();\r
1464\r
1465 //\r
1466 // Find the entry that the covers the range\r
1467 //\r
e63da9f0 1468 IsGuarded = FALSE;\r
28a00297 1469 Entry = NULL;\r
1470 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1471 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1472 if (Entry->Start <= Memory && Entry->End > Memory) {\r
1473 break;\r
1474 }\r
1475 }\r
1476 if (Link == &gMemoryMap) {\r
a5ca8fa7 1477 Status = EFI_NOT_FOUND;\r
1478 goto Done;\r
28a00297 1479 }\r
1480\r
d4731a98 1481 Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r
28a00297 1482\r
525aded9 1483 ASSERT (Entry != NULL);\r
28a00297 1484 if (Entry->Type == EfiACPIReclaimMemory ||\r
1485 Entry->Type == EfiACPIMemoryNVS ||\r
1486 Entry->Type == EfiRuntimeServicesCode ||\r
1487 Entry->Type == EfiRuntimeServicesData) {\r
1488\r
d4731a98 1489 Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r
28a00297 1490\r
1491 }\r
1492\r
1493 if ((Memory & (Alignment - 1)) != 0) {\r
a5ca8fa7 1494 Status = EFI_INVALID_PARAMETER;\r
1495 goto Done;\r
28a00297 1496 }\r
1497\r
1498 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1499 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1500\r
925f0d1a
SZ
1501 if (MemoryType != NULL) {\r
1502 *MemoryType = Entry->Type;\r
1503 }\r
1504\r
e63da9f0
JW
1505 IsGuarded = IsPageTypeToGuard (Entry->Type, AllocateAnyPages) &&\r
1506 IsMemoryGuarded (Memory);\r
1507 if (IsGuarded) {\r
1508 Status = CoreConvertPagesWithGuard (Memory, NumberOfPages,\r
1509 EfiConventionalMemory);\r
1510 } else {\r
1511 Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
28a00297 1512 }\r
1513\r
a5ca8fa7 1514Done:\r
1515 CoreReleaseMemoryLock ();\r
e63da9f0
JW
1516 if (IsGuarded) {\r
1517 UnsetGuardForMemory(Memory, NumberOfPages);\r
1518 }\r
28a00297 1519 return Status;\r
1520}\r
1521\r
84edd20b
SZ
1522/**\r
1523 Frees previous allocated pages.\r
1524\r
1525 @param Memory Base address of memory being freed\r
1526 @param NumberOfPages The number of pages to free\r
1527\r
1528 @retval EFI_NOT_FOUND Could not find the entry that covers the range\r
1529 @retval EFI_INVALID_PARAMETER Address not aligned\r
1530 @return EFI_SUCCESS -Pages successfully freed.\r
1531\r
1532**/\r
1533EFI_STATUS\r
1534EFIAPI\r
1535CoreFreePages (\r
1536 IN EFI_PHYSICAL_ADDRESS Memory,\r
1537 IN UINTN NumberOfPages\r
1538 )\r
1539{\r
925f0d1a
SZ
1540 EFI_STATUS Status;\r
1541 EFI_MEMORY_TYPE MemoryType;\r
736a692e 1542\r
925f0d1a 1543 Status = CoreInternalFreePages (Memory, NumberOfPages, &MemoryType);\r
736a692e 1544 if (!EFI_ERROR (Status)) {\r
1d60fe96
SZ
1545 CoreUpdateProfile (\r
1546 (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r
1547 MemoryProfileActionFreePages,\r
1548 MemoryType,\r
1549 EFI_PAGES_TO_SIZE (NumberOfPages),\r
1550 (VOID *) (UINTN) Memory,\r
1551 NULL\r
1552 );\r
74a88770 1553 InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);\r
7eb927db
AB
1554 ApplyMemoryProtectionPolicy (MemoryType, EfiConventionalMemory, Memory,\r
1555 EFI_PAGES_TO_SIZE (NumberOfPages));\r
736a692e
HT
1556 }\r
1557 return Status;\r
1558}\r
84edd20b 1559\r
2345e7d4 1560/**\r
1561 This function checks to see if the last memory map descriptor in a memory map\r
1562 can be merged with any of the other memory map descriptors in a memorymap.\r
1563 Memory descriptors may be merged if they are adjacent and have the same type\r
1564 and attributes.\r
1565\r
1566 @param MemoryMap A pointer to the start of the memory map.\r
1567 @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.\r
1568 @param DescriptorSize The size, in bytes, of an individual\r
1569 EFI_MEMORY_DESCRIPTOR.\r
1570\r
1571 @return A pointer to the next available descriptor in MemoryMap\r
1572\r
1573**/\r
1574EFI_MEMORY_DESCRIPTOR *\r
1575MergeMemoryMapDescriptor (\r
1576 IN EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
1577 IN EFI_MEMORY_DESCRIPTOR *MemoryMapDescriptor,\r
1578 IN UINTN DescriptorSize\r
1579 )\r
1580{\r
1581 //\r
1582 // Traverse the array of descriptors in MemoryMap\r
1583 //\r
1584 for (; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) {\r
1585 //\r
1586 // Check to see if the Type fields are identical.\r
1587 //\r
1588 if (MemoryMap->Type != MemoryMapDescriptor->Type) {\r
1589 continue;\r
1590 }\r
1591\r
1592 //\r
1593 // Check to see if the Attribute fields are identical.\r
1594 //\r
1595 if (MemoryMap->Attribute != MemoryMapDescriptor->Attribute) {\r
1596 continue;\r
1597 }\r
1598\r
1599 //\r
1600 // Check to see if MemoryMapDescriptor is immediately above MemoryMap\r
1601 //\r
1602 if (MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) { \r
1603 //\r
1604 // Merge MemoryMapDescriptor into MemoryMap\r
1605 //\r
1606 MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r
1607\r
1608 //\r
1609 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r
1610 //\r
1611 return MemoryMapDescriptor;\r
1612 }\r
1613\r
1614 //\r
1615 // Check to see if MemoryMapDescriptor is immediately below MemoryMap\r
1616 //\r
1617 if (MemoryMap->PhysicalStart - EFI_PAGES_TO_SIZE ((UINTN)MemoryMapDescriptor->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {\r
1618 //\r
1619 // Merge MemoryMapDescriptor into MemoryMap\r
1620 //\r
1621 MemoryMap->PhysicalStart = MemoryMapDescriptor->PhysicalStart;\r
1622 MemoryMap->VirtualStart = MemoryMapDescriptor->VirtualStart;\r
1623 MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r
1624\r
1625 //\r
1626 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r
1627 //\r
1628 return MemoryMapDescriptor;\r
1629 }\r
1630 }\r
1631\r
1632 //\r
1633 // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.\r
1634 //\r
1635 // Return the slot immediately after MemoryMapDescriptor as the next available \r
1636 // slot in the MemoryMap array\r
1637 //\r
1638 return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor, DescriptorSize);\r
1639}\r
28a00297 1640\r
162ed594 1641/**\r
1642 This function returns a copy of the current memory map. The map is an array of\r
1643 memory descriptors, each of which describes a contiguous block of memory.\r
1644\r
022c6d45 1645 @param MemoryMapSize A pointer to the size, in bytes, of the\r
1646 MemoryMap buffer. On input, this is the size of\r
1647 the buffer allocated by the caller. On output,\r
1648 it is the size of the buffer returned by the\r
1649 firmware if the buffer was large enough, or the\r
1650 size of the buffer needed to contain the map if\r
1651 the buffer was too small.\r
1652 @param MemoryMap A pointer to the buffer in which firmware places\r
1653 the current memory map.\r
1654 @param MapKey A pointer to the location in which firmware\r
1655 returns the key for the current memory map.\r
1656 @param DescriptorSize A pointer to the location in which firmware\r
1657 returns the size, in bytes, of an individual\r
1658 EFI_MEMORY_DESCRIPTOR.\r
1659 @param DescriptorVersion A pointer to the location in which firmware\r
1660 returns the version number associated with the\r
1661 EFI_MEMORY_DESCRIPTOR.\r
1662\r
1663 @retval EFI_SUCCESS The memory map was returned in the MemoryMap\r
1664 buffer.\r
1665 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current\r
1666 buffer size needed to hold the memory map is\r
1667 returned in MemoryMapSize.\r
162ed594 1668 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
1669\r
1670**/\r
28a00297 1671EFI_STATUS\r
1672EFIAPI\r
1673CoreGetMemoryMap (\r
1674 IN OUT UINTN *MemoryMapSize,\r
1675 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
1676 OUT UINTN *MapKey,\r
1677 OUT UINTN *DescriptorSize,\r
1678 OUT UINT32 *DescriptorVersion\r
1679 )\r
28a00297 1680{\r
1681 EFI_STATUS Status;\r
022c6d45 1682 UINTN Size;\r
1683 UINTN BufferSize;\r
ba2c0527 1684 UINTN NumberOfEntries;\r
28a00297 1685 LIST_ENTRY *Link;\r
022c6d45 1686 MEMORY_MAP *Entry;\r
1687 EFI_GCD_MAP_ENTRY *GcdMapEntry;\r
46a65f18 1688 EFI_GCD_MAP_ENTRY MergeGcdMapEntry;\r
b74350e9 1689 EFI_MEMORY_TYPE Type;\r
2345e7d4 1690 EFI_MEMORY_DESCRIPTOR *MemoryMapStart;\r
e38451cd 1691 EFI_MEMORY_DESCRIPTOR *MemoryMapEnd;\r
28a00297 1692\r
1693 //\r
1694 // Make sure the parameters are valid\r
1695 //\r
1696 if (MemoryMapSize == NULL) {\r
1697 return EFI_INVALID_PARAMETER;\r
1698 }\r
022c6d45 1699\r
28a00297 1700 CoreAcquireGcdMemoryLock ();\r
022c6d45 1701\r
28a00297 1702 //\r
ba2c0527 1703 // Count the number of Reserved and runtime MMIO entries\r
a671a012 1704 // And, count the number of Persistent entries.\r
28a00297 1705 //\r
ba2c0527 1706 NumberOfEntries = 0;\r
28a00297 1707 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
1708 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
35ac962b 1709 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypePersistent) || \r
ba2c0527
LG
1710 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1711 ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&\r
1712 ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {\r
1713 NumberOfEntries ++;\r
a671a012 1714 }\r
28a00297 1715 }\r
1716\r
1717 Size = sizeof (EFI_MEMORY_DESCRIPTOR);\r
1718\r
1719 //\r
1720 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will\r
1721 // prevent people from having pointer math bugs in their code.\r
1722 // now you have to use *DescriptorSize to make things work.\r
1723 //\r
1724 Size += sizeof(UINT64) - (Size % sizeof (UINT64));\r
1725\r
1726 if (DescriptorSize != NULL) {\r
1727 *DescriptorSize = Size;\r
1728 }\r
022c6d45 1729\r
28a00297 1730 if (DescriptorVersion != NULL) {\r
1731 *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;\r
1732 }\r
1733\r
1734 CoreAcquireMemoryLock ();\r
1735\r
1736 //\r
1737 // Compute the buffer size needed to fit the entire map\r
1738 //\r
ba2c0527 1739 BufferSize = Size * NumberOfEntries;\r
28a00297 1740 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1741 BufferSize += Size;\r
1742 }\r
1743\r
1744 if (*MemoryMapSize < BufferSize) {\r
1745 Status = EFI_BUFFER_TOO_SMALL;\r
1746 goto Done;\r
1747 }\r
1748\r
1749 if (MemoryMap == NULL) {\r
1750 Status = EFI_INVALID_PARAMETER;\r
1751 goto Done;\r
1752 }\r
1753\r
1754 //\r
1755 // Build the map\r
1756 //\r
383c303c 1757 ZeroMem (MemoryMap, BufferSize);\r
2345e7d4 1758 MemoryMapStart = MemoryMap;\r
28a00297 1759 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1760 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1761 ASSERT (Entry->VirtualStart == 0);\r
1762\r
b74350e9 1763 //\r
1764 // Convert internal map into an EFI_MEMORY_DESCRIPTOR\r
1765 //\r
28a00297 1766 MemoryMap->Type = Entry->Type;\r
1767 MemoryMap->PhysicalStart = Entry->Start;\r
1768 MemoryMap->VirtualStart = Entry->VirtualStart;\r
1769 MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);\r
b74350e9 1770 //\r
1771 // If the memory type is EfiConventionalMemory, then determine if the range is part of a\r
022c6d45 1772 // memory type bin and needs to be converted to the same memory type as the rest of the\r
1773 // memory type bin in order to minimize EFI Memory Map changes across reboots. This\r
b74350e9 1774 // improves the chances for a successful S4 resume in the presence of minor page allocation\r
1775 // differences across reboots.\r
1776 //\r
1777 if (MemoryMap->Type == EfiConventionalMemory) {\r
1778 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
1779 if (mMemoryTypeStatistics[Type].Special &&\r
1780 mMemoryTypeStatistics[Type].NumberOfPages > 0 &&\r
1781 Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress &&\r
e94a9ff7 1782 Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) {\r
b74350e9 1783 MemoryMap->Type = Type;\r
1784 }\r
1785 }\r
1786 }\r
1787 MemoryMap->Attribute = Entry->Attribute;\r
10fe0d81
RN
1788 if (MemoryMap->Type < EfiMaxMemoryType) {\r
1789 if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {\r
1790 MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;\r
1791 }\r
28a00297 1792 }\r
022c6d45 1793\r
2345e7d4 1794 //\r
1795 // Check to see if the new Memory Map Descriptor can be merged with an \r
1796 // existing descriptor if they are adjacent and have the same attributes\r
1797 //\r
1798 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
28a00297 1799 }\r
1800\r
46a65f18
LG
1801 \r
1802 ZeroMem (&MergeGcdMapEntry, sizeof (MergeGcdMapEntry));\r
1803 GcdMapEntry = NULL;\r
1804 for (Link = mGcdMemorySpaceMap.ForwardLink; ; Link = Link->ForwardLink) {\r
1805 if (Link != &mGcdMemorySpaceMap) {\r
1806 //\r
1807 // Merge adjacent same type and attribute GCD memory range\r
1808 //\r
1809 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
1810 \r
1811 if ((MergeGcdMapEntry.Capabilities == GcdMapEntry->Capabilities) && \r
1812 (MergeGcdMapEntry.Attributes == GcdMapEntry->Attributes) &&\r
1813 (MergeGcdMapEntry.GcdMemoryType == GcdMapEntry->GcdMemoryType) &&\r
1814 (MergeGcdMapEntry.GcdIoType == GcdMapEntry->GcdIoType)) {\r
1815 MergeGcdMapEntry.EndAddress = GcdMapEntry->EndAddress;\r
1816 continue;\r
1817 }\r
1818 }\r
1819\r
1820 if ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1821 ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&\r
1822 ((MergeGcdMapEntry.Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {\r
1823 //\r
1824 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR, \r
1825 // it will be recorded as page PhysicalStart and NumberOfPages. \r
1826 //\r
1827 ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);\r
1828 ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);\r
1829 \r
ba2c0527
LG
1830 // \r
1831 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and runtime MMIO GCD entries\r
1832 //\r
46a65f18 1833 MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;\r
ba2c0527 1834 MemoryMap->VirtualStart = 0;\r
46a65f18
LG
1835 MemoryMap->NumberOfPages = RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);\r
1836 MemoryMap->Attribute = (MergeGcdMapEntry.Attributes & ~EFI_MEMORY_PORT_IO) | \r
1837 (MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |\r
ff0c6d66 1838 EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));\r
ba2c0527 1839\r
46a65f18 1840 if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) {\r
ba2c0527 1841 MemoryMap->Type = EfiReservedMemoryType;\r
46a65f18
LG
1842 } else if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {\r
1843 if ((MergeGcdMapEntry.Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {\r
ba2c0527
LG
1844 MemoryMap->Type = EfiMemoryMappedIOPortSpace;\r
1845 } else {\r
1846 MemoryMap->Type = EfiMemoryMappedIO;\r
28a00297 1847 }\r
28a00297 1848 }\r
ba2c0527
LG
1849\r
1850 //\r
1851 // Check to see if the new Memory Map Descriptor can be merged with an \r
1852 // existing descriptor if they are adjacent and have the same attributes\r
1853 //\r
1854 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
28a00297 1855 }\r
a671a012 1856 \r
35ac962b 1857 if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypePersistent) {\r
46a65f18
LG
1858 //\r
1859 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR, \r
1860 // it will be recorded as page PhysicalStart and NumberOfPages. \r
1861 //\r
1862 ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);\r
1863 ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);\r
1864\r
a671a012
LG
1865 // \r
1866 // Create EFI_MEMORY_DESCRIPTOR for every Persistent GCD entries\r
1867 //\r
46a65f18 1868 MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;\r
a671a012 1869 MemoryMap->VirtualStart = 0;\r
46a65f18
LG
1870 MemoryMap->NumberOfPages = RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);\r
1871 MemoryMap->Attribute = MergeGcdMapEntry.Attributes | EFI_MEMORY_NV | \r
1872 (MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |\r
ff0c6d66 1873 EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));\r
a671a012
LG
1874 MemoryMap->Type = EfiPersistentMemory;\r
1875 \r
1876 //\r
1877 // Check to see if the new Memory Map Descriptor can be merged with an \r
1878 // existing descriptor if they are adjacent and have the same attributes\r
1879 //\r
1880 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
1881 }\r
46a65f18
LG
1882 if (Link == &mGcdMemorySpaceMap) {\r
1883 //\r
1884 // break loop when arrive at head.\r
1885 //\r
1886 break;\r
1887 }\r
1888 if (GcdMapEntry != NULL) {\r
1889 //\r
1890 // Copy new GCD map entry for the following GCD range merge\r
1891 //\r
1892 CopyMem (&MergeGcdMapEntry, GcdMapEntry, sizeof (MergeGcdMapEntry));\r
1893 }\r
28a00297 1894 }\r
022c6d45 1895\r
2345e7d4 1896 //\r
1897 // Compute the size of the buffer actually used after all memory map descriptor merge operations\r
1898 //\r
1899 BufferSize = ((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart);\r
1900\r
e38451cd
JW
1901 //\r
1902 // Note: Some OSs will treat EFI_MEMORY_DESCRIPTOR.Attribute as really\r
1903 // set attributes and change memory paging attribute accordingly.\r
1904 // But current EFI_MEMORY_DESCRIPTOR.Attribute is assigned by\r
1905 // value from Capabilities in GCD memory map. This might cause\r
1906 // boot problems. Clearing all paging related capabilities can\r
1907 // workaround it. Following code is supposed to be removed once\r
1908 // the usage of EFI_MEMORY_DESCRIPTOR.Attribute is clarified in\r
1909 // UEFI spec and adopted by both EDK-II Core and all supported\r
1910 // OSs.\r
1911 //\r
1912 MemoryMapEnd = MemoryMap;\r
1913 MemoryMap = MemoryMapStart;\r
1914 while (MemoryMap < MemoryMapEnd) {\r
1915 MemoryMap->Attribute &= ~(UINT64)(EFI_MEMORY_RP | EFI_MEMORY_RO |\r
1916 EFI_MEMORY_XP);\r
1917 MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);\r
1918 }\r
646127c1
JW
1919 MergeMemoryMap (MemoryMapStart, &BufferSize, Size);\r
1920 MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMapStart + BufferSize);\r
e38451cd 1921\r
28a00297 1922 Status = EFI_SUCCESS;\r
1923\r
1924Done:\r
022c6d45 1925 //\r
1926 // Update the map key finally\r
1927 //\r
28a00297 1928 if (MapKey != NULL) {\r
1929 *MapKey = mMemoryMapKey;\r
1930 }\r
022c6d45 1931\r
e439df50 1932 CoreReleaseMemoryLock ();\r
1933\r
1934 CoreReleaseGcdMemoryLock ();\r
1935\r
28a00297 1936 *MemoryMapSize = BufferSize;\r
022c6d45 1937\r
e63da9f0
JW
1938 DEBUG_CODE (\r
1939 if (PcdGet8 (PcdHeapGuardPropertyMask) & (BIT1|BIT0)) {\r
1940 DumpGuardedMemoryBitmap ();\r
1941 }\r
1942 );\r
1943\r
28a00297 1944 return Status;\r
1945}\r
1946\r
28a00297 1947\r
162ed594 1948/**\r
28a00297 1949 Internal function. Used by the pool functions to allocate pages\r
1950 to back pool allocation requests.\r
1951\r
022c6d45 1952 @param PoolType The type of memory for the new pool pages\r
1953 @param NumberOfPages No of pages to allocate\r
1954 @param Alignment Bits to align.\r
e63da9f0 1955 @param NeedGuard Flag to indicate Guard page is needed or not\r
28a00297 1956\r
162ed594 1957 @return The allocated memory, or NULL\r
28a00297 1958\r
162ed594 1959**/\r
1960VOID *\r
1961CoreAllocatePoolPages (\r
1962 IN EFI_MEMORY_TYPE PoolType,\r
1963 IN UINTN NumberOfPages,\r
e63da9f0
JW
1964 IN UINTN Alignment,\r
1965 IN BOOLEAN NeedGuard\r
162ed594 1966 )\r
28a00297 1967{\r
1968 UINT64 Start;\r
1969\r
1970 //\r
1971 // Find the pages to convert\r
1972 //\r
e63da9f0
JW
1973 Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment,\r
1974 NeedGuard);\r
28a00297 1975\r
1976 //\r
1977 // Convert it to boot services data\r
1978 //\r
1979 if (Start == 0) {\r
7df7393f 1980 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32)NumberOfPages));\r
28a00297 1981 } else {\r
e63da9f0
JW
1982 if (NeedGuard) {\r
1983 CoreConvertPagesWithGuard (Start, NumberOfPages, PoolType);\r
1984 } else {\r
1985 CoreConvertPages (Start, NumberOfPages, PoolType);\r
1986 }\r
28a00297 1987 }\r
1988\r
e94a9ff7 1989 return (VOID *)(UINTN) Start;\r
28a00297 1990}\r
1991\r
162ed594 1992\r
1993/**\r
1994 Internal function. Frees pool pages allocated via AllocatePoolPages ()\r
1995\r
022c6d45 1996 @param Memory The base address to free\r
162ed594 1997 @param NumberOfPages The number of pages to free\r
1998\r
1999**/\r
28a00297 2000VOID\r
2001CoreFreePoolPages (\r
2002 IN EFI_PHYSICAL_ADDRESS Memory,\r
2003 IN UINTN NumberOfPages\r
2004 )\r
28a00297 2005{\r
2006 CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
2007}\r
2008\r
2009\r
28a00297 2010\r
162ed594 2011/**\r
2012 Make sure the memory map is following all the construction rules,\r
28a00297 2013 it is the last time to check memory map error before exit boot services.\r
2014\r
022c6d45 2015 @param MapKey Memory map key\r
28a00297 2016\r
022c6d45 2017 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction\r
2018 rules.\r
162ed594 2019 @retval EFI_SUCCESS Valid memory map.\r
28a00297 2020\r
162ed594 2021**/\r
2022EFI_STATUS\r
2023CoreTerminateMemoryMap (\r
2024 IN UINTN MapKey\r
2025 )\r
28a00297 2026{\r
2027 EFI_STATUS Status;\r
2028 LIST_ENTRY *Link;\r
2029 MEMORY_MAP *Entry;\r
2030\r
2031 Status = EFI_SUCCESS;\r
2032\r
2033 CoreAcquireMemoryLock ();\r
2034\r
2035 if (MapKey == mMemoryMapKey) {\r
2036\r
2037 //\r
2038 // Make sure the memory map is following all the construction rules\r
2039 // This is the last chance we will be able to display any messages on\r
2040 // the console devices.\r
2041 //\r
2042\r
2043 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
2044 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
7d17a6a1
AB
2045 if (Entry->Type < EfiMaxMemoryType) {\r
2046 if (mMemoryTypeStatistics[Entry->Type].Runtime) {\r
2047 ASSERT (Entry->Type != EfiACPIReclaimMemory);\r
2048 ASSERT (Entry->Type != EfiACPIMemoryNVS);\r
d4731a98 2049 if ((Entry->Start & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {\r
7d17a6a1
AB
2050 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
2051 Status = EFI_INVALID_PARAMETER;\r
2052 goto Done;\r
2053 }\r
d4731a98 2054 if (((Entry->End + 1) & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {\r
7d17a6a1
AB
2055 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
2056 Status = EFI_INVALID_PARAMETER;\r
2057 goto Done;\r
2058 }\r
28a00297 2059 }\r
2060 }\r
2061 }\r
2062\r
2063 //\r
2064 // The map key they gave us matches what we expect. Fall through and\r
2065 // return success. In an ideal world we would clear out all of\r
2066 // EfiBootServicesCode and EfiBootServicesData. However this function\r
2067 // is not the last one called by ExitBootServices(), so we have to\r
2068 // preserve the memory contents.\r
2069 //\r
2070 } else {\r
2071 Status = EFI_INVALID_PARAMETER;\r
2072 }\r
2073\r
d45fd260 2074Done:\r
28a00297 2075 CoreReleaseMemoryLock ();\r
2076\r
2077 return Status;\r
2078}\r
2079\r
2080\r
2081\r
2082\r
2083\r
2084\r
2085\r
2086\r
162ed594 2087\r