Skip to contentSkip to conversation

KVM: selftests: Guest printf and asserts overhaul

PATCH In review Sean Christopherson 2 participants 40 messages Jul 29, 2023 – Aug 2, 2023

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

v4 cover KVM: selftests: Guest printf and asserts overhaul

This is effectively v4 of Aaron's series to add printf capabilities to the guest[*]. I also pulled in Thomas' patch to rename ASSERT_EQ() to TEST_ASSERT_EQ(), mainly so that we can decide on a common output format for both host and guest asserts in a single series, but also so that all these basically treewide patches are contained in a single series.

Note, Aaron did all of the heavy lifting, I just mopped up. The core code is pretty much unchanged from Aaron's v3, v4 massages the assert code a bit and converts all the tests.

I initially did the conversion in one big patch (the flag is ugly), but after hitting a nasty bug (see "Add a shameful hack to preserve/clobber GPRs across ucall") that occurred purely due to the compiler using registers differently, I decided splitting it up was probably for the best (even though I still think it probably wasn't worth the effort).

Other maintainers, I want to get this into 6.6, hell or high water. The ability to use proper asserts in the guest is super nice, and given how much selftests code is written via CTRL-C + CTRL-V, the sooner we switch over the better.

My thought is to apply this whole thing to kvm-x86/selftests early next week, and then create a tag to make that part of branch immutable. That would allow other architectures to pull the code into their trees, e.g. if an arch is gaining a big selftest or something.

Apologies for not giving advance warning, I was originally thinking we could leisurely convert to the printf-based asserts, but then realized that we would probably never get rid of the old crud if we tried that approach.

Any objections, or better ideas?

Thanks!

Oh, and tested on Intel, AMD, and whatever flavor of ARM we have. Compile tested on s390 and RISC-V.

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

Aaron Lewis (5):
KVM: selftests: Add strnlen() to the string overrides
KVM: selftests: Add guest_snprintf() to KVM selftests
KVM: selftests: Add additional pages to the guest to accommodate ucall
KVM: selftests: Add string formatting options to ucall
KVM: selftests: Add a selftest for guest prints and formatted asserts

Sean Christopherson (28):
KVM: selftests: Make TEST_ASSERT_EQ() output look like normal
TEST_ASSERT()
KVM: selftests: Add a shameful hack to preserve/clobber GPRs across
ucall
KVM: selftests: Add formatted guest assert support in ucall framework
KVM: selftests: Convert aarch_timer to printf style GUEST_ASSERT
KVM: selftests: Convert debug-exceptions to printf style GUEST_ASSERT
KVM: selftests: Convert ARM's hypercalls test to printf style
GUEST_ASSERT
KVM: selftests: Convert ARM's page fault test to printf style
GUEST_ASSERT
KVM: selftests: Convert ARM's vGIC IRQ test to printf style
GUEST_ASSERT
KVM: selftests: Convert the memslot performance test to printf guest
asserts
KVM: selftests: Convert s390's memop test to printf style GUEST_ASSERT
KVM: selftests: Convert s390's tprot test to printf style GUEST_ASSERT
KVM: selftests: Convert set_memory_region_test to printf-based
GUEST_ASSERT
KVM: selftests: Convert steal_time test to printf style GUEST_ASSERT
KVM: selftests: Convert x86's CPUID test to printf style GUEST_ASSERT
KVM: selftests: Convert the Hyper-V extended hypercalls test to printf
asserts
KVM: selftests: Convert the Hyper-V feature test to printf style
GUEST_ASSERT
KVM: selftests: Convert x86's KVM paravirt test to printf style
GUEST_ASSERT
KVM: selftests: Convert the MONITOR/MWAIT test to use printf guest
asserts
KVM: selftests: Convert x86's nested exceptions test to printf guest
asserts
KVM: selftests: Convert x86's set BSP ID test to printf style guest
asserts
KVM: selftests: Convert the nSVM software interrupt test to printf
guest asserts
KVM: selftests: Convert x86's TSC MSRs test to use printf guest
asserts
KVM: selftests: Convert the x86 userspace I/O test to printf guest
assert
KVM: selftests: Convert VMX's PMU capabilities test to printf guest
asserts
KVM: selftests: Convert x86's XCR0 test to use printf-based guest
asserts
KVM: selftests: Rip out old, param-based guest assert macros
KVM: selftests: Print out guest RIP on unhandled exception
KVM: selftests: Use GUEST_FAIL() in ARM's arch timer helpers

Thomas Huth (1):
KVM: selftests: Rename the ASSERT_EQ macro

 tools/testing/selftests/kvm/Makefile          |   3 +
 .../selftests/kvm/aarch64/aarch32_id_regs.c   |   8 +-
 .../selftests/kvm/aarch64/arch_timer.c        |  22 +-
 .../selftests/kvm/aarch64/debug-exceptions.c  |   8 +-
 .../selftests/kvm/aarch64/hypercalls.c        |  20 +-
 .../selftests/kvm/aarch64/page_fault_test.c   |  17 +-
 .../testing/selftests/kvm/aarch64/vgic_irq.c  |   3 +-
 .../testing/selftests/kvm/guest_print_test.c  | 221 +++++++++++++
 .../kvm/include/aarch64/arch_timer.h          |  12 +-
 .../testing/selftests/kvm/include/test_util.h |  18 +-
 .../selftests/kvm/include/ucall_common.h      |  97 +++---
 .../testing/selftests/kvm/lib/guest_sprintf.c | 307 ++++++++++++++++++
 tools/testing/selftests/kvm/lib/kvm_util.c    |   6 +-
 .../selftests/kvm/lib/string_override.c       |   9 +
 .../testing/selftests/kvm/lib/ucall_common.c  |  44 +++
 .../selftests/kvm/lib/x86_64/processor.c      |  18 +-
 .../testing/selftests/kvm/lib/x86_64/ucall.c  |  32 +-
 .../selftests/kvm/max_guest_memory_test.c     |   2 +-
 .../testing/selftests/kvm/memslot_perf_test.c |   4 +-
 tools/testing/selftests/kvm/s390x/cmma_test.c |  62 ++--
 tools/testing/selftests/kvm/s390x/memop.c     |  13 +-
 tools/testing/selftests/kvm/s390x/tprot.c     |  11 +-
 .../selftests/kvm/set_memory_region_test.c    |  21 +-
 tools/testing/selftests/kvm/steal_time.c      |  20 +-
 .../testing/selftests/kvm/x86_64/cpuid_test.c |  12 +-
 .../x86_64/dirty_log_page_splitting_test.c    |  18 +-
 .../x86_64/exit_on_emulation_failure_test.c   |   2 +-
 .../kvm/x86_64/hyperv_extended_hypercalls.c   |   3 +-
 .../selftests/kvm/x86_64/hyperv_features.c    |  29 +-
 .../selftests/kvm/x86_64/kvm_pv_test.c        |   8 +-
 .../selftests/kvm/x86_64/monitor_mwait_test.c |  35 +-
 .../kvm/x86_64/nested_exceptions_test.c       |  16 +-
 .../kvm/x86_64/recalc_apic_map_test.c         |   6 +-
 .../selftests/kvm/x86_64/set_boot_cpu_id.c    |   6 +-
 .../kvm/x86_64/svm_nested_soft_inject_test.c  |  22 +-
 .../selftests/kvm/x86_64/tsc_msrs_test.c      |  34 +-
 .../selftests/kvm/x86_64/userspace_io_test.c  |  10 +-
 .../vmx_exception_with_invalid_guest_state.c  |   2 +-
 .../selftests/kvm/x86_64/vmx_pmu_caps_test.c  |  31 +-
 .../selftests/kvm/x86_64/xapic_state_test.c   |   8 +-
 .../selftests/kvm/x86_64/xcr0_cpuid_test.c    |  29 +-
 .../selftests/kvm/x86_64/xen_vmcall_test.c    |  20 +-
 42 files changed, 938 insertions(+), 331 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/guest_print_test.c
 create mode 100644 tools/testing/selftests/kvm/lib/guest_sprintf.c

base-commit: fdf0eaf11452d72945af31804e2a1048ee1b574c

Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#1 lore raw

PATCH v4 1/34 KVM: selftests: Rename the ASSERT_EQ macro

From: Thomas Huth <[email protected]>

There is already an ASSERT_EQ macro in the file tools/testing/selftests/kselftest_harness.h, so we currently can't include test_util.h from the KVM selftests together with that file. Rename the macro in the KVM selftests to TEST_ASSERT_EQ to avoid the problem - it is also more similar to the other macros in test_util.h that way.

---
 .../selftests/kvm/aarch64/aarch32_id_regs.c   |  8 +--
 .../selftests/kvm/aarch64/page_fault_test.c   | 10 +--
 .../testing/selftests/kvm/include/test_util.h |  4 +-
 tools/testing/selftests/kvm/lib/kvm_util.c    |  2 +-
 .../selftests/kvm/max_guest_memory_test.c     |  2 +-
 tools/testing/selftests/kvm/s390x/cmma_test.c | 62 +++++++++----------
 tools/testing/selftests/kvm/s390x/memop.c     |  6 +-
 tools/testing/selftests/kvm/s390x/tprot.c     |  4 +-
 .../x86_64/dirty_log_page_splitting_test.c    | 18 +++---
 .../x86_64/exit_on_emulation_failure_test.c   |  2 +-
 .../kvm/x86_64/nested_exceptions_test.c       | 12 ++--
 .../kvm/x86_64/recalc_apic_map_test.c         |  6 +-
 .../selftests/kvm/x86_64/tsc_msrs_test.c      | 32 +++++-----
 .../vmx_exception_with_invalid_guest_state.c  |  2 +-
 .../selftests/kvm/x86_64/vmx_pmu_caps_test.c  |  3 +-
 .../selftests/kvm/x86_64/xapic_state_test.c   |  8 +--
 .../selftests/kvm/x86_64/xen_vmcall_test.c    | 20 +++---
 17 files changed, 101 insertions(+), 100 deletions(-)
tools/testing/selftests/kvm/aarch64/aarch32_id_regs.c +4 -4
index 4951ac53d1f8..b90580840b22 100644
@@ -98,7 +98,7 @@ static void test_user_raz_wi(struct kvm_vcpu *vcpu)
 		uint64_t val;
 
 		vcpu_get_reg(vcpu, reg_id, &val);
-		ASSERT_EQ(val, 0);
+		TEST_ASSERT_EQ(val, 0);
 
 		/*
 		 * Expect the ioctl to succeed with no effect on the register
@@ -107,7 +107,7 @@ static void test_user_raz_wi(struct kvm_vcpu *vcpu)
 		vcpu_set_reg(vcpu, reg_id, BAD_ID_REG_VAL);
 
 		vcpu_get_reg(vcpu, reg_id, &val);
-		ASSERT_EQ(val, 0);
+		TEST_ASSERT_EQ(val, 0);
 	}
 }
 
@@ -127,14 +127,14 @@ static void test_user_raz_invariant(struct kvm_vcpu *vcpu)
 		uint64_t val;
 
 		vcpu_get_reg(vcpu, reg_id, &val);
-		ASSERT_EQ(val, 0);
+		TEST_ASSERT_EQ(val, 0);
 
 		r = __vcpu_set_reg(vcpu, reg_id, BAD_ID_REG_VAL);
 		TEST_ASSERT(r < 0 && errno == EINVAL,
 			    "unexpected KVM_SET_ONE_REG error: r=%d, errno=%d", r, errno);
 
 		vcpu_get_reg(vcpu, reg_id, &val);
-		ASSERT_EQ(val, 0);
+		TEST_ASSERT_EQ(val, 0);
 	}
 }
 
tools/testing/selftests/kvm/aarch64/page_fault_test.c +5 -5
index df10f1ffa20d..e5bb8767d2cb 100644
@@ -318,7 +318,7 @@ static int uffd_generic_handler(int uffd_mode, int uffd, struct uffd_msg *msg,
 
 	TEST_ASSERT(uffd_mode == UFFDIO_REGISTER_MODE_MISSING,
 		    "The only expected UFFD mode is MISSING");
-	ASSERT_EQ(addr, (uint64_t)args->hva);
+	TEST_ASSERT_EQ(addr, (uint64_t)args->hva);
 
 	pr_debug("uffd fault: addr=%p write=%d\n",
 		 (void *)addr, !!(flags & UFFD_PAGEFAULT_FLAG_WRITE));
@@ -432,7 +432,7 @@ static void mmio_on_test_gpa_handler(struct kvm_vm *vm, struct kvm_run *run)
 	region = vm_get_mem_region(vm, MEM_REGION_TEST_DATA);
 	hva = (void *)region->region.userspace_addr;
 
-	ASSERT_EQ(run->mmio.phys_addr, region->region.guest_phys_addr);
+	TEST_ASSERT_EQ(run->mmio.phys_addr, region->region.guest_phys_addr);
 
 	memcpy(hva, run->mmio.data, run->mmio.len);
 	events.mmio_exits += 1;
@@ -631,9 +631,9 @@ static void setup_default_handlers(struct test_desc *test)
 
 static void check_event_counts(struct test_desc *test)
 {
-	ASSERT_EQ(test->expected_events.uffd_faults, events.uffd_faults);
-	ASSERT_EQ(test->expected_events.mmio_exits, events.mmio_exits);
-	ASSERT_EQ(test->expected_events.fail_vcpu_runs, events.fail_vcpu_runs);
+	TEST_ASSERT_EQ(test->expected_events.uffd_faults, events.uffd_faults);
+	TEST_ASSERT_EQ(test->expected_events.mmio_exits, events.mmio_exits);
+	TEST_ASSERT_EQ(test->expected_events.fail_vcpu_runs, events.fail_vcpu_runs);
 }
 
 static void print_test_banner(enum vm_guest_mode mode, struct test_params *p)
tools/testing/selftests/kvm/include/test_util.h +2 -2
index a6e9f215ce70..e734e52d8a3a 100644
@@ -53,11 +53,11 @@ void test_assert(bool exp, const char *exp_str,
 #define TEST_ASSERT(e, fmt, ...) \
 	test_assert((e), #e, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
 
-#define ASSERT_EQ(a, b) do { \
+#define TEST_ASSERT_EQ(a, b) do { \
 	typeof(a) __a = (a); \
 	typeof(b) __b = (b); \
 	TEST_ASSERT(__a == __b, \
-		    "ASSERT_EQ(%s, %s) failed.\n" \
+		    "TEST_ASSERT_EQ(%s, %s) failed.\n" \
 		    "\t%s is %#lx\n" \
 		    "\t%s is %#lx", \
 		    #a, #b, #a, (unsigned long) __a, #b, (unsigned long) __b); \
tools/testing/selftests/kvm/lib/kvm_util.c +1 -1
index 9741a7ff6380..3170d7a4520b 100644
@@ -994,7 +994,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
 	if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
 		alignment = max(backing_src_pagesz, alignment);
 
-	ASSERT_EQ(guest_paddr, align_up(guest_paddr, backing_src_pagesz));
+	TEST_ASSERT_EQ(guest_paddr, align_up(guest_paddr, backing_src_pagesz));
 
 	/* Add enough memory to align up if necessary */
 	if (alignment > 1)
tools/testing/selftests/kvm/max_guest_memory_test.c +1 -1
index feaf2be20ff2..6628dc4dda89 100644
@@ -55,7 +55,7 @@ static void rendezvous_with_boss(void)
 static void run_vcpu(struct kvm_vcpu *vcpu)
 {
 	vcpu_run(vcpu);
-	ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE);
+	TEST_ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE);
 }
 
 static void *vcpu_worker(void *data)
tools/testing/selftests/kvm/s390x/cmma_test.c +31 -31

138 lines not on this page · raw message

tools/testing/selftests/kvm/s390x/memop.c +3 -3
index 8e4b94d7b8dd..de73dc030905 100644
@@ -281,8 +281,8 @@ enum stage {
 	if (uc.cmd == UCALL_ABORT) {					\
 		REPORT_GUEST_ASSERT_2(uc, "hints: %lu, %lu");		\
 	}								\
-	ASSERT_EQ(uc.cmd, UCALL_SYNC);					\
-	ASSERT_EQ(uc.args[1], __stage);					\
+	TEST_ASSERT_EQ(uc.cmd, UCALL_SYNC);				\
+	TEST_ASSERT_EQ(uc.args[1], __stage);				\
 })									\
 
 static void prepare_mem12(void)
@@ -808,7 +808,7 @@ static void test_termination(void)
 	HOST_SYNC(t.vcpu, STAGE_IDLED);
 	MOP(t.vm, ABSOLUTE, READ, &teid, sizeof(teid), GADDR(prefix + 168));
 	/* Bits 56, 60, 61 form a code, 0 being the only one allowing for termination */
-	ASSERT_EQ(teid & teid_mask, 0);
+	TEST_ASSERT_EQ(teid & teid_mask, 0);
 
 	kvm_vm_free(t.kvm_vm);
 }
tools/testing/selftests/kvm/s390x/tprot.c +2 -2
index a9a0b76e5fa4..40d3ea16c052 100644
@@ -191,8 +191,8 @@ static void guest_code(void)
 	get_ucall(__vcpu, &uc);					\
 	if (uc.cmd == UCALL_ABORT)				\
 		REPORT_GUEST_ASSERT_2(uc, "hints: %lu, %lu");	\
-	ASSERT_EQ(uc.cmd, UCALL_SYNC);				\
-	ASSERT_EQ(uc.args[1], __stage);				\
+	TEST_ASSERT_EQ(uc.cmd, UCALL_SYNC);			\
+	TEST_ASSERT_EQ(uc.args[1], __stage);			\
 })
 
 #define HOST_SYNC(vcpu, stage)			\
tools/testing/selftests/kvm/x86_64/dirty_log_page_splitting_test.c +9 -9
index beb7e2c10211..634c6bfcd572 100644
@@ -72,7 +72,7 @@ static void vcpu_worker(struct memstress_vcpu_args *vcpu_args)
 
 		vcpu_run(vcpu);
 
-		ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_SYNC);
+		TEST_ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_SYNC);
 
 		vcpu_last_completed_iteration[vcpu_idx] = current_iteration;
 
@@ -179,12 +179,12 @@ static void run_test(enum vm_guest_mode mode, void *unused)
 	 * with that capability.
 	 */
 	if (dirty_log_manual_caps) {
-		ASSERT_EQ(stats_clear_pass[0].hugepages, 0);
-		ASSERT_EQ(stats_clear_pass[0].pages_4k, total_4k_pages);
-		ASSERT_EQ(stats_dirty_logging_enabled.hugepages, stats_populated.hugepages);
+		TEST_ASSERT_EQ(stats_clear_pass[0].hugepages, 0);
+		TEST_ASSERT_EQ(stats_clear_pass[0].pages_4k, total_4k_pages);
+		TEST_ASSERT_EQ(stats_dirty_logging_enabled.hugepages, stats_populated.hugepages);
 	} else {
-		ASSERT_EQ(stats_dirty_logging_enabled.hugepages, 0);
-		ASSERT_EQ(stats_dirty_logging_enabled.pages_4k, total_4k_pages);
+		TEST_ASSERT_EQ(stats_dirty_logging_enabled.hugepages, 0);
+		TEST_ASSERT_EQ(stats_dirty_logging_enabled.pages_4k, total_4k_pages);
 	}
 
 	/*
@@ -192,9 +192,9 @@ static void run_test(enum vm_guest_mode mode, void *unused)
 	 * memory again, the page counts should be the same as they were
 	 * right after initial population of memory.
 	 */
-	ASSERT_EQ(stats_populated.pages_4k, stats_repopulated.pages_4k);
-	ASSERT_EQ(stats_populated.pages_2m, stats_repopulated.pages_2m);
-	ASSERT_EQ(stats_populated.pages_1g, stats_repopulated.pages_1g);
+	TEST_ASSERT_EQ(stats_populated.pages_4k, stats_repopulated.pages_4k);
+	TEST_ASSERT_EQ(stats_populated.pages_2m, stats_repopulated.pages_2m);
+	TEST_ASSERT_EQ(stats_populated.pages_1g, stats_repopulated.pages_1g);
 }
 
 static void help(char *name)
tools/testing/selftests/kvm/x86_64/exit_on_emulation_failure_test.c +1 -1
index e334844d6e1d..6c2e5e0ceb1f 100644
@@ -35,7 +35,7 @@ int main(int argc, char *argv[])
 	vcpu_run(vcpu);
 	handle_flds_emulation_failure_exit(vcpu);
 	vcpu_run(vcpu);
-	ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE);
+	TEST_ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE);
 
 	kvm_vm_free(vm);
 	return 0;
tools/testing/selftests/kvm/x86_64/nested_exceptions_test.c +6 -6
index 6502aa23c2f8..5f074a6da90c 100644
@@ -247,12 +247,12 @@ int main(int argc, char *argv[])
 
 	/* Verify the pending events comes back out the same as it went in. */
 	vcpu_events_get(vcpu, &events);
-	ASSERT_EQ(events.flags & KVM_VCPUEVENT_VALID_PAYLOAD,
-		  KVM_VCPUEVENT_VALID_PAYLOAD);
-	ASSERT_EQ(events.exception.pending, true);
-	ASSERT_EQ(events.exception.nr, SS_VECTOR);
-	ASSERT_EQ(events.exception.has_error_code, true);
-	ASSERT_EQ(events.exception.error_code, SS_ERROR_CODE);
+	TEST_ASSERT_EQ(events.flags & KVM_VCPUEVENT_VALID_PAYLOAD,
+			KVM_VCPUEVENT_VALID_PAYLOAD);
+	TEST_ASSERT_EQ(events.exception.pending, true);
+	TEST_ASSERT_EQ(events.exception.nr, SS_VECTOR);
+	TEST_ASSERT_EQ(events.exception.has_error_code, true);
+	TEST_ASSERT_EQ(events.exception.error_code, SS_ERROR_CODE);
 
 	/*
 	 * Run for real with the pending #SS, L1 should get a VM-Exit due to
tools/testing/selftests/kvm/x86_64/recalc_apic_map_test.c +3 -3
index 4c416ebe7d66..cbc92a862ea9 100644
@@ -57,7 +57,7 @@ int main(void)
 	for (i = 0; i < KVM_MAX_VCPUS; i++)
 		vcpu_set_msr(vcpus[i], MSR_IA32_APICBASE, LAPIC_X2APIC);
 
-	ASSERT_EQ(pthread_create(&thread, NULL, race, vcpus[0]), 0);
+	TEST_ASSERT_EQ(pthread_create(&thread, NULL, race, vcpus[0]), 0);
 
 	vcpuN = vcpus[KVM_MAX_VCPUS - 1];
 	for (t = time(NULL) + TIMEOUT; time(NULL) < t;) {
@@ -65,8 +65,8 @@ int main(void)
 		vcpu_set_msr(vcpuN, MSR_IA32_APICBASE, LAPIC_DISABLED);
 	}
 
-	ASSERT_EQ(pthread_cancel(thread), 0);
-	ASSERT_EQ(pthread_join(thread, NULL), 0);
+	TEST_ASSERT_EQ(pthread_cancel(thread), 0);
+	TEST_ASSERT_EQ(pthread_join(thread, NULL), 0);
 
 	kvm_vm_free(vm);
 
tools/testing/selftests/kvm/x86_64/tsc_msrs_test.c +16 -16
index c9f67702f657..9265965bd2cd 100644
@@ -103,39 +103,39 @@ int main(void)
 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
 
 	val = 0;
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
 
 	/* Guest: writes to MSR_IA32_TSC affect both MSRs.  */
 	run_vcpu(vcpu, 1);
 	val = 1ull * GUEST_STEP;
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
 
 	/* Guest: writes to MSR_IA32_TSC_ADJUST affect both MSRs.  */
 	run_vcpu(vcpu, 2);
 	val = 2ull * GUEST_STEP;
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
 
 	/*
 	 * Host: writes to MSR_IA32_TSC set the host-side offset
 	 * and therefore do not change MSR_IA32_TSC_ADJUST.
 	 */
 	vcpu_set_msr(vcpu, MSR_IA32_TSC, HOST_ADJUST + val);
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
 	run_vcpu(vcpu, 3);
 
 	/* Host: writes to MSR_IA32_TSC_ADJUST do not modify the TSC.  */
 	vcpu_set_msr(vcpu, MSR_IA32_TSC_ADJUST, UNITY * 123456);
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
-	ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_TSC_ADJUST), UNITY * 123456);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
+	TEST_ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_TSC_ADJUST), UNITY * 123456);
 
 	/* Restore previous value.  */
 	vcpu_set_msr(vcpu, MSR_IA32_TSC_ADJUST, val);
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
 
 	/*
 	 * Guest: writes to MSR_IA32_TSC_ADJUST do not destroy the
@@ -143,8 +143,8 @@ int main(void)
 	 */
 	run_vcpu(vcpu, 4);
 	val = 3ull * GUEST_STEP;
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
 
 	/*
 	 * Guest: writes to MSR_IA32_TSC affect both MSRs, so the host-side
@@ -152,8 +152,8 @@ int main(void)
 	 */
 	run_vcpu(vcpu, 5);
 	val = 4ull * GUEST_STEP;
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
-	ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val - HOST_ADJUST);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
+	TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val - HOST_ADJUST);
 
 	kvm_vm_free(vm);
 
tools/testing/selftests/kvm/x86_64/vmx_exception_with_invalid_guest_state.c +1 -1
index be0bdb8c6f78..a9b827c69f32 100644
@@ -50,7 +50,7 @@ static void set_timer(void)
 	timer.it_value.tv_sec  = 0;
 	timer.it_value.tv_usec = 200;
 	timer.it_interval = timer.it_value;
-	ASSERT_EQ(setitimer(ITIMER_REAL, &timer, NULL), 0);
+	TEST_ASSERT_EQ(setitimer(ITIMER_REAL, &timer, NULL), 0);
 }
 
 static void set_or_clear_invalid_guest_state(struct kvm_vcpu *vcpu, bool set)
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +2 -1
index 4c90f76930f9..34efd57c2b32 100644
@@ -103,7 +103,8 @@ static void test_guest_wrmsr_perf_capabilities(union perf_capabilities host_cap)
 		TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
 	}
 
-	ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabilities);
+	TEST_ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES),
+			host_cap.capabilities);
 
 	vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
 
tools/testing/selftests/kvm/x86_64/xapic_state_test.c +4 -4
index 396c13f42457..ab75b873a4ad 100644
@@ -65,17 +65,17 @@ static void ____test_icr(struct xapic_vcpu *x, uint64_t val)
 	vcpu_ioctl(vcpu, KVM_SET_LAPIC, &xapic);
 
 	vcpu_run(vcpu);
-	ASSERT_EQ(get_ucall(vcpu, &uc), UCALL_SYNC);
-	ASSERT_EQ(uc.args[1], val);
+	TEST_ASSERT_EQ(get_ucall(vcpu, &uc), UCALL_SYNC);
+	TEST_ASSERT_EQ(uc.args[1], val);
 
 	vcpu_ioctl(vcpu, KVM_GET_LAPIC, &xapic);
 	icr = (u64)(*((u32 *)&xapic.regs[APIC_ICR])) |
 	      (u64)(*((u32 *)&xapic.regs[APIC_ICR2])) << 32;
 	if (!x->is_x2apic) {
 		val &= (-1u | (0xffull << (32 + 24)));
-		ASSERT_EQ(icr, val & ~APIC_ICR_BUSY);
+		TEST_ASSERT_EQ(icr, val & ~APIC_ICR_BUSY);
 	} else {
-		ASSERT_EQ(icr & ~APIC_ICR_BUSY, val & ~APIC_ICR_BUSY);
+		TEST_ASSERT_EQ(icr & ~APIC_ICR_BUSY, val & ~APIC_ICR_BUSY);
 	}
 }
 
tools/testing/selftests/kvm/x86_64/xen_vmcall_test.c +10 -10
index c94cde3b523f..e149d0574961 100644
@@ -108,16 +108,16 @@ int main(int argc, char *argv[])
 		vcpu_run(vcpu);
 
 		if (run->exit_reason == KVM_EXIT_XEN) {
-			ASSERT_EQ(run->xen.type, KVM_EXIT_XEN_HCALL);
-			ASSERT_EQ(run->xen.u.hcall.cpl, 0);
-			ASSERT_EQ(run->xen.u.hcall.longmode, 1);
-			ASSERT_EQ(run->xen.u.hcall.input, INPUTVALUE);
-			ASSERT_EQ(run->xen.u.hcall.params[0], ARGVALUE(1));
-			ASSERT_EQ(run->xen.u.hcall.params[1], ARGVALUE(2));
-			ASSERT_EQ(run->xen.u.hcall.params[2], ARGVALUE(3));
-			ASSERT_EQ(run->xen.u.hcall.params[3], ARGVALUE(4));
-			ASSERT_EQ(run->xen.u.hcall.params[4], ARGVALUE(5));
-			ASSERT_EQ(run->xen.u.hcall.params[5], ARGVALUE(6));
+			TEST_ASSERT_EQ(run->xen.type, KVM_EXIT_XEN_HCALL);
+			TEST_ASSERT_EQ(run->xen.u.hcall.cpl, 0);
+			TEST_ASSERT_EQ(run->xen.u.hcall.longmode, 1);
+			TEST_ASSERT_EQ(run->xen.u.hcall.input, INPUTVALUE);
+			TEST_ASSERT_EQ(run->xen.u.hcall.params[0], ARGVALUE(1));
+			TEST_ASSERT_EQ(run->xen.u.hcall.params[1], ARGVALUE(2));
+			TEST_ASSERT_EQ(run->xen.u.hcall.params[2], ARGVALUE(3));
+			TEST_ASSERT_EQ(run->xen.u.hcall.params[3], ARGVALUE(4));
+			TEST_ASSERT_EQ(run->xen.u.hcall.params[4], ARGVALUE(5));
+			TEST_ASSERT_EQ(run->xen.u.hcall.params[5], ARGVALUE(6));
 			run->xen.u.hcall.result = RETVALUE;
 			continue;
 		}
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#2 lore raw

PATCH v4 2/34 KVM: selftests: Make TEST_ASSERT_EQ() output look like normal TEST_ASSERT()

In reply to Sean Christopherson

Clean up TEST_ASSERT_EQ() so that the (mostly) raw code is captured in the main assert message, not the helper macro's code. E.g. make this:

x86_64/tsc_msrs_test.c:106: __a == __b
pid=40470 tid=40470 errno=0 - Success

     1	0x000000000040170e: main at tsc_msrs_test.c:106
     2	0x0000000000416f23: __libc_start_call_main at libc-start.o:?
     3	0x000000000041856f: __libc_start_main_impl at ??:?
     4	0x0000000000401ef0: _start at ??:?
  TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val + 1) failed.
	rounded_host_rdmsr(MSR_IA32_TSC) is 0
	val + 1 is 0x1

look like this:

x86_64/tsc_msrs_test.c:106: rounded_host_rdmsr(MSR_IA32_TSC) == val + 1 pid=5737 tid=5737 errno=0 - Success

     1	0x0000000000401714: main at tsc_msrs_test.c:106
     2	0x0000000000415c23: __libc_start_call_main at libc-start.o:?
     3	0x000000000041726f: __libc_start_main_impl at ??:?
     4	0x0000000000401e60: _start at ??:?

0 != 0x1 (rounded_host_rdmsr(MSR_IA32_TSC) != val + 1)

Opportunstically clean up the formatting of the entire macro.

---
 tools/testing/selftests/kvm/include/test_util.h | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/test_util.h +7 -8
index e734e52d8a3a..a4bea44f990c 100644
@@ -53,14 +53,13 @@ void test_assert(bool exp, const char *exp_str,
 #define TEST_ASSERT(e, fmt, ...) \
 	test_assert((e), #e, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
 
-#define TEST_ASSERT_EQ(a, b) do { \
-	typeof(a) __a = (a); \
-	typeof(b) __b = (b); \
-	TEST_ASSERT(__a == __b, \
-		    "TEST_ASSERT_EQ(%s, %s) failed.\n" \
-		    "\t%s is %#lx\n" \
-		    "\t%s is %#lx", \
-		    #a, #b, #a, (unsigned long) __a, #b, (unsigned long) __b); \
+#define TEST_ASSERT_EQ(a, b)						\
+do {									\
+	typeof(a) __a = (a);						\
+	typeof(b) __b = (b);						\
+	test_assert(__a == __b, #a " == " #b, __FILE__, __LINE__,	\
+		    "%#lx != %#lx (%s != %s)",				\
+		    (unsigned long)(__a), (unsigned long)(__b), #a, #b);\
 } while (0)
 
 #define TEST_ASSERT_KVM_EXIT_REASON(vcpu, expected) do {		\
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#3 lore raw

PATCH v4 3/34 KVM: selftests: Add a shameful hack to preserve/clobber GPRs across ucall

In reply to Sean Christopherson

Preserve or clobber all GPRs (except RIP and RSP, as they're saved and restored via the VMCS) when performing a ucall on x86 to fudge around a horrific long-standing bug in selftests' nested VMX support where L2's GPRs are not preserved across a nested VM-Exit. I.e. if a test triggers a nested VM-Exit to L1 in response to a ucall, e.g. GUEST_SYNC(), then L2's GPR state can be corrupted.

The issues manifests as an unexpected #GP in clear_bit() when running the hyperv_evmcs test due to RBX being used to track the ucall object, and RBX being clobbered by the nested VM-Exit. The problematic hyperv_evmcs testcase is where L0 (test's host userspace) injects an NMI in response to GUEST_SYNC(8) from L2, but the bug could "randomly" manifest in any test that induces a nested VM-Exit from L0. The bug hasn't caused failures in the past due to sheer dumb luck.

The obvious fix is to rework the nVMX helpers to save/restore L2 GPRs across VM-Exit and VM-Enter, but that is a much bigger task and carries its own risks, e.g. nSVM does save/restore GPRs, but not in a thread-safe manner, and there is a _lot_ of cleanup that can be done to unify code for doing VM-Enter on nVMX, nSVM, and eVMCS.

---
 .../testing/selftests/kvm/lib/x86_64/ucall.c  | 32 +++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/ucall.c +30 -2
index 4d41dc63cc9e..a53df3ece2f8 100644
@@ -14,8 +14,36 @@ void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa)
 
 void ucall_arch_do_ucall(vm_vaddr_t uc)
 {
-	asm volatile("in %[port], %%al"
-		: : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax", "memory");
+	/*
+	 * FIXME: Revert this hack (the entire commit that added it) once nVMX
+	 * preserves L2 GPRs across a nested VM-Exit.  If a ucall from L2, e.g.
+	 * to do a GUEST_SYNC(), lands the vCPU in L1, any and all GPRs can be
+	 * clobbered by L1.  Save and restore non-volatile GPRs (clobbering RBP
+	 * in particular is problematic) along with RDX and RDI (which are
+	 * inputs), and clobber volatile GPRs. *sigh*
+	 */
+#define HORRIFIC_L2_UCALL_CLOBBER_HACK	\
+	"rcx", "rsi", "r8", "r9", "r10", "r11"
+
+	asm volatile("push %%rbp\n\t"
+		     "push %%r15\n\t"
+		     "push %%r14\n\t"
+		     "push %%r13\n\t"
+		     "push %%r12\n\t"
+		     "push %%rbx\n\t"
+		     "push %%rdx\n\t"
+		     "push %%rdi\n\t"
+		     "in %[port], %%al\n\t"
+		     "pop %%rdi\n\t"
+		     "pop %%rdx\n\t"
+		     "pop %%rbx\n\t"
+		     "pop %%r12\n\t"
+		     "pop %%r13\n\t"
+		     "pop %%r14\n\t"
+		     "pop %%r15\n\t"
+		     "pop %%rbp\n\t"
+		: : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax", "memory",
+		     HORRIFIC_L2_UCALL_CLOBBER_HACK);
 }
 
 void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#4 lore raw

PATCH v4 4/34 KVM: selftests: Add strnlen() to the string overrides

In reply to Sean Christopherson

From: Aaron Lewis <[email protected]>

Add strnlen() to the string overrides to allow it to be called in the guest.

The implementation for strnlen() was taken from the kernel's generic version, lib/string.c.

This will be needed when printf() is introduced.

---
 tools/testing/selftests/kvm/Makefile              | 1 +
 tools/testing/selftests/kvm/lib/string_override.c | 9 +++++++++
 2 files changed, 10 insertions(+)
tools/testing/selftests/kvm/Makefile +1 -0
index c692cc86e7da..f6c14ab476ac 100644
@@ -204,6 +204,7 @@ endif
 CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
 	-Wno-gnu-variable-sized-type-not-at-end -MD\
 	-fno-builtin-memcmp -fno-builtin-memcpy -fno-builtin-memset \
+	-fno-builtin-strnlen \
 	-fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \
 	-I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \
 	-I$(<D) -Iinclude/$(ARCH_DIR) -I ../rseq -I.. $(EXTRA_CFLAGS) \
tools/testing/selftests/kvm/lib/string_override.c +9 -0
index 632398adc229..5d1c87277c49 100644
@@ -37,3 +37,12 @@ void *memset(void *s, int c, size_t count)
 		*xs++ = c;
 	return s;
 }
+
+size_t strnlen(const char *s, size_t count)
+{
+	const char *sc;
+
+	for (sc = s; count-- && *sc != '\0'; ++sc)
+		/* nothing */;
+	return sc - s;
+}
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#5 lore raw

PATCH v4 5/34 KVM: selftests: Add guest_snprintf() to KVM selftests

In reply to Sean Christopherson

From: Aaron Lewis <[email protected]>

Add a local version of guest_snprintf() for use in the guest.

Having a local copy allows the guest access to string formatting options without dependencies on LIBC. LIBC is problematic because it heavily relies on both AVX-512 instructions and a TLS, neither of which are guaranteed to be set up in the guest.

The file guest_sprintf.c was lifted from arch/x86/boot/printf.c and adapted to work in the guest, including the addition of buffer length. I.e. s/sprintf/snprintf/

The functions where prefixed with "guest_" to allow guests to explicitly call them.

A string formatted by this function is expected to succeed or die. If something goes wrong during the formatting process a GUEST_ASSERT() will be thrown.

---
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../testing/selftests/kvm/include/test_util.h |   3 +
 .../testing/selftests/kvm/lib/guest_sprintf.c | 307 ++++++++++++++++++
 3 files changed, 311 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/lib/guest_sprintf.c
tools/testing/selftests/kvm/Makefile +1 -0
index f6c14ab476ac..f65889f5a083 100644
@@ -23,6 +23,7 @@ LIBKVM += lib/guest_modes.c
 LIBKVM += lib/io.c
 LIBKVM += lib/kvm_util.c
 LIBKVM += lib/memstress.c
+LIBKVM += lib/guest_sprintf.c
 LIBKVM += lib/rbtree.c
 LIBKVM += lib/sparsebit.c
 LIBKVM += lib/test_util.c
tools/testing/selftests/kvm/include/test_util.h +3 -0
index a4bea44f990c..7a5907da1719 100644
@@ -185,4 +185,7 @@ static inline uint32_t atoi_non_negative(const char *name, const char *num_str)
 	return num;
 }
 
+int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args);
+int guest_snprintf(char *buf, int n, const char *fmt, ...);
+
 #endif /* SELFTEST_KVM_TEST_UTIL_H */
tools/testing/selftests/kvm/lib/guest_sprintf.c new file +307 -0

307 lines not on this page · raw message

Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#6 lore raw

PATCH v4 6/34 KVM: selftests: Add additional pages to the guest to accommodate ucall

In reply to Sean Christopherson

From: Aaron Lewis <[email protected]>

Add additional pages to the guest to account for the number of pages the ucall headers need. The only reason things worked before is the ucall headers are fairly small. If they were ever to increase in size the guest could run out of memory.

This is done in preparation for adding string formatting options to the guest through the ucall framework which increases the size of the ucall headers.

Fixes 426729b2cf2e ("KVM: selftests: Add ucall pool based implementation")
---
 tools/testing/selftests/kvm/include/ucall_common.h | 1 +
 tools/testing/selftests/kvm/lib/kvm_util.c         | 4 ++++
 tools/testing/selftests/kvm/lib/ucall_common.c     | 5 +++++
 3 files changed, 10 insertions(+)
tools/testing/selftests/kvm/include/ucall_common.h +1 -0
index 1a6aaef5ccae..bcbb362aa77f 100644
@@ -34,6 +34,7 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);
 void ucall(uint64_t cmd, int nargs, ...);
 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
 void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa);
+int ucall_nr_pages_required(uint64_t page_size);
 
 /*
  * Perform userspace call without any associated data.  This bare call avoids
tools/testing/selftests/kvm/lib/kvm_util.c +4 -0
index 3170d7a4520b..7a8af1821f5d 100644
@@ -312,6 +312,7 @@ static uint64_t vm_nr_pages_required(enum vm_guest_mode mode,
 				     uint32_t nr_runnable_vcpus,
 				     uint64_t extra_mem_pages)
 {
+	uint64_t page_size = vm_guest_mode_params[mode].page_size;
 	uint64_t nr_pages;
 
 	TEST_ASSERT(nr_runnable_vcpus,
@@ -340,6 +341,9 @@ static uint64_t vm_nr_pages_required(enum vm_guest_mode mode,
 	 */
 	nr_pages += (nr_pages + extra_mem_pages) / PTES_PER_MIN_PAGE * 2;
 
+	/* Account for the number of pages needed by ucall. */
+	nr_pages += ucall_nr_pages_required(page_size);
+
 	return vm_adjust_num_guest_pages(mode, nr_pages);
 }
 
tools/testing/selftests/kvm/lib/ucall_common.c +5 -0
index 2f0e2ea941cc..77ada362273d 100644
@@ -11,6 +11,11 @@ struct ucall_header {
 	struct ucall ucalls[KVM_MAX_VCPUS];
 };
 
+int ucall_nr_pages_required(uint64_t page_size)
+{
+	return align_up(sizeof(struct ucall_header), page_size) / page_size;
+}
+
 /*
  * ucall_pool holds per-VM values (global data is duplicated by each VM), it
  * must not be accessed from host code.
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#7 lore raw

PATCH v4 7/34 KVM: selftests: Add string formatting options to ucall

In reply to Sean Christopherson

From: Aaron Lewis <[email protected]>

Add more flexibility to guest debugging and testing by adding GUEST_PRINTF() and GUEST_ASSERT_FMT() to the ucall framework.

Add a sized buffer to the ucall structure to hold the formatted string, i.e. to allow the guest to easily resolve the string, and thus avoid the ugly pattern of the host side having to make assumptions about the desired format, as well as having to pass around a large number of parameters.

The buffer size was chosen to accommodate most use cases, and based on similar usage. E.g. printf() uses the same size buffer in arch/x86/boot/printf.c. And 1KiB ought to be enough for anybody.

[sean: massage changelog, wrap macro param in ()]
---
 .../selftests/kvm/include/ucall_common.h        |  7 +++++++
 tools/testing/selftests/kvm/lib/ucall_common.c  | 17 +++++++++++++++++
 2 files changed, 24 insertions(+)
tools/testing/selftests/kvm/include/ucall_common.h +7 -0
index bcbb362aa77f..b5548aeba9f0 100644
@@ -13,15 +13,18 @@ enum {
 	UCALL_NONE,
 	UCALL_SYNC,
 	UCALL_ABORT,
+	UCALL_PRINTF,
 	UCALL_DONE,
 	UCALL_UNHANDLED,
 };
 
 #define UCALL_MAX_ARGS 7
+#define UCALL_BUFFER_LEN 1024
 
 struct ucall {
 	uint64_t cmd;
 	uint64_t args[UCALL_MAX_ARGS];
+	char buffer[UCALL_BUFFER_LEN];
 
 	/* Host virtual address of this struct. */
 	struct ucall *hva;
@@ -32,6 +35,7 @@ void ucall_arch_do_ucall(vm_vaddr_t uc);
 void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);
 
 void ucall(uint64_t cmd, int nargs, ...);
+void ucall_fmt(uint64_t cmd, const char *fmt, ...);
 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
 void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa);
 int ucall_nr_pages_required(uint64_t page_size);
@@ -47,8 +51,11 @@ int ucall_nr_pages_required(uint64_t page_size);
 #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)
+#define GUEST_PRINTF(_fmt, _args...) ucall_fmt(UCALL_PRINTF, _fmt, ##_args)
 #define GUEST_DONE()		ucall(UCALL_DONE, 0)
 
+#define REPORT_GUEST_PRINTF(ucall) pr_info("%s", (ucall).buffer)
+
 enum guest_assert_builtin_args {
 	GUEST_ERROR_STRING,
 	GUEST_FILE,
tools/testing/selftests/kvm/lib/ucall_common.c +17 -0
index 77ada362273d..b507db91139b 100644
@@ -75,6 +75,23 @@ static void ucall_free(struct ucall *uc)
 	clear_bit(uc - ucall_pool->ucalls, ucall_pool->in_use);
 }
 
+void ucall_fmt(uint64_t cmd, const char *fmt, ...)
+{
+	struct ucall *uc;
+	va_list va;
+
+	uc = ucall_alloc();
+	uc->cmd = cmd;
+
+	va_start(va, fmt);
+	guest_vsnprintf(uc->buffer, UCALL_BUFFER_LEN, fmt, va);
+	va_end(va);
+
+	ucall_arch_do_ucall((vm_vaddr_t)uc->hva);
+
+	ucall_free(uc);
+}
+
 void ucall(uint64_t cmd, int nargs, ...)
 {
 	struct ucall *uc;
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#8 lore raw

PATCH v4 8/34 KVM: selftests: Add formatted guest assert support in ucall framework

In reply to Sean Christopherson

Add printf-based GUEST_ASSERT macros and accompanying host-side support to provide an assert-specific versions of GUEST_PRINTF(). To make it easier to parse assert messages, for humans and bots alike, preserve/use the same layout as host asserts, e.g. in the example below, the reported expression, file, line number, and message are from the guest assertion, not the host reporting of the assertion.

The call stack still captures the host reporting, but capturing the guest stack is a less pressing concern, i.e. can be done in the future, and an optimal solution would capture *both* the host and guest stacks, i.e. capturing the host stack isn't an outright bug.

Running soft int test
==== Test Assertion Failure ====

    x86_64/svm_nested_soft_inject_test.c:39: regs->rip != (unsigned long)l2_guest_code_int
    pid=214104 tid=214104 errno=4 - Interrupted system call
       1	0x0000000000401b35: run_test at svm_nested_soft_inject_test.c:191
       2	0x00000000004017d2: main at svm_nested_soft_inject_test.c:212
       3	0x0000000000415b03: __libc_start_call_main at libc-start.o:?
       4	0x000000000041714f: __libc_start_main_impl at ??:?
       5	0x0000000000401660: _start at ??:?
    Expected IRQ at RIP 0x401e50, received IRQ at 0x401e50

Don't bother sharing code between ucall_assert() and ucall_fmt(), as forwarding the variable arguments would either require using macros or building a va_list, i.e. would make the code less readable and/or require just as much copy+paste code anyways.

Gate the new macros with a flag so that tests can more or less be switched over one-by-one. The slow conversion won't be perfect, e.g. library code won't pick up the flag, but the only asserts in library code are of the vanilla GUEST_ASSERT() variety, i.e. don't print out variables.

Add a temporary alias to GUEST_ASSERT_1() to fudge around ARM's arch_timer.h header using GUEST_ASSERT_1(), thus thwarting any attempt to convert tests one-by-one.

---
 .../selftests/kvm/include/ucall_common.h      | 48 +++++++++++++++++++
 .../testing/selftests/kvm/lib/ucall_common.c  | 22 +++++++++
 2 files changed, 70 insertions(+)
tools/testing/selftests/kvm/include/ucall_common.h +48 -0
index b5548aeba9f0..4ce11c15285a 100644
@@ -36,6 +36,8 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);
 
 void ucall(uint64_t cmd, int nargs, ...);
 void ucall_fmt(uint64_t cmd, const char *fmt, ...);
+void ucall_assert(uint64_t cmd, const char *exp, const char *file,
+		  unsigned int line, const char *fmt, ...);
 uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
 void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa);
 int ucall_nr_pages_required(uint64_t page_size);
@@ -63,6 +65,50 @@ enum guest_assert_builtin_args {
 	GUEST_ASSERT_BUILTIN_NARGS
 };
 
+#ifdef USE_GUEST_ASSERT_PRINTF
+#define ____GUEST_ASSERT(_condition, _exp, _fmt, _args...)				\
+do {											\
+	if (!(_condition))								\
+		ucall_assert(UCALL_ABORT, _exp, __FILE__, __LINE__, _fmt, ##_args);	\
+} while (0)
+
+#define __GUEST_ASSERT(_condition, _fmt, _args...)				\
+	____GUEST_ASSERT(_condition, #_condition, _fmt, ##_args)
+
+#define GUEST_ASSERT(_condition)						\
+	__GUEST_ASSERT(_condition, #_condition)
+
+#define GUEST_FAIL(_fmt, _args...)						\
+	ucall_assert(UCALL_ABORT, "Unconditional guest failure",		\
+		     __FILE__, __LINE__, _fmt, ##_args)
+
+#define GUEST_ASSERT_EQ(a, b)							\
+do {										\
+	typeof(a) __a = (a);							\
+	typeof(b) __b = (b);							\
+	____GUEST_ASSERT(__a == __b, #a " == " #b, "%#lx != %#lx (%s != %s)",	\
+			 (unsigned long)(__a), (unsigned long)(__b), #a, #b);	\
+} while (0)
+
+#define GUEST_ASSERT_NE(a, b)							\
+do {										\
+	typeof(a) __a = (a);							\
+	typeof(b) __b = (b);							\
+	____GUEST_ASSERT(__a != __b, #a " != " #b, "%#lx == %#lx (%s == %s)",	\
+			 (unsigned long)(__a), (unsigned long)(__b), #a, #b);	\
+} while (0)
+
+#define REPORT_GUEST_ASSERT(ucall)						\
+	test_assert(false, (const char *)(ucall).args[GUEST_ERROR_STRING],	\
+		    (const char *)(ucall).args[GUEST_FILE],			\
+		    (ucall).args[GUEST_LINE], "%s", (ucall).buffer)
+
+/* FIXME: Drop this alias once the param-based guest asserts are gone. */
+#define GUEST_ASSERT_1(_condition, arg1) \
+	__GUEST_ASSERT(_condition, "arg1 = 0x%lx", arg1)
+
+#else
+
 #define __GUEST_ASSERT(_condition, _condstr, _nargs, _args...)		\
 do {									\
 	if (!(_condition))						\
@@ -129,4 +175,6 @@ do {									\
 #define REPORT_GUEST_ASSERT_N(ucall, fmt, args...)	\
 	__REPORT_GUEST_ASSERT((ucall), fmt, ##args)
 
+#endif /* USE_GUEST_ASSERT_PRINTF */
+
 #endif /* SELFTEST_KVM_UCALL_COMMON_H */
tools/testing/selftests/kvm/lib/ucall_common.c +22 -0
index b507db91139b..816a3fa109bf 100644
@@ -75,6 +75,28 @@ static void ucall_free(struct ucall *uc)
 	clear_bit(uc - ucall_pool->ucalls, ucall_pool->in_use);
 }
 
+void ucall_assert(uint64_t cmd, const char *exp, const char *file,
+		  unsigned int line, const char *fmt, ...)
+{
+	struct ucall *uc;
+	va_list va;
+
+	uc = ucall_alloc();
+	uc->cmd = cmd;
+
+	WRITE_ONCE(uc->args[GUEST_ERROR_STRING], (uint64_t)(exp));
+	WRITE_ONCE(uc->args[GUEST_FILE], (uint64_t)(file));
+	WRITE_ONCE(uc->args[GUEST_LINE], line);
+
+	va_start(va, fmt);
+	guest_vsnprintf(uc->buffer, UCALL_BUFFER_LEN, fmt, va);
+	va_end(va);
+
+	ucall_arch_do_ucall((vm_vaddr_t)uc->hva);
+
+	ucall_free(uc);
+}
+
 void ucall_fmt(uint64_t cmd, const char *fmt, ...)
 {
 	struct ucall *uc;
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#9 lore raw

PATCH v4 9/34 KVM: selftests: Add a selftest for guest prints and formatted asserts

In reply to Sean Christopherson

From: Aaron Lewis <[email protected]>

Add a test to exercise the various features in KVM selftest's local snprintf() and compare them to LIBC's snprintf() to ensure they behave the same.

This is not an exhaustive test. KVM's local snprintf() does not implement all the features LIBC does, e.g. KVM's local snprintf() does not support floats or doubles, so testing for those features were excluded.

Testing was added for the features that are expected to work to support a minimal version of printf() in the guest.

---
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../testing/selftests/kvm/guest_print_test.c  | 223 ++++++++++++++++++
 2 files changed, 224 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/guest_print_test.c
tools/testing/selftests/kvm/Makefile +1 -0
index f65889f5a083..f2a8b3262f17 100644
@@ -123,6 +123,7 @@ TEST_GEN_PROGS_x86_64 += access_tracking_perf_test
 TEST_GEN_PROGS_x86_64 += demand_paging_test
 TEST_GEN_PROGS_x86_64 += dirty_log_test
 TEST_GEN_PROGS_x86_64 += dirty_log_perf_test
+TEST_GEN_PROGS_x86_64 += guest_print_test
 TEST_GEN_PROGS_x86_64 += hardware_disable_test
 TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
 TEST_GEN_PROGS_x86_64 += kvm_page_table_test
tools/testing/selftests/kvm/guest_print_test.c new file +223 -0

223 lines not on this page · raw message

Signature · 1 line
2.41.0.487.g6d72f3e995-goog
#10 lore raw

Re: [PATCH v4 09/34] KVM: selftests: Add a selftest for guest prints and formatted asserts

Quoted 14 lines · Sean Christopherson

From: Aaron Lewis <[email protected]>

Add a test to exercise the various features in KVM selftest's local snprintf() and compare them to LIBC's snprintf() to ensure they behave the same.

This is not an exhaustive test. KVM's local snprintf() does not implement all the features LIBC does, e.g. KVM's local snprintf() does not support floats or doubles, so testing for those features were excluded.

Testing was added for the features that are expected to work to support a minimal version of printf() in the guest.

Quoted 7 lines
---
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../testing/selftests/kvm/guest_print_test.c  | 223 ++++++++++++++++++
 2 files changed, 224 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/guest_print_test.c

I added this diff to this patch

tools/testing/selftests/kvm/guest_print_test.c +2 -2
index 3a9a5db9794e..602a23ea9f01 100644
@@ -115,7 +115,7 @@ static void run_test(struct kvm_vcpu *vcpu, const char *expected_printf,
        while (1) {
                vcpu_run(vcpu);
 
-               TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
+               TEST_ASSERT(run->exit_reason == UCALL_EXIT_REASON,
                            "Unexpected exit reason: %u (%s),\n",
                            run->exit_reason,
                            exit_reason_str(run->exit_reason));
@@ -161,7 +161,7 @@ static void test_limits(void)
        run = vcpu->run;
        vcpu_run(vcpu);
 
-       TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
+       TEST_ASSERT(run->exit_reason == UCALL_EXIT_REASON,
                    "Unexpected exit reason: %u (%s),\n",
                    run->exit_reason,
                    exit_reason_str(run->exit_reason));
tools/testing/selftests/kvm/include/ucall_common.h +11 -0
index 4cf69fa8bfba..4adf526dc378 100644
@@ -6,8 +6,19 @@
  */
 #ifndef SELFTEST_KVM_UCALL_COMMON_H
 #define SELFTEST_KVM_UCALL_COMMON_H
+#include <linux/kvm.h>
 #include "test_util.h"
 
+#if defined(__aarch64__)
+#define UCALL_EXIT_REASON      KVM_EXIT_MMIO
+#elif defined(__x86_64__)
+#define UCALL_EXIT_REASON      KVM_EXIT_IO
+#elif defined(__s390x__)
+#define UCALL_EXIT_REASON      KVM_EXIT_S390_SIEIC
+#elif defined(__riscv)
+#define UCALL_EXIT_REASON      KVM_EXIT_RISCV_SBI
+#endif
+
 /* Common ucalls */
 enum {
        UCALL_NONE,

and then compiled the test for riscv and it passed. I also ran all other riscv tests successfully.

Thanks,
drew

Quoted 245 lines
tools/testing/selftests/kvm/Makefile +1 -0
index f65889f5a083..f2a8b3262f17 100644
@@ -123,6 +123,7 @@ TEST_GEN_PROGS_x86_64 += access_tracking_perf_test
 TEST_GEN_PROGS_x86_64 += demand_paging_test
 TEST_GEN_PROGS_x86_64 += dirty_log_test
 TEST_GEN_PROGS_x86_64 += dirty_log_perf_test
+TEST_GEN_PROGS_x86_64 += guest_print_test
 TEST_GEN_PROGS_x86_64 += hardware_disable_test
 TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
 TEST_GEN_PROGS_x86_64 += kvm_page_table_test
tools/testing/selftests/kvm/guest_print_test.c new file +223 -0
new file mode 100644
index 000000000000..777838d42427
@@ -0,0 +1,223 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A test for GUEST_PRINTF
+ *
+ * Copyright 2022, Google, Inc. and/or its affiliates.
+ */
+#define USE_GUEST_ASSERT_PRINTF 1
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+
+struct guest_vals {
+	uint64_t a;
+	uint64_t b;
+	uint64_t type;
+};
+
+static struct guest_vals vals;
+
+/* GUEST_PRINTF()/GUEST_ASSERT_FMT() does not support float or double. */
+#define TYPE_LIST					\
+TYPE(test_type_i64,  I64,  "%ld",   int64_t)		\
+TYPE(test_type_u64,  U64u, "%lu",   uint64_t)		\
+TYPE(test_type_x64,  U64x, "0x%lx", uint64_t)		\
+TYPE(test_type_X64,  U64X, "0x%lX", uint64_t)		\
+TYPE(test_type_u32,  U32u, "%u",    uint32_t)		\
+TYPE(test_type_x32,  U32x, "0x%x",  uint32_t)		\
+TYPE(test_type_X32,  U32X, "0x%X",  uint32_t)		\
+TYPE(test_type_int,  INT,  "%d",    int)		\
+TYPE(test_type_char, CHAR, "%c",    char)		\
+TYPE(test_type_str,  STR,  "'%s'",  const char *)	\
+TYPE(test_type_ptr,  PTR,  "%p",    uintptr_t)
+
+enum args_type {
+#define TYPE(fn, ext, fmt_t, T) TYPE_##ext,
+	TYPE_LIST
+#undef TYPE
+};
+
+static void run_test(struct kvm_vcpu *vcpu, const char *expected_printf,
+		     const char *expected_assert);
+
+#define BUILD_TYPE_STRINGS_AND_HELPER(fn, ext, fmt_t, T)		     \
+const char *PRINTF_FMT_##ext = "Got params a = " fmt_t " and b = " fmt_t;    \
+const char *ASSERT_FMT_##ext = "Expected " fmt_t ", got " fmt_t " instead";  \
+static void fn(struct kvm_vcpu *vcpu, T a, T b)				     \
+{									     \
+	char expected_printf[UCALL_BUFFER_LEN];				     \
+	char expected_assert[UCALL_BUFFER_LEN];				     \
+									     \
+	snprintf(expected_printf, UCALL_BUFFER_LEN, PRINTF_FMT_##ext, a, b); \
+	snprintf(expected_assert, UCALL_BUFFER_LEN, ASSERT_FMT_##ext, a, b); \
+	vals = (struct guest_vals){ (uint64_t)a, (uint64_t)b, TYPE_##ext };  \
+	sync_global_to_guest(vcpu->vm, vals);				     \
+	run_test(vcpu, expected_printf, expected_assert);		     \
+}
+
+#define TYPE(fn, ext, fmt_t, T) \
+		BUILD_TYPE_STRINGS_AND_HELPER(fn, ext, fmt_t, T)
+	TYPE_LIST
+#undef TYPE
+
+static void guest_code(void)
+{
+	while (1) {
+		switch (vals.type) {
+#define TYPE(fn, ext, fmt_t, T)							\
+		case TYPE_##ext:						\
+			GUEST_PRINTF(PRINTF_FMT_##ext, vals.a, vals.b);		\
+			__GUEST_ASSERT(vals.a == vals.b,			\
+				       ASSERT_FMT_##ext, vals.a, vals.b);	\
+			break;
+		TYPE_LIST
+#undef TYPE
+		default:
+			GUEST_SYNC(vals.type);
+		}
+
+		GUEST_DONE();
+	}
+}
+
+/*
+ * Unfortunately this gets a little messy because 'assert_msg' doesn't
+ * just contains the matching string, it also contains additional assert
+ * info.  Fortunately the part that matches should be at the very end of
+ * 'assert_msg'.
+ */
+static void ucall_abort(const char *assert_msg, const char *expected_assert_msg)
+{
+	int len_str = strlen(assert_msg);
+	int len_substr = strlen(expected_assert_msg);
+	int offset = len_str - len_substr;
+
+	TEST_ASSERT(len_substr <= len_str,
+		    "Expected '%s' to be a substring of '%s'\n",
+		    assert_msg, expected_assert_msg);
+
+	TEST_ASSERT(strcmp(&assert_msg[offset], expected_assert_msg) == 0,
+		    "Unexpected mismatch. Expected: '%s', got: '%s'",
+		    expected_assert_msg, &assert_msg[offset]);
+}
+
+static void run_test(struct kvm_vcpu *vcpu, const char *expected_printf,
+		     const char *expected_assert)
+{
+	struct kvm_run *run = vcpu->run;
+	struct ucall uc;
+
+	while (1) {
+		vcpu_run(vcpu);
+
+		TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
+			    "Unexpected exit reason: %u (%s),\n",
+			    run->exit_reason,
+			    exit_reason_str(run->exit_reason));
+
+		switch (get_ucall(vcpu, &uc)) {
+		case UCALL_SYNC:
+			TEST_FAIL("Unknown 'args_type' = %lu", uc.args[1]);
+			break;
+		case UCALL_PRINTF:
+			TEST_ASSERT(strcmp(uc.buffer, expected_printf) == 0,
+				    "Unexpected mismatch. Expected: '%s', got: '%s'",
+				    expected_printf, uc.buffer);
+			break;
+		case UCALL_ABORT:
+			ucall_abort(uc.buffer, expected_assert);
+			break;
+		case UCALL_DONE:
+			return;
+		default:
+			TEST_FAIL("Unknown ucall %lu", uc.cmd);
+		}
+	}
+}
+
+static void guest_code_limits(void)
+{
+	char test_str[UCALL_BUFFER_LEN + 10];
+
+	memset(test_str, 'a', sizeof(test_str));
+	test_str[sizeof(test_str) - 1] = 0;
+
+	GUEST_PRINTF("%s", test_str);
+}
+
+static void test_limits(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_run *run;
+	struct kvm_vm *vm;
+	struct ucall uc;
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code_limits);
+	run = vcpu->run;
+	vcpu_run(vcpu);
+
+	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
+		    "Unexpected exit reason: %u (%s),\n",
+		    run->exit_reason,
+		    exit_reason_str(run->exit_reason));
+
+	TEST_ASSERT(get_ucall(vcpu, &uc) == UCALL_ABORT,
+		    "Unexpected ucall command: %lu,  Expected: %u (UCALL_ABORT)\n",
+		    uc.cmd, UCALL_ABORT);
+
+	kvm_vm_free(vm);
+}
+
+int main(int argc, char *argv[])
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+
+	test_type_i64(vcpu, -1, -1);
+	test_type_i64(vcpu, -1,  1);
+	test_type_i64(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);
+	test_type_i64(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);
+
+	test_type_u64(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);
+	test_type_u64(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);
+	test_type_x64(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);
+	test_type_x64(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);
+	test_type_X64(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);
+	test_type_X64(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);
+
+	test_type_u32(vcpu, 0x90abcdef, 0x90abcdef);
+	test_type_u32(vcpu, 0x90abcdef, 0x90abcdee);
+	test_type_x32(vcpu, 0x90abcdef, 0x90abcdef);
+	test_type_x32(vcpu, 0x90abcdef, 0x90abcdee);
+	test_type_X32(vcpu, 0x90abcdef, 0x90abcdef);
+	test_type_X32(vcpu, 0x90abcdef, 0x90abcdee);
+
+	test_type_int(vcpu, -1, -1);
+	test_type_int(vcpu, -1,  1);
+	test_type_int(vcpu,  1,  1);
+
+	test_type_char(vcpu, 'a', 'a');
+	test_type_char(vcpu, 'a', 'A');
+	test_type_char(vcpu, 'a', 'b');
+
+	test_type_str(vcpu, "foo", "foo");
+	test_type_str(vcpu, "foo", "bar");
+
+	test_type_ptr(vcpu, 0x1234567890abcdef, 0x1234567890abcdef);
+	test_type_ptr(vcpu, 0x1234567890abcdef, 0x1234567890abcdee);
+
+	kvm_vm_free(vm);
+
+	test_limits();
+
+	return 0;
+}
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#11 lore raw

Re: [PATCH v4 09/34] KVM: selftests: Add a selftest for guest prints and formatted asserts

Quoted 72 lines · Andrew Jones
Quoted 14 lines · Sean Christopherson

From: Aaron Lewis <[email protected]>

Add a test to exercise the various features in KVM selftest's local snprintf() and compare them to LIBC's snprintf() to ensure they behave the same.

This is not an exhaustive test. KVM's local snprintf() does not implement all the features LIBC does, e.g. KVM's local snprintf() does not support floats or doubles, so testing for those features were excluded.

Testing was added for the features that are expected to work to support a minimal version of printf() in the guest.

Quoted 7 lines
---
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../testing/selftests/kvm/guest_print_test.c  | 223 ++++++++++++++++++
 2 files changed, 224 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/guest_print_test.c

I added this diff to this patch

tools/testing/selftests/kvm/guest_print_test.c +2 -2
index 3a9a5db9794e..602a23ea9f01 100644
@@ -115,7 +115,7 @@ static void run_test(struct kvm_vcpu *vcpu, const char *expected_printf,
        while (1) {
                vcpu_run(vcpu);
 
-               TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
+               TEST_ASSERT(run->exit_reason == UCALL_EXIT_REASON,
                            "Unexpected exit reason: %u (%s),\n",
                            run->exit_reason,
                            exit_reason_str(run->exit_reason));
@@ -161,7 +161,7 @@ static void test_limits(void)
        run = vcpu->run;
        vcpu_run(vcpu);
 
-       TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
+       TEST_ASSERT(run->exit_reason == UCALL_EXIT_REASON,
                    "Unexpected exit reason: %u (%s),\n",
                    run->exit_reason,
                    exit_reason_str(run->exit_reason));
tools/testing/selftests/kvm/include/ucall_common.h +11 -0
index 4cf69fa8bfba..4adf526dc378 100644
@@ -6,8 +6,19 @@
  */
 #ifndef SELFTEST_KVM_UCALL_COMMON_H
 #define SELFTEST_KVM_UCALL_COMMON_H
+#include <linux/kvm.h>
 #include "test_util.h"
 
+#if defined(__aarch64__)
+#define UCALL_EXIT_REASON      KVM_EXIT_MMIO
+#elif defined(__x86_64__)
+#define UCALL_EXIT_REASON      KVM_EXIT_IO
+#elif defined(__s390x__)
+#define UCALL_EXIT_REASON      KVM_EXIT_S390_SIEIC
+#elif defined(__riscv)
+#define UCALL_EXIT_REASON      KVM_EXIT_RISCV_SBI
+#endif
+
 /* Common ucalls */
 enum {
        UCALL_NONE,
Quoted 2 lines

and then compiled the test for riscv and it passed. I also ran all other riscv tests successfully.

Can I have your SoB for the ucall_common.h patch? I'll write a changelog and fold in a separate prep patch for that change.

Quoted 9 lines

tools/testing/selftests/kvm/Makefile TEST_GEN_PROGS_x86_64 += access_tracking_perf_test · line 126

Quoted 9 lines
tools/testing/selftests/kvm/Makefile +1 -0
index f65889f5a083..f2a8b3262f17 100644
@@ -123,6 +123,7 @@ TEST_GEN_PROGS_x86_64 += access_tracking_perf_test
 TEST_GEN_PROGS_x86_64 += demand_paging_test
 TEST_GEN_PROGS_x86_64 += dirty_log_test
 TEST_GEN_PROGS_x86_64 += dirty_log_perf_test
+TEST_GEN_PROGS_x86_64 += guest_print_test

Argh, this is why ARM didn't fail for me, the test was only built for x86. I'll double check that ARM works with the above, and also enable the test for all architectures. If the printf stuff doesn't work on s390, then we definitely want to know before this is fully merged.

Thanks Drew!

author reviewer
#12 lore raw

Re: [PATCH v4 09/34] KVM: selftests: Add a selftest for guest prints and formatted asserts

Quoted 26 lines · Sean Christopherson
Quoted 25 lines · Andrew Jones
tools/testing/selftests/kvm/include/ucall_common.h +11 -0
index 4cf69fa8bfba..4adf526dc378 100644
@@ -6,8 +6,19 @@
  */
 #ifndef SELFTEST_KVM_UCALL_COMMON_H
 #define SELFTEST_KVM_UCALL_COMMON_H
+#include <linux/kvm.h>
 #include "test_util.h"
 
+#if defined(__aarch64__)
+#define UCALL_EXIT_REASON      KVM_EXIT_MMIO
+#elif defined(__x86_64__)
+#define UCALL_EXIT_REASON      KVM_EXIT_IO
+#elif defined(__s390x__)
+#define UCALL_EXIT_REASON      KVM_EXIT_S390_SIEIC
+#elif defined(__riscv)
+#define UCALL_EXIT_REASON      KVM_EXIT_RISCV_SBI
+#endif
+
 /* Common ucalls */
 enum {
        UCALL_NONE,
Quoted 5 lines
Quoted 2 lines

and then compiled the test for riscv and it passed. I also ran all other riscv tests successfully.

Can I have your SoB for the ucall_common.h patch? I'll write a changelog and fold in a separate prep patch for that change.

On second thought, I take that back. I think it makes more sense to add a ucall.h for each arch and #define the exit type there. All then move all of the ucall_arch_do_ucall() implementations to ucall.h (except maybe x86 while it has the horrific save/restore GPRs hack...). That way the #define is colocated with the code that generates the exit reason.

#13 lore raw

Re: [PATCH v4 09/34] KVM: selftests: Add a selftest for guest prints and formatted asserts

Quoted 33 lines · Sean Christopherson
Quoted 26 lines · Sean Christopherson
Quoted 25 lines · Andrew Jones
tools/testing/selftests/kvm/include/ucall_common.h +11 -0
index 4cf69fa8bfba..4adf526dc378 100644
@@ -6,8 +6,19 @@
  */
 #ifndef SELFTEST_KVM_UCALL_COMMON_H
 #define SELFTEST_KVM_UCALL_COMMON_H
+#include <linux/kvm.h>
 #include "test_util.h"
 
+#if defined(__aarch64__)
+#define UCALL_EXIT_REASON      KVM_EXIT_MMIO
+#elif defined(__x86_64__)
+#define UCALL_EXIT_REASON      KVM_EXIT_IO
+#elif defined(__s390x__)
+#define UCALL_EXIT_REASON      KVM_EXIT_S390_SIEIC
+#elif defined(__riscv)
+#define UCALL_EXIT_REASON      KVM_EXIT_RISCV_SBI
+#endif
+
 /* Common ucalls */
 enum {
        UCALL_NONE,
Quoted 5 lines
Quoted 2 lines

and then compiled the test for riscv and it passed. I also ran all other riscv tests successfully.

Can I have your SoB for the ucall_common.h patch? I'll write a changelog and fold in a separate prep patch for that change.

Quoted 5 lines

On second thought, I take that back. I think it makes more sense to add a ucall.h for each arch and #define the exit type there. All then move all of the ucall_arch_do_ucall() implementations to ucall.h (except maybe x86 while it has the horrific save/restore GPRs hack...). That way the #define is colocated with the code that generates the exit reason.

Yup, feel free to just take the above as inspiration and create a different patch. If you decide you want an s-o-b for the above, though, then here's one

Thanks,
drew

author reviewer
#14 lore raw

PATCH v4 10/34 KVM: selftests: Convert aarch_timer to printf style GUEST_ASSERT

In reply to Sean Christopherson

Convert ARM's aarch_timer test to use printf-based GUEST_ASSERT(). To maintain existing functionality, manually print the host information, e.g. stage and iteration, to stderr prior to reporting the guest assert.

---
 .../selftests/kvm/aarch64/arch_timer.c        | 22 +++++++++----------
 1 file changed, 10 insertions(+), 12 deletions(-)
tools/testing/selftests/kvm/aarch64/arch_timer.c +10 -12
index 8ef370924a02..b53bcf126e6a 100644
@@ -19,6 +19,7 @@
  *
  * Copyright (c) 2021, Google LLC.
  */
+#define USE_GUEST_ASSERT_PRINTF 1
 
 #define _GNU_SOURCE
 
@@ -155,11 +156,13 @@ static void guest_validate_irq(unsigned int intid,
 	xcnt_diff_us = cycles_to_usec(xcnt - shared_data->xcnt);
 
 	/* Make sure we are dealing with the correct timer IRQ */
-	GUEST_ASSERT_2(intid == timer_irq, intid, timer_irq);
+	GUEST_ASSERT_EQ(intid, timer_irq);
 
 	/* Basic 'timer condition met' check */
-	GUEST_ASSERT_3(xcnt >= cval, xcnt, cval, xcnt_diff_us);
-	GUEST_ASSERT_1(xctl & CTL_ISTATUS, xctl);
+	__GUEST_ASSERT(xcnt >= cval,
+		       "xcnt = 0x%llx, cval = 0x%llx, xcnt_diff_us = 0x%llx",
+		       xcnt, cval, xcnt_diff_us);
+	__GUEST_ASSERT(xctl & CTL_ISTATUS, "xcnt = 0x%llx", xcnt);
 
 	WRITE_ONCE(shared_data->nr_iter, shared_data->nr_iter + 1);
 }
@@ -192,8 +195,7 @@ static void guest_run_stage(struct test_vcpu_shared_data *shared_data,
 			TIMER_TEST_ERR_MARGIN_US);
 
 		irq_iter = READ_ONCE(shared_data->nr_iter);
-		GUEST_ASSERT_2(config_iter + 1 == irq_iter,
-				config_iter + 1, irq_iter);
+		GUEST_ASSERT_EQ(config_iter + 1, irq_iter);
 	}
 }
 
@@ -243,13 +245,9 @@ static void *test_vcpu_run(void *arg)
 		break;
 	case UCALL_ABORT:
 		sync_global_from_guest(vm, *shared_data);
-		REPORT_GUEST_ASSERT_N(uc, "values: %lu, %lu; %lu, vcpu %u; stage; %u; iter: %u",
-				      GUEST_ASSERT_ARG(uc, 0),
-				      GUEST_ASSERT_ARG(uc, 1),
-				      GUEST_ASSERT_ARG(uc, 2),
-				      vcpu_idx,
-				      shared_data->guest_stage,
-				      shared_data->nr_iter);
+		fprintf(stderr, "Guest assert failed,  vcpu %u; stage; %u; iter: %u\n",
+			vcpu_idx, shared_data->guest_stage, shared_data->nr_iter);
+		REPORT_GUEST_ASSERT(uc);
 		break;
 	default:
 		TEST_FAIL("Unexpected guest exit\n");
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#15 lore raw

PATCH v4 11/34 KVM: selftests: Convert debug-exceptions to printf style GUEST_ASSERT

In reply to Sean Christopherson

Convert ARM's debug exceptions test to use printf-based GUEST_ASSERT(). Opportunistically Use GUEST_ASSERT_EQ() in guest_code_ss() so that the expected vs. actual values get printed out.

---
 tools/testing/selftests/kvm/aarch64/debug-exceptions.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/aarch64/debug-exceptions.c +6 -4
index 637be796086f..fdd5b05e1b0e 100644
@@ -1,4 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
+#define USE_GUEST_ASSERT_PRINTF 1
+
 #include <test_util.h>
 #include <kvm_util.h>
 #include <processor.h>
@@ -365,7 +367,7 @@ static void guest_wp_handler(struct ex_regs *regs)
 
 static void guest_ss_handler(struct ex_regs *regs)
 {
-	GUEST_ASSERT_1(ss_idx < 4, ss_idx);
+	__GUEST_ASSERT(ss_idx < 4, "Expected index < 4, got '%u'", ss_idx);
 	ss_addr[ss_idx++] = regs->pc;
 	regs->pstate |= SPSR_SS;
 }
@@ -410,8 +412,8 @@ static void guest_code_ss(int test_cnt)
 		/* Userspace disables Single Step when the end is nigh. */
 		asm volatile("iter_ss_end:\n");
 
-		GUEST_ASSERT(bvr == w_bvr);
-		GUEST_ASSERT(wvr == w_wvr);
+		GUEST_ASSERT_EQ(bvr, w_bvr);
+		GUEST_ASSERT_EQ(wvr, w_wvr);
 	}
 	GUEST_DONE();
 }
@@ -450,7 +452,7 @@ static void test_guest_debug_exceptions(uint8_t bpn, uint8_t wpn, uint8_t ctx_bp
 	vcpu_run(vcpu);
 	switch (get_ucall(vcpu, &uc)) {
 	case UCALL_ABORT:
-		REPORT_GUEST_ASSERT_2(uc, "values: %#lx, %#lx");
+		REPORT_GUEST_ASSERT(uc);
 		break;
 	case UCALL_DONE:
 		goto done;
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#16 lore raw

PATCH v4 12/34 KVM: selftests: Convert ARM's hypercalls test to printf style GUEST_ASSERT

In reply to Sean Christopherson

Convert ARM's hypercalls test to use printf-based GUEST_ASSERT(). Opportunistically use GUEST_FAIL() to complain about an unexpected stage.

---
 .../selftests/kvm/aarch64/hypercalls.c        | 20 +++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/aarch64/hypercalls.c +10 -10
index bef1499fb465..94555a7d3c7e 100644
@@ -8,6 +8,7 @@
  * hypercalls are properly masked or unmasked to the guest when disabled or
  * enabled from the KVM userspace, respectively.
  */
+#define USE_GUEST_ASSERT_PRINTF 1
 
 #include <errno.h>
 #include <linux/arm-smccc.h>
@@ -105,15 +106,17 @@ static void guest_test_hvc(const struct test_hvc_info *hc_info)
 		switch (stage) {
 		case TEST_STAGE_HVC_IFACE_FEAT_DISABLED:
 		case TEST_STAGE_HVC_IFACE_FALSE_INFO:
-			GUEST_ASSERT_3(res.a0 == SMCCC_RET_NOT_SUPPORTED,
-					res.a0, hc_info->func_id, hc_info->arg1);
+			__GUEST_ASSERT(res.a0 == SMCCC_RET_NOT_SUPPORTED,
+				       "a0 = 0x%lx, func_id = 0x%x, arg1 = 0x%llx, stage = %u",
+					res.a0, hc_info->func_id, hc_info->arg1, stage);
 			break;
 		case TEST_STAGE_HVC_IFACE_FEAT_ENABLED:
-			GUEST_ASSERT_3(res.a0 != SMCCC_RET_NOT_SUPPORTED,
-					res.a0, hc_info->func_id, hc_info->arg1);
+			__GUEST_ASSERT(res.a0 != SMCCC_RET_NOT_SUPPORTED,
+				       "a0 = 0x%lx, func_id = 0x%x, arg1 = 0x%llx, stage = %u",
+					res.a0, hc_info->func_id, hc_info->arg1, stage);
 			break;
 		default:
-			GUEST_ASSERT_1(0, stage);
+			GUEST_FAIL("Unexpected stage = %u", stage);
 		}
 	}
 }
@@ -132,7 +135,7 @@ static void guest_code(void)
 			guest_test_hvc(false_hvc_info);
 			break;
 		default:
-			GUEST_ASSERT_1(0, stage);
+			GUEST_FAIL("Unexpected stage = %u", stage);
 		}
 
 		GUEST_SYNC(stage);
@@ -290,10 +293,7 @@ static void test_run(void)
 			guest_done = true;
 			break;
 		case UCALL_ABORT:
-			REPORT_GUEST_ASSERT_N(uc, "values: 0x%lx, 0x%lx; 0x%lx, stage: %u",
-					      GUEST_ASSERT_ARG(uc, 0),
-					      GUEST_ASSERT_ARG(uc, 1),
-					      GUEST_ASSERT_ARG(uc, 2), stage);
+			REPORT_GUEST_ASSERT(uc);
 			break;
 		default:
 			TEST_FAIL("Unexpected guest exit\n");
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#17 lore raw

PATCH v4 13/34 KVM: selftests: Convert ARM's page fault test to printf style GUEST_ASSERT

In reply to Sean Christopherson

Use GUEST_FAIL() in ARM's page fault test to report unexpected faults.

---
 tools/testing/selftests/kvm/aarch64/page_fault_test.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/aarch64/page_fault_test.c +4 -3
index e5bb8767d2cb..0b0dd90feae5 100644
@@ -7,6 +7,7 @@
  * hugetlbfs with a hole). It checks that the expected handling method is
  * called (e.g., uffd faults with the right address and write/read flag).
  */
+#define USE_GUEST_ASSERT_PRINTF 1
 
 #define _GNU_SOURCE
 #include <linux/bitmap.h>
@@ -293,12 +294,12 @@ static void guest_code(struct test_desc *test)
 
 static void no_dabt_handler(struct ex_regs *regs)
 {
-	GUEST_ASSERT_1(false, read_sysreg(far_el1));
+	GUEST_FAIL("Unexpected dabt, far_el1 = 0x%llx", read_sysreg(far_el1));
 }
 
 static void no_iabt_handler(struct ex_regs *regs)
 {
-	GUEST_ASSERT_1(false, regs->pc);
+	GUEST_FAIL("Unexpected iabt, pc = 0x%lx", regs->pc);
 }
 
 static struct uffd_args {
@@ -679,7 +680,7 @@ static void vcpu_run_loop(struct kvm_vm *vm, struct kvm_vcpu *vcpu,
 			}
 			break;
 		case UCALL_ABORT:
-			REPORT_GUEST_ASSERT_2(uc, "values: %#lx, %#lx");
+			REPORT_GUEST_ASSERT(uc);
 			break;
 		case UCALL_DONE:
 			goto done;
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#18 lore raw

PATCH v4 14/34 KVM: selftests: Convert ARM's vGIC IRQ test to printf style GUEST_ASSERT

In reply to Sean Christopherson

Use printf-based guest assert reporting in ARM's vGIC IRQ test. Note, this is not as innocuous as it looks! The printf-based version of GUEST_ASSERT_EQ() ensures the expressions are evaluated only once, whereas the old version did not!

---
 tools/testing/selftests/kvm/aarch64/vgic_irq.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/aarch64/vgic_irq.c +2 -1
index 90d854e0fcff..67da33aa6d17 100644
@@ -7,6 +7,7 @@
  * host to inject a specific intid via a GUEST_SYNC call, and then checks that
  * it received it.
  */
+#define USE_GUEST_ASSERT_PRINTF 1
 
 #include <asm/kvm.h>
 #include <asm/kvm_para.h>
@@ -781,7 +782,7 @@ static void test_vgic(uint32_t nr_irqs, bool level_sensitive, bool eoi_split)
 			run_guest_cmd(vcpu, gic_fd, &inject_args, &args);
 			break;
 		case UCALL_ABORT:
-			REPORT_GUEST_ASSERT_2(uc, "values: %#lx, %#lx");
+			REPORT_GUEST_ASSERT(uc);
 			break;
 		case UCALL_DONE:
 			goto done;
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#19 lore raw

PATCH v4 15/34 KVM: selftests: Convert the memslot performance test to printf guest asserts

In reply to Sean Christopherson

Use the printf-based GUEST_ASSERT_EQ() in the memslot perf test instead of an half-baked open code version.

---
 tools/testing/selftests/kvm/memslot_perf_test.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/memslot_perf_test.c +4 -2
index 4210cd21d159..55f1bc70e571 100644
@@ -6,6 +6,8 @@
  *
  * Basic guest setup / host vCPU thread code lifted from set_memory_region_test.
  */
+#define USE_GUEST_ASSERT_PRINTF 1
+
 #include <pthread.h>
 #include <sched.h>
 #include <semaphore.h>
@@ -157,7 +159,7 @@ static void *vcpu_worker(void *__data)
 				goto done;
 			break;
 		case UCALL_ABORT:
-			REPORT_GUEST_ASSERT_1(uc, "val = %lu");
+			REPORT_GUEST_ASSERT(uc);
 			break;
 		case UCALL_DONE:
 			goto done;
@@ -560,7 +562,7 @@ static void guest_code_test_memslot_rw(void)
 		     ptr < MEM_TEST_GPA + MEM_TEST_SIZE; ptr += page_size) {
 			uint64_t val = *(uint64_t *)ptr;
 
-			GUEST_ASSERT_1(val == MEM_TEST_VAL_2, val);
+			GUEST_ASSERT_EQ(val, MEM_TEST_VAL_2);
 			*(uint64_t *)ptr = 0;
 		}
 
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#20 lore raw

PATCH v4 16/34 KVM: selftests: Convert s390's memop test to printf style GUEST_ASSERT

In reply to Sean Christopherson

Convert s390's memop test to printf-based GUEST_ASSERT, and opportunistically use GUEST_FAIL() to report invalid sizes.

---
 tools/testing/selftests/kvm/s390x/memop.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/s390x/memop.c +4 -3
index de73dc030905..a49173907cec 100644
@@ -4,6 +4,7 @@
  *
  * Copyright (C) 2019, Red Hat, Inc.
  */
+#define USE_GUEST_ASSERT_PRINTF 1
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -279,7 +280,7 @@ enum stage {
 	vcpu_run(__vcpu);						\
 	get_ucall(__vcpu, &uc);						\
 	if (uc.cmd == UCALL_ABORT) {					\
-		REPORT_GUEST_ASSERT_2(uc, "hints: %lu, %lu");		\
+		REPORT_GUEST_ASSERT(uc);				\
 	}								\
 	TEST_ASSERT_EQ(uc.cmd, UCALL_SYNC);				\
 	TEST_ASSERT_EQ(uc.args[1], __stage);				\
@@ -469,7 +470,7 @@ static __uint128_t cut_to_size(int size, __uint128_t val)
 	case 16:
 		return val;
 	}
-	GUEST_ASSERT_1(false, "Invalid size");
+	GUEST_FAIL("Invalid size = %u", size);
 	return 0;
 }
 
@@ -598,7 +599,7 @@ static bool _cmpxchg(int size, void *target, __uint128_t *old_addr, __uint128_t
 			return ret;
 		}
 	}
-	GUEST_ASSERT_1(false, "Invalid size");
+	GUEST_FAIL("Invalid size = %u", size);
 	return 0;
 }
 
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#21 lore raw

PATCH v4 17/34 KVM: selftests: Convert s390's tprot test to printf style GUEST_ASSERT

In reply to Sean Christopherson

Convert s390's tprot test to printf-based GUEST_ASSERT.

---
 tools/testing/selftests/kvm/s390x/tprot.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/s390x/tprot.c +5 -2
index 40d3ea16c052..c12c6824d963 100644
@@ -4,6 +4,7 @@
  *
  * Copyright IBM Corp. 2021
  */
+#define USE_GUEST_ASSERT_PRINTF 1
 
 #include <sys/mman.h>
 #include "test_util.h"
@@ -156,7 +157,9 @@ static enum stage perform_next_stage(int *i, bool mapped_0)
 		       !mapped_0;
 		if (!skip) {
 			result = test_protection(tests[*i].addr, tests[*i].key);
-			GUEST_ASSERT_2(result == tests[*i].expected, *i, result);
+			__GUEST_ASSERT(result == tests[*i].expected,
+				       "Wanted %u, got %u, for i = %u",
+				       tests[*i].expected, result, *i);
 		}
 	}
 	return stage;
@@ -190,7 +193,7 @@ static void guest_code(void)
 	vcpu_run(__vcpu);					\
 	get_ucall(__vcpu, &uc);					\
 	if (uc.cmd == UCALL_ABORT)				\
-		REPORT_GUEST_ASSERT_2(uc, "hints: %lu, %lu");	\
+		REPORT_GUEST_ASSERT(uc);			\
 	TEST_ASSERT_EQ(uc.cmd, UCALL_SYNC);			\
 	TEST_ASSERT_EQ(uc.args[1], __stage);			\
 })
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#22 lore raw

PATCH v4 18/34 KVM: selftests: Convert set_memory_region_test to printf-based GUEST_ASSERT

In reply to Sean Christopherson

Convert set_memory_region_test to print-based GUEST_ASSERT, using a combo of newfangled macros to report (hopefully) useful information.

---
 .../selftests/kvm/set_memory_region_test.c    | 23 +++++++++++--------
 1 file changed, 14 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/set_memory_region_test.c +14 -9
index a849ce23ca97..dd8f4bac9df8 100644
@@ -1,4 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
+#define USE_GUEST_ASSERT_PRINTF 1
+
 #define _GNU_SOURCE /* for program_invocation_short_name */
 #include <fcntl.h>
 #include <pthread.h>
@@ -88,7 +90,7 @@ static void *vcpu_worker(void *data)
 	}
 
 	if (run->exit_reason == KVM_EXIT_IO && cmd == UCALL_ABORT)
-		REPORT_GUEST_ASSERT_1(uc, "val = %lu");
+		REPORT_GUEST_ASSERT(uc);
 
 	return NULL;
 }
@@ -156,19 +158,22 @@ static void guest_code_move_memory_region(void)
 	 * window where the memslot is invalid is usually quite small.
 	 */
 	val = guest_spin_on_val(0);
-	GUEST_ASSERT_1(val == 1 || val == MMIO_VAL, val);
+	__GUEST_ASSERT(val == 1 || val == MMIO_VAL,
+		       "Expected '1' or MMIO ('%llx'), got '%llx'", MMIO_VAL, val);
 
 	/* Spin until the misaligning memory region move completes. */
 	val = guest_spin_on_val(MMIO_VAL);
-	GUEST_ASSERT_1(val == 1 || val == 0, val);
+	__GUEST_ASSERT(val == 1 || val == 0,
+		       "Expected '0' or '1' (no MMIO), got '%llx'", val);
 
 	/* Spin until the memory region starts to get re-aligned. */
 	val = guest_spin_on_val(0);
-	GUEST_ASSERT_1(val == 1 || val == MMIO_VAL, val);
+	__GUEST_ASSERT(val == 1 || val == MMIO_VAL,
+		       "Expected '1' or MMIO ('%llx'), got '%llx'", MMIO_VAL, val);
 
 	/* Spin until the re-aligning memory region move completes. */
 	val = guest_spin_on_val(MMIO_VAL);
-	GUEST_ASSERT_1(val == 1, val);
+	GUEST_ASSERT_EQ(val, 1);
 
 	GUEST_DONE();
 }
@@ -224,15 +229,15 @@ static void guest_code_delete_memory_region(void)
 
 	/* Spin until the memory region is deleted. */
 	val = guest_spin_on_val(0);
-	GUEST_ASSERT_1(val == MMIO_VAL, val);
+	GUEST_ASSERT_EQ(val, MMIO_VAL);
 
 	/* Spin until the memory region is recreated. */
 	val = guest_spin_on_val(MMIO_VAL);
-	GUEST_ASSERT_1(val == 0, val);
+	GUEST_ASSERT_EQ(val, 0);
 
 	/* Spin until the memory region is deleted. */
 	val = guest_spin_on_val(0);
-	GUEST_ASSERT_1(val == MMIO_VAL, val);
+	GUEST_ASSERT_EQ(val, MMIO_VAL);
 
 	asm("1:\n\t"
 	    ".pushsection .rodata\n\t"
@@ -249,7 +254,7 @@ static void guest_code_delete_memory_region(void)
 	    "final_rip_end: .quad 1b\n\t"
 	    ".popsection");
 
-	GUEST_ASSERT_1(0, 0);
+	GUEST_ASSERT(0);
 }
 
 static void test_delete_memory_region(void)
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#23 lore raw

PATCH v4 19/34 KVM: selftests: Convert steal_time test to printf style GUEST_ASSERT

In reply to Sean Christopherson

Convert the steal_time test to use printf-based GUEST_ASERT. Opportunistically use GUEST_ASSERT_EQ() and GUEST_ASSERT_NE() so that the test spits out debug information on failure.

---
 tools/testing/selftests/kvm/steal_time.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/steal_time.c +12 -10
index c87f38712073..8649c8545882 100644
@@ -4,6 +4,8 @@
  *
  * Copyright (C) 2020, Red Hat, Inc.
  */
+#define USE_GUEST_ASSERT_PRINTF 1
+
 #define _GNU_SOURCE
 #include <stdio.h>
 #include <time.h>
@@ -31,8 +33,8 @@ static uint64_t guest_stolen_time[NR_VCPUS];
 static void check_status(struct kvm_steal_time *st)
 {
 	GUEST_ASSERT(!(READ_ONCE(st->version) & 1));
-	GUEST_ASSERT(READ_ONCE(st->flags) == 0);
-	GUEST_ASSERT(READ_ONCE(st->preempted) == 0);
+	GUEST_ASSERT_EQ(READ_ONCE(st->flags), 0);
+	GUEST_ASSERT_EQ(READ_ONCE(st->preempted), 0);
 }
 
 static void guest_code(int cpu)
@@ -40,7 +42,7 @@ static void guest_code(int cpu)
 	struct kvm_steal_time *st = st_gva[cpu];
 	uint32_t version;
 
-	GUEST_ASSERT(rdmsr(MSR_KVM_STEAL_TIME) == ((uint64_t)st_gva[cpu] | KVM_MSR_ENABLED));
+	GUEST_ASSERT_EQ(rdmsr(MSR_KVM_STEAL_TIME), ((uint64_t)st_gva[cpu] | KVM_MSR_ENABLED));
 
 	memset(st, 0, sizeof(*st));
 	GUEST_SYNC(0);
@@ -122,8 +124,8 @@ static int64_t smccc(uint32_t func, uint64_t arg)
 
 static void check_status(struct st_time *st)
 {
-	GUEST_ASSERT(READ_ONCE(st->rev) == 0);
-	GUEST_ASSERT(READ_ONCE(st->attr) == 0);
+	GUEST_ASSERT_EQ(READ_ONCE(st->rev), 0);
+	GUEST_ASSERT_EQ(READ_ONCE(st->attr), 0);
 }
 
 static void guest_code(int cpu)
@@ -132,15 +134,15 @@ static void guest_code(int cpu)
 	int64_t status;
 
 	status = smccc(SMCCC_ARCH_FEATURES, PV_TIME_FEATURES);
-	GUEST_ASSERT(status == 0);
+	GUEST_ASSERT_EQ(status, 0);
 	status = smccc(PV_TIME_FEATURES, PV_TIME_FEATURES);
-	GUEST_ASSERT(status == 0);
+	GUEST_ASSERT_EQ(status, 0);
 	status = smccc(PV_TIME_FEATURES, PV_TIME_ST);
-	GUEST_ASSERT(status == 0);
+	GUEST_ASSERT_EQ(status, 0);
 
 	status = smccc(PV_TIME_ST, 0);
-	GUEST_ASSERT(status != -1);
-	GUEST_ASSERT(status == (ulong)st_gva[cpu]);
+	GUEST_ASSERT_NE(status, -1);
+	GUEST_ASSERT_EQ(status, (ulong)st_gva[cpu]);
 
 	st = (struct st_time *)status;
 	GUEST_SYNC(0);
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#24 lore raw

PATCH v4 20/34 KVM: selftests: Convert x86's CPUID test to printf style GUEST_ASSERT

In reply to Sean Christopherson

Convert x86's CPUID test to use printf-based GUEST_ASSERT_EQ() so that the test prints out debug information. Note, the test previously used REPORT_GUEST_ASSERT_2(), but that was pointless because none of the guest-side code passed any parameters to the assert.

---
 tools/testing/selftests/kvm/x86_64/cpuid_test.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/x86_64/cpuid_test.c +8 -6
index d3c3aa93f090..eb1b65ffc0d5 100644
@@ -4,6 +4,8 @@
  *
  * Generic tests for KVM CPUID set/get ioctls
  */
+#define USE_GUEST_ASSERT_PRINTF 1
+
 #include <asm/kvm_para.h>
 #include <linux/kvm_para.h>
 #include <stdint.h>
@@ -35,10 +37,10 @@ static void test_guest_cpuids(struct kvm_cpuid2 *guest_cpuid)
 			guest_cpuid->entries[i].index,
 			&eax, &ebx, &ecx, &edx);
 
-		GUEST_ASSERT(eax == guest_cpuid->entries[i].eax &&
-			     ebx == guest_cpuid->entries[i].ebx &&
-			     ecx == guest_cpuid->entries[i].ecx &&
-			     edx == guest_cpuid->entries[i].edx);
+		GUEST_ASSERT_EQ(eax, guest_cpuid->entries[i].eax);
+		GUEST_ASSERT_EQ(ebx, guest_cpuid->entries[i].ebx);
+		GUEST_ASSERT_EQ(ecx, guest_cpuid->entries[i].ecx);
+		GUEST_ASSERT_EQ(edx, guest_cpuid->entries[i].edx);
 	}
 
 }
@@ -51,7 +53,7 @@ static void guest_main(struct kvm_cpuid2 *guest_cpuid)
 
 	GUEST_SYNC(2);
 
-	GUEST_ASSERT(this_cpu_property(X86_PROPERTY_MAX_KVM_LEAF) == 0x40000001);
+	GUEST_ASSERT_EQ(this_cpu_property(X86_PROPERTY_MAX_KVM_LEAF), 0x40000001);
 
 	GUEST_DONE();
 }
@@ -116,7 +118,7 @@ static void run_vcpu(struct kvm_vcpu *vcpu, int stage)
 	case UCALL_DONE:
 		return;
 	case UCALL_ABORT:
-		REPORT_GUEST_ASSERT_2(uc, "values: %#lx, %#lx");
+		REPORT_GUEST_ASSERT(uc);
 	default:
 		TEST_ASSERT(false, "Unexpected exit: %s",
 			    exit_reason_str(vcpu->run->exit_reason));
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#25 lore raw

PATCH v4 21/34 KVM: selftests: Convert the Hyper-V extended hypercalls test to printf asserts

In reply to Sean Christopherson

Convert x86's Hyper-V extended hypercalls test to use printf-based GUEST_ASSERT_EQ().

---
 .../testing/selftests/kvm/x86_64/hyperv_extended_hypercalls.c  | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/x86_64/hyperv_extended_hypercalls.c +2 -1
index 73af44d2167f..0107d54a1a08 100644
@@ -8,6 +8,7 @@
  * Copyright 2022 Google LLC
  * Author: Vipin Sharma <[email protected]>
  */
+#define USE_GUEST_ASSERT_PRINTF 1
 
 #include "kvm_util.h"
 #include "processor.h"
@@ -84,7 +85,7 @@ int main(void)
 
 	switch (get_ucall(vcpu, &uc)) {
 	case UCALL_ABORT:
-		REPORT_GUEST_ASSERT_2(uc, "arg1 = %ld, arg2 = %ld");
+		REPORT_GUEST_ASSERT(uc);
 		break;
 	case UCALL_DONE:
 		break;
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#26 lore raw

PATCH v4 22/34 KVM: selftests: Convert the Hyper-V feature test to printf style GUEST_ASSERT

In reply to Sean Christopherson

Convert x86's Hyper-V feature test to use print-based guest asserts. Opportunistically use the EQ and NE variants in a few places to capture additional information.

---
 .../selftests/kvm/x86_64/hyperv_features.c    | 31 +++++++++++++------
 1 file changed, 21 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/x86_64/hyperv_features.c +21 -10
index 78606de9385d..41a6beff78c4 100644
@@ -4,6 +4,8 @@
  *
  * Tests for Hyper-V features enablement
  */
+#define USE_GUEST_ASSERT_PRINTF 1
+
 #include <asm/kvm_para.h>
 #include <linux/kvm_para.h>
 #include <stdint.h>
@@ -53,16 +55,21 @@ static void guest_msr(struct msr_data *msr)
 		vector = rdmsr_safe(msr->idx, &msr_val);
 
 	if (msr->fault_expected)
-		GUEST_ASSERT_3(vector == GP_VECTOR, msr->idx, vector, GP_VECTOR);
+		__GUEST_ASSERT(vector == GP_VECTOR,
+			       "Expected #GP on %sMSR(0x%x), got vector '0x%x'",
+			       msr->idx, msr->write ? "WR" : "RD", vector);
 	else
-		GUEST_ASSERT_3(!vector, msr->idx, vector, 0);
+		__GUEST_ASSERT(!vector,
+			       "Expected success on %sMSR(0x%x), got vector '0x%x'",
+			       msr->idx, msr->write ? "WR" : "RD", vector);
 
 	if (vector || is_write_only_msr(msr->idx))
 		goto done;
 
 	if (msr->write)
-		GUEST_ASSERT_3(msr_val == msr->write_val, msr->idx,
-			       msr_val, msr->write_val);
+		__GUEST_ASSERT(!vector,
+			       "WRMSR(0x%x) to '0x%llx', RDMSR read '0x%llx'",
+			       msr->idx, msr->write_val, msr_val);
 
 	/* Invariant TSC bit appears when TSC invariant control MSR is written to */
 	if (msr->idx == HV_X64_MSR_TSC_INVARIANT_CONTROL) {
@@ -82,7 +89,7 @@ static void guest_hcall(vm_vaddr_t pgs_gpa, struct hcall_data *hcall)
 	u64 res, input, output;
 	uint8_t vector;
 
-	GUEST_ASSERT(hcall->control);
+	GUEST_ASSERT_NE(hcall->control, 0);
 
 	wrmsr(HV_X64_MSR_GUEST_OS_ID, HYPERV_LINUX_OS_ID);
 	wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa);
@@ -96,10 +103,14 @@ static void guest_hcall(vm_vaddr_t pgs_gpa, struct hcall_data *hcall)
 
 	vector = __hyperv_hypercall(hcall->control, input, output, &res);
 	if (hcall->ud_expected) {
-		GUEST_ASSERT_2(vector == UD_VECTOR, hcall->control, vector);
+		__GUEST_ASSERT(vector == UD_VECTOR,
+			       "Expected #UD for control '%u', got vector '0x%x'",
+			       hcall->control, vector);
 	} else {
-		GUEST_ASSERT_2(!vector, hcall->control, vector);
-		GUEST_ASSERT_2(res == hcall->expect, hcall->expect, res);
+		__GUEST_ASSERT(!vector,
+			       "Expected no exception for control '%u', got vector '0x%x'",
+			       hcall->control, vector);
+		GUEST_ASSERT_EQ(res, hcall->expect);
 	}
 
 	GUEST_DONE();
@@ -495,7 +506,7 @@ static void guest_test_msrs_access(void)
 
 		switch (get_ucall(vcpu, &uc)) {
 		case UCALL_ABORT:
-			REPORT_GUEST_ASSERT_3(uc, "MSR = %lx, arg1 = %lx, arg2 = %lx");
+			REPORT_GUEST_ASSERT(uc);
 			return;
 		case UCALL_DONE:
 			break;
@@ -665,7 +676,7 @@ static void guest_test_hcalls_access(void)
 
 		switch (get_ucall(vcpu, &uc)) {
 		case UCALL_ABORT:
-			REPORT_GUEST_ASSERT_2(uc, "arg1 = %lx, arg2 = %lx");
+			REPORT_GUEST_ASSERT(uc);
 			return;
 		case UCALL_DONE:
 			break;
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#27 lore raw

PATCH v4 23/34 KVM: selftests: Convert x86's KVM paravirt test to printf style GUEST_ASSERT

In reply to Sean Christopherson

Convert x86's KVM paravirtualization test to use the printf-based GUEST_ASSERT_EQ().

---
 tools/testing/selftests/kvm/x86_64/kvm_pv_test.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/x86_64/kvm_pv_test.c +6 -4
index f774a9e62858..1c28b77ff3cd 100644
@@ -4,6 +4,8 @@
  *
  * Tests for KVM paravirtual feature disablement
  */
+#define USE_GUEST_ASSERT_PRINTF 1
+
 #include <asm/kvm_para.h>
 #include <linux/kvm_para.h>
 #include <stdint.h>
@@ -46,10 +48,10 @@ static void test_msr(struct msr_data *msr)
 	PR_MSR(msr);
 
 	vector = rdmsr_safe(msr->idx, &ignored);
-	GUEST_ASSERT_1(vector == GP_VECTOR, vector);
+	GUEST_ASSERT_EQ(vector, GP_VECTOR);
 
 	vector = wrmsr_safe(msr->idx, 0);
-	GUEST_ASSERT_1(vector == GP_VECTOR, vector);
+	GUEST_ASSERT_EQ(vector, GP_VECTOR);
 }
 
 struct hcall_data {
@@ -77,7 +79,7 @@ static void test_hcall(struct hcall_data *hc)
 
 	PR_HCALL(hc);
 	r = kvm_hypercall(hc->nr, 0, 0, 0, 0);
-	GUEST_ASSERT(r == -KVM_ENOSYS);
+	GUEST_ASSERT_EQ(r, -KVM_ENOSYS);
 }
 
 static void guest_main(void)
@@ -125,7 +127,7 @@ static void enter_guest(struct kvm_vcpu *vcpu)
 			pr_hcall(&uc);
 			break;
 		case UCALL_ABORT:
-			REPORT_GUEST_ASSERT_1(uc, "vector = %lu");
+			REPORT_GUEST_ASSERT(uc);
 			return;
 		case UCALL_DONE:
 			return;
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#28 lore raw

PATCH v4 24/34 KVM: selftests: Convert the MONITOR/MWAIT test to use printf guest asserts

In reply to Sean Christopherson

Convert x86's MONITOR/MWAIT test to use printf-based guest asserts. Add a macro to handle reporting failures to reduce the amount of copy+paste needed for MONITOR vs. MWAIT.

---
 .../selftests/kvm/x86_64/monitor_mwait_test.c | 37 +++++++++++--------
 1 file changed, 22 insertions(+), 15 deletions(-)
tools/testing/selftests/kvm/x86_64/monitor_mwait_test.c +22 -15
index 72812644d7f5..960fecab3742 100644
@@ -1,4 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
+#define USE_GUEST_ASSERT_PRINTF 1
+
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -16,14 +18,25 @@ enum monitor_mwait_testcases {
 	MWAIT_DISABLED = BIT(2),
 };
 
+/*
+ * If both MWAIT and its quirk are disabled, MONITOR/MWAIT should #UD, in all
+ * other scenarios KVM should emulate them as nops.
+ */
+#define GUEST_ASSERT_MONITOR_MWAIT(insn, testcase, vector)		\
+do {									\
+	bool fault_wanted = ((testcase) & MWAIT_QUIRK_DISABLED) &&	\
+			    ((testcase) & MWAIT_DISABLED);		\
+									\
+	if (fault_wanted)						\
+		__GUEST_ASSERT((vector) == UD_VECTOR,			\
+			       "Expected #UD on " insn " for testcase '0x%x', got '0x%x'", vector); \
+	else								\
+		__GUEST_ASSERT(!(vector),				\
+			       "Expected success on " insn " for testcase '0x%x', got '0x%x'", vector); \
+} while (0)
+
 static void guest_monitor_wait(int testcase)
 {
-	/*
-	 * If both MWAIT and its quirk are disabled, MONITOR/MWAIT should #UD,
-	 * in all other scenarios KVM should emulate them as nops.
-	 */
-	bool fault_wanted = (testcase & MWAIT_QUIRK_DISABLED) &&
-			    (testcase & MWAIT_DISABLED);
 	u8 vector;
 
 	GUEST_SYNC(testcase);
@@ -33,16 +46,10 @@ static void guest_monitor_wait(int testcase)
 	 * intercept checks, so the inputs for MONITOR and MWAIT must be valid.
 	 */
 	vector = kvm_asm_safe("monitor", "a"(guest_monitor_wait), "c"(0), "d"(0));
-	if (fault_wanted)
-		GUEST_ASSERT_2(vector == UD_VECTOR, testcase, vector);
-	else
-		GUEST_ASSERT_2(!vector, testcase, vector);
+	GUEST_ASSERT_MONITOR_MWAIT("MONITOR", testcase, vector);
 
 	vector = kvm_asm_safe("mwait", "a"(guest_monitor_wait), "c"(0), "d"(0));
-	if (fault_wanted)
-		GUEST_ASSERT_2(vector == UD_VECTOR, testcase, vector);
-	else
-		GUEST_ASSERT_2(!vector, testcase, vector);
+	GUEST_ASSERT_MONITOR_MWAIT("MWAIT", testcase, vector);
 }
 
 static void guest_code(void)
@@ -85,7 +92,7 @@ int main(int argc, char *argv[])
 			testcase = uc.args[1];
 			break;
 		case UCALL_ABORT:
-			REPORT_GUEST_ASSERT_2(uc, "testcase = %lx, vector = %ld");
+			REPORT_GUEST_ASSERT(uc);
 			goto done;
 		case UCALL_DONE:
 			goto done;
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#29 lore raw

PATCH v4 25/34 KVM: selftests: Convert x86's nested exceptions test to printf guest asserts

In reply to Sean Christopherson

Convert x86's nested exceptions test to printf-based guest asserts, and use REPORT_GUEST_ASSERT() instead of TEST_FAIL() so that output is formatted correctly.

---
 tools/testing/selftests/kvm/x86_64/nested_exceptions_test.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/x86_64/nested_exceptions_test.c +3 -3
index 5f074a6da90c..4a29f59a76be 100644
@@ -1,4 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
+#define USE_GUEST_ASSERT_PRINTF 1
+
 #define _GNU_SOURCE /* for program_invocation_short_name */
 
 #include "test_util.h"
@@ -180,9 +182,7 @@ static void assert_ucall_vector(struct kvm_vcpu *vcpu, int vector)
 			    "Expected L2 to ask for %d, L2 says it's done", vector);
 		break;
 	case UCALL_ABORT:
-		TEST_FAIL("%s at %s:%ld (0x%lx != 0x%lx)",
-			  (const char *)uc.args[0], __FILE__, uc.args[1],
-			  uc.args[2], uc.args[3]);
+		REPORT_GUEST_ASSERT(uc);
 		break;
 	default:
 		TEST_FAIL("Expected L2 to ask for %d, got unexpected ucall %lu", vector, uc.cmd);
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#30 lore raw

PATCH v4 26/34 KVM: selftests: Convert x86's set BSP ID test to printf style guest asserts

In reply to Sean Christopherson

Convert the set_boot_cpu_id test to use printf-based guest asserts, specifically the EQ and NE variants.

---
 tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c +5 -3
index b25d7556b638..abb3f26d3ce0 100644
@@ -4,6 +4,8 @@
  *
  * Copyright (C) 2020, Red Hat, Inc.
  */
+#define USE_GUEST_ASSERT_PRINTF 1
+
 #define _GNU_SOURCE /* for program_invocation_name */
 #include <fcntl.h>
 #include <stdio.h>
@@ -20,7 +22,7 @@ static void guest_bsp_vcpu(void *arg)
 {
 	GUEST_SYNC(1);
 
-	GUEST_ASSERT(get_bsp_flag() != 0);
+	GUEST_ASSERT_NE(get_bsp_flag(), 0);
 
 	GUEST_DONE();
 }
@@ -29,7 +31,7 @@ static void guest_not_bsp_vcpu(void *arg)
 {
 	GUEST_SYNC(1);
 
-	GUEST_ASSERT(get_bsp_flag() == 0);
+	GUEST_ASSERT_EQ(get_bsp_flag(), 0);
 
 	GUEST_DONE();
 }
@@ -65,7 +67,7 @@ static void run_vcpu(struct kvm_vcpu *vcpu)
 					stage);
 			break;
 		case UCALL_ABORT:
-			REPORT_GUEST_ASSERT_2(uc, "values: %#lx, %#lx");
+			REPORT_GUEST_ASSERT(uc);
 		default:
 			TEST_ASSERT(false, "Unexpected exit: %s",
 				    exit_reason_str(vcpu->run->exit_reason));
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#31 lore raw

PATCH v4 27/34 KVM: selftests: Convert the nSVM software interrupt test to printf guest asserts

In reply to Sean Christopherson

Convert x86's nested SVM software interrupt injection test to use printf- based guest asserts. Opportunistically use GUEST_ASSERT() and GUEST_FAIL() in a few locations to spit out more debug information.

---
 .../kvm/x86_64/svm_nested_soft_inject_test.c  | 22 ++++++++++---------
 1 file changed, 12 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/x86_64/svm_nested_soft_inject_test.c +12 -10
index 4e2479716da6..c908412c5754 100644
@@ -8,6 +8,7 @@
  *   Copyright (C) 2021, Red Hat, Inc.
  *
  */
+#define USE_GUEST_ASSERT_PRINTF 1
 
 #include <stdatomic.h>
 #include <stdio.h>
@@ -34,13 +35,12 @@ static void l2_guest_code_int(void);
 static void guest_int_handler(struct ex_regs *regs)
 {
 	int_fired++;
-	GUEST_ASSERT_2(regs->rip == (unsigned long)l2_guest_code_int,
-		       regs->rip, (unsigned long)l2_guest_code_int);
+	GUEST_ASSERT_EQ(regs->rip, (unsigned long)l2_guest_code_int);
 }
 
 static void l2_guest_code_int(void)
 {
-	GUEST_ASSERT_1(int_fired == 1, int_fired);
+	GUEST_ASSERT_EQ(int_fired, 1);
 
 	/*
          * Same as the vmmcall() function, but with a ud2 sneaked after the
@@ -53,7 +53,7 @@ static void l2_guest_code_int(void)
                              : "rbx", "rdx", "rsi", "rdi", "r8", "r9",
                                "r10", "r11", "r12", "r13", "r14", "r15");
 
-	GUEST_ASSERT_1(bp_fired == 1, bp_fired);
+	GUEST_ASSERT_EQ(bp_fired, 1);
 	hlt();
 }
 
@@ -66,9 +66,9 @@ static void guest_nmi_handler(struct ex_regs *regs)
 
 	if (nmi_stage_get() == 1) {
 		vmmcall();
-		GUEST_ASSERT(false);
+		GUEST_FAIL("Unexpected resume after VMMCALL");
 	} else {
-		GUEST_ASSERT_1(nmi_stage_get() == 3, nmi_stage_get());
+		GUEST_ASSERT_EQ(nmi_stage_get(), 3);
 		GUEST_DONE();
 	}
 }
@@ -104,7 +104,8 @@ static void l1_guest_code(struct svm_test_data *svm, uint64_t is_nmi, uint64_t i
 	}
 
 	run_guest(vmcb, svm->vmcb_gpa);
-	GUEST_ASSERT_3(vmcb->control.exit_code == SVM_EXIT_VMMCALL,
+	__GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL,
+		       "Expected VMMCAL #VMEXIT, got '0x%x', info1 = '0x%llx, info2 = '0x%llx'",
 		       vmcb->control.exit_code,
 		       vmcb->control.exit_info_1, vmcb->control.exit_info_2);
 
@@ -112,7 +113,7 @@ static void l1_guest_code(struct svm_test_data *svm, uint64_t is_nmi, uint64_t i
 		clgi();
 		x2apic_write_reg(APIC_ICR, APIC_DEST_SELF | APIC_INT_ASSERT | APIC_DM_NMI);
 
-		GUEST_ASSERT_1(nmi_stage_get() == 1, nmi_stage_get());
+		GUEST_ASSERT_EQ(nmi_stage_get(), 1);
 		nmi_stage_inc();
 
 		stgi();
@@ -133,7 +134,8 @@ static void l1_guest_code(struct svm_test_data *svm, uint64_t is_nmi, uint64_t i
 	vmcb->control.next_rip = vmcb->save.rip + 2;
 
 	run_guest(vmcb, svm->vmcb_gpa);
-	GUEST_ASSERT_3(vmcb->control.exit_code == SVM_EXIT_HLT,
+	__GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_HLT,
+		       "Expected HLT #VMEXIT, got '0x%x', info1 = '0x%llx, info2 = '0x%llx'",
 		       vmcb->control.exit_code,
 		       vmcb->control.exit_info_1, vmcb->control.exit_info_2);
 
@@ -185,7 +187,7 @@ static void run_test(bool is_nmi)
 
 	switch (get_ucall(vcpu, &uc)) {
 	case UCALL_ABORT:
-		REPORT_GUEST_ASSERT_3(uc, "vals = 0x%lx 0x%lx 0x%lx");
+		REPORT_GUEST_ASSERT(uc);
 		break;
 		/* NOT REACHED */
 	case UCALL_DONE:
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#32 lore raw

PATCH v4 28/34 KVM: selftests: Convert x86's TSC MSRs test to use printf guest asserts

In reply to Sean Christopherson

Convert x86's TSC MSRs test, and it's liberal use of GUEST_ASSERT_EQ(), to use printf-based guest assert reporting.

---
 tools/testing/selftests/kvm/x86_64/tsc_msrs_test.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/x86_64/tsc_msrs_test.c +3 -1
index 9265965bd2cd..cf9114f70e1c 100644
@@ -4,6 +4,8 @@
  *
  * Copyright (C) 2020, Red Hat, Inc.
  */
+#define USE_GUEST_ASSERT_PRINTF 1
+
 #include <stdio.h>
 #include <string.h>
 #include "kvm_util.h"
@@ -84,7 +86,7 @@ static void run_vcpu(struct kvm_vcpu *vcpu, int stage)
 		ksft_test_result_pass("stage %d passed\n", stage + 1);
 		return;
 	case UCALL_ABORT:
-		REPORT_GUEST_ASSERT_2(uc, "values: %#lx, %#lx");
+		REPORT_GUEST_ASSERT(uc);
 	default:
 		TEST_ASSERT(false, "Unexpected exit: %s",
 			    exit_reason_str(vcpu->run->exit_reason));
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#33 lore raw

PATCH v4 29/34 KVM: selftests: Convert the x86 userspace I/O test to printf guest assert

In reply to Sean Christopherson

Convert x86's userspace I/O test to use printf-based guest asserts.

---
 .../testing/selftests/kvm/x86_64/userspace_io_test.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/x86_64/userspace_io_test.c +8 -4
index 0cb51fa42773..2c5d2a18d184 100644
@@ -1,4 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
+#define USE_GUEST_ASSERT_PRINTF 1
+
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -20,8 +22,8 @@ static void guest_ins_port80(uint8_t *buffer, unsigned int count)
 		end = (unsigned long)buffer + 8192;
 
 	asm volatile("cld; rep; insb" : "+D"(buffer), "+c"(count) : "d"(0x80) : "memory");
-	GUEST_ASSERT_1(count == 0, count);
-	GUEST_ASSERT_2((unsigned long)buffer == end, buffer, end);
+	GUEST_ASSERT_EQ(count, 0);
+	GUEST_ASSERT_EQ((unsigned long)buffer, end);
 }
 
 static void guest_code(void)
@@ -43,7 +45,9 @@ static void guest_code(void)
 	memset(buffer, 0, sizeof(buffer));
 	guest_ins_port80(buffer, 8192);
 	for (i = 0; i < 8192; i++)
-		GUEST_ASSERT_2(buffer[i] == 0xaa, i, buffer[i]);
+		__GUEST_ASSERT(buffer[i] == 0xaa,
+			       "Expected '0xaa', got '0x%x' at buffer[%u]",
+			       buffer[i], i);
 
 	GUEST_DONE();
 }
@@ -91,7 +95,7 @@ int main(int argc, char *argv[])
 	case UCALL_DONE:
 		break;
 	case UCALL_ABORT:
-		REPORT_GUEST_ASSERT_2(uc, "argN+1 = 0x%lx, argN+2 = 0x%lx");
+		REPORT_GUEST_ASSERT(uc);
 	default:
 		TEST_FAIL("Unknown ucall %lu", uc.cmd);
 	}
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#34 lore raw

PATCH v4 30/34 KVM: selftests: Convert VMX's PMU capabilities test to printf guest asserts

In reply to Sean Christopherson

Convert x86's VMX PMU capabilities test to use printf-based guest asserts. Opportunstically add a helper to do the WRMSR+assert so as to reduce the amount of copy+paste needed to spit out debug information.

---
 .../selftests/kvm/x86_64/vmx_pmu_caps_test.c  | 28 ++++++++++---------
 1 file changed, 15 insertions(+), 13 deletions(-)
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +15 -13
index 34efd57c2b32..ba09d5a01c39 100644
@@ -10,6 +10,7 @@
  * and check it can be retrieved with KVM_GET_MSR, also test
  * the invalid LBR formats are rejected.
  */
+#define USE_GUEST_ASSERT_PRINTF 1
 
 #define _GNU_SOURCE /* for program_invocation_short_name */
 #include <sys/ioctl.h>
@@ -52,23 +53,24 @@ static const union perf_capabilities format_caps = {
 	.pebs_format = -1,
 };
 
+static void guest_test_perf_capabilities_gp(uint64_t val)
+{
+	uint8_t vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, val);
+
+	__GUEST_ASSERT(vector == GP_VECTOR,
+		       "Expected #GP for value '0x%llx', got vector '0x%x'",
+		       val, vector);
+}
+
 static void guest_code(uint64_t current_val)
 {
-	uint8_t vector;
 	int i;
 
-	vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, current_val);
-	GUEST_ASSERT_2(vector == GP_VECTOR, current_val, vector);
+	guest_test_perf_capabilities_gp(current_val);
+	guest_test_perf_capabilities_gp(0);
 
-	vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES, 0);
-	GUEST_ASSERT_2(vector == GP_VECTOR, 0, vector);
-
-	for (i = 0; i < 64; i++) {
-		vector = wrmsr_safe(MSR_IA32_PERF_CAPABILITIES,
-				    current_val ^ BIT_ULL(i));
-		GUEST_ASSERT_2(vector == GP_VECTOR,
-			       current_val ^ BIT_ULL(i), vector);
-	}
+	for (i = 0; i < 64; i++)
+		guest_test_perf_capabilities_gp(current_val ^ BIT_ULL(i));
 
 	GUEST_DONE();
 }
@@ -95,7 +97,7 @@ static void test_guest_wrmsr_perf_capabilities(union perf_capabilities host_cap)
 
 	switch (get_ucall(vcpu, &uc)) {
 	case UCALL_ABORT:
-		REPORT_GUEST_ASSERT_2(uc, "val = 0x%lx, vector = %lu");
+		REPORT_GUEST_ASSERT(uc);
 		break;
 	case UCALL_DONE:
 		break;
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#35 lore raw

PATCH v4 31/34 KVM: selftests: Convert x86's XCR0 test to use printf-based guest asserts

In reply to Sean Christopherson

Convert x86's XCR0 vs. CPUID test to use printf-based guest asserts.

---
 .../selftests/kvm/x86_64/xcr0_cpuid_test.c    | 29 ++++++++++++-------
 1 file changed, 18 insertions(+), 11 deletions(-)
tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c +18 -11
index 905bd5ae4431..5e8290797720 100644
@@ -4,6 +4,7 @@
  *
  * Copyright (C) 2022, Google LLC.
  */
+#define USE_GUEST_ASSERT_PRINTF 1
 
 #include <fcntl.h>
 #include <stdio.h>
@@ -20,13 +21,14 @@
  * Assert that architectural dependency rules are satisfied, e.g. that AVX is
  * supported if and only if SSE is supported.
  */
-#define ASSERT_XFEATURE_DEPENDENCIES(supported_xcr0, xfeatures, dependencies)	  \
-do {										  \
-	uint64_t __supported = (supported_xcr0) & ((xfeatures) | (dependencies)); \
-										  \
-	GUEST_ASSERT_3((__supported & (xfeatures)) != (xfeatures) ||		  \
-		       __supported == ((xfeatures) | (dependencies)),		  \
-		       __supported, (xfeatures), (dependencies));		  \
+#define ASSERT_XFEATURE_DEPENDENCIES(supported_xcr0, xfeatures, dependencies)		\
+do {											\
+	uint64_t __supported = (supported_xcr0) & ((xfeatures) | (dependencies));	\
+											\
+	__GUEST_ASSERT((__supported & (xfeatures)) != (xfeatures) ||			\
+		       __supported == ((xfeatures) | (dependencies)),			\
+		       "supported = 0x%llx, xfeatures = 0x%llx, dependencies = 0x%llx",	\
+		       __supported, (xfeatures), (dependencies));			\
 } while (0)
 
 /*
@@ -41,7 +43,8 @@ do {										  \
 do {									\
 	uint64_t __supported = (supported_xcr0) & (xfeatures);		\
 									\
-	GUEST_ASSERT_2(!__supported || __supported == (xfeatures),	\
+	__GUEST_ASSERT(!__supported || __supported == (xfeatures),	\
+		       "supported = 0x%llx, xfeatures = 0x%llx",	\
 		       __supported, (xfeatures));			\
 } while (0)
 
@@ -79,14 +82,18 @@ static void guest_code(void)
 				    XFEATURE_MASK_XTILE);
 
 	vector = xsetbv_safe(0, supported_xcr0);
-	GUEST_ASSERT_2(!vector, supported_xcr0, vector);
+	__GUEST_ASSERT(!vector,
+		       "Expected success on XSETBV(0x%llx), got vector '0x%x'",
+		       supported_xcr0, vector);
 
 	for (i = 0; i < 64; i++) {
 		if (supported_xcr0 & BIT_ULL(i))
 			continue;
 
 		vector = xsetbv_safe(0, supported_xcr0 | BIT_ULL(i));
-		GUEST_ASSERT_3(vector == GP_VECTOR, supported_xcr0, vector, BIT_ULL(i));
+		__GUEST_ASSERT(vector == GP_VECTOR,
+			       "Expected #GP on XSETBV(0x%llx), supported XCR0 = %llx, got vector '0x%x'",
+			       BIT_ULL(i), supported_xcr0, vector);
 	}
 
 	GUEST_DONE();
@@ -117,7 +124,7 @@ int main(int argc, char *argv[])
 
 		switch (get_ucall(vcpu, &uc)) {
 		case UCALL_ABORT:
-			REPORT_GUEST_ASSERT_3(uc, "0x%lx 0x%lx 0x%lx");
+			REPORT_GUEST_ASSERT(uc);
 			break;
 		case UCALL_DONE:
 			goto done;
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#36 lore raw

PATCH v4 32/34 KVM: selftests: Rip out old, param-based guest assert macros

In reply to Sean Christopherson

Drop the param-based guest assert macros and enable the printf versions for all selftests. Note! This change can affect tests even if they don't use directly use guest asserts! E.g. via library code, or due to the compiler making different optimization decisions.

---
 .../selftests/kvm/aarch64/arch_timer.c        |  2 -
 .../selftests/kvm/aarch64/debug-exceptions.c  |  2 -
 .../selftests/kvm/aarch64/hypercalls.c        |  2 -
 .../selftests/kvm/aarch64/page_fault_test.c   |  2 -
 .../testing/selftests/kvm/aarch64/vgic_irq.c  |  2 -
 .../testing/selftests/kvm/guest_print_test.c  |  2 -
 .../selftests/kvm/include/ucall_common.h      | 71 -------------------
 .../testing/selftests/kvm/memslot_perf_test.c |  2 -
 tools/testing/selftests/kvm/s390x/memop.c     |  2 -
 tools/testing/selftests/kvm/s390x/tprot.c     |  2 -
 .../selftests/kvm/set_memory_region_test.c    |  2 -
 tools/testing/selftests/kvm/steal_time.c      |  2 -
 .../testing/selftests/kvm/x86_64/cpuid_test.c |  2 -
 .../kvm/x86_64/hyperv_extended_hypercalls.c   |  2 -
 .../selftests/kvm/x86_64/hyperv_features.c    |  2 -
 .../selftests/kvm/x86_64/kvm_pv_test.c        |  2 -
 .../selftests/kvm/x86_64/monitor_mwait_test.c |  2 -
 .../kvm/x86_64/nested_exceptions_test.c       |  2 -
 .../selftests/kvm/x86_64/set_boot_cpu_id.c    |  2 -
 .../kvm/x86_64/svm_nested_soft_inject_test.c  |  2 -
 .../selftests/kvm/x86_64/tsc_msrs_test.c      |  2 -
 .../selftests/kvm/x86_64/userspace_io_test.c  |  2 -
 .../selftests/kvm/x86_64/vmx_pmu_caps_test.c  |  2 -
 .../selftests/kvm/x86_64/xcr0_cpuid_test.c    |  2 -
 24 files changed, 117 deletions(-)
tools/testing/selftests/kvm/aarch64/arch_timer.c +0 -2
index b53bcf126e6a..274b8465b42a 100644
@@ -19,8 +19,6 @@
  *
  * Copyright (c) 2021, Google LLC.
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #define _GNU_SOURCE
 
 #include <stdlib.h>
tools/testing/selftests/kvm/aarch64/debug-exceptions.c +0 -2
index fdd5b05e1b0e..f5b6cb3a0019 100644
@@ -1,6 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <test_util.h>
 #include <kvm_util.h>
 #include <processor.h>
tools/testing/selftests/kvm/aarch64/hypercalls.c +0 -2
index 94555a7d3c7e..31f66ba97228 100644
@@ -8,8 +8,6 @@
  * hypercalls are properly masked or unmasked to the guest when disabled or
  * enabled from the KVM userspace, respectively.
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <errno.h>
 #include <linux/arm-smccc.h>
 #include <asm/kvm.h>
tools/testing/selftests/kvm/aarch64/page_fault_test.c +0 -2
index 0b0dd90feae5..47bb914ab2fa 100644
@@ -7,8 +7,6 @@
  * hugetlbfs with a hole). It checks that the expected handling method is
  * called (e.g., uffd faults with the right address and write/read flag).
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #define _GNU_SOURCE
 #include <linux/bitmap.h>
 #include <fcntl.h>
tools/testing/selftests/kvm/aarch64/vgic_irq.c +0 -2
index 67da33aa6d17..2e64b4856e38 100644
@@ -7,8 +7,6 @@
  * host to inject a specific intid via a GUEST_SYNC call, and then checks that
  * it received it.
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <asm/kvm.h>
 #include <asm/kvm_para.h>
 #include <sys/eventfd.h>
tools/testing/selftests/kvm/guest_print_test.c +0 -2
index 777838d42427..3a9a5db9794e 100644
@@ -4,8 +4,6 @@
  *
  * Copyright 2022, Google, Inc. and/or its affiliates.
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
tools/testing/selftests/kvm/include/ucall_common.h +0 -71

81 lines not on this page · raw message

tools/testing/selftests/kvm/memslot_perf_test.c +0 -2
index 55f1bc70e571..20eb2e730800 100644
@@ -6,8 +6,6 @@
  *
  * Basic guest setup / host vCPU thread code lifted from set_memory_region_test.
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <pthread.h>
 #include <sched.h>
 #include <semaphore.h>
tools/testing/selftests/kvm/s390x/memop.c +0 -2
index a49173907cec..bb3ca9a5d731 100644
@@ -4,8 +4,6 @@
  *
  * Copyright (C) 2019, Red Hat, Inc.
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
tools/testing/selftests/kvm/s390x/tprot.c +0 -2
index c12c6824d963..c73f948c9b63 100644
@@ -4,8 +4,6 @@
  *
  * Copyright IBM Corp. 2021
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <sys/mman.h>
 #include "test_util.h"
 #include "kvm_util.h"
tools/testing/selftests/kvm/set_memory_region_test.c +0 -2
index dd8f4bac9df8..b32960189f5f 100644
@@ -1,6 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #define _GNU_SOURCE /* for program_invocation_short_name */
 #include <fcntl.h>
 #include <pthread.h>
tools/testing/selftests/kvm/steal_time.c +0 -2
index 8649c8545882..171adfb2a6cb 100644
@@ -4,8 +4,6 @@
  *
  * Copyright (C) 2020, Red Hat, Inc.
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #define _GNU_SOURCE
 #include <stdio.h>
 #include <time.h>
tools/testing/selftests/kvm/x86_64/cpuid_test.c +0 -2
index eb1b65ffc0d5..3b34d8156d1c 100644
@@ -4,8 +4,6 @@
  *
  * Generic tests for KVM CPUID set/get ioctls
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <asm/kvm_para.h>
 #include <linux/kvm_para.h>
 #include <stdint.h>
tools/testing/selftests/kvm/x86_64/hyperv_extended_hypercalls.c +0 -2
index 0107d54a1a08..e036db1f32b9 100644
@@ -8,8 +8,6 @@
  * Copyright 2022 Google LLC
  * Author: Vipin Sharma <[email protected]>
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include "kvm_util.h"
 #include "processor.h"
 #include "hyperv.h"
tools/testing/selftests/kvm/x86_64/hyperv_features.c +0 -2
index 41a6beff78c4..9f28aa276c4e 100644
@@ -4,8 +4,6 @@
  *
  * Tests for Hyper-V features enablement
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <asm/kvm_para.h>
 #include <linux/kvm_para.h>
 #include <stdint.h>
tools/testing/selftests/kvm/x86_64/kvm_pv_test.c +0 -2
index 1c28b77ff3cd..9e2879af7c20 100644
@@ -4,8 +4,6 @@
  *
  * Tests for KVM paravirtual feature disablement
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <asm/kvm_para.h>
 #include <linux/kvm_para.h>
 #include <stdint.h>
tools/testing/selftests/kvm/x86_64/monitor_mwait_test.c +0 -2
index 960fecab3742..80aa3d8b18f8 100644
@@ -1,6 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
tools/testing/selftests/kvm/x86_64/nested_exceptions_test.c +0 -2
index 4a29f59a76be..3670331adf21 100644
@@ -1,6 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0-only
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #define _GNU_SOURCE /* for program_invocation_short_name */
 
 #include "test_util.h"
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c +0 -2
index abb3f26d3ce0..366cf18600bc 100644
@@ -4,8 +4,6 @@
  *
  * Copyright (C) 2020, Red Hat, Inc.
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #define _GNU_SOURCE /* for program_invocation_name */
 #include <fcntl.h>
 #include <stdio.h>
tools/testing/selftests/kvm/x86_64/svm_nested_soft_inject_test.c +0 -2
index c908412c5754..7ee44496cf97 100644
@@ -8,8 +8,6 @@
  *   Copyright (C) 2021, Red Hat, Inc.
  *
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <stdatomic.h>
 #include <stdio.h>
 #include <unistd.h>
tools/testing/selftests/kvm/x86_64/tsc_msrs_test.c +0 -2
index cf9114f70e1c..12b0964f4f13 100644
@@ -4,8 +4,6 @@
  *
  * Copyright (C) 2020, Red Hat, Inc.
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <stdio.h>
 #include <string.h>
 #include "kvm_util.h"
tools/testing/selftests/kvm/x86_64/userspace_io_test.c +0 -2
index 2c5d2a18d184..255c50b0dc32 100644
@@ -1,6 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +0 -2
index ba09d5a01c39..ebbcb0a3f743 100644
@@ -10,8 +10,6 @@
  * and check it can be retrieved with KVM_GET_MSR, also test
  * the invalid LBR formats are rejected.
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #define _GNU_SOURCE /* for program_invocation_short_name */
 #include <sys/ioctl.h>
 
tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c +0 -2
index 5e8290797720..77d04a7bdadd 100644
@@ -4,8 +4,6 @@
  *
  * Copyright (C) 2022, Google LLC.
  */
-#define USE_GUEST_ASSERT_PRINTF 1
-
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#37 lore raw

PATCH v4 33/34 KVM: selftests: Print out guest RIP on unhandled exception

In reply to Sean Christopherson

Use the newfanged printf-based guest assert framework to spit out the guest RIP when an unhandled exception is detected, which makes debugging such failures *much* easier.

---
 .../selftests/kvm/lib/x86_64/processor.c       | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +6 -12
index d4a0b504b1e0..d8288374078e 100644
@@ -1074,11 +1074,6 @@ static bool kvm_fixup_exception(struct ex_regs *regs)
 	return true;
 }
 
-void kvm_exit_unexpected_vector(uint32_t value)
-{
-	ucall(UCALL_UNHANDLED, 1, value);
-}
-
 void route_exception(struct ex_regs *regs)
 {
 	typedef void(*handler)(struct ex_regs *);
@@ -1092,7 +1087,10 @@ void route_exception(struct ex_regs *regs)
 	if (kvm_fixup_exception(regs))
 		return;
 
-	kvm_exit_unexpected_vector(regs->vector);
+	ucall_assert(UCALL_UNHANDLED,
+		     "Unhandled exception in guest", __FILE__, __LINE__,
+		     "Unhandled exception '0x%lx' at guest RIP '0x%lx'",
+		     regs->vector, regs->rip);
 }
 
 void vm_init_descriptor_tables(struct kvm_vm *vm)
@@ -1135,12 +1133,8 @@ void assert_on_unhandled_exception(struct kvm_vcpu *vcpu)
 {
 	struct ucall uc;
 
-	if (get_ucall(vcpu, &uc) == UCALL_UNHANDLED) {
-		uint64_t vector = uc.args[0];
-
-		TEST_FAIL("Unexpected vectored event in guest (vector:0x%lx)",
-			  vector);
-	}
+	if (get_ucall(vcpu, &uc) == UCALL_UNHANDLED)
+		REPORT_GUEST_ASSERT(uc);
 }
 
 const struct kvm_cpuid_entry2 *get_cpuid_entry(const struct kvm_cpuid2 *cpuid,
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#38 lore raw

PATCH v4 34/34 KVM: selftests: Use GUEST_FAIL() in ARM's arch timer helpers

In reply to Sean Christopherson

Use GUEST_FAIL() in ARM's arch timer helpers now that printf-based guest asserts are the default (and only) style of guest asserts, and say goodbye to the GUEST_ASSERT_1() alias.

---
 .../selftests/kvm/include/aarch64/arch_timer.h       | 12 ++++++------
 tools/testing/selftests/kvm/include/ucall_common.h   |  4 ----
 2 files changed, 6 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/include/aarch64/arch_timer.h +6 -6
index cb7c03de3a21..b3e97525cb55 100644
@@ -41,7 +41,7 @@ static inline uint64_t timer_get_cntct(enum arch_timer timer)
 	case PHYSICAL:
 		return read_sysreg(cntpct_el0);
 	default:
-		GUEST_ASSERT_1(0, timer);
+		GUEST_FAIL("Unexpected timer type = %u", timer);
 	}
 
 	/* We should not reach here */
@@ -58,7 +58,7 @@ static inline void timer_set_cval(enum arch_timer timer, uint64_t cval)
 		write_sysreg(cval, cntp_cval_el0);
 		break;
 	default:
-		GUEST_ASSERT_1(0, timer);
+		GUEST_FAIL("Unexpected timer type = %u", timer);
 	}
 
 	isb();
@@ -72,7 +72,7 @@ static inline uint64_t timer_get_cval(enum arch_timer timer)
 	case PHYSICAL:
 		return read_sysreg(cntp_cval_el0);
 	default:
-		GUEST_ASSERT_1(0, timer);
+		GUEST_FAIL("Unexpected timer type = %u", timer);
 	}
 
 	/* We should not reach here */
@@ -89,7 +89,7 @@ static inline void timer_set_tval(enum arch_timer timer, uint32_t tval)
 		write_sysreg(tval, cntp_tval_el0);
 		break;
 	default:
-		GUEST_ASSERT_1(0, timer);
+		GUEST_FAIL("Unexpected timer type = %u", timer);
 	}
 
 	isb();
@@ -105,7 +105,7 @@ static inline void timer_set_ctl(enum arch_timer timer, uint32_t ctl)
 		write_sysreg(ctl, cntp_ctl_el0);
 		break;
 	default:
-		GUEST_ASSERT_1(0, timer);
+		GUEST_FAIL("Unexpected timer type = %u", timer);
 	}
 
 	isb();
@@ -119,7 +119,7 @@ static inline uint32_t timer_get_ctl(enum arch_timer timer)
 	case PHYSICAL:
 		return read_sysreg(cntp_ctl_el0);
 	default:
-		GUEST_ASSERT_1(0, timer);
+		GUEST_FAIL("Unexpected timer type = %u", timer);
 	}
 
 	/* We should not reach here */
tools/testing/selftests/kvm/include/ucall_common.h +0 -4
index b7e964b3182e..4cf69fa8bfba 100644
@@ -102,8 +102,4 @@ do {										\
 		    (const char *)(ucall).args[GUEST_FILE],			\
 		    (ucall).args[GUEST_LINE], "%s", (ucall).buffer)
 
-/* FIXME: Drop this alias once the param-based guest asserts are gone. */
-#define GUEST_ASSERT_1(_condition, arg1) \
-	__GUEST_ASSERT(_condition, "arg1 = 0x%lx", arg1)
-
 #endif /* SELFTEST_KVM_UCALL_COMMON_H */
Signature · 1 line
2.41.0.487.g6d72f3e995-goog
author reviewer
#39 lore raw
In reply to Sean Christopherson
Quoted 11 lines · Sean Christopherson

This is effectively v4 of Aaron's series to add printf capabilities to the guest[*]. I also pulled in Thomas' patch to rename ASSERT_EQ() to TEST_ASSERT_EQ(), mainly so that we can decide on a common output format for both host and guest asserts in a single series, but also so that all these basically treewide patches are contained in a single series.

Note, Aaron did all of the heavy lifting, I just mopped up. The core code is pretty much unchanged from Aaron's v3, v4 massages the assert code a bit and converts all the tests.

[...]

Applied to kvm-x86 selftests and officially started this runaway train. I'll give this a few days to stew in linux-next, and assuming no fireworks, will create a tag and make everything up to that point immutable.

I will follow-up with all the in-flight selftests series I can find to coordinate, though I'm hoping we'll get lucky and just not have any major conflitcs.

Please yell asap if you encounter issues and/or object to any of this.

Note, I technically applied patch 1 from Thomas' original posting, but captured it here too as I figured that'd be the least confusing thing.

[1/36] KVM: selftests: Rename the ASSERT_EQ macro

      https://github.com/kvm-x86/linux/commit/6d85f51a1f08
[2/36] KVM: selftests: Make TEST_ASSERT_EQ() output look like normal TEST_ASSERT()
      https://github.com/kvm-x86/linux/commit/b145c58d95ff
[3/36] KVM: selftests: Add a shameful hack to preserve/clobber GPRs across ucall
      https://github.com/kvm-x86/linux/commit/6783ca4105a7
[4/36] KVM: selftests: Add strnlen() to the string overrides
      https://github.com/kvm-x86/linux/commit/a1c1b55e116c
[5/36] KVM: selftests: Add guest_snprintf() to KVM selftests
      https://github.com/kvm-x86/linux/commit/e5119382499c
[6/36] KVM: selftests: Add additional pages to the guest to accommodate ucall
      https://github.com/kvm-x86/linux/commit/215a681710a5
[7/36] KVM: selftests: Add string formatting options to ucall
      https://github.com/kvm-x86/linux/commit/57e5c1fef5ec
[8/36] KVM: selftests: Add formatted guest assert support in ucall framework
      https://github.com/kvm-x86/linux/commit/289c2b4db8f3
[9/36] KVM: selftests: Add arch ucall.h and inline simple arch hooks
      https://github.com/kvm-x86/linux/commit/b35f4c73d389
[10/36] KVM: selftests: Add #define of expected KVM exit reason for ucall
      https://github.com/kvm-x86/linux/commit/edb5b700f9f8
[11/36] KVM: selftests: Add a selftest for guest prints and formatted asserts
      https://github.com/kvm-x86/linux/commit/5d1d46f9d56f
[12/36] KVM: selftests: Convert aarch_timer to printf style GUEST_ASSERT
      https://github.com/kvm-x86/linux/commit/db44e1c871bc
[13/36] KVM: selftests: Convert debug-exceptions to printf style GUEST_ASSERT
      https://github.com/kvm-x86/linux/commit/bac9aeecc387
[14/36] KVM: selftests: Convert ARM's hypercalls test to printf style GUEST_ASSERT
      https://github.com/kvm-x86/linux/commit/af5b41b97f1c
[15/36] KVM: selftests: Convert ARM's page fault test to printf style GUEST_ASSERT
      https://github.com/kvm-x86/linux/commit/df27f6b45454
[16/36] KVM: selftests: Convert ARM's vGIC IRQ test to printf style GUEST_ASSERT
      https://github.com/kvm-x86/linux/commit/d0ad3bacc523
[17/36] KVM: selftests: Convert the memslot performance test to printf guest asserts
      https://github.com/kvm-x86/linux/commit/c55a475d5fc4
[18/36] KVM: selftests: Convert s390's memop test to printf style GUEST_ASSERT
      https://github.com/kvm-x86/linux/commit/428c76c769fa
[19/36] KVM: selftests: Convert s390's tprot test to printf style GUEST_ASSERT
      https://github.com/kvm-x86/linux/commit/5f82bbab84ad
[20/36] KVM: selftests: Convert set_memory_region_test to printf-based GUEST_ASSERT
      https://github.com/kvm-x86/linux/commit/9291c9cef5b5
[21/36] KVM: selftests: Convert steal_time test to printf style GUEST_ASSERT
      https://github.com/kvm-x86/linux/commit/3d9bd831175e
[22/36] KVM: selftests: Convert x86's CPUID test to printf style GUEST_ASSERT
      https://github.com/kvm-x86/linux/commit/06b651d250e5
[23/36] KVM: selftests: Convert the Hyper-V extended hypercalls test to printf asserts
      https://github.com/kvm-x86/linux/commit/82cb0ed66d4e
[24/36] KVM: selftests: Convert the Hyper-V feature test to printf style GUEST_ASSERT
      https://github.com/kvm-x86/linux/commit/8d1d3ce604e5
[25/36] KVM: selftests: Convert x86's KVM paravirt test to printf style GUEST_ASSERT
      https://github.com/kvm-x86/linux/commit/bf6c760b9df3
[26/36] KVM: selftests: Convert the MONITOR/MWAIT test to use printf guest asserts
      https://github.com/kvm-x86/linux/commit/0f52e4aaa614
[27/36] KVM: selftests: Convert x86's nested exceptions test to printf guest asserts
      https://github.com/kvm-x86/linux/commit/b13a307ce3c6
[28/36] KVM: selftests: Convert x86's set BSP ID test to printf style guest asserts
      https://github.com/kvm-x86/linux/commit/40b319d6b4e1
[29/36] KVM: selftests: Convert the nSVM software interrupt test to printf guest asserts
      https://github.com/kvm-x86/linux/commit/a925f7994281
[30/36] KVM: selftests: Convert x86's TSC MSRs test to use printf guest asserts
      https://github.com/kvm-x86/linux/commit/847ae0795514
[31/36] KVM: selftests: Convert the x86 userspace I/O test to printf guest assert
      https://github.com/kvm-x86/linux/commit/417bfd0c820f
[32/36] KVM: selftests: Convert VMX's PMU capabilities test to printf guest asserts
      https://github.com/kvm-x86/linux/commit/30a6e0b4553d
[33/36] KVM: selftests: Convert x86's XCR0 test to use printf-based guest asserts
      https://github.com/kvm-x86/linux/commit/4e15c38a1aca
[34/36] KVM: selftests: Rip out old, param-based guest assert macros
      https://github.com/kvm-x86/linux/commit/7ce7f8e75418
[35/36] KVM: selftests: Print out guest RIP on unhandled exception
      https://github.com/kvm-x86/linux/commit/6f321017c84b
[36/36] KVM: selftests: Use GUEST_FAIL() in ARM's arch timer helpers
      https://github.com/kvm-x86/linux/commit/a05c4c2bd8b5

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