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