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 06/15] KVM: x86: hyper-v: Let userspace leave a hypercall uncompleted
Date: Fri, 27 Nov 2026 15:27:21 +0000 [thread overview]
Message-ID: <20261127152730.99319-7-dwmw2@infradead.org> (raw)
In-Reply-To: <20261127152730.99319-1-dwmw2@infradead.org>
From: David Woodhouse <dwmw@amazon.co.uk>
A VMM handling KVM_EXIT_HYPERV_HCALL can only complete the hypercall:
KVM writes the result and advances RIP. The TLFS has some raise #UD
at the instruction instead: a VTL return from the lowest VTL, for one.
Add a 'ret' field to the exit, which takes KVM_INSN_*: zero completes
as before, KVM_INSN_UD and KVM_INSN_GP inject the exception, and
KVM_INSN_RESUME does nothing, so an exception which userspace queued
is taken at the hypercall. It is opt-in (KVM_HYPERV_HCALL_RET):
existing VMMs never zeroed the union.
Link: https://lore.kernel.org/kvm/avuDUNAU_a933zvp@seanjc-mac.roam.internal/
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
Documentation/virt/kvm/api.rst | 25 +++++++++++++++++++++++++
arch/x86/kvm/hyperv.c | 20 +++++++++++++++++++-
arch/x86/kvm/hyperv.h | 3 ++-
include/uapi/linux/kvm.h | 3 +++
4 files changed, 49 insertions(+), 2 deletions(-)
diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index 782c010a08a0..ea48247c2976 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -7336,6 +7336,8 @@ EOI was received.
__u64 input;
__u64 result;
__u64 params[2];
+ __u8 ret;
+ __u8 pad3[7];
} hcall;
struct {
__u32 msr;
@@ -7372,6 +7374,23 @@ way, HvCallPostMessage in some cases among them;
KVM_CAP_HYPERV_FORWARD_HCALL (see section 7.49) adds the hypercalls KVM
does not implement.
+KVM sets 'ret' to zero, KVM_INSN_COMPLETE, which means that the
+hypercall completes as described above. With KVM_HYPERV_HCALL_RET
+enabled, userspace may set 'ret' to another of the KVM_INSN_* values
+instead (see :ref:`the description of completion <kvm_insn_action>`),
+and the hypercall does not complete: KVM writes no result and leaves RIP
+at the hypercall instruction. KVM_INSN_UD and KVM_INSN_GP inject #UD
+and #GP(0), the exceptions which Hyper-V raises when it refuses a
+hypercall itself. KVM_INSN_RESUME injects nothing: a VMM which wants
+another exception (e.g. #PF with the address in exception_payload)
+queues it with KVM_SET_VCPU_EVENTS before it resumes the vCPU, and the
+guest takes it at the hypercall instruction. 'pad3' is reserved and is
+zero.
+
+For a guest whose register state is protected (SEV-ES, SEV-SNP, TDX)
+'ret' is ignored, with or without KVM_HYPERV_HCALL_RET: the hypercall
+completes.
+
- KVM_EXIT_HYPERV_SYNDBG -- synchronously notify user-space about
Hyper-V Synthetic debugger state change. Notification is used to either update
@@ -9293,6 +9312,7 @@ KVM_CHECK_EXTENSION returns, so features only ever use bits 0 to 30.
The defined bits are::
#define KVM_HYPERV_HCALL_FORWARD_UNKNOWN (1ULL << 0)
+ #define KVM_HYPERV_HCALL_RET (1ULL << 1)
KVM_HYPERV_HCALL_FORWARD_UNKNOWN: a hypercall that KVM would otherwise
fail with HV_STATUS_INVALID_HYPERCALL_CODE exits to userspace with
@@ -9313,6 +9333,11 @@ KVM_GET_FPU/KVM_SET_FPU) before the vCPU is resumed. Hypercalls that
KVM implements itself, and the CPUID-enforcement checks KVM applies
before dispatching a hypercall, are unaffected.
+KVM_HYPERV_HCALL_RET: KVM honours the 'ret' field of every
+KVM_EXIT_HYPERV_HCALL exit, whichever hypercall caused it, and fails
+KVM_RUN with -EINVAL for a value it does not define. Without it the
+field is ignored and every such exit completes.
+
8. Other capabilities.
======================
diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
index e72539f9acc9..67c9d20547d8 100644
--- a/arch/x86/kvm/hyperv.c
+++ b/arch/x86/kvm/hyperv.c
@@ -2430,7 +2430,22 @@ static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result)
static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
{
- return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result);
+ /*
+ * Userspace may say that the hypercall does not complete: the result
+ * stays unwritten and RIP at the instruction, where the guest takes
+ * the exception that userspace asked for, or queued itself. Not so
+ * for a guest with protected state, whose hypercall can only complete.
+ */
+ if (kvm_hv_hcall_feature(vcpu->kvm, KVM_HYPERV_HCALL_RET)) {
+ u8 ret = READ_ONCE(vcpu->run->hyperv.u.hcall.ret);
+ int r = kvm_insn_uncompleted(vcpu, ret);
+
+ if (r)
+ return r;
+ }
+
+ return kvm_hv_hypercall_complete(vcpu,
+ READ_ONCE(vcpu->run->hyperv.u.hcall.result));
}
static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
@@ -2716,6 +2731,9 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
vcpu->run->hyperv.u.hcall.input = hc.param;
vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa;
vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa;
+ vcpu->run->hyperv.u.hcall.ret = 0;
+ memset(vcpu->run->hyperv.u.hcall.pad3, 0,
+ sizeof(vcpu->run->hyperv.u.hcall.pad3));
vcpu->arch.complete_userspace_io = kvm_hv_hypercall_complete_userspace;
return 0;
}
diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h
index 20d40261c75b..5b6220b17e77 100644
--- a/arch/x86/kvm/hyperv.h
+++ b/arch/x86/kvm/hyperv.h
@@ -206,7 +206,8 @@ static inline bool kvm_hv_hypercall_enabled(struct kvm_vcpu *vcpu)
}
/* Features reported and accepted by KVM_CAP_HYPERV_FORWARD_HCALL. */
-#define KVM_HYPERV_HCALL_SUPPORTED KVM_HYPERV_HCALL_FORWARD_UNKNOWN
+#define KVM_HYPERV_HCALL_SUPPORTED (KVM_HYPERV_HCALL_FORWARD_UNKNOWN | \
+ KVM_HYPERV_HCALL_RET)
static inline bool kvm_hv_hcall_feature(struct kvm *kvm, u64 feature)
{
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 5803c84cf8f8..e480544a3746 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -113,6 +113,8 @@ struct kvm_hyperv_exit {
__u64 input;
__u64 result;
__u64 params[2];
+ __u8 ret; /* in: KVM_INSN_* */
+ __u8 pad3[7];
} hcall;
struct {
__u32 msr;
@@ -128,6 +130,7 @@ struct kvm_hyperv_exit {
/* KVM_CAP_HYPERV_FORWARD_HCALL feature bits */
#define KVM_HYPERV_HCALL_FORWARD_UNKNOWN (1ULL << 0)
+#define KVM_HYPERV_HCALL_RET (1ULL << 1)
struct kvm_xen_exit {
#define KVM_EXIT_XEN_HCALL 1
--
2.50.1
next prev parent 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 [PATCH v3 00/15] KVM: x86: Exit handling for Hyper-V hcalls, register intercept David Woodhouse
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 ` David Woodhouse [this message]
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-7-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®