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