[PATCH v3 02/15] KVM: Define what completing an exit does with its instruction

From: David Woodhouse

Date: Fri Nov 27 2026 - 10:29:37 EST


From: David Woodhouse <dwmw@xxxxxxxxxxxx>

The KVM_RUN after an exit which left an instruction to complete retires
that instruction. Only MSR exits offer anything else: their 'error'
byte asks for #GP instead.

Define one set of values for such exits: complete the instruction,
inject #GP or #UD at it, or do nothing and resume the guest as
userspace left it. Add the x86 helper which acts on them; a guest with
protected state can only have the instruction completed. No exit takes
them yet.

Signed-off-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
---
Documentation/virt/kvm/api.rst | 50 ++++++++++++++++++++++++++++++++++
arch/x86/kvm/x86.h | 32 ++++++++++++++++++++++
include/uapi/linux/kvm.h | 9 ++++++
3 files changed, 91 insertions(+)

diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index e95c9cda085d..d15dddd79e5b 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -6976,6 +6976,56 @@ to the byte array.
to complete pending operations without allowing any further instructions
to be executed.

+.. _kvm_insn_action:
+
+Some exits which need completion let userspace choose what the
+completion does with the guest instruction that caused the exit. Their
+descriptions say which field takes the choice, and when. The values are
+the same for all of them::
+
+ #define KVM_INSN_COMPLETE 0
+ #define KVM_INSN_GP 1
+ #define KVM_INSN_UD 2
+ #define KVM_INSN_RESUME 3
+
+KVM_INSN_COMPLETE
+ The instruction completes: KVM writes its result, if it has one, and
+ retires it, i.e. moves RIP past it, clears RFLAGS.RF and the interrupt
+ shadow, and honours single-stepping. KVM sets the field to this. RIP
+ must still point at the instruction, and RFLAGS be as the exit left
+ it: userspace which changes either retires the instruction itself,
+ with KVM_INSN_RESUME.
+
+KVM_INSN_GP, KVM_INSN_UD
+ The instruction faults: KVM injects #GP with a zero error code, or
+ #UD, and RIP stays at the instruction. Every exit which takes these
+ values takes both; userspace chooses the one which the CPU would raise
+ for the instruction.
+
+KVM_INSN_RESUME
+ KVM does nothing for the instruction. It writes no result and injects
+ nothing, and the guest resumes from its state as userspace left it.
+ The instruction has not retired, so the interrupt shadow and RFLAGS.RF
+ stay as they are and no single-step trap is raised, neither a #DB for
+ the guest nor KVM_EXIT_DEBUG. What the guest sees is up to userspace:
+
+ - with an exception queued by KVM_SET_VCPU_EVENTS, the guest takes the
+ exception at the instruction;
+ - with RIP moved, the guest continues from there: userspace has
+ retired the instruction itself and owns everything that goes with
+ that;
+ - with nothing changed, the guest executes the instruction again and
+ KVM_RUN returns with the same exit.
+
+Any other value is reserved: KVM_RUN fails with -EINVAL, and the guest
+is left as for KVM_INSN_RESUME.
+
+A guest whose register state is protected (SEV-ES, SEV-SNP, TDX) does
+not execute the instruction again, and KVM cannot inject #UD into it.
+KVM can only complete the exit there, with whatever outcome the exit
+itself has, and the description of each exit says what it makes of
+another value.
+
::

/* KVM_EXIT_HYPERCALL */
diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
index 436e2fb1396c..7e5ccbce9114 100644
--- a/arch/x86/kvm/x86.h
+++ b/arch/x86/kvm/x86.h
@@ -526,6 +526,38 @@ static inline void kvm_inject_gp(struct kvm_vcpu *vcpu, u32 error_code)
kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
}

+/*
+ * Act on the KVM_INSN_* answer which userspace gave to an exit that left an
+ * instruction to complete. Returns 0 if the caller is to complete the
+ * instruction, 1 if there is nothing more to do for it, or -EINVAL.
+ *
+ * A guest whose state is protected does not execute the instruction again,
+ * and KVM cannot inject #UD into it: all that can be done there is to
+ * complete the exit, with an outcome which is the caller's to choose from
+ * the answer, or to ignore it.
+ */
+static inline int kvm_insn_uncompleted(struct kvm_vcpu *vcpu, u8 answer)
+{
+ if (vcpu->kvm->arch.has_protected_state ||
+ vcpu->arch.guest_state_protected)
+ return 0;
+
+ switch (answer) {
+ case KVM_INSN_COMPLETE:
+ return 0;
+ case KVM_INSN_GP:
+ kvm_inject_gp(vcpu, 0);
+ return 1;
+ case KVM_INSN_UD:
+ kvm_queue_exception(vcpu, UD_VECTOR);
+ return 1;
+ case KVM_INSN_RESUME:
+ return 1;
+ default:
+ return -EINVAL;
+ }
+}
+
void kvm_inject_nmi(struct kvm_vcpu *vcpu);
int kvm_get_nr_pending_nmis(struct kvm_vcpu *vcpu);

diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index 8dff2fc1972e..0a4c548cb329 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -220,6 +220,15 @@ struct kvm_exit_snp_req_certs {
#define HINT_UNSAFE_IN_KVM(_symbol) _symbol
#endif

+/*
+ * What the next KVM_RUN does with the instruction which an exit left to be
+ * completed, for the exits whose description says that they take one.
+ */
+#define KVM_INSN_COMPLETE 0 /* retire it, with its result if any */
+#define KVM_INSN_GP 1 /* inject #GP(0) at it */
+#define KVM_INSN_UD 2 /* inject #UD at it */
+#define KVM_INSN_RESUME 3 /* nothing: resume the guest as it is */
+
/* for KVM_RUN, returned by mmap(vcpu_fd, offset=0) */
struct kvm_run {
/* in */
--
2.50.1