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