2 X64 processor specific functions to enable SMM profile.
4 Copyright (c) 2012 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "PiSmmCpuDxeSmm.h"
16 #include "SmmProfileInternal.h"
19 // Current page index.
24 // Pool for dynamically creating page table in page fault handler.
29 // Store the uplink information for each page being used.
31 UINT64
*mPFPageUplink
[MAX_PF_PAGE_COUNT
];
34 Create SMM page table for S3 path.
42 EFI_PHYSICAL_ADDRESS Pages
;
46 // Generate PAE page table for the first 4GB memory space
48 Pages
= Gen4GPageTable (FALSE
);
51 // Fill Page-Table-Level4 (PML4) entry
53 PTEntry
= (UINT64
*)AllocatePageTableMemory (1);
54 ASSERT (PTEntry
!= NULL
);
55 *PTEntry
= Pages
| PAGE_ATTRIBUTE_BITS
;
56 ZeroMem (PTEntry
+ 1, EFI_PAGE_SIZE
- sizeof (*PTEntry
));
59 // Return the address of PML4 (to set CR3)
61 mSmmS3ResumeState
->SmmS3Cr3
= (UINT32
)(UINTN
)PTEntry
;
67 Allocate pages for creating 4KB-page based on 2MB-page when page fault happens.
71 InitPagesForPFHandler (
78 // Pre-Allocate memory for page fault handler
81 Address
= AllocatePages (MAX_PF_PAGE_COUNT
);
82 ASSERT (Address
!= NULL
);
84 mPFPageBuffer
= (UINT64
)(UINTN
) Address
;
86 ZeroMem ((VOID
*) (UINTN
) mPFPageBuffer
, EFI_PAGE_SIZE
* MAX_PF_PAGE_COUNT
);
87 ZeroMem (mPFPageUplink
, sizeof (mPFPageUplink
));
93 Allocate one page for creating 4KB-page based on 2MB-page.
95 @param Uplink The address of Page-Directory entry.
108 Address
= mPFPageBuffer
+ EFI_PAGES_TO_SIZE (mPFPageIndex
);
109 ZeroMem ((VOID
*) (UINTN
) Address
, EFI_PAGE_SIZE
);
112 // Cut the previous uplink if it exists and wasn't overwritten
114 if ((mPFPageUplink
[mPFPageIndex
] != NULL
) && ((*mPFPageUplink
[mPFPageIndex
] & PHYSICAL_ADDRESS_MASK
) == Address
)) {
115 *mPFPageUplink
[mPFPageIndex
] = 0;
119 // Link & Record the current uplink
121 *Uplink
= Address
| PAGE_ATTRIBUTE_BITS
;
122 mPFPageUplink
[mPFPageIndex
] = Uplink
;
124 mPFPageIndex
= (mPFPageIndex
+ 1) % MAX_PF_PAGE_COUNT
;
128 Update page table to map the memory correctly in order to make the instruction
129 which caused page fault execute successfully. And it also save the original page
130 table to be restored in single-step exception.
132 @param PageTable PageTable Address.
133 @param PFAddress The memory address which caused page fault exception.
134 @param CpuIndex The index of the processor.
135 @param ErrorCode The Error code of exception.
136 @param IsValidPFAddress The flag indicates if SMM profile data need be added.
140 RestorePageTableAbove4G (
145 BOOLEAN
*IsValidPFAddress
155 ASSERT ((PageTable
!= NULL
) && (IsValidPFAddress
!= NULL
));
158 // If page fault address is 4GB above.
162 // Check if page fault address has existed in page table.
163 // If it exists in page table but page fault is generated,
164 // there are 2 possible reasons: 1. present flag is set to 0; 2. instruction fetch in protected memory range.
167 PageTable
= (UINT64
*)(AsmReadCr3 () & PHYSICAL_ADDRESS_MASK
);
168 PTIndex
= BitFieldRead64 (PFAddress
, 39, 47);
169 if ((PageTable
[PTIndex
] & IA32_PG_P
) != 0) {
171 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
172 PTIndex
= BitFieldRead64 (PFAddress
, 30, 38);
173 if ((PageTable
[PTIndex
] & IA32_PG_P
) != 0) {
175 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
176 PTIndex
= BitFieldRead64 (PFAddress
, 21, 29);
178 if ((PageTable
[PTIndex
] & IA32_PG_PS
) != 0) {
182 Address
= (UINT64
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
183 if ((Address
& PHYSICAL_ADDRESS_MASK
& ~((1ull << 21) - 1)) == ((PFAddress
& PHYSICAL_ADDRESS_MASK
& ~((1ull << 21) - 1)))) {
190 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
191 if (PageTable
!= 0) {
193 // When there is a valid entry to map to 4KB page, need not create a new entry to map 2MB.
195 PTIndex
= BitFieldRead64 (PFAddress
, 12, 20);
196 Address
= (UINT64
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
197 if ((Address
& PHYSICAL_ADDRESS_MASK
& ~((1ull << 12) - 1)) == (PFAddress
& PHYSICAL_ADDRESS_MASK
& ~((1ull << 12) - 1))) {
206 // If page entry does not existed in page table at all, create a new entry.
210 if (IsAddressValid (PFAddress
, &Nx
)) {
212 // If page fault address above 4GB is in protected range but it causes a page fault exception,
213 // Will create a page entry for this page fault address, make page table entry as present/rw and execution-disable.
214 // this access is not saved into SMM profile data.
216 *IsValidPFAddress
= TRUE
;
220 // Create one entry in page table for page fault address.
222 SmiDefaultPFHandler ();
224 // Find the page table entry created just now.
226 PageTable
= (UINT64
*)(AsmReadCr3 () & PHYSICAL_ADDRESS_MASK
);
227 PFAddress
= AsmReadCr2 ();
229 PTIndex
= BitFieldRead64 (PFAddress
, 39, 47);
230 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
232 PTIndex
= BitFieldRead64 (PFAddress
, 30, 38);
233 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
235 PTIndex
= BitFieldRead64 (PFAddress
, 21, 29);
236 Address
= PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
;
238 // Check if 2MB-page entry need be changed to 4KB-page entry.
240 if (IsAddressSplit (Address
)) {
241 AcquirePage (&PageTable
[PTIndex
]);
244 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
245 for (Index
= 0; Index
< 512; Index
++) {
246 PageTable
[Index
] = Address
| PAGE_ATTRIBUTE_BITS
;
247 if (!IsAddressValid (Address
, &Nx
)) {
248 PageTable
[Index
] = PageTable
[Index
] & (INTN
)(INT32
)(~PAGE_ATTRIBUTE_BITS
);
250 if (Nx
&& mXdSupported
) {
251 PageTable
[Index
] = PageTable
[Index
] | IA32_PG_NX
;
253 if (Address
== (PFAddress
& PHYSICAL_ADDRESS_MASK
& ~((1ull << 12) - 1))) {
260 // Update 2MB page entry.
262 if (!IsAddressValid (Address
, &Nx
)) {
264 // Patch to remove present flag and rw flag.
266 PageTable
[PTIndex
] = PageTable
[PTIndex
] & (INTN
)(INT32
)(~PAGE_ATTRIBUTE_BITS
);
271 if (Nx
&& mXdSupported
) {
272 PageTable
[PTIndex
] = PageTable
[PTIndex
] | IA32_PG_NX
;
278 // Record old entries with non-present status
279 // Old entries include the memory which instruction is at and the memory which instruction access.
282 ASSERT (mPFEntryCount
[CpuIndex
] < MAX_PF_ENTRY_COUNT
);
283 if (mPFEntryCount
[CpuIndex
] < MAX_PF_ENTRY_COUNT
) {
284 PFIndex
= mPFEntryCount
[CpuIndex
];
285 mLastPFEntryValue
[CpuIndex
][PFIndex
] = PageTable
[PTIndex
];
286 mLastPFEntryPointer
[CpuIndex
][PFIndex
] = &PageTable
[PTIndex
];
287 mPFEntryCount
[CpuIndex
]++;
291 // Add present flag or clear XD flag to make page fault handler succeed.
293 PageTable
[PTIndex
] |= (UINT64
)(PAGE_ATTRIBUTE_BITS
);
294 if ((ErrorCode
& IA32_PF_EC_ID
) != 0) {
296 // If page fault is caused by instruction fetch, clear XD bit in the entry.
298 PageTable
[PTIndex
] &= ~IA32_PG_NX
;
307 @param SystemContext A pointer to the processor context when
308 the interrupt occurred on the processor.
313 IN OUT EFI_SYSTEM_CONTEXT SystemContext
316 SystemContext
.SystemContextX64
->Rflags
&= (UINTN
) ~BIT8
;