2 Page table manipulation functions for IA-32 processors
4 Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "PiSmmCpuDxeSmm.h"
32 Get page table base address and the depth of the page table.
34 @param[out] Base Page table base address.
35 @param[out] FiveLevels TRUE means 5 level paging. FALSE means 4 level paging.
40 OUT BOOLEAN
*FiveLevels OPTIONAL
43 *Base
= ((mInternalCr3
== 0) ?
44 (AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64
) :
46 if (FiveLevels
!= NULL
) {
52 Create PageTable for SMM use.
54 @return PageTable Address
62 UINTN PageFaultHandlerHookAddress
;
63 IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
;
67 // Initialize spin lock
69 InitializeSpinLock (mPFLock
);
71 mPhysicalAddressBits
= 32;
73 if (FeaturePcdGet (PcdCpuSmmProfileEnable
) ||
74 HEAP_GUARD_NONSTOP_MODE
||
75 NULL_DETECTION_NONSTOP_MODE
) {
77 // Set own Page Fault entry instead of the default one, because SMM Profile
78 // feature depends on IRET instruction to do Single Step
80 PageFaultHandlerHookAddress
= (UINTN
)PageFaultIdtHandlerSmmProfile
;
81 IdtEntry
= (IA32_IDT_GATE_DESCRIPTOR
*) gcSmiIdtr
.Base
;
82 IdtEntry
+= EXCEPT_IA32_PAGE_FAULT
;
83 IdtEntry
->Bits
.OffsetLow
= (UINT16
)PageFaultHandlerHookAddress
;
84 IdtEntry
->Bits
.Reserved_0
= 0;
85 IdtEntry
->Bits
.GateType
= IA32_IDT_GATE_TYPE_INTERRUPT_32
;
86 IdtEntry
->Bits
.OffsetHigh
= (UINT16
)(PageFaultHandlerHookAddress
>> 16);
89 // Register SMM Page Fault Handler
91 Status
= SmmRegisterExceptionHandler (&mSmmCpuService
, EXCEPT_IA32_PAGE_FAULT
, SmiPFHandler
);
92 ASSERT_EFI_ERROR (Status
);
96 // Additional SMM IDT initialization for SMM stack guard
98 if (FeaturePcdGet (PcdCpuSmmStackGuard
)) {
99 InitializeIDTSmmStackGuard ();
101 return Gen4GPageTable (TRUE
);
105 Page Fault handler for SMM use.
109 SmiDefaultPFHandler (
117 ThePage Fault handler wrapper for SMM use.
119 @param InterruptType Defines the type of interrupt or exception that
120 occurred on the processor.This parameter is processor architecture specific.
121 @param SystemContext A pointer to the processor context when
122 the interrupt occurred on the processor.
127 IN EFI_EXCEPTION_TYPE InterruptType
,
128 IN EFI_SYSTEM_CONTEXT SystemContext
132 UINTN GuardPageAddress
;
135 ASSERT (InterruptType
== EXCEPT_IA32_PAGE_FAULT
);
137 AcquireSpinLock (mPFLock
);
139 PFAddress
= AsmReadCr2 ();
142 // If a page fault occurs in SMRAM range, it might be in a SMM stack guard page,
143 // or SMM page protection violation.
145 if ((PFAddress
>= mCpuHotPlugData
.SmrrBase
) &&
146 (PFAddress
< (mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
))) {
147 DumpCpuContext (InterruptType
, SystemContext
);
148 CpuIndex
= GetCpuIndex ();
149 GuardPageAddress
= (mSmmStackArrayBase
+ EFI_PAGE_SIZE
+ CpuIndex
* mSmmStackSize
);
150 if ((FeaturePcdGet (PcdCpuSmmStackGuard
)) &&
151 (PFAddress
>= GuardPageAddress
) &&
152 (PFAddress
< (GuardPageAddress
+ EFI_PAGE_SIZE
))) {
153 DEBUG ((DEBUG_ERROR
, "SMM stack overflow!\n"));
155 if ((SystemContext
.SystemContextIa32
->ExceptionData
& IA32_PF_EC_ID
) != 0) {
156 DEBUG ((DEBUG_ERROR
, "SMM exception at execution (0x%x)\n", PFAddress
));
158 DumpModuleInfoByIp (*(UINTN
*)(UINTN
)SystemContext
.SystemContextIa32
->Esp
);
161 DEBUG ((DEBUG_ERROR
, "SMM exception at access (0x%x)\n", PFAddress
));
163 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
167 if (HEAP_GUARD_NONSTOP_MODE
) {
168 GuardPagePFHandler (SystemContext
.SystemContextIa32
->ExceptionData
);
177 // If a page fault occurs in non-SMRAM range.
179 if ((PFAddress
< mCpuHotPlugData
.SmrrBase
) ||
180 (PFAddress
>= mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
)) {
181 if ((SystemContext
.SystemContextIa32
->ExceptionData
& IA32_PF_EC_ID
) != 0) {
182 DumpCpuContext (InterruptType
, SystemContext
);
183 DEBUG ((DEBUG_ERROR
, "Code executed on IP(0x%x) out of SMM range after SMM is locked!\n", PFAddress
));
185 DumpModuleInfoByIp (*(UINTN
*)(UINTN
)SystemContext
.SystemContextIa32
->Esp
);
192 // If NULL pointer was just accessed
194 if ((PcdGet8 (PcdNullPointerDetectionPropertyMask
) & BIT1
) != 0 &&
195 (PFAddress
< EFI_PAGE_SIZE
)) {
196 DumpCpuContext (InterruptType
, SystemContext
);
197 DEBUG ((DEBUG_ERROR
, "!!! NULL pointer access !!!\n"));
199 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
202 if (NULL_DETECTION_NONSTOP_MODE
) {
203 GuardPagePFHandler (SystemContext
.SystemContextIa32
->ExceptionData
);
211 if (IsSmmCommBufferForbiddenAddress (PFAddress
)) {
212 DumpCpuContext (InterruptType
, SystemContext
);
213 DEBUG ((DEBUG_ERROR
, "Access SMM communication forbidden address (0x%x)!\n", PFAddress
));
215 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
222 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
223 SmmProfilePFHandler (
224 SystemContext
.SystemContextIa32
->Eip
,
225 SystemContext
.SystemContextIa32
->ExceptionData
228 DumpCpuContext (InterruptType
, SystemContext
);
229 SmiDefaultPFHandler ();
233 ReleaseSpinLock (mPFLock
);
237 This function sets memory attribute for page table.
240 SetPageTableAttributes (
251 BOOLEAN PageTableSplitted
;
255 // Don't mark page table to read-only if heap guard is enabled.
257 // BIT2: SMM page guard enabled
258 // BIT3: SMM pool guard enabled
260 if ((PcdGet8 (PcdHeapGuardPropertyMask
) & (BIT3
| BIT2
)) != 0) {
261 DEBUG ((DEBUG_INFO
, "Don't mark page table to read-only as heap guard is enabled\n"));
266 // Don't mark page table to read-only if SMM profile is enabled.
268 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
269 DEBUG ((DEBUG_INFO
, "Don't mark page table to read-only as SMM profile is enabled\n"));
273 DEBUG ((DEBUG_INFO
, "SetPageTableAttributes\n"));
276 // Disable write protection, because we need mark page table to be write protected.
277 // We need *write* page table memory, to mark itself to be *read only*.
279 CetEnabled
= ((AsmReadCr4() & CR4_CET_ENABLE
) != 0) ? TRUE
: FALSE
;
282 // CET must be disabled if WP is disabled.
286 AsmWriteCr0 (AsmReadCr0() & ~CR0_WP
);
289 DEBUG ((DEBUG_INFO
, "Start...\n"));
290 PageTableSplitted
= FALSE
;
292 GetPageTable (&PageTableBase
, NULL
);
293 L3PageTable
= (UINT64
*)PageTableBase
;
295 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)PageTableBase
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
296 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
298 for (Index3
= 0; Index3
< 4; Index3
++) {
299 L2PageTable
= (UINT64
*)(UINTN
)(L3PageTable
[Index3
] & ~mAddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
300 if (L2PageTable
== NULL
) {
304 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L2PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
305 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
307 for (Index2
= 0; Index2
< SIZE_4KB
/sizeof(UINT64
); Index2
++) {
308 if ((L2PageTable
[Index2
] & IA32_PG_PS
) != 0) {
312 L1PageTable
= (UINT64
*)(UINTN
)(L2PageTable
[Index2
] & ~mAddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
313 if (L1PageTable
== NULL
) {
316 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L1PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
317 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
320 } while (PageTableSplitted
);
323 // Enable write protection, after page table updated.
325 AsmWriteCr0 (AsmReadCr0() | CR0_WP
);
337 This function returns with no action for 32 bit.
339 @param[out] *Cr2 Pointer to variable to hold CR2 register value.
350 This function returns with no action for 32 bit.
352 @param[in] Cr2 Value to write into CR2 register.
363 Return whether access to non-SMRAM is restricted.
365 @retval TRUE Access to non-SMRAM is restricted.
366 @retval FALSE Access to non-SMRAM is not restricted.
369 IsRestrictedMemoryAccess (