]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Dxe/Mem/Page.c
Fix the bug that definition of EFI_DISPATCH_OPROM_TABLE does not follow Framework...
[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 - 2010, 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_STAISTICS;\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_STAISTICS 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 NumberOfPages Number of pages needed\r
866 @param NewType The type of memory the range is going to be\r
867 turned into\r
868 @param Alignment Bits to align with\r
869\r
870 @return The base address of the range, or 0 if the range was not found\r
871\r
872**/\r
873UINT64\r
874CoreFindFreePagesI (\r
875 IN UINT64 MaxAddress,\r
876 IN UINT64 MinAddress,\r
877 IN UINT64 NumberOfPages,\r
878 IN EFI_MEMORY_TYPE NewType,\r
879 IN UINTN Alignment\r
880 )\r
881{\r
882 UINT64 NumberOfBytes;\r
883 UINT64 Target;\r
884 UINT64 DescStart;\r
885 UINT64 DescEnd;\r
886 UINT64 DescNumberOfBytes;\r
887 LIST_ENTRY *Link;\r
888 MEMORY_MAP *Entry;\r
889\r
890 if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages == 0)) {\r
891 return 0;\r
892 }\r
893\r
894 if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {\r
895\r
896 //\r
897 // If MaxAddress is not aligned to the end of a page\r
898 //\r
899\r
900 //\r
901 // Change MaxAddress to be 1 page lower\r
902 //\r
903 MaxAddress -= (EFI_PAGE_MASK + 1);\r
904\r
905 //\r
906 // Set MaxAddress to a page boundary\r
907 //\r
908 MaxAddress &= ~EFI_PAGE_MASK;\r
909\r
910 //\r
911 // Set MaxAddress to end of the page\r
912 //\r
913 MaxAddress |= EFI_PAGE_MASK;\r
914 }\r
915\r
916 NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r
917 Target = 0;\r
918\r
919 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
920 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
921\r
922 //\r
923 // If it's not a free entry, don't bother with it\r
924 //\r
925 if (Entry->Type != EfiConventionalMemory) {\r
926 continue;\r
927 }\r
928\r
929 DescStart = Entry->Start;\r
930 DescEnd = Entry->End;\r
931\r
932 //\r
933 // If desc is past max allowed address or below min allowed address, skip it\r
934 //\r
935 if ((DescStart >= MaxAddress) || (DescEnd < MinAddress)) {\r
936 continue;\r
937 }\r
938\r
939 //\r
940 // If desc ends past max allowed address, clip the end\r
941 //\r
942 if (DescEnd >= MaxAddress) {\r
943 DescEnd = MaxAddress;\r
944 }\r
945\r
946 DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;\r
947\r
948 //\r
949 // Compute the number of bytes we can used from this\r
950 // descriptor, and see it's enough to satisfy the request\r
951 //\r
952 DescNumberOfBytes = DescEnd - DescStart + 1;\r
953\r
954 if (DescNumberOfBytes >= NumberOfBytes) {\r
955 //\r
956 // If the start of the allocated range is below the min address allowed, skip it\r
957 //\r
958 if ((DescEnd - NumberOfBytes + 1) < MinAddress) {\r
959 continue;\r
960 }\r
961\r
962 //\r
963 // If this is the best match so far remember it\r
964 //\r
965 if (DescEnd > Target) {\r
966 Target = DescEnd;\r
967 }\r
968 }\r
969 }\r
970\r
971 //\r
972 // If this is a grow down, adjust target to be the allocation base\r
973 //\r
974 Target -= NumberOfBytes - 1;\r
975\r
976 //\r
977 // If we didn't find a match, return 0\r
978 //\r
979 if ((Target & EFI_PAGE_MASK) != 0) {\r
980 return 0;\r
981 }\r
982\r
983 return Target;\r
984}\r
985\r
986\r
987/**\r
988 Internal function. Finds a consecutive free page range below\r
989 the requested address\r
990\r
991 @param MaxAddress The address that the range must be below\r
992 @param NoPages Number of pages needed\r
993 @param NewType The type of memory the range is going to be\r
994 turned into\r
995 @param Alignment Bits to align with\r
996\r
997 @return The base address of the range, or 0 if the range was not found.\r
998\r
999**/\r
1000UINT64\r
1001FindFreePages (\r
1002 IN UINT64 MaxAddress,\r
1003 IN UINT64 NoPages,\r
1004 IN EFI_MEMORY_TYPE NewType,\r
1005 IN UINTN Alignment\r
1006 )\r
1007{\r
1008 UINT64 Start;\r
1009\r
1010 //\r
1011 // Attempt to find free pages in the preferred bin based on the requested memory type\r
1012 //\r
1013 if (NewType >= 0 && NewType < EfiMaxMemoryType && MaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {\r
1014 Start = CoreFindFreePagesI (\r
1015 mMemoryTypeStatistics[NewType].MaximumAddress, \r
1016 mMemoryTypeStatistics[NewType].BaseAddress, \r
1017 NoPages, \r
1018 NewType, \r
1019 Alignment\r
1020 );\r
1021 if (Start != 0) {\r
1022 return Start;\r
1023 }\r
1024 }\r
1025\r
1026 //\r
1027 // Attempt to find free pages in the default allocation bin\r
1028 //\r
1029 if (MaxAddress >= mDefaultMaximumAddress) {\r
1030 Start = CoreFindFreePagesI (mDefaultMaximumAddress, 0, NoPages, NewType, Alignment);\r
1031 if (Start != 0) {\r
1032 if (Start < mDefaultBaseAddress) {\r
1033 mDefaultBaseAddress = Start;\r
1034 }\r
1035 return Start;\r
1036 }\r
1037 }\r
1038\r
1039 //\r
1040 // The allocation did not succeed in any of the prefered bins even after \r
1041 // promoting resources. Attempt to find free pages anywhere is the requested \r
1042 // address range. If this allocation fails, then there are not enough \r
1043 // resources anywhere to satisfy the request.\r
1044 //\r
1045 Start = CoreFindFreePagesI (MaxAddress, 0, NoPages, NewType, Alignment);\r
1046 if (Start != 0) {\r
1047 return Start;\r
1048 }\r
1049\r
1050 //\r
1051 // If allocations from the preferred bins fail, then attempt to promote memory resources.\r
1052 //\r
1053 if (!PromoteMemoryResource ()) {\r
1054 return 0;\r
1055 }\r
1056\r
1057 //\r
1058 // If any memory resources were promoted, then re-attempt the allocation\r
1059 //\r
1060 return FindFreePages (MaxAddress, NoPages, NewType, Alignment);\r
1061}\r
1062\r
1063\r
1064/**\r
1065 Allocates pages from the memory map.\r
1066\r
1067 @param Type The type of allocation to perform\r
1068 @param MemoryType The type of memory to turn the allocated pages\r
1069 into\r
1070 @param NumberOfPages The number of pages to allocate\r
1071 @param Memory A pointer to receive the base allocated memory\r
1072 address\r
1073\r
1074 @return Status. On success, Memory is filled in with the base address allocated\r
1075 @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in\r
1076 spec.\r
1077 @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r
1078 @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r
1079 @retval EFI_SUCCESS Pages successfully allocated.\r
1080\r
1081**/\r
1082EFI_STATUS\r
1083EFIAPI\r
1084CoreAllocatePages (\r
1085 IN EFI_ALLOCATE_TYPE Type,\r
1086 IN EFI_MEMORY_TYPE MemoryType,\r
1087 IN UINTN NumberOfPages,\r
1088 IN OUT EFI_PHYSICAL_ADDRESS *Memory\r
1089 )\r
1090{\r
1091 EFI_STATUS Status;\r
1092 UINT64 Start;\r
1093 UINT64 MaxAddress;\r
1094 UINTN Alignment;\r
1095\r
1096 if (Type < AllocateAnyPages || Type >= (UINTN) MaxAllocateType) {\r
1097 return EFI_INVALID_PARAMETER;\r
1098 }\r
1099\r
1100 if ((MemoryType >= EfiMaxMemoryType && MemoryType <= 0x7fffffff) ||\r
1101 MemoryType == EfiConventionalMemory) {\r
1102 return EFI_INVALID_PARAMETER;\r
1103 }\r
1104\r
1105 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
1106\r
1107 if (MemoryType == EfiACPIReclaimMemory ||\r
1108 MemoryType == EfiACPIMemoryNVS ||\r
1109 MemoryType == EfiRuntimeServicesCode ||\r
1110 MemoryType == EfiRuntimeServicesData) {\r
1111\r
1112 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
1113 }\r
1114\r
1115 if (Type == AllocateAddress) {\r
1116 if ((*Memory & (Alignment - 1)) != 0) {\r
1117 return EFI_NOT_FOUND;\r
1118 }\r
1119 }\r
1120\r
1121 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1122 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1123\r
1124 //\r
1125 // If this is for below a particular address, then\r
1126 //\r
1127 Start = *Memory;\r
1128\r
1129 //\r
1130 // The max address is the max natively addressable address for the processor\r
1131 //\r
1132 MaxAddress = MAX_ADDRESS;\r
1133\r
1134 if (Type == AllocateMaxAddress) {\r
1135 MaxAddress = Start;\r
1136 }\r
1137\r
1138 CoreAcquireMemoryLock ();\r
1139\r
1140 //\r
1141 // If not a specific address, then find an address to allocate\r
1142 //\r
1143 if (Type != AllocateAddress) {\r
1144 Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment);\r
1145 if (Start == 0) {\r
1146 Status = EFI_OUT_OF_RESOURCES;\r
1147 goto Done;\r
1148 }\r
1149 }\r
1150\r
1151 //\r
1152 // Convert pages from FreeMemory to the requested type\r
1153 //\r
1154 Status = CoreConvertPages (Start, NumberOfPages, MemoryType);\r
1155\r
1156Done:\r
1157 CoreReleaseMemoryLock ();\r
1158\r
1159 if (!EFI_ERROR (Status)) {\r
1160 *Memory = Start;\r
1161 }\r
1162\r
1163 return Status;\r
1164}\r
1165\r
1166\r
1167/**\r
1168 Frees previous allocated pages.\r
1169\r
1170 @param Memory Base address of memory being freed\r
1171 @param NumberOfPages The number of pages to free\r
1172\r
1173 @retval EFI_NOT_FOUND Could not find the entry that covers the range\r
1174 @retval EFI_INVALID_PARAMETER Address not aligned\r
1175 @return EFI_SUCCESS -Pages successfully freed.\r
1176\r
1177**/\r
1178EFI_STATUS\r
1179EFIAPI\r
1180CoreFreePages (\r
1181 IN EFI_PHYSICAL_ADDRESS Memory,\r
1182 IN UINTN NumberOfPages\r
1183 )\r
1184{\r
1185 EFI_STATUS Status;\r
1186 LIST_ENTRY *Link;\r
1187 MEMORY_MAP *Entry;\r
1188 UINTN Alignment;\r
1189\r
1190 //\r
1191 // Free the range\r
1192 //\r
1193 CoreAcquireMemoryLock ();\r
1194\r
1195 //\r
1196 // Find the entry that the covers the range\r
1197 //\r
1198 Entry = NULL;\r
1199 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1200 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1201 if (Entry->Start <= Memory && Entry->End > Memory) {\r
1202 break;\r
1203 }\r
1204 }\r
1205 if (Link == &gMemoryMap) {\r
1206 Status = EFI_NOT_FOUND;\r
1207 goto Done;\r
1208 }\r
1209\r
1210 Alignment = EFI_DEFAULT_PAGE_ALLOCATION_ALIGNMENT;\r
1211\r
1212 ASSERT (Entry != NULL);\r
1213 if (Entry->Type == EfiACPIReclaimMemory ||\r
1214 Entry->Type == EfiACPIMemoryNVS ||\r
1215 Entry->Type == EfiRuntimeServicesCode ||\r
1216 Entry->Type == EfiRuntimeServicesData) {\r
1217\r
1218 Alignment = EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT;\r
1219\r
1220 }\r
1221\r
1222 if ((Memory & (Alignment - 1)) != 0) {\r
1223 Status = EFI_INVALID_PARAMETER;\r
1224 goto Done;\r
1225 }\r
1226\r
1227 NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r
1228 NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r
1229\r
1230 Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
1231\r
1232 if (EFI_ERROR (Status)) {\r
1233 goto Done;\r
1234 }\r
1235\r
1236Done:\r
1237 CoreReleaseMemoryLock ();\r
1238 return Status;\r
1239}\r
1240\r
1241/**\r
1242 This function checks to see if the last memory map descriptor in a memory map\r
1243 can be merged with any of the other memory map descriptors in a memorymap.\r
1244 Memory descriptors may be merged if they are adjacent and have the same type\r
1245 and attributes.\r
1246\r
1247 @param MemoryMap A pointer to the start of the memory map.\r
1248 @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.\r
1249 @param DescriptorSize The size, in bytes, of an individual\r
1250 EFI_MEMORY_DESCRIPTOR.\r
1251\r
1252 @return A pointer to the next available descriptor in MemoryMap\r
1253\r
1254**/\r
1255EFI_MEMORY_DESCRIPTOR *\r
1256MergeMemoryMapDescriptor (\r
1257 IN EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
1258 IN EFI_MEMORY_DESCRIPTOR *MemoryMapDescriptor,\r
1259 IN UINTN DescriptorSize\r
1260 )\r
1261{\r
1262 //\r
1263 // Traverse the array of descriptors in MemoryMap\r
1264 //\r
1265 for (; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) {\r
1266 //\r
1267 // Check to see if the Type fields are identical.\r
1268 //\r
1269 if (MemoryMap->Type != MemoryMapDescriptor->Type) {\r
1270 continue;\r
1271 }\r
1272\r
1273 //\r
1274 // Check to see if the Attribute fields are identical.\r
1275 //\r
1276 if (MemoryMap->Attribute != MemoryMapDescriptor->Attribute) {\r
1277 continue;\r
1278 }\r
1279\r
1280 //\r
1281 // Check to see if MemoryMapDescriptor is immediately above MemoryMap\r
1282 //\r
1283 if (MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) { \r
1284 //\r
1285 // Merge MemoryMapDescriptor into MemoryMap\r
1286 //\r
1287 MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r
1288\r
1289 //\r
1290 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r
1291 //\r
1292 return MemoryMapDescriptor;\r
1293 }\r
1294\r
1295 //\r
1296 // Check to see if MemoryMapDescriptor is immediately below MemoryMap\r
1297 //\r
1298 if (MemoryMap->PhysicalStart - EFI_PAGES_TO_SIZE ((UINTN)MemoryMapDescriptor->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {\r
1299 //\r
1300 // Merge MemoryMapDescriptor into MemoryMap\r
1301 //\r
1302 MemoryMap->PhysicalStart = MemoryMapDescriptor->PhysicalStart;\r
1303 MemoryMap->VirtualStart = MemoryMapDescriptor->VirtualStart;\r
1304 MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r
1305\r
1306 //\r
1307 // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r
1308 //\r
1309 return MemoryMapDescriptor;\r
1310 }\r
1311 }\r
1312\r
1313 //\r
1314 // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.\r
1315 //\r
1316 // Return the slot immediately after MemoryMapDescriptor as the next available \r
1317 // slot in the MemoryMap array\r
1318 //\r
1319 return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor, DescriptorSize);\r
1320}\r
1321\r
1322/**\r
1323 This function returns a copy of the current memory map. The map is an array of\r
1324 memory descriptors, each of which describes a contiguous block of memory.\r
1325\r
1326 @param MemoryMapSize A pointer to the size, in bytes, of the\r
1327 MemoryMap buffer. On input, this is the size of\r
1328 the buffer allocated by the caller. On output,\r
1329 it is the size of the buffer returned by the\r
1330 firmware if the buffer was large enough, or the\r
1331 size of the buffer needed to contain the map if\r
1332 the buffer was too small.\r
1333 @param MemoryMap A pointer to the buffer in which firmware places\r
1334 the current memory map.\r
1335 @param MapKey A pointer to the location in which firmware\r
1336 returns the key for the current memory map.\r
1337 @param DescriptorSize A pointer to the location in which firmware\r
1338 returns the size, in bytes, of an individual\r
1339 EFI_MEMORY_DESCRIPTOR.\r
1340 @param DescriptorVersion A pointer to the location in which firmware\r
1341 returns the version number associated with the\r
1342 EFI_MEMORY_DESCRIPTOR.\r
1343\r
1344 @retval EFI_SUCCESS The memory map was returned in the MemoryMap\r
1345 buffer.\r
1346 @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current\r
1347 buffer size needed to hold the memory map is\r
1348 returned in MemoryMapSize.\r
1349 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
1350\r
1351**/\r
1352EFI_STATUS\r
1353EFIAPI\r
1354CoreGetMemoryMap (\r
1355 IN OUT UINTN *MemoryMapSize,\r
1356 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
1357 OUT UINTN *MapKey,\r
1358 OUT UINTN *DescriptorSize,\r
1359 OUT UINT32 *DescriptorVersion\r
1360 )\r
1361{\r
1362 EFI_STATUS Status;\r
1363 UINTN Size;\r
1364 UINTN BufferSize;\r
1365 UINTN NumberOfRuntimeEntries;\r
1366 LIST_ENTRY *Link;\r
1367 MEMORY_MAP *Entry;\r
1368 EFI_GCD_MAP_ENTRY *GcdMapEntry;\r
1369 EFI_MEMORY_TYPE Type;\r
1370 EFI_MEMORY_DESCRIPTOR *MemoryMapStart;\r
1371\r
1372 //\r
1373 // Make sure the parameters are valid\r
1374 //\r
1375 if (MemoryMapSize == NULL) {\r
1376 return EFI_INVALID_PARAMETER;\r
1377 }\r
1378\r
1379 CoreAcquireGcdMemoryLock ();\r
1380\r
1381 //\r
1382 // Count the number of Reserved and MMIO entries that are marked for runtime use\r
1383 //\r
1384 NumberOfRuntimeEntries = 0;\r
1385 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
1386 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
1387 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1388 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {\r
1389 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
1390 NumberOfRuntimeEntries++;\r
1391 }\r
1392 }\r
1393 }\r
1394\r
1395 Size = sizeof (EFI_MEMORY_DESCRIPTOR);\r
1396\r
1397 //\r
1398 // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will\r
1399 // prevent people from having pointer math bugs in their code.\r
1400 // now you have to use *DescriptorSize to make things work.\r
1401 //\r
1402 Size += sizeof(UINT64) - (Size % sizeof (UINT64));\r
1403\r
1404 if (DescriptorSize != NULL) {\r
1405 *DescriptorSize = Size;\r
1406 }\r
1407\r
1408 if (DescriptorVersion != NULL) {\r
1409 *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;\r
1410 }\r
1411\r
1412 CoreAcquireMemoryLock ();\r
1413\r
1414 //\r
1415 // Compute the buffer size needed to fit the entire map\r
1416 //\r
1417 BufferSize = Size * NumberOfRuntimeEntries;\r
1418 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1419 BufferSize += Size;\r
1420 }\r
1421\r
1422 if (*MemoryMapSize < BufferSize) {\r
1423 Status = EFI_BUFFER_TOO_SMALL;\r
1424 goto Done;\r
1425 }\r
1426\r
1427 if (MemoryMap == NULL) {\r
1428 Status = EFI_INVALID_PARAMETER;\r
1429 goto Done;\r
1430 }\r
1431\r
1432 //\r
1433 // Build the map\r
1434 //\r
1435 ZeroMem (MemoryMap, BufferSize);\r
1436 MemoryMapStart = MemoryMap;\r
1437 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1438 Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1439 ASSERT (Entry->VirtualStart == 0);\r
1440\r
1441 //\r
1442 // Convert internal map into an EFI_MEMORY_DESCRIPTOR\r
1443 //\r
1444 MemoryMap->Type = Entry->Type;\r
1445 MemoryMap->PhysicalStart = Entry->Start;\r
1446 MemoryMap->VirtualStart = Entry->VirtualStart;\r
1447 MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);\r
1448 //\r
1449 // If the memory type is EfiConventionalMemory, then determine if the range is part of a\r
1450 // memory type bin and needs to be converted to the same memory type as the rest of the\r
1451 // memory type bin in order to minimize EFI Memory Map changes across reboots. This\r
1452 // improves the chances for a successful S4 resume in the presence of minor page allocation\r
1453 // differences across reboots.\r
1454 //\r
1455 if (MemoryMap->Type == EfiConventionalMemory) {\r
1456 for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r
1457 if (mMemoryTypeStatistics[Type].Special &&\r
1458 mMemoryTypeStatistics[Type].NumberOfPages > 0 &&\r
1459 Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress &&\r
1460 Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) {\r
1461 MemoryMap->Type = Type;\r
1462 }\r
1463 }\r
1464 }\r
1465 MemoryMap->Attribute = Entry->Attribute;\r
1466 if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {\r
1467 MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;\r
1468 }\r
1469\r
1470 //\r
1471 // Check to see if the new Memory Map Descriptor can be merged with an \r
1472 // existing descriptor if they are adjacent and have the same attributes\r
1473 //\r
1474 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
1475 }\r
1476\r
1477 for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r
1478 GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r
1479 if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r
1480 (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo)) {\r
1481 if ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
1482 // \r
1483 // Create EFI_MEMORY_DESCRIPTOR for every Reserved and MMIO GCD entries\r
1484 // that are marked for runtime use\r
1485 //\r
1486 MemoryMap->PhysicalStart = GcdMapEntry->BaseAddress;\r
1487 MemoryMap->VirtualStart = 0;\r
1488 MemoryMap->NumberOfPages = RShiftU64 ((GcdMapEntry->EndAddress - GcdMapEntry->BaseAddress + 1), EFI_PAGE_SHIFT);\r
1489 MemoryMap->Attribute = GcdMapEntry->Attributes & ~EFI_MEMORY_PORT_IO;\r
1490\r
1491 if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) {\r
1492 MemoryMap->Type = EfiReservedMemoryType;\r
1493 } else if (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {\r
1494 if ((GcdMapEntry->Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {\r
1495 MemoryMap->Type = EfiMemoryMappedIOPortSpace;\r
1496 } else {\r
1497 MemoryMap->Type = EfiMemoryMappedIO;\r
1498 }\r
1499 }\r
1500\r
1501 //\r
1502 // Check to see if the new Memory Map Descriptor can be merged with an \r
1503 // existing descriptor if they are adjacent and have the same attributes\r
1504 //\r
1505 MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r
1506 }\r
1507 }\r
1508 }\r
1509\r
1510 //\r
1511 // Compute the size of the buffer actually used after all memory map descriptor merge operations\r
1512 //\r
1513 BufferSize = ((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart);\r
1514\r
1515 Status = EFI_SUCCESS;\r
1516\r
1517Done:\r
1518\r
1519 CoreReleaseMemoryLock ();\r
1520\r
1521 CoreReleaseGcdMemoryLock ();\r
1522\r
1523 //\r
1524 // Update the map key finally\r
1525 //\r
1526 if (MapKey != NULL) {\r
1527 *MapKey = mMemoryMapKey;\r
1528 }\r
1529\r
1530 *MemoryMapSize = BufferSize;\r
1531\r
1532 return Status;\r
1533}\r
1534\r
1535\r
1536/**\r
1537 Internal function. Used by the pool functions to allocate pages\r
1538 to back pool allocation requests.\r
1539\r
1540 @param PoolType The type of memory for the new pool pages\r
1541 @param NumberOfPages No of pages to allocate\r
1542 @param Alignment Bits to align.\r
1543\r
1544 @return The allocated memory, or NULL\r
1545\r
1546**/\r
1547VOID *\r
1548CoreAllocatePoolPages (\r
1549 IN EFI_MEMORY_TYPE PoolType,\r
1550 IN UINTN NumberOfPages,\r
1551 IN UINTN Alignment\r
1552 )\r
1553{\r
1554 UINT64 Start;\r
1555\r
1556 //\r
1557 // Find the pages to convert\r
1558 //\r
1559 Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment);\r
1560\r
1561 //\r
1562 // Convert it to boot services data\r
1563 //\r
1564 if (Start == 0) {\r
1565 DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32)NumberOfPages));\r
1566 } else {\r
1567 CoreConvertPages (Start, NumberOfPages, PoolType);\r
1568 }\r
1569\r
1570 return (VOID *)(UINTN) Start;\r
1571}\r
1572\r
1573\r
1574/**\r
1575 Internal function. Frees pool pages allocated via AllocatePoolPages ()\r
1576\r
1577 @param Memory The base address to free\r
1578 @param NumberOfPages The number of pages to free\r
1579\r
1580**/\r
1581VOID\r
1582CoreFreePoolPages (\r
1583 IN EFI_PHYSICAL_ADDRESS Memory,\r
1584 IN UINTN NumberOfPages\r
1585 )\r
1586{\r
1587 CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r
1588}\r
1589\r
1590\r
1591\r
1592/**\r
1593 Make sure the memory map is following all the construction rules,\r
1594 it is the last time to check memory map error before exit boot services.\r
1595\r
1596 @param MapKey Memory map key\r
1597\r
1598 @retval EFI_INVALID_PARAMETER Memory map not consistent with construction\r
1599 rules.\r
1600 @retval EFI_SUCCESS Valid memory map.\r
1601\r
1602**/\r
1603EFI_STATUS\r
1604CoreTerminateMemoryMap (\r
1605 IN UINTN MapKey\r
1606 )\r
1607{\r
1608 EFI_STATUS Status;\r
1609 LIST_ENTRY *Link;\r
1610 MEMORY_MAP *Entry;\r
1611\r
1612 Status = EFI_SUCCESS;\r
1613\r
1614 CoreAcquireMemoryLock ();\r
1615\r
1616 if (MapKey == mMemoryMapKey) {\r
1617\r
1618 //\r
1619 // Make sure the memory map is following all the construction rules\r
1620 // This is the last chance we will be able to display any messages on\r
1621 // the console devices.\r
1622 //\r
1623\r
1624 for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r
1625 Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r
1626 if ((Entry->Attribute & EFI_MEMORY_RUNTIME) != 0) {\r
1627 if (Entry->Type == EfiACPIReclaimMemory || Entry->Type == EfiACPIMemoryNVS) {\r
1628 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: ACPI memory entry has RUNTIME attribute set.\n"));\r
1629 Status = EFI_INVALID_PARAMETER;\r
1630 goto Done;\r
1631 }\r
1632 if ((Entry->Start & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
1633 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
1634 Status = EFI_INVALID_PARAMETER;\r
1635 goto Done;\r
1636 }\r
1637 if (((Entry->End + 1) & (EFI_ACPI_RUNTIME_PAGE_ALLOCATION_ALIGNMENT - 1)) != 0) {\r
1638 DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r
1639 Status = EFI_INVALID_PARAMETER;\r
1640 goto Done;\r
1641 }\r
1642 }\r
1643 }\r
1644\r
1645 //\r
1646 // The map key they gave us matches what we expect. Fall through and\r
1647 // return success. In an ideal world we would clear out all of\r
1648 // EfiBootServicesCode and EfiBootServicesData. However this function\r
1649 // is not the last one called by ExitBootServices(), so we have to\r
1650 // preserve the memory contents.\r
1651 //\r
1652 } else {\r
1653 Status = EFI_INVALID_PARAMETER;\r
1654 }\r
1655\r
1656Done:\r
1657 CoreReleaseMemoryLock ();\r
1658\r
1659 return Status;\r
1660}\r
1661\r
1662\r
1663\r
1664\r
1665\r
1666\r
1667\r
1668\r
1669\r