public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: David Matlack <dmatlack@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson <seanjc@google.com>,
	David Matlack <dmatlack@google.com>,
	Colton Lewis <coltonlewis@google.com>,
	Peter Xu <peterx@redhat.com>, Jim Mattson <jmattson@google.com>,
	Aaron Lewis <aaronlewis@google.com>,
	kvm@vger.kernel.org
Subject: [PATCH 1/4] KVM: selftests: Use MMIO to trigger emulation in emulator_error_test
Date: Thu, 29 Sep 2022 13:47:05 -0700	[thread overview]
Message-ID: <20220929204708.2548375-2-dmatlack@google.com> (raw)
In-Reply-To: <20220929204708.2548375-1-dmatlack@google.com>

Use MMIO to force KVM to emulate the flds isntruction in
emulator_error_test, rather than relying on KVM_CAP_SMALLER_MAXPHYADDR.

KVM_CAP_SMALLER_MAXPHYADDR is not enabled by default when TDP is
enabled. So developers that run all selftests against KVM in its default
configuration do not get test coverage of
KVM_CAP_EXIT_ON_EMULATION_FAILURE.

When TDP is disabled, KVM_CAP_SMALLER_MAXPHYADDR is enabled by default,
but emulator_error_test actually fails because KVM does not need to
emulate flds.  i.e. The test fails to induce and emulation failure.

Fixes: 39bbcc3a4e39 ("selftests: kvm: Allows userspace to handle emulation errors.")
Signed-off-by: David Matlack <dmatlack@google.com>
---
 .../kvm/x86_64/emulator_error_test.c          | 36 ++++++-------------
 1 file changed, 10 insertions(+), 26 deletions(-)

diff --git a/tools/testing/selftests/kvm/x86_64/emulator_error_test.c b/tools/testing/selftests/kvm/x86_64/emulator_error_test.c
index 236e11755ba6..2dff57991d31 100644
--- a/tools/testing/selftests/kvm/x86_64/emulator_error_test.c
+++ b/tools/testing/selftests/kvm/x86_64/emulator_error_test.c
@@ -11,18 +11,12 @@
 #include "kvm_util.h"
 #include "vmx.h"
 
-#define MAXPHYADDR 36
-
-#define MEM_REGION_GVA	0x0000123456789000
-#define MEM_REGION_GPA	0x0000000700000000
-#define MEM_REGION_SLOT	10
-#define MEM_REGION_SIZE PAGE_SIZE
+#define MMIO_REGION_GPA	0x700000000
+#define MMIO_REGION_GVA	0x700000000
 
 static void guest_code(void)
 {
-	__asm__ __volatile__("flds (%[addr])"
-			     :: [addr]"r"(MEM_REGION_GVA));
-
+	__asm__ __volatile__("flds (%[addr])" :: [addr]"r"(MMIO_REGION_GVA));
 	GUEST_DONE();
 }
 
@@ -152,34 +146,24 @@ int main(int argc, char *argv[])
 {
 	struct kvm_vcpu *vcpu;
 	struct kvm_vm *vm;
-	uint64_t gpa, pte;
-	uint64_t *hva;
 	int rc;
 
 	/* Tell stdout not to buffer its content */
 	setbuf(stdout, NULL);
 
-	TEST_REQUIRE(kvm_has_cap(KVM_CAP_SMALLER_MAXPHYADDR));
-
 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
 
-	vcpu_set_cpuid_maxphyaddr(vcpu, MAXPHYADDR);
-
 	rc = kvm_check_cap(KVM_CAP_EXIT_ON_EMULATION_FAILURE);
 	TEST_ASSERT(rc, "KVM_CAP_EXIT_ON_EMULATION_FAILURE is unavailable");
 	vm_enable_cap(vm, KVM_CAP_EXIT_ON_EMULATION_FAILURE, 1);
 
-	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
-				    MEM_REGION_GPA, MEM_REGION_SLOT,
-				    MEM_REGION_SIZE / PAGE_SIZE, 0);
-	gpa = vm_phy_pages_alloc(vm, MEM_REGION_SIZE / PAGE_SIZE,
-				 MEM_REGION_GPA, MEM_REGION_SLOT);
-	TEST_ASSERT(gpa == MEM_REGION_GPA, "Failed vm_phy_pages_alloc\n");
-	virt_map(vm, MEM_REGION_GVA, MEM_REGION_GPA, 1);
-	hva = addr_gpa2hva(vm, MEM_REGION_GPA);
-	memset(hva, 0, PAGE_SIZE);
-	pte = vm_get_page_table_entry(vm, vcpu, MEM_REGION_GVA);
-	vm_set_page_table_entry(vm, vcpu, MEM_REGION_GVA, pte | (1ull << 36));
+	/*
+	 * Create a virtual mapping so the guest can access MMIO_REGION_GPA.
+	 * MMIO_REGION_GPA is not mapped by a memslot so KVM will treat the
+	 * access as MMIO and attempt to emulate the flds instruction, which
+	 * will then generate an emulation error.
+	 */
+	virt_map(vm, MMIO_REGION_GVA, MMIO_REGION_GPA, 1);
 
 	vcpu_run(vcpu);
 	process_exit_on_emulation_error(vcpu);
-- 
2.38.0.rc1.362.ged0d419d3c-goog


  reply	other threads:[~2022-09-29 20:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-29 20:47 [PATCH 0/4] KVM: selftests: Fix and clean up emulator_error_test David Matlack
2022-09-29 20:47 ` David Matlack [this message]
2022-09-29 20:47 ` [PATCH 2/4] KVM: selftests: Delete dead ucall code from emulator_error_test David Matlack
2022-09-29 20:47 ` [PATCH 3/4] KVM: selftests: Skip emulator_error_test if KVM_CAP_EXIT_ON_EMULATION_FAILURE not available David Matlack
2022-09-29 20:47 ` [PATCH 4/4] KVM: selftests: Explicitly require instructions bytes in emulator_error_test David Matlack
2022-10-03 23:31 ` [PATCH 0/4] KVM: selftests: Fix and clean up emulator_error_test Sean Christopherson
2022-10-04 16:33   ` David Matlack
2022-10-04 20:30     ` Sean Christopherson

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=20220929204708.2548375-2-dmatlack@google.com \
    --to=dmatlack@google.com \
    --cc=aaronlewis@google.com \
    --cc=coltonlewis@google.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=seanjc@google.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