[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] libs/foreignmemory: implement the missing functions on FreeBSD


  • To: <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Roger Pau Monne <roger.pau@xxxxxxxxxx>
  • Date: Tue, 5 Jan 2021 11:25:45 +0100
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=bn1x1xofG0i+QAYYSC/0bk22fkyzrXgwirO+IHzjkEE=; b=EanZ0ut6MqaQiNMj216Z/kGTfGswPAnZyONRrCwrcwId92tBB7grFehPrnriBsjL0Yd5UQO3xrYhjhCsGNrQcQ0RI6EOpx+jrSB/J6XUHNHpKbmiC37EMgdiUNnQVFfv2TarSf6QgnwznLUHFcKeenlaaHRl/vdqCtaUspen5MGWqybOGOfNPsKd3EzjX7imjr7Bgh20zxQQSfwxjD9UXXPQ8F1orawaN8N2sNY13P6cMMeDXES7yBwf7zz3Cnid1GR+I3QqB26NT75nhCi3ZJB+RQBxomq9QuWrGi2zbCPvsGfmFrl8ZI/E31E4mT57mzDCKw52OLpEo+fGgxZkgg==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G0OSQuudgdjmCos59IfdGeCkYC8vBhspB+puOfRWlujDvapBBHPKiWTBrEmJtX/UDdV7Fbm3HcNAYPo7glp4DGPM0FX4tPg0I+GixCmcqSsoOcZihAX8H8gctxZZus0Lm9KghxaBEZY6K6YOWD5r1U6TBXkLM66HQ/PMFFThNGveV6oiVLYA8cfDEWahDtQv4SGwXHn/aARg/SQZSctUilyZ6EU1cOpVHJMC8GWQaXW70KjA1AO2UQR5TGFRwO8iUcrRhsVUoLdOjalrLFVi1WYuJnkLFc4J7RA9ZInKScV5iUsg10OWptCeUU6tAVLKQYLY+qi76k8L+Ip3URN1GA==
  • Authentication-results: esa6.hc3370-68.iphmx.com; dkim=pass (signature verified) header.i=@citrix.onmicrosoft.com
  • Cc: Roger Pau Monne <roger.pau@xxxxxxxxxx>, Ian Jackson <iwj@xxxxxxxxxxxxxx>, Wei Liu <wl@xxxxxxx>
  • Delivery-date: Tue, 05 Jan 2021 10:29:58 +0000
  • Ironport-sdr: Ho8BAJ21+zox3nbzfUMXrKAjZ74+9VqpxwBkdXww9gAvD2J1TP2NhCNvpp5lFtwtllJIXy7Vca OCFkV2EPmLI0oMOIRp7EmEuLokihZHKJ7VsFlWE5RxigXCYplAuV0bPBAw2/GqyiEKpOtSyA/L QhUWDl3Dhm6361qZQmBgGfVnV43qXrWrRUBL8aqcwRSR/nqsrsltUoj02dJW7X+yO6rC9/8KHs UdX0JzCdzZcUkvHeZP5ed9oMWrVcc/RPWj+6PRge0vMMW7906GUFgBDUoYmDYRBphMJ1dw9wpI Wos=
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

Implement restrict, map resource and unmap resource helpers on
FreeBSD.

Signed-off-by: Roger Pau Monné <roger.pau@xxxxxxxxxx>
---
Note the implementation is fairly similar to the Linux one, so could
likely be merged with some ifdefary. Note sure it's worth it given
that we already have a split file.
---
 tools/include/xen-sys/FreeBSD/privcmd.h | 14 +++++++
 tools/libs/foreignmemory/freebsd.c      | 51 +++++++++++++++++++++++++
 tools/libs/foreignmemory/private.h      |  2 +-
 3 files changed, 66 insertions(+), 1 deletion(-)

diff --git a/tools/include/xen-sys/FreeBSD/privcmd.h 
b/tools/include/xen-sys/FreeBSD/privcmd.h
index cf1241f039..603aad67d5 100644
--- a/tools/include/xen-sys/FreeBSD/privcmd.h
+++ b/tools/include/xen-sys/FreeBSD/privcmd.h
@@ -56,9 +56,23 @@ typedef struct privcmd_mmap_entry {
        unsigned long npages;
 } privcmd_mmap_entry_t;
 
+struct ioctl_privcmd_mmapresource {
+       domid_t dom; /* target domain */
+       unsigned int type; /* type of resource to map */
+       unsigned int id; /* type-specific resource identifier */
+       unsigned int idx; /* the index of the initial frame to be mapped */
+       unsigned long num; /* number of frames of the resource to be mapped */
+       unsigned long addr; /* physical address to map into */
+};
+typedef struct ioctl_privcmd_mmapresource privcmd_mmap_resource_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_RESTRICT                                 \
+       _IOW('E', 4, domid_t)
 
 #endif /* !__XEN_PRIVCMD_H__ */
diff --git a/tools/libs/foreignmemory/freebsd.c 
b/tools/libs/foreignmemory/freebsd.c
index 6e6bc4b11f..3d403a7cd0 100644
--- a/tools/libs/foreignmemory/freebsd.c
+++ b/tools/libs/foreignmemory/freebsd.c
@@ -95,6 +95,57 @@ int osdep_xenforeignmemory_unmap(xenforeignmemory_handle 
*fmem,
     return munmap(addr, num << PAGE_SHIFT);
 }
 
+int osdep_xenforeignmemory_restrict(xenforeignmemory_handle *fmem,
+                                    domid_t domid)
+{
+    return ioctl(fmem->fd, IOCTL_PRIVCMD_RESTRICT, &domid);
+}
+
+int osdep_xenforeignmemory_unmap_resource(xenforeignmemory_handle *fmem,
+                                        xenforeignmemory_resource_handle *fres)
+{
+    return fres ? munmap(fres->addr, fres->nr_frames << PAGE_SHIFT) : 0;
+}
+
+int osdep_xenforeignmemory_map_resource(xenforeignmemory_handle *fmem,
+                                        xenforeignmemory_resource_handle *fres)
+{
+    privcmd_mmap_resource_t mr = {
+        .dom = fres->domid,
+        .type = fres->type,
+        .id = fres->id,
+        .idx = fres->frame,
+        .num = fres->nr_frames,
+    };
+    int rc;
+
+    fres->addr = mmap(fres->addr, fres->nr_frames << PAGE_SHIFT,
+                      fres->prot, fres->flags | MAP_SHARED, fmem->fd, 0);
+    if ( fres->addr == MAP_FAILED )
+        return -1;
+
+    mr.addr = (uintptr_t)fres->addr;
+
+    rc = ioctl(fmem->fd, IOCTL_PRIVCMD_MMAP_RESOURCE, &mr);
+    if ( rc )
+    {
+        int saved_errno;
+
+        if ( errno != ENOSYS )
+            PERROR("mmap resource ioctl failed");
+        else
+            errno = EOPNOTSUPP;
+
+        saved_errno = errno;
+        osdep_xenforeignmemory_unmap_resource(fmem, fres);
+        errno = saved_errno;
+
+        return -1;
+    }
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libs/foreignmemory/private.h 
b/tools/libs/foreignmemory/private.h
index 8f1bf081ed..ebd45c4785 100644
--- a/tools/libs/foreignmemory/private.h
+++ b/tools/libs/foreignmemory/private.h
@@ -54,7 +54,7 @@ struct xenforeignmemory_resource_handle {
     int flags;
 };
 
-#ifndef __linux__
+#if !defined(__linux__) && !defined(__FreeBSD__)
 static inline int osdep_xenforeignmemory_restrict(xenforeignmemory_handle 
*fmem,
                                                   domid_t domid)
 {
-- 
2.29.2




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.