[PATCH v3 00/40] KVM: x86: GPR accessors and x86.{c,h} spring cleaning

Sean Christopherson posted 40 patches 1 week, 2 days ago
arch/x86/include/asm/kvm_host.h           |  452 +--
arch/x86/kvm/Makefile                     |    4 +-
arch/x86/kvm/cpuid.c                      |   13 +-
arch/x86/kvm/emulate.c                    |    2 +-
arch/x86/kvm/fpu.h                        |   26 +
arch/x86/kvm/hyperv.c                     |   21 +-
arch/x86/kvm/hyperv.h                     |    7 +-
arch/x86/kvm/ioapic.c                     |    1 +
arch/x86/kvm/ioapic.h                     |   12 +
arch/x86/kvm/irq.c                        |    7 +
arch/x86/kvm/irq.h                        |    6 +
arch/x86/kvm/lapic.c                      |   28 +-
arch/x86/kvm/lapic.h                      |    9 +
arch/x86/kvm/mmu.h                        |   92 +-
arch/x86/kvm/mmu/mmu.c                    |  134 +-
arch/x86/kvm/mmu/mmu_internal.h           |   66 -
arch/x86/kvm/msrs.c                       | 2732 +++++++++++++++
arch/x86/kvm/msrs.h                       |  156 +
arch/x86/kvm/mtrr.c                       |    1 +
arch/x86/kvm/regs.c                       |  875 +++++
arch/x86/kvm/{kvm_cache_regs.h => regs.h} |  258 +-
arch/x86/kvm/smm.c                        |    2 +-
arch/x86/kvm/svm/avic.c                   |    5 +
arch/x86/kvm/svm/nested.c                 |    9 +-
arch/x86/kvm/svm/svm.c                    |   19 +-
arch/x86/kvm/svm/svm.h                    |    2 +-
arch/x86/kvm/vmx/nested.c                 |    9 +-
arch/x86/kvm/vmx/nested.h                 |    2 +-
arch/x86/kvm/vmx/sgx.c                    |    6 +-
arch/x86/kvm/vmx/tdx.c                    |   18 +-
arch/x86/kvm/vmx/vmx.c                    |   14 +-
arch/x86/kvm/vmx/vmx.h                    |    2 +-
arch/x86/kvm/x86.c                        | 3789 +--------------------
arch/x86/kvm/x86.h                        |  480 ++-
arch/x86/kvm/xen.c                        |   39 +-
35 files changed, 4722 insertions(+), 4576 deletions(-)
create mode 100644 arch/x86/kvm/msrs.c
create mode 100644 arch/x86/kvm/msrs.h
create mode 100644 arch/x86/kvm/regs.c
rename arch/x86/kvm/{kvm_cache_regs.h => regs.h} (50%)
[PATCH v3 00/40] KVM: x86: GPR accessors and x86.{c,h} spring cleaning
Posted by Sean Christopherson 1 week, 2 days ago
The first half of this series adds proper, explicit "raw" versions of
kvm_<reg>_{read,write}(), along with "e" versions (for hardcoded 32-bit
accesses), and converts the existing kvm_<reg>_{read,write}() APIs into
mode-aware variants.

At the end of that journey, introduce regs.{c,h} to avoid moving _more_ code
into x86.h, especially since the resulting code split would be super arbitrary.

The second half of the series runs with the regs.{c,h} changes and performs
spring cleaning on x86.{c,h} and asm/kvm_host.h (in case it wasn't already
obvious, I have poor impulse control when it comes to cleaning up code).

I'm most intersted in getting feedback on the file names (regs.{c,h} and
msrs.{c,h}.  I'm quite confident the actual code split is the way to go, and
pulling stuff out of asm/kvm_host.h has been on my wish/todo list for years.

I'll grab these fixes for 7.2 no matter what:

  KVM: x86: Trace hypercall register *after* truncating values for 32-bit
  KVM: VMX: Read 32-bit GPR values for ENCLS instructions outside of 64-bit mode
  KVM: x86/xen: Don't truncate RAX when handling hypercall from protected guest
  KVM: x86/xen: Bug the VM if 32-bit KVM observes a 64-bit mode hypercall

Depending on how people feel about the names and cleanups, I'll either send
the big cleanups as a separate pull request after the initial for-7.2 pull
requests (if there's overwhelming consensus on the names/splits), or wait for
7.3 (if more discussion is needed).

v3:
 - Collect more tags. [David, Binbin, Yosry]
 - Use kvm_run_sync_regs_{from,to}_user() instead of kvm_run_{g,s}et_regs().
   [Kai]
 - Fix a variety of typos. [Binbin]
 - Everything beyond patch 15...

v2:
 - https://lore.kernel.org/all/20260514215355.1648463-2-seanjc@google.com
 - Collect tags. [Yosry, Kai]
 - Fix some truly egregious goofs. [Binbin]
 - Rename kvm_cache_regs.h => regs.h, add regs.c. [Yosry, because he
   complained, not because he actually suggested this :-D ]
 - Drop superfluous casting/masking of e*x() usage. [Kai]

v1: https://lore.kernel.org/all/20260409235622.2052730-1-seanjc@google.com

Sean Christopherson (40):
  KVM: SVM: Truncate INVLPGA address in compatibility mode
  KVM: x86/xen: Bug the VM if 32-bit KVM observes a 64-bit mode
    hypercall
  KVM: x86/xen: Don't truncate RAX when handling hypercall from
    protected guest
  KVM: VMX: Read 32-bit GPR values for ENCLS instructions outside of
    64-bit mode
  KVM: x86: Trace hypercall register *after* truncating values for
    32-bit
  KVM: x86: Rename kvm_cache_regs.h => regs.h
  KVM: x86: Move inlined GPR, CR, and DR helpers from x86.h to regs.h
  KVM: x86: Add mode-aware versions of kvm_<reg>_{read,write}() helpers
  KVM: x86: Drop non-raw kvm_<reg>_write() helpers
  KVM: nSVM: Use kvm_rax_read() now that it's mode-aware
  Revert "KVM: VMX: Read 32-bit GPR values for ENCLS instructions
    outside of 64-bit mode"
  KVM: x86: Harden is_64_bit_hypercall() against bugs on 32-bit kernels
  KVM: x86: Move update_cr8_intercept() to lapic.c
  KVM: x86: Move async #PF helpers to x86.h (as inlines)
  KVM: x86: Move the bulk of register specific code from x86.c to regs.c
  KVM: x86: Move local APIC specific helpers out of asm/kvm_host.h
  KVM: x86: Drop defunct vcpu_tsc_khz() declaration
  KVM: x86: Move kvm_caps and kvm_host_values to asm/kvm_host.h
  KVM: x86: Swap the include order between x86.h and mmu.h
  KVM: x86: Move tdp_enabled from kvm_host.h to mmu.h
  KVM: x86: Move eager_page_split to mmu.{c,h}
  KVM: x86/hyperv: Eliminate an unnecessary include of x86.h in hyperv.h
  KVM: x86: Move kvm_{load,put}_guest_fpu() to fpu.h
  KVM: x86: Extract get/set MSR (list) ioctl logic to helpers
  KVM: x86: Expose several TSC helpers via x86.h for use by MSR code
  KVM: x86: Move the bulk of MSR specific code from x86.c to msrs.{c,h}
  KVM: x86: Move register helper declarations from kvm_host.h => regs.h
  KVM: x86: Move kvm_{g,s}et_segment() to inline helpers in regs.h
  KVM: x86: Remove defunct kvm_load_segment_descriptor() declaration.
  KVM: x86: Move MSR helper declarations from kvm_host.h => msrs.h
  KVM: x86: Move MMU helper declarations from kvm_host.h => mmu.h
  KVM: x86: Move LLDT assembly wrappers into VMX
  KVM: x86: Move kvm_cpu_get_apicid() from kvm_host.h => avic.c
  KVM: x86: Move misc "VALID MASK" defines from kvm_host.h => x86.c
  KVM: x86: Move __kvm_irq_line_state() from kvm_host.h => ioapic.h
  KVM: x86: Move IRQ-related helper declarations from kvm_host.h =>
    irq.h
  KVM: x86: Move kvm_pv_send_ipi() declaration from kvm_host.h =>
    lapic.h
  KVM: x86/mmu: Move kvm_arch_async_page_ready() below
    kvm_tdp_page_fault()
  KVM: x86/mmu: Move kvm_mmu_do_page_fault() from mmu_internal.h =>
    mmu.c
  KVM: x86: Move a pile of stuff from kvm_host.h => x86.h

 arch/x86/include/asm/kvm_host.h           |  452 +--
 arch/x86/kvm/Makefile                     |    4 +-
 arch/x86/kvm/cpuid.c                      |   13 +-
 arch/x86/kvm/emulate.c                    |    2 +-
 arch/x86/kvm/fpu.h                        |   26 +
 arch/x86/kvm/hyperv.c                     |   21 +-
 arch/x86/kvm/hyperv.h                     |    7 +-
 arch/x86/kvm/ioapic.c                     |    1 +
 arch/x86/kvm/ioapic.h                     |   12 +
 arch/x86/kvm/irq.c                        |    7 +
 arch/x86/kvm/irq.h                        |    6 +
 arch/x86/kvm/lapic.c                      |   28 +-
 arch/x86/kvm/lapic.h                      |    9 +
 arch/x86/kvm/mmu.h                        |   92 +-
 arch/x86/kvm/mmu/mmu.c                    |  134 +-
 arch/x86/kvm/mmu/mmu_internal.h           |   66 -
 arch/x86/kvm/msrs.c                       | 2732 +++++++++++++++
 arch/x86/kvm/msrs.h                       |  156 +
 arch/x86/kvm/mtrr.c                       |    1 +
 arch/x86/kvm/regs.c                       |  875 +++++
 arch/x86/kvm/{kvm_cache_regs.h => regs.h} |  258 +-
 arch/x86/kvm/smm.c                        |    2 +-
 arch/x86/kvm/svm/avic.c                   |    5 +
 arch/x86/kvm/svm/nested.c                 |    9 +-
 arch/x86/kvm/svm/svm.c                    |   19 +-
 arch/x86/kvm/svm/svm.h                    |    2 +-
 arch/x86/kvm/vmx/nested.c                 |    9 +-
 arch/x86/kvm/vmx/nested.h                 |    2 +-
 arch/x86/kvm/vmx/sgx.c                    |    6 +-
 arch/x86/kvm/vmx/tdx.c                    |   18 +-
 arch/x86/kvm/vmx/vmx.c                    |   14 +-
 arch/x86/kvm/vmx/vmx.h                    |    2 +-
 arch/x86/kvm/x86.c                        | 3789 +--------------------
 arch/x86/kvm/x86.h                        |  480 ++-
 arch/x86/kvm/xen.c                        |   39 +-
 35 files changed, 4722 insertions(+), 4576 deletions(-)
 create mode 100644 arch/x86/kvm/msrs.c
 create mode 100644 arch/x86/kvm/msrs.h
 create mode 100644 arch/x86/kvm/regs.c
 rename arch/x86/kvm/{kvm_cache_regs.h => regs.h} (50%)


base-commit: d1568b1332b6b3b36b222c2868fc102727c12a34
-- 
2.54.0.823.g6e5bcc1fc9-goog
Re: [PATCH v3 00/40] KVM: x86: GPR accessors and x86.{c,h} spring cleaning
Posted by Paolo Bonzini 1 week, 1 day ago
On 5/30/26 00:21, Sean Christopherson wrote:
> The first half of this series adds proper, explicit "raw" versions of
> kvm_<reg>_{read,write}(), along with "e" versions (for hardcoded 32-bit
> accesses), and converts the existing kvm_<reg>_{read,write}() APIs into
> mode-aware variants.
> 
> At the end of that journey, introduce regs.{c,h} to avoid moving _more_ code
> into x86.h, especially since the resulting code split would be super arbitrary.

What about getting everything up to patch 14 into 7.2, and then starting 
7.3 development with these, the MMU split series, and possibly the 
pfncache cleanups?

I'll be away starting June 28th, so we probably want to get those three 
merged into kvm/next already towards the end of the merge window.

Paolo

> The second half of the series runs with the regs.{c,h} changes and performs
> spring cleaning on x86.{c,h} and asm/kvm_host.h (in case it wasn't already
> obvious, I have poor impulse control when it comes to cleaning up code).
> 
> I'm most intersted in getting feedback on the file names (regs.{c,h} and
> msrs.{c,h}.  I'm quite confident the actual code split is the way to go, and
> pulling stuff out of asm/kvm_host.h has been on my wish/todo list for years.
> 
> I'll grab these fixes for 7.2 no matter what:
> 
>    KVM: x86: Trace hypercall register *after* truncating values for 32-bit
>    KVM: VMX: Read 32-bit GPR values for ENCLS instructions outside of 64-bit mode
>    KVM: x86/xen: Don't truncate RAX when handling hypercall from protected guest
>    KVM: x86/xen: Bug the VM if 32-bit KVM observes a 64-bit mode hypercall
> 
> Depending on how people feel about the names and cleanups, I'll either send
> the big cleanups as a separate pull request after the initial for-7.2 pull
> requests (if there's overwhelming consensus on the names/splits), or wait for
> 7.3 (if more discussion is needed).
> 
> v3:
>   - Collect more tags. [David, Binbin, Yosry]
>   - Use kvm_run_sync_regs_{from,to}_user() instead of kvm_run_{g,s}et_regs().
>     [Kai]
>   - Fix a variety of typos. [Binbin]
>   - Everything beyond patch 15...
> 
> v2:
>   - https://lore.kernel.org/all/20260514215355.1648463-2-seanjc@google.com
>   - Collect tags. [Yosry, Kai]
>   - Fix some truly egregious goofs. [Binbin]
>   - Rename kvm_cache_regs.h => regs.h, add regs.c. [Yosry, because he
>     complained, not because he actually suggested this :-D ]
>   - Drop superfluous casting/masking of e*x() usage. [Kai]
> 
> v1: https://lore.kernel.org/all/20260409235622.2052730-1-seanjc@google.com
> 
> Sean Christopherson (40):
>    KVM: SVM: Truncate INVLPGA address in compatibility mode
>    KVM: x86/xen: Bug the VM if 32-bit KVM observes a 64-bit mode
>      hypercall
>    KVM: x86/xen: Don't truncate RAX when handling hypercall from
>      protected guest
>    KVM: VMX: Read 32-bit GPR values for ENCLS instructions outside of
>      64-bit mode
>    KVM: x86: Trace hypercall register *after* truncating values for
>      32-bit
>    KVM: x86: Rename kvm_cache_regs.h => regs.h
>    KVM: x86: Move inlined GPR, CR, and DR helpers from x86.h to regs.h
>    KVM: x86: Add mode-aware versions of kvm_<reg>_{read,write}() helpers
>    KVM: x86: Drop non-raw kvm_<reg>_write() helpers
>    KVM: nSVM: Use kvm_rax_read() now that it's mode-aware
>    Revert "KVM: VMX: Read 32-bit GPR values for ENCLS instructions
>      outside of 64-bit mode"
>    KVM: x86: Harden is_64_bit_hypercall() against bugs on 32-bit kernels
>    KVM: x86: Move update_cr8_intercept() to lapic.c
>    KVM: x86: Move async #PF helpers to x86.h (as inlines)
>    KVM: x86: Move the bulk of register specific code from x86.c to regs.c
>    KVM: x86: Move local APIC specific helpers out of asm/kvm_host.h
>    KVM: x86: Drop defunct vcpu_tsc_khz() declaration
>    KVM: x86: Move kvm_caps and kvm_host_values to asm/kvm_host.h
>    KVM: x86: Swap the include order between x86.h and mmu.h
>    KVM: x86: Move tdp_enabled from kvm_host.h to mmu.h
>    KVM: x86: Move eager_page_split to mmu.{c,h}
>    KVM: x86/hyperv: Eliminate an unnecessary include of x86.h in hyperv.h
>    KVM: x86: Move kvm_{load,put}_guest_fpu() to fpu.h
>    KVM: x86: Extract get/set MSR (list) ioctl logic to helpers
>    KVM: x86: Expose several TSC helpers via x86.h for use by MSR code
>    KVM: x86: Move the bulk of MSR specific code from x86.c to msrs.{c,h}
>    KVM: x86: Move register helper declarations from kvm_host.h => regs.h
>    KVM: x86: Move kvm_{g,s}et_segment() to inline helpers in regs.h
>    KVM: x86: Remove defunct kvm_load_segment_descriptor() declaration.
>    KVM: x86: Move MSR helper declarations from kvm_host.h => msrs.h
>    KVM: x86: Move MMU helper declarations from kvm_host.h => mmu.h
>    KVM: x86: Move LLDT assembly wrappers into VMX
>    KVM: x86: Move kvm_cpu_get_apicid() from kvm_host.h => avic.c
>    KVM: x86: Move misc "VALID MASK" defines from kvm_host.h => x86.c
>    KVM: x86: Move __kvm_irq_line_state() from kvm_host.h => ioapic.h
>    KVM: x86: Move IRQ-related helper declarations from kvm_host.h =>
>      irq.h
>    KVM: x86: Move kvm_pv_send_ipi() declaration from kvm_host.h =>
>      lapic.h
>    KVM: x86/mmu: Move kvm_arch_async_page_ready() below
>      kvm_tdp_page_fault()
>    KVM: x86/mmu: Move kvm_mmu_do_page_fault() from mmu_internal.h =>
>      mmu.c
>    KVM: x86: Move a pile of stuff from kvm_host.h => x86.h
> 
>   arch/x86/include/asm/kvm_host.h           |  452 +--
>   arch/x86/kvm/Makefile                     |    4 +-
>   arch/x86/kvm/cpuid.c                      |   13 +-
>   arch/x86/kvm/emulate.c                    |    2 +-
>   arch/x86/kvm/fpu.h                        |   26 +
>   arch/x86/kvm/hyperv.c                     |   21 +-
>   arch/x86/kvm/hyperv.h                     |    7 +-
>   arch/x86/kvm/ioapic.c                     |    1 +
>   arch/x86/kvm/ioapic.h                     |   12 +
>   arch/x86/kvm/irq.c                        |    7 +
>   arch/x86/kvm/irq.h                        |    6 +
>   arch/x86/kvm/lapic.c                      |   28 +-
>   arch/x86/kvm/lapic.h                      |    9 +
>   arch/x86/kvm/mmu.h                        |   92 +-
>   arch/x86/kvm/mmu/mmu.c                    |  134 +-
>   arch/x86/kvm/mmu/mmu_internal.h           |   66 -
>   arch/x86/kvm/msrs.c                       | 2732 +++++++++++++++
>   arch/x86/kvm/msrs.h                       |  156 +
>   arch/x86/kvm/mtrr.c                       |    1 +
>   arch/x86/kvm/regs.c                       |  875 +++++
>   arch/x86/kvm/{kvm_cache_regs.h => regs.h} |  258 +-
>   arch/x86/kvm/smm.c                        |    2 +-
>   arch/x86/kvm/svm/avic.c                   |    5 +
>   arch/x86/kvm/svm/nested.c                 |    9 +-
>   arch/x86/kvm/svm/svm.c                    |   19 +-
>   arch/x86/kvm/svm/svm.h                    |    2 +-
>   arch/x86/kvm/vmx/nested.c                 |    9 +-
>   arch/x86/kvm/vmx/nested.h                 |    2 +-
>   arch/x86/kvm/vmx/sgx.c                    |    6 +-
>   arch/x86/kvm/vmx/tdx.c                    |   18 +-
>   arch/x86/kvm/vmx/vmx.c                    |   14 +-
>   arch/x86/kvm/vmx/vmx.h                    |    2 +-
>   arch/x86/kvm/x86.c                        | 3789 +--------------------
>   arch/x86/kvm/x86.h                        |  480 ++-
>   arch/x86/kvm/xen.c                        |   39 +-
>   35 files changed, 4722 insertions(+), 4576 deletions(-)
>   create mode 100644 arch/x86/kvm/msrs.c
>   create mode 100644 arch/x86/kvm/msrs.h
>   create mode 100644 arch/x86/kvm/regs.c
>   rename arch/x86/kvm/{kvm_cache_regs.h => regs.h} (50%)
> 
> 
> base-commit: d1568b1332b6b3b36b222c2868fc102727c12a34
Re: [PATCH v3 00/40] KVM: x86: GPR accessors and x86.{c,h} spring cleaning
Posted by Sean Christopherson 4 days, 19 hours ago
On Sat, May 30, 2026, Paolo Bonzini wrote:
> On 5/30/26 00:21, Sean Christopherson wrote:
> > The first half of this series adds proper, explicit "raw" versions of
> > kvm_<reg>_{read,write}(), along with "e" versions (for hardcoded 32-bit
> > accesses), and converts the existing kvm_<reg>_{read,write}() APIs into
> > mode-aware variants.
> > 
> > At the end of that journey, introduce regs.{c,h} to avoid moving _more_ code
> > into x86.h, especially since the resulting code split would be super arbitrary.
> 
> What about getting everything up to patch 14 into 7.2, and then starting 7.3
> development with these, the MMU split series, and possibly the pfncache
> cleanups?

I like that idea.  I'll grab these two as well, no reason not to.

  KVM: x86: Remove defunct kvm_load_segment_descriptor() declaration
  KVM: x86: Drop defunct vcpu_tsc_khz() declaration

> I'll be away starting June 28th, so we probably want to get those three
> merged into kvm/next already towards the end of the merge window.
Re: [PATCH v3 00/40] KVM: x86: GPR accessors and x86.{c,h} spring cleaning
Posted by Sean Christopherson 2 days, 14 hours ago
On Fri, 29 May 2026 15:21:43 -0700, Sean Christopherson wrote:
> The first half of this series adds proper, explicit "raw" versions of
> kvm_<reg>_{read,write}(), along with "e" versions (for hardcoded 32-bit
> accesses), and converts the existing kvm_<reg>_{read,write}() APIs into
> mode-aware variants.
> 
> At the end of that journey, introduce regs.{c,h} to avoid moving _more_ code
> into x86.h, especially since the resulting code split would be super arbitrary.
> 
> [...]

Applied the cleanups, and two one-off patches, to kvm-x86 misc.  I'll send a v4
for the code reshuffling, targeting 7.3.

[01/40] KVM: SVM: Truncate INVLPGA address in compatibility mode
        https://github.com/kvm-x86/linux/commit/42a842f3f6b9
[02/40] KVM: x86/xen: Bug the VM if 32-bit KVM observes a 64-bit mode hypercall
        https://github.com/kvm-x86/linux/commit/09912b8ad22f
[03/40] KVM: x86/xen: Don't truncate RAX when handling hypercall from protected guest
        https://github.com/kvm-x86/linux/commit/9377016c2f9e
[04/40] KVM: VMX: Read 32-bit GPR values for ENCLS instructions outside of 64-bit mode
        https://github.com/kvm-x86/linux/commit/f43caad54cc0
[05/40] KVM: x86: Trace hypercall register *after* truncating values for 32-bit
        https://github.com/kvm-x86/linux/commit/16b5d193b212
[06/40] KVM: x86: Rename kvm_cache_regs.h => regs.h
        https://github.com/kvm-x86/linux/commit/ece08316ca21
[07/40] KVM: x86: Move inlined GPR, CR, and DR helpers from x86.h to regs.h
        https://github.com/kvm-x86/linux/commit/ed8a7b89c504
[08/40] KVM: x86: Add mode-aware versions of kvm_<reg>_{read,write}() helpers
        https://github.com/kvm-x86/linux/commit/fc40b1254c32
[09/40] KVM: x86: Drop non-raw kvm_<reg>_write() helpers
        https://github.com/kvm-x86/linux/commit/308851c2d6f8
[10/40] KVM: nSVM: Use kvm_rax_read() now that it's mode-aware
        https://github.com/kvm-x86/linux/commit/a7e6e09b27ca
[11/40] Revert "KVM: VMX: Read 32-bit GPR values for ENCLS instructions outside of 64-bit mode"
        https://github.com/kvm-x86/linux/commit/b6a0fdf2bbf0
[12/40] KVM: x86: Harden is_64_bit_hypercall() against bugs on 32-bit kernels
        https://github.com/kvm-x86/linux/commit/879fffc09474
[13/40] KVM: x86: Move update_cr8_intercept() to lapic.c
        https://github.com/kvm-x86/linux/commit/c7722e5e1dae
[14/40] KVM: x86: Move async #PF helpers to x86.h (as inlines)
        https://github.com/kvm-x86/linux/commit/bc87aec39399

...

[17/40] KVM: x86: Drop defunct vcpu_tsc_khz() declaration
        https://github.com/kvm-x86/linux/commit/e688ca78589d

...

[29/40] KVM: x86: Remove defunct kvm_load_segment_descriptor() declaration.
        https://github.com/kvm-x86/linux/commit/5fd25aacdf89

--
https://github.com/kvm-x86/linux/tree/next