Skip to contentSkip to conversation

Extend test coverage for nested SVM

PATCH Superseded Yosry Ahmed 3 participants 33 messages Oct 1, 2025 – Oct 15, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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.

Yosry Ahmed (12):
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: Remove invalid CR3 test from vmx_tsc_adjust_test
KVM: selftests: Extend vmx_tsc_adjust_test to cover SVM
KVM: selftests: Pass the root HVA directly to nested mapping functions
KVM: selftests: Use 'leaf' instead of hugepage to describe EPT entries
KVM: selftests: Move all PTE accesses into nested_create_pte()
KVM: selftests: Move EPT-specific init outside nested_create_pte()
KVM: selftests: Refactor generic nested mapping outside VMX code
KVM: selftests: Extend vmx_dirty_log_test to cover SVM

 tools/testing/selftests/kvm/Makefile.kvm      |  11 +-
 .../selftests/kvm/include/x86/nested_map.h    |  20 ++
 .../selftests/kvm/include/x86/svm_util.h      |  13 ++
 tools/testing/selftests/kvm/include/x86/vmx.h |  13 +-
 .../testing/selftests/kvm/lib/x86/memstress.c |   5 +-
 .../selftests/kvm/lib/x86/nested_map.c        | 150 +++++++++++++++
 tools/testing/selftests/kvm/lib/x86/svm.c     |  70 +++++++
 tools/testing/selftests/kvm/lib/x86/vmx.c     | 180 +++---------------
 ...ested_test.c => close_while_nested_test.c} |  42 +++-
 ...rty_log_test.c => nested_dirty_log_test.c} |  95 ++++++---
 ...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} | 132 +++++++++++--
 13 files changed, 609 insertions(+), 249 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/include/x86/nested_map.h
 create mode 100644 tools/testing/selftests/kvm/lib/x86/nested_map.c
 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} (62%)
 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.618.g983fd99d29-goog
#1 lore raw

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

From: Yosry Ahmed <[email protected]>

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.618.g983fd99d29-goog
#3 lore raw

PATCH 2/12 KVM: selftests: Extend vmx_set_nested_state_test to cover SVM

In reply to Yosry Ahmed

From: Yosry Ahmed <[email protected]>

Add test cases for the validation checks in svm_set_nested_state(), and allow the test to run with SVM as well as VMX.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   2 +-
 ...d_state_test.c => set_nested_state_test.c} | 122 ++++++++++++++++--
 2 files changed, 113 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 148d427ff24be..6582396518b19 100644
@@ -116,7 +116,7 @@ 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/set_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/apic_bus_clock_test
tools/testing/selftests/kvm/x86/set_nested_state_test.c renamed from tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c +112 -10

157 lines not on this page · raw message

Signature · 1 line
2.51.0.618.g983fd99d29-goog
#4 lore raw

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

Quoted 10 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

Add test cases for the validation checks in svm_set_nested_state(), and allow the test to run with SVM as well as VMX.

---
...
+ /* If EFER.SVME is clear, GIF must be set and guest mode is disallowed */

The GIF constraint is not architected. See
https://lore.kernel.org/kvm/[email protected]/.

#5 lore raw

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

Quoted 7 lines · Jim Mattson
Quoted 6 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

Add test cases for the validation checks in svm_set_nested_state(), and allow the test to run with SVM as well as VMX.

Quoted 7 lines
Quoted 4 lines

---
...
+ /* If EFER.SVME is clear, GIF must be set and guest mode is disallowed */

The GIF constraint is not architected. See
https://lore.kernel.org/kvm/[email protected]/.

Yes, I didn't connect our discussion about clearing GIF when EFER.SVME is cleared to the nested state code. I will remove the GIF assertion in the next version.

Thanks!

#6 lore raw

PATCH 3/12 KVM: selftests: Extend vmx_close_while_nested_test to cover SVM

In reply to Yosry Ahmed

From: Yosry Ahmed <[email protected]>

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 6582396518b19..61c6abdc9b36f 100644
@@ -111,7 +111,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.618.g983fd99d29-goog
#8 lore raw

PATCH 4/12 KVM: selftests: Extend vmx_nested_tsc_scaling_test to cover SVM

In reply to Yosry Ahmed

From: Yosry Ahmed <[email protected]>

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 61c6abdc9b36f..dc68147ace97f 100644
@@ -118,7 +118,7 @@ TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
 TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state
 TEST_GEN_PROGS_x86 += x86/set_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.618.g983fd99d29-goog
#9 lore raw

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

tools/testing/selftests/kvm/x86/nested_tsc_scaling_test.c static void l2_guest_code(void) · line 94

Quoted 11 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

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/x86/nested_tsc_scaling_test.c renamed from tools/testing/selftests/kvm/x86/vmx_nested_tsc_scaling_test.c +2 -0
+       /* enable TSC scaling for L2 */
+       wrmsr(MSR_AMD64_TSC_RATIO, (L2_SCALE_FACTOR << 32) | 1);

Why set bit 0 here?

#10 lore raw

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

Quoted 7 lines · Jim Mattson
Quoted 6 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

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

Quoted 7 lines

tools/testing/selftests/kvm/x86/nested_tsc_scaling_test.c static void l2_guest_code(void) · line 94

Quoted 5 lines

---
...

tools/testing/selftests/kvm/x86/nested_tsc_scaling_test.c renamed from tools/testing/selftests/kvm/x86/vmx_nested_tsc_scaling_test.c +2 -0
+       /* enable TSC scaling for L2 */
+       wrmsr(MSR_AMD64_TSC_RATIO, (L2_SCALE_FACTOR << 32) | 1);

Why set bit 0 here?

Brainfart. Will fix in the next version, thanks.

#11 lore raw

PATCH 5/12 KVM: selftests: Remove invalid CR3 test from vmx_tsc_adjust_test

In reply to Yosry Ahmed

From: Yosry Ahmed <[email protected]>

Checking that VMLAUNCH fails with an invalid CR3 is irrelevant to this test. Remove it to simplify the test a little bit before generalizing it to cover SVM.

---
 tools/testing/selftests/kvm/x86/vmx_tsc_adjust_test.c | 10 ----------
 1 file changed, 10 deletions(-)
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.618.g983fd99d29-goog
#12 lore raw

Re: [PATCH 05/12] KVM: selftests: Remove invalid CR3 test from vmx_tsc_adjust_test

Quoted 8 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

Checking that VMLAUNCH fails with an invalid CR3 is irrelevant to this test. Remove it to simplify the test a little bit before generalizing it to cover SVM.

Is there value in moving the invalid CR3 test elsewhere, rather than just eliminating it?

#13 lore raw

Re: [PATCH 05/12] KVM: selftests: Remove invalid CR3 test from vmx_tsc_adjust_test

Quoted 4 lines · Jim Mattson
Quoted 3 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

Quoted 8 lines
Quoted 5 lines

Checking that VMLAUNCH fails with an invalid CR3 is irrelevant to this test. Remove it to simplify the test a little bit before generalizing it to cover SVM.

Is there value in moving the invalid CR3 test elsewhere, rather than just eliminating it?

It's a very basic test, but yeah we can keep it. I was doing the lazy thing by just dropping it to see if anyone will comment.

I will find a new home for it (or keep it here out of more laziness).

#14 lore raw

PATCH 6/12 KVM: selftests: Extend vmx_tsc_adjust_test to cover SVM

In reply to Yosry Ahmed

From: Yosry Ahmed <[email protected]>

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 dc68147ace97f..9b3c99acd51a3 100644
@@ -117,7 +117,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/set_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.618.g983fd99d29-goog
#16 lore raw

PATCH 7/12 KVM: selftests: Pass the root HVA directly to nested mapping functions

In reply to Yosry Ahmed

From: Yosry Ahmed <[email protected]>

The nested mapping functions used to create EPT mappings currently accept a struct vmx_pages argument, only to get the EPT root from it later. In preparation for generalizing these functions to work for NPTs, pass the EPT root HVA directly instead.

---
 tools/testing/selftests/kvm/include/x86/vmx.h |  8 +++----
 .../testing/selftests/kvm/lib/x86/memstress.c |  4 ++--
 tools/testing/selftests/kvm/lib/x86/vmx.c     | 24 +++++++++----------
 .../selftests/kvm/x86/vmx_dirty_log_test.c    |  6 ++---
 4 files changed, 21 insertions(+), 21 deletions(-)
tools/testing/selftests/kvm/include/x86/vmx.h +4 -4
index edb3c391b9824..06ae68cf9635c 100644
@@ -559,13 +559,13 @@ 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,
+void nested_pg_map(void *root_hva, struct kvm_vm *vm,
 		   uint64_t nested_paddr, uint64_t paddr);
-void nested_map(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_map(void *root_hva, 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,
+void nested_map_memslot(void *root_hva, struct kvm_vm *vm,
 			uint32_t memslot);
-void nested_identity_map_1g(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_identity_map_1g(void *root_hva, 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,
tools/testing/selftests/kvm/lib/x86/memstress.c +2 -2
index 7f5d62a65c68a..7981e295cac70 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(vmx->eptp_hva, vm, 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(vmx->eptp_hva, vm, 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 +12 -12
index d4d1208dd0238..04c4b97bcd1e7 100644
@@ -394,11 +394,11 @@ static void nested_create_pte(struct kvm_vm *vm,
 }
 
 
-void __nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm,
+void __nested_pg_map(void *root_hva, struct kvm_vm *vm,
 		     uint64_t nested_paddr, uint64_t paddr, int target_level)
 {
 	const uint64_t page_size = PG_LEVEL_SIZE(target_level);
-	struct eptPageTableEntry *pt = vmx->eptp_hva, *pte;
+	struct eptPageTableEntry *pt = root_hva, *pte;
 	uint16_t index;
 
 	TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use "
@@ -445,10 +445,10 @@ void __nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm,
 
 }
 
-void nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_pg_map(void *root_hva, struct kvm_vm *vm,
 		   uint64_t nested_paddr, uint64_t paddr)
 {
-	__nested_pg_map(vmx, vm, nested_paddr, paddr, PG_LEVEL_4K);
+	__nested_pg_map(root_hva, vm, nested_paddr, paddr, PG_LEVEL_4K);
 }
 
 /*
@@ -468,7 +468,7 @@ void nested_pg_map(struct vmx_pages *vmx, struct kvm_vm *vm,
  * 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(void *root_hva, struct kvm_vm *vm,
 		  uint64_t nested_paddr, uint64_t paddr, uint64_t size,
 		  int level)
 {
@@ -479,22 +479,22 @@ void __nested_map(struct vmx_pages *vmx, struct kvm_vm *vm,
 	TEST_ASSERT(paddr + size > paddr, "Paddr overflow");
 
 	while (npages--) {
-		__nested_pg_map(vmx, vm, nested_paddr, paddr, level);
+		__nested_pg_map(root_hva, vm, nested_paddr, paddr, level);
 		nested_paddr += page_size;
 		paddr += page_size;
 	}
 }
 
-void nested_map(struct vmx_pages *vmx, struct kvm_vm *vm,
+void nested_map(void *root_hva, struct kvm_vm *vm,
 		uint64_t nested_paddr, uint64_t paddr, uint64_t size)
 {
-	__nested_map(vmx, vm, nested_paddr, paddr, size, PG_LEVEL_4K);
+	__nested_map(root_hva, vm, 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(void *root_hva, struct kvm_vm *vm,
 			uint32_t memslot)
 {
 	sparsebit_idx_t i, last;
@@ -508,7 +508,7 @@ void nested_map_memslot(struct vmx_pages *vmx, struct kvm_vm *vm,
 		if (i > last)
 			break;
 
-		nested_map(vmx, vm,
+		nested_map(root_hva, vm,
 			   (uint64_t)i << vm->page_shift,
 			   (uint64_t)i << vm->page_shift,
 			   1 << vm->page_shift);
@@ -516,10 +516,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(void *root_hva, struct kvm_vm *vm,
 			    uint64_t addr, uint64_t size)
 {
-	__nested_map(vmx, vm, addr, addr, size, PG_LEVEL_1G);
+	__nested_map(root_hva, vm, 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 fa512d033205f..21a57805e9780 100644
@@ -121,9 +121,9 @@ 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_memslot(vmx->eptp_hva, vm, 0);
+		nested_map(vmx->eptp_hva, vm, NESTED_TEST_MEM1, GUEST_TEST_MEM, 4096);
+		nested_map(vmx->eptp_hva, vm, NESTED_TEST_MEM2, GUEST_TEST_MEM, 4096);
 	}
 
 	bmap = bitmap_zalloc(TEST_MEM_PAGES);
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Reviewed-by on v1 7/12
#17 lore raw

Re: [PATCH 07/12] KVM: selftests: Pass the root HVA directly to nested mapping functions

Quoted 9 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

The nested mapping functions used to create EPT mappings currently accept a struct vmx_pages argument, only to get the EPT root from it later. In preparation for generalizing these functions to work for NPTs, pass the EPT root HVA directly instead.

#18 lore raw

PATCH 8/12 KVM: selftests: Use 'leaf' instead of hugepage to describe EPT entries

In reply to Yosry Ahmed

From: Yosry Ahmed <[email protected]>

The assertions use 'hugepage' to describe a terminal EPT entry, but 'leaf' is more accruate as a PG_LEVEL_4K EPT entry is a leaf but not a hugepage. The distincion will be useful in coming changes that will pass the value around and 'leaf' is clearer than hugepage or page_size.

Leave the EPT bit named page_size to keep it conforming to the manual.

---
 tools/testing/selftests/kvm/lib/x86/vmx.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/lib/x86/vmx.c +5 -5
index 04c4b97bcd1e7..673756b27e903 100644
@@ -380,15 +380,15 @@ static void nested_create_pte(struct kvm_vm *vm,
 			pte->address = vm_alloc_page_table(vm) >> vm->page_shift;
 	} else {
 		/*
-		 * Entry already present.  Assert that the caller doesn't want
-		 * a hugepage at this level, and that there isn't a hugepage at
-		 * this level.
+		 * Entry already present.  Assert that the caller doesn't want a
+		 * leaf entry at this level, and that there isn't a leaf entry
+		 * at this level.
 		 */
 		TEST_ASSERT(current_level != target_level,
-			    "Cannot create hugepage at level: %u, nested_paddr: 0x%lx",
+			    "Cannot create leaf entry at level: %u, nested_paddr: 0x%lx",
 			    current_level, nested_paddr);
 		TEST_ASSERT(!pte->page_size,
-			    "Cannot create page table at level: %u, nested_paddr: 0x%lx",
+			    "Leaf entry already exists at level: %u, nested_paddr: 0x%lx",
 			    current_level, nested_paddr);
 	}
 }
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Reviewed-by on v1 8/12
#19 lore raw

Re: [PATCH 08/12] KVM: selftests: Use 'leaf' instead of hugepage to describe EPT entries

Quoted 5 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

The assertions use 'hugepage' to describe a terminal EPT entry, but 'leaf' is more accruate as a PG_LEVEL_4K EPT entry is a leaf but not a

Nit: accurate

Quoted 1 line

hugepage. The distincion will be useful in coming changes that will pass

Nit: distinction

Quoted 5 lines

the value around and 'leaf' is clearer than hugepage or page_size.

Leave the EPT bit named page_size to keep it conforming to the manual.

#20 lore raw

Re: [PATCH 08/12] KVM: selftests: Use 'leaf' instead of hugepage to describe EPT entries

In reply to Yosry Ahmed
Quoted 5 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

The assertions use 'hugepage' to describe a terminal EPT entry, but 'leaf' is more accruate as a PG_LEVEL_4K EPT entry is a leaf but not a hugepage.

Yes, it's more accurate, but also less precise. I'm guessing the assert message and comment talked about hugepages because that's the type of mappings that caused problems at the time.

Ah, actually, I bet the code was copy+pasted from virt_create_upper_pte(), in which case the assumptions about wanting to create a hupage are both accurate and precise.

Quoted 2 lines

The distincion will be useful in coming changes that will pass the value around and 'leaf' is clearer than hugepage or page_size.

What value?

Quoted 19 lines

Leave the EPT bit named page_size to keep it conforming to the manual.

---
 tools/testing/selftests/kvm/lib/x86/vmx.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/lib/x86/vmx.c +1 -3
index 04c4b97bcd1e7..673756b27e903 100644
@@ -380,15 +380,15 @@ static void nested_create_pte(struct kvm_vm *vm,
 			pte->address = vm_alloc_page_table(vm) >> vm->page_shift;
 	} else {
 		/*
-		 * Entry already present.  Assert that the caller doesn't want
-		 * a hugepage at this level, and that there isn't a hugepage at
-		 * this level.
+		 * Entry already present.  Assert that the caller doesn't want a

tools/testing/selftests/kvm/lib/x86/vmx.c static void nested_create_pte(struct kvm_vm *vm, · line 391

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +4 -2
+		 * leaf entry at this level, and that there isn't a leaf entry
+		 * at this level.
 		 */
 		TEST_ASSERT(current_level != target_level,
-			    "Cannot create hugepage at level: %u, nested_paddr: 0x%lx",
+			    "Cannot create leaf entry at level: %u, nested_paddr: 0x%lx",
 			    current_level, nested_paddr);
 		TEST_ASSERT(!pte->page_size,
-			    "Cannot create page table at level: %u, nested_paddr: 0x%lx",
+			    "Leaf entry already exists at level: %u, nested_paddr: 0x%lx",

This change is flat out wrong. The existing PRESENT PTE _might_ be a 4KiB leaf entry, but it might also be an existing non-leaf page table.

Instead of hacking on the nested code, can we instead tweak __virt_pg_map() to work with nested TDP? At a glance, it's already quite close, e.g. "just" needs to be taught about EPT RWX bits and allow the call to pass in the root pointer.

Quoted 6 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +0 -0
 			    current_level, nested_paddr);
 	}
 }
Signature · 1 line
2.51.0.618.g983fd99d29-goog
#21 lore raw

Re: [PATCH 08/12] KVM: selftests: Use 'leaf' instead of hugepage to describe EPT entries

Quoted 10 lines · Sean Christopherson
Quoted 5 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

The assertions use 'hugepage' to describe a terminal EPT entry, but 'leaf' is more accruate as a PG_LEVEL_4K EPT entry is a leaf but not a hugepage.

Yes, it's more accurate, but also less precise. I'm guessing the assert message and comment talked about hugepages because that's the type of mappings that caused problems at the time.

Given that it refers to PG_LEVEL_4K entries too, I wouldn't call it less precise. All callers actually create 4K mappings so it is never actually a hugepage in the current context :D

Quoted 9 lines

Ah, actually, I bet the code was copy+pasted from virt_create_upper_pte(), in which case the assumptions about wanting to create a hupage are both accurate and precise.

Quoted 2 lines

The distincion will be useful in coming changes that will pass the value around and 'leaf' is clearer than hugepage or page_size.

What value?

'leaf'. The following changes will pass 'leaf' in as a boolean instead of checking 'current_level == target_level' here. So passing in 'hugepage' would be inaccurate, and 'page_size' is not as clear (but still works).

Quoted 25 lines
Quoted 25 lines

Leave the EPT bit named page_size to keep it conforming to the manual.

---
 tools/testing/selftests/kvm/lib/x86/vmx.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/lib/x86/vmx.c +3 -4
index 04c4b97bcd1e7..673756b27e903 100644
@@ -380,15 +380,15 @@ static void nested_create_pte(struct kvm_vm *vm,
 			pte->address = vm_alloc_page_table(vm) >> vm->page_shift;
 	} else {
 		/*
-		 * Entry already present.  Assert that the caller doesn't want
-		 * a hugepage at this level, and that there isn't a hugepage at
-		 * this level.
+		 * Entry already present.  Assert that the caller doesn't want a
+		 * leaf entry at this level, and that there isn't a leaf entry
+		 * at this level.
 		 */
 		TEST_ASSERT(current_level != target_level,
-			    "Cannot create hugepage at level: %u, nested_paddr: 0x%lx",
Quoted 8 lines

tools/testing/selftests/kvm/lib/x86/vmx.c static void nested_create_pte(struct kvm_vm *vm, · line 391

Quoted 5 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +2 -1
+			    "Cannot create leaf entry at level: %u, nested_paddr: 0x%lx",
 			    current_level, nested_paddr);
 		TEST_ASSERT(!pte->page_size,
-			    "Cannot create page table at level: %u, nested_paddr: 0x%lx",
+			    "Leaf entry already exists at level: %u, nested_paddr: 0x%lx",

This change is flat out wrong. The existing PRESENT PTE _might_ be a 4KiB leaf entry, but it might also be an existing non-leaf page table.

Hmm if pte->page_size is true then it has to be a leaf page table, right?

If it's an existing non-leaf page table we shouldn't fail, the assertion here is when we try to override a leaf page table IIUC.

Quoted 4 lines

Instead of hacking on the nested code, can we instead tweak __virt_pg_map() to work with nested TDP? At a glance, it's already quite close, e.g. "just" needs to be taught about EPT RWX bits and allow the call to pass in the root pointer.

That would be ideal, I'll take a look. In case I don't have time for that unification, can this be a follow-up change?

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +0 -0
 			    current_level, nested_paddr);
 	}
 }
Signature · 1 line
2.51.0.618.g983fd99d29-goog
#22 lore raw

Re: [PATCH 08/12] KVM: selftests: Use 'leaf' instead of hugepage to describe EPT entries

Quoted 8 lines · Yosry Ahmed
Quoted 7 lines · Sean Christopherson
Quoted 5 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

The assertions use 'hugepage' to describe a terminal EPT entry, but 'leaf' is more accruate as a PG_LEVEL_4K EPT entry is a leaf but not a hugepage.

Quoted 7 lines
Quoted 3 lines

Yes, it's more accurate, but also less precise. I'm guessing the assert message and comment talked about hugepages because that's the type of mappings that caused problems at the time.

Given that it refers to PG_LEVEL_4K entries too, I wouldn't call it less precise. All callers actually create 4K mappings so it is never actually a hugepage in the current context :D

nested_identity_map_1g()?

Quoted 48 lines
Quoted 8 lines

Ah, actually, I bet the code was copy+pasted from virt_create_upper_pte(), in which case the assumptions about wanting to create a hupage are both accurate and precise.

Quoted 2 lines

The distincion will be useful in coming changes that will pass the value around and 'leaf' is clearer than hugepage or page_size.

What value?

'leaf'. The following changes will pass 'leaf' in as a boolean instead of checking 'current_level == target_level' here. So passing in 'hugepage' would be inaccurate, and 'page_size' is not as clear (but still works).

Quoted 25 lines
Quoted 25 lines

Leave the EPT bit named page_size to keep it conforming to the manual.

---
 tools/testing/selftests/kvm/lib/x86/vmx.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/lib/x86/vmx.c +3 -4
index 04c4b97bcd1e7..673756b27e903 100644
@@ -380,15 +380,15 @@ static void nested_create_pte(struct kvm_vm *vm,
 			pte->address = vm_alloc_page_table(vm) >> vm->page_shift;
 	} else {
 		/*
-		 * Entry already present.  Assert that the caller doesn't want
-		 * a hugepage at this level, and that there isn't a hugepage at
-		 * this level.
+		 * Entry already present.  Assert that the caller doesn't want a
+		 * leaf entry at this level, and that there isn't a leaf entry
+		 * at this level.
 		 */
 		TEST_ASSERT(current_level != target_level,
-			    "Cannot create hugepage at level: %u, nested_paddr: 0x%lx",
Quoted 8 lines

tools/testing/selftests/kvm/lib/x86/vmx.c static void nested_create_pte(struct kvm_vm *vm, · line 391

Quoted 5 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +2 -1
+			    "Cannot create leaf entry at level: %u, nested_paddr: 0x%lx",
 			    current_level, nested_paddr);
 		TEST_ASSERT(!pte->page_size,
-			    "Cannot create page table at level: %u, nested_paddr: 0x%lx",
+			    "Leaf entry already exists at level: %u, nested_paddr: 0x%lx",

This change is flat out wrong. The existing PRESENT PTE _might_ be a 4KiB leaf entry, but it might also be an existing non-leaf page table.

Quoted 2 lines

Hmm if pte->page_size is true then it has to be a leaf page table, right?

No, because bit 7 is ignored by hardware for 4KiB entries. I.e. it can be 0 or 1 depending on the whims of software. Ugh, this code uses bit 7 to flag leaf entries. That's lovely.

Quoted 1 line

If it's an existing non-leaf page table we shouldn't fail,

Ah, right, current_level can never be less than target_level because the first assert will fail on iteration-1.

Quoted 8 lines

the assertion here is when we try to override a leaf page table IIUC.

Quoted 3 lines

Instead of hacking on the nested code, can we instead tweak __virt_pg_map() to work with nested TDP? At a glance, it's already quite close, e.g. "just" needs to be taught about EPT RWX bits and allow the call to pass in the root pointer.

That would be ideal, I'll take a look. In case I don't have time for that unification, can this be a follow-up change?

Part of me wants to be nice and say "yes", but most of me wants to say "no".

Struct overlays for PTEs suck. At best, they generate poor code and obfuscate simple logic (e.g. vm->page_size vs pte->page_size is a confusion that simply should not be possible). At worst, they lead to hard-to-debug issues like the one that led to commit f18b4aebe107 ("kvm: selftests: do not use bitfields larger than 32-bits for PTEs").

eptPageTableEntry obviously isn't your fault, but nptPageTableEntry is. :-D And I suspect the hardest part of unificiation will be adding the globals to deal with variable bit positions that are currently being handled by the struct overlays.

#23 lore raw

Re: [PATCH 08/12] KVM: selftests: Use 'leaf' instead of hugepage to describe EPT entries

Quoted 13 lines · Sean Christopherson
Quoted 12 lines · Yosry Ahmed
Quoted 10 lines · Sean Christopherson
Quoted 5 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

The assertions use 'hugepage' to describe a terminal EPT entry, but 'leaf' is more accruate as a PG_LEVEL_4K EPT entry is a leaf but not a hugepage.

Yes, it's more accurate, but also less precise. I'm guessing the assert message and comment talked about hugepages because that's the type of mappings that caused problems at the time.

Quoted 5 lines
Quoted 3 lines

Given that it refers to PG_LEVEL_4K entries too, I wouldn't call it less precise. All callers actually create 4K mappings so it is never actually a hugepage in the current context :D

nested_identity_map_1g()?

Yeah I missed this one.

Quoted 49 lines
Quoted 49 lines
Quoted 9 lines

Ah, actually, I bet the code was copy+pasted from virt_create_upper_pte(), in which case the assumptions about wanting to create a hupage are both accurate and precise.

Quoted 2 lines

The distincion will be useful in coming changes that will pass the value around and 'leaf' is clearer than hugepage or page_size.

What value?

'leaf'. The following changes will pass 'leaf' in as a boolean instead of checking 'current_level == target_level' here. So passing in 'hugepage' would be inaccurate, and 'page_size' is not as clear (but still works).

Quoted 33 lines
Quoted 20 lines

Leave the EPT bit named page_size to keep it conforming to the manual.

---
 tools/testing/selftests/kvm/lib/x86/vmx.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/lib/x86/vmx.c +1 -3
index 04c4b97bcd1e7..673756b27e903 100644
@@ -380,15 +380,15 @@ static void nested_create_pte(struct kvm_vm *vm,
 			pte->address = vm_alloc_page_table(vm) >> vm->page_shift;
 	} else {
 		/*
-		 * Entry already present.  Assert that the caller doesn't want
-		 * a hugepage at this level, and that there isn't a hugepage at
-		 * this level.
+		 * Entry already present.  Assert that the caller doesn't want a

tools/testing/selftests/kvm/lib/x86/vmx.c static void nested_create_pte(struct kvm_vm *vm, · line 391

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +4 -2
+		 * leaf entry at this level, and that there isn't a leaf entry
+		 * at this level.
 		 */
 		TEST_ASSERT(current_level != target_level,
-			    "Cannot create hugepage at level: %u, nested_paddr: 0x%lx",
+			    "Cannot create leaf entry at level: %u, nested_paddr: 0x%lx",
 			    current_level, nested_paddr);
 		TEST_ASSERT(!pte->page_size,
-			    "Cannot create page table at level: %u, nested_paddr: 0x%lx",
+			    "Leaf entry already exists at level: %u, nested_paddr: 0x%lx",

This change is flat out wrong. The existing PRESENT PTE _might_ be a 4KiB leaf entry, but it might also be an existing non-leaf page table.

Quoted 6 lines
Quoted 2 lines

Hmm if pte->page_size is true then it has to be a leaf page table, right?

No, because bit 7 is ignored by hardware for 4KiB entries. I.e. it can be 0 or 1 depending on the whims of software. Ugh, this code uses bit 7 to flag leaf entries. That's lovely.

That's not my fault :P

Quoted 8 lines
Quoted 2 lines

If it's an existing non-leaf page table we shouldn't fail,

Ah, right, current_level can never be less than target_level because the first assert will fail on iteration-1.

Quoted 2 lines

the assertion here is when we try to override a leaf page table IIUC.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Instead of hacking on the nested code, can we instead tweak __virt_pg_map() to work with nested TDP? At a glance, it's already quite close, e.g. "just" needs to be taught about EPT RWX bits and allow the call to pass in the root pointer.

That would be ideal, I'll take a look. In case I don't have time for that unification, can this be a follow-up change?

Part of me wants to be nice and say "yes", but most of me wants to say "no".

So.. which part won?

Quoted 11 lines

Struct overlays for PTEs suck. At best, they generate poor code and obfuscate simple logic (e.g. vm->page_size vs pte->page_size is a confusion that simply should not be possible). At worst, they lead to hard-to-debug issues like the one that led to commit f18b4aebe107 ("kvm: selftests: do not use bitfields larger than 32-bits for PTEs").

eptPageTableEntry obviously isn't your fault, but nptPageTableEntry is. :-D And I suspect the hardest part of unificiation will be adding the globals to deal with variable bit positions that are currently being handled by the struct overlays.

I have no problem getting rid of eptPageTableEntry and using bitmasks and whatnot on a uint64_t PTE (assuming that's what you are asking for here).

But I think tweaking __virt_pg_map() will involve more than that, or maybe I just didn't look close enough yet.

#24 lore raw

Re: [PATCH 08/12] KVM: selftests: Use 'leaf' instead of hugepage to describe EPT entries

Quoted 29 lines · Yosry Ahmed
Quoted 5 lines · Sean Christopherson

Ah, right, current_level can never be less than target_level because the first assert will fail on iteration-1.

Quoted 2 lines

the assertion here is when we try to override a leaf page table IIUC.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Instead of hacking on the nested code, can we instead tweak __virt_pg_map() to work with nested TDP? At a glance, it's already quite close, e.g. "just" needs to be taught about EPT RWX bits and allow the call to pass in the root pointer.

That would be ideal, I'll take a look. In case I don't have time for that unification, can this be a follow-up change?

Part of me wants to be nice and say "yes", but most of me wants to say "no".

So.. which part won?

Quoted 11 lines

Struct overlays for PTEs suck. At best, they generate poor code and obfuscate simple logic (e.g. vm->page_size vs pte->page_size is a confusion that simply should not be possible). At worst, they lead to hard-to-debug issues like the one that led to commit f18b4aebe107 ("kvm: selftests: do not use bitfields larger than 32-bits for PTEs").

eptPageTableEntry obviously isn't your fault, but nptPageTableEntry is. :-D And I suspect the hardest part of unificiation will be adding the globals to deal with variable bit positions that are currently being handled by the struct overlays.

Quoted 6 lines

I have no problem getting rid of eptPageTableEntry and using bitmasks and whatnot on a uint64_t PTE (assuming that's what you are asking for here).

But I think tweaking __virt_pg_map() will involve more than that, or maybe I just didn't look close enough yet.

For posterity, Yosry and I chatted off-list. The plan is to try and convert __virt_pg_map() to work with variable "mmu", using an approach similar to KVM, where the bit positions of variable flags, e.g. NX vs. X, are stored in an mmu structure. If for whatever reason converting __virt_pg_map() is meaningfully harder than getting nEPT and nNPT to play nice, then we'll revisit all of this.

#25 lore raw

PATCH 9/12 KVM: selftests: Move all PTE accesses into nested_create_pte()

In reply to Yosry Ahmed

From: Yosry Ahmed <[email protected]>

In preparation for making the nested mapping functions work for NPT, move all logic that directly accesses the PTE into nested_create_pte(), as these accesses will be different for SVM.

Stop using struct eptPageTableEntry in the caller, instead pass a uint64_t pointer (and add an assertion on the size to make sure it stays correct).

Calculate whether or not an EPT entry is a leaf in __nested_pg_map(), and return the address from nested_create_pte() to __nested_pg_map(). Also, set the access and dirty bits in nested_create_pte() for leaf entries. This matches the current behavior and removes all direct accesses to the EPT entry from __nested_pg_map().

---
 tools/testing/selftests/kvm/lib/x86/vmx.c | 69 +++++++++++++----------
 1 file changed, 39 insertions(+), 30 deletions(-)
tools/testing/selftests/kvm/lib/x86/vmx.c +39 -30
index 673756b27e903..b0e6267eac806 100644
@@ -33,6 +33,7 @@ struct eptPageTableEntry {
 	uint64_t ignored_62_52:11;
 	uint64_t suppress_ve:1;
 };
+kvm_static_assert(sizeof(struct eptPageTableEntry) == sizeof(uint64_t));
 
 struct eptPageTablePointer {
 	uint64_t memory_type:3;
@@ -42,6 +43,8 @@ struct eptPageTablePointer {
 	uint64_t address:40;
 	uint64_t reserved_63_52:12;
 };
+kvm_static_assert(sizeof(struct eptPageTablePointer) == sizeof(uint64_t));
+
 int vcpu_enable_evmcs(struct kvm_vcpu *vcpu)
 {
 	uint16_t evmcs_ver;
@@ -362,35 +365,46 @@ void prepare_vmcs(struct vmx_pages *vmx, void *guest_rip, void *guest_rsp)
 	init_vmcs_guest_state(guest_rip, guest_rsp);
 }
 
-static void nested_create_pte(struct kvm_vm *vm,
-			      struct eptPageTableEntry *pte,
-			      uint64_t nested_paddr,
-			      uint64_t paddr,
-			      int current_level,
-			      int target_level)
+static uint64_t nested_create_pte(struct kvm_vm *vm,
+				  uint64_t *pte,
+				  uint64_t nested_paddr,
+				  uint64_t paddr,
+				  int level,
+				  bool leaf)
 {
-	if (!pte->readable) {
-		pte->writable = true;
-		pte->readable = true;
-		pte->executable = true;
-		pte->page_size = (current_level == target_level);
-		if (pte->page_size)
-			pte->address = paddr >> vm->page_shift;
+	struct eptPageTableEntry *epte = (struct eptPageTableEntry *)pte;
+
+	if (!epte->readable) {
+		epte->writable = true;
+		epte->readable = true;
+		epte->executable = true;
+		epte->page_size = leaf;
+
+		if (leaf)
+			epte->address = paddr >> vm->page_shift;
 		else
-			pte->address = vm_alloc_page_table(vm) >> vm->page_shift;
+			epte->address = vm_alloc_page_table(vm) >> vm->page_shift;
+
+		/*
+		 * For now mark these as accessed and dirty because the only
+		 * testcase we have needs that.  Can be reconsidered later.
+		 */
+		epte->accessed = leaf;
+		epte->dirty = leaf;
 	} else {
 		/*
 		 * Entry already present.  Assert that the caller doesn't want a
 		 * leaf entry at this level, and that there isn't a leaf entry
 		 * at this level.
 		 */
-		TEST_ASSERT(current_level != target_level,
+		TEST_ASSERT(!leaf,
 			    "Cannot create leaf entry at level: %u, nested_paddr: 0x%lx",
-			    current_level, nested_paddr);
-		TEST_ASSERT(!pte->page_size,
+			    level, nested_paddr);
+		TEST_ASSERT(!epte->page_size,
 			    "Leaf entry already exists at level: %u, nested_paddr: 0x%lx",
-			    current_level, nested_paddr);
+			    level, nested_paddr);
 	}
+	return epte->address;
 }
 
 
@@ -398,8 +412,9 @@ void __nested_pg_map(void *root_hva, struct kvm_vm *vm,
 		     uint64_t nested_paddr, uint64_t paddr, int target_level)
 {
 	const uint64_t page_size = PG_LEVEL_SIZE(target_level);
-	struct eptPageTableEntry *pt = root_hva, *pte;
-	uint16_t index;
+	uint64_t *pt = root_hva, *pte;
+	uint16_t index, address;
+	bool leaf;
 
 	TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use "
 		    "unknown or unsupported guest mode, mode: 0x%x", vm->mode);
@@ -427,22 +442,16 @@ void __nested_pg_map(void *root_hva, struct kvm_vm *vm,
 	for (int level = PG_LEVEL_512G; level >= PG_LEVEL_4K; level--) {
 		index = (nested_paddr >> PG_LEVEL_SHIFT(level)) & 0x1ffu;
 		pte = &pt[index];
+		leaf = (level == target_level);
 
-		nested_create_pte(vm, pte, nested_paddr, paddr, level, target_level);
+		address = nested_create_pte(vm, pte, nested_paddr, paddr, level, leaf);
 
-		if (pte->page_size)
+		if (leaf)
 			break;
 
-		pt = addr_gpa2hva(vm, pte->address * vm->page_size);
+		pt = addr_gpa2hva(vm, 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(void *root_hva, struct kvm_vm *vm,
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Reviewed-by on v1 9/12
#26 lore raw

Re: [PATCH 09/12] KVM: selftests: Move all PTE accesses into nested_create_pte()

Quoted 11 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

In preparation for making the nested mapping functions work for NPT, move all logic that directly accesses the PTE into nested_create_pte(), as these accesses will be different for SVM.

Stop using struct eptPageTableEntry in the caller, instead pass a uint64_t pointer (and add an assertion on the size to make sure it stays correct).

Quoted 7 lines

Calculate whether or not an EPT entry is a leaf in __nested_pg_map(), and return the address from nested_create_pte() to __nested_pg_map(). Also, set the access and dirty bits in nested_create_pte() for leaf entries. This matches the current behavior and removes all direct accesses to the EPT entry from __nested_pg_map().

#27 lore raw

PATCH 10/12 KVM: selftests: Move EPT-specific init outside nested_create_pte()

In reply to Yosry Ahmed

From: Yosry Ahmed <[email protected]>

Refactor the EPT specific initialization into nested_ept_create_pte(), in preparation for making nested_create_pte() NPT-friendly.

---
 tools/testing/selftests/kvm/lib/x86/vmx.c | 71 ++++++++++++++---------
 1 file changed, 43 insertions(+), 28 deletions(-)
tools/testing/selftests/kvm/lib/x86/vmx.c +43 -28
index b0e6267eac806..eeacf42bf30b1 100644
@@ -365,46 +365,61 @@ void prepare_vmcs(struct vmx_pages *vmx, void *guest_rip, void *guest_rsp)
 	init_vmcs_guest_state(guest_rip, guest_rsp);
 }
 
+static bool nested_ept_create_pte(struct kvm_vm *vm,
+				  uint64_t *pte,
+				  uint64_t paddr,
+				  uint64_t *address,
+				  bool *leaf)
+{
+	struct eptPageTableEntry *epte = (struct eptPageTableEntry *)pte;
+
+	/* PTE already exists? */
+	if (epte->readable) {
+		*leaf = epte->page_size;
+		*address = epte->address;
+		return false;
+	}
+
+	epte->writable = true;
+	epte->readable = true;
+	epte->executable = true;
+	epte->page_size = *leaf;
+
+	if (*leaf)
+		epte->address = paddr >> vm->page_shift;
+	else
+		epte->address = vm_alloc_page_table(vm) >> vm->page_shift;
+
+	*address = epte->address;
+
+	/*
+	 * For now mark these as accessed and dirty because the only
+	 * testcase we have needs that.  Can be reconsidered later.
+	 */
+	epte->accessed = *leaf;
+	epte->dirty = *leaf;
+	return true;
+}
+
 static uint64_t nested_create_pte(struct kvm_vm *vm,
 				  uint64_t *pte,
 				  uint64_t nested_paddr,
 				  uint64_t paddr,
 				  int level,
-				  bool leaf)
+				  bool want_leaf)
 {
-	struct eptPageTableEntry *epte = (struct eptPageTableEntry *)pte;
-
-	if (!epte->readable) {
-		epte->writable = true;
-		epte->readable = true;
-		epte->executable = true;
-		epte->page_size = leaf;
+	bool leaf = want_leaf;
+	uint64_t address;
 
-		if (leaf)
-			epte->address = paddr >> vm->page_shift;
-		else
-			epte->address = vm_alloc_page_table(vm) >> vm->page_shift;
-
-		/*
-		 * For now mark these as accessed and dirty because the only
-		 * testcase we have needs that.  Can be reconsidered later.
-		 */
-		epte->accessed = leaf;
-		epte->dirty = leaf;
-	} else {
-		/*
-		 * Entry already present.  Assert that the caller doesn't want a
-		 * leaf entry at this level, and that there isn't a leaf entry
-		 * at this level.
-		 */
-		TEST_ASSERT(!leaf,
+	if (!nested_ept_create_pte(vm, pte, paddr, &address, &leaf)) {
+		TEST_ASSERT(!want_leaf,
 			    "Cannot create leaf entry at level: %u, nested_paddr: 0x%lx",
 			    level, nested_paddr);
-		TEST_ASSERT(!epte->page_size,
+		TEST_ASSERT(!leaf,
 			    "Leaf entry already exists at level: %u, nested_paddr: 0x%lx",
 			    level, nested_paddr);
 	}
-	return epte->address;
+	return address;
 }
 
 
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Reviewed-by on v1 10/12
#28 lore raw

Re: [PATCH 10/12] KVM: selftests: Move EPT-specific init outside nested_create_pte()

Quoted 12 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

Refactor the EPT specific initialization into nested_ept_create_pte(), in preparation for making nested_create_pte() NPT-friendly.

---
 tools/testing/selftests/kvm/lib/x86/vmx.c | 71 ++++++++++++++---------
 1 file changed, 43 insertions(+), 28 deletions(-)

...

tools/testing/selftests/kvm/lib/x86/vmx.c void prepare_vmcs(struct vmx_pages *vmx, void *guest_rip, void *guest_rsp) · line 400

Quoted 7 lines
tools/testing/selftests/kvm/lib/x86/vmx.c +7 -0
+
+       /*
+        * For now mark these as accessed and dirty because the only
+        * testcase we have needs that.  Can be reconsidered later.
+        */
+       epte->accessed = *leaf;
+       epte->dirty = *leaf;

Not your change, but it seems strange to set the 'accessed' bit only at the leaf. The CPU will set the 'accessed' bits from the PGD down as it does the walk. So, to only have an accessed bit set on the leaf requires the existence of some software agent to clear the higher levels.

#29 lore raw

PATCH 11/12 KVM: selftests: Refactor generic nested mapping outside VMX code

In reply to Yosry Ahmed

From: Yosry Ahmed <[email protected]>

Now that the nested mapping functions in vmx.c are all generic except for nested_ept_create_pte(), move them all out into a new nested_map.c lib file and expose nested_ept_create_pte() in vmx.h. This allows reusing the code for NPT in following changes.

While we're at it, merge nested_pg_map() and __nested_pg_map(), as the former is unused, and make sure all functions not exposed in the header are static.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/include/x86/nested_map.h    |  20 +++
 tools/testing/selftests/kvm/include/x86/vmx.h |  13 +-
 .../testing/selftests/kvm/lib/x86/memstress.c |   1 +
 .../selftests/kvm/lib/x86/nested_map.c        | 150 +++++++++++++++++
 tools/testing/selftests/kvm/lib/x86/vmx.c     | 155 +-----------------
 .../selftests/kvm/x86/vmx_dirty_log_test.c    |   1 +
 7 files changed, 183 insertions(+), 158 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/include/x86/nested_map.h
 create mode 100644 tools/testing/selftests/kvm/lib/x86/nested_map.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 9b3c99acd51a3..9547d7263e236 100644
@@ -23,6 +23,7 @@ LIBKVM_x86 += lib/x86/apic.c
 LIBKVM_x86 += lib/x86/handlers.S
 LIBKVM_x86 += lib/x86/hyperv.c
 LIBKVM_x86 += lib/x86/memstress.c
+LIBKVM_x86 += lib/x86/nested_map.c
 LIBKVM_x86 += lib/x86/pmu.c
 LIBKVM_x86 += lib/x86/processor.c
 LIBKVM_x86 += lib/x86/sev.c
tools/testing/selftests/kvm/include/x86/nested_map.h new file +20 -0
new file mode 100644
index 0000000000000..362162dd6db43
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * tools/testing/selftests/kvm/include/x86_64/nested_map.h
+ *
+ * Copyright (C) 2025, Google LLC.
+ */
+
+#ifndef SELFTEST_KVM_NESTED_MAP_H
+#define SELFTEST_KVM_NESTED_MAP_H
+
+#include "kvm_util.h"
+
+void nested_map(void *root_hva, struct kvm_vm *vm,
+		uint64_t nested_paddr, uint64_t paddr, uint64_t size);
+void nested_map_memslot(void *root_hva, struct kvm_vm *vm,
+			uint32_t memslot);
+void nested_identity_map_1g(void *root_hva, struct kvm_vm *vm,
+			    uint64_t addr, uint64_t size);
+
+#endif /* SELFTEST_KVM_NESTED_MAP_H */
tools/testing/selftests/kvm/include/x86/vmx.h +5 -8
index 06ae68cf9635c..49d763144dbfe 100644
@@ -559,14 +559,11 @@ bool load_vmcs(struct vmx_pages *vmx);
 
 bool ept_1g_pages_supported(void);
 
-void nested_pg_map(void *root_hva, struct kvm_vm *vm,
-		   uint64_t nested_paddr, uint64_t paddr);
-void nested_map(void *root_hva, struct kvm_vm *vm,
-		 uint64_t nested_paddr, uint64_t paddr, uint64_t size);
-void nested_map_memslot(void *root_hva, struct kvm_vm *vm,
-			uint32_t memslot);
-void nested_identity_map_1g(void *root_hva, struct kvm_vm *vm,
-			    uint64_t addr, uint64_t size);
+bool nested_ept_create_pte(struct kvm_vm *vm,
+			   uint64_t *pte,
+			   uint64_t paddr,
+			   uint64_t *address,
+			   bool *leaf);
 bool kvm_cpu_has_ept(void);
 void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm,
 		  uint32_t eptp_memslot);
tools/testing/selftests/kvm/lib/x86/memstress.c +1 -0
index 7981e295cac70..d3e2fbd550acd 100644
@@ -12,6 +12,7 @@
 #include "test_util.h"
 #include "kvm_util.h"
 #include "memstress.h"
+#include "nested_map.h"
 #include "processor.h"
 #include "vmx.h"
 
tools/testing/selftests/kvm/lib/x86/nested_map.c new file +150 -0

150 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86/vmx.c +5 -150

167 lines not on this page · raw message

tools/testing/selftests/kvm/x86/vmx_dirty_log_test.c +1 -0
index 21a57805e9780..db88a1e5e9d0c 100644
@@ -11,6 +11,7 @@
 
 #include "test_util.h"
 #include "kvm_util.h"
+#include "nested_map.h"
 #include "processor.h"
 #include "vmx.h"
 
Signature · 1 line
2.51.0.618.g983fd99d29-goog
Reviewed-by on v1 11/12
#30 lore raw

Re: [PATCH 11/12] KVM: selftests: Refactor generic nested mapping outside VMX code

Quoted 8 lines · Yosry Ahmed

From: Yosry Ahmed <[email protected]>

Now that the nested mapping functions in vmx.c are all generic except for nested_ept_create_pte(), move them all out into a new nested_map.c lib file and expose nested_ept_create_pte() in vmx.h. This allows reusing the code for NPT in following changes.

Quoted 5 lines

While we're at it, merge nested_pg_map() and __nested_pg_map(), as the former is unused, and make sure all functions not exposed in the header are static.

#31 lore raw

PATCH 12/12 KVM: selftests: Extend vmx_dirty_log_test to cover SVM

In reply to Yosry Ahmed

From: Yosry Ahmed <[email protected]>

Add the necessary infrastructure to support setting up nested NPTs and creating nested NPT mappings. There is some redundancy between nested_npt_create_pte() and nested_ept_create_pte(), especially that we access the same fields in both. An alternative is to have a single function in nested_map.c, and use macros to cast an obaque PTE pointer to the correct type (EPT entry vs NPT entry).

Add a check in kvm_cpu_has_ept() to return false on AMD CPUs without attempting to read VMX-specific MSRs, since now it can be called on AMD CPUs. 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      | 13 +++
 tools/testing/selftests/kvm/lib/x86/svm.c     | 70 ++++++++++++++
 tools/testing/selftests/kvm/lib/x86/vmx.c     |  3 +
 ...rty_log_test.c => nested_dirty_log_test.c} | 94 ++++++++++++++-----
 5 files changed, 155 insertions(+), 27 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 9547d7263e236..acedbf726f493 100644
@@ -113,7 +113,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 +13 -0
index b74c6dcddcbd6..84b79113b5433 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,14 @@ 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 nested_npt_create_pte(struct kvm_vm *vm,
+			   uint64_t *pte,
+			   uint64_t paddr,
+			   uint64_t *address,
+			   bool *leaf);
+bool kvm_cpu_has_npt(void);
+void prepare_npt(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 +70 -0

88 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86/vmx.c +3 -0
index 24345213fcd04..0ced959184cd9 100644
@@ -405,6 +405,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;
tools/testing/selftests/kvm/x86/nested_dirty_log_test.c renamed from tools/testing/selftests/kvm/x86/vmx_dirty_log_test.c +68 -26

160 lines not on this page · raw message

Signature · 1 line
2.51.0.618.g983fd99d29-goog
#32 lore raw
In reply to Yosry Ahmed
Quoted 17 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.

Yosry Ahmed (12):
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: Remove invalid CR3 test from vmx_tsc_adjust_test
KVM: selftests: Extend vmx_tsc_adjust_test to cover SVM
KVM: selftests: Pass the root HVA directly to nested mapping functions
KVM: selftests: Use 'leaf' instead of hugepage to describe EPT entries
KVM: selftests: Move all PTE accesses into nested_create_pte()
KVM: selftests: Move EPT-specific init outside nested_create_pte()
KVM: selftests: Refactor generic nested mapping outside VMX code
KVM: selftests: Extend vmx_dirty_log_test to cover SVM

Ugh, wrong From email on all the patches due to some unorthodox cherry-picking :)

Quoted 26 lines
 tools/testing/selftests/kvm/Makefile.kvm      |  11 +-
 .../selftests/kvm/include/x86/nested_map.h    |  20 ++
 .../selftests/kvm/include/x86/svm_util.h      |  13 ++
 tools/testing/selftests/kvm/include/x86/vmx.h |  13 +-
 .../testing/selftests/kvm/lib/x86/memstress.c |   5 +-
 .../selftests/kvm/lib/x86/nested_map.c        | 150 +++++++++++++++
 tools/testing/selftests/kvm/lib/x86/svm.c     |  70 +++++++
 tools/testing/selftests/kvm/lib/x86/vmx.c     | 180 +++---------------
 ...ested_test.c => close_while_nested_test.c} |  42 +++-
 ...rty_log_test.c => nested_dirty_log_test.c} |  95 ++++++---
 ...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} | 132 +++++++++++--
 13 files changed, 609 insertions(+), 249 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/include/x86/nested_map.h
 create mode 100644 tools/testing/selftests/kvm/lib/x86/nested_map.c
 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} (62%)
 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.618.g983fd99d29-goog