From: Maxim Levitsky <mlevitsk@redhat.com>
To: kvm@vger.kernel.org
Cc: "Andrew Jones" <drjones@redhat.com>,
"Alexandru Elisei" <alexandru.elisei@arm.com>,
"Maxim Levitsky" <mlevitsk@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Claudio Imbrenda" <imbrenda@linux.ibm.com>,
"Thomas Huth" <thuth@redhat.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Nico Boehr" <nrb@linux.ibm.com>,
"Cathy Avery" <cavery@redhat.com>,
"Janosch Frank" <frankja@linux.ibm.com>
Subject: [kvm-unit-tests PATCH v3 19/27] svm: rewerite vm entry macros
Date: Tue, 22 Nov 2022 18:11:44 +0200 [thread overview]
Message-ID: <20221122161152.293072-20-mlevitsk@redhat.com> (raw)
In-Reply-To: <20221122161152.293072-1-mlevitsk@redhat.com>
Make SVM VM entry macros to not use harcode regs label
and also simplify them as much as possible
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
lib/x86/svm_lib.h | 71 +++++++++++++++++++++++++++++++++++++++++++++++
x86/svm.c | 58 ++++++++++++--------------------------
x86/svm.h | 70 ++--------------------------------------------
x86/svm_tests.c | 24 ++++++++++------
4 files changed, 106 insertions(+), 117 deletions(-)
diff --git a/lib/x86/svm_lib.h b/lib/x86/svm_lib.h
index 3bb098dc..f9c2b352 100644
--- a/lib/x86/svm_lib.h
+++ b/lib/x86/svm_lib.h
@@ -66,4 +66,75 @@ u8 *svm_get_io_bitmap(void);
#define MSR_BITMAP_SIZE 8192
+struct svm_gprs {
+ u64 rax;
+ u64 rbx;
+ u64 rcx;
+ u64 rdx;
+ u64 rbp;
+ u64 rsi;
+ u64 rdi;
+ u64 r8;
+ u64 r9;
+ u64 r10;
+ u64 r11;
+ u64 r12;
+ u64 r13;
+ u64 r14;
+ u64 r15;
+ u64 rsp;
+};
+
+#define SWAP_GPRS \
+ "xchg %%rbx, 0x08(%%rax)\n" \
+ "xchg %%rcx, 0x10(%%rax)\n" \
+ "xchg %%rdx, 0x18(%%rax)\n" \
+ "xchg %%rbp, 0x20(%%rax)\n" \
+ "xchg %%rsi, 0x28(%%rax)\n" \
+ "xchg %%rdi, 0x30(%%rax)\n" \
+ "xchg %%r8, 0x38(%%rax)\n" \
+ "xchg %%r9, 0x40(%%rax)\n" \
+ "xchg %%r10, 0x48(%%rax)\n" \
+ "xchg %%r11, 0x50(%%rax)\n" \
+ "xchg %%r12, 0x58(%%rax)\n" \
+ "xchg %%r13, 0x60(%%rax)\n" \
+ "xchg %%r14, 0x68(%%rax)\n" \
+ "xchg %%r15, 0x70(%%rax)\n" \
+ \
+
+
+#define __SVM_VMRUN(vmcb, regs, label) \
+{ \
+ u32 dummy; \
+\
+ (vmcb)->save.rax = (regs)->rax; \
+ (vmcb)->save.rsp = (regs)->rsp; \
+\
+ asm volatile ( \
+ "vmload %%rax\n" \
+ "push %%rbx\n" \
+ "push %%rax\n" \
+ "mov %%rbx, %%rax\n" \
+ SWAP_GPRS \
+ "pop %%rax\n" \
+ ".global " label "\n" \
+ label ": vmrun %%rax\n" \
+ "vmsave %%rax\n" \
+ "pop %%rax\n" \
+ SWAP_GPRS \
+ : "=a"(dummy), \
+ "=b"(dummy) \
+ : "a" (virt_to_phys(vmcb)), \
+ "b"(regs) \
+ /* clobbers*/ \
+ : "memory" \
+ ); \
+\
+ (regs)->rax = (vmcb)->save.rax; \
+ (regs)->rsp = (vmcb)->save.rsp; \
+}
+
+#define SVM_VMRUN(vmcb, regs) \
+ __SVM_VMRUN(vmcb, regs, "vmrun_dummy_label_%=")
+
#endif /* SRC_LIB_X86_SVM_LIB_H_ */
diff --git a/x86/svm.c b/x86/svm.c
index 5e2c3a83..220bce66 100644
--- a/x86/svm.c
+++ b/x86/svm.c
@@ -76,16 +76,13 @@ static void test_thunk(struct svm_test *test)
vmmcall();
}
-struct regs regs;
+static struct svm_gprs regs;
-struct regs get_regs(void)
+struct svm_gprs *get_regs(void)
{
- return regs;
+ return ®s;
}
-// rax handled specially below
-
-
struct svm_test *v2_test;
@@ -94,16 +91,10 @@ u64 guest_stack[10000];
int __svm_vmrun(u64 rip)
{
vmcb->save.rip = (ulong)rip;
- vmcb->save.rsp = (ulong)(guest_stack + ARRAY_SIZE(guest_stack));
+ regs.rsp = (ulong)(guest_stack + ARRAY_SIZE(guest_stack));
regs.rdi = (ulong)v2_test;
- asm volatile (
- ASM_PRE_VMRUN_CMD
- "vmrun %%rax\n\t" \
- ASM_POST_VMRUN_CMD
- :
- : "a" (virt_to_phys(vmcb))
- : "memory", "r15");
+ SVM_VMRUN(vmcb, ®s);
return (vmcb->control.exit_code);
}
@@ -113,43 +104,28 @@ int svm_vmrun(void)
return __svm_vmrun((u64)test_thunk);
}
-extern u8 vmrun_rip;
-
static noinline void test_run(struct svm_test *test)
{
- u64 vmcb_phys = virt_to_phys(vmcb);
-
cli();
vmcb_ident(vmcb);
test->prepare(test);
guest_main = test->guest_func;
vmcb->save.rip = (ulong)test_thunk;
- vmcb->save.rsp = (ulong)(guest_stack + ARRAY_SIZE(guest_stack));
+ regs.rsp = (ulong)(guest_stack + ARRAY_SIZE(guest_stack));
regs.rdi = (ulong)test;
do {
- struct svm_test *the_test = test;
- u64 the_vmcb = vmcb_phys;
- asm volatile (
- "clgi;\n\t" // semi-colon needed for LLVM compatibility
- "sti \n\t"
- "call *%c[PREPARE_GIF_CLEAR](%[test]) \n \t"
- "mov %[vmcb_phys], %%rax \n\t"
- ASM_PRE_VMRUN_CMD
- ".global vmrun_rip\n\t" \
- "vmrun_rip: vmrun %%rax\n\t" \
- ASM_POST_VMRUN_CMD
- "cli \n\t"
- "stgi"
- : // inputs clobbered by the guest:
- "=D" (the_test), // first argument register
- "=b" (the_vmcb) // callee save register!
- : [test] "0" (the_test),
- [vmcb_phys] "1"(the_vmcb),
- [PREPARE_GIF_CLEAR] "i" (offsetof(struct svm_test, prepare_gif_clear))
- : "rax", "rcx", "rdx", "rsi",
- "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15",
- "memory");
+
+ clgi();
+ sti();
+
+ test->prepare_gif_clear(test);
+
+ __SVM_VMRUN(vmcb, ®s, "vmrun_rip");
+
+ cli();
+ stgi();
+
++test->exits;
} while (!test->finished(test));
sti();
diff --git a/x86/svm.h b/x86/svm.h
index a4aabeb2..6f809ce3 100644
--- a/x86/svm.h
+++ b/x86/svm.h
@@ -23,26 +23,6 @@ struct svm_test {
bool on_vcpu_done;
};
-struct regs {
- u64 rax;
- u64 rbx;
- u64 rcx;
- u64 rdx;
- u64 cr2;
- u64 rbp;
- u64 rsi;
- u64 rdi;
- u64 r8;
- u64 r9;
- u64 r10;
- u64 r11;
- u64 r12;
- u64 r13;
- u64 r14;
- u64 r15;
- u64 rflags;
-};
-
typedef void (*test_guest_func)(struct svm_test *);
int run_svm_tests(int ac, char **av, struct svm_test *svm_tests);
@@ -55,58 +35,12 @@ bool default_finished(struct svm_test *test);
int get_test_stage(struct svm_test *test);
void set_test_stage(struct svm_test *test, int s);
void inc_test_stage(struct svm_test *test);
-struct regs get_regs(void);
+struct svm_gprs *get_regs(void);
int __svm_vmrun(u64 rip);
void __svm_bare_vmrun(void);
int svm_vmrun(void);
void test_set_guest(test_guest_func func);
extern struct vmcb *vmcb;
-
-
-#define SAVE_GPR_C \
- "xchg %%rbx, regs+0x8\n\t" \
- "xchg %%rcx, regs+0x10\n\t" \
- "xchg %%rdx, 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 LOAD_GPR_C SAVE_GPR_C
-
-#define ASM_PRE_VMRUN_CMD \
- "vmload %%rax\n\t" \
- "mov regs+0x80, %%r15\n\t" \
- "mov %%r15, 0x170(%%rax)\n\t" \
- "mov regs, %%r15\n\t" \
- "mov %%r15, 0x1f8(%%rax)\n\t" \
- LOAD_GPR_C \
-
-#define ASM_POST_VMRUN_CMD \
- SAVE_GPR_C \
- "mov 0x170(%%rax), %%r15\n\t" \
- "mov %%r15, regs+0x80\n\t" \
- "mov 0x1f8(%%rax), %%r15\n\t" \
- "mov %%r15, regs\n\t" \
- "vmsave %%rax\n\t" \
-
-
-
-#define SVM_BARE_VMRUN \
- asm volatile ( \
- ASM_PRE_VMRUN_CMD \
- "vmrun %%rax\n\t" \
- ASM_POST_VMRUN_CMD \
- : \
- : "a" (virt_to_phys(vmcb)) \
- : "memory", "r15") \
-
+extern struct svm_test svm_tests[];
#endif
diff --git a/x86/svm_tests.c b/x86/svm_tests.c
index 712d24e2..70e41300 100644
--- a/x86/svm_tests.c
+++ b/x86/svm_tests.c
@@ -399,7 +399,7 @@ static bool msr_intercept_finished(struct svm_test *test)
* RCX holds the MSR index.
*/
printf("%s 0x%lx #GP exception\n",
- exit_info_1 ? "WRMSR" : "RDMSR", get_regs().rcx);
+ exit_info_1 ? "WRMSR" : "RDMSR", get_regs()->rcx);
}
/* Jump over RDMSR/WRMSR instruction */
@@ -415,9 +415,9 @@ static bool msr_intercept_finished(struct svm_test *test)
*/
if (exit_info_1)
test->scratch =
- ((get_regs().rdx << 32) | (vmcb->save.rax & 0xffffffff));
+ ((get_regs()->rdx << 32) | (get_regs()->rax & 0xffffffff));
else
- test->scratch = get_regs().rcx;
+ test->scratch = get_regs()->rcx;
return false;
}
@@ -1842,7 +1842,7 @@ static volatile bool host_rflags_set_tf = false;
static volatile bool host_rflags_set_rf = false;
static u64 rip_detected;
-extern u64 *vmrun_rip;
+extern u64 vmrun_rip;
static void host_rflags_db_handler(struct ex_regs *r)
{
@@ -2878,6 +2878,8 @@ static void svm_lbrv_test0(void)
static void svm_lbrv_test1(void)
{
+ struct svm_gprs *regs = get_regs();
+
report(true, "Test that without LBRV enabled, guest LBR state does 'leak' to the host(1)");
vmcb->save.rip = (ulong)svm_lbrv_test_guest1;
@@ -2885,7 +2887,7 @@ static void svm_lbrv_test1(void)
wrmsr(MSR_IA32_DEBUGCTLMSR, DEBUGCTLMSR_LBR);
DO_BRANCH(host_branch1);
- SVM_BARE_VMRUN;
+ SVM_VMRUN(vmcb, regs);
dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
if (vmcb->control.exit_code != SVM_EXIT_VMMCALL) {
@@ -2900,6 +2902,8 @@ static void svm_lbrv_test1(void)
static void svm_lbrv_test2(void)
{
+ struct svm_gprs *regs = get_regs();
+
report(true, "Test that without LBRV enabled, guest LBR state does 'leak' to the host(2)");
vmcb->save.rip = (ulong)svm_lbrv_test_guest2;
@@ -2908,7 +2912,7 @@ static void svm_lbrv_test2(void)
wrmsr(MSR_IA32_DEBUGCTLMSR, DEBUGCTLMSR_LBR);
DO_BRANCH(host_branch2);
wrmsr(MSR_IA32_DEBUGCTLMSR, 0);
- SVM_BARE_VMRUN;
+ SVM_VMRUN(vmcb, regs);
dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
wrmsr(MSR_IA32_DEBUGCTLMSR, 0);
@@ -2924,6 +2928,8 @@ static void svm_lbrv_test2(void)
static void svm_lbrv_nested_test1(void)
{
+ struct svm_gprs *regs = get_regs();
+
if (!lbrv_supported()) {
report_skip("LBRV not supported in the guest");
return;
@@ -2936,7 +2942,7 @@ static void svm_lbrv_nested_test1(void)
wrmsr(MSR_IA32_DEBUGCTLMSR, DEBUGCTLMSR_LBR);
DO_BRANCH(host_branch3);
- SVM_BARE_VMRUN;
+ SVM_VMRUN(vmcb, regs);
dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
wrmsr(MSR_IA32_DEBUGCTLMSR, 0);
@@ -2957,6 +2963,8 @@ static void svm_lbrv_nested_test1(void)
static void svm_lbrv_nested_test2(void)
{
+ struct svm_gprs *regs = get_regs();
+
if (!lbrv_supported()) {
report_skip("LBRV not supported in the guest");
return;
@@ -2972,7 +2980,7 @@ static void svm_lbrv_nested_test2(void)
wrmsr(MSR_IA32_DEBUGCTLMSR, DEBUGCTLMSR_LBR);
DO_BRANCH(host_branch4);
- SVM_BARE_VMRUN;
+ SVM_VMRUN(vmcb, regs);
dbgctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
wrmsr(MSR_IA32_DEBUGCTLMSR, 0);
--
2.34.3
next prev parent reply other threads:[~2022-11-22 16:15 UTC|newest]
Thread overview: 56+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-22 16:11 [kvm-unit-tests PATCH v3 00/27] kvm-unit-tests: set of fixes and new tests Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 01/27] x86: replace irq_{enable|disable}() with sti()/cli() Maxim Levitsky
2022-12-01 13:46 ` Emanuele Giuseppe Esposito
2022-12-06 13:55 ` Maxim Levitsky
2022-12-06 14:15 ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 02/27] x86: introduce sti_nop() and sti_nop_cli() Maxim Levitsky
2022-12-01 13:46 ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 03/27] x86: add few helper functions for apic local timer Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 04/27] svm: remove nop after stgi/clgi Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 05/27] svm: make svm_intr_intercept_mix_if/gif test a bit more robust Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 06/27] svm: use apic_start_timer/apic_stop_timer instead of open coding it Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 07/27] x86: Add test for #SMI during interrupt window Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 08/27] x86: Add a simple test for SYSENTER instruction Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 09/27] svm: add simple nested shutdown test Maxim Levitsky
2022-12-01 13:46 ` Emanuele Giuseppe Esposito
2022-12-06 13:56 ` Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 10/27] SVM: add two tests for exitintinto on exception Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 11/27] lib: Add random number generator Maxim Levitsky
2022-11-23 9:28 ` Claudio Imbrenda
2022-11-23 12:54 ` Andrew Jones
2022-12-06 13:57 ` Maxim Levitsky
2022-12-06 14:07 ` Maxim Levitsky
2022-12-14 10:33 ` Claudio Imbrenda
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 12/27] x86: add IPI stress test Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 13/27] svm: remove get_npt_pte extern Maxim Levitsky
2022-12-01 13:46 ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 14/27] svm: move svm spec definitions to lib/x86/svm.h Maxim Levitsky
2022-12-01 13:54 ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 15/27] svm: move some svm support functions into lib/x86/svm_lib.h Maxim Levitsky
2022-12-01 13:59 ` Emanuele Giuseppe Esposito
2022-12-06 14:10 ` Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 16/27] svm: move setup_svm() to svm_lib.c Maxim Levitsky
2022-12-01 16:14 ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 17/27] svm: correctly skip if NPT not supported Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 18/27] svm: move vmcb_ident to svm_lib.c Maxim Levitsky
2022-12-01 16:18 ` Emanuele Giuseppe Esposito
2022-12-06 14:11 ` Maxim Levitsky
2022-11-22 16:11 ` Maxim Levitsky [this message]
2022-12-02 10:14 ` [kvm-unit-tests PATCH v3 19/27] svm: rewerite vm entry macros Emanuele Giuseppe Esposito
2022-12-06 13:56 ` Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 20/27] svm: move v2 tests run into test_run Maxim Levitsky
2022-12-02 9:53 ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 21/27] svm: cleanup the default_prepare Maxim Levitsky
2022-12-02 9:45 ` Emanuele Giuseppe Esposito
2022-12-06 13:56 ` Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 22/27] svm: introduce svm_vcpu Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 23/27] svm: introduce struct svm_test_context Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 24/27] svm: use svm_test_context in v2 tests Maxim Levitsky
2022-12-02 10:27 ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 25/27] svm: move nested vcpu to test context Maxim Levitsky
2022-12-02 10:22 ` Emanuele Giuseppe Esposito
2022-12-06 14:29 ` Maxim Levitsky
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 26/27] svm: move test_guest_func " Maxim Levitsky
2022-12-02 10:28 ` Emanuele Giuseppe Esposito
2022-11-22 16:11 ` [kvm-unit-tests PATCH v3 27/27] x86: ipi_stress: add optional SVM support Maxim Levitsky
2023-06-07 23:25 ` [kvm-unit-tests PATCH v3 00/27] kvm-unit-tests: set of fixes and new tests 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=20221122161152.293072-20-mlevitsk@redhat.com \
--to=mlevitsk@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=alexandru.elisei@arm.com \
--cc=cavery@redhat.com \
--cc=drjones@redhat.com \
--cc=frankja@linux.ibm.com \
--cc=imbrenda@linux.ibm.com \
--cc=kvm@vger.kernel.org \
--cc=nrb@linux.ibm.com \
--cc=pbonzini@redhat.com \
--cc=thuth@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox