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