]>
Commit | Line | Data |
---|---|---|
717fb604 JY |
1 | /** @file\r |
2 | \r | |
714c2603 | 3 | Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r |
717fb604 JY |
4 | This program and the accompanying materials\r |
5 | are licensed and made available under the terms and conditions of the BSD License\r | |
6 | which accompanies this distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | **/\r | |
13 | \r | |
14 | #include "PiSmmCpuDxeSmm.h"\r | |
15 | \r | |
16 | #define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \\r | |
17 | ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))\r | |
18 | \r | |
d2fc7711 JY |
19 | #define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \\r |
20 | ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size)))\r | |
21 | \r | |
22 | EFI_MEMORY_DESCRIPTOR *mUefiMemoryMap;\r | |
23 | UINTN mUefiMemoryMapSize;\r | |
24 | UINTN mUefiDescriptorSize;\r | |
25 | \r | |
717fb604 JY |
26 | PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] = {\r |
27 | {Page4K, SIZE_4KB, PAGING_4K_ADDRESS_MASK_64},\r | |
28 | {Page2M, SIZE_2MB, PAGING_2M_ADDRESS_MASK_64},\r | |
29 | {Page1G, SIZE_1GB, PAGING_1G_ADDRESS_MASK_64},\r | |
30 | };\r | |
31 | \r | |
32 | /**\r | |
33 | Return page table base.\r | |
34 | \r | |
35 | @return page table base.\r | |
36 | **/\r | |
37 | UINTN\r | |
38 | GetPageTableBase (\r | |
39 | VOID\r | |
40 | )\r | |
41 | {\r | |
42 | return (AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64);\r | |
43 | }\r | |
44 | \r | |
45 | /**\r | |
46 | Return length according to page attributes.\r | |
47 | \r | |
48 | @param[in] PageAttributes The page attribute of the page entry.\r | |
49 | \r | |
50 | @return The length of page entry.\r | |
51 | **/\r | |
52 | UINTN\r | |
53 | PageAttributeToLength (\r | |
54 | IN PAGE_ATTRIBUTE PageAttribute\r | |
55 | )\r | |
56 | {\r | |
57 | UINTN Index;\r | |
58 | for (Index = 0; Index < sizeof(mPageAttributeTable)/sizeof(mPageAttributeTable[0]); Index++) {\r | |
59 | if (PageAttribute == mPageAttributeTable[Index].Attribute) {\r | |
60 | return (UINTN)mPageAttributeTable[Index].Length;\r | |
61 | }\r | |
62 | }\r | |
63 | return 0;\r | |
64 | }\r | |
65 | \r | |
66 | /**\r | |
67 | Return address mask according to page attributes.\r | |
68 | \r | |
69 | @param[in] PageAttributes The page attribute of the page entry.\r | |
70 | \r | |
71 | @return The address mask of page entry.\r | |
72 | **/\r | |
73 | UINTN\r | |
74 | PageAttributeToMask (\r | |
75 | IN PAGE_ATTRIBUTE PageAttribute\r | |
76 | )\r | |
77 | {\r | |
78 | UINTN Index;\r | |
79 | for (Index = 0; Index < sizeof(mPageAttributeTable)/sizeof(mPageAttributeTable[0]); Index++) {\r | |
80 | if (PageAttribute == mPageAttributeTable[Index].Attribute) {\r | |
81 | return (UINTN)mPageAttributeTable[Index].AddressMask;\r | |
82 | }\r | |
83 | }\r | |
84 | return 0;\r | |
85 | }\r | |
86 | \r | |
87 | /**\r | |
88 | Return page table entry to match the address.\r | |
89 | \r | |
90 | @param[in] Address The address to be checked.\r | |
91 | @param[out] PageAttributes The page attribute of the page entry.\r | |
92 | \r | |
93 | @return The page entry.\r | |
94 | **/\r | |
95 | VOID *\r | |
96 | GetPageTableEntry (\r | |
97 | IN PHYSICAL_ADDRESS Address,\r | |
98 | OUT PAGE_ATTRIBUTE *PageAttribute\r | |
99 | )\r | |
100 | {\r | |
101 | UINTN Index1;\r | |
102 | UINTN Index2;\r | |
103 | UINTN Index3;\r | |
104 | UINTN Index4;\r | |
105 | UINT64 *L1PageTable;\r | |
106 | UINT64 *L2PageTable;\r | |
107 | UINT64 *L3PageTable;\r | |
108 | UINT64 *L4PageTable;\r | |
109 | \r | |
110 | Index4 = ((UINTN)RShiftU64 (Address, 39)) & PAGING_PAE_INDEX_MASK;\r | |
111 | Index3 = ((UINTN)Address >> 30) & PAGING_PAE_INDEX_MASK;\r | |
112 | Index2 = ((UINTN)Address >> 21) & PAGING_PAE_INDEX_MASK;\r | |
113 | Index1 = ((UINTN)Address >> 12) & PAGING_PAE_INDEX_MASK;\r | |
114 | \r | |
115 | if (sizeof(UINTN) == sizeof(UINT64)) {\r | |
116 | L4PageTable = (UINT64 *)GetPageTableBase ();\r | |
117 | if (L4PageTable[Index4] == 0) {\r | |
118 | *PageAttribute = PageNone;\r | |
119 | return NULL;\r | |
120 | }\r | |
121 | \r | |
241f9149 | 122 | L3PageTable = (UINT64 *)(UINTN)(L4PageTable[Index4] & ~mAddressEncMask & PAGING_4K_ADDRESS_MASK_64);\r |
717fb604 JY |
123 | } else {\r |
124 | L3PageTable = (UINT64 *)GetPageTableBase ();\r | |
125 | }\r | |
126 | if (L3PageTable[Index3] == 0) {\r | |
127 | *PageAttribute = PageNone;\r | |
128 | return NULL;\r | |
129 | }\r | |
130 | if ((L3PageTable[Index3] & IA32_PG_PS) != 0) {\r | |
131 | // 1G\r | |
132 | *PageAttribute = Page1G;\r | |
133 | return &L3PageTable[Index3];\r | |
134 | }\r | |
135 | \r | |
241f9149 | 136 | L2PageTable = (UINT64 *)(UINTN)(L3PageTable[Index3] & ~mAddressEncMask & PAGING_4K_ADDRESS_MASK_64);\r |
717fb604 JY |
137 | if (L2PageTable[Index2] == 0) {\r |
138 | *PageAttribute = PageNone;\r | |
139 | return NULL;\r | |
140 | }\r | |
141 | if ((L2PageTable[Index2] & IA32_PG_PS) != 0) {\r | |
142 | // 2M\r | |
143 | *PageAttribute = Page2M;\r | |
144 | return &L2PageTable[Index2];\r | |
145 | }\r | |
146 | \r | |
147 | // 4k\r | |
241f9149 | 148 | L1PageTable = (UINT64 *)(UINTN)(L2PageTable[Index2] & ~mAddressEncMask & PAGING_4K_ADDRESS_MASK_64);\r |
717fb604 JY |
149 | if ((L1PageTable[Index1] == 0) && (Address != 0)) {\r |
150 | *PageAttribute = PageNone;\r | |
151 | return NULL;\r | |
152 | }\r | |
153 | *PageAttribute = Page4K;\r | |
154 | return &L1PageTable[Index1];\r | |
155 | }\r | |
156 | \r | |
157 | /**\r | |
158 | Return memory attributes of page entry.\r | |
159 | \r | |
160 | @param[in] PageEntry The page entry.\r | |
161 | \r | |
162 | @return Memory attributes of page entry.\r | |
163 | **/\r | |
164 | UINT64\r | |
165 | GetAttributesFromPageEntry (\r | |
166 | IN UINT64 *PageEntry\r | |
167 | )\r | |
168 | {\r | |
169 | UINT64 Attributes;\r | |
170 | Attributes = 0;\r | |
171 | if ((*PageEntry & IA32_PG_P) == 0) {\r | |
172 | Attributes |= EFI_MEMORY_RP;\r | |
173 | }\r | |
174 | if ((*PageEntry & IA32_PG_RW) == 0) {\r | |
175 | Attributes |= EFI_MEMORY_RO;\r | |
176 | }\r | |
177 | if ((*PageEntry & IA32_PG_NX) != 0) {\r | |
178 | Attributes |= EFI_MEMORY_XP;\r | |
179 | }\r | |
180 | return Attributes;\r | |
181 | }\r | |
182 | \r | |
183 | /**\r | |
184 | Modify memory attributes of page entry.\r | |
185 | \r | |
186 | @param[in] PageEntry The page entry.\r | |
187 | @param[in] Attributes The bit mask of attributes to modify for the memory region.\r | |
188 | @param[in] IsSet TRUE means to set attributes. FALSE means to clear attributes.\r | |
189 | @param[out] IsModified TRUE means page table modified. FALSE means page table not modified.\r | |
190 | **/\r | |
191 | VOID\r | |
192 | ConvertPageEntryAttribute (\r | |
193 | IN UINT64 *PageEntry,\r | |
194 | IN UINT64 Attributes,\r | |
195 | IN BOOLEAN IsSet,\r | |
196 | OUT BOOLEAN *IsModified\r | |
197 | )\r | |
198 | {\r | |
199 | UINT64 CurrentPageEntry;\r | |
200 | UINT64 NewPageEntry;\r | |
201 | \r | |
202 | CurrentPageEntry = *PageEntry;\r | |
203 | NewPageEntry = CurrentPageEntry;\r | |
204 | if ((Attributes & EFI_MEMORY_RP) != 0) {\r | |
205 | if (IsSet) {\r | |
206 | NewPageEntry &= ~(UINT64)IA32_PG_P;\r | |
207 | } else {\r | |
208 | NewPageEntry |= IA32_PG_P;\r | |
209 | }\r | |
210 | }\r | |
211 | if ((Attributes & EFI_MEMORY_RO) != 0) {\r | |
212 | if (IsSet) {\r | |
213 | NewPageEntry &= ~(UINT64)IA32_PG_RW;\r | |
214 | } else {\r | |
215 | NewPageEntry |= IA32_PG_RW;\r | |
216 | }\r | |
217 | }\r | |
218 | if ((Attributes & EFI_MEMORY_XP) != 0) {\r | |
750ec4ca JY |
219 | if (mXdSupported) {\r |
220 | if (IsSet) {\r | |
221 | NewPageEntry |= IA32_PG_NX;\r | |
222 | } else {\r | |
223 | NewPageEntry &= ~IA32_PG_NX;\r | |
224 | }\r | |
717fb604 JY |
225 | }\r |
226 | }\r | |
227 | *PageEntry = NewPageEntry;\r | |
228 | if (CurrentPageEntry != NewPageEntry) {\r | |
229 | *IsModified = TRUE;\r | |
230 | DEBUG ((DEBUG_VERBOSE, "ConvertPageEntryAttribute 0x%lx", CurrentPageEntry));\r | |
231 | DEBUG ((DEBUG_VERBOSE, "->0x%lx\n", NewPageEntry));\r | |
232 | } else {\r | |
233 | *IsModified = FALSE;\r | |
234 | }\r | |
235 | }\r | |
236 | \r | |
237 | /**\r | |
238 | This function returns if there is need to split page entry.\r | |
239 | \r | |
240 | @param[in] BaseAddress The base address to be checked.\r | |
241 | @param[in] Length The length to be checked.\r | |
242 | @param[in] PageEntry The page entry to be checked.\r | |
243 | @param[in] PageAttribute The page attribute of the page entry.\r | |
244 | \r | |
245 | @retval SplitAttributes on if there is need to split page entry.\r | |
246 | **/\r | |
247 | PAGE_ATTRIBUTE\r | |
248 | NeedSplitPage (\r | |
249 | IN PHYSICAL_ADDRESS BaseAddress,\r | |
250 | IN UINT64 Length,\r | |
251 | IN UINT64 *PageEntry,\r | |
252 | IN PAGE_ATTRIBUTE PageAttribute\r | |
253 | )\r | |
254 | {\r | |
255 | UINT64 PageEntryLength;\r | |
256 | \r | |
257 | PageEntryLength = PageAttributeToLength (PageAttribute);\r | |
258 | \r | |
259 | if (((BaseAddress & (PageEntryLength - 1)) == 0) && (Length >= PageEntryLength)) {\r | |
260 | return PageNone;\r | |
261 | }\r | |
262 | \r | |
263 | if (((BaseAddress & PAGING_2M_MASK) != 0) || (Length < SIZE_2MB)) {\r | |
264 | return Page4K;\r | |
265 | }\r | |
266 | \r | |
267 | return Page2M;\r | |
268 | }\r | |
269 | \r | |
270 | /**\r | |
271 | This function splits one page entry to small page entries.\r | |
272 | \r | |
273 | @param[in] PageEntry The page entry to be splitted.\r | |
274 | @param[in] PageAttribute The page attribute of the page entry.\r | |
275 | @param[in] SplitAttribute How to split the page entry.\r | |
276 | \r | |
277 | @retval RETURN_SUCCESS The page entry is splitted.\r | |
278 | @retval RETURN_UNSUPPORTED The page entry does not support to be splitted.\r | |
279 | @retval RETURN_OUT_OF_RESOURCES No resource to split page entry.\r | |
280 | **/\r | |
281 | RETURN_STATUS\r | |
282 | SplitPage (\r | |
283 | IN UINT64 *PageEntry,\r | |
284 | IN PAGE_ATTRIBUTE PageAttribute,\r | |
285 | IN PAGE_ATTRIBUTE SplitAttribute\r | |
286 | )\r | |
287 | {\r | |
288 | UINT64 BaseAddress;\r | |
289 | UINT64 *NewPageEntry;\r | |
290 | UINTN Index;\r | |
291 | \r | |
292 | ASSERT (PageAttribute == Page2M || PageAttribute == Page1G);\r | |
293 | \r | |
294 | if (PageAttribute == Page2M) {\r | |
295 | //\r | |
296 | // Split 2M to 4K\r | |
297 | //\r | |
298 | ASSERT (SplitAttribute == Page4K);\r | |
299 | if (SplitAttribute == Page4K) {\r | |
300 | NewPageEntry = AllocatePageTableMemory (1);\r | |
301 | DEBUG ((DEBUG_VERBOSE, "Split - 0x%x\n", NewPageEntry));\r | |
302 | if (NewPageEntry == NULL) {\r | |
303 | return RETURN_OUT_OF_RESOURCES;\r | |
304 | }\r | |
305 | BaseAddress = *PageEntry & PAGING_2M_ADDRESS_MASK_64;\r | |
306 | for (Index = 0; Index < SIZE_4KB / sizeof(UINT64); Index++) {\r | |
241f9149 | 307 | NewPageEntry[Index] = (BaseAddress + SIZE_4KB * Index) | mAddressEncMask | ((*PageEntry) & PAGE_PROGATE_BITS);\r |
717fb604 | 308 | }\r |
241f9149 | 309 | (*PageEntry) = (UINT64)(UINTN)NewPageEntry | mAddressEncMask | PAGE_ATTRIBUTE_BITS;\r |
717fb604 JY |
310 | return RETURN_SUCCESS;\r |
311 | } else {\r | |
312 | return RETURN_UNSUPPORTED;\r | |
313 | }\r | |
314 | } else if (PageAttribute == Page1G) {\r | |
315 | //\r | |
316 | // Split 1G to 2M\r | |
317 | // No need support 1G->4K directly, we should use 1G->2M, then 2M->4K to get more compact page table.\r | |
318 | //\r | |
319 | ASSERT (SplitAttribute == Page2M || SplitAttribute == Page4K);\r | |
320 | if ((SplitAttribute == Page2M || SplitAttribute == Page4K)) {\r | |
321 | NewPageEntry = AllocatePageTableMemory (1);\r | |
322 | DEBUG ((DEBUG_VERBOSE, "Split - 0x%x\n", NewPageEntry));\r | |
323 | if (NewPageEntry == NULL) {\r | |
324 | return RETURN_OUT_OF_RESOURCES;\r | |
325 | }\r | |
326 | BaseAddress = *PageEntry & PAGING_1G_ADDRESS_MASK_64;\r | |
327 | for (Index = 0; Index < SIZE_4KB / sizeof(UINT64); Index++) {\r | |
241f9149 | 328 | NewPageEntry[Index] = (BaseAddress + SIZE_2MB * Index) | mAddressEncMask | IA32_PG_PS | ((*PageEntry) & PAGE_PROGATE_BITS);\r |
717fb604 | 329 | }\r |
241f9149 | 330 | (*PageEntry) = (UINT64)(UINTN)NewPageEntry | mAddressEncMask | PAGE_ATTRIBUTE_BITS;\r |
717fb604 JY |
331 | return RETURN_SUCCESS;\r |
332 | } else {\r | |
333 | return RETURN_UNSUPPORTED;\r | |
334 | }\r | |
335 | } else {\r | |
336 | return RETURN_UNSUPPORTED;\r | |
337 | }\r | |
338 | }\r | |
339 | \r | |
340 | /**\r | |
341 | This function modifies the page attributes for the memory region specified by BaseAddress and\r | |
342 | Length from their current attributes to the attributes specified by Attributes.\r | |
343 | \r | |
344 | Caller should make sure BaseAddress and Length is at page boundary.\r | |
345 | \r | |
346 | @param[in] BaseAddress The physical address that is the start address of a memory region.\r | |
347 | @param[in] Length The size in bytes of the memory region.\r | |
348 | @param[in] Attributes The bit mask of attributes to modify for the memory region.\r | |
349 | @param[in] IsSet TRUE means to set attributes. FALSE means to clear attributes.\r | |
350 | @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.\r | |
351 | @param[out] IsModified TRUE means page table modified. FALSE means page table not modified.\r | |
352 | \r | |
353 | @retval RETURN_SUCCESS The attributes were modified for the memory region.\r | |
354 | @retval RETURN_ACCESS_DENIED The attributes for the memory resource range specified by\r | |
355 | BaseAddress and Length cannot be modified.\r | |
356 | @retval RETURN_INVALID_PARAMETER Length is zero.\r | |
357 | Attributes specified an illegal combination of attributes that\r | |
358 | cannot be set together.\r | |
359 | @retval RETURN_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r | |
360 | the memory resource range.\r | |
361 | @retval RETURN_UNSUPPORTED The processor does not support one or more bytes of the memory\r | |
362 | resource range specified by BaseAddress and Length.\r | |
363 | The bit mask of attributes is not support for the memory resource\r | |
364 | range specified by BaseAddress and Length.\r | |
365 | **/\r | |
366 | RETURN_STATUS\r | |
367 | EFIAPI\r | |
368 | ConvertMemoryPageAttributes (\r | |
369 | IN PHYSICAL_ADDRESS BaseAddress,\r | |
370 | IN UINT64 Length,\r | |
371 | IN UINT64 Attributes,\r | |
372 | IN BOOLEAN IsSet,\r | |
373 | OUT BOOLEAN *IsSplitted, OPTIONAL\r | |
374 | OUT BOOLEAN *IsModified OPTIONAL\r | |
375 | )\r | |
376 | {\r | |
377 | UINT64 *PageEntry;\r | |
378 | PAGE_ATTRIBUTE PageAttribute;\r | |
379 | UINTN PageEntryLength;\r | |
380 | PAGE_ATTRIBUTE SplitAttribute;\r | |
381 | RETURN_STATUS Status;\r | |
382 | BOOLEAN IsEntryModified;\r | |
714c2603 | 383 | EFI_PHYSICAL_ADDRESS MaximumSupportMemAddress;\r |
717fb604 JY |
384 | \r |
385 | ASSERT (Attributes != 0);\r | |
386 | ASSERT ((Attributes & ~(EFI_MEMORY_RP | EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0);\r | |
387 | \r | |
388 | ASSERT ((BaseAddress & (SIZE_4KB - 1)) == 0);\r | |
389 | ASSERT ((Length & (SIZE_4KB - 1)) == 0);\r | |
390 | \r | |
391 | if (Length == 0) {\r | |
392 | return RETURN_INVALID_PARAMETER;\r | |
393 | }\r | |
394 | \r | |
714c2603 SZ |
395 | MaximumSupportMemAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, mPhysicalAddressBits) - 1);\r |
396 | if (BaseAddress > MaximumSupportMemAddress) {\r | |
397 | return RETURN_UNSUPPORTED;\r | |
398 | }\r | |
399 | if (Length > MaximumSupportMemAddress) {\r | |
400 | return RETURN_UNSUPPORTED;\r | |
401 | }\r | |
402 | if ((Length != 0) && (BaseAddress > MaximumSupportMemAddress - (Length - 1))) {\r | |
403 | return RETURN_UNSUPPORTED;\r | |
404 | }\r | |
405 | \r | |
717fb604 JY |
406 | // DEBUG ((DEBUG_ERROR, "ConvertMemoryPageAttributes(%x) - %016lx, %016lx, %02lx\n", IsSet, BaseAddress, Length, Attributes));\r |
407 | \r | |
408 | if (IsSplitted != NULL) {\r | |
409 | *IsSplitted = FALSE;\r | |
410 | }\r | |
411 | if (IsModified != NULL) {\r | |
412 | *IsModified = FALSE;\r | |
413 | }\r | |
414 | \r | |
415 | //\r | |
416 | // Below logic is to check 2M/4K page to make sure we donot waist memory.\r | |
417 | //\r | |
418 | while (Length != 0) {\r | |
419 | PageEntry = GetPageTableEntry (BaseAddress, &PageAttribute);\r | |
420 | if (PageEntry == NULL) {\r | |
421 | return RETURN_UNSUPPORTED;\r | |
422 | }\r | |
423 | PageEntryLength = PageAttributeToLength (PageAttribute);\r | |
424 | SplitAttribute = NeedSplitPage (BaseAddress, Length, PageEntry, PageAttribute);\r | |
425 | if (SplitAttribute == PageNone) {\r | |
426 | ConvertPageEntryAttribute (PageEntry, Attributes, IsSet, &IsEntryModified);\r | |
427 | if (IsEntryModified) {\r | |
428 | if (IsModified != NULL) {\r | |
429 | *IsModified = TRUE;\r | |
430 | }\r | |
431 | }\r | |
432 | //\r | |
433 | // Convert success, move to next\r | |
434 | //\r | |
435 | BaseAddress += PageEntryLength;\r | |
436 | Length -= PageEntryLength;\r | |
437 | } else {\r | |
438 | Status = SplitPage (PageEntry, PageAttribute, SplitAttribute);\r | |
439 | if (RETURN_ERROR (Status)) {\r | |
440 | return RETURN_UNSUPPORTED;\r | |
441 | }\r | |
442 | if (IsSplitted != NULL) {\r | |
443 | *IsSplitted = TRUE;\r | |
444 | }\r | |
445 | if (IsModified != NULL) {\r | |
446 | *IsModified = TRUE;\r | |
447 | }\r | |
448 | //\r | |
449 | // Just split current page\r | |
450 | // Convert success in next around\r | |
451 | //\r | |
452 | }\r | |
453 | }\r | |
454 | \r | |
455 | return RETURN_SUCCESS;\r | |
456 | }\r | |
457 | \r | |
458 | /**\r | |
459 | FlushTlb on current processor.\r | |
460 | \r | |
461 | @param[in,out] Buffer Pointer to private data buffer.\r | |
462 | **/\r | |
463 | VOID\r | |
464 | EFIAPI\r | |
465 | FlushTlbOnCurrentProcessor (\r | |
466 | IN OUT VOID *Buffer\r | |
467 | )\r | |
468 | {\r | |
469 | CpuFlushTlb ();\r | |
470 | }\r | |
471 | \r | |
472 | /**\r | |
473 | FlushTlb for all processors.\r | |
474 | **/\r | |
475 | VOID\r | |
476 | FlushTlbForAll (\r | |
477 | VOID\r | |
478 | )\r | |
479 | {\r | |
480 | UINTN Index;\r | |
481 | \r | |
482 | FlushTlbOnCurrentProcessor (NULL);\r | |
483 | \r | |
484 | for (Index = 0; Index < gSmst->NumberOfCpus; Index++) {\r | |
485 | if (Index != gSmst->CurrentlyExecutingCpu) {\r | |
486 | // Force to start up AP in blocking mode,\r | |
487 | SmmBlockingStartupThisAp (FlushTlbOnCurrentProcessor, Index, NULL);\r | |
488 | // Do not check return status, because AP might not be present in some corner cases.\r | |
489 | }\r | |
490 | }\r | |
491 | }\r | |
492 | \r | |
493 | /**\r | |
494 | This function sets the attributes for the memory region specified by BaseAddress and\r | |
495 | Length from their current attributes to the attributes specified by Attributes.\r | |
496 | \r | |
497 | @param[in] BaseAddress The physical address that is the start address of a memory region.\r | |
498 | @param[in] Length The size in bytes of the memory region.\r | |
499 | @param[in] Attributes The bit mask of attributes to set for the memory region.\r | |
500 | @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.\r | |
501 | \r | |
502 | @retval EFI_SUCCESS The attributes were set for the memory region.\r | |
503 | @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r | |
504 | BaseAddress and Length cannot be modified.\r | |
505 | @retval EFI_INVALID_PARAMETER Length is zero.\r | |
506 | Attributes specified an illegal combination of attributes that\r | |
507 | cannot be set together.\r | |
508 | @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r | |
509 | the memory resource range.\r | |
510 | @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r | |
511 | resource range specified by BaseAddress and Length.\r | |
512 | The bit mask of attributes is not support for the memory resource\r | |
513 | range specified by BaseAddress and Length.\r | |
514 | \r | |
515 | **/\r | |
516 | EFI_STATUS\r | |
517 | EFIAPI\r | |
518 | SmmSetMemoryAttributesEx (\r | |
519 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r | |
520 | IN UINT64 Length,\r | |
521 | IN UINT64 Attributes,\r | |
522 | OUT BOOLEAN *IsSplitted OPTIONAL\r | |
523 | )\r | |
524 | {\r | |
525 | EFI_STATUS Status;\r | |
526 | BOOLEAN IsModified;\r | |
527 | \r | |
528 | Status = ConvertMemoryPageAttributes (BaseAddress, Length, Attributes, TRUE, IsSplitted, &IsModified);\r | |
529 | if (!EFI_ERROR(Status)) {\r | |
530 | if (IsModified) {\r | |
531 | //\r | |
532 | // Flush TLB as last step\r | |
533 | //\r | |
534 | FlushTlbForAll();\r | |
535 | }\r | |
536 | }\r | |
537 | \r | |
538 | return Status;\r | |
539 | }\r | |
540 | \r | |
541 | /**\r | |
542 | This function clears the attributes for the memory region specified by BaseAddress and\r | |
543 | Length from their current attributes to the attributes specified by Attributes.\r | |
544 | \r | |
545 | @param[in] BaseAddress The physical address that is the start address of a memory region.\r | |
546 | @param[in] Length The size in bytes of the memory region.\r | |
547 | @param[in] Attributes The bit mask of attributes to clear for the memory region.\r | |
548 | @param[out] IsSplitted TRUE means page table splitted. FALSE means page table not splitted.\r | |
549 | \r | |
550 | @retval EFI_SUCCESS The attributes were cleared for the memory region.\r | |
551 | @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r | |
552 | BaseAddress and Length cannot be modified.\r | |
553 | @retval EFI_INVALID_PARAMETER Length is zero.\r | |
554 | Attributes specified an illegal combination of attributes that\r | |
555 | cannot be set together.\r | |
556 | @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r | |
557 | the memory resource range.\r | |
558 | @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r | |
559 | resource range specified by BaseAddress and Length.\r | |
560 | The bit mask of attributes is not support for the memory resource\r | |
561 | range specified by BaseAddress and Length.\r | |
562 | \r | |
563 | **/\r | |
564 | EFI_STATUS\r | |
565 | EFIAPI\r | |
566 | SmmClearMemoryAttributesEx (\r | |
567 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r | |
568 | IN UINT64 Length,\r | |
569 | IN UINT64 Attributes,\r | |
570 | OUT BOOLEAN *IsSplitted OPTIONAL\r | |
571 | )\r | |
572 | {\r | |
573 | EFI_STATUS Status;\r | |
574 | BOOLEAN IsModified;\r | |
575 | \r | |
576 | Status = ConvertMemoryPageAttributes (BaseAddress, Length, Attributes, FALSE, IsSplitted, &IsModified);\r | |
577 | if (!EFI_ERROR(Status)) {\r | |
578 | if (IsModified) {\r | |
579 | //\r | |
580 | // Flush TLB as last step\r | |
581 | //\r | |
582 | FlushTlbForAll();\r | |
583 | }\r | |
584 | }\r | |
585 | \r | |
586 | return Status;\r | |
587 | }\r | |
588 | \r | |
589 | /**\r | |
590 | This function sets the attributes for the memory region specified by BaseAddress and\r | |
591 | Length from their current attributes to the attributes specified by Attributes.\r | |
592 | \r | |
593 | @param[in] BaseAddress The physical address that is the start address of a memory region.\r | |
594 | @param[in] Length The size in bytes of the memory region.\r | |
595 | @param[in] Attributes The bit mask of attributes to set for the memory region.\r | |
596 | \r | |
597 | @retval EFI_SUCCESS The attributes were set for the memory region.\r | |
598 | @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r | |
599 | BaseAddress and Length cannot be modified.\r | |
600 | @retval EFI_INVALID_PARAMETER Length is zero.\r | |
601 | Attributes specified an illegal combination of attributes that\r | |
602 | cannot be set together.\r | |
603 | @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r | |
604 | the memory resource range.\r | |
605 | @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r | |
606 | resource range specified by BaseAddress and Length.\r | |
607 | The bit mask of attributes is not support for the memory resource\r | |
608 | range specified by BaseAddress and Length.\r | |
609 | \r | |
610 | **/\r | |
611 | EFI_STATUS\r | |
612 | EFIAPI\r | |
613 | SmmSetMemoryAttributes (\r | |
614 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r | |
615 | IN UINT64 Length,\r | |
616 | IN UINT64 Attributes\r | |
617 | )\r | |
618 | {\r | |
619 | return SmmSetMemoryAttributesEx (BaseAddress, Length, Attributes, NULL);\r | |
620 | }\r | |
621 | \r | |
622 | /**\r | |
623 | This function clears the attributes for the memory region specified by BaseAddress and\r | |
624 | Length from their current attributes to the attributes specified by Attributes.\r | |
625 | \r | |
626 | @param[in] BaseAddress The physical address that is the start address of a memory region.\r | |
627 | @param[in] Length The size in bytes of the memory region.\r | |
628 | @param[in] Attributes The bit mask of attributes to clear for the memory region.\r | |
629 | \r | |
630 | @retval EFI_SUCCESS The attributes were cleared for the memory region.\r | |
631 | @retval EFI_ACCESS_DENIED The attributes for the memory resource range specified by\r | |
632 | BaseAddress and Length cannot be modified.\r | |
633 | @retval EFI_INVALID_PARAMETER Length is zero.\r | |
634 | Attributes specified an illegal combination of attributes that\r | |
635 | cannot be set together.\r | |
636 | @retval EFI_OUT_OF_RESOURCES There are not enough system resources to modify the attributes of\r | |
637 | the memory resource range.\r | |
638 | @retval EFI_UNSUPPORTED The processor does not support one or more bytes of the memory\r | |
639 | resource range specified by BaseAddress and Length.\r | |
640 | The bit mask of attributes is not support for the memory resource\r | |
641 | range specified by BaseAddress and Length.\r | |
642 | \r | |
643 | **/\r | |
644 | EFI_STATUS\r | |
645 | EFIAPI\r | |
646 | SmmClearMemoryAttributes (\r | |
647 | IN EFI_PHYSICAL_ADDRESS BaseAddress,\r | |
648 | IN UINT64 Length,\r | |
649 | IN UINT64 Attributes\r | |
650 | )\r | |
651 | {\r | |
652 | return SmmClearMemoryAttributesEx (BaseAddress, Length, Attributes, NULL);\r | |
653 | }\r | |
654 | \r | |
655 | \r | |
656 | \r | |
657 | /**\r | |
658 | Retrieves a pointer to the system configuration table from the SMM System Table\r | |
659 | based on a specified GUID.\r | |
660 | \r | |
661 | @param[in] TableGuid The pointer to table's GUID type.\r | |
662 | @param[out] Table The pointer to the table associated with TableGuid in the EFI System Table.\r | |
663 | \r | |
664 | @retval EFI_SUCCESS A configuration table matching TableGuid was found.\r | |
665 | @retval EFI_NOT_FOUND A configuration table matching TableGuid could not be found.\r | |
666 | \r | |
667 | **/\r | |
668 | EFI_STATUS\r | |
669 | EFIAPI\r | |
670 | SmmGetSystemConfigurationTable (\r | |
671 | IN EFI_GUID *TableGuid,\r | |
672 | OUT VOID **Table\r | |
673 | )\r | |
674 | {\r | |
675 | UINTN Index;\r | |
676 | \r | |
677 | ASSERT (TableGuid != NULL);\r | |
678 | ASSERT (Table != NULL);\r | |
679 | \r | |
680 | *Table = NULL;\r | |
681 | for (Index = 0; Index < gSmst->NumberOfTableEntries; Index++) {\r | |
682 | if (CompareGuid (TableGuid, &(gSmst->SmmConfigurationTable[Index].VendorGuid))) {\r | |
683 | *Table = gSmst->SmmConfigurationTable[Index].VendorTable;\r | |
684 | return EFI_SUCCESS;\r | |
685 | }\r | |
686 | }\r | |
687 | \r | |
688 | return EFI_NOT_FOUND;\r | |
689 | }\r | |
690 | \r | |
691 | /**\r | |
692 | This function sets SMM save state buffer to be RW and XP.\r | |
693 | **/\r | |
694 | VOID\r | |
695 | PatchSmmSaveStateMap (\r | |
696 | VOID\r | |
697 | )\r | |
698 | {\r | |
699 | UINTN Index;\r | |
700 | UINTN TileCodeSize;\r | |
701 | UINTN TileDataSize;\r | |
702 | UINTN TileSize;\r | |
703 | \r | |
704 | TileCodeSize = GetSmiHandlerSize ();\r | |
705 | TileCodeSize = ALIGN_VALUE(TileCodeSize, SIZE_4KB);\r | |
f12367a0 | 706 | TileDataSize = (SMRAM_SAVE_STATE_MAP_OFFSET - SMM_PSD_OFFSET) + sizeof (SMRAM_SAVE_STATE_MAP);\r |
717fb604 JY |
707 | TileDataSize = ALIGN_VALUE(TileDataSize, SIZE_4KB);\r |
708 | TileSize = TileDataSize + TileCodeSize - 1;\r | |
709 | TileSize = 2 * GetPowerOfTwo32 ((UINT32)TileSize);\r | |
710 | \r | |
711 | DEBUG ((DEBUG_INFO, "PatchSmmSaveStateMap:\n"));\r | |
712 | for (Index = 0; Index < mMaxNumberOfCpus - 1; Index++) {\r | |
713 | //\r | |
714 | // Code\r | |
715 | //\r | |
716 | SmmSetMemoryAttributes (\r | |
717 | mCpuHotPlugData.SmBase[Index] + SMM_HANDLER_OFFSET,\r | |
718 | TileCodeSize,\r | |
719 | EFI_MEMORY_RO\r | |
720 | );\r | |
721 | SmmClearMemoryAttributes (\r | |
722 | mCpuHotPlugData.SmBase[Index] + SMM_HANDLER_OFFSET,\r | |
723 | TileCodeSize,\r | |
724 | EFI_MEMORY_XP\r | |
725 | );\r | |
726 | \r | |
727 | //\r | |
728 | // Data\r | |
729 | //\r | |
730 | SmmClearMemoryAttributes (\r | |
731 | mCpuHotPlugData.SmBase[Index] + SMM_HANDLER_OFFSET + TileCodeSize,\r | |
732 | TileSize - TileCodeSize,\r | |
733 | EFI_MEMORY_RO\r | |
734 | );\r | |
735 | SmmSetMemoryAttributes (\r | |
736 | mCpuHotPlugData.SmBase[Index] + SMM_HANDLER_OFFSET + TileCodeSize,\r | |
737 | TileSize - TileCodeSize,\r | |
738 | EFI_MEMORY_XP\r | |
739 | );\r | |
740 | }\r | |
741 | \r | |
742 | //\r | |
743 | // Code\r | |
744 | //\r | |
745 | SmmSetMemoryAttributes (\r | |
746 | mCpuHotPlugData.SmBase[mMaxNumberOfCpus - 1] + SMM_HANDLER_OFFSET,\r | |
747 | TileCodeSize,\r | |
748 | EFI_MEMORY_RO\r | |
749 | );\r | |
750 | SmmClearMemoryAttributes (\r | |
751 | mCpuHotPlugData.SmBase[mMaxNumberOfCpus - 1] + SMM_HANDLER_OFFSET,\r | |
752 | TileCodeSize,\r | |
753 | EFI_MEMORY_XP\r | |
754 | );\r | |
755 | \r | |
756 | //\r | |
757 | // Data\r | |
758 | //\r | |
759 | SmmClearMemoryAttributes (\r | |
760 | mCpuHotPlugData.SmBase[mMaxNumberOfCpus - 1] + SMM_HANDLER_OFFSET + TileCodeSize,\r | |
761 | SIZE_32KB - TileCodeSize,\r | |
762 | EFI_MEMORY_RO\r | |
763 | );\r | |
764 | SmmSetMemoryAttributes (\r | |
765 | mCpuHotPlugData.SmBase[mMaxNumberOfCpus - 1] + SMM_HANDLER_OFFSET + TileCodeSize,\r | |
766 | SIZE_32KB - TileCodeSize,\r | |
767 | EFI_MEMORY_XP\r | |
768 | );\r | |
769 | }\r | |
770 | \r | |
717fb604 JY |
771 | /**\r |
772 | This function sets memory attribute according to MemoryAttributesTable.\r | |
773 | **/\r | |
774 | VOID\r | |
775 | SetMemMapAttributes (\r | |
776 | VOID\r | |
777 | )\r | |
778 | {\r | |
779 | EFI_MEMORY_DESCRIPTOR *MemoryMap;\r | |
780 | EFI_MEMORY_DESCRIPTOR *MemoryMapStart;\r | |
781 | UINTN MemoryMapEntryCount;\r | |
782 | UINTN DescriptorSize;\r | |
783 | UINTN Index;\r | |
784 | EDKII_PI_SMM_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable;\r | |
785 | \r | |
786 | SmmGetSystemConfigurationTable (&gEdkiiPiSmmMemoryAttributesTableGuid, (VOID **)&MemoryAttributesTable);\r | |
787 | if (MemoryAttributesTable == NULL) {\r | |
788 | DEBUG ((DEBUG_INFO, "MemoryAttributesTable - NULL\n"));\r | |
789 | return ;\r | |
790 | }\r | |
791 | \r | |
792 | DEBUG ((DEBUG_INFO, "MemoryAttributesTable:\n"));\r | |
793 | DEBUG ((DEBUG_INFO, " Version - 0x%08x\n", MemoryAttributesTable->Version));\r | |
794 | DEBUG ((DEBUG_INFO, " NumberOfEntries - 0x%08x\n", MemoryAttributesTable->NumberOfEntries));\r | |
795 | DEBUG ((DEBUG_INFO, " DescriptorSize - 0x%08x\n", MemoryAttributesTable->DescriptorSize));\r | |
796 | \r | |
797 | MemoryMapEntryCount = MemoryAttributesTable->NumberOfEntries;\r | |
798 | DescriptorSize = MemoryAttributesTable->DescriptorSize;\r | |
799 | MemoryMapStart = (EFI_MEMORY_DESCRIPTOR *)(MemoryAttributesTable + 1);\r | |
800 | MemoryMap = MemoryMapStart;\r | |
801 | for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r | |
802 | DEBUG ((DEBUG_INFO, "Entry (0x%x)\n", MemoryMap));\r | |
803 | DEBUG ((DEBUG_INFO, " Type - 0x%x\n", MemoryMap->Type));\r | |
804 | DEBUG ((DEBUG_INFO, " PhysicalStart - 0x%016lx\n", MemoryMap->PhysicalStart));\r | |
805 | DEBUG ((DEBUG_INFO, " VirtualStart - 0x%016lx\n", MemoryMap->VirtualStart));\r | |
806 | DEBUG ((DEBUG_INFO, " NumberOfPages - 0x%016lx\n", MemoryMap->NumberOfPages));\r | |
807 | DEBUG ((DEBUG_INFO, " Attribute - 0x%016lx\n", MemoryMap->Attribute));\r | |
808 | MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, DescriptorSize);\r | |
809 | }\r | |
810 | \r | |
811 | MemoryMap = MemoryMapStart;\r | |
812 | for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r | |
813 | DEBUG ((DEBUG_VERBOSE, "SetAttribute: Memory Entry - 0x%lx, 0x%x\n", MemoryMap->PhysicalStart, MemoryMap->NumberOfPages));\r | |
814 | switch (MemoryMap->Type) {\r | |
815 | case EfiRuntimeServicesCode:\r | |
816 | SmmSetMemoryAttributes (\r | |
817 | MemoryMap->PhysicalStart,\r | |
818 | EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages),\r | |
819 | EFI_MEMORY_RO\r | |
820 | );\r | |
821 | break;\r | |
822 | case EfiRuntimeServicesData:\r | |
823 | SmmSetMemoryAttributes (\r | |
824 | MemoryMap->PhysicalStart,\r | |
825 | EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages),\r | |
826 | EFI_MEMORY_XP\r | |
827 | );\r | |
828 | break;\r | |
829 | default:\r | |
830 | SmmSetMemoryAttributes (\r | |
831 | MemoryMap->PhysicalStart,\r | |
832 | EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages),\r | |
833 | EFI_MEMORY_XP\r | |
834 | );\r | |
835 | break;\r | |
836 | }\r | |
837 | MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, DescriptorSize);\r | |
838 | }\r | |
839 | \r | |
840 | PatchSmmSaveStateMap ();\r | |
841 | PatchGdtIdtMap ();\r | |
842 | \r | |
843 | return ;\r | |
844 | }\r | |
d2fc7711 JY |
845 | \r |
846 | /**\r | |
847 | Sort memory map entries based upon PhysicalStart, from low to high.\r | |
848 | \r | |
849 | @param MemoryMap A pointer to the buffer in which firmware places\r | |
850 | the current memory map.\r | |
851 | @param MemoryMapSize Size, in bytes, of the MemoryMap buffer.\r | |
852 | @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r | |
853 | **/\r | |
854 | STATIC\r | |
855 | VOID\r | |
856 | SortMemoryMap (\r | |
857 | IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r | |
858 | IN UINTN MemoryMapSize,\r | |
859 | IN UINTN DescriptorSize\r | |
860 | )\r | |
861 | {\r | |
862 | EFI_MEMORY_DESCRIPTOR *MemoryMapEntry;\r | |
863 | EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry;\r | |
864 | EFI_MEMORY_DESCRIPTOR *MemoryMapEnd;\r | |
865 | EFI_MEMORY_DESCRIPTOR TempMemoryMap;\r | |
866 | \r | |
867 | MemoryMapEntry = MemoryMap;\r | |
868 | NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r | |
869 | MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + MemoryMapSize);\r | |
870 | while (MemoryMapEntry < MemoryMapEnd) {\r | |
871 | while (NextMemoryMapEntry < MemoryMapEnd) {\r | |
872 | if (MemoryMapEntry->PhysicalStart > NextMemoryMapEntry->PhysicalStart) {\r | |
873 | CopyMem (&TempMemoryMap, MemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPTOR));\r | |
874 | CopyMem (MemoryMapEntry, NextMemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPTOR));\r | |
875 | CopyMem (NextMemoryMapEntry, &TempMemoryMap, sizeof(EFI_MEMORY_DESCRIPTOR));\r | |
876 | }\r | |
877 | \r | |
878 | NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);\r | |
879 | }\r | |
880 | \r | |
881 | MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r | |
882 | NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r | |
883 | }\r | |
884 | }\r | |
885 | \r | |
886 | /**\r | |
887 | Return if a UEFI memory page should be marked as not present in SMM page table.\r | |
888 | If the memory map entries type is\r | |
889 | EfiLoaderCode/Data, EfiBootServicesCode/Data, EfiConventionalMemory,\r | |
890 | EfiUnusableMemory, EfiACPIReclaimMemory, return TRUE.\r | |
891 | Or return FALSE.\r | |
892 | \r | |
893 | @param[in] MemoryMap A pointer to the memory descriptor.\r | |
894 | \r | |
895 | @return TRUE The memory described will be marked as not present in SMM page table.\r | |
896 | @return FALSE The memory described will not be marked as not present in SMM page table.\r | |
897 | **/\r | |
898 | BOOLEAN\r | |
899 | IsUefiPageNotPresent (\r | |
900 | IN EFI_MEMORY_DESCRIPTOR *MemoryMap\r | |
901 | )\r | |
902 | {\r | |
903 | switch (MemoryMap->Type) {\r | |
904 | case EfiLoaderCode:\r | |
905 | case EfiLoaderData:\r | |
906 | case EfiBootServicesCode:\r | |
907 | case EfiBootServicesData:\r | |
908 | case EfiConventionalMemory:\r | |
909 | case EfiUnusableMemory:\r | |
910 | case EfiACPIReclaimMemory:\r | |
911 | return TRUE;\r | |
912 | default:\r | |
913 | return FALSE;\r | |
914 | }\r | |
915 | }\r | |
916 | \r | |
917 | /**\r | |
918 | Merge continous memory map entries whose type is\r | |
919 | EfiLoaderCode/Data, EfiBootServicesCode/Data, EfiConventionalMemory,\r | |
920 | EfiUnusableMemory, EfiACPIReclaimMemory, because the memory described by\r | |
921 | these entries will be set as NOT present in SMM page table.\r | |
922 | \r | |
923 | @param[in, out] MemoryMap A pointer to the buffer in which firmware places\r | |
924 | the current memory map.\r | |
925 | @param[in, out] MemoryMapSize A pointer to the size, in bytes, of the\r | |
926 | MemoryMap buffer. On input, this is the size of\r | |
927 | the current memory map. On output,\r | |
928 | it is the size of new memory map after merge.\r | |
929 | @param[in] DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r | |
930 | **/\r | |
931 | STATIC\r | |
932 | VOID\r | |
933 | MergeMemoryMapForNotPresentEntry (\r | |
934 | IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r | |
935 | IN OUT UINTN *MemoryMapSize,\r | |
936 | IN UINTN DescriptorSize\r | |
937 | )\r | |
938 | {\r | |
939 | EFI_MEMORY_DESCRIPTOR *MemoryMapEntry;\r | |
940 | EFI_MEMORY_DESCRIPTOR *MemoryMapEnd;\r | |
941 | UINT64 MemoryBlockLength;\r | |
942 | EFI_MEMORY_DESCRIPTOR *NewMemoryMapEntry;\r | |
943 | EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry;\r | |
944 | \r | |
945 | MemoryMapEntry = MemoryMap;\r | |
946 | NewMemoryMapEntry = MemoryMap;\r | |
947 | MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + *MemoryMapSize);\r | |
948 | while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) {\r | |
949 | CopyMem (NewMemoryMapEntry, MemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPTOR));\r | |
950 | NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r | |
951 | \r | |
952 | do {\r | |
953 | MemoryBlockLength = (UINT64) (EFI_PAGES_TO_SIZE((UINTN)MemoryMapEntry->NumberOfPages));\r | |
954 | if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) &&\r | |
955 | IsUefiPageNotPresent(MemoryMapEntry) && IsUefiPageNotPresent(NextMemoryMapEntry) &&\r | |
956 | ((MemoryMapEntry->PhysicalStart + MemoryBlockLength) == NextMemoryMapEntry->PhysicalStart)) {\r | |
957 | MemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;\r | |
958 | if (NewMemoryMapEntry != MemoryMapEntry) {\r | |
959 | NewMemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;\r | |
960 | }\r | |
961 | \r | |
962 | NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);\r | |
963 | continue;\r | |
964 | } else {\r | |
965 | MemoryMapEntry = PREVIOUS_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);\r | |
966 | break;\r | |
967 | }\r | |
968 | } while (TRUE);\r | |
969 | \r | |
970 | MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r | |
971 | NewMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NewMemoryMapEntry, DescriptorSize);\r | |
972 | }\r | |
973 | \r | |
974 | *MemoryMapSize = (UINTN)NewMemoryMapEntry - (UINTN)MemoryMap;\r | |
975 | \r | |
976 | return ;\r | |
977 | }\r | |
978 | \r | |
979 | /**\r | |
980 | This function caches the UEFI memory map information.\r | |
981 | **/\r | |
982 | VOID\r | |
983 | GetUefiMemoryMap (\r | |
984 | VOID\r | |
985 | )\r | |
986 | {\r | |
987 | EFI_STATUS Status;\r | |
988 | UINTN MapKey;\r | |
989 | UINT32 DescriptorVersion;\r | |
990 | EFI_MEMORY_DESCRIPTOR *MemoryMap;\r | |
991 | UINTN UefiMemoryMapSize;\r | |
992 | \r | |
993 | DEBUG ((DEBUG_INFO, "GetUefiMemoryMap\n"));\r | |
994 | \r | |
995 | UefiMemoryMapSize = 0;\r | |
996 | MemoryMap = NULL;\r | |
997 | Status = gBS->GetMemoryMap (\r | |
998 | &UefiMemoryMapSize,\r | |
999 | MemoryMap,\r | |
1000 | &MapKey,\r | |
1001 | &mUefiDescriptorSize,\r | |
1002 | &DescriptorVersion\r | |
1003 | );\r | |
1004 | ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r | |
1005 | \r | |
1006 | do {\r | |
1007 | Status = gBS->AllocatePool (EfiBootServicesData, UefiMemoryMapSize, (VOID **)&MemoryMap);\r | |
1008 | ASSERT (MemoryMap != NULL);\r | |
1009 | if (MemoryMap == NULL) {\r | |
1010 | return ;\r | |
1011 | }\r | |
1012 | \r | |
1013 | Status = gBS->GetMemoryMap (\r | |
1014 | &UefiMemoryMapSize,\r | |
1015 | MemoryMap,\r | |
1016 | &MapKey,\r | |
1017 | &mUefiDescriptorSize,\r | |
1018 | &DescriptorVersion\r | |
1019 | );\r | |
1020 | if (EFI_ERROR (Status)) {\r | |
1021 | gBS->FreePool (MemoryMap);\r | |
1022 | MemoryMap = NULL;\r | |
1023 | }\r | |
1024 | } while (Status == EFI_BUFFER_TOO_SMALL);\r | |
1025 | \r | |
403f5476 HW |
1026 | if (MemoryMap == NULL) {\r |
1027 | return ;\r | |
1028 | }\r | |
1029 | \r | |
d2fc7711 JY |
1030 | SortMemoryMap (MemoryMap, UefiMemoryMapSize, mUefiDescriptorSize);\r |
1031 | MergeMemoryMapForNotPresentEntry (MemoryMap, &UefiMemoryMapSize, mUefiDescriptorSize);\r | |
1032 | \r | |
1033 | mUefiMemoryMapSize = UefiMemoryMapSize;\r | |
1034 | mUefiMemoryMap = AllocateCopyPool (UefiMemoryMapSize, MemoryMap);\r | |
1035 | ASSERT (mUefiMemoryMap != NULL);\r | |
1036 | \r | |
1037 | gBS->FreePool (MemoryMap);\r | |
1038 | }\r | |
1039 | \r | |
1040 | /**\r | |
1041 | This function sets UEFI memory attribute according to UEFI memory map.\r | |
1042 | \r | |
1043 | The normal memory region is marked as not present, such as\r | |
1044 | EfiLoaderCode/Data, EfiBootServicesCode/Data, EfiConventionalMemory,\r | |
1045 | EfiUnusableMemory, EfiACPIReclaimMemory.\r | |
1046 | **/\r | |
1047 | VOID\r | |
1048 | SetUefiMemMapAttributes (\r | |
1049 | VOID\r | |
1050 | )\r | |
1051 | {\r | |
714c2603 | 1052 | EFI_STATUS Status;\r |
d2fc7711 JY |
1053 | EFI_MEMORY_DESCRIPTOR *MemoryMap;\r |
1054 | UINTN MemoryMapEntryCount;\r | |
1055 | UINTN Index;\r | |
1056 | \r | |
1057 | DEBUG ((DEBUG_INFO, "SetUefiMemMapAttributes\n"));\r | |
1058 | \r | |
1059 | if (mUefiMemoryMap == NULL) {\r | |
1060 | DEBUG ((DEBUG_INFO, "UefiMemoryMap - NULL\n"));\r | |
1061 | return ;\r | |
1062 | }\r | |
1063 | \r | |
1064 | MemoryMapEntryCount = mUefiMemoryMapSize/mUefiDescriptorSize;\r | |
1065 | MemoryMap = mUefiMemoryMap;\r | |
1066 | for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r | |
1067 | if (IsUefiPageNotPresent(MemoryMap)) {\r | |
714c2603 SZ |
1068 | Status = SmmSetMemoryAttributes (\r |
1069 | MemoryMap->PhysicalStart,\r | |
1070 | EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages),\r | |
1071 | EFI_MEMORY_RP\r | |
1072 | );\r | |
1073 | DEBUG ((\r | |
1074 | DEBUG_INFO,\r | |
1075 | "UefiMemory protection: 0x%lx - 0x%lx %r\n",\r | |
d2fc7711 | 1076 | MemoryMap->PhysicalStart,\r |
714c2603 SZ |
1077 | MemoryMap->PhysicalStart + (UINT64)EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages),\r |
1078 | Status\r | |
1079 | ));\r | |
d2fc7711 JY |
1080 | }\r |
1081 | MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize);\r | |
1082 | }\r | |
1083 | \r | |
1084 | //\r | |
1085 | // Do free mUefiMemoryMap, it will be checked in IsSmmCommBufferForbiddenAddress().\r | |
1086 | //\r | |
1087 | }\r | |
1088 | \r | |
1089 | /**\r | |
1090 | Return if the Address is forbidden as SMM communication buffer.\r | |
1091 | \r | |
1092 | @param[in] Address the address to be checked\r | |
1093 | \r | |
1094 | @return TRUE The address is forbidden as SMM communication buffer.\r | |
1095 | @return FALSE The address is allowed as SMM communication buffer.\r | |
1096 | **/\r | |
1097 | BOOLEAN\r | |
1098 | IsSmmCommBufferForbiddenAddress (\r | |
1099 | IN UINT64 Address\r | |
1100 | )\r | |
1101 | {\r | |
1102 | EFI_MEMORY_DESCRIPTOR *MemoryMap;\r | |
1103 | UINTN MemoryMapEntryCount;\r | |
1104 | UINTN Index;\r | |
1105 | \r | |
403f5476 HW |
1106 | if (mUefiMemoryMap == NULL) {\r |
1107 | return FALSE;\r | |
1108 | }\r | |
1109 | \r | |
d2fc7711 JY |
1110 | MemoryMap = mUefiMemoryMap;\r |
1111 | MemoryMapEntryCount = mUefiMemoryMapSize/mUefiDescriptorSize;\r | |
1112 | for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r | |
1113 | if (IsUefiPageNotPresent (MemoryMap)) {\r | |
1114 | if ((Address >= MemoryMap->PhysicalStart) &&\r | |
1115 | (Address < MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages)) ) {\r | |
1116 | return TRUE;\r | |
1117 | }\r | |
1118 | }\r | |
1119 | MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize);\r | |
1120 | }\r | |
1121 | return FALSE;\r | |
1122 | }\r |