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