Skip to contentSkip to conversation

KVM: selftests: Add simple SEV test

PATCH Superseded Peter Gonda 2 participants 23 messages Aug 1, 2022 – Aug 2, 2022

Subsystems (4)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

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

V2
* Dropped RFC tag
* Correctly seperated 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: Make ucall work with encrypted guests
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/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   3 +
 .../selftests/kvm/include/kvm_util_base.h     |  26 ++
 .../testing/selftests/kvm/include/sparsebit.h |  36 +--
 .../selftests/kvm/include/ucall_common.h      |  15 +-
 .../selftests/kvm/include/x86_64/sev.h        |  47 +++
 .../testing/selftests/kvm/lib/aarch64/ucall.c |  33 +--
 tools/testing/selftests/kvm/lib/kvm_util.c    | 267 +++++++++++++-----
 tools/testing/selftests/kvm/lib/riscv/ucall.c |  40 +--
 tools/testing/selftests/kvm/lib/s390x/ucall.c |  37 +--
 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  |  37 +--
 .../selftests/kvm/x86_64/sev_all_boot_test.c  | 131 +++++++++
 17 files changed, 885 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.455.g008518b4e5-goog
#1 lore raw

PATCH v2 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.455.g008518b4e5-goog
Reviewed-by on v2 1/11
#2 lore raw

Re: [V2 01/11] KVM: selftests: move vm_phy_pages_alloc() earlier in file

Quoted 10 lines · Peter Gonda

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.

Quoted 7 lines
---
 tools/testing/selftests/kvm/lib/kvm_util.c | 145 ++++++++++-----------
 1 file changed, 72 insertions(+), 73 deletions(-)
#3 lore raw

PATCH v2 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.455.g008518b4e5-goog
Reviewed-by on v2 2/11
#4 lore raw

Re: [V2 02/11] KVM: selftests: sparsebit: add const where appropriate

Quoted 8 lines · 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.

Quoted 7 lines
---
 .../testing/selftests/kvm/include/sparsebit.h | 36 +++++++-------
 tools/testing/selftests/kvm/lib/sparsebit.c   | 48 +++++++++----------
 2 files changed, 42 insertions(+), 42 deletions(-)
#5 lore raw

PATCH v2 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.455.g008518b4e5-goog
#6 lore raw

PATCH v2 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.455.g008518b4e5-goog
#7 lore raw

PATCH v2 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.455.g008518b4e5-goog
#8 lore raw

PATCH v2 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...)

No functional change intended.

---
 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..749ffdf23855
@@ -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 = {
+		.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);
+
+	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.455.g008518b4e5-goog
#9 lore raw

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

Quoted 9 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...)

No functional change intended.

But there is...

Quoted 92 lines
---
 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 -5
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;

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

Quoted 10 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +0 -10
-	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);
-

There are WRITE_ONCE's being used here...

Quoted 114 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +1 -1
 	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 +7 -0
new file mode 100644
index 000000000000..749ffdf23855
@@ -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 = {
+		.cmd = cmd,

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

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c new file +10 -0
+	};
+	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);

...but not here. At least AArch64 needs them, see commit 9e2f6498efbb ("selftests: KVM: Handle compiler optimizations in ucall")

Quoted 50 lines
tools/testing/selftests/kvm/lib/ucall_common.c new file +3 -0
+
+	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.455.g008518b4e5-goog

Thanks,
drew

#10 lore raw

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

Quoted 7 lines · Andrew Jones
Quoted 5 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.

Quoted 6 lines
Quoted 4 lines

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

No functional change intended.

But there is...

Yea my mistake. Looking at 9e2f6498efbbc it appears this optimization could happen on all the other archs given the same reasoning in the description "perhaps due to no immediate readers".

What do you think about dropping the "No functional change intended" from this patch and moving the WRITE_ONCE() for the ops in the common ucall() code?

Quoted 287 lines
Quoted 93 lines
---
 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 -5
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;

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

Quoted 10 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +0 -10
-     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);
-

There are WRITE_ONCE's being used here...

Quoted 114 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +1 -1
      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 +7 -0
new file mode 100644
index 000000000000..749ffdf23855
@@ -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 = {
+             .cmd = cmd,

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

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c new file +10 -0
+     };
+     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);

...but not here. At least AArch64 needs them, see commit 9e2f6498efbb ("selftests: KVM: Handle compiler optimizations in ucall")

Quoted 48 lines
tools/testing/selftests/kvm/lib/ucall_common.c new file +3 -0
+
+     ucall_arch_do_ucall((vm_vaddr_t)&uc);
+}
tools/testing/selftests/kvm/lib/x86_64/ucall.c +5 -19
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 = {};
--
Quoted 2 lines

2.37.1.455.g008518b4e5-goog

Thanks,
drew

#11 lore raw

PATCH v2 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 | 13 +++----------
 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(+), 58 deletions(-)
tools/testing/selftests/kvm/include/ucall_common.h +2 -6
index 5a85f5318bbe..c1bc8e33ef3f 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);
+uint64_t 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 -10
index 1c81a6a5c1f2..d2f099caa9ab 100644
@@ -78,24 +78,17 @@ void ucall_arch_do_ucall(vm_vaddr_t uc)
 uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 {
 	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) {
-		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));
-		memcpy(&ucall, addr_gva2hva(vcpu->vm, gva), sizeof(ucall));
 
-		vcpu_run_complete_io(vcpu);
-		if (uc)
-			memcpy(uc, &ucall, sizeof(ucall));
+		return ucall_addr;
 	}
 
-	return ucall.cmd;
+	return 0;
 }
tools/testing/selftests/kvm/lib/riscv/ucall.c +3 -16
index b1598f418c1f..3f000d0b705f 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)
+uint64_t 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 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 0;
 }
tools/testing/selftests/kvm/lib/s390x/ucall.c +3 -13
index 114cb4af295f..f7a5a7eb4aa8 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)
+uint64_t 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 run->s.regs.gprs[reg];
 	}
-
-	return ucall.cmd;
+	return 0;
 }
tools/testing/selftests/kvm/lib/ucall_common.c +19 -0
index 749ffdf23855..a060252bab40 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 = addr_gva2hva(vcpu->vm, 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..24746120a593 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)
+uint64_t 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 regs.rdi;
 	}
-
-	return ucall.cmd;
+	return 0;
 }
Signature · 1 line
2.37.1.455.g008518b4e5-goog
#12 lore raw

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

Quoted 28 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.

---
 .../selftests/kvm/include/ucall_common.h      |  8 ++------
 .../testing/selftests/kvm/lib/aarch64/ucall.c | 13 +++----------
 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(+), 58 deletions(-)
tools/testing/selftests/kvm/include/ucall_common.h +0 -0
index 5a85f5318bbe..c1bc8e33ef3f 100644

tools/testing/selftests/kvm/include/ucall_common.h struct ucall { · line 30

Quoted 5 lines
tools/testing/selftests/kvm/include/ucall_common.h +1 -1
@@ -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);
+uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu);

This should now return a vm_vaddr_t instead of a uint64_t.

Quoted 23 lines
tools/testing/selftests/kvm/include/ucall_common.h +1 -5
 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 +0 -0
index 1c81a6a5c1f2..d2f099caa9ab 100644

tools/testing/selftests/kvm/lib/aarch64/ucall.c void ucall_arch_do_ucall(vm_vaddr_t uc) · line 78

Quoted 1 line
tools/testing/selftests/kvm/lib/aarch64/ucall.c +0 -0
@@ -78,24 +78,17 @@ void ucall_arch_do_ucall(vm_vaddr_t uc)
 uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)

Need to drop uc from the parameters.

Quoted 5 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +0 -3
 {
 	struct kvm_run *run = vcpu->run;
-	struct ucall ucall = {};
-
-	if (uc)

tools/testing/selftests/kvm/lib/aarch64/ucall.c void ucall_arch_do_ucall(vm_vaddr_t uc) · line 88

Quoted 10 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +1 -2
-		memset(uc, 0, sizeof(*uc));
 
 	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));

We just dropped the gva variable, so this can't compile. We don't want to return a uint64_t anyway, though, so we shouldn't replace gva, we should return it.

Quoted 32 lines
tools/testing/selftests/kvm/lib/aarch64/ucall.c +2 -5
-		memcpy(&ucall, addr_gva2hva(vcpu->vm, gva), sizeof(ucall));
 
-		vcpu_run_complete_io(vcpu);
-		if (uc)
-			memcpy(uc, &ucall, sizeof(ucall));
+		return ucall_addr;
 	}
 
-	return ucall.cmd;
+	return 0;
 }
tools/testing/selftests/kvm/lib/riscv/ucall.c +1 -5
index b1598f418c1f..3f000d0b705f 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)
+uint64_t 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:

tools/testing/selftests/kvm/lib/riscv/ucall.c void ucall_arch_do_ucall(vm_vaddr_t uc) · line 62

Quoted 10 lines
tools/testing/selftests/kvm/lib/riscv/ucall.c +1 -9
-			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 vcpu->vm, run->riscv_sbi.args[0];

The "vcpu->vm," needs to go away.

Quoted 109 lines
tools/testing/selftests/kvm/lib/riscv/ucall.c +1 -2
 		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 0;
 }
tools/testing/selftests/kvm/lib/s390x/ucall.c +3 -13
index 114cb4af295f..f7a5a7eb4aa8 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)
+uint64_t 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 run->s.regs.gprs[reg];
 	}
-
-	return ucall.cmd;
+	return 0;
 }
tools/testing/selftests/kvm/lib/ucall_common.c +19 -0
index 749ffdf23855..a060252bab40 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 = addr_gva2hva(vcpu->vm, 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..24746120a593 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)
+uint64_t 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 regs.rdi;
 	}
-
-	return ucall.cmd;
+	return 0;
 }
Signature · 1 line
2.37.1.455.g008518b4e5-goog

Thanks,
drew

#13 lore raw

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

In reply to Peter Gonda
Quoted 8 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).

The code does not match this description anymore, now that ucall_arch_get_ucall() returns a gva (as a uint64_t), but the description is good, the code is wrong. Please restore the spirit of Sean's patch (particularly because it still says it's from Sean...)

Thanks,
drew

Quoted 215 lines

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 | 13 +++----------
 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(+), 58 deletions(-)
tools/testing/selftests/kvm/include/ucall_common.h +2 -6
index 5a85f5318bbe..c1bc8e33ef3f 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);
+uint64_t 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 -10
index 1c81a6a5c1f2..d2f099caa9ab 100644
@@ -78,24 +78,17 @@ void ucall_arch_do_ucall(vm_vaddr_t uc)
 uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 {
 	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) {
-		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));
-		memcpy(&ucall, addr_gva2hva(vcpu->vm, gva), sizeof(ucall));
 
-		vcpu_run_complete_io(vcpu);
-		if (uc)
-			memcpy(uc, &ucall, sizeof(ucall));
+		return ucall_addr;
 	}
 
-	return ucall.cmd;
+	return 0;
 }
tools/testing/selftests/kvm/lib/riscv/ucall.c +3 -16
index b1598f418c1f..3f000d0b705f 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)
+uint64_t 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 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 0;
 }
tools/testing/selftests/kvm/lib/s390x/ucall.c +3 -13
index 114cb4af295f..f7a5a7eb4aa8 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)
+uint64_t 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 run->s.regs.gprs[reg];
 	}
-
-	return ucall.cmd;
+	return 0;
 }
tools/testing/selftests/kvm/lib/ucall_common.c +19 -0
index 749ffdf23855..a060252bab40 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 = addr_gva2hva(vcpu->vm, 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..24746120a593 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)
+uint64_t 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 regs.rdi;
 	}
-
-	return ucall.cmd;
+	return 0;
 }
Signature · 1 line
2.37.1.455.g008518b4e5-goog
#14 lore raw

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

Quoted 11 lines · Andrew Jones
Quoted 8 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).

Quoted 4 lines

The code does not match this description anymore, now that ucall_arch_get_ucall() returns a gva (as a uint64_t), but the description is good, the code is wrong. Please restore the spirit of Sean's patch (particularly because it still says it's from Sean...)

As discussed in the encrypted ucall patch I'll get this fixed up to return gva from ucall_arch_get_ucall() and return a vm_addr_t.

Quoted 219 lines

Thanks,
drew

Quoted 215 lines

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 | 13 +++----------
 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(+), 58 deletions(-)
tools/testing/selftests/kvm/include/ucall_common.h +2 -6
index 5a85f5318bbe..c1bc8e33ef3f 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);
+uint64_t 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 -10
index 1c81a6a5c1f2..d2f099caa9ab 100644
@@ -78,24 +78,17 @@ void ucall_arch_do_ucall(vm_vaddr_t uc)
 uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 {
      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) {
-             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));
-             memcpy(&ucall, addr_gva2hva(vcpu->vm, gva), sizeof(ucall));
 
-             vcpu_run_complete_io(vcpu);
-             if (uc)
-                     memcpy(uc, &ucall, sizeof(ucall));
+             return ucall_addr;
      }
 
-     return ucall.cmd;
+     return 0;
 }
tools/testing/selftests/kvm/lib/riscv/ucall.c +3 -16
index b1598f418c1f..3f000d0b705f 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)
+uint64_t 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 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 0;
 }
tools/testing/selftests/kvm/lib/s390x/ucall.c +3 -13
index 114cb4af295f..f7a5a7eb4aa8 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)
+uint64_t 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 run->s.regs.gprs[reg];
      }
-
-     return ucall.cmd;
+     return 0;
 }
tools/testing/selftests/kvm/lib/ucall_common.c +19 -0
index 749ffdf23855..a060252bab40 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 = addr_gva2hva(vcpu->vm, 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 -14
index 9f532dba1003..24746120a593 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)
+uint64_t 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 regs.rdi;
      }
-
-     return ucall.cmd;
+     return 0;
 }
--

2.37.1.455.g008518b4e5-goog

#15 lore raw

PATCH v2 8/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 +++++++
 1 file changed, 7 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 */
Signature · 1 line
2.37.1.455.g008518b4e5-goog
#16 lore raw

PATCH v2 9/11 KVM: selftests: Make ucall work with encrypted guests

In reply to Peter Gonda

Add support for encrypted, SEV, guests in the ucall framework. If encryption is enabled set up a pool of ucall structs in the guests' shared memory region. 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 result.

---
 .../selftests/kvm/include/kvm_util_base.h     |   3 +
 .../selftests/kvm/include/ucall_common.h      |  14 +--
 .../testing/selftests/kvm/lib/ucall_common.c  | 112 +++++++++++++++++-
 3 files changed, 115 insertions(+), 14 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +3 -0
index 8ce9e5be70a3..ad4abc6be1ab 100644
@@ -102,6 +102,9 @@ struct kvm_vm {
 	int stats_fd;
 	struct kvm_stats_header stats_header;
 	struct kvm_stats_desc *stats_desc;
+
+	bool use_ucall_list;
+	struct list_head ucall_list;
 };
 
 
tools/testing/selftests/kvm/include/ucall_common.h +6 -8
index c1bc8e33ef3f..a96220ac6024 100644
@@ -22,6 +22,10 @@ enum {
 struct ucall {
 	uint64_t cmd;
 	uint64_t args[UCALL_MAX_ARGS];
+
+	/* For encrypted guests. */
+	uint64_t idx;
+	struct ucall *hva;
 };
 
 void ucall_arch_init(struct kvm_vm *vm, void *arg);
@@ -32,15 +36,9 @@ uint64_t 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/ucall_common.c +106 -6

135 lines not on this page · raw message

Signature · 1 line
2.37.1.455.g008518b4e5-goog
#17 lore raw

Re: [V2 09/11] KVM: selftests: Make ucall work with encrypted guests

Quoted 21 lines · Peter Gonda

Add support for encrypted, SEV, guests in the ucall framework. If encryption is enabled set up a pool of ucall structs in the guests' shared memory region. 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 result.

---
 .../selftests/kvm/include/kvm_util_base.h     |   3 +
 .../selftests/kvm/include/ucall_common.h      |  14 +--
 .../testing/selftests/kvm/lib/ucall_common.c  | 112 +++++++++++++++++-
 3 files changed, 115 insertions(+), 14 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +0 -0
index 8ce9e5be70a3..ad4abc6be1ab 100644

tools/testing/selftests/kvm/include/kvm_util_base.h struct kvm_vm { · line 106

Quoted 5 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +2 -0
@@ -102,6 +102,9 @@ struct kvm_vm {
 	int stats_fd;
 	struct kvm_stats_header stats_header;
 	struct kvm_stats_desc *stats_desc;
+
+	bool use_ucall_list;

use_ucall_pool

Quoted 9 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +1 -0
+	struct list_head ucall_list;
 };
 
 
tools/testing/selftests/kvm/include/ucall_common.h +0 -0
index c1bc8e33ef3f..a96220ac6024 100644

tools/testing/selftests/kvm/include/ucall_common.h enum { · line 26

Quoted 5 lines
tools/testing/selftests/kvm/include/ucall_common.h +2 -0
@@ -22,6 +22,10 @@ enum {
 struct ucall {
 	uint64_t cmd;
 	uint64_t args[UCALL_MAX_ARGS];
+
+	/* For encrypted guests. */

Please, no 'encrypted' words in ucall files. ucalls shouldn't care about guest types. Indeed, the summary of this patch could even drop the word 'encrypted'. This patch is adding support for ucall pools, which is motivated by the need to support encrypted guests (the motivation should go in the commit message, but otherwise the patch should be ucall specific and guest type agnostic)

tools/testing/selftests/kvm/include/ucall_common.h enum { · line 27

Quoted 1 line
tools/testing/selftests/kvm/include/ucall_common.h +1 -0
+	uint64_t idx;

We don't need 'idx' because 'hva' will always be at the idx * sizeof(struct ucall) offset of ucall_hdr->ucalls, which means we can always calculate it,

static inline size_t uc_pool_idx(struct ucall *uc) {
return uc->hva - ucall_hdr->ucalls;
}

Quoted 29 lines
tools/testing/selftests/kvm/include/ucall_common.h +3 -8
+	struct ucall *hva;
 };
 
 void ucall_arch_init(struct kvm_vm *vm, void *arg);
@@ -32,15 +36,9 @@ uint64_t 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/ucall_common.c +0 -0
index a060252bab40..feb0173179ec 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_list;

use_ucall_pool

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

Quoted 1 line
tools/testing/selftests/kvm/lib/ucall_common.c +1 -0
+static struct ucall_header *ucall_hdr;

ucall_pool

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

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

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

single VM at a time

And I'd leave that on a single line. checkpatch allows up to 100 chars and I'm included to use all 110 chars of it.

Quoted 11 lines
tools/testing/selftests/kvm/lib/ucall_common.c +11 -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;
+		uc->idx = i;
+	}
+
+	ucall_hdr = (struct ucall_header *)vaddr;

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

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +10 -0
+	sync_global_to_guest(vm, ucall_hdr);
+
+out:
+	ucall_arch_init(vm, arg);
+}
+
+void ucall_uninit(struct kvm_vm *vm)
+{
+	use_ucall_list = false;
+	ucall_hdr = NULL;

It's unlikely we'd ever change the ucall setup on a running VM, but we should sync these changes to the guest for good measure.

Quoted 8 lines
tools/testing/selftests/kvm/lib/ucall_common.c +8 -0
+
+	ucall_arch_uninit(vm);
+}
+
+static struct ucall *ucall_alloc(void)
+{
+	struct ucall *uc = NULL;
+	int i;

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

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +10 -0
+
+	if (!use_ucall_list)
+		goto out;
+
+	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
+		if (atomic_test_and_set_bit(i, ucall_hdr->in_use))
+			continue;
+
+		uc = &ucall_hdr->ucalls[i];
+	}

This is not what I suggested in the last revision and it's still wrong. Here, you're still looping through all of the pool and returning the last one.

What I suggested was

for (i = 0; i < KVM_MAX_VCPUS; ++i) {
if (!atomic_test_and_set_bit(i, ucall_hdr->in_use)) {
uc = &ucall_hdr->ucalls[i];
break;
}
}

Notice how we leave the loop early when we find a free uc.

We should also zero the contents of the uc buffer before returning.

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

Quoted 11 lines
tools/testing/selftests/kvm/lib/ucall_common.c +11 -0
+
+out:
+	return uc;
+}
+
+static void ucall_free(struct ucall *uc)
+{
+	if (!use_ucall_list)
+		return;
+
+	clear_bit(uc->idx, ucall_hdr->in_use);

This seems to be the one and only use of idx which is another argument for dropping the variable and just calculating it instead.

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

Quoted 6 lines
tools/testing/selftests/kvm/lib/ucall_common.c +6 -0
+}
+
+static vm_vaddr_t get_ucall_addr(struct ucall *uc)
+{
+	if (use_ucall_list)
+		return (vm_vaddr_t)uc->hva;

A comment explaining that this hva has already been synchronized with the guest would be good. Or maybe a different name for the pointer than hva, one which conveys that it's a pointer that works as both an hva and gva would be better.

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

Quoted 9 lines
tools/testing/selftests/kvm/lib/ucall_common.c +3 -3
+
+	return (vm_vaddr_t)uc;
+}
 
 void ucall(uint64_t cmd, int nargs, ...)
 {
-	struct ucall uc = {
-		.cmd = cmd,
-	};

This zeros all members except cmd.

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

Quoted 2 lines
tools/testing/selftests/kvm/lib/ucall_common.c +2 -0
+	struct ucall *uc;
+	struct ucall tmp;

And this makes tmp full of stack garbage, so in the non uc-pool case we no longer have an equivalent uc. Please initialize tmp the same way uc was initialized.

Quoted 9 lines
tools/testing/selftests/kvm/lib/ucall_common.c +6 -0
 	va_list va;
 	int i;
 
+	uc = ucall_alloc();
+	if (!uc)
+		uc = &tmp;
+
+	uc->cmd = cmd;
+

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

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +2 -2
 	nargs = min(nargs, UCALL_MAX_ARGS);
 
 	va_start(va, nargs);
 	for (i = 0; i < nargs; ++i)
-		uc.args[i] = va_arg(va, uint64_t);
+		uc->args[i] = va_arg(va, uint64_t);
 	va_end(va);
 
-	ucall_arch_do_ucall((vm_vaddr_t)&uc);
+	ucall_arch_do_ucall(get_ucall_addr(uc));

We don't need get_ucall_addr(). Just do the if-else right here

   if (use_ucall_list)
       ucall_arch_do_ucall((vm_vaddr_t)uc->hva);
   else
      ucall_arch_do_ucall((vm_vaddr_t)uc);
Quoted 14 lines
tools/testing/selftests/kvm/lib/ucall_common.c +10 -0
+
+	ucall_free(uc);
+}
+
+static void *get_ucall_hva(struct kvm_vm *vm, uint64_t uc)
+{
+	if (vm->use_ucall_list)
+		return (void *)uc;
+
+	return addr_gva2hva(vm, uc);
 }
 
 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)

tools/testing/selftests/kvm/lib/ucall_common.c uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) · line 130

Quoted 5 lines
tools/testing/selftests/kvm/lib/ucall_common.c +1 -1
@@ -27,7 +127,7 @@ uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
 	if (!uc)
 		uc = &ucall;
 
-	addr = addr_gva2hva(vcpu->vm, ucall_arch_get_ucall(vcpu));
+	addr = get_ucall_hva(vcpu->vm, ucall_arch_get_ucall(vcpu));

Wait, I thought we wanted ucall_arch_get_ucall() to return hvas? It would make more sense if it did, since it's called from the host.

Quoted 6 lines
tools/testing/selftests/kvm/lib/ucall_common.c +0 -0
 	if (addr) {
 		memcpy(uc, addr, sizeof(*uc));
 		vcpu_run_complete_io(vcpu);
Signature · 1 line
2.37.1.455.g008518b4e5-goog

Thanks,
drew

#18 lore raw

Re: [V2 09/11] KVM: selftests: Make ucall work with encrypted guests

Quoted 18 lines · Andrew Jones
Quoted 16 lines · Peter Gonda

Add support for encrypted, SEV, guests in the ucall framework. If encryption is enabled set up a pool of ucall structs in the guests' shared memory region. 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 result.

---
 .../selftests/kvm/include/kvm_util_base.h     |   3 +
 .../selftests/kvm/include/ucall_common.h      |  14 +--
 .../testing/selftests/kvm/lib/ucall_common.c  | 112 +++++++++++++++++-
 3 files changed, 115 insertions(+), 14 deletions(-)
Quoted 15 lines

tools/testing/selftests/kvm/include/kvm_util_base.h struct kvm_vm { · line 106

Quoted 10 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +2 -0
index 8ce9e5be70a3..ad4abc6be1ab 100644
@@ -102,6 +102,9 @@ struct kvm_vm {
      int stats_fd;
      struct kvm_stats_header stats_header;
      struct kvm_stats_desc *stats_desc;
+
+     bool use_ucall_list;

use_ucall_pool

Quoted 2 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +1 -0
+     struct list_head ucall_list;
 };

Will do. I also need to remove this |ucall_list| member.

Quoted 22 lines

tools/testing/selftests/kvm/include/ucall_common.h enum { · line 26

Quoted 12 lines
tools/testing/selftests/kvm/include/ucall_common.h +2 -0
index c1bc8e33ef3f..a96220ac6024 100644
@@ -22,6 +22,10 @@ enum {
 struct ucall {
      uint64_t cmd;
      uint64_t args[UCALL_MAX_ARGS];
+
+     /* For encrypted guests. */

Please, no 'encrypted' words in ucall files. ucalls shouldn't care about guest types. Indeed, the summary of this patch could even drop the word 'encrypted'. This patch is adding support for ucall pools, which is motivated by the need to support encrypted guests (the motivation should go in the commit message, but otherwise the patch should be ucall specific and guest type agnostic)

tools/testing/selftests/kvm/include/ucall_common.h enum { · line 27

Quoted 1 line
tools/testing/selftests/kvm/include/ucall_common.h +1 -0
+     uint64_t idx;
Quoted 8 lines

We don't need 'idx' because 'hva' will always be at the idx * sizeof(struct ucall) offset of ucall_hdr->ucalls, which means we can always calculate it,

static inline size_t uc_pool_idx(struct ucall *uc)

 {
        return uc->hva - ucall_hdr->ucalls;

}

Good call, I didn't think of that.

Quoted 43 lines
Quoted 30 lines
tools/testing/selftests/kvm/include/ucall_common.h +3 -8
+     struct ucall *hva;
 };
 
 void ucall_arch_init(struct kvm_vm *vm, void *arg);
@@ -32,15 +36,9 @@ uint64_t 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/ucall_common.c +0 -0
index a060252bab40..feb0173179ec 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_list;

use_ucall_pool

Quoted 3 lines

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

Quoted 1 line
tools/testing/selftests/kvm/lib/ucall_common.c +1 -0
+static struct ucall_header *ucall_hdr;

ucall_pool

Will update naming here.

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

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

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

single VM at a time

Quoted 2 lines

And I'd leave that on a single line. checkpatch allows up to 100 chars and I'm included to use all 110 chars of it.

Sounds good.

Quoted 17 lines
Quoted 17 lines
tools/testing/selftests/kvm/lib/ucall_common.c +16 -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;
+             uc->idx = i;
+     }
+
+     ucall_hdr = (struct ucall_header *)vaddr;
+     sync_global_to_guest(vm, ucall_hdr);
+
+out:
+     ucall_arch_init(vm, arg);
+}
Quoted 8 lines

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

Quoted 5 lines
tools/testing/selftests/kvm/lib/ucall_common.c +5 -0
+
+void ucall_uninit(struct kvm_vm *vm)
+{
+     use_ucall_list = false;
+     ucall_hdr = NULL;

It's unlikely we'd ever change the ucall setup on a running VM, but we should sync these changes to the guest for good measure.

Hmm I'll need to have some notion of encrypted guests here then. Since the guests page tables also get encrypted we can no longer get the gva2gpa translations so sync_global_to_guest() cannot be done due to the addr_ga2hva(). So is it OK if this call references encryption like below?

    use_ucall_list = false;
     ucall_hdr = NULL;

if (!vm->memencrypt.encrypted) {
sync_global_to_guest(vm, use_ucall_list);
sync_global_to_guest(vm, ucall_hdr);
}

Quoted 26 lines
Quoted 9 lines
tools/testing/selftests/kvm/lib/ucall_common.c +8 -0
+
+     ucall_arch_uninit(vm);
+}
+
+static struct ucall *ucall_alloc(void)
+{
+     struct ucall *uc = NULL;
+     int i;

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

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +10 -0
+
+     if (!use_ucall_list)
+             goto out;
+
+     for (i = 0; i < KVM_MAX_VCPUS; ++i) {
+             if (atomic_test_and_set_bit(i, ucall_hdr->in_use))
+                     continue;
+
+             uc = &ucall_hdr->ucalls[i];
+     }

This is not what I suggested in the last revision and it's still wrong. Here, you're still looping through all of the pool and returning the last one.

What I suggested was

Quoted 8 lines
        for (i = 0; i < KVM_MAX_VCPUS; ++i) {
                if (!atomic_test_and_set_bit(i, ucall_hdr->in_use)) {
                        uc = &ucall_hdr->ucalls[i];
                        break;
                }
        }

Notice how we leave the loop early when we find a free uc.

Ah my mistake, I didn't get this fix into this series

Quoted 2 lines

We should also zero the contents of the uc buffer before returning.

Ack will do.

Quoted 15 lines

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

Quoted 12 lines
tools/testing/selftests/kvm/lib/ucall_common.c +11 -0
+
+out:
+     return uc;
+}
+
+static void ucall_free(struct ucall *uc)
+{
+     if (!use_ucall_list)
+             return;
+
+     clear_bit(uc->idx, ucall_hdr->in_use);

This seems to be the one and only use of idx which is another argument for dropping the variable and just calculating it instead.

Will do.

Quoted 12 lines

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

Quoted 7 lines
tools/testing/selftests/kvm/lib/ucall_common.c +6 -0
+}
+
+static vm_vaddr_t get_ucall_addr(struct ucall *uc)
+{
+     if (use_ucall_list)
+             return (vm_vaddr_t)uc->hva;

A comment explaining that this hva has already been synchronized with the guest would be good. Or maybe a different name for the pointer than hva, one which conveys that it's a pointer that works as both an hva and gva would be better.

I don't think this pointer does work as a gva though since its created by just taking &uc during host execution. I can add a comment that mentions this is a hva is already translated for the host and not for guest use other than communicating with the host. I thought 'hva' was a good name here since it is literally the hva of this ucall struct, what name would you prefer?

Quoted 14 lines

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

Quoted 11 lines
tools/testing/selftests/kvm/lib/ucall_common.c +3 -3
+
+     return (vm_vaddr_t)uc;
+}
 
 void ucall(uint64_t cmd, int nargs, ...)
 {
-     struct ucall uc = {
-             .cmd = cmd,
-     };

This zeros all members except cmd.

Quoted 6 lines

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

Quoted 2 lines
tools/testing/selftests/kvm/lib/ucall_common.c +2 -0
+     struct ucall *uc;
+     struct ucall tmp;

And this makes tmp full of stack garbage, so in the non uc-pool case we no longer have an equivalent uc. Please initialize tmp the same way uc was initialized.

Ack, I'll get this fixed.

Quoted 23 lines
Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +6 -0
      va_list va;
      int i;
 
+     uc = ucall_alloc();
+     if (!uc)
+             uc = &tmp;
+
+     uc->cmd = cmd;
+

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

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +2 -2
      nargs = min(nargs, UCALL_MAX_ARGS);
 
      va_start(va, nargs);
      for (i = 0; i < nargs; ++i)
-             uc.args[i] = va_arg(va, uint64_t);
+             uc->args[i] = va_arg(va, uint64_t);
      va_end(va);
 
-     ucall_arch_do_ucall((vm_vaddr_t)&uc);
+     ucall_arch_do_ucall(get_ucall_addr(uc));

We don't need get_ucall_addr(). Just do the if-else right here

Quoted 4 lines
   if (use_ucall_list)
       ucall_arch_do_ucall((vm_vaddr_t)uc->hva);
   else
      ucall_arch_do_ucall((vm_vaddr_t)uc);

Will do.

Quoted 18 lines
Quoted 18 lines
tools/testing/selftests/kvm/lib/ucall_common.c +10 -0
+
+     ucall_free(uc);
+}
+
+static void *get_ucall_hva(struct kvm_vm *vm, uint64_t uc)
+{
+     if (vm->use_ucall_list)
+             return (void *)uc;
+
+     return addr_gva2hva(vm, uc);
 }
 
 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
@@ -27,7 +127,7 @@ uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
      if (!uc)
              uc = &ucall;
 
Quoted 6 lines

tools/testing/selftests/kvm/lib/ucall_common.c uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) · line 130

Quoted 2 lines
tools/testing/selftests/kvm/lib/ucall_common.c +1 -1
-     addr = addr_gva2hva(vcpu->vm, ucall_arch_get_ucall(vcpu));
+     addr = get_ucall_hva(vcpu->vm, ucall_arch_get_ucall(vcpu));

Wait, I thought we wanted ucall_arch_get_ucall() to return hvas? It would make more sense if it did, since it's called from the host.

As you noted in [V2 07/11] I updated ucall_arch_get_ucall() to return the gva. I figured this was just more boilerplate which could be consolidated given all archs right now just use the addr_gva2hva() helper to do this translation. This change also allowed for the "use_ucall_pool" to be completely contained in ucall_common.c instead of spilling into each arch specific file. If there is a preference to have the arch specific calls translate from gva to hva I can do that, I think I'll need to have them all check if "use_ucall_pool" is set though. Thoughts?

We can still update ucall_arch_get_ucall() to return a pointer type so that we can return NULL instead of 0.

Quoted 10 lines
Quoted 7 lines
tools/testing/selftests/kvm/lib/ucall_common.c +0 -1
      if (addr) {
              memcpy(uc, addr, sizeof(*uc));
              vcpu_run_complete_io(vcpu);
--

2.37.1.455.g008518b4e5-goog

Thanks,
drew

#19 lore raw

Re: [V2 09/11] KVM: selftests: Make ucall work with encrypted guests

Quoted 175 lines · Peter Gonda
Quoted 18 lines · Andrew Jones
Quoted 16 lines · Peter Gonda

Add support for encrypted, SEV, guests in the ucall framework. If encryption is enabled set up a pool of ucall structs in the guests' shared memory region. 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 result.

---
 .../selftests/kvm/include/kvm_util_base.h     |   3 +
 .../selftests/kvm/include/ucall_common.h      |  14 +--
 .../testing/selftests/kvm/lib/ucall_common.c  | 112 +++++++++++++++++-
 3 files changed, 115 insertions(+), 14 deletions(-)
Quoted 15 lines

tools/testing/selftests/kvm/include/kvm_util_base.h struct kvm_vm { · line 106

Quoted 10 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +2 -0
index 8ce9e5be70a3..ad4abc6be1ab 100644
@@ -102,6 +102,9 @@ struct kvm_vm {
      int stats_fd;
      struct kvm_stats_header stats_header;
      struct kvm_stats_desc *stats_desc;
+
+     bool use_ucall_list;

use_ucall_pool

Quoted 2 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +1 -0
+     struct list_head ucall_list;
 };

Will do. I also need to remove this |ucall_list| member.

Quoted 22 lines

tools/testing/selftests/kvm/include/ucall_common.h enum { · line 26

Quoted 12 lines
tools/testing/selftests/kvm/include/ucall_common.h +2 -0
index c1bc8e33ef3f..a96220ac6024 100644
@@ -22,6 +22,10 @@ enum {
 struct ucall {
      uint64_t cmd;
      uint64_t args[UCALL_MAX_ARGS];
+
+     /* For encrypted guests. */

Please, no 'encrypted' words in ucall files. ucalls shouldn't care about guest types. Indeed, the summary of this patch could even drop the word 'encrypted'. This patch is adding support for ucall pools, which is motivated by the need to support encrypted guests (the motivation should go in the commit message, but otherwise the patch should be ucall specific and guest type agnostic)

tools/testing/selftests/kvm/include/ucall_common.h enum { · line 27

Quoted 1 line
tools/testing/selftests/kvm/include/ucall_common.h +1 -0
+     uint64_t idx;
Quoted 8 lines

We don't need 'idx' because 'hva' will always be at the idx * sizeof(struct ucall) offset of ucall_hdr->ucalls, which means we can always calculate it,

static inline size_t uc_pool_idx(struct ucall *uc)

 {
        return uc->hva - ucall_hdr->ucalls;

}

Good call, I didn't think of that.

Quoted 43 lines
Quoted 30 lines
tools/testing/selftests/kvm/include/ucall_common.h +3 -8
+     struct ucall *hva;
 };
 
 void ucall_arch_init(struct kvm_vm *vm, void *arg);
@@ -32,15 +36,9 @@ uint64_t 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/ucall_common.c +0 -0
index a060252bab40..feb0173179ec 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_list;

use_ucall_pool

Quoted 3 lines

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

Quoted 1 line
tools/testing/selftests/kvm/lib/ucall_common.c +1 -0
+static struct ucall_header *ucall_hdr;

ucall_pool

Will update naming here.

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

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

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

single VM at a time

Quoted 2 lines

And I'd leave that on a single line. checkpatch allows up to 100 chars and I'm included to use all 110 chars of it.

Sounds good.

Quoted 17 lines
Quoted 17 lines
tools/testing/selftests/kvm/lib/ucall_common.c +16 -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;
+             uc->idx = i;
+     }
+
+     ucall_hdr = (struct ucall_header *)vaddr;
+     sync_global_to_guest(vm, ucall_hdr);
+
+out:
+     ucall_arch_init(vm, arg);
+}
Quoted 8 lines

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

Quoted 5 lines
tools/testing/selftests/kvm/lib/ucall_common.c +5 -0
+
+void ucall_uninit(struct kvm_vm *vm)
+{
+     use_ucall_list = false;
+     ucall_hdr = NULL;

It's unlikely we'd ever change the ucall setup on a running VM, but we should sync these changes to the guest for good measure.

Hmm I'll need to have some notion of encrypted guests here then. Since the guests page tables also get encrypted we can no longer get the gva2gpa translations so sync_global_to_guest() cannot be done due to the addr_ga2hva(). So is it OK if this call references encryption like below?

Quoted 6 lines
    use_ucall_list = false;
     ucall_hdr = NULL;
if (!vm->memencrypt.encrypted) {
   sync_global_to_guest(vm, use_ucall_list);

sync_global_to_guest(vm, ucall_hdr);
}

Yes, I guess that's the best we can do.

If sync_global_to_guest() becomes useless once a guest is encrypted then maybe it should assert !vm->memencrypt.encrypted.

Quoted 74 lines
Quoted 27 lines
Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +8 -0
+
+     ucall_arch_uninit(vm);
+}
+
+static struct ucall *ucall_alloc(void)
+{
+     struct ucall *uc = NULL;
+     int i;

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

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +10 -0
+
+     if (!use_ucall_list)
+             goto out;
+
+     for (i = 0; i < KVM_MAX_VCPUS; ++i) {
+             if (atomic_test_and_set_bit(i, ucall_hdr->in_use))
+                     continue;
+
+             uc = &ucall_hdr->ucalls[i];
+     }

This is not what I suggested in the last revision and it's still wrong. Here, you're still looping through all of the pool and returning the last one.

What I suggested was

Quoted 8 lines
        for (i = 0; i < KVM_MAX_VCPUS; ++i) {
                if (!atomic_test_and_set_bit(i, ucall_hdr->in_use)) {
                        uc = &ucall_hdr->ucalls[i];
                        break;
                }
        }

Notice how we leave the loop early when we find a free uc.

Ah my mistake, I didn't get this fix into this series

Quoted 2 lines

We should also zero the contents of the uc buffer before returning.

Ack will do.

Quoted 15 lines

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

Quoted 12 lines
tools/testing/selftests/kvm/lib/ucall_common.c +11 -0
+
+out:
+     return uc;
+}
+
+static void ucall_free(struct ucall *uc)
+{
+     if (!use_ucall_list)
+             return;
+
+     clear_bit(uc->idx, ucall_hdr->in_use);

This seems to be the one and only use of idx which is another argument for dropping the variable and just calculating it instead.

Will do.

Quoted 12 lines

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

Quoted 7 lines
tools/testing/selftests/kvm/lib/ucall_common.c +6 -0
+}
+
+static vm_vaddr_t get_ucall_addr(struct ucall *uc)
+{
+     if (use_ucall_list)
+             return (vm_vaddr_t)uc->hva;

A comment explaining that this hva has already been synchronized with the guest would be good. Or maybe a different name for the pointer than hva, one which conveys that it's a pointer that works as both an hva and gva would be better.

Quoted 6 lines

I don't think this pointer does work as a gva though since its created by just taking &uc during host execution. I can add a comment that mentions this is a hva is already translated for the host and not for guest use other than communicating with the host. I thought 'hva' was a good name here since it is literally the hva of this ucall struct, what name would you prefer?

My mistake, I forgot how we initialized uc->hva. 'hva' is a good name.

Quoted 79 lines
Quoted 15 lines

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

Quoted 12 lines
tools/testing/selftests/kvm/lib/ucall_common.c +3 -3
+
+     return (vm_vaddr_t)uc;
+}
 
 void ucall(uint64_t cmd, int nargs, ...)
 {
-     struct ucall uc = {
-             .cmd = cmd,
-     };

This zeros all members except cmd.

Quoted 6 lines

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

Quoted 2 lines
tools/testing/selftests/kvm/lib/ucall_common.c +2 -0
+     struct ucall *uc;
+     struct ucall tmp;

And this makes tmp full of stack garbage, so in the non uc-pool case we no longer have an equivalent uc. Please initialize tmp the same way uc was initialized.

Ack, I'll get this fixed.

Quoted 23 lines
Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +6 -0
      va_list va;
      int i;
 
+     uc = ucall_alloc();
+     if (!uc)
+             uc = &tmp;
+
+     uc->cmd = cmd;
+

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

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +2 -2
      nargs = min(nargs, UCALL_MAX_ARGS);
 
      va_start(va, nargs);
      for (i = 0; i < nargs; ++i)
-             uc.args[i] = va_arg(va, uint64_t);
+             uc->args[i] = va_arg(va, uint64_t);
      va_end(va);
 
-     ucall_arch_do_ucall((vm_vaddr_t)&uc);
+     ucall_arch_do_ucall(get_ucall_addr(uc));

We don't need get_ucall_addr(). Just do the if-else right here

Quoted 4 lines
   if (use_ucall_list)
       ucall_arch_do_ucall((vm_vaddr_t)uc->hva);
   else
      ucall_arch_do_ucall((vm_vaddr_t)uc);

Will do.

Quoted 18 lines
Quoted 18 lines
tools/testing/selftests/kvm/lib/ucall_common.c +10 -0
+
+     ucall_free(uc);
+}
+
+static void *get_ucall_hva(struct kvm_vm *vm, uint64_t uc)
+{
+     if (vm->use_ucall_list)
+             return (void *)uc;
+
+     return addr_gva2hva(vm, uc);
 }
 
 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
@@ -27,7 +127,7 @@ uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
      if (!uc)
              uc = &ucall;
 
Quoted 6 lines

tools/testing/selftests/kvm/lib/ucall_common.c uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) · line 130

Quoted 2 lines
tools/testing/selftests/kvm/lib/ucall_common.c +1 -1
-     addr = addr_gva2hva(vcpu->vm, ucall_arch_get_ucall(vcpu));
+     addr = get_ucall_hva(vcpu->vm, ucall_arch_get_ucall(vcpu));

Wait, I thought we wanted ucall_arch_get_ucall() to return hvas? It would make more sense if it did, since it's called from the host.

Quoted 9 lines

As you noted in [V2 07/11] I updated ucall_arch_get_ucall() to return the gva. I figured this was just more boilerplate which could be consolidated given all archs right now just use the addr_gva2hva() helper to do this translation. This change also allowed for the "use_ucall_pool" to be completely contained in ucall_common.c instead of spilling into each arch specific file. If there is a preference to have the arch specific calls translate from gva to hva I can do that, I think I'll need to have them all check if "use_ucall_pool" is set though. Thoughts?

I just checked what I wrote for the last version of this patch, "[RFC V1 08/10] KVM: selftests: Make ucall work with encrypted guests". It was

Quoted 5 lines

...we're only updating x86's ucall_arch_get_ucall() to return gvas. What about the other architectures? Anyway, I'd rather we don't change ucall_arch_get_ucall() to return gvas. They should continue returning hvas and any trickery needed to translate a pool uc to an hva should be put inside ucall_arch_get_ucall().

(It's comforting to see I was consistent, at least wrt the last review.)

So, I would keep returning hvas and put the 'if use_ucall_pool' inside the ucall_arch_get_ucall() functions.

Thanks,
drew

Quoted 14 lines

We can still update ucall_arch_get_ucall() to return a pointer type so that we can return NULL instead of 0.

Quoted 10 lines
Quoted 7 lines
tools/testing/selftests/kvm/lib/ucall_common.c +0 -1
      if (addr) {
              memcpy(uc, addr, sizeof(*uc));
              vcpu_run_complete_io(vcpu);
--

2.37.1.455.g008518b4e5-goog

Thanks,
drew

#20 lore raw

Re: [V2 09/11] KVM: selftests: Make ucall work with encrypted guests

()

Quoted 184 lines · Andrew Jones
Quoted 175 lines · Peter Gonda
Quoted 18 lines · Andrew Jones
Quoted 16 lines · Peter Gonda

Add support for encrypted, SEV, guests in the ucall framework. If encryption is enabled set up a pool of ucall structs in the guests' shared memory region. 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 result.

---
 .../selftests/kvm/include/kvm_util_base.h     |   3 +
 .../selftests/kvm/include/ucall_common.h      |  14 +--
 .../testing/selftests/kvm/lib/ucall_common.c  | 112 +++++++++++++++++-
 3 files changed, 115 insertions(+), 14 deletions(-)
Quoted 15 lines

tools/testing/selftests/kvm/include/kvm_util_base.h struct kvm_vm { · line 106

Quoted 10 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +2 -0
index 8ce9e5be70a3..ad4abc6be1ab 100644
@@ -102,6 +102,9 @@ struct kvm_vm {
      int stats_fd;
      struct kvm_stats_header stats_header;
      struct kvm_stats_desc *stats_desc;
+
+     bool use_ucall_list;

use_ucall_pool

Quoted 2 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +1 -0
+     struct list_head ucall_list;
 };

Will do. I also need to remove this |ucall_list| member.

Quoted 22 lines

tools/testing/selftests/kvm/include/ucall_common.h enum { · line 26

Quoted 12 lines
tools/testing/selftests/kvm/include/ucall_common.h +2 -0
index c1bc8e33ef3f..a96220ac6024 100644
@@ -22,6 +22,10 @@ enum {
 struct ucall {
      uint64_t cmd;
      uint64_t args[UCALL_MAX_ARGS];
+
+     /* For encrypted guests. */

Please, no 'encrypted' words in ucall files. ucalls shouldn't care about guest types. Indeed, the summary of this patch could even drop the word 'encrypted'. This patch is adding support for ucall pools, which is motivated by the need to support encrypted guests (the motivation should go in the commit message, but otherwise the patch should be ucall specific and guest type agnostic)

tools/testing/selftests/kvm/include/ucall_common.h enum { · line 27

Quoted 1 line
tools/testing/selftests/kvm/include/ucall_common.h +1 -0
+     uint64_t idx;
Quoted 8 lines

We don't need 'idx' because 'hva' will always be at the idx * sizeof(struct ucall) offset of ucall_hdr->ucalls, which means we can always calculate it,

static inline size_t uc_pool_idx(struct ucall *uc)

 {
        return uc->hva - ucall_hdr->ucalls;

}

Good call, I didn't think of that.

Quoted 43 lines
Quoted 30 lines
tools/testing/selftests/kvm/include/ucall_common.h +3 -8
+     struct ucall *hva;
 };
 
 void ucall_arch_init(struct kvm_vm *vm, void *arg);
@@ -32,15 +36,9 @@ uint64_t 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/ucall_common.c +0 -0
index a060252bab40..feb0173179ec 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_list;

use_ucall_pool

Quoted 3 lines

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

Quoted 1 line
tools/testing/selftests/kvm/lib/ucall_common.c +1 -0
+static struct ucall_header *ucall_hdr;

ucall_pool

Will update naming here.

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

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

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

single VM at a time

Quoted 2 lines

And I'd leave that on a single line. checkpatch allows up to 100 chars and I'm included to use all 110 chars of it.

Sounds good.

Quoted 17 lines
Quoted 17 lines
tools/testing/selftests/kvm/lib/ucall_common.c +16 -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;
+             uc->idx = i;
+     }
+
+     ucall_hdr = (struct ucall_header *)vaddr;
+     sync_global_to_guest(vm, ucall_hdr);
+
+out:
+     ucall_arch_init(vm, arg);
+}
Quoted 8 lines

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

Quoted 5 lines
tools/testing/selftests/kvm/lib/ucall_common.c +5 -0
+
+void ucall_uninit(struct kvm_vm *vm)
+{
+     use_ucall_list = false;
+     ucall_hdr = NULL;

It's unlikely we'd ever change the ucall setup on a running VM, but we should sync these changes to the guest for good measure.

Hmm I'll need to have some notion of encrypted guests here then. Since the guests page tables also get encrypted we can no longer get the gva2gpa translations so sync_global_to_guest() cannot be done due to the addr_ga2hva(). So is it OK if this call references encryption like below?

Quoted 6 lines
    use_ucall_list = false;
     ucall_hdr = NULL;
if (!vm->memencrypt.encrypted) {
   sync_global_to_guest(vm, use_ucall_list);

sync_global_to_guest(vm, ucall_hdr);
}

Quoted 4 lines

Yes, I guess that's the best we can do.

If sync_global_to_guest() becomes useless once a guest is encrypted then maybe it should assert !vm->memencrypt.encrypted.

In the last patch I have edited addr_gva2gpa like below to assert. This should cover sync_global_to_guest() and other similar cases.

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 gva2* conversions are not possible.");

        return addr_arch_gva2gpa(vm, gva);

}

Speaking of that though I should move that to 10/11 instead of 11/11.

Quoted 183 lines
Quoted 75 lines
Quoted 28 lines
Quoted 11 lines
tools/testing/selftests/kvm/lib/ucall_common.c +8 -0
+
+     ucall_arch_uninit(vm);
+}
+
+static struct ucall *ucall_alloc(void)
+{
+     struct ucall *uc = NULL;
+     int i;

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

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +10 -0
+
+     if (!use_ucall_list)
+             goto out;
+
+     for (i = 0; i < KVM_MAX_VCPUS; ++i) {
+             if (atomic_test_and_set_bit(i, ucall_hdr->in_use))
+                     continue;
+
+             uc = &ucall_hdr->ucalls[i];
+     }

This is not what I suggested in the last revision and it's still wrong. Here, you're still looping through all of the pool and returning the last one.

What I suggested was

Quoted 8 lines
        for (i = 0; i < KVM_MAX_VCPUS; ++i) {
                if (!atomic_test_and_set_bit(i, ucall_hdr->in_use)) {
                        uc = &ucall_hdr->ucalls[i];
                        break;
                }
        }

Notice how we leave the loop early when we find a free uc.

Ah my mistake, I didn't get this fix into this series

Quoted 2 lines

We should also zero the contents of the uc buffer before returning.

Ack will do.

Quoted 15 lines

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

Quoted 12 lines
tools/testing/selftests/kvm/lib/ucall_common.c +11 -0
+
+out:
+     return uc;
+}
+
+static void ucall_free(struct ucall *uc)
+{
+     if (!use_ucall_list)
+             return;
+
+     clear_bit(uc->idx, ucall_hdr->in_use);

This seems to be the one and only use of idx which is another argument for dropping the variable and just calculating it instead.

Will do.

Quoted 12 lines

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

Quoted 7 lines
tools/testing/selftests/kvm/lib/ucall_common.c +6 -0
+}
+
+static vm_vaddr_t get_ucall_addr(struct ucall *uc)
+{
+     if (use_ucall_list)
+             return (vm_vaddr_t)uc->hva;

A comment explaining that this hva has already been synchronized with the guest would be good. Or maybe a different name for the pointer than hva, one which conveys that it's a pointer that works as both an hva and gva would be better.

Quoted 6 lines

I don't think this pointer does work as a gva though since its created by just taking &uc during host execution. I can add a comment that mentions this is a hva is already translated for the host and not for guest use other than communicating with the host. I thought 'hva' was a good name here since it is literally the hva of this ucall struct, what name would you prefer?

My mistake, I forgot how we initialized uc->hva. 'hva' is a good name.

Quoted 79 lines
Quoted 15 lines

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

Quoted 12 lines
tools/testing/selftests/kvm/lib/ucall_common.c +3 -3
+
+     return (vm_vaddr_t)uc;
+}
 
 void ucall(uint64_t cmd, int nargs, ...)
 {
-     struct ucall uc = {
-             .cmd = cmd,
-     };

This zeros all members except cmd.

Quoted 6 lines

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

Quoted 2 lines
tools/testing/selftests/kvm/lib/ucall_common.c +2 -0
+     struct ucall *uc;
+     struct ucall tmp;

And this makes tmp full of stack garbage, so in the non uc-pool case we no longer have an equivalent uc. Please initialize tmp the same way uc was initialized.

Ack, I'll get this fixed.

Quoted 23 lines
Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +6 -0
      va_list va;
      int i;
 
+     uc = ucall_alloc();
+     if (!uc)
+             uc = &tmp;
+
+     uc->cmd = cmd;
+

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

Quoted 10 lines
tools/testing/selftests/kvm/lib/ucall_common.c +2 -2
      nargs = min(nargs, UCALL_MAX_ARGS);
 
      va_start(va, nargs);
      for (i = 0; i < nargs; ++i)
-             uc.args[i] = va_arg(va, uint64_t);
+             uc->args[i] = va_arg(va, uint64_t);
      va_end(va);
 
-     ucall_arch_do_ucall((vm_vaddr_t)&uc);
+     ucall_arch_do_ucall(get_ucall_addr(uc));

We don't need get_ucall_addr(). Just do the if-else right here

Quoted 4 lines
   if (use_ucall_list)
       ucall_arch_do_ucall((vm_vaddr_t)uc->hva);
   else
      ucall_arch_do_ucall((vm_vaddr_t)uc);

Will do.

Quoted 18 lines
Quoted 18 lines
tools/testing/selftests/kvm/lib/ucall_common.c +10 -0
+
+     ucall_free(uc);
+}
+
+static void *get_ucall_hva(struct kvm_vm *vm, uint64_t uc)
+{
+     if (vm->use_ucall_list)
+             return (void *)uc;
+
+     return addr_gva2hva(vm, uc);
 }
 
 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
@@ -27,7 +127,7 @@ uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
      if (!uc)
              uc = &ucall;
 
Quoted 6 lines

tools/testing/selftests/kvm/lib/ucall_common.c uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) · line 130

Quoted 2 lines
tools/testing/selftests/kvm/lib/ucall_common.c +1 -1
-     addr = addr_gva2hva(vcpu->vm, ucall_arch_get_ucall(vcpu));
+     addr = get_ucall_hva(vcpu->vm, ucall_arch_get_ucall(vcpu));

Wait, I thought we wanted ucall_arch_get_ucall() to return hvas? It would make more sense if it did, since it's called from the host.

Quoted 9 lines

As you noted in [V2 07/11] I updated ucall_arch_get_ucall() to return the gva. I figured this was just more boilerplate which could be consolidated given all archs right now just use the addr_gva2hva() helper to do this translation. This change also allowed for the "use_ucall_pool" to be completely contained in ucall_common.c instead of spilling into each arch specific file. If there is a preference to have the arch specific calls translate from gva to hva I can do that, I think I'll need to have them all check if "use_ucall_pool" is set though. Thoughts?

I just checked what I wrote for the last version of this patch, "[RFC V1 08/10] KVM: selftests: Make ucall work with encrypted guests". It was

Quoted 5 lines

...we're only updating x86's ucall_arch_get_ucall() to return gvas. What about the other architectures? Anyway, I'd rather we don't change ucall_arch_get_ucall() to return gvas. They should continue returning hvas and any trickery needed to translate a pool uc to an hva should be put inside ucall_arch_get_ucall().

Quoted 4 lines

(It's comforting to see I was consistent, at least wrt the last review.)

So, I would keep returning hvas and put the 'if use_ucall_pool' inside the ucall_arch_get_ucall() functions.

Makes sense. I'll have ucall_arch_get_ucall() return hvas and deal with the use_pool trickery. Sorry for not following that for this version I had just thought consolidating that logic would be cleaner.

Thanks for thorough review Drew!

Quoted 18 lines

Thanks,
drew

Quoted 14 lines

We can still update ucall_arch_get_ucall() to return a pointer type so that we can return NULL instead of 0.

Quoted 10 lines
Quoted 7 lines
tools/testing/selftests/kvm/lib/ucall_common.c +0 -1
      if (addr) {
              memcpy(uc, addr, sizeof(*uc));
              vcpu_run_complete_io(vcpu);
--

2.37.1.455.g008518b4e5-goog

Thanks,
drew

#21 lore raw

PATCH v2 10/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     |   1 +
 .../selftests/kvm/include/x86_64/sev.h        |  44 +++
 tools/testing/selftests/kvm/lib/x86_64/sev.c  | 251 ++++++++++++++++++
 4 files changed, 297 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 +1 -0
index ad4abc6be1ab..1172c4218ccc 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;
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.455.g008518b4e5-goog
#22 lore raw

PATCH v2 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/kvm_util_base.h     |   2 +
 .../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 ++++++++++++++++++
 6 files changed, 138 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/kvm_util_base.h +2 -0
index 1172c4218ccc..3c0ab83c5f69 100644
@@ -820,6 +820,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 gva2* conversions are not possible.");
 	return addr_arch_gva2gpa(vm, gva);
 }
 
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.455.g008518b4e5-goog