2 Page table manipulation functions for IA-32 processors
4 Copyright (c) 2009 - 2017, 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"
20 Create PageTable for SMM use.
22 @return PageTable Address
30 UINTN PageFaultHandlerHookAddress
;
31 IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
;
35 // Initialize spin lock
37 InitializeSpinLock (mPFLock
);
39 mPhysicalAddressBits
= 32;
41 if (FeaturePcdGet (PcdCpuSmmProfileEnable
) ||
42 HEAP_GUARD_NONSTOP_MODE
||
43 NULL_DETECTION_NONSTOP_MODE
) {
45 // Set own Page Fault entry instead of the default one, because SMM Profile
46 // feature depends on IRET instruction to do Single Step
48 PageFaultHandlerHookAddress
= (UINTN
)PageFaultIdtHandlerSmmProfile
;
49 IdtEntry
= (IA32_IDT_GATE_DESCRIPTOR
*) gcSmiIdtr
.Base
;
50 IdtEntry
+= EXCEPT_IA32_PAGE_FAULT
;
51 IdtEntry
->Bits
.OffsetLow
= (UINT16
)PageFaultHandlerHookAddress
;
52 IdtEntry
->Bits
.Reserved_0
= 0;
53 IdtEntry
->Bits
.GateType
= IA32_IDT_GATE_TYPE_INTERRUPT_32
;
54 IdtEntry
->Bits
.OffsetHigh
= (UINT16
)(PageFaultHandlerHookAddress
>> 16);
57 // Register SMM Page Fault Handler
59 Status
= SmmRegisterExceptionHandler (&mSmmCpuService
, EXCEPT_IA32_PAGE_FAULT
, SmiPFHandler
);
60 ASSERT_EFI_ERROR (Status
);
64 // Additional SMM IDT initialization for SMM stack guard
66 if (FeaturePcdGet (PcdCpuSmmStackGuard
)) {
67 InitializeIDTSmmStackGuard ();
69 return Gen4GPageTable (TRUE
);
73 Page Fault handler for SMM use.
85 ThePage Fault handler wrapper for SMM use.
87 @param InterruptType Defines the type of interrupt or exception that
88 occurred on the processor.This parameter is processor architecture specific.
89 @param SystemContext A pointer to the processor context when
90 the interrupt occurred on the processor.
95 IN EFI_EXCEPTION_TYPE InterruptType
,
96 IN EFI_SYSTEM_CONTEXT SystemContext
100 UINTN GuardPageAddress
;
103 ASSERT (InterruptType
== EXCEPT_IA32_PAGE_FAULT
);
105 AcquireSpinLock (mPFLock
);
107 PFAddress
= AsmReadCr2 ();
110 // If a page fault occurs in SMRAM range, it might be in a SMM stack guard page,
111 // or SMM page protection violation.
113 if ((PFAddress
>= mCpuHotPlugData
.SmrrBase
) &&
114 (PFAddress
< (mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
))) {
115 DumpCpuContext (InterruptType
, SystemContext
);
116 CpuIndex
= GetCpuIndex ();
117 GuardPageAddress
= (mSmmStackArrayBase
+ EFI_PAGE_SIZE
+ CpuIndex
* mSmmStackSize
);
118 if ((FeaturePcdGet (PcdCpuSmmStackGuard
)) &&
119 (PFAddress
>= GuardPageAddress
) &&
120 (PFAddress
< (GuardPageAddress
+ EFI_PAGE_SIZE
))) {
121 DEBUG ((DEBUG_ERROR
, "SMM stack overflow!\n"));
123 if ((SystemContext
.SystemContextIa32
->ExceptionData
& IA32_PF_EC_ID
) != 0) {
124 DEBUG ((DEBUG_ERROR
, "SMM exception at execution (0x%x)\n", PFAddress
));
126 DumpModuleInfoByIp (*(UINTN
*)(UINTN
)SystemContext
.SystemContextIa32
->Esp
);
129 DEBUG ((DEBUG_ERROR
, "SMM exception at access (0x%x)\n", PFAddress
));
131 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
135 if (HEAP_GUARD_NONSTOP_MODE
) {
136 GuardPagePFHandler (SystemContext
.SystemContextIa32
->ExceptionData
);
144 // If a page fault occurs in non-SMRAM range.
146 if ((PFAddress
< mCpuHotPlugData
.SmrrBase
) ||
147 (PFAddress
>= mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
)) {
148 if ((SystemContext
.SystemContextIa32
->ExceptionData
& IA32_PF_EC_ID
) != 0) {
149 DumpCpuContext (InterruptType
, SystemContext
);
150 DEBUG ((DEBUG_ERROR
, "Code executed on IP(0x%x) out of SMM range after SMM is locked!\n", PFAddress
));
152 DumpModuleInfoByIp (*(UINTN
*)(UINTN
)SystemContext
.SystemContextIa32
->Esp
);
158 // If NULL pointer was just accessed
160 if ((PcdGet8 (PcdNullPointerDetectionPropertyMask
) & BIT1
) != 0 &&
161 (PFAddress
< EFI_PAGE_SIZE
)) {
162 DumpCpuContext (InterruptType
, SystemContext
);
163 DEBUG ((DEBUG_ERROR
, "!!! NULL pointer access !!!\n"));
165 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
168 if (NULL_DETECTION_NONSTOP_MODE
) {
169 GuardPagePFHandler (SystemContext
.SystemContextIa32
->ExceptionData
);
176 if (IsSmmCommBufferForbiddenAddress (PFAddress
)) {
177 DumpCpuContext (InterruptType
, SystemContext
);
178 DEBUG ((DEBUG_ERROR
, "Access SMM communication forbidden address (0x%x)!\n", PFAddress
));
180 DumpModuleInfoByIp ((UINTN
)SystemContext
.SystemContextIa32
->Eip
);
186 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
187 SmmProfilePFHandler (
188 SystemContext
.SystemContextIa32
->Eip
,
189 SystemContext
.SystemContextIa32
->ExceptionData
192 DumpCpuContext (InterruptType
, SystemContext
);
193 SmiDefaultPFHandler ();
197 ReleaseSpinLock (mPFLock
);
201 This function sets memory attribute for page table.
204 SetPageTableAttributes (
214 BOOLEAN PageTableSplitted
;
217 // Don't mark page table to read-only if heap guard is enabled.
219 // BIT2: SMM page guard enabled
220 // BIT3: SMM pool guard enabled
222 if ((PcdGet8 (PcdHeapGuardPropertyMask
) & (BIT3
| BIT2
)) != 0) {
223 DEBUG ((DEBUG_INFO
, "Don't mark page table to read-only as heap guard is enabled\n"));
228 // Don't mark page table to read-only if SMM profile is enabled.
230 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
231 DEBUG ((DEBUG_INFO
, "Don't mark page table to read-only as SMM profile is enabled\n"));
235 DEBUG ((DEBUG_INFO
, "SetPageTableAttributes\n"));
238 // Disable write protection, because we need mark page table to be write protected.
239 // We need *write* page table memory, to mark itself to be *read only*.
241 AsmWriteCr0 (AsmReadCr0() & ~CR0_WP
);
244 DEBUG ((DEBUG_INFO
, "Start...\n"));
245 PageTableSplitted
= FALSE
;
247 L3PageTable
= (UINT64
*)GetPageTableBase ();
249 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L3PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
250 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
252 for (Index3
= 0; Index3
< 4; Index3
++) {
253 L2PageTable
= (UINT64
*)(UINTN
)(L3PageTable
[Index3
] & ~mAddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
254 if (L2PageTable
== NULL
) {
258 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L2PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
259 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
261 for (Index2
= 0; Index2
< SIZE_4KB
/sizeof(UINT64
); Index2
++) {
262 if ((L2PageTable
[Index2
] & IA32_PG_PS
) != 0) {
266 L1PageTable
= (UINT64
*)(UINTN
)(L2PageTable
[Index2
] & ~mAddressEncMask
& PAGING_4K_ADDRESS_MASK_64
);
267 if (L1PageTable
== NULL
) {
270 SmmSetMemoryAttributesEx ((EFI_PHYSICAL_ADDRESS
)(UINTN
)L1PageTable
, SIZE_4KB
, EFI_MEMORY_RO
, &IsSplitted
);
271 PageTableSplitted
= (PageTableSplitted
|| IsSplitted
);
274 } while (PageTableSplitted
);
277 // Enable write protection, after page table updated.
279 AsmWriteCr0 (AsmReadCr0() | CR0_WP
);