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