2015-11-25 05:23:01 +01:00
|
|
|
/** @file
|
|
|
|
SMM CPU misc functions for Ia32 arch specific.
|
|
|
|
|
2016-11-10 06:40:12 +01:00
|
|
|
Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
|
2015-11-25 05:23:01 +01:00
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "PiSmmCpuDxeSmm.h"
|
|
|
|
|
2016-10-23 17:19:52 +02:00
|
|
|
extern UINT64 gTaskGateDescriptor;
|
|
|
|
|
|
|
|
EFI_PHYSICAL_ADDRESS mGdtBuffer;
|
|
|
|
UINTN mGdtBufferSize;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Initialize IDT for SMM Stack Guard.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
EFIAPI
|
|
|
|
InitializeIDTSmmStackGuard (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
IA32_IDT_GATE_DESCRIPTOR *IdtGate;
|
|
|
|
|
|
|
|
//
|
|
|
|
// If SMM Stack Guard feature is enabled, the Page Fault Exception entry in IDT
|
|
|
|
// is a Task Gate Descriptor so that when a Page Fault Exception occurs,
|
|
|
|
// the processors can use a known good stack in case stack is ran out.
|
|
|
|
//
|
|
|
|
IdtGate = (IA32_IDT_GATE_DESCRIPTOR *)gcSmiIdtr.Base;
|
|
|
|
IdtGate += EXCEPT_IA32_PAGE_FAULT;
|
|
|
|
IdtGate->Uint64 = gTaskGateDescriptor;
|
|
|
|
}
|
|
|
|
|
2015-11-25 05:23:01 +01:00
|
|
|
/**
|
|
|
|
Initialize Gdt for all processors.
|
|
|
|
|
|
|
|
@param[in] Cr3 CR3 value.
|
|
|
|
@param[out] GdtStepSize The step size for GDT table.
|
|
|
|
|
|
|
|
@return GdtBase for processor 0.
|
|
|
|
GdtBase for processor X is: GdtBase + (GdtStepSize * X)
|
|
|
|
**/
|
|
|
|
VOID *
|
|
|
|
InitGdt (
|
|
|
|
IN UINTN Cr3,
|
|
|
|
OUT UINTN *GdtStepSize
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UINTN Index;
|
|
|
|
IA32_SEGMENT_DESCRIPTOR *GdtDescriptor;
|
|
|
|
UINTN TssBase;
|
|
|
|
UINTN GdtTssTableSize;
|
|
|
|
UINT8 *GdtTssTables;
|
|
|
|
UINTN GdtTableStepSize;
|
|
|
|
|
|
|
|
if (FeaturePcdGet (PcdCpuSmmStackGuard)) {
|
|
|
|
//
|
|
|
|
// For IA32 SMM, if SMM Stack Guard feature is enabled, we use 2 TSS.
|
|
|
|
// in this case, we allocate separate GDT/TSS for each CPUs to avoid TSS load contention
|
|
|
|
// on each SMI entry.
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Enlarge GDT to contain 2 TSS descriptors
|
|
|
|
//
|
|
|
|
gcSmiGdtr.Limit += (UINT16)(2 * sizeof (IA32_SEGMENT_DESCRIPTOR));
|
|
|
|
|
|
|
|
GdtTssTableSize = (gcSmiGdtr.Limit + 1 + TSS_SIZE * 2 + 7) & ~7; // 8 bytes aligned
|
2016-10-23 17:19:52 +02:00
|
|
|
mGdtBufferSize = GdtTssTableSize * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus;
|
|
|
|
GdtTssTables = (UINT8*)AllocateCodePages (EFI_SIZE_TO_PAGES (mGdtBufferSize));
|
2015-11-25 05:23:01 +01:00
|
|
|
ASSERT (GdtTssTables != NULL);
|
2016-10-23 17:19:52 +02:00
|
|
|
mGdtBuffer = (UINTN)GdtTssTables;
|
2015-11-25 05:23:01 +01:00
|
|
|
GdtTableStepSize = GdtTssTableSize;
|
|
|
|
|
|
|
|
for (Index = 0; Index < gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus; Index++) {
|
|
|
|
CopyMem (GdtTssTables + GdtTableStepSize * Index, (VOID*)(UINTN)gcSmiGdtr.Base, gcSmiGdtr.Limit + 1 + TSS_SIZE * 2);
|
|
|
|
//
|
|
|
|
// Fixup TSS descriptors
|
|
|
|
//
|
|
|
|
TssBase = (UINTN)(GdtTssTables + GdtTableStepSize * Index + gcSmiGdtr.Limit + 1);
|
|
|
|
GdtDescriptor = (IA32_SEGMENT_DESCRIPTOR *)(TssBase) - 2;
|
|
|
|
GdtDescriptor->Bits.BaseLow = (UINT16)TssBase;
|
|
|
|
GdtDescriptor->Bits.BaseMid = (UINT8)(TssBase >> 16);
|
|
|
|
GdtDescriptor->Bits.BaseHigh = (UINT8)(TssBase >> 24);
|
|
|
|
|
|
|
|
TssBase += TSS_SIZE;
|
|
|
|
GdtDescriptor++;
|
|
|
|
GdtDescriptor->Bits.BaseLow = (UINT16)TssBase;
|
|
|
|
GdtDescriptor->Bits.BaseMid = (UINT8)(TssBase >> 16);
|
|
|
|
GdtDescriptor->Bits.BaseHigh = (UINT8)(TssBase >> 24);
|
|
|
|
//
|
|
|
|
// Fixup TSS segments
|
|
|
|
//
|
|
|
|
// ESP as known good stack
|
|
|
|
//
|
|
|
|
*(UINTN *)(TssBase + TSS_IA32_ESP_OFFSET) = mSmmStackArrayBase + EFI_PAGE_SIZE + Index * mSmmStackSize;
|
|
|
|
*(UINT32 *)(TssBase + TSS_IA32_CR3_OFFSET) = Cr3;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Just use original table, AllocatePage and copy them here to make sure GDTs are covered in page memory.
|
|
|
|
//
|
|
|
|
GdtTssTableSize = gcSmiGdtr.Limit + 1;
|
2016-10-23 17:19:52 +02:00
|
|
|
mGdtBufferSize = GdtTssTableSize * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus;
|
|
|
|
GdtTssTables = (UINT8*)AllocateCodePages (EFI_SIZE_TO_PAGES (mGdtBufferSize));
|
2015-11-25 05:23:01 +01:00
|
|
|
ASSERT (GdtTssTables != NULL);
|
2016-10-23 17:19:52 +02:00
|
|
|
mGdtBuffer = (UINTN)GdtTssTables;
|
2015-11-25 05:23:01 +01:00
|
|
|
GdtTableStepSize = GdtTssTableSize;
|
|
|
|
|
|
|
|
for (Index = 0; Index < gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus; Index++) {
|
|
|
|
CopyMem (GdtTssTables + GdtTableStepSize * Index, (VOID*)(UINTN)gcSmiGdtr.Base, gcSmiGdtr.Limit + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*GdtStepSize = GdtTableStepSize;
|
|
|
|
return GdtTssTables;
|
|
|
|
}
|
2016-11-10 06:40:12 +01:00
|
|
|
|
2016-11-23 14:24:32 +01:00
|
|
|
/**
|
|
|
|
This function sets GDT/IDT buffer to be RO and XP.
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
PatchGdtIdtMap (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_PHYSICAL_ADDRESS BaseAddress;
|
|
|
|
UINTN Size;
|
|
|
|
|
|
|
|
//
|
|
|
|
// GDT
|
|
|
|
//
|
|
|
|
DEBUG ((DEBUG_INFO, "PatchGdtIdtMap - GDT:\n"));
|
|
|
|
|
|
|
|
BaseAddress = mGdtBuffer;
|
|
|
|
Size = ALIGN_VALUE(mGdtBufferSize, SIZE_4KB);
|
|
|
|
if (!FeaturePcdGet (PcdCpuSmmStackGuard)) {
|
|
|
|
//
|
|
|
|
// Do not set RO for IA32 when stack guard feature is enabled.
|
|
|
|
// Stack Guard need use task switch to switch stack.
|
|
|
|
// It need write GDT and TSS.
|
|
|
|
//
|
|
|
|
SmmSetMemoryAttributes (
|
|
|
|
BaseAddress,
|
|
|
|
Size,
|
|
|
|
EFI_MEMORY_RO
|
|
|
|
);
|
|
|
|
}
|
|
|
|
SmmSetMemoryAttributes (
|
|
|
|
BaseAddress,
|
|
|
|
Size,
|
|
|
|
EFI_MEMORY_XP
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// IDT
|
|
|
|
//
|
|
|
|
DEBUG ((DEBUG_INFO, "PatchGdtIdtMap - IDT:\n"));
|
|
|
|
|
|
|
|
BaseAddress = gcSmiIdtr.Base;
|
|
|
|
Size = ALIGN_VALUE(gcSmiIdtr.Limit + 1, SIZE_4KB);
|
|
|
|
SmmSetMemoryAttributes (
|
|
|
|
BaseAddress,
|
|
|
|
Size,
|
|
|
|
EFI_MEMORY_RO
|
|
|
|
);
|
|
|
|
SmmSetMemoryAttributes (
|
|
|
|
BaseAddress,
|
|
|
|
Size,
|
|
|
|
EFI_MEMORY_XP
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-10 06:40:12 +01:00
|
|
|
/**
|
|
|
|
Transfer AP to safe hlt-loop after it finished restore CPU features on S3 patch.
|
|
|
|
|
2016-11-17 21:41:35 +01:00
|
|
|
@param[in] ApHltLoopCode The address of the safe hlt-loop function.
|
|
|
|
@param[in] TopOfStack A pointer to the new stack to use for the ApHltLoopCode.
|
|
|
|
@param[in] NumberToFinishAddress Address of Semaphore of APs finish count.
|
2016-11-10 06:40:12 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
TransferApToSafeState (
|
2016-11-17 21:41:35 +01:00
|
|
|
IN UINTN ApHltLoopCode,
|
|
|
|
IN UINTN TopOfStack,
|
|
|
|
IN UINTN NumberToFinishAddress
|
2016-11-10 06:40:12 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
SwitchStack (
|
2016-11-17 21:41:35 +01:00
|
|
|
(SWITCH_STACK_ENTRY_POINT)ApHltLoopCode,
|
|
|
|
(VOID *)NumberToFinishAddress,
|
2016-11-10 06:40:12 +01:00
|
|
|
NULL,
|
2016-11-17 21:41:35 +01:00
|
|
|
(VOID *)TopOfStack
|
2016-11-10 06:40:12 +01:00
|
|
|
);
|
|
|
|
//
|
|
|
|
// It should never reach here
|
|
|
|
//
|
|
|
|
ASSERT (FALSE);
|
|
|
|
}
|