]> xenbits.xensource.com Git - people/jgross/xen.git/commitdiff
libs/devicemodel: add dm_op support for FreeBSD
authorRoger Pau Monne <roger.pau@citrix.com>
Tue, 5 Jan 2021 10:25:46 +0000 (11:25 +0100)
committerWei Liu <wl@xen.org>
Tue, 5 Jan 2021 12:33:55 +0000 (12:33 +0000)
The FreeBSD ioctls have the same fields has the Linux ones, so the
same file can be shared between both OSes.

No functional change for OSes different than FreeBSD.

Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
Acked-by: Wei Liu <wl@xen.org>
tools/include/xen-sys/FreeBSD/privcmd.h
tools/libs/devicemodel/Makefile
tools/libs/devicemodel/common.c [new file with mode: 0644]
tools/libs/devicemodel/linux.c [deleted file]

index 603aad67d5b1e9c57e44dfca88d514dee5a6d7f2..649ad443c7aade34383b3423ddffd414ea96a2c3 100644 (file)
@@ -66,12 +66,27 @@ struct ioctl_privcmd_mmapresource {
 };
 typedef struct ioctl_privcmd_mmapresource privcmd_mmap_resource_t;
 
+struct privcmd_dmop_buf {
+       void *uptr; /* pointer to memory (in calling process) */
+       size_t size; /* size of the buffer */
+};
+typedef struct privcmd_dmop_buf privcmd_dm_op_buf_t;
+
+struct ioctl_privcmd_dmop {
+       domid_t dom; /* target domain */
+       unsigned int num; /* num of buffers */
+       const struct privcmd_dmop_buf *ubufs; /* array of buffers */
+};
+typedef struct ioctl_privcmd_dmop privcmd_dm_op_t;
+
 #define IOCTL_PRIVCMD_HYPERCALL                                        \
        _IOWR('E', 0, struct ioctl_privcmd_hypercall)
 #define IOCTL_PRIVCMD_MMAPBATCH                                        \
        _IOWR('E', 1, struct ioctl_privcmd_mmapbatch)
 #define IOCTL_PRIVCMD_MMAP_RESOURCE                            \
        _IOW('E', 2, struct ioctl_privcmd_mmapresource)
+#define IOCTL_PRIVCMD_DM_OP                                    \
+       _IOW('E', 3, struct ioctl_privcmd_dmop)
 #define IOCTL_PRIVCMD_RESTRICT                                 \
        _IOW('E', 4, domid_t)
 
index b67fc0fac10f7c00dc09fbd79781900d292a2917..500de7adc560b30d57e28d4de0b93dd1fb9087aa 100644 (file)
@@ -5,8 +5,8 @@ MAJOR    = 1
 MINOR    = 3
 
 SRCS-y                 += core.c
-SRCS-$(CONFIG_Linux)   += linux.c
-SRCS-$(CONFIG_FreeBSD) += compat.c
+SRCS-$(CONFIG_Linux)   += common.c
+SRCS-$(CONFIG_FreeBSD) += common.c
 SRCS-$(CONFIG_SunOS)   += compat.c
 SRCS-$(CONFIG_NetBSD)  += compat.c
 SRCS-$(CONFIG_MiniOS)  += compat.c
diff --git a/tools/libs/devicemodel/common.c b/tools/libs/devicemodel/common.c
new file mode 100644 (file)
index 0000000..0fdc712
--- /dev/null
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2017 Citrix Systems Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <xen/xen.h>
+#include <xen/sys/privcmd.h>
+
+#include "private.h"
+
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
+int osdep_xendevicemodel_open(xendevicemodel_handle *dmod)
+{
+    int fd = open("/dev/xen/privcmd", O_RDWR | O_CLOEXEC);
+    privcmd_dm_op_t uop;
+    int rc;
+
+    if (fd < 0) {
+        /*
+         * If the 'new' privcmd interface doesn't exist then don't treat
+         * this as an error, but an old privcmd clearly won't implement
+         * IOCTL_PRIVCMD_DM_OP so don't bother trying to open it.
+         */
+        if (errno == ENOENT || errno == ENXIO || errno == ENODEV)
+            goto out;
+
+        PERROR("Could not obtain handle on privileged command interface");
+        return -1;
+    }
+
+    /*
+     * Check to see if IOCTL_PRIVCMD_DM_OP is implemented as we want to
+     * use that in preference to libxencall.
+     */
+    uop.dom = DOMID_INVALID;
+    uop.num = 0;
+    uop.ubufs = NULL;
+
+    rc = ioctl(fd, IOCTL_PRIVCMD_DM_OP, &uop);
+    if (rc < 0) {
+        close(fd);
+        fd = -1;
+    }
+
+out:
+    dmod->fd = fd;
+    return 0;
+}
+
+int osdep_xendevicemodel_close(xendevicemodel_handle *dmod)
+{
+    if (dmod->fd < 0)
+        return 0;
+
+    return close(dmod->fd);
+}
+
+int osdep_xendevicemodel_op(xendevicemodel_handle *dmod,
+                            domid_t domid, unsigned int nr_bufs,
+                            struct xendevicemodel_buf bufs[])
+{
+    privcmd_dm_op_buf_t *ubufs;
+    privcmd_dm_op_t uop;
+    unsigned int i;
+    int rc;
+
+    if (dmod->fd < 0)
+        return xendevicemodel_xcall(dmod, domid, nr_bufs, bufs);
+
+    ubufs = calloc(nr_bufs, sizeof (*ubufs));
+    if (!ubufs)
+        return -1;
+
+    for (i = 0; i < nr_bufs; i++) {
+        ubufs[i].uptr = bufs[i].ptr;
+        ubufs[i].size = bufs[i].size;
+    }
+
+    uop.dom = domid;
+    uop.num = nr_bufs;
+    uop.ubufs = ubufs;
+
+    rc = ioctl(dmod->fd, IOCTL_PRIVCMD_DM_OP, &uop);
+
+    free(ubufs);
+
+    if (rc < 0)
+        return -1;
+
+    return 0;
+}
+
+int osdep_xendevicemodel_restrict(xendevicemodel_handle *dmod,
+                                  domid_t domid)
+{
+    if (dmod->fd < 0) {
+        errno = EOPNOTSUPP;
+        return -1;
+    }
+
+    return ioctl(dmod->fd, IOCTL_PRIVCMD_RESTRICT, &domid);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libs/devicemodel/linux.c b/tools/libs/devicemodel/linux.c
deleted file mode 100644 (file)
index 0fdc712..0000000
+++ /dev/null
@@ -1,138 +0,0 @@
-/*
- * Copyright (c) 2017 Citrix Systems Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation;
- * version 2.1 of the License.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <errno.h>
-#include <fcntl.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <sys/ioctl.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#include <xen/xen.h>
-#include <xen/sys/privcmd.h>
-
-#include "private.h"
-
-#ifndef O_CLOEXEC
-#define O_CLOEXEC 0
-#endif
-
-int osdep_xendevicemodel_open(xendevicemodel_handle *dmod)
-{
-    int fd = open("/dev/xen/privcmd", O_RDWR | O_CLOEXEC);
-    privcmd_dm_op_t uop;
-    int rc;
-
-    if (fd < 0) {
-        /*
-         * If the 'new' privcmd interface doesn't exist then don't treat
-         * this as an error, but an old privcmd clearly won't implement
-         * IOCTL_PRIVCMD_DM_OP so don't bother trying to open it.
-         */
-        if (errno == ENOENT || errno == ENXIO || errno == ENODEV)
-            goto out;
-
-        PERROR("Could not obtain handle on privileged command interface");
-        return -1;
-    }
-
-    /*
-     * Check to see if IOCTL_PRIVCMD_DM_OP is implemented as we want to
-     * use that in preference to libxencall.
-     */
-    uop.dom = DOMID_INVALID;
-    uop.num = 0;
-    uop.ubufs = NULL;
-
-    rc = ioctl(fd, IOCTL_PRIVCMD_DM_OP, &uop);
-    if (rc < 0) {
-        close(fd);
-        fd = -1;
-    }
-
-out:
-    dmod->fd = fd;
-    return 0;
-}
-
-int osdep_xendevicemodel_close(xendevicemodel_handle *dmod)
-{
-    if (dmod->fd < 0)
-        return 0;
-
-    return close(dmod->fd);
-}
-
-int osdep_xendevicemodel_op(xendevicemodel_handle *dmod,
-                            domid_t domid, unsigned int nr_bufs,
-                            struct xendevicemodel_buf bufs[])
-{
-    privcmd_dm_op_buf_t *ubufs;
-    privcmd_dm_op_t uop;
-    unsigned int i;
-    int rc;
-
-    if (dmod->fd < 0)
-        return xendevicemodel_xcall(dmod, domid, nr_bufs, bufs);
-
-    ubufs = calloc(nr_bufs, sizeof (*ubufs));
-    if (!ubufs)
-        return -1;
-
-    for (i = 0; i < nr_bufs; i++) {
-        ubufs[i].uptr = bufs[i].ptr;
-        ubufs[i].size = bufs[i].size;
-    }
-
-    uop.dom = domid;
-    uop.num = nr_bufs;
-    uop.ubufs = ubufs;
-
-    rc = ioctl(dmod->fd, IOCTL_PRIVCMD_DM_OP, &uop);
-
-    free(ubufs);
-
-    if (rc < 0)
-        return -1;
-
-    return 0;
-}
-
-int osdep_xendevicemodel_restrict(xendevicemodel_handle *dmod,
-                                  domid_t domid)
-{
-    if (dmod->fd < 0) {
-        errno = EOPNOTSUPP;
-        return -1;
-    }
-
-    return ioctl(dmod->fd, IOCTL_PRIVCMD_RESTRICT, &domid);
-}
-
-/*
- * Local variables:
- * mode: C
- * c-file-style: "BSD"
- * c-basic-offset: 4
- * tab-width: 4
- * indent-tabs-mode: nil
- * End:
- */