Skip to contentSkip to conversation

KVM: selftests: Add simple SEV test

PATCH Superseded Peter Gonda 4 participants 30 messages Aug 10, 2022 – Aug 29, 2022

Subsystems (4)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover KVM: selftests: Add simple SEV test

This patch series combines the work Michael Roth has done in supporting SEV guests in selftests and the work Sean Christopherson suggested to allow ucalls from SEV guests. And the work Sean has sent to consolidate the ucall boilerplate code. Along with a very simple version of the SEV selftests Michael originally proposed.

V3
* Addressed more of andrew.jones@ in ucall patches.
* Fix build in non-x86 archs.

V2
* Dropped RFC tag
* Correctly separated Sean's ucall patches into 2 as originally intended.
* Addressed andrew.jones@ in ucall patches.
* Fixed ucall pool usage to work for other archs

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

Michael Roth (6):
KVM: selftests: move vm_phy_pages_alloc() earlier in file
KVM: selftests: sparsebit: add const where appropriate
KVM: selftests: add hooks for managing encrypted guest memory
KVM: selftests: handle encryption bits in page tables
KVM: selftests: add support for encrypted vm_vaddr_* allocations
KVM: selftests: add library for creating/interacting with SEV guests

Peter Gonda (3):
tools: Add atomic_test_and_set_bit()
KVM: selftests: Add ucall pool based implementation
KVM: selftests: Add simple sev vm testing

Sean Christopherson (2):
KVM: selftests: Consolidate common code for popuplating
KVM: selftests: Consolidate boilerplate code in get_ucall()

 tools/arch/x86/include/asm/atomic.h           |   7 +
 tools/include/asm-generic/atomic-gcc.h        |  15 +
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   3 +
 .../selftests/kvm/include/kvm_util_base.h     |  25 ++
 .../testing/selftests/kvm/include/sparsebit.h |  36 +--
 .../selftests/kvm/include/ucall_common.h      |  14 +-
 .../selftests/kvm/include/x86_64/sev.h        |  47 +++
 .../testing/selftests/kvm/lib/aarch64/ucall.c |  38 +--
 tools/testing/selftests/kvm/lib/kvm_util.c    | 267 +++++++++++++-----
 tools/testing/selftests/kvm/lib/riscv/ucall.c |  43 +--
 tools/testing/selftests/kvm/lib/s390x/ucall.c |  40 +--
 tools/testing/selftests/kvm/lib/sparsebit.c   |  48 ++--
 .../testing/selftests/kvm/lib/ucall_common.c  | 139 +++++++++
 .../selftests/kvm/lib/x86_64/processor.c      |  15 +-
 tools/testing/selftests/kvm/lib/x86_64/sev.c  | 249 ++++++++++++++++
 .../testing/selftests/kvm/lib/x86_64/ucall.c  |  39 +--
 .../selftests/kvm/x86_64/sev_all_boot_test.c  | 131 +++++++++
 18 files changed, 911 insertions(+), 246 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/include/x86_64/sev.h
 create mode 100644 tools/testing/selftests/kvm/lib/ucall_common.c
 create mode 100644 tools/testing/selftests/kvm/lib/x86_64/sev.c
 create mode 100644 tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c
Signature · 1 line
2.37.1.559.g78731f0fdb-goog
#1 lore raw

PATCH v3 1/11 KVM: selftests: move vm_phy_pages_alloc() earlier in file

From: Michael Roth <[email protected]>

Subsequent patches will break some of this code out into file-local helper functions, which will be used by functions like vm_vaddr_alloc(), which currently are defined earlier in the file, so a forward declaration would be needed.

Instead, move it earlier in the file, just above vm_vaddr_alloc() and and friends, which are the main users.

---
 tools/testing/selftests/kvm/lib/kvm_util.c | 145 ++++++++++-----------
 1 file changed, 72 insertions(+), 73 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +72 -73
index 9889fe0d8919..cb3a5f8a53b7 100644
@@ -1089,6 +1089,78 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
 	return vcpu;
 }
 
+/*
+ * Physical Contiguous Page Allocator
+ *
+ * Input Args:
+ *   vm - Virtual Machine
+ *   num - number of pages
+ *   paddr_min - Physical address minimum
+ *   memslot - Memory region to allocate page from
+ *
+ * Output Args: None
+ *
+ * Return:
+ *   Starting physical address
+ *
+ * Within the VM specified by vm, locates a range of available physical
+ * pages at or above paddr_min. If found, the pages are marked as in use
+ * and their base address is returned. A TEST_ASSERT failure occurs if
+ * not enough pages are available at or above paddr_min.
+ */
+vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
+			      vm_paddr_t paddr_min, uint32_t memslot)
+{
+	struct userspace_mem_region *region;
+	sparsebit_idx_t pg, base;
+
+	TEST_ASSERT(num > 0, "Must allocate at least one page");
+
+	TEST_ASSERT((paddr_min % vm->page_size) == 0,
+		"Min physical address not divisible by page size.\n paddr_min: 0x%lx page_size: 0x%x",
+		paddr_min, vm->page_size);
+
+	region = memslot2region(vm, memslot);
+	base = pg = paddr_min >> vm->page_shift;
+
+	do {
+		for (; pg < base + num; ++pg) {
+			if (!sparsebit_is_set(region->unused_phy_pages, pg)) {
+				base = pg = sparsebit_next_set(region->unused_phy_pages, pg);
+				break;
+			}
+		}
+	} while (pg && pg != base + num);
+
+	if (pg == 0) {
+		fprintf(stderr,
+			"No guest physical page available, paddr_min: 0x%lx page_size: 0x%x memslot: %u\n",
+			paddr_min, vm->page_size, memslot);
+		fputs("---- vm dump ----\n", stderr);
+		vm_dump(stderr, vm, 2);
+		abort();
+	}
+
+	for (pg = base; pg < base + num; ++pg)
+		sparsebit_clear(region->unused_phy_pages, pg);
+
+	return base * vm->page_size;
+}
+
+vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
+			     uint32_t memslot)
+{
+	return vm_phy_pages_alloc(vm, 1, paddr_min, memslot);
+}
+
+/* Arbitrary minimum physical address used for virtual translation tables. */
+#define KVM_GUEST_PAGE_TABLE_MIN_PADDR 0x180000
+
+vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm)
+{
+	return vm_phy_page_alloc(vm, KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0);
+}
+
 /*
  * VM Virtual Address Unused Gap
  *
@@ -1735,79 +1807,6 @@ const char *exit_reason_str(unsigned int exit_reason)
 	return "Unknown";
 }
 
-/*
- * Physical Contiguous Page Allocator
- *
- * Input Args:
- *   vm - Virtual Machine
- *   num - number of pages
- *   paddr_min - Physical address minimum
- *   memslot - Memory region to allocate page from
- *
- * Output Args: None
- *
- * Return:
- *   Starting physical address
- *
- * Within the VM specified by vm, locates a range of available physical
- * pages at or above paddr_min. If found, the pages are marked as in use
- * and their base address is returned. A TEST_ASSERT failure occurs if
- * not enough pages are available at or above paddr_min.
- */
-vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
-			      vm_paddr_t paddr_min, uint32_t memslot)
-{
-	struct userspace_mem_region *region;
-	sparsebit_idx_t pg, base;
-
-	TEST_ASSERT(num > 0, "Must allocate at least one page");
-
-	TEST_ASSERT((paddr_min % vm->page_size) == 0, "Min physical address "
-		"not divisible by page size.\n"
-		"  paddr_min: 0x%lx page_size: 0x%x",
-		paddr_min, vm->page_size);
-
-	region = memslot2region(vm, memslot);
-	base = pg = paddr_min >> vm->page_shift;
-
-	do {
-		for (; pg < base + num; ++pg) {
-			if (!sparsebit_is_set(region->unused_phy_pages, pg)) {
-				base = pg = sparsebit_next_set(region->unused_phy_pages, pg);
-				break;
-			}
-		}
-	} while (pg && pg != base + num);
-
-	if (pg == 0) {
-		fprintf(stderr, "No guest physical page available, "
-			"paddr_min: 0x%lx page_size: 0x%x memslot: %u\n",
-			paddr_min, vm->page_size, memslot);
-		fputs("---- vm dump ----\n", stderr);
-		vm_dump(stderr, vm, 2);
-		abort();
-	}
-
-	for (pg = base; pg < base + num; ++pg)
-		sparsebit_clear(region->unused_phy_pages, pg);
-
-	return base * vm->page_size;
-}
-
-vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
-			     uint32_t memslot)
-{
-	return vm_phy_pages_alloc(vm, 1, paddr_min, memslot);
-}
-
-/* Arbitrary minimum physical address used for virtual translation tables. */
-#define KVM_GUEST_PAGE_TABLE_MIN_PADDR 0x180000
-
-vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm)
-{
-	return vm_phy_page_alloc(vm, KVM_GUEST_PAGE_TABLE_MIN_PADDR, 0);
-}
-
 /*
  * Address Guest Virtual to Host Virtual
  *
Signature · 1 line
2.37.1.559.g78731f0fdb-goog
#2 lore raw

PATCH v3 2/11 KVM: selftests: sparsebit: add const where appropriate

In reply to Peter Gonda

From: Michael Roth <[email protected]>

Subsequent patches will introduce an encryption bitmap in kvm_util that would be useful to allow tests to access in read-only fashion. This will be done via a const sparsebit*. To avoid warnings or the need to add casts everywhere, add const to the various sparsebit functions that are applicable for read-only usage of sparsebit.

---
 .../testing/selftests/kvm/include/sparsebit.h | 36 +++++++-------
 tools/testing/selftests/kvm/lib/sparsebit.c   | 48 +++++++++----------
 2 files changed, 42 insertions(+), 42 deletions(-)
tools/testing/selftests/kvm/include/sparsebit.h +18 -18
index 12a9a4b9cead..fb5170d57fcb 100644
@@ -30,26 +30,26 @@ typedef uint64_t sparsebit_num_t;
 
 struct sparsebit *sparsebit_alloc(void);
 void sparsebit_free(struct sparsebit **sbitp);
-void sparsebit_copy(struct sparsebit *dstp, struct sparsebit *src);
+void sparsebit_copy(struct sparsebit *dstp, const struct sparsebit *src);
 
-bool sparsebit_is_set(struct sparsebit *sbit, sparsebit_idx_t idx);
-bool sparsebit_is_set_num(struct sparsebit *sbit,
+bool sparsebit_is_set(const struct sparsebit *sbit, sparsebit_idx_t idx);
+bool sparsebit_is_set_num(const struct sparsebit *sbit,
 			  sparsebit_idx_t idx, sparsebit_num_t num);
-bool sparsebit_is_clear(struct sparsebit *sbit, sparsebit_idx_t idx);
-bool sparsebit_is_clear_num(struct sparsebit *sbit,
+bool sparsebit_is_clear(const struct sparsebit *sbit, sparsebit_idx_t idx);
+bool sparsebit_is_clear_num(const struct sparsebit *sbit,
 			    sparsebit_idx_t idx, sparsebit_num_t num);
-sparsebit_num_t sparsebit_num_set(struct sparsebit *sbit);
-bool sparsebit_any_set(struct sparsebit *sbit);
-bool sparsebit_any_clear(struct sparsebit *sbit);
-bool sparsebit_all_set(struct sparsebit *sbit);
-bool sparsebit_all_clear(struct sparsebit *sbit);
-sparsebit_idx_t sparsebit_first_set(struct sparsebit *sbit);
-sparsebit_idx_t sparsebit_first_clear(struct sparsebit *sbit);
-sparsebit_idx_t sparsebit_next_set(struct sparsebit *sbit, sparsebit_idx_t prev);
-sparsebit_idx_t sparsebit_next_clear(struct sparsebit *sbit, sparsebit_idx_t prev);
-sparsebit_idx_t sparsebit_next_set_num(struct sparsebit *sbit,
+sparsebit_num_t sparsebit_num_set(const struct sparsebit *sbit);
+bool sparsebit_any_set(const struct sparsebit *sbit);
+bool sparsebit_any_clear(const struct sparsebit *sbit);
+bool sparsebit_all_set(const struct sparsebit *sbit);
+bool sparsebit_all_clear(const struct sparsebit *sbit);
+sparsebit_idx_t sparsebit_first_set(const struct sparsebit *sbit);
+sparsebit_idx_t sparsebit_first_clear(const struct sparsebit *sbit);
+sparsebit_idx_t sparsebit_next_set(const struct sparsebit *sbit, sparsebit_idx_t prev);
+sparsebit_idx_t sparsebit_next_clear(const struct sparsebit *sbit, sparsebit_idx_t prev);
+sparsebit_idx_t sparsebit_next_set_num(const struct sparsebit *sbit,
 				       sparsebit_idx_t start, sparsebit_num_t num);
-sparsebit_idx_t sparsebit_next_clear_num(struct sparsebit *sbit,
+sparsebit_idx_t sparsebit_next_clear_num(const struct sparsebit *sbit,
 					 sparsebit_idx_t start, sparsebit_num_t num);
 
 void sparsebit_set(struct sparsebit *sbitp, sparsebit_idx_t idx);
@@ -62,9 +62,9 @@ void sparsebit_clear_num(struct sparsebit *sbitp,
 			 sparsebit_idx_t start, sparsebit_num_t num);
 void sparsebit_clear_all(struct sparsebit *sbitp);
 
-void sparsebit_dump(FILE *stream, struct sparsebit *sbit,
+void sparsebit_dump(FILE *stream, const struct sparsebit *sbit,
 		    unsigned int indent);
-void sparsebit_validate_internal(struct sparsebit *sbit);
+void sparsebit_validate_internal(const struct sparsebit *sbit);
 
 #ifdef __cplusplus
 }
tools/testing/selftests/kvm/lib/sparsebit.c +24 -24

190 lines not on this page · raw message

Signature · 1 line
2.37.1.559.g78731f0fdb-goog
#3 lore raw

PATCH v3 3/11 KVM: selftests: add hooks for managing encrypted guest memory

In reply to Peter Gonda

From: Michael Roth <[email protected]>

VM implementations that make use of encrypted memory need a way to configure things like the encryption/shared bit position for page table handling, the default encryption policy for internal allocations made by the core library, and a way to fetch the list/bitmap of encrypted pages to do the actual memory encryption. Add an interface to configure these parameters. Also introduce a sparsebit map to track allocations/mappings that should be treated as encrypted, and provide a way for VM implementations to retrieve it to handle operations related memory encryption.

---
 .../selftests/kvm/include/kvm_util_base.h     | 17 ++++++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 52 +++++++++++++++++--
 2 files changed, 66 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +17 -0
index 24fde97f6121..3928351e497e 100644
@@ -32,6 +32,7 @@ typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */
 struct userspace_mem_region {
 	struct kvm_userspace_memory_region region;
 	struct sparsebit *unused_phy_pages;
+	struct sparsebit *encrypted_phy_pages;
 	int fd;
 	off_t offset;
 	void *host_mem;
@@ -64,6 +65,14 @@ struct userspace_mem_regions {
 	DECLARE_HASHTABLE(slot_hash, 9);
 };
 
+/* Memory encryption policy/configuration. */
+struct vm_memcrypt {
+	bool enabled;
+	int8_t enc_by_default;
+	bool has_enc_bit;
+	int8_t enc_bit;
+};
+
 struct kvm_vm {
 	int mode;
 	unsigned long type;
@@ -87,6 +96,7 @@ struct kvm_vm {
 	vm_vaddr_t idt;
 	vm_vaddr_t handlers;
 	uint32_t dirty_ring_size;
+	struct vm_memcrypt memcrypt;
 
 	/* Cache of information for binary stats interface */
 	int stats_fd;
@@ -834,4 +844,11 @@ static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm)
 	return __vm_enable_cap(vm, KVM_CAP_VM_DISABLE_NX_HUGE_PAGES, 0);
 }
 
+void vm_set_memory_encryption(struct kvm_vm *vm, bool enc_by_default, bool has_enc_bit,
+			      uint8_t enc_bit);
+
+const struct sparsebit *vm_get_encrypted_phy_pages(struct kvm_vm *vm, int slot,
+						   vm_paddr_t *gpa_start,
+						   uint64_t *size);
+
 #endif /* SELFTEST_KVM_UTIL_BASE_H */
tools/testing/selftests/kvm/lib/kvm_util.c +49 -3

96 lines not on this page · raw message

Signature · 1 line
2.37.1.559.g78731f0fdb-goog
#4 lore raw

PATCH v3 4/11 KVM: selftests: handle encryption bits in page tables

In reply to Peter Gonda

From: Michael Roth <[email protected]>

SEV guests rely on an encyption bit which resides within the range that current code treats as address bits. Guest code will expect these bits to be set appropriately in their page tables, whereas the rest of the kvm_util functions will generally expect these bits to not be present. Introduce addr_gpa2raw()/addr_raw2gpa() to add/remove these bits, then use them where appropriate.

---
 .../selftests/kvm/include/kvm_util_base.h     |  2 +
 tools/testing/selftests/kvm/lib/kvm_util.c    | 55 ++++++++++++++++++-
 .../selftests/kvm/lib/x86_64/processor.c      | 15 +++--
 3 files changed, 66 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +2 -0
index 3928351e497e..de769b3de274 100644
@@ -399,6 +399,8 @@ void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa);
 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva);
 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva);
 void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa);
+vm_paddr_t addr_raw2gpa(struct kvm_vm *vm, vm_vaddr_t gpa_raw);
+vm_paddr_t addr_gpa2raw(struct kvm_vm *vm, vm_vaddr_t gpa);
 
 void vcpu_run(struct kvm_vcpu *vcpu);
 int _vcpu_run(struct kvm_vcpu *vcpu);
tools/testing/selftests/kvm/lib/kvm_util.c +54 -1

69 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86_64/processor.c +10 -5
index f35626df1dea..55855594d26d 100644
@@ -118,7 +118,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)
 
 	/* If needed, create page map l4 table. */
 	if (!vm->pgd_created) {
-		vm->pgd = vm_alloc_page_table(vm);
+		vm->pgd = addr_gpa2raw(vm, vm_alloc_page_table(vm));
 		vm->pgd_created = true;
 	}
 }
@@ -140,13 +140,15 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm,
 				       int target_level)
 {
 	uint64_t *pte = virt_get_pte(vm, pt_pfn, vaddr, current_level);
+	uint64_t paddr_raw = addr_gpa2raw(vm, paddr);
 
 	if (!(*pte & PTE_PRESENT_MASK)) {
 		*pte = PTE_PRESENT_MASK | PTE_WRITABLE_MASK;
 		if (current_level == target_level)
-			*pte |= PTE_LARGE_MASK | (paddr & PHYSICAL_PAGE_MASK);
+			*pte |= PTE_LARGE_MASK | (paddr_raw & PHYSICAL_PAGE_MASK);
 		else
-			*pte |= vm_alloc_page_table(vm) & PHYSICAL_PAGE_MASK;
+			*pte |= addr_gpa2raw(vm, vm_alloc_page_table(vm)) & PHYSICAL_PAGE_MASK;
+
 	} else {
 		/*
 		 * Entry already present.  Assert that the caller doesn't want
@@ -184,6 +186,8 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)
 		    "Physical address beyond maximum supported,\n"
 		    "  paddr: 0x%lx vm->max_gfn: 0x%lx vm->page_size: 0x%x",
 		    paddr, vm->max_gfn, vm->page_size);
+	TEST_ASSERT(addr_raw2gpa(vm, paddr) == paddr,
+		    "Unexpected bits in paddr: %lx", paddr);
 
 	/*
 	 * Allocate upper level page tables, if not already present.  Return
@@ -206,7 +210,8 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)
 	pte = virt_get_pte(vm, PTE_GET_PFN(*pde), vaddr, PG_LEVEL_4K);
 	TEST_ASSERT(!(*pte & PTE_PRESENT_MASK),
 		    "PTE already present for 4k page at vaddr: 0x%lx\n", vaddr);
-	*pte = PTE_PRESENT_MASK | PTE_WRITABLE_MASK | (paddr & PHYSICAL_PAGE_MASK);
+	*pte = PTE_PRESENT_MASK | PTE_WRITABLE_MASK |
+	       (addr_gpa2raw(vm, paddr) & PHYSICAL_PAGE_MASK);
 }
 
 void virt_arch_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
@@ -515,7 +520,7 @@ vm_paddr_t addr_arch_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva)
 	if (!(pte[index[0]] & PTE_PRESENT_MASK))
 		goto unmapped_gva;
 
-	return (PTE_GET_PFN(pte[index[0]]) * vm->page_size) + (gva & ~PAGE_MASK);
+	return addr_raw2gpa(vm, PTE_GET_PFN(pte[index[0]]) * vm->page_size) + (gva & ~PAGE_MASK);
 
 unmapped_gva:
 	TEST_FAIL("No mapping for vm virtual address, gva: 0x%lx", gva);
Signature · 1 line
2.37.1.559.g78731f0fdb-goog
#5 lore raw

PATCH v3 5/11 KVM: selftests: add support for encrypted vm_vaddr_* allocations

In reply to Peter Gonda

From: Michael Roth <[email protected]>

The default policy for whether to handle allocations as encrypted or shared pages is currently determined by vm_phy_pages_alloc(), which in turn uses the policy defined by vm->memcrypt.enc_by_default.

Test programs may wish to allocate shared vaddrs for things like sharing memory with the guest. Since enc_by_default will be true in the case of SEV guests (since it's required in order to have the initial ELF binary and page table become part of the initial guest payload), an interface is needed to explicitly request shared pages.

Implement this by splitting the common code out from vm_vaddr_alloc() and introducing a new vm_vaddr_alloc_shared().

---
 .../selftests/kvm/include/kvm_util_base.h     |  1 +
 tools/testing/selftests/kvm/lib/kvm_util.c    | 21 +++++++++++++++----
 2 files changed, 18 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +1 -0
index de769b3de274..8ce9e5be70a3 100644
@@ -390,6 +390,7 @@ void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa);
 void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot);
 struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id);
 vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min);
+vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min);
 vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages);
 vm_vaddr_t vm_vaddr_alloc_page(struct kvm_vm *vm);
 
tools/testing/selftests/kvm/lib/kvm_util.c +17 -4
index 87772e23d1b5..4e4b28e4e890 100644
@@ -1262,12 +1262,13 @@ static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,
 }
 
 /*
- * VM Virtual Address Allocate
+ * VM Virtual Address Allocate Shared/Encrypted
  *
  * Input Args:
  *   vm - Virtual Machine
  *   sz - Size in bytes
  *   vaddr_min - Minimum starting virtual address
+ *   encrypt - Whether the region should be handled as encrypted
  *
  * Output Args: None
  *
@@ -1280,13 +1281,15 @@ static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,
  * a unique set of pages, with the minimum real allocation being at least
  * a page.
  */
-vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min)
+static vm_vaddr_t
+_vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, bool encrypt)
 {
 	uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0);
 
 	virt_pgd_alloc(vm);
-	vm_paddr_t paddr = vm_phy_pages_alloc(vm, pages,
-					      KVM_UTIL_MIN_PFN * vm->page_size, 0);
+	vm_paddr_t paddr = _vm_phy_pages_alloc(vm, pages,
+					       KVM_UTIL_MIN_PFN * vm->page_size,
+					       0, encrypt);
 
 	/*
 	 * Find an unused range of virtual page addresses of at least
@@ -1307,6 +1310,16 @@ vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min)
 	return vaddr_start;
 }
 
+vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min)
+{
+	return _vm_vaddr_alloc(vm, sz, vaddr_min, vm->memcrypt.enc_by_default);
+}
+
+vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min)
+{
+	return _vm_vaddr_alloc(vm, sz, vaddr_min, false);
+}
+
 /*
  * VM Virtual Address Allocate Pages
  *
Signature · 1 line
2.37.1.559.g78731f0fdb-goog
#6 lore raw

PATCH v3 6/11 KVM: selftests: Consolidate common code for popuplating

In reply to Peter Gonda

From: Sean Christopherson <[email protected]>

Make ucall() a common helper that populates struct ucall, and only calls into arch code to make the actually call out to userspace.

Rename all arch-specific helpers to make it clear they're arch-specific, and to avoid collisions with common helpers (one more on its way...)

Add WRITE_ONCE() to stores in ucall() code to as already done to aarch64 code in commit 9e2f6498efbb ("selftests: KVM: Handle compiler optimizations in ucall") to prevent clang optimizations breaking ucalls.

---
 tools/testing/selftests/kvm/Makefile          |  1 +
 .../selftests/kvm/include/ucall_common.h      | 23 ++++++++++++++++---
 .../testing/selftests/kvm/lib/aarch64/ucall.c | 20 ++++------------
 tools/testing/selftests/kvm/lib/riscv/ucall.c | 23 ++++---------------
 tools/testing/selftests/kvm/lib/s390x/ucall.c | 23 ++++---------------
 .../testing/selftests/kvm/lib/ucall_common.c  | 20 ++++++++++++++++
 .../testing/selftests/kvm/lib/x86_64/ucall.c  | 23 ++++---------------
 7 files changed, 60 insertions(+), 73 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/lib/ucall_common.c
tools/testing/selftests/kvm/Makefile +1 -0
index 690b499c3471..39fc5e8e5594 100644
@@ -46,6 +46,7 @@ LIBKVM += lib/perf_test_util.c
 LIBKVM += lib/rbtree.c
 LIBKVM += lib/sparsebit.c
 LIBKVM += lib/test_util.c
+LIBKVM += lib/ucall_common.c
 
 LIBKVM_x86_64 += lib/x86_64/apic.c
 LIBKVM_x86_64 += lib/x86_64/handlers.S
tools/testing/selftests/kvm/include/ucall_common.h +20 -3
index ee79d180e07e..5a85f5318bbe 100644
@@ -24,10 +24,27 @@ struct ucall {
 	uint64_t args[UCALL_MAX_ARGS];
 };
 
-void ucall_init(struct kvm_vm *vm, void *arg);
-void ucall_uninit(struct kvm_vm *vm);
+void ucall_arch_init(struct kvm_vm *vm, void *arg);
+void ucall_arch_uninit(struct kvm_vm *vm);
+void ucall_arch_do_ucall(vm_vaddr_t uc);
+uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
+
 void ucall(uint64_t cmd, int nargs, ...);
-uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
+
+static inline void ucall_init(struct kvm_vm *vm, void *arg)
+{
+	ucall_arch_init(vm, arg);
+}
+
+static inline void ucall_uninit(struct kvm_vm *vm)
+{
+	ucall_arch_uninit(vm);
+}
+
+static inline uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+{
+	return ucall_arch_get_ucall(vcpu, uc);
+}
 
 #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4)	\
 				ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
tools/testing/selftests/kvm/lib/aarch64/ucall.c +4 -16
index ed237b744690..1c81a6a5c1f2 100644
@@ -21,7 +21,7 @@ static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa)
 	return true;
 }
 
-void ucall_init(struct kvm_vm *vm, void *arg)
+void ucall_arch_init(struct kvm_vm *vm, void *arg)
 {
 	vm_paddr_t gpa, start, end, step, offset;
 	unsigned int bits;
@@ -64,30 +64,18 @@ void ucall_init(struct kvm_vm *vm, void *arg)
 	TEST_FAIL("Can't find a ucall mmio address");
 }
 
-void ucall_uninit(struct kvm_vm *vm)
+void ucall_arch_uninit(struct kvm_vm *vm)
 {
 	ucall_exit_mmio_addr = 0;
 	sync_global_to_guest(vm, ucall_exit_mmio_addr);
 }
 
-void ucall(uint64_t cmd, int nargs, ...)
+void ucall_arch_do_ucall(vm_vaddr_t uc)
 {
-	struct ucall uc = {};
-	va_list va;
-	int i;
-
-	WRITE_ONCE(uc.cmd, cmd);
-	nargs = min(nargs, UCALL_MAX_ARGS);
-
-	va_start(va, nargs);
-	for (i = 0; i < nargs; ++i)
-		WRITE_ONCE(uc.args[i], va_arg(va, uint64_t));
-	va_end(va);
-
 	WRITE_ONCE(*ucall_exit_mmio_addr, (vm_vaddr_t)&uc);
 }
 
-uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 {
 	struct kvm_run *run = vcpu->run;
 	struct ucall ucall = {};
tools/testing/selftests/kvm/lib/riscv/ucall.c +5 -18
index 087b9740bc8f..b1598f418c1f 100644
@@ -10,11 +10,11 @@
 #include "kvm_util.h"
 #include "processor.h"
 
-void ucall_init(struct kvm_vm *vm, void *arg)
+void ucall_arch_init(struct kvm_vm *vm, void *arg)
 {
 }
 
-void ucall_uninit(struct kvm_vm *vm)
+void ucall_arch_uninit(struct kvm_vm *vm)
 {
 }
 
@@ -44,27 +44,14 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
 	return ret;
 }
 
-void ucall(uint64_t cmd, int nargs, ...)
+void ucall_arch_do_ucall(vm_vaddr_t uc)
 {
-	struct ucall uc = {
-		.cmd = cmd,
-	};
-	va_list va;
-	int i;
-
-	nargs = min(nargs, UCALL_MAX_ARGS);
-
-	va_start(va, nargs);
-	for (i = 0; i < nargs; ++i)
-		uc.args[i] = va_arg(va, uint64_t);
-	va_end(va);
-
 	sbi_ecall(KVM_RISCV_SELFTESTS_SBI_EXT,
 		  KVM_RISCV_SELFTESTS_SBI_UCALL,
-		  (vm_vaddr_t)&uc, 0, 0, 0, 0, 0);
+		  uc, 0, 0, 0, 0, 0);
 }
 
-uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 {
 	struct kvm_run *run = vcpu->run;
 	struct ucall ucall = {};
tools/testing/selftests/kvm/lib/s390x/ucall.c +5 -18
index 73dc4e21190f..114cb4af295f 100644
@@ -6,34 +6,21 @@
  */
 #include "kvm_util.h"
 
-void ucall_init(struct kvm_vm *vm, void *arg)
+void ucall_arch_init(struct kvm_vm *vm, void *arg)
 {
 }
 
-void ucall_uninit(struct kvm_vm *vm)
+void ucall_arch_uninit(struct kvm_vm *vm)
 {
 }
 
-void ucall(uint64_t cmd, int nargs, ...)
+void ucall_arch_do_ucall(vm_vaddr_t uc)
 {
-	struct ucall uc = {
-		.cmd = cmd,
-	};
-	va_list va;
-	int i;
-
-	nargs = min(nargs, UCALL_MAX_ARGS);
-
-	va_start(va, nargs);
-	for (i = 0; i < nargs; ++i)
-		uc.args[i] = va_arg(va, uint64_t);
-	va_end(va);
-
 	/* Exit via DIAGNOSE 0x501 (normally used for breakpoints) */
-	asm volatile ("diag 0,%0,0x501" : : "a"(&uc) : "memory");
+	asm volatile ("diag 0,%0,0x501" : : "a"(uc) : "memory");
 }
 
-uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 {
 	struct kvm_run *run = vcpu->run;
 	struct ucall ucall = {};
tools/testing/selftests/kvm/lib/ucall_common.c new file +20 -0
new file mode 100644
index 000000000000..2395c7f1d543
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include "kvm_util.h"
+
+void ucall(uint64_t cmd, int nargs, ...)
+{
+	struct ucall uc = {};
+	va_list va;
+	int i;
+
+	WRITE_ONCE(uc.cmd, cmd);
+
+	nargs = min(nargs, UCALL_MAX_ARGS);
+
+	va_start(va, nargs);
+	for (i = 0; i < nargs; ++i)
+		WRITE_ONCE(uc.args[i], va_arg(va, uint64_t));
+	va_end(va);
+
+	ucall_arch_do_ucall((vm_vaddr_t)&uc);
+}
tools/testing/selftests/kvm/lib/x86_64/ucall.c +5 -18
index e5f0f9e0d3ee..9f532dba1003 100644
@@ -8,34 +8,21 @@
 
 #define UCALL_PIO_PORT ((uint16_t)0x1000)
 
-void ucall_init(struct kvm_vm *vm, void *arg)
+void ucall_arch_init(struct kvm_vm *vm, void *arg)
 {
 }
 
-void ucall_uninit(struct kvm_vm *vm)
+void ucall_arch_uninit(struct kvm_vm *vm)
 {
 }
 
-void ucall(uint64_t cmd, int nargs, ...)
+void ucall_arch_do_ucall(vm_vaddr_t uc)
 {
-	struct ucall uc = {
-		.cmd = cmd,
-	};
-	va_list va;
-	int i;
-
-	nargs = min(nargs, UCALL_MAX_ARGS);
-
-	va_start(va, nargs);
-	for (i = 0; i < nargs; ++i)
-		uc.args[i] = va_arg(va, uint64_t);
-	va_end(va);
-
 	asm volatile("in %[port], %%al"
-		: : [port] "d" (UCALL_PIO_PORT), "D" (&uc) : "rax", "memory");
+		: : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax", "memory");
 }
 
-uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 {
 	struct kvm_run *run = vcpu->run;
 	struct ucall ucall = {};
Signature · 1 line
2.37.1.559.g78731f0fdb-goog
Reviewed-by on v3 6/11
#7 lore raw

Re: [V3 06/11] KVM: selftests: Consolidate common code for popuplating

Quoted 104 lines · Peter Gonda

From: Sean Christopherson <[email protected]>

Make ucall() a common helper that populates struct ucall, and only calls into arch code to make the actually call out to userspace.

Rename all arch-specific helpers to make it clear they're arch-specific, and to avoid collisions with common helpers (one more on its way...)

Add WRITE_ONCE() to stores in ucall() code to as already done to aarch64 code in commit 9e2f6498efbb ("selftests: KVM: Handle compiler optimizations in ucall") to prevent clang optimizations breaking ucalls.

---
 tools/testing/selftests/kvm/Makefile          |  1 +
 .../selftests/kvm/include/ucall_common.h      | 23 ++++++++++++++++---
 .../testing/selftests/kvm/lib/aarch64/ucall.c | 20 ++++------------
 tools/testing/selftests/kvm/lib/riscv/ucall.c | 23 ++++---------------
 tools/testing/selftests/kvm/lib/s390x/ucall.c | 23 ++++---------------
 .../testing/selftests/kvm/lib/ucall_common.c  | 20 ++++++++++++++++
 .../testing/selftests/kvm/lib/x86_64/ucall.c  | 23 ++++---------------
 7 files changed, 60 insertions(+), 73 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/lib/ucall_common.c
tools/testing/selftests/kvm/Makefile +1 -0
index 690b499c3471..39fc5e8e5594 100644
@@ -46,6 +46,7 @@ LIBKVM += lib/perf_test_util.c
 LIBKVM += lib/rbtree.c
 LIBKVM += lib/sparsebit.c
 LIBKVM += lib/test_util.c
+LIBKVM += lib/ucall_common.c
 
 LIBKVM_x86_64 += lib/x86_64/apic.c
 LIBKVM_x86_64 += lib/x86_64/handlers.S
tools/testing/selftests/kvm/include/ucall_common.h +20 -3
index ee79d180e07e..5a85f5318bbe 100644
@@ -24,10 +24,27 @@ struct ucall {
 	uint64_t args[UCALL_MAX_ARGS];
 };
 
-void ucall_init(struct kvm_vm *vm, void *arg);
-void ucall_uninit(struct kvm_vm *vm);
+void ucall_arch_init(struct kvm_vm *vm, void *arg);
+void ucall_arch_uninit(struct kvm_vm *vm);
+void ucall_arch_do_ucall(vm_vaddr_t uc);
+uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
+
 void ucall(uint64_t cmd, int nargs, ...);
-uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
+
+static inline void ucall_init(struct kvm_vm *vm, void *arg)
+{
+	ucall_arch_init(vm, arg);
+}
+
+static inline void ucall_uninit(struct kvm_vm *vm)
+{
+	ucall_arch_uninit(vm);
+}
+
+static inline uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+{
+	return ucall_arch_get_ucall(vcpu, uc);
+}
 
 #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4)	\
 				ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
tools/testing/selftests/kvm/lib/aarch64/ucall.c +3 -6
index ed237b744690..1c81a6a5c1f2 100644
@@ -21,7 +21,7 @@ static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa)
 	return true;
 }
 
-void ucall_init(struct kvm_vm *vm, void *arg)
+void ucall_arch_init(struct kvm_vm *vm, void *arg)
 {
 	vm_paddr_t gpa, start, end, step, offset;
 	unsigned int bits;
@@ -64,30 +64,18 @@ void ucall_init(struct kvm_vm *vm, void *arg)
 	TEST_FAIL("Can't find a ucall mmio address");
 }
 
-void ucall_uninit(struct kvm_vm *vm)
+void ucall_arch_uninit(struct kvm_vm *vm)
 {
 	ucall_exit_mmio_addr = 0;
 	sync_global_to_guest(vm, ucall_exit_mmio_addr);
 }
 
-void ucall(uint64_t cmd, int nargs, ...)
+void ucall_arch_do_ucall(vm_vaddr_t uc)
 {
-	struct ucall uc = {};
-	va_list va;
-	int i;

tools/testing/selftests/kvm/lib/aarch64/ucall.c void ucall_init(struct kvm_vm *vm, void *arg) · line 75

Quoted 10 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +0 -9
-
-	WRITE_ONCE(uc.cmd, cmd);
-	nargs = min(nargs, UCALL_MAX_ARGS);
-
-	va_start(va, nargs);
-	for (i = 0; i < nargs; ++i)
-		WRITE_ONCE(uc.args[i], va_arg(va, uint64_t));
-	va_end(va);
-
 	WRITE_ONCE(*ucall_exit_mmio_addr, (vm_vaddr_t)&uc);

^ should just be uc

Quoted 173 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +1 -1
 }
 
-uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 {
 	struct kvm_run *run = vcpu->run;
 	struct ucall ucall = {};
tools/testing/selftests/kvm/lib/riscv/ucall.c +5 -18
index 087b9740bc8f..b1598f418c1f 100644
@@ -10,11 +10,11 @@
 #include "kvm_util.h"
 #include "processor.h"
 
-void ucall_init(struct kvm_vm *vm, void *arg)
+void ucall_arch_init(struct kvm_vm *vm, void *arg)
 {
 }
 
-void ucall_uninit(struct kvm_vm *vm)
+void ucall_arch_uninit(struct kvm_vm *vm)
 {
 }
 
@@ -44,27 +44,14 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
 	return ret;
 }
 
-void ucall(uint64_t cmd, int nargs, ...)
+void ucall_arch_do_ucall(vm_vaddr_t uc)
 {
-	struct ucall uc = {
-		.cmd = cmd,
-	};
-	va_list va;
-	int i;
-
-	nargs = min(nargs, UCALL_MAX_ARGS);
-
-	va_start(va, nargs);
-	for (i = 0; i < nargs; ++i)
-		uc.args[i] = va_arg(va, uint64_t);
-	va_end(va);
-
 	sbi_ecall(KVM_RISCV_SELFTESTS_SBI_EXT,
 		  KVM_RISCV_SELFTESTS_SBI_UCALL,
-		  (vm_vaddr_t)&uc, 0, 0, 0, 0, 0);
+		  uc, 0, 0, 0, 0, 0);
 }
 
-uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 {
 	struct kvm_run *run = vcpu->run;
 	struct ucall ucall = {};
tools/testing/selftests/kvm/lib/s390x/ucall.c +5 -18
index 73dc4e21190f..114cb4af295f 100644
@@ -6,34 +6,21 @@
  */
 #include "kvm_util.h"
 
-void ucall_init(struct kvm_vm *vm, void *arg)
+void ucall_arch_init(struct kvm_vm *vm, void *arg)
 {
 }
 
-void ucall_uninit(struct kvm_vm *vm)
+void ucall_arch_uninit(struct kvm_vm *vm)
 {
 }
 
-void ucall(uint64_t cmd, int nargs, ...)
+void ucall_arch_do_ucall(vm_vaddr_t uc)
 {
-	struct ucall uc = {
-		.cmd = cmd,
-	};
-	va_list va;
-	int i;
-
-	nargs = min(nargs, UCALL_MAX_ARGS);
-
-	va_start(va, nargs);
-	for (i = 0; i < nargs; ++i)
-		uc.args[i] = va_arg(va, uint64_t);
-	va_end(va);
-
 	/* Exit via DIAGNOSE 0x501 (normally used for breakpoints) */
-	asm volatile ("diag 0,%0,0x501" : : "a"(&uc) : "memory");
+	asm volatile ("diag 0,%0,0x501" : : "a"(uc) : "memory");
 }
 
-uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 {
 	struct kvm_run *run = vcpu->run;
 	struct ucall ucall = {};
tools/testing/selftests/kvm/lib/ucall_common.c new file +20 -0
new file mode 100644
index 000000000000..2395c7f1d543
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include "kvm_util.h"
+
+void ucall(uint64_t cmd, int nargs, ...)
+{
+	struct ucall uc = {};
+	va_list va;
+	int i;
+
+	WRITE_ONCE(uc.cmd, cmd);
+
+	nargs = min(nargs, UCALL_MAX_ARGS);
+
+	va_start(va, nargs);
+	for (i = 0; i < nargs; ++i)
+		WRITE_ONCE(uc.args[i], va_arg(va, uint64_t));
+	va_end(va);
+
+	ucall_arch_do_ucall((vm_vaddr_t)&uc);
+}
tools/testing/selftests/kvm/lib/x86_64/ucall.c +5 -18
index e5f0f9e0d3ee..9f532dba1003 100644
@@ -8,34 +8,21 @@
 
 #define UCALL_PIO_PORT ((uint16_t)0x1000)
 
-void ucall_init(struct kvm_vm *vm, void *arg)
+void ucall_arch_init(struct kvm_vm *vm, void *arg)
 {
 }
 
-void ucall_uninit(struct kvm_vm *vm)
+void ucall_arch_uninit(struct kvm_vm *vm)
 {
 }
 
-void ucall(uint64_t cmd, int nargs, ...)
+void ucall_arch_do_ucall(vm_vaddr_t uc)
 {
-	struct ucall uc = {
-		.cmd = cmd,
-	};
-	va_list va;
-	int i;
-
-	nargs = min(nargs, UCALL_MAX_ARGS);
-
-	va_start(va, nargs);
-	for (i = 0; i < nargs; ++i)
-		uc.args[i] = va_arg(va, uint64_t);
-	va_end(va);
-
 	asm volatile("in %[port], %%al"
-		: : [port] "d" (UCALL_PIO_PORT), "D" (&uc) : "rax", "memory");
+		: : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax", "memory");
 }
 
-uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 {
 	struct kvm_run *run = vcpu->run;
 	struct ucall ucall = {};
Signature · 1 line
2.37.1.559.g78731f0fdb-goog

Otherwise,

#8 lore raw

PATCH v3 7/11 KVM: selftests: Consolidate boilerplate code in get_ucall()

In reply to Peter Gonda

From: Sean Christopherson <[email protected]>

Consolidate the actual copying of a ucall struct from guest=>host into the common get_ucall(). Return a host virtual address instead of a guest virtual address even though the addr_gva2hva() part could be moved to get_ucall() too. Conceptually, get_ucall() is invoked from the host and should return a host virtual address (and returning NULL for "nothing to see here" is far superior to returning 0).

Use pointer shenanigans instead of an unnecessary bounce buffer when the caller of get_ucall() provides a valid pointer.

---
 .../selftests/kvm/include/ucall_common.h      |  8 ++------
 .../testing/selftests/kvm/lib/aarch64/ucall.c | 14 +++-----------
 tools/testing/selftests/kvm/lib/riscv/ucall.c | 19 +++----------------
 tools/testing/selftests/kvm/lib/s390x/ucall.c | 16 +++-------------
 .../testing/selftests/kvm/lib/ucall_common.c  | 19 +++++++++++++++++++
 .../testing/selftests/kvm/lib/x86_64/ucall.c  | 16 +++-------------
 6 files changed, 33 insertions(+), 59 deletions(-)
tools/testing/selftests/kvm/include/ucall_common.h +2 -6
index 5a85f5318bbe..63bfc60be995 100644
@@ -27,9 +27,10 @@ struct ucall {
 void ucall_arch_init(struct kvm_vm *vm, void *arg);
 void ucall_arch_uninit(struct kvm_vm *vm);
 void ucall_arch_do_ucall(vm_vaddr_t uc);
-uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
+void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);
 
 void ucall(uint64_t cmd, int nargs, ...);
+uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
 
 static inline void ucall_init(struct kvm_vm *vm, void *arg)
 {
@@ -41,11 +42,6 @@ static inline void ucall_uninit(struct kvm_vm *vm)
 	ucall_arch_uninit(vm);
 }
 
-static inline uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
-{
-	return ucall_arch_get_ucall(vcpu, uc);
-}
-
 #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4)	\
 				ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
 #define GUEST_SYNC(stage)	ucall(UCALL_SYNC, 2, "hello", stage)
tools/testing/selftests/kvm/lib/aarch64/ucall.c +3 -11
index 1c81a6a5c1f2..132c0e98bf49 100644
@@ -75,13 +75,9 @@ void ucall_arch_do_ucall(vm_vaddr_t uc)
 	WRITE_ONCE(*ucall_exit_mmio_addr, (vm_vaddr_t)&uc);
 }
 
-uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 {
 	struct kvm_run *run = vcpu->run;
-	struct ucall ucall = {};
-
-	if (uc)
-		memset(uc, 0, sizeof(*uc));
 
 	if (run->exit_reason == KVM_EXIT_MMIO &&
 	    run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
@@ -90,12 +86,8 @@ uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 		TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
 			    "Unexpected ucall exit mmio address access");
 		memcpy(&gva, run->mmio.data, sizeof(gva));
-		memcpy(&ucall, addr_gva2hva(vcpu->vm, gva), sizeof(ucall));
-
-		vcpu_run_complete_io(vcpu);
-		if (uc)
-			memcpy(uc, &ucall, sizeof(ucall));
+		return addr_gva2hva(vcpu->vm, gva);
 	}
 
-	return ucall.cmd;
+	return NULL;
 }
tools/testing/selftests/kvm/lib/riscv/ucall.c +3 -16
index b1598f418c1f..37e091d4366e 100644
@@ -51,27 +51,15 @@ void ucall_arch_do_ucall(vm_vaddr_t uc)
 		  uc, 0, 0, 0, 0, 0);
 }
 
-uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 {
 	struct kvm_run *run = vcpu->run;
-	struct ucall ucall = {};
-
-	if (uc)
-		memset(uc, 0, sizeof(*uc));
 
 	if (run->exit_reason == KVM_EXIT_RISCV_SBI &&
 	    run->riscv_sbi.extension_id == KVM_RISCV_SELFTESTS_SBI_EXT) {
 		switch (run->riscv_sbi.function_id) {
 		case KVM_RISCV_SELFTESTS_SBI_UCALL:
-			memcpy(&ucall,
-			       addr_gva2hva(vcpu->vm, run->riscv_sbi.args[0]),
-			       sizeof(ucall));
-
-			vcpu_run_complete_io(vcpu);
-			if (uc)
-				memcpy(uc, &ucall, sizeof(ucall));
-
-			break;
+			return addr_gva2hva(vcpu->vm, run->riscv_sbi.args[0]);
 		case KVM_RISCV_SELFTESTS_SBI_UNEXP:
 			vcpu_dump(stderr, vcpu, 2);
 			TEST_ASSERT(0, "Unexpected trap taken by guest");
@@ -80,6 +68,5 @@ uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 			break;
 		}
 	}
-
-	return ucall.cmd;
+	return NULL;
 }
tools/testing/selftests/kvm/lib/s390x/ucall.c +3 -13
index 114cb4af295f..0f695a031d35 100644
@@ -20,13 +20,9 @@ void ucall_arch_do_ucall(vm_vaddr_t uc)
 	asm volatile ("diag 0,%0,0x501" : : "a"(uc) : "memory");
 }
 
-uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 {
 	struct kvm_run *run = vcpu->run;
-	struct ucall ucall = {};
-
-	if (uc)
-		memset(uc, 0, sizeof(*uc));
 
 	if (run->exit_reason == KVM_EXIT_S390_SIEIC &&
 	    run->s390_sieic.icptcode == 4 &&
@@ -34,13 +30,7 @@ uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 	    (run->s390_sieic.ipb >> 16) == 0x501) {
 		int reg = run->s390_sieic.ipa & 0xf;
 
-		memcpy(&ucall, addr_gva2hva(vcpu->vm, run->s.regs.gprs[reg]),
-		       sizeof(ucall));
-
-		vcpu_run_complete_io(vcpu);
-		if (uc)
-			memcpy(uc, &ucall, sizeof(ucall));
+		return addr_gva2hva(vcpu->vm, run->s.regs.gprs[reg]);
 	}
-
-	return ucall.cmd;
+	return NULL;
 }
tools/testing/selftests/kvm/lib/ucall_common.c +19 -0
index 2395c7f1d543..ced480860746 100644
@@ -18,3 +18,22 @@ void ucall(uint64_t cmd, int nargs, ...)
 
 	ucall_arch_do_ucall((vm_vaddr_t)&uc);
 }
+
+uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+{
+	struct ucall ucall;
+	void *addr;
+
+	if (!uc)
+		uc = &ucall;
+
+	addr = ucall_arch_get_ucall(vcpu);
+	if (addr) {
+		memcpy(uc, addr, sizeof(*uc));
+		vcpu_run_complete_io(vcpu);
+	} else {
+		memset(uc, 0, sizeof(*uc));
+	}
+
+	return uc->cmd;
+}
tools/testing/selftests/kvm/lib/x86_64/ucall.c +3 -13
index 9f532dba1003..ead9946399ab 100644
@@ -22,25 +22,15 @@ void ucall_arch_do_ucall(vm_vaddr_t uc)
 		: : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax", "memory");
 }
 
-uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
+void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 {
 	struct kvm_run *run = vcpu->run;
-	struct ucall ucall = {};
-
-	if (uc)
-		memset(uc, 0, sizeof(*uc));
 
 	if (run->exit_reason == KVM_EXIT_IO && run->io.port == UCALL_PIO_PORT) {
 		struct kvm_regs regs;
 
 		vcpu_regs_get(vcpu, &regs);
-		memcpy(&ucall, addr_gva2hva(vcpu->vm, (vm_vaddr_t)regs.rdi),
-		       sizeof(ucall));
-
-		vcpu_run_complete_io(vcpu);
-		if (uc)
-			memcpy(uc, &ucall, sizeof(ucall));
+		return addr_gva2hva(vcpu->vm, regs.rdi);
 	}
-
-	return ucall.cmd;
+	return NULL;
 }
Signature · 1 line
2.37.1.559.g78731f0fdb-goog
Reviewed-by on v3 7/11
#9 lore raw

Re: [V3 07/11] KVM: selftests: Consolidate boilerplate code in get_ucall()

Quoted 14 lines · Peter Gonda

From: Sean Christopherson <[email protected]>

Consolidate the actual copying of a ucall struct from guest=>host into the common get_ucall(). Return a host virtual address instead of a guest virtual address even though the addr_gva2hva() part could be moved to get_ucall() too. Conceptually, get_ucall() is invoked from the host and should return a host virtual address (and returning NULL for "nothing to see here" is far superior to returning 0).

Use pointer shenanigans instead of an unnecessary bounce buffer when the caller of get_ucall() provides a valid pointer.

Quoted 9 lines
---
 .../selftests/kvm/include/ucall_common.h      |  8 ++------
 .../testing/selftests/kvm/lib/aarch64/ucall.c | 14 +++-----------
 tools/testing/selftests/kvm/lib/riscv/ucall.c | 19 +++----------------
 tools/testing/selftests/kvm/lib/s390x/ucall.c | 16 +++-------------
 .../testing/selftests/kvm/lib/ucall_common.c  | 19 +++++++++++++++++++
 .../testing/selftests/kvm/lib/x86_64/ucall.c  | 16 +++-------------
 6 files changed, 33 insertions(+), 59 deletions(-)
#10 lore raw

PATCH v3 8/11 KVM: selftests: add library for creating/interacting with SEV guests

In reply to Peter Gonda

From: Michael Roth <[email protected]>

Add interfaces to allow tests to create/manage SEV guests. The additional state associated with these guests is encapsulated in a new struct sev_vm, which is a light wrapper around struct kvm_vm. These VMs will use vm_set_memory_encryption() and vm_get_encrypted_phy_pages() under the covers to configure and sync up with the core kvm_util library on what should/shouldn't be treated as encrypted memory.

---
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/include/kvm_util_base.h     |   3 +
 .../selftests/kvm/include/x86_64/sev.h        |  44 +++
 tools/testing/selftests/kvm/lib/x86_64/sev.c  | 251 ++++++++++++++++++
 4 files changed, 299 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/include/x86_64/sev.h
 create mode 100644 tools/testing/selftests/kvm/lib/x86_64/sev.c
tools/testing/selftests/kvm/Makefile +1 -0
index 39fc5e8e5594..b247c4b595af 100644
@@ -55,6 +55,7 @@ LIBKVM_x86_64 += lib/x86_64/processor.c
 LIBKVM_x86_64 += lib/x86_64/svm.c
 LIBKVM_x86_64 += lib/x86_64/ucall.c
 LIBKVM_x86_64 += lib/x86_64/vmx.c
+LIBKVM_x86_64 += lib/x86_64/sev.c
 
 LIBKVM_aarch64 += lib/aarch64/gic.c
 LIBKVM_aarch64 += lib/aarch64/gic_v3.c
tools/testing/selftests/kvm/include/kvm_util_base.h +3 -0
index 8ce9e5be70a3..1a84d2d1d85b 100644
@@ -68,6 +68,7 @@ struct userspace_mem_regions {
 /* Memory encryption policy/configuration. */
 struct vm_memcrypt {
 	bool enabled;
+	bool encrypted;
 	int8_t enc_by_default;
 	bool has_enc_bit;
 	int8_t enc_bit;
@@ -816,6 +817,8 @@ vm_paddr_t addr_arch_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva);
 
 static inline vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva)
 {
+	TEST_ASSERT(!vm->memcrypt.encrypted,
+		    "Encrypted guests have their page tables encrypted so gva2gpa conversions are not possible.");
 	return addr_arch_gva2gpa(vm, gva);
 }
 
tools/testing/selftests/kvm/include/x86_64/sev.h new file +44 -0

44 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86_64/sev.c new file +251 -0

251 lines not on this page · raw message

Signature · 1 line
2.37.1.559.g78731f0fdb-goog
#11 lore raw

Re: [V3 08/11] KVM: selftests: add library for creating/interacting with SEV guests

tools/testing/selftests/kvm/include/x86_64/sev.h line 31

Quoted 7 lines · Peter Gonda
tools/testing/selftests/kvm/include/x86_64/sev.h new file +7 -0
+enum {
+       SEV_GSTATE_UNINIT = 0,
+       SEV_GSTATE_LUPDATE,
+       SEV_GSTATE_LSECRET,
+       SEV_GSTATE_RUNNING,
+};
+

Name the enum, e.g. enum sev_guest_state?

And s/GSTATE/GUEST? Ugh, AMD's documentation uses GSTATE.

But looking at the docs, I only see GSTATE_LAUNCH? Or does SEV have different status codes than -ES and/or -SNP?

tools/testing/selftests/kvm/lib/x86_64/sev.c line 30

Quoted 3 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +3 -0
+struct kvm_vm *sev_get_vm(struct sev_vm *sev)
+{
+	return sev->vm;

Why bother with a wrapper?

tools/testing/selftests/kvm/lib/x86_64/sev.c line 34

Quoted 4 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +4 -0
+}
+
+uint8_t sev_get_enc_bit(struct sev_vm *sev)
+{

Same here, IMO it just obfuscates code with no real benefit. ANd it's inconsistent, e.g. why have a wrapper for enc_bit but not sev->fd?

Quoted 24 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +24 -0
+	return sev->enc_bit;
+}
+
+void sev_ioctl(int sev_fd, int cmd, void *data)
+{
+	int ret;
+	struct sev_issue_cmd arg;
+
+	arg.cmd = cmd;
+	arg.data = (unsigned long)data;
+	ret = ioctl(sev_fd, SEV_ISSUE_CMD, &arg);
+	TEST_ASSERT(ret == 0,
+		    "SEV ioctl %d failed, error: %d, fw_error: %d",
+		    cmd, ret, arg.error);
+}
+
+void kvm_sev_ioctl(struct sev_vm *sev, int cmd, void *data)
+{
+	struct kvm_sev_cmd arg = {0};
+	int ret;
+
+	arg.id = cmd;
+	arg.sev_fd = sev->fd;
+	arg.data = (__u64)data;

tools/testing/selftests/kvm/lib/x86_64/sev.c line 68

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +10 -0
+
+	ret = ioctl(sev->vm->fd, KVM_MEMORY_ENCRYPT_OP, &arg);
+	TEST_ASSERT(ret == 0,
+		    "SEV KVM ioctl %d failed, rc: %i errno: %i (%s), fw_error: %d",
+		    cmd, ret, errno, strerror(errno), arg.error);
+}
+
+/* Local helpers. */
+
+static void

Don't split here, e.g. a grep/search for the function, should also show the return type and any attributes, e.g. "static" vs. something else is typically much more interesting than the parameters (and parameters is not a fully solvable problem).

Quoted 1 line

+sev_register_user_region(struct sev_vm *sev, void *hva, uint64_t size)

Align like so:

static void sev_register_user_region(struct sev_vm *sev, void *hva,

				     uint64_t size)

or maybe even let it poke out.

Quoted 5 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +5 -0
+{
+	struct kvm_enc_region range = {0};
+	int ret;
+
+	pr_debug("%s: hva: %p, size: %lu\n", __func__, hva, size);

tools/testing/selftests/kvm/lib/x86_64/sev.c line 84

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +10 -0
+
+	range.addr = (__u64)hva;
+	range.size = size;
+
+	ret = ioctl(sev->vm->fd, KVM_MEMORY_ENCRYPT_REG_REGION, &range);
+	TEST_ASSERT(ret == 0, "failed to register user range, errno: %i\n", errno);
+}
+
+static void
+sev_encrypt_phy_range(struct sev_vm *sev, vm_paddr_t gpa, uint64_t size)

Same thing here.

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +10 -0
+{
+	struct kvm_sev_launch_update_data ksev_update_data = {0};
+
+	pr_debug("%s: addr: 0x%lx, size: %lu\n", __func__, gpa, size);
+
+	ksev_update_data.uaddr = (__u64)addr_gpa2hva(sev->vm, gpa);
+	ksev_update_data.len = size;
+
+	kvm_sev_ioctl(sev, KVM_SEV_LAUNCH_UPDATE_DATA, &ksev_update_data);
+}

tools/testing/selftests/kvm/lib/x86_64/sev.c line 104

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +10 -0
+
+static void sev_encrypt(struct sev_vm *sev)
+{
+	const struct sparsebit *enc_phy_pages;
+	struct kvm_vm *vm = sev->vm;
+	sparsebit_idx_t pg = 0;
+	vm_paddr_t gpa_start;
+	uint64_t memory_size;
+
+	/* Only memslot 0 supported for now. */

Eww. Haven't looked at this in depth, but is there a way to avoid hardcoding the memslot in this code?

tools/testing/selftests/kvm/lib/x86_64/sev.c line 205

Quoted 4 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +4 -0
+void sev_vm_launch(struct sev_vm *sev)
+{
+	struct kvm_sev_launch_start ksev_launch_start = {0};
+	struct kvm_sev_guest_status ksev_status = {0};

Doesn't " = {};" do the same thing? And for the status, and any other cases where userspace is reading, wouldn't it be better from a test coverage perspective to _not_ zero the data? Hmm, though I suppose false passes are possible in that case...

tools/testing/selftests/kvm/lib/x86_64/sev.c line 208

Quoted 2 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +2 -0
+	/* Need to use ucall_shared for synchronization. */
+	//ucall_init_ops(sev_get_vm(sev), NULL, &ucall_ops_halt);

Can this be deleted? If not, what's up?

#12 lore raw

Re: [V3 08/11] KVM: selftests: add library for creating/interacting with SEV guests

Quoted 11 lines · Sean Christopherson

tools/testing/selftests/kvm/include/x86_64/sev.h line 31

Quoted 7 lines · Peter Gonda
tools/testing/selftests/kvm/include/x86_64/sev.h new file +7 -0
+enum {
+       SEV_GSTATE_UNINIT = 0,
+       SEV_GSTATE_LUPDATE,
+       SEV_GSTATE_LSECRET,
+       SEV_GSTATE_RUNNING,
+};
+

Name the enum, e.g. enum sev_guest_state?

Done

Quoted 5 lines

And s/GSTATE/GUEST? Ugh, AMD's documentation uses GSTATE.

But looking at the docs, I only see GSTATE_LAUNCH? Or does SEV have different status codes than -ES and/or -SNP?

SEV and SEV-ES have the same states, SNP has a different set.

See "Table 43. GSTATE Finite State Machine":
https://www.amd.com/system/files/TechDocs/55766_SEV-KM_API_Specification.pdf

Quoted 7 lines

tools/testing/selftests/kvm/lib/x86_64/sev.c line 30

Quoted 4 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +3 -0
+struct kvm_vm *sev_get_vm(struct sev_vm *sev)
+{
+     return sev->vm;

Why bother with a wrapper?

Quoted 8 lines

tools/testing/selftests/kvm/lib/x86_64/sev.c line 34

Quoted 4 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +4 -0
+}
+
+uint8_t sev_get_enc_bit(struct sev_vm *sev)
+{

Same here, IMO it just obfuscates code with no real benefit. ANd it's inconsistent, e.g. why have a wrapper for enc_bit but not sev->fd?

Removed.

Quoted 39 lines
Quoted 24 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +24 -0
+     return sev->enc_bit;
+}
+
+void sev_ioctl(int sev_fd, int cmd, void *data)
+{
+     int ret;
+     struct sev_issue_cmd arg;
+
+     arg.cmd = cmd;
+     arg.data = (unsigned long)data;
+     ret = ioctl(sev_fd, SEV_ISSUE_CMD, &arg);
+     TEST_ASSERT(ret == 0,
+                 "SEV ioctl %d failed, error: %d, fw_error: %d",
+                 cmd, ret, arg.error);
+}
+
+void kvm_sev_ioctl(struct sev_vm *sev, int cmd, void *data)
+{
+     struct kvm_sev_cmd arg = {0};
+     int ret;
+
+     arg.id = cmd;
+     arg.sev_fd = sev->fd;
+     arg.data = (__u64)data;

tools/testing/selftests/kvm/lib/x86_64/sev.c line 68

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +10 -0
+
+     ret = ioctl(sev->vm->fd, KVM_MEMORY_ENCRYPT_OP, &arg);
+     TEST_ASSERT(ret == 0,
+                 "SEV KVM ioctl %d failed, rc: %i errno: %i (%s), fw_error: %d",
+                 cmd, ret, errno, strerror(errno), arg.error);
+}
+
+/* Local helpers. */
+
+static void

Don't split here, e.g. a grep/search for the function, should also show the return type and any attributes, e.g. "static" vs. something else is typically much more interesting than the parameters (and parameters is not a fully solvable problem).

Quoted 8 lines
Quoted 1 line

+sev_register_user_region(struct sev_vm *sev, void *hva, uint64_t size)

Align like so:

static void sev_register_user_region(struct sev_vm *sev, void *hva,

                                     uint64_t size)

or maybe even let it poke out.

Ah never thought about that, sounds good. Done.

Quoted 34 lines
Quoted 6 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +5 -0
+{
+     struct kvm_enc_region range = {0};
+     int ret;
+
+     pr_debug("%s: hva: %p, size: %lu\n", __func__, hva, size);

tools/testing/selftests/kvm/lib/x86_64/sev.c line 84

Quoted 10 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +10 -0
+
+     range.addr = (__u64)hva;
+     range.size = size;
+
+     ret = ioctl(sev->vm->fd, KVM_MEMORY_ENCRYPT_REG_REGION, &range);
+     TEST_ASSERT(ret == 0, "failed to register user range, errno: %i\n", errno);
+}
+
+static void
+sev_encrypt_phy_range(struct sev_vm *sev, vm_paddr_t gpa, uint64_t size)

Same thing here.

Quoted 15 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +15 -0
+{
+     struct kvm_sev_launch_update_data ksev_update_data = {0};
+
+     pr_debug("%s: addr: 0x%lx, size: %lu\n", __func__, gpa, size);
+
+     ksev_update_data.uaddr = (__u64)addr_gpa2hva(sev->vm, gpa);
+     ksev_update_data.len = size;
+
+     kvm_sev_ioctl(sev, KVM_SEV_LAUNCH_UPDATE_DATA, &ksev_update_data);
+}
+
+static void sev_encrypt(struct sev_vm *sev)
+{
+     const struct sparsebit *enc_phy_pages;
+     struct kvm_vm *vm = sev->vm;
Quoted 8 lines

tools/testing/selftests/kvm/lib/x86_64/sev.c line 104

Quoted 5 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +5 -0
+     sparsebit_idx_t pg = 0;
+     vm_paddr_t gpa_start;
+     uint64_t memory_size;
+
+     /* Only memslot 0 supported for now. */

Eww. Haven't looked at this in depth, but is there a way to avoid hardcoding the memslot in this code?

Done.

Quoted 9 lines

tools/testing/selftests/kvm/lib/x86_64/sev.c line 205

Quoted 5 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +4 -0
+void sev_vm_launch(struct sev_vm *sev)
+{
+     struct kvm_sev_launch_start ksev_launch_start = {0};
+     struct kvm_sev_guest_status ksev_status = {0};

Doesn't " = {};" do the same thing? And for the status, and any other cases where userspace is reading, wouldn't it be better from a test coverage perspective to _not_ zero the data? Hmm, though I suppose false passes are possible in that case...

We want to zero out kvm_sev_launch_start because it has main fields we explicitly want zero: the dh_* and session_* fields need to be set up correctly or zeroed. We can add a test for these later if we want, but it would be mostly testing userspace + the PSP.

We can not zero status I agree.

Quoted 5 lines

tools/testing/selftests/kvm/lib/x86_64/sev.c line 208

Quoted 3 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +2 -0
+     /* Need to use ucall_shared for synchronization. */
+     //ucall_init_ops(sev_get_vm(sev), NULL, &ucall_ops_halt);

Can this be deleted? If not, what's up?

Whoops, removed.

#13 lore raw

PATCH v3 9/11 tools: Add atomic_test_and_set_bit()

In reply to Peter Gonda

atomic_test_and_set_bit() allows for atomic bitmap usage from KVM selftests.

---
 tools/arch/x86/include/asm/atomic.h    |  7 +++++++
 tools/include/asm-generic/atomic-gcc.h | 15 +++++++++++++++
 2 files changed, 22 insertions(+)
tools/arch/x86/include/asm/atomic.h +7 -0
index 1f5e26aae9fc..01cc27ec4520 100644
@@ -8,6 +8,7 @@
 
 #define LOCK_PREFIX "\n\tlock; "
 
+#include <asm/asm.h>
 #include <asm/cmpxchg.h>
 
 /*
@@ -70,4 +71,10 @@ static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new)
 	return cmpxchg(&v->counter, old, new);
 }
 
+static inline int atomic_test_and_set_bit(long nr, unsigned long *addr)
+{
+	GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(bts), *addr, "Ir", nr, "%0", "c");
+
+}
+
 #endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */
tools/include/asm-generic/atomic-gcc.h +15 -0
index 4c1966f7c77a..8d9b2d1768bf 100644
@@ -4,6 +4,7 @@
 
 #include <linux/compiler.h>
 #include <linux/types.h>
+#include <linux/bitops.h>
 
 /*
  * Atomic operations that C can't guarantee us.  Useful for
@@ -69,4 +70,18 @@ static inline int atomic_cmpxchg(atomic_t *v, int oldval, int newval)
 	return cmpxchg(&(v)->counter, oldval, newval);
 }
 
+static inline int atomic_test_and_set_bit(long nr, unsigned long *addr)
+{
+	long old, val;
+	unsigned long mask = BIT_MASK(nr);
+
+	addr += BIT_WORD(nr);
+	val = READ_ONCE(*addr);
+	if (val & mask)
+		return 1;
+
+	old = cmpxchg(addr, val, val & mask);
+	return !!(old & mask);
+}
+
 #endif /* __TOOLS_ASM_GENERIC_ATOMIC_H */
Signature · 1 line
2.37.1.559.g78731f0fdb-goog
#14 lore raw

Re: [V3 09/11] tools: Add atomic_test_and_set_bit()

Quoted 23 lines · Peter Gonda

atomic_test_and_set_bit() allows for atomic bitmap usage from KVM selftests.

---
 tools/arch/x86/include/asm/atomic.h    |  7 +++++++
 tools/include/asm-generic/atomic-gcc.h | 15 +++++++++++++++
 2 files changed, 22 insertions(+)
tools/arch/x86/include/asm/atomic.h +1 -0
index 1f5e26aae9fc..01cc27ec4520 100644
@@ -8,6 +8,7 @@
 
 #define LOCK_PREFIX "\n\tlock; "
 
+#include <asm/asm.h>
 #include <asm/cmpxchg.h>
 
 /*

tools/arch/x86/include/asm/atomic.h static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new) · line 77

Quoted 7 lines
tools/arch/x86/include/asm/atomic.h +4 -0
@@ -70,4 +71,10 @@ static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new)
 	return cmpxchg(&v->counter, old, new);
 }
 
+static inline int atomic_test_and_set_bit(long nr, unsigned long *addr)
+{
+	GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(bts), *addr, "Ir", nr, "%0", "c");
+

Unnecessary newline.

Quoted 17 lines
tools/arch/x86/include/asm/atomic.h +2 -0
+}
+
 #endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */
tools/include/asm-generic/atomic-gcc.h +1 -0
index 4c1966f7c77a..8d9b2d1768bf 100644
@@ -4,6 +4,7 @@
 
 #include <linux/compiler.h>
 #include <linux/types.h>
+#include <linux/bitops.h>
 
 /*
  * Atomic operations that C can't guarantee us.  Useful for
@@ -69,4 +70,18 @@ static inline int atomic_cmpxchg(atomic_t *v, int oldval, int newval)
 	return cmpxchg(&(v)->counter, oldval, newval);

tools/include/asm-generic/atomic-gcc.h static inline int atomic_cmpxchg(atomic_t *v, int oldval, int newval) · line 80

Quoted 10 lines
tools/include/asm-generic/atomic-gcc.h +9 -0
 }
+static inline int atomic_test_and_set_bit(long nr, unsigned long *addr)
+{
+	long old, val;
+	unsigned long mask = BIT_MASK(nr);
+
+	addr += BIT_WORD(nr);
+	val = READ_ONCE(*addr);
+	if (val & mask)
+		return 1;

Probably should drop the READ_ONCE() shortcut to stay consistent with the kernel proper.

https://lore.kernel.org/all/CAHk-=wgSNiT5qJX53RHtWECsUiFq6d6VWYNAvu71ViOEan07yw@mail.gmail.com

tools/include/asm-generic/atomic-gcc.h static inline int atomic_cmpxchg(atomic_t *v, int oldval, int newval) · line 82

Quoted 2 lines
tools/include/asm-generic/atomic-gcc.h +2 -0
+
+	old = cmpxchg(addr, val, val & mask);

This is wrong on two fronts: 1) cmpxchg() writes the entire new value, and 2) it fails if the old value is not an exact match with what's in memory. Bug #1 means that setting a bit will clear all existing bits, and bug #2 means that this will fail to set the bit if another atomic_test_and_set_bit() sneaks in between reading into "val" and doing the cmpxchg.

And obviously dropping the READ_ONCE() above makes cmpxchg impossible (not a coincidence, it's simply the wrong operation to use).

I believe what we want is:

	unsigned long mask = BIT_MASK(nr);
	long old;

	old = __sync_fetch_and_or(addr, mask);
	return !!(old & mask);
Quoted 7 lines
tools/include/asm-generic/atomic-gcc.h +3 -0
+	return !!(old & mask);
+}
+
 #endif /* __TOOLS_ASM_GENERIC_ATOMIC_H */
Signature · 1 line
2.37.1.559.g78731f0fdb-goog
#15 lore raw

PATCH v3 10/11 KVM: selftests: Add ucall pool based implementation

In reply to Peter Gonda

To add support for encrypted, SEV, guests in the ucall framework introduce a new "ucall pool" implementation. This was suggested in the thread on "[RFC PATCH 00/10] KVM: selftests: Add support for test-selectable ucall implementations". Using a listed as suggested there doesn't work well because the list is setup using HVAs not GVAs so use a bitmap + array solution instead to get the same pool of ucall structs result.

This allows for guests with encryption enabled set up a pool of ucall structs in the guest's shared memory region.

---
 .../selftests/kvm/include/kvm_util_base.h     |   2 +
 .../selftests/kvm/include/ucall_common.h      |  13 +--
 .../testing/selftests/kvm/lib/aarch64/ucall.c |  10 +-
 tools/testing/selftests/kvm/lib/riscv/ucall.c |   5 +-
 tools/testing/selftests/kvm/lib/s390x/ucall.c |   5 +-
 .../testing/selftests/kvm/lib/ucall_common.c  | 108 +++++++++++++++++-
 .../testing/selftests/kvm/lib/x86_64/ucall.c  |   6 +-
 7 files changed, 131 insertions(+), 18 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +2 -0
index 1a84d2d1d85b..baede0d118c5 100644
@@ -103,6 +103,8 @@ struct kvm_vm {
 	int stats_fd;
 	struct kvm_stats_header stats_header;
 	struct kvm_stats_desc *stats_desc;
+
+	bool use_ucall_pool;
 };
 
 
tools/testing/selftests/kvm/include/ucall_common.h +5 -8
index 63bfc60be995..002a22e1cd1d 100644
@@ -22,6 +22,9 @@ enum {
 struct ucall {
 	uint64_t cmd;
 	uint64_t args[UCALL_MAX_ARGS];
+
+	/* For ucall pool usage. */
+	struct ucall *hva;
 };
 
 void ucall_arch_init(struct kvm_vm *vm, void *arg);
@@ -32,15 +35,9 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);
 void ucall(uint64_t cmd, int nargs, ...);
 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
 
-static inline void ucall_init(struct kvm_vm *vm, void *arg)
-{
-	ucall_arch_init(vm, arg);
-}
+void ucall_init(struct kvm_vm *vm, void *arg);
 
-static inline void ucall_uninit(struct kvm_vm *vm)
-{
-	ucall_arch_uninit(vm);
-}
+void ucall_uninit(struct kvm_vm *vm);
 
 #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4)	\
 				ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
tools/testing/selftests/kvm/lib/aarch64/ucall.c +7 -3
index 132c0e98bf49..ee70531e8e51 100644
@@ -81,12 +81,16 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 
 	if (run->exit_reason == KVM_EXIT_MMIO &&
 	    run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
-		vm_vaddr_t gva;
+		uint64_t ucall_addr;
 
 		TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
 			    "Unexpected ucall exit mmio address access");
-		memcpy(&gva, run->mmio.data, sizeof(gva));
-		return addr_gva2hva(vcpu->vm, gva);
+		memcpy(&ucall_addr, run->mmio.data, sizeof(ucall_addr));
+
+		if (vcpu->vm->use_ucall_pool)
+			return (void *)ucall_addr;
+		else
+			return addr_gva2hva(vcpu->vm, ucall_addr);
 	}
 
 	return NULL;
tools/testing/selftests/kvm/lib/riscv/ucall.c +4 -1
index 37e091d4366e..4bb5616df29f 100644
@@ -59,7 +59,10 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 	    run->riscv_sbi.extension_id == KVM_RISCV_SELFTESTS_SBI_EXT) {
 		switch (run->riscv_sbi.function_id) {
 		case KVM_RISCV_SELFTESTS_SBI_UCALL:
-			return addr_gva2hva(vcpu->vm, run->riscv_sbi.args[0]);
+			if (vcpu->vm->use_ucall_pool)
+				return (void *)run->riscv_sbi.args[0];
+			else
+				return addr_gva2hva(vcpu->vm, run->riscv_sbi.args[0]);
 		case KVM_RISCV_SELFTESTS_SBI_UNEXP:
 			vcpu_dump(stderr, vcpu, 2);
 			TEST_ASSERT(0, "Unexpected trap taken by guest");
tools/testing/selftests/kvm/lib/s390x/ucall.c +4 -1
index 0f695a031d35..b24c6649877a 100644
@@ -30,7 +30,10 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 	    (run->s390_sieic.ipb >> 16) == 0x501) {
 		int reg = run->s390_sieic.ipa & 0xf;
 
-		return addr_gva2hva(vcpu->vm, run->s.regs.gprs[reg]);
+		if (vcpu->vm->use_ucall_pool)
+			return (void *)run->s.regs.gprs[reg];
+		else
+			return addr_gva2hva(vcpu->vm, run->s.regs.gprs[reg]);
 	}
 	return NULL;
 }
tools/testing/selftests/kvm/lib/ucall_common.c +104 -4

126 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86_64/ucall.c +5 -1
index ead9946399ab..07c1bc41fa5c 100644
@@ -30,7 +30,11 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 		struct kvm_regs regs;
 
 		vcpu_regs_get(vcpu, &regs);
-		return addr_gva2hva(vcpu->vm, regs.rdi);
+
+		if (vcpu->vm->use_ucall_pool)
+			return (void *)regs.rdi;
+		else
+			return addr_gva2hva(vcpu->vm, regs.rdi);
 	}
 	return NULL;
 }
Signature · 1 line
2.37.1.559.g78731f0fdb-goog
#16 lore raw

Re: [V3 10/11] KVM: selftests: Add ucall pool based implementation

Quoted 4 lines · Peter Gonda

To add support for encrypted, SEV, guests in the ucall framework introduce a new "ucall pool" implementation. This was suggested in the thread on "[RFC PATCH 00/10] KVM: selftests: Add support for test-selectable ucall implementations". Using a listed as suggested

s/listed/list/

Quoted 5 lines

there doesn't work well because the list is setup using HVAs not GVAs so use a bitmap + array solution instead to get the same pool of ucall structs result.

This allows for guests with encryption enabled set up a pool of ucall

to set up

Quoted 64 lines

structs in the guest's shared memory region.

---
 .../selftests/kvm/include/kvm_util_base.h     |   2 +
 .../selftests/kvm/include/ucall_common.h      |  13 +--
 .../testing/selftests/kvm/lib/aarch64/ucall.c |  10 +-
 tools/testing/selftests/kvm/lib/riscv/ucall.c |   5 +-
 tools/testing/selftests/kvm/lib/s390x/ucall.c |   5 +-
 .../testing/selftests/kvm/lib/ucall_common.c  | 108 +++++++++++++++++-
 .../testing/selftests/kvm/lib/x86_64/ucall.c  |   6 +-
 7 files changed, 131 insertions(+), 18 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +2 -0
index 1a84d2d1d85b..baede0d118c5 100644
@@ -103,6 +103,8 @@ struct kvm_vm {
 	int stats_fd;
 	struct kvm_stats_header stats_header;
 	struct kvm_stats_desc *stats_desc;
+
+	bool use_ucall_pool;
 };
 
 
tools/testing/selftests/kvm/include/ucall_common.h +5 -8
index 63bfc60be995..002a22e1cd1d 100644
@@ -22,6 +22,9 @@ enum {
 struct ucall {
 	uint64_t cmd;
 	uint64_t args[UCALL_MAX_ARGS];
+
+	/* For ucall pool usage. */
+	struct ucall *hva;
 };
 
 void ucall_arch_init(struct kvm_vm *vm, void *arg);
@@ -32,15 +35,9 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);
 void ucall(uint64_t cmd, int nargs, ...);
 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
 
-static inline void ucall_init(struct kvm_vm *vm, void *arg)
-{
-	ucall_arch_init(vm, arg);
-}
+void ucall_init(struct kvm_vm *vm, void *arg);
 
-static inline void ucall_uninit(struct kvm_vm *vm)
-{
-	ucall_arch_uninit(vm);
-}
+void ucall_uninit(struct kvm_vm *vm);
 
 #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4)	\
 				ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
tools/testing/selftests/kvm/lib/aarch64/ucall.c +0 -0
index 132c0e98bf49..ee70531e8e51 100644

tools/testing/selftests/kvm/lib/aarch64/ucall.c void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 84

Quoted 5 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +1 -1
@@ -81,12 +81,16 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 
 	if (run->exit_reason == KVM_EXIT_MMIO &&
 	    run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
-		vm_vaddr_t gva;
+		uint64_t ucall_addr;

Why change this vm_vaddr_t to a uint64_t? We shouldn't, because...

tools/testing/selftests/kvm/lib/aarch64/ucall.c void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 88

Quoted 6 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +1 -2
 		TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
 			    "Unexpected ucall exit mmio address access");
-		memcpy(&gva, run->mmio.data, sizeof(gva));
-		return addr_gva2hva(vcpu->vm, gva);
+		memcpy(&ucall_addr, run->mmio.data, sizeof(ucall_addr));

...here we assume it's a vm_vaddr_t and only...

tools/testing/selftests/kvm/lib/aarch64/ucall.c void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 91

Quoted 3 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +3 -0
+
+		if (vcpu->vm->use_ucall_pool)
+			return (void *)ucall_addr;

...here do we know otherwise. So only here should be any casting.

Also, I think here and in all the ucall_arch_get_ucall() functions we should add a comment explaining a host-shared address is used, which is why we don't need addr_gva2hva()

Quoted 10 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +2 -0
+		else
+			return addr_gva2hva(vcpu->vm, ucall_addr);
 	}
 
 	return NULL;
tools/testing/selftests/kvm/lib/riscv/ucall.c +0 -0
index 37e091d4366e..4bb5616df29f 100644

tools/testing/selftests/kvm/lib/riscv/ucall.c void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 65

Quoted 8 lines
tools/testing/selftests/kvm/lib/riscv/ucall.c +4 -1
@@ -59,7 +59,10 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 	    run->riscv_sbi.extension_id == KVM_RISCV_SELFTESTS_SBI_EXT) {
 		switch (run->riscv_sbi.function_id) {
 		case KVM_RISCV_SELFTESTS_SBI_UCALL:
-			return addr_gva2hva(vcpu->vm, run->riscv_sbi.args[0]);
+			if (vcpu->vm->use_ucall_pool)
+				return (void *)run->riscv_sbi.args[0];
+			else
+				return addr_gva2hva(vcpu->vm, run->riscv_sbi.args[0]);

Using vm_vaddr_t gva variable for run->riscv_sbi.args[0] like aarch64 does for it's address would look a bit nicer.

Quoted 8 lines
tools/testing/selftests/kvm/lib/riscv/ucall.c +0 -0
 		case KVM_RISCV_SELFTESTS_SBI_UNEXP:
 			vcpu_dump(stderr, vcpu, 2);
 			TEST_ASSERT(0, "Unexpected trap taken by guest");
tools/testing/selftests/kvm/lib/s390x/ucall.c +0 -0
index 0f695a031d35..b24c6649877a 100644

tools/testing/selftests/kvm/lib/s390x/ucall.c void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 36

Quoted 8 lines
tools/testing/selftests/kvm/lib/s390x/ucall.c +4 -1
@@ -30,7 +30,10 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 	    (run->s390_sieic.ipb >> 16) == 0x501) {
 		int reg = run->s390_sieic.ipa & 0xf;
 
-		return addr_gva2hva(vcpu->vm, run->s.regs.gprs[reg]);
+		if (vcpu->vm->use_ucall_pool)
+			return (void *)run->s.regs.gprs[reg];
+		else
+			return addr_gva2hva(vcpu->vm, run->s.regs.gprs[reg]);

Same comment as for riscv.

Quoted 10 lines
tools/testing/selftests/kvm/lib/s390x/ucall.c +0 -0
 	}
 	return NULL;
 }
tools/testing/selftests/kvm/lib/ucall_common.c +0 -0
index ced480860746..b6502a9420c4 100644
@@ -1,22 +1,122 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include "kvm_util.h"

tools/testing/selftests/kvm/lib/ucall_common.c line 12

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +10 -0
+#include "linux/types.h"
+#include "linux/bitmap.h"
+#include "linux/atomic.h"
+
+struct ucall_header {
+	DECLARE_BITMAP(in_use, KVM_MAX_VCPUS);
+	struct ucall ucalls[KVM_MAX_VCPUS];
+};
+
+static bool use_ucall_pool;

I don't think we need this boolean. It will always be true when ucall_pool is non-null and always false with ucall_pool is null. So we can just test ucall_pool.

Quoted 5 lines
tools/testing/selftests/kvm/lib/ucall_common.c +5 -0
+static struct ucall_header *ucall_pool;
+
+void ucall_init(struct kvm_vm *vm, void *arg)
+{
+	struct ucall *uc;

tools/testing/selftests/kvm/lib/ucall_common.c line 27

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +10 -0
+	struct ucall_header *hdr;
+	vm_vaddr_t vaddr;
+	int i;
+
+	use_ucall_pool = vm->use_ucall_pool;
+	sync_global_to_guest(vm, use_ucall_pool);
+	if (!use_ucall_pool)
+		goto out;
+
+	TEST_ASSERT(!ucall_pool, "Only a single encrypted guest at a time for ucalls.");

"Only a single VM may use a ucall pool at a time"

Quoted 12 lines
tools/testing/selftests/kvm/lib/ucall_common.c +12 -0
+	vaddr = vm_vaddr_alloc_shared(vm, sizeof(*hdr), vm->page_size);
+	hdr = (struct ucall_header *)addr_gva2hva(vm, vaddr);
+	memset(hdr, 0, sizeof(*hdr));
+
+	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
+		uc = &hdr->ucalls[i];
+		uc->hva = uc;
+	}
+
+	ucall_pool = (struct ucall_header *)vaddr;
+	sync_global_to_guest(vm, ucall_pool);
+

tools/testing/selftests/kvm/lib/ucall_common.c line 49

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +10 -0
+out:
+	ucall_arch_init(vm, arg);
+}
+
+void ucall_uninit(struct kvm_vm *vm)
+{
+	use_ucall_pool = false;
+	ucall_pool = NULL;
+
+	if (!vm->memcrypt.encrypted) {

Why is this condition here?

Quoted 9 lines
tools/testing/selftests/kvm/lib/ucall_common.c +9 -0
+		sync_global_to_guest(vm, use_ucall_pool);
+		sync_global_to_guest(vm, ucall_pool);
+	}
+
+	ucall_arch_uninit(vm);
+}
+
+static struct ucall *ucall_alloc(void)
+{

tools/testing/selftests/kvm/lib/ucall_common.c line 68

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +10 -0
+	struct ucall *uc = NULL;
+	int i;
+
+	if (!use_ucall_pool)
+		goto out;
+
+	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
+		if (!atomic_test_and_set_bit(i, ucall_pool->in_use)) {
+			uc = &ucall_pool->ucalls[i];
+			memset(uc->args, 0, sizeof(uc->args));

Do we need to zero uc? If so, what about uc->cmd?

tools/testing/selftests/kvm/lib/ucall_common.c line 71

Quoted 3 lines
tools/testing/selftests/kvm/lib/ucall_common.c +3 -0
+			break;
+		}
+	}

Need a blank line here

Quoted 75 lines
tools/testing/selftests/kvm/lib/ucall_common.c +35 -4
+out:
+	return uc;
+}
+
+static inline size_t uc_pool_idx(struct ucall *uc)
+{
+	return uc->hva - ucall_pool->ucalls;
+}
+
+static void ucall_free(struct ucall *uc)
+{
+	if (!use_ucall_pool)
+		return;
+
+	clear_bit(uc_pool_idx(uc), ucall_pool->in_use);
+}
 
 void ucall(uint64_t cmd, int nargs, ...)
 {
-	struct ucall uc = {};
+	struct ucall *uc;
+	struct ucall tmp = {};
 	va_list va;
 	int i;
 
-	WRITE_ONCE(uc.cmd, cmd);
+	uc = ucall_alloc();
+	if (!uc)
+		uc = &tmp;
+
+	WRITE_ONCE(uc->cmd, cmd);
 
 	nargs = min(nargs, UCALL_MAX_ARGS);
 
 	va_start(va, nargs);
 	for (i = 0; i < nargs; ++i)
-		WRITE_ONCE(uc.args[i], va_arg(va, uint64_t));
+		WRITE_ONCE(uc->args[i], va_arg(va, uint64_t));
 	va_end(va);
 
-	ucall_arch_do_ucall((vm_vaddr_t)&uc);
+	/*
+	 * When using the ucall pool implementation the @hva member of the ucall
+	 * structs in the pool has been initialized to the hva of the ucall
+	 * object.
+	 */
+	if (use_ucall_pool)
+		ucall_arch_do_ucall((vm_vaddr_t)uc->hva);
+	else
+		ucall_arch_do_ucall((vm_vaddr_t)uc);
+
+	ucall_free(uc);
 }
 
 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
tools/testing/selftests/kvm/lib/x86_64/ucall.c +5 -1
index ead9946399ab..07c1bc41fa5c 100644
@@ -30,7 +30,11 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 		struct kvm_regs regs;
 
 		vcpu_regs_get(vcpu, &regs);
-		return addr_gva2hva(vcpu->vm, regs.rdi);
+
+		if (vcpu->vm->use_ucall_pool)
+			return (void *)regs.rdi;
+		else
+			return addr_gva2hva(vcpu->vm, regs.rdi);
 	}
 	return NULL;
 }
Signature · 1 line
2.37.1.559.g78731f0fdb-goog

Thanks,
drew

#17 lore raw

Re: [V3 10/11] KVM: selftests: Add ucall pool based implementation

Quoted 23 lines · Andrew Jones

tools/testing/selftests/kvm/lib/aarch64/ucall.c void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 84

Quoted 10 lines · Peter Gonda
tools/testing/selftests/kvm/lib/aarch64/ucall.c +1 -1
index 132c0e98bf49..ee70531e8e51 100644
@@ -81,12 +81,16 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 
 	if (run->exit_reason == KVM_EXIT_MMIO &&
 	    run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
-		vm_vaddr_t gva;
+		uint64_t ucall_addr;

Why change this vm_vaddr_t to a uint64_t? We shouldn't, because...

tools/testing/selftests/kvm/lib/aarch64/ucall.c void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 88

Quoted 6 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +1 -2
 		TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
 			    "Unexpected ucall exit mmio address access");
-		memcpy(&gva, run->mmio.data, sizeof(gva));
-		return addr_gva2hva(vcpu->vm, gva);
+		memcpy(&ucall_addr, run->mmio.data, sizeof(ucall_addr));

...here we assume it's a vm_vaddr_t and only...

Quoted 5 lines

tools/testing/selftests/kvm/lib/aarch64/ucall.c void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 91

Quoted 3 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +3 -0
+
+		if (vcpu->vm->use_ucall_pool)
+			return (void *)ucall_addr;

...here do we know otherwise. So only here should be any casting.

It technically should be a union, because if sizeof(vm_vaddr_t) < sizeof(void *) then declaring it as a vm_addr_t will do the wrong thing. But then it's possible that this could read too many bytes and inducs failure. So I guess what we really need is a "static_assert(sizeof(vm_vaddr_t) == sizeof(void *))".

But why is "use_ucall_pool" optional? Unless there's a use case that fundamentally conflicts with the pool approach, let's make the pool approach the _only_ approach. IIRC, ARM's implementation isn't thread safe, i.e. there's at least one other use case that _needs_ the pool implementation.

By supporting both, we are signing ourselves up for extra maintenance and pain, e.g. inevitably we'll break whichever option isn't the default and not notice for quite some time.

#18 lore raw

Re: [V3 10/11] KVM: selftests: Add ucall pool based implementation

Quoted 30 lines · Sean Christopherson
Quoted 23 lines · Andrew Jones

tools/testing/selftests/kvm/lib/aarch64/ucall.c void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 84

Quoted 10 lines · Peter Gonda
tools/testing/selftests/kvm/lib/aarch64/ucall.c +1 -1
index 132c0e98bf49..ee70531e8e51 100644
@@ -81,12 +81,16 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 
 	if (run->exit_reason == KVM_EXIT_MMIO &&
 	    run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
-		vm_vaddr_t gva;
+		uint64_t ucall_addr;

Why change this vm_vaddr_t to a uint64_t? We shouldn't, because...

tools/testing/selftests/kvm/lib/aarch64/ucall.c void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 88

Quoted 6 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +1 -2
 		TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
 			    "Unexpected ucall exit mmio address access");
-		memcpy(&gva, run->mmio.data, sizeof(gva));
-		return addr_gva2hva(vcpu->vm, gva);
+		memcpy(&ucall_addr, run->mmio.data, sizeof(ucall_addr));

...here we assume it's a vm_vaddr_t and only...

Quoted 5 lines

tools/testing/selftests/kvm/lib/aarch64/ucall.c void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 91

Quoted 3 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +3 -0
+
+		if (vcpu->vm->use_ucall_pool)
+			return (void *)ucall_addr;

...here do we know otherwise. So only here should be any casting.

Quoted 4 lines

It technically should be a union, because if sizeof(vm_vaddr_t) < sizeof(void *) then declaring it as a vm_addr_t will do the wrong thing. But then it's possible that this could read too many bytes and inducs failure. So I guess what we really need is a "static_assert(sizeof(vm_vaddr_t) == sizeof(void *))".

ack

Quoted 5 lines

But why is "use_ucall_pool" optional? Unless there's a use case that fundamentally conflicts with the pool approach, let's make the pool approach the _only_ approach. IIRC, ARM's implementation isn't thread safe, i.e. there's at least one other use case that _needs_ the pool implementation.

Really? The ucall structure is on the vcpu's stack like the other architectures. Ah, you're probably thinking about the shared address used to exit to userspace. The address doesn't matter as long as no VM maps it, but, yes, a multi-VM test where the VMs have different maps could end up breaking ucalls for one or more VMs. It wouldn't be hard to make that address per-VM, though, if ever necessary.

Quoted 4 lines

By supporting both, we are signing ourselves up for extra maintenance and pain, e.g. inevitably we'll break whichever option isn't the default and not notice for quite some time.

uc pools are currently limited to a single VM. That could be changed, but at the expense of even more code to maintain. The simple uc implementation is, well, simple, and also supports multiple VMs. I'd prefer we keep that one and keep it as the default.

Thanks,
drew

#19 lore raw

Re: [V3 10/11] KVM: selftests: Add ucall pool based implementation

Quoted 11 lines · Andrew Jones
Quoted 4 lines · Sean Christopherson

But why is "use_ucall_pool" optional? Unless there's a use case that fundamentally conflicts with the pool approach, let's make the pool approach the _only_ approach. IIRC, ARM's implementation isn't thread safe, i.e. there's at least one other use case that _needs_ the pool implementation.

Really? The ucall structure is on the vcpu's stack like the other architectures. Ah, you're probably thinking about the shared address used to exit to userspace. The address doesn't matter as long as no VM maps it, but, yes, a multi-VM test where the VMs have different maps could end up breaking ucalls for one or more VMs.

Yah, that's what I was thinking of.

Quoted 9 lines

It wouldn't be hard to make that address per-VM, though, if ever necessary.

Quoted 4 lines

By supporting both, we are signing ourselves up for extra maintenance and pain, e.g. inevitably we'll break whichever option isn't the default and not notice for quite some time.

uc pools are currently limited to a single VM. That could be changed, but at the expense of even more code to maintain.

Unless I'm missing something, it's actually less code. "globals" that are sync'd to the guest aren't truly global, each VM has a separate physical page that is a copy of the global, hence the need for sync_global_to_guest().

And FWIW, the code is actually "safe" in practice because I doubt any test spawns multiple VMs in parallel, i.e. the host might get confused over the value of ucall_pool, but guests won't stomp on each other so long as they're created serially. Ditto for ARM's ucall_exit_mmio_addr.

To make this truly thread safe, we just need a way to atomically sync the pointer to the guest, and that's easy enough to add.

With that out of the way, all of the conditional "use pool" code goes away, which IMO simplifies things overall. Using a pool versus stack memory isn't _that_ much more complicated, and we _know_ the stack approach doesn't work for all VM types.

Indeed, this partial conversion is:

  7 files changed, 131 insertions(+), 18 deletions(-)

versus a full conversion:

  6 files changed, 89 insertions(+), 20 deletions(-)

And simplification is only a secondary concern, what I'm really worried about is things bitrotting and then some poor sod having to wade through unrelated issues because somebody else broke the pool implementation and didn't notice.

Argh, case in point, none of the x86 (or s390 or RISC-V) tests do ucall_init(), which would have been a lurking bug if any of them tried to switch to the pool.

Argh + case in point #2, this code is broken, and that bug may have sat unnoticed due to only the esoteric SEV test using the pool.

-static inline size_t uc_pool_idx(struct ucall *uc) +static noinline void ucall_free(struct ucall *uc)
{
- return uc->hva - ucall_pool->ucalls;
-}
-
-static void ucall_free(struct ucall *uc) -{

-       clear_bit(uc_pool_idx(uc), ucall_pool->in_use);
+       /* Beware, here be pointer arithmetic.  */
+       clear_bit(uc - ucall_pool->ucalls, ucall_pool->in_use);

}

So my very strong vote is to fully switch to a single implementation. That way our code either works or it doesn't, i.e. we notice very quickly if it breaks.

Peter, if I can convince Drew of One Pool To Rule Them All, can you slot in the attached patches and slightly rework the ordering so that all SEV patches are at the end? E.g. something like:

KVM: selftests: Automatically do init_ucall() for non-barebones VMs
KVM: selftests: move vm_phy_pages_alloc() earlier in file
KVM: selftests: sparsebit: add const where appropriate
KVM: selftests: add hooks for managing encrypted guest memory
KVM: selftests: handle encryption bits in page tables
KVM: selftests: add support for encrypted vm_vaddr_* allocations
KVM: selftests: Consolidate common code for popuplating
KVM: selftests: Consolidate boilerplate code in get_ucall()
tools: Add atomic_test_and_set_bit()
KVM: selftests: Add macro to atomically sync per-VM "global" pointers
KVM: selftests: Add ucall pool based implementation
KVM: selftests: add library for creating/interacting with SEV guests
KVM: selftests: Add simple sev vm testing

The patches are tested on x86 and compile tested on arm. I can provide more testing if/when necessary. I also haven't addressed any other feedback in the "ucall pool" patch, though I'm guessing much of it no longer applies.

From 9264f40a8cff865e5845d89e8e44e0c3a6fa482a Mon Sep 17 00:00:00 2001
From: Sean Christopherson <[email protected]>
Date: Thu, 18 Aug 2022 15:15:57 -0700
Subject: [PATCH 01/13] KVM: selftests: Automatically do init_ucall() for
non-barebones VMs

Do init_ucall() automatically during VM creation in preparation for making ucall initialization meaningful on all architectures. aarch64 is currently the only arch that needs to do any setup, but that will change in the future by switching to a pool-based implementation (instead of the current stack-based approach).

---
 tools/testing/selftests/kvm/aarch64/arch_timer.c         | 1 -
 tools/testing/selftests/kvm/aarch64/debug-exceptions.c   | 1 -
 tools/testing/selftests/kvm/aarch64/hypercalls.c         | 1 -
 tools/testing/selftests/kvm/aarch64/psci_test.c          | 1 -
 tools/testing/selftests/kvm/aarch64/vgic_init.c          | 2 --
 tools/testing/selftests/kvm/aarch64/vgic_irq.c           | 1 -
 tools/testing/selftests/kvm/dirty_log_test.c             | 2 --
 tools/testing/selftests/kvm/kvm_page_table_test.c        | 1 -
 tools/testing/selftests/kvm/lib/kvm_util.c               | 3 +++
 tools/testing/selftests/kvm/lib/perf_test_util.c         | 2 --
 tools/testing/selftests/kvm/memslot_perf_test.c          | 1 -
 tools/testing/selftests/kvm/rseq_test.c                  | 1 -
 tools/testing/selftests/kvm/steal_time.c                 | 1 -
 tools/testing/selftests/kvm/system_counter_offset_test.c | 1 -
 14 files changed, 3 insertions(+), 16 deletions(-)
tools/testing/selftests/kvm/aarch64/arch_timer.c +0 -1
index 574eb73f0e90..37c0ddebf4db 100644
@@ -375,7 +375,6 @@ static struct kvm_vm *test_vm_create(void)
 	for (i = 0; i < nr_vcpus; i++)
 		vcpu_init_descriptor_tables(vcpus[i]);
 
-	ucall_init(vm, NULL);
 	test_init_timer_irq(vm);
 	gic_fd = vgic_v3_setup(vm, nr_vcpus, 64, GICD_BASE_GPA, GICR_BASE_GPA);
 	__TEST_REQUIRE(gic_fd >= 0, "Failed to create vgic-v3");
tools/testing/selftests/kvm/aarch64/debug-exceptions.c +0 -1
index 2ee35cf9801e..eaf225fd2a4a 100644
@@ -254,7 +254,6 @@ int main(int argc, char *argv[])
 	int stage;
 
 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
-	ucall_init(vm, NULL);
 
 	vm_init_descriptor_tables(vm);
 	vcpu_init_descriptor_tables(vcpu);
tools/testing/selftests/kvm/aarch64/hypercalls.c +0 -1
index a39da3fe4952..3dceecfd1f62 100644
@@ -236,7 +236,6 @@ static struct kvm_vm *test_vm_create(struct kvm_vcpu **vcpu)
 
 	vm = vm_create_with_one_vcpu(vcpu, guest_code);
 
-	ucall_init(vm, NULL);
 	steal_time_init(*vcpu);
 
 	return vm;
tools/testing/selftests/kvm/aarch64/psci_test.c +0 -1
index f7621f6e938e..56278f3df891 100644
@@ -77,7 +77,6 @@ static struct kvm_vm *setup_vm(void *guest_code, struct kvm_vcpu **source,
 	struct kvm_vm *vm;
 
 	vm = vm_create(2);
-	ucall_init(vm, NULL);
 
 	vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &init);
 	init.features[0] |= (1 << KVM_ARM_VCPU_PSCI_0_2);
tools/testing/selftests/kvm/aarch64/vgic_init.c +0 -2
index e05ecb31823f..cc828fb53d8f 100644
@@ -68,8 +68,6 @@ static void guest_code(void)
 /* we don't want to assert on run execution, hence that helper */
 static int run_vcpu(struct kvm_vcpu *vcpu)
 {
-	ucall_init(vcpu->vm, NULL);
-
 	return __vcpu_run(vcpu) ? -errno : 0;
 }
 
tools/testing/selftests/kvm/aarch64/vgic_irq.c +0 -1
index 17417220a083..d1817f852daf 100644
@@ -756,7 +756,6 @@ static void test_vgic(uint32_t nr_irqs, bool level_sensitive, bool eoi_split)
 	print_args(&args);
 
 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
-	ucall_init(vm, NULL);
 
 	vm_init_descriptor_tables(vm);
 	vcpu_init_descriptor_tables(vcpu);
tools/testing/selftests/kvm/dirty_log_test.c +0 -2
index 9c883c94d478..583b46250d07 100644
@@ -754,8 +754,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	/* Cache the HVA pointer of the region */
 	host_test_mem = addr_gpa2hva(vm, (vm_paddr_t)guest_test_phys_mem);
 
-	ucall_init(vm, NULL);
-
 	/* Export the shared variables to the guest */
 	sync_global_to_guest(vm, host_page_size);
 	sync_global_to_guest(vm, guest_page_size);
tools/testing/selftests/kvm/kvm_page_table_test.c +0 -1
index f42c6ac6d71d..20533c48ba3d 100644
@@ -289,7 +289,6 @@ static struct kvm_vm *pre_init_before_test(enum vm_guest_mode mode, void *arg)
 	host_test_mem = addr_gpa2hva(vm, (vm_paddr_t)guest_test_phys_mem);
 
 	/* Export shared structure test_args to guest */
-	ucall_init(vm, NULL);
 	sync_global_to_guest(vm, test_args);
 
 	ret = sem_init(&test_stage_updated, 0, 0);
tools/testing/selftests/kvm/lib/kvm_util.c +3 -0
index 9889fe0d8919..ea679987f038 100644
@@ -301,6 +301,9 @@ struct kvm_vm *__vm_create(enum vm_guest_mode mode, uint32_t nr_runnable_vcpus,
 #ifdef __x86_64__
 	vm_create_irqchip(vm);
 #endif
+
+	ucall_init(vm, NULL);
+
 	return vm;
 }
 
tools/testing/selftests/kvm/lib/perf_test_util.c +0 -2
index 9618b37c66f7..5161fa68cdf3 100644
@@ -209,8 +209,6 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int nr_vcpus,
 		perf_test_setup_nested(vm, nr_vcpus, vcpus);
 	}
 
-	ucall_init(vm, NULL);
-
 	/* Export the shared variables to the guest. */
 	sync_global_to_guest(vm, perf_test_args);
 
tools/testing/selftests/kvm/memslot_perf_test.c +0 -1
index 44995446d942..4ed5acd74278 100644
@@ -277,7 +277,6 @@ static bool prepare_vm(struct vm_data *data, int nslots, uint64_t *maxslots,
 	TEST_ASSERT(data->hva_slots, "malloc() fail");
 
 	data->vm = __vm_create_with_one_vcpu(&data->vcpu, mempages, guest_code);
-	ucall_init(data->vm, NULL);
 
 	pr_info_v("Adding slots 1..%i, each slot with %"PRIu64" pages + %"PRIu64" extra pages last\n",
 		max_mem_slots - 1, data->pages_per_slot, rempages);
tools/testing/selftests/kvm/rseq_test.c +0 -1
index fac248a43666..8dc745effb5e 100644
@@ -224,7 +224,6 @@ int main(int argc, char *argv[])
 	 * CPU affinity.
 	 */
 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
-	ucall_init(vm, NULL);
 
 	pthread_create(&migration_thread, NULL, migration_worker,
 		       (void *)(unsigned long)gettid());
tools/testing/selftests/kvm/steal_time.c +0 -1
index db8967f1a17b..c87f38712073 100644
@@ -266,7 +266,6 @@ int main(int ac, char **av)
 	gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE * NR_VCPUS);
 	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, gpages, 0);
 	virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, gpages);
-	ucall_init(vm, NULL);
 
 	TEST_REQUIRE(is_steal_time_supported(vcpus[0]));
 
tools/testing/selftests/kvm/system_counter_offset_test.c +0 -1
index 1c274933912b..7f5b330b6a1b 100644
@@ -121,7 +121,6 @@ int main(void)
 
 	vm = vm_create_with_one_vcpu(&vcpu, guest_main);
 	check_preconditions(vcpu);
-	ucall_init(vm, NULL);
 
 	enter_guest(vcpu);
 	kvm_vm_free(vm);
Signature · 1 line
2.37.1.595.g718a3a8f04-goog

From bbc527a48fe0481495d6a0a562815e3d33fb655b Mon Sep 17 00:00:00 2001
From: Sean Christopherson <[email protected]>
Date: Thu, 18 Aug 2022 15:22:55 -0700
Subject: [PATCH 10/13] KVM: selftests: Add macro to atomically sync per-VM
"global" pointers

Add atomic_sync_global_pointer_to_guest() to allow sync'ing "global" pointers that hold per-VM values, i.e. technically need to be handled in a thread-safe manner.

Use the new macro to fix a mostly-theoretical bug where ARM's ucall MMIO setup could result in different VMs stomping on each other.

---
 .../testing/selftests/kvm/include/kvm_util_base.h | 15 +++++++++++++++
 tools/testing/selftests/kvm/lib/aarch64/ucall.c   | 15 +++++++++++----
 2 files changed, 26 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +15 -0
index 8ce9e5be70a3..f4a2622db53c 100644
@@ -16,6 +16,7 @@
 #include <linux/kvm.h>
 #include "linux/rbtree.h"
 
+#include <asm/atomic.h>
 
 #include <sys/ioctl.h>
 
@@ -727,6 +728,20 @@ kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
 	memcpy(&(g), _p, sizeof(g));				\
 })
 
+/*
+ * Sync a global pointer to the guest that has a per-VM value, in which case
+ * writes to the host copy of the "global" must be serialized (in case a test
+ * is being truly crazy and spawning multiple VMs concurrently).
+ */
+#define atomic_sync_global_pointer_to_guest(vm, g, val) ({	\
+	typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g));	\
+								\
+	while (cmpxchg(&g, NULL, val))				\
+		;						\
+	memcpy(_p, &(g), sizeof(g));				\
+	WRITE_ONCE(g, NULL);					\
+})
+
 void assert_on_unhandled_exception(struct kvm_vcpu *vcpu);
 
 void vcpu_arch_dump(FILE *stream, struct kvm_vcpu *vcpu,
tools/testing/selftests/kvm/lib/aarch64/ucall.c +11 -4
index 132c0e98bf49..c30a6eacde34 100644
@@ -6,8 +6,17 @@
  */
 #include "kvm_util.h"
 
+/*
+ * This "global" holds different per-VM values, it must not be accessed from
+ * host code except to sync the guest value, and that must be done atomically.
+ */
 static vm_vaddr_t *ucall_exit_mmio_addr;
 
+static void ucall_set_mmio_addr(struct kvm_vm *vm, vm_vaddr_t *val)
+{
+	atomic_sync_global_pointer_to_guest(vm, ucall_exit_mmio_addr, val);
+}
+
 static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa)
 {
 	if (kvm_userspace_memory_region_find(vm, gpa, gpa + 1))
@@ -15,8 +24,7 @@ static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa)
 
 	virt_pg_map(vm, gpa, gpa);
 
-	ucall_exit_mmio_addr = (vm_vaddr_t *)gpa;
-	sync_global_to_guest(vm, ucall_exit_mmio_addr);
+	ucall_set_mmio_addr(vm, (vm_vaddr_t *)gpa);
 
 	return true;
 }
@@ -66,8 +74,7 @@ void ucall_arch_init(struct kvm_vm *vm, void *arg)
 
 void ucall_arch_uninit(struct kvm_vm *vm)
 {
-	ucall_exit_mmio_addr = 0;
-	sync_global_to_guest(vm, ucall_exit_mmio_addr);
+	ucall_set_mmio_addr(vm, (vm_vaddr_t *)NULL);
 }
 
 void ucall_arch_do_ucall(vm_vaddr_t uc)
Signature · 1 line
2.37.1.595.g718a3a8f04-goog

From ba28b353f632bcf8a3b6a8bc4e678c18a3dae7e9 Mon Sep 17 00:00:00 2001
From: Sean Christopherson <[email protected]>
Date: Wed, 10 Aug 2022 08:20:32 -0700
Subject: [PATCH 11/13] KVM: selftests: Add ucall pool based implementation

From: Peter Gonda <[email protected]>

To play nice with guests whose stack memory is encrypted, e.g. AMD SEV, introduce a new "ucall pool" implementation that passes the ucall struct via dedicated memory (which can be mapped shared, a.k.a. as plain text).

Because not all architectures have access to the vCPU index in the guest, use a bitmap with atomic accesses to track which entries in the pool are free/used. A list+lock could also work in theory, but synchronizing the individual pointers to the guest would be a mess.

Note, there's no need to rewalk the bitmap to ensure success. If all vCPUs are simply allocating, success is guaranteed because there are enough entries for all vCPUs. If one or more vCPUs are freeing and then reallocating, success is guaranteed because vCPUs _always_ walk the bitmap from 0=>N; if vCPU frees an entry and then wins a race to re-allocate, then either it will consume the entry it just freed (bit is the first free bit), or the losing vCPU is guaranteed to see the freed bit (winner consumes an earlier bit, which the loser hasn't yet visited).

---
 .../selftests/kvm/include/ucall_common.h      | 13 ++-
 .../testing/selftests/kvm/lib/aarch64/ucall.c |  7 +-
 tools/testing/selftests/kvm/lib/riscv/ucall.c |  2 +-
 tools/testing/selftests/kvm/lib/s390x/ucall.c |  2 +-
 .../testing/selftests/kvm/lib/ucall_common.c  | 83 ++++++++++++++++++-
 .../testing/selftests/kvm/lib/x86_64/ucall.c  |  2 +-
 6 files changed, 89 insertions(+), 20 deletions(-)
tools/testing/selftests/kvm/include/ucall_common.h +5 -8
index 63bfc60be995..002a22e1cd1d 100644
@@ -22,6 +22,9 @@ enum {
 struct ucall {
 	uint64_t cmd;
 	uint64_t args[UCALL_MAX_ARGS];
+
+	/* For ucall pool usage. */
+	struct ucall *hva;
 };
 
 void ucall_arch_init(struct kvm_vm *vm, void *arg);
@@ -32,15 +35,9 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);
 void ucall(uint64_t cmd, int nargs, ...);
 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
 
-static inline void ucall_init(struct kvm_vm *vm, void *arg)
-{
-	ucall_arch_init(vm, arg);
-}
+void ucall_init(struct kvm_vm *vm, void *arg);
 
-static inline void ucall_uninit(struct kvm_vm *vm)
-{
-	ucall_arch_uninit(vm);
-}
+void ucall_uninit(struct kvm_vm *vm);
 
 #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4)	\
 				ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
tools/testing/selftests/kvm/lib/aarch64/ucall.c +2 -5
index c30a6eacde34..dadcebf1c904 100644
@@ -88,12 +88,9 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 
 	if (run->exit_reason == KVM_EXIT_MMIO &&
 	    run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
-		vm_vaddr_t gva;
-
-		TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
+		TEST_ASSERT(run->mmio.is_write && run->mmio.len == sizeof(uint64_t),
 			    "Unexpected ucall exit mmio address access");
-		memcpy(&gva, run->mmio.data, sizeof(gva));
-		return addr_gva2hva(vcpu->vm, gva);
+		return (void *)(*((uint64_t *)run->mmio.data));
 	}
 
 	return NULL;
tools/testing/selftests/kvm/lib/riscv/ucall.c +1 -1
index 37e091d4366e..bef4df251e7e 100644
@@ -59,7 +59,7 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 	    run->riscv_sbi.extension_id == KVM_RISCV_SELFTESTS_SBI_EXT) {
 		switch (run->riscv_sbi.function_id) {
 		case KVM_RISCV_SELFTESTS_SBI_UCALL:
-			return addr_gva2hva(vcpu->vm, run->riscv_sbi.args[0]);
+			return (void *)run->riscv_sbi.args[0];
 		case KVM_RISCV_SELFTESTS_SBI_UNEXP:
 			vcpu_dump(stderr, vcpu, 2);
 			TEST_ASSERT(0, "Unexpected trap taken by guest");
tools/testing/selftests/kvm/lib/s390x/ucall.c +1 -1
index 0f695a031d35..da73cd466296 100644
@@ -30,7 +30,7 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 	    (run->s390_sieic.ipb >> 16) == 0x501) {
 		int reg = run->s390_sieic.ipa & 0xf;
 
-		return addr_gva2hva(vcpu->vm, run->s.regs.gprs[reg]);
+		return (void *)run->s.regs.gprs[reg];
 	}
 	return NULL;
 }
tools/testing/selftests/kvm/lib/ucall_common.c +79 -4

101 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86_64/ucall.c +1 -1
index ead9946399ab..ea6b2e3a8e39 100644
@@ -30,7 +30,7 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
 		struct kvm_regs regs;
 
 		vcpu_regs_get(vcpu, &regs);
-		return addr_gva2hva(vcpu->vm, regs.rdi);
+		return (void *)regs.rdi;
 	}
 	return NULL;
 }
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
#20 lore raw

Re: [V3 10/11] KVM: selftests: Add ucall pool based implementation

Quoted 29 lines · Sean Christopherson
Quoted 11 lines · Andrew Jones
Quoted 4 lines · Sean Christopherson

But why is "use_ucall_pool" optional? Unless there's a use case that fundamentally conflicts with the pool approach, let's make the pool approach the _only_ approach. IIRC, ARM's implementation isn't thread safe, i.e. there's at least one other use case that _needs_ the pool implementation.

Really? The ucall structure is on the vcpu's stack like the other architectures. Ah, you're probably thinking about the shared address used to exit to userspace. The address doesn't matter as long as no VM maps it, but, yes, a multi-VM test where the VMs have different maps could end up breaking ucalls for one or more VMs.

Yah, that's what I was thinking of.

Quoted 9 lines

It wouldn't be hard to make that address per-VM, though, if ever necessary.

Quoted 4 lines

By supporting both, we are signing ourselves up for extra maintenance and pain, e.g. inevitably we'll break whichever option isn't the default and not notice for quite some time.

uc pools are currently limited to a single VM. That could be changed, but at the expense of even more code to maintain.

Unless I'm missing something, it's actually less code. "globals" that are sync'd to the guest aren't truly global, each VM has a separate physical page that is a copy of the global, hence the need for sync_global_to_guest().

Quoted 7 lines

And FWIW, the code is actually "safe" in practice because I doubt any test spawns multiple VMs in parallel, i.e. the host might get confused over the value of ucall_pool, but guests won't stomp on each other so long as they're created serially. Ditto for ARM's ucall_exit_mmio_addr.

To make this truly thread safe, we just need a way to atomically sync the pointer to the guest, and that's easy enough to add.

I like that.

Quoted 25 lines

With that out of the way, all of the conditional "use pool" code goes away, which IMO simplifies things overall. Using a pool versus stack memory isn't _that_ much more complicated, and we _know_ the stack approach doesn't work for all VM types.

Indeed, this partial conversion is:

  7 files changed, 131 insertions(+), 18 deletions(-)

versus a full conversion:

  6 files changed, 89 insertions(+), 20 deletions(-)

And simplification is only a secondary concern, what I'm really worried about is things bitrotting and then some poor sod having to wade through unrelated issues because somebody else broke the pool implementation and didn't notice.

Argh, case in point, none of the x86 (or s390 or RISC-V) tests do ucall_init(), which would have been a lurking bug if any of them tried to switch to the pool.

Argh + case in point #2, this code is broken, and that bug may have sat unnoticed due to only the esoteric SEV test using the pool.

(quoted diff) +1 -1
-static inline size_t uc_pool_idx(struct ucall *uc)
+static noinline void ucall_free(struct ucall *uc)

Review

Quoted 10 lines
(quoted diff) +2 -6
 {
-       return uc->hva - ucall_pool->ucalls;
-}
-
-static void ucall_free(struct ucall *uc)
-{
-       clear_bit(uc_pool_idx(uc), ucall_pool->in_use);
+       /* Beware, here be pointer arithmetic.  */
+       clear_bit(uc - ucall_pool->ucalls, ucall_pool->in_use);
 }

Dropping the only once-used uc_pool_idx() and adding the comment looks better, but I don't think there was a bug because uc == uc->hva.

Quoted 6 lines

So my very strong vote is to fully switch to a single implementation. That way our code either works or it doesn't, i.e. we notice very quickly if it breaks.

Peter, if I can convince Drew of One Pool To Rule Them All, can you slot in the

There are other hills for me to stand on, so I won't insist on this one.

Quoted 17 lines

attached patches and slightly rework the ordering so that all SEV patches are at the end? E.g. something like:

KVM: selftests: Automatically do init_ucall() for non-barebones VMs
KVM: selftests: move vm_phy_pages_alloc() earlier in file
KVM: selftests: sparsebit: add const where appropriate
KVM: selftests: add hooks for managing encrypted guest memory
KVM: selftests: handle encryption bits in page tables
KVM: selftests: add support for encrypted vm_vaddr_* allocations
KVM: selftests: Consolidate common code for popuplating
KVM: selftests: Consolidate boilerplate code in get_ucall()
tools: Add atomic_test_and_set_bit()
KVM: selftests: Add macro to atomically sync per-VM "global" pointers
KVM: selftests: Add ucall pool based implementation
KVM: selftests: add library for creating/interacting with SEV guests
KVM: selftests: Add simple sev vm testing

Quoted 3 lines

The patches are tested on x86 and compile tested on arm. I can provide more testing if/when necessary. I also haven't addressed any other feedback in the "ucall pool" patch, though I'm guessing much of it no longer applies.

I skimmed the patches but don't know how to comment on attachments, so I'll add the two things that popped to mind here.

1) Doing ucall_init() at VM create time may be too early for Arm. Arm probes for an unmapped address for the MMIO address it needs, so it's best to do that after all mapping.

2) We'll need to change the sanity checks in Arm's get_ucall() to not check that the MMIO address == ucall_exit_mmio_addr since there's no guarantee that the exiting guest's address matches whatever is lingering in the host's version. We can either drop the sanity check altogether or try to come up with something else.

Thanks,
drew

#21 lore raw

Re: [V3 10/11] KVM: selftests: Add ucall pool based implementation

Quoted 4 lines · Andrew Jones
Quoted 1 line · Sean Christopherson

On Thu, Aug 18, 2022, Andrew Jones wrote:

Dropping the only once-used uc_pool_idx() and adding the comment looks better, but I don't think there was a bug because uc == uc->hva.

uc == uc->hva for the host, but not for the guest. From the guest's perspective, "hva" is an opaque pointer that has no direct relation to "uc".

Quoted 3 lines

1) Doing ucall_init() at VM create time may be too early for Arm. Arm probes for an unmapped address for the MMIO address it needs, so it's best to do that after all mapping.

Argh. I really hate the MMIO probing. Is there really no arbitrary address that selftests can carve out and simply say "thou shalt not create a memslot here"?

Or can we just say that it's always immediate after memslot0? That would allow us to delete the searching code in ARM's ucall_arch_init().

Quoted 5 lines

2) We'll need to change the sanity checks in Arm's get_ucall() to not check that the MMIO address == ucall_exit_mmio_addr since there's no guarantee that the exiting guest's address matches whatever is lingering in the host's version. We can either drop the sanity check altogether or try to come up with something else.

Ah, right. This one at least is easy to handle. If the address is per-VM, just track the address. If it's hardcoded (as a fix for #1) then, it's even simpler.

tools/testing/selftests/kvm/include/kvm_util_base.h +2 -1
index 8623c1568f97..74167540815b 100644
@@ -94,7 +94,8 @@ struct kvm_vm {
        vm_paddr_t pgd;
        vm_vaddr_t gdt;
        vm_vaddr_t tss;
-       vm_vaddr_t idt;
+       vm_paddr_t pgd;
+       vm_vaddr_t ucall_addr;
        vm_vaddr_t handlers;
        uint32_t dirty_ring_size;
        struct vm_memcrypt memcrypt;
tools/testing/selftests/kvm/lib/aarch64/ucall.c +3 -1
index b5d904f074b6..7f1d50dab0df 100644
@@ -14,6 +14,8 @@ static vm_vaddr_t *ucall_exit_mmio_addr;
 
 static void ucall_set_mmio_addr(struct kvm_vm *vm, vm_vaddr_t val)
 {
+       vm->ucall_addr = val;
+
        atomic_sync_global_pointer_to_guest(vm, ucall_exit_mmio_addr, val);
 }
 
@@ -87,7 +89,7 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
        struct kvm_run *run = vcpu->run;
 
        if (run->exit_reason == KVM_EXIT_MMIO &&
-           run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
+           run->mmio.phys_addr == vcpu->vm->ucall_addr) {
                TEST_ASSERT(run->mmio.is_write && run->mmio.len == sizeof(uint64_t),
                            "Unexpected ucall exit mmio address access");
                return (void *)(*((uint64_t *)run->mmio.data));
#22 lore raw

Re: [V3 10/11] KVM: selftests: Add ucall pool based implementation

Quoted 2 lines · Sean Christopherson

Or can we just say that it's always immediate after memslot0? That would allow us to delete the searching code in ARM's ucall_arch_init().

I have this coded up, will test on x86 and arm64 and send out a series (essentially all of the non-SEV bits in this series).

Prescribing an MMIO address from __vm_create() has a some nice side effects.

1) KVM treats writes to read-only memslots as MMIO, so a future cleanup would be to have __vm_create() create a memslot for the MMIO range to prevent silently clobbering the address. I'll leave this for later because selftests currently assumes they can use all memslots except memslot0.

2) It will simplify wwitching x86 and RISC-V to a common MMIO implementation, if we ever want to do that. I.e. have common code for everything except s390.

#23 lore raw

Re: [V3 10/11] KVM: selftests: Add ucall pool based implementation

In reply to Peter Gonda
Quoted 29 lines · Peter Gonda

...

tools/testing/selftests/kvm/lib/ucall_common.c +28 -0
+
+static struct ucall *ucall_alloc(void)
+{
+       struct ucall *uc = NULL;
+       int i;
+
+       if (!use_ucall_pool)
+               goto out;
+
+       for (i = 0; i < KVM_MAX_VCPUS; ++i) {
+               if (!atomic_test_and_set_bit(i, ucall_pool->in_use)) {
+                       uc = &ucall_pool->ucalls[i];
+                       memset(uc->args, 0, sizeof(uc->args));
+                       break;
+               }
+       }
+out:
+       return uc;
+}
+
+static inline size_t uc_pool_idx(struct ucall *uc)
+{
+       return uc->hva - ucall_pool->ucalls;
+}
+
+static void ucall_free(struct ucall *uc)
+{
+       if (!use_ucall_pool)

tools/testing/selftests/kvm/lib/ucall_common.c line 92

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +6 -1
+               return;
+
+       clear_bit(uc_pool_idx(uc), ucall_pool->in_use);
+}
 
 void ucall(uint64_t cmd, int nargs, ...)
 {
-       struct ucall uc = {};
+       struct ucall *uc;
+       struct ucall tmp = {};

This steps seems to result in generating instructions that need SSE support on x86:
struct ucall tmp = {};
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x50(%rsp)

This initialization will need proper compilation flags to generate instructions according to VM configuration.

Quoted 20 lines
        va_list va;
        int i;
tools/testing/selftests/kvm/lib/ucall_common.c +6 -3
-       WRITE_ONCE(uc.cmd, cmd);
+       uc = ucall_alloc();
+       if (!uc)
+               uc = &tmp;
+
+       WRITE_ONCE(uc->cmd, cmd);
 
        nargs = min(nargs, UCALL_MAX_ARGS);
 
        va_start(va, nargs);
        for (i = 0; i < nargs; ++i)
-               WRITE_ONCE(uc.args[i], va_arg(va, uint64_t));
+               WRITE_ONCE(uc->args[i], va_arg(va, uint64_t));
        va_end(va);
 
-       ucall_arch_do_ucall((vm_vaddr_t)&uc);
...
#24 lore raw

Re: [V3 10/11] KVM: selftests: Add ucall pool based implementation

Quoted 9 lines · Vishal Annapurve

tools/testing/selftests/kvm/lib/ucall_common.c line 92

Quoted 5 lines · Peter Gonda

void ucall(uint64_t cmd, int nargs, ...)
{

tools/testing/selftests/kvm/lib/ucall_common.c +2 -1
-       struct ucall uc = {};
+       struct ucall *uc;
+       struct ucall tmp = {};

This steps seems to result in generating instructions that need SSE support on x86:

Quoted 8 lines
struct ucall tmp = {};
   movaps %xmm0,0x20(%rsp)
   movaps %xmm0,0x30(%rsp)
   movaps %xmm0,0x40(%rsp)
   movaps %xmm0,0x50(%rsp)

This initialization will need proper compilation flags to generate instructions according to VM configuration.

Can you be more specific as to why generating SSE instructiions is problematic? The compiler emitting fancy instructions for struct initialization is not out of the ordinary.

#25 lore raw

Re: [V3 10/11] KVM: selftests: Add ucall pool based implementation

Quoted 17 lines · Sean Christopherson
Quoted 15 lines · Vishal Annapurve

tools/testing/selftests/kvm/lib/ucall_common.c line 92

Quoted 5 lines · Peter Gonda

void ucall(uint64_t cmd, int nargs, ...)
{

tools/testing/selftests/kvm/lib/ucall_common.c +2 -1
-       struct ucall uc = {};
+       struct ucall *uc;
+       struct ucall tmp = {};

This steps seems to result in generating instructions that need SSE support on x86:

struct ucall tmp = {};
   movaps %xmm0,0x20(%rsp)
   movaps %xmm0,0x30(%rsp)
   movaps %xmm0,0x40(%rsp)
   movaps %xmm0,0x50(%rsp)
Quoted 6 lines
Quoted 2 lines

This initialization will need proper compilation flags to generate instructions according to VM configuration.

Can you be more specific as to why generating SSE instructiions is problematic? The compiler emitting fancy instructions for struct initialization is not out of the ordinary.

When executing these instructions, I was hitting #GP in the guest VM. CR4/CPUID settings seem to allow a VM to execute instructions that need SSE support enabled (which I was doubting earlier). After some more digging, it looks like the compiler is not able to ensure that operands for such instructions are 16 byte aligned as required by the hardware. There is a bug discussing this same issue:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838.

Adding -mstackrealign seems to help avoid running into #GP and things seem to work normally. Though I am not sure if SSE instruction execution is reliable from guest VM within selftests and so was suggesting usage of "-mno-sse" or equivalent flags.

#26 lore raw

PATCH v3 11/11 KVM: selftests: Add simple sev vm testing

In reply to Peter Gonda

A very simple of booting SEV guests that checks related CPUID bits. This is a stripped down version of "[PATCH v2 08/13] KVM: selftests: add SEV boot tests" from Michael but much simpler.

---
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../selftests/kvm/include/x86_64/sev.h        |   3 +
 tools/testing/selftests/kvm/lib/x86_64/sev.c  |   2 -
 .../selftests/kvm/x86_64/sev_all_boot_test.c  | 131 ++++++++++++++++++
 5 files changed, 136 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
index d625a3f83780..ca57969a0923 100644
@@ -33,6 +33,7 @@
 /x86_64/pmu_event_filter_test
 /x86_64/set_boot_cpu_id
 /x86_64/set_sregs_test
+/x86_64/sev_all_boot_test
 /x86_64/sev_migrate_tests
 /x86_64/smm_test
 /x86_64/state_test
tools/testing/selftests/kvm/Makefile +1 -0
index b247c4b595af..73b083f93b46 100644
@@ -122,6 +122,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/tsc_msrs_test
 TEST_GEN_PROGS_x86_64 += x86_64/vmx_pmu_caps_test
 TEST_GEN_PROGS_x86_64 += x86_64/xen_shinfo_test
 TEST_GEN_PROGS_x86_64 += x86_64/xen_vmcall_test
+TEST_GEN_PROGS_x86_64 += x86_64/sev_all_boot_test
 TEST_GEN_PROGS_x86_64 += x86_64/sev_migrate_tests
 TEST_GEN_PROGS_x86_64 += x86_64/amx_test
 TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test
tools/testing/selftests/kvm/include/x86_64/sev.h +3 -0
index 2f7f7c741b12..b6552ea1c716 100644
@@ -22,6 +22,9 @@
 #define SEV_POLICY_NO_DBG	(1UL << 0)
 #define SEV_POLICY_ES		(1UL << 2)
 
+#define CPUID_MEM_ENC_LEAF 0x8000001f
+#define CPUID_EBX_CBIT_MASK 0x3f
+
 enum {
 	SEV_GSTATE_UNINIT = 0,
 	SEV_GSTATE_LUPDATE,
tools/testing/selftests/kvm/lib/x86_64/sev.c +0 -2
index 3abcf50c0b5d..8f9f55c685a7 100644
@@ -13,8 +13,6 @@
 #include "sev.h"
 
 #define PAGE_SHIFT		12
-#define CPUID_MEM_ENC_LEAF 0x8000001f
-#define CPUID_EBX_CBIT_MASK 0x3f
 
 struct sev_vm {
 	struct kvm_vm *vm;
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +131 -0

131 lines not on this page · raw message

Signature · 1 line
2.37.1.559.g78731f0fdb-goog
#27 lore raw

Re: [V3 11/11] KVM: selftests: Add simple sev vm testing

tools/testing/selftests/kvm/include/x86_64/sev.h line 26

Quoted 10 lines · Peter Gonda
tools/testing/selftests/kvm/include/x86_64/sev.h +2 -0
index 2f7f7c741b12..b6552ea1c716 100644
@@ -22,6 +22,9 @@
 #define SEV_POLICY_NO_DBG	(1UL << 0)
 #define SEV_POLICY_ES		(1UL << 2)
 
+#define CPUID_MEM_ENC_LEAF 0x8000001f
+#define CPUID_EBX_CBIT_MASK 0x3f

Ha! I was going to say "put these in processor.h", but I have an even better idea. I'll try to a series posted tomorrow (compile tested only at this point), but what I'm hoping to do is to allow automagic retrieval of multi-bit CPUID properties, a la the existing this_cpu_has() stuff.

E.g.

	#define X86_PROPERTY_CBIT_LOCATION		KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 0, 5)

and then

	sev->enc_bit = this_cpu_property(X86_PROPERTY_CBIT_LOCATION);

LOL, now I see that the defines in sev.c were introduced back in patch 08. That's probably fine for your submission so as not to take a dependency on the "property" idea. This patch doesn't need to move the CPUID_* defines because it can use this_cpu_has(X86_FEATURE_SEV) and avoid referencing CPUID_MEM_ENC_LEAF.

tools/testing/selftests/kvm/lib/x86_64/sev.c line 15

Quoted 11 lines
tools/testing/selftests/kvm/include/x86_64/sev.h +0 -0
 enum {
 	SEV_GSTATE_UNINIT = 0,
 	SEV_GSTATE_LUPDATE,
tools/testing/selftests/kvm/lib/x86_64/sev.c +0 -0
index 3abcf50c0b5d..8f9f55c685a7 100644
@@ -13,8 +13,6 @@
 #include "sev.h"
 
 #define PAGE_SHIFT		12

Already defined in processor.h

Quoted 23 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c +0 -2
-#define CPUID_MEM_ENC_LEAF 0x8000001f
-#define CPUID_EBX_CBIT_MASK 0x3f
 
 struct sev_vm {
 	struct kvm_vm *vm;
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +12 -0
new file mode 100644
index 000000000000..b319d18bdb60
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Basic SEV boot tests.
+ *
+ * Copyright (C) 2021 Advanced Micro Devices
+ */
+#define _GNU_SOURCE /* for program_invocation_short_name */
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>

tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 22

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +10 -0
+
+#include "test_util.h"
+
+#include "kvm_util.h"
+#include "processor.h"
+#include "svm_util.h"
+#include "linux/psp-sev.h"
+#include "sev.h"
+
+#define VCPU_ID			2

Nooooooo. Unless there is a really, REALLY good reason this needs to be '2', just pass '0' as a literal to vm_vcpu_add() and delete VCPU_ID.

Quoted 27 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +27 -0
+#define PAGE_STRIDE		32
+
+#define SHARED_PAGES		8192
+#define SHARED_VADDR_MIN	0x1000000
+
+#define PRIVATE_PAGES		2048
+#define PRIVATE_VADDR_MIN	(SHARED_VADDR_MIN + SHARED_PAGES * PAGE_SIZE)
+
+#define TOTAL_PAGES		(512 + SHARED_PAGES + PRIVATE_PAGES)
+
+#define NR_SYNCS 1
+
+static void guest_run_loop(struct kvm_vcpu *vcpu)
+{
+	struct ucall uc;
+	int i;
+
+	for (i = 0; i <= NR_SYNCS; ++i) {
+		vcpu_run(vcpu);
+		switch (get_ucall(vcpu, &uc)) {
+		case UCALL_SYNC:
+			continue;
+		case UCALL_DONE:
+			return;
+		case UCALL_ABORT:
+			TEST_ASSERT(false, "%s at %s:%ld\n\tvalues: %#lx, %#lx",
+				    (const char *)uc.args[0], __FILE__,

tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 59

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +10 -0
+				    uc.args[1], uc.args[2], uc.args[3]);
+		default:
+			TEST_ASSERT(
+				false, "Unexpected exit: %s",
+				exit_reason_str(vcpu->run->exit_reason));
+		}
+	}
+}
+
+static void __attribute__((__flatten__)) guest_sev_code(void)

Is __flatten__ strictly necessary? I don't see this being copied over anything that would require it to be a contiguous chunk.

tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 67

Quoted 8 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +8 -0
+{
+	uint32_t eax, ebx, ecx, edx;
+	uint64_t sev_status;
+
+	GUEST_SYNC(1);
+
+	cpuid(CPUID_MEM_ENC_LEAF, &eax, &ebx, &ecx, &edx);
+	GUEST_ASSERT(eax & (1 << 1));
	GUEST_ASSERT(this_cpu_has(X86_FEATURE_SEV));
Quoted 6 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +6 -0
+
+	sev_status = rdmsr(MSR_AMD64_SEV);
+	GUEST_ASSERT((sev_status & 0x1) == 1);
+
+	GUEST_DONE();
+}

tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 83

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +10 -0
+
+static struct sev_vm *setup_test_common(void *guest_code, uint64_t policy,
+					struct kvm_vcpu **vcpu)
+{
+	uint8_t measurement[512];
+	struct sev_vm *sev;
+	struct kvm_vm *vm;
+	int i;
+
+	sev = sev_vm_create(policy, TOTAL_PAGES);

TEST_ASSERT(sev, ...) so that this doesn't silently "pass"?

Quoted 21 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +21 -0
+	if (!sev)
+		return NULL;
+	vm = sev_get_vm(sev);
+
+	/* Set up VCPU and initial guest kernel. */
+	*vcpu = vm_vcpu_add(vm, VCPU_ID, guest_code);
+	kvm_vm_elf_load(vm, program_invocation_name);
+
+	/* Allocations/setup done. Encrypt initial guest payload. */
+	sev_vm_launch(sev);
+
+	/* Dump the initial measurement. A test to actually verify it would be nice. */
+	sev_vm_launch_measure(sev, measurement);
+	pr_info("guest measurement: ");
+	for (i = 0; i < 32; ++i)
+		pr_info("%02x", measurement[i]);
+	pr_info("\n");
+
+	sev_vm_launch_finish(sev);
+
+	return sev;

tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 114

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +10 -0
+}
+
+static void test_sev(void *guest_code, uint64_t policy)
+{
+	struct sev_vm *sev;
+	struct kvm_vcpu *vcpu;
+
+	sev = setup_test_common(guest_code, policy, &vcpu);
+	if (!sev)
+		return;

And with an assert above, this return goes away. Or better yet, fold setup_test_common() into test_sev(), there's only the one user of the so called "common" function.

Quoted 20 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +17 -0
+
+	/* Guest is ready to run. Do the tests. */
+	guest_run_loop(vcpu);
+
+	pr_info("guest ran successfully\n");
+
+	sev_vm_free(sev);
+}
+
+int main(int argc, char *argv[])
+{
+	/* SEV tests */
+	test_sev(guest_sev_code, SEV_POLICY_NO_DBG);
+	test_sev(guest_sev_code, 0);
+
+	return 0;
+}
Signature · 1 line
2.37.1.559.g78731f0fdb-goog
#28 lore raw

Re: [V3 11/11] KVM: selftests: Add simple sev vm testing

Quoted 22 lines · Sean Christopherson

tools/testing/selftests/kvm/include/x86_64/sev.h line 26

Quoted 10 lines · Peter Gonda
tools/testing/selftests/kvm/include/x86_64/sev.h +2 -0
index 2f7f7c741b12..b6552ea1c716 100644
@@ -22,6 +22,9 @@
 #define SEV_POLICY_NO_DBG    (1UL << 0)
 #define SEV_POLICY_ES                (1UL << 2)
 
+#define CPUID_MEM_ENC_LEAF 0x8000001f
+#define CPUID_EBX_CBIT_MASK 0x3f

Ha! I was going to say "put these in processor.h", but I have an even better idea. I'll try to a series posted tomorrow (compile tested only at this point), but what I'm hoping to do is to allow automagic retrieval of multi-bit CPUID properties, a la the existing this_cpu_has() stuff.

E.g.

        #define X86_PROPERTY_CBIT_LOCATION              KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 0, 5)
Quoted 8 lines

and then

        sev->enc_bit = this_cpu_property(X86_PROPERTY_CBIT_LOCATION);

LOL, now I see that the defines in sev.c were introduced back in patch 08. That's probably fine for your submission so as not to take a dependency on the "property" idea. This patch doesn't need to move the CPUID_* defines because it can use this_cpu_has(X86_FEATURE_SEV) and avoid referencing CPUID_MEM_ENC_LEAF.

OK I've put these into sev.c instead of the header. I can clean up after you property patch series goes in.

Quoted 11 lines
Quoted 11 lines
tools/testing/selftests/kvm/include/x86_64/sev.h +0 -0
 enum {
      SEV_GSTATE_UNINIT = 0,
      SEV_GSTATE_LUPDATE,
tools/testing/selftests/kvm/lib/x86_64/sev.c +0 -0
index 3abcf50c0b5d..8f9f55c685a7 100644
@@ -13,8 +13,6 @@
 #include "sev.h"
 
Quoted 3 lines

tools/testing/selftests/kvm/lib/x86_64/sev.c line 15

Quoted 1 line
tools/testing/selftests/kvm/lib/x86_64/sev.c +0 -0
 #define PAGE_SHIFT           12

Already defined in processor.h

Removed

Quoted 29 lines
Quoted 29 lines
tools/testing/selftests/kvm/lib/x86_64/sev.c +0 -2
-#define CPUID_MEM_ENC_LEAF 0x8000001f
-#define CPUID_EBX_CBIT_MASK 0x3f
 
 struct sev_vm {
      struct kvm_vm *vm;
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +17 -0
new file mode 100644
index 000000000000..b319d18bdb60
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Basic SEV boot tests.
+ *
+ * Copyright (C) 2021 Advanced Micro Devices
+ */
+#define _GNU_SOURCE /* for program_invocation_short_name */
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+
+#include "test_util.h"
+
+#include "kvm_util.h"
+#include "processor.h"
Quoted 8 lines

tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 22

Quoted 5 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +5 -0
+#include "svm_util.h"
+#include "linux/psp-sev.h"
+#include "sev.h"
+
+#define VCPU_ID                      2

Nooooooo. Unless there is a really, REALLY good reason this needs to be '2', just pass '0' as a literal to vm_vcpu_add() and delete VCPU_ID.

Not needed, removed.

Quoted 33 lines
Quoted 33 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +32 -0
+#define PAGE_STRIDE          32
+
+#define SHARED_PAGES         8192
+#define SHARED_VADDR_MIN     0x1000000
+
+#define PRIVATE_PAGES                2048
+#define PRIVATE_VADDR_MIN    (SHARED_VADDR_MIN + SHARED_PAGES * PAGE_SIZE)
+
+#define TOTAL_PAGES          (512 + SHARED_PAGES + PRIVATE_PAGES)
+
+#define NR_SYNCS 1
+
+static void guest_run_loop(struct kvm_vcpu *vcpu)
+{
+     struct ucall uc;
+     int i;
+
+     for (i = 0; i <= NR_SYNCS; ++i) {
+             vcpu_run(vcpu);
+             switch (get_ucall(vcpu, &uc)) {
+             case UCALL_SYNC:
+                     continue;
+             case UCALL_DONE:
+                     return;
+             case UCALL_ABORT:
+                     TEST_ASSERT(false, "%s at %s:%ld\n\tvalues: %#lx, %#lx",
+                                 (const char *)uc.args[0], __FILE__,
+                                 uc.args[1], uc.args[2], uc.args[3]);
+             default:
+                     TEST_ASSERT(
+                             false, "Unexpected exit: %s",
+                             exit_reason_str(vcpu->run->exit_reason));
Quoted 8 lines

tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 59

Quoted 5 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +5 -0
+             }
+     }
+}
+
+static void __attribute__((__flatten__)) guest_sev_code(void)

Is __flatten__ strictly necessary? I don't see this being copied over anything that would require it to be a contiguous chunk.

Nope, removed.

Quoted 11 lines

tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 67

Quoted 9 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +8 -0
+{
+     uint32_t eax, ebx, ecx, edx;
+     uint64_t sev_status;
+
+     GUEST_SYNC(1);
+
+     cpuid(CPUID_MEM_ENC_LEAF, &eax, &ebx, &ecx, &edx);
+     GUEST_ASSERT(eax & (1 << 1));
        GUEST_ASSERT(this_cpu_has(X86_FEATURE_SEV));

Done.

Quoted 10 lines
Quoted 10 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +10 -0
+
+     sev_status = rdmsr(MSR_AMD64_SEV);
+     GUEST_ASSERT((sev_status & 0x1) == 1);
+
+     GUEST_DONE();
+}
+
+static struct sev_vm *setup_test_common(void *guest_code, uint64_t policy,
+                                     struct kvm_vcpu **vcpu)
+{
Quoted 8 lines

tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 83

Quoted 6 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +6 -0
+     uint8_t measurement[512];
+     struct sev_vm *sev;
+     struct kvm_vm *vm;
+     int i;
+
+     sev = sev_vm_create(policy, TOTAL_PAGES);

TEST_ASSERT(sev, ...) so that this doesn't silently "pass"?

Done.

Quoted 27 lines
Quoted 27 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +26 -0
+     if (!sev)
+             return NULL;
+     vm = sev_get_vm(sev);
+
+     /* Set up VCPU and initial guest kernel. */
+     *vcpu = vm_vcpu_add(vm, VCPU_ID, guest_code);
+     kvm_vm_elf_load(vm, program_invocation_name);
+
+     /* Allocations/setup done. Encrypt initial guest payload. */
+     sev_vm_launch(sev);
+
+     /* Dump the initial measurement. A test to actually verify it would be nice. */
+     sev_vm_launch_measure(sev, measurement);
+     pr_info("guest measurement: ");
+     for (i = 0; i < 32; ++i)
+             pr_info("%02x", measurement[i]);
+     pr_info("\n");
+
+     sev_vm_launch_finish(sev);
+
+     return sev;
+}
+
+static void test_sev(void *guest_code, uint64_t policy)
+{
+     struct sev_vm *sev;
Quoted 8 lines

tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 114

Quoted 5 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +5 -0
+     struct kvm_vcpu *vcpu;
+
+     sev = setup_test_common(guest_code, policy, &vcpu);
+     if (!sev)
+             return;

And with an assert above, this return goes away. Or better yet, fold setup_test_common() into test_sev(), there's only the one user of the so called "common" function.

Done.

Quoted 21 lines
Quoted 21 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +17 -1
+
+     /* Guest is ready to run. Do the tests. */
+     guest_run_loop(vcpu);
+
+     pr_info("guest ran successfully\n");
+
+     sev_vm_free(sev);
+}
+
+int main(int argc, char *argv[])
+{
+     /* SEV tests */
+     test_sev(guest_sev_code, SEV_POLICY_NO_DBG);
+     test_sev(guest_sev_code, 0);
+
+     return 0;
+}
--

2.37.1.559.g78731f0fdb-goog

#29 lore raw

Re: [V3 11/11] KVM: selftests: Add simple sev vm testing

In reply to Peter Gonda
Quoted 5 lines · Peter Gonda

A very simple of booting SEV guests that checks related CPUID bits. This is a stripped down version of "[PATCH v2 08/13] KVM: selftests: add SEV boot tests" from Michael but much simpler.

If you're including Mike's SOB, then he should either be credited as the author or with Co-developed-by. If the code has been significantly rewritten, i.e. carrying Mike's SOB would be disingenuous, then do a less "official"

or