]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c
UefiCpuPkg PiSmmCpuDxeSmm: Fixed #double fault on #page fault for IA32
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / SmmCpuMemoryManagement.c
CommitLineData
717fb604
JY
1/** @file\r
2\r
6e601a41 3Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
717fb604
JY
4This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT 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
22EFI_MEMORY_DESCRIPTOR *mUefiMemoryMap;\r
23UINTN mUefiMemoryMapSize;\r
24UINTN mUefiDescriptorSize;\r
25\r
717fb604
JY
26PAGE_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
37UINTN\r
38GetPageTableBase (\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
52UINTN\r
53PageAttributeToLength (\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
73UINTN\r
74PageAttributeToMask (\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
95VOID *\r
96GetPageTableEntry (\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
164UINT64\r
165GetAttributesFromPageEntry (\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
191VOID\r
192ConvertPageEntryAttribute (\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
247PAGE_ATTRIBUTE\r
248NeedSplitPage (\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
281RETURN_STATUS\r
282SplitPage (\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
366RETURN_STATUS\r
367EFIAPI\r
368ConvertMemoryPageAttributes (\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
463VOID\r
464EFIAPI\r
465FlushTlbOnCurrentProcessor (\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
475VOID\r
476FlushTlbForAll (\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
516EFI_STATUS\r
517EFIAPI\r
518SmmSetMemoryAttributesEx (\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
564EFI_STATUS\r
565EFIAPI\r
566SmmClearMemoryAttributesEx (\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
611EFI_STATUS\r
612EFIAPI\r
613SmmSetMemoryAttributes (\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
644EFI_STATUS\r
645EFIAPI\r
646SmmClearMemoryAttributes (\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
668EFI_STATUS\r
669EFIAPI\r
670SmmGetSystemConfigurationTable (\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
694VOID\r
695PatchSmmSaveStateMap (\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
6e601a41
SZ
771/**\r
772 This function sets GDT/IDT buffer to be RO and XP.\r
773**/\r
774VOID\r
775PatchGdtIdtMap (\r
776 VOID\r
777 )\r
778{\r
779 EFI_PHYSICAL_ADDRESS BaseAddress;\r
780 UINTN Size;\r
781\r
782 //\r
783 // GDT\r
784 //\r
785 DEBUG ((DEBUG_INFO, "PatchGdtIdtMap - GDT:\n"));\r
786\r
787 BaseAddress = mGdtBuffer;\r
788 Size = ALIGN_VALUE(mGdtBufferSize, SIZE_4KB);\r
789 //\r
790 // The range should have been set to RO\r
791 // if it is allocated with EfiRuntimeServicesCode.\r
792 //\r
793 SmmSetMemoryAttributes (\r
794 BaseAddress,\r
795 Size,\r
796 EFI_MEMORY_XP\r
797 );\r
798\r
799 //\r
800 // IDT\r
801 //\r
802 DEBUG ((DEBUG_INFO, "PatchGdtIdtMap - IDT:\n"));\r
803\r
804 BaseAddress = gcSmiIdtr.Base;\r
805 Size = ALIGN_VALUE(gcSmiIdtr.Limit + 1, SIZE_4KB);\r
806 SmmSetMemoryAttributes (\r
807 BaseAddress,\r
808 Size,\r
809 EFI_MEMORY_RO\r
810 );\r
811 SmmSetMemoryAttributes (\r
812 BaseAddress,\r
813 Size,\r
814 EFI_MEMORY_XP\r
815 );\r
816}\r
817\r
717fb604
JY
818/**\r
819 This function sets memory attribute according to MemoryAttributesTable.\r
820**/\r
821VOID\r
822SetMemMapAttributes (\r
823 VOID\r
824 )\r
825{\r
826 EFI_MEMORY_DESCRIPTOR *MemoryMap;\r
827 EFI_MEMORY_DESCRIPTOR *MemoryMapStart;\r
828 UINTN MemoryMapEntryCount;\r
829 UINTN DescriptorSize;\r
830 UINTN Index;\r
831 EDKII_PI_SMM_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable;\r
832\r
833 SmmGetSystemConfigurationTable (&gEdkiiPiSmmMemoryAttributesTableGuid, (VOID **)&MemoryAttributesTable);\r
834 if (MemoryAttributesTable == NULL) {\r
835 DEBUG ((DEBUG_INFO, "MemoryAttributesTable - NULL\n"));\r
836 return ;\r
837 }\r
838\r
839 DEBUG ((DEBUG_INFO, "MemoryAttributesTable:\n"));\r
840 DEBUG ((DEBUG_INFO, " Version - 0x%08x\n", MemoryAttributesTable->Version));\r
841 DEBUG ((DEBUG_INFO, " NumberOfEntries - 0x%08x\n", MemoryAttributesTable->NumberOfEntries));\r
842 DEBUG ((DEBUG_INFO, " DescriptorSize - 0x%08x\n", MemoryAttributesTable->DescriptorSize));\r
843\r
844 MemoryMapEntryCount = MemoryAttributesTable->NumberOfEntries;\r
845 DescriptorSize = MemoryAttributesTable->DescriptorSize;\r
846 MemoryMapStart = (EFI_MEMORY_DESCRIPTOR *)(MemoryAttributesTable + 1);\r
847 MemoryMap = MemoryMapStart;\r
848 for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r
849 DEBUG ((DEBUG_INFO, "Entry (0x%x)\n", MemoryMap));\r
850 DEBUG ((DEBUG_INFO, " Type - 0x%x\n", MemoryMap->Type));\r
851 DEBUG ((DEBUG_INFO, " PhysicalStart - 0x%016lx\n", MemoryMap->PhysicalStart));\r
852 DEBUG ((DEBUG_INFO, " VirtualStart - 0x%016lx\n", MemoryMap->VirtualStart));\r
853 DEBUG ((DEBUG_INFO, " NumberOfPages - 0x%016lx\n", MemoryMap->NumberOfPages));\r
854 DEBUG ((DEBUG_INFO, " Attribute - 0x%016lx\n", MemoryMap->Attribute));\r
855 MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, DescriptorSize);\r
856 }\r
857\r
858 MemoryMap = MemoryMapStart;\r
859 for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r
860 DEBUG ((DEBUG_VERBOSE, "SetAttribute: Memory Entry - 0x%lx, 0x%x\n", MemoryMap->PhysicalStart, MemoryMap->NumberOfPages));\r
861 switch (MemoryMap->Type) {\r
862 case EfiRuntimeServicesCode:\r
863 SmmSetMemoryAttributes (\r
864 MemoryMap->PhysicalStart,\r
865 EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages),\r
866 EFI_MEMORY_RO\r
867 );\r
868 break;\r
869 case EfiRuntimeServicesData:\r
870 SmmSetMemoryAttributes (\r
871 MemoryMap->PhysicalStart,\r
872 EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages),\r
873 EFI_MEMORY_XP\r
874 );\r
875 break;\r
876 default:\r
877 SmmSetMemoryAttributes (\r
878 MemoryMap->PhysicalStart,\r
879 EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages),\r
880 EFI_MEMORY_XP\r
881 );\r
882 break;\r
883 }\r
884 MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, DescriptorSize);\r
885 }\r
886\r
887 PatchSmmSaveStateMap ();\r
888 PatchGdtIdtMap ();\r
889\r
890 return ;\r
891}\r
d2fc7711
JY
892\r
893/**\r
894 Sort memory map entries based upon PhysicalStart, from low to high.\r
895\r
896 @param MemoryMap A pointer to the buffer in which firmware places\r
897 the current memory map.\r
898 @param MemoryMapSize Size, in bytes, of the MemoryMap buffer.\r
899 @param DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r
900**/\r
901STATIC\r
902VOID\r
903SortMemoryMap (\r
904 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
905 IN UINTN MemoryMapSize,\r
906 IN UINTN DescriptorSize\r
907 )\r
908{\r
909 EFI_MEMORY_DESCRIPTOR *MemoryMapEntry;\r
910 EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry;\r
911 EFI_MEMORY_DESCRIPTOR *MemoryMapEnd;\r
912 EFI_MEMORY_DESCRIPTOR TempMemoryMap;\r
913\r
914 MemoryMapEntry = MemoryMap;\r
915 NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
916 MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + MemoryMapSize);\r
917 while (MemoryMapEntry < MemoryMapEnd) {\r
918 while (NextMemoryMapEntry < MemoryMapEnd) {\r
919 if (MemoryMapEntry->PhysicalStart > NextMemoryMapEntry->PhysicalStart) {\r
920 CopyMem (&TempMemoryMap, MemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPTOR));\r
921 CopyMem (MemoryMapEntry, NextMemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPTOR));\r
922 CopyMem (NextMemoryMapEntry, &TempMemoryMap, sizeof(EFI_MEMORY_DESCRIPTOR));\r
923 }\r
924\r
925 NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);\r
926 }\r
927\r
928 MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
929 NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
930 }\r
931}\r
932\r
933/**\r
934 Return if a UEFI memory page should be marked as not present in SMM page table.\r
935 If the memory map entries type is\r
936 EfiLoaderCode/Data, EfiBootServicesCode/Data, EfiConventionalMemory,\r
937 EfiUnusableMemory, EfiACPIReclaimMemory, return TRUE.\r
938 Or return FALSE.\r
939\r
940 @param[in] MemoryMap A pointer to the memory descriptor.\r
941\r
942 @return TRUE The memory described will be marked as not present in SMM page table.\r
943 @return FALSE The memory described will not be marked as not present in SMM page table.\r
944**/\r
945BOOLEAN\r
946IsUefiPageNotPresent (\r
947 IN EFI_MEMORY_DESCRIPTOR *MemoryMap\r
948 )\r
949{\r
950 switch (MemoryMap->Type) {\r
951 case EfiLoaderCode:\r
952 case EfiLoaderData:\r
953 case EfiBootServicesCode:\r
954 case EfiBootServicesData:\r
955 case EfiConventionalMemory:\r
956 case EfiUnusableMemory:\r
957 case EfiACPIReclaimMemory:\r
958 return TRUE;\r
959 default:\r
960 return FALSE;\r
961 }\r
962}\r
963\r
964/**\r
965 Merge continous memory map entries whose type is\r
966 EfiLoaderCode/Data, EfiBootServicesCode/Data, EfiConventionalMemory,\r
967 EfiUnusableMemory, EfiACPIReclaimMemory, because the memory described by\r
968 these entries will be set as NOT present in SMM page table.\r
969\r
970 @param[in, out] MemoryMap A pointer to the buffer in which firmware places\r
971 the current memory map.\r
972 @param[in, out] MemoryMapSize A pointer to the size, in bytes, of the\r
973 MemoryMap buffer. On input, this is the size of\r
974 the current memory map. On output,\r
975 it is the size of new memory map after merge.\r
976 @param[in] DescriptorSize Size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.\r
977**/\r
978STATIC\r
979VOID\r
980MergeMemoryMapForNotPresentEntry (\r
981 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
982 IN OUT UINTN *MemoryMapSize,\r
983 IN UINTN DescriptorSize\r
984 )\r
985{\r
986 EFI_MEMORY_DESCRIPTOR *MemoryMapEntry;\r
987 EFI_MEMORY_DESCRIPTOR *MemoryMapEnd;\r
988 UINT64 MemoryBlockLength;\r
989 EFI_MEMORY_DESCRIPTOR *NewMemoryMapEntry;\r
990 EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry;\r
991\r
992 MemoryMapEntry = MemoryMap;\r
993 NewMemoryMapEntry = MemoryMap;\r
994 MemoryMapEnd = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) MemoryMap + *MemoryMapSize);\r
995 while ((UINTN)MemoryMapEntry < (UINTN)MemoryMapEnd) {\r
996 CopyMem (NewMemoryMapEntry, MemoryMapEntry, sizeof(EFI_MEMORY_DESCRIPTOR));\r
997 NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
998\r
999 do {\r
1000 MemoryBlockLength = (UINT64) (EFI_PAGES_TO_SIZE((UINTN)MemoryMapEntry->NumberOfPages));\r
1001 if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) &&\r
1002 IsUefiPageNotPresent(MemoryMapEntry) && IsUefiPageNotPresent(NextMemoryMapEntry) &&\r
1003 ((MemoryMapEntry->PhysicalStart + MemoryBlockLength) == NextMemoryMapEntry->PhysicalStart)) {\r
1004 MemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;\r
1005 if (NewMemoryMapEntry != MemoryMapEntry) {\r
1006 NewMemoryMapEntry->NumberOfPages += NextMemoryMapEntry->NumberOfPages;\r
1007 }\r
1008\r
1009 NextMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);\r
1010 continue;\r
1011 } else {\r
1012 MemoryMapEntry = PREVIOUS_MEMORY_DESCRIPTOR (NextMemoryMapEntry, DescriptorSize);\r
1013 break;\r
1014 }\r
1015 } while (TRUE);\r
1016\r
1017 MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
1018 NewMemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (NewMemoryMapEntry, DescriptorSize);\r
1019 }\r
1020\r
1021 *MemoryMapSize = (UINTN)NewMemoryMapEntry - (UINTN)MemoryMap;\r
1022\r
1023 return ;\r
1024}\r
1025\r
1026/**\r
1027 This function caches the UEFI memory map information.\r
1028**/\r
1029VOID\r
1030GetUefiMemoryMap (\r
1031 VOID\r
1032 )\r
1033{\r
1034 EFI_STATUS Status;\r
1035 UINTN MapKey;\r
1036 UINT32 DescriptorVersion;\r
1037 EFI_MEMORY_DESCRIPTOR *MemoryMap;\r
1038 UINTN UefiMemoryMapSize;\r
1039\r
1040 DEBUG ((DEBUG_INFO, "GetUefiMemoryMap\n"));\r
1041\r
1042 UefiMemoryMapSize = 0;\r
1043 MemoryMap = NULL;\r
1044 Status = gBS->GetMemoryMap (\r
1045 &UefiMemoryMapSize,\r
1046 MemoryMap,\r
1047 &MapKey,\r
1048 &mUefiDescriptorSize,\r
1049 &DescriptorVersion\r
1050 );\r
1051 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
1052\r
1053 do {\r
1054 Status = gBS->AllocatePool (EfiBootServicesData, UefiMemoryMapSize, (VOID **)&MemoryMap);\r
1055 ASSERT (MemoryMap != NULL);\r
1056 if (MemoryMap == NULL) {\r
1057 return ;\r
1058 }\r
1059\r
1060 Status = gBS->GetMemoryMap (\r
1061 &UefiMemoryMapSize,\r
1062 MemoryMap,\r
1063 &MapKey,\r
1064 &mUefiDescriptorSize,\r
1065 &DescriptorVersion\r
1066 );\r
1067 if (EFI_ERROR (Status)) {\r
1068 gBS->FreePool (MemoryMap);\r
1069 MemoryMap = NULL;\r
1070 }\r
1071 } while (Status == EFI_BUFFER_TOO_SMALL);\r
1072\r
403f5476
HW
1073 if (MemoryMap == NULL) {\r
1074 return ;\r
1075 }\r
1076\r
d2fc7711
JY
1077 SortMemoryMap (MemoryMap, UefiMemoryMapSize, mUefiDescriptorSize);\r
1078 MergeMemoryMapForNotPresentEntry (MemoryMap, &UefiMemoryMapSize, mUefiDescriptorSize);\r
1079\r
1080 mUefiMemoryMapSize = UefiMemoryMapSize;\r
1081 mUefiMemoryMap = AllocateCopyPool (UefiMemoryMapSize, MemoryMap);\r
1082 ASSERT (mUefiMemoryMap != NULL);\r
1083\r
1084 gBS->FreePool (MemoryMap);\r
1085}\r
1086\r
1087/**\r
1088 This function sets UEFI memory attribute according to UEFI memory map.\r
1089\r
1090 The normal memory region is marked as not present, such as\r
1091 EfiLoaderCode/Data, EfiBootServicesCode/Data, EfiConventionalMemory,\r
1092 EfiUnusableMemory, EfiACPIReclaimMemory.\r
1093**/\r
1094VOID\r
1095SetUefiMemMapAttributes (\r
1096 VOID\r
1097 )\r
1098{\r
714c2603 1099 EFI_STATUS Status;\r
d2fc7711
JY
1100 EFI_MEMORY_DESCRIPTOR *MemoryMap;\r
1101 UINTN MemoryMapEntryCount;\r
1102 UINTN Index;\r
1103\r
1104 DEBUG ((DEBUG_INFO, "SetUefiMemMapAttributes\n"));\r
1105\r
1106 if (mUefiMemoryMap == NULL) {\r
1107 DEBUG ((DEBUG_INFO, "UefiMemoryMap - NULL\n"));\r
1108 return ;\r
1109 }\r
1110\r
1111 MemoryMapEntryCount = mUefiMemoryMapSize/mUefiDescriptorSize;\r
1112 MemoryMap = mUefiMemoryMap;\r
1113 for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r
1114 if (IsUefiPageNotPresent(MemoryMap)) {\r
714c2603
SZ
1115 Status = SmmSetMemoryAttributes (\r
1116 MemoryMap->PhysicalStart,\r
1117 EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages),\r
1118 EFI_MEMORY_RP\r
1119 );\r
1120 DEBUG ((\r
1121 DEBUG_INFO,\r
1122 "UefiMemory protection: 0x%lx - 0x%lx %r\n",\r
d2fc7711 1123 MemoryMap->PhysicalStart,\r
714c2603
SZ
1124 MemoryMap->PhysicalStart + (UINT64)EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages),\r
1125 Status\r
1126 ));\r
d2fc7711
JY
1127 }\r
1128 MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize);\r
1129 }\r
1130\r
1131 //\r
1132 // Do free mUefiMemoryMap, it will be checked in IsSmmCommBufferForbiddenAddress().\r
1133 //\r
1134}\r
1135\r
1136/**\r
1137 Return if the Address is forbidden as SMM communication buffer.\r
1138\r
1139 @param[in] Address the address to be checked\r
1140\r
1141 @return TRUE The address is forbidden as SMM communication buffer.\r
1142 @return FALSE The address is allowed as SMM communication buffer.\r
1143**/\r
1144BOOLEAN\r
1145IsSmmCommBufferForbiddenAddress (\r
1146 IN UINT64 Address\r
1147 )\r
1148{\r
1149 EFI_MEMORY_DESCRIPTOR *MemoryMap;\r
1150 UINTN MemoryMapEntryCount;\r
1151 UINTN Index;\r
1152\r
403f5476
HW
1153 if (mUefiMemoryMap == NULL) {\r
1154 return FALSE;\r
1155 }\r
1156\r
d2fc7711
JY
1157 MemoryMap = mUefiMemoryMap;\r
1158 MemoryMapEntryCount = mUefiMemoryMapSize/mUefiDescriptorSize;\r
1159 for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r
1160 if (IsUefiPageNotPresent (MemoryMap)) {\r
1161 if ((Address >= MemoryMap->PhysicalStart) &&\r
1162 (Address < MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages)) ) {\r
1163 return TRUE;\r
1164 }\r
1165 }\r
1166 MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize);\r
1167 }\r
1168 return FALSE;\r
1169}\r
827330cc
JW
1170\r
1171/**\r
1172 This function set given attributes of the memory region specified by\r
1173 BaseAddress and Length.\r
1174\r
1175 @param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.\r
1176 @param BaseAddress The physical address that is the start address of\r
1177 a memory region.\r
1178 @param Length The size in bytes of the memory region.\r
1179 @param Attributes The bit mask of attributes to set for the memory\r
1180 region.\r
1181\r
1182 @retval EFI_SUCCESS The attributes were set for the memory region.\r
1183 @retval EFI_INVALID_PARAMETER Length is zero.\r
1184 Attributes specified an illegal combination of\r
1185 attributes that cannot be set together.\r
1186 @retval EFI_UNSUPPORTED The processor does not support one or more\r
1187 bytes of the memory resource range specified\r
1188 by BaseAddress and Length.\r
1189 The bit mask of attributes is not support for\r
1190 the memory resource range specified by\r
1191 BaseAddress and Length.\r
1192\r
1193**/\r
1194EFI_STATUS\r
1195EFIAPI\r
1196EdkiiSmmSetMemoryAttributes (\r
1197 IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *This,\r
1198 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
1199 IN UINT64 Length,\r
1200 IN UINT64 Attributes\r
1201 )\r
1202{\r
1203 return SmmSetMemoryAttributes (BaseAddress, Length, Attributes);\r
1204}\r
1205\r
1206/**\r
1207 This function clears given attributes of the memory region specified by\r
1208 BaseAddress and Length.\r
1209\r
1210 @param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.\r
1211 @param BaseAddress The physical address that is the start address of\r
1212 a memory region.\r
1213 @param Length The size in bytes of the memory region.\r
1214 @param Attributes The bit mask of attributes to set for the memory\r
1215 region.\r
1216\r
1217 @retval EFI_SUCCESS The attributes were set for the memory region.\r
1218 @retval EFI_INVALID_PARAMETER Length is zero.\r
1219 Attributes specified an illegal combination of\r
1220 attributes that cannot be set together.\r
1221 @retval EFI_UNSUPPORTED The processor does not support one or more\r
1222 bytes of the memory resource range specified\r
1223 by BaseAddress and Length.\r
1224 The bit mask of attributes is not support for\r
1225 the memory resource range specified by\r
1226 BaseAddress and Length.\r
1227\r
1228**/\r
1229EFI_STATUS\r
1230EFIAPI\r
1231EdkiiSmmClearMemoryAttributes (\r
1232 IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *This,\r
1233 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
1234 IN UINT64 Length,\r
1235 IN UINT64 Attributes\r
1236 )\r
1237{\r
1238 return SmmClearMemoryAttributes (BaseAddress, Length, Attributes);\r
1239}\r
1240\r
1241/**\r
1242 This function retrieve the attributes of the memory region specified by\r
1243 BaseAddress and Length. If different attributes are got from different part\r
1244 of the memory region, EFI_NO_MAPPING will be returned.\r
1245\r
1246 @param This The EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL instance.\r
1247 @param BaseAddress The physical address that is the start address of\r
1248 a memory region.\r
1249 @param Length The size in bytes of the memory region.\r
1250 @param Attributes Pointer to attributes returned.\r
1251\r
1252 @retval EFI_SUCCESS The attributes got for the memory region.\r
1253 @retval EFI_INVALID_PARAMETER Length is zero.\r
1254 Attributes is NULL.\r
1255 @retval EFI_NO_MAPPING Attributes are not consistent cross the memory\r
1256 region.\r
1257 @retval EFI_UNSUPPORTED The processor does not support one or more\r
1258 bytes of the memory resource range specified\r
1259 by BaseAddress and Length.\r
1260 The bit mask of attributes is not support for\r
1261 the memory resource range specified by\r
1262 BaseAddress and Length.\r
1263\r
1264**/\r
1265EFI_STATUS\r
1266EFIAPI\r
1267EdkiiSmmGetMemoryAttributes (\r
1268 IN EDKII_SMM_MEMORY_ATTRIBUTE_PROTOCOL *This,\r
1269 IN EFI_PHYSICAL_ADDRESS BaseAddress,\r
1270 IN UINT64 Length,\r
1271 OUT UINT64 *Attributes\r
1272 )\r
1273{\r
1274 EFI_PHYSICAL_ADDRESS Address;\r
1275 UINT64 *PageEntry;\r
1276 UINT64 MemAttr;\r
1277 PAGE_ATTRIBUTE PageAttr;\r
1278 INT64 Size;\r
1279\r
1280 if (Length < SIZE_4KB || Attributes == NULL) {\r
1281 return EFI_INVALID_PARAMETER;\r
1282 }\r
1283\r
1284 Size = (INT64)Length;\r
1285 MemAttr = (UINT64)-1;\r
1286\r
1287 do {\r
1288\r
1289 PageEntry = GetPageTableEntry (BaseAddress, &PageAttr);\r
1290 if (PageEntry == NULL || PageAttr == PageNone) {\r
1291 return EFI_UNSUPPORTED;\r
1292 }\r
1293\r
1294 //\r
1295 // If the memory range is cross page table boundary, make sure they\r
1296 // share the same attribute. Return EFI_NO_MAPPING if not.\r
1297 //\r
1298 *Attributes = GetAttributesFromPageEntry (PageEntry);\r
1299 if (MemAttr != (UINT64)-1 && *Attributes != MemAttr) {\r
1300 return EFI_NO_MAPPING;\r
1301 }\r
1302\r
1303 switch (PageAttr) {\r
1304 case Page4K:\r
1305 Address = *PageEntry & ~mAddressEncMask & PAGING_4K_ADDRESS_MASK_64;\r
1306 Size -= (SIZE_4KB - (BaseAddress - Address));\r
1307 BaseAddress += (SIZE_4KB - (BaseAddress - Address));\r
1308 break;\r
1309\r
1310 case Page2M:\r
1311 Address = *PageEntry & ~mAddressEncMask & PAGING_2M_ADDRESS_MASK_64;\r
1312 Size -= SIZE_2MB - (BaseAddress - Address);\r
1313 BaseAddress += SIZE_2MB - (BaseAddress - Address);\r
1314 break;\r
1315\r
1316 case Page1G:\r
1317 Address = *PageEntry & ~mAddressEncMask & PAGING_1G_ADDRESS_MASK_64;\r
1318 Size -= SIZE_1GB - (BaseAddress - Address);\r
1319 BaseAddress += SIZE_1GB - (BaseAddress - Address);\r
1320 break;\r
1321\r
1322 default:\r
1323 return EFI_UNSUPPORTED;\r
1324 }\r
1325\r
1326 MemAttr = *Attributes;\r
1327\r
1328 } while (Size > 0);\r
1329\r
1330 return EFI_SUCCESS;\r
1331}\r
1332\r