14extern BOOLEAN mCetSupported;
16X86_ASSEMBLY_PATCH_LABEL mPatchCetPl0Ssp;
17X86_ASSEMBLY_PATCH_LABEL mPatchCetInterruptSsp;
18X86_ASSEMBLY_PATCH_LABEL mPatchCetInterruptSspTable;
20UINT32 mCetInterruptSsp;
21UINT32 mCetInterruptSspTable;
23UINTN mSmmInterruptSspTables;
39 IA32_IDT_GATE_DESCRIPTOR *IdtGate;
41 IdtGate = (IA32_IDT_GATE_DESCRIPTOR *)gcSmiIdtr.Base;
42 IdtGate += ExceptionType;
43 IdtGate->Bits.Reserved_0 = Ist;
62 IA32_SEGMENT_DESCRIPTOR *GdtDescriptor;
64 UINTN GdtTssTableSize;
66 UINTN GdtTableStepSize;
72 GdtTssTableSize = (gcSmiGdtr.Limit + 1 + TSS_SIZE + 7) & ~7;
73 mGdtBufferSize = GdtTssTableSize * gSmmCpuPrivate->SmmCoreEntryContext.
NumberOfCpus;
75 ASSERT (GdtTssTables !=
NULL);
76 mGdtBuffer = (
UINTN)GdtTssTables;
77 GdtTableStepSize = GdtTssTableSize;
79 for (Index = 0; Index < gSmmCpuPrivate->SmmCoreEntryContext.
NumberOfCpus; Index++) {
80 CopyMem (GdtTssTables + GdtTableStepSize * Index, (VOID *)(
UINTN)gcSmiGdtr.Base, gcSmiGdtr.Limit + 1 + TSS_SIZE);
85 TssBase = (
UINTN)(GdtTssTables + GdtTableStepSize * Index + gcSmiGdtr.Limit + 1);
86 GdtDescriptor = (IA32_SEGMENT_DESCRIPTOR *)(TssBase) - 2;
87 GdtDescriptor->Bits.BaseLow = (UINT16)(
UINTN)TssBase;
88 GdtDescriptor->Bits.BaseMid = (UINT8)((
UINTN)TssBase >> 16);
89 GdtDescriptor->Bits.BaseHigh = (UINT8)((
UINTN)TssBase >> 24);
91 if ((
FeaturePcdGet (PcdCpuSmmStackGuard)) || ((
PcdGet32 (PcdControlFlowEnforcementPropertyMask) != 0) && mCetSupported)) {
95 *(
UINTN *)(TssBase + TSS_X64_IST1_OFFSET) = (mSmmStackArrayBase + EFI_PAGE_SIZE + Index * (mSmmStackSize + mSmmShadowStackSize));
99 *GdtStepSize = GdtTableStepSize;
113 IA32_DESCRIPTOR GdtrDesc;
114 IA32_SEGMENT_DESCRIPTOR *GdtEntry;
119 GdtEntryCount = (GdtrDesc.Limit + 1) /
sizeof (IA32_SEGMENT_DESCRIPTOR);
120 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *)GdtrDesc.Base;
121 for (Index = 0; (
UINTN)Index < GdtEntryCount; Index++) {
122 if (GdtEntry->Bits.L == 0) {
123 if ((GdtEntry->Bits.Type > 8) && (GdtEntry->Bits.DB == 1)) {
131 ASSERT (Index != GdtEntryCount);
147 UINTN SmmShadowStackSize;
148 UINT64 *InterruptSspTable;
151 if ((
PcdGet32 (PcdControlFlowEnforcementPropertyMask) != 0) && mCetSupported) {
165 mCetPl0Ssp = (UINT32)((
UINTN)ShadowStack + SmmShadowStackSize -
sizeof (UINT64));
167 DEBUG ((DEBUG_INFO,
"mCetPl0Ssp - 0x%x\n", mCetPl0Ssp));
168 DEBUG ((DEBUG_INFO,
"ShadowStack - 0x%x\n", ShadowStack));
169 DEBUG ((DEBUG_INFO,
" SmmShadowStackSize - 0x%x\n", SmmShadowStackSize));
171 if (mSmmInterruptSspTables == 0) {
173 ASSERT (mSmmInterruptSspTables != 0);
174 DEBUG ((DEBUG_INFO,
"mSmmInterruptSspTables - 0x%x\n", mSmmInterruptSspTables));
187 *(UINT64 *)(
UINTN)InterruptSsp = (InterruptSsp -
sizeof (UINT64) * 4) | 0x2;
188 mCetInterruptSsp = InterruptSsp -
sizeof (UINT64);
190 mCetInterruptSspTable = (UINT32)(
UINTN)(mSmmInterruptSspTables +
sizeof (UINT64) * 8 * CpuIndex);
191 InterruptSspTable = (UINT64 *)(
UINTN)mCetInterruptSspTable;
192 InterruptSspTable[1] = mCetInterruptSsp;
195 DEBUG ((DEBUG_INFO,
"mCetInterruptSsp - 0x%x\n", mCetInterruptSsp));
196 DEBUG ((DEBUG_INFO,
"mCetInterruptSspTable - 0x%x\n", mCetInterruptSspTable));
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID InitShadowStack(IN UINTN CpuIndex, IN VOID *ShadowStack)
VOID * InitGdt(IN UINTN Cr3, OUT UINTN *GdtStepSize)
#define DEBUG(Expression)
#define PcdGet32(TokenName)
#define FeaturePcdGet(TokenName)
STATIC VOID *EFIAPI AllocateCodePages(IN UINTN Pages)
UINT64 EFI_PHYSICAL_ADDRESS
#define EFI_PAGES_TO_SIZE(Pages)
#define EFI_SIZE_TO_PAGES(Size)
VOID EFIAPI InitializeIdtIst(IN EFI_EXCEPTION_TYPE ExceptionType, IN UINT8 Ist)
UINT16 GetProtectedModeCS(VOID)
VOID EFIAPI PatchInstructionX86(OUT X86_ASSEMBLY_PATCH_LABEL *InstructionEnd, IN UINT64 PatchValue, IN UINTN ValueSize)
VOID EFIAPI AsmReadGdtr(OUT IA32_DESCRIPTOR *Gdtr)