Skip to contentSkip to conversation

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

PATCH Superseded Yosry Ahmed 1 participant 3 messages May 21

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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

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
* Save/restore with the vCPU in guest mode preserves both hPAT and gPAT

---

v1 is essentially the patch in v7 of Jim's gPAT series [*]

v1 -> 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.

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

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/x86/svm_nested_pat_test.c   | 200 ++++++++++++++++++
 2 files changed, 201 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_pat_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 9118a5a51b89f..d658dc7cd2b14 100644
@@ -117,6 +117,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 +200 -0

200 lines not on this page · raw message

base-commit: 4f256d5770febb9d61f9b57a4c79c491bf4987f1

Signature · 1 line
2.54.0.669.g59709faab0-goog
#1 lore raw

On Thu, May 21, 2026 at 02:34:48AM +0000, Yosry Ahmed wrote:
[..]

Review

Quoted 12 lines
(quoted diff) +12 -0
+
+static void l2_guest_code(void)
+{
+	u64 expected_initial_pat = npt_enabled ? L2_VMCB12_PAT : L1_PAT_VALUE;
+	int i;
+
+	for (i = 0; i < nr_iterations; i++) {
+		GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), expected_initial_pat);
+		GUEST_SYNC(1);
+		GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), expected_initial_pat);
+
+		wrmsr(MSR_IA32_CR_PAT, L2_PAT_MODIFIED);

expected_initial_pat (or more maybe just expected_pat now) should be updated here.

Quoted 28 lines
(quoted diff) +28 -0
+
+		GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L2_PAT_MODIFIED);
+		GUEST_SYNC(2);
+		GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L2_PAT_MODIFIED);
+
+		vmmcall();
+	}
+}
+
+static void l1_guest_code(struct svm_test_data *svm)
+{
+	unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
+	struct vmcb *vmcb = svm->vmcb;
+	int i;
+
+	wrmsr(MSR_IA32_CR_PAT, L1_PAT_VALUE);
+	GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L1_PAT_VALUE);
+
+	generic_svm_setup(svm, l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE]);
+
+	vmcb->save.g_pat = L2_VMCB12_PAT;
+	vmcb->control.intercept &= ~(1ULL << INTERCEPT_MSR_PROT);
+
+	for (i = 0; i < nr_iterations; i++) {
+		run_guest(vmcb, svm->vmcb_gpa);
+
+		GUEST_ASSERT_EQ(vmcb->control.exit_code, SVM_EXIT_VMMCALL);
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+		/*
+		 * If NPT is enabled by L1, L2 has a unique PAT and L1's PAT is
+		 * unchanged. Otherwise, PAT is shared between L1 and L2.
+		 */
+		if (npt_enabled) {
+			GUEST_ASSERT_EQ(vmcb->save.g_pat, L2_PAT_MODIFIED);
+			GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L1_PAT_VALUE);
+		} else {
+			GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L2_PAT_MODIFIED);
+		}

..and we should skip over VMMCALL here.

Quoted 4 lines
(quoted diff) +4 -0
+	}
+
+	GUEST_DONE();
+}

[..]

Quoted 12 lines
(quoted diff) +12 -0
+int main(int argc, char *argv[])
+{
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_NPT));
+	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);
+
+	NESTED_PAT_TEST("Invalid gPAT", l1_guest_code_invalid_gpat,
+			NPT_ENABLED, NO_SAVE_RESTORE, 1);
+
+	NESTED_PAT_TEST("Nested NPT enabled", l1_guest_code,

Review

Quoted 10 lines
(quoted diff) +10 -0
+			NPT_DISABLED, NO_SAVE_RESTORE, 1);
+
+	NESTED_PAT_TEST("Nested NPT enabled", l1_guest_code,
+			NPT_ENABLED, NO_SAVE_RESTORE, 1);
+
+	NESTED_PAT_TEST("Save/Restore", l1_guest_code,
+			NPT_ENABLED, DO_SAVE_RESTORE, 1);
+
+	NESTED_PAT_TEST("Multile VM-Entries", l1_guest_code,
+			NPT_ENABLED, NO_SAVE_RESTORE, 1);

..and I didn't notice any of the above because I am passing in nr_iterations=1 here lol.

This diff fixes it, let me know if you want a v3:

tools/testing/selftests/kvm/x86/svm_nested_pat_test.c +5 -3
index b9301b3e3f4bd..d9d78db847d61 100644
@@ -38,15 +38,16 @@ int nr_iterations;
 
 static void l2_guest_code(void)
 {
-       u64 expected_initial_pat = npt_enabled ? L2_VMCB12_PAT : L1_PAT_VALUE;
+       u64 expected_pat = npt_enabled ? L2_VMCB12_PAT : L1_PAT_VALUE;
        int i;
 
        for (i = 0; i < nr_iterations; i++) {
-               GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), expected_initial_pat);
+               GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), expected_pat);
                GUEST_SYNC(1);
-               GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), expected_initial_pat);
+               GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), expected_pat);
 
                wrmsr(MSR_IA32_CR_PAT, L2_PAT_MODIFIED);
+               expected_pat = L2_PAT_MODIFIED;
 
                GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L2_PAT_MODIFIED);
                GUEST_SYNC(2);
@@ -85,6 +86,7 @@ static void l1_guest_code(struct svm_test_data *svm)
                } else {
                        GUEST_ASSERT_EQ(rdmsr(MSR_IA32_CR_PAT), L2_PAT_MODIFIED);
                }
+               vmcb->save.rip += 3; /* VMMCALL */
        }
 
        GUEST_DONE();
#2 lore raw
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4 -0
+int main(int argc, char *argv[])
+{
+     TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
+     TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_NPT));
Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+     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);
+
+     NESTED_PAT_TEST("Invalid gPAT", l1_guest_code_invalid_gpat,
+                     NPT_ENABLED, NO_SAVE_RESTORE, 1);
+
+     NESTED_PAT_TEST("Nested NPT enabled", l1_guest_code,

..and this name is wrong. RIP.

I will send a new version tomorrow.