]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Dxe/Mem/Page.c
revise the debug message to add 0x in front of the HEX number for consistency
[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
23c98c94 4Copyright (c) 2007 - 2008, Intel Corporation. <BR>\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
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
28a00297 31} EFI_MEMORY_TYPE_STAISTICS;\r
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
55EFI_MEMORY_TYPE_STAISTICS 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
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
90 { EfiMaxMemoryType, 0 }\r
91};\r
54ea99a7 92//\r
93// Only used when load module at fixed address feature is enabled. True means the memory is alreay successfully allocated\r
94// 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
95// address assigned by DXE core.\r
96//\r
97GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN gLoadFixedAddressCodeMemoryReady = FALSE;\r
d9177625 98\r
162ed594 99/**\r
d9177625 100 Enter critical section by gaining lock on gMemoryLock.\r
162ed594 101\r
102**/\r
23c98c94 103VOID\r
d9177625 104CoreAcquireMemoryLock (\r
105 VOID\r
106 )\r
107{\r
108 CoreAcquireLock (&gMemoryLock);\r
109}\r
110\r
111\r
112\r
113/**\r
114 Exit critical section by releasing lock on gMemoryLock.\r
115\r
116**/\r
117VOID\r
118CoreReleaseMemoryLock (\r
28a00297 119 VOID\r
d9177625 120 )\r
121{\r
122 CoreReleaseLock (&gMemoryLock);\r
123}\r
124\r
125\r
126\r
127\r
128/**\r
129 Internal function. Removes a descriptor entry.\r
130\r
131 @param Entry The entry to remove\r
132\r
133**/\r
134VOID\r
135RemoveMemoryMapEntry (\r
136 IN OUT MEMORY_MAP *Entry\r
137 )\r
138{\r
139 RemoveEntryList (&Entry->Link);\r
140 Entry->Link.ForwardLink = NULL;\r
141\r
142 if (Entry->FromPages) {\r
143 //\r
144 // Insert the free memory map descriptor to the end of mFreeMemoryMapEntryList\r
145 //\r
146 InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
147 }\r
148}\r
162ed594 149\r
150/**\r
151 Internal function. Adds a ranges to the memory map.\r
152 The range must not already exist in the map.\r
153\r
022c6d45 154 @param Type The type of memory range to add\r
155 @param Start The starting address in the memory range Must be\r
156 paged aligned\r
157 @param End The last address in the range Must be the last\r
158 byte of a page\r
159 @param Attribute The attributes of the memory range to add\r
28a00297 160\r
162ed594 161**/\r
28a00297 162VOID\r
163CoreAddRange (\r
164 IN EFI_MEMORY_TYPE Type,\r
165 IN EFI_PHYSICAL_ADDRESS Start,\r
166 IN EFI_PHYSICAL_ADDRESS End,\r
167 IN UINT64 Attribute\r
d9177625 168 )\r
169{\r
170 LIST_ENTRY *Link;\r
171 MEMORY_MAP *Entry;\r
28a00297 172\r
d9177625 173 ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
174 ASSERT (End > Start) ;\r
162ed594 175\r
d9177625 176 ASSERT_LOCKED (&gMemoryLock);\r
28a00297 177\r
d9177625 178 DEBUG ((DEBUG_PAGE, "AddRange: %lx-%lx to %d\n", Start, End, Type));\r
162ed594 179\r
d9177625 180 //\r
181 // Memory map being altered so updated key\r
182 //\r
183 mMemoryMapKey += 1;\r
162ed594 184\r
d9177625 185 //\r
186 // UEFI 2.0 added an event group for notificaiton on memory map changes.\r
187 // So we need to signal this Event Group every time the memory map changes.\r
188 // If we are in EFI 1.10 compatability mode no event groups will be\r
189 // found and nothing will happen we we call this function. These events\r
190 // will get signaled but since a lock is held around the call to this\r
191 // function the notificaiton events will only be called after this funciton\r
192 // returns and the lock is released.\r
193 //\r
194 CoreNotifySignalList (&gEfiEventMemoryMapChangeGuid);\r
162ed594 195\r
d9177625 196 //\r
197 // Look for adjoining memory descriptor\r
198 //\r
28a00297 199\r
d9177625 200 // Two memory descriptors can only be merged if they have the same Type\r
201 // and the same Attribute\r
202 //\r
162ed594 203\r
d9177625 204 Link = gMemoryMap.ForwardLink;\r
205 while (Link != &gMemoryMap) {\r
206 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
207 Link = Link->ForwardLink;\r
162ed594 208\r
d9177625 209 if (Entry->Type != Type) {\r
210 continue;\r
211 }\r
212\r
213 if (Entry->Attribute != Attribute) {\r
214 continue;\r
215 }\r
216\r
217 if (Entry->End + 1 == Start) {\r
218\r
219 Start = Entry->Start;\r
220 RemoveMemoryMapEntry (Entry);\r
221\r
222 } else if (Entry->Start == End + 1) {\r
223\r
224 End = Entry->End;\r
225 RemoveMemoryMapEntry (Entry);\r
226 }\r
227 }\r
228\r
229 //\r
230 // Add descriptor\r
231 //\r
232\r
233 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
234 mMapStack[mMapDepth].FromPages = FALSE;\r
235 mMapStack[mMapDepth].Type = Type;\r
236 mMapStack[mMapDepth].Start = Start;\r
237 mMapStack[mMapDepth].End = End;\r
238 mMapStack[mMapDepth].VirtualStart = 0;\r
239 mMapStack[mMapDepth].Attribute = Attribute;\r
240 InsertTailList (&gMemoryMap, &mMapStack[mMapDepth].Link);\r
241\r
242 mMapDepth += 1;\r
243 ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
244\r
245 return ;\r
246}\r
022c6d45 247\r
162ed594 248/**\r
249 Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.\r
250 If the list is emtry, then allocate a new page to refuel the list.\r
251 Please Note this algorithm to allocate the memory map descriptor has a property\r
252 that the memory allocated for memory entries always grows, and will never really be freed\r
253 For example, if the current boot uses 2000 memory map entries at the maximum point, but\r
254 ends up with only 50 at the time the OS is booted, then the memory associated with the 1950\r
255 memory map entries is still allocated from EfiBootServicesMemory.\r
256\r
257\r
258 @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList\r
259\r
260**/\r
28a00297 261MEMORY_MAP *\r
262AllocateMemoryMapEntry (\r
263 VOID\r
d9177625 264 )\r
265{\r
266 MEMORY_MAP* FreeDescriptorEntries;\r
267 MEMORY_MAP* Entry;\r
268 UINTN Index;\r
269\r
270 if (IsListEmpty (&mFreeMemoryMapEntryList)) {\r
271 //\r
272 // The list is empty, to allocate one page to refuel the list\r
273 //\r
274 FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData, EFI_SIZE_TO_PAGES(DEFAULT_PAGE_ALLOCATION), DEFAULT_PAGE_ALLOCATION);\r
275 if(FreeDescriptorEntries != NULL) {\r
276 //\r
277 // Enque the free memmory map entries into the list\r
278 //\r
279 for (Index = 0; Index< DEFAULT_PAGE_ALLOCATION / sizeof(MEMORY_MAP); Index++) {\r
280 FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;\r
281 InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);\r
282 }\r
283 } else {\r
284 return NULL;\r
285 }\r
286 }\r
287 //\r
288 // dequeue the first descriptor from the list\r
289 //\r
290 Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
291 RemoveEntryList (&Entry->Link);\r
292\r
293 return Entry;\r
294}\r
022c6d45 295\r
162ed594 296\r
297/**\r
d9177625 298 Internal function. Moves any memory descriptors that are on the\r
299 temporary descriptor stack to heap.\r
162ed594 300\r
301**/\r
28a00297 302VOID\r
d9177625 303CoreFreeMemoryMapStack (\r
28a00297 304 VOID\r
305 )\r
28a00297 306{\r
d9177625 307 MEMORY_MAP *Entry;\r
308 MEMORY_MAP *Entry2;\r
309 LIST_ENTRY *Link2;\r
28a00297 310\r
d9177625 311 ASSERT_LOCKED (&gMemoryLock);\r
28a00297 312\r
d9177625 313 //\r
314 // If already freeing the map stack, then return\r
315 //\r
316 if (mFreeMapStack != 0) {\r
317 return ;\r
318 }\r
162ed594 319\r
d9177625 320 //\r
321 // Move the temporary memory descriptor stack into pool\r
322 //\r
323 mFreeMapStack += 1;\r
162ed594 324\r
d9177625 325 while (mMapDepth != 0) {\r
326 //\r
327 // Deque an memory map entry from mFreeMemoryMapEntryList\r
328 //\r
329 Entry = AllocateMemoryMapEntry ();\r
330\r
331 ASSERT (Entry);\r
332\r
333 //\r
334 // Update to proper entry\r
335 //\r
336 mMapDepth -= 1;\r
337\r
338 if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {\r
339\r
340 //\r
341 // Move this entry to general memory\r
342 //\r
343 RemoveEntryList (&mMapStack[mMapDepth].Link);\r
344 mMapStack[mMapDepth].Link.ForwardLink = NULL;\r
345\r
346 CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));\r
347 Entry->FromPages = TRUE;\r
348\r
349 //\r
350 // Find insertion location\r
351 //\r
352 for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) {\r
353 Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
354 if (Entry2->FromPages && Entry2->Start > Entry->Start) {\r
355 break;\r
356 }\r
357 }\r
358\r
359 InsertTailList (Link2, &Entry->Link);\r
360\r
361 } else {\r
362 //\r
363 // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,\r
364 // so here no need to move it to memory.\r
365 //\r
366 InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r
367 }\r
368 }\r
28a00297 369\r
d9177625 370 mFreeMapStack -= 1;\r
371}\r
162ed594 372\r
373/**\r
374 Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.\r
375\r
376**/\r
28a00297 377VOID\r
378PromoteMemoryResource (\r
379 VOID\r
380 )\r
28a00297 381{\r
382 LIST_ENTRY *Link;\r
383 EFI_GCD_MAP_ENTRY *Entry;\r
384\r
d45fd260 385 DEBUG ((DEBUG_PAGE, "Promote the memory resource\n"));\r
022c6d45 386\r
28a00297 387 CoreAcquireGcdMemoryLock ();\r
022c6d45 388\r
28a00297 389 Link = mGcdMemorySpaceMap.ForwardLink;\r
390 while (Link != &mGcdMemorySpaceMap) {\r
391\r
392 Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
393\r
394 if (Entry->GcdMemoryType == EfiGcdMemoryTypeReserved &&\r
f3f2e05d 395 Entry->EndAddress < MAX_ADDRESS &&\r
28a00297 396 (Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r
397 (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) {\r
398 //\r
399 // Update the GCD map\r
400 //\r
401 Entry->GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r
402 Entry->Capabilities |= EFI_MEMORY_TESTED;\r
403 Entry->ImageHandle = gDxeCoreImageHandle;\r
404 Entry->DeviceHandle = NULL;\r
405\r
406 //\r
407 // Add to allocable system memory resource\r
022c6d45 408 //\r
28a00297 409\r
410 CoreAddRange (\r
022c6d45 411 EfiConventionalMemory,\r
412 Entry->BaseAddress,\r
413 Entry->EndAddress,\r
28a00297 414 Entry->Capabilities & ~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r
415 );\r
416 CoreFreeMemoryMapStack ();\r
022c6d45 417\r
28a00297 418 }\r
419\r
420 Link = Link->ForwardLink;\r
421 }\r
022c6d45 422\r
28a00297 423 CoreReleaseGcdMemoryLock ();\r
022c6d45 424\r
28a00297 425 return;\r
426}\r
54ea99a7 427/**\r
428 This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD \r
429 PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the \r
430 size of boot time and runtime code.\r
28a00297 431\r
54ea99a7 432**/\r
433VOID\r
434CoreLoadingFixedAddressHook (\r
435 VOID\r
436 )\r
437{\r
438 UINT32 RuntimeCodePageNumber;\r
439 UINT32 BootTimeCodePageNumber;\r
440 EFI_PHYSICAL_ADDRESS RuntimeCodeBase;\r
441 EFI_PHYSICAL_ADDRESS BootTimeCodeBase;\r
442 EFI_STATUS Status;\r
443\r
444 //\r
445 // Make sure these 2 areas are not initialzied.\r
446 //\r
447 if (!gLoadFixedAddressCodeMemoryReady) { \r
448 RuntimeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
449 BootTimeCodePageNumber= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
450 RuntimeCodeBase = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - EFI_PAGES_TO_SIZE (RuntimeCodePageNumber));\r
451 BootTimeCodeBase = (EFI_PHYSICAL_ADDRESS)(RuntimeCodeBase - EFI_PAGES_TO_SIZE (BootTimeCodePageNumber));\r
452 //\r
453 // Try to allocate runtime memory.\r
454 //\r
455 Status = CoreAllocatePages (\r
456 AllocateAddress,\r
457 EfiRuntimeServicesCode,\r
458 RuntimeCodePageNumber,\r
459 &RuntimeCodeBase\r
460 );\r
461 if (EFI_ERROR(Status)) {\r
462 //\r
463 // Runtime memory allocation failed \r
464 //\r
465 return;\r
466 }\r
467 //\r
468 // Try to allocate boot memory.\r
469 //\r
470 Status = CoreAllocatePages (\r
471 AllocateAddress,\r
472 EfiBootServicesCode,\r
473 BootTimeCodePageNumber,\r
474 &BootTimeCodeBase\r
475 );\r
476 if (EFI_ERROR(Status)) {\r
477 //\r
478 // boot memory allocation failed. Free Runtime code range and will try the allocation again when \r
479 // new memory range is installed.\r
480 //\r
481 CoreFreePages (\r
482 RuntimeCodeBase,\r
483 RuntimeCodePageNumber\r
484 );\r
485 return;\r
486 }\r
487 gLoadFixedAddressCodeMemoryReady = TRUE;\r
488 } \r
489 return;\r
490} \r
28a00297 491\r
162ed594 492/**\r
28a00297 493 Called to initialize the memory map and add descriptors to\r
494 the current descriptor list.\r
28a00297 495 The first descriptor that is added must be general usable\r
496 memory as the addition allocates heap.\r
497\r
022c6d45 498 @param Type The type of memory to add\r
499 @param Start The starting address in the memory range Must be\r
500 page aligned\r
501 @param NumberOfPages The number of pages in the range\r
502 @param Attribute Attributes of the memory to add\r
28a00297 503\r
162ed594 504 @return None. The range is added to the memory map\r
28a00297 505\r
162ed594 506**/\r
507VOID\r
508CoreAddMemoryDescriptor (\r
509 IN EFI_MEMORY_TYPE Type,\r
510 IN EFI_PHYSICAL_ADDRESS Start,\r
511 IN UINT64 NumberOfPages,\r
512 IN UINT64 Attribute\r
513 )\r
28a00297 514{\r
515 EFI_PHYSICAL_ADDRESS End;\r
516 EFI_STATUS Status;\r
517 UINTN Index;\r
518 UINTN FreeIndex;\r
54ea99a7 519 \r
28a00297 520 if ((Start & EFI_PAGE_MASK) != 0) {\r
521 return;\r
522 }\r
523\r
524 if (Type >= EfiMaxMemoryType && Type <= 0x7fffffff) {\r
525 return;\r
526 }\r
28a00297 527 CoreAcquireMemoryLock ();\r
528 End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
529 CoreAddRange (Type, Start, End, Attribute);\r
530 CoreFreeMemoryMapStack ();\r
531 CoreReleaseMemoryLock ();\r
532\r
54ea99a7 533 //\r
534 // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type\r
535 //\r
852081fc 536 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
54ea99a7 537 CoreLoadingFixedAddressHook();\r
538 }\r
539 \r
28a00297 540 //\r
541 // Check to see if the statistics for the different memory types have already been established\r
542 //\r
543 if (mMemoryTypeInformationInitialized) {\r
544 return;\r
545 }\r
546\r
54ea99a7 547 \r
28a00297 548 //\r
549 // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array\r
550 //\r
551 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
552 //\r
553 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
554 //\r
555 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
556 if (Type < 0 || Type > EfiMaxMemoryType) {\r
557 continue;\r
558 }\r
28a00297 559 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
560 //\r
561 // Allocate pages for the current memory type from the top of available memory\r
562 //\r
563 Status = CoreAllocatePages (\r
564 AllocateAnyPages,\r
565 Type,\r
566 gMemoryTypeInformation[Index].NumberOfPages,\r
567 &mMemoryTypeStatistics[Type].BaseAddress\r
568 );\r
569 if (EFI_ERROR (Status)) {\r
570 //\r
022c6d45 571 // If an error occurs allocating the pages for the current memory type, then\r
28a00297 572 // free all the pages allocates for the previous memory types and return. This\r
573 // operation with be retied when/if more memory is added to the system\r
574 //\r
575 for (FreeIndex = 0; FreeIndex < Index; FreeIndex++) {\r
576 //\r
577 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
578 //\r
579 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type);\r
580 if (Type < 0 || Type > EfiMaxMemoryType) {\r
581 continue;\r
582 }\r
583\r
584 if (gMemoryTypeInformation[FreeIndex].NumberOfPages != 0) {\r
585 CoreFreePages (\r
022c6d45 586 mMemoryTypeStatistics[Type].BaseAddress,\r
28a00297 587 gMemoryTypeInformation[FreeIndex].NumberOfPages\r
588 );\r
589 mMemoryTypeStatistics[Type].BaseAddress = 0;\r
f3f2e05d 590 mMemoryTypeStatistics[Type].MaximumAddress = MAX_ADDRESS;\r
28a00297 591 }\r
592 }\r
593 return;\r
594 }\r
595\r
596 //\r
597 // Compute the address at the top of the current statistics\r
598 //\r
022c6d45 599 mMemoryTypeStatistics[Type].MaximumAddress =\r
600 mMemoryTypeStatistics[Type].BaseAddress +\r
28a00297 601 LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT) - 1;\r
602\r
603 //\r
022c6d45 604 // If the current base address is the lowest address so far, then update the default\r
28a00297 605 // maximum address\r
606 //\r
607 if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) {\r
608 mDefaultMaximumAddress = mMemoryTypeStatistics[Type].BaseAddress - 1;\r
609 }\r
610 }\r
611 }\r
612\r
613 //\r
614 // There was enough system memory for all the the memory types were allocated. So,\r
615 // those memory areas can be freed for future allocations, and all future memory\r
616 // allocations can occur within their respective bins\r
617 //\r
618 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
619 //\r
620 // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r
621 //\r
622 Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r
623 if (Type < 0 || Type > EfiMaxMemoryType) {\r
624 continue;\r
625 }\r
28a00297 626 if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r
627 CoreFreePages (\r
022c6d45 628 mMemoryTypeStatistics[Type].BaseAddress,\r
28a00297 629 gMemoryTypeInformation[Index].NumberOfPages\r
630 );\r
b74350e9 631 mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages;\r
28a00297 632 gMemoryTypeInformation[Index].NumberOfPages = 0;\r
633 }\r
634 }\r
635\r
636 //\r
637 // If the number of pages reserved for a memory type is 0, then all allocations for that type\r
638 // should be in the default range.\r
639 //\r
640 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
641 for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r
642 if (Type == (EFI_MEMORY_TYPE)gMemoryTypeInformation[Index].Type) {\r
643 mMemoryTypeStatistics[Type].InformationIndex = Index;\r
644 }\r
645 }\r
646 mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0;\r
f3f2e05d 647 if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ADDRESS) {\r
28a00297 648 mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress;\r
649 }\r
650 }\r
651\r
652 mMemoryTypeInformationInitialized = TRUE;\r
653}\r
654\r
655\r
162ed594 656/**\r
657 Internal function. Converts a memory range to the specified type.\r
658 The range must exist in the memory map.\r
659\r
022c6d45 660 @param Start The first address of the range Must be page\r
661 aligned\r
662 @param NumberOfPages The number of pages to convert\r
663 @param NewType The new type for the memory range\r
162ed594 664\r
022c6d45 665 @retval EFI_INVALID_PARAMETER Invalid parameter\r
666 @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r
667 range or convertion not allowed.\r
668 @retval EFI_SUCCESS Successfully converts the memory range to the\r
162ed594 669 specified type.\r
670\r
671**/\r
28a00297 672EFI_STATUS\r
673CoreConvertPages (\r
674 IN UINT64 Start,\r
675 IN UINT64 NumberOfPages,\r
676 IN EFI_MEMORY_TYPE NewType\r
677 )\r
28a00297 678{\r
679\r
680 UINT64 NumberOfBytes;\r
681 UINT64 End;\r
682 UINT64 RangeEnd;\r
683 UINT64 Attribute;\r
684 LIST_ENTRY *Link;\r
685 MEMORY_MAP *Entry;\r
686\r
687 Entry = NULL;\r
688 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
689 End = Start + NumberOfBytes - 1;\r
690\r
691 ASSERT (NumberOfPages);\r
692 ASSERT ((Start & EFI_PAGE_MASK) == 0);\r
693 ASSERT (End > Start) ;\r
694 ASSERT_LOCKED (&gMemoryLock);\r
695\r
71f68914 696 if (NumberOfPages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start > (Start + NumberOfBytes))) {\r
28a00297 697 return EFI_INVALID_PARAMETER;\r
698 }\r
699\r
700 //\r
701 // Convert the entire range\r
702 //\r
703\r
704 while (Start < End) {\r
705\r
706 //\r
707 // Find the entry that the covers the range\r
708 //\r
709 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
710 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
711\r
712 if (Entry->Start <= Start && Entry->End > Start) {\r
713 break;\r
714 }\r
715 }\r
716\r
717 if (Link == &gMemoryMap) {\r
162ed594 718 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: failed to find range %lx - %lx\n", Start, End));\r
28a00297 719 return EFI_NOT_FOUND;\r
720 }\r
721\r
722 //\r
723 // Convert range to the end, or to the end of the descriptor\r
724 // if that's all we've got\r
725 //\r
726 RangeEnd = End;\r
525aded9 727\r
728 ASSERT (Entry != NULL);\r
28a00297 729 if (Entry->End < End) {\r
730 RangeEnd = Entry->End;\r
731 }\r
732\r
162ed594 733 DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to %d\n", Start, RangeEnd, NewType));\r
28a00297 734\r
735 //\r
736 // Debug code - verify conversion is allowed\r
737 //\r
738 if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) {\r
d45fd260 739 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types\n"));\r
28a00297 740 return EFI_NOT_FOUND;\r
022c6d45 741 }\r
28a00297 742\r
743 //\r
744 // Update counters for the number of pages allocated to each memory type\r
745 //\r
746 if (Entry->Type >= 0 && Entry->Type < EfiMaxMemoryType) {\r
022c6d45 747 if (Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress &&\r
28a00297 748 Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) {\r
749 if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {\r
750 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;\r
751 } else {\r
752 mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;\r
753 }\r
754 }\r
755 }\r
756\r
757 if (NewType >= 0 && NewType < EfiMaxMemoryType) {\r
758 if (Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) {\r
759 mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;\r
022c6d45 760 if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages >\r
28a00297 761 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {\r
762 gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;\r
763 }\r
764 }\r
765 }\r
766\r
767 //\r
768 // Pull range out of descriptor\r
769 //\r
770 if (Entry->Start == Start) {\r
022c6d45 771\r
28a00297 772 //\r
773 // Clip start\r
774 //\r
775 Entry->Start = RangeEnd + 1;\r
776\r
777 } else if (Entry->End == RangeEnd) {\r
022c6d45 778\r
28a00297 779 //\r
780 // Clip end\r
781 //\r
782 Entry->End = Start - 1;\r
783\r
784 } else {\r
785\r
786 //\r
787 // Pull it out of the center, clip current\r
788 //\r
022c6d45 789\r
28a00297 790 //\r
791 // Add a new one\r
792 //\r
793 mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r
794 mMapStack[mMapDepth].FromPages = FALSE;\r
795 mMapStack[mMapDepth].Type = Entry->Type;\r
796 mMapStack[mMapDepth].Start = RangeEnd+1;\r
797 mMapStack[mMapDepth].End = Entry->End;\r
798\r
799 //\r
800 // Inherit Attribute from the Memory Descriptor that is being clipped\r
801 //\r
802 mMapStack[mMapDepth].Attribute = Entry->Attribute;\r
803\r
804 Entry->End = Start - 1;\r
805 ASSERT (Entry->Start < Entry->End);\r
806\r
807 Entry = &mMapStack[mMapDepth];\r
808 InsertTailList (&gMemoryMap, &Entry->Link);\r
809\r
810 mMapDepth += 1;\r
811 ASSERT (mMapDepth < MAX_MAP_DEPTH);\r
812 }\r
813\r
814 //\r
022c6d45 815 // The new range inherits the same Attribute as the Entry\r
28a00297 816 //it is being cut out of\r
817 //\r
818 Attribute = Entry->Attribute;\r
819\r
820 //\r
821 // If the descriptor is empty, then remove it from the map\r
822 //\r
823 if (Entry->Start == Entry->End + 1) {\r
824 RemoveMemoryMapEntry (Entry);\r
825 Entry = NULL;\r
826 }\r
022c6d45 827\r
28a00297 828 //\r
829 // Add our new range in\r
830 //\r
831 CoreAddRange (NewType, Start, RangeEnd, Attribute);\r
832\r
833 //\r
834 // Move any map descriptor stack to general pool\r
835 //\r
836 CoreFreeMemoryMapStack ();\r
837\r
838 //\r
839 // Bump the starting address, and convert the next range\r
840 //\r
841 Start = RangeEnd + 1;\r
842 }\r
843\r
844 //\r
845 // Converted the whole range, done\r
846 //\r
847\r
848 return EFI_SUCCESS;\r
849}\r
850\r
851\r
162ed594 852\r
853/**\r
854 Internal function. Finds a consecutive free page range below\r
855 the requested address.\r
856\r
022c6d45 857 @param MaxAddress The address that the range must be below\r
858 @param NumberOfPages Number of pages needed\r
859 @param NewType The type of memory the range is going to be\r
860 turned into\r
861 @param Alignment Bits to align with\r
162ed594 862\r
863 @return The base address of the range, or 0 if the range was not found\r
864\r
865**/\r
28a00297 866UINT64\r
867CoreFindFreePagesI (\r
868 IN UINT64 MaxAddress,\r
869 IN UINT64 NumberOfPages,\r
870 IN EFI_MEMORY_TYPE NewType,\r
871 IN UINTN Alignment\r
872 )\r
28a00297 873{\r
874 UINT64 NumberOfBytes;\r
875 UINT64 Target;\r
876 UINT64 DescStart;\r
877 UINT64 DescEnd;\r
878 UINT64 DescNumberOfBytes;\r
879 LIST_ENTRY *Link;\r
880 MEMORY_MAP *Entry;\r
881\r
882 if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages == 0)) {\r
883 return 0;\r
884 }\r
885\r
886 if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {\r
022c6d45 887\r
28a00297 888 //\r
889 // If MaxAddress is not aligned to the end of a page\r
890 //\r
022c6d45 891\r
28a00297 892 //\r
893 // Change MaxAddress to be 1 page lower\r
894 //\r
895 MaxAddress -= (EFI_PAGE_MASK + 1);\r
022c6d45 896\r
28a00297 897 //\r
898 // Set MaxAddress to a page boundary\r
899 //\r
900 MaxAddress &= ~EFI_PAGE_MASK;\r
022c6d45 901\r
28a00297 902 //\r
903 // Set MaxAddress to end of the page\r
904 //\r
905 MaxAddress |= EFI_PAGE_MASK;\r
906 }\r
907\r
908 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
909 Target = 0;\r
910\r
911 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
912 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
022c6d45 913\r
28a00297 914 //\r
915 // If it's not a free entry, don't bother with it\r
916 //\r
917 if (Entry->Type != EfiConventionalMemory) {\r
918 continue;\r
919 }\r
920\r
921 DescStart = Entry->Start;\r
922 DescEnd = Entry->End;\r
923\r
924 //\r
925 // If desc is past max allowed address, skip it\r
926 //\r
927 if (DescStart >= MaxAddress) {\r
928 continue;\r
929 }\r
930\r
931 //\r
932 // If desc ends past max allowed address, clip the end\r
933 //\r
934 if (DescEnd >= MaxAddress) {\r
935 DescEnd = MaxAddress;\r
936 }\r
937\r
938 DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;\r
939\r
940 //\r
022c6d45 941 // Compute the number of bytes we can used from this\r
28a00297 942 // descriptor, and see it's enough to satisfy the request\r
943 //\r
944 DescNumberOfBytes = DescEnd - DescStart + 1;\r
945\r
946 if (DescNumberOfBytes >= NumberOfBytes) {\r
947\r
948 //\r
949 // If this is the best match so far remember it\r
950 //\r
951 if (DescEnd > Target) {\r
952 Target = DescEnd;\r
953 }\r
954 }\r
022c6d45 955 }\r
28a00297 956\r
957 //\r
958 // If this is a grow down, adjust target to be the allocation base\r
959 //\r
960 Target -= NumberOfBytes - 1;\r
961\r
962 //\r
963 // If we didn't find a match, return 0\r
964 //\r
965 if ((Target & EFI_PAGE_MASK) != 0) {\r
966 return 0;\r
967 }\r
968\r
969 return Target;\r
970}\r
971\r
162ed594 972\r
973/**\r
974 Internal function. Finds a consecutive free page range below\r
975 the requested address\r
976\r
022c6d45 977 @param MaxAddress The address that the range must be below\r
978 @param NoPages Number of pages needed\r
979 @param NewType The type of memory the range is going to be\r
980 turned into\r
981 @param Alignment Bits to align with\r
162ed594 982\r
983 @return The base address of the range, or 0 if the range was not found.\r
984\r
985**/\r
28a00297 986UINT64\r
987FindFreePages (\r
988 IN UINT64 MaxAddress,\r
989 IN UINT64 NoPages,\r
990 IN EFI_MEMORY_TYPE NewType,\r
991 IN UINTN Alignment\r
992 )\r
28a00297 993{\r
994 UINT64 NewMaxAddress;\r
995 UINT64 Start;\r
996\r
997 NewMaxAddress = MaxAddress;\r
998\r
999 if (NewType >= 0 && NewType < EfiMaxMemoryType && NewMaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {\r
1000 NewMaxAddress = mMemoryTypeStatistics[NewType].MaximumAddress;\r
1001 } else {\r
1002 if (NewMaxAddress > mDefaultMaximumAddress) {\r
1003 NewMaxAddress = mDefaultMaximumAddress;\r
1004 }\r
1005 }\r
1006\r
1007 Start = CoreFindFreePagesI (NewMaxAddress, NoPages, NewType, Alignment);\r
71f68914 1008 if (Start == 0) {\r
28a00297 1009 Start = CoreFindFreePagesI (MaxAddress, NoPages, NewType, Alignment);\r
71f68914 1010 if (Start == 0) {\r
28a00297 1011 //\r
1012 // Here means there may be no enough memory to use, so try to go through\r
1013 // all the memory descript to promote the untested memory directly\r
1014 //\r
1015 PromoteMemoryResource ();\r
1016\r
1017 //\r
1018 // Allocate memory again after the memory resource re-arranged\r
1019 //\r
1020 Start = CoreFindFreePagesI (MaxAddress, NoPages, NewType, Alignment);\r
1021 }\r
1022 }\r
1023\r
1024 return Start;\r
1025}\r
1026\r
1027\r
162ed594 1028\r
1029/**\r
1030 Allocates pages from the memory map.\r
1031\r
022c6d45 1032 @param Type The type of allocation to perform\r
1033 @param MemoryType The type of memory to turn the allocated pages\r
1034 into\r
1035 @param NumberOfPages The number of pages to allocate\r
1036 @param Memory A pointer to receive the base allocated memory\r
1037 address\r
162ed594 1038\r
1039 @return Status. On success, Memory is filled in with the base address allocated\r
022c6d45 1040 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in\r
1041 spec.\r
1042 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r
1043 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r
162ed594 1044 @retval EFI_SUCCESS Pages successfully allocated.\r
1045\r
1046**/\r
28a00297 1047EFI_STATUS\r
1048EFIAPI\r
1049CoreAllocatePages (\r
1050 IN EFI_ALLOCATE_TYPE Type,\r
1051 IN EFI_MEMORY_TYPE MemoryType,\r
1052 IN UINTN NumberOfPages,\r
1053 IN OUT EFI_PHYSICAL_ADDRESS *Memory\r
1054 )\r
28a00297 1055{\r
1056 EFI_STATUS Status;\r
1057 UINT64 Start;\r
1058 UINT64 MaxAddress;\r
1059 UINTN Alignment;\r
1060\r
1061 if (Type < AllocateAnyPages || Type >= (UINTN) MaxAllocateType) {\r
1062 return EFI_INVALID_PARAMETER;\r
1063 }\r
1064\r
1065 if ((MemoryType >= EfiMaxMemoryType && MemoryType <= 0x7fffffff) ||\r
1066 MemoryType == EfiConventionalMemory) {\r
1067 return EFI_INVALID_PARAMETER;\r
1068 }\r
1069\r
1070 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
1071\r
1072 if (MemoryType == EfiACPIReclaimMemory ||\r
1073 MemoryType == EfiACPIMemoryNVS ||\r
1074 MemoryType == EfiRuntimeServicesCode ||\r
1075 MemoryType == EfiRuntimeServicesData) {\r
1076\r
1077 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
1078 }\r
1079\r
1080 if (Type == AllocateAddress) {\r
1081 if ((*Memory & (Alignment - 1)) != 0) {\r
1082 return EFI_NOT_FOUND;\r
1083 }\r
1084 }\r
1085\r
1086 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1087 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1088\r
1089 //\r
022c6d45 1090 // If this is for below a particular address, then\r
28a00297 1091 //\r
1092 Start = *Memory;\r
022c6d45 1093\r
28a00297 1094 //\r
1095 // The max address is the max natively addressable address for the processor\r
1096 //\r
f3f2e05d 1097 MaxAddress = MAX_ADDRESS;\r
022c6d45 1098\r
28a00297 1099 if (Type == AllocateMaxAddress) {\r
1100 MaxAddress = Start;\r
1101 }\r
1102\r
1103 CoreAcquireMemoryLock ();\r
022c6d45 1104\r
28a00297 1105 //\r
1106 // If not a specific address, then find an address to allocate\r
1107 //\r
1108 if (Type != AllocateAddress) {\r
1109 Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment);\r
1110 if (Start == 0) {\r
1111 Status = EFI_OUT_OF_RESOURCES;\r
1112 goto Done;\r
1113 }\r
1114 }\r
1115\r
1116 //\r
1117 // Convert pages from FreeMemory to the requested type\r
1118 //\r
1119 Status = CoreConvertPages (Start, NumberOfPages, MemoryType);\r
1120\r
1121Done:\r
1122 CoreReleaseMemoryLock ();\r
1123\r
1124 if (!EFI_ERROR (Status)) {\r
1125 *Memory = Start;\r
1126 }\r
1127\r
1128 return Status;\r
1129}\r
1130\r
1131\r
162ed594 1132/**\r
1133 Frees previous allocated pages.\r
1134\r
022c6d45 1135 @param Memory Base address of memory being freed\r
1136 @param NumberOfPages The number of pages to free\r
162ed594 1137\r
022c6d45 1138 @retval EFI_NOT_FOUND Could not find the entry that covers the range\r
1139 @retval EFI_INVALID_PARAMETER Address not aligned\r
162ed594 1140 @return EFI_SUCCESS -Pages successfully freed.\r
1141\r
1142**/\r
022c6d45 1143EFI_STATUS\r
28a00297 1144EFIAPI\r
1145CoreFreePages (\r
1146 IN EFI_PHYSICAL_ADDRESS Memory,\r
1147 IN UINTN NumberOfPages\r
1148 )\r
28a00297 1149{\r
1150 EFI_STATUS Status;\r
1151 LIST_ENTRY *Link;\r
1152 MEMORY_MAP *Entry;\r
1153 UINTN Alignment;\r
1154\r
1155 //\r
1156 // Free the range\r
1157 //\r
1158 CoreAcquireMemoryLock ();\r
1159\r
1160 //\r
1161 // Find the entry that the covers the range\r
1162 //\r
1163 Entry = NULL;\r
1164 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1165 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1166 if (Entry->Start <= Memory && Entry->End > Memory) {\r
1167 break;\r
1168 }\r
1169 }\r
1170 if (Link == &gMemoryMap) {\r
1171 CoreReleaseMemoryLock ();\r
1172 return EFI_NOT_FOUND;\r
1173 }\r
1174\r
1175 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
1176\r
525aded9 1177 ASSERT (Entry != NULL);\r
28a00297 1178 if (Entry->Type == EfiACPIReclaimMemory ||\r
1179 Entry->Type == EfiACPIMemoryNVS ||\r
1180 Entry->Type == EfiRuntimeServicesCode ||\r
1181 Entry->Type == EfiRuntimeServicesData) {\r
1182\r
1183 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
1184\r
1185 }\r
1186\r
1187 if ((Memory & (Alignment - 1)) != 0) {\r
1188 CoreReleaseMemoryLock ();\r
1189 return EFI_INVALID_PARAMETER;\r
1190 }\r
1191\r
1192 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1193 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1194\r
1195 Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
1196\r
1197 CoreReleaseMemoryLock ();\r
1198\r
1199 if (EFI_ERROR (Status)) {\r
1200 return Status;\r
1201 }\r
1202\r
1203 //\r
1204 // Destroy the contents\r
1205 //\r
f3f2e05d 1206 if (Memory < MAX_ADDRESS) {\r
28a00297 1207 DEBUG_CLEAR_MEMORY ((VOID *)(UINTN)Memory, NumberOfPages << EFI_PAGE_SHIFT);\r
1208 }\r
022c6d45 1209\r
28a00297 1210 return Status;\r
1211}\r
1212\r
1213\r
162ed594 1214/**\r
1215 This function returns a copy of the current memory map. The map is an array of\r
1216 memory descriptors, each of which describes a contiguous block of memory.\r
1217\r
022c6d45 1218 @param MemoryMapSize A pointer to the size, in bytes, of the\r
1219 MemoryMap buffer. On input, this is the size of\r
1220 the buffer allocated by the caller. On output,\r
1221 it is the size of the buffer returned by the\r
1222 firmware if the buffer was large enough, or the\r
1223 size of the buffer needed to contain the map if\r
1224 the buffer was too small.\r
1225 @param MemoryMap A pointer to the buffer in which firmware places\r
1226 the current memory map.\r
1227 @param MapKey A pointer to the location in which firmware\r
1228 returns the key for the current memory map.\r
1229 @param DescriptorSize A pointer to the location in which firmware\r
1230 returns the size, in bytes, of an individual\r
1231 EFI_MEMORY_DESCRIPTOR.\r
1232 @param DescriptorVersion A pointer to the location in which firmware\r
1233 returns the version number associated with the\r
1234 EFI_MEMORY_DESCRIPTOR.\r
1235\r
1236 @retval EFI_SUCCESS The memory map was returned in the MemoryMap\r
1237 buffer.\r
1238 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current\r
1239 buffer size needed to hold the memory map is\r
1240 returned in MemoryMapSize.\r
162ed594 1241 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
1242\r
1243**/\r
28a00297 1244EFI_STATUS\r
1245EFIAPI\r
1246CoreGetMemoryMap (\r
1247 IN OUT UINTN *MemoryMapSize,\r
1248 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
1249 OUT UINTN *MapKey,\r
1250 OUT UINTN *DescriptorSize,\r
1251 OUT UINT32 *DescriptorVersion\r
1252 )\r
28a00297 1253{\r
1254 EFI_STATUS Status;\r
022c6d45 1255 UINTN Size;\r
1256 UINTN BufferSize;\r
28a00297 1257 UINTN NumberOfRuntimeEntries;\r
1258 LIST_ENTRY *Link;\r
022c6d45 1259 MEMORY_MAP *Entry;\r
1260 EFI_GCD_MAP_ENTRY *GcdMapEntry;\r
b74350e9 1261 EFI_MEMORY_TYPE Type;\r
28a00297 1262\r
1263 //\r
1264 // Make sure the parameters are valid\r
1265 //\r
1266 if (MemoryMapSize == NULL) {\r
1267 return EFI_INVALID_PARAMETER;\r
1268 }\r
022c6d45 1269\r
28a00297 1270 CoreAcquireGcdMemoryLock ();\r
022c6d45 1271\r
28a00297 1272 //\r
1273 // Count the number of Reserved and MMIO entries that are marked for runtime use\r
1274 //\r
1275 NumberOfRuntimeEntries = 0;\r
1276 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
1277 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
1278 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1279 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {\r
1280 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
1281 NumberOfRuntimeEntries++;\r
1282 }\r
1283 }\r
1284 }\r
1285\r
1286 Size = sizeof (EFI_MEMORY_DESCRIPTOR);\r
1287\r
1288 //\r
1289 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will\r
1290 // prevent people from having pointer math bugs in their code.\r
1291 // now you have to use *DescriptorSize to make things work.\r
1292 //\r
1293 Size += sizeof(UINT64) - (Size % sizeof (UINT64));\r
1294\r
1295 if (DescriptorSize != NULL) {\r
1296 *DescriptorSize = Size;\r
1297 }\r
022c6d45 1298\r
28a00297 1299 if (DescriptorVersion != NULL) {\r
1300 *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;\r
1301 }\r
1302\r
1303 CoreAcquireMemoryLock ();\r
1304\r
1305 //\r
1306 // Compute the buffer size needed to fit the entire map\r
1307 //\r
1308 BufferSize = Size * NumberOfRuntimeEntries;\r
1309 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1310 BufferSize += Size;\r
1311 }\r
1312\r
1313 if (*MemoryMapSize < BufferSize) {\r
1314 Status = EFI_BUFFER_TOO_SMALL;\r
1315 goto Done;\r
1316 }\r
1317\r
1318 if (MemoryMap == NULL) {\r
1319 Status = EFI_INVALID_PARAMETER;\r
1320 goto Done;\r
1321 }\r
1322\r
1323 //\r
1324 // Build the map\r
1325 //\r
383c303c 1326 ZeroMem (MemoryMap, BufferSize);\r
28a00297 1327 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1328 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1329 ASSERT (Entry->VirtualStart == 0);\r
1330\r
b74350e9 1331 //\r
1332 // Convert internal map into an EFI_MEMORY_DESCRIPTOR\r
1333 //\r
28a00297 1334 MemoryMap->Type = Entry->Type;\r
1335 MemoryMap->PhysicalStart = Entry->Start;\r
1336 MemoryMap->VirtualStart = Entry->VirtualStart;\r
1337 MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);\r
b74350e9 1338 //\r
1339 // If the memory type is EfiConventionalMemory, then determine if the range is part of a\r
022c6d45 1340 // memory type bin and needs to be converted to the same memory type as the rest of the\r
1341 // memory type bin in order to minimize EFI Memory Map changes across reboots. This\r
b74350e9 1342 // improves the chances for a successful S4 resume in the presence of minor page allocation\r
1343 // differences across reboots.\r
1344 //\r
1345 if (MemoryMap->Type == EfiConventionalMemory) {\r
1346 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
1347 if (mMemoryTypeStatistics[Type].Special &&\r
1348 mMemoryTypeStatistics[Type].NumberOfPages > 0 &&\r
1349 Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress &&\r
e94a9ff7 1350 Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) {\r
b74350e9 1351 MemoryMap->Type = Type;\r
1352 }\r
1353 }\r
1354 }\r
1355 MemoryMap->Attribute = Entry->Attribute;\r
1356 if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {\r
1357 MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;\r
28a00297 1358 }\r
022c6d45 1359\r
43025b29 1360 MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);\r
28a00297 1361 }\r
1362\r
1363 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
1364 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
1365 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1366 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {\r
1367 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
dc8d93ca 1368 // \r
1369 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries\r
1370 // that are marked for runtime use\r
1371 //\r
28a00297 1372 MemoryMap->PhysicalStart = GcdMapEntry->BaseAddress;\r
1373 MemoryMap->VirtualStart = 0;\r
1374 MemoryMap->NumberOfPages = RShiftU64 ((GcdMapEntry->EndAddress - GcdMapEntry->BaseAddress + 1), EFI_PAGE_SHIFT);\r
1375 MemoryMap->Attribute = GcdMapEntry->Attributes & ~EFI_MEMORY_PORT_IO;\r
1376\r
1377 if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) {\r
1378 MemoryMap->Type = EfiReservedMemoryType;\r
1379 } else if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {\r
1380 if ((GcdMapEntry->Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {\r
1381 MemoryMap->Type = EfiMemoryMappedIOPortSpace;\r
1382 } else {\r
1383 MemoryMap->Type = EfiMemoryMappedIO;\r
1384 }\r
1385 }\r
1386\r
43025b29 1387 MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);\r
28a00297 1388 }\r
1389 }\r
1390 }\r
022c6d45 1391\r
28a00297 1392 Status = EFI_SUCCESS;\r
1393\r
1394Done:\r
1395\r
1396 CoreReleaseMemoryLock ();\r
022c6d45 1397\r
28a00297 1398 CoreReleaseGcdMemoryLock ();\r
022c6d45 1399\r
1400 //\r
1401 // Update the map key finally\r
1402 //\r
28a00297 1403 if (MapKey != NULL) {\r
1404 *MapKey = mMemoryMapKey;\r
1405 }\r
022c6d45 1406\r
28a00297 1407 *MemoryMapSize = BufferSize;\r
022c6d45 1408\r
28a00297 1409 return Status;\r
1410}\r
1411\r
28a00297 1412\r
162ed594 1413/**\r
28a00297 1414 Internal function. Used by the pool functions to allocate pages\r
1415 to back pool allocation requests.\r
1416\r
022c6d45 1417 @param PoolType The type of memory for the new pool pages\r
1418 @param NumberOfPages No of pages to allocate\r
1419 @param Alignment Bits to align.\r
28a00297 1420\r
162ed594 1421 @return The allocated memory, or NULL\r
28a00297 1422\r
162ed594 1423**/\r
1424VOID *\r
1425CoreAllocatePoolPages (\r
1426 IN EFI_MEMORY_TYPE PoolType,\r
1427 IN UINTN NumberOfPages,\r
1428 IN UINTN Alignment\r
1429 )\r
28a00297 1430{\r
1431 UINT64 Start;\r
1432\r
1433 //\r
1434 // Find the pages to convert\r
1435 //\r
f3f2e05d 1436 Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment);\r
28a00297 1437\r
1438 //\r
1439 // Convert it to boot services data\r
1440 //\r
1441 if (Start == 0) {\r
7df7393f 1442 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32)NumberOfPages));\r
28a00297 1443 } else {\r
1444 CoreConvertPages (Start, NumberOfPages, PoolType);\r
1445 }\r
1446\r
e94a9ff7 1447 return (VOID *)(UINTN) Start;\r
28a00297 1448}\r
1449\r
162ed594 1450\r
1451/**\r
1452 Internal function. Frees pool pages allocated via AllocatePoolPages ()\r
1453\r
022c6d45 1454 @param Memory The base address to free\r
162ed594 1455 @param NumberOfPages The number of pages to free\r
1456\r
1457**/\r
28a00297 1458VOID\r
1459CoreFreePoolPages (\r
1460 IN EFI_PHYSICAL_ADDRESS Memory,\r
1461 IN UINTN NumberOfPages\r
1462 )\r
28a00297 1463{\r
1464 CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
1465}\r
1466\r
1467\r
28a00297 1468\r
162ed594 1469/**\r
1470 Make sure the memory map is following all the construction rules,\r
28a00297 1471 it is the last time to check memory map error before exit boot services.\r
1472\r
022c6d45 1473 @param MapKey Memory map key\r
28a00297 1474\r
022c6d45 1475 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction\r
1476 rules.\r
162ed594 1477 @retval EFI_SUCCESS Valid memory map.\r
28a00297 1478\r
162ed594 1479**/\r
1480EFI_STATUS\r
1481CoreTerminateMemoryMap (\r
1482 IN UINTN MapKey\r
1483 )\r
28a00297 1484{\r
1485 EFI_STATUS Status;\r
1486 LIST_ENTRY *Link;\r
1487 MEMORY_MAP *Entry;\r
1488\r
1489 Status = EFI_SUCCESS;\r
1490\r
1491 CoreAcquireMemoryLock ();\r
1492\r
1493 if (MapKey == mMemoryMapKey) {\r
1494\r
1495 //\r
1496 // Make sure the memory map is following all the construction rules\r
1497 // This is the last chance we will be able to display any messages on\r
1498 // the console devices.\r
1499 //\r
1500\r
1501 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1502 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
6e536468 1503 if ((Entry->Attribute & EFI_MEMORY_RUNTIME) != 0) {\r
28a00297 1504 if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS) {\r
d45fd260 1505 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));\r
1506 Status = EFI_INVALID_PARAMETER;\r
1507 goto Done;\r
28a00297 1508 }\r
6e536468 1509 if ((Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
d45fd260 1510 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
1511 Status = EFI_INVALID_PARAMETER;\r
1512 goto Done;\r
28a00297 1513 }\r
6e536468 1514 if (((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
d45fd260 1515 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
1516 Status = EFI_INVALID_PARAMETER;\r
1517 goto Done;\r
28a00297 1518 }\r
1519 }\r
1520 }\r
1521\r
1522 //\r
1523 // The map key they gave us matches what we expect. Fall through and\r
1524 // return success. In an ideal world we would clear out all of\r
1525 // EfiBootServicesCode and EfiBootServicesData. However this function\r
1526 // is not the last one called by ExitBootServices(), so we have to\r
1527 // preserve the memory contents.\r
1528 //\r
1529 } else {\r
1530 Status = EFI_INVALID_PARAMETER;\r
1531 }\r
1532\r
d45fd260 1533Done:\r
28a00297 1534 CoreReleaseMemoryLock ();\r
1535\r
1536 return Status;\r
1537}\r
1538\r
1539\r
1540\r
1541\r
1542\r
1543\r
1544\r
1545\r
162ed594 1546\r