kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, Sean Christopherson <seanjc@google.com>,
	Manali Shukla <manali.shukla@amd.com>
Subject: [kvm-unit-tests PATCH v3 8/9] x86: nSVM: Move part of #NM test to exception test framework
Date: Wed,  5 Oct 2022 23:52:11 +0000	[thread overview]
Message-ID: <20221005235212.57836-9-seanjc@google.com> (raw)
In-Reply-To: <20221005235212.57836-1-seanjc@google.com>

From: Manali Shukla <manali.shukla@amd.com>

Remove the boiler plate code for #NM test and move #NM exception test
into the exception test framework.

Keep the test case for the condition where #NM exception is not
generated, but drop the #NM handler entirely and rely on an unexpected
exception being reported as such (the VMMCALL assertion would also fail).

Signed-off-by: Manali Shukla <manali.shukla@amd.com>
Co-developed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 lib/x86/processor.h | 19 +++++++++++++++++++
 x86/svm_tests.c     | 46 +++++++--------------------------------------
 2 files changed, 26 insertions(+), 39 deletions(-)

diff --git a/lib/x86/processor.h b/lib/x86/processor.h
index 5865933..c178998 100644
--- a/lib/x86/processor.h
+++ b/lib/x86/processor.h
@@ -869,6 +869,25 @@ into:
 	__builtin_unreachable();
 }
 
+static inline void fnop(void)
+{
+	asm volatile("fnop");
+}
+
+/* If CR0.TS is set in L2, #NM is generated. */
+static inline void generate_cr0_ts_nm(void)
+{
+	write_cr0((read_cr0() & ~X86_CR0_EM) | X86_CR0_TS);
+	fnop();
+}
+
+/* If CR0.TS is cleared and CR0.EM is set, #NM is generated. */
+static inline void generate_cr0_em_nm(void)
+{
+	write_cr0((read_cr0() & ~X86_CR0_TS) | X86_CR0_EM);
+	fnop();
+}
+
 static inline u8 pmu_version(void)
 {
 	return cpuid(10).a & 0xff;
diff --git a/x86/svm_tests.c b/x86/svm_tests.c
index 0870cc5..27ce47b 100644
--- a/x86/svm_tests.c
+++ b/x86/svm_tests.c
@@ -2756,48 +2756,14 @@ static void pause_filter_test(void)
 	}
 }
 
-static int nm_test_counter;
-
-static void guest_test_nm_handler(struct ex_regs *r)
+/* If CR0.TS and CR0.EM are cleared in L2, no #NM is generated. */
+static void svm_no_nm_test(void)
 {
-	nm_test_counter++;
 	write_cr0(read_cr0() & ~X86_CR0_TS);
-	write_cr0(read_cr0() & ~X86_CR0_EM);
-}
-
-static void svm_nm_test_guest(struct svm_test *test)
-{
-	asm volatile("fnop");
-}
-
-/* This test checks that:
- *
- * (a) If CR0.TS is set in L2, #NM is handled by L2 when
- *     just an L2 handler is registered.
- *
- * (b) If CR0.TS is cleared and CR0.EM is set, #NM is handled
- *     by L2 when just an l2 handler is registered.
- *
- * (c) If CR0.TS and CR0.EM are cleared in L2, no exception
- *     is generated.
- */
-
-static void svm_nm_test(void)
-{
-	handle_exception(NM_VECTOR, guest_test_nm_handler);
-	write_cr0(read_cr0() & ~X86_CR0_TS);
-	test_set_guest(svm_nm_test_guest);
-
-	vmcb->save.cr0 = vmcb->save.cr0 | X86_CR0_TS;
-	report(svm_vmrun() == SVM_EXIT_VMMCALL && nm_test_counter == 1,
-	       "fnop with CR0.TS set in L2, #NM is triggered");
-
-	vmcb->save.cr0 = (vmcb->save.cr0 & ~X86_CR0_TS) | X86_CR0_EM;
-	report(svm_vmrun() == SVM_EXIT_VMMCALL && nm_test_counter == 2,
-	       "fnop with CR0.EM set in L2, #NM is triggered");
+	test_set_guest((test_guest_func)fnop);
 
 	vmcb->save.cr0 = vmcb->save.cr0 & ~(X86_CR0_TS | X86_CR0_EM);
-	report(svm_vmrun() == SVM_EXIT_VMMCALL && nm_test_counter == 2,
+	report(svm_vmrun() == SVM_EXIT_VMMCALL,
 	       "fnop with CR0.TS and CR0.EM unset no #NM excpetion");
 }
 
@@ -3247,6 +3213,8 @@ struct svm_exception_test svm_exception_tests[] = {
 	{ BP_VECTOR, generate_bp },
 	{ AC_VECTOR, svm_l2_ac_test },
 	{ OF_VECTOR, generate_of },
+	{ NM_VECTOR, generate_cr0_ts_nm },
+	{ NM_VECTOR, generate_cr0_em_nm },
 };
 
 static u8 svm_exception_test_vector;
@@ -3396,7 +3364,7 @@ struct svm_test svm_tests[] = {
 	TEST(svm_vmrun_errata_test),
 	TEST(svm_vmload_vmsave),
 	TEST(svm_test_singlestep),
-	TEST(svm_nm_test),
+	TEST(svm_no_nm_test),
 	TEST(svm_exception_test),
 	TEST(svm_lbrv_test0),
 	TEST(svm_lbrv_test1),
-- 
2.38.0.rc1.362.ged0d419d3c-goog


  parent reply	other threads:[~2022-10-05 23:52 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-05 23:52 [kvm-unit-tests PATCH v3 0/9] x86: n{VMX,SVM} exception tests Sean Christopherson
2022-10-05 23:52 ` [kvm-unit-tests PATCH v3 1/9] nVMX: Add "nop" after setting EFLAGS.TF to guarantee single-step #DB Sean Christopherson
2022-10-05 23:52 ` [kvm-unit-tests PATCH v3 2/9] x86: Move helpers to generate misc exceptions to processor.h Sean Christopherson
2022-10-05 23:52 ` [kvm-unit-tests PATCH v3 3/9] nVMX: Move #OF test to generic exceptions test Sean Christopherson
2022-10-05 23:52 ` [kvm-unit-tests PATCH v3 4/9] nVMX: Drop one-off INT3=>#BP test Sean Christopherson
2022-10-05 23:52 ` [kvm-unit-tests PATCH v3 5/9] x86: nSVM: Add an exception test framework and tests Sean Christopherson
2022-10-05 23:52 ` [kvm-unit-tests PATCH v3 6/9] x86: nSVM: Move #BP test to exception test framework Sean Christopherson
2022-10-05 23:52 ` [kvm-unit-tests PATCH v3 7/9] x86: nSVM: Move #OF " Sean Christopherson
2022-10-05 23:52 ` Sean Christopherson [this message]
2022-10-05 23:52 ` [kvm-unit-tests PATCH v3 9/9] nVMX: Move #NM test to generic " Sean Christopherson
2022-10-12  4:17 ` [kvm-unit-tests PATCH v3 0/9] x86: n{VMX,SVM} exception tests Manali Shukla

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=20221005235212.57836-9-seanjc@google.com \
    --to=seanjc@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=manali.shukla@amd.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).