]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/Mem/Page.c
Refine code to follow edkii coding style.
[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
6e1e5405 4Copyright (c) 2007 - 2014, 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
18#define EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT (EFI_PAGE_SIZE)\r
19\r
20//\r
d45fd260 21// Entry for tracking the memory regions for each memory type to coalesce similar memory types\r
28a00297 22//\r
23typedef struct {\r
24 EFI_PHYSICAL_ADDRESS BaseAddress;\r
25 EFI_PHYSICAL_ADDRESS MaximumAddress;\r
26 UINT64 CurrentNumberOfPages;\r
b74350e9 27 UINT64 NumberOfPages;\r
28a00297 28 UINTN InformationIndex;\r
b74350e9 29 BOOLEAN Special;\r
30 BOOLEAN Runtime;\r
d613c2a8 31} EFI_MEMORY_TYPE_STATISTICS;\r
28a00297 32\r
33//\r
34// MemoryMap - The current memory map\r
35//\r
36UINTN mMemoryMapKey = 0;\r
37\r
28a00297 38#define MAX_MAP_DEPTH 6\r
dc8d93ca 39\r
40///\r
41/// mMapDepth - depth of new descriptor stack\r
42///\r
28a00297 43UINTN mMapDepth = 0;\r
dc8d93ca 44///\r
45/// mMapStack - space to use as temp storage to build new map descriptors\r
46///\r
28a00297 47MEMORY_MAP mMapStack[MAX_MAP_DEPTH];\r
48UINTN mFreeMapStack = 0;\r
dc8d93ca 49///\r
50/// This list maintain the free memory map list\r
51///\r
e94a9ff7 52LIST_ENTRY mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList);\r
53BOOLEAN mMemoryTypeInformationInitialized = FALSE;\r
28a00297 54\r
d613c2a8 55EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {\r
f3f2e05d 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 } // EfiMaxMemoryType\r
28a00297 71};\r
72\r
f3f2e05d 73EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress = MAX_ADDRESS;\r
2345e7d4 74EFI_PHYSICAL_ADDRESS mDefaultBaseAddress = MAX_ADDRESS;\r
28a00297 75\r
76EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {\r
77 { EfiReservedMemoryType, 0 },\r
78 { EfiLoaderCode, 0 },\r
79 { EfiLoaderData, 0 },\r
80 { EfiBootServicesCode, 0 },\r
81 { EfiBootServicesData, 0 },\r
82 { EfiRuntimeServicesCode, 0 },\r
83 { EfiRuntimeServicesData, 0 },\r
84 { EfiConventionalMemory, 0 },\r
85 { EfiUnusableMemory, 0 },\r
86 { EfiACPIReclaimMemory, 0 },\r
87 { EfiACPIMemoryNVS, 0 },\r
88 { EfiMemoryMappedIO, 0 },\r
89 { EfiMemoryMappedIOPortSpace, 0 },\r
90 { EfiPalCode, 0 },\r
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
205 // function the notificaiton events will only be called after this funciton\r
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
288 FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION);\r
289 if(FreeDescriptorEntries != NULL) {\r
290 //\r
291 // Enque the free memmory map entries into the list\r
292 //\r
293 for (Index = 0; Index< DEFAULT_PAGE_ALLOCATION / sizeof(MEMORY_MAP); Index++) {\r
294 FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;\r
295 InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);\r
296 }\r
297 } else {\r
298 return NULL;\r
299 }\r
300 }\r
301 //\r
302 // dequeue the first descriptor from the list\r
303 //\r
304 Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
305 RemoveEntryList (&Entry->Link);\r
306\r
307 return Entry;\r
308}\r
022c6d45 309\r
162ed594 310\r
311/**\r
d9177625 312 Internal function. Moves any memory descriptors that are on the\r
313 temporary descriptor stack to heap.\r
162ed594 314\r
315**/\r
28a00297 316VOID\r
d9177625 317CoreFreeMemoryMapStack (\r
28a00297 318 VOID\r
319 )\r
28a00297 320{\r
d9177625 321 MEMORY_MAP *Entry;\r
322 MEMORY_MAP *Entry2;\r
323 LIST_ENTRY *Link2;\r
28a00297 324\r
d9177625 325 ASSERT_LOCKED (&gMemoryLock);\r
28a00297 326\r
d9177625 327 //\r
328 // If already freeing the map stack, then return\r
329 //\r
330 if (mFreeMapStack != 0) {\r
331 return ;\r
332 }\r
162ed594 333\r
d9177625 334 //\r
335 // Move the temporary memory descriptor stack into pool\r
336 //\r
337 mFreeMapStack += 1;\r
162ed594 338\r
d9177625 339 while (mMapDepth != 0) {\r
340 //\r
341 // Deque an memory map entry from mFreeMemoryMapEntryList\r
342 //\r
343 Entry = AllocateMemoryMapEntry ();\r
344\r
345 ASSERT (Entry);\r
346\r
347 //\r
348 // Update to proper entry\r
349 //\r
350 mMapDepth -= 1;\r
351\r
352 if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {\r
353\r
354 //\r
355 // Move this entry to general memory\r
356 //\r
357 RemoveEntryList (&mMapStack[mMapDepth].Link);\r
358 mMapStack[mMapDepth].Link.ForwardLink = NULL;\r
359\r
360 CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));\r
361 Entry->FromPages = TRUE;\r
362\r
363 //\r
364 // Find insertion location\r
365 //\r
366 for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) {\r
367 Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
368 if (Entry2->FromPages && Entry2->Start > Entry->Start) {\r
369 break;\r
370 }\r
371 }\r
372\r
373 InsertTailList (Link2, &Entry->Link);\r
374\r
375 } else {\r
376 //\r
377 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,\r
378 // so here no need to move it to memory.\r
379 //\r
380 InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
381 }\r
382 }\r
28a00297 383\r
d9177625 384 mFreeMapStack -= 1;\r
385}\r
162ed594 386\r
387/**\r
388 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.\r
389\r
390**/\r
2345e7d4 391BOOLEAN\r
28a00297 392PromoteMemoryResource (\r
393 VOID\r
394 )\r
28a00297 395{\r
2345e7d4 396 LIST_ENTRY *Link;\r
397 EFI_GCD_MAP_ENTRY *Entry;\r
398 BOOLEAN Promoted;\r
28a00297 399\r
d45fd260 400 DEBUG ((DEBUG_PAGE, "Promote the memory resource\n"));\r
022c6d45 401\r
28a00297 402 CoreAcquireGcdMemoryLock ();\r
022c6d45 403\r
2345e7d4 404 Promoted = FALSE;\r
28a00297 405 Link = mGcdMemorySpaceMap.ForwardLink;\r
406 while (Link != &mGcdMemorySpaceMap) {\r
407\r
408 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
409\r
410 if (Entry->GcdMemoryType == EfiGcdMemoryTypeReserved &&\r
f3f2e05d 411 Entry->EndAddress < MAX_ADDRESS &&\r
28a00297 412 (Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r
413 (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) {\r
414 //\r
415 // Update the GCD map\r
416 //\r
417 Entry->GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r
418 Entry->Capabilities |= EFI_MEMORY_TESTED;\r
419 Entry->ImageHandle = gDxeCoreImageHandle;\r
420 Entry->DeviceHandle = NULL;\r
421\r
422 //\r
423 // Add to allocable system memory resource\r
022c6d45 424 //\r
28a00297 425\r
426 CoreAddRange (\r
022c6d45 427 EfiConventionalMemory,\r
428 Entry->BaseAddress,\r
429 Entry->EndAddress,\r
28a00297 430 Entry->Capabilities & ~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r
431 );\r
432 CoreFreeMemoryMapStack ();\r
022c6d45 433\r
2345e7d4 434 Promoted = TRUE;\r
28a00297 435 }\r
436\r
437 Link = Link->ForwardLink;\r
438 }\r
022c6d45 439\r
28a00297 440 CoreReleaseGcdMemoryLock ();\r
022c6d45 441\r
2345e7d4 442 return Promoted;\r
28a00297 443}\r
54ea99a7 444/**\r
445 This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD \r
446 PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the \r
447 size of boot time and runtime code.\r
28a00297 448\r
54ea99a7 449**/\r
450VOID\r
451CoreLoadingFixedAddressHook (\r
452 VOID\r
453 )\r
454{\r
455 UINT32 RuntimeCodePageNumber;\r
456 UINT32 BootTimeCodePageNumber;\r
457 EFI_PHYSICAL_ADDRESS RuntimeCodeBase;\r
458 EFI_PHYSICAL_ADDRESS BootTimeCodeBase;\r
459 EFI_STATUS Status;\r
460\r
461 //\r
462 // Make sure these 2 areas are not initialzied.\r
463 //\r
464 if (!gLoadFixedAddressCodeMemoryReady) { \r
465 RuntimeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
466 BootTimeCodePageNumber= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
467 RuntimeCodeBase = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - EFI_PAGES_TO_SIZE (RuntimeCodePageNumber));\r
468 BootTimeCodeBase = (EFI_PHYSICAL_ADDRESS)(RuntimeCodeBase - EFI_PAGES_TO_SIZE (BootTimeCodePageNumber));\r
469 //\r
470 // Try to allocate runtime memory.\r
471 //\r
472 Status = CoreAllocatePages (\r
473 AllocateAddress,\r
474 EfiRuntimeServicesCode,\r
475 RuntimeCodePageNumber,\r
476 &RuntimeCodeBase\r
477 );\r
478 if (EFI_ERROR(Status)) {\r
479 //\r
480 // Runtime memory allocation failed \r
481 //\r
482 return;\r
483 }\r
484 //\r
485 // Try to allocate boot memory.\r
486 //\r
487 Status = CoreAllocatePages (\r
488 AllocateAddress,\r
489 EfiBootServicesCode,\r
490 BootTimeCodePageNumber,\r
491 &BootTimeCodeBase\r
492 );\r
493 if (EFI_ERROR(Status)) {\r
494 //\r
495 // boot memory allocation failed. Free Runtime code range and will try the allocation again when \r
496 // new memory range is installed.\r
497 //\r
498 CoreFreePages (\r
499 RuntimeCodeBase,\r
500 RuntimeCodePageNumber\r
501 );\r
502 return;\r
503 }\r
504 gLoadFixedAddressCodeMemoryReady = TRUE;\r
505 } \r
506 return;\r
507} \r
28a00297 508\r
162ed594 509/**\r
28a00297 510 Called to initialize the memory map and add descriptors to\r
511 the current descriptor list.\r
28a00297 512 The first descriptor that is added must be general usable\r
513 memory as the addition allocates heap.\r
514\r
022c6d45 515 @param Type The type of memory to add\r
516 @param Start The starting address in the memory range Must be\r
517 page aligned\r
518 @param NumberOfPages The number of pages in the range\r
519 @param Attribute Attributes of the memory to add\r
28a00297 520\r
162ed594 521 @return None. The range is added to the memory map\r
28a00297 522\r
162ed594 523**/\r
524VOID\r
525CoreAddMemoryDescriptor (\r
526 IN EFI_MEMORY_TYPE Type,\r
527 IN EFI_PHYSICAL_ADDRESS Start,\r
528 IN UINT64 NumberOfPages,\r
529 IN UINT64 Attribute\r
530 )\r
28a00297 531{\r
532 EFI_PHYSICAL_ADDRESS End;\r
533 EFI_STATUS Status;\r
534 UINTN Index;\r
535 UINTN FreeIndex;\r
54ea99a7 536 \r
28a00297 537 if ((Start & EFI_PAGE_MASK) != 0) {\r
538 return;\r
539 }\r
540\r
541 if (Type >= EfiMaxMemoryType && Type <= 0x7fffffff) {\r
542 return;\r
543 }\r
28a00297 544 CoreAcquireMemoryLock ();\r
545 End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
546 CoreAddRange (Type, Start, End, Attribute);\r
547 CoreFreeMemoryMapStack ();\r
548 CoreReleaseMemoryLock ();\r
549\r
54ea99a7 550 //\r
551 // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type\r
552 //\r
852081fc 553 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
54ea99a7 554 CoreLoadingFixedAddressHook();\r
555 }\r
556 \r
28a00297 557 //\r
558 // Check to see if the statistics for the different memory types have already been established\r
559 //\r
560 if (mMemoryTypeInformationInitialized) {\r
561 return;\r
562 }\r
563\r
54ea99a7 564 \r
28a00297 565 //\r
566 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array\r
567 //\r
568 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
569 //\r
570 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
571 //\r
572 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
3d78c020 573 if ((UINT32)Type > EfiMaxMemoryType) {\r
28a00297 574 continue;\r
575 }\r
28a00297 576 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
577 //\r
578 // Allocate pages for the current memory type from the top of available memory\r
579 //\r
580 Status = CoreAllocatePages (\r
581 AllocateAnyPages,\r
582 Type,\r
583 gMemoryTypeInformation[Index].NumberOfPages,\r
584 &mMemoryTypeStatistics[Type].BaseAddress\r
585 );\r
586 if (EFI_ERROR (Status)) {\r
587 //\r
022c6d45 588 // If an error occurs allocating the pages for the current memory type, then\r
28a00297 589 // free all the pages allocates for the previous memory types and return. This\r
590 // operation with be retied when/if more memory is added to the system\r
591 //\r
592 for (FreeIndex = 0; FreeIndex < Index; FreeIndex++) {\r
593 //\r
594 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
595 //\r
596 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type);\r
3d78c020 597 if ((UINT32)Type > EfiMaxMemoryType) {\r
28a00297 598 continue;\r
599 }\r
600\r
601 if (gMemoryTypeInformation[FreeIndex].NumberOfPages != 0) {\r
602 CoreFreePages (\r
022c6d45 603 mMemoryTypeStatistics[Type].BaseAddress,\r
28a00297 604 gMemoryTypeInformation[FreeIndex].NumberOfPages\r
605 );\r
606 mMemoryTypeStatistics[Type].BaseAddress = 0;\r
f3f2e05d 607 mMemoryTypeStatistics[Type].MaximumAddress = MAX_ADDRESS;\r
28a00297 608 }\r
609 }\r
610 return;\r
611 }\r
612\r
613 //\r
614 // Compute the address at the top of the current statistics\r
615 //\r
022c6d45 616 mMemoryTypeStatistics[Type].MaximumAddress =\r
617 mMemoryTypeStatistics[Type].BaseAddress +\r
28a00297 618 LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
619\r
620 //\r
022c6d45 621 // If the current base address is the lowest address so far, then update the default\r
28a00297 622 // maximum address\r
623 //\r
624 if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) {\r
625 mDefaultMaximumAddress = mMemoryTypeStatistics[Type].BaseAddress - 1;\r
626 }\r
627 }\r
628 }\r
629\r
630 //\r
631 // There was enough system memory for all the the memory types were allocated. So,\r
632 // those memory areas can be freed for future allocations, and all future memory\r
633 // allocations can occur within their respective bins\r
634 //\r
635 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
636 //\r
637 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
638 //\r
639 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
3d78c020 640 if ((UINT32)Type > EfiMaxMemoryType) {\r
28a00297 641 continue;\r
642 }\r
28a00297 643 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
644 CoreFreePages (\r
022c6d45 645 mMemoryTypeStatistics[Type].BaseAddress,\r
28a00297 646 gMemoryTypeInformation[Index].NumberOfPages\r
647 );\r
b74350e9 648 mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages;\r
28a00297 649 gMemoryTypeInformation[Index].NumberOfPages = 0;\r
650 }\r
651 }\r
652\r
653 //\r
654 // If the number of pages reserved for a memory type is 0, then all allocations for that type\r
655 // should be in the default range.\r
656 //\r
657 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
658 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
659 if (Type == (EFI_MEMORY_TYPE)gMemoryTypeInformation[Index].Type) {\r
660 mMemoryTypeStatistics[Type].InformationIndex = Index;\r
661 }\r
662 }\r
663 mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0;\r
f3f2e05d 664 if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ADDRESS) {\r
28a00297 665 mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress;\r
666 }\r
667 }\r
668\r
669 mMemoryTypeInformationInitialized = TRUE;\r
670}\r
671\r
672\r
162ed594 673/**\r
771ee501
EC
674 Internal function. Converts a memory range to the specified type or attributes.\r
675 The range must exist in the memory map. Either ChangingType or\r
676 ChangingAttributes must be set, but not both.\r
162ed594 677\r
022c6d45 678 @param Start The first address of the range Must be page\r
679 aligned\r
680 @param NumberOfPages The number of pages to convert\r
771ee501 681 @param ChangingType Boolean indicating that type value should be changed\r
022c6d45 682 @param NewType The new type for the memory range\r
771ee501
EC
683 @param ChangingAttributes Boolean indicating that attributes value should be changed\r
684 @param NewAttributes The new attributes for the memory range\r
162ed594 685\r
022c6d45 686 @retval EFI_INVALID_PARAMETER Invalid parameter\r
687 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r
688 range or convertion not allowed.\r
689 @retval EFI_SUCCESS Successfully converts the memory range to the\r
162ed594 690 specified type.\r
691\r
692**/\r
28a00297 693EFI_STATUS\r
771ee501 694CoreConvertPagesEx (\r
28a00297 695 IN UINT64 Start,\r
696 IN UINT64 NumberOfPages,\r
771ee501
EC
697 IN BOOLEAN ChangingType,\r
698 IN EFI_MEMORY_TYPE NewType,\r
699 IN BOOLEAN ChangingAttributes,\r
700 IN UINT64 NewAttributes\r
28a00297 701 )\r
28a00297 702{\r
703\r
704 UINT64 NumberOfBytes;\r
705 UINT64 End;\r
706 UINT64 RangeEnd;\r
707 UINT64 Attribute;\r
771ee501 708 EFI_MEMORY_TYPE MemType;\r
28a00297 709 LIST_ENTRY *Link;\r
710 MEMORY_MAP *Entry;\r
711\r
712 Entry = NULL;\r
713 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
714 End = Start + NumberOfBytes - 1;\r
715\r
716 ASSERT (NumberOfPages);\r
717 ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
718 ASSERT (End > Start) ;\r
719 ASSERT_LOCKED (&gMemoryLock);\r
771ee501 720 ASSERT ( (ChangingType == FALSE) || (ChangingAttributes == FALSE) );\r
28a00297 721\r
71f68914 722 if (NumberOfPages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start > (Start + NumberOfBytes))) {\r
28a00297 723 return EFI_INVALID_PARAMETER;\r
724 }\r
725\r
726 //\r
727 // Convert the entire range\r
728 //\r
729\r
730 while (Start < End) {\r
731\r
732 //\r
733 // Find the entry that the covers the range\r
734 //\r
735 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
736 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
737\r
738 if (Entry->Start <= Start && Entry->End > Start) {\r
739 break;\r
740 }\r
741 }\r
742\r
743 if (Link == &gMemoryMap) {\r
162ed594 744 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: failed to find range %lx - %lx\n", Start, End));\r
28a00297 745 return EFI_NOT_FOUND;\r
746 }\r
747\r
748 //\r
749 // Convert range to the end, or to the end of the descriptor\r
750 // if that's all we've got\r
751 //\r
752 RangeEnd = End;\r
525aded9 753\r
754 ASSERT (Entry != NULL);\r
28a00297 755 if (Entry->End < End) {\r
756 RangeEnd = Entry->End;\r
757 }\r
758\r
771ee501
EC
759 if (ChangingType) {\r
760 DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to type %d\n", Start, RangeEnd, NewType));\r
761 }\r
762 if (ChangingAttributes) {\r
763 DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to attr %lx\n", Start, RangeEnd, NewAttributes));\r
022c6d45 764 }\r
28a00297 765\r
771ee501
EC
766 if (ChangingType) {\r
767 //\r
768 // Debug code - verify conversion is allowed\r
769 //\r
770 if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) {\r
771 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types\n"));\r
772 return EFI_NOT_FOUND;\r
773 }\r
774\r
775 //\r
776 // Update counters for the number of pages allocated to each memory type\r
777 //\r
778 if ((UINT32)Entry->Type < EfiMaxMemoryType) {\r
779 if ((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) ||\r
780 (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {\r
781 if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {\r
782 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;\r
783 } else {\r
784 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;\r
785 }\r
28a00297 786 }\r
787 }\r
28a00297 788\r
771ee501
EC
789 if ((UINT32)NewType < EfiMaxMemoryType) {\r
790 if ((Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) ||\r
791 (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {\r
792 mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;\r
793 if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {\r
794 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;\r
795 }\r
28a00297 796 }\r
797 }\r
798 }\r
799\r
800 //\r
801 // Pull range out of descriptor\r
802 //\r
803 if (Entry->Start == Start) {\r
022c6d45 804\r
28a00297 805 //\r
806 // Clip start\r
807 //\r
808 Entry->Start = RangeEnd + 1;\r
809\r
810 } else if (Entry->End == RangeEnd) {\r
022c6d45 811\r
28a00297 812 //\r
813 // Clip end\r
814 //\r
815 Entry->End = Start - 1;\r
816\r
817 } else {\r
818\r
819 //\r
820 // Pull it out of the center, clip current\r
821 //\r
022c6d45 822\r
28a00297 823 //\r
824 // Add a new one\r
825 //\r
826 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
827 mMapStack[mMapDepth].FromPages = FALSE;\r
828 mMapStack[mMapDepth].Type = Entry->Type;\r
829 mMapStack[mMapDepth].Start = RangeEnd+1;\r
830 mMapStack[mMapDepth].End = Entry->End;\r
831\r
832 //\r
833 // Inherit Attribute from the Memory Descriptor that is being clipped\r
834 //\r
835 mMapStack[mMapDepth].Attribute = Entry->Attribute;\r
836\r
837 Entry->End = Start - 1;\r
838 ASSERT (Entry->Start < Entry->End);\r
839\r
840 Entry = &mMapStack[mMapDepth];\r
841 InsertTailList (&gMemoryMap, &Entry->Link);\r
842\r
843 mMapDepth += 1;\r
844 ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
845 }\r
846\r
847 //\r
022c6d45 848 // The new range inherits the same Attribute as the Entry\r
771ee501 849 // it is being cut out of unless attributes are being changed\r
28a00297 850 //\r
771ee501
EC
851 if (ChangingType) {\r
852 Attribute = Entry->Attribute;\r
853 MemType = NewType;\r
854 } else {\r
855 Attribute = NewAttributes;\r
856 MemType = Entry->Type;\r
857 }\r
28a00297 858\r
859 //\r
860 // If the descriptor is empty, then remove it from the map\r
861 //\r
862 if (Entry->Start == Entry->End + 1) {\r
863 RemoveMemoryMapEntry (Entry);\r
864 Entry = NULL;\r
865 }\r
022c6d45 866\r
28a00297 867 //\r
868 // Add our new range in\r
869 //\r
771ee501
EC
870 CoreAddRange (MemType, Start, RangeEnd, Attribute);\r
871 if (ChangingType && (MemType == EfiConventionalMemory)) {\r
9a340872 872 //\r
873 // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this\r
874 // macro will ASSERT() if address is 0. Instead, CoreAddRange() guarantees\r
875 // that the page starting at address 0 is always filled with zeros.\r
876 //\r
877 if (Start == 0) {\r
878 if (RangeEnd > EFI_PAGE_SIZE) {\r
879 DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) EFI_PAGE_SIZE, (UINTN) (RangeEnd - EFI_PAGE_SIZE + 1));\r
880 }\r
881 } else {\r
882 DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) Start, (UINTN) (RangeEnd - Start + 1));\r
883 }\r
38c7df98 884 }\r
28a00297 885\r
886 //\r
887 // Move any map descriptor stack to general pool\r
888 //\r
889 CoreFreeMemoryMapStack ();\r
890\r
891 //\r
892 // Bump the starting address, and convert the next range\r
893 //\r
894 Start = RangeEnd + 1;\r
895 }\r
896\r
897 //\r
898 // Converted the whole range, done\r
899 //\r
900\r
901 return EFI_SUCCESS;\r
902}\r
903\r
904\r
771ee501
EC
905/**\r
906 Internal function. Converts a memory range to the specified type.\r
907 The range must exist in the memory map.\r
908\r
909 @param Start The first address of the range Must be page\r
910 aligned\r
911 @param NumberOfPages The number of pages to convert\r
912 @param NewType The new type for the memory range\r
913\r
914 @retval EFI_INVALID_PARAMETER Invalid parameter\r
915 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r
916 range or convertion not allowed.\r
917 @retval EFI_SUCCESS Successfully converts the memory range to the\r
918 specified type.\r
919\r
920**/\r
921EFI_STATUS\r
922CoreConvertPages (\r
923 IN UINT64 Start,\r
924 IN UINT64 NumberOfPages,\r
925 IN EFI_MEMORY_TYPE NewType\r
926 )\r
927{\r
928 return CoreConvertPagesEx(Start, NumberOfPages, TRUE, NewType, FALSE, 0);\r
929}\r
930\r
931\r
932/**\r
933 Internal function. Converts a memory range to use new attributes.\r
934\r
935 @param Start The first address of the range Must be page\r
936 aligned\r
937 @param NumberOfPages The number of pages to convert\r
938 @param NewAttributes The new attributes value for the range.\r
939\r
940 @retval EFI_INVALID_PARAMETER Invalid parameter\r
941 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r
942 range or convertion not allowed.\r
943 @retval EFI_SUCCESS Successfully converts the memory range to the\r
944 specified attributes.\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
162ed594 964\r
965/**\r
966 Internal function. Finds a consecutive free page range below\r
967 the requested address.\r
968\r
022c6d45 969 @param MaxAddress The address that the range must be below\r
35f9e94e 970 @param MinAddress The address that the range must be above\r
022c6d45 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
162ed594 975\r
976 @return The base address of the range, or 0 if the range was not found\r
977\r
978**/\r
28a00297 979UINT64\r
980CoreFindFreePagesI (\r
981 IN UINT64 MaxAddress,\r
2345e7d4 982 IN UINT64 MinAddress,\r
28a00297 983 IN UINT64 NumberOfPages,\r
984 IN EFI_MEMORY_TYPE NewType,\r
985 IN UINTN Alignment\r
986 )\r
28a00297 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
022c6d45 1001\r
28a00297 1002 //\r
1003 // If MaxAddress is not aligned to the end of a page\r
1004 //\r
022c6d45 1005\r
28a00297 1006 //\r
1007 // Change MaxAddress to be 1 page lower\r
1008 //\r
1009 MaxAddress -= (EFI_PAGE_MASK + 1);\r
022c6d45 1010\r
28a00297 1011 //\r
1012 // Set MaxAddress to a page boundary\r
1013 //\r
6e1e5405 1014 MaxAddress &= ~(UINT64)EFI_PAGE_MASK;\r
022c6d45 1015\r
28a00297 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
022c6d45 1027\r
28a00297 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
2345e7d4 1039 // If desc is past max allowed address or below min allowed address, skip it\r
28a00297 1040 //\r
2345e7d4 1041 if ((DescStart >= MaxAddress) || (DescEnd < MinAddress)) {\r
28a00297 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 //\r
022c6d45 1055 // Compute the number of bytes we can used from this\r
28a00297 1056 // descriptor, and see it's enough to satisfy the request\r
1057 //\r
1058 DescNumberOfBytes = DescEnd - DescStart + 1;\r
1059\r
1060 if (DescNumberOfBytes >= NumberOfBytes) {\r
2345e7d4 1061 //\r
1062 // If the start of the allocated range is below the min address allowed, skip it\r
1063 //\r
1064 if ((DescEnd - NumberOfBytes + 1) < MinAddress) {\r
1065 continue;\r
1066 }\r
28a00297 1067\r
1068 //\r
1069 // If this is the best match so far remember it\r
1070 //\r
1071 if (DescEnd > Target) {\r
1072 Target = DescEnd;\r
1073 }\r
1074 }\r
022c6d45 1075 }\r
28a00297 1076\r
1077 //\r
1078 // If this is a grow down, adjust target to be the allocation base\r
1079 //\r
1080 Target -= NumberOfBytes - 1;\r
1081\r
1082 //\r
1083 // If we didn't find a match, return 0\r
1084 //\r
1085 if ((Target & EFI_PAGE_MASK) != 0) {\r
1086 return 0;\r
1087 }\r
1088\r
1089 return Target;\r
1090}\r
1091\r
162ed594 1092\r
1093/**\r
1094 Internal function. Finds a consecutive free page range below\r
1095 the requested address\r
1096\r
022c6d45 1097 @param MaxAddress The address that the range must be below\r
1098 @param NoPages Number of pages needed\r
1099 @param NewType The type of memory the range is going to be\r
1100 turned into\r
1101 @param Alignment Bits to align with\r
162ed594 1102\r
1103 @return The base address of the range, or 0 if the range was not found.\r
1104\r
1105**/\r
28a00297 1106UINT64\r
1107FindFreePages (\r
1108 IN UINT64 MaxAddress,\r
1109 IN UINT64 NoPages,\r
1110 IN EFI_MEMORY_TYPE NewType,\r
1111 IN UINTN Alignment\r
1112 )\r
28a00297 1113{\r
2345e7d4 1114 UINT64 Start;\r
28a00297 1115\r
2345e7d4 1116 //\r
1117 // Attempt to find free pages in the preferred bin based on the requested memory type\r
1118 //\r
3d78c020 1119 if ((UINT32)NewType < EfiMaxMemoryType && MaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {\r
2345e7d4 1120 Start = CoreFindFreePagesI (\r
1121 mMemoryTypeStatistics[NewType].MaximumAddress, \r
1122 mMemoryTypeStatistics[NewType].BaseAddress, \r
1123 NoPages, \r
1124 NewType, \r
1125 Alignment\r
1126 );\r
1127 if (Start != 0) {\r
1128 return Start;\r
1129 }\r
1130 }\r
28a00297 1131\r
2345e7d4 1132 //\r
1133 // Attempt to find free pages in the default allocation bin\r
1134 //\r
1135 if (MaxAddress >= mDefaultMaximumAddress) {\r
1136 Start = CoreFindFreePagesI (mDefaultMaximumAddress, 0, NoPages, NewType, Alignment);\r
1137 if (Start != 0) {\r
1138 if (Start < mDefaultBaseAddress) {\r
1139 mDefaultBaseAddress = Start;\r
1140 }\r
1141 return Start;\r
28a00297 1142 }\r
1143 }\r
1144\r
2345e7d4 1145 //\r
1146 // The allocation did not succeed in any of the prefered bins even after \r
1147 // promoting resources. Attempt to find free pages anywhere is the requested \r
1148 // address range. If this allocation fails, then there are not enough \r
1149 // resources anywhere to satisfy the request.\r
1150 //\r
1151 Start = CoreFindFreePagesI (MaxAddress, 0, NoPages, NewType, Alignment);\r
1152 if (Start != 0) {\r
1153 return Start;\r
1154 }\r
28a00297 1155\r
2345e7d4 1156 //\r
1157 // If allocations from the preferred bins fail, then attempt to promote memory resources.\r
1158 //\r
1159 if (!PromoteMemoryResource ()) {\r
1160 return 0;\r
28a00297 1161 }\r
1162\r
2345e7d4 1163 //\r
1164 // If any memory resources were promoted, then re-attempt the allocation\r
1165 //\r
1166 return FindFreePages (MaxAddress, NoPages, NewType, Alignment);\r
28a00297 1167}\r
1168\r
1169\r
162ed594 1170/**\r
1171 Allocates pages from the memory map.\r
1172\r
022c6d45 1173 @param Type The type of allocation to perform\r
1174 @param MemoryType The type of memory to turn the allocated pages\r
1175 into\r
1176 @param NumberOfPages The number of pages to allocate\r
1177 @param Memory A pointer to receive the base allocated memory\r
1178 address\r
162ed594 1179\r
1180 @return Status. On success, Memory is filled in with the base address allocated\r
022c6d45 1181 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in\r
1182 spec.\r
1183 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r
1184 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r
162ed594 1185 @retval EFI_SUCCESS Pages successfully allocated.\r
1186\r
1187**/\r
28a00297 1188EFI_STATUS\r
1189EFIAPI\r
84edd20b 1190CoreInternalAllocatePages (\r
28a00297 1191 IN EFI_ALLOCATE_TYPE Type,\r
1192 IN EFI_MEMORY_TYPE MemoryType,\r
1193 IN UINTN NumberOfPages,\r
1194 IN OUT EFI_PHYSICAL_ADDRESS *Memory\r
1195 )\r
28a00297 1196{\r
1197 EFI_STATUS Status;\r
1198 UINT64 Start;\r
1199 UINT64 MaxAddress;\r
1200 UINTN Alignment;\r
1201\r
3d78c020 1202 if ((UINT32)Type >= MaxAllocateType) {\r
28a00297 1203 return EFI_INVALID_PARAMETER;\r
1204 }\r
1205\r
1206 if ((MemoryType >= EfiMaxMemoryType && MemoryType <= 0x7fffffff) ||\r
1207 MemoryType == EfiConventionalMemory) {\r
1208 return EFI_INVALID_PARAMETER;\r
1209 }\r
1210\r
3e058701
ED
1211 if (Memory == NULL) {\r
1212 return EFI_INVALID_PARAMETER;\r
1213 }\r
1214\r
28a00297 1215 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
1216\r
1217 if (MemoryType == EfiACPIReclaimMemory ||\r
1218 MemoryType == EfiACPIMemoryNVS ||\r
1219 MemoryType == EfiRuntimeServicesCode ||\r
1220 MemoryType == EfiRuntimeServicesData) {\r
1221\r
1222 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
1223 }\r
1224\r
1225 if (Type == AllocateAddress) {\r
1226 if ((*Memory & (Alignment - 1)) != 0) {\r
1227 return EFI_NOT_FOUND;\r
1228 }\r
1229 }\r
1230\r
1231 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1232 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1233\r
1234 //\r
022c6d45 1235 // If this is for below a particular address, then\r
28a00297 1236 //\r
1237 Start = *Memory;\r
022c6d45 1238\r
28a00297 1239 //\r
1240 // The max address is the max natively addressable address for the processor\r
1241 //\r
f3f2e05d 1242 MaxAddress = MAX_ADDRESS;\r
022c6d45 1243\r
28a00297 1244 if (Type == AllocateMaxAddress) {\r
1245 MaxAddress = Start;\r
1246 }\r
1247\r
1248 CoreAcquireMemoryLock ();\r
022c6d45 1249\r
28a00297 1250 //\r
1251 // If not a specific address, then find an address to allocate\r
1252 //\r
1253 if (Type != AllocateAddress) {\r
1254 Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment);\r
1255 if (Start == 0) {\r
1256 Status = EFI_OUT_OF_RESOURCES;\r
1257 goto Done;\r
1258 }\r
1259 }\r
1260\r
1261 //\r
1262 // Convert pages from FreeMemory to the requested type\r
1263 //\r
1264 Status = CoreConvertPages (Start, NumberOfPages, MemoryType);\r
1265\r
1266Done:\r
1267 CoreReleaseMemoryLock ();\r
1268\r
1269 if (!EFI_ERROR (Status)) {\r
1270 *Memory = Start;\r
1271 }\r
1272\r
1273 return Status;\r
1274}\r
1275\r
84edd20b
SZ
1276/**\r
1277 Allocates pages from the memory map.\r
1278\r
1279 @param Type The type of allocation to perform\r
1280 @param MemoryType The type of memory to turn the allocated pages\r
1281 into\r
1282 @param NumberOfPages The number of pages to allocate\r
1283 @param Memory A pointer to receive the base allocated memory\r
1284 address\r
1285\r
1286 @return Status. On success, Memory is filled in with the base address allocated\r
1287 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in\r
1288 spec.\r
1289 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r
1290 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r
1291 @retval EFI_SUCCESS Pages successfully allocated.\r
1292\r
1293**/\r
1294EFI_STATUS\r
1295EFIAPI\r
1296CoreAllocatePages (\r
1297 IN EFI_ALLOCATE_TYPE Type,\r
1298 IN EFI_MEMORY_TYPE MemoryType,\r
1299 IN UINTN NumberOfPages,\r
1300 OUT EFI_PHYSICAL_ADDRESS *Memory\r
1301 )\r
1302{\r
1303 EFI_STATUS Status;\r
1304\r
1305 Status = CoreInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);\r
1306 if (!EFI_ERROR (Status)) {\r
1307 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionAllocatePages, MemoryType, EFI_PAGES_TO_SIZE (NumberOfPages), (VOID *) (UINTN) *Memory);\r
1308 }\r
1309 return Status;\r
1310}\r
28a00297 1311\r
162ed594 1312/**\r
1313 Frees previous allocated pages.\r
1314\r
022c6d45 1315 @param Memory Base address of memory being freed\r
1316 @param NumberOfPages The number of pages to free\r
162ed594 1317\r
022c6d45 1318 @retval EFI_NOT_FOUND Could not find the entry that covers the range\r
1319 @retval EFI_INVALID_PARAMETER Address not aligned\r
162ed594 1320 @return EFI_SUCCESS -Pages successfully freed.\r
1321\r
1322**/\r
022c6d45 1323EFI_STATUS\r
28a00297 1324EFIAPI\r
84edd20b 1325CoreInternalFreePages (\r
28a00297 1326 IN EFI_PHYSICAL_ADDRESS Memory,\r
1327 IN UINTN NumberOfPages\r
1328 )\r
28a00297 1329{\r
1330 EFI_STATUS Status;\r
1331 LIST_ENTRY *Link;\r
1332 MEMORY_MAP *Entry;\r
1333 UINTN Alignment;\r
1334\r
1335 //\r
1336 // Free the range\r
1337 //\r
1338 CoreAcquireMemoryLock ();\r
1339\r
1340 //\r
1341 // Find the entry that the covers the range\r
1342 //\r
1343 Entry = NULL;\r
1344 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1345 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1346 if (Entry->Start <= Memory && Entry->End > Memory) {\r
1347 break;\r
1348 }\r
1349 }\r
1350 if (Link == &gMemoryMap) {\r
a5ca8fa7 1351 Status = EFI_NOT_FOUND;\r
1352 goto Done;\r
28a00297 1353 }\r
1354\r
1355 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
1356\r
525aded9 1357 ASSERT (Entry != NULL);\r
28a00297 1358 if (Entry->Type == EfiACPIReclaimMemory ||\r
1359 Entry->Type == EfiACPIMemoryNVS ||\r
1360 Entry->Type == EfiRuntimeServicesCode ||\r
1361 Entry->Type == EfiRuntimeServicesData) {\r
1362\r
1363 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
1364\r
1365 }\r
1366\r
1367 if ((Memory & (Alignment - 1)) != 0) {\r
a5ca8fa7 1368 Status = EFI_INVALID_PARAMETER;\r
1369 goto Done;\r
28a00297 1370 }\r
1371\r
1372 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1373 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1374\r
1375 Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
1376\r
28a00297 1377 if (EFI_ERROR (Status)) {\r
a5ca8fa7 1378 goto Done;\r
28a00297 1379 }\r
1380\r
a5ca8fa7 1381Done:\r
1382 CoreReleaseMemoryLock ();\r
28a00297 1383 return Status;\r
1384}\r
1385\r
84edd20b
SZ
1386/**\r
1387 Frees previous allocated pages.\r
1388\r
1389 @param Memory Base address of memory being freed\r
1390 @param NumberOfPages The number of pages to free\r
1391\r
1392 @retval EFI_NOT_FOUND Could not find the entry that covers the range\r
1393 @retval EFI_INVALID_PARAMETER Address not aligned\r
1394 @return EFI_SUCCESS -Pages successfully freed.\r
1395\r
1396**/\r
1397EFI_STATUS\r
1398EFIAPI\r
1399CoreFreePages (\r
1400 IN EFI_PHYSICAL_ADDRESS Memory,\r
1401 IN UINTN NumberOfPages\r
1402 )\r
1403{\r
1404 EFI_STATUS Status;\r
1405\r
1406 Status = CoreInternalFreePages (Memory, NumberOfPages);\r
1407 if (!EFI_ERROR (Status)) {\r
1408 CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionFreePages, 0, EFI_PAGES_TO_SIZE (NumberOfPages), (VOID *) (UINTN) Memory);\r
1409 }\r
1410 return Status;\r
1411}\r
1412\r
2345e7d4 1413/**\r
1414 This function checks to see if the last memory map descriptor in a memory map\r
1415 can be merged with any of the other memory map descriptors in a memorymap.\r
1416 Memory descriptors may be merged if they are adjacent and have the same type\r
1417 and attributes.\r
1418\r
1419 @param MemoryMap A pointer to the start of the memory map.\r
1420 @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.\r
1421 @param DescriptorSize The size, in bytes, of an individual\r
1422 EFI_MEMORY_DESCRIPTOR.\r
1423\r
1424 @return A pointer to the next available descriptor in MemoryMap\r
1425\r
1426**/\r
1427EFI_MEMORY_DESCRIPTOR *\r
1428MergeMemoryMapDescriptor (\r
1429 IN EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
1430 IN EFI_MEMORY_DESCRIPTOR *MemoryMapDescriptor,\r
1431 IN UINTN DescriptorSize\r
1432 )\r
1433{\r
1434 //\r
1435 // Traverse the array of descriptors in MemoryMap\r
1436 //\r
1437 for (; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) {\r
1438 //\r
1439 // Check to see if the Type fields are identical.\r
1440 //\r
1441 if (MemoryMap->Type != MemoryMapDescriptor->Type) {\r
1442 continue;\r
1443 }\r
1444\r
1445 //\r
1446 // Check to see if the Attribute fields are identical.\r
1447 //\r
1448 if (MemoryMap->Attribute != MemoryMapDescriptor->Attribute) {\r
1449 continue;\r
1450 }\r
1451\r
1452 //\r
1453 // Check to see if MemoryMapDescriptor is immediately above MemoryMap\r
1454 //\r
1455 if (MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) { \r
1456 //\r
1457 // Merge MemoryMapDescriptor into MemoryMap\r
1458 //\r
1459 MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r
1460\r
1461 //\r
1462 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r
1463 //\r
1464 return MemoryMapDescriptor;\r
1465 }\r
1466\r
1467 //\r
1468 // Check to see if MemoryMapDescriptor is immediately below MemoryMap\r
1469 //\r
1470 if (MemoryMap->PhysicalStart - EFI_PAGES_TO_SIZE ((UINTN)MemoryMapDescriptor->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {\r
1471 //\r
1472 // Merge MemoryMapDescriptor into MemoryMap\r
1473 //\r
1474 MemoryMap->PhysicalStart = MemoryMapDescriptor->PhysicalStart;\r
1475 MemoryMap->VirtualStart = MemoryMapDescriptor->VirtualStart;\r
1476 MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r
1477\r
1478 //\r
1479 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r
1480 //\r
1481 return MemoryMapDescriptor;\r
1482 }\r
1483 }\r
1484\r
1485 //\r
1486 // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.\r
1487 //\r
1488 // Return the slot immediately after MemoryMapDescriptor as the next available \r
1489 // slot in the MemoryMap array\r
1490 //\r
1491 return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor, DescriptorSize);\r
1492}\r
28a00297 1493\r
162ed594 1494/**\r
1495 This function returns a copy of the current memory map. The map is an array of\r
1496 memory descriptors, each of which describes a contiguous block of memory.\r
1497\r
022c6d45 1498 @param MemoryMapSize A pointer to the size, in bytes, of the\r
1499 MemoryMap buffer. On input, this is the size of\r
1500 the buffer allocated by the caller. On output,\r
1501 it is the size of the buffer returned by the\r
1502 firmware if the buffer was large enough, or the\r
1503 size of the buffer needed to contain the map if\r
1504 the buffer was too small.\r
1505 @param MemoryMap A pointer to the buffer in which firmware places\r
1506 the current memory map.\r
1507 @param MapKey A pointer to the location in which firmware\r
1508 returns the key for the current memory map.\r
1509 @param DescriptorSize A pointer to the location in which firmware\r
1510 returns the size, in bytes, of an individual\r
1511 EFI_MEMORY_DESCRIPTOR.\r
1512 @param DescriptorVersion A pointer to the location in which firmware\r
1513 returns the version number associated with the\r
1514 EFI_MEMORY_DESCRIPTOR.\r
1515\r
1516 @retval EFI_SUCCESS The memory map was returned in the MemoryMap\r
1517 buffer.\r
1518 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current\r
1519 buffer size needed to hold the memory map is\r
1520 returned in MemoryMapSize.\r
162ed594 1521 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
1522\r
1523**/\r
28a00297 1524EFI_STATUS\r
1525EFIAPI\r
1526CoreGetMemoryMap (\r
1527 IN OUT UINTN *MemoryMapSize,\r
1528 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
1529 OUT UINTN *MapKey,\r
1530 OUT UINTN *DescriptorSize,\r
1531 OUT UINT32 *DescriptorVersion\r
1532 )\r
28a00297 1533{\r
1534 EFI_STATUS Status;\r
022c6d45 1535 UINTN Size;\r
1536 UINTN BufferSize;\r
28a00297 1537 UINTN NumberOfRuntimeEntries;\r
1538 LIST_ENTRY *Link;\r
022c6d45 1539 MEMORY_MAP *Entry;\r
1540 EFI_GCD_MAP_ENTRY *GcdMapEntry;\r
b74350e9 1541 EFI_MEMORY_TYPE Type;\r
2345e7d4 1542 EFI_MEMORY_DESCRIPTOR *MemoryMapStart;\r
28a00297 1543\r
1544 //\r
1545 // Make sure the parameters are valid\r
1546 //\r
1547 if (MemoryMapSize == NULL) {\r
1548 return EFI_INVALID_PARAMETER;\r
1549 }\r
022c6d45 1550\r
28a00297 1551 CoreAcquireGcdMemoryLock ();\r
022c6d45 1552\r
28a00297 1553 //\r
1554 // Count the number of Reserved and MMIO entries that are marked for runtime use\r
1555 //\r
1556 NumberOfRuntimeEntries = 0;\r
1557 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
1558 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
1559 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1560 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {\r
1561 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
1562 NumberOfRuntimeEntries++;\r
1563 }\r
1564 }\r
1565 }\r
1566\r
1567 Size = sizeof (EFI_MEMORY_DESCRIPTOR);\r
1568\r
1569 //\r
1570 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will\r
1571 // prevent people from having pointer math bugs in their code.\r
1572 // now you have to use *DescriptorSize to make things work.\r
1573 //\r
1574 Size += sizeof(UINT64) - (Size % sizeof (UINT64));\r
1575\r
1576 if (DescriptorSize != NULL) {\r
1577 *DescriptorSize = Size;\r
1578 }\r
022c6d45 1579\r
28a00297 1580 if (DescriptorVersion != NULL) {\r
1581 *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;\r
1582 }\r
1583\r
1584 CoreAcquireMemoryLock ();\r
1585\r
1586 //\r
1587 // Compute the buffer size needed to fit the entire map\r
1588 //\r
1589 BufferSize = Size * NumberOfRuntimeEntries;\r
1590 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1591 BufferSize += Size;\r
1592 }\r
1593\r
1594 if (*MemoryMapSize < BufferSize) {\r
1595 Status = EFI_BUFFER_TOO_SMALL;\r
1596 goto Done;\r
1597 }\r
1598\r
1599 if (MemoryMap == NULL) {\r
1600 Status = EFI_INVALID_PARAMETER;\r
1601 goto Done;\r
1602 }\r
1603\r
1604 //\r
1605 // Build the map\r
1606 //\r
383c303c 1607 ZeroMem (MemoryMap, BufferSize);\r
2345e7d4 1608 MemoryMapStart = MemoryMap;\r
28a00297 1609 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1610 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1611 ASSERT (Entry->VirtualStart == 0);\r
1612\r
b74350e9 1613 //\r
1614 // Convert internal map into an EFI_MEMORY_DESCRIPTOR\r
1615 //\r
28a00297 1616 MemoryMap->Type = Entry->Type;\r
1617 MemoryMap->PhysicalStart = Entry->Start;\r
1618 MemoryMap->VirtualStart = Entry->VirtualStart;\r
1619 MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);\r
b74350e9 1620 //\r
1621 // If the memory type is EfiConventionalMemory, then determine if the range is part of a\r
022c6d45 1622 // memory type bin and needs to be converted to the same memory type as the rest of the\r
1623 // memory type bin in order to minimize EFI Memory Map changes across reboots. This\r
b74350e9 1624 // improves the chances for a successful S4 resume in the presence of minor page allocation\r
1625 // differences across reboots.\r
1626 //\r
1627 if (MemoryMap->Type == EfiConventionalMemory) {\r
1628 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
1629 if (mMemoryTypeStatistics[Type].Special &&\r
1630 mMemoryTypeStatistics[Type].NumberOfPages > 0 &&\r
1631 Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress &&\r
e94a9ff7 1632 Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) {\r
b74350e9 1633 MemoryMap->Type = Type;\r
1634 }\r
1635 }\r
1636 }\r
1637 MemoryMap->Attribute = Entry->Attribute;\r
10fe0d81
RN
1638 if (MemoryMap->Type < EfiMaxMemoryType) {\r
1639 if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {\r
1640 MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;\r
1641 }\r
28a00297 1642 }\r
022c6d45 1643\r
2345e7d4 1644 //\r
1645 // Check to see if the new Memory Map Descriptor can be merged with an \r
1646 // existing descriptor if they are adjacent and have the same attributes\r
1647 //\r
1648 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
28a00297 1649 }\r
1650\r
1651 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
1652 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
1653 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1654 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {\r
1655 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
dc8d93ca 1656 // \r
1657 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries\r
1658 // that are marked for runtime use\r
1659 //\r
28a00297 1660 MemoryMap->PhysicalStart = GcdMapEntry->BaseAddress;\r
1661 MemoryMap->VirtualStart = 0;\r
1662 MemoryMap->NumberOfPages = RShiftU64 ((GcdMapEntry->EndAddress - GcdMapEntry->BaseAddress + 1), EFI_PAGE_SHIFT);\r
1663 MemoryMap->Attribute = GcdMapEntry->Attributes & ~EFI_MEMORY_PORT_IO;\r
1664\r
1665 if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) {\r
1666 MemoryMap->Type = EfiReservedMemoryType;\r
1667 } else if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {\r
1668 if ((GcdMapEntry->Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {\r
1669 MemoryMap->Type = EfiMemoryMappedIOPortSpace;\r
1670 } else {\r
1671 MemoryMap->Type = EfiMemoryMappedIO;\r
1672 }\r
1673 }\r
1674\r
2345e7d4 1675 //\r
1676 // Check to see if the new Memory Map Descriptor can be merged with an \r
1677 // existing descriptor if they are adjacent and have the same attributes\r
1678 //\r
1679 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
28a00297 1680 }\r
1681 }\r
1682 }\r
022c6d45 1683\r
2345e7d4 1684 //\r
1685 // Compute the size of the buffer actually used after all memory map descriptor merge operations\r
1686 //\r
1687 BufferSize = ((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart);\r
1688\r
28a00297 1689 Status = EFI_SUCCESS;\r
1690\r
1691Done:\r
022c6d45 1692 //\r
1693 // Update the map key finally\r
1694 //\r
28a00297 1695 if (MapKey != NULL) {\r
1696 *MapKey = mMemoryMapKey;\r
1697 }\r
022c6d45 1698\r
e439df50 1699 CoreReleaseMemoryLock ();\r
1700\r
1701 CoreReleaseGcdMemoryLock ();\r
1702\r
28a00297 1703 *MemoryMapSize = BufferSize;\r
022c6d45 1704\r
28a00297 1705 return Status;\r
1706}\r
1707\r
28a00297 1708\r
162ed594 1709/**\r
28a00297 1710 Internal function. Used by the pool functions to allocate pages\r
1711 to back pool allocation requests.\r
1712\r
022c6d45 1713 @param PoolType The type of memory for the new pool pages\r
1714 @param NumberOfPages No of pages to allocate\r
1715 @param Alignment Bits to align.\r
28a00297 1716\r
162ed594 1717 @return The allocated memory, or NULL\r
28a00297 1718\r
162ed594 1719**/\r
1720VOID *\r
1721CoreAllocatePoolPages (\r
1722 IN EFI_MEMORY_TYPE PoolType,\r
1723 IN UINTN NumberOfPages,\r
1724 IN UINTN Alignment\r
1725 )\r
28a00297 1726{\r
1727 UINT64 Start;\r
1728\r
1729 //\r
1730 // Find the pages to convert\r
1731 //\r
f3f2e05d 1732 Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment);\r
28a00297 1733\r
1734 //\r
1735 // Convert it to boot services data\r
1736 //\r
1737 if (Start == 0) {\r
7df7393f 1738 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32)NumberOfPages));\r
28a00297 1739 } else {\r
1740 CoreConvertPages (Start, NumberOfPages, PoolType);\r
1741 }\r
1742\r
e94a9ff7 1743 return (VOID *)(UINTN) Start;\r
28a00297 1744}\r
1745\r
162ed594 1746\r
1747/**\r
1748 Internal function. Frees pool pages allocated via AllocatePoolPages ()\r
1749\r
022c6d45 1750 @param Memory The base address to free\r
162ed594 1751 @param NumberOfPages The number of pages to free\r
1752\r
1753**/\r
28a00297 1754VOID\r
1755CoreFreePoolPages (\r
1756 IN EFI_PHYSICAL_ADDRESS Memory,\r
1757 IN UINTN NumberOfPages\r
1758 )\r
28a00297 1759{\r
1760 CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
1761}\r
1762\r
1763\r
28a00297 1764\r
162ed594 1765/**\r
1766 Make sure the memory map is following all the construction rules,\r
28a00297 1767 it is the last time to check memory map error before exit boot services.\r
1768\r
022c6d45 1769 @param MapKey Memory map key\r
28a00297 1770\r
022c6d45 1771 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction\r
1772 rules.\r
162ed594 1773 @retval EFI_SUCCESS Valid memory map.\r
28a00297 1774\r
162ed594 1775**/\r
1776EFI_STATUS\r
1777CoreTerminateMemoryMap (\r
1778 IN UINTN MapKey\r
1779 )\r
28a00297 1780{\r
1781 EFI_STATUS Status;\r
1782 LIST_ENTRY *Link;\r
1783 MEMORY_MAP *Entry;\r
1784\r
1785 Status = EFI_SUCCESS;\r
1786\r
1787 CoreAcquireMemoryLock ();\r
1788\r
1789 if (MapKey == mMemoryMapKey) {\r
1790\r
1791 //\r
1792 // Make sure the memory map is following all the construction rules\r
1793 // This is the last chance we will be able to display any messages on\r
1794 // the console devices.\r
1795 //\r
1796\r
1797 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1798 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
6e536468 1799 if ((Entry->Attribute & EFI_MEMORY_RUNTIME) != 0) {\r
28a00297 1800 if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS) {\r
d45fd260 1801 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));\r
1802 Status = EFI_INVALID_PARAMETER;\r
1803 goto Done;\r
28a00297 1804 }\r
6e536468 1805 if ((Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
d45fd260 1806 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
1807 Status = EFI_INVALID_PARAMETER;\r
1808 goto Done;\r
28a00297 1809 }\r
6e536468 1810 if (((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
d45fd260 1811 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
1812 Status = EFI_INVALID_PARAMETER;\r
1813 goto Done;\r
28a00297 1814 }\r
1815 }\r
1816 }\r
1817\r
1818 //\r
1819 // The map key they gave us matches what we expect. Fall through and\r
1820 // return success. In an ideal world we would clear out all of\r
1821 // EfiBootServicesCode and EfiBootServicesData. However this function\r
1822 // is not the last one called by ExitBootServices(), so we have to\r
1823 // preserve the memory contents.\r
1824 //\r
1825 } else {\r
1826 Status = EFI_INVALID_PARAMETER;\r
1827 }\r
1828\r
d45fd260 1829Done:\r
28a00297 1830 CoreReleaseMemoryLock ();\r
1831\r
1832 return Status;\r
1833}\r
1834\r
1835\r
1836\r
1837\r
1838\r
1839\r
1840\r
1841\r
162ed594 1842\r