]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Dxe/Mem/Page.c
MdeModulePkg DxeCore: Return memory type from internal free pool/pages
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Mem / Page.c
... / ...
CommitLineData
1/** @file\r
2 UEFI Memory page management functions.\r
3\r
4Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "DxeMain.h"\r
16#include "Imem.h"\r
17\r
18#define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT (EFI_PAGE_SIZE)\r
19\r
20//\r
21// Entry for tracking the memory regions for each memory type to coalesce similar memory types\r
22//\r
23typedef struct {\r
24 EFI_PHYSICAL_ADDRESS BaseAddress;\r
25 EFI_PHYSICAL_ADDRESS MaximumAddress;\r
26 UINT64 CurrentNumberOfPages;\r
27 UINT64 NumberOfPages;\r
28 UINTN InformationIndex;\r
29 BOOLEAN Special;\r
30 BOOLEAN Runtime;\r
31} EFI_MEMORY_TYPE_STATISTICS;\r
32\r
33//\r
34// MemoryMap - The current memory map\r
35//\r
36UINTN mMemoryMapKey = 0;\r
37\r
38#define MAX_MAP_DEPTH 6\r
39\r
40///\r
41/// mMapDepth - depth of new descriptor stack\r
42///\r
43UINTN mMapDepth = 0;\r
44///\r
45/// mMapStack - space to use as temp storage to build new map descriptors\r
46///\r
47MEMORY_MAP mMapStack[MAX_MAP_DEPTH];\r
48UINTN mFreeMapStack = 0;\r
49///\r
50/// This list maintain the free memory map list\r
51///\r
52LIST_ENTRY mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList);\r
53BOOLEAN mMemoryTypeInformationInitialized = FALSE;\r
54\r
55EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {\r
56 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiReservedMemoryType\r
57 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderCode\r
58 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderData\r
59 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesCode\r
60 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesData\r
61 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesCode\r
62 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesData\r
63 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiConventionalMemory\r
64 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiUnusableMemory\r
65 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIReclaimMemory\r
66 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIMemoryNVS\r
67 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIO\r
68 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIOPortSpace\r
69 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiPalCode\r
70 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiPersistentMemory\r
71 { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE } // EfiMaxMemoryType\r
72};\r
73\r
74EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress = MAX_ADDRESS;\r
75EFI_PHYSICAL_ADDRESS mDefaultBaseAddress = MAX_ADDRESS;\r
76\r
77EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {\r
78 { EfiReservedMemoryType, 0 },\r
79 { EfiLoaderCode, 0 },\r
80 { EfiLoaderData, 0 },\r
81 { EfiBootServicesCode, 0 },\r
82 { EfiBootServicesData, 0 },\r
83 { EfiRuntimeServicesCode, 0 },\r
84 { EfiRuntimeServicesData, 0 },\r
85 { EfiConventionalMemory, 0 },\r
86 { EfiUnusableMemory, 0 },\r
87 { EfiACPIReclaimMemory, 0 },\r
88 { EfiACPIMemoryNVS, 0 },\r
89 { EfiMemoryMappedIO, 0 },\r
90 { EfiMemoryMappedIOPortSpace, 0 },\r
91 { EfiPalCode, 0 },\r
92 { EfiPersistentMemory, 0 },\r
93 { EfiMaxMemoryType, 0 }\r
94};\r
95//\r
96// Only used when load module at fixed address feature is enabled. True means the memory is alreay successfully allocated\r
97// and ready to load the module in to specified address.or else, the memory is not ready and module will be loaded at a \r
98// address assigned by DXE core.\r
99//\r
100GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN gLoadFixedAddressCodeMemoryReady = FALSE;\r
101\r
102/**\r
103 Enter critical section by gaining lock on gMemoryLock.\r
104\r
105**/\r
106VOID\r
107CoreAcquireMemoryLock (\r
108 VOID\r
109 )\r
110{\r
111 CoreAcquireLock (&gMemoryLock);\r
112}\r
113\r
114\r
115\r
116/**\r
117 Exit critical section by releasing lock on gMemoryLock.\r
118\r
119**/\r
120VOID\r
121CoreReleaseMemoryLock (\r
122 VOID\r
123 )\r
124{\r
125 CoreReleaseLock (&gMemoryLock);\r
126}\r
127\r
128\r
129\r
130\r
131/**\r
132 Internal function. Removes a descriptor entry.\r
133\r
134 @param Entry The entry to remove\r
135\r
136**/\r
137VOID\r
138RemoveMemoryMapEntry (\r
139 IN OUT MEMORY_MAP *Entry\r
140 )\r
141{\r
142 RemoveEntryList (&Entry->Link);\r
143 Entry->Link.ForwardLink = NULL;\r
144\r
145 if (Entry->FromPages) {\r
146 //\r
147 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList\r
148 //\r
149 InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
150 }\r
151}\r
152\r
153/**\r
154 Internal function. Adds a ranges to the memory map.\r
155 The range must not already exist in the map.\r
156\r
157 @param Type The type of memory range to add\r
158 @param Start The starting address in the memory range Must be\r
159 paged aligned\r
160 @param End The last address in the range Must be the last\r
161 byte of a page\r
162 @param Attribute The attributes of the memory range to add\r
163\r
164**/\r
165VOID\r
166CoreAddRange (\r
167 IN EFI_MEMORY_TYPE Type,\r
168 IN EFI_PHYSICAL_ADDRESS Start,\r
169 IN EFI_PHYSICAL_ADDRESS End,\r
170 IN UINT64 Attribute\r
171 )\r
172{\r
173 LIST_ENTRY *Link;\r
174 MEMORY_MAP *Entry;\r
175\r
176 ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
177 ASSERT (End > Start) ;\r
178\r
179 ASSERT_LOCKED (&gMemoryLock);\r
180\r
181 DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type));\r
182 \r
183 //\r
184 // If memory of type EfiConventionalMemory is being added that includes the page \r
185 // starting at address 0, then zero the page starting at address 0. This has \r
186 // two benifits. It helps find NULL pointer bugs and it also maximizes \r
187 // compatibility with operating systems that may evaluate memory in this page \r
188 // for legacy data structures. If memory of any other type is added starting \r
189 // at address 0, then do not zero the page at address 0 because the page is being \r
190 // used for other purposes.\r
191 // \r
192 if (Type == EfiConventionalMemory && Start == 0 && (End >= EFI_PAGE_SIZE - 1)) {\r
193 SetMem ((VOID *)(UINTN)Start, EFI_PAGE_SIZE, 0);\r
194 }\r
195 \r
196 //\r
197 // Memory map being altered so updated key\r
198 //\r
199 mMemoryMapKey += 1;\r
200\r
201 //\r
202 // UEFI 2.0 added an event group for notificaiton on memory map changes.\r
203 // So we need to signal this Event Group every time the memory map changes.\r
204 // If we are in EFI 1.10 compatability mode no event groups will be\r
205 // found and nothing will happen we we call this function. These events\r
206 // will get signaled but since a lock is held around the call to this\r
207 // function the notificaiton events will only be called after this funciton\r
208 // returns and the lock is released.\r
209 //\r
210 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid);\r
211\r
212 //\r
213 // Look for adjoining memory descriptor\r
214 //\r
215\r
216 // Two memory descriptors can only be merged if they have the same Type\r
217 // and the same Attribute\r
218 //\r
219\r
220 Link = gMemoryMap.ForwardLink;\r
221 while (Link != &gMemoryMap) {\r
222 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
223 Link = Link->ForwardLink;\r
224\r
225 if (Entry->Type != Type) {\r
226 continue;\r
227 }\r
228\r
229 if (Entry->Attribute != Attribute) {\r
230 continue;\r
231 }\r
232\r
233 if (Entry->End + 1 == Start) {\r
234\r
235 Start = Entry->Start;\r
236 RemoveMemoryMapEntry (Entry);\r
237\r
238 } else if (Entry->Start == End + 1) {\r
239\r
240 End = Entry->End;\r
241 RemoveMemoryMapEntry (Entry);\r
242 }\r
243 }\r
244\r
245 //\r
246 // Add descriptor\r
247 //\r
248\r
249 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
250 mMapStack[mMapDepth].FromPages = FALSE;\r
251 mMapStack[mMapDepth].Type = Type;\r
252 mMapStack[mMapDepth].Start = Start;\r
253 mMapStack[mMapDepth].End = End;\r
254 mMapStack[mMapDepth].VirtualStart = 0;\r
255 mMapStack[mMapDepth].Attribute = Attribute;\r
256 InsertTailList (&gMemoryMap, &mMapStack[mMapDepth].Link);\r
257\r
258 mMapDepth += 1;\r
259 ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
260\r
261 return ;\r
262}\r
263\r
264/**\r
265 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.\r
266 If the list is emtry, then allocate a new page to refuel the list.\r
267 Please Note this algorithm to allocate the memory map descriptor has a property\r
268 that the memory allocated for memory entries always grows, and will never really be freed\r
269 For example, if the current boot uses 2000 memory map entries at the maximum point, but\r
270 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950\r
271 memory map entries is still allocated from EfiBootServicesMemory.\r
272\r
273\r
274 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList\r
275\r
276**/\r
277MEMORY_MAP *\r
278AllocateMemoryMapEntry (\r
279 VOID\r
280 )\r
281{\r
282 MEMORY_MAP* FreeDescriptorEntries;\r
283 MEMORY_MAP* Entry;\r
284 UINTN Index;\r
285\r
286 if (IsListEmpty (&mFreeMemoryMapEntryList)) {\r
287 //\r
288 // The list is empty, to allocate one page to refuel the list\r
289 //\r
290 FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION);\r
291 if(FreeDescriptorEntries != NULL) {\r
292 //\r
293 // Enque the free memmory map entries into the list\r
294 //\r
295 for (Index = 0; Index< DEFAULT_PAGE_ALLOCATION / sizeof(MEMORY_MAP); Index++) {\r
296 FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;\r
297 InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);\r
298 }\r
299 } else {\r
300 return NULL;\r
301 }\r
302 }\r
303 //\r
304 // dequeue the first descriptor from the list\r
305 //\r
306 Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
307 RemoveEntryList (&Entry->Link);\r
308\r
309 return Entry;\r
310}\r
311\r
312\r
313/**\r
314 Internal function. Moves any memory descriptors that are on the\r
315 temporary descriptor stack to heap.\r
316\r
317**/\r
318VOID\r
319CoreFreeMemoryMapStack (\r
320 VOID\r
321 )\r
322{\r
323 MEMORY_MAP *Entry;\r
324 MEMORY_MAP *Entry2;\r
325 LIST_ENTRY *Link2;\r
326\r
327 ASSERT_LOCKED (&gMemoryLock);\r
328\r
329 //\r
330 // If already freeing the map stack, then return\r
331 //\r
332 if (mFreeMapStack != 0) {\r
333 return ;\r
334 }\r
335\r
336 //\r
337 // Move the temporary memory descriptor stack into pool\r
338 //\r
339 mFreeMapStack += 1;\r
340\r
341 while (mMapDepth != 0) {\r
342 //\r
343 // Deque an memory map entry from mFreeMemoryMapEntryList\r
344 //\r
345 Entry = AllocateMemoryMapEntry ();\r
346\r
347 ASSERT (Entry);\r
348\r
349 //\r
350 // Update to proper entry\r
351 //\r
352 mMapDepth -= 1;\r
353\r
354 if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {\r
355\r
356 //\r
357 // Move this entry to general memory\r
358 //\r
359 RemoveEntryList (&mMapStack[mMapDepth].Link);\r
360 mMapStack[mMapDepth].Link.ForwardLink = NULL;\r
361\r
362 CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));\r
363 Entry->FromPages = TRUE;\r
364\r
365 //\r
366 // Find insertion location\r
367 //\r
368 for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) {\r
369 Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
370 if (Entry2->FromPages && Entry2->Start > Entry->Start) {\r
371 break;\r
372 }\r
373 }\r
374\r
375 InsertTailList (Link2, &Entry->Link);\r
376\r
377 } else {\r
378 //\r
379 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,\r
380 // so here no need to move it to memory.\r
381 //\r
382 InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
383 }\r
384 }\r
385\r
386 mFreeMapStack -= 1;\r
387}\r
388\r
389/**\r
390 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.\r
391\r
392**/\r
393BOOLEAN\r
394PromoteMemoryResource (\r
395 VOID\r
396 )\r
397{\r
398 LIST_ENTRY *Link;\r
399 EFI_GCD_MAP_ENTRY *Entry;\r
400 BOOLEAN Promoted;\r
401\r
402 DEBUG ((DEBUG_PAGE, "Promote the memory resource\n"));\r
403\r
404 CoreAcquireGcdMemoryLock ();\r
405\r
406 Promoted = FALSE;\r
407 Link = mGcdMemorySpaceMap.ForwardLink;\r
408 while (Link != &mGcdMemorySpaceMap) {\r
409\r
410 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
411\r
412 if (Entry->GcdMemoryType == EfiGcdMemoryTypeReserved &&\r
413 Entry->EndAddress < MAX_ADDRESS &&\r
414 (Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r
415 (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) {\r
416 //\r
417 // Update the GCD map\r
418 //\r
419 if ((Entry->Capabilities & EFI_MEMORY_MORE_RELIABLE) == EFI_MEMORY_MORE_RELIABLE) {\r
420 Entry->GcdMemoryType = EfiGcdMemoryTypeMoreReliable;\r
421 } else {\r
422 Entry->GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r
423 }\r
424 Entry->Capabilities |= EFI_MEMORY_TESTED;\r
425 Entry->ImageHandle = gDxeCoreImageHandle;\r
426 Entry->DeviceHandle = NULL;\r
427\r
428 //\r
429 // Add to allocable system memory resource\r
430 //\r
431\r
432 CoreAddRange (\r
433 EfiConventionalMemory,\r
434 Entry->BaseAddress,\r
435 Entry->EndAddress,\r
436 Entry->Capabilities & ~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r
437 );\r
438 CoreFreeMemoryMapStack ();\r
439\r
440 Promoted = TRUE;\r
441 }\r
442\r
443 Link = Link->ForwardLink;\r
444 }\r
445\r
446 CoreReleaseGcdMemoryLock ();\r
447\r
448 return Promoted;\r
449}\r
450/**\r
451 This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD \r
452 PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the \r
453 size of boot time and runtime code.\r
454\r
455**/\r
456VOID\r
457CoreLoadingFixedAddressHook (\r
458 VOID\r
459 )\r
460{\r
461 UINT32 RuntimeCodePageNumber;\r
462 UINT32 BootTimeCodePageNumber;\r
463 EFI_PHYSICAL_ADDRESS RuntimeCodeBase;\r
464 EFI_PHYSICAL_ADDRESS BootTimeCodeBase;\r
465 EFI_STATUS Status;\r
466\r
467 //\r
468 // Make sure these 2 areas are not initialzied.\r
469 //\r
470 if (!gLoadFixedAddressCodeMemoryReady) { \r
471 RuntimeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
472 BootTimeCodePageNumber= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
473 RuntimeCodeBase = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - EFI_PAGES_TO_SIZE (RuntimeCodePageNumber));\r
474 BootTimeCodeBase = (EFI_PHYSICAL_ADDRESS)(RuntimeCodeBase - EFI_PAGES_TO_SIZE (BootTimeCodePageNumber));\r
475 //\r
476 // Try to allocate runtime memory.\r
477 //\r
478 Status = CoreAllocatePages (\r
479 AllocateAddress,\r
480 EfiRuntimeServicesCode,\r
481 RuntimeCodePageNumber,\r
482 &RuntimeCodeBase\r
483 );\r
484 if (EFI_ERROR(Status)) {\r
485 //\r
486 // Runtime memory allocation failed \r
487 //\r
488 return;\r
489 }\r
490 //\r
491 // Try to allocate boot memory.\r
492 //\r
493 Status = CoreAllocatePages (\r
494 AllocateAddress,\r
495 EfiBootServicesCode,\r
496 BootTimeCodePageNumber,\r
497 &BootTimeCodeBase\r
498 );\r
499 if (EFI_ERROR(Status)) {\r
500 //\r
501 // boot memory allocation failed. Free Runtime code range and will try the allocation again when \r
502 // new memory range is installed.\r
503 //\r
504 CoreFreePages (\r
505 RuntimeCodeBase,\r
506 RuntimeCodePageNumber\r
507 );\r
508 return;\r
509 }\r
510 gLoadFixedAddressCodeMemoryReady = TRUE;\r
511 } \r
512 return;\r
513} \r
514\r
515/**\r
516 Called to initialize the memory map and add descriptors to\r
517 the current descriptor list.\r
518 The first descriptor that is added must be general usable\r
519 memory as the addition allocates heap.\r
520\r
521 @param Type The type of memory to add\r
522 @param Start The starting address in the memory range Must be\r
523 page aligned\r
524 @param NumberOfPages The number of pages in the range\r
525 @param Attribute Attributes of the memory to add\r
526\r
527 @return None. The range is added to the memory map\r
528\r
529**/\r
530VOID\r
531CoreAddMemoryDescriptor (\r
532 IN EFI_MEMORY_TYPE Type,\r
533 IN EFI_PHYSICAL_ADDRESS Start,\r
534 IN UINT64 NumberOfPages,\r
535 IN UINT64 Attribute\r
536 )\r
537{\r
538 EFI_PHYSICAL_ADDRESS End;\r
539 EFI_STATUS Status;\r
540 UINTN Index;\r
541 UINTN FreeIndex;\r
542 \r
543 if ((Start & EFI_PAGE_MASK) != 0) {\r
544 return;\r
545 }\r
546\r
547 if (Type >= EfiMaxMemoryType && Type < MEMORY_TYPE_OEM_RESERVED_MIN) {\r
548 return;\r
549 }\r
550 CoreAcquireMemoryLock ();\r
551 End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
552 CoreAddRange (Type, Start, End, Attribute);\r
553 CoreFreeMemoryMapStack ();\r
554 CoreReleaseMemoryLock ();\r
555\r
556 //\r
557 // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type\r
558 //\r
559 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
560 CoreLoadingFixedAddressHook();\r
561 }\r
562 \r
563 //\r
564 // Check to see if the statistics for the different memory types have already been established\r
565 //\r
566 if (mMemoryTypeInformationInitialized) {\r
567 return;\r
568 }\r
569\r
570 \r
571 //\r
572 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array\r
573 //\r
574 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
575 //\r
576 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
577 //\r
578 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
579 if ((UINT32)Type > EfiMaxMemoryType) {\r
580 continue;\r
581 }\r
582 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
583 //\r
584 // Allocate pages for the current memory type from the top of available memory\r
585 //\r
586 Status = CoreAllocatePages (\r
587 AllocateAnyPages,\r
588 Type,\r
589 gMemoryTypeInformation[Index].NumberOfPages,\r
590 &mMemoryTypeStatistics[Type].BaseAddress\r
591 );\r
592 if (EFI_ERROR (Status)) {\r
593 //\r
594 // If an error occurs allocating the pages for the current memory type, then\r
595 // free all the pages allocates for the previous memory types and return. This\r
596 // operation with be retied when/if more memory is added to the system\r
597 //\r
598 for (FreeIndex = 0; FreeIndex < Index; FreeIndex++) {\r
599 //\r
600 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
601 //\r
602 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type);\r
603 if ((UINT32)Type > EfiMaxMemoryType) {\r
604 continue;\r
605 }\r
606\r
607 if (gMemoryTypeInformation[FreeIndex].NumberOfPages != 0) {\r
608 CoreFreePages (\r
609 mMemoryTypeStatistics[Type].BaseAddress,\r
610 gMemoryTypeInformation[FreeIndex].NumberOfPages\r
611 );\r
612 mMemoryTypeStatistics[Type].BaseAddress = 0;\r
613 mMemoryTypeStatistics[Type].MaximumAddress = MAX_ADDRESS;\r
614 }\r
615 }\r
616 return;\r
617 }\r
618\r
619 //\r
620 // Compute the address at the top of the current statistics\r
621 //\r
622 mMemoryTypeStatistics[Type].MaximumAddress =\r
623 mMemoryTypeStatistics[Type].BaseAddress +\r
624 LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
625\r
626 //\r
627 // If the current base address is the lowest address so far, then update the default\r
628 // maximum address\r
629 //\r
630 if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) {\r
631 mDefaultMaximumAddress = mMemoryTypeStatistics[Type].BaseAddress - 1;\r
632 }\r
633 }\r
634 }\r
635\r
636 //\r
637 // There was enough system memory for all the the memory types were allocated. So,\r
638 // those memory areas can be freed for future allocations, and all future memory\r
639 // allocations can occur within their respective bins\r
640 //\r
641 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
642 //\r
643 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
644 //\r
645 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
646 if ((UINT32)Type > EfiMaxMemoryType) {\r
647 continue;\r
648 }\r
649 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
650 CoreFreePages (\r
651 mMemoryTypeStatistics[Type].BaseAddress,\r
652 gMemoryTypeInformation[Index].NumberOfPages\r
653 );\r
654 mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages;\r
655 gMemoryTypeInformation[Index].NumberOfPages = 0;\r
656 }\r
657 }\r
658\r
659 //\r
660 // If the number of pages reserved for a memory type is 0, then all allocations for that type\r
661 // should be in the default range.\r
662 //\r
663 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
664 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
665 if (Type == (EFI_MEMORY_TYPE)gMemoryTypeInformation[Index].Type) {\r
666 mMemoryTypeStatistics[Type].InformationIndex = Index;\r
667 }\r
668 }\r
669 mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0;\r
670 if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ADDRESS) {\r
671 mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress;\r
672 }\r
673 }\r
674\r
675 mMemoryTypeInformationInitialized = TRUE;\r
676}\r
677\r
678\r
679/**\r
680 Internal function. Converts a memory range to the specified type or attributes.\r
681 The range must exist in the memory map. Either ChangingType or\r
682 ChangingAttributes must be set, but not both.\r
683\r
684 @param Start The first address of the range Must be page\r
685 aligned\r
686 @param NumberOfPages The number of pages to convert\r
687 @param ChangingType Boolean indicating that type value should be changed\r
688 @param NewType The new type for the memory range\r
689 @param ChangingAttributes Boolean indicating that attributes value should be changed\r
690 @param NewAttributes The new attributes for the memory range\r
691\r
692 @retval EFI_INVALID_PARAMETER Invalid parameter\r
693 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r
694 range or convertion not allowed.\r
695 @retval EFI_SUCCESS Successfully converts the memory range to the\r
696 specified type.\r
697\r
698**/\r
699EFI_STATUS\r
700CoreConvertPagesEx (\r
701 IN UINT64 Start,\r
702 IN UINT64 NumberOfPages,\r
703 IN BOOLEAN ChangingType,\r
704 IN EFI_MEMORY_TYPE NewType,\r
705 IN BOOLEAN ChangingAttributes,\r
706 IN UINT64 NewAttributes\r
707 )\r
708{\r
709\r
710 UINT64 NumberOfBytes;\r
711 UINT64 End;\r
712 UINT64 RangeEnd;\r
713 UINT64 Attribute;\r
714 EFI_MEMORY_TYPE MemType;\r
715 LIST_ENTRY *Link;\r
716 MEMORY_MAP *Entry;\r
717\r
718 Entry = NULL;\r
719 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
720 End = Start + NumberOfBytes - 1;\r
721\r
722 ASSERT (NumberOfPages);\r
723 ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
724 ASSERT (End > Start) ;\r
725 ASSERT_LOCKED (&gMemoryLock);\r
726 ASSERT ( (ChangingType == FALSE) || (ChangingAttributes == FALSE) );\r
727\r
728 if (NumberOfPages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start >= End)) {\r
729 return EFI_INVALID_PARAMETER;\r
730 }\r
731\r
732 //\r
733 // Convert the entire range\r
734 //\r
735\r
736 while (Start < End) {\r
737\r
738 //\r
739 // Find the entry that the covers the range\r
740 //\r
741 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
742 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
743\r
744 if (Entry->Start <= Start && Entry->End > Start) {\r
745 break;\r
746 }\r
747 }\r
748\r
749 if (Link == &gMemoryMap) {\r
750 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: failed to find range %lx - %lx\n", Start, End));\r
751 return EFI_NOT_FOUND;\r
752 }\r
753\r
754 //\r
755 // Convert range to the end, or to the end of the descriptor\r
756 // if that's all we've got\r
757 //\r
758 RangeEnd = End;\r
759\r
760 ASSERT (Entry != NULL);\r
761 if (Entry->End < End) {\r
762 RangeEnd = Entry->End;\r
763 }\r
764\r
765 if (ChangingType) {\r
766 DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to type %d\n", Start, RangeEnd, NewType));\r
767 }\r
768 if (ChangingAttributes) {\r
769 DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to attr %lx\n", Start, RangeEnd, NewAttributes));\r
770 }\r
771\r
772 if (ChangingType) {\r
773 //\r
774 // Debug code - verify conversion is allowed\r
775 //\r
776 if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) {\r
777 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types\n"));\r
778 return EFI_NOT_FOUND;\r
779 }\r
780\r
781 //\r
782 // Update counters for the number of pages allocated to each memory type\r
783 //\r
784 if ((UINT32)Entry->Type < EfiMaxMemoryType) {\r
785 if ((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) ||\r
786 (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {\r
787 if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {\r
788 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;\r
789 } else {\r
790 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;\r
791 }\r
792 }\r
793 }\r
794\r
795 if ((UINT32)NewType < EfiMaxMemoryType) {\r
796 if ((Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) ||\r
797 (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {\r
798 mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;\r
799 if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {\r
800 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;\r
801 }\r
802 }\r
803 }\r
804 }\r
805\r
806 //\r
807 // Pull range out of descriptor\r
808 //\r
809 if (Entry->Start == Start) {\r
810\r
811 //\r
812 // Clip start\r
813 //\r
814 Entry->Start = RangeEnd + 1;\r
815\r
816 } else if (Entry->End == RangeEnd) {\r
817\r
818 //\r
819 // Clip end\r
820 //\r
821 Entry->End = Start - 1;\r
822\r
823 } else {\r
824\r
825 //\r
826 // Pull it out of the center, clip current\r
827 //\r
828\r
829 //\r
830 // Add a new one\r
831 //\r
832 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
833 mMapStack[mMapDepth].FromPages = FALSE;\r
834 mMapStack[mMapDepth].Type = Entry->Type;\r
835 mMapStack[mMapDepth].Start = RangeEnd+1;\r
836 mMapStack[mMapDepth].End = Entry->End;\r
837\r
838 //\r
839 // Inherit Attribute from the Memory Descriptor that is being clipped\r
840 //\r
841 mMapStack[mMapDepth].Attribute = Entry->Attribute;\r
842\r
843 Entry->End = Start - 1;\r
844 ASSERT (Entry->Start < Entry->End);\r
845\r
846 Entry = &mMapStack[mMapDepth];\r
847 InsertTailList (&gMemoryMap, &Entry->Link);\r
848\r
849 mMapDepth += 1;\r
850 ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
851 }\r
852\r
853 //\r
854 // The new range inherits the same Attribute as the Entry\r
855 // it is being cut out of unless attributes are being changed\r
856 //\r
857 if (ChangingType) {\r
858 Attribute = Entry->Attribute;\r
859 MemType = NewType;\r
860 } else {\r
861 Attribute = NewAttributes;\r
862 MemType = Entry->Type;\r
863 }\r
864\r
865 //\r
866 // If the descriptor is empty, then remove it from the map\r
867 //\r
868 if (Entry->Start == Entry->End + 1) {\r
869 RemoveMemoryMapEntry (Entry);\r
870 Entry = NULL;\r
871 }\r
872\r
873 //\r
874 // Add our new range in\r
875 //\r
876 CoreAddRange (MemType, Start, RangeEnd, Attribute);\r
877 if (ChangingType && (MemType == EfiConventionalMemory)) {\r
878 //\r
879 // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this\r
880 // macro will ASSERT() if address is 0. Instead, CoreAddRange() guarantees\r
881 // that the page starting at address 0 is always filled with zeros.\r
882 //\r
883 if (Start == 0) {\r
884 if (RangeEnd > EFI_PAGE_SIZE) {\r
885 DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) EFI_PAGE_SIZE, (UINTN) (RangeEnd - EFI_PAGE_SIZE + 1));\r
886 }\r
887 } else {\r
888 DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) Start, (UINTN) (RangeEnd - Start + 1));\r
889 }\r
890 }\r
891\r
892 //\r
893 // Move any map descriptor stack to general pool\r
894 //\r
895 CoreFreeMemoryMapStack ();\r
896\r
897 //\r
898 // Bump the starting address, and convert the next range\r
899 //\r
900 Start = RangeEnd + 1;\r
901 }\r
902\r
903 //\r
904 // Converted the whole range, done\r
905 //\r
906\r
907 return EFI_SUCCESS;\r
908}\r
909\r
910\r
911/**\r
912 Internal function. Converts a memory range to the specified type.\r
913 The range must exist in the memory map.\r
914\r
915 @param Start The first address of the range Must be page\r
916 aligned\r
917 @param NumberOfPages The number of pages to convert\r
918 @param NewType The new type for the memory range\r
919\r
920 @retval EFI_INVALID_PARAMETER Invalid parameter\r
921 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r
922 range or convertion not allowed.\r
923 @retval EFI_SUCCESS Successfully converts the memory range to the\r
924 specified type.\r
925\r
926**/\r
927EFI_STATUS\r
928CoreConvertPages (\r
929 IN UINT64 Start,\r
930 IN UINT64 NumberOfPages,\r
931 IN EFI_MEMORY_TYPE NewType\r
932 )\r
933{\r
934 return CoreConvertPagesEx(Start, NumberOfPages, TRUE, NewType, FALSE, 0);\r
935}\r
936\r
937\r
938/**\r
939 Internal function. Converts a memory range to use new attributes.\r
940\r
941 @param Start The first address of the range Must be page\r
942 aligned\r
943 @param NumberOfPages The number of pages to convert\r
944 @param NewAttributes The new attributes value for the range.\r
945\r
946**/\r
947VOID\r
948CoreUpdateMemoryAttributes (\r
949 IN EFI_PHYSICAL_ADDRESS Start,\r
950 IN UINT64 NumberOfPages,\r
951 IN UINT64 NewAttributes\r
952 )\r
953{\r
954 CoreAcquireMemoryLock ();\r
955\r
956 //\r
957 // Update the attributes to the new value\r
958 //\r
959 CoreConvertPagesEx(Start, NumberOfPages, FALSE, (EFI_MEMORY_TYPE)0, TRUE, NewAttributes);\r
960\r
961 CoreReleaseMemoryLock ();\r
962}\r
963\r
964\r
965/**\r
966 Internal function. Finds a consecutive free page range below\r
967 the requested address.\r
968\r
969 @param MaxAddress The address that the range must be below\r
970 @param MinAddress The address that the range must be above\r
971 @param NumberOfPages Number of pages needed\r
972 @param NewType The type of memory the range is going to be\r
973 turned into\r
974 @param Alignment Bits to align with\r
975\r
976 @return The base address of the range, or 0 if the range was not found\r
977\r
978**/\r
979UINT64\r
980CoreFindFreePagesI (\r
981 IN UINT64 MaxAddress,\r
982 IN UINT64 MinAddress,\r
983 IN UINT64 NumberOfPages,\r
984 IN EFI_MEMORY_TYPE NewType,\r
985 IN UINTN Alignment\r
986 )\r
987{\r
988 UINT64 NumberOfBytes;\r
989 UINT64 Target;\r
990 UINT64 DescStart;\r
991 UINT64 DescEnd;\r
992 UINT64 DescNumberOfBytes;\r
993 LIST_ENTRY *Link;\r
994 MEMORY_MAP *Entry;\r
995\r
996 if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages == 0)) {\r
997 return 0;\r
998 }\r
999\r
1000 if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {\r
1001\r
1002 //\r
1003 // If MaxAddress is not aligned to the end of a page\r
1004 //\r
1005\r
1006 //\r
1007 // Change MaxAddress to be 1 page lower\r
1008 //\r
1009 MaxAddress -= (EFI_PAGE_MASK + 1);\r
1010\r
1011 //\r
1012 // Set MaxAddress to a page boundary\r
1013 //\r
1014 MaxAddress &= ~(UINT64)EFI_PAGE_MASK;\r
1015\r
1016 //\r
1017 // Set MaxAddress to end of the page\r
1018 //\r
1019 MaxAddress |= EFI_PAGE_MASK;\r
1020 }\r
1021\r
1022 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
1023 Target = 0;\r
1024\r
1025 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1026 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1027\r
1028 //\r
1029 // If it's not a free entry, don't bother with it\r
1030 //\r
1031 if (Entry->Type != EfiConventionalMemory) {\r
1032 continue;\r
1033 }\r
1034\r
1035 DescStart = Entry->Start;\r
1036 DescEnd = Entry->End;\r
1037\r
1038 //\r
1039 // If desc is past max allowed address or below min allowed address, skip it\r
1040 //\r
1041 if ((DescStart >= MaxAddress) || (DescEnd < MinAddress)) {\r
1042 continue;\r
1043 }\r
1044\r
1045 //\r
1046 // If desc ends past max allowed address, clip the end\r
1047 //\r
1048 if (DescEnd >= MaxAddress) {\r
1049 DescEnd = MaxAddress;\r
1050 }\r
1051\r
1052 DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;\r
1053\r
1054 // Skip if DescEnd is less than DescStart after alignment clipping\r
1055 if (DescEnd < DescStart) {\r
1056 continue;\r
1057 }\r
1058\r
1059 //\r
1060 // Compute the number of bytes we can used from this\r
1061 // descriptor, and see it's enough to satisfy the request\r
1062 //\r
1063 DescNumberOfBytes = DescEnd - DescStart + 1;\r
1064\r
1065 if (DescNumberOfBytes >= NumberOfBytes) {\r
1066 //\r
1067 // If the start of the allocated range is below the min address allowed, skip it\r
1068 //\r
1069 if ((DescEnd - NumberOfBytes + 1) < MinAddress) {\r
1070 continue;\r
1071 }\r
1072\r
1073 //\r
1074 // If this is the best match so far remember it\r
1075 //\r
1076 if (DescEnd > Target) {\r
1077 Target = DescEnd;\r
1078 }\r
1079 }\r
1080 }\r
1081\r
1082 //\r
1083 // If this is a grow down, adjust target to be the allocation base\r
1084 //\r
1085 Target -= NumberOfBytes - 1;\r
1086\r
1087 //\r
1088 // If we didn't find a match, return 0\r
1089 //\r
1090 if ((Target & EFI_PAGE_MASK) != 0) {\r
1091 return 0;\r
1092 }\r
1093\r
1094 return Target;\r
1095}\r
1096\r
1097\r
1098/**\r
1099 Internal function. Finds a consecutive free page range below\r
1100 the requested address\r
1101\r
1102 @param MaxAddress The address that the range must be below\r
1103 @param NoPages Number of pages needed\r
1104 @param NewType The type of memory the range is going to be\r
1105 turned into\r
1106 @param Alignment Bits to align with\r
1107\r
1108 @return The base address of the range, or 0 if the range was not found.\r
1109\r
1110**/\r
1111UINT64\r
1112FindFreePages (\r
1113 IN UINT64 MaxAddress,\r
1114 IN UINT64 NoPages,\r
1115 IN EFI_MEMORY_TYPE NewType,\r
1116 IN UINTN Alignment\r
1117 )\r
1118{\r
1119 UINT64 Start;\r
1120\r
1121 //\r
1122 // Attempt to find free pages in the preferred bin based on the requested memory type\r
1123 //\r
1124 if ((UINT32)NewType < EfiMaxMemoryType && MaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {\r
1125 Start = CoreFindFreePagesI (\r
1126 mMemoryTypeStatistics[NewType].MaximumAddress, \r
1127 mMemoryTypeStatistics[NewType].BaseAddress, \r
1128 NoPages, \r
1129 NewType, \r
1130 Alignment\r
1131 );\r
1132 if (Start != 0) {\r
1133 return Start;\r
1134 }\r
1135 }\r
1136\r
1137 //\r
1138 // Attempt to find free pages in the default allocation bin\r
1139 //\r
1140 if (MaxAddress >= mDefaultMaximumAddress) {\r
1141 Start = CoreFindFreePagesI (mDefaultMaximumAddress, 0, NoPages, NewType, Alignment);\r
1142 if (Start != 0) {\r
1143 if (Start < mDefaultBaseAddress) {\r
1144 mDefaultBaseAddress = Start;\r
1145 }\r
1146 return Start;\r
1147 }\r
1148 }\r
1149\r
1150 //\r
1151 // The allocation did not succeed in any of the prefered bins even after \r
1152 // promoting resources. Attempt to find free pages anywhere is the requested \r
1153 // address range. If this allocation fails, then there are not enough \r
1154 // resources anywhere to satisfy the request.\r
1155 //\r
1156 Start = CoreFindFreePagesI (MaxAddress, 0, NoPages, NewType, Alignment);\r
1157 if (Start != 0) {\r
1158 return Start;\r
1159 }\r
1160\r
1161 //\r
1162 // If allocations from the preferred bins fail, then attempt to promote memory resources.\r
1163 //\r
1164 if (!PromoteMemoryResource ()) {\r
1165 return 0;\r
1166 }\r
1167\r
1168 //\r
1169 // If any memory resources were promoted, then re-attempt the allocation\r
1170 //\r
1171 return FindFreePages (MaxAddress, NoPages, NewType, Alignment);\r
1172}\r
1173\r
1174\r
1175/**\r
1176 Allocates pages from the memory map.\r
1177\r
1178 @param Type The type of allocation to perform\r
1179 @param MemoryType The type of memory to turn the allocated pages\r
1180 into\r
1181 @param NumberOfPages The number of pages to allocate\r
1182 @param Memory A pointer to receive the base allocated memory\r
1183 address\r
1184\r
1185 @return Status. On success, Memory is filled in with the base address allocated\r
1186 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in\r
1187 spec.\r
1188 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r
1189 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r
1190 @retval EFI_SUCCESS Pages successfully allocated.\r
1191\r
1192**/\r
1193EFI_STATUS\r
1194EFIAPI\r
1195CoreInternalAllocatePages (\r
1196 IN EFI_ALLOCATE_TYPE Type,\r
1197 IN EFI_MEMORY_TYPE MemoryType,\r
1198 IN UINTN NumberOfPages,\r
1199 IN OUT EFI_PHYSICAL_ADDRESS *Memory\r
1200 )\r
1201{\r
1202 EFI_STATUS Status;\r
1203 UINT64 Start;\r
1204 UINT64 NumberOfBytes;\r
1205 UINT64 End;\r
1206 UINT64 MaxAddress;\r
1207 UINTN Alignment;\r
1208\r
1209 if ((UINT32)Type >= MaxAllocateType) {\r
1210 return EFI_INVALID_PARAMETER;\r
1211 }\r
1212\r
1213 if ((MemoryType >= EfiMaxMemoryType && MemoryType < MEMORY_TYPE_OEM_RESERVED_MIN) ||\r
1214 (MemoryType == EfiConventionalMemory) || (MemoryType == EfiPersistentMemory)) {\r
1215 return EFI_INVALID_PARAMETER;\r
1216 }\r
1217\r
1218 if (Memory == NULL) {\r
1219 return EFI_INVALID_PARAMETER;\r
1220 }\r
1221\r
1222 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
1223\r
1224 if (MemoryType == EfiACPIReclaimMemory ||\r
1225 MemoryType == EfiACPIMemoryNVS ||\r
1226 MemoryType == EfiRuntimeServicesCode ||\r
1227 MemoryType == EfiRuntimeServicesData) {\r
1228\r
1229 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
1230 }\r
1231\r
1232 if (Type == AllocateAddress) {\r
1233 if ((*Memory & (Alignment - 1)) != 0) {\r
1234 return EFI_NOT_FOUND;\r
1235 }\r
1236 }\r
1237\r
1238 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1239 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1240\r
1241 //\r
1242 // If this is for below a particular address, then\r
1243 //\r
1244 Start = *Memory;\r
1245\r
1246 //\r
1247 // The max address is the max natively addressable address for the processor\r
1248 //\r
1249 MaxAddress = MAX_ADDRESS;\r
1250\r
1251 //\r
1252 // Check for Type AllocateAddress,\r
1253 // if NumberOfPages is 0 or\r
1254 // if (NumberOfPages << EFI_PAGE_SHIFT) is above MAX_ADDRESS or\r
1255 // if (Start + NumberOfBytes) rolls over 0 or\r
1256 // if Start is above MAX_ADDRESS or\r
1257 // if End is above MAX_ADDRESS,\r
1258 // return EFI_NOT_FOUND.\r
1259 //\r
1260 if (Type == AllocateAddress) {\r
1261 if ((NumberOfPages == 0) ||\r
1262 (NumberOfPages > RShiftU64 (MaxAddress, EFI_PAGE_SHIFT))) {\r
1263 return EFI_NOT_FOUND;\r
1264 }\r
1265 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
1266 End = Start + NumberOfBytes - 1;\r
1267\r
1268 if ((Start >= End) ||\r
1269 (Start > MaxAddress) || \r
1270 (End > MaxAddress)) {\r
1271 return EFI_NOT_FOUND;\r
1272 }\r
1273 }\r
1274\r
1275 if (Type == AllocateMaxAddress) {\r
1276 MaxAddress = Start;\r
1277 }\r
1278\r
1279 CoreAcquireMemoryLock ();\r
1280\r
1281 //\r
1282 // If not a specific address, then find an address to allocate\r
1283 //\r
1284 if (Type != AllocateAddress) {\r
1285 Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment);\r
1286 if (Start == 0) {\r
1287 Status = EFI_OUT_OF_RESOURCES;\r
1288 goto Done;\r
1289 }\r
1290 }\r
1291\r
1292 //\r
1293 // Convert pages from FreeMemory to the requested type\r
1294 //\r
1295 Status = CoreConvertPages (Start, NumberOfPages, MemoryType);\r
1296\r
1297Done:\r
1298 CoreReleaseMemoryLock ();\r
1299\r
1300 if (!EFI_ERROR (Status)) {\r
1301 *Memory = Start;\r
1302 }\r
1303\r
1304 return Status;\r
1305}\r
1306\r
1307/**\r
1308 Allocates pages from the memory map.\r
1309\r
1310 @param Type The type of allocation to perform\r
1311 @param MemoryType The type of memory to turn the allocated pages\r
1312 into\r
1313 @param NumberOfPages The number of pages to allocate\r
1314 @param Memory A pointer to receive the base allocated memory\r
1315 address\r
1316\r
1317 @return Status. On success, Memory is filled in with the base address allocated\r
1318 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in\r
1319 spec.\r
1320 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r
1321 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r
1322 @retval EFI_SUCCESS Pages successfully allocated.\r
1323\r
1324**/\r
1325EFI_STATUS\r
1326EFIAPI\r
1327CoreAllocatePages (\r
1328 IN EFI_ALLOCATE_TYPE Type,\r
1329 IN EFI_MEMORY_TYPE MemoryType,\r
1330 IN UINTN NumberOfPages,\r
1331 OUT EFI_PHYSICAL_ADDRESS *Memory\r
1332 )\r
1333{\r
1334 EFI_STATUS Status;\r
1335\r
1336 Status = CoreInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);\r
1337 if (!EFI_ERROR (Status)) {\r
1338 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionAllocatePages, MemoryType, EFI_PAGES_TO_SIZE (NumberOfPages), (VOID *) (UINTN) *Memory);\r
1339 }\r
1340 return Status;\r
1341}\r
1342\r
1343/**\r
1344 Frees previous allocated pages.\r
1345\r
1346 @param Memory Base address of memory being freed\r
1347 @param NumberOfPages The number of pages to free\r
1348 @param MemoryType Pointer to memory type\r
1349\r
1350 @retval EFI_NOT_FOUND Could not find the entry that covers the range\r
1351 @retval EFI_INVALID_PARAMETER Address not aligned\r
1352 @return EFI_SUCCESS -Pages successfully freed.\r
1353\r
1354**/\r
1355EFI_STATUS\r
1356EFIAPI\r
1357CoreInternalFreePages (\r
1358 IN EFI_PHYSICAL_ADDRESS Memory,\r
1359 IN UINTN NumberOfPages,\r
1360 OUT EFI_MEMORY_TYPE *MemoryType OPTIONAL\r
1361 )\r
1362{\r
1363 EFI_STATUS Status;\r
1364 LIST_ENTRY *Link;\r
1365 MEMORY_MAP *Entry;\r
1366 UINTN Alignment;\r
1367\r
1368 //\r
1369 // Free the range\r
1370 //\r
1371 CoreAcquireMemoryLock ();\r
1372\r
1373 //\r
1374 // Find the entry that the covers the range\r
1375 //\r
1376 Entry = NULL;\r
1377 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1378 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1379 if (Entry->Start <= Memory && Entry->End > Memory) {\r
1380 break;\r
1381 }\r
1382 }\r
1383 if (Link == &gMemoryMap) {\r
1384 Status = EFI_NOT_FOUND;\r
1385 goto Done;\r
1386 }\r
1387\r
1388 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
1389\r
1390 ASSERT (Entry != NULL);\r
1391 if (Entry->Type == EfiACPIReclaimMemory ||\r
1392 Entry->Type == EfiACPIMemoryNVS ||\r
1393 Entry->Type == EfiRuntimeServicesCode ||\r
1394 Entry->Type == EfiRuntimeServicesData) {\r
1395\r
1396 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
1397\r
1398 }\r
1399\r
1400 if ((Memory & (Alignment - 1)) != 0) {\r
1401 Status = EFI_INVALID_PARAMETER;\r
1402 goto Done;\r
1403 }\r
1404\r
1405 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1406 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1407\r
1408 if (MemoryType != NULL) {\r
1409 *MemoryType = Entry->Type;\r
1410 }\r
1411\r
1412 Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
1413\r
1414 if (EFI_ERROR (Status)) {\r
1415 goto Done;\r
1416 }\r
1417\r
1418Done:\r
1419 CoreReleaseMemoryLock ();\r
1420 return Status;\r
1421}\r
1422\r
1423/**\r
1424 Frees previous allocated pages.\r
1425\r
1426 @param Memory Base address of memory being freed\r
1427 @param NumberOfPages The number of pages to free\r
1428\r
1429 @retval EFI_NOT_FOUND Could not find the entry that covers the range\r
1430 @retval EFI_INVALID_PARAMETER Address not aligned\r
1431 @return EFI_SUCCESS -Pages successfully freed.\r
1432\r
1433**/\r
1434EFI_STATUS\r
1435EFIAPI\r
1436CoreFreePages (\r
1437 IN EFI_PHYSICAL_ADDRESS Memory,\r
1438 IN UINTN NumberOfPages\r
1439 )\r
1440{\r
1441 EFI_STATUS Status;\r
1442 EFI_MEMORY_TYPE MemoryType;\r
1443\r
1444 Status = CoreInternalFreePages (Memory, NumberOfPages, &MemoryType);\r
1445 if (!EFI_ERROR (Status)) {\r
1446 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionFreePages, (EFI_MEMORY_TYPE) 0, EFI_PAGES_TO_SIZE (NumberOfPages), (VOID *) (UINTN) Memory);\r
1447 }\r
1448 return Status;\r
1449}\r
1450\r
1451/**\r
1452 This function checks to see if the last memory map descriptor in a memory map\r
1453 can be merged with any of the other memory map descriptors in a memorymap.\r
1454 Memory descriptors may be merged if they are adjacent and have the same type\r
1455 and attributes.\r
1456\r
1457 @param MemoryMap A pointer to the start of the memory map.\r
1458 @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.\r
1459 @param DescriptorSize The size, in bytes, of an individual\r
1460 EFI_MEMORY_DESCRIPTOR.\r
1461\r
1462 @return A pointer to the next available descriptor in MemoryMap\r
1463\r
1464**/\r
1465EFI_MEMORY_DESCRIPTOR *\r
1466MergeMemoryMapDescriptor (\r
1467 IN EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
1468 IN EFI_MEMORY_DESCRIPTOR *MemoryMapDescriptor,\r
1469 IN UINTN DescriptorSize\r
1470 )\r
1471{\r
1472 //\r
1473 // Traverse the array of descriptors in MemoryMap\r
1474 //\r
1475 for (; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) {\r
1476 //\r
1477 // Check to see if the Type fields are identical.\r
1478 //\r
1479 if (MemoryMap->Type != MemoryMapDescriptor->Type) {\r
1480 continue;\r
1481 }\r
1482\r
1483 //\r
1484 // Check to see if the Attribute fields are identical.\r
1485 //\r
1486 if (MemoryMap->Attribute != MemoryMapDescriptor->Attribute) {\r
1487 continue;\r
1488 }\r
1489\r
1490 //\r
1491 // Check to see if MemoryMapDescriptor is immediately above MemoryMap\r
1492 //\r
1493 if (MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) { \r
1494 //\r
1495 // Merge MemoryMapDescriptor into MemoryMap\r
1496 //\r
1497 MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r
1498\r
1499 //\r
1500 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r
1501 //\r
1502 return MemoryMapDescriptor;\r
1503 }\r
1504\r
1505 //\r
1506 // Check to see if MemoryMapDescriptor is immediately below MemoryMap\r
1507 //\r
1508 if (MemoryMap->PhysicalStart - EFI_PAGES_TO_SIZE ((UINTN)MemoryMapDescriptor->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {\r
1509 //\r
1510 // Merge MemoryMapDescriptor into MemoryMap\r
1511 //\r
1512 MemoryMap->PhysicalStart = MemoryMapDescriptor->PhysicalStart;\r
1513 MemoryMap->VirtualStart = MemoryMapDescriptor->VirtualStart;\r
1514 MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r
1515\r
1516 //\r
1517 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r
1518 //\r
1519 return MemoryMapDescriptor;\r
1520 }\r
1521 }\r
1522\r
1523 //\r
1524 // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.\r
1525 //\r
1526 // Return the slot immediately after MemoryMapDescriptor as the next available \r
1527 // slot in the MemoryMap array\r
1528 //\r
1529 return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor, DescriptorSize);\r
1530}\r
1531\r
1532/**\r
1533 This function returns a copy of the current memory map. The map is an array of\r
1534 memory descriptors, each of which describes a contiguous block of memory.\r
1535\r
1536 @param MemoryMapSize A pointer to the size, in bytes, of the\r
1537 MemoryMap buffer. On input, this is the size of\r
1538 the buffer allocated by the caller. On output,\r
1539 it is the size of the buffer returned by the\r
1540 firmware if the buffer was large enough, or the\r
1541 size of the buffer needed to contain the map if\r
1542 the buffer was too small.\r
1543 @param MemoryMap A pointer to the buffer in which firmware places\r
1544 the current memory map.\r
1545 @param MapKey A pointer to the location in which firmware\r
1546 returns the key for the current memory map.\r
1547 @param DescriptorSize A pointer to the location in which firmware\r
1548 returns the size, in bytes, of an individual\r
1549 EFI_MEMORY_DESCRIPTOR.\r
1550 @param DescriptorVersion A pointer to the location in which firmware\r
1551 returns the version number associated with the\r
1552 EFI_MEMORY_DESCRIPTOR.\r
1553\r
1554 @retval EFI_SUCCESS The memory map was returned in the MemoryMap\r
1555 buffer.\r
1556 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current\r
1557 buffer size needed to hold the memory map is\r
1558 returned in MemoryMapSize.\r
1559 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
1560\r
1561**/\r
1562EFI_STATUS\r
1563EFIAPI\r
1564CoreGetMemoryMap (\r
1565 IN OUT UINTN *MemoryMapSize,\r
1566 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
1567 OUT UINTN *MapKey,\r
1568 OUT UINTN *DescriptorSize,\r
1569 OUT UINT32 *DescriptorVersion\r
1570 )\r
1571{\r
1572 EFI_STATUS Status;\r
1573 UINTN Size;\r
1574 UINTN BufferSize;\r
1575 UINTN NumberOfEntries;\r
1576 LIST_ENTRY *Link;\r
1577 MEMORY_MAP *Entry;\r
1578 EFI_GCD_MAP_ENTRY *GcdMapEntry;\r
1579 EFI_GCD_MAP_ENTRY MergeGcdMapEntry;\r
1580 EFI_MEMORY_TYPE Type;\r
1581 EFI_MEMORY_DESCRIPTOR *MemoryMapStart;\r
1582\r
1583 //\r
1584 // Make sure the parameters are valid\r
1585 //\r
1586 if (MemoryMapSize == NULL) {\r
1587 return EFI_INVALID_PARAMETER;\r
1588 }\r
1589\r
1590 CoreAcquireGcdMemoryLock ();\r
1591\r
1592 //\r
1593 // Count the number of Reserved and runtime MMIO entries\r
1594 // And, count the number of Persistent entries.\r
1595 //\r
1596 NumberOfEntries = 0;\r
1597 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
1598 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
1599 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypePersistentMemory) || \r
1600 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1601 ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&\r
1602 ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {\r
1603 NumberOfEntries ++;\r
1604 }\r
1605 }\r
1606\r
1607 Size = sizeof (EFI_MEMORY_DESCRIPTOR);\r
1608\r
1609 //\r
1610 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will\r
1611 // prevent people from having pointer math bugs in their code.\r
1612 // now you have to use *DescriptorSize to make things work.\r
1613 //\r
1614 Size += sizeof(UINT64) - (Size % sizeof (UINT64));\r
1615\r
1616 if (DescriptorSize != NULL) {\r
1617 *DescriptorSize = Size;\r
1618 }\r
1619\r
1620 if (DescriptorVersion != NULL) {\r
1621 *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;\r
1622 }\r
1623\r
1624 CoreAcquireMemoryLock ();\r
1625\r
1626 //\r
1627 // Compute the buffer size needed to fit the entire map\r
1628 //\r
1629 BufferSize = Size * NumberOfEntries;\r
1630 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1631 BufferSize += Size;\r
1632 }\r
1633\r
1634 if (*MemoryMapSize < BufferSize) {\r
1635 Status = EFI_BUFFER_TOO_SMALL;\r
1636 goto Done;\r
1637 }\r
1638\r
1639 if (MemoryMap == NULL) {\r
1640 Status = EFI_INVALID_PARAMETER;\r
1641 goto Done;\r
1642 }\r
1643\r
1644 //\r
1645 // Build the map\r
1646 //\r
1647 ZeroMem (MemoryMap, BufferSize);\r
1648 MemoryMapStart = MemoryMap;\r
1649 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1650 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1651 ASSERT (Entry->VirtualStart == 0);\r
1652\r
1653 //\r
1654 // Convert internal map into an EFI_MEMORY_DESCRIPTOR\r
1655 //\r
1656 MemoryMap->Type = Entry->Type;\r
1657 MemoryMap->PhysicalStart = Entry->Start;\r
1658 MemoryMap->VirtualStart = Entry->VirtualStart;\r
1659 MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);\r
1660 //\r
1661 // If the memory type is EfiConventionalMemory, then determine if the range is part of a\r
1662 // memory type bin and needs to be converted to the same memory type as the rest of the\r
1663 // memory type bin in order to minimize EFI Memory Map changes across reboots. This\r
1664 // improves the chances for a successful S4 resume in the presence of minor page allocation\r
1665 // differences across reboots.\r
1666 //\r
1667 if (MemoryMap->Type == EfiConventionalMemory) {\r
1668 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
1669 if (mMemoryTypeStatistics[Type].Special &&\r
1670 mMemoryTypeStatistics[Type].NumberOfPages > 0 &&\r
1671 Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress &&\r
1672 Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) {\r
1673 MemoryMap->Type = Type;\r
1674 }\r
1675 }\r
1676 }\r
1677 MemoryMap->Attribute = Entry->Attribute;\r
1678 if (MemoryMap->Type < EfiMaxMemoryType) {\r
1679 if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {\r
1680 MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;\r
1681 }\r
1682 }\r
1683\r
1684 //\r
1685 // Check to see if the new Memory Map Descriptor can be merged with an \r
1686 // existing descriptor if they are adjacent and have the same attributes\r
1687 //\r
1688 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
1689 }\r
1690\r
1691 \r
1692 ZeroMem (&MergeGcdMapEntry, sizeof (MergeGcdMapEntry));\r
1693 GcdMapEntry = NULL;\r
1694 for (Link = mGcdMemorySpaceMap.ForwardLink; ; Link = Link->ForwardLink) {\r
1695 if (Link != &mGcdMemorySpaceMap) {\r
1696 //\r
1697 // Merge adjacent same type and attribute GCD memory range\r
1698 //\r
1699 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
1700 \r
1701 if ((MergeGcdMapEntry.Capabilities == GcdMapEntry->Capabilities) && \r
1702 (MergeGcdMapEntry.Attributes == GcdMapEntry->Attributes) &&\r
1703 (MergeGcdMapEntry.GcdMemoryType == GcdMapEntry->GcdMemoryType) &&\r
1704 (MergeGcdMapEntry.GcdIoType == GcdMapEntry->GcdIoType)) {\r
1705 MergeGcdMapEntry.EndAddress = GcdMapEntry->EndAddress;\r
1706 continue;\r
1707 }\r
1708 }\r
1709\r
1710 if ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1711 ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&\r
1712 ((MergeGcdMapEntry.Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {\r
1713 //\r
1714 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR, \r
1715 // it will be recorded as page PhysicalStart and NumberOfPages. \r
1716 //\r
1717 ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);\r
1718 ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);\r
1719 \r
1720 // \r
1721 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and runtime MMIO GCD entries\r
1722 //\r
1723 MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;\r
1724 MemoryMap->VirtualStart = 0;\r
1725 MemoryMap->NumberOfPages = RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);\r
1726 MemoryMap->Attribute = (MergeGcdMapEntry.Attributes & ~EFI_MEMORY_PORT_IO) | \r
1727 (MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |\r
1728 EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));\r
1729\r
1730 if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) {\r
1731 MemoryMap->Type = EfiReservedMemoryType;\r
1732 } else if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {\r
1733 if ((MergeGcdMapEntry.Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {\r
1734 MemoryMap->Type = EfiMemoryMappedIOPortSpace;\r
1735 } else {\r
1736 MemoryMap->Type = EfiMemoryMappedIO;\r
1737 }\r
1738 }\r
1739\r
1740 //\r
1741 // Check to see if the new Memory Map Descriptor can be merged with an \r
1742 // existing descriptor if they are adjacent and have the same attributes\r
1743 //\r
1744 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
1745 }\r
1746 \r
1747 if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypePersistentMemory) {\r
1748 //\r
1749 // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR, \r
1750 // it will be recorded as page PhysicalStart and NumberOfPages. \r
1751 //\r
1752 ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);\r
1753 ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);\r
1754\r
1755 // \r
1756 // Create EFI_MEMORY_DESCRIPTOR for every Persistent GCD entries\r
1757 //\r
1758 MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;\r
1759 MemoryMap->VirtualStart = 0;\r
1760 MemoryMap->NumberOfPages = RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);\r
1761 MemoryMap->Attribute = MergeGcdMapEntry.Attributes | EFI_MEMORY_NV | \r
1762 (MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |\r
1763 EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));\r
1764 MemoryMap->Type = EfiPersistentMemory;\r
1765 \r
1766 //\r
1767 // Check to see if the new Memory Map Descriptor can be merged with an \r
1768 // existing descriptor if they are adjacent and have the same attributes\r
1769 //\r
1770 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
1771 }\r
1772 if (Link == &mGcdMemorySpaceMap) {\r
1773 //\r
1774 // break loop when arrive at head.\r
1775 //\r
1776 break;\r
1777 }\r
1778 if (GcdMapEntry != NULL) {\r
1779 //\r
1780 // Copy new GCD map entry for the following GCD range merge\r
1781 //\r
1782 CopyMem (&MergeGcdMapEntry, GcdMapEntry, sizeof (MergeGcdMapEntry));\r
1783 }\r
1784 }\r
1785\r
1786 //\r
1787 // Compute the size of the buffer actually used after all memory map descriptor merge operations\r
1788 //\r
1789 BufferSize = ((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart);\r
1790\r
1791 Status = EFI_SUCCESS;\r
1792\r
1793Done:\r
1794 //\r
1795 // Update the map key finally\r
1796 //\r
1797 if (MapKey != NULL) {\r
1798 *MapKey = mMemoryMapKey;\r
1799 }\r
1800\r
1801 CoreReleaseMemoryLock ();\r
1802\r
1803 CoreReleaseGcdMemoryLock ();\r
1804\r
1805 *MemoryMapSize = BufferSize;\r
1806\r
1807 return Status;\r
1808}\r
1809\r
1810\r
1811/**\r
1812 Internal function. Used by the pool functions to allocate pages\r
1813 to back pool allocation requests.\r
1814\r
1815 @param PoolType The type of memory for the new pool pages\r
1816 @param NumberOfPages No of pages to allocate\r
1817 @param Alignment Bits to align.\r
1818\r
1819 @return The allocated memory, or NULL\r
1820\r
1821**/\r
1822VOID *\r
1823CoreAllocatePoolPages (\r
1824 IN EFI_MEMORY_TYPE PoolType,\r
1825 IN UINTN NumberOfPages,\r
1826 IN UINTN Alignment\r
1827 )\r
1828{\r
1829 UINT64 Start;\r
1830\r
1831 //\r
1832 // Find the pages to convert\r
1833 //\r
1834 Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment);\r
1835\r
1836 //\r
1837 // Convert it to boot services data\r
1838 //\r
1839 if (Start == 0) {\r
1840 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32)NumberOfPages));\r
1841 } else {\r
1842 CoreConvertPages (Start, NumberOfPages, PoolType);\r
1843 }\r
1844\r
1845 return (VOID *)(UINTN) Start;\r
1846}\r
1847\r
1848\r
1849/**\r
1850 Internal function. Frees pool pages allocated via AllocatePoolPages ()\r
1851\r
1852 @param Memory The base address to free\r
1853 @param NumberOfPages The number of pages to free\r
1854\r
1855**/\r
1856VOID\r
1857CoreFreePoolPages (\r
1858 IN EFI_PHYSICAL_ADDRESS Memory,\r
1859 IN UINTN NumberOfPages\r
1860 )\r
1861{\r
1862 CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
1863}\r
1864\r
1865\r
1866\r
1867/**\r
1868 Make sure the memory map is following all the construction rules,\r
1869 it is the last time to check memory map error before exit boot services.\r
1870\r
1871 @param MapKey Memory map key\r
1872\r
1873 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction\r
1874 rules.\r
1875 @retval EFI_SUCCESS Valid memory map.\r
1876\r
1877**/\r
1878EFI_STATUS\r
1879CoreTerminateMemoryMap (\r
1880 IN UINTN MapKey\r
1881 )\r
1882{\r
1883 EFI_STATUS Status;\r
1884 LIST_ENTRY *Link;\r
1885 MEMORY_MAP *Entry;\r
1886\r
1887 Status = EFI_SUCCESS;\r
1888\r
1889 CoreAcquireMemoryLock ();\r
1890\r
1891 if (MapKey == mMemoryMapKey) {\r
1892\r
1893 //\r
1894 // Make sure the memory map is following all the construction rules\r
1895 // This is the last chance we will be able to display any messages on\r
1896 // the console devices.\r
1897 //\r
1898\r
1899 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1900 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1901 if (Entry->Type < EfiMaxMemoryType) {\r
1902 if (mMemoryTypeStatistics[Entry->Type].Runtime) {\r
1903 ASSERT (Entry->Type != EfiACPIReclaimMemory);\r
1904 ASSERT (Entry->Type != EfiACPIMemoryNVS);\r
1905 if ((Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
1906 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
1907 Status = EFI_INVALID_PARAMETER;\r
1908 goto Done;\r
1909 }\r
1910 if (((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
1911 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
1912 Status = EFI_INVALID_PARAMETER;\r
1913 goto Done;\r
1914 }\r
1915 }\r
1916 }\r
1917 }\r
1918\r
1919 //\r
1920 // The map key they gave us matches what we expect. Fall through and\r
1921 // return success. In an ideal world we would clear out all of\r
1922 // EfiBootServicesCode and EfiBootServicesData. However this function\r
1923 // is not the last one called by ExitBootServices(), so we have to\r
1924 // preserve the memory contents.\r
1925 //\r
1926 } else {\r
1927 Status = EFI_INVALID_PARAMETER;\r
1928 }\r
1929\r
1930Done:\r
1931 CoreReleaseMemoryLock ();\r
1932\r
1933 return Status;\r
1934}\r
1935\r
1936\r
1937\r
1938\r
1939\r
1940\r
1941\r
1942\r
1943\r