4 Copyright (c) 2012 - 2019, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2017 - 2020, AMD Incorporated. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "PiSmmCpuDxeSmm.h"
12 #include "SmmProfileInternal.h"
14 UINT32 mSmmProfileCr3
;
16 SMM_PROFILE_HEADER
*mSmmProfileBase
;
17 MSR_DS_AREA_STRUCT
*mMsrDsAreaBase
;
19 // The buffer to store SMM profile data.
21 UINTN mSmmProfileSize
;
24 // The buffer to enable branch trace store.
26 UINTN mMsrDsAreaSize
= SMM_PROFILE_DTS_SIZE
;
29 // The flag indicates if execute-disable is supported by processor.
31 BOOLEAN mXdSupported
= TRUE
;
34 // The flag indicates if execute-disable is enabled on processor.
36 BOOLEAN mXdEnabled
= FALSE
;
39 // The flag indicates if BTS is supported by processor.
41 BOOLEAN mBtsSupported
= TRUE
;
44 // The flag indicates if SMM profile starts to record data.
46 BOOLEAN mSmmProfileStart
= FALSE
;
49 // The flag indicates if #DB will be setup in #PF handler.
51 BOOLEAN mSetupDebugTrap
= FALSE
;
54 // Record the page fault exception count for one instruction execution.
58 UINT64 (*mLastPFEntryValue
)[MAX_PF_ENTRY_COUNT
];
59 UINT64
*(*mLastPFEntryPointer
)[MAX_PF_ENTRY_COUNT
];
61 MSR_DS_AREA_STRUCT
**mMsrDsArea
;
62 BRANCH_TRACE_RECORD
**mMsrBTSRecord
;
63 UINTN mBTSRecordNumber
;
64 PEBS_RECORD
**mMsrPEBSRecord
;
67 // These memory ranges are always present, they does not generate the access type of page fault exception,
68 // but they possibly generate instruction fetch type of page fault exception.
70 MEMORY_PROTECTION_RANGE
*mProtectionMemRange
= NULL
;
71 UINTN mProtectionMemRangeCount
= 0;
74 // Some predefined memory ranges.
76 MEMORY_PROTECTION_RANGE mProtectionMemRangeTemplate
[] = {
78 // SMRAM range (to be fixed in runtime).
79 // It is always present and instruction fetches are allowed.
81 {{0x00000000, 0x00000000},TRUE
,FALSE
},
84 // SMM profile data range( to be fixed in runtime).
85 // It is always present and instruction fetches are not allowed.
87 {{0x00000000, 0x00000000},TRUE
,TRUE
},
90 // SMRAM ranges not covered by mCpuHotPlugData.SmrrBase/mCpuHotPlugData.SmrrSiz (to be fixed in runtime).
91 // It is always present and instruction fetches are allowed.
92 // {{0x00000000, 0x00000000},TRUE,FALSE},
96 // Future extended range could be added here.
100 // PCI MMIO ranges (to be added in runtime).
101 // They are always present and instruction fetches are not allowed.
106 // These memory ranges are mapped by 4KB-page instead of 2MB-page.
108 MEMORY_RANGE
*mSplitMemRange
= NULL
;
109 UINTN mSplitMemRangeCount
= 0;
114 UINT32 mSmiCommandPort
;
117 Disable branch trace store.
125 AsmMsrAnd64 (MSR_DEBUG_CTL
, ~((UINT64
)(MSR_DEBUG_CTL_BTS
| MSR_DEBUG_CTL_TR
)));
129 Enable branch trace store.
137 AsmMsrOr64 (MSR_DEBUG_CTL
, (MSR_DEBUG_CTL_BTS
| MSR_DEBUG_CTL_TR
));
141 Get CPU Index from APIC ID.
152 ApicId
= GetApicId ();
154 for (Index
= 0; Index
< mMaxNumberOfCpus
; Index
++) {
155 if (gSmmCpuPrivate
->ProcessorInfo
[Index
].ProcessorId
== ApicId
) {
164 Get the source of IP after execute-disable exception is triggered.
166 @param CpuIndex The index of CPU.
167 @param DestinationIP The destination address.
171 GetSourceFromDestinationOnBts (
176 BRANCH_TRACE_RECORD
*CurrentBTSRecord
;
182 CurrentBTSRecord
= (BRANCH_TRACE_RECORD
*)mMsrDsArea
[CpuIndex
]->BTSIndex
;
183 for (Index
= 0; Index
< mBTSRecordNumber
; Index
++) {
184 if ((UINTN
)CurrentBTSRecord
< (UINTN
)mMsrBTSRecord
[CpuIndex
]) {
188 CurrentBTSRecord
= (BRANCH_TRACE_RECORD
*)((UINTN
)mMsrDsArea
[CpuIndex
]->BTSAbsoluteMaximum
- 1);
191 if (CurrentBTSRecord
->LastBranchTo
== DestinationIP
) {
193 // Good! find 1st one, then find 2nd one.
197 // The first one is DEBUG exception
202 // Good find proper one.
204 return CurrentBTSRecord
->LastBranchFrom
;
214 SMM profile specific INT 1 (single-step) exception handler.
216 @param InterruptType Defines the type of interrupt or exception that
217 occurred on the processor.This parameter is processor architecture specific.
218 @param SystemContext A pointer to the processor context when
219 the interrupt occurred on the processor.
223 DebugExceptionHandler (
224 IN EFI_EXCEPTION_TYPE InterruptType
,
225 IN EFI_SYSTEM_CONTEXT SystemContext
231 if (!mSmmProfileStart
&&
232 !HEAP_GUARD_NONSTOP_MODE
&&
233 !NULL_DETECTION_NONSTOP_MODE
) {
236 CpuIndex
= GetCpuIndex ();
239 // Clear last PF entries
241 for (PFEntry
= 0; PFEntry
< mPFEntryCount
[CpuIndex
]; PFEntry
++) {
242 *mLastPFEntryPointer
[CpuIndex
][PFEntry
] = mLastPFEntryValue
[CpuIndex
][PFEntry
];
246 // Reset page fault exception count for next page fault.
248 mPFEntryCount
[CpuIndex
] = 0;
256 // Clear TF in EFLAGS
258 ClearTrapFlag (SystemContext
);
262 Check if the input address is in SMM ranges.
264 @param[in] Address The input address.
266 @retval TRUE The input address is in SMM.
267 @retval FALSE The input address is not in SMM.
271 IN EFI_PHYSICAL_ADDRESS Address
276 if ((Address
>= mCpuHotPlugData
.SmrrBase
) && (Address
< mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
)) {
279 for (Index
= 0; Index
< mSmmCpuSmramRangeCount
; Index
++) {
280 if (Address
>= mSmmCpuSmramRanges
[Index
].CpuStart
&&
281 Address
< mSmmCpuSmramRanges
[Index
].CpuStart
+ mSmmCpuSmramRanges
[Index
].PhysicalSize
) {
289 Check if the memory address will be mapped by 4KB-page.
291 @param Address The address of Memory.
292 @param Nx The flag indicates if the memory is execute-disable.
297 IN EFI_PHYSICAL_ADDRESS Address
,
303 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
305 // Check configuration
307 for (Index
= 0; Index
< mProtectionMemRangeCount
; Index
++) {
308 if ((Address
>= mProtectionMemRange
[Index
].Range
.Base
) && (Address
< mProtectionMemRange
[Index
].Range
.Top
)) {
309 *Nx
= mProtectionMemRange
[Index
].Nx
;
310 return mProtectionMemRange
[Index
].Present
;
318 if (IsInSmmRanges (Address
)) {
326 Check if the memory address will be mapped by 4KB-page.
328 @param Address The address of Memory.
333 IN EFI_PHYSICAL_ADDRESS Address
338 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
340 // Check configuration
342 for (Index
= 0; Index
< mSplitMemRangeCount
; Index
++) {
343 if ((Address
>= mSplitMemRange
[Index
].Base
) && (Address
< mSplitMemRange
[Index
].Top
)) {
348 if (Address
< mCpuHotPlugData
.SmrrBase
) {
349 if ((mCpuHotPlugData
.SmrrBase
- Address
) < BASE_2MB
) {
352 } else if (Address
> (mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
- BASE_2MB
)) {
353 if ((Address
- (mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
- BASE_2MB
)) < BASE_2MB
) {
365 Initialize the protected memory ranges and the 4KB-page mapped memory ranges.
369 InitProtectedMemRange (
374 UINTN NumberOfDescriptors
;
375 UINTN NumberOfAddedDescriptors
;
376 UINTN NumberOfProtectRange
;
377 UINTN NumberOfSpliteRange
;
378 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
380 EFI_PHYSICAL_ADDRESS ProtectBaseAddress
;
381 EFI_PHYSICAL_ADDRESS ProtectEndAddress
;
382 EFI_PHYSICAL_ADDRESS Top2MBAlignedAddress
;
383 EFI_PHYSICAL_ADDRESS Base2MBAlignedAddress
;
384 UINT64 High4KBPageSize
;
385 UINT64 Low4KBPageSize
;
387 NumberOfDescriptors
= 0;
388 NumberOfAddedDescriptors
= mSmmCpuSmramRangeCount
;
389 NumberOfSpliteRange
= 0;
390 MemorySpaceMap
= NULL
;
393 // Get MMIO ranges from GCD and add them into protected memory ranges.
395 gDS
->GetMemorySpaceMap (
396 &NumberOfDescriptors
,
399 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
400 if (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
401 NumberOfAddedDescriptors
++;
405 if (NumberOfAddedDescriptors
!= 0) {
406 TotalSize
= NumberOfAddedDescriptors
* sizeof (MEMORY_PROTECTION_RANGE
) + sizeof (mProtectionMemRangeTemplate
);
407 mProtectionMemRange
= (MEMORY_PROTECTION_RANGE
*) AllocateZeroPool (TotalSize
);
408 ASSERT (mProtectionMemRange
!= NULL
);
409 mProtectionMemRangeCount
= TotalSize
/ sizeof (MEMORY_PROTECTION_RANGE
);
412 // Copy existing ranges.
414 CopyMem (mProtectionMemRange
, mProtectionMemRangeTemplate
, sizeof (mProtectionMemRangeTemplate
));
417 // Create split ranges which come from protected ranges.
419 TotalSize
= (TotalSize
/ sizeof (MEMORY_PROTECTION_RANGE
)) * sizeof (MEMORY_RANGE
);
420 mSplitMemRange
= (MEMORY_RANGE
*) AllocateZeroPool (TotalSize
);
421 ASSERT (mSplitMemRange
!= NULL
);
424 // Create SMM ranges which are set to present and execution-enable.
426 NumberOfProtectRange
= sizeof (mProtectionMemRangeTemplate
) / sizeof (MEMORY_PROTECTION_RANGE
);
427 for (Index
= 0; Index
< mSmmCpuSmramRangeCount
; Index
++) {
428 if (mSmmCpuSmramRanges
[Index
].CpuStart
>= mProtectionMemRange
[0].Range
.Base
&&
429 mSmmCpuSmramRanges
[Index
].CpuStart
+ mSmmCpuSmramRanges
[Index
].PhysicalSize
< mProtectionMemRange
[0].Range
.Top
) {
431 // If the address have been already covered by mCpuHotPlugData.SmrrBase/mCpuHotPlugData.SmrrSiz
435 mProtectionMemRange
[NumberOfProtectRange
].Range
.Base
= mSmmCpuSmramRanges
[Index
].CpuStart
;
436 mProtectionMemRange
[NumberOfProtectRange
].Range
.Top
= mSmmCpuSmramRanges
[Index
].CpuStart
+ mSmmCpuSmramRanges
[Index
].PhysicalSize
;
437 mProtectionMemRange
[NumberOfProtectRange
].Present
= TRUE
;
438 mProtectionMemRange
[NumberOfProtectRange
].Nx
= FALSE
;
439 NumberOfProtectRange
++;
443 // Create MMIO ranges which are set to present and execution-disable.
445 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
446 if (MemorySpaceMap
[Index
].GcdMemoryType
!= EfiGcdMemoryTypeMemoryMappedIo
) {
449 mProtectionMemRange
[NumberOfProtectRange
].Range
.Base
= MemorySpaceMap
[Index
].BaseAddress
;
450 mProtectionMemRange
[NumberOfProtectRange
].Range
.Top
= MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
;
451 mProtectionMemRange
[NumberOfProtectRange
].Present
= TRUE
;
452 mProtectionMemRange
[NumberOfProtectRange
].Nx
= TRUE
;
453 NumberOfProtectRange
++;
457 // Check and updated actual protected memory ranges count
459 ASSERT (NumberOfProtectRange
<= mProtectionMemRangeCount
);
460 mProtectionMemRangeCount
= NumberOfProtectRange
;
464 // According to protected ranges, create the ranges which will be mapped by 2KB page.
466 NumberOfSpliteRange
= 0;
467 NumberOfProtectRange
= mProtectionMemRangeCount
;
468 for (Index
= 0; Index
< NumberOfProtectRange
; Index
++) {
470 // If MMIO base address is not 2MB alignment, make 2MB alignment for create 4KB page in page table.
472 ProtectBaseAddress
= mProtectionMemRange
[Index
].Range
.Base
;
473 ProtectEndAddress
= mProtectionMemRange
[Index
].Range
.Top
;
474 if (((ProtectBaseAddress
& (SIZE_2MB
- 1)) != 0) || ((ProtectEndAddress
& (SIZE_2MB
- 1)) != 0)) {
476 // Check if it is possible to create 4KB-page for not 2MB-aligned range and to create 2MB-page for 2MB-aligned range.
477 // A mix of 4KB and 2MB page could save SMRAM space.
479 Top2MBAlignedAddress
= ProtectEndAddress
& ~(SIZE_2MB
- 1);
480 Base2MBAlignedAddress
= (ProtectBaseAddress
+ SIZE_2MB
- 1) & ~(SIZE_2MB
- 1);
481 if ((Top2MBAlignedAddress
> Base2MBAlignedAddress
) &&
482 ((Top2MBAlignedAddress
- Base2MBAlignedAddress
) >= SIZE_2MB
)) {
484 // There is an range which could be mapped by 2MB-page.
486 High4KBPageSize
= ((ProtectEndAddress
+ SIZE_2MB
- 1) & ~(SIZE_2MB
- 1)) - (ProtectEndAddress
& ~(SIZE_2MB
- 1));
487 Low4KBPageSize
= ((ProtectBaseAddress
+ SIZE_2MB
- 1) & ~(SIZE_2MB
- 1)) - (ProtectBaseAddress
& ~(SIZE_2MB
- 1));
488 if (High4KBPageSize
!= 0) {
490 // Add not 2MB-aligned range to be mapped by 4KB-page.
492 mSplitMemRange
[NumberOfSpliteRange
].Base
= ProtectEndAddress
& ~(SIZE_2MB
- 1);
493 mSplitMemRange
[NumberOfSpliteRange
].Top
= (ProtectEndAddress
+ SIZE_2MB
- 1) & ~(SIZE_2MB
- 1);
494 NumberOfSpliteRange
++;
496 if (Low4KBPageSize
!= 0) {
498 // Add not 2MB-aligned range to be mapped by 4KB-page.
500 mSplitMemRange
[NumberOfSpliteRange
].Base
= ProtectBaseAddress
& ~(SIZE_2MB
- 1);
501 mSplitMemRange
[NumberOfSpliteRange
].Top
= (ProtectBaseAddress
+ SIZE_2MB
- 1) & ~(SIZE_2MB
- 1);
502 NumberOfSpliteRange
++;
506 // The range could only be mapped by 4KB-page.
508 mSplitMemRange
[NumberOfSpliteRange
].Base
= ProtectBaseAddress
& ~(SIZE_2MB
- 1);
509 mSplitMemRange
[NumberOfSpliteRange
].Top
= (ProtectEndAddress
+ SIZE_2MB
- 1) & ~(SIZE_2MB
- 1);
510 NumberOfSpliteRange
++;
515 mSplitMemRangeCount
= NumberOfSpliteRange
;
517 DEBUG ((DEBUG_INFO
, "SMM Profile Memory Ranges:\n"));
518 for (Index
= 0; Index
< mProtectionMemRangeCount
; Index
++) {
519 DEBUG ((DEBUG_INFO
, "mProtectionMemRange[%d].Base = %lx\n", Index
, mProtectionMemRange
[Index
].Range
.Base
));
520 DEBUG ((DEBUG_INFO
, "mProtectionMemRange[%d].Top = %lx\n", Index
, mProtectionMemRange
[Index
].Range
.Top
));
522 for (Index
= 0; Index
< mSplitMemRangeCount
; Index
++) {
523 DEBUG ((DEBUG_INFO
, "mSplitMemRange[%d].Base = %lx\n", Index
, mSplitMemRange
[Index
].Base
));
524 DEBUG ((DEBUG_INFO
, "mSplitMemRange[%d].Top = %lx\n", Index
, mSplitMemRange
[Index
].Top
));
529 Update page table according to protected memory ranges and the 4KB-page mapped memory ranges.
550 UINTN NumberOfPdptEntries
;
551 UINTN NumberOfPml4Entries
;
552 UINTN NumberOfPml5Entries
;
553 UINTN SizeOfMemorySpace
;
556 BOOLEAN Enable5LevelPaging
;
558 Cr4
.UintN
= AsmReadCr4 ();
559 Enable5LevelPaging
= (BOOLEAN
) (Cr4
.Bits
.LA57
== 1);
561 if (sizeof (UINTN
) == sizeof (UINT64
)) {
562 if (!Enable5LevelPaging
) {
563 Pml5Entry
= (UINTN
) mSmmProfileCr3
| IA32_PG_P
;
566 Pml5
= (UINT64
*) (UINTN
) mSmmProfileCr3
;
568 SizeOfMemorySpace
= HighBitSet64 (gPhyMask
) + 1;
570 // Calculate the table entries of PML4E and PDPTE.
572 NumberOfPml5Entries
= 1;
573 if (SizeOfMemorySpace
> 48) {
574 NumberOfPml5Entries
= (UINTN
) LShiftU64 (1, SizeOfMemorySpace
- 48);
575 SizeOfMemorySpace
= 48;
578 NumberOfPml4Entries
= 1;
579 if (SizeOfMemorySpace
> 39) {
580 NumberOfPml4Entries
= (UINTN
) LShiftU64 (1, SizeOfMemorySpace
- 39);
581 SizeOfMemorySpace
= 39;
584 NumberOfPdptEntries
= 1;
585 ASSERT (SizeOfMemorySpace
> 30);
586 NumberOfPdptEntries
= (UINTN
) LShiftU64 (1, SizeOfMemorySpace
- 30);
588 Pml4Entry
= (UINTN
) mSmmProfileCr3
| IA32_PG_P
;
590 Pml5Entry
= (UINTN
) Pml4
| IA32_PG_P
;
592 NumberOfPml5Entries
= 1;
593 NumberOfPml4Entries
= 1;
594 NumberOfPdptEntries
= 4;
598 // Go through page table and change 2MB-page into 4KB-page.
600 for (Pml5Index
= 0; Pml5Index
< NumberOfPml5Entries
; Pml5Index
++) {
601 if ((Pml5
[Pml5Index
] & IA32_PG_P
) == 0) {
603 // If PML5 entry does not exist, skip it
607 Pml4
= (UINT64
*) (UINTN
) (Pml5
[Pml5Index
] & PHYSICAL_ADDRESS_MASK
);
608 for (Pml4Index
= 0; Pml4Index
< NumberOfPml4Entries
; Pml4Index
++) {
609 if ((Pml4
[Pml4Index
] & IA32_PG_P
) == 0) {
611 // If PML4 entry does not exist, skip it
615 Pdpt
= (UINT64
*)(UINTN
)(Pml4
[Pml4Index
] & ~mAddressEncMask
& PHYSICAL_ADDRESS_MASK
);
616 for (PdptIndex
= 0; PdptIndex
< NumberOfPdptEntries
; PdptIndex
++, Pdpt
++) {
617 if ((*Pdpt
& IA32_PG_P
) == 0) {
619 // If PDPT entry does not exist, skip it
623 if ((*Pdpt
& IA32_PG_PS
) != 0) {
625 // This is 1G entry, skip it
629 Pd
= (UINT64
*)(UINTN
)(*Pdpt
& ~mAddressEncMask
& PHYSICAL_ADDRESS_MASK
);
633 for (PdIndex
= 0; PdIndex
< SIZE_4KB
/ sizeof (*Pd
); PdIndex
++, Pd
++) {
634 if ((*Pd
& IA32_PG_P
) == 0) {
636 // If PD entry does not exist, skip it
640 Address
= (UINTN
) LShiftU64 (
642 LShiftU64 ((Pml5Index
<< 9) + Pml4Index
, 9) + PdptIndex
,
649 // If it is 2M page, check IsAddressSplit()
651 if (((*Pd
& IA32_PG_PS
) != 0) && IsAddressSplit (Address
)) {
653 // Based on current page table, create 4KB page table for split area.
655 ASSERT (Address
== (*Pd
& PHYSICAL_ADDRESS_MASK
));
657 Pt
= AllocatePageTableMemory (1);
661 for (PtIndex
= 0; PtIndex
< SIZE_4KB
/ sizeof(*Pt
); PtIndex
++) {
662 Pt
[PtIndex
] = Address
+ ((PtIndex
<< 12) | mAddressEncMask
| PAGE_ATTRIBUTE_BITS
);
664 *Pd
= (UINT64
)(UINTN
)Pt
| mAddressEncMask
| PAGE_ATTRIBUTE_BITS
;
665 } // end if IsAddressSplit
672 // Go through page table and set several page table entries to absent or execute-disable.
674 DEBUG ((DEBUG_INFO
, "Patch page table start ...\n"));
675 for (Pml5Index
= 0; Pml5Index
< NumberOfPml5Entries
; Pml5Index
++) {
676 if ((Pml5
[Pml5Index
] & IA32_PG_P
) == 0) {
678 // If PML5 entry does not exist, skip it
682 Pml4
= (UINT64
*) (UINTN
) (Pml5
[Pml5Index
] & PHYSICAL_ADDRESS_MASK
);
683 for (Pml4Index
= 0; Pml4Index
< NumberOfPml4Entries
; Pml4Index
++) {
684 if ((Pml4
[Pml4Index
] & IA32_PG_P
) == 0) {
686 // If PML4 entry does not exist, skip it
690 Pdpt
= (UINT64
*)(UINTN
)(Pml4
[Pml4Index
] & ~mAddressEncMask
& PHYSICAL_ADDRESS_MASK
);
691 for (PdptIndex
= 0; PdptIndex
< NumberOfPdptEntries
; PdptIndex
++, Pdpt
++) {
692 if ((*Pdpt
& IA32_PG_P
) == 0) {
694 // If PDPT entry does not exist, skip it
698 if ((*Pdpt
& IA32_PG_PS
) != 0) {
700 // This is 1G entry, set NX bit and skip it
703 *Pdpt
= *Pdpt
| IA32_PG_NX
;
707 Pd
= (UINT64
*)(UINTN
)(*Pdpt
& ~mAddressEncMask
& PHYSICAL_ADDRESS_MASK
);
711 for (PdIndex
= 0; PdIndex
< SIZE_4KB
/ sizeof (*Pd
); PdIndex
++, Pd
++) {
712 if ((*Pd
& IA32_PG_P
) == 0) {
714 // If PD entry does not exist, skip it
718 Address
= (UINTN
) LShiftU64 (
720 LShiftU64 ((Pml5Index
<< 9) + Pml4Index
, 9) + PdptIndex
,
726 if ((*Pd
& IA32_PG_PS
) != 0) {
729 if (!IsAddressValid (Address
, &Nx
)) {
731 // Patch to remove Present flag and RW flag
733 *Pd
= *Pd
& (INTN
)(INT32
)(~PAGE_ATTRIBUTE_BITS
);
735 if (Nx
&& mXdSupported
) {
736 *Pd
= *Pd
| IA32_PG_NX
;
740 Pt
= (UINT64
*)(UINTN
)(*Pd
& ~mAddressEncMask
& PHYSICAL_ADDRESS_MASK
);
744 for (PtIndex
= 0; PtIndex
< SIZE_4KB
/ sizeof(*Pt
); PtIndex
++, Pt
++) {
745 if (!IsAddressValid (Address
, &Nx
)) {
746 *Pt
= *Pt
& (INTN
)(INT32
)(~PAGE_ATTRIBUTE_BITS
);
748 if (Nx
&& mXdSupported
) {
749 *Pt
= *Pt
| IA32_PG_NX
;
763 DEBUG ((DEBUG_INFO
, "Patch page table done!\n"));
765 // Set execute-disable flag
773 To get system port address of the SMI Command Port in FADT table.
781 EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE
*Fadt
;
783 Fadt
= (EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE
*) EfiLocateFirstAcpiTable (
784 EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE
786 ASSERT (Fadt
!= NULL
);
788 mSmiCommandPort
= Fadt
->SmiCmd
;
789 DEBUG ((DEBUG_INFO
, "mSmiCommandPort = %x\n", mSmiCommandPort
));
793 Updates page table to make some memory ranges (like system memory) absent
794 and make some memory ranges (like MMIO) present and execute disable. It also
795 update 2MB-page to 4KB-page for some memory ranges.
804 // The flag indicates SMM profile starts to work.
806 mSmmProfileStart
= TRUE
;
810 Initialize SMM profile in SmmReadyToLock protocol callback function.
812 @param Protocol Points to the protocol's unique identifier.
813 @param Interface Points to the interface instance.
814 @param Handle The handle on which the interface was installed.
816 @retval EFI_SUCCESS SmmReadyToLock protocol callback runs successfully.
820 InitSmmProfileCallBack (
821 IN CONST EFI_GUID
*Protocol
,
827 // Save to variable so that SMM profile data can be found.
832 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
833 sizeof(mSmmProfileBase
),
838 // Get Software SMI from FADT
840 GetSmiCommandPort ();
843 // Initialize protected memory range for patching page table later.
845 InitProtectedMemRange ();
851 Initialize SMM profile data structures.
855 InitSmmProfileInternal (
860 EFI_PHYSICAL_ADDRESS Base
;
863 UINTN MsrDsAreaSizePerCpu
;
866 mPFEntryCount
= (UINTN
*)AllocateZeroPool (sizeof (UINTN
) * mMaxNumberOfCpus
);
867 ASSERT (mPFEntryCount
!= NULL
);
868 mLastPFEntryValue
= (UINT64 (*)[MAX_PF_ENTRY_COUNT
])AllocateZeroPool (
869 sizeof (mLastPFEntryValue
[0]) * mMaxNumberOfCpus
);
870 ASSERT (mLastPFEntryValue
!= NULL
);
871 mLastPFEntryPointer
= (UINT64
*(*)[MAX_PF_ENTRY_COUNT
])AllocateZeroPool (
872 sizeof (mLastPFEntryPointer
[0]) * mMaxNumberOfCpus
);
873 ASSERT (mLastPFEntryPointer
!= NULL
);
876 // Allocate memory for SmmProfile below 4GB.
879 mSmmProfileSize
= PcdGet32 (PcdCpuSmmProfileSize
);
880 ASSERT ((mSmmProfileSize
& 0xFFF) == 0);
883 TotalSize
= mSmmProfileSize
+ mMsrDsAreaSize
;
885 TotalSize
= mSmmProfileSize
;
889 Status
= gBS
->AllocatePages (
891 EfiReservedMemoryType
,
892 EFI_SIZE_TO_PAGES (TotalSize
),
895 ASSERT_EFI_ERROR (Status
);
896 ZeroMem ((VOID
*)(UINTN
)Base
, TotalSize
);
897 mSmmProfileBase
= (SMM_PROFILE_HEADER
*)(UINTN
)Base
;
900 // Initialize SMM profile data header.
902 mSmmProfileBase
->HeaderSize
= sizeof (SMM_PROFILE_HEADER
);
903 mSmmProfileBase
->MaxDataEntries
= (UINT64
)((mSmmProfileSize
- sizeof(SMM_PROFILE_HEADER
)) / sizeof (SMM_PROFILE_ENTRY
));
904 mSmmProfileBase
->MaxDataSize
= MultU64x64 (mSmmProfileBase
->MaxDataEntries
, sizeof(SMM_PROFILE_ENTRY
));
905 mSmmProfileBase
->CurDataEntries
= 0;
906 mSmmProfileBase
->CurDataSize
= 0;
907 mSmmProfileBase
->TsegStart
= mCpuHotPlugData
.SmrrBase
;
908 mSmmProfileBase
->TsegSize
= mCpuHotPlugData
.SmrrSize
;
909 mSmmProfileBase
->NumSmis
= 0;
910 mSmmProfileBase
->NumCpus
= gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
;
913 mMsrDsArea
= (MSR_DS_AREA_STRUCT
**)AllocateZeroPool (sizeof (MSR_DS_AREA_STRUCT
*) * mMaxNumberOfCpus
);
914 ASSERT (mMsrDsArea
!= NULL
);
915 mMsrBTSRecord
= (BRANCH_TRACE_RECORD
**)AllocateZeroPool (sizeof (BRANCH_TRACE_RECORD
*) * mMaxNumberOfCpus
);
916 ASSERT (mMsrBTSRecord
!= NULL
);
917 mMsrPEBSRecord
= (PEBS_RECORD
**)AllocateZeroPool (sizeof (PEBS_RECORD
*) * mMaxNumberOfCpus
);
918 ASSERT (mMsrPEBSRecord
!= NULL
);
920 mMsrDsAreaBase
= (MSR_DS_AREA_STRUCT
*)((UINTN
)Base
+ mSmmProfileSize
);
921 MsrDsAreaSizePerCpu
= mMsrDsAreaSize
/ mMaxNumberOfCpus
;
922 mBTSRecordNumber
= (MsrDsAreaSizePerCpu
- sizeof(PEBS_RECORD
) * PEBS_RECORD_NUMBER
- sizeof(MSR_DS_AREA_STRUCT
)) / sizeof(BRANCH_TRACE_RECORD
);
923 for (Index
= 0; Index
< mMaxNumberOfCpus
; Index
++) {
924 mMsrDsArea
[Index
] = (MSR_DS_AREA_STRUCT
*)((UINTN
)mMsrDsAreaBase
+ MsrDsAreaSizePerCpu
* Index
);
925 mMsrBTSRecord
[Index
] = (BRANCH_TRACE_RECORD
*)((UINTN
)mMsrDsArea
[Index
] + sizeof(MSR_DS_AREA_STRUCT
));
926 mMsrPEBSRecord
[Index
] = (PEBS_RECORD
*)((UINTN
)mMsrDsArea
[Index
] + MsrDsAreaSizePerCpu
- sizeof(PEBS_RECORD
) * PEBS_RECORD_NUMBER
);
928 mMsrDsArea
[Index
]->BTSBufferBase
= (UINTN
)mMsrBTSRecord
[Index
];
929 mMsrDsArea
[Index
]->BTSIndex
= mMsrDsArea
[Index
]->BTSBufferBase
;
930 mMsrDsArea
[Index
]->BTSAbsoluteMaximum
= mMsrDsArea
[Index
]->BTSBufferBase
+ mBTSRecordNumber
* sizeof(BRANCH_TRACE_RECORD
) + 1;
931 mMsrDsArea
[Index
]->BTSInterruptThreshold
= mMsrDsArea
[Index
]->BTSAbsoluteMaximum
+ 1;
933 mMsrDsArea
[Index
]->PEBSBufferBase
= (UINTN
)mMsrPEBSRecord
[Index
];
934 mMsrDsArea
[Index
]->PEBSIndex
= mMsrDsArea
[Index
]->PEBSBufferBase
;
935 mMsrDsArea
[Index
]->PEBSAbsoluteMaximum
= mMsrDsArea
[Index
]->PEBSBufferBase
+ PEBS_RECORD_NUMBER
* sizeof(PEBS_RECORD
) + 1;
936 mMsrDsArea
[Index
]->PEBSInterruptThreshold
= mMsrDsArea
[Index
]->PEBSAbsoluteMaximum
+ 1;
940 mProtectionMemRange
= mProtectionMemRangeTemplate
;
941 mProtectionMemRangeCount
= sizeof (mProtectionMemRangeTemplate
) / sizeof (MEMORY_PROTECTION_RANGE
);
944 // Update TSeg entry.
946 mProtectionMemRange
[0].Range
.Base
= mCpuHotPlugData
.SmrrBase
;
947 mProtectionMemRange
[0].Range
.Top
= mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
;
950 // Update SMM profile entry.
952 mProtectionMemRange
[1].Range
.Base
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)mSmmProfileBase
;
953 mProtectionMemRange
[1].Range
.Top
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)mSmmProfileBase
+ TotalSize
;
956 // Allocate memory reserved for creating 4KB pages.
958 InitPagesForPFHandler ();
961 // Start SMM profile when SmmReadyToLock protocol is installed.
963 Status
= gSmst
->SmmRegisterProtocolNotify (
964 &gEfiSmmReadyToLockProtocolGuid
,
965 InitSmmProfileCallBack
,
968 ASSERT_EFI_ERROR (Status
);
974 Check if feature is supported by a processor.
978 CheckFeatureSupported (
985 MSR_IA32_MISC_ENABLE_REGISTER MiscEnableMsr
;
987 if ((PcdGet32 (PcdControlFlowEnforcementPropertyMask
) != 0) && mCetSupported
) {
988 AsmCpuid (CPUID_SIGNATURE
, &RegEax
, NULL
, NULL
, NULL
);
989 if (RegEax
>= CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS
) {
990 AsmCpuidEx (CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS
, CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_SUB_LEAF_INFO
, NULL
, NULL
, &RegEcx
, NULL
);
991 if ((RegEcx
& CPUID_CET_SS
) == 0) {
992 mCetSupported
= FALSE
;
993 PatchInstructionX86 (mPatchCetSupported
, mCetSupported
, 1);
996 mCetSupported
= FALSE
;
997 PatchInstructionX86 (mPatchCetSupported
, mCetSupported
, 1);
1002 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &RegEax
, NULL
, NULL
, NULL
);
1003 if (RegEax
<= CPUID_EXTENDED_FUNCTION
) {
1005 // Extended CPUID functions are not supported on this processor.
1007 mXdSupported
= FALSE
;
1008 PatchInstructionX86 (gPatchXdSupported
, mXdSupported
, 1);
1011 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &RegEdx
);
1012 if ((RegEdx
& CPUID1_EDX_XD_SUPPORT
) == 0) {
1014 // Execute Disable Bit feature is not supported on this processor.
1016 mXdSupported
= FALSE
;
1017 PatchInstructionX86 (gPatchXdSupported
, mXdSupported
, 1);
1020 if (StandardSignatureIsAuthenticAMD ()) {
1022 // AMD processors do not support MSR_IA32_MISC_ENABLE
1024 PatchInstructionX86 (gPatchMsrIa32MiscEnableSupported
, FALSE
, 1);
1028 if (mBtsSupported
) {
1029 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &RegEdx
);
1030 if ((RegEdx
& CPUID1_EDX_BTS_AVAILABLE
) != 0) {
1032 // Per IA32 manuals:
1033 // When CPUID.1:EDX[21] is set, the following BTS facilities are available:
1034 // 1. The BTS_UNAVAILABLE flag in the IA32_MISC_ENABLE MSR indicates the
1035 // availability of the BTS facilities, including the ability to set the BTS and
1036 // BTINT bits in the MSR_DEBUGCTLA MSR.
1037 // 2. The IA32_DS_AREA MSR can be programmed to point to the DS save area.
1039 MiscEnableMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_MISC_ENABLE
);
1040 if (MiscEnableMsr
.Bits
.BTS
== 1) {
1042 // BTS facilities is not supported if MSR_IA32_MISC_ENABLE.BTS bit is set.
1044 mBtsSupported
= FALSE
;
1055 ActivateSingleStepDB (
1061 Dr6
= AsmReadDr6 ();
1062 if ((Dr6
& DR6_SINGLE_STEP
) != 0) {
1065 Dr6
|= DR6_SINGLE_STEP
;
1080 DebugCtl
= AsmReadMsr64 (MSR_DEBUG_CTL
);
1081 if ((DebugCtl
& MSR_DEBUG_CTL_LBR
) != 0) {
1084 DebugCtl
|= MSR_DEBUG_CTL_LBR
;
1085 AsmWriteMsr64 (MSR_DEBUG_CTL
, DebugCtl
);
1089 Enable branch trace store.
1091 @param CpuIndex The index of the processor.
1101 DebugCtl
= AsmReadMsr64 (MSR_DEBUG_CTL
);
1102 if ((DebugCtl
& MSR_DEBUG_CTL_BTS
) != 0) {
1106 AsmWriteMsr64 (MSR_DS_AREA
, (UINT64
)(UINTN
)mMsrDsArea
[CpuIndex
]);
1107 DebugCtl
|= (UINT64
)(MSR_DEBUG_CTL_BTS
| MSR_DEBUG_CTL_TR
);
1108 DebugCtl
&= ~((UINT64
)MSR_DEBUG_CTL_BTINT
);
1109 AsmWriteMsr64 (MSR_DEBUG_CTL
, DebugCtl
);
1113 Increase SMI number in each SMI entry.
1117 SmmProfileRecordSmiNum (
1121 if (mSmmProfileStart
) {
1122 mSmmProfileBase
->NumSmis
++;
1127 Initialize processor environment for SMM profile.
1129 @param CpuIndex The index of the processor.
1133 ActivateSmmProfile (
1138 // Enable Single Step DB#
1140 ActivateSingleStepDB ();
1142 if (mBtsSupported
) {
1144 // We can not get useful information from LER, so we have to use BTS.
1151 ActivateBTS (CpuIndex
);
1156 Initialize SMM profile in SMM CPU entry point.
1158 @param[in] Cr3 The base address of the page tables to use in SMM.
1169 mSmmProfileCr3
= Cr3
;
1172 // Skip SMM profile initialization if feature is disabled
1174 if (!FeaturePcdGet (PcdCpuSmmProfileEnable
) &&
1175 !HEAP_GUARD_NONSTOP_MODE
&&
1176 !NULL_DETECTION_NONSTOP_MODE
) {
1181 // Initialize SmmProfile here
1183 InitSmmProfileInternal ();
1186 // Initialize profile IDT.
1191 // Tell #PF handler to prepare a #DB subsequently.
1193 mSetupDebugTrap
= TRUE
;
1197 Update page table to map the memory correctly in order to make the instruction
1198 which caused page fault execute successfully. And it also save the original page
1199 table to be restored in single-step exception.
1201 @param PageTable PageTable Address.
1202 @param PFAddress The memory address which caused page fault exception.
1203 @param CpuIndex The index of the processor.
1204 @param ErrorCode The Error code of exception.
1208 RestorePageTableBelow4G (
1218 BOOLEAN Enable5LevelPaging
;
1220 Cr4
.UintN
= AsmReadCr4 ();
1221 Enable5LevelPaging
= (BOOLEAN
) (Cr4
.Bits
.LA57
== 1);
1226 if (Enable5LevelPaging
) {
1227 PTIndex
= (UINTN
)BitFieldRead64 (PFAddress
, 48, 56);
1228 ASSERT (PageTable
[PTIndex
] != 0);
1229 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
1235 if (sizeof(UINT64
) == sizeof(UINTN
)) {
1236 PTIndex
= (UINTN
)BitFieldRead64 (PFAddress
, 39, 47);
1237 ASSERT (PageTable
[PTIndex
] != 0);
1238 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
1244 PTIndex
= (UINTN
)BitFieldRead64 (PFAddress
, 30, 38);
1245 ASSERT (PageTable
[PTIndex
] != 0);
1246 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
1251 PTIndex
= (UINTN
)BitFieldRead64 (PFAddress
, 21, 29);
1252 if ((PageTable
[PTIndex
] & IA32_PG_PS
) != 0) {
1258 // Record old entries with non-present status
1259 // Old entries include the memory which instruction is at and the memory which instruction access.
1262 ASSERT (mPFEntryCount
[CpuIndex
] < MAX_PF_ENTRY_COUNT
);
1263 if (mPFEntryCount
[CpuIndex
] < MAX_PF_ENTRY_COUNT
) {
1264 PFIndex
= mPFEntryCount
[CpuIndex
];
1265 mLastPFEntryValue
[CpuIndex
][PFIndex
] = PageTable
[PTIndex
];
1266 mLastPFEntryPointer
[CpuIndex
][PFIndex
] = &PageTable
[PTIndex
];
1267 mPFEntryCount
[CpuIndex
]++;
1273 PageTable
[PTIndex
] = (PFAddress
& ~((1ull << 21) - 1));
1274 PageTable
[PTIndex
] |= (UINT64
)IA32_PG_PS
;
1275 PageTable
[PTIndex
] |= (UINT64
)PAGE_ATTRIBUTE_BITS
;
1276 if ((ErrorCode
& IA32_PF_EC_ID
) != 0) {
1277 PageTable
[PTIndex
] &= ~IA32_PG_NX
;
1283 ASSERT (PageTable
[PTIndex
] != 0);
1284 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
1289 PTIndex
= (UINTN
)BitFieldRead64 (PFAddress
, 12, 20);
1292 // Record old entries with non-present status
1293 // Old entries include the memory which instruction is at and the memory which instruction access.
1296 ASSERT (mPFEntryCount
[CpuIndex
] < MAX_PF_ENTRY_COUNT
);
1297 if (mPFEntryCount
[CpuIndex
] < MAX_PF_ENTRY_COUNT
) {
1298 PFIndex
= mPFEntryCount
[CpuIndex
];
1299 mLastPFEntryValue
[CpuIndex
][PFIndex
] = PageTable
[PTIndex
];
1300 mLastPFEntryPointer
[CpuIndex
][PFIndex
] = &PageTable
[PTIndex
];
1301 mPFEntryCount
[CpuIndex
]++;
1307 PageTable
[PTIndex
] = (PFAddress
& ~((1ull << 12) - 1));
1308 PageTable
[PTIndex
] |= (UINT64
)PAGE_ATTRIBUTE_BITS
;
1309 if ((ErrorCode
& IA32_PF_EC_ID
) != 0) {
1310 PageTable
[PTIndex
] &= ~IA32_PG_NX
;
1316 Handler for Page Fault triggered by Guard page.
1318 @param ErrorCode The Error code of exception.
1322 GuardPagePFHandler (
1328 UINT64 RestoreAddress
;
1329 UINTN RestorePageNumber
;
1332 PageTable
= (UINT64
*)AsmReadCr3 ();
1333 PFAddress
= AsmReadCr2 ();
1334 CpuIndex
= GetCpuIndex ();
1337 // Memory operation cross pages, like "rep mov" instruction, will cause
1338 // infinite loop between this and Debug Trap handler. We have to make sure
1339 // that current page and the page followed are both in PRESENT state.
1341 RestorePageNumber
= 2;
1342 RestoreAddress
= PFAddress
;
1343 while (RestorePageNumber
> 0) {
1344 RestorePageTableBelow4G (PageTable
, RestoreAddress
, CpuIndex
, ErrorCode
);
1345 RestoreAddress
+= EFI_PAGE_SIZE
;
1346 RestorePageNumber
--;
1356 The Page fault handler to save SMM profile data.
1358 @param Rip The RIP when exception happens.
1359 @param ErrorCode The Error code of exception.
1363 SmmProfilePFHandler (
1370 UINT64 RestoreAddress
;
1371 UINTN RestorePageNumber
;
1374 UINT64 InstructionAddress
;
1375 UINTN MaxEntryNumber
;
1376 UINTN CurrentEntryNumber
;
1377 BOOLEAN IsValidPFAddress
;
1378 SMM_PROFILE_ENTRY
*SmmProfileEntry
;
1382 EFI_SMM_SAVE_STATE_IO_INFO IoInfo
;
1384 if (!mSmmProfileStart
) {
1386 // If SMM profile does not start, call original page fault handler.
1388 SmiDefaultPFHandler ();
1392 if (mBtsSupported
) {
1396 IsValidPFAddress
= FALSE
;
1397 PageTable
= (UINT64
*)AsmReadCr3 ();
1398 PFAddress
= AsmReadCr2 ();
1399 CpuIndex
= GetCpuIndex ();
1402 // Memory operation cross pages, like "rep mov" instruction, will cause
1403 // infinite loop between this and Debug Trap handler. We have to make sure
1404 // that current page and the page followed are both in PRESENT state.
1406 RestorePageNumber
= 2;
1407 RestoreAddress
= PFAddress
;
1408 while (RestorePageNumber
> 0) {
1409 if (RestoreAddress
<= 0xFFFFFFFF) {
1410 RestorePageTableBelow4G (PageTable
, RestoreAddress
, CpuIndex
, ErrorCode
);
1412 RestorePageTableAbove4G (PageTable
, RestoreAddress
, CpuIndex
, ErrorCode
, &IsValidPFAddress
);
1414 RestoreAddress
+= EFI_PAGE_SIZE
;
1415 RestorePageNumber
--;
1418 if (!IsValidPFAddress
) {
1419 InstructionAddress
= Rip
;
1420 if ((ErrorCode
& IA32_PF_EC_ID
) != 0 && (mBtsSupported
)) {
1422 // If it is instruction fetch failure, get the correct IP from BTS.
1424 InstructionAddress
= GetSourceFromDestinationOnBts (CpuIndex
, Rip
);
1425 if (InstructionAddress
== 0) {
1427 // It indicates the instruction which caused page fault is not a jump instruction,
1428 // set instruction address same as the page fault address.
1430 InstructionAddress
= PFAddress
;
1435 // Indicate it is not software SMI
1437 SmiCommand
= 0xFFFFFFFFFFFFFFFFULL
;
1438 for (Index
= 0; Index
< gSmst
->NumberOfCpus
; Index
++) {
1439 Status
= SmmReadSaveState(&mSmmCpu
, sizeof(IoInfo
), EFI_SMM_SAVE_STATE_REGISTER_IO
, Index
, &IoInfo
);
1440 if (EFI_ERROR (Status
)) {
1443 if (IoInfo
.IoPort
== mSmiCommandPort
) {
1445 // A software SMI triggered by SMI command port has been found, get SmiCommand from SMI command port.
1447 SoftSmiValue
= IoRead8 (mSmiCommandPort
);
1448 SmiCommand
= (UINT64
)SoftSmiValue
;
1453 SmmProfileEntry
= (SMM_PROFILE_ENTRY
*)(UINTN
)(mSmmProfileBase
+ 1);
1455 // Check if there is already a same entry in profile data.
1457 for (Index
= 0; Index
< (UINTN
) mSmmProfileBase
->CurDataEntries
; Index
++) {
1458 if ((SmmProfileEntry
[Index
].ErrorCode
== (UINT64
)ErrorCode
) &&
1459 (SmmProfileEntry
[Index
].Address
== PFAddress
) &&
1460 (SmmProfileEntry
[Index
].CpuNum
== (UINT64
)CpuIndex
) &&
1461 (SmmProfileEntry
[Index
].Instruction
== InstructionAddress
) &&
1462 (SmmProfileEntry
[Index
].SmiCmd
== SmiCommand
)) {
1464 // Same record exist, need not save again.
1469 if (Index
== mSmmProfileBase
->CurDataEntries
) {
1470 CurrentEntryNumber
= (UINTN
) mSmmProfileBase
->CurDataEntries
;
1471 MaxEntryNumber
= (UINTN
) mSmmProfileBase
->MaxDataEntries
;
1472 if (FeaturePcdGet (PcdCpuSmmProfileRingBuffer
)) {
1473 CurrentEntryNumber
= CurrentEntryNumber
% MaxEntryNumber
;
1475 if (CurrentEntryNumber
< MaxEntryNumber
) {
1477 // Log the new entry
1479 SmmProfileEntry
[CurrentEntryNumber
].SmiNum
= mSmmProfileBase
->NumSmis
;
1480 SmmProfileEntry
[CurrentEntryNumber
].ErrorCode
= (UINT64
)ErrorCode
;
1481 SmmProfileEntry
[CurrentEntryNumber
].ApicId
= (UINT64
)GetApicId ();
1482 SmmProfileEntry
[CurrentEntryNumber
].CpuNum
= (UINT64
)CpuIndex
;
1483 SmmProfileEntry
[CurrentEntryNumber
].Address
= PFAddress
;
1484 SmmProfileEntry
[CurrentEntryNumber
].Instruction
= InstructionAddress
;
1485 SmmProfileEntry
[CurrentEntryNumber
].SmiCmd
= SmiCommand
;
1487 // Update current entry index and data size in the header.
1489 mSmmProfileBase
->CurDataEntries
++;
1490 mSmmProfileBase
->CurDataSize
= MultU64x64 (mSmmProfileBase
->CurDataEntries
, sizeof (SMM_PROFILE_ENTRY
));
1499 if (mBtsSupported
) {
1505 Replace INT1 exception handler to restore page table to absent/execute-disable state
1506 in order to trigger page fault again to save SMM profile data..
1516 Status
= SmmRegisterExceptionHandler (&mSmmCpuService
, EXCEPT_IA32_DEBUG
, DebugExceptionHandler
);
1517 ASSERT_EFI_ERROR (Status
);