[PATCH v2 0/4] KVM: x86: hyper-v: Forward unimplemented hypercalls to userspace
From: David Woodhouse
Date: Fri Nov 13 2026 - 13:42:00 EST
From: David Woodhouse <dwmw@xxxxxxxxxxxx>
KVM implements a fixed set of Hyper-V hypercalls and fails every other
code with HV_STATUS_INVALID_HYPERCALL_CODE. A VMM that wants to
implement more of the TLFS, such as the VSM hypercalls Windows uses for
Virtualization Based Security, never sees them.
This series adds 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, rejecting any
bit the kernel does not report. The first feature,
KVM_HYPERV_HCALL_FORWARD_UNKNOWN, sends the hypercalls KVM does not
implement, and extended hypercalls in fast form, out through the
existing KVM_EXIT_HYPERV_HCALL exit, whose format does not change at
all. Further mask bits will follow for HvCallVtlCall and HvCallVtlReturn
on VMs with planes, so that KVM can handle VTL transitions in the
kernel.
Patch 2 lets userspace cancel the pending completion of a hypercall
that exited to userspace, with a new KVM_VCPUEVENT_CANCEL_HCALL flag on
KVM_SET_VCPU_EVENTS. The guest then re-executes the hypercall, unless
the same ioctl queued an exception to be delivered at it. That is how a
VMM makes a hypercall raise #UD (or #PF with CR2, or anything else the
events machinery can queue), which the TLFS specifies for some invalid
uses, instead of returning a status. It is independent of the rest of
the series: in v1 this was a #UD request flag in the hcall exit itself,
but the KVM_EXIT_VMWARE series [1] wants the same for other exits and
other vectors, and KVM_SET_VCPU_EVENTS already knows how to queue every
exception shape, so v2 adds only the cancel primitive and lets the
existing machinery do the rest. KVM_SET_VCPU_EVENTS has no flag
discovery, so KVM_CHECK_EXTENSION(KVM_CAP_VCPU_EVENTS) becomes a
feature mask (bit 0 keeps truth-value checks working, bit 1 advertises
the cancel). The KVM_VCPUEVENT_ namespace so far only holds VALID_*
field gates; if the action flag wants a different name, I am not
attached to this one.
Patch 3 reports HV_X64_HYPERCALL_XMM_OUTPUT_AVAILABLE, since a VMM
handling a forwarded fast hypercall can write the XMM output itself.
Patch 4 adds the Hyper-V forwarding selftest; the generic cancel error
paths come with patch 2's own selftest.
v1->v2:
- Drop KVM_HYPERV_HCALL_RAISE_UD, KVM_HYPERV_HCALL_UD and the hcall
exit's flags field; cancel the completion through
KVM_SET_VCPU_EVENTS instead (patch 2).
- Selftests: a generic vcpu_events_cancel_test, and forwarding-test
legs for cancel+#UD (RIP and RAX untouched), cancel alone (the
hypercall exits a second time), and EBUSY with nothing cancellable
pending.
The series is based on v7.4-rc2. Tested on a bare-metal AMD EPYC
(Zen 4) system: vcpu_events_cancel_test and hyperv_forward_hcall pass,
as do hyperv_clock, hyperv_cpuid, hyperv_extended_hypercalls,
hyperv_features, hyperv_ipi, hyperv_tlb_flush and hyperv_svm_test, plus
triple_fault_event_test and nested_exceptions_test for the
KVM_SET_VCPU_EVENTS change. Not yet retested on Intel.
[1] https://lore.kernel.org/all/90467782be23b83fd3e9a6afe2f1d2149eaf9db9.1794374897.git.zack.rusin@xxxxxxxxxxxx/
David Woodhouse (4):
KVM: x86: hyper-v: Add KVM_CAP_HYPERV_FORWARD_HCALL
KVM: x86: Let userspace cancel a pending hypercall completion
KVM: x86: hyper-v: Advertise XMM fast hypercall output
KVM: selftests: Add Hyper-V hypercall forwarding test
Documentation/virt/kvm/api.rst | 73 ++++
arch/x86/include/asm/kvm_host.h | 4 +
arch/x86/include/uapi/asm/kvm.h | 7 +
arch/x86/kvm/hyperv.c | 14 +-
arch/x86/kvm/hyperv.h | 8 +
arch/x86/kvm/x86.c | 51 ++-
include/uapi/linux/kvm.h | 4 +
tools/testing/selftests/kvm/Makefile.kvm | 2 +
.../selftests/kvm/x86/hyperv_forward_hcall.c | 337 ++++++++++++++++++
.../kvm/x86/vcpu_events_cancel_test.c | 119 +++++++
10 files changed, 616 insertions(+), 3 deletions(-)
create mode 100644 tools/testing/selftests/kvm/x86/hyperv_forward_hcall.c
create mode 100644 tools/testing/selftests/kvm/x86/vcpu_events_cancel_test.c
--
2.50.1