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