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