mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Yosry Ahmed <yosry@kernel.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 03/10] KVM: selftests: Use an array for guest_regs (and fix offsets)
Date: Fri, 24 Jul 2026 08:33:46 -0700	[thread overview]
Message-ID: <amOF2p2tTjOXcRS_@google.com> (raw)
In-Reply-To: <20260629183746.699840-4-yosry@kernel.org>

On Mon, Jun 29, 2026, Yosry Ahmed wrote:
> The assembly code defined by SAVE_GPR_C uses the wrong offsets for some
> registers in guest_regs. For example, the offset of RCX should 0x08 not
> 0x10. Also, the last offset in the struct (R15) is 0x78, not 0x80, so
> the code actually saves and restore beyond the end of gpr64_regs.
> 
> Eliminate hardcoded offsets by using an array instead of a struct
> (similar to KVM's per-vCPU regs), and use the array index to generate
> the offset. While at it, rename SAVE_GPR_C and LOAD_GPR_C to a single
> macro, SVM_SWITCH_GPRS_ASM.
> 
> Signed-off-by: Yosry Ahmed <yosry@kernel.org>
> ---
>  .../selftests/kvm/include/x86/processor.h     | 36 +++++++-------
>  tools/testing/selftests/kvm/lib/x86/svm.c     | 47 ++++++++++---------
>  2 files changed, 41 insertions(+), 42 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
> index 7d3a27bc0d842..535f26e077570 100644
> --- a/tools/testing/selftests/kvm/include/x86/processor.h
> +++ b/tools/testing/selftests/kvm/include/x86/processor.h
> @@ -396,25 +396,23 @@ static inline unsigned int x86_model(unsigned int eax)
>  #define PTE_GET_PA(pte)		((pte) & PHYSICAL_PAGE_MASK)
>  #define PTE_GET_PFN(pte)        (PTE_GET_PA(pte) >> PAGE_SHIFT)
>  
> -/* General Registers in 64-Bit Mode */
> -struct gpr64_regs {
> -	u64 rax;
> -	u64 rcx;
> -	u64 rdx;
> -	u64 rbx;
> -	u64 rsp;
> -	u64 rbp;
> -	u64 rsi;
> -	u64 rdi;
> -	u64 r8;
> -	u64 r9;
> -	u64 r10;
> -	u64 r11;
> -	u64 r12;
> -	u64 r13;
> -	u64 r14;
> -	u64 r15;
> -};
> +#define GUEST_REGS_RAX	0
> +#define GUEST_REGS_RCX	1
> +#define GUEST_REGS_RDX	2
> +#define GUEST_REGS_RBX	3
> +#define GUEST_REGS_RSP	4
> +#define GUEST_REGS_RBP	5
> +#define GUEST_REGS_RSI	6
> +#define GUEST_REGS_RDI	7
> +#define GUEST_REGS_R8	8
> +#define GUEST_REGS_R9	9
> +#define GUEST_REGS_R10	10
> +#define GUEST_REGS_R11	11
> +#define GUEST_REGS_R12	12
> +#define GUEST_REGS_R13	13
> +#define GUEST_REGS_R14	14
> +#define GUEST_REGS_R15	15
> +#define NR_GUEST_REGS	(GUEST_REGS_R15 + 1)

Eh, these aren't guest specific, they're architectural x86 values.  Ideally,
KVM's own VCPU_REGS_xxx would be X86_REGS_xxx, which is possible now that KVM
doesn't do pretend RIP is a GPR.

I also prefer arch/x86/include/asm/kvm_host.h's approach of using an enum, e.g.

enum x86_general_purpose_reg {
	X86_REGS_RAX,
	X86_REGS_RCX,
	X86_REGS_RDX,
	X86_REGS_RBX,
	X86_REGS_RSP,
	X86_REGS_RBP,
	X86_REGS_RSI,
	X86_REGS_RDI,
	X86_REGS_R8,
	X86_REGS_R9,
	X86_REGS_R10,
	X86_REGS_R11,
	X86_REGS_R12,
	X86_REGS_R13,
	X86_REGS_R14,
	X86_REGS_R15,
	NR_X86_GENERAL_PURPOSE_REGS,
};

And I think it makes sense to squeeze this in with the other architectural
definitions, e.g.

diff --git tools/testing/selftests/kvm/include/x86/processor.h tools/testing/selftests/kvm/include/x86/processor.h
index 7d3a27bc0d84..bbb7c71bb768 100644
--- tools/testing/selftests/kvm/include/x86/processor.h
+++ tools/testing/selftests/kvm/include/x86/processor.h
@@ -57,6 +57,26 @@ const char *ex_str(int vector);
 #define X86_EFLAGS_VIP         BIT(20) /* Virtual Interrupt Pending */
 #define X86_EFLAGS_ID          BIT(21) /* CPUID detection */
 
+enum x86_general_purpose_reg {
+       X86_REGS_RAX,
+       VCPU_REGS_RCX,
+       VCPU_REGS_RDX,
+       VCPU_REGS_RBX,
+       VCPU_REGS_RSP,
+       VCPU_REGS_RBP,
+       VCPU_REGS_RSI,
+       VCPU_REGS_RDI,
+       VCPU_REGS_R8,
+       VCPU_REGS_R9,
+       VCPU_REGS_R10,
+       VCPU_REGS_R11,
+       VCPU_REGS_R12,
+       VCPU_REGS_R13,
+       VCPU_REGS_R14,
+       VCPU_REGS_R15,
+       NR_X86_GENERAL_PURPOSE_REGS,
+};
+
 #define X86_CR4_VME            (1ul << 0)
 #define X86_CR4_PVI            (1ul << 1)
 #define X86_CR4_TSD            (1ul << 2)

  reply	other threads:[~2026-07-24 15:33 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-29 18:37 [PATCH v3 00/10] KVM: selftests: Stress save+restore and #PF (ft. nested) Yosry Ahmed
2026-06-29 18:37 ` [PATCH v3 01/10] KVM: selftests: Move STR() and XSTR() definitions to test_util.h Yosry Ahmed
2026-07-24 15:26   ` Sean Christopherson
2026-06-29 18:37 ` [PATCH v3 02/10] KVM: selftests: Fix RAX and RFLAGS VMCB offsets when running L2 Yosry Ahmed
2026-06-29 18:37 ` [PATCH v3 03/10] KVM: selftests: Use an array for guest_regs (and fix offsets) Yosry Ahmed
2026-07-24 15:33   ` Sean Christopherson [this message]
2026-07-24 16:18     ` Yosry Ahmed
2026-07-24 17:03       ` Sean Christopherson
2026-07-24 17:44         ` Yosry Ahmed
2026-06-29 18:37 ` [PATCH v3 04/10] KVM: selftests: Move GPR load/save definitions outside of nSVM code Yosry Ahmed
2026-06-29 18:37 ` [PATCH v3 05/10] KVM: selftests: Reuse GPR switching logic for nVMX Yosry Ahmed
2026-06-29 18:37 ` [PATCH v3 06/10] KVM: selftests: Drop HORRIFIC_L2_UCALL_CLOBBER_HACK Yosry Ahmed
2026-07-24 15:37   ` Sean Christopherson
2026-07-24 16:20     ` Yosry Ahmed
2026-06-29 18:37 ` [PATCH v3 07/10] KVM: selftests: Add basic stress test for save+restore and #PF handling Yosry Ahmed
2026-07-24 16:45   ` Sean Christopherson
2026-07-24 17:38     ` Yosry Ahmed
2026-07-24 18:12       ` Sean Christopherson
2026-07-24 18:25         ` Yosry Ahmed
2026-06-29 18:37 ` [PATCH v3 08/10] KVM: selftests: Trigger save+restore randomly in the #PF stress test Yosry Ahmed
2026-06-29 18:37 ` [PATCH v3 09/10] KVM: selftests: Support running stress save+restore and #PF test in L2 Yosry Ahmed
2026-07-24 17:37   ` Sean Christopherson
2026-07-24 17:40     ` Yosry Ahmed
2026-06-29 18:37 ` [PATCH v3 10/10] KVM: selftests: Trigger L2->L1 exits stress save+restore and #PF test Yosry Ahmed
2026-07-24 17:50   ` Sean Christopherson
2026-07-24 18:15     ` Yosry Ahmed
2026-07-24 18:25       ` Sean Christopherson
2026-07-24 18:35         ` Yosry Ahmed
2026-07-24 20:32           ` Sean Christopherson

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=amOF2p2tTjOXcRS_@google.com \
    --to=seanjc@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=yosry@kernel.org \
    /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

Powered by JetHome