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