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