Skip to contentSkip to conversation

Enable Secure TSC for SEV-SNP

PATCH Superseded Nikunj A Dadhania 3 participants 11 messages Mar 17, 2025 – Mar 19, 2025

Subsystems (3)

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v5 cover Enable Secure TSC for SEV-SNP

The hypervisor controls TSC value calculations for the guest. A malicious hypervisor can prevent the guest from progressing. The Secure TSC feature for SEV-SNP allows guests to securely use the RDTSC and RDTSCP instructions. This ensures the guest has a consistent view of time and prevents a malicious hypervisor from manipulating time, such as making it appear to move backward or advance too quickly. For more details, refer to the "Secure Nested Paging (SEV-SNP)" section, subsection "Secure TSC" in APM Volume 2.

This patch set is also available at:

https://github.com/AMDESE/linux-kvm/tree/sectsc-host-latest

and is based on kvm/queue

Testing Secure TSC
-----------------

Secure TSC guest patches are available as part of v6.14-rc1.

QEMU changes:
https://github.com/nikunjad/qemu/tree/snp-securetsc-latest

QEMU command line SEV-SNP with Secure TSC:

  qemu-system-x86_64 -cpu EPYC-Milan-v2 -smp 4 \
    -object memory-backend-memfd,id=ram1,size=1G,share=true,prealloc=false,reserve=false \
    -object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,secure-tsc=on,stsc-freq=2000000000 \
    -machine q35,confidential-guest-support=sev0,memory-backend=ram1 \
    ...

Changelog:
----------
v5:
* Rebased on top of kvm/queue that includes protected TSC patches
https://lore.kernel.org/kvm/[email protected]/
* Dropped patch 4/5 as it is not required after protected TSC patches
* Set guest_tsc_protected when Secure TSC is enabled (Paolo)
* Collect Reviewed-by from Tom
* Base the desired_tsc_freq on KVM's ABI (Sean)

v4: https://lore.kernel.org/kvm/[email protected]/
* Rebased on top of latest kvm-x86/next
* Collect Reviewed-by from Tom
* Use "KVM: SVM" instead of "crypto: ccp" (Tom)
* Clear the intercept in sev_es_init_vmcb() (Tom)
* Differentiate between guest and host MSR_IA32_TSC writes (Tom)

Ketan Chaturvedi (1):
KVM: SVM: Enable Secure TSC for SNP guests

Nikunj A Dadhania (3):
x86/cpufeatures: Add SNP Secure TSC
KVM: SVM: Add missing member in SNP_LAUNCH_START command structure
KVM: SVM: Add GUEST_TSC_FREQ MSR for Secure TSC enabled guests

 arch/x86/include/asm/cpufeatures.h |  1 +
 arch/x86/include/asm/svm.h         |  1 +
 arch/x86/include/uapi/asm/kvm.h    |  3 ++-
 arch/x86/kvm/svm/sev.c             | 17 +++++++++++++++++
 arch/x86/kvm/svm/svm.c             |  1 +
 arch/x86/kvm/svm/svm.h             | 11 ++++++++++-
 include/linux/psp-sev.h            |  2 ++
 7 files changed, 34 insertions(+), 2 deletions(-)

base-commit: 9f443c33263385cbb8565ab58db3f7983e769bed

Signature · 1 line
2.43.0
#1 lore raw

PATCH v5 1/4 x86/cpufeatures: Add SNP Secure TSC

The Secure TSC feature for SEV-SNP allows guests to securely use the RDTSC and RDTSCP instructions, ensuring that the parameters used cannot be altered by the hypervisor once the guest is launched. For more details, refer to the AMD64 APM Vol 2, Section "Secure TSC".

---
 arch/x86/include/asm/cpufeatures.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index 508c0dad116b..921ed26b0be7 100644
@@ -448,6 +448,7 @@
 #define X86_FEATURE_VM_PAGE_FLUSH	(19*32+ 2) /* VM Page Flush MSR is supported */
 #define X86_FEATURE_SEV_ES		(19*32+ 3) /* "sev_es" Secure Encrypted Virtualization - Encrypted State */
 #define X86_FEATURE_SEV_SNP		(19*32+ 4) /* "sev_snp" Secure Encrypted Virtualization - Secure Nested Paging */
+#define X86_FEATURE_SNP_SECURE_TSC	(19*32+ 8) /* SEV-SNP Secure TSC */
 #define X86_FEATURE_V_TSC_AUX		(19*32+ 9) /* Virtual TSC_AUX */
 #define X86_FEATURE_SME_COHERENT	(19*32+10) /* hardware-enforced cache coherency */
 #define X86_FEATURE_DEBUG_SWAP		(19*32+14) /* "debug_swap" SEV-ES full debug state swap support */
Signature · 1 line
2.43.0
#2 lore raw

PATCH v5 2/4 KVM: SVM: Add missing member in SNP_LAUNCH_START command structure

In reply to Nikunj A Dadhania

The sev_data_snp_launch_start structure should include a 4-byte desired_tsc_khz field before the gosvw field, which was missed in the initial implementation. As a result, the structure is 4 bytes shorter than expected by the firmware, causing the gosvw field to start 4 bytes early. Fix this by adding the missing 4-byte member for the desired TSC frequency.

Fixes 3a45dc2b419e ("crypto: ccp: Define the SEV-SNP commands")
---
 include/linux/psp-sev.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/psp-sev.h +2 -0
index f3cad182d4ef..1f3620aaa4e7 100644
@@ -594,6 +594,7 @@ struct sev_data_snp_addr {
  * @imi_en: launch flow is launching an IMI (Incoming Migration Image) for the
  *          purpose of guest-assisted migration.
  * @rsvd: reserved
+ * @desired_tsc_khz: hypervisor desired mean TSC freq in kHz of the guest
  * @gosvw: guest OS-visible workarounds, as defined by hypervisor
  */
 struct sev_data_snp_launch_start {
@@ -603,6 +604,7 @@ struct sev_data_snp_launch_start {
 	u32 ma_en:1;				/* In */
 	u32 imi_en:1;				/* In */
 	u32 rsvd:30;
+	u32 desired_tsc_khz;			/* In */
 	u8 gosvw[16];				/* In */
 } __packed;
 
Signature · 1 line
2.43.0
#3 lore raw

PATCH v5 3/4 KVM: SVM: Add GUEST_TSC_FREQ MSR for Secure TSC enabled guests

In reply to Nikunj A Dadhania

Introduce the read-only MSR GUEST_TSC_FREQ (0xc0010134) that returns guest's effective frequency in MHZ when Secure TSC is enabled for SNP guests. Disable interception of this MSR when Secure TSC is enabled. Note that GUEST_TSC_FREQ MSR is accessible only to the guest and not from the hypervisor context.

---
 arch/x86/include/asm/svm.h |  1 +
 arch/x86/kvm/svm/sev.c     |  3 +++
 arch/x86/kvm/svm/svm.c     |  1 +
 arch/x86/kvm/svm/svm.h     | 11 ++++++++++-
 4 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/include/asm/svm.h +1 -0
index e2fac21471f5..a04346068c60 100644
@@ -289,6 +289,7 @@ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_
 #define SVM_SEV_FEAT_RESTRICTED_INJECTION		BIT(3)
 #define SVM_SEV_FEAT_ALTERNATE_INJECTION		BIT(4)
 #define SVM_SEV_FEAT_DEBUG_SWAP				BIT(5)
+#define SVM_SEV_FEAT_SECURE_TSC				BIT(9)
 
 #define SVM_SEV_FEAT_INT_INJ_MODES		\
 	(SVM_SEV_FEAT_RESTRICTED_INJECTION |	\
arch/x86/kvm/svm/sev.c +3 -0
index 661108d65ee7..80a80929e6a3 100644
@@ -4555,6 +4555,9 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm)
 	/* Clear intercepts on selected MSRs */
 	set_msr_interception(vcpu, svm->msrpm, MSR_EFER, 1, 1);
 	set_msr_interception(vcpu, svm->msrpm, MSR_IA32_CR_PAT, 1, 1);
+
+	if (snp_secure_tsc_enabled(vcpu->kvm))
+		set_msr_interception(vcpu, svm->msrpm, MSR_AMD64_GUEST_TSC_FREQ, 1, 1);
 }
 
 void sev_init_vmcb(struct vcpu_svm *svm)
arch/x86/kvm/svm/svm.c +1 -0
index e67de787fc71..6e1608f04b85 100644
@@ -143,6 +143,7 @@ static const struct svm_direct_access_msrs {
 	{ .index = X2APIC_MSR(APIC_TMICT),		.always = false },
 	{ .index = X2APIC_MSR(APIC_TMCCT),		.always = false },
 	{ .index = X2APIC_MSR(APIC_TDCR),		.always = false },
+	{ .index = MSR_AMD64_GUEST_TSC_FREQ,		.always = false },
 	{ .index = MSR_INVALID,				.always = false },
 };
 
arch/x86/kvm/svm/svm.h +10 -1
index ea44c1da5a7c..513681604268 100644
@@ -44,7 +44,7 @@ static inline struct page *__sme_pa_to_page(unsigned long pa)
 #define	IOPM_SIZE PAGE_SIZE * 3
 #define	MSRPM_SIZE PAGE_SIZE * 2
 
-#define MAX_DIRECT_ACCESS_MSRS	48
+#define MAX_DIRECT_ACCESS_MSRS	49
 #define MSRPM_OFFSETS	32
 extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
 extern bool npt_enabled;
@@ -379,10 +379,19 @@ static __always_inline bool sev_snp_guest(struct kvm *kvm)
 	return (sev->vmsa_features & SVM_SEV_FEAT_SNP_ACTIVE) &&
 	       !WARN_ON_ONCE(!sev_es_guest(kvm));
 }
+
+static inline bool snp_secure_tsc_enabled(struct kvm *kvm)
+{
+	struct kvm_sev_info *sev = to_kvm_sev_info(kvm);
+
+	return (sev->vmsa_features & SVM_SEV_FEAT_SECURE_TSC) &&
+		!WARN_ON_ONCE(!sev_snp_guest(kvm));
+}
 #else
 #define sev_guest(kvm) false
 #define sev_es_guest(kvm) false
 #define sev_snp_guest(kvm) false
+#define snp_secure_tsc_enabled(kvm) false
 #endif
 
 static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val)
Signature · 1 line
2.43.0
#4 lore raw

PATCH v5 4/4 KVM: SVM: Enable Secure TSC for SNP guests

In reply to Nikunj A Dadhania

From: Ketan Chaturvedi <[email protected]>

Add support for Secure TSC, allowing userspace to configure the Secure TSC feature for SNP guests. Use the SNP specification's desired TSC frequency parameter during the SNP_LAUNCH_START command to set the mean TSC frequency in KHz for Secure TSC enabled guests.

As the frequency needs to be set in the SNP_LAUNCH_START command, userspace should set the frequency using the KVM_CAP_SET_TSC_KHZ VM ioctl instead of the VCPU ioctl. The desired_tsc_khz defaults to kvm->arch.default_tsc_khz.

---
 arch/x86/include/uapi/asm/kvm.h |  3 ++-
 arch/x86/kvm/svm/sev.c          | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
arch/x86/include/uapi/asm/kvm.h +2 -1
index 9e75da97bce0..87ed9f77314d 100644
@@ -836,7 +836,8 @@ struct kvm_sev_snp_launch_start {
 	__u64 policy;
 	__u8 gosvw[16];
 	__u16 flags;
-	__u8 pad0[6];
+	__u8 pad0[2];
+	__u32 desired_tsc_khz;
 	__u64 pad1[4];
 };
 
arch/x86/kvm/svm/sev.c +14 -0
index 80a80929e6a3..4ee8d233f61f 100644
@@ -2226,6 +2226,14 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
 
 	start.gctx_paddr = __psp_pa(sev->snp_context);
 	start.policy = params.policy;
+
+	if (snp_secure_tsc_enabled(kvm)) {
+		if (!kvm->arch.default_tsc_khz)
+			return -EINVAL;
+
+		start.desired_tsc_khz = kvm->arch.default_tsc_khz;
+	}
+
 	memcpy(start.gosvw, params.gosvw, sizeof(params.gosvw));
 	rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_START, &start, &argp->error);
 	if (rc) {
@@ -2467,6 +2475,9 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
 		}
 
 		svm->vcpu.arch.guest_state_protected = true;
+		if (snp_secure_tsc_enabled(kvm))
+			svm->vcpu.arch.guest_tsc_protected = true;
+
 		/*
 		 * SEV-ES (and thus SNP) guest mandates LBR Virtualization to
 		 * be _always_ ON. Enable it only after setting
@@ -3079,6 +3090,9 @@ void __init sev_hardware_setup(void)
 	sev_supported_vmsa_features = 0;
 	if (sev_es_debug_swap_enabled)
 		sev_supported_vmsa_features |= SVM_SEV_FEAT_DEBUG_SWAP;
+
+	if (sev_snp_enabled && cpu_feature_enabled(X86_FEATURE_SNP_SECURE_TSC))
+		sev_supported_vmsa_features |= SVM_SEV_FEAT_SECURE_TSC;
 }
 
 void sev_hardware_unsetup(void)
Signature · 1 line
2.43.0
Reviewed-by on v5 4/4
#5 lore raw

Re: [PATCH v5 4/4] KVM: SVM: Enable Secure TSC for SNP guests

Quoted 7 lines · Nikunj A Dadhania

From: Ketan Chaturvedi <[email protected]>

Add support for Secure TSC, allowing userspace to configure the Secure TSC feature for SNP guests. Use the SNP specification's desired TSC frequency parameter during the SNP_LAUNCH_START command to set the mean TSC frequency in KHz for Secure TSC enabled guests.

Quoted 7 lines

As the frequency needs to be set in the SNP_LAUNCH_START command, userspace should set the frequency using the KVM_CAP_SET_TSC_KHZ VM ioctl instead of the VCPU ioctl. The desired_tsc_khz defaults to kvm->arch.default_tsc_khz.

Just one minor comment below, that can be ignored unless you have to do another version.

Quoted 39 lines
---
 arch/x86/include/uapi/asm/kvm.h |  3 ++-
 arch/x86/kvm/svm/sev.c          | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
arch/x86/include/uapi/asm/kvm.h +2 -1
index 9e75da97bce0..87ed9f77314d 100644
@@ -836,7 +836,8 @@ struct kvm_sev_snp_launch_start {
 	__u64 policy;
 	__u8 gosvw[16];
 	__u16 flags;
-	__u8 pad0[6];
+	__u8 pad0[2];
+	__u32 desired_tsc_khz;
 	__u64 pad1[4];
 };
 
arch/x86/kvm/svm/sev.c +8 -0
index 80a80929e6a3..4ee8d233f61f 100644
@@ -2226,6 +2226,14 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
 
 	start.gctx_paddr = __psp_pa(sev->snp_context);
 	start.policy = params.policy;
+
+	if (snp_secure_tsc_enabled(kvm)) {
+		if (!kvm->arch.default_tsc_khz)
+			return -EINVAL;
+
+		start.desired_tsc_khz = kvm->arch.default_tsc_khz;
+	}
+
 	memcpy(start.gosvw, params.gosvw, sizeof(params.gosvw));
 	rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_START, &start, &argp->error);
 	if (rc) {

arch/x86/kvm/svm/sev.c static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2480

Quoted 6 lines
arch/x86/kvm/svm/sev.c +3 -0
@@ -2467,6 +2475,9 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
 		}
 
 		svm->vcpu.arch.guest_state_protected = true;
+		if (snp_secure_tsc_enabled(kvm))
+			svm->vcpu.arch.guest_tsc_protected = true;
+

This could just be:

	vcpu->arch.guest_tsc_protected = snp_secure_tsc_enabled(kvm);

(and you could clean up the line above to:

	vcpu->arch.guest_state_protected = true;

while you're at it.)

Thanks,
Tom

Quoted 13 lines
arch/x86/kvm/svm/sev.c +3 -0
 		/*
 		 * SEV-ES (and thus SNP) guest mandates LBR Virtualization to
 		 * be _always_ ON. Enable it only after setting
@@ -3079,6 +3090,9 @@ void __init sev_hardware_setup(void)
 	sev_supported_vmsa_features = 0;
 	if (sev_es_debug_swap_enabled)
 		sev_supported_vmsa_features |= SVM_SEV_FEAT_DEBUG_SWAP;
+
+	if (sev_snp_enabled && cpu_feature_enabled(X86_FEATURE_SNP_SECURE_TSC))
+		sev_supported_vmsa_features |= SVM_SEV_FEAT_SECURE_TSC;
 }
 
 void sev_hardware_unsetup(void)
#6 lore raw

PATCH 1/1 KVM: SVM: Enable Secure TSC for SNP guests

From: Ketan Chaturvedi <[email protected]>

Add support for Secure TSC, allowing userspace to configure the Secure TSC feature for SNP guests. Use the SNP specification's desired TSC frequency parameter during the SNP_LAUNCH_START command to set the mean TSC frequency in KHz for Secure TSC enabled guests.

As the frequency needs to be set in the SNP_LAUNCH_START command, userspace should set the frequency using the KVM_CAP_SET_TSC_KHZ VM ioctl instead of the VCPU ioctl. The desired_tsc_khz defaults to kvm->arch.default_tsc_khz.

---
 arch/x86/include/uapi/asm/kvm.h |  3 ++-
 arch/x86/kvm/svm/sev.c          | 15 ++++++++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)
arch/x86/include/uapi/asm/kvm.h +2 -1
index 9e75da97bce0..87ed9f77314d 100644
@@ -836,7 +836,8 @@ struct kvm_sev_snp_launch_start {
 	__u64 policy;
 	__u8 gosvw[16];
 	__u16 flags;
-	__u8 pad0[6];
+	__u8 pad0[2];
+	__u32 desired_tsc_khz;
 	__u64 pad1[4];
 };
 
arch/x86/kvm/svm/sev.c +14 -1
index 80a80929e6a3..c33b3bc598a7 100644
@@ -2226,6 +2226,14 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
 
 	start.gctx_paddr = __psp_pa(sev->snp_context);
 	start.policy = params.policy;
+
+	if (snp_secure_tsc_enabled(kvm)) {
+		if (!kvm->arch.default_tsc_khz)
+			return -EINVAL;
+
+		start.desired_tsc_khz = kvm->arch.default_tsc_khz;
+	}
+
 	memcpy(start.gosvw, params.gosvw, sizeof(params.gosvw));
 	rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_START, &start, &argp->error);
 	if (rc) {
@@ -2466,7 +2474,9 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
 			return ret;
 		}
 
-		svm->vcpu.arch.guest_state_protected = true;
+		vcpu->arch.guest_state_protected = true;
+		vcpu->arch.guest_tsc_protected = snp_secure_tsc_enabled(kvm);
+
 		/*
 		 * SEV-ES (and thus SNP) guest mandates LBR Virtualization to
 		 * be _always_ ON. Enable it only after setting
@@ -3079,6 +3089,9 @@ void __init sev_hardware_setup(void)
 	sev_supported_vmsa_features = 0;
 	if (sev_es_debug_swap_enabled)
 		sev_supported_vmsa_features |= SVM_SEV_FEAT_DEBUG_SWAP;
+
+	if (sev_snp_enabled && cpu_feature_enabled(X86_FEATURE_SNP_SECURE_TSC))
+		sev_supported_vmsa_features |= SVM_SEV_FEAT_SECURE_TSC;
 }
 
 void sev_hardware_unsetup(void)
Signature · 1 line
2.43.0
#7 lore raw
In reply to Nikunj A Dadhania
Quoted 22 lines · Nikunj A Dadhania

The hypervisor controls TSC value calculations for the guest. A malicious hypervisor can prevent the guest from progressing. The Secure TSC feature for SEV-SNP allows guests to securely use the RDTSC and RDTSCP instructions. This ensures the guest has a consistent view of time and prevents a malicious hypervisor from manipulating time, such as making it appear to move backward or advance too quickly. For more details, refer to the "Secure Nested Paging (SEV-SNP)" section, subsection "Secure TSC" in APM Volume 2.

This patch set is also available at:

https://github.com/AMDESE/linux-kvm/tree/sectsc-host-latest

and is based on kvm/queue

Testing Secure TSC
-----------------

Secure TSC guest patches are available as part of v6.14-rc1.

QEMU changes:
https://github.com/nikunjad/qemu/tree/snp-securetsc-latest

Quoted 8 lines

QEMU command line SEV-SNP with Secure TSC:

   qemu-system-x86_64 -cpu EPYC-Milan-v2 -smp 4 \
     -object memory-backend-memfd,id=ram1,size=1G,share=true,prealloc=false,reserve=false \
     -object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,secure-tsc=on,stsc-freq=2000000000 \
     -machine q35,confidential-guest-support=sev0,memory-backend=ram1 \
     ...

Hi Nikunj,

I've been trying to test this patchset with the above QEMU command line and with the OVMF built from upstream master. But I'm encountering following errors:

" !!!!!!!! Image Section Alignment(0x40) does not match Required Alignment (0x1000) !!!!!!!!
ProtectUefiImage failed to create image properties record "

I briefly looked at this[1] branch as well but it appears to be no longer actively maintained as I ran into some build errors which are fixed in upstream.

The build command I'm using to build the OVMF is as follows:
build -a X64 -b DEBUG -t GCC5 -D DEBUG_VERBOSE -p OvmfPkg/OvmfPkgX64.dsc

So, I was wondering if you've some extra patches on top of upstream OVMF to test SecureTSC or are there any modifications required in my build command?

Thank you!

[1] https://github.com/AMDESE/ovmf/tree/snp-latest

Quoted 36 lines

Changelog:
----------
v5:
* Rebased on top of kvm/queue that includes protected TSC patches
https://lore.kernel.org/kvm/[email protected]/
* Dropped patch 4/5 as it is not required after protected TSC patches
* Set guest_tsc_protected when Secure TSC is enabled (Paolo)
* Collect Reviewed-by from Tom
* Base the desired_tsc_freq on KVM's ABI (Sean)

v4: https://lore.kernel.org/kvm/[email protected]/
* Rebased on top of latest kvm-x86/next
* Collect Reviewed-by from Tom
* Use "KVM: SVM" instead of "crypto: ccp" (Tom)
* Clear the intercept in sev_es_init_vmcb() (Tom)
* Differentiate between guest and host MSR_IA32_TSC writes (Tom)

Ketan Chaturvedi (1):
KVM: SVM: Enable Secure TSC for SNP guests

Nikunj A Dadhania (3):
x86/cpufeatures: Add SNP Secure TSC
KVM: SVM: Add missing member in SNP_LAUNCH_START command structure
KVM: SVM: Add GUEST_TSC_FREQ MSR for Secure TSC enabled guests

  arch/x86/include/asm/cpufeatures.h |  1 +
  arch/x86/include/asm/svm.h         |  1 +
  arch/x86/include/uapi/asm/kvm.h    |  3 ++-
  arch/x86/kvm/svm/sev.c             | 17 +++++++++++++++++
  arch/x86/kvm/svm/svm.c             |  1 +
  arch/x86/kvm/svm/svm.h             | 11 ++++++++++-
  include/linux/psp-sev.h            |  2 ++
  7 files changed, 34 insertions(+), 2 deletions(-)

base-commit: 9f443c33263385cbb8565ab58db3f7983e769bed

#8 lore raw
Quoted 39 lines · Vaishali Thakkar
Quoted 27 lines · Nikunj A Dadhania

The hypervisor controls TSC value calculations for the guest. A malicious hypervisor can prevent the guest from progressing. The Secure TSC feature for
SEV-SNP allows guests to securely use the RDTSC and RDTSCP instructions. This
ensures the guest has a consistent view of time and prevents a malicious hypervisor from manipulating time, such as making it appear to move backward or
advance too quickly. For more details, refer to the "Secure Nested Paging (SEV-SNP)" section, subsection "Secure TSC" in APM Volume 2.

This patch set is also available at:

   https://github.com/AMDESE/linux-kvm/tree/sectsc-host-latest

and is based on kvm/queue

Testing Secure TSC
-----------------

Secure TSC guest patches are available as part of v6.14-rc1.

QEMU changes:
https://github.com/nikunjad/qemu/tree/snp-securetsc-latest

QEMU command line SEV-SNP with Secure TSC:

Quoted 8 lines

   qemu-system-x86_64 -cpu EPYC-Milan-v2 -smp 4 \
     -object memory-backend-memfd,id=ram1,size=1G,share=true,prealloc=false,reserve=false \
     -object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,secure-tsc=on,stsc-freq=2000000000 \
     -machine q35,confidential-guest-support=sev0,memory-backend=ram1 \
     ...

Hi Nikunj,

Quoted 7 lines

I've been trying to test this patchset with the above QEMU command line and with the OVMF built from upstream master. But I'm encountering following errors:

" !!!!!!!!  Image Section Alignment(0x40) does not match Required Alignment (0x1000)  !!!!!!!!
ProtectUefiImage failed to create image properties record "

I bisected EDK2/OVMF and found that the above messages started appearing with commit 37f63deeefa8 ("MdeModulePkg: MemoryProtection: Use ImageRecordPropertiesLib")

It doesn't appear to cause any issues while booting as I'm able to progress to the grub menu and boot the OS. Is it failing for you?

Thanks,
Tom

Quoted 55 lines

I briefly looked at this[1] branch as well but it appears to be no longer actively maintained as I ran into some build errors which are fixed in upstream.

The build command I'm using to build the OVMF is as follows:
build -a X64 -b DEBUG -t GCC5 -D DEBUG_VERBOSE -p OvmfPkg/OvmfPkgX64.dsc

So, I was wondering if you've some extra patches on top of upstream OVMF to test SecureTSC or are there any modifications required in my build command?

Thank you!

[1] https://github.com/AMDESE/ovmf/tree/snp-latest

Quoted 37 lines

Changelog:
----------
v5:
* Rebased on top of kvm/queue that includes protected TSC patches
https://lore.kernel.org/kvm/[email protected]/
* Dropped patch 4/5 as it is not required after protected TSC patches
* Set guest_tsc_protected when Secure TSC is enabled (Paolo)
* Collect Reviewed-by from Tom
* Base the desired_tsc_freq on KVM's ABI (Sean)

v4: https://lore.kernel.org/kvm/[email protected]/
* Rebased on top of latest kvm-x86/next
* Collect Reviewed-by from Tom
* Use "KVM: SVM" instead of "crypto: ccp" (Tom)
* Clear the intercept in sev_es_init_vmcb() (Tom)
* Differentiate between guest and host MSR_IA32_TSC writes (Tom)

Ketan Chaturvedi (1):
   KVM: SVM: Enable Secure TSC for SNP guests

Nikunj A Dadhania (3):
   x86/cpufeatures: Add SNP Secure TSC
   KVM: SVM: Add missing member in SNP_LAUNCH_START command structure
   KVM: SVM: Add GUEST_TSC_FREQ MSR for Secure TSC enabled guests

  arch/x86/include/asm/cpufeatures.h |  1 +
  arch/x86/include/asm/svm.h         |  1 +
  arch/x86/include/uapi/asm/kvm.h    |  3 ++-

  arch/x86/kvm/svm/sev.c             | 17 +++++++++++++++++
  arch/x86/kvm/svm/svm.c             |  1 +
  arch/x86/kvm/svm/svm.h             | 11 ++++++++++-
  include/linux/psp-sev.h            |  2 ++
  7 files changed, 34 insertions(+), 2 deletions(-)

base-commit: 9f443c33263385cbb8565ab58db3f7983e769bed

#9 lore raw

Hi Vaishali,

Thanks for testing the patches.

Quoted 18 lines · Tom Lendacky
Quoted 5 lines · Vaishali Thakkar
Quoted 1 line · Nikunj A Dadhania

Hi Nikunj,

Quoted 7 lines

I've been trying to test this patchset with the above QEMU command line and with the OVMF built from upstream master. But I'm encountering following errors:

" !!!!!!!!  Image Section Alignment(0x40) does not match Required Alignment (0x1000)  !!!!!!!!
ProtectUefiImage failed to create image properties record "

I bisected EDK2/OVMF and found that the above messages started appearing with commit 37f63deeefa8 ("MdeModulePkg: MemoryProtection: Use ImageRecordPropertiesLib")

Quoted 17 lines

It doesn't appear to cause any issues while booting as I'm able to progress to the grub menu and boot the OS. Is it failing for you?

Thanks,
Tom

Quoted 11 lines

I briefly looked at this[1] branch as well but it appears to be no longer actively maintained as I ran into some build errors which are fixed in upstream.

The build command I'm using to build the OVMF is as follows:
build -a X64 -b DEBUG -t GCC5 -D DEBUG_VERBOSE -p OvmfPkg/OvmfPkgX64.dsc

So, I was wondering if you've some extra patches on top of upstream OVMF to test SecureTSC or are there any modifications required in my build command?

No, I do not have any Secure TSC related modification in OVMF. If it can boot SNP, that should work for Secure TSC guests as well.

Regards
Nikunj

Quoted 6 lines
Quoted 6 lines

Thank you!

[1] https://github.com/AMDESE/ovmf/tree/snp-latest

#10 lore raw
Quoted 37 lines · Nikunj A. Dadhania

Hi Vaishali,

Thanks for testing the patches.

Quoted 32 lines · Tom Lendacky
Quoted 5 lines · Vaishali Thakkar
Quoted 1 line · Nikunj A Dadhania

Hi Nikunj,

Quoted 7 lines

I've been trying to test this patchset with the above QEMU command line and with the OVMF built from upstream master. But I'm encountering following errors:

" !!!!!!!!  Image Section Alignment(0x40) does not match Required Alignment (0x1000)  !!!!!!!!
ProtectUefiImage failed to create image properties record "

I bisected EDK2/OVMF and found that the above messages started appearing with commit 37f63deeefa8 ("MdeModulePkg: MemoryProtection: Use ImageRecordPropertiesLib")

It doesn't appear to cause any issues while booting as I'm able to progress to the grub menu and boot the OS. Is it failing for you?

Thanks,
Tom

Quoted 8 lines

I briefly looked at this[1] branch as well but it appears to be no longer actively maintained as I ran into some build errors which are fixed in upstream.

The build command I'm using to build the OVMF is as follows:
build -a X64 -b DEBUG -t GCC5 -D DEBUG_VERBOSE -p OvmfPkg/OvmfPkgX64.dsc

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

So, I was wondering if you've some extra patches on top of upstream OVMF to test SecureTSC or are there any modifications required in my build command?

No, I do not have any Secure TSC related modification in OVMF. If it can boot SNP, that should work for Secure TSC guests as well.

Thanks both for confirming that those OVMF messages were a red herring. It helped me uncover an issue unrelated to this patchset. I have been able to test this patchset successfully now. For the whole patchset, please feel free to add:

Quoted 9 lines

Regards
Nikunj

Quoted 6 lines
Quoted 6 lines

Thank you!

[1] https://github.com/AMDESE/ovmf/tree/snp-latest