From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, Sean Christopherson <seanjc@google.com>,
Mathias Krause <minipli@grsecurity.net>,
Andrew Jones <andrew.jones@linux.dev>
Subject: [kvm-unit-tests PATCH v3 05/20] x86/virt: Use macro shenanigans to get reg offsets when swapping guest/host regs
Date: Thu, 14 May 2026 14:04:45 -0700 [thread overview]
Message-ID: <20260514210500.1626871-6-seanjc@google.com> (raw)
In-Reply-To: <20260514210500.1626871-1-seanjc@google.com>
Replace the hand-coded literal offsets in the guest/host assembly code
with programmatically generated offsets to make the code more readable and
easier to maintain. E.g. this will make it much, much easier to make
the guest register structure per-CPU.
To workaround offsetof() being resolved at compile-time, i.e. not by the
preprocessor, provide a macro to define the immediate constraints for
inline assembly.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
lib/x86/virt.h | 62 ++++++++++++++++++++++++++++++++++++--------------
x86/svm.c | 5 ++--
x86/svm.h | 11 +++++----
x86/vmx.c | 3 ++-
4 files changed, 56 insertions(+), 25 deletions(-)
diff --git a/lib/x86/virt.h b/lib/x86/virt.h
index ccc90c25..1066390d 100644
--- a/lib/x86/virt.h
+++ b/lib/x86/virt.h
@@ -29,24 +29,52 @@ struct guest_regs {
extern struct guest_regs regs;
-#define __SWAP_GPRS \
- "xchg %%rcx, regs+0x8\n\t" \
- "xchg %%rdx, regs+0x10\n\t" \
- "xchg %%rbx, regs+0x18\n\t" \
- "xchg %%rbp, regs+0x28\n\t" \
- "xchg %%rsi, regs+0x30\n\t" \
- "xchg %%rdi, regs+0x38\n\t" \
- "xchg %%r8, regs+0x40\n\t" \
- "xchg %%r9, regs+0x48\n\t" \
- "xchg %%r10, regs+0x50\n\t" \
- "xchg %%r11, regs+0x58\n\t" \
- "xchg %%r12, regs+0x60\n\t" \
- "xchg %%r13, regs+0x68\n\t" \
- "xchg %%r14, regs+0x70\n\t" \
- "xchg %%r15, regs+0x78\n\t"
+#define GUEST_REG_OFFSET(name) \
+ [off_##name] "i" (offsetof(struct guest_regs, name))
-#define SWAP_GPRS \
- "xchg %%rax, regs+0x0\n\t" \
+#define GUEST_REGS_OFFSETS \
+ GUEST_REG_OFFSET(rax), \
+ GUEST_REG_OFFSET(rcx), \
+ GUEST_REG_OFFSET(rdx), \
+ GUEST_REG_OFFSET(rbx), \
+ GUEST_REG_OFFSET(cr2), \
+ GUEST_REG_OFFSET(rbp), \
+ GUEST_REG_OFFSET(rsi), \
+ GUEST_REG_OFFSET(rdi), \
+ GUEST_REG_OFFSET(r8), \
+ GUEST_REG_OFFSET(r9), \
+ GUEST_REG_OFFSET(r10), \
+ GUEST_REG_OFFSET(r11), \
+ GUEST_REG_OFFSET(r12), \
+ GUEST_REG_OFFSET(r13), \
+ GUEST_REG_OFFSET(r14), \
+ GUEST_REG_OFFSET(r15), \
+ GUEST_REG_OFFSET(rflags)
+
+#define GUEST_REG(name) \
+ xxstr(regs+%c[off_##name])
+
+#define SWAP_REG(name) \
+ "xchg %%" xxstr(name) "," GUEST_REG(name) "\n\t"
+
+#define __SWAP_GPRS \
+ SWAP_REG(rcx) \
+ SWAP_REG(rdx) \
+ SWAP_REG(rbx) \
+ SWAP_REG(rbp) \
+ SWAP_REG(rsi) \
+ SWAP_REG(rdi) \
+ SWAP_REG(r8) \
+ SWAP_REG(r9) \
+ SWAP_REG(r10) \
+ SWAP_REG(r11) \
+ SWAP_REG(r12) \
+ SWAP_REG(r13) \
+ SWAP_REG(r14) \
+ SWAP_REG(r15)
+
+#define SWAP_GPRS \
+ SWAP_REG(rax) \
__SWAP_GPRS
#endif /* _x86_VIRT_H_ */
\ No newline at end of file
diff --git a/x86/svm.c b/x86/svm.c
index 893b3f49..1762cadb 100644
--- a/x86/svm.c
+++ b/x86/svm.c
@@ -254,7 +254,7 @@ u64 __svm_vmrun(u64 rip)
"vmrun %%rax\n\t" \
ASM_POST_VMRUN_CMD
:
- : "a" (virt_to_phys(vmcb))
+ : GUEST_REGS_OFFSETS, "a" (virt_to_phys(vmcb))
: "memory", "r15");
return (vmcb->control.exit_code);
@@ -296,7 +296,8 @@ static noinline void test_run(struct svm_test *test)
: // inputs clobbered by the guest:
"=D" (the_test), // first argument register
"=b" (the_vmcb) // callee save register!
- : [test] "0" (the_test),
+ : GUEST_REGS_OFFSETS,
+ [test] "0" (the_test),
[vmcb_phys] "1"(the_vmcb),
[PREPARE_GIF_CLEAR] "i" (offsetof(struct svm_test, prepare_gif_clear))
: "rax", "rcx", "rdx", "rsi",
diff --git a/x86/svm.h b/x86/svm.h
index a9e15f67..67a1cddd 100644
--- a/x86/svm.h
+++ b/x86/svm.h
@@ -437,18 +437,18 @@ static inline void clgi(void)
#define ASM_PRE_VMRUN_CMD \
"vmload %%rax\n\t" \
- "mov regs+0x80, %%r15\n\t" \
+ "mov " GUEST_REG(rflags) ", %%r15\n\t" \
"mov %%r15, 0x170(%%rax)\n\t" \
- "mov regs, %%r15\n\t" \
+ "mov " GUEST_REG(rax) ", %%r15\n\t" \
"mov %%r15, 0x1f8(%%rax)\n\t" \
__SWAP_GPRS \
#define ASM_POST_VMRUN_CMD \
__SWAP_GPRS \
"mov 0x170(%%rax), %%r15\n\t" \
- "mov %%r15, regs+0x80\n\t" \
+ "mov %%r15, " GUEST_REG(rflags) "\n\t" \
"mov 0x1f8(%%rax), %%r15\n\t" \
- "mov %%r15, regs\n\t" \
+ "mov %%r15, " GUEST_REG(rax)"\n\t" \
"vmsave %%rax\n\t" \
@@ -459,7 +459,8 @@ static inline void clgi(void)
"vmrun %%rax\n\t" \
ASM_POST_VMRUN_CMD \
: \
- : "a" (virt_to_phys(vmcb)) \
+ : GUEST_REGS_OFFSETS, \
+ "a" (virt_to_phys(vmcb)) \
: "memory", "r15") \
#endif
diff --git a/x86/vmx.c b/x86/vmx.c
index 603730c2..8a38ae8a 100644
--- a/x86/vmx.c
+++ b/x86/vmx.c
@@ -1751,7 +1751,8 @@ static noinline void vmx_enter_guest(struct vmentry_result *result)
"3: \n\t"
: [vm_fail]"+m"(result->vm_fail),
[vm_fail_flags]"=m"(result->flags)
- : [launched]"m"(launched), [HOST_RSP]"i"(HOST_RSP)
+ : [launched]"m"(launched), [HOST_RSP]"i"(HOST_RSP),
+ GUEST_REGS_OFFSETS
: "rdi", "memory", "cc"
);
in_guest = 0;
--
2.54.0.563.g4f69b47b94-goog
next prev parent reply other threads:[~2026-05-14 21:05 UTC|newest]
Thread overview: 21+ 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-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-14 21:04 ` Sean Christopherson [this message]
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-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-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
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=20260514210500.1626871-6-seanjc@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.