Skip to contentSkip to conversation

Extend test coverage for nested SVM

PATCH In review Yosry Ahmed 3 participants 49 messages Oct 21, 2025 – Nov 21, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover Extend test coverage for nested SVM

There are multiple selftests exercising nested VMX that are not specific to VMX (at least not anymore). Extend their coverage to nested SVM.

This version is significantly different (and longer) than v1 [1], mainly due to the change of direction to reuse __virt_pg_map() for nested EPT/NPT mappings instead of extending the existing nested EPT infrastructure. It also has a lot more fixups and cleanups.

This series depends on two other series:
- "KVM: SVM: GIF and EFER.SVME are independent" [2]
- "KVM: selftests: Add test of SET_NESTED_STATE with 48-bit L2 on 57-bit L1" [3]

The dependency on the former is because set_nested_state_test is now also a regression test for that fix. The dependency on the latter is purely to avoid conflicts.

The patch ordering is not perfect, I did some cleanups toward the end that arguably should have been moved to the beginning, but I had to stop rebasing and send the patches out at some point:

Block #1 (patch 1 to patch 7):
- Direct successors to the first 6 patches in v1, addressing review comments from Jim and collecting his review tags. These patch extend 5 of the nVMX tests to cover nSVM.

Block #2 (patch 8 to patch 11):
- Miscellaneous fixups and cleanups.

Block #3 (patch 11 to patch 17):
- Moving nested EPT mapping functions to use __virt_pg_map(), patches 11 to 15 do the prep work, and patch 16 does the switch. Patch 17 is a minor cleanup on top (which arguably fits better in block #2).

Block #4 (patch 18 to 23):
- Patches 18 to 22 are prep work to generalize the nested EPT mapping code to work with nested NPT, and patch 23 finally extends the nested dirty logging test to work with nSVM using the nested NPT infrastructure. Patch 19 is admittedly an imposter in this block and should have been in block #2.

[1]https://lore.kernel.org/kvm/[email protected]/ [2]https://lore.kernel.org/kvm/[email protected]/ [3]https://lore.kernel.org/kvm/[email protected]/

Yosry Ahmed (23):
KVM: selftests: Minor improvements to asserts in
test_vmx_nested_state()
KVM: selftests: Extend vmx_set_nested_state_test to cover SVM
KVM: selftests: Extend vmx_close_while_nested_test to cover SVM
KVM: selftests: Extend vmx_nested_tsc_scaling_test to cover SVM
KVM: selftests: Move nested invalid CR3 check to its own test
KVM: selftests: Extend nested_invalid_cr3_test to cover SVM
KVM: selftests: Extend vmx_tsc_adjust_test to cover SVM
KVM: selftests: Stop hardcoding PAGE_SIZE in x86 selftests
KVM: selftests: Remove the unused argument to prepare_eptp()
KVM: selftests: Stop using __virt_pg_map() directly in tests
KVM: selftests: Make sure vm->vpages_mapped is always up-to-date
KVM: selftests: Parameterize the PTE bitmasks for virt mapping
functions
KVM: selftests: Pass the root GPA into virt_get_pte()
KVM: selftests: Pass the root GPA into __virt_pg_map()
KVM: selftests: Stop setting AD bits on nested EPTs on creation
KVM: selftests: Use __virt_pg_map() for nested EPTs
KVM: selftests: Kill eptPageTablePointer
KVM: selftests: Generalize nested mapping functions
KVM: selftests: Move nested MMU mapping functions outside of vmx.c
KVM: selftests: Stop passing a memslot to nested_map_memslot()
KVM: selftests: Allow kvm_cpu_has_ept() to be called on AMD CPUs
KVM: selftests: Set the user bit on nested MMU PTEs
KVM: selftests: Extend vmx_dirty_log_test to cover SVM

 tools/testing/selftests/kvm/Makefile.kvm      |  11 +-
 .../testing/selftests/kvm/include/kvm_util.h  |   1 +
 .../selftests/kvm/include/x86/processor.h     |  34 ++-
 .../selftests/kvm/include/x86/svm_util.h      |   8 +
 tools/testing/selftests/kvm/include/x86/vmx.h |  15 +-
 tools/testing/selftests/kvm/lib/kvm_util.c    |   3 -
 .../testing/selftests/kvm/lib/x86/memstress.c |   6 +-
 .../testing/selftests/kvm/lib/x86/processor.c | 184 +++++++++++---
 tools/testing/selftests/kvm/lib/x86/svm.c     |  19 ++
 tools/testing/selftests/kvm/lib/x86/vmx.c     | 232 +++---------------
 tools/testing/selftests/kvm/mmu_stress_test.c |   6 +-
 ...ested_test.c => close_while_nested_test.c} |  42 +++-
 .../selftests/kvm/x86/hyperv_features.c       |   2 +-
 tools/testing/selftests/kvm/x86/hyperv_ipi.c  |  18 +-
 .../selftests/kvm/x86/hyperv_tlb_flush.c      |   2 +-
 ...rty_log_test.c => nested_dirty_log_test.c} | 102 +++++---
 .../kvm/x86/nested_invalid_cr3_test.c         | 118 +++++++++
 ...adjust_test.c => nested_tsc_adjust_test.c} |  79 +++---
 ...aling_test.c => nested_tsc_scaling_test.c} |  48 +++-
 ...d_state_test.c => set_nested_state_test.c} | 135 +++++++++-
 .../selftests/kvm/x86/sev_smoke_test.c        |   2 +-
 tools/testing/selftests/kvm/x86/state_test.c  |   2 +-
 .../selftests/kvm/x86/userspace_io_test.c     |   2 +-
 23 files changed, 695 insertions(+), 376 deletions(-)
 rename tools/testing/selftests/kvm/x86/{vmx_close_while_nested_test.c => close_while_nested_test.c} (64%)
 rename tools/testing/selftests/kvm/x86/{vmx_dirty_log_test.c => nested_dirty_log_test.c} (57%)
 create mode 100644 tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c
 rename tools/testing/selftests/kvm/x86/{vmx_tsc_adjust_test.c => nested_tsc_adjust_test.c} (61%)
 rename tools/testing/selftests/kvm/x86/{vmx_nested_tsc_scaling_test.c => nested_tsc_scaling_test.c} (83%)
 rename tools/testing/selftests/kvm/x86/{vmx_set_nested_state_test.c => set_nested_state_test.c} (67%)
Signature · 1 line
2.51.0.869.ge66316f041-goog
#1 lore raw

PATCH v2 1/23 KVM: selftests: Minor improvements to asserts in test_vmx_nested_state()

Display the address as hex if the asserts for the vmxon_pa and vmcs12_pa fail, and assert that the flags are 0 as expected.

---
 .../selftests/kvm/x86/vmx_set_nested_state_test.c      | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c +8 -2
index 67a62a5a88951..c4c400d2824c1 100644
@@ -241,8 +241,14 @@ void test_vmx_nested_state(struct kvm_vcpu *vcpu)
 	TEST_ASSERT(state->size >= sizeof(*state) && state->size <= state_sz,
 		    "Size must be between %ld and %d.  The size returned was %d.",
 		    sizeof(*state), state_sz, state->size);
-	TEST_ASSERT(state->hdr.vmx.vmxon_pa == -1ull, "vmxon_pa must be -1ull.");
-	TEST_ASSERT(state->hdr.vmx.vmcs12_pa == -1ull, "vmcs_pa must be -1ull.");
+	TEST_ASSERT(state->hdr.vmx.vmxon_pa == -1ull,
+		    "vmxon_pa must be 0x%llx, but was 0x%llx",
+		    -1ull, state->hdr.vmx.vmxon_pa);
+	TEST_ASSERT(state->hdr.vmx.vmcs12_pa == -1ull,
+		    "vmcs12_pa must be 0x%llx, but was 0x%llx",
+		    -1llu, state->hdr.vmx.vmcs12_pa);
+	TEST_ASSERT(state->flags == 0,
+		    "Flags must be equal to 0, but was 0x%hx", state->flags);
 
 	free(state);
 }
Signature · 1 line
2.51.0.869.ge66316f041-goog
#2 lore raw

Re: [PATCH v2 01/23] KVM: selftests: Minor improvements to asserts in test_vmx_nested_state()

Quoted 17 lines · Yosry Ahmed

Display the address as hex if the asserts for the vmxon_pa and vmcs12_pa fail, and assert that the flags are 0 as expected.

---
 .../selftests/kvm/x86/vmx_set_nested_state_test.c      | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c +0 -0
index 67a62a5a88951..c4c400d2824c1 100644
@@ -241,8 +241,14 @@ void test_vmx_nested_state(struct kvm_vcpu *vcpu)
 	TEST_ASSERT(state->size >= sizeof(*state) && state->size <= state_sz,
 		    "Size must be between %ld and %d.  The size returned was %d.",
 		    sizeof(*state), state_sz, state->size);

tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c void test_vmx_nested_state(struct kvm_vcpu *vcpu) · line 251

Quoted 10 lines
tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c +8 -2
-	TEST_ASSERT(state->hdr.vmx.vmxon_pa == -1ull, "vmxon_pa must be -1ull.");
-	TEST_ASSERT(state->hdr.vmx.vmcs12_pa == -1ull, "vmcs_pa must be -1ull.");
+	TEST_ASSERT(state->hdr.vmx.vmxon_pa == -1ull,
+		    "vmxon_pa must be 0x%llx, but was 0x%llx",
+		    -1ull, state->hdr.vmx.vmxon_pa);
+	TEST_ASSERT(state->hdr.vmx.vmcs12_pa == -1ull,
+		    "vmcs12_pa must be 0x%llx, but was 0x%llx",
+		    -1llu, state->hdr.vmx.vmcs12_pa);
+	TEST_ASSERT(state->flags == 0,
+		    "Flags must be equal to 0, but was 0x%hx", state->flags);

The error messages aren't adding a whole lot, why not just use TEST_ASSERT_EQ()?

#3 lore raw

Re: [PATCH v2 01/23] KVM: selftests: Minor improvements to asserts in test_vmx_nested_state()

Quoted 22 lines · Sean Christopherson
Quoted 21 lines · Yosry Ahmed

Display the address as hex if the asserts for the vmxon_pa and vmcs12_pa fail, and assert that the flags are 0 as expected.

---
 .../selftests/kvm/x86/vmx_set_nested_state_test.c      | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c +2 -2
index 67a62a5a88951..c4c400d2824c1 100644
@@ -241,8 +241,14 @@ void test_vmx_nested_state(struct kvm_vcpu *vcpu)
 	TEST_ASSERT(state->size >= sizeof(*state) && state->size <= state_sz,
 		    "Size must be between %ld and %d.  The size returned was %d.",
 		    sizeof(*state), state_sz, state->size);
-	TEST_ASSERT(state->hdr.vmx.vmxon_pa == -1ull, "vmxon_pa must be -1ull.");
-	TEST_ASSERT(state->hdr.vmx.vmcs12_pa == -1ull, "vmcs_pa must be -1ull.");
+	TEST_ASSERT(state->hdr.vmx.vmxon_pa == -1ull,
+		    "vmxon_pa must be 0x%llx, but was 0x%llx",
Quoted 8 lines

tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c void test_vmx_nested_state(struct kvm_vcpu *vcpu) · line 251

Quoted 6 lines
tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c +6 -0
+		    -1ull, state->hdr.vmx.vmxon_pa);
+	TEST_ASSERT(state->hdr.vmx.vmcs12_pa == -1ull,
+		    "vmcs12_pa must be 0x%llx, but was 0x%llx",
+		    -1llu, state->hdr.vmx.vmcs12_pa);
+	TEST_ASSERT(state->flags == 0,
+		    "Flags must be equal to 0, but was 0x%hx", state->flags);

The error messages aren't adding a whole lot, why not just use TEST_ASSERT_EQ()?

I hadn't discovered TEST_ASSERT_EQ() at that point in my life, not until later while working on that series :D

Seems like TEST_ASSERT_EQ() already prints in hex, so that's nice. I can switch to TEST_ASSERT_EQ() if/when I respin.

#4 lore raw

PATCH v2 2/23 KVM: selftests: Extend vmx_set_nested_state_test to cover SVM

In reply to Yosry Ahmed

Add test cases for the validation checks in svm_set_nested_state(), and allow the test to run with SVM as well as VMX. The SVM test also makes sure that KVM_SET_NESTED_STATE accepts GIF being set or cleared if EFER.SVME is cleared, verifying a recently fixed bug where GIF was incorrectly expected to always be set when EFER.SVME is cleared.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   2 +-
 ...d_state_test.c => set_nested_state_test.c} | 125 ++++++++++++++++--
 2 files changed, 116 insertions(+), 11 deletions(-)
 rename tools/testing/selftests/kvm/x86/{vmx_set_nested_state_test.c => set_nested_state_test.c} (70%)
tools/testing/selftests/kvm/Makefile.kvm +1 -1
index b9279ce4eaab8..acfa22206e6f3 100644
@@ -100,6 +100,7 @@ TEST_GEN_PROGS_x86 += x86/set_sregs_test
 TEST_GEN_PROGS_x86 += x86/smaller_maxphyaddr_emulation_test
 TEST_GEN_PROGS_x86 += x86/smm_test
 TEST_GEN_PROGS_x86 += x86/state_test
+TEST_GEN_PROGS_x86 += x86/set_nested_state_test
 TEST_GEN_PROGS_x86 += x86/vmx_preemption_timer_test
 TEST_GEN_PROGS_x86 += x86/svm_vmcall_test
 TEST_GEN_PROGS_x86 += x86/svm_int_ctl_test
@@ -116,7 +117,6 @@ TEST_GEN_PROGS_x86 += x86/vmx_dirty_log_test
 TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state
 TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
 TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state
-TEST_GEN_PROGS_x86 += x86/vmx_set_nested_state_test
 TEST_GEN_PROGS_x86 += x86/vmx_la57_nested_state_test
 TEST_GEN_PROGS_x86 += x86/vmx_tsc_adjust_test
 TEST_GEN_PROGS_x86 += x86/vmx_nested_tsc_scaling_test
tools/testing/selftests/kvm/x86/set_nested_state_test.c renamed from tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c +115 -10

160 lines not on this page · raw message

Signature · 1 line
2.51.0.869.ge66316f041-goog
#5 lore raw

Re: [PATCH v2 02/23] KVM: selftests: Extend vmx_set_nested_state_test to cover SVM

Quoted 18 lines · Yosry Ahmed

Add test cases for the validation checks in svm_set_nested_state(), and allow the test to run with SVM as well as VMX. The SVM test also makes sure that KVM_SET_NESTED_STATE accepts GIF being set or cleared if EFER.SVME is cleared, verifying a recently fixed bug where GIF was incorrectly expected to always be set when EFER.SVME is cleared.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   2 +-
 ...d_state_test.c => set_nested_state_test.c} | 125 ++++++++++++++++--
 2 files changed, 116 insertions(+), 11 deletions(-)
 rename tools/testing/selftests/kvm/x86/{vmx_set_nested_state_test.c => set_nested_state_test.c} (70%)
tools/testing/selftests/kvm/Makefile.kvm +0 -0
index b9279ce4eaab8..acfa22206e6f3 100644

tools/testing/selftests/kvm/Makefile.kvm TEST_GEN_PROGS_x86 += x86/set_sregs_test · line 103

Quoted 4 lines
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -100,6 +100,7 @@ TEST_GEN_PROGS_x86 += x86/set_sregs_test
 TEST_GEN_PROGS_x86 += x86/smaller_maxphyaddr_emulation_test
 TEST_GEN_PROGS_x86 += x86/smm_test
 TEST_GEN_PROGS_x86 += x86/state_test
+TEST_GEN_PROGS_x86 += x86/set_nested_state_test

Hmm, when you post this with the next version of the GIF fixes, I think it makes sense to go with nested_set_state_test, so that we can bundle all the nested tests together (selftests are often sorted alphabetically).

Quoted 19 lines
tools/testing/selftests/kvm/Makefile.kvm +0 -1
 TEST_GEN_PROGS_x86 += x86/vmx_preemption_timer_test
 TEST_GEN_PROGS_x86 += x86/svm_vmcall_test
 TEST_GEN_PROGS_x86 += x86/svm_int_ctl_test
@@ -116,7 +117,6 @@ TEST_GEN_PROGS_x86 += x86/vmx_dirty_log_test
 TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state
 TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
 TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state
-TEST_GEN_PROGS_x86 += x86/vmx_set_nested_state_test
 TEST_GEN_PROGS_x86 += x86/vmx_la57_nested_state_test
 TEST_GEN_PROGS_x86 += x86/vmx_tsc_adjust_test
 TEST_GEN_PROGS_x86 += x86/vmx_nested_tsc_scaling_test
tools/testing/selftests/kvm/x86/set_nested_state_test.c renamed from tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c +0 -0
similarity index 70%
rename from tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c
rename to tools/testing/selftests/kvm/x86/set_nested_state_test.c
index c4c400d2824c1..fe12fffacd2ec 100644

tools/testing/selftests/kvm/x86/set_nested_state_test.c line 3

Quoted 4 lines
tools/testing/selftests/kvm/x86/set_nested_state_test.c renamed from tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c +1 -1
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * vmx_set_nested_state_test
+ * set_nested_state_test

Just delete these comments, they're completely useless.

#6 lore raw

PATCH v2 3/23 KVM: selftests: Extend vmx_close_while_nested_test to cover SVM

In reply to Yosry Ahmed

Add SVM L1 code to run the nested guest, and allow the test to run with SVM as well as VMX.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  2 +-
 ...ested_test.c => close_while_nested_test.c} | 42 +++++++++++++++----
 2 files changed, 35 insertions(+), 9 deletions(-)
 rename tools/testing/selftests/kvm/x86/{vmx_close_while_nested_test.c => close_while_nested_test.c} (64%)
tools/testing/selftests/kvm/Makefile.kvm +1 -1
index acfa22206e6f3..e70a844a52bdc 100644
@@ -112,7 +112,7 @@ TEST_GEN_PROGS_x86 += x86/ucna_injection_test
 TEST_GEN_PROGS_x86 += x86/userspace_io_test
 TEST_GEN_PROGS_x86 += x86/userspace_msr_exit_test
 TEST_GEN_PROGS_x86 += x86/vmx_apic_access_test
-TEST_GEN_PROGS_x86 += x86/vmx_close_while_nested_test
+TEST_GEN_PROGS_x86 += x86/close_while_nested_test
 TEST_GEN_PROGS_x86 += x86/vmx_dirty_log_test
 TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state
 TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
tools/testing/selftests/kvm/x86/close_while_nested_test.c renamed from tools/testing/selftests/kvm/x86/vmx_close_while_nested_test.c +34 -8

80 lines not on this page · raw message

Signature · 1 line
2.51.0.869.ge66316f041-goog
#7 lore raw

Re: [PATCH v2 03/23] KVM: selftests: Extend vmx_close_while_nested_test to cover SVM

Quoted 17 lines · Yosry Ahmed

Add SVM L1 code to run the nested guest, and allow the test to run with SVM as well as VMX.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  2 +-
 ...ested_test.c => close_while_nested_test.c} | 42 +++++++++++++++----
 2 files changed, 35 insertions(+), 9 deletions(-)
 rename tools/testing/selftests/kvm/x86/{vmx_close_while_nested_test.c => close_while_nested_test.c} (64%)
tools/testing/selftests/kvm/Makefile.kvm +0 -0
index acfa22206e6f3..e70a844a52bdc 100644

tools/testing/selftests/kvm/Makefile.kvm TEST_GEN_PROGS_x86 += x86/ucna_injection_test · line 115

Quoted 5 lines
tools/testing/selftests/kvm/Makefile.kvm +1 -1
@@ -112,7 +112,7 @@ TEST_GEN_PROGS_x86 += x86/ucna_injection_test
 TEST_GEN_PROGS_x86 += x86/userspace_io_test
 TEST_GEN_PROGS_x86 += x86/userspace_msr_exit_test
 TEST_GEN_PROGS_x86 += x86/vmx_apic_access_test
-TEST_GEN_PROGS_x86 += x86/vmx_close_while_nested_test
+TEST_GEN_PROGS_x86 += x86/close_while_nested_test

Unless someone vehemently objects, I'm going to tweak this to nested_close_kvm_test, and keep Makefile.kvm sorted.

Quoted 11 lines
tools/testing/selftests/kvm/Makefile.kvm +0 -0
 TEST_GEN_PROGS_x86 += x86/vmx_dirty_log_test
 TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state
 TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
tools/testing/selftests/kvm/x86/close_while_nested_test.c renamed from tools/testing/selftests/kvm/x86/vmx_close_while_nested_test.c +0 -0
similarity index 64%
rename from tools/testing/selftests/kvm/x86/vmx_close_while_nested_test.c
rename to tools/testing/selftests/kvm/x86/close_while_nested_test.c
index dad988351493e..cf5f24c83c448 100644

tools/testing/selftests/kvm/x86/close_while_nested_test.c line 3

Quoted 4 lines
tools/testing/selftests/kvm/x86/close_while_nested_test.c renamed from tools/testing/selftests/kvm/x86/vmx_close_while_nested_test.c +1 -1
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * vmx_close_while_nested
+ * close_while_nested_test

And of course zap this :-)

#8 lore raw

PATCH v2 4/23 KVM: selftests: Extend vmx_nested_tsc_scaling_test to cover SVM

In reply to Yosry Ahmed

Add SVM L1 code to run the nested guest, and allow the test to run with SVM as well as VMX.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  2 +-
 ...aling_test.c => nested_tsc_scaling_test.c} | 48 +++++++++++++++++--
 2 files changed, 44 insertions(+), 6 deletions(-)
 rename tools/testing/selftests/kvm/x86/{vmx_nested_tsc_scaling_test.c => nested_tsc_scaling_test.c} (83%)
tools/testing/selftests/kvm/Makefile.kvm +1 -1
index e70a844a52bdc..bb2ff7927ef57 100644
@@ -119,7 +119,7 @@ TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
 TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state
 TEST_GEN_PROGS_x86 += x86/vmx_la57_nested_state_test
 TEST_GEN_PROGS_x86 += x86/vmx_tsc_adjust_test
-TEST_GEN_PROGS_x86 += x86/vmx_nested_tsc_scaling_test
+TEST_GEN_PROGS_x86 += x86/nested_tsc_scaling_test
 TEST_GEN_PROGS_x86 += x86/apic_bus_clock_test
 TEST_GEN_PROGS_x86 += x86/xapic_ipi_test
 TEST_GEN_PROGS_x86 += x86/xapic_state_test
tools/testing/selftests/kvm/x86/nested_tsc_scaling_test.c renamed from tools/testing/selftests/kvm/x86/vmx_nested_tsc_scaling_test.c +43 -5

82 lines not on this page · raw message

Signature · 1 line
2.51.0.869.ge66316f041-goog
#9 lore raw

Re: [PATCH v2 04/23] KVM: selftests: Extend vmx_nested_tsc_scaling_test to cover SVM

Quoted 15 lines · Yosry Ahmed

Add SVM L1 code to run the nested guest, and allow the test to run with SVM as well as VMX.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  2 +-
 ...aling_test.c => nested_tsc_scaling_test.c} | 48 +++++++++++++++++--
 2 files changed, 44 insertions(+), 6 deletions(-)
 rename tools/testing/selftests/kvm/x86/{vmx_nested_tsc_scaling_test.c => nested_tsc_scaling_test.c} (83%)
tools/testing/selftests/kvm/Makefile.kvm +0 -0
index e70a844a52bdc..bb2ff7927ef57 100644

tools/testing/selftests/kvm/Makefile.kvm TEST_GEN_PROGS_x86 += x86/vmx_msrs_test · line 122

Quoted 5 lines
tools/testing/selftests/kvm/Makefile.kvm +1 -1
@@ -119,7 +119,7 @@ TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
 TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state
 TEST_GEN_PROGS_x86 += x86/vmx_la57_nested_state_test
 TEST_GEN_PROGS_x86 += x86/vmx_tsc_adjust_test
-TEST_GEN_PROGS_x86 += x86/vmx_nested_tsc_scaling_test
+TEST_GEN_PROGS_x86 += x86/nested_tsc_scaling_test

Please keep the tests sorted (bad apic_bus_clock_test, bad test).

#10 lore raw

PATCH v2 5/23 KVM: selftests: Move nested invalid CR3 check to its own test

In reply to Yosry Ahmed

vmx_tsc_adjust_test currently verifies that a nested VMLAUNCH fails with an invalid CR3. This is irrelevant to TSC scaling, move it to a standalone test.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  1 +
 .../kvm/x86/nested_invalid_cr3_test.c         | 81 +++++++++++++++++++
 .../selftests/kvm/x86/vmx_tsc_adjust_test.c   | 10 ---
 3 files changed, 82 insertions(+), 10 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index bb2ff7927ef57..b78700c574fc7 100644
@@ -88,6 +88,7 @@ TEST_GEN_PROGS_x86 += x86/kvm_pv_test
 TEST_GEN_PROGS_x86 += x86/kvm_buslock_test
 TEST_GEN_PROGS_x86 += x86/monitor_mwait_test
 TEST_GEN_PROGS_x86 += x86/msrs_test
+TEST_GEN_PROGS_x86 += x86/nested_invalid_cr3_test
 TEST_GEN_PROGS_x86 += x86/nested_emulation_test
 TEST_GEN_PROGS_x86 += x86/nested_exceptions_test
 TEST_GEN_PROGS_x86 += x86/platform_info_test
tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c new file +81 -0

81 lines not on this page · raw message

tools/testing/selftests/kvm/x86/vmx_tsc_adjust_test.c +0 -10
index 2ceb5c78c4427..2dcc0306a0d9b 100644
@@ -77,7 +77,6 @@ static void l1_guest_code(struct vmx_pages *vmx_pages)
 #define L2_GUEST_STACK_SIZE 64
 	unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
 	uint32_t control;
-	uintptr_t save_cr3;
 
 	GUEST_ASSERT(rdtsc() < TSC_ADJUST_VALUE);
 	wrmsr(MSR_IA32_TSC, rdtsc() - TSC_ADJUST_VALUE);
@@ -94,15 +93,6 @@ static void l1_guest_code(struct vmx_pages *vmx_pages)
 	vmwrite(CPU_BASED_VM_EXEC_CONTROL, control);
 	vmwrite(TSC_OFFSET, TSC_OFFSET_VALUE);
 
-	/* Jump into L2.  First, test failure to load guest CR3.  */
-	save_cr3 = vmreadz(GUEST_CR3);
-	vmwrite(GUEST_CR3, -1ull);
-	GUEST_ASSERT(!vmlaunch());
-	GUEST_ASSERT(vmreadz(VM_EXIT_REASON) ==
-		     (EXIT_REASON_FAILED_VMENTRY | EXIT_REASON_INVALID_STATE));
-	check_ia32_tsc_adjust(-1 * TSC_ADJUST_VALUE);
-	vmwrite(GUEST_CR3, save_cr3);
-
 	GUEST_ASSERT(!vmlaunch());
 	GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
 
Signature · 1 line
2.51.0.869.ge66316f041-goog
#11 lore raw

Re: [PATCH v2 05/23] KVM: selftests: Move nested invalid CR3 check to its own test

Quoted 17 lines · Yosry Ahmed

vmx_tsc_adjust_test currently verifies that a nested VMLAUNCH fails with an invalid CR3. This is irrelevant to TSC scaling, move it to a standalone test.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  1 +
 .../kvm/x86/nested_invalid_cr3_test.c         | 81 +++++++++++++++++++
 .../selftests/kvm/x86/vmx_tsc_adjust_test.c   | 10 ---
 3 files changed, 82 insertions(+), 10 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c
tools/testing/selftests/kvm/Makefile.kvm +0 -0
index bb2ff7927ef57..b78700c574fc7 100644

tools/testing/selftests/kvm/Makefile.kvm TEST_GEN_PROGS_x86 += x86/kvm_pv_test · line 91

Quoted 4 lines
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -88,6 +88,7 @@ TEST_GEN_PROGS_x86 += x86/kvm_pv_test
 TEST_GEN_PROGS_x86 += x86/kvm_buslock_test
 TEST_GEN_PROGS_x86 += x86/monitor_mwait_test
 TEST_GEN_PROGS_x86 += x86/msrs_test
+TEST_GEN_PROGS_x86 += x86/nested_invalid_cr3_test

Almost. A. B. C. D. I? E. :-D

tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c line 3

Quoted 12 lines
tools/testing/selftests/kvm/Makefile.kvm +0 -0
 TEST_GEN_PROGS_x86 += x86/nested_emulation_test
 TEST_GEN_PROGS_x86 += x86/nested_exceptions_test
 TEST_GEN_PROGS_x86 += x86/platform_info_test
tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c new file +3 -0
new file mode 100644
index 0000000000000..b9853ab532cfe
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * nested_invalid_cr3_test

Boooooh.

#12 lore raw

PATCH v2 6/23 KVM: selftests: Extend nested_invalid_cr3_test to cover SVM

In reply to Yosry Ahmed

Add SVM L1 code to run the nested guest, and allow the test to run with SVM as well as VMX.

---
 .../kvm/x86/nested_invalid_cr3_test.c         | 43 +++++++++++++++++--
 1 file changed, 40 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c +40 -3
index b9853ab532cfe..2a472440cb962 100644
@@ -9,6 +9,7 @@
  */
 #include "kvm_util.h"
 #include "vmx.h"
+#include "svm_util.h"
 #include "kselftest.h"
 
 
@@ -19,6 +20,28 @@ static void l2_guest_code(void)
 	vmcall();
 }
 
+static void l1_svm_code(struct svm_test_data *svm)
+{
+	unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
+	uintptr_t save_cr3;
+
+	generic_svm_setup(svm, l2_guest_code,
+			  &l2_guest_stack[L2_GUEST_STACK_SIZE]);
+
+	/* Try to run L2 with invalid CR3 and make sure it fails */
+	save_cr3 = svm->vmcb->save.cr3;
+	svm->vmcb->save.cr3 = -1ull;
+	run_guest(svm->vmcb, svm->vmcb_gpa);
+	GUEST_ASSERT(svm->vmcb->control.exit_code == SVM_EXIT_ERR);
+
+	/* Now restore CR3 and make sure L2 runs successfully */
+	svm->vmcb->save.cr3 = save_cr3;
+	run_guest(svm->vmcb, svm->vmcb_gpa);
+	GUEST_ASSERT(svm->vmcb->control.exit_code == SVM_EXIT_VMMCALL);
+
+	GUEST_DONE();
+}
+
 static void l1_vmx_code(struct vmx_pages *vmx_pages)
 {
 	unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
@@ -45,16 +68,30 @@ static void l1_vmx_code(struct vmx_pages *vmx_pages)
 	GUEST_DONE();
 }
 
+static void l1_guest_code(void *data)
+{
+	if (this_cpu_has(X86_FEATURE_VMX))
+		l1_vmx_code(data);
+	else
+		l1_svm_code(data);
+}
+
 int main(int argc, char *argv[])
 {
 	struct kvm_vcpu *vcpu;
 	struct kvm_vm *vm;
 	vm_vaddr_t guest_gva = 0;
 
-	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX) ||
+		     kvm_cpu_has(X86_FEATURE_SVM));
+
+	vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
+
+	if (kvm_cpu_has(X86_FEATURE_VMX))
+		vcpu_alloc_vmx(vm, &guest_gva);
+	else
+		vcpu_alloc_svm(vm, &guest_gva);
 
-	vm = vm_create_with_one_vcpu(&vcpu, l1_vmx_code);
-	vcpu_alloc_vmx(vm, &guest_gva);
 	vcpu_args_set(vcpu, 1, guest_gva);
 
 	for (;;) {
Signature · 1 line
2.51.0.869.ge66316f041-goog
#13 lore raw

PATCH v2 7/23 KVM: selftests: Extend vmx_tsc_adjust_test to cover SVM

In reply to Yosry Ahmed

Add SVM L1 code to run the nested guest, and allow the test to run with SVM as well as VMX.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  2 +-
 ...adjust_test.c => nested_tsc_adjust_test.c} | 69 ++++++++++++-------
 2 files changed, 46 insertions(+), 25 deletions(-)
 rename tools/testing/selftests/kvm/x86/{vmx_tsc_adjust_test.c => nested_tsc_adjust_test.c} (61%)
tools/testing/selftests/kvm/Makefile.kvm +1 -1
index b78700c574fc7..6625ac53545e8 100644
@@ -119,7 +119,7 @@ TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state
 TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
 TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state
 TEST_GEN_PROGS_x86 += x86/vmx_la57_nested_state_test
-TEST_GEN_PROGS_x86 += x86/vmx_tsc_adjust_test
+TEST_GEN_PROGS_x86 += x86/nested_tsc_adjust_test
 TEST_GEN_PROGS_x86 += x86/nested_tsc_scaling_test
 TEST_GEN_PROGS_x86 += x86/apic_bus_clock_test
 TEST_GEN_PROGS_x86 += x86/xapic_ipi_test
tools/testing/selftests/kvm/x86/nested_tsc_adjust_test.c renamed from tools/testing/selftests/kvm/x86/vmx_tsc_adjust_test.c +45 -24

111 lines not on this page · raw message

Signature · 1 line
2.51.0.869.ge66316f041-goog
#14 lore raw

PATCH v2 8/23 KVM: selftests: Stop hardcoding PAGE_SIZE in x86 selftests

In reply to Yosry Ahmed

Use PAGE_SIZE instead of 4096.

---
 .../selftests/kvm/x86/hyperv_features.c        |  2 +-
 tools/testing/selftests/kvm/x86/hyperv_ipi.c   | 18 +++++++++---------
 .../testing/selftests/kvm/x86/sev_smoke_test.c |  2 +-
 tools/testing/selftests/kvm/x86/state_test.c   |  2 +-
 .../selftests/kvm/x86/userspace_io_test.c      |  2 +-
 .../selftests/kvm/x86/vmx_dirty_log_test.c     | 10 +++++-----
 6 files changed, 18 insertions(+), 18 deletions(-)
tools/testing/selftests/kvm/x86/hyperv_features.c +1 -1
index 99d327084172f..130b9ce7e5ddd 100644
@@ -94,7 +94,7 @@ static void guest_hcall(vm_vaddr_t pgs_gpa, struct hcall_data *hcall)
 
 	if (!(hcall->control & HV_HYPERCALL_FAST_BIT)) {
 		input = pgs_gpa;
-		output = pgs_gpa + 4096;
+		output = pgs_gpa + PAGE_SIZE;
 	} else {
 		input = output = 0;
 	}
tools/testing/selftests/kvm/x86/hyperv_ipi.c +9 -9
index 2b5b4bc6ef7ec..ca61836c4e325 100644
@@ -102,7 +102,7 @@ static void sender_guest_code(void *hcall_page, vm_vaddr_t pgs_gpa)
 	/* 'Slow' HvCallSendSyntheticClusterIpi to RECEIVER_VCPU_ID_1 */
 	ipi->vector = IPI_VECTOR;
 	ipi->cpu_mask = 1 << RECEIVER_VCPU_ID_1;
-	hyperv_hypercall(HVCALL_SEND_IPI, pgs_gpa, pgs_gpa + 4096);
+	hyperv_hypercall(HVCALL_SEND_IPI, pgs_gpa, pgs_gpa + PAGE_SIZE);
 	nop_loop();
 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ipis_expected[1]);
@@ -116,13 +116,13 @@ static void sender_guest_code(void *hcall_page, vm_vaddr_t pgs_gpa)
 	GUEST_SYNC(stage++);
 
 	/* 'Slow' HvCallSendSyntheticClusterIpiEx to RECEIVER_VCPU_ID_1 */
-	memset(hcall_page, 0, 4096);
+	memset(hcall_page, 0, PAGE_SIZE);
 	ipi_ex->vector = IPI_VECTOR;
 	ipi_ex->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
 	ipi_ex->vp_set.valid_bank_mask = 1 << 0;
 	ipi_ex->vp_set.bank_contents[0] = BIT(RECEIVER_VCPU_ID_1);
 	hyperv_hypercall(HVCALL_SEND_IPI_EX | (1 << HV_HYPERCALL_VARHEAD_OFFSET),
-			 pgs_gpa, pgs_gpa + 4096);
+			 pgs_gpa, pgs_gpa + PAGE_SIZE);
 	nop_loop();
 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ipis_expected[1]);
@@ -138,13 +138,13 @@ static void sender_guest_code(void *hcall_page, vm_vaddr_t pgs_gpa)
 	GUEST_SYNC(stage++);
 
 	/* 'Slow' HvCallSendSyntheticClusterIpiEx to RECEIVER_VCPU_ID_2 */
-	memset(hcall_page, 0, 4096);
+	memset(hcall_page, 0, PAGE_SIZE);
 	ipi_ex->vector = IPI_VECTOR;
 	ipi_ex->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
 	ipi_ex->vp_set.valid_bank_mask = 1 << 1;
 	ipi_ex->vp_set.bank_contents[0] = BIT(RECEIVER_VCPU_ID_2 - 64);
 	hyperv_hypercall(HVCALL_SEND_IPI_EX | (1 << HV_HYPERCALL_VARHEAD_OFFSET),
-			 pgs_gpa, pgs_gpa + 4096);
+			 pgs_gpa, pgs_gpa + PAGE_SIZE);
 	nop_loop();
 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ipis_expected[0]);
 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ++ipis_expected[1]);
@@ -160,14 +160,14 @@ static void sender_guest_code(void *hcall_page, vm_vaddr_t pgs_gpa)
 	GUEST_SYNC(stage++);
 
 	/* 'Slow' HvCallSendSyntheticClusterIpiEx to both RECEIVER_VCPU_ID_{1,2} */
-	memset(hcall_page, 0, 4096);
+	memset(hcall_page, 0, PAGE_SIZE);
 	ipi_ex->vector = IPI_VECTOR;
 	ipi_ex->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
 	ipi_ex->vp_set.valid_bank_mask = 1 << 1 | 1;
 	ipi_ex->vp_set.bank_contents[0] = BIT(RECEIVER_VCPU_ID_1);
 	ipi_ex->vp_set.bank_contents[1] = BIT(RECEIVER_VCPU_ID_2 - 64);
 	hyperv_hypercall(HVCALL_SEND_IPI_EX | (2 << HV_HYPERCALL_VARHEAD_OFFSET),
-			 pgs_gpa, pgs_gpa + 4096);
+			 pgs_gpa, pgs_gpa + PAGE_SIZE);
 	nop_loop();
 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ++ipis_expected[1]);
@@ -183,10 +183,10 @@ static void sender_guest_code(void *hcall_page, vm_vaddr_t pgs_gpa)
 	GUEST_SYNC(stage++);
 
 	/* 'Slow' HvCallSendSyntheticClusterIpiEx to HV_GENERIC_SET_ALL */
-	memset(hcall_page, 0, 4096);
+	memset(hcall_page, 0, PAGE_SIZE);
 	ipi_ex->vector = IPI_VECTOR;
 	ipi_ex->vp_set.format = HV_GENERIC_SET_ALL;
-	hyperv_hypercall(HVCALL_SEND_IPI_EX, pgs_gpa, pgs_gpa + 4096);
+	hyperv_hypercall(HVCALL_SEND_IPI_EX, pgs_gpa, pgs_gpa + PAGE_SIZE);
 	nop_loop();
 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_1] == ++ipis_expected[0]);
 	GUEST_ASSERT(ipis_rcvd[RECEIVER_VCPU_ID_2] == ++ipis_expected[1]);
tools/testing/selftests/kvm/x86/sev_smoke_test.c +1 -1
index 77256c89bb8de..86ad1c7d068f2 100644
@@ -104,7 +104,7 @@ static void test_sync_vmsa(uint32_t type, uint64_t policy)
 	vm_sev_launch(vm, policy, NULL);
 
 	/* This page is shared, so make it decrypted.  */
-	memset(hva, 0, 4096);
+	memset(hva, 0, PAGE_SIZE);
 
 	vcpu_run(vcpu);
 
tools/testing/selftests/kvm/x86/state_test.c +1 -1
index 141b7fc0c965b..f2c7a1c297e37 100644
@@ -141,7 +141,7 @@ static void __attribute__((__flatten__)) guest_code(void *arg)
 
 	if (this_cpu_has(X86_FEATURE_XSAVE)) {
 		uint64_t supported_xcr0 = this_cpu_supported_xcr0();
-		uint8_t buffer[4096];
+		uint8_t buffer[PAGE_SIZE];
 
 		memset(buffer, 0xcc, sizeof(buffer));
 
tools/testing/selftests/kvm/x86/userspace_io_test.c +1 -1
index 9481cbcf284f6..be7d72f3c029f 100644
@@ -85,7 +85,7 @@ int main(int argc, char *argv[])
 			regs.rcx = 1;
 		if (regs.rcx == 3)
 			regs.rcx = 8192;
-		memset((void *)run + run->io.data_offset, 0xaa, 4096);
+		memset((void *)run + run->io.data_offset, 0xaa, PAGE_SIZE);
 		vcpu_regs_set(vcpu, &regs);
 	}
 
tools/testing/selftests/kvm/x86/vmx_dirty_log_test.c +5 -5
index fa512d033205f..34a57fe747f64 100644
@@ -122,15 +122,15 @@ static void test_vmx_dirty_log(bool enable_ept)
 	if (enable_ept) {
 		prepare_eptp(vmx, vm, 0);
 		nested_map_memslot(vmx, vm, 0);
-		nested_map(vmx, vm, NESTED_TEST_MEM1, GUEST_TEST_MEM, 4096);
-		nested_map(vmx, vm, NESTED_TEST_MEM2, GUEST_TEST_MEM, 4096);
+		nested_map(vmx, vm, NESTED_TEST_MEM1, GUEST_TEST_MEM, PAGE_SIZE);
+		nested_map(vmx, vm, NESTED_TEST_MEM2, GUEST_TEST_MEM, PAGE_SIZE);
 	}
 
 	bmap = bitmap_zalloc(TEST_MEM_PAGES);
 	host_test_mem = addr_gpa2hva(vm, GUEST_TEST_MEM);
 
 	while (!done) {
-		memset(host_test_mem, 0xaa, TEST_MEM_PAGES * 4096);
+		memset(host_test_mem, 0xaa, TEST_MEM_PAGES * PAGE_SIZE);
 		vcpu_run(vcpu);
 		TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
 
@@ -153,9 +153,9 @@ static void test_vmx_dirty_log(bool enable_ept)
 			}
 
 			TEST_ASSERT(!test_bit(1, bmap), "Page 1 incorrectly reported dirty");
-			TEST_ASSERT(host_test_mem[4096 / 8] == 0xaaaaaaaaaaaaaaaaULL, "Page 1 written by guest");
+			TEST_ASSERT(host_test_mem[PAGE_SIZE / 8] == 0xaaaaaaaaaaaaaaaaULL, "Page 1 written by guest");
 			TEST_ASSERT(!test_bit(2, bmap), "Page 2 incorrectly reported dirty");
-			TEST_ASSERT(host_test_mem[8192 / 8] == 0xaaaaaaaaaaaaaaaaULL, "Page 2 written by guest");
+			TEST_ASSERT(host_test_mem[PAGE_SIZE*2 / 8] == 0xaaaaaaaaaaaaaaaaULL, "Page 2 written by guest");
 			break;
 		case UCALL_DONE:
 			done = true;
Signature · 1 line
2.51.0.869.ge66316f041-goog
#15 lore raw

PATCH v2 9/23 KVM: selftests: Remove the unused argument to prepare_eptp()

In reply to Yosry Ahmed

eptp_memslot is unused, remove it. No functional change intended.

---
 tools/testing/selftests/kvm/include/x86/vmx.h        | 3 +--
 tools/testing/selftests/kvm/lib/x86/memstress.c      | 2 +-
 tools/testing/selftests/kvm/lib/x86/vmx.c            | 3 +--
 tools/testing/selftests/kvm/x86/vmx_dirty_log_test.c | 2 +-
 4 files changed, 4 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/include/x86/vmx.h +1 -2
index edb3c391b9824..96e2b4c630a9b 100644
@@ -568,8 +568,7 @@ void nested_map_memslot(struct vmx_pages *vmx, struct kvm_vm *vm,
 void nested_identity_map_1g(struct vmx_pages *vmx, struct kvm_vm *vm,
 			    uint64_t addr, uint64_t size);
 bool kvm_cpu_has_ept(void);
-void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm,
-		  uint32_t eptp_memslot);
+void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm);
 void prepare_virtualize_apic_accesses(struct vmx_pages *vmx, struct kvm_vm *vm);
 
 #endif /* SELFTEST_KVM_VMX_H */
tools/testing/selftests/kvm/lib/x86/memstress.c +1 -1
index 7f5d62a65c68a..0b1f288ad5564 100644
@@ -63,7 +63,7 @@ void memstress_setup_ept(struct vmx_pages *vmx, struct kvm_vm *vm)
 {
 	uint64_t start, end;
 
-	prepare_eptp(vmx, vm, 0);
+	prepare_eptp(vmx, vm);
 
 	/*
 	 * Identity map the first 4G and the test region with 1G pages so that
tools/testing/selftests/kvm/lib/x86/vmx.c +1 -2
index 1b6d4a0077980..f0023a3b0137e 100644
@@ -535,8 +535,7 @@ bool kvm_cpu_has_ept(void)
 	return ctrl & SECONDARY_EXEC_ENABLE_EPT;
 }
 
-void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm,
-		  uint32_t eptp_memslot)
+void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm)
 {
 	TEST_ASSERT(kvm_cpu_has_ept(), "KVM doesn't support nested EPT");
 
tools/testing/selftests/kvm/x86/vmx_dirty_log_test.c +1 -1
index 34a57fe747f64..98cb6bdab3e6d 100644
@@ -120,7 +120,7 @@ static void test_vmx_dirty_log(bool enable_ept)
 	 * GPAs as the EPT enabled case.
 	 */
 	if (enable_ept) {
-		prepare_eptp(vmx, vm, 0);
+		prepare_eptp(vmx, vm);
 		nested_map_memslot(vmx, vm, 0);
 		nested_map(vmx, vm, NESTED_TEST_MEM1, GUEST_TEST_MEM, PAGE_SIZE);
 		nested_map(vmx, vm, NESTED_TEST_MEM2, GUEST_TEST_MEM, PAGE_SIZE);
Signature · 1 line
2.51.0.869.ge66316f041-goog
#16 lore raw

PATCH v2 10/23 KVM: selftests: Stop using __virt_pg_map() directly in tests

In reply to Yosry Ahmed

Replace __virt_pg_map() calls in tests by high-level equivalent functions, removing some loops in the process.

No functional change intended.

---
 tools/testing/selftests/kvm/mmu_stress_test.c      | 6 ++----
 tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c | 2 +-
 2 files changed, 3 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/mmu_stress_test.c +2 -4
index 6a437d2be9fa4..21c0f8e6552cd 100644
@@ -361,11 +361,9 @@ int main(int argc, char *argv[])
 
 #ifdef __x86_64__
 		/* Identity map memory in the guest using 1gb pages. */
-		for (i = 0; i < slot_size; i += SZ_1G)
-			__virt_pg_map(vm, gpa + i, gpa + i, PG_LEVEL_1G);
+		virt_map_level(vm, gpa, gpa, slot_size, PG_LEVEL_1G);
 #else
-		for (i = 0; i < slot_size; i += vm->page_size)
-			virt_pg_map(vm, gpa + i, gpa + i);
+		virt_map(vm, gpa, gpa, slot_size >> vm->page_shift);
 #endif
 	}
 
tools/testing/selftests/kvm/x86/hyperv_tlb_flush.c +1 -1
index 077cd0ec3040e..a3b7ce1559812 100644
@@ -621,7 +621,7 @@ int main(int argc, char *argv[])
 	for (i = 0; i < NTEST_PAGES; i++) {
 		pte = vm_get_page_table_entry(vm, data->test_pages + i * PAGE_SIZE);
 		gpa = addr_hva2gpa(vm, pte);
-		__virt_pg_map(vm, gva + PAGE_SIZE * i, gpa & PAGE_MASK, PG_LEVEL_4K);
+		virt_pg_map(vm, gva + PAGE_SIZE * i, gpa & PAGE_MASK);
 		data->test_pages_pte[i] = gva + (gpa & ~PAGE_MASK);
 	}
 
Signature · 1 line
2.51.0.869.ge66316f041-goog
#17 lore raw

PATCH v2 11/23 KVM: selftests: Make sure vm->vpages_mapped is always up-to-date

In reply to Yosry Ahmed

Call paths leading to __virt_pg_map() are currently:
(a) virt_pg_map() -> virt_arch_pg_map() -> __virt_pg_map()
(b) virt_map_level() -> __virt_pg_map()

For (a), calls to virt_pg_map() from kvm_util.c make sure they update vm->vpages_mapped, but other callers do not. Move the sparsebit_set() call into virt_pg_map() to make sure all callers are captured.

For (b), call sparsebit_set_num() from virt_map_level().

It's tempting to have a single the call inside __virt_pg_map(), however:
- The call path in (a) is not x86-specific, while (b) is. Moving the call into __virt_pg_map() would require doing something similar for other archs implementing virt_pg_map().

- Incoming changes will reusue __virt_pg_map() for nested PTEs, which should not update vm->vpages_mapped. This means we'd need to break out a triple underscore version that does not update vm->vpages_mapped.

---
 tools/testing/selftests/kvm/include/kvm_util.h  | 1 +
 tools/testing/selftests/kvm/lib/kvm_util.c      | 3 ---
 tools/testing/selftests/kvm/lib/x86/processor.c | 2 ++
 3 files changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +1 -0
index bcbcfb4deb9a2..15c69af11a684 100644
@@ -1206,6 +1206,7 @@ void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr);
 static inline void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
 {
 	virt_arch_pg_map(vm, vaddr, paddr);
+	sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift);
 }
 
 
tools/testing/selftests/kvm/lib/kvm_util.c +0 -3
index 5e6b51768f67f..473c533774898 100644
@@ -1490,8 +1490,6 @@ static vm_vaddr_t ____vm_vaddr_alloc(struct kvm_vm *vm, size_t sz,
 		pages--, vaddr += vm->page_size, paddr += vm->page_size) {
 
 		virt_pg_map(vm, vaddr, paddr);
-
-		sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift);
 	}
 
 	return vaddr_start;
@@ -1605,7 +1603,6 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 
 	while (npages--) {
 		virt_pg_map(vm, vaddr, paddr);
-		sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift);
 
 		vaddr += page_size;
 		paddr += page_size;
tools/testing/selftests/kvm/lib/x86/processor.c +2 -0
index c484e83255c70..4ee9fc844ee66 100644
@@ -285,6 +285,8 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 
 	for (i = 0; i < nr_pages; i++) {
 		__virt_pg_map(vm, vaddr, paddr, level);
+		sparsebit_set_num(vm->vpages_mapped, vaddr >> vm->page_shift,
+				  nr_bytes / PAGE_SIZE);
 
 		vaddr += pg_size;
 		paddr += pg_size;
Signature · 1 line
2.51.0.869.ge66316f041-goog
#18 lore raw

PATCH v2 12/23 KVM: selftests: Parameterize the PTE bitmasks for virt mapping functions

In reply to Yosry Ahmed

Move the PTE bitmasks into a struct and define one for x86 page tables. Move all internal functions in lib/x86/processor.c to take in and use the structs but do not expose it to external callers. Drop the 'global' bit definition as it's currently unused, but leave the 'user' bit as it will be used in coming changes.

Following changes will add support to use the virt mapping functions for EPTs and NPTs, so they will define their own bitmask structs.

Leave PHYSICAL_PAGE_MASK alone, it's fixed in all page table formats and a lot of other macros depend on it. It's tempting to move all the other macros to be per-struct instead, but it would be too much noise for little benefit.

While at it, make __vm_get_page_table_entry() static as it's not used in any other files.

No functional change intended.

---
 .../selftests/kvm/include/x86/processor.h     | 27 +++---
 .../testing/selftests/kvm/lib/x86/processor.c | 92 +++++++++++--------
 2 files changed, 69 insertions(+), 50 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +14 -13
index 51cd84b9ca664..8debe0df3ffca 100644
@@ -362,16 +362,6 @@ static inline unsigned int x86_model(unsigned int eax)
 	return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
 }
 
-/* Page table bitfield declarations */
-#define PTE_PRESENT_MASK        BIT_ULL(0)
-#define PTE_WRITABLE_MASK       BIT_ULL(1)
-#define PTE_USER_MASK           BIT_ULL(2)
-#define PTE_ACCESSED_MASK       BIT_ULL(5)
-#define PTE_DIRTY_MASK          BIT_ULL(6)
-#define PTE_LARGE_MASK          BIT_ULL(7)
-#define PTE_GLOBAL_MASK         BIT_ULL(8)
-#define PTE_NX_MASK             BIT_ULL(63)
-
 #define PHYSICAL_PAGE_MASK      GENMASK_ULL(51, 12)
 
 #define PAGE_SHIFT		12
@@ -1367,8 +1357,6 @@ static inline bool kvm_is_ignore_msrs(void)
 	return get_kvm_param_bool("ignore_msrs");
 }
 
-uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
-				    int *level);
 uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr);
 
 uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2,
@@ -1451,7 +1439,20 @@ enum pg_level {
 #define PG_SIZE_2M PG_LEVEL_SIZE(PG_LEVEL_2M)
 #define PG_SIZE_1G PG_LEVEL_SIZE(PG_LEVEL_1G)
 
-void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level);
+struct pte_masks {
+	uint64_t present;
+	uint64_t writeable;
+	uint64_t user;
+	uint64_t accessed;
+	uint64_t dirty;
+	uint64_t large;
+	uint64_t nx;
+};
+
+extern const struct pte_masks x86_pte_masks;
+
+void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
+		   int level, const struct pte_masks *masks);
 void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 		    uint64_t nr_bytes, int level);
 
tools/testing/selftests/kvm/lib/x86/processor.c +55 -37

228 lines not on this page · raw message

Signature · 1 line
2.51.0.869.ge66316f041-goog
#19 lore raw

Re: [PATCH v2 12/23] KVM: selftests: Parameterize the PTE bitmasks for virt mapping functions

Quoted 11 lines · Yosry Ahmed
tools/testing/selftests/kvm/include/x86/processor.h +0 -2
@@ -1367,8 +1357,6 @@ static inline bool kvm_is_ignore_msrs(void)
 	return get_kvm_param_bool("ignore_msrs");
 }
 
-uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
-				    int *level);
 uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr);
 
 uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2,
@@ -1451,7 +1439,20 @@ enum pg_level {
 #define PG_SIZE_2M PG_LEVEL_SIZE(PG_LEVEL_2M)

tools/testing/selftests/kvm/include/x86/processor.h enum pg_level { · line 1448

Quoted 10 lines
tools/testing/selftests/kvm/include/x86/processor.h +7 -1
 #define PG_SIZE_1G PG_LEVEL_SIZE(PG_LEVEL_1G)
 
-void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level);
+struct pte_masks {
+	uint64_t present;
+	uint64_t writeable;
+	uint64_t user;
+	uint64_t accessed;
+	uint64_t dirty;
+	uint64_t large;

Ugh, "large". I vote for "huge" or "hugepage", as that's for more ubiquitous in the kernel.

tools/testing/selftests/kvm/include/x86/processor.h enum pg_level { · line 1449

Quoted 1 line
tools/testing/selftests/kvm/include/x86/processor.h +1 -0
+	uint64_t nx;

The values themselves should be const, e.g. to communicate that they are fixed values.

Quoted 16 lines
tools/testing/selftests/kvm/lib/x86/processor.c +5 -3
+};
+
+extern const struct pte_masks x86_pte_masks;
 
-void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)
+void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
+		   int level, const struct pte_masks *masks)
 {
 	const uint64_t pg_size = PG_LEVEL_SIZE(level);
 	uint64_t *pte = &vm->pgd;
@@ -246,16 +259,16 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)
 	 * early if a hugepage was created.
 	 */
 	for (current_level = vm->pgtable_levels; current_level > PG_LEVEL_4K; current_level--) {
-		pte = virt_create_upper_pte(vm, pte, vaddr, paddr, current_level, level);
-		if (*pte & PTE_LARGE_MASK)

tools/testing/selftests/kvm/lib/x86/processor.c void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) · line 269

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/processor.c +4 -2
+		pte = virt_create_upper_pte(vm, pte, vaddr, paddr, current_level, level, masks);
+		if (*pte & masks->large)
 			return;
 	}
 
 	/* Fill in page table entry. */
-	pte = virt_get_pte(vm, pte, vaddr, PG_LEVEL_4K);
-	TEST_ASSERT(!(*pte & PTE_PRESENT_MASK),
+	pte = virt_get_pte(vm, pte, vaddr, PG_LEVEL_4K, masks);
+	TEST_ASSERT(!(*pte & masks->present),

I think accessors would help for the "read" cases? E.g.

TEST_ASSERT(!is_present_pte(mmu, *pte)

or maybe go with a slightly atypical ordering of:

	TEST_ASSERT(!is_present_pte(*pte, mmu),

The second one seems more readable.

tools/testing/selftests/kvm/lib/x86/processor.c void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) · line 271

Quoted 3 lines
tools/testing/selftests/kvm/lib/x86/processor.c +1 -1
 		    "PTE already present for 4k page at vaddr: 0x%lx", vaddr);
-	*pte = PTE_PRESENT_MASK | PTE_WRITABLE_MASK | (paddr & PHYSICAL_PAGE_MASK);
+	*pte = masks->present | masks->writeable | (paddr & PHYSICAL_PAGE_MASK);

Hrm. I don't love the masks->lowercase style, but I don't hate it either. One idea would be to add macros to grab the correct bit, e.g.

	*pte = PTE_PRESENT(mmu) | PTE_WRITABLE(mmu) | (paddr & PHYSICAL_PAGE_MASK);

Alternatively, assuming we add "struct kvm_mmu", we could grab the "pte_masks" structure locally as "m" and do this? Note sure the super-shorthand is a net positive though.

	*pte = PTE_PRESENT(m) | PTE_WRITABLE(m) | (paddr & PHYSICAL_PAGE_MASK);

Or we could YELL REALLY LOUDLY in the fields themselves, e.g.

	*pte = m->PRESENT | m->WRITABLE | (paddr & PHYSICAL_PAGE_MASK);

but that looks kinda weird to me.

I don't have a super strong preference, though I'm leaning towards accessor functions with macros for retrieving the bits.

tools/testing/selftests/kvm/lib/x86/processor.c void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) · line 274

Quoted 2 lines
tools/testing/selftests/kvm/lib/x86/processor.c +0 -0
 	/*
 	 * Neither SEV nor TDX supports shared page tables, so only the final

Hiding just out of sight is this code:

	/*
	 * Neither SEV nor TDX supports shared page tables, so only the final
	 * leaf PTE needs manually set the C/S-bit.
	 */
	if (vm_is_gpa_protected(vm, paddr))
		*pte |= vm->arch.c_bit;
	else
		*pte |= vm->arch.s_bit;

The C-bit (enCrypted) and S-bit (Shared) values need to be moved into the masks/mmu context as well. In practice, they'll both be zero when creating nested mappings since KVM doesn't support nested VMs with encrypted memory, but it's still wrong, e.g. the Shared bit doesn't exist in EPT.

#20 lore raw

Re: [PATCH v2 12/23] KVM: selftests: Parameterize the PTE bitmasks for virt mapping functions

Quoted 14 lines · Sean Christopherson
Quoted 13 lines · Yosry Ahmed
tools/testing/selftests/kvm/include/x86/processor.h +0 -2
@@ -1367,8 +1357,6 @@ static inline bool kvm_is_ignore_msrs(void)
 	return get_kvm_param_bool("ignore_msrs");
 }
 
-uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
-				    int *level);
 uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr);
 
 uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2,
@@ -1451,7 +1439,20 @@ enum pg_level {
 #define PG_SIZE_2M PG_LEVEL_SIZE(PG_LEVEL_2M)
 #define PG_SIZE_1G PG_LEVEL_SIZE(PG_LEVEL_1G)
 
Quoted 11 lines

tools/testing/selftests/kvm/include/x86/processor.h enum pg_level { · line 1448

Quoted 8 lines
tools/testing/selftests/kvm/include/x86/processor.h +7 -1
-void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level);
+struct pte_masks {
+	uint64_t present;
+	uint64_t writeable;
+	uint64_t user;
+	uint64_t accessed;
+	uint64_t dirty;
+	uint64_t large;

Ugh, "large". I vote for "huge" or "hugepage", as that's for more ubiquitous in the kernel.

"huge" sounds good.

Quoted 35 lines

tools/testing/selftests/kvm/include/x86/processor.h enum pg_level { · line 1449

Quoted 2 lines
tools/testing/selftests/kvm/include/x86/processor.h +1 -0
+	uint64_t nx;

The values themselves should be const, e.g. to communicate that they are fixed values.

Quoted 16 lines
tools/testing/selftests/kvm/lib/x86/processor.c +5 -3
+};
+
+extern const struct pte_masks x86_pte_masks;
 
-void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)
+void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
+		   int level, const struct pte_masks *masks)
 {
 	const uint64_t pg_size = PG_LEVEL_SIZE(level);
 	uint64_t *pte = &vm->pgd;
@@ -246,16 +259,16 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)
 	 * early if a hugepage was created.
 	 */
 	for (current_level = vm->pgtable_levels; current_level > PG_LEVEL_4K; current_level--) {
-		pte = virt_create_upper_pte(vm, pte, vaddr, paddr, current_level, level);
-		if (*pte & PTE_LARGE_MASK)

tools/testing/selftests/kvm/lib/x86/processor.c void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) · line 269

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/processor.c +4 -2
+		pte = virt_create_upper_pte(vm, pte, vaddr, paddr, current_level, level, masks);
+		if (*pte & masks->large)
 			return;
 	}
 
 	/* Fill in page table entry. */
-	pte = virt_get_pte(vm, pte, vaddr, PG_LEVEL_4K);
-	TEST_ASSERT(!(*pte & PTE_PRESENT_MASK),
+	pte = virt_get_pte(vm, pte, vaddr, PG_LEVEL_4K, masks);
+	TEST_ASSERT(!(*pte & masks->present),

I think accessors would help for the "read" cases? E.g.

Quoted 7 lines

TEST_ASSERT(!is_present_pte(mmu, *pte)

or maybe go with a slightly atypical ordering of:

	TEST_ASSERT(!is_present_pte(*pte, mmu),

The second one seems more readable.

How about is_present_pte(mmu, pte)? Any objections to passing in the pointer?

Quoted 9 lines

tools/testing/selftests/kvm/lib/x86/processor.c void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) · line 271

Quoted 4 lines
tools/testing/selftests/kvm/lib/x86/processor.c +1 -1
 		    "PTE already present for 4k page at vaddr: 0x%lx", vaddr);
-	*pte = PTE_PRESENT_MASK | PTE_WRITABLE_MASK | (paddr & PHYSICAL_PAGE_MASK);
+	*pte = masks->present | masks->writeable | (paddr & PHYSICAL_PAGE_MASK);

Hrm. I don't love the masks->lowercase style, but I don't hate it either. One idea would be to add macros to grab the correct bit, e.g.

	*pte = PTE_PRESENT(mmu) | PTE_WRITABLE(mmu) | (paddr & PHYSICAL_PAGE_MASK);

I like this one best, I think. Abstracts the masks away completely, which is nice.

Quoted 30 lines

Alternatively, assuming we add "struct kvm_mmu", we could grab the "pte_masks" structure locally as "m" and do this? Note sure the super-shorthand is a net positive though.

	*pte = PTE_PRESENT(m) | PTE_WRITABLE(m) | (paddr & PHYSICAL_PAGE_MASK);

Or we could YELL REALLY LOUDLY in the fields themselves, e.g.

	*pte = m->PRESENT | m->WRITABLE | (paddr & PHYSICAL_PAGE_MASK);

but that looks kinda weird to me.

I don't have a super strong preference, though I'm leaning towards accessor functions with macros for retrieving the bits.

tools/testing/selftests/kvm/lib/x86/processor.c void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) · line 274

Quoted 2 lines
tools/testing/selftests/kvm/lib/x86/processor.c +0 -0
 	/*
 	 * Neither SEV nor TDX supports shared page tables, so only the final

Hiding just out of sight is this code:

	/*
	 * Neither SEV nor TDX supports shared page tables, so only the final
	 * leaf PTE needs manually set the C/S-bit.
	 */
	if (vm_is_gpa_protected(vm, paddr))
		*pte |= vm->arch.c_bit;
	else
		*pte |= vm->arch.s_bit;
Quoted 4 lines

The C-bit (enCrypted) and S-bit (Shared) values need to be moved into the masks/mmu context as well. In practice, they'll both be zero when creating nested mappings since KVM doesn't support nested VMs with encrypted memory, but it's still wrong, e.g. the Shared bit doesn't exist in EPT.

Will add, thanks!

#21 lore raw

PATCH v2 13/23 KVM: selftests: Pass the root GPA into virt_get_pte()

In reply to Yosry Ahmed

Instead of hardcoding the root check against vm->pgd, pass the root_gpa into virt_get_pte(). There's a subtle change here, instead of checking that the parent pointer has the address of vm->pgd, check if the value pointed at by the parent pointer is the root_gpa. No change in behavior expected, but this will be required for following changes that generalize __virt_pg_map() to other MMUs.

---
 tools/testing/selftests/kvm/lib/x86/processor.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
tools/testing/selftests/kvm/lib/x86/processor.c +10 -7
index 8a838f208abe4..92a2b5aefd880 100644
@@ -179,15 +179,15 @@ const struct pte_masks x86_pte_masks = {
 	.nx		=	BIT_ULL(63),
 };
 
-static void *virt_get_pte(struct kvm_vm *vm, uint64_t *parent_pte,
-			  uint64_t vaddr, int level,
+static void *virt_get_pte(struct kvm_vm *vm, vm_paddr_t root_gpa,
+			  uint64_t *parent_pte, uint64_t vaddr, int level,
 			  const struct pte_masks *masks)
 {
 	uint64_t pt_gpa = PTE_GET_PA(*parent_pte);
 	uint64_t *page_table = addr_gpa2hva(vm, pt_gpa);
 	int index = (vaddr >> PG_LEVEL_SHIFT(level)) & 0x1ffu;
 
-	TEST_ASSERT((*parent_pte & masks->present) || parent_pte == &vm->pgd,
+	TEST_ASSERT((*parent_pte == root_gpa) || (*parent_pte & masks->present),
 		    "Parent PTE (level %d) not PRESENT for gva: 0x%08lx",
 		    level + 1, vaddr);
 
@@ -195,6 +195,7 @@ static void *virt_get_pte(struct kvm_vm *vm, uint64_t *parent_pte,
 }
 
 static uint64_t *virt_create_upper_pte(struct kvm_vm *vm,
+				       vm_paddr_t root_gpa,
 				       uint64_t *parent_pte,
 				       uint64_t vaddr,
 				       uint64_t paddr,
@@ -202,7 +203,8 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm,
 				       int target_level,
 				       const struct pte_masks *masks)
 {
-	uint64_t *pte = virt_get_pte(vm, parent_pte, vaddr, current_level, masks);
+	uint64_t *pte = virt_get_pte(vm, root_gpa, parent_pte,
+				     vaddr, current_level, masks);
 
 	paddr = vm_untag_gpa(vm, paddr);
 
@@ -259,13 +261,14 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 	 * early if a hugepage was created.
 	 */
 	for (current_level = vm->pgtable_levels; current_level > PG_LEVEL_4K; current_level--) {
-		pte = virt_create_upper_pte(vm, pte, vaddr, paddr, current_level, level, masks);
+		pte = virt_create_upper_pte(vm, vm->pgd, pte, vaddr, paddr,
+					    current_level, level, masks);
 		if (*pte & masks->large)
 			return;
 	}
 
 	/* Fill in page table entry. */
-	pte = virt_get_pte(vm, pte, vaddr, PG_LEVEL_4K, masks);
+	pte = virt_get_pte(vm, vm->pgd, pte, vaddr, PG_LEVEL_4K, masks);
 	TEST_ASSERT(!(*pte & masks->present),
 		    "PTE already present for 4k page at vaddr: 0x%lx", vaddr);
 	*pte = masks->present | masks->writeable | (paddr & PHYSICAL_PAGE_MASK);
@@ -348,7 +351,7 @@ static uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm,
 		"Canonical check failed.  The virtual address is invalid.");
 
 	for (current_level = vm->pgtable_levels; current_level >= PG_LEVEL_4K; current_level--) {
-		pte = virt_get_pte(vm, pte, vaddr, current_level, masks);
+		pte = virt_get_pte(vm, vm->pgd, pte, vaddr, current_level, masks);
 		if (vm_is_target_pte(pte, level, current_level, masks))
 			return pte;
 	}
Signature · 1 line
2.51.0.869.ge66316f041-goog
#22 lore raw

PATCH v2 14/23 KVM: selftests: Pass the root GPA into __virt_pg_map()

In reply to Yosry Ahmed

Pass the root_gpa into __virt_pg_map() instead of assuming vm->pgd in preparation for using it with other MMUs.

No functional change intended.

---
 .../testing/selftests/kvm/include/x86/processor.h  |  4 ++--
 tools/testing/selftests/kvm/lib/x86/processor.c    | 14 +++++++-------
 2 files changed, 9 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +2 -2
index 8debe0df3ffca..2f0d83b6e5952 100644
@@ -1451,8 +1451,8 @@ struct pte_masks {
 
 extern const struct pte_masks x86_pte_masks;
 
-void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
-		   int level, const struct pte_masks *masks);
+void __virt_pg_map(struct kvm_vm *vm, vm_paddr_t root_gpa, uint64_t vaddr,
+		   uint64_t paddr, int level, const struct pte_masks *masks);
 void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 		    uint64_t nr_bytes, int level);
 
tools/testing/selftests/kvm/lib/x86/processor.c +7 -7
index 92a2b5aefd880..13e9376d5f545 100644
@@ -230,11 +230,11 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm,
 	return pte;
 }
 
-void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
-		   int level, const struct pte_masks *masks)
+void __virt_pg_map(struct kvm_vm *vm, vm_paddr_t root_gpa, uint64_t vaddr,
+		   uint64_t paddr, int level, const struct pte_masks *masks)
 {
 	const uint64_t pg_size = PG_LEVEL_SIZE(level);
-	uint64_t *pte = &vm->pgd;
+	uint64_t *pte = &root_gpa;
 	int current_level;
 
 	TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K ||
@@ -261,14 +261,14 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 	 * early if a hugepage was created.
 	 */
 	for (current_level = vm->pgtable_levels; current_level > PG_LEVEL_4K; current_level--) {
-		pte = virt_create_upper_pte(vm, vm->pgd, pte, vaddr, paddr,
+		pte = virt_create_upper_pte(vm, root_gpa, pte, vaddr, paddr,
 					    current_level, level, masks);
 		if (*pte & masks->large)
 			return;
 	}
 
 	/* Fill in page table entry. */
-	pte = virt_get_pte(vm, vm->pgd, pte, vaddr, PG_LEVEL_4K, masks);
+	pte = virt_get_pte(vm, root_gpa, pte, vaddr, PG_LEVEL_4K, masks);
 	TEST_ASSERT(!(*pte & masks->present),
 		    "PTE already present for 4k page at vaddr: 0x%lx", vaddr);
 	*pte = masks->present | masks->writeable | (paddr & PHYSICAL_PAGE_MASK);
@@ -285,7 +285,7 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 
 void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
 {
-	__virt_pg_map(vm, vaddr, paddr, PG_LEVEL_4K, &x86_pte_masks);
+	__virt_pg_map(vm, vm->pgd, vaddr, paddr, PG_LEVEL_4K, &x86_pte_masks);
 }
 
 void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
@@ -300,7 +300,7 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 		    nr_bytes, pg_size);
 
 	for (i = 0; i < nr_pages; i++) {
-		__virt_pg_map(vm, vaddr, paddr, level, &x86_pte_masks);
+		__virt_pg_map(vm, vm->pgd, vaddr, paddr, level, &x86_pte_masks);
 		sparsebit_set_num(vm->vpages_mapped, vaddr >> vm->page_shift,
 				  nr_bytes / PAGE_SIZE);
 
Signature · 1 line
2.51.0.869.ge66316f041-goog
#23 lore raw

PATCH v2 15/23 KVM: selftests: Stop setting AD bits on nested EPTs on creation

In reply to Yosry Ahmed

When new nested EPTs are created, the AD bits are set. This was introduced by commit 094444204570 ("selftests: kvm: add test for dirty logging inside nested guests"), which introduced vmx_dirty_log_test.

It's unclear why that was needed at the time, but regardless, the test seems to pass without them so probably no longer needed. dirty_log_perf_test (with -n to run in L2) also passes, and these are the only tests currently using nested EPT mappings.

---
 tools/testing/selftests/kvm/lib/x86/vmx.c | 8 --------
 1 file changed, 8 deletions(-)
tools/testing/selftests/kvm/lib/x86/vmx.c +0 -8
index f0023a3b0137e..36e60016fa7b2 100644
@@ -436,14 +436,6 @@ void __nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm,
 
 		pt = addr_gpa2hva(vm, pte->address * vm->page_size);
 	}
-
-	/*
-	 * For now mark these as accessed and dirty because the only
-	 * testcase we have needs that.  Can be reconsidered later.
-	 */
-	pte->accessed = true;
-	pte->dirty = true;
-
 }
 
 void nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm,
Signature · 1 line
2.51.0.869.ge66316f041-goog
#24 lore raw

PATCH v2 16/23 KVM: selftests: Use __virt_pg_map() for nested EPTs

In reply to Yosry Ahmed

__nested_pg_map() bears a lot of resemblence to __virt_pg_map(). The main difference is using the EPT struct overlay instead of the PTE masks. Now that PTE masks are passed into __virt_pg_map() as a struct, define a similar struct for EPTs and use __virt_pg_map() instead of __nested_pg_map().

EPTs have no 'present' or 'user' bits, so use the 'readable' bit instead like shadow_{present/user}_mask, ignoring the fact that entries can be present and not readable if the CPU has VMX_EPT_EXECUTE_ONLY_BIT. This is simple and sufficient for testing.

Opportunistically drop nested_pg_map() since it has no callers.

Add an executable bitmask to struct pte_masks, and update __virt_pg_map() and friends to set the bit on newly created entries to match the EPT behavior. It's a noop for x86 page tables.

Another benefit of reusing the code is having separate handling for upper-level PTEs vs 4K PTEs, which avoids some quirks like setting the large bit on a 4K PTE.

---
 .../selftests/kvm/include/x86/processor.h     |   1 +
 tools/testing/selftests/kvm/include/x86/vmx.h |   4 +-
 .../testing/selftests/kvm/lib/x86/processor.c |   9 +-
 tools/testing/selftests/kvm/lib/x86/vmx.c     | 113 +++---------------
 4 files changed, 27 insertions(+), 100 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +1 -0
index 2f0d83b6e5952..0d6d335d309ef 100644
@@ -1447,6 +1447,7 @@ struct pte_masks {
 	uint64_t dirty;
 	uint64_t large;
 	uint64_t nx;
+	uint64_t x;
 };
 
 extern const struct pte_masks x86_pte_masks;
tools/testing/selftests/kvm/include/x86/vmx.h +2 -2
index 96e2b4c630a9b..5aa14ceed050a 100644
@@ -99,6 +99,8 @@
 #define VMX_EPT_VPID_CAP_1G_PAGES		0x00020000
 #define VMX_EPT_VPID_CAP_AD_BITS		0x00200000
 
+extern const struct pte_masks ept_pte_masks;
+
 #define EXIT_REASON_FAILED_VMENTRY	0x80000000
 
 enum vmcs_field {
@@ -559,8 +561,6 @@ bool load_vmcs(struct vmx_pages *vmx);
 
 bool ept_1g_pages_supported(void);
 
-void nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm,
-		   uint64_t nested_paddr, uint64_t paddr);
 void nested_map(struct vmx_pages *vmx, struct kvm_vm *vm,
 		 uint64_t nested_paddr, uint64_t paddr, uint64_t size);
 void nested_map_memslot(struct vmx_pages *vmx, struct kvm_vm *vm,
tools/testing/selftests/kvm/lib/x86/processor.c +7 -2
index 13e9376d5f545..caad8a9b3f067 100644
@@ -177,6 +177,7 @@ const struct pte_masks x86_pte_masks = {
 	.dirty		=	BIT_ULL(6),
 	.large		=	BIT_ULL(7),
 	.nx		=	BIT_ULL(63),
+	.x		=	0,
 };
 
 static void *virt_get_pte(struct kvm_vm *vm, vm_paddr_t root_gpa,
@@ -209,7 +210,7 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm,
 	paddr = vm_untag_gpa(vm, paddr);
 
 	if (!(*pte & masks->present)) {
-		*pte = masks->present | masks->writeable;
+		*pte = masks->present | masks->writeable | masks->x;
 		if (current_level == target_level)
 			*pte |= masks->large | (paddr & PHYSICAL_PAGE_MASK);
 		else
@@ -256,6 +257,9 @@ void __virt_pg_map(struct kvm_vm *vm, vm_paddr_t root_gpa, uint64_t vaddr,
 	TEST_ASSERT(vm_untag_gpa(vm, paddr) == paddr,
 		    "Unexpected bits in paddr: %lx", paddr);
 
+	TEST_ASSERT(!masks->x || !masks->nx,
+		    "X and NX bit masks cannot be used simultaneously");
+
 	/*
 	 * Allocate upper level page tables, if not already present.  Return
 	 * early if a hugepage was created.
@@ -271,7 +275,8 @@ void __virt_pg_map(struct kvm_vm *vm, vm_paddr_t root_gpa, uint64_t vaddr,
 	pte = virt_get_pte(vm, root_gpa, pte, vaddr, PG_LEVEL_4K, masks);
 	TEST_ASSERT(!(*pte & masks->present),
 		    "PTE already present for 4k page at vaddr: 0x%lx", vaddr);
-	*pte = masks->present | masks->writeable | (paddr & PHYSICAL_PAGE_MASK);
+	*pte = masks->present | masks->writeable | masks->x
+		| (paddr & PHYSICAL_PAGE_MASK);
 
 	/*
 	 * Neither SEV nor TDX supports shared page tables, so only the final
tools/testing/selftests/kvm/lib/x86/vmx.c +17 -96

131 lines not on this page · raw message

Signature · 1 line
2.51.0.869.ge66316f041-goog
#25 lore raw

PATCH v2 17/23 KVM: selftests: Kill eptPageTablePointer

In reply to Yosry Ahmed

Replace the struct overlay with explicit bitmasks, which is clearer and less error-prone. See commit f18b4aebe107 ("kvm: selftests: do not use bitfields larger than 32-bits for PTEs") for an example of why bitfields are not preferrable.

Remove the unused PAGE_SHIFT_4K definition while at it.

No functional change intended.

---
 tools/testing/selftests/kvm/lib/x86/vmx.c | 37 +++++++++++------------
 1 file changed, 17 insertions(+), 20 deletions(-)
tools/testing/selftests/kvm/lib/x86/vmx.c +17 -20
index 46a491eb083c9..75996fc00501e 100644
@@ -10,10 +10,16 @@
 #include "processor.h"
 #include "vmx.h"
 
-#define PAGE_SHIFT_4K  12
-
 #define KVM_EPT_PAGE_TABLE_MIN_PADDR 0x1c0000
 
+#define EPTP_MT_SHIFT		0 /* EPTP memtype bits 2:0 */
+#define EPTP_PWL_SHIFT		3 /* EPTP page walk length bits 5:3 */
+#define EPTP_AD_ENABLED_SHIFT	6 /* EPTP AD enabled bit 6 */
+
+#define EPTP_WB			(X86_MEMTYPE_WB << EPTP_MT_SHIFT)
+#define EPTP_PWL_4		(3ULL << EPTP_PWL_SHIFT) /* PWL is (levels - 1) */
+#define EPTP_AD_ENABLED		(1ULL << EPTP_AD_ENABLED_SHIFT)
+
 bool enable_evmcs;
 
 struct hv_enlightened_vmcs *current_evmcs;
@@ -36,14 +42,6 @@ const struct pte_masks ept_pte_masks = {
 	.nx		=	0,
 };
 
-struct eptPageTablePointer {
-	uint64_t memory_type:3;
-	uint64_t page_walk_length:3;
-	uint64_t ad_enabled:1;
-	uint64_t reserved_11_07:5;
-	uint64_t address:40;
-	uint64_t reserved_63_52:12;
-};
 int vcpu_enable_evmcs(struct kvm_vcpu *vcpu)
 {
 	uint16_t evmcs_ver;
@@ -198,16 +196,15 @@ static inline void init_vmcs_control_fields(struct vmx_pages *vmx)
 	vmwrite(PIN_BASED_VM_EXEC_CONTROL, rdmsr(MSR_IA32_VMX_TRUE_PINBASED_CTLS));
 
 	if (vmx->eptp_gpa) {
-		uint64_t ept_paddr;
-		struct eptPageTablePointer eptp = {
-			.memory_type = X86_MEMTYPE_WB,
-			.page_walk_length = 3, /* + 1 */
-			.ad_enabled = ept_vpid_cap_supported(VMX_EPT_VPID_CAP_AD_BITS),
-			.address = vmx->eptp_gpa >> PAGE_SHIFT_4K,
-		};
-
-		memcpy(&ept_paddr, &eptp, sizeof(ept_paddr));
-		vmwrite(EPT_POINTER, ept_paddr);
+		uint64_t eptp = vmx->eptp_gpa | EPTP_WB | EPTP_PWL_4;
+
+		TEST_ASSERT((vmx->eptp_gpa & ~PHYSICAL_PAGE_MASK) == 0,
+			    "Illegal bits set in vmx->eptp_gpa");
+
+		if (ept_vpid_cap_supported(VMX_EPT_VPID_CAP_AD_BITS))
+			eptp |= EPTP_AD_ENABLED;
+
+		vmwrite(EPT_POINTER, eptp);
 		sec_exec_ctl |= SECONDARY_EXEC_ENABLE_EPT;
 	}
 
Signature · 1 line
2.51.0.869.ge66316f041-goog
#26 lore raw

PATCH v2 18/23 KVM: selftests: Generalize nested mapping functions

In reply to Yosry Ahmed

Instead of passing in a pointer to struct vmx_pages, pass in the GPA of the root of the EPTs, as that's the only member being used. Furthermore, only use ept_pte_masks for VMX, and use x86_pte_masks otherwise (which is what NPT uses).

This is in preparation of supporting NPTs as well.

No functional change intended.

---
 tools/testing/selftests/kvm/include/x86/vmx.h |  6 +++---
 .../testing/selftests/kvm/lib/x86/memstress.c |  4 ++--
 tools/testing/selftests/kvm/lib/x86/vmx.c     | 20 ++++++++++---------
 .../selftests/kvm/x86/vmx_dirty_log_test.c    |  6 +++---
 4 files changed, 19 insertions(+), 17 deletions(-)
tools/testing/selftests/kvm/include/x86/vmx.h +3 -3
index 5aa14ceed050a..4429e83e1f52c 100644
@@ -561,11 +561,11 @@ bool load_vmcs(struct vmx_pages *vmx);
 
 bool ept_1g_pages_supported(void);
 
-void nested_map(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_map(struct kvm_vm *vm, vm_paddr_t root_gpa,
 		 uint64_t nested_paddr, uint64_t paddr, uint64_t size);
-void nested_map_memslot(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_map_memslot(struct kvm_vm *vm, vm_paddr_t root_gpa,
 			uint32_t memslot);
-void nested_identity_map_1g(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_identity_map_1g(struct kvm_vm *vm, vm_paddr_t root_gpa,
 			    uint64_t addr, uint64_t size);
 bool kvm_cpu_has_ept(void);
 void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm);
tools/testing/selftests/kvm/lib/x86/memstress.c +2 -2
index 0b1f288ad5564..5ca970a8a5c14 100644
@@ -70,11 +70,11 @@ void memstress_setup_ept(struct vmx_pages *vmx, struct kvm_vm *vm)
 	 * KVM can shadow the EPT12 with the maximum huge page size supported
 	 * by the backing source.
 	 */
-	nested_identity_map_1g(vmx, vm, 0, 0x100000000ULL);
+	nested_identity_map_1g(vm, vmx->eptp_gpa, 0, 0x100000000ULL);
 
 	start = align_down(memstress_args.gpa, PG_SIZE_1G);
 	end = align_up(memstress_args.gpa + memstress_args.size, PG_SIZE_1G);
-	nested_identity_map_1g(vmx, vm, start, end - start);
+	nested_identity_map_1g(vm, vmx->eptp_gpa, start, end - start);
 }
 
 void memstress_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu *vcpus[])
tools/testing/selftests/kvm/lib/x86/vmx.c +11 -9
index 75996fc00501e..0573b3ea717cb 100644
@@ -378,34 +378,36 @@ void prepare_vmcs(struct vmx_pages *vmx, void *guest_rip, void *guest_rsp)
  * Within the VM given by vm, creates a nested guest translation for the
  * page range starting at nested_paddr to the page range starting at paddr.
  */
-void __nested_map(struct vmx_pages *vmx, struct kvm_vm *vm,
+void __nested_map(struct kvm_vm *vm, vm_paddr_t root_gpa,
 		  uint64_t nested_paddr, uint64_t paddr, uint64_t size,
 		  int level)
 {
 	size_t page_size = PG_LEVEL_SIZE(level);
 	size_t npages = size / page_size;
+	const struct pte_masks *masks;
+
+	masks = kvm_cpu_has(X86_FEATURE_VMX) ? &ept_pte_masks : &x86_pte_masks;
 
 	TEST_ASSERT(nested_paddr + size > nested_paddr, "Vaddr overflow");
 	TEST_ASSERT(paddr + size > paddr, "Paddr overflow");
 
 	while (npages--) {
-		__virt_pg_map(vm, vmx->eptp_gpa, nested_paddr, paddr,
-			      level, &ept_pte_masks);
+		__virt_pg_map(vm, root_gpa, nested_paddr, paddr, level, masks);
 		nested_paddr += page_size;
 		paddr += page_size;
 	}
 }
 
-void nested_map(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_map(struct kvm_vm *vm, vm_paddr_t root_gpa,
 		uint64_t nested_paddr, uint64_t paddr, uint64_t size)
 {
-	__nested_map(vmx, vm, nested_paddr, paddr, size, PG_LEVEL_4K);
+	__nested_map(vm, root_gpa, nested_paddr, paddr, size, PG_LEVEL_4K);
 }
 
 /* Prepare an identity extended page table that maps all the
  * physical pages in VM.
  */
-void nested_map_memslot(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_map_memslot(struct kvm_vm *vm, vm_paddr_t root_gpa,
 			uint32_t memslot)
 {
 	sparsebit_idx_t i, last;
@@ -419,7 +421,7 @@ void nested_map_memslot(struct vmx_pages *vmx, struct kvm_vm *vm,
 		if (i > last)
 			break;
 
-		nested_map(vmx, vm,
+		nested_map(vm, root_gpa,
 			   (uint64_t)i << vm->page_shift,
 			   (uint64_t)i << vm->page_shift,
 			   1 << vm->page_shift);
@@ -427,10 +429,10 @@ void nested_map_memslot(struct vmx_pages *vmx, struct kvm_vm *vm,
 }
 
 /* Identity map a region with 1GiB Pages. */
-void nested_identity_map_1g(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_identity_map_1g(struct kvm_vm *vm, vm_paddr_t root_gpa,
 			    uint64_t addr, uint64_t size)
 {
-	__nested_map(vmx, vm, addr, addr, size, PG_LEVEL_1G);
+	__nested_map(vm, root_gpa, addr, addr, size, PG_LEVEL_1G);
 }
 
 bool kvm_cpu_has_ept(void)
tools/testing/selftests/kvm/x86/vmx_dirty_log_test.c +3 -3
index 98cb6bdab3e6d..e54e6111164e7 100644
@@ -121,9 +121,9 @@ static void test_vmx_dirty_log(bool enable_ept)
 	 */
 	if (enable_ept) {
 		prepare_eptp(vmx, vm);
-		nested_map_memslot(vmx, vm, 0);
-		nested_map(vmx, vm, NESTED_TEST_MEM1, GUEST_TEST_MEM, PAGE_SIZE);
-		nested_map(vmx, vm, NESTED_TEST_MEM2, GUEST_TEST_MEM, PAGE_SIZE);
+		nested_map_memslot(vm, vmx->eptp_gpa, 0);
+		nested_map(vm, vmx->eptp_gpa, NESTED_TEST_MEM1, GUEST_TEST_MEM, PAGE_SIZE);
+		nested_map(vm, vmx->eptp_gpa, NESTED_TEST_MEM2, GUEST_TEST_MEM, PAGE_SIZE);
 	}
 
 	bmap = bitmap_zalloc(TEST_MEM_PAGES);
Signature · 1 line
2.51.0.869.ge66316f041-goog
#27 lore raw

Re: [PATCH v2 18/23] KVM: selftests: Generalize nested mapping functions

Quoted 24 lines · Yosry Ahmed

Instead of passing in a pointer to struct vmx_pages, pass in the GPA of the root of the EPTs, as that's the only member being used. Furthermore, only use ept_pte_masks for VMX, and use x86_pte_masks otherwise (which is what NPT uses).

This is in preparation of supporting NPTs as well.

No functional change intended.

---
 tools/testing/selftests/kvm/include/x86/vmx.h |  6 +++---
 .../testing/selftests/kvm/lib/x86/memstress.c |  4 ++--
 tools/testing/selftests/kvm/lib/x86/vmx.c     | 20 ++++++++++---------
 .../selftests/kvm/x86/vmx_dirty_log_test.c    |  6 +++---
 4 files changed, 19 insertions(+), 17 deletions(-)
tools/testing/selftests/kvm/include/x86/vmx.h +0 -0
index 5aa14ceed050a..4429e83e1f52c 100644
@@ -561,11 +561,11 @@ bool load_vmcs(struct vmx_pages *vmx);
 
 bool ept_1g_pages_supported(void);

tools/testing/selftests/kvm/include/x86/vmx.h bool load_vmcs(struct vmx_pages *vmx); · line 569

Quoted 10 lines
tools/testing/selftests/kvm/include/x86/vmx.h +3 -3
 
-void nested_map(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_map(struct kvm_vm *vm, vm_paddr_t root_gpa,
 		 uint64_t nested_paddr, uint64_t paddr, uint64_t size);
-void nested_map_memslot(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_map_memslot(struct kvm_vm *vm, vm_paddr_t root_gpa,
 			uint32_t memslot);
-void nested_identity_map_1g(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_identity_map_1g(struct kvm_vm *vm, vm_paddr_t root_gpa,
 			    uint64_t addr, uint64_t size);

Ugh, "nested" is a bad namespace. Running L2 doesn't strictly require nested TDP, and the operations themselves are non-nested, in the sense that we're modifying stage-2 / TDP page tables.

My vote would be to do a rename to either stage2_pg_map() or tdp_pg_map()

#28 lore raw

Re: [PATCH v2 18/23] KVM: selftests: Generalize nested mapping functions

Quoted 40 lines · Sean Christopherson
Quoted 24 lines · Yosry Ahmed

Instead of passing in a pointer to struct vmx_pages, pass in the GPA of the root of the EPTs, as that's the only member being used. Furthermore, only use ept_pte_masks for VMX, and use x86_pte_masks otherwise (which is what NPT uses).

This is in preparation of supporting NPTs as well.

No functional change intended.

---
 tools/testing/selftests/kvm/include/x86/vmx.h |  6 +++---
 .../testing/selftests/kvm/lib/x86/memstress.c |  4 ++--
 tools/testing/selftests/kvm/lib/x86/vmx.c     | 20 ++++++++++---------
 .../selftests/kvm/x86/vmx_dirty_log_test.c    |  6 +++---
 4 files changed, 19 insertions(+), 17 deletions(-)
tools/testing/selftests/kvm/include/x86/vmx.h +0 -0
index 5aa14ceed050a..4429e83e1f52c 100644
@@ -561,11 +561,11 @@ bool load_vmcs(struct vmx_pages *vmx);
 
 bool ept_1g_pages_supported(void);

tools/testing/selftests/kvm/include/x86/vmx.h bool load_vmcs(struct vmx_pages *vmx); · line 569

Quoted 10 lines
tools/testing/selftests/kvm/include/x86/vmx.h +3 -3
 
-void nested_map(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_map(struct kvm_vm *vm, vm_paddr_t root_gpa,
 		 uint64_t nested_paddr, uint64_t paddr, uint64_t size);
-void nested_map_memslot(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_map_memslot(struct kvm_vm *vm, vm_paddr_t root_gpa,
 			uint32_t memslot);
-void nested_identity_map_1g(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_identity_map_1g(struct kvm_vm *vm, vm_paddr_t root_gpa,
 			    uint64_t addr, uint64_t size);

Ugh, "nested" is a bad namespace. Running L2 doesn't strictly require nested TDP, and the operations themselves are non-nested, in the sense that we're modifying stage-2 / TDP page tables.

Quoted 1 line

My vote would be to do a rename to either stage2_pg_map() or tdp_pg_map()

It is 'nested' in the since that we are creating mappings in nested TDP (i.e. not L0's TDP), but I guess that doesn't make much sense given that most selftests library functions are already referring to the guest anyway.

I will use tdp_pg_map() only because that's the common x86 terminology (although I do like stage2 better in general).

#29 lore raw

PATCH v2 19/23 KVM: selftests: Move nested MMU mapping functions outside of vmx.c

In reply to Yosry Ahmed

Now that the functions are no longer VMX-specific, move them to processor.c. Expose ept_pte_masks in vmx.h to make it accessible by __nested_map().

No functional change intended.

---
 .../selftests/kvm/include/x86/processor.h     |  7 ++
 tools/testing/selftests/kvm/include/x86/vmx.h |  8 +-
 .../testing/selftests/kvm/lib/x86/processor.c | 75 +++++++++++++++++++
 tools/testing/selftests/kvm/lib/x86/vmx.c     | 74 ------------------
 4 files changed, 84 insertions(+), 80 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +7 -0
index 0d6d335d309ef..13e8f4a1f589d 100644
@@ -1457,6 +1457,13 @@ void __virt_pg_map(struct kvm_vm *vm, vm_paddr_t root_gpa, uint64_t vaddr,
 void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 		    uint64_t nr_bytes, int level);
 
+void nested_map(struct kvm_vm *vm, vm_paddr_t root_gpa,
+		 uint64_t nested_paddr, uint64_t paddr, uint64_t size);
+void nested_map_memslot(struct kvm_vm *vm, vm_paddr_t root_gpa,
+			uint32_t memslot);
+void nested_identity_map_1g(struct kvm_vm *vm, vm_paddr_t root_gpa,
+			    uint64_t addr, uint64_t size);
+
 /*
  * Basic CPU control in CR0
  */
tools/testing/selftests/kvm/include/x86/vmx.h +2 -6
index 4429e83e1f52c..b832774d99cdb 100644
@@ -559,14 +559,10 @@ bool prepare_for_vmx_operation(struct vmx_pages *vmx);
 void prepare_vmcs(struct vmx_pages *vmx, void *guest_rip, void *guest_rsp);
 bool load_vmcs(struct vmx_pages *vmx);
 
+extern const struct pte_masks ept_pte_masks;
+
 bool ept_1g_pages_supported(void);
 
-void nested_map(struct kvm_vm *vm, vm_paddr_t root_gpa,
-		 uint64_t nested_paddr, uint64_t paddr, uint64_t size);
-void nested_map_memslot(struct kvm_vm *vm, vm_paddr_t root_gpa,
-			uint32_t memslot);
-void nested_identity_map_1g(struct kvm_vm *vm, vm_paddr_t root_gpa,
-			    uint64_t addr, uint64_t size);
 bool kvm_cpu_has_ept(void);
 void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm);
 void prepare_virtualize_apic_accesses(struct vmx_pages *vmx, struct kvm_vm *vm);
tools/testing/selftests/kvm/lib/x86/processor.c +75 -0

87 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86/vmx.c +0 -74

80 lines not on this page · raw message

Signature · 1 line
2.51.0.869.ge66316f041-goog
#30 lore raw

PATCH v2 20/23 KVM: selftests: Stop passing a memslot to nested_map_memslot()

In reply to Yosry Ahmed

On x86, KVM selftests use memslot 0 for all the default regions used by the test infrastructure. This is an implementation detail. nested_map_memslot() is currently used to map the default regions by explicitly passing slot 0, which leaks the library implementation into the caller.

Rename the function to a very verbose nested_identity_map_default_memslots() to reflect what it actually does. Add an assertion that only memslot 0 is being used so that the implementation does not change from under us.

No functional change intended.

---
 tools/testing/selftests/kvm/include/x86/processor.h  |  3 +--
 tools/testing/selftests/kvm/lib/x86/processor.c      | 11 +++++++----
 tools/testing/selftests/kvm/x86/vmx_dirty_log_test.c |  2 +-
 3 files changed, 9 insertions(+), 7 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +1 -2
index 13e8f4a1f589d..2608152b2fa27 100644
@@ -1459,8 +1459,7 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 
 void nested_map(struct kvm_vm *vm, vm_paddr_t root_gpa,
 		 uint64_t nested_paddr, uint64_t paddr, uint64_t size);
-void nested_map_memslot(struct kvm_vm *vm, vm_paddr_t root_gpa,
-			uint32_t memslot);
+void nested_identity_map_default_memslots(struct kvm_vm *vm, vm_paddr_t root_gpa);
 void nested_identity_map_1g(struct kvm_vm *vm, vm_paddr_t root_gpa,
 			    uint64_t addr, uint64_t size);
 
tools/testing/selftests/kvm/lib/x86/processor.c +7 -4
index 1725f8fde2aa5..958389ec1722d 100644
@@ -496,12 +496,15 @@ void nested_map(struct kvm_vm *vm, vm_paddr_t root_gpa,
 /* Prepare an identity extended page table that maps all the
  * physical pages in VM.
  */
-void nested_map_memslot(struct kvm_vm *vm, vm_paddr_t root_gpa,
-			uint32_t memslot)
+void nested_identity_map_default_memslots(struct kvm_vm *vm, vm_paddr_t root_gpa)
 {
+	uint32_t s, memslot = 0;
 	sparsebit_idx_t i, last;
-	struct userspace_mem_region *region =
-		memslot2region(vm, memslot);
+	struct userspace_mem_region *region = memslot2region(vm, memslot);
+
+	/* Only memslot 0 is mapped here, ensure it's the only one being used */
+	for (s = 0; s < NR_MEM_REGIONS; s++)
+		TEST_ASSERT_EQ(vm->memslots[s], 0);
 
 	i = (region->region.guest_phys_addr >> vm->page_shift) - 1;
 	last = i + (region->region.memory_size >> vm->page_shift);
tools/testing/selftests/kvm/x86/vmx_dirty_log_test.c +1 -1
index e54e6111164e7..b8ebb246aaf15 100644
@@ -121,7 +121,7 @@ static void test_vmx_dirty_log(bool enable_ept)
 	 */
 	if (enable_ept) {
 		prepare_eptp(vmx, vm);
-		nested_map_memslot(vm, vmx->eptp_gpa, 0);
+		nested_identity_map_default_memslots(vm, vmx->eptp_gpa);
 		nested_map(vm, vmx->eptp_gpa, NESTED_TEST_MEM1, GUEST_TEST_MEM, PAGE_SIZE);
 		nested_map(vm, vmx->eptp_gpa, NESTED_TEST_MEM2, GUEST_TEST_MEM, PAGE_SIZE);
 	}
Signature · 1 line
2.51.0.869.ge66316f041-goog
#31 lore raw

PATCH v2 21/23 KVM: selftests: Allow kvm_cpu_has_ept() to be called on AMD CPUs

In reply to Yosry Ahmed

In preparation for generalizing the nested dirty logging test, checking if either EPT or NPT is enabled will be needed. To avoid needing to gate the kvm_cpu_has_ept() call by the CPU type, make sure the function returns false if VMX is not available instead of trying to read VMX-only MSRs.

No functional change intended.

---
 tools/testing/selftests/kvm/lib/x86/vmx.c | 3 +++
 1 file changed, 3 insertions(+)
tools/testing/selftests/kvm/lib/x86/vmx.c +3 -0
index 1a9743cabcf4b..b1f22e78aca1a 100644
@@ -365,6 +365,9 @@ bool kvm_cpu_has_ept(void)
 {
 	uint64_t ctrl;
 
+	if (!kvm_cpu_has(X86_FEATURE_VMX))
+		return false;
+
 	ctrl = kvm_get_feature_msr(MSR_IA32_VMX_TRUE_PROCBASED_CTLS) >> 32;
 	if (!(ctrl & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
 		return false;
Signature · 1 line
2.51.0.869.ge66316f041-goog
#32 lore raw

PATCH v2 22/23 KVM: selftests: Set the user bit on nested MMU PTEs

In reply to Yosry Ahmed

Similar to KVM's shadow MMU (in most cases), set the user bit on nested PTEs. This is in preparation for supporting NPT mappings, which require the user bit to be set. This should be nop for VMX.

---
 tools/testing/selftests/kvm/lib/x86/processor.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/x86/processor.c +2 -2
index 958389ec1722d..c2912b0a49e90 100644
@@ -211,7 +211,7 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm,
 	paddr = vm_untag_gpa(vm, paddr);
 
 	if (!(*pte & masks->present)) {
-		*pte = masks->present | masks->writeable | masks->x;
+		*pte = masks->present | masks->writeable | masks->x | masks->user;
 		if (current_level == target_level)
 			*pte |= masks->large | (paddr & PHYSICAL_PAGE_MASK);
 		else
@@ -276,7 +276,7 @@ void __virt_pg_map(struct kvm_vm *vm, vm_paddr_t root_gpa, uint64_t vaddr,
 	pte = virt_get_pte(vm, root_gpa, pte, vaddr, PG_LEVEL_4K, masks);
 	TEST_ASSERT(!(*pte & masks->present),
 		    "PTE already present for 4k page at vaddr: 0x%lx", vaddr);
-	*pte = masks->present | masks->writeable | masks->x
+	*pte = masks->present | masks->writeable | masks->x | masks->user
 		| (paddr & PHYSICAL_PAGE_MASK);
 
 	/*
Signature · 1 line
2.51.0.869.ge66316f041-goog
#33 lore raw

PATCH v2 23/23 KVM: selftests: Extend vmx_dirty_log_test to cover SVM

In reply to Yosry Ahmed

Add prepare_ncr3() to initialize the nested NPTs, and enable NPT from L1 accordingly. Everything else should work for creating nested NPT mappings.

Generalize the code in vmx_dirty_log_test.c by adding SVM-specific L1 code, doing some renaming (e.g. EPT -> TDP), and having setup code for both SVM and VMX in test_dirty_log().

Having multiple points to check for SVM vs VMX is not ideal, but the alternatives either include a lot of redundancy or a lot of abstracting functions that will make the test logic harder to follow.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  2 +-
 .../selftests/kvm/include/x86/svm_util.h      |  8 ++
 tools/testing/selftests/kvm/lib/x86/svm.c     | 19 ++++
 ...rty_log_test.c => nested_dirty_log_test.c} | 96 +++++++++++++------
 4 files changed, 97 insertions(+), 28 deletions(-)
 rename tools/testing/selftests/kvm/x86/{vmx_dirty_log_test.c => nested_dirty_log_test.c} (62%)
tools/testing/selftests/kvm/Makefile.kvm +1 -1
index 6625ac53545e8..5da95776fc9c2 100644
@@ -114,7 +114,7 @@ TEST_GEN_PROGS_x86 += x86/userspace_io_test
 TEST_GEN_PROGS_x86 += x86/userspace_msr_exit_test
 TEST_GEN_PROGS_x86 += x86/vmx_apic_access_test
 TEST_GEN_PROGS_x86 += x86/close_while_nested_test
-TEST_GEN_PROGS_x86 += x86/vmx_dirty_log_test
+TEST_GEN_PROGS_x86 += x86/nested_dirty_log_test
 TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state
 TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
 TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state
tools/testing/selftests/kvm/include/x86/svm_util.h +8 -0
index b74c6dcddcbd6..70bb22dd6de54 100644
@@ -27,6 +27,11 @@ struct svm_test_data {
 	void *msr; /* gva */
 	void *msr_hva;
 	uint64_t msr_gpa;
+
+	/* NPT */
+	void *ncr3; /* gva */
+	void *ncr3_hva;
+	uint64_t ncr3_gpa;
 };
 
 static inline void vmmcall(void)
@@ -57,6 +62,9 @@ struct svm_test_data *vcpu_alloc_svm(struct kvm_vm *vm, vm_vaddr_t *p_svm_gva);
 void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_rsp);
 void run_guest(struct vmcb *vmcb, uint64_t vmcb_gpa);
 
+bool kvm_cpu_has_npt(void);
+void prepare_ncr3(struct svm_test_data *svm, struct kvm_vm *vm);
+
 int open_sev_dev_path_or_exit(void);
 
 #endif /* SELFTEST_KVM_SVM_UTILS_H */
tools/testing/selftests/kvm/lib/x86/svm.c +19 -0
index d239c20973918..190a8044dba0e 100644
@@ -59,6 +59,20 @@ static void vmcb_set_seg(struct vmcb_seg *seg, u16 selector,
 	seg->base = base;
 }
 
+bool kvm_cpu_has_npt(void)
+{
+	return kvm_cpu_has(X86_FEATURE_NPT);
+}
+
+void prepare_ncr3(struct svm_test_data *svm, struct kvm_vm *vm)
+{
+	TEST_ASSERT(kvm_cpu_has_npt(), "KVM doesn't support nested NPT");
+
+	svm->ncr3 = (void *)vm_vaddr_alloc_page(vm);
+	svm->ncr3_hva = addr_gva2hva(vm, (uintptr_t)svm->ncr3);
+	svm->ncr3_gpa = addr_gva2gpa(vm, (uintptr_t)svm->ncr3);
+}
+
 void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_rsp)
 {
 	struct vmcb *vmcb = svm->vmcb;
@@ -102,6 +116,11 @@ void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_r
 	vmcb->save.rip = (u64)guest_rip;
 	vmcb->save.rsp = (u64)guest_rsp;
 	guest_regs.rdi = (u64)svm;
+
+	if (svm->ncr3_gpa) {
+		ctrl->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
+		ctrl->nested_cr3 = svm->ncr3_gpa;
+	}
 }
 
 /*
tools/testing/selftests/kvm/x86/nested_dirty_log_test.c renamed from tools/testing/selftests/kvm/x86/vmx_dirty_log_test.c +69 -27

165 lines not on this page · raw message

Signature · 1 line
2.51.0.869.ge66316f041-goog
#34 lore raw
In reply to Yosry Ahmed
Quoted 33 lines · Yosry Ahmed

There are multiple selftests exercising nested VMX that are not specific to VMX (at least not anymore). Extend their coverage to nested SVM.

This version is significantly different (and longer) than v1 [1], mainly due to the change of direction to reuse __virt_pg_map() for nested EPT/NPT mappings instead of extending the existing nested EPT infrastructure. It also has a lot more fixups and cleanups.

This series depends on two other series:
- "KVM: SVM: GIF and EFER.SVME are independent" [2]
- "KVM: selftests: Add test of SET_NESTED_STATE with 48-bit L2 on 57-bit L1" [3]

The dependency on the former is because set_nested_state_test is now also a regression test for that fix. The dependency on the latter is purely to avoid conflicts.

The patch ordering is not perfect, I did some cleanups toward the end that arguably should have been moved to the beginning, but I had to stop rebasing and send the patches out at some point:

Block #1 (patch 1 to patch 7):
- Direct successors to the first 6 patches in v1, addressing review comments from Jim and collecting his review tags. These patch extend 5 of the nVMX tests to cover nSVM.

Block #2 (patch 8 to patch 11):
- Miscellaneous fixups and cleanups.

Block #3 (patch 11 to patch 17):
- Moving nested EPT mapping functions to use __virt_pg_map(), patches 11 to 15 do the prep work, and patch 16 does the switch. Patch 17 is a minor cleanup on top (which arguably fits better in block #2).

Quoted 6 lines

Block #4 (patch 18 to 23):
- Patches 18 to 22 are prep work to generalize the nested EPT mapping code to work with nested NPT, and patch 23 finally extends the nested dirty logging test to work with nSVM using the nested NPT infrastructure. Patch 19 is admittedly an imposter in this block and should have been in block #2.

Hi Sean,

Any thoughts on the current version? Is this what you had in mind for reusing __virt_pg_map()?

Quoted 66 lines

[1]https://lore.kernel.org/kvm/[email protected]/ [2]https://lore.kernel.org/kvm/[email protected]/ [3]https://lore.kernel.org/kvm/[email protected]/

Yosry Ahmed (23):
KVM: selftests: Minor improvements to asserts in
test_vmx_nested_state()
KVM: selftests: Extend vmx_set_nested_state_test to cover SVM
KVM: selftests: Extend vmx_close_while_nested_test to cover SVM
KVM: selftests: Extend vmx_nested_tsc_scaling_test to cover SVM
KVM: selftests: Move nested invalid CR3 check to its own test
KVM: selftests: Extend nested_invalid_cr3_test to cover SVM
KVM: selftests: Extend vmx_tsc_adjust_test to cover SVM
KVM: selftests: Stop hardcoding PAGE_SIZE in x86 selftests
KVM: selftests: Remove the unused argument to prepare_eptp()
KVM: selftests: Stop using __virt_pg_map() directly in tests
KVM: selftests: Make sure vm->vpages_mapped is always up-to-date
KVM: selftests: Parameterize the PTE bitmasks for virt mapping
functions
KVM: selftests: Pass the root GPA into virt_get_pte()
KVM: selftests: Pass the root GPA into __virt_pg_map()
KVM: selftests: Stop setting AD bits on nested EPTs on creation
KVM: selftests: Use __virt_pg_map() for nested EPTs
KVM: selftests: Kill eptPageTablePointer
KVM: selftests: Generalize nested mapping functions
KVM: selftests: Move nested MMU mapping functions outside of vmx.c
KVM: selftests: Stop passing a memslot to nested_map_memslot()
KVM: selftests: Allow kvm_cpu_has_ept() to be called on AMD CPUs
KVM: selftests: Set the user bit on nested MMU PTEs
KVM: selftests: Extend vmx_dirty_log_test to cover SVM

 tools/testing/selftests/kvm/Makefile.kvm      |  11 +-
 .../testing/selftests/kvm/include/kvm_util.h  |   1 +
 .../selftests/kvm/include/x86/processor.h     |  34 ++-
 .../selftests/kvm/include/x86/svm_util.h      |   8 +
 tools/testing/selftests/kvm/include/x86/vmx.h |  15 +-
 tools/testing/selftests/kvm/lib/kvm_util.c    |   3 -
 .../testing/selftests/kvm/lib/x86/memstress.c |   6 +-
 .../testing/selftests/kvm/lib/x86/processor.c | 184 +++++++++++---
 tools/testing/selftests/kvm/lib/x86/svm.c     |  19 ++
 tools/testing/selftests/kvm/lib/x86/vmx.c     | 232 +++---------------
 tools/testing/selftests/kvm/mmu_stress_test.c |   6 +-
 ...ested_test.c => close_while_nested_test.c} |  42 +++-
 .../selftests/kvm/x86/hyperv_features.c       |   2 +-
 tools/testing/selftests/kvm/x86/hyperv_ipi.c  |  18 +-
 .../selftests/kvm/x86/hyperv_tlb_flush.c      |   2 +-
 ...rty_log_test.c => nested_dirty_log_test.c} | 102 +++++---
 .../kvm/x86/nested_invalid_cr3_test.c         | 118 +++++++++
 ...adjust_test.c => nested_tsc_adjust_test.c} |  79 +++---
 ...aling_test.c => nested_tsc_scaling_test.c} |  48 +++-
 ...d_state_test.c => set_nested_state_test.c} | 135 +++++++++-
 .../selftests/kvm/x86/sev_smoke_test.c        |   2 +-
 tools/testing/selftests/kvm/x86/state_test.c  |   2 +-
 .../selftests/kvm/x86/userspace_io_test.c     |   2 +-
 23 files changed, 695 insertions(+), 376 deletions(-)
 rename tools/testing/selftests/kvm/x86/{vmx_close_while_nested_test.c => close_while_nested_test.c} (64%)
 rename tools/testing/selftests/kvm/x86/{vmx_dirty_log_test.c => nested_dirty_log_test.c} (57%)
 create mode 100644 tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c
 rename tools/testing/selftests/kvm/x86/{vmx_tsc_adjust_test.c => nested_tsc_adjust_test.c} (61%)
 rename tools/testing/selftests/kvm/x86/{vmx_nested_tsc_scaling_test.c => nested_tsc_scaling_test.c} (83%)
 rename tools/testing/selftests/kvm/x86/{vmx_set_nested_state_test.c => set_nested_state_test.c} (67%)
Signature · 1 line
2.51.0.869.ge66316f041-goog
#35 lore raw
In reply to Yosry Ahmed
Quoted 11 lines · Yosry Ahmed

There are multiple selftests exercising nested VMX that are not specific to VMX (at least not anymore). Extend their coverage to nested SVM.

This version is significantly different (and longer) than v1 [1], mainly due to the change of direction to reuse __virt_pg_map() for nested EPT/NPT mappings instead of extending the existing nested EPT infrastructure. It also has a lot more fixups and cleanups.

This series depends on two other series:
- "KVM: SVM: GIF and EFER.SVME are independent" [2]
- "KVM: selftests: Add test of SET_NESTED_STATE with 48-bit L2 on 57-bit L1" [3]

v2 of Jim's series switches all tests to use 57-bit by default when available:
https://lore.kernel.org/kvm/[email protected]/

This breaks moving nested EPT mappings to use __virt_pg_map() because nested EPTs are hardcoded to use 4-level paging, while __virt_pg_map() will assume we're using 5-level paging.

Patch #16 ("KVM: selftests: Use __virt_pg_map() for nested EPTs") will need the following diff to make nested EPTs use the same paging level as the guest:

tools/testing/selftests/kvm/lib/x86/vmx.c +1 -1
index 358143bf8dd0d..8bacb74c00053 100644
@@ -203,7 +203,7 @@ static inline void init_vmcs_control_fields(struct vmx_pages *vmx)
                uint64_t ept_paddr;
                struct eptPageTablePointer eptp = {
                        .memory_type = X86_MEMTYPE_WB,
-                       .page_walk_length = 3, /* + 1 */
+                       .page_walk_length = get_cr4() & X86_CR4_LA57 ? 4 : 3, /* + 1 */
                        .ad_enabled = ept_vpid_cap_supported(VMX_EPT_VPID_CAP_AD_BITS),
                        .address = vmx->eptp_gpa >> PAGE_SHIFT_4K,
                };

Which will conflict at patch #17, and should end up looking like this:

	uint64_t eptp = vmx->eptp_gpa | EPTP_WB;

	eptp |= get_cr4() & X86_CR4_LA57 ? EPTP_PWL_5 : EPTP_PWL_4;

Sean, let me know if you prefer that I rebase this series on top of Jim's v2 and resend, or if you'll fix it up while applying.

Quoted 94 lines

The dependency on the former is because set_nested_state_test is now also a regression test for that fix. The dependency on the latter is purely to avoid conflicts.

The patch ordering is not perfect, I did some cleanups toward the end that arguably should have been moved to the beginning, but I had to stop rebasing and send the patches out at some point:

Block #1 (patch 1 to patch 7):
- Direct successors to the first 6 patches in v1, addressing review comments from Jim and collecting his review tags. These patch extend 5 of the nVMX tests to cover nSVM.

Block #2 (patch 8 to patch 11):
- Miscellaneous fixups and cleanups.

Block #3 (patch 11 to patch 17):
- Moving nested EPT mapping functions to use __virt_pg_map(), patches 11 to 15 do the prep work, and patch 16 does the switch. Patch 17 is a minor cleanup on top (which arguably fits better in block #2).

Block #4 (patch 18 to 23):
- Patches 18 to 22 are prep work to generalize the nested EPT mapping code to work with nested NPT, and patch 23 finally extends the nested dirty logging test to work with nSVM using the nested NPT infrastructure. Patch 19 is admittedly an imposter in this block and should have been in block #2.

[1]https://lore.kernel.org/kvm/[email protected]/ [2]https://lore.kernel.org/kvm/[email protected]/ [3]https://lore.kernel.org/kvm/[email protected]/

Yosry Ahmed (23):
KVM: selftests: Minor improvements to asserts in
test_vmx_nested_state()
KVM: selftests: Extend vmx_set_nested_state_test to cover SVM
KVM: selftests: Extend vmx_close_while_nested_test to cover SVM
KVM: selftests: Extend vmx_nested_tsc_scaling_test to cover SVM
KVM: selftests: Move nested invalid CR3 check to its own test
KVM: selftests: Extend nested_invalid_cr3_test to cover SVM
KVM: selftests: Extend vmx_tsc_adjust_test to cover SVM
KVM: selftests: Stop hardcoding PAGE_SIZE in x86 selftests
KVM: selftests: Remove the unused argument to prepare_eptp()
KVM: selftests: Stop using __virt_pg_map() directly in tests
KVM: selftests: Make sure vm->vpages_mapped is always up-to-date
KVM: selftests: Parameterize the PTE bitmasks for virt mapping
functions
KVM: selftests: Pass the root GPA into virt_get_pte()
KVM: selftests: Pass the root GPA into __virt_pg_map()
KVM: selftests: Stop setting AD bits on nested EPTs on creation
KVM: selftests: Use __virt_pg_map() for nested EPTs
KVM: selftests: Kill eptPageTablePointer
KVM: selftests: Generalize nested mapping functions
KVM: selftests: Move nested MMU mapping functions outside of vmx.c
KVM: selftests: Stop passing a memslot to nested_map_memslot()
KVM: selftests: Allow kvm_cpu_has_ept() to be called on AMD CPUs
KVM: selftests: Set the user bit on nested MMU PTEs
KVM: selftests: Extend vmx_dirty_log_test to cover SVM

 tools/testing/selftests/kvm/Makefile.kvm      |  11 +-
 .../testing/selftests/kvm/include/kvm_util.h  |   1 +
 .../selftests/kvm/include/x86/processor.h     |  34 ++-
 .../selftests/kvm/include/x86/svm_util.h      |   8 +
 tools/testing/selftests/kvm/include/x86/vmx.h |  15 +-
 tools/testing/selftests/kvm/lib/kvm_util.c    |   3 -
 .../testing/selftests/kvm/lib/x86/memstress.c |   6 +-
 .../testing/selftests/kvm/lib/x86/processor.c | 184 +++++++++++---
 tools/testing/selftests/kvm/lib/x86/svm.c     |  19 ++
 tools/testing/selftests/kvm/lib/x86/vmx.c     | 232 +++---------------
 tools/testing/selftests/kvm/mmu_stress_test.c |   6 +-
 ...ested_test.c => close_while_nested_test.c} |  42 +++-
 .../selftests/kvm/x86/hyperv_features.c       |   2 +-
 tools/testing/selftests/kvm/x86/hyperv_ipi.c  |  18 +-
 .../selftests/kvm/x86/hyperv_tlb_flush.c      |   2 +-
 ...rty_log_test.c => nested_dirty_log_test.c} | 102 +++++---
 .../kvm/x86/nested_invalid_cr3_test.c         | 118 +++++++++
 ...adjust_test.c => nested_tsc_adjust_test.c} |  79 +++---
 ...aling_test.c => nested_tsc_scaling_test.c} |  48 +++-
 ...d_state_test.c => set_nested_state_test.c} | 135 +++++++++-
 .../selftests/kvm/x86/sev_smoke_test.c        |   2 +-
 tools/testing/selftests/kvm/x86/state_test.c  |   2 +-
 .../selftests/kvm/x86/userspace_io_test.c     |   2 +-
 23 files changed, 695 insertions(+), 376 deletions(-)
 rename tools/testing/selftests/kvm/x86/{vmx_close_while_nested_test.c => close_while_nested_test.c} (64%)
 rename tools/testing/selftests/kvm/x86/{vmx_dirty_log_test.c => nested_dirty_log_test.c} (57%)
 create mode 100644 tools/testing/selftests/kvm/x86/nested_invalid_cr3_test.c
 rename tools/testing/selftests/kvm/x86/{vmx_tsc_adjust_test.c => nested_tsc_adjust_test.c} (61%)
 rename tools/testing/selftests/kvm/x86/{vmx_nested_tsc_scaling_test.c => nested_tsc_scaling_test.c} (83%)
 rename tools/testing/selftests/kvm/x86/{vmx_set_nested_state_test.c => set_nested_state_test.c} (67%)
Signature · 1 line
2.51.0.869.ge66316f041-goog
#36 lore raw
Quoted 31 lines · Yosry Ahmed
Quoted 11 lines · Yosry Ahmed

There are multiple selftests exercising nested VMX that are not specific to VMX (at least not anymore). Extend their coverage to nested SVM.

This version is significantly different (and longer) than v1 [1], mainly due to the change of direction to reuse __virt_pg_map() for nested EPT/NPT mappings instead of extending the existing nested EPT infrastructure. It also has a lot more fixups and cleanups.

This series depends on two other series:
- "KVM: SVM: GIF and EFER.SVME are independent" [2]
- "KVM: selftests: Add test of SET_NESTED_STATE with 48-bit L2 on 57-bit L1" [3]

v2 of Jim's series switches all tests to use 57-bit by default when available:
https://lore.kernel.org/kvm/[email protected]/

This breaks moving nested EPT mappings to use __virt_pg_map() because nested EPTs are hardcoded to use 4-level paging, while __virt_pg_map() will assume we're using 5-level paging.

Patch #16 ("KVM: selftests: Use __virt_pg_map() for nested EPTs") will need the following diff to make nested EPTs use the same paging level as the guest:

tools/testing/selftests/kvm/lib/x86/vmx.c +0 -0
index 358143bf8dd0d..8bacb74c00053 100644

tools/testing/selftests/kvm/lib/x86/vmx.c static inline void init_vmcs_control_fields(struct vmx_pages *vmx) · line 206

Quoted 5 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +1 -1
@@ -203,7 +203,7 @@ static inline void init_vmcs_control_fields(struct vmx_pages *vmx)
                uint64_t ept_paddr;
                struct eptPageTablePointer eptp = {
                        .memory_type = X86_MEMTYPE_WB,
-                       .page_walk_length = 3, /* + 1 */
+                       .page_walk_length = get_cr4() & X86_CR4_LA57 ? 4 : 3, /* + 1 */

LA57 does not imply support for 5-level EPT. (SRF, IIRC)

#37 lore raw
Quoted 27 lines · Jim Mattson
Quoted 26 lines · Yosry Ahmed
Quoted 11 lines · Yosry Ahmed

There are multiple selftests exercising nested VMX that are not specific to VMX (at least not anymore). Extend their coverage to nested SVM.

This version is significantly different (and longer) than v1 [1], mainly due to the change of direction to reuse __virt_pg_map() for nested EPT/NPT mappings instead of extending the existing nested EPT infrastructure. It also has a lot more fixups and cleanups.

This series depends on two other series:
- "KVM: SVM: GIF and EFER.SVME are independent" [2]
- "KVM: selftests: Add test of SET_NESTED_STATE with 48-bit L2 on 57-bit L1" [3]

v2 of Jim's series switches all tests to use 57-bit by default when available:
https://lore.kernel.org/kvm/[email protected]/

This breaks moving nested EPT mappings to use __virt_pg_map() because nested EPTs are hardcoded to use 4-level paging, while __virt_pg_map() will assume we're using 5-level paging.

Patch #16 ("KVM: selftests: Use __virt_pg_map() for nested EPTs") will need the following diff to make nested EPTs use the same paging level as the guest:

Quoted 12 lines

tools/testing/selftests/kvm/lib/x86/vmx.c static inline void init_vmcs_control_fields(struct vmx_pages *vmx) · line 206

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +1 -1
index 358143bf8dd0d..8bacb74c00053 100644
@@ -203,7 +203,7 @@ static inline void init_vmcs_control_fields(struct vmx_pages *vmx)
                uint64_t ept_paddr;
                struct eptPageTablePointer eptp = {
                        .memory_type = X86_MEMTYPE_WB,
-                       .page_walk_length = 3, /* + 1 */
+                       .page_walk_length = get_cr4() & X86_CR4_LA57 ? 4 : 3, /* + 1 */

LA57 does not imply support for 5-level EPT. (SRF, IIRC)

Huh, that's annoying. We can keep the EPTs hardcoded to 4 levels and pass in the max level to __virt_pg_map() instead of hardcoding vm->pgtable_levels.

Sean, let me know how you want to handle this. I can fix this and rebase the series (or part of it?), or you can fix it up if you prefer to do so.

#38 lore raw
Quoted 28 lines · Yosry Ahmed
Quoted 27 lines · Jim Mattson
Quoted 26 lines · Yosry Ahmed
Quoted 11 lines · Yosry Ahmed

There are multiple selftests exercising nested VMX that are not specific to VMX (at least not anymore). Extend their coverage to nested SVM.

This version is significantly different (and longer) than v1 [1], mainly due to the change of direction to reuse __virt_pg_map() for nested EPT/NPT mappings instead of extending the existing nested EPT infrastructure. It also has a lot more fixups and cleanups.

This series depends on two other series:
- "KVM: SVM: GIF and EFER.SVME are independent" [2]
- "KVM: selftests: Add test of SET_NESTED_STATE with 48-bit L2 on 57-bit L1" [3]

v2 of Jim's series switches all tests to use 57-bit by default when available:
https://lore.kernel.org/kvm/[email protected]/

This breaks moving nested EPT mappings to use __virt_pg_map() because nested EPTs are hardcoded to use 4-level paging, while __virt_pg_map() will assume we're using 5-level paging.

Patch #16 ("KVM: selftests: Use __virt_pg_map() for nested EPTs") will need the following diff to make nested EPTs use the same paging level as the guest:

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +0 -0
index 358143bf8dd0d..8bacb74c00053 100644
Quoted 8 lines

tools/testing/selftests/kvm/lib/x86/vmx.c static inline void init_vmcs_control_fields(struct vmx_pages *vmx) · line 206

Quoted 6 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +1 -1
@@ -203,7 +203,7 @@ static inline void init_vmcs_control_fields(struct vmx_pages *vmx)
                uint64_t ept_paddr;
                struct eptPageTablePointer eptp = {
                        .memory_type = X86_MEMTYPE_WB,
-                       .page_walk_length = 3, /* + 1 */
+                       .page_walk_length = get_cr4() & X86_CR4_LA57 ? 4 : 3, /* + 1 */

LA57 does not imply support for 5-level EPT. (SRF, IIRC)

Yuuuup. And similarly, MAXPHYADDR=52 doesn't imply 5-level EPT (thank you TDX!).

Quoted 3 lines

Huh, that's annoying. We can keep the EPTs hardcoded to 4 levels and pass in the max level to __virt_pg_map() instead of hardcoding vm->pgtable_levels.

I haven't looked at the series in-depth so I don't know exactly what you're trying to do, but why not check MSR_IA32_VMX_EPT_VPID_CAP for PWL5?

#39 lore raw
Quoted 42 lines · Sean Christopherson
Quoted 28 lines · Yosry Ahmed
Quoted 27 lines · Jim Mattson
Quoted 26 lines · Yosry Ahmed
Quoted 11 lines · Yosry Ahmed

There are multiple selftests exercising nested VMX that are not specific to VMX (at least not anymore). Extend their coverage to nested SVM.

This version is significantly different (and longer) than v1 [1], mainly due to the change of direction to reuse __virt_pg_map() for nested EPT/NPT mappings instead of extending the existing nested EPT infrastructure. It also has a lot more fixups and cleanups.

This series depends on two other series:
- "KVM: SVM: GIF and EFER.SVME are independent" [2]
- "KVM: selftests: Add test of SET_NESTED_STATE with 48-bit L2 on 57-bit L1" [3]

v2 of Jim's series switches all tests to use 57-bit by default when available:
https://lore.kernel.org/kvm/[email protected]/

This breaks moving nested EPT mappings to use __virt_pg_map() because nested EPTs are hardcoded to use 4-level paging, while __virt_pg_map() will assume we're using 5-level paging.

Patch #16 ("KVM: selftests: Use __virt_pg_map() for nested EPTs") will need the following diff to make nested EPTs use the same paging level as the guest:

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +0 -0
index 358143bf8dd0d..8bacb74c00053 100644
Quoted 8 lines

tools/testing/selftests/kvm/lib/x86/vmx.c static inline void init_vmcs_control_fields(struct vmx_pages *vmx) · line 206

Quoted 6 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +1 -1
@@ -203,7 +203,7 @@ static inline void init_vmcs_control_fields(struct vmx_pages *vmx)
                uint64_t ept_paddr;
                struct eptPageTablePointer eptp = {
                        .memory_type = X86_MEMTYPE_WB,
-                       .page_walk_length = 3, /* + 1 */
+                       .page_walk_length = get_cr4() & X86_CR4_LA57 ? 4 : 3, /* + 1 */

LA57 does not imply support for 5-level EPT. (SRF, IIRC)

Quoted 8 lines

Yuuuup. And similarly, MAXPHYADDR=52 doesn't imply 5-level EPT (thank you TDX!).

Quoted 3 lines

Huh, that's annoying. We can keep the EPTs hardcoded to 4 levels and pass in the max level to __virt_pg_map() instead of hardcoding vm->pgtable_levels.

I haven't looked at the series in-depth so I don't know exactly what you're trying to do, but why not check MSR_IA32_VMX_EPT_VPID_CAP for PWL5?

The second part of the series reuses __virt_pg_map() to be used for nested EPTs (and NPTs). __virt_pg_map() uses vm->pgtable_levels to find out how many page table levels we have.

So we need to either:

(a) Always use the same number of levels for page tables and EPTs.

(b) Make __virt_pg_map() take the number of page table levels as a parameter, and always pass 4 for EPTs (for now).

I suggested (a) initially, but it doesn't work because we can technically have LA57 but not MSR_IA32_VMX_EPT_VPID_CAP, so we need to do (b). We can still check MSR_IA32_VMX_EPT_VPID_CAP and use PWL5 for EPTs, but that's an orthogonal change at this point.

Anyway, do you prefer that I resend the series on top of Jim's v2, or do you want to wait and see if you'll fix it up (or apply a part of it before I rebase the rest)?

#40 lore raw
Quoted 63 lines · Yosry Ahmed
Quoted 42 lines · Sean Christopherson
Quoted 28 lines · Yosry Ahmed
Quoted 27 lines · Jim Mattson
Quoted 26 lines · Yosry Ahmed
Quoted 11 lines · Yosry Ahmed

There are multiple selftests exercising nested VMX that are not specific to VMX (at least not anymore). Extend their coverage to nested SVM.

This version is significantly different (and longer) than v1 [1], mainly due to the change of direction to reuse __virt_pg_map() for nested EPT/NPT mappings instead of extending the existing nested EPT infrastructure. It also has a lot more fixups and cleanups.

This series depends on two other series:
- "KVM: SVM: GIF and EFER.SVME are independent" [2]
- "KVM: selftests: Add test of SET_NESTED_STATE with 48-bit L2 on 57-bit L1" [3]

v2 of Jim's series switches all tests to use 57-bit by default when available:
https://lore.kernel.org/kvm/[email protected]/

This breaks moving nested EPT mappings to use __virt_pg_map() because nested EPTs are hardcoded to use 4-level paging, while __virt_pg_map() will assume we're using 5-level paging.

Patch #16 ("KVM: selftests: Use __virt_pg_map() for nested EPTs") will need the following diff to make nested EPTs use the same paging level as the guest:

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +0 -0
index 358143bf8dd0d..8bacb74c00053 100644
Quoted 8 lines

tools/testing/selftests/kvm/lib/x86/vmx.c static inline void init_vmcs_control_fields(struct vmx_pages *vmx) · line 206

Quoted 6 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +1 -1
@@ -203,7 +203,7 @@ static inline void init_vmcs_control_fields(struct vmx_pages *vmx)
                uint64_t ept_paddr;
                struct eptPageTablePointer eptp = {
                        .memory_type = X86_MEMTYPE_WB,
-                       .page_walk_length = 3, /* + 1 */
+                       .page_walk_length = get_cr4() & X86_CR4_LA57 ? 4 : 3, /* + 1 */

LA57 does not imply support for 5-level EPT. (SRF, IIRC)

Quoted 8 lines

Yuuuup. And similarly, MAXPHYADDR=52 doesn't imply 5-level EPT (thank you TDX!).

Quoted 3 lines

Huh, that's annoying. We can keep the EPTs hardcoded to 4 levels and pass in the max level to __virt_pg_map() instead of hardcoding vm->pgtable_levels.

I haven't looked at the series in-depth so I don't know exactly what you're trying to do, but why not check MSR_IA32_VMX_EPT_VPID_CAP for PWL5?

The second part of the series reuses __virt_pg_map() to be used for nested EPTs (and NPTs). __virt_pg_map() uses vm->pgtable_levels to find out how many page table levels we have.

So we need to either:

(a) Always use the same number of levels for page tables and EPTs.

(b) Make __virt_pg_map() take the number of page table levels as a parameter, and always pass 4 for EPTs (for now).

Quoted 8 lines

I suggested (a) initially, but it doesn't work because we can technically have LA57 but not MSR_IA32_VMX_EPT_VPID_CAP, so we need to do (b). We can still check MSR_IA32_VMX_EPT_VPID_CAP and use PWL5 for EPTs, but that's an orthogonal change at this point.

Anyway, do you prefer that I resend the series on top of Jim's v2, or do you want to wait and see if you'll fix it up (or apply a part of it before I rebase the rest)?

Hold off for now, sending v3 of a 23-patch series at this point isn't likely to make things go faster :-)

#41 lore raw
In reply to Yosry Ahmed
Quoted 34 lines · Yosry Ahmed
Quoted 16 lines · Sean Christopherson
Quoted 14 lines · Yosry Ahmed
Quoted 13 lines · Jim Mattson

tools/testing/selftests/kvm/lib/x86/vmx.c static inline void init_vmcs_control_fields(struct vmx_pages *vmx) · line 206

Quoted 10 lines · Yosry Ahmed
tools/testing/selftests/kvm/lib/x86/vmx.c +1 -1
index 358143bf8dd0d..8bacb74c00053 100644
@@ -203,7 +203,7 @@ static inline void init_vmcs_control_fields(struct vmx_pages *vmx)
                uint64_t ept_paddr;
                struct eptPageTablePointer eptp = {
                        .memory_type = X86_MEMTYPE_WB,
-                       .page_walk_length = 3, /* + 1 */
+                       .page_walk_length = get_cr4() & X86_CR4_LA57 ? 4 : 3, /* + 1 */

LA57 does not imply support for 5-level EPT. (SRF, IIRC)

Quoted 8 lines

Yuuuup. And similarly, MAXPHYADDR=52 doesn't imply 5-level EPT (thank you TDX!).

Quoted 3 lines

Huh, that's annoying. We can keep the EPTs hardcoded to 4 levels and pass in the max level to __virt_pg_map() instead of hardcoding vm->pgtable_levels.

I haven't looked at the series in-depth so I don't know exactly what you're trying to do, but why not check MSR_IA32_VMX_EPT_VPID_CAP for PWL5?

The second part of the series reuses __virt_pg_map() to be used for nested EPTs (and NPTs). __virt_pg_map() uses vm->pgtable_levels to find out how many page table levels we have.

So we need to either:

(a) Always use the same number of levels for page tables and EPTs.

Quoted 7 lines

(b) Make __virt_pg_map() take the number of page table levels as a parameter, and always pass 4 for EPTs (for now).

I suggested (a) initially, but it doesn't work because we can technically have LA57 but not MSR_IA32_VMX_EPT_VPID_CAP, so we need to do (b). We can still check MSR_IA32_VMX_EPT_VPID_CAP and use PWL5 for EPTs, but that's an orthogonal change at this point.

I choose option (c)

(c) Add a "struct kvm_mmu" and use it to hold the PTE masks, root, root level, and any other metadata that comes along in the future.

Then we only need to do the core plumbing once, to get "struct kvm_mmu *mmu" passed in. After that, adding each piece only needs to touch code that actually cares about those things.

That was going to be my vote even without this particular discussion (I forgot why I even started reviewing the series, *sigh*), because the root_gpa and pte_masks should be passed as a single entity. The root+level are even more tightly coupled.

#42 lore raw
In reply to Yosry Ahmed
Quoted 11 lines · Yosry Ahmed

There are multiple selftests exercising nested VMX that are not specific to VMX (at least not anymore). Extend their coverage to nested SVM.

This version is significantly different (and longer) than v1 [1], mainly due to the change of direction to reuse __virt_pg_map() for nested EPT/NPT mappings instead of extending the existing nested EPT infrastructure. It also has a lot more fixups and cleanups.

This series depends on two other series:
- "KVM: SVM: GIF and EFER.SVME are independent" [2]
- "KVM: selftests: Add test of SET_NESTED_STATE with 48-bit L2 on 57-bit L1" [3]

No, it depends on local commits that are very similar to [3], but not precisely [3]. In the future, please provide a link to a git repo+branch when posting series with dependencies. It took me several attempts and a bit of conflict resolution to get this series applied.

Quoted 3 lines

[1]https://lore.kernel.org/kvm/[email protected]/ [2]https://lore.kernel.org/kvm/[email protected]/ [3]https://lore.kernel.org/kvm/[email protected]/

#43 lore raw
Quoted 9 lines · Sean Christopherson
Quoted 8 lines · Yosry Ahmed

There are multiple selftests exercising nested VMX that are not specific to VMX (at least not anymore). Extend their coverage to nested SVM.

This version is significantly different (and longer) than v1 [1], mainly due to the change of direction to reuse __virt_pg_map() for nested EPT/NPT mappings instead of extending the existing nested EPT infrastructure. It also has a lot more fixups and cleanups.

Quoted 6 lines
Quoted 3 lines

This series depends on two other series:
- "KVM: SVM: GIF and EFER.SVME are independent" [2]
- "KVM: selftests: Add test of SET_NESTED_STATE with 48-bit L2 on 57-bit L1" [3]

No, it depends on local commits that are very similar to [3], but not precisely [3].

Hmm I just applied that series with b4 without local changes, on top of kvm-x86/next at that time, which was kvm-x86-next-2025.09.30.

Maybe you had v2 or it was the patches that landed between kvm-x86-next-2025.09.30 and the current tip of kvm-x86/next?

Quoted 3 lines

In the future, please provide a link to a git repo+branch when posting series with dependencies. It took me several attempts and a bit of conflict resolution to get this series applied.

Yeah I can do that, although I think it wouldn't have helped in this case as the same conflicts would apply. Perhaps mentioning that this is based on kvm-x86-next-2025.09.30 would have helped?

Quoted 4 lines
Quoted 4 lines

[1]https://lore.kernel.org/kvm/[email protected]/ [2]https://lore.kernel.org/kvm/[email protected]/ [3]https://lore.kernel.org/kvm/[email protected]/

#44 lore raw
In reply to Yosry Ahmed
Quoted 8 lines · Yosry Ahmed

There are multiple selftests exercising nested VMX that are not specific to VMX (at least not anymore). Extend their coverage to nested SVM.

This version is significantly different (and longer) than v1 [1], mainly due to the change of direction to reuse __virt_pg_map() for nested EPT/NPT mappings instead of extending the existing nested EPT infrastructure. It also has a lot more fixups and cleanups.

Quoted 6 lines

This series depends on two other series:
- "KVM: SVM: GIF and EFER.SVME are independent" [2]
- "KVM: selftests: Add test of SET_NESTED_STATE with 48-bit L2 on 57-bit L1" [3]

The dependency on the former is because set_nested_state_test is now also a regression test for that fix.

Uh, then the selftest change absolutely should be sent at the same time as the KVM change. One of the big benefits of selftests over KUT is that selftests are in the same repo as KVM. We should almost never have to coordinate selftests chagnes against KVM changes across different series.

Quoted 1 line

The dependency on the latter is purely to avoid conflicts.

Similar to my feedback on your mega-series for KUT, don't bundle unrelated patches without good reason (and no reason _NOT_ to bundle them).

I want to immediate take the patches that aren't related to the paging API changes, but that's proving to be difficult because there are superficial dependencies on Jim's LA57 changes, and I need to drop the vmx_set_nested_state_test changes because they belong elsewhere.

Bundling these is fine since they're thematically related and do generate superficial conflicts, though even then I would be a-ok with splitting these up (superficial conflicts are trivial to resolve (knock wood), and avoiding such conflicts isn't a good reason to bundle unrelated things).

KVM: selftests: Extend vmx_tsc_adjust_test to cover SVM
KVM: selftests: Extend nested_invalid_cr3_test to cover SVM
KVM: selftests: Move nested invalid CR3 check to its own test
KVM: selftests: Extend vmx_nested_tsc_scaling_test to cover SVM
KVM: selftests: Extend vmx_close_while_nested_test to cover SVM

#45 lore raw
Quoted 13 lines · Sean Christopherson
Quoted 12 lines · Yosry Ahmed

There are multiple selftests exercising nested VMX that are not specific to VMX (at least not anymore). Extend their coverage to nested SVM.

This version is significantly different (and longer) than v1 [1], mainly due to the change of direction to reuse __virt_pg_map() for nested EPT/NPT mappings instead of extending the existing nested EPT infrastructure. It also has a lot more fixups and cleanups.

This series depends on two other series:
- "KVM: SVM: GIF and EFER.SVME are independent" [2]
- "KVM: selftests: Add test of SET_NESTED_STATE with 48-bit L2 on 57-bit L1" [3]

Quoted 7 lines
Quoted 2 lines

The dependency on the former is because set_nested_state_test is now also a regression test for that fix.

Uh, then the selftest change absolutely should be sent at the same time as the KVM change. One of the big benefits of selftests over KUT is that selftests are in the same repo as KVM. We should almost never have to coordinate selftests chagnes against KVM changes across different series.

Yeah that didn't work out well. I saw Jim's fixes as I was working on that test and thought might as well test for Jim's changes. In retrospect I should have split this into two patches.

Quoted 5 lines
Quoted 2 lines

The dependency on the latter is purely to avoid conflicts.

Similar to my feedback on your mega-series for KUT, don't bundle unrelated patches without good reason (and no reason _NOT_ to bundle them).

Noted.

Quoted 11 lines

I want to immediate take the patches that aren't related to the paging API changes, but that's proving to be difficult because there are superficial dependencies on Jim's LA57 changes, and I need to drop the vmx_set_nested_state_test changes because they belong elsewhere.

Bundling these is fine since they're thematically related and do generate superficial conflicts, though even then I would be a-ok with splitting these up (superficial conflicts are trivial to resolve (knock wood), and avoiding such conflicts isn't a good reason to bundle unrelated things).

Quoted 5 lines

KVM: selftests: Extend vmx_tsc_adjust_test to cover SVM
KVM: selftests: Extend nested_invalid_cr3_test to cover SVM
KVM: selftests: Move nested invalid CR3 check to its own test
KVM: selftests: Extend vmx_nested_tsc_scaling_test to cover SVM
KVM: selftests: Extend vmx_close_while_nested_test to cover SVM

Not sure I understand how you to proceed. Do you want me to respin these patches separately (as series A), on top of kvm-x86/next, and then respin the rest of the series separately (as series B, with your struct kvm_mmu suggestion)?

As for set_nested_state, if you plan to pickup Jim's EFER fixes I can just include it as-is in series (A). If not, I can include generalization of the test, and send covering Jim's fix separately.

Series B will still need to depend on Jim's selftests changes, if you're planning to pick those up soon I can base my changes on whatever branch you'll use. Otherwise I can resend both together, maybe?

#46 lore raw
Quoted 11 lines · Yosry Ahmed
Quoted 5 lines · Sean Christopherson

KVM: selftests: Extend vmx_tsc_adjust_test to cover SVM
KVM: selftests: Extend nested_invalid_cr3_test to cover SVM
KVM: selftests: Move nested invalid CR3 check to its own test
KVM: selftests: Extend vmx_nested_tsc_scaling_test to cover SVM
KVM: selftests: Extend vmx_close_while_nested_test to cover SVM

Not sure I understand how you to proceed. Do you want me to respin these patches separately (as series A), on top of kvm-x86/next, and then respin the rest of the series separately (as series B, with your struct kvm_mmu suggestion)?

I'm going to apply a subset "soon", hopefully they'll show up in kvm-x86/next tomorrow. I think it's patches 3-9?

Quoted 3 lines

As for set_nested_state, if you plan to pickup Jim's EFER fixes I can just include it as-is in series (A). If not, I can include generalization of the test, and send covering Jim's fix separately.

We're likely going to need a v3 of Jim's GIF series no matter what, so let's plan on bundling patches 1-2 with v3 of that series.

That leaves the paging patches. Unless you're super duper speedy, I should get patches 3-9 and Jim's LA57 changes+test pushed to kvm-x86 before you're ready to post the next version of those patches.

So:
Fold 1-2 into Jim's GIF series.
Do nothing for 3-9.
Spin a new version of 10+ (the paging patches) after kvm-x86/next is refreshed

#47 lore raw
Quoted 8 lines · Sean Christopherson
Quoted 7 lines · Yosry Ahmed
Quoted 5 lines · Sean Christopherson

KVM: selftests: Extend vmx_tsc_adjust_test to cover SVM
KVM: selftests: Extend nested_invalid_cr3_test to cover SVM
KVM: selftests: Move nested invalid CR3 check to its own test
KVM: selftests: Extend vmx_nested_tsc_scaling_test to cover SVM
KVM: selftests: Extend vmx_close_while_nested_test to cover SVM

Quoted 7 lines
Quoted 4 lines

Not sure I understand how you to proceed. Do you want me to respin these patches separately (as series A), on top of kvm-x86/next, and then respin the rest of the series separately (as series B, with your struct kvm_mmu suggestion)?

I'm going to apply a subset "soon", hopefully they'll show up in kvm-x86/next tomorrow. I think it's patches 3-9?

I think 10 and 11 should also be good to go, unless you have reason to think otherwise.

Quoted 8 lines
Quoted 4 lines

As for set_nested_state, if you plan to pickup Jim's EFER fixes I can just include it as-is in series (A). If not, I can include generalization of the test, and send covering Jim's fix separately.

We're likely going to need a v3 of Jim's GIF series no matter what, so let's plan on bundling patches 1-2 with v3 of that series.

Quoted 8 lines

That leaves the paging patches. Unless you're super duper speedy, I should get patches 3-9 and Jim's LA57 changes+test pushed to kvm-x86 before you're ready to post the next version of those patches.

So:
Fold 1-2 into Jim's GIF series.
Do nothing for 3-9.
Spin a new version of 10+ (the paging patches) after kvm-x86/next is refreshed

Makes sense, I will coordinate with Jim. Thanks!

#48 lore raw
In reply to Yosry Ahmed
Quoted 9 lines · Yosry Ahmed

There are multiple selftests exercising nested VMX that are not specific to VMX (at least not anymore). Extend their coverage to nested SVM.

This version is significantly different (and longer) than v1 [1], mainly due to the change of direction to reuse __virt_pg_map() for nested EPT/NPT mappings instead of extending the existing nested EPT infrastructure. It also has a lot more fixups and cleanups.

[...]

Applied 3-11 to kvm-x86 selftests, thanks!

[03/23] KVM: selftests: Extend vmx_close_while_nested_test to cover SVM

        https://github.com/kvm-x86/linux/commit/0a9eb2afa185
[04/23] KVM: selftests: Extend vmx_nested_tsc_scaling_test to cover SVM
        https://github.com/kvm-x86/linux/commit/e6bcdd212238
[05/23] KVM: selftests: Move nested invalid CR3 check to its own test
        https://github.com/kvm-x86/linux/commit/4d256d00e44e
[06/23] KVM: selftests: Extend nested_invalid_cr3_test to cover SVM
        https://github.com/kvm-x86/linux/commit/91423b041d3c
[07/23] KVM: selftests: Extend vmx_tsc_adjust_test to cover SVM
        https://github.com/kvm-x86/linux/commit/3c40777f0ed8
[08/23] KVM: selftests: Stop hardcoding PAGE_SIZE in x86 selftests
        https://github.com/kvm-x86/linux/commit/28b2dced8ba4
[09/23] KVM: selftests: Remove the unused argument to prepare_eptp()
        https://github.com/kvm-x86/linux/commit/ff736dba478c
[10/23] KVM: selftests: Stop using __virt_pg_map() directly in tests
        https://github.com/kvm-x86/linux/commit/1de4dc15baa1
[11/23] KVM: selftests: Make sure vm->vpages_mapped is always up-to-date
        https://github.com/kvm-x86/linux/commit/d2e50389ab44

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