From: isaku.yamahata@intel.com
To: kvm@vger.kernel.org
Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com,
linux-kernel@vger.kernel.org,
Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Michael Roth <michael.roth@amd.com>,
David Matlack <dmatlack@google.com>,
Federico Parola <federico.parola@polito.it>
Subject: [RFC PATCH 8/8] KVM: selftests: x86: Add test for KVM_MAP_MEMORY
Date: Fri, 1 Mar 2024 09:28:50 -0800 [thread overview]
Message-ID: <ff83f599fc364276f4fd9a262770f88efdac4af4.1709288671.git.isaku.yamahata@intel.com> (raw)
In-Reply-To: <cover.1709288671.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <isaku.yamahata@intel.com>
Add a test case to exercise KVM_MAP_MEMORY and run the guest to access
pre-populated area. It tests KVM_MAP_MEMORY ioctl for KVM_X86_DEFAULT_VM
and KVM_X86_SW_PROTECTED_VM. The other VM type is just for future place
hodler.
Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
---
tools/include/uapi/linux/kvm.h | 14 ++
tools/testing/selftests/kvm/Makefile | 1 +
.../selftests/kvm/x86_64/map_memory_test.c | 136 ++++++++++++++++++
3 files changed, 151 insertions(+)
create mode 100644 tools/testing/selftests/kvm/x86_64/map_memory_test.c
diff --git a/tools/include/uapi/linux/kvm.h b/tools/include/uapi/linux/kvm.h
index c3308536482b..ea8d3cf840ab 100644
--- a/tools/include/uapi/linux/kvm.h
+++ b/tools/include/uapi/linux/kvm.h
@@ -2227,4 +2227,18 @@ struct kvm_create_guest_memfd {
__u64 reserved[6];
};
+#define KVM_MAP_MEMORY _IOWR(KVMIO, 0xd5, struct kvm_memory_mapping)
+
+#define KVM_MEMORY_MAPPING_FLAG_WRITE _BITULL(0)
+#define KVM_MEMORY_MAPPING_FLAG_EXEC _BITULL(1)
+#define KVM_MEMORY_MAPPING_FLAG_USER _BITULL(2)
+#define KVM_MEMORY_MAPPING_FLAG_PRIVATE _BITULL(3)
+
+struct kvm_memory_mapping {
+ __u64 base_gfn;
+ __u64 nr_pages;
+ __u64 flags;
+ __u64 source;
+};
+
#endif /* __LINUX_KVM_H */
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
index da20e6bb43ed..baef461ed38a 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -142,6 +142,7 @@ TEST_GEN_PROGS_x86_64 += set_memory_region_test
TEST_GEN_PROGS_x86_64 += steal_time
TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test
TEST_GEN_PROGS_x86_64 += system_counter_offset_test
+TEST_GEN_PROGS_x86_64 += x86_64/map_memory_test
# Compiled outputs used by test targets
TEST_GEN_PROGS_EXTENDED_x86_64 += x86_64/nx_huge_pages_test
diff --git a/tools/testing/selftests/kvm/x86_64/map_memory_test.c b/tools/testing/selftests/kvm/x86_64/map_memory_test.c
new file mode 100644
index 000000000000..9480c6c89226
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86_64/map_memory_test.c
@@ -0,0 +1,136 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 202r, Intel, Inc
+ *
+ * Author:
+ * Isaku Yamahata <isaku.yamahata at gmail.com>
+ */
+#include <linux/sizes.h>
+
+#include <test_util.h>
+#include <kvm_util.h>
+#include <processor.h>
+
+/* Arbitrarily chosen value. Pick 3G */
+#define TEST_GVA 0xc0000000
+#define TEST_GPA TEST_GVA
+#define TEST_SIZE (SZ_2M + PAGE_SIZE)
+#define TEST_NPAGES (TEST_SIZE / PAGE_SIZE)
+#define TEST_SLOT 10
+
+static void guest_code(uint64_t base_gpa)
+{
+ volatile uint64_t val __used;
+ int i;
+
+ for (i = 0; i < TEST_NPAGES; i++) {
+ uint64_t *src = (uint64_t *)(base_gpa + i * PAGE_SIZE);
+
+ val = *src;
+ }
+
+ GUEST_DONE();
+}
+
+static void map_memory(struct kvm_vcpu *vcpu, u64 base_gfn, u64 nr_pages,
+ u64 source, bool should_success)
+{
+ struct kvm_memory_mapping mapping = {
+ .base_gfn = base_gfn,
+ .nr_pages = nr_pages,
+ .flags = KVM_MEMORY_MAPPING_FLAG_WRITE,
+ .source = source,
+ };
+ int ret;
+
+ do {
+ ret = __vcpu_ioctl(vcpu, KVM_MAP_MEMORY, &mapping);
+ } while (ret && errno == EAGAIN);
+
+ if (should_success) {
+ __TEST_ASSERT_VM_VCPU_IOCTL(!ret, "KVM_MAP_MEMORY", ret, vcpu->vm);
+ } else {
+ __TEST_ASSERT_VM_VCPU_IOCTL(ret && errno == EFAULT,
+ "KVM_MAP_MEMORY", ret, vcpu->vm);
+ }
+}
+
+static void __test_map_memory(unsigned long vm_type, bool private, bool use_source)
+{
+ const struct vm_shape shape = {
+ .mode = VM_MODE_DEFAULT,
+ .type = vm_type,
+ };
+ struct kvm_vcpu *vcpu;
+ struct kvm_run *run;
+ struct kvm_vm *vm;
+ struct ucall uc;
+ u64 source;
+
+ vm = vm_create_shape_with_one_vcpu(shape, &vcpu, guest_code);
+ vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
+ TEST_GPA, TEST_SLOT, TEST_NPAGES,
+ private ? KVM_MEM_GUEST_MEMFD : 0);
+ virt_map(vm, TEST_GVA, TEST_GPA, TEST_NPAGES);
+
+ if (private)
+ vm_mem_set_private(vm, TEST_GPA, TEST_SIZE);
+
+ source = use_source ? TEST_GVA: 0;
+ map_memory(vcpu, TEST_GPA / PAGE_SIZE, SZ_2M / PAGE_SIZE, source, true);
+ source = use_source ? TEST_GVA + SZ_2M: 0;
+ map_memory(vcpu, (TEST_GPA + SZ_2M) / PAGE_SIZE, 1, source, true);
+
+ source = use_source ? TEST_GVA + TEST_SIZE : 0;
+ map_memory(vcpu, (TEST_GPA + TEST_SIZE) / PAGE_SIZE, 1, source, false);
+
+ vcpu_args_set(vcpu, 1, TEST_GVA);
+ vcpu_run(vcpu);
+
+ run = vcpu->run;
+ TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
+ "Wanted KVM_EXIT_IO, got exit reason: %u (%s)",
+ run->exit_reason, exit_reason_str(run->exit_reason));
+
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ break;
+ case UCALL_DONE:
+ break;
+ default:
+ TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
+ break;
+ }
+
+ kvm_vm_free(vm);
+}
+
+static void test_map_memory(unsigned long vm_type, bool use_source)
+{
+ if (!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type))) {
+ pr_info("Skipping tests for vm_type 0x%lx\n", vm_type);
+ return;
+ }
+
+ __test_map_memory(vm_type, false, use_source);
+ __test_map_memory(vm_type, true, use_source);
+}
+
+int main(int argc, char *argv[])
+{
+ TEST_REQUIRE(kvm_check_cap(KVM_CAP_MAP_MEMORY));
+
+ __test_map_memory(KVM_X86_DEFAULT_VM, false, false);
+ test_map_memory(KVM_X86_SW_PROTECTED_VM, false);
+#ifdef KVM_X86_SEV_VM
+ test_map_memory(KVM_X86_SEV_VM, false);
+#endif
+#ifdef KVM_X86_SEV_ES_VM
+ test_map_memory(KVM_X86_SEV_ES_VM, false);
+#endif
+#ifdef KVM_X86_TDX_VM
+ test_map_memory(KVM_X86_TDX_VM, true);
+#endif
+ return 0;
+}
--
2.25.1
next prev parent reply other threads:[~2024-03-01 17:29 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-01 17:28 [RFC PATCH 0/8] KVM: Prepopulate guest memory API isaku.yamahata
2024-03-01 17:28 ` [RFC PATCH 1/8] KVM: Document KVM_MAP_MEMORY ioctl isaku.yamahata
2024-03-07 0:43 ` David Matlack
2024-03-07 1:29 ` Isaku Yamahata
2024-03-07 12:30 ` Huang, Kai
2024-03-07 20:33 ` Isaku Yamahata
2024-03-08 0:20 ` Huang, Kai
2024-03-08 0:56 ` David Matlack
2024-03-08 1:28 ` Sean Christopherson
2024-03-08 2:19 ` Isaku Yamahata
2024-03-10 23:12 ` Michael Roth
2024-03-11 1:05 ` Huang, Kai
2024-03-11 1:08 ` Huang, Kai
2024-03-12 1:34 ` Isaku Yamahata
2024-03-01 17:28 ` [RFC PATCH 2/8] KVM: Add KVM_MAP_MEMORY vcpu ioctl to pre-populate guest memory isaku.yamahata
2024-03-07 0:49 ` David Matlack
2024-03-07 2:52 ` Isaku Yamahata
2024-03-07 12:45 ` Huang, Kai
2024-03-07 20:41 ` Isaku Yamahata
2024-03-11 17:23 ` Sean Christopherson
2024-03-11 22:19 ` Isaku Yamahata
2024-03-01 17:28 ` [RFC PATCH 3/8] KVM: x86/mmu: Introduce initialier macro for struct kvm_page_fault isaku.yamahata
2024-03-11 17:24 ` Sean Christopherson
2024-03-11 22:56 ` Isaku Yamahata
2024-03-01 17:28 ` [RFC PATCH 4/8] KVM: x86/mmu: Factor out kvm_mmu_do_page_fault() isaku.yamahata
2024-03-01 17:28 ` [RFC PATCH 5/8] KVM: x86/mmu: Introduce kvm_mmu_map_page() for prepopulating guest memory isaku.yamahata
2024-03-07 0:38 ` David Matlack
2024-03-19 15:53 ` Isaku Yamahata
2024-03-11 17:29 ` Sean Christopherson
2024-03-11 22:57 ` Isaku Yamahata
2024-03-01 17:28 ` [RFC PATCH 6/8] KVM: x86: Implement kvm_arch_{, pre_}vcpu_map_memory() isaku.yamahata
2024-03-07 0:30 ` David Matlack
2024-03-07 0:36 ` David Matlack
2024-03-07 1:51 ` Isaku Yamahata
2024-03-19 16:26 ` Isaku Yamahata
2024-04-03 23:15 ` Sean Christopherson
2024-03-07 1:34 ` Isaku Yamahata
2024-03-11 23:26 ` Sean Christopherson
2024-03-12 12:38 ` Huang, Kai
2024-03-12 14:20 ` Sean Christopherson
2024-03-12 21:41 ` Huang, Kai
2024-03-12 21:46 ` Huang, Kai
2024-03-12 23:03 ` Sean Christopherson
2024-03-01 17:28 ` [RFC PATCH 7/8] KVM: x86: Add hooks in kvm_arch_vcpu_map_memory() isaku.yamahata
2024-03-01 17:28 ` isaku.yamahata [this message]
2024-03-07 0:53 ` [RFC PATCH 0/8] KVM: Prepopulate guest memory API David Matlack
2024-03-07 2:09 ` Isaku Yamahata
2024-03-19 16:33 ` Isaku Yamahata
2024-04-03 18:30 ` Sean Christopherson
2024-04-03 22:00 ` Isaku Yamahata
2024-04-03 22:42 ` Sean Christopherson
2024-03-11 3:20 ` Michael Roth
2024-03-11 23:44 ` Sean Christopherson
2024-03-12 1:32 ` Isaku Yamahata
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=ff83f599fc364276f4fd9a262770f88efdac4af4.1709288671.git.isaku.yamahata@intel.com \
--to=isaku.yamahata@intel.com \
--cc=dmatlack@google.com \
--cc=federico.parola@polito.it \
--cc=isaku.yamahata@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=michael.roth@amd.com \
--cc=pbonzini@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;
as well as URLs for NNTP newsgroup(s).