Re: [PATCH v3 02/15] KVM: Define what completing an exit does with its instruction
From: Zack Rusin
Date: Mon Nov 30 2026 - 12:27:01 EST
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?
> +
> +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.
z
Attachment:
smime.p7s
Description: S/MIME Cryptographic Signature