]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmProfileArch.c
UefiCpuPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / X64 / SmmProfileArch.c
CommitLineData
427e3573
MK
1/** @file\r
2X64 processor specific functions to enable SMM profile.\r
3\r
717fb604 4Copyright (c) 2012 - 2016, Intel Corporation. All rights reserved.<BR>\r
241f9149
LD
5Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
6\r
0acd8697 7SPDX-License-Identifier: BSD-2-Clause-Patent\r
427e3573
MK
8\r
9**/\r
10\r
11#include "PiSmmCpuDxeSmm.h"\r
12#include "SmmProfileInternal.h"\r
13\r
14//\r
15// Current page index.\r
16//\r
17UINTN mPFPageIndex;\r
18\r
19//\r
20// Pool for dynamically creating page table in page fault handler.\r
21//\r
22UINT64 mPFPageBuffer;\r
23\r
24//\r
25// Store the uplink information for each page being used.\r
26//\r
27UINT64 *mPFPageUplink[MAX_PF_PAGE_COUNT];\r
28\r
29/**\r
30 Create SMM page table for S3 path.\r
31\r
32**/\r
33VOID\r
34InitSmmS3Cr3 (\r
35 VOID\r
36 )\r
37{\r
38 EFI_PHYSICAL_ADDRESS Pages;\r
39 UINT64 *PTEntry;\r
40\r
41 //\r
42 // Generate PAE page table for the first 4GB memory space\r
43 //\r
717fb604 44 Pages = Gen4GPageTable (FALSE);\r
427e3573
MK
45\r
46 //\r
47 // Fill Page-Table-Level4 (PML4) entry\r
48 //\r
717fb604
JY
49 PTEntry = (UINT64*)AllocatePageTableMemory (1);\r
50 ASSERT (PTEntry != NULL);\r
241f9149 51 *PTEntry = Pages | mAddressEncMask | PAGE_ATTRIBUTE_BITS;\r
427e3573
MK
52 ZeroMem (PTEntry + 1, EFI_PAGE_SIZE - sizeof (*PTEntry));\r
53\r
54 //\r
55 // Return the address of PML4 (to set CR3)\r
56 //\r
57 mSmmS3ResumeState->SmmS3Cr3 = (UINT32)(UINTN)PTEntry;\r
58\r
59 return ;\r
60}\r
61\r
62/**\r
63 Allocate pages for creating 4KB-page based on 2MB-page when page fault happens.\r
64\r
65**/\r
66VOID\r
67InitPagesForPFHandler (\r
68 VOID\r
69 )\r
70{\r
71 VOID *Address;\r
72\r
73 //\r
74 // Pre-Allocate memory for page fault handler\r
75 //\r
76 Address = NULL;\r
77 Address = AllocatePages (MAX_PF_PAGE_COUNT);\r
ef3e20e3 78 ASSERT (Address != NULL);\r
427e3573
MK
79\r
80 mPFPageBuffer = (UINT64)(UINTN) Address;\r
81 mPFPageIndex = 0;\r
82 ZeroMem ((VOID *) (UINTN) mPFPageBuffer, EFI_PAGE_SIZE * MAX_PF_PAGE_COUNT);\r
83 ZeroMem (mPFPageUplink, sizeof (mPFPageUplink));\r
84\r
85 return;\r
86}\r
87\r
88/**\r
89 Allocate one page for creating 4KB-page based on 2MB-page.\r
90\r
91 @param Uplink The address of Page-Directory entry.\r
92\r
93**/\r
94VOID\r
95AcquirePage (\r
96 UINT64 *Uplink\r
97 )\r
98{\r
99 UINT64 Address;\r
100\r
101 //\r
102 // Get the buffer\r
103 //\r
104 Address = mPFPageBuffer + EFI_PAGES_TO_SIZE (mPFPageIndex);\r
105 ZeroMem ((VOID *) (UINTN) Address, EFI_PAGE_SIZE);\r
106\r
107 //\r
108 // Cut the previous uplink if it exists and wasn't overwritten\r
109 //\r
241f9149 110 if ((mPFPageUplink[mPFPageIndex] != NULL) && ((*mPFPageUplink[mPFPageIndex] & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK) == Address)) {\r
427e3573
MK
111 *mPFPageUplink[mPFPageIndex] = 0;\r
112 }\r
113\r
114 //\r
115 // Link & Record the current uplink\r
116 //\r
241f9149 117 *Uplink = Address | mAddressEncMask | PAGE_ATTRIBUTE_BITS;\r
427e3573
MK
118 mPFPageUplink[mPFPageIndex] = Uplink;\r
119\r
120 mPFPageIndex = (mPFPageIndex + 1) % MAX_PF_PAGE_COUNT;\r
121}\r
122\r
123/**\r
124 Update page table to map the memory correctly in order to make the instruction\r
125 which caused page fault execute successfully. And it also save the original page\r
126 table to be restored in single-step exception.\r
127\r
128 @param PageTable PageTable Address.\r
129 @param PFAddress The memory address which caused page fault exception.\r
130 @param CpuIndex The index of the processor.\r
131 @param ErrorCode The Error code of exception.\r
132 @param IsValidPFAddress The flag indicates if SMM profile data need be added.\r
133\r
134**/\r
135VOID\r
136RestorePageTableAbove4G (\r
137 UINT64 *PageTable,\r
138 UINT64 PFAddress,\r
139 UINTN CpuIndex,\r
140 UINTN ErrorCode,\r
141 BOOLEAN *IsValidPFAddress\r
142 )\r
143{\r
144 UINTN PTIndex;\r
145 UINT64 Address;\r
146 BOOLEAN Nx;\r
147 BOOLEAN Existed;\r
148 UINTN Index;\r
149 UINTN PFIndex;\r
150\r
151 ASSERT ((PageTable != NULL) && (IsValidPFAddress != NULL));\r
152\r
153 //\r
154 // If page fault address is 4GB above.\r
155 //\r
156\r
157 //\r
158 // Check if page fault address has existed in page table.\r
159 // If it exists in page table but page fault is generated,\r
160 // there are 2 possible reasons: 1. present flag is set to 0; 2. instruction fetch in protected memory range.\r
161 //\r
162 Existed = FALSE;\r
163 PageTable = (UINT64*)(AsmReadCr3 () & PHYSICAL_ADDRESS_MASK);\r
164 PTIndex = BitFieldRead64 (PFAddress, 39, 47);\r
165 if ((PageTable[PTIndex] & IA32_PG_P) != 0) {\r
166 // PML4E\r
241f9149 167 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK);\r
427e3573
MK
168 PTIndex = BitFieldRead64 (PFAddress, 30, 38);\r
169 if ((PageTable[PTIndex] & IA32_PG_P) != 0) {\r
170 // PDPTE\r
241f9149 171 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK);\r
427e3573
MK
172 PTIndex = BitFieldRead64 (PFAddress, 21, 29);\r
173 // PD\r
174 if ((PageTable[PTIndex] & IA32_PG_PS) != 0) {\r
175 //\r
176 // 2MB page\r
177 //\r
241f9149
LD
178 Address = (UINT64)(PageTable[PTIndex] & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK);\r
179 if ((Address & ~((1ull << 21) - 1)) == ((PFAddress & PHYSICAL_ADDRESS_MASK & ~((1ull << 21) - 1)))) {\r
427e3573
MK
180 Existed = TRUE;\r
181 }\r
182 } else {\r
183 //\r
184 // 4KB page\r
185 //\r
241f9149 186 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & ~mAddressEncMask& PHYSICAL_ADDRESS_MASK);\r
427e3573
MK
187 if (PageTable != 0) {\r
188 //\r
189 // When there is a valid entry to map to 4KB page, need not create a new entry to map 2MB.\r
190 //\r
191 PTIndex = BitFieldRead64 (PFAddress, 12, 20);\r
241f9149
LD
192 Address = (UINT64)(PageTable[PTIndex] & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK);\r
193 if ((Address & ~((1ull << 12) - 1)) == (PFAddress & PHYSICAL_ADDRESS_MASK & ~((1ull << 12) - 1))) {\r
427e3573
MK
194 Existed = TRUE;\r
195 }\r
196 }\r
197 }\r
198 }\r
199 }\r
200\r
201 //\r
202 // If page entry does not existed in page table at all, create a new entry.\r
203 //\r
204 if (!Existed) {\r
205\r
206 if (IsAddressValid (PFAddress, &Nx)) {\r
207 //\r
208 // If page fault address above 4GB is in protected range but it causes a page fault exception,\r
209 // Will create a page entry for this page fault address, make page table entry as present/rw and execution-disable.\r
210 // this access is not saved into SMM profile data.\r
211 //\r
212 *IsValidPFAddress = TRUE;\r
213 }\r
214\r
215 //\r
216 // Create one entry in page table for page fault address.\r
217 //\r
218 SmiDefaultPFHandler ();\r
219 //\r
220 // Find the page table entry created just now.\r
221 //\r
222 PageTable = (UINT64*)(AsmReadCr3 () & PHYSICAL_ADDRESS_MASK);\r
223 PFAddress = AsmReadCr2 ();\r
224 // PML4E\r
225 PTIndex = BitFieldRead64 (PFAddress, 39, 47);\r
241f9149 226 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK);\r
427e3573
MK
227 // PDPTE\r
228 PTIndex = BitFieldRead64 (PFAddress, 30, 38);\r
241f9149 229 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK);\r
427e3573
MK
230 // PD\r
231 PTIndex = BitFieldRead64 (PFAddress, 21, 29);\r
241f9149 232 Address = PageTable[PTIndex] & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK;\r
427e3573
MK
233 //\r
234 // Check if 2MB-page entry need be changed to 4KB-page entry.\r
235 //\r
236 if (IsAddressSplit (Address)) {\r
237 AcquirePage (&PageTable[PTIndex]);\r
238\r
239 // PTE\r
241f9149 240 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & ~mAddressEncMask & PHYSICAL_ADDRESS_MASK);\r
427e3573 241 for (Index = 0; Index < 512; Index++) {\r
241f9149 242 PageTable[Index] = Address | mAddressEncMask | PAGE_ATTRIBUTE_BITS;\r
427e3573 243 if (!IsAddressValid (Address, &Nx)) {\r
881520ea 244 PageTable[Index] = PageTable[Index] & (INTN)(INT32)(~PAGE_ATTRIBUTE_BITS);\r
427e3573
MK
245 }\r
246 if (Nx && mXdSupported) {\r
247 PageTable[Index] = PageTable[Index] | IA32_PG_NX;\r
248 }\r
249 if (Address == (PFAddress & PHYSICAL_ADDRESS_MASK & ~((1ull << 12) - 1))) {\r
250 PTIndex = Index;\r
251 }\r
252 Address += SIZE_4KB;\r
253 } // end for PT\r
254 } else {\r
255 //\r
256 // Update 2MB page entry.\r
257 //\r
258 if (!IsAddressValid (Address, &Nx)) {\r
259 //\r
260 // Patch to remove present flag and rw flag.\r
261 //\r
881520ea 262 PageTable[PTIndex] = PageTable[PTIndex] & (INTN)(INT32)(~PAGE_ATTRIBUTE_BITS);\r
427e3573
MK
263 }\r
264 //\r
265 // Set XD bit to 1\r
266 //\r
267 if (Nx && mXdSupported) {\r
268 PageTable[PTIndex] = PageTable[PTIndex] | IA32_PG_NX;\r
269 }\r
270 }\r
271 }\r
272\r
273 //\r
274 // Record old entries with non-present status\r
275 // Old entries include the memory which instruction is at and the memory which instruction access.\r
276 //\r
277 //\r
278 ASSERT (mPFEntryCount[CpuIndex] < MAX_PF_ENTRY_COUNT);\r
279 if (mPFEntryCount[CpuIndex] < MAX_PF_ENTRY_COUNT) {\r
280 PFIndex = mPFEntryCount[CpuIndex];\r
281 mLastPFEntryValue[CpuIndex][PFIndex] = PageTable[PTIndex];\r
282 mLastPFEntryPointer[CpuIndex][PFIndex] = &PageTable[PTIndex];\r
283 mPFEntryCount[CpuIndex]++;\r
284 }\r
285\r
286 //\r
287 // Add present flag or clear XD flag to make page fault handler succeed.\r
288 //\r
881520ea 289 PageTable[PTIndex] |= (UINT64)(PAGE_ATTRIBUTE_BITS);\r
427e3573
MK
290 if ((ErrorCode & IA32_PF_EC_ID) != 0) {\r
291 //\r
292 // If page fault is caused by instruction fetch, clear XD bit in the entry.\r
293 //\r
294 PageTable[PTIndex] &= ~IA32_PG_NX;\r
295 }\r
296\r
297 return;\r
298}\r
299\r
300/**\r
301 Clear TF in FLAGS.\r
302\r
303 @param SystemContext A pointer to the processor context when\r
304 the interrupt occurred on the processor.\r
305\r
306**/\r
307VOID\r
308ClearTrapFlag (\r
309 IN OUT EFI_SYSTEM_CONTEXT SystemContext\r
310 )\r
311{\r
312 SystemContext.SystemContextX64->Rflags &= (UINTN) ~BIT8;\r
313}\r