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