mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Woodhouse <dwmw2@infradead.org>
To: Zack Rusin <zack.rusin@broadcom.com>
Cc: kvm@vger.kernel.org, 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>,
	"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>
Subject: Re: [PATCH v3 02/15] KVM: Define what completing an exit does with its instruction
Date: Mon, 30 Nov 2026 18:42:17 +0000	[thread overview]
Message-ID: <f8048aeb500567332c8b434bc695996eca764dc9.camel@infradead.org> (raw)
In-Reply-To: <CABQX2QOCgjK6iBWhetqYELxecJVERUp9=2k3aCeXeH4e+2QcCA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 4381 bytes --]

On Mon, 2026-11-30 at 12:23 -0500, Zack Rusin wrote:
> On Fri, Nov 27, 2026 at 10:27 AM David Woodhouse <dwmw2@infradead.org> wrote:
> > 
> > From: David Woodhouse <dwmw@amazon.co.uk>
> > 
> > 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@amazon.co.uk>
> > ---
> >  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 #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 6179 bytes --]

  reply	other threads:[~2026-11-30 18:42 UTC|newest]

Thread overview: 23+ 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-30 17:23   ` Zack Rusin
2026-11-30 18:42     ` David Woodhouse [this message]
2026-11-30 19:51       ` Zack Rusin
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 ` [PATCH v3 06/15] KVM: x86: hyper-v: Let userspace leave a hypercall uncompleted David Woodhouse
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
2026-11-30 14:55 ` [PATCH v3 00/15] KVM: x86: Exit handling for Hyper-V hcalls, register intercept David Woodhouse
2026-11-30 17:19 ` Zack Rusin
2026-11-30 18:07   ` David Woodhouse
2026-11-30 20:00     ` Zack Rusin

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=f8048aeb500567332c8b434bc695996eca764dc9.camel@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=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®