]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/Mem/Page.c
Fix comparisons of enumerated types which may cause warnings for some compilers.
[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
3e058701 4Copyright (c) 2007 - 2012, 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
28a00297 17\r
18#define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT (EFI_PAGE_SIZE)\r
19\r
20//\r
d45fd260 21// Entry for tracking the memory regions for each memory type to coalesce similar memory types\r
28a00297 22//\r
23typedef struct {\r
24 EFI_PHYSICAL_ADDRESS BaseAddress;\r
25 EFI_PHYSICAL_ADDRESS MaximumAddress;\r
26 UINT64 CurrentNumberOfPages;\r
b74350e9 27 UINT64 NumberOfPages;\r
28a00297 28 UINTN InformationIndex;\r
b74350e9 29 BOOLEAN Special;\r
30 BOOLEAN Runtime;\r
d613c2a8 31} EFI_MEMORY_TYPE_STATISTICS;\r
28a00297 32\r
33//\r
34// MemoryMap - The current memory map\r
35//\r
36UINTN mMemoryMapKey = 0;\r
37\r
28a00297 38#define MAX_MAP_DEPTH 6\r
dc8d93ca 39\r
40///\r
41/// mMapDepth - depth of new descriptor stack\r
42///\r
28a00297 43UINTN mMapDepth = 0;\r
dc8d93ca 44///\r
45/// mMapStack - space to use as temp storage to build new map descriptors\r
46///\r
28a00297 47MEMORY_MAP mMapStack[MAX_MAP_DEPTH];\r
48UINTN mFreeMapStack = 0;\r
dc8d93ca 49///\r
50/// This list maintain the free memory map list\r
51///\r
e94a9ff7 52LIST_ENTRY mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList);\r
53BOOLEAN mMemoryTypeInformationInitialized = FALSE;\r
28a00297 54\r
d613c2a8 55EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {\r
f3f2e05d 56 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiReservedMemoryType\r
57 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderCode\r
58 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderData\r
59 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesCode\r
60 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesData\r
61 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesCode\r
62 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesData\r
63 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiConventionalMemory\r
64 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiUnusableMemory\r
65 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIReclaimMemory\r
66 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIMemoryNVS\r
67 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIO\r
68 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIOPortSpace\r
69 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiPalCode\r
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
91 { EfiMaxMemoryType, 0 }\r
92};\r
54ea99a7 93//\r
94// Only used when load module at fixed address feature is enabled. True means the memory is alreay successfully allocated\r
95// 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
96// address assigned by DXE core.\r
97//\r
98GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN gLoadFixedAddressCodeMemoryReady = FALSE;\r
d9177625 99\r
162ed594 100/**\r
d9177625 101 Enter critical section by gaining lock on gMemoryLock.\r
162ed594 102\r
103**/\r
23c98c94 104VOID\r
d9177625 105CoreAcquireMemoryLock (\r
106 VOID\r
107 )\r
108{\r
109 CoreAcquireLock (&gMemoryLock);\r
110}\r
111\r
112\r
113\r
114/**\r
115 Exit critical section by releasing lock on gMemoryLock.\r
116\r
117**/\r
118VOID\r
119CoreReleaseMemoryLock (\r
28a00297 120 VOID\r
d9177625 121 )\r
122{\r
123 CoreReleaseLock (&gMemoryLock);\r
124}\r
125\r
126\r
127\r
128\r
129/**\r
130 Internal function. Removes a descriptor entry.\r
131\r
132 @param Entry The entry to remove\r
133\r
134**/\r
135VOID\r
136RemoveMemoryMapEntry (\r
137 IN OUT MEMORY_MAP *Entry\r
138 )\r
139{\r
140 RemoveEntryList (&Entry->Link);\r
141 Entry->Link.ForwardLink = NULL;\r
142\r
143 if (Entry->FromPages) {\r
144 //\r
145 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList\r
146 //\r
147 InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
148 }\r
149}\r
162ed594 150\r
151/**\r
152 Internal function. Adds a ranges to the memory map.\r
153 The range must not already exist in the map.\r
154\r
022c6d45 155 @param Type The type of memory range to add\r
156 @param Start The starting address in the memory range Must be\r
157 paged aligned\r
158 @param End The last address in the range Must be the last\r
159 byte of a page\r
160 @param Attribute The attributes of the memory range to add\r
28a00297 161\r
162ed594 162**/\r
28a00297 163VOID\r
164CoreAddRange (\r
165 IN EFI_MEMORY_TYPE Type,\r
166 IN EFI_PHYSICAL_ADDRESS Start,\r
167 IN EFI_PHYSICAL_ADDRESS End,\r
168 IN UINT64 Attribute\r
d9177625 169 )\r
170{\r
171 LIST_ENTRY *Link;\r
172 MEMORY_MAP *Entry;\r
28a00297 173\r
d9177625 174 ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
175 ASSERT (End > Start) ;\r
162ed594 176\r
d9177625 177 ASSERT_LOCKED (&gMemoryLock);\r
28a00297 178\r
d9177625 179 DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type));\r
162ed594 180\r
d9177625 181 //\r
182 // Memory map being altered so updated key\r
183 //\r
184 mMemoryMapKey += 1;\r
162ed594 185\r
d9177625 186 //\r
187 // UEFI 2.0 added an event group for notificaiton on memory map changes.\r
188 // So we need to signal this Event Group every time the memory map changes.\r
189 // If we are in EFI 1.10 compatability mode no event groups will be\r
190 // found and nothing will happen we we call this function. These events\r
191 // will get signaled but since a lock is held around the call to this\r
192 // function the notificaiton events will only be called after this funciton\r
193 // returns and the lock is released.\r
194 //\r
195 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid);\r
162ed594 196\r
d9177625 197 //\r
198 // Look for adjoining memory descriptor\r
199 //\r
28a00297 200\r
d9177625 201 // Two memory descriptors can only be merged if they have the same Type\r
202 // and the same Attribute\r
203 //\r
162ed594 204\r
d9177625 205 Link = gMemoryMap.ForwardLink;\r
206 while (Link != &gMemoryMap) {\r
207 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
208 Link = Link->ForwardLink;\r
162ed594 209\r
d9177625 210 if (Entry->Type != Type) {\r
211 continue;\r
212 }\r
213\r
214 if (Entry->Attribute != Attribute) {\r
215 continue;\r
216 }\r
217\r
218 if (Entry->End + 1 == Start) {\r
219\r
220 Start = Entry->Start;\r
221 RemoveMemoryMapEntry (Entry);\r
222\r
223 } else if (Entry->Start == End + 1) {\r
224\r
225 End = Entry->End;\r
226 RemoveMemoryMapEntry (Entry);\r
227 }\r
228 }\r
229\r
230 //\r
231 // Add descriptor\r
232 //\r
233\r
234 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
235 mMapStack[mMapDepth].FromPages = FALSE;\r
236 mMapStack[mMapDepth].Type = Type;\r
237 mMapStack[mMapDepth].Start = Start;\r
238 mMapStack[mMapDepth].End = End;\r
239 mMapStack[mMapDepth].VirtualStart = 0;\r
240 mMapStack[mMapDepth].Attribute = Attribute;\r
241 InsertTailList (&gMemoryMap, &mMapStack[mMapDepth].Link);\r
242\r
243 mMapDepth += 1;\r
244 ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
245\r
246 return ;\r
247}\r
022c6d45 248\r
162ed594 249/**\r
250 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.\r
251 If the list is emtry, then allocate a new page to refuel the list.\r
252 Please Note this algorithm to allocate the memory map descriptor has a property\r
253 that the memory allocated for memory entries always grows, and will never really be freed\r
254 For example, if the current boot uses 2000 memory map entries at the maximum point, but\r
255 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950\r
256 memory map entries is still allocated from EfiBootServicesMemory.\r
257\r
258\r
259 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList\r
260\r
261**/\r
28a00297 262MEMORY_MAP *\r
263AllocateMemoryMapEntry (\r
264 VOID\r
d9177625 265 )\r
266{\r
267 MEMORY_MAP* FreeDescriptorEntries;\r
268 MEMORY_MAP* Entry;\r
269 UINTN Index;\r
270\r
271 if (IsListEmpty (&mFreeMemoryMapEntryList)) {\r
272 //\r
273 // The list is empty, to allocate one page to refuel the list\r
274 //\r
275 FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION);\r
276 if(FreeDescriptorEntries != NULL) {\r
277 //\r
278 // Enque the free memmory map entries into the list\r
279 //\r
280 for (Index = 0; Index< DEFAULT_PAGE_ALLOCATION / sizeof(MEMORY_MAP); Index++) {\r
281 FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;\r
282 InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);\r
283 }\r
284 } else {\r
285 return NULL;\r
286 }\r
287 }\r
288 //\r
289 // dequeue the first descriptor from the list\r
290 //\r
291 Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
292 RemoveEntryList (&Entry->Link);\r
293\r
294 return Entry;\r
295}\r
022c6d45 296\r
162ed594 297\r
298/**\r
d9177625 299 Internal function. Moves any memory descriptors that are on the\r
300 temporary descriptor stack to heap.\r
162ed594 301\r
302**/\r
28a00297 303VOID\r
d9177625 304CoreFreeMemoryMapStack (\r
28a00297 305 VOID\r
306 )\r
28a00297 307{\r
d9177625 308 MEMORY_MAP *Entry;\r
309 MEMORY_MAP *Entry2;\r
310 LIST_ENTRY *Link2;\r
28a00297 311\r
d9177625 312 ASSERT_LOCKED (&gMemoryLock);\r
28a00297 313\r
d9177625 314 //\r
315 // If already freeing the map stack, then return\r
316 //\r
317 if (mFreeMapStack != 0) {\r
318 return ;\r
319 }\r
162ed594 320\r
d9177625 321 //\r
322 // Move the temporary memory descriptor stack into pool\r
323 //\r
324 mFreeMapStack += 1;\r
162ed594 325\r
d9177625 326 while (mMapDepth != 0) {\r
327 //\r
328 // Deque an memory map entry from mFreeMemoryMapEntryList\r
329 //\r
330 Entry = AllocateMemoryMapEntry ();\r
331\r
332 ASSERT (Entry);\r
333\r
334 //\r
335 // Update to proper entry\r
336 //\r
337 mMapDepth -= 1;\r
338\r
339 if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {\r
340\r
341 //\r
342 // Move this entry to general memory\r
343 //\r
344 RemoveEntryList (&mMapStack[mMapDepth].Link);\r
345 mMapStack[mMapDepth].Link.ForwardLink = NULL;\r
346\r
347 CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));\r
348 Entry->FromPages = TRUE;\r
349\r
350 //\r
351 // Find insertion location\r
352 //\r
353 for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) {\r
354 Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
355 if (Entry2->FromPages && Entry2->Start > Entry->Start) {\r
356 break;\r
357 }\r
358 }\r
359\r
360 InsertTailList (Link2, &Entry->Link);\r
361\r
362 } else {\r
363 //\r
364 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,\r
365 // so here no need to move it to memory.\r
366 //\r
367 InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
368 }\r
369 }\r
28a00297 370\r
d9177625 371 mFreeMapStack -= 1;\r
372}\r
162ed594 373\r
374/**\r
375 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.\r
376\r
377**/\r
2345e7d4 378BOOLEAN\r
28a00297 379PromoteMemoryResource (\r
380 VOID\r
381 )\r
28a00297 382{\r
2345e7d4 383 LIST_ENTRY *Link;\r
384 EFI_GCD_MAP_ENTRY *Entry;\r
385 BOOLEAN Promoted;\r
28a00297 386\r
d45fd260 387 DEBUG ((DEBUG_PAGE, "Promote the memory resource\n"));\r
022c6d45 388\r
28a00297 389 CoreAcquireGcdMemoryLock ();\r
022c6d45 390\r
2345e7d4 391 Promoted = FALSE;\r
28a00297 392 Link = mGcdMemorySpaceMap.ForwardLink;\r
393 while (Link != &mGcdMemorySpaceMap) {\r
394\r
395 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
396\r
397 if (Entry->GcdMemoryType == EfiGcdMemoryTypeReserved &&\r
f3f2e05d 398 Entry->EndAddress < MAX_ADDRESS &&\r
28a00297 399 (Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r
400 (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) {\r
401 //\r
402 // Update the GCD map\r
403 //\r
404 Entry->GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r
405 Entry->Capabilities |= EFI_MEMORY_TESTED;\r
406 Entry->ImageHandle = gDxeCoreImageHandle;\r
407 Entry->DeviceHandle = NULL;\r
408\r
409 //\r
410 // Add to allocable system memory resource\r
022c6d45 411 //\r
28a00297 412\r
413 CoreAddRange (\r
022c6d45 414 EfiConventionalMemory,\r
415 Entry->BaseAddress,\r
416 Entry->EndAddress,\r
28a00297 417 Entry->Capabilities & ~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r
418 );\r
419 CoreFreeMemoryMapStack ();\r
022c6d45 420\r
2345e7d4 421 Promoted = TRUE;\r
28a00297 422 }\r
423\r
424 Link = Link->ForwardLink;\r
425 }\r
022c6d45 426\r
28a00297 427 CoreReleaseGcdMemoryLock ();\r
022c6d45 428\r
2345e7d4 429 return Promoted;\r
28a00297 430}\r
54ea99a7 431/**\r
432 This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD \r
433 PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the \r
434 size of boot time and runtime code.\r
28a00297 435\r
54ea99a7 436**/\r
437VOID\r
438CoreLoadingFixedAddressHook (\r
439 VOID\r
440 )\r
441{\r
442 UINT32 RuntimeCodePageNumber;\r
443 UINT32 BootTimeCodePageNumber;\r
444 EFI_PHYSICAL_ADDRESS RuntimeCodeBase;\r
445 EFI_PHYSICAL_ADDRESS BootTimeCodeBase;\r
446 EFI_STATUS Status;\r
447\r
448 //\r
449 // Make sure these 2 areas are not initialzied.\r
450 //\r
451 if (!gLoadFixedAddressCodeMemoryReady) { \r
452 RuntimeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
453 BootTimeCodePageNumber= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
454 RuntimeCodeBase = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - EFI_PAGES_TO_SIZE (RuntimeCodePageNumber));\r
455 BootTimeCodeBase = (EFI_PHYSICAL_ADDRESS)(RuntimeCodeBase - EFI_PAGES_TO_SIZE (BootTimeCodePageNumber));\r
456 //\r
457 // Try to allocate runtime memory.\r
458 //\r
459 Status = CoreAllocatePages (\r
460 AllocateAddress,\r
461 EfiRuntimeServicesCode,\r
462 RuntimeCodePageNumber,\r
463 &RuntimeCodeBase\r
464 );\r
465 if (EFI_ERROR(Status)) {\r
466 //\r
467 // Runtime memory allocation failed \r
468 //\r
469 return;\r
470 }\r
471 //\r
472 // Try to allocate boot memory.\r
473 //\r
474 Status = CoreAllocatePages (\r
475 AllocateAddress,\r
476 EfiBootServicesCode,\r
477 BootTimeCodePageNumber,\r
478 &BootTimeCodeBase\r
479 );\r
480 if (EFI_ERROR(Status)) {\r
481 //\r
482 // boot memory allocation failed. Free Runtime code range and will try the allocation again when \r
483 // new memory range is installed.\r
484 //\r
485 CoreFreePages (\r
486 RuntimeCodeBase,\r
487 RuntimeCodePageNumber\r
488 );\r
489 return;\r
490 }\r
491 gLoadFixedAddressCodeMemoryReady = TRUE;\r
492 } \r
493 return;\r
494} \r
28a00297 495\r
162ed594 496/**\r
28a00297 497 Called to initialize the memory map and add descriptors to\r
498 the current descriptor list.\r
28a00297 499 The first descriptor that is added must be general usable\r
500 memory as the addition allocates heap.\r
501\r
022c6d45 502 @param Type The type of memory to add\r
503 @param Start The starting address in the memory range Must be\r
504 page aligned\r
505 @param NumberOfPages The number of pages in the range\r
506 @param Attribute Attributes of the memory to add\r
28a00297 507\r
162ed594 508 @return None. The range is added to the memory map\r
28a00297 509\r
162ed594 510**/\r
511VOID\r
512CoreAddMemoryDescriptor (\r
513 IN EFI_MEMORY_TYPE Type,\r
514 IN EFI_PHYSICAL_ADDRESS Start,\r
515 IN UINT64 NumberOfPages,\r
516 IN UINT64 Attribute\r
517 )\r
28a00297 518{\r
519 EFI_PHYSICAL_ADDRESS End;\r
520 EFI_STATUS Status;\r
521 UINTN Index;\r
522 UINTN FreeIndex;\r
54ea99a7 523 \r
28a00297 524 if ((Start & EFI_PAGE_MASK) != 0) {\r
525 return;\r
526 }\r
527\r
528 if (Type >= EfiMaxMemoryType && Type <= 0x7fffffff) {\r
529 return;\r
530 }\r
28a00297 531 CoreAcquireMemoryLock ();\r
532 End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
533 CoreAddRange (Type, Start, End, Attribute);\r
534 CoreFreeMemoryMapStack ();\r
535 CoreReleaseMemoryLock ();\r
536\r
54ea99a7 537 //\r
538 // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type\r
539 //\r
852081fc 540 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
54ea99a7 541 CoreLoadingFixedAddressHook();\r
542 }\r
543 \r
28a00297 544 //\r
545 // Check to see if the statistics for the different memory types have already been established\r
546 //\r
547 if (mMemoryTypeInformationInitialized) {\r
548 return;\r
549 }\r
550\r
54ea99a7 551 \r
28a00297 552 //\r
553 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array\r
554 //\r
555 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
556 //\r
557 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
558 //\r
559 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
3d78c020 560 if ((UINT32)Type > EfiMaxMemoryType) {\r
28a00297 561 continue;\r
562 }\r
28a00297 563 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
564 //\r
565 // Allocate pages for the current memory type from the top of available memory\r
566 //\r
567 Status = CoreAllocatePages (\r
568 AllocateAnyPages,\r
569 Type,\r
570 gMemoryTypeInformation[Index].NumberOfPages,\r
571 &mMemoryTypeStatistics[Type].BaseAddress\r
572 );\r
573 if (EFI_ERROR (Status)) {\r
574 //\r
022c6d45 575 // If an error occurs allocating the pages for the current memory type, then\r
28a00297 576 // free all the pages allocates for the previous memory types and return. This\r
577 // operation with be retied when/if more memory is added to the system\r
578 //\r
579 for (FreeIndex = 0; FreeIndex < Index; FreeIndex++) {\r
580 //\r
581 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
582 //\r
583 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type);\r
3d78c020 584 if ((UINT32)Type > EfiMaxMemoryType) {\r
28a00297 585 continue;\r
586 }\r
587\r
588 if (gMemoryTypeInformation[FreeIndex].NumberOfPages != 0) {\r
589 CoreFreePages (\r
022c6d45 590 mMemoryTypeStatistics[Type].BaseAddress,\r
28a00297 591 gMemoryTypeInformation[FreeIndex].NumberOfPages\r
592 );\r
593 mMemoryTypeStatistics[Type].BaseAddress = 0;\r
f3f2e05d 594 mMemoryTypeStatistics[Type].MaximumAddress = MAX_ADDRESS;\r
28a00297 595 }\r
596 }\r
597 return;\r
598 }\r
599\r
600 //\r
601 // Compute the address at the top of the current statistics\r
602 //\r
022c6d45 603 mMemoryTypeStatistics[Type].MaximumAddress =\r
604 mMemoryTypeStatistics[Type].BaseAddress +\r
28a00297 605 LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
606\r
607 //\r
022c6d45 608 // If the current base address is the lowest address so far, then update the default\r
28a00297 609 // maximum address\r
610 //\r
611 if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) {\r
612 mDefaultMaximumAddress = mMemoryTypeStatistics[Type].BaseAddress - 1;\r
613 }\r
614 }\r
615 }\r
616\r
617 //\r
618 // There was enough system memory for all the the memory types were allocated. So,\r
619 // those memory areas can be freed for future allocations, and all future memory\r
620 // allocations can occur within their respective bins\r
621 //\r
622 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
623 //\r
624 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
625 //\r
626 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
3d78c020 627 if ((UINT32)Type > EfiMaxMemoryType) {\r
28a00297 628 continue;\r
629 }\r
28a00297 630 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
631 CoreFreePages (\r
022c6d45 632 mMemoryTypeStatistics[Type].BaseAddress,\r
28a00297 633 gMemoryTypeInformation[Index].NumberOfPages\r
634 );\r
b74350e9 635 mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages;\r
28a00297 636 gMemoryTypeInformation[Index].NumberOfPages = 0;\r
637 }\r
638 }\r
639\r
640 //\r
641 // If the number of pages reserved for a memory type is 0, then all allocations for that type\r
642 // should be in the default range.\r
643 //\r
644 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
645 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
646 if (Type == (EFI_MEMORY_TYPE)gMemoryTypeInformation[Index].Type) {\r
647 mMemoryTypeStatistics[Type].InformationIndex = Index;\r
648 }\r
649 }\r
650 mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0;\r
f3f2e05d 651 if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ADDRESS) {\r
28a00297 652 mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress;\r
653 }\r
654 }\r
655\r
656 mMemoryTypeInformationInitialized = TRUE;\r
657}\r
658\r
659\r
162ed594 660/**\r
661 Internal function. Converts a memory range to the specified type.\r
662 The range must exist in the memory map.\r
663\r
022c6d45 664 @param Start The first address of the range Must be page\r
665 aligned\r
666 @param NumberOfPages The number of pages to convert\r
667 @param NewType The new type for the memory range\r
162ed594 668\r
022c6d45 669 @retval EFI_INVALID_PARAMETER Invalid parameter\r
670 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r
671 range or convertion not allowed.\r
672 @retval EFI_SUCCESS Successfully converts the memory range to the\r
162ed594 673 specified type.\r
674\r
675**/\r
28a00297 676EFI_STATUS\r
677CoreConvertPages (\r
678 IN UINT64 Start,\r
679 IN UINT64 NumberOfPages,\r
680 IN EFI_MEMORY_TYPE NewType\r
681 )\r
28a00297 682{\r
683\r
684 UINT64 NumberOfBytes;\r
685 UINT64 End;\r
686 UINT64 RangeEnd;\r
687 UINT64 Attribute;\r
688 LIST_ENTRY *Link;\r
689 MEMORY_MAP *Entry;\r
690\r
691 Entry = NULL;\r
692 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
693 End = Start + NumberOfBytes - 1;\r
694\r
695 ASSERT (NumberOfPages);\r
696 ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
697 ASSERT (End > Start) ;\r
698 ASSERT_LOCKED (&gMemoryLock);\r
699\r
71f68914 700 if (NumberOfPages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start > (Start + NumberOfBytes))) {\r
28a00297 701 return EFI_INVALID_PARAMETER;\r
702 }\r
703\r
704 //\r
705 // Convert the entire range\r
706 //\r
707\r
708 while (Start < End) {\r
709\r
710 //\r
711 // Find the entry that the covers the range\r
712 //\r
713 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
714 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
715\r
716 if (Entry->Start <= Start && Entry->End > Start) {\r
717 break;\r
718 }\r
719 }\r
720\r
721 if (Link == &gMemoryMap) {\r
162ed594 722 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: failed to find range %lx - %lx\n", Start, End));\r
28a00297 723 return EFI_NOT_FOUND;\r
724 }\r
725\r
726 //\r
727 // Convert range to the end, or to the end of the descriptor\r
728 // if that's all we've got\r
729 //\r
730 RangeEnd = End;\r
525aded9 731\r
732 ASSERT (Entry != NULL);\r
28a00297 733 if (Entry->End < End) {\r
734 RangeEnd = Entry->End;\r
735 }\r
736\r
162ed594 737 DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to %d\n", Start, RangeEnd, NewType));\r
28a00297 738\r
739 //\r
740 // Debug code - verify conversion is allowed\r
741 //\r
742 if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) {\r
d45fd260 743 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types\n"));\r
28a00297 744 return EFI_NOT_FOUND;\r
022c6d45 745 }\r
28a00297 746\r
747 //\r
748 // Update counters for the number of pages allocated to each memory type\r
749 //\r
3d78c020 750 if ((UINT32)Entry->Type < EfiMaxMemoryType) {\r
2345e7d4 751 if ((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) ||\r
752 (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {\r
28a00297 753 if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {\r
754 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;\r
755 } else {\r
756 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;\r
757 }\r
758 }\r
759 }\r
760\r
3d78c020 761 if ((UINT32)NewType < EfiMaxMemoryType) {\r
2345e7d4 762 if ((Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) ||\r
763 (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {\r
28a00297 764 mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;\r
2345e7d4 765 if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {\r
28a00297 766 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;\r
767 }\r
768 }\r
769 }\r
770\r
771 //\r
772 // Pull range out of descriptor\r
773 //\r
774 if (Entry->Start == Start) {\r
022c6d45 775\r
28a00297 776 //\r
777 // Clip start\r
778 //\r
779 Entry->Start = RangeEnd + 1;\r
780\r
781 } else if (Entry->End == RangeEnd) {\r
022c6d45 782\r
28a00297 783 //\r
784 // Clip end\r
785 //\r
786 Entry->End = Start - 1;\r
787\r
788 } else {\r
789\r
790 //\r
791 // Pull it out of the center, clip current\r
792 //\r
022c6d45 793\r
28a00297 794 //\r
795 // Add a new one\r
796 //\r
797 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
798 mMapStack[mMapDepth].FromPages = FALSE;\r
799 mMapStack[mMapDepth].Type = Entry->Type;\r
800 mMapStack[mMapDepth].Start = RangeEnd+1;\r
801 mMapStack[mMapDepth].End = Entry->End;\r
802\r
803 //\r
804 // Inherit Attribute from the Memory Descriptor that is being clipped\r
805 //\r
806 mMapStack[mMapDepth].Attribute = Entry->Attribute;\r
807\r
808 Entry->End = Start - 1;\r
809 ASSERT (Entry->Start < Entry->End);\r
810\r
811 Entry = &mMapStack[mMapDepth];\r
812 InsertTailList (&gMemoryMap, &Entry->Link);\r
813\r
814 mMapDepth += 1;\r
815 ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
816 }\r
817\r
818 //\r
022c6d45 819 // The new range inherits the same Attribute as the Entry\r
28a00297 820 //it is being cut out of\r
821 //\r
822 Attribute = Entry->Attribute;\r
823\r
824 //\r
825 // If the descriptor is empty, then remove it from the map\r
826 //\r
827 if (Entry->Start == Entry->End + 1) {\r
828 RemoveMemoryMapEntry (Entry);\r
829 Entry = NULL;\r
830 }\r
022c6d45 831\r
28a00297 832 //\r
833 // Add our new range in\r
834 //\r
835 CoreAddRange (NewType, Start, RangeEnd, Attribute);\r
38c7df98 836 if (NewType == EfiConventionalMemory) {\r
b295dfd4 837 DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) Start, (UINTN) (RangeEnd - Start + 1));\r
38c7df98 838 }\r
28a00297 839\r
840 //\r
841 // Move any map descriptor stack to general pool\r
842 //\r
843 CoreFreeMemoryMapStack ();\r
844\r
845 //\r
846 // Bump the starting address, and convert the next range\r
847 //\r
848 Start = RangeEnd + 1;\r
849 }\r
850\r
851 //\r
852 // Converted the whole range, done\r
853 //\r
854\r
855 return EFI_SUCCESS;\r
856}\r
857\r
858\r
162ed594 859\r
860/**\r
861 Internal function. Finds a consecutive free page range below\r
862 the requested address.\r
863\r
022c6d45 864 @param MaxAddress The address that the range must be below\r
35f9e94e 865 @param MinAddress The address that the range must be above\r
022c6d45 866 @param NumberOfPages Number of pages needed\r
867 @param NewType The type of memory the range is going to be\r
868 turned into\r
869 @param Alignment Bits to align with\r
162ed594 870\r
871 @return The base address of the range, or 0 if the range was not found\r
872\r
873**/\r
28a00297 874UINT64\r
875CoreFindFreePagesI (\r
876 IN UINT64 MaxAddress,\r
2345e7d4 877 IN UINT64 MinAddress,\r
28a00297 878 IN UINT64 NumberOfPages,\r
879 IN EFI_MEMORY_TYPE NewType,\r
880 IN UINTN Alignment\r
881 )\r
28a00297 882{\r
883 UINT64 NumberOfBytes;\r
884 UINT64 Target;\r
885 UINT64 DescStart;\r
886 UINT64 DescEnd;\r
887 UINT64 DescNumberOfBytes;\r
888 LIST_ENTRY *Link;\r
889 MEMORY_MAP *Entry;\r
890\r
891 if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages == 0)) {\r
892 return 0;\r
893 }\r
894\r
895 if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {\r
022c6d45 896\r
28a00297 897 //\r
898 // If MaxAddress is not aligned to the end of a page\r
899 //\r
022c6d45 900\r
28a00297 901 //\r
902 // Change MaxAddress to be 1 page lower\r
903 //\r
904 MaxAddress -= (EFI_PAGE_MASK + 1);\r
022c6d45 905\r
28a00297 906 //\r
907 // Set MaxAddress to a page boundary\r
908 //\r
909 MaxAddress &= ~EFI_PAGE_MASK;\r
022c6d45 910\r
28a00297 911 //\r
912 // Set MaxAddress to end of the page\r
913 //\r
914 MaxAddress |= EFI_PAGE_MASK;\r
915 }\r
916\r
917 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
918 Target = 0;\r
919\r
920 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
921 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
022c6d45 922\r
28a00297 923 //\r
924 // If it's not a free entry, don't bother with it\r
925 //\r
926 if (Entry->Type != EfiConventionalMemory) {\r
927 continue;\r
928 }\r
929\r
930 DescStart = Entry->Start;\r
931 DescEnd = Entry->End;\r
932\r
933 //\r
2345e7d4 934 // If desc is past max allowed address or below min allowed address, skip it\r
28a00297 935 //\r
2345e7d4 936 if ((DescStart >= MaxAddress) || (DescEnd < MinAddress)) {\r
28a00297 937 continue;\r
938 }\r
939\r
940 //\r
941 // If desc ends past max allowed address, clip the end\r
942 //\r
943 if (DescEnd >= MaxAddress) {\r
944 DescEnd = MaxAddress;\r
945 }\r
946\r
947 DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;\r
948\r
949 //\r
022c6d45 950 // Compute the number of bytes we can used from this\r
28a00297 951 // descriptor, and see it's enough to satisfy the request\r
952 //\r
953 DescNumberOfBytes = DescEnd - DescStart + 1;\r
954\r
955 if (DescNumberOfBytes >= NumberOfBytes) {\r
2345e7d4 956 //\r
957 // If the start of the allocated range is below the min address allowed, skip it\r
958 //\r
959 if ((DescEnd - NumberOfBytes + 1) < MinAddress) {\r
960 continue;\r
961 }\r
28a00297 962\r
963 //\r
964 // If this is the best match so far remember it\r
965 //\r
966 if (DescEnd > Target) {\r
967 Target = DescEnd;\r
968 }\r
969 }\r
022c6d45 970 }\r
28a00297 971\r
972 //\r
973 // If this is a grow down, adjust target to be the allocation base\r
974 //\r
975 Target -= NumberOfBytes - 1;\r
976\r
977 //\r
978 // If we didn't find a match, return 0\r
979 //\r
980 if ((Target & EFI_PAGE_MASK) != 0) {\r
981 return 0;\r
982 }\r
983\r
984 return Target;\r
985}\r
986\r
162ed594 987\r
988/**\r
989 Internal function. Finds a consecutive free page range below\r
990 the requested address\r
991\r
022c6d45 992 @param MaxAddress The address that the range must be below\r
993 @param NoPages Number of pages needed\r
994 @param NewType The type of memory the range is going to be\r
995 turned into\r
996 @param Alignment Bits to align with\r
162ed594 997\r
998 @return The base address of the range, or 0 if the range was not found.\r
999\r
1000**/\r
28a00297 1001UINT64\r
1002FindFreePages (\r
1003 IN UINT64 MaxAddress,\r
1004 IN UINT64 NoPages,\r
1005 IN EFI_MEMORY_TYPE NewType,\r
1006 IN UINTN Alignment\r
1007 )\r
28a00297 1008{\r
2345e7d4 1009 UINT64 Start;\r
28a00297 1010\r
2345e7d4 1011 //\r
1012 // Attempt to find free pages in the preferred bin based on the requested memory type\r
1013 //\r
3d78c020 1014 if ((UINT32)NewType < EfiMaxMemoryType && MaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {\r
2345e7d4 1015 Start = CoreFindFreePagesI (\r
1016 mMemoryTypeStatistics[NewType].MaximumAddress, \r
1017 mMemoryTypeStatistics[NewType].BaseAddress, \r
1018 NoPages, \r
1019 NewType, \r
1020 Alignment\r
1021 );\r
1022 if (Start != 0) {\r
1023 return Start;\r
1024 }\r
1025 }\r
28a00297 1026\r
2345e7d4 1027 //\r
1028 // Attempt to find free pages in the default allocation bin\r
1029 //\r
1030 if (MaxAddress >= mDefaultMaximumAddress) {\r
1031 Start = CoreFindFreePagesI (mDefaultMaximumAddress, 0, NoPages, NewType, Alignment);\r
1032 if (Start != 0) {\r
1033 if (Start < mDefaultBaseAddress) {\r
1034 mDefaultBaseAddress = Start;\r
1035 }\r
1036 return Start;\r
28a00297 1037 }\r
1038 }\r
1039\r
2345e7d4 1040 //\r
1041 // The allocation did not succeed in any of the prefered bins even after \r
1042 // promoting resources. Attempt to find free pages anywhere is the requested \r
1043 // address range. If this allocation fails, then there are not enough \r
1044 // resources anywhere to satisfy the request.\r
1045 //\r
1046 Start = CoreFindFreePagesI (MaxAddress, 0, NoPages, NewType, Alignment);\r
1047 if (Start != 0) {\r
1048 return Start;\r
1049 }\r
28a00297 1050\r
2345e7d4 1051 //\r
1052 // If allocations from the preferred bins fail, then attempt to promote memory resources.\r
1053 //\r
1054 if (!PromoteMemoryResource ()) {\r
1055 return 0;\r
28a00297 1056 }\r
1057\r
2345e7d4 1058 //\r
1059 // If any memory resources were promoted, then re-attempt the allocation\r
1060 //\r
1061 return FindFreePages (MaxAddress, NoPages, NewType, Alignment);\r
28a00297 1062}\r
1063\r
1064\r
162ed594 1065/**\r
1066 Allocates pages from the memory map.\r
1067\r
022c6d45 1068 @param Type The type of allocation to perform\r
1069 @param MemoryType The type of memory to turn the allocated pages\r
1070 into\r
1071 @param NumberOfPages The number of pages to allocate\r
1072 @param Memory A pointer to receive the base allocated memory\r
1073 address\r
162ed594 1074\r
1075 @return Status. On success, Memory is filled in with the base address allocated\r
022c6d45 1076 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in\r
1077 spec.\r
1078 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r
1079 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r
162ed594 1080 @retval EFI_SUCCESS Pages successfully allocated.\r
1081\r
1082**/\r
28a00297 1083EFI_STATUS\r
1084EFIAPI\r
1085CoreAllocatePages (\r
1086 IN EFI_ALLOCATE_TYPE Type,\r
1087 IN EFI_MEMORY_TYPE MemoryType,\r
1088 IN UINTN NumberOfPages,\r
1089 IN OUT EFI_PHYSICAL_ADDRESS *Memory\r
1090 )\r
28a00297 1091{\r
1092 EFI_STATUS Status;\r
1093 UINT64 Start;\r
1094 UINT64 MaxAddress;\r
1095 UINTN Alignment;\r
1096\r
3d78c020 1097 if ((UINT32)Type >= MaxAllocateType) {\r
28a00297 1098 return EFI_INVALID_PARAMETER;\r
1099 }\r
1100\r
1101 if ((MemoryType >= EfiMaxMemoryType && MemoryType <= 0x7fffffff) ||\r
1102 MemoryType == EfiConventionalMemory) {\r
1103 return EFI_INVALID_PARAMETER;\r
1104 }\r
1105\r
3e058701
ED
1106 if (Memory == NULL) {\r
1107 return EFI_INVALID_PARAMETER;\r
1108 }\r
1109\r
28a00297 1110 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
1111\r
1112 if (MemoryType == EfiACPIReclaimMemory ||\r
1113 MemoryType == EfiACPIMemoryNVS ||\r
1114 MemoryType == EfiRuntimeServicesCode ||\r
1115 MemoryType == EfiRuntimeServicesData) {\r
1116\r
1117 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
1118 }\r
1119\r
1120 if (Type == AllocateAddress) {\r
1121 if ((*Memory & (Alignment - 1)) != 0) {\r
1122 return EFI_NOT_FOUND;\r
1123 }\r
1124 }\r
1125\r
1126 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1127 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1128\r
1129 //\r
022c6d45 1130 // If this is for below a particular address, then\r
28a00297 1131 //\r
1132 Start = *Memory;\r
022c6d45 1133\r
28a00297 1134 //\r
1135 // The max address is the max natively addressable address for the processor\r
1136 //\r
f3f2e05d 1137 MaxAddress = MAX_ADDRESS;\r
022c6d45 1138\r
28a00297 1139 if (Type == AllocateMaxAddress) {\r
1140 MaxAddress = Start;\r
1141 }\r
1142\r
1143 CoreAcquireMemoryLock ();\r
022c6d45 1144\r
28a00297 1145 //\r
1146 // If not a specific address, then find an address to allocate\r
1147 //\r
1148 if (Type != AllocateAddress) {\r
1149 Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment);\r
1150 if (Start == 0) {\r
1151 Status = EFI_OUT_OF_RESOURCES;\r
1152 goto Done;\r
1153 }\r
1154 }\r
1155\r
1156 //\r
1157 // Convert pages from FreeMemory to the requested type\r
1158 //\r
1159 Status = CoreConvertPages (Start, NumberOfPages, MemoryType);\r
1160\r
1161Done:\r
1162 CoreReleaseMemoryLock ();\r
1163\r
1164 if (!EFI_ERROR (Status)) {\r
1165 *Memory = Start;\r
1166 }\r
1167\r
1168 return Status;\r
1169}\r
1170\r
1171\r
162ed594 1172/**\r
1173 Frees previous allocated pages.\r
1174\r
022c6d45 1175 @param Memory Base address of memory being freed\r
1176 @param NumberOfPages The number of pages to free\r
162ed594 1177\r
022c6d45 1178 @retval EFI_NOT_FOUND Could not find the entry that covers the range\r
1179 @retval EFI_INVALID_PARAMETER Address not aligned\r
162ed594 1180 @return EFI_SUCCESS -Pages successfully freed.\r
1181\r
1182**/\r
022c6d45 1183EFI_STATUS\r
28a00297 1184EFIAPI\r
1185CoreFreePages (\r
1186 IN EFI_PHYSICAL_ADDRESS Memory,\r
1187 IN UINTN NumberOfPages\r
1188 )\r
28a00297 1189{\r
1190 EFI_STATUS Status;\r
1191 LIST_ENTRY *Link;\r
1192 MEMORY_MAP *Entry;\r
1193 UINTN Alignment;\r
1194\r
1195 //\r
1196 // Free the range\r
1197 //\r
1198 CoreAcquireMemoryLock ();\r
1199\r
1200 //\r
1201 // Find the entry that the covers the range\r
1202 //\r
1203 Entry = NULL;\r
1204 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1205 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1206 if (Entry->Start <= Memory && Entry->End > Memory) {\r
1207 break;\r
1208 }\r
1209 }\r
1210 if (Link == &gMemoryMap) {\r
a5ca8fa7 1211 Status = EFI_NOT_FOUND;\r
1212 goto Done;\r
28a00297 1213 }\r
1214\r
1215 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
1216\r
525aded9 1217 ASSERT (Entry != NULL);\r
28a00297 1218 if (Entry->Type == EfiACPIReclaimMemory ||\r
1219 Entry->Type == EfiACPIMemoryNVS ||\r
1220 Entry->Type == EfiRuntimeServicesCode ||\r
1221 Entry->Type == EfiRuntimeServicesData) {\r
1222\r
1223 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
1224\r
1225 }\r
1226\r
1227 if ((Memory & (Alignment - 1)) != 0) {\r
a5ca8fa7 1228 Status = EFI_INVALID_PARAMETER;\r
1229 goto Done;\r
28a00297 1230 }\r
1231\r
1232 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1233 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1234\r
1235 Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
1236\r
28a00297 1237 if (EFI_ERROR (Status)) {\r
a5ca8fa7 1238 goto Done;\r
28a00297 1239 }\r
1240\r
a5ca8fa7 1241Done:\r
1242 CoreReleaseMemoryLock ();\r
28a00297 1243 return Status;\r
1244}\r
1245\r
2345e7d4 1246/**\r
1247 This function checks to see if the last memory map descriptor in a memory map\r
1248 can be merged with any of the other memory map descriptors in a memorymap.\r
1249 Memory descriptors may be merged if they are adjacent and have the same type\r
1250 and attributes.\r
1251\r
1252 @param MemoryMap A pointer to the start of the memory map.\r
1253 @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.\r
1254 @param DescriptorSize The size, in bytes, of an individual\r
1255 EFI_MEMORY_DESCRIPTOR.\r
1256\r
1257 @return A pointer to the next available descriptor in MemoryMap\r
1258\r
1259**/\r
1260EFI_MEMORY_DESCRIPTOR *\r
1261MergeMemoryMapDescriptor (\r
1262 IN EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
1263 IN EFI_MEMORY_DESCRIPTOR *MemoryMapDescriptor,\r
1264 IN UINTN DescriptorSize\r
1265 )\r
1266{\r
1267 //\r
1268 // Traverse the array of descriptors in MemoryMap\r
1269 //\r
1270 for (; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) {\r
1271 //\r
1272 // Check to see if the Type fields are identical.\r
1273 //\r
1274 if (MemoryMap->Type != MemoryMapDescriptor->Type) {\r
1275 continue;\r
1276 }\r
1277\r
1278 //\r
1279 // Check to see if the Attribute fields are identical.\r
1280 //\r
1281 if (MemoryMap->Attribute != MemoryMapDescriptor->Attribute) {\r
1282 continue;\r
1283 }\r
1284\r
1285 //\r
1286 // Check to see if MemoryMapDescriptor is immediately above MemoryMap\r
1287 //\r
1288 if (MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) { \r
1289 //\r
1290 // Merge MemoryMapDescriptor into MemoryMap\r
1291 //\r
1292 MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r
1293\r
1294 //\r
1295 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r
1296 //\r
1297 return MemoryMapDescriptor;\r
1298 }\r
1299\r
1300 //\r
1301 // Check to see if MemoryMapDescriptor is immediately below MemoryMap\r
1302 //\r
1303 if (MemoryMap->PhysicalStart - EFI_PAGES_TO_SIZE ((UINTN)MemoryMapDescriptor->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {\r
1304 //\r
1305 // Merge MemoryMapDescriptor into MemoryMap\r
1306 //\r
1307 MemoryMap->PhysicalStart = MemoryMapDescriptor->PhysicalStart;\r
1308 MemoryMap->VirtualStart = MemoryMapDescriptor->VirtualStart;\r
1309 MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r
1310\r
1311 //\r
1312 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r
1313 //\r
1314 return MemoryMapDescriptor;\r
1315 }\r
1316 }\r
1317\r
1318 //\r
1319 // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.\r
1320 //\r
1321 // Return the slot immediately after MemoryMapDescriptor as the next available \r
1322 // slot in the MemoryMap array\r
1323 //\r
1324 return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor, DescriptorSize);\r
1325}\r
28a00297 1326\r
162ed594 1327/**\r
1328 This function returns a copy of the current memory map. The map is an array of\r
1329 memory descriptors, each of which describes a contiguous block of memory.\r
1330\r
022c6d45 1331 @param MemoryMapSize A pointer to the size, in bytes, of the\r
1332 MemoryMap buffer. On input, this is the size of\r
1333 the buffer allocated by the caller. On output,\r
1334 it is the size of the buffer returned by the\r
1335 firmware if the buffer was large enough, or the\r
1336 size of the buffer needed to contain the map if\r
1337 the buffer was too small.\r
1338 @param MemoryMap A pointer to the buffer in which firmware places\r
1339 the current memory map.\r
1340 @param MapKey A pointer to the location in which firmware\r
1341 returns the key for the current memory map.\r
1342 @param DescriptorSize A pointer to the location in which firmware\r
1343 returns the size, in bytes, of an individual\r
1344 EFI_MEMORY_DESCRIPTOR.\r
1345 @param DescriptorVersion A pointer to the location in which firmware\r
1346 returns the version number associated with the\r
1347 EFI_MEMORY_DESCRIPTOR.\r
1348\r
1349 @retval EFI_SUCCESS The memory map was returned in the MemoryMap\r
1350 buffer.\r
1351 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current\r
1352 buffer size needed to hold the memory map is\r
1353 returned in MemoryMapSize.\r
162ed594 1354 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
1355\r
1356**/\r
28a00297 1357EFI_STATUS\r
1358EFIAPI\r
1359CoreGetMemoryMap (\r
1360 IN OUT UINTN *MemoryMapSize,\r
1361 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
1362 OUT UINTN *MapKey,\r
1363 OUT UINTN *DescriptorSize,\r
1364 OUT UINT32 *DescriptorVersion\r
1365 )\r
28a00297 1366{\r
1367 EFI_STATUS Status;\r
022c6d45 1368 UINTN Size;\r
1369 UINTN BufferSize;\r
28a00297 1370 UINTN NumberOfRuntimeEntries;\r
1371 LIST_ENTRY *Link;\r
022c6d45 1372 MEMORY_MAP *Entry;\r
1373 EFI_GCD_MAP_ENTRY *GcdMapEntry;\r
b74350e9 1374 EFI_MEMORY_TYPE Type;\r
2345e7d4 1375 EFI_MEMORY_DESCRIPTOR *MemoryMapStart;\r
28a00297 1376\r
1377 //\r
1378 // Make sure the parameters are valid\r
1379 //\r
1380 if (MemoryMapSize == NULL) {\r
1381 return EFI_INVALID_PARAMETER;\r
1382 }\r
022c6d45 1383\r
28a00297 1384 CoreAcquireGcdMemoryLock ();\r
022c6d45 1385\r
28a00297 1386 //\r
1387 // Count the number of Reserved and MMIO entries that are marked for runtime use\r
1388 //\r
1389 NumberOfRuntimeEntries = 0;\r
1390 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
1391 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
1392 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1393 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {\r
1394 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
1395 NumberOfRuntimeEntries++;\r
1396 }\r
1397 }\r
1398 }\r
1399\r
1400 Size = sizeof (EFI_MEMORY_DESCRIPTOR);\r
1401\r
1402 //\r
1403 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will\r
1404 // prevent people from having pointer math bugs in their code.\r
1405 // now you have to use *DescriptorSize to make things work.\r
1406 //\r
1407 Size += sizeof(UINT64) - (Size % sizeof (UINT64));\r
1408\r
1409 if (DescriptorSize != NULL) {\r
1410 *DescriptorSize = Size;\r
1411 }\r
022c6d45 1412\r
28a00297 1413 if (DescriptorVersion != NULL) {\r
1414 *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;\r
1415 }\r
1416\r
1417 CoreAcquireMemoryLock ();\r
1418\r
1419 //\r
1420 // Compute the buffer size needed to fit the entire map\r
1421 //\r
1422 BufferSize = Size * NumberOfRuntimeEntries;\r
1423 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1424 BufferSize += Size;\r
1425 }\r
1426\r
1427 if (*MemoryMapSize < BufferSize) {\r
1428 Status = EFI_BUFFER_TOO_SMALL;\r
1429 goto Done;\r
1430 }\r
1431\r
1432 if (MemoryMap == NULL) {\r
1433 Status = EFI_INVALID_PARAMETER;\r
1434 goto Done;\r
1435 }\r
1436\r
1437 //\r
1438 // Build the map\r
1439 //\r
383c303c 1440 ZeroMem (MemoryMap, BufferSize);\r
2345e7d4 1441 MemoryMapStart = MemoryMap;\r
28a00297 1442 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1443 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1444 ASSERT (Entry->VirtualStart == 0);\r
1445\r
b74350e9 1446 //\r
1447 // Convert internal map into an EFI_MEMORY_DESCRIPTOR\r
1448 //\r
28a00297 1449 MemoryMap->Type = Entry->Type;\r
1450 MemoryMap->PhysicalStart = Entry->Start;\r
1451 MemoryMap->VirtualStart = Entry->VirtualStart;\r
1452 MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);\r
b74350e9 1453 //\r
1454 // If the memory type is EfiConventionalMemory, then determine if the range is part of a\r
022c6d45 1455 // memory type bin and needs to be converted to the same memory type as the rest of the\r
1456 // memory type bin in order to minimize EFI Memory Map changes across reboots. This\r
b74350e9 1457 // improves the chances for a successful S4 resume in the presence of minor page allocation\r
1458 // differences across reboots.\r
1459 //\r
1460 if (MemoryMap->Type == EfiConventionalMemory) {\r
1461 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
1462 if (mMemoryTypeStatistics[Type].Special &&\r
1463 mMemoryTypeStatistics[Type].NumberOfPages > 0 &&\r
1464 Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress &&\r
e94a9ff7 1465 Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) {\r
b74350e9 1466 MemoryMap->Type = Type;\r
1467 }\r
1468 }\r
1469 }\r
1470 MemoryMap->Attribute = Entry->Attribute;\r
10fe0d81
RN
1471 if (MemoryMap->Type < EfiMaxMemoryType) {\r
1472 if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {\r
1473 MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;\r
1474 }\r
28a00297 1475 }\r
022c6d45 1476\r
2345e7d4 1477 //\r
1478 // Check to see if the new Memory Map Descriptor can be merged with an \r
1479 // existing descriptor if they are adjacent and have the same attributes\r
1480 //\r
1481 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
28a00297 1482 }\r
1483\r
1484 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
1485 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
1486 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1487 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {\r
1488 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
dc8d93ca 1489 // \r
1490 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries\r
1491 // that are marked for runtime use\r
1492 //\r
28a00297 1493 MemoryMap->PhysicalStart = GcdMapEntry->BaseAddress;\r
1494 MemoryMap->VirtualStart = 0;\r
1495 MemoryMap->NumberOfPages = RShiftU64 ((GcdMapEntry->EndAddress - GcdMapEntry->BaseAddress + 1), EFI_PAGE_SHIFT);\r
1496 MemoryMap->Attribute = GcdMapEntry->Attributes & ~EFI_MEMORY_PORT_IO;\r
1497\r
1498 if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) {\r
1499 MemoryMap->Type = EfiReservedMemoryType;\r
1500 } else if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {\r
1501 if ((GcdMapEntry->Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {\r
1502 MemoryMap->Type = EfiMemoryMappedIOPortSpace;\r
1503 } else {\r
1504 MemoryMap->Type = EfiMemoryMappedIO;\r
1505 }\r
1506 }\r
1507\r
2345e7d4 1508 //\r
1509 // Check to see if the new Memory Map Descriptor can be merged with an \r
1510 // existing descriptor if they are adjacent and have the same attributes\r
1511 //\r
1512 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
28a00297 1513 }\r
1514 }\r
1515 }\r
022c6d45 1516\r
2345e7d4 1517 //\r
1518 // Compute the size of the buffer actually used after all memory map descriptor merge operations\r
1519 //\r
1520 BufferSize = ((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart);\r
1521\r
28a00297 1522 Status = EFI_SUCCESS;\r
1523\r
1524Done:\r
022c6d45 1525 //\r
1526 // Update the map key finally\r
1527 //\r
28a00297 1528 if (MapKey != NULL) {\r
1529 *MapKey = mMemoryMapKey;\r
1530 }\r
022c6d45 1531\r
e439df50 1532 CoreReleaseMemoryLock ();\r
1533\r
1534 CoreReleaseGcdMemoryLock ();\r
1535\r
28a00297 1536 *MemoryMapSize = BufferSize;\r
022c6d45 1537\r
28a00297 1538 return Status;\r
1539}\r
1540\r
28a00297 1541\r
162ed594 1542/**\r
28a00297 1543 Internal function. Used by the pool functions to allocate pages\r
1544 to back pool allocation requests.\r
1545\r
022c6d45 1546 @param PoolType The type of memory for the new pool pages\r
1547 @param NumberOfPages No of pages to allocate\r
1548 @param Alignment Bits to align.\r
28a00297 1549\r
162ed594 1550 @return The allocated memory, or NULL\r
28a00297 1551\r
162ed594 1552**/\r
1553VOID *\r
1554CoreAllocatePoolPages (\r
1555 IN EFI_MEMORY_TYPE PoolType,\r
1556 IN UINTN NumberOfPages,\r
1557 IN UINTN Alignment\r
1558 )\r
28a00297 1559{\r
1560 UINT64 Start;\r
1561\r
1562 //\r
1563 // Find the pages to convert\r
1564 //\r
f3f2e05d 1565 Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment);\r
28a00297 1566\r
1567 //\r
1568 // Convert it to boot services data\r
1569 //\r
1570 if (Start == 0) {\r
7df7393f 1571 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32)NumberOfPages));\r
28a00297 1572 } else {\r
1573 CoreConvertPages (Start, NumberOfPages, PoolType);\r
1574 }\r
1575\r
e94a9ff7 1576 return (VOID *)(UINTN) Start;\r
28a00297 1577}\r
1578\r
162ed594 1579\r
1580/**\r
1581 Internal function. Frees pool pages allocated via AllocatePoolPages ()\r
1582\r
022c6d45 1583 @param Memory The base address to free\r
162ed594 1584 @param NumberOfPages The number of pages to free\r
1585\r
1586**/\r
28a00297 1587VOID\r
1588CoreFreePoolPages (\r
1589 IN EFI_PHYSICAL_ADDRESS Memory,\r
1590 IN UINTN NumberOfPages\r
1591 )\r
28a00297 1592{\r
1593 CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
1594}\r
1595\r
1596\r
28a00297 1597\r
162ed594 1598/**\r
1599 Make sure the memory map is following all the construction rules,\r
28a00297 1600 it is the last time to check memory map error before exit boot services.\r
1601\r
022c6d45 1602 @param MapKey Memory map key\r
28a00297 1603\r
022c6d45 1604 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction\r
1605 rules.\r
162ed594 1606 @retval EFI_SUCCESS Valid memory map.\r
28a00297 1607\r
162ed594 1608**/\r
1609EFI_STATUS\r
1610CoreTerminateMemoryMap (\r
1611 IN UINTN MapKey\r
1612 )\r
28a00297 1613{\r
1614 EFI_STATUS Status;\r
1615 LIST_ENTRY *Link;\r
1616 MEMORY_MAP *Entry;\r
1617\r
1618 Status = EFI_SUCCESS;\r
1619\r
1620 CoreAcquireMemoryLock ();\r
1621\r
1622 if (MapKey == mMemoryMapKey) {\r
1623\r
1624 //\r
1625 // Make sure the memory map is following all the construction rules\r
1626 // This is the last chance we will be able to display any messages on\r
1627 // the console devices.\r
1628 //\r
1629\r
1630 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1631 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
6e536468 1632 if ((Entry->Attribute & EFI_MEMORY_RUNTIME) != 0) {\r
28a00297 1633 if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS) {\r
d45fd260 1634 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));\r
1635 Status = EFI_INVALID_PARAMETER;\r
1636 goto Done;\r
28a00297 1637 }\r
6e536468 1638 if ((Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
d45fd260 1639 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
1640 Status = EFI_INVALID_PARAMETER;\r
1641 goto Done;\r
28a00297 1642 }\r
6e536468 1643 if (((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
d45fd260 1644 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
1645 Status = EFI_INVALID_PARAMETER;\r
1646 goto Done;\r
28a00297 1647 }\r
1648 }\r
1649 }\r
1650\r
1651 //\r
1652 // The map key they gave us matches what we expect. Fall through and\r
1653 // return success. In an ideal world we would clear out all of\r
1654 // EfiBootServicesCode and EfiBootServicesData. However this function\r
1655 // is not the last one called by ExitBootServices(), so we have to\r
1656 // preserve the memory contents.\r
1657 //\r
1658 } else {\r
1659 Status = EFI_INVALID_PARAMETER;\r
1660 }\r
1661\r
d45fd260 1662Done:\r
28a00297 1663 CoreReleaseMemoryLock ();\r
1664\r
1665 return Status;\r
1666}\r
1667\r
1668\r
1669\r
1670\r
1671\r
1672\r
1673\r
1674\r
162ed594 1675\r