From: Ivan Orlov <iorlov@amazon.com>
To: <bp@alien8.de>, <dave.hansen@linux.intel.com>, <mingo@redhat.com>,
<pbonzini@redhat.com>, <seanjc@google.com>, <shuah@kernel.org>,
<tglx@linutronix.de>
Cc: Ivan Orlov <iorlov@amazon.com>, <hpa@zytor.com>,
<kvm@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<linux-kselftest@vger.kernel.org>, <x86@kernel.org>,
<jalliste@amazon.com>, <nh-open-source@amazon.com>,
<pdurrant@amazon.co.uk>
Subject: [PATCH 3/3] selftests: KVM: Add test case for MMIO during event delivery
Date: Fri, 27 Sep 2024 16:16:57 +0000 [thread overview]
Message-ID: <20240927161657.68110-4-iorlov@amazon.com> (raw)
In-Reply-To: <20240927161657.68110-1-iorlov@amazon.com>
Extend the 'set_memory_region_test' with a test case which covers the
MMIO during event delivery error handling. The test case
1) Tries to set an IDT descriptor base to point to an MMIO address
2) Generates a #GP
3) Verifies that we got a correct exit reason (KVM_EXIT_INTERNAL_ERROR)
and suberror code (KVM_INTERNAL_ERROR_DELIVERY_EV)
4) Verifies that we got a corrent "faulty" GPA in internal.data[3]
Signed-off-by: Ivan Orlov <iorlov@amazon.com>
---
.../selftests/kvm/set_memory_region_test.c | 46 +++++++++++++++++++
1 file changed, 46 insertions(+)
diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c
index a8267628e9ed..e9e97346edf1 100644
--- a/tools/testing/selftests/kvm/set_memory_region_test.c
+++ b/tools/testing/selftests/kvm/set_memory_region_test.c
@@ -553,6 +553,51 @@ static void test_add_overlapping_private_memory_regions(void)
close(memfd);
kvm_vm_free(vm);
}
+
+static const struct desc_ptr faulty_idt_desc = {
+ .address = MEM_REGION_GPA,
+ .size = 0xFFF,
+};
+
+static void guest_code_faulty_idt_desc(void)
+{
+ __asm__ __volatile__("lidt %0"::"m"(faulty_idt_desc));
+
+ /* Generate a #GP by dereferencing a non-canonical address */
+ *((uint8_t *)0xDEADBEEFDEADBEEFULL) = 0x1;
+
+ /* We should never reach this point */
+ GUEST_ASSERT(0);
+}
+
+/*
+ * This test tries to point the IDT descriptor base to an MMIO address. This action
+ * should cause a KVM internal error, so the VMM could handle such situations gracefully.
+ */
+static void test_faulty_idt_desc(void)
+{
+ struct kvm_vm *vm;
+ struct kvm_vcpu *vcpu;
+
+ pr_info("Testing a faulty IDT descriptor pointing to an MMIO address\n");
+
+ vm = vm_create_with_one_vcpu(&vcpu, guest_code_faulty_idt_desc);
+ virt_map(vm, MEM_REGION_GPA, MEM_REGION_GPA, 1);
+
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_INTERNAL_ERROR);
+ TEST_ASSERT(vcpu->run->internal.suberror == KVM_INTERNAL_ERROR_DELIVERY_EV,
+ "Unexpected suberror = %d", vcpu->run->internal.suberror);
+ TEST_ASSERT(vcpu->run->internal.ndata > 4, "Unexpected internal error data array size = %d",
+ vcpu->run->internal.ndata);
+
+ /* The "faulty" GPA address should be = IDT base + offset of the GP vector */
+ TEST_ASSERT(vcpu->run->internal.data[3] == MEM_REGION_GPA +
+ GP_VECTOR * sizeof(struct idt_entry),
+ "Unexpected GPA = %llx", vcpu->run->internal.data[3]);
+
+ kvm_vm_free(vm);
+}
#endif
int main(int argc, char *argv[])
@@ -568,6 +613,7 @@ int main(int argc, char *argv[])
* KVM_RUN fails with ENOEXEC or EFAULT.
*/
test_zero_memory_regions();
+ test_faulty_idt_desc();
#endif
test_invalid_memory_region_flags();
--
2.43.0
next prev parent reply other threads:[~2024-09-27 16:17 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-27 16:16 [PATCH 0/3] Handle MMIO during event delivery error on SVM Ivan Orlov
2024-09-27 16:16 ` [PATCH 1/3] KVM: x86, vmx: Add function for event delivery error generation Ivan Orlov
2024-10-11 23:20 ` Sean Christopherson
2024-10-12 0:06 ` Sean Christopherson
2024-10-15 19:52 ` Ivan Orlov
2024-10-16 21:05 ` Sean Christopherson
2024-10-16 22:05 ` Ivan Orlov
2024-10-16 23:12 ` Sean Christopherson
2024-09-27 16:16 ` [PATCH 2/3] KVM: vmx, svm, mmu: Process MMIO during event delivery Ivan Orlov
2024-10-12 0:05 ` Sean Christopherson
2024-10-16 22:53 ` Ivan Orlov
2024-10-17 16:20 ` Sean Christopherson
2024-09-27 16:16 ` Ivan Orlov [this message]
2024-10-12 0:21 ` [PATCH 3/3] selftests: KVM: Add test case for " Sean Christopherson
2024-10-17 16:27 ` David Woodhouse
2024-10-17 16:58 ` 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=20240927161657.68110-4-iorlov@amazon.com \
--to=iorlov@amazon.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=jalliste@amazon.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=nh-open-source@amazon.com \
--cc=pbonzini@redhat.com \
--cc=pdurrant@amazon.co.uk \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.