Skip to contentSkip to conversation

Documentation: KVM: add a place to document API quirks and (x86) CPU errata

PATCH In review Paolo Bonzini 3 participants 9 messages Mar 22, 2022 – Mar 24, 2022

Subsystems (3)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

cover Documentation: KVM: add a place to document API quirks and (x86) CPU errata

Reorganize Documentation/virt/kvm to be less x86-centric, and add a new file to document the ways in which x86 emulation is "wrong". There are surely many more, for now just add the skeleton. Please reply to this message with things that you'd like to see documented.

While at it add also a section for places where the KVM API is bad or returns wrong values. Please also include them in reply to this message if you are interested in seeing them documented.

Paolo

Paolo Bonzini (3):
Documentation: KVM: add separate directories for architecture-specific
documentation
Documentation: KVM: add virtual CPU errata documentation
Documentation: KVM: add API issues section

 Documentation/virt/kvm/api.rst                | 46 +++++++++++++++++++
 Documentation/virt/kvm/index.rst              | 28 ++++-------
 Documentation/virt/kvm/s390/index.rst         | 12 +++++
 .../virt/kvm/{ => s390}/s390-diag.rst         |  0
 .../virt/kvm/{ => s390}/s390-pv-boot.rst      |  0
 Documentation/virt/kvm/{ => s390}/s390-pv.rst |  0
 .../kvm/{ => x86}/amd-memory-encryption.rst   |  0
 Documentation/virt/kvm/{ => x86}/cpuid.rst    |  0
 Documentation/virt/kvm/x86/errata.rst         | 39 ++++++++++++++++
 .../virt/kvm/{ => x86}/halt-polling.rst       |  0
 .../virt/kvm/{ => x86}/hypercalls.rst         |  0
 Documentation/virt/kvm/x86/index.rst          | 19 ++++++++
 Documentation/virt/kvm/{ => x86}/mmu.rst      |  0
 Documentation/virt/kvm/{ => x86}/msr.rst      |  0
 .../virt/kvm/{ => x86}/nested-vmx.rst         |  0
 .../kvm/{ => x86}/running-nested-guests.rst   |  0
 .../virt/kvm/{ => x86}/timekeeping.rst        |  0
 17 files changed, 124 insertions(+), 20 deletions(-)
 create mode 100644 Documentation/virt/kvm/s390/index.rst
 rename Documentation/virt/kvm/{ => s390}/s390-diag.rst (100%)
 rename Documentation/virt/kvm/{ => s390}/s390-pv-boot.rst (100%)
 rename Documentation/virt/kvm/{ => s390}/s390-pv.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/amd-memory-encryption.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/cpuid.rst (100%)
 create mode 100644 Documentation/virt/kvm/x86/errata.rst
 rename Documentation/virt/kvm/{ => x86}/halt-polling.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/hypercalls.rst (100%)
 create mode 100644 Documentation/virt/kvm/x86/index.rst
 rename Documentation/virt/kvm/{ => x86}/mmu.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/msr.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/nested-vmx.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/running-nested-guests.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/timekeeping.rst (100%)
Signature · 1 line
2.35.1
author maintainer
#1 lore raw

PATCH 1/3 Documentation: KVM: add separate directories for architecture-specific documentation

ARM already has an arm/ subdirectory, but s390 and x86 do not even though they have a relatively large number of files specific to them. Create new directories in Documentation/virt/kvm for these two architectures as well.

While at it, group the API documentation and the developer documentation in the table of contents.

---
 Documentation/virt/kvm/index.rst              | 28 ++++++-------------
 Documentation/virt/kvm/s390/index.rst         | 12 ++++++++
 .../virt/kvm/{ => s390}/s390-diag.rst         |  0
 .../virt/kvm/{ => s390}/s390-pv-boot.rst      |  0
 Documentation/virt/kvm/{ => s390}/s390-pv.rst |  0
 .../kvm/{ => x86}/amd-memory-encryption.rst   |  0
 Documentation/virt/kvm/{ => x86}/cpuid.rst    |  0
 .../virt/kvm/{ => x86}/halt-polling.rst       |  0
 .../virt/kvm/{ => x86}/hypercalls.rst         |  0
 Documentation/virt/kvm/x86/index.rst          | 18 ++++++++++++
 Documentation/virt/kvm/{ => x86}/mmu.rst      |  0
 Documentation/virt/kvm/{ => x86}/msr.rst      |  0
 .../virt/kvm/{ => x86}/nested-vmx.rst         |  0
 .../kvm/{ => x86}/running-nested-guests.rst   |  0
 .../virt/kvm/{ => x86}/timekeeping.rst        |  0
 15 files changed, 38 insertions(+), 20 deletions(-)
 create mode 100644 Documentation/virt/kvm/s390/index.rst
 rename Documentation/virt/kvm/{ => s390}/s390-diag.rst (100%)
 rename Documentation/virt/kvm/{ => s390}/s390-pv-boot.rst (100%)
 rename Documentation/virt/kvm/{ => s390}/s390-pv.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/amd-memory-encryption.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/cpuid.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/halt-polling.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/hypercalls.rst (100%)
 create mode 100644 Documentation/virt/kvm/x86/index.rst
 rename Documentation/virt/kvm/{ => x86}/mmu.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/msr.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/nested-vmx.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/running-nested-guests.rst (100%)
 rename Documentation/virt/kvm/{ => x86}/timekeeping.rst (100%)
Documentation/virt/kvm/index.rst +8 -20
index b6833c7bb474..e0a2c74e1043 100644
@@ -8,25 +8,13 @@ KVM
    :maxdepth: 2
 
    api
-   amd-memory-encryption
-   cpuid
-   halt-polling
-   hypercalls
-   locking
-   mmu
-   msr
-   nested-vmx
-   ppc-pv
-   s390-diag
-   s390-pv
-   s390-pv-boot
-   timekeeping
-   vcpu-requests
-
-   review-checklist
-
-   arm/index
-
    devices/index
 
-   running-nested-guests
+   arm/index
+   s390/index
+   ppc-pv
+   x86/index
+
+   locking
+   vcpu-requests
+   review-checklist
Documentation/virt/kvm/s390/index.rst new file +12 -0
new file mode 100644
index 000000000000..605f488f0cc5
@@ -0,0 +1,12 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+====================
+KVM for s390 systems
+====================
+
+.. toctree::
+   :maxdepth: 2
+
+   s390-diag
+   s390-pv
+   s390-pv-boot
Documentation/virt/kvm/s390/s390-diag.rst renamed from Documentation/virt/kvm/s390-diag.rst +0 -0
similarity index 100%
rename from Documentation/virt/kvm/s390-diag.rst
rename to Documentation/virt/kvm/s390/s390-diag.rst
Documentation/virt/kvm/s390/s390-pv-boot.rst renamed from Documentation/virt/kvm/s390-pv-boot.rst +0 -0
similarity index 100%
rename from Documentation/virt/kvm/s390-pv-boot.rst
rename to Documentation/virt/kvm/s390/s390-pv-boot.rst
Documentation/virt/kvm/s390/s390-pv.rst renamed from Documentation/virt/kvm/s390-pv.rst +0 -0
similarity index 100%
rename from Documentation/virt/kvm/s390-pv.rst
rename to Documentation/virt/kvm/s390/s390-pv.rst
Documentation/virt/kvm/x86/amd-memory-encryption.rst renamed from Documentation/virt/kvm/amd-memory-encryption.rst +0 -0
similarity index 100%
rename from Documentation/virt/kvm/amd-memory-encryption.rst
rename to Documentation/virt/kvm/x86/amd-memory-encryption.rst
Documentation/virt/kvm/x86/cpuid.rst renamed from Documentation/virt/kvm/cpuid.rst +0 -0
similarity index 100%
rename from Documentation/virt/kvm/cpuid.rst
rename to Documentation/virt/kvm/x86/cpuid.rst
Documentation/virt/kvm/x86/halt-polling.rst renamed from Documentation/virt/kvm/halt-polling.rst +0 -0
similarity index 100%
rename from Documentation/virt/kvm/halt-polling.rst
rename to Documentation/virt/kvm/x86/halt-polling.rst
Documentation/virt/kvm/x86/hypercalls.rst renamed from Documentation/virt/kvm/hypercalls.rst +0 -0
similarity index 100%
rename from Documentation/virt/kvm/hypercalls.rst
rename to Documentation/virt/kvm/x86/hypercalls.rst
Documentation/virt/kvm/x86/index.rst new file +18 -0
new file mode 100644
index 000000000000..55ede8e070b6
@@ -0,0 +1,18 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+===================
+KVM for x86 systems
+===================
+
+.. toctree::
+   :maxdepth: 2
+
+   amd-memory-encryption
+   cpuid
+   halt-polling
+   hypercalls
+   mmu
+   msr
+   nested-vmx
+   running-nested-guests
+   timekeeping
Documentation/virt/kvm/x86/mmu.rst renamed from Documentation/virt/kvm/mmu.rst +0 -0
similarity index 100%
rename from Documentation/virt/kvm/mmu.rst
rename to Documentation/virt/kvm/x86/mmu.rst
Documentation/virt/kvm/x86/msr.rst renamed from Documentation/virt/kvm/msr.rst +0 -0
similarity index 100%
rename from Documentation/virt/kvm/msr.rst
rename to Documentation/virt/kvm/x86/msr.rst
Documentation/virt/kvm/x86/nested-vmx.rst renamed from Documentation/virt/kvm/nested-vmx.rst +0 -0
similarity index 100%
rename from Documentation/virt/kvm/nested-vmx.rst
rename to Documentation/virt/kvm/x86/nested-vmx.rst
Documentation/virt/kvm/x86/running-nested-guests.rst renamed from Documentation/virt/kvm/running-nested-guests.rst +0 -0
similarity index 100%
rename from Documentation/virt/kvm/running-nested-guests.rst
rename to Documentation/virt/kvm/x86/running-nested-guests.rst
Documentation/virt/kvm/x86/timekeeping.rst renamed from Documentation/virt/kvm/timekeeping.rst +0 -0
similarity index 100%
rename from Documentation/virt/kvm/timekeeping.rst
rename to Documentation/virt/kvm/x86/timekeeping.rst
Signature · 1 line
2.35.1
author maintainer
#2 lore raw

PATCH 2/3 Documentation: KVM: add virtual CPU errata documentation

In reply to Paolo Bonzini

Add a file to document all the different ways in which the virtual CPU emulation is imperfect. Include an example to show how to document such errata.

---
 Documentation/virt/kvm/x86/errata.rst | 39 +++++++++++++++++++++++++++
 Documentation/virt/kvm/x86/index.rst  |  1 +
 2 files changed, 40 insertions(+)
 create mode 100644 Documentation/virt/kvm/x86/errata.rst
Documentation/virt/kvm/x86/errata.rst new file +39 -0
new file mode 100644
index 000000000000..df394d34a836
@@ -0,0 +1,39 @@
+
+=======================================
+Known limitations of CPU virtualization
+=======================================
+
+Whenever perfect emulation of a CPU feature is impossible or too hard, KVM
+has to choose between not implementing the feature at all or introducing
+behavioral differences between virtual machines and bare metal systems.
+
+This file documents some of the known limitations that KVM has in
+virtualizing CPU features.
+
+x86
+===
+
+``KVM_GET_SUPPORTED_CPUID`` issues
+----------------------------------
+
+x87 features
+~~~~~~~~~~~~
+
+Unlike most other CPUID feature bits, CPUID[EAX=7,ECX=0]:EBX[6]
+(FDP_EXCPTN_ONLY) and CPUID[EAX=7,ECX=0]:EBX]13] (ZERO_FCS_FDS) are
+clear if the features are present and set if the features are not present.
+
+Clearing these bits in CPUID has no effect on the operation of the guest;
+if these bits are set on hardware, the features will not be present on
+any virtual machine that runs on that hardware.
+
+**Workaround:** It is recommended to always set these bits in guest CPUID.
+Note however that any software (e.g ``WIN87EM.DLL``) expecting these features
+to be present likely predates these CPUID feature bits, and therefore
+doesn't know to check for them anyway.
+
+Nested virtualization features
+------------------------------
+
+TBD
+
Documentation/virt/kvm/x86/index.rst +1 -0
index 55ede8e070b6..7ff588826b9f 100644
@@ -9,6 +9,7 @@ KVM for x86 systems
 
    amd-memory-encryption
    cpuid
+   errata
    halt-polling
    hypercalls
    mmu
Signature · 1 line
2.35.1
Reviewed-by on v1 2/3
#3 lore raw

Re: [PATCH 2/3] Documentation: KVM: add virtual CPU errata documentation

Quoted 4 lines · Paolo Bonzini

Add a file to document all the different ways in which the virtual CPU emulation is imperfect. Include an example to show how to document such errata.

This is fantastic. Thank you. Along these lines, I wonder if we should come up with a mechanism a la IA32_ARCH_CAPABILITIES (or AMD's OSVW MSRs) for declaring fixed errata.

For example, today, KVM ignores the guest-only and host-only bits in PerfEvtSel MSRs, but I can't imagine we're going to do that forever.

Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] Documentation: KVM: add virtual CPU errata documentation

In reply to Paolo Bonzini

Hi Paolo,

Quoted 5 lines · Paolo Bonzini

Add a file to document all the different ways in which the virtual CPU emulation is imperfect. Include an example to show how to document such errata.

+100, this is excellent!

Quoted 12 lines
---
 Documentation/virt/kvm/x86/errata.rst | 39 +++++++++++++++++++++++++++
 Documentation/virt/kvm/x86/index.rst  |  1 +
 2 files changed, 40 insertions(+)
 create mode 100644 Documentation/virt/kvm/x86/errata.rst
Documentation/virt/kvm/x86/errata.rst new file +0 -0
new file mode 100644
index 000000000000..df394d34a836

Documentation/virt/kvm/x86/errata.rst line 4

Quoted 4 lines
Documentation/virt/kvm/x86/errata.rst new file +4 -0
@@ -0,0 +1,39 @@
+
+=======================================
+Known limitations of CPU virtualization
+=======================================

In an attempt for us KVM developers to save face, would it make sense to rip the table for KVM_CAP_DISABLE_QUIRKS2 out of api.rst and drop it here?

That way, we can discuss all of the known errata in KVM *and* mention the chicken bits we provide to mitigate :)

--
Thanks,
Oliver

author maintainer
#5 lore raw

PATCH 3/3 Documentation: KVM: add API issues section

In reply to Paolo Bonzini

Add a section to document all the different ways in which the KVM API sucks.

I am sure there are way more, give people a place to vent so that userspace authors are aware.

---
 Documentation/virt/kvm/api.rst | 46 ++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
Documentation/virt/kvm/api.rst +46 -0
index 9f3172376ec3..8787fcd3b23f 100644
@@ -7575,3 +7575,49 @@ The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset
 of the result of KVM_CHECK_EXTENSION.  KVM will forward to userspace
 the hypercalls whose corresponding bit is in the argument, and return
 ENOSYS for the others.
+
+9. Known KVM API problems
+=========================
+
+In some cases, KVM's API has some inconsistencies or common pitfalls
+that userspace need to be aware of.  This section details some of
+these issues.
+
+Most of them are architecture specific, so the section is split by
+architecture.
+
+9.1. x86
+--------
+
+``KVM_GET_SUPPORTED_CPUID`` issues
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+In general, ``KVM_GET_SUPPORTED_CPUID`` is designed so that it is possible
+to take its result and pass it directly to ``KVM_SET_CPUID2``.  This section
+documents some cases in which that requires some care.
+
+Local APIC features
+~~~~~~~~~~~~~~~~~~~
+
+CPU[EAX=1]:ECX[21] (X2APIC) is reported by ``KVM_GET_SUPPORTED_CPUID``,
+but it can only be enabled if ``KVM_CREATE_IRQCHIP`` or
+``KVM_ENABLE_CAP(KVM_CAP_IRQCHIP_SPLIT)`` are used to enable in-kernel emulation of
+the local APIC.
+
+The same is true for the ``KVM_FEATURE_PV_UNHALT`` paravirtualized feature.
+
+CPU[EAX=1]:ECX[24] (TSC_DEADLINE) is not reported by ``KVM_GET_SUPPORTED_CPUID``.
+It can be enabled if ``KVM_CAP_TSC_DEADLINE_TIMER`` is present and the kernel
+has enabled in-kernel emulation of the local APIC.
+
+Obsolete ioctls and capabilities
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+KVM_CAP_DISABLE_QUIRKS does not let userspace know which quirks are actually
+available.  Use ``KVM_CHECK_EXTENSION(KVM_CAP_DISABLE_QUIRKS2)`` instead if
+available.
+
+Ordering of KVM_GET_*/KVM_SET_* ioctls
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+TBD
Signature · 1 line
2.35.1
#6 lore raw

Re: [PATCH 3/3] Documentation: KVM: add API issues section

Hi Paolo,

Quoted 6 lines · Paolo Bonzini

Add a section to document all the different ways in which the KVM API sucks.

I am sure there are way more, give people a place to vent so that userspace authors are aware.

Do you think we should vent about our mistakes inline with the descriptions of the corresponding UAPI? One example that comes to mind is ARM's CNTV_CVAL_EL0/CNTVCT_EL0 mixup, which is mentioned in 4.68 'KVM_SET_ONE_REG'. That, of course, doesn't cover the previously-undocumented bits of UAPI that are problematic :)

If we go that route we likely should have a good format for documenting the ugliness.

--
Thanks,
Oliver

author maintainer
#7 lore raw

Re: [PATCH 3/3] Documentation: KVM: add API issues section

Quoted 5 lines · Oliver Upton

Do you think we should vent about our mistakes inline with the descriptions of the corresponding UAPI? One example that comes to mind is ARM's CNTV_CVAL_EL0/CNTVCT_EL0 mixup, which is mentioned in 4.68 'KVM_SET_ONE_REG'. That, of course, doesn't cover the previously-undocumented bits of UAPI that are problematic:)

It depends. My intention was to use this document more for hidden interdependencies, in this case between KVM_GET_SUPPORTED_CPUID and KVM_CREATE_IRQCHIP, KVM_ENABLE_CAP(KVM_CAP_IRQCHIP_SPLIT), KVM_CAP_TSC_DEADLINE_TIMER.

Paolo

#8 lore raw

Re: [PATCH 3/3] Documentation: KVM: add API issues section

Quoted 11 lines · Paolo Bonzini
Quoted 5 lines · Oliver Upton

Do you think we should vent about our mistakes inline with the descriptions of the corresponding UAPI? One example that comes to mind is ARM's CNTV_CVAL_EL0/CNTVCT_EL0 mixup, which is mentioned in 4.68 'KVM_SET_ONE_REG'. That, of course, doesn't cover the previously-undocumented bits of UAPI that are problematic:)

It depends. My intention was to use this document more for hidden interdependencies, in this case between KVM_GET_SUPPORTED_CPUID and KVM_CREATE_IRQCHIP, KVM_ENABLE_CAP(KVM_CAP_IRQCHIP_SPLIT), KVM_CAP_TSC_DEADLINE_TIMER.

Ah good point. I agree bad cross interactions need to be called out separately.

Thanks!

--
Oliver