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