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