All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Mathias Krause <minipli@grsecurity.net>
Cc: kvm@vger.kernel.org, Paolo Bonzini <pbonzini@redhat.com>,
	 Andrew Jones <andrew.jones@linux.dev>
Subject: Re: [kvm-unit-tests PATCH v3 04/20] x86: Dedup guest/host context switch of registers across SVM and VMX
Date: Tue, 19 May 2026 18:30:02 -0700	[thread overview]
Message-ID: <ag0OmjV_1q9D5QAQ@google.com> (raw)
In-Reply-To: <3d357a67-b1e0-4204-8748-a926ced24c5c@grsecurity.net>

On Tue, May 19, 2026, Mathias Krause wrote:
> On 14.05.26 23:04, Sean Christopherson wrote:
> > Deduplicate the context switching of registers across VM-Enter<=>VM-Exit
> > between SVM and VMX.  The required functionality and implementations are
> > practically identical, literally the only difference is that SVM doesn't
> > need (or want) to manually swap RAX as SVM automatically swaps RAX at
> > VMRUN and #VMEXIT.
> > 
> > Opportunistically rename the structure to "guest_regs" to clarify its
> > purpose, and to avoid conflicts, e.g. with realmode's "struct regs".
> > 
> > Signed-off-by: Sean Christopherson <seanjc@google.com>
> > ---
> >  lib/x86/virt.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++
> >  x86/svm.c      |  4 ++--
> >  x86/svm.h      | 47 ++++-----------------------------------------
> >  x86/vmx.c      |  8 ++++----
> >  x86/vmx.h      | 42 +---------------------------------------
> >  5 files changed, 63 insertions(+), 90 deletions(-)
> >  create mode 100644 lib/x86/virt.h
> > 
> > diff --git a/lib/x86/virt.h b/lib/x86/virt.h
> > new file mode 100644
> > index 00000000..ccc90c25
> > --- /dev/null
> > +++ b/lib/x86/virt.h
> > @@ -0,0 +1,52 @@
> > +#ifndef _x86_VIRT_H_
> > +#define _x86_VIRT_H_
> > +
> > +#include "libcflat.h"
> > +
> > +struct guest_regs {
> > +	u64 rax;
> > +	u64 rcx;
> > +	u64 rdx;
> > +	u64 rbx;
> > +	/*
> > +	 * Use RSP's index to hold CR3, as RSP isn't manually context switched
>                                    ^^^ that should be CR2...

This one should be CR2.  CR2 isn't context switch via the VMCS.  Ugh, but looking
at the usage, I think this has been dead code since it was introduced in commit
9d7eaa29 ("kvm-unit-tests : Basic architecture of VMX nested test case").  I was
just reverse engineering the intent when writing the comment, and didn't actually
check to see if KUT does what someone intended it to do.

nVMX likely works because no test takes page faults in both L1 and L2, i.e. either
L2 doesn't care about CR2, or L1 never clobbers it.

Hrm.  I'm leaning towards deleting my comment and maintaining the status quo, for
now.  As tempting as it is to fix the bug, nSVM doesn't need the help, there's
zero reason to context CR2 in assembly code, and I doubt a test will ever rely on
CR2 being context switched.  

> > +	 * by software in any relevant flows.
> > +	 */
> > +	u64 cr2;
>             ^^^ or this one cr3?
> 
> > +	u64 rbp;
> > +	u64 rsi;
> > +	u64 rdi;
> > +	u64 r8;
> > +	u64 r9;
> > +	u64 r10;
> > +	u64 r11;
> > +	u64 r12;
> > +	u64 r13;
> > +	u64 r14;
> > +	u64 r15;
> > +	u64 rflags;

  reply	other threads:[~2026-05-20  1:30 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-14 21:04 [kvm-unit-tests PATCH v3 00/20] x86: Better backtraces for leaf functions Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 01/20] x86/vmx: Drop unused SYSENTER "support" in nested VMX infrastructure Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 02/20] x86/vmx: Drop unused guest_regs " Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 03/20] x86/svm: Sort (and swap) GPRs by their index, not alphabetically Sean Christopherson
2026-05-21 16:51   ` Yosry Ahmed
2026-05-21 18:42     ` Sean Christopherson
2026-05-21 18:47       ` Yosry Ahmed
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 04/20] x86: Dedup guest/host context switch of registers across SVM and VMX Sean Christopherson
2026-05-19 21:51   ` Mathias Krause
2026-05-20  1:30     ` Sean Christopherson [this message]
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 05/20] x86/virt: Use macro shenanigans to get reg offsets when swapping guest/host regs Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 06/20] x86/virt: Track "guest regs" using per-CPU variable Sean Christopherson
2026-05-19 21:52   ` Mathias Krause
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 07/20] x86/svm: Don't VMLOAD/VMSAVE "guest" state around VMRUN Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 08/20] x86/vmx: Use separate VMCSes for BSP vs. AP in INIT test Sean Christopherson
2026-05-19 21:54   ` Mathias Krause
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 09/20] x86/vmx: Swap GPRs after checking "launched" status Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 10/20] x86/vmx: Track VMCS "launched" state per-CPU Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 11/20] x86/vmx: Track "is this CPU in guest mode" per-CPU Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 12/20] x86/vmx: Communicate hypercalls via RAX, not a global field Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 13/20] x86/vmx: Initialize test stage in SIPI test *before* launching AP thread Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 14/20] x86/kvmclock: Replace spaces with tabs Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 15/20] x86/kvmclock: Skip kvmclock test when not running on KVM with CLOCKSOURCE2 Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 16/20] x86/vmx: Tag "struct vmx_msr_entry" as needing to be 16-byte aligned Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 17/20] x86/smp: Align the stack to a 16-byte boundary when invoking SMP function calls Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 18/20] x86/vmx: Write to KVM's WALL_CLOCK MSR via VM-Entry load list sync in SIPI test Sean Christopherson
2026-05-14 21:04 ` [kvm-unit-tests PATCH v3 19/20] x86: Better backtraces for leaf functions Sean Christopherson
2026-05-14 21:05 ` [kvm-unit-tests PATCH v3 20/20] x86: Prevent realmode test code instrumentation with nop-mcount Sean Christopherson
2026-05-19 22:15 ` [kvm-unit-tests PATCH v3 00/20] x86: Better backtraces for leaf functions Mathias Krause
2026-05-21  9:24 ` Paolo Bonzini
2026-05-21 10:14   ` Paolo Bonzini
2026-05-21 10:35   ` Mathias Krause
2026-05-21 10:48     ` Paolo Bonzini
2026-05-21 12:19       ` Mathias Krause
2026-05-27 15:41         ` Sean Christopherson
2026-05-27 18:25           ` Mathias Krause

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=ag0OmjV_1q9D5QAQ@google.com \
    --to=seanjc@google.com \
    --cc=andrew.jones@linux.dev \
    --cc=kvm@vger.kernel.org \
    --cc=minipli@grsecurity.net \
    --cc=pbonzini@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.