[PATCH v3 00/15] KVM: x86: Exit handling for Hyper-V hcalls, register intercept
From: David Woodhouse
Date: Fri Nov 27 2026 - 10:28:01 EST
From: David Woodhouse <dwmw@xxxxxxxxxxxx>
Add KVM_CAP_HYPERV_FORWARD_HCALL. As with KVM_CAP_XEN_HVM and
KVM_CAP_EXIT_HYPERCALL, KVM_CHECK_EXTENSION returns a bitmask of
features and KVM_ENABLE_CAP takes the subset to enable. The first
feature, KVM_HYPERV_HCALL_FORWARD_UNKNOWN, surfaces unknown hypercalls
to userspace through the existing KVM_EXIT_HYPERV_HCALL exit.
A second feature, KVM_HYPERV_HCALL_RET, allows userspace to choose
whether/how the kernel should complete the hypercall, populating a
'ret' field with one of:
KVM_INSN_COMPLETE finish the instruction, as now
KVM_INSN_GP inject #GP(0) at it
KVM_INSN_UD inject #UD at it
KVM_INSN_RESUME do nothing; resume the guest as userspace left it
Hyper-V needs to do this in some cases, for example HvCallVtlCall and
HvCallVtlReturn are supposed to raise #UD when they are refused.
Further features will follow for HvCallVtlCall and HvCallVtlReturn on
VMs with planes, so that KVM can handle VTL transitions in the kernel.
Also add KVM_CAP_X86_REG_WRITE_FILTER for trapping control register
writes (CR0, CR4, XCR0, IDTR, LDTR, etc.).
Add KVM_CAP_X86_MSR_EXIT_INSN to allow MSR exits to gain the same
ability to COMPLETE/GP/UD/RESUME so that they can be handled by
another plane too. The existing 'error' field was already a u8 there,
so userspace can just opt in to using the new KVM_INSN_xxx values in
place of the existing 'non-zero means #GP' behaviour.
The VMware hypercall series¹ needs to fault its instruction too, and
with more than #GP. As posted, it has its exit carry a vector, an
error code and a fault address for KVM to inject. It can use the same
model, and the complex faults can be injected with KVM_SET_VCPU_EVENTS
followed by a ret of KVM_INSN_RESUME.
The completion result code deliberately does not cover:
- PIO and MMIO. Those completions carry state which can't just be
dropped, and VSM has no use for it: what a higher VTL intercepts
there is the memory access, which faults before the instruction has
done anything.
- Guests with protected state. There is no instruction for KVM to
leave alone, so MSR exits keep today's meaning (any non-zero value
fails the access), a Hyper-V hypercall always completes, and a
register write filter cannot be armed. Nor can one be armed for a
plain SEV guest, whose instructions KVM cannot decode.
- KVM_EXIT_HYPERCALL, KVM_EXIT_XEN and KVM_EXIT_TDX, which are
untouched.
On an Intel Xeon 6 (itself a KVM guest, so VMX there is nested), on the
series as posted, the three new tests and hyperv_forward_hcall pass, as
do userspace_msr_exit_test, msrs_test, hyperv_features,
hyperv_extended_hypercalls, set_sregs_test, cr4_cpuid_sync_test,
xcr0_cpuid_test, smm_test and state_test: 13 of 13. The three with
emulator legs (msr_exit_insn_test, userspace_msr_exit_test and
reg_write_filter_test) pass again with kvm.force_emulation_prefix=1, as
do hyperv_forward_hcall and nested_reg_write_filter_test.
On a bare-metal AMD EPYC 9R14 (Zen 4), also on the series as posted,
the same 13 pass, and the same five again with
kvm.force_emulation_prefix=1. The three new tests also pass with
kvm_amd nrips=0, with and without forced emulation; there the
instruction lengths come from the emulator.
An earlier revision of these patches is at the base of the full VSM
tree², on which Windows Server 2025 boots with VBS and Credential
Guard, through the hypercall and register write exits.
One open question, on patch 7. It reports
HV_X64_HYPERCALL_XMM_OUTPUT_AVAILABLE unconditionally, as it did in
v2, although only a VMM which handles forwarded hypercalls can honour
it. I wonder if it should only be reported once the VMM has opted in,
perhaps just tied to KVM_HYPERV_HCALL_FORWARD_UNKNOWN. I've left it as
it is for now.
Based on v7.4-rc2. Also at:
https://git.infradead.org/users/dwmw2/linux.git/shortlog/refs/heads/exit-insn
Changes since v2³ of the hypercall forwarding series, which is patches
5-8 here. Everything else is new.
- Drop KVM_VCPUEVENT_CANCEL_HCALL and the KVM_CAP_VCPU_EVENTS feature
mask; the exit takes KVM_INSN_* in a 'ret' field instead (Sean⁴)
- Drop vcpu_events_cancel_test; hyperv_forward_hcall covers 'ret'
- Document that the feature mask only uses bits 0 to 30, and enforce
it at build time
- No allowlist of forwarded hypercalls. As discussed in the v2 thread⁵
the capability is needed either way, and anything KVM learns to
handle later has to be a new bit in its mask, so that a VMM which
forwards that hypercall today keeps receiving it
- The fix to the XMM helpers which Sashiko's review of the selftest
led to⁶ is patch 1 here, now with a Fixes: tag
¹ https://lore.kernel.org/kvm/cover.1794374897.git.zack.rusin@xxxxxxxxxxxx/
² https://git.infradead.org/users/dwmw2/linux.git/shortlog/refs/heads/vsm3w/p4-startup
³ https://lore.kernel.org/kvm/20261113170535.79246-1-dwmw2@xxxxxxxxxxxxx/
⁴ https://lore.kernel.org/kvm/avuDUNAU_a933zvp@xxxxxxxxxxxxxxx.internal/
⁵ https://lore.kernel.org/kvm/awCNTITOOiS1JEod@xxxxxxxxxxxxxxx.internal/
⁶ https://lore.kernel.org/kvm/20261113181053.81779-1-dwmw2@xxxxxxxxxxxxx/
David Woodhouse (15):
KVM: selftests: Make the x86 SSE register helpers volatile
KVM: Define what completing an exit does with its instruction
KVM: x86: Let userspace leave an MSR access alone
KVM: selftests: Test MSR accesses which userspace leaves alone
KVM: x86: hyper-v: Add KVM_CAP_HYPERV_FORWARD_HCALL
KVM: x86: hyper-v: Let userspace leave a hypercall uncompleted
KVM: x86: hyper-v: Advertise XMM fast hypercall output
KVM: selftests: Add Hyper-V hypercall forwarding test
KVM: x86: Add a per-vCPU register write filter
KVM: x86: Filter emulated CR0 and CR4 writes
KVM: VMX: Support CR0 and CR4 write filters
KVM: SVM: Support CR0 and CR4 write filters
KVM: x86: Add descriptor-table registers to the write filter
KVM: selftests: Add x86 register write filter test
KVM: selftests: Test the register write filter with a nested guest
Documentation/virt/kvm/api.rst | 302 ++++-
arch/x86/include/asm/kvm-x86-ops.h | 1 +
arch/x86/include/asm/kvm_host.h | 23 +
arch/x86/include/uapi/asm/kvm.h | 17 +
arch/x86/kvm/emulate.c | 54 +-
arch/x86/kvm/hyperv.c | 28 +-
arch/x86/kvm/hyperv.h | 9 +
arch/x86/kvm/kvm_emulate.h | 28 +
arch/x86/kvm/msrs.c | 75 +-
arch/x86/kvm/regs.h | 43 +
arch/x86/kvm/svm/nested.c | 16 +
arch/x86/kvm/svm/sev.c | 22 +
arch/x86/kvm/svm/svm.c | 138 ++-
arch/x86/kvm/vmx/main.c | 13 +
arch/x86/kvm/vmx/nested.c | 27 +-
arch/x86/kvm/vmx/vmx.c | 167 ++-
arch/x86/kvm/vmx/vmx.h | 14 +-
arch/x86/kvm/vmx/x86_ops.h | 1 +
arch/x86/kvm/x86.c | 343 +++++-
arch/x86/kvm/x86.h | 34 +-
include/uapi/linux/kvm.h | 35 +-
tools/testing/selftests/kvm/Makefile.kvm | 4 +
.../selftests/kvm/include/x86/processor.h | 33 +-
tools/testing/selftests/kvm/lib/kvm_util.c | 1 +
.../selftests/kvm/x86/hyperv_forward_hcall.c | 395 +++++++
.../selftests/kvm/x86/msr_exit_insn_test.c | 308 +++++
.../kvm/x86/nested_reg_write_filter_test.c | 362 ++++++
.../selftests/kvm/x86/reg_write_filter_test.c | 1004 +++++++++++++++++
28 files changed, 3437 insertions(+), 60 deletions(-)
create mode 100644 tools/testing/selftests/kvm/x86/hyperv_forward_hcall.c
create mode 100644 tools/testing/selftests/kvm/x86/msr_exit_insn_test.c
create mode 100644 tools/testing/selftests/kvm/x86/nested_reg_write_filter_test.c
create mode 100644 tools/testing/selftests/kvm/x86/reg_write_filter_test.c
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
--
2.50.1