[PATCH 3/3] KVM: selftests: x86: Add a test for KVM_CAP_X86_VMWARE_HYPERCALL

Zack Rusin
2024-10-30T03:35:23Z

Add a testcase to exercise KVM_CAP_X86_VMWARE_HYPERCALL and validate
that KVM exits to userspace on hypercalls and registers are correctly
preserved.

Signed-off-by: Zack Rusin <zack.rusin@broadcom.com>
Cc: Doug Covelli <doug.covelli@broadcom.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: x86@kernel.org
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Isaku Yamahata <isaku.yamahata@intel.com>
Cc: Joel Stanley <joel@jms.id.au>
Cc: Zack Rusin <zack.rusin@broadcom.com>
Cc: kvm@vger.kernel.org
Cc: linux-doc@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-kselftest@vger.kernel.org
---
tools/include/uapi/linux/kvm.h | 2 +
tools/testing/selftests/kvm/Makefile | 1 +
.../kvm/x86_64/vmware_hypercall_test.c | 108 ++++++++++++++++++
3 files changed, 111 insertions(+)
create mode 100644 tools/testing/selftests/kvm/x86_64/vmware_hypercall_test.c

diff --git a/tools/include/uapi/linux/kvm.h b/tools/include/uapi/linux/kvm.h
index 637efc055145..4c2cc6ed29a0 100644
--- a/tools/include/uapi/linux/kvm.h
+++ b/tools/include/uapi/linux/kvm.h
@@ -933,6 +933,8 @@ struct kvm_enable_cap {
#define KVM_CAP_PRE_FAULT_MEMORY 236
#define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
#define KVM_CAP_X86_GUEST_MODE 238
+#define KVM_CAP_X86_VMWARE_BACKDOOR 239
+#define KVM_CAP_X86_VMWARE_HYPERCALL 240

struct kvm_irq_routing_irqchip {
__u32 irqchip;
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
index 48d32c5aa3eb..fa538d01249b 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -103,6 +103,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/sync_regs_test
TEST_GEN_PROGS_x86_64 += x86_64/ucna_injection_test
TEST_GEN_PROGS_x86_64 += x86_64/userspace_io_test
TEST_GEN_PROGS_x86_64 += x86_64/userspace_msr_exit_test
+TEST_GEN_PROGS_x86_64 += x86_64/vmware_hypercall_test
TEST_GEN_PROGS_x86_64 += x86_64/vmx_apic_access_test
TEST_GEN_PROGS_x86_64 += x86_64/vmx_close_while_nested_test
TEST_GEN_PROGS_x86_64 += x86_64/vmx_dirty_log_test
diff --git a/tools/testing/selftests/kvm/x86_64/vmware_hypercall_test.c b/tools/testing/selftests/kvm/x86_64/vmware_hypercall_test.c
new file mode 100644
index 000000000000..0600919ecec8
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86_64/vmware_hypercall_test.c
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * vmware_hypercall_test
+ *
+ * Copyright (c) 2024 Broadcom. All Rights Reserved. The term
+ * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
+ *
+ * Based on:
+ * xen_vmcall_test.c
+ *
+ * Copyright © 2020 Amazon.com, Inc. or its affiliates.
+ *
+ * VMware hypercall testing
+ */
+
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+
+#define ARGVALUE(x) (0xdeadbeef5a5a0000UL + (x))
+#define RETVALUE(x) (0xcafef00dfbfbffffUL + (x))
+
+static void guest_code(void)
+{
+ unsigned long rax = ARGVALUE(1);
+ unsigned long rbx = ARGVALUE(2);
+ unsigned long rcx = ARGVALUE(3);
+ unsigned long rdx = ARGVALUE(4);
+ unsigned long rsi = ARGVALUE(5);
+ unsigned long rdi = ARGVALUE(6);
+ register unsigned long rbp __asm__("rbp") = ARGVALUE(7);
+
+ __asm__ __volatile__("vmcall" :
+ "=a"(rax), "=b"(rbx), "=c"(rcx), "=d"(rdx),
+ "=S"(rsi), "=D"(rdi) :
+ "a"(rax), "b"(rbx), "c"(rcx), "d"(rdx),
+ "S"(rsi), "D"(rdi), "r"(rbp));
+ GUEST_ASSERT_EQ(rax, RETVALUE(1));
+ GUEST_ASSERT_EQ(rbx, RETVALUE(2));
+ GUEST_ASSERT_EQ(rcx, RETVALUE(3));
+ GUEST_ASSERT_EQ(rdx, RETVALUE(4));
+ GUEST_ASSERT_EQ(rdi, RETVALUE(5));
+ GUEST_ASSERT_EQ(rsi, RETVALUE(6));
+ GUEST_DONE();
+}
+
+int main(int argc, char *argv[])
+{
+ struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+
+ if (!kvm_check_cap(KVM_CAP_X86_VMWARE_HYPERCALL)) {
+ print_skip("KVM_CAP_X86_VMWARE_HYPERCALL not available");
+ exit(KSFT_SKIP);
+ }
+
+ vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+
+ vm_enable_cap(vm, KVM_CAP_X86_VMWARE_HYPERCALL, 1);
+
+ for (;;) {
+ struct kvm_run *run = vcpu->run;
+ struct ucall uc;
+
+ vcpu_run(vcpu);
+
+ if (run->exit_reason == KVM_EXIT_HYPERCALL) {
+ struct kvm_regs regs;
+
+ TEST_ASSERT_EQ(run->hypercall.ret, 0);
+ TEST_ASSERT_EQ(run->hypercall.longmode, 1);
+ TEST_ASSERT_EQ(run->hypercall.nr, ARGVALUE(1));
+ TEST_ASSERT_EQ(run->hypercall.args[0], ARGVALUE(2));
+ TEST_ASSERT_EQ(run->hypercall.args[1], ARGVALUE(3));
+ TEST_ASSERT_EQ(run->hypercall.args[2], ARGVALUE(4));
+ TEST_ASSERT_EQ(run->hypercall.args[3], ARGVALUE(5));
+ TEST_ASSERT_EQ(run->hypercall.args[4], ARGVALUE(6));
+ TEST_ASSERT_EQ(run->hypercall.args[5], ARGVALUE(7));
+
+ run->hypercall.ret = RETVALUE(1);
+ vcpu_regs_get(vcpu, &regs);
+ regs.rbx = RETVALUE(2);
+ regs.rcx = RETVALUE(3);
+ regs.rdx = RETVALUE(4);
+ regs.rdi = RETVALUE(5);
+ regs.rsi = RETVALUE(6);
+ vcpu_regs_set(vcpu, &regs);
+ continue;
+ }
+
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ /* NOT REACHED */
+ case UCALL_SYNC:
+ break;
+ case UCALL_DONE:
+ goto done;
+ default:
+ TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
+ }
+ }
+done:
+ kvm_vm_free(vm);
+ return 0;
+}
--
2.43.0