]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Mem/Page.c
Add a UNION definition (IP_IO_IP_PROTOOCL) for EFI_IP4/6_PROTOCOL and change IP_IO...
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Page.c
1 /** @file
2 UEFI Memory page management functions.
3
4 Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "DxeMain.h"
16 #include "Imem.h"
17
18 #define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT (EFI_PAGE_SIZE)
19
20 //
21 // Entry for tracking the memory regions for each memory type to coalesce similar memory types
22 //
23 typedef struct {
24 EFI_PHYSICAL_ADDRESS BaseAddress;
25 EFI_PHYSICAL_ADDRESS MaximumAddress;
26 UINT64 CurrentNumberOfPages;
27 UINT64 NumberOfPages;
28 UINTN InformationIndex;
29 BOOLEAN Special;
30 BOOLEAN Runtime;
31 } EFI_MEMORY_TYPE_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 if (NewType == EfiConventionalMemory) {
833 DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) Start, (UINTN) (RangeEnd - Start + 1));
834 }
835
836 //
837 // Move any map descriptor stack to general pool
838 //
839 CoreFreeMemoryMapStack ();
840
841 //
842 // Bump the starting address, and convert the next range
843 //
844 Start = RangeEnd + 1;
845 }
846
847 //
848 // Converted the whole range, done
849 //
850
851 return EFI_SUCCESS;
852 }
853
854
855
856 /**
857 Internal function. Finds a consecutive free page range below
858 the requested address.
859
860 @param MaxAddress The address that the range must be below
861 @param NumberOfPages Number of pages needed
862 @param NewType The type of memory the range is going to be
863 turned into
864 @param Alignment Bits to align with
865
866 @return The base address of the range, or 0 if the range was not found
867
868 **/
869 UINT64
870 CoreFindFreePagesI (
871 IN UINT64 MaxAddress,
872 IN UINT64 NumberOfPages,
873 IN EFI_MEMORY_TYPE NewType,
874 IN UINTN Alignment
875 )
876 {
877 UINT64 NumberOfBytes;
878 UINT64 Target;
879 UINT64 DescStart;
880 UINT64 DescEnd;
881 UINT64 DescNumberOfBytes;
882 LIST_ENTRY *Link;
883 MEMORY_MAP *Entry;
884
885 if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages == 0)) {
886 return 0;
887 }
888
889 if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {
890
891 //
892 // If MaxAddress is not aligned to the end of a page
893 //
894
895 //
896 // Change MaxAddress to be 1 page lower
897 //
898 MaxAddress -= (EFI_PAGE_MASK + 1);
899
900 //
901 // Set MaxAddress to a page boundary
902 //
903 MaxAddress &= ~EFI_PAGE_MASK;
904
905 //
906 // Set MaxAddress to end of the page
907 //
908 MaxAddress |= EFI_PAGE_MASK;
909 }
910
911 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);
912 Target = 0;
913
914 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
915 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
916
917 //
918 // If it's not a free entry, don't bother with it
919 //
920 if (Entry->Type != EfiConventionalMemory) {
921 continue;
922 }
923
924 DescStart = Entry->Start;
925 DescEnd = Entry->End;
926
927 //
928 // If desc is past max allowed address, skip it
929 //
930 if (DescStart >= MaxAddress) {
931 continue;
932 }
933
934 //
935 // If desc ends past max allowed address, clip the end
936 //
937 if (DescEnd >= MaxAddress) {
938 DescEnd = MaxAddress;
939 }
940
941 DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;
942
943 //
944 // Compute the number of bytes we can used from this
945 // descriptor, and see it's enough to satisfy the request
946 //
947 DescNumberOfBytes = DescEnd - DescStart + 1;
948
949 if (DescNumberOfBytes >= NumberOfBytes) {
950
951 //
952 // If this is the best match so far remember it
953 //
954 if (DescEnd > Target) {
955 Target = DescEnd;
956 }
957 }
958 }
959
960 //
961 // If this is a grow down, adjust target to be the allocation base
962 //
963 Target -= NumberOfBytes - 1;
964
965 //
966 // If we didn't find a match, return 0
967 //
968 if ((Target & EFI_PAGE_MASK) != 0) {
969 return 0;
970 }
971
972 return Target;
973 }
974
975
976 /**
977 Internal function. Finds a consecutive free page range below
978 the requested address
979
980 @param MaxAddress The address that the range must be below
981 @param NoPages Number of pages needed
982 @param NewType The type of memory the range is going to be
983 turned into
984 @param Alignment Bits to align with
985
986 @return The base address of the range, or 0 if the range was not found.
987
988 **/
989 UINT64
990 FindFreePages (
991 IN UINT64 MaxAddress,
992 IN UINT64 NoPages,
993 IN EFI_MEMORY_TYPE NewType,
994 IN UINTN Alignment
995 )
996 {
997 UINT64 NewMaxAddress;
998 UINT64 Start;
999
1000 NewMaxAddress = MaxAddress;
1001
1002 if (NewType >= 0 && NewType < EfiMaxMemoryType && NewMaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {
1003 NewMaxAddress = mMemoryTypeStatistics[NewType].MaximumAddress;
1004 } else {
1005 if (NewMaxAddress > mDefaultMaximumAddress) {
1006 NewMaxAddress = mDefaultMaximumAddress;
1007 }
1008 }
1009
1010 Start = CoreFindFreePagesI (NewMaxAddress, NoPages, NewType, Alignment);
1011 if (Start == 0) {
1012 Start = CoreFindFreePagesI (MaxAddress, NoPages, NewType, Alignment);
1013 if (Start == 0) {
1014 //
1015 // Here means there may be no enough memory to use, so try to go through
1016 // all the memory descript to promote the untested memory directly
1017 //
1018 PromoteMemoryResource ();
1019
1020 //
1021 // Allocate memory again after the memory resource re-arranged
1022 //
1023 Start = CoreFindFreePagesI (MaxAddress, NoPages, NewType, Alignment);
1024 }
1025 }
1026
1027 return Start;
1028 }
1029
1030
1031
1032 /**
1033 Allocates pages from the memory map.
1034
1035 @param Type The type of allocation to perform
1036 @param MemoryType The type of memory to turn the allocated pages
1037 into
1038 @param NumberOfPages The number of pages to allocate
1039 @param Memory A pointer to receive the base allocated memory
1040 address
1041
1042 @return Status. On success, Memory is filled in with the base address allocated
1043 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
1044 spec.
1045 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
1046 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
1047 @retval EFI_SUCCESS Pages successfully allocated.
1048
1049 **/
1050 EFI_STATUS
1051 EFIAPI
1052 CoreAllocatePages (
1053 IN EFI_ALLOCATE_TYPE Type,
1054 IN EFI_MEMORY_TYPE MemoryType,
1055 IN UINTN NumberOfPages,
1056 IN OUT EFI_PHYSICAL_ADDRESS *Memory
1057 )
1058 {
1059 EFI_STATUS Status;
1060 UINT64 Start;
1061 UINT64 MaxAddress;
1062 UINTN Alignment;
1063
1064 if (Type < AllocateAnyPages || Type >= (UINTN) MaxAllocateType) {
1065 return EFI_INVALID_PARAMETER;
1066 }
1067
1068 if ((MemoryType >= EfiMaxMemoryType && MemoryType <= 0x7fffffff) ||
1069 MemoryType == EfiConventionalMemory) {
1070 return EFI_INVALID_PARAMETER;
1071 }
1072
1073 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;
1074
1075 if (MemoryType == EfiACPIReclaimMemory ||
1076 MemoryType == EfiACPIMemoryNVS ||
1077 MemoryType == EfiRuntimeServicesCode ||
1078 MemoryType == EfiRuntimeServicesData) {
1079
1080 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;
1081 }
1082
1083 if (Type == AllocateAddress) {
1084 if ((*Memory & (Alignment - 1)) != 0) {
1085 return EFI_NOT_FOUND;
1086 }
1087 }
1088
1089 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;
1090 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
1091
1092 //
1093 // If this is for below a particular address, then
1094 //
1095 Start = *Memory;
1096
1097 //
1098 // The max address is the max natively addressable address for the processor
1099 //
1100 MaxAddress = MAX_ADDRESS;
1101
1102 if (Type == AllocateMaxAddress) {
1103 MaxAddress = Start;
1104 }
1105
1106 CoreAcquireMemoryLock ();
1107
1108 //
1109 // If not a specific address, then find an address to allocate
1110 //
1111 if (Type != AllocateAddress) {
1112 Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment);
1113 if (Start == 0) {
1114 Status = EFI_OUT_OF_RESOURCES;
1115 goto Done;
1116 }
1117 }
1118
1119 //
1120 // Convert pages from FreeMemory to the requested type
1121 //
1122 Status = CoreConvertPages (Start, NumberOfPages, MemoryType);
1123
1124 Done:
1125 CoreReleaseMemoryLock ();
1126
1127 if (!EFI_ERROR (Status)) {
1128 *Memory = Start;
1129 }
1130
1131 return Status;
1132 }
1133
1134
1135 /**
1136 Frees previous allocated pages.
1137
1138 @param Memory Base address of memory being freed
1139 @param NumberOfPages The number of pages to free
1140
1141 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1142 @retval EFI_INVALID_PARAMETER Address not aligned
1143 @return EFI_SUCCESS -Pages successfully freed.
1144
1145 **/
1146 EFI_STATUS
1147 EFIAPI
1148 CoreFreePages (
1149 IN EFI_PHYSICAL_ADDRESS Memory,
1150 IN UINTN NumberOfPages
1151 )
1152 {
1153 EFI_STATUS Status;
1154 LIST_ENTRY *Link;
1155 MEMORY_MAP *Entry;
1156 UINTN Alignment;
1157
1158 //
1159 // Free the range
1160 //
1161 CoreAcquireMemoryLock ();
1162
1163 //
1164 // Find the entry that the covers the range
1165 //
1166 Entry = NULL;
1167 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1168 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1169 if (Entry->Start <= Memory && Entry->End > Memory) {
1170 break;
1171 }
1172 }
1173 if (Link == &gMemoryMap) {
1174 Status = EFI_NOT_FOUND;
1175 goto Done;
1176 }
1177
1178 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;
1179
1180 ASSERT (Entry != NULL);
1181 if (Entry->Type == EfiACPIReclaimMemory ||
1182 Entry->Type == EfiACPIMemoryNVS ||
1183 Entry->Type == EfiRuntimeServicesCode ||
1184 Entry->Type == EfiRuntimeServicesData) {
1185
1186 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;
1187
1188 }
1189
1190 if ((Memory & (Alignment - 1)) != 0) {
1191 Status = EFI_INVALID_PARAMETER;
1192 goto Done;
1193 }
1194
1195 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;
1196 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
1197
1198 Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);
1199
1200 if (EFI_ERROR (Status)) {
1201 goto Done;
1202 }
1203
1204 Done:
1205 CoreReleaseMemoryLock ();
1206 return Status;
1207 }
1208
1209
1210 /**
1211 This function returns a copy of the current memory map. The map is an array of
1212 memory descriptors, each of which describes a contiguous block of memory.
1213
1214 @param MemoryMapSize A pointer to the size, in bytes, of the
1215 MemoryMap buffer. On input, this is the size of
1216 the buffer allocated by the caller. On output,
1217 it is the size of the buffer returned by the
1218 firmware if the buffer was large enough, or the
1219 size of the buffer needed to contain the map if
1220 the buffer was too small.
1221 @param MemoryMap A pointer to the buffer in which firmware places
1222 the current memory map.
1223 @param MapKey A pointer to the location in which firmware
1224 returns the key for the current memory map.
1225 @param DescriptorSize A pointer to the location in which firmware
1226 returns the size, in bytes, of an individual
1227 EFI_MEMORY_DESCRIPTOR.
1228 @param DescriptorVersion A pointer to the location in which firmware
1229 returns the version number associated with the
1230 EFI_MEMORY_DESCRIPTOR.
1231
1232 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1233 buffer.
1234 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1235 buffer size needed to hold the memory map is
1236 returned in MemoryMapSize.
1237 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1238
1239 **/
1240 EFI_STATUS
1241 EFIAPI
1242 CoreGetMemoryMap (
1243 IN OUT UINTN *MemoryMapSize,
1244 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
1245 OUT UINTN *MapKey,
1246 OUT UINTN *DescriptorSize,
1247 OUT UINT32 *DescriptorVersion
1248 )
1249 {
1250 EFI_STATUS Status;
1251 UINTN Size;
1252 UINTN BufferSize;
1253 UINTN NumberOfRuntimeEntries;
1254 LIST_ENTRY *Link;
1255 MEMORY_MAP *Entry;
1256 EFI_GCD_MAP_ENTRY *GcdMapEntry;
1257 EFI_MEMORY_TYPE Type;
1258
1259 //
1260 // Make sure the parameters are valid
1261 //
1262 if (MemoryMapSize == NULL) {
1263 return EFI_INVALID_PARAMETER;
1264 }
1265
1266 CoreAcquireGcdMemoryLock ();
1267
1268 //
1269 // Count the number of Reserved and MMIO entries that are marked for runtime use
1270 //
1271 NumberOfRuntimeEntries = 0;
1272 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {
1273 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1274 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||
1275 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {
1276 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {
1277 NumberOfRuntimeEntries++;
1278 }
1279 }
1280 }
1281
1282 Size = sizeof (EFI_MEMORY_DESCRIPTOR);
1283
1284 //
1285 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1286 // prevent people from having pointer math bugs in their code.
1287 // now you have to use *DescriptorSize to make things work.
1288 //
1289 Size += sizeof(UINT64) - (Size % sizeof (UINT64));
1290
1291 if (DescriptorSize != NULL) {
1292 *DescriptorSize = Size;
1293 }
1294
1295 if (DescriptorVersion != NULL) {
1296 *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;
1297 }
1298
1299 CoreAcquireMemoryLock ();
1300
1301 //
1302 // Compute the buffer size needed to fit the entire map
1303 //
1304 BufferSize = Size * NumberOfRuntimeEntries;
1305 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1306 BufferSize += Size;
1307 }
1308
1309 if (*MemoryMapSize < BufferSize) {
1310 Status = EFI_BUFFER_TOO_SMALL;
1311 goto Done;
1312 }
1313
1314 if (MemoryMap == NULL) {
1315 Status = EFI_INVALID_PARAMETER;
1316 goto Done;
1317 }
1318
1319 //
1320 // Build the map
1321 //
1322 ZeroMem (MemoryMap, BufferSize);
1323 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1324 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1325 ASSERT (Entry->VirtualStart == 0);
1326
1327 //
1328 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1329 //
1330 MemoryMap->Type = Entry->Type;
1331 MemoryMap->PhysicalStart = Entry->Start;
1332 MemoryMap->VirtualStart = Entry->VirtualStart;
1333 MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);
1334 //
1335 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1336 // memory type bin and needs to be converted to the same memory type as the rest of the
1337 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1338 // improves the chances for a successful S4 resume in the presence of minor page allocation
1339 // differences across reboots.
1340 //
1341 if (MemoryMap->Type == EfiConventionalMemory) {
1342 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {
1343 if (mMemoryTypeStatistics[Type].Special &&
1344 mMemoryTypeStatistics[Type].NumberOfPages > 0 &&
1345 Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress &&
1346 Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) {
1347 MemoryMap->Type = Type;
1348 }
1349 }
1350 }
1351 MemoryMap->Attribute = Entry->Attribute;
1352 if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {
1353 MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;
1354 }
1355
1356 MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);
1357 }
1358
1359 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {
1360 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1361 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||
1362 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {
1363 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {
1364 //
1365 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries
1366 // that are marked for runtime use
1367 //
1368 MemoryMap->PhysicalStart = GcdMapEntry->BaseAddress;
1369 MemoryMap->VirtualStart = 0;
1370 MemoryMap->NumberOfPages = RShiftU64 ((GcdMapEntry->EndAddress - GcdMapEntry->BaseAddress + 1), EFI_PAGE_SHIFT);
1371 MemoryMap->Attribute = GcdMapEntry->Attributes & ~EFI_MEMORY_PORT_IO;
1372
1373 if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) {
1374 MemoryMap->Type = EfiReservedMemoryType;
1375 } else if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {
1376 if ((GcdMapEntry->Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {
1377 MemoryMap->Type = EfiMemoryMappedIOPortSpace;
1378 } else {
1379 MemoryMap->Type = EfiMemoryMappedIO;
1380 }
1381 }
1382
1383 MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);
1384 }
1385 }
1386 }
1387
1388 Status = EFI_SUCCESS;
1389
1390 Done:
1391
1392 CoreReleaseMemoryLock ();
1393
1394 CoreReleaseGcdMemoryLock ();
1395
1396 //
1397 // Update the map key finally
1398 //
1399 if (MapKey != NULL) {
1400 *MapKey = mMemoryMapKey;
1401 }
1402
1403 *MemoryMapSize = BufferSize;
1404
1405 return Status;
1406 }
1407
1408
1409 /**
1410 Internal function. Used by the pool functions to allocate pages
1411 to back pool allocation requests.
1412
1413 @param PoolType The type of memory for the new pool pages
1414 @param NumberOfPages No of pages to allocate
1415 @param Alignment Bits to align.
1416
1417 @return The allocated memory, or NULL
1418
1419 **/
1420 VOID *
1421 CoreAllocatePoolPages (
1422 IN EFI_MEMORY_TYPE PoolType,
1423 IN UINTN NumberOfPages,
1424 IN UINTN Alignment
1425 )
1426 {
1427 UINT64 Start;
1428
1429 //
1430 // Find the pages to convert
1431 //
1432 Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment);
1433
1434 //
1435 // Convert it to boot services data
1436 //
1437 if (Start == 0) {
1438 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32)NumberOfPages));
1439 } else {
1440 CoreConvertPages (Start, NumberOfPages, PoolType);
1441 }
1442
1443 return (VOID *)(UINTN) Start;
1444 }
1445
1446
1447 /**
1448 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1449
1450 @param Memory The base address to free
1451 @param NumberOfPages The number of pages to free
1452
1453 **/
1454 VOID
1455 CoreFreePoolPages (
1456 IN EFI_PHYSICAL_ADDRESS Memory,
1457 IN UINTN NumberOfPages
1458 )
1459 {
1460 CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);
1461 }
1462
1463
1464
1465 /**
1466 Make sure the memory map is following all the construction rules,
1467 it is the last time to check memory map error before exit boot services.
1468
1469 @param MapKey Memory map key
1470
1471 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1472 rules.
1473 @retval EFI_SUCCESS Valid memory map.
1474
1475 **/
1476 EFI_STATUS
1477 CoreTerminateMemoryMap (
1478 IN UINTN MapKey
1479 )
1480 {
1481 EFI_STATUS Status;
1482 LIST_ENTRY *Link;
1483 MEMORY_MAP *Entry;
1484
1485 Status = EFI_SUCCESS;
1486
1487 CoreAcquireMemoryLock ();
1488
1489 if (MapKey == mMemoryMapKey) {
1490
1491 //
1492 // Make sure the memory map is following all the construction rules
1493 // This is the last chance we will be able to display any messages on
1494 // the console devices.
1495 //
1496
1497 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1498 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1499 if ((Entry->Attribute & EFI_MEMORY_RUNTIME) != 0) {
1500 if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS) {
1501 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));
1502 Status = EFI_INVALID_PARAMETER;
1503 goto Done;
1504 }
1505 if ((Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {
1506 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1507 Status = EFI_INVALID_PARAMETER;
1508 goto Done;
1509 }
1510 if (((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {
1511 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1512 Status = EFI_INVALID_PARAMETER;
1513 goto Done;
1514 }
1515 }
1516 }
1517
1518 //
1519 // The map key they gave us matches what we expect. Fall through and
1520 // return success. In an ideal world we would clear out all of
1521 // EfiBootServicesCode and EfiBootServicesData. However this function
1522 // is not the last one called by ExitBootServices(), so we have to
1523 // preserve the memory contents.
1524 //
1525 } else {
1526 Status = EFI_INVALID_PARAMETER;
1527 }
1528
1529 Done:
1530 CoreReleaseMemoryLock ();
1531
1532 return Status;
1533 }
1534
1535
1536
1537
1538
1539
1540
1541
1542