* [PATCH 1/2] nSVM: Add a variant of svm_vmrun() for executing custom guest code
2021-07-15 18:08 [PATCH 0/2] Test: nSVM: Test the effect of guest EFLAGS.TF on VMRUN Krish Sadhukhan
@ 2021-07-15 18:08 ` Krish Sadhukhan
2021-07-15 19:05 ` Sean Christopherson
2021-07-15 18:08 ` [PATCH 2/2] Test: nSVM: Test the effect of guest EFLAGS.TF on VMRUN Krish Sadhukhan
1 sibling, 1 reply; 4+ messages in thread
From: Krish Sadhukhan @ 2021-07-15 18:08 UTC (permalink / raw)
To: kvm; +Cc: pbonzini, jmattson, seanjc, vkuznets, wanpengli, joro
Current implementation of svm_vmrun() and test_run() sets the guest RIP to a
wrapper function which executes the guest code being used by tests. This is
not suitable for tests like testing the effect of guest EFLAGS.TF on VMRUN
because the trap handler will point to the second guest instruction to which
the test code does not have access.
Therefore, add a variant of svm_vmrun() that will set the guest RIP to the
actual guest code that tests want to test. This will be used by the next
patch in this series.
Signed-off-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
---
x86/svm.c | 14 ++++++++++++--
x86/svm.h | 1 +
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/x86/svm.c b/x86/svm.c
index f185ca0..50b6a15 100644
--- a/x86/svm.c
+++ b/x86/svm.c
@@ -227,9 +227,9 @@ struct svm_test *v2_test;
u64 guest_stack[10000];
-int svm_vmrun(void)
+static int _svm_vmrun(u64 rip)
{
- vmcb->save.rip = (ulong)test_thunk;
+ vmcb->save.rip = (ulong)rip;
vmcb->save.rsp = (ulong)(guest_stack + ARRAY_SIZE(guest_stack));
regs.rdi = (ulong)v2_test;
@@ -244,6 +244,16 @@ int svm_vmrun(void)
return (vmcb->control.exit_code);
}
+int svm_vmrun(void)
+{
+ return _svm_vmrun((u64)test_thunk);
+}
+
+int svm_vmrun_custom(u64 rip)
+{
+ return _svm_vmrun(rip);
+}
+
extern u64 *vmrun_rip;
static void test_run(struct svm_test *test)
diff --git a/x86/svm.h b/x86/svm.h
index 995b0f8..3753e2e 100644
--- a/x86/svm.h
+++ b/x86/svm.h
@@ -409,6 +409,7 @@ void vmcb_ident(struct vmcb *vmcb);
struct regs get_regs(void);
void vmmcall(void);
int svm_vmrun(void);
+int svm_vmrun_custom(u64 rip);
void test_set_guest(test_guest_func func);
extern struct vmcb *vmcb;
--
2.27.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/2] Test: nSVM: Test the effect of guest EFLAGS.TF on VMRUN
2021-07-15 18:08 [PATCH 0/2] Test: nSVM: Test the effect of guest EFLAGS.TF on VMRUN Krish Sadhukhan
2021-07-15 18:08 ` [PATCH 1/2] nSVM: Add a variant of svm_vmrun() for executing custom guest code Krish Sadhukhan
@ 2021-07-15 18:08 ` Krish Sadhukhan
1 sibling, 0 replies; 4+ messages in thread
From: Krish Sadhukhan @ 2021-07-15 18:08 UTC (permalink / raw)
To: kvm; +Cc: pbonzini, jmattson, seanjc, vkuznets, wanpengli, joro
According to section "VMRUN and TF/RF Bits in EFLAGS." in APM vol 2,
"When VMRUN loads a guest value of 1 in EFLAGS.TF, that value does not
cause a trace trap between the VMRUN and the first guest instruction,
but rather after completion of the first guest instruction."
Signed-off-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
---
x86/svm_tests.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 61 insertions(+)
diff --git a/x86/svm_tests.c b/x86/svm_tests.c
index a56a197..2d06d9e 100644
--- a/x86/svm_tests.c
+++ b/x86/svm_tests.c
@@ -2491,6 +2491,66 @@ static void test_vmrun_canonicalization(void)
TEST_CANONICAL(vmcb->save.tr.base, "TR");
}
+/*
+ * When VMRUN loads a guest value of 1 in EFLAGS.TF, that value does not
+ * cause a trace trap between the VMRUN and the first guest instruction, but
+ * rather after completion of the first guest instruction.
+ *
+ * [APM vol 2]
+ */
+u64 guest_rflags_test_trap_rip;
+
+static void guest_rflags_test_db_handler(struct ex_regs *r)
+{
+ guest_rflags_test_trap_rip = r->rip;
+ r->rflags &= ~X86_EFLAGS_TF;
+}
+
+extern void guest_rflags_test_guest(struct svm_test *test);
+extern u64 *insn2;
+extern u64 *guest_end;
+
+asm("guest_rflags_test_guest:\n\t"
+ "push %rbp\n\t"
+ ".global insn2\n\t"
+ "insn2:\n\t"
+ "mov %rsp,%rbp\n\t"
+ "vmmcall\n\t"
+ "vmmcall\n\t"
+ ".global guest_end\n\t"
+ "guest_end:\n\t"
+ "vmmcall\n\t"
+ "pop %rbp\n\t"
+ "ret");
+
+static void test_guest_rflags(void)
+{
+ handle_exception(DB_VECTOR, guest_rflags_test_db_handler);
+
+ /*
+ * Trap expected after completion of first guest instruction
+ */
+ vmcb->save.rflags |= X86_EFLAGS_TF;
+ report (svm_vmrun_custom((u64)guest_rflags_test_guest) == SVM_EXIT_VMMCALL &&
+ guest_rflags_test_trap_rip == (u64)&insn2,
+ "Test EFLAGS.TF on VMRUN: trap expected after completion of first guest instruction");
+ /*
+ * No trap expected
+ */
+ guest_rflags_test_trap_rip = 0;
+ vmcb->save.rip += 3;
+ vmcb->save.rflags |= X86_EFLAGS_TF;
+ report (svm_vmrun_custom(vmcb->save.rip) == SVM_EXIT_VMMCALL &&
+ guest_rflags_test_trap_rip == 0, "Test EFLAGS.TF on VMRUN: trap not expected");
+
+ /*
+ * Let guest finish execution
+ */
+ vmcb->save.rip += 3;
+ report (svm_vmrun_custom(vmcb->save.rip) == SVM_EXIT_VMMCALL &&
+ vmcb->save.rip == (u64)&guest_end, "Test EFLAGS.TF on VMRUN: guest execution completion");
+}
+
static void svm_guest_state_test(void)
{
test_set_guest(basic_guest_main);
@@ -2501,6 +2561,7 @@ static void svm_guest_state_test(void)
test_dr();
test_msrpm_iopm_bitmap_addrs();
test_vmrun_canonicalization();
+ test_guest_rflags();
}
static void __svm_npt_rsvd_bits_test(u64 *pxe, u64 rsvd_bits, u64 efer,
--
2.27.0
^ permalink raw reply related [flat|nested] 4+ messages in thread