]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Dxe/Mem/Page.c
Fix several coding style violations
[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, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiReservedMemoryType
57 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderCode
58 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderData
59 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesCode
60 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesData
61 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesCode
62 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesData
63 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiConventionalMemory
64 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiUnusableMemory
65 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIReclaimMemory
66 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIMemoryNVS
67 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIO
68 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIOPortSpace
69 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiPalCode
70 { 0, EFI_MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE } // EfiMaxMemoryType
71 };
72
73 EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress = EFI_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 < EFI_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 = EFI_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 == EFI_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 if (Entry->End < End) {
655 RangeEnd = Entry->End;
656 }
657
658 DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to %d\n", Start, RangeEnd, NewType));
659
660 //
661 // Debug code - verify conversion is allowed
662 //
663 if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) {
664 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types\n"));
665 return EFI_NOT_FOUND;
666 }
667
668 //
669 // Update counters for the number of pages allocated to each memory type
670 //
671 if (Entry->Type >= 0 && Entry->Type < EfiMaxMemoryType) {
672 if (Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress &&
673 Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) {
674 if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {
675 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;
676 } else {
677 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;
678 }
679 }
680 }
681
682 if (NewType >= 0 && NewType < EfiMaxMemoryType) {
683 if (Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) {
684 mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;
685 if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages >
686 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {
687 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;
688 }
689 }
690 }
691
692 //
693 // Pull range out of descriptor
694 //
695 if (Entry->Start == Start) {
696
697 //
698 // Clip start
699 //
700 Entry->Start = RangeEnd + 1;
701
702 } else if (Entry->End == RangeEnd) {
703
704 //
705 // Clip end
706 //
707 Entry->End = Start - 1;
708
709 } else {
710
711 //
712 // Pull it out of the center, clip current
713 //
714
715 //
716 // Add a new one
717 //
718 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;
719 mMapStack[mMapDepth].FromPages = FALSE;
720 mMapStack[mMapDepth].Type = Entry->Type;
721 mMapStack[mMapDepth].Start = RangeEnd+1;
722 mMapStack[mMapDepth].End = Entry->End;
723
724 //
725 // Inherit Attribute from the Memory Descriptor that is being clipped
726 //
727 mMapStack[mMapDepth].Attribute = Entry->Attribute;
728
729 Entry->End = Start - 1;
730 ASSERT (Entry->Start < Entry->End);
731
732 Entry = &mMapStack[mMapDepth];
733 InsertTailList (&gMemoryMap, &Entry->Link);
734
735 mMapDepth += 1;
736 ASSERT (mMapDepth < MAX_MAP_DEPTH);
737 }
738
739 //
740 // The new range inherits the same Attribute as the Entry
741 //it is being cut out of
742 //
743 Attribute = Entry->Attribute;
744
745 //
746 // If the descriptor is empty, then remove it from the map
747 //
748 if (Entry->Start == Entry->End + 1) {
749 RemoveMemoryMapEntry (Entry);
750 Entry = NULL;
751 }
752
753 //
754 // Add our new range in
755 //
756 CoreAddRange (NewType, Start, RangeEnd, Attribute);
757
758 //
759 // Move any map descriptor stack to general pool
760 //
761 CoreFreeMemoryMapStack ();
762
763 //
764 // Bump the starting address, and convert the next range
765 //
766 Start = RangeEnd + 1;
767 }
768
769 //
770 // Converted the whole range, done
771 //
772
773 return EFI_SUCCESS;
774 }
775
776
777
778 /**
779 Internal function. Finds a consecutive free page range below
780 the requested address.
781
782 @param MaxAddress The address that the range must be below
783 @param NumberOfPages Number of pages needed
784 @param NewType The type of memory the range is going to be
785 turned into
786 @param Alignment Bits to align with
787
788 @return The base address of the range, or 0 if the range was not found
789
790 **/
791 UINT64
792 CoreFindFreePagesI (
793 IN UINT64 MaxAddress,
794 IN UINT64 NumberOfPages,
795 IN EFI_MEMORY_TYPE NewType,
796 IN UINTN Alignment
797 )
798 {
799 UINT64 NumberOfBytes;
800 UINT64 Target;
801 UINT64 DescStart;
802 UINT64 DescEnd;
803 UINT64 DescNumberOfBytes;
804 LIST_ENTRY *Link;
805 MEMORY_MAP *Entry;
806
807 if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages == 0)) {
808 return 0;
809 }
810
811 if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {
812
813 //
814 // If MaxAddress is not aligned to the end of a page
815 //
816
817 //
818 // Change MaxAddress to be 1 page lower
819 //
820 MaxAddress -= (EFI_PAGE_MASK + 1);
821
822 //
823 // Set MaxAddress to a page boundary
824 //
825 MaxAddress &= ~EFI_PAGE_MASK;
826
827 //
828 // Set MaxAddress to end of the page
829 //
830 MaxAddress |= EFI_PAGE_MASK;
831 }
832
833 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);
834 Target = 0;
835
836 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
837 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
838
839 //
840 // If it's not a free entry, don't bother with it
841 //
842 if (Entry->Type != EfiConventionalMemory) {
843 continue;
844 }
845
846 DescStart = Entry->Start;
847 DescEnd = Entry->End;
848
849 //
850 // If desc is past max allowed address, skip it
851 //
852 if (DescStart >= MaxAddress) {
853 continue;
854 }
855
856 //
857 // If desc ends past max allowed address, clip the end
858 //
859 if (DescEnd >= MaxAddress) {
860 DescEnd = MaxAddress;
861 }
862
863 DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;
864
865 //
866 // Compute the number of bytes we can used from this
867 // descriptor, and see it's enough to satisfy the request
868 //
869 DescNumberOfBytes = DescEnd - DescStart + 1;
870
871 if (DescNumberOfBytes >= NumberOfBytes) {
872
873 //
874 // If this is the best match so far remember it
875 //
876 if (DescEnd > Target) {
877 Target = DescEnd;
878 }
879 }
880 }
881
882 //
883 // If this is a grow down, adjust target to be the allocation base
884 //
885 Target -= NumberOfBytes - 1;
886
887 //
888 // If we didn't find a match, return 0
889 //
890 if ((Target & EFI_PAGE_MASK) != 0) {
891 return 0;
892 }
893
894 return Target;
895 }
896
897
898 /**
899 Internal function. Finds a consecutive free page range below
900 the requested address
901
902 @param MaxAddress The address that the range must be below
903 @param NoPages Number of pages needed
904 @param NewType The type of memory the range is going to be
905 turned into
906 @param Alignment Bits to align with
907
908 @return The base address of the range, or 0 if the range was not found.
909
910 **/
911 UINT64
912 FindFreePages (
913 IN UINT64 MaxAddress,
914 IN UINT64 NoPages,
915 IN EFI_MEMORY_TYPE NewType,
916 IN UINTN Alignment
917 )
918 {
919 UINT64 NewMaxAddress;
920 UINT64 Start;
921
922 NewMaxAddress = MaxAddress;
923
924 if (NewType >= 0 && NewType < EfiMaxMemoryType && NewMaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {
925 NewMaxAddress = mMemoryTypeStatistics[NewType].MaximumAddress;
926 } else {
927 if (NewMaxAddress > mDefaultMaximumAddress) {
928 NewMaxAddress = mDefaultMaximumAddress;
929 }
930 }
931
932 Start = CoreFindFreePagesI (NewMaxAddress, NoPages, NewType, Alignment);
933 if (Start == 0) {
934 Start = CoreFindFreePagesI (MaxAddress, NoPages, NewType, Alignment);
935 if (Start == 0) {
936 //
937 // Here means there may be no enough memory to use, so try to go through
938 // all the memory descript to promote the untested memory directly
939 //
940 PromoteMemoryResource ();
941
942 //
943 // Allocate memory again after the memory resource re-arranged
944 //
945 Start = CoreFindFreePagesI (MaxAddress, NoPages, NewType, Alignment);
946 }
947 }
948
949 return Start;
950 }
951
952
953
954 /**
955 Allocates pages from the memory map.
956
957 @param Type The type of allocation to perform
958 @param MemoryType The type of memory to turn the allocated pages
959 into
960 @param NumberOfPages The number of pages to allocate
961 @param Memory A pointer to receive the base allocated memory
962 address
963
964 @return Status. On success, Memory is filled in with the base address allocated
965 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in
966 spec.
967 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.
968 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.
969 @retval EFI_SUCCESS Pages successfully allocated.
970
971 **/
972 EFI_STATUS
973 EFIAPI
974 CoreAllocatePages (
975 IN EFI_ALLOCATE_TYPE Type,
976 IN EFI_MEMORY_TYPE MemoryType,
977 IN UINTN NumberOfPages,
978 IN OUT EFI_PHYSICAL_ADDRESS *Memory
979 )
980 {
981 EFI_STATUS Status;
982 UINT64 Start;
983 UINT64 MaxAddress;
984 UINTN Alignment;
985
986 if (Type < AllocateAnyPages || Type >= (UINTN) MaxAllocateType) {
987 return EFI_INVALID_PARAMETER;
988 }
989
990 if ((MemoryType >= EfiMaxMemoryType && MemoryType <= 0x7fffffff) ||
991 MemoryType == EfiConventionalMemory) {
992 return EFI_INVALID_PARAMETER;
993 }
994
995 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;
996
997 if (MemoryType == EfiACPIReclaimMemory ||
998 MemoryType == EfiACPIMemoryNVS ||
999 MemoryType == EfiRuntimeServicesCode ||
1000 MemoryType == EfiRuntimeServicesData) {
1001
1002 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;
1003 }
1004
1005 if (Type == AllocateAddress) {
1006 if ((*Memory & (Alignment - 1)) != 0) {
1007 return EFI_NOT_FOUND;
1008 }
1009 }
1010
1011 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;
1012 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
1013
1014 //
1015 // If this is for below a particular address, then
1016 //
1017 Start = *Memory;
1018
1019 //
1020 // The max address is the max natively addressable address for the processor
1021 //
1022 MaxAddress = EFI_MAX_ADDRESS;
1023
1024 if (Type == AllocateMaxAddress) {
1025 MaxAddress = Start;
1026 }
1027
1028 CoreAcquireMemoryLock ();
1029
1030 //
1031 // If not a specific address, then find an address to allocate
1032 //
1033 if (Type != AllocateAddress) {
1034 Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment);
1035 if (Start == 0) {
1036 Status = EFI_OUT_OF_RESOURCES;
1037 goto Done;
1038 }
1039 }
1040
1041 //
1042 // Convert pages from FreeMemory to the requested type
1043 //
1044 Status = CoreConvertPages (Start, NumberOfPages, MemoryType);
1045
1046 Done:
1047 CoreReleaseMemoryLock ();
1048
1049 if (!EFI_ERROR (Status)) {
1050 *Memory = Start;
1051 }
1052
1053 return Status;
1054 }
1055
1056
1057 /**
1058 Frees previous allocated pages.
1059
1060 @param Memory Base address of memory being freed
1061 @param NumberOfPages The number of pages to free
1062
1063 @retval EFI_NOT_FOUND Could not find the entry that covers the range
1064 @retval EFI_INVALID_PARAMETER Address not aligned
1065 @return EFI_SUCCESS -Pages successfully freed.
1066
1067 **/
1068 EFI_STATUS
1069 EFIAPI
1070 CoreFreePages (
1071 IN EFI_PHYSICAL_ADDRESS Memory,
1072 IN UINTN NumberOfPages
1073 )
1074 {
1075 EFI_STATUS Status;
1076 LIST_ENTRY *Link;
1077 MEMORY_MAP *Entry;
1078 UINTN Alignment;
1079
1080 //
1081 // Free the range
1082 //
1083 CoreAcquireMemoryLock ();
1084
1085 //
1086 // Find the entry that the covers the range
1087 //
1088 Entry = NULL;
1089 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1090 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1091 if (Entry->Start <= Memory && Entry->End > Memory) {
1092 break;
1093 }
1094 }
1095 if (Link == &gMemoryMap) {
1096 CoreReleaseMemoryLock ();
1097 return EFI_NOT_FOUND;
1098 }
1099
1100 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;
1101
1102 if (Entry->Type == EfiACPIReclaimMemory ||
1103 Entry->Type == EfiACPIMemoryNVS ||
1104 Entry->Type == EfiRuntimeServicesCode ||
1105 Entry->Type == EfiRuntimeServicesData) {
1106
1107 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;
1108
1109 }
1110
1111 if ((Memory & (Alignment - 1)) != 0) {
1112 CoreReleaseMemoryLock ();
1113 return EFI_INVALID_PARAMETER;
1114 }
1115
1116 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;
1117 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
1118
1119 Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);
1120
1121 CoreReleaseMemoryLock ();
1122
1123 if (EFI_ERROR (Status)) {
1124 return Status;
1125 }
1126
1127 //
1128 // Destroy the contents
1129 //
1130 if (Memory < EFI_MAX_ADDRESS) {
1131 DEBUG_CLEAR_MEMORY ((VOID *)(UINTN)Memory, NumberOfPages << EFI_PAGE_SHIFT);
1132 }
1133
1134 return Status;
1135 }
1136
1137
1138 /**
1139 This function returns a copy of the current memory map. The map is an array of
1140 memory descriptors, each of which describes a contiguous block of memory.
1141
1142 @param MemoryMapSize A pointer to the size, in bytes, of the
1143 MemoryMap buffer. On input, this is the size of
1144 the buffer allocated by the caller. On output,
1145 it is the size of the buffer returned by the
1146 firmware if the buffer was large enough, or the
1147 size of the buffer needed to contain the map if
1148 the buffer was too small.
1149 @param MemoryMap A pointer to the buffer in which firmware places
1150 the current memory map.
1151 @param MapKey A pointer to the location in which firmware
1152 returns the key for the current memory map.
1153 @param DescriptorSize A pointer to the location in which firmware
1154 returns the size, in bytes, of an individual
1155 EFI_MEMORY_DESCRIPTOR.
1156 @param DescriptorVersion A pointer to the location in which firmware
1157 returns the version number associated with the
1158 EFI_MEMORY_DESCRIPTOR.
1159
1160 @retval EFI_SUCCESS The memory map was returned in the MemoryMap
1161 buffer.
1162 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current
1163 buffer size needed to hold the memory map is
1164 returned in MemoryMapSize.
1165 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
1166
1167 **/
1168 EFI_STATUS
1169 EFIAPI
1170 CoreGetMemoryMap (
1171 IN OUT UINTN *MemoryMapSize,
1172 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
1173 OUT UINTN *MapKey,
1174 OUT UINTN *DescriptorSize,
1175 OUT UINT32 *DescriptorVersion
1176 )
1177 {
1178 EFI_STATUS Status;
1179 UINTN Size;
1180 UINTN BufferSize;
1181 UINTN NumberOfRuntimeEntries;
1182 LIST_ENTRY *Link;
1183 MEMORY_MAP *Entry;
1184 EFI_GCD_MAP_ENTRY *GcdMapEntry;
1185 EFI_MEMORY_TYPE Type;
1186
1187 //
1188 // Make sure the parameters are valid
1189 //
1190 if (MemoryMapSize == NULL) {
1191 return EFI_INVALID_PARAMETER;
1192 }
1193
1194 CoreAcquireGcdMemoryLock ();
1195
1196 //
1197 // Count the number of Reserved and MMIO entries that are marked for runtime use
1198 //
1199 NumberOfRuntimeEntries = 0;
1200 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {
1201 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1202 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||
1203 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {
1204 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {
1205 NumberOfRuntimeEntries++;
1206 }
1207 }
1208 }
1209
1210 Size = sizeof (EFI_MEMORY_DESCRIPTOR);
1211
1212 //
1213 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will
1214 // prevent people from having pointer math bugs in their code.
1215 // now you have to use *DescriptorSize to make things work.
1216 //
1217 Size += sizeof(UINT64) - (Size % sizeof (UINT64));
1218
1219 if (DescriptorSize != NULL) {
1220 *DescriptorSize = Size;
1221 }
1222
1223 if (DescriptorVersion != NULL) {
1224 *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;
1225 }
1226
1227 CoreAcquireMemoryLock ();
1228
1229 //
1230 // Compute the buffer size needed to fit the entire map
1231 //
1232 BufferSize = Size * NumberOfRuntimeEntries;
1233 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1234 BufferSize += Size;
1235 }
1236
1237 if (*MemoryMapSize < BufferSize) {
1238 Status = EFI_BUFFER_TOO_SMALL;
1239 goto Done;
1240 }
1241
1242 if (MemoryMap == NULL) {
1243 Status = EFI_INVALID_PARAMETER;
1244 goto Done;
1245 }
1246
1247 //
1248 // Build the map
1249 //
1250 ZeroMem (MemoryMap, BufferSize);
1251 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1252 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1253 ASSERT (Entry->VirtualStart == 0);
1254
1255 //
1256 // Convert internal map into an EFI_MEMORY_DESCRIPTOR
1257 //
1258 MemoryMap->Type = Entry->Type;
1259 MemoryMap->PhysicalStart = Entry->Start;
1260 MemoryMap->VirtualStart = Entry->VirtualStart;
1261 MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);
1262 //
1263 // If the memory type is EfiConventionalMemory, then determine if the range is part of a
1264 // memory type bin and needs to be converted to the same memory type as the rest of the
1265 // memory type bin in order to minimize EFI Memory Map changes across reboots. This
1266 // improves the chances for a successful S4 resume in the presence of minor page allocation
1267 // differences across reboots.
1268 //
1269 if (MemoryMap->Type == EfiConventionalMemory) {
1270 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {
1271 if (mMemoryTypeStatistics[Type].Special &&
1272 mMemoryTypeStatistics[Type].NumberOfPages > 0 &&
1273 Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress &&
1274 Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) {
1275 MemoryMap->Type = Type;
1276 }
1277 }
1278 }
1279 MemoryMap->Attribute = Entry->Attribute;
1280 if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {
1281 MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;
1282 }
1283
1284 MemoryMap = NextMemoryDescriptor (MemoryMap, Size);
1285 }
1286
1287 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {
1288 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
1289 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||
1290 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {
1291 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {
1292 //
1293 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries
1294 // that are marked for runtime use
1295 //
1296 MemoryMap->PhysicalStart = GcdMapEntry->BaseAddress;
1297 MemoryMap->VirtualStart = 0;
1298 MemoryMap->NumberOfPages = RShiftU64 ((GcdMapEntry->EndAddress - GcdMapEntry->BaseAddress + 1), EFI_PAGE_SHIFT);
1299 MemoryMap->Attribute = GcdMapEntry->Attributes & ~EFI_MEMORY_PORT_IO;
1300
1301 if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) {
1302 MemoryMap->Type = EfiReservedMemoryType;
1303 } else if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {
1304 if ((GcdMapEntry->Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {
1305 MemoryMap->Type = EfiMemoryMappedIOPortSpace;
1306 } else {
1307 MemoryMap->Type = EfiMemoryMappedIO;
1308 }
1309 }
1310
1311 MemoryMap = NextMemoryDescriptor (MemoryMap, Size);
1312 }
1313 }
1314 }
1315
1316 Status = EFI_SUCCESS;
1317
1318 Done:
1319
1320 CoreReleaseMemoryLock ();
1321
1322 CoreReleaseGcdMemoryLock ();
1323
1324 //
1325 // Update the map key finally
1326 //
1327 if (MapKey != NULL) {
1328 *MapKey = mMemoryMapKey;
1329 }
1330
1331 *MemoryMapSize = BufferSize;
1332
1333 return Status;
1334 }
1335
1336
1337 /**
1338 Internal function. Used by the pool functions to allocate pages
1339 to back pool allocation requests.
1340
1341 @param PoolType The type of memory for the new pool pages
1342 @param NumberOfPages No of pages to allocate
1343 @param Alignment Bits to align.
1344
1345 @return The allocated memory, or NULL
1346
1347 **/
1348 VOID *
1349 CoreAllocatePoolPages (
1350 IN EFI_MEMORY_TYPE PoolType,
1351 IN UINTN NumberOfPages,
1352 IN UINTN Alignment
1353 )
1354 {
1355 UINT64 Start;
1356
1357 //
1358 // Find the pages to convert
1359 //
1360 Start = FindFreePages (EFI_MAX_ADDRESS, NumberOfPages, PoolType, Alignment);
1361
1362 //
1363 // Convert it to boot services data
1364 //
1365 if (Start == 0) {
1366 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", NumberOfPages));
1367 } else {
1368 CoreConvertPages (Start, NumberOfPages, PoolType);
1369 }
1370
1371 return (VOID *)(UINTN) Start;
1372 }
1373
1374
1375 /**
1376 Internal function. Frees pool pages allocated via AllocatePoolPages ()
1377
1378 @param Memory The base address to free
1379 @param NumberOfPages The number of pages to free
1380
1381 **/
1382 VOID
1383 CoreFreePoolPages (
1384 IN EFI_PHYSICAL_ADDRESS Memory,
1385 IN UINTN NumberOfPages
1386 )
1387 {
1388 CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);
1389 }
1390
1391
1392
1393 /**
1394 Make sure the memory map is following all the construction rules,
1395 it is the last time to check memory map error before exit boot services.
1396
1397 @param MapKey Memory map key
1398
1399 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction
1400 rules.
1401 @retval EFI_SUCCESS Valid memory map.
1402
1403 **/
1404 EFI_STATUS
1405 CoreTerminateMemoryMap (
1406 IN UINTN MapKey
1407 )
1408 {
1409 EFI_STATUS Status;
1410 LIST_ENTRY *Link;
1411 MEMORY_MAP *Entry;
1412
1413 Status = EFI_SUCCESS;
1414
1415 CoreAcquireMemoryLock ();
1416
1417 if (MapKey == mMemoryMapKey) {
1418
1419 //
1420 // Make sure the memory map is following all the construction rules
1421 // This is the last chance we will be able to display any messages on
1422 // the console devices.
1423 //
1424
1425 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {
1426 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);
1427 if ((Entry->Attribute & EFI_MEMORY_RUNTIME) != 0) {
1428 if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS) {
1429 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));
1430 Status = EFI_INVALID_PARAMETER;
1431 goto Done;
1432 }
1433 if ((Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {
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 if (((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {
1439 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));
1440 Status = EFI_INVALID_PARAMETER;
1441 goto Done;
1442 }
1443 }
1444 }
1445
1446 //
1447 // The map key they gave us matches what we expect. Fall through and
1448 // return success. In an ideal world we would clear out all of
1449 // EfiBootServicesCode and EfiBootServicesData. However this function
1450 // is not the last one called by ExitBootServices(), so we have to
1451 // preserve the memory contents.
1452 //
1453 } else {
1454 Status = EFI_INVALID_PARAMETER;
1455 }
1456
1457 Done:
1458 CoreReleaseMemoryLock ();
1459
1460 return Status;
1461 }
1462
1463
1464
1465
1466
1467
1468
1469
1470