4 Copyright (c) 2012 - 2023, 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.
82 { 0x00000000, 0x00000000 }, TRUE
, FALSE
86 // SMM profile data range( to be fixed in runtime).
87 // It is always present and instruction fetches are not allowed.
90 { 0x00000000, 0x00000000 }, TRUE
, TRUE
94 // SMRAM ranges not covered by mCpuHotPlugData.SmrrBase/mCpuHotPlugData.SmrrSiz (to be fixed in runtime).
95 // It is always present and instruction fetches are allowed.
96 // {{0x00000000, 0x00000000},TRUE,FALSE},
100 // Future extended range could be added here.
104 // PCI MMIO ranges (to be added in runtime).
105 // They are always present and instruction fetches are not allowed.
110 // These memory ranges are mapped by 4KB-page instead of 2MB-page.
112 MEMORY_RANGE
*mSplitMemRange
= NULL
;
113 UINTN mSplitMemRangeCount
= 0;
118 UINT32 mSmiCommandPort
;
121 Disable branch trace store.
129 AsmMsrAnd64 (MSR_DEBUG_CTL
, ~((UINT64
)(MSR_DEBUG_CTL_BTS
| MSR_DEBUG_CTL_TR
)));
133 Enable branch trace store.
141 AsmMsrOr64 (MSR_DEBUG_CTL
, (MSR_DEBUG_CTL_BTS
| MSR_DEBUG_CTL_TR
));
145 Get CPU Index from APIC ID.
156 ApicId
= GetApicId ();
158 for (Index
= 0; Index
< mMaxNumberOfCpus
; Index
++) {
159 if (gSmmCpuPrivate
->ProcessorInfo
[Index
].ProcessorId
== ApicId
) {
169 Get the source of IP after execute-disable exception is triggered.
171 @param CpuIndex The index of CPU.
172 @param DestinationIP The destination address.
176 GetSourceFromDestinationOnBts (
181 BRANCH_TRACE_RECORD
*CurrentBTSRecord
;
187 CurrentBTSRecord
= (BRANCH_TRACE_RECORD
*)mMsrDsArea
[CpuIndex
]->BTSIndex
;
188 for (Index
= 0; Index
< mBTSRecordNumber
; Index
++) {
189 if ((UINTN
)CurrentBTSRecord
< (UINTN
)mMsrBTSRecord
[CpuIndex
]) {
193 CurrentBTSRecord
= (BRANCH_TRACE_RECORD
*)((UINTN
)mMsrDsArea
[CpuIndex
]->BTSAbsoluteMaximum
- 1);
197 if (CurrentBTSRecord
->LastBranchTo
== DestinationIP
) {
199 // Good! find 1st one, then find 2nd one.
203 // The first one is DEBUG exception
208 // Good find proper one.
210 return CurrentBTSRecord
->LastBranchFrom
;
221 SMM profile specific INT 1 (single-step) exception handler.
223 @param InterruptType Defines the type of interrupt or exception that
224 occurred on the processor.This parameter is processor architecture specific.
225 @param SystemContext A pointer to the processor context when
226 the interrupt occurred on the processor.
230 DebugExceptionHandler (
231 IN EFI_EXCEPTION_TYPE InterruptType
,
232 IN EFI_SYSTEM_CONTEXT SystemContext
238 if (!mSmmProfileStart
&&
239 !HEAP_GUARD_NONSTOP_MODE
&&
240 !NULL_DETECTION_NONSTOP_MODE
)
245 CpuIndex
= GetCpuIndex ();
248 // Clear last PF entries
250 for (PFEntry
= 0; PFEntry
< mPFEntryCount
[CpuIndex
]; PFEntry
++) {
251 *mLastPFEntryPointer
[CpuIndex
][PFEntry
] = mLastPFEntryValue
[CpuIndex
][PFEntry
];
255 // Reset page fault exception count for next page fault.
257 mPFEntryCount
[CpuIndex
] = 0;
265 // Clear TF in EFLAGS
267 ClearTrapFlag (SystemContext
);
271 Check if the input address is in SMM ranges.
273 @param[in] Address The input address.
275 @retval TRUE The input address is in SMM.
276 @retval FALSE The input address is not in SMM.
280 IN EFI_PHYSICAL_ADDRESS Address
285 if ((Address
>= mCpuHotPlugData
.SmrrBase
) && (Address
< mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
)) {
289 for (Index
= 0; Index
< mSmmCpuSmramRangeCount
; Index
++) {
290 if ((Address
>= mSmmCpuSmramRanges
[Index
].CpuStart
) &&
291 (Address
< mSmmCpuSmramRanges
[Index
].CpuStart
+ mSmmCpuSmramRanges
[Index
].PhysicalSize
))
301 Check if the memory address will be mapped by 4KB-page.
303 @param Address The address of Memory.
304 @param Nx The flag indicates if the memory is execute-disable.
309 IN EFI_PHYSICAL_ADDRESS Address
,
315 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
317 // Check configuration
319 for (Index
= 0; Index
< mProtectionMemRangeCount
; Index
++) {
320 if ((Address
>= mProtectionMemRange
[Index
].Range
.Base
) && (Address
< mProtectionMemRange
[Index
].Range
.Top
)) {
321 *Nx
= mProtectionMemRange
[Index
].Nx
;
322 return mProtectionMemRange
[Index
].Present
;
330 if (IsInSmmRanges (Address
)) {
339 Check if the memory address will be mapped by 4KB-page.
341 @param Address The address of Memory.
346 IN EFI_PHYSICAL_ADDRESS Address
351 if (FeaturePcdGet (PcdCpuSmmProfileEnable
)) {
353 // Check configuration
355 for (Index
= 0; Index
< mSplitMemRangeCount
; Index
++) {
356 if ((Address
>= mSplitMemRange
[Index
].Base
) && (Address
< mSplitMemRange
[Index
].Top
)) {
361 if (Address
< mCpuHotPlugData
.SmrrBase
) {
362 if ((mCpuHotPlugData
.SmrrBase
- Address
) < BASE_2MB
) {
365 } else if (Address
> (mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
- BASE_2MB
)) {
366 if ((Address
- (mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
- BASE_2MB
)) < BASE_2MB
) {
379 Initialize the protected memory ranges and the 4KB-page mapped memory ranges.
383 InitProtectedMemRange (
388 UINTN NumberOfDescriptors
;
389 UINTN NumberOfAddedDescriptors
;
390 UINTN NumberOfProtectRange
;
391 UINTN NumberOfSpliteRange
;
392 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemorySpaceMap
;
394 EFI_PHYSICAL_ADDRESS ProtectBaseAddress
;
395 EFI_PHYSICAL_ADDRESS ProtectEndAddress
;
396 EFI_PHYSICAL_ADDRESS Top2MBAlignedAddress
;
397 EFI_PHYSICAL_ADDRESS Base2MBAlignedAddress
;
398 UINT64 High4KBPageSize
;
399 UINT64 Low4KBPageSize
;
401 NumberOfDescriptors
= 0;
402 NumberOfAddedDescriptors
= mSmmCpuSmramRangeCount
;
403 NumberOfSpliteRange
= 0;
404 MemorySpaceMap
= NULL
;
407 // Get MMIO ranges from GCD and add them into protected memory ranges.
409 gDS
->GetMemorySpaceMap (
410 &NumberOfDescriptors
,
413 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
414 if (MemorySpaceMap
[Index
].GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) {
415 NumberOfAddedDescriptors
++;
419 if (NumberOfAddedDescriptors
!= 0) {
420 TotalSize
= NumberOfAddedDescriptors
* sizeof (MEMORY_PROTECTION_RANGE
) + sizeof (mProtectionMemRangeTemplate
);
421 mProtectionMemRange
= (MEMORY_PROTECTION_RANGE
*)AllocateZeroPool (TotalSize
);
422 ASSERT (mProtectionMemRange
!= NULL
);
423 mProtectionMemRangeCount
= TotalSize
/ sizeof (MEMORY_PROTECTION_RANGE
);
426 // Copy existing ranges.
428 CopyMem (mProtectionMemRange
, mProtectionMemRangeTemplate
, sizeof (mProtectionMemRangeTemplate
));
431 // Create split ranges which come from protected ranges.
433 TotalSize
= (TotalSize
/ sizeof (MEMORY_PROTECTION_RANGE
)) * sizeof (MEMORY_RANGE
);
434 mSplitMemRange
= (MEMORY_RANGE
*)AllocateZeroPool (TotalSize
);
435 ASSERT (mSplitMemRange
!= NULL
);
438 // Create SMM ranges which are set to present and execution-enable.
440 NumberOfProtectRange
= sizeof (mProtectionMemRangeTemplate
) / sizeof (MEMORY_PROTECTION_RANGE
);
441 for (Index
= 0; Index
< mSmmCpuSmramRangeCount
; Index
++) {
442 if ((mSmmCpuSmramRanges
[Index
].CpuStart
>= mProtectionMemRange
[0].Range
.Base
) &&
443 (mSmmCpuSmramRanges
[Index
].CpuStart
+ mSmmCpuSmramRanges
[Index
].PhysicalSize
< mProtectionMemRange
[0].Range
.Top
))
446 // If the address have been already covered by mCpuHotPlugData.SmrrBase/mCpuHotPlugData.SmrrSiz
451 mProtectionMemRange
[NumberOfProtectRange
].Range
.Base
= mSmmCpuSmramRanges
[Index
].CpuStart
;
452 mProtectionMemRange
[NumberOfProtectRange
].Range
.Top
= mSmmCpuSmramRanges
[Index
].CpuStart
+ mSmmCpuSmramRanges
[Index
].PhysicalSize
;
453 mProtectionMemRange
[NumberOfProtectRange
].Present
= TRUE
;
454 mProtectionMemRange
[NumberOfProtectRange
].Nx
= FALSE
;
455 NumberOfProtectRange
++;
459 // Create MMIO ranges which are set to present and execution-disable.
461 for (Index
= 0; Index
< NumberOfDescriptors
; Index
++) {
462 if (MemorySpaceMap
[Index
].GcdMemoryType
!= EfiGcdMemoryTypeMemoryMappedIo
) {
466 mProtectionMemRange
[NumberOfProtectRange
].Range
.Base
= MemorySpaceMap
[Index
].BaseAddress
;
467 mProtectionMemRange
[NumberOfProtectRange
].Range
.Top
= MemorySpaceMap
[Index
].BaseAddress
+ MemorySpaceMap
[Index
].Length
;
468 mProtectionMemRange
[NumberOfProtectRange
].Present
= TRUE
;
469 mProtectionMemRange
[NumberOfProtectRange
].Nx
= TRUE
;
470 NumberOfProtectRange
++;
474 // Check and updated actual protected memory ranges count
476 ASSERT (NumberOfProtectRange
<= mProtectionMemRangeCount
);
477 mProtectionMemRangeCount
= NumberOfProtectRange
;
481 // According to protected ranges, create the ranges which will be mapped by 2KB page.
483 NumberOfSpliteRange
= 0;
484 NumberOfProtectRange
= mProtectionMemRangeCount
;
485 for (Index
= 0; Index
< NumberOfProtectRange
; Index
++) {
487 // If MMIO base address is not 2MB alignment, make 2MB alignment for create 4KB page in page table.
489 ProtectBaseAddress
= mProtectionMemRange
[Index
].Range
.Base
;
490 ProtectEndAddress
= mProtectionMemRange
[Index
].Range
.Top
;
491 if (((ProtectBaseAddress
& (SIZE_2MB
- 1)) != 0) || ((ProtectEndAddress
& (SIZE_2MB
- 1)) != 0)) {
493 // Check if it is possible to create 4KB-page for not 2MB-aligned range and to create 2MB-page for 2MB-aligned range.
494 // A mix of 4KB and 2MB page could save SMRAM space.
496 Top2MBAlignedAddress
= ProtectEndAddress
& ~(SIZE_2MB
- 1);
497 Base2MBAlignedAddress
= (ProtectBaseAddress
+ SIZE_2MB
- 1) & ~(SIZE_2MB
- 1);
498 if ((Top2MBAlignedAddress
> Base2MBAlignedAddress
) &&
499 ((Top2MBAlignedAddress
- Base2MBAlignedAddress
) >= SIZE_2MB
))
502 // There is an range which could be mapped by 2MB-page.
504 High4KBPageSize
= ((ProtectEndAddress
+ SIZE_2MB
- 1) & ~(SIZE_2MB
- 1)) - (ProtectEndAddress
& ~(SIZE_2MB
- 1));
505 Low4KBPageSize
= ((ProtectBaseAddress
+ SIZE_2MB
- 1) & ~(SIZE_2MB
- 1)) - (ProtectBaseAddress
& ~(SIZE_2MB
- 1));
506 if (High4KBPageSize
!= 0) {
508 // Add not 2MB-aligned range to be mapped by 4KB-page.
510 mSplitMemRange
[NumberOfSpliteRange
].Base
= ProtectEndAddress
& ~(SIZE_2MB
- 1);
511 mSplitMemRange
[NumberOfSpliteRange
].Top
= (ProtectEndAddress
+ SIZE_2MB
- 1) & ~(SIZE_2MB
- 1);
512 NumberOfSpliteRange
++;
515 if (Low4KBPageSize
!= 0) {
517 // Add not 2MB-aligned range to be mapped by 4KB-page.
519 mSplitMemRange
[NumberOfSpliteRange
].Base
= ProtectBaseAddress
& ~(SIZE_2MB
- 1);
520 mSplitMemRange
[NumberOfSpliteRange
].Top
= (ProtectBaseAddress
+ SIZE_2MB
- 1) & ~(SIZE_2MB
- 1);
521 NumberOfSpliteRange
++;
525 // The range could only be mapped by 4KB-page.
527 mSplitMemRange
[NumberOfSpliteRange
].Base
= ProtectBaseAddress
& ~(SIZE_2MB
- 1);
528 mSplitMemRange
[NumberOfSpliteRange
].Top
= (ProtectEndAddress
+ SIZE_2MB
- 1) & ~(SIZE_2MB
- 1);
529 NumberOfSpliteRange
++;
534 mSplitMemRangeCount
= NumberOfSpliteRange
;
536 DEBUG ((DEBUG_INFO
, "SMM Profile Memory Ranges:\n"));
537 for (Index
= 0; Index
< mProtectionMemRangeCount
; Index
++) {
538 DEBUG ((DEBUG_INFO
, "mProtectionMemRange[%d].Base = %lx\n", Index
, mProtectionMemRange
[Index
].Range
.Base
));
539 DEBUG ((DEBUG_INFO
, "mProtectionMemRange[%d].Top = %lx\n", Index
, mProtectionMemRange
[Index
].Range
.Top
));
542 for (Index
= 0; Index
< mSplitMemRangeCount
; Index
++) {
543 DEBUG ((DEBUG_INFO
, "mSplitMemRange[%d].Base = %lx\n", Index
, mSplitMemRange
[Index
].Base
));
544 DEBUG ((DEBUG_INFO
, "mSplitMemRange[%d].Top = %lx\n", Index
, mSplitMemRange
[Index
].Top
));
549 Update page table according to protected memory ranges and the 4KB-page mapped memory ranges.
570 UINTN NumberOfPdptEntries
;
571 UINTN NumberOfPml4Entries
;
572 UINTN NumberOfPml5Entries
;
573 UINTN SizeOfMemorySpace
;
576 BOOLEAN Enable5LevelPaging
;
578 Cr4
.UintN
= AsmReadCr4 ();
579 Enable5LevelPaging
= (BOOLEAN
)(Cr4
.Bits
.LA57
== 1);
581 if (sizeof (UINTN
) == sizeof (UINT64
)) {
582 if (!Enable5LevelPaging
) {
583 Pml5Entry
= (UINTN
)mSmmProfileCr3
| IA32_PG_P
;
586 Pml5
= (UINT64
*)(UINTN
)mSmmProfileCr3
;
589 SizeOfMemorySpace
= HighBitSet64 (gPhyMask
) + 1;
590 ASSERT (SizeOfMemorySpace
<= 52);
593 // Calculate the table entries of PML5E, PML4E and PDPTE.
595 NumberOfPml5Entries
= 1;
596 if (SizeOfMemorySpace
> 48) {
597 if (Enable5LevelPaging
) {
598 NumberOfPml5Entries
= (UINTN
)LShiftU64 (1, SizeOfMemorySpace
- 48);
601 SizeOfMemorySpace
= 48;
604 NumberOfPml4Entries
= 1;
605 if (SizeOfMemorySpace
> 39) {
606 NumberOfPml4Entries
= (UINTN
)LShiftU64 (1, SizeOfMemorySpace
- 39);
607 SizeOfMemorySpace
= 39;
610 NumberOfPdptEntries
= 1;
611 ASSERT (SizeOfMemorySpace
> 30);
612 NumberOfPdptEntries
= (UINTN
)LShiftU64 (1, SizeOfMemorySpace
- 30);
614 Pml4Entry
= (UINTN
)mSmmProfileCr3
| IA32_PG_P
;
616 Pml5Entry
= (UINTN
)Pml4
| IA32_PG_P
;
618 NumberOfPml5Entries
= 1;
619 NumberOfPml4Entries
= 1;
620 NumberOfPdptEntries
= 4;
624 // Go through page table and change 2MB-page into 4KB-page.
626 for (Pml5Index
= 0; Pml5Index
< NumberOfPml5Entries
; Pml5Index
++) {
627 if ((Pml5
[Pml5Index
] & IA32_PG_P
) == 0) {
629 // If PML5 entry does not exist, skip it
634 Pml4
= (UINT64
*)(UINTN
)(Pml5
[Pml5Index
] & PHYSICAL_ADDRESS_MASK
);
635 for (Pml4Index
= 0; Pml4Index
< NumberOfPml4Entries
; Pml4Index
++) {
636 if ((Pml4
[Pml4Index
] & IA32_PG_P
) == 0) {
638 // If PML4 entry does not exist, skip it
643 Pdpt
= (UINT64
*)(UINTN
)(Pml4
[Pml4Index
] & ~mAddressEncMask
& PHYSICAL_ADDRESS_MASK
);
644 for (PdptIndex
= 0; PdptIndex
< NumberOfPdptEntries
; PdptIndex
++, Pdpt
++) {
645 if ((*Pdpt
& IA32_PG_P
) == 0) {
647 // If PDPT entry does not exist, skip it
652 if ((*Pdpt
& IA32_PG_PS
) != 0) {
654 // This is 1G entry, skip it
659 Pd
= (UINT64
*)(UINTN
)(*Pdpt
& ~mAddressEncMask
& PHYSICAL_ADDRESS_MASK
);
664 for (PdIndex
= 0; PdIndex
< SIZE_4KB
/ sizeof (*Pd
); PdIndex
++, Pd
++) {
665 if ((*Pd
& IA32_PG_P
) == 0) {
667 // If PD entry does not exist, skip it
672 Address
= (UINTN
)LShiftU64 (
674 LShiftU64 ((Pml5Index
<< 9) + Pml4Index
, 9) + PdptIndex
,
681 // If it is 2M page, check IsAddressSplit()
683 if (((*Pd
& IA32_PG_PS
) != 0) && IsAddressSplit (Address
)) {
685 // Based on current page table, create 4KB page table for split area.
687 ASSERT (Address
== (*Pd
& PHYSICAL_ADDRESS_MASK
));
689 Pt
= AllocatePageTableMemory (1);
693 for (PtIndex
= 0; PtIndex
< SIZE_4KB
/ sizeof (*Pt
); PtIndex
++) {
694 Pt
[PtIndex
] = Address
+ ((PtIndex
<< 12) | mAddressEncMask
| PAGE_ATTRIBUTE_BITS
);
697 *Pd
= (UINT64
)(UINTN
)Pt
| mAddressEncMask
| PAGE_ATTRIBUTE_BITS
;
698 } // end if IsAddressSplit
705 // Go through page table and set several page table entries to absent or execute-disable.
707 DEBUG ((DEBUG_INFO
, "Patch page table start ...\n"));
708 for (Pml5Index
= 0; Pml5Index
< NumberOfPml5Entries
; Pml5Index
++) {
709 if ((Pml5
[Pml5Index
] & IA32_PG_P
) == 0) {
711 // If PML5 entry does not exist, skip it
716 Pml4
= (UINT64
*)(UINTN
)(Pml5
[Pml5Index
] & PHYSICAL_ADDRESS_MASK
);
717 for (Pml4Index
= 0; Pml4Index
< NumberOfPml4Entries
; Pml4Index
++) {
718 if ((Pml4
[Pml4Index
] & IA32_PG_P
) == 0) {
720 // If PML4 entry does not exist, skip it
725 Pdpt
= (UINT64
*)(UINTN
)(Pml4
[Pml4Index
] & ~mAddressEncMask
& PHYSICAL_ADDRESS_MASK
);
726 for (PdptIndex
= 0; PdptIndex
< NumberOfPdptEntries
; PdptIndex
++, Pdpt
++) {
727 if ((*Pdpt
& IA32_PG_P
) == 0) {
729 // If PDPT entry does not exist, skip it
734 if ((*Pdpt
& IA32_PG_PS
) != 0) {
736 // This is 1G entry, set NX bit and skip it
739 *Pdpt
= *Pdpt
| IA32_PG_NX
;
745 Pd
= (UINT64
*)(UINTN
)(*Pdpt
& ~mAddressEncMask
& PHYSICAL_ADDRESS_MASK
);
750 for (PdIndex
= 0; PdIndex
< SIZE_4KB
/ sizeof (*Pd
); PdIndex
++, Pd
++) {
751 if ((*Pd
& IA32_PG_P
) == 0) {
753 // If PD entry does not exist, skip it
758 Address
= (UINTN
)LShiftU64 (
760 LShiftU64 ((Pml5Index
<< 9) + Pml4Index
, 9) + PdptIndex
,
766 if ((*Pd
& IA32_PG_PS
) != 0) {
769 if (!IsAddressValid (Address
, &Nx
)) {
771 // Patch to remove Present flag and RW flag
773 *Pd
= *Pd
& (INTN
)(INT32
)(~PAGE_ATTRIBUTE_BITS
);
776 if (Nx
&& mXdSupported
) {
777 *Pd
= *Pd
| IA32_PG_NX
;
781 Pt
= (UINT64
*)(UINTN
)(*Pd
& ~mAddressEncMask
& PHYSICAL_ADDRESS_MASK
);
786 for (PtIndex
= 0; PtIndex
< SIZE_4KB
/ sizeof (*Pt
); PtIndex
++, Pt
++) {
787 if (!IsAddressValid (Address
, &Nx
)) {
788 *Pt
= *Pt
& (INTN
)(INT32
)(~PAGE_ATTRIBUTE_BITS
);
791 if (Nx
&& mXdSupported
) {
792 *Pt
= *Pt
| IA32_PG_NX
;
807 DEBUG ((DEBUG_INFO
, "Patch page table done!\n"));
809 // Set execute-disable flag
817 To get system port address of the SMI Command Port in FADT table.
825 EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE
*Fadt
;
827 Fadt
= (EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE
*)EfiLocateFirstAcpiTable (
828 EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE
830 ASSERT (Fadt
!= NULL
);
832 mSmiCommandPort
= Fadt
->SmiCmd
;
833 DEBUG ((DEBUG_INFO
, "mSmiCommandPort = %x\n", mSmiCommandPort
));
837 Updates page table to make some memory ranges (like system memory) absent
838 and make some memory ranges (like MMIO) present and execute disable. It also
839 update 2MB-page to 4KB-page for some memory ranges.
848 // The flag indicates SMM profile starts to work.
850 mSmmProfileStart
= TRUE
;
854 Initialize SMM profile in SmmReadyToLock protocol callback function.
856 @param Protocol Points to the protocol's unique identifier.
857 @param Interface Points to the interface instance.
858 @param Handle The handle on which the interface was installed.
860 @retval EFI_SUCCESS SmmReadyToLock protocol callback runs successfully.
864 InitSmmProfileCallBack (
865 IN CONST EFI_GUID
*Protocol
,
871 // Save to variable so that SMM profile data can be found.
876 EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
877 sizeof (mSmmProfileBase
),
882 // Get Software SMI from FADT
884 GetSmiCommandPort ();
887 // Initialize protected memory range for patching page table later.
889 InitProtectedMemRange ();
895 Initialize SMM profile data structures.
899 InitSmmProfileInternal (
904 EFI_PHYSICAL_ADDRESS Base
;
907 UINTN MsrDsAreaSizePerCpu
;
910 mPFEntryCount
= (UINTN
*)AllocateZeroPool (sizeof (UINTN
) * mMaxNumberOfCpus
);
911 ASSERT (mPFEntryCount
!= NULL
);
912 mLastPFEntryValue
= (UINT64 (*)[MAX_PF_ENTRY_COUNT
])AllocateZeroPool (
913 sizeof (mLastPFEntryValue
[0]) * mMaxNumberOfCpus
915 ASSERT (mLastPFEntryValue
!= NULL
);
916 mLastPFEntryPointer
= (UINT64
*(*)[MAX_PF_ENTRY_COUNT
])AllocateZeroPool (
917 sizeof (mLastPFEntryPointer
[0]) * mMaxNumberOfCpus
919 ASSERT (mLastPFEntryPointer
!= NULL
);
922 // Allocate memory for SmmProfile below 4GB.
925 mSmmProfileSize
= PcdGet32 (PcdCpuSmmProfileSize
);
926 ASSERT ((mSmmProfileSize
& 0xFFF) == 0);
929 TotalSize
= mSmmProfileSize
+ mMsrDsAreaSize
;
931 TotalSize
= mSmmProfileSize
;
935 Status
= gBS
->AllocatePages (
937 EfiReservedMemoryType
,
938 EFI_SIZE_TO_PAGES (TotalSize
),
941 ASSERT_EFI_ERROR (Status
);
942 ZeroMem ((VOID
*)(UINTN
)Base
, TotalSize
);
943 mSmmProfileBase
= (SMM_PROFILE_HEADER
*)(UINTN
)Base
;
946 // Initialize SMM profile data header.
948 mSmmProfileBase
->HeaderSize
= sizeof (SMM_PROFILE_HEADER
);
949 mSmmProfileBase
->MaxDataEntries
= (UINT64
)((mSmmProfileSize
- sizeof (SMM_PROFILE_HEADER
)) / sizeof (SMM_PROFILE_ENTRY
));
950 mSmmProfileBase
->MaxDataSize
= MultU64x64 (mSmmProfileBase
->MaxDataEntries
, sizeof (SMM_PROFILE_ENTRY
));
951 mSmmProfileBase
->CurDataEntries
= 0;
952 mSmmProfileBase
->CurDataSize
= 0;
953 mSmmProfileBase
->TsegStart
= mCpuHotPlugData
.SmrrBase
;
954 mSmmProfileBase
->TsegSize
= mCpuHotPlugData
.SmrrSize
;
955 mSmmProfileBase
->NumSmis
= 0;
956 mSmmProfileBase
->NumCpus
= gSmmCpuPrivate
->SmmCoreEntryContext
.NumberOfCpus
;
959 mMsrDsArea
= (MSR_DS_AREA_STRUCT
**)AllocateZeroPool (sizeof (MSR_DS_AREA_STRUCT
*) * mMaxNumberOfCpus
);
960 ASSERT (mMsrDsArea
!= NULL
);
961 mMsrBTSRecord
= (BRANCH_TRACE_RECORD
**)AllocateZeroPool (sizeof (BRANCH_TRACE_RECORD
*) * mMaxNumberOfCpus
);
962 ASSERT (mMsrBTSRecord
!= NULL
);
963 mMsrPEBSRecord
= (PEBS_RECORD
**)AllocateZeroPool (sizeof (PEBS_RECORD
*) * mMaxNumberOfCpus
);
964 ASSERT (mMsrPEBSRecord
!= NULL
);
966 mMsrDsAreaBase
= (MSR_DS_AREA_STRUCT
*)((UINTN
)Base
+ mSmmProfileSize
);
967 MsrDsAreaSizePerCpu
= mMsrDsAreaSize
/ mMaxNumberOfCpus
;
968 mBTSRecordNumber
= (MsrDsAreaSizePerCpu
- sizeof (PEBS_RECORD
) * PEBS_RECORD_NUMBER
- sizeof (MSR_DS_AREA_STRUCT
)) / sizeof (BRANCH_TRACE_RECORD
);
969 for (Index
= 0; Index
< mMaxNumberOfCpus
; Index
++) {
970 mMsrDsArea
[Index
] = (MSR_DS_AREA_STRUCT
*)((UINTN
)mMsrDsAreaBase
+ MsrDsAreaSizePerCpu
* Index
);
971 mMsrBTSRecord
[Index
] = (BRANCH_TRACE_RECORD
*)((UINTN
)mMsrDsArea
[Index
] + sizeof (MSR_DS_AREA_STRUCT
));
972 mMsrPEBSRecord
[Index
] = (PEBS_RECORD
*)((UINTN
)mMsrDsArea
[Index
] + MsrDsAreaSizePerCpu
- sizeof (PEBS_RECORD
) * PEBS_RECORD_NUMBER
);
974 mMsrDsArea
[Index
]->BTSBufferBase
= (UINTN
)mMsrBTSRecord
[Index
];
975 mMsrDsArea
[Index
]->BTSIndex
= mMsrDsArea
[Index
]->BTSBufferBase
;
976 mMsrDsArea
[Index
]->BTSAbsoluteMaximum
= mMsrDsArea
[Index
]->BTSBufferBase
+ mBTSRecordNumber
* sizeof (BRANCH_TRACE_RECORD
) + 1;
977 mMsrDsArea
[Index
]->BTSInterruptThreshold
= mMsrDsArea
[Index
]->BTSAbsoluteMaximum
+ 1;
979 mMsrDsArea
[Index
]->PEBSBufferBase
= (UINTN
)mMsrPEBSRecord
[Index
];
980 mMsrDsArea
[Index
]->PEBSIndex
= mMsrDsArea
[Index
]->PEBSBufferBase
;
981 mMsrDsArea
[Index
]->PEBSAbsoluteMaximum
= mMsrDsArea
[Index
]->PEBSBufferBase
+ PEBS_RECORD_NUMBER
* sizeof (PEBS_RECORD
) + 1;
982 mMsrDsArea
[Index
]->PEBSInterruptThreshold
= mMsrDsArea
[Index
]->PEBSAbsoluteMaximum
+ 1;
986 mProtectionMemRange
= mProtectionMemRangeTemplate
;
987 mProtectionMemRangeCount
= sizeof (mProtectionMemRangeTemplate
) / sizeof (MEMORY_PROTECTION_RANGE
);
990 // Update TSeg entry.
992 mProtectionMemRange
[0].Range
.Base
= mCpuHotPlugData
.SmrrBase
;
993 mProtectionMemRange
[0].Range
.Top
= mCpuHotPlugData
.SmrrBase
+ mCpuHotPlugData
.SmrrSize
;
996 // Update SMM profile entry.
998 mProtectionMemRange
[1].Range
.Base
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)mSmmProfileBase
;
999 mProtectionMemRange
[1].Range
.Top
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)mSmmProfileBase
+ TotalSize
;
1002 // Allocate memory reserved for creating 4KB pages.
1004 InitPagesForPFHandler ();
1007 // Start SMM profile when SmmReadyToLock protocol is installed.
1009 Status
= gSmst
->SmmRegisterProtocolNotify (
1010 &gEfiSmmReadyToLockProtocolGuid
,
1011 InitSmmProfileCallBack
,
1014 ASSERT_EFI_ERROR (Status
);
1020 Check if feature is supported by a processor.
1024 CheckFeatureSupported (
1031 MSR_IA32_MISC_ENABLE_REGISTER MiscEnableMsr
;
1033 if ((PcdGet32 (PcdControlFlowEnforcementPropertyMask
) != 0) && mCetSupported
) {
1034 AsmCpuid (CPUID_SIGNATURE
, &RegEax
, NULL
, NULL
, NULL
);
1035 if (RegEax
>= CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS
) {
1036 AsmCpuidEx (CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS
, CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_SUB_LEAF_INFO
, NULL
, NULL
, &RegEcx
, NULL
);
1037 if ((RegEcx
& CPUID_CET_SS
) == 0) {
1038 mCetSupported
= FALSE
;
1039 PatchInstructionX86 (mPatchCetSupported
, mCetSupported
, 1);
1042 mCetSupported
= FALSE
;
1043 PatchInstructionX86 (mPatchCetSupported
, mCetSupported
, 1);
1048 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &RegEax
, NULL
, NULL
, NULL
);
1049 if (RegEax
<= CPUID_EXTENDED_FUNCTION
) {
1051 // Extended CPUID functions are not supported on this processor.
1053 mXdSupported
= FALSE
;
1054 PatchInstructionX86 (gPatchXdSupported
, mXdSupported
, 1);
1057 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &RegEdx
);
1058 if ((RegEdx
& CPUID1_EDX_XD_SUPPORT
) == 0) {
1060 // Execute Disable Bit feature is not supported on this processor.
1062 mXdSupported
= FALSE
;
1063 PatchInstructionX86 (gPatchXdSupported
, mXdSupported
, 1);
1066 if (StandardSignatureIsAuthenticAMD ()) {
1068 // AMD processors do not support MSR_IA32_MISC_ENABLE
1070 PatchInstructionX86 (gPatchMsrIa32MiscEnableSupported
, FALSE
, 1);
1074 if (mBtsSupported
) {
1075 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &RegEdx
);
1076 if ((RegEdx
& CPUID1_EDX_BTS_AVAILABLE
) != 0) {
1078 // Per IA32 manuals:
1079 // When CPUID.1:EDX[21] is set, the following BTS facilities are available:
1080 // 1. The BTS_UNAVAILABLE flag in the IA32_MISC_ENABLE MSR indicates the
1081 // availability of the BTS facilities, including the ability to set the BTS and
1082 // BTINT bits in the MSR_DEBUGCTLA MSR.
1083 // 2. The IA32_DS_AREA MSR can be programmed to point to the DS save area.
1085 MiscEnableMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_MISC_ENABLE
);
1086 if (MiscEnableMsr
.Bits
.BTS
== 1) {
1088 // BTS facilities is not supported if MSR_IA32_MISC_ENABLE.BTS bit is set.
1090 mBtsSupported
= FALSE
;
1101 ActivateSingleStepDB (
1107 Dr6
= AsmReadDr6 ();
1108 if ((Dr6
& DR6_SINGLE_STEP
) != 0) {
1112 Dr6
|= DR6_SINGLE_STEP
;
1127 DebugCtl
= AsmReadMsr64 (MSR_DEBUG_CTL
);
1128 if ((DebugCtl
& MSR_DEBUG_CTL_LBR
) != 0) {
1132 DebugCtl
|= MSR_DEBUG_CTL_LBR
;
1133 AsmWriteMsr64 (MSR_DEBUG_CTL
, DebugCtl
);
1137 Enable branch trace store.
1139 @param CpuIndex The index of the processor.
1149 DebugCtl
= AsmReadMsr64 (MSR_DEBUG_CTL
);
1150 if ((DebugCtl
& MSR_DEBUG_CTL_BTS
) != 0) {
1154 AsmWriteMsr64 (MSR_DS_AREA
, (UINT64
)(UINTN
)mMsrDsArea
[CpuIndex
]);
1155 DebugCtl
|= (UINT64
)(MSR_DEBUG_CTL_BTS
| MSR_DEBUG_CTL_TR
);
1156 DebugCtl
&= ~((UINT64
)MSR_DEBUG_CTL_BTINT
);
1157 AsmWriteMsr64 (MSR_DEBUG_CTL
, DebugCtl
);
1161 Increase SMI number in each SMI entry.
1165 SmmProfileRecordSmiNum (
1169 if (mSmmProfileStart
) {
1170 mSmmProfileBase
->NumSmis
++;
1175 Initialize processor environment for SMM profile.
1177 @param CpuIndex The index of the processor.
1181 ActivateSmmProfile (
1186 // Enable Single Step DB#
1188 ActivateSingleStepDB ();
1190 if (mBtsSupported
) {
1192 // We can not get useful information from LER, so we have to use BTS.
1199 ActivateBTS (CpuIndex
);
1204 Initialize SMM profile in SMM CPU entry point.
1206 @param[in] Cr3 The base address of the page tables to use in SMM.
1217 mSmmProfileCr3
= Cr3
;
1220 // Skip SMM profile initialization if feature is disabled
1222 if (!FeaturePcdGet (PcdCpuSmmProfileEnable
) &&
1223 !HEAP_GUARD_NONSTOP_MODE
&&
1224 !NULL_DETECTION_NONSTOP_MODE
)
1230 // Initialize SmmProfile here
1232 InitSmmProfileInternal ();
1235 // Initialize profile IDT.
1240 // Tell #PF handler to prepare a #DB subsequently.
1242 mSetupDebugTrap
= TRUE
;
1246 Update page table to map the memory correctly in order to make the instruction
1247 which caused page fault execute successfully. And it also save the original page
1248 table to be restored in single-step exception.
1250 @param PageTable PageTable Address.
1251 @param PFAddress The memory address which caused page fault exception.
1252 @param CpuIndex The index of the processor.
1253 @param ErrorCode The Error code of exception.
1257 RestorePageTableBelow4G (
1267 BOOLEAN Enable5LevelPaging
;
1269 Cr4
.UintN
= AsmReadCr4 ();
1270 Enable5LevelPaging
= (BOOLEAN
)(Cr4
.Bits
.LA57
== 1);
1275 if (Enable5LevelPaging
) {
1276 PTIndex
= (UINTN
)BitFieldRead64 (PFAddress
, 48, 56);
1277 ASSERT (PageTable
[PTIndex
] != 0);
1278 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
1284 if (sizeof (UINT64
) == sizeof (UINTN
)) {
1285 PTIndex
= (UINTN
)BitFieldRead64 (PFAddress
, 39, 47);
1286 ASSERT (PageTable
[PTIndex
] != 0);
1287 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
1293 PTIndex
= (UINTN
)BitFieldRead64 (PFAddress
, 30, 38);
1294 ASSERT (PageTable
[PTIndex
] != 0);
1295 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
1300 PTIndex
= (UINTN
)BitFieldRead64 (PFAddress
, 21, 29);
1301 if ((PageTable
[PTIndex
] & IA32_PG_PS
) != 0) {
1307 // Record old entries with non-present status
1308 // Old entries include the memory which instruction is at and the memory which instruction access.
1311 ASSERT (mPFEntryCount
[CpuIndex
] < MAX_PF_ENTRY_COUNT
);
1312 if (mPFEntryCount
[CpuIndex
] < MAX_PF_ENTRY_COUNT
) {
1313 PFIndex
= mPFEntryCount
[CpuIndex
];
1314 mLastPFEntryValue
[CpuIndex
][PFIndex
] = PageTable
[PTIndex
];
1315 mLastPFEntryPointer
[CpuIndex
][PFIndex
] = &PageTable
[PTIndex
];
1316 mPFEntryCount
[CpuIndex
]++;
1322 PageTable
[PTIndex
] = (PFAddress
& ~((1ull << 21) - 1));
1323 PageTable
[PTIndex
] |= (UINT64
)IA32_PG_PS
;
1324 PageTable
[PTIndex
] |= (UINT64
)PAGE_ATTRIBUTE_BITS
;
1325 if ((ErrorCode
& IA32_PF_EC_ID
) != 0) {
1326 PageTable
[PTIndex
] &= ~IA32_PG_NX
;
1332 ASSERT (PageTable
[PTIndex
] != 0);
1333 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & PHYSICAL_ADDRESS_MASK
);
1338 PTIndex
= (UINTN
)BitFieldRead64 (PFAddress
, 12, 20);
1341 // Record old entries with non-present status
1342 // Old entries include the memory which instruction is at and the memory which instruction access.
1345 ASSERT (mPFEntryCount
[CpuIndex
] < MAX_PF_ENTRY_COUNT
);
1346 if (mPFEntryCount
[CpuIndex
] < MAX_PF_ENTRY_COUNT
) {
1347 PFIndex
= mPFEntryCount
[CpuIndex
];
1348 mLastPFEntryValue
[CpuIndex
][PFIndex
] = PageTable
[PTIndex
];
1349 mLastPFEntryPointer
[CpuIndex
][PFIndex
] = &PageTable
[PTIndex
];
1350 mPFEntryCount
[CpuIndex
]++;
1356 PageTable
[PTIndex
] = (PFAddress
& ~((1ull << 12) - 1));
1357 PageTable
[PTIndex
] |= (UINT64
)PAGE_ATTRIBUTE_BITS
;
1358 if ((ErrorCode
& IA32_PF_EC_ID
) != 0) {
1359 PageTable
[PTIndex
] &= ~IA32_PG_NX
;
1365 Handler for Page Fault triggered by Guard page.
1367 @param ErrorCode The Error code of exception.
1371 GuardPagePFHandler (
1377 UINT64 RestoreAddress
;
1378 UINTN RestorePageNumber
;
1381 PageTable
= (UINT64
*)AsmReadCr3 ();
1382 PFAddress
= AsmReadCr2 ();
1383 CpuIndex
= GetCpuIndex ();
1386 // Memory operation cross pages, like "rep mov" instruction, will cause
1387 // infinite loop between this and Debug Trap handler. We have to make sure
1388 // that current page and the page followed are both in PRESENT state.
1390 RestorePageNumber
= 2;
1391 RestoreAddress
= PFAddress
;
1392 while (RestorePageNumber
> 0) {
1393 RestorePageTableBelow4G (PageTable
, RestoreAddress
, CpuIndex
, ErrorCode
);
1394 RestoreAddress
+= EFI_PAGE_SIZE
;
1395 RestorePageNumber
--;
1405 The Page fault handler to save SMM profile data.
1407 @param Rip The RIP when exception happens.
1408 @param ErrorCode The Error code of exception.
1412 SmmProfilePFHandler (
1419 UINT64 RestoreAddress
;
1420 UINTN RestorePageNumber
;
1423 UINT64 InstructionAddress
;
1424 UINTN MaxEntryNumber
;
1425 UINTN CurrentEntryNumber
;
1426 BOOLEAN IsValidPFAddress
;
1427 SMM_PROFILE_ENTRY
*SmmProfileEntry
;
1431 EFI_SMM_SAVE_STATE_IO_INFO IoInfo
;
1433 if (!mSmmProfileStart
) {
1435 // If SMM profile does not start, call original page fault handler.
1437 SmiDefaultPFHandler ();
1441 if (mBtsSupported
) {
1445 IsValidPFAddress
= FALSE
;
1446 PageTable
= (UINT64
*)AsmReadCr3 ();
1447 PFAddress
= AsmReadCr2 ();
1448 CpuIndex
= GetCpuIndex ();
1451 // Memory operation cross pages, like "rep mov" instruction, will cause
1452 // infinite loop between this and Debug Trap handler. We have to make sure
1453 // that current page and the page followed are both in PRESENT state.
1455 RestorePageNumber
= 2;
1456 RestoreAddress
= PFAddress
;
1457 while (RestorePageNumber
> 0) {
1458 if (RestoreAddress
<= 0xFFFFFFFF) {
1459 RestorePageTableBelow4G (PageTable
, RestoreAddress
, CpuIndex
, ErrorCode
);
1461 RestorePageTableAbove4G (PageTable
, RestoreAddress
, CpuIndex
, ErrorCode
, &IsValidPFAddress
);
1464 RestoreAddress
+= EFI_PAGE_SIZE
;
1465 RestorePageNumber
--;
1468 if (!IsValidPFAddress
) {
1469 InstructionAddress
= Rip
;
1470 if (((ErrorCode
& IA32_PF_EC_ID
) != 0) && (mBtsSupported
)) {
1472 // If it is instruction fetch failure, get the correct IP from BTS.
1474 InstructionAddress
= GetSourceFromDestinationOnBts (CpuIndex
, Rip
);
1475 if (InstructionAddress
== 0) {
1477 // It indicates the instruction which caused page fault is not a jump instruction,
1478 // set instruction address same as the page fault address.
1480 InstructionAddress
= PFAddress
;
1485 // Indicate it is not software SMI
1487 SmiCommand
= 0xFFFFFFFFFFFFFFFFULL
;
1488 for (Index
= 0; Index
< gSmst
->NumberOfCpus
; Index
++) {
1489 Status
= SmmReadSaveState (&mSmmCpu
, sizeof (IoInfo
), EFI_SMM_SAVE_STATE_REGISTER_IO
, Index
, &IoInfo
);
1490 if (EFI_ERROR (Status
)) {
1494 if (IoInfo
.IoPort
== mSmiCommandPort
) {
1496 // A software SMI triggered by SMI command port has been found, get SmiCommand from SMI command port.
1498 SoftSmiValue
= IoRead8 (mSmiCommandPort
);
1499 SmiCommand
= (UINT64
)SoftSmiValue
;
1504 SmmProfileEntry
= (SMM_PROFILE_ENTRY
*)(UINTN
)(mSmmProfileBase
+ 1);
1506 // Check if there is already a same entry in profile data.
1508 for (Index
= 0; Index
< (UINTN
)mSmmProfileBase
->CurDataEntries
; Index
++) {
1509 if ((SmmProfileEntry
[Index
].ErrorCode
== (UINT64
)ErrorCode
) &&
1510 (SmmProfileEntry
[Index
].Address
== PFAddress
) &&
1511 (SmmProfileEntry
[Index
].CpuNum
== (UINT64
)CpuIndex
) &&
1512 (SmmProfileEntry
[Index
].Instruction
== InstructionAddress
) &&
1513 (SmmProfileEntry
[Index
].SmiCmd
== SmiCommand
))
1516 // Same record exist, need not save again.
1522 if (Index
== mSmmProfileBase
->CurDataEntries
) {
1523 CurrentEntryNumber
= (UINTN
)mSmmProfileBase
->CurDataEntries
;
1524 MaxEntryNumber
= (UINTN
)mSmmProfileBase
->MaxDataEntries
;
1525 if (FeaturePcdGet (PcdCpuSmmProfileRingBuffer
)) {
1526 CurrentEntryNumber
= CurrentEntryNumber
% MaxEntryNumber
;
1529 if (CurrentEntryNumber
< MaxEntryNumber
) {
1531 // Log the new entry
1533 SmmProfileEntry
[CurrentEntryNumber
].SmiNum
= mSmmProfileBase
->NumSmis
;
1534 SmmProfileEntry
[CurrentEntryNumber
].ErrorCode
= (UINT64
)ErrorCode
;
1535 SmmProfileEntry
[CurrentEntryNumber
].ApicId
= (UINT64
)GetApicId ();
1536 SmmProfileEntry
[CurrentEntryNumber
].CpuNum
= (UINT64
)CpuIndex
;
1537 SmmProfileEntry
[CurrentEntryNumber
].Address
= PFAddress
;
1538 SmmProfileEntry
[CurrentEntryNumber
].Instruction
= InstructionAddress
;
1539 SmmProfileEntry
[CurrentEntryNumber
].SmiCmd
= SmiCommand
;
1541 // Update current entry index and data size in the header.
1543 mSmmProfileBase
->CurDataEntries
++;
1544 mSmmProfileBase
->CurDataSize
= MultU64x64 (mSmmProfileBase
->CurDataEntries
, sizeof (SMM_PROFILE_ENTRY
));
1554 if (mBtsSupported
) {
1560 Replace INT1 exception handler to restore page table to absent/execute-disable state
1561 in order to trigger page fault again to save SMM profile data..
1571 Status
= SmmRegisterExceptionHandler (&mSmmCpuService
, EXCEPT_IA32_DEBUG
, DebugExceptionHandler
);
1572 ASSERT_EFI_ERROR (Status
);