Skip to contentSkip to conversation

Documentation: KVM: Describe guest CPUID.15H settings for in-kernel APIC

PATCH In review Jim Mattson 2 participants 2 messages Sep 21, 2022 – Sep 27, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Documentation: KVM: Describe guest CPUID.15H settings for in-kernel APIC

KVM_GET_SUPPORTED_CPUID must not populate guest CPUID.15H, because KVM has no way of knowing the base frequency of the local APIC emulated in userspace.

However, in reality, the in-kernel APIC emulation is in prevalent use. Document how KVM_GET_SUPPORTED_CPUID would populate CPUID.15H if the in-kernel APIC were the default.

---
 Documentation/virt/kvm/api.rst | 10 ++++++++++
 1 file changed, 10 insertions(+)
Documentation/virt/kvm/api.rst +10 -0
index abd7c32126ce..1e09ac9d48e9 100644
@@ -1786,6 +1786,16 @@ support.  Instead it is reported via::
 if that returns true and you use KVM_CREATE_IRQCHIP, or if you emulate the
 feature in userspace, then you can enable the feature for KVM_SET_CPUID2.
 
+Similarly, CPUID leaf 0x15 always returns zeroes, because the core
+crystal clock frequency must match the local APIC base frequency, and
+the default configuration leaves the local APIC emulation to
+userspace.
+
+If KVM_CREATE_IRQCHIP is used to enable the in-kernel local APIC
+emulation, CPUID.15H:ECX can be set to 1000000000 (0x3b9aca00). For
+the default guest TSC frequency, CPUID.15H:EBX can be set to tsc_khz
+and CPUID.15H:ECX can be set to 1000000 (0xf4240).  The fraction can
+be simplified if desired.
 
 4.47 KVM_PPC_GET_PVINFO
 -----------------------
Signature · 1 line
2.37.3.968.ga6b4b080e4-goog
#1 lore raw
Quoted 21 lines · Jim Mattson

KVM_GET_SUPPORTED_CPUID must not populate guest CPUID.15H, because KVM has no way of knowing the base frequency of the local APIC emulated in userspace.

However, in reality, the in-kernel APIC emulation is in prevalent use. Document how KVM_GET_SUPPORTED_CPUID would populate CPUID.15H if the in-kernel APIC were the default.

---
 Documentation/virt/kvm/api.rst | 10 ++++++++++
 1 file changed, 10 insertions(+)
Documentation/virt/kvm/api.rst +0 -0
index abd7c32126ce..1e09ac9d48e9 100644
@@ -1786,6 +1786,16 @@ support.  Instead it is reported via::
 if that returns true and you use KVM_CREATE_IRQCHIP, or if you emulate the
 feature in userspace, then you can enable the feature for KVM_SET_CPUID2.
 

Documentation/virt/kvm/api.rst support. Instead it is reported via:: · line 1798

Quoted 10 lines
Documentation/virt/kvm/api.rst +10 -0
+Similarly, CPUID leaf 0x15 always returns zeroes, because the core
+crystal clock frequency must match the local APIC base frequency, and
+the default configuration leaves the local APIC emulation to
+userspace.
+
+If KVM_CREATE_IRQCHIP is used to enable the in-kernel local APIC
+emulation, CPUID.15H:ECX can be set to 1000000000 (0x3b9aca00). For
+the default guest TSC frequency, CPUID.15H:EBX can be set to tsc_khz
+and CPUID.15H:ECX can be set to 1000000 (0xf4240).  The fraction can
+be simplified if desired.

It would be helpful to explain what these numbers mean and where they come from.

That said, unlike 16H, I think we can actually "solve" this case, we just need a way to force an in-kernel local APIC, i.e. add a module param or Kconfig.

I'm tempted to make it a Kconfig so that KVM can start moving toward removing "support" for a userspace local APIC entirely, e.g. x2APIC is basically unusable without an in-kernel APIC, so KVM is already quite far down that path anyways.

E.g.

arch/x86/kvm/cpuid.c +5 -0
index ffdc28684cb7..b67135a53edf 100644
@@ -1061,6 +1061,11 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
                                goto out;
                }
                break;
+#ifdef CONFIG_KVM_REQUIRE_IN_KERNEL_LOCAL_APIC
+       case 0x15:
+               <fill in magic values>
+               break;
+#endif
        /* Intel AMX TILE */
        case 0x1d:
                if (!kvm_cpu_cap_has(X86_FEATURE_AMX_TILE)) {
arch/x86/kvm/lapic.h +2 -0
index a5ac4a5a5179..5abaa09ec5aa 100644
@@ -181,8 +181,10 @@ DECLARE_STATIC_KEY_FALSE(kvm_has_noapic_vcpu);
 
 static inline bool lapic_in_kernel(struct kvm_vcpu *vcpu)
 {
+#ifndef CONFIG_KVM_REQUIRE_IN_KERNEL_LOCAL_APIC
        if (static_branch_unlikely(&kvm_has_noapic_vcpu))
                return vcpu->arch.apic;
+#endif
        return true;
 }
 
arch/x86/kvm/x86.c +4 -0
index eb412f9f03ea..8ea6f0175c1c 100644
@@ -11763,6 +11763,10 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
        struct page *page;
        int r;
 
+       if (IS_ENABLED(CONFIG_KVM_REQUIRE_IN_KERNEL_LOCAL_APIC) &&
+           !irqchip_in_kernel(vcpu->kvm))
+               return -EINVAL;
+
        vcpu->arch.last_vmentry_cpu = -1;
        vcpu->arch.regs_avail = ~0;
        vcpu->arch.regs_dirty = ~0;