]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Mem/Page.c
Use TmpStr as a backup, as StrCpy in BaseLib does not handle copy of two strings
[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 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 @return None. The range is added to the memory map
440
441 **/
442 VOID
443 CoreAddRange (
444 IN EFI_MEMORY_TYPE Type,
445 IN EFI_PHYSICAL_ADDRESS Start,
446 IN EFI_PHYSICAL_ADDRESS End,
447 IN UINT64 Attribute
448 )
449 {
450 LIST_ENTRY *Link;
451 MEMORY_MAP *Entry;
452
453 ASSERT ((Start & EFI_PAGE_MASK) == 0);
454 ASSERT (End > Start) ;
455
456 ASSERT_LOCKED (&gMemoryLock);
457
458 DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type));
459
460 //
461 // Memory map being altered so updated key
462 //
463 mMemoryMapKey += 1;
464
465 //
466 // UEFI 2.0 added an event group for notificaiton on memory map changes.
467 // So we need to signal this Event Group every time the memory map changes.
468 // If we are in EFI 1.10 compatability mode no event groups will be
469 // found and nothing will happen we we call this function. These events
470 // will get signaled but since a lock is held around the call to this
471 // function the notificaiton events will only be called after this funciton
472 // returns and the lock is released.
473 //
474 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid);
475
476 //
477 // Look for adjoining memory descriptor
478 //
479
480 // Two memory descriptors can only be merged if they have the same Type
481 // and the same Attribute
482 //
483
484 Link = gMemoryMap.ForwardLink;
485 while (Link != &gMemoryMap) {
486 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
487 Link = Link->ForwardLink;
488
489 if (Entry->Type != Type) {
490 continue;
491 }
492
493 if (Entry->Attribute != Attribute) {
494 continue;
495 }
496
497 if (Entry->End + 1 == Start) {
498
499 Start = Entry->Start;
500 RemoveMemoryMapEntry (Entry);
501
502 } else if (Entry->Start == End + 1) {
503
504 End = Entry->End;
505 RemoveMemoryMapEntry (Entry);
506 }
507 }
508
509 //
510 // Add descriptor
511 //
512
513 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;
514 mMapStack[mMapDepth].FromPages = FALSE;
515 mMapStack[mMapDepth].Type = Type;
516 mMapStack[mMapDepth].Start = Start;
517 mMapStack[mMapDepth].End = End;
518 mMapStack[mMapDepth].VirtualStart = 0;
519 mMapStack[mMapDepth].Attribute = Attribute;
520 InsertTailList (&gMemoryMap, &mMapStack[mMapDepth].Link);
521
522 mMapDepth += 1;
523 ASSERT (mMapDepth < MAX_MAP_DEPTH);
524
525 return ;
526 }
527
528
529 /**
530 Internal function. Moves any memory descriptors that are on the
531 temporary descriptor stack to heap.
532
533 **/
534 VOID
535 CoreFreeMemoryMapStack (
536 VOID
537 )
538 {
539 MEMORY_MAP *Entry;
540 MEMORY_MAP *Entry2;
541 LIST_ENTRY *Link2;
542
543 ASSERT_LOCKED (&gMemoryLock);
544
545 //
546 // If already freeing the map stack, then return
547 //
548 if (mFreeMapStack != 0) {
549 return ;
550 }
551
552 //
553 // Move the temporary memory descriptor stack into pool
554 //
555 mFreeMapStack += 1;
556
557 while (mMapDepth != 0) {
558 //
559 // Deque an memory map entry from mFreeMemoryMapEntryList
560 //
561 Entry = AllocateMemoryMapEntry ();
562
563 ASSERT (Entry);
564
565 //
566 // Update to proper entry
567 //
568 mMapDepth -= 1;
569
570 if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {
571
572 //
573 // Move this entry to general memory
574 //
575 RemoveEntryList (&mMapStack[mMapDepth].Link);
576 mMapStack[mMapDepth].Link.ForwardLink = NULL;
577
578 CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));
579 Entry->FromPages = TRUE;
580
581 //
582 // Find insertion location
583 //
584 for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) {
585 Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
586 if (Entry2->FromPages && Entry2->Start > Entry->Start) {
587 break;
588 }
589 }
590
591 InsertTailList (Link2, &Entry->Link);
592
593 } else {
594 //
595 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,
596 // so here no need to move it to memory.
597 //
598 InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);
599 }
600 }
601
602 mFreeMapStack -= 1;
603 }
604
605
606 /**
607 Internal function. Removes a descriptor entry.
608
609 @param Entry The entry to remove
610
611 **/
612 VOID
613 RemoveMemoryMapEntry (
614 IN OUT MEMORY_MAP *Entry
615 )
616 {
617 RemoveEntryList (&Entry->Link);
618 Entry->Link.ForwardLink = NULL;
619
620 if (Entry->FromPages) {
621 //
622 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList
623 //
624 InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);
625 }
626 }
627
628
629 /**
630 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.
631 If the list is emtry, then allocate a new page to refuel the list.
632 Please Note this algorithm to allocate the memory map descriptor has a property
633 that the memory allocated for memory entries always grows, and will never really be freed
634 For example, if the current boot uses 2000 memory map entries at the maximum point, but
635 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950
636 memory map entries is still allocated from EfiBootServicesMemory.
637
638
639 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList
640
641 **/
642 MEMORY_MAP *
643 AllocateMemoryMapEntry (
644 VOID
645 )
646 {
647 MEMORY_MAP* FreeDescriptorEntries;
648 MEMORY_MAP* Entry;
649 UINTN Index;
650
651 if (IsListEmpty (&mFreeMemoryMapEntryList)) {
652 //
653 // The list is empty, to allocate one page to refuel the list
654 //
655 FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION);
656 if(FreeDescriptorEntries != NULL) {
657 //
658 // Enque the free memmory map entries into the list
659 //
660 for (Index = 0; Index< DEFAULT_PAGE_ALLOCATION / sizeof(MEMORY_MAP); Index++) {
661 FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;
662 InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);
663 }
664 } else {
665 return NULL;
666 }
667 }
668 //
669 // dequeue the first descriptor from the list
670 //
671 Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
672 RemoveEntryList (&Entry->Link);
673
674 return Entry;
675 }
676
677
678 /**
679 Internal function. Converts a memory range to the specified type.
680 The range must exist in the memory map.
681
682 @param Start The first address of the range Must be page
683 aligned
684 @param NumberOfPages The number of pages to convert
685 @param NewType The new type for the memory range
686
687 @retval EFI_INVALID_PARAMETER Invalid parameter
688 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified
689 range or convertion not allowed.
690 @retval EFI_SUCCESS Successfully converts the memory range to the
691 specified type.
692
693 **/
694 EFI_STATUS
695 CoreConvertPages (
696 IN UINT64 Start,
697 IN UINT64 NumberOfPages,
698 IN EFI_MEMORY_TYPE NewType
699 )
700 {
701
702 UINT64 NumberOfBytes;
703 UINT64 End;
704 UINT64 RangeEnd;
705 UINT64 Attribute;
706 LIST_ENTRY *Link;
707 MEMORY_MAP *Entry;
708
709 Entry = NULL;
710 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);
711 End = Start + NumberOfBytes - 1;
712
713 ASSERT (NumberOfPages);
714 ASSERT ((Start & EFI_PAGE_MASK) == 0);
715 ASSERT (End > Start) ;
716 ASSERT_LOCKED (&gMemoryLock);
717
718 if (NumberOfPages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start > (Start + NumberOfBytes))) {
719 return EFI_INVALID_PARAMETER;
720 }
721
722 //
723 // Convert the entire range
724 //
725
726 while (Start < End) {
727
728 //
729 // Find the entry that the covers the range
730 //
731 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
732 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
733
734 if (Entry->Start <= Start && Entry->End > Start) {
735 break;
736 }
737 }
738
739 if (Link == &gMemoryMap) {
740 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: failed to find range %lx - %lx\n", Start, End));
741 return EFI_NOT_FOUND;
742 }
743
744 //
745 // Convert range to the end, or to the end of the descriptor
746 // if that's all we've got
747 //
748 RangeEnd = End;
749 if (Entry->End < End) {
750 RangeEnd = Entry->End;
751 }
752
753 DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to %d\n", Start, RangeEnd, NewType));
754
755 //
756 // Debug code - verify conversion is allowed
757 //
758 if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) {
759 DEBUG ((DEBUG_ERROR , "ConvertPages: Incompatible memory types\n"));
760 return EFI_NOT_FOUND;
761 }
762
763 //
764 // Update counters for the number of pages allocated to each memory type
765 //
766 if (Entry->Type >= 0 && Entry->Type < EfiMaxMemoryType) {
767 if (Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress &&
768 Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) {
769 if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {
770 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;
771 } else {
772 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;
773 }
774 }
775 }
776
777 if (NewType >= 0 && NewType < EfiMaxMemoryType) {
778 if (Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) {
779 mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;
780 if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages >
781 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {
782 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;
783 }
784 }
785 }
786
787 //
788 // Pull range out of descriptor
789 //
790 if (Entry->Start == Start) {
791
792 //
793 // Clip start
794 //
795 Entry->Start = RangeEnd + 1;
796
797 } else if (Entry->End == RangeEnd) {
798
799 //
800 // Clip end
801 //
802 Entry->End = Start - 1;
803
804 } else {
805
806 //
807 // Pull it out of the center, clip current
808 //
809
810 //
811 // Add a new one
812 //
813 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;
814 mMapStack[mMapDepth].FromPages = FALSE;
815 mMapStack[mMapDepth].Type = Entry->Type;
816 mMapStack[mMapDepth].Start = RangeEnd+1;
817 mMapStack[mMapDepth].End = Entry->End;
818
819 //
820 // Inherit Attribute from the Memory Descriptor that is being clipped
821 //
822 mMapStack[mMapDepth].Attribute = Entry->Attribute;
823
824 Entry->End = Start - 1;
825 ASSERT (Entry->Start < Entry->End);
826
827 Entry = &mMapStack[mMapDepth];
828 InsertTailList (&gMemoryMap, &Entry->Link);
829
830 mMapDepth += 1;
831 ASSERT (mMapDepth < MAX_MAP_DEPTH);
832 }
833
834 //
835 // The new range inherits the same Attribute as the Entry
836 //it is being cut out of
837 //
838 Attribute = Entry->Attribute;
839
840 //
841 // If the descriptor is empty, then remove it from the map
842 //
843 if (Entry->Start == Entry->End + 1) {
844 RemoveMemoryMapEntry (Entry);
845 Entry = NULL;
846 }
847
848 //
849 // Add our new range in
850 //
851 CoreAddRange (NewType, Start, RangeEnd, Attribute);
852
853 //
854 // Move any map descriptor stack to general pool
855 //
856 CoreFreeMemoryMapStack ();
857
858 //
859 // Bump the starting address, and convert the next range
860 //
861 Start = RangeEnd + 1;
862 }
863
864 //
865 // Converted the whole range, done
866 //
867
868 return EFI_SUCCESS;
869 }
870
871
872
873 /**
874 Internal function. Finds a consecutive free page range below
875 the requested address.
876
877 @param MaxAddress The address that the range must be below
878 @param NumberOfPages Number of pages needed
879 @param NewType The type of memory the range is going to be
880 turned into
881 @param Alignment Bits to align with
882
883 @return The base address of the range, or 0 if the range was not found
884
885 **/
886 UINT64
887 CoreFindFreePagesI (
888 IN UINT64 MaxAddress,
889 IN UINT64 NumberOfPages,
890 IN EFI_MEMORY_TYPE NewType,
891 IN UINTN Alignment
892 )
893 {
894 UINT64 NumberOfBytes;
895 UINT64 Target;
896 UINT64 DescStart;
897 UINT64 DescEnd;
898 UINT64 DescNumberOfBytes;
899 LIST_ENTRY *Link;
900 MEMORY_MAP *Entry;
901
902 if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages == 0)) {
903 return 0;
904 }
905
906 if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {
907
908 //
909 // If MaxAddress is not aligned to the end of a page
910 //
911
912 //
913 // Change MaxAddress to be 1 page lower
914 //
915 MaxAddress -= (EFI_PAGE_MASK + 1);
916
917 //
918 // Set MaxAddress to a page boundary
919 //
920 MaxAddress &= ~EFI_PAGE_MASK;
921
922 //
923 // Set MaxAddress to end of the page
924 //
925 MaxAddress |= EFI_PAGE_MASK;
926 }
927
928 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);
929 Target = 0;
930
931 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
932 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
933
934 //
935 // If it's not a free entry, don't bother with it
936 //
937 if (Entry->Type != EfiConventionalMemory) {
938 continue;
939 }
940
941 DescStart = Entry->Start;
942 DescEnd = Entry->End;
943
944 //
945 // If desc is past max allowed address, skip it
946 //
947 if (DescStart >= MaxAddress) {
948 continue;
949 }
950
951 //
952 // If desc ends past max allowed address, clip the end
953 //
954 if (DescEnd >= MaxAddress) {
955 DescEnd = MaxAddress;
956 }
957
958 DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;
959
960 //
961 // Compute the number of bytes we can used from this
962 // descriptor, and see it's enough to satisfy the request
963 //
964 DescNumberOfBytes = DescEnd - DescStart + 1;
965
966 if (DescNumberOfBytes >= NumberOfBytes) {
967
968 //
969 // If this is the best match so far remember it
970 //
971 if (DescEnd > Target) {
972 Target = DescEnd;
973 }
974 }
975 }
976
977 //
978 // If this is a grow down, adjust target to be the allocation base
979 //
980 Target -= NumberOfBytes - 1;
981
982 //
983 // If we didn't find a match, return 0
984 //
985 if ((Target & EFI_PAGE_MASK) != 0) {
986 return 0;
987 }
988
989 return Target;
990 }
991
992
993 /**
994 Internal function. Finds a consecutive free page range below
995 the requested address
996
997 @param MaxAddress The address that the range must be below
998 @param NoPages Number of pages needed
999 @param NewType The type of memory the range is going to be
1000 turned into
1001 @param Alignment Bits to align with
1002
1003 @return The base address of the range, or 0 if the range was not found.
1004
1005 **/
1006 UINT64
1007 FindFreePages (
1008 IN UINT64 MaxAddress,
1009 IN UINT64 NoPages,
1010 IN EFI_MEMORY_TYPE NewType,
1011 IN UINTN Alignment
1012 )
1013 {
1014 UINT64 NewMaxAddress;
1015 UINT64 Start;
1016
1017 NewMaxAddress = MaxAddress;
1018
1019 if (NewType >= 0 && NewType < EfiMaxMemoryType && NewMaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {
1020 NewMaxAddress = mMemoryTypeStatistics[NewType].MaximumAddress;
1021 } else {
1022 if (NewMaxAddress > mDefaultMaximumAddress) {
1023 NewMaxAddress = mDefaultMaximumAddress;
1024 }
1025 }
1026
1027 Start = CoreFindFreePagesI (NewMaxAddress, NoPages, NewType, Alignment);
1028 if (Start == 0) {
1029 Start = CoreFindFreePagesI (MaxAddress, NoPages, NewType, Alignment);
1030 if (Start == 0) {
1031 //
1032 // Here means there may be no enough memory to use, so try to go through
1033 // all the memory descript to promote the untested memory directly
1034 //
1035 PromoteMemoryResource ();
1036
1037 //
1038 // Allocate memory again after the memory resource re-arranged
1039 //
1040 Start = CoreFindFreePagesI (MaxAddress, NoPages, NewType, Alignment);
1041 }
1042 }
1043
1044 return Start;
1045 }
1046
1047
1048
1049 /**
1050 Allocates pages from the memory map.
1051
1052 @param Type The type of allocation to perform
1053 @param MemoryType The type of memory to turn the allocated pages
1054 into
1055 @param NumberOfPages The number of pages to allocate
1056 @param Memory A pointer to receive the base allocated memory
1057 address
1058
1059 @return Status. On success, Memory is filled in with the base address allocated
1060 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1061 spec.
1062 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1063 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1064 @retval EFI_SUCCESS Pages successfully allocated.
1065
1066 **/
1067 EFI_STATUS
1068 EFIAPI
1069 CoreAllocatePages (
1070 IN EFI_ALLOCATE_TYPE Type,
1071 IN EFI_MEMORY_TYPE MemoryType,
1072 IN UINTN NumberOfPages,
1073 IN OUT EFI_PHYSICAL_ADDRESS *Memory
1074 )
1075 {
1076 EFI_STATUS Status;
1077 UINT64 Start;
1078 UINT64 MaxAddress;
1079 UINTN Alignment;
1080
1081 if (Type < AllocateAnyPages || Type >= (UINTN) MaxAllocateType) {
1082 return EFI_INVALID_PARAMETER;
1083 }
1084
1085 if ((MemoryType >= EfiMaxMemoryType && MemoryType <= 0x7fffffff) ||
1086 MemoryType == EfiConventionalMemory) {
1087 return EFI_INVALID_PARAMETER;
1088 }
1089
1090 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;
1091
1092 if (MemoryType == EfiACPIReclaimMemory ||
1093 MemoryType == EfiACPIMemoryNVS ||
1094 MemoryType == EfiRuntimeServicesCode ||
1095 MemoryType == EfiRuntimeServicesData) {
1096
1097 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;
1098 }
1099
1100 if (Type == AllocateAddress) {
1101 if ((*Memory & (Alignment - 1)) != 0) {
1102 return EFI_NOT_FOUND;
1103 }
1104 }
1105
1106 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;
1107 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
1108
1109 //
1110 // If this is for below a particular address, then
1111 //
1112 Start = *Memory;
1113
1114 //
1115 // The max address is the max natively addressable address for the processor
1116 //
1117 MaxAddress = EFI_MAX_ADDRESS;
1118
1119 if (Type == AllocateMaxAddress) {
1120 MaxAddress = Start;
1121 }
1122
1123 CoreAcquireMemoryLock ();
1124
1125 //
1126 // If not a specific address, then find an address to allocate
1127 //
1128 if (Type != AllocateAddress) {
1129 Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment);
1130 if (Start == 0) {
1131 Status = EFI_OUT_OF_RESOURCES;
1132 goto Done;
1133 }
1134 }
1135
1136 //
1137 // Convert pages from FreeMemory to the requested type
1138 //
1139 Status = CoreConvertPages (Start, NumberOfPages, MemoryType);
1140
1141 Done:
1142 CoreReleaseMemoryLock ();
1143
1144 if (!EFI_ERROR (Status)) {
1145 *Memory = Start;
1146 }
1147
1148 return Status;
1149 }
1150
1151
1152 /**
1153 Frees previous allocated pages.
1154
1155 @param Memory Base address of memory being freed
1156 @param NumberOfPages The number of pages to free
1157
1158 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1159 @retval EFI_INVALID_PARAMETER Address not aligned
1160 @return EFI_SUCCESS -Pages successfully freed.
1161
1162 **/
1163 EFI_STATUS
1164 EFIAPI
1165 CoreFreePages (
1166 IN EFI_PHYSICAL_ADDRESS Memory,
1167 IN UINTN NumberOfPages
1168 )
1169 {
1170 EFI_STATUS Status;
1171 LIST_ENTRY *Link;
1172 MEMORY_MAP *Entry;
1173 UINTN Alignment;
1174
1175 //
1176 // Free the range
1177 //
1178 CoreAcquireMemoryLock ();
1179
1180 //
1181 // Find the entry that the covers the range
1182 //
1183 Entry = NULL;
1184 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1185 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1186 if (Entry->Start <= Memory && Entry->End > Memory) {
1187 break;
1188 }
1189 }
1190 if (Link == &gMemoryMap) {
1191 CoreReleaseMemoryLock ();
1192 return EFI_NOT_FOUND;
1193 }
1194
1195 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;
1196
1197 if (Entry->Type == EfiACPIReclaimMemory ||
1198 Entry->Type == EfiACPIMemoryNVS ||
1199 Entry->Type == EfiRuntimeServicesCode ||
1200 Entry->Type == EfiRuntimeServicesData) {
1201
1202 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;
1203
1204 }
1205
1206 if ((Memory & (Alignment - 1)) != 0) {
1207 CoreReleaseMemoryLock ();
1208 return EFI_INVALID_PARAMETER;
1209 }
1210
1211 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;
1212 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
1213
1214 Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);
1215
1216 CoreReleaseMemoryLock ();
1217
1218 if (EFI_ERROR (Status)) {
1219 return Status;
1220 }
1221
1222 //
1223 // Destroy the contents
1224 //
1225 if (Memory < EFI_MAX_ADDRESS) {
1226 DEBUG_CLEAR_MEMORY ((VOID *)(UINTN)Memory, NumberOfPages << EFI_PAGE_SHIFT);
1227 }
1228
1229 return Status;
1230 }
1231
1232
1233 /**
1234 This function returns a copy of the current memory map. The map is an array of
1235 memory descriptors, each of which describes a contiguous block of memory.
1236
1237 @param MemoryMapSize A pointer to the size, in bytes, of the
1238 MemoryMap buffer. On input, this is the size of
1239 the buffer allocated by the caller. On output,
1240 it is the size of the buffer returned by the
1241 firmware if the buffer was large enough, or the
1242 size of the buffer needed to contain the map if
1243 the buffer was too small.
1244 @param MemoryMap A pointer to the buffer in which firmware places
1245 the current memory map.
1246 @param MapKey A pointer to the location in which firmware
1247 returns the key for the current memory map.
1248 @param DescriptorSize A pointer to the location in which firmware
1249 returns the size, in bytes, of an individual
1250 EFI_MEMORY_DESCRIPTOR.
1251 @param DescriptorVersion A pointer to the location in which firmware
1252 returns the version number associated with the
1253 EFI_MEMORY_DESCRIPTOR.
1254
1255 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1256 buffer.
1257 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1258 buffer size needed to hold the memory map is
1259 returned in MemoryMapSize.
1260 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1261
1262 **/
1263 EFI_STATUS
1264 EFIAPI
1265 CoreGetMemoryMap (
1266 IN OUT UINTN *MemoryMapSize,
1267 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
1268 OUT UINTN *MapKey,
1269 OUT UINTN *DescriptorSize,
1270 OUT UINT32 *DescriptorVersion
1271 )
1272 {
1273 EFI_STATUS Status;
1274 UINTN Size;
1275 UINTN BufferSize;
1276 UINTN NumberOfRuntimeEntries;
1277 LIST_ENTRY *Link;
1278 MEMORY_MAP *Entry;
1279 EFI_GCD_MAP_ENTRY *GcdMapEntry;
1280 EFI_MEMORY_TYPE Type;
1281
1282 //
1283 // Make sure the parameters are valid
1284 //
1285 if (MemoryMapSize == NULL) {
1286 return EFI_INVALID_PARAMETER;
1287 }
1288
1289 CoreAcquireGcdMemoryLock ();
1290
1291 //
1292 // Count the number of Reserved and MMIO entries that are marked for runtime use
1293 //
1294 NumberOfRuntimeEntries = 0;
1295 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {
1296 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1297 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||
1298 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {
1299 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {
1300 NumberOfRuntimeEntries++;
1301 }
1302 }
1303 }
1304
1305 Size = sizeof (EFI_MEMORY_DESCRIPTOR);
1306
1307 //
1308 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1309 // prevent people from having pointer math bugs in their code.
1310 // now you have to use *DescriptorSize to make things work.
1311 //
1312 Size += sizeof(UINT64) - (Size % sizeof (UINT64));
1313
1314 if (DescriptorSize != NULL) {
1315 *DescriptorSize = Size;
1316 }
1317
1318 if (DescriptorVersion != NULL) {
1319 *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;
1320 }
1321
1322 CoreAcquireMemoryLock ();
1323
1324 //
1325 // Compute the buffer size needed to fit the entire map
1326 //
1327 BufferSize = Size * NumberOfRuntimeEntries;
1328 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1329 BufferSize += Size;
1330 }
1331
1332 if (*MemoryMapSize < BufferSize) {
1333 Status = EFI_BUFFER_TOO_SMALL;
1334 goto Done;
1335 }
1336
1337 if (MemoryMap == NULL) {
1338 Status = EFI_INVALID_PARAMETER;
1339 goto Done;
1340 }
1341
1342 //
1343 // Build the map
1344 //
1345 ZeroMem (MemoryMap, BufferSize);
1346 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1347 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1348 ASSERT (Entry->VirtualStart == 0);
1349
1350 //
1351 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1352 //
1353 MemoryMap->Type = Entry->Type;
1354 MemoryMap->PhysicalStart = Entry->Start;
1355 MemoryMap->VirtualStart = Entry->VirtualStart;
1356 MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);
1357 //
1358 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1359 // memory type bin and needs to be converted to the same memory type as the rest of the
1360 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1361 // improves the chances for a successful S4 resume in the presence of minor page allocation
1362 // differences across reboots.
1363 //
1364 if (MemoryMap->Type == EfiConventionalMemory) {
1365 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {
1366 if (mMemoryTypeStatistics[Type].Special &&
1367 mMemoryTypeStatistics[Type].NumberOfPages > 0 &&
1368 Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress &&
1369 Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) {
1370 MemoryMap->Type = Type;
1371 }
1372 }
1373 }
1374 MemoryMap->Attribute = Entry->Attribute;
1375 if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {
1376 MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;
1377 }
1378
1379 MemoryMap = NextMemoryDescriptor (MemoryMap, Size);
1380 }
1381
1382 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {
1383 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1384 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||
1385 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {
1386 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {
1387
1388 MemoryMap->PhysicalStart = GcdMapEntry->BaseAddress;
1389 MemoryMap->VirtualStart = 0;
1390 MemoryMap->NumberOfPages = RShiftU64 ((GcdMapEntry->EndAddress - GcdMapEntry->BaseAddress + 1), EFI_PAGE_SHIFT);
1391 MemoryMap->Attribute = GcdMapEntry->Attributes & ~EFI_MEMORY_PORT_IO;
1392
1393 if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) {
1394 MemoryMap->Type = EfiReservedMemoryType;
1395 } else if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {
1396 if ((GcdMapEntry->Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {
1397 MemoryMap->Type = EfiMemoryMappedIOPortSpace;
1398 } else {
1399 MemoryMap->Type = EfiMemoryMappedIO;
1400 }
1401 }
1402
1403 MemoryMap = NextMemoryDescriptor (MemoryMap, Size);
1404 }
1405 }
1406 }
1407
1408 Status = EFI_SUCCESS;
1409
1410 Done:
1411
1412 CoreReleaseMemoryLock ();
1413
1414 CoreReleaseGcdMemoryLock ();
1415
1416 //
1417 // Update the map key finally
1418 //
1419 if (MapKey != NULL) {
1420 *MapKey = mMemoryMapKey;
1421 }
1422
1423 *MemoryMapSize = BufferSize;
1424
1425 return Status;
1426 }
1427
1428
1429 /**
1430 Internal function. Used by the pool functions to allocate pages
1431 to back pool allocation requests.
1432
1433 @param PoolType The type of memory for the new pool pages
1434 @param NumberOfPages No of pages to allocate
1435 @param Alignment Bits to align.
1436
1437 @return The allocated memory, or NULL
1438
1439 **/
1440 VOID *
1441 CoreAllocatePoolPages (
1442 IN EFI_MEMORY_TYPE PoolType,
1443 IN UINTN NumberOfPages,
1444 IN UINTN Alignment
1445 )
1446 {
1447 UINT64 Start;
1448
1449 //
1450 // Find the pages to convert
1451 //
1452 Start = FindFreePages (EFI_MAX_ADDRESS, NumberOfPages, PoolType, Alignment);
1453
1454 //
1455 // Convert it to boot services data
1456 //
1457 if (Start == 0) {
1458 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", NumberOfPages));
1459 } else {
1460 CoreConvertPages (Start, NumberOfPages, PoolType);
1461 }
1462
1463 return (VOID *)(UINTN) Start;
1464 }
1465
1466
1467 /**
1468 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1469
1470 @param Memory The base address to free
1471 @param NumberOfPages The number of pages to free
1472
1473 **/
1474 VOID
1475 CoreFreePoolPages (
1476 IN EFI_PHYSICAL_ADDRESS Memory,
1477 IN UINTN NumberOfPages
1478 )
1479 {
1480 CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);
1481 }
1482
1483
1484
1485 /**
1486 Make sure the memory map is following all the construction rules,
1487 it is the last time to check memory map error before exit boot services.
1488
1489 @param MapKey Memory map key
1490
1491 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1492 rules.
1493 @retval EFI_SUCCESS Valid memory map.
1494
1495 **/
1496 EFI_STATUS
1497 CoreTerminateMemoryMap (
1498 IN UINTN MapKey
1499 )
1500 {
1501 EFI_STATUS Status;
1502 LIST_ENTRY *Link;
1503 MEMORY_MAP *Entry;
1504
1505 Status = EFI_SUCCESS;
1506
1507 CoreAcquireMemoryLock ();
1508
1509 if (MapKey == mMemoryMapKey) {
1510
1511 //
1512 // Make sure the memory map is following all the construction rules
1513 // This is the last chance we will be able to display any messages on
1514 // the console devices.
1515 //
1516
1517 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1518 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1519 if (Entry->Attribute & EFI_MEMORY_RUNTIME) {
1520 if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS) {
1521 DEBUG((DEBUG_ERROR, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));
1522 CoreReleaseMemoryLock ();
1523 return EFI_INVALID_PARAMETER;
1524 }
1525 if (Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) {
1526 DEBUG((DEBUG_ERROR, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1527 CoreReleaseMemoryLock ();
1528 return EFI_INVALID_PARAMETER;
1529 }
1530 if ((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) {
1531 DEBUG((DEBUG_ERROR, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1532 CoreReleaseMemoryLock ();
1533 return EFI_INVALID_PARAMETER;
1534 }
1535 }
1536 }
1537
1538 //
1539 // The map key they gave us matches what we expect. Fall through and
1540 // return success. In an ideal world we would clear out all of
1541 // EfiBootServicesCode and EfiBootServicesData. However this function
1542 // is not the last one called by ExitBootServices(), so we have to
1543 // preserve the memory contents.
1544 //
1545 } else {
1546 Status = EFI_INVALID_PARAMETER;
1547 }
1548
1549 CoreReleaseMemoryLock ();
1550
1551 return Status;
1552 }
1553
1554
1555
1556
1557
1558
1559
1560
1561