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