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