kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Nikunj A Dadhania <nikunj@amd.com>
Cc: pbonzini@redhat.com, kvm@vger.kernel.org,
	thomas.lendacky@amd.com,  santosh.shukla@amd.com, bp@alien8.de,
	isaku.yamahata@intel.com,  vaishali.thakkar@suse.com,
	kai.huang@intel.com
Subject: Re: [PATCH v10 2/2] KVM: SVM: Enable Secure TSC for SNP guests
Date: Tue, 19 Aug 2025 11:31:32 -0700	[thread overview]
Message-ID: <aKTDBMCPxOXQhzDq@google.com> (raw)
In-Reply-To: <20250804103751.7760-3-nikunj@amd.com>

On Mon, Aug 04, 2025, Nikunj A Dadhania wrote:
> 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.
> 
> Always use kvm->arch.arch.default_tsc_khz as the TSC frequency that is
> passed to SNP guests in the SNP_LAUNCH_START command.  The default value
> is the host TSC frequency.  The userspace can optionally change the TSC
> frequency via the KVM_SET_TSC_KHZ ioctl before calling the
> SNP_LAUNCH_START ioctl.
> 
> 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.
> 
> Co-developed-by: Ketan Chaturvedi <Ketan.Chaturvedi@amd.com>
> Signed-off-by: Ketan Chaturvedi <Ketan.Chaturvedi@amd.com>
> Reviewed-by: Kai Huang <kai.huang@intel.com>
> Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
> Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
> ---
>  arch/x86/include/asm/svm.h |  1 +
>  arch/x86/kvm/svm/sev.c     | 27 +++++++++++++++++++++++++++
>  arch/x86/kvm/svm/svm.c     |  2 ++
>  arch/x86/kvm/svm/svm.h     |  2 ++
>  4 files changed, 32 insertions(+)
> 
> diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h
> index ffc27f676243..17f6c3fedeee 100644
> --- a/arch/x86/include/asm/svm.h
> +++ b/arch/x86/include/asm/svm.h
> @@ -299,6 +299,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 VMCB_ALLOWED_SEV_FEATURES_VALID			BIT_ULL(63)
>  
> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
> index e88dce598785..f9ab9ecc213f 100644
> --- a/arch/x86/kvm/svm/sev.c
> +++ b/arch/x86/kvm/svm/sev.c
> @@ -146,6 +146,14 @@ static bool sev_vcpu_has_debug_swap(struct vcpu_svm *svm)
>  	return sev->vmsa_features & SVM_SEV_FEAT_DEBUG_SWAP;
>  }
>  
> +bool snp_secure_tsc_enabled(struct kvm *kvm)

snp_is_secure_tsc_enabled() to make it super obvious this is a predicate.

> +{
> +	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));

Align indentation.

> +}
> @@ -4455,6 +4479,9 @@ void sev_es_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
>  					  !guest_cpu_cap_has(vcpu, X86_FEATURE_RDTSCP) &&
>  					  !guest_cpu_cap_has(vcpu, X86_FEATURE_RDPID));
>  
> +	if (snp_secure_tsc_enabled(vcpu->kvm))
> +		svm_disable_intercept_for_msr(vcpu, MSR_AMD64_GUEST_TSC_FREQ, MSR_TYPE_R);

I'm leaning towards:

	svm_set_intercept_for_msr(vcpu, MSR_AMD64_GUEST_TSC_FREQ, MSR_TYPE_R,
				  !snp_is_secure_tsc_enabled(vcpu->kvm));

because the cost of setting a bit is negligible.

> +
>  	/*
>  	 * For SEV-ES, accesses to MSR_IA32_XSS should not be intercepted if
>  	 * the host/guest supports its use.
> diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> index d9931c6c4bc6..a81bf83ccb52 100644
> --- a/arch/x86/kvm/svm/svm.c
> +++ b/arch/x86/kvm/svm/svm.c
> @@ -1317,6 +1317,8 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
>  
>  	svm->guest_state_loaded = false;
>  
> +	vcpu->arch.guest_tsc_protected = snp_secure_tsc_enabled(vcpu->kvm);

Hmm, we can and should handle this in sev.c.  If we add sev_vcpu_create(), then
we don't need to expose snp_is_secure_tsc_enabled(), and we can move more code
into that helper.

I'll post a combined series of this and the GHCB version patches.

  reply	other threads:[~2025-08-19 18:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-04 10:37 [PATCH v10 0/2] Enable Secure TSC for SEV-SNP Nikunj A Dadhania
2025-08-04 10:37 ` [PATCH v10 1/2] x86/cpufeatures: Add SNP Secure TSC Nikunj A Dadhania
2025-08-04 10:37 ` [PATCH v10 2/2] KVM: SVM: Enable Secure TSC for SNP guests Nikunj A Dadhania
2025-08-19 18:31   ` Sean Christopherson [this message]
2025-08-20  5:31     ` Nikunj A. Dadhania
2025-08-19  3:47 ` [PATCH v10 0/2] Enable Secure TSC for SEV-SNP Nikunj A. Dadhania

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aKTDBMCPxOXQhzDq@google.com \
    --to=seanjc@google.com \
    --cc=bp@alien8.de \
    --cc=isaku.yamahata@intel.com \
    --cc=kai.huang@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=nikunj@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=santosh.shukla@amd.com \
    --cc=thomas.lendacky@amd.com \
    --cc=vaishali.thakkar@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).