Commit | Line | Data |
---|---|---|
504214c4 | 1 | /** @file\r |
504214c4 LG |
2 | UEFI Memory page management functions.\r |
3 | \r | |
f0459afe | 4 | Copyright (c) 2007 - 2016, 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 |
28a00297 | 18 | //\r |
d45fd260 | 19 | // Entry for tracking the memory regions for each memory type to coalesce similar memory types\r |
28a00297 | 20 | //\r |
21 | typedef struct {\r | |
22 | EFI_PHYSICAL_ADDRESS BaseAddress;\r | |
23 | EFI_PHYSICAL_ADDRESS MaximumAddress;\r | |
24 | UINT64 CurrentNumberOfPages;\r | |
b74350e9 | 25 | UINT64 NumberOfPages;\r |
28a00297 | 26 | UINTN InformationIndex;\r |
b74350e9 | 27 | BOOLEAN Special;\r |
28 | BOOLEAN Runtime;\r | |
d613c2a8 | 29 | } EFI_MEMORY_TYPE_STATISTICS;\r |
28a00297 | 30 | \r |
31 | //\r | |
32 | // MemoryMap - The current memory map\r | |
33 | //\r | |
34 | UINTN mMemoryMapKey = 0;\r | |
35 | \r | |
28a00297 | 36 | #define MAX_MAP_DEPTH 6\r |
dc8d93ca | 37 | \r |
38 | ///\r | |
39 | /// mMapDepth - depth of new descriptor stack\r | |
40 | ///\r | |
28a00297 | 41 | UINTN mMapDepth = 0;\r |
dc8d93ca | 42 | ///\r |
43 | /// mMapStack - space to use as temp storage to build new map descriptors\r | |
44 | ///\r | |
28a00297 | 45 | MEMORY_MAP mMapStack[MAX_MAP_DEPTH];\r |
46 | UINTN mFreeMapStack = 0;\r | |
dc8d93ca | 47 | ///\r |
48 | /// This list maintain the free memory map list\r | |
49 | ///\r | |
e94a9ff7 | 50 | LIST_ENTRY mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList);\r |
51 | BOOLEAN mMemoryTypeInformationInitialized = FALSE;\r | |
28a00297 | 52 | \r |
d613c2a8 | 53 | EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {\r |
f3f2e05d | 54 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiReservedMemoryType\r |
55 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderCode\r | |
56 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiLoaderData\r | |
57 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesCode\r | |
58 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiBootServicesData\r | |
59 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesCode\r | |
60 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiRuntimeServicesData\r | |
61 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiConventionalMemory\r | |
62 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiUnusableMemory\r | |
63 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIReclaimMemory\r | |
64 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiACPIMemoryNVS\r | |
65 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIO\r | |
66 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIOPortSpace\r | |
67 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiPalCode\r | |
a671a012 | 68 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiPersistentMemory\r |
f3f2e05d | 69 | { 0, MAX_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE } // EfiMaxMemoryType\r |
28a00297 | 70 | };\r |
71 | \r | |
f3f2e05d | 72 | EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress = MAX_ADDRESS;\r |
2345e7d4 | 73 | EFI_PHYSICAL_ADDRESS mDefaultBaseAddress = MAX_ADDRESS;\r |
28a00297 | 74 | \r |
75 | EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {\r | |
76 | { EfiReservedMemoryType, 0 },\r | |
77 | { EfiLoaderCode, 0 },\r | |
78 | { EfiLoaderData, 0 },\r | |
79 | { EfiBootServicesCode, 0 },\r | |
80 | { EfiBootServicesData, 0 },\r | |
81 | { EfiRuntimeServicesCode, 0 },\r | |
82 | { EfiRuntimeServicesData, 0 },\r | |
83 | { EfiConventionalMemory, 0 },\r | |
84 | { EfiUnusableMemory, 0 },\r | |
85 | { EfiACPIReclaimMemory, 0 },\r | |
86 | { EfiACPIMemoryNVS, 0 },\r | |
87 | { EfiMemoryMappedIO, 0 },\r | |
88 | { EfiMemoryMappedIOPortSpace, 0 },\r | |
89 | { EfiPalCode, 0 },\r | |
a671a012 | 90 | { EfiPersistentMemory, 0 },\r |
28a00297 | 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 | |
6393d9c8 | 205 | // function the notificaiton events will only be called after this function\r |
d9177625 | 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 | |
d4731a98 AB |
288 | FreeDescriptorEntries = CoreAllocatePoolPages (EfiBootServicesData,\r |
289 | EFI_SIZE_TO_PAGES (DEFAULT_PAGE_ALLOCATION_GRANULARITY),\r | |
290 | DEFAULT_PAGE_ALLOCATION_GRANULARITY);\r | |
291 | if (FreeDescriptorEntries != NULL) {\r | |
d9177625 | 292 | //\r |
293 | // Enque the free memmory map entries into the list\r | |
294 | //\r | |
d4731a98 | 295 | for (Index = 0; Index < DEFAULT_PAGE_ALLOCATION_GRANULARITY / sizeof(MEMORY_MAP); Index++) {\r |
d9177625 | 296 | FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;\r |
297 | InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);\r | |
298 | }\r | |
299 | } else {\r | |
300 | return NULL;\r | |
301 | }\r | |
302 | }\r | |
303 | //\r | |
304 | // dequeue the first descriptor from the list\r | |
305 | //\r | |
306 | Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
307 | RemoveEntryList (&Entry->Link);\r | |
308 | \r | |
309 | return Entry;\r | |
310 | }\r | |
022c6d45 | 311 | \r |
162ed594 | 312 | \r |
313 | /**\r | |
d9177625 | 314 | Internal function. Moves any memory descriptors that are on the\r |
315 | temporary descriptor stack to heap.\r | |
162ed594 | 316 | \r |
317 | **/\r | |
28a00297 | 318 | VOID\r |
d9177625 | 319 | CoreFreeMemoryMapStack (\r |
28a00297 | 320 | VOID\r |
321 | )\r | |
28a00297 | 322 | {\r |
d9177625 | 323 | MEMORY_MAP *Entry;\r |
324 | MEMORY_MAP *Entry2;\r | |
325 | LIST_ENTRY *Link2;\r | |
28a00297 | 326 | \r |
d9177625 | 327 | ASSERT_LOCKED (&gMemoryLock);\r |
28a00297 | 328 | \r |
d9177625 | 329 | //\r |
330 | // If already freeing the map stack, then return\r | |
331 | //\r | |
332 | if (mFreeMapStack != 0) {\r | |
333 | return ;\r | |
334 | }\r | |
162ed594 | 335 | \r |
d9177625 | 336 | //\r |
337 | // Move the temporary memory descriptor stack into pool\r | |
338 | //\r | |
339 | mFreeMapStack += 1;\r | |
162ed594 | 340 | \r |
d9177625 | 341 | while (mMapDepth != 0) {\r |
342 | //\r | |
343 | // Deque an memory map entry from mFreeMemoryMapEntryList\r | |
344 | //\r | |
345 | Entry = AllocateMemoryMapEntry ();\r | |
346 | \r | |
347 | ASSERT (Entry);\r | |
348 | \r | |
349 | //\r | |
350 | // Update to proper entry\r | |
351 | //\r | |
352 | mMapDepth -= 1;\r | |
353 | \r | |
354 | if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {\r | |
355 | \r | |
356 | //\r | |
357 | // Move this entry to general memory\r | |
358 | //\r | |
359 | RemoveEntryList (&mMapStack[mMapDepth].Link);\r | |
360 | mMapStack[mMapDepth].Link.ForwardLink = NULL;\r | |
361 | \r | |
362 | CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));\r | |
363 | Entry->FromPages = TRUE;\r | |
364 | \r | |
365 | //\r | |
366 | // Find insertion location\r | |
367 | //\r | |
368 | for (Link2 = gMemoryMap.ForwardLink; Link2 != &gMemoryMap; Link2 = Link2->ForwardLink) {\r | |
369 | Entry2 = CR (Link2, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
370 | if (Entry2->FromPages && Entry2->Start > Entry->Start) {\r | |
371 | break;\r | |
372 | }\r | |
373 | }\r | |
374 | \r | |
375 | InsertTailList (Link2, &Entry->Link);\r | |
376 | \r | |
377 | } else {\r | |
378 | //\r | |
379 | // This item of mMapStack[mMapDepth] has already been dequeued from gMemoryMap list,\r | |
380 | // so here no need to move it to memory.\r | |
381 | //\r | |
382 | InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r | |
383 | }\r | |
384 | }\r | |
28a00297 | 385 | \r |
d9177625 | 386 | mFreeMapStack -= 1;\r |
387 | }\r | |
162ed594 | 388 | \r |
389 | /**\r | |
390 | Find untested but initialized memory regions in GCD map and convert them to be DXE allocatable.\r | |
391 | \r | |
392 | **/\r | |
2345e7d4 | 393 | BOOLEAN\r |
28a00297 | 394 | PromoteMemoryResource (\r |
395 | VOID\r | |
396 | )\r | |
28a00297 | 397 | {\r |
2345e7d4 | 398 | LIST_ENTRY *Link;\r |
399 | EFI_GCD_MAP_ENTRY *Entry;\r | |
400 | BOOLEAN Promoted;\r | |
28a00297 | 401 | \r |
d45fd260 | 402 | DEBUG ((DEBUG_PAGE, "Promote the memory resource\n"));\r |
022c6d45 | 403 | \r |
28a00297 | 404 | CoreAcquireGcdMemoryLock ();\r |
022c6d45 | 405 | \r |
2345e7d4 | 406 | Promoted = FALSE;\r |
28a00297 | 407 | Link = mGcdMemorySpaceMap.ForwardLink;\r |
408 | while (Link != &mGcdMemorySpaceMap) {\r | |
409 | \r | |
410 | Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r | |
411 | \r | |
412 | if (Entry->GcdMemoryType == EfiGcdMemoryTypeReserved &&\r | |
f3f2e05d | 413 | Entry->EndAddress < MAX_ADDRESS &&\r |
28a00297 | 414 | (Entry->Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==\r |
415 | (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)) {\r | |
416 | //\r | |
417 | // Update the GCD map\r | |
418 | //\r | |
74705ca5 SZ |
419 | if ((Entry->Capabilities & EFI_MEMORY_MORE_RELIABLE) == EFI_MEMORY_MORE_RELIABLE) {\r |
420 | Entry->GcdMemoryType = EfiGcdMemoryTypeMoreReliable;\r | |
421 | } else {\r | |
422 | Entry->GcdMemoryType = EfiGcdMemoryTypeSystemMemory;\r | |
423 | }\r | |
28a00297 | 424 | Entry->Capabilities |= EFI_MEMORY_TESTED;\r |
425 | Entry->ImageHandle = gDxeCoreImageHandle;\r | |
426 | Entry->DeviceHandle = NULL;\r | |
427 | \r | |
428 | //\r | |
429 | // Add to allocable system memory resource\r | |
022c6d45 | 430 | //\r |
28a00297 | 431 | \r |
432 | CoreAddRange (\r | |
022c6d45 | 433 | EfiConventionalMemory,\r |
434 | Entry->BaseAddress,\r | |
435 | Entry->EndAddress,\r | |
28a00297 | 436 | Entry->Capabilities & ~(EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED | EFI_MEMORY_RUNTIME)\r |
437 | );\r | |
438 | CoreFreeMemoryMapStack ();\r | |
022c6d45 | 439 | \r |
2345e7d4 | 440 | Promoted = TRUE;\r |
28a00297 | 441 | }\r |
442 | \r | |
443 | Link = Link->ForwardLink;\r | |
444 | }\r | |
022c6d45 | 445 | \r |
28a00297 | 446 | CoreReleaseGcdMemoryLock ();\r |
022c6d45 | 447 | \r |
2345e7d4 | 448 | return Promoted;\r |
28a00297 | 449 | }\r |
54ea99a7 | 450 | /**\r |
451 | This function try to allocate Runtime code & Boot time code memory range. If LMFA enabled, 2 patchable PCD \r | |
452 | PcdLoadFixAddressRuntimeCodePageNumber & PcdLoadFixAddressBootTimeCodePageNumber which are set by tools will record the \r | |
453 | size of boot time and runtime code.\r | |
28a00297 | 454 | \r |
54ea99a7 | 455 | **/\r |
456 | VOID\r | |
457 | CoreLoadingFixedAddressHook (\r | |
458 | VOID\r | |
459 | )\r | |
460 | {\r | |
461 | UINT32 RuntimeCodePageNumber;\r | |
462 | UINT32 BootTimeCodePageNumber;\r | |
463 | EFI_PHYSICAL_ADDRESS RuntimeCodeBase;\r | |
464 | EFI_PHYSICAL_ADDRESS BootTimeCodeBase;\r | |
465 | EFI_STATUS Status;\r | |
466 | \r | |
467 | //\r | |
468 | // Make sure these 2 areas are not initialzied.\r | |
469 | //\r | |
470 | if (!gLoadFixedAddressCodeMemoryReady) { \r | |
471 | RuntimeCodePageNumber = PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r | |
472 | BootTimeCodePageNumber= PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r | |
473 | RuntimeCodeBase = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressConfigurationTable.DxeCodeTopAddress - EFI_PAGES_TO_SIZE (RuntimeCodePageNumber));\r | |
474 | BootTimeCodeBase = (EFI_PHYSICAL_ADDRESS)(RuntimeCodeBase - EFI_PAGES_TO_SIZE (BootTimeCodePageNumber));\r | |
475 | //\r | |
476 | // Try to allocate runtime memory.\r | |
477 | //\r | |
478 | Status = CoreAllocatePages (\r | |
479 | AllocateAddress,\r | |
480 | EfiRuntimeServicesCode,\r | |
481 | RuntimeCodePageNumber,\r | |
482 | &RuntimeCodeBase\r | |
483 | );\r | |
484 | if (EFI_ERROR(Status)) {\r | |
485 | //\r | |
486 | // Runtime memory allocation failed \r | |
487 | //\r | |
488 | return;\r | |
489 | }\r | |
490 | //\r | |
491 | // Try to allocate boot memory.\r | |
492 | //\r | |
493 | Status = CoreAllocatePages (\r | |
494 | AllocateAddress,\r | |
495 | EfiBootServicesCode,\r | |
496 | BootTimeCodePageNumber,\r | |
497 | &BootTimeCodeBase\r | |
498 | );\r | |
499 | if (EFI_ERROR(Status)) {\r | |
500 | //\r | |
501 | // boot memory allocation failed. Free Runtime code range and will try the allocation again when \r | |
502 | // new memory range is installed.\r | |
503 | //\r | |
504 | CoreFreePages (\r | |
505 | RuntimeCodeBase,\r | |
506 | RuntimeCodePageNumber\r | |
507 | );\r | |
508 | return;\r | |
509 | }\r | |
510 | gLoadFixedAddressCodeMemoryReady = TRUE;\r | |
511 | } \r | |
512 | return;\r | |
513 | } \r | |
28a00297 | 514 | \r |
162ed594 | 515 | /**\r |
28a00297 | 516 | Called to initialize the memory map and add descriptors to\r |
517 | the current descriptor list.\r | |
28a00297 | 518 | The first descriptor that is added must be general usable\r |
519 | memory as the addition allocates heap.\r | |
520 | \r | |
022c6d45 | 521 | @param Type The type of memory to add\r |
522 | @param Start The starting address in the memory range Must be\r | |
523 | page aligned\r | |
524 | @param NumberOfPages The number of pages in the range\r | |
525 | @param Attribute Attributes of the memory to add\r | |
28a00297 | 526 | \r |
162ed594 | 527 | @return None. The range is added to the memory map\r |
28a00297 | 528 | \r |
162ed594 | 529 | **/\r |
530 | VOID\r | |
531 | CoreAddMemoryDescriptor (\r | |
532 | IN EFI_MEMORY_TYPE Type,\r | |
533 | IN EFI_PHYSICAL_ADDRESS Start,\r | |
534 | IN UINT64 NumberOfPages,\r | |
535 | IN UINT64 Attribute\r | |
536 | )\r | |
28a00297 | 537 | {\r |
538 | EFI_PHYSICAL_ADDRESS End;\r | |
539 | EFI_STATUS Status;\r | |
540 | UINTN Index;\r | |
541 | UINTN FreeIndex;\r | |
54ea99a7 | 542 | \r |
28a00297 | 543 | if ((Start & EFI_PAGE_MASK) != 0) {\r |
544 | return;\r | |
545 | }\r | |
546 | \r | |
8ee25f48 | 547 | if (Type >= EfiMaxMemoryType && Type < MEMORY_TYPE_OEM_RESERVED_MIN) {\r |
28a00297 | 548 | return;\r |
549 | }\r | |
28a00297 | 550 | CoreAcquireMemoryLock ();\r |
551 | End = Start + LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT) - 1;\r | |
552 | CoreAddRange (Type, Start, End, Attribute);\r | |
553 | CoreFreeMemoryMapStack ();\r | |
554 | CoreReleaseMemoryLock ();\r | |
555 | \r | |
7eb927db | 556 | ApplyMemoryProtectionPolicy (EfiMaxMemoryType, Type, Start,\r |
4879e130 | 557 | LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT));\r |
7eb927db | 558 | \r |
54ea99a7 | 559 | //\r |
560 | // If Loading Module At Fixed Address feature is enabled. try to allocate memory with Runtime code & Boot time code type\r | |
561 | //\r | |
852081fc | 562 | if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r |
54ea99a7 | 563 | CoreLoadingFixedAddressHook();\r |
564 | }\r | |
565 | \r | |
28a00297 | 566 | //\r |
567 | // Check to see if the statistics for the different memory types have already been established\r | |
568 | //\r | |
569 | if (mMemoryTypeInformationInitialized) {\r | |
570 | return;\r | |
571 | }\r | |
572 | \r | |
54ea99a7 | 573 | \r |
28a00297 | 574 | //\r |
575 | // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array\r | |
576 | //\r | |
577 | for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r | |
578 | //\r | |
579 | // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r | |
580 | //\r | |
581 | Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r | |
3d78c020 | 582 | if ((UINT32)Type > EfiMaxMemoryType) {\r |
28a00297 | 583 | continue;\r |
584 | }\r | |
28a00297 | 585 | if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r |
586 | //\r | |
587 | // Allocate pages for the current memory type from the top of available memory\r | |
588 | //\r | |
589 | Status = CoreAllocatePages (\r | |
590 | AllocateAnyPages,\r | |
591 | Type,\r | |
592 | gMemoryTypeInformation[Index].NumberOfPages,\r | |
593 | &mMemoryTypeStatistics[Type].BaseAddress\r | |
594 | );\r | |
595 | if (EFI_ERROR (Status)) {\r | |
596 | //\r | |
022c6d45 | 597 | // If an error occurs allocating the pages for the current memory type, then\r |
28a00297 | 598 | // free all the pages allocates for the previous memory types and return. This\r |
599 | // operation with be retied when/if more memory is added to the system\r | |
600 | //\r | |
601 | for (FreeIndex = 0; FreeIndex < Index; FreeIndex++) {\r | |
602 | //\r | |
603 | // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r | |
604 | //\r | |
605 | Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[FreeIndex].Type);\r | |
3d78c020 | 606 | if ((UINT32)Type > EfiMaxMemoryType) {\r |
28a00297 | 607 | continue;\r |
608 | }\r | |
609 | \r | |
610 | if (gMemoryTypeInformation[FreeIndex].NumberOfPages != 0) {\r | |
611 | CoreFreePages (\r | |
022c6d45 | 612 | mMemoryTypeStatistics[Type].BaseAddress,\r |
28a00297 | 613 | gMemoryTypeInformation[FreeIndex].NumberOfPages\r |
614 | );\r | |
615 | mMemoryTypeStatistics[Type].BaseAddress = 0;\r | |
f3f2e05d | 616 | mMemoryTypeStatistics[Type].MaximumAddress = MAX_ADDRESS;\r |
28a00297 | 617 | }\r |
618 | }\r | |
619 | return;\r | |
620 | }\r | |
621 | \r | |
622 | //\r | |
623 | // Compute the address at the top of the current statistics\r | |
624 | //\r | |
022c6d45 | 625 | mMemoryTypeStatistics[Type].MaximumAddress =\r |
626 | mMemoryTypeStatistics[Type].BaseAddress +\r | |
28a00297 | 627 | LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT) - 1;\r |
628 | \r | |
629 | //\r | |
022c6d45 | 630 | // If the current base address is the lowest address so far, then update the default\r |
28a00297 | 631 | // maximum address\r |
632 | //\r | |
633 | if (mMemoryTypeStatistics[Type].BaseAddress < mDefaultMaximumAddress) {\r | |
634 | mDefaultMaximumAddress = mMemoryTypeStatistics[Type].BaseAddress - 1;\r | |
635 | }\r | |
636 | }\r | |
637 | }\r | |
638 | \r | |
639 | //\r | |
640 | // There was enough system memory for all the the memory types were allocated. So,\r | |
641 | // those memory areas can be freed for future allocations, and all future memory\r | |
642 | // allocations can occur within their respective bins\r | |
643 | //\r | |
644 | for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r | |
645 | //\r | |
646 | // Make sure the memory type in the gMemoryTypeInformation[] array is valid\r | |
647 | //\r | |
648 | Type = (EFI_MEMORY_TYPE) (gMemoryTypeInformation[Index].Type);\r | |
3d78c020 | 649 | if ((UINT32)Type > EfiMaxMemoryType) {\r |
28a00297 | 650 | continue;\r |
651 | }\r | |
28a00297 | 652 | if (gMemoryTypeInformation[Index].NumberOfPages != 0) {\r |
653 | CoreFreePages (\r | |
022c6d45 | 654 | mMemoryTypeStatistics[Type].BaseAddress,\r |
28a00297 | 655 | gMemoryTypeInformation[Index].NumberOfPages\r |
656 | );\r | |
b74350e9 | 657 | mMemoryTypeStatistics[Type].NumberOfPages = gMemoryTypeInformation[Index].NumberOfPages;\r |
28a00297 | 658 | gMemoryTypeInformation[Index].NumberOfPages = 0;\r |
659 | }\r | |
660 | }\r | |
661 | \r | |
662 | //\r | |
663 | // If the number of pages reserved for a memory type is 0, then all allocations for that type\r | |
664 | // should be in the default range.\r | |
665 | //\r | |
666 | for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r | |
667 | for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {\r | |
668 | if (Type == (EFI_MEMORY_TYPE)gMemoryTypeInformation[Index].Type) {\r | |
669 | mMemoryTypeStatistics[Type].InformationIndex = Index;\r | |
670 | }\r | |
671 | }\r | |
672 | mMemoryTypeStatistics[Type].CurrentNumberOfPages = 0;\r | |
f3f2e05d | 673 | if (mMemoryTypeStatistics[Type].MaximumAddress == MAX_ADDRESS) {\r |
28a00297 | 674 | mMemoryTypeStatistics[Type].MaximumAddress = mDefaultMaximumAddress;\r |
675 | }\r | |
676 | }\r | |
677 | \r | |
678 | mMemoryTypeInformationInitialized = TRUE;\r | |
679 | }\r | |
680 | \r | |
681 | \r | |
162ed594 | 682 | /**\r |
771ee501 EC |
683 | Internal function. Converts a memory range to the specified type or attributes.\r |
684 | The range must exist in the memory map. Either ChangingType or\r | |
685 | ChangingAttributes must be set, but not both.\r | |
162ed594 | 686 | \r |
022c6d45 | 687 | @param Start The first address of the range Must be page\r |
688 | aligned\r | |
689 | @param NumberOfPages The number of pages to convert\r | |
771ee501 | 690 | @param ChangingType Boolean indicating that type value should be changed\r |
022c6d45 | 691 | @param NewType The new type for the memory range\r |
771ee501 EC |
692 | @param ChangingAttributes Boolean indicating that attributes value should be changed\r |
693 | @param NewAttributes The new attributes for the memory range\r | |
162ed594 | 694 | \r |
022c6d45 | 695 | @retval EFI_INVALID_PARAMETER Invalid parameter\r |
696 | @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r | |
697 | range or convertion not allowed.\r | |
698 | @retval EFI_SUCCESS Successfully converts the memory range to the\r | |
162ed594 | 699 | specified type.\r |
700 | \r | |
701 | **/\r | |
28a00297 | 702 | EFI_STATUS\r |
771ee501 | 703 | CoreConvertPagesEx (\r |
28a00297 | 704 | IN UINT64 Start,\r |
705 | IN UINT64 NumberOfPages,\r | |
771ee501 EC |
706 | IN BOOLEAN ChangingType,\r |
707 | IN EFI_MEMORY_TYPE NewType,\r | |
708 | IN BOOLEAN ChangingAttributes,\r | |
709 | IN UINT64 NewAttributes\r | |
28a00297 | 710 | )\r |
28a00297 | 711 | {\r |
712 | \r | |
713 | UINT64 NumberOfBytes;\r | |
714 | UINT64 End;\r | |
715 | UINT64 RangeEnd;\r | |
716 | UINT64 Attribute;\r | |
771ee501 | 717 | EFI_MEMORY_TYPE MemType;\r |
28a00297 | 718 | LIST_ENTRY *Link;\r |
719 | MEMORY_MAP *Entry;\r | |
720 | \r | |
721 | Entry = NULL;\r | |
722 | NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r | |
723 | End = Start + NumberOfBytes - 1;\r | |
724 | \r | |
725 | ASSERT (NumberOfPages);\r | |
726 | ASSERT ((Start & EFI_PAGE_MASK) == 0);\r | |
727 | ASSERT (End > Start) ;\r | |
728 | ASSERT_LOCKED (&gMemoryLock);\r | |
771ee501 | 729 | ASSERT ( (ChangingType == FALSE) || (ChangingAttributes == FALSE) );\r |
28a00297 | 730 | \r |
f0459afe | 731 | if (NumberOfPages == 0 || ((Start & EFI_PAGE_MASK) != 0) || (Start >= End)) {\r |
28a00297 | 732 | return EFI_INVALID_PARAMETER;\r |
733 | }\r | |
734 | \r | |
735 | //\r | |
736 | // Convert the entire range\r | |
737 | //\r | |
738 | \r | |
739 | while (Start < End) {\r | |
740 | \r | |
741 | //\r | |
742 | // Find the entry that the covers the range\r | |
743 | //\r | |
744 | for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r | |
745 | Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
746 | \r | |
747 | if (Entry->Start <= Start && Entry->End > Start) {\r | |
748 | break;\r | |
749 | }\r | |
750 | }\r | |
751 | \r | |
752 | if (Link == &gMemoryMap) {\r | |
162ed594 | 753 | DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: failed to find range %lx - %lx\n", Start, End));\r |
28a00297 | 754 | return EFI_NOT_FOUND;\r |
755 | }\r | |
756 | \r | |
757 | //\r | |
758 | // Convert range to the end, or to the end of the descriptor\r | |
759 | // if that's all we've got\r | |
760 | //\r | |
761 | RangeEnd = End;\r | |
525aded9 | 762 | \r |
763 | ASSERT (Entry != NULL);\r | |
28a00297 | 764 | if (Entry->End < End) {\r |
765 | RangeEnd = Entry->End;\r | |
766 | }\r | |
767 | \r | |
771ee501 EC |
768 | if (ChangingType) {\r |
769 | DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to type %d\n", Start, RangeEnd, NewType));\r | |
770 | }\r | |
771 | if (ChangingAttributes) {\r | |
772 | DEBUG ((DEBUG_PAGE, "ConvertRange: %lx-%lx to attr %lx\n", Start, RangeEnd, NewAttributes));\r | |
022c6d45 | 773 | }\r |
28a00297 | 774 | \r |
771ee501 EC |
775 | if (ChangingType) {\r |
776 | //\r | |
777 | // Debug code - verify conversion is allowed\r | |
778 | //\r | |
779 | if (!(NewType == EfiConventionalMemory ? 1 : 0) ^ (Entry->Type == EfiConventionalMemory ? 1 : 0)) {\r | |
780 | DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "ConvertPages: Incompatible memory types\n"));\r | |
781 | return EFI_NOT_FOUND;\r | |
782 | }\r | |
783 | \r | |
784 | //\r | |
785 | // Update counters for the number of pages allocated to each memory type\r | |
786 | //\r | |
787 | if ((UINT32)Entry->Type < EfiMaxMemoryType) {\r | |
788 | if ((Start >= mMemoryTypeStatistics[Entry->Type].BaseAddress && Start <= mMemoryTypeStatistics[Entry->Type].MaximumAddress) ||\r | |
789 | (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {\r | |
790 | if (NumberOfPages > mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages) {\r | |
791 | mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages = 0;\r | |
792 | } else {\r | |
793 | mMemoryTypeStatistics[Entry->Type].CurrentNumberOfPages -= NumberOfPages;\r | |
794 | }\r | |
28a00297 | 795 | }\r |
796 | }\r | |
28a00297 | 797 | \r |
771ee501 EC |
798 | if ((UINT32)NewType < EfiMaxMemoryType) {\r |
799 | if ((Start >= mMemoryTypeStatistics[NewType].BaseAddress && Start <= mMemoryTypeStatistics[NewType].MaximumAddress) ||\r | |
800 | (Start >= mDefaultBaseAddress && Start <= mDefaultMaximumAddress) ) {\r | |
801 | mMemoryTypeStatistics[NewType].CurrentNumberOfPages += NumberOfPages;\r | |
802 | if (mMemoryTypeStatistics[NewType].CurrentNumberOfPages > gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages) {\r | |
803 | gMemoryTypeInformation[mMemoryTypeStatistics[NewType].InformationIndex].NumberOfPages = (UINT32)mMemoryTypeStatistics[NewType].CurrentNumberOfPages;\r | |
804 | }\r | |
28a00297 | 805 | }\r |
806 | }\r | |
807 | }\r | |
808 | \r | |
809 | //\r | |
810 | // Pull range out of descriptor\r | |
811 | //\r | |
812 | if (Entry->Start == Start) {\r | |
022c6d45 | 813 | \r |
28a00297 | 814 | //\r |
815 | // Clip start\r | |
816 | //\r | |
817 | Entry->Start = RangeEnd + 1;\r | |
818 | \r | |
819 | } else if (Entry->End == RangeEnd) {\r | |
022c6d45 | 820 | \r |
28a00297 | 821 | //\r |
822 | // Clip end\r | |
823 | //\r | |
824 | Entry->End = Start - 1;\r | |
825 | \r | |
826 | } else {\r | |
827 | \r | |
828 | //\r | |
829 | // Pull it out of the center, clip current\r | |
830 | //\r | |
022c6d45 | 831 | \r |
28a00297 | 832 | //\r |
833 | // Add a new one\r | |
834 | //\r | |
835 | mMapStack[mMapDepth].Signature = MEMORY_MAP_SIGNATURE;\r | |
836 | mMapStack[mMapDepth].FromPages = FALSE;\r | |
837 | mMapStack[mMapDepth].Type = Entry->Type;\r | |
838 | mMapStack[mMapDepth].Start = RangeEnd+1;\r | |
839 | mMapStack[mMapDepth].End = Entry->End;\r | |
840 | \r | |
841 | //\r | |
842 | // Inherit Attribute from the Memory Descriptor that is being clipped\r | |
843 | //\r | |
844 | mMapStack[mMapDepth].Attribute = Entry->Attribute;\r | |
845 | \r | |
846 | Entry->End = Start - 1;\r | |
847 | ASSERT (Entry->Start < Entry->End);\r | |
848 | \r | |
849 | Entry = &mMapStack[mMapDepth];\r | |
850 | InsertTailList (&gMemoryMap, &Entry->Link);\r | |
851 | \r | |
852 | mMapDepth += 1;\r | |
853 | ASSERT (mMapDepth < MAX_MAP_DEPTH);\r | |
854 | }\r | |
855 | \r | |
856 | //\r | |
022c6d45 | 857 | // The new range inherits the same Attribute as the Entry\r |
771ee501 | 858 | // it is being cut out of unless attributes are being changed\r |
28a00297 | 859 | //\r |
771ee501 EC |
860 | if (ChangingType) {\r |
861 | Attribute = Entry->Attribute;\r | |
862 | MemType = NewType;\r | |
863 | } else {\r | |
864 | Attribute = NewAttributes;\r | |
865 | MemType = Entry->Type;\r | |
866 | }\r | |
28a00297 | 867 | \r |
868 | //\r | |
869 | // If the descriptor is empty, then remove it from the map\r | |
870 | //\r | |
871 | if (Entry->Start == Entry->End + 1) {\r | |
872 | RemoveMemoryMapEntry (Entry);\r | |
873 | Entry = NULL;\r | |
874 | }\r | |
022c6d45 | 875 | \r |
28a00297 | 876 | //\r |
877 | // Add our new range in\r | |
878 | //\r | |
771ee501 EC |
879 | CoreAddRange (MemType, Start, RangeEnd, Attribute);\r |
880 | if (ChangingType && (MemType == EfiConventionalMemory)) {\r | |
9a340872 | 881 | //\r |
882 | // Avoid calling DEBUG_CLEAR_MEMORY() for an address of 0 because this\r | |
883 | // macro will ASSERT() if address is 0. Instead, CoreAddRange() guarantees\r | |
884 | // that the page starting at address 0 is always filled with zeros.\r | |
885 | //\r | |
886 | if (Start == 0) {\r | |
887 | if (RangeEnd > EFI_PAGE_SIZE) {\r | |
888 | DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) EFI_PAGE_SIZE, (UINTN) (RangeEnd - EFI_PAGE_SIZE + 1));\r | |
889 | }\r | |
890 | } else {\r | |
891 | DEBUG_CLEAR_MEMORY ((VOID *)(UINTN) Start, (UINTN) (RangeEnd - Start + 1));\r | |
892 | }\r | |
38c7df98 | 893 | }\r |
28a00297 | 894 | \r |
895 | //\r | |
896 | // Move any map descriptor stack to general pool\r | |
897 | //\r | |
898 | CoreFreeMemoryMapStack ();\r | |
899 | \r | |
900 | //\r | |
901 | // Bump the starting address, and convert the next range\r | |
902 | //\r | |
903 | Start = RangeEnd + 1;\r | |
904 | }\r | |
905 | \r | |
906 | //\r | |
907 | // Converted the whole range, done\r | |
908 | //\r | |
909 | \r | |
910 | return EFI_SUCCESS;\r | |
911 | }\r | |
912 | \r | |
913 | \r | |
771ee501 EC |
914 | /**\r |
915 | Internal function. Converts a memory range to the specified type.\r | |
916 | The range must exist in the memory map.\r | |
917 | \r | |
918 | @param Start The first address of the range Must be page\r | |
919 | aligned\r | |
920 | @param NumberOfPages The number of pages to convert\r | |
921 | @param NewType The new type for the memory range\r | |
922 | \r | |
923 | @retval EFI_INVALID_PARAMETER Invalid parameter\r | |
924 | @retval EFI_NOT_FOUND Could not find a descriptor cover the specified\r | |
925 | range or convertion not allowed.\r | |
926 | @retval EFI_SUCCESS Successfully converts the memory range to the\r | |
927 | specified type.\r | |
928 | \r | |
929 | **/\r | |
930 | EFI_STATUS\r | |
931 | CoreConvertPages (\r | |
932 | IN UINT64 Start,\r | |
933 | IN UINT64 NumberOfPages,\r | |
934 | IN EFI_MEMORY_TYPE NewType\r | |
935 | )\r | |
936 | {\r | |
937 | return CoreConvertPagesEx(Start, NumberOfPages, TRUE, NewType, FALSE, 0);\r | |
938 | }\r | |
939 | \r | |
940 | \r | |
941 | /**\r | |
942 | Internal function. Converts a memory range to use new attributes.\r | |
943 | \r | |
944 | @param Start The first address of the range Must be page\r | |
945 | aligned\r | |
946 | @param NumberOfPages The number of pages to convert\r | |
947 | @param NewAttributes The new attributes value for the range.\r | |
948 | \r | |
771ee501 EC |
949 | **/\r |
950 | VOID\r | |
951 | CoreUpdateMemoryAttributes (\r | |
952 | IN EFI_PHYSICAL_ADDRESS Start,\r | |
953 | IN UINT64 NumberOfPages,\r | |
954 | IN UINT64 NewAttributes\r | |
955 | )\r | |
956 | {\r | |
957 | CoreAcquireMemoryLock ();\r | |
958 | \r | |
959 | //\r | |
960 | // Update the attributes to the new value\r | |
961 | //\r | |
962 | CoreConvertPagesEx(Start, NumberOfPages, FALSE, (EFI_MEMORY_TYPE)0, TRUE, NewAttributes);\r | |
963 | \r | |
964 | CoreReleaseMemoryLock ();\r | |
965 | }\r | |
966 | \r | |
162ed594 | 967 | \r |
968 | /**\r | |
969 | Internal function. Finds a consecutive free page range below\r | |
970 | the requested address.\r | |
971 | \r | |
022c6d45 | 972 | @param MaxAddress The address that the range must be below\r |
35f9e94e | 973 | @param MinAddress The address that the range must be above\r |
022c6d45 | 974 | @param NumberOfPages Number of pages needed\r |
975 | @param NewType The type of memory the range is going to be\r | |
976 | turned into\r | |
977 | @param Alignment Bits to align with\r | |
162ed594 | 978 | \r |
979 | @return The base address of the range, or 0 if the range was not found\r | |
980 | \r | |
981 | **/\r | |
28a00297 | 982 | UINT64\r |
983 | CoreFindFreePagesI (\r | |
984 | IN UINT64 MaxAddress,\r | |
2345e7d4 | 985 | IN UINT64 MinAddress,\r |
28a00297 | 986 | IN UINT64 NumberOfPages,\r |
987 | IN EFI_MEMORY_TYPE NewType,\r | |
988 | IN UINTN Alignment\r | |
989 | )\r | |
28a00297 | 990 | {\r |
991 | UINT64 NumberOfBytes;\r | |
992 | UINT64 Target;\r | |
993 | UINT64 DescStart;\r | |
994 | UINT64 DescEnd;\r | |
995 | UINT64 DescNumberOfBytes;\r | |
996 | LIST_ENTRY *Link;\r | |
997 | MEMORY_MAP *Entry;\r | |
998 | \r | |
999 | if ((MaxAddress < EFI_PAGE_MASK) ||(NumberOfPages == 0)) {\r | |
1000 | return 0;\r | |
1001 | }\r | |
1002 | \r | |
1003 | if ((MaxAddress & EFI_PAGE_MASK) != EFI_PAGE_MASK) {\r | |
022c6d45 | 1004 | \r |
28a00297 | 1005 | //\r |
1006 | // If MaxAddress is not aligned to the end of a page\r | |
1007 | //\r | |
022c6d45 | 1008 | \r |
28a00297 | 1009 | //\r |
1010 | // Change MaxAddress to be 1 page lower\r | |
1011 | //\r | |
1012 | MaxAddress -= (EFI_PAGE_MASK + 1);\r | |
022c6d45 | 1013 | \r |
28a00297 | 1014 | //\r |
1015 | // Set MaxAddress to a page boundary\r | |
1016 | //\r | |
6e1e5405 | 1017 | MaxAddress &= ~(UINT64)EFI_PAGE_MASK;\r |
022c6d45 | 1018 | \r |
28a00297 | 1019 | //\r |
1020 | // Set MaxAddress to end of the page\r | |
1021 | //\r | |
1022 | MaxAddress |= EFI_PAGE_MASK;\r | |
1023 | }\r | |
1024 | \r | |
1025 | NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r | |
1026 | Target = 0;\r | |
1027 | \r | |
1028 | for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r | |
1029 | Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
022c6d45 | 1030 | \r |
28a00297 | 1031 | //\r |
1032 | // If it's not a free entry, don't bother with it\r | |
1033 | //\r | |
1034 | if (Entry->Type != EfiConventionalMemory) {\r | |
1035 | continue;\r | |
1036 | }\r | |
1037 | \r | |
1038 | DescStart = Entry->Start;\r | |
1039 | DescEnd = Entry->End;\r | |
1040 | \r | |
1041 | //\r | |
2345e7d4 | 1042 | // If desc is past max allowed address or below min allowed address, skip it\r |
28a00297 | 1043 | //\r |
2345e7d4 | 1044 | if ((DescStart >= MaxAddress) || (DescEnd < MinAddress)) {\r |
28a00297 | 1045 | continue;\r |
1046 | }\r | |
1047 | \r | |
1048 | //\r | |
1049 | // If desc ends past max allowed address, clip the end\r | |
1050 | //\r | |
1051 | if (DescEnd >= MaxAddress) {\r | |
1052 | DescEnd = MaxAddress;\r | |
1053 | }\r | |
1054 | \r | |
1055 | DescEnd = ((DescEnd + 1) & (~(Alignment - 1))) - 1;\r | |
1056 | \r | |
1365bedd HG |
1057 | // Skip if DescEnd is less than DescStart after alignment clipping\r |
1058 | if (DescEnd < DescStart) {\r | |
1059 | continue;\r | |
1060 | }\r | |
1061 | \r | |
28a00297 | 1062 | //\r |
022c6d45 | 1063 | // Compute the number of bytes we can used from this\r |
28a00297 | 1064 | // descriptor, and see it's enough to satisfy the request\r |
1065 | //\r | |
1066 | DescNumberOfBytes = DescEnd - DescStart + 1;\r | |
1067 | \r | |
1068 | if (DescNumberOfBytes >= NumberOfBytes) {\r | |
2345e7d4 | 1069 | //\r |
1070 | // If the start of the allocated range is below the min address allowed, skip it\r | |
1071 | //\r | |
1072 | if ((DescEnd - NumberOfBytes + 1) < MinAddress) {\r | |
1073 | continue;\r | |
1074 | }\r | |
28a00297 | 1075 | \r |
1076 | //\r | |
1077 | // If this is the best match so far remember it\r | |
1078 | //\r | |
1079 | if (DescEnd > Target) {\r | |
1080 | Target = DescEnd;\r | |
1081 | }\r | |
1082 | }\r | |
022c6d45 | 1083 | }\r |
28a00297 | 1084 | \r |
1085 | //\r | |
1086 | // If this is a grow down, adjust target to be the allocation base\r | |
1087 | //\r | |
1088 | Target -= NumberOfBytes - 1;\r | |
1089 | \r | |
1090 | //\r | |
1091 | // If we didn't find a match, return 0\r | |
1092 | //\r | |
1093 | if ((Target & EFI_PAGE_MASK) != 0) {\r | |
1094 | return 0;\r | |
1095 | }\r | |
1096 | \r | |
1097 | return Target;\r | |
1098 | }\r | |
1099 | \r | |
162ed594 | 1100 | \r |
1101 | /**\r | |
1102 | Internal function. Finds a consecutive free page range below\r | |
1103 | the requested address\r | |
1104 | \r | |
022c6d45 | 1105 | @param MaxAddress The address that the range must be below\r |
1106 | @param NoPages Number of pages needed\r | |
1107 | @param NewType The type of memory the range is going to be\r | |
1108 | turned into\r | |
1109 | @param Alignment Bits to align with\r | |
162ed594 | 1110 | \r |
1111 | @return The base address of the range, or 0 if the range was not found.\r | |
1112 | \r | |
1113 | **/\r | |
28a00297 | 1114 | UINT64\r |
1115 | FindFreePages (\r | |
1116 | IN UINT64 MaxAddress,\r | |
1117 | IN UINT64 NoPages,\r | |
1118 | IN EFI_MEMORY_TYPE NewType,\r | |
1119 | IN UINTN Alignment\r | |
1120 | )\r | |
28a00297 | 1121 | {\r |
2345e7d4 | 1122 | UINT64 Start;\r |
28a00297 | 1123 | \r |
2345e7d4 | 1124 | //\r |
1125 | // Attempt to find free pages in the preferred bin based on the requested memory type\r | |
1126 | //\r | |
3d78c020 | 1127 | if ((UINT32)NewType < EfiMaxMemoryType && MaxAddress >= mMemoryTypeStatistics[NewType].MaximumAddress) {\r |
2345e7d4 | 1128 | Start = CoreFindFreePagesI (\r |
1129 | mMemoryTypeStatistics[NewType].MaximumAddress, \r | |
1130 | mMemoryTypeStatistics[NewType].BaseAddress, \r | |
1131 | NoPages, \r | |
1132 | NewType, \r | |
1133 | Alignment\r | |
1134 | );\r | |
1135 | if (Start != 0) {\r | |
1136 | return Start;\r | |
1137 | }\r | |
1138 | }\r | |
28a00297 | 1139 | \r |
2345e7d4 | 1140 | //\r |
1141 | // Attempt to find free pages in the default allocation bin\r | |
1142 | //\r | |
1143 | if (MaxAddress >= mDefaultMaximumAddress) {\r | |
1144 | Start = CoreFindFreePagesI (mDefaultMaximumAddress, 0, NoPages, NewType, Alignment);\r | |
1145 | if (Start != 0) {\r | |
1146 | if (Start < mDefaultBaseAddress) {\r | |
1147 | mDefaultBaseAddress = Start;\r | |
1148 | }\r | |
1149 | return Start;\r | |
28a00297 | 1150 | }\r |
1151 | }\r | |
1152 | \r | |
2345e7d4 | 1153 | //\r |
1154 | // The allocation did not succeed in any of the prefered bins even after \r | |
1155 | // promoting resources. Attempt to find free pages anywhere is the requested \r | |
1156 | // address range. If this allocation fails, then there are not enough \r | |
1157 | // resources anywhere to satisfy the request.\r | |
1158 | //\r | |
1159 | Start = CoreFindFreePagesI (MaxAddress, 0, NoPages, NewType, Alignment);\r | |
1160 | if (Start != 0) {\r | |
1161 | return Start;\r | |
1162 | }\r | |
28a00297 | 1163 | \r |
2345e7d4 | 1164 | //\r |
1165 | // If allocations from the preferred bins fail, then attempt to promote memory resources.\r | |
1166 | //\r | |
1167 | if (!PromoteMemoryResource ()) {\r | |
1168 | return 0;\r | |
28a00297 | 1169 | }\r |
1170 | \r | |
2345e7d4 | 1171 | //\r |
1172 | // If any memory resources were promoted, then re-attempt the allocation\r | |
1173 | //\r | |
1174 | return FindFreePages (MaxAddress, NoPages, NewType, Alignment);\r | |
28a00297 | 1175 | }\r |
1176 | \r | |
1177 | \r | |
162ed594 | 1178 | /**\r |
1179 | Allocates pages from the memory map.\r | |
1180 | \r | |
022c6d45 | 1181 | @param Type The type of allocation to perform\r |
1182 | @param MemoryType The type of memory to turn the allocated pages\r | |
1183 | into\r | |
1184 | @param NumberOfPages The number of pages to allocate\r | |
1185 | @param Memory A pointer to receive the base allocated memory\r | |
1186 | address\r | |
162ed594 | 1187 | \r |
1188 | @return Status. On success, Memory is filled in with the base address allocated\r | |
022c6d45 | 1189 | @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in\r |
1190 | spec.\r | |
1191 | @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r | |
1192 | @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r | |
162ed594 | 1193 | @retval EFI_SUCCESS Pages successfully allocated.\r |
1194 | \r | |
1195 | **/\r | |
28a00297 | 1196 | EFI_STATUS\r |
1197 | EFIAPI\r | |
84edd20b | 1198 | CoreInternalAllocatePages (\r |
28a00297 | 1199 | IN EFI_ALLOCATE_TYPE Type,\r |
1200 | IN EFI_MEMORY_TYPE MemoryType,\r | |
1201 | IN UINTN NumberOfPages,\r | |
1202 | IN OUT EFI_PHYSICAL_ADDRESS *Memory\r | |
1203 | )\r | |
28a00297 | 1204 | {\r |
1205 | EFI_STATUS Status;\r | |
1206 | UINT64 Start;\r | |
c2a07a10 SZ |
1207 | UINT64 NumberOfBytes;\r |
1208 | UINT64 End;\r | |
28a00297 | 1209 | UINT64 MaxAddress;\r |
1210 | UINTN Alignment;\r | |
1211 | \r | |
3d78c020 | 1212 | if ((UINT32)Type >= MaxAllocateType) {\r |
28a00297 | 1213 | return EFI_INVALID_PARAMETER;\r |
1214 | }\r | |
1215 | \r | |
8ee25f48 | 1216 | if ((MemoryType >= EfiMaxMemoryType && MemoryType < MEMORY_TYPE_OEM_RESERVED_MIN) ||\r |
a671a012 | 1217 | (MemoryType == EfiConventionalMemory) || (MemoryType == EfiPersistentMemory)) {\r |
28a00297 | 1218 | return EFI_INVALID_PARAMETER;\r |
1219 | }\r | |
1220 | \r | |
3e058701 ED |
1221 | if (Memory == NULL) {\r |
1222 | return EFI_INVALID_PARAMETER;\r | |
1223 | }\r | |
1224 | \r | |
d4731a98 | 1225 | Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r |
28a00297 | 1226 | \r |
1227 | if (MemoryType == EfiACPIReclaimMemory ||\r | |
1228 | MemoryType == EfiACPIMemoryNVS ||\r | |
1229 | MemoryType == EfiRuntimeServicesCode ||\r | |
1230 | MemoryType == EfiRuntimeServicesData) {\r | |
1231 | \r | |
d4731a98 | 1232 | Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r |
28a00297 | 1233 | }\r |
1234 | \r | |
1235 | if (Type == AllocateAddress) {\r | |
1236 | if ((*Memory & (Alignment - 1)) != 0) {\r | |
1237 | return EFI_NOT_FOUND;\r | |
1238 | }\r | |
1239 | }\r | |
1240 | \r | |
1241 | NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r | |
1242 | NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r | |
1243 | \r | |
1244 | //\r | |
022c6d45 | 1245 | // If this is for below a particular address, then\r |
28a00297 | 1246 | //\r |
1247 | Start = *Memory;\r | |
022c6d45 | 1248 | \r |
28a00297 | 1249 | //\r |
1250 | // The max address is the max natively addressable address for the processor\r | |
1251 | //\r | |
f3f2e05d | 1252 | MaxAddress = MAX_ADDRESS;\r |
022c6d45 | 1253 | \r |
c2a07a10 SZ |
1254 | //\r |
1255 | // Check for Type AllocateAddress,\r | |
1256 | // if NumberOfPages is 0 or\r | |
1257 | // if (NumberOfPages << EFI_PAGE_SHIFT) is above MAX_ADDRESS or\r | |
1258 | // if (Start + NumberOfBytes) rolls over 0 or\r | |
1259 | // if Start is above MAX_ADDRESS or\r | |
1260 | // if End is above MAX_ADDRESS,\r | |
1261 | // return EFI_NOT_FOUND.\r | |
1262 | //\r | |
1263 | if (Type == AllocateAddress) {\r | |
1264 | if ((NumberOfPages == 0) ||\r | |
1265 | (NumberOfPages > RShiftU64 (MaxAddress, EFI_PAGE_SHIFT))) {\r | |
1266 | return EFI_NOT_FOUND;\r | |
1267 | }\r | |
1268 | NumberOfBytes = LShiftU64 (NumberOfPages, EFI_PAGE_SHIFT);\r | |
1269 | End = Start + NumberOfBytes - 1;\r | |
1270 | \r | |
1271 | if ((Start >= End) ||\r | |
1272 | (Start > MaxAddress) || \r | |
1273 | (End > MaxAddress)) {\r | |
1274 | return EFI_NOT_FOUND;\r | |
1275 | }\r | |
1276 | }\r | |
1277 | \r | |
28a00297 | 1278 | if (Type == AllocateMaxAddress) {\r |
1279 | MaxAddress = Start;\r | |
1280 | }\r | |
1281 | \r | |
1282 | CoreAcquireMemoryLock ();\r | |
022c6d45 | 1283 | \r |
28a00297 | 1284 | //\r |
1285 | // If not a specific address, then find an address to allocate\r | |
1286 | //\r | |
1287 | if (Type != AllocateAddress) {\r | |
1288 | Start = FindFreePages (MaxAddress, NumberOfPages, MemoryType, Alignment);\r | |
1289 | if (Start == 0) {\r | |
1290 | Status = EFI_OUT_OF_RESOURCES;\r | |
1291 | goto Done;\r | |
1292 | }\r | |
1293 | }\r | |
1294 | \r | |
1295 | //\r | |
1296 | // Convert pages from FreeMemory to the requested type\r | |
1297 | //\r | |
1298 | Status = CoreConvertPages (Start, NumberOfPages, MemoryType);\r | |
1299 | \r | |
1300 | Done:\r | |
1301 | CoreReleaseMemoryLock ();\r | |
1302 | \r | |
1303 | if (!EFI_ERROR (Status)) {\r | |
1304 | *Memory = Start;\r | |
1305 | }\r | |
1306 | \r | |
1307 | return Status;\r | |
1308 | }\r | |
1309 | \r | |
84edd20b SZ |
1310 | /**\r |
1311 | Allocates pages from the memory map.\r | |
1312 | \r | |
1313 | @param Type The type of allocation to perform\r | |
1314 | @param MemoryType The type of memory to turn the allocated pages\r | |
1315 | into\r | |
1316 | @param NumberOfPages The number of pages to allocate\r | |
1317 | @param Memory A pointer to receive the base allocated memory\r | |
1318 | address\r | |
1319 | \r | |
1320 | @return Status. On success, Memory is filled in with the base address allocated\r | |
1321 | @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in\r | |
1322 | spec.\r | |
1323 | @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r | |
1324 | @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r | |
1325 | @retval EFI_SUCCESS Pages successfully allocated.\r | |
1326 | \r | |
1327 | **/\r | |
1328 | EFI_STATUS\r | |
1329 | EFIAPI\r | |
1330 | CoreAllocatePages (\r | |
1331 | IN EFI_ALLOCATE_TYPE Type,\r | |
1332 | IN EFI_MEMORY_TYPE MemoryType,\r | |
1333 | IN UINTN NumberOfPages,\r | |
1334 | OUT EFI_PHYSICAL_ADDRESS *Memory\r | |
1335 | )\r | |
1336 | {\r | |
1337 | EFI_STATUS Status;\r | |
1338 | \r | |
1339 | Status = CoreInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory);\r | |
1340 | if (!EFI_ERROR (Status)) {\r | |
1d60fe96 SZ |
1341 | CoreUpdateProfile (\r |
1342 | (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r | |
1343 | MemoryProfileActionAllocatePages,\r | |
1344 | MemoryType,\r | |
1345 | EFI_PAGES_TO_SIZE (NumberOfPages),\r | |
1346 | (VOID *) (UINTN) *Memory,\r | |
1347 | NULL\r | |
1348 | );\r | |
74a88770 | 1349 | InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);\r |
7eb927db AB |
1350 | ApplyMemoryProtectionPolicy (EfiConventionalMemory, MemoryType, *Memory,\r |
1351 | EFI_PAGES_TO_SIZE (NumberOfPages));\r | |
84edd20b SZ |
1352 | }\r |
1353 | return Status;\r | |
1354 | }\r | |
28a00297 | 1355 | \r |
162ed594 | 1356 | /**\r |
1357 | Frees previous allocated pages.\r | |
1358 | \r | |
022c6d45 | 1359 | @param Memory Base address of memory being freed\r |
1360 | @param NumberOfPages The number of pages to free\r | |
925f0d1a | 1361 | @param MemoryType Pointer to memory type\r |
162ed594 | 1362 | \r |
022c6d45 | 1363 | @retval EFI_NOT_FOUND Could not find the entry that covers the range\r |
1364 | @retval EFI_INVALID_PARAMETER Address not aligned\r | |
162ed594 | 1365 | @return EFI_SUCCESS -Pages successfully freed.\r |
1366 | \r | |
1367 | **/\r | |
022c6d45 | 1368 | EFI_STATUS\r |
28a00297 | 1369 | EFIAPI\r |
84edd20b | 1370 | CoreInternalFreePages (\r |
28a00297 | 1371 | IN EFI_PHYSICAL_ADDRESS Memory,\r |
925f0d1a SZ |
1372 | IN UINTN NumberOfPages,\r |
1373 | OUT EFI_MEMORY_TYPE *MemoryType OPTIONAL\r | |
28a00297 | 1374 | )\r |
28a00297 | 1375 | {\r |
1376 | EFI_STATUS Status;\r | |
1377 | LIST_ENTRY *Link;\r | |
1378 | MEMORY_MAP *Entry;\r | |
1379 | UINTN Alignment;\r | |
1380 | \r | |
1381 | //\r | |
1382 | // Free the range\r | |
1383 | //\r | |
1384 | CoreAcquireMemoryLock ();\r | |
1385 | \r | |
1386 | //\r | |
1387 | // Find the entry that the covers the range\r | |
1388 | //\r | |
1389 | Entry = NULL;\r | |
1390 | for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r | |
1391 | Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
1392 | if (Entry->Start <= Memory && Entry->End > Memory) {\r | |
1393 | break;\r | |
1394 | }\r | |
1395 | }\r | |
1396 | if (Link == &gMemoryMap) {\r | |
a5ca8fa7 | 1397 | Status = EFI_NOT_FOUND;\r |
1398 | goto Done;\r | |
28a00297 | 1399 | }\r |
1400 | \r | |
d4731a98 | 1401 | Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;\r |
28a00297 | 1402 | \r |
525aded9 | 1403 | ASSERT (Entry != NULL);\r |
28a00297 | 1404 | if (Entry->Type == EfiACPIReclaimMemory ||\r |
1405 | Entry->Type == EfiACPIMemoryNVS ||\r | |
1406 | Entry->Type == EfiRuntimeServicesCode ||\r | |
1407 | Entry->Type == EfiRuntimeServicesData) {\r | |
1408 | \r | |
d4731a98 | 1409 | Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;\r |
28a00297 | 1410 | \r |
1411 | }\r | |
1412 | \r | |
1413 | if ((Memory & (Alignment - 1)) != 0) {\r | |
a5ca8fa7 | 1414 | Status = EFI_INVALID_PARAMETER;\r |
1415 | goto Done;\r | |
28a00297 | 1416 | }\r |
1417 | \r | |
1418 | NumberOfPages += EFI_SIZE_TO_PAGES (Alignment) - 1;\r | |
1419 | NumberOfPages &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);\r | |
1420 | \r | |
925f0d1a SZ |
1421 | if (MemoryType != NULL) {\r |
1422 | *MemoryType = Entry->Type;\r | |
1423 | }\r | |
1424 | \r | |
28a00297 | 1425 | Status = CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r |
1426 | \r | |
28a00297 | 1427 | if (EFI_ERROR (Status)) {\r |
a5ca8fa7 | 1428 | goto Done;\r |
28a00297 | 1429 | }\r |
1430 | \r | |
a5ca8fa7 | 1431 | Done:\r |
1432 | CoreReleaseMemoryLock ();\r | |
28a00297 | 1433 | return Status;\r |
1434 | }\r | |
1435 | \r | |
84edd20b SZ |
1436 | /**\r |
1437 | Frees previous allocated pages.\r | |
1438 | \r | |
1439 | @param Memory Base address of memory being freed\r | |
1440 | @param NumberOfPages The number of pages to free\r | |
1441 | \r | |
1442 | @retval EFI_NOT_FOUND Could not find the entry that covers the range\r | |
1443 | @retval EFI_INVALID_PARAMETER Address not aligned\r | |
1444 | @return EFI_SUCCESS -Pages successfully freed.\r | |
1445 | \r | |
1446 | **/\r | |
1447 | EFI_STATUS\r | |
1448 | EFIAPI\r | |
1449 | CoreFreePages (\r | |
1450 | IN EFI_PHYSICAL_ADDRESS Memory,\r | |
1451 | IN UINTN NumberOfPages\r | |
1452 | )\r | |
1453 | {\r | |
925f0d1a SZ |
1454 | EFI_STATUS Status;\r |
1455 | EFI_MEMORY_TYPE MemoryType;\r | |
736a692e | 1456 | \r |
925f0d1a | 1457 | Status = CoreInternalFreePages (Memory, NumberOfPages, &MemoryType);\r |
736a692e | 1458 | if (!EFI_ERROR (Status)) {\r |
1d60fe96 SZ |
1459 | CoreUpdateProfile (\r |
1460 | (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r | |
1461 | MemoryProfileActionFreePages,\r | |
1462 | MemoryType,\r | |
1463 | EFI_PAGES_TO_SIZE (NumberOfPages),\r | |
1464 | (VOID *) (UINTN) Memory,\r | |
1465 | NULL\r | |
1466 | );\r | |
74a88770 | 1467 | InstallMemoryAttributesTableOnMemoryAllocation (MemoryType);\r |
7eb927db AB |
1468 | ApplyMemoryProtectionPolicy (MemoryType, EfiConventionalMemory, Memory,\r |
1469 | EFI_PAGES_TO_SIZE (NumberOfPages));\r | |
736a692e HT |
1470 | }\r |
1471 | return Status;\r | |
1472 | }\r | |
84edd20b | 1473 | \r |
2345e7d4 | 1474 | /**\r |
1475 | This function checks to see if the last memory map descriptor in a memory map\r | |
1476 | can be merged with any of the other memory map descriptors in a memorymap.\r | |
1477 | Memory descriptors may be merged if they are adjacent and have the same type\r | |
1478 | and attributes.\r | |
1479 | \r | |
1480 | @param MemoryMap A pointer to the start of the memory map.\r | |
1481 | @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.\r | |
1482 | @param DescriptorSize The size, in bytes, of an individual\r | |
1483 | EFI_MEMORY_DESCRIPTOR.\r | |
1484 | \r | |
1485 | @return A pointer to the next available descriptor in MemoryMap\r | |
1486 | \r | |
1487 | **/\r | |
1488 | EFI_MEMORY_DESCRIPTOR *\r | |
1489 | MergeMemoryMapDescriptor (\r | |
1490 | IN EFI_MEMORY_DESCRIPTOR *MemoryMap,\r | |
1491 | IN EFI_MEMORY_DESCRIPTOR *MemoryMapDescriptor,\r | |
1492 | IN UINTN DescriptorSize\r | |
1493 | )\r | |
1494 | {\r | |
1495 | //\r | |
1496 | // Traverse the array of descriptors in MemoryMap\r | |
1497 | //\r | |
1498 | for (; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) {\r | |
1499 | //\r | |
1500 | // Check to see if the Type fields are identical.\r | |
1501 | //\r | |
1502 | if (MemoryMap->Type != MemoryMapDescriptor->Type) {\r | |
1503 | continue;\r | |
1504 | }\r | |
1505 | \r | |
1506 | //\r | |
1507 | // Check to see if the Attribute fields are identical.\r | |
1508 | //\r | |
1509 | if (MemoryMap->Attribute != MemoryMapDescriptor->Attribute) {\r | |
1510 | continue;\r | |
1511 | }\r | |
1512 | \r | |
1513 | //\r | |
1514 | // Check to see if MemoryMapDescriptor is immediately above MemoryMap\r | |
1515 | //\r | |
1516 | if (MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) { \r | |
1517 | //\r | |
1518 | // Merge MemoryMapDescriptor into MemoryMap\r | |
1519 | //\r | |
1520 | MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r | |
1521 | \r | |
1522 | //\r | |
1523 | // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r | |
1524 | //\r | |
1525 | return MemoryMapDescriptor;\r | |
1526 | }\r | |
1527 | \r | |
1528 | //\r | |
1529 | // Check to see if MemoryMapDescriptor is immediately below MemoryMap\r | |
1530 | //\r | |
1531 | if (MemoryMap->PhysicalStart - EFI_PAGES_TO_SIZE ((UINTN)MemoryMapDescriptor->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {\r | |
1532 | //\r | |
1533 | // Merge MemoryMapDescriptor into MemoryMap\r | |
1534 | //\r | |
1535 | MemoryMap->PhysicalStart = MemoryMapDescriptor->PhysicalStart;\r | |
1536 | MemoryMap->VirtualStart = MemoryMapDescriptor->VirtualStart;\r | |
1537 | MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r | |
1538 | \r | |
1539 | //\r | |
1540 | // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r | |
1541 | //\r | |
1542 | return MemoryMapDescriptor;\r | |
1543 | }\r | |
1544 | }\r | |
1545 | \r | |
1546 | //\r | |
1547 | // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.\r | |
1548 | //\r | |
1549 | // Return the slot immediately after MemoryMapDescriptor as the next available \r | |
1550 | // slot in the MemoryMap array\r | |
1551 | //\r | |
1552 | return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor, DescriptorSize);\r | |
1553 | }\r | |
28a00297 | 1554 | \r |
162ed594 | 1555 | /**\r |
1556 | This function returns a copy of the current memory map. The map is an array of\r | |
1557 | memory descriptors, each of which describes a contiguous block of memory.\r | |
1558 | \r | |
022c6d45 | 1559 | @param MemoryMapSize A pointer to the size, in bytes, of the\r |
1560 | MemoryMap buffer. On input, this is the size of\r | |
1561 | the buffer allocated by the caller. On output,\r | |
1562 | it is the size of the buffer returned by the\r | |
1563 | firmware if the buffer was large enough, or the\r | |
1564 | size of the buffer needed to contain the map if\r | |
1565 | the buffer was too small.\r | |
1566 | @param MemoryMap A pointer to the buffer in which firmware places\r | |
1567 | the current memory map.\r | |
1568 | @param MapKey A pointer to the location in which firmware\r | |
1569 | returns the key for the current memory map.\r | |
1570 | @param DescriptorSize A pointer to the location in which firmware\r | |
1571 | returns the size, in bytes, of an individual\r | |
1572 | EFI_MEMORY_DESCRIPTOR.\r | |
1573 | @param DescriptorVersion A pointer to the location in which firmware\r | |
1574 | returns the version number associated with the\r | |
1575 | EFI_MEMORY_DESCRIPTOR.\r | |
1576 | \r | |
1577 | @retval EFI_SUCCESS The memory map was returned in the MemoryMap\r | |
1578 | buffer.\r | |
1579 | @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current\r | |
1580 | buffer size needed to hold the memory map is\r | |
1581 | returned in MemoryMapSize.\r | |
162ed594 | 1582 | @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r |
1583 | \r | |
1584 | **/\r | |
28a00297 | 1585 | EFI_STATUS\r |
1586 | EFIAPI\r | |
1587 | CoreGetMemoryMap (\r | |
1588 | IN OUT UINTN *MemoryMapSize,\r | |
1589 | IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r | |
1590 | OUT UINTN *MapKey,\r | |
1591 | OUT UINTN *DescriptorSize,\r | |
1592 | OUT UINT32 *DescriptorVersion\r | |
1593 | )\r | |
28a00297 | 1594 | {\r |
1595 | EFI_STATUS Status;\r | |
022c6d45 | 1596 | UINTN Size;\r |
1597 | UINTN BufferSize;\r | |
ba2c0527 | 1598 | UINTN NumberOfEntries;\r |
28a00297 | 1599 | LIST_ENTRY *Link;\r |
022c6d45 | 1600 | MEMORY_MAP *Entry;\r |
1601 | EFI_GCD_MAP_ENTRY *GcdMapEntry;\r | |
46a65f18 | 1602 | EFI_GCD_MAP_ENTRY MergeGcdMapEntry;\r |
b74350e9 | 1603 | EFI_MEMORY_TYPE Type;\r |
2345e7d4 | 1604 | EFI_MEMORY_DESCRIPTOR *MemoryMapStart;\r |
28a00297 | 1605 | \r |
1606 | //\r | |
1607 | // Make sure the parameters are valid\r | |
1608 | //\r | |
1609 | if (MemoryMapSize == NULL) {\r | |
1610 | return EFI_INVALID_PARAMETER;\r | |
1611 | }\r | |
022c6d45 | 1612 | \r |
28a00297 | 1613 | CoreAcquireGcdMemoryLock ();\r |
022c6d45 | 1614 | \r |
28a00297 | 1615 | //\r |
ba2c0527 | 1616 | // Count the number of Reserved and runtime MMIO entries\r |
a671a012 | 1617 | // And, count the number of Persistent entries.\r |
28a00297 | 1618 | //\r |
ba2c0527 | 1619 | NumberOfEntries = 0;\r |
28a00297 | 1620 | for (Link = mGcdMemorySpaceMap.ForwardLink; Link != &mGcdMemorySpaceMap; Link = Link->ForwardLink) {\r |
1621 | GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r | |
ba2c0527 LG |
1622 | if ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypePersistentMemory) || \r |
1623 | (GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r | |
1624 | ((GcdMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&\r | |
1625 | ((GcdMapEntry->Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {\r | |
1626 | NumberOfEntries ++;\r | |
a671a012 | 1627 | }\r |
28a00297 | 1628 | }\r |
1629 | \r | |
1630 | Size = sizeof (EFI_MEMORY_DESCRIPTOR);\r | |
1631 | \r | |
1632 | //\r | |
1633 | // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will\r | |
1634 | // prevent people from having pointer math bugs in their code.\r | |
1635 | // now you have to use *DescriptorSize to make things work.\r | |
1636 | //\r | |
1637 | Size += sizeof(UINT64) - (Size % sizeof (UINT64));\r | |
1638 | \r | |
1639 | if (DescriptorSize != NULL) {\r | |
1640 | *DescriptorSize = Size;\r | |
1641 | }\r | |
022c6d45 | 1642 | \r |
28a00297 | 1643 | if (DescriptorVersion != NULL) {\r |
1644 | *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;\r | |
1645 | }\r | |
1646 | \r | |
1647 | CoreAcquireMemoryLock ();\r | |
1648 | \r | |
1649 | //\r | |
1650 | // Compute the buffer size needed to fit the entire map\r | |
1651 | //\r | |
ba2c0527 | 1652 | BufferSize = Size * NumberOfEntries;\r |
28a00297 | 1653 | for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r |
1654 | BufferSize += Size;\r | |
1655 | }\r | |
1656 | \r | |
1657 | if (*MemoryMapSize < BufferSize) {\r | |
1658 | Status = EFI_BUFFER_TOO_SMALL;\r | |
1659 | goto Done;\r | |
1660 | }\r | |
1661 | \r | |
1662 | if (MemoryMap == NULL) {\r | |
1663 | Status = EFI_INVALID_PARAMETER;\r | |
1664 | goto Done;\r | |
1665 | }\r | |
1666 | \r | |
1667 | //\r | |
1668 | // Build the map\r | |
1669 | //\r | |
383c303c | 1670 | ZeroMem (MemoryMap, BufferSize);\r |
2345e7d4 | 1671 | MemoryMapStart = MemoryMap;\r |
28a00297 | 1672 | for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r |
1673 | Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
1674 | ASSERT (Entry->VirtualStart == 0);\r | |
1675 | \r | |
b74350e9 | 1676 | //\r |
1677 | // Convert internal map into an EFI_MEMORY_DESCRIPTOR\r | |
1678 | //\r | |
28a00297 | 1679 | MemoryMap->Type = Entry->Type;\r |
1680 | MemoryMap->PhysicalStart = Entry->Start;\r | |
1681 | MemoryMap->VirtualStart = Entry->VirtualStart;\r | |
1682 | MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);\r | |
b74350e9 | 1683 | //\r |
1684 | // If the memory type is EfiConventionalMemory, then determine if the range is part of a\r | |
022c6d45 | 1685 | // memory type bin and needs to be converted to the same memory type as the rest of the\r |
1686 | // memory type bin in order to minimize EFI Memory Map changes across reboots. This\r | |
b74350e9 | 1687 | // improves the chances for a successful S4 resume in the presence of minor page allocation\r |
1688 | // differences across reboots.\r | |
1689 | //\r | |
1690 | if (MemoryMap->Type == EfiConventionalMemory) {\r | |
1691 | for (Type = (EFI_MEMORY_TYPE) 0; Type < EfiMaxMemoryType; Type++) {\r | |
1692 | if (mMemoryTypeStatistics[Type].Special &&\r | |
1693 | mMemoryTypeStatistics[Type].NumberOfPages > 0 &&\r | |
1694 | Entry->Start >= mMemoryTypeStatistics[Type].BaseAddress &&\r | |
e94a9ff7 | 1695 | Entry->End <= mMemoryTypeStatistics[Type].MaximumAddress) {\r |
b74350e9 | 1696 | MemoryMap->Type = Type;\r |
1697 | }\r | |
1698 | }\r | |
1699 | }\r | |
1700 | MemoryMap->Attribute = Entry->Attribute;\r | |
10fe0d81 RN |
1701 | if (MemoryMap->Type < EfiMaxMemoryType) {\r |
1702 | if (mMemoryTypeStatistics[MemoryMap->Type].Runtime) {\r | |
1703 | MemoryMap->Attribute |= EFI_MEMORY_RUNTIME;\r | |
1704 | }\r | |
28a00297 | 1705 | }\r |
022c6d45 | 1706 | \r |
2345e7d4 | 1707 | //\r |
1708 | // Check to see if the new Memory Map Descriptor can be merged with an \r | |
1709 | // existing descriptor if they are adjacent and have the same attributes\r | |
1710 | //\r | |
1711 | MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r | |
28a00297 | 1712 | }\r |
1713 | \r | |
46a65f18 LG |
1714 | \r |
1715 | ZeroMem (&MergeGcdMapEntry, sizeof (MergeGcdMapEntry));\r | |
1716 | GcdMapEntry = NULL;\r | |
1717 | for (Link = mGcdMemorySpaceMap.ForwardLink; ; Link = Link->ForwardLink) {\r | |
1718 | if (Link != &mGcdMemorySpaceMap) {\r | |
1719 | //\r | |
1720 | // Merge adjacent same type and attribute GCD memory range\r | |
1721 | //\r | |
1722 | GcdMapEntry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);\r | |
1723 | \r | |
1724 | if ((MergeGcdMapEntry.Capabilities == GcdMapEntry->Capabilities) && \r | |
1725 | (MergeGcdMapEntry.Attributes == GcdMapEntry->Attributes) &&\r | |
1726 | (MergeGcdMapEntry.GcdMemoryType == GcdMapEntry->GcdMemoryType) &&\r | |
1727 | (MergeGcdMapEntry.GcdIoType == GcdMapEntry->GcdIoType)) {\r | |
1728 | MergeGcdMapEntry.EndAddress = GcdMapEntry->EndAddress;\r | |
1729 | continue;\r | |
1730 | }\r | |
1731 | }\r | |
1732 | \r | |
1733 | if ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) ||\r | |
1734 | ((MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&\r | |
1735 | ((MergeGcdMapEntry.Attributes & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME))) {\r | |
1736 | //\r | |
1737 | // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR, \r | |
1738 | // it will be recorded as page PhysicalStart and NumberOfPages. \r | |
1739 | //\r | |
1740 | ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);\r | |
1741 | ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);\r | |
1742 | \r | |
ba2c0527 LG |
1743 | // \r |
1744 | // Create EFI_MEMORY_DESCRIPTOR for every Reserved and runtime MMIO GCD entries\r | |
1745 | //\r | |
46a65f18 | 1746 | MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;\r |
ba2c0527 | 1747 | MemoryMap->VirtualStart = 0;\r |
46a65f18 LG |
1748 | MemoryMap->NumberOfPages = RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);\r |
1749 | MemoryMap->Attribute = (MergeGcdMapEntry.Attributes & ~EFI_MEMORY_PORT_IO) | \r | |
1750 | (MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |\r | |
ff0c6d66 | 1751 | EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));\r |
ba2c0527 | 1752 | \r |
46a65f18 | 1753 | if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeReserved) {\r |
ba2c0527 | 1754 | MemoryMap->Type = EfiReservedMemoryType;\r |
46a65f18 LG |
1755 | } else if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) {\r |
1756 | if ((MergeGcdMapEntry.Attributes & EFI_MEMORY_PORT_IO) == EFI_MEMORY_PORT_IO) {\r | |
ba2c0527 LG |
1757 | MemoryMap->Type = EfiMemoryMappedIOPortSpace;\r |
1758 | } else {\r | |
1759 | MemoryMap->Type = EfiMemoryMappedIO;\r | |
28a00297 | 1760 | }\r |
28a00297 | 1761 | }\r |
ba2c0527 LG |
1762 | \r |
1763 | //\r | |
1764 | // Check to see if the new Memory Map Descriptor can be merged with an \r | |
1765 | // existing descriptor if they are adjacent and have the same attributes\r | |
1766 | //\r | |
1767 | MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r | |
28a00297 | 1768 | }\r |
a671a012 | 1769 | \r |
46a65f18 LG |
1770 | if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypePersistentMemory) {\r |
1771 | //\r | |
1772 | // Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR, \r | |
1773 | // it will be recorded as page PhysicalStart and NumberOfPages. \r | |
1774 | //\r | |
1775 | ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);\r | |
1776 | ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);\r | |
1777 | \r | |
a671a012 LG |
1778 | // \r |
1779 | // Create EFI_MEMORY_DESCRIPTOR for every Persistent GCD entries\r | |
1780 | //\r | |
46a65f18 | 1781 | MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;\r |
a671a012 | 1782 | MemoryMap->VirtualStart = 0;\r |
46a65f18 LG |
1783 | MemoryMap->NumberOfPages = RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);\r |
1784 | MemoryMap->Attribute = MergeGcdMapEntry.Attributes | EFI_MEMORY_NV | \r | |
1785 | (MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |\r | |
ff0c6d66 | 1786 | EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));\r |
a671a012 LG |
1787 | MemoryMap->Type = EfiPersistentMemory;\r |
1788 | \r | |
1789 | //\r | |
1790 | // Check to see if the new Memory Map Descriptor can be merged with an \r | |
1791 | // existing descriptor if they are adjacent and have the same attributes\r | |
1792 | //\r | |
1793 | MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);\r | |
1794 | }\r | |
46a65f18 LG |
1795 | if (Link == &mGcdMemorySpaceMap) {\r |
1796 | //\r | |
1797 | // break loop when arrive at head.\r | |
1798 | //\r | |
1799 | break;\r | |
1800 | }\r | |
1801 | if (GcdMapEntry != NULL) {\r | |
1802 | //\r | |
1803 | // Copy new GCD map entry for the following GCD range merge\r | |
1804 | //\r | |
1805 | CopyMem (&MergeGcdMapEntry, GcdMapEntry, sizeof (MergeGcdMapEntry));\r | |
1806 | }\r | |
28a00297 | 1807 | }\r |
022c6d45 | 1808 | \r |
2345e7d4 | 1809 | //\r |
1810 | // Compute the size of the buffer actually used after all memory map descriptor merge operations\r | |
1811 | //\r | |
1812 | BufferSize = ((UINT8 *)MemoryMap - (UINT8 *)MemoryMapStart);\r | |
1813 | \r | |
28a00297 | 1814 | Status = EFI_SUCCESS;\r |
1815 | \r | |
1816 | Done:\r | |
022c6d45 | 1817 | //\r |
1818 | // Update the map key finally\r | |
1819 | //\r | |
28a00297 | 1820 | if (MapKey != NULL) {\r |
1821 | *MapKey = mMemoryMapKey;\r | |
1822 | }\r | |
022c6d45 | 1823 | \r |
e439df50 | 1824 | CoreReleaseMemoryLock ();\r |
1825 | \r | |
1826 | CoreReleaseGcdMemoryLock ();\r | |
1827 | \r | |
28a00297 | 1828 | *MemoryMapSize = BufferSize;\r |
022c6d45 | 1829 | \r |
28a00297 | 1830 | return Status;\r |
1831 | }\r | |
1832 | \r | |
28a00297 | 1833 | \r |
162ed594 | 1834 | /**\r |
28a00297 | 1835 | Internal function. Used by the pool functions to allocate pages\r |
1836 | to back pool allocation requests.\r | |
1837 | \r | |
022c6d45 | 1838 | @param PoolType The type of memory for the new pool pages\r |
1839 | @param NumberOfPages No of pages to allocate\r | |
1840 | @param Alignment Bits to align.\r | |
28a00297 | 1841 | \r |
162ed594 | 1842 | @return The allocated memory, or NULL\r |
28a00297 | 1843 | \r |
162ed594 | 1844 | **/\r |
1845 | VOID *\r | |
1846 | CoreAllocatePoolPages (\r | |
1847 | IN EFI_MEMORY_TYPE PoolType,\r | |
1848 | IN UINTN NumberOfPages,\r | |
1849 | IN UINTN Alignment\r | |
1850 | )\r | |
28a00297 | 1851 | {\r |
1852 | UINT64 Start;\r | |
1853 | \r | |
1854 | //\r | |
1855 | // Find the pages to convert\r | |
1856 | //\r | |
f3f2e05d | 1857 | Start = FindFreePages (MAX_ADDRESS, NumberOfPages, PoolType, Alignment);\r |
28a00297 | 1858 | \r |
1859 | //\r | |
1860 | // Convert it to boot services data\r | |
1861 | //\r | |
1862 | if (Start == 0) {\r | |
7df7393f | 1863 | DEBUG ((DEBUG_ERROR | DEBUG_PAGE, "AllocatePoolPages: failed to allocate %d pages\n", (UINT32)NumberOfPages));\r |
28a00297 | 1864 | } else {\r |
1865 | CoreConvertPages (Start, NumberOfPages, PoolType);\r | |
1866 | }\r | |
1867 | \r | |
e94a9ff7 | 1868 | return (VOID *)(UINTN) Start;\r |
28a00297 | 1869 | }\r |
1870 | \r | |
162ed594 | 1871 | \r |
1872 | /**\r | |
1873 | Internal function. Frees pool pages allocated via AllocatePoolPages ()\r | |
1874 | \r | |
022c6d45 | 1875 | @param Memory The base address to free\r |
162ed594 | 1876 | @param NumberOfPages The number of pages to free\r |
1877 | \r | |
1878 | **/\r | |
28a00297 | 1879 | VOID\r |
1880 | CoreFreePoolPages (\r | |
1881 | IN EFI_PHYSICAL_ADDRESS Memory,\r | |
1882 | IN UINTN NumberOfPages\r | |
1883 | )\r | |
28a00297 | 1884 | {\r |
1885 | CoreConvertPages (Memory, NumberOfPages, EfiConventionalMemory);\r | |
1886 | }\r | |
1887 | \r | |
1888 | \r | |
28a00297 | 1889 | \r |
162ed594 | 1890 | /**\r |
1891 | Make sure the memory map is following all the construction rules,\r | |
28a00297 | 1892 | it is the last time to check memory map error before exit boot services.\r |
1893 | \r | |
022c6d45 | 1894 | @param MapKey Memory map key\r |
28a00297 | 1895 | \r |
022c6d45 | 1896 | @retval EFI_INVALID_PARAMETER Memory map not consistent with construction\r |
1897 | rules.\r | |
162ed594 | 1898 | @retval EFI_SUCCESS Valid memory map.\r |
28a00297 | 1899 | \r |
162ed594 | 1900 | **/\r |
1901 | EFI_STATUS\r | |
1902 | CoreTerminateMemoryMap (\r | |
1903 | IN UINTN MapKey\r | |
1904 | )\r | |
28a00297 | 1905 | {\r |
1906 | EFI_STATUS Status;\r | |
1907 | LIST_ENTRY *Link;\r | |
1908 | MEMORY_MAP *Entry;\r | |
1909 | \r | |
1910 | Status = EFI_SUCCESS;\r | |
1911 | \r | |
1912 | CoreAcquireMemoryLock ();\r | |
1913 | \r | |
1914 | if (MapKey == mMemoryMapKey) {\r | |
1915 | \r | |
1916 | //\r | |
1917 | // Make sure the memory map is following all the construction rules\r | |
1918 | // This is the last chance we will be able to display any messages on\r | |
1919 | // the console devices.\r | |
1920 | //\r | |
1921 | \r | |
1922 | for (Link = gMemoryMap.ForwardLink; Link != &gMemoryMap; Link = Link->ForwardLink) {\r | |
1923 | Entry = CR(Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
7d17a6a1 AB |
1924 | if (Entry->Type < EfiMaxMemoryType) {\r |
1925 | if (mMemoryTypeStatistics[Entry->Type].Runtime) {\r | |
1926 | ASSERT (Entry->Type != EfiACPIReclaimMemory);\r | |
1927 | ASSERT (Entry->Type != EfiACPIMemoryNVS);\r | |
d4731a98 | 1928 | if ((Entry->Start & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {\r |
7d17a6a1 AB |
1929 | DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r |
1930 | Status = EFI_INVALID_PARAMETER;\r | |
1931 | goto Done;\r | |
1932 | }\r | |
d4731a98 | 1933 | if (((Entry->End + 1) & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) != 0) {\r |
7d17a6a1 AB |
1934 | DEBUG((DEBUG_ERROR | DEBUG_PAGE, "ExitBootServices: A RUNTIME memory entry is not on a proper alignment.\n"));\r |
1935 | Status = EFI_INVALID_PARAMETER;\r | |
1936 | goto Done;\r | |
1937 | }\r | |
28a00297 | 1938 | }\r |
1939 | }\r | |
1940 | }\r | |
1941 | \r | |
1942 | //\r | |
1943 | // The map key they gave us matches what we expect. Fall through and\r | |
1944 | // return success. In an ideal world we would clear out all of\r | |
1945 | // EfiBootServicesCode and EfiBootServicesData. However this function\r | |
1946 | // is not the last one called by ExitBootServices(), so we have to\r | |
1947 | // preserve the memory contents.\r | |
1948 | //\r | |
1949 | } else {\r | |
1950 | Status = EFI_INVALID_PARAMETER;\r | |
1951 | }\r | |
1952 | \r | |
d45fd260 | 1953 | Done:\r |
28a00297 | 1954 | CoreReleaseMemoryLock ();\r |
1955 | \r | |
1956 | return Status;\r | |
1957 | }\r | |
1958 | \r | |
1959 | \r | |
1960 | \r | |
1961 | \r | |
1962 | \r | |
1963 | \r | |
1964 | \r | |
1965 | \r | |
162ed594 | 1966 | \r |