Skip to content

Commit

Permalink
Ovmfpkg/CcExitLib: Provide SVSM discovery support
Browse files Browse the repository at this point in the history
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4654

The SVSM specification documents an alternative method of discovery for
the SVSM using a reserved CPUID bit and a reserved MSR.

For the CPUID support, the #VC handler of an SEV-SNP guest should modify
the returned value in the EAX register for the 0x8000001f CPUID function
by setting bit 28 when an SVSM is present.

For the MSR support, new reserved MSR 0xc001f000 has been defined. A #VC
should be generated when accessing this MSR. The #VC handler is expected
to ignore writes to this MSR and return the physical calling area address
(CAA) on reads of this MSR.

Cc: Ard Biesheuvel <[email protected]>
Cc: Erdem Aktas <[email protected]>
Cc: Gerd Hoffmann <[email protected]>
Cc: Jiewen Yao <[email protected]>
Cc: Laszlo Ersek <[email protected]>
Cc: Michael Roth <[email protected]>
Cc: Min Xu <[email protected]>
Acked-by: Gerd Hoffmann <[email protected]>
Signed-off-by: Tom Lendacky <[email protected]>
  • Loading branch information
tlendacky authored and mergify[bot] committed Apr 17, 2024
1 parent a010681 commit 47001ab
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
3 changes: 2 additions & 1 deletion OvmfPkg/Library/CcExitLib/CcExitLib.inf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## @file
# CcExitLib Library.
#
# Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved.<BR>
# Copyright (C) 2020 - 2024, Advanced Micro Devices, Inc. All rights reserved.<BR>
# Copyright (C) 2020 - 2022, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
Expand Down Expand Up @@ -41,6 +41,7 @@
DebugLib
LocalApicLib
MemEncryptSevLib
AmdSvsmLib

[Pcd]
gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCpuidBase
Expand Down
29 changes: 27 additions & 2 deletions OvmfPkg/Library/CcExitLib/CcExitVcHandler.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @file
X64 #VC Exception Handler functon.
Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved.<BR>
Copyright (C) 2020 - 2024, Advanced Micro Devices, Inc. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
Expand All @@ -12,6 +12,7 @@
#include <Library/LocalApicLib.h>
#include <Library/MemEncryptSevLib.h>
#include <Library/CcExitLib.h>
#include <Library/AmdSvsmLib.h>
#include <Register/Amd/Msr.h>
#include <Register/Intel/Cpuid.h>
#include <IndustryStandard/InstructionParsing.h>
Expand Down Expand Up @@ -713,10 +714,29 @@ MsrExit (
IN CC_INSTRUCTION_DATA *InstructionData
)
{
UINT64 ExitInfo1, Status;
MSR_SVSM_CAA_REGISTER Msr;
UINT64 ExitInfo1;
UINT64 Status;

ExitInfo1 = 0;

//
// The SVSM CAA MSR is a software implemented MSR and not supported
// by the hardware, handle it directly.
//
if (Regs->Rax == MSR_SVSM_CAA) {
// Writes to the SVSM CAA MSR are ignored
if (*(InstructionData->OpCodes + 1) == 0x30) {
return 0;
}

Msr.Uint64 = AmdSvsmSnpGetCaa ();
Regs->Rax = Msr.Bits.Lower32Bits;
Regs->Rdx = Msr.Bits.Upper32Bits;

return 0;
}

switch (*(InstructionData->OpCodes + 1)) {
case 0x30: // WRMSR
ExitInfo1 = 1;
Expand Down Expand Up @@ -1388,6 +1408,11 @@ GetCpuidFw (
*Ebx = (*Ebx & 0xFFFFFF00) | (Ebx2 & 0x000000FF);
/* node ID */
*Ecx = (*Ecx & 0xFFFFFF00) | (Ecx2 & 0x000000FF);
} else if (EaxIn == 0x8000001F) {
/* Set the SVSM feature bit if running under an SVSM */
if (AmdSvsmIsSvsmPresent ()) {
*Eax |= BIT28;
}
}

Out:
Expand Down
3 changes: 2 additions & 1 deletion OvmfPkg/Library/CcExitLib/SecCcExitLib.inf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## @file
# VMGEXIT Support Library.
#
# Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved.<BR>
# Copyright (C) 2020 - 2024, Advanced Micro Devices, Inc. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
##
Expand Down Expand Up @@ -41,6 +41,7 @@
LocalApicLib
MemEncryptSevLib
PcdLib
AmdSvsmLib

[FixedPcd]
gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBackupBase
Expand Down

0 comments on commit 47001ab

Please sign in to comment.