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