mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zack Rusin <zack.rusin@broadcom.com>
To: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	kvm@vger.kernel.org
Cc: "Doug Covelli" <doug.covelli@broadcom.com>,
	"Thomas Gleixner" <tglx@kernel.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Borislav Petkov" <bp@alien8.de>,
	"Dave Hansen" <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Shuah Khan" <shuah@kernel.org>,
	"Vitaly Kuznetsov" <vkuznets@redhat.com>,
	"David Woodhouse" <dwmw2@infradead.org>,
	"Paul Durrant" <paul@xen.org>, "Xin Li" <xin@zytor.com>,
	"Francesco Lavra" <flavra@baylibre.com>,
	"Radim Krčmář" <rkrcmar@ventanamicro.com>,
	"Liran Alon" <liran.alon@oracle.com>,
	"Arbel Moshe" <arbel.moshe@oracle.com>,
	linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org,
	bcm-kernel-feedback-list@broadcom.com,
	linux-kernel@vger.kernel.org,
	"Zack Rusin" <zack.rusin@broadcom.com>
Subject: [PATCH v3 09/12] KVM: selftests: Port VMware backdoor coverage
Date: Wed, 11 Nov 2026 00:59:58 -0500	[thread overview]
Message-ID: <4dca09deb85d2d5db157888ae4a2de7cbf6421d2.1794374897.git.zack.rusin@broadcom.com> (raw)
In-Reply-To: <cover.1794374897.git.zack.rusin@broadcom.com>

Add vmware_backdoor_test, adapted from KVM-unit-tests' vmware_backdoors
test.  From CPL3, with CR4.PCE clear and no I/O permission bitmap in
the TSS, read the VMware pseudo-PMCs with RDPMC and issue backdoor
commands on the backdoor port with IN.  Verify that the pseudo-PMC
reads succeed and the port accesses exit to userspace only when the
backdoor is enabled and raise #GP otherwise, and that other PMC indices
and ports always raise #GP.  Repeat the pseudo-PMC checks at CPL3 with
CR4.PCE set and at CPL0, where RDPMC doesn't fault before KVM handles
it.  Skip the pseudo-PMC checks if KVM doesn't virtualize a PMU, as KVM
then injects #GP for every RDPMC.

Run the checks for a VM that relies on the enable_vmware_backdoor
default and for a VM that enables KVM_VMWARE_ENABLE_BACKDOOR, and
verify that KVM_CAP_X86_VMWARE rejects every flag it doesn't report.
KVM-unit-tests can only cover the module parameter, which applies to
every VM, while the selftest also covers the per-VM capability.

Suggested-by: Sean Christopherson <seanjc@google.com>
Link: https://lore.kernel.org/r/aIEoNpe-LFoH6LBm@google.com
Signed-off-by: Zack Rusin <zack.rusin@broadcom.com>
---
 MAINTAINERS                                   |   1 +
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/x86/vmware_backdoor_test.c  | 272 ++++++++++++++++++
 3 files changed, 274 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/vmware_backdoor_test.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 017053e8f76e..39fa67a180f5 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -15050,6 +15050,7 @@ S:	Supported
 T:	git git://git.kernel.org/pub/scm/virt/kvm/kvm.git
 T:	git https://github.com/kvm-x86/linux.git
 F:	arch/x86/kvm/vmware.*
+F:	tools/testing/selftests/kvm/x86/vmware_backdoor_test.c
 
 KVM X86 Xen (KVM/Xen)
 M:	David Woodhouse <dwmw2@infradead.org>
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 95867977beb3..0ef08d7783ae 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -140,6 +140,7 @@ TEST_GEN_PROGS_x86 += x86/sync_regs_test
 TEST_GEN_PROGS_x86 += x86/ucna_injection_test
 TEST_GEN_PROGS_x86 += x86/userspace_io_test
 TEST_GEN_PROGS_x86 += x86/userspace_msr_exit_test
+TEST_GEN_PROGS_x86 += x86/vmware_backdoor_test
 TEST_GEN_PROGS_x86 += x86/vmx_apic_access_test
 TEST_GEN_PROGS_x86 += x86/vmx_apicv_updates_test
 TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state
diff --git a/tools/testing/selftests/kvm/x86/vmware_backdoor_test.c b/tools/testing/selftests/kvm/x86/vmware_backdoor_test.c
new file mode 100644
index 000000000000..e4e87d2ce3c3
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/vmware_backdoor_test.c
@@ -0,0 +1,272 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025-2026 Broadcom. All Rights Reserved. The term
+ * "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
+ *
+ * Adapted from kvm-unit-tests' x86/vmware_backdoors.c.
+ */
+
+#include "kvm_util.h"
+#include "processor.h"
+#include "test_util.h"
+
+#define VMWARE_BACKDOOR_PMC_HOST_TSC		0x10000
+#define VMWARE_BACKDOOR_PMC_REAL_TIME		0x10001
+#define VMWARE_BACKDOOR_PMC_APPARENT_TIME	0x10002
+
+#define VMWARE_BACKDOOR_PORT	0x5658
+#define VMWARE_MAGIC		0x564d5868
+#define VMPORT_CMD_GETVERSION	0x0a
+#define VMPORT_CMD_ILLEGAL	0xfff
+#define VMPORT_DEFAULT_RETVAL	0xdeadbeef
+#define RANDOM_IO_PORT		0x1234
+#define RANDOM_PMC		0xfff
+
+struct insn_result {
+	u64 value;
+	u8 vector;
+};
+
+struct backdoor_results {
+	struct insn_result pmc[4];
+	struct insn_result port[3];
+};
+
+enum backdoor_mode {
+	USER_MODE,		/* CPL3, CR4.PCE clear */
+	USER_MODE_PCE,		/* CPL3, CR4.PCE set */
+	KERNEL_MODE,		/* CPL0 */
+};
+
+static struct backdoor_results results;
+static bool expect_backdoor;
+static bool test_pmcs;
+
+static struct insn_result guest_rdpmc(u32 pmc)
+{
+	struct insn_result result;
+	u32 eax;
+	u32 edx;
+	u64 error_code;
+
+	asm volatile(KVM_ASM_SAFE("rdpmc")
+		     : "=a"(eax), "=d"(edx),
+		       KVM_ASM_SAFE_OUTPUTS(result.vector, error_code)
+		     : "c"(pmc)
+		     : KVM_ASM_SAFE_CLOBBERS);
+	result.value = ((u64)edx << 32) | eax;
+	return result;
+}
+
+static struct insn_result guest_inl(u16 port, u32 command)
+{
+	struct insn_result result;
+	unsigned long rax = VMPORT_DEFAULT_RETVAL;
+	unsigned long rbx = VMPORT_DEFAULT_RETVAL;
+	unsigned long rcx = command;
+	unsigned long rdx = port;
+	u64 error_code;
+
+	asm volatile(KVM_ASM_SAFE("inl %%dx, %%eax")
+		     : "+a"(rax), "+b"(rbx), "+c"(rcx), "+d"(rdx),
+		       KVM_ASM_SAFE_OUTPUTS(result.vector, error_code)
+		     :
+		     : KVM_ASM_SAFE_CLOBBERS);
+	result.value = rbx;
+	return result;
+}
+
+static void guest_read_pmcs(void)
+{
+	results.pmc[0] = guest_rdpmc(VMWARE_BACKDOOR_PMC_HOST_TSC);
+	results.pmc[1] = guest_rdpmc(VMWARE_BACKDOOR_PMC_REAL_TIME);
+	results.pmc[2] = guest_rdpmc(VMWARE_BACKDOOR_PMC_APPARENT_TIME);
+	results.pmc[3] = guest_rdpmc(RANDOM_PMC);
+}
+
+static void guest_check_pmcs(void)
+{
+	int i;
+
+	for (i = 0; i < 3; i++) {
+		GUEST_ASSERT_EQ(results.pmc[i].vector,
+				expect_backdoor ? 0 : GP_VECTOR);
+		if (expect_backdoor)
+			GUEST_ASSERT_NE(results.pmc[i].value, 0);
+	}
+	GUEST_ASSERT_EQ(results.pmc[3].vector, GP_VECTOR);
+}
+
+static void guest_done_handler(struct ex_regs *regs)
+{
+	if (test_pmcs)
+		guest_check_pmcs();
+
+	GUEST_ASSERT_EQ(results.port[0].vector,
+			expect_backdoor ? 0 : GP_VECTOR);
+	GUEST_ASSERT_EQ(results.port[1].vector,
+			expect_backdoor ? 0 : GP_VECTOR);
+	if (expect_backdoor)
+		GUEST_ASSERT_EQ(results.port[0].value, VMWARE_MAGIC);
+	GUEST_ASSERT_EQ(results.port[2].vector, GP_VECTOR);
+
+	GUEST_DONE();
+}
+
+static void guest_user_code(void)
+{
+	if (test_pmcs)
+		guest_read_pmcs();
+
+	results.port[0] = guest_inl(VMWARE_BACKDOOR_PORT,
+				    VMPORT_CMD_GETVERSION);
+	results.port[1] = guest_inl(VMWARE_BACKDOOR_PORT,
+				    VMPORT_CMD_ILLEGAL);
+	results.port[2] = guest_inl(RANDOM_IO_PORT, VMPORT_CMD_ILLEGAL);
+
+	asm volatile("int3");
+}
+
+/* At CPL0, only the pseudo-PMCs depend on the backdoor. */
+static void guest_kernel_code(void)
+{
+	guest_read_pmcs();
+	guest_check_pmcs();
+	GUEST_DONE();
+}
+
+static void handle_vmware_port(struct kvm_vcpu *vcpu)
+{
+	struct kvm_run *run = vcpu->run;
+	struct kvm_regs regs;
+	u32 *data;
+
+	TEST_ASSERT_EQ(run->io.direction, KVM_EXIT_IO_IN);
+	TEST_ASSERT_EQ(run->io.size, 4);
+	TEST_ASSERT_EQ(run->io.count, 1);
+	TEST_ASSERT_EQ(run->io.port, VMWARE_BACKDOOR_PORT);
+
+	data = (void *)run + run->io.data_offset;
+	*data = 0;
+
+	vcpu_regs_get(vcpu, &regs);
+	regs.rbx = VMWARE_MAGIC;
+	vcpu_regs_set(vcpu, &regs);
+}
+
+static void test_unsupported_flags(u64 supported)
+{
+	struct kvm_vm *vm = vm_create_barebones();
+	int bit, ret;
+
+	for (bit = 0; bit < 64; bit++) {
+		if (supported & BIT_ULL(bit))
+			continue;
+
+		errno = 0;
+		ret = __vm_enable_cap(vm, KVM_CAP_X86_VMWARE, BIT_ULL(bit));
+		TEST_ASSERT(ret == -1 && errno == EINVAL,
+			    "Unsupported VMware flag %d: ret=%d errno=%d",
+			    bit, ret, errno);
+	}
+	kvm_vm_free(vm);
+}
+
+static void run_backdoor_test(bool enable_cap, bool expected,
+			      enum backdoor_mode mode)
+{
+	struct kvm_sregs sregs;
+	struct idt_entry *idt;
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	int port_exits = 0;
+
+	expect_backdoor = expected;
+	memset(&results, 0, sizeof(results));
+
+	vm = vm_create(1);
+	sync_global_to_guest(vm, expect_backdoor);
+	sync_global_to_guest(vm, test_pmcs);
+	sync_global_to_guest(vm, results);
+	if (enable_cap)
+		vm_enable_cap(vm, KVM_CAP_X86_VMWARE,
+			      KVM_VMWARE_ENABLE_BACKDOOR);
+	if (mode == KERNEL_MODE) {
+		vcpu = vm_vcpu_add(vm, 0, guest_kernel_code);
+	} else {
+		vcpu = vm_vcpu_add(vm, 0, guest_user_code);
+
+		idt = addr_gva2hva(vm, vm->arch.idt);
+		idt[BP_VECTOR].dpl = 3;
+		vm_install_exception_handler(vm, BP_VECTOR, guest_done_handler);
+		vm_init_user_mode(vm);
+		vcpu_enter_user_mode(vcpu);
+	}
+
+	if (mode == USER_MODE_PCE) {
+		vcpu_sregs_get(vcpu, &sregs);
+		sregs.cr4 |= X86_CR4_PCE;
+		vcpu_sregs_set(vcpu, &sregs);
+	}
+
+	for (;;) {
+		struct kvm_run *run = vcpu->run;
+		struct ucall uc;
+
+		vcpu_run(vcpu);
+		TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+		if (run->io.port == VMWARE_BACKDOOR_PORT) {
+			TEST_ASSERT(expected,
+				    "Disabled VMware backdoor reached userspace");
+			handle_vmware_port(vcpu);
+			port_exits++;
+			continue;
+		}
+
+		switch (get_ucall(vcpu, &uc)) {
+		case UCALL_ABORT:
+			REPORT_GUEST_ASSERT(uc);
+			break;
+		case UCALL_DONE:
+			TEST_ASSERT_EQ(port_exits,
+				       expected && mode != KERNEL_MODE ? 2 : 0);
+			kvm_vm_free(vm);
+			return;
+		default:
+			TEST_FAIL("Unexpected ucall 0x%lx", uc.cmd);
+		}
+	}
+}
+
+int main(int argc, char *argv[])
+{
+	unsigned int supported = kvm_check_cap(KVM_CAP_X86_VMWARE);
+	enum backdoor_mode mode;
+	bool module_default;
+
+	TEST_REQUIRE(supported & KVM_VMWARE_ENABLE_BACKDOOR);
+
+	test_unsupported_flags(supported);
+
+	/* KVM serves the pseudo-PMCs only if it virtualizes a PMU. */
+	test_pmcs = kvm_is_pmu_enabled();
+	if (!test_pmcs)
+		ksft_print_msg("vPMU disabled, skipping pseudo-PMC checks\n");
+
+	module_default = get_kvm_param_bool("enable_vmware_backdoor");
+	run_backdoor_test(false, module_default, USER_MODE);
+	run_backdoor_test(true, true, USER_MODE);
+
+	/*
+	 * RDPMC at CPL0, or at CPL3 with CR4.PCE set, doesn't raise the #GP
+	 * that the emulator handles, so check the pseudo-PMCs there as well.
+	 */
+	if (test_pmcs) {
+		for (mode = USER_MODE_PCE; mode <= KERNEL_MODE; mode++) {
+			run_backdoor_test(false, module_default, mode);
+			run_backdoor_test(true, true, mode);
+		}
+	}
+	return 0;
+}
-- 
2.53.0


  parent reply	other threads:[~2026-11-11  6:08 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11  5:59 [PATCH v3 00/12] KVM: Improve VMware guest support Zack Rusin
2026-11-11  5:59 ` [PATCH v3 01/12] KVM: x86/pmu: Honor enable_vmware_backdoor for VMware pseudo-PMCs Zack Rusin
2026-11-11  5:59 ` [PATCH v3 02/12] KVM: x86: Move VMware backdoor support to separate files Zack Rusin
2026-11-11  5:59 ` [PATCH v3 03/12] KVM: x86: Encapsulate VMware backdoor checks Zack Rusin
2026-11-11  5:59 ` [PATCH v3 04/12] KVM: x86: Add CONFIG_KVM_VMWARE Zack Rusin
2026-11-11  5:59 ` [PATCH v3 05/12] KVM: x86: Add a unified VMware capability Zack Rusin
2026-11-11  5:59 ` [PATCH v3 06/12] KVM: x86: Forward VMware hypercalls to userspace Zack Rusin
2026-11-11  5:59 ` [PATCH v3 07/12] KVM: x86: Route nested VMware backdoors to KVM Zack Rusin
2026-11-11  5:59 ` [PATCH v3 08/12] KVM: selftests: Add helpers to run x86 guest code in user mode Zack Rusin
2026-11-11  5:59 ` Zack Rusin [this message]
2026-11-11  5:59 ` [PATCH v3 10/12] KVM: selftests: Test VMware hypercall exits Zack Rusin
2026-11-11  6:00 ` [PATCH v3 11/12] KVM: selftests: Test VMware hypercall exits with nested Hyper-V flushes Zack Rusin
2026-11-11  6:00 ` [PATCH v3 12/12] KVM: selftests: Test nested VMware backdoor routing Zack Rusin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4dca09deb85d2d5db157888ae4a2de7cbf6421d2.1794374897.git.zack.rusin@broadcom.com \
    --to=zack.rusin@broadcom.com \
    --cc=arbel.moshe@oracle.com \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=bp@alien8.de \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=doug.covelli@broadcom.com \
    --cc=dwmw2@infradead.org \
    --cc=flavra@baylibre.com \
    --cc=hpa@zytor.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=liran.alon@oracle.com \
    --cc=mingo@redhat.com \
    --cc=paul@xen.org \
    --cc=pbonzini@redhat.com \
    --cc=rdunlap@infradead.org \
    --cc=rkrcmar@ventanamicro.com \
    --cc=seanjc@google.com \
    --cc=shuah@kernel.org \
    --cc=tglx@kernel.org \
    --cc=vkuznets@redhat.com \
    --cc=x86@kernel.org \
    --cc=xin@zytor.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®