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