On Wed, Feb 07, 2024, Xin Li wrote:
Add kvm_is_fred_enabled() to get if FRED is enabled on a vCPU.
Signed-off-by: Xin Li xin3.li@intel.com Tested-by: Shan Kang shan.kang@intel.com
Change since v1:
- Explain why it is ok to only check CR4.FRED (Chao Gao).
arch/x86/kvm/kvm_cache_regs.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
diff --git a/arch/x86/kvm/kvm_cache_regs.h b/arch/x86/kvm/kvm_cache_regs.h index 75eae9c4998a..1d431c703fdf 100644 --- a/arch/x86/kvm/kvm_cache_regs.h +++ b/arch/x86/kvm/kvm_cache_regs.h @@ -187,6 +187,23 @@ 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:
- CR4.FRED can be set to 1 only _after_ IA32_EFER.LMA = 1.
- To leave IA-32e mode, CR4.FRED must be cleared first.
- More details at FRED Spec 6.0 Section 4.2 Enabling in CR4.
Please don't reference specific sections/tables/fields in comments. They always become stale. And the code+comments always reflect the current state, i.e. don't need to worry about spec revisions and whatnot. If there is a spec change, then there darn well needs to be a way for software to differentiate old vs. new, at which point there will be accompanying code to capture the difference.
Even in changelogs, references specific specs by section number is usually discouraged. Again, it shouldn't matter if its FRED spec 6.0 vs. spec 5.0, because if there is a difference between those two, then the code better be different too.
Instead, for the changelog, if it's really necessary/helpful, reference the section by name and/or keyword, as those are much less likely to become stale.
- */
+static __always_inline bool kvm_is_fred_enabled(struct kvm_vcpu *vcpu)
This doesn't need to be __always_inline, it's not used from a noinstr section. kvm_is_cr4_bit_set() is __always_inline so that @cr4_bit is guaranteed to be a compile-time constant, otherwise the BUILD_BUG_ON() would fail.