]>
Commit | Line | Data |
---|---|---|
e42e9404 | 1 | /** @file\r |
2 | SMM Memory page management functions.\r | |
3 | \r | |
e524f680 | 4 | Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r |
285a682c JY |
5 | This program and the accompanying materials are licensed and made available\r |
6 | under the terms and conditions of the BSD License which accompanies this\r | |
7 | distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
e42e9404 | 9 | \r |
285a682c JY |
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 | |
e42e9404 | 12 | \r |
13 | **/\r | |
14 | \r | |
15 | #include "PiSmmCore.h"\r | |
285a682c | 16 | #include <Library/SmmServicesTableLib.h>\r |
e42e9404 | 17 | \r |
18 | #define TRUNCATE_TO_PAGES(a) ((a) >> EFI_PAGE_SHIFT)\r | |
19 | \r | |
e42e9404 | 20 | LIST_ENTRY mSmmMemoryMap = INITIALIZE_LIST_HEAD_VARIABLE (mSmmMemoryMap);\r |
21 | \r | |
285a682c JY |
22 | //\r |
23 | // For GetMemoryMap()\r | |
24 | //\r | |
25 | \r | |
26 | #define MEMORY_MAP_SIGNATURE SIGNATURE_32('m','m','a','p')\r | |
27 | typedef struct {\r | |
28 | UINTN Signature;\r | |
29 | LIST_ENTRY Link;\r | |
30 | \r | |
31 | BOOLEAN FromStack;\r | |
32 | EFI_MEMORY_TYPE Type;\r | |
33 | UINT64 Start;\r | |
34 | UINT64 End;\r | |
35 | \r | |
36 | } MEMORY_MAP;\r | |
37 | \r | |
38 | LIST_ENTRY gMemoryMap = INITIALIZE_LIST_HEAD_VARIABLE (gMemoryMap);\r | |
39 | \r | |
40 | \r | |
41 | #define MAX_MAP_DEPTH 6\r | |
42 | \r | |
43 | ///\r | |
44 | /// mMapDepth - depth of new descriptor stack\r | |
45 | ///\r | |
46 | UINTN mMapDepth = 0;\r | |
47 | ///\r | |
48 | /// mMapStack - space to use as temp storage to build new map descriptors\r | |
49 | ///\r | |
50 | MEMORY_MAP mMapStack[MAX_MAP_DEPTH];\r | |
51 | UINTN mFreeMapStack = 0;\r | |
52 | ///\r | |
53 | /// This list maintain the free memory map list\r | |
54 | ///\r | |
55 | LIST_ENTRY mFreeMemoryMapEntryList = INITIALIZE_LIST_HEAD_VARIABLE (mFreeMemoryMapEntryList);\r | |
56 | \r | |
57 | /**\r | |
58 | Allocates pages from the memory map.\r | |
59 | \r | |
60 | @param[in] Type The type of allocation to perform.\r | |
61 | @param[in] MemoryType The type of memory to turn the allocated pages\r | |
62 | into.\r | |
63 | @param[in] NumberOfPages The number of pages to allocate.\r | |
64 | @param[out] Memory A pointer to receive the base allocated memory\r | |
65 | address.\r | |
66 | @param[in] AddRegion If this memory is new added region.\r | |
e63da9f0 JW |
67 | @param[in] NeedGuard Flag to indicate Guard page is needed\r |
68 | or not\r | |
285a682c JY |
69 | \r |
70 | @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.\r | |
71 | @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r | |
72 | @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r | |
73 | @retval EFI_SUCCESS Pages successfully allocated.\r | |
74 | \r | |
75 | **/\r | |
76 | EFI_STATUS\r | |
77 | SmmInternalAllocatePagesEx (\r | |
78 | IN EFI_ALLOCATE_TYPE Type,\r | |
79 | IN EFI_MEMORY_TYPE MemoryType,\r | |
80 | IN UINTN NumberOfPages,\r | |
81 | OUT EFI_PHYSICAL_ADDRESS *Memory,\r | |
e63da9f0 JW |
82 | IN BOOLEAN AddRegion,\r |
83 | IN BOOLEAN NeedGuard\r | |
285a682c JY |
84 | );\r |
85 | \r | |
86 | /**\r | |
87 | Internal function. Deque a descriptor entry from the mFreeMemoryMapEntryList.\r | |
88 | If the list is emtry, then allocate a new page to refuel the list.\r | |
89 | Please Note this algorithm to allocate the memory map descriptor has a property\r | |
90 | that the memory allocated for memory entries always grows, and will never really be freed.\r | |
91 | \r | |
92 | @return The Memory map descriptor dequed from the mFreeMemoryMapEntryList\r | |
93 | \r | |
94 | **/\r | |
95 | MEMORY_MAP *\r | |
96 | AllocateMemoryMapEntry (\r | |
97 | VOID\r | |
98 | )\r | |
99 | {\r | |
100 | EFI_PHYSICAL_ADDRESS Mem;\r | |
101 | EFI_STATUS Status;\r | |
102 | MEMORY_MAP* FreeDescriptorEntries;\r | |
103 | MEMORY_MAP* Entry;\r | |
104 | UINTN Index;\r | |
105 | \r | |
106 | //DEBUG((DEBUG_INFO, "AllocateMemoryMapEntry\n"));\r | |
107 | \r | |
108 | if (IsListEmpty (&mFreeMemoryMapEntryList)) {\r | |
109 | //DEBUG((DEBUG_INFO, "mFreeMemoryMapEntryList is empty\n"));\r | |
110 | //\r | |
111 | // The list is empty, to allocate one page to refuel the list\r | |
112 | //\r | |
113 | Status = SmmInternalAllocatePagesEx (\r | |
114 | AllocateAnyPages,\r | |
115 | EfiRuntimeServicesData,\r | |
115d9753 | 116 | EFI_SIZE_TO_PAGES (RUNTIME_PAGE_ALLOCATION_GRANULARITY),\r |
285a682c | 117 | &Mem,\r |
e63da9f0 JW |
118 | TRUE,\r |
119 | FALSE\r | |
285a682c JY |
120 | );\r |
121 | ASSERT_EFI_ERROR (Status);\r | |
122 | if(!EFI_ERROR (Status)) {\r | |
123 | FreeDescriptorEntries = (MEMORY_MAP *)(UINTN)Mem;\r | |
124 | //DEBUG((DEBUG_INFO, "New FreeDescriptorEntries - 0x%x\n", FreeDescriptorEntries));\r | |
125 | //\r | |
126 | // Enque the free memmory map entries into the list\r | |
127 | //\r | |
115d9753 | 128 | for (Index = 0; Index< RUNTIME_PAGE_ALLOCATION_GRANULARITY / sizeof(MEMORY_MAP); Index++) {\r |
285a682c JY |
129 | FreeDescriptorEntries[Index].Signature = MEMORY_MAP_SIGNATURE;\r |
130 | InsertTailList (&mFreeMemoryMapEntryList, &FreeDescriptorEntries[Index].Link);\r | |
131 | }\r | |
132 | } else {\r | |
133 | return NULL;\r | |
134 | }\r | |
135 | }\r | |
136 | //\r | |
137 | // dequeue the first descriptor from the list\r | |
138 | //\r | |
139 | Entry = CR (mFreeMemoryMapEntryList.ForwardLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
140 | RemoveEntryList (&Entry->Link);\r | |
141 | \r | |
142 | return Entry;\r | |
143 | }\r | |
144 | \r | |
145 | \r | |
146 | /**\r | |
147 | Internal function. Moves any memory descriptors that are on the\r | |
148 | temporary descriptor stack to heap.\r | |
149 | \r | |
150 | **/\r | |
151 | VOID\r | |
152 | CoreFreeMemoryMapStack (\r | |
153 | VOID\r | |
154 | )\r | |
155 | {\r | |
156 | MEMORY_MAP *Entry;\r | |
157 | \r | |
158 | //\r | |
159 | // If already freeing the map stack, then return\r | |
160 | //\r | |
161 | if (mFreeMapStack != 0) {\r | |
162 | ASSERT (FALSE);\r | |
163 | return ;\r | |
164 | }\r | |
165 | \r | |
166 | //\r | |
167 | // Move the temporary memory descriptor stack into pool\r | |
168 | //\r | |
169 | mFreeMapStack += 1;\r | |
170 | \r | |
171 | while (mMapDepth != 0) {\r | |
172 | //\r | |
173 | // Deque an memory map entry from mFreeMemoryMapEntryList\r | |
174 | //\r | |
175 | Entry = AllocateMemoryMapEntry ();\r | |
176 | ASSERT (Entry);\r | |
177 | \r | |
178 | //\r | |
179 | // Update to proper entry\r | |
180 | //\r | |
181 | mMapDepth -= 1;\r | |
182 | \r | |
183 | if (mMapStack[mMapDepth].Link.ForwardLink != NULL) {\r | |
184 | \r | |
185 | CopyMem (Entry , &mMapStack[mMapDepth], sizeof (MEMORY_MAP));\r | |
186 | Entry->FromStack = FALSE;\r | |
187 | \r | |
188 | //\r | |
189 | // Move this entry to general memory\r | |
190 | //\r | |
191 | InsertTailList (&mMapStack[mMapDepth].Link, &Entry->Link);\r | |
192 | RemoveEntryList (&mMapStack[mMapDepth].Link);\r | |
193 | mMapStack[mMapDepth].Link.ForwardLink = NULL;\r | |
194 | }\r | |
195 | }\r | |
196 | \r | |
197 | mFreeMapStack -= 1;\r | |
198 | }\r | |
199 | \r | |
200 | /**\r | |
201 | Insert new entry from memory map.\r | |
202 | \r | |
203 | @param[in] Link The old memory map entry to be linked.\r | |
204 | @param[in] Start The start address of new memory map entry.\r | |
205 | @param[in] End The end address of new memory map entry.\r | |
206 | @param[in] Type The type of new memory map entry.\r | |
207 | @param[in] Next If new entry is inserted to the next of old entry.\r | |
208 | @param[in] AddRegion If this memory is new added region.\r | |
209 | **/\r | |
210 | VOID\r | |
211 | InsertNewEntry (\r | |
212 | IN LIST_ENTRY *Link,\r | |
213 | IN UINT64 Start,\r | |
214 | IN UINT64 End,\r | |
215 | IN EFI_MEMORY_TYPE Type,\r | |
216 | IN BOOLEAN Next,\r | |
217 | IN BOOLEAN AddRegion\r | |
218 | )\r | |
219 | {\r | |
220 | MEMORY_MAP *Entry;\r | |
221 | \r | |
222 | Entry = &mMapStack[mMapDepth];\r | |
223 | mMapDepth += 1;\r | |
224 | ASSERT (mMapDepth < MAX_MAP_DEPTH);\r | |
225 | Entry->FromStack = TRUE;\r | |
226 | \r | |
227 | Entry->Signature = MEMORY_MAP_SIGNATURE;\r | |
228 | Entry->Type = Type;\r | |
229 | Entry->Start = Start;\r | |
230 | Entry->End = End;\r | |
231 | if (Next) {\r | |
232 | InsertHeadList (Link, &Entry->Link);\r | |
233 | } else {\r | |
234 | InsertTailList (Link, &Entry->Link);\r | |
235 | }\r | |
236 | }\r | |
237 | \r | |
238 | /**\r | |
239 | Remove old entry from memory map.\r | |
240 | \r | |
241 | @param[in] Entry Memory map entry to be removed.\r | |
242 | **/\r | |
243 | VOID\r | |
244 | RemoveOldEntry (\r | |
245 | IN MEMORY_MAP *Entry\r | |
246 | )\r | |
247 | {\r | |
248 | RemoveEntryList (&Entry->Link);\r | |
249 | if (!Entry->FromStack) {\r | |
250 | InsertTailList (&mFreeMemoryMapEntryList, &Entry->Link);\r | |
251 | }\r | |
252 | }\r | |
253 | \r | |
254 | /**\r | |
255 | Update SMM memory map entry.\r | |
256 | \r | |
257 | @param[in] Type The type of allocation to perform.\r | |
258 | @param[in] Memory The base of memory address.\r | |
259 | @param[in] NumberOfPages The number of pages to allocate.\r | |
260 | @param[in] AddRegion If this memory is new added region.\r | |
261 | **/\r | |
262 | VOID\r | |
263 | ConvertSmmMemoryMapEntry (\r | |
264 | IN EFI_MEMORY_TYPE Type,\r | |
265 | IN EFI_PHYSICAL_ADDRESS Memory,\r | |
266 | IN UINTN NumberOfPages,\r | |
267 | IN BOOLEAN AddRegion\r | |
268 | )\r | |
269 | {\r | |
270 | LIST_ENTRY *Link;\r | |
271 | MEMORY_MAP *Entry;\r | |
272 | MEMORY_MAP *NextEntry;\r | |
273 | LIST_ENTRY *NextLink;\r | |
274 | MEMORY_MAP *PreviousEntry;\r | |
275 | LIST_ENTRY *PreviousLink;\r | |
276 | EFI_PHYSICAL_ADDRESS Start;\r | |
277 | EFI_PHYSICAL_ADDRESS End;\r | |
278 | \r | |
279 | Start = Memory;\r | |
280 | End = Memory + EFI_PAGES_TO_SIZE(NumberOfPages) - 1;\r | |
281 | \r | |
282 | //\r | |
283 | // Exclude memory region\r | |
284 | //\r | |
285 | Link = gMemoryMap.ForwardLink;\r | |
286 | while (Link != &gMemoryMap) {\r | |
287 | Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
288 | Link = Link->ForwardLink;\r | |
289 | \r | |
290 | //\r | |
291 | // ---------------------------------------------------\r | |
292 | // | +----------+ +------+ +------+ +------+ |\r | |
293 | // ---|gMemoryMep|---|Entry1|---|Entry2|---|Entry3|---\r | |
294 | // +----------+ ^ +------+ +------+ +------+\r | |
295 | // |\r | |
296 | // +------+\r | |
297 | // |EntryX|\r | |
298 | // +------+\r | |
299 | //\r | |
300 | if (Entry->Start > End) {\r | |
301 | if ((Entry->Start == End + 1) && (Entry->Type == Type)) {\r | |
302 | Entry->Start = Start;\r | |
303 | return ;\r | |
304 | }\r | |
305 | InsertNewEntry (\r | |
306 | &Entry->Link,\r | |
307 | Start,\r | |
308 | End,\r | |
309 | Type,\r | |
310 | FALSE,\r | |
311 | AddRegion\r | |
312 | );\r | |
313 | return ;\r | |
314 | }\r | |
315 | \r | |
316 | if ((Entry->Start <= Start) && (Entry->End >= End)) {\r | |
317 | if (Entry->Type != Type) {\r | |
318 | if (Entry->Start < Start) {\r | |
319 | //\r | |
320 | // ---------------------------------------------------\r | |
321 | // | +----------+ +------+ +------+ +------+ |\r | |
322 | // ---|gMemoryMep|---|Entry1|---|EntryX|---|Entry3|---\r | |
323 | // +----------+ +------+ ^ +------+ +------+\r | |
324 | // |\r | |
325 | // +------+\r | |
326 | // |EntryA|\r | |
327 | // +------+\r | |
328 | //\r | |
329 | InsertNewEntry (\r | |
330 | &Entry->Link,\r | |
331 | Entry->Start,\r | |
332 | Start - 1,\r | |
333 | Entry->Type,\r | |
334 | FALSE,\r | |
335 | AddRegion\r | |
336 | );\r | |
337 | }\r | |
338 | if (Entry->End > End) {\r | |
339 | //\r | |
340 | // ---------------------------------------------------\r | |
341 | // | +----------+ +------+ +------+ +------+ |\r | |
342 | // ---|gMemoryMep|---|Entry1|---|EntryX|---|Entry3|---\r | |
343 | // +----------+ +------+ +------+ ^ +------+\r | |
344 | // |\r | |
345 | // +------+\r | |
346 | // |EntryZ|\r | |
347 | // +------+\r | |
348 | //\r | |
349 | InsertNewEntry (\r | |
350 | &Entry->Link,\r | |
351 | End + 1,\r | |
352 | Entry->End,\r | |
353 | Entry->Type,\r | |
354 | TRUE,\r | |
355 | AddRegion\r | |
356 | );\r | |
357 | }\r | |
358 | //\r | |
359 | // Update this node\r | |
360 | //\r | |
361 | Entry->Start = Start;\r | |
362 | Entry->End = End;\r | |
363 | Entry->Type = Type;\r | |
364 | \r | |
365 | //\r | |
366 | // Check adjacent\r | |
367 | //\r | |
368 | NextLink = Entry->Link.ForwardLink;\r | |
369 | if (NextLink != &gMemoryMap) {\r | |
370 | NextEntry = CR (NextLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
371 | //\r | |
372 | // ---------------------------------------------------\r | |
373 | // | +----------+ +------+ +-----------------+ |\r | |
374 | // ---|gMemoryMep|---|Entry1|---|EntryX Entry3|---\r | |
375 | // +----------+ +------+ +-----------------+\r | |
376 | //\r | |
377 | if ((Entry->Type == NextEntry->Type) && (Entry->End + 1 == NextEntry->Start)) {\r | |
378 | Entry->End = NextEntry->End;\r | |
379 | RemoveOldEntry (NextEntry);\r | |
380 | }\r | |
381 | }\r | |
382 | PreviousLink = Entry->Link.BackLink;\r | |
383 | if (PreviousLink != &gMemoryMap) {\r | |
384 | PreviousEntry = CR (PreviousLink, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
385 | //\r | |
386 | // ---------------------------------------------------\r | |
387 | // | +----------+ +-----------------+ +------+ |\r | |
388 | // ---|gMemoryMep|---|Entry1 EntryX|---|Entry3|---\r | |
389 | // +----------+ +-----------------+ +------+\r | |
390 | //\r | |
391 | if ((PreviousEntry->Type == Entry->Type) && (PreviousEntry->End + 1 == Entry->Start)) {\r | |
392 | PreviousEntry->End = Entry->End;\r | |
393 | RemoveOldEntry (Entry);\r | |
394 | }\r | |
395 | }\r | |
396 | }\r | |
397 | return ;\r | |
398 | }\r | |
399 | }\r | |
400 | \r | |
401 | //\r | |
402 | // ---------------------------------------------------\r | |
403 | // | +----------+ +------+ +------+ +------+ |\r | |
404 | // ---|gMemoryMep|---|Entry1|---|Entry2|---|Entry3|---\r | |
405 | // +----------+ +------+ +------+ +------+ ^\r | |
406 | // |\r | |
407 | // +------+\r | |
408 | // |EntryX|\r | |
409 | // +------+\r | |
410 | //\r | |
411 | Link = gMemoryMap.BackLink;\r | |
412 | if (Link != &gMemoryMap) {\r | |
413 | Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
414 | if ((Entry->End + 1 == Start) && (Entry->Type == Type)) {\r | |
415 | Entry->End = End;\r | |
416 | return ;\r | |
417 | }\r | |
418 | }\r | |
419 | InsertNewEntry (\r | |
420 | &gMemoryMap,\r | |
421 | Start,\r | |
422 | End,\r | |
423 | Type,\r | |
424 | FALSE,\r | |
425 | AddRegion\r | |
426 | );\r | |
427 | return ;\r | |
428 | }\r | |
429 | \r | |
430 | /**\r | |
431 | Return the count of Smm memory map entry.\r | |
432 | \r | |
433 | @return The count of Smm memory map entry.\r | |
434 | **/\r | |
435 | UINTN\r | |
436 | GetSmmMemoryMapEntryCount (\r | |
437 | VOID\r | |
438 | )\r | |
439 | {\r | |
440 | LIST_ENTRY *Link;\r | |
441 | UINTN Count;\r | |
442 | \r | |
443 | Count = 0;\r | |
444 | Link = gMemoryMap.ForwardLink;\r | |
445 | while (Link != &gMemoryMap) {\r | |
446 | Link = Link->ForwardLink;\r | |
447 | Count++;\r | |
448 | }\r | |
449 | return Count;\r | |
450 | }\r | |
451 | \r | |
452 | /**\r | |
453 | Dump Smm memory map entry.\r | |
454 | **/\r | |
455 | VOID\r | |
456 | DumpSmmMemoryMapEntry (\r | |
457 | VOID\r | |
458 | )\r | |
459 | {\r | |
460 | LIST_ENTRY *Link;\r | |
461 | MEMORY_MAP *Entry;\r | |
462 | EFI_PHYSICAL_ADDRESS Last;\r | |
463 | \r | |
464 | Last = 0;\r | |
465 | DEBUG ((DEBUG_INFO, "DumpSmmMemoryMapEntry:\n"));\r | |
466 | Link = gMemoryMap.ForwardLink;\r | |
467 | while (Link != &gMemoryMap) {\r | |
468 | Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
469 | Link = Link->ForwardLink;\r | |
470 | \r | |
471 | if ((Last != 0) && (Last != (UINT64)-1)) {\r | |
472 | if (Last + 1 != Entry->Start) {\r | |
473 | Last = (UINT64)-1;\r | |
474 | } else {\r | |
475 | Last = Entry->End;\r | |
476 | }\r | |
477 | } else if (Last == 0) {\r | |
478 | Last = Entry->End;\r | |
479 | }\r | |
480 | \r | |
481 | DEBUG ((DEBUG_INFO, "Entry (Link - 0x%x)\n", &Entry->Link));\r | |
482 | DEBUG ((DEBUG_INFO, " Signature - 0x%x\n", Entry->Signature));\r | |
483 | DEBUG ((DEBUG_INFO, " Link.ForwardLink - 0x%x\n", Entry->Link.ForwardLink));\r | |
484 | DEBUG ((DEBUG_INFO, " Link.BackLink - 0x%x\n", Entry->Link.BackLink));\r | |
485 | DEBUG ((DEBUG_INFO, " Type - 0x%x\n", Entry->Type));\r | |
486 | DEBUG ((DEBUG_INFO, " Start - 0x%016lx\n", Entry->Start));\r | |
487 | DEBUG ((DEBUG_INFO, " End - 0x%016lx\n", Entry->End));\r | |
488 | }\r | |
489 | \r | |
490 | ASSERT (Last != (UINT64)-1);\r | |
491 | }\r | |
492 | \r | |
493 | /**\r | |
494 | Dump Smm memory map.\r | |
495 | **/\r | |
496 | VOID\r | |
497 | DumpSmmMemoryMap (\r | |
498 | VOID\r | |
499 | )\r | |
500 | {\r | |
501 | LIST_ENTRY *Node;\r | |
502 | FREE_PAGE_LIST *Pages;\r | |
503 | \r | |
504 | DEBUG ((DEBUG_INFO, "DumpSmmMemoryMap\n"));\r | |
505 | \r | |
506 | Pages = NULL;\r | |
507 | Node = mSmmMemoryMap.ForwardLink;\r | |
508 | while (Node != &mSmmMemoryMap) {\r | |
509 | Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);\r | |
510 | DEBUG ((DEBUG_INFO, "Pages - 0x%x\n", Pages));\r | |
511 | DEBUG ((DEBUG_INFO, "Pages->NumberOfPages - 0x%x\n", Pages->NumberOfPages));\r | |
512 | Node = Node->ForwardLink;\r | |
513 | }\r | |
514 | }\r | |
515 | \r | |
516 | /**\r | |
517 | Check if a Smm base~length is in Smm memory map.\r | |
518 | \r | |
519 | @param[in] Base The base address of Smm memory to be checked.\r | |
520 | @param[in] Length THe length of Smm memory to be checked.\r | |
521 | \r | |
522 | @retval TRUE Smm base~length is in smm memory map.\r | |
523 | @retval FALSE Smm base~length is in smm memory map.\r | |
524 | **/\r | |
525 | BOOLEAN\r | |
526 | SmmMemoryMapConsistencyCheckRange (\r | |
527 | IN EFI_PHYSICAL_ADDRESS Base,\r | |
528 | IN UINTN Length\r | |
529 | )\r | |
530 | {\r | |
531 | LIST_ENTRY *Link;\r | |
532 | MEMORY_MAP *Entry;\r | |
533 | BOOLEAN Result;\r | |
534 | \r | |
535 | Result = FALSE;\r | |
536 | Link = gMemoryMap.ForwardLink;\r | |
537 | while (Link != &gMemoryMap) {\r | |
538 | Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
539 | Link = Link->ForwardLink;\r | |
540 | \r | |
541 | if (Entry->Type != EfiConventionalMemory) {\r | |
542 | continue;\r | |
543 | }\r | |
544 | if (Entry->Start == Base && Entry->End == Base + Length - 1) {\r | |
545 | Result = TRUE;\r | |
546 | break;\r | |
547 | }\r | |
548 | }\r | |
549 | \r | |
550 | return Result;\r | |
551 | }\r | |
552 | \r | |
553 | /**\r | |
554 | Check the consistency of Smm memory map.\r | |
555 | **/\r | |
556 | VOID\r | |
557 | SmmMemoryMapConsistencyCheck (\r | |
558 | VOID\r | |
559 | )\r | |
560 | {\r | |
561 | LIST_ENTRY *Node;\r | |
562 | FREE_PAGE_LIST *Pages;\r | |
563 | BOOLEAN Result;\r | |
564 | \r | |
565 | Pages = NULL;\r | |
566 | Node = mSmmMemoryMap.ForwardLink;\r | |
567 | while (Node != &mSmmMemoryMap) {\r | |
568 | Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);\r | |
569 | Result = SmmMemoryMapConsistencyCheckRange ((EFI_PHYSICAL_ADDRESS)(UINTN)Pages, (UINTN)EFI_PAGES_TO_SIZE(Pages->NumberOfPages));\r | |
570 | ASSERT (Result);\r | |
571 | Node = Node->ForwardLink;\r | |
572 | }\r | |
573 | }\r | |
574 | \r | |
e42e9404 | 575 | /**\r |
576 | Internal Function. Allocate n pages from given free page node.\r | |
577 | \r | |
578 | @param Pages The free page node.\r | |
579 | @param NumberOfPages Number of pages to be allocated.\r | |
580 | @param MaxAddress Request to allocate memory below this address.\r | |
581 | \r | |
582 | @return Memory address of allocated pages.\r | |
583 | \r | |
584 | **/\r | |
585 | UINTN\r | |
586 | InternalAllocPagesOnOneNode (\r | |
587 | IN OUT FREE_PAGE_LIST *Pages,\r | |
588 | IN UINTN NumberOfPages,\r | |
589 | IN UINTN MaxAddress\r | |
590 | )\r | |
591 | {\r | |
592 | UINTN Top;\r | |
593 | UINTN Bottom;\r | |
594 | FREE_PAGE_LIST *Node;\r | |
595 | \r | |
596 | Top = TRUNCATE_TO_PAGES (MaxAddress + 1 - (UINTN)Pages);\r | |
597 | if (Top > Pages->NumberOfPages) {\r | |
598 | Top = Pages->NumberOfPages;\r | |
599 | }\r | |
600 | Bottom = Top - NumberOfPages;\r | |
601 | \r | |
602 | if (Top < Pages->NumberOfPages) {\r | |
603 | Node = (FREE_PAGE_LIST*)((UINTN)Pages + EFI_PAGES_TO_SIZE (Top));\r | |
604 | Node->NumberOfPages = Pages->NumberOfPages - Top;\r | |
605 | InsertHeadList (&Pages->Link, &Node->Link);\r | |
606 | }\r | |
607 | \r | |
608 | if (Bottom > 0) {\r | |
609 | Pages->NumberOfPages = Bottom;\r | |
610 | } else {\r | |
611 | RemoveEntryList (&Pages->Link);\r | |
612 | }\r | |
613 | \r | |
614 | return (UINTN)Pages + EFI_PAGES_TO_SIZE (Bottom);\r | |
615 | }\r | |
616 | \r | |
617 | /**\r | |
618 | Internal Function. Allocate n pages from free page list below MaxAddress.\r | |
619 | \r | |
620 | @param FreePageList The free page node.\r | |
621 | @param NumberOfPages Number of pages to be allocated.\r | |
622 | @param MaxAddress Request to allocate memory below this address.\r | |
623 | \r | |
624 | @return Memory address of allocated pages.\r | |
625 | \r | |
626 | **/\r | |
627 | UINTN\r | |
628 | InternalAllocMaxAddress (\r | |
629 | IN OUT LIST_ENTRY *FreePageList,\r | |
630 | IN UINTN NumberOfPages,\r | |
631 | IN UINTN MaxAddress\r | |
632 | )\r | |
633 | {\r | |
634 | LIST_ENTRY *Node;\r | |
635 | FREE_PAGE_LIST *Pages;\r | |
636 | \r | |
637 | for (Node = FreePageList->BackLink; Node != FreePageList; Node = Node->BackLink) {\r | |
638 | Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);\r | |
639 | if (Pages->NumberOfPages >= NumberOfPages &&\r | |
640 | (UINTN)Pages + EFI_PAGES_TO_SIZE (NumberOfPages) - 1 <= MaxAddress) {\r | |
641 | return InternalAllocPagesOnOneNode (Pages, NumberOfPages, MaxAddress);\r | |
642 | }\r | |
643 | }\r | |
644 | return (UINTN)(-1);\r | |
645 | }\r | |
646 | \r | |
647 | /**\r | |
648 | Internal Function. Allocate n pages from free page list at given address.\r | |
649 | \r | |
650 | @param FreePageList The free page node.\r | |
651 | @param NumberOfPages Number of pages to be allocated.\r | |
652 | @param MaxAddress Request to allocate memory below this address.\r | |
653 | \r | |
654 | @return Memory address of allocated pages.\r | |
655 | \r | |
656 | **/\r | |
657 | UINTN\r | |
658 | InternalAllocAddress (\r | |
659 | IN OUT LIST_ENTRY *FreePageList,\r | |
660 | IN UINTN NumberOfPages,\r | |
661 | IN UINTN Address\r | |
662 | )\r | |
663 | {\r | |
664 | UINTN EndAddress;\r | |
665 | LIST_ENTRY *Node;\r | |
666 | FREE_PAGE_LIST *Pages;\r | |
667 | \r | |
668 | if ((Address & EFI_PAGE_MASK) != 0) {\r | |
669 | return ~Address;\r | |
670 | }\r | |
671 | \r | |
672 | EndAddress = Address + EFI_PAGES_TO_SIZE (NumberOfPages);\r | |
673 | for (Node = FreePageList->BackLink; Node!= FreePageList; Node = Node->BackLink) {\r | |
674 | Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);\r | |
675 | if ((UINTN)Pages <= Address) {\r | |
676 | if ((UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages) < EndAddress) {\r | |
677 | break;\r | |
678 | }\r | |
679 | return InternalAllocPagesOnOneNode (Pages, NumberOfPages, EndAddress);\r | |
680 | }\r | |
681 | }\r | |
682 | return ~Address;\r | |
683 | }\r | |
684 | \r | |
685 | /**\r | |
686 | Allocates pages from the memory map.\r | |
687 | \r | |
285a682c JY |
688 | @param[in] Type The type of allocation to perform.\r |
689 | @param[in] MemoryType The type of memory to turn the allocated pages\r | |
690 | into.\r | |
691 | @param[in] NumberOfPages The number of pages to allocate.\r | |
692 | @param[out] Memory A pointer to receive the base allocated memory\r | |
693 | address.\r | |
694 | @param[in] AddRegion If this memory is new added region.\r | |
e63da9f0 JW |
695 | @param[in] NeedGuard Flag to indicate Guard page is needed\r |
696 | or not\r | |
e42e9404 | 697 | \r |
698 | @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.\r | |
699 | @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r | |
700 | @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r | |
701 | @retval EFI_SUCCESS Pages successfully allocated.\r | |
702 | \r | |
703 | **/\r | |
704 | EFI_STATUS\r | |
285a682c | 705 | SmmInternalAllocatePagesEx (\r |
e42e9404 | 706 | IN EFI_ALLOCATE_TYPE Type,\r |
707 | IN EFI_MEMORY_TYPE MemoryType,\r | |
708 | IN UINTN NumberOfPages,\r | |
285a682c | 709 | OUT EFI_PHYSICAL_ADDRESS *Memory,\r |
e63da9f0 JW |
710 | IN BOOLEAN AddRegion,\r |
711 | IN BOOLEAN NeedGuard\r | |
e42e9404 | 712 | )\r |
713 | {\r | |
714 | UINTN RequestedAddress;\r | |
715 | \r | |
716 | if (MemoryType != EfiRuntimeServicesCode &&\r | |
717 | MemoryType != EfiRuntimeServicesData) {\r | |
718 | return EFI_INVALID_PARAMETER;\r | |
719 | }\r | |
720 | \r | |
721 | if (NumberOfPages > TRUNCATE_TO_PAGES ((UINTN)-1) + 1) {\r | |
722 | return EFI_OUT_OF_RESOURCES;\r | |
723 | }\r | |
724 | \r | |
725 | //\r | |
726 | // We don't track memory type in SMM\r | |
727 | //\r | |
728 | RequestedAddress = (UINTN)*Memory;\r | |
729 | switch (Type) {\r | |
730 | case AllocateAnyPages:\r | |
731 | RequestedAddress = (UINTN)(-1);\r | |
732 | case AllocateMaxAddress:\r | |
e63da9f0 JW |
733 | if (NeedGuard) {\r |
734 | *Memory = InternalAllocMaxAddressWithGuard (\r | |
735 | &mSmmMemoryMap,\r | |
736 | NumberOfPages,\r | |
737 | RequestedAddress,\r | |
738 | MemoryType\r | |
739 | );\r | |
740 | if (*Memory == (UINTN)-1) {\r | |
741 | return EFI_OUT_OF_RESOURCES;\r | |
742 | } else {\r | |
743 | ASSERT (VerifyMemoryGuard (*Memory, NumberOfPages) == TRUE);\r | |
744 | return EFI_SUCCESS;\r | |
745 | }\r | |
746 | }\r | |
747 | \r | |
e42e9404 | 748 | *Memory = InternalAllocMaxAddress (\r |
749 | &mSmmMemoryMap,\r | |
750 | NumberOfPages,\r | |
751 | RequestedAddress\r | |
752 | );\r | |
753 | if (*Memory == (UINTN)-1) {\r | |
754 | return EFI_OUT_OF_RESOURCES;\r | |
285a682c | 755 | }\r |
e42e9404 | 756 | break;\r |
757 | case AllocateAddress:\r | |
758 | *Memory = InternalAllocAddress (\r | |
759 | &mSmmMemoryMap,\r | |
760 | NumberOfPages,\r | |
761 | RequestedAddress\r | |
762 | );\r | |
763 | if (*Memory != RequestedAddress) {\r | |
764 | return EFI_NOT_FOUND;\r | |
765 | }\r | |
766 | break;\r | |
767 | default:\r | |
768 | return EFI_INVALID_PARAMETER;\r | |
769 | }\r | |
285a682c JY |
770 | \r |
771 | //\r | |
772 | // Update SmmMemoryMap here.\r | |
773 | //\r | |
774 | ConvertSmmMemoryMapEntry (MemoryType, *Memory, NumberOfPages, AddRegion);\r | |
775 | if (!AddRegion) {\r | |
776 | CoreFreeMemoryMapStack();\r | |
777 | }\r | |
778 | \r | |
e42e9404 | 779 | return EFI_SUCCESS;\r |
780 | }\r | |
781 | \r | |
285a682c JY |
782 | /**\r |
783 | Allocates pages from the memory map.\r | |
784 | \r | |
785 | @param[in] Type The type of allocation to perform.\r | |
786 | @param[in] MemoryType The type of memory to turn the allocated pages\r | |
787 | into.\r | |
788 | @param[in] NumberOfPages The number of pages to allocate.\r | |
789 | @param[out] Memory A pointer to receive the base allocated memory\r | |
790 | address.\r | |
e63da9f0 JW |
791 | @param[in] NeedGuard Flag to indicate Guard page is needed\r |
792 | or not\r | |
285a682c JY |
793 | \r |
794 | @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.\r | |
795 | @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r | |
796 | @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r | |
797 | @retval EFI_SUCCESS Pages successfully allocated.\r | |
798 | \r | |
799 | **/\r | |
800 | EFI_STATUS\r | |
801 | EFIAPI\r | |
802 | SmmInternalAllocatePages (\r | |
803 | IN EFI_ALLOCATE_TYPE Type,\r | |
804 | IN EFI_MEMORY_TYPE MemoryType,\r | |
805 | IN UINTN NumberOfPages,\r | |
e63da9f0 JW |
806 | OUT EFI_PHYSICAL_ADDRESS *Memory,\r |
807 | IN BOOLEAN NeedGuard\r | |
285a682c JY |
808 | )\r |
809 | {\r | |
e63da9f0 JW |
810 | return SmmInternalAllocatePagesEx (Type, MemoryType, NumberOfPages, Memory,\r |
811 | FALSE, NeedGuard);\r | |
285a682c JY |
812 | }\r |
813 | \r | |
84edd20b SZ |
814 | /**\r |
815 | Allocates pages from the memory map.\r | |
816 | \r | |
817 | @param Type The type of allocation to perform.\r | |
818 | @param MemoryType The type of memory to turn the allocated pages\r | |
819 | into.\r | |
820 | @param NumberOfPages The number of pages to allocate.\r | |
821 | @param Memory A pointer to receive the base allocated memory\r | |
822 | address.\r | |
823 | \r | |
824 | @retval EFI_INVALID_PARAMETER Parameters violate checking rules defined in spec.\r | |
825 | @retval EFI_NOT_FOUND Could not allocate pages match the requirement.\r | |
826 | @retval EFI_OUT_OF_RESOURCES No enough pages to allocate.\r | |
827 | @retval EFI_SUCCESS Pages successfully allocated.\r | |
828 | \r | |
829 | **/\r | |
830 | EFI_STATUS\r | |
831 | EFIAPI\r | |
832 | SmmAllocatePages (\r | |
833 | IN EFI_ALLOCATE_TYPE Type,\r | |
834 | IN EFI_MEMORY_TYPE MemoryType,\r | |
835 | IN UINTN NumberOfPages,\r | |
836 | OUT EFI_PHYSICAL_ADDRESS *Memory\r | |
837 | )\r | |
838 | {\r | |
839 | EFI_STATUS Status;\r | |
e63da9f0 | 840 | BOOLEAN NeedGuard;\r |
84edd20b | 841 | \r |
e63da9f0 JW |
842 | NeedGuard = IsPageTypeToGuard (MemoryType, Type);\r |
843 | Status = SmmInternalAllocatePages (Type, MemoryType, NumberOfPages, Memory,\r | |
844 | NeedGuard);\r | |
84edd20b | 845 | if (!EFI_ERROR (Status)) {\r |
e524f680 SZ |
846 | SmmCoreUpdateProfile (\r |
847 | (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r | |
848 | MemoryProfileActionAllocatePages,\r | |
849 | MemoryType,\r | |
850 | EFI_PAGES_TO_SIZE (NumberOfPages),\r | |
851 | (VOID *) (UINTN) *Memory,\r | |
852 | NULL\r | |
853 | );\r | |
84edd20b SZ |
854 | }\r |
855 | return Status;\r | |
856 | }\r | |
857 | \r | |
e42e9404 | 858 | /**\r |
859 | Internal Function. Merge two adjacent nodes.\r | |
860 | \r | |
861 | @param First The first of two nodes to merge.\r | |
862 | \r | |
863 | @return Pointer to node after merge (if success) or pointer to next node (if fail).\r | |
864 | \r | |
865 | **/\r | |
866 | FREE_PAGE_LIST *\r | |
867 | InternalMergeNodes (\r | |
868 | IN FREE_PAGE_LIST *First\r | |
869 | )\r | |
870 | {\r | |
871 | FREE_PAGE_LIST *Next;\r | |
872 | \r | |
873 | Next = BASE_CR (First->Link.ForwardLink, FREE_PAGE_LIST, Link);\r | |
874 | ASSERT (\r | |
875 | TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) >= First->NumberOfPages);\r | |
876 | \r | |
877 | if (TRUNCATE_TO_PAGES ((UINTN)Next - (UINTN)First) == First->NumberOfPages) {\r | |
878 | First->NumberOfPages += Next->NumberOfPages;\r | |
879 | RemoveEntryList (&Next->Link);\r | |
880 | Next = First;\r | |
881 | }\r | |
882 | return Next;\r | |
883 | }\r | |
884 | \r | |
885 | /**\r | |
886 | Frees previous allocated pages.\r | |
887 | \r | |
285a682c JY |
888 | @param[in] Memory Base address of memory being freed.\r |
889 | @param[in] NumberOfPages The number of pages to free.\r | |
890 | @param[in] AddRegion If this memory is new added region.\r | |
e42e9404 | 891 | \r |
892 | @retval EFI_NOT_FOUND Could not find the entry that covers the range.\r | |
ddfae264 | 893 | @retval EFI_INVALID_PARAMETER Address not aligned, Address is zero or NumberOfPages is zero.\r |
e42e9404 | 894 | @return EFI_SUCCESS Pages successfully freed.\r |
895 | \r | |
896 | **/\r | |
897 | EFI_STATUS\r | |
285a682c | 898 | SmmInternalFreePagesEx (\r |
e42e9404 | 899 | IN EFI_PHYSICAL_ADDRESS Memory,\r |
285a682c JY |
900 | IN UINTN NumberOfPages,\r |
901 | IN BOOLEAN AddRegion\r | |
e42e9404 | 902 | )\r |
903 | {\r | |
904 | LIST_ENTRY *Node;\r | |
905 | FREE_PAGE_LIST *Pages;\r | |
906 | \r | |
ddfae264 | 907 | if (((Memory & EFI_PAGE_MASK) != 0) || (Memory == 0) || (NumberOfPages == 0)) {\r |
e42e9404 | 908 | return EFI_INVALID_PARAMETER;\r |
909 | }\r | |
910 | \r | |
911 | Pages = NULL;\r | |
912 | Node = mSmmMemoryMap.ForwardLink;\r | |
913 | while (Node != &mSmmMemoryMap) {\r | |
914 | Pages = BASE_CR (Node, FREE_PAGE_LIST, Link);\r | |
915 | if (Memory < (UINTN)Pages) {\r | |
916 | break;\r | |
917 | }\r | |
918 | Node = Node->ForwardLink;\r | |
919 | }\r | |
920 | \r | |
921 | if (Node != &mSmmMemoryMap &&\r | |
922 | Memory + EFI_PAGES_TO_SIZE (NumberOfPages) > (UINTN)Pages) {\r | |
923 | return EFI_INVALID_PARAMETER;\r | |
924 | }\r | |
925 | \r | |
926 | if (Node->BackLink != &mSmmMemoryMap) {\r | |
927 | Pages = BASE_CR (Node->BackLink, FREE_PAGE_LIST, Link);\r | |
928 | if ((UINTN)Pages + EFI_PAGES_TO_SIZE (Pages->NumberOfPages) > Memory) {\r | |
929 | return EFI_INVALID_PARAMETER;\r | |
930 | }\r | |
931 | }\r | |
932 | \r | |
933 | Pages = (FREE_PAGE_LIST*)(UINTN)Memory;\r | |
934 | Pages->NumberOfPages = NumberOfPages;\r | |
935 | InsertTailList (Node, &Pages->Link);\r | |
936 | \r | |
937 | if (Pages->Link.BackLink != &mSmmMemoryMap) {\r | |
938 | Pages = InternalMergeNodes (\r | |
939 | BASE_CR (Pages->Link.BackLink, FREE_PAGE_LIST, Link)\r | |
940 | );\r | |
941 | }\r | |
942 | \r | |
943 | if (Node != &mSmmMemoryMap) {\r | |
944 | InternalMergeNodes (Pages);\r | |
945 | }\r | |
946 | \r | |
285a682c JY |
947 | //\r |
948 | // Update SmmMemoryMap here.\r | |
949 | //\r | |
950 | ConvertSmmMemoryMapEntry (EfiConventionalMemory, Memory, NumberOfPages, AddRegion);\r | |
951 | if (!AddRegion) {\r | |
952 | CoreFreeMemoryMapStack();\r | |
953 | }\r | |
954 | \r | |
e42e9404 | 955 | return EFI_SUCCESS;\r |
956 | }\r | |
957 | \r | |
285a682c JY |
958 | /**\r |
959 | Frees previous allocated pages.\r | |
960 | \r | |
961 | @param[in] Memory Base address of memory being freed.\r | |
962 | @param[in] NumberOfPages The number of pages to free.\r | |
e63da9f0 | 963 | @param[in] IsGuarded Is the memory to free guarded or not.\r |
285a682c JY |
964 | \r |
965 | @retval EFI_NOT_FOUND Could not find the entry that covers the range.\r | |
ddfae264 | 966 | @retval EFI_INVALID_PARAMETER Address not aligned, Address is zero or NumberOfPages is zero.\r |
285a682c JY |
967 | @return EFI_SUCCESS Pages successfully freed.\r |
968 | \r | |
969 | **/\r | |
970 | EFI_STATUS\r | |
971 | EFIAPI\r | |
972 | SmmInternalFreePages (\r | |
973 | IN EFI_PHYSICAL_ADDRESS Memory,\r | |
e63da9f0 JW |
974 | IN UINTN NumberOfPages,\r |
975 | IN BOOLEAN IsGuarded\r | |
285a682c JY |
976 | )\r |
977 | {\r | |
e63da9f0 JW |
978 | if (IsGuarded) {\r |
979 | return SmmInternalFreePagesExWithGuard (Memory, NumberOfPages, FALSE);\r | |
980 | }\r | |
285a682c JY |
981 | return SmmInternalFreePagesEx (Memory, NumberOfPages, FALSE);\r |
982 | }\r | |
983 | \r | |
84edd20b SZ |
984 | /**\r |
985 | Frees previous allocated pages.\r | |
986 | \r | |
987 | @param Memory Base address of memory being freed.\r | |
988 | @param NumberOfPages The number of pages to free.\r | |
989 | \r | |
990 | @retval EFI_NOT_FOUND Could not find the entry that covers the range.\r | |
ddfae264 | 991 | @retval EFI_INVALID_PARAMETER Address not aligned, Address is zero or NumberOfPages is zero.\r |
84edd20b SZ |
992 | @return EFI_SUCCESS Pages successfully freed.\r |
993 | \r | |
994 | **/\r | |
995 | EFI_STATUS\r | |
996 | EFIAPI\r | |
997 | SmmFreePages (\r | |
998 | IN EFI_PHYSICAL_ADDRESS Memory,\r | |
999 | IN UINTN NumberOfPages\r | |
1000 | )\r | |
1001 | {\r | |
1002 | EFI_STATUS Status;\r | |
e63da9f0 | 1003 | BOOLEAN IsGuarded;\r |
84edd20b | 1004 | \r |
e63da9f0 JW |
1005 | IsGuarded = IsHeapGuardEnabled () && IsMemoryGuarded (Memory);\r |
1006 | Status = SmmInternalFreePages (Memory, NumberOfPages, IsGuarded);\r | |
84edd20b | 1007 | if (!EFI_ERROR (Status)) {\r |
e524f680 SZ |
1008 | SmmCoreUpdateProfile (\r |
1009 | (EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0),\r | |
1010 | MemoryProfileActionFreePages,\r | |
1011 | EfiMaxMemoryType,\r | |
1012 | EFI_PAGES_TO_SIZE (NumberOfPages),\r | |
1013 | (VOID *) (UINTN) Memory,\r | |
1014 | NULL\r | |
1015 | );\r | |
84edd20b SZ |
1016 | }\r |
1017 | return Status;\r | |
1018 | }\r | |
1019 | \r | |
e42e9404 | 1020 | /**\r |
1021 | Add free SMRAM region for use by memory service.\r | |
1022 | \r | |
1023 | @param MemBase Base address of memory region.\r | |
1024 | @param MemLength Length of the memory region.\r | |
1025 | @param Type Memory type.\r | |
1026 | @param Attributes Memory region state.\r | |
1027 | \r | |
1028 | **/\r | |
1029 | VOID\r | |
1030 | SmmAddMemoryRegion (\r | |
1031 | IN EFI_PHYSICAL_ADDRESS MemBase,\r | |
1032 | IN UINT64 MemLength,\r | |
1033 | IN EFI_MEMORY_TYPE Type,\r | |
1034 | IN UINT64 Attributes\r | |
1035 | )\r | |
1036 | {\r | |
1037 | UINTN AlignedMemBase;\r | |
1038 | \r | |
3df4b6e7 | 1039 | //\r |
285a682c | 1040 | // Add EfiRuntimeServicesData for memory regions that is already allocated, needs testing, or needs ECC initialization\r |
3df4b6e7 | 1041 | //\r |
1042 | if ((Attributes & (EFI_ALLOCATED | EFI_NEEDS_TESTING | EFI_NEEDS_ECC_INITIALIZATION)) != 0) {\r | |
285a682c JY |
1043 | Type = EfiRuntimeServicesData;\r |
1044 | } else {\r | |
1045 | Type = EfiConventionalMemory;\r | |
3df4b6e7 | 1046 | }\r |
285a682c JY |
1047 | \r |
1048 | DEBUG ((DEBUG_INFO, "SmmAddMemoryRegion\n"));\r | |
1049 | DEBUG ((DEBUG_INFO, " MemBase - 0x%lx\n", MemBase));\r | |
1050 | DEBUG ((DEBUG_INFO, " MemLength - 0x%lx\n", MemLength));\r | |
1051 | DEBUG ((DEBUG_INFO, " Type - 0x%x\n", Type));\r | |
1052 | DEBUG ((DEBUG_INFO, " Attributes - 0x%lx\n", Attributes));\r | |
1053 | \r | |
3df4b6e7 | 1054 | //\r |
1055 | // Align range on an EFI_PAGE_SIZE boundary\r | |
285a682c | 1056 | //\r |
e42e9404 | 1057 | AlignedMemBase = (UINTN)(MemBase + EFI_PAGE_MASK) & ~EFI_PAGE_MASK;\r |
1058 | MemLength -= AlignedMemBase - MemBase;\r | |
285a682c JY |
1059 | if (Type == EfiConventionalMemory) {\r |
1060 | SmmInternalFreePagesEx (AlignedMemBase, TRUNCATE_TO_PAGES ((UINTN)MemLength), TRUE);\r | |
1061 | } else {\r | |
1062 | ConvertSmmMemoryMapEntry (EfiRuntimeServicesData, AlignedMemBase, TRUNCATE_TO_PAGES ((UINTN)MemLength), TRUE);\r | |
1063 | }\r | |
1064 | \r | |
1065 | CoreFreeMemoryMapStack ();\r | |
1066 | }\r | |
1067 | \r | |
1068 | /**\r | |
1069 | This function returns a copy of the current memory map. The map is an array of\r | |
1070 | memory descriptors, each of which describes a contiguous block of memory.\r | |
1071 | \r | |
1072 | @param[in, out] MemoryMapSize A pointer to the size, in bytes, of the\r | |
1073 | MemoryMap buffer. On input, this is the size of\r | |
1074 | the buffer allocated by the caller. On output,\r | |
1075 | it is the size of the buffer returned by the\r | |
1076 | firmware if the buffer was large enough, or the\r | |
1077 | size of the buffer needed to contain the map if\r | |
1078 | the buffer was too small.\r | |
1079 | @param[in, out] MemoryMap A pointer to the buffer in which firmware places\r | |
1080 | the current memory map.\r | |
1081 | @param[out] MapKey A pointer to the location in which firmware\r | |
1082 | returns the key for the current memory map.\r | |
1083 | @param[out] DescriptorSize A pointer to the location in which firmware\r | |
1084 | returns the size, in bytes, of an individual\r | |
1085 | EFI_MEMORY_DESCRIPTOR.\r | |
1086 | @param[out] DescriptorVersion A pointer to the location in which firmware\r | |
1087 | returns the version number associated with the\r | |
1088 | EFI_MEMORY_DESCRIPTOR.\r | |
1089 | \r | |
1090 | @retval EFI_SUCCESS The memory map was returned in the MemoryMap\r | |
1091 | buffer.\r | |
1092 | @retval EFI_BUFFER_TOO_SMALL The MemoryMap buffer was too small. The current\r | |
1093 | buffer size needed to hold the memory map is\r | |
1094 | returned in MemoryMapSize.\r | |
1095 | @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r | |
1096 | \r | |
1097 | **/\r | |
1098 | EFI_STATUS\r | |
1099 | EFIAPI\r | |
1100 | SmmCoreGetMemoryMap (\r | |
1101 | IN OUT UINTN *MemoryMapSize,\r | |
1102 | IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r | |
1103 | OUT UINTN *MapKey,\r | |
1104 | OUT UINTN *DescriptorSize,\r | |
1105 | OUT UINT32 *DescriptorVersion\r | |
1106 | )\r | |
1107 | {\r | |
1108 | UINTN Count;\r | |
1109 | LIST_ENTRY *Link;\r | |
1110 | MEMORY_MAP *Entry;\r | |
1111 | UINTN Size;\r | |
1112 | UINTN BufferSize;\r | |
1113 | \r | |
1114 | Size = sizeof (EFI_MEMORY_DESCRIPTOR);\r | |
1115 | \r | |
1116 | //\r | |
1117 | // Make sure Size != sizeof(EFI_MEMORY_DESCRIPTOR). This will\r | |
1118 | // prevent people from having pointer math bugs in their code.\r | |
1119 | // now you have to use *DescriptorSize to make things work.\r | |
1120 | //\r | |
1121 | Size += sizeof(UINT64) - (Size % sizeof (UINT64));\r | |
1122 | \r | |
1123 | if (DescriptorSize != NULL) {\r | |
1124 | *DescriptorSize = Size;\r | |
1125 | }\r | |
1126 | \r | |
1127 | if (DescriptorVersion != NULL) {\r | |
1128 | *DescriptorVersion = EFI_MEMORY_DESCRIPTOR_VERSION;\r | |
1129 | }\r | |
1130 | \r | |
1131 | Count = GetSmmMemoryMapEntryCount ();\r | |
1132 | BufferSize = Size * Count;\r | |
1133 | if (*MemoryMapSize < BufferSize) {\r | |
1134 | *MemoryMapSize = BufferSize;\r | |
1135 | return EFI_BUFFER_TOO_SMALL;\r | |
1136 | }\r | |
1137 | \r | |
1138 | *MemoryMapSize = BufferSize;\r | |
1139 | if (MemoryMap == NULL) {\r | |
1140 | return EFI_INVALID_PARAMETER;\r | |
1141 | }\r | |
1142 | \r | |
1143 | ZeroMem (MemoryMap, BufferSize);\r | |
1144 | Link = gMemoryMap.ForwardLink;\r | |
1145 | while (Link != &gMemoryMap) {\r | |
1146 | Entry = CR (Link, MEMORY_MAP, Link, MEMORY_MAP_SIGNATURE);\r | |
1147 | Link = Link->ForwardLink;\r | |
1148 | \r | |
1149 | MemoryMap->Type = Entry->Type;\r | |
1150 | MemoryMap->PhysicalStart = Entry->Start;\r | |
1151 | MemoryMap->NumberOfPages = RShiftU64 (Entry->End - Entry->Start + 1, EFI_PAGE_SHIFT);\r | |
1152 | \r | |
1153 | MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, Size);\r | |
1154 | }\r | |
1155 | \r | |
1156 | return EFI_SUCCESS;\r | |
e42e9404 | 1157 | }\r |