]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Mem/Page.c
MdeModulePkg/DxeCore: switch to MdePkg allocation granularity macros
[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 // Convert range to the end, or to the end of the descriptor
759 // if that's all we've got
760 //
761 RangeEnd = End;
762
763 ASSERT (Entry != NULL);
764 if (Entry->End < End) {
765 RangeEnd = Entry->End;
766 }
767
768 if (ChangingType) {
769 DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to type %d\n", Start, RangeEnd, NewType));
770 }
771 if (ChangingAttributes) {
772 DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to attr %lx\n", Start, RangeEnd, NewAttributes));
773 }
774
775 if (ChangingType) {
776 //
777 // Debug code - verify conversion is allowed
778 //
779 if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) {
780 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types\n"));
781 return EFI_NOT_FOUND;
782 }
783
784 //
785 // Update counters for the number of pages allocated to each memory type
786 //
787 if ((UINT32)Entry->Type < EfiMaxMemoryType) {
788 if ((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) ||
789 (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {
790 if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {
791 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;
792 } else {
793 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;
794 }
795 }
796 }
797
798 if ((UINT32)NewType < EfiMaxMemoryType) {
799 if ((Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) ||
800 (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {
801 mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;
802 if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {
803 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;
804 }
805 }
806 }
807 }
808
809 //
810 // Pull range out of descriptor
811 //
812 if (Entry->Start == Start) {
813
814 //
815 // Clip start
816 //
817 Entry->Start = RangeEnd + 1;
818
819 } else if (Entry->End == RangeEnd) {
820
821 //
822 // Clip end
823 //
824 Entry->End = Start - 1;
825
826 } else {
827
828 //
829 // Pull it out of the center, clip current
830 //
831
832 //
833 // Add a new one
834 //
835 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;
836 mMapStack[mMapDepth].FromPages = FALSE;
837 mMapStack[mMapDepth].Type = Entry->Type;
838 mMapStack[mMapDepth].Start = RangeEnd+1;
839 mMapStack[mMapDepth].End = Entry->End;
840
841 //
842 // Inherit Attribute from the Memory Descriptor that is being clipped
843 //
844 mMapStack[mMapDepth].Attribute = Entry->Attribute;
845
846 Entry->End = Start - 1;
847 ASSERT (Entry->Start < Entry->End);
848
849 Entry = &mMapStack[mMapDepth];
850 InsertTailList (&gMemoryMap, &Entry->Link);
851
852 mMapDepth += 1;
853 ASSERT (mMapDepth < MAX_MAP_DEPTH);
854 }
855
856 //
857 // The new range inherits the same Attribute as the Entry
858 // it is being cut out of unless attributes are being changed
859 //
860 if (ChangingType) {
861 Attribute = Entry->Attribute;
862 MemType = NewType;
863 } else {
864 Attribute = NewAttributes;
865 MemType = Entry->Type;
866 }
867
868 //
869 // If the descriptor is empty, then remove it from the map
870 //
871 if (Entry->Start == Entry->End + 1) {
872 RemoveMemoryMapEntry (Entry);
873 Entry = NULL;
874 }
875
876 //
877 // Add our new range in
878 //
879 CoreAddRange (MemType, Start, RangeEnd, Attribute);
880 if (ChangingType && (MemType == EfiConventionalMemory)) {
881 //
882 // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this
883 // macro will ASSERT() if address is 0. Instead, CoreAddRange() guarantees
884 // that the page starting at address 0 is always filled with zeros.
885 //
886 if (Start == 0) {
887 if (RangeEnd > EFI_PAGE_SIZE) {
888 DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) EFI_PAGE_SIZE, (UINTN) (RangeEnd - EFI_PAGE_SIZE + 1));
889 }
890 } else {
891 DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) Start, (UINTN) (RangeEnd - Start + 1));
892 }
893 }
894
895 //
896 // Move any map descriptor stack to general pool
897 //
898 CoreFreeMemoryMapStack ();
899
900 //
901 // Bump the starting address, and convert the next range
902 //
903 Start = RangeEnd + 1;
904 }
905
906 //
907 // Converted the whole range, done
908 //
909
910 return EFI_SUCCESS;
911 }
912
913
914 /**
915 Internal function. Converts a memory range to the specified type.
916 The range must exist in the memory map.
917
918 @param Start The first address of the range Must be page
919 aligned
920 @param NumberOfPages The number of pages to convert
921 @param NewType The new type for the memory range
922
923 @retval EFI_INVALID_PARAMETER Invalid parameter
924 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
925 range or convertion not allowed.
926 @retval EFI_SUCCESS Successfully converts the memory range to the
927 specified type.
928
929 **/
930 EFI_STATUS
931 CoreConvertPages (
932 IN UINT64 Start,
933 IN UINT64 NumberOfPages,
934 IN EFI_MEMORY_TYPE NewType
935 )
936 {
937 return CoreConvertPagesEx(Start, NumberOfPages, TRUE, NewType, FALSE, 0);
938 }
939
940
941 /**
942 Internal function. Converts a memory range to use new attributes.
943
944 @param Start The first address of the range Must be page
945 aligned
946 @param NumberOfPages The number of pages to convert
947 @param NewAttributes The new attributes value for the range.
948
949 **/
950 VOID
951 CoreUpdateMemoryAttributes (
952 IN EFI_PHYSICAL_ADDRESS Start,
953 IN UINT64 NumberOfPages,
954 IN UINT64 NewAttributes
955 )
956 {
957 CoreAcquireMemoryLock ();
958
959 //
960 // Update the attributes to the new value
961 //
962 CoreConvertPagesEx(Start, NumberOfPages, FALSE, (EFI_MEMORY_TYPE)0, TRUE, NewAttributes);
963
964 CoreReleaseMemoryLock ();
965 }
966
967
968 /**
969 Internal function. Finds a consecutive free page range below
970 the requested address.
971
972 @param MaxAddress The address that the range must be below
973 @param MinAddress The address that the range must be above
974 @param NumberOfPages Number of pages needed
975 @param NewType The type of memory the range is going to be
976 turned into
977 @param Alignment Bits to align with
978
979 @return The base address of the range, or 0 if the range was not found
980
981 **/
982 UINT64
983 CoreFindFreePagesI (
984 IN UINT64 MaxAddress,
985 IN UINT64 MinAddress,
986 IN UINT64 NumberOfPages,
987 IN EFI_MEMORY_TYPE NewType,
988 IN UINTN Alignment
989 )
990 {
991 UINT64 NumberOfBytes;
992 UINT64 Target;
993 UINT64 DescStart;
994 UINT64 DescEnd;
995 UINT64 DescNumberOfBytes;
996 LIST_ENTRY *Link;
997 MEMORY_MAP *Entry;
998
999 if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages == 0)) {
1000 return 0;
1001 }
1002
1003 if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {
1004
1005 //
1006 // If MaxAddress is not aligned to the end of a page
1007 //
1008
1009 //
1010 // Change MaxAddress to be 1 page lower
1011 //
1012 MaxAddress -= (EFI_PAGE_MASK + 1);
1013
1014 //
1015 // Set MaxAddress to a page boundary
1016 //
1017 MaxAddress &= ~(UINT64)EFI_PAGE_MASK;
1018
1019 //
1020 // Set MaxAddress to end of the page
1021 //
1022 MaxAddress |= EFI_PAGE_MASK;
1023 }
1024
1025 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);
1026 Target = 0;
1027
1028 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1029 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1030
1031 //
1032 // If it's not a free entry, don't bother with it
1033 //
1034 if (Entry->Type != EfiConventionalMemory) {
1035 continue;
1036 }
1037
1038 DescStart = Entry->Start;
1039 DescEnd = Entry->End;
1040
1041 //
1042 // If desc is past max allowed address or below min allowed address, skip it
1043 //
1044 if ((DescStart >= MaxAddress) || (DescEnd < MinAddress)) {
1045 continue;
1046 }
1047
1048 //
1049 // If desc ends past max allowed address, clip the end
1050 //
1051 if (DescEnd >= MaxAddress) {
1052 DescEnd = MaxAddress;
1053 }
1054
1055 DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;
1056
1057 // Skip if DescEnd is less than DescStart after alignment clipping
1058 if (DescEnd < DescStart) {
1059 continue;
1060 }
1061
1062 //
1063 // Compute the number of bytes we can used from this
1064 // descriptor, and see it's enough to satisfy the request
1065 //
1066 DescNumberOfBytes = DescEnd - DescStart + 1;
1067
1068 if (DescNumberOfBytes >= NumberOfBytes) {
1069 //
1070 // If the start of the allocated range is below the min address allowed, skip it
1071 //
1072 if ((DescEnd - NumberOfBytes + 1) < MinAddress) {
1073 continue;
1074 }
1075
1076 //
1077 // If this is the best match so far remember it
1078 //
1079 if (DescEnd > Target) {
1080 Target = DescEnd;
1081 }
1082 }
1083 }
1084
1085 //
1086 // If this is a grow down, adjust target to be the allocation base
1087 //
1088 Target -= NumberOfBytes - 1;
1089
1090 //
1091 // If we didn't find a match, return 0
1092 //
1093 if ((Target & EFI_PAGE_MASK) != 0) {
1094 return 0;
1095 }
1096
1097 return Target;
1098 }
1099
1100
1101 /**
1102 Internal function. Finds a consecutive free page range below
1103 the requested address
1104
1105 @param MaxAddress The address that the range must be below
1106 @param NoPages Number of pages needed
1107 @param NewType The type of memory the range is going to be
1108 turned into
1109 @param Alignment Bits to align with
1110
1111 @return The base address of the range, or 0 if the range was not found.
1112
1113 **/
1114 UINT64
1115 FindFreePages (
1116 IN UINT64 MaxAddress,
1117 IN UINT64 NoPages,
1118 IN EFI_MEMORY_TYPE NewType,
1119 IN UINTN Alignment
1120 )
1121 {
1122 UINT64 Start;
1123
1124 //
1125 // Attempt to find free pages in the preferred bin based on the requested memory type
1126 //
1127 if ((UINT32)NewType < EfiMaxMemoryType && MaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {
1128 Start = CoreFindFreePagesI (
1129 mMemoryTypeStatistics[NewType].MaximumAddress,
1130 mMemoryTypeStatistics[NewType].BaseAddress,
1131 NoPages,
1132 NewType,
1133 Alignment
1134 );
1135 if (Start != 0) {
1136 return Start;
1137 }
1138 }
1139
1140 //
1141 // Attempt to find free pages in the default allocation bin
1142 //
1143 if (MaxAddress >= mDefaultMaximumAddress) {
1144 Start = CoreFindFreePagesI (mDefaultMaximumAddress, 0, NoPages, NewType, Alignment);
1145 if (Start != 0) {
1146 if (Start < mDefaultBaseAddress) {
1147 mDefaultBaseAddress = Start;
1148 }
1149 return Start;
1150 }
1151 }
1152
1153 //
1154 // The allocation did not succeed in any of the prefered bins even after
1155 // promoting resources. Attempt to find free pages anywhere is the requested
1156 // address range. If this allocation fails, then there are not enough
1157 // resources anywhere to satisfy the request.
1158 //
1159 Start = CoreFindFreePagesI (MaxAddress, 0, NoPages, NewType, Alignment);
1160 if (Start != 0) {
1161 return Start;
1162 }
1163
1164 //
1165 // If allocations from the preferred bins fail, then attempt to promote memory resources.
1166 //
1167 if (!PromoteMemoryResource ()) {
1168 return 0;
1169 }
1170
1171 //
1172 // If any memory resources were promoted, then re-attempt the allocation
1173 //
1174 return FindFreePages (MaxAddress, NoPages, NewType, Alignment);
1175 }
1176
1177
1178 /**
1179 Allocates pages from the memory map.
1180
1181 @param Type The type of allocation to perform
1182 @param MemoryType The type of memory to turn the allocated pages
1183 into
1184 @param NumberOfPages The number of pages to allocate
1185 @param Memory A pointer to receive the base allocated memory
1186 address
1187
1188 @return Status. On success, Memory is filled in with the base address allocated
1189 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1190 spec.
1191 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1192 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1193 @retval EFI_SUCCESS Pages successfully allocated.
1194
1195 **/
1196 EFI_STATUS
1197 EFIAPI
1198 CoreInternalAllocatePages (
1199 IN EFI_ALLOCATE_TYPE Type,
1200 IN EFI_MEMORY_TYPE MemoryType,
1201 IN UINTN NumberOfPages,
1202 IN OUT EFI_PHYSICAL_ADDRESS *Memory
1203 )
1204 {
1205 EFI_STATUS Status;
1206 UINT64 Start;
1207 UINT64 NumberOfBytes;
1208 UINT64 End;
1209 UINT64 MaxAddress;
1210 UINTN Alignment;
1211
1212 if ((UINT32)Type >= MaxAllocateType) {
1213 return EFI_INVALID_PARAMETER;
1214 }
1215
1216 if ((MemoryType >= EfiMaxMemoryType && MemoryType < MEMORY_TYPE_OEM_RESERVED_MIN) ||
1217 (MemoryType == EfiConventionalMemory) || (MemoryType == EfiPersistentMemory)) {
1218 return EFI_INVALID_PARAMETER;
1219 }
1220
1221 if (Memory == NULL) {
1222 return EFI_INVALID_PARAMETER;
1223 }
1224
1225 Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
1226
1227 if (MemoryType == EfiACPIReclaimMemory ||
1228 MemoryType == EfiACPIMemoryNVS ||
1229 MemoryType == EfiRuntimeServicesCode ||
1230 MemoryType == EfiRuntimeServicesData) {
1231
1232 Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
1233 }
1234
1235 if (Type == AllocateAddress) {
1236 if ((*Memory & (Alignment - 1)) != 0) {
1237 return EFI_NOT_FOUND;
1238 }
1239 }
1240
1241 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;
1242 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
1243
1244 //
1245 // If this is for below a particular address, then
1246 //
1247 Start = *Memory;
1248
1249 //
1250 // The max address is the max natively addressable address for the processor
1251 //
1252 MaxAddress = MAX_ADDRESS;
1253
1254 //
1255 // Check for Type AllocateAddress,
1256 // if NumberOfPages is 0 or
1257 // if (NumberOfPages << EFI_PAGE_SHIFT) is above MAX_ADDRESS or
1258 // if (Start + NumberOfBytes) rolls over 0 or
1259 // if Start is above MAX_ADDRESS or
1260 // if End is above MAX_ADDRESS,
1261 // return EFI_NOT_FOUND.
1262 //
1263 if (Type == AllocateAddress) {
1264 if ((NumberOfPages == 0) ||
1265 (NumberOfPages > RShiftU64 (MaxAddress, EFI_PAGE_SHIFT))) {
1266 return EFI_NOT_FOUND;
1267 }
1268 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);
1269 End = Start + NumberOfBytes - 1;
1270
1271 if ((Start >= End) ||
1272 (Start > MaxAddress) ||
1273 (End > MaxAddress)) {
1274 return EFI_NOT_FOUND;
1275 }
1276 }
1277
1278 if (Type == AllocateMaxAddress) {
1279 MaxAddress = Start;
1280 }
1281
1282 CoreAcquireMemoryLock ();
1283
1284 //
1285 // If not a specific address, then find an address to allocate
1286 //
1287 if (Type != AllocateAddress) {
1288 Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment);
1289 if (Start == 0) {
1290 Status = EFI_OUT_OF_RESOURCES;
1291 goto Done;
1292 }
1293 }
1294
1295 //
1296 // Convert pages from FreeMemory to the requested type
1297 //
1298 Status = CoreConvertPages (Start, NumberOfPages, MemoryType);
1299
1300 Done:
1301 CoreReleaseMemoryLock ();
1302
1303 if (!EFI_ERROR (Status)) {
1304 *Memory = Start;
1305 }
1306
1307 return Status;
1308 }
1309
1310 /**
1311 Allocates pages from the memory map.
1312
1313 @param Type The type of allocation to perform
1314 @param MemoryType The type of memory to turn the allocated pages
1315 into
1316 @param NumberOfPages The number of pages to allocate
1317 @param Memory A pointer to receive the base allocated memory
1318 address
1319
1320 @return Status. On success, Memory is filled in with the base address allocated
1321 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1322 spec.
1323 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1324 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1325 @retval EFI_SUCCESS Pages successfully allocated.
1326
1327 **/
1328 EFI_STATUS
1329 EFIAPI
1330 CoreAllocatePages (
1331 IN EFI_ALLOCATE_TYPE Type,
1332 IN EFI_MEMORY_TYPE MemoryType,
1333 IN UINTN NumberOfPages,
1334 OUT EFI_PHYSICAL_ADDRESS *Memory
1335 )
1336 {
1337 EFI_STATUS Status;
1338
1339 Status = CoreInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);
1340 if (!EFI_ERROR (Status)) {
1341 CoreUpdateProfile (
1342 (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),
1343 MemoryProfileActionAllocatePages,
1344 MemoryType,
1345 EFI_PAGES_TO_SIZE (NumberOfPages),
1346 (VOID *) (UINTN) *Memory,
1347 NULL
1348 );
1349 InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);
1350 ApplyMemoryProtectionPolicy (EfiConventionalMemory, MemoryType, *Memory,
1351 EFI_PAGES_TO_SIZE (NumberOfPages));
1352 }
1353 return Status;
1354 }
1355
1356 /**
1357 Frees previous allocated pages.
1358
1359 @param Memory Base address of memory being freed
1360 @param NumberOfPages The number of pages to free
1361 @param MemoryType Pointer to memory type
1362
1363 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1364 @retval EFI_INVALID_PARAMETER Address not aligned
1365 @return EFI_SUCCESS -Pages successfully freed.
1366
1367 **/
1368 EFI_STATUS
1369 EFIAPI
1370 CoreInternalFreePages (
1371 IN EFI_PHYSICAL_ADDRESS Memory,
1372 IN UINTN NumberOfPages,
1373 OUT EFI_MEMORY_TYPE *MemoryType OPTIONAL
1374 )
1375 {
1376 EFI_STATUS Status;
1377 LIST_ENTRY *Link;
1378 MEMORY_MAP *Entry;
1379 UINTN Alignment;
1380
1381 //
1382 // Free the range
1383 //
1384 CoreAcquireMemoryLock ();
1385
1386 //
1387 // Find the entry that the covers the range
1388 //
1389 Entry = NULL;
1390 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1391 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1392 if (Entry->Start <= Memory && Entry->End > Memory) {
1393 break;
1394 }
1395 }
1396 if (Link == &gMemoryMap) {
1397 Status = EFI_NOT_FOUND;
1398 goto Done;
1399 }
1400
1401 Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
1402
1403 ASSERT (Entry != NULL);
1404 if (Entry->Type == EfiACPIReclaimMemory ||
1405 Entry->Type == EfiACPIMemoryNVS ||
1406 Entry->Type == EfiRuntimeServicesCode ||
1407 Entry->Type == EfiRuntimeServicesData) {
1408
1409 Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
1410
1411 }
1412
1413 if ((Memory & (Alignment - 1)) != 0) {
1414 Status = EFI_INVALID_PARAMETER;
1415 goto Done;
1416 }
1417
1418 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;
1419 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
1420
1421 if (MemoryType != NULL) {
1422 *MemoryType = Entry->Type;
1423 }
1424
1425 Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);
1426
1427 if (EFI_ERROR (Status)) {
1428 goto Done;
1429 }
1430
1431 Done:
1432 CoreReleaseMemoryLock ();
1433 return Status;
1434 }
1435
1436 /**
1437 Frees previous allocated pages.
1438
1439 @param Memory Base address of memory being freed
1440 @param NumberOfPages The number of pages to free
1441
1442 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1443 @retval EFI_INVALID_PARAMETER Address not aligned
1444 @return EFI_SUCCESS -Pages successfully freed.
1445
1446 **/
1447 EFI_STATUS
1448 EFIAPI
1449 CoreFreePages (
1450 IN EFI_PHYSICAL_ADDRESS Memory,
1451 IN UINTN NumberOfPages
1452 )
1453 {
1454 EFI_STATUS Status;
1455 EFI_MEMORY_TYPE MemoryType;
1456
1457 Status = CoreInternalFreePages (Memory, NumberOfPages, &MemoryType);
1458 if (!EFI_ERROR (Status)) {
1459 CoreUpdateProfile (
1460 (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),
1461 MemoryProfileActionFreePages,
1462 MemoryType,
1463 EFI_PAGES_TO_SIZE (NumberOfPages),
1464 (VOID *) (UINTN) Memory,
1465 NULL
1466 );
1467 InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);
1468 ApplyMemoryProtectionPolicy (MemoryType, EfiConventionalMemory, Memory,
1469 EFI_PAGES_TO_SIZE (NumberOfPages));
1470 }
1471 return Status;
1472 }
1473
1474 /**
1475 This function checks to see if the last memory map descriptor in a memory map
1476 can be merged with any of the other memory map descriptors in a memorymap.
1477 Memory descriptors may be merged if they are adjacent and have the same type
1478 and attributes.
1479
1480 @param MemoryMap A pointer to the start of the memory map.
1481 @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.
1482 @param DescriptorSize The size, in bytes, of an individual
1483 EFI_MEMORY_DESCRIPTOR.
1484
1485 @return A pointer to the next available descriptor in MemoryMap
1486
1487 **/
1488 EFI_MEMORY_DESCRIPTOR *
1489 MergeMemoryMapDescriptor (
1490 IN EFI_MEMORY_DESCRIPTOR *MemoryMap,
1491 IN EFI_MEMORY_DESCRIPTOR *MemoryMapDescriptor,
1492 IN UINTN DescriptorSize
1493 )
1494 {
1495 //
1496 // Traverse the array of descriptors in MemoryMap
1497 //
1498 for (; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) {
1499 //
1500 // Check to see if the Type fields are identical.
1501 //
1502 if (MemoryMap->Type != MemoryMapDescriptor->Type) {
1503 continue;
1504 }
1505
1506 //
1507 // Check to see if the Attribute fields are identical.
1508 //
1509 if (MemoryMap->Attribute != MemoryMapDescriptor->Attribute) {
1510 continue;
1511 }
1512
1513 //
1514 // Check to see if MemoryMapDescriptor is immediately above MemoryMap
1515 //
1516 if (MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {
1517 //
1518 // Merge MemoryMapDescriptor into MemoryMap
1519 //
1520 MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;
1521
1522 //
1523 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1524 //
1525 return MemoryMapDescriptor;
1526 }
1527
1528 //
1529 // Check to see if MemoryMapDescriptor is immediately below MemoryMap
1530 //
1531 if (MemoryMap->PhysicalStart - EFI_PAGES_TO_SIZE ((UINTN)MemoryMapDescriptor->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {
1532 //
1533 // Merge MemoryMapDescriptor into MemoryMap
1534 //
1535 MemoryMap->PhysicalStart = MemoryMapDescriptor->PhysicalStart;
1536 MemoryMap->VirtualStart = MemoryMapDescriptor->VirtualStart;
1537 MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;
1538
1539 //
1540 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array
1541 //
1542 return MemoryMapDescriptor;
1543 }
1544 }
1545
1546 //
1547 // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.
1548 //
1549 // Return the slot immediately after MemoryMapDescriptor as the next available
1550 // slot in the MemoryMap array
1551 //
1552 return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor, DescriptorSize);
1553 }
1554
1555 /**
1556 This function returns a copy of the current memory map. The map is an array of
1557 memory descriptors, each of which describes a contiguous block of memory.
1558
1559 @param MemoryMapSize A pointer to the size, in bytes, of the
1560 MemoryMap buffer. On input, this is the size of
1561 the buffer allocated by the caller. On output,
1562 it is the size of the buffer returned by the
1563 firmware if the buffer was large enough, or the
1564 size of the buffer needed to contain the map if
1565 the buffer was too small.
1566 @param MemoryMap A pointer to the buffer in which firmware places
1567 the current memory map.
1568 @param MapKey A pointer to the location in which firmware
1569 returns the key for the current memory map.
1570 @param DescriptorSize A pointer to the location in which firmware
1571 returns the size, in bytes, of an individual
1572 EFI_MEMORY_DESCRIPTOR.
1573 @param DescriptorVersion A pointer to the location in which firmware
1574 returns the version number associated with the
1575 EFI_MEMORY_DESCRIPTOR.
1576
1577 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1578 buffer.
1579 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1580 buffer size needed to hold the memory map is
1581 returned in MemoryMapSize.
1582 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1583
1584 **/
1585 EFI_STATUS
1586 EFIAPI
1587 CoreGetMemoryMap (
1588 IN OUT UINTN *MemoryMapSize,
1589 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
1590 OUT UINTN *MapKey,
1591 OUT UINTN *DescriptorSize,
1592 OUT UINT32 *DescriptorVersion
1593 )
1594 {
1595 EFI_STATUS Status;
1596 UINTN Size;
1597 UINTN BufferSize;
1598 UINTN NumberOfEntries;
1599 LIST_ENTRY *Link;
1600 MEMORY_MAP *Entry;
1601 EFI_GCD_MAP_ENTRY *GcdMapEntry;
1602 EFI_GCD_MAP_ENTRY MergeGcdMapEntry;
1603 EFI_MEMORY_TYPE Type;
1604 EFI_MEMORY_DESCRIPTOR *MemoryMapStart;
1605
1606 //
1607 // Make sure the parameters are valid
1608 //
1609 if (MemoryMapSize == NULL) {
1610 return EFI_INVALID_PARAMETER;
1611 }
1612
1613 CoreAcquireGcdMemoryLock ();
1614
1615 //
1616 // Count the number of Reserved and runtime MMIO entries
1617 // And, count the number of Persistent entries.
1618 //
1619 NumberOfEntries = 0;
1620 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {
1621 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1622 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypePersistentMemory) ||
1623 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||
1624 ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&
1625 ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {
1626 NumberOfEntries ++;
1627 }
1628 }
1629
1630 Size = sizeof (EFI_MEMORY_DESCRIPTOR);
1631
1632 //
1633 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1634 // prevent people from having pointer math bugs in their code.
1635 // now you have to use *DescriptorSize to make things work.
1636 //
1637 Size += sizeof(UINT64) - (Size % sizeof (UINT64));
1638
1639 if (DescriptorSize != NULL) {
1640 *DescriptorSize = Size;
1641 }
1642
1643 if (DescriptorVersion != NULL) {
1644 *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;
1645 }
1646
1647 CoreAcquireMemoryLock ();
1648
1649 //
1650 // Compute the buffer size needed to fit the entire map
1651 //
1652 BufferSize = Size * NumberOfEntries;
1653 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1654 BufferSize += Size;
1655 }
1656
1657 if (*MemoryMapSize < BufferSize) {
1658 Status = EFI_BUFFER_TOO_SMALL;
1659 goto Done;
1660 }
1661
1662 if (MemoryMap == NULL) {
1663 Status = EFI_INVALID_PARAMETER;
1664 goto Done;
1665 }
1666
1667 //
1668 // Build the map
1669 //
1670 ZeroMem (MemoryMap, BufferSize);
1671 MemoryMapStart = MemoryMap;
1672 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1673 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1674 ASSERT (Entry->VirtualStart == 0);
1675
1676 //
1677 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1678 //
1679 MemoryMap->Type = Entry->Type;
1680 MemoryMap->PhysicalStart = Entry->Start;
1681 MemoryMap->VirtualStart = Entry->VirtualStart;
1682 MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);
1683 //
1684 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1685 // memory type bin and needs to be converted to the same memory type as the rest of the
1686 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1687 // improves the chances for a successful S4 resume in the presence of minor page allocation
1688 // differences across reboots.
1689 //
1690 if (MemoryMap->Type == EfiConventionalMemory) {
1691 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {
1692 if (mMemoryTypeStatistics[Type].Special &&
1693 mMemoryTypeStatistics[Type].NumberOfPages > 0 &&
1694 Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress &&
1695 Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) {
1696 MemoryMap->Type = Type;
1697 }
1698 }
1699 }
1700 MemoryMap->Attribute = Entry->Attribute;
1701 if (MemoryMap->Type < EfiMaxMemoryType) {
1702 if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {
1703 MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;
1704 }
1705 }
1706
1707 //
1708 // Check to see if the new Memory Map Descriptor can be merged with an
1709 // existing descriptor if they are adjacent and have the same attributes
1710 //
1711 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);
1712 }
1713
1714
1715 ZeroMem (&MergeGcdMapEntry, sizeof (MergeGcdMapEntry));
1716 GcdMapEntry = NULL;
1717 for (Link = mGcdMemorySpaceMap.ForwardLink; ; Link = Link->ForwardLink) {
1718 if (Link != &mGcdMemorySpaceMap) {
1719 //
1720 // Merge adjacent same type and attribute GCD memory range
1721 //
1722 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1723
1724 if ((MergeGcdMapEntry.Capabilities == GcdMapEntry->Capabilities) &&
1725 (MergeGcdMapEntry.Attributes == GcdMapEntry->Attributes) &&
1726 (MergeGcdMapEntry.GcdMemoryType == GcdMapEntry->GcdMemoryType) &&
1727 (MergeGcdMapEntry.GcdIoType == GcdMapEntry->GcdIoType)) {
1728 MergeGcdMapEntry.EndAddress = GcdMapEntry->EndAddress;
1729 continue;
1730 }
1731 }
1732
1733 if ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) ||
1734 ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&
1735 ((MergeGcdMapEntry.Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {
1736 //
1737 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR,
1738 // it will be recorded as page PhysicalStart and NumberOfPages.
1739 //
1740 ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);
1741 ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);
1742
1743 //
1744 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and runtime MMIO GCD entries
1745 //
1746 MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;
1747 MemoryMap->VirtualStart = 0;
1748 MemoryMap->NumberOfPages = RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);
1749 MemoryMap->Attribute = (MergeGcdMapEntry.Attributes & ~EFI_MEMORY_PORT_IO) |
1750 (MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |
1751 EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));
1752
1753 if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) {
1754 MemoryMap->Type = EfiReservedMemoryType;
1755 } else if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {
1756 if ((MergeGcdMapEntry.Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {
1757 MemoryMap->Type = EfiMemoryMappedIOPortSpace;
1758 } else {
1759 MemoryMap->Type = EfiMemoryMappedIO;
1760 }
1761 }
1762
1763 //
1764 // Check to see if the new Memory Map Descriptor can be merged with an
1765 // existing descriptor if they are adjacent and have the same attributes
1766 //
1767 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);
1768 }
1769
1770 if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypePersistentMemory) {
1771 //
1772 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR,
1773 // it will be recorded as page PhysicalStart and NumberOfPages.
1774 //
1775 ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);
1776 ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);
1777
1778 //
1779 // Create EFI_MEMORY_DESCRIPTOR for every Persistent GCD entries
1780 //
1781 MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;
1782 MemoryMap->VirtualStart = 0;
1783 MemoryMap->NumberOfPages = RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);
1784 MemoryMap->Attribute = MergeGcdMapEntry.Attributes | EFI_MEMORY_NV |
1785 (MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |
1786 EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));
1787 MemoryMap->Type = EfiPersistentMemory;
1788
1789 //
1790 // Check to see if the new Memory Map Descriptor can be merged with an
1791 // existing descriptor if they are adjacent and have the same attributes
1792 //
1793 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);
1794 }
1795 if (Link == &mGcdMemorySpaceMap) {
1796 //
1797 // break loop when arrive at head.
1798 //
1799 break;
1800 }
1801 if (GcdMapEntry != NULL) {
1802 //
1803 // Copy new GCD map entry for the following GCD range merge
1804 //
1805 CopyMem (&MergeGcdMapEntry, GcdMapEntry, sizeof (MergeGcdMapEntry));
1806 }
1807 }
1808
1809 //
1810 // Compute the size of the buffer actually used after all memory map descriptor merge operations
1811 //
1812 BufferSize = ((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart);
1813
1814 Status = EFI_SUCCESS;
1815
1816 Done:
1817 //
1818 // Update the map key finally
1819 //
1820 if (MapKey != NULL) {
1821 *MapKey = mMemoryMapKey;
1822 }
1823
1824 CoreReleaseMemoryLock ();
1825
1826 CoreReleaseGcdMemoryLock ();
1827
1828 *MemoryMapSize = BufferSize;
1829
1830 return Status;
1831 }
1832
1833
1834 /**
1835 Internal function. Used by the pool functions to allocate pages
1836 to back pool allocation requests.
1837
1838 @param PoolType The type of memory for the new pool pages
1839 @param NumberOfPages No of pages to allocate
1840 @param Alignment Bits to align.
1841
1842 @return The allocated memory, or NULL
1843
1844 **/
1845 VOID *
1846 CoreAllocatePoolPages (
1847 IN EFI_MEMORY_TYPE PoolType,
1848 IN UINTN NumberOfPages,
1849 IN UINTN Alignment
1850 )
1851 {
1852 UINT64 Start;
1853
1854 //
1855 // Find the pages to convert
1856 //
1857 Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment);
1858
1859 //
1860 // Convert it to boot services data
1861 //
1862 if (Start == 0) {
1863 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32)NumberOfPages));
1864 } else {
1865 CoreConvertPages (Start, NumberOfPages, PoolType);
1866 }
1867
1868 return (VOID *)(UINTN) Start;
1869 }
1870
1871
1872 /**
1873 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1874
1875 @param Memory The base address to free
1876 @param NumberOfPages The number of pages to free
1877
1878 **/
1879 VOID
1880 CoreFreePoolPages (
1881 IN EFI_PHYSICAL_ADDRESS Memory,
1882 IN UINTN NumberOfPages
1883 )
1884 {
1885 CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);
1886 }
1887
1888
1889
1890 /**
1891 Make sure the memory map is following all the construction rules,
1892 it is the last time to check memory map error before exit boot services.
1893
1894 @param MapKey Memory map key
1895
1896 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1897 rules.
1898 @retval EFI_SUCCESS Valid memory map.
1899
1900 **/
1901 EFI_STATUS
1902 CoreTerminateMemoryMap (
1903 IN UINTN MapKey
1904 )
1905 {
1906 EFI_STATUS Status;
1907 LIST_ENTRY *Link;
1908 MEMORY_MAP *Entry;
1909
1910 Status = EFI_SUCCESS;
1911
1912 CoreAcquireMemoryLock ();
1913
1914 if (MapKey == mMemoryMapKey) {
1915
1916 //
1917 // Make sure the memory map is following all the construction rules
1918 // This is the last chance we will be able to display any messages on
1919 // the console devices.
1920 //
1921
1922 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1923 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1924 if (Entry->Type < EfiMaxMemoryType) {
1925 if (mMemoryTypeStatistics[Entry->Type].Runtime) {
1926 ASSERT (Entry->Type != EfiACPIReclaimMemory);
1927 ASSERT (Entry->Type != EfiACPIMemoryNVS);
1928 if ((Entry->Start & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {
1929 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1930 Status = EFI_INVALID_PARAMETER;
1931 goto Done;
1932 }
1933 if (((Entry->End + 1) & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {
1934 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1935 Status = EFI_INVALID_PARAMETER;
1936 goto Done;
1937 }
1938 }
1939 }
1940 }
1941
1942 //
1943 // The map key they gave us matches what we expect. Fall through and
1944 // return success. In an ideal world we would clear out all of
1945 // EfiBootServicesCode and EfiBootServicesData. However this function
1946 // is not the last one called by ExitBootServices(), so we have to
1947 // preserve the memory contents.
1948 //
1949 } else {
1950 Status = EFI_INVALID_PARAMETER;
1951 }
1952
1953 Done:
1954 CoreReleaseMemoryLock ();
1955
1956 return Status;
1957 }
1958
1959
1960
1961
1962
1963
1964
1965
1966