Re: [PATCH v3 02/15] KVM: Define what completing an exit does with its instruction
From: David Woodhouse
Date: Mon Nov 30 2026 - 13:42:27 EST
On Mon, 2026-11-30 at 12:23 -0500, Zack Rusin wrote:
> On Fri, Nov 27, 2026 at 10:27 AM David Woodhouse <dwmw2@xxxxxxxxxxxxx> wrote:
> >
> > 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.
>
> Our userspace returns arithmetic flags as part of the hypercall
> result: a successful VMware hypercall through VMCALL returns with them
> cleared. Could KVM_INSN_COMPLETE allow userspace to update the
> arithmetic flags that an exit defines as outputs?
Yes, I suppose so. Although I think we're inconsistent about that,
aren't we? For most exits kvm_skip_emulated_instruction() will consume
what userspace sets, but if the exit is raised from the *emulator* it
doesn't. Should we fix the latter? Or just keep quiet about it and let
it continue to be inconsistent?
I think I can fix the Hyper-V exit to check the RIP, perhaps only under
the new CAP as it's strictly an API change?
> > +
> > +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;
>
> One thing I'd mention is that queuing a pending exception requires
> enabling KVM_CAP_EXCEPTION_PAYLOAD and setting
> KVM_VCPUEVENT_VALID_PAYLOAD. afaict later KVM_SET_REGS clears the
> pending exception, so userspace has to write the registers first, or
> mark REGS and EVENTS dirty on the same KVM_RUN.
Makes sense. How about this:
https://git.infradead.org/users/dwmw2/linux.git/blob/exit-insn-v4:/Documentation/virt/kvm/api.rst#l7075
Attachment:
smime.p7s
Description: S/MIME cryptographic signature