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 Create PageTable for SMM use.
34 @return PageTable Address
42 UINTN PageFaultHandlerHookAddress
;
43 IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
;
47 // Initialize spin lock
49 InitializeSpinLock (mPFLock
);
51 mPhysicalAddressBits
= 32;
53 if (FeaturePcdGet (PcdCpuSmmProfileEnable
) ||
54 HEAP_GUARD_NONSTOP_MODE
||
55 NULL_DETECTION_NONSTOP_MODE
) {
57 // Set own Page Fault entry instead of the default one, because SMM Profile
58 // feature depends on IRET instruction to do Single Step
60 PageFaultHandlerHookAddress
= (UINTN
)PageFaultIdtHandlerSmmProfile
;
61 IdtEntry
= (IA32_IDT_GATE_DESCRIPTOR
*) gcSmiIdtr
.Base
;
62 IdtEntry
+= EXCEPT_IA32_PAGE_FAULT
;
63 IdtEntry
->Bits
.OffsetLow
= (UINT16
)PageFaultHandlerHookAddress
;
64 IdtEntry
->Bits
.Reserved_0
= 0;
65 IdtEntry
->Bits
.GateType
= IA32_IDT_GATE_TYPE_INTERRUPT_32
;
66 IdtEntry
->Bits
.OffsetHigh
= (UINT16
)(PageFaultHandlerHookAddress
>> 16);
69 // Register SMM Page Fault Handler
71 Status
= SmmRegisterExceptionHandler (&mSmmCpuService
, EXCEPT_IA32_PAGE_FAULT
, SmiPFHandler
);
72 ASSERT_EFI_ERROR (Status
);
76 // Additional SMM IDT initialization for SMM stack guard
78 if (FeaturePcdGet (PcdCpuSmmStackGuard
)) {
79 InitializeIDTSmmStackGuard ();
81 return Gen4GPageTable (TRUE
);
85 Page Fault handler for SMM use.
97 ThePage Fault handler wrapper for SMM use.
99 @param InterruptType Defines the type of interrupt or exception that
100 occurred on the processor.This parameter is processor architecture specific.
101 @param SystemContext A pointer to the processor context when
102 the interrupt occurred on the processor.
107 IN EFI_EXCEPTION_TYPE InterruptType
,
108 IN EFI_SYSTEM_CONTEXT SystemContext
112 UINTN GuardPageAddress
;
115 ASSERT (InterruptType
== EXCEPT_IA32_PAGE_FAULT
);
117 AcquireSpinLock (mPFLock
);
119 PFAddress
= AsmReadCr2 ();
122 // If a page fault occurs in SMRAM range, it might be in a SMM stack guard page,
123 // or SMM page protection violation.
125 if ((PFAddress
>= mCpuHotPlugData
.SmrrBase
) &&
126 (PFAddress
< (mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
))) {
127 DumpCpuContext (InterruptType
, SystemContext
);
128 CpuIndex
= GetCpuIndex ();
129 GuardPageAddress
= (mSmmStackArrayBase
+ EFI_PAGE_SIZE
+ CpuIndex
* mSmmStackSize
);
130 if ((FeaturePcdGet (PcdCpuSmmStackGuard
)) &&
131 (PFAddress
>= GuardPageAddress
) &&
132 (PFAddress
< (GuardPageAddress
+ EFI_PAGE_SIZE
))) {
133 DEBUG ((DEBUG_ERROR
, "SMM stack overflow!\n"));
135 if ((SystemContext
.SystemContextIa32
->ExceptionData
& IA32_PF_EC_ID
) != 0) {
136 DEBUG ((DEBUG_ERROR
, "SMM exception at execution (0x%x)\n", PFAddress
));
138 DumpModuleInfoByIp (*(UINTN
*)(UINTN
)SystemContext
.SystemContextIa32
->Esp
);
141 DEBUG ((DEBUG_ERROR
, "SMM exception at access (0x%x)\n", PFAddress
));
143 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
147 if (HEAP_GUARD_NONSTOP_MODE
) {
148 GuardPagePFHandler (SystemContext
.SystemContextIa32
->ExceptionData
);
157 // If a page fault occurs in non-SMRAM range.
159 if ((PFAddress
< mCpuHotPlugData
.SmrrBase
) ||
160 (PFAddress
>= mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
)) {
161 if ((SystemContext
.SystemContextIa32
->ExceptionData
& IA32_PF_EC_ID
) != 0) {
162 DumpCpuContext (InterruptType
, SystemContext
);
163 DEBUG ((DEBUG_ERROR
, "Code executed on IP(0x%x) out of SMM range after SMM is locked!\n", PFAddress
));
165 DumpModuleInfoByIp (*(UINTN
*)(UINTN
)SystemContext
.SystemContextIa32
->Esp
);
172 // If NULL pointer was just accessed
174 if ((PcdGet8 (PcdNullPointerDetectionPropertyMask
) & BIT1
) != 0 &&
175 (PFAddress
< EFI_PAGE_SIZE
)) {
176 DumpCpuContext (InterruptType
, SystemContext
);
177 DEBUG ((DEBUG_ERROR
, "!!! NULL pointer access !!!\n"));
179 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
182 if (NULL_DETECTION_NONSTOP_MODE
) {
183 GuardPagePFHandler (SystemContext
.SystemContextIa32
->ExceptionData
);
191 if (IsSmmCommBufferForbiddenAddress (PFAddress
)) {
192 DumpCpuContext (InterruptType
, SystemContext
);
193 DEBUG ((DEBUG_ERROR
, "Access SMM communication forbidden address (0x%x)!\n", PFAddress
));
195 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
202 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
203 SmmProfilePFHandler (
204 SystemContext
.SystemContextIa32
->Eip
,
205 SystemContext
.SystemContextIa32
->ExceptionData
208 DumpCpuContext (InterruptType
, SystemContext
);
209 SmiDefaultPFHandler ();
213 ReleaseSpinLock (mPFLock
);
217 This function sets memory attribute for page table.
220 SetPageTableAttributes (
230 BOOLEAN PageTableSplitted
;
234 // Don't mark page table to read-only if heap guard is enabled.
236 // BIT2: SMM page guard enabled
237 // BIT3: SMM pool guard enabled
239 if ((PcdGet8 (PcdHeapGuardPropertyMask
) & (BIT3
| BIT2
)) != 0) {
240 DEBUG ((DEBUG_INFO
, "Don't mark page table to read-only as heap guard is enabled\n"));
245 // Don't mark page table to read-only if SMM profile is enabled.
247 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
248 DEBUG ((DEBUG_INFO
, "Don't mark page table to read-only as SMM profile is enabled\n"));
252 DEBUG ((DEBUG_INFO
, "SetPageTableAttributes\n"));
255 // Disable write protection, because we need mark page table to be write protected.
256 // We need *write* page table memory, to mark itself to be *read only*.
258 CetEnabled
= ((AsmReadCr4() & CR4_CET_ENABLE
) != 0) ? TRUE
: FALSE
;
261 // CET must be disabled if WP is disabled.
265 AsmWriteCr0 (AsmReadCr0() & ~CR0_WP
);
268 DEBUG ((DEBUG_INFO
, "Start...\n"));
269 PageTableSplitted
= FALSE
;
271 L3PageTable
= (UINT64
*)GetPageTableBase ();
273 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L3PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
274 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
276 for (Index3
= 0; Index3
< 4; Index3
++) {
277 L2PageTable
= (UINT64
*)(UINTN
)(L3PageTable
[Index3
] & ~mAddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
278 if (L2PageTable
== NULL
) {
282 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L2PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
283 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
285 for (Index2
= 0; Index2
< SIZE_4KB
/sizeof(UINT64
); Index2
++) {
286 if ((L2PageTable
[Index2
] & IA32_PG_PS
) != 0) {
290 L1PageTable
= (UINT64
*)(UINTN
)(L2PageTable
[Index2
] & ~mAddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
291 if (L1PageTable
== NULL
) {
294 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L1PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
295 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
298 } while (PageTableSplitted
);
301 // Enable write protection, after page table updated.
303 AsmWriteCr0 (AsmReadCr0() | CR0_WP
);
315 This function returns with no action for 32 bit.
317 @param[out] *Cr2 Pointer to variable to hold CR2 register value.
328 This function returns with no action for 32 bit.
330 @param[in] Cr2 Value to write into CR2 register.