Skip to contentSkip to conversation

Add L2 exception handling KVM unit tests for nSVM

PATCH Applied Manali Shukla 4 participants 9 messages Dec 29, 2021 – Jan 18, 2022 KVM

lore
Share & tools
lore
#0 lore raw

cover Add L2 exception handling KVM unit tests for nSVM

This series adds 3 KVM Unit tests for nested SVM
1) Check #NM is handled in L2 when L2 #NM handler is registered "fnop" instruction is called in L2 to generate the exception

2) Check #BP is handled in L2 when L2 #BP handler is registered "int3" instruction is called in L2 to generate the exception

3) Check #OF is handled in L2 when L2 #OF handler is registered "into" instruction with instrumented code is used in L2 to generate the exception

Manali Shukla (3):
x86: nSVM: Check #NM exception handling in L2
x86: nSVM: Check #BP exception handling in L2
x86: nSVM: Check #OF exception handling in L2

 x86/svm_tests.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 114 insertions(+)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/3 x86: nSVM: Check #NM exception handling in L2

Add coverage for NM exception handling in L2 when only L2 NM exception handler is registered

Verifies 3 different conditions for which #NM is generated and handled in L2
1) CR0.TS is set - #NM is generated and handled in L2 exception handler
2) CR0.TS is cleared and CR0.EM is set - #NM is generated and handled in L2 exception handler
3) CR0.TS and CR0.EM are cleared - #NM is not generated

---
 x86/svm_tests.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
x86/svm_tests.c +45 -0
index 8ad6122..681272c 100644
@@ -2962,6 +2962,50 @@ static bool vgif_check(struct svm_test *test)
     return get_test_stage(test) == 3;
 }
 
+static int nm_test_counter;
+
+static void guest_test_nm_handler(struct ex_regs *r)
+{
+    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");
+
+    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.TS and CR0.EM unset no #NM excpetion");
+}
 
 struct svm_test svm_tests[] = {
     { "null", default_supported, default_prepare,
@@ -3082,5 +3126,6 @@ struct svm_test svm_tests[] = {
     TEST(svm_vmrun_errata_test),
     TEST(svm_vmload_vmsave),
     TEST(svm_test_singlestep),
+    TEST(svm_nm_test),
     { NULL, NULL, NULL, NULL, NULL, NULL, NULL }
 };
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/3 x86: nSVM: Check #BP exception handling in L2

In reply to Manali Shukla

Add coverage for BP exception handling in L2 when only L2 BP exception handler is registered

BP exception is generated using int3 instruction and it is handled by L2 BP exception handler.

---
 x86/svm_tests.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
x86/svm_tests.c +21 -0
index 681272c..ed67ae1 100644
@@ -2962,6 +2962,26 @@ static bool vgif_check(struct svm_test *test)
     return get_test_stage(test) == 3;
 }
 
+static int bp_test_counter;
+
+static void guest_test_bp_handler(struct ex_regs *r)
+{
+    bp_test_counter++;
+}
+
+static void svm_bp_test_guest(struct svm_test *test)
+{
+    asm volatile("int3");
+}
+
+static void svm_int3_test(void)
+{
+    handle_exception(BP_VECTOR, guest_test_bp_handler);
+    test_set_guest(svm_bp_test_guest);
+    report(svm_vmrun() == SVM_EXIT_VMMCALL && bp_test_counter == 1,
+        "#BP is handled in L2 exception handler");
+}
+
 static int nm_test_counter;
 
 static void guest_test_nm_handler(struct ex_regs *r)
@@ -3127,5 +3147,6 @@ struct svm_test svm_tests[] = {
     TEST(svm_vmload_vmsave),
     TEST(svm_test_singlestep),
     TEST(svm_nm_test),
+    TEST(svm_int3_test),
     { NULL, NULL, NULL, NULL, NULL, NULL, NULL }
 };
Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/3 x86: nSVM: Check #OF exception handling in L2

In reply to Manali Shukla

Add coverage for OF exception handling in L2 when only L2 OF exception handler is registered.

OF exception generated using instrumented code and it is handled by L2 OF exception handler.

---
 x86/svm_tests.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
x86/svm_tests.c +48 -0
index ed67ae1..0707786 100644
@@ -2962,6 +2962,53 @@ static bool vgif_check(struct svm_test *test)
     return get_test_stage(test) == 3;
 }
 
+static int of_test_counter;
+
+static void guest_test_of_handler(struct ex_regs *r)
+{
+    of_test_counter++;
+}
+
+static void svm_of_test_guest(struct svm_test *test)
+{
+    struct far_pointer32 fp = {
+        .offset = (uintptr_t)&&into,
+        .selector = KERNEL_CS32,
+    };
+    uintptr_t rsp;
+
+    asm volatile ("mov %%rsp, %0" : "=r"(rsp));
+
+    if (fp.offset != (uintptr_t)&&into) {
+        printf("Codee address too high.\n");
+        return;
+    }
+
+    if ((u32)rsp != rsp) {
+        printf("Stack address too high.\n");
+    }
+
+    asm goto("lcall *%0" : : "m" (fp) : "rax" : into);
+    return;
+into:
+
+    asm volatile (".code32;"
+            "movl $0x7fffffff, %eax;"
+            "addl %eax, %eax;"
+            "into;"
+            "lret;"
+            ".code64");
+    __builtin_unreachable();
+}
+
+static void svm_into_test(void)
+{
+    handle_exception(OF_VECTOR, guest_test_of_handler);
+    test_set_guest(svm_of_test_guest);
+    report(svm_vmrun() == SVM_EXIT_VMMCALL && of_test_counter == 1,
+        "#OF is generated in L2 exception handler0");
+}
+
 static int bp_test_counter;
 
 static void guest_test_bp_handler(struct ex_regs *r)
@@ -3148,5 +3195,6 @@ struct svm_test svm_tests[] = {
     TEST(svm_test_singlestep),
     TEST(svm_nm_test),
     TEST(svm_int3_test),
+    TEST(svm_into_test),
     { NULL, NULL, NULL, NULL, NULL, NULL, NULL }
 };
Signature · 1 line
2.30.2
#4 lore raw
In reply to Manali Shukla
Quoted 11 lines · Manali Shukla

This series adds 3 KVM Unit tests for nested SVM
1) Check #NM is handled in L2 when L2 #NM handler is registered "fnop" instruction is called in L2 to generate the exception

2) Check #BP is handled in L2 when L2 #BP handler is registered "int3" instruction is called in L2 to generate the exception

3) Check #OF is handled in L2 when L2 #OF handler is registered "into" instruction with instrumented code is used in L2 to generate the exception

Quoted 8 lines

Manali Shukla (3):
x86: nSVM: Check #NM exception handling in L2
x86: nSVM: Check #BP exception handling in L2
x86: nSVM: Check #OF exception handling in L2

 x86/svm_tests.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 114 insertions(+)

A gentle reminder for review

Thank you,
Manali

#5 lore raw
In reply to Manali Shukla

+Aaron, and +Paolo who may or may not subsribe to kvm@ :-)

Quoted 10 lines · Manali Shukla

This series adds 3 KVM Unit tests for nested SVM
1) Check #NM is handled in L2 when L2 #NM handler is registered "fnop" instruction is called in L2 to generate the exception

2) Check #BP is handled in L2 when L2 #BP handler is registered "int3" instruction is called in L2 to generate the exception

3) Check #OF is handled in L2 when L2 #OF handler is registered "into" instruction with instrumented code is used in L2 to generate the exception

This is all basically identical in terms of desired functionality to existing or in-flight nVMX tests, e.g. vmx_nm_test() and Aaron's vmx_exception_test() work[*]. And much of the feedback I provided to Aaron's earlier revisions applies to this series as well, e.g. create a framework to test intercpetion of arbitrary exceptions instead of writing the same boilerplate for each and every test.

It doesn't seem like it'd be _that_ difficult to turn vmx_exception_test into a generic-ish l2_exception_test. To avoid too much scope creep, what if we first get Aaron's code merged, and than attempt to extract the core functionality into a shared library to reuse it for nSVM? If it turns out to be more trouble then its worth, we can always fall back to something like this series.

[*] https://lore.kernel.org/all/[email protected]

#6 lore raw
Quoted 20 lines · Sean Christopherson

+Aaron, and +Paolo who may or may not subsribe to kvm@ :-)

Quoted 10 lines · Manali Shukla

This series adds 3 KVM Unit tests for nested SVM
1) Check #NM is handled in L2 when L2 #NM handler is registered "fnop" instruction is called in L2 to generate the exception

2) Check #BP is handled in L2 when L2 #BP handler is registered "int3" instruction is called in L2 to generate the exception

3) Check #OF is handled in L2 when L2 #OF handler is registered "into" instruction with instrumented code is used in L2 to generate the exception

This is all basically identical in terms of desired functionality to existing or in-flight nVMX tests, e.g. vmx_nm_test() and Aaron's vmx_exception_test() work[*]. And much of the feedback I provided to Aaron's earlier revisions applies to this series as well, e.g. create a framework to test intercpetion of arbitrary exceptions instead of writing the same boilerplate for each and every test.

Quoted 7 lines

It doesn't seem like it'd be _that_ difficult to turn vmx_exception_test into a generic-ish l2_exception_test. To avoid too much scope creep, what if we first get Aaron's code merged, and than attempt to extract the core functionality into a shared library to reuse it for nSVM? If it turns out to be more trouble then its worth, we can always fall back to something like this series.

[*] https://lore.kernel.org/all/[email protected]

This patch has already been queued by Paolo.
I will wait till Aaron's code is merged and than do appropriate changes. I hope this is fine.

Thank you
Manali

maintainer
#7 lore raw
Quoted 11 lines · Manali
Quoted 8 lines

It doesn't seem like it'd be_that_ difficult to turn vmx_exception_test into a generic-ish l2_exception_test. To avoid too much scope creep, what if we first get Aaron's code merged, and than attempt to extract the core functionality into a shared library to reuse it for nSVM? If it turns out to be more trouble then its worth, we can always fall back to something like this series.

[*]https://lore.kernel.org/all/[email protected]

This patch has already been queued by Paolo.
I will wait till Aaron's code is merged and than do appropriate changes. I hope this is fine.

Yes, it is. I am holding on Aaron's series because of the remarks from Sean[1], on the other hand it's really difficult to share code between VMX and SVM tests. So while I agree that a lot of code changes can be done the same way to VMX and SVM tests, it's not really feasible to keep them in sync.

Paolo

[1] https://lore.kernel.org/all/Yd8+n+%[email protected]/#t

maintainer Applied
#8 lore raw
In reply to Manali Shukla
Quoted 11 lines · Manali Shukla

This series adds 3 KVM Unit tests for nested SVM
1) Check #NM is handled in L2 when L2 #NM handler is registered "fnop" instruction is called in L2 to generate the exception

2) Check #BP is handled in L2 when L2 #BP handler is registered "int3" instruction is called in L2 to generate the exception

3) Check #OF is handled in L2 when L2 #OF handler is registered "into" instruction with instrumented code is used in L2 to generate the exception

Quoted 8 lines

Manali Shukla (3):
x86: nSVM: Check #NM exception handling in L2
x86: nSVM: Check #BP exception handling in L2
x86: nSVM: Check #OF exception handling in L2

  x86/svm_tests.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 114 insertions(+)

Queued, thanks.

Paolo