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