Re: [PATCH v10 11/28] KVM: x86: Add a helper to detect if FRED is enabled for a vCPU
From: Binbin Wu
Date: Fri Oct 23 2026 - 05:25:28 EST
On 9/12/2026 5:36 AM, Sohil Mehta wrote:
> From: "Xin Li (Intel)" <xin@xxxxxxxxx>
>
> Add is_fred_enabled() to detect if FRED is enabled on a vCPU.
>
> Signed-off-by: Xin Li (Intel) <xin@xxxxxxxxx>
> [ Sean: removed the "kvm_" prefix from the function name ]
> Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
> Signed-off-by: Sohil Mehta <sohil.mehta@xxxxxxxxx>
> Tested-by: Shan Kang <shan.kang@xxxxxxxxx>
> Tested-by: Xuelian Guo <xuelian.guo@xxxxxxxxx>
> Reviewed-by: Chao Gao <chao.gao@xxxxxxxxx>
> ---
> v10:
> - Add back the change log (Binbin Wu and Sean).
> ---
> arch/x86/kvm/regs.h | 15 +++++++++++++++
> 1 file changed, 15 insertions(+)
>
> diff --git a/arch/x86/kvm/regs.h b/arch/x86/kvm/regs.h
> index 447f0ec3e63e..4e641a6c20f9 100644
> --- a/arch/x86/kvm/regs.h
> +++ b/arch/x86/kvm/regs.h
> @@ -364,6 +364,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.
It seems KVM doesn't enforce it in kvm_set_cr0() for 2).
> + */
> +static inline bool is_fred_enabled(struct kvm_vcpu *vcpu)
> +{
> +#ifdef CONFIG_X86_64
Nit:
This seems to be unnecessary.
X86_CR4_FRED is defined as 0 in non 64-bit build.
> + 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_REG_CR3))