Skip to contentSkip to conversation

KVM: selftests: Add a test for gPAT handling in L2

PATCH Applied Sean Christopherson 3 participants 9 messages May 28 – Jun 5

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

PATCH v4 1/1 KVM: selftests: Add a test for gPAT handling in L2

From: Yosry Ahmed <[email protected]>

When KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled, verify that KVM correctly virtualizes the host PAT MSR and the guest PAT register for nested SVM guests.

With nested NPT disabled:
* L1 and L2 share the same PAT
* The vmcb12.g_pat is ignored

With nested NPT enabled:
* An invalid g_pat in vmcb12 causes VMEXIT_INVALID
* RDMSR(IA32_PAT) from L2 returns the value of the guest PAT register
* WRMSR(IA32_PAT) from L2 is reflected in vmcb12's g_pat on VMEXIT
* RDMSR(IA32_PAT) from L1 returns the value of the host PAT MSR

Verify that save/restore with the vCPU in guest mode behaves as expected in both cases, e.g. preserves both hPAT and gPAT when NPT is enabled.

[sean: use even fancier macro shenanigans]

---

v4:
- Stop trying to use macros, and use macros.
- Identity map TDP after allocating SVM to play nice with upcoming automatic stack allocation/configuration.
- Run the npt=false testcase if NPT is unsupported, instead of skipping the entire test.
- Run multiple iterations and save/restore for npt=false as well.
- Run the variants via the main run macro.
- Name the run macro so it looks like a function.
- Drop printfs leftover from initial development.

v3:
- Fixed multiple VM-Entries test case.
- Fixed typos.

v2:
- Rewrote most of the test to dedup L1 and L2 code, move assertions to L2 where possible to simplify the test, and drop the shared test struct.

v1: https://lore.kernel.org/kvm/[email protected]

 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/x86/svm_nested_pat_test.c   | 195 ++++++++++++++++++
 2 files changed, 196 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_pat_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 2908eca1647a..e0ddd3ff9472 100644
@@ -118,6 +118,7 @@ TEST_GEN_PROGS_x86 += x86/svm_nested_clear_efer_svme
 TEST_GEN_PROGS_x86 += x86/svm_nested_shutdown_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_soft_inject_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_vmcb12_gpa
+TEST_GEN_PROGS_x86 += x86/svm_nested_pat_test
 TEST_GEN_PROGS_x86 += x86/svm_lbr_nested_state
 TEST_GEN_PROGS_x86 += x86/tsc_scaling_sync
 TEST_GEN_PROGS_x86 += x86/sync_regs_test
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +195 -0

195 lines not on this page · raw message

base-commit: d1568b1332b6b3b36b222c2868fc102727c12a34

Signature · 1 line
2.54.0.823.g6e5bcc1fc9-goog
#1 lore raw
Quoted 6 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +6 -0
+int main(int argc, char *argv[])
+{
+       TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
+       TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE));
+       TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
+                    KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);

tools/testing/selftests/kvm/x86/svm_nested_pat_test.c line 195

Quoted 10 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +10 -0
+
+       if (!kvm_cpu_has(X86_FEATURE_NPT))
+               goto skip_npt;
+
+       gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat, npt_enabled = true);
+       gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
+skip_npt:
+       gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);
+       return 0;
+}

The goto is really unnecessary here:

if (kvm_cpu_has(X86_FEATURE_NPT)) {
gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat,

npt_enabled = true);
       gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
}

gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);

author maintainer
#2 lore raw
Quoted 19 lines · Yosry Ahmed
Quoted 6 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +6 -0
+int main(int argc, char *argv[])
+{
+       TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
+       TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE));
+       TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
+                    KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);

tools/testing/selftests/kvm/x86/svm_nested_pat_test.c line 195

Quoted 10 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +10 -0
+
+       if (!kvm_cpu_has(X86_FEATURE_NPT))
+               goto skip_npt;
+
+       gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat, npt_enabled = true);
+       gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
+skip_npt:
+       gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);
+       return 0;
+}

The goto is really unnecessary here:

Quoted 6 lines

if (kvm_cpu_has(X86_FEATURE_NPT)) {
gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat,

npt_enabled = true);
       gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
}

gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);

Well, it's necessary to avoid the indentation.

#3 lore raw
Quoted 21 lines · Sean Christopherson
Quoted 19 lines · Yosry Ahmed
Quoted 6 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +6 -0
+int main(int argc, char *argv[])
+{
+       TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
+       TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE));
+       TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
+                    KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);

tools/testing/selftests/kvm/x86/svm_nested_pat_test.c line 195

Quoted 10 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +10 -0
+
+       if (!kvm_cpu_has(X86_FEATURE_NPT))
+               goto skip_npt;
+
+       gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat, npt_enabled = true);
+       gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
+skip_npt:
+       gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);
+       return 0;
+}

The goto is really unnecessary here:

Quoted 8 lines
Quoted 6 lines

if (kvm_cpu_has(X86_FEATURE_NPT)) {
gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat,

npt_enabled = true);
       gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
}

gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);

Well, it's necessary to avoid the indentation.

Or refactor as:

if (kvm_cpu_has(X86_FEATURE_NPT))

        run_npt_tests();
author maintainer
#4 lore raw
Quoted 29 lines · Jim Mattson
Quoted 28 lines · Sean Christopherson
Quoted 25 lines · Yosry Ahmed
Quoted 6 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +6 -0
+int main(int argc, char *argv[])
+{
+       TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
+       TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE));
+       TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
+                    KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);

tools/testing/selftests/kvm/x86/svm_nested_pat_test.c line 195

Quoted 10 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +10 -0
+
+       if (!kvm_cpu_has(X86_FEATURE_NPT))
+               goto skip_npt;
+
+       gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat, npt_enabled = true);
+       gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
+skip_npt:
+       gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);
+       return 0;
+}

The goto is really unnecessary here:

if (kvm_cpu_has(X86_FEATURE_NPT)) {
gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat,

npt_enabled = true);
       gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
}

gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);

Quoted 6 lines
Quoted 1 line

Well, it's necessary to avoid the indentation.

Or refactor as:

if (kvm_cpu_has(X86_FEATURE_NPT))

        run_npt_tests();

Anyone have a strong preference? I like the goto approach (obviously), so that that all the gpat_test() invocations are prettily aligned. But I can live with an if-statement as Yosry suggested. I think the only option I don't like is moving the NPT tests to a separate helper (it's two lines of code, and testing the NPT case really is the focal point of the test).

#5 lore raw
Quoted 35 lines · Sean Christopherson
Quoted 33 lines · Jim Mattson
Quoted 21 lines · Sean Christopherson
Quoted 19 lines · Yosry Ahmed
Quoted 6 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +6 -0
+int main(int argc, char *argv[])
+{
+       TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
+       TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE));
+       TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
+                    KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);

tools/testing/selftests/kvm/x86/svm_nested_pat_test.c line 195

Quoted 10 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +10 -0
+
+       if (!kvm_cpu_has(X86_FEATURE_NPT))
+               goto skip_npt;
+
+       gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat, npt_enabled = true);
+       gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
+skip_npt:
+       gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);
+       return 0;
+}

The goto is really unnecessary here:

Quoted 8 lines
Quoted 6 lines

if (kvm_cpu_has(X86_FEATURE_NPT)) {
gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat,

npt_enabled = true);
       gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
}

gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);

Well, it's necessary to avoid the indentation.

Or refactor as:

Quoted 8 lines
Quoted 2 lines

if (kvm_cpu_has(X86_FEATURE_NPT))

        run_npt_tests();

Anyone have a strong preference? I like the goto approach (obviously), so that that all the gpat_test() invocations are prettily aligned. But I can live with an if-statement as Yosry suggested. I think the only option I don't like is moving the NPT tests to a separate helper (it's two lines of code, and testing the NPT case really is the focal point of the test).

No strong preference, I obviously also like my suggestion :)

author maintainer
#6 lore raw
Quoted 39 lines · Yosry Ahmed
Quoted 38 lines · Sean Christopherson
Quoted 35 lines · Jim Mattson
Quoted 21 lines · Sean Christopherson
Quoted 19 lines · Yosry Ahmed
Quoted 6 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +6 -0
+int main(int argc, char *argv[])
+{
+       TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
+       TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE));
+       TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
+                    KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);

tools/testing/selftests/kvm/x86/svm_nested_pat_test.c line 195

Quoted 10 lines
tools/testing/selftests/kvm/x86/svm_nested_pat_test.c new file +10 -0
+
+       if (!kvm_cpu_has(X86_FEATURE_NPT))
+               goto skip_npt;
+
+       gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat, npt_enabled = true);
+       gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
+skip_npt:
+       gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);
+       return 0;
+}

The goto is really unnecessary here:

Quoted 8 lines
Quoted 6 lines

if (kvm_cpu_has(X86_FEATURE_NPT)) {
gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat,

npt_enabled = true);
       gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
}

gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);

Well, it's necessary to avoid the indentation.

Or refactor as:

if (kvm_cpu_has(X86_FEATURE_NPT))

        run_npt_tests();
Quoted 7 lines
Quoted 5 lines

Anyone have a strong preference? I like the goto approach (obviously), so that that all the gpat_test() invocations are prettily aligned. But I can live with an if-statement as Yosry suggested. I think the only option I don't like is moving the NPT tests to a separate helper (it's two lines of code, and testing the NPT case really is the focal point of the test).

No strong preference, I obviously also like my suggestion :)

Ok, third option, which I've pushed to kvm-x86/svm (but not kvm-x86/next, yet) in anticipation of overwhelming support:

#define gpat_test(test_name, guest_code, npt_setting) \

do {									\
	npt_setting;							\
									\
	if (npt_enabled && !kvm_cpu_has(X86_FEATURE_NPT)) {		\
		pr_info("Skipping: " test_name " (no NPT support)\n");	\
		break;							\
	}								\
									\
	pr_info("Testing: " test_name "\n");				\
	run_test(guest_code, false, 1);					\
									\
	if (guest_code == l1_guest_code) {				\
		pr_info("Testing: " test_name " Save/Restore\n");	\
		run_test(guest_code, true, 1);				\
									\
		pr_info("Testing: " test_name " Multiple VMRUNs\n");	\
		run_test(guest_code, false, 10);			\
	}								\
} while (0)

int main(int argc, char *argv[])

{
	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
	TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE));
	TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
		     KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);

	gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat, npt_enabled = true);
	gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
	gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);
	return 0;
}

Which (obviously) allows printing out which subtests are being skipped:

# ./x86/svm_nested_pat_test 
Random seed: 0x6b8b4567
Skipping: Invalid gPAT (no NPT support)
Skipping: Nested NPT enabled (no NPT support)
Testing: Nested NPT disabled
Testing: Nested NPT disabled Save/Restore
Testing: Nested NPT disabled Multiple VMRUNs
#7 lore raw
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

Anyone have a strong preference? I like the goto approach (obviously), so that that all the gpat_test() invocations are prettily aligned. But I can live with an if-statement as Yosry suggested. I think the only option I don't like is moving the NPT tests to a separate helper (it's two lines of code, and testing the NPT case really is the focal point of the test).

No strong preference, I obviously also like my suggestion :)

Ok, third option, which I've pushed to kvm-x86/svm (but not kvm-x86/next, yet) in anticipation of overwhelming support:

Looks good, thank you!

Quoted 45 lines
#define gpat_test(test_name, guest_code, npt_setting)                   \
do {                                                                    \
        npt_setting;                                                    \
                                                                        \
        if (npt_enabled && !kvm_cpu_has(X86_FEATURE_NPT)) {             \
                pr_info("Skipping: " test_name " (no NPT support)\n");  \
                break;                                                  \
        }                                                               \
                                                                        \
        pr_info("Testing: " test_name "\n");                            \
        run_test(guest_code, false, 1);                                 \
                                                                        \
        if (guest_code == l1_guest_code) {                              \
                pr_info("Testing: " test_name " Save/Restore\n");       \
                run_test(guest_code, true, 1);                          \
                                                                        \
                pr_info("Testing: " test_name " Multiple VMRUNs\n");    \
                run_test(guest_code, false, 10);                        \
        }                                                               \
} while (0)

int main(int argc, char *argv[])

{
        TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
        TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE));
        TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) &
                     KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT);

        gpat_test("Invalid gPAT", l1_guest_code_invalid_gpat, npt_enabled = true);
        gpat_test("Nested NPT enabled", l1_guest_code, npt_enabled = true);
        gpat_test("Nested NPT disabled", l1_guest_code, npt_enabled = false);
        return 0;
}

Which (obviously) allows printing out which subtests are being skipped:

# ./x86/svm_nested_pat_test
Random seed: 0x6b8b4567
Skipping: Invalid gPAT (no NPT support)
Skipping: Nested NPT enabled (no NPT support)
Testing: Nested NPT disabled
Testing: Nested NPT disabled Save/Restore
Testing: Nested NPT disabled Multiple VMRUNs
author maintainer Applied
#8 lore raw
In reply to Sean Christopherson
Quoted 9 lines · Sean Christopherson

When KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled, verify that KVM correctly virtualizes the host PAT MSR and the guest PAT register for nested SVM guests.

With nested NPT disabled:
* L1 and L2 share the same PAT
* The vmcb12.g_pat is ignored

[...]

Applied to kvm-x86 svm, thanks!

[1/1] KVM: selftests: Add a test for gPAT handling in L2

      https://github.com/kvm-x86/linux/commit/adeb462cecae

--
https://github.com/kvm-x86/linux/tree/next