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 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "PiSmmCpuDxeSmm.h"
38 Create PageTable for SMM use.
40 @return PageTable Address
48 UINTN PageFaultHandlerHookAddress
;
49 IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
;
53 // Initialize spin lock
55 InitializeSpinLock (mPFLock
);
57 mPhysicalAddressBits
= 32;
59 if (FeaturePcdGet (PcdCpuSmmProfileEnable
) ||
60 HEAP_GUARD_NONSTOP_MODE
||
61 NULL_DETECTION_NONSTOP_MODE
) {
63 // Set own Page Fault entry instead of the default one, because SMM Profile
64 // feature depends on IRET instruction to do Single Step
66 PageFaultHandlerHookAddress
= (UINTN
)PageFaultIdtHandlerSmmProfile
;
67 IdtEntry
= (IA32_IDT_GATE_DESCRIPTOR
*) gcSmiIdtr
.Base
;
68 IdtEntry
+= EXCEPT_IA32_PAGE_FAULT
;
69 IdtEntry
->Bits
.OffsetLow
= (UINT16
)PageFaultHandlerHookAddress
;
70 IdtEntry
->Bits
.Reserved_0
= 0;
71 IdtEntry
->Bits
.GateType
= IA32_IDT_GATE_TYPE_INTERRUPT_32
;
72 IdtEntry
->Bits
.OffsetHigh
= (UINT16
)(PageFaultHandlerHookAddress
>> 16);
75 // Register SMM Page Fault Handler
77 Status
= SmmRegisterExceptionHandler (&mSmmCpuService
, EXCEPT_IA32_PAGE_FAULT
, SmiPFHandler
);
78 ASSERT_EFI_ERROR (Status
);
82 // Additional SMM IDT initialization for SMM stack guard
84 if (FeaturePcdGet (PcdCpuSmmStackGuard
)) {
85 InitializeIDTSmmStackGuard ();
87 return Gen4GPageTable (TRUE
);
91 Page Fault handler for SMM use.
103 ThePage Fault handler wrapper for SMM use.
105 @param InterruptType Defines the type of interrupt or exception that
106 occurred on the processor.This parameter is processor architecture specific.
107 @param SystemContext A pointer to the processor context when
108 the interrupt occurred on the processor.
113 IN EFI_EXCEPTION_TYPE InterruptType
,
114 IN EFI_SYSTEM_CONTEXT SystemContext
118 UINTN GuardPageAddress
;
121 ASSERT (InterruptType
== EXCEPT_IA32_PAGE_FAULT
);
123 AcquireSpinLock (mPFLock
);
125 PFAddress
= AsmReadCr2 ();
128 // If a page fault occurs in SMRAM range, it might be in a SMM stack guard page,
129 // or SMM page protection violation.
131 if ((PFAddress
>= mCpuHotPlugData
.SmrrBase
) &&
132 (PFAddress
< (mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
))) {
133 DumpCpuContext (InterruptType
, SystemContext
);
134 CpuIndex
= GetCpuIndex ();
135 GuardPageAddress
= (mSmmStackArrayBase
+ EFI_PAGE_SIZE
+ CpuIndex
* mSmmStackSize
);
136 if ((FeaturePcdGet (PcdCpuSmmStackGuard
)) &&
137 (PFAddress
>= GuardPageAddress
) &&
138 (PFAddress
< (GuardPageAddress
+ EFI_PAGE_SIZE
))) {
139 DEBUG ((DEBUG_ERROR
, "SMM stack overflow!\n"));
141 if ((SystemContext
.SystemContextIa32
->ExceptionData
& IA32_PF_EC_ID
) != 0) {
142 DEBUG ((DEBUG_ERROR
, "SMM exception at execution (0x%x)\n", PFAddress
));
144 DumpModuleInfoByIp (*(UINTN
*)(UINTN
)SystemContext
.SystemContextIa32
->Esp
);
147 DEBUG ((DEBUG_ERROR
, "SMM exception at access (0x%x)\n", PFAddress
));
149 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
153 if (HEAP_GUARD_NONSTOP_MODE
) {
154 GuardPagePFHandler (SystemContext
.SystemContextIa32
->ExceptionData
);
163 // If a page fault occurs in non-SMRAM range.
165 if ((PFAddress
< mCpuHotPlugData
.SmrrBase
) ||
166 (PFAddress
>= mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
)) {
167 if ((SystemContext
.SystemContextIa32
->ExceptionData
& IA32_PF_EC_ID
) != 0) {
168 DumpCpuContext (InterruptType
, SystemContext
);
169 DEBUG ((DEBUG_ERROR
, "Code executed on IP(0x%x) out of SMM range after SMM is locked!\n", PFAddress
));
171 DumpModuleInfoByIp (*(UINTN
*)(UINTN
)SystemContext
.SystemContextIa32
->Esp
);
178 // If NULL pointer was just accessed
180 if ((PcdGet8 (PcdNullPointerDetectionPropertyMask
) & BIT1
) != 0 &&
181 (PFAddress
< EFI_PAGE_SIZE
)) {
182 DumpCpuContext (InterruptType
, SystemContext
);
183 DEBUG ((DEBUG_ERROR
, "!!! NULL pointer access !!!\n"));
185 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
188 if (NULL_DETECTION_NONSTOP_MODE
) {
189 GuardPagePFHandler (SystemContext
.SystemContextIa32
->ExceptionData
);
197 if (IsSmmCommBufferForbiddenAddress (PFAddress
)) {
198 DumpCpuContext (InterruptType
, SystemContext
);
199 DEBUG ((DEBUG_ERROR
, "Access SMM communication forbidden address (0x%x)!\n", PFAddress
));
201 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
208 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
209 SmmProfilePFHandler (
210 SystemContext
.SystemContextIa32
->Eip
,
211 SystemContext
.SystemContextIa32
->ExceptionData
214 DumpCpuContext (InterruptType
, SystemContext
);
215 SmiDefaultPFHandler ();
219 ReleaseSpinLock (mPFLock
);
223 This function sets memory attribute for page table.
226 SetPageTableAttributes (
236 BOOLEAN PageTableSplitted
;
240 // Don't mark page table to read-only if heap guard is enabled.
242 // BIT2: SMM page guard enabled
243 // BIT3: SMM pool guard enabled
245 if ((PcdGet8 (PcdHeapGuardPropertyMask
) & (BIT3
| BIT2
)) != 0) {
246 DEBUG ((DEBUG_INFO
, "Don't mark page table to read-only as heap guard is enabled\n"));
251 // Don't mark page table to read-only if SMM profile is enabled.
253 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
254 DEBUG ((DEBUG_INFO
, "Don't mark page table to read-only as SMM profile is enabled\n"));
258 DEBUG ((DEBUG_INFO
, "SetPageTableAttributes\n"));
261 // Disable write protection, because we need mark page table to be write protected.
262 // We need *write* page table memory, to mark itself to be *read only*.
264 CetEnabled
= ((AsmReadCr4() & CR4_CET_ENABLE
) != 0) ? TRUE
: FALSE
;
267 // CET must be disabled if WP is disabled.
271 AsmWriteCr0 (AsmReadCr0() & ~CR0_WP
);
274 DEBUG ((DEBUG_INFO
, "Start...\n"));
275 PageTableSplitted
= FALSE
;
277 L3PageTable
= (UINT64
*)GetPageTableBase ();
279 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L3PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
280 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
282 for (Index3
= 0; Index3
< 4; Index3
++) {
283 L2PageTable
= (UINT64
*)(UINTN
)(L3PageTable
[Index3
] & ~mAddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
284 if (L2PageTable
== NULL
) {
288 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L2PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
289 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
291 for (Index2
= 0; Index2
< SIZE_4KB
/sizeof(UINT64
); Index2
++) {
292 if ((L2PageTable
[Index2
] & IA32_PG_PS
) != 0) {
296 L1PageTable
= (UINT64
*)(UINTN
)(L2PageTable
[Index2
] & ~mAddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
297 if (L1PageTable
== NULL
) {
300 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L1PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
301 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
304 } while (PageTableSplitted
);
307 // Enable write protection, after page table updated.
309 AsmWriteCr0 (AsmReadCr0() | CR0_WP
);
321 This function returns with no action for 32 bit.
323 @param[out] *Cr2 Pointer to variable to hold CR2 register value.
334 This function returns with no action for 32 bit.
336 @param[in] Cr2 Value to write into CR2 register.