[PATCH v9 11/22] KVM: x86: Add a helper to detect if FRED is enabled for a vCPU

Xin Li (Intel) posted 22 patches 3 months, 2 weeks ago
[PATCH v9 11/22] KVM: x86: Add a helper to detect if FRED is enabled for a vCPU
Posted by Xin Li (Intel) 3 months, 2 weeks ago
From: Xin Li <xin3.li@intel.com>

Signed-off-by: Xin Li <xin3.li@intel.com>
[ Sean: removed the "kvm_" prefix from the function name ]
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Xin Li (Intel) <xin@zytor.com>
Tested-by: Shan Kang <shan.kang@intel.com>
Tested-by: Xuelian Guo <xuelian.guo@intel.com>
---

Change in v5:
* Add TB from Xuelian Guo.
---
 arch/x86/kvm/kvm_cache_regs.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/arch/x86/kvm/kvm_cache_regs.h b/arch/x86/kvm/kvm_cache_regs.h
index 8ddb01191d6f..3c8dbb77d7d4 100644
--- a/arch/x86/kvm/kvm_cache_regs.h
+++ b/arch/x86/kvm/kvm_cache_regs.h
@@ -205,6 +205,21 @@ static __always_inline bool kvm_is_cr4_bit_set(struct kvm_vcpu *vcpu,
 	return !!kvm_read_cr4_bits(vcpu, cr4_bit);
 }
 
+/*
+ * It's enough to check just CR4.FRED (X86_CR4_FRED) to tell if
+ * a vCPU is running with FRED enabled, because:
+ * 1) CR4.FRED can be set to 1 only _after_ IA32_EFER.LMA = 1.
+ * 2) To leave IA-32e mode, CR4.FRED must be cleared first.
+ */
+static inline bool is_fred_enabled(struct kvm_vcpu *vcpu)
+{
+#ifdef CONFIG_X86_64
+	return kvm_is_cr4_bit_set(vcpu, X86_CR4_FRED);
+#else
+	return false;
+#endif
+}
+
 static inline ulong kvm_read_cr3(struct kvm_vcpu *vcpu)
 {
 	if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3))
-- 
2.51.0
Re: [PATCH v9 11/22] KVM: x86: Add a helper to detect if FRED is enabled for a vCPU
Posted by Binbin Wu 2 weeks, 5 days ago

On 10/27/2025 4:18 AM, Xin Li (Intel) wrote:
> From: Xin Li <xin3.li@intel.com>
> 

Not sure if it's OK with empty change log even though the patch is simple and
the title has already described it.

> Signed-off-by: Xin Li <xin3.li@intel.com>
> [ Sean: removed the "kvm_" prefix from the function name ]
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> Signed-off-by: Xin Li (Intel) <xin@zytor.com>
> Tested-by: Shan Kang <shan.kang@intel.com>
> Tested-by: Xuelian Guo <xuelian.guo@intel.com>
> ---
> 
> Change in v5:
> * Add TB from Xuelian Guo.
> ---
>  arch/x86/kvm/kvm_cache_regs.h | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
> 
> diff --git a/arch/x86/kvm/kvm_cache_regs.h b/arch/x86/kvm/kvm_cache_regs.h
> index 8ddb01191d6f..3c8dbb77d7d4 100644
> --- a/arch/x86/kvm/kvm_cache_regs.h
> +++ b/arch/x86/kvm/kvm_cache_regs.h
> @@ -205,6 +205,21 @@ static __always_inline bool kvm_is_cr4_bit_set(struct kvm_vcpu *vcpu,
>  	return !!kvm_read_cr4_bits(vcpu, cr4_bit);
>  }
>  
> +/*
> + * It's enough to check just CR4.FRED (X86_CR4_FRED) to tell if
> + * a vCPU is running with FRED enabled, because:
> + * 1) CR4.FRED can be set to 1 only _after_ IA32_EFER.LMA = 1.
> + * 2) To leave IA-32e mode, CR4.FRED must be cleared first.
> + */
> +static inline bool is_fred_enabled(struct kvm_vcpu *vcpu)
> +{
> +#ifdef CONFIG_X86_64
> +	return kvm_is_cr4_bit_set(vcpu, X86_CR4_FRED);
> +#else
> +	return false;
> +#endif
> +}
> +
>  static inline ulong kvm_read_cr3(struct kvm_vcpu *vcpu)
>  {
>  	if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3))
Re: [PATCH v9 11/22] KVM: x86: Add a helper to detect if FRED is enabled for a vCPU
Posted by Xin Li 2 weeks, 5 days ago
> On Jan 21, 2026, at 12:05 AM, Binbin Wu <binbin.wu@linux.intel.com> wrote:
> 
> 
> Not sure if it's OK with empty change log even though the patch is simple and
> the title has already described it.

IIRC, Sean changed it this way ;)
Re: [PATCH v9 11/22] KVM: x86: Add a helper to detect if FRED is enabled for a vCPU
Posted by Sean Christopherson 2 weeks, 5 days ago
On Wed, Jan 21, 2026, Xin Li wrote:
> 
> > On Jan 21, 2026, at 12:05 AM, Binbin Wu <binbin.wu@linux.intel.com> wrote:
> > 
> > 
> > Not sure if it's OK with empty change log even though the patch is simple and
> > the title has already described it.
> 
> IIRC, Sean changed it this way ;)

I doubt that.  Ha!  Found it.  From: https://lore.kernel.org/all/ZmszIOsGtNsvqbpI@google.com
as an attachment:

[-- Attachment #4: 0013-KVM-x86-Add-a-helper-to-detect-if-FRED-is-enabled-fo.patch --]
[-- Type: text/x-diff, Size: 1418 bytes --]

From f38dcc04e334cda572289f05f4be7702bebfc96a Mon Sep 17 00:00:00 2001
From: Xin Li <xin3.li@intel.com>
Date: Wed, 7 Feb 2024 09:26:31 -0800
Subject: [PATCH 13/28] KVM: x86: Add a helper to detect if FRED is enabled for
 a vCPU

Add is_fred_enabled() to detect if FRED is enabled on a vCPU.

Signed-off-by: Xin Li <xin3.li@intel.com>
Tested-by: Shan Kang <shan.kang@intel.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/kvm_cache_regs.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/arch/x86/kvm/kvm_cache_regs.h b/arch/x86/kvm/kvm_cache_regs.h
index 75eae9c4998a..fe5546efd388 100644
--- a/arch/x86/kvm/kvm_cache_regs.h
+++ b/arch/x86/kvm/kvm_cache_regs.h
@@ -187,6 +187,21 @@ static __always_inline bool kvm_is_cr4_bit_set(struct kvm_vcpu *vcpu,
 	return !!kvm_read_cr4_bits(vcpu, cr4_bit);
 }
 
+/*
+ * It's enough to check just CR4.FRED (X86_CR4_FRED) to tell if
+ * a vCPU is running with FRED enabled, because:
+ * 1) CR4.FRED can be set to 1 only _after_ IA32_EFER.LMA = 1.
+ * 2) To leave IA-32e mode, CR4.FRED must be cleared first.
+ */
+static inline bool is_fred_enabled(struct kvm_vcpu *vcpu)
+{
+#ifdef CONFIG_X86_64
+	return kvm_is_cr4_bit_set(vcpu, X86_CR4_FRED);
+#else
+	return false;
+#endif
+}
+
 static inline ulong kvm_read_cr3(struct kvm_vcpu *vcpu)
 {
 	if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3))
-- 
2.45.2.627.g7a2c4fd464-goog
Re: [PATCH v9 11/22] KVM: x86: Add a helper to detect if FRED is enabled for a vCPU
Posted by Xin Li 2 weeks, 4 days ago
> On Jan 21, 2026, at 12:24 PM, Sean Christopherson <seanjc@google.com> wrote:
> 
> Subject: [PATCH 13/28] KVM: x86: Add a helper to detect if FRED is enabled for
> a vCPU
> 
> Add is_fred_enabled() to detect if FRED is enabled on a vCPU.

Yeah, you won ;)

> 
> Signed-off-by: Xin Li <xin3.li@intel.com>
> Tested-by: Shan Kang <shan.kang@intel.com>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
Re: [PATCH v9 11/22] KVM: x86: Add a helper to detect if FRED is enabled for a vCPU
Posted by Chao Gao 2 months, 4 weeks ago
On Sun, Oct 26, 2025 at 01:18:59PM -0700, Xin Li (Intel) wrote:
>From: Xin Li <xin3.li@intel.com>
>
>Signed-off-by: Xin Li <xin3.li@intel.com>
>[ Sean: removed the "kvm_" prefix from the function name ]
>Signed-off-by: Sean Christopherson <seanjc@google.com>
>Signed-off-by: Xin Li (Intel) <xin@zytor.com>
>Tested-by: Shan Kang <shan.kang@intel.com>
>Tested-by: Xuelian Guo <xuelian.guo@intel.com>

Reviewed-by: Chao Gao <chao.gao@intel.com>