From: David Woodhouse <dwmw2@infradead.org>
To: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org,
linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Sean Christopherson" <seanjc@google.com>,
"Vitaly Kuznetsov" <vkuznets@redhat.com>,
"Marcelo Tosatti" <mtosatti@redhat.com>,
"Joerg Roedel" <joro@8bytes.org>,
"Tom Lendacky" <thomas.lendacky@amd.com>,
"James Bottomley" <James.Bottomley@HansenPartnership.com>,
"Thara Gopinath" <tgopinath@linux.microsoft.com>,
"Wei Liu" <wei.liu@kernel.org>,
"Michael Kelley" <mhklinux@outlook.com>,
"Nicolas Saenz Julienne" <nsaenz@amazon.com>,
"Nikolas Wipper" <nik.wipper@gmx.de>,
"Mushahid Hussain" <hmushi@amazon.co.uk>,
"Sriram Nambakam" <snambakam@linux.microsoft.com>,
"Zack Rusin" <zack.rusin@broadcom.com>,
"Doug Covelli" <doug.covelli@broadcom.com>,
"Jim Mattson" <jmattson@google.com>,
"Thomas Gleixner" <tglx@kernel.org>,
"Ingo Molnar" <mingo@redhat.com>,
"Borislav Petkov" <bp@alien8.de>,
"Dave Hansen" <dave.hansen@linux.intel.com>,
x86@kernel.org, "H . Peter Anvin" <hpa@zytor.com>,
"Jonathan Corbet" <corbet@lwn.net>,
"Randy Dunlap" <rdunlap@infradead.org>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Paul Durrant" <paul@xen.org>, "Xin Li" <xin@zytor.com>,
"Francesco Lavra" <flavra@baylibre.com>,
"Radim Krčmář" <rkrcmar@ventanamicro.com>,
"Liran Alon" <liran.alon@oracle.com>,
"Arbel Moshe" <arbel.moshe@oracle.com>,
"David Woodhouse" <dwmw@amazon.co.uk>
Subject: [PATCH v3 00/15] KVM: x86: Exit handling for Hyper-V hcalls, register intercept
Date: Fri, 27 Nov 2026 15:27:15 +0000 [thread overview]
Message-ID: <20261127152730.99319-1-dwmw2@infradead.org> (raw)
From: David Woodhouse <dwmw@amazon.co.uk>
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@broadcom.com/
² https://git.infradead.org/users/dwmw2/linux.git/shortlog/refs/heads/vsm3w/p4-startup
³ https://lore.kernel.org/kvm/20261113170535.79246-1-dwmw2@infradead.org/
⁴ https://lore.kernel.org/kvm/avuDUNAU_a933zvp@seanjc-mac.roam.internal/
⁵ https://lore.kernel.org/kvm/awCNTITOOiS1JEod@seanjc-mac.roam.internal/
⁶ https://lore.kernel.org/kvm/20261113181053.81779-1-dwmw2@infradead.org/
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
next reply other threads:[~2026-11-27 15:27 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 15:27 David Woodhouse [this message]
2026-11-27 15:27 ` [PATCH v3 01/15] KVM: selftests: Make the x86 SSE register helpers volatile David Woodhouse
2026-11-27 15:27 ` [PATCH v3 02/15] KVM: Define what completing an exit does with its instruction David Woodhouse
2026-11-27 15:27 ` [PATCH v3 03/15] KVM: x86: Let userspace leave an MSR access alone David Woodhouse
2026-11-27 15:27 ` [PATCH v3 04/15] KVM: selftests: Test MSR accesses which userspace leaves alone David Woodhouse
2026-11-27 15:27 ` [PATCH v3 05/15] KVM: x86: hyper-v: Add KVM_CAP_HYPERV_FORWARD_HCALL David Woodhouse
2026-11-27 15:27 ` [PATCH v3 06/15] KVM: x86: hyper-v: Let userspace leave a hypercall uncompleted David Woodhouse
2026-11-27 15:27 ` [PATCH v3 07/15] KVM: x86: hyper-v: Advertise XMM fast hypercall output David Woodhouse
2026-11-27 15:27 ` [PATCH v3 08/15] KVM: selftests: Add Hyper-V hypercall forwarding test David Woodhouse
2026-11-27 15:27 ` [PATCH v3 09/15] KVM: x86: Add a per-vCPU register write filter David Woodhouse
2026-11-27 15:27 ` [PATCH v3 10/15] KVM: x86: Filter emulated CR0 and CR4 writes David Woodhouse
2026-11-27 15:27 ` [PATCH v3 11/15] KVM: VMX: Support CR0 and CR4 write filters David Woodhouse
2026-11-27 15:27 ` [PATCH v3 12/15] KVM: SVM: " David Woodhouse
2026-11-27 15:27 ` [PATCH v3 13/15] KVM: x86: Add descriptor-table registers to the write filter David Woodhouse
2026-11-27 15:27 ` [PATCH v3 14/15] KVM: selftests: Add x86 register write filter test David Woodhouse
2026-11-27 15:27 ` [PATCH v3 15/15] KVM: selftests: Test the register write filter with a nested guest David Woodhouse
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261127152730.99319-1-dwmw2@infradead.org \
--to=dwmw2@infradead.org \
--cc=James.Bottomley@HansenPartnership.com \
--cc=arbel.moshe@oracle.com \
--cc=bp@alien8.de \
--cc=corbet@lwn.net \
--cc=dave.hansen@linux.intel.com \
--cc=doug.covelli@broadcom.com \
--cc=dwmw@amazon.co.uk \
--cc=flavra@baylibre.com \
--cc=hmushi@amazon.co.uk \
--cc=hpa@zytor.com \
--cc=jmattson@google.com \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hyperv@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=liran.alon@oracle.com \
--cc=mhklinux@outlook.com \
--cc=mingo@redhat.com \
--cc=mtosatti@redhat.com \
--cc=nik.wipper@gmx.de \
--cc=nsaenz@amazon.com \
--cc=paul@xen.org \
--cc=pbonzini@redhat.com \
--cc=rdunlap@infradead.org \
--cc=rkrcmar@ventanamicro.com \
--cc=seanjc@google.com \
--cc=skhan@linuxfoundation.org \
--cc=snambakam@linux.microsoft.com \
--cc=tglx@kernel.org \
--cc=tgopinath@linux.microsoft.com \
--cc=thomas.lendacky@amd.com \
--cc=vkuznets@redhat.com \
--cc=wei.liu@kernel.org \
--cc=x86@kernel.org \
--cc=xin@zytor.com \
--cc=zack.rusin@broadcom.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®