]> xenbits.xensource.com Git - xen.git/commitdiff
tools/tests: Introduce a test for acquire_resource
authorAndrew Cooper <andrew.cooper3@citrix.com>
Thu, 23 Jul 2020 16:26:16 +0000 (17:26 +0100)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Thu, 4 Feb 2021 22:03:33 +0000 (22:03 +0000)
For now, simply try to map 40 frames of grant table.  This catches most of the
basic errors with resource sizes found and fixed through the 4.15 dev window.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Tested-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>
Release-Acked-by: Ian Jackson <iwj@xenproject.org>
tools/tests/Makefile
tools/tests/resource/.gitignore [new file with mode: 0644]
tools/tests/resource/Makefile [new file with mode: 0644]
tools/tests/resource/test-resource.c [new file with mode: 0644]

index fc9b715951d88d26224910447e7606cc5f8842ed..8746aabe6b8e4068ad7779a6ec34cbf97e52a616 100644 (file)
@@ -2,6 +2,7 @@ XEN_ROOT = $(CURDIR)/../..
 include $(XEN_ROOT)/tools/Rules.mk
 
 SUBDIRS-y :=
+SUBDIRS-y += resource
 SUBDIRS-$(CONFIG_X86) += cpu-policy
 SUBDIRS-$(CONFIG_X86) += mce-test
 ifneq ($(clang),y)
diff --git a/tools/tests/resource/.gitignore b/tools/tests/resource/.gitignore
new file mode 100644 (file)
index 0000000..4872e97
--- /dev/null
@@ -0,0 +1 @@
+test-resource
diff --git a/tools/tests/resource/Makefile b/tools/tests/resource/Makefile
new file mode 100644 (file)
index 0000000..4bef482
--- /dev/null
@@ -0,0 +1,40 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+TARGET := test-resource
+
+.PHONY: all
+all: $(TARGET)
+
+.PHONY: run
+run: $(TARGET)
+       ./$(TARGET)
+
+.PHONY: clean
+clean:
+       $(RM) -f -- *.o $(TARGET) $(DEPS_RM)
+
+.PHONY: distclean
+distclean: clean
+       $(RM) -f -- *~
+
+.PHONY: install
+install: all
+
+.PHONY: uninstall
+uninstall:
+
+CFLAGS += -Werror
+CFLAGS += $(CFLAGS_xeninclude)
+CFLAGS += $(CFLAGS_libxenctrl)
+CFLAGS += $(CFLAGS_libxenforeginmemory)
+CFLAGS += $(APPEND_CFLAGS)
+
+LDFLAGS += $(LDLIBS_libxenctrl)
+LDFLAGS += $(LDLIBS_libxenforeignmemory)
+LDFLAGS += $(APPEND_LDFLAGS)
+
+test-resource: test-resource.o
+       $(CC) -o $@ $< $(LDFLAGS)
+
+-include $(DEPS_INCLUDE)
diff --git a/tools/tests/resource/test-resource.c b/tools/tests/resource/test-resource.c
new file mode 100644 (file)
index 0000000..a409a82
--- /dev/null
@@ -0,0 +1,151 @@
+#include <err.h>
+#include <errno.h>
+#include <error.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/mman.h>
+
+#include <xenctrl.h>
+#include <xenforeignmemory.h>
+#include <xen-tools/libs.h>
+
+static unsigned int nr_failures;
+#define fail(fmt, ...)                          \
+({                                              \
+    nr_failures++;                              \
+    (void)printf(fmt, ##__VA_ARGS__);           \
+})
+
+static xc_interface *xch;
+static xenforeignmemory_handle *fh;
+
+static void test_gnttab(uint32_t domid, unsigned int nr_frames)
+{
+    xenforeignmemory_resource_handle *res;
+    void *addr = NULL;
+    size_t size;
+    int rc;
+
+    printf("  Test grant table\n");
+
+    /* Obtain the grant table resource size. */
+    rc = xenforeignmemory_resource_size(
+        fh, domid, XENMEM_resource_grant_table,
+        XENMEM_resource_grant_table_id_shared, &size);
+
+    /*
+     * A failure of this call indicates missing kernel support for size
+     * ioctl(), or missing Xen acquire_resource support.
+     */
+    if ( rc )
+        return fail("    Fail: Get size: %d - %s\n", errno, strerror(errno));
+
+    /*
+     * Getting 32 frames back instead of nr_frames indicates Xen is missing
+     * the bugfix to make size requests actually return real data.
+     */
+    if ( (size >> XC_PAGE_SHIFT) != nr_frames )
+        return fail("    Fail: Get size: expected %u frames, got %zu\n",
+                    nr_frames, size >> XC_PAGE_SHIFT);
+
+    /* Map the entire grant table. */
+    res = xenforeignmemory_map_resource(
+        fh, domid, XENMEM_resource_grant_table,
+        XENMEM_resource_grant_table_id_shared, 0, size >> XC_PAGE_SHIFT,
+        &addr, PROT_READ | PROT_WRITE, 0);
+
+    /*
+     * Failure here with E2BIG indicates Xen is missing the bugfix to map
+     * resources larger than 32 frames.
+     */
+    if ( !res )
+        return fail("    Fail: Map %d - %s\n", errno, strerror(errno));
+
+    rc = xenforeignmemory_unmap_resource(fh, res);
+    if ( rc )
+        return fail("    Fail: Unmap %d - %s\n", errno, strerror(errno));
+}
+
+static void test_domain_configurations(void)
+{
+    static struct test {
+        const char *name;
+        struct xen_domctl_createdomain create;
+    } tests[] = {
+#if defined(__x86_64__) || defined(__i386__)
+        {
+            .name = "x86 PV",
+            .create = {
+                .max_vcpus = 2,
+                .max_grant_frames = 40,
+            },
+        },
+        {
+            .name = "x86 PVH",
+            .create = {
+                .flags = XEN_DOMCTL_CDF_hvm,
+                .max_vcpus = 2,
+                .max_grant_frames = 40,
+                .arch = {
+                    .emulation_flags = XEN_X86_EMU_LAPIC,
+                },
+            },
+        },
+#elif defined(__aarch64__) || defined(__arm__)
+        {
+            .name = "ARM",
+            .create = {
+                .flags = XEN_DOMCTL_CDF_hvm | XEN_DOMCTL_CDF_hap,
+                .max_vcpus = 2,
+                .max_grant_frames = 40,
+            },
+        },
+#endif
+    };
+
+    for ( unsigned int i = 0; i < ARRAY_SIZE(tests); ++i )
+    {
+        struct test *t = &tests[i];
+        uint32_t domid = 0;
+        int rc;
+
+        printf("Test %s\n", t->name);
+
+        rc = xc_domain_create(xch, &domid, &t->create);
+        if ( rc )
+        {
+            if ( errno == EINVAL || errno == EOPNOTSUPP )
+                printf("  Skip: %d - %s\n", errno, strerror(errno));
+            else
+                fail("  Domain create failure: %d - %s\n",
+                     errno, strerror(errno));
+            continue;
+        }
+
+        printf("  Created d%u\n", domid);
+
+        test_gnttab(domid, t->create.max_grant_frames);
+
+        rc = xc_domain_destroy(xch, domid);
+        if ( rc )
+            fail("  Failed to destroy domain: %d - %s\n",
+                 errno, strerror(errno));
+    }
+}
+
+int main(int argc, char **argv)
+{
+    printf("XENMEM_acquire_resource tests\n");
+
+    xch = xc_interface_open(NULL, NULL, 0);
+    fh = xenforeignmemory_open(NULL, 0);
+
+    if ( !xch )
+        err(1, "xc_interface_open");
+    if ( !fh )
+        err(1, "xenforeignmemory_open");
+
+    test_domain_configurations();
+
+    return !!nr_failures;
+}